mbedtls_crypto.c 5.49 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
8
9
 */

#include <stddef.h>
#include <string.h>

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

16
17
18
19
20
#include <common/debug.h>
#include <drivers/auth/crypto_mod.h>
#include <drivers/auth/mbedtls/mbedtls_common.h>
#include <drivers/auth/mbedtls/mbedtls_config.h>

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

/*
 * 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
45
	/* Initialize mbed TLS */
46
47
48
49
50
51
52
53
54
55
56
57
58
59
	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
60
61
62
63
	mbedtls_asn1_buf sig_oid, sig_params;
	mbedtls_asn1_buf signature;
	mbedtls_md_type_t md_alg;
	mbedtls_pk_type_t pk_alg;
64
	mbedtls_pk_context pk = {0};
65
66
	int rc;
	void *sig_opts = NULL;
Juan Castillo's avatar
Juan Castillo committed
67
	const mbedtls_md_info_t *md_info;
68
	unsigned char *p, *end;
Juan Castillo's avatar
Juan Castillo committed
69
	unsigned char hash[MBEDTLS_MD_MAX_SIZE];
70
71
72
73

	/* 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
74
	rc = mbedtls_asn1_get_alg(&p, end, &sig_oid, &sig_params);
75
76
77
78
79
	if (rc != 0) {
		return CRYPTO_ERR_SIGNATURE;
	}

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

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

	/* 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
99
	rc = mbedtls_asn1_get_bitstring_null(&p, end, &signature.len);
100
101
	if (rc != 0) {
		rc = CRYPTO_ERR_SIGNATURE;
102
		goto end1;
103
104
105
106
	}
	signature.p = p;

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

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

	/* Signature verification success */
	rc = CRYPTO_SUCCESS;

131
end1:
Juan Castillo's avatar
Juan Castillo committed
132
	mbedtls_pk_free(&pk);
133
134
end2:
	mbedtls_free(sig_opts);
135
136
137
138
139
140
141
142
143
144
145
146
	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
147
148
149
	mbedtls_asn1_buf hash_oid, params;
	mbedtls_md_type_t md_alg;
	const mbedtls_md_info_t *md_info;
150
	unsigned char *p, *end, *hash;
Juan Castillo's avatar
Juan Castillo committed
151
	unsigned char data_hash[MBEDTLS_MD_MAX_SIZE];
152
153
154
	size_t len;
	int rc;

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

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

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

Juan Castillo's avatar
Juan Castillo committed
175
	md_info = mbedtls_md_info_from_type(md_alg);
176
177
178
179
180
	if (md_info == NULL) {
		return CRYPTO_ERR_HASH;
	}

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

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

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

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

	return CRYPTO_SUCCESS;
}

208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#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 */

229
230
231
/*
 * Register crypto library descriptor
 */
232
233
234
#if MEASURED_BOOT
REGISTER_CRYPTO_LIB(LIB_NAME, init, verify_signature, verify_hash, calc_hash);
#else
235
REGISTER_CRYPTO_LIB(LIB_NAME, init, verify_signature, verify_hash);
236
#endif /* MEASURED_BOOT */