Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0+ */ |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 2 | /* |
| 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 Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #ifndef _RSA_H |
| 12 | #define _RSA_H |
| 13 | |
| 14 | #include <errno.h> |
| 15 | #include <image.h> |
| 16 | |
Heiko Schocher | edaf9b1 | 2014-03-03 12:19:26 +0100 | [diff] [blame] | 17 | /** |
| 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 | |
| 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 */ |
Michael van der Westhuizen | 89f4ed1 | 2014-07-02 10:17:26 +0200 | [diff] [blame] | 29 | uint64_t exponent; /* public exponent */ |
Heiko Schocher | edaf9b1 | 2014-03-03 12:19:26 +0100 | [diff] [blame] | 30 | }; |
| 31 | |
Simon Glass | e3ee2fb | 2016-02-22 22:55:43 -0700 | [diff] [blame] | 32 | struct image_sign_info; |
| 33 | |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 34 | /** |
| 35 | * sign() - calculate and return signature for given input data |
| 36 | * |
| 37 | * @info: Specifies key and FIT information |
| 38 | * @data: Pointer to the input data |
| 39 | * @data_len: Data length |
| 40 | * @sigp: Set to an allocated buffer holding the signature |
| 41 | * @sig_len: Set to length of the calculated hash |
| 42 | * |
| 43 | * This computes input data signature according to selected algorithm. |
| 44 | * Resulting signature value is placed in an allocated buffer, the |
| 45 | * pointer is returned as *sigp. The length of the calculated |
| 46 | * signature is returned via the sig_len pointer argument. The caller |
| 47 | * should free *sigp. |
| 48 | * |
| 49 | * @return: 0, on success, -ve on error |
| 50 | */ |
| 51 | int rsa_sign(struct image_sign_info *info, |
| 52 | const struct image_region region[], |
| 53 | int region_count, uint8_t **sigp, uint *sig_len); |
| 54 | |
| 55 | /** |
| 56 | * add_verify_data() - Add verification information to FDT |
| 57 | * |
| 58 | * Add public key information to the FDT node, suitable for |
| 59 | * verification at run-time. The information added depends on the |
| 60 | * algorithm being used. |
| 61 | * |
| 62 | * @info: Specifies key and FIT information |
| 63 | * @keydest: Destination FDT blob for public key data |
Simon Glass | 94336dc | 2021-11-12 12:28:11 -0700 | [diff] [blame] | 64 | * @return: node offset within the FDT blob where the data was written on |
| 65 | * success, -ENOSPC if the keydest FDT blob ran out of space, other -ve |
| 66 | * value on other error |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 67 | */ |
| 68 | int rsa_add_verify_data(struct image_sign_info *info, void *keydest); |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 69 | |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 70 | /** |
Heiko Stuebner | b404409 | 2020-05-22 16:20:33 +0200 | [diff] [blame] | 71 | * rsa_verify_hash() - Verify a signature against a hash |
| 72 | * |
| 73 | * Verify a RSA PKCS1.5 signature against an expected hash. |
| 74 | * |
| 75 | * @info: Specifies key and FIT information |
| 76 | * @hash: Hash according to algorithm specified in @info |
| 77 | * @sig: Signature |
| 78 | * @sig_len: Number of bytes in signature |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 79 | * Return: 0 if verified, -ve on error |
Heiko Stuebner | b404409 | 2020-05-22 16:20:33 +0200 | [diff] [blame] | 80 | */ |
| 81 | int rsa_verify_hash(struct image_sign_info *info, |
| 82 | const uint8_t *hash, uint8_t *sig, uint sig_len); |
| 83 | |
| 84 | /** |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 85 | * rsa_verify() - Verify a signature against some data |
| 86 | * |
| 87 | * Verify a RSA PKCS1.5 signature against an expected hash. |
| 88 | * |
| 89 | * @info: Specifies key and FIT information |
| 90 | * @data: Pointer to the input data |
| 91 | * @data_len: Data length |
| 92 | * @sig: Signature |
| 93 | * @sig_len: Number of bytes in signature |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 94 | * Return: 0 if verified, -ve on error |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 95 | */ |
| 96 | int rsa_verify(struct image_sign_info *info, |
| 97 | const struct image_region region[], int region_count, |
| 98 | uint8_t *sig, uint sig_len); |
Philippe Reynes | 1246835 | 2018-11-14 13:51:00 +0100 | [diff] [blame] | 99 | |
AKASHI Takahiro | 30962bf | 2020-06-16 14:26:48 +0900 | [diff] [blame] | 100 | int rsa_verify_with_pkey(struct image_sign_info *info, |
| 101 | const void *hash, uint8_t *sig, uint sig_len); |
| 102 | |
Philippe Reynes | 1246835 | 2018-11-14 13:51:00 +0100 | [diff] [blame] | 103 | int padding_pkcs_15_verify(struct image_sign_info *info, |
SESA644425 | 1304b46 | 2022-03-09 01:27:15 -0800 | [diff] [blame] | 104 | const uint8_t *msg, int msg_len, |
Philippe Reynes | 1246835 | 2018-11-14 13:51:00 +0100 | [diff] [blame] | 105 | const uint8_t *hash, int hash_len); |
Philippe Reynes | 47d73f0 | 2018-11-14 13:51:01 +0100 | [diff] [blame] | 106 | |
Philippe Reynes | 47d73f0 | 2018-11-14 13:51:01 +0100 | [diff] [blame] | 107 | int padding_pss_verify(struct image_sign_info *info, |
SESA644425 | 1304b46 | 2022-03-09 01:27:15 -0800 | [diff] [blame] | 108 | const uint8_t *msg, int msg_len, |
Philippe Reynes | 47d73f0 | 2018-11-14 13:51:01 +0100 | [diff] [blame] | 109 | const uint8_t *hash, int hash_len); |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 110 | |
Philippe Reynes | 1246835 | 2018-11-14 13:51:00 +0100 | [diff] [blame] | 111 | #define RSA_DEFAULT_PADDING_NAME "pkcs-1.5" |
| 112 | |
Heiko Schocher | 4b81756 | 2014-03-03 12:19:27 +0100 | [diff] [blame] | 113 | #define RSA2048_BYTES (2048 / 8) |
Jamin Lin | 5975ad7 | 2022-01-19 16:23:21 +0800 | [diff] [blame] | 114 | #define RSA3072_BYTES (3072 / 8) |
Heiko Schocher | 4b81756 | 2014-03-03 12:19:27 +0100 | [diff] [blame] | 115 | #define RSA4096_BYTES (4096 / 8) |
| 116 | |
| 117 | /* This is the minimum/maximum key size we support, in bits */ |
| 118 | #define RSA_MIN_KEY_BITS 2048 |
| 119 | #define RSA_MAX_KEY_BITS 4096 |
| 120 | |
| 121 | /* This is the maximum signature length that we support, in bits */ |
| 122 | #define RSA_MAX_SIG_BITS 4096 |
| 123 | |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 124 | #endif |