2
0

cipher-nettle.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
  1. /*
  2. * QEMU Crypto cipher nettle 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. #ifdef CONFIG_QEMU_PRIVATE_XTS
  22. #include "crypto/xts.h"
  23. #endif
  24. #include "cipherpriv.h"
  25. #include <nettle/nettle-types.h>
  26. #include <nettle/aes.h>
  27. #include <nettle/des.h>
  28. #include <nettle/cbc.h>
  29. #include <nettle/cast128.h>
  30. #include <nettle/serpent.h>
  31. #include <nettle/twofish.h>
  32. #include <nettle/ctr.h>
  33. #ifndef CONFIG_QEMU_PRIVATE_XTS
  34. #include <nettle/xts.h>
  35. #endif
  36. typedef void (*QCryptoCipherNettleFuncWrapper)(const void *ctx,
  37. size_t length,
  38. uint8_t *dst,
  39. const uint8_t *src);
  40. #if CONFIG_NETTLE_VERSION_MAJOR < 3
  41. typedef nettle_crypt_func * QCryptoCipherNettleFuncNative;
  42. typedef void * cipher_ctx_t;
  43. typedef unsigned cipher_length_t;
  44. #define cast5_set_key cast128_set_key
  45. #define aes128_ctx aes_ctx
  46. #define aes192_ctx aes_ctx
  47. #define aes256_ctx aes_ctx
  48. #define aes128_set_encrypt_key(c, k) \
  49. aes_set_encrypt_key(c, 16, k)
  50. #define aes192_set_encrypt_key(c, k) \
  51. aes_set_encrypt_key(c, 24, k)
  52. #define aes256_set_encrypt_key(c, k) \
  53. aes_set_encrypt_key(c, 32, k)
  54. #define aes128_set_decrypt_key(c, k) \
  55. aes_set_decrypt_key(c, 16, k)
  56. #define aes192_set_decrypt_key(c, k) \
  57. aes_set_decrypt_key(c, 24, k)
  58. #define aes256_set_decrypt_key(c, k) \
  59. aes_set_decrypt_key(c, 32, k)
  60. #define aes128_encrypt aes_encrypt
  61. #define aes192_encrypt aes_encrypt
  62. #define aes256_encrypt aes_encrypt
  63. #define aes128_decrypt aes_decrypt
  64. #define aes192_decrypt aes_decrypt
  65. #define aes256_decrypt aes_decrypt
  66. #else
  67. typedef nettle_cipher_func * QCryptoCipherNettleFuncNative;
  68. typedef const void * cipher_ctx_t;
  69. typedef size_t cipher_length_t;
  70. #endif
  71. typedef struct QCryptoNettleAES128 {
  72. struct aes128_ctx enc;
  73. struct aes128_ctx dec;
  74. } QCryptoNettleAES128;
  75. typedef struct QCryptoNettleAES192 {
  76. struct aes192_ctx enc;
  77. struct aes192_ctx dec;
  78. } QCryptoNettleAES192;
  79. typedef struct QCryptoNettleAES256 {
  80. struct aes256_ctx enc;
  81. struct aes256_ctx dec;
  82. } QCryptoNettleAES256;
  83. static void aes128_encrypt_native(cipher_ctx_t ctx, cipher_length_t length,
  84. uint8_t *dst, const uint8_t *src)
  85. {
  86. const QCryptoNettleAES128 *aesctx = ctx;
  87. aes128_encrypt(&aesctx->enc, length, dst, src);
  88. }
  89. static void aes128_decrypt_native(cipher_ctx_t ctx, cipher_length_t length,
  90. uint8_t *dst, const uint8_t *src)
  91. {
  92. const QCryptoNettleAES128 *aesctx = ctx;
  93. aes128_decrypt(&aesctx->dec, length, dst, src);
  94. }
  95. static void aes192_encrypt_native(cipher_ctx_t ctx, cipher_length_t length,
  96. uint8_t *dst, const uint8_t *src)
  97. {
  98. const QCryptoNettleAES192 *aesctx = ctx;
  99. aes192_encrypt(&aesctx->enc, length, dst, src);
  100. }
  101. static void aes192_decrypt_native(cipher_ctx_t ctx, cipher_length_t length,
  102. uint8_t *dst, const uint8_t *src)
  103. {
  104. const QCryptoNettleAES192 *aesctx = ctx;
  105. aes192_decrypt(&aesctx->dec, length, dst, src);
  106. }
  107. static void aes256_encrypt_native(cipher_ctx_t ctx, cipher_length_t length,
  108. uint8_t *dst, const uint8_t *src)
  109. {
  110. const QCryptoNettleAES256 *aesctx = ctx;
  111. aes256_encrypt(&aesctx->enc, length, dst, src);
  112. }
  113. static void aes256_decrypt_native(cipher_ctx_t ctx, cipher_length_t length,
  114. uint8_t *dst, const uint8_t *src)
  115. {
  116. const QCryptoNettleAES256 *aesctx = ctx;
  117. aes256_decrypt(&aesctx->dec, length, dst, src);
  118. }
  119. static void des_encrypt_native(cipher_ctx_t ctx, cipher_length_t length,
  120. uint8_t *dst, const uint8_t *src)
  121. {
  122. des_encrypt(ctx, length, dst, src);
  123. }
  124. static void des_decrypt_native(cipher_ctx_t ctx, cipher_length_t length,
  125. uint8_t *dst, const uint8_t *src)
  126. {
  127. des_decrypt(ctx, length, dst, src);
  128. }
  129. static void des3_encrypt_native(cipher_ctx_t ctx, cipher_length_t length,
  130. uint8_t *dst, const uint8_t *src)
  131. {
  132. des3_encrypt(ctx, length, dst, src);
  133. }
  134. static void des3_decrypt_native(cipher_ctx_t ctx, cipher_length_t length,
  135. uint8_t *dst, const uint8_t *src)
  136. {
  137. des3_decrypt(ctx, length, dst, src);
  138. }
  139. static void cast128_encrypt_native(cipher_ctx_t ctx, cipher_length_t length,
  140. uint8_t *dst, const uint8_t *src)
  141. {
  142. cast128_encrypt(ctx, length, dst, src);
  143. }
  144. static void cast128_decrypt_native(cipher_ctx_t ctx, cipher_length_t length,
  145. uint8_t *dst, const uint8_t *src)
  146. {
  147. cast128_decrypt(ctx, length, dst, src);
  148. }
  149. static void serpent_encrypt_native(cipher_ctx_t ctx, cipher_length_t length,
  150. uint8_t *dst, const uint8_t *src)
  151. {
  152. serpent_encrypt(ctx, length, dst, src);
  153. }
  154. static void serpent_decrypt_native(cipher_ctx_t ctx, cipher_length_t length,
  155. uint8_t *dst, const uint8_t *src)
  156. {
  157. serpent_decrypt(ctx, length, dst, src);
  158. }
  159. static void twofish_encrypt_native(cipher_ctx_t ctx, cipher_length_t length,
  160. uint8_t *dst, const uint8_t *src)
  161. {
  162. twofish_encrypt(ctx, length, dst, src);
  163. }
  164. static void twofish_decrypt_native(cipher_ctx_t ctx, cipher_length_t length,
  165. uint8_t *dst, const uint8_t *src)
  166. {
  167. twofish_decrypt(ctx, length, dst, src);
  168. }
  169. static void aes128_encrypt_wrapper(const void *ctx, size_t length,
  170. uint8_t *dst, const uint8_t *src)
  171. {
  172. const QCryptoNettleAES128 *aesctx = ctx;
  173. aes128_encrypt(&aesctx->enc, length, dst, src);
  174. }
  175. static void aes128_decrypt_wrapper(const void *ctx, size_t length,
  176. uint8_t *dst, const uint8_t *src)
  177. {
  178. const QCryptoNettleAES128 *aesctx = ctx;
  179. aes128_decrypt(&aesctx->dec, length, dst, src);
  180. }
  181. static void aes192_encrypt_wrapper(const void *ctx, size_t length,
  182. uint8_t *dst, const uint8_t *src)
  183. {
  184. const QCryptoNettleAES192 *aesctx = ctx;
  185. aes192_encrypt(&aesctx->enc, length, dst, src);
  186. }
  187. static void aes192_decrypt_wrapper(const void *ctx, size_t length,
  188. uint8_t *dst, const uint8_t *src)
  189. {
  190. const QCryptoNettleAES192 *aesctx = ctx;
  191. aes192_decrypt(&aesctx->dec, length, dst, src);
  192. }
  193. static void aes256_encrypt_wrapper(const void *ctx, size_t length,
  194. uint8_t *dst, const uint8_t *src)
  195. {
  196. const QCryptoNettleAES256 *aesctx = ctx;
  197. aes256_encrypt(&aesctx->enc, length, dst, src);
  198. }
  199. static void aes256_decrypt_wrapper(const void *ctx, size_t length,
  200. uint8_t *dst, const uint8_t *src)
  201. {
  202. const QCryptoNettleAES256 *aesctx = ctx;
  203. aes256_decrypt(&aesctx->dec, length, dst, src);
  204. }
  205. static void des_encrypt_wrapper(const void *ctx, size_t length,
  206. uint8_t *dst, const uint8_t *src)
  207. {
  208. des_encrypt(ctx, length, dst, src);
  209. }
  210. static void des_decrypt_wrapper(const void *ctx, size_t length,
  211. uint8_t *dst, const uint8_t *src)
  212. {
  213. des_decrypt(ctx, length, dst, src);
  214. }
  215. static void des3_encrypt_wrapper(const void *ctx, size_t length,
  216. uint8_t *dst, const uint8_t *src)
  217. {
  218. des3_encrypt(ctx, length, dst, src);
  219. }
  220. static void des3_decrypt_wrapper(const void *ctx, size_t length,
  221. uint8_t *dst, const uint8_t *src)
  222. {
  223. des3_decrypt(ctx, length, dst, src);
  224. }
  225. static void cast128_encrypt_wrapper(const void *ctx, size_t length,
  226. uint8_t *dst, const uint8_t *src)
  227. {
  228. cast128_encrypt(ctx, length, dst, src);
  229. }
  230. static void cast128_decrypt_wrapper(const void *ctx, size_t length,
  231. uint8_t *dst, const uint8_t *src)
  232. {
  233. cast128_decrypt(ctx, length, dst, src);
  234. }
  235. static void serpent_encrypt_wrapper(const void *ctx, size_t length,
  236. uint8_t *dst, const uint8_t *src)
  237. {
  238. serpent_encrypt(ctx, length, dst, src);
  239. }
  240. static void serpent_decrypt_wrapper(const void *ctx, size_t length,
  241. uint8_t *dst, const uint8_t *src)
  242. {
  243. serpent_decrypt(ctx, length, dst, src);
  244. }
  245. static void twofish_encrypt_wrapper(const void *ctx, size_t length,
  246. uint8_t *dst, const uint8_t *src)
  247. {
  248. twofish_encrypt(ctx, length, dst, src);
  249. }
  250. static void twofish_decrypt_wrapper(const void *ctx, size_t length,
  251. uint8_t *dst, const uint8_t *src)
  252. {
  253. twofish_decrypt(ctx, length, dst, src);
  254. }
  255. typedef struct QCryptoCipherNettle QCryptoCipherNettle;
  256. struct QCryptoCipherNettle {
  257. /* Primary cipher context for all modes */
  258. void *ctx;
  259. /* Second cipher context for XTS mode only */
  260. void *ctx_tweak;
  261. /* Cipher callbacks for both contexts */
  262. QCryptoCipherNettleFuncNative alg_encrypt_native;
  263. QCryptoCipherNettleFuncNative alg_decrypt_native;
  264. QCryptoCipherNettleFuncWrapper alg_encrypt_wrapper;
  265. QCryptoCipherNettleFuncWrapper alg_decrypt_wrapper;
  266. /* Initialization vector or Counter */
  267. uint8_t *iv;
  268. size_t blocksize;
  269. };
  270. bool qcrypto_cipher_supports(QCryptoCipherAlgorithm alg,
  271. QCryptoCipherMode mode)
  272. {
  273. switch (alg) {
  274. case QCRYPTO_CIPHER_ALG_DES_RFB:
  275. case QCRYPTO_CIPHER_ALG_3DES:
  276. case QCRYPTO_CIPHER_ALG_AES_128:
  277. case QCRYPTO_CIPHER_ALG_AES_192:
  278. case QCRYPTO_CIPHER_ALG_AES_256:
  279. case QCRYPTO_CIPHER_ALG_CAST5_128:
  280. case QCRYPTO_CIPHER_ALG_SERPENT_128:
  281. case QCRYPTO_CIPHER_ALG_SERPENT_192:
  282. case QCRYPTO_CIPHER_ALG_SERPENT_256:
  283. case QCRYPTO_CIPHER_ALG_TWOFISH_128:
  284. case QCRYPTO_CIPHER_ALG_TWOFISH_192:
  285. case QCRYPTO_CIPHER_ALG_TWOFISH_256:
  286. break;
  287. default:
  288. return false;
  289. }
  290. switch (mode) {
  291. case QCRYPTO_CIPHER_MODE_ECB:
  292. case QCRYPTO_CIPHER_MODE_CBC:
  293. case QCRYPTO_CIPHER_MODE_XTS:
  294. case QCRYPTO_CIPHER_MODE_CTR:
  295. return true;
  296. default:
  297. return false;
  298. }
  299. }
  300. static void
  301. qcrypto_nettle_cipher_free_ctx(QCryptoCipherNettle *ctx)
  302. {
  303. if (!ctx) {
  304. return;
  305. }
  306. g_free(ctx->iv);
  307. g_free(ctx->ctx);
  308. g_free(ctx->ctx_tweak);
  309. g_free(ctx);
  310. }
  311. static QCryptoCipherNettle *qcrypto_cipher_ctx_new(QCryptoCipherAlgorithm alg,
  312. QCryptoCipherMode mode,
  313. const uint8_t *key,
  314. size_t nkey,
  315. Error **errp)
  316. {
  317. QCryptoCipherNettle *ctx;
  318. uint8_t *rfbkey;
  319. switch (mode) {
  320. case QCRYPTO_CIPHER_MODE_ECB:
  321. case QCRYPTO_CIPHER_MODE_CBC:
  322. case QCRYPTO_CIPHER_MODE_XTS:
  323. case QCRYPTO_CIPHER_MODE_CTR:
  324. break;
  325. default:
  326. error_setg(errp, "Unsupported cipher mode %s",
  327. QCryptoCipherMode_str(mode));
  328. return NULL;
  329. }
  330. if (!qcrypto_cipher_validate_key_length(alg, mode, nkey, errp)) {
  331. return NULL;
  332. }
  333. ctx = g_new0(QCryptoCipherNettle, 1);
  334. switch (alg) {
  335. case QCRYPTO_CIPHER_ALG_DES_RFB:
  336. ctx->ctx = g_new0(struct des_ctx, 1);
  337. rfbkey = qcrypto_cipher_munge_des_rfb_key(key, nkey);
  338. des_set_key(ctx->ctx, rfbkey);
  339. g_free(rfbkey);
  340. ctx->alg_encrypt_native = des_encrypt_native;
  341. ctx->alg_decrypt_native = des_decrypt_native;
  342. ctx->alg_encrypt_wrapper = des_encrypt_wrapper;
  343. ctx->alg_decrypt_wrapper = des_decrypt_wrapper;
  344. ctx->blocksize = DES_BLOCK_SIZE;
  345. break;
  346. case QCRYPTO_CIPHER_ALG_3DES:
  347. ctx->ctx = g_new0(struct des3_ctx, 1);
  348. des3_set_key(ctx->ctx, key);
  349. ctx->alg_encrypt_native = des3_encrypt_native;
  350. ctx->alg_decrypt_native = des3_decrypt_native;
  351. ctx->alg_encrypt_wrapper = des3_encrypt_wrapper;
  352. ctx->alg_decrypt_wrapper = des3_decrypt_wrapper;
  353. ctx->blocksize = DES3_BLOCK_SIZE;
  354. break;
  355. case QCRYPTO_CIPHER_ALG_AES_128:
  356. ctx->ctx = g_new0(QCryptoNettleAES128, 1);
  357. if (mode == QCRYPTO_CIPHER_MODE_XTS) {
  358. ctx->ctx_tweak = g_new0(QCryptoNettleAES128, 1);
  359. nkey /= 2;
  360. aes128_set_encrypt_key(&((QCryptoNettleAES128 *)ctx->ctx)->enc,
  361. key);
  362. aes128_set_decrypt_key(&((QCryptoNettleAES128 *)ctx->ctx)->dec,
  363. key);
  364. aes128_set_encrypt_key(&((QCryptoNettleAES128 *)ctx->ctx_tweak)->
  365. enc, key + nkey);
  366. aes128_set_decrypt_key(&((QCryptoNettleAES128 *)ctx->ctx_tweak)->
  367. dec, key + nkey);
  368. } else {
  369. aes128_set_encrypt_key(&((QCryptoNettleAES128 *)ctx->ctx)->enc,
  370. key);
  371. aes128_set_decrypt_key(&((QCryptoNettleAES128 *)ctx->ctx)->dec,
  372. key);
  373. }
  374. ctx->alg_encrypt_native = aes128_encrypt_native;
  375. ctx->alg_decrypt_native = aes128_decrypt_native;
  376. ctx->alg_encrypt_wrapper = aes128_encrypt_wrapper;
  377. ctx->alg_decrypt_wrapper = aes128_decrypt_wrapper;
  378. ctx->blocksize = AES_BLOCK_SIZE;
  379. break;
  380. case QCRYPTO_CIPHER_ALG_AES_192:
  381. ctx->ctx = g_new0(QCryptoNettleAES192, 1);
  382. if (mode == QCRYPTO_CIPHER_MODE_XTS) {
  383. ctx->ctx_tweak = g_new0(QCryptoNettleAES192, 1);
  384. nkey /= 2;
  385. aes192_set_encrypt_key(&((QCryptoNettleAES192 *)ctx->ctx)->enc,
  386. key);
  387. aes192_set_decrypt_key(&((QCryptoNettleAES192 *)ctx->ctx)->dec,
  388. key);
  389. aes192_set_encrypt_key(&((QCryptoNettleAES192 *)ctx->ctx_tweak)->
  390. enc, key + nkey);
  391. aes192_set_decrypt_key(&((QCryptoNettleAES192 *)ctx->ctx_tweak)->
  392. dec, key + nkey);
  393. } else {
  394. aes192_set_encrypt_key(&((QCryptoNettleAES192 *)ctx->ctx)->enc,
  395. key);
  396. aes192_set_decrypt_key(&((QCryptoNettleAES192 *)ctx->ctx)->dec,
  397. key);
  398. }
  399. ctx->alg_encrypt_native = aes192_encrypt_native;
  400. ctx->alg_decrypt_native = aes192_decrypt_native;
  401. ctx->alg_encrypt_wrapper = aes192_encrypt_wrapper;
  402. ctx->alg_decrypt_wrapper = aes192_decrypt_wrapper;
  403. ctx->blocksize = AES_BLOCK_SIZE;
  404. break;
  405. case QCRYPTO_CIPHER_ALG_AES_256:
  406. ctx->ctx = g_new0(QCryptoNettleAES256, 1);
  407. if (mode == QCRYPTO_CIPHER_MODE_XTS) {
  408. ctx->ctx_tweak = g_new0(QCryptoNettleAES256, 1);
  409. nkey /= 2;
  410. aes256_set_encrypt_key(&((QCryptoNettleAES256 *)ctx->ctx)->enc,
  411. key);
  412. aes256_set_decrypt_key(&((QCryptoNettleAES256 *)ctx->ctx)->dec,
  413. key);
  414. aes256_set_encrypt_key(&((QCryptoNettleAES256 *)ctx->ctx_tweak)->
  415. enc, key + nkey);
  416. aes256_set_decrypt_key(&((QCryptoNettleAES256 *)ctx->ctx_tweak)->
  417. dec, key + nkey);
  418. } else {
  419. aes256_set_encrypt_key(&((QCryptoNettleAES256 *)ctx->ctx)->enc,
  420. key);
  421. aes256_set_decrypt_key(&((QCryptoNettleAES256 *)ctx->ctx)->dec,
  422. key);
  423. }
  424. ctx->alg_encrypt_native = aes256_encrypt_native;
  425. ctx->alg_decrypt_native = aes256_decrypt_native;
  426. ctx->alg_encrypt_wrapper = aes256_encrypt_wrapper;
  427. ctx->alg_decrypt_wrapper = aes256_decrypt_wrapper;
  428. ctx->blocksize = AES_BLOCK_SIZE;
  429. break;
  430. case QCRYPTO_CIPHER_ALG_CAST5_128:
  431. ctx->ctx = g_new0(struct cast128_ctx, 1);
  432. if (mode == QCRYPTO_CIPHER_MODE_XTS) {
  433. ctx->ctx_tweak = g_new0(struct cast128_ctx, 1);
  434. nkey /= 2;
  435. cast5_set_key(ctx->ctx, nkey, key);
  436. cast5_set_key(ctx->ctx_tweak, nkey, key + nkey);
  437. } else {
  438. cast5_set_key(ctx->ctx, nkey, key);
  439. }
  440. ctx->alg_encrypt_native = cast128_encrypt_native;
  441. ctx->alg_decrypt_native = cast128_decrypt_native;
  442. ctx->alg_encrypt_wrapper = cast128_encrypt_wrapper;
  443. ctx->alg_decrypt_wrapper = cast128_decrypt_wrapper;
  444. ctx->blocksize = CAST128_BLOCK_SIZE;
  445. break;
  446. case QCRYPTO_CIPHER_ALG_SERPENT_128:
  447. case QCRYPTO_CIPHER_ALG_SERPENT_192:
  448. case QCRYPTO_CIPHER_ALG_SERPENT_256:
  449. ctx->ctx = g_new0(struct serpent_ctx, 1);
  450. if (mode == QCRYPTO_CIPHER_MODE_XTS) {
  451. ctx->ctx_tweak = g_new0(struct serpent_ctx, 1);
  452. nkey /= 2;
  453. serpent_set_key(ctx->ctx, nkey, key);
  454. serpent_set_key(ctx->ctx_tweak, nkey, key + nkey);
  455. } else {
  456. serpent_set_key(ctx->ctx, nkey, key);
  457. }
  458. ctx->alg_encrypt_native = serpent_encrypt_native;
  459. ctx->alg_decrypt_native = serpent_decrypt_native;
  460. ctx->alg_encrypt_wrapper = serpent_encrypt_wrapper;
  461. ctx->alg_decrypt_wrapper = serpent_decrypt_wrapper;
  462. ctx->blocksize = SERPENT_BLOCK_SIZE;
  463. break;
  464. case QCRYPTO_CIPHER_ALG_TWOFISH_128:
  465. case QCRYPTO_CIPHER_ALG_TWOFISH_192:
  466. case QCRYPTO_CIPHER_ALG_TWOFISH_256:
  467. ctx->ctx = g_new0(struct twofish_ctx, 1);
  468. if (mode == QCRYPTO_CIPHER_MODE_XTS) {
  469. ctx->ctx_tweak = g_new0(struct twofish_ctx, 1);
  470. nkey /= 2;
  471. twofish_set_key(ctx->ctx, nkey, key);
  472. twofish_set_key(ctx->ctx_tweak, nkey, key + nkey);
  473. } else {
  474. twofish_set_key(ctx->ctx, nkey, key);
  475. }
  476. ctx->alg_encrypt_native = twofish_encrypt_native;
  477. ctx->alg_decrypt_native = twofish_decrypt_native;
  478. ctx->alg_encrypt_wrapper = twofish_encrypt_wrapper;
  479. ctx->alg_decrypt_wrapper = twofish_decrypt_wrapper;
  480. ctx->blocksize = TWOFISH_BLOCK_SIZE;
  481. break;
  482. default:
  483. error_setg(errp, "Unsupported cipher algorithm %s",
  484. QCryptoCipherAlgorithm_str(alg));
  485. goto error;
  486. }
  487. if (mode == QCRYPTO_CIPHER_MODE_XTS &&
  488. ctx->blocksize != XTS_BLOCK_SIZE) {
  489. error_setg(errp, "Cipher block size %zu must equal XTS block size %d",
  490. ctx->blocksize, XTS_BLOCK_SIZE);
  491. goto error;
  492. }
  493. ctx->iv = g_new0(uint8_t, ctx->blocksize);
  494. return ctx;
  495. error:
  496. qcrypto_nettle_cipher_free_ctx(ctx);
  497. return NULL;
  498. }
  499. static void
  500. qcrypto_nettle_cipher_ctx_free(QCryptoCipher *cipher)
  501. {
  502. QCryptoCipherNettle *ctx;
  503. ctx = cipher->opaque;
  504. qcrypto_nettle_cipher_free_ctx(ctx);
  505. }
  506. static int
  507. qcrypto_nettle_cipher_encrypt(QCryptoCipher *cipher,
  508. const void *in,
  509. void *out,
  510. size_t len,
  511. Error **errp)
  512. {
  513. QCryptoCipherNettle *ctx = cipher->opaque;
  514. if (len % ctx->blocksize) {
  515. error_setg(errp, "Length %zu must be a multiple of block size %zu",
  516. len, ctx->blocksize);
  517. return -1;
  518. }
  519. switch (cipher->mode) {
  520. case QCRYPTO_CIPHER_MODE_ECB:
  521. ctx->alg_encrypt_wrapper(ctx->ctx, len, out, in);
  522. break;
  523. case QCRYPTO_CIPHER_MODE_CBC:
  524. cbc_encrypt(ctx->ctx, ctx->alg_encrypt_native,
  525. ctx->blocksize, ctx->iv,
  526. len, out, in);
  527. break;
  528. case QCRYPTO_CIPHER_MODE_XTS:
  529. #ifdef CONFIG_QEMU_PRIVATE_XTS
  530. xts_encrypt(ctx->ctx, ctx->ctx_tweak,
  531. ctx->alg_encrypt_wrapper, ctx->alg_encrypt_wrapper,
  532. ctx->iv, len, out, in);
  533. #else
  534. xts_encrypt_message(ctx->ctx, ctx->ctx_tweak,
  535. ctx->alg_encrypt_native,
  536. ctx->iv, len, out, in);
  537. #endif
  538. break;
  539. case QCRYPTO_CIPHER_MODE_CTR:
  540. ctr_crypt(ctx->ctx, ctx->alg_encrypt_native,
  541. ctx->blocksize, ctx->iv,
  542. len, out, in);
  543. break;
  544. default:
  545. error_setg(errp, "Unsupported cipher mode %s",
  546. QCryptoCipherMode_str(cipher->mode));
  547. return -1;
  548. }
  549. return 0;
  550. }
  551. static int
  552. qcrypto_nettle_cipher_decrypt(QCryptoCipher *cipher,
  553. const void *in,
  554. void *out,
  555. size_t len,
  556. Error **errp)
  557. {
  558. QCryptoCipherNettle *ctx = cipher->opaque;
  559. if (len % ctx->blocksize) {
  560. error_setg(errp, "Length %zu must be a multiple of block size %zu",
  561. len, ctx->blocksize);
  562. return -1;
  563. }
  564. switch (cipher->mode) {
  565. case QCRYPTO_CIPHER_MODE_ECB:
  566. ctx->alg_decrypt_wrapper(ctx->ctx, len, out, in);
  567. break;
  568. case QCRYPTO_CIPHER_MODE_CBC:
  569. cbc_decrypt(ctx->ctx, ctx->alg_decrypt_native,
  570. ctx->blocksize, ctx->iv,
  571. len, out, in);
  572. break;
  573. case QCRYPTO_CIPHER_MODE_XTS:
  574. #ifdef CONFIG_QEMU_PRIVATE_XTS
  575. xts_decrypt(ctx->ctx, ctx->ctx_tweak,
  576. ctx->alg_encrypt_wrapper, ctx->alg_decrypt_wrapper,
  577. ctx->iv, len, out, in);
  578. #else
  579. xts_decrypt_message(ctx->ctx, ctx->ctx_tweak,
  580. ctx->alg_decrypt_native,
  581. ctx->alg_encrypt_native,
  582. ctx->iv, len, out, in);
  583. #endif
  584. break;
  585. case QCRYPTO_CIPHER_MODE_CTR:
  586. ctr_crypt(ctx->ctx, ctx->alg_encrypt_native,
  587. ctx->blocksize, ctx->iv,
  588. len, out, in);
  589. break;
  590. default:
  591. error_setg(errp, "Unsupported cipher mode %s",
  592. QCryptoCipherMode_str(cipher->mode));
  593. return -1;
  594. }
  595. return 0;
  596. }
  597. static int
  598. qcrypto_nettle_cipher_setiv(QCryptoCipher *cipher,
  599. const uint8_t *iv, size_t niv,
  600. Error **errp)
  601. {
  602. QCryptoCipherNettle *ctx = cipher->opaque;
  603. if (niv != ctx->blocksize) {
  604. error_setg(errp, "Expected IV size %zu not %zu",
  605. ctx->blocksize, niv);
  606. return -1;
  607. }
  608. memcpy(ctx->iv, iv, niv);
  609. return 0;
  610. }
  611. static struct QCryptoCipherDriver qcrypto_cipher_lib_driver = {
  612. .cipher_encrypt = qcrypto_nettle_cipher_encrypt,
  613. .cipher_decrypt = qcrypto_nettle_cipher_decrypt,
  614. .cipher_setiv = qcrypto_nettle_cipher_setiv,
  615. .cipher_free = qcrypto_nettle_cipher_ctx_free,
  616. };