diff options
author | Daniel Wyatt <[email protected]> | 2017-05-25 22:15:48 -0400 |
---|---|---|
committer | Daniel Wyatt <[email protected]> | 2017-05-25 22:15:48 -0400 |
commit | da5871a12ca8f5031b1945e3fcca88921ae8ebd8 (patch) | |
tree | 4ab3cf5feb08073f761804841d869b4a768c1a6a | |
parent | d8fc6ff06f955358286973e05142dc6c5066f2b8 (diff) |
Add botan_hash_copy_state FFI function.
-rw-r--r-- | doc/manual/ffi.rst | 4 | ||||
-rw-r--r-- | src/lib/ffi/ffi.cpp | 6 | ||||
-rw-r--r-- | src/lib/ffi/ffi.h | 8 | ||||
-rw-r--r-- | src/tests/test_ffi.cpp | 20 |
4 files changed, 38 insertions, 0 deletions
diff --git a/doc/manual/ffi.rst b/doc/manual/ffi.rst index 24b2aaa38..89e075885 100644 --- a/doc/manual/ffi.rst +++ b/doc/manual/ffi.rst @@ -149,6 +149,10 @@ Hash Functions Destroy the object created by :cpp:func:`botan_hash_init`. +.. cpp:function:: int botan_hash_copy_state(botan_hash_t* dest, const botan_hash_t source) + + Copies the state of the hash object to a new hash object. + .. cpp:function:: int botan_hash_clear(botan_hash_t hash) Reset the state of this object back to clean, as if no input has diff --git a/src/lib/ffi/ffi.cpp b/src/lib/ffi/ffi.cpp index 3686c26f1..7e96e5514 100644 --- a/src/lib/ffi/ffi.cpp +++ b/src/lib/ffi/ffi.cpp @@ -852,6 +852,12 @@ int botan_hash_final(botan_hash_t hash, uint8_t out[]) return BOTAN_FFI_DO(Botan::HashFunction, hash, h, { h.final(out); }); } +int botan_hash_copy_state(botan_hash_t* dest, const botan_hash_t source) + { + return BOTAN_FFI_DO(Botan::HashFunction, source, src, { + *dest = new botan_hash_struct(src.copy_state().release()); }); + } + int botan_mac_init(botan_mac_t* mac, const char* mac_name, uint32_t flags) { try diff --git a/src/lib/ffi/ffi.h b/src/lib/ffi/ffi.h index 4953bce06..5ce86a9b0 100644 --- a/src/lib/ffi/ffi.h +++ b/src/lib/ffi/ffi.h @@ -225,6 +225,14 @@ typedef struct botan_hash_struct* botan_hash_t; BOTAN_DLL int botan_hash_init(botan_hash_t* hash, const char* hash_name, uint32_t flags); /** +* Copy the state of a hash function object +* @param dest destination hash object +* @param source source hash object +* @return 0 on success, a negative value on failure +*/ +BOTAN_DLL int botan_hash_copy_state(botan_hash_t *dest, const botan_hash_t source); + +/** * Writes the output length of the hash function to *output_length * @param hash hash object * @param output_length output buffer to hold the hash function output length diff --git a/src/tests/test_ffi.cpp b/src/tests/test_ffi.cpp index 624b01032..cf97dd947 100644 --- a/src/tests/test_ffi.cpp +++ b/src/tests/test_ffi.cpp @@ -131,6 +131,26 @@ class FFI_Unit_Tests : public Test result.test_eq("SHA-256 output", outbuf, "B5D4045C3F466FA91FE2CC6ABE79232A1A57CDF104F7A26E716E0A1E2789DF78"); } + // Test botan_hash_copy_state + const char *msg = "message digest"; + const char *expected = "F7846F55CF23E14EEBEAB5B4E1550CAD5B509E3348FBC4EFA3A1413D393CB650"; + TEST_FFI_OK(botan_hash_clear, (hash)); + TEST_FFI_OK(botan_hash_update, (hash, reinterpret_cast<const uint8_t*>(&msg[0]), 1)); + botan_hash_t fork; + if (TEST_FFI_OK(botan_hash_copy_state, (&fork, hash))) + { + TEST_FFI_OK(botan_hash_update, (fork, reinterpret_cast<const uint8_t*>(&msg[1]), std::strlen(msg) - 2)); + + TEST_FFI_OK(botan_hash_update, (hash, reinterpret_cast<const uint8_t*>(&msg[1]), std::strlen(msg) - 1)); + TEST_FFI_OK(botan_hash_final, (hash, outbuf.data())); + result.test_eq("hashing split", outbuf, expected); + + TEST_FFI_OK(botan_hash_update, (fork, reinterpret_cast<const uint8_t*>(&msg[std::strlen(msg)-1]), 1)); + TEST_FFI_OK(botan_hash_final, (fork, outbuf.data())); + result.test_eq("hashing split", outbuf, expected); + + TEST_FFI_OK(botan_hash_destroy, (fork)); + } } TEST_FFI_OK(botan_hash_destroy, (hash)); |