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 uint32_t burn_key; /* The flag to burn key's hash */ 25 const void *public_exponent; /* public exponent as byte array */ 26 const void *public_exponent_BN; /* public exponent as byte array */ 27 #ifdef CONFIG_ROCKCHIP_CRYPTO_V1 28 const void *factor_c; /* rockchip crypto v1 accelerate factor */ 29 #else 30 const void *factor_np; /* rockchip crypto v2 accelerate factor */ 31 #endif 32 const void *hash; /* the key hash */ 33 uint32_t n0inv; /* -1 / modulus[0] mod 2^32 */ 34 int num_bits; /* Key length in bits */ 35 uint32_t exp_len; /* Exponent length in number of uint8_t */ 36 }; 37 38 /** 39 * rsa_mod_exp_sw() - Perform RSA Modular Exponentiation in sw 40 * 41 * Operation: out[] = sig ^ exponent % modulus 42 * 43 * @sig: RSA PKCS1.5 signature 44 * @sig_len: Length of signature in number of bytes 45 * @node: Node with RSA key elements like modulus, exponent, R^2, n0inv 46 * @out: Result in form of byte array of len equal to sig_len 47 */ 48 int rsa_mod_exp_sw(const uint8_t *sig, uint32_t sig_len, 49 struct key_prop *node, uint8_t *out); 50 51 int rsa_mod_exp(struct udevice *dev, const uint8_t *sig, uint32_t sig_len, 52 struct key_prop *node, uint8_t *out); 53 54 /** 55 * struct struct mod_exp_ops - Driver model for RSA Modular Exponentiation 56 * operations 57 * 58 * The uclass interface is implemented by all crypto devices which use 59 * driver model. 60 */ 61 struct mod_exp_ops { 62 /** 63 * Perform Modular Exponentiation 64 * 65 * Operation: out[] = sig ^ exponent % modulus 66 * 67 * @dev: RSA Device 68 * @sig: RSA PKCS1.5 signature 69 * @sig_len: Length of signature in number of bytes 70 * @node: Node with RSA key elements like modulus, exponent, 71 * R^2, n0inv 72 * @out: Result in form of byte array of len equal to sig_len 73 * 74 * This function computes exponentiation over the signature. 75 * Returns: 0 if exponentiation is successful, or a negative value 76 * if it wasn't. 77 */ 78 int (*mod_exp)(struct udevice *dev, const uint8_t *sig, 79 uint32_t sig_len, struct key_prop *node, 80 uint8_t *outp); 81 }; 82 83 #endif 84