Deprecate the low level DES functions.
[openssl.git] / providers / implementations / ciphers / cipher_tdes.c
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 /*
11  * DES low level APIs are deprecated for public use, but still ok for internal
12  * use.
13  */
14 #include "internal/deprecated.h"
15
16 #include "prov/ciphercommon.h"
17 #include "cipher_tdes.h"
18 #include <openssl/rand.h>
19 #include "prov/implementations.h"
20 #include "prov/providercommonerr.h"
21
22 void *tdes_newctx(void *provctx, int mode, size_t kbits, size_t blkbits,
23                   size_t ivbits, uint64_t flags, const PROV_CIPHER_HW *hw)
24 {
25     PROV_TDES_CTX *tctx = OPENSSL_zalloc(sizeof(*tctx));
26
27     if (tctx != NULL)
28         cipher_generic_initkey(tctx, kbits, blkbits, ivbits, mode, flags, hw,
29                                provctx);
30     return tctx;
31 }
32
33 void tdes_freectx(void *vctx)
34 {
35     PROV_TDES_CTX *ctx = (PROV_TDES_CTX *)vctx;
36
37     OPENSSL_clear_free(ctx,  sizeof(*ctx));
38 }
39
40 static int tdes_init(void *vctx, const unsigned char *key, size_t keylen,
41                      const unsigned char *iv, size_t ivlen, int enc)
42 {
43     PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
44
45     ctx->enc = enc;
46
47     if (iv != NULL) {
48         if (!cipher_generic_initiv(ctx, iv, ivlen))
49             return 0;
50     }
51
52     if (key != NULL) {
53         if (keylen != ctx->keylen) {
54             ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEYLEN);
55             return 0;
56         }
57         return ctx->hw->init(ctx, key, ctx->keylen);
58     }
59     return 1;
60 }
61
62 int tdes_einit(void *vctx, const unsigned char *key, size_t keylen,
63                const unsigned char *iv, size_t ivlen)
64 {
65     return tdes_init(vctx, key, keylen, iv, ivlen, 1);
66 }
67
68 int tdes_dinit(void *vctx, const unsigned char *key, size_t keylen,
69                const unsigned char *iv, size_t ivlen)
70 {
71     return tdes_init(vctx, key, keylen, iv, ivlen, 0);
72 }
73
74 static int tdes_generatekey(PROV_CIPHER_CTX *ctx, void *ptr)
75 {
76
77     DES_cblock *deskey = ptr;
78     size_t kl = ctx->keylen;
79
80     if (kl == 0 || RAND_priv_bytes_ex(ctx->libctx, ptr, kl) <= 0)
81         return 0;
82     DES_set_odd_parity(deskey);
83     if (kl >= 16)
84         DES_set_odd_parity(deskey + 1);
85     if (kl >= 24) {
86         DES_set_odd_parity(deskey + 2);
87         return 1;
88     }
89     return 0;
90 }
91
92 CIPHER_DEFAULT_GETTABLE_CTX_PARAMS_START(tdes)
93     OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_RANDOM_KEY, NULL, 0),
94 CIPHER_DEFAULT_GETTABLE_CTX_PARAMS_END(tdes)
95
96 int tdes_get_ctx_params(void *vctx, OSSL_PARAM params[])
97 {
98     PROV_CIPHER_CTX  *ctx = (PROV_CIPHER_CTX *)vctx;
99     OSSL_PARAM *p;
100
101     if (!cipher_generic_get_ctx_params(vctx, params))
102         return 0;
103
104     p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_RANDOM_KEY);
105     if (p != NULL && !tdes_generatekey(ctx, p->data)) {
106         ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GENERATE_KEY);
107         return 0;
108     }
109     return 1;
110 }
111
112 /*
113  * TODO(3.0) - ECB mode does not use an IV - but existing test code is setting
114  * an IV. Fixing this could potentially make applications break.
115  */
116
117 /* tdes_ede3_ecb_functions */
118 IMPLEMENT_tdes_cipher(ede3, EDE3, ecb, ECB, TDES_FLAGS, 64*3, 64, 64, block);
119 /* tdes_ede3_cbc_functions */
120 IMPLEMENT_tdes_cipher(ede3, EDE3, cbc, CBC, TDES_FLAGS, 64*3, 64, 64, block);