2
0

cipher-builtin.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. /*
  2. * QEMU Crypto cipher built-in 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 "crypto/aes.h"
  22. #include "crypto/desrfb.h"
  23. #include "crypto/xts.h"
  24. #include "cipherpriv.h"
  25. typedef struct QCryptoCipherBuiltinAESContext QCryptoCipherBuiltinAESContext;
  26. struct QCryptoCipherBuiltinAESContext {
  27. AES_KEY enc;
  28. AES_KEY dec;
  29. };
  30. typedef struct QCryptoCipherBuiltinAES QCryptoCipherBuiltinAES;
  31. struct QCryptoCipherBuiltinAES {
  32. QCryptoCipherBuiltinAESContext key;
  33. QCryptoCipherBuiltinAESContext key_tweak;
  34. uint8_t iv[AES_BLOCK_SIZE];
  35. };
  36. typedef struct QCryptoCipherBuiltinDESRFB QCryptoCipherBuiltinDESRFB;
  37. struct QCryptoCipherBuiltinDESRFB {
  38. uint8_t *key;
  39. size_t nkey;
  40. };
  41. typedef struct QCryptoCipherBuiltin QCryptoCipherBuiltin;
  42. struct QCryptoCipherBuiltin {
  43. union {
  44. QCryptoCipherBuiltinAES aes;
  45. QCryptoCipherBuiltinDESRFB desrfb;
  46. } state;
  47. size_t blocksize;
  48. void (*free)(QCryptoCipher *cipher);
  49. int (*setiv)(QCryptoCipher *cipher,
  50. const uint8_t *iv, size_t niv,
  51. Error **errp);
  52. int (*encrypt)(QCryptoCipher *cipher,
  53. const void *in,
  54. void *out,
  55. size_t len,
  56. Error **errp);
  57. int (*decrypt)(QCryptoCipher *cipher,
  58. const void *in,
  59. void *out,
  60. size_t len,
  61. Error **errp);
  62. };
  63. static void qcrypto_cipher_free_aes(QCryptoCipher *cipher)
  64. {
  65. QCryptoCipherBuiltin *ctxt = cipher->opaque;
  66. g_free(ctxt);
  67. cipher->opaque = NULL;
  68. }
  69. static void qcrypto_cipher_aes_ecb_encrypt(const AES_KEY *key,
  70. const void *in,
  71. void *out,
  72. size_t len)
  73. {
  74. const uint8_t *inptr = in;
  75. uint8_t *outptr = out;
  76. while (len) {
  77. if (len > AES_BLOCK_SIZE) {
  78. AES_encrypt(inptr, outptr, key);
  79. inptr += AES_BLOCK_SIZE;
  80. outptr += AES_BLOCK_SIZE;
  81. len -= AES_BLOCK_SIZE;
  82. } else {
  83. uint8_t tmp1[AES_BLOCK_SIZE], tmp2[AES_BLOCK_SIZE];
  84. memcpy(tmp1, inptr, len);
  85. /* Fill with 0 to avoid valgrind uninitialized reads */
  86. memset(tmp1 + len, 0, sizeof(tmp1) - len);
  87. AES_encrypt(tmp1, tmp2, key);
  88. memcpy(outptr, tmp2, len);
  89. len = 0;
  90. }
  91. }
  92. }
  93. static void qcrypto_cipher_aes_ecb_decrypt(const AES_KEY *key,
  94. const void *in,
  95. void *out,
  96. size_t len)
  97. {
  98. const uint8_t *inptr = in;
  99. uint8_t *outptr = out;
  100. while (len) {
  101. if (len > AES_BLOCK_SIZE) {
  102. AES_decrypt(inptr, outptr, key);
  103. inptr += AES_BLOCK_SIZE;
  104. outptr += AES_BLOCK_SIZE;
  105. len -= AES_BLOCK_SIZE;
  106. } else {
  107. uint8_t tmp1[AES_BLOCK_SIZE], tmp2[AES_BLOCK_SIZE];
  108. memcpy(tmp1, inptr, len);
  109. /* Fill with 0 to avoid valgrind uninitialized reads */
  110. memset(tmp1 + len, 0, sizeof(tmp1) - len);
  111. AES_decrypt(tmp1, tmp2, key);
  112. memcpy(outptr, tmp2, len);
  113. len = 0;
  114. }
  115. }
  116. }
  117. static void qcrypto_cipher_aes_xts_encrypt(const void *ctx,
  118. size_t length,
  119. uint8_t *dst,
  120. const uint8_t *src)
  121. {
  122. const QCryptoCipherBuiltinAESContext *aesctx = ctx;
  123. qcrypto_cipher_aes_ecb_encrypt(&aesctx->enc, src, dst, length);
  124. }
  125. static void qcrypto_cipher_aes_xts_decrypt(const void *ctx,
  126. size_t length,
  127. uint8_t *dst,
  128. const uint8_t *src)
  129. {
  130. const QCryptoCipherBuiltinAESContext *aesctx = ctx;
  131. qcrypto_cipher_aes_ecb_decrypt(&aesctx->dec, src, dst, length);
  132. }
  133. static int qcrypto_cipher_encrypt_aes(QCryptoCipher *cipher,
  134. const void *in,
  135. void *out,
  136. size_t len,
  137. Error **errp)
  138. {
  139. QCryptoCipherBuiltin *ctxt = cipher->opaque;
  140. switch (cipher->mode) {
  141. case QCRYPTO_CIPHER_MODE_ECB:
  142. qcrypto_cipher_aes_ecb_encrypt(&ctxt->state.aes.key.enc,
  143. in, out, len);
  144. break;
  145. case QCRYPTO_CIPHER_MODE_CBC:
  146. AES_cbc_encrypt(in, out, len,
  147. &ctxt->state.aes.key.enc,
  148. ctxt->state.aes.iv, 1);
  149. break;
  150. case QCRYPTO_CIPHER_MODE_XTS:
  151. xts_encrypt(&ctxt->state.aes.key,
  152. &ctxt->state.aes.key_tweak,
  153. qcrypto_cipher_aes_xts_encrypt,
  154. qcrypto_cipher_aes_xts_decrypt,
  155. ctxt->state.aes.iv,
  156. len, out, in);
  157. break;
  158. default:
  159. g_assert_not_reached();
  160. }
  161. return 0;
  162. }
  163. static int qcrypto_cipher_decrypt_aes(QCryptoCipher *cipher,
  164. const void *in,
  165. void *out,
  166. size_t len,
  167. Error **errp)
  168. {
  169. QCryptoCipherBuiltin *ctxt = cipher->opaque;
  170. switch (cipher->mode) {
  171. case QCRYPTO_CIPHER_MODE_ECB:
  172. qcrypto_cipher_aes_ecb_decrypt(&ctxt->state.aes.key.dec,
  173. in, out, len);
  174. break;
  175. case QCRYPTO_CIPHER_MODE_CBC:
  176. AES_cbc_encrypt(in, out, len,
  177. &ctxt->state.aes.key.dec,
  178. ctxt->state.aes.iv, 0);
  179. break;
  180. case QCRYPTO_CIPHER_MODE_XTS:
  181. xts_decrypt(&ctxt->state.aes.key,
  182. &ctxt->state.aes.key_tweak,
  183. qcrypto_cipher_aes_xts_encrypt,
  184. qcrypto_cipher_aes_xts_decrypt,
  185. ctxt->state.aes.iv,
  186. len, out, in);
  187. break;
  188. default:
  189. g_assert_not_reached();
  190. }
  191. return 0;
  192. }
  193. static int qcrypto_cipher_setiv_aes(QCryptoCipher *cipher,
  194. const uint8_t *iv, size_t niv,
  195. Error **errp)
  196. {
  197. QCryptoCipherBuiltin *ctxt = cipher->opaque;
  198. if (niv != AES_BLOCK_SIZE) {
  199. error_setg(errp, "IV must be %d bytes not %zu",
  200. AES_BLOCK_SIZE, niv);
  201. return -1;
  202. }
  203. memcpy(ctxt->state.aes.iv, iv, AES_BLOCK_SIZE);
  204. return 0;
  205. }
  206. static QCryptoCipherBuiltin *
  207. qcrypto_cipher_init_aes(QCryptoCipherMode mode,
  208. const uint8_t *key, size_t nkey,
  209. Error **errp)
  210. {
  211. QCryptoCipherBuiltin *ctxt;
  212. if (mode != QCRYPTO_CIPHER_MODE_CBC &&
  213. mode != QCRYPTO_CIPHER_MODE_ECB &&
  214. mode != QCRYPTO_CIPHER_MODE_XTS) {
  215. error_setg(errp, "Unsupported cipher mode %s",
  216. QCryptoCipherMode_str(mode));
  217. return NULL;
  218. }
  219. ctxt = g_new0(QCryptoCipherBuiltin, 1);
  220. if (mode == QCRYPTO_CIPHER_MODE_XTS) {
  221. if (AES_set_encrypt_key(key, nkey * 4, &ctxt->state.aes.key.enc) != 0) {
  222. error_setg(errp, "Failed to set encryption key");
  223. goto error;
  224. }
  225. if (AES_set_decrypt_key(key, nkey * 4, &ctxt->state.aes.key.dec) != 0) {
  226. error_setg(errp, "Failed to set decryption key");
  227. goto error;
  228. }
  229. if (AES_set_encrypt_key(key + (nkey / 2), nkey * 4,
  230. &ctxt->state.aes.key_tweak.enc) != 0) {
  231. error_setg(errp, "Failed to set encryption key");
  232. goto error;
  233. }
  234. if (AES_set_decrypt_key(key + (nkey / 2), nkey * 4,
  235. &ctxt->state.aes.key_tweak.dec) != 0) {
  236. error_setg(errp, "Failed to set decryption key");
  237. goto error;
  238. }
  239. } else {
  240. if (AES_set_encrypt_key(key, nkey * 8, &ctxt->state.aes.key.enc) != 0) {
  241. error_setg(errp, "Failed to set encryption key");
  242. goto error;
  243. }
  244. if (AES_set_decrypt_key(key, nkey * 8, &ctxt->state.aes.key.dec) != 0) {
  245. error_setg(errp, "Failed to set decryption key");
  246. goto error;
  247. }
  248. }
  249. ctxt->blocksize = AES_BLOCK_SIZE;
  250. ctxt->free = qcrypto_cipher_free_aes;
  251. ctxt->setiv = qcrypto_cipher_setiv_aes;
  252. ctxt->encrypt = qcrypto_cipher_encrypt_aes;
  253. ctxt->decrypt = qcrypto_cipher_decrypt_aes;
  254. return ctxt;
  255. error:
  256. g_free(ctxt);
  257. return NULL;
  258. }
  259. static void qcrypto_cipher_free_des_rfb(QCryptoCipher *cipher)
  260. {
  261. QCryptoCipherBuiltin *ctxt = cipher->opaque;
  262. g_free(ctxt->state.desrfb.key);
  263. g_free(ctxt);
  264. cipher->opaque = NULL;
  265. }
  266. static int qcrypto_cipher_encrypt_des_rfb(QCryptoCipher *cipher,
  267. const void *in,
  268. void *out,
  269. size_t len,
  270. Error **errp)
  271. {
  272. QCryptoCipherBuiltin *ctxt = cipher->opaque;
  273. size_t i;
  274. if (len % 8) {
  275. error_setg(errp, "Buffer size must be multiple of 8 not %zu",
  276. len);
  277. return -1;
  278. }
  279. deskey(ctxt->state.desrfb.key, EN0);
  280. for (i = 0; i < len; i += 8) {
  281. des((void *)in + i, out + i);
  282. }
  283. return 0;
  284. }
  285. static int qcrypto_cipher_decrypt_des_rfb(QCryptoCipher *cipher,
  286. const void *in,
  287. void *out,
  288. size_t len,
  289. Error **errp)
  290. {
  291. QCryptoCipherBuiltin *ctxt = cipher->opaque;
  292. size_t i;
  293. if (len % 8) {
  294. error_setg(errp, "Buffer size must be multiple of 8 not %zu",
  295. len);
  296. return -1;
  297. }
  298. deskey(ctxt->state.desrfb.key, DE1);
  299. for (i = 0; i < len; i += 8) {
  300. des((void *)in + i, out + i);
  301. }
  302. return 0;
  303. }
  304. static int qcrypto_cipher_setiv_des_rfb(QCryptoCipher *cipher,
  305. const uint8_t *iv, size_t niv,
  306. Error **errp)
  307. {
  308. error_setg(errp, "Setting IV is not supported");
  309. return -1;
  310. }
  311. static QCryptoCipherBuiltin *
  312. qcrypto_cipher_init_des_rfb(QCryptoCipherMode mode,
  313. const uint8_t *key, size_t nkey,
  314. Error **errp)
  315. {
  316. QCryptoCipherBuiltin *ctxt;
  317. if (mode != QCRYPTO_CIPHER_MODE_ECB) {
  318. error_setg(errp, "Unsupported cipher mode %s",
  319. QCryptoCipherMode_str(mode));
  320. return NULL;
  321. }
  322. ctxt = g_new0(QCryptoCipherBuiltin, 1);
  323. ctxt->state.desrfb.key = g_new0(uint8_t, nkey);
  324. memcpy(ctxt->state.desrfb.key, key, nkey);
  325. ctxt->state.desrfb.nkey = nkey;
  326. ctxt->blocksize = 8;
  327. ctxt->free = qcrypto_cipher_free_des_rfb;
  328. ctxt->setiv = qcrypto_cipher_setiv_des_rfb;
  329. ctxt->encrypt = qcrypto_cipher_encrypt_des_rfb;
  330. ctxt->decrypt = qcrypto_cipher_decrypt_des_rfb;
  331. return ctxt;
  332. }
  333. bool qcrypto_cipher_supports(QCryptoCipherAlgorithm alg,
  334. QCryptoCipherMode mode)
  335. {
  336. switch (alg) {
  337. case QCRYPTO_CIPHER_ALG_DES_RFB:
  338. case QCRYPTO_CIPHER_ALG_AES_128:
  339. case QCRYPTO_CIPHER_ALG_AES_192:
  340. case QCRYPTO_CIPHER_ALG_AES_256:
  341. break;
  342. default:
  343. return false;
  344. }
  345. switch (mode) {
  346. case QCRYPTO_CIPHER_MODE_ECB:
  347. case QCRYPTO_CIPHER_MODE_CBC:
  348. case QCRYPTO_CIPHER_MODE_XTS:
  349. return true;
  350. case QCRYPTO_CIPHER_MODE_CTR:
  351. return false;
  352. default:
  353. return false;
  354. }
  355. }
  356. static QCryptoCipherBuiltin *qcrypto_cipher_ctx_new(QCryptoCipherAlgorithm alg,
  357. QCryptoCipherMode mode,
  358. const uint8_t *key,
  359. size_t nkey,
  360. Error **errp)
  361. {
  362. QCryptoCipherBuiltin *ctxt;
  363. switch (mode) {
  364. case QCRYPTO_CIPHER_MODE_ECB:
  365. case QCRYPTO_CIPHER_MODE_CBC:
  366. case QCRYPTO_CIPHER_MODE_XTS:
  367. break;
  368. default:
  369. error_setg(errp, "Unsupported cipher mode %s",
  370. QCryptoCipherMode_str(mode));
  371. return NULL;
  372. }
  373. if (!qcrypto_cipher_validate_key_length(alg, mode, nkey, errp)) {
  374. return NULL;
  375. }
  376. switch (alg) {
  377. case QCRYPTO_CIPHER_ALG_DES_RFB:
  378. ctxt = qcrypto_cipher_init_des_rfb(mode, key, nkey, errp);
  379. break;
  380. case QCRYPTO_CIPHER_ALG_AES_128:
  381. case QCRYPTO_CIPHER_ALG_AES_192:
  382. case QCRYPTO_CIPHER_ALG_AES_256:
  383. ctxt = qcrypto_cipher_init_aes(mode, key, nkey, errp);
  384. break;
  385. default:
  386. error_setg(errp,
  387. "Unsupported cipher algorithm %s",
  388. QCryptoCipherAlgorithm_str(alg));
  389. return NULL;
  390. }
  391. return ctxt;
  392. }
  393. static void
  394. qcrypto_builtin_cipher_ctx_free(QCryptoCipher *cipher)
  395. {
  396. QCryptoCipherBuiltin *ctxt;
  397. ctxt = cipher->opaque;
  398. ctxt->free(cipher);
  399. }
  400. static int
  401. qcrypto_builtin_cipher_encrypt(QCryptoCipher *cipher,
  402. const void *in,
  403. void *out,
  404. size_t len,
  405. Error **errp)
  406. {
  407. QCryptoCipherBuiltin *ctxt = cipher->opaque;
  408. if (len % ctxt->blocksize) {
  409. error_setg(errp, "Length %zu must be a multiple of block size %zu",
  410. len, ctxt->blocksize);
  411. return -1;
  412. }
  413. return ctxt->encrypt(cipher, in, out, len, errp);
  414. }
  415. static int
  416. qcrypto_builtin_cipher_decrypt(QCryptoCipher *cipher,
  417. const void *in,
  418. void *out,
  419. size_t len,
  420. Error **errp)
  421. {
  422. QCryptoCipherBuiltin *ctxt = cipher->opaque;
  423. if (len % ctxt->blocksize) {
  424. error_setg(errp, "Length %zu must be a multiple of block size %zu",
  425. len, ctxt->blocksize);
  426. return -1;
  427. }
  428. return ctxt->decrypt(cipher, in, out, len, errp);
  429. }
  430. static int
  431. qcrypto_builtin_cipher_setiv(QCryptoCipher *cipher,
  432. const uint8_t *iv, size_t niv,
  433. Error **errp)
  434. {
  435. QCryptoCipherBuiltin *ctxt = cipher->opaque;
  436. return ctxt->setiv(cipher, iv, niv, errp);
  437. }
  438. static struct QCryptoCipherDriver qcrypto_cipher_lib_driver = {
  439. .cipher_encrypt = qcrypto_builtin_cipher_encrypt,
  440. .cipher_decrypt = qcrypto_builtin_cipher_decrypt,
  441. .cipher_setiv = qcrypto_builtin_cipher_setiv,
  442. .cipher_free = qcrypto_builtin_cipher_ctx_free,
  443. };