xref: /freebsd/crypto/openssl/providers/implementations/include/prov/ciphercommon.h (revision 2e3507c25e42292b45a5482e116d278f5515d04d)
1 /*
2  * Copyright 2019-2022 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 <openssl/params.h>
11 #include <openssl/core_dispatch.h>
12 #include <openssl/core_names.h>
13 #include <openssl/evp.h>
14 #include "internal/cryptlib.h"
15 #include "crypto/modes.h"
16 
17 # define MAXCHUNK    ((size_t)1 << 30)
18 # define MAXBITCHUNK ((size_t)1 << (sizeof(size_t) * 8 - 4))
19 
20 #define GENERIC_BLOCK_SIZE 16
21 #define IV_STATE_UNINITIALISED 0  /* initial state is not initialized */
22 #define IV_STATE_BUFFERED      1  /* iv has been copied to the iv buffer */
23 #define IV_STATE_COPIED        2  /* iv has been copied from the iv buffer */
24 #define IV_STATE_FINISHED      3  /* the iv has been used - so don't reuse it */
25 
26 #define PROV_CIPHER_FUNC(type, name, args) typedef type (* OSSL_##name##_fn)args
27 
28 typedef struct prov_cipher_hw_st PROV_CIPHER_HW;
29 typedef struct prov_cipher_ctx_st PROV_CIPHER_CTX;
30 
31 typedef int (PROV_CIPHER_HW_FN)(PROV_CIPHER_CTX *dat, unsigned char *out,
32                                 const unsigned char *in, size_t len);
33 
34 /* Internal flags that can be queried */
35 #define PROV_CIPHER_FLAG_AEAD             0x0001
36 #define PROV_CIPHER_FLAG_CUSTOM_IV        0x0002
37 #define PROV_CIPHER_FLAG_CTS              0x0004
38 #define PROV_CIPHER_FLAG_TLS1_MULTIBLOCK  0x0008
39 #define PROV_CIPHER_FLAG_RAND_KEY         0x0010
40 /* Internal flags that are only used within the provider */
41 #define PROV_CIPHER_FLAG_VARIABLE_LENGTH  0x0100
42 #define PROV_CIPHER_FLAG_INVERSE_CIPHER   0x0200
43 
44 struct prov_cipher_ctx_st {
45     block128_f block;
46     union {
47         cbc128_f cbc;
48         ctr128_f ctr;
49         ecb128_f ecb;
50     } stream;
51 
52     unsigned int mode;
53     size_t keylen;           /* key size (in bytes) */
54     size_t ivlen;
55     size_t blocksize;
56     size_t bufsz;            /* Number of bytes in buf */
57     unsigned int cts_mode;   /* Use to set the type for CTS modes */
58     unsigned int pad : 1;    /* Whether padding should be used or not */
59     unsigned int enc : 1;    /* Set to 1 for encrypt, or 0 otherwise */
60     unsigned int iv_set : 1; /* Set when the iv is copied to the iv/oiv buffers */
61     unsigned int updated : 1; /* Set to 1 during update for one shot ciphers */
62     unsigned int variable_keylength : 1;
63     unsigned int inverse_cipher : 1; /* set to 1 to use inverse cipher */
64     unsigned int use_bits : 1; /* Set to 0 for cfb1 to use bits instead of bytes */
65 
66     unsigned int tlsversion; /* If TLS padding is in use the TLS version number */
67     unsigned char *tlsmac;   /* tls MAC extracted from the last record */
68     int alloced;             /*
69                               * Whether the tlsmac data has been allocated or
70                               * points into the user buffer.
71                               */
72     size_t tlsmacsize;       /* Size of the TLS MAC */
73     int removetlspad;        /* Whether TLS padding should be removed or not */
74     size_t removetlsfixed;   /*
75                               * Length of the fixed size data to remove when
76                               * processing TLS data (equals mac size plus
77                               * IV size if applicable)
78                               */
79 
80     /*
81      * num contains the number of bytes of |iv| which are valid for modes that
82      * manage partial blocks themselves.
83      */
84     unsigned int num;
85 
86     /* The original value of the iv */
87     unsigned char oiv[GENERIC_BLOCK_SIZE];
88     /* Buffer of partial blocks processed via update calls */
89     unsigned char buf[GENERIC_BLOCK_SIZE];
90     unsigned char iv[GENERIC_BLOCK_SIZE];
91     const PROV_CIPHER_HW *hw; /* hardware specific functions */
92     const void *ks; /* Pointer to algorithm specific key data */
93     OSSL_LIB_CTX *libctx;
94 };
95 
96 struct prov_cipher_hw_st {
97     int (*init)(PROV_CIPHER_CTX *dat, const uint8_t *key, size_t keylen);
98     PROV_CIPHER_HW_FN *cipher;
99     void (*copyctx)(PROV_CIPHER_CTX *dst, const PROV_CIPHER_CTX *src);
100 };
101 
102 void ossl_cipher_generic_reset_ctx(PROV_CIPHER_CTX *ctx);
103 OSSL_FUNC_cipher_encrypt_init_fn ossl_cipher_generic_einit;
104 OSSL_FUNC_cipher_decrypt_init_fn ossl_cipher_generic_dinit;
105 OSSL_FUNC_cipher_update_fn ossl_cipher_generic_block_update;
106 OSSL_FUNC_cipher_final_fn ossl_cipher_generic_block_final;
107 OSSL_FUNC_cipher_update_fn ossl_cipher_generic_stream_update;
108 OSSL_FUNC_cipher_final_fn ossl_cipher_generic_stream_final;
109 OSSL_FUNC_cipher_cipher_fn ossl_cipher_generic_cipher;
110 OSSL_FUNC_cipher_get_ctx_params_fn ossl_cipher_generic_get_ctx_params;
111 OSSL_FUNC_cipher_set_ctx_params_fn ossl_cipher_generic_set_ctx_params;
112 OSSL_FUNC_cipher_gettable_params_fn     ossl_cipher_generic_gettable_params;
113 OSSL_FUNC_cipher_gettable_ctx_params_fn ossl_cipher_generic_gettable_ctx_params;
114 OSSL_FUNC_cipher_settable_ctx_params_fn ossl_cipher_generic_settable_ctx_params;
115 OSSL_FUNC_cipher_set_ctx_params_fn ossl_cipher_var_keylen_set_ctx_params;
116 OSSL_FUNC_cipher_settable_ctx_params_fn ossl_cipher_var_keylen_settable_ctx_params;
117 OSSL_FUNC_cipher_gettable_ctx_params_fn ossl_cipher_aead_gettable_ctx_params;
118 OSSL_FUNC_cipher_settable_ctx_params_fn ossl_cipher_aead_settable_ctx_params;
119 
120 int ossl_cipher_generic_get_params(OSSL_PARAM params[], unsigned int md,
121                                    uint64_t flags,
122                                    size_t kbits, size_t blkbits, size_t ivbits);
123 void ossl_cipher_generic_initkey(void *vctx, size_t kbits, size_t blkbits,
124                                  size_t ivbits, unsigned int mode,
125                                  uint64_t flags,
126                                  const PROV_CIPHER_HW *hw, void *provctx);
127 
128 #define IMPLEMENT_generic_cipher_func(alg, UCALG, lcmode, UCMODE, flags, kbits,\
129                                       blkbits, ivbits, typ)                    \
130 const OSSL_DISPATCH ossl_##alg##kbits##lcmode##_functions[] = {                \
131     { OSSL_FUNC_CIPHER_NEWCTX,                                                 \
132       (void (*)(void)) alg##_##kbits##_##lcmode##_newctx },                    \
133     { OSSL_FUNC_CIPHER_FREECTX, (void (*)(void)) alg##_freectx },              \
134     { OSSL_FUNC_CIPHER_DUPCTX, (void (*)(void)) alg##_dupctx },                \
135     { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))ossl_cipher_generic_einit },   \
136     { OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))ossl_cipher_generic_dinit },   \
137     { OSSL_FUNC_CIPHER_UPDATE, (void (*)(void))ossl_cipher_generic_##typ##_update },\
138     { OSSL_FUNC_CIPHER_FINAL, (void (*)(void))ossl_cipher_generic_##typ##_final },  \
139     { OSSL_FUNC_CIPHER_CIPHER, (void (*)(void))ossl_cipher_generic_cipher },        \
140     { OSSL_FUNC_CIPHER_GET_PARAMS,                                             \
141       (void (*)(void)) alg##_##kbits##_##lcmode##_get_params },                \
142     { OSSL_FUNC_CIPHER_GET_CTX_PARAMS,                                         \
143       (void (*)(void))ossl_cipher_generic_get_ctx_params },                    \
144     { OSSL_FUNC_CIPHER_SET_CTX_PARAMS,                                         \
145       (void (*)(void))ossl_cipher_generic_set_ctx_params },                    \
146     { OSSL_FUNC_CIPHER_GETTABLE_PARAMS,                                        \
147       (void (*)(void))ossl_cipher_generic_gettable_params },                   \
148     { OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS,                                    \
149       (void (*)(void))ossl_cipher_generic_gettable_ctx_params },               \
150     { OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS,                                    \
151      (void (*)(void))ossl_cipher_generic_settable_ctx_params },                \
152     { 0, NULL }                                                                \
153 };
154 
155 #define IMPLEMENT_var_keylen_cipher_func(alg, UCALG, lcmode, UCMODE, flags,    \
156                                          kbits, blkbits, ivbits, typ)          \
157 const OSSL_DISPATCH ossl_##alg##kbits##lcmode##_functions[] = {                \
158     { OSSL_FUNC_CIPHER_NEWCTX,                                                 \
159       (void (*)(void)) alg##_##kbits##_##lcmode##_newctx },                    \
160     { OSSL_FUNC_CIPHER_FREECTX, (void (*)(void)) alg##_freectx },              \
161     { OSSL_FUNC_CIPHER_DUPCTX, (void (*)(void)) alg##_dupctx },                \
162     { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))ossl_cipher_generic_einit },\
163     { OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))ossl_cipher_generic_dinit },\
164     { OSSL_FUNC_CIPHER_UPDATE, (void (*)(void))ossl_cipher_generic_##typ##_update },\
165     { OSSL_FUNC_CIPHER_FINAL, (void (*)(void))ossl_cipher_generic_##typ##_final },  \
166     { OSSL_FUNC_CIPHER_CIPHER, (void (*)(void))ossl_cipher_generic_cipher },   \
167     { OSSL_FUNC_CIPHER_GET_PARAMS,                                             \
168       (void (*)(void)) alg##_##kbits##_##lcmode##_get_params },                \
169     { OSSL_FUNC_CIPHER_GET_CTX_PARAMS,                                         \
170       (void (*)(void))ossl_cipher_generic_get_ctx_params },                    \
171     { OSSL_FUNC_CIPHER_SET_CTX_PARAMS,                                         \
172       (void (*)(void))ossl_cipher_var_keylen_set_ctx_params },                 \
173     { OSSL_FUNC_CIPHER_GETTABLE_PARAMS,                                        \
174       (void (*)(void))ossl_cipher_generic_gettable_params },                   \
175     { OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS,                                    \
176       (void (*)(void))ossl_cipher_generic_gettable_ctx_params },               \
177     { OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS,                                    \
178      (void (*)(void))ossl_cipher_var_keylen_settable_ctx_params },             \
179     { 0, NULL }                                                                \
180 };
181 
182 
183 #define IMPLEMENT_generic_cipher_genfn(alg, UCALG, lcmode, UCMODE, flags,      \
184                                        kbits, blkbits, ivbits, typ)            \
185 static OSSL_FUNC_cipher_get_params_fn alg##_##kbits##_##lcmode##_get_params;   \
186 static int alg##_##kbits##_##lcmode##_get_params(OSSL_PARAM params[])          \
187 {                                                                              \
188     return ossl_cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE,    \
189                                           flags, kbits, blkbits, ivbits);      \
190 }                                                                              \
191 static OSSL_FUNC_cipher_newctx_fn alg##_##kbits##_##lcmode##_newctx;           \
192 static void * alg##_##kbits##_##lcmode##_newctx(void *provctx)                 \
193 {                                                                              \
194      PROV_##UCALG##_CTX *ctx = ossl_prov_is_running() ? OPENSSL_zalloc(sizeof(*ctx))\
195                                                      : NULL;                   \
196      if (ctx != NULL) {                                                        \
197          ossl_cipher_generic_initkey(ctx, kbits, blkbits, ivbits,              \
198                                      EVP_CIPH_##UCMODE##_MODE, flags,          \
199                                      ossl_prov_cipher_hw_##alg##_##lcmode(kbits),\
200                                      provctx);                                 \
201      }                                                                         \
202      return ctx;                                                               \
203 }                                                                              \
204 
205 #define IMPLEMENT_generic_cipher(alg, UCALG, lcmode, UCMODE, flags, kbits,     \
206                                  blkbits, ivbits, typ)                         \
207 IMPLEMENT_generic_cipher_genfn(alg, UCALG, lcmode, UCMODE, flags, kbits,       \
208                                blkbits, ivbits, typ)                           \
209 IMPLEMENT_generic_cipher_func(alg, UCALG, lcmode, UCMODE, flags, kbits,        \
210                               blkbits, ivbits, typ)
211 
212 #define IMPLEMENT_var_keylen_cipher(alg, UCALG, lcmode, UCMODE, flags, kbits,  \
213                                     blkbits, ivbits, typ)                      \
214 IMPLEMENT_generic_cipher_genfn(alg, UCALG, lcmode, UCMODE, flags, kbits,       \
215                                blkbits, ivbits, typ)                           \
216 IMPLEMENT_var_keylen_cipher_func(alg, UCALG, lcmode, UCMODE, flags, kbits,     \
217                                  blkbits, ivbits, typ)
218 
219 PROV_CIPHER_HW_FN ossl_cipher_hw_generic_cbc;
220 PROV_CIPHER_HW_FN ossl_cipher_hw_generic_ecb;
221 PROV_CIPHER_HW_FN ossl_cipher_hw_generic_ofb128;
222 PROV_CIPHER_HW_FN ossl_cipher_hw_generic_cfb128;
223 PROV_CIPHER_HW_FN ossl_cipher_hw_generic_cfb8;
224 PROV_CIPHER_HW_FN ossl_cipher_hw_generic_cfb1;
225 PROV_CIPHER_HW_FN ossl_cipher_hw_generic_ctr;
226 PROV_CIPHER_HW_FN ossl_cipher_hw_chunked_cbc;
227 PROV_CIPHER_HW_FN ossl_cipher_hw_chunked_cfb8;
228 PROV_CIPHER_HW_FN ossl_cipher_hw_chunked_cfb128;
229 PROV_CIPHER_HW_FN ossl_cipher_hw_chunked_ofb128;
230 #define ossl_cipher_hw_chunked_ecb  ossl_cipher_hw_generic_ecb
231 #define ossl_cipher_hw_chunked_ctr  ossl_cipher_hw_generic_ctr
232 #define ossl_cipher_hw_chunked_cfb1 ossl_cipher_hw_generic_cfb1
233 
234 #define IMPLEMENT_CIPHER_HW_OFB(MODE, NAME, CTX_NAME, KEY_NAME, FUNC_PREFIX)   \
235 static int cipher_hw_##NAME##_##MODE##_cipher(PROV_CIPHER_CTX *ctx,            \
236                                          unsigned char *out,                   \
237                                          const unsigned char *in, size_t len)  \
238 {                                                                              \
239     int num = ctx->num;                                                        \
240     KEY_NAME *key = &(((CTX_NAME *)ctx)->ks.ks);                               \
241                                                                                \
242     while (len >= MAXCHUNK) {                                                  \
243         FUNC_PREFIX##_encrypt(in, out, MAXCHUNK, key, ctx->iv, &num);          \
244         len -= MAXCHUNK;                                                       \
245         in += MAXCHUNK;                                                        \
246         out += MAXCHUNK;                                                       \
247     }                                                                          \
248     if (len > 0) {                                                             \
249         FUNC_PREFIX##_encrypt(in, out, (long)len, key, ctx->iv, &num);         \
250     }                                                                          \
251     ctx->num = num;                                                            \
252     return 1;                                                                  \
253 }
254 
255 #define IMPLEMENT_CIPHER_HW_ECB(MODE, NAME, CTX_NAME, KEY_NAME, FUNC_PREFIX)   \
256 static int cipher_hw_##NAME##_##MODE##_cipher(PROV_CIPHER_CTX *ctx,            \
257                                          unsigned char *out,                   \
258                                          const unsigned char *in, size_t len)  \
259 {                                                                              \
260     size_t i, bl = ctx->blocksize;                                             \
261     KEY_NAME *key = &(((CTX_NAME *)ctx)->ks.ks);                               \
262                                                                                \
263     if (len < bl)                                                              \
264         return 1;                                                              \
265     for (i = 0, len -= bl; i <= len; i += bl)                                  \
266         FUNC_PREFIX##_encrypt(in + i, out + i, key, ctx->enc);                 \
267     return 1;                                                                  \
268 }
269 
270 #define IMPLEMENT_CIPHER_HW_CBC(MODE, NAME, CTX_NAME, KEY_NAME, FUNC_PREFIX)   \
271 static int cipher_hw_##NAME##_##MODE##_cipher(PROV_CIPHER_CTX *ctx,            \
272                                          unsigned char *out,                   \
273                                          const unsigned char *in, size_t len)  \
274 {                                                                              \
275     KEY_NAME *key = &(((CTX_NAME *)ctx)->ks.ks);                               \
276                                                                                \
277     while (len >= MAXCHUNK) {                                                  \
278         FUNC_PREFIX##_encrypt(in, out, MAXCHUNK, key, ctx->iv, ctx->enc);      \
279         len -= MAXCHUNK;                                                       \
280         in += MAXCHUNK;                                                        \
281         out += MAXCHUNK;                                                       \
282     }                                                                          \
283     if (len > 0)                                                               \
284         FUNC_PREFIX##_encrypt(in, out, (long)len, key, ctx->iv, ctx->enc);     \
285     return 1;                                                                  \
286 }
287 
288 #define IMPLEMENT_CIPHER_HW_CFB(MODE, NAME, CTX_NAME, KEY_NAME, FUNC_PREFIX)   \
289 static int cipher_hw_##NAME##_##MODE##_cipher(PROV_CIPHER_CTX *ctx,            \
290                                          unsigned char *out,                   \
291                                          const unsigned char *in, size_t len)  \
292 {                                                                              \
293     size_t chunk = MAXCHUNK;                                                   \
294     KEY_NAME *key = &(((CTX_NAME *)ctx)->ks.ks);                               \
295     int num = ctx->num;                                                        \
296                                                                                \
297     if (len < chunk)                                                           \
298         chunk = len;                                                           \
299     while (len > 0 && len >= chunk) {                                          \
300         FUNC_PREFIX##_encrypt(in, out, (long)chunk, key, ctx->iv, &num,        \
301                               ctx->enc);                                       \
302         len -= chunk;                                                          \
303         in += chunk;                                                           \
304         out += chunk;                                                          \
305         if (len < chunk)                                                       \
306             chunk = len;                                                       \
307     }                                                                          \
308     ctx->num = num;                                                            \
309     return 1;                                                                  \
310 }
311 
312 #define IMPLEMENT_CIPHER_HW_COPYCTX(name, CTX_TYPE)                            \
313 static void name(PROV_CIPHER_CTX *dst, const PROV_CIPHER_CTX *src)             \
314 {                                                                              \
315     CTX_TYPE *sctx = (CTX_TYPE *)src;                                          \
316     CTX_TYPE *dctx = (CTX_TYPE *)dst;                                          \
317                                                                                \
318     *dctx = *sctx;                                                             \
319     dst->ks = &dctx->ks.ks;                                                    \
320 }
321 
322 #define CIPHER_DEFAULT_GETTABLE_CTX_PARAMS_START(name)                         \
323 static const OSSL_PARAM name##_known_gettable_ctx_params[] = {                 \
324     OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_KEYLEN, NULL),                         \
325     OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_IVLEN, NULL),                          \
326     OSSL_PARAM_uint(OSSL_CIPHER_PARAM_PADDING, NULL),                          \
327     OSSL_PARAM_uint(OSSL_CIPHER_PARAM_NUM, NULL),                              \
328     OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_IV, NULL, 0),                    \
329     OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_UPDATED_IV, NULL, 0),
330 
331 #define CIPHER_DEFAULT_GETTABLE_CTX_PARAMS_END(name)                           \
332     OSSL_PARAM_END                                                             \
333 };                                                                             \
334 const OSSL_PARAM * name##_gettable_ctx_params(ossl_unused void *cctx,          \
335                                               ossl_unused void *provctx)       \
336 {                                                                              \
337     return name##_known_gettable_ctx_params;                                   \
338 }
339 
340 #define CIPHER_DEFAULT_SETTABLE_CTX_PARAMS_START(name)                         \
341 static const OSSL_PARAM name##_known_settable_ctx_params[] = {                 \
342     OSSL_PARAM_uint(OSSL_CIPHER_PARAM_PADDING, NULL),                          \
343     OSSL_PARAM_uint(OSSL_CIPHER_PARAM_NUM, NULL),
344 #define CIPHER_DEFAULT_SETTABLE_CTX_PARAMS_END(name)                           \
345     OSSL_PARAM_END                                                             \
346 };                                                                             \
347 const OSSL_PARAM * name##_settable_ctx_params(ossl_unused void *cctx,          \
348                                               ossl_unused void *provctx)       \
349 {                                                                              \
350     return name##_known_settable_ctx_params;                                   \
351 }
352 
353 int ossl_cipher_generic_initiv(PROV_CIPHER_CTX *ctx, const unsigned char *iv,
354                                size_t ivlen);
355 
356 size_t ossl_cipher_fillblock(unsigned char *buf, size_t *buflen,
357                              size_t blocksize,
358                              const unsigned char **in, size_t *inlen);
359 int ossl_cipher_trailingdata(unsigned char *buf, size_t *buflen,
360                              size_t blocksize,
361                              const unsigned char **in, size_t *inlen);
362