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