This repository has been archived by the owner on Jan 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
crypt.c
277 lines (243 loc) · 7.36 KB
/
crypt.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
/*
* Copyright (C) 2014-2016 The Android Open Source Project
*
* 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 <assert.h>
#include <lk/compiler.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <openssl/evp.h>
#include <openssl/hmac.h>
#include <openssl/rand.h>
#include "crypt.h"
/* Backwards compatability */
#if OPENSSL_VERSION_NUMBER < 0x10100000
static void EVP_CIPHER_CTX_reset(EVP_CIPHER_CTX *ctx) {
EVP_CIPHER_CTX_cleanup(ctx);
EVP_CIPHER_CTX_init(ctx);
}
static HMAC_CTX *HMAC_CTX_new(void) {
HMAC_CTX *ctx = malloc(sizeof(HMAC_CTX));
if (ctx) {
HMAC_CTX_init(ctx);
}
return ctx;
}
static void HMAC_CTX_reset(HMAC_CTX *ctx) {
HMAC_CTX_cleanup(ctx);
HMAC_CTX_init(ctx);
}
static void HMAC_CTX_free(HMAC_CTX *ctx) {
if (ctx) {
HMAC_CTX_cleanup(ctx);
free(ctx);
}
}
#endif
/*
* The OpenSSL 1.1.0 API requires we allocate these dynamically. Cache them
* globally to avoid alocator thrash and the potential for another dynamic
* failure.
*/
static EVP_CIPHER_CTX *cipher_ctx;
static HMAC_CTX *hmac_ctx;
void crypt_init(void) {
assert(!cipher_ctx);
assert(!hmac_ctx);
cipher_ctx = EVP_CIPHER_CTX_new();
assert(cipher_ctx);
hmac_ctx = HMAC_CTX_new();
assert(hmac_ctx);
}
void crypt_shutdown(void) {
EVP_CIPHER_CTX_free(cipher_ctx);
cipher_ctx = NULL;
HMAC_CTX_free(hmac_ctx);
hmac_ctx = NULL;
}
/**
* crypt - Helper function for encrypt and decrypt.
* @key: Key object.
* @data_in_out: Data to encrypt or decrypt.
* @data_size: Number of bytes in @data_in_out.
* @iv: Initialization vector to use for Cipher Block Chaining.
* @encrypt: %true to select encrypt, %false to select decrypt.
*
* Return: 0 on success, -1 if an error was detected.
*/
static int crypt(const struct key *key, void *data_in_out, size_t data_size,
const struct iv *iv, bool encrypt)
{
int evp_ret;
const EVP_CIPHER *cipher;
int out_data_size;
size_t key_len;
/*
* Make sure iv is large enough. Current implementation allows static
* check.
* TODO: Switch to runtime check for selcted cipher if EVP_MAX_IV_LENGTH
* changes to cover larger ivs used by other cipers.
*/
STATIC_ASSERT(sizeof(*iv) >= EVP_MAX_IV_LENGTH);
cipher = EVP_aes_128_ctr();
key_len = EVP_CIPHER_key_length(cipher);
if (key_len > sizeof(*key)) {
fprintf(stderr, "key too small for selected cipher, %zd < %zd\n",
sizeof(*key), key_len);
evp_ret = 0;
goto err;
}
assert(cipher_ctx);
EVP_CIPHER_CTX_reset(cipher_ctx);
evp_ret = EVP_CipherInit_ex(cipher_ctx, cipher, NULL,
key->byte, iv->byte, encrypt);
if (!evp_ret) {
fprintf(stderr, "EVP_CipherInit_ex failed\n");
goto err;
}
evp_ret = EVP_CIPHER_CTX_set_padding(cipher_ctx, 0);
if (!evp_ret) {
fprintf(stderr, "EVP_CIPHER_CTX_set_padding failed\n");
goto err;
}
evp_ret = EVP_CipherUpdate(cipher_ctx, data_in_out, &out_data_size,
data_in_out, data_size);
if (!evp_ret) {
fprintf(stderr, "EVP_CipherUpdate failed\n");
goto err;
}
if (out_data_size != (int)data_size) {
fprintf(stderr, "bad output data size %d != %zd\n",
out_data_size, data_size);
evp_ret = 0;
goto err;
}
evp_ret = EVP_CipherFinal_ex(cipher_ctx, NULL, &out_data_size);
if (!evp_ret) {
fprintf(stderr, "EVP_CipherFinal_ex failed\n");
goto err;
}
err:
return evp_ret ? 0 : -1;
}
/**
* str_hash - Cacluate a 64-bit hash for a string.
* @str: Key object.
*
* Return: Low 8 bytes of SHA1 hash as a little endian 64 bit value.
*/
uint64_t str_hash(const char *str)
{
int evp_ret;
size_t len = strlen(str);
uint8_t md[EVP_MAX_MD_SIZE];
uint64_t ret;
evp_ret = EVP_Digest(str, len, md, NULL, EVP_sha1(), NULL);
if (!evp_ret) {
fprintf(stderr, "EVP_Digest failed\n");
assert(false);
return 0;
}
STATIC_ASSERT(sizeof(ret) <= sizeof(md));
memcpy(&ret, md, sizeof(ret));
return ret;
}
/**
* calculate_mac - Calulate keyed-hash message authentication code (HMAC SHA256)
* @key: Key object.
* @mac: Mac object to return calulated mac in.
* @data: Data to calculate mac for.
* @data_size: Number of bytes in @data.
*
* Return: 0 on success, -1 if an error was detected.
*/
int calculate_mac(const struct key *key, struct mac *mac,
const void *data, size_t data_size)
{
int hmac_ret;
unsigned int md_len;
unsigned char mac_buf[EVP_MAX_MD_SIZE];
assert(hmac_ctx);
HMAC_CTX_reset(hmac_ctx);
hmac_ret = HMAC_Init_ex(hmac_ctx, key, sizeof(*key), EVP_sha256(), NULL);
if (!hmac_ret) {
fprintf(stderr, "HMAC_Init_ex failed\n");
goto err;
}
hmac_ret = HMAC_Update(hmac_ctx, data, data_size);
if (!hmac_ret) {
fprintf(stderr, "HMAC_Update failed\n");
goto err;
}
hmac_ret = HMAC_Final(hmac_ctx, mac_buf, &md_len);
if (!hmac_ret) {
fprintf(stderr, "HMAC_Final failed\n");
goto err;
}
if (md_len < sizeof(*mac)) {
fprintf(stderr, "bad md_len %d < %zd\n", md_len, sizeof(*mac));
hmac_ret = 0;
goto err;
}
memcpy(mac, mac_buf, sizeof(*mac));
err:
return hmac_ret ? 0 : -1;
}
/**
* generate_iv - Generate a random iv value.
* @iv_out: IV object.
*
* Return: 0 on success, -1 if an error was detected.
*/
int generate_iv(struct iv *iv_out)
{
int rand_ret;
rand_ret = RAND_bytes(iv_out->byte, sizeof(iv_out->byte));
if (!rand_ret) {
fprintf(stderr, "RAND_bytes failed\n");
}
return rand_ret ? 0 : -1;
}
/**
* encrypt - Encrypt data using AES-128-CTR.
* @key: Key object.
* @data_in_out: Data to encrypt.
* @data_size: Number of bytes in @data_in_out, but be a multiple of
* AES_BLOCK_SIZE.
* @iv_in: Initialization vector to use for Cipher Block Chaining.
*
* Return: 0 on success, -1 if an error was detected.
*/
int encrypt(const struct key *key, void *data_in_out, size_t data_size,
const struct iv *iv_in)
{
return crypt(key, data_in_out, data_size, iv_in, true);
}
/**
* encrypt - Decrypt data using AES-128-CTR.
* @key: Key object.
* @data_in_out: Data to decrypt.
* @data_size: Number of bytes in @data_in_out, but be a multiple of
* AES_BLOCK_SIZE.
* @iv_in: Initialization vector to use for Cipher Block Chaining.
*
* Return: 0 on success, -1 if an error was detected.
*/
int decrypt(const struct key *key, void *data_in_out, size_t data_size,
const struct iv *iv_in)
{
return crypt(key, data_in_out, data_size, iv_in, false);
}