6443e6574203ec9f90908fbf607ea2fde0db2652
[oweals/openssl.git] / providers / implementations / ciphers / cipher_null.c
1 /*
2  * Copyright 2020 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_numbers.h>
13 #include "prov/implementations.h"
14 #include "prov/ciphercommon.h"
15 #include "prov/providercommonerr.h"
16
17 static OSSL_OP_cipher_newctx_fn null_newctx;
18 static void *null_newctx(void *provctx)
19 {
20     static int dummy = 0;
21
22     return &dummy;
23 }
24
25 static OSSL_OP_cipher_freectx_fn null_freectx;
26 static void null_freectx(void *vctx)
27 {
28 }
29
30 static OSSL_OP_cipher_encrypt_init_fn null_init;
31 static int null_init(void *vctx, const unsigned char *key, size_t keylen,
32                      const unsigned char *iv, size_t ivlen)
33 {
34     return 1;
35 }
36
37 static OSSL_OP_cipher_cipher_fn null_cipher;
38 static int null_cipher(void *vctx, unsigned char *out, size_t *outl,
39                        size_t outsize, const unsigned char *in, size_t inl)
40 {
41     if (outsize < inl)
42         return 0;
43     if (in != out)
44         memcpy(out, in, inl);
45     *outl = inl;
46     return 1;
47 }
48
49 static OSSL_OP_cipher_final_fn null_final;
50 static int null_final(void *vctx, unsigned char *out, size_t *outl,
51                       size_t outsize)
52 {
53     *outl = 0;
54     return 1;
55 }
56
57 static OSSL_OP_cipher_get_params_fn null_get_params;
58 static int null_get_params(OSSL_PARAM params[])
59 {
60     return cipher_generic_get_params(params, 0, 0, 0, 8, 0);
61 }
62
63 static const OSSL_PARAM null_known_gettable_ctx_params[] = {
64     OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_KEYLEN, NULL),
65     OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_IVLEN, NULL),
66     OSSL_PARAM_END
67 };
68
69 static OSSL_OP_cipher_gettable_ctx_params_fn null_gettable_ctx_params;
70 static const OSSL_PARAM *null_gettable_ctx_params(void)
71 {
72     return null_known_gettable_ctx_params;
73 }
74
75 static OSSL_OP_cipher_get_ctx_params_fn null_get_ctx_params;
76 static int null_get_ctx_params(void *vctx, OSSL_PARAM params[])
77 {
78     OSSL_PARAM *p;
79
80     p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_IVLEN);
81     if (p != NULL && !OSSL_PARAM_set_size_t(p, 0)) {
82         ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
83         return 0;
84     }
85     p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_KEYLEN);
86     if (p != NULL && !OSSL_PARAM_set_size_t(p, 0)) {
87         ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
88         return 0;
89     }
90     return 1;
91 }
92
93 const OSSL_DISPATCH null_functions[] = {
94     { OSSL_FUNC_CIPHER_NEWCTX,
95       (void (*)(void)) null_newctx },
96     { OSSL_FUNC_CIPHER_FREECTX, (void (*)(void)) null_freectx },
97     { OSSL_FUNC_CIPHER_DUPCTX, (void (*)(void)) null_newctx },
98     { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))null_init },
99     { OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))null_init },
100     { OSSL_FUNC_CIPHER_UPDATE, (void (*)(void))null_cipher },
101     { OSSL_FUNC_CIPHER_FINAL, (void (*)(void))null_final },
102     { OSSL_FUNC_CIPHER_CIPHER, (void (*)(void))null_cipher },
103     { OSSL_FUNC_CIPHER_GET_PARAMS, (void (*)(void)) null_get_params },
104     { OSSL_FUNC_CIPHER_GETTABLE_PARAMS,
105         (void (*)(void))cipher_generic_gettable_params },
106     { OSSL_FUNC_CIPHER_GET_CTX_PARAMS, (void (*)(void))null_get_ctx_params },
107     { OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS,
108       (void (*)(void))null_gettable_ctx_params },
109     { 0, NULL }
110 };