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