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