]>
Commit | Line | Data |
---|---|---|
4a42e264 SL |
1 | /* |
2 | * Copyright 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 | #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 "internal/modes_int.h" | |
16 | #include "internal/ciphermode_platform.h" | |
17 | ||
18 | #define MAXCHUNK ((size_t)1 << (sizeof(long) * 8 - 2)) | |
19 | #define MAXBITCHUNK ((size_t)1 << (sizeof(size_t) * 8 - 4)) | |
20 | ||
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 */ | |
26 | ||
27 | #define PROV_CIPHER_FUNC(type, name, args) typedef type (* OSSL_##name##_fn)args | |
28 | ||
29 | typedef struct prov_cipher_hw_st PROV_CIPHER_HW; | |
30 | typedef struct prov_cipher_ctx_st PROV_CIPHER_CTX; | |
31 | ||
32 | typedef int (PROV_CIPHER_HW_FN)(PROV_CIPHER_CTX *dat, unsigned char *out, | |
33 | const unsigned char *in, size_t len); | |
34 | ||
35 | struct prov_cipher_ctx_st { | |
36 | block128_f block; | |
37 | union { | |
38 | cbc128_f cbc; | |
39 | ctr128_f ctr; | |
40 | } stream; | |
41 | ||
1c3ace68 SL |
42 | unsigned int mode; |
43 | size_t keylen; /* key size (in bytes) */ | |
44 | size_t ivlen; | |
45 | size_t blocksize; | |
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 | ||
4a42e264 SL |
50 | /* |
51 | * num contains the number of bytes of |iv| which are valid for modes that | |
52 | * manage partial blocks themselves. | |
53 | */ | |
1c3ace68 | 54 | unsigned int num; |
4a42e264 | 55 | uint64_t flags; |
4a42e264 SL |
56 | |
57 | /* Buffer of partial blocks processed via update calls */ | |
58 | unsigned char buf[GENERIC_BLOCK_SIZE]; | |
59 | unsigned char iv[GENERIC_BLOCK_SIZE]; | |
60 | const PROV_CIPHER_HW *hw; /* hardware specific functions */ | |
61 | const void *ks; /* Pointer to algorithm specific key data */ | |
62 | OPENSSL_CTX *libctx; | |
63 | }; | |
64 | ||
65 | struct prov_cipher_hw_st { | |
66 | int (*init)(PROV_CIPHER_CTX *dat, const uint8_t *key, size_t keylen); | |
67 | PROV_CIPHER_HW_FN *cipher; | |
68 | }; | |
69 | ||
70 | OSSL_OP_cipher_encrypt_init_fn cipher_generic_einit; | |
71 | OSSL_OP_cipher_decrypt_init_fn cipher_generic_dinit; | |
72 | OSSL_OP_cipher_update_fn cipher_generic_block_update; | |
73 | OSSL_OP_cipher_final_fn cipher_generic_block_final; | |
74 | OSSL_OP_cipher_update_fn cipher_generic_stream_update; | |
75 | OSSL_OP_cipher_final_fn cipher_generic_stream_final; | |
76 | OSSL_OP_cipher_cipher_fn cipher_generic_cipher; | |
77 | OSSL_OP_cipher_get_ctx_params_fn cipher_generic_get_ctx_params; | |
78 | OSSL_OP_cipher_set_ctx_params_fn cipher_generic_set_ctx_params; | |
79 | OSSL_OP_cipher_gettable_params_fn cipher_generic_gettable_params; | |
80 | OSSL_OP_cipher_gettable_ctx_params_fn cipher_generic_gettable_ctx_params; | |
81 | OSSL_OP_cipher_settable_ctx_params_fn cipher_generic_settable_ctx_params; | |
82 | OSSL_OP_cipher_gettable_ctx_params_fn cipher_aead_gettable_ctx_params; | |
83 | OSSL_OP_cipher_settable_ctx_params_fn cipher_aead_settable_ctx_params; | |
1c3ace68 SL |
84 | int cipher_generic_get_params(OSSL_PARAM params[], unsigned int md, |
85 | unsigned long flags, | |
86 | size_t kbits, size_t blkbits, size_t ivbits); | |
4a42e264 | 87 | void cipher_generic_initkey(void *vctx, size_t kbits, size_t blkbits, |
55c7dc79 | 88 | size_t ivbits, unsigned int mode, uint64_t flags, |
4a42e264 SL |
89 | const PROV_CIPHER_HW *hw, void *provctx); |
90 | ||
91 | #define IMPLEMENT_generic_cipher(alg, UCALG, lcmode, UCMODE, flags, kbits, \ | |
92 | blkbits, ivbits, typ) \ | |
93 | static OSSL_OP_cipher_get_params_fn alg##_##kbits##_##lcmode##_get_params; \ | |
94 | static int alg##_##kbits##_##lcmode##_get_params(OSSL_PARAM params[]) \ | |
95 | { \ | |
96 | return cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE, flags, \ | |
97 | kbits, blkbits, ivbits); \ | |
98 | } \ | |
99 | static OSSL_OP_cipher_newctx_fn alg##_##kbits##_##lcmode##_newctx; \ | |
100 | static void * alg##_##kbits##_##lcmode##_newctx(void *provctx) \ | |
101 | { \ | |
102 | PROV_##UCALG##_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx)); \ | |
103 | if (ctx != NULL) { \ | |
104 | cipher_generic_initkey(ctx, kbits, blkbits, ivbits, \ | |
55c7dc79 | 105 | EVP_CIPH_##UCMODE##_MODE, flags, \ |
4a42e264 SL |
106 | PROV_CIPHER_HW_##alg##_##lcmode(kbits), NULL); \ |
107 | } \ | |
108 | return ctx; \ | |
109 | } \ | |
110 | const OSSL_DISPATCH alg##kbits##lcmode##_functions[] = { \ | |
111 | { OSSL_FUNC_CIPHER_NEWCTX, \ | |
112 | (void (*)(void)) alg##_##kbits##_##lcmode##_newctx }, \ | |
113 | { OSSL_FUNC_CIPHER_FREECTX, (void (*)(void)) alg##_freectx }, \ | |
114 | { OSSL_FUNC_CIPHER_DUPCTX, (void (*)(void)) alg##_dupctx }, \ | |
115 | { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))cipher_generic_einit }, \ | |
116 | { OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))cipher_generic_dinit }, \ | |
117 | { OSSL_FUNC_CIPHER_UPDATE, (void (*)(void))cipher_generic_##typ##_update },\ | |
118 | { OSSL_FUNC_CIPHER_FINAL, (void (*)(void))cipher_generic_##typ##_final }, \ | |
119 | { OSSL_FUNC_CIPHER_CIPHER, (void (*)(void))cipher_generic_cipher }, \ | |
120 | { OSSL_FUNC_CIPHER_GET_PARAMS, \ | |
121 | (void (*)(void)) alg##_##kbits##_##lcmode##_get_params }, \ | |
122 | { OSSL_FUNC_CIPHER_GET_CTX_PARAMS, \ | |
123 | (void (*)(void))cipher_generic_get_ctx_params }, \ | |
124 | { OSSL_FUNC_CIPHER_SET_CTX_PARAMS, \ | |
125 | (void (*)(void))cipher_generic_set_ctx_params }, \ | |
126 | { OSSL_FUNC_CIPHER_GETTABLE_PARAMS, \ | |
127 | (void (*)(void))cipher_generic_gettable_params }, \ | |
128 | { OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS, \ | |
129 | (void (*)(void))cipher_generic_gettable_ctx_params }, \ | |
130 | { OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS, \ | |
131 | (void (*)(void))cipher_generic_settable_ctx_params }, \ | |
132 | { 0, NULL } \ | |
133 | }; | |
134 | ||
135 | PROV_CIPHER_HW_FN cipher_hw_generic_cbc; | |
136 | PROV_CIPHER_HW_FN cipher_hw_generic_ecb; | |
137 | PROV_CIPHER_HW_FN cipher_hw_generic_ofb128; | |
138 | PROV_CIPHER_HW_FN cipher_hw_generic_cfb128; | |
139 | PROV_CIPHER_HW_FN cipher_hw_generic_cfb8; | |
140 | PROV_CIPHER_HW_FN cipher_hw_generic_cfb1; | |
141 | PROV_CIPHER_HW_FN cipher_hw_generic_ctr; | |
142 | PROV_CIPHER_HW_FN cipher_hw_chunked_cbc; | |
143 | PROV_CIPHER_HW_FN cipher_hw_chunked_cfb8; | |
144 | PROV_CIPHER_HW_FN cipher_hw_chunked_cfb128; | |
145 | PROV_CIPHER_HW_FN cipher_hw_chunked_ofb128; | |
146 | #define cipher_hw_chunked_ecb cipher_hw_generic_ecb | |
147 | #define cipher_hw_chunked_ctr cipher_hw_generic_ctr | |
148 | #define cipher_hw_chunked_cfb1 cipher_hw_generic_cfb1 | |
149 | ||
55c7dc79 SL |
150 | #define IMPLEMENT_CIPHER_HW_OFB(MODE, NAME, CTX_NAME, KEY_NAME, FUNC_PREFIX) \ |
151 | static int cipher_hw_##NAME##_##MODE##_cipher(PROV_CIPHER_CTX *ctx, \ | |
152 | unsigned char *out, \ | |
153 | const unsigned char *in, size_t len) \ | |
154 | { \ | |
155 | int num = ctx->num; \ | |
156 | KEY_NAME *key = &(((CTX_NAME *)ctx)->ks.ks); \ | |
157 | \ | |
158 | while (len >= MAXCHUNK) { \ | |
159 | FUNC_PREFIX##_encrypt(in, out, MAXCHUNK, key, ctx->iv, &num); \ | |
160 | len -= MAXCHUNK; \ | |
161 | in += MAXCHUNK; \ | |
162 | out += MAXCHUNK; \ | |
163 | } \ | |
164 | if (len > 0) { \ | |
165 | FUNC_PREFIX##_encrypt(in, out, (long)len, key, ctx->iv, &num); \ | |
166 | } \ | |
167 | ctx->num = num; \ | |
168 | return 1; \ | |
169 | } | |
170 | ||
171 | #define IMPLEMENT_CIPHER_HW_ECB(MODE, NAME, CTX_NAME, KEY_NAME, FUNC_PREFIX) \ | |
172 | static int cipher_hw_##NAME##_##MODE##_cipher(PROV_CIPHER_CTX *ctx, \ | |
173 | unsigned char *out, \ | |
174 | const unsigned char *in, size_t len) \ | |
175 | { \ | |
176 | size_t i, bl = ctx->blocksize; \ | |
177 | KEY_NAME *key = &(((CTX_NAME *)ctx)->ks.ks); \ | |
178 | \ | |
179 | if (len < bl) \ | |
180 | return 1; \ | |
181 | for (i = 0, len -= bl; i <= len; i += bl) \ | |
182 | FUNC_PREFIX##_encrypt(in + i, out + i, key, ctx->enc); \ | |
183 | return 1; \ | |
184 | } | |
185 | ||
186 | #define IMPLEMENT_CIPHER_HW_CBC(MODE, NAME, CTX_NAME, KEY_NAME, FUNC_PREFIX) \ | |
187 | static int cipher_hw_##NAME##_##MODE##_cipher(PROV_CIPHER_CTX *ctx, \ | |
188 | unsigned char *out, \ | |
189 | const unsigned char *in, size_t len) \ | |
190 | { \ | |
191 | KEY_NAME *key = &(((CTX_NAME *)ctx)->ks.ks); \ | |
192 | \ | |
193 | while (len >= MAXCHUNK) { \ | |
194 | FUNC_PREFIX##_encrypt(in, out, MAXCHUNK, key, ctx->iv, ctx->enc); \ | |
195 | len -= MAXCHUNK; \ | |
196 | in += MAXCHUNK; \ | |
197 | out += MAXCHUNK; \ | |
198 | } \ | |
199 | if (len > 0) \ | |
200 | FUNC_PREFIX##_encrypt(in, out, (long)len, key, ctx->iv, ctx->enc); \ | |
201 | return 1; \ | |
202 | } | |
203 | ||
204 | #define IMPLEMENT_CIPHER_HW_CFB(MODE, NAME, CTX_NAME, KEY_NAME, FUNC_PREFIX) \ | |
205 | static int cipher_hw_##NAME##_##MODE##_cipher(PROV_CIPHER_CTX *ctx, \ | |
206 | unsigned char *out, \ | |
207 | const unsigned char *in, size_t len) \ | |
208 | { \ | |
209 | size_t chunk = MAXCHUNK; \ | |
210 | KEY_NAME *key = &(((CTX_NAME *)ctx)->ks.ks); \ | |
211 | int num = ctx->num; \ | |
212 | \ | |
213 | if (len < chunk) \ | |
214 | chunk = len; \ | |
215 | while (len > 0 && len >= chunk) { \ | |
216 | FUNC_PREFIX##_encrypt(in, out, (long)chunk, key, ctx->iv, &num, \ | |
217 | ctx->enc); \ | |
218 | len -= chunk; \ | |
219 | in += chunk; \ | |
220 | out += chunk; \ | |
221 | if (len < chunk) \ | |
222 | chunk = len; \ | |
223 | } \ | |
224 | ctx->num = num; \ | |
225 | return 1; \ | |
226 | } |