]>
Commit | Line | Data |
---|---|---|
e3f3ee44 | 1 | /* |
33388b44 | 2 | * Copyright 2019-2020 The OpenSSL Project Authors. All Rights Reserved. |
e3f3ee44 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 | ||
c6fec81b P |
10 | /* |
11 | * DES low level APIs are deprecated for public use, but still ok for internal | |
12 | * use. | |
13 | */ | |
14 | #include "internal/deprecated.h" | |
15 | ||
604e884b | 16 | #include "prov/ciphercommon.h" |
e3f3ee44 | 17 | #include "cipher_des.h" |
993ebac9 | 18 | #include <openssl/rand.h> |
af3e7e1b | 19 | #include "prov/implementations.h" |
f99d3eed | 20 | #include "prov/providercommon.h" |
ddd21319 | 21 | #include "prov/providercommonerr.h" |
e3f3ee44 SL |
22 | |
23 | /* TODO(3.0) Figure out what flags need to be here */ | |
24 | #define DES_FLAGS (EVP_CIPH_RAND_KEY) | |
25 | ||
363b1e5d DMSP |
26 | static OSSL_FUNC_cipher_freectx_fn des_freectx; |
27 | static OSSL_FUNC_cipher_encrypt_init_fn des_einit; | |
28 | static OSSL_FUNC_cipher_decrypt_init_fn des_dinit; | |
29 | static OSSL_FUNC_cipher_get_ctx_params_fn des_get_ctx_params; | |
30 | static OSSL_FUNC_cipher_gettable_ctx_params_fn des_gettable_ctx_params; | |
e3f3ee44 SL |
31 | |
32 | static void *des_newctx(void *provctx, size_t kbits, size_t blkbits, | |
33 | size_t ivbits, unsigned int mode, uint64_t flags, | |
34 | const PROV_CIPHER_HW *hw) | |
35 | { | |
f99d3eed | 36 | PROV_DES_CTX *ctx; |
e3f3ee44 | 37 | |
f99d3eed P |
38 | if (!ossl_prov_is_running()) |
39 | return NULL; | |
40 | ||
41 | ctx = OPENSSL_zalloc(sizeof(*ctx)); | |
e3f3ee44 SL |
42 | if (ctx != NULL) |
43 | cipher_generic_initkey(ctx, kbits, blkbits, ivbits, mode, flags, hw, | |
44 | provctx); | |
45 | return ctx; | |
46 | } | |
47 | ||
abfc73f3 PS |
48 | static void *des_dupctx(void *ctx) |
49 | { | |
50 | PROV_DES_CTX *in = (PROV_DES_CTX *)ctx; | |
f99d3eed | 51 | PROV_DES_CTX *ret; |
abfc73f3 | 52 | |
f99d3eed P |
53 | if (!ossl_prov_is_running()) |
54 | return NULL; | |
55 | ||
56 | ret = OPENSSL_malloc(sizeof(*ret)); | |
abfc73f3 PS |
57 | if (ret == NULL) { |
58 | ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE); | |
59 | return NULL; | |
60 | } | |
61 | in->base.hw->copyctx(&ret->base, &in->base); | |
62 | ||
63 | return ret; | |
64 | } | |
65 | ||
e3f3ee44 SL |
66 | static void des_freectx(void *vctx) |
67 | { | |
089cb623 | 68 | PROV_DES_CTX *ctx = (PROV_DES_CTX *)vctx; |
e3f3ee44 | 69 | |
63ee6ec1 | 70 | cipher_generic_reset_ctx((PROV_CIPHER_CTX *)vctx); |
e3f3ee44 SL |
71 | OPENSSL_clear_free(ctx, sizeof(*ctx)); |
72 | } | |
73 | ||
74 | static int des_init(void *vctx, const unsigned char *key, size_t keylen, | |
75 | const unsigned char *iv, size_t ivlen, int enc) | |
76 | { | |
77 | PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx; | |
78 | ||
f99d3eed P |
79 | if (!ossl_prov_is_running()) |
80 | return 0; | |
81 | ||
90409da6 | 82 | ctx->num = 0; |
914f97ee | 83 | ctx->bufsz = 0; |
e3f3ee44 SL |
84 | ctx->enc = enc; |
85 | ||
86 | if (iv != NULL) { | |
089cb623 | 87 | if (!cipher_generic_initiv(ctx, iv, ivlen)) |
e3f3ee44 | 88 | return 0; |
e3f3ee44 SL |
89 | } |
90 | ||
91 | if (key != NULL) { | |
92 | if (keylen != ctx->keylen) { | |
93 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEYLEN); | |
94 | return 0; | |
95 | } | |
96 | return ctx->hw->init(ctx, key, keylen); | |
97 | } | |
98 | return 1; | |
99 | } | |
100 | ||
101 | static int des_einit(void *vctx, const unsigned char *key, size_t keylen, | |
102 | const unsigned char *iv, size_t ivlen) | |
103 | { | |
104 | return des_init(vctx, key, keylen, iv, ivlen, 1); | |
105 | } | |
106 | ||
107 | static int des_dinit(void *vctx, const unsigned char *key, size_t keylen, | |
108 | const unsigned char *iv, size_t ivlen) | |
109 | { | |
110 | return des_init(vctx, key, keylen, iv, ivlen, 0); | |
111 | } | |
112 | ||
113 | static int des_generatekey(PROV_CIPHER_CTX *ctx, void *ptr) | |
114 | { | |
115 | ||
116 | DES_cblock *deskey = ptr; | |
117 | size_t kl = ctx->keylen; | |
118 | ||
993ebac9 | 119 | if (kl == 0 || RAND_priv_bytes_ex(ctx->libctx, ptr, kl) <= 0) |
e3f3ee44 SL |
120 | return 0; |
121 | DES_set_odd_parity(deskey); | |
122 | return 1; | |
123 | } | |
124 | ||
125 | CIPHER_DEFAULT_GETTABLE_CTX_PARAMS_START(des) | |
126 | OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_RANDOM_KEY, NULL, 0), | |
127 | CIPHER_DEFAULT_GETTABLE_CTX_PARAMS_END(des) | |
128 | ||
129 | static int des_get_ctx_params(void *vctx, OSSL_PARAM params[]) | |
130 | { | |
131 | PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx; | |
132 | OSSL_PARAM *p; | |
133 | ||
134 | if (!cipher_generic_get_ctx_params(vctx, params)) | |
135 | return 0; | |
136 | ||
137 | p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_RANDOM_KEY); | |
138 | if (p != NULL && !des_generatekey(ctx, p->data)) { | |
139 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GENERATE_KEY); | |
140 | return 0; | |
141 | } | |
142 | return 1; | |
143 | } | |
144 | ||
145 | #define IMPLEMENT_des_cipher(type, lcmode, UCMODE, flags, \ | |
146 | kbits, blkbits, ivbits, block) \ | |
363b1e5d | 147 | static OSSL_FUNC_cipher_newctx_fn type##_##lcmode##_newctx; \ |
e3f3ee44 SL |
148 | static void *des_##lcmode##_newctx(void *provctx) \ |
149 | { \ | |
150 | return des_newctx(provctx, kbits, blkbits, ivbits, \ | |
151 | EVP_CIPH_##UCMODE##_MODE, flags, \ | |
152 | PROV_CIPHER_HW_des_##lcmode()); \ | |
153 | } \ | |
363b1e5d | 154 | static OSSL_FUNC_cipher_get_params_fn des_##lcmode##_get_params; \ |
e3f3ee44 SL |
155 | static int des_##lcmode##_get_params(OSSL_PARAM params[]) \ |
156 | { \ | |
157 | return cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE, flags, \ | |
158 | kbits, blkbits, ivbits); \ | |
159 | } \ | |
1be63951 | 160 | const OSSL_DISPATCH ossl_##des_##lcmode##_functions[] = { \ |
e3f3ee44 SL |
161 | { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))des_einit }, \ |
162 | { OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))des_dinit }, \ | |
163 | { OSSL_FUNC_CIPHER_UPDATE, \ | |
164 | (void (*)(void))cipher_generic_##block##_update }, \ | |
165 | { OSSL_FUNC_CIPHER_FINAL, (void (*)(void))cipher_generic_##block##_final },\ | |
166 | { OSSL_FUNC_CIPHER_CIPHER, (void (*)(void))cipher_generic_cipher }, \ | |
167 | { OSSL_FUNC_CIPHER_NEWCTX, \ | |
168 | (void (*)(void))des_##lcmode##_newctx }, \ | |
abfc73f3 | 169 | { OSSL_FUNC_CIPHER_DUPCTX, (void (*)(void))des_dupctx }, \ |
e3f3ee44 SL |
170 | { OSSL_FUNC_CIPHER_FREECTX, (void (*)(void))des_freectx }, \ |
171 | { OSSL_FUNC_CIPHER_GET_PARAMS, \ | |
172 | (void (*)(void))des_##lcmode##_get_params }, \ | |
173 | { OSSL_FUNC_CIPHER_GETTABLE_PARAMS, \ | |
174 | (void (*)(void))cipher_generic_gettable_params }, \ | |
175 | { OSSL_FUNC_CIPHER_GET_CTX_PARAMS, (void (*)(void))des_get_ctx_params }, \ | |
176 | { OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS, \ | |
177 | (void (*)(void))des_gettable_ctx_params }, \ | |
178 | { OSSL_FUNC_CIPHER_SET_CTX_PARAMS, \ | |
179 | (void (*)(void))cipher_generic_set_ctx_params }, \ | |
180 | { OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS, \ | |
181 | (void (*)(void))cipher_generic_settable_ctx_params }, \ | |
182 | { 0, NULL } \ | |
183 | } | |
184 | ||
1be63951 | 185 | /* ossl_des_ecb_functions */ |
e3f3ee44 | 186 | IMPLEMENT_des_cipher(des, ecb, ECB, DES_FLAGS, 64, 64, 0, block); |
1be63951 | 187 | /* ossl_des_cbc_functions */ |
e3f3ee44 | 188 | IMPLEMENT_des_cipher(des, cbc, CBC, DES_FLAGS, 64, 64, 64, block); |
1be63951 | 189 | /* ossl_des_ofb64_functions */ |
e3f3ee44 | 190 | IMPLEMENT_des_cipher(des, ofb64, OFB, DES_FLAGS, 64, 8, 64, stream); |
1be63951 | 191 | /* ossl_des_cfb64_functions */ |
e3f3ee44 | 192 | IMPLEMENT_des_cipher(des, cfb64, CFB, DES_FLAGS, 64, 8, 64, stream); |
1be63951 | 193 | /* ossl_des_cfb1_functions */ |
e3f3ee44 | 194 | IMPLEMENT_des_cipher(des, cfb1, CFB, DES_FLAGS, 64, 8, 64, stream); |
1be63951 | 195 | /* ossl_des_cfb8_functions */ |
e3f3ee44 | 196 | IMPLEMENT_des_cipher(des, cfb8, CFB, DES_FLAGS, 64, 8, 64, stream); |