/src/openssl/providers/implementations/ciphers/ciphercommon_gcm.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2019-2024 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 | | |
11 | | /* Dispatch functions for gcm mode */ |
12 | | |
13 | | #include <openssl/rand.h> |
14 | | #include <openssl/proverr.h> |
15 | | #include "prov/ciphercommon.h" |
16 | | #include "prov/ciphercommon_gcm.h" |
17 | | #include "prov/providercommon.h" |
18 | | #include "prov/provider_ctx.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 | 0 | { |
36 | 0 | ctx->pad = 1; |
37 | 0 | ctx->mode = EVP_CIPH_GCM_MODE; |
38 | 0 | ctx->taglen = UNINITIALISED_SIZET; |
39 | 0 | ctx->tls_aad_len = UNINITIALISED_SIZET; |
40 | 0 | ctx->ivlen = (EVP_GCM_TLS_FIXED_IV_LEN + EVP_GCM_TLS_EXPLICIT_IV_LEN); |
41 | 0 | ctx->keylen = keybits / 8; |
42 | 0 | ctx->hw = hw; |
43 | 0 | ctx->libctx = PROV_LIBCTX_OF(provctx); |
44 | 0 | } |
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 | 0 | { |
53 | 0 | PROV_GCM_CTX *ctx = (PROV_GCM_CTX *)vctx; |
54 | |
|
55 | 0 | if (!ossl_prov_is_running()) |
56 | 0 | return 0; |
57 | | |
58 | 0 | ctx->enc = enc; |
59 | |
|
60 | 0 | if (iv != NULL) { |
61 | 0 | 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 | 0 | ctx->ivlen = ivlen; |
66 | 0 | memcpy(ctx->iv, iv, ivlen); |
67 | 0 | ctx->iv_state = IV_STATE_BUFFERED; |
68 | 0 | } |
69 | | |
70 | 0 | if (key != NULL) { |
71 | 0 | if (keylen != ctx->keylen) { |
72 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH); |
73 | 0 | return 0; |
74 | 0 | } |
75 | 0 | if (!ctx->hw->setkey(ctx, key, ctx->keylen)) |
76 | 0 | return 0; |
77 | 0 | ctx->tls_enc_records = 0; |
78 | 0 | } |
79 | 0 | return ossl_gcm_set_ctx_params(ctx, params); |
80 | 0 | } |
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 | 0 | { |
86 | 0 | return gcm_init(vctx, key, keylen, iv, ivlen, params, 1); |
87 | 0 | } |
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 | 0 | { |
93 | 0 | return gcm_init(vctx, key, keylen, iv, ivlen, params, 0); |
94 | 0 | } |
95 | | |
96 | | /* increment counter (64-bit int) by 1 */ |
97 | | static void ctr64_inc(unsigned char *counter) |
98 | 0 | { |
99 | 0 | int n = 8; |
100 | 0 | unsigned char c; |
101 | |
|
102 | 0 | do { |
103 | 0 | --n; |
104 | 0 | c = counter[n]; |
105 | 0 | ++c; |
106 | 0 | counter[n] = c; |
107 | 0 | if (c > 0) |
108 | 0 | return; |
109 | 0 | } while (n > 0); |
110 | 0 | } |
111 | | |
112 | | static int getivgen(PROV_GCM_CTX *ctx, unsigned char *out, size_t olen) |
113 | 0 | { |
114 | 0 | if (!ctx->iv_gen |
115 | 0 | || !ctx->key_set |
116 | 0 | || !ctx->hw->setiv(ctx, ctx->iv, ctx->ivlen)) |
117 | 0 | return 0; |
118 | 0 | if (olen == 0 || olen > ctx->ivlen) |
119 | 0 | olen = ctx->ivlen; |
120 | 0 | 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 | 0 | ctr64_inc(ctx->iv + ctx->ivlen - 8); |
126 | 0 | ctx->iv_state = IV_STATE_COPIED; |
127 | 0 | return 1; |
128 | 0 | } |
129 | | |
130 | | static int setivinv(PROV_GCM_CTX *ctx, unsigned char *in, size_t inl) |
131 | 0 | { |
132 | 0 | if (!ctx->iv_gen |
133 | 0 | || !ctx->key_set |
134 | 0 | || ctx->enc) |
135 | 0 | return 0; |
136 | | |
137 | 0 | memcpy(ctx->iv + ctx->ivlen - inl, in, inl); |
138 | 0 | if (!ctx->hw->setiv(ctx, ctx->iv, ctx->ivlen)) |
139 | 0 | return 0; |
140 | 0 | ctx->iv_state = IV_STATE_COPIED; |
141 | 0 | return 1; |
142 | 0 | } |
143 | | |
144 | | /* Machine generated by util/perl/OpenSSL/paramnames.pm */ |
145 | | #ifndef ossl_cipher_gcm_get_ctx_params_list |
146 | | static const OSSL_PARAM ossl_cipher_gcm_get_ctx_params_list[] = { |
147 | | OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_KEYLEN, NULL), |
148 | | OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_IVLEN, NULL), |
149 | | OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_AEAD_TAGLEN, NULL), |
150 | | OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_IV, NULL, 0), |
151 | | OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_UPDATED_IV, NULL, 0), |
152 | | OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_AEAD_TAG, NULL, 0), |
153 | | OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_AEAD_TLS1_AAD_PAD, NULL), |
154 | | OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_AEAD_TLS1_GET_IV_GEN, NULL, 0), |
155 | | OSSL_PARAM_uint(OSSL_CIPHER_PARAM_AEAD_IV_GENERATED, NULL), |
156 | | OSSL_PARAM_END |
157 | | }; |
158 | | #endif |
159 | | |
160 | | #ifndef ossl_cipher_gcm_get_ctx_params_st |
161 | | struct ossl_cipher_gcm_get_ctx_params_st { |
162 | | OSSL_PARAM *gen; |
163 | | OSSL_PARAM *iv; |
164 | | OSSL_PARAM *ivgen; |
165 | | OSSL_PARAM *ivlen; |
166 | | OSSL_PARAM *keylen; |
167 | | OSSL_PARAM *pad; |
168 | | OSSL_PARAM *tag; |
169 | | OSSL_PARAM *taglen; |
170 | | OSSL_PARAM *updiv; |
171 | | }; |
172 | | #endif |
173 | | |
174 | | #ifndef ossl_cipher_gcm_get_ctx_params_decoder |
175 | | static struct ossl_cipher_gcm_get_ctx_params_st |
176 | 0 | ossl_cipher_gcm_get_ctx_params_decoder(const OSSL_PARAM params[]) { |
177 | 0 | struct ossl_cipher_gcm_get_ctx_params_st r; |
178 | 0 | const OSSL_PARAM *p; |
179 | 0 | const char *s; |
180 | |
|
181 | 0 | memset(&r, 0, sizeof(r)); |
182 | 0 | for (p = params; (s = p->key) != NULL; p++) |
183 | 0 | switch(s[0]) { |
184 | 0 | default: |
185 | 0 | break; |
186 | 0 | case 'i': |
187 | 0 | switch(s[1]) { |
188 | 0 | default: |
189 | 0 | break; |
190 | 0 | case 'v': |
191 | 0 | switch(s[2]) { |
192 | 0 | default: |
193 | 0 | break; |
194 | 0 | case '-': |
195 | 0 | if (ossl_likely(r.gen == NULL && strcmp("generated", s + 3) == 0)) |
196 | 0 | r.gen = (OSSL_PARAM *)p; |
197 | 0 | break; |
198 | 0 | case 'l': |
199 | 0 | if (ossl_likely(r.ivlen == NULL && strcmp("en", s + 3) == 0)) |
200 | 0 | r.ivlen = (OSSL_PARAM *)p; |
201 | 0 | break; |
202 | 0 | case '\0': |
203 | 0 | r.iv = ossl_likely(r.iv == NULL) ? (OSSL_PARAM *)p : r.iv; |
204 | 0 | } |
205 | 0 | } |
206 | 0 | break; |
207 | 0 | case 'k': |
208 | 0 | if (ossl_likely(r.keylen == NULL && strcmp("eylen", s + 1) == 0)) |
209 | 0 | r.keylen = (OSSL_PARAM *)p; |
210 | 0 | break; |
211 | 0 | case 't': |
212 | 0 | switch(s[1]) { |
213 | 0 | default: |
214 | 0 | break; |
215 | 0 | case 'a': |
216 | 0 | switch(s[2]) { |
217 | 0 | default: |
218 | 0 | break; |
219 | 0 | case 'g': |
220 | 0 | switch(s[3]) { |
221 | 0 | default: |
222 | 0 | break; |
223 | 0 | case 'l': |
224 | 0 | if (ossl_likely(r.taglen == NULL && strcmp("en", s + 4) == 0)) |
225 | 0 | r.taglen = (OSSL_PARAM *)p; |
226 | 0 | break; |
227 | 0 | case '\0': |
228 | 0 | r.tag = ossl_likely(r.tag == NULL) ? (OSSL_PARAM *)p : r.tag; |
229 | 0 | } |
230 | 0 | } |
231 | 0 | break; |
232 | 0 | case 'l': |
233 | 0 | switch(s[2]) { |
234 | 0 | default: |
235 | 0 | break; |
236 | 0 | case 's': |
237 | 0 | switch(s[3]) { |
238 | 0 | default: |
239 | 0 | break; |
240 | 0 | case 'a': |
241 | 0 | if (ossl_likely(r.pad == NULL && strcmp("adpad", s + 4) == 0)) |
242 | 0 | r.pad = (OSSL_PARAM *)p; |
243 | 0 | break; |
244 | 0 | case 'i': |
245 | 0 | if (ossl_likely(r.ivgen == NULL && strcmp("vgen", s + 4) == 0)) |
246 | 0 | r.ivgen = (OSSL_PARAM *)p; |
247 | 0 | } |
248 | 0 | } |
249 | 0 | } |
250 | 0 | break; |
251 | 0 | case 'u': |
252 | 0 | if (ossl_likely(r.updiv == NULL && strcmp("pdated-iv", s + 1) == 0)) |
253 | 0 | r.updiv = (OSSL_PARAM *)p; |
254 | 0 | } |
255 | 0 | return r; |
256 | 0 | } |
257 | | #endif |
258 | | /* End of machine generated */ |
259 | | |
260 | | const OSSL_PARAM *ossl_gcm_gettable_ctx_params( |
261 | | ossl_unused void *cctx, ossl_unused void *provctx |
262 | | ) |
263 | 112 | { |
264 | 112 | return ossl_cipher_gcm_get_ctx_params_list; |
265 | 112 | } |
266 | | |
267 | | int ossl_gcm_get_ctx_params(void *vctx, OSSL_PARAM params[]) |
268 | 0 | { |
269 | 0 | PROV_GCM_CTX *ctx = (PROV_GCM_CTX *)vctx; |
270 | 0 | size_t sz; |
271 | 0 | struct ossl_cipher_gcm_get_ctx_params_st p; |
272 | |
|
273 | 0 | p = ossl_cipher_gcm_get_ctx_params_decoder(params); |
274 | |
|
275 | 0 | if (p.ivlen != NULL && !OSSL_PARAM_set_size_t(p.ivlen, ctx->ivlen)) { |
276 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER); |
277 | 0 | return 0; |
278 | 0 | } |
279 | | |
280 | 0 | if (p.keylen != NULL && !OSSL_PARAM_set_size_t(p.keylen, ctx->keylen)) { |
281 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER); |
282 | 0 | return 0; |
283 | 0 | } |
284 | | |
285 | 0 | if (p.taglen != NULL) { |
286 | 0 | size_t taglen = (ctx->taglen != UNINITIALISED_SIZET) ? ctx->taglen : |
287 | 0 | GCM_TAG_MAX_SIZE; |
288 | |
|
289 | 0 | if (!OSSL_PARAM_set_size_t(p.taglen, taglen)) { |
290 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER); |
291 | 0 | return 0; |
292 | 0 | } |
293 | 0 | } |
294 | | |
295 | 0 | if (p.iv != NULL) { |
296 | 0 | if (ctx->iv_state == IV_STATE_UNINITIALISED) |
297 | 0 | return 0; |
298 | 0 | if (ctx->ivlen > p.iv->data_size) { |
299 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH); |
300 | 0 | return 0; |
301 | 0 | } |
302 | 0 | if (!OSSL_PARAM_set_octet_string_or_ptr(p.iv, ctx->iv, ctx->ivlen)) { |
303 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER); |
304 | 0 | return 0; |
305 | 0 | } |
306 | 0 | } |
307 | | |
308 | 0 | if (p.updiv != NULL) { |
309 | 0 | if (ctx->iv_state == IV_STATE_UNINITIALISED) |
310 | 0 | return 0; |
311 | 0 | if (ctx->ivlen > p.updiv->data_size) { |
312 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH); |
313 | 0 | return 0; |
314 | 0 | } |
315 | 0 | if (!OSSL_PARAM_set_octet_string_or_ptr(p.updiv, ctx->iv, ctx->ivlen)) { |
316 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER); |
317 | 0 | return 0; |
318 | 0 | } |
319 | 0 | } |
320 | | |
321 | 0 | if (p.pad != NULL && !OSSL_PARAM_set_size_t(p.pad, ctx->tls_aad_pad_sz)) { |
322 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER); |
323 | 0 | return 0; |
324 | 0 | } |
325 | | |
326 | 0 | if (p.tag != NULL) { |
327 | 0 | sz = p.tag->data_size; |
328 | 0 | if (sz == 0 |
329 | 0 | || sz > EVP_GCM_TLS_TAG_LEN |
330 | 0 | || !ctx->enc |
331 | 0 | || ctx->taglen == UNINITIALISED_SIZET) { |
332 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_TAG); |
333 | 0 | return 0; |
334 | 0 | } |
335 | 0 | if (!OSSL_PARAM_set_octet_string(p.tag, ctx->buf, sz)) { |
336 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER); |
337 | 0 | return 0; |
338 | 0 | } |
339 | 0 | } |
340 | | |
341 | 0 | if (p.ivgen != NULL) |
342 | 0 | if (p.ivgen->data == NULL |
343 | 0 | || p.ivgen->data_type != OSSL_PARAM_OCTET_STRING |
344 | 0 | || !getivgen(ctx, p.ivgen->data, p.ivgen->data_size)) |
345 | 0 | return 0; |
346 | | |
347 | 0 | if (p.gen != NULL && !OSSL_PARAM_set_uint(p.gen, ctx->iv_gen_rand)) |
348 | 0 | return 0; |
349 | | |
350 | 0 | return 1; |
351 | 0 | } |
352 | | |
353 | | /* Machine generated by util/perl/OpenSSL/paramnames.pm */ |
354 | | #ifndef ossl_cipher_gcm_set_ctx_params_list |
355 | | static const OSSL_PARAM ossl_cipher_gcm_set_ctx_params_list[] = { |
356 | | OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_AEAD_IVLEN, NULL), |
357 | | OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_AEAD_TAG, NULL, 0), |
358 | | OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_AEAD_TLS1_AAD, NULL, 0), |
359 | | OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_AEAD_TLS1_IV_FIXED, NULL, 0), |
360 | | OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_AEAD_TLS1_SET_IV_INV, NULL, 0), |
361 | | OSSL_PARAM_END |
362 | | }; |
363 | | #endif |
364 | | |
365 | | #ifndef ossl_cipher_gcm_set_ctx_params_st |
366 | | struct ossl_cipher_gcm_set_ctx_params_st { |
367 | | OSSL_PARAM *aad; |
368 | | OSSL_PARAM *fixed; |
369 | | OSSL_PARAM *inviv; |
370 | | OSSL_PARAM *ivlen; |
371 | | OSSL_PARAM *tag; |
372 | | }; |
373 | | #endif |
374 | | |
375 | | #ifndef ossl_cipher_gcm_set_ctx_params_decoder |
376 | | static struct ossl_cipher_gcm_set_ctx_params_st |
377 | 0 | ossl_cipher_gcm_set_ctx_params_decoder(const OSSL_PARAM params[]) { |
378 | 0 | struct ossl_cipher_gcm_set_ctx_params_st r; |
379 | 0 | const OSSL_PARAM *p; |
380 | 0 | const char *s; |
381 | |
|
382 | 0 | memset(&r, 0, sizeof(r)); |
383 | 0 | for (p = params; (s = p->key) != NULL; p++) |
384 | 0 | switch(s[0]) { |
385 | 0 | default: |
386 | 0 | break; |
387 | 0 | case 'i': |
388 | 0 | if (ossl_likely(r.ivlen == NULL && strcmp("vlen", s + 1) == 0)) |
389 | 0 | r.ivlen = (OSSL_PARAM *)p; |
390 | 0 | break; |
391 | 0 | case 't': |
392 | 0 | switch(s[1]) { |
393 | 0 | default: |
394 | 0 | break; |
395 | 0 | case 'a': |
396 | 0 | if (ossl_likely(r.tag == NULL && strcmp("g", s + 2) == 0)) |
397 | 0 | r.tag = (OSSL_PARAM *)p; |
398 | 0 | break; |
399 | 0 | case 'l': |
400 | 0 | switch(s[2]) { |
401 | 0 | default: |
402 | 0 | break; |
403 | 0 | case 's': |
404 | 0 | switch(s[3]) { |
405 | 0 | default: |
406 | 0 | break; |
407 | 0 | case 'a': |
408 | 0 | if (ossl_likely(r.aad == NULL && strcmp("ad", s + 4) == 0)) |
409 | 0 | r.aad = (OSSL_PARAM *)p; |
410 | 0 | break; |
411 | 0 | case 'i': |
412 | 0 | switch(s[4]) { |
413 | 0 | default: |
414 | 0 | break; |
415 | 0 | case 'v': |
416 | 0 | switch(s[5]) { |
417 | 0 | default: |
418 | 0 | break; |
419 | 0 | case 'f': |
420 | 0 | if (ossl_likely(r.fixed == NULL && strcmp("ixed", s + 6) == 0)) |
421 | 0 | r.fixed = (OSSL_PARAM *)p; |
422 | 0 | break; |
423 | 0 | case 'i': |
424 | 0 | if (ossl_likely(r.inviv == NULL && strcmp("nv", s + 6) == 0)) |
425 | 0 | r.inviv = (OSSL_PARAM *)p; |
426 | 0 | } |
427 | 0 | } |
428 | 0 | } |
429 | 0 | } |
430 | 0 | } |
431 | 0 | } |
432 | 0 | return r; |
433 | 0 | } |
434 | | #endif |
435 | | /* End of machine generated */ |
436 | | |
437 | | const OSSL_PARAM *ossl_gcm_settable_ctx_params( |
438 | | ossl_unused void *cctx, ossl_unused void *provctx |
439 | | ) |
440 | 0 | { |
441 | 0 | return ossl_cipher_gcm_set_ctx_params_list; |
442 | 0 | } |
443 | | |
444 | | int ossl_gcm_set_ctx_params(void *vctx, const OSSL_PARAM params[]) |
445 | 0 | { |
446 | 0 | PROV_GCM_CTX *ctx = (PROV_GCM_CTX *)vctx; |
447 | 0 | size_t sz; |
448 | 0 | void *vp; |
449 | 0 | struct ossl_cipher_gcm_set_ctx_params_st p; |
450 | |
|
451 | 0 | if (ossl_param_is_empty(params)) |
452 | 0 | return 1; |
453 | | |
454 | 0 | p = ossl_cipher_gcm_set_ctx_params_decoder(params); |
455 | |
|
456 | 0 | if (p.tag != NULL) { |
457 | 0 | vp = ctx->buf; |
458 | 0 | if (!OSSL_PARAM_get_octet_string(p.tag, &vp, EVP_GCM_TLS_TAG_LEN, &sz)) { |
459 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER); |
460 | 0 | return 0; |
461 | 0 | } |
462 | 0 | if (sz == 0 || ctx->enc) { |
463 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_TAG); |
464 | 0 | return 0; |
465 | 0 | } |
466 | 0 | ctx->taglen = sz; |
467 | 0 | } |
468 | | |
469 | 0 | if (p.ivlen != NULL) { |
470 | 0 | if (!OSSL_PARAM_get_size_t(p.ivlen, &sz)) { |
471 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER); |
472 | 0 | return 0; |
473 | 0 | } |
474 | 0 | if (sz == 0 || sz > sizeof(ctx->iv)) { |
475 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH); |
476 | 0 | return 0; |
477 | 0 | } |
478 | 0 | if (ctx->ivlen != sz) { |
479 | | /* If the iv was already set or autogenerated, it is invalid. */ |
480 | 0 | if (ctx->iv_state != IV_STATE_UNINITIALISED) |
481 | 0 | ctx->iv_state = IV_STATE_FINISHED; |
482 | 0 | ctx->ivlen = sz; |
483 | 0 | } |
484 | 0 | } |
485 | | |
486 | 0 | if (p.aad != NULL) { |
487 | 0 | if (p.aad->data_type != OSSL_PARAM_OCTET_STRING) { |
488 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER); |
489 | 0 | return 0; |
490 | 0 | } |
491 | 0 | sz = gcm_tls_init(ctx, p.aad->data, p.aad->data_size); |
492 | 0 | if (sz == 0) { |
493 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_AAD); |
494 | 0 | return 0; |
495 | 0 | } |
496 | 0 | ctx->tls_aad_pad_sz = sz; |
497 | 0 | } |
498 | | |
499 | 0 | if (p.fixed != NULL) { |
500 | 0 | if (p.fixed->data_type != OSSL_PARAM_OCTET_STRING) { |
501 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER); |
502 | 0 | return 0; |
503 | 0 | } |
504 | 0 | if (gcm_tls_iv_set_fixed(ctx, p.fixed->data, p.fixed->data_size) == 0) { |
505 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER); |
506 | 0 | return 0; |
507 | 0 | } |
508 | 0 | } |
509 | | |
510 | 0 | if (p.inviv != NULL) |
511 | 0 | if (p.inviv->data == NULL |
512 | 0 | || p.inviv->data_type != OSSL_PARAM_OCTET_STRING |
513 | 0 | || !setivinv(ctx, p.inviv->data, p.inviv->data_size)) |
514 | 0 | return 0; |
515 | | |
516 | 0 | return 1; |
517 | 0 | } |
518 | | |
519 | | int ossl_gcm_stream_update(void *vctx, unsigned char *out, size_t *outl, |
520 | | size_t outsize, const unsigned char *in, size_t inl) |
521 | 0 | { |
522 | 0 | PROV_GCM_CTX *ctx = (PROV_GCM_CTX *)vctx; |
523 | |
|
524 | 0 | if (inl == 0) { |
525 | 0 | *outl = 0; |
526 | 0 | return 1; |
527 | 0 | } |
528 | | |
529 | 0 | if (outsize < inl) { |
530 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL); |
531 | 0 | return 0; |
532 | 0 | } |
533 | | |
534 | 0 | if (gcm_cipher_internal(ctx, out, outl, in, inl) <= 0) { |
535 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED); |
536 | 0 | return 0; |
537 | 0 | } |
538 | 0 | return 1; |
539 | 0 | } |
540 | | |
541 | | int ossl_gcm_stream_final(void *vctx, unsigned char *out, size_t *outl, |
542 | | size_t outsize) |
543 | 0 | { |
544 | 0 | PROV_GCM_CTX *ctx = (PROV_GCM_CTX *)vctx; |
545 | 0 | int i; |
546 | |
|
547 | 0 | if (!ossl_prov_is_running()) |
548 | 0 | return 0; |
549 | | |
550 | 0 | i = gcm_cipher_internal(ctx, out, outl, NULL, 0); |
551 | 0 | if (i <= 0) |
552 | 0 | return 0; |
553 | | |
554 | 0 | *outl = 0; |
555 | 0 | return 1; |
556 | 0 | } |
557 | | |
558 | | int ossl_gcm_cipher(void *vctx, |
559 | | unsigned char *out, size_t *outl, size_t outsize, |
560 | | const unsigned char *in, size_t inl) |
561 | 0 | { |
562 | 0 | PROV_GCM_CTX *ctx = (PROV_GCM_CTX *)vctx; |
563 | |
|
564 | 0 | if (!ossl_prov_is_running()) |
565 | 0 | return 0; |
566 | | |
567 | 0 | if (outsize < inl) { |
568 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL); |
569 | 0 | return 0; |
570 | 0 | } |
571 | | |
572 | 0 | if (gcm_cipher_internal(ctx, out, outl, in, inl) <= 0) |
573 | 0 | return 0; |
574 | | |
575 | 0 | *outl = inl; |
576 | 0 | return 1; |
577 | 0 | } |
578 | | |
579 | | /* |
580 | | * See SP800-38D (GCM) Section 8 "Uniqueness requirement on IVS and keys" |
581 | | * |
582 | | * See also 8.2.2 RBG-based construction. |
583 | | * Random construction consists of a free field (which can be NULL) and a |
584 | | * random field which will use a DRBG that can return at least 96 bits of |
585 | | * entropy strength. (The DRBG must be seeded by the FIPS module). |
586 | | */ |
587 | | static int gcm_iv_generate(PROV_GCM_CTX *ctx, int offset) |
588 | 0 | { |
589 | 0 | int sz = (int)(ctx->ivlen - offset); |
590 | | |
591 | | /* Must be at least 96 bits */ |
592 | 0 | if (sz <= 0 || ctx->ivlen < GCM_IV_DEFAULT_SIZE) |
593 | 0 | return 0; |
594 | | |
595 | | /* Use DRBG to generate random iv */ |
596 | 0 | if (RAND_bytes_ex(ctx->libctx, ctx->iv + offset, sz, 0) <= 0) |
597 | 0 | return 0; |
598 | 0 | ctx->iv_state = IV_STATE_BUFFERED; |
599 | 0 | ctx->iv_gen_rand = 1; |
600 | 0 | return 1; |
601 | 0 | } |
602 | | |
603 | | static int gcm_cipher_internal(PROV_GCM_CTX *ctx, unsigned char *out, |
604 | | size_t *padlen, const unsigned char *in, |
605 | | size_t len) |
606 | 0 | { |
607 | 0 | size_t olen = 0; |
608 | 0 | int rv = 0; |
609 | 0 | const PROV_GCM_HW *hw = ctx->hw; |
610 | |
|
611 | 0 | if (ctx->tls_aad_len != UNINITIALISED_SIZET) |
612 | 0 | return gcm_tls_cipher(ctx, out, padlen, in, len); |
613 | | |
614 | 0 | if (!ctx->key_set || ctx->iv_state == IV_STATE_FINISHED) |
615 | 0 | goto err; |
616 | | |
617 | | /* |
618 | | * FIPS requires generation of AES-GCM IV's inside the FIPS module. |
619 | | * The IV can still be set externally (the security policy will state that |
620 | | * this is not FIPS compliant). There are some applications |
621 | | * where setting the IV externally is the only option available. |
622 | | */ |
623 | 0 | if (ctx->iv_state == IV_STATE_UNINITIALISED) { |
624 | 0 | if (!ctx->enc || !gcm_iv_generate(ctx, 0)) |
625 | 0 | goto err; |
626 | 0 | } |
627 | | |
628 | 0 | if (ctx->iv_state == IV_STATE_BUFFERED) { |
629 | 0 | if (!hw->setiv(ctx, ctx->iv, ctx->ivlen)) |
630 | 0 | goto err; |
631 | 0 | ctx->iv_state = IV_STATE_COPIED; |
632 | 0 | } |
633 | | |
634 | 0 | if (in != NULL) { |
635 | | /* The input is AAD if out is NULL */ |
636 | 0 | if (out == NULL) { |
637 | 0 | if (!hw->aadupdate(ctx, in, len)) |
638 | 0 | goto err; |
639 | 0 | } else { |
640 | | /* The input is ciphertext OR plaintext */ |
641 | 0 | if (!hw->cipherupdate(ctx, in, len, out)) |
642 | 0 | goto err; |
643 | 0 | } |
644 | 0 | } else { |
645 | | /* The tag must be set before actually decrypting data */ |
646 | 0 | if (!ctx->enc && ctx->taglen == UNINITIALISED_SIZET) |
647 | 0 | goto err; |
648 | 0 | if (!hw->cipherfinal(ctx, ctx->buf)) |
649 | 0 | goto err; |
650 | 0 | ctx->iv_state = IV_STATE_FINISHED; /* Don't reuse the IV */ |
651 | 0 | goto finish; |
652 | 0 | } |
653 | 0 | olen = len; |
654 | 0 | finish: |
655 | 0 | rv = 1; |
656 | 0 | err: |
657 | 0 | *padlen = olen; |
658 | 0 | return rv; |
659 | 0 | } |
660 | | |
661 | | static int gcm_tls_init(PROV_GCM_CTX *dat, unsigned char *aad, size_t aad_len) |
662 | 0 | { |
663 | 0 | unsigned char *buf; |
664 | 0 | size_t len; |
665 | |
|
666 | 0 | if (!ossl_prov_is_running() || aad_len != EVP_AEAD_TLS1_AAD_LEN) |
667 | 0 | return 0; |
668 | | |
669 | | /* Save the aad for later use. */ |
670 | 0 | buf = dat->buf; |
671 | 0 | memcpy(buf, aad, aad_len); |
672 | 0 | dat->tls_aad_len = aad_len; |
673 | |
|
674 | 0 | len = buf[aad_len - 2] << 8 | buf[aad_len - 1]; |
675 | | /* Correct length for explicit iv. */ |
676 | 0 | if (len < EVP_GCM_TLS_EXPLICIT_IV_LEN) |
677 | 0 | return 0; |
678 | 0 | len -= EVP_GCM_TLS_EXPLICIT_IV_LEN; |
679 | | |
680 | | /* If decrypting correct for tag too. */ |
681 | 0 | if (!dat->enc) { |
682 | 0 | if (len < EVP_GCM_TLS_TAG_LEN) |
683 | 0 | return 0; |
684 | 0 | len -= EVP_GCM_TLS_TAG_LEN; |
685 | 0 | } |
686 | 0 | buf[aad_len - 2] = (unsigned char)(len >> 8); |
687 | 0 | buf[aad_len - 1] = (unsigned char)(len & 0xff); |
688 | | /* Extra padding: tag appended to record. */ |
689 | 0 | return EVP_GCM_TLS_TAG_LEN; |
690 | 0 | } |
691 | | |
692 | | static int gcm_tls_iv_set_fixed(PROV_GCM_CTX *ctx, unsigned char *iv, |
693 | | size_t len) |
694 | 0 | { |
695 | | /* Special case: -1 length restores whole IV */ |
696 | 0 | if (len == (size_t)-1) { |
697 | 0 | memcpy(ctx->iv, iv, ctx->ivlen); |
698 | 0 | ctx->iv_gen = 1; |
699 | 0 | ctx->iv_state = IV_STATE_BUFFERED; |
700 | 0 | return 1; |
701 | 0 | } |
702 | | /* Fixed field must be at least 4 bytes and invocation field at least 8 */ |
703 | 0 | if ((len < EVP_GCM_TLS_FIXED_IV_LEN) |
704 | 0 | || (ctx->ivlen - (int)len) < EVP_GCM_TLS_EXPLICIT_IV_LEN) |
705 | 0 | return 0; |
706 | 0 | if (len > 0) |
707 | 0 | memcpy(ctx->iv, iv, len); |
708 | 0 | if (ctx->enc) { |
709 | 0 | if (RAND_bytes_ex(ctx->libctx, ctx->iv + len, ctx->ivlen - len, 0) <= 0) |
710 | 0 | return 0; |
711 | 0 | ctx->iv_gen_rand = 1; |
712 | 0 | } |
713 | 0 | ctx->iv_gen = 1; |
714 | 0 | ctx->iv_state = IV_STATE_BUFFERED; |
715 | 0 | return 1; |
716 | 0 | } |
717 | | |
718 | | /* |
719 | | * Handle TLS GCM packet format. This consists of the last portion of the IV |
720 | | * followed by the payload and finally the tag. On encrypt generate IV, |
721 | | * encrypt payload and write the tag. On verify retrieve IV, decrypt payload |
722 | | * and verify tag. |
723 | | */ |
724 | | static int gcm_tls_cipher(PROV_GCM_CTX *ctx, unsigned char *out, size_t *padlen, |
725 | | const unsigned char *in, size_t len) |
726 | 0 | { |
727 | 0 | int rv = 0; |
728 | 0 | size_t arg = EVP_GCM_TLS_EXPLICIT_IV_LEN; |
729 | 0 | size_t plen = 0; |
730 | 0 | unsigned char *tag = NULL; |
731 | |
|
732 | 0 | if (!ossl_prov_is_running() || !ctx->key_set) |
733 | 0 | goto err; |
734 | | |
735 | | /* Encrypt/decrypt must be performed in place */ |
736 | 0 | if (out != in || len < (EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN)) |
737 | 0 | goto err; |
738 | | |
739 | | /* |
740 | | * Check for too many keys as per FIPS 140-2 IG A.5 "Key/IV Pair Uniqueness |
741 | | * Requirements from SP 800-38D". The requirements is for one party to the |
742 | | * communication to fail after 2^64 - 1 keys. We do this on the encrypting |
743 | | * side only. |
744 | | */ |
745 | 0 | if (ctx->enc && ++ctx->tls_enc_records == 0) { |
746 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_TOO_MANY_RECORDS); |
747 | 0 | goto err; |
748 | 0 | } |
749 | | |
750 | | /* |
751 | | * Set IV from start of buffer or generate IV and write to start of |
752 | | * buffer. |
753 | | */ |
754 | 0 | if (ctx->enc) { |
755 | 0 | if (!getivgen(ctx, out, arg)) |
756 | 0 | goto err; |
757 | 0 | } else { |
758 | 0 | if (!setivinv(ctx, out, arg)) |
759 | 0 | goto err; |
760 | 0 | } |
761 | | |
762 | | /* Fix buffer and length to point to payload */ |
763 | 0 | in += EVP_GCM_TLS_EXPLICIT_IV_LEN; |
764 | 0 | out += EVP_GCM_TLS_EXPLICIT_IV_LEN; |
765 | 0 | len -= EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN; |
766 | |
|
767 | 0 | tag = ctx->enc ? out + len : (unsigned char *)in + len; |
768 | 0 | if (!ctx->hw->oneshot(ctx, ctx->buf, ctx->tls_aad_len, in, len, out, tag, |
769 | 0 | EVP_GCM_TLS_TAG_LEN)) { |
770 | 0 | if (!ctx->enc) |
771 | 0 | OPENSSL_cleanse(out, len); |
772 | 0 | goto err; |
773 | 0 | } |
774 | 0 | if (ctx->enc) |
775 | 0 | plen = len + EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN; |
776 | 0 | else |
777 | 0 | plen = len; |
778 | |
|
779 | 0 | rv = 1; |
780 | 0 | err: |
781 | 0 | ctx->iv_state = IV_STATE_FINISHED; |
782 | 0 | ctx->tls_aad_len = UNINITIALISED_SIZET; |
783 | 0 | *padlen = plen; |
784 | 0 | return rv; |
785 | 0 | } |