rsakey.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * QEMU Crypto RSA key parser
  3. *
  4. * Copyright (c) 2022 Bytedance
  5. * Author: lei he <helei.sig11@bytedance.com>
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. #ifndef QCRYPTO_RSAKEY_H
  22. #define QCRYPTO_RSAKEY_H
  23. #include "qemu/host-utils.h"
  24. #include "crypto/akcipher.h"
  25. typedef struct QCryptoAkCipherRSAKey QCryptoAkCipherRSAKey;
  26. typedef struct QCryptoAkCipherMPI QCryptoAkCipherMPI;
  27. /**
  28. * Multiple precious integer, encoded as two' complement,
  29. * copied directly from DER encoded ASN.1 structures.
  30. */
  31. struct QCryptoAkCipherMPI {
  32. uint8_t *data;
  33. size_t len;
  34. };
  35. /* See rfc2437: https://datatracker.ietf.org/doc/html/rfc2437 */
  36. struct QCryptoAkCipherRSAKey {
  37. /* The modulus */
  38. QCryptoAkCipherMPI n;
  39. /* The public exponent */
  40. QCryptoAkCipherMPI e;
  41. /* The private exponent */
  42. QCryptoAkCipherMPI d;
  43. /* The first factor */
  44. QCryptoAkCipherMPI p;
  45. /* The second factor */
  46. QCryptoAkCipherMPI q;
  47. /* The first factor's exponent */
  48. QCryptoAkCipherMPI dp;
  49. /* The second factor's exponent */
  50. QCryptoAkCipherMPI dq;
  51. /* The CRT coefficient */
  52. QCryptoAkCipherMPI u;
  53. };
  54. /**
  55. * Parse DER encoded ASN.1 RSA keys, expected ASN.1 schemas:
  56. * RsaPrivKey ::= SEQUENCE {
  57. * version INTEGER
  58. * n INTEGER
  59. * e INTEGER
  60. * d INTEGER
  61. * p INTEGER
  62. * q INTEGER
  63. * dp INTEGER
  64. * dq INTEGER
  65. * u INTEGER
  66. * otherPrimeInfos OtherPrimeInfos OPTIONAL
  67. * }
  68. *
  69. * RsaPubKey ::= SEQUENCE {
  70. * n INTEGER
  71. * e INTEGER
  72. * }
  73. *
  74. * Returns: On success QCryptoAkCipherRSAKey is returned, otherwise returns NULL
  75. */
  76. QCryptoAkCipherRSAKey *qcrypto_akcipher_rsakey_parse(
  77. QCryptoAkCipherKeyType type,
  78. const uint8_t *key, size_t keylen, Error **errp);
  79. /**
  80. * qcrypto_akcipher_rsakey_export_as_p8info:
  81. *
  82. * Export RSA private key to PKCS#8 private key info.
  83. */
  84. void qcrypto_akcipher_rsakey_export_p8info(const uint8_t *key,
  85. size_t keylen,
  86. uint8_t **dst,
  87. size_t *dlen);
  88. void qcrypto_akcipher_rsakey_free(QCryptoAkCipherRSAKey *key);
  89. G_DEFINE_AUTOPTR_CLEANUP_FUNC(QCryptoAkCipherRSAKey,
  90. qcrypto_akcipher_rsakey_free);
  91. #endif