1 /* SPDX-License-Identifier: GPL-2.0+ */
3 * Copyright (c) 2013, Google Inc.
5 * (C) Copyright 2008 Semihalf
7 * (C) Copyright 2000-2006
8 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
18 * struct rsa_public_key - holder for a public key
20 * An RSA public key consists of a modulus (typically called N), the inverse
21 * and R^2, where R is 2^(# key bits).
24 struct rsa_public_key {
25 uint len; /* len of modulus[] in number of uint32_t */
26 uint32_t n0inv; /* -1 / modulus[0] mod 2^32 */
27 uint32_t *modulus; /* modulus as little endian array */
28 uint32_t *rr; /* R^2 as little endian array */
29 uint64_t exponent; /* public exponent */
32 struct image_sign_info;
36 * sign() - calculate and return signature for given input data
38 * @info: Specifies key and FIT information
39 * @data: Pointer to the input data
40 * @data_len: Data length
41 * @sigp: Set to an allocated buffer holding the signature
42 * @sig_len: Set to length of the calculated hash
44 * This computes input data signature according to selected algorithm.
45 * Resulting signature value is placed in an allocated buffer, the
46 * pointer is returned as *sigp. The length of the calculated
47 * signature is returned via the sig_len pointer argument. The caller
50 * @return: 0, on success, -ve on error
52 int rsa_sign(struct image_sign_info *info,
53 const struct image_region region[],
54 int region_count, uint8_t **sigp, uint *sig_len);
57 * add_verify_data() - Add verification information to FDT
59 * Add public key information to the FDT node, suitable for
60 * verification at run-time. The information added depends on the
61 * algorithm being used.
63 * @info: Specifies key and FIT information
64 * @keydest: Destination FDT blob for public key data
65 * @return: 0, on success, -ENOSPC if the keydest FDT blob ran out of space,
66 other -ve value on error
68 int rsa_add_verify_data(struct image_sign_info *info, void *keydest);
70 static inline int rsa_sign(struct image_sign_info *info,
71 const struct image_region region[], int region_count,
72 uint8_t **sigp, uint *sig_len)
77 static inline int rsa_add_verify_data(struct image_sign_info *info,
84 #if IMAGE_ENABLE_VERIFY
86 * rsa_verify() - Verify a signature against some data
88 * Verify a RSA PKCS1.5 signature against an expected hash.
90 * @info: Specifies key and FIT information
91 * @data: Pointer to the input data
92 * @data_len: Data length
94 * @sig_len: Number of bytes in signature
95 * @return 0 if verified, -ve on error
97 int rsa_verify(struct image_sign_info *info,
98 const struct image_region region[], int region_count,
99 uint8_t *sig, uint sig_len);
101 int padding_pkcs_15_verify(struct image_sign_info *info,
102 uint8_t *msg, int msg_len,
103 const uint8_t *hash, int hash_len);
105 #ifdef CONFIG_FIT_ENABLE_RSASSA_PSS_SUPPORT
106 int padding_pss_verify(struct image_sign_info *info,
107 uint8_t *msg, int msg_len,
108 const uint8_t *hash, int hash_len);
109 #endif /* CONFIG_FIT_ENABLE_RSASSA_PSS_SUPPORT */
111 static inline int rsa_verify(struct image_sign_info *info,
112 const struct image_region region[], int region_count,
113 uint8_t *sig, uint sig_len)
118 static inline int padding_pkcs_15_verify(struct image_sign_info *info,
119 uint8_t *msg, int msg_len,
120 const uint8_t *hash, int hash_len)
125 #ifdef CONFIG_FIT_ENABLE_RSASSA_PSS_SUPPORT
126 static inline int padding_pss_verify(struct image_sign_info *info,
127 uint8_t *msg, int msg_len,
128 const uint8_t *hash, int hash_len)
132 #endif /* CONFIG_FIT_ENABLE_RSASSA_PSS_SUPPORT */
135 #define RSA_DEFAULT_PADDING_NAME "pkcs-1.5"
137 #define RSA2048_BYTES (2048 / 8)
138 #define RSA4096_BYTES (4096 / 8)
140 /* This is the minimum/maximum key size we support, in bits */
141 #define RSA_MIN_KEY_BITS 2048
142 #define RSA_MAX_KEY_BITS 4096
144 /* This is the maximum signature length that we support, in bits */
145 #define RSA_MAX_SIG_BITS 4096