Update copyright year
[openssl.git] / providers / implementations / ciphers / cipher_tdes_common.c
1 /*
2  * Copyright 2019-2021 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 <openssl/rand.h>
17 #include <openssl/proverr.h>
18 #include "prov/ciphercommon.h"
19 #include "cipher_tdes.h"
20 #include "prov/implementations.h"
21 #include "prov/providercommon.h"
22
23 void *ossl_tdes_newctx(void *provctx, int mode, size_t kbits, size_t blkbits,
24                        size_t ivbits, uint64_t flags, const PROV_CIPHER_HW *hw)
25 {
26     PROV_TDES_CTX *tctx;
27
28     if (!ossl_prov_is_running())
29         return NULL;
30
31     tctx = OPENSSL_zalloc(sizeof(*tctx));
32     if (tctx != NULL)
33         ossl_cipher_generic_initkey(tctx, kbits, blkbits, ivbits, mode, flags,
34                                     hw, provctx);
35     return tctx;
36 }
37
38 void *ossl_tdes_dupctx(void *ctx)
39 {
40     PROV_TDES_CTX *in = (PROV_TDES_CTX *)ctx;
41     PROV_TDES_CTX *ret;
42
43     if (!ossl_prov_is_running())
44         return NULL;
45
46     ret = OPENSSL_malloc(sizeof(*ret));
47     if (ret == NULL) {
48         ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
49         return NULL;
50     }
51     in->base.hw->copyctx(&ret->base, &in->base);
52
53     return ret;
54 }
55
56 void ossl_tdes_freectx(void *vctx)
57 {
58     PROV_TDES_CTX *ctx = (PROV_TDES_CTX *)vctx;
59
60     ossl_cipher_generic_reset_ctx((PROV_CIPHER_CTX *)vctx);
61     OPENSSL_clear_free(ctx,  sizeof(*ctx));
62 }
63
64 static int tdes_init(void *vctx, const unsigned char *key, size_t keylen,
65                      const unsigned char *iv, size_t ivlen, int enc)
66 {
67     PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
68
69     if (!ossl_prov_is_running())
70         return 0;
71
72     ctx->num = 0;
73     ctx->bufsz = 0;
74     ctx->enc = enc;
75
76     if (iv != NULL) {
77         if (!ossl_cipher_generic_initiv(ctx, iv, ivlen))
78             return 0;
79     }
80
81     if (key != NULL) {
82         if (keylen != ctx->keylen) {
83             ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
84             return 0;
85         }
86         return ctx->hw->init(ctx, key, ctx->keylen);
87     }
88     return 1;
89 }
90
91 int ossl_tdes_einit(void *vctx, const unsigned char *key, size_t keylen,
92                     const unsigned char *iv, size_t ivlen)
93 {
94     return tdes_init(vctx, key, keylen, iv, ivlen, 1);
95 }
96
97 int ossl_tdes_dinit(void *vctx, const unsigned char *key, size_t keylen,
98                     const unsigned char *iv, size_t ivlen)
99 {
100     return tdes_init(vctx, key, keylen, iv, ivlen, 0);
101 }
102
103 CIPHER_DEFAULT_GETTABLE_CTX_PARAMS_START(ossl_tdes)
104     OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_RANDOM_KEY, NULL, 0),
105 CIPHER_DEFAULT_GETTABLE_CTX_PARAMS_END(ossl_tdes)
106
107 static int tdes_generatekey(PROV_CIPHER_CTX *ctx, void *ptr)
108 {
109
110     DES_cblock *deskey = ptr;
111     size_t kl = ctx->keylen;
112
113     if (kl == 0 || RAND_priv_bytes_ex(ctx->libctx, ptr, kl) <= 0)
114         return 0;
115     DES_set_odd_parity(deskey);
116     if (kl >= 16)
117         DES_set_odd_parity(deskey + 1);
118     if (kl >= 24) {
119         DES_set_odd_parity(deskey + 2);
120         return 1;
121     }
122     return 0;
123 }
124
125 int ossl_tdes_get_ctx_params(void *vctx, OSSL_PARAM params[])
126 {
127     PROV_CIPHER_CTX  *ctx = (PROV_CIPHER_CTX *)vctx;
128     OSSL_PARAM *p;
129
130     if (!ossl_cipher_generic_get_ctx_params(vctx, params))
131         return 0;
132
133     p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_RANDOM_KEY);
134     if (p != NULL && !tdes_generatekey(ctx, p->data)) {
135         ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GENERATE_KEY);
136         return 0;
137     }
138     return 1;
139 }