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