Convert rand_bytes_ex and rand_priv_bytes_ex to public functions
[openssl.git] / providers / implementations / ciphers / cipher_tdes_wrap.c
1 /*
2  * Copyright 1995-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  * SHA-1 low level APIs are deprecated for public use, but still ok for
12  * internal use.
13  */
14 #include "internal/deprecated.h"
15
16 #include <openssl/sha.h>
17 #include <openssl/rand.h>
18 #include "cipher_tdes_default.h"
19 #include "crypto/evp.h"
20 #include "prov/implementations.h"
21 #include "prov/providercommonerr.h"
22
23 /* TODO (3.0) Figure out what flags are required */
24 #define TDES_WRAP_FLAGS (EVP_CIPH_WRAP_MODE | EVP_CIPH_CUSTOM_IV)
25
26
27 static OSSL_OP_cipher_update_fn tdes_wrap_update;
28 static OSSL_OP_cipher_cipher_fn tdes_wrap_cipher;
29
30 static const unsigned char wrap_iv[8] =
31 {
32     0x4a, 0xdd, 0xa2, 0x2c, 0x79, 0xe8, 0x21, 0x05
33 };
34
35 static int des_ede3_unwrap(PROV_CIPHER_CTX *ctx, unsigned char *out,
36                            const unsigned char *in, size_t inl)
37 {
38     unsigned char icv[8], iv[TDES_IVLEN], sha1tmp[SHA_DIGEST_LENGTH];
39     int rv = -1;
40
41     if (inl < 24)
42         return -1;
43     if (out == NULL)
44         return inl - 16;
45
46     memcpy(ctx->iv, wrap_iv, 8);
47     /* Decrypt first block which will end up as icv */
48     ctx->hw->cipher(ctx, icv, in, 8);
49     /* Decrypt central blocks */
50     /*
51      * If decrypting in place move whole output along a block so the next
52      * des_ede_cbc_cipher is in place.
53      */
54     if (out == in) {
55         memmove(out, out + 8, inl - 8);
56         in -= 8;
57     }
58     ctx->hw->cipher(ctx, out, in + 8, inl - 16);
59     /* Decrypt final block which will be IV */
60     ctx->hw->cipher(ctx, iv, in + inl - 8, 8);
61     /* Reverse order of everything */
62     BUF_reverse(icv, NULL, 8);
63     BUF_reverse(out, NULL, inl - 16);
64     BUF_reverse(ctx->iv, iv, 8);
65     /* Decrypt again using new IV */
66     ctx->hw->cipher(ctx, out, out, inl - 16);
67     ctx->hw->cipher(ctx, icv, icv, 8);
68     /* Work out SHA1 hash of first portion */
69     SHA1(out, inl - 16, sha1tmp);
70
71     if (!CRYPTO_memcmp(sha1tmp, icv, 8))
72         rv = inl - 16;
73     OPENSSL_cleanse(icv, 8);
74     OPENSSL_cleanse(sha1tmp, SHA_DIGEST_LENGTH);
75     OPENSSL_cleanse(iv, 8);
76     OPENSSL_cleanse(ctx->iv, sizeof(ctx->iv));
77     if (rv == -1)
78         OPENSSL_cleanse(out, inl - 16);
79
80     return rv;
81 }
82
83 static int des_ede3_wrap(PROV_CIPHER_CTX *ctx, unsigned char *out,
84                          const unsigned char *in, size_t inl)
85 {
86     unsigned char sha1tmp[SHA_DIGEST_LENGTH];
87     size_t ivlen = TDES_IVLEN;
88     size_t icvlen = TDES_IVLEN;
89     size_t len = inl + ivlen + icvlen;
90
91     if (out == NULL)
92         return len;
93
94     /* Copy input to output buffer + 8 so we have space for IV */
95     memmove(out + ivlen, in, inl);
96     /* Work out ICV */
97     SHA1(in, inl, sha1tmp);
98     memcpy(out + inl + ivlen, sha1tmp, icvlen);
99     OPENSSL_cleanse(sha1tmp, SHA_DIGEST_LENGTH);
100     /* Generate random IV */
101     if (RAND_bytes_ex(ctx->libctx, ctx->iv, ivlen) <= 0)
102         return 0;
103     memcpy(out, ctx->iv, ivlen);
104     /* Encrypt everything after IV in place */
105     ctx->hw->cipher(ctx, out + ivlen, out + ivlen, inl + ivlen);
106     BUF_reverse(out, NULL, len);
107     memcpy(ctx->iv, wrap_iv, ivlen);
108     ctx->hw->cipher(ctx, out, out, len);
109     return len;
110 }
111
112 static int tdes_wrap_cipher_internal(PROV_CIPHER_CTX *ctx, unsigned char *out,
113                                      const unsigned char *in, size_t inl)
114 {
115     /*
116      * Sanity check input length: we typically only wrap keys so EVP_MAXCHUNK
117      * is more than will ever be needed. Also input length must be a multiple
118      * of 8 bits.
119      */
120     if (inl >= EVP_MAXCHUNK || inl % 8)
121         return -1;
122     if (ctx->enc)
123         return des_ede3_wrap(ctx, out, in, inl);
124     else
125         return des_ede3_unwrap(ctx, out, in, inl);
126 }
127
128 static int tdes_wrap_cipher(void *vctx,
129                             unsigned char *out, size_t *outl, size_t outsize,
130                             const unsigned char *in, size_t inl)
131 {
132     PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
133     int ret;
134
135     *outl = 0;
136     if (outsize < inl) {
137         PROVerr(0, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
138         return 0;
139     }
140
141     ret = tdes_wrap_cipher_internal(ctx, out, in, inl);
142     if (ret <= 0)
143         return 0;
144
145     *outl = ret;
146     return 1;
147 }
148
149 static int tdes_wrap_update(void *vctx, unsigned char *out, size_t *outl,
150                             size_t outsize, const unsigned char *in,
151                             size_t inl)
152 {
153     *outl = 0;
154     if (inl == 0)
155         return 1;
156     if (outsize < inl) {
157         PROVerr(0, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
158         return 0;
159     }
160
161     if (!tdes_wrap_cipher(vctx, out, outl, outsize, in, inl)) {
162         PROVerr(0, PROV_R_CIPHER_OPERATION_FAILED);
163         return 0;
164     }
165     return 1;
166 }
167
168
169 # define IMPLEMENT_WRAP_CIPHER(flags, kbits, blkbits, ivbits)                  \
170 static OSSL_OP_cipher_newctx_fn tdes_wrap_newctx;                              \
171 static void *tdes_wrap_newctx(void *provctx)                                   \
172 {                                                                              \
173     return tdes_newctx(provctx, EVP_CIPH_WRAP_MODE, kbits, blkbits, ivbits,    \
174                        flags, PROV_CIPHER_HW_tdes_wrap_cbc());                 \
175 }                                                                              \
176 static OSSL_OP_cipher_get_params_fn tdes_wrap_get_params;                      \
177 static int tdes_wrap_get_params(OSSL_PARAM params[])                           \
178 {                                                                              \
179     return cipher_generic_get_params(params, EVP_CIPH_WRAP_MODE, flags,        \
180                                      kbits, blkbits, ivbits);                  \
181 }                                                                              \
182 const OSSL_DISPATCH tdes_wrap_cbc_functions[] =                                \
183 {                                                                              \
184     { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void)) tdes_einit },            \
185     { OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void)) tdes_dinit },            \
186     { OSSL_FUNC_CIPHER_CIPHER, (void (*)(void))tdes_wrap_cipher },             \
187     { OSSL_FUNC_CIPHER_NEWCTX, (void (*)(void))tdes_wrap_newctx },             \
188     { OSSL_FUNC_CIPHER_FREECTX, (void (*)(void))tdes_freectx },                \
189     { OSSL_FUNC_CIPHER_UPDATE, (void (*)(void))tdes_wrap_update },             \
190     { OSSL_FUNC_CIPHER_FINAL, (void (*)(void))cipher_generic_stream_final },   \
191     { OSSL_FUNC_CIPHER_GET_PARAMS, (void (*)(void))tdes_wrap_get_params },     \
192     { OSSL_FUNC_CIPHER_GETTABLE_PARAMS,                                        \
193       (void (*)(void))cipher_generic_gettable_params },                        \
194     { OSSL_FUNC_CIPHER_GET_CTX_PARAMS, (void (*)(void))tdes_get_ctx_params },  \
195     { OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS,                                    \
196       (void (*)(void))tdes_gettable_ctx_params },                              \
197     { OSSL_FUNC_CIPHER_SET_CTX_PARAMS,                                         \
198       (void (*)(void))cipher_generic_set_ctx_params },                         \
199     { OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS,                                    \
200       (void (*)(void))cipher_generic_settable_ctx_params },                    \
201     { 0, NULL }                                                                \
202 }
203
204 /* tdes_wrap_cbc_functions */
205 IMPLEMENT_WRAP_CIPHER(TDES_WRAP_FLAGS, 64*3, 64, 0);