xref: /freebsd-src/crypto/openssl/providers/implementations/ciphers/ciphercommon.c (revision e0c4386e7e71d93b0edc0c8fa156263fc4a8b0b6)
1b077aed3SPierre Pronchery /*
2b077aed3SPierre Pronchery  * Copyright 2019-2021 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 /*
11b077aed3SPierre Pronchery  * Generic dispatch table functions for ciphers.
12b077aed3SPierre Pronchery  */
13b077aed3SPierre Pronchery 
14b077aed3SPierre Pronchery /* For SSL3_VERSION */
15b077aed3SPierre Pronchery #include <openssl/prov_ssl.h>
16b077aed3SPierre Pronchery #include <openssl/proverr.h>
17b077aed3SPierre Pronchery #include "ciphercommon_local.h"
18b077aed3SPierre Pronchery #include "prov/provider_ctx.h"
19b077aed3SPierre Pronchery #include "prov/providercommon.h"
20b077aed3SPierre Pronchery 
21b077aed3SPierre Pronchery /*-
22b077aed3SPierre Pronchery  * Generic cipher functions for OSSL_PARAM gettables and settables
23b077aed3SPierre Pronchery  */
24b077aed3SPierre Pronchery static const OSSL_PARAM cipher_known_gettable_params[] = {
25b077aed3SPierre Pronchery     OSSL_PARAM_uint(OSSL_CIPHER_PARAM_MODE, NULL),
26b077aed3SPierre Pronchery     OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_KEYLEN, NULL),
27b077aed3SPierre Pronchery     OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_IVLEN, NULL),
28b077aed3SPierre Pronchery     OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_BLOCK_SIZE, NULL),
29b077aed3SPierre Pronchery     OSSL_PARAM_int(OSSL_CIPHER_PARAM_AEAD, NULL),
30b077aed3SPierre Pronchery     OSSL_PARAM_int(OSSL_CIPHER_PARAM_CUSTOM_IV, NULL),
31b077aed3SPierre Pronchery     OSSL_PARAM_int(OSSL_CIPHER_PARAM_CTS, NULL),
32b077aed3SPierre Pronchery     OSSL_PARAM_int(OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK, NULL),
33b077aed3SPierre Pronchery     OSSL_PARAM_int(OSSL_CIPHER_PARAM_HAS_RAND_KEY, NULL),
34b077aed3SPierre Pronchery     OSSL_PARAM_END
35b077aed3SPierre Pronchery };
ossl_cipher_generic_gettable_params(ossl_unused void * provctx)36b077aed3SPierre Pronchery const OSSL_PARAM *ossl_cipher_generic_gettable_params(ossl_unused void *provctx)
37b077aed3SPierre Pronchery {
38b077aed3SPierre Pronchery     return cipher_known_gettable_params;
39b077aed3SPierre Pronchery }
40b077aed3SPierre Pronchery 
ossl_cipher_generic_get_params(OSSL_PARAM params[],unsigned int md,uint64_t flags,size_t kbits,size_t blkbits,size_t ivbits)41b077aed3SPierre Pronchery int ossl_cipher_generic_get_params(OSSL_PARAM params[], unsigned int md,
42b077aed3SPierre Pronchery                                    uint64_t flags,
43b077aed3SPierre Pronchery                                    size_t kbits, size_t blkbits, size_t ivbits)
44b077aed3SPierre Pronchery {
45b077aed3SPierre Pronchery     OSSL_PARAM *p;
46b077aed3SPierre Pronchery 
47b077aed3SPierre Pronchery     p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_MODE);
48b077aed3SPierre Pronchery     if (p != NULL && !OSSL_PARAM_set_uint(p, md)) {
49b077aed3SPierre Pronchery         ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
50b077aed3SPierre Pronchery         return 0;
51b077aed3SPierre Pronchery     }
52b077aed3SPierre Pronchery     p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_AEAD);
53b077aed3SPierre Pronchery     if (p != NULL
54b077aed3SPierre Pronchery         && !OSSL_PARAM_set_int(p, (flags & PROV_CIPHER_FLAG_AEAD) != 0)) {
55b077aed3SPierre Pronchery         ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
56b077aed3SPierre Pronchery         return 0;
57b077aed3SPierre Pronchery     }
58b077aed3SPierre Pronchery     p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_CUSTOM_IV);
59b077aed3SPierre Pronchery     if (p != NULL
60b077aed3SPierre Pronchery         && !OSSL_PARAM_set_int(p, (flags & PROV_CIPHER_FLAG_CUSTOM_IV) != 0)) {
61b077aed3SPierre Pronchery         ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
62b077aed3SPierre Pronchery         return 0;
63b077aed3SPierre Pronchery     }
64b077aed3SPierre Pronchery     p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_CTS);
65b077aed3SPierre Pronchery     if (p != NULL
66b077aed3SPierre Pronchery         && !OSSL_PARAM_set_int(p, (flags & PROV_CIPHER_FLAG_CTS) != 0)) {
67b077aed3SPierre Pronchery         ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
68b077aed3SPierre Pronchery         return 0;
69b077aed3SPierre Pronchery     }
70b077aed3SPierre Pronchery     p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK);
71b077aed3SPierre Pronchery     if (p != NULL
72b077aed3SPierre Pronchery         && !OSSL_PARAM_set_int(p, (flags & PROV_CIPHER_FLAG_TLS1_MULTIBLOCK) != 0)) {
73b077aed3SPierre Pronchery         ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
74b077aed3SPierre Pronchery         return 0;
75b077aed3SPierre Pronchery     }
76b077aed3SPierre Pronchery     p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_HAS_RAND_KEY);
77b077aed3SPierre Pronchery     if (p != NULL
78b077aed3SPierre Pronchery         && !OSSL_PARAM_set_int(p, (flags & PROV_CIPHER_FLAG_RAND_KEY) != 0)) {
79b077aed3SPierre Pronchery         ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
80b077aed3SPierre Pronchery         return 0;
81b077aed3SPierre Pronchery     }
82b077aed3SPierre Pronchery     p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_KEYLEN);
83b077aed3SPierre Pronchery     if (p != NULL && !OSSL_PARAM_set_size_t(p, kbits / 8)) {
84b077aed3SPierre Pronchery         ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
85b077aed3SPierre Pronchery         return 0;
86b077aed3SPierre Pronchery     }
87b077aed3SPierre Pronchery     p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_BLOCK_SIZE);
88b077aed3SPierre Pronchery     if (p != NULL && !OSSL_PARAM_set_size_t(p, blkbits / 8)) {
89b077aed3SPierre Pronchery         ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
90b077aed3SPierre Pronchery         return 0;
91b077aed3SPierre Pronchery     }
92b077aed3SPierre Pronchery     p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_IVLEN);
93b077aed3SPierre Pronchery     if (p != NULL && !OSSL_PARAM_set_size_t(p, ivbits / 8)) {
94b077aed3SPierre Pronchery         ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
95b077aed3SPierre Pronchery         return 0;
96b077aed3SPierre Pronchery     }
97b077aed3SPierre Pronchery     return 1;
98b077aed3SPierre Pronchery }
99b077aed3SPierre Pronchery 
CIPHER_DEFAULT_GETTABLE_CTX_PARAMS_START(ossl_cipher_generic)100b077aed3SPierre Pronchery CIPHER_DEFAULT_GETTABLE_CTX_PARAMS_START(ossl_cipher_generic)
101b077aed3SPierre Pronchery { OSSL_CIPHER_PARAM_TLS_MAC, OSSL_PARAM_OCTET_PTR, NULL, 0, OSSL_PARAM_UNMODIFIED },
102b077aed3SPierre Pronchery CIPHER_DEFAULT_GETTABLE_CTX_PARAMS_END(ossl_cipher_generic)
103b077aed3SPierre Pronchery 
104b077aed3SPierre Pronchery CIPHER_DEFAULT_SETTABLE_CTX_PARAMS_START(ossl_cipher_generic)
105b077aed3SPierre Pronchery OSSL_PARAM_uint(OSSL_CIPHER_PARAM_USE_BITS, NULL),
106b077aed3SPierre Pronchery OSSL_PARAM_uint(OSSL_CIPHER_PARAM_TLS_VERSION, NULL),
107b077aed3SPierre Pronchery OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_TLS_MAC_SIZE, NULL),
108b077aed3SPierre Pronchery CIPHER_DEFAULT_SETTABLE_CTX_PARAMS_END(ossl_cipher_generic)
109b077aed3SPierre Pronchery 
110b077aed3SPierre Pronchery /*
111b077aed3SPierre Pronchery  * Variable key length cipher functions for OSSL_PARAM settables
112b077aed3SPierre Pronchery  */
113b077aed3SPierre Pronchery int ossl_cipher_var_keylen_set_ctx_params(void *vctx, const OSSL_PARAM params[])
114b077aed3SPierre Pronchery {
115b077aed3SPierre Pronchery     PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
116b077aed3SPierre Pronchery     const OSSL_PARAM *p;
117b077aed3SPierre Pronchery 
118b077aed3SPierre Pronchery     if (params == NULL)
119b077aed3SPierre Pronchery         return 1;
120b077aed3SPierre Pronchery 
121b077aed3SPierre Pronchery     if (!ossl_cipher_generic_set_ctx_params(vctx, params))
122b077aed3SPierre Pronchery         return 0;
123b077aed3SPierre Pronchery     p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_KEYLEN);
124b077aed3SPierre Pronchery     if (p != NULL) {
125b077aed3SPierre Pronchery         size_t keylen;
126b077aed3SPierre Pronchery 
127b077aed3SPierre Pronchery         if (!OSSL_PARAM_get_size_t(p, &keylen)) {
128b077aed3SPierre Pronchery             ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
129b077aed3SPierre Pronchery             return 0;
130b077aed3SPierre Pronchery         }
131*e0c4386eSCy Schubert         if (ctx->keylen != keylen) {
132b077aed3SPierre Pronchery             ctx->keylen = keylen;
133*e0c4386eSCy Schubert             ctx->key_set = 0;
134*e0c4386eSCy Schubert         }
135b077aed3SPierre Pronchery     }
136b077aed3SPierre Pronchery     return 1;
137b077aed3SPierre Pronchery }
138b077aed3SPierre Pronchery 
139b077aed3SPierre Pronchery CIPHER_DEFAULT_SETTABLE_CTX_PARAMS_START(ossl_cipher_var_keylen)
140b077aed3SPierre Pronchery OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_KEYLEN, NULL),
141b077aed3SPierre Pronchery CIPHER_DEFAULT_SETTABLE_CTX_PARAMS_END(ossl_cipher_var_keylen)
142b077aed3SPierre Pronchery 
143b077aed3SPierre Pronchery /*-
144b077aed3SPierre Pronchery  * AEAD cipher functions for OSSL_PARAM gettables and settables
145b077aed3SPierre Pronchery  */
146b077aed3SPierre Pronchery static const OSSL_PARAM cipher_aead_known_gettable_ctx_params[] = {
147b077aed3SPierre Pronchery     OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_KEYLEN, NULL),
148b077aed3SPierre Pronchery     OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_IVLEN, NULL),
149b077aed3SPierre Pronchery     OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_AEAD_TAGLEN, NULL),
150b077aed3SPierre Pronchery     OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_IV, NULL, 0),
151b077aed3SPierre Pronchery     OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_UPDATED_IV, NULL, 0),
152b077aed3SPierre Pronchery     OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_AEAD_TAG, NULL, 0),
153b077aed3SPierre Pronchery     OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_AEAD_TLS1_AAD_PAD, NULL),
154b077aed3SPierre Pronchery     OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_AEAD_TLS1_GET_IV_GEN, NULL, 0),
155b077aed3SPierre Pronchery     OSSL_PARAM_END
156b077aed3SPierre Pronchery };
ossl_cipher_aead_gettable_ctx_params(ossl_unused void * cctx,ossl_unused void * provctx)157b077aed3SPierre Pronchery const OSSL_PARAM *ossl_cipher_aead_gettable_ctx_params(
158b077aed3SPierre Pronchery         ossl_unused void *cctx, ossl_unused void *provctx
159b077aed3SPierre Pronchery     )
160b077aed3SPierre Pronchery {
161b077aed3SPierre Pronchery     return cipher_aead_known_gettable_ctx_params;
162b077aed3SPierre Pronchery }
163b077aed3SPierre Pronchery 
164b077aed3SPierre Pronchery static const OSSL_PARAM cipher_aead_known_settable_ctx_params[] = {
165b077aed3SPierre Pronchery     OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_AEAD_IVLEN, NULL),
166b077aed3SPierre Pronchery     OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_AEAD_TAG, NULL, 0),
167b077aed3SPierre Pronchery     OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_AEAD_TLS1_AAD, NULL, 0),
168b077aed3SPierre Pronchery     OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_AEAD_TLS1_IV_FIXED, NULL, 0),
169b077aed3SPierre Pronchery     OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_AEAD_TLS1_SET_IV_INV, NULL, 0),
170b077aed3SPierre Pronchery     OSSL_PARAM_END
171b077aed3SPierre Pronchery };
ossl_cipher_aead_settable_ctx_params(ossl_unused void * cctx,ossl_unused void * provctx)172b077aed3SPierre Pronchery const OSSL_PARAM *ossl_cipher_aead_settable_ctx_params(
173b077aed3SPierre Pronchery         ossl_unused void *cctx, ossl_unused void *provctx
174b077aed3SPierre Pronchery     )
175b077aed3SPierre Pronchery {
176b077aed3SPierre Pronchery     return cipher_aead_known_settable_ctx_params;
177b077aed3SPierre Pronchery }
178b077aed3SPierre Pronchery 
ossl_cipher_generic_reset_ctx(PROV_CIPHER_CTX * ctx)179b077aed3SPierre Pronchery void ossl_cipher_generic_reset_ctx(PROV_CIPHER_CTX *ctx)
180b077aed3SPierre Pronchery {
181b077aed3SPierre Pronchery     if (ctx != NULL && ctx->alloced) {
182b077aed3SPierre Pronchery         OPENSSL_free(ctx->tlsmac);
183b077aed3SPierre Pronchery         ctx->alloced = 0;
184b077aed3SPierre Pronchery         ctx->tlsmac = NULL;
185b077aed3SPierre Pronchery     }
186b077aed3SPierre Pronchery }
187b077aed3SPierre Pronchery 
cipher_generic_init_internal(PROV_CIPHER_CTX * ctx,const unsigned char * key,size_t keylen,const unsigned char * iv,size_t ivlen,const OSSL_PARAM params[],int enc)188b077aed3SPierre Pronchery static int cipher_generic_init_internal(PROV_CIPHER_CTX *ctx,
189b077aed3SPierre Pronchery                                         const unsigned char *key, size_t keylen,
190b077aed3SPierre Pronchery                                         const unsigned char *iv, size_t ivlen,
191b077aed3SPierre Pronchery                                         const OSSL_PARAM params[], int enc)
192b077aed3SPierre Pronchery {
193b077aed3SPierre Pronchery     ctx->num = 0;
194b077aed3SPierre Pronchery     ctx->bufsz = 0;
195b077aed3SPierre Pronchery     ctx->updated = 0;
196b077aed3SPierre Pronchery     ctx->enc = enc ? 1 : 0;
197b077aed3SPierre Pronchery 
198b077aed3SPierre Pronchery     if (!ossl_prov_is_running())
199b077aed3SPierre Pronchery         return 0;
200b077aed3SPierre Pronchery 
201b077aed3SPierre Pronchery     if (iv != NULL && ctx->mode != EVP_CIPH_ECB_MODE) {
202b077aed3SPierre Pronchery         if (!ossl_cipher_generic_initiv(ctx, iv, ivlen))
203b077aed3SPierre Pronchery             return 0;
204b077aed3SPierre Pronchery     }
205b077aed3SPierre Pronchery     if (iv == NULL && ctx->iv_set
206b077aed3SPierre Pronchery         && (ctx->mode == EVP_CIPH_CBC_MODE
207b077aed3SPierre Pronchery             || ctx->mode == EVP_CIPH_CFB_MODE
208b077aed3SPierre Pronchery             || ctx->mode == EVP_CIPH_OFB_MODE))
209b077aed3SPierre Pronchery         /* reset IV for these modes to keep compatibility with 1.1.1 */
210b077aed3SPierre Pronchery         memcpy(ctx->iv, ctx->oiv, ctx->ivlen);
211b077aed3SPierre Pronchery 
212b077aed3SPierre Pronchery     if (key != NULL) {
213b077aed3SPierre Pronchery         if (ctx->variable_keylength == 0) {
214b077aed3SPierre Pronchery             if (keylen != ctx->keylen) {
215b077aed3SPierre Pronchery                 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
216b077aed3SPierre Pronchery                 return 0;
217b077aed3SPierre Pronchery             }
218b077aed3SPierre Pronchery         } else {
219b077aed3SPierre Pronchery             ctx->keylen = keylen;
220b077aed3SPierre Pronchery         }
221b077aed3SPierre Pronchery         if (!ctx->hw->init(ctx, key, ctx->keylen))
222b077aed3SPierre Pronchery             return 0;
223*e0c4386eSCy Schubert         ctx->key_set = 1;
224b077aed3SPierre Pronchery     }
225b077aed3SPierre Pronchery     return ossl_cipher_generic_set_ctx_params(ctx, params);
226b077aed3SPierre Pronchery }
227b077aed3SPierre Pronchery 
ossl_cipher_generic_einit(void * vctx,const unsigned char * key,size_t keylen,const unsigned char * iv,size_t ivlen,const OSSL_PARAM params[])228b077aed3SPierre Pronchery int ossl_cipher_generic_einit(void *vctx, const unsigned char *key,
229b077aed3SPierre Pronchery                               size_t keylen, const unsigned char *iv,
230b077aed3SPierre Pronchery                               size_t ivlen, const OSSL_PARAM params[])
231b077aed3SPierre Pronchery {
232b077aed3SPierre Pronchery     return cipher_generic_init_internal((PROV_CIPHER_CTX *)vctx, key, keylen,
233b077aed3SPierre Pronchery                                         iv, ivlen, params, 1);
234b077aed3SPierre Pronchery }
235b077aed3SPierre Pronchery 
ossl_cipher_generic_dinit(void * vctx,const unsigned char * key,size_t keylen,const unsigned char * iv,size_t ivlen,const OSSL_PARAM params[])236b077aed3SPierre Pronchery int ossl_cipher_generic_dinit(void *vctx, const unsigned char *key,
237b077aed3SPierre Pronchery                               size_t keylen, const unsigned char *iv,
238b077aed3SPierre Pronchery                               size_t ivlen, const OSSL_PARAM params[])
239b077aed3SPierre Pronchery {
240b077aed3SPierre Pronchery     return cipher_generic_init_internal((PROV_CIPHER_CTX *)vctx, key, keylen,
241b077aed3SPierre Pronchery                                         iv, ivlen, params, 0);
242b077aed3SPierre Pronchery }
243b077aed3SPierre Pronchery 
244b077aed3SPierre Pronchery /* Max padding including padding length byte */
245b077aed3SPierre Pronchery #define MAX_PADDING 256
246b077aed3SPierre Pronchery 
ossl_cipher_generic_block_update(void * vctx,unsigned char * out,size_t * outl,size_t outsize,const unsigned char * in,size_t inl)247b077aed3SPierre Pronchery int ossl_cipher_generic_block_update(void *vctx, unsigned char *out,
248b077aed3SPierre Pronchery                                      size_t *outl, size_t outsize,
249b077aed3SPierre Pronchery                                      const unsigned char *in, size_t inl)
250b077aed3SPierre Pronchery {
251b077aed3SPierre Pronchery     size_t outlint = 0;
252b077aed3SPierre Pronchery     PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
253b077aed3SPierre Pronchery     size_t blksz = ctx->blocksize;
254b077aed3SPierre Pronchery     size_t nextblocks;
255b077aed3SPierre Pronchery 
256*e0c4386eSCy Schubert     if (!ctx->key_set) {
257*e0c4386eSCy Schubert         ERR_raise(ERR_LIB_PROV, PROV_R_NO_KEY_SET);
258*e0c4386eSCy Schubert         return 0;
259*e0c4386eSCy Schubert     }
260*e0c4386eSCy Schubert 
261b077aed3SPierre Pronchery     if (ctx->tlsversion > 0) {
262b077aed3SPierre Pronchery         /*
263b077aed3SPierre Pronchery          * Each update call corresponds to a TLS record and is individually
264b077aed3SPierre Pronchery          * padded
265b077aed3SPierre Pronchery          */
266b077aed3SPierre Pronchery 
267b077aed3SPierre Pronchery         /* Sanity check inputs */
268b077aed3SPierre Pronchery         if (in == NULL
269b077aed3SPierre Pronchery                 || in != out
270b077aed3SPierre Pronchery                 || outsize < inl
271b077aed3SPierre Pronchery                 || !ctx->pad) {
272b077aed3SPierre Pronchery             ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);
273b077aed3SPierre Pronchery             return 0;
274b077aed3SPierre Pronchery         }
275b077aed3SPierre Pronchery 
276b077aed3SPierre Pronchery         if (ctx->enc) {
277b077aed3SPierre Pronchery             unsigned char padval;
278b077aed3SPierre Pronchery             size_t padnum, loop;
279b077aed3SPierre Pronchery 
280b077aed3SPierre Pronchery             /* Add padding */
281b077aed3SPierre Pronchery 
282b077aed3SPierre Pronchery             padnum = blksz - (inl % blksz);
283b077aed3SPierre Pronchery 
284b077aed3SPierre Pronchery             if (outsize < inl + padnum) {
285b077aed3SPierre Pronchery                 ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);
286b077aed3SPierre Pronchery                 return 0;
287b077aed3SPierre Pronchery             }
288b077aed3SPierre Pronchery 
289b077aed3SPierre Pronchery             if (padnum > MAX_PADDING) {
290b077aed3SPierre Pronchery                 ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);
291b077aed3SPierre Pronchery                 return 0;
292b077aed3SPierre Pronchery             }
293b077aed3SPierre Pronchery             padval = (unsigned char)(padnum - 1);
294b077aed3SPierre Pronchery             if (ctx->tlsversion == SSL3_VERSION) {
295b077aed3SPierre Pronchery                 if (padnum > 1)
296b077aed3SPierre Pronchery                     memset(out + inl, 0, padnum - 1);
297b077aed3SPierre Pronchery                 *(out + inl + padnum - 1) = padval;
298b077aed3SPierre Pronchery             } else {
299b077aed3SPierre Pronchery                 /* we need to add 'padnum' padding bytes of value padval */
300b077aed3SPierre Pronchery                 for (loop = inl; loop < inl + padnum; loop++)
301b077aed3SPierre Pronchery                     out[loop] = padval;
302b077aed3SPierre Pronchery             }
303b077aed3SPierre Pronchery             inl += padnum;
304b077aed3SPierre Pronchery         }
305b077aed3SPierre Pronchery 
306b077aed3SPierre Pronchery         if ((inl % blksz) != 0) {
307b077aed3SPierre Pronchery             ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);
308b077aed3SPierre Pronchery             return 0;
309b077aed3SPierre Pronchery         }
310b077aed3SPierre Pronchery 
311b077aed3SPierre Pronchery 
312b077aed3SPierre Pronchery         /* Shouldn't normally fail */
313b077aed3SPierre Pronchery         if (!ctx->hw->cipher(ctx, out, in, inl)) {
314b077aed3SPierre Pronchery             ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);
315b077aed3SPierre Pronchery             return 0;
316b077aed3SPierre Pronchery         }
317b077aed3SPierre Pronchery 
318b077aed3SPierre Pronchery         if (ctx->alloced) {
319b077aed3SPierre Pronchery             OPENSSL_free(ctx->tlsmac);
320b077aed3SPierre Pronchery             ctx->alloced = 0;
321b077aed3SPierre Pronchery             ctx->tlsmac = NULL;
322b077aed3SPierre Pronchery         }
323b077aed3SPierre Pronchery 
324b077aed3SPierre Pronchery         /* This only fails if padding is publicly invalid */
325b077aed3SPierre Pronchery         *outl = inl;
326b077aed3SPierre Pronchery         if (!ctx->enc
327b077aed3SPierre Pronchery             && !ossl_cipher_tlsunpadblock(ctx->libctx, ctx->tlsversion,
328b077aed3SPierre Pronchery                                           out, outl,
329b077aed3SPierre Pronchery                                           blksz, &ctx->tlsmac, &ctx->alloced,
330b077aed3SPierre Pronchery                                           ctx->tlsmacsize, 0)) {
331b077aed3SPierre Pronchery             ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);
332b077aed3SPierre Pronchery             return 0;
333b077aed3SPierre Pronchery         }
334b077aed3SPierre Pronchery         return 1;
335b077aed3SPierre Pronchery     }
336b077aed3SPierre Pronchery 
337b077aed3SPierre Pronchery     if (ctx->bufsz != 0)
338b077aed3SPierre Pronchery         nextblocks = ossl_cipher_fillblock(ctx->buf, &ctx->bufsz, blksz,
339b077aed3SPierre Pronchery                                            &in, &inl);
340b077aed3SPierre Pronchery     else
341b077aed3SPierre Pronchery         nextblocks = inl & ~(blksz-1);
342b077aed3SPierre Pronchery 
343b077aed3SPierre Pronchery     /*
344b077aed3SPierre Pronchery      * If we're decrypting and we end an update on a block boundary we hold
345b077aed3SPierre Pronchery      * the last block back in case this is the last update call and the last
346b077aed3SPierre Pronchery      * block is padded.
347b077aed3SPierre Pronchery      */
348b077aed3SPierre Pronchery     if (ctx->bufsz == blksz && (ctx->enc || inl > 0 || !ctx->pad)) {
349b077aed3SPierre Pronchery         if (outsize < blksz) {
350b077aed3SPierre Pronchery             ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
351b077aed3SPierre Pronchery             return 0;
352b077aed3SPierre Pronchery         }
353b077aed3SPierre Pronchery         if (!ctx->hw->cipher(ctx, out, ctx->buf, blksz)) {
354b077aed3SPierre Pronchery             ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);
355b077aed3SPierre Pronchery             return 0;
356b077aed3SPierre Pronchery         }
357b077aed3SPierre Pronchery         ctx->bufsz = 0;
358b077aed3SPierre Pronchery         outlint = blksz;
359b077aed3SPierre Pronchery         out += blksz;
360b077aed3SPierre Pronchery     }
361b077aed3SPierre Pronchery     if (nextblocks > 0) {
362b077aed3SPierre Pronchery         if (!ctx->enc && ctx->pad && nextblocks == inl) {
363b077aed3SPierre Pronchery             if (!ossl_assert(inl >= blksz)) {
364b077aed3SPierre Pronchery                 ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
365b077aed3SPierre Pronchery                 return 0;
366b077aed3SPierre Pronchery             }
367b077aed3SPierre Pronchery             nextblocks -= blksz;
368b077aed3SPierre Pronchery         }
369b077aed3SPierre Pronchery         outlint += nextblocks;
370b077aed3SPierre Pronchery         if (outsize < outlint) {
371b077aed3SPierre Pronchery             ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
372b077aed3SPierre Pronchery             return 0;
373b077aed3SPierre Pronchery         }
374b077aed3SPierre Pronchery     }
375b077aed3SPierre Pronchery     if (nextblocks > 0) {
376b077aed3SPierre Pronchery         if (!ctx->hw->cipher(ctx, out, in, nextblocks)) {
377b077aed3SPierre Pronchery             ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);
378b077aed3SPierre Pronchery             return 0;
379b077aed3SPierre Pronchery         }
380b077aed3SPierre Pronchery         in += nextblocks;
381b077aed3SPierre Pronchery         inl -= nextblocks;
382b077aed3SPierre Pronchery     }
383b077aed3SPierre Pronchery     if (inl != 0
384b077aed3SPierre Pronchery         && !ossl_cipher_trailingdata(ctx->buf, &ctx->bufsz, blksz, &in, &inl)) {
385b077aed3SPierre Pronchery         /* ERR_raise already called */
386b077aed3SPierre Pronchery         return 0;
387b077aed3SPierre Pronchery     }
388b077aed3SPierre Pronchery 
389b077aed3SPierre Pronchery     *outl = outlint;
390b077aed3SPierre Pronchery     return inl == 0;
391b077aed3SPierre Pronchery }
392b077aed3SPierre Pronchery 
ossl_cipher_generic_block_final(void * vctx,unsigned char * out,size_t * outl,size_t outsize)393b077aed3SPierre Pronchery int ossl_cipher_generic_block_final(void *vctx, unsigned char *out,
394b077aed3SPierre Pronchery                                     size_t *outl, size_t outsize)
395b077aed3SPierre Pronchery {
396b077aed3SPierre Pronchery     PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
397b077aed3SPierre Pronchery     size_t blksz = ctx->blocksize;
398b077aed3SPierre Pronchery 
399b077aed3SPierre Pronchery     if (!ossl_prov_is_running())
400b077aed3SPierre Pronchery         return 0;
401b077aed3SPierre Pronchery 
402*e0c4386eSCy Schubert     if (!ctx->key_set) {
403*e0c4386eSCy Schubert         ERR_raise(ERR_LIB_PROV, PROV_R_NO_KEY_SET);
404*e0c4386eSCy Schubert         return 0;
405*e0c4386eSCy Schubert     }
406*e0c4386eSCy Schubert 
407b077aed3SPierre Pronchery     if (ctx->tlsversion > 0) {
408b077aed3SPierre Pronchery         /* We never finalize TLS, so this is an error */
409b077aed3SPierre Pronchery         ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);
410b077aed3SPierre Pronchery         return 0;
411b077aed3SPierre Pronchery     }
412b077aed3SPierre Pronchery 
413b077aed3SPierre Pronchery     if (ctx->enc) {
414b077aed3SPierre Pronchery         if (ctx->pad) {
415b077aed3SPierre Pronchery             ossl_cipher_padblock(ctx->buf, &ctx->bufsz, blksz);
416b077aed3SPierre Pronchery         } else if (ctx->bufsz == 0) {
417b077aed3SPierre Pronchery             *outl = 0;
418b077aed3SPierre Pronchery             return 1;
419b077aed3SPierre Pronchery         } else if (ctx->bufsz != blksz) {
420b077aed3SPierre Pronchery             ERR_raise(ERR_LIB_PROV, PROV_R_WRONG_FINAL_BLOCK_LENGTH);
421b077aed3SPierre Pronchery             return 0;
422b077aed3SPierre Pronchery         }
423b077aed3SPierre Pronchery 
424b077aed3SPierre Pronchery         if (outsize < blksz) {
425b077aed3SPierre Pronchery             ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
426b077aed3SPierre Pronchery             return 0;
427b077aed3SPierre Pronchery         }
428b077aed3SPierre Pronchery         if (!ctx->hw->cipher(ctx, out, ctx->buf, blksz)) {
429b077aed3SPierre Pronchery             ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);
430b077aed3SPierre Pronchery             return 0;
431b077aed3SPierre Pronchery         }
432b077aed3SPierre Pronchery         ctx->bufsz = 0;
433b077aed3SPierre Pronchery         *outl = blksz;
434b077aed3SPierre Pronchery         return 1;
435b077aed3SPierre Pronchery     }
436b077aed3SPierre Pronchery 
437b077aed3SPierre Pronchery     /* Decrypting */
438b077aed3SPierre Pronchery     if (ctx->bufsz != blksz) {
439b077aed3SPierre Pronchery         if (ctx->bufsz == 0 && !ctx->pad) {
440b077aed3SPierre Pronchery             *outl = 0;
441b077aed3SPierre Pronchery             return 1;
442b077aed3SPierre Pronchery         }
443b077aed3SPierre Pronchery         ERR_raise(ERR_LIB_PROV, PROV_R_WRONG_FINAL_BLOCK_LENGTH);
444b077aed3SPierre Pronchery         return 0;
445b077aed3SPierre Pronchery     }
446b077aed3SPierre Pronchery 
447b077aed3SPierre Pronchery     if (!ctx->hw->cipher(ctx, ctx->buf, ctx->buf, blksz)) {
448b077aed3SPierre Pronchery         ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);
449b077aed3SPierre Pronchery         return 0;
450b077aed3SPierre Pronchery     }
451b077aed3SPierre Pronchery 
452b077aed3SPierre Pronchery     if (ctx->pad && !ossl_cipher_unpadblock(ctx->buf, &ctx->bufsz, blksz)) {
453b077aed3SPierre Pronchery         /* ERR_raise already called */
454b077aed3SPierre Pronchery         return 0;
455b077aed3SPierre Pronchery     }
456b077aed3SPierre Pronchery 
457b077aed3SPierre Pronchery     if (outsize < ctx->bufsz) {
458b077aed3SPierre Pronchery         ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
459b077aed3SPierre Pronchery         return 0;
460b077aed3SPierre Pronchery     }
461b077aed3SPierre Pronchery     memcpy(out, ctx->buf, ctx->bufsz);
462b077aed3SPierre Pronchery     *outl = ctx->bufsz;
463b077aed3SPierre Pronchery     ctx->bufsz = 0;
464b077aed3SPierre Pronchery     return 1;
465b077aed3SPierre Pronchery }
466b077aed3SPierre Pronchery 
ossl_cipher_generic_stream_update(void * vctx,unsigned char * out,size_t * outl,size_t outsize,const unsigned char * in,size_t inl)467b077aed3SPierre Pronchery int ossl_cipher_generic_stream_update(void *vctx, unsigned char *out,
468b077aed3SPierre Pronchery                                       size_t *outl, size_t outsize,
469b077aed3SPierre Pronchery                                       const unsigned char *in, size_t inl)
470b077aed3SPierre Pronchery {
471b077aed3SPierre Pronchery     PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
472b077aed3SPierre Pronchery 
473*e0c4386eSCy Schubert     if (!ctx->key_set) {
474*e0c4386eSCy Schubert         ERR_raise(ERR_LIB_PROV, PROV_R_NO_KEY_SET);
475*e0c4386eSCy Schubert         return 0;
476*e0c4386eSCy Schubert     }
477*e0c4386eSCy Schubert 
478b077aed3SPierre Pronchery     if (inl == 0) {
479b077aed3SPierre Pronchery         *outl = 0;
480b077aed3SPierre Pronchery         return 1;
481b077aed3SPierre Pronchery     }
482b077aed3SPierre Pronchery 
483b077aed3SPierre Pronchery     if (outsize < inl) {
484b077aed3SPierre Pronchery         ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
485b077aed3SPierre Pronchery         return 0;
486b077aed3SPierre Pronchery     }
487b077aed3SPierre Pronchery 
488b077aed3SPierre Pronchery     if (!ctx->hw->cipher(ctx, out, in, inl)) {
489b077aed3SPierre Pronchery         ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);
490b077aed3SPierre Pronchery         return 0;
491b077aed3SPierre Pronchery     }
492b077aed3SPierre Pronchery 
493b077aed3SPierre Pronchery     *outl = inl;
494b077aed3SPierre Pronchery     if (!ctx->enc && ctx->tlsversion > 0) {
495b077aed3SPierre Pronchery         /*
496b077aed3SPierre Pronchery         * Remove any TLS padding. Only used by cipher_aes_cbc_hmac_sha1_hw.c and
497b077aed3SPierre Pronchery         * cipher_aes_cbc_hmac_sha256_hw.c
498b077aed3SPierre Pronchery         */
499b077aed3SPierre Pronchery         if (ctx->removetlspad) {
500b077aed3SPierre Pronchery             /*
501b077aed3SPierre Pronchery              * We should have already failed in the cipher() call above if this
502b077aed3SPierre Pronchery              * isn't true.
503b077aed3SPierre Pronchery              */
504b077aed3SPierre Pronchery             if (!ossl_assert(*outl >= (size_t)(out[inl - 1] + 1)))
505b077aed3SPierre Pronchery                 return 0;
506b077aed3SPierre Pronchery             /* The actual padding length */
507b077aed3SPierre Pronchery             *outl -= out[inl - 1] + 1;
508b077aed3SPierre Pronchery         }
509b077aed3SPierre Pronchery 
510b077aed3SPierre Pronchery         /* TLS MAC and explicit IV if relevant. We should have already failed
511b077aed3SPierre Pronchery          * in the cipher() call above if *outl is too short.
512b077aed3SPierre Pronchery          */
513b077aed3SPierre Pronchery         if (!ossl_assert(*outl >= ctx->removetlsfixed))
514b077aed3SPierre Pronchery             return 0;
515b077aed3SPierre Pronchery         *outl -= ctx->removetlsfixed;
516b077aed3SPierre Pronchery 
517b077aed3SPierre Pronchery         /* Extract the MAC if there is one */
518b077aed3SPierre Pronchery         if (ctx->tlsmacsize > 0) {
519b077aed3SPierre Pronchery             if (*outl < ctx->tlsmacsize)
520b077aed3SPierre Pronchery                 return 0;
521b077aed3SPierre Pronchery 
522b077aed3SPierre Pronchery             ctx->tlsmac = out + *outl - ctx->tlsmacsize;
523b077aed3SPierre Pronchery             *outl -= ctx->tlsmacsize;
524b077aed3SPierre Pronchery         }
525b077aed3SPierre Pronchery     }
526b077aed3SPierre Pronchery 
527b077aed3SPierre Pronchery     return 1;
528b077aed3SPierre Pronchery }
ossl_cipher_generic_stream_final(void * vctx,unsigned char * out,size_t * outl,size_t outsize)529b077aed3SPierre Pronchery int ossl_cipher_generic_stream_final(void *vctx, unsigned char *out,
530b077aed3SPierre Pronchery                                      size_t *outl, size_t outsize)
531b077aed3SPierre Pronchery {
532*e0c4386eSCy Schubert     PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
533*e0c4386eSCy Schubert 
534b077aed3SPierre Pronchery     if (!ossl_prov_is_running())
535b077aed3SPierre Pronchery         return 0;
536b077aed3SPierre Pronchery 
537*e0c4386eSCy Schubert     if (!ctx->key_set) {
538*e0c4386eSCy Schubert         ERR_raise(ERR_LIB_PROV, PROV_R_NO_KEY_SET);
539*e0c4386eSCy Schubert         return 0;
540*e0c4386eSCy Schubert     }
541*e0c4386eSCy Schubert 
542b077aed3SPierre Pronchery     *outl = 0;
543b077aed3SPierre Pronchery     return 1;
544b077aed3SPierre Pronchery }
545b077aed3SPierre Pronchery 
ossl_cipher_generic_cipher(void * vctx,unsigned char * out,size_t * outl,size_t outsize,const unsigned char * in,size_t inl)546b077aed3SPierre Pronchery int ossl_cipher_generic_cipher(void *vctx, unsigned char *out, size_t *outl,
547b077aed3SPierre Pronchery                                size_t outsize, const unsigned char *in,
548b077aed3SPierre Pronchery                                size_t inl)
549b077aed3SPierre Pronchery {
550b077aed3SPierre Pronchery     PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
551b077aed3SPierre Pronchery 
552b077aed3SPierre Pronchery     if (!ossl_prov_is_running())
553b077aed3SPierre Pronchery         return 0;
554b077aed3SPierre Pronchery 
555*e0c4386eSCy Schubert     if (!ctx->key_set) {
556*e0c4386eSCy Schubert         ERR_raise(ERR_LIB_PROV, PROV_R_NO_KEY_SET);
557*e0c4386eSCy Schubert         return 0;
558*e0c4386eSCy Schubert     }
559*e0c4386eSCy Schubert 
560b077aed3SPierre Pronchery     if (outsize < inl) {
561b077aed3SPierre Pronchery         ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
562b077aed3SPierre Pronchery         return 0;
563b077aed3SPierre Pronchery     }
564b077aed3SPierre Pronchery 
565b077aed3SPierre Pronchery     if (!ctx->hw->cipher(ctx, out, in, inl)) {
566b077aed3SPierre Pronchery         ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);
567b077aed3SPierre Pronchery         return 0;
568b077aed3SPierre Pronchery     }
569b077aed3SPierre Pronchery 
570b077aed3SPierre Pronchery     *outl = inl;
571b077aed3SPierre Pronchery     return 1;
572b077aed3SPierre Pronchery }
573b077aed3SPierre Pronchery 
ossl_cipher_generic_get_ctx_params(void * vctx,OSSL_PARAM params[])574b077aed3SPierre Pronchery int ossl_cipher_generic_get_ctx_params(void *vctx, OSSL_PARAM params[])
575b077aed3SPierre Pronchery {
576b077aed3SPierre Pronchery     PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
577b077aed3SPierre Pronchery     OSSL_PARAM *p;
578b077aed3SPierre Pronchery 
579b077aed3SPierre Pronchery     p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_IVLEN);
580b077aed3SPierre Pronchery     if (p != NULL && !OSSL_PARAM_set_size_t(p, ctx->ivlen)) {
581b077aed3SPierre Pronchery         ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
582b077aed3SPierre Pronchery         return 0;
583b077aed3SPierre Pronchery     }
584b077aed3SPierre Pronchery     p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_PADDING);
585b077aed3SPierre Pronchery     if (p != NULL && !OSSL_PARAM_set_uint(p, ctx->pad)) {
586b077aed3SPierre Pronchery         ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
587b077aed3SPierre Pronchery         return 0;
588b077aed3SPierre Pronchery     }
589b077aed3SPierre Pronchery     p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_IV);
590b077aed3SPierre Pronchery     if (p != NULL
591b077aed3SPierre Pronchery         && !OSSL_PARAM_set_octet_ptr(p, &ctx->oiv, ctx->ivlen)
592b077aed3SPierre Pronchery         && !OSSL_PARAM_set_octet_string(p, &ctx->oiv, ctx->ivlen)) {
593b077aed3SPierre Pronchery         ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
594b077aed3SPierre Pronchery         return 0;
595b077aed3SPierre Pronchery     }
596b077aed3SPierre Pronchery     p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_UPDATED_IV);
597b077aed3SPierre Pronchery     if (p != NULL
598b077aed3SPierre Pronchery         && !OSSL_PARAM_set_octet_ptr(p, &ctx->iv, ctx->ivlen)
599b077aed3SPierre Pronchery         && !OSSL_PARAM_set_octet_string(p, &ctx->iv, ctx->ivlen)) {
600b077aed3SPierre Pronchery         ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
601b077aed3SPierre Pronchery         return 0;
602b077aed3SPierre Pronchery     }
603b077aed3SPierre Pronchery     p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_NUM);
604b077aed3SPierre Pronchery     if (p != NULL && !OSSL_PARAM_set_uint(p, ctx->num)) {
605b077aed3SPierre Pronchery         ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
606b077aed3SPierre Pronchery         return 0;
607b077aed3SPierre Pronchery     }
608b077aed3SPierre Pronchery     p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_KEYLEN);
609b077aed3SPierre Pronchery     if (p != NULL && !OSSL_PARAM_set_size_t(p, ctx->keylen)) {
610b077aed3SPierre Pronchery         ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
611b077aed3SPierre Pronchery         return 0;
612b077aed3SPierre Pronchery     }
613b077aed3SPierre Pronchery     p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_TLS_MAC);
614b077aed3SPierre Pronchery     if (p != NULL
615b077aed3SPierre Pronchery         && !OSSL_PARAM_set_octet_ptr(p, ctx->tlsmac, ctx->tlsmacsize)) {
616b077aed3SPierre Pronchery         ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
617b077aed3SPierre Pronchery         return 0;
618b077aed3SPierre Pronchery     }
619b077aed3SPierre Pronchery     return 1;
620b077aed3SPierre Pronchery }
621b077aed3SPierre Pronchery 
ossl_cipher_generic_set_ctx_params(void * vctx,const OSSL_PARAM params[])622b077aed3SPierre Pronchery int ossl_cipher_generic_set_ctx_params(void *vctx, const OSSL_PARAM params[])
623b077aed3SPierre Pronchery {
624b077aed3SPierre Pronchery     PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
625b077aed3SPierre Pronchery     const OSSL_PARAM *p;
626b077aed3SPierre Pronchery 
627b077aed3SPierre Pronchery     if (params == NULL)
628b077aed3SPierre Pronchery         return 1;
629b077aed3SPierre Pronchery 
630b077aed3SPierre Pronchery     p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_PADDING);
631b077aed3SPierre Pronchery     if (p != NULL) {
632b077aed3SPierre Pronchery         unsigned int pad;
633b077aed3SPierre Pronchery 
634b077aed3SPierre Pronchery         if (!OSSL_PARAM_get_uint(p, &pad)) {
635b077aed3SPierre Pronchery             ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
636b077aed3SPierre Pronchery             return 0;
637b077aed3SPierre Pronchery         }
638b077aed3SPierre Pronchery         ctx->pad = pad ? 1 : 0;
639b077aed3SPierre Pronchery     }
640b077aed3SPierre Pronchery     p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_USE_BITS);
641b077aed3SPierre Pronchery     if (p != NULL) {
642b077aed3SPierre Pronchery         unsigned int bits;
643b077aed3SPierre Pronchery 
644b077aed3SPierre Pronchery         if (!OSSL_PARAM_get_uint(p, &bits)) {
645b077aed3SPierre Pronchery             ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
646b077aed3SPierre Pronchery             return 0;
647b077aed3SPierre Pronchery         }
648b077aed3SPierre Pronchery         ctx->use_bits = bits ? 1 : 0;
649b077aed3SPierre Pronchery     }
650b077aed3SPierre Pronchery     p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_TLS_VERSION);
651b077aed3SPierre Pronchery     if (p != NULL) {
652b077aed3SPierre Pronchery         if (!OSSL_PARAM_get_uint(p, &ctx->tlsversion)) {
653b077aed3SPierre Pronchery             ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
654b077aed3SPierre Pronchery             return 0;
655b077aed3SPierre Pronchery         }
656b077aed3SPierre Pronchery     }
657b077aed3SPierre Pronchery     p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_TLS_MAC_SIZE);
658b077aed3SPierre Pronchery     if (p != NULL) {
659b077aed3SPierre Pronchery         if (!OSSL_PARAM_get_size_t(p, &ctx->tlsmacsize)) {
660b077aed3SPierre Pronchery             ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
661b077aed3SPierre Pronchery             return 0;
662b077aed3SPierre Pronchery         }
663b077aed3SPierre Pronchery     }
664b077aed3SPierre Pronchery     p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_NUM);
665b077aed3SPierre Pronchery     if (p != NULL) {
666b077aed3SPierre Pronchery         unsigned int num;
667b077aed3SPierre Pronchery 
668b077aed3SPierre Pronchery         if (!OSSL_PARAM_get_uint(p, &num)) {
669b077aed3SPierre Pronchery             ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
670b077aed3SPierre Pronchery             return 0;
671b077aed3SPierre Pronchery         }
672b077aed3SPierre Pronchery         ctx->num = num;
673b077aed3SPierre Pronchery     }
674b077aed3SPierre Pronchery     return 1;
675b077aed3SPierre Pronchery }
676b077aed3SPierre Pronchery 
ossl_cipher_generic_initiv(PROV_CIPHER_CTX * ctx,const unsigned char * iv,size_t ivlen)677b077aed3SPierre Pronchery int ossl_cipher_generic_initiv(PROV_CIPHER_CTX *ctx, const unsigned char *iv,
678b077aed3SPierre Pronchery                                size_t ivlen)
679b077aed3SPierre Pronchery {
680b077aed3SPierre Pronchery     if (ivlen != ctx->ivlen
681b077aed3SPierre Pronchery         || ivlen > sizeof(ctx->iv)) {
682b077aed3SPierre Pronchery         ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH);
683b077aed3SPierre Pronchery         return 0;
684b077aed3SPierre Pronchery     }
685b077aed3SPierre Pronchery     ctx->iv_set = 1;
686b077aed3SPierre Pronchery     memcpy(ctx->iv, iv, ivlen);
687b077aed3SPierre Pronchery     memcpy(ctx->oiv, iv, ivlen);
688b077aed3SPierre Pronchery     return 1;
689b077aed3SPierre Pronchery }
690b077aed3SPierre Pronchery 
ossl_cipher_generic_initkey(void * vctx,size_t kbits,size_t blkbits,size_t ivbits,unsigned int mode,uint64_t flags,const PROV_CIPHER_HW * hw,void * provctx)691b077aed3SPierre Pronchery void ossl_cipher_generic_initkey(void *vctx, size_t kbits, size_t blkbits,
692b077aed3SPierre Pronchery                                  size_t ivbits, unsigned int mode,
693b077aed3SPierre Pronchery                                  uint64_t flags, const PROV_CIPHER_HW *hw,
694b077aed3SPierre Pronchery                                  void *provctx)
695b077aed3SPierre Pronchery {
696b077aed3SPierre Pronchery     PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
697b077aed3SPierre Pronchery 
698b077aed3SPierre Pronchery     if ((flags & PROV_CIPHER_FLAG_INVERSE_CIPHER) != 0)
699b077aed3SPierre Pronchery         ctx->inverse_cipher = 1;
700b077aed3SPierre Pronchery     if ((flags & PROV_CIPHER_FLAG_VARIABLE_LENGTH) != 0)
701b077aed3SPierre Pronchery         ctx->variable_keylength = 1;
702b077aed3SPierre Pronchery 
703b077aed3SPierre Pronchery     ctx->pad = 1;
704b077aed3SPierre Pronchery     ctx->keylen = ((kbits) / 8);
705b077aed3SPierre Pronchery     ctx->ivlen = ((ivbits) / 8);
706b077aed3SPierre Pronchery     ctx->hw = hw;
707b077aed3SPierre Pronchery     ctx->mode = mode;
708b077aed3SPierre Pronchery     ctx->blocksize = blkbits / 8;
709b077aed3SPierre Pronchery     if (provctx != NULL)
710b077aed3SPierre Pronchery         ctx->libctx = PROV_LIBCTX_OF(provctx); /* used for rand */
711b077aed3SPierre Pronchery }
712