prov: upport modified gettable/settable ctx calls for ciphers
[openssl.git] / providers / implementations / ciphers / cipher_null.c
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 {
43     PROV_CIPHER_NULL_CTX *ctx = (PROV_CIPHER_NULL_CTX *)vctx;
44
45     if (!ossl_prov_is_running())
46         return 0;
47
48     ctx->enc = 1;
49     return 1;
50 }
51
52 static OSSL_FUNC_cipher_decrypt_init_fn null_dinit;
53 static int null_dinit(void *vctx, const unsigned char *key, size_t keylen,
54                       const unsigned char *iv, size_t ivlen)
55 {
56     if (!ossl_prov_is_running())
57         return 0;
58
59     return 1;
60 }
61
62 static OSSL_FUNC_cipher_cipher_fn null_cipher;
63 static int null_cipher(void *vctx, unsigned char *out, size_t *outl,
64                        size_t outsize, const unsigned char *in, size_t inl)
65 {
66     PROV_CIPHER_NULL_CTX *ctx = (PROV_CIPHER_NULL_CTX *)vctx;
67
68     if (!ossl_prov_is_running())
69         return 0;
70
71     if (!ctx->enc && ctx->tlsmacsize > 0) {
72         /*
73          * TLS NULL cipher as per:
74          * https://tools.ietf.org/html/rfc5246#section-6.2.3.1
75          */
76         if (inl < ctx->tlsmacsize)
77             return 0;
78         ctx->tlsmac = in + inl - ctx->tlsmacsize;
79         inl -= ctx->tlsmacsize;
80     }
81     if (outsize < inl)
82         return 0;
83     if (in != out)
84         memcpy(out, in, inl);
85     *outl = inl;
86     return 1;
87 }
88
89 static OSSL_FUNC_cipher_final_fn null_final;
90 static int null_final(void *vctx, unsigned char *out, size_t *outl,
91                       size_t outsize)
92 {
93     if (!ossl_prov_is_running())
94         return 0;
95
96     *outl = 0;
97     return 1;
98 }
99
100 static OSSL_FUNC_cipher_get_params_fn null_get_params;
101 static int null_get_params(OSSL_PARAM params[])
102 {
103     return ossl_cipher_generic_get_params(params, 0, 0, 0, 8, 0);
104 }
105
106 static const OSSL_PARAM null_known_gettable_ctx_params[] = {
107     OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_KEYLEN, NULL),
108     OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_IVLEN, NULL),
109     { OSSL_CIPHER_PARAM_TLS_MAC, OSSL_PARAM_OCTET_PTR, NULL, 0, OSSL_PARAM_UNMODIFIED },
110     OSSL_PARAM_END
111 };
112
113 static OSSL_FUNC_cipher_gettable_ctx_params_fn null_gettable_ctx_params;
114 static const OSSL_PARAM *null_gettable_ctx_params(ossl_unused void *cctx,
115                                                   ossl_unused void *provctx)
116 {
117     return null_known_gettable_ctx_params;
118 }
119
120 static OSSL_FUNC_cipher_get_ctx_params_fn null_get_ctx_params;
121 static int null_get_ctx_params(void *vctx, OSSL_PARAM params[])
122 {
123     PROV_CIPHER_NULL_CTX *ctx = (PROV_CIPHER_NULL_CTX *)vctx;
124     OSSL_PARAM *p;
125
126     p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_IVLEN);
127     if (p != NULL && !OSSL_PARAM_set_size_t(p, 0)) {
128         ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
129         return 0;
130     }
131     p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_KEYLEN);
132     if (p != NULL && !OSSL_PARAM_set_size_t(p, 0)) {
133         ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
134         return 0;
135     }
136     p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_TLS_MAC);
137     if (p != NULL
138         && !OSSL_PARAM_set_octet_ptr(p, ctx->tlsmac, ctx->tlsmacsize)) {
139         ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
140         return 0;
141     }
142     return 1;
143 }
144
145 static const OSSL_PARAM null_known_settable_ctx_params[] = {
146     OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_TLS_MAC_SIZE, NULL),
147     OSSL_PARAM_END
148 };
149
150 static OSSL_FUNC_cipher_settable_ctx_params_fn null_settable_ctx_params;
151 static const OSSL_PARAM *null_settable_ctx_params(ossl_unused void *cctx,
152                                                   ossl_unused void *provctx)
153 {
154     return null_known_settable_ctx_params;
155 }
156
157
158 static OSSL_FUNC_cipher_set_ctx_params_fn null_set_ctx_params;
159 static int null_set_ctx_params(void *vctx, const OSSL_PARAM params[])
160 {
161     PROV_CIPHER_NULL_CTX *ctx = (PROV_CIPHER_NULL_CTX *)vctx;
162     const OSSL_PARAM *p;
163
164     p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_TLS_MAC_SIZE);
165     if (p != NULL) {
166         if (!OSSL_PARAM_get_size_t(p, &ctx->tlsmacsize)) {
167             ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
168             return 0;
169         }
170     }
171
172     return 1;
173 }
174
175 const OSSL_DISPATCH ossl_null_functions[] = {
176     { OSSL_FUNC_CIPHER_NEWCTX,
177       (void (*)(void)) null_newctx },
178     { OSSL_FUNC_CIPHER_FREECTX, (void (*)(void)) null_freectx },
179     { OSSL_FUNC_CIPHER_DUPCTX, (void (*)(void)) null_newctx },
180     { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))null_einit },
181     { OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))null_dinit },
182     { OSSL_FUNC_CIPHER_UPDATE, (void (*)(void))null_cipher },
183     { OSSL_FUNC_CIPHER_FINAL, (void (*)(void))null_final },
184     { OSSL_FUNC_CIPHER_CIPHER, (void (*)(void))null_cipher },
185     { OSSL_FUNC_CIPHER_GET_PARAMS, (void (*)(void)) null_get_params },
186     { OSSL_FUNC_CIPHER_GETTABLE_PARAMS,
187         (void (*)(void))ossl_cipher_generic_gettable_params },
188     { OSSL_FUNC_CIPHER_GET_CTX_PARAMS, (void (*)(void))null_get_ctx_params },
189     { OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS,
190       (void (*)(void))null_gettable_ctx_params },
191     { OSSL_FUNC_CIPHER_SET_CTX_PARAMS, (void (*)(void))null_set_ctx_params },
192     { OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS,
193       (void (*)(void))null_settable_ctx_params },
194     { 0, NULL }
195 };