cipher-builtin.c.inc 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. typedef struct QCryptoCipherBuiltinAESContext QCryptoCipherBuiltinAESContext;
  22. struct QCryptoCipherBuiltinAESContext {
  23. AES_KEY enc;
  24. AES_KEY dec;
  25. };
  26. typedef struct QCryptoCipherBuiltinAES QCryptoCipherBuiltinAES;
  27. struct QCryptoCipherBuiltinAES {
  28. QCryptoCipher base;
  29. QCryptoCipherBuiltinAESContext key;
  30. uint8_t iv[AES_BLOCK_SIZE];
  31. };
  32. static inline bool qcrypto_length_check(size_t len, size_t blocksize,
  33. Error **errp)
  34. {
  35. if (unlikely(len & (blocksize - 1))) {
  36. error_setg(errp, "Length %zu must be a multiple of block size %zu",
  37. len, blocksize);
  38. return false;
  39. }
  40. return true;
  41. }
  42. static void qcrypto_cipher_ctx_free(QCryptoCipher *cipher)
  43. {
  44. g_free(cipher);
  45. }
  46. static int qcrypto_cipher_no_setiv(QCryptoCipher *cipher,
  47. const uint8_t *iv, size_t niv,
  48. Error **errp)
  49. {
  50. error_setg(errp, "Setting IV is not supported");
  51. return -1;
  52. }
  53. static void do_aes_encrypt_ecb(const void *vctx,
  54. size_t len,
  55. uint8_t *out,
  56. const uint8_t *in)
  57. {
  58. const QCryptoCipherBuiltinAESContext *ctx = vctx;
  59. /* We have already verified that len % AES_BLOCK_SIZE == 0. */
  60. while (len) {
  61. AES_encrypt(in, out, &ctx->enc);
  62. in += AES_BLOCK_SIZE;
  63. out += AES_BLOCK_SIZE;
  64. len -= AES_BLOCK_SIZE;
  65. }
  66. }
  67. static void do_aes_decrypt_ecb(const void *vctx,
  68. size_t len,
  69. uint8_t *out,
  70. const uint8_t *in)
  71. {
  72. const QCryptoCipherBuiltinAESContext *ctx = vctx;
  73. /* We have already verified that len % AES_BLOCK_SIZE == 0. */
  74. while (len) {
  75. AES_decrypt(in, out, &ctx->dec);
  76. in += AES_BLOCK_SIZE;
  77. out += AES_BLOCK_SIZE;
  78. len -= AES_BLOCK_SIZE;
  79. }
  80. }
  81. static void do_aes_encrypt_cbc(const AES_KEY *key,
  82. size_t len,
  83. uint8_t *out,
  84. const uint8_t *in,
  85. uint8_t *ivec)
  86. {
  87. uint8_t tmp[AES_BLOCK_SIZE];
  88. size_t n;
  89. /* We have already verified that len % AES_BLOCK_SIZE == 0. */
  90. while (len) {
  91. for (n = 0; n < AES_BLOCK_SIZE; ++n) {
  92. tmp[n] = in[n] ^ ivec[n];
  93. }
  94. AES_encrypt(tmp, out, key);
  95. memcpy(ivec, out, AES_BLOCK_SIZE);
  96. len -= AES_BLOCK_SIZE;
  97. in += AES_BLOCK_SIZE;
  98. out += AES_BLOCK_SIZE;
  99. }
  100. }
  101. static void do_aes_decrypt_cbc(const AES_KEY *key,
  102. size_t len,
  103. uint8_t *out,
  104. const uint8_t *in,
  105. uint8_t *ivec)
  106. {
  107. uint8_t tmp[AES_BLOCK_SIZE];
  108. size_t n;
  109. /* We have already verified that len % AES_BLOCK_SIZE == 0. */
  110. while (len) {
  111. memcpy(tmp, in, AES_BLOCK_SIZE);
  112. AES_decrypt(in, out, key);
  113. for (n = 0; n < AES_BLOCK_SIZE; ++n) {
  114. out[n] ^= ivec[n];
  115. }
  116. memcpy(ivec, tmp, AES_BLOCK_SIZE);
  117. len -= AES_BLOCK_SIZE;
  118. in += AES_BLOCK_SIZE;
  119. out += AES_BLOCK_SIZE;
  120. }
  121. }
  122. static int qcrypto_cipher_aes_encrypt_ecb(QCryptoCipher *cipher,
  123. const void *in, void *out,
  124. size_t len, Error **errp)
  125. {
  126. QCryptoCipherBuiltinAES *ctx
  127. = container_of(cipher, QCryptoCipherBuiltinAES, base);
  128. if (!qcrypto_length_check(len, AES_BLOCK_SIZE, errp)) {
  129. return -1;
  130. }
  131. do_aes_encrypt_ecb(&ctx->key, len, out, in);
  132. return 0;
  133. }
  134. static int qcrypto_cipher_aes_decrypt_ecb(QCryptoCipher *cipher,
  135. const void *in, void *out,
  136. size_t len, Error **errp)
  137. {
  138. QCryptoCipherBuiltinAES *ctx
  139. = container_of(cipher, QCryptoCipherBuiltinAES, base);
  140. if (!qcrypto_length_check(len, AES_BLOCK_SIZE, errp)) {
  141. return -1;
  142. }
  143. do_aes_decrypt_ecb(&ctx->key, len, out, in);
  144. return 0;
  145. }
  146. static int qcrypto_cipher_aes_encrypt_cbc(QCryptoCipher *cipher,
  147. const void *in, void *out,
  148. size_t len, Error **errp)
  149. {
  150. QCryptoCipherBuiltinAES *ctx
  151. = container_of(cipher, QCryptoCipherBuiltinAES, base);
  152. if (!qcrypto_length_check(len, AES_BLOCK_SIZE, errp)) {
  153. return -1;
  154. }
  155. do_aes_encrypt_cbc(&ctx->key.enc, len, out, in, ctx->iv);
  156. return 0;
  157. }
  158. static int qcrypto_cipher_aes_decrypt_cbc(QCryptoCipher *cipher,
  159. const void *in, void *out,
  160. size_t len, Error **errp)
  161. {
  162. QCryptoCipherBuiltinAES *ctx
  163. = container_of(cipher, QCryptoCipherBuiltinAES, base);
  164. if (!qcrypto_length_check(len, AES_BLOCK_SIZE, errp)) {
  165. return -1;
  166. }
  167. do_aes_decrypt_cbc(&ctx->key.dec, len, out, in, ctx->iv);
  168. return 0;
  169. }
  170. static int qcrypto_cipher_aes_setiv(QCryptoCipher *cipher, const uint8_t *iv,
  171. size_t niv, Error **errp)
  172. {
  173. QCryptoCipherBuiltinAES *ctx
  174. = container_of(cipher, QCryptoCipherBuiltinAES, base);
  175. if (niv != AES_BLOCK_SIZE) {
  176. error_setg(errp, "IV must be %d bytes not %zu",
  177. AES_BLOCK_SIZE, niv);
  178. return -1;
  179. }
  180. memcpy(ctx->iv, iv, AES_BLOCK_SIZE);
  181. return 0;
  182. }
  183. static const struct QCryptoCipherDriver qcrypto_cipher_aes_driver_ecb = {
  184. .cipher_encrypt = qcrypto_cipher_aes_encrypt_ecb,
  185. .cipher_decrypt = qcrypto_cipher_aes_decrypt_ecb,
  186. .cipher_setiv = qcrypto_cipher_no_setiv,
  187. .cipher_free = qcrypto_cipher_ctx_free,
  188. };
  189. static const struct QCryptoCipherDriver qcrypto_cipher_aes_driver_cbc = {
  190. .cipher_encrypt = qcrypto_cipher_aes_encrypt_cbc,
  191. .cipher_decrypt = qcrypto_cipher_aes_decrypt_cbc,
  192. .cipher_setiv = qcrypto_cipher_aes_setiv,
  193. .cipher_free = qcrypto_cipher_ctx_free,
  194. };
  195. bool qcrypto_cipher_supports(QCryptoCipherAlgorithm alg,
  196. QCryptoCipherMode mode)
  197. {
  198. switch (alg) {
  199. case QCRYPTO_CIPHER_ALG_AES_128:
  200. case QCRYPTO_CIPHER_ALG_AES_192:
  201. case QCRYPTO_CIPHER_ALG_AES_256:
  202. switch (mode) {
  203. case QCRYPTO_CIPHER_MODE_ECB:
  204. case QCRYPTO_CIPHER_MODE_CBC:
  205. return true;
  206. default:
  207. return false;
  208. }
  209. break;
  210. default:
  211. return false;
  212. }
  213. }
  214. static QCryptoCipher *qcrypto_cipher_ctx_new(QCryptoCipherAlgorithm alg,
  215. QCryptoCipherMode mode,
  216. const uint8_t *key,
  217. size_t nkey,
  218. Error **errp)
  219. {
  220. if (!qcrypto_cipher_validate_key_length(alg, mode, nkey, errp)) {
  221. return NULL;
  222. }
  223. switch (alg) {
  224. case QCRYPTO_CIPHER_ALG_AES_128:
  225. case QCRYPTO_CIPHER_ALG_AES_192:
  226. case QCRYPTO_CIPHER_ALG_AES_256:
  227. {
  228. QCryptoCipherBuiltinAES *ctx;
  229. const QCryptoCipherDriver *drv;
  230. switch (mode) {
  231. case QCRYPTO_CIPHER_MODE_ECB:
  232. drv = &qcrypto_cipher_aes_driver_ecb;
  233. break;
  234. case QCRYPTO_CIPHER_MODE_CBC:
  235. drv = &qcrypto_cipher_aes_driver_cbc;
  236. break;
  237. default:
  238. goto bad_mode;
  239. }
  240. ctx = g_new0(QCryptoCipherBuiltinAES, 1);
  241. ctx->base.driver = drv;
  242. if (AES_set_encrypt_key(key, nkey * 8, &ctx->key.enc)) {
  243. error_setg(errp, "Failed to set encryption key");
  244. goto error;
  245. }
  246. if (AES_set_decrypt_key(key, nkey * 8, &ctx->key.dec)) {
  247. error_setg(errp, "Failed to set decryption key");
  248. goto error;
  249. }
  250. return &ctx->base;
  251. error:
  252. g_free(ctx);
  253. return NULL;
  254. }
  255. default:
  256. error_setg(errp,
  257. "Unsupported cipher algorithm %s",
  258. QCryptoCipherAlgorithm_str(alg));
  259. return NULL;
  260. }
  261. bad_mode:
  262. error_setg(errp, "Unsupported cipher mode %s",
  263. QCryptoCipherMode_str(mode));
  264. return NULL;
  265. }