mbedtls_crypto.c 6.13 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/*
 * Copyright (c) 2015, ARM Limited and Contributors. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 *
 * Neither the name of ARM nor the names of its contributors may be used
 * to endorse or promote products derived from this software without specific
 * prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */


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

Juan Castillo's avatar
Juan Castillo committed
38
39
40
41
42
/* mbed TLS headers */
#include <mbedtls/md.h>
#include <mbedtls/memory_buffer_alloc.h>
#include <mbedtls/oid.h>
#include <mbedtls/platform.h>
43

Juan Castillo's avatar
Juan Castillo committed
44
#define LIB_NAME		"mbed TLS"
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67

/*
 * 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
68
	/* Initialize mbed TLS */
69
70
71
72
73
74
75
76
77
78
79
80
81
82
	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
83
84
85
86
87
	mbedtls_asn1_buf sig_oid, sig_params;
	mbedtls_asn1_buf signature;
	mbedtls_md_type_t md_alg;
	mbedtls_pk_type_t pk_alg;
	mbedtls_pk_context pk;
88
89
	int rc;
	void *sig_opts = NULL;
Juan Castillo's avatar
Juan Castillo committed
90
	const mbedtls_md_info_t *md_info;
91
	unsigned char *p, *end;
Juan Castillo's avatar
Juan Castillo committed
92
	unsigned char hash[MBEDTLS_MD_MAX_SIZE];
93
94
95
96

	/* 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
97
	rc = mbedtls_asn1_get_alg(&p, end, &sig_oid, &sig_params);
98
99
100
101
102
	if (rc != 0) {
		return CRYPTO_ERR_SIGNATURE;
	}

	/* Get the actual signature algorithm (MD + PK) */
Juan Castillo's avatar
Juan Castillo committed
103
	rc = mbedtls_oid_get_sig_alg(&sig_oid, &md_alg, &pk_alg);
104
105
106
107
108
	if (rc != 0) {
		return CRYPTO_ERR_SIGNATURE;
	}

	/* Parse the public key */
Juan Castillo's avatar
Juan Castillo committed
109
	mbedtls_pk_init(&pk);
110
111
	p = (unsigned char *)pk_ptr;
	end = (unsigned char *)(p + pk_len);
Juan Castillo's avatar
Juan Castillo committed
112
	rc = mbedtls_pk_parse_subpubkey(&p, end, &pk);
113
114
115
116
117
118
119
120
	if (rc != 0) {
		return CRYPTO_ERR_SIGNATURE;
	}

	/* 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
121
	rc = mbedtls_asn1_get_bitstring_null(&p, end, &signature.len);
122
123
124
125
126
127
128
	if (rc != 0) {
		rc = CRYPTO_ERR_SIGNATURE;
		goto end;
	}
	signature.p = p;

	/* Calculate the hash of the data */
Juan Castillo's avatar
Juan Castillo committed
129
	md_info = mbedtls_md_info_from_type(md_alg);
130
131
132
133
134
	if (md_info == NULL) {
		rc = CRYPTO_ERR_SIGNATURE;
		goto end;
	}
	p = (unsigned char *)data_ptr;
Juan Castillo's avatar
Juan Castillo committed
135
	rc = mbedtls_md(md_info, p, data_len, hash);
136
137
138
139
140
141
	if (rc != 0) {
		rc = CRYPTO_ERR_SIGNATURE;
		goto end;
	}

	/* Verify the signature */
Juan Castillo's avatar
Juan Castillo committed
142
143
144
	rc = mbedtls_pk_verify_ext(pk_alg, sig_opts, &pk, md_alg, hash,
			mbedtls_md_get_size(md_info),
			signature.p, signature.len);
145
146
147
148
149
150
151
152
153
	if (rc != 0) {
		rc = CRYPTO_ERR_SIGNATURE;
		goto end;
	}

	/* Signature verification success */
	rc = CRYPTO_SUCCESS;

end:
Juan Castillo's avatar
Juan Castillo committed
154
	mbedtls_pk_free(&pk);
155
156
157
158
159
160
161
162
163
164
165
166
	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
167
168
169
	mbedtls_asn1_buf hash_oid, params;
	mbedtls_md_type_t md_alg;
	const mbedtls_md_info_t *md_info;
170
	unsigned char *p, *end, *hash;
Juan Castillo's avatar
Juan Castillo committed
171
	unsigned char data_hash[MBEDTLS_MD_MAX_SIZE];
172
173
174
	size_t len;
	int rc;

Juan Castillo's avatar
Juan Castillo committed
175
	/* Digest info should be an MBEDTLS_ASN1_SEQUENCE */
176
	p = (unsigned char *)digest_info_ptr;
177
	end = p + digest_info_len;
Juan Castillo's avatar
Juan Castillo committed
178
179
	rc = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_CONSTRUCTED |
				  MBEDTLS_ASN1_SEQUENCE);
180
181
182
183
184
	if (rc != 0) {
		return CRYPTO_ERR_HASH;
	}

	/* Get the hash algorithm */
Juan Castillo's avatar
Juan Castillo committed
185
	rc = mbedtls_asn1_get_alg(&p, end, &hash_oid, &params);
186
187
188
189
	if (rc != 0) {
		return CRYPTO_ERR_HASH;
	}

Juan Castillo's avatar
Juan Castillo committed
190
	rc = mbedtls_oid_get_md_alg(&hash_oid, &md_alg);
191
192
193
194
	if (rc != 0) {
		return CRYPTO_ERR_HASH;
	}

Juan Castillo's avatar
Juan Castillo committed
195
	md_info = mbedtls_md_info_from_type(md_alg);
196
197
198
199
200
	if (md_info == NULL) {
		return CRYPTO_ERR_HASH;
	}

	/* Hash should be octet string type */
Juan Castillo's avatar
Juan Castillo committed
201
	rc = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING);
202
203
204
205
206
	if (rc != 0) {
		return CRYPTO_ERR_HASH;
	}

	/* Length of hash must match the algorithm's size */
Juan Castillo's avatar
Juan Castillo committed
207
	if (len != mbedtls_md_get_size(md_info)) {
208
209
210
211
212
213
		return CRYPTO_ERR_HASH;
	}
	hash = p;

	/* Calculate the hash of the data */
	p = (unsigned char *)data_ptr;
Juan Castillo's avatar
Juan Castillo committed
214
	rc = mbedtls_md(md_info, p, data_len, data_hash);
215
216
217
218
219
	if (rc != 0) {
		return CRYPTO_ERR_HASH;
	}

	/* Compare values */
Juan Castillo's avatar
Juan Castillo committed
220
	rc = memcmp(data_hash, hash, mbedtls_md_get_size(md_info));
221
222
223
224
225
226
227
228
229
230
231
	if (rc != 0) {
		return CRYPTO_ERR_HASH;
	}

	return CRYPTO_SUCCESS;
}

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