]> git.ipfire.org Git - thirdparty/openssl.git/blame - providers/implementations/ciphers/cipher_rc5.c
Update copyright year
[thirdparty/openssl.git] / providers / implementations / ciphers / cipher_rc5.c
CommitLineData
6a41156c 1/*
a28d06f3 2 * Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved.
6a41156c
SL
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
62c3fed0
P
12/*
13 * RC5 low level APIs are deprecated for public use, but still ok for internal
14 * use.
15 */
16#include "internal/deprecated.h"
17
2741128e 18#include <openssl/proverr.h>
6a41156c 19#include "cipher_rc5.h"
af3e7e1b 20#include "prov/implementations.h"
f99d3eed 21#include "prov/providercommon.h"
6a41156c 22
a054d15c
SL
23#define RC5_FLAGS PROV_CIPHER_FLAG_VARIABLE_LENGTH
24
363b1e5d
DMSP
25static OSSL_FUNC_cipher_freectx_fn rc5_freectx;
26static OSSL_FUNC_cipher_dupctx_fn rc5_dupctx;
27OSSL_FUNC_cipher_gettable_ctx_params_fn rc5_gettable_ctx_params;
28OSSL_FUNC_cipher_settable_ctx_params_fn rc5_settable_ctx_params;
6a41156c
SL
29
30static void rc5_freectx(void *vctx)
31{
32 PROV_RC5_CTX *ctx = (PROV_RC5_CTX *)vctx;
33
592dcfd3 34 ossl_cipher_generic_reset_ctx((PROV_CIPHER_CTX *)vctx);
6a41156c
SL
35 OPENSSL_clear_free(ctx, sizeof(*ctx));
36}
37
38static void *rc5_dupctx(void *ctx)
39{
40 PROV_RC5_CTX *in = (PROV_RC5_CTX *)ctx;
f99d3eed 41 PROV_RC5_CTX *ret;
6a41156c 42
f99d3eed
P
43 if (!ossl_prov_is_running())
44 return NULL;
45
46 ret = OPENSSL_malloc(sizeof(*ret));
6a41156c
SL
47 if (ret == NULL) {
48 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
49 return NULL;
50 }
51 *ret = *in;
52
53 return ret;
54}
55
56static int rc5_set_ctx_params(void *vctx, const OSSL_PARAM params[])
57{
58 PROV_RC5_CTX *ctx = (PROV_RC5_CTX *)vctx;
59 const OSSL_PARAM *p;
60
592dcfd3 61 if (!ossl_cipher_var_keylen_set_ctx_params(vctx, params))
6a41156c
SL
62 return 0;
63
64 p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_ROUNDS);
65 if (p != NULL) {
66 unsigned int rounds;
67
68 if (!OSSL_PARAM_get_uint(p, &rounds)) {
69 ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
70 return 0;
71 }
72 if (rounds != RC5_8_ROUNDS
73 && rounds != RC5_12_ROUNDS
74 && rounds != RC5_16_ROUNDS) {
75 ERR_raise(ERR_LIB_PROV, PROV_R_UNSUPPORTED_NUMBER_OF_ROUNDS);
76 return 0;
77 }
78 ctx->rounds = rounds;
79 }
80 return 1;
81}
82
83CIPHER_DEFAULT_GETTABLE_CTX_PARAMS_START(rc5)
84 OSSL_PARAM_uint(OSSL_CIPHER_PARAM_ROUNDS, NULL),
85CIPHER_DEFAULT_GETTABLE_CTX_PARAMS_END(rc5)
86
87CIPHER_DEFAULT_SETTABLE_CTX_PARAMS_START(rc5)
d23adad1 88 OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_KEYLEN, NULL),
6a41156c
SL
89 OSSL_PARAM_uint(OSSL_CIPHER_PARAM_ROUNDS, NULL),
90CIPHER_DEFAULT_SETTABLE_CTX_PARAMS_END(rc5)
91
92
93static int rc5_get_ctx_params(void *vctx, OSSL_PARAM params[])
94{
95 PROV_RC5_CTX *ctx = (PROV_RC5_CTX *)vctx;
96 OSSL_PARAM *p;
97
592dcfd3 98 if (!ossl_cipher_generic_get_ctx_params(vctx, params))
6a41156c
SL
99 return 0;
100 p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_ROUNDS);
101 if (p != NULL && !OSSL_PARAM_set_uint(p, ctx->rounds)) {
102 ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
103 return 0;
104 }
105 return 1;
106}
107
108#define IMPLEMENT_cipher(alg, UCALG, lcmode, UCMODE, flags, kbits, \
109 blkbits, ivbits, typ) \
363b1e5d 110static OSSL_FUNC_cipher_get_params_fn alg##_##kbits##_##lcmode##_get_params; \
6a41156c
SL
111static int alg##_##kbits##_##lcmode##_get_params(OSSL_PARAM params[]) \
112{ \
592dcfd3
P
113 return ossl_cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE, \
114 flags, kbits, blkbits, ivbits); \
6a41156c 115} \
363b1e5d 116static OSSL_FUNC_cipher_newctx_fn alg##_##kbits##_##lcmode##_newctx; \
6a41156c
SL
117static void * alg##_##kbits##_##lcmode##_newctx(void *provctx) \
118{ \
f99d3eed 119 PROV_##UCALG##_CTX *ctx; \
592dcfd3 120 if (!ossl_prov_is_running()) \
f99d3eed
P
121 return NULL; \
122 ctx = OPENSSL_zalloc(sizeof(*ctx)); \
6a41156c 123 if (ctx != NULL) { \
592dcfd3
P
124 ossl_cipher_generic_initkey(ctx, kbits, blkbits, ivbits, \
125 EVP_CIPH_##UCMODE##_MODE, flags, \
126 ossl_prov_cipher_hw_##alg##_##lcmode(kbits),\
7d6766cb 127 NULL); \
6a41156c
SL
128 ctx->rounds = RC5_12_ROUNDS; \
129 } \
130 return ctx; \
131} \
1be63951 132const OSSL_DISPATCH ossl_##alg##kbits##lcmode##_functions[] = { \
6a41156c
SL
133 { OSSL_FUNC_CIPHER_NEWCTX, \
134 (void (*)(void)) alg##_##kbits##_##lcmode##_newctx }, \
135 { OSSL_FUNC_CIPHER_FREECTX, (void (*)(void)) alg##_freectx }, \
136 { OSSL_FUNC_CIPHER_DUPCTX, (void (*)(void)) alg##_dupctx }, \
592dcfd3
P
137 { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))ossl_cipher_generic_einit },\
138 { OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))ossl_cipher_generic_dinit },\
139 { OSSL_FUNC_CIPHER_UPDATE, (void (*)(void))ossl_cipher_generic_##typ##_update },\
140 { OSSL_FUNC_CIPHER_FINAL, (void (*)(void))ossl_cipher_generic_##typ##_final }, \
141 { OSSL_FUNC_CIPHER_CIPHER, (void (*)(void))ossl_cipher_generic_cipher }, \
6a41156c
SL
142 { OSSL_FUNC_CIPHER_GET_PARAMS, \
143 (void (*)(void)) alg##_##kbits##_##lcmode##_get_params }, \
144 { OSSL_FUNC_CIPHER_GETTABLE_PARAMS, \
592dcfd3 145 (void (*)(void))ossl_cipher_generic_gettable_params }, \
6a41156c
SL
146 { OSSL_FUNC_CIPHER_GET_CTX_PARAMS, \
147 (void (*)(void))rc5_get_ctx_params }, \
148 { OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS, \
149 (void (*)(void))rc5_gettable_ctx_params }, \
150 { OSSL_FUNC_CIPHER_SET_CTX_PARAMS, \
151 (void (*)(void))rc5_set_ctx_params }, \
152 { OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS, \
153 (void (*)(void))rc5_settable_ctx_params }, \
154 { 0, NULL } \
155};
156
1be63951 157/* ossl_rc5128ecb_functions */
a054d15c 158IMPLEMENT_cipher(rc5, RC5, ecb, ECB, RC5_FLAGS, 128, 64, 0, block)
1be63951 159/* ossl_rc5128cbc_functions */
a054d15c 160IMPLEMENT_cipher(rc5, RC5, cbc, CBC, RC5_FLAGS, 128, 64, 64, block)
1be63951 161/* ossl_rc5128ofb64_functions */
a054d15c 162IMPLEMENT_cipher(rc5, RC5, ofb64, OFB, RC5_FLAGS, 128, 8, 64, stream)
1be63951 163/* ossl_rc5128cfb64_functions */
a054d15c 164IMPLEMENT_cipher(rc5, RC5, cfb64, CFB, RC5_FLAGS, 128, 8, 64, stream)