645a6b8b64542f986e73fbc706ba9ece6a42dba4
[openssl.git] / providers / implementations / ciphers / cipher_rc5.c
1 /*
2  * Copyright 2019 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 /* Dispatch functions for RC5 cipher modes ecb, cbc, ofb, cfb */
11
12 #include "cipher_rc5.h"
13 #include "internal/provider_algs.h"
14 #include "internal/providercommonerr.h"
15
16 static OSSL_OP_cipher_freectx_fn rc5_freectx;
17 static OSSL_OP_cipher_dupctx_fn rc5_dupctx;
18 OSSL_OP_cipher_gettable_ctx_params_fn rc5_gettable_ctx_params;
19 OSSL_OP_cipher_settable_ctx_params_fn rc5_settable_ctx_params;
20
21 static void rc5_freectx(void *vctx)
22 {
23     PROV_RC5_CTX *ctx = (PROV_RC5_CTX *)vctx;
24
25     OPENSSL_clear_free(ctx,  sizeof(*ctx));
26 }
27
28 static void *rc5_dupctx(void *ctx)
29 {
30     PROV_RC5_CTX *in = (PROV_RC5_CTX *)ctx;
31     PROV_RC5_CTX *ret = OPENSSL_malloc(sizeof(*ret));
32
33     if (ret == NULL) {
34         ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
35         return NULL;
36     }
37     *ret = *in;
38
39     return ret;
40 }
41
42 static int rc5_set_ctx_params(void *vctx, const OSSL_PARAM params[])
43 {
44     PROV_RC5_CTX *ctx = (PROV_RC5_CTX *)vctx;
45     const OSSL_PARAM *p;
46
47     if (!cipher_generic_set_ctx_params(vctx, params))
48         return 0;
49
50     p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_ROUNDS);
51     if (p != NULL) {
52         unsigned int rounds;
53
54         if (!OSSL_PARAM_get_uint(p, &rounds)) {
55             ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
56             return 0;
57         }
58         if (rounds != RC5_8_ROUNDS
59             && rounds != RC5_12_ROUNDS
60             && rounds != RC5_16_ROUNDS) {
61             ERR_raise(ERR_LIB_PROV, PROV_R_UNSUPPORTED_NUMBER_OF_ROUNDS);
62             return 0;
63         }
64         ctx->rounds = rounds;
65     }
66     return 1;
67 }
68
69 CIPHER_DEFAULT_GETTABLE_CTX_PARAMS_START(rc5)
70     OSSL_PARAM_uint(OSSL_CIPHER_PARAM_ROUNDS, NULL),
71 CIPHER_DEFAULT_GETTABLE_CTX_PARAMS_END(rc5)
72
73 CIPHER_DEFAULT_SETTABLE_CTX_PARAMS_START(rc5)
74     OSSL_PARAM_uint(OSSL_CIPHER_PARAM_ROUNDS, NULL),
75 CIPHER_DEFAULT_SETTABLE_CTX_PARAMS_END(rc5)
76
77
78 static int rc5_get_ctx_params(void *vctx, OSSL_PARAM params[])
79 {
80     PROV_RC5_CTX *ctx = (PROV_RC5_CTX *)vctx;
81     OSSL_PARAM *p;
82
83     if (!cipher_generic_get_ctx_params(vctx, params))
84         return 0;
85     p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_ROUNDS);
86     if (p != NULL && !OSSL_PARAM_set_uint(p, ctx->rounds)) {
87         ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
88         return 0;
89     }
90     return 1;
91 }
92
93 #define IMPLEMENT_cipher(alg, UCALG, lcmode, UCMODE, flags, kbits,             \
94                          blkbits, ivbits, typ)                                 \
95 static OSSL_OP_cipher_get_params_fn alg##_##kbits##_##lcmode##_get_params;     \
96 static int alg##_##kbits##_##lcmode##_get_params(OSSL_PARAM params[])          \
97 {                                                                              \
98     return cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE, flags,  \
99                                      kbits, blkbits, ivbits);                  \
100 }                                                                              \
101 static OSSL_OP_cipher_newctx_fn alg##_##kbits##_##lcmode##_newctx;             \
102 static void * alg##_##kbits##_##lcmode##_newctx(void *provctx)                 \
103 {                                                                              \
104      PROV_##UCALG##_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));                   \
105      if (ctx != NULL) {                                                        \
106          cipher_generic_initkey(ctx, kbits, blkbits, ivbits,                   \
107                                 EVP_CIPH_##UCMODE##_MODE, flags,               \
108                                 PROV_CIPHER_HW_##alg##_##lcmode(kbits), NULL); \
109          ctx->rounds = RC5_12_ROUNDS;                                          \
110      }                                                                         \
111      return ctx;                                                               \
112 }                                                                              \
113 const OSSL_DISPATCH alg##kbits##lcmode##_functions[] = {                       \
114     { OSSL_FUNC_CIPHER_NEWCTX,                                                 \
115       (void (*)(void)) alg##_##kbits##_##lcmode##_newctx },                    \
116     { OSSL_FUNC_CIPHER_FREECTX, (void (*)(void)) alg##_freectx },              \
117     { OSSL_FUNC_CIPHER_DUPCTX, (void (*)(void)) alg##_dupctx },                \
118     { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))cipher_generic_einit },   \
119     { OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))cipher_generic_dinit },   \
120     { OSSL_FUNC_CIPHER_UPDATE, (void (*)(void))cipher_generic_##typ##_update },\
121     { OSSL_FUNC_CIPHER_FINAL, (void (*)(void))cipher_generic_##typ##_final },  \
122     { OSSL_FUNC_CIPHER_CIPHER, (void (*)(void))cipher_generic_cipher },        \
123     { OSSL_FUNC_CIPHER_GET_PARAMS,                                             \
124       (void (*)(void)) alg##_##kbits##_##lcmode##_get_params },                \
125     { OSSL_FUNC_CIPHER_GETTABLE_PARAMS,                                        \
126       (void (*)(void))cipher_generic_gettable_params },                        \
127     { OSSL_FUNC_CIPHER_GET_CTX_PARAMS,                                         \
128       (void (*)(void))rc5_get_ctx_params },                                    \
129     { OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS,                                    \
130       (void (*)(void))rc5_gettable_ctx_params },                               \
131     { OSSL_FUNC_CIPHER_SET_CTX_PARAMS,                                         \
132       (void (*)(void))rc5_set_ctx_params },                                    \
133     { OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS,                                    \
134      (void (*)(void))rc5_settable_ctx_params },                                \
135     { 0, NULL }                                                                \
136 };
137
138 /* rc5128ecb_functions */
139 IMPLEMENT_cipher(rc5, RC5, ecb, ECB, EVP_CIPH_VARIABLE_LENGTH, 128, 64, 0, block)
140 /* rc5128cbc_functions */
141 IMPLEMENT_cipher(rc5, RC5, cbc, CBC, EVP_CIPH_VARIABLE_LENGTH, 128, 64, 64, block)
142 /* rc5128ofb64_functions */
143 IMPLEMENT_cipher(rc5, RC5, ofb64, OFB, EVP_CIPH_VARIABLE_LENGTH, 128, 8, 64, stream)
144 /* rc5128cfb64_functions */
145 IMPLEMENT_cipher(rc5, RC5, cfb64,  CFB, EVP_CIPH_VARIABLE_LENGTH, 128, 8, 64, stream)