rsa-mod-exp.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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_gen_key_prop() - Generate key properties of RSA public key
  27. * @key: Specifies key data in DER format
  28. * @keylen: Length of @key
  29. * @prop: Generated key property
  30. *
  31. * This function takes a blob of encoded RSA public key data in DER
  32. * format, parse it and generate all the relevant properties
  33. * in key_prop structure.
  34. * Return a pointer to struct key_prop in @prop on success.
  35. *
  36. * Return: 0 on success, negative on error
  37. */
  38. int rsa_gen_key_prop(const void *key, uint32_t keylen, struct key_prop **proc);
  39. /**
  40. * rsa_free_key_prop() - Free key properties
  41. * @prop: Pointer to struct key_prop
  42. *
  43. * This function frees all the memories allocated by rsa_gen_key_prop().
  44. */
  45. void rsa_free_key_prop(struct key_prop *prop);
  46. /**
  47. * rsa_mod_exp_sw() - Perform RSA Modular Exponentiation in sw
  48. *
  49. * Operation: out[] = sig ^ exponent % modulus
  50. *
  51. * @sig: RSA PKCS1.5 signature
  52. * @sig_len: Length of signature in number of bytes
  53. * @node: Node with RSA key elements like modulus, exponent, R^2, n0inv
  54. * @out: Result in form of byte array of len equal to sig_len
  55. */
  56. int rsa_mod_exp_sw(const uint8_t *sig, uint32_t sig_len,
  57. struct key_prop *node, uint8_t *out);
  58. int rsa_mod_exp(struct udevice *dev, const uint8_t *sig, uint32_t sig_len,
  59. struct key_prop *node, uint8_t *out);
  60. #if defined(CONFIG_CMD_ZYNQ_RSA)
  61. int zynq_pow_mod(u32 *keyptr, u32 *inout);
  62. #endif
  63. /**
  64. * struct struct mod_exp_ops - Driver model for RSA Modular Exponentiation
  65. * operations
  66. *
  67. * The uclass interface is implemented by all crypto devices which use
  68. * driver model.
  69. */
  70. struct mod_exp_ops {
  71. /**
  72. * Perform Modular Exponentiation
  73. *
  74. * Operation: out[] = sig ^ exponent % modulus
  75. *
  76. * @dev: RSA Device
  77. * @sig: RSA PKCS1.5 signature
  78. * @sig_len: Length of signature in number of bytes
  79. * @node: Node with RSA key elements like modulus, exponent,
  80. * R^2, n0inv
  81. * @out: Result in form of byte array of len equal to sig_len
  82. *
  83. * This function computes exponentiation over the signature.
  84. * Returns: 0 if exponentiation is successful, or a negative value
  85. * if it wasn't.
  86. */
  87. int (*mod_exp)(struct udevice *dev, const uint8_t *sig,
  88. uint32_t sig_len, struct key_prop *node,
  89. uint8_t *outp);
  90. };
  91. #endif