2
0

akcipher.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * QEMU Crypto akcipher algorithms
  3. *
  4. * Copyright (c) 2022 Bytedance
  5. * Author: zhenwei pi <pizhenwei@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. #include "qemu/osdep.h"
  22. #include "crypto/akcipher.h"
  23. #include "akcipherpriv.h"
  24. #include "der.h"
  25. #include "rsakey.h"
  26. #if defined(CONFIG_GCRYPT)
  27. #include "akcipher-gcrypt.c.inc"
  28. #elif defined(CONFIG_NETTLE) && defined(CONFIG_HOGWEED)
  29. #include "akcipher-nettle.c.inc"
  30. #else
  31. QCryptoAkCipher *qcrypto_akcipher_new(const QCryptoAkCipherOptions *opts,
  32. QCryptoAkCipherKeyType type,
  33. const uint8_t *key, size_t keylen,
  34. Error **errp)
  35. {
  36. QCryptoAkCipher *akcipher = NULL;
  37. return akcipher;
  38. }
  39. bool qcrypto_akcipher_supports(QCryptoAkCipherOptions *opts)
  40. {
  41. return false;
  42. }
  43. #endif
  44. int qcrypto_akcipher_encrypt(QCryptoAkCipher *akcipher,
  45. const void *in, size_t in_len,
  46. void *out, size_t out_len, Error **errp)
  47. {
  48. const QCryptoAkCipherDriver *drv = akcipher->driver;
  49. return drv->encrypt(akcipher, in, in_len, out, out_len, errp);
  50. }
  51. int qcrypto_akcipher_decrypt(QCryptoAkCipher *akcipher,
  52. const void *in, size_t in_len,
  53. void *out, size_t out_len, Error **errp)
  54. {
  55. const QCryptoAkCipherDriver *drv = akcipher->driver;
  56. return drv->decrypt(akcipher, in, in_len, out, out_len, errp);
  57. }
  58. int qcrypto_akcipher_sign(QCryptoAkCipher *akcipher,
  59. const void *in, size_t in_len,
  60. void *out, size_t out_len, Error **errp)
  61. {
  62. const QCryptoAkCipherDriver *drv = akcipher->driver;
  63. return drv->sign(akcipher, in, in_len, out, out_len, errp);
  64. }
  65. int qcrypto_akcipher_verify(QCryptoAkCipher *akcipher,
  66. const void *in, size_t in_len,
  67. const void *in2, size_t in2_len, Error **errp)
  68. {
  69. const QCryptoAkCipherDriver *drv = akcipher->driver;
  70. return drv->verify(akcipher, in, in_len, in2, in2_len, errp);
  71. }
  72. int qcrypto_akcipher_max_plaintext_len(QCryptoAkCipher *akcipher)
  73. {
  74. return akcipher->max_plaintext_len;
  75. }
  76. int qcrypto_akcipher_max_ciphertext_len(QCryptoAkCipher *akcipher)
  77. {
  78. return akcipher->max_ciphertext_len;
  79. }
  80. int qcrypto_akcipher_max_signature_len(QCryptoAkCipher *akcipher)
  81. {
  82. return akcipher->max_signature_len;
  83. }
  84. int qcrypto_akcipher_max_dgst_len(QCryptoAkCipher *akcipher)
  85. {
  86. return akcipher->max_dgst_len;
  87. }
  88. void qcrypto_akcipher_free(QCryptoAkCipher *akcipher)
  89. {
  90. const QCryptoAkCipherDriver *drv = akcipher->driver;
  91. drv->free(akcipher);
  92. }
  93. int qcrypto_akcipher_export_p8info(const QCryptoAkCipherOptions *opts,
  94. uint8_t *key, size_t keylen,
  95. uint8_t **dst, size_t *dst_len,
  96. Error **errp)
  97. {
  98. switch (opts->alg) {
  99. case QCRYPTO_AKCIPHER_ALG_RSA:
  100. qcrypto_akcipher_rsakey_export_p8info(key, keylen, dst, dst_len);
  101. return 0;
  102. default:
  103. error_setg(errp, "Unsupported algorithm: %u", opts->alg);
  104. return -1;
  105. }
  106. }