blob: 8521fba5d50069a904fcb0f74cf06d992cf74458 [file] [log] [blame]
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +02001/* 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 */
11void ccm_self_test( )
12{
13 TEST_ASSERT( ccm_self_test( 0 ) == 0 );
14}
15/* END_CASE */
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020016
17/* BEGIN_CASE */
18void 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é-Gonnard637eb3d2014-05-06 12:13:09 +020033
Manuel Pégourié-Gonnard87df5ba2014-05-06 18:07:24 +020034/* BEGIN_CASE depends_on:POLARSSL_AES_C */
35void 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é-Gonnard637eb3d2014-05-06 12:13:09 +020071/* BEGIN_CASE */
72void 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é-Gonnarde8b8d012014-05-06 18:19:06 +020077 unsigned char key[32];
78 unsigned char msg[50];
79 unsigned char iv[13];
80 unsigned char add[32];
Manuel Pégourié-Gonnarde8b8d012014-05-06 18:19:06 +020081 unsigned char result[50];
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +020082 ccm_context ctx;
83 size_t key_len, msg_len, iv_len, add_len, tag_len, result_len;
84
85 memset( key, 0x00, sizeof( key ) );
86 memset( msg, 0x00, sizeof( msg ) );
87 memset( iv, 0x00, sizeof( iv ) );
88 memset( add, 0x00, sizeof( add ) );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +020089 memset( result, 0x00, sizeof( result ) );
90
91 key_len = unhexify( key, key_hex );
92 msg_len = unhexify( msg, msg_hex );
93 iv_len = unhexify( iv, iv_hex );
94 add_len = unhexify( add, add_hex );
95 result_len = unhexify( result, result_hex );
96 tag_len = result_len - msg_len;
97
98 TEST_ASSERT( ccm_init( &ctx, cipher_id, key, key_len * 8 ) == 0 );
99
Manuel Pégourié-Gonnard0f6b66d2014-05-07 14:43:46 +0200100 /* Test with input == output */
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200101 TEST_ASSERT( ccm_encrypt_and_tag( &ctx, msg_len, iv, iv_len, add, add_len,
Manuel Pégourié-Gonnard0f6b66d2014-05-07 14:43:46 +0200102 msg, msg, msg + msg_len, tag_len ) == 0 );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200103
Manuel Pégourié-Gonnard0f6b66d2014-05-07 14:43:46 +0200104 TEST_ASSERT( memcmp( msg, result, result_len ) == 0 );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200105
106 /* Check we didn't write past the end */
Manuel Pégourié-Gonnard0f6b66d2014-05-07 14:43:46 +0200107 TEST_ASSERT( msg[result_len] == 0 && msg[result_len + 1] == 0 );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200108
109 ccm_free( &ctx );
110}
111/* END_CASE */
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200112
113/* BEGIN_CASE */
114void ccm_auth_decrypt( int cipher_id,
115 char *key_hex, char *msg_hex,
116 char *iv_hex, char *add_hex,
117 int tag_len, char *result_hex )
118{
Manuel Pégourié-Gonnarde8b8d012014-05-06 18:19:06 +0200119 unsigned char key[32];
120 unsigned char msg[50];
121 unsigned char iv[13];
122 unsigned char add[32];
Manuel Pégourié-Gonnard0f6b66d2014-05-07 14:43:46 +0200123 unsigned char tag[16];
Manuel Pégourié-Gonnarde8b8d012014-05-06 18:19:06 +0200124 unsigned char result[50];
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200125 ccm_context ctx;
126 size_t key_len, msg_len, iv_len, add_len, result_len;
127 int ret;
128
129 memset( key, 0x00, sizeof( key ) );
130 memset( msg, 0x00, sizeof( msg ) );
131 memset( iv, 0x00, sizeof( iv ) );
132 memset( add, 0x00, sizeof( add ) );
Manuel Pégourié-Gonnard0f6b66d2014-05-07 14:43:46 +0200133 memset( tag, 0x00, sizeof( tag ) );
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200134 memset( result, 0x00, sizeof( result ) );
135
136 key_len = unhexify( key, key_hex );
137 msg_len = unhexify( msg, msg_hex );
138 iv_len = unhexify( iv, iv_hex );
139 add_len = unhexify( add, add_hex );
140 msg_len -= tag_len;
Manuel Pégourié-Gonnard0f6b66d2014-05-07 14:43:46 +0200141 memcpy( tag, msg + msg_len, tag_len );
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200142
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
Manuel Pégourié-Gonnard0f6b66d2014-05-07 14:43:46 +0200155 /* Test with input == output */
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200156 TEST_ASSERT( ccm_auth_decrypt( &ctx, msg_len, iv, iv_len, add, add_len,
Manuel Pégourié-Gonnard0f6b66d2014-05-07 14:43:46 +0200157 msg, msg, msg + msg_len, tag_len ) == ret );
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200158
159 if( ret == 0 )
160 {
Manuel Pégourié-Gonnard0f6b66d2014-05-07 14:43:46 +0200161 TEST_ASSERT( memcmp( msg, result, result_len ) == 0 );
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200162 }
163 else
164 {
165 size_t i;
166
167 for( i = 0; i < msg_len; i++ )
Manuel Pégourié-Gonnard0f6b66d2014-05-07 14:43:46 +0200168 TEST_ASSERT( msg[i] == 0 );
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200169 }
170
Manuel Pégourié-Gonnard0f6b66d2014-05-07 14:43:46 +0200171 /* Check we didn't write past the end (where the original tag is) */
172 TEST_ASSERT( memcmp( msg + msg_len, tag, tag_len ) == 0 );
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200173
174 ccm_free( &ctx );
175}
176/* END_CASE */