diff options
author | Jack Lloyd <[email protected]> | 2018-07-19 20:12:14 -0400 |
---|---|---|
committer | Jack Lloyd <[email protected]> | 2018-07-19 20:12:14 -0400 |
commit | 381ee690999318a86fa61a0ac54416230b5567da (patch) | |
tree | 0793d4c04458402cf2a6ce0d929b31d71501883d /src | |
parent | 9b379f6bcdd7fbe688013a1d76f863ef24caec39 (diff) |
Add FFI funcs to get algo name from cipher, MAC and hash objs
Diffstat (limited to 'src')
-rw-r--r-- | src/lib/ffi/ffi.h | 54 | ||||
-rw-r--r-- | src/lib/ffi/ffi_block.cpp | 6 | ||||
-rw-r--r-- | src/lib/ffi/ffi_cipher.cpp | 6 | ||||
-rw-r--r-- | src/lib/ffi/ffi_hash.cpp | 6 | ||||
-rw-r--r-- | src/lib/ffi/ffi_mac.cpp | 6 | ||||
-rw-r--r-- | src/lib/ffi/ffi_rng.cpp | 5 | ||||
-rw-r--r-- | src/tests/test_ffi.cpp | 64 |
7 files changed, 122 insertions, 25 deletions
diff --git a/src/lib/ffi/ffi.h b/src/lib/ffi/ffi.h index f62fe17d2..676a451f1 100644 --- a/src/lib/ffi/ffi.h +++ b/src/lib/ffi/ffi.h @@ -214,6 +214,18 @@ BOTAN_PUBLIC_API(2,0) int botan_rng_get(botan_rng_t rng, uint8_t* out, size_t ou BOTAN_PUBLIC_API(2,0) int botan_rng_reseed(botan_rng_t rng, size_t bits); /** +* Add some seed material to a random number generator +* +* @param rng rng object +* @param entropy the data to add +* @param entropy_len length of entropy buffer +* @return 0 on success, a negative value on failure +*/ +BOTAN_PUBLIC_API(2,8) int botan_rng_add_entropy(botan_rng_t rng, + const uint8_t* entropy, + size_t entropy_len); + +/** * Frees all resources of the random number generator object * @param rng rng object * @return always returns 0 @@ -296,9 +308,12 @@ BOTAN_PUBLIC_API(2,0) int botan_hash_clear(botan_hash_t hash); BOTAN_PUBLIC_API(2,0) int botan_hash_destroy(botan_hash_t hash); /** -* TODO has no implementation +* Get the name of this hash function +* @param hash the object to read +* @param name output buffer +* @param name_len on input, the length of buffer, on success the number of bytes written */ -BOTAN_PUBLIC_API(2,0) int botan_hash_name(botan_hash_t hash, char* name, size_t name_len); +BOTAN_PUBLIC_API(2,8) int botan_hash_name(botan_hash_t hash, char* name, size_t* name_len); /* * Message Authentication type @@ -360,6 +375,14 @@ BOTAN_PUBLIC_API(2,0) int botan_mac_final(botan_mac_t mac, uint8_t out[]); BOTAN_PUBLIC_API(2,0) int botan_mac_clear(botan_mac_t mac); /** +* Get the name of this MAC +* @param mac the object to read +* @param name output buffer +* @param name_len on input, the length of buffer, on success the number of bytes written +*/ +BOTAN_PUBLIC_API(2,8) int botan_mac_name(botan_mac_t mac, char* name, size_t* name_len); + +/** * Frees all resources of the MAC object * @param mac mac object * @return always returns 0 @@ -377,6 +400,8 @@ typedef struct botan_cipher_struct* botan_cipher_t; BOTAN_PUBLIC_API(2,0) int botan_cipher_init(botan_cipher_t* cipher, const char* name, uint32_t flags); +BOTAN_PUBLIC_API(2,8) int botan_cipher_name(botan_cipher_t cipher, char* name, size_t* name_len); + BOTAN_PUBLIC_API(2,0) int botan_cipher_valid_nonce_length(botan_cipher_t cipher, size_t nl); BOTAN_PUBLIC_API(2,0) int botan_cipher_get_tag_length(botan_cipher_t cipher, size_t* tag_size); BOTAN_PUBLIC_API(2,0) int botan_cipher_get_default_nonce_length(botan_cipher_t cipher, size_t* nl); @@ -493,7 +518,7 @@ typedef struct botan_block_cipher_struct* botan_block_cipher_t; * Initialize a block cipher object */ BOTAN_PUBLIC_API(2,1) int botan_block_cipher_init(botan_block_cipher_t* bc, - const char* cipher_name); + const char* cipher_name); /** * Destroy a block cipher object @@ -510,7 +535,7 @@ BOTAN_PUBLIC_API(2,1) int botan_block_cipher_clear(botan_block_cipher_t bc); * Set the key for a block cipher instance */ BOTAN_PUBLIC_API(2,1) int botan_block_cipher_set_key(botan_block_cipher_t bc, - const uint8_t key[], size_t len); + const uint8_t key[], size_t len); /** * Return the positive block size of this block cipher, or negative to @@ -519,14 +544,23 @@ BOTAN_PUBLIC_API(2,1) int botan_block_cipher_set_key(botan_block_cipher_t bc, BOTAN_PUBLIC_API(2,1) int botan_block_cipher_block_size(botan_block_cipher_t bc); BOTAN_PUBLIC_API(2,1) int botan_block_cipher_encrypt_blocks(botan_block_cipher_t bc, - const uint8_t in[], - uint8_t out[], - size_t blocks); + const uint8_t in[], + uint8_t out[], + size_t blocks); BOTAN_PUBLIC_API(2,1) int botan_block_cipher_decrypt_blocks(botan_block_cipher_t bc, - const uint8_t in[], - uint8_t out[], - size_t blocks); + const uint8_t in[], + uint8_t out[], + size_t blocks); + +/** +* Get the name of this block cipher +* @param cipher the object to read +* @param name output buffer +* @param name_len on input, the length of buffer, on success the number of bytes written +*/ +BOTAN_PUBLIC_API(2,8) int botan_block_cipher_name(botan_block_cipher_t cipher, + char* name, size_t* name_len); /* diff --git a/src/lib/ffi/ffi_block.cpp b/src/lib/ffi/ffi_block.cpp index f3648430a..fad492fd4 100644 --- a/src/lib/ffi/ffi_block.cpp +++ b/src/lib/ffi/ffi_block.cpp @@ -79,4 +79,10 @@ int botan_block_cipher_decrypt_blocks(botan_block_cipher_t bc, return BOTAN_FFI_DO(Botan::BlockCipher, bc, b, { b.decrypt_n(in, out, blocks); }); } +int botan_block_cipher_name(botan_block_cipher_t cipher, char* name, size_t* name_len) + { + return BOTAN_FFI_DO(Botan::BlockCipher, cipher, bc, { + return write_str_output(name, name_len, bc.name()); }); + } + } diff --git a/src/lib/ffi/ffi_cipher.cpp b/src/lib/ffi/ffi_cipher.cpp index 871cbd31f..094656fd2 100644 --- a/src/lib/ffi/ffi_cipher.cpp +++ b/src/lib/ffi/ffi_cipher.cpp @@ -193,4 +193,10 @@ int botan_cipher_get_tag_length(botan_cipher_t cipher, size_t* tl) return BOTAN_FFI_DO(Botan::Cipher_Mode, cipher, c, { *tl = c.tag_size(); }); } +int botan_cipher_name(botan_cipher_t cipher, char* name, size_t* name_len) + { + return BOTAN_FFI_DO(Botan::Cipher_Mode, cipher, c, { + return write_str_output(name, name_len, c.name()); }); + } + } diff --git a/src/lib/ffi/ffi_hash.cpp b/src/lib/ffi/ffi_hash.cpp index b5f9451d7..601d97c52 100644 --- a/src/lib/ffi/ffi_hash.cpp +++ b/src/lib/ffi/ffi_hash.cpp @@ -67,4 +67,10 @@ int botan_hash_copy_state(botan_hash_t* dest, const botan_hash_t source) *dest = new botan_hash_struct(src.copy_state().release()); }); } +int botan_hash_name(botan_hash_t hash, char* name, size_t* name_len) + { + return BOTAN_FFI_DO(Botan::HashFunction, hash, h, { + return write_str_output(name, name_len, h.name()); }); + } + } diff --git a/src/lib/ffi/ffi_mac.cpp b/src/lib/ffi/ffi_mac.cpp index 22dbb2513..02ac7900c 100644 --- a/src/lib/ffi/ffi_mac.cpp +++ b/src/lib/ffi/ffi_mac.cpp @@ -61,4 +61,10 @@ int botan_mac_final(botan_mac_t mac, uint8_t out[]) return BOTAN_FFI_DO(Botan::MessageAuthenticationCode, mac, m, { m.final(out); }); } +int botan_mac_name(botan_mac_t mac, char* name, size_t* name_len) + { + return BOTAN_FFI_DO(Botan::MessageAuthenticationCode, mac, m, { + return write_str_output(name, name_len, m.name()); }); + } + } diff --git a/src/lib/ffi/ffi_rng.cpp b/src/lib/ffi/ffi_rng.cpp index 6c27b2e7e..8d97a39d8 100644 --- a/src/lib/ffi/ffi_rng.cpp +++ b/src/lib/ffi/ffi_rng.cpp @@ -51,4 +51,9 @@ int botan_rng_reseed(botan_rng_t rng, size_t bits) return BOTAN_FFI_DO(Botan::RandomNumberGenerator, rng, r, { r.reseed_from_rng(Botan::system_rng(), bits); }); } +int botan_rng_add_entropy(botan_rng_t rng, const uint8_t* input, size_t len) + { + return BOTAN_FFI_DO(Botan::RandomNumberGenerator, rng, r, { r.add_entropy(input, len); }); + } + } diff --git a/src/tests/test_ffi.cpp b/src/tests/test_ffi.cpp index 916388778..c0afd9ac2 100644 --- a/src/tests/test_ffi.cpp +++ b/src/tests/test_ffi.cpp @@ -56,6 +56,10 @@ class FFI_Unit_Tests final : public Test { TEST_FFI_OK(botan_rng_get, (rng, outbuf.data(), outbuf.size())); TEST_FFI_OK(botan_rng_reseed, (rng, 256)); + + uint8_t not_really_entropy[32] = { 0 }; + TEST_FFI_OK(botan_rng_add_entropy, (rng, not_really_entropy, 32)); + // used for the rest of this function and destroyed at the end } else @@ -167,7 +171,6 @@ class FFI_Unit_Tests final : public Test std::string outstr; std::vector<uint8_t> outbuf; - //char namebuf[32]; outstr.resize(2 * bin.size()); TEST_FFI_OK(botan_hex_encode, (bin.data(), bin.size(), &outstr[0], 0)); @@ -404,6 +407,18 @@ class FFI_Unit_Tests final : public Test if(TEST_FFI_OK(botan_cipher_init, (&cipher_encrypt, "AES-128/GCM", BOTAN_CIPHER_INIT_FLAG_ENCRYPT))) { + char namebuf[18]; + size_t name_len = 15; + TEST_FFI_FAIL("output buffer too short", botan_cipher_name, (cipher_encrypt, namebuf, &name_len)); + result.test_eq("name len", name_len, 16); + + name_len = sizeof(namebuf); + if(TEST_FFI_OK(botan_cipher_name, (cipher_encrypt, namebuf, &name_len))) + { + result.test_eq("name len", name_len, 16); + result.test_eq("name", std::string(namebuf), "AES-128/GCM(16)"); + } + size_t min_keylen = 0; size_t max_keylen = 0; size_t nonce_len = 0; @@ -632,14 +647,18 @@ class FFI_Unit_Tests final : public Test if(TEST_FFI_OK(botan_hash_init, (&hash, "SHA-256", 0))) { - /* - TEST_FFI_FAIL("output buffer too short", botan_hash_name, (hash, namebuf, 5)); + char namebuf[10]; + size_t name_len = 7; + TEST_FFI_FAIL("output buffer too short", botan_hash_name, (hash, namebuf, &name_len)); + result.test_eq("name len", name_len, 8); + + name_len = sizeof(namebuf); + if(TEST_FFI_OK(botan_hash_name, (hash, namebuf, &name_len))) + { + result.test_eq("name len", name_len, 8); + result.test_eq("name", std::string(namebuf), "SHA-256"); + } - if(TEST_FFI_OK(botan_hash_name, (hash, namebuf, sizeof(namebuf)))) - { - result.test_eq("hash name", std::string(namebuf), "SHA-256"); - } - */ size_t block_size; if (TEST_FFI_OK(botan_hash_block_size, (hash, &block_size))) { @@ -705,14 +724,17 @@ class FFI_Unit_Tests final : public Test if(TEST_FFI_OK(botan_mac_init, (&mac, "HMAC(SHA-256)", 0))) { - /* - TEST_FFI_FAIL("output buffer too short", botan_mac_name, (mac, namebuf, 5)); + char namebuf[16]; + size_t name_len = 13; + TEST_FFI_FAIL("output buffer too short", botan_mac_name, (mac, namebuf, &name_len)); + result.test_eq("name len", name_len, 14); - if(TEST_FFI_OK(botan_mac_name, (mac, namebuf, 20))) - { - result.test_eq("mac name", std::string(namebuf), "HMAC(SHA-256)"); - } - */ + name_len = sizeof(namebuf); + if(TEST_FFI_OK(botan_mac_name, (mac, namebuf, &name_len))) + { + result.test_eq("name len", name_len, 14); + result.test_eq("name", std::string(namebuf), "HMAC(SHA-256)"); + } size_t output_len; if(TEST_FFI_OK(botan_mac_output_length, (mac, &output_len))) @@ -819,6 +841,18 @@ class FFI_Unit_Tests final : public Test if(TEST_FFI_OK(botan_block_cipher_init, (&cipher, "AES-128"))) { + char namebuf[10]; + size_t name_len = 7; + TEST_FFI_FAIL("output buffer too short", botan_block_cipher_name, (cipher, namebuf, &name_len)); + result.test_eq("name len", name_len, 8); + + name_len = sizeof(namebuf); + if(TEST_FFI_OK(botan_block_cipher_name, (cipher, namebuf, &name_len))) + { + result.test_eq("name len", name_len, 8); + result.test_eq("name", std::string(namebuf), "AES-128"); + } + const std::vector<uint8_t> zero16(16, 0); std::vector<uint8_t> block(16, 0); |