mbedtls_crypto.c 8.01 KB
Newer Older
1
/*
2
 * Copyright (c) 2015-2020, ARM Limited and Contributors. All rights reserved.
3
 *
dp-arm's avatar
dp-arm committed
4
 * SPDX-License-Identifier: BSD-3-Clause
5
6
 */

7
#include <assert.h>
8
9
10
#include <stddef.h>
#include <string.h>

Juan Castillo's avatar
Juan Castillo committed
11
/* mbed TLS headers */
12
#include <mbedtls/gcm.h>
Juan Castillo's avatar
Juan Castillo committed
13
14
15
16
#include <mbedtls/md.h>
#include <mbedtls/memory_buffer_alloc.h>
#include <mbedtls/oid.h>
#include <mbedtls/platform.h>
17

18
19
20
21
#include <common/debug.h>
#include <drivers/auth/crypto_mod.h>
#include <drivers/auth/mbedtls/mbedtls_common.h>
#include <drivers/auth/mbedtls/mbedtls_config.h>
22
#include <plat/common/platform.h>
23

Juan Castillo's avatar
Juan Castillo committed
24
#define LIB_NAME		"mbed TLS"
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47

/*
 * AlgorithmIdentifier  ::=  SEQUENCE  {
 *     algorithm               OBJECT IDENTIFIER,
 *     parameters              ANY DEFINED BY algorithm OPTIONAL
 * }
 *
 * SubjectPublicKeyInfo  ::=  SEQUENCE  {
 *     algorithm            AlgorithmIdentifier,
 *     subjectPublicKey     BIT STRING
 * }
 *
 * DigestInfo ::= SEQUENCE {
 *     digestAlgorithm AlgorithmIdentifier,
 *     digest OCTET STRING
 * }
 */

/*
 * Initialize the library and export the descriptor
 */
static void init(void)
{
Juan Castillo's avatar
Juan Castillo committed
48
	/* Initialize mbed TLS */
49
50
51
52
53
54
55
56
57
58
59
60
61
62
	mbedtls_init();
}

/*
 * Verify a signature.
 *
 * Parameters are passed using the DER encoding format following the ASN.1
 * structures detailed above.
 */
static int verify_signature(void *data_ptr, unsigned int data_len,
			    void *sig_ptr, unsigned int sig_len,
			    void *sig_alg, unsigned int sig_alg_len,
			    void *pk_ptr, unsigned int pk_len)
{
Juan Castillo's avatar
Juan Castillo committed
63
64
65
66
	mbedtls_asn1_buf sig_oid, sig_params;
	mbedtls_asn1_buf signature;
	mbedtls_md_type_t md_alg;
	mbedtls_pk_type_t pk_alg;
67
	mbedtls_pk_context pk = {0};
68
69
	int rc;
	void *sig_opts = NULL;
Juan Castillo's avatar
Juan Castillo committed
70
	const mbedtls_md_info_t *md_info;
71
	unsigned char *p, *end;
Juan Castillo's avatar
Juan Castillo committed
72
	unsigned char hash[MBEDTLS_MD_MAX_SIZE];
73
74
75
76

	/* Get pointers to signature OID and parameters */
	p = (unsigned char *)sig_alg;
	end = (unsigned char *)(p + sig_alg_len);
Juan Castillo's avatar
Juan Castillo committed
77
	rc = mbedtls_asn1_get_alg(&p, end, &sig_oid, &sig_params);
78
79
80
81
82
	if (rc != 0) {
		return CRYPTO_ERR_SIGNATURE;
	}

	/* Get the actual signature algorithm (MD + PK) */
83
	rc = mbedtls_x509_get_sig_alg(&sig_oid, &sig_params, &md_alg, &pk_alg, &sig_opts);
84
85
86
87
88
	if (rc != 0) {
		return CRYPTO_ERR_SIGNATURE;
	}

	/* Parse the public key */
Juan Castillo's avatar
Juan Castillo committed
89
	mbedtls_pk_init(&pk);
90
91
	p = (unsigned char *)pk_ptr;
	end = (unsigned char *)(p + pk_len);
Juan Castillo's avatar
Juan Castillo committed
92
	rc = mbedtls_pk_parse_subpubkey(&p, end, &pk);
93
	if (rc != 0) {
94
95
		rc = CRYPTO_ERR_SIGNATURE;
		goto end2;
96
97
98
99
100
101
	}

	/* Get the signature (bitstring) */
	p = (unsigned char *)sig_ptr;
	end = (unsigned char *)(p + sig_len);
	signature.tag = *p;
Juan Castillo's avatar
Juan Castillo committed
102
	rc = mbedtls_asn1_get_bitstring_null(&p, end, &signature.len);
103
104
	if (rc != 0) {
		rc = CRYPTO_ERR_SIGNATURE;
105
		goto end1;
106
107
108
109
	}
	signature.p = p;

	/* Calculate the hash of the data */
Juan Castillo's avatar
Juan Castillo committed
110
	md_info = mbedtls_md_info_from_type(md_alg);
111
112
	if (md_info == NULL) {
		rc = CRYPTO_ERR_SIGNATURE;
113
		goto end1;
114
115
	}
	p = (unsigned char *)data_ptr;
Juan Castillo's avatar
Juan Castillo committed
116
	rc = mbedtls_md(md_info, p, data_len, hash);
117
118
	if (rc != 0) {
		rc = CRYPTO_ERR_SIGNATURE;
119
		goto end1;
120
121
122
	}

	/* Verify the signature */
Juan Castillo's avatar
Juan Castillo committed
123
124
125
	rc = mbedtls_pk_verify_ext(pk_alg, sig_opts, &pk, md_alg, hash,
			mbedtls_md_get_size(md_info),
			signature.p, signature.len);
126
127
	if (rc != 0) {
		rc = CRYPTO_ERR_SIGNATURE;
128
		goto end1;
129
130
131
132
133
	}

	/* Signature verification success */
	rc = CRYPTO_SUCCESS;

134
end1:
Juan Castillo's avatar
Juan Castillo committed
135
	mbedtls_pk_free(&pk);
136
137
end2:
	mbedtls_free(sig_opts);
138
139
140
141
142
143
144
145
146
147
148
149
	return rc;
}

/*
 * Match a hash
 *
 * Digest info is passed in DER format following the ASN.1 structure detailed
 * above.
 */
static int verify_hash(void *data_ptr, unsigned int data_len,
		       void *digest_info_ptr, unsigned int digest_info_len)
{
Juan Castillo's avatar
Juan Castillo committed
150
151
152
	mbedtls_asn1_buf hash_oid, params;
	mbedtls_md_type_t md_alg;
	const mbedtls_md_info_t *md_info;
153
	unsigned char *p, *end, *hash;
Juan Castillo's avatar
Juan Castillo committed
154
	unsigned char data_hash[MBEDTLS_MD_MAX_SIZE];
155
156
157
	size_t len;
	int rc;

Juan Castillo's avatar
Juan Castillo committed
158
	/* Digest info should be an MBEDTLS_ASN1_SEQUENCE */
159
	p = (unsigned char *)digest_info_ptr;
160
	end = p + digest_info_len;
Juan Castillo's avatar
Juan Castillo committed
161
162
	rc = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_CONSTRUCTED |
				  MBEDTLS_ASN1_SEQUENCE);
163
164
165
166
167
	if (rc != 0) {
		return CRYPTO_ERR_HASH;
	}

	/* Get the hash algorithm */
Juan Castillo's avatar
Juan Castillo committed
168
	rc = mbedtls_asn1_get_alg(&p, end, &hash_oid, &params);
169
170
171
172
	if (rc != 0) {
		return CRYPTO_ERR_HASH;
	}

Juan Castillo's avatar
Juan Castillo committed
173
	rc = mbedtls_oid_get_md_alg(&hash_oid, &md_alg);
174
175
176
177
	if (rc != 0) {
		return CRYPTO_ERR_HASH;
	}

Juan Castillo's avatar
Juan Castillo committed
178
	md_info = mbedtls_md_info_from_type(md_alg);
179
180
181
182
183
	if (md_info == NULL) {
		return CRYPTO_ERR_HASH;
	}

	/* Hash should be octet string type */
Juan Castillo's avatar
Juan Castillo committed
184
	rc = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING);
185
186
187
188
189
	if (rc != 0) {
		return CRYPTO_ERR_HASH;
	}

	/* Length of hash must match the algorithm's size */
Juan Castillo's avatar
Juan Castillo committed
190
	if (len != mbedtls_md_get_size(md_info)) {
191
192
193
194
195
196
		return CRYPTO_ERR_HASH;
	}
	hash = p;

	/* Calculate the hash of the data */
	p = (unsigned char *)data_ptr;
Juan Castillo's avatar
Juan Castillo committed
197
	rc = mbedtls_md(md_info, p, data_len, data_hash);
198
199
200
201
202
	if (rc != 0) {
		return CRYPTO_ERR_HASH;
	}

	/* Compare values */
203
	rc = memcmp(data_hash, hash, mbedtls_md_get_size(md_info));
204
205
206
207
208
209
210
	if (rc != 0) {
		return CRYPTO_ERR_HASH;
	}

	return CRYPTO_SUCCESS;
}

211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#if MEASURED_BOOT
/*
 * Calculate a hash
 *
 * output points to the computed hash
 */
int calc_hash(unsigned int alg, void *data_ptr,
	      unsigned int data_len, unsigned char *output)
{
	const mbedtls_md_info_t *md_info;

	md_info = mbedtls_md_info_from_type((mbedtls_md_type_t)alg);
	if (md_info == NULL) {
		return CRYPTO_ERR_HASH;
	}

	/* Calculate the hash of the data */
	return mbedtls_md(md_info, data_ptr, data_len, output);
}
#endif /* MEASURED_BOOT */

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
#if TF_MBEDTLS_USE_AES_GCM
/*
 * Stack based buffer allocation for decryption operation. It could
 * be configured to balance stack usage vs execution speed.
 */
#define DEC_OP_BUF_SIZE		128

static int aes_gcm_decrypt(void *data_ptr, size_t len, const void *key,
			   unsigned int key_len, const void *iv,
			   unsigned int iv_len, const void *tag,
			   unsigned int tag_len)
{
	mbedtls_gcm_context ctx;
	mbedtls_cipher_id_t cipher = MBEDTLS_CIPHER_ID_AES;
	unsigned char buf[DEC_OP_BUF_SIZE];
	unsigned char tag_buf[CRYPTO_MAX_TAG_SIZE];
	unsigned char *pt = data_ptr;
	size_t dec_len;
	int diff, i, rc;

	mbedtls_gcm_init(&ctx);

	rc = mbedtls_gcm_setkey(&ctx, cipher, key, key_len * 8);
	if (rc != 0) {
		rc = CRYPTO_ERR_DECRYPTION;
		goto exit_gcm;
	}

	rc = mbedtls_gcm_starts(&ctx, MBEDTLS_GCM_DECRYPT, iv, iv_len, NULL, 0);
	if (rc != 0) {
		rc = CRYPTO_ERR_DECRYPTION;
		goto exit_gcm;
	}

	while (len > 0) {
		dec_len = MIN(sizeof(buf), len);

		rc = mbedtls_gcm_update(&ctx, dec_len, pt, buf);
		if (rc != 0) {
			rc = CRYPTO_ERR_DECRYPTION;
			goto exit_gcm;
		}

		memcpy(pt, buf, dec_len);
		pt += dec_len;
		len -= dec_len;
	}

	rc = mbedtls_gcm_finish(&ctx, tag_buf, sizeof(tag_buf));
	if (rc != 0) {
		rc = CRYPTO_ERR_DECRYPTION;
		goto exit_gcm;
	}

	/* Check tag in "constant-time" */
	for (diff = 0, i = 0; i < tag_len; i++)
		diff |= ((const unsigned char *)tag)[i] ^ tag_buf[i];

	if (diff != 0) {
		rc = CRYPTO_ERR_DECRYPTION;
		goto exit_gcm;
	}

	/* GCM decryption success */
	rc = CRYPTO_SUCCESS;

exit_gcm:
	mbedtls_gcm_free(&ctx);
	return rc;
}

/*
 * Authenticated decryption of an image
 */
static int auth_decrypt(enum crypto_dec_algo dec_algo, void *data_ptr,
			size_t len, const void *key, unsigned int key_len,
			unsigned int key_flags, const void *iv,
			unsigned int iv_len, const void *tag,
			unsigned int tag_len)
{
	int rc;

	assert((key_flags & ENC_KEY_IS_IDENTIFIER) == 0);

	switch (dec_algo) {
	case CRYPTO_GCM_DECRYPT:
		rc = aes_gcm_decrypt(data_ptr, len, key, key_len, iv, iv_len,
				     tag, tag_len);
		if (rc != 0)
			return rc;
		break;
	default:
		return CRYPTO_ERR_DECRYPTION;
	}

	return CRYPTO_SUCCESS;
}
#endif /* TF_MBEDTLS_USE_AES_GCM */

331
332
333
/*
 * Register crypto library descriptor
 */
334
#if MEASURED_BOOT
335
336
337
338
339
340
341
342
343
344
345
#if TF_MBEDTLS_USE_AES_GCM
REGISTER_CRYPTO_LIB(LIB_NAME, init, verify_signature, verify_hash, calc_hash,
		    auth_decrypt);
#else
REGISTER_CRYPTO_LIB(LIB_NAME, init, verify_signature, verify_hash, calc_hash,
		    NULL);
#endif
#else /* MEASURED_BOOT */
#if TF_MBEDTLS_USE_AES_GCM
REGISTER_CRYPTO_LIB(LIB_NAME, init, verify_signature, verify_hash,
		    auth_decrypt);
346
#else
347
348
REGISTER_CRYPTO_LIB(LIB_NAME, init, verify_signature, verify_hash, NULL);
#endif
349
#endif /* MEASURED_BOOT */