]>
Commit | Line | Data |
---|---|---|
f5056577 | 1 | /* |
da1c088f | 2 | * Copyright 2019-2023 The OpenSSL Project Authors. All Rights Reserved. |
f5056577 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 | /* | |
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> | |
f5056577 SL |
18 | #include "prov/ciphercommon.h" |
19 | #include "cipher_tdes.h" | |
f5056577 | 20 | #include "prov/implementations.h" |
f99d3eed | 21 | #include "prov/providercommon.h" |
f5056577 | 22 | |
e36b3c2f SL |
23 | void *ossl_tdes_newctx(void *provctx, int mode, size_t kbits, size_t blkbits, |
24 | size_t ivbits, uint64_t flags, const PROV_CIPHER_HW *hw) | |
f5056577 | 25 | { |
f99d3eed | 26 | PROV_TDES_CTX *tctx; |
f5056577 | 27 | |
f99d3eed P |
28 | if (!ossl_prov_is_running()) |
29 | return NULL; | |
30 | ||
31 | tctx = OPENSSL_zalloc(sizeof(*tctx)); | |
f5056577 | 32 | if (tctx != NULL) |
592dcfd3 P |
33 | ossl_cipher_generic_initkey(tctx, kbits, blkbits, ivbits, mode, flags, |
34 | hw, provctx); | |
f5056577 SL |
35 | return tctx; |
36 | } | |
37 | ||
e36b3c2f | 38 | void *ossl_tdes_dupctx(void *ctx) |
abfc73f3 PS |
39 | { |
40 | PROV_TDES_CTX *in = (PROV_TDES_CTX *)ctx; | |
f99d3eed | 41 | PROV_TDES_CTX *ret; |
abfc73f3 | 42 | |
f99d3eed P |
43 | if (!ossl_prov_is_running()) |
44 | return NULL; | |
45 | ||
46 | ret = OPENSSL_malloc(sizeof(*ret)); | |
e077455e | 47 | if (ret == NULL) |
abfc73f3 | 48 | return NULL; |
abfc73f3 PS |
49 | in->base.hw->copyctx(&ret->base, &in->base); |
50 | ||
51 | return ret; | |
52 | } | |
53 | ||
e36b3c2f | 54 | void ossl_tdes_freectx(void *vctx) |
f5056577 SL |
55 | { |
56 | PROV_TDES_CTX *ctx = (PROV_TDES_CTX *)vctx; | |
57 | ||
592dcfd3 | 58 | ossl_cipher_generic_reset_ctx((PROV_CIPHER_CTX *)vctx); |
f5056577 SL |
59 | OPENSSL_clear_free(ctx, sizeof(*ctx)); |
60 | } | |
61 | ||
62 | static int tdes_init(void *vctx, const unsigned char *key, size_t keylen, | |
deee9672 P |
63 | const unsigned char *iv, size_t ivlen, |
64 | const OSSL_PARAM params[], int enc) | |
f5056577 SL |
65 | { |
66 | PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx; | |
67 | ||
f99d3eed P |
68 | if (!ossl_prov_is_running()) |
69 | return 0; | |
70 | ||
90409da6 | 71 | ctx->num = 0; |
914f97ee | 72 | ctx->bufsz = 0; |
f5056577 SL |
73 | ctx->enc = enc; |
74 | ||
75 | if (iv != NULL) { | |
592dcfd3 | 76 | if (!ossl_cipher_generic_initiv(ctx, iv, ivlen)) |
f5056577 | 77 | return 0; |
d6c6f6c5 TM |
78 | } else if (ctx->iv_set |
79 | && (ctx->mode == EVP_CIPH_CBC_MODE | |
80 | || ctx->mode == EVP_CIPH_CFB_MODE | |
81 | || ctx->mode == EVP_CIPH_OFB_MODE)) { | |
82 | /* reset IV to keep compatibility with 1.1.1 */ | |
83 | memcpy(ctx->iv, ctx->oiv, ctx->ivlen); | |
f5056577 SL |
84 | } |
85 | ||
86 | if (key != NULL) { | |
87 | if (keylen != ctx->keylen) { | |
f5f29796 | 88 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH); |
f5056577 SL |
89 | return 0; |
90 | } | |
deee9672 P |
91 | if (!ctx->hw->init(ctx, key, ctx->keylen)) |
92 | return 0; | |
3a95d1e4 | 93 | ctx->key_set = 1; |
f5056577 | 94 | } |
deee9672 | 95 | return ossl_cipher_generic_set_ctx_params(ctx, params); |
f5056577 SL |
96 | } |
97 | ||
e36b3c2f | 98 | int ossl_tdes_einit(void *vctx, const unsigned char *key, size_t keylen, |
deee9672 P |
99 | const unsigned char *iv, size_t ivlen, |
100 | const OSSL_PARAM params[]) | |
f5056577 | 101 | { |
deee9672 | 102 | return tdes_init(vctx, key, keylen, iv, ivlen, params, 1); |
f5056577 SL |
103 | } |
104 | ||
e36b3c2f | 105 | int ossl_tdes_dinit(void *vctx, const unsigned char *key, size_t keylen, |
deee9672 P |
106 | const unsigned char *iv, size_t ivlen, |
107 | const OSSL_PARAM params[]) | |
f5056577 | 108 | { |
deee9672 | 109 | return tdes_init(vctx, key, keylen, iv, ivlen, params, 0); |
f5056577 SL |
110 | } |
111 | ||
e36b3c2f | 112 | CIPHER_DEFAULT_GETTABLE_CTX_PARAMS_START(ossl_tdes) |
f5056577 | 113 | OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_RANDOM_KEY, NULL, 0), |
e36b3c2f | 114 | CIPHER_DEFAULT_GETTABLE_CTX_PARAMS_END(ossl_tdes) |
f5056577 SL |
115 | |
116 | static int tdes_generatekey(PROV_CIPHER_CTX *ctx, void *ptr) | |
117 | { | |
118 | ||
119 | DES_cblock *deskey = ptr; | |
120 | size_t kl = ctx->keylen; | |
121 | ||
965fa9c0 | 122 | if (kl == 0 || RAND_priv_bytes_ex(ctx->libctx, ptr, kl, 0) <= 0) |
f5056577 SL |
123 | return 0; |
124 | DES_set_odd_parity(deskey); | |
587e0407 | 125 | if (kl >= 16) { |
f5056577 | 126 | DES_set_odd_parity(deskey + 1); |
587e0407 P |
127 | if (kl >= 24) |
128 | DES_set_odd_parity(deskey + 2); | |
f5056577 | 129 | } |
587e0407 | 130 | return 1; |
f5056577 SL |
131 | } |
132 | ||
e36b3c2f | 133 | int ossl_tdes_get_ctx_params(void *vctx, OSSL_PARAM params[]) |
f5056577 SL |
134 | { |
135 | PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx; | |
136 | OSSL_PARAM *p; | |
137 | ||
592dcfd3 | 138 | if (!ossl_cipher_generic_get_ctx_params(vctx, params)) |
f5056577 SL |
139 | return 0; |
140 | ||
141 | p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_RANDOM_KEY); | |
142 | if (p != NULL && !tdes_generatekey(ctx, p->data)) { | |
143 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GENERATE_KEY); | |
144 | return 0; | |
145 | } | |
146 | return 1; | |
147 | } |