blob: 43c11c3d00429697b638e4101c47eae2da4cd19c [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
Chien Wong99ff1f52024-01-24 20:44:01 +0800156static void gcm_reset_ctx(mbedtls_gcm_context *ctx, const uint8_t *key,
157 size_t key_bits, const uint8_t *iv, size_t iv_len,
158 int starts_ret)
159{
160 int mode = MBEDTLS_GCM_ENCRYPT;
161 mbedtls_cipher_id_t valid_cipher = MBEDTLS_CIPHER_ID_AES;
162
163 mbedtls_gcm_init(ctx);
164 TEST_EQUAL(mbedtls_gcm_setkey(ctx, valid_cipher, key, key_bits), 0);
165 TEST_EQUAL(starts_ret, mbedtls_gcm_starts(ctx, mode, iv, iv_len));
166exit:
167 /* empty */
Dave Rodgman68232472024-01-31 15:59:06 +0000168 return;
Chien Wong99ff1f52024-01-24 20:44:01 +0800169}
170
Paul Bakker33b43f12013-08-20 11:48:36 +0200171/* END_HEADER */
Paul Bakker89e80c92012-03-20 13:50:09 +0000172
Paul Bakker33b43f12013-08-20 11:48:36 +0200173/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200174 * depends_on:MBEDTLS_GCM_C
Paul Bakker33b43f12013-08-20 11:48:36 +0200175 * END_DEPENDENCIES
176 */
Paul Bakker89e80c92012-03-20 13:50:09 +0000177
Paul Bakker33b43f12013-08-20 11:48:36 +0200178/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100179void gcm_bad_parameters(int cipher_id, int direction,
180 data_t *key_str, data_t *src_str,
181 data_t *iv_str, data_t *add_str,
182 int tag_len_bits, int gcm_result)
Ron Eldor5a21fd62016-12-16 16:15:56 +0200183{
Ron Eldor5a21fd62016-12-16 16:15:56 +0200184 unsigned char output[128];
185 unsigned char tag_output[16];
186 mbedtls_gcm_context ctx;
Azim Khan317efe82017-08-02 17:33:54 +0100187 size_t tag_len = tag_len_bits / 8;
Ron Eldor5a21fd62016-12-16 16:15:56 +0200188
Valerio Setti10e9aa22023-12-12 11:54:20 +0100189 BLOCK_CIPHER_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100190 mbedtls_gcm_init(&ctx);
Ron Eldor5a21fd62016-12-16 16:15:56 +0200191
Gilles Peskine449bd832023-01-11 14:50:10 +0100192 memset(output, 0x00, sizeof(output));
193 memset(tag_output, 0x00, sizeof(tag_output));
Darryl Green11999bb2018-03-13 15:22:58 +0000194
Gilles Peskine449bd832023-01-11 14:50:10 +0100195 TEST_ASSERT(mbedtls_gcm_setkey(&ctx, cipher_id, key_str->x, key_str->len * 8) == 0);
196 TEST_ASSERT(mbedtls_gcm_crypt_and_tag(&ctx, direction, src_str->len, iv_str->x, iv_str->len,
197 add_str->x, add_str->len, src_str->x, output, tag_len,
198 tag_output) == gcm_result);
Ron Eldor5a21fd62016-12-16 16:15:56 +0200199
200exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100201 mbedtls_gcm_free(&ctx);
Valerio Setti10e9aa22023-12-12 11:54:20 +0100202 BLOCK_CIPHER_PSA_DONE();
Ron Eldor5a21fd62016-12-16 16:15:56 +0200203}
204/* END_CASE */
205
206/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100207void gcm_encrypt_and_tag(int cipher_id, data_t *key_str,
208 data_t *src_str, data_t *iv_str,
209 data_t *add_str, data_t *dst,
210 int tag_len_bits, data_t *tag,
211 int init_result)
Paul Bakker89e80c92012-03-20 13:50:09 +0000212{
Paul Bakker89e80c92012-03-20 13:50:09 +0000213 unsigned char output[128];
214 unsigned char tag_output[16];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215 mbedtls_gcm_context ctx;
Azim Khanf1aaec92017-05-30 14:23:15 +0100216 size_t tag_len = tag_len_bits / 8;
Gilles Peskine36dd93e2021-04-13 13:02:03 +0200217 size_t n1;
Mateusz Starzykaf4ecdd2021-06-15 15:29:48 +0200218 size_t n1_add;
Paul Bakker89e80c92012-03-20 13:50:09 +0000219
Valerio Setti10e9aa22023-12-12 11:54:20 +0100220 BLOCK_CIPHER_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100221 mbedtls_gcm_init(&ctx);
Manuel Pégourié-Gonnardc34e8dd2015-04-28 21:42:17 +0200222
Paul Bakker89e80c92012-03-20 13:50:09 +0000223 memset(output, 0x00, 128);
224 memset(tag_output, 0x00, 16);
225
Paul Bakker89e80c92012-03-20 13:50:09 +0000226
Gilles Peskine449bd832023-01-11 14:50:10 +0100227 TEST_ASSERT(mbedtls_gcm_setkey(&ctx, cipher_id, key_str->x, key_str->len * 8) == init_result);
228 if (init_result == 0) {
229 TEST_ASSERT(mbedtls_gcm_crypt_and_tag(&ctx, MBEDTLS_GCM_ENCRYPT, src_str->len, iv_str->x,
230 iv_str->len, add_str->x, add_str->len, src_str->x,
231 output, tag_len, tag_output) == 0);
Paul Bakker89e80c92012-03-20 13:50:09 +0000232
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +0100233 TEST_MEMORY_COMPARE(output, src_str->len, dst->x, dst->len);
234 TEST_MEMORY_COMPARE(tag_output, tag_len, tag->x, tag->len);
Gilles Peskine36dd93e2021-04-13 13:02:03 +0200235
Gilles Peskine449bd832023-01-11 14:50:10 +0100236 for (n1 = 0; n1 <= src_str->len; n1 += 1) {
237 for (n1_add = 0; n1_add <= add_str->len; n1_add += 1) {
238 mbedtls_test_set_step(n1 * 10000 + n1_add);
239 if (!check_multipart(&ctx, MBEDTLS_GCM_ENCRYPT,
240 iv_str, add_str, src_str,
241 dst, tag,
242 n1, n1_add)) {
Mateusz Starzykaf4ecdd2021-06-15 15:29:48 +0200243 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100244 }
Mateusz Starzykaf4ecdd2021-06-15 15:29:48 +0200245 }
Gilles Peskine36dd93e2021-04-13 13:02:03 +0200246 }
Paul Bakker89e80c92012-03-20 13:50:09 +0000247 }
Manuel Pégourié-Gonnard4fe92002013-09-13 13:45:58 +0200248
Paul Bakkerbd51b262014-07-10 15:26:12 +0200249exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100250 mbedtls_gcm_free(&ctx);
Valerio Setti10e9aa22023-12-12 11:54:20 +0100251 BLOCK_CIPHER_PSA_DONE();
Paul Bakker89e80c92012-03-20 13:50:09 +0000252}
Paul Bakker33b43f12013-08-20 11:48:36 +0200253/* END_CASE */
Paul Bakker89e80c92012-03-20 13:50:09 +0000254
Paul Bakker33b43f12013-08-20 11:48:36 +0200255/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100256void gcm_decrypt_and_verify(int cipher_id, data_t *key_str,
257 data_t *src_str, data_t *iv_str,
258 data_t *add_str, int tag_len_bits,
259 data_t *tag_str, char *result,
260 data_t *pt_result, int init_result)
Paul Bakker89e80c92012-03-20 13:50:09 +0000261{
Paul Bakker89e80c92012-03-20 13:50:09 +0000262 unsigned char output[128];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200263 mbedtls_gcm_context ctx;
Paul Bakker89e80c92012-03-20 13:50:09 +0000264 int ret;
Azim Khanf1aaec92017-05-30 14:23:15 +0100265 size_t tag_len = tag_len_bits / 8;
Gilles Peskine36dd93e2021-04-13 13:02:03 +0200266 size_t n1;
Mateusz Starzykaf4ecdd2021-06-15 15:29:48 +0200267 size_t n1_add;
Paul Bakker89e80c92012-03-20 13:50:09 +0000268
Valerio Setti10e9aa22023-12-12 11:54:20 +0100269 BLOCK_CIPHER_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100270 mbedtls_gcm_init(&ctx);
Manuel Pégourié-Gonnardc34e8dd2015-04-28 21:42:17 +0200271
Paul Bakker89e80c92012-03-20 13:50:09 +0000272 memset(output, 0x00, 128);
273
Paul Bakker89e80c92012-03-20 13:50:09 +0000274
Gilles Peskine449bd832023-01-11 14:50:10 +0100275 TEST_ASSERT(mbedtls_gcm_setkey(&ctx, cipher_id, key_str->x, key_str->len * 8) == init_result);
276 if (init_result == 0) {
277 ret = mbedtls_gcm_auth_decrypt(&ctx,
278 src_str->len,
279 iv_str->x,
280 iv_str->len,
281 add_str->x,
282 add_str->len,
283 tag_str->x,
284 tag_len,
285 src_str->x,
286 output);
Paul Bakker89e80c92012-03-20 13:50:09 +0000287
Gilles Peskine449bd832023-01-11 14:50:10 +0100288 if (strcmp("FAIL", result) == 0) {
289 TEST_ASSERT(ret == MBEDTLS_ERR_GCM_AUTH_FAILED);
290 } else {
291 TEST_ASSERT(ret == 0);
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +0100292 TEST_MEMORY_COMPARE(output, src_str->len, pt_result->x, pt_result->len);
Paul Bakker89e80c92012-03-20 13:50:09 +0000293
Gilles Peskine449bd832023-01-11 14:50:10 +0100294 for (n1 = 0; n1 <= src_str->len; n1 += 1) {
295 for (n1_add = 0; n1_add <= add_str->len; n1_add += 1) {
296 mbedtls_test_set_step(n1 * 10000 + n1_add);
297 if (!check_multipart(&ctx, MBEDTLS_GCM_DECRYPT,
298 iv_str, add_str, src_str,
299 pt_result, tag_str,
300 n1, n1_add)) {
Mateusz Starzykaf4ecdd2021-06-15 15:29:48 +0200301 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100302 }
Mateusz Starzykaf4ecdd2021-06-15 15:29:48 +0200303 }
Gilles Peskine36dd93e2021-04-13 13:02:03 +0200304 }
Paul Bakker89e80c92012-03-20 13:50:09 +0000305 }
306 }
Manuel Pégourié-Gonnard4fe92002013-09-13 13:45:58 +0200307
Paul Bakkerbd51b262014-07-10 15:26:12 +0200308exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100309 mbedtls_gcm_free(&ctx);
Valerio Setti10e9aa22023-12-12 11:54:20 +0100310 BLOCK_CIPHER_PSA_DONE();
Paul Bakker89e80c92012-03-20 13:50:09 +0000311}
Paul Bakker33b43f12013-08-20 11:48:36 +0200312/* END_CASE */
Paul Bakker89e80c92012-03-20 13:50:09 +0000313
Mateusz Starzykfc606222021-06-16 11:04:07 +0200314/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100315void gcm_decrypt_and_verify_empty_cipher(int cipher_id,
316 data_t *key_str,
317 data_t *iv_str,
318 data_t *add_str,
319 data_t *tag_str,
320 int cipher_update_calls)
Mateusz Starzykfc606222021-06-16 11:04:07 +0200321{
322 mbedtls_gcm_context ctx;
323
Valerio Setti10e9aa22023-12-12 11:54:20 +0100324 BLOCK_CIPHER_PSA_INIT();
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_empty_cipher_with_ad(&ctx, MBEDTLS_GCM_DECRYPT,
329 iv_str, add_str, tag_str,
330 cipher_update_calls);
Mateusz Starzykfc606222021-06-16 11:04:07 +0200331
Gilles Peskine449bd832023-01-11 14:50:10 +0100332 mbedtls_gcm_free(&ctx);
Valerio Setti10e9aa22023-12-12 11:54:20 +0100333 BLOCK_CIPHER_PSA_DONE();
Mateusz Starzykfc606222021-06-16 11:04:07 +0200334}
335/* END_CASE */
336
337/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100338void gcm_decrypt_and_verify_empty_ad(int cipher_id,
339 data_t *key_str,
340 data_t *iv_str,
341 data_t *src_str,
342 data_t *tag_str,
343 data_t *pt_result,
344 int ad_update_calls)
Mateusz Starzykfc606222021-06-16 11:04:07 +0200345{
346 mbedtls_gcm_context ctx;
347
Valerio Setti10e9aa22023-12-12 11:54:20 +0100348 BLOCK_CIPHER_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100349 mbedtls_gcm_init(&ctx);
Mateusz Starzykfc606222021-06-16 11:04:07 +0200350
Gilles Peskine449bd832023-01-11 14:50:10 +0100351 TEST_ASSERT(mbedtls_gcm_setkey(&ctx, cipher_id, key_str->x, key_str->len * 8) == 0);
352 check_cipher_with_empty_ad(&ctx, MBEDTLS_GCM_DECRYPT,
353 iv_str, src_str, pt_result, tag_str,
354 ad_update_calls);
Mateusz Starzykfc606222021-06-16 11:04:07 +0200355
Gilles Peskine449bd832023-01-11 14:50:10 +0100356 mbedtls_gcm_free(&ctx);
Valerio Setti10e9aa22023-12-12 11:54:20 +0100357 BLOCK_CIPHER_PSA_DONE();
Mateusz Starzykfc606222021-06-16 11:04:07 +0200358}
359/* END_CASE */
360
361/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100362void gcm_decrypt_and_verify_no_ad_no_cipher(int cipher_id,
363 data_t *key_str,
364 data_t *iv_str,
365 data_t *tag_str)
Mateusz Starzyk469c9f32021-06-18 00:06:52 +0200366{
367 mbedtls_gcm_context ctx;
368
Valerio Setti10e9aa22023-12-12 11:54:20 +0100369 BLOCK_CIPHER_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100370 mbedtls_gcm_init(&ctx);
Mateusz Starzyk469c9f32021-06-18 00:06:52 +0200371
Gilles Peskine449bd832023-01-11 14:50:10 +0100372 TEST_ASSERT(mbedtls_gcm_setkey(&ctx, cipher_id, key_str->x, key_str->len * 8) == 0);
373 check_no_cipher_no_ad(&ctx, MBEDTLS_GCM_DECRYPT,
374 iv_str, tag_str);
Mateusz Starzyk469c9f32021-06-18 00:06:52 +0200375
Gilles Peskine449bd832023-01-11 14:50:10 +0100376 mbedtls_gcm_free(&ctx);
Valerio Setti10e9aa22023-12-12 11:54:20 +0100377 BLOCK_CIPHER_PSA_DONE();
Mateusz Starzyk469c9f32021-06-18 00:06:52 +0200378}
379/* END_CASE */
380
381/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100382void gcm_encrypt_and_tag_empty_cipher(int cipher_id,
383 data_t *key_str,
384 data_t *iv_str,
385 data_t *add_str,
386 data_t *tag_str,
387 int cipher_update_calls)
Mateusz Starzykfc606222021-06-16 11:04:07 +0200388{
389 mbedtls_gcm_context ctx;
390
Valerio Setti10e9aa22023-12-12 11:54:20 +0100391 BLOCK_CIPHER_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100392 mbedtls_gcm_init(&ctx);
Mateusz Starzykfc606222021-06-16 11:04:07 +0200393
Gilles Peskine449bd832023-01-11 14:50:10 +0100394 TEST_ASSERT(mbedtls_gcm_setkey(&ctx, cipher_id, key_str->x, key_str->len * 8) == 0);
395 check_empty_cipher_with_ad(&ctx, MBEDTLS_GCM_ENCRYPT,
396 iv_str, add_str, tag_str,
397 cipher_update_calls);
Mateusz Starzykfc606222021-06-16 11:04:07 +0200398
399exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100400 mbedtls_gcm_free(&ctx);
Valerio Setti10e9aa22023-12-12 11:54:20 +0100401 BLOCK_CIPHER_PSA_DONE();
Mateusz Starzykfc606222021-06-16 11:04:07 +0200402}
403/* END_CASE */
404
405/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100406void gcm_encrypt_and_tag_empty_ad(int cipher_id,
407 data_t *key_str,
408 data_t *iv_str,
409 data_t *src_str,
410 data_t *dst,
411 data_t *tag_str,
412 int ad_update_calls)
Mateusz Starzykfc606222021-06-16 11:04:07 +0200413{
414 mbedtls_gcm_context ctx;
415
Valerio Setti10e9aa22023-12-12 11:54:20 +0100416 BLOCK_CIPHER_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100417 mbedtls_gcm_init(&ctx);
Mateusz Starzykfc606222021-06-16 11:04:07 +0200418
Gilles Peskine449bd832023-01-11 14:50:10 +0100419 TEST_ASSERT(mbedtls_gcm_setkey(&ctx, cipher_id, key_str->x, key_str->len * 8) == 0);
420 check_cipher_with_empty_ad(&ctx, MBEDTLS_GCM_ENCRYPT,
421 iv_str, src_str, dst, tag_str,
422 ad_update_calls);
Mateusz Starzykfc606222021-06-16 11:04:07 +0200423
424exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100425 mbedtls_gcm_free(&ctx);
Valerio Setti10e9aa22023-12-12 11:54:20 +0100426 BLOCK_CIPHER_PSA_DONE();
Mateusz Starzykfc606222021-06-16 11:04:07 +0200427}
428/* END_CASE */
429
Mateusz Starzyk469c9f32021-06-18 00:06:52 +0200430/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100431void gcm_encrypt_and_verify_no_ad_no_cipher(int cipher_id,
432 data_t *key_str,
433 data_t *iv_str,
434 data_t *tag_str)
Mateusz Starzyk469c9f32021-06-18 00:06:52 +0200435{
436 mbedtls_gcm_context ctx;
437
Valerio Setti10e9aa22023-12-12 11:54:20 +0100438 BLOCK_CIPHER_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100439 mbedtls_gcm_init(&ctx);
Mateusz Starzyk469c9f32021-06-18 00:06:52 +0200440
Gilles Peskine449bd832023-01-11 14:50:10 +0100441 TEST_ASSERT(mbedtls_gcm_setkey(&ctx, cipher_id, key_str->x, key_str->len * 8) == 0);
442 check_no_cipher_no_ad(&ctx, MBEDTLS_GCM_ENCRYPT,
443 iv_str, tag_str);
Mateusz Starzyk469c9f32021-06-18 00:06:52 +0200444
Gilles Peskine449bd832023-01-11 14:50:10 +0100445 mbedtls_gcm_free(&ctx);
Valerio Setti10e9aa22023-12-12 11:54:20 +0100446 BLOCK_CIPHER_PSA_DONE();
Mateusz Starzyk469c9f32021-06-18 00:06:52 +0200447}
448/* END_CASE */
449
Tuvshinzaya Erdenekhuu104eb7f2022-07-29 14:48:21 +0100450/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100451void gcm_invalid_param()
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500452{
453 mbedtls_gcm_context ctx;
454 unsigned char valid_buffer[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 };
455 mbedtls_cipher_id_t valid_cipher = MBEDTLS_CIPHER_ID_AES;
Ronald Cron875b5fb2021-05-21 08:50:00 +0200456 int invalid_bitlen = 1;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500457
Gilles Peskine449bd832023-01-11 14:50:10 +0100458 mbedtls_gcm_init(&ctx);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500459
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500460 /* mbedtls_gcm_setkey */
Ronald Cron875b5fb2021-05-21 08:50:00 +0200461 TEST_EQUAL(
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500462 MBEDTLS_ERR_GCM_BAD_INPUT,
Gilles Peskine449bd832023-01-11 14:50:10 +0100463 mbedtls_gcm_setkey(&ctx, valid_cipher, valid_buffer, invalid_bitlen));
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500464
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500465exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100466 mbedtls_gcm_free(&ctx);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500467}
468/* END_CASE */
469
Mateusz Starzykc48f43b2021-10-04 13:46:38 +0200470/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100471void gcm_update_output_buffer_too_small(int cipher_id, int mode,
472 data_t *key_str, const data_t *input,
473 const data_t *iv)
Mateusz Starzykc48f43b2021-10-04 13:46:38 +0200474{
475 mbedtls_gcm_context ctx;
476 uint8_t *output = NULL;
Mateusz Starzyk33d01ff2021-10-21 14:55:59 +0200477 size_t olen = 0;
Mateusz Starzykc48f43b2021-10-04 13:46:38 +0200478 size_t output_len = input->len - 1;
479
Valerio Setti10e9aa22023-12-12 11:54:20 +0100480 BLOCK_CIPHER_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100481 mbedtls_gcm_init(&ctx);
482 TEST_EQUAL(mbedtls_gcm_setkey(&ctx, cipher_id, key_str->x, key_str->len * 8), 0);
483 TEST_EQUAL(0, mbedtls_gcm_starts(&ctx, mode, iv->x, iv->len));
Mateusz Starzykc48f43b2021-10-04 13:46:38 +0200484
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100485 TEST_CALLOC(output, output_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100486 TEST_EQUAL(MBEDTLS_ERR_GCM_BUFFER_TOO_SMALL,
487 mbedtls_gcm_update(&ctx, input->x, input->len, output, output_len, &olen));
Mateusz Starzykc48f43b2021-10-04 13:46:38 +0200488
489exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100490 mbedtls_free(output);
491 mbedtls_gcm_free(&ctx);
Valerio Setti10e9aa22023-12-12 11:54:20 +0100492 BLOCK_CIPHER_PSA_DONE();
Mateusz Starzykc48f43b2021-10-04 13:46:38 +0200493}
494/* END_CASE */
495
Chien Wong99ff1f52024-01-24 20:44:01 +0800496/* BEGIN_CASE */
Chien Wong92c17c42024-01-25 19:11:03 +0800497/* NISP SP 800-38D, Section 5.2.1.1 requires that bit length of IV should
498 * satisfy 1 <= bit_len(IV) <= 2^64 - 1. */
Chien Wong99ff1f52024-01-24 20:44:01 +0800499void gcm_invalid_iv_len(void)
500{
501 mbedtls_gcm_context ctx;
Dave Rodgman12285c52024-02-02 17:52:41 +0000502 mbedtls_gcm_init(&ctx);
Chien Wong99ff1f52024-01-24 20:44:01 +0800503 uint8_t b16[16] = { 0 };
504
Dave Rodgman12285c52024-02-02 17:52:41 +0000505 BLOCK_CIPHER_PSA_INIT();
506
Chien Wong92c17c42024-01-25 19:11:03 +0800507 // Invalid IV length 0
Chien Wong99ff1f52024-01-24 20:44:01 +0800508 gcm_reset_ctx(&ctx, b16, sizeof(b16) * 8, b16, 0, MBEDTLS_ERR_GCM_BAD_INPUT);
509 mbedtls_gcm_free(&ctx);
510
Chien Wong92c17c42024-01-25 19:11:03 +0800511 // Only testable on platforms where sizeof(size_t) >= 8.
Chien Wong99ff1f52024-01-24 20:44:01 +0800512#if SIZE_MAX >= UINT64_MAX
Chien Wong92c17c42024-01-25 19:11:03 +0800513 // Invalid IV length 2^61
Chien Wong99ff1f52024-01-24 20:44:01 +0800514 gcm_reset_ctx(&ctx, b16, sizeof(b16) * 8, b16, 1ULL << 61, MBEDTLS_ERR_GCM_BAD_INPUT);
515 mbedtls_gcm_free(&ctx);
516#endif
517
518 goto exit; /* To suppress error that exit is defined but not used */
519exit:
Dave Rodgman12285c52024-02-02 17:52:41 +0000520 mbedtls_gcm_free(&ctx);
521 BLOCK_CIPHER_PSA_DONE();
Chien Wong99ff1f52024-01-24 20:44:01 +0800522}
523/* END_CASE */
524
525/* BEGIN_CASE */
Chien Wong99ff1f52024-01-24 20:44:01 +0800526void gcm_add_len_too_long(void)
527{
Chien Wong92c17c42024-01-25 19:11:03 +0800528 // Only testable on platforms where sizeof(size_t) >= 8.
Chien Wong99ff1f52024-01-24 20:44:01 +0800529#if SIZE_MAX >= UINT64_MAX
530 mbedtls_gcm_context ctx;
Dave Rodgman12285c52024-02-02 17:52:41 +0000531 mbedtls_gcm_init(&ctx);
Chien Wong99ff1f52024-01-24 20:44:01 +0800532 uint8_t b16[16] = { 0 };
Dave Rodgman12285c52024-02-02 17:52:41 +0000533 BLOCK_CIPHER_PSA_INIT();
534
Chien Wong92c17c42024-01-25 19:11:03 +0800535 /* NISP SP 800-38D, Section 5.2.1.1 requires that bit length of AD should
536 * be <= 2^64 - 1, ie < 2^64. This is the minimum invalid length in bytes. */
537 uint64_t len_max = 1ULL << 61;
Chien Wong99ff1f52024-01-24 20:44:01 +0800538
539 gcm_reset_ctx(&ctx, b16, sizeof(b16) * 8, b16, sizeof(b16), 0);
Chien Wong92c17c42024-01-25 19:11:03 +0800540 // Feed AD that just exceeds the length limit
541 TEST_EQUAL(mbedtls_gcm_update_ad(&ctx, b16, len_max),
Chien Wong99ff1f52024-01-24 20:44:01 +0800542 MBEDTLS_ERR_GCM_BAD_INPUT);
543 mbedtls_gcm_free(&ctx);
544
545 gcm_reset_ctx(&ctx, b16, sizeof(b16) * 8, b16, sizeof(b16), 0);
Chien Wong92c17c42024-01-25 19:11:03 +0800546 // Feed AD that just exceeds the length limit in two calls
Chien Wong99ff1f52024-01-24 20:44:01 +0800547 TEST_EQUAL(mbedtls_gcm_update_ad(&ctx, b16, 1), 0);
Chien Wong92c17c42024-01-25 19:11:03 +0800548 TEST_EQUAL(mbedtls_gcm_update_ad(&ctx, b16, len_max - 1),
Chien Wong99ff1f52024-01-24 20:44:01 +0800549 MBEDTLS_ERR_GCM_BAD_INPUT);
550 mbedtls_gcm_free(&ctx);
551
552 gcm_reset_ctx(&ctx, b16, sizeof(b16) * 8, b16, sizeof(b16), 0);
Chien Wong92c17c42024-01-25 19:11:03 +0800553 // Test if potential total AD length overflow is handled properly
Chien Wong99ff1f52024-01-24 20:44:01 +0800554 TEST_EQUAL(mbedtls_gcm_update_ad(&ctx, b16, 1), 0);
555 TEST_EQUAL(mbedtls_gcm_update_ad(&ctx, b16, UINT64_MAX), MBEDTLS_ERR_GCM_BAD_INPUT);
556
557exit:
558 mbedtls_gcm_free(&ctx);
Dave Rodgman12285c52024-02-02 17:52:41 +0000559 BLOCK_CIPHER_PSA_DONE();
Chien Wong99ff1f52024-01-24 20:44:01 +0800560#endif
561}
562/* END_CASE */
563
564/* BEGIN_CASE */
Chien Wong99ff1f52024-01-24 20:44:01 +0800565void gcm_input_len_too_long(void)
566{
Chien Wong92c17c42024-01-25 19:11:03 +0800567 // Only testable on platforms where sizeof(size_t) >= 8
Chien Wong99ff1f52024-01-24 20:44:01 +0800568#if SIZE_MAX >= UINT64_MAX
569 mbedtls_gcm_context ctx;
570 uint8_t b16[16] = { 0 };
Chien Wong92c17c42024-01-25 19:11:03 +0800571 uint8_t out[1];
Chien Wong99ff1f52024-01-24 20:44:01 +0800572 size_t out_len;
Dave Rodgman12285c52024-02-02 17:52:41 +0000573 mbedtls_gcm_init(&ctx);
574 BLOCK_CIPHER_PSA_INIT();
575
Chien Wong92c17c42024-01-25 19:11:03 +0800576 /* NISP SP 800-38D, Section 5.2.1.1 requires that bit length of input should
577 * be <= 2^39 - 256. This is the maximum valid length in bytes. */
Chien Wong99ff1f52024-01-24 20:44:01 +0800578 uint64_t len_max = (1ULL << 36) - 32;
579
580 gcm_reset_ctx(&ctx, b16, sizeof(b16) * 8, b16, sizeof(b16), 0);
Chien Wong92c17c42024-01-25 19:11:03 +0800581 // Feed input that just exceeds the length limit
582 TEST_EQUAL(mbedtls_gcm_update(&ctx, b16, len_max + 1, out, len_max + 1,
Chien Wong99ff1f52024-01-24 20:44:01 +0800583 &out_len),
584 MBEDTLS_ERR_GCM_BAD_INPUT);
585 mbedtls_gcm_free(&ctx);
586
587 gcm_reset_ctx(&ctx, b16, sizeof(b16) * 8, b16, sizeof(b16), 0);
Chien Wong92c17c42024-01-25 19:11:03 +0800588 // Feed input that just exceeds the length limit in two calls
589 TEST_EQUAL(mbedtls_gcm_update(&ctx, b16, 1, out, 1, &out_len), 0);
Chien Wongef567952024-01-25 19:22:50 +0800590 TEST_EQUAL(mbedtls_gcm_update(&ctx, b16, len_max, out, len_max, &out_len),
Chien Wong99ff1f52024-01-24 20:44:01 +0800591 MBEDTLS_ERR_GCM_BAD_INPUT);
592 mbedtls_gcm_free(&ctx);
593
594 gcm_reset_ctx(&ctx, b16, sizeof(b16) * 8, b16, sizeof(b16), 0);
Chien Wong92c17c42024-01-25 19:11:03 +0800595 // Test if potential total input length overflow is handled properly
596 TEST_EQUAL(mbedtls_gcm_update(&ctx, b16, 1, out, 1, &out_len), 0);
597 TEST_EQUAL(mbedtls_gcm_update(&ctx, b16, UINT64_MAX, out, UINT64_MAX,
Chien Wong99ff1f52024-01-24 20:44:01 +0800598 &out_len),
599 MBEDTLS_ERR_GCM_BAD_INPUT);
600
601exit:
602 mbedtls_gcm_free(&ctx);
Dave Rodgman12285c52024-02-02 17:52:41 +0000603 BLOCK_CIPHER_PSA_DONE();
Chien Wong99ff1f52024-01-24 20:44:01 +0800604#endif
605}
606/* END_CASE */
607
Harry Ramsey187fcce2024-11-07 09:26:43 +0000608/* BEGIN_CASE */
609void gcm_encrypt_input_output_buffer_overlap(int cipher_id, data_t *key_str,
610 data_t *src_str, data_t *iv_str,
611 data_t *add_str, data_t *dst,
612 int tag_len_bits, data_t *tag,
613 int init_result)
614{
615 unsigned char *buffer = NULL;
616 size_t buffer_len;
617 unsigned char tag_output[16];
618 mbedtls_gcm_context ctx;
619 size_t tag_len = tag_len_bits / 8;
620 size_t n1;
621 size_t n1_add;
622
623 BLOCK_CIPHER_PSA_INIT();
624 mbedtls_gcm_init(&ctx);
625
626 /* GCM includes padding and therefore input length can be shorter than the output length
627 * Therefore we must ensure we round up to the nearest 128-bits/16-bytes.
628 */
629 buffer_len = src_str->len;
Harry Ramseyd77207e2024-11-13 09:42:59 +0000630 if (buffer_len % 16 != 0 || buffer_len == 0) {
Harry Ramsey187fcce2024-11-07 09:26:43 +0000631 buffer_len += (16 - (buffer_len % 16));
632 }
633 TEST_CALLOC(buffer, buffer_len);
Harry Ramsey187fcce2024-11-07 09:26:43 +0000634 memcpy(buffer, src_str->x, src_str->len);
635
636 memset(tag_output, 0x00, 16);
637
638 TEST_ASSERT(mbedtls_gcm_setkey(&ctx, cipher_id, key_str->x, key_str->len * 8) == init_result);
639 if (init_result == 0) {
640 TEST_ASSERT(mbedtls_gcm_crypt_and_tag(&ctx, MBEDTLS_GCM_ENCRYPT, src_str->len, iv_str->x,
641 iv_str->len, add_str->x, add_str->len, buffer,
642 buffer, tag_len, tag_output) == 0);
643
644 TEST_MEMORY_COMPARE(buffer, src_str->len, dst->x, dst->len);
645 TEST_MEMORY_COMPARE(tag_output, tag_len, tag->x, tag->len);
646
647 for (n1 = 0; n1 <= src_str->len; n1 += 1) {
648 for (n1_add = 0; n1_add <= add_str->len; n1_add += 1) {
649 mbedtls_test_set_step(n1 * 10000 + n1_add);
650 if (!check_multipart(&ctx, MBEDTLS_GCM_ENCRYPT,
651 iv_str, add_str, src_str,
652 dst, tag,
653 n1, n1_add)) {
654 goto exit;
655 }
656 }
657 }
658 }
659
660exit:
Harry Ramseye320b892024-11-11 15:02:26 +0000661 mbedtls_free(buffer);
Harry Ramsey187fcce2024-11-07 09:26:43 +0000662 mbedtls_gcm_free(&ctx);
663 BLOCK_CIPHER_PSA_DONE();
664}
665/* END_CASE */
666
667/* BEGIN_CASE */
668void gcm_decrypt_input_output_buffer_overlap(int cipher_id, data_t *key_str,
669 data_t *src_str, data_t *iv_str,
670 data_t *add_str, int tag_len_bits,
671 data_t *tag_str, char *result,
672 data_t *pt_result, int init_result)
673{
674 unsigned char *buffer = NULL;
675 size_t buffer_len;
676 mbedtls_gcm_context ctx;
677 int ret;
678 size_t tag_len = tag_len_bits / 8;
679 size_t n1;
680 size_t n1_add;
681
682 BLOCK_CIPHER_PSA_INIT();
683 mbedtls_gcm_init(&ctx);
684
685 /* GCM includes padding and therefore input length can be shorter than the output length
686 * Therefore we must ensure we round up to the nearest 128-bits/16-bytes.
687 */
688 buffer_len = src_str->len;
Harry Ramseyd77207e2024-11-13 09:42:59 +0000689 if (buffer_len % 16 != 0 || buffer_len == 0) {
Harry Ramsey187fcce2024-11-07 09:26:43 +0000690 buffer_len += (16 - (buffer_len % 16));
691 }
692 TEST_CALLOC(buffer, buffer_len);
Harry Ramsey187fcce2024-11-07 09:26:43 +0000693 memcpy(buffer, src_str->x, src_str->len);
694
695 TEST_ASSERT(mbedtls_gcm_setkey(&ctx, cipher_id, key_str->x, key_str->len * 8) == init_result);
696 if (init_result == 0) {
697 ret = mbedtls_gcm_auth_decrypt(&ctx,
698 src_str->len,
699 iv_str->x,
700 iv_str->len,
701 add_str->x,
702 add_str->len,
703 tag_str->x,
704 tag_len,
705 buffer,
706 buffer);
707
708 if (strcmp("FAIL", result) == 0) {
709 TEST_ASSERT(ret == MBEDTLS_ERR_GCM_AUTH_FAILED);
710 } else {
711 TEST_ASSERT(ret == 0);
712 TEST_MEMORY_COMPARE(buffer, src_str->len, pt_result->x, pt_result->len);
713
714 for (n1 = 0; n1 <= src_str->len; n1 += 1) {
715 for (n1_add = 0; n1_add <= add_str->len; n1_add += 1) {
716 mbedtls_test_set_step(n1 * 10000 + n1_add);
717 if (!check_multipart(&ctx, MBEDTLS_GCM_DECRYPT,
718 iv_str, add_str, src_str,
719 pt_result, tag_str,
720 n1, n1_add)) {
721 goto exit;
722 }
723 }
724 }
725 }
726 }
727
728exit:
Harry Ramseye320b892024-11-11 15:02:26 +0000729 mbedtls_free(buffer);
Harry Ramsey187fcce2024-11-07 09:26:43 +0000730 mbedtls_gcm_free(&ctx);
731 BLOCK_CIPHER_PSA_DONE();
732
733}
734/* END_CASE */
735
Valerio Setti689c0f72023-12-20 09:53:39 +0100736/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST:MBEDTLS_CCM_GCM_CAN_AES */
Gilles Peskine449bd832023-01-11 14:50:10 +0100737void gcm_selftest()
Paul Bakker89e80c92012-03-20 13:50:09 +0000738{
Valerio Setti10e9aa22023-12-12 11:54:20 +0100739 BLOCK_CIPHER_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100740 TEST_ASSERT(mbedtls_gcm_self_test(1) == 0);
Valerio Setti10e9aa22023-12-12 11:54:20 +0100741 BLOCK_CIPHER_PSA_DONE();
Paul Bakker89e80c92012-03-20 13:50:09 +0000742}
Paul Bakker33b43f12013-08-20 11:48:36 +0200743/* END_CASE */