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