1*d289c2baSAndroid Build Coastguard Worker /* 2*d289c2baSAndroid Build Coastguard Worker * Copyright (C) 2016 The Android Open Source Project 3*d289c2baSAndroid Build Coastguard Worker * 4*d289c2baSAndroid Build Coastguard Worker * Permission is hereby granted, free of charge, to any person 5*d289c2baSAndroid Build Coastguard Worker * obtaining a copy of this software and associated documentation 6*d289c2baSAndroid Build Coastguard Worker * files (the "Software"), to deal in the Software without 7*d289c2baSAndroid Build Coastguard Worker * restriction, including without limitation the rights to use, copy, 8*d289c2baSAndroid Build Coastguard Worker * modify, merge, publish, distribute, sublicense, and/or sell copies 9*d289c2baSAndroid Build Coastguard Worker * of the Software, and to permit persons to whom the Software is 10*d289c2baSAndroid Build Coastguard Worker * furnished to do so, subject to the following conditions: 11*d289c2baSAndroid Build Coastguard Worker * 12*d289c2baSAndroid Build Coastguard Worker * The above copyright notice and this permission notice shall be 13*d289c2baSAndroid Build Coastguard Worker * included in all copies or substantial portions of the Software. 14*d289c2baSAndroid Build Coastguard Worker * 15*d289c2baSAndroid Build Coastguard Worker * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16*d289c2baSAndroid Build Coastguard Worker * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17*d289c2baSAndroid Build Coastguard Worker * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18*d289c2baSAndroid Build Coastguard Worker * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19*d289c2baSAndroid Build Coastguard Worker * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20*d289c2baSAndroid Build Coastguard Worker * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21*d289c2baSAndroid Build Coastguard Worker * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22*d289c2baSAndroid Build Coastguard Worker * SOFTWARE. 23*d289c2baSAndroid Build Coastguard Worker */ 24*d289c2baSAndroid Build Coastguard Worker 25*d289c2baSAndroid Build Coastguard Worker #if !defined(AVB_INSIDE_LIBAVB_H) && !defined(AVB_COMPILATION) 26*d289c2baSAndroid Build Coastguard Worker #error "Never include this file directly, include libavb.h instead." 27*d289c2baSAndroid Build Coastguard Worker #endif 28*d289c2baSAndroid Build Coastguard Worker 29*d289c2baSAndroid Build Coastguard Worker #ifndef AVB_CRYPTO_H_ 30*d289c2baSAndroid Build Coastguard Worker #define AVB_CRYPTO_H_ 31*d289c2baSAndroid Build Coastguard Worker 32*d289c2baSAndroid Build Coastguard Worker #include "avb_sysdeps.h" 33*d289c2baSAndroid Build Coastguard Worker 34*d289c2baSAndroid Build Coastguard Worker #ifdef __cplusplus 35*d289c2baSAndroid Build Coastguard Worker extern "C" { 36*d289c2baSAndroid Build Coastguard Worker #endif 37*d289c2baSAndroid Build Coastguard Worker 38*d289c2baSAndroid Build Coastguard Worker /* Size of a RSA-2048 signature. */ 39*d289c2baSAndroid Build Coastguard Worker #define AVB_RSA2048_NUM_BYTES 256 40*d289c2baSAndroid Build Coastguard Worker 41*d289c2baSAndroid Build Coastguard Worker /* Size of a RSA-4096 signature. */ 42*d289c2baSAndroid Build Coastguard Worker #define AVB_RSA4096_NUM_BYTES 512 43*d289c2baSAndroid Build Coastguard Worker 44*d289c2baSAndroid Build Coastguard Worker /* Size of a RSA-8192 signature. */ 45*d289c2baSAndroid Build Coastguard Worker #define AVB_RSA8192_NUM_BYTES 1024 46*d289c2baSAndroid Build Coastguard Worker 47*d289c2baSAndroid Build Coastguard Worker /* Size in bytes of a SHA-1 digest. */ 48*d289c2baSAndroid Build Coastguard Worker #define AVB_SHA1_DIGEST_SIZE 20 49*d289c2baSAndroid Build Coastguard Worker 50*d289c2baSAndroid Build Coastguard Worker /* Size in bytes of a SHA-256 digest. */ 51*d289c2baSAndroid Build Coastguard Worker #define AVB_SHA256_DIGEST_SIZE 32 52*d289c2baSAndroid Build Coastguard Worker 53*d289c2baSAndroid Build Coastguard Worker /* Size in bytes of a SHA-512 digest. */ 54*d289c2baSAndroid Build Coastguard Worker #define AVB_SHA512_DIGEST_SIZE 64 55*d289c2baSAndroid Build Coastguard Worker 56*d289c2baSAndroid Build Coastguard Worker /* Possible digest types supported by libavb routines. */ 57*d289c2baSAndroid Build Coastguard Worker typedef enum { 58*d289c2baSAndroid Build Coastguard Worker AVB_DIGEST_TYPE_SHA256, 59*d289c2baSAndroid Build Coastguard Worker AVB_DIGEST_TYPE_SHA512, 60*d289c2baSAndroid Build Coastguard Worker } AvbDigestType; 61*d289c2baSAndroid Build Coastguard Worker 62*d289c2baSAndroid Build Coastguard Worker /* Algorithms that can be used in the vbmeta image for 63*d289c2baSAndroid Build Coastguard Worker * verification. An algorithm consists of a hash type and a signature 64*d289c2baSAndroid Build Coastguard Worker * type. 65*d289c2baSAndroid Build Coastguard Worker * 66*d289c2baSAndroid Build Coastguard Worker * The data used to calculate the hash is the three blocks mentioned 67*d289c2baSAndroid Build Coastguard Worker * in the documentation for |AvbVBMetaImageHeader| except for the data 68*d289c2baSAndroid Build Coastguard Worker * in the "Authentication data" block. 69*d289c2baSAndroid Build Coastguard Worker * 70*d289c2baSAndroid Build Coastguard Worker * For signatures with RSA keys, PKCS v1.5 padding is used. The public 71*d289c2baSAndroid Build Coastguard Worker * key data is stored in the auxiliary data block, see 72*d289c2baSAndroid Build Coastguard Worker * |AvbRSAPublicKeyHeader| for the serialization format. 73*d289c2baSAndroid Build Coastguard Worker * 74*d289c2baSAndroid Build Coastguard Worker * Each algorithm type is described below: 75*d289c2baSAndroid Build Coastguard Worker * 76*d289c2baSAndroid Build Coastguard Worker * AVB_ALGORITHM_TYPE_NONE: There is no hash, no signature of the 77*d289c2baSAndroid Build Coastguard Worker * data, and no public key. The data cannot be verified. The fields 78*d289c2baSAndroid Build Coastguard Worker * |hash_size|, |signature_size|, and |public_key_size| must be zero. 79*d289c2baSAndroid Build Coastguard Worker * 80*d289c2baSAndroid Build Coastguard Worker * AVB_ALGORITHM_TYPE_SHA256_RSA2048: The hash function used is 81*d289c2baSAndroid Build Coastguard Worker * SHA-256, resulting in 32 bytes of hash digest data. This hash is 82*d289c2baSAndroid Build Coastguard Worker * signed with a 2048-bit RSA key. The field |hash_size| must be 32, 83*d289c2baSAndroid Build Coastguard Worker * |signature_size| must be 256, and the public key data must have 84*d289c2baSAndroid Build Coastguard Worker * |key_num_bits| set to 2048. 85*d289c2baSAndroid Build Coastguard Worker * 86*d289c2baSAndroid Build Coastguard Worker * AVB_ALGORITHM_TYPE_SHA256_RSA4096: Like above, but only with 87*d289c2baSAndroid Build Coastguard Worker * a 4096-bit RSA key and |signature_size| set to 512. 88*d289c2baSAndroid Build Coastguard Worker * 89*d289c2baSAndroid Build Coastguard Worker * AVB_ALGORITHM_TYPE_SHA256_RSA8192: Like above, but only with 90*d289c2baSAndroid Build Coastguard Worker * a 8192-bit RSA key and |signature_size| set to 1024. 91*d289c2baSAndroid Build Coastguard Worker * 92*d289c2baSAndroid Build Coastguard Worker * AVB_ALGORITHM_TYPE_SHA512_RSA2048: The hash function used is 93*d289c2baSAndroid Build Coastguard Worker * SHA-512, resulting in 64 bytes of hash digest data. This hash is 94*d289c2baSAndroid Build Coastguard Worker * signed with a 2048-bit RSA key. The field |hash_size| must be 64, 95*d289c2baSAndroid Build Coastguard Worker * |signature_size| must be 256, and the public key data must have 96*d289c2baSAndroid Build Coastguard Worker * |key_num_bits| set to 2048. 97*d289c2baSAndroid Build Coastguard Worker * 98*d289c2baSAndroid Build Coastguard Worker * AVB_ALGORITHM_TYPE_SHA512_RSA4096: Like above, but only with 99*d289c2baSAndroid Build Coastguard Worker * a 4096-bit RSA key and |signature_size| set to 512. 100*d289c2baSAndroid Build Coastguard Worker * 101*d289c2baSAndroid Build Coastguard Worker * AVB_ALGORITHM_TYPE_SHA512_RSA8192: Like above, but only with 102*d289c2baSAndroid Build Coastguard Worker * a 8192-bit RSA key and |signature_size| set to 1024. 103*d289c2baSAndroid Build Coastguard Worker */ 104*d289c2baSAndroid Build Coastguard Worker typedef enum { 105*d289c2baSAndroid Build Coastguard Worker AVB_ALGORITHM_TYPE_NONE, 106*d289c2baSAndroid Build Coastguard Worker AVB_ALGORITHM_TYPE_SHA256_RSA2048, 107*d289c2baSAndroid Build Coastguard Worker AVB_ALGORITHM_TYPE_SHA256_RSA4096, 108*d289c2baSAndroid Build Coastguard Worker AVB_ALGORITHM_TYPE_SHA256_RSA8192, 109*d289c2baSAndroid Build Coastguard Worker AVB_ALGORITHM_TYPE_SHA512_RSA2048, 110*d289c2baSAndroid Build Coastguard Worker AVB_ALGORITHM_TYPE_SHA512_RSA4096, 111*d289c2baSAndroid Build Coastguard Worker AVB_ALGORITHM_TYPE_SHA512_RSA8192, 112*d289c2baSAndroid Build Coastguard Worker _AVB_ALGORITHM_NUM_TYPES 113*d289c2baSAndroid Build Coastguard Worker } AvbAlgorithmType; 114*d289c2baSAndroid Build Coastguard Worker 115*d289c2baSAndroid Build Coastguard Worker /* Holds algorithm-specific data. The |padding| is needed by avb_rsa_verify. */ 116*d289c2baSAndroid Build Coastguard Worker typedef struct { 117*d289c2baSAndroid Build Coastguard Worker const uint8_t* padding; 118*d289c2baSAndroid Build Coastguard Worker size_t padding_len; 119*d289c2baSAndroid Build Coastguard Worker size_t hash_len; 120*d289c2baSAndroid Build Coastguard Worker } AvbAlgorithmData; 121*d289c2baSAndroid Build Coastguard Worker 122*d289c2baSAndroid Build Coastguard Worker /* Provides algorithm-specific data for a given |algorithm|. Returns NULL if 123*d289c2baSAndroid Build Coastguard Worker * |algorithm| is invalid. 124*d289c2baSAndroid Build Coastguard Worker */ 125*d289c2baSAndroid Build Coastguard Worker const AvbAlgorithmData* avb_get_algorithm_data(AvbAlgorithmType algorithm) 126*d289c2baSAndroid Build Coastguard Worker AVB_ATTR_WARN_UNUSED_RESULT; 127*d289c2baSAndroid Build Coastguard Worker 128*d289c2baSAndroid Build Coastguard Worker /* The header for a serialized RSA public key. 129*d289c2baSAndroid Build Coastguard Worker * 130*d289c2baSAndroid Build Coastguard Worker * The size of the key is given by |key_num_bits|, for example 2048 131*d289c2baSAndroid Build Coastguard Worker * for a RSA-2048 key. By definition, a RSA public key is the pair (n, 132*d289c2baSAndroid Build Coastguard Worker * e) where |n| is the modulus (which can be represented in 133*d289c2baSAndroid Build Coastguard Worker * |key_num_bits| bits) and |e| is the public exponent. The exponent 134*d289c2baSAndroid Build Coastguard Worker * is not stored since it's assumed to always be 65537. 135*d289c2baSAndroid Build Coastguard Worker * 136*d289c2baSAndroid Build Coastguard Worker * To optimize verification, the key block includes two precomputed 137*d289c2baSAndroid Build Coastguard Worker * values, |n0inv| (fits in 32 bits) and |rr| and can always be 138*d289c2baSAndroid Build Coastguard Worker * represented in |key_num_bits|. 139*d289c2baSAndroid Build Coastguard Worker 140*d289c2baSAndroid Build Coastguard Worker * The value |n0inv| is the value -1/n[0] (mod 2^32). The value |rr| 141*d289c2baSAndroid Build Coastguard Worker * is (2^key_num_bits)^2 (mod n). 142*d289c2baSAndroid Build Coastguard Worker * 143*d289c2baSAndroid Build Coastguard Worker * Following this header is |key_num_bits| bits of |n|, then 144*d289c2baSAndroid Build Coastguard Worker * |key_num_bits| bits of |rr|. Both values are stored with most 145*d289c2baSAndroid Build Coastguard Worker * significant bit first. Each serialized number takes up 146*d289c2baSAndroid Build Coastguard Worker * |key_num_bits|/8 bytes. 147*d289c2baSAndroid Build Coastguard Worker * 148*d289c2baSAndroid Build Coastguard Worker * All fields in this struct are stored in network byte order when 149*d289c2baSAndroid Build Coastguard Worker * serialized. To generate a copy with fields swapped to native byte 150*d289c2baSAndroid Build Coastguard Worker * order, use the function avb_rsa_public_key_header_validate_and_byteswap(). 151*d289c2baSAndroid Build Coastguard Worker * 152*d289c2baSAndroid Build Coastguard Worker * The avb_rsa_verify() function expects a key in this serialized 153*d289c2baSAndroid Build Coastguard Worker * format. 154*d289c2baSAndroid Build Coastguard Worker * 155*d289c2baSAndroid Build Coastguard Worker * The 'avbtool extract_public_key' command can be used to generate a 156*d289c2baSAndroid Build Coastguard Worker * serialized RSA public key. 157*d289c2baSAndroid Build Coastguard Worker */ 158*d289c2baSAndroid Build Coastguard Worker typedef struct AvbRSAPublicKeyHeader { 159*d289c2baSAndroid Build Coastguard Worker uint32_t key_num_bits; 160*d289c2baSAndroid Build Coastguard Worker uint32_t n0inv; 161*d289c2baSAndroid Build Coastguard Worker } AVB_ATTR_PACKED AvbRSAPublicKeyHeader; 162*d289c2baSAndroid Build Coastguard Worker 163*d289c2baSAndroid Build Coastguard Worker /* Copies |src| to |dest| and validates, byte-swapping fields in the 164*d289c2baSAndroid Build Coastguard Worker * process if needed. Returns true if valid, false if invalid. 165*d289c2baSAndroid Build Coastguard Worker */ 166*d289c2baSAndroid Build Coastguard Worker bool avb_rsa_public_key_header_validate_and_byteswap( 167*d289c2baSAndroid Build Coastguard Worker const AvbRSAPublicKeyHeader* src, 168*d289c2baSAndroid Build Coastguard Worker AvbRSAPublicKeyHeader* dest) AVB_ATTR_WARN_UNUSED_RESULT; 169*d289c2baSAndroid Build Coastguard Worker 170*d289c2baSAndroid Build Coastguard Worker #ifdef __cplusplus 171*d289c2baSAndroid Build Coastguard Worker } 172*d289c2baSAndroid Build Coastguard Worker #endif 173*d289c2baSAndroid Build Coastguard Worker 174*d289c2baSAndroid Build Coastguard Worker #endif /* AVB_CRYPTO_H_ */ 175