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