Add AES_CBC_CTS ciphers to providers
[openssl.git] / providers / implementations / ciphers / cipher_tdes_common.c
1 /*
2  * Copyright 2019-2020 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_dupctx(void *ctx)
34 {
35     PROV_TDES_CTX *in = (PROV_TDES_CTX *)ctx;
36     PROV_TDES_CTX *ret = OPENSSL_malloc(sizeof(*ret));
37
38     if (ret == NULL) {
39         ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
40         return NULL;
41     }
42     in->base.hw->copyctx(&ret->base, &in->base);
43
44     return ret;
45 }
46
47 void tdes_freectx(void *vctx)
48 {
49     PROV_TDES_CTX *ctx = (PROV_TDES_CTX *)vctx;
50
51     cipher_generic_reset_ctx((PROV_CIPHER_CTX *)vctx);
52     OPENSSL_clear_free(ctx,  sizeof(*ctx));
53 }
54
55 static int tdes_init(void *vctx, const unsigned char *key, size_t keylen,
56                      const unsigned char *iv, size_t ivlen, int enc)
57 {
58     PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
59
60     ctx->enc = enc;
61
62     if (iv != NULL) {
63         if (!cipher_generic_initiv(ctx, iv, ivlen))
64             return 0;
65     }
66
67     if (key != NULL) {
68         if (keylen != ctx->keylen) {
69             ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEYLEN);
70             return 0;
71         }
72         return ctx->hw->init(ctx, key, ctx->keylen);
73     }
74     return 1;
75 }
76
77 int tdes_einit(void *vctx, const unsigned char *key, size_t keylen,
78                const unsigned char *iv, size_t ivlen)
79 {
80     return tdes_init(vctx, key, keylen, iv, ivlen, 1);
81 }
82
83 int tdes_dinit(void *vctx, const unsigned char *key, size_t keylen,
84                const unsigned char *iv, size_t ivlen)
85 {
86     return tdes_init(vctx, key, keylen, iv, ivlen, 0);
87 }
88
89 CIPHER_DEFAULT_GETTABLE_CTX_PARAMS_START(tdes)
90     OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_RANDOM_KEY, NULL, 0),
91 CIPHER_DEFAULT_GETTABLE_CTX_PARAMS_END(tdes)
92
93 static int tdes_generatekey(PROV_CIPHER_CTX *ctx, void *ptr)
94 {
95
96     DES_cblock *deskey = ptr;
97     size_t kl = ctx->keylen;
98
99     if (kl == 0 || RAND_priv_bytes_ex(ctx->libctx, ptr, kl) <= 0)
100         return 0;
101     DES_set_odd_parity(deskey);
102     if (kl >= 16)
103         DES_set_odd_parity(deskey + 1);
104     if (kl >= 24) {
105         DES_set_odd_parity(deskey + 2);
106         return 1;
107     }
108     return 0;
109 }
110
111 int tdes_get_ctx_params(void *vctx, OSSL_PARAM params[])
112 {
113     PROV_CIPHER_CTX  *ctx = (PROV_CIPHER_CTX *)vctx;
114     OSSL_PARAM *p;
115
116     if (!cipher_generic_get_ctx_params(vctx, params))
117         return 0;
118
119     p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_RANDOM_KEY);
120     if (p != NULL && !tdes_generatekey(ctx, p->data)) {
121         ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GENERATE_KEY);
122         return 0;
123     }
124     return 1;
125 }