xref: /rk3399_rockchip-uboot/include/android_avb/avb_crypto.h (revision ab608f806ee1d7fa63a18cc035e8ea62b67634e5)
15b69db07SJason Zhu /*
25b69db07SJason Zhu  * Copyright (C) 2016 The Android Open Source Project
35b69db07SJason Zhu  *
45b69db07SJason Zhu  * Permission is hereby granted, free of charge, to any person
55b69db07SJason Zhu  * obtaining a copy of this software and associated documentation
65b69db07SJason Zhu  * files (the "Software"), to deal in the Software without
75b69db07SJason Zhu  * restriction, including without limitation the rights to use, copy,
85b69db07SJason Zhu  * modify, merge, publish, distribute, sublicense, and/or sell copies
95b69db07SJason Zhu  * of the Software, and to permit persons to whom the Software is
105b69db07SJason Zhu  * furnished to do so, subject to the following conditions:
115b69db07SJason Zhu  *
125b69db07SJason Zhu  * The above copyright notice and this permission notice shall be
135b69db07SJason Zhu  * included in all copies or substantial portions of the Software.
145b69db07SJason Zhu  *
155b69db07SJason Zhu  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
165b69db07SJason Zhu  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
175b69db07SJason Zhu  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
185b69db07SJason Zhu  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
195b69db07SJason Zhu  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
205b69db07SJason Zhu  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
215b69db07SJason Zhu  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
225b69db07SJason Zhu  * SOFTWARE.
235b69db07SJason Zhu  */
245b69db07SJason Zhu 
255b69db07SJason Zhu /*
265b69db07SJason Zhu #if !defined(AVB_INSIDE_LIBAVB_H) && !defined(AVB_COMPILATION)
275b69db07SJason Zhu #error "Never include this file directly, include libavb.h instead."
285b69db07SJason Zhu #endif
295b69db07SJason Zhu */
305b69db07SJason Zhu 
315b69db07SJason Zhu #ifndef AVB_CRYPTO_H_
325b69db07SJason Zhu #define AVB_CRYPTO_H_
335b69db07SJason Zhu 
345b69db07SJason Zhu #include <android_avb/avb_sysdeps.h>
355b69db07SJason Zhu 
365b69db07SJason Zhu #ifdef __cplusplus
375b69db07SJason Zhu extern "C" {
385b69db07SJason Zhu #endif
395b69db07SJason Zhu 
405b69db07SJason Zhu /* Size of a RSA-2048 signature. */
415b69db07SJason Zhu #define AVB_RSA2048_NUM_BYTES 256
425b69db07SJason Zhu 
435b69db07SJason Zhu /* Size of a RSA-4096 signature. */
445b69db07SJason Zhu #define AVB_RSA4096_NUM_BYTES 512
455b69db07SJason Zhu 
465b69db07SJason Zhu /* Size of a RSA-8192 signature. */
475b69db07SJason Zhu #define AVB_RSA8192_NUM_BYTES 1024
485b69db07SJason Zhu 
49*ab608f80SJason Zhu /* Size in bytes of a SHA-1 digest. */
50*ab608f80SJason Zhu #define AVB_SHA1_DIGEST_SIZE 20
51*ab608f80SJason Zhu 
525b69db07SJason Zhu /* Size in bytes of a SHA-256 digest. */
535b69db07SJason Zhu #define AVB_SHA256_DIGEST_SIZE 32
545b69db07SJason Zhu 
555b69db07SJason Zhu /* Size in bytes of a SHA-512 digest. */
565b69db07SJason Zhu #define AVB_SHA512_DIGEST_SIZE 64
575b69db07SJason Zhu 
58*ab608f80SJason Zhu /* Possible digest types supported by libavb routines. */
59*ab608f80SJason Zhu typedef enum {
60*ab608f80SJason Zhu   AVB_DIGEST_TYPE_SHA256,
61*ab608f80SJason Zhu   AVB_DIGEST_TYPE_SHA512,
62*ab608f80SJason Zhu } AvbDigestType;
63*ab608f80SJason Zhu 
645b69db07SJason Zhu /* Algorithms that can be used in the vbmeta image for
655b69db07SJason Zhu  * verification. An algorithm consists of a hash type and a signature
665b69db07SJason Zhu  * type.
675b69db07SJason Zhu  *
685b69db07SJason Zhu  * The data used to calculate the hash is the three blocks mentioned
695b69db07SJason Zhu  * in the documentation for |AvbVBMetaImageHeader| except for the data
705b69db07SJason Zhu  * in the "Authentication data" block.
715b69db07SJason Zhu  *
725b69db07SJason Zhu  * For signatures with RSA keys, PKCS v1.5 padding is used. The public
735b69db07SJason Zhu  * key data is stored in the auxiliary data block, see
745b69db07SJason Zhu  * |AvbRSAPublicKeyHeader| for the serialization format.
755b69db07SJason Zhu  *
765b69db07SJason Zhu  * Each algorithm type is described below:
775b69db07SJason Zhu  *
785b69db07SJason Zhu  * AVB_ALGORITHM_TYPE_NONE: There is no hash, no signature of the
795b69db07SJason Zhu  * data, and no public key. The data cannot be verified. The fields
805b69db07SJason Zhu  * |hash_size|, |signature_size|, and |public_key_size| must be zero.
815b69db07SJason Zhu  *
825b69db07SJason Zhu  * AVB_ALGORITHM_TYPE_SHA256_RSA2048: The hash function used is
835b69db07SJason Zhu  * SHA-256, resulting in 32 bytes of hash digest data. This hash is
845b69db07SJason Zhu  * signed with a 2048-bit RSA key. The field |hash_size| must be 32,
855b69db07SJason Zhu  * |signature_size| must be 256, and the public key data must have
865b69db07SJason Zhu  * |key_num_bits| set to 2048.
875b69db07SJason Zhu  *
885b69db07SJason Zhu  * AVB_ALGORITHM_TYPE_SHA256_RSA4096: Like above, but only with
895b69db07SJason Zhu  * a 4096-bit RSA key and |signature_size| set to 512.
905b69db07SJason Zhu  *
915b69db07SJason Zhu  * AVB_ALGORITHM_TYPE_SHA256_RSA8192: Like above, but only with
925b69db07SJason Zhu  * a 8192-bit RSA key and |signature_size| set to 1024.
935b69db07SJason Zhu  *
945b69db07SJason Zhu  * AVB_ALGORITHM_TYPE_SHA512_RSA2048: The hash function used is
955b69db07SJason Zhu  * SHA-512, resulting in 64 bytes of hash digest data. This hash is
965b69db07SJason Zhu  * signed with a 2048-bit RSA key. The field |hash_size| must be 64,
975b69db07SJason Zhu  * |signature_size| must be 256, and the public key data must have
985b69db07SJason Zhu  * |key_num_bits| set to 2048.
995b69db07SJason Zhu  *
1005b69db07SJason Zhu  * AVB_ALGORITHM_TYPE_SHA512_RSA4096: Like above, but only with
1015b69db07SJason Zhu  * a 4096-bit RSA key and |signature_size| set to 512.
1025b69db07SJason Zhu  *
1035b69db07SJason Zhu  * AVB_ALGORITHM_TYPE_SHA512_RSA8192: Like above, but only with
1045b69db07SJason Zhu  * a 8192-bit RSA key and |signature_size| set to 1024.
1055b69db07SJason Zhu  */
1065b69db07SJason Zhu typedef enum {
1075b69db07SJason Zhu   AVB_ALGORITHM_TYPE_NONE,
1085b69db07SJason Zhu   AVB_ALGORITHM_TYPE_SHA256_RSA2048,
1095b69db07SJason Zhu   AVB_ALGORITHM_TYPE_SHA256_RSA4096,
1105b69db07SJason Zhu   AVB_ALGORITHM_TYPE_SHA256_RSA8192,
1115b69db07SJason Zhu   AVB_ALGORITHM_TYPE_SHA512_RSA2048,
1125b69db07SJason Zhu   AVB_ALGORITHM_TYPE_SHA512_RSA4096,
1135b69db07SJason Zhu   AVB_ALGORITHM_TYPE_SHA512_RSA8192,
1145b69db07SJason Zhu   _AVB_ALGORITHM_NUM_TYPES
1155b69db07SJason Zhu } AvbAlgorithmType;
1165b69db07SJason Zhu 
1175b69db07SJason Zhu /* Holds algorithm-specific data. The |padding| is needed by avb_rsa_verify. */
1185b69db07SJason Zhu typedef struct {
1195b69db07SJason Zhu   const uint8_t* padding;
1205b69db07SJason Zhu   size_t padding_len;
1215b69db07SJason Zhu   size_t hash_len;
1225b69db07SJason Zhu } AvbAlgorithmData;
1235b69db07SJason Zhu 
1245b69db07SJason Zhu /* Provides algorithm-specific data for a given |algorithm|. Returns NULL if
1255b69db07SJason Zhu  * |algorithm| is invalid.
1265b69db07SJason Zhu  */
1275b69db07SJason Zhu const AvbAlgorithmData* avb_get_algorithm_data(AvbAlgorithmType algorithm)
1285b69db07SJason Zhu     AVB_ATTR_WARN_UNUSED_RESULT;
1295b69db07SJason Zhu 
1305b69db07SJason Zhu /* The header for a serialized RSA public key.
1315b69db07SJason Zhu  *
1325b69db07SJason Zhu  * The size of the key is given by |key_num_bits|, for example 2048
1335b69db07SJason Zhu  * for a RSA-2048 key. By definition, a RSA public key is the pair (n,
1345b69db07SJason Zhu  * e) where |n| is the modulus (which can be represented in
1355b69db07SJason Zhu  * |key_num_bits| bits) and |e| is the public exponent. The exponent
1365b69db07SJason Zhu  * is not stored since it's assumed to always be 65537.
1375b69db07SJason Zhu  *
1385b69db07SJason Zhu  * To optimize verification, the key block includes two precomputed
1395b69db07SJason Zhu  * values, |n0inv| (fits in 32 bits) and |rr| and can always be
1405b69db07SJason Zhu  * represented in |key_num_bits|.
1415b69db07SJason Zhu 
1425b69db07SJason Zhu  * The value |n0inv| is the value -1/n[0] (mod 2^32). The value |rr|
1435b69db07SJason Zhu  * is (2^key_num_bits)^2 (mod n).
1445b69db07SJason Zhu  *
1455b69db07SJason Zhu  * Following this header is |key_num_bits| bits of |n|, then
1465b69db07SJason Zhu  * |key_num_bits| bits of |rr|. Both values are stored with most
1475b69db07SJason Zhu  * significant bit first. Each serialized number takes up
1485b69db07SJason Zhu  * |key_num_bits|/8 bytes.
1495b69db07SJason Zhu  *
1505b69db07SJason Zhu  * All fields in this struct are stored in network byte order when
1515b69db07SJason Zhu  * serialized.  To generate a copy with fields swapped to native byte
1525b69db07SJason Zhu  * order, use the function avb_rsa_public_key_header_validate_and_byteswap().
1535b69db07SJason Zhu  *
1545b69db07SJason Zhu  * The avb_rsa_verify() function expects a key in this serialized
1555b69db07SJason Zhu  * format.
1565b69db07SJason Zhu  *
1575b69db07SJason Zhu  * The 'avbtool extract_public_key' command can be used to generate a
1585b69db07SJason Zhu  * serialized RSA public key.
1595b69db07SJason Zhu  */
1605b69db07SJason Zhu typedef struct AvbRSAPublicKeyHeader {
1615b69db07SJason Zhu   uint32_t key_num_bits;
1625b69db07SJason Zhu   uint32_t n0inv;
1635b69db07SJason Zhu } AVB_ATTR_PACKED AvbRSAPublicKeyHeader;
1645b69db07SJason Zhu 
1655b69db07SJason Zhu /* Copies |src| to |dest| and validates, byte-swapping fields in the
1665b69db07SJason Zhu  * process if needed. Returns true if valid, false if invalid.
1675b69db07SJason Zhu  */
1685b69db07SJason Zhu bool avb_rsa_public_key_header_validate_and_byteswap(
1695b69db07SJason Zhu     const AvbRSAPublicKeyHeader* src,
1705b69db07SJason Zhu     AvbRSAPublicKeyHeader* dest) AVB_ATTR_WARN_UNUSED_RESULT;
1715b69db07SJason Zhu 
1725b69db07SJason Zhu #ifdef __cplusplus
1735b69db07SJason Zhu }
1745b69db07SJason Zhu #endif
1755b69db07SJason Zhu 
1765b69db07SJason Zhu #endif /* AVB_CRYPTO_H_ */
177