/src/openssl31/providers/implementations/ciphers/cipher_null.c
Line  | Count  | Source (jump to first uncovered line)  | 
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  | 5.68k  | { | 
27  | 5.68k  |     if (!ossl_prov_is_running())  | 
28  | 0  |         return NULL;  | 
29  |  |  | 
30  | 5.68k  |     return OPENSSL_zalloc(sizeof(PROV_CIPHER_NULL_CTX));  | 
31  | 5.68k  | }  | 
32  |  |  | 
33  |  | static OSSL_FUNC_cipher_freectx_fn null_freectx;  | 
34  |  | static void null_freectx(void *vctx)  | 
35  | 5.68k  | { | 
36  | 5.68k  |     OPENSSL_free(vctx);  | 
37  | 5.68k  | }  | 
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  | 2.38k  | { | 
44  | 2.38k  |     PROV_CIPHER_NULL_CTX *ctx = (PROV_CIPHER_NULL_CTX *)vctx;  | 
45  |  |  | 
46  | 2.38k  |     if (!ossl_prov_is_running())  | 
47  | 0  |         return 0;  | 
48  |  |  | 
49  | 2.38k  |     ctx->enc = 1;  | 
50  | 2.38k  |     return 1;  | 
51  | 2.38k  | }  | 
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  | 3.30k  | { | 
58  | 3.30k  |     if (!ossl_prov_is_running())  | 
59  | 0  |         return 0;  | 
60  |  |  | 
61  | 3.30k  |     return 1;  | 
62  | 3.30k  | }  | 
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  | 63.8k  | { | 
68  | 63.8k  |     PROV_CIPHER_NULL_CTX *ctx = (PROV_CIPHER_NULL_CTX *)vctx;  | 
69  |  |  | 
70  | 63.8k  |     if (!ossl_prov_is_running())  | 
71  | 0  |         return 0;  | 
72  |  |  | 
73  | 63.8k  |     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  | 31.0k  |         if (inl < ctx->tlsmacsize)  | 
79  | 72  |             return 0;  | 
80  | 31.0k  |         ctx->tlsmac = in + inl - ctx->tlsmacsize;  | 
81  | 31.0k  |         inl -= ctx->tlsmacsize;  | 
82  | 31.0k  |     }  | 
83  | 63.7k  |     if (outsize < inl)  | 
84  | 0  |         return 0;  | 
85  | 63.7k  |     if (in != out)  | 
86  | 0  |         memcpy(out, in, inl);  | 
87  | 63.7k  |     *outl = inl;  | 
88  | 63.7k  |     return 1;  | 
89  | 63.7k  | }  | 
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  | 0  | { | 
95  | 0  |     if (!ossl_prov_is_running())  | 
96  | 0  |         return 0;  | 
97  |  |  | 
98  | 0  |     *outl = 0;  | 
99  | 0  |     return 1;  | 
100  | 0  | }  | 
101  |  |  | 
102  |  | static OSSL_FUNC_cipher_get_params_fn null_get_params;  | 
103  |  | static int null_get_params(OSSL_PARAM params[])  | 
104  | 25  | { | 
105  | 25  |     return ossl_cipher_generic_get_params(params, 0, 0, 0, 8, 0);  | 
106  | 25  | }  | 
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  | 25  | { | 
119  | 25  |     return null_known_gettable_ctx_params;  | 
120  | 25  | }  | 
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  | 106k  | { | 
125  | 106k  |     PROV_CIPHER_NULL_CTX *ctx = (PROV_CIPHER_NULL_CTX *)vctx;  | 
126  | 106k  |     OSSL_PARAM *p;  | 
127  |  |  | 
128  | 106k  |     p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_IVLEN);  | 
129  | 106k  |     if (p != NULL && !OSSL_PARAM_set_size_t(p, 0)) { | 
130  | 0  |         ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);  | 
131  | 0  |         return 0;  | 
132  | 0  |     }  | 
133  | 106k  |     p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_KEYLEN);  | 
134  | 106k  |     if (p != NULL && !OSSL_PARAM_set_size_t(p, 0)) { | 
135  | 0  |         ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);  | 
136  | 0  |         return 0;  | 
137  | 0  |     }  | 
138  | 106k  |     p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_TLS_MAC);  | 
139  | 106k  |     if (p != NULL  | 
140  | 106k  |         && !OSSL_PARAM_set_octet_ptr(p, ctx->tlsmac, ctx->tlsmacsize)) { | 
141  | 0  |         ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);  | 
142  | 0  |         return 0;  | 
143  | 0  |     }  | 
144  | 106k  |     return 1;  | 
145  | 106k  | }  | 
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  | 2  | { | 
156  | 2  |     return null_known_settable_ctx_params;  | 
157  | 2  | }  | 
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  | 5.68k  | { | 
163  | 5.68k  |     PROV_CIPHER_NULL_CTX *ctx = (PROV_CIPHER_NULL_CTX *)vctx;  | 
164  | 5.68k  |     const OSSL_PARAM *p;  | 
165  |  |  | 
166  | 5.68k  |     p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_TLS_MAC_SIZE);  | 
167  | 5.68k  |     if (p != NULL) { | 
168  | 5.68k  |         if (!OSSL_PARAM_get_size_t(p, &ctx->tlsmacsize)) { | 
169  | 0  |             ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);  | 
170  | 0  |             return 0;  | 
171  | 0  |         }  | 
172  | 5.68k  |     }  | 
173  |  |  | 
174  | 5.68k  |     return 1;  | 
175  | 5.68k  | }  | 
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  |  | };  |