2
0

cipher-builtin.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  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(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(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((AES_KEY *)&aesctx->enc,
  124. src, dst, length);
  125. }
  126. static void qcrypto_cipher_aes_xts_decrypt(const void *ctx,
  127. size_t length,
  128. uint8_t *dst,
  129. const uint8_t *src)
  130. {
  131. const QCryptoCipherBuiltinAESContext *aesctx = ctx;
  132. qcrypto_cipher_aes_ecb_decrypt((AES_KEY *)&aesctx->dec,
  133. src, dst, length);
  134. }
  135. static int qcrypto_cipher_encrypt_aes(QCryptoCipher *cipher,
  136. const void *in,
  137. void *out,
  138. size_t len,
  139. Error **errp)
  140. {
  141. QCryptoCipherBuiltin *ctxt = cipher->opaque;
  142. switch (cipher->mode) {
  143. case QCRYPTO_CIPHER_MODE_ECB:
  144. qcrypto_cipher_aes_ecb_encrypt(&ctxt->state.aes.key.enc,
  145. in, out, len);
  146. break;
  147. case QCRYPTO_CIPHER_MODE_CBC:
  148. AES_cbc_encrypt(in, out, len,
  149. &ctxt->state.aes.key.enc,
  150. ctxt->state.aes.iv, 1);
  151. break;
  152. case QCRYPTO_CIPHER_MODE_XTS:
  153. xts_encrypt(&ctxt->state.aes.key,
  154. &ctxt->state.aes.key_tweak,
  155. qcrypto_cipher_aes_xts_encrypt,
  156. qcrypto_cipher_aes_xts_decrypt,
  157. ctxt->state.aes.iv,
  158. len, out, in);
  159. break;
  160. default:
  161. g_assert_not_reached();
  162. }
  163. return 0;
  164. }
  165. static int qcrypto_cipher_decrypt_aes(QCryptoCipher *cipher,
  166. const void *in,
  167. void *out,
  168. size_t len,
  169. Error **errp)
  170. {
  171. QCryptoCipherBuiltin *ctxt = cipher->opaque;
  172. switch (cipher->mode) {
  173. case QCRYPTO_CIPHER_MODE_ECB:
  174. qcrypto_cipher_aes_ecb_decrypt(&ctxt->state.aes.key.dec,
  175. in, out, len);
  176. break;
  177. case QCRYPTO_CIPHER_MODE_CBC:
  178. AES_cbc_encrypt(in, out, len,
  179. &ctxt->state.aes.key.dec,
  180. ctxt->state.aes.iv, 0);
  181. break;
  182. case QCRYPTO_CIPHER_MODE_XTS:
  183. xts_decrypt(&ctxt->state.aes.key,
  184. &ctxt->state.aes.key_tweak,
  185. qcrypto_cipher_aes_xts_encrypt,
  186. qcrypto_cipher_aes_xts_decrypt,
  187. ctxt->state.aes.iv,
  188. len, out, in);
  189. break;
  190. default:
  191. g_assert_not_reached();
  192. }
  193. return 0;
  194. }
  195. static int qcrypto_cipher_setiv_aes(QCryptoCipher *cipher,
  196. const uint8_t *iv, size_t niv,
  197. Error **errp)
  198. {
  199. QCryptoCipherBuiltin *ctxt = cipher->opaque;
  200. if (niv != AES_BLOCK_SIZE) {
  201. error_setg(errp, "IV must be %d bytes not %zu",
  202. AES_BLOCK_SIZE, niv);
  203. return -1;
  204. }
  205. memcpy(ctxt->state.aes.iv, iv, AES_BLOCK_SIZE);
  206. return 0;
  207. }
  208. static QCryptoCipherBuiltin *
  209. qcrypto_cipher_init_aes(QCryptoCipherMode mode,
  210. const uint8_t *key, size_t nkey,
  211. Error **errp)
  212. {
  213. QCryptoCipherBuiltin *ctxt;
  214. if (mode != QCRYPTO_CIPHER_MODE_CBC &&
  215. mode != QCRYPTO_CIPHER_MODE_ECB &&
  216. mode != QCRYPTO_CIPHER_MODE_XTS) {
  217. error_setg(errp, "Unsupported cipher mode %s",
  218. QCryptoCipherMode_str(mode));
  219. return NULL;
  220. }
  221. ctxt = g_new0(QCryptoCipherBuiltin, 1);
  222. if (mode == QCRYPTO_CIPHER_MODE_XTS) {
  223. if (AES_set_encrypt_key(key, nkey * 4, &ctxt->state.aes.key.enc) != 0) {
  224. error_setg(errp, "Failed to set encryption key");
  225. goto error;
  226. }
  227. if (AES_set_decrypt_key(key, nkey * 4, &ctxt->state.aes.key.dec) != 0) {
  228. error_setg(errp, "Failed to set decryption key");
  229. goto error;
  230. }
  231. if (AES_set_encrypt_key(key + (nkey / 2), nkey * 4,
  232. &ctxt->state.aes.key_tweak.enc) != 0) {
  233. error_setg(errp, "Failed to set encryption key");
  234. goto error;
  235. }
  236. if (AES_set_decrypt_key(key + (nkey / 2), nkey * 4,
  237. &ctxt->state.aes.key_tweak.dec) != 0) {
  238. error_setg(errp, "Failed to set decryption key");
  239. goto error;
  240. }
  241. } else {
  242. if (AES_set_encrypt_key(key, nkey * 8, &ctxt->state.aes.key.enc) != 0) {
  243. error_setg(errp, "Failed to set encryption key");
  244. goto error;
  245. }
  246. if (AES_set_decrypt_key(key, nkey * 8, &ctxt->state.aes.key.dec) != 0) {
  247. error_setg(errp, "Failed to set decryption key");
  248. goto error;
  249. }
  250. }
  251. ctxt->blocksize = AES_BLOCK_SIZE;
  252. ctxt->free = qcrypto_cipher_free_aes;
  253. ctxt->setiv = qcrypto_cipher_setiv_aes;
  254. ctxt->encrypt = qcrypto_cipher_encrypt_aes;
  255. ctxt->decrypt = qcrypto_cipher_decrypt_aes;
  256. return ctxt;
  257. error:
  258. g_free(ctxt);
  259. return NULL;
  260. }
  261. static void qcrypto_cipher_free_des_rfb(QCryptoCipher *cipher)
  262. {
  263. QCryptoCipherBuiltin *ctxt = cipher->opaque;
  264. g_free(ctxt->state.desrfb.key);
  265. g_free(ctxt);
  266. cipher->opaque = NULL;
  267. }
  268. static int qcrypto_cipher_encrypt_des_rfb(QCryptoCipher *cipher,
  269. const void *in,
  270. void *out,
  271. size_t len,
  272. Error **errp)
  273. {
  274. QCryptoCipherBuiltin *ctxt = cipher->opaque;
  275. size_t i;
  276. if (len % 8) {
  277. error_setg(errp, "Buffer size must be multiple of 8 not %zu",
  278. len);
  279. return -1;
  280. }
  281. deskey(ctxt->state.desrfb.key, EN0);
  282. for (i = 0; i < len; i += 8) {
  283. des((void *)in + i, out + i);
  284. }
  285. return 0;
  286. }
  287. static int qcrypto_cipher_decrypt_des_rfb(QCryptoCipher *cipher,
  288. const void *in,
  289. void *out,
  290. size_t len,
  291. Error **errp)
  292. {
  293. QCryptoCipherBuiltin *ctxt = cipher->opaque;
  294. size_t i;
  295. if (len % 8) {
  296. error_setg(errp, "Buffer size must be multiple of 8 not %zu",
  297. len);
  298. return -1;
  299. }
  300. deskey(ctxt->state.desrfb.key, DE1);
  301. for (i = 0; i < len; i += 8) {
  302. des((void *)in + i, out + i);
  303. }
  304. return 0;
  305. }
  306. static int qcrypto_cipher_setiv_des_rfb(QCryptoCipher *cipher,
  307. const uint8_t *iv, size_t niv,
  308. Error **errp)
  309. {
  310. error_setg(errp, "Setting IV is not supported");
  311. return -1;
  312. }
  313. static QCryptoCipherBuiltin *
  314. qcrypto_cipher_init_des_rfb(QCryptoCipherMode mode,
  315. const uint8_t *key, size_t nkey,
  316. Error **errp)
  317. {
  318. QCryptoCipherBuiltin *ctxt;
  319. if (mode != QCRYPTO_CIPHER_MODE_ECB) {
  320. error_setg(errp, "Unsupported cipher mode %s",
  321. QCryptoCipherMode_str(mode));
  322. return NULL;
  323. }
  324. ctxt = g_new0(QCryptoCipherBuiltin, 1);
  325. ctxt->state.desrfb.key = g_new0(uint8_t, nkey);
  326. memcpy(ctxt->state.desrfb.key, key, nkey);
  327. ctxt->state.desrfb.nkey = nkey;
  328. ctxt->blocksize = 8;
  329. ctxt->free = qcrypto_cipher_free_des_rfb;
  330. ctxt->setiv = qcrypto_cipher_setiv_des_rfb;
  331. ctxt->encrypt = qcrypto_cipher_encrypt_des_rfb;
  332. ctxt->decrypt = qcrypto_cipher_decrypt_des_rfb;
  333. return ctxt;
  334. }
  335. bool qcrypto_cipher_supports(QCryptoCipherAlgorithm alg,
  336. QCryptoCipherMode mode)
  337. {
  338. switch (alg) {
  339. case QCRYPTO_CIPHER_ALG_DES_RFB:
  340. case QCRYPTO_CIPHER_ALG_AES_128:
  341. case QCRYPTO_CIPHER_ALG_AES_192:
  342. case QCRYPTO_CIPHER_ALG_AES_256:
  343. break;
  344. default:
  345. return false;
  346. }
  347. switch (mode) {
  348. case QCRYPTO_CIPHER_MODE_ECB:
  349. case QCRYPTO_CIPHER_MODE_CBC:
  350. case QCRYPTO_CIPHER_MODE_XTS:
  351. return true;
  352. case QCRYPTO_CIPHER_MODE_CTR:
  353. return false;
  354. default:
  355. return false;
  356. }
  357. }
  358. static QCryptoCipherBuiltin *qcrypto_cipher_ctx_new(QCryptoCipherAlgorithm alg,
  359. QCryptoCipherMode mode,
  360. const uint8_t *key,
  361. size_t nkey,
  362. Error **errp)
  363. {
  364. QCryptoCipherBuiltin *ctxt;
  365. switch (mode) {
  366. case QCRYPTO_CIPHER_MODE_ECB:
  367. case QCRYPTO_CIPHER_MODE_CBC:
  368. case QCRYPTO_CIPHER_MODE_XTS:
  369. break;
  370. default:
  371. error_setg(errp, "Unsupported cipher mode %s",
  372. QCryptoCipherMode_str(mode));
  373. return NULL;
  374. }
  375. if (!qcrypto_cipher_validate_key_length(alg, mode, nkey, errp)) {
  376. return NULL;
  377. }
  378. switch (alg) {
  379. case QCRYPTO_CIPHER_ALG_DES_RFB:
  380. ctxt = qcrypto_cipher_init_des_rfb(mode, key, nkey, errp);
  381. break;
  382. case QCRYPTO_CIPHER_ALG_AES_128:
  383. case QCRYPTO_CIPHER_ALG_AES_192:
  384. case QCRYPTO_CIPHER_ALG_AES_256:
  385. ctxt = qcrypto_cipher_init_aes(mode, key, nkey, errp);
  386. break;
  387. default:
  388. error_setg(errp,
  389. "Unsupported cipher algorithm %s",
  390. QCryptoCipherAlgorithm_str(alg));
  391. return NULL;
  392. }
  393. return ctxt;
  394. }
  395. static void
  396. qcrypto_builtin_cipher_ctx_free(QCryptoCipher *cipher)
  397. {
  398. QCryptoCipherBuiltin *ctxt;
  399. ctxt = cipher->opaque;
  400. ctxt->free(cipher);
  401. }
  402. static int
  403. qcrypto_builtin_cipher_encrypt(QCryptoCipher *cipher,
  404. const void *in,
  405. void *out,
  406. size_t len,
  407. Error **errp)
  408. {
  409. QCryptoCipherBuiltin *ctxt = cipher->opaque;
  410. if (len % ctxt->blocksize) {
  411. error_setg(errp, "Length %zu must be a multiple of block size %zu",
  412. len, ctxt->blocksize);
  413. return -1;
  414. }
  415. return ctxt->encrypt(cipher, in, out, len, errp);
  416. }
  417. static int
  418. qcrypto_builtin_cipher_decrypt(QCryptoCipher *cipher,
  419. const void *in,
  420. void *out,
  421. size_t len,
  422. Error **errp)
  423. {
  424. QCryptoCipherBuiltin *ctxt = cipher->opaque;
  425. if (len % ctxt->blocksize) {
  426. error_setg(errp, "Length %zu must be a multiple of block size %zu",
  427. len, ctxt->blocksize);
  428. return -1;
  429. }
  430. return ctxt->decrypt(cipher, in, out, len, errp);
  431. }
  432. static int
  433. qcrypto_builtin_cipher_setiv(QCryptoCipher *cipher,
  434. const uint8_t *iv, size_t niv,
  435. Error **errp)
  436. {
  437. QCryptoCipherBuiltin *ctxt = cipher->opaque;
  438. return ctxt->setiv(cipher, iv, niv, errp);
  439. }
  440. static struct QCryptoCipherDriver qcrypto_cipher_lib_driver = {
  441. .cipher_encrypt = qcrypto_builtin_cipher_encrypt,
  442. .cipher_decrypt = qcrypto_builtin_cipher_decrypt,
  443. .cipher_setiv = qcrypto_builtin_cipher_setiv,
  444. .cipher_free = qcrypto_builtin_cipher_ctx_free,
  445. };