blob: 8a428c4b6a1ac88fcbe5ef5fce62b0e356a3c11c [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
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 */
20struct 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/**
30 * rsa_mod_exp_sw() - Perform RSA Modular Exponentiation in sw
31 *
32 * Operation: out[] = sig ^ exponent % modulus
33 *
34 * @sig: RSA PKCS1.5 signature
35 * @sig_len: Length of signature in number of bytes
36 * @node: Node with RSA key elements like modulus, exponent, R^2, n0inv
Ruchika Gupta98ebb122015-01-23 16:01:52 +053037 * @out: Result in form of byte array of len equal to sig_len
Ruchika Guptab92ebab2015-01-23 16:01:50 +053038 */
39int rsa_mod_exp_sw(const uint8_t *sig, uint32_t sig_len,
40 struct key_prop *node, uint8_t *out);
41
Ruchika Gupta98ebb122015-01-23 16:01:52 +053042int rsa_mod_exp(struct udevice *dev, const uint8_t *sig, uint32_t sig_len,
43 struct key_prop *node, uint8_t *out);
44
Siva Durga Prasad Paladugue4603522018-06-26 15:02:19 +053045#if defined(CONFIG_CMD_ZYNQ_RSA)
46int zynq_pow_mod(u32 *keyptr, u32 *inout);
47#endif
48
Ruchika Gupta98ebb122015-01-23 16:01:52 +053049/**
50 * struct struct mod_exp_ops - Driver model for RSA Modular Exponentiation
51 * operations
52 *
53 * The uclass interface is implemented by all crypto devices which use
54 * driver model.
55 */
56struct mod_exp_ops {
57 /**
58 * Perform Modular Exponentiation
59 *
60 * Operation: out[] = sig ^ exponent % modulus
61 *
62 * @dev: RSA Device
63 * @sig: RSA PKCS1.5 signature
64 * @sig_len: Length of signature in number of bytes
65 * @node: Node with RSA key elements like modulus, exponent,
66 * R^2, n0inv
67 * @out: Result in form of byte array of len equal to sig_len
68 *
69 * This function computes exponentiation over the signature.
70 * Returns: 0 if exponentiation is successful, or a negative value
71 * if it wasn't.
72 */
73 int (*mod_exp)(struct udevice *dev, const uint8_t *sig,
74 uint32_t sig_len, struct key_prop *node,
75 uint8_t *outp);
76};
77
Ruchika Guptab92ebab2015-01-23 16:01:50 +053078#endif