akcipher.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * QEMU Crypto asymmetric 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. #ifndef QCRYPTO_AKCIPHER_H
  22. #define QCRYPTO_AKCIPHER_H
  23. #include "qapi/qapi-types-crypto.h"
  24. typedef struct QCryptoAkCipher QCryptoAkCipher;
  25. /**
  26. * qcrypto_akcipher_supports:
  27. * @opts: the asymmetric key algorithm and related options
  28. *
  29. * Determine if asymmetric key cipher described with @opts is
  30. * supported by the current configured build
  31. *
  32. * Returns: true if it is supported, false otherwise.
  33. */
  34. bool qcrypto_akcipher_supports(QCryptoAkCipherOptions *opts);
  35. /**
  36. * qcrypto_akcipher_new:
  37. * @opts: specify the algorithm and the related arguments
  38. * @type: private or public key type
  39. * @key: buffer to store the key
  40. * @key_len: the length of key buffer
  41. * @errp: error pointer
  42. *
  43. * Create akcipher context
  44. *
  45. * Returns: On success, a new QCryptoAkCipher initialized with @opt
  46. * is created and returned, otherwise NULL is returned.
  47. */
  48. QCryptoAkCipher *qcrypto_akcipher_new(const QCryptoAkCipherOptions *opts,
  49. QCryptoAkCipherKeyType type,
  50. const uint8_t *key, size_t key_len,
  51. Error **errp);
  52. /**
  53. * qcrypto_akcipher_encrypt:
  54. * @akcipher: akcipher context
  55. * @in: plaintext pending to be encrypted
  56. * @in_len: length of plaintext, less or equal to the size reported
  57. * by a call to qcrypto_akcipher_max_plaintext_len()
  58. * @out: buffer to store the ciphertext
  59. * @out_len: length of ciphertext, less or equal to the size reported
  60. * by a call to qcrypto_akcipher_max_ciphertext_len()
  61. * @errp: error pointer
  62. *
  63. * Encrypt @in and write ciphertext into @out
  64. *
  65. * Returns: length of ciphertext if encrypt succeed,
  66. * otherwise -1 is returned
  67. */
  68. int qcrypto_akcipher_encrypt(QCryptoAkCipher *akcipher,
  69. const void *in, size_t in_len,
  70. void *out, size_t out_len, Error **errp);
  71. /**
  72. * qcrypto_akcipher_decrypt:
  73. * @akcipher: akcipher context
  74. * @in: ciphertext to be decrypted
  75. * @in_len: the length of ciphertext, less or equal to the size reported
  76. * by a call to qcrypto_akcipher_max_ciphertext_len()
  77. * @out: buffer to store the plaintext
  78. * @out_len: length of the plaintext buffer, less or equal to the size
  79. * reported by a call to qcrypto_akcipher_max_plaintext_len()
  80. * @errp: error pointer
  81. *
  82. * Decrypt @in and write plaintext into @out
  83. *
  84. * Returns: length of plaintext if decrypt succeed,
  85. * otherwise -1 is returned
  86. */
  87. int qcrypto_akcipher_decrypt(QCryptoAkCipher *akcipher,
  88. const void *in, size_t in_len,
  89. void *out, size_t out_len, Error **errp);
  90. /**
  91. * qcrypto_akcipher_sign:
  92. * @akcipher: akcipher context
  93. * @in: data to be signed
  94. * @in_len: the length of data, less or equal to the size reported
  95. * by a call to qcrypto_akcipher_max_dgst_len()
  96. * @out: buffer to store the signature
  97. * @out_len: length of the signature buffer, less or equal to the size
  98. * by a call to qcrypto_akcipher_max_signature_len()
  99. * @errp: error pointer
  100. *
  101. * Generate signature for @in, write into @out
  102. *
  103. * Returns: length of signature if succeed,
  104. * otherwise -1 is returned
  105. */
  106. int qcrypto_akcipher_sign(QCryptoAkCipher *akcipher,
  107. const void *in, size_t in_len,
  108. void *out, size_t out_len, Error **errp);
  109. /**
  110. * qcrypto_akcipher_verify:
  111. * @akcipher: akcipher context
  112. * @in: pointer to the signature
  113. * @in_len: length of signature, ess or equal to the size reported
  114. * by a call to qcrypto_akcipher_max_signature_len()
  115. * @in2: pointer to original data
  116. * @in2_len: the length of original data, less or equal to the size
  117. * by a call to qcrypto_akcipher_max_dgst_len()
  118. * @errp: error pointer
  119. *
  120. * Verify @in and @in2 match or not
  121. *
  122. * Returns: 0 for succeed,
  123. * otherwise -1 is returned
  124. */
  125. int qcrypto_akcipher_verify(QCryptoAkCipher *akcipher,
  126. const void *in, size_t in_len,
  127. const void *in2, size_t in2_len, Error **errp);
  128. int qcrypto_akcipher_max_plaintext_len(QCryptoAkCipher *akcipher);
  129. int qcrypto_akcipher_max_ciphertext_len(QCryptoAkCipher *akcipher);
  130. int qcrypto_akcipher_max_signature_len(QCryptoAkCipher *akcipher);
  131. int qcrypto_akcipher_max_dgst_len(QCryptoAkCipher *akcipher);
  132. /**
  133. * qcrypto_akcipher_free:
  134. * @akcipher: akcipher context
  135. *
  136. * Free the akcipher context
  137. *
  138. */
  139. void qcrypto_akcipher_free(QCryptoAkCipher *akcipher);
  140. /**
  141. * qcrypto_akcipher_export_p8info:
  142. * @opts: the options of the akcipher to be exported.
  143. * @key: the original key of the akcipher to be exported.
  144. * @keylen: length of the 'key'
  145. * @dst: output parameter, if export succeed, *dst is set to the
  146. * PKCS#8 encoded private key, caller MUST free this key with
  147. * g_free after use.
  148. * @dst_len: output parameter, indicates the length of PKCS#8 encoded
  149. * key.
  150. *
  151. * Export the akcipher into DER encoded pkcs#8 private key info, expects
  152. * |key| stores a valid asymmetric PRIVATE key.
  153. *
  154. * Returns: 0 for succeed, otherwise -1 is returned.
  155. */
  156. int qcrypto_akcipher_export_p8info(const QCryptoAkCipherOptions *opts,
  157. uint8_t *key, size_t keylen,
  158. uint8_t **dst, size_t *dst_len,
  159. Error **errp);
  160. G_DEFINE_AUTOPTR_CLEANUP_FUNC(QCryptoAkCipher, qcrypto_akcipher_free)
  161. #endif /* QCRYPTO_AKCIPHER_H */