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