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