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