2 * Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
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
10 #include <openssl/params.h>
11 #include <openssl/core_numbers.h>
12 #include <openssl/core_names.h>
13 #include <openssl/evp.h>
14 #include "internal/cryptlib.h"
15 #include "crypto/modes.h"
16 #include "crypto/ciphermode_platform.h"
18 #define MAXCHUNK ((size_t)1 << (sizeof(long) * 8 - 2))
19 #define MAXBITCHUNK ((size_t)1 << (sizeof(size_t) * 8 - 4))
21 #define GENERIC_BLOCK_SIZE 16
22 #define IV_STATE_UNINITIALISED 0 /* initial state is not initialized */
23 #define IV_STATE_BUFFERED 1 /* iv has been copied to the iv buffer */
24 #define IV_STATE_COPIED 2 /* iv has been copied from the iv buffer */
25 #define IV_STATE_FINISHED 3 /* the iv has been used - so don't reuse it */
27 #define PROV_CIPHER_FUNC(type, name, args) typedef type (* OSSL_##name##_fn)args
29 typedef struct prov_cipher_hw_st PROV_CIPHER_HW;
30 typedef struct prov_cipher_ctx_st PROV_CIPHER_CTX;
32 typedef int (PROV_CIPHER_HW_FN)(PROV_CIPHER_CTX *dat, unsigned char *out,
33 const unsigned char *in, size_t len);
35 struct prov_cipher_ctx_st {
43 size_t keylen; /* key size (in bytes) */
46 size_t bufsz; /* Number of bytes in buf */
47 unsigned int pad : 1; /* Whether padding should be used or not */
48 unsigned int enc : 1; /* Set to 1 for encrypt, or 0 otherwise */
49 unsigned int iv_set : 1; /* Set when the iv is copied to the iv/oiv buffers */
52 * num contains the number of bytes of |iv| which are valid for modes that
53 * manage partial blocks themselves.
58 /* The original value of the iv */
59 unsigned char oiv[GENERIC_BLOCK_SIZE];
60 /* Buffer of partial blocks processed via update calls */
61 unsigned char buf[GENERIC_BLOCK_SIZE];
62 unsigned char iv[GENERIC_BLOCK_SIZE];
63 const PROV_CIPHER_HW *hw; /* hardware specific functions */
64 const void *ks; /* Pointer to algorithm specific key data */
68 struct prov_cipher_hw_st {
69 int (*init)(PROV_CIPHER_CTX *dat, const uint8_t *key, size_t keylen);
70 PROV_CIPHER_HW_FN *cipher;
73 OSSL_OP_cipher_encrypt_init_fn cipher_generic_einit;
74 OSSL_OP_cipher_decrypt_init_fn cipher_generic_dinit;
75 OSSL_OP_cipher_update_fn cipher_generic_block_update;
76 OSSL_OP_cipher_final_fn cipher_generic_block_final;
77 OSSL_OP_cipher_update_fn cipher_generic_stream_update;
78 OSSL_OP_cipher_final_fn cipher_generic_stream_final;
79 OSSL_OP_cipher_cipher_fn cipher_generic_cipher;
80 OSSL_OP_cipher_get_ctx_params_fn cipher_generic_get_ctx_params;
81 OSSL_OP_cipher_set_ctx_params_fn cipher_generic_set_ctx_params;
82 OSSL_OP_cipher_gettable_params_fn cipher_generic_gettable_params;
83 OSSL_OP_cipher_gettable_ctx_params_fn cipher_generic_gettable_ctx_params;
84 OSSL_OP_cipher_settable_ctx_params_fn cipher_generic_settable_ctx_params;
85 OSSL_OP_cipher_gettable_ctx_params_fn cipher_aead_gettable_ctx_params;
86 OSSL_OP_cipher_settable_ctx_params_fn cipher_aead_settable_ctx_params;
87 int cipher_generic_get_params(OSSL_PARAM params[], unsigned int md,
89 size_t kbits, size_t blkbits, size_t ivbits);
90 void cipher_generic_initkey(void *vctx, size_t kbits, size_t blkbits,
91 size_t ivbits, unsigned int mode, uint64_t flags,
92 const PROV_CIPHER_HW *hw, void *provctx);
94 #define IMPLEMENT_generic_cipher(alg, UCALG, lcmode, UCMODE, flags, kbits, \
95 blkbits, ivbits, typ) \
96 static OSSL_OP_cipher_get_params_fn alg##_##kbits##_##lcmode##_get_params; \
97 static int alg##_##kbits##_##lcmode##_get_params(OSSL_PARAM params[]) \
99 return cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE, flags, \
100 kbits, blkbits, ivbits); \
102 static OSSL_OP_cipher_newctx_fn alg##_##kbits##_##lcmode##_newctx; \
103 static void * alg##_##kbits##_##lcmode##_newctx(void *provctx) \
105 PROV_##UCALG##_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx)); \
107 cipher_generic_initkey(ctx, kbits, blkbits, ivbits, \
108 EVP_CIPH_##UCMODE##_MODE, flags, \
109 PROV_CIPHER_HW_##alg##_##lcmode(kbits), NULL); \
113 const OSSL_DISPATCH alg##kbits##lcmode##_functions[] = { \
114 { OSSL_FUNC_CIPHER_NEWCTX, \
115 (void (*)(void)) alg##_##kbits##_##lcmode##_newctx }, \
116 { OSSL_FUNC_CIPHER_FREECTX, (void (*)(void)) alg##_freectx }, \
117 { OSSL_FUNC_CIPHER_DUPCTX, (void (*)(void)) alg##_dupctx }, \
118 { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))cipher_generic_einit }, \
119 { OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))cipher_generic_dinit }, \
120 { OSSL_FUNC_CIPHER_UPDATE, (void (*)(void))cipher_generic_##typ##_update },\
121 { OSSL_FUNC_CIPHER_FINAL, (void (*)(void))cipher_generic_##typ##_final }, \
122 { OSSL_FUNC_CIPHER_CIPHER, (void (*)(void))cipher_generic_cipher }, \
123 { OSSL_FUNC_CIPHER_GET_PARAMS, \
124 (void (*)(void)) alg##_##kbits##_##lcmode##_get_params }, \
125 { OSSL_FUNC_CIPHER_GET_CTX_PARAMS, \
126 (void (*)(void))cipher_generic_get_ctx_params }, \
127 { OSSL_FUNC_CIPHER_SET_CTX_PARAMS, \
128 (void (*)(void))cipher_generic_set_ctx_params }, \
129 { OSSL_FUNC_CIPHER_GETTABLE_PARAMS, \
130 (void (*)(void))cipher_generic_gettable_params }, \
131 { OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS, \
132 (void (*)(void))cipher_generic_gettable_ctx_params }, \
133 { OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS, \
134 (void (*)(void))cipher_generic_settable_ctx_params }, \
138 PROV_CIPHER_HW_FN cipher_hw_generic_cbc;
139 PROV_CIPHER_HW_FN cipher_hw_generic_ecb;
140 PROV_CIPHER_HW_FN cipher_hw_generic_ofb128;
141 PROV_CIPHER_HW_FN cipher_hw_generic_cfb128;
142 PROV_CIPHER_HW_FN cipher_hw_generic_cfb8;
143 PROV_CIPHER_HW_FN cipher_hw_generic_cfb1;
144 PROV_CIPHER_HW_FN cipher_hw_generic_ctr;
145 PROV_CIPHER_HW_FN cipher_hw_chunked_cbc;
146 PROV_CIPHER_HW_FN cipher_hw_chunked_cfb8;
147 PROV_CIPHER_HW_FN cipher_hw_chunked_cfb128;
148 PROV_CIPHER_HW_FN cipher_hw_chunked_ofb128;
149 #define cipher_hw_chunked_ecb cipher_hw_generic_ecb
150 #define cipher_hw_chunked_ctr cipher_hw_generic_ctr
151 #define cipher_hw_chunked_cfb1 cipher_hw_generic_cfb1
153 #define IMPLEMENT_CIPHER_HW_OFB(MODE, NAME, CTX_NAME, KEY_NAME, FUNC_PREFIX) \
154 static int cipher_hw_##NAME##_##MODE##_cipher(PROV_CIPHER_CTX *ctx, \
155 unsigned char *out, \
156 const unsigned char *in, size_t len) \
158 int num = ctx->num; \
159 KEY_NAME *key = &(((CTX_NAME *)ctx)->ks.ks); \
161 while (len >= MAXCHUNK) { \
162 FUNC_PREFIX##_encrypt(in, out, MAXCHUNK, key, ctx->iv, &num); \
168 FUNC_PREFIX##_encrypt(in, out, (long)len, key, ctx->iv, &num); \
174 #define IMPLEMENT_CIPHER_HW_ECB(MODE, NAME, CTX_NAME, KEY_NAME, FUNC_PREFIX) \
175 static int cipher_hw_##NAME##_##MODE##_cipher(PROV_CIPHER_CTX *ctx, \
176 unsigned char *out, \
177 const unsigned char *in, size_t len) \
179 size_t i, bl = ctx->blocksize; \
180 KEY_NAME *key = &(((CTX_NAME *)ctx)->ks.ks); \
184 for (i = 0, len -= bl; i <= len; i += bl) \
185 FUNC_PREFIX##_encrypt(in + i, out + i, key, ctx->enc); \
189 #define IMPLEMENT_CIPHER_HW_CBC(MODE, NAME, CTX_NAME, KEY_NAME, FUNC_PREFIX) \
190 static int cipher_hw_##NAME##_##MODE##_cipher(PROV_CIPHER_CTX *ctx, \
191 unsigned char *out, \
192 const unsigned char *in, size_t len) \
194 KEY_NAME *key = &(((CTX_NAME *)ctx)->ks.ks); \
196 while (len >= MAXCHUNK) { \
197 FUNC_PREFIX##_encrypt(in, out, MAXCHUNK, key, ctx->iv, ctx->enc); \
203 FUNC_PREFIX##_encrypt(in, out, (long)len, key, ctx->iv, ctx->enc); \
207 #define IMPLEMENT_CIPHER_HW_CFB(MODE, NAME, CTX_NAME, KEY_NAME, FUNC_PREFIX) \
208 static int cipher_hw_##NAME##_##MODE##_cipher(PROV_CIPHER_CTX *ctx, \
209 unsigned char *out, \
210 const unsigned char *in, size_t len) \
212 size_t chunk = MAXCHUNK; \
213 KEY_NAME *key = &(((CTX_NAME *)ctx)->ks.ks); \
214 int num = ctx->num; \
218 while (len > 0 && len >= chunk) { \
219 FUNC_PREFIX##_encrypt(in, out, (long)chunk, key, ctx->iv, &num, \
231 #define CIPHER_DEFAULT_GETTABLE_CTX_PARAMS_START(name) \
232 static const OSSL_PARAM name##_known_gettable_ctx_params[] = { \
233 OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_KEYLEN, NULL), \
234 OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_IVLEN, NULL), \
235 OSSL_PARAM_uint(OSSL_CIPHER_PARAM_PADDING, NULL), \
236 OSSL_PARAM_uint(OSSL_CIPHER_PARAM_NUM, NULL), \
237 OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_IV, NULL, 0),
239 #define CIPHER_DEFAULT_GETTABLE_CTX_PARAMS_END(name) \
242 const OSSL_PARAM * name##_gettable_ctx_params(void) \
244 return name##_known_gettable_ctx_params; \
247 #define CIPHER_DEFAULT_SETTABLE_CTX_PARAMS_START(name) \
248 static const OSSL_PARAM name##_known_settable_ctx_params[] = { \
249 OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_KEYLEN, NULL), \
250 OSSL_PARAM_uint(OSSL_CIPHER_PARAM_PADDING, NULL), \
251 OSSL_PARAM_uint(OSSL_CIPHER_PARAM_NUM, NULL),
252 #define CIPHER_DEFAULT_SETTABLE_CTX_PARAMS_END(name) \
255 const OSSL_PARAM * name##_settable_ctx_params(void) \
257 return name##_known_settable_ctx_params; \
260 int cipher_generic_initiv(PROV_CIPHER_CTX *ctx, const unsigned char *iv,
263 size_t fillblock(unsigned char *buf, size_t *buflen, size_t blocksize,
264 const unsigned char **in, size_t *inlen);
265 int trailingdata(unsigned char *buf, size_t *buflen, size_t blocksize,
266 const unsigned char **in, size_t *inlen);