mbedtls_crypto.c 5.51 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
#include <mbedtls/x509.h>
16

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>

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

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

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

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

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

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

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

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

	/* Signature verification success */
	rc = CRYPTO_SUCCESS;

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

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

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

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

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

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

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

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

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

	return CRYPTO_SUCCESS;
}

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

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