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