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