]>
Commit | Line | Data |
---|---|---|
e3f3ee44 SL |
1 | /* |
2 | * Copyright 2019 The OpenSSL Project Authors. All Rights Reserved. | |
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 | #include "cipher_locl.h" | |
11 | #include "cipher_des.h" | |
25f2138b | 12 | #include "crypto/rand.h" |
e3f3ee44 SL |
13 | #include "internal/provider_algs.h" |
14 | #include "internal/providercommonerr.h" | |
15 | ||
16 | /* TODO(3.0) Figure out what flags need to be here */ | |
17 | #define DES_FLAGS (EVP_CIPH_RAND_KEY) | |
18 | ||
19 | static OSSL_OP_cipher_freectx_fn des_freectx; | |
20 | static OSSL_OP_cipher_encrypt_init_fn des_einit; | |
21 | static OSSL_OP_cipher_decrypt_init_fn des_dinit; | |
22 | static OSSL_OP_cipher_get_ctx_params_fn des_get_ctx_params; | |
23 | static OSSL_OP_cipher_gettable_ctx_params_fn des_gettable_ctx_params; | |
24 | ||
25 | static void *des_newctx(void *provctx, size_t kbits, size_t blkbits, | |
26 | size_t ivbits, unsigned int mode, uint64_t flags, | |
27 | const PROV_CIPHER_HW *hw) | |
28 | { | |
29 | PROV_DES_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx)); | |
30 | ||
31 | if (ctx != NULL) | |
32 | cipher_generic_initkey(ctx, kbits, blkbits, ivbits, mode, flags, hw, | |
33 | provctx); | |
34 | return ctx; | |
35 | } | |
36 | ||
37 | static void des_freectx(void *vctx) | |
38 | { | |
39 | PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx; | |
40 | ||
41 | OPENSSL_clear_free(ctx, sizeof(*ctx)); | |
42 | } | |
43 | ||
44 | static int des_init(void *vctx, const unsigned char *key, size_t keylen, | |
45 | const unsigned char *iv, size_t ivlen, int enc) | |
46 | { | |
47 | PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx; | |
48 | ||
49 | ctx->enc = enc; | |
50 | ||
51 | if (iv != NULL) { | |
52 | if (ivlen != ctx->ivlen) { | |
53 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IVLEN); | |
54 | return 0; | |
55 | } | |
56 | memcpy(ctx->iv, iv, ivlen); | |
57 | } | |
58 | ||
59 | if (key != NULL) { | |
60 | if (keylen != ctx->keylen) { | |
61 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEYLEN); | |
62 | return 0; | |
63 | } | |
64 | return ctx->hw->init(ctx, key, keylen); | |
65 | } | |
66 | return 1; | |
67 | } | |
68 | ||
69 | static int des_einit(void *vctx, const unsigned char *key, size_t keylen, | |
70 | const unsigned char *iv, size_t ivlen) | |
71 | { | |
72 | return des_init(vctx, key, keylen, iv, ivlen, 1); | |
73 | } | |
74 | ||
75 | static int des_dinit(void *vctx, const unsigned char *key, size_t keylen, | |
76 | const unsigned char *iv, size_t ivlen) | |
77 | { | |
78 | return des_init(vctx, key, keylen, iv, ivlen, 0); | |
79 | } | |
80 | ||
81 | static int des_generatekey(PROV_CIPHER_CTX *ctx, void *ptr) | |
82 | { | |
83 | ||
84 | DES_cblock *deskey = ptr; | |
85 | size_t kl = ctx->keylen; | |
86 | ||
87 | if (kl == 0 || rand_priv_bytes_ex(ctx->libctx, ptr, kl) <= 0) | |
88 | return 0; | |
89 | DES_set_odd_parity(deskey); | |
90 | return 1; | |
91 | } | |
92 | ||
93 | CIPHER_DEFAULT_GETTABLE_CTX_PARAMS_START(des) | |
94 | OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_RANDOM_KEY, NULL, 0), | |
95 | CIPHER_DEFAULT_GETTABLE_CTX_PARAMS_END(des) | |
96 | ||
97 | static int des_get_ctx_params(void *vctx, OSSL_PARAM params[]) | |
98 | { | |
99 | PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx; | |
100 | OSSL_PARAM *p; | |
101 | ||
102 | if (!cipher_generic_get_ctx_params(vctx, params)) | |
103 | return 0; | |
104 | ||
105 | p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_RANDOM_KEY); | |
106 | if (p != NULL && !des_generatekey(ctx, p->data)) { | |
107 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GENERATE_KEY); | |
108 | return 0; | |
109 | } | |
110 | return 1; | |
111 | } | |
112 | ||
113 | #define IMPLEMENT_des_cipher(type, lcmode, UCMODE, flags, \ | |
114 | kbits, blkbits, ivbits, block) \ | |
115 | static OSSL_OP_cipher_newctx_fn type##_##lcmode##_newctx; \ | |
116 | static void *des_##lcmode##_newctx(void *provctx) \ | |
117 | { \ | |
118 | return des_newctx(provctx, kbits, blkbits, ivbits, \ | |
119 | EVP_CIPH_##UCMODE##_MODE, flags, \ | |
120 | PROV_CIPHER_HW_des_##lcmode()); \ | |
121 | } \ | |
122 | static OSSL_OP_cipher_get_params_fn des_##lcmode##_get_params; \ | |
123 | static int des_##lcmode##_get_params(OSSL_PARAM params[]) \ | |
124 | { \ | |
125 | return cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE, flags, \ | |
126 | kbits, blkbits, ivbits); \ | |
127 | } \ | |
128 | const OSSL_DISPATCH des_##lcmode##_functions[] = { \ | |
129 | { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))des_einit }, \ | |
130 | { OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))des_dinit }, \ | |
131 | { OSSL_FUNC_CIPHER_UPDATE, \ | |
132 | (void (*)(void))cipher_generic_##block##_update }, \ | |
133 | { OSSL_FUNC_CIPHER_FINAL, (void (*)(void))cipher_generic_##block##_final },\ | |
134 | { OSSL_FUNC_CIPHER_CIPHER, (void (*)(void))cipher_generic_cipher }, \ | |
135 | { OSSL_FUNC_CIPHER_NEWCTX, \ | |
136 | (void (*)(void))des_##lcmode##_newctx }, \ | |
137 | { OSSL_FUNC_CIPHER_FREECTX, (void (*)(void))des_freectx }, \ | |
138 | { OSSL_FUNC_CIPHER_GET_PARAMS, \ | |
139 | (void (*)(void))des_##lcmode##_get_params }, \ | |
140 | { OSSL_FUNC_CIPHER_GETTABLE_PARAMS, \ | |
141 | (void (*)(void))cipher_generic_gettable_params }, \ | |
142 | { OSSL_FUNC_CIPHER_GET_CTX_PARAMS, (void (*)(void))des_get_ctx_params }, \ | |
143 | { OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS, \ | |
144 | (void (*)(void))des_gettable_ctx_params }, \ | |
145 | { OSSL_FUNC_CIPHER_SET_CTX_PARAMS, \ | |
146 | (void (*)(void))cipher_generic_set_ctx_params }, \ | |
147 | { OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS, \ | |
148 | (void (*)(void))cipher_generic_settable_ctx_params }, \ | |
149 | { 0, NULL } \ | |
150 | } | |
151 | ||
152 | /* des_ecb_functions */ | |
153 | IMPLEMENT_des_cipher(des, ecb, ECB, DES_FLAGS, 64, 64, 0, block); | |
154 | /* des_cbc_functions */ | |
155 | IMPLEMENT_des_cipher(des, cbc, CBC, DES_FLAGS, 64, 64, 64, block); | |
156 | /* des_ofb64_functions */ | |
157 | IMPLEMENT_des_cipher(des, ofb64, OFB, DES_FLAGS, 64, 8, 64, stream); | |
158 | /* des_cfb64_functions */ | |
159 | IMPLEMENT_des_cipher(des, cfb64, CFB, DES_FLAGS, 64, 8, 64, stream); | |
160 | /* des_cfb1_functions */ | |
161 | IMPLEMENT_des_cipher(des, cfb1, CFB, DES_FLAGS, 64, 8, 64, stream); | |
162 | /* des_cfb8_functions */ | |
163 | IMPLEMENT_des_cipher(des, cfb8, CFB, DES_FLAGS, 64, 8, 64, stream); |