Manuel Pégourié-Gonnard | a6916fa | 2014-05-02 15:17:29 +0200 | [diff] [blame] | 1 | /* BEGIN_HEADER */ |
| 2 | #include <polarssl/ccm.h> |
| 3 | /* END_HEADER */ |
| 4 | |
| 5 | /* BEGIN_DEPENDENCIES |
| 6 | * depends_on:POLARSSL_CCM_C |
| 7 | * END_DEPENDENCIES |
| 8 | */ |
| 9 | |
| 10 | /* BEGIN_CASE depends_on:POLARSSL_SELF_TEST:POLARSSL_AES_C */ |
| 11 | void ccm_self_test( ) |
| 12 | { |
| 13 | TEST_ASSERT( ccm_self_test( 0 ) == 0 ); |
| 14 | } |
| 15 | /* END_CASE */ |
Manuel Pégourié-Gonnard | 9fe0d13 | 2014-05-06 12:12:45 +0200 | [diff] [blame] | 16 | |
| 17 | /* BEGIN_CASE */ |
| 18 | void ccm_init( int cipher_id, int key_size, int result ) |
| 19 | { |
| 20 | ccm_context ctx; |
| 21 | unsigned char key[32]; |
| 22 | int ret; |
| 23 | |
| 24 | memset( key, 0x2A, sizeof( key ) ); |
| 25 | TEST_ASSERT( (unsigned) key_size <= 8 * sizeof( key ) ); |
| 26 | |
| 27 | ret = ccm_init( &ctx, cipher_id, key, key_size ); |
| 28 | TEST_ASSERT( ret == result ); |
| 29 | |
| 30 | ccm_free( &ctx ); |
| 31 | } |
| 32 | /* END_CASE */ |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 33 | |
Manuel Pégourié-Gonnard | 87df5ba | 2014-05-06 18:07:24 +0200 | [diff] [blame] | 34 | /* BEGIN_CASE depends_on:POLARSSL_AES_C */ |
| 35 | void ccm_lengths( int msg_len, int iv_len, int add_len, int tag_len, int res ) |
| 36 | { |
| 37 | ccm_context ctx; |
| 38 | unsigned char key[16]; |
| 39 | unsigned char msg[10]; |
| 40 | unsigned char iv[14]; |
| 41 | unsigned char add[10]; |
| 42 | unsigned char out[10]; |
| 43 | unsigned char tag[18]; |
| 44 | int decrypt_ret; |
| 45 | |
| 46 | memset( key, 0, sizeof( key ) ); |
| 47 | memset( msg, 0, sizeof( msg ) ); |
| 48 | memset( iv, 0, sizeof( iv ) ); |
| 49 | memset( add, 0, sizeof( add ) ); |
| 50 | memset( out, 0, sizeof( out ) ); |
| 51 | memset( tag, 0, sizeof( tag ) ); |
| 52 | |
| 53 | TEST_ASSERT( ccm_init( &ctx, POLARSSL_CIPHER_ID_AES, |
| 54 | key, 8 * sizeof( key ) ) == 0 ); |
| 55 | |
| 56 | TEST_ASSERT( ccm_encrypt_and_tag( &ctx, msg_len, iv, iv_len, add, add_len, |
| 57 | msg, out, tag, tag_len ) == res ); |
| 58 | |
| 59 | decrypt_ret = ccm_auth_decrypt( &ctx, msg_len, iv, iv_len, add, add_len, |
| 60 | msg, out, tag, tag_len ); |
| 61 | |
| 62 | if( res == 0 ) |
| 63 | TEST_ASSERT( decrypt_ret == POLARSSL_ERR_CCM_AUTH_FAILED ); |
| 64 | else |
| 65 | TEST_ASSERT( decrypt_ret == res ); |
| 66 | |
| 67 | ccm_free( &ctx ); |
| 68 | } |
| 69 | /* END_CASE */ |
| 70 | |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 71 | /* BEGIN_CASE */ |
| 72 | void ccm_encrypt_and_tag( int cipher_id, |
| 73 | char *key_hex, char *msg_hex, |
| 74 | char *iv_hex, char *add_hex, |
| 75 | char *result_hex ) |
| 76 | { |
Manuel Pégourié-Gonnard | e8b8d01 | 2014-05-06 18:19:06 +0200 | [diff] [blame] | 77 | unsigned char key[32]; |
| 78 | unsigned char msg[50]; |
| 79 | unsigned char iv[13]; |
| 80 | unsigned char add[32]; |
| 81 | unsigned char output[50]; |
| 82 | unsigned char result[50]; |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 83 | ccm_context ctx; |
| 84 | size_t key_len, msg_len, iv_len, add_len, tag_len, result_len; |
| 85 | |
| 86 | memset( key, 0x00, sizeof( key ) ); |
| 87 | memset( msg, 0x00, sizeof( msg ) ); |
| 88 | memset( iv, 0x00, sizeof( iv ) ); |
| 89 | memset( add, 0x00, sizeof( add ) ); |
| 90 | memset( output, 0x00, sizeof( output ) ); |
| 91 | memset( result, 0x00, sizeof( result ) ); |
| 92 | |
| 93 | key_len = unhexify( key, key_hex ); |
| 94 | msg_len = unhexify( msg, msg_hex ); |
| 95 | iv_len = unhexify( iv, iv_hex ); |
| 96 | add_len = unhexify( add, add_hex ); |
| 97 | result_len = unhexify( result, result_hex ); |
| 98 | tag_len = result_len - msg_len; |
| 99 | |
| 100 | TEST_ASSERT( ccm_init( &ctx, cipher_id, key, key_len * 8 ) == 0 ); |
| 101 | |
Manuel Pégourié-Gonnard | 0023233 | 2014-05-06 15:56:07 +0200 | [diff] [blame] | 102 | TEST_ASSERT( ccm_encrypt_and_tag( &ctx, msg_len, iv, iv_len, add, add_len, |
Manuel Pégourié-Gonnard | 637eb3d | 2014-05-06 12:13:09 +0200 | [diff] [blame] | 103 | msg, output, output + msg_len, tag_len ) == 0 ); |
| 104 | |
| 105 | TEST_ASSERT( memcmp( output, result, result_len ) == 0 ); |
| 106 | |
| 107 | /* Check we didn't write past the end */ |
| 108 | TEST_ASSERT( output[result_len] == 0 && output[result_len + 1] == 0 ); |
| 109 | |
| 110 | ccm_free( &ctx ); |
| 111 | } |
| 112 | /* END_CASE */ |
Manuel Pégourié-Gonnard | ce77d55 | 2014-05-06 18:06:52 +0200 | [diff] [blame] | 113 | |
| 114 | /* BEGIN_CASE */ |
| 115 | void ccm_auth_decrypt( int cipher_id, |
| 116 | char *key_hex, char *msg_hex, |
| 117 | char *iv_hex, char *add_hex, |
| 118 | int tag_len, char *result_hex ) |
| 119 | { |
Manuel Pégourié-Gonnard | e8b8d01 | 2014-05-06 18:19:06 +0200 | [diff] [blame] | 120 | unsigned char key[32]; |
| 121 | unsigned char msg[50]; |
| 122 | unsigned char iv[13]; |
| 123 | unsigned char add[32]; |
| 124 | unsigned char output[50]; |
| 125 | unsigned char result[50]; |
Manuel Pégourié-Gonnard | ce77d55 | 2014-05-06 18:06:52 +0200 | [diff] [blame] | 126 | ccm_context ctx; |
| 127 | size_t key_len, msg_len, iv_len, add_len, result_len; |
| 128 | int ret; |
| 129 | |
| 130 | memset( key, 0x00, sizeof( key ) ); |
| 131 | memset( msg, 0x00, sizeof( msg ) ); |
| 132 | memset( iv, 0x00, sizeof( iv ) ); |
| 133 | memset( add, 0x00, sizeof( add ) ); |
| 134 | memset( output, 0xFF, sizeof( output ) ); |
| 135 | memset( result, 0x00, sizeof( result ) ); |
| 136 | |
| 137 | key_len = unhexify( key, key_hex ); |
| 138 | msg_len = unhexify( msg, msg_hex ); |
| 139 | iv_len = unhexify( iv, iv_hex ); |
| 140 | add_len = unhexify( add, add_hex ); |
| 141 | msg_len -= tag_len; |
| 142 | |
| 143 | if( strcmp( "FAIL", result_hex ) == 0 ) |
| 144 | { |
| 145 | ret = POLARSSL_ERR_CCM_AUTH_FAILED; |
| 146 | } |
| 147 | else |
| 148 | { |
| 149 | ret = 0; |
| 150 | result_len = unhexify( result, result_hex ); |
| 151 | } |
| 152 | |
| 153 | TEST_ASSERT( ccm_init( &ctx, cipher_id, key, key_len * 8 ) == 0 ); |
| 154 | |
| 155 | TEST_ASSERT( ccm_auth_decrypt( &ctx, msg_len, iv, iv_len, add, add_len, |
| 156 | msg, output, msg + msg_len, tag_len ) == ret ); |
| 157 | |
| 158 | if( ret == 0 ) |
| 159 | { |
| 160 | TEST_ASSERT( memcmp( output, result, result_len ) == 0 ); |
| 161 | } |
| 162 | else |
| 163 | { |
| 164 | size_t i; |
| 165 | |
| 166 | for( i = 0; i < msg_len; i++ ) |
| 167 | TEST_ASSERT( output[i] == 0 ); |
| 168 | } |
| 169 | |
| 170 | /* Check we didn't write past the end */ |
| 171 | TEST_ASSERT( output[msg_len] == 0xFF && output[msg_len + 1] == 0xFF ); |
| 172 | |
| 173 | ccm_free( &ctx ); |
| 174 | } |
| 175 | /* END_CASE */ |