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