diff options
-rw-r--r-- | lib/aes.h | 44 | ||||
-rw-r--r-- | lib/aes_decrypt.c | 12 | ||||
-rw-r--r-- | lib/aes_encrypt.c | 10 | ||||
-rw-r--r-- | lib/cbc_mode.c | 48 | ||||
-rw-r--r-- | lib/cbc_mode.h | 40 | ||||
-rw-r--r-- | lib/ctr_mode.c | 16 | ||||
-rw-r--r-- | lib/ctr_mode.h | 20 | ||||
-rw-r--r-- | lib/hmac.c | 64 | ||||
-rw-r--r-- | lib/hmac.h | 50 | ||||
-rw-r--r-- | lib/hmac_prng.c | 72 | ||||
-rw-r--r-- | lib/hmac_prng.h | 50 | ||||
-rw-r--r-- | lib/sha256.c | 22 | ||||
-rw-r--r-- | lib/sha256.h | 33 | ||||
-rw-r--r-- | lib/utils.h | 4 | ||||
-rw-r--r-- | tests/test_aes.c | 30 | ||||
-rw-r--r-- | tests/test_cbc_mode.c | 16 | ||||
-rw-r--r-- | tests/test_ctr_mode.c | 8 | ||||
-rw-r--r-- | tests/test_hmac.c | 36 | ||||
-rw-r--r-- | tests/test_hmac_prng.c | 8 | ||||
-rw-r--r-- | tests/test_sha256.c | 112 |
20 files changed, 347 insertions, 348 deletions
@@ -37,26 +37,26 @@ * * Security: AES-128 provides approximately 128 bits of security. * - * Usage: 1) call aes128_set_encrypt/decrypt_key to set the key. + * Usage: 1) call tc_aes128_set_encrypt/decrypt_key to set the key. * - * 2) call aes_encrypt/decrypt to process the data. + * 2) call tc_aes_encrypt/decrypt to process the data. */ -#ifndef __AES_H__ -#define __AES_H__ +#ifndef __TC_AES_H__ +#define __TC_AES_H__ #include<stdint.h> #define Nb (4) // number of columns (32-bit words) comprising the state #define Nk (4) // number of 32-bit words comprising the key #define Nr (10) // number of rounds -#define AES_BLOCK_SIZE (Nb*Nk) -#define AES_KEY_SIZE (Nb*Nk) +#define TC_AES_BLOCK_SIZE (Nb*Nk) +#define TC_AES_KEY_SIZE (Nb*Nk) -struct aes_key_sched_struct { +struct tc_aes_key_sched_struct { uint32_t words[Nb*(Nr+1)]; }; -typedef struct aes_key_sched_struct *AesKeySched_t; +typedef struct tc_aes_key_sched_struct *TCAesKeySched_t; /* * Set AES-128 encryption key. @@ -72,9 +72,9 @@ typedef struct aes_key_sched_struct *AesKeySched_t; * AES-256 key schedule -- see FIPS 197 for details. * */ -int32_t aes128_set_encrypt_key ( - AesKeySched_t s, /* IN/OUT -- inited struct aes_key_sched_struct */ - const uint8_t *k); /* IN -- points to the AES key */ +int32_t tc_aes128_set_encrypt_key ( + TCAesKeySched_t s, + const uint8_t *k); /* * AES-128 Encryption procedure. @@ -90,10 +90,10 @@ int32_t aes128_set_encrypt_key ( * in == NULL or * s == NULL. */ -int32_t aes_encrypt ( - uint8_t *out, /* IN/OUT -- buffer to receive ciphertext block */ - const uint8_t *in, /* IN -- a plaintext block to encrypt */ - const AesKeySched_t s); /* IN -- initialized AES key schedule */ +int32_t tc_aes_encrypt ( + uint8_t *out, + const uint8_t *in, + const TCAesKeySched_t s); /* * Set the AES-128 decryption key. @@ -112,9 +112,9 @@ int32_t aes_encrypt ( * than 128, and must not be used for AES-192 or AES-256 key * schedule -- see FIPS 197 for details. */ -int32_t aes128_set_decrypt_key ( - AesKeySched_t s, /* IN/OUT -- inited struct aes_key_sched_struct */ - const uint8_t *k); /* IN -- points to the AES key */ +int32_t tc_aes128_set_decrypt_key ( + TCAesKeySched_t s, + const uint8_t *k); /* * AES-128 Encryption procedure. @@ -130,9 +130,9 @@ int32_t aes128_set_decrypt_key ( * in is NULL or * s is NULL. */ -int32_t aes_decrypt ( - uint8_t *out, /* IN/OUT -- buffer to receive ciphertext block */ - const uint8_t *in, /* IN -- a plaintext block to encrypt */ - const AesKeySched_t s); /* IN -- initialized AES key schedule */ +int32_t tc_aes_decrypt ( + uint8_t *out, + const uint8_t *in, + const TCAesKeySched_t s); #endif diff --git a/lib/aes_decrypt.c b/lib/aes_decrypt.c index cf74fc3..50eed52 100644 --- a/lib/aes_decrypt.c +++ b/lib/aes_decrypt.c @@ -62,9 +62,9 @@ static const uint8_t inv_sbox[256] = { 0x55, 0x21, 0x0c, 0x7d }; -int32_t aes128_set_decrypt_key ( - AesKeySched_t s, const uint8_t *k) { - return aes128_set_encrypt_key (s, k); +int32_t tc_aes128_set_decrypt_key ( + TCAesKeySched_t s, const uint8_t *k) { + return tc_aes128_set_encrypt_key (s, k); } #define mult8(a) (double_byte (double_byte (double_byte (a)))) @@ -123,8 +123,8 @@ static inline void inv_shift_rows (uint8_t *s) { (void) copy (s, sizeof (t), t, sizeof (t)); } -int32_t aes_decrypt ( - uint8_t *out, const uint8_t *in, const AesKeySched_t s) { +int32_t tc_aes_decrypt ( + uint8_t *out, const uint8_t *in, const TCAesKeySched_t s) { uint8_t state[Nk*Nb]; uint32_t i; @@ -132,7 +132,7 @@ int32_t aes_decrypt ( return 0; } else if (in == (const uint8_t *) 0) { return 0; - } else if (s == (AesKeySched_t) 0) { + } else if (s == (TCAesKeySched_t) 0) { return 0; } diff --git a/lib/aes_encrypt.c b/lib/aes_encrypt.c index 3ed0a1d..a5397d8 100644 --- a/lib/aes_encrypt.c +++ b/lib/aes_encrypt.c @@ -66,7 +66,7 @@ static const uint8_t sbox[256] = { #define subbyte(a,o) (sbox[((a)>>(o))&0xff]<<(o)) #define subword(a) (subbyte(a,24)|subbyte(a,16)|subbyte(a,8)|subbyte(a,0)) -int32_t aes128_set_encrypt_key (AesKeySched_t s, const uint8_t *k) { +int32_t tc_aes128_set_encrypt_key (TCAesKeySched_t s, const uint8_t *k) { const uint32_t rconst[11] = { 0x00000000, 0x01000000, 0x02000000, 0x04000000, 0x08000000, 0x10000000, 0x20000000, 0x40000000, 0x80000000, 0x1b000000, 0x36000000 @@ -74,7 +74,7 @@ int32_t aes128_set_encrypt_key (AesKeySched_t s, const uint8_t *k) { uint32_t i; uint32_t t; - if (s == (AesKeySched_t) 0) { + if (s == (TCAesKeySched_t) 0) { return 0; } else if (k == (const uint8_t *) 0) { return 0; @@ -145,8 +145,8 @@ static inline void shift_rows (uint8_t *s) { (void) copy (s, sizeof (t), t, sizeof (t)); } -int32_t aes_encrypt ( - uint8_t *out, const uint8_t *in, const AesKeySched_t s) { +int32_t tc_aes_encrypt ( + uint8_t *out, const uint8_t *in, const TCAesKeySched_t s) { uint8_t state[Nk*Nb]; uint32_t i; @@ -154,7 +154,7 @@ int32_t aes_encrypt ( return 0; } else if (in == (const uint8_t *) 0) { return 0; - } else if (s == (AesKeySched_t) 0) { + } else if (s == (TCAesKeySched_t) 0) { return 0; } diff --git a/lib/cbc_mode.c b/lib/cbc_mode.c index 130c1ed..a23d973 100644 --- a/lib/cbc_mode.c +++ b/lib/cbc_mode.c @@ -37,41 +37,41 @@ #include "cbc_mode.h" #include "utils.h" -int32_t cbc_mode_encrypt ( +int32_t tc_cbc_mode_encrypt ( uint8_t *out, uint32_t outlen, const uint8_t *in, uint32_t inlen, const uint8_t *iv, - const AesKeySched_t sched) { + const TCAesKeySched_t sched) { - uint8_t buffer[AES_BLOCK_SIZE]; + uint8_t buffer[TC_AES_BLOCK_SIZE]; uint32_t n, m; /* input sanity check: */ if (out == (uint8_t *) 0 || in == (const uint8_t *) 0 || - sched == (AesKeySched_t) 0 || + sched == (TCAesKeySched_t) 0 || inlen == 0 || outlen == 0 || - (inlen % AES_BLOCK_SIZE) != 0 || - (outlen % AES_BLOCK_SIZE) != 0 || - outlen != inlen + AES_BLOCK_SIZE) { + (inlen % TC_AES_BLOCK_SIZE) != 0 || + (outlen % TC_AES_BLOCK_SIZE) != 0 || + outlen != inlen + TC_AES_BLOCK_SIZE) { return 0; } /* copy iv to the buffer */ - (void) copy (buffer, AES_BLOCK_SIZE, iv, AES_BLOCK_SIZE); + (void) copy (buffer, TC_AES_BLOCK_SIZE, iv, TC_AES_BLOCK_SIZE); /* copy iv to the output buffer */ - (void) copy (out, AES_BLOCK_SIZE, iv, AES_BLOCK_SIZE); - out += AES_BLOCK_SIZE; + (void) copy (out, TC_AES_BLOCK_SIZE, iv, TC_AES_BLOCK_SIZE); + out += TC_AES_BLOCK_SIZE; for (n = m = 0; n < inlen; ++n) { buffer[m++] ^= *in++; - if (m == AES_BLOCK_SIZE) { - (void) aes_encrypt (buffer, buffer, sched); - (void) copy (out, AES_BLOCK_SIZE, buffer, AES_BLOCK_SIZE); - out += AES_BLOCK_SIZE; + if (m == TC_AES_BLOCK_SIZE) { + (void) tc_aes_encrypt (buffer, buffer, sched); + (void) copy (out, TC_AES_BLOCK_SIZE, buffer, TC_AES_BLOCK_SIZE); + out += TC_AES_BLOCK_SIZE; m = 0; } } @@ -79,27 +79,27 @@ int32_t cbc_mode_encrypt ( return 1; } -int32_t cbc_mode_decrypt ( +int32_t tc_cbc_mode_decrypt ( uint8_t *out, uint32_t outlen, const uint8_t *in, uint32_t inlen, const uint8_t *iv, - const AesKeySched_t sched) { + const TCAesKeySched_t sched) { - uint8_t buffer[AES_BLOCK_SIZE]; + uint8_t buffer[TC_AES_BLOCK_SIZE]; const uint8_t *p; uint32_t n, m; /* sanity check the inputs */ if (out == (uint8_t *) 0 || in == (const uint8_t *) 0 || - sched == (AesKeySched_t) 0 || + sched == (TCAesKeySched_t) 0 || inlen == 0 || outlen == 0 || - (inlen % AES_BLOCK_SIZE) != 0 || - (outlen % AES_BLOCK_SIZE) != 0 || - outlen != inlen - AES_BLOCK_SIZE) { + (inlen % TC_AES_BLOCK_SIZE) != 0 || + (outlen % TC_AES_BLOCK_SIZE) != 0 || + outlen != inlen - TC_AES_BLOCK_SIZE) { return 0; } @@ -108,9 +108,9 @@ int32_t cbc_mode_decrypt ( * would not otherwise be possible. */ p = iv; for (n = m = 0; n < inlen; ++n) { - if ((n % AES_BLOCK_SIZE) == 0) { - (void) aes_decrypt (buffer, in, sched); - in += AES_BLOCK_SIZE; + if ((n % TC_AES_BLOCK_SIZE) == 0) { + (void) tc_aes_decrypt (buffer, in, sched); + in += TC_AES_BLOCK_SIZE; m = 0; } *out++ = buffer[m++] ^ *p++; diff --git a/lib/cbc_mode.h b/lib/cbc_mode.h index 0f44662..0cc5bf8 100644 --- a/lib/cbc_mode.h +++ b/lib/cbc_mode.h @@ -42,7 +42,7 @@ * good practice to replace the encryption when q == 2^56). CBC mode * provides NO data integrity. * - * CBC mode assumes that the IV value input into the cbc_mode_encrypt + * CBC mode assumes that the IV value input into the tc_cbc_mode_encrypt * is randomly generated. The TinyCrypt library provides HMAC-PRNG * module, which generates suitable IVs. Other methods for generating * IVs are acceptable, provided that the values of the IVs generated @@ -56,19 +56,19 @@ * * TinyCrypt CBC encryption prepends the IV to the ciphertext, * because this affords a more efficient (few buffers) decryption. - * Hence cbc_mode_encrypt assumes the ciphertext buffer is always + * Hence tc_cbc_mode_encrypt assumes the ciphertext buffer is always * 16 bytes larger than the plaintext buffer. * * Requires: AES-128 * - * Usage: 1) call cbc_mode_encrypt to encrypt data. + * Usage: 1) call tc_cbc_mode_encrypt to encrypt data. * - * 2) call cbc_mode_decrypt to decrypt data. + * 2) call tc_cbc_mode_decrypt to decrypt data. * */ -#ifndef __CBC_MODE_H__ -#define __CBC_MODE_H__ +#ifndef __TC_CBC_MODE_H__ +#define __TC_CBC_MODE_H__ #include "aes.h" @@ -96,13 +96,13 @@ * (outlen % AES_BLOCK_SIZE) != 0 or * outlen != inlen + AES_BLOCK_SIZE. */ -int32_t cbc_mode_encrypt ( - uint8_t *out, /* IN/OUT -- buffer to receive the ciphertext */ - uint32_t outlen, /* IN -- length of ciphertext buffer in bytes */ - const uint8_t *in, /* IN -- plaintext to encrypt */ - uint32_t inlen, /* IN -- length of plaintext buffer in bytes */ - const uint8_t *iv, /* IN -- the IV for the this encrypt/decrypt */ - const AesKeySched_t sched); /* IN -- AES key schedule for this encrypt */ +int32_t tc_cbc_mode_encrypt ( + uint8_t *out, + uint32_t outlen, + const uint8_t *in, + uint32_t inlen, + const uint8_t *iv, + const TCAesKeySched_t sched); /* * CBC decryption procedure. @@ -129,12 +129,12 @@ int32_t cbc_mode_encrypt ( * outlen != inlen + AES_BLOCK_SIZE. * */ -int32_t cbc_mode_decrypt ( - uint8_t *out, /* IN/OUT -- buffer to receive decrypted data */ - uint32_t outlen, /* IN -- length of plaintext buffer in bytes */ - const uint8_t *in, /* IN -- ciphertext to decrypt, including IV */ - uint32_t inlen, /* IN -- length of ciphertext buffer in bytes */ - const uint8_t *iv, /* IN -- the IV for the this encrypt/decrypt */ - const AesKeySched_t sched); /* IN -- AES key schedule for this decrypt */ +int32_t tc_cbc_mode_decrypt ( + uint8_t *out, + uint32_t outlen, + const uint8_t *in, + uint32_t inlen, + const uint8_t *iv, + const TCAesKeySched_t sched); #endif diff --git a/lib/ctr_mode.c b/lib/ctr_mode.c index 3a416f7..e297f90 100644 --- a/lib/ctr_mode.c +++ b/lib/ctr_mode.c @@ -37,16 +37,16 @@ #include "ctr_mode.h" #include "utils.h" -int32_t ctr_mode ( +int32_t tc_ctr_mode ( uint8_t *out, uint32_t outlen, const uint8_t *in, uint32_t inlen, uint8_t *ctr, - const AesKeySched_t sched) { + const TCAesKeySched_t sched) { - uint8_t buffer[AES_BLOCK_SIZE]; - uint8_t nonce[AES_BLOCK_SIZE]; + uint8_t buffer[TC_AES_BLOCK_SIZE]; + uint8_t nonce[TC_AES_BLOCK_SIZE]; uint32_t block_num; uint32_t i; @@ -54,7 +54,7 @@ int32_t ctr_mode ( if (out == (uint8_t *) 0 || in == (uint8_t *) 0 || ctr == (uint8_t *) 0 || - sched == (AesKeySched_t) 0 || + sched == (TCAesKeySched_t) 0 || inlen == 0 || outlen == 0 || outlen != inlen) { @@ -67,9 +67,9 @@ int32_t ctr_mode ( /* select the last 4 bytes of the nonce to be incremented */ block_num = (nonce[12] << 24)|(nonce[13] << 16)|(nonce[14] << 8)|(nonce[15]); for (i = 0; i < inlen; ++i) { - if ((i % (AES_BLOCK_SIZE)) == 0) { + if ((i % (TC_AES_BLOCK_SIZE)) == 0) { /* encrypt data using the current nonce */ - if (aes_encrypt (buffer, nonce, sched)) { + if (tc_aes_encrypt (buffer, nonce, sched)) { block_num++; nonce[12] = (uint8_t)(block_num >> 24); nonce[13] = (uint8_t)(block_num >> 16); @@ -80,7 +80,7 @@ int32_t ctr_mode ( } } /* update the output */ - *out++ = buffer[i%(AES_BLOCK_SIZE)] ^ *in++; + *out++ = buffer[i%(TC_AES_BLOCK_SIZE)] ^ *in++; } /* update the counter */ diff --git a/lib/ctr_mode.h b/lib/ctr_mode.h index 422463a..dc1d8e3 100644 --- a/lib/ctr_mode.h +++ b/lib/ctr_mode.h @@ -56,12 +56,12 @@ * * Requires: AES-128 * - * Usage: 1) call ctr_mode to process the data to encrypt/decrypt. + * Usage: 1) call tc_ctr_mode to process the data to encrypt/decrypt. * */ -#ifndef __CTR_MODE_H__ -#define __CTR_MODE_H__ +#ifndef __TC_CTR_MODE_H__ +#define __TC_CTR_MODE_H__ #include "aes.h" @@ -86,12 +86,12 @@ * outlen == 0 or * inlen != outlen. */ -int32_t ctr_mode ( - uint8_t *out, /* OUT -- produced ciphertext (plaintext) */ - uint32_t outlen, /* IN -- length of ciphertext buffer in bytes */ - const uint8_t *in, /* IN -- data to encrypt (or decrypt) */ - uint32_t inlen, /* IN -- length of input data in bytes */ - uint8_t *ctr, /* IN/OUT -- the current counter value */ - const AesKeySched_t sched); /* IN -- an initialized AES key schedule */ +int32_t tc_ctr_mode ( + uint8_t *out, + uint32_t outlen, + const uint8_t *in, + uint32_t inlen, + uint8_t *ctr, + const TCAesKeySched_t sched); #endif @@ -45,91 +45,91 @@ static void rekey ( for (i = 0; i < key_size; ++i) { key[i] = inner_pad ^ new_key[i]; - key[i + SHA256_BLOCK_SIZE] = outer_pad ^ new_key[i]; + key[i + TC_SHA256_BLOCK_SIZE] = outer_pad ^ new_key[i]; } - for (; i < SHA256_BLOCK_SIZE; ++i) { - key[i] = inner_pad; key[i + SHA256_BLOCK_SIZE] = outer_pad; + for (; i < TC_SHA256_BLOCK_SIZE; ++i) { + key[i] = inner_pad; key[i + TC_SHA256_BLOCK_SIZE] = outer_pad; } } -int32_t hmac_set_key (HmacState_t ctx, const uint8_t *key, uint32_t key_size) { +int32_t tc_hmac_set_key (TCHmacState_t ctx, const uint8_t *key, uint32_t key_size) { /* input sanity check: */ - if (ctx == (HmacState_t) 0 || + if (ctx == (TCHmacState_t) 0 || key == (const uint8_t *) 0 || key_size == 0) { return 0; } const uint8_t dummy_key[key_size]; - struct hmac_state_struct dummy_state; + struct tc_hmac_state_struct dummy_state; - if (key_size <= SHA256_BLOCK_SIZE) { + if (key_size <= TC_SHA256_BLOCK_SIZE) { /* The next three lines consist of dummy calls just to avoid certain timing * attacks. Without these dummy calls, adversaries would be able to learn - * whether the key_size is greater than SHA256_BLOCK_SIZE by measuring the + * whether the key_size is greater than TC_SHA256_BLOCK_SIZE by measuring the * time consumed in this process.*/ - (void) sha256_init (&dummy_state.hash_state); - (void) sha256_update (&dummy_state.hash_state, dummy_key, key_size); - (void) sha256_final (&dummy_state.key[SHA256_DIGEST_SIZE], + (void) tc_sha256_init (&dummy_state.hash_state); + (void) tc_sha256_update (&dummy_state.hash_state, dummy_key, key_size); + (void) tc_sha256_final (&dummy_state.key[TC_SHA256_DIGEST_SIZE], &dummy_state.hash_state); - /* Actual code for when key_size <= SHA256_BLOCK_SIZE: */ + /* Actual code for when key_size <= TC_SHA256_BLOCK_SIZE: */ rekey (ctx->key, key, key_size); } else { - (void) sha256_init (&ctx->hash_state); - (void) sha256_update (&ctx->hash_state, key, key_size); - (void) sha256_final (&ctx->key[SHA256_DIGEST_SIZE], &ctx->hash_state); - rekey (ctx->key, &ctx->key[SHA256_DIGEST_SIZE], SHA256_DIGEST_SIZE); + (void) tc_sha256_init (&ctx->hash_state); + (void) tc_sha256_update (&ctx->hash_state, key, key_size); + (void) tc_sha256_final (&ctx->key[TC_SHA256_DIGEST_SIZE], &ctx->hash_state); + rekey (ctx->key, &ctx->key[TC_SHA256_DIGEST_SIZE], TC_SHA256_DIGEST_SIZE); } return 1; } -int32_t hmac_init (HmacState_t ctx) { +int32_t tc_hmac_init (TCHmacState_t ctx) { /* input sanity check: */ - if (ctx == (HmacState_t) 0 || + if (ctx == (TCHmacState_t) 0 || ctx->key == (uint8_t *) 0) { return 0; } - (void) sha256_init (&ctx->hash_state); - (void) sha256_update (&ctx->hash_state, ctx->key, SHA256_BLOCK_SIZE); + (void) tc_sha256_init (&ctx->hash_state); + (void) tc_sha256_update (&ctx->hash_state, ctx->key, TC_SHA256_BLOCK_SIZE); return 1; } -int32_t hmac_update (HmacState_t ctx, const void *data, uint32_t data_length) { +int32_t tc_hmac_update (TCHmacState_t ctx, const void *data, uint32_t data_length) { /* input sanity check: */ - if (ctx == (HmacState_t) 0 || + if (ctx == (TCHmacState_t) 0 || ctx->key == (uint8_t *) 0) { return 0; } - (void) sha256_update (&ctx->hash_state, data, data_length); + (void) tc_sha256_update (&ctx->hash_state, data, data_length); return 1; } -int32_t hmac_final (uint8_t *tag, uint32_t taglen, HmacState_t ctx) { +int32_t tc_hmac_final (uint8_t *tag, uint32_t taglen, TCHmacState_t ctx) { /* input sanity check: */ if (tag == (uint8_t *) 0 || - taglen != SHA256_DIGEST_SIZE || - ctx == (HmacState_t) 0 || + taglen != TC_SHA256_DIGEST_SIZE || + ctx == (TCHmacState_t) 0 || ctx->key == (uint8_t *) 0) { return 0; } - (void) sha256_final (tag, &ctx->hash_state); + (void) tc_sha256_final (tag, &ctx->hash_state); - (void) sha256_init (&ctx->hash_state); - (void) sha256_update (&ctx->hash_state, &ctx->key[SHA256_BLOCK_SIZE], - SHA256_BLOCK_SIZE); - (void) sha256_update (&ctx->hash_state, tag, SHA256_DIGEST_SIZE); - (void) sha256_final (tag, &ctx->hash_state); + (void) tc_sha256_init (&ctx->hash_state); + (void) tc_sha256_update (&ctx->hash_state, &ctx->key[TC_SHA256_BLOCK_SIZE], + TC_SHA256_BLOCK_SIZE); + (void) tc_sha256_update (&ctx->hash_state, tag, TC_SHA256_DIGEST_SIZE); + (void) tc_sha256_final (tag, &ctx->hash_state); return 1; } @@ -44,28 +44,28 @@ * * Requires: SHA-256 * - * Usage: 1) call hmac_set_key to set the HMAC key. + * Usage: 1) call tc_hmac_set_key to set the HMAC key. * - * 2) call hmac_init to initialize a struct hash_state before + * 2) call tc_hmac_init to initialize a struct hash_state before * processing the data. * - * 3) call hmac_update to process the next input segment; - * hmac_update can be called as many times as needed to process + * 3) call tc_hmac_update to process the next input segment; + * tc_hmac_update can be called as many times as needed to process * all of the segments of the input; the order is important. * - * 4) call hmac_final to out put the tag. + * 4) call tc_hmac_final to out put the tag. */ -#ifndef __HMAC_H__ -#define __HMAC_H__ +#ifndef __TC_HMAC_H__ +#define __TC_HMAC_H__ -#include "sha256.h" +#include <sha256.h> -struct hmac_state_struct { - struct sha256_state_struct hash_state; /* the internal state required by h */ - uint8_t key[2*SHA256_BLOCK_SIZE]; /* HMAC key schedule */ +struct tc_hmac_state_struct { + struct tc_sha256_state_struct hash_state; /* the internal state required by h */ + uint8_t key[2*TC_SHA256_BLOCK_SIZE]; /* HMAC key schedule */ }; -typedef struct hmac_state_struct *HmacState_t; +typedef struct tc_hmac_state_struct *TCHmacState_t; /* * HMAC set key procedure. @@ -77,9 +77,9 @@ typedef struct hmac_state_struct *HmacState_t; * key == NULL or * key_size == 0. */ -int32_t hmac_set_key ( - HmacState_t ctx, /* IN/OUT -- the struct hmac_state_struct to initial */ - const uint8_t *key, /* IN -- the HMAC key to configure */ +int32_t tc_hmac_set_key ( + TCHmacState_t ctx, + const uint8_t *key, uint32_t key_size); /* @@ -91,8 +91,8 @@ int32_t hmac_set_key ( * ctx == NULL or * key == NULL. */ -int32_t hmac_init ( - HmacState_t ctx); /* IN/OUT -- struct hmac_state_struct buffer to init */ +int32_t tc_hmac_init ( + TCHmacState_t ctx); /* * HMAC update procedure. @@ -107,10 +107,10 @@ int32_t hmac_init ( * key == NULL. * */ -int32_t hmac_update ( - HmacState_t ctx, /* IN/OUT -- state of HMAC computation so far */ - const void *data, /* IN -- data to incorporate into state */ - uint32_t data_length); /* IN -- size of data in bytes */ +int32_t tc_hmac_update ( + TCHmacState_t ctx, + const void *data, + uint32_t data_length); /* * HMAC final procedure. @@ -127,9 +127,9 @@ int32_t hmac_update ( * taglen != SHA256_DIGEST_SIZE. * */ -int32_t hmac_final ( - uint8_t *tag, /* IN/OUT -- buffer to receive computed HMAC tag */ - uint32_t taglen, /* IN -- size of tag in bytes */ - HmacState_t ctx); /* IN -- the HMAC state for computing tag */ +int32_t tc_hmac_final ( + uint8_t *tag, + uint32_t taglen, + TCHmacState_t ctx); #endif diff --git a/lib/hmac_prng.c b/lib/hmac_prng.c index b5530af..d2878ac 100644 --- a/lib/hmac_prng.c +++ b/lib/hmac_prng.c @@ -66,46 +66,46 @@ static const uint32_t MAX_OUT = (1 << 19); /* * Assumes: prng != NULL, e != NULL, len >= 0. */ -static void update (HmacPrng_t prng, const uint8_t *e, uint32_t len) { +static void update (TCHmacPrng_t prng, const uint8_t *e, uint32_t len) { const uint8_t separator0 = 0x00; const uint8_t separator1 = 0x01; /* use current state, e and separator 0 to compute a new prng key: */ - (void) hmac_init (&prng->h); - (void) hmac_update (&prng->h, prng->v, sizeof (prng->v)); - (void) hmac_update (&prng->h, &separator0, sizeof (separator0)); - (void) hmac_update (&prng->h, e, len); - (void) hmac_final (prng->key, sizeof(prng->key), &prng->h); + (void) tc_hmac_init (&prng->h); + (void) tc_hmac_update (&prng->h, prng->v, sizeof (prng->v)); + (void) tc_hmac_update (&prng->h, &separator0, sizeof (separator0)); + (void) tc_hmac_update (&prng->h, e, len); + (void) tc_hmac_final (prng->key, sizeof(prng->key), &prng->h); /* configure the new prng key into the prng's instance of hmac */ - (void) hmac_set_key (&prng->h, prng->key, sizeof (prng->key)); + (void) tc_hmac_set_key (&prng->h, prng->key, sizeof (prng->key)); /* use the new key to compute a new state variable v */ - (void) hmac_init (&prng->h); - (void) hmac_update (&prng->h, prng->v, sizeof (prng->v)); - (void) hmac_final (prng->v, sizeof(prng->v), &prng->h); + (void) tc_hmac_init (&prng->h); + (void) tc_hmac_update (&prng->h, prng->v, sizeof (prng->v)); + (void) tc_hmac_final (prng->v, sizeof(prng->v), &prng->h); /* use current state, e and separator 1 to compute a new prng key: */ - (void) hmac_init (&prng->h); - (void) hmac_update (&prng->h, prng->v, sizeof (prng->v)); - (void) hmac_update (&prng->h, &separator1, sizeof (separator1)); - (void) hmac_update (&prng->h, e, len); - (void) hmac_final (prng->key, sizeof(prng->key), &prng->h); + (void) tc_hmac_init (&prng->h); + (void) tc_hmac_update (&prng->h, prng->v, sizeof (prng->v)); + (void) tc_hmac_update (&prng->h, &separator1, sizeof (separator1)); + (void) tc_hmac_update (&prng->h, e, len); + (void) tc_hmac_final (prng->key, sizeof(prng->key), &prng->h); /* configure the new prng key into the prng's instance of hmac */ - (void) hmac_set_key (&prng->h, prng->key, sizeof (prng->key)); + (void) tc_hmac_set_key (&prng->h, prng->key, sizeof (prng->key)); /* use the new key to compute a new state variable v */ - (void) hmac_init (&prng->h); - (void) hmac_update (&prng->h, prng->v, sizeof (prng->v)); - (void) hmac_final (prng->v, sizeof(prng->v), &prng->h); + (void) tc_hmac_init (&prng->h); + (void) tc_hmac_update (&prng->h, prng->v, sizeof (prng->v)); + (void) tc_hmac_final (prng->v, sizeof(prng->v), &prng->h); } -int32_t hmac_prng_init ( - HmacPrng_t prng, /* IN/OUT -- the PRNG state to initialize */ +int32_t tc_hmac_prng_init ( + TCHmacPrng_t prng, /* IN/OUT -- the PRNG state to initialize */ const uint8_t *personalization, /* IN -- personalization string */ uint32_t plen) { /* IN -- personalization length in bytes */ /* input sanity check: */ - if (prng == (HmacPrng_t) 0 || + if (prng == (TCHmacPrng_t) 0 || personalization == (uint8_t*) 0 || plen > MAX_PLEN) { return 0; @@ -114,26 +114,26 @@ int32_t hmac_prng_init ( /* put the generator into a known state: */ set (prng->key, 0x00, sizeof (prng->key)); set (prng->v, 0x01, sizeof (prng->v)); - hmac_set_key (&prng->h, prng->key, sizeof (prng->key)); + tc_hmac_set_key (&prng->h, prng->key, sizeof (prng->key)); /* update assumes SOME key has been configured into HMAC */ update (prng, personalization, plen); - /* force a reseed before allowing hmac_prng_generate to succeed: */ + /* force a reseed before allowing tc_hmac_prng_generate to succeed: */ prng->countdown = 0; return 1; } -int32_t hmac_prng_reseed ( - HmacPrng_t prng, +int32_t tc_hmac_prng_reseed ( + TCHmacPrng_t prng, const uint8_t *seed, uint32_t seedlen, const uint8_t *additional_input, uint32_t additionallen) { /* input sanity check: */ - if (prng == (HmacPrng_t) 0 || + if (prng == (TCHmacPrng_t) 0 || seed == (const uint8_t *) 0 || seedlen < MIN_SLEN || seedlen > MAX_SLEN) { @@ -159,15 +159,15 @@ int32_t hmac_prng_reseed ( return 1; } -int32_t hmac_prng_generate ( +int32_t tc_hmac_prng_generate ( uint8_t *out, /* IN/OUT -- buffer to receive output */ uint32_t outlen, /* IN -- size of out buffer in bytes */ - HmacPrng_t prng) { /* IN/OUT -- the PRNG state */ + TCHmacPrng_t prng) { /* IN/OUT -- the PRNG state */ uint32_t bufferlen; /* input sanity check: */ if (out == (uint8_t *) 0 || - prng == (HmacPrng_t) 0 || + prng == (TCHmacPrng_t) 0 || outlen == 0 || outlen > MAX_OUT) { return 0; @@ -179,19 +179,19 @@ int32_t hmac_prng_generate ( while (outlen != 0) { /* operate HMAC in OFB mode to create "random" outputs */ - (void) hmac_init (&prng->h); - (void) hmac_update (&prng->h, prng->v, sizeof (prng->v)); - (void) hmac_final (prng->v, sizeof(prng->v), &prng->h); + (void) tc_hmac_init (&prng->h); + (void) tc_hmac_update (&prng->h, prng->v, sizeof (prng->v)); + (void) tc_hmac_final (prng->v, sizeof(prng->v), &prng->h); - bufferlen = (SHA256_DIGEST_SIZE > outlen) ? outlen : SHA256_DIGEST_SIZE; + bufferlen = (TC_SHA256_DIGEST_SIZE > outlen) ? outlen : TC_SHA256_DIGEST_SIZE; (void) copy (out, bufferlen, prng->v, bufferlen); out += bufferlen; - outlen = (outlen > SHA256_DIGEST_SIZE) ? (outlen - SHA256_DIGEST_SIZE) : 0; + outlen = (outlen > TC_SHA256_DIGEST_SIZE) ? (outlen - TC_SHA256_DIGEST_SIZE) : 0; } /* block future PRNG compromises from revealing past state */ - update (prng, prng->v, SHA256_DIGEST_SIZE); + update (prng, prng->v, TC_SHA256_DIGEST_SIZE); return 1; } diff --git a/lib/hmac_prng.h b/lib/hmac_prng.h index 297270e..8414454 100644 --- a/lib/hmac_prng.h +++ b/lib/hmac_prng.h @@ -52,29 +52,29 @@ * Requires: - SHA-256 * - HMAC * - * Usage: 1) call hmac_prng_init to set the HMAC key and process the + * Usage: 1) call tc_hmac_prng_init to set the HMAC key and process the * personalization data. * - * 2) call hmac_prng_reseed to process the seed and additional + * 2) call tc_hmac_prng_reseed to process the seed and additional * input. * - * 3) call hmac_prng_generate to out put the pseudo-random data. + * 3) call tc_hmac_prng_generate to out put the pseudo-random data. */ -#ifndef __HMAC_PRNG_H__ -#define __HMAC_PRNG_H__ +#ifndef __TC_HMAC_PRNG_H__ +#define __TC_HMAC_PRNG_H__ #include "sha256.h" #include "hmac.h" -struct hmac_prng_struct { - struct hmac_state_struct h; /* the HMAC instance for this PRNG */ - uint8_t key[SHA256_DIGEST_SIZE]; /* the PRNG key */ - uint8_t v[SHA256_DIGEST_SIZE]; /* PRNG state */ - uint32_t countdown; /* calls to hmac_prng_generate left before re-seed */ +struct tc_hmac_prng_struct { + struct tc_hmac_state_struct h; /* the HMAC instance for this PRNG */ + uint8_t key[TC_SHA256_DIGEST_SIZE]; /* the PRNG key */ + uint8_t v[TC_SHA256_DIGEST_SIZE]; /* PRNG state */ + uint32_t countdown; /* calls to tc_hmac_prng_generate left before re-seed */ }; -typedef struct hmac_prng_struct *HmacPrng_t; +typedef struct tc_hmac_prng_struct *TCHmacPrng_t; /* * HMAC-PRNG initialization procedure. @@ -100,10 +100,10 @@ typedef struct hmac_prng_struct *HmacPrng_t; * init). * */ -int32_t hmac_prng_init ( - HmacPrng_t prng, /* IN/OUT -- the PRNG state to initialize */ - const uint8_t *personalization, /* IN -- personalization string */ - uint32_t plen); /* IN -- personalization length in bytes */ +int32_t tc_hmac_prng_init ( + TCHmacPrng_t prng, + const uint8_t *personalization, + uint32_t plen); /* * HMAC-PRNG reseed procedure. @@ -121,12 +121,12 @@ int32_t hmac_prng_init ( * additional_input != (const uint8_t *) 0 && additionallen == 0, * additional_input != (const uint8_t *) 0 && additionallen > MAX_ALEN. */ -int32_t hmac_prng_reseed ( - HmacPrng_t prng, /* IN/OUT -- the PRNG state */ - const uint8_t *seed, /* IN -- entropy to mix into the prng */ - uint32_t seedlen, /* IN -- length of seed in bytes */ - const uint8_t *additional_input,/* IN -- additional input to the prng */ - uint32_t additionallen); /* IN -- additional input length in bytes */ +int32_t tc_hmac_prng_reseed ( + TCHmacPrng_t prng, + const uint8_t *seed, + uint32_t seedlen, + const uint8_t *additional_input, + uint32_t additionallen); /* * HMAC-PRNG generate procedure. @@ -145,9 +145,9 @@ int32_t hmac_prng_reseed ( * outlen == 0, * outlen >= MAX_OUT. */ -int32_t hmac_prng_generate ( - uint8_t *out, /* IN/OUT -- buffer to receive output */ - uint32_t outlen, /* IN -- size of out buffer in bytes */ - HmacPrng_t prng); /* IN/OUT -- the PRNG state */ +int32_t tc_hmac_prng_generate ( + uint8_t *out, + uint32_t outlen, + TCHmacPrng_t prng); #endif diff --git a/lib/sha256.c b/lib/sha256.c index ddef0c9..e6ba0c5 100644 --- a/lib/sha256.c +++ b/lib/sha256.c @@ -39,15 +39,15 @@ static void compress (uint32_t *iv, const uint8_t *data); -int32_t sha256_init (Sha256State_t s) { +int32_t tc_sha256_init (TCSha256State_t s) { /* input sanity check: */ - if (s == (Sha256State_t) 0) { + if (s == (TCSha256State_t) 0) { return 0; } - /* Setting the initial state values. - These values correspond to the first 32 bits of the fractional parts of + /* Setting the initial state values. + These values correspond to the first 32 bits of the fractional parts of the square roots of the first 8 primes: 2, 3, 5, 7, 11, 13, 17 and 19. */ set ((uint8_t *) s, 0x00, sizeof (*s)); s->iv[0] = 0x6a09e667; @@ -62,10 +62,10 @@ int32_t sha256_init (Sha256State_t s) { return 1; } -int32_t sha256_update (Sha256State_t s, const uint8_t* data, size_t datalen) { +int32_t tc_sha256_update (TCSha256State_t s, const uint8_t* data, size_t datalen) { /* input sanity check: */ - if (s == (Sha256State_t) 0 || + if (s == (TCSha256State_t) 0 || s->iv == (uint32_t *) 0 || data == (void *) 0) { return 0; @@ -75,22 +75,22 @@ int32_t sha256_update (Sha256State_t s, const uint8_t* data, size_t datalen) { while (datalen-- > 0) { s->leftover[s->leftover_offset++] = *(data++); - if (s->leftover_offset >= SHA256_BLOCK_SIZE) { + if (s->leftover_offset >= TC_SHA256_BLOCK_SIZE) { compress (s->iv, s->leftover); s->leftover_offset = 0; - s->bits_hashed += (SHA256_BLOCK_SIZE << 3); + s->bits_hashed += (TC_SHA256_BLOCK_SIZE << 3); } } return 1; } -int32_t sha256_final (uint8_t *digest, Sha256State_t s) { +int32_t tc_sha256_final (uint8_t *digest, TCSha256State_t s) { uint32_t i; /* input sanity check: */ if (digest == (uint8_t *) 0 || - s == (Sha256State_t) 0 || + s == (TCSha256State_t) 0 || s->iv == (uint32_t *) 0) { return 0; } @@ -122,7 +122,7 @@ int32_t sha256_final (uint8_t *digest, Sha256State_t s) { compress (s->iv, s->leftover); /* copy the iv out to digest */ - for (i = 0; i < SHA256_STATE_BLOCKS; ++i) { + for (i = 0; i < TC_SHA256_STATE_BLOCKS; ++i) { uint32_t t = *((uint32_t *) &s->iv[i]); *digest++ = (uint8_t)(t >> 24); *digest++ = (uint8_t)(t >> 16); diff --git a/lib/sha256.h b/lib/sha256.h index 2616c62..3a64aab 100644 --- a/lib/sha256.h +++ b/lib/sha256.h @@ -39,35 +39,35 @@ * NOT behave like a random oracle, but it can be used as one if * the string being hashed is prefix-free encoded before hashing. * - * Usage: 1) call sha256_init to initialize a struct sha256_state_struct + * Usage: 1) call tc_sha256_init to initialize a struct tc_sha256_state_struct * before hashing a new string. * - * 2) call sha256_update to hash the next string segment; - * sha256_update can be called as many times as needed to hash + * 2) call tc_sha256_update to hash the next string segment; + * tc_sha256_update can be called as many times as needed to hash * all of the segments of a string; the order is important. * - * 3) call sha256_final to out put the digest from a hashing + * 3) call tc_sha256_final to out put the digest from a hashing * operation. */ -#ifndef __SHA256_H__ -#define __SHA256_H__ +#ifndef __TC_SHA256_H__ +#define __TC_SHA256_H__ #include <stddef.h> #include <stdint.h> -#define SHA256_BLOCK_SIZE (64) -#define SHA256_DIGEST_SIZE (32) -#define SHA256_STATE_BLOCKS (SHA256_DIGEST_SIZE/4) +#define TC_SHA256_BLOCK_SIZE (64) +#define TC_SHA256_DIGEST_SIZE (32) +#define TC_SHA256_STATE_BLOCKS (TC_SHA256_DIGEST_SIZE/4) -struct sha256_state_struct { - uint32_t iv[SHA256_STATE_BLOCKS]; +struct tc_sha256_state_struct { + uint32_t iv[TC_SHA256_STATE_BLOCKS]; uint64_t bits_hashed; - uint8_t leftover[SHA256_BLOCK_SIZE]; + uint8_t leftover[TC_SHA256_BLOCK_SIZE]; size_t leftover_offset; }; -typedef struct sha256_state_struct *Sha256State_t; +typedef struct tc_sha256_state_struct *TCSha256State_t; /* * SHA256 initialization procedure. @@ -77,7 +77,7 @@ typedef struct sha256_state_struct *Sha256State_t; * Exceptions: Returns 0 if: * s == NULL. */ -int32_t sha256_init (Sha256State_t s); +int32_t tc_sha256_init (TCSha256State_t s); /* * SHA256 update procedure. @@ -96,7 +96,7 @@ int32_t sha256_init (Sha256State_t s); * If your application intends to have sensitive data in this * buffer, remind to erase it after the data has been processed. */ -int32_t sha256_update (Sha256State_t s, const uint8_t *data, size_t datalen); +int32_t tc_sha256_update (TCSha256State_t s, const uint8_t *data, size_t datalen); /* * SHA256 final procedure. @@ -115,7 +115,6 @@ int32_t sha256_update (Sha256State_t s, const uint8_t *data, size_t datalen); * If your application intends to have sensitive data in this * buffer, remind to erase it after the data has been processed. */ -int32_t sha256_final (uint8_t *digest, Sha256State_t s); - +int32_t tc_sha256_final (uint8_t *digest, TCSha256State_t s); #endif diff --git a/lib/utils.h b/lib/utils.h index 0eb8658..8b4baa1 100644 --- a/lib/utils.h +++ b/lib/utils.h @@ -32,8 +32,8 @@ * */ -#ifndef __UTILS_H__ -#define __UTILS_H__ +#ifndef __TC_UTILS_H__ +#define __TC_UTILS_H__ #include <stdint.h> diff --git a/tests/test_aes.c b/tests/test_aes.c index b890906..d441437 100644 --- a/tests/test_aes.c +++ b/tests/test_aes.c @@ -47,7 +47,7 @@ int32_t test_1() { 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c }; - const struct aes_key_sched_struct expected = {{ + const struct tc_aes_key_sched_struct expected = {{ 0x2b7e1516, 0x28aed2a6, 0xabf71588, 0x09cf4f3c, 0xa0fafe17, 0x88542cb1, 0x23a33939, 0x2a6c7605, 0xf2c295f2, 0x7a96b943, 0x5935807a, 0x7359f67f, @@ -60,9 +60,9 @@ int32_t test_1() { 0xac7766f3, 0x19fadc21, 0x28d12941, 0x575c006e, 0xd014f9a8, 0xc9ee2589, 0xe13f0cc8, 0xb6630ca6 }}; - struct aes_key_sched_struct s; + struct tc_aes_key_sched_struct s; - if (aes128_set_encrypt_key (&s, nist_key) == 0) { + if (tc_aes128_set_encrypt_key (&s, nist_key) == 0) { fprintf (stderr, "AES128 test #1 (NIST key schedule test) failed.\n"); exit (-1); } @@ -91,11 +91,11 @@ int32_t test_2 (void) { 0x39, 0x25, 0x84, 0x1d, 0x02, 0xdc, 0x09, 0xfb, 0xdc, 0x11, 0x85, 0x97, 0x19, 0x6a, 0x0b, 0x32 }; - struct aes_key_sched_struct s; + struct tc_aes_key_sched_struct s; uint8_t ciphertext[16]; - (void) aes128_set_encrypt_key (&s, nist_key); - if (aes_encrypt (ciphertext, nist_input, &s) == 0) { + (void) tc_aes128_set_encrypt_key (&s, nist_key); + if (tc_aes_encrypt (ciphertext, nist_input, &s) == 0) { fprintf (stderr, "AES128 test #2 (NIST encryption test) failed.\n"); exit (-1); } @@ -106,15 +106,15 @@ int32_t test_2 (void) { } void var_text_test ( - uint32_t r, const uint8_t *in, const uint8_t *out, AesKeySched_t s) { + uint32_t r, const uint8_t *in, const uint8_t *out, TCAesKeySched_t s) { uint8_t ciphertext[16]; uint8_t decrypted[16]; - (void) aes_encrypt (ciphertext, in, s); + (void) tc_aes_encrypt (ciphertext, in, s); check_result(r, out, 16, ciphertext, sizeof(ciphertext), 0); - if (aes_decrypt (decrypted, ciphertext, s) == 0) { - fprintf (stderr, "aes_decrypt failed\n"); + if (tc_aes_decrypt (decrypted, ciphertext, s) == 0) { + fprintf (stderr, "tc_aes_decrypt failed\n"); exit (-1); } else { check_result(r, in, 16, decrypted, sizeof(decrypted), 0); @@ -1031,10 +1031,10 @@ int32_t test_3() { 0xfa, 0x73, 0x47, 0xd2, 0x3e, 0x8d, 0x66, 0x4e }} }; - struct aes_key_sched_struct s; + struct tc_aes_key_sched_struct s; uint32_t i; - (void) aes128_set_encrypt_key (&s, key); + (void) tc_aes128_set_encrypt_key (&s, key); for (i = 0; i < 128; ++i) { var_text_test (i, kat_tbl[i].in, kat_tbl[i].out, &s); @@ -1052,11 +1052,11 @@ void var_key_test ( 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; uint8_t ciphertext[16]; - struct aes_key_sched_struct s; + struct tc_aes_key_sched_struct s; - (void) aes128_set_encrypt_key (&s, in); + (void) tc_aes128_set_encrypt_key (&s, in); - (void) aes_encrypt (ciphertext, plaintext, &s); + (void) tc_aes_encrypt (ciphertext, plaintext, &s); check_result(r, out, 16, ciphertext, sizeof(ciphertext), 0); } diff --git a/tests/test_cbc_mode.c b/tests/test_cbc_mode.c index a0afc46..3e5ec05 100644 --- a/tests/test_cbc_mode.c +++ b/tests/test_cbc_mode.c @@ -94,19 +94,19 @@ const uint8_t ciphertext[80] = { * NIST SP 800-38a CBC Test for encryption and decryption. */ void test_1_2 (void) { - struct aes_key_sched_struct a; + struct tc_aes_key_sched_struct a; uint8_t iv_buffer[16]; uint8_t encrypted[80]; uint8_t decrypted[64]; uint8_t *p; uint32_t length; - (void) aes128_set_encrypt_key (&a, key); + (void) tc_aes128_set_encrypt_key (&a, key); - (void) memcpy (iv_buffer, iv, AES_BLOCK_SIZE); + (void) memcpy (iv_buffer, iv, TC_AES_BLOCK_SIZE); printf ("\tPerforming CBC test #1 (encryption SP 800-38a tests)..."); - if (cbc_mode_encrypt (encrypted, sizeof (plaintext) + AES_BLOCK_SIZE, + if (tc_cbc_mode_encrypt (encrypted, sizeof (plaintext) + TC_AES_BLOCK_SIZE, plaintext, sizeof (plaintext), iv_buffer, &a) == 0) { fprintf (stderr, "CBC test #1 (encryption SP 800-38a tests) failed.\n"); exit (-1); @@ -115,12 +115,12 @@ void test_1_2 (void) { check_result(1, ciphertext, sizeof(encrypted), encrypted, sizeof(encrypted), 1); printf ("\tPerforming CBC test #2 (decryption SP 800-38a tests)..."); - (void) aes128_set_decrypt_key (&a, key); + (void) tc_aes128_set_decrypt_key (&a, key); - p = &encrypted[AES_BLOCK_SIZE]; - length = ((uint32_t) sizeof (encrypted)) - AES_BLOCK_SIZE; + p = &encrypted[TC_AES_BLOCK_SIZE]; + length = ((uint32_t) sizeof (encrypted)) - TC_AES_BLOCK_SIZE; - if (cbc_mode_decrypt (decrypted, length - AES_BLOCK_SIZE, p, + if (tc_cbc_mode_decrypt (decrypted, length - TC_AES_BLOCK_SIZE, p, length, encrypted, &a) == 0) { fprintf (stderr, "CBC test #2 (decryption SP 800-38a tests) failed.\n"); exit (-1); diff --git a/tests/test_ctr_mode.c b/tests/test_ctr_mode.c index 9cdf04d..4ce0613 100644 --- a/tests/test_ctr_mode.c +++ b/tests/test_ctr_mode.c @@ -69,15 +69,15 @@ void test_1_2 (void) { 0x0d, 0xb0, 0x3e, 0xab, 0x1e, 0x03, 0x1d, 0xda, 0x2f, 0xbe, 0x03, 0xd1, 0x79, 0x21, 0x70, 0xa0, 0xf3, 0x00, 0x9c, 0xee }; - struct aes_key_sched_struct sched; + struct tc_aes_key_sched_struct sched; uint8_t out[80]; uint8_t decrypted[64]; printf ("\tPerforming CTR test #1 (encryption SP 800-38a tests)..."); - (void) aes128_set_encrypt_key (&sched, key); + (void) tc_aes128_set_encrypt_key (&sched, key); (void) memcpy (out, ctr, sizeof (ctr)); - if (ctr_mode (&out[AES_BLOCK_SIZE], sizeof (plaintext), plaintext, sizeof (plaintext), + if (tc_ctr_mode (&out[TC_AES_BLOCK_SIZE], sizeof (plaintext), plaintext, sizeof (plaintext), ctr, &sched) == 0) { fprintf (stderr, "CTR test #1 (encryption SP 800-38a tests) failed.\n"); exit (-1); @@ -87,7 +87,7 @@ void test_1_2 (void) { printf ("\tPerforming CTR test #2 (decryption SP 800-38a tests)..."); (void) memcpy (ctr, out, sizeof (ctr)); - if (ctr_mode (decrypted, sizeof (decrypted), &out[AES_BLOCK_SIZE], sizeof (decrypted), ctr, + if (tc_ctr_mode (decrypted, sizeof (decrypted), &out[TC_AES_BLOCK_SIZE], sizeof (decrypted), ctr, &sched) == 0) { fprintf (stderr, "CTR test #2 (decryption SP 800-38a tests) failed.\n"); exit (-1); diff --git a/tests/test_hmac.c b/tests/test_hmac.c index 82e76bb..d22c955 100644 --- a/tests/test_hmac.c +++ b/tests/test_hmac.c @@ -42,15 +42,15 @@ #include <stdint.h> void do_hmac_test ( - HmacState_t h, + TCHmacState_t h, uint32_t testnum, const uint8_t *data, size_t datalen, const uint8_t *expected, size_t expectedlen) { uint8_t digest[32]; - (void) hmac_init (h); - (void) hmac_update (h, data, datalen); - (void) hmac_final (digest, SHA256_DIGEST_SIZE, h); + (void) tc_hmac_init (h); + (void) tc_hmac_update (h, data, datalen); + (void) tc_hmac_final (digest, TC_SHA256_DIGEST_SIZE, h); check_result(testnum, expected, expectedlen, digest, sizeof(digest), 1); } @@ -69,10 +69,10 @@ void test_1 (void) { 0xaf, 0x0b, 0xf1, 0x2b, 0x88, 0x1d, 0xc2, 0x00, 0xc9, 0x83, 0x3d, 0xa7, 0x26, 0xe9, 0x37, 0x6c, 0x2e, 0x32, 0xcf, 0xf7 }; - struct hmac_state_struct h; + struct tc_hmac_state_struct h; (void) memset (&h, 0x00, sizeof (h)); - (void) hmac_set_key (&h, key, sizeof (key)); + (void) tc_hmac_set_key (&h, key, sizeof (key)); do_hmac_test (&h, 1, data, sizeof (data), expected, sizeof(expected)); } @@ -92,10 +92,10 @@ void test_2 (void) { 0x08, 0x95, 0x75, 0xc7, 0x5a, 0x00, 0x3f, 0x08, 0x9d, 0x27, 0x39, 0x83, 0x9d, 0xec, 0x58, 0xb9, 0x64, 0xec, 0x38, 0x43 }; - struct hmac_state_struct h; + struct tc_hmac_state_struct h; (void) memset (&h, 0x00, sizeof (h)); - (void) hmac_set_key (&h, key, sizeof (key)); + (void) tc_hmac_set_key (&h, key, sizeof (key)); do_hmac_test (&h, 2, data, sizeof (data), expected, sizeof(expected)); } @@ -118,10 +118,10 @@ void test_3 (void) { 0xd0, 0x91, 0x81, 0xa7, 0x29, 0x59, 0x09, 0x8b, 0x3e, 0xf8, 0xc1, 0x22, 0xd9, 0x63, 0x55, 0x14, 0xce, 0xd5, 0x65, 0xfe }; - struct hmac_state_struct h; + struct tc_hmac_state_struct h; (void) memset (&h, 0x00, sizeof (h)); - (void) hmac_set_key (&h, key, sizeof (key)); + (void) tc_hmac_set_key (&h, key, sizeof (key)); do_hmac_test (&h, 3, data, sizeof (data), expected, sizeof(expected)); } @@ -145,10 +145,10 @@ void test_4 (void) { 0x99, 0xf2, 0x08, 0x3a, 0x85, 0xf0, 0xfa, 0xa3, 0xe5, 0x78, 0xf8, 0x07, 0x7a, 0x2e, 0x3f, 0xf4, 0x67, 0x29, 0x66, 0x5b }; - struct hmac_state_struct h; + struct tc_hmac_state_struct h; (void) memset (&h, 0x00, sizeof (h)); - (void) hmac_set_key (&h, key, sizeof (key)); + (void) tc_hmac_set_key (&h, key, sizeof (key)); do_hmac_test (&h, 4, data, sizeof (data), expected, sizeof(expected)); } @@ -168,10 +168,10 @@ void test_5 (void) { 0x29, 0x55, 0x55, 0x2b, 0xfa, 0x6f, 0x7c, 0x0a, 0x6a, 0x8a, 0xef, 0x8b, 0x93, 0xf8, 0x60, 0xaa, 0xb0, 0xcd, 0x20, 0xc5 }; - struct hmac_state_struct h; + struct tc_hmac_state_struct h; (void) memset (&h, 0x00, sizeof (h)); - (void) hmac_set_key (&h, key, sizeof (key)); + (void) tc_hmac_set_key (&h, key, sizeof (key)); do_hmac_test (&h, 5, data, sizeof (data), expected, sizeof(expected)); } @@ -203,10 +203,10 @@ void test_6 (void) { 0xcb, 0xf5, 0xb7, 0x7f, 0x8e, 0x0b, 0xc6, 0x21, 0x37, 0x28, 0xc5, 0x14, 0x05, 0x46, 0x04, 0x0f, 0x0e, 0xe3, 0x7f, 0x54 }; - struct hmac_state_struct h; + struct tc_hmac_state_struct h; (void) memset (&h, 0x00, sizeof (h)); - (void) hmac_set_key (&h, key, sizeof (key)); + (void) tc_hmac_set_key (&h, key, sizeof (key)); do_hmac_test (&h, 6, data, sizeof (data), expected, sizeof(expected)); } @@ -246,10 +246,10 @@ void test_7 (void) { 0xd5, 0xb0, 0xe9, 0x44, 0xbf, 0xdc, 0x63, 0x64, 0x4f, 0x07, 0x13, 0x93, 0x8a, 0x7f, 0x51, 0x53, 0x5c, 0x3a, 0x35, 0xe2 }; - struct hmac_state_struct h; + struct tc_hmac_state_struct h; (void) memset (&h, 0x00, sizeof (h)); - (void) hmac_set_key (&h, key, sizeof (key)); + (void) tc_hmac_set_key (&h, key, sizeof (key)); do_hmac_test (&h, 7, data, sizeof (data), expected, sizeof(expected)); } diff --git a/tests/test_hmac_prng.c b/tests/test_hmac_prng.c index 92f9526..74b09f3 100644 --- a/tests/test_hmac_prng.c +++ b/tests/test_hmac_prng.c @@ -63,7 +63,7 @@ int32_t main (void) { printf ("\tPerforming HMAC-PRNG test#1 (init, reseed, generate)..."); uint8_t seed[128]; - struct hmac_prng_struct h; + struct tc_hmac_prng_struct h; uint32_t size = (1 << 19); uint8_t random[size]; uint32_t i; @@ -76,16 +76,16 @@ int32_t main (void) { uint8_t *personalization = (uint8_t *) "HOSTNAME";//e.g.: hostname+timestamp uint8_t *additional_input = (uint8_t *) "additional input"; - if (hmac_prng_init (&h, personalization, sizeof (personalization)) == 0) { + if (tc_hmac_prng_init (&h, personalization, sizeof (personalization)) == 0) { fatal ("HMAC-PRNG initialization failed."); } - if (hmac_prng_reseed (&h, seed, sizeof (seed), additional_input, + if (tc_hmac_prng_reseed (&h, seed, sizeof (seed), additional_input, sizeof (additional_input)) == 0) { fatal ("HMAC-PRNG reseed failed."); } - if (hmac_prng_generate (random, size, &h) < 1) { + if (tc_hmac_prng_generate (random, size, &h) < 1) { fatal ("HMAC-PRNG generate failed."); } //printBinaryFile(random, size); diff --git a/tests/test_sha256.c b/tests/test_sha256.c index 3314198..b37bbdf 100644 --- a/tests/test_sha256.c +++ b/tests/test_sha256.c @@ -51,11 +51,11 @@ void test_1 (void) { }; const char *m = "abc"; uint8_t digest[32]; - struct sha256_state_struct s; + struct tc_sha256_state_struct s; - (void) sha256_init (&s); - sha256_update (&s, (const uint8_t *) m, strlen (m)); - (void) sha256_final (digest, &s); + (void) tc_sha256_init (&s); + tc_sha256_update (&s, (const uint8_t *) m, strlen (m)); + (void) tc_sha256_final (digest, &s); check_result(1, expected, sizeof(expected), digest, sizeof(digest), 1); } @@ -72,11 +72,11 @@ void test_2 (void) { }; const char *m = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"; uint8_t digest[32]; - struct sha256_state_struct s; + struct tc_sha256_state_struct s; - (void) sha256_init (&s); - sha256_update (&s, (const uint8_t *) m, strlen (m)); - (void) sha256_final (digest, &s); + (void) tc_sha256_init (&s); + tc_sha256_update (&s, (const uint8_t *) m, strlen (m)); + (void) tc_sha256_final (digest, &s); check_result(2, expected, sizeof(expected), digest, sizeof(digest), 1); } @@ -90,11 +90,11 @@ void test_3 (void) { }; const uint8_t m[1] = { 0xbd }; uint8_t digest[32]; - struct sha256_state_struct s; + struct tc_sha256_state_struct s; - (void) sha256_init (&s); - sha256_update (&s, m, sizeof (m)); - (void) sha256_final (digest, &s); + (void) tc_sha256_init (&s); + tc_sha256_update (&s, m, sizeof (m)); + (void) tc_sha256_final (digest, &s); check_result(3, expected, sizeof(expected), digest, sizeof(digest), 1); } @@ -108,11 +108,11 @@ void test_4 (void) { }; const uint8_t m[4] = { 0xc9, 0x8c, 0x8e, 0x55 }; uint8_t digest[32]; - struct sha256_state_struct s; + struct tc_sha256_state_struct s; - (void) sha256_init (&s); - sha256_update (&s, m, sizeof (m)); - (void) sha256_final (digest, &s); + (void) tc_sha256_init (&s); + tc_sha256_update (&s, m, sizeof (m)); + (void) tc_sha256_final (digest, &s); check_result(4, expected, sizeof(expected), digest, sizeof(digest), 1); } @@ -126,13 +126,13 @@ void test_5 (void) { }; uint8_t m[55]; uint8_t digest[32]; - struct sha256_state_struct s; + struct tc_sha256_state_struct s; (void) memset (m, 0x00, sizeof (m)); - (void) sha256_init (&s); - sha256_update (&s, m, sizeof (m)); - (void) sha256_final (digest, &s); + (void) tc_sha256_init (&s); + tc_sha256_update (&s, m, sizeof (m)); + (void) tc_sha256_final (digest, &s); check_result(5, expected, sizeof(expected), digest, sizeof(digest), 1); } @@ -146,13 +146,13 @@ void test_6 (void) { }; uint8_t m[56]; uint8_t digest[32]; - struct sha256_state_struct s; + struct tc_sha256_state_struct s; (void) memset (m, 0x00, sizeof (m)); - (void) sha256_init (&s); - sha256_update (&s, m, sizeof (m)); - (void) sha256_final (digest, &s); + (void) tc_sha256_init (&s); + tc_sha256_update (&s, m, sizeof (m)); + (void) tc_sha256_final (digest, &s); check_result(6, expected, sizeof(expected), digest, sizeof(digest), 1); } @@ -166,13 +166,13 @@ void test_7 (void) { }; uint8_t m[57]; uint8_t digest[32]; - struct sha256_state_struct s; + struct tc_sha256_state_struct s; (void) memset (m, 0x00, sizeof (m)); - (void) sha256_init (&s); - sha256_update (&s, m, sizeof (m)); - (void) sha256_final (digest, &s); + (void) tc_sha256_init (&s); + tc_sha256_update (&s, m, sizeof (m)); + (void) tc_sha256_final (digest, &s); check_result(7, expected, sizeof(expected), digest, sizeof(digest), 1); } @@ -186,13 +186,13 @@ void test_8 (void) { }; uint8_t m[64]; uint8_t digest[32]; - struct sha256_state_struct s; + struct tc_sha256_state_struct s; (void) memset (m, 0x00, sizeof (m)); - (void) sha256_init (&s); - sha256_update (&s, m, sizeof (m)); - (void) sha256_final (digest, &s); + (void) tc_sha256_init (&s); + tc_sha256_update (&s, m, sizeof (m)); + (void) tc_sha256_final (digest, &s); check_result(8, expected, sizeof(expected), digest, sizeof(digest), 1); } @@ -206,13 +206,13 @@ void test_9 (void) { }; uint8_t m[1000]; uint8_t digest[32]; - struct sha256_state_struct s; + struct tc_sha256_state_struct s; (void) memset (m, 0x00, sizeof (m)); - (void) sha256_init (&s); - sha256_update (&s, m, sizeof (m)); - (void) sha256_final (digest, &s); + (void) tc_sha256_init (&s); + tc_sha256_update (&s, m, sizeof (m)); + (void) tc_sha256_final (digest, &s); check_result(9, expected, sizeof(expected), digest, sizeof(digest), 1); } @@ -226,13 +226,13 @@ void test_10 (void) { }; uint8_t m[1000]; uint8_t digest[32]; - struct sha256_state_struct s; + struct tc_sha256_state_struct s; (void) memset (m, 0x41, sizeof (m)); - (void) sha256_init (&s); - sha256_update (&s, m, sizeof (m)); - (void) sha256_final (digest, &s); + (void) tc_sha256_init (&s); + tc_sha256_update (&s, m, sizeof (m)); + (void) tc_sha256_final (digest, &s); check_result(10, expected, sizeof(expected), digest, sizeof(digest), 1); } @@ -246,13 +246,13 @@ void test_11 (void) { }; uint8_t m[1005]; uint8_t digest[32]; - struct sha256_state_struct s; + struct tc_sha256_state_struct s; (void) memset (m, 0x55, sizeof (m)); - (void) sha256_init (&s); - sha256_update (&s, m, sizeof (m)); - (void) sha256_final (digest, &s); + (void) tc_sha256_init (&s); + tc_sha256_update (&s, m, sizeof (m)); + (void) tc_sha256_final (digest, &s); check_result(11, expected, sizeof(expected), digest, sizeof(digest), 1); } @@ -266,16 +266,16 @@ void test_12 (void) { }; uint8_t m[1000]; uint8_t digest[32]; - struct sha256_state_struct s; + struct tc_sha256_state_struct s; uint32_t i; (void) memset (m, 0x00, sizeof (m)); - (void) sha256_init (&s); + (void) tc_sha256_init (&s); for (i = 0; i < 1000; ++i) { - sha256_update (&s, m, sizeof (m)); + tc_sha256_update (&s, m, sizeof (m)); } - (void) sha256_final (digest, &s); + (void) tc_sha256_final (digest, &s); check_result(12, expected, sizeof(expected), digest, sizeof(digest), 1); } @@ -289,16 +289,16 @@ void test_13 (void) { }; uint8_t m[32768]; uint8_t digest[32]; - struct sha256_state_struct s; + struct tc_sha256_state_struct s; uint32_t i; (void) memset (m, 0x5a, sizeof (m)); - (void) sha256_init (&s); + (void) tc_sha256_init (&s); for (i = 0; i < 16384; ++i) { - sha256_update (&s, m, sizeof (m)); + tc_sha256_update (&s, m, sizeof (m)); } - (void) sha256_final (digest, &s); + (void) tc_sha256_final (digest, &s); check_result(13, expected, sizeof(expected), digest, sizeof(digest), 1); } @@ -312,16 +312,16 @@ void test_14 (void) { }; uint8_t m[32768]; uint8_t digest[32]; - struct sha256_state_struct s; + struct tc_sha256_state_struct s; uint32_t i; (void) memset (m, 0x00, sizeof (m)); - (void) sha256_init (&s); + (void) tc_sha256_init (&s); for (i = 0; i < 33280; ++i) { - sha256_update (&s, m, sizeof (m)); + tc_sha256_update (&s, m, sizeof (m)); } - (void) sha256_final (digest, &s); + (void) tc_sha256_final (digest, &s); check_result(14, expected, sizeof(expected), digest, sizeof(digest), 1); } |