]>
Commit | Line | Data |
---|---|---|
8fece335 | 1 | /* |
33388b44 | 2 | * Copyright 2019-2020 The OpenSSL Project Authors. All Rights Reserved. |
8fece335 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 RC4_HMAC_MD5 cipher */ | |
11 | ||
a8fca728 | 12 | /* |
781aa7ab P |
13 | * MD5 and RC4 low level APIs are deprecated for public use, but still ok for |
14 | * internal use. | |
a8fca728 P |
15 | */ |
16 | #include "internal/deprecated.h" | |
17 | ||
8fece335 SL |
18 | #include "cipher_rc4_hmac_md5.h" |
19 | #include "prov/implementations.h" | |
20 | #include "prov/providercommonerr.h" | |
21 | ||
22 | /* TODO(3.0) Figure out what flags are required */ | |
23 | #define RC4_HMAC_MD5_FLAGS (EVP_CIPH_STREAM_CIPHER | EVP_CIPH_VARIABLE_LENGTH \ | |
24 | | EVP_CIPH_FLAG_AEAD_CIPHER) | |
25 | ||
26 | #define RC4_HMAC_MD5_KEY_BITS (16 * 8) | |
27 | #define RC4_HMAC_MD5_BLOCK_BITS (1 * 8) | |
28 | #define RC4_HMAC_MD5_IV_BITS 0 | |
29 | #define RC4_HMAC_MD5_MODE 0 | |
30 | ||
31 | #define GET_HW(ctx) ((PROV_CIPHER_HW_RC4_HMAC_MD5 *)ctx->base.hw) | |
32 | ||
363b1e5d DMSP |
33 | static OSSL_FUNC_cipher_newctx_fn rc4_hmac_md5_newctx; |
34 | static OSSL_FUNC_cipher_freectx_fn rc4_hmac_md5_freectx; | |
35 | static OSSL_FUNC_cipher_get_ctx_params_fn rc4_hmac_md5_get_ctx_params; | |
36 | static OSSL_FUNC_cipher_gettable_ctx_params_fn rc4_hmac_md5_gettable_ctx_params; | |
37 | static OSSL_FUNC_cipher_set_ctx_params_fn rc4_hmac_md5_set_ctx_params; | |
38 | static OSSL_FUNC_cipher_settable_ctx_params_fn rc4_hmac_md5_settable_ctx_params; | |
39 | static OSSL_FUNC_cipher_get_params_fn rc4_hmac_md5_get_params; | |
8fece335 SL |
40 | #define rc4_hmac_md5_gettable_params cipher_generic_gettable_params |
41 | #define rc4_hmac_md5_einit cipher_generic_einit | |
42 | #define rc4_hmac_md5_dinit cipher_generic_dinit | |
43 | #define rc4_hmac_md5_update cipher_generic_stream_update | |
44 | #define rc4_hmac_md5_final cipher_generic_stream_final | |
45 | #define rc4_hmac_md5_cipher cipher_generic_cipher | |
46 | ||
47 | static void *rc4_hmac_md5_newctx(void *provctx) | |
48 | { | |
49 | PROV_RC4_HMAC_MD5_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx)); | |
50 | ||
51 | if (ctx != NULL) | |
52 | cipher_generic_initkey(ctx, RC4_HMAC_MD5_KEY_BITS, | |
53 | RC4_HMAC_MD5_BLOCK_BITS, | |
54 | RC4_HMAC_MD5_IV_BITS, | |
55 | RC4_HMAC_MD5_MODE, RC4_HMAC_MD5_FLAGS, | |
56 | PROV_CIPHER_HW_rc4_hmac_md5(RC4_HMAC_MD5_KEY_BITS), | |
57 | NULL); | |
58 | return ctx; | |
59 | } | |
60 | ||
61 | static void rc4_hmac_md5_freectx(void *vctx) | |
62 | { | |
63 | PROV_RC4_HMAC_MD5_CTX *ctx = (PROV_RC4_HMAC_MD5_CTX *)vctx; | |
64 | ||
63ee6ec1 | 65 | cipher_generic_reset_ctx((PROV_CIPHER_CTX *)vctx); |
8fece335 SL |
66 | OPENSSL_clear_free(ctx, sizeof(*ctx)); |
67 | } | |
68 | ||
69 | static const OSSL_PARAM rc4_hmac_md5_known_gettable_ctx_params[] = { | |
70 | OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_KEYLEN, NULL), | |
71 | OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_IVLEN, NULL), | |
72 | OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_AEAD_TLS1_AAD_PAD, NULL), | |
73 | OSSL_PARAM_END | |
74 | }; | |
af5e1e85 | 75 | const OSSL_PARAM *rc4_hmac_md5_gettable_ctx_params(void *provctx) |
8fece335 SL |
76 | { |
77 | return rc4_hmac_md5_known_gettable_ctx_params; | |
78 | } | |
79 | ||
80 | static int rc4_hmac_md5_get_ctx_params(void *vctx, OSSL_PARAM params[]) | |
81 | { | |
82 | PROV_RC4_HMAC_MD5_CTX *ctx = (PROV_RC4_HMAC_MD5_CTX *)vctx; | |
83 | OSSL_PARAM *p; | |
84 | ||
85 | p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_KEYLEN); | |
86 | if (p != NULL && !OSSL_PARAM_set_size_t(p, ctx->base.keylen)) { | |
87 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER); | |
88 | return 0; | |
89 | } | |
90 | ||
91 | p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_IVLEN); | |
92 | if (p != NULL && !OSSL_PARAM_set_size_t(p, ctx->base.ivlen)) { | |
93 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER); | |
94 | return 0; | |
95 | } | |
96 | p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_AEAD_TLS1_AAD_PAD); | |
97 | if (p != NULL && !OSSL_PARAM_set_size_t(p, ctx->tls_aad_pad_sz)) { | |
98 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER); | |
99 | return 0; | |
100 | } | |
101 | return 1; | |
102 | } | |
103 | ||
104 | static const OSSL_PARAM rc4_hmac_md5_known_settable_ctx_params[] = { | |
105 | OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_KEYLEN, NULL), | |
106 | OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_IVLEN, NULL), | |
107 | OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_AEAD_TLS1_AAD, NULL, 0), | |
108 | OSSL_PARAM_END | |
109 | }; | |
af5e1e85 | 110 | const OSSL_PARAM *rc4_hmac_md5_settable_ctx_params(void *provctx) |
8fece335 SL |
111 | { |
112 | return rc4_hmac_md5_known_settable_ctx_params; | |
113 | } | |
114 | ||
115 | static int rc4_hmac_md5_set_ctx_params(void *vctx, const OSSL_PARAM params[]) | |
116 | { | |
117 | PROV_RC4_HMAC_MD5_CTX *ctx = (PROV_RC4_HMAC_MD5_CTX *)vctx; | |
118 | const OSSL_PARAM *p; | |
119 | size_t sz; | |
120 | ||
121 | p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_KEYLEN); | |
122 | if (p != NULL) { | |
123 | if (!OSSL_PARAM_get_size_t(p, &sz)) { | |
124 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER); | |
125 | return 0; | |
126 | } | |
127 | if (ctx->base.keylen != sz) { | |
128 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH); | |
129 | return 0; | |
130 | } | |
131 | } | |
132 | ||
133 | p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_IVLEN); | |
134 | if (p != NULL) { | |
135 | if (!OSSL_PARAM_get_size_t(p, &sz)) { | |
136 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER); | |
137 | return 0; | |
138 | } | |
139 | if (ctx->base.ivlen != sz) { | |
140 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH); | |
141 | return 0; | |
142 | } | |
143 | } | |
144 | ||
145 | p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_AEAD_TLS1_AAD); | |
146 | if (p != NULL) { | |
147 | if (p->data_type != OSSL_PARAM_OCTET_STRING) { | |
148 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER); | |
149 | return 0; | |
150 | } | |
151 | sz = GET_HW(ctx)->tls_init(&ctx->base, p->data, p->data_size); | |
152 | if (sz == 0) { | |
153 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DATA); | |
154 | return 0; | |
155 | } | |
156 | ctx->tls_aad_pad_sz = sz; | |
157 | } | |
158 | p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_AEAD_TLS1_AAD); | |
159 | if (p != NULL) { | |
160 | if (p->data_type != OSSL_PARAM_OCTET_STRING) { | |
161 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER); | |
162 | return 0; | |
163 | } | |
164 | GET_HW(ctx)->init_mackey(&ctx->base, p->data, p->data_size); | |
165 | } | |
166 | return 1; | |
167 | } | |
168 | ||
169 | static int rc4_hmac_md5_get_params(OSSL_PARAM params[]) | |
170 | { | |
171 | return cipher_generic_get_params(params, RC4_HMAC_MD5_MODE, | |
172 | RC4_HMAC_MD5_FLAGS, | |
173 | RC4_HMAC_MD5_KEY_BITS, | |
174 | RC4_HMAC_MD5_BLOCK_BITS, | |
175 | RC4_HMAC_MD5_IV_BITS); | |
176 | } | |
177 | ||
178 | const OSSL_DISPATCH rc4_hmac_md5_functions[] = { | |
179 | { OSSL_FUNC_CIPHER_NEWCTX, (void (*)(void))rc4_hmac_md5_newctx }, | |
180 | { OSSL_FUNC_CIPHER_FREECTX, (void (*)(void))rc4_hmac_md5_freectx }, | |
181 | { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))rc4_hmac_md5_einit }, | |
182 | { OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))rc4_hmac_md5_dinit }, | |
183 | { OSSL_FUNC_CIPHER_UPDATE, (void (*)(void))rc4_hmac_md5_update }, | |
184 | { OSSL_FUNC_CIPHER_FINAL, (void (*)(void))rc4_hmac_md5_final }, | |
185 | { OSSL_FUNC_CIPHER_CIPHER, (void (*)(void))rc4_hmac_md5_cipher }, | |
186 | { OSSL_FUNC_CIPHER_GET_PARAMS, (void (*)(void))rc4_hmac_md5_get_params }, | |
187 | { OSSL_FUNC_CIPHER_GETTABLE_PARAMS, | |
188 | (void (*)(void))rc4_hmac_md5_gettable_params }, | |
189 | { OSSL_FUNC_CIPHER_GET_CTX_PARAMS, | |
190 | (void (*)(void))rc4_hmac_md5_get_ctx_params }, | |
191 | { OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS, | |
192 | (void (*)(void))rc4_hmac_md5_gettable_ctx_params }, | |
193 | { OSSL_FUNC_CIPHER_SET_CTX_PARAMS, | |
194 | (void (*)(void))rc4_hmac_md5_set_ctx_params }, | |
195 | { OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS, | |
196 | (void (*)(void))rc4_hmac_md5_settable_ctx_params }, | |
197 | { 0, NULL } | |
198 | }; |