From 637eb3d31d1e9e5fe53993df15c5850901a32a89 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20P=C3=A9gouri=C3=A9-Gonnard?= Date: Tue, 6 May 2014 12:13:09 +0200 Subject: [PATCH] Add ccm_encrypt_and_tag() --- include/polarssl/ccm.h | 34 ++++ library/ccm.c | 286 ++++++++++++++++++++++++++- tests/suites/test_suite_ccm.data | 96 +++++++++ tests/suites/test_suite_ccm.function | 43 ++++ 4 files changed, 457 insertions(+), 2 deletions(-) diff --git a/include/polarssl/ccm.h b/include/polarssl/ccm.h index 24405e431..de2908352 100644 --- a/include/polarssl/ccm.h +++ b/include/polarssl/ccm.h @@ -64,6 +64,40 @@ int ccm_init( ccm_context *ctx, cipher_id_t cipher, */ void ccm_free( ccm_context *ctx ); +/** + * \brief CCM buffer encryption + * + * \todo Document if input/output buffers can be the same + * + * \param ctx CCM context + * \param length length of the input data in bytes + * \param iv nonce (initialization vector) + * \param iv_len length of IV in bytes + * must be 2, 3, 4, 5, 6, 7 or 8 + * \param add additional data + * \param add_len length of additional data in bytes + * must be less than 2^16 - 2^8 + * \param input buffer holding the input data + * \param output buffer for holding the output data + * must be at least 'length' bytes wide + * \param tag buffer for holding the tag + * \param tag_len length of the tag to generate in bytes + * must be 4, 6, 8, 10, 14 or 16 + * + * \note The tag is written to a separete buffer. To get the tag + * concatenated with the output as in the CCM spec, use + * tag = output + length and make sure the output buffer is + * at least length + tag_len wide. + * + * \return 0 if successful + */ +int ccm_crypt_and_tag( ccm_context *ctx, size_t length, + const unsigned char *iv, size_t iv_len, + const unsigned char *add, size_t add_len, + const unsigned char *input, unsigned char *output, + unsigned char *tag, size_t tag_len ); + + #if defined(POLARSSL_SELF_TEST) && defined(POLARSSL_AES_C) /** * \brief Checkup routine diff --git a/library/ccm.c b/library/ccm.c index 453edfb13..4a8decf5b 100644 --- a/library/ccm.c +++ b/library/ccm.c @@ -81,6 +81,206 @@ void ccm_free( ccm_context *ctx ) memset( ctx, 0, sizeof( ccm_context ) ); } +#define UPDATE_CBC_MAC( src ) \ +{ \ + size_t olen; \ + \ + for( i = 0; i < 16; i++ ) \ + src[i] ^= y[i]; \ + \ + if( ( ret = cipher_update( &ctx->cipher_ctx, src, 16, \ + y, &olen ) ) != 0 ) \ + { \ + return( ret ); \ + } \ +} + +#define CTR_CRYPT_BLOCK( dst, src ) \ +{ \ + size_t olen; \ + \ + if( ( ret = cipher_update( &ctx->cipher_ctx, ctr, 16, \ + dst, &olen ) ) != 0 ) \ + { \ + return( ret ); \ + } \ + \ + for( i = 0; i < 16; i++ ) \ + dst[i] ^= src[i]; \ +} + +/* + * Authenticated encryption + */ +int ccm_crypt_and_tag( ccm_context *ctx, size_t length, + const unsigned char *iv, size_t iv_len, + const unsigned char *add, size_t add_len, + const unsigned char *input, unsigned char *output, + unsigned char *tag, size_t tag_len ) +{ + int ret; + unsigned char i; + unsigned char q = 16 - 1 - iv_len; + size_t len_left; + unsigned char b[16]; + unsigned char y[16]; + unsigned char ctr[16]; + const unsigned char *src; + unsigned char *dst; + + /* + * Check length requirements: SP800-38C A.1 + * Additional requirement: a < 2^16 - 2^8 to simplify the code. + * 'length' checked later (when writing it to the first block) + */ + if( tag_len < 4 || tag_len > 16 || tag_len % 2 != 0 ) + return( POLARSSL_ERR_CCM_BAD_INPUT ); + + /* Also implies q is within bounds */ + if( iv_len < 7 || iv_len > 13 ) + return( POLARSSL_ERR_CCM_BAD_INPUT ); + + if( add_len > 0xFF00 ) + return( POLARSSL_ERR_CCM_BAD_INPUT ); + + /* + * First block B_0: + * 0 .. 0 flags + * 1 .. iv_len nonce (aka iv) + * iv_len+1 .. 15 length + * + * With flags as (bits): + * 7 0 + * 6 add present? + * 5 .. 3 (t - 2) / 2 + * 2 .. 0 q - 1 + */ + b[0] = 0; + b[0] |= ( add_len > 0 ) << 6; + b[0] |= ( ( tag_len - 2 ) / 2 ) << 3; + b[0] |= q - 1; + + memcpy( b + 1, iv, iv_len ); + + for( i = 0, len_left = length; i < q; i++, len_left >>= 8 ) + b[15-i] = (unsigned char)( len_left & 0xFF ); + + if( len_left > 0 ) + return( POLARSSL_ERR_CCM_BAD_INPUT ); + + + /* Start CBC-MAC with first block */ + memset( y, 0, 16 ); + UPDATE_CBC_MAC( b ); + + /* + * If there is additional data, update CBC-MAC with + * add_len, add, 0 (padding to a block boundary) + */ + if( add_len > 0 ) + { + size_t use_len; + len_left = add_len; + src = add; + + memset( b, 0, 16 ); + b[0] = (unsigned char)( ( add_len >> 8 ) & 0xFF ); + b[1] = (unsigned char)( ( add_len ) & 0xFF ); + + use_len = len_left < 16 - 2 ? len_left : 16 - 2; + memcpy( b + 2, src, use_len ); + len_left -= use_len; + src += use_len; + + UPDATE_CBC_MAC( b ); + + while( len_left > 16 ) + { + memcpy( b, src, 16 ); + UPDATE_CBC_MAC( b ); + + len_left -= 16; + src += 16; + } + + if( len_left > 0 ) + { + memset( b, 0, 16 ); + memcpy( b, src, len_left ); + + UPDATE_CBC_MAC( b ); + } + } + + /* + * Counter block: + * 0 .. 0 flags + * 1 .. iv_len nonce (aka iv) + * iv_len+1 .. 15 counter (initially 1) + * + * With flags as (bits): + * 7 .. 3 0 + * 2 .. 0 q - 1 + */ + ctr[0] = q - 1; + memcpy( ctr + 1, iv, iv_len ); + memset( ctr + 1 + iv_len, 0, q ); + ctr[15] = 1; + + len_left = length; + src = input; + dst = output; + + /* + * Authenticate and encrypt message + */ + while( len_left > 16 ) + { + memcpy( b, src, 16 ); + UPDATE_CBC_MAC( b ); + CTR_CRYPT_BLOCK( dst, src ); + + dst += 16; + src += 16; + len_left -= 16; + + for( i = 0; i < q; i++ ) + if( ++ctr[15-i] != 0 ) + break; + } + + if( len_left > 0 ) + { + unsigned char mask[16]; + size_t olen; + + memset( b, 0, 16 ); + memcpy( b, src, len_left ); + + UPDATE_CBC_MAC( b ); + + if( ( ret = cipher_update( &ctx->cipher_ctx, ctr, 16, + mask, &olen ) ) != 0 ) + { + return( ret ); + } + + for( i = 0; i < len_left; i++ ) + dst[i] = src[i] ^ mask[i]; + } + + /* + * Authentication: reset counter and encrypt internal tag + */ + for( i = 0; i < q; i++ ) + ctr[15-i] = 0; + + CTR_CRYPT_BLOCK( b, y ); + memcpy( tag, b, tag_len ); + + return( 0 ); +} + #if defined(POLARSSL_SELF_TEST) && defined(POLARSSL_AES_C) @@ -90,10 +290,92 @@ void ccm_free( ccm_context *ctx ) #define polarssl_printf printf #endif +/* + * Examples 1 to 3 from SP800-38C Appendix C + */ + +#define NB_TESTS 3 + +/* + * The data is the same for all tests, only the used length changes + */ +static const unsigned char key[] = { + 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, + 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f +}; + +static const unsigned char iv[] = { + 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, + 0x18, 0x19, 0x1a, 0x1b +}; + +static const unsigned char ad[] = { + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, + 0x10, 0x11, 0x12, 0x13 +}; + +static const unsigned char msg[] = { + 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, + 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, +}; + +static const size_t iv_len [NB_TESTS] = { 7, 8, 12 }; +static const size_t add_len[NB_TESTS] = { 8, 16, 20 }; +static const size_t msg_len[NB_TESTS] = { 4, 16, 24 }; +static const size_t tag_len[NB_TESTS] = { 4, 6, 8 }; + +static const unsigned char res[NB_TESTS][32] = { + { 0x71, 0x62, 0x01, 0x5b, 0x4d, 0xac, 0x25, 0x5d }, + { 0xd2, 0xa1, 0xf0, 0xe0, 0x51, 0xea, 0x5f, 0x62, + 0x08, 0x1a, 0x77, 0x92, 0x07, 0x3d, 0x59, 0x3d, + 0x1f, 0xc6, 0x4f, 0xbf, 0xac, 0xcd }, + { 0xe3, 0xb2, 0x01, 0xa9, 0xf5, 0xb7, 0x1a, 0x7a, + 0x9b, 0x1c, 0xea, 0xec, 0xcd, 0x97, 0xe7, 0x0b, + 0x61, 0x76, 0xaa, 0xd9, 0xa4, 0x42, 0x8a, 0xa5, + 0x48, 0x43, 0x92, 0xfb, 0xc1, 0xb0, 0x99, 0x51 } +}; + int ccm_self_test( int verbose ) { - if( verbose != 0 ) - polarssl_printf( " CCM: skip\n" ); + ccm_context ctx; + unsigned char out[32]; + size_t i; + int ret; + + if( ccm_init( &ctx, POLARSSL_CIPHER_ID_AES, key, 8 * sizeof key ) != 0 ) + { + if( verbose != 0 ) + polarssl_printf( " CCM: setup failed" ); + + return( 1 ); + } + + for( i = 0; i < NB_TESTS; i++ ) + { + if( verbose != 0 ) + polarssl_printf( " CCM-AES #%u: ", (unsigned int) i + 1 ); + + ret = ccm_crypt_and_tag( &ctx, msg_len[i], + iv, iv_len[i], ad, add_len[i], + msg, out, + out + msg_len[i], tag_len[i] ); + + if( ret != 0 || + memcmp( out, res[i], msg_len[i] + tag_len[i] ) != 0 ) + { + if( verbose != 0 ) + polarssl_printf( "failed\n" ); + + return( 1 ); + } + + if( verbose != 0 ) + polarssl_printf( "passed\n" ); + } + + ccm_free( &ctx ); if( verbose != 0 ) polarssl_printf( "\n" ); diff --git a/tests/suites/test_suite_ccm.data b/tests/suites/test_suite_ccm.data index 61ae8319f..c972e7881 100644 --- a/tests/suites/test_suite_ccm.data +++ b/tests/suites/test_suite_ccm.data @@ -16,3 +16,99 @@ ccm_init:POLARSSL_CIPHER_ID_AES:224:POLARSSL_ERR_CCM_BAD_INPUT CCM init #4 BLOWFISH-128: bad block size depends_on:POLARSSL_BLOWFISH_C ccm_init:POLARSSL_CIPHER_ID_BLOWFISH:128:POLARSSL_ERR_CCM_BAD_INPUT + +CCM encrypt and tag RFC 3610 #1 +depends_on:POLARSSL_AES_C +ccm_encrypt_and_tag:POLARSSL_CIPHER_ID_AES:"C0C1C2C3C4C5C6C7C8C9CACBCCCDCECF":"08090A0B0C0D0E0F101112131415161718191A1B1C1D1E":"00000003020100A0A1A2A3A4A5":"0001020304050607":"588C979A61C663D2F066D0C2C0F989806D5F6B61DAC38417E8D12CFDF926E0" + +CCM encrypt and tag RFC 3610 #2 +depends_on:POLARSSL_AES_C +ccm_encrypt_and_tag:POLARSSL_CIPHER_ID_AES:"C0C1C2C3C4C5C6C7C8C9CACBCCCDCECF":"08090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F":"00000004030201A0A1A2A3A4A5":"0001020304050607":"72C91A36E135F8CF291CA894085C87E3CC15C439C9E43A3BA091D56E10400916" + +CCM encrypt and tag RFC 3610 #3 +depends_on:POLARSSL_AES_C +ccm_encrypt_and_tag:POLARSSL_CIPHER_ID_AES:"C0C1C2C3C4C5C6C7C8C9CACBCCCDCECF":"08090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F20":"00000005040302A0A1A2A3A4A5":"0001020304050607":"51B1E5F44A197D1DA46B0F8E2D282AE871E838BB64DA8596574ADAA76FBD9FB0C5" + +CCM encrypt and tag RFC 3610 #4 +depends_on:POLARSSL_AES_C +ccm_encrypt_and_tag:POLARSSL_CIPHER_ID_AES:"C0C1C2C3C4C5C6C7C8C9CACBCCCDCECF":"0C0D0E0F101112131415161718191A1B1C1D1E":"00000006050403A0A1A2A3A4A5":"000102030405060708090A0B":"A28C6865939A9A79FAAA5C4C2A9D4A91CDAC8C96C861B9C9E61EF1" + +CCM encrypt and tag RFC 3610 #5 +depends_on:POLARSSL_AES_C +ccm_encrypt_and_tag:POLARSSL_CIPHER_ID_AES:"C0C1C2C3C4C5C6C7C8C9CACBCCCDCECF":"0C0D0E0F101112131415161718191A1B1C1D1E1F":"00000007060504A0A1A2A3A4A5":"000102030405060708090A0B":"DCF1FB7B5D9E23FB9D4E131253658AD86EBDCA3E51E83F077D9C2D93" + +CCM encrypt and tag RFC 3610 #6 +depends_on:POLARSSL_AES_C +ccm_encrypt_and_tag:POLARSSL_CIPHER_ID_AES:"C0C1C2C3C4C5C6C7C8C9CACBCCCDCECF":"0C0D0E0F101112131415161718191A1B1C1D1E1F20":"00000008070605A0A1A2A3A4A5":"000102030405060708090A0B":"6FC1B011F006568B5171A42D953D469B2570A4BD87405A0443AC91CB94" + +CCM encrypt and tag RFC 3610 #7 +depends_on:POLARSSL_AES_C +ccm_encrypt_and_tag:POLARSSL_CIPHER_ID_AES:"C0C1C2C3C4C5C6C7C8C9CACBCCCDCECF":"08090A0B0C0D0E0F101112131415161718191A1B1C1D1E":"00000009080706A0A1A2A3A4A5":"0001020304050607":"0135D1B2C95F41D5D1D4FEC185D166B8094E999DFED96C048C56602C97ACBB7490" + +CCM encrypt and tag RFC 3610 #8 +depends_on:POLARSSL_AES_C +ccm_encrypt_and_tag:POLARSSL_CIPHER_ID_AES:"C0C1C2C3C4C5C6C7C8C9CACBCCCDCECF":"08090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F":"0000000A090807A0A1A2A3A4A5":"0001020304050607":"7B75399AC0831DD2F0BBD75879A2FD8F6CAE6B6CD9B7DB24C17B4433F434963F34B4" + +CCM encrypt and tag RFC 3610 #9 +depends_on:POLARSSL_AES_C +ccm_encrypt_and_tag:POLARSSL_CIPHER_ID_AES:"C0C1C2C3C4C5C6C7C8C9CACBCCCDCECF":"08090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F20":"0000000B0A0908A0A1A2A3A4A5":"0001020304050607":"82531A60CC24945A4B8279181AB5C84DF21CE7F9B73F42E197EA9C07E56B5EB17E5F4E" + +CCM encrypt and tag RFC 3610 #10 +depends_on:POLARSSL_AES_C +ccm_encrypt_and_tag:POLARSSL_CIPHER_ID_AES:"C0C1C2C3C4C5C6C7C8C9CACBCCCDCECF":"0C0D0E0F101112131415161718191A1B1C1D1E":"0000000C0B0A09A0A1A2A3A4A5":"000102030405060708090A0B":"07342594157785152B074098330ABB141B947B566AA9406B4D999988DD" + +CCM encrypt and tag RFC 3610 #11 +depends_on:POLARSSL_AES_C +ccm_encrypt_and_tag:POLARSSL_CIPHER_ID_AES:"C0C1C2C3C4C5C6C7C8C9CACBCCCDCECF":"0C0D0E0F101112131415161718191A1B1C1D1E1F":"0000000D0C0B0AA0A1A2A3A4A5":"000102030405060708090A0B":"676BB20380B0E301E8AB79590A396DA78B834934F53AA2E9107A8B6C022C" + +CCM encrypt and tag RFC 3610 #12 +depends_on:POLARSSL_AES_C +ccm_encrypt_and_tag:POLARSSL_CIPHER_ID_AES:"C0C1C2C3C4C5C6C7C8C9CACBCCCDCECF":"0C0D0E0F101112131415161718191A1B1C1D1E1F20":"0000000E0D0C0BA0A1A2A3A4A5":"000102030405060708090A0B":"C0FFA0D6F05BDB67F24D43A4338D2AA4BED7B20E43CD1AA31662E7AD65D6DB" + +CCM encrypt and tag RFC 3610 #13 +depends_on:POLARSSL_AES_C +ccm_encrypt_and_tag:POLARSSL_CIPHER_ID_AES:"D7828D13B2B0BDC325A76236DF93CC6B":"08E8CF97D820EA258460E96AD9CF5289054D895CEAC47C":"00412B4EA9CDBE3C9696766CFA":"0BE1A88BACE018B1":"4CB97F86A2A4689A877947AB8091EF5386A6FFBDD080F8E78CF7CB0CDDD7B3" + +CCM encrypt and tag RFC 3610 #14 +depends_on:POLARSSL_AES_C +ccm_encrypt_and_tag:POLARSSL_CIPHER_ID_AES:"D7828D13B2B0BDC325A76236DF93CC6B":"9020EA6F91BDD85AFA0039BA4BAFF9BFB79C7028949CD0EC":"0033568EF7B2633C9696766CFA":"63018F76DC8A1BCB":"4CCB1E7CA981BEFAA0726C55D378061298C85C92814ABC33C52EE81D7D77C08A" + +CCM encrypt and tag RFC 3610 #15 +depends_on:POLARSSL_AES_C +ccm_encrypt_and_tag:POLARSSL_CIPHER_ID_AES:"D7828D13B2B0BDC325A76236DF93CC6B":"B916E0EACC1C00D7DCEC68EC0B3BBB1A02DE8A2D1AA346132E":"00103FE41336713C9696766CFA":"AA6CFA36CAE86B40":"B1D23A2220DDC0AC900D9AA03C61FCF4A559A4417767089708A776796EDB723506" + +CCM encrypt and tag RFC 3610 #16 +depends_on:POLARSSL_AES_C +ccm_encrypt_and_tag:POLARSSL_CIPHER_ID_AES:"D7828D13B2B0BDC325A76236DF93CC6B":"12DAAC5630EFA5396F770CE1A66B21F7B2101C":"00764C63B8058E3C9696766CFA":"D0D0735C531E1BECF049C244":"14D253C3967B70609B7CBB7C499160283245269A6F49975BCADEAF" + +CCM encrypt and tag RFC 3610 #17 +depends_on:POLARSSL_AES_C +ccm_encrypt_and_tag:POLARSSL_CIPHER_ID_AES:"D7828D13B2B0BDC325A76236DF93CC6B":"E88B6A46C78D63E52EB8C546EFB5DE6F75E9CC0D":"00F8B678094E3B3C9696766CFA":"77B60F011C03E1525899BCAE":"5545FF1A085EE2EFBF52B2E04BEE1E2336C73E3F762C0C7744FE7E3C" + +CCM encrypt and tag RFC 3610 #18 +depends_on:POLARSSL_AES_C +ccm_encrypt_and_tag:POLARSSL_CIPHER_ID_AES:"D7828D13B2B0BDC325A76236DF93CC6B":"6435ACBAFB11A82E2F071D7CA4A5EBD93A803BA87F":"00D560912D3F703C9696766CFA":"CD9044D2B71FDB8120EA60C0":"009769ECABDF48625594C59251E6035722675E04C847099E5AE0704551" + +CCM encrypt and tag RFC 3610 #19 +depends_on:POLARSSL_AES_C +ccm_encrypt_and_tag:POLARSSL_CIPHER_ID_AES:"D7828D13B2B0BDC325A76236DF93CC6B":"8A19B950BCF71A018E5E6701C91787659809D67DBEDD18":"0042FFF8F1951C3C9696766CFA":"D85BC7E69F944FB8":"BC218DAA947427B6DB386A99AC1AEF23ADE0B52939CB6A637CF9BEC2408897C6BA" + +CCM encrypt and tag RFC 3610 #20 +depends_on:POLARSSL_AES_C +ccm_encrypt_and_tag:POLARSSL_CIPHER_ID_AES:"D7828D13B2B0BDC325A76236DF93CC6B":"1761433C37C5A35FC1F39F406302EB907C6163BE38C98437":"00920F40E56CDC3C9696766CFA":"74A0EBC9069F5B37":"5810E6FD25874022E80361A478E3E9CF484AB04F447EFFF6F0A477CC2FC9BF548944" + +CCM encrypt and tag RFC 3610 #21 +depends_on:POLARSSL_AES_C +ccm_encrypt_and_tag:POLARSSL_CIPHER_ID_AES:"D7828D13B2B0BDC325A76236DF93CC6B":"A434A8E58500C6E41530538862D686EA9E81301B5AE4226BFA":"0027CA0C7120BC3C9696766CFA":"44A3AA3AAE6475CA":"F2BEED7BC5098E83FEB5B31608F8E29C38819A89C8E776F1544D4151A4ED3A8B87B9CE" + +CCM encrypt and tag RFC 3610 #22 +depends_on:POLARSSL_AES_C +ccm_encrypt_and_tag:POLARSSL_CIPHER_ID_AES:"D7828D13B2B0BDC325A76236DF93CC6B":"B96B49E21D621741632875DB7F6C9243D2D7C2":"005B8CCBCD9AF83C9696766CFA":"EC46BB63B02520C33C49FD70":"31D750A09DA3ED7FDDD49A2032AABF17EC8EBF7D22C8088C666BE5C197" + +CCM encrypt and tag RFC 3610 #23 +depends_on:POLARSSL_AES_C +ccm_encrypt_and_tag:POLARSSL_CIPHER_ID_AES:"D7828D13B2B0BDC325A76236DF93CC6B":"E2FCFBB880442C731BF95167C8FFD7895E337076":"003EBE94044B9A3C9696766CFA":"47A65AC78B3D594227E85E71":"E882F1DBD38CE3EDA7C23F04DD65071EB41342ACDF7E00DCCEC7AE52987D" + +CCM encrypt and tag RFC 3610 #24 +depends_on:POLARSSL_AES_C +ccm_encrypt_and_tag:POLARSSL_CIPHER_ID_AES:"D7828D13B2B0BDC325A76236DF93CC6B":"ABF21C0B02FEB88F856DF4A37381BCE3CC128517D4":"008D493B30AE8B3C9696766CFA":"6E37A6EF546D955D34AB6059":"F32905B88A641B04B9C9FFB58CC390900F3DA12AB16DCE9E82EFA16DA62059" diff --git a/tests/suites/test_suite_ccm.function b/tests/suites/test_suite_ccm.function index 7137a6029..3e97abad6 100644 --- a/tests/suites/test_suite_ccm.function +++ b/tests/suites/test_suite_ccm.function @@ -30,3 +30,46 @@ void ccm_init( int cipher_id, int key_size, int result ) ccm_free( &ctx ); } /* END_CASE */ + +/* BEGIN_CASE */ +void ccm_encrypt_and_tag( int cipher_id, + char *key_hex, char *msg_hex, + char *iv_hex, char *add_hex, + char *result_hex ) +{ + unsigned char key[128]; + unsigned char msg[128]; + unsigned char iv[128]; + unsigned char add[128]; + unsigned char output[144]; + unsigned char result[144]; + ccm_context ctx; + size_t key_len, msg_len, iv_len, add_len, tag_len, result_len; + + memset( key, 0x00, sizeof( key ) ); + memset( msg, 0x00, sizeof( msg ) ); + memset( iv, 0x00, sizeof( iv ) ); + memset( add, 0x00, sizeof( add ) ); + memset( output, 0x00, sizeof( output ) ); + memset( result, 0x00, sizeof( result ) ); + + key_len = unhexify( key, key_hex ); + msg_len = unhexify( msg, msg_hex ); + iv_len = unhexify( iv, iv_hex ); + add_len = unhexify( add, add_hex ); + result_len = unhexify( result, result_hex ); + tag_len = result_len - msg_len; + + TEST_ASSERT( ccm_init( &ctx, cipher_id, key, key_len * 8 ) == 0 ); + + TEST_ASSERT( ccm_crypt_and_tag( &ctx, msg_len, iv, iv_len, add, add_len, + msg, output, output + msg_len, tag_len ) == 0 ); + + TEST_ASSERT( memcmp( output, result, result_len ) == 0 ); + + /* Check we didn't write past the end */ + TEST_ASSERT( output[result_len] == 0 && output[result_len + 1] == 0 ); + + ccm_free( &ctx ); +} +/* END_CASE */