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