blob: d513a15039158f78347ee88d8073923036014799 [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
Paul Bakkerbd51b262014-07-10 15:26:12 +020030exit:
Manuel Pégourié-Gonnard9fe0d132014-05-06 12:12:45 +020031 ccm_free( &ctx );
32}
33/* END_CASE */
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +020034
Manuel Pégourié-Gonnard87df5ba2014-05-06 18:07:24 +020035/* BEGIN_CASE depends_on:POLARSSL_AES_C */
36void 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 Bakkerbd51b262014-07-10 15:26:12 +020068exit:
Manuel Pégourié-Gonnard87df5ba2014-05-06 18:07:24 +020069 ccm_free( &ctx );
70}
71/* END_CASE */
72
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +020073/* BEGIN_CASE */
74void 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é-Gonnarde8b8d012014-05-06 18:19:06 +020079 unsigned char key[32];
80 unsigned char msg[50];
81 unsigned char iv[13];
82 unsigned char add[32];
Manuel Pégourié-Gonnarde8b8d012014-05-06 18:19:06 +020083 unsigned char result[50];
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +020084 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é-Gonnard637eb3d2014-05-06 12:13:09 +020091 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é-Gonnard0f6b66d2014-05-07 14:43:46 +0200102 /* Test with input == output */
Manuel Pégourié-Gonnard00232332014-05-06 15:56:07 +0200103 TEST_ASSERT( ccm_encrypt_and_tag( &ctx, msg_len, iv, iv_len, add, add_len,
Manuel Pégourié-Gonnard0f6b66d2014-05-07 14:43:46 +0200104 msg, msg, msg + msg_len, tag_len ) == 0 );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200105
Manuel Pégourié-Gonnard0f6b66d2014-05-07 14:43:46 +0200106 TEST_ASSERT( memcmp( msg, result, result_len ) == 0 );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200107
108 /* Check we didn't write past the end */
Manuel Pégourié-Gonnard0f6b66d2014-05-07 14:43:46 +0200109 TEST_ASSERT( msg[result_len] == 0 && msg[result_len + 1] == 0 );
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200110
Paul Bakkerbd51b262014-07-10 15:26:12 +0200111exit:
Manuel Pégourié-Gonnard637eb3d2014-05-06 12:13:09 +0200112 ccm_free( &ctx );
113}
114/* END_CASE */
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200115
116/* BEGIN_CASE */
117void 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é-Gonnarde8b8d012014-05-06 18:19:06 +0200122 unsigned char key[32];
123 unsigned char msg[50];
124 unsigned char iv[13];
125 unsigned char add[32];
Manuel Pégourié-Gonnard0f6b66d2014-05-07 14:43:46 +0200126 unsigned char tag[16];
Manuel Pégourié-Gonnarde8b8d012014-05-06 18:19:06 +0200127 unsigned char result[50];
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200128 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é-Gonnard0f6b66d2014-05-07 14:43:46 +0200136 memset( tag, 0x00, sizeof( tag ) );
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200137 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é-Gonnard0f6b66d2014-05-07 14:43:46 +0200144 memcpy( tag, msg + msg_len, tag_len );
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200145
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é-Gonnard0f6b66d2014-05-07 14:43:46 +0200158 /* Test with input == output */
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200159 TEST_ASSERT( ccm_auth_decrypt( &ctx, msg_len, iv, iv_len, add, add_len,
Manuel Pégourié-Gonnard0f6b66d2014-05-07 14:43:46 +0200160 msg, msg, msg + msg_len, tag_len ) == ret );
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200161
162 if( ret == 0 )
163 {
Manuel Pégourié-Gonnard0f6b66d2014-05-07 14:43:46 +0200164 TEST_ASSERT( memcmp( msg, result, result_len ) == 0 );
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200165 }
166 else
167 {
168 size_t i;
169
170 for( i = 0; i < msg_len; i++ )
Manuel Pégourié-Gonnard0f6b66d2014-05-07 14:43:46 +0200171 TEST_ASSERT( msg[i] == 0 );
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200172 }
173
Manuel Pégourié-Gonnard0f6b66d2014-05-07 14:43:46 +0200174 /* 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é-Gonnardce77d552014-05-06 18:06:52 +0200176
Paul Bakkerbd51b262014-07-10 15:26:12 +0200177exit:
Manuel Pégourié-Gonnardce77d552014-05-06 18:06:52 +0200178 ccm_free( &ctx );
179}
180/* END_CASE */