From ae2b415c6d99b6677c738138ff35470aaf4f192a Mon Sep 17 00:00:00 2001 From: Sukchan Lee Date: Wed, 8 Mar 2017 12:10:39 +0900 Subject: [PATCH] aes ctr128 is verified --- lib/core/include/core_aes.h | 4 ++++ lib/core/src/aes.c | 47 +++++++++++++++++++++++++++++++++++++ test/security_test.c | 44 +++++++++++++++++++++++++++++++--- 3 files changed, 92 insertions(+), 3 deletions(-) diff --git a/lib/core/include/core_aes.h b/lib/core/include/core_aes.h index afec811b1..51d401b59 100644 --- a/lib/core/include/core_aes.h +++ b/lib/core/include/core_aes.h @@ -34,6 +34,10 @@ CORE_DECLARE(status_t) aes_cbc_decrypt(const c_uint8_t *key, const c_uint8_t *in, const c_uint32_t inlen, c_uint8_t *out, c_uint32_t *outlen); +CORE_DECLARE(status_t) aes_ctr128_encrypt(const c_uint8_t *key, + c_uint8_t *ivec, const c_uint8_t *in, const c_uint32_t len, + c_uint8_t *out); + #ifdef __cplusplus } #endif /* __cplusplus */ diff --git a/lib/core/src/aes.c b/lib/core/src/aes.c index c732b0e98..2ddcee5bb 100644 --- a/lib/core/src/aes.c +++ b/lib/core/src/aes.c @@ -1342,3 +1342,50 @@ status_t aes_cbc_decrypt(const c_uint8_t *key, const c_uint32_t keybits, return CORE_OK; } + +static void ctr128_inc(c_uint8_t *counter) +{ + c_uint32_t n = 16, c = 1; + + do { + --n; + c += counter[n]; + counter[n] = (c_uint8_t)c; + c >>= 8; + } while (n); +} + +status_t aes_ctr128_encrypt(const c_uint8_t *key, + c_uint8_t *ivec, const c_uint8_t *in, const c_uint32_t len, + c_uint8_t *out) +{ + c_uint8_t ecount_buf[16]; + c_uint32_t rk[RKLENGTH(MAX_KEY_BITS)]; + int nrounds; + + unsigned int n = 0; + size_t l = 0; + + d_assert(key, return CORE_ERROR, "Null param"); + d_assert(ivec, return CORE_ERROR, "Null param"); + d_assert(in, return CORE_ERROR, "Null param"); + d_assert(len, return CORE_ERROR, "param 'inlen' is zero"); + d_assert(out, return CORE_ERROR, "Null param"); + + memset(ecount_buf, 0, 16); + nrounds = aes_setup_enc(rk, key, 128); + + while (l < len) + { + if (n == 0) + { + aes_encrypt(rk, nrounds, ivec, ecount_buf); + ctr128_inc(ivec); + } + out[l] = in[l] ^ ecount_buf[n]; + ++l; + n = (n + 1) % 16; + } + + return CORE_OK; +} diff --git a/test/security_test.c b/test/security_test.c index 8dd57f27a..094d34313 100644 --- a/test/security_test.c +++ b/test/security_test.c @@ -165,8 +165,8 @@ static void security_test5(abts_case *tc, void *data) "ddc1b65f 0aa0d97a 053db55a 88c4c4f9" "605e4140"; c_uint8_t ck[16]; - c_uint8_t plain[SECURITY_TEST5_BIT_LEN]; - c_uint8_t tmp[SECURITY_TEST5_BIT_LEN]; + c_uint8_t plain[SECURITY_TEST5_LEN]; + c_uint8_t tmp[SECURITY_TEST5_LEN]; snow_3g_f8( core_ascii_to_hex(_ck, strlen(_ck), ck, sizeof(ck)), @@ -212,8 +212,46 @@ static void security_test6(abts_case *tc, void *data) static void security_test7(abts_case *tc, void *data) { -} +#define SECURITY_TEST7_BIT_LEN 800 +#define SECURITY_TEST7_LEN ((SECURITY_TEST7_BIT_LEN+7)/8) + char *_ck = "2bd6459f 82c440e0 952c4910 4805ff48"; + char *_plain = + "7ec61272 743bf161 4726446a 6c38ced1 66f6ca76 eb543004 4286346c ef130f92" + "922b0345 0d3a9975 e5bd2ea0 eb55ad8e 1b199e3e c4316020 e9a1b285 e7627953" + "59b7bdfd 39bef4b2 484583d5 afe082ae e638bf5f d5a60619 3901a08f 4ab41aab" + "9b134880"; + char *_cipher = + "59616053 53c64bdc a15b195e 288553a9 10632506 d6200aa7 90c4c806 c99904cf" + "2445cc50 bb1cf168 a4967373 4e081b57 e324ce52 59c0e78d 4cd97b87 0976503c" + "0943f2cb 5ae8f052 c7b7d392 239587b8 956086bc ab188360 42e2e6ce 42432a17" + "105c53d3"; + c_uint8_t ck[16]; + c_uint8_t plain[SECURITY_TEST7_LEN]; + c_uint8_t cipher[SECURITY_TEST7_LEN]; + c_uint8_t tmp[SECURITY_TEST7_LEN]; + c_uint8_t ecount_buf[16]; + c_uint32_t num = 0; + + c_uint8_t ivec[16]; + c_uint32_t count = htonl(0xc675a64b); + memset(ivec, 0, sizeof(ivec)); + memcpy(ivec+0, &count, sizeof(count)); + ivec[4] = (0x0c << 3) | (1 << 2); + + memset(ecount_buf, 0, 16); + + aes_ctr128_encrypt( + core_ascii_to_hex(_ck, strlen(_ck), ck, sizeof(ck)), + ivec, + core_ascii_to_hex(_plain, strlen(_plain), plain, sizeof(plain)), + SECURITY_TEST7_LEN, + cipher); + + ABTS_TRUE(tc, memcmp(cipher, + core_ascii_to_hex(_cipher, strlen(_cipher), tmp, SECURITY_TEST7_LEN), + SECURITY_TEST7_LEN) == 0); +} static void security_test8(abts_case *tc, void *data) {