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