Simon Glass | 19c402a | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2013, Google Inc. |
| 3 | * |
| 4 | * (C) Copyright 2008 Semihalf |
| 5 | * |
| 6 | * (C) Copyright 2000-2006 |
| 7 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 8 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 9 | * SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 19c402a | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #ifndef _RSA_H |
| 13 | #define _RSA_H |
| 14 | |
| 15 | #include <errno.h> |
| 16 | #include <image.h> |
| 17 | |
Heiko Schocher | 646257d | 2014-03-03 12:19:26 +0100 | [diff] [blame] | 18 | /** |
| 19 | * struct rsa_public_key - holder for a public key |
| 20 | * |
| 21 | * An RSA public key consists of a modulus (typically called N), the inverse |
| 22 | * and R^2, where R is 2^(# key bits). |
| 23 | */ |
| 24 | |
| 25 | struct rsa_public_key { |
| 26 | uint len; /* len of modulus[] in number of uint32_t */ |
| 27 | uint32_t n0inv; /* -1 / modulus[0] mod 2^32 */ |
| 28 | uint32_t *modulus; /* modulus as little endian array */ |
| 29 | uint32_t *rr; /* R^2 as little endian array */ |
| 30 | }; |
| 31 | |
Simon Glass | 19c402a | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 32 | #if IMAGE_ENABLE_SIGN |
| 33 | /** |
| 34 | * sign() - calculate and return signature for given input data |
| 35 | * |
| 36 | * @info: Specifies key and FIT information |
| 37 | * @data: Pointer to the input data |
| 38 | * @data_len: Data length |
| 39 | * @sigp: Set to an allocated buffer holding the signature |
| 40 | * @sig_len: Set to length of the calculated hash |
| 41 | * |
| 42 | * This computes input data signature according to selected algorithm. |
| 43 | * Resulting signature value is placed in an allocated buffer, the |
| 44 | * pointer is returned as *sigp. The length of the calculated |
| 45 | * signature is returned via the sig_len pointer argument. The caller |
| 46 | * should free *sigp. |
| 47 | * |
| 48 | * @return: 0, on success, -ve on error |
| 49 | */ |
| 50 | int rsa_sign(struct image_sign_info *info, |
| 51 | const struct image_region region[], |
| 52 | int region_count, uint8_t **sigp, uint *sig_len); |
| 53 | |
| 54 | /** |
| 55 | * add_verify_data() - Add verification information to FDT |
| 56 | * |
| 57 | * Add public key information to the FDT node, suitable for |
| 58 | * verification at run-time. The information added depends on the |
| 59 | * algorithm being used. |
| 60 | * |
| 61 | * @info: Specifies key and FIT information |
| 62 | * @keydest: Destination FDT blob for public key data |
Simon Glass | 4f427a4 | 2014-06-02 22:04:51 -0600 | [diff] [blame^] | 63 | * @return: 0, on success, -ENOSPC if the keydest FDT blob ran out of space, |
| 64 | other -ve value on error |
Simon Glass | 19c402a | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 65 | */ |
| 66 | int rsa_add_verify_data(struct image_sign_info *info, void *keydest); |
| 67 | #else |
| 68 | static inline int rsa_sign(struct image_sign_info *info, |
| 69 | const struct image_region region[], int region_count, |
| 70 | uint8_t **sigp, uint *sig_len) |
| 71 | { |
| 72 | return -ENXIO; |
| 73 | } |
| 74 | |
| 75 | static inline int rsa_add_verify_data(struct image_sign_info *info, |
| 76 | void *keydest) |
| 77 | { |
| 78 | return -ENXIO; |
| 79 | } |
| 80 | #endif |
| 81 | |
| 82 | #if IMAGE_ENABLE_VERIFY |
| 83 | /** |
| 84 | * rsa_verify() - Verify a signature against some data |
| 85 | * |
| 86 | * Verify a RSA PKCS1.5 signature against an expected hash. |
| 87 | * |
| 88 | * @info: Specifies key and FIT information |
| 89 | * @data: Pointer to the input data |
| 90 | * @data_len: Data length |
| 91 | * @sig: Signature |
| 92 | * @sig_len: Number of bytes in signature |
| 93 | * @return 0 if verified, -ve on error |
| 94 | */ |
| 95 | int rsa_verify(struct image_sign_info *info, |
| 96 | const struct image_region region[], int region_count, |
| 97 | uint8_t *sig, uint sig_len); |
| 98 | #else |
| 99 | static inline int rsa_verify(struct image_sign_info *info, |
| 100 | const struct image_region region[], int region_count, |
| 101 | uint8_t *sig, uint sig_len) |
| 102 | { |
| 103 | return -ENXIO; |
| 104 | } |
| 105 | #endif |
| 106 | |
Heiko Schocher | db1b5f3 | 2014-03-03 12:19:27 +0100 | [diff] [blame] | 107 | #define RSA2048_BYTES (2048 / 8) |
| 108 | #define RSA4096_BYTES (4096 / 8) |
| 109 | |
| 110 | /* This is the minimum/maximum key size we support, in bits */ |
| 111 | #define RSA_MIN_KEY_BITS 2048 |
| 112 | #define RSA_MAX_KEY_BITS 4096 |
| 113 | |
| 114 | /* This is the maximum signature length that we support, in bits */ |
| 115 | #define RSA_MAX_SIG_BITS 4096 |
| 116 | |
Simon Glass | 19c402a | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 117 | #endif |