rsa-mod-exp.h 2.2 KB

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