Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 1 | /* |
| 2 | * NIST SP800-38D compliant GCM implementation |
| 3 | * |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 4 | * Copyright The Mbed TLS Contributors |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: Apache-2.0 |
| 6 | * |
| 7 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 8 | * not use this file except in compliance with the License. |
| 9 | * You may obtain a copy of the License at |
| 10 | * |
| 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | * |
| 13 | * Unless required by applicable law or agreed to in writing, software |
| 14 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | * See the License for the specific language governing permissions and |
| 17 | * limitations under the License. |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 18 | */ |
Manuel Pégourié-Gonnard | 9d57482 | 2013-12-25 15:41:25 +0100 | [diff] [blame] | 19 | |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 20 | /* |
Manuel Pégourié-Gonnard | 9d57482 | 2013-12-25 15:41:25 +0100 | [diff] [blame] | 21 | * http://csrc.nist.gov/publications/nistpubs/800-38D/SP-800-38D.pdf |
| 22 | * |
| 23 | * See also: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 24 | * [MGV] |
| 25 | * http://csrc.nist.gov/groups/ST/toolkit/BCM/documents/proposedmodes/gcm/gcm-revised-spec.pdf |
Manuel Pégourié-Gonnard | 9d57482 | 2013-12-25 15:41:25 +0100 | [diff] [blame] | 26 | * |
| 27 | * We use the algorithm described as Shoup's method with 4-bit tables in |
| 28 | * [MGV] 4.1, pp. 12-13, to enhance speed without using too much memory. |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 29 | */ |
Manuel Pégourié-Gonnard | 9d57482 | 2013-12-25 15:41:25 +0100 | [diff] [blame] | 30 | |
Gilles Peskine | db09ef6 | 2020-06-03 01:43:33 +0200 | [diff] [blame] | 31 | #include "common.h" |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 32 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 33 | #if defined(MBEDTLS_GCM_C) |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 34 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 35 | # include "mbedtls/gcm.h" |
| 36 | # include "mbedtls/platform_util.h" |
| 37 | # include "mbedtls/error.h" |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 38 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 39 | # include <string.h> |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 40 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 41 | # if defined(MBEDTLS_AESNI_C) |
| 42 | # include "aesni.h" |
| 43 | # endif |
Manuel Pégourié-Gonnard | 80637c7 | 2013-12-26 16:09:58 +0100 | [diff] [blame] | 44 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 45 | # if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_AES_C) |
| 46 | # include "mbedtls/aes.h" |
| 47 | # include "mbedtls/platform.h" |
| 48 | # if !defined(MBEDTLS_PLATFORM_C) |
| 49 | # include <stdio.h> |
| 50 | # define mbedtls_printf printf |
| 51 | # endif /* MBEDTLS_PLATFORM_C */ |
| 52 | # endif /* MBEDTLS_SELF_TEST && MBEDTLS_AES_C */ |
Paul Bakker | 7dc4c44 | 2014-02-01 22:50:26 +0100 | [diff] [blame] | 53 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 54 | # if !defined(MBEDTLS_GCM_ALT) |
Jaeden Amero | 1526330 | 2017-09-21 12:53:48 +0100 | [diff] [blame] | 55 | |
k-stachowiak | 8ffc92a | 2018-12-12 14:21:59 +0100 | [diff] [blame] | 56 | /* Parameter validation macros */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 57 | # define GCM_VALIDATE_RET(cond) \ |
| 58 | MBEDTLS_INTERNAL_VALIDATE_RET(cond, MBEDTLS_ERR_GCM_BAD_INPUT) |
| 59 | # define GCM_VALIDATE(cond) MBEDTLS_INTERNAL_VALIDATE(cond) |
k-stachowiak | 8ffc92a | 2018-12-12 14:21:59 +0100 | [diff] [blame] | 60 | |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 61 | /* |
| 62 | * 32-bit integer manipulation macros (big endian) |
| 63 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 64 | # ifndef GET_UINT32_BE |
| 65 | # define GET_UINT32_BE(n, b, i) \ |
| 66 | { \ |
| 67 | (n) = ((uint32_t)(b)[(i)] << 24) | \ |
| 68 | ((uint32_t)(b)[(i) + 1] << 16) | \ |
| 69 | ((uint32_t)(b)[(i) + 2] << 8) | \ |
| 70 | ((uint32_t)(b)[(i) + 3]); \ |
| 71 | } |
| 72 | # endif |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 73 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 74 | # ifndef PUT_UINT32_BE |
| 75 | # define PUT_UINT32_BE(n, b, i) \ |
| 76 | { \ |
| 77 | (b)[(i)] = (unsigned char)((n) >> 24); \ |
| 78 | (b)[(i) + 1] = (unsigned char)((n) >> 16); \ |
| 79 | (b)[(i) + 2] = (unsigned char)((n) >> 8); \ |
| 80 | (b)[(i) + 3] = (unsigned char)((n)); \ |
| 81 | } |
| 82 | # endif |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 83 | |
Manuel Pégourié-Gonnard | 9d57482 | 2013-12-25 15:41:25 +0100 | [diff] [blame] | 84 | /* |
Manuel Pégourié-Gonnard | c34e8dd | 2015-04-28 21:42:17 +0200 | [diff] [blame] | 85 | * Initialize a context |
| 86 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 87 | void mbedtls_gcm_init(mbedtls_gcm_context *ctx) |
Manuel Pégourié-Gonnard | c34e8dd | 2015-04-28 21:42:17 +0200 | [diff] [blame] | 88 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 89 | GCM_VALIDATE(ctx != NULL); |
| 90 | memset(ctx, 0, sizeof(mbedtls_gcm_context)); |
Manuel Pégourié-Gonnard | c34e8dd | 2015-04-28 21:42:17 +0200 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | /* |
Manuel Pégourié-Gonnard | 9d57482 | 2013-12-25 15:41:25 +0100 | [diff] [blame] | 94 | * Precompute small multiples of H, that is set |
| 95 | * HH[i] || HL[i] = H times i, |
| 96 | * where i is seen as a field element as in [MGV], ie high-order bits |
| 97 | * correspond to low powers of P. The result is stored in the same way, that |
| 98 | * is the high-order bit of HH corresponds to P^0 and the low-order bit of HL |
| 99 | * corresponds to P^127. |
| 100 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 101 | static int gcm_gen_table(mbedtls_gcm_context *ctx) |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 102 | { |
Paul Bakker | 43aff2a | 2013-09-09 00:10:27 +0200 | [diff] [blame] | 103 | int ret, i, j; |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 104 | uint64_t hi, lo; |
| 105 | uint64_t vl, vh; |
| 106 | unsigned char h[16]; |
Paul Bakker | 43aff2a | 2013-09-09 00:10:27 +0200 | [diff] [blame] | 107 | size_t olen = 0; |
Paul Bakker | 169b7f4 | 2013-06-25 14:58:00 +0200 | [diff] [blame] | 108 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 109 | memset(h, 0, 16); |
| 110 | if ((ret = mbedtls_cipher_update(&ctx->cipher_ctx, h, 16, h, &olen)) != 0) |
| 111 | return ret; |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 112 | |
Manuel Pégourié-Gonnard | 80637c7 | 2013-12-26 16:09:58 +0100 | [diff] [blame] | 113 | /* pack h as two 64-bits ints, big-endian */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 114 | GET_UINT32_BE(hi, h, 0); |
| 115 | GET_UINT32_BE(lo, h, 4); |
| 116 | vh = (uint64_t)hi << 32 | lo; |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 117 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 118 | GET_UINT32_BE(hi, h, 8); |
| 119 | GET_UINT32_BE(lo, h, 12); |
| 120 | vl = (uint64_t)hi << 32 | lo; |
Paul Bakker | 169b7f4 | 2013-06-25 14:58:00 +0200 | [diff] [blame] | 121 | |
Manuel Pégourié-Gonnard | 9d57482 | 2013-12-25 15:41:25 +0100 | [diff] [blame] | 122 | /* 8 = 1000 corresponds to 1 in GF(2^128) */ |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 123 | ctx->HL[8] = vl; |
| 124 | ctx->HH[8] = vh; |
| 125 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 126 | # if defined(MBEDTLS_AESNI_C) && defined(MBEDTLS_HAVE_X86_64) |
Manuel Pégourié-Gonnard | 80637c7 | 2013-12-26 16:09:58 +0100 | [diff] [blame] | 127 | /* With CLMUL support, we need only h, not the rest of the table */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 128 | if (mbedtls_aesni_has_support(MBEDTLS_AESNI_CLMUL)) |
| 129 | return 0; |
| 130 | # endif |
Manuel Pégourié-Gonnard | 80637c7 | 2013-12-26 16:09:58 +0100 | [diff] [blame] | 131 | |
| 132 | /* 0 corresponds to 0 in GF(2^128) */ |
| 133 | ctx->HH[0] = 0; |
| 134 | ctx->HL[0] = 0; |
| 135 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 136 | for (i = 4; i > 0; i >>= 1) { |
| 137 | uint32_t T = (vl & 1) * 0xe1000000U; |
| 138 | vl = (vh << 63) | (vl >> 1); |
| 139 | vh = (vh >> 1) ^ ((uint64_t)T << 32); |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 140 | |
| 141 | ctx->HL[i] = vl; |
| 142 | ctx->HH[i] = vh; |
| 143 | } |
| 144 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 145 | for (i = 2; i <= 8; i *= 2) { |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 146 | uint64_t *HiL = ctx->HL + i, *HiH = ctx->HH + i; |
| 147 | vh = *HiH; |
| 148 | vl = *HiL; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 149 | for (j = 1; j < i; j++) { |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 150 | HiH[j] = vh ^ ctx->HH[j]; |
| 151 | HiL[j] = vl ^ ctx->HL[j]; |
| 152 | } |
| 153 | } |
Paul Bakker | 43aff2a | 2013-09-09 00:10:27 +0200 | [diff] [blame] | 154 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 155 | return 0; |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 156 | } |
| 157 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 158 | int mbedtls_gcm_setkey(mbedtls_gcm_context *ctx, |
| 159 | mbedtls_cipher_id_t cipher, |
| 160 | const unsigned char *key, |
| 161 | unsigned int keybits) |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 162 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 163 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 164 | const mbedtls_cipher_info_t *cipher_info; |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 165 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 166 | GCM_VALIDATE_RET(ctx != NULL); |
| 167 | GCM_VALIDATE_RET(key != NULL); |
| 168 | GCM_VALIDATE_RET(keybits == 128 || keybits == 192 || keybits == 256); |
k-stachowiak | 8ffc92a | 2018-12-12 14:21:59 +0100 | [diff] [blame] | 169 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 170 | cipher_info = |
| 171 | mbedtls_cipher_info_from_values(cipher, keybits, MBEDTLS_MODE_ECB); |
| 172 | if (cipher_info == NULL) |
| 173 | return MBEDTLS_ERR_GCM_BAD_INPUT; |
Paul Bakker | 43aff2a | 2013-09-09 00:10:27 +0200 | [diff] [blame] | 174 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 175 | if (cipher_info->block_size != 16) |
| 176 | return MBEDTLS_ERR_GCM_BAD_INPUT; |
Paul Bakker | a0558e0 | 2013-09-10 14:25:51 +0200 | [diff] [blame] | 177 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 178 | mbedtls_cipher_free(&ctx->cipher_ctx); |
Manuel Pégourié-Gonnard | 43b0857 | 2015-05-27 17:23:30 +0200 | [diff] [blame] | 179 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 180 | if ((ret = mbedtls_cipher_setup(&ctx->cipher_ctx, cipher_info)) != 0) |
| 181 | return ret; |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 182 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 183 | if ((ret = mbedtls_cipher_setkey(&ctx->cipher_ctx, key, keybits, |
| 184 | MBEDTLS_ENCRYPT)) != 0) { |
| 185 | return ret; |
Paul Bakker | 43aff2a | 2013-09-09 00:10:27 +0200 | [diff] [blame] | 186 | } |
| 187 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 188 | if ((ret = gcm_gen_table(ctx)) != 0) |
| 189 | return ret; |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 190 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 191 | return 0; |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 192 | } |
| 193 | |
Manuel Pégourié-Gonnard | 9d57482 | 2013-12-25 15:41:25 +0100 | [diff] [blame] | 194 | /* |
| 195 | * Shoup's method for multiplication use this table with |
| 196 | * last4[x] = x times P^128 |
| 197 | * where x and last4[x] are seen as elements of GF(2^128) as in [MGV] |
| 198 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 199 | static const uint64_t last4[16] = { 0x0000, 0x1c20, 0x3840, 0x2460, |
| 200 | 0x7080, 0x6ca0, 0x48c0, 0x54e0, |
| 201 | 0xe100, 0xfd20, 0xd940, 0xc560, |
| 202 | 0x9180, 0x8da0, 0xa9c0, 0xb5e0 }; |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 203 | |
Manuel Pégourié-Gonnard | 9d57482 | 2013-12-25 15:41:25 +0100 | [diff] [blame] | 204 | /* |
| 205 | * Sets output to x times H using the precomputed tables. |
| 206 | * x and output are seen as elements of GF(2^128) as in [MGV]. |
| 207 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 208 | static void gcm_mult(mbedtls_gcm_context *ctx, |
| 209 | const unsigned char x[16], |
| 210 | unsigned char output[16]) |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 211 | { |
| 212 | int i = 0; |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 213 | unsigned char lo, hi, rem; |
| 214 | uint64_t zh, zl; |
| 215 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 216 | # if defined(MBEDTLS_AESNI_C) && defined(MBEDTLS_HAVE_X86_64) |
| 217 | if (mbedtls_aesni_has_support(MBEDTLS_AESNI_CLMUL)) { |
Manuel Pégourié-Gonnard | 80637c7 | 2013-12-26 16:09:58 +0100 | [diff] [blame] | 218 | unsigned char h[16]; |
| 219 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 220 | PUT_UINT32_BE(ctx->HH[8] >> 32, h, 0); |
| 221 | PUT_UINT32_BE(ctx->HH[8], h, 4); |
| 222 | PUT_UINT32_BE(ctx->HL[8] >> 32, h, 8); |
| 223 | PUT_UINT32_BE(ctx->HL[8], h, 12); |
Manuel Pégourié-Gonnard | 80637c7 | 2013-12-26 16:09:58 +0100 | [diff] [blame] | 224 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 225 | mbedtls_aesni_gcm_mult(output, x, h); |
Manuel Pégourié-Gonnard | 80637c7 | 2013-12-26 16:09:58 +0100 | [diff] [blame] | 226 | return; |
| 227 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 228 | # endif /* MBEDTLS_AESNI_C && MBEDTLS_HAVE_X86_64 */ |
Manuel Pégourié-Gonnard | 80637c7 | 2013-12-26 16:09:58 +0100 | [diff] [blame] | 229 | |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 230 | lo = x[15] & 0xf; |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 231 | |
| 232 | zh = ctx->HH[lo]; |
| 233 | zl = ctx->HL[lo]; |
| 234 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 235 | for (i = 15; i >= 0; i--) { |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 236 | lo = x[i] & 0xf; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 237 | hi = (x[i] >> 4) & 0xf; |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 238 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 239 | if (i != 15) { |
| 240 | rem = (unsigned char)zl & 0xf; |
| 241 | zl = (zh << 60) | (zl >> 4); |
| 242 | zh = (zh >> 4); |
| 243 | zh ^= (uint64_t)last4[rem] << 48; |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 244 | zh ^= ctx->HH[lo]; |
| 245 | zl ^= ctx->HL[lo]; |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 246 | } |
| 247 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 248 | rem = (unsigned char)zl & 0xf; |
| 249 | zl = (zh << 60) | (zl >> 4); |
| 250 | zh = (zh >> 4); |
| 251 | zh ^= (uint64_t)last4[rem] << 48; |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 252 | zh ^= ctx->HH[hi]; |
| 253 | zl ^= ctx->HL[hi]; |
| 254 | } |
| 255 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 256 | PUT_UINT32_BE(zh >> 32, output, 0); |
| 257 | PUT_UINT32_BE(zh, output, 4); |
| 258 | PUT_UINT32_BE(zl >> 32, output, 8); |
| 259 | PUT_UINT32_BE(zl, output, 12); |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 260 | } |
| 261 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 262 | int mbedtls_gcm_starts(mbedtls_gcm_context *ctx, |
| 263 | int mode, |
| 264 | const unsigned char *iv, |
| 265 | size_t iv_len) |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 266 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 267 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 268 | unsigned char work_buf[16]; |
| 269 | size_t i; |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 270 | const unsigned char *p; |
Paul Bakker | 43aff2a | 2013-09-09 00:10:27 +0200 | [diff] [blame] | 271 | size_t use_len, olen = 0; |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 272 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 273 | GCM_VALIDATE_RET(ctx != NULL); |
| 274 | GCM_VALIDATE_RET(iv != NULL); |
k-stachowiak | 8ffc92a | 2018-12-12 14:21:59 +0100 | [diff] [blame] | 275 | |
Mateusz Starzyk | 939a54c | 2021-06-22 11:12:28 +0200 | [diff] [blame] | 276 | /* IV is limited to 2^64 bits, so 2^61 bytes */ |
Ron Eldor | 5a21fd6 | 2016-12-16 16:15:56 +0200 | [diff] [blame] | 277 | /* IV is not allowed to be zero length */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 278 | if (iv_len == 0 || (uint64_t)iv_len >> 61 != 0) |
| 279 | return MBEDTLS_ERR_GCM_BAD_INPUT; |
Manuel Pégourié-Gonnard | b46e6ad | 2014-06-18 11:29:30 +0200 | [diff] [blame] | 280 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 281 | memset(ctx->y, 0x00, sizeof(ctx->y)); |
| 282 | memset(ctx->buf, 0x00, sizeof(ctx->buf)); |
Paul Bakker | 52cf16c | 2013-07-26 13:55:38 +0200 | [diff] [blame] | 283 | |
Paul Bakker | b9d3cfa | 2013-06-26 15:07:16 +0200 | [diff] [blame] | 284 | ctx->mode = mode; |
Paul Bakker | 52cf16c | 2013-07-26 13:55:38 +0200 | [diff] [blame] | 285 | ctx->len = 0; |
| 286 | ctx->add_len = 0; |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 287 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 288 | if (iv_len == 12) { |
| 289 | memcpy(ctx->y, iv, iv_len); |
Paul Bakker | b9d3cfa | 2013-06-26 15:07:16 +0200 | [diff] [blame] | 290 | ctx->y[15] = 1; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 291 | } else { |
| 292 | memset(work_buf, 0x00, 16); |
| 293 | PUT_UINT32_BE(iv_len * 8, work_buf, 12); |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 294 | |
| 295 | p = iv; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 296 | while (iv_len > 0) { |
| 297 | use_len = (iv_len < 16) ? iv_len : 16; |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 298 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 299 | for (i = 0; i < use_len; i++) |
Paul Bakker | b9d3cfa | 2013-06-26 15:07:16 +0200 | [diff] [blame] | 300 | ctx->y[i] ^= p[i]; |
Paul Bakker | 169b7f4 | 2013-06-25 14:58:00 +0200 | [diff] [blame] | 301 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 302 | gcm_mult(ctx, ctx->y, ctx->y); |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 303 | |
| 304 | iv_len -= use_len; |
| 305 | p += use_len; |
| 306 | } |
| 307 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 308 | for (i = 0; i < 16; i++) |
Paul Bakker | b9d3cfa | 2013-06-26 15:07:16 +0200 | [diff] [blame] | 309 | ctx->y[i] ^= work_buf[i]; |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 310 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 311 | gcm_mult(ctx, ctx->y, ctx->y); |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 312 | } |
| 313 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 314 | if ((ret = mbedtls_cipher_update(&ctx->cipher_ctx, ctx->y, 16, |
| 315 | ctx->base_ectr, &olen)) != 0) { |
| 316 | return ret; |
Paul Bakker | 43aff2a | 2013-09-09 00:10:27 +0200 | [diff] [blame] | 317 | } |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 318 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 319 | return 0; |
Gilles Peskine | 295fc13 | 2021-04-15 18:32:23 +0200 | [diff] [blame] | 320 | } |
| 321 | |
Mateusz Starzyk | b45b57e | 2021-06-07 15:44:18 +0200 | [diff] [blame] | 322 | /** |
Mateusz Starzyk | 3d0bbee | 2021-06-15 14:26:53 +0200 | [diff] [blame] | 323 | * mbedtls_gcm_context::buf contains the partial state of the computation of |
| 324 | * the authentication tag. |
Mateusz Starzyk | 939a54c | 2021-06-22 11:12:28 +0200 | [diff] [blame] | 325 | * mbedtls_gcm_context::add_len and mbedtls_gcm_context::len indicate |
| 326 | * different stages of the computation: |
Mateusz Starzyk | 3d0bbee | 2021-06-15 14:26:53 +0200 | [diff] [blame] | 327 | * * len == 0 && add_len == 0: initial state |
| 328 | * * len == 0 && add_len % 16 != 0: the first `add_len % 16` bytes have |
| 329 | * a partial block of AD that has been |
| 330 | * xored in but not yet multiplied in. |
| 331 | * * len == 0 && add_len % 16 == 0: the authentication tag is correct if |
| 332 | * the data ends now. |
| 333 | * * len % 16 != 0: the first `len % 16` bytes have |
| 334 | * a partial block of ciphertext that has |
| 335 | * been xored in but not yet multiplied in. |
| 336 | * * len > 0 && len % 16 == 0: the authentication tag is correct if |
| 337 | * the data ends now. |
Mateusz Starzyk | b45b57e | 2021-06-07 15:44:18 +0200 | [diff] [blame] | 338 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 339 | int mbedtls_gcm_update_ad(mbedtls_gcm_context *ctx, |
| 340 | const unsigned char *add, |
| 341 | size_t add_len) |
Gilles Peskine | 295fc13 | 2021-04-15 18:32:23 +0200 | [diff] [blame] | 342 | { |
| 343 | const unsigned char *p; |
Mateusz Starzyk | bd513bb | 2021-05-26 14:25:39 +0200 | [diff] [blame] | 344 | size_t use_len, i, offset; |
Gilles Peskine | 295fc13 | 2021-04-15 18:32:23 +0200 | [diff] [blame] | 345 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 346 | GCM_VALIDATE_RET(add_len == 0 || add != NULL); |
Gilles Peskine | 295fc13 | 2021-04-15 18:32:23 +0200 | [diff] [blame] | 347 | |
Mateusz Starzyk | 939a54c | 2021-06-22 11:12:28 +0200 | [diff] [blame] | 348 | /* IV is limited to 2^64 bits, so 2^61 bytes */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 349 | if ((uint64_t)add_len >> 61 != 0) |
| 350 | return MBEDTLS_ERR_GCM_BAD_INPUT; |
Gilles Peskine | 295fc13 | 2021-04-15 18:32:23 +0200 | [diff] [blame] | 351 | |
Mateusz Starzyk | bd513bb | 2021-05-26 14:25:39 +0200 | [diff] [blame] | 352 | offset = ctx->add_len % 16; |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 353 | p = add; |
Mateusz Starzyk | bd513bb | 2021-05-26 14:25:39 +0200 | [diff] [blame] | 354 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 355 | if (offset != 0) { |
Mateusz Starzyk | bd513bb | 2021-05-26 14:25:39 +0200 | [diff] [blame] | 356 | use_len = 16 - offset; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 357 | if (use_len > add_len) |
Mateusz Starzyk | bd513bb | 2021-05-26 14:25:39 +0200 | [diff] [blame] | 358 | use_len = add_len; |
| 359 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 360 | for (i = 0; i < use_len; i++) |
| 361 | ctx->buf[i + offset] ^= p[i]; |
Mateusz Starzyk | bd513bb | 2021-05-26 14:25:39 +0200 | [diff] [blame] | 362 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 363 | if (offset + use_len == 16) |
| 364 | gcm_mult(ctx, ctx->buf, ctx->buf); |
Mateusz Starzyk | bd513bb | 2021-05-26 14:25:39 +0200 | [diff] [blame] | 365 | |
| 366 | ctx->add_len += use_len; |
| 367 | add_len -= use_len; |
| 368 | p += use_len; |
| 369 | } |
| 370 | |
| 371 | ctx->add_len += add_len; |
| 372 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 373 | while (add_len >= 16) { |
| 374 | for (i = 0; i < 16; i++) |
Paul Bakker | b9d3cfa | 2013-06-26 15:07:16 +0200 | [diff] [blame] | 375 | ctx->buf[i] ^= p[i]; |
Paul Bakker | 169b7f4 | 2013-06-25 14:58:00 +0200 | [diff] [blame] | 376 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 377 | gcm_mult(ctx, ctx->buf, ctx->buf); |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 378 | |
Mateusz Starzyk | 25a571e | 2021-06-15 13:22:42 +0200 | [diff] [blame] | 379 | add_len -= 16; |
| 380 | p += 16; |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 381 | } |
| 382 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 383 | if (add_len > 0) { |
| 384 | for (i = 0; i < add_len; i++) |
Mateusz Starzyk | bd513bb | 2021-05-26 14:25:39 +0200 | [diff] [blame] | 385 | ctx->buf[i] ^= p[i]; |
| 386 | } |
| 387 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 388 | return 0; |
Paul Bakker | b9d3cfa | 2013-06-26 15:07:16 +0200 | [diff] [blame] | 389 | } |
| 390 | |
Gilles Peskine | 58fc272 | 2021-04-13 15:58:27 +0200 | [diff] [blame] | 391 | /* Increment the counter. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 392 | static void gcm_incr(unsigned char y[16]) |
Gilles Peskine | 58fc272 | 2021-04-13 15:58:27 +0200 | [diff] [blame] | 393 | { |
| 394 | size_t i; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 395 | for (i = 16; i > 12; i--) |
| 396 | if (++y[i - 1] != 0) |
Gilles Peskine | 58fc272 | 2021-04-13 15:58:27 +0200 | [diff] [blame] | 397 | break; |
| 398 | } |
| 399 | |
| 400 | /* Calculate and apply the encryption mask. Process use_len bytes of data, |
| 401 | * starting at position offset in the mask block. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 402 | static int gcm_mask(mbedtls_gcm_context *ctx, |
| 403 | unsigned char ectr[16], |
| 404 | size_t offset, |
| 405 | size_t use_len, |
| 406 | const unsigned char *input, |
| 407 | unsigned char *output) |
Gilles Peskine | 58fc272 | 2021-04-13 15:58:27 +0200 | [diff] [blame] | 408 | { |
| 409 | size_t i; |
| 410 | size_t olen = 0; |
| 411 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 412 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 413 | if ((ret = mbedtls_cipher_update(&ctx->cipher_ctx, ctx->y, 16, ectr, |
| 414 | &olen)) != 0) { |
| 415 | mbedtls_platform_zeroize(ectr, 16); |
| 416 | return ret; |
Gilles Peskine | 58fc272 | 2021-04-13 15:58:27 +0200 | [diff] [blame] | 417 | } |
| 418 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 419 | for (i = 0; i < use_len; i++) { |
| 420 | if (ctx->mode == MBEDTLS_GCM_DECRYPT) |
Gilles Peskine | 58fc272 | 2021-04-13 15:58:27 +0200 | [diff] [blame] | 421 | ctx->buf[offset + i] ^= input[i]; |
| 422 | output[i] = ectr[offset + i] ^ input[i]; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 423 | if (ctx->mode == MBEDTLS_GCM_ENCRYPT) |
Gilles Peskine | 58fc272 | 2021-04-13 15:58:27 +0200 | [diff] [blame] | 424 | ctx->buf[offset + i] ^= output[i]; |
| 425 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 426 | return 0; |
Gilles Peskine | 58fc272 | 2021-04-13 15:58:27 +0200 | [diff] [blame] | 427 | } |
| 428 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 429 | int mbedtls_gcm_update(mbedtls_gcm_context *ctx, |
| 430 | const unsigned char *input, |
| 431 | size_t input_length, |
| 432 | unsigned char *output, |
| 433 | size_t output_size, |
| 434 | size_t *output_length) |
Paul Bakker | b9d3cfa | 2013-06-26 15:07:16 +0200 | [diff] [blame] | 435 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 436 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 58fc272 | 2021-04-13 15:58:27 +0200 | [diff] [blame] | 437 | const unsigned char *p = input; |
Paul Bakker | b9d3cfa | 2013-06-26 15:07:16 +0200 | [diff] [blame] | 438 | unsigned char *out_p = output; |
Gilles Peskine | 58fc272 | 2021-04-13 15:58:27 +0200 | [diff] [blame] | 439 | size_t offset; |
| 440 | unsigned char ectr[16]; |
| 441 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 442 | if (output_size < input_length) |
| 443 | return MBEDTLS_ERR_GCM_BAD_INPUT; |
| 444 | GCM_VALIDATE_RET(output_length != NULL); |
Gilles Peskine | a56c448 | 2021-04-15 17:22:35 +0200 | [diff] [blame] | 445 | *output_length = input_length; |
| 446 | |
| 447 | /* Exit early if input_length==0 so that we don't do any pointer arithmetic |
Mateusz Starzyk | 3443bd2 | 2021-06-07 16:03:27 +0200 | [diff] [blame] | 448 | * on a potentially null pointer. |
| 449 | * Returning early also means that the last partial block of AD remains |
| 450 | * untouched for mbedtls_gcm_finish */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 451 | if (input_length == 0) |
| 452 | return 0; |
Paul Bakker | b9d3cfa | 2013-06-26 15:07:16 +0200 | [diff] [blame] | 453 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 454 | GCM_VALIDATE_RET(ctx != NULL); |
| 455 | GCM_VALIDATE_RET(input != NULL); |
| 456 | GCM_VALIDATE_RET(output != NULL); |
k-stachowiak | 8ffc92a | 2018-12-12 14:21:59 +0100 | [diff] [blame] | 457 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 458 | if (output > input && (size_t)(output - input) < input_length) |
| 459 | return MBEDTLS_ERR_GCM_BAD_INPUT; |
Paul Bakker | b9d3cfa | 2013-06-26 15:07:16 +0200 | [diff] [blame] | 460 | |
Manuel Pégourié-Gonnard | b46e6ad | 2014-06-18 11:29:30 +0200 | [diff] [blame] | 461 | /* Total length is restricted to 2^39 - 256 bits, ie 2^36 - 2^5 bytes |
| 462 | * Also check for possible overflow */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 463 | if (ctx->len + input_length < ctx->len || |
| 464 | (uint64_t)ctx->len + input_length > 0xFFFFFFFE0ull) { |
| 465 | return MBEDTLS_ERR_GCM_BAD_INPUT; |
Manuel Pégourié-Gonnard | b46e6ad | 2014-06-18 11:29:30 +0200 | [diff] [blame] | 466 | } |
| 467 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 468 | if (ctx->len == 0 && ctx->add_len % 16 != 0) { |
| 469 | gcm_mult(ctx, ctx->buf, ctx->buf); |
Mateusz Starzyk | bd513bb | 2021-05-26 14:25:39 +0200 | [diff] [blame] | 470 | } |
| 471 | |
Gilles Peskine | 58fc272 | 2021-04-13 15:58:27 +0200 | [diff] [blame] | 472 | offset = ctx->len % 16; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 473 | if (offset != 0) { |
Gilles Peskine | 58fc272 | 2021-04-13 15:58:27 +0200 | [diff] [blame] | 474 | size_t use_len = 16 - offset; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 475 | if (use_len > input_length) |
Gilles Peskine | a56c448 | 2021-04-15 17:22:35 +0200 | [diff] [blame] | 476 | use_len = input_length; |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 477 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 478 | if ((ret = gcm_mask(ctx, ectr, offset, use_len, p, out_p)) != 0) |
| 479 | return ret; |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 480 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 481 | if (offset + use_len == 16) |
| 482 | gcm_mult(ctx, ctx->buf, ctx->buf); |
Paul Bakker | 169b7f4 | 2013-06-25 14:58:00 +0200 | [diff] [blame] | 483 | |
Gilles Peskine | 58fc272 | 2021-04-13 15:58:27 +0200 | [diff] [blame] | 484 | ctx->len += use_len; |
Gilles Peskine | a56c448 | 2021-04-15 17:22:35 +0200 | [diff] [blame] | 485 | input_length -= use_len; |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 486 | p += use_len; |
| 487 | out_p += use_len; |
| 488 | } |
| 489 | |
Gilles Peskine | a56c448 | 2021-04-15 17:22:35 +0200 | [diff] [blame] | 490 | ctx->len += input_length; |
Gilles Peskine | 58fc272 | 2021-04-13 15:58:27 +0200 | [diff] [blame] | 491 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 492 | while (input_length >= 16) { |
| 493 | gcm_incr(ctx->y); |
| 494 | if ((ret = gcm_mask(ctx, ectr, 0, 16, p, out_p)) != 0) |
| 495 | return ret; |
Gilles Peskine | 58fc272 | 2021-04-13 15:58:27 +0200 | [diff] [blame] | 496 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 497 | gcm_mult(ctx, ctx->buf, ctx->buf); |
Gilles Peskine | 58fc272 | 2021-04-13 15:58:27 +0200 | [diff] [blame] | 498 | |
Gilles Peskine | a56c448 | 2021-04-15 17:22:35 +0200 | [diff] [blame] | 499 | input_length -= 16; |
Gilles Peskine | 58fc272 | 2021-04-13 15:58:27 +0200 | [diff] [blame] | 500 | p += 16; |
| 501 | out_p += 16; |
| 502 | } |
| 503 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 504 | if (input_length > 0) { |
| 505 | gcm_incr(ctx->y); |
| 506 | if ((ret = gcm_mask(ctx, ectr, 0, input_length, p, out_p)) != 0) |
| 507 | return ret; |
Gilles Peskine | 58fc272 | 2021-04-13 15:58:27 +0200 | [diff] [blame] | 508 | } |
| 509 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 510 | mbedtls_platform_zeroize(ectr, sizeof(ectr)); |
| 511 | return 0; |
Paul Bakker | b9d3cfa | 2013-06-26 15:07:16 +0200 | [diff] [blame] | 512 | } |
| 513 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 514 | int mbedtls_gcm_finish(mbedtls_gcm_context *ctx, |
| 515 | unsigned char *output, |
| 516 | size_t output_size, |
| 517 | size_t *output_length, |
| 518 | unsigned char *tag, |
| 519 | size_t tag_len) |
Paul Bakker | b9d3cfa | 2013-06-26 15:07:16 +0200 | [diff] [blame] | 520 | { |
| 521 | unsigned char work_buf[16]; |
| 522 | size_t i; |
k-stachowiak | 8ffc92a | 2018-12-12 14:21:59 +0100 | [diff] [blame] | 523 | uint64_t orig_len; |
| 524 | uint64_t orig_add_len; |
| 525 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 526 | GCM_VALIDATE_RET(ctx != NULL); |
| 527 | GCM_VALIDATE_RET(tag != NULL); |
k-stachowiak | 8ffc92a | 2018-12-12 14:21:59 +0100 | [diff] [blame] | 528 | |
Gilles Peskine | 9461e45 | 2021-04-15 16:48:32 +0200 | [diff] [blame] | 529 | /* We never pass any output in finish(). The output parameter exists only |
| 530 | * for the sake of alternative implementations. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 531 | (void)output; |
| 532 | (void)output_size; |
Gilles Peskine | 5a7be10 | 2021-06-23 21:51:32 +0200 | [diff] [blame] | 533 | *output_length = 0; |
Gilles Peskine | 9461e45 | 2021-04-15 16:48:32 +0200 | [diff] [blame] | 534 | |
k-stachowiak | 8ffc92a | 2018-12-12 14:21:59 +0100 | [diff] [blame] | 535 | orig_len = ctx->len * 8; |
| 536 | orig_add_len = ctx->add_len * 8; |
Paul Bakker | b9d3cfa | 2013-06-26 15:07:16 +0200 | [diff] [blame] | 537 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 538 | if (ctx->len == 0 && ctx->add_len % 16 != 0) { |
| 539 | gcm_mult(ctx, ctx->buf, ctx->buf); |
Mateusz Starzyk | bd513bb | 2021-05-26 14:25:39 +0200 | [diff] [blame] | 540 | } |
| 541 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 542 | if (tag_len > 16 || tag_len < 4) |
| 543 | return MBEDTLS_ERR_GCM_BAD_INPUT; |
Paul Bakker | b9d3cfa | 2013-06-26 15:07:16 +0200 | [diff] [blame] | 544 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 545 | if (ctx->len % 16 != 0) |
| 546 | gcm_mult(ctx, ctx->buf, ctx->buf); |
Gilles Peskine | 58fc272 | 2021-04-13 15:58:27 +0200 | [diff] [blame] | 547 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 548 | memcpy(tag, ctx->base_ectr, tag_len); |
Manuel Pégourié-Gonnard | 07f8fa5 | 2013-08-30 18:34:08 +0200 | [diff] [blame] | 549 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 550 | if (orig_len || orig_add_len) { |
| 551 | memset(work_buf, 0x00, 16); |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 552 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 553 | PUT_UINT32_BE((orig_add_len >> 32), work_buf, 0); |
| 554 | PUT_UINT32_BE((orig_add_len), work_buf, 4); |
| 555 | PUT_UINT32_BE((orig_len >> 32), work_buf, 8); |
| 556 | PUT_UINT32_BE((orig_len), work_buf, 12); |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 557 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 558 | for (i = 0; i < 16; i++) |
Paul Bakker | b9d3cfa | 2013-06-26 15:07:16 +0200 | [diff] [blame] | 559 | ctx->buf[i] ^= work_buf[i]; |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 560 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 561 | gcm_mult(ctx, ctx->buf, ctx->buf); |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 562 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 563 | for (i = 0; i < tag_len; i++) |
Paul Bakker | b9d3cfa | 2013-06-26 15:07:16 +0200 | [diff] [blame] | 564 | tag[i] ^= ctx->buf[i]; |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 565 | } |
| 566 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 567 | return 0; |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 568 | } |
| 569 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 570 | int mbedtls_gcm_crypt_and_tag(mbedtls_gcm_context *ctx, |
| 571 | int mode, |
| 572 | size_t length, |
| 573 | const unsigned char *iv, |
| 574 | size_t iv_len, |
| 575 | const unsigned char *add, |
| 576 | size_t add_len, |
| 577 | const unsigned char *input, |
| 578 | unsigned char *output, |
| 579 | size_t tag_len, |
| 580 | unsigned char *tag) |
Paul Bakker | b9d3cfa | 2013-06-26 15:07:16 +0200 | [diff] [blame] | 581 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 582 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | a56c448 | 2021-04-15 17:22:35 +0200 | [diff] [blame] | 583 | size_t olen; |
Paul Bakker | b9d3cfa | 2013-06-26 15:07:16 +0200 | [diff] [blame] | 584 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 585 | GCM_VALIDATE_RET(ctx != NULL); |
| 586 | GCM_VALIDATE_RET(iv != NULL); |
| 587 | GCM_VALIDATE_RET(add_len == 0 || add != NULL); |
| 588 | GCM_VALIDATE_RET(length == 0 || input != NULL); |
| 589 | GCM_VALIDATE_RET(length == 0 || output != NULL); |
| 590 | GCM_VALIDATE_RET(tag != NULL); |
k-stachowiak | 8ffc92a | 2018-12-12 14:21:59 +0100 | [diff] [blame] | 591 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 592 | if ((ret = mbedtls_gcm_starts(ctx, mode, iv, iv_len)) != 0) |
| 593 | return ret; |
Gilles Peskine | 295fc13 | 2021-04-15 18:32:23 +0200 | [diff] [blame] | 594 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 595 | if ((ret = mbedtls_gcm_update_ad(ctx, add, add_len)) != 0) |
| 596 | return ret; |
Paul Bakker | b9d3cfa | 2013-06-26 15:07:16 +0200 | [diff] [blame] | 597 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 598 | if ((ret = mbedtls_gcm_update(ctx, input, length, output, length, &olen)) != |
| 599 | 0) |
| 600 | return ret; |
Paul Bakker | b9d3cfa | 2013-06-26 15:07:16 +0200 | [diff] [blame] | 601 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 602 | if ((ret = mbedtls_gcm_finish(ctx, NULL, 0, &olen, tag, tag_len)) != 0) |
| 603 | return ret; |
Paul Bakker | b9d3cfa | 2013-06-26 15:07:16 +0200 | [diff] [blame] | 604 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 605 | return 0; |
Paul Bakker | b9d3cfa | 2013-06-26 15:07:16 +0200 | [diff] [blame] | 606 | } |
| 607 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 608 | int mbedtls_gcm_auth_decrypt(mbedtls_gcm_context *ctx, |
| 609 | size_t length, |
| 610 | const unsigned char *iv, |
| 611 | size_t iv_len, |
| 612 | const unsigned char *add, |
| 613 | size_t add_len, |
| 614 | const unsigned char *tag, |
| 615 | size_t tag_len, |
| 616 | const unsigned char *input, |
| 617 | unsigned char *output) |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 618 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 619 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 620 | unsigned char check_tag[16]; |
Manuel Pégourié-Gonnard | 20d6a17 | 2013-08-31 16:37:46 +0200 | [diff] [blame] | 621 | size_t i; |
| 622 | int diff; |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 623 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 624 | GCM_VALIDATE_RET(ctx != NULL); |
| 625 | GCM_VALIDATE_RET(iv != NULL); |
| 626 | GCM_VALIDATE_RET(add_len == 0 || add != NULL); |
| 627 | GCM_VALIDATE_RET(tag != NULL); |
| 628 | GCM_VALIDATE_RET(length == 0 || input != NULL); |
| 629 | GCM_VALIDATE_RET(length == 0 || output != NULL); |
k-stachowiak | 8ffc92a | 2018-12-12 14:21:59 +0100 | [diff] [blame] | 630 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 631 | if ((ret = mbedtls_gcm_crypt_and_tag(ctx, MBEDTLS_GCM_DECRYPT, length, iv, |
| 632 | iv_len, add, add_len, input, output, |
| 633 | tag_len, check_tag)) != 0) { |
| 634 | return ret; |
Manuel Pégourié-Gonnard | 073f0fa | 2014-01-18 18:49:32 +0100 | [diff] [blame] | 635 | } |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 636 | |
Manuel Pégourié-Gonnard | 20d6a17 | 2013-08-31 16:37:46 +0200 | [diff] [blame] | 637 | /* Check tag in "constant-time" */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 638 | for (diff = 0, i = 0; i < tag_len; i++) |
Manuel Pégourié-Gonnard | 20d6a17 | 2013-08-31 16:37:46 +0200 | [diff] [blame] | 639 | diff |= tag[i] ^ check_tag[i]; |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 640 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 641 | if (diff != 0) { |
| 642 | mbedtls_platform_zeroize(output, length); |
| 643 | return MBEDTLS_ERR_GCM_AUTH_FAILED; |
Manuel Pégourié-Gonnard | 20d6a17 | 2013-08-31 16:37:46 +0200 | [diff] [blame] | 644 | } |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 645 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 646 | return 0; |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 647 | } |
| 648 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 649 | void mbedtls_gcm_free(mbedtls_gcm_context *ctx) |
Manuel Pégourié-Gonnard | 4fe9200 | 2013-09-13 13:45:58 +0200 | [diff] [blame] | 650 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 651 | if (ctx == NULL) |
k-stachowiak | 8ffc92a | 2018-12-12 14:21:59 +0100 | [diff] [blame] | 652 | return; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 653 | mbedtls_cipher_free(&ctx->cipher_ctx); |
| 654 | mbedtls_platform_zeroize(ctx, sizeof(mbedtls_gcm_context)); |
Manuel Pégourié-Gonnard | 4fe9200 | 2013-09-13 13:45:58 +0200 | [diff] [blame] | 655 | } |
| 656 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 657 | # endif /* !MBEDTLS_GCM_ALT */ |
Jaeden Amero | 1526330 | 2017-09-21 12:53:48 +0100 | [diff] [blame] | 658 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 659 | # if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_AES_C) |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 660 | /* |
Manuel Pégourié-Gonnard | dae7093 | 2013-10-24 15:06:33 +0200 | [diff] [blame] | 661 | * AES-GCM test vectors from: |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 662 | * |
| 663 | * http://csrc.nist.gov/groups/STM/cavp/documents/mac/gcmtestvectors.zip |
| 664 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 665 | # define MAX_TESTS 6 |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 666 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 667 | static const int key_index_test_data[MAX_TESTS] = { 0, 0, 1, 1, 1, 1 }; |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 668 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 669 | static const unsigned char key_test_data[MAX_TESTS][32] = { |
| 670 | { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 671 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 672 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, |
| 673 | { 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c, 0x6d, 0x6a, 0x8f, |
| 674 | 0x94, 0x67, 0x30, 0x83, 0x08, 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, |
| 675 | 0x73, 0x1c, 0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08 }, |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 676 | }; |
| 677 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 678 | static const size_t iv_len_test_data[MAX_TESTS] = { 12, 12, 12, 12, 8, 60 }; |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 679 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 680 | static const int iv_index_test_data[MAX_TESTS] = { 0, 0, 1, 1, 1, 2 }; |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 681 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 682 | static const unsigned char iv_test_data[MAX_TESTS][64] = { |
| 683 | { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, |
| 684 | { 0xca, 0xfe, 0xba, 0xbe, 0xfa, 0xce, 0xdb, 0xad, 0xde, 0xca, 0xf8, 0x88 }, |
| 685 | { 0x93, 0x13, 0x22, 0x5d, 0xf8, 0x84, 0x06, 0xe5, 0x55, 0x90, 0x9c, 0x5a, |
| 686 | 0xff, 0x52, 0x69, 0xaa, 0x6a, 0x7a, 0x95, 0x38, 0x53, 0x4f, 0x7d, 0xa1, |
| 687 | 0xe4, 0xc3, 0x03, 0xd2, 0xa3, 0x18, 0xa7, 0x28, 0xc3, 0xc0, 0xc9, 0x51, |
| 688 | 0x56, 0x80, 0x95, 0x39, 0xfc, 0xf0, 0xe2, 0x42, 0x9a, 0x6b, 0x52, 0x54, |
| 689 | 0x16, 0xae, 0xdb, 0xf5, 0xa0, 0xde, 0x6a, 0x57, 0xa6, 0x37, 0xb3, 0x9b }, |
| 690 | }; |
| 691 | |
| 692 | static const size_t add_len_test_data[MAX_TESTS] = { 0, 0, 0, 20, 20, 20 }; |
| 693 | |
| 694 | static const int add_index_test_data[MAX_TESTS] = { 0, 0, 0, 1, 1, 1 }; |
| 695 | |
| 696 | static const unsigned char additional_test_data[MAX_TESTS][64] = { |
| 697 | { 0x00 }, |
| 698 | { 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef, 0xfe, 0xed, |
| 699 | 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef, 0xab, 0xad, 0xda, 0xd2 }, |
| 700 | }; |
| 701 | |
| 702 | static const size_t pt_len_test_data[MAX_TESTS] = { 0, 16, 64, 60, 60, 60 }; |
| 703 | |
| 704 | static const int pt_index_test_data[MAX_TESTS] = { 0, 0, 1, 1, 1, 1 }; |
| 705 | |
| 706 | static const unsigned char pt_test_data[MAX_TESTS][64] = { |
| 707 | { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 708 | 0x00, 0x00, 0x00, 0x00 }, |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 709 | { 0xd9, 0x31, 0x32, 0x25, 0xf8, 0x84, 0x06, 0xe5, 0xa5, 0x59, 0x09, |
| 710 | 0xc5, 0xaf, 0xf5, 0x26, 0x9a, 0x86, 0xa7, 0xa9, 0x53, 0x15, 0x34, |
| 711 | 0xf7, 0xda, 0x2e, 0x4c, 0x30, 0x3d, 0x8a, 0x31, 0x8a, 0x72, 0x1c, |
| 712 | 0x3c, 0x0c, 0x95, 0x95, 0x68, 0x09, 0x53, 0x2f, 0xcf, 0x0e, 0x24, |
| 713 | 0x49, 0xa6, 0xb5, 0x25, 0xb1, 0x6a, 0xed, 0xf5, 0xaa, 0x0d, 0xe6, |
| 714 | 0x57, 0xba, 0x63, 0x7b, 0x39, 0x1a, 0xaf, 0xd2, 0x55 }, |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 715 | }; |
| 716 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 717 | static const unsigned char ct_test_data[MAX_TESTS * 3][64] = { |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 718 | { 0x00 }, |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 719 | { 0x03, 0x88, 0xda, 0xce, 0x60, 0xb6, 0xa3, 0x92, 0xf3, 0x28, 0xc2, 0xb9, |
| 720 | 0x71, 0xb2, 0xfe, 0x78 }, |
| 721 | { 0x42, 0x83, 0x1e, 0xc2, 0x21, 0x77, 0x74, 0x24, 0x4b, 0x72, 0x21, |
| 722 | 0xb7, 0x84, 0xd0, 0xd4, 0x9c, 0xe3, 0xaa, 0x21, 0x2f, 0x2c, 0x02, |
| 723 | 0xa4, 0xe0, 0x35, 0xc1, 0x7e, 0x23, 0x29, 0xac, 0xa1, 0x2e, 0x21, |
| 724 | 0xd5, 0x14, 0xb2, 0x54, 0x66, 0x93, 0x1c, 0x7d, 0x8f, 0x6a, 0x5a, |
| 725 | 0xac, 0x84, 0xaa, 0x05, 0x1b, 0xa3, 0x0b, 0x39, 0x6a, 0x0a, 0xac, |
| 726 | 0x97, 0x3d, 0x58, 0xe0, 0x91, 0x47, 0x3f, 0x59, 0x85 }, |
| 727 | { 0x42, 0x83, 0x1e, 0xc2, 0x21, 0x77, 0x74, 0x24, 0x4b, 0x72, 0x21, 0xb7, |
| 728 | 0x84, 0xd0, 0xd4, 0x9c, 0xe3, 0xaa, 0x21, 0x2f, 0x2c, 0x02, 0xa4, 0xe0, |
| 729 | 0x35, 0xc1, 0x7e, 0x23, 0x29, 0xac, 0xa1, 0x2e, 0x21, 0xd5, 0x14, 0xb2, |
| 730 | 0x54, 0x66, 0x93, 0x1c, 0x7d, 0x8f, 0x6a, 0x5a, 0xac, 0x84, 0xaa, 0x05, |
| 731 | 0x1b, 0xa3, 0x0b, 0x39, 0x6a, 0x0a, 0xac, 0x97, 0x3d, 0x58, 0xe0, 0x91 }, |
| 732 | { 0x61, 0x35, 0x3b, 0x4c, 0x28, 0x06, 0x93, 0x4a, 0x77, 0x7f, 0xf5, 0x1f, |
| 733 | 0xa2, 0x2a, 0x47, 0x55, 0x69, 0x9b, 0x2a, 0x71, 0x4f, 0xcd, 0xc6, 0xf8, |
| 734 | 0x37, 0x66, 0xe5, 0xf9, 0x7b, 0x6c, 0x74, 0x23, 0x73, 0x80, 0x69, 0x00, |
| 735 | 0xe4, 0x9f, 0x24, 0xb2, 0x2b, 0x09, 0x75, 0x44, 0xd4, 0x89, 0x6b, 0x42, |
| 736 | 0x49, 0x89, 0xb5, 0xe1, 0xeb, 0xac, 0x0f, 0x07, 0xc2, 0x3f, 0x45, 0x98 }, |
| 737 | { 0x8c, 0xe2, 0x49, 0x98, 0x62, 0x56, 0x15, 0xb6, 0x03, 0xa0, 0x33, 0xac, |
| 738 | 0xa1, 0x3f, 0xb8, 0x94, 0xbe, 0x91, 0x12, 0xa5, 0xc3, 0xa2, 0x11, 0xa8, |
| 739 | 0xba, 0x26, 0x2a, 0x3c, 0xca, 0x7e, 0x2c, 0xa7, 0x01, 0xe4, 0xa9, 0xa4, |
| 740 | 0xfb, 0xa4, 0x3c, 0x90, 0xcc, 0xdc, 0xb2, 0x81, 0xd4, 0x8c, 0x7c, 0x6f, |
| 741 | 0xd6, 0x28, 0x75, 0xd2, 0xac, 0xa4, 0x17, 0x03, 0x4c, 0x34, 0xae, 0xe5 }, |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 742 | { 0x00 }, |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 743 | { 0x98, 0xe7, 0x24, 0x7c, 0x07, 0xf0, 0xfe, 0x41, 0x1c, 0x26, 0x7e, 0x43, |
| 744 | 0x84, 0xb0, 0xf6, 0x00 }, |
| 745 | { 0x39, 0x80, 0xca, 0x0b, 0x3c, 0x00, 0xe8, 0x41, 0xeb, 0x06, 0xfa, |
| 746 | 0xc4, 0x87, 0x2a, 0x27, 0x57, 0x85, 0x9e, 0x1c, 0xea, 0xa6, 0xef, |
| 747 | 0xd9, 0x84, 0x62, 0x85, 0x93, 0xb4, 0x0c, 0xa1, 0xe1, 0x9c, 0x7d, |
| 748 | 0x77, 0x3d, 0x00, 0xc1, 0x44, 0xc5, 0x25, 0xac, 0x61, 0x9d, 0x18, |
| 749 | 0xc8, 0x4a, 0x3f, 0x47, 0x18, 0xe2, 0x44, 0x8b, 0x2f, 0xe3, 0x24, |
| 750 | 0xd9, 0xcc, 0xda, 0x27, 0x10, 0xac, 0xad, 0xe2, 0x56 }, |
| 751 | { 0x39, 0x80, 0xca, 0x0b, 0x3c, 0x00, 0xe8, 0x41, 0xeb, 0x06, 0xfa, 0xc4, |
| 752 | 0x87, 0x2a, 0x27, 0x57, 0x85, 0x9e, 0x1c, 0xea, 0xa6, 0xef, 0xd9, 0x84, |
| 753 | 0x62, 0x85, 0x93, 0xb4, 0x0c, 0xa1, 0xe1, 0x9c, 0x7d, 0x77, 0x3d, 0x00, |
| 754 | 0xc1, 0x44, 0xc5, 0x25, 0xac, 0x61, 0x9d, 0x18, 0xc8, 0x4a, 0x3f, 0x47, |
| 755 | 0x18, 0xe2, 0x44, 0x8b, 0x2f, 0xe3, 0x24, 0xd9, 0xcc, 0xda, 0x27, 0x10 }, |
| 756 | { 0x0f, 0x10, 0xf5, 0x99, 0xae, 0x14, 0xa1, 0x54, 0xed, 0x24, 0xb3, 0x6e, |
| 757 | 0x25, 0x32, 0x4d, 0xb8, 0xc5, 0x66, 0x63, 0x2e, 0xf2, 0xbb, 0xb3, 0x4f, |
| 758 | 0x83, 0x47, 0x28, 0x0f, 0xc4, 0x50, 0x70, 0x57, 0xfd, 0xdc, 0x29, 0xdf, |
| 759 | 0x9a, 0x47, 0x1f, 0x75, 0xc6, 0x65, 0x41, 0xd4, 0xd4, 0xda, 0xd1, 0xc9, |
| 760 | 0xe9, 0x3a, 0x19, 0xa5, 0x8e, 0x8b, 0x47, 0x3f, 0xa0, 0xf0, 0x62, 0xf7 }, |
| 761 | { 0xd2, 0x7e, 0x88, 0x68, 0x1c, 0xe3, 0x24, 0x3c, 0x48, 0x30, 0x16, 0x5a, |
| 762 | 0x8f, 0xdc, 0xf9, 0xff, 0x1d, 0xe9, 0xa1, 0xd8, 0xe6, 0xb4, 0x47, 0xef, |
| 763 | 0x6e, 0xf7, 0xb7, 0x98, 0x28, 0x66, 0x6e, 0x45, 0x81, 0xe7, 0x90, 0x12, |
| 764 | 0xaf, 0x34, 0xdd, 0xd9, 0xe2, 0xf0, 0x37, 0x58, 0x9b, 0x29, 0x2d, 0xb3, |
| 765 | 0xe6, 0x7c, 0x03, 0x67, 0x45, 0xfa, 0x22, 0xe7, 0xe9, 0xb7, 0x37, 0x3b }, |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 766 | { 0x00 }, |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 767 | { 0xce, 0xa7, 0x40, 0x3d, 0x4d, 0x60, 0x6b, 0x6e, 0x07, 0x4e, 0xc5, 0xd3, |
| 768 | 0xba, 0xf3, 0x9d, 0x18 }, |
| 769 | { 0x52, 0x2d, 0xc1, 0xf0, 0x99, 0x56, 0x7d, 0x07, 0xf4, 0x7f, 0x37, |
| 770 | 0xa3, 0x2a, 0x84, 0x42, 0x7d, 0x64, 0x3a, 0x8c, 0xdc, 0xbf, 0xe5, |
| 771 | 0xc0, 0xc9, 0x75, 0x98, 0xa2, 0xbd, 0x25, 0x55, 0xd1, 0xaa, 0x8c, |
| 772 | 0xb0, 0x8e, 0x48, 0x59, 0x0d, 0xbb, 0x3d, 0xa7, 0xb0, 0x8b, 0x10, |
| 773 | 0x56, 0x82, 0x88, 0x38, 0xc5, 0xf6, 0x1e, 0x63, 0x93, 0xba, 0x7a, |
| 774 | 0x0a, 0xbc, 0xc9, 0xf6, 0x62, 0x89, 0x80, 0x15, 0xad }, |
| 775 | { 0x52, 0x2d, 0xc1, 0xf0, 0x99, 0x56, 0x7d, 0x07, 0xf4, 0x7f, 0x37, 0xa3, |
| 776 | 0x2a, 0x84, 0x42, 0x7d, 0x64, 0x3a, 0x8c, 0xdc, 0xbf, 0xe5, 0xc0, 0xc9, |
| 777 | 0x75, 0x98, 0xa2, 0xbd, 0x25, 0x55, 0xd1, 0xaa, 0x8c, 0xb0, 0x8e, 0x48, |
| 778 | 0x59, 0x0d, 0xbb, 0x3d, 0xa7, 0xb0, 0x8b, 0x10, 0x56, 0x82, 0x88, 0x38, |
| 779 | 0xc5, 0xf6, 0x1e, 0x63, 0x93, 0xba, 0x7a, 0x0a, 0xbc, 0xc9, 0xf6, 0x62 }, |
| 780 | { 0xc3, 0x76, 0x2d, 0xf1, 0xca, 0x78, 0x7d, 0x32, 0xae, 0x47, 0xc1, 0x3b, |
| 781 | 0xf1, 0x98, 0x44, 0xcb, 0xaf, 0x1a, 0xe1, 0x4d, 0x0b, 0x97, 0x6a, 0xfa, |
| 782 | 0xc5, 0x2f, 0xf7, 0xd7, 0x9b, 0xba, 0x9d, 0xe0, 0xfe, 0xb5, 0x82, 0xd3, |
| 783 | 0x39, 0x34, 0xa4, 0xf0, 0x95, 0x4c, 0xc2, 0x36, 0x3b, 0xc7, 0x3f, 0x78, |
| 784 | 0x62, 0xac, 0x43, 0x0e, 0x64, 0xab, 0xe4, 0x99, 0xf4, 0x7c, 0x9b, 0x1f }, |
| 785 | { 0x5a, 0x8d, 0xef, 0x2f, 0x0c, 0x9e, 0x53, 0xf1, 0xf7, 0x5d, 0x78, 0x53, |
| 786 | 0x65, 0x9e, 0x2a, 0x20, 0xee, 0xb2, 0xb2, 0x2a, 0xaf, 0xde, 0x64, 0x19, |
| 787 | 0xa0, 0x58, 0xab, 0x4f, 0x6f, 0x74, 0x6b, 0xf4, 0x0f, 0xc0, 0xc3, 0xb7, |
| 788 | 0x80, 0xf2, 0x44, 0x45, 0x2d, 0xa3, 0xeb, 0xf1, 0xc5, 0xd8, 0x2c, 0xde, |
| 789 | 0xa2, 0x41, 0x89, 0x97, 0x20, 0x0e, 0xf8, 0x2e, 0x44, 0xae, 0x7e, 0x3f }, |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 790 | }; |
| 791 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 792 | static const unsigned char tag_test_data[MAX_TESTS * 3][16] = { |
| 793 | { 0x58, 0xe2, 0xfc, 0xce, 0xfa, 0x7e, 0x30, 0x61, 0x36, 0x7f, 0x1d, 0x57, |
| 794 | 0xa4, 0xe7, 0x45, 0x5a }, |
| 795 | { 0xab, 0x6e, 0x47, 0xd4, 0x2c, 0xec, 0x13, 0xbd, 0xf5, 0x3a, 0x67, 0xb2, |
| 796 | 0x12, 0x57, 0xbd, 0xdf }, |
| 797 | { 0x4d, 0x5c, 0x2a, 0xf3, 0x27, 0xcd, 0x64, 0xa6, 0x2c, 0xf3, 0x5a, 0xbd, |
| 798 | 0x2b, 0xa6, 0xfa, 0xb4 }, |
| 799 | { 0x5b, 0xc9, 0x4f, 0xbc, 0x32, 0x21, 0xa5, 0xdb, 0x94, 0xfa, 0xe9, 0x5a, |
| 800 | 0xe7, 0x12, 0x1a, 0x47 }, |
| 801 | { 0x36, 0x12, 0xd2, 0xe7, 0x9e, 0x3b, 0x07, 0x85, 0x56, 0x1b, 0xe1, 0x4a, |
| 802 | 0xac, 0xa2, 0xfc, 0xcb }, |
| 803 | { 0x61, 0x9c, 0xc5, 0xae, 0xff, 0xfe, 0x0b, 0xfa, 0x46, 0x2a, 0xf4, 0x3c, |
| 804 | 0x16, 0x99, 0xd0, 0x50 }, |
| 805 | { 0xcd, 0x33, 0xb2, 0x8a, 0xc7, 0x73, 0xf7, 0x4b, 0xa0, 0x0e, 0xd1, 0xf3, |
| 806 | 0x12, 0x57, 0x24, 0x35 }, |
| 807 | { 0x2f, 0xf5, 0x8d, 0x80, 0x03, 0x39, 0x27, 0xab, 0x8e, 0xf4, 0xd4, 0x58, |
| 808 | 0x75, 0x14, 0xf0, 0xfb }, |
| 809 | { 0x99, 0x24, 0xa7, 0xc8, 0x58, 0x73, 0x36, 0xbf, 0xb1, 0x18, 0x02, 0x4d, |
| 810 | 0xb8, 0x67, 0x4a, 0x14 }, |
| 811 | { 0x25, 0x19, 0x49, 0x8e, 0x80, 0xf1, 0x47, 0x8f, 0x37, 0xba, 0x55, 0xbd, |
| 812 | 0x6d, 0x27, 0x61, 0x8c }, |
| 813 | { 0x65, 0xdc, 0xc5, 0x7f, 0xcf, 0x62, 0x3a, 0x24, 0x09, 0x4f, 0xcc, 0xa4, |
| 814 | 0x0d, 0x35, 0x33, 0xf8 }, |
| 815 | { 0xdc, 0xf5, 0x66, 0xff, 0x29, 0x1c, 0x25, 0xbb, 0xb8, 0x56, 0x8f, 0xc3, |
| 816 | 0xd3, 0x76, 0xa6, 0xd9 }, |
| 817 | { 0x53, 0x0f, 0x8a, 0xfb, 0xc7, 0x45, 0x36, 0xb9, 0xa9, 0x63, 0xb4, 0xf1, |
| 818 | 0xc4, 0xcb, 0x73, 0x8b }, |
| 819 | { 0xd0, 0xd1, 0xc8, 0xa7, 0x99, 0x99, 0x6b, 0xf0, 0x26, 0x5b, 0x98, 0xb5, |
| 820 | 0xd4, 0x8a, 0xb9, 0x19 }, |
| 821 | { 0xb0, 0x94, 0xda, 0xc5, 0xd9, 0x34, 0x71, 0xbd, 0xec, 0x1a, 0x50, 0x22, |
| 822 | 0x70, 0xe3, 0xcc, 0x6c }, |
| 823 | { 0x76, 0xfc, 0x6e, 0xce, 0x0f, 0x4e, 0x17, 0x68, 0xcd, 0xdf, 0x88, 0x53, |
| 824 | 0xbb, 0x2d, 0x55, 0x1b }, |
| 825 | { 0x3a, 0x33, 0x7d, 0xbf, 0x46, 0xa7, 0x92, 0xc4, 0x5e, 0x45, 0x49, 0x13, |
| 826 | 0xfe, 0x2e, 0xa8, 0xf2 }, |
| 827 | { 0xa4, 0x4a, 0x82, 0x66, 0xee, 0x1c, 0x8e, 0xb0, 0xc8, 0xb5, 0xd4, 0xcf, |
| 828 | 0x5a, 0xe9, 0xf1, 0x9a }, |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 829 | }; |
| 830 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 831 | int mbedtls_gcm_self_test(int verbose) |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 832 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 833 | mbedtls_gcm_context ctx; |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 834 | unsigned char buf[64]; |
| 835 | unsigned char tag_buf[16]; |
| 836 | int i, j, ret; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 837 | mbedtls_cipher_id_t cipher = MBEDTLS_CIPHER_ID_AES; |
Gilles Peskine | a56c448 | 2021-04-15 17:22:35 +0200 | [diff] [blame] | 838 | size_t olen; |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 839 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 840 | for (j = 0; j < 3; j++) { |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 841 | int key_len = 128 + 64 * j; |
| 842 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 843 | for (i = 0; i < MAX_TESTS; i++) { |
| 844 | mbedtls_gcm_init(&ctx); |
Andres Amaya Garcia | 2a078da | 2017-06-15 11:30:51 +0100 | [diff] [blame] | 845 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 846 | if (verbose != 0) |
| 847 | mbedtls_printf(" AES-GCM-%3d #%d (%s): ", key_len, i, "enc"); |
Paul Bakker | b9d3cfa | 2013-06-26 15:07:16 +0200 | [diff] [blame] | 848 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 849 | ret = mbedtls_gcm_setkey( |
| 850 | &ctx, cipher, key_test_data[key_index_test_data[i]], key_len); |
Andres Amaya Garcia | d3e7e7d | 2017-06-15 16:17:46 +0100 | [diff] [blame] | 851 | /* |
| 852 | * AES-192 is an optional feature that may be unavailable when |
| 853 | * there is an alternative underlying implementation i.e. when |
| 854 | * MBEDTLS_AES_ALT is defined. |
| 855 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 856 | if (ret == MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED && |
| 857 | key_len == 192) { |
| 858 | mbedtls_printf("skipped\n"); |
| 859 | break; |
| 860 | } else if (ret != 0) { |
| 861 | goto exit; |
| 862 | } |
| 863 | |
| 864 | ret = mbedtls_gcm_crypt_and_tag( |
| 865 | &ctx, MBEDTLS_GCM_ENCRYPT, pt_len_test_data[i], |
| 866 | iv_test_data[iv_index_test_data[i]], iv_len_test_data[i], |
| 867 | additional_test_data[add_index_test_data[i]], |
| 868 | add_len_test_data[i], pt_test_data[pt_index_test_data[i]], buf, |
| 869 | 16, tag_buf); |
| 870 | # if defined(MBEDTLS_GCM_ALT) |
| 871 | /* Allow alternative implementations to only support 12-byte nonces. |
| 872 | */ |
| 873 | if (ret == MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED && |
| 874 | iv_len_test_data[i] != 12) { |
| 875 | mbedtls_printf("skipped\n"); |
Andres Amaya Garcia | 2a078da | 2017-06-15 11:30:51 +0100 | [diff] [blame] | 876 | break; |
| 877 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 878 | # endif /* defined(MBEDTLS_GCM_ALT) */ |
| 879 | if (ret != 0) |
Andres Amaya Garcia | 2a078da | 2017-06-15 11:30:51 +0100 | [diff] [blame] | 880 | goto exit; |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 881 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 882 | if (memcmp(buf, ct_test_data[j * 6 + i], pt_len_test_data[i]) != |
| 883 | 0 || |
| 884 | memcmp(tag_buf, tag_test_data[j * 6 + i], 16) != 0) { |
Andres Amaya Garcia | 2a078da | 2017-06-15 11:30:51 +0100 | [diff] [blame] | 885 | ret = 1; |
| 886 | goto exit; |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 887 | } |
| 888 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 889 | mbedtls_gcm_free(&ctx); |
Manuel Pégourié-Gonnard | 4fe9200 | 2013-09-13 13:45:58 +0200 | [diff] [blame] | 890 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 891 | if (verbose != 0) |
| 892 | mbedtls_printf("passed\n"); |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 893 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 894 | mbedtls_gcm_init(&ctx); |
Andres Amaya Garcia | 2a078da | 2017-06-15 11:30:51 +0100 | [diff] [blame] | 895 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 896 | if (verbose != 0) |
| 897 | mbedtls_printf(" AES-GCM-%3d #%d (%s): ", key_len, i, "dec"); |
Paul Bakker | b9d3cfa | 2013-06-26 15:07:16 +0200 | [diff] [blame] | 898 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 899 | ret = mbedtls_gcm_setkey( |
| 900 | &ctx, cipher, key_test_data[key_index_test_data[i]], key_len); |
| 901 | if (ret != 0) |
Andres Amaya Garcia | 2a078da | 2017-06-15 11:30:51 +0100 | [diff] [blame] | 902 | goto exit; |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 903 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 904 | ret = mbedtls_gcm_crypt_and_tag( |
| 905 | &ctx, MBEDTLS_GCM_DECRYPT, pt_len_test_data[i], |
| 906 | iv_test_data[iv_index_test_data[i]], iv_len_test_data[i], |
| 907 | additional_test_data[add_index_test_data[i]], |
| 908 | add_len_test_data[i], ct_test_data[j * 6 + i], buf, 16, |
| 909 | tag_buf); |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 910 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 911 | if (ret != 0) |
Andres Amaya Garcia | 2a078da | 2017-06-15 11:30:51 +0100 | [diff] [blame] | 912 | goto exit; |
| 913 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 914 | if (memcmp(buf, pt_test_data[pt_index_test_data[i]], |
| 915 | pt_len_test_data[i]) != 0 || |
| 916 | memcmp(tag_buf, tag_test_data[j * 6 + i], 16) != 0) { |
Andres Amaya Garcia | 2a078da | 2017-06-15 11:30:51 +0100 | [diff] [blame] | 917 | ret = 1; |
| 918 | goto exit; |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 919 | } |
| 920 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 921 | mbedtls_gcm_free(&ctx); |
Manuel Pégourié-Gonnard | 4fe9200 | 2013-09-13 13:45:58 +0200 | [diff] [blame] | 922 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 923 | if (verbose != 0) |
| 924 | mbedtls_printf("passed\n"); |
Paul Bakker | b9d3cfa | 2013-06-26 15:07:16 +0200 | [diff] [blame] | 925 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 926 | mbedtls_gcm_init(&ctx); |
Andres Amaya Garcia | 2a078da | 2017-06-15 11:30:51 +0100 | [diff] [blame] | 927 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 928 | if (verbose != 0) |
| 929 | mbedtls_printf(" AES-GCM-%3d #%d split (%s): ", key_len, i, |
| 930 | "enc"); |
Paul Bakker | b9d3cfa | 2013-06-26 15:07:16 +0200 | [diff] [blame] | 931 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 932 | ret = mbedtls_gcm_setkey( |
| 933 | &ctx, cipher, key_test_data[key_index_test_data[i]], key_len); |
| 934 | if (ret != 0) |
Andres Amaya Garcia | 2a078da | 2017-06-15 11:30:51 +0100 | [diff] [blame] | 935 | goto exit; |
Paul Bakker | b9d3cfa | 2013-06-26 15:07:16 +0200 | [diff] [blame] | 936 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 937 | ret = mbedtls_gcm_starts(&ctx, MBEDTLS_GCM_ENCRYPT, |
| 938 | iv_test_data[iv_index_test_data[i]], |
| 939 | iv_len_test_data[i]); |
| 940 | if (ret != 0) |
Gilles Peskine | 295fc13 | 2021-04-15 18:32:23 +0200 | [diff] [blame] | 941 | goto exit; |
| 942 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 943 | ret = mbedtls_gcm_update_ad( |
| 944 | &ctx, additional_test_data[add_index_test_data[i]], |
| 945 | add_len_test_data[i]); |
| 946 | if (ret != 0) |
Andres Amaya Garcia | 2a078da | 2017-06-15 11:30:51 +0100 | [diff] [blame] | 947 | goto exit; |
Paul Bakker | b9d3cfa | 2013-06-26 15:07:16 +0200 | [diff] [blame] | 948 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 949 | if (pt_len_test_data[i] > 32) { |
Michał Janiszewski | c79e92b | 2018-10-31 20:43:05 +0100 | [diff] [blame] | 950 | size_t rest_len = pt_len_test_data[i] - 32; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 951 | ret = mbedtls_gcm_update(&ctx, |
| 952 | pt_test_data[pt_index_test_data[i]], |
| 953 | 32, buf, sizeof(buf), &olen); |
| 954 | if (ret != 0) |
Andres Amaya Garcia | 2a078da | 2017-06-15 11:30:51 +0100 | [diff] [blame] | 955 | goto exit; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 956 | if (olen != 32) |
Gilles Peskine | a56c448 | 2021-04-15 17:22:35 +0200 | [diff] [blame] | 957 | goto exit; |
Paul Bakker | b9d3cfa | 2013-06-26 15:07:16 +0200 | [diff] [blame] | 958 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 959 | ret = mbedtls_gcm_update( |
| 960 | &ctx, pt_test_data[pt_index_test_data[i]] + 32, rest_len, |
| 961 | buf + 32, sizeof(buf) - 32, &olen); |
| 962 | if (ret != 0) |
Andres Amaya Garcia | 2a078da | 2017-06-15 11:30:51 +0100 | [diff] [blame] | 963 | goto exit; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 964 | if (olen != rest_len) |
Gilles Peskine | a56c448 | 2021-04-15 17:22:35 +0200 | [diff] [blame] | 965 | goto exit; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 966 | } else { |
| 967 | ret = mbedtls_gcm_update(&ctx, |
| 968 | pt_test_data[pt_index_test_data[i]], |
| 969 | pt_len_test_data[i], buf, sizeof(buf), |
| 970 | &olen); |
| 971 | if (ret != 0) |
Andres Amaya Garcia | 2a078da | 2017-06-15 11:30:51 +0100 | [diff] [blame] | 972 | goto exit; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 973 | if (olen != pt_len_test_data[i]) |
Gilles Peskine | a56c448 | 2021-04-15 17:22:35 +0200 | [diff] [blame] | 974 | goto exit; |
Paul Bakker | b9d3cfa | 2013-06-26 15:07:16 +0200 | [diff] [blame] | 975 | } |
| 976 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 977 | ret = mbedtls_gcm_finish(&ctx, NULL, 0, &olen, tag_buf, 16); |
| 978 | if (ret != 0) |
Andres Amaya Garcia | 2a078da | 2017-06-15 11:30:51 +0100 | [diff] [blame] | 979 | goto exit; |
| 980 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 981 | if (memcmp(buf, ct_test_data[j * 6 + i], pt_len_test_data[i]) != |
| 982 | 0 || |
| 983 | memcmp(tag_buf, tag_test_data[j * 6 + i], 16) != 0) { |
Andres Amaya Garcia | 2a078da | 2017-06-15 11:30:51 +0100 | [diff] [blame] | 984 | ret = 1; |
| 985 | goto exit; |
Paul Bakker | b9d3cfa | 2013-06-26 15:07:16 +0200 | [diff] [blame] | 986 | } |
| 987 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 988 | mbedtls_gcm_free(&ctx); |
Manuel Pégourié-Gonnard | 4fe9200 | 2013-09-13 13:45:58 +0200 | [diff] [blame] | 989 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 990 | if (verbose != 0) |
| 991 | mbedtls_printf("passed\n"); |
Paul Bakker | b9d3cfa | 2013-06-26 15:07:16 +0200 | [diff] [blame] | 992 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 993 | mbedtls_gcm_init(&ctx); |
Andres Amaya Garcia | 2a078da | 2017-06-15 11:30:51 +0100 | [diff] [blame] | 994 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 995 | if (verbose != 0) |
| 996 | mbedtls_printf(" AES-GCM-%3d #%d split (%s): ", key_len, i, |
| 997 | "dec"); |
Paul Bakker | b9d3cfa | 2013-06-26 15:07:16 +0200 | [diff] [blame] | 998 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 999 | ret = mbedtls_gcm_setkey( |
| 1000 | &ctx, cipher, key_test_data[key_index_test_data[i]], key_len); |
| 1001 | if (ret != 0) |
Andres Amaya Garcia | 2a078da | 2017-06-15 11:30:51 +0100 | [diff] [blame] | 1002 | goto exit; |
Paul Bakker | b9d3cfa | 2013-06-26 15:07:16 +0200 | [diff] [blame] | 1003 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1004 | ret = mbedtls_gcm_starts(&ctx, MBEDTLS_GCM_DECRYPT, |
| 1005 | iv_test_data[iv_index_test_data[i]], |
| 1006 | iv_len_test_data[i]); |
| 1007 | if (ret != 0) |
Gilles Peskine | 295fc13 | 2021-04-15 18:32:23 +0200 | [diff] [blame] | 1008 | goto exit; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1009 | ret = mbedtls_gcm_update_ad( |
| 1010 | &ctx, additional_test_data[add_index_test_data[i]], |
| 1011 | add_len_test_data[i]); |
| 1012 | if (ret != 0) |
Andres Amaya Garcia | 2a078da | 2017-06-15 11:30:51 +0100 | [diff] [blame] | 1013 | goto exit; |
Paul Bakker | b9d3cfa | 2013-06-26 15:07:16 +0200 | [diff] [blame] | 1014 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1015 | if (pt_len_test_data[i] > 32) { |
Michał Janiszewski | c79e92b | 2018-10-31 20:43:05 +0100 | [diff] [blame] | 1016 | size_t rest_len = pt_len_test_data[i] - 32; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1017 | ret = mbedtls_gcm_update(&ctx, ct_test_data[j * 6 + i], 32, buf, |
| 1018 | sizeof(buf), &olen); |
| 1019 | if (ret != 0) |
Andres Amaya Garcia | 2a078da | 2017-06-15 11:30:51 +0100 | [diff] [blame] | 1020 | goto exit; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1021 | if (olen != 32) |
Gilles Peskine | a56c448 | 2021-04-15 17:22:35 +0200 | [diff] [blame] | 1022 | goto exit; |
Paul Bakker | b9d3cfa | 2013-06-26 15:07:16 +0200 | [diff] [blame] | 1023 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1024 | ret = mbedtls_gcm_update(&ctx, ct_test_data[j * 6 + i] + 32, |
| 1025 | rest_len, buf + 32, sizeof(buf) - 32, |
| 1026 | &olen); |
| 1027 | if (ret != 0) |
Andres Amaya Garcia | 2a078da | 2017-06-15 11:30:51 +0100 | [diff] [blame] | 1028 | goto exit; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1029 | if (olen != rest_len) |
Gilles Peskine | a56c448 | 2021-04-15 17:22:35 +0200 | [diff] [blame] | 1030 | goto exit; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1031 | } else { |
| 1032 | ret = mbedtls_gcm_update(&ctx, ct_test_data[j * 6 + i], |
| 1033 | pt_len_test_data[i], buf, sizeof(buf), |
| 1034 | &olen); |
| 1035 | if (ret != 0) |
Andres Amaya Garcia | 2a078da | 2017-06-15 11:30:51 +0100 | [diff] [blame] | 1036 | goto exit; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1037 | if (olen != pt_len_test_data[i]) |
Gilles Peskine | a56c448 | 2021-04-15 17:22:35 +0200 | [diff] [blame] | 1038 | goto exit; |
Paul Bakker | b9d3cfa | 2013-06-26 15:07:16 +0200 | [diff] [blame] | 1039 | } |
| 1040 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1041 | ret = mbedtls_gcm_finish(&ctx, NULL, 0, &olen, tag_buf, 16); |
| 1042 | if (ret != 0) |
Andres Amaya Garcia | 2a078da | 2017-06-15 11:30:51 +0100 | [diff] [blame] | 1043 | goto exit; |
| 1044 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1045 | if (memcmp(buf, pt_test_data[pt_index_test_data[i]], |
| 1046 | pt_len_test_data[i]) != 0 || |
| 1047 | memcmp(tag_buf, tag_test_data[j * 6 + i], 16) != 0) { |
Andres Amaya Garcia | 2a078da | 2017-06-15 11:30:51 +0100 | [diff] [blame] | 1048 | ret = 1; |
| 1049 | goto exit; |
Paul Bakker | b9d3cfa | 2013-06-26 15:07:16 +0200 | [diff] [blame] | 1050 | } |
| 1051 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1052 | mbedtls_gcm_free(&ctx); |
Manuel Pégourié-Gonnard | 4fe9200 | 2013-09-13 13:45:58 +0200 | [diff] [blame] | 1053 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1054 | if (verbose != 0) |
| 1055 | mbedtls_printf("passed\n"); |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 1056 | } |
| 1057 | } |
Paul Bakker | 169b7f4 | 2013-06-25 14:58:00 +0200 | [diff] [blame] | 1058 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1059 | if (verbose != 0) |
| 1060 | mbedtls_printf("\n"); |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 1061 | |
Andres Amaya Garcia | 2a078da | 2017-06-15 11:30:51 +0100 | [diff] [blame] | 1062 | ret = 0; |
| 1063 | |
| 1064 | exit: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1065 | if (ret != 0) { |
| 1066 | if (verbose != 0) |
| 1067 | mbedtls_printf("failed\n"); |
| 1068 | mbedtls_gcm_free(&ctx); |
Andres Amaya Garcia | 2a078da | 2017-06-15 11:30:51 +0100 | [diff] [blame] | 1069 | } |
| 1070 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1071 | return ret; |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 1072 | } |
| 1073 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1074 | # endif /* MBEDTLS_SELF_TEST && MBEDTLS_AES_C */ |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 1075 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1076 | #endif /* MBEDTLS_GCM_C */ |