2
0

cipher.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. * QEMU Crypto cipher algorithms
  3. *
  4. * Copyright (c) 2015 Red Hat, Inc.
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. #ifndef QCRYPTO_CIPHER_H
  21. #define QCRYPTO_CIPHER_H
  22. #include "qapi/qapi-types-crypto.h"
  23. typedef struct QCryptoCipher QCryptoCipher;
  24. typedef struct QCryptoCipherDriver QCryptoCipherDriver;
  25. /* See also "QCryptoCipherAlgo" and "QCryptoCipherMode"
  26. * enums defined in qapi/crypto.json */
  27. /**
  28. * QCryptoCipher:
  29. *
  30. * The QCryptoCipher object provides a way to perform encryption
  31. * and decryption of data, with a standard API, regardless of the
  32. * algorithm used. It further isolates the calling code from the
  33. * details of the specific underlying implementation, whether
  34. * built-in, libgcrypt or nettle.
  35. *
  36. * Each QCryptoCipher object is capable of performing both
  37. * encryption and decryption, and can operate in a number
  38. * or modes including ECB, CBC.
  39. *
  40. * <example>
  41. * <title>Encrypting data with AES-128 in CBC mode</title>
  42. * <programlisting>
  43. * QCryptoCipher *cipher;
  44. * uint8_t key = ....;
  45. * size_t keylen = 16;
  46. * uint8_t iv = ....;
  47. *
  48. * if (!qcrypto_cipher_supports(QCRYPTO_CIPHER_ALGO_AES_128)) {
  49. * error_report(errp, "Feature <blah> requires AES cipher support");
  50. * return -1;
  51. * }
  52. *
  53. * cipher = qcrypto_cipher_new(QCRYPTO_CIPHER_ALGO_AES_128,
  54. * QCRYPTO_CIPHER_MODE_CBC,
  55. * key, keylen,
  56. * errp);
  57. * if (!cipher) {
  58. * return -1;
  59. * }
  60. *
  61. * if (qcrypto_cipher_set_iv(cipher, iv, keylen, errp) < 0) {
  62. * return -1;
  63. * }
  64. *
  65. * if (qcrypto_cipher_encrypt(cipher, rawdata, encdata, datalen, errp) < 0) {
  66. * return -1;
  67. * }
  68. *
  69. * qcrypto_cipher_free(cipher);
  70. * </programlisting>
  71. * </example>
  72. *
  73. */
  74. struct QCryptoCipher {
  75. QCryptoCipherAlgo alg;
  76. QCryptoCipherMode mode;
  77. const QCryptoCipherDriver *driver;
  78. };
  79. /**
  80. * qcrypto_cipher_supports:
  81. * @alg: the cipher algorithm
  82. * @mode: the cipher mode
  83. *
  84. * Determine if @alg cipher algorithm in @mode is supported by the
  85. * current configured build
  86. *
  87. * Returns: true if the algorithm is supported, false otherwise
  88. */
  89. bool qcrypto_cipher_supports(QCryptoCipherAlgo alg,
  90. QCryptoCipherMode mode);
  91. /**
  92. * qcrypto_cipher_get_block_len:
  93. * @alg: the cipher algorithm
  94. *
  95. * Get the required data block size in bytes. When
  96. * encrypting data, it must be a multiple of the
  97. * block size.
  98. *
  99. * Returns: the block size in bytes
  100. */
  101. size_t qcrypto_cipher_get_block_len(QCryptoCipherAlgo alg);
  102. /**
  103. * qcrypto_cipher_get_key_len:
  104. * @alg: the cipher algorithm
  105. *
  106. * Get the required key size in bytes.
  107. *
  108. * Returns: the key size in bytes
  109. */
  110. size_t qcrypto_cipher_get_key_len(QCryptoCipherAlgo alg);
  111. /**
  112. * qcrypto_cipher_get_iv_len:
  113. * @alg: the cipher algorithm
  114. * @mode: the cipher mode
  115. *
  116. * Get the required initialization vector size
  117. * in bytes, if one is required.
  118. *
  119. * Returns: the IV size in bytes, or 0 if no IV is permitted
  120. */
  121. size_t qcrypto_cipher_get_iv_len(QCryptoCipherAlgo alg,
  122. QCryptoCipherMode mode);
  123. /**
  124. * qcrypto_cipher_new:
  125. * @alg: the cipher algorithm
  126. * @mode: the cipher usage mode
  127. * @key: the private key bytes
  128. * @nkey: the length of @key
  129. * @errp: pointer to a NULL-initialized error object
  130. *
  131. * Creates a new cipher object for encrypting/decrypting
  132. * data with the algorithm @alg in the usage mode @mode.
  133. *
  134. * The @key parameter provides the bytes representing
  135. * the encryption/decryption key to use. The @nkey parameter
  136. * specifies the length of @key in bytes. Each algorithm has
  137. * one or more valid key lengths, and it is an error to provide
  138. * a key of the incorrect length.
  139. *
  140. * The returned cipher object must be released with
  141. * qcrypto_cipher_free() when no longer required
  142. *
  143. * Returns: a new cipher object, or NULL on error
  144. */
  145. QCryptoCipher *qcrypto_cipher_new(QCryptoCipherAlgo alg,
  146. QCryptoCipherMode mode,
  147. const uint8_t *key, size_t nkey,
  148. Error **errp);
  149. /**
  150. * qcrypto_cipher_free:
  151. * @cipher: the cipher object
  152. *
  153. * Release the memory associated with @cipher that
  154. * was previously allocated by qcrypto_cipher_new()
  155. */
  156. void qcrypto_cipher_free(QCryptoCipher *cipher);
  157. G_DEFINE_AUTOPTR_CLEANUP_FUNC(QCryptoCipher, qcrypto_cipher_free)
  158. /**
  159. * qcrypto_cipher_encrypt:
  160. * @cipher: the cipher object
  161. * @in: buffer holding the plain text input data
  162. * @out: buffer to fill with the cipher text output data
  163. * @len: the length of @in and @out buffers
  164. * @errp: pointer to a NULL-initialized error object
  165. *
  166. * Encrypts the plain text stored in @in, filling
  167. * @out with the resulting ciphered text. Both the
  168. * @in and @out buffers must have the same size,
  169. * given by @len.
  170. *
  171. * Returns: 0 on success, or -1 on error
  172. */
  173. int qcrypto_cipher_encrypt(QCryptoCipher *cipher,
  174. const void *in,
  175. void *out,
  176. size_t len,
  177. Error **errp);
  178. /**
  179. * qcrypto_cipher_decrypt:
  180. * @cipher: the cipher object
  181. * @in: buffer holding the cipher text input data
  182. * @out: buffer to fill with the plain text output data
  183. * @len: the length of @in and @out buffers
  184. * @errp: pointer to a NULL-initialized error object
  185. *
  186. * Decrypts the cipher text stored in @in, filling
  187. * @out with the resulting plain text. Both the
  188. * @in and @out buffers must have the same size,
  189. * given by @len.
  190. *
  191. * Returns: 0 on success, or -1 on error
  192. */
  193. int qcrypto_cipher_decrypt(QCryptoCipher *cipher,
  194. const void *in,
  195. void *out,
  196. size_t len,
  197. Error **errp);
  198. /**
  199. * qcrypto_cipher_setiv:
  200. * @cipher: the cipher object
  201. * @iv: the initialization vector or counter (CTR mode) bytes
  202. * @niv: the length of @iv
  203. * @errpr: pointer to a NULL-initialized error object
  204. *
  205. * If the @cipher object is setup to use a mode that requires
  206. * initialization vectors or counter, this sets the @niv
  207. * bytes. The @iv data should have the same length as the
  208. * cipher key used when originally constructing the cipher
  209. * object. It is an error to set an initialization vector
  210. * or counter if the cipher mode does not require one.
  211. *
  212. * Returns: 0 on success, -1 on error
  213. */
  214. int qcrypto_cipher_setiv(QCryptoCipher *cipher,
  215. const uint8_t *iv, size_t niv,
  216. Error **errp);
  217. #endif /* QCRYPTO_CIPHER_H */