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