diff options
-rw-r--r-- | include/axolotl/crypto.hh | 88 | ||||
-rw-r--r-- | src/crypto.cpp | 223 | ||||
-rw-r--r-- | src/libs.cpp | 5 | ||||
-rw-r--r-- | tests/test_crypto.cpp | 100 |
4 files changed, 416 insertions, 0 deletions
diff --git a/include/axolotl/crypto.hh b/include/axolotl/crypto.hh new file mode 100644 index 0000000..9691228 --- /dev/null +++ b/include/axolotl/crypto.hh @@ -0,0 +1,88 @@ +#include <cstdint> + +namespace axolotl { + + +struct Curve25519PublicKey { + static const int LENGTH = 32; + std::uint8_t public_key[32]; +}; + + +struct Curve25519KeyPair : public Curve25519PublicKey { + std::uint8_t private_key[32]; +}; + + +Curve25519KeyPair generate_key( + std::uint8_t const * random_32_bytes +); + + +const std::size_t CURVE25519_SHARED_SECRET_LENGTH = 16; + + +void curve25519_shared_secret( + Curve25519KeyPair const & our_key, + Curve25519PublicKey const & their_key, + std::uint8_t * output +); + + +struct Aes256Key { + static const int LENGTH = 32; + std::uint8_t key[32]; +}; + + +struct Aes256Iv { + static const int LENGTH = 16; + std::uint8_t iv[16]; +}; + + +std::size_t aes_pkcs_7_padded_length( + std::size_t input_length +); + + +void aes_pkcs_7_padding( + std::uint8_t const * input, std::size_t input_length, + std::uint8_t * output +); + + +void aes_encrypt_cbc( + Aes256Key const & key, + Aes256Iv const & iv, + std::uint8_t const * input, std::size_t input_length, + std::uint8_t * output +); + + +void aes_decrypt_cbc( + Aes256Key const & key, + Aes256Iv const & iv, + std::uint8_t const * input, std::size_t input_length, + std::uint8_t * output +); + + +const std::size_t HMAC_SHA256_OUTPUT_LENGTH = 32; + + +void hmac_sha256( + std::uint8_t const * key, std::size_t key_length, + std::uint8_t const * input, std::size_t input_length, + std::uint8_t * output +); + + +void hkdf_sha256( + std::uint8_t const * input, std::size_t input_length, + std::uint8_t const * info, std::size_t info_length, + std::uint8_t const * salt, std::size_t salt_length, + std::uint8_t * output, std::size_t output_length +); + +} // namespace axolotl diff --git a/src/crypto.cpp b/src/crypto.cpp new file mode 100644 index 0000000..c1ef88a --- /dev/null +++ b/src/crypto.cpp @@ -0,0 +1,223 @@ +#include "axolotl/crypto.hh" +#include <cstring> + +extern "C" { + +int curve25519_donna( + uint8_t * output, + const uint8_t * secret, + const uint8_t * basepoint +); + +#include "crypto-algorithms/aes.h" +#include "crypto-algorithms/sha256.h" + +} + + +namespace { + +static const std::uint8_t CURVE25519_BASEPOINT[32] = {9}; +static const std::size_t AES_BLOCK_LENGTH = 16; +static const std::size_t SHA256_BLOCK_LENGTH = 32; +static const std::uint8_t HKDF_DEFAULT_SALT[32] = {}; + +template<std::size_t block_size> +inline static void xor_block( + std::uint8_t * block, + std::uint8_t const * input +) { + for (std::size_t i = 0; i < block_size; ++i) { + block[i] ^= input[i]; + } +} + + +inline static void hmac_sha256_key( + std::uint8_t const * input_key, std::size_t input_key_length, + std::uint8_t * hmac_key +) { + if (input_key_length > SHA256_BLOCK_LENGTH) { + ::SHA256_CTX context; + ::sha256_init(&context); + ::sha256_update(&context, input_key, input_key_length); + ::sha256_final(&context, hmac_key); + } else { + std::memset(hmac_key, 0, SHA256_BLOCK_LENGTH); + std::memcpy(hmac_key, input_key, input_key_length); + } +} + + +inline void hmac_sha256_init( + ::SHA256_CTX * context, + std::uint8_t const * hmac_key +) { + std::uint8_t i_pad[SHA256_BLOCK_LENGTH]; + std::memcpy(i_pad, hmac_key, SHA256_BLOCK_LENGTH); + for (std::size_t i = 0; i < SHA256_BLOCK_LENGTH; ++i) { + i_pad[i] ^= 0x5C; + } + ::sha256_init(context); + ::sha256_update(context, i_pad, SHA256_BLOCK_LENGTH); + std::memset(i_pad, 0, sizeof(i_pad)); +} + + +inline void hmac_sha256_final( + ::SHA256_CTX * context, + std::uint8_t const * hmac_key, + std::uint8_t * output +) { + std::uint8_t o_pad[SHA256_BLOCK_LENGTH]; + std::memcpy(o_pad, hmac_key, SHA256_BLOCK_LENGTH); + for (std::size_t i = 0; i < SHA256_BLOCK_LENGTH; ++i) { + o_pad[i] ^= 0x36; + } + ::SHA256_CTX final_context; + ::sha256_init(&final_context); + ::sha256_update(&final_context, o_pad, SHA256_BLOCK_LENGTH); + ::sha256_final(context, o_pad); + ::sha256_update(&final_context, o_pad, SHA256_BLOCK_LENGTH); + ::sha256_final(&final_context, output); + std::memset(o_pad, 0, sizeof(o_pad)); +} + +} // namespace + + +axolotl::Curve25519KeyPair axolotl::generate_key( + std::uint8_t const * random_32_bytes +) { + axolotl::Curve25519KeyPair key_pair; + std::memcpy(key_pair.private_key, random_32_bytes, 32); + ::curve25519_donna( + key_pair.public_key, key_pair.private_key, CURVE25519_BASEPOINT + ); + return key_pair; +} + + +void axolotl::curve25519_shared_secret( + axolotl::Curve25519KeyPair const & our_key, + axolotl::Curve25519PublicKey const & their_key, + std::uint8_t * output +) { + ::curve25519_donna(output, our_key.private_key, their_key.public_key); +} + + +std::size_t axolotl::aes_pkcs_7_padded_length( + std::size_t input_length +) { + return input_length + AES_BLOCK_LENGTH - input_length % AES_BLOCK_LENGTH; +} + + +void axolotl::aes_pkcs_7_padding( + std::uint8_t const * input, std::size_t input_length, + std::uint8_t * output +) { + std::memcpy(output, input, input_length); + std::size_t padded_length = axolotl::aes_pkcs_7_padded_length(input_length); + std::uint8_t padding = padded_length - input_length; + for (std::size_t i = input_length; i < padded_length; ++i) { + output[i] = padding; + } +} + + +void axolotl::aes_encrypt_cbc( + axolotl::Aes256Key const & key, + axolotl::Aes256Iv const & iv, + std::uint8_t const * input, std::size_t input_length, + std::uint8_t * output +) { + std::uint32_t key_schedule[60]; + ::aes_key_setup(key.key, key_schedule, 256); + std::uint8_t input_block[AES_BLOCK_LENGTH]; + std::memcpy(input_block, iv.iv, AES_BLOCK_LENGTH); + for (std::size_t i = 0; i < input_length; i += AES_BLOCK_LENGTH) { + xor_block<AES_BLOCK_LENGTH>(input_block, &input[i]); + ::aes_encrypt(input_block, &output[i], key_schedule, 256); + std::memcpy(input_block, &output[i], AES_BLOCK_LENGTH); + } + std::memset(key_schedule, 0, sizeof(key_schedule)); + std::memset(input_block, 0, sizeof(AES_BLOCK_LENGTH)); +} + + +void axolotl::aes_decrypt_cbc( + axolotl::Aes256Key const & key, + axolotl::Aes256Iv const & iv, + std::uint8_t const * input, std::size_t input_length, + std::uint8_t * output +) { + std::uint32_t key_schedule[60]; + ::aes_key_setup(key.key, key_schedule, 256); + for (std::size_t i = 0; i < input_length; i += AES_BLOCK_LENGTH) { + ::aes_decrypt(&input[i], &output[i], key_schedule, 256); + if (i == 0) { + xor_block<AES_BLOCK_LENGTH>(&output[i], iv.iv); + } else { + xor_block<AES_BLOCK_LENGTH>(&output[i], &input[i - AES_BLOCK_LENGTH]); + } + } + std::memset(key_schedule, 0, sizeof(key_schedule)); +} + + +void axolotl::hmac_sha256( + std::uint8_t const * key, std::size_t key_length, + std::uint8_t const * input, std::size_t input_length, + std::uint8_t * output +) { + std::uint8_t hmac_key[SHA256_BLOCK_LENGTH]; + ::SHA256_CTX context; + hmac_sha256_key(key, key_length, hmac_key); + hmac_sha256_init(&context, hmac_key); + ::sha256_update(&context, input, input_length); + hmac_sha256_final(&context, hmac_key, output); + std::memset(hmac_key, 0, sizeof(hmac_key)); +} + + +void axolotl::hkdf_sha256( + std::uint8_t const * input, std::size_t input_length, + std::uint8_t const * info, std::size_t info_length, + std::uint8_t const * salt, std::size_t salt_length, + std::uint8_t * output, std::size_t output_length +) { + ::SHA256_CTX context; + std::uint8_t extract_key[SHA256_BLOCK_LENGTH]; + std::uint8_t expand_key[SHA256_BLOCK_LENGTH]; + std::uint8_t step_result[SHA256_BLOCK_LENGTH]; + std::size_t bytes_remaining = output_length; + std::uint8_t iteration = 1; + if (!salt) { + salt = HKDF_DEFAULT_SALT; + salt_length = sizeof(HKDF_DEFAULT_SALT); + } + /* Expand */ + hmac_sha256_key(salt, salt_length, extract_key); + hmac_sha256_init(&context, extract_key); + ::sha256_update(&context, input, input_length); + hmac_sha256_final(&context, extract_key, expand_key); + /* Extract */ + hmac_sha256_init(&context, expand_key); + ::sha256_update(&context, info, info_length); + ::sha256_update(&context, &iteration, 1); + hmac_sha256_final(&context, expand_key, step_result); + while (bytes_remaining > SHA256_BLOCK_LENGTH) { + std::memcpy(output, step_result, SHA256_BLOCK_LENGTH); + output += SHA256_BLOCK_LENGTH; + bytes_remaining -= SHA256_BLOCK_LENGTH; + iteration ++; + hmac_sha256_init(&context, expand_key); + ::sha256_update(&context, step_result, SHA256_BLOCK_LENGTH); + ::sha256_update(&context, info, info_length); + ::sha256_update(&context, &iteration, 1); + hmac_sha256_final(&context, expand_key, step_result); + } + std::memcpy(output, step_result, bytes_remaining); +} diff --git a/src/libs.cpp b/src/libs.cpp new file mode 100644 index 0000000..5b9c2aa --- /dev/null +++ b/src/libs.cpp @@ -0,0 +1,5 @@ +extern "C" { +#include "crypto-algorithms/sha256.c" +#include "crypto-algorithms/aes.c" +#include "curve25519-donna/curve25519-donna.c" +} diff --git a/tests/test_crypto.cpp b/tests/test_crypto.cpp new file mode 100644 index 0000000..ed50905 --- /dev/null +++ b/tests/test_crypto.cpp @@ -0,0 +1,100 @@ +#include "axolotl/crypto.hh" + +#include <cstring> +#include <iostream> +#include <iomanip> +#include <cstdlib> + +std::ostream & print_hex( + std::ostream & os, + std::uint8_t const * data, + std::size_t length +) { + for (std::size_t i = 0; i < length; i++) { + std::cerr << std::setw(2) << std::setfill('0') << std::right + << std::hex << (int) data[i]; + } + return os; +} + +char const * TEST_CASE; + +void assert_equals( + std::uint8_t *expected, + std::uint8_t *actual, + std::size_t length +) { + if (std::memcmp(expected, actual, length)) { + std::cerr << "FAILED :" << TEST_CASE << std::endl; + print_hex(std::cerr << "Expected: ", expected, length) << std::endl; + print_hex(std::cerr << "Actual: ", actual, length) << std::endl; + std::exit(1); + } +} + + +int main() { + + + + +{ /* HDKF Test Case 1 */ + +TEST_CASE = "HDKF Test Case 1"; + +std::uint8_t input[22] = { + 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, + 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, + 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b +}; + +std::uint8_t salt[13] = { + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x08, 0x09, 0x0a, 0x0b, 0x0c +}; + +std::uint8_t info[10] = { + 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, + 0xf8, 0xf9 +}; + +std::uint8_t hmac_expected_output[32] = { + 0x07, 0x77, 0x09, 0x36, 0x2c, 0x2e, 0x32, 0xdf, + 0x0d, 0xdc, 0x3f, 0x0d, 0xc4, 0x7b, 0xba, 0x63, + 0x90, 0xb6, 0xc7, 0x3b, 0xb5, 0x0f, 0x9c, 0x31, + 0x22, 0xec, 0x84, 0x4a, 0xd7, 0xc2, 0xb3, 0xe5, +}; + +std::uint8_t hmac_actual_output[32] = {}; + +axolotl::hmac_sha256( + salt, 0 * sizeof(salt), + input, 0 * sizeof(input), + hmac_actual_output +); + +assert_equals(hmac_expected_output, hmac_actual_output, 32); + +std::uint8_t hkdf_expected_output[42] { + 0x3c, 0xb2, 0x5f, 0x25, 0xfa, 0xac, 0xd5, 0x7a, + 0x90, 0x43, 0x4f, 0x64, 0xd0, 0x36, 0x2f, 0x2a, + 0x2d, 0x2d, 0x0a, 0x90, 0xcf, 0x1a, 0x5a, 0x4c, + 0x5d, 0xb0, 0x2d, 0x56, 0xec, 0xc4, 0xc5, 0xbf, + 0x34, 0x00, 0x72, 0x08, 0xd5, 0xb8, 0x87, 0x18, + 0x58, 0x65 +}; + +std::uint8_t hkdf_actual_output[42] = {}; + +axolotl::hkdf_sha256( + input, sizeof(input), + salt, sizeof(salt), + info, sizeof(info), + hkdf_actual_output, sizeof(hkdf_actual_output) +); + +assert_equals(hkdf_expected_output, hkdf_actual_output, 42); + +} /* HDKF Test Case 1 */ + +} |