17d37aa17SJuan Castillo /* 2*9a3088a5SQixiang Xu * Copyright (c) 2015-2017, ARM Limited and Contributors. All rights reserved. 37d37aa17SJuan Castillo * 482cb2c1aSdp-arm * SPDX-License-Identifier: BSD-3-Clause 57d37aa17SJuan Castillo */ 67d37aa17SJuan Castillo 77d37aa17SJuan Castillo #include <crypto_mod.h> 87d37aa17SJuan Castillo #include <debug.h> 97d37aa17SJuan Castillo #include <mbedtls_common.h> 10*9a3088a5SQixiang Xu #include <mbedtls_config.h> 117d37aa17SJuan Castillo #include <stddef.h> 127d37aa17SJuan Castillo #include <string.h> 137d37aa17SJuan Castillo 147d37aa17SJuan Castillo /* mbed TLS headers */ 15649dbf6fSJuan Castillo #include <mbedtls/md.h> 16649dbf6fSJuan Castillo #include <mbedtls/memory_buffer_alloc.h> 17649dbf6fSJuan Castillo #include <mbedtls/oid.h> 18649dbf6fSJuan Castillo #include <mbedtls/platform.h> 197d37aa17SJuan Castillo 207d37aa17SJuan Castillo #define LIB_NAME "mbed TLS" 217d37aa17SJuan Castillo 227d37aa17SJuan Castillo /* 237d37aa17SJuan Castillo * AlgorithmIdentifier ::= SEQUENCE { 247d37aa17SJuan Castillo * algorithm OBJECT IDENTIFIER, 257d37aa17SJuan Castillo * parameters ANY DEFINED BY algorithm OPTIONAL 267d37aa17SJuan Castillo * } 277d37aa17SJuan Castillo * 287d37aa17SJuan Castillo * SubjectPublicKeyInfo ::= SEQUENCE { 297d37aa17SJuan Castillo * algorithm AlgorithmIdentifier, 307d37aa17SJuan Castillo * subjectPublicKey BIT STRING 317d37aa17SJuan Castillo * } 327d37aa17SJuan Castillo * 337d37aa17SJuan Castillo * DigestInfo ::= SEQUENCE { 347d37aa17SJuan Castillo * digestAlgorithm AlgorithmIdentifier, 357d37aa17SJuan Castillo * digest OCTET STRING 367d37aa17SJuan Castillo * } 377d37aa17SJuan Castillo */ 387d37aa17SJuan Castillo 397d37aa17SJuan Castillo /* 407d37aa17SJuan Castillo * Initialize the library and export the descriptor 417d37aa17SJuan Castillo */ 427d37aa17SJuan Castillo static void init(void) 437d37aa17SJuan Castillo { 447d37aa17SJuan Castillo /* Initialize mbed TLS */ 457d37aa17SJuan Castillo mbedtls_init(); 467d37aa17SJuan Castillo } 477d37aa17SJuan Castillo 487d37aa17SJuan Castillo /* 497d37aa17SJuan Castillo * Verify a signature. 507d37aa17SJuan Castillo * 517d37aa17SJuan Castillo * Parameters are passed using the DER encoding format following the ASN.1 527d37aa17SJuan Castillo * structures detailed above. 537d37aa17SJuan Castillo */ 547d37aa17SJuan Castillo static int verify_signature(void *data_ptr, unsigned int data_len, 557d37aa17SJuan Castillo void *sig_ptr, unsigned int sig_len, 567d37aa17SJuan Castillo void *sig_alg, unsigned int sig_alg_len, 577d37aa17SJuan Castillo void *pk_ptr, unsigned int pk_len) 587d37aa17SJuan Castillo { 59649dbf6fSJuan Castillo mbedtls_asn1_buf sig_oid, sig_params; 60649dbf6fSJuan Castillo mbedtls_asn1_buf signature; 61649dbf6fSJuan Castillo mbedtls_md_type_t md_alg; 62649dbf6fSJuan Castillo mbedtls_pk_type_t pk_alg; 631001202dSSoby Mathew mbedtls_pk_context pk = {0}; 647d37aa17SJuan Castillo int rc; 657d37aa17SJuan Castillo void *sig_opts = NULL; 66649dbf6fSJuan Castillo const mbedtls_md_info_t *md_info; 677d37aa17SJuan Castillo unsigned char *p, *end; 68649dbf6fSJuan Castillo unsigned char hash[MBEDTLS_MD_MAX_SIZE]; 697d37aa17SJuan Castillo 707d37aa17SJuan Castillo /* Get pointers to signature OID and parameters */ 717d37aa17SJuan Castillo p = (unsigned char *)sig_alg; 727d37aa17SJuan Castillo end = (unsigned char *)(p + sig_alg_len); 73649dbf6fSJuan Castillo rc = mbedtls_asn1_get_alg(&p, end, &sig_oid, &sig_params); 747d37aa17SJuan Castillo if (rc != 0) { 757d37aa17SJuan Castillo return CRYPTO_ERR_SIGNATURE; 767d37aa17SJuan Castillo } 777d37aa17SJuan Castillo 787d37aa17SJuan Castillo /* Get the actual signature algorithm (MD + PK) */ 791001202dSSoby Mathew rc = mbedtls_x509_get_sig_alg(&sig_oid, &sig_params, &md_alg, &pk_alg, &sig_opts); 807d37aa17SJuan Castillo if (rc != 0) { 817d37aa17SJuan Castillo return CRYPTO_ERR_SIGNATURE; 827d37aa17SJuan Castillo } 837d37aa17SJuan Castillo 847d37aa17SJuan Castillo /* Parse the public key */ 85649dbf6fSJuan Castillo mbedtls_pk_init(&pk); 867d37aa17SJuan Castillo p = (unsigned char *)pk_ptr; 877d37aa17SJuan Castillo end = (unsigned char *)(p + pk_len); 88649dbf6fSJuan Castillo rc = mbedtls_pk_parse_subpubkey(&p, end, &pk); 897d37aa17SJuan Castillo if (rc != 0) { 901001202dSSoby Mathew rc = CRYPTO_ERR_SIGNATURE; 911001202dSSoby Mathew goto end2; 927d37aa17SJuan Castillo } 937d37aa17SJuan Castillo 947d37aa17SJuan Castillo /* Get the signature (bitstring) */ 957d37aa17SJuan Castillo p = (unsigned char *)sig_ptr; 967d37aa17SJuan Castillo end = (unsigned char *)(p + sig_len); 977d37aa17SJuan Castillo signature.tag = *p; 98649dbf6fSJuan Castillo rc = mbedtls_asn1_get_bitstring_null(&p, end, &signature.len); 997d37aa17SJuan Castillo if (rc != 0) { 1007d37aa17SJuan Castillo rc = CRYPTO_ERR_SIGNATURE; 1011001202dSSoby Mathew goto end1; 1027d37aa17SJuan Castillo } 1037d37aa17SJuan Castillo signature.p = p; 1047d37aa17SJuan Castillo 1057d37aa17SJuan Castillo /* Calculate the hash of the data */ 106649dbf6fSJuan Castillo md_info = mbedtls_md_info_from_type(md_alg); 1077d37aa17SJuan Castillo if (md_info == NULL) { 1087d37aa17SJuan Castillo rc = CRYPTO_ERR_SIGNATURE; 1091001202dSSoby Mathew goto end1; 1107d37aa17SJuan Castillo } 1117d37aa17SJuan Castillo p = (unsigned char *)data_ptr; 112649dbf6fSJuan Castillo rc = mbedtls_md(md_info, p, data_len, hash); 1137d37aa17SJuan Castillo if (rc != 0) { 1147d37aa17SJuan Castillo rc = CRYPTO_ERR_SIGNATURE; 1151001202dSSoby Mathew goto end1; 1167d37aa17SJuan Castillo } 1177d37aa17SJuan Castillo 1187d37aa17SJuan Castillo /* Verify the signature */ 119649dbf6fSJuan Castillo rc = mbedtls_pk_verify_ext(pk_alg, sig_opts, &pk, md_alg, hash, 120649dbf6fSJuan Castillo mbedtls_md_get_size(md_info), 121649dbf6fSJuan Castillo signature.p, signature.len); 1227d37aa17SJuan Castillo if (rc != 0) { 1237d37aa17SJuan Castillo rc = CRYPTO_ERR_SIGNATURE; 1241001202dSSoby Mathew goto end1; 1257d37aa17SJuan Castillo } 1267d37aa17SJuan Castillo 1277d37aa17SJuan Castillo /* Signature verification success */ 1287d37aa17SJuan Castillo rc = CRYPTO_SUCCESS; 1297d37aa17SJuan Castillo 1301001202dSSoby Mathew end1: 131649dbf6fSJuan Castillo mbedtls_pk_free(&pk); 1321001202dSSoby Mathew end2: 1331001202dSSoby Mathew mbedtls_free(sig_opts); 1347d37aa17SJuan Castillo return rc; 1357d37aa17SJuan Castillo } 1367d37aa17SJuan Castillo 1377d37aa17SJuan Castillo /* 1387d37aa17SJuan Castillo * Match a hash 1397d37aa17SJuan Castillo * 1407d37aa17SJuan Castillo * Digest info is passed in DER format following the ASN.1 structure detailed 1417d37aa17SJuan Castillo * above. 1427d37aa17SJuan Castillo */ 1437d37aa17SJuan Castillo static int verify_hash(void *data_ptr, unsigned int data_len, 1447d37aa17SJuan Castillo void *digest_info_ptr, unsigned int digest_info_len) 1457d37aa17SJuan Castillo { 146649dbf6fSJuan Castillo mbedtls_asn1_buf hash_oid, params; 147649dbf6fSJuan Castillo mbedtls_md_type_t md_alg; 148649dbf6fSJuan Castillo const mbedtls_md_info_t *md_info; 1497d37aa17SJuan Castillo unsigned char *p, *end, *hash; 150649dbf6fSJuan Castillo unsigned char data_hash[MBEDTLS_MD_MAX_SIZE]; 1517d37aa17SJuan Castillo size_t len; 1527d37aa17SJuan Castillo int rc; 1537d37aa17SJuan Castillo 154649dbf6fSJuan Castillo /* Digest info should be an MBEDTLS_ASN1_SEQUENCE */ 1557d37aa17SJuan Castillo p = (unsigned char *)digest_info_ptr; 156aa856917SSandrine Bailleux end = p + digest_info_len; 157649dbf6fSJuan Castillo rc = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_CONSTRUCTED | 158649dbf6fSJuan Castillo MBEDTLS_ASN1_SEQUENCE); 1597d37aa17SJuan Castillo if (rc != 0) { 1607d37aa17SJuan Castillo return CRYPTO_ERR_HASH; 1617d37aa17SJuan Castillo } 1627d37aa17SJuan Castillo 1637d37aa17SJuan Castillo /* Get the hash algorithm */ 164649dbf6fSJuan Castillo rc = mbedtls_asn1_get_alg(&p, end, &hash_oid, ¶ms); 1657d37aa17SJuan Castillo if (rc != 0) { 1667d37aa17SJuan Castillo return CRYPTO_ERR_HASH; 1677d37aa17SJuan Castillo } 1687d37aa17SJuan Castillo 169649dbf6fSJuan Castillo rc = mbedtls_oid_get_md_alg(&hash_oid, &md_alg); 1707d37aa17SJuan Castillo if (rc != 0) { 1717d37aa17SJuan Castillo return CRYPTO_ERR_HASH; 1727d37aa17SJuan Castillo } 1737d37aa17SJuan Castillo 174649dbf6fSJuan Castillo md_info = mbedtls_md_info_from_type(md_alg); 1757d37aa17SJuan Castillo if (md_info == NULL) { 1767d37aa17SJuan Castillo return CRYPTO_ERR_HASH; 1777d37aa17SJuan Castillo } 1787d37aa17SJuan Castillo 1797d37aa17SJuan Castillo /* Hash should be octet string type */ 180649dbf6fSJuan Castillo rc = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OCTET_STRING); 1817d37aa17SJuan Castillo if (rc != 0) { 1827d37aa17SJuan Castillo return CRYPTO_ERR_HASH; 1837d37aa17SJuan Castillo } 1847d37aa17SJuan Castillo 1857d37aa17SJuan Castillo /* Length of hash must match the algorithm's size */ 186649dbf6fSJuan Castillo if (len != mbedtls_md_get_size(md_info)) { 1877d37aa17SJuan Castillo return CRYPTO_ERR_HASH; 1887d37aa17SJuan Castillo } 1897d37aa17SJuan Castillo hash = p; 1907d37aa17SJuan Castillo 1917d37aa17SJuan Castillo /* Calculate the hash of the data */ 1927d37aa17SJuan Castillo p = (unsigned char *)data_ptr; 193649dbf6fSJuan Castillo rc = mbedtls_md(md_info, p, data_len, data_hash); 1947d37aa17SJuan Castillo if (rc != 0) { 1957d37aa17SJuan Castillo return CRYPTO_ERR_HASH; 1967d37aa17SJuan Castillo } 1977d37aa17SJuan Castillo 1987d37aa17SJuan Castillo /* Compare values */ 199fabd21adSAntonio Nino Diaz rc = memcmp(data_hash, hash, mbedtls_md_get_size(md_info)); 2007d37aa17SJuan Castillo if (rc != 0) { 2017d37aa17SJuan Castillo return CRYPTO_ERR_HASH; 2027d37aa17SJuan Castillo } 2037d37aa17SJuan Castillo 2047d37aa17SJuan Castillo return CRYPTO_SUCCESS; 2057d37aa17SJuan Castillo } 2067d37aa17SJuan Castillo 2077d37aa17SJuan Castillo /* 2087d37aa17SJuan Castillo * Register crypto library descriptor 2097d37aa17SJuan Castillo */ 2107d37aa17SJuan Castillo REGISTER_CRYPTO_LIB(LIB_NAME, init, verify_signature, verify_hash); 211