-
Notifications
You must be signed in to change notification settings - Fork 143
/
soter_sym.c
444 lines (411 loc) · 17.6 KB
/
soter_sym.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
/*
* Copyright (c) 2015 Cossack Labs Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "soter/soter_sym.h"
#include <string.h>
#include <openssl/cipher.h>
#include <openssl/err.h>
#include "soter/boringssl/soter_engine.h"
#define SOTER_SYM_MAX_KEY_LENGTH 128
#define SOTER_SYM_MAX_IV_LENGTH 16
#define SOTER_AES_GCM_AUTH_TAG_LENGTH 16
soter_status_t soter_pbkdf2(const uint8_t* password,
const size_t password_length,
const uint8_t* salt,
const size_t salt_length,
uint8_t* key,
const size_t* key_length)
{
if (!PKCS5_PBKDF2_HMAC((const char*)password,
(const int)password_length,
salt,
(const int)salt_length,
0,
EVP_sha256(),
(const int)(*key_length),
key)) {
return SOTER_FAIL;
}
return SOTER_SUCCESS;
}
soter_status_t soter_nokdf(const uint8_t* password,
const size_t password_length,
uint8_t* key,
const size_t* key_length)
{
if (password_length < (*key_length)) {
return SOTER_FAIL;
}
memcpy(key, password, (*key_length));
return SOTER_SUCCESS;
}
soter_status_t soter_withkdf(uint32_t alg,
const uint8_t* password,
const size_t password_length,
const uint8_t* salt,
const size_t salt_length,
uint8_t* key,
size_t* key_length)
{
switch (alg & SOTER_SYM_KDF_MASK) {
case SOTER_SYM_NOKDF:
return soter_nokdf(password, password_length, key, key_length);
break;
case SOTER_SYM_PBKDF2:
return soter_pbkdf2(password, password_length, salt, salt_length, key, key_length);
break;
}
return SOTER_INVALID_PARAMETER;
}
const EVP_CIPHER* algid_to_evp(uint32_t alg)
{
switch (alg & (SOTER_SYM_ALG_MASK | SOTER_SYM_PADDING_MASK | SOTER_SYM_KEY_LENGTH_MASK)) {
case SOTER_SYM_AES_ECB_PKCS7 | SOTER_SYM_256_KEY_LENGTH:
return EVP_aes_256_ecb();
case SOTER_SYM_AES_ECB_PKCS7 | SOTER_SYM_192_KEY_LENGTH:
return EVP_aes_192_ecb();
case SOTER_SYM_AES_ECB_PKCS7 | SOTER_SYM_128_KEY_LENGTH:
return EVP_aes_128_ecb();
case SOTER_SYM_AES_CTR | SOTER_SYM_256_KEY_LENGTH:
return EVP_aes_256_ctr();
case SOTER_SYM_AES_CTR | SOTER_SYM_192_KEY_LENGTH:
return EVP_aes_192_ctr();
case SOTER_SYM_AES_CTR | SOTER_SYM_128_KEY_LENGTH:
return EVP_aes_128_ctr();
/*
* Workaround for using BoringSSL on iOS, because XTS is not included in BoringSSL pod
* implementation. see
* https://github.com/CocoaPods/Specs/blob/master/Specs/0/8/a/BoringSSL/10.0.6/BoringSSL.podspec.json#L44
* see https://github.com/cossacklabs/themis/issues/223#issuecomment-432720576
*/
#ifndef SOTER_BORINGSSL_DISABLE_XTS
case SOTER_SYM_AES_XTS | SOTER_SYM_256_KEY_LENGTH:
return EVP_aes_256_xts();
#endif
}
return NULL;
}
const EVP_CIPHER* algid_to_evp_aead(uint32_t alg)
{
switch (alg & (SOTER_SYM_ALG_MASK | SOTER_SYM_PADDING_MASK | SOTER_SYM_KEY_LENGTH_MASK)) {
case SOTER_SYM_AES_GCM | SOTER_SYM_256_KEY_LENGTH:
return EVP_aes_256_gcm();
case SOTER_SYM_AES_GCM | SOTER_SYM_192_KEY_LENGTH:
return EVP_aes_192_gcm();
case SOTER_SYM_AES_GCM | SOTER_SYM_128_KEY_LENGTH:
return EVP_aes_128_gcm();
}
return NULL;
}
soter_sym_ctx_t* soter_sym_ctx_init(const uint32_t alg,
const void* key,
const size_t key_length,
const void* salt,
const size_t salt_length,
const void* iv,
const size_t iv_length,
bool encrypt)
{
const EVP_CIPHER* evp = algid_to_evp(alg);
SOTER_CHECK_PARAM_(evp != NULL);
SOTER_CHECK_PARAM_(key != NULL);
SOTER_CHECK_PARAM_(key_length != 0);
if (salt == NULL) {
SOTER_CHECK_PARAM_(salt_length == 0);
}
if (iv != NULL) {
SOTER_CHECK_PARAM_(iv_length >= (size_t)EVP_CIPHER_iv_length(evp));
}
soter_sym_ctx_t* ctx = NULL;
ctx = malloc(sizeof(soter_sym_ctx_t));
SOTER_CHECK_MALLOC_(ctx);
ctx->alg = alg;
uint8_t key_[SOTER_SYM_MAX_KEY_LENGTH];
size_t key_length_ = (alg & SOTER_SYM_KEY_LENGTH_MASK) / 8;
EVP_CIPHER_CTX_init(&(ctx->evp_sym_ctx));
SOTER_IF_FAIL_(soter_withkdf(alg, key, key_length, salt, salt_length, key_, &key_length_)
== SOTER_SUCCESS,
soter_sym_encrypt_destroy(ctx));
if (encrypt) {
SOTER_IF_FAIL_(EVP_EncryptInit_ex(&(ctx->evp_sym_ctx), evp, NULL, key_, iv),
soter_sym_encrypt_destroy(ctx));
} else {
SOTER_IF_FAIL_(EVP_DecryptInit_ex(&(ctx->evp_sym_ctx), evp, NULL, key_, iv),
soter_sym_encrypt_destroy(ctx));
}
return ctx;
}
soter_sym_ctx_t* soter_sym_aead_ctx_init(const uint32_t alg,
const void* key,
const size_t key_length,
const void* salt,
const size_t salt_length,
const void* iv,
const size_t iv_length,
bool encrypt)
{
const EVP_CIPHER* evp = algid_to_evp_aead(alg);
SOTER_CHECK_PARAM_(evp != NULL);
SOTER_CHECK_PARAM_(key != NULL);
SOTER_CHECK_PARAM_(key_length != 0);
if (salt == NULL) {
SOTER_CHECK_PARAM_(salt_length == 0);
}
if (iv != NULL) {
SOTER_CHECK_PARAM_(iv_length >= (size_t)EVP_CIPHER_iv_length(evp));
}
soter_sym_ctx_t* ctx = NULL;
ctx = malloc(sizeof(soter_sym_ctx_t));
SOTER_CHECK_MALLOC_(ctx);
ctx->alg = alg;
uint8_t key_[SOTER_SYM_MAX_KEY_LENGTH];
size_t key_length_ = (alg & SOTER_SYM_KEY_LENGTH_MASK) / 8;
EVP_CIPHER_CTX_init(&(ctx->evp_sym_ctx));
SOTER_IF_FAIL_(soter_withkdf(alg, key, key_length, salt, salt_length, key_, &key_length_)
== SOTER_SUCCESS,
soter_sym_encrypt_destroy(ctx));
if (encrypt) {
SOTER_IF_FAIL_(EVP_EncryptInit_ex(&(ctx->evp_sym_ctx), evp, NULL, key_, iv),
soter_sym_encrypt_destroy(ctx));
} else {
SOTER_IF_FAIL_(EVP_DecryptInit_ex(&(ctx->evp_sym_ctx), evp, NULL, key_, iv),
soter_sym_encrypt_destroy(ctx));
}
return ctx;
}
soter_status_t soter_sym_ctx_update(soter_sym_ctx_t* ctx,
const void* in_data,
const size_t in_data_length,
void* out_data,
size_t* out_data_length,
bool encrypt)
{
if (encrypt) {
SOTER_CHECK(EVP_EncryptUpdate(&(ctx->evp_sym_ctx),
out_data,
(int*)out_data_length,
(void*)in_data,
(int)in_data_length)
== 1);
} else {
SOTER_CHECK(EVP_DecryptUpdate(&(ctx->evp_sym_ctx),
out_data,
(int*)out_data_length,
(void*)in_data,
(int)in_data_length)
== 1);
}
return SOTER_SUCCESS;
}
soter_status_t soter_sym_ctx_final(soter_sym_ctx_t* ctx, void* out_data, size_t* out_data_length, bool encrypt)
{
if ((ctx->alg & SOTER_SYM_PADDING_MASK) != 0) {
if ((*out_data_length) < EVP_CIPHER_CTX_block_size(&(ctx->evp_sym_ctx))) {
(*out_data_length) = EVP_CIPHER_CTX_block_size(&(ctx->evp_sym_ctx));
return SOTER_BUFFER_TOO_SMALL;
}
}
if (encrypt) {
SOTER_CHECK(EVP_EncryptFinal_ex(&(ctx->evp_sym_ctx), out_data, (int*)out_data_length) != 0);
} else {
SOTER_CHECK(EVP_DecryptFinal_ex(&(ctx->evp_sym_ctx), out_data, (int*)out_data_length) != 0);
}
return SOTER_SUCCESS;
}
soter_status_t soter_sym_aead_ctx_final(soter_sym_ctx_t* ctx, bool encrypt)
{
uint8_t out_data[16];
size_t out_data_length = 0;
if (encrypt) {
SOTER_CHECK(EVP_EncryptFinal_ex(&(ctx->evp_sym_ctx), out_data, (int*)&out_data_length) != 0
&& out_data_length == 0);
} else {
SOTER_CHECK(EVP_DecryptFinal_ex(&(ctx->evp_sym_ctx), out_data, (int*)&out_data_length) != 0
&& out_data_length == 0);
}
return SOTER_SUCCESS;
}
soter_status_t soter_sym_ctx_destroy(soter_sym_ctx_t* ctx)
{
EVP_CIPHER_CTX_cleanup(&(ctx->evp_sym_ctx));
free(ctx);
return SOTER_SUCCESS;
}
soter_sym_ctx_t* soter_sym_encrypt_create(const uint32_t alg,
const void* key,
const size_t key_length,
const void* salt,
const size_t salt_length,
const void* iv,
const size_t iv_length)
{
return soter_sym_ctx_init(alg, key, key_length, salt, salt_length, iv, iv_length, true);
}
soter_status_t soter_sym_encrypt_update(soter_sym_ctx_t* ctx,
const void* plain_data,
const size_t plain_data_length,
void* cipher_data,
size_t* cipher_data_length)
{
if (cipher_data == NULL
|| (*cipher_data_length)
< (plain_data_length + EVP_CIPHER_CTX_block_size(&(ctx->evp_sym_ctx)) - 1)) {
(*cipher_data_length) = plain_data_length + EVP_CIPHER_CTX_block_size(&(ctx->evp_sym_ctx)) - 1;
return SOTER_BUFFER_TOO_SMALL;
}
return soter_sym_ctx_update(ctx, plain_data, plain_data_length, cipher_data, cipher_data_length, true);
}
soter_status_t soter_sym_encrypt_final(soter_sym_ctx_t* ctx, void* cipher_data, size_t* cipher_data_length)
{
return soter_sym_ctx_final(ctx, cipher_data, cipher_data_length, true);
}
soter_status_t soter_sym_encrypt_destroy(soter_sym_ctx_t* ctx)
{
return soter_sym_ctx_destroy(ctx);
}
soter_sym_ctx_t* soter_sym_decrypt_create(const uint32_t alg,
const void* key,
const size_t key_length,
const void* salt,
const size_t salt_length,
const void* iv,
const size_t iv_length)
{
return soter_sym_ctx_init(alg, key, key_length, salt, salt_length, iv, iv_length, false);
}
soter_status_t soter_sym_decrypt_update(soter_sym_ctx_t* ctx,
const void* cipher_data,
const size_t cipher_data_length,
void* plain_data,
size_t* plain_data_length)
{
if (plain_data == NULL
|| (*plain_data_length)
< (cipher_data_length + EVP_CIPHER_CTX_block_size(&(ctx->evp_sym_ctx)) - 1)) {
(*plain_data_length) = cipher_data_length + EVP_CIPHER_CTX_block_size(&(ctx->evp_sym_ctx)) - 1;
return SOTER_BUFFER_TOO_SMALL;
}
return soter_sym_ctx_update(ctx, cipher_data, cipher_data_length, plain_data, plain_data_length, false);
}
soter_status_t soter_sym_decrypt_final(soter_sym_ctx_t* ctx, void* plain_data, size_t* plain_data_length)
{
return soter_sym_ctx_final(ctx, plain_data, plain_data_length, false);
}
soter_status_t soter_sym_decrypt_destroy(soter_sym_ctx_t* ctx)
{
return soter_sym_ctx_destroy(ctx);
}
soter_sym_ctx_t* soter_sym_aead_encrypt_create(const uint32_t alg,
const void* key,
const size_t key_length,
const void* salt,
const size_t salt_length,
const void* iv,
const size_t iv_length)
{
return soter_sym_aead_ctx_init(alg, key, key_length, salt, salt_length, iv, iv_length, true);
}
soter_status_t soter_sym_aead_encrypt_update(soter_sym_ctx_t* ctx,
const void* plain_data,
const size_t plain_data_length,
void* cipher_data,
size_t* cipher_data_length)
{
if (cipher_data == NULL
|| (*cipher_data_length)
< (plain_data_length + EVP_CIPHER_CTX_block_size(&(ctx->evp_sym_ctx)) - 1)) {
(*cipher_data_length) = plain_data_length + EVP_CIPHER_CTX_block_size(&(ctx->evp_sym_ctx)) - 1;
return SOTER_BUFFER_TOO_SMALL;
}
(*cipher_data_length) = plain_data_length + EVP_CIPHER_CTX_block_size(&(ctx->evp_sym_ctx)) - 1;
return soter_sym_ctx_update(ctx, plain_data, plain_data_length, cipher_data, cipher_data_length, true);
}
soter_status_t soter_sym_aead_encrypt_aad(soter_sym_ctx_t* ctx,
const void* plain_data,
const size_t plain_data_length)
{
size_t tmp = 0;
return soter_sym_ctx_update(ctx, plain_data, plain_data_length, NULL, &tmp, true);
}
soter_status_t soter_sym_aead_encrypt_final(soter_sym_ctx_t* ctx, void* auth_tag, size_t* auth_tag_length)
{
if (!auth_tag_length) {
return SOTER_INVALID_PARAMETER;
}
if (!auth_tag || (*auth_tag_length) < SOTER_AES_GCM_AUTH_TAG_LENGTH) {
(*auth_tag_length) = SOTER_AES_GCM_AUTH_TAG_LENGTH;
return SOTER_BUFFER_TOO_SMALL;
}
SOTER_CHECK(soter_sym_aead_ctx_final(ctx, true) == SOTER_SUCCESS);
SOTER_CHECK(EVP_CIPHER_CTX_ctrl(&(ctx->evp_sym_ctx),
EVP_CTRL_GCM_GET_TAG,
SOTER_AES_GCM_AUTH_TAG_LENGTH,
auth_tag));
(*auth_tag_length) = SOTER_AES_GCM_AUTH_TAG_LENGTH;
return SOTER_SUCCESS;
}
soter_status_t soter_sym_aead_encrypt_destroy(soter_sym_ctx_t* ctx)
{
return soter_sym_ctx_destroy(ctx);
}
soter_sym_ctx_t* soter_sym_aead_decrypt_create(const uint32_t alg,
const void* key,
const size_t key_length,
const void* salt,
const size_t salt_length,
const void* iv,
const size_t iv_length)
{
return soter_sym_aead_ctx_init(alg, key, key_length, salt, salt_length, iv, iv_length, false);
}
soter_status_t soter_sym_aead_decrypt_update(soter_sym_ctx_t* ctx,
const void* cipher_data,
const size_t cipher_data_length,
void* plain_data,
size_t* plain_data_length)
{
if (plain_data == NULL
|| (*plain_data_length)
< (cipher_data_length + EVP_CIPHER_CTX_block_size(&(ctx->evp_sym_ctx)) - 1)) {
(*plain_data_length) = cipher_data_length + EVP_CIPHER_CTX_block_size(&(ctx->evp_sym_ctx)) - 1;
return SOTER_BUFFER_TOO_SMALL;
}
return soter_sym_ctx_update(ctx, cipher_data, cipher_data_length, plain_data, plain_data_length, false);
}
soter_status_t soter_sym_aead_decrypt_aad(soter_sym_ctx_t* ctx,
const void* plain_data,
const size_t plain_data_length)
{
size_t tmp = 0;
return soter_sym_ctx_update(ctx, plain_data, plain_data_length, NULL, &tmp, false);
}
soter_status_t soter_sym_aead_decrypt_final(soter_sym_ctx_t* ctx,
const void* auth_tag,
const size_t auth_tag_length)
{
SOTER_CHECK_PARAM(auth_tag != NULL);
SOTER_CHECK_PARAM(auth_tag_length >= SOTER_AES_GCM_AUTH_TAG_LENGTH);
SOTER_CHECK(ctx != NULL);
SOTER_IF_FAIL(EVP_CIPHER_CTX_ctrl(&(ctx->evp_sym_ctx),
EVP_CTRL_GCM_SET_TAG,
SOTER_AES_GCM_AUTH_TAG_LENGTH,
(void*)auth_tag),
soter_sym_aead_decrypt_destroy(ctx));
return soter_sym_aead_ctx_final(ctx, false);
}
soter_status_t soter_sym_aead_decrypt_destroy(soter_sym_ctx_t* ctx)
{
return soter_sym_ctx_destroy(ctx);
}