From 686371913aeb6ad823320dfc6caf3a8e7d24e01e Mon Sep 17 00:00:00 2001 From: Jack Lloyd Date: Tue, 4 Apr 2017 14:55:11 -0400 Subject: Add block cipher interface to C API --- src/lib/ffi/ffi.cpp | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/lib/ffi/ffi.h | 45 +++++++++++++++++++++++++++++++ 2 files changed, 122 insertions(+) (limited to 'src/lib') diff --git a/src/lib/ffi/ffi.cpp b/src/lib/ffi/ffi.cpp index 945eacaf8..b4f0412d6 100644 --- a/src/lib/ffi/ffi.cpp +++ b/src/lib/ffi/ffi.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -217,6 +218,7 @@ struct botan_cipher_struct : public botan_struct BOTAN_FFI_DECLARE_STRUCT(botan_rng_struct, Botan::RandomNumberGenerator, 0x4901F9C1); BOTAN_FFI_DECLARE_STRUCT(botan_mp_struct, Botan::BigInt, 0xC828B9D2); +BOTAN_FFI_DECLARE_STRUCT(botan_block_cipher_struct, Botan::BlockCipher, 0x64C29716); BOTAN_FFI_DECLARE_STRUCT(botan_hash_struct, Botan::HashFunction, 0x1F0A4F84); BOTAN_FFI_DECLARE_STRUCT(botan_mac_struct, Botan::MessageAuthenticationCode, 0xA06E8FC1); BOTAN_FFI_DECLARE_STRUCT(botan_pubkey_struct, Botan::Public_Key, 0x2C286519); @@ -593,6 +595,81 @@ int botan_mp_num_bytes(const botan_mp_t mp, size_t* bytes) return BOTAN_FFI_DO(Botan::BigInt, mp, n, { *bytes = n.bytes(); }); } +int botan_block_cipher_init(botan_block_cipher_t* bc, const char* bc_name) + { + try + { + if(bc == nullptr || bc_name == nullptr || *bc_name == 0) + return BOTAN_FFI_ERROR_NULL_POINTER; + + std::unique_ptr cipher(Botan::BlockCipher::create(bc_name)); + if(cipher) + { + *bc = new botan_block_cipher_struct(cipher.release()); + return 0; + } + } + catch(std::exception& e) + { + log_exception(BOTAN_CURRENT_FUNCTION, e.what()); + } + catch(...) + { + log_exception(BOTAN_CURRENT_FUNCTION, "unknown"); + } + + return BOTAN_FFI_ERROR_EXCEPTION_THROWN; + + } + +/** +* Destroy a block cipher object +*/ +int botan_block_cipher_destroy(botan_block_cipher_t bc) + { + delete bc; + return 0; + } + +int botan_block_cipher_clear(botan_block_cipher_t bc) + { + return BOTAN_FFI_DO(Botan::BlockCipher, bc, b, { b.clear(); }); + } + +/** +* Set the key for a block cipher instance +*/ +int botan_block_cipher_set_key(botan_block_cipher_t bc, + const uint8_t key[], size_t len) + { + return BOTAN_FFI_DO(Botan::BlockCipher, bc, b, { b.set_key(key, len); }); + } + +/** +* Return the positive block size of this block cipher, or negative to +* indicate an error +*/ +int botan_block_cipher_block_size(botan_block_cipher_t bc) + { + return BOTAN_FFI_DO(Botan::BlockCipher, bc, b, { return b.block_size(); }); + } + +int botan_block_cipher_encrypt_blocks(botan_block_cipher_t bc, + const uint8_t in[], + uint8_t out[], + size_t blocks) + { + return BOTAN_FFI_DO(Botan::BlockCipher, bc, b, { b.encrypt_n(in, out, blocks); }); + } + +int botan_block_cipher_decrypt_blocks(botan_block_cipher_t bc, + const uint8_t in[], + uint8_t out[], + size_t blocks) + { + return BOTAN_FFI_DO(Botan::BlockCipher, bc, b, { b.decrypt_n(in, out, blocks); }); + } + int botan_hash_init(botan_hash_t* hash, const char* hash_name, uint32_t flags) { try diff --git a/src/lib/ffi/ffi.h b/src/lib/ffi/ffi.h index 0901c4d4d..76cdaa145 100644 --- a/src/lib/ffi/ffi.h +++ b/src/lib/ffi/ffi.h @@ -455,6 +455,51 @@ BOTAN_DLL int botan_bcrypt_generate(uint8_t* out, size_t* out_len, size_t work_factor, uint32_t flags); +/* +* Raw Block Cipher (PRP) interface +*/ +typedef struct botan_block_cipher_struct* botan_block_cipher_t; + +/** +* Initialize a block cipher object +*/ +BOTAN_DLL int botan_block_cipher_init(botan_block_cipher_t* bc, + const char* cipher_name); + +/** +* Destroy a block cipher object +*/ +BOTAN_DLL int botan_block_cipher_destroy(botan_block_cipher_t bc); + +/** +* Reinitializes the block cipher +* @return 0 on success, a negative value on failure +*/ +BOTAN_DLL int botan_block_cipher_clear(botan_block_cipher_t bc); + +/** +* Set the key for a block cipher instance +*/ +BOTAN_DLL int botan_block_cipher_set_key(botan_block_cipher_t bc, + const uint8_t key[], size_t len); + +/** +* Return the positive block size of this block cipher, or negative to +* indicate an error +*/ +BOTAN_DLL int botan_block_cipher_block_size(botan_block_cipher_t bc); + +BOTAN_DLL int botan_block_cipher_encrypt_blocks(botan_block_cipher_t bc, + const uint8_t in[], + uint8_t out[], + size_t blocks); + +BOTAN_DLL int botan_block_cipher_decrypt_blocks(botan_block_cipher_t bc, + const uint8_t in[], + uint8_t out[], + size_t blocks); + + /* * Multiple precision integers */ -- cgit v1.2.3