cipher.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. #include "qemu/osdep.h"
  21. #include "qemu/host-utils.h"
  22. #include "qapi/error.h"
  23. #include "crypto/cipher.h"
  24. #include "cipherpriv.h"
  25. static const size_t alg_key_len[QCRYPTO_CIPHER_ALG__MAX] = {
  26. [QCRYPTO_CIPHER_ALG_AES_128] = 16,
  27. [QCRYPTO_CIPHER_ALG_AES_192] = 24,
  28. [QCRYPTO_CIPHER_ALG_AES_256] = 32,
  29. [QCRYPTO_CIPHER_ALG_DES_RFB] = 8,
  30. [QCRYPTO_CIPHER_ALG_3DES] = 24,
  31. [QCRYPTO_CIPHER_ALG_CAST5_128] = 16,
  32. [QCRYPTO_CIPHER_ALG_SERPENT_128] = 16,
  33. [QCRYPTO_CIPHER_ALG_SERPENT_192] = 24,
  34. [QCRYPTO_CIPHER_ALG_SERPENT_256] = 32,
  35. [QCRYPTO_CIPHER_ALG_TWOFISH_128] = 16,
  36. [QCRYPTO_CIPHER_ALG_TWOFISH_192] = 24,
  37. [QCRYPTO_CIPHER_ALG_TWOFISH_256] = 32,
  38. };
  39. static const size_t alg_block_len[QCRYPTO_CIPHER_ALG__MAX] = {
  40. [QCRYPTO_CIPHER_ALG_AES_128] = 16,
  41. [QCRYPTO_CIPHER_ALG_AES_192] = 16,
  42. [QCRYPTO_CIPHER_ALG_AES_256] = 16,
  43. [QCRYPTO_CIPHER_ALG_DES_RFB] = 8,
  44. [QCRYPTO_CIPHER_ALG_3DES] = 8,
  45. [QCRYPTO_CIPHER_ALG_CAST5_128] = 8,
  46. [QCRYPTO_CIPHER_ALG_SERPENT_128] = 16,
  47. [QCRYPTO_CIPHER_ALG_SERPENT_192] = 16,
  48. [QCRYPTO_CIPHER_ALG_SERPENT_256] = 16,
  49. [QCRYPTO_CIPHER_ALG_TWOFISH_128] = 16,
  50. [QCRYPTO_CIPHER_ALG_TWOFISH_192] = 16,
  51. [QCRYPTO_CIPHER_ALG_TWOFISH_256] = 16,
  52. };
  53. static const bool mode_need_iv[QCRYPTO_CIPHER_MODE__MAX] = {
  54. [QCRYPTO_CIPHER_MODE_ECB] = false,
  55. [QCRYPTO_CIPHER_MODE_CBC] = true,
  56. [QCRYPTO_CIPHER_MODE_XTS] = true,
  57. [QCRYPTO_CIPHER_MODE_CTR] = true,
  58. };
  59. size_t qcrypto_cipher_get_block_len(QCryptoCipherAlgorithm alg)
  60. {
  61. assert(alg < G_N_ELEMENTS(alg_key_len));
  62. return alg_block_len[alg];
  63. }
  64. size_t qcrypto_cipher_get_key_len(QCryptoCipherAlgorithm alg)
  65. {
  66. assert(alg < G_N_ELEMENTS(alg_key_len));
  67. return alg_key_len[alg];
  68. }
  69. size_t qcrypto_cipher_get_iv_len(QCryptoCipherAlgorithm alg,
  70. QCryptoCipherMode mode)
  71. {
  72. if (alg >= G_N_ELEMENTS(alg_block_len)) {
  73. return 0;
  74. }
  75. if (mode >= G_N_ELEMENTS(mode_need_iv)) {
  76. return 0;
  77. }
  78. if (mode_need_iv[mode]) {
  79. return alg_block_len[alg];
  80. }
  81. return 0;
  82. }
  83. static bool
  84. qcrypto_cipher_validate_key_length(QCryptoCipherAlgorithm alg,
  85. QCryptoCipherMode mode,
  86. size_t nkey,
  87. Error **errp)
  88. {
  89. if ((unsigned)alg >= QCRYPTO_CIPHER_ALG__MAX) {
  90. error_setg(errp, "Cipher algorithm %d out of range",
  91. alg);
  92. return false;
  93. }
  94. if (mode == QCRYPTO_CIPHER_MODE_XTS) {
  95. if (alg == QCRYPTO_CIPHER_ALG_DES_RFB
  96. || alg == QCRYPTO_CIPHER_ALG_3DES) {
  97. error_setg(errp, "XTS mode not compatible with DES-RFB/3DES");
  98. return false;
  99. }
  100. if (nkey % 2) {
  101. error_setg(errp, "XTS cipher key length should be a multiple of 2");
  102. return false;
  103. }
  104. if (alg_key_len[alg] != (nkey / 2)) {
  105. error_setg(errp, "Cipher key length %zu should be %zu",
  106. nkey, alg_key_len[alg] * 2);
  107. return false;
  108. }
  109. } else {
  110. if (alg_key_len[alg] != nkey) {
  111. error_setg(errp, "Cipher key length %zu should be %zu",
  112. nkey, alg_key_len[alg]);
  113. return false;
  114. }
  115. }
  116. return true;
  117. }
  118. #if defined(CONFIG_GCRYPT) || defined(CONFIG_NETTLE)
  119. static uint8_t *
  120. qcrypto_cipher_munge_des_rfb_key(const uint8_t *key,
  121. size_t nkey)
  122. {
  123. uint8_t *ret = g_new0(uint8_t, nkey);
  124. size_t i;
  125. for (i = 0; i < nkey; i++) {
  126. uint8_t r = key[i];
  127. r = (r & 0xf0) >> 4 | (r & 0x0f) << 4;
  128. r = (r & 0xcc) >> 2 | (r & 0x33) << 2;
  129. r = (r & 0xaa) >> 1 | (r & 0x55) << 1;
  130. ret[i] = r;
  131. }
  132. return ret;
  133. }
  134. #endif /* CONFIG_GCRYPT || CONFIG_NETTLE */
  135. #ifdef CONFIG_GCRYPT
  136. #include "cipher-gcrypt.c.inc"
  137. #elif defined CONFIG_NETTLE
  138. #include "cipher-nettle.c.inc"
  139. #else
  140. #include "cipher-builtin.c.inc"
  141. #endif
  142. QCryptoCipher *qcrypto_cipher_new(QCryptoCipherAlgorithm alg,
  143. QCryptoCipherMode mode,
  144. const uint8_t *key, size_t nkey,
  145. Error **errp)
  146. {
  147. QCryptoCipher *cipher = NULL;
  148. #ifdef CONFIG_AF_ALG
  149. cipher = qcrypto_afalg_cipher_ctx_new(alg, mode, key, nkey, NULL);
  150. #endif
  151. if (!cipher) {
  152. cipher = qcrypto_cipher_ctx_new(alg, mode, key, nkey, errp);
  153. if (!cipher) {
  154. return NULL;
  155. }
  156. }
  157. cipher->alg = alg;
  158. cipher->mode = mode;
  159. return cipher;
  160. }
  161. int qcrypto_cipher_encrypt(QCryptoCipher *cipher,
  162. const void *in,
  163. void *out,
  164. size_t len,
  165. Error **errp)
  166. {
  167. const QCryptoCipherDriver *drv = cipher->driver;
  168. return drv->cipher_encrypt(cipher, in, out, len, errp);
  169. }
  170. int qcrypto_cipher_decrypt(QCryptoCipher *cipher,
  171. const void *in,
  172. void *out,
  173. size_t len,
  174. Error **errp)
  175. {
  176. const QCryptoCipherDriver *drv = cipher->driver;
  177. return drv->cipher_decrypt(cipher, in, out, len, errp);
  178. }
  179. int qcrypto_cipher_setiv(QCryptoCipher *cipher,
  180. const uint8_t *iv, size_t niv,
  181. Error **errp)
  182. {
  183. const QCryptoCipherDriver *drv = cipher->driver;
  184. return drv->cipher_setiv(cipher, iv, niv, errp);
  185. }
  186. void qcrypto_cipher_free(QCryptoCipher *cipher)
  187. {
  188. if (cipher) {
  189. cipher->driver->cipher_free(cipher);
  190. }
  191. }