blob: fd08a617fbb129034375415365b67d573ef9c6a3 [file] [log] [blame]
Simon Glass35191a32013-06-13 15:10:02 -07001/*
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 Denkd79de1d2013-07-08 09:37:19 +02009 * SPDX-License-Identifier: GPL-2.0+
Simon Glass35191a32013-06-13 15:10:02 -070010 */
11
12#ifndef _RSA_H
13#define _RSA_H
14
15#include <errno.h>
16#include <image.h>
17
Heiko Schocheredaf9b12014-03-03 12:19:26 +010018/**
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
25struct 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 */
Michael van der Westhuizen89f4ed12014-07-02 10:17:26 +020030 uint64_t exponent; /* public exponent */
Heiko Schocheredaf9b12014-03-03 12:19:26 +010031};
32
Simon Glass35191a32013-06-13 15:10:02 -070033#if IMAGE_ENABLE_SIGN
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 */
51int 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 Glassaf2f9d52014-06-02 22:04:51 -060064 * @return: 0, on success, -ENOSPC if the keydest FDT blob ran out of space,
65 other -ve value on error
Simon Glass35191a32013-06-13 15:10:02 -070066*/
67int rsa_add_verify_data(struct image_sign_info *info, void *keydest);
68#else
69static inline int rsa_sign(struct image_sign_info *info,
70 const struct image_region region[], int region_count,
71 uint8_t **sigp, uint *sig_len)
72{
73 return -ENXIO;
74}
75
76static inline int rsa_add_verify_data(struct image_sign_info *info,
77 void *keydest)
78{
79 return -ENXIO;
80}
81#endif
82
83#if IMAGE_ENABLE_VERIFY
84/**
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
94 * @return 0 if verified, -ve on error
95 */
96int rsa_verify(struct image_sign_info *info,
97 const struct image_region region[], int region_count,
98 uint8_t *sig, uint sig_len);
99#else
100static inline int rsa_verify(struct image_sign_info *info,
101 const struct image_region region[], int region_count,
102 uint8_t *sig, uint sig_len)
103{
104 return -ENXIO;
105}
106#endif
107
Heiko Schocher4b817562014-03-03 12:19:27 +0100108#define RSA2048_BYTES (2048 / 8)
109#define RSA4096_BYTES (4096 / 8)
110
111/* This is the minimum/maximum key size we support, in bits */
112#define RSA_MIN_KEY_BITS 2048
113#define RSA_MAX_KEY_BITS 4096
114
115/* This is the maximum signature length that we support, in bits */
116#define RSA_MAX_SIG_BITS 4096
117
Simon Glass35191a32013-06-13 15:10:02 -0700118#endif