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