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
 */

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

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

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

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

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

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

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

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

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

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

	/* Signature verification success */
	rc = CRYPTO_SUCCESS;

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

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

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

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

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

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

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

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

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

	return CRYPTO_SUCCESS;
}

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