/src/openssl33/providers/implementations/ciphers/ciphercommon_gcm.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2019-2023 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the Apache License 2.0 (the "License"). You may not use |
5 | | * this file except in compliance with the License. You can obtain a copy |
6 | | * in the file LICENSE in the source distribution or at |
7 | | * https://www.openssl.org/source/license.html |
8 | | */ |
9 | | |
10 | | /* Dispatch functions for gcm mode */ |
11 | | |
12 | | #include <openssl/rand.h> |
13 | | #include <openssl/proverr.h> |
14 | | #include "prov/ciphercommon.h" |
15 | | #include "prov/ciphercommon_gcm.h" |
16 | | #include "prov/providercommon.h" |
17 | | #include "prov/provider_ctx.h" |
18 | | #include "internal/param_names.h" |
19 | | |
20 | | static int gcm_tls_init(PROV_GCM_CTX *dat, unsigned char *aad, size_t aad_len); |
21 | | static int gcm_tls_iv_set_fixed(PROV_GCM_CTX *ctx, unsigned char *iv, |
22 | | size_t len); |
23 | | static int gcm_tls_cipher(PROV_GCM_CTX *ctx, unsigned char *out, size_t *padlen, |
24 | | const unsigned char *in, size_t len); |
25 | | static int gcm_cipher_internal(PROV_GCM_CTX *ctx, unsigned char *out, |
26 | | size_t *padlen, const unsigned char *in, |
27 | | size_t len); |
28 | | |
29 | | /* |
30 | | * Called from EVP_CipherInit when there is currently no context via |
31 | | * the new_ctx() function |
32 | | */ |
33 | | void ossl_gcm_initctx(void *provctx, PROV_GCM_CTX *ctx, size_t keybits, |
34 | | const PROV_GCM_HW *hw) |
35 | 616k | { |
36 | 616k | ctx->pad = 1; |
37 | 616k | ctx->mode = EVP_CIPH_GCM_MODE; |
38 | 616k | ctx->taglen = UNINITIALISED_SIZET; |
39 | 616k | ctx->tls_aad_len = UNINITIALISED_SIZET; |
40 | 616k | ctx->ivlen = (EVP_GCM_TLS_FIXED_IV_LEN + EVP_GCM_TLS_EXPLICIT_IV_LEN); |
41 | 616k | ctx->keylen = keybits / 8; |
42 | 616k | ctx->hw = hw; |
43 | 616k | ctx->libctx = PROV_LIBCTX_OF(provctx); |
44 | 616k | } |
45 | | |
46 | | /* |
47 | | * Called by EVP_CipherInit via the _einit and _dinit functions |
48 | | */ |
49 | | static int gcm_init(void *vctx, const unsigned char *key, size_t keylen, |
50 | | const unsigned char *iv, size_t ivlen, |
51 | | const OSSL_PARAM params[], int enc) |
52 | 3.88M | { |
53 | 3.88M | PROV_GCM_CTX *ctx = (PROV_GCM_CTX *)vctx; |
54 | | |
55 | 3.88M | if (!ossl_prov_is_running()) |
56 | 0 | return 0; |
57 | | |
58 | 3.88M | ctx->enc = enc; |
59 | | |
60 | 3.88M | if (iv != NULL) { |
61 | 3.80M | if (ivlen == 0 || ivlen > sizeof(ctx->iv)) { |
62 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH); |
63 | 0 | return 0; |
64 | 0 | } |
65 | 3.80M | ctx->ivlen = ivlen; |
66 | 3.80M | memcpy(ctx->iv, iv, ivlen); |
67 | 3.80M | ctx->iv_state = IV_STATE_BUFFERED; |
68 | 3.80M | } |
69 | | |
70 | 3.88M | if (key != NULL) { |
71 | 616k | if (keylen != ctx->keylen) { |
72 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH); |
73 | 0 | return 0; |
74 | 0 | } |
75 | 616k | if (!ctx->hw->setkey(ctx, key, ctx->keylen)) |
76 | 0 | return 0; |
77 | 616k | ctx->tls_enc_records = 0; |
78 | 616k | } |
79 | 3.88M | return ossl_gcm_set_ctx_params(ctx, params); |
80 | 3.88M | } |
81 | | |
82 | | int ossl_gcm_einit(void *vctx, const unsigned char *key, size_t keylen, |
83 | | const unsigned char *iv, size_t ivlen, |
84 | | const OSSL_PARAM params[]) |
85 | 2.39M | { |
86 | 2.39M | return gcm_init(vctx, key, keylen, iv, ivlen, params, 1); |
87 | 2.39M | } |
88 | | |
89 | | int ossl_gcm_dinit(void *vctx, const unsigned char *key, size_t keylen, |
90 | | const unsigned char *iv, size_t ivlen, |
91 | | const OSSL_PARAM params[]) |
92 | 1.48M | { |
93 | 1.48M | return gcm_init(vctx, key, keylen, iv, ivlen, params, 0); |
94 | 1.48M | } |
95 | | |
96 | | /* increment counter (64-bit int) by 1 */ |
97 | | static void ctr64_inc(unsigned char *counter) |
98 | 1.68k | { |
99 | 1.68k | int n = 8; |
100 | 1.68k | unsigned char c; |
101 | | |
102 | 1.68k | do { |
103 | 1.68k | --n; |
104 | 1.68k | c = counter[n]; |
105 | 1.68k | ++c; |
106 | 1.68k | counter[n] = c; |
107 | 1.68k | if (c > 0) |
108 | 1.68k | return; |
109 | 1.68k | } while (n > 0); |
110 | 1.68k | } |
111 | | |
112 | | static int getivgen(PROV_GCM_CTX *ctx, unsigned char *out, size_t olen) |
113 | 1.68k | { |
114 | 1.68k | if (!ctx->iv_gen |
115 | 1.68k | || !ctx->key_set |
116 | 1.68k | || !ctx->hw->setiv(ctx, ctx->iv, ctx->ivlen)) |
117 | 0 | return 0; |
118 | 1.68k | if (olen == 0 || olen > ctx->ivlen) |
119 | 0 | olen = ctx->ivlen; |
120 | 1.68k | memcpy(out, ctx->iv + ctx->ivlen - olen, olen); |
121 | | /* |
122 | | * Invocation field will be at least 8 bytes in size and so no need |
123 | | * to check wrap around or increment more than last 8 bytes. |
124 | | */ |
125 | 1.68k | ctr64_inc(ctx->iv + ctx->ivlen - 8); |
126 | 1.68k | ctx->iv_state = IV_STATE_COPIED; |
127 | 1.68k | return 1; |
128 | 1.68k | } |
129 | | |
130 | | static int setivinv(PROV_GCM_CTX *ctx, unsigned char *in, size_t inl) |
131 | 66.0k | { |
132 | 66.0k | if (!ctx->iv_gen |
133 | 66.0k | || !ctx->key_set |
134 | 66.0k | || ctx->enc) |
135 | 0 | return 0; |
136 | | |
137 | 66.0k | memcpy(ctx->iv + ctx->ivlen - inl, in, inl); |
138 | 66.0k | if (!ctx->hw->setiv(ctx, ctx->iv, ctx->ivlen)) |
139 | 0 | return 0; |
140 | 66.0k | ctx->iv_state = IV_STATE_COPIED; |
141 | 66.0k | return 1; |
142 | 66.0k | } |
143 | | |
144 | | int ossl_gcm_get_ctx_params(void *vctx, OSSL_PARAM params[]) |
145 | 670k | { |
146 | 670k | PROV_GCM_CTX *ctx = (PROV_GCM_CTX *)vctx; |
147 | 670k | OSSL_PARAM *p; |
148 | 670k | size_t sz; |
149 | 670k | int type; |
150 | | |
151 | 1.34M | for (p = params; p->key != NULL; p++) { |
152 | 670k | type = ossl_param_find_pidx(p->key); |
153 | 670k | switch (type) { |
154 | 11 | default: |
155 | 11 | break; |
156 | | |
157 | 79.0k | case PIDX_CIPHER_PARAM_IVLEN: |
158 | 79.0k | if (!OSSL_PARAM_set_size_t(p, ctx->ivlen)) { |
159 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER); |
160 | 0 | return 0; |
161 | 0 | } |
162 | 79.0k | break; |
163 | | |
164 | 79.7k | case PIDX_CIPHER_PARAM_KEYLEN: |
165 | 79.7k | if (!OSSL_PARAM_set_size_t(p, ctx->keylen)) { |
166 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER); |
167 | 0 | return 0; |
168 | 0 | } |
169 | 79.7k | break; |
170 | | |
171 | 79.7k | case PIDX_CIPHER_PARAM_AEAD_TAGLEN: { |
172 | 0 | size_t taglen = (ctx->taglen != UNINITIALISED_SIZET) ? ctx->taglen : GCM_TAG_MAX_SIZE; |
173 | |
|
174 | 0 | if (!OSSL_PARAM_set_size_t(p, taglen)) { |
175 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER); |
176 | 0 | return 0; |
177 | 0 | } |
178 | 0 | } break; |
179 | | |
180 | 0 | case PIDX_CIPHER_PARAM_IV: |
181 | 0 | if (ctx->iv_state == IV_STATE_UNINITIALISED) |
182 | 0 | return 0; |
183 | 0 | if (ctx->ivlen > p->data_size) { |
184 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH); |
185 | 0 | return 0; |
186 | 0 | } |
187 | 0 | if (!OSSL_PARAM_set_octet_string(p, ctx->iv, ctx->ivlen) |
188 | 0 | && !OSSL_PARAM_set_octet_ptr(p, &ctx->iv, ctx->ivlen)) { |
189 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER); |
190 | 0 | return 0; |
191 | 0 | } |
192 | 0 | break; |
193 | | |
194 | 0 | case PIDX_CIPHER_PARAM_UPDATED_IV: |
195 | 0 | if (ctx->iv_state == IV_STATE_UNINITIALISED) |
196 | 0 | return 0; |
197 | 0 | if (ctx->ivlen > p->data_size) { |
198 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH); |
199 | 0 | return 0; |
200 | 0 | } |
201 | 0 | if (!OSSL_PARAM_set_octet_string(p, ctx->iv, ctx->ivlen) |
202 | 0 | && !OSSL_PARAM_set_octet_ptr(p, &ctx->iv, ctx->ivlen)) { |
203 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER); |
204 | 0 | return 0; |
205 | 0 | } |
206 | 0 | break; |
207 | | |
208 | 15.2k | case PIDX_CIPHER_PARAM_AEAD_TLS1_AAD_PAD: |
209 | 15.2k | if (!OSSL_PARAM_set_size_t(p, ctx->tls_aad_pad_sz)) { |
210 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER); |
211 | 0 | return 0; |
212 | 0 | } |
213 | 15.2k | break; |
214 | | |
215 | 496k | case PIDX_CIPHER_PARAM_AEAD_TAG: |
216 | 496k | sz = p->data_size; |
217 | 496k | if (sz == 0 |
218 | 496k | || sz > EVP_GCM_TLS_TAG_LEN |
219 | 496k | || !ctx->enc |
220 | 496k | || ctx->taglen == UNINITIALISED_SIZET) { |
221 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_TAG); |
222 | 0 | return 0; |
223 | 0 | } |
224 | 496k | if (!OSSL_PARAM_set_octet_string(p, ctx->buf, sz)) { |
225 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER); |
226 | 0 | return 0; |
227 | 0 | } |
228 | 496k | break; |
229 | | |
230 | 496k | case PIDX_CIPHER_PARAM_AEAD_TLS1_GET_IV_GEN: |
231 | 0 | if (p->data == NULL |
232 | 0 | || p->data_type != OSSL_PARAM_OCTET_STRING |
233 | 0 | || !getivgen(ctx, p->data, p->data_size)) |
234 | 0 | return 0; |
235 | 0 | break; |
236 | 670k | } |
237 | 670k | } |
238 | 670k | return 1; |
239 | 670k | } |
240 | | |
241 | | int ossl_gcm_set_ctx_params(void *vctx, const OSSL_PARAM params[]) |
242 | 2.13M | { |
243 | 2.13M | PROV_GCM_CTX *ctx = (PROV_GCM_CTX *)vctx; |
244 | 2.13M | const OSSL_PARAM *p; |
245 | 2.13M | size_t sz; |
246 | 2.13M | void *vp; |
247 | 2.13M | int type; |
248 | | |
249 | 2.13M | if (params == NULL) |
250 | 1.56M | return 1; |
251 | | |
252 | 1.13M | for (p = params; p->key != NULL; p++) { |
253 | 567k | type = ossl_param_find_pidx(p->key); |
254 | 567k | switch (type) { |
255 | 2.04k | default: |
256 | 2.04k | break; |
257 | | |
258 | 543k | case PIDX_CIPHER_PARAM_AEAD_TAG: |
259 | 543k | vp = ctx->buf; |
260 | 543k | if (!OSSL_PARAM_get_octet_string(p, &vp, EVP_GCM_TLS_TAG_LEN, &sz)) { |
261 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER); |
262 | 0 | return 0; |
263 | 0 | } |
264 | 543k | if (sz == 0 || ctx->enc) { |
265 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_TAG); |
266 | 0 | return 0; |
267 | 0 | } |
268 | 543k | ctx->taglen = sz; |
269 | 543k | break; |
270 | | |
271 | 1.44k | case PIDX_CIPHER_PARAM_AEAD_IVLEN: |
272 | 1.44k | if (!OSSL_PARAM_get_size_t(p, &sz)) { |
273 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER); |
274 | 0 | return 0; |
275 | 0 | } |
276 | 1.44k | if (sz == 0 || sz > sizeof(ctx->iv)) { |
277 | 20 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH); |
278 | 20 | return 0; |
279 | 20 | } |
280 | 1.42k | if (ctx->ivlen != sz) { |
281 | | /* If the iv was already set or autogenerated, it is invalid. */ |
282 | 5 | if (ctx->iv_state != IV_STATE_UNINITIALISED) |
283 | 0 | ctx->iv_state = IV_STATE_FINISHED; |
284 | 5 | ctx->ivlen = sz; |
285 | 5 | } |
286 | 1.42k | break; |
287 | | |
288 | 20.2k | case PIDX_CIPHER_PARAM_AEAD_TLS1_AAD: |
289 | 20.2k | if (p->data_type != OSSL_PARAM_OCTET_STRING) { |
290 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER); |
291 | 0 | return 0; |
292 | 0 | } |
293 | 20.2k | sz = gcm_tls_init(ctx, p->data, p->data_size); |
294 | 20.2k | if (sz == 0) { |
295 | 39 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_AAD); |
296 | 39 | return 0; |
297 | 39 | } |
298 | 20.1k | ctx->tls_aad_pad_sz = sz; |
299 | 20.1k | break; |
300 | | |
301 | 876 | case PIDX_CIPHER_PARAM_AEAD_TLS1_IV_FIXED: |
302 | 876 | if (p->data_type != OSSL_PARAM_OCTET_STRING) { |
303 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER); |
304 | 0 | return 0; |
305 | 0 | } |
306 | 876 | if (gcm_tls_iv_set_fixed(ctx, p->data, p->data_size) == 0) { |
307 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER); |
308 | 0 | return 0; |
309 | 0 | } |
310 | 876 | break; |
311 | | |
312 | 876 | case PIDX_CIPHER_PARAM_AEAD_TLS1_SET_IV_INV: |
313 | 0 | if (p->data == NULL |
314 | 0 | || p->data_type != OSSL_PARAM_OCTET_STRING |
315 | 0 | || !setivinv(ctx, p->data, p->data_size)) |
316 | 0 | return 0; |
317 | 0 | break; |
318 | 567k | } |
319 | 567k | } |
320 | | |
321 | 566k | return 1; |
322 | 566k | } |
323 | | |
324 | | int ossl_gcm_stream_update(void *vctx, unsigned char *out, size_t *outl, |
325 | | size_t outsize, const unsigned char *in, size_t inl) |
326 | 9.11M | { |
327 | 9.11M | PROV_GCM_CTX *ctx = (PROV_GCM_CTX *)vctx; |
328 | | |
329 | 9.11M | if (inl == 0) { |
330 | 0 | *outl = 0; |
331 | 0 | return 1; |
332 | 0 | } |
333 | | |
334 | 9.11M | if (outsize < inl) { |
335 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL); |
336 | 0 | return 0; |
337 | 0 | } |
338 | | |
339 | 9.11M | if (gcm_cipher_internal(ctx, out, outl, in, inl) <= 0) { |
340 | 65.8k | ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED); |
341 | 65.8k | return 0; |
342 | 65.8k | } |
343 | 9.05M | return 1; |
344 | 9.11M | } |
345 | | |
346 | | int ossl_gcm_stream_final(void *vctx, unsigned char *out, size_t *outl, |
347 | | size_t outsize) |
348 | 3.56M | { |
349 | 3.56M | PROV_GCM_CTX *ctx = (PROV_GCM_CTX *)vctx; |
350 | 3.56M | int i; |
351 | | |
352 | 3.56M | if (!ossl_prov_is_running()) |
353 | 0 | return 0; |
354 | | |
355 | 3.56M | i = gcm_cipher_internal(ctx, out, outl, NULL, 0); |
356 | 3.56M | if (i <= 0) |
357 | 1.23M | return 0; |
358 | | |
359 | 2.33M | *outl = 0; |
360 | 2.33M | return 1; |
361 | 3.56M | } |
362 | | |
363 | | int ossl_gcm_cipher(void *vctx, |
364 | | unsigned char *out, size_t *outl, size_t outsize, |
365 | | const unsigned char *in, size_t inl) |
366 | 0 | { |
367 | 0 | PROV_GCM_CTX *ctx = (PROV_GCM_CTX *)vctx; |
368 | |
|
369 | 0 | if (!ossl_prov_is_running()) |
370 | 0 | return 0; |
371 | | |
372 | 0 | if (outsize < inl) { |
373 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL); |
374 | 0 | return 0; |
375 | 0 | } |
376 | | |
377 | 0 | if (gcm_cipher_internal(ctx, out, outl, in, inl) <= 0) |
378 | 0 | return 0; |
379 | | |
380 | 0 | *outl = inl; |
381 | 0 | return 1; |
382 | 0 | } |
383 | | |
384 | | /* |
385 | | * See SP800-38D (GCM) Section 8 "Uniqueness requirement on IVS and keys" |
386 | | * |
387 | | * See also 8.2.2 RBG-based construction. |
388 | | * Random construction consists of a free field (which can be NULL) and a |
389 | | * random field which will use a DRBG that can return at least 96 bits of |
390 | | * entropy strength. (The DRBG must be seeded by the FIPS module). |
391 | | */ |
392 | | static int gcm_iv_generate(PROV_GCM_CTX *ctx, int offset) |
393 | 672 | { |
394 | 672 | int sz = ctx->ivlen - offset; |
395 | | |
396 | | /* Must be at least 96 bits */ |
397 | 672 | if (sz <= 0 || ctx->ivlen < GCM_IV_DEFAULT_SIZE) |
398 | 0 | return 0; |
399 | | |
400 | | /* Use DRBG to generate random iv */ |
401 | 672 | if (RAND_bytes_ex(ctx->libctx, ctx->iv + offset, sz, 0) <= 0) |
402 | 0 | return 0; |
403 | 672 | ctx->iv_state = IV_STATE_BUFFERED; |
404 | 672 | ctx->iv_gen_rand = 1; |
405 | 672 | return 1; |
406 | 672 | } |
407 | | |
408 | | static int gcm_cipher_internal(PROV_GCM_CTX *ctx, unsigned char *out, |
409 | | size_t *padlen, const unsigned char *in, |
410 | | size_t len) |
411 | 12.6M | { |
412 | 12.6M | size_t olen = 0; |
413 | 12.6M | int rv = 0; |
414 | 12.6M | const PROV_GCM_HW *hw = ctx->hw; |
415 | | |
416 | 12.6M | if (ctx->tls_aad_len != UNINITIALISED_SIZET) |
417 | 67.6k | return gcm_tls_cipher(ctx, out, padlen, in, len); |
418 | | |
419 | 12.6M | if (!ctx->key_set || ctx->iv_state == IV_STATE_FINISHED) |
420 | 0 | goto err; |
421 | | |
422 | | /* |
423 | | * FIPS requires generation of AES-GCM IV's inside the FIPS module. |
424 | | * The IV can still be set externally (the security policy will state that |
425 | | * this is not FIPS compliant). There are some applications |
426 | | * where setting the IV externally is the only option available. |
427 | | */ |
428 | 12.6M | if (ctx->iv_state == IV_STATE_UNINITIALISED) { |
429 | 672 | if (!ctx->enc || !gcm_iv_generate(ctx, 0)) |
430 | 0 | goto err; |
431 | 672 | } |
432 | | |
433 | 12.6M | if (ctx->iv_state == IV_STATE_BUFFERED) { |
434 | 3.56M | if (!hw->setiv(ctx, ctx->iv, ctx->ivlen)) |
435 | 0 | goto err; |
436 | 3.56M | ctx->iv_state = IV_STATE_COPIED; |
437 | 3.56M | } |
438 | | |
439 | 12.6M | if (in != NULL) { |
440 | | /* The input is AAD if out is NULL */ |
441 | 9.05M | if (out == NULL) { |
442 | 3.89M | if (!hw->aadupdate(ctx, in, len)) |
443 | 0 | goto err; |
444 | 5.15M | } else { |
445 | | /* The input is ciphertext OR plaintext */ |
446 | 5.15M | if (!hw->cipherupdate(ctx, in, len, out)) |
447 | 0 | goto err; |
448 | 5.15M | } |
449 | 9.05M | } else { |
450 | | /* The tag must be set before actually decrypting data */ |
451 | 3.56M | if (!ctx->enc && ctx->taglen == UNINITIALISED_SIZET) |
452 | 0 | goto err; |
453 | 3.56M | if (!hw->cipherfinal(ctx, ctx->buf)) |
454 | 1.23M | goto err; |
455 | 2.33M | ctx->iv_state = IV_STATE_FINISHED; /* Don't reuse the IV */ |
456 | 2.33M | goto finish; |
457 | 3.56M | } |
458 | 9.05M | olen = len; |
459 | 11.3M | finish: |
460 | 11.3M | rv = 1; |
461 | 12.6M | err: |
462 | 12.6M | *padlen = olen; |
463 | 12.6M | return rv; |
464 | 11.3M | } |
465 | | |
466 | | static int gcm_tls_init(PROV_GCM_CTX *dat, unsigned char *aad, size_t aad_len) |
467 | 67.8k | { |
468 | 67.8k | unsigned char *buf; |
469 | 67.8k | size_t len; |
470 | | |
471 | 67.8k | if (!ossl_prov_is_running() || aad_len != EVP_AEAD_TLS1_AAD_LEN) |
472 | 0 | return 0; |
473 | | |
474 | | /* Save the aad for later use. */ |
475 | 67.8k | buf = dat->buf; |
476 | 67.8k | memcpy(buf, aad, aad_len); |
477 | 67.8k | dat->tls_aad_len = aad_len; |
478 | | |
479 | 67.8k | len = buf[aad_len - 2] << 8 | buf[aad_len - 1]; |
480 | | /* Correct length for explicit iv. */ |
481 | 67.8k | if (len < EVP_GCM_TLS_EXPLICIT_IV_LEN) |
482 | 87 | return 0; |
483 | 67.7k | len -= EVP_GCM_TLS_EXPLICIT_IV_LEN; |
484 | | |
485 | | /* If decrypting correct for tag too. */ |
486 | 67.7k | if (!dat->enc) { |
487 | 66.0k | if (len < EVP_GCM_TLS_TAG_LEN) |
488 | 51 | return 0; |
489 | 66.0k | len -= EVP_GCM_TLS_TAG_LEN; |
490 | 66.0k | } |
491 | 67.6k | buf[aad_len - 2] = (unsigned char)(len >> 8); |
492 | 67.6k | buf[aad_len - 1] = (unsigned char)(len & 0xff); |
493 | | /* Extra padding: tag appended to record. */ |
494 | 67.6k | return EVP_GCM_TLS_TAG_LEN; |
495 | 67.7k | } |
496 | | |
497 | | static int gcm_tls_iv_set_fixed(PROV_GCM_CTX *ctx, unsigned char *iv, |
498 | | size_t len) |
499 | 731 | { |
500 | | /* Special case: -1 length restores whole IV */ |
501 | 731 | if (len == (size_t)-1) { |
502 | 0 | memcpy(ctx->iv, iv, ctx->ivlen); |
503 | 0 | ctx->iv_gen = 1; |
504 | 0 | ctx->iv_state = IV_STATE_BUFFERED; |
505 | 0 | return 1; |
506 | 0 | } |
507 | | /* Fixed field must be at least 4 bytes and invocation field at least 8 */ |
508 | 731 | if ((len < EVP_GCM_TLS_FIXED_IV_LEN) |
509 | 731 | || (ctx->ivlen - (int)len) < EVP_GCM_TLS_EXPLICIT_IV_LEN) |
510 | 0 | return 0; |
511 | 731 | if (len > 0) |
512 | 731 | memcpy(ctx->iv, iv, len); |
513 | 731 | if (ctx->enc |
514 | 182 | && RAND_bytes_ex(ctx->libctx, ctx->iv + len, ctx->ivlen - len, 0) <= 0) |
515 | 0 | return 0; |
516 | 731 | ctx->iv_gen = 1; |
517 | 731 | ctx->iv_state = IV_STATE_BUFFERED; |
518 | 731 | return 1; |
519 | 731 | } |
520 | | |
521 | | /* |
522 | | * Handle TLS GCM packet format. This consists of the last portion of the IV |
523 | | * followed by the payload and finally the tag. On encrypt generate IV, |
524 | | * encrypt payload and write the tag. On verify retrieve IV, decrypt payload |
525 | | * and verify tag. |
526 | | */ |
527 | | static int gcm_tls_cipher(PROV_GCM_CTX *ctx, unsigned char *out, size_t *padlen, |
528 | | const unsigned char *in, size_t len) |
529 | 67.6k | { |
530 | 67.6k | int rv = 0; |
531 | 67.6k | size_t arg = EVP_GCM_TLS_EXPLICIT_IV_LEN; |
532 | 67.6k | size_t plen = 0; |
533 | 67.6k | unsigned char *tag = NULL; |
534 | | |
535 | 67.6k | if (!ossl_prov_is_running() || !ctx->key_set) |
536 | 0 | goto err; |
537 | | |
538 | | /* Encrypt/decrypt must be performed in place */ |
539 | 67.6k | if (out != in || len < (EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN)) |
540 | 0 | goto err; |
541 | | |
542 | | /* |
543 | | * Check for too many keys as per FIPS 140-2 IG A.5 "Key/IV Pair Uniqueness |
544 | | * Requirements from SP 800-38D". The requirements is for one party to the |
545 | | * communication to fail after 2^64 - 1 keys. We do this on the encrypting |
546 | | * side only. |
547 | | */ |
548 | 67.6k | if (ctx->enc && ++ctx->tls_enc_records == 0) { |
549 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_TOO_MANY_RECORDS); |
550 | 0 | goto err; |
551 | 0 | } |
552 | | |
553 | | /* |
554 | | * Set IV from start of buffer or generate IV and write to start of |
555 | | * buffer. |
556 | | */ |
557 | 67.6k | if (ctx->enc) { |
558 | 1.68k | if (!getivgen(ctx, out, arg)) |
559 | 0 | goto err; |
560 | 66.0k | } else { |
561 | 66.0k | if (!setivinv(ctx, out, arg)) |
562 | 0 | goto err; |
563 | 66.0k | } |
564 | | |
565 | | /* Fix buffer and length to point to payload */ |
566 | 67.6k | in += EVP_GCM_TLS_EXPLICIT_IV_LEN; |
567 | 67.6k | out += EVP_GCM_TLS_EXPLICIT_IV_LEN; |
568 | 67.6k | len -= EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN; |
569 | | |
570 | 67.6k | tag = ctx->enc ? out + len : (unsigned char *)in + len; |
571 | 67.6k | if (!ctx->hw->oneshot(ctx, ctx->buf, ctx->tls_aad_len, in, len, out, tag, |
572 | 67.6k | EVP_GCM_TLS_TAG_LEN)) { |
573 | 65.8k | if (!ctx->enc) |
574 | 65.8k | OPENSSL_cleanse(out, len); |
575 | 65.8k | goto err; |
576 | 65.8k | } |
577 | 1.86k | if (ctx->enc) |
578 | 1.68k | plen = len + EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN; |
579 | 180 | else |
580 | 180 | plen = len; |
581 | | |
582 | 1.86k | rv = 1; |
583 | 67.6k | err: |
584 | 67.6k | ctx->iv_state = IV_STATE_FINISHED; |
585 | 67.6k | ctx->tls_aad_len = UNINITIALISED_SIZET; |
586 | 67.6k | *padlen = plen; |
587 | 67.6k | return rv; |
588 | 1.86k | } |