]>
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 | ||
65 | OPENSSL_clear_free(ctx, sizeof(*ctx)); | |
66 | } | |
67 | ||
68 | static const OSSL_PARAM rc4_hmac_md5_known_gettable_ctx_params[] = { | |
69 | OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_KEYLEN, NULL), | |
70 | OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_IVLEN, NULL), | |
71 | OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_AEAD_TLS1_AAD_PAD, NULL), | |
72 | OSSL_PARAM_END | |
73 | }; | |
74 | const OSSL_PARAM *rc4_hmac_md5_gettable_ctx_params(void) | |
75 | { | |
76 | return rc4_hmac_md5_known_gettable_ctx_params; | |
77 | } | |
78 | ||
79 | static int rc4_hmac_md5_get_ctx_params(void *vctx, OSSL_PARAM params[]) | |
80 | { | |
81 | PROV_RC4_HMAC_MD5_CTX *ctx = (PROV_RC4_HMAC_MD5_CTX *)vctx; | |
82 | OSSL_PARAM *p; | |
83 | ||
84 | p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_KEYLEN); | |
85 | if (p != NULL && !OSSL_PARAM_set_size_t(p, ctx->base.keylen)) { | |
86 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER); | |
87 | return 0; | |
88 | } | |
89 | ||
90 | p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_IVLEN); | |
91 | if (p != NULL && !OSSL_PARAM_set_size_t(p, ctx->base.ivlen)) { | |
92 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER); | |
93 | return 0; | |
94 | } | |
95 | p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_AEAD_TLS1_AAD_PAD); | |
96 | if (p != NULL && !OSSL_PARAM_set_size_t(p, ctx->tls_aad_pad_sz)) { | |
97 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER); | |
98 | return 0; | |
99 | } | |
100 | return 1; | |
101 | } | |
102 | ||
103 | static const OSSL_PARAM rc4_hmac_md5_known_settable_ctx_params[] = { | |
104 | OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_KEYLEN, NULL), | |
105 | OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_IVLEN, NULL), | |
106 | OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_AEAD_TLS1_AAD, NULL, 0), | |
107 | OSSL_PARAM_END | |
108 | }; | |
109 | const OSSL_PARAM *rc4_hmac_md5_settable_ctx_params(void) | |
110 | { | |
111 | return rc4_hmac_md5_known_settable_ctx_params; | |
112 | } | |
113 | ||
114 | static int rc4_hmac_md5_set_ctx_params(void *vctx, const OSSL_PARAM params[]) | |
115 | { | |
116 | PROV_RC4_HMAC_MD5_CTX *ctx = (PROV_RC4_HMAC_MD5_CTX *)vctx; | |
117 | const OSSL_PARAM *p; | |
118 | size_t sz; | |
119 | ||
120 | p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_KEYLEN); | |
121 | if (p != NULL) { | |
122 | if (!OSSL_PARAM_get_size_t(p, &sz)) { | |
123 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER); | |
124 | return 0; | |
125 | } | |
126 | if (ctx->base.keylen != sz) { | |
127 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH); | |
128 | return 0; | |
129 | } | |
130 | } | |
131 | ||
132 | p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_IVLEN); | |
133 | if (p != NULL) { | |
134 | if (!OSSL_PARAM_get_size_t(p, &sz)) { | |
135 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER); | |
136 | return 0; | |
137 | } | |
138 | if (ctx->base.ivlen != sz) { | |
139 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH); | |
140 | return 0; | |
141 | } | |
142 | } | |
143 | ||
144 | p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_AEAD_TLS1_AAD); | |
145 | if (p != NULL) { | |
146 | if (p->data_type != OSSL_PARAM_OCTET_STRING) { | |
147 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER); | |
148 | return 0; | |
149 | } | |
150 | sz = GET_HW(ctx)->tls_init(&ctx->base, p->data, p->data_size); | |
151 | if (sz == 0) { | |
152 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DATA); | |
153 | return 0; | |
154 | } | |
155 | ctx->tls_aad_pad_sz = sz; | |
156 | } | |
157 | p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_AEAD_TLS1_AAD); | |
158 | if (p != NULL) { | |
159 | if (p->data_type != OSSL_PARAM_OCTET_STRING) { | |
160 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER); | |
161 | return 0; | |
162 | } | |
163 | GET_HW(ctx)->init_mackey(&ctx->base, p->data, p->data_size); | |
164 | } | |
165 | return 1; | |
166 | } | |
167 | ||
168 | static int rc4_hmac_md5_get_params(OSSL_PARAM params[]) | |
169 | { | |
170 | return cipher_generic_get_params(params, RC4_HMAC_MD5_MODE, | |
171 | RC4_HMAC_MD5_FLAGS, | |
172 | RC4_HMAC_MD5_KEY_BITS, | |
173 | RC4_HMAC_MD5_BLOCK_BITS, | |
174 | RC4_HMAC_MD5_IV_BITS); | |
175 | } | |
176 | ||
177 | const OSSL_DISPATCH rc4_hmac_md5_functions[] = { | |
178 | { OSSL_FUNC_CIPHER_NEWCTX, (void (*)(void))rc4_hmac_md5_newctx }, | |
179 | { OSSL_FUNC_CIPHER_FREECTX, (void (*)(void))rc4_hmac_md5_freectx }, | |
180 | { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))rc4_hmac_md5_einit }, | |
181 | { OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))rc4_hmac_md5_dinit }, | |
182 | { OSSL_FUNC_CIPHER_UPDATE, (void (*)(void))rc4_hmac_md5_update }, | |
183 | { OSSL_FUNC_CIPHER_FINAL, (void (*)(void))rc4_hmac_md5_final }, | |
184 | { OSSL_FUNC_CIPHER_CIPHER, (void (*)(void))rc4_hmac_md5_cipher }, | |
185 | { OSSL_FUNC_CIPHER_GET_PARAMS, (void (*)(void))rc4_hmac_md5_get_params }, | |
186 | { OSSL_FUNC_CIPHER_GETTABLE_PARAMS, | |
187 | (void (*)(void))rc4_hmac_md5_gettable_params }, | |
188 | { OSSL_FUNC_CIPHER_GET_CTX_PARAMS, | |
189 | (void (*)(void))rc4_hmac_md5_get_ctx_params }, | |
190 | { OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS, | |
191 | (void (*)(void))rc4_hmac_md5_gettable_ctx_params }, | |
192 | { OSSL_FUNC_CIPHER_SET_CTX_PARAMS, | |
193 | (void (*)(void))rc4_hmac_md5_set_ctx_params }, | |
194 | { OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS, | |
195 | (void (*)(void))rc4_hmac_md5_settable_ctx_params }, | |
196 | { 0, NULL } | |
197 | }; |