1*05799ae0SJuan Castillo /* 2*05799ae0SJuan Castillo * Copyright (c) 2015, ARM Limited and Contributors. All rights reserved. 3*05799ae0SJuan Castillo * 4*05799ae0SJuan Castillo * Redistribution and use in source and binary forms, with or without 5*05799ae0SJuan Castillo * modification, are permitted provided that the following conditions are met: 6*05799ae0SJuan Castillo * 7*05799ae0SJuan Castillo * Redistributions of source code must retain the above copyright notice, this 8*05799ae0SJuan Castillo * list of conditions and the following disclaimer. 9*05799ae0SJuan Castillo * 10*05799ae0SJuan Castillo * Redistributions in binary form must reproduce the above copyright notice, 11*05799ae0SJuan Castillo * this list of conditions and the following disclaimer in the documentation 12*05799ae0SJuan Castillo * and/or other materials provided with the distribution. 13*05799ae0SJuan Castillo * 14*05799ae0SJuan Castillo * Neither the name of ARM nor the names of its contributors may be used 15*05799ae0SJuan Castillo * to endorse or promote products derived from this software without specific 16*05799ae0SJuan Castillo * prior written permission. 17*05799ae0SJuan Castillo * 18*05799ae0SJuan Castillo * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19*05799ae0SJuan Castillo * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20*05799ae0SJuan Castillo * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21*05799ae0SJuan Castillo * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 22*05799ae0SJuan Castillo * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23*05799ae0SJuan Castillo * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24*05799ae0SJuan Castillo * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25*05799ae0SJuan Castillo * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26*05799ae0SJuan Castillo * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27*05799ae0SJuan Castillo * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28*05799ae0SJuan Castillo * POSSIBILITY OF SUCH DAMAGE. 29*05799ae0SJuan Castillo */ 30*05799ae0SJuan Castillo 31*05799ae0SJuan Castillo #include <assert.h> 32*05799ae0SJuan Castillo #include <crypto_mod.h> 33*05799ae0SJuan Castillo #include <debug.h> 34*05799ae0SJuan Castillo 35*05799ae0SJuan Castillo /* Variable exported by the crypto library through REGISTER_CRYPTO_LIB() */ 36*05799ae0SJuan Castillo extern const crypto_lib_desc_t crypto_lib_desc; 37*05799ae0SJuan Castillo 38*05799ae0SJuan Castillo /* 39*05799ae0SJuan Castillo * The crypto module is responsible for verifying digital signatures and hashes. 40*05799ae0SJuan Castillo * It relies on a crypto library to perform the cryptographic operations. 41*05799ae0SJuan Castillo * 42*05799ae0SJuan Castillo * The crypto module itself does not impose any specific format on signatures, 43*05799ae0SJuan Castillo * signature algorithm, keys or hashes, but most cryptographic libraries will 44*05799ae0SJuan Castillo * take the parameters as the following DER encoded ASN.1 structures: 45*05799ae0SJuan Castillo * 46*05799ae0SJuan Castillo * AlgorithmIdentifier ::= SEQUENCE { 47*05799ae0SJuan Castillo * algorithm OBJECT IDENTIFIER, 48*05799ae0SJuan Castillo * parameters ANY DEFINED BY algorithm OPTIONAL 49*05799ae0SJuan Castillo * } 50*05799ae0SJuan Castillo * 51*05799ae0SJuan Castillo * DigestInfo ::= SEQUENCE { 52*05799ae0SJuan Castillo * digestAlgorithm AlgorithmIdentifier, 53*05799ae0SJuan Castillo * digest OCTET STRING 54*05799ae0SJuan Castillo * } 55*05799ae0SJuan Castillo * 56*05799ae0SJuan Castillo * SubjectPublicKeyInfo ::= SEQUENCE { 57*05799ae0SJuan Castillo * algorithm AlgorithmIdentifier, 58*05799ae0SJuan Castillo * subjectPublicKey BIT STRING 59*05799ae0SJuan Castillo * } 60*05799ae0SJuan Castillo * 61*05799ae0SJuan Castillo * SignatureAlgorithm ::= AlgorithmIdentifier 62*05799ae0SJuan Castillo * 63*05799ae0SJuan Castillo * SignatureValue ::= BIT STRING 64*05799ae0SJuan Castillo */ 65*05799ae0SJuan Castillo 66*05799ae0SJuan Castillo /* 67*05799ae0SJuan Castillo * Perform some static checking and call the library initialization function 68*05799ae0SJuan Castillo */ 69*05799ae0SJuan Castillo void crypto_mod_init(void) 70*05799ae0SJuan Castillo { 71*05799ae0SJuan Castillo assert(crypto_lib_desc.name != NULL); 72*05799ae0SJuan Castillo assert(crypto_lib_desc.init != NULL); 73*05799ae0SJuan Castillo assert(crypto_lib_desc.verify_signature != NULL); 74*05799ae0SJuan Castillo assert(crypto_lib_desc.verify_hash != NULL); 75*05799ae0SJuan Castillo 76*05799ae0SJuan Castillo /* Initialize the cryptographic library */ 77*05799ae0SJuan Castillo crypto_lib_desc.init(); 78*05799ae0SJuan Castillo INFO("Using crypto library '%s'\n", crypto_lib_desc.name); 79*05799ae0SJuan Castillo } 80*05799ae0SJuan Castillo 81*05799ae0SJuan Castillo /* 82*05799ae0SJuan Castillo * Function to verify a digital signature 83*05799ae0SJuan Castillo * 84*05799ae0SJuan Castillo * Parameters: 85*05799ae0SJuan Castillo * 86*05799ae0SJuan Castillo * data_ptr, data_len: signed data 87*05799ae0SJuan Castillo * sig_ptr, sig_len: the digital signature 88*05799ae0SJuan Castillo * sig_alg_ptr, sig_alg_len: the digital signature algorithm 89*05799ae0SJuan Castillo * pk_ptr, pk_len: the public key 90*05799ae0SJuan Castillo */ 91*05799ae0SJuan Castillo int crypto_mod_verify_signature(void *data_ptr, unsigned int data_len, 92*05799ae0SJuan Castillo void *sig_ptr, unsigned int sig_len, 93*05799ae0SJuan Castillo void *sig_alg_ptr, unsigned int sig_alg_len, 94*05799ae0SJuan Castillo void *pk_ptr, unsigned int pk_len) 95*05799ae0SJuan Castillo { 96*05799ae0SJuan Castillo assert(data_ptr != NULL); 97*05799ae0SJuan Castillo assert(data_len != 0); 98*05799ae0SJuan Castillo assert(sig_ptr != NULL); 99*05799ae0SJuan Castillo assert(sig_len != 0); 100*05799ae0SJuan Castillo assert(sig_alg_ptr != NULL); 101*05799ae0SJuan Castillo assert(sig_alg_len != 0); 102*05799ae0SJuan Castillo assert(pk_ptr != NULL); 103*05799ae0SJuan Castillo assert(pk_len != 0); 104*05799ae0SJuan Castillo 105*05799ae0SJuan Castillo return crypto_lib_desc.verify_signature(data_ptr, data_len, 106*05799ae0SJuan Castillo sig_ptr, sig_len, 107*05799ae0SJuan Castillo sig_alg_ptr, sig_alg_len, 108*05799ae0SJuan Castillo pk_ptr, pk_len); 109*05799ae0SJuan Castillo } 110*05799ae0SJuan Castillo 111*05799ae0SJuan Castillo /* 112*05799ae0SJuan Castillo * Verify a hash by comparison 113*05799ae0SJuan Castillo * 114*05799ae0SJuan Castillo * Parameters: 115*05799ae0SJuan Castillo * 116*05799ae0SJuan Castillo * data_ptr, data_len: data to be hashed 117*05799ae0SJuan Castillo * digest_info_ptr, digest_info_len: hash to be compared 118*05799ae0SJuan Castillo */ 119*05799ae0SJuan Castillo int crypto_mod_verify_hash(void *data_ptr, unsigned int data_len, 120*05799ae0SJuan Castillo void *digest_info_ptr, unsigned int digest_info_len) 121*05799ae0SJuan Castillo { 122*05799ae0SJuan Castillo assert(data_ptr != NULL); 123*05799ae0SJuan Castillo assert(data_len != 0); 124*05799ae0SJuan Castillo assert(digest_info_ptr != NULL); 125*05799ae0SJuan Castillo assert(digest_info_len != 0); 126*05799ae0SJuan Castillo 127*05799ae0SJuan Castillo return crypto_lib_desc.verify_hash(data_ptr, data_len, 128*05799ae0SJuan Castillo digest_info_ptr, digest_info_len); 129*05799ae0SJuan Castillo } 130