1b077aed3SPierre Pronchery /*
2*e7be843bSPierre Pronchery * Copyright 2020-2024 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 #include <string.h>
11b077aed3SPierre Pronchery #include <openssl/crypto.h>
12b077aed3SPierre Pronchery #include <openssl/core_dispatch.h>
13b077aed3SPierre Pronchery #include <openssl/proverr.h>
14b077aed3SPierre Pronchery #include "prov/implementations.h"
15b077aed3SPierre Pronchery #include "prov/ciphercommon.h"
16b077aed3SPierre Pronchery #include "prov/providercommon.h"
17b077aed3SPierre Pronchery
18b077aed3SPierre Pronchery typedef struct prov_cipher_null_ctx_st {
19b077aed3SPierre Pronchery int enc;
20b077aed3SPierre Pronchery size_t tlsmacsize;
21b077aed3SPierre Pronchery const unsigned char *tlsmac;
22b077aed3SPierre Pronchery } PROV_CIPHER_NULL_CTX;
23b077aed3SPierre Pronchery
24b077aed3SPierre Pronchery static OSSL_FUNC_cipher_newctx_fn null_newctx;
null_newctx(void * provctx)25b077aed3SPierre Pronchery static void *null_newctx(void *provctx)
26b077aed3SPierre Pronchery {
27b077aed3SPierre Pronchery if (!ossl_prov_is_running())
28b077aed3SPierre Pronchery return NULL;
29b077aed3SPierre Pronchery
30b077aed3SPierre Pronchery return OPENSSL_zalloc(sizeof(PROV_CIPHER_NULL_CTX));
31b077aed3SPierre Pronchery }
32b077aed3SPierre Pronchery
33b077aed3SPierre Pronchery static OSSL_FUNC_cipher_freectx_fn null_freectx;
null_freectx(void * vctx)34b077aed3SPierre Pronchery static void null_freectx(void *vctx)
35b077aed3SPierre Pronchery {
36b077aed3SPierre Pronchery OPENSSL_free(vctx);
37b077aed3SPierre Pronchery }
38b077aed3SPierre Pronchery
39b077aed3SPierre Pronchery static OSSL_FUNC_cipher_encrypt_init_fn null_einit;
null_einit(void * vctx,const unsigned char * key,size_t keylen,const unsigned char * iv,size_t ivlen,const OSSL_PARAM params[])40b077aed3SPierre Pronchery static int null_einit(void *vctx, const unsigned char *key, size_t keylen,
41b077aed3SPierre Pronchery const unsigned char *iv, size_t ivlen,
42b077aed3SPierre Pronchery const OSSL_PARAM params[])
43b077aed3SPierre Pronchery {
44b077aed3SPierre Pronchery PROV_CIPHER_NULL_CTX *ctx = (PROV_CIPHER_NULL_CTX *)vctx;
45b077aed3SPierre Pronchery
46b077aed3SPierre Pronchery if (!ossl_prov_is_running())
47b077aed3SPierre Pronchery return 0;
48b077aed3SPierre Pronchery
49b077aed3SPierre Pronchery ctx->enc = 1;
50b077aed3SPierre Pronchery return 1;
51b077aed3SPierre Pronchery }
52b077aed3SPierre Pronchery
53b077aed3SPierre Pronchery static OSSL_FUNC_cipher_decrypt_init_fn null_dinit;
null_dinit(void * vctx,const unsigned char * key,size_t keylen,const unsigned char * iv,size_t ivlen,const OSSL_PARAM params[])54b077aed3SPierre Pronchery static int null_dinit(void *vctx, const unsigned char *key, size_t keylen,
55b077aed3SPierre Pronchery const unsigned char *iv, size_t ivlen,
56b077aed3SPierre Pronchery const OSSL_PARAM params[])
57b077aed3SPierre Pronchery {
58b077aed3SPierre Pronchery if (!ossl_prov_is_running())
59b077aed3SPierre Pronchery return 0;
60b077aed3SPierre Pronchery
61b077aed3SPierre Pronchery return 1;
62b077aed3SPierre Pronchery }
63b077aed3SPierre Pronchery
64b077aed3SPierre Pronchery static OSSL_FUNC_cipher_cipher_fn null_cipher;
null_cipher(void * vctx,unsigned char * out,size_t * outl,size_t outsize,const unsigned char * in,size_t inl)65b077aed3SPierre Pronchery static int null_cipher(void *vctx, unsigned char *out, size_t *outl,
66b077aed3SPierre Pronchery size_t outsize, const unsigned char *in, size_t inl)
67b077aed3SPierre Pronchery {
68b077aed3SPierre Pronchery PROV_CIPHER_NULL_CTX *ctx = (PROV_CIPHER_NULL_CTX *)vctx;
69b077aed3SPierre Pronchery
70b077aed3SPierre Pronchery if (!ossl_prov_is_running())
71b077aed3SPierre Pronchery return 0;
72b077aed3SPierre Pronchery
73b077aed3SPierre Pronchery if (!ctx->enc && ctx->tlsmacsize > 0) {
74b077aed3SPierre Pronchery /*
75b077aed3SPierre Pronchery * TLS NULL cipher as per:
76b077aed3SPierre Pronchery * https://tools.ietf.org/html/rfc5246#section-6.2.3.1
77b077aed3SPierre Pronchery */
78b077aed3SPierre Pronchery if (inl < ctx->tlsmacsize)
79b077aed3SPierre Pronchery return 0;
80b077aed3SPierre Pronchery ctx->tlsmac = in + inl - ctx->tlsmacsize;
81b077aed3SPierre Pronchery inl -= ctx->tlsmacsize;
82b077aed3SPierre Pronchery }
83b077aed3SPierre Pronchery if (outsize < inl)
84b077aed3SPierre Pronchery return 0;
85*e7be843bSPierre Pronchery if (out != NULL && in != out)
86b077aed3SPierre Pronchery memcpy(out, in, inl);
87b077aed3SPierre Pronchery *outl = inl;
88b077aed3SPierre Pronchery return 1;
89b077aed3SPierre Pronchery }
90b077aed3SPierre Pronchery
91b077aed3SPierre Pronchery static OSSL_FUNC_cipher_final_fn null_final;
null_final(void * vctx,unsigned char * out,size_t * outl,size_t outsize)92b077aed3SPierre Pronchery static int null_final(void *vctx, unsigned char *out, size_t *outl,
93b077aed3SPierre Pronchery size_t outsize)
94b077aed3SPierre Pronchery {
95b077aed3SPierre Pronchery if (!ossl_prov_is_running())
96b077aed3SPierre Pronchery return 0;
97b077aed3SPierre Pronchery
98b077aed3SPierre Pronchery *outl = 0;
99b077aed3SPierre Pronchery return 1;
100b077aed3SPierre Pronchery }
101b077aed3SPierre Pronchery
102b077aed3SPierre Pronchery static OSSL_FUNC_cipher_get_params_fn null_get_params;
null_get_params(OSSL_PARAM params[])103b077aed3SPierre Pronchery static int null_get_params(OSSL_PARAM params[])
104b077aed3SPierre Pronchery {
105b077aed3SPierre Pronchery return ossl_cipher_generic_get_params(params, 0, 0, 0, 8, 0);
106b077aed3SPierre Pronchery }
107b077aed3SPierre Pronchery
108b077aed3SPierre Pronchery static const OSSL_PARAM null_known_gettable_ctx_params[] = {
109b077aed3SPierre Pronchery OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_KEYLEN, NULL),
110b077aed3SPierre Pronchery OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_IVLEN, NULL),
111b077aed3SPierre Pronchery { OSSL_CIPHER_PARAM_TLS_MAC, OSSL_PARAM_OCTET_PTR, NULL, 0, OSSL_PARAM_UNMODIFIED },
112b077aed3SPierre Pronchery OSSL_PARAM_END
113b077aed3SPierre Pronchery };
114b077aed3SPierre Pronchery
115b077aed3SPierre Pronchery static OSSL_FUNC_cipher_gettable_ctx_params_fn null_gettable_ctx_params;
null_gettable_ctx_params(ossl_unused void * cctx,ossl_unused void * provctx)116b077aed3SPierre Pronchery static const OSSL_PARAM *null_gettable_ctx_params(ossl_unused void *cctx,
117b077aed3SPierre Pronchery ossl_unused void *provctx)
118b077aed3SPierre Pronchery {
119b077aed3SPierre Pronchery return null_known_gettable_ctx_params;
120b077aed3SPierre Pronchery }
121b077aed3SPierre Pronchery
122b077aed3SPierre Pronchery static OSSL_FUNC_cipher_get_ctx_params_fn null_get_ctx_params;
null_get_ctx_params(void * vctx,OSSL_PARAM params[])123b077aed3SPierre Pronchery static int null_get_ctx_params(void *vctx, OSSL_PARAM params[])
124b077aed3SPierre Pronchery {
125b077aed3SPierre Pronchery PROV_CIPHER_NULL_CTX *ctx = (PROV_CIPHER_NULL_CTX *)vctx;
126b077aed3SPierre Pronchery OSSL_PARAM *p;
127b077aed3SPierre Pronchery
128b077aed3SPierre Pronchery p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_IVLEN);
129b077aed3SPierre Pronchery if (p != NULL && !OSSL_PARAM_set_size_t(p, 0)) {
130b077aed3SPierre Pronchery ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
131b077aed3SPierre Pronchery return 0;
132b077aed3SPierre Pronchery }
133b077aed3SPierre Pronchery p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_KEYLEN);
134b077aed3SPierre Pronchery if (p != NULL && !OSSL_PARAM_set_size_t(p, 0)) {
135b077aed3SPierre Pronchery ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
136b077aed3SPierre Pronchery return 0;
137b077aed3SPierre Pronchery }
138b077aed3SPierre Pronchery p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_TLS_MAC);
139b077aed3SPierre Pronchery if (p != NULL
140b077aed3SPierre Pronchery && !OSSL_PARAM_set_octet_ptr(p, ctx->tlsmac, ctx->tlsmacsize)) {
141b077aed3SPierre Pronchery ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
142b077aed3SPierre Pronchery return 0;
143b077aed3SPierre Pronchery }
144b077aed3SPierre Pronchery return 1;
145b077aed3SPierre Pronchery }
146b077aed3SPierre Pronchery
147b077aed3SPierre Pronchery static const OSSL_PARAM null_known_settable_ctx_params[] = {
148b077aed3SPierre Pronchery OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_TLS_MAC_SIZE, NULL),
149b077aed3SPierre Pronchery OSSL_PARAM_END
150b077aed3SPierre Pronchery };
151b077aed3SPierre Pronchery
152b077aed3SPierre Pronchery static OSSL_FUNC_cipher_settable_ctx_params_fn null_settable_ctx_params;
null_settable_ctx_params(ossl_unused void * cctx,ossl_unused void * provctx)153b077aed3SPierre Pronchery static const OSSL_PARAM *null_settable_ctx_params(ossl_unused void *cctx,
154b077aed3SPierre Pronchery ossl_unused void *provctx)
155b077aed3SPierre Pronchery {
156b077aed3SPierre Pronchery return null_known_settable_ctx_params;
157b077aed3SPierre Pronchery }
158b077aed3SPierre Pronchery
159b077aed3SPierre Pronchery
160b077aed3SPierre Pronchery static OSSL_FUNC_cipher_set_ctx_params_fn null_set_ctx_params;
null_set_ctx_params(void * vctx,const OSSL_PARAM params[])161b077aed3SPierre Pronchery static int null_set_ctx_params(void *vctx, const OSSL_PARAM params[])
162b077aed3SPierre Pronchery {
163b077aed3SPierre Pronchery PROV_CIPHER_NULL_CTX *ctx = (PROV_CIPHER_NULL_CTX *)vctx;
164b077aed3SPierre Pronchery const OSSL_PARAM *p;
165b077aed3SPierre Pronchery
166b077aed3SPierre Pronchery p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_TLS_MAC_SIZE);
167b077aed3SPierre Pronchery if (p != NULL) {
168b077aed3SPierre Pronchery if (!OSSL_PARAM_get_size_t(p, &ctx->tlsmacsize)) {
169b077aed3SPierre Pronchery ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
170b077aed3SPierre Pronchery return 0;
171b077aed3SPierre Pronchery }
172b077aed3SPierre Pronchery }
173b077aed3SPierre Pronchery
174b077aed3SPierre Pronchery return 1;
175b077aed3SPierre Pronchery }
176b077aed3SPierre Pronchery
177b077aed3SPierre Pronchery const OSSL_DISPATCH ossl_null_functions[] = {
178b077aed3SPierre Pronchery { OSSL_FUNC_CIPHER_NEWCTX,
179b077aed3SPierre Pronchery (void (*)(void)) null_newctx },
180b077aed3SPierre Pronchery { OSSL_FUNC_CIPHER_FREECTX, (void (*)(void)) null_freectx },
181b077aed3SPierre Pronchery { OSSL_FUNC_CIPHER_DUPCTX, (void (*)(void)) null_newctx },
182b077aed3SPierre Pronchery { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))null_einit },
183b077aed3SPierre Pronchery { OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))null_dinit },
184b077aed3SPierre Pronchery { OSSL_FUNC_CIPHER_UPDATE, (void (*)(void))null_cipher },
185b077aed3SPierre Pronchery { OSSL_FUNC_CIPHER_FINAL, (void (*)(void))null_final },
186b077aed3SPierre Pronchery { OSSL_FUNC_CIPHER_CIPHER, (void (*)(void))null_cipher },
187b077aed3SPierre Pronchery { OSSL_FUNC_CIPHER_GET_PARAMS, (void (*)(void)) null_get_params },
188b077aed3SPierre Pronchery { OSSL_FUNC_CIPHER_GETTABLE_PARAMS,
189b077aed3SPierre Pronchery (void (*)(void))ossl_cipher_generic_gettable_params },
190b077aed3SPierre Pronchery { OSSL_FUNC_CIPHER_GET_CTX_PARAMS, (void (*)(void))null_get_ctx_params },
191b077aed3SPierre Pronchery { OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS,
192b077aed3SPierre Pronchery (void (*)(void))null_gettable_ctx_params },
193b077aed3SPierre Pronchery { OSSL_FUNC_CIPHER_SET_CTX_PARAMS, (void (*)(void))null_set_ctx_params },
194b077aed3SPierre Pronchery { OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS,
195b077aed3SPierre Pronchery (void (*)(void))null_settable_ctx_params },
196*e7be843bSPierre Pronchery OSSL_DISPATCH_END
197b077aed3SPierre Pronchery };
198