blob: 747914f6bcc2f3f1047e4822e5f7e07ae3711be3 [file] [log] [blame]
Paul Bakker33b43f12013-08-20 11:48:36 +02001/* BEGIN_HEADER */
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002#include "mbedtls/gcm.h"
Gilles Peskine36dd93e2021-04-13 13:02:03 +02003
4/* Use the multipart interface to process the encrypted data in two parts
5 * and check that the output matches the expected output.
6 * The context must have been set up with the key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007static int check_multipart(mbedtls_gcm_context *ctx,
8 int mode,
9 const data_t *iv,
10 const data_t *add,
11 const data_t *input,
12 const data_t *expected_output,
13 const data_t *tag,
14 size_t n1,
15 size_t n1_add)
Gilles Peskine36dd93e2021-04-13 13:02:03 +020016{
17 int ok = 0;
18 uint8_t *output = NULL;
19 size_t n2 = input->len - n1;
Mateusz Starzyk658f4fd2021-05-26 14:26:48 +020020 size_t n2_add = add->len - n1_add;
Gilles Peskinea56c4482021-04-15 17:22:35 +020021 size_t olen;
Gilles Peskine36dd93e2021-04-13 13:02:03 +020022
23 /* Sanity checks on the test data */
Gilles Peskine449bd832023-01-11 14:50:10 +010024 TEST_ASSERT(n1 <= input->len);
25 TEST_ASSERT(n1_add <= add->len);
26 TEST_EQUAL(input->len, expected_output->len);
Gilles Peskine36dd93e2021-04-13 13:02:03 +020027
Gilles Peskine449bd832023-01-11 14:50:10 +010028 TEST_EQUAL(0, mbedtls_gcm_starts(ctx, mode,
29 iv->x, iv->len));
30 TEST_EQUAL(0, mbedtls_gcm_update_ad(ctx, add->x, n1_add));
31 TEST_EQUAL(0, mbedtls_gcm_update_ad(ctx, add->x + n1_add, n2_add));
Gilles Peskine36dd93e2021-04-13 13:02:03 +020032
33 /* Allocate a tight buffer for each update call. This way, if the function
34 * tries to write beyond the advertised required buffer size, this will
35 * count as an overflow for memory sanitizers and static checkers. */
Tom Cosgrove05b2a872023-07-21 11:31:13 +010036 TEST_CALLOC(output, n1);
Gilles Peskinea56c4482021-04-15 17:22:35 +020037 olen = 0xdeadbeef;
Gilles Peskine449bd832023-01-11 14:50:10 +010038 TEST_EQUAL(0, mbedtls_gcm_update(ctx, input->x, n1, output, n1, &olen));
39 TEST_EQUAL(n1, olen);
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +010040 TEST_MEMORY_COMPARE(output, olen, expected_output->x, n1);
Gilles Peskine449bd832023-01-11 14:50:10 +010041 mbedtls_free(output);
Gilles Peskine36dd93e2021-04-13 13:02:03 +020042 output = NULL;
43
Tom Cosgrove05b2a872023-07-21 11:31:13 +010044 TEST_CALLOC(output, n2);
Gilles Peskinea56c4482021-04-15 17:22:35 +020045 olen = 0xdeadbeef;
Gilles Peskine449bd832023-01-11 14:50:10 +010046 TEST_EQUAL(0, mbedtls_gcm_update(ctx, input->x + n1, n2, output, n2, &olen));
47 TEST_EQUAL(n2, olen);
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +010048 TEST_MEMORY_COMPARE(output, olen, expected_output->x + n1, n2);
Gilles Peskine449bd832023-01-11 14:50:10 +010049 mbedtls_free(output);
Gilles Peskine36dd93e2021-04-13 13:02:03 +020050 output = NULL;
51
Tom Cosgrove05b2a872023-07-21 11:31:13 +010052 TEST_CALLOC(output, tag->len);
Gilles Peskine449bd832023-01-11 14:50:10 +010053 TEST_EQUAL(0, mbedtls_gcm_finish(ctx, NULL, 0, &olen, output, tag->len));
54 TEST_EQUAL(0, olen);
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +010055 TEST_MEMORY_COMPARE(output, tag->len, tag->x, tag->len);
Gilles Peskine449bd832023-01-11 14:50:10 +010056 mbedtls_free(output);
Gilles Peskine36dd93e2021-04-13 13:02:03 +020057 output = NULL;
58
59 ok = 1;
60exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010061 mbedtls_free(output);
62 return ok;
Gilles Peskine36dd93e2021-04-13 13:02:03 +020063}
64
Gilles Peskine449bd832023-01-11 14:50:10 +010065static void check_cipher_with_empty_ad(mbedtls_gcm_context *ctx,
66 int mode,
67 const data_t *iv,
68 const data_t *input,
69 const data_t *expected_output,
70 const data_t *tag,
71 size_t ad_update_count)
Mateusz Starzykfc606222021-06-16 11:04:07 +020072{
73 size_t n;
74 uint8_t *output = NULL;
75 size_t olen;
76
77 /* Sanity checks on the test data */
Gilles Peskine449bd832023-01-11 14:50:10 +010078 TEST_EQUAL(input->len, expected_output->len);
Mateusz Starzykfc606222021-06-16 11:04:07 +020079
Gilles Peskine449bd832023-01-11 14:50:10 +010080 TEST_EQUAL(0, mbedtls_gcm_starts(ctx, mode,
81 iv->x, iv->len));
Mateusz Starzykfc606222021-06-16 11:04:07 +020082
Gilles Peskine449bd832023-01-11 14:50:10 +010083 for (n = 0; n < ad_update_count; n++) {
84 TEST_EQUAL(0, mbedtls_gcm_update_ad(ctx, NULL, 0));
Mateusz Starzykfc606222021-06-16 11:04:07 +020085 }
86
87 /* Allocate a tight buffer for each update call. This way, if the function
88 * tries to write beyond the advertised required buffer size, this will
89 * count as an overflow for memory sanitizers and static checkers. */
Tom Cosgrove05b2a872023-07-21 11:31:13 +010090 TEST_CALLOC(output, input->len);
Mateusz Starzykfc606222021-06-16 11:04:07 +020091 olen = 0xdeadbeef;
Gilles Peskine449bd832023-01-11 14:50:10 +010092 TEST_EQUAL(0, mbedtls_gcm_update(ctx, input->x, input->len, output, input->len, &olen));
93 TEST_EQUAL(input->len, olen);
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +010094 TEST_MEMORY_COMPARE(output, olen, expected_output->x, input->len);
Gilles Peskine449bd832023-01-11 14:50:10 +010095 mbedtls_free(output);
Mateusz Starzykfc606222021-06-16 11:04:07 +020096 output = NULL;
97
Tom Cosgrove05b2a872023-07-21 11:31:13 +010098 TEST_CALLOC(output, tag->len);
Gilles Peskine449bd832023-01-11 14:50:10 +010099 TEST_EQUAL(0, mbedtls_gcm_finish(ctx, NULL, 0, &olen, output, tag->len));
100 TEST_EQUAL(0, olen);
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +0100101 TEST_MEMORY_COMPARE(output, tag->len, tag->x, tag->len);
Mateusz Starzykfc606222021-06-16 11:04:07 +0200102
103exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100104 mbedtls_free(output);
Mateusz Starzykfc606222021-06-16 11:04:07 +0200105}
106
Gilles Peskine449bd832023-01-11 14:50:10 +0100107static void check_empty_cipher_with_ad(mbedtls_gcm_context *ctx,
Mateusz Starzykfc606222021-06-16 11:04:07 +0200108 int mode,
109 const data_t *iv,
110 const data_t *add,
111 const data_t *tag,
112 size_t cipher_update_count)
113{
114 size_t olen;
115 size_t n;
Gilles Peskine449bd832023-01-11 14:50:10 +0100116 uint8_t *output_tag = NULL;
Mateusz Starzykfc606222021-06-16 11:04:07 +0200117
Gilles Peskine449bd832023-01-11 14:50:10 +0100118 TEST_EQUAL(0, mbedtls_gcm_starts(ctx, mode, iv->x, iv->len));
119 TEST_EQUAL(0, mbedtls_gcm_update_ad(ctx, add->x, add->len));
Mateusz Starzykfc606222021-06-16 11:04:07 +0200120
Gilles Peskine449bd832023-01-11 14:50:10 +0100121 for (n = 0; n < cipher_update_count; n++) {
Mateusz Starzykfc606222021-06-16 11:04:07 +0200122 olen = 0xdeadbeef;
Gilles Peskine449bd832023-01-11 14:50:10 +0100123 TEST_EQUAL(0, mbedtls_gcm_update(ctx, NULL, 0, NULL, 0, &olen));
124 TEST_EQUAL(0, olen);
Mateusz Starzykfc606222021-06-16 11:04:07 +0200125 }
126
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100127 TEST_CALLOC(output_tag, tag->len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100128 TEST_EQUAL(0, mbedtls_gcm_finish(ctx, NULL, 0, &olen,
129 output_tag, tag->len));
130 TEST_EQUAL(0, olen);
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +0100131 TEST_MEMORY_COMPARE(output_tag, tag->len, tag->x, tag->len);
Mateusz Starzykfc606222021-06-16 11:04:07 +0200132
133exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100134 mbedtls_free(output_tag);
Mateusz Starzykfc606222021-06-16 11:04:07 +0200135}
136
Gilles Peskine449bd832023-01-11 14:50:10 +0100137static void check_no_cipher_no_ad(mbedtls_gcm_context *ctx,
138 int mode,
139 const data_t *iv,
140 const data_t *tag)
Mateusz Starzyk469c9f32021-06-18 00:06:52 +0200141{
142 uint8_t *output = NULL;
Gilles Peskine5a7be102021-06-23 21:51:32 +0200143 size_t olen = 0;
Mateusz Starzyk469c9f32021-06-18 00:06:52 +0200144
Gilles Peskine449bd832023-01-11 14:50:10 +0100145 TEST_EQUAL(0, mbedtls_gcm_starts(ctx, mode,
146 iv->x, iv->len));
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100147 TEST_CALLOC(output, tag->len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100148 TEST_EQUAL(0, mbedtls_gcm_finish(ctx, NULL, 0, &olen, output, tag->len));
149 TEST_EQUAL(0, olen);
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +0100150 TEST_MEMORY_COMPARE(output, tag->len, tag->x, tag->len);
Mateusz Starzyk469c9f32021-06-18 00:06:52 +0200151
152exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100153 mbedtls_free(output);
Mateusz Starzyk469c9f32021-06-18 00:06:52 +0200154}
155
Paul Bakker33b43f12013-08-20 11:48:36 +0200156/* END_HEADER */
Paul Bakker89e80c92012-03-20 13:50:09 +0000157
Paul Bakker33b43f12013-08-20 11:48:36 +0200158/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159 * depends_on:MBEDTLS_GCM_C
Paul Bakker33b43f12013-08-20 11:48:36 +0200160 * END_DEPENDENCIES
161 */
Paul Bakker89e80c92012-03-20 13:50:09 +0000162
Paul Bakker33b43f12013-08-20 11:48:36 +0200163/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100164void gcm_bad_parameters(int cipher_id, int direction,
165 data_t *key_str, data_t *src_str,
166 data_t *iv_str, data_t *add_str,
167 int tag_len_bits, int gcm_result)
Ron Eldor5a21fd62016-12-16 16:15:56 +0200168{
Ron Eldor5a21fd62016-12-16 16:15:56 +0200169 unsigned char output[128];
170 unsigned char tag_output[16];
171 mbedtls_gcm_context ctx;
Azim Khan317efe82017-08-02 17:33:54 +0100172 size_t tag_len = tag_len_bits / 8;
Ron Eldor5a21fd62016-12-16 16:15:56 +0200173
Gilles Peskine449bd832023-01-11 14:50:10 +0100174 mbedtls_gcm_init(&ctx);
Ron Eldor5a21fd62016-12-16 16:15:56 +0200175
Gilles Peskine449bd832023-01-11 14:50:10 +0100176 memset(output, 0x00, sizeof(output));
177 memset(tag_output, 0x00, sizeof(tag_output));
Darryl Green11999bb2018-03-13 15:22:58 +0000178
Gilles Peskine449bd832023-01-11 14:50:10 +0100179 TEST_ASSERT(mbedtls_gcm_setkey(&ctx, cipher_id, key_str->x, key_str->len * 8) == 0);
180 TEST_ASSERT(mbedtls_gcm_crypt_and_tag(&ctx, direction, src_str->len, iv_str->x, iv_str->len,
181 add_str->x, add_str->len, src_str->x, output, tag_len,
182 tag_output) == gcm_result);
Ron Eldor5a21fd62016-12-16 16:15:56 +0200183
184exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100185 mbedtls_gcm_free(&ctx);
Ron Eldor5a21fd62016-12-16 16:15:56 +0200186}
187/* END_CASE */
188
189/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100190void gcm_encrypt_and_tag(int cipher_id, data_t *key_str,
191 data_t *src_str, data_t *iv_str,
192 data_t *add_str, data_t *dst,
193 int tag_len_bits, data_t *tag,
194 int init_result)
Paul Bakker89e80c92012-03-20 13:50:09 +0000195{
Paul Bakker89e80c92012-03-20 13:50:09 +0000196 unsigned char output[128];
197 unsigned char tag_output[16];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200198 mbedtls_gcm_context ctx;
Azim Khanf1aaec92017-05-30 14:23:15 +0100199 size_t tag_len = tag_len_bits / 8;
Gilles Peskine36dd93e2021-04-13 13:02:03 +0200200 size_t n1;
Mateusz Starzykaf4ecdd2021-06-15 15:29:48 +0200201 size_t n1_add;
Paul Bakker89e80c92012-03-20 13:50:09 +0000202
Gilles Peskine449bd832023-01-11 14:50:10 +0100203 mbedtls_gcm_init(&ctx);
Manuel Pégourié-Gonnardc34e8dd2015-04-28 21:42:17 +0200204
Paul Bakker89e80c92012-03-20 13:50:09 +0000205 memset(output, 0x00, 128);
206 memset(tag_output, 0x00, 16);
207
Paul Bakker89e80c92012-03-20 13:50:09 +0000208
Gilles Peskine449bd832023-01-11 14:50:10 +0100209 TEST_ASSERT(mbedtls_gcm_setkey(&ctx, cipher_id, key_str->x, key_str->len * 8) == init_result);
210 if (init_result == 0) {
211 TEST_ASSERT(mbedtls_gcm_crypt_and_tag(&ctx, MBEDTLS_GCM_ENCRYPT, src_str->len, iv_str->x,
212 iv_str->len, add_str->x, add_str->len, src_str->x,
213 output, tag_len, tag_output) == 0);
Paul Bakker89e80c92012-03-20 13:50:09 +0000214
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +0100215 TEST_MEMORY_COMPARE(output, src_str->len, dst->x, dst->len);
216 TEST_MEMORY_COMPARE(tag_output, tag_len, tag->x, tag->len);
Gilles Peskine36dd93e2021-04-13 13:02:03 +0200217
Gilles Peskine449bd832023-01-11 14:50:10 +0100218 for (n1 = 0; n1 <= src_str->len; n1 += 1) {
219 for (n1_add = 0; n1_add <= add_str->len; n1_add += 1) {
220 mbedtls_test_set_step(n1 * 10000 + n1_add);
221 if (!check_multipart(&ctx, MBEDTLS_GCM_ENCRYPT,
222 iv_str, add_str, src_str,
223 dst, tag,
224 n1, n1_add)) {
Mateusz Starzykaf4ecdd2021-06-15 15:29:48 +0200225 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100226 }
Mateusz Starzykaf4ecdd2021-06-15 15:29:48 +0200227 }
Gilles Peskine36dd93e2021-04-13 13:02:03 +0200228 }
Paul Bakker89e80c92012-03-20 13:50:09 +0000229 }
Manuel Pégourié-Gonnard4fe92002013-09-13 13:45:58 +0200230
Paul Bakkerbd51b262014-07-10 15:26:12 +0200231exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100232 mbedtls_gcm_free(&ctx);
Paul Bakker89e80c92012-03-20 13:50:09 +0000233}
Paul Bakker33b43f12013-08-20 11:48:36 +0200234/* END_CASE */
Paul Bakker89e80c92012-03-20 13:50:09 +0000235
Paul Bakker33b43f12013-08-20 11:48:36 +0200236/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100237void gcm_decrypt_and_verify(int cipher_id, data_t *key_str,
238 data_t *src_str, data_t *iv_str,
239 data_t *add_str, int tag_len_bits,
240 data_t *tag_str, char *result,
241 data_t *pt_result, int init_result)
Paul Bakker89e80c92012-03-20 13:50:09 +0000242{
Paul Bakker89e80c92012-03-20 13:50:09 +0000243 unsigned char output[128];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200244 mbedtls_gcm_context ctx;
Paul Bakker89e80c92012-03-20 13:50:09 +0000245 int ret;
Azim Khanf1aaec92017-05-30 14:23:15 +0100246 size_t tag_len = tag_len_bits / 8;
Gilles Peskine36dd93e2021-04-13 13:02:03 +0200247 size_t n1;
Mateusz Starzykaf4ecdd2021-06-15 15:29:48 +0200248 size_t n1_add;
Paul Bakker89e80c92012-03-20 13:50:09 +0000249
Gilles Peskine449bd832023-01-11 14:50:10 +0100250 mbedtls_gcm_init(&ctx);
Manuel Pégourié-Gonnardc34e8dd2015-04-28 21:42:17 +0200251
Paul Bakker89e80c92012-03-20 13:50:09 +0000252 memset(output, 0x00, 128);
253
Paul Bakker89e80c92012-03-20 13:50:09 +0000254
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 TEST_ASSERT(mbedtls_gcm_setkey(&ctx, cipher_id, key_str->x, key_str->len * 8) == init_result);
256 if (init_result == 0) {
257 ret = mbedtls_gcm_auth_decrypt(&ctx,
258 src_str->len,
259 iv_str->x,
260 iv_str->len,
261 add_str->x,
262 add_str->len,
263 tag_str->x,
264 tag_len,
265 src_str->x,
266 output);
Paul Bakker89e80c92012-03-20 13:50:09 +0000267
Gilles Peskine449bd832023-01-11 14:50:10 +0100268 if (strcmp("FAIL", result) == 0) {
269 TEST_ASSERT(ret == MBEDTLS_ERR_GCM_AUTH_FAILED);
270 } else {
271 TEST_ASSERT(ret == 0);
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +0100272 TEST_MEMORY_COMPARE(output, src_str->len, pt_result->x, pt_result->len);
Paul Bakker89e80c92012-03-20 13:50:09 +0000273
Gilles Peskine449bd832023-01-11 14:50:10 +0100274 for (n1 = 0; n1 <= src_str->len; n1 += 1) {
275 for (n1_add = 0; n1_add <= add_str->len; n1_add += 1) {
276 mbedtls_test_set_step(n1 * 10000 + n1_add);
277 if (!check_multipart(&ctx, MBEDTLS_GCM_DECRYPT,
278 iv_str, add_str, src_str,
279 pt_result, tag_str,
280 n1, n1_add)) {
Mateusz Starzykaf4ecdd2021-06-15 15:29:48 +0200281 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100282 }
Mateusz Starzykaf4ecdd2021-06-15 15:29:48 +0200283 }
Gilles Peskine36dd93e2021-04-13 13:02:03 +0200284 }
Paul Bakker89e80c92012-03-20 13:50:09 +0000285 }
286 }
Manuel Pégourié-Gonnard4fe92002013-09-13 13:45:58 +0200287
Paul Bakkerbd51b262014-07-10 15:26:12 +0200288exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100289 mbedtls_gcm_free(&ctx);
Paul Bakker89e80c92012-03-20 13:50:09 +0000290}
Paul Bakker33b43f12013-08-20 11:48:36 +0200291/* END_CASE */
Paul Bakker89e80c92012-03-20 13:50:09 +0000292
Mateusz Starzykfc606222021-06-16 11:04:07 +0200293/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100294void gcm_decrypt_and_verify_empty_cipher(int cipher_id,
295 data_t *key_str,
296 data_t *iv_str,
297 data_t *add_str,
298 data_t *tag_str,
299 int cipher_update_calls)
Mateusz Starzykfc606222021-06-16 11:04:07 +0200300{
301 mbedtls_gcm_context ctx;
302
Gilles Peskine449bd832023-01-11 14:50:10 +0100303 mbedtls_gcm_init(&ctx);
Mateusz Starzykfc606222021-06-16 11:04:07 +0200304
Gilles Peskine449bd832023-01-11 14:50:10 +0100305 TEST_ASSERT(mbedtls_gcm_setkey(&ctx, cipher_id, key_str->x, key_str->len * 8) == 0);
306 check_empty_cipher_with_ad(&ctx, MBEDTLS_GCM_DECRYPT,
307 iv_str, add_str, tag_str,
308 cipher_update_calls);
Mateusz Starzykfc606222021-06-16 11:04:07 +0200309
Gilles Peskine449bd832023-01-11 14:50:10 +0100310 mbedtls_gcm_free(&ctx);
Mateusz Starzykfc606222021-06-16 11:04:07 +0200311}
312/* END_CASE */
313
314/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100315void gcm_decrypt_and_verify_empty_ad(int cipher_id,
316 data_t *key_str,
317 data_t *iv_str,
318 data_t *src_str,
319 data_t *tag_str,
320 data_t *pt_result,
321 int ad_update_calls)
Mateusz Starzykfc606222021-06-16 11:04:07 +0200322{
323 mbedtls_gcm_context ctx;
324
Gilles Peskine449bd832023-01-11 14:50:10 +0100325 mbedtls_gcm_init(&ctx);
Mateusz Starzykfc606222021-06-16 11:04:07 +0200326
Gilles Peskine449bd832023-01-11 14:50:10 +0100327 TEST_ASSERT(mbedtls_gcm_setkey(&ctx, cipher_id, key_str->x, key_str->len * 8) == 0);
328 check_cipher_with_empty_ad(&ctx, MBEDTLS_GCM_DECRYPT,
329 iv_str, src_str, pt_result, tag_str,
330 ad_update_calls);
Mateusz Starzykfc606222021-06-16 11:04:07 +0200331
Gilles Peskine449bd832023-01-11 14:50:10 +0100332 mbedtls_gcm_free(&ctx);
Mateusz Starzykfc606222021-06-16 11:04:07 +0200333}
334/* END_CASE */
335
336/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100337void gcm_decrypt_and_verify_no_ad_no_cipher(int cipher_id,
338 data_t *key_str,
339 data_t *iv_str,
340 data_t *tag_str)
Mateusz Starzyk469c9f32021-06-18 00:06:52 +0200341{
342 mbedtls_gcm_context ctx;
343
Gilles Peskine449bd832023-01-11 14:50:10 +0100344 mbedtls_gcm_init(&ctx);
Mateusz Starzyk469c9f32021-06-18 00:06:52 +0200345
Gilles Peskine449bd832023-01-11 14:50:10 +0100346 TEST_ASSERT(mbedtls_gcm_setkey(&ctx, cipher_id, key_str->x, key_str->len * 8) == 0);
347 check_no_cipher_no_ad(&ctx, MBEDTLS_GCM_DECRYPT,
348 iv_str, tag_str);
Mateusz Starzyk469c9f32021-06-18 00:06:52 +0200349
Gilles Peskine449bd832023-01-11 14:50:10 +0100350 mbedtls_gcm_free(&ctx);
Mateusz Starzyk469c9f32021-06-18 00:06:52 +0200351}
352/* END_CASE */
353
354/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100355void gcm_encrypt_and_tag_empty_cipher(int cipher_id,
356 data_t *key_str,
357 data_t *iv_str,
358 data_t *add_str,
359 data_t *tag_str,
360 int cipher_update_calls)
Mateusz Starzykfc606222021-06-16 11:04:07 +0200361{
362 mbedtls_gcm_context ctx;
363
Gilles Peskine449bd832023-01-11 14:50:10 +0100364 mbedtls_gcm_init(&ctx);
Mateusz Starzykfc606222021-06-16 11:04:07 +0200365
Gilles Peskine449bd832023-01-11 14:50:10 +0100366 TEST_ASSERT(mbedtls_gcm_setkey(&ctx, cipher_id, key_str->x, key_str->len * 8) == 0);
367 check_empty_cipher_with_ad(&ctx, MBEDTLS_GCM_ENCRYPT,
368 iv_str, add_str, tag_str,
369 cipher_update_calls);
Mateusz Starzykfc606222021-06-16 11:04:07 +0200370
371exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100372 mbedtls_gcm_free(&ctx);
Mateusz Starzykfc606222021-06-16 11:04:07 +0200373}
374/* END_CASE */
375
376/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100377void gcm_encrypt_and_tag_empty_ad(int cipher_id,
378 data_t *key_str,
379 data_t *iv_str,
380 data_t *src_str,
381 data_t *dst,
382 data_t *tag_str,
383 int ad_update_calls)
Mateusz Starzykfc606222021-06-16 11:04:07 +0200384{
385 mbedtls_gcm_context ctx;
386
Gilles Peskine449bd832023-01-11 14:50:10 +0100387 mbedtls_gcm_init(&ctx);
Mateusz Starzykfc606222021-06-16 11:04:07 +0200388
Gilles Peskine449bd832023-01-11 14:50:10 +0100389 TEST_ASSERT(mbedtls_gcm_setkey(&ctx, cipher_id, key_str->x, key_str->len * 8) == 0);
390 check_cipher_with_empty_ad(&ctx, MBEDTLS_GCM_ENCRYPT,
391 iv_str, src_str, dst, tag_str,
392 ad_update_calls);
Mateusz Starzykfc606222021-06-16 11:04:07 +0200393
394exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100395 mbedtls_gcm_free(&ctx);
Mateusz Starzykfc606222021-06-16 11:04:07 +0200396}
397/* END_CASE */
398
Mateusz Starzyk469c9f32021-06-18 00:06:52 +0200399/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100400void gcm_encrypt_and_verify_no_ad_no_cipher(int cipher_id,
401 data_t *key_str,
402 data_t *iv_str,
403 data_t *tag_str)
Mateusz Starzyk469c9f32021-06-18 00:06:52 +0200404{
405 mbedtls_gcm_context ctx;
406
Gilles Peskine449bd832023-01-11 14:50:10 +0100407 mbedtls_gcm_init(&ctx);
Mateusz Starzyk469c9f32021-06-18 00:06:52 +0200408
Gilles Peskine449bd832023-01-11 14:50:10 +0100409 TEST_ASSERT(mbedtls_gcm_setkey(&ctx, cipher_id, key_str->x, key_str->len * 8) == 0);
410 check_no_cipher_no_ad(&ctx, MBEDTLS_GCM_ENCRYPT,
411 iv_str, tag_str);
Mateusz Starzyk469c9f32021-06-18 00:06:52 +0200412
Gilles Peskine449bd832023-01-11 14:50:10 +0100413 mbedtls_gcm_free(&ctx);
Mateusz Starzyk469c9f32021-06-18 00:06:52 +0200414}
415/* END_CASE */
416
Tuvshinzaya Erdenekhuu104eb7f2022-07-29 14:48:21 +0100417/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100418void gcm_invalid_param()
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500419{
420 mbedtls_gcm_context ctx;
421 unsigned char valid_buffer[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 };
422 mbedtls_cipher_id_t valid_cipher = MBEDTLS_CIPHER_ID_AES;
Ronald Cron875b5fb2021-05-21 08:50:00 +0200423 int invalid_bitlen = 1;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500424
Gilles Peskine449bd832023-01-11 14:50:10 +0100425 mbedtls_gcm_init(&ctx);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500426
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500427 /* mbedtls_gcm_setkey */
Ronald Cron875b5fb2021-05-21 08:50:00 +0200428 TEST_EQUAL(
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500429 MBEDTLS_ERR_GCM_BAD_INPUT,
Gilles Peskine449bd832023-01-11 14:50:10 +0100430 mbedtls_gcm_setkey(&ctx, valid_cipher, valid_buffer, invalid_bitlen));
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500431
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500432exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100433 mbedtls_gcm_free(&ctx);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500434}
435/* END_CASE */
436
Mateusz Starzykc48f43b2021-10-04 13:46:38 +0200437/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100438void gcm_update_output_buffer_too_small(int cipher_id, int mode,
439 data_t *key_str, const data_t *input,
440 const data_t *iv)
Mateusz Starzykc48f43b2021-10-04 13:46:38 +0200441{
442 mbedtls_gcm_context ctx;
443 uint8_t *output = NULL;
Mateusz Starzyk33d01ff2021-10-21 14:55:59 +0200444 size_t olen = 0;
Mateusz Starzykc48f43b2021-10-04 13:46:38 +0200445 size_t output_len = input->len - 1;
446
Gilles Peskine449bd832023-01-11 14:50:10 +0100447 mbedtls_gcm_init(&ctx);
448 TEST_EQUAL(mbedtls_gcm_setkey(&ctx, cipher_id, key_str->x, key_str->len * 8), 0);
449 TEST_EQUAL(0, mbedtls_gcm_starts(&ctx, mode, iv->x, iv->len));
Mateusz Starzykc48f43b2021-10-04 13:46:38 +0200450
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100451 TEST_CALLOC(output, output_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100452 TEST_EQUAL(MBEDTLS_ERR_GCM_BUFFER_TOO_SMALL,
453 mbedtls_gcm_update(&ctx, input->x, input->len, output, output_len, &olen));
Mateusz Starzykc48f43b2021-10-04 13:46:38 +0200454
455exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100456 mbedtls_free(output);
457 mbedtls_gcm_free(&ctx);
Mateusz Starzykc48f43b2021-10-04 13:46:38 +0200458}
459/* END_CASE */
460
Andrzej Kurekf502bcb2022-09-27 09:27:56 -0400461/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST:MBEDTLS_AES_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100462void gcm_selftest()
Paul Bakker89e80c92012-03-20 13:50:09 +0000463{
Gilles Peskine449bd832023-01-11 14:50:10 +0100464 TEST_ASSERT(mbedtls_gcm_self_test(1) == 0);
Paul Bakker89e80c92012-03-20 13:50:09 +0000465}
Paul Bakker33b43f12013-08-20 11:48:36 +0200466/* END_CASE */