Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0+ */ |
Ruchika Gupta | b92ebab | 2015-01-23 16:01:50 +0530 | [diff] [blame] | 2 | /* |
Ruchika Gupta | 9c078c3 | 2015-07-27 09:07:39 +0530 | [diff] [blame] | 3 | * Copyright 2014 Freescale Semiconductor, Inc. |
Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 4 | */ |
Ruchika Gupta | b92ebab | 2015-01-23 16:01:50 +0530 | [diff] [blame] | 5 | |
| 6 | #ifndef _RSA_MOD_EXP_H |
| 7 | #define _RSA_MOD_EXP_H |
| 8 | |
| 9 | #include <errno.h> |
| 10 | #include <image.h> |
| 11 | |
| 12 | /** |
| 13 | * struct key_prop - holder for a public key properties |
| 14 | * |
| 15 | * The struct has pointers to modulus (Typically called N), |
| 16 | * The inverse, R^2, exponent. These can be typecasted and |
| 17 | * used as byte arrays or converted to the required format |
| 18 | * as per requirement of RSA implementation. |
| 19 | */ |
| 20 | struct key_prop { |
| 21 | const void *rr; /* R^2 can be treated as byte array */ |
| 22 | const void *modulus; /* modulus as byte array */ |
| 23 | const void *public_exponent; /* public exponent as byte array */ |
| 24 | uint32_t n0inv; /* -1 / modulus[0] mod 2^32 */ |
| 25 | int num_bits; /* Key length in bits */ |
| 26 | uint32_t exp_len; /* Exponent length in number of uint8_t */ |
| 27 | }; |
| 28 | |
| 29 | /** |
AKASHI Takahiro | d4aece1 | 2020-02-21 15:12:58 +0900 | [diff] [blame] | 30 | * rsa_gen_key_prop() - Generate key properties of RSA public key |
| 31 | * @key: Specifies key data in DER format |
| 32 | * @keylen: Length of @key |
| 33 | * @prop: Generated key property |
| 34 | * |
| 35 | * This function takes a blob of encoded RSA public key data in DER |
| 36 | * format, parse it and generate all the relevant properties |
| 37 | * in key_prop structure. |
| 38 | * Return a pointer to struct key_prop in @prop on success. |
| 39 | * |
| 40 | * Return: 0 on success, negative on error |
| 41 | */ |
| 42 | int rsa_gen_key_prop(const void *key, uint32_t keylen, struct key_prop **proc); |
| 43 | |
| 44 | /** |
| 45 | * rsa_free_key_prop() - Free key properties |
| 46 | * @prop: Pointer to struct key_prop |
| 47 | * |
| 48 | * This function frees all the memories allocated by rsa_gen_key_prop(). |
| 49 | */ |
| 50 | void rsa_free_key_prop(struct key_prop *prop); |
| 51 | |
| 52 | /** |
Ruchika Gupta | b92ebab | 2015-01-23 16:01:50 +0530 | [diff] [blame] | 53 | * rsa_mod_exp_sw() - Perform RSA Modular Exponentiation in sw |
| 54 | * |
| 55 | * Operation: out[] = sig ^ exponent % modulus |
| 56 | * |
| 57 | * @sig: RSA PKCS1.5 signature |
| 58 | * @sig_len: Length of signature in number of bytes |
| 59 | * @node: Node with RSA key elements like modulus, exponent, R^2, n0inv |
Ruchika Gupta | 98ebb12 | 2015-01-23 16:01:52 +0530 | [diff] [blame] | 60 | * @out: Result in form of byte array of len equal to sig_len |
Ruchika Gupta | b92ebab | 2015-01-23 16:01:50 +0530 | [diff] [blame] | 61 | */ |
| 62 | int rsa_mod_exp_sw(const uint8_t *sig, uint32_t sig_len, |
| 63 | struct key_prop *node, uint8_t *out); |
| 64 | |
Ruchika Gupta | 98ebb12 | 2015-01-23 16:01:52 +0530 | [diff] [blame] | 65 | int rsa_mod_exp(struct udevice *dev, const uint8_t *sig, uint32_t sig_len, |
| 66 | struct key_prop *node, uint8_t *out); |
| 67 | |
Siva Durga Prasad Paladugu | e460352 | 2018-06-26 15:02:19 +0530 | [diff] [blame] | 68 | #if defined(CONFIG_CMD_ZYNQ_RSA) |
Michal Simek | 38e828e | 2020-10-22 10:59:08 +0200 | [diff] [blame] | 69 | int zynq_pow_mod(uint32_t *keyptr, uint32_t *inout); |
Siva Durga Prasad Paladugu | e460352 | 2018-06-26 15:02:19 +0530 | [diff] [blame] | 70 | #endif |
| 71 | |
Ruchika Gupta | 98ebb12 | 2015-01-23 16:01:52 +0530 | [diff] [blame] | 72 | /** |
| 73 | * struct struct mod_exp_ops - Driver model for RSA Modular Exponentiation |
| 74 | * operations |
| 75 | * |
| 76 | * The uclass interface is implemented by all crypto devices which use |
| 77 | * driver model. |
| 78 | */ |
| 79 | struct mod_exp_ops { |
| 80 | /** |
| 81 | * Perform Modular Exponentiation |
| 82 | * |
| 83 | * Operation: out[] = sig ^ exponent % modulus |
| 84 | * |
| 85 | * @dev: RSA Device |
| 86 | * @sig: RSA PKCS1.5 signature |
| 87 | * @sig_len: Length of signature in number of bytes |
| 88 | * @node: Node with RSA key elements like modulus, exponent, |
| 89 | * R^2, n0inv |
| 90 | * @out: Result in form of byte array of len equal to sig_len |
| 91 | * |
| 92 | * This function computes exponentiation over the signature. |
| 93 | * Returns: 0 if exponentiation is successful, or a negative value |
| 94 | * if it wasn't. |
| 95 | */ |
| 96 | int (*mod_exp)(struct udevice *dev, const uint8_t *sig, |
| 97 | uint32_t sig_len, struct key_prop *node, |
| 98 | uint8_t *outp); |
| 99 | }; |
| 100 | |
Ruchika Gupta | b92ebab | 2015-01-23 16:01:50 +0530 | [diff] [blame] | 101 | #endif |