blob: bed1c097c2ce146a5b7d2699fc6b32780b5e21bc [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001/* SPDX-License-Identifier: GPL-2.0+ */
Simon Glass35191a32013-06-13 15:10:02 -07002/*
3 * Copyright (c) 2013, Google Inc.
4 *
5 * (C) Copyright 2008 Semihalf
6 *
7 * (C) Copyright 2000-2006
8 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
Simon Glass35191a32013-06-13 15:10:02 -07009 */
10
11#ifndef _RSA_H
12#define _RSA_H
13
14#include <errno.h>
15#include <image.h>
16
Heiko Schocheredaf9b12014-03-03 12:19:26 +010017/**
18 * struct rsa_public_key - holder for a public key
19 *
20 * An RSA public key consists of a modulus (typically called N), the inverse
21 * and R^2, where R is 2^(# key bits).
22 */
23
24struct 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 */
Michael van der Westhuizen89f4ed12014-07-02 10:17:26 +020029 uint64_t exponent; /* public exponent */
Heiko Schocheredaf9b12014-03-03 12:19:26 +010030};
31
Simon Glasse3ee2fb2016-02-22 22:55:43 -070032struct image_sign_info;
33
Simon Glass35191a32013-06-13 15:10:02 -070034#if IMAGE_ENABLE_SIGN
35/**
36 * sign() - calculate and return signature for given input data
37 *
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
43 *
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
48 * should free *sigp.
49 *
50 * @return: 0, on success, -ve on error
51 */
52int rsa_sign(struct image_sign_info *info,
53 const struct image_region region[],
54 int region_count, uint8_t **sigp, uint *sig_len);
55
56/**
57 * add_verify_data() - Add verification information to FDT
58 *
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.
62 *
63 * @info: Specifies key and FIT information
64 * @keydest: Destination FDT blob for public key data
Simon Glassaf2f9d52014-06-02 22:04:51 -060065 * @return: 0, on success, -ENOSPC if the keydest FDT blob ran out of space,
66 other -ve value on error
Simon Glass35191a32013-06-13 15:10:02 -070067*/
68int rsa_add_verify_data(struct image_sign_info *info, void *keydest);
69#else
70static 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)
73{
74 return -ENXIO;
75}
76
77static inline int rsa_add_verify_data(struct image_sign_info *info,
78 void *keydest)
79{
80 return -ENXIO;
81}
82#endif
83
84#if IMAGE_ENABLE_VERIFY
85/**
Heiko Stuebnerb4044092020-05-22 16:20:33 +020086 * rsa_verify_hash() - Verify a signature against a hash
87 *
88 * Verify a RSA PKCS1.5 signature against an expected hash.
89 *
90 * @info: Specifies key and FIT information
91 * @hash: Hash according to algorithm specified in @info
92 * @sig: Signature
93 * @sig_len: Number of bytes in signature
94 * @return 0 if verified, -ve on error
95 */
96int rsa_verify_hash(struct image_sign_info *info,
97 const uint8_t *hash, uint8_t *sig, uint sig_len);
98
99/**
Simon Glass35191a32013-06-13 15:10:02 -0700100 * rsa_verify() - Verify a signature against some data
101 *
102 * Verify a RSA PKCS1.5 signature against an expected hash.
103 *
104 * @info: Specifies key and FIT information
105 * @data: Pointer to the input data
106 * @data_len: Data length
107 * @sig: Signature
108 * @sig_len: Number of bytes in signature
109 * @return 0 if verified, -ve on error
110 */
111int rsa_verify(struct image_sign_info *info,
112 const struct image_region region[], int region_count,
113 uint8_t *sig, uint sig_len);
Philippe Reynes12468352018-11-14 13:51:00 +0100114
AKASHI Takahiro30962bf2020-06-16 14:26:48 +0900115int rsa_verify_with_pkey(struct image_sign_info *info,
116 const void *hash, uint8_t *sig, uint sig_len);
117
Philippe Reynes12468352018-11-14 13:51:00 +0100118int padding_pkcs_15_verify(struct image_sign_info *info,
119 uint8_t *msg, int msg_len,
120 const uint8_t *hash, int hash_len);
Philippe Reynes47d73f02018-11-14 13:51:01 +0100121
122#ifdef CONFIG_FIT_ENABLE_RSASSA_PSS_SUPPORT
123int padding_pss_verify(struct image_sign_info *info,
124 uint8_t *msg, int msg_len,
125 const uint8_t *hash, int hash_len);
126#endif /* CONFIG_FIT_ENABLE_RSASSA_PSS_SUPPORT */
Simon Glass35191a32013-06-13 15:10:02 -0700127#else
Heiko Stuebnerb4044092020-05-22 16:20:33 +0200128static inline int rsa_verify_hash(struct image_sign_info *info,
129 const uint8_t *hash,
130 uint8_t *sig, uint sig_len)
131{
132 return -ENXIO;
133}
134
Simon Glass35191a32013-06-13 15:10:02 -0700135static inline int rsa_verify(struct image_sign_info *info,
136 const struct image_region region[], int region_count,
137 uint8_t *sig, uint sig_len)
138{
139 return -ENXIO;
140}
Philippe Reynes12468352018-11-14 13:51:00 +0100141
142static inline int padding_pkcs_15_verify(struct image_sign_info *info,
143 uint8_t *msg, int msg_len,
144 const uint8_t *hash, int hash_len)
145{
146 return -ENXIO;
147}
Philippe Reynes47d73f02018-11-14 13:51:01 +0100148
149#ifdef CONFIG_FIT_ENABLE_RSASSA_PSS_SUPPORT
150static inline int padding_pss_verify(struct image_sign_info *info,
151 uint8_t *msg, int msg_len,
152 const uint8_t *hash, int hash_len)
153{
154 return -ENXIO;
155}
156#endif /* CONFIG_FIT_ENABLE_RSASSA_PSS_SUPPORT */
Simon Glass35191a32013-06-13 15:10:02 -0700157#endif
158
Philippe Reynes12468352018-11-14 13:51:00 +0100159#define RSA_DEFAULT_PADDING_NAME "pkcs-1.5"
160
Heiko Schocher4b817562014-03-03 12:19:27 +0100161#define RSA2048_BYTES (2048 / 8)
162#define RSA4096_BYTES (4096 / 8)
163
164/* This is the minimum/maximum key size we support, in bits */
165#define RSA_MIN_KEY_BITS 2048
166#define RSA_MAX_KEY_BITS 4096
167
168/* This is the maximum signature length that we support, in bits */
169#define RSA_MAX_SIG_BITS 4096
170
Simon Glass35191a32013-06-13 15:10:02 -0700171#endif