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
|
/*
* Licensed Materials - Property of IBM
*
* trousers - An open source TCG Software Stack
*
* (C) Copyright International Business Machines Corp. 2004-2006
*
*/
/*
* symmetric.c - openssl TSS crypto routines
*
* Kent Yoder <shpedoikal@gmail.com>
*
*/
#include <limits.h>
#include <string.h>
#include <openssl/evp.h>
#include <openssl/err.h>
#include <openssl/rand.h>
#include <limits.h>
#include "trousers/tss.h"
#include "trousers/trousers.h"
#include "trousers_types.h"
#include "spi_utils.h"
#include "tsplog.h"
#ifdef TSS_DEBUG
#define DEBUG_print_openssl_errors() \
do { \
ERR_load_crypto_strings(); \
ERR_print_errors_fp(stderr); \
} while (0)
#else
#define DEBUG_print_openssl_errors()
#endif
/*
* Hopefully this will make the code clearer since
* OpenSSL returns 1 on success
*/
#define EVP_SUCCESS 1
TSS_RESULT
Trspi_Encrypt_ECB(UINT16 alg, BYTE *key, BYTE *in, UINT32 in_len, BYTE *out,
UINT32 *out_len)
{
TSS_RESULT result = TSS_SUCCESS;
EVP_CIPHER_CTX ctx;
UINT32 tmp;
switch (alg) {
case TSS_ALG_AES:
break;
default:
result = TSPERR(TSS_E_INTERNAL_ERROR);
goto done;
break;
}
EVP_CIPHER_CTX_init(&ctx);
if (!EVP_EncryptInit(&ctx, EVP_aes_256_ecb(), key, NULL)) {
result = TSPERR(TSS_E_INTERNAL_ERROR);
DEBUG_print_openssl_errors();
goto done;
}
if (*out_len < in_len + EVP_CIPHER_CTX_block_size(&ctx) - 1) {
result = TSPERR(TSS_E_INTERNAL_ERROR);
goto done;
}
if (!EVP_EncryptUpdate(&ctx, out, (int *)out_len, in, in_len)) {
result = TSPERR(TSS_E_INTERNAL_ERROR);
DEBUG_print_openssl_errors();
goto done;
}
if (!EVP_EncryptFinal(&ctx, out + *out_len, (int *)&tmp)) {
result = TSPERR(TSS_E_INTERNAL_ERROR);
DEBUG_print_openssl_errors();
goto done;
}
*out_len += tmp;
done:
EVP_CIPHER_CTX_cleanup(&ctx);
return result;
}
TSS_RESULT
Trspi_Decrypt_ECB(UINT16 alg, BYTE *key, BYTE *in, UINT32 in_len, BYTE *out,
UINT32 *out_len)
{
TSS_RESULT result = TSS_SUCCESS;
EVP_CIPHER_CTX ctx;
UINT32 tmp;
switch (alg) {
case TSS_ALG_AES:
break;
default:
result = TSPERR(TSS_E_INTERNAL_ERROR);
goto done;
break;
}
EVP_CIPHER_CTX_init(&ctx);
if (!EVP_DecryptInit(&ctx, EVP_aes_256_ecb(), key, NULL)) {
result = TSPERR(TSS_E_INTERNAL_ERROR);
DEBUG_print_openssl_errors();
goto done;
}
if (!EVP_DecryptUpdate(&ctx, out, (int *)out_len, in, in_len)) {
result = TSPERR(TSS_E_INTERNAL_ERROR);
DEBUG_print_openssl_errors();
goto done;
}
if (!EVP_DecryptFinal(&ctx, out + *out_len, (int *)&tmp)) {
result = TSPERR(TSS_E_INTERNAL_ERROR);
DEBUG_print_openssl_errors();
goto done;
}
*out_len += tmp;
done:
EVP_CIPHER_CTX_cleanup(&ctx);
return result;
}
EVP_CIPHER *
get_openssl_cipher(UINT16 alg, UINT16 mode)
{
EVP_CIPHER *cipher = NULL;
switch (alg) {
case TSS_ALG_AES:
case TCPA_ALG_AES:
switch (mode) {
case TPM_ES_NONE:
case TSS_ES_NONE:
case TPM_ES_SYM_CBC_PKCS5PAD:
LogDebug("XXX Make sure this is really PKCS5 padded");
case TR_SYM_MODE_CBC:
cipher = (EVP_CIPHER *)EVP_aes_128_cbc();
break;
case TPM_ES_SYM_OFB:
cipher = (EVP_CIPHER *)EVP_aes_128_ofb();
break;
case TPM_ES_SYM_CNT:
LogDebug("XXX AES128 in CTR mode unsupp by openssl EVP");
default:
LogDebug("Invalid mode in doing symmetric encryption");
break;
}
break;
case TSS_ALG_DES:
case TCPA_ALG_DES:
switch (mode) {
case TPM_ES_NONE:
case TSS_ES_NONE:
case TPM_ES_SYM_CBC_PKCS5PAD:
LogDebug("XXX Make sure this is really PKCS5 padded");
case TR_SYM_MODE_CBC:
cipher = (EVP_CIPHER *)EVP_des_cbc();
break;
case TPM_ES_SYM_OFB:
cipher = (EVP_CIPHER *)EVP_des_ofb();
break;
case TPM_ES_SYM_CNT:
LogDebug("XXX DES in CTR mode unsupp by openssl EVP");
default:
LogDebug("Invalid mode in doing symmetric encryption");
break;
}
break;
case TSS_ALG_3DES:
case TCPA_ALG_3DES:
switch (mode) {
case TPM_ES_NONE:
case TSS_ES_NONE:
case TPM_ES_SYM_CBC_PKCS5PAD:
LogDebug("XXX Make sure this is really PKCS5 padded");
case TR_SYM_MODE_CBC:
cipher = (EVP_CIPHER *)EVP_des_ede3_cbc();
break;
case TPM_ES_SYM_OFB:
cipher = (EVP_CIPHER *)EVP_des_ede3_ofb();
break;
case TPM_ES_SYM_CNT:
LogDebug("XXX 3DES in CTR mode unsupp by openssl EVP");
default:
LogDebug("Invalid mode in doing symmetric encryption");
break;
}
break;
case TPM_ALG_AES192:
case TSS_ALG_AES192:
switch (mode) {
case TPM_ES_NONE:
case TSS_ES_NONE:
case TPM_ES_SYM_CBC_PKCS5PAD:
LogDebug("XXX Make sure this is really PKCS5 padded");
case TR_SYM_MODE_CBC:
cipher = (EVP_CIPHER *)EVP_aes_192_cbc();
break;
case TPM_ES_SYM_OFB:
cipher = (EVP_CIPHER *)EVP_aes_192_ofb();
break;
case TPM_ES_SYM_CNT:
LogDebug("XXX AES192 in CTR mode unsupp by openssl EVP");
default:
LogDebug("Invalid mode in doing symmetric encryption");
break;
}
break;
case TPM_ALG_AES256:
case TSS_ALG_AES256:
switch (mode) {
case TPM_ES_NONE:
case TSS_ES_NONE:
case TPM_ES_SYM_CBC_PKCS5PAD:
LogDebug("XXX Make sure this is really PKCS5 padded");
case TR_SYM_MODE_CBC:
cipher = (EVP_CIPHER *)EVP_aes_256_cbc();
break;
case TPM_ES_SYM_OFB:
cipher = (EVP_CIPHER *)EVP_aes_256_ofb();
break;
case TPM_ES_SYM_CNT:
LogDebug("XXX AES256 in CTR mode unsupp by openssl EVP");
default:
LogDebug("Invalid mode in doing symmetric encryption");
break;
}
break;
default:
LogDebug("Invalid algorithm in doing symmetric encryption");
break;
}
return cipher;
}
TSS_RESULT
Trspi_SymEncrypt(UINT16 alg, UINT16 mode, BYTE *key, BYTE *iv, BYTE *in, UINT32 in_len, BYTE *out,
UINT32 *out_len)
{
TSS_RESULT result = TSS_SUCCESS;
EVP_CIPHER_CTX ctx;
EVP_CIPHER *cipher;
BYTE *def_iv = NULL, *outiv_ptr;
UINT32 tmp;
int iv_len, outiv_len;
if (*out_len > INT_MAX)
outiv_len = INT_MAX;
else
outiv_len = *(int *)out_len;
if ((cipher = get_openssl_cipher(alg, mode)) == NULL)
return TSPERR(TSS_E_INTERNAL_ERROR);
EVP_CIPHER_CTX_init(&ctx);
/* If the iv passed in is NULL, create a new random iv and prepend it to the ciphertext */
iv_len = EVP_CIPHER_iv_length(cipher);
if (iv == NULL) {
def_iv = malloc(iv_len);
if (def_iv == NULL) {
LogError("malloc of %d bytes failed.", iv_len);
return TSPERR(TSS_E_OUTOFMEMORY);
}
RAND_bytes(def_iv, iv_len);
memcpy(out, def_iv, iv_len);
outiv_ptr = &out[iv_len];
outiv_len -= iv_len;
} else {
def_iv = iv;
outiv_ptr = out;
}
if (!EVP_EncryptInit(&ctx, (const EVP_CIPHER *)cipher, key, def_iv)) {
result = TSPERR(TSS_E_INTERNAL_ERROR);
DEBUG_print_openssl_errors();
goto done;
}
if ((UINT32)outiv_len < in_len + (EVP_CIPHER_CTX_block_size(&ctx) * 2) - 1) {
LogDebug("Not enough space to do symmetric encryption");
result = TSPERR(TSS_E_INTERNAL_ERROR);
goto done;
}
if (!EVP_EncryptUpdate(&ctx, outiv_ptr, &outiv_len, in, in_len)) {
result = TSPERR(TSS_E_INTERNAL_ERROR);
DEBUG_print_openssl_errors();
goto done;
}
if (!EVP_EncryptFinal(&ctx, outiv_ptr + outiv_len, (int *)&tmp)) {
result = TSPERR(TSS_E_INTERNAL_ERROR);
DEBUG_print_openssl_errors();
goto done;
}
outiv_len += tmp;
*out_len = outiv_len;
done:
if (def_iv != iv) {
*out_len += iv_len;
free(def_iv);
}
EVP_CIPHER_CTX_cleanup(&ctx);
return result;
}
TSS_RESULT
Trspi_SymDecrypt(UINT16 alg, UINT16 mode, BYTE *key, BYTE *iv, BYTE *in, UINT32 in_len, BYTE *out,
UINT32 *out_len)
{
TSS_RESULT result = TSS_SUCCESS;
EVP_CIPHER_CTX ctx;
EVP_CIPHER *cipher;
BYTE *def_iv = NULL, *iniv_ptr;
UINT32 tmp;
int iv_len, iniv_len;
if (in_len > INT_MAX)
return TSS_E_BAD_PARAMETER;
if ((cipher = get_openssl_cipher(alg, mode)) == NULL)
return TSPERR(TSS_E_INTERNAL_ERROR);
EVP_CIPHER_CTX_init(&ctx);
/* If the iv is NULL, assume that its prepended to the ciphertext */
if (iv == NULL) {
iv_len = EVP_CIPHER_iv_length(cipher);
def_iv = malloc(iv_len);
if (def_iv == NULL) {
LogError("malloc of %d bytes failed.", iv_len);
return TSPERR(TSS_E_OUTOFMEMORY);
}
memcpy(def_iv, in, iv_len);
iniv_ptr = &in[iv_len];
iniv_len = in_len - iv_len;
} else {
def_iv = iv;
iniv_ptr = in;
iniv_len = in_len;
}
if (!EVP_DecryptInit(&ctx, cipher, key, def_iv)) {
result = TSPERR(TSS_E_INTERNAL_ERROR);
DEBUG_print_openssl_errors();
goto done;
}
if (!EVP_DecryptUpdate(&ctx, out, (int *)out_len, iniv_ptr, iniv_len)) {
result = TSPERR(TSS_E_INTERNAL_ERROR);
DEBUG_print_openssl_errors();
goto done;
}
if (!EVP_DecryptFinal(&ctx, out + *out_len, (int *)&tmp)) {
result = TSPERR(TSS_E_INTERNAL_ERROR);
DEBUG_print_openssl_errors();
goto done;
}
*out_len += tmp;
done:
if (def_iv != iv)
free(def_iv);
EVP_CIPHER_CTX_cleanup(&ctx);
return result;
}
|