blob: 097e42408cbb664c937a3cdb0a9d8f476d452271 [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
Valerio Setti10e9aa22023-12-12 11:54:20 +0100174 BLOCK_CIPHER_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100175 mbedtls_gcm_init(&ctx);
Ron Eldor5a21fd62016-12-16 16:15:56 +0200176
Gilles Peskine449bd832023-01-11 14:50:10 +0100177 memset(output, 0x00, sizeof(output));
178 memset(tag_output, 0x00, sizeof(tag_output));
Darryl Green11999bb2018-03-13 15:22:58 +0000179
Gilles Peskine449bd832023-01-11 14:50:10 +0100180 TEST_ASSERT(mbedtls_gcm_setkey(&ctx, cipher_id, key_str->x, key_str->len * 8) == 0);
181 TEST_ASSERT(mbedtls_gcm_crypt_and_tag(&ctx, direction, src_str->len, iv_str->x, iv_str->len,
182 add_str->x, add_str->len, src_str->x, output, tag_len,
183 tag_output) == gcm_result);
Ron Eldor5a21fd62016-12-16 16:15:56 +0200184
185exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100186 mbedtls_gcm_free(&ctx);
Valerio Setti10e9aa22023-12-12 11:54:20 +0100187 BLOCK_CIPHER_PSA_DONE();
Ron Eldor5a21fd62016-12-16 16:15:56 +0200188}
189/* END_CASE */
190
191/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100192void gcm_encrypt_and_tag(int cipher_id, data_t *key_str,
193 data_t *src_str, data_t *iv_str,
194 data_t *add_str, data_t *dst,
195 int tag_len_bits, data_t *tag,
196 int init_result)
Paul Bakker89e80c92012-03-20 13:50:09 +0000197{
Paul Bakker89e80c92012-03-20 13:50:09 +0000198 unsigned char output[128];
199 unsigned char tag_output[16];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200 mbedtls_gcm_context ctx;
Azim Khanf1aaec92017-05-30 14:23:15 +0100201 size_t tag_len = tag_len_bits / 8;
Gilles Peskine36dd93e2021-04-13 13:02:03 +0200202 size_t n1;
Mateusz Starzykaf4ecdd2021-06-15 15:29:48 +0200203 size_t n1_add;
Paul Bakker89e80c92012-03-20 13:50:09 +0000204
Valerio Setti10e9aa22023-12-12 11:54:20 +0100205 BLOCK_CIPHER_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100206 mbedtls_gcm_init(&ctx);
Manuel Pégourié-Gonnardc34e8dd2015-04-28 21:42:17 +0200207
Paul Bakker89e80c92012-03-20 13:50:09 +0000208 memset(output, 0x00, 128);
209 memset(tag_output, 0x00, 16);
210
Paul Bakker89e80c92012-03-20 13:50:09 +0000211
Gilles Peskine449bd832023-01-11 14:50:10 +0100212 TEST_ASSERT(mbedtls_gcm_setkey(&ctx, cipher_id, key_str->x, key_str->len * 8) == init_result);
213 if (init_result == 0) {
214 TEST_ASSERT(mbedtls_gcm_crypt_and_tag(&ctx, MBEDTLS_GCM_ENCRYPT, src_str->len, iv_str->x,
215 iv_str->len, add_str->x, add_str->len, src_str->x,
216 output, tag_len, tag_output) == 0);
Paul Bakker89e80c92012-03-20 13:50:09 +0000217
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +0100218 TEST_MEMORY_COMPARE(output, src_str->len, dst->x, dst->len);
219 TEST_MEMORY_COMPARE(tag_output, tag_len, tag->x, tag->len);
Gilles Peskine36dd93e2021-04-13 13:02:03 +0200220
Gilles Peskine449bd832023-01-11 14:50:10 +0100221 for (n1 = 0; n1 <= src_str->len; n1 += 1) {
222 for (n1_add = 0; n1_add <= add_str->len; n1_add += 1) {
223 mbedtls_test_set_step(n1 * 10000 + n1_add);
224 if (!check_multipart(&ctx, MBEDTLS_GCM_ENCRYPT,
225 iv_str, add_str, src_str,
226 dst, tag,
227 n1, n1_add)) {
Mateusz Starzykaf4ecdd2021-06-15 15:29:48 +0200228 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100229 }
Mateusz Starzykaf4ecdd2021-06-15 15:29:48 +0200230 }
Gilles Peskine36dd93e2021-04-13 13:02:03 +0200231 }
Paul Bakker89e80c92012-03-20 13:50:09 +0000232 }
Manuel Pégourié-Gonnard4fe92002013-09-13 13:45:58 +0200233
Paul Bakkerbd51b262014-07-10 15:26:12 +0200234exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100235 mbedtls_gcm_free(&ctx);
Valerio Setti10e9aa22023-12-12 11:54:20 +0100236 BLOCK_CIPHER_PSA_DONE();
Paul Bakker89e80c92012-03-20 13:50:09 +0000237}
Paul Bakker33b43f12013-08-20 11:48:36 +0200238/* END_CASE */
Paul Bakker89e80c92012-03-20 13:50:09 +0000239
Paul Bakker33b43f12013-08-20 11:48:36 +0200240/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100241void gcm_decrypt_and_verify(int cipher_id, data_t *key_str,
242 data_t *src_str, data_t *iv_str,
243 data_t *add_str, int tag_len_bits,
244 data_t *tag_str, char *result,
245 data_t *pt_result, int init_result)
Paul Bakker89e80c92012-03-20 13:50:09 +0000246{
Paul Bakker89e80c92012-03-20 13:50:09 +0000247 unsigned char output[128];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200248 mbedtls_gcm_context ctx;
Paul Bakker89e80c92012-03-20 13:50:09 +0000249 int ret;
Azim Khanf1aaec92017-05-30 14:23:15 +0100250 size_t tag_len = tag_len_bits / 8;
Gilles Peskine36dd93e2021-04-13 13:02:03 +0200251 size_t n1;
Mateusz Starzykaf4ecdd2021-06-15 15:29:48 +0200252 size_t n1_add;
Paul Bakker89e80c92012-03-20 13:50:09 +0000253
Valerio Setti10e9aa22023-12-12 11:54:20 +0100254 BLOCK_CIPHER_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 mbedtls_gcm_init(&ctx);
Manuel Pégourié-Gonnardc34e8dd2015-04-28 21:42:17 +0200256
Paul Bakker89e80c92012-03-20 13:50:09 +0000257 memset(output, 0x00, 128);
258
Paul Bakker89e80c92012-03-20 13:50:09 +0000259
Gilles Peskine449bd832023-01-11 14:50:10 +0100260 TEST_ASSERT(mbedtls_gcm_setkey(&ctx, cipher_id, key_str->x, key_str->len * 8) == init_result);
261 if (init_result == 0) {
262 ret = mbedtls_gcm_auth_decrypt(&ctx,
263 src_str->len,
264 iv_str->x,
265 iv_str->len,
266 add_str->x,
267 add_str->len,
268 tag_str->x,
269 tag_len,
270 src_str->x,
271 output);
Paul Bakker89e80c92012-03-20 13:50:09 +0000272
Gilles Peskine449bd832023-01-11 14:50:10 +0100273 if (strcmp("FAIL", result) == 0) {
274 TEST_ASSERT(ret == MBEDTLS_ERR_GCM_AUTH_FAILED);
275 } else {
276 TEST_ASSERT(ret == 0);
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +0100277 TEST_MEMORY_COMPARE(output, src_str->len, pt_result->x, pt_result->len);
Paul Bakker89e80c92012-03-20 13:50:09 +0000278
Gilles Peskine449bd832023-01-11 14:50:10 +0100279 for (n1 = 0; n1 <= src_str->len; n1 += 1) {
280 for (n1_add = 0; n1_add <= add_str->len; n1_add += 1) {
281 mbedtls_test_set_step(n1 * 10000 + n1_add);
282 if (!check_multipart(&ctx, MBEDTLS_GCM_DECRYPT,
283 iv_str, add_str, src_str,
284 pt_result, tag_str,
285 n1, n1_add)) {
Mateusz Starzykaf4ecdd2021-06-15 15:29:48 +0200286 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100287 }
Mateusz Starzykaf4ecdd2021-06-15 15:29:48 +0200288 }
Gilles Peskine36dd93e2021-04-13 13:02:03 +0200289 }
Paul Bakker89e80c92012-03-20 13:50:09 +0000290 }
291 }
Manuel Pégourié-Gonnard4fe92002013-09-13 13:45:58 +0200292
Paul Bakkerbd51b262014-07-10 15:26:12 +0200293exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100294 mbedtls_gcm_free(&ctx);
Valerio Setti10e9aa22023-12-12 11:54:20 +0100295 BLOCK_CIPHER_PSA_DONE();
Paul Bakker89e80c92012-03-20 13:50:09 +0000296}
Paul Bakker33b43f12013-08-20 11:48:36 +0200297/* END_CASE */
Paul Bakker89e80c92012-03-20 13:50:09 +0000298
Mateusz Starzykfc606222021-06-16 11:04:07 +0200299/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100300void gcm_decrypt_and_verify_empty_cipher(int cipher_id,
301 data_t *key_str,
302 data_t *iv_str,
303 data_t *add_str,
304 data_t *tag_str,
305 int cipher_update_calls)
Mateusz Starzykfc606222021-06-16 11:04:07 +0200306{
307 mbedtls_gcm_context ctx;
308
Valerio Setti10e9aa22023-12-12 11:54:20 +0100309 BLOCK_CIPHER_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100310 mbedtls_gcm_init(&ctx);
Mateusz Starzykfc606222021-06-16 11:04:07 +0200311
Gilles Peskine449bd832023-01-11 14:50:10 +0100312 TEST_ASSERT(mbedtls_gcm_setkey(&ctx, cipher_id, key_str->x, key_str->len * 8) == 0);
313 check_empty_cipher_with_ad(&ctx, MBEDTLS_GCM_DECRYPT,
314 iv_str, add_str, tag_str,
315 cipher_update_calls);
Mateusz Starzykfc606222021-06-16 11:04:07 +0200316
Gilles Peskine449bd832023-01-11 14:50:10 +0100317 mbedtls_gcm_free(&ctx);
Valerio Setti10e9aa22023-12-12 11:54:20 +0100318 BLOCK_CIPHER_PSA_DONE();
Mateusz Starzykfc606222021-06-16 11:04:07 +0200319}
320/* END_CASE */
321
322/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100323void gcm_decrypt_and_verify_empty_ad(int cipher_id,
324 data_t *key_str,
325 data_t *iv_str,
326 data_t *src_str,
327 data_t *tag_str,
328 data_t *pt_result,
329 int ad_update_calls)
Mateusz Starzykfc606222021-06-16 11:04:07 +0200330{
331 mbedtls_gcm_context ctx;
332
Valerio Setti10e9aa22023-12-12 11:54:20 +0100333 BLOCK_CIPHER_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100334 mbedtls_gcm_init(&ctx);
Mateusz Starzykfc606222021-06-16 11:04:07 +0200335
Gilles Peskine449bd832023-01-11 14:50:10 +0100336 TEST_ASSERT(mbedtls_gcm_setkey(&ctx, cipher_id, key_str->x, key_str->len * 8) == 0);
337 check_cipher_with_empty_ad(&ctx, MBEDTLS_GCM_DECRYPT,
338 iv_str, src_str, pt_result, tag_str,
339 ad_update_calls);
Mateusz Starzykfc606222021-06-16 11:04:07 +0200340
Gilles Peskine449bd832023-01-11 14:50:10 +0100341 mbedtls_gcm_free(&ctx);
Valerio Setti10e9aa22023-12-12 11:54:20 +0100342 BLOCK_CIPHER_PSA_DONE();
Mateusz Starzykfc606222021-06-16 11:04:07 +0200343}
344/* END_CASE */
345
346/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100347void gcm_decrypt_and_verify_no_ad_no_cipher(int cipher_id,
348 data_t *key_str,
349 data_t *iv_str,
350 data_t *tag_str)
Mateusz Starzyk469c9f32021-06-18 00:06:52 +0200351{
352 mbedtls_gcm_context ctx;
353
Valerio Setti10e9aa22023-12-12 11:54:20 +0100354 BLOCK_CIPHER_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100355 mbedtls_gcm_init(&ctx);
Mateusz Starzyk469c9f32021-06-18 00:06:52 +0200356
Gilles Peskine449bd832023-01-11 14:50:10 +0100357 TEST_ASSERT(mbedtls_gcm_setkey(&ctx, cipher_id, key_str->x, key_str->len * 8) == 0);
358 check_no_cipher_no_ad(&ctx, MBEDTLS_GCM_DECRYPT,
359 iv_str, tag_str);
Mateusz Starzyk469c9f32021-06-18 00:06:52 +0200360
Gilles Peskine449bd832023-01-11 14:50:10 +0100361 mbedtls_gcm_free(&ctx);
Valerio Setti10e9aa22023-12-12 11:54:20 +0100362 BLOCK_CIPHER_PSA_DONE();
Mateusz Starzyk469c9f32021-06-18 00:06:52 +0200363}
364/* END_CASE */
365
366/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100367void gcm_encrypt_and_tag_empty_cipher(int cipher_id,
368 data_t *key_str,
369 data_t *iv_str,
370 data_t *add_str,
371 data_t *tag_str,
372 int cipher_update_calls)
Mateusz Starzykfc606222021-06-16 11:04:07 +0200373{
374 mbedtls_gcm_context ctx;
375
Valerio Setti10e9aa22023-12-12 11:54:20 +0100376 BLOCK_CIPHER_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100377 mbedtls_gcm_init(&ctx);
Mateusz Starzykfc606222021-06-16 11:04:07 +0200378
Gilles Peskine449bd832023-01-11 14:50:10 +0100379 TEST_ASSERT(mbedtls_gcm_setkey(&ctx, cipher_id, key_str->x, key_str->len * 8) == 0);
380 check_empty_cipher_with_ad(&ctx, MBEDTLS_GCM_ENCRYPT,
381 iv_str, add_str, tag_str,
382 cipher_update_calls);
Mateusz Starzykfc606222021-06-16 11:04:07 +0200383
384exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100385 mbedtls_gcm_free(&ctx);
Valerio Setti10e9aa22023-12-12 11:54:20 +0100386 BLOCK_CIPHER_PSA_DONE();
Mateusz Starzykfc606222021-06-16 11:04:07 +0200387}
388/* END_CASE */
389
390/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100391void gcm_encrypt_and_tag_empty_ad(int cipher_id,
392 data_t *key_str,
393 data_t *iv_str,
394 data_t *src_str,
395 data_t *dst,
396 data_t *tag_str,
397 int ad_update_calls)
Mateusz Starzykfc606222021-06-16 11:04:07 +0200398{
399 mbedtls_gcm_context ctx;
400
Valerio Setti10e9aa22023-12-12 11:54:20 +0100401 BLOCK_CIPHER_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100402 mbedtls_gcm_init(&ctx);
Mateusz Starzykfc606222021-06-16 11:04:07 +0200403
Gilles Peskine449bd832023-01-11 14:50:10 +0100404 TEST_ASSERT(mbedtls_gcm_setkey(&ctx, cipher_id, key_str->x, key_str->len * 8) == 0);
405 check_cipher_with_empty_ad(&ctx, MBEDTLS_GCM_ENCRYPT,
406 iv_str, src_str, dst, tag_str,
407 ad_update_calls);
Mateusz Starzykfc606222021-06-16 11:04:07 +0200408
409exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100410 mbedtls_gcm_free(&ctx);
Valerio Setti10e9aa22023-12-12 11:54:20 +0100411 BLOCK_CIPHER_PSA_DONE();
Mateusz Starzykfc606222021-06-16 11:04:07 +0200412}
413/* END_CASE */
414
Mateusz Starzyk469c9f32021-06-18 00:06:52 +0200415/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100416void gcm_encrypt_and_verify_no_ad_no_cipher(int cipher_id,
417 data_t *key_str,
418 data_t *iv_str,
419 data_t *tag_str)
Mateusz Starzyk469c9f32021-06-18 00:06:52 +0200420{
421 mbedtls_gcm_context ctx;
422
Valerio Setti10e9aa22023-12-12 11:54:20 +0100423 BLOCK_CIPHER_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100424 mbedtls_gcm_init(&ctx);
Mateusz Starzyk469c9f32021-06-18 00:06:52 +0200425
Gilles Peskine449bd832023-01-11 14:50:10 +0100426 TEST_ASSERT(mbedtls_gcm_setkey(&ctx, cipher_id, key_str->x, key_str->len * 8) == 0);
427 check_no_cipher_no_ad(&ctx, MBEDTLS_GCM_ENCRYPT,
428 iv_str, tag_str);
Mateusz Starzyk469c9f32021-06-18 00:06:52 +0200429
Gilles Peskine449bd832023-01-11 14:50:10 +0100430 mbedtls_gcm_free(&ctx);
Valerio Setti10e9aa22023-12-12 11:54:20 +0100431 BLOCK_CIPHER_PSA_DONE();
Mateusz Starzyk469c9f32021-06-18 00:06:52 +0200432}
433/* END_CASE */
434
Tuvshinzaya Erdenekhuu104eb7f2022-07-29 14:48:21 +0100435/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100436void gcm_invalid_param()
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500437{
438 mbedtls_gcm_context ctx;
439 unsigned char valid_buffer[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 };
440 mbedtls_cipher_id_t valid_cipher = MBEDTLS_CIPHER_ID_AES;
Ronald Cron875b5fb2021-05-21 08:50:00 +0200441 int invalid_bitlen = 1;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500442
Gilles Peskine449bd832023-01-11 14:50:10 +0100443 mbedtls_gcm_init(&ctx);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500444
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500445 /* mbedtls_gcm_setkey */
Ronald Cron875b5fb2021-05-21 08:50:00 +0200446 TEST_EQUAL(
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500447 MBEDTLS_ERR_GCM_BAD_INPUT,
Gilles Peskine449bd832023-01-11 14:50:10 +0100448 mbedtls_gcm_setkey(&ctx, valid_cipher, valid_buffer, invalid_bitlen));
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500449
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500450exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100451 mbedtls_gcm_free(&ctx);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500452}
453/* END_CASE */
454
Mateusz Starzykc48f43b2021-10-04 13:46:38 +0200455/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100456void gcm_update_output_buffer_too_small(int cipher_id, int mode,
457 data_t *key_str, const data_t *input,
458 const data_t *iv)
Mateusz Starzykc48f43b2021-10-04 13:46:38 +0200459{
460 mbedtls_gcm_context ctx;
461 uint8_t *output = NULL;
Mateusz Starzyk33d01ff2021-10-21 14:55:59 +0200462 size_t olen = 0;
Mateusz Starzykc48f43b2021-10-04 13:46:38 +0200463 size_t output_len = input->len - 1;
464
Valerio Setti10e9aa22023-12-12 11:54:20 +0100465 BLOCK_CIPHER_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100466 mbedtls_gcm_init(&ctx);
467 TEST_EQUAL(mbedtls_gcm_setkey(&ctx, cipher_id, key_str->x, key_str->len * 8), 0);
468 TEST_EQUAL(0, mbedtls_gcm_starts(&ctx, mode, iv->x, iv->len));
Mateusz Starzykc48f43b2021-10-04 13:46:38 +0200469
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100470 TEST_CALLOC(output, output_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100471 TEST_EQUAL(MBEDTLS_ERR_GCM_BUFFER_TOO_SMALL,
472 mbedtls_gcm_update(&ctx, input->x, input->len, output, output_len, &olen));
Mateusz Starzykc48f43b2021-10-04 13:46:38 +0200473
474exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100475 mbedtls_free(output);
476 mbedtls_gcm_free(&ctx);
Valerio Setti10e9aa22023-12-12 11:54:20 +0100477 BLOCK_CIPHER_PSA_DONE();
Mateusz Starzykc48f43b2021-10-04 13:46:38 +0200478}
479/* END_CASE */
480
Andrzej Kurekf502bcb2022-09-27 09:27:56 -0400481/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST:MBEDTLS_AES_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100482void gcm_selftest()
Paul Bakker89e80c92012-03-20 13:50:09 +0000483{
Valerio Setti10e9aa22023-12-12 11:54:20 +0100484 BLOCK_CIPHER_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100485 TEST_ASSERT(mbedtls_gcm_self_test(1) == 0);
Valerio Setti10e9aa22023-12-12 11:54:20 +0100486 BLOCK_CIPHER_PSA_DONE();
Paul Bakker89e80c92012-03-20 13:50:09 +0000487}
Paul Bakker33b43f12013-08-20 11:48:36 +0200488/* END_CASE */