Fix provider cipher reinit after init/update with a partial update block.
[openssl.git] / providers / implementations / ciphers / cipher_des.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_des.h"
18 #include <openssl/rand.h>
19 #include "prov/implementations.h"
20 #include "prov/providercommonerr.h"
21
22 /* TODO(3.0) Figure out what flags need to be here */
23 #define DES_FLAGS (EVP_CIPH_RAND_KEY)
24
25 static OSSL_FUNC_cipher_freectx_fn des_freectx;
26 static OSSL_FUNC_cipher_encrypt_init_fn des_einit;
27 static OSSL_FUNC_cipher_decrypt_init_fn des_dinit;
28 static OSSL_FUNC_cipher_get_ctx_params_fn des_get_ctx_params;
29 static OSSL_FUNC_cipher_gettable_ctx_params_fn des_gettable_ctx_params;
30
31 static void *des_newctx(void *provctx, size_t kbits, size_t blkbits,
32                         size_t ivbits, unsigned int mode, uint64_t flags,
33                         const PROV_CIPHER_HW *hw)
34 {
35     PROV_DES_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
36
37     if (ctx != NULL)
38         cipher_generic_initkey(ctx, kbits, blkbits, ivbits, mode, flags, hw,
39                                provctx);
40     return ctx;
41 }
42
43 static void *des_dupctx(void *ctx)
44 {
45     PROV_DES_CTX *in = (PROV_DES_CTX *)ctx;
46     PROV_DES_CTX *ret = OPENSSL_malloc(sizeof(*ret));
47
48     if (ret == NULL) {
49         ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
50         return NULL;
51     }
52     in->base.hw->copyctx(&ret->base, &in->base);
53
54     return ret;
55 }
56
57 static void des_freectx(void *vctx)
58 {
59     PROV_DES_CTX *ctx = (PROV_DES_CTX *)vctx;
60
61     cipher_generic_reset_ctx((PROV_CIPHER_CTX *)vctx);
62     OPENSSL_clear_free(ctx,  sizeof(*ctx));
63 }
64
65 static int des_init(void *vctx, const unsigned char *key, size_t keylen,
66                     const unsigned char *iv, size_t ivlen, int enc)
67 {
68     PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
69
70     ctx->num = 0;
71     ctx->bufsz = 0;
72     ctx->enc = enc;
73
74     if (iv != NULL) {
75         if (!cipher_generic_initiv(ctx, iv, ivlen))
76             return 0;
77     }
78
79     if (key != NULL) {
80         if (keylen != ctx->keylen) {
81             ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEYLEN);
82             return 0;
83         }
84         return ctx->hw->init(ctx, key, keylen);
85     }
86     return 1;
87 }
88
89 static int des_einit(void *vctx, const unsigned char *key, size_t keylen,
90                      const unsigned char *iv, size_t ivlen)
91 {
92     return des_init(vctx, key, keylen, iv, ivlen, 1);
93 }
94
95 static int des_dinit(void *vctx, const unsigned char *key, size_t keylen,
96                      const unsigned char *iv, size_t ivlen)
97 {
98     return des_init(vctx, key, keylen, iv, ivlen, 0);
99 }
100
101 static int des_generatekey(PROV_CIPHER_CTX *ctx, void *ptr)
102 {
103
104     DES_cblock *deskey = ptr;
105     size_t kl = ctx->keylen;
106
107     if (kl == 0 || RAND_priv_bytes_ex(ctx->libctx, ptr, kl) <= 0)
108         return 0;
109     DES_set_odd_parity(deskey);
110     return 1;
111 }
112
113 CIPHER_DEFAULT_GETTABLE_CTX_PARAMS_START(des)
114     OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_RANDOM_KEY, NULL, 0),
115 CIPHER_DEFAULT_GETTABLE_CTX_PARAMS_END(des)
116
117 static int des_get_ctx_params(void *vctx, OSSL_PARAM params[])
118 {
119     PROV_CIPHER_CTX  *ctx = (PROV_CIPHER_CTX *)vctx;
120     OSSL_PARAM *p;
121
122     if (!cipher_generic_get_ctx_params(vctx, params))
123         return 0;
124
125     p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_RANDOM_KEY);
126     if (p != NULL && !des_generatekey(ctx, p->data)) {
127         ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GENERATE_KEY);
128         return 0;
129     }
130     return 1;
131 }
132
133 #define IMPLEMENT_des_cipher(type, lcmode, UCMODE, flags,                      \
134                              kbits, blkbits, ivbits, block)                    \
135 static OSSL_FUNC_cipher_newctx_fn type##_##lcmode##_newctx;                    \
136 static void *des_##lcmode##_newctx(void *provctx)                              \
137 {                                                                              \
138     return des_newctx(provctx, kbits, blkbits, ivbits,                         \
139                       EVP_CIPH_##UCMODE##_MODE, flags,                         \
140                       PROV_CIPHER_HW_des_##lcmode());                          \
141 }                                                                              \
142 static OSSL_FUNC_cipher_get_params_fn des_##lcmode##_get_params;               \
143 static int des_##lcmode##_get_params(OSSL_PARAM params[])                      \
144 {                                                                              \
145     return cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE, flags,  \
146                                      kbits, blkbits, ivbits);                  \
147 }                                                                              \
148 const OSSL_DISPATCH des_##lcmode##_functions[] = {                             \
149     { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))des_einit },              \
150     { OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))des_dinit },              \
151     { OSSL_FUNC_CIPHER_UPDATE,                                                 \
152       (void (*)(void))cipher_generic_##block##_update },                       \
153     { OSSL_FUNC_CIPHER_FINAL, (void (*)(void))cipher_generic_##block##_final },\
154     { OSSL_FUNC_CIPHER_CIPHER, (void (*)(void))cipher_generic_cipher },        \
155     { OSSL_FUNC_CIPHER_NEWCTX,                                                 \
156       (void (*)(void))des_##lcmode##_newctx },                                 \
157     { OSSL_FUNC_CIPHER_DUPCTX, (void (*)(void))des_dupctx },                   \
158     { OSSL_FUNC_CIPHER_FREECTX, (void (*)(void))des_freectx },                 \
159     { OSSL_FUNC_CIPHER_GET_PARAMS,                                             \
160       (void (*)(void))des_##lcmode##_get_params },                             \
161     { OSSL_FUNC_CIPHER_GETTABLE_PARAMS,                                        \
162       (void (*)(void))cipher_generic_gettable_params },                        \
163     { OSSL_FUNC_CIPHER_GET_CTX_PARAMS, (void (*)(void))des_get_ctx_params },   \
164     { OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS,                                    \
165       (void (*)(void))des_gettable_ctx_params },                               \
166     { OSSL_FUNC_CIPHER_SET_CTX_PARAMS,                                         \
167      (void (*)(void))cipher_generic_set_ctx_params },                          \
168     { OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS,                                    \
169      (void (*)(void))cipher_generic_settable_ctx_params },                     \
170     { 0, NULL }                                                                \
171 }
172
173 /* des_ecb_functions */
174 IMPLEMENT_des_cipher(des, ecb, ECB, DES_FLAGS, 64, 64, 0, block);
175 /* des_cbc_functions */
176 IMPLEMENT_des_cipher(des, cbc, CBC, DES_FLAGS, 64, 64, 64, block);
177 /* des_ofb64_functions */
178 IMPLEMENT_des_cipher(des, ofb64, OFB, DES_FLAGS, 64, 8, 64, stream);
179 /* des_cfb64_functions */
180 IMPLEMENT_des_cipher(des, cfb64, CFB, DES_FLAGS, 64, 8, 64, stream);
181 /* des_cfb1_functions */
182 IMPLEMENT_des_cipher(des, cfb1, CFB, DES_FLAGS, 64, 8, 64, stream);
183 /* des_cfb8_functions */
184 IMPLEMENT_des_cipher(des, cfb8, CFB, DES_FLAGS, 64, 8, 64, stream);