1 /* 2 * Copyright 2014 Freescale Semiconductor, Inc. 3 * 4 * SPDX-License-Identifier: GPL-2.0+ 5 */ 6 7 #ifndef _RSA_MOD_EXP_H 8 #define _RSA_MOD_EXP_H 9 10 #include <errno.h> 11 #include <image.h> 12 13 /** 14 * struct key_prop - holder for a public key properties 15 * 16 * The struct has pointers to modulus (Typically called N), 17 * The inverse, R^2, exponent. These can be typecasted and 18 * used as byte arrays or converted to the required format 19 * as per requirement of RSA implementation. 20 */ 21 struct key_prop { 22 const void *rr; /* R^2 can be treated as byte array */ 23 const void *modulus; /* modulus as byte array */ 24 const void *public_exponent; /* public exponent as byte array */ 25 const void *public_exponent_BN; /* public exponent as byte array */ 26 #ifdef CONFIG_ROCKCHIP_CRYPTO_V1 27 const void *factor_c; /* rockchip crypto v1 accelerate factor */ 28 #else 29 const void *factor_np; /* rockchip crypto v2 accelerate factor */ 30 #endif 31 uint32_t n0inv; /* -1 / modulus[0] mod 2^32 */ 32 int num_bits; /* Key length in bits */ 33 uint32_t exp_len; /* Exponent length in number of uint8_t */ 34 }; 35 36 /** 37 * rsa_mod_exp_sw() - Perform RSA Modular Exponentiation in sw 38 * 39 * Operation: out[] = sig ^ exponent % modulus 40 * 41 * @sig: RSA PKCS1.5 signature 42 * @sig_len: Length of signature in number of bytes 43 * @node: Node with RSA key elements like modulus, exponent, R^2, n0inv 44 * @out: Result in form of byte array of len equal to sig_len 45 */ 46 int rsa_mod_exp_sw(const uint8_t *sig, uint32_t sig_len, 47 struct key_prop *node, uint8_t *out); 48 49 int rsa_mod_exp(struct udevice *dev, const uint8_t *sig, uint32_t sig_len, 50 struct key_prop *node, uint8_t *out); 51 52 /** 53 * struct struct mod_exp_ops - Driver model for RSA Modular Exponentiation 54 * operations 55 * 56 * The uclass interface is implemented by all crypto devices which use 57 * driver model. 58 */ 59 struct mod_exp_ops { 60 /** 61 * Perform Modular Exponentiation 62 * 63 * Operation: out[] = sig ^ exponent % modulus 64 * 65 * @dev: RSA Device 66 * @sig: RSA PKCS1.5 signature 67 * @sig_len: Length of signature in number of bytes 68 * @node: Node with RSA key elements like modulus, exponent, 69 * R^2, n0inv 70 * @out: Result in form of byte array of len equal to sig_len 71 * 72 * This function computes exponentiation over the signature. 73 * Returns: 0 if exponentiation is successful, or a negative value 74 * if it wasn't. 75 */ 76 int (*mod_exp)(struct udevice *dev, const uint8_t *sig, 77 uint32_t sig_len, struct key_prop *node, 78 uint8_t *outp); 79 }; 80 81 #endif 82