mbedtls_crypto.c 4.81 KB
Newer Older
1
2
3
/*
 * Copyright (c) 2015, ARM Limited and Contributors. All rights reserved.
 *
dp-arm's avatar
dp-arm committed
4
 * SPDX-License-Identifier: BSD-3-Clause
5
6
7
8
9
10
11
12
13
 */


#include <crypto_mod.h>
#include <debug.h>
#include <mbedtls_common.h>
#include <stddef.h>
#include <string.h>

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

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

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

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

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

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

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

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

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

	/* Signature verification success */
	rc = CRYPTO_SUCCESS;

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

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

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

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

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

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

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

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

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

	return CRYPTO_SUCCESS;
}

/*
 * Register crypto library descriptor
 */
REGISTER_CRYPTO_LIB(LIB_NAME, init, verify_signature, verify_hash);