cipher-builtin.c.inc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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 "crypto/aes.h"
  21. #include "crypto/desrfb.h"
  22. #include "crypto/xts.h"
  23. typedef struct QCryptoCipherBuiltinAESContext QCryptoCipherBuiltinAESContext;
  24. struct QCryptoCipherBuiltinAESContext {
  25. AES_KEY enc;
  26. AES_KEY dec;
  27. };
  28. typedef struct QCryptoCipherBuiltinAES QCryptoCipherBuiltinAES;
  29. struct QCryptoCipherBuiltinAES {
  30. QCryptoCipher base;
  31. QCryptoCipherBuiltinAESContext key;
  32. QCryptoCipherBuiltinAESContext key_tweak;
  33. uint8_t iv[AES_BLOCK_SIZE];
  34. };
  35. static inline bool qcrypto_length_check(size_t len, size_t blocksize,
  36. Error **errp)
  37. {
  38. if (unlikely(len & (blocksize - 1))) {
  39. error_setg(errp, "Length %zu must be a multiple of block size %zu",
  40. len, blocksize);
  41. return false;
  42. }
  43. return true;
  44. }
  45. static void qcrypto_cipher_ctx_free(QCryptoCipher *cipher)
  46. {
  47. g_free(cipher);
  48. }
  49. static int qcrypto_cipher_no_setiv(QCryptoCipher *cipher,
  50. const uint8_t *iv, size_t niv,
  51. Error **errp)
  52. {
  53. error_setg(errp, "Setting IV is not supported");
  54. return -1;
  55. }
  56. static void do_aes_encrypt_ecb(const void *vctx,
  57. size_t len,
  58. uint8_t *out,
  59. const uint8_t *in)
  60. {
  61. const QCryptoCipherBuiltinAESContext *ctx = vctx;
  62. /* We have already verified that len % AES_BLOCK_SIZE == 0. */
  63. while (len) {
  64. AES_encrypt(in, out, &ctx->enc);
  65. in += AES_BLOCK_SIZE;
  66. out += AES_BLOCK_SIZE;
  67. len -= AES_BLOCK_SIZE;
  68. }
  69. }
  70. static void do_aes_decrypt_ecb(const void *vctx,
  71. size_t len,
  72. uint8_t *out,
  73. const uint8_t *in)
  74. {
  75. const QCryptoCipherBuiltinAESContext *ctx = vctx;
  76. /* We have already verified that len % AES_BLOCK_SIZE == 0. */
  77. while (len) {
  78. AES_decrypt(in, out, &ctx->dec);
  79. in += AES_BLOCK_SIZE;
  80. out += AES_BLOCK_SIZE;
  81. len -= AES_BLOCK_SIZE;
  82. }
  83. }
  84. static void do_aes_encrypt_cbc(const AES_KEY *key,
  85. size_t len,
  86. uint8_t *out,
  87. const uint8_t *in,
  88. uint8_t *ivec)
  89. {
  90. uint8_t tmp[AES_BLOCK_SIZE];
  91. size_t n;
  92. /* We have already verified that len % AES_BLOCK_SIZE == 0. */
  93. while (len) {
  94. for (n = 0; n < AES_BLOCK_SIZE; ++n) {
  95. tmp[n] = in[n] ^ ivec[n];
  96. }
  97. AES_encrypt(tmp, out, key);
  98. memcpy(ivec, out, AES_BLOCK_SIZE);
  99. len -= AES_BLOCK_SIZE;
  100. in += AES_BLOCK_SIZE;
  101. out += AES_BLOCK_SIZE;
  102. }
  103. }
  104. static void do_aes_decrypt_cbc(const AES_KEY *key,
  105. size_t len,
  106. uint8_t *out,
  107. const uint8_t *in,
  108. uint8_t *ivec)
  109. {
  110. uint8_t tmp[AES_BLOCK_SIZE];
  111. size_t n;
  112. /* We have already verified that len % AES_BLOCK_SIZE == 0. */
  113. while (len) {
  114. memcpy(tmp, in, AES_BLOCK_SIZE);
  115. AES_decrypt(in, out, key);
  116. for (n = 0; n < AES_BLOCK_SIZE; ++n) {
  117. out[n] ^= ivec[n];
  118. }
  119. memcpy(ivec, tmp, AES_BLOCK_SIZE);
  120. len -= AES_BLOCK_SIZE;
  121. in += AES_BLOCK_SIZE;
  122. out += AES_BLOCK_SIZE;
  123. }
  124. }
  125. static int qcrypto_cipher_aes_encrypt_ecb(QCryptoCipher *cipher,
  126. const void *in, void *out,
  127. size_t len, Error **errp)
  128. {
  129. QCryptoCipherBuiltinAES *ctx
  130. = container_of(cipher, QCryptoCipherBuiltinAES, base);
  131. if (!qcrypto_length_check(len, AES_BLOCK_SIZE, errp)) {
  132. return -1;
  133. }
  134. do_aes_encrypt_ecb(&ctx->key, len, out, in);
  135. return 0;
  136. }
  137. static int qcrypto_cipher_aes_decrypt_ecb(QCryptoCipher *cipher,
  138. const void *in, void *out,
  139. size_t len, Error **errp)
  140. {
  141. QCryptoCipherBuiltinAES *ctx
  142. = container_of(cipher, QCryptoCipherBuiltinAES, base);
  143. if (!qcrypto_length_check(len, AES_BLOCK_SIZE, errp)) {
  144. return -1;
  145. }
  146. do_aes_decrypt_ecb(&ctx->key, len, out, in);
  147. return 0;
  148. }
  149. static int qcrypto_cipher_aes_encrypt_cbc(QCryptoCipher *cipher,
  150. const void *in, void *out,
  151. size_t len, Error **errp)
  152. {
  153. QCryptoCipherBuiltinAES *ctx
  154. = container_of(cipher, QCryptoCipherBuiltinAES, base);
  155. if (!qcrypto_length_check(len, AES_BLOCK_SIZE, errp)) {
  156. return -1;
  157. }
  158. do_aes_encrypt_cbc(&ctx->key.enc, len, out, in, ctx->iv);
  159. return 0;
  160. }
  161. static int qcrypto_cipher_aes_decrypt_cbc(QCryptoCipher *cipher,
  162. const void *in, void *out,
  163. size_t len, Error **errp)
  164. {
  165. QCryptoCipherBuiltinAES *ctx
  166. = container_of(cipher, QCryptoCipherBuiltinAES, base);
  167. if (!qcrypto_length_check(len, AES_BLOCK_SIZE, errp)) {
  168. return -1;
  169. }
  170. do_aes_decrypt_cbc(&ctx->key.dec, len, out, in, ctx->iv);
  171. return 0;
  172. }
  173. static int qcrypto_cipher_aes_encrypt_xts(QCryptoCipher *cipher,
  174. const void *in, void *out,
  175. size_t len, Error **errp)
  176. {
  177. QCryptoCipherBuiltinAES *ctx
  178. = container_of(cipher, QCryptoCipherBuiltinAES, base);
  179. if (!qcrypto_length_check(len, AES_BLOCK_SIZE, errp)) {
  180. return -1;
  181. }
  182. xts_encrypt(&ctx->key, &ctx->key_tweak,
  183. do_aes_encrypt_ecb, do_aes_decrypt_ecb,
  184. ctx->iv, len, out, in);
  185. return 0;
  186. }
  187. static int qcrypto_cipher_aes_decrypt_xts(QCryptoCipher *cipher,
  188. const void *in, void *out,
  189. size_t len, Error **errp)
  190. {
  191. QCryptoCipherBuiltinAES *ctx
  192. = container_of(cipher, QCryptoCipherBuiltinAES, base);
  193. if (!qcrypto_length_check(len, AES_BLOCK_SIZE, errp)) {
  194. return -1;
  195. }
  196. xts_decrypt(&ctx->key, &ctx->key_tweak,
  197. do_aes_encrypt_ecb, do_aes_decrypt_ecb,
  198. ctx->iv, len, out, in);
  199. return 0;
  200. }
  201. static int qcrypto_cipher_aes_setiv(QCryptoCipher *cipher, const uint8_t *iv,
  202. size_t niv, Error **errp)
  203. {
  204. QCryptoCipherBuiltinAES *ctx
  205. = container_of(cipher, QCryptoCipherBuiltinAES, base);
  206. if (niv != AES_BLOCK_SIZE) {
  207. error_setg(errp, "IV must be %d bytes not %zu",
  208. AES_BLOCK_SIZE, niv);
  209. return -1;
  210. }
  211. memcpy(ctx->iv, iv, AES_BLOCK_SIZE);
  212. return 0;
  213. }
  214. static const struct QCryptoCipherDriver qcrypto_cipher_aes_driver_ecb = {
  215. .cipher_encrypt = qcrypto_cipher_aes_encrypt_ecb,
  216. .cipher_decrypt = qcrypto_cipher_aes_decrypt_ecb,
  217. .cipher_setiv = qcrypto_cipher_no_setiv,
  218. .cipher_free = qcrypto_cipher_ctx_free,
  219. };
  220. static const struct QCryptoCipherDriver qcrypto_cipher_aes_driver_cbc = {
  221. .cipher_encrypt = qcrypto_cipher_aes_encrypt_cbc,
  222. .cipher_decrypt = qcrypto_cipher_aes_decrypt_cbc,
  223. .cipher_setiv = qcrypto_cipher_aes_setiv,
  224. .cipher_free = qcrypto_cipher_ctx_free,
  225. };
  226. static const struct QCryptoCipherDriver qcrypto_cipher_aes_driver_xts = {
  227. .cipher_encrypt = qcrypto_cipher_aes_encrypt_xts,
  228. .cipher_decrypt = qcrypto_cipher_aes_decrypt_xts,
  229. .cipher_setiv = qcrypto_cipher_aes_setiv,
  230. .cipher_free = qcrypto_cipher_ctx_free,
  231. };
  232. typedef struct QCryptoCipherBuiltinDESRFB QCryptoCipherBuiltinDESRFB;
  233. struct QCryptoCipherBuiltinDESRFB {
  234. QCryptoCipher base;
  235. /* C.f. alg_key_len[QCRYPTO_CIPHER_ALG_DES_RFB] */
  236. uint8_t key[8];
  237. };
  238. static int qcrypto_cipher_encrypt_des_rfb(QCryptoCipher *cipher,
  239. const void *in, void *out,
  240. size_t len, Error **errp)
  241. {
  242. QCryptoCipherBuiltinDESRFB *ctx
  243. = container_of(cipher, QCryptoCipherBuiltinDESRFB, base);
  244. size_t i;
  245. if (!qcrypto_length_check(len, 8, errp)) {
  246. return -1;
  247. }
  248. deskey(ctx->key, EN0);
  249. for (i = 0; i < len; i += 8) {
  250. des((void *)in + i, out + i);
  251. }
  252. return 0;
  253. }
  254. static int qcrypto_cipher_decrypt_des_rfb(QCryptoCipher *cipher,
  255. const void *in, void *out,
  256. size_t len, Error **errp)
  257. {
  258. QCryptoCipherBuiltinDESRFB *ctx
  259. = container_of(cipher, QCryptoCipherBuiltinDESRFB, base);
  260. size_t i;
  261. if (!qcrypto_length_check(len, 8, errp)) {
  262. return -1;
  263. }
  264. deskey(ctx->key, DE1);
  265. for (i = 0; i < len; i += 8) {
  266. des((void *)in + i, out + i);
  267. }
  268. return 0;
  269. }
  270. static const struct QCryptoCipherDriver qcrypto_cipher_des_rfb_driver = {
  271. .cipher_encrypt = qcrypto_cipher_encrypt_des_rfb,
  272. .cipher_decrypt = qcrypto_cipher_decrypt_des_rfb,
  273. .cipher_setiv = qcrypto_cipher_no_setiv,
  274. .cipher_free = qcrypto_cipher_ctx_free,
  275. };
  276. bool qcrypto_cipher_supports(QCryptoCipherAlgorithm alg,
  277. QCryptoCipherMode mode)
  278. {
  279. switch (alg) {
  280. case QCRYPTO_CIPHER_ALG_DES_RFB:
  281. return mode == QCRYPTO_CIPHER_MODE_ECB;
  282. case QCRYPTO_CIPHER_ALG_AES_128:
  283. case QCRYPTO_CIPHER_ALG_AES_192:
  284. case QCRYPTO_CIPHER_ALG_AES_256:
  285. switch (mode) {
  286. case QCRYPTO_CIPHER_MODE_ECB:
  287. case QCRYPTO_CIPHER_MODE_CBC:
  288. case QCRYPTO_CIPHER_MODE_XTS:
  289. return true;
  290. default:
  291. return false;
  292. }
  293. break;
  294. default:
  295. return false;
  296. }
  297. }
  298. static QCryptoCipher *qcrypto_cipher_ctx_new(QCryptoCipherAlgorithm alg,
  299. QCryptoCipherMode mode,
  300. const uint8_t *key,
  301. size_t nkey,
  302. Error **errp)
  303. {
  304. if (!qcrypto_cipher_validate_key_length(alg, mode, nkey, errp)) {
  305. return NULL;
  306. }
  307. switch (alg) {
  308. case QCRYPTO_CIPHER_ALG_DES_RFB:
  309. if (mode == QCRYPTO_CIPHER_MODE_ECB) {
  310. QCryptoCipherBuiltinDESRFB *ctx;
  311. ctx = g_new0(QCryptoCipherBuiltinDESRFB, 1);
  312. ctx->base.driver = &qcrypto_cipher_des_rfb_driver;
  313. memcpy(ctx->key, key, sizeof(ctx->key));
  314. return &ctx->base;
  315. }
  316. goto bad_mode;
  317. case QCRYPTO_CIPHER_ALG_AES_128:
  318. case QCRYPTO_CIPHER_ALG_AES_192:
  319. case QCRYPTO_CIPHER_ALG_AES_256:
  320. {
  321. QCryptoCipherBuiltinAES *ctx;
  322. const QCryptoCipherDriver *drv;
  323. switch (mode) {
  324. case QCRYPTO_CIPHER_MODE_ECB:
  325. drv = &qcrypto_cipher_aes_driver_ecb;
  326. break;
  327. case QCRYPTO_CIPHER_MODE_CBC:
  328. drv = &qcrypto_cipher_aes_driver_cbc;
  329. break;
  330. case QCRYPTO_CIPHER_MODE_XTS:
  331. drv = &qcrypto_cipher_aes_driver_xts;
  332. break;
  333. default:
  334. goto bad_mode;
  335. }
  336. ctx = g_new0(QCryptoCipherBuiltinAES, 1);
  337. ctx->base.driver = drv;
  338. if (mode == QCRYPTO_CIPHER_MODE_XTS) {
  339. nkey /= 2;
  340. if (AES_set_encrypt_key(key + nkey, nkey * 8,
  341. &ctx->key_tweak.enc)) {
  342. error_setg(errp, "Failed to set encryption key");
  343. goto error;
  344. }
  345. if (AES_set_decrypt_key(key + nkey, nkey * 8,
  346. &ctx->key_tweak.dec)) {
  347. error_setg(errp, "Failed to set decryption key");
  348. goto error;
  349. }
  350. }
  351. if (AES_set_encrypt_key(key, nkey * 8, &ctx->key.enc)) {
  352. error_setg(errp, "Failed to set encryption key");
  353. goto error;
  354. }
  355. if (AES_set_decrypt_key(key, nkey * 8, &ctx->key.dec)) {
  356. error_setg(errp, "Failed to set decryption key");
  357. goto error;
  358. }
  359. return &ctx->base;
  360. error:
  361. g_free(ctx);
  362. return NULL;
  363. }
  364. default:
  365. error_setg(errp,
  366. "Unsupported cipher algorithm %s",
  367. QCryptoCipherAlgorithm_str(alg));
  368. return NULL;
  369. }
  370. bad_mode:
  371. error_setg(errp, "Unsupported cipher mode %s",
  372. QCryptoCipherMode_str(mode));
  373. return NULL;
  374. }