rsa-mod-exp.h 2.3 KB

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