test-crypto-cipher.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805
  1. /*
  2. * QEMU Crypto cipher 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 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/init.h"
  22. #include "crypto/cipher.h"
  23. #include "qapi/error.h"
  24. typedef struct QCryptoCipherTestData QCryptoCipherTestData;
  25. struct QCryptoCipherTestData {
  26. const char *path;
  27. QCryptoCipherAlgorithm alg;
  28. QCryptoCipherMode mode;
  29. const char *key;
  30. const char *plaintext;
  31. const char *ciphertext;
  32. const char *iv;
  33. };
  34. /* AES test data comes from appendix F of:
  35. *
  36. * http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf
  37. */
  38. static QCryptoCipherTestData test_data[] = {
  39. {
  40. /* NIST F.1.1 ECB-AES128.Encrypt */
  41. .path = "/crypto/cipher/aes-ecb-128",
  42. .alg = QCRYPTO_CIPHER_ALG_AES_128,
  43. .mode = QCRYPTO_CIPHER_MODE_ECB,
  44. .key = "2b7e151628aed2a6abf7158809cf4f3c",
  45. .plaintext =
  46. "6bc1bee22e409f96e93d7e117393172a"
  47. "ae2d8a571e03ac9c9eb76fac45af8e51"
  48. "30c81c46a35ce411e5fbc1191a0a52ef"
  49. "f69f2445df4f9b17ad2b417be66c3710",
  50. .ciphertext =
  51. "3ad77bb40d7a3660a89ecaf32466ef97"
  52. "f5d3d58503b9699de785895a96fdbaaf"
  53. "43b1cd7f598ece23881b00e3ed030688"
  54. "7b0c785e27e8ad3f8223207104725dd4"
  55. },
  56. {
  57. /* NIST F.1.3 ECB-AES192.Encrypt */
  58. .path = "/crypto/cipher/aes-ecb-192",
  59. .alg = QCRYPTO_CIPHER_ALG_AES_192,
  60. .mode = QCRYPTO_CIPHER_MODE_ECB,
  61. .key = "8e73b0f7da0e6452c810f32b809079e562f8ead2522c6b7b",
  62. .plaintext =
  63. "6bc1bee22e409f96e93d7e117393172a"
  64. "ae2d8a571e03ac9c9eb76fac45af8e51"
  65. "30c81c46a35ce411e5fbc1191a0a52ef"
  66. "f69f2445df4f9b17ad2b417be66c3710",
  67. .ciphertext =
  68. "bd334f1d6e45f25ff712a214571fa5cc"
  69. "974104846d0ad3ad7734ecb3ecee4eef"
  70. "ef7afd2270e2e60adce0ba2face6444e"
  71. "9a4b41ba738d6c72fb16691603c18e0e"
  72. },
  73. {
  74. /* NIST F.1.5 ECB-AES256.Encrypt */
  75. .path = "/crypto/cipher/aes-ecb-256",
  76. .alg = QCRYPTO_CIPHER_ALG_AES_256,
  77. .mode = QCRYPTO_CIPHER_MODE_ECB,
  78. .key =
  79. "603deb1015ca71be2b73aef0857d7781"
  80. "1f352c073b6108d72d9810a30914dff4",
  81. .plaintext =
  82. "6bc1bee22e409f96e93d7e117393172a"
  83. "ae2d8a571e03ac9c9eb76fac45af8e51"
  84. "30c81c46a35ce411e5fbc1191a0a52ef"
  85. "f69f2445df4f9b17ad2b417be66c3710",
  86. .ciphertext =
  87. "f3eed1bdb5d2a03c064b5a7e3db181f8"
  88. "591ccb10d410ed26dc5ba74a31362870"
  89. "b6ed21b99ca6f4f9f153e7b1beafed1d"
  90. "23304b7a39f9f3ff067d8d8f9e24ecc7",
  91. },
  92. {
  93. /* NIST F.2.1 CBC-AES128.Encrypt */
  94. .path = "/crypto/cipher/aes-cbc-128",
  95. .alg = QCRYPTO_CIPHER_ALG_AES_128,
  96. .mode = QCRYPTO_CIPHER_MODE_CBC,
  97. .key = "2b7e151628aed2a6abf7158809cf4f3c",
  98. .iv = "000102030405060708090a0b0c0d0e0f",
  99. .plaintext =
  100. "6bc1bee22e409f96e93d7e117393172a"
  101. "ae2d8a571e03ac9c9eb76fac45af8e51"
  102. "30c81c46a35ce411e5fbc1191a0a52ef"
  103. "f69f2445df4f9b17ad2b417be66c3710",
  104. .ciphertext =
  105. "7649abac8119b246cee98e9b12e9197d"
  106. "5086cb9b507219ee95db113a917678b2"
  107. "73bed6b8e3c1743b7116e69e22229516"
  108. "3ff1caa1681fac09120eca307586e1a7",
  109. },
  110. {
  111. /* NIST F.2.3 CBC-AES128.Encrypt */
  112. .path = "/crypto/cipher/aes-cbc-192",
  113. .alg = QCRYPTO_CIPHER_ALG_AES_192,
  114. .mode = QCRYPTO_CIPHER_MODE_CBC,
  115. .key = "8e73b0f7da0e6452c810f32b809079e562f8ead2522c6b7b",
  116. .iv = "000102030405060708090a0b0c0d0e0f",
  117. .plaintext =
  118. "6bc1bee22e409f96e93d7e117393172a"
  119. "ae2d8a571e03ac9c9eb76fac45af8e51"
  120. "30c81c46a35ce411e5fbc1191a0a52ef"
  121. "f69f2445df4f9b17ad2b417be66c3710",
  122. .ciphertext =
  123. "4f021db243bc633d7178183a9fa071e8"
  124. "b4d9ada9ad7dedf4e5e738763f69145a"
  125. "571b242012fb7ae07fa9baac3df102e0"
  126. "08b0e27988598881d920a9e64f5615cd",
  127. },
  128. {
  129. /* NIST F.2.5 CBC-AES128.Encrypt */
  130. .path = "/crypto/cipher/aes-cbc-256",
  131. .alg = QCRYPTO_CIPHER_ALG_AES_256,
  132. .mode = QCRYPTO_CIPHER_MODE_CBC,
  133. .key =
  134. "603deb1015ca71be2b73aef0857d7781"
  135. "1f352c073b6108d72d9810a30914dff4",
  136. .iv = "000102030405060708090a0b0c0d0e0f",
  137. .plaintext =
  138. "6bc1bee22e409f96e93d7e117393172a"
  139. "ae2d8a571e03ac9c9eb76fac45af8e51"
  140. "30c81c46a35ce411e5fbc1191a0a52ef"
  141. "f69f2445df4f9b17ad2b417be66c3710",
  142. .ciphertext =
  143. "f58c4c04d6e5f1ba779eabfb5f7bfbd6"
  144. "9cfc4e967edb808d679f777bc6702c7d"
  145. "39f23369a9d9bacfa530e26304231461"
  146. "b2eb05e2c39be9fcda6c19078c6a9d1b",
  147. },
  148. {
  149. .path = "/crypto/cipher/des-rfb-ecb-56",
  150. .alg = QCRYPTO_CIPHER_ALG_DES_RFB,
  151. .mode = QCRYPTO_CIPHER_MODE_ECB,
  152. .key = "0123456789abcdef",
  153. .plaintext =
  154. "6bc1bee22e409f96e93d7e117393172a"
  155. "ae2d8a571e03ac9c9eb76fac45af8e51"
  156. "30c81c46a35ce411e5fbc1191a0a52ef"
  157. "f69f2445df4f9b17ad2b417be66c3710",
  158. .ciphertext =
  159. "8f346aaf64eaf24040720d80648c52e7"
  160. "aefc616be53ab1a3d301e69d91e01838"
  161. "ffd29f1bb5596ad94ea2d8e6196b7f09"
  162. "30d8ed0bf2773af36dd82a6280c20926",
  163. },
  164. #if defined(CONFIG_NETTLE) || defined(CONFIG_GCRYPT)
  165. {
  166. /* Borrowed from linux-kernel crypto/testmgr.h */
  167. .path = "/crypto/cipher/3des-cbc",
  168. .alg = QCRYPTO_CIPHER_ALG_3DES,
  169. .mode = QCRYPTO_CIPHER_MODE_CBC,
  170. .key =
  171. "e9c0ff2e760b6424444d995a12d640c0"
  172. "eac284e81495dbe8",
  173. .iv =
  174. "7d3388930f93b242",
  175. .plaintext =
  176. "6f54206f614d796e5320636565727374"
  177. "54206f6f4d206e612079655372637465"
  178. "20736f54206f614d796e532063656572"
  179. "737454206f6f4d206e61207965537263"
  180. "746520736f54206f614d796e53206365"
  181. "6572737454206f6f4d206e6120796553"
  182. "7263746520736f54206f614d796e5320"
  183. "63656572737454206f6f4d206e610a79",
  184. .ciphertext =
  185. "0e2db6973c5633f4671721c76e8ad549"
  186. "74b34905c51cd0ed12565c5396b6007d"
  187. "9048fcf58d2939cc8ad5351836234ed7"
  188. "76d1da0c9467bb048bf2036ca8cfb6ea"
  189. "226447aa8f7513bf9fc2c3f0c956c57a"
  190. "71632e897b1e12cae25fafd8a4f8c97a"
  191. "d6f92131624445a6d6bc5ad32d5443cc"
  192. "9ddea570e942458a6bfab19113b0d919",
  193. },
  194. {
  195. /* Borrowed from linux-kernel crypto/testmgr.h */
  196. .path = "/crypto/cipher/3des-ecb",
  197. .alg = QCRYPTO_CIPHER_ALG_3DES,
  198. .mode = QCRYPTO_CIPHER_MODE_ECB,
  199. .key =
  200. "0123456789abcdef5555555555555555"
  201. "fedcba9876543210",
  202. .plaintext =
  203. "736f6d6564617461",
  204. .ciphertext =
  205. "18d748e563620572",
  206. },
  207. {
  208. /* Borrowed from linux-kernel crypto/testmgr.h */
  209. .path = "/crypto/cipher/3des-ctr",
  210. .alg = QCRYPTO_CIPHER_ALG_3DES,
  211. .mode = QCRYPTO_CIPHER_MODE_CTR,
  212. .key =
  213. "9cd6f39cb95a67005a67002dceeb2dce"
  214. "ebb45172b451721f",
  215. .iv =
  216. "ffffffffffffffff",
  217. .plaintext =
  218. "05ec77fb42d559208b128669f05bcf56"
  219. "39ad349f66ea7dc448d3ba0db118e34a"
  220. "fe41285c278e11856cf75ec2553ca00b"
  221. "9265e970db4fd6b900b41fe649fd442f"
  222. "533a8d149863ca5dc1a833a70e9178ec"
  223. "77de42d5bc078b12e54cf05b22563980"
  224. "6b9f66c950c4af36ba0d947fe34add41"
  225. "28b31a8e11f843f75e21553c876e9265"
  226. "cc57dba235b900eb72e649d0442fb619"
  227. "8d14ff46ca5d24a8339a6d9178c377de"
  228. "a108bc07ee71e54cd75b22b51c806bf2"
  229. "45c9503baf369960947fc64adda40fb3"
  230. "1aed74f8432a5e218813876ef158cc57"
  231. "3ea2359c67eb72c549d0bb02b619e04b"
  232. "ff46295d248f169a6df45fc3aa3da108"
  233. "937aee71d84cd7be01b51ce74ef2452c"
  234. "503b82159960cb52c6a930a40f9679ed"
  235. "74df432abd048813fa4df15823573e81"
  236. "689c67ce51c5ac37bb02957ce04bd246"
  237. "29b01b8f16f940f45f26aa3d846f937a"
  238. "cd54d8a30abe01e873e74ed1452cb71e"
  239. "8215fc47cb5225a9309b629679c074df"
  240. "a609bd04ef76fa4dd458238a1d8168f3"
  241. "5ace5138ac379e61957cc74bd2a50cb0"
  242. "1be275f9402b5f268910846ff659cd54"
  243. "3fa30a9d64e873da4ed1b803b71ee148"
  244. "fc472e52258c179b62f55cc0ab32a609"
  245. "907bef76d94dd4bf068a1de44ff35a2d"
  246. "5138836a9e61c853c7ae31a50c977ee2"
  247. "75dc402bb2058910fb42f65920543f86"
  248. "699d64cf56daad34b803ea7de148d347",
  249. .ciphertext =
  250. "07c20820721f49ef19cd6f3253052215"
  251. "a2852bdb85d2d8b9dd0d1b45cb6911d4"
  252. "eabeb2455d0caebea0c127ac659f537e"
  253. "afc21bb5b86d360c25c0f86d0b2901da"
  254. "1378dc89121243faf612ef8d87627883"
  255. "e2be41204c6d351bd10c30cfe2de2b03"
  256. "bf4573d4e55995d1b39b276297bdde7f"
  257. "a4d23980aa5023f074883da86a18793b"
  258. "c4966c8d2240926ed6ad2a1fde63c0e7"
  259. "07f72df7b5f3f0cc017c2a9bc210caaa"
  260. "fd2b3fc5f3f6fc9b45db53e45bf3c97b"
  261. "8e52ffc802b8ac9da10039da3d2d0e01"
  262. "097d8d5ebe53b9b08ee7e2966ab278ea"
  263. "de238ba5fa5ce3dabf8e316a55d16ab2"
  264. "b5466fa5f0eeba1f9f98b0664fd03fa9"
  265. "df5f58c4f4ff755c403a097e6e1c97d4"
  266. "cce7e771cf0b150871fa0797cde6ca1d"
  267. "14280ccf99137af1ebfafa9207de1da1"
  268. "d33669fe514d9f2e83374f1f4830ed04"
  269. "4da4ef3aca76f41c418f6337782f86a6"
  270. "ef417ed2af88ab675271c38ef8269372"
  271. "aad60ee70b46b13ab408a9a8a0cf200c"
  272. "52bc8b0556b2bc319b74b92929969a50"
  273. "dc45dc1aeb0c64d4d3057e5955c3f490"
  274. "c2abf89b8adacea1c3f4ad77dd44c8ac"
  275. "a3f1c9d2195cb0caa234c1f76cfdac65"
  276. "32dc48c4f2006b77f17d76acc031632a"
  277. "a53a62c891b10365cb43d106dfc367bc"
  278. "dce0cd35ce4965a0527ba70d07a91bb0"
  279. "407772c2ea0e3a7846b991b6e73d5142"
  280. "fd51b0c62c6313785ceefccfc4700034",
  281. },
  282. #endif
  283. {
  284. /* RFC 2144, Appendix B.1 */
  285. .path = "/crypto/cipher/cast5-128",
  286. .alg = QCRYPTO_CIPHER_ALG_CAST5_128,
  287. .mode = QCRYPTO_CIPHER_MODE_ECB,
  288. .key = "0123456712345678234567893456789A",
  289. .plaintext = "0123456789abcdef",
  290. .ciphertext = "238b4fe5847e44b2",
  291. },
  292. {
  293. /* libgcrypt serpent.c */
  294. .path = "/crypto/cipher/serpent-128",
  295. .alg = QCRYPTO_CIPHER_ALG_SERPENT_128,
  296. .mode = QCRYPTO_CIPHER_MODE_ECB,
  297. .key = "00000000000000000000000000000000",
  298. .plaintext = "d29d576fcea3a3a7ed9099f29273d78e",
  299. .ciphertext = "b2288b968ae8b08648d1ce9606fd992d",
  300. },
  301. {
  302. /* libgcrypt serpent.c */
  303. .path = "/crypto/cipher/serpent-192",
  304. .alg = QCRYPTO_CIPHER_ALG_SERPENT_192,
  305. .mode = QCRYPTO_CIPHER_MODE_ECB,
  306. .key = "00000000000000000000000000000000"
  307. "0000000000000000",
  308. .plaintext = "d29d576fceaba3a7ed9899f2927bd78e",
  309. .ciphertext = "130e353e1037c22405e8faefb2c3c3e9",
  310. },
  311. {
  312. /* libgcrypt serpent.c */
  313. .path = "/crypto/cipher/serpent-256a",
  314. .alg = QCRYPTO_CIPHER_ALG_SERPENT_256,
  315. .mode = QCRYPTO_CIPHER_MODE_ECB,
  316. .key = "00000000000000000000000000000000"
  317. "00000000000000000000000000000000",
  318. .plaintext = "d095576fcea3e3a7ed98d9f29073d78e",
  319. .ciphertext = "b90ee5862de69168f2bdd5125b45472b",
  320. },
  321. {
  322. /* libgcrypt serpent.c */
  323. .path = "/crypto/cipher/serpent-256b",
  324. .alg = QCRYPTO_CIPHER_ALG_SERPENT_256,
  325. .mode = QCRYPTO_CIPHER_MODE_ECB,
  326. .key = "00000000000000000000000000000000"
  327. "00000000000000000000000000000000",
  328. .plaintext = "00000000010000000200000003000000",
  329. .ciphertext = "2061a42782bd52ec691ec383b03ba77c",
  330. },
  331. {
  332. /* Twofish paper "Known Answer Test" */
  333. .path = "/crypto/cipher/twofish-128",
  334. .alg = QCRYPTO_CIPHER_ALG_TWOFISH_128,
  335. .mode = QCRYPTO_CIPHER_MODE_ECB,
  336. .key = "d491db16e7b1c39e86cb086b789f5419",
  337. .plaintext = "019f9809de1711858faac3a3ba20fbc3",
  338. .ciphertext = "6363977de839486297e661c6c9d668eb",
  339. },
  340. {
  341. /* Twofish paper "Known Answer Test", I=3 */
  342. .path = "/crypto/cipher/twofish-192",
  343. .alg = QCRYPTO_CIPHER_ALG_TWOFISH_192,
  344. .mode = QCRYPTO_CIPHER_MODE_ECB,
  345. .key = "88b2b2706b105e36b446bb6d731a1e88"
  346. "efa71f788965bd44",
  347. .plaintext = "39da69d6ba4997d585b6dc073ca341b2",
  348. .ciphertext = "182b02d81497ea45f9daacdc29193a65",
  349. },
  350. {
  351. /* Twofish paper "Known Answer Test", I=4 */
  352. .path = "/crypto/cipher/twofish-256",
  353. .alg = QCRYPTO_CIPHER_ALG_TWOFISH_256,
  354. .mode = QCRYPTO_CIPHER_MODE_ECB,
  355. .key = "d43bb7556ea32e46f2a282b7d45b4e0d"
  356. "57ff739d4dc92c1bd7fc01700cc8216f",
  357. .plaintext = "90afe91bb288544f2c32dc239b2635e6",
  358. .ciphertext = "6cb4561c40bf0a9705931cb6d408e7fa",
  359. },
  360. {
  361. /* #1 32 byte key, 32 byte PTX */
  362. .path = "/crypto/cipher/aes-xts-128-1",
  363. .alg = QCRYPTO_CIPHER_ALG_AES_128,
  364. .mode = QCRYPTO_CIPHER_MODE_XTS,
  365. .key =
  366. "00000000000000000000000000000000"
  367. "00000000000000000000000000000000",
  368. .iv =
  369. "00000000000000000000000000000000",
  370. .plaintext =
  371. "00000000000000000000000000000000"
  372. "00000000000000000000000000000000",
  373. .ciphertext =
  374. "917cf69ebd68b2ec9b9fe9a3eadda692"
  375. "cd43d2f59598ed858c02c2652fbf922e",
  376. },
  377. {
  378. /* #2, 32 byte key, 32 byte PTX */
  379. .path = "/crypto/cipher/aes-xts-128-2",
  380. .alg = QCRYPTO_CIPHER_ALG_AES_128,
  381. .mode = QCRYPTO_CIPHER_MODE_XTS,
  382. .key =
  383. "11111111111111111111111111111111"
  384. "22222222222222222222222222222222",
  385. .iv =
  386. "33333333330000000000000000000000",
  387. .plaintext =
  388. "44444444444444444444444444444444"
  389. "44444444444444444444444444444444",
  390. .ciphertext =
  391. "c454185e6a16936e39334038acef838b"
  392. "fb186fff7480adc4289382ecd6d394f0",
  393. },
  394. {
  395. /* #5 from xts.7, 32 byte key, 32 byte PTX */
  396. .path = "/crypto/cipher/aes-xts-128-3",
  397. .alg = QCRYPTO_CIPHER_ALG_AES_128,
  398. .mode = QCRYPTO_CIPHER_MODE_XTS,
  399. .key =
  400. "fffefdfcfbfaf9f8f7f6f5f4f3f2f1f0"
  401. "bfbebdbcbbbab9b8b7b6b5b4b3b2b1b0",
  402. .iv =
  403. "9a785634120000000000000000000000",
  404. .plaintext =
  405. "44444444444444444444444444444444"
  406. "44444444444444444444444444444444",
  407. .ciphertext =
  408. "b01f86f8edc1863706fa8a4253e34f28"
  409. "af319de38334870f4dd1f94cbe9832f1",
  410. },
  411. {
  412. /* #4, 32 byte key, 512 byte PTX */
  413. .path = "/crypto/cipher/aes-xts-128-4",
  414. .alg = QCRYPTO_CIPHER_ALG_AES_128,
  415. .mode = QCRYPTO_CIPHER_MODE_XTS,
  416. .key =
  417. "27182818284590452353602874713526"
  418. "31415926535897932384626433832795",
  419. .iv =
  420. "00000000000000000000000000000000",
  421. .plaintext =
  422. "000102030405060708090a0b0c0d0e0f"
  423. "101112131415161718191a1b1c1d1e1f"
  424. "202122232425262728292a2b2c2d2e2f"
  425. "303132333435363738393a3b3c3d3e3f"
  426. "404142434445464748494a4b4c4d4e4f"
  427. "505152535455565758595a5b5c5d5e5f"
  428. "606162636465666768696a6b6c6d6e6f"
  429. "707172737475767778797a7b7c7d7e7f"
  430. "808182838485868788898a8b8c8d8e8f"
  431. "909192939495969798999a9b9c9d9e9f"
  432. "a0a1a2a3a4a5a6a7a8a9aaabacadaeaf"
  433. "b0b1b2b3b4b5b6b7b8b9babbbcbdbebf"
  434. "c0c1c2c3c4c5c6c7c8c9cacbcccdcecf"
  435. "d0d1d2d3d4d5d6d7d8d9dadbdcdddedf"
  436. "e0e1e2e3e4e5e6e7e8e9eaebecedeeef"
  437. "f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff"
  438. "000102030405060708090a0b0c0d0e0f"
  439. "101112131415161718191a1b1c1d1e1f"
  440. "202122232425262728292a2b2c2d2e2f"
  441. "303132333435363738393a3b3c3d3e3f"
  442. "404142434445464748494a4b4c4d4e4f"
  443. "505152535455565758595a5b5c5d5e5f"
  444. "606162636465666768696a6b6c6d6e6f"
  445. "707172737475767778797a7b7c7d7e7f"
  446. "808182838485868788898a8b8c8d8e8f"
  447. "909192939495969798999a9b9c9d9e9f"
  448. "a0a1a2a3a4a5a6a7a8a9aaabacadaeaf"
  449. "b0b1b2b3b4b5b6b7b8b9babbbcbdbebf"
  450. "c0c1c2c3c4c5c6c7c8c9cacbcccdcecf"
  451. "d0d1d2d3d4d5d6d7d8d9dadbdcdddedf"
  452. "e0e1e2e3e4e5e6e7e8e9eaebecedeeef"
  453. "f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff",
  454. .ciphertext =
  455. "27a7479befa1d476489f308cd4cfa6e2"
  456. "a96e4bbe3208ff25287dd3819616e89c"
  457. "c78cf7f5e543445f8333d8fa7f560000"
  458. "05279fa5d8b5e4ad40e736ddb4d35412"
  459. "328063fd2aab53e5ea1e0a9f332500a5"
  460. "df9487d07a5c92cc512c8866c7e860ce"
  461. "93fdf166a24912b422976146ae20ce84"
  462. "6bb7dc9ba94a767aaef20c0d61ad0265"
  463. "5ea92dc4c4e41a8952c651d33174be51"
  464. "a10c421110e6d81588ede82103a252d8"
  465. "a750e8768defffed9122810aaeb99f91"
  466. "72af82b604dc4b8e51bcb08235a6f434"
  467. "1332e4ca60482a4ba1a03b3e65008fc5"
  468. "da76b70bf1690db4eae29c5f1badd03c"
  469. "5ccf2a55d705ddcd86d449511ceb7ec3"
  470. "0bf12b1fa35b913f9f747a8afd1b130e"
  471. "94bff94effd01a91735ca1726acd0b19"
  472. "7c4e5b03393697e126826fb6bbde8ecc"
  473. "1e08298516e2c9ed03ff3c1b7860f6de"
  474. "76d4cecd94c8119855ef5297ca67e9f3"
  475. "e7ff72b1e99785ca0a7e7720c5b36dc6"
  476. "d72cac9574c8cbbc2f801e23e56fd344"
  477. "b07f22154beba0f08ce8891e643ed995"
  478. "c94d9a69c9f1b5f499027a78572aeebd"
  479. "74d20cc39881c213ee770b1010e4bea7"
  480. "18846977ae119f7a023ab58cca0ad752"
  481. "afe656bb3c17256a9f6e9bf19fdd5a38"
  482. "fc82bbe872c5539edb609ef4f79c203e"
  483. "bb140f2e583cb2ad15b4aa5b655016a8"
  484. "449277dbd477ef2c8d6c017db738b18d"
  485. "eb4a427d1923ce3ff262735779a418f2"
  486. "0a282df920147beabe421ee5319d0568",
  487. },
  488. {
  489. /* Bad config - cast5-128 has 8 byte block size
  490. * which is incompatible with XTS
  491. */
  492. .path = "/crypto/cipher/cast5-xts-128",
  493. .alg = QCRYPTO_CIPHER_ALG_CAST5_128,
  494. .mode = QCRYPTO_CIPHER_MODE_XTS,
  495. .key =
  496. "27182818284590452353602874713526"
  497. "31415926535897932384626433832795",
  498. },
  499. {
  500. /* NIST F.5.1 CTR-AES128.Encrypt */
  501. .path = "/crypto/cipher/aes-ctr-128",
  502. .alg = QCRYPTO_CIPHER_ALG_AES_128,
  503. .mode = QCRYPTO_CIPHER_MODE_CTR,
  504. .key = "2b7e151628aed2a6abf7158809cf4f3c",
  505. .iv = "f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff",
  506. .plaintext =
  507. "6bc1bee22e409f96e93d7e117393172a"
  508. "ae2d8a571e03ac9c9eb76fac45af8e51"
  509. "30c81c46a35ce411e5fbc1191a0a52ef"
  510. "f69f2445df4f9b17ad2b417be66c3710",
  511. .ciphertext =
  512. "874d6191b620e3261bef6864990db6ce"
  513. "9806f66b7970fdff8617187bb9fffdff"
  514. "5ae4df3edbd5d35e5b4f09020db03eab"
  515. "1e031dda2fbe03d1792170a0f3009cee",
  516. },
  517. {
  518. /* NIST F.5.3 CTR-AES192.Encrypt */
  519. .path = "/crypto/cipher/aes-ctr-192",
  520. .alg = QCRYPTO_CIPHER_ALG_AES_192,
  521. .mode = QCRYPTO_CIPHER_MODE_CTR,
  522. .key = "8e73b0f7da0e6452c810f32b809079e562f8ead2522c6b7b",
  523. .iv = "f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff",
  524. .plaintext =
  525. "6bc1bee22e409f96e93d7e117393172a"
  526. "ae2d8a571e03ac9c9eb76fac45af8e51"
  527. "30c81c46a35ce411e5fbc1191a0a52ef"
  528. "f69f2445df4f9b17ad2b417be66c3710",
  529. .ciphertext =
  530. "1abc932417521ca24f2b0459fe7e6e0b"
  531. "090339ec0aa6faefd5ccc2c6f4ce8e94"
  532. "1e36b26bd1ebc670d1bd1d665620abf7"
  533. "4f78a7f6d29809585a97daec58c6b050",
  534. },
  535. {
  536. /* NIST F.5.5 CTR-AES256.Encrypt */
  537. .path = "/crypto/cipher/aes-ctr-256",
  538. .alg = QCRYPTO_CIPHER_ALG_AES_256,
  539. .mode = QCRYPTO_CIPHER_MODE_CTR,
  540. .key = "603deb1015ca71be2b73aef0857d7781"
  541. "1f352c073b6108d72d9810a30914dff4",
  542. .iv = "f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff",
  543. .plaintext =
  544. "6bc1bee22e409f96e93d7e117393172a"
  545. "ae2d8a571e03ac9c9eb76fac45af8e51"
  546. "30c81c46a35ce411e5fbc1191a0a52ef"
  547. "f69f2445df4f9b17ad2b417be66c3710",
  548. .ciphertext =
  549. "601ec313775789a5b7a7f504bbf3d228"
  550. "f443e3ca4d62b59aca84e990cacaf5c5"
  551. "2b0930daa23de94ce87017ba2d84988d"
  552. "dfc9c58db67aada613c2dd08457941a6",
  553. }
  554. };
  555. static inline int unhex(char c)
  556. {
  557. if (c >= 'a' && c <= 'f') {
  558. return 10 + (c - 'a');
  559. }
  560. if (c >= 'A' && c <= 'F') {
  561. return 10 + (c - 'A');
  562. }
  563. return c - '0';
  564. }
  565. static inline char hex(int i)
  566. {
  567. if (i < 10) {
  568. return '0' + i;
  569. }
  570. return 'a' + (i - 10);
  571. }
  572. static size_t unhex_string(const char *hexstr,
  573. uint8_t **data)
  574. {
  575. size_t len;
  576. size_t i;
  577. if (!hexstr) {
  578. *data = NULL;
  579. return 0;
  580. }
  581. len = strlen(hexstr);
  582. *data = g_new0(uint8_t, len / 2);
  583. for (i = 0; i < len; i += 2) {
  584. (*data)[i/2] = (unhex(hexstr[i]) << 4) | unhex(hexstr[i+1]);
  585. }
  586. return len / 2;
  587. }
  588. static char *hex_string(const uint8_t *bytes,
  589. size_t len)
  590. {
  591. char *hexstr = g_new0(char, len * 2 + 1);
  592. size_t i;
  593. for (i = 0; i < len; i++) {
  594. hexstr[i*2] = hex((bytes[i] >> 4) & 0xf);
  595. hexstr[i*2+1] = hex(bytes[i] & 0xf);
  596. }
  597. hexstr[len*2] = '\0';
  598. return hexstr;
  599. }
  600. static void test_cipher(const void *opaque)
  601. {
  602. const QCryptoCipherTestData *data = opaque;
  603. QCryptoCipher *cipher;
  604. uint8_t *key, *iv = NULL, *ciphertext = NULL,
  605. *plaintext = NULL, *outtext = NULL;
  606. size_t nkey, niv = 0, nciphertext = 0, nplaintext = 0;
  607. char *outtexthex = NULL;
  608. size_t ivsize, keysize, blocksize;
  609. Error *err = NULL;
  610. nkey = unhex_string(data->key, &key);
  611. if (data->iv) {
  612. niv = unhex_string(data->iv, &iv);
  613. }
  614. if (data->ciphertext) {
  615. nciphertext = unhex_string(data->ciphertext, &ciphertext);
  616. }
  617. if (data->plaintext) {
  618. nplaintext = unhex_string(data->plaintext, &plaintext);
  619. }
  620. g_assert(nciphertext == nplaintext);
  621. outtext = g_new0(uint8_t, nciphertext);
  622. cipher = qcrypto_cipher_new(
  623. data->alg, data->mode,
  624. key, nkey,
  625. &err);
  626. if (data->plaintext) {
  627. g_assert(err == NULL);
  628. g_assert(cipher != NULL);
  629. } else {
  630. error_free_or_abort(&err);
  631. g_assert(cipher == NULL);
  632. goto cleanup;
  633. }
  634. keysize = qcrypto_cipher_get_key_len(data->alg);
  635. blocksize = qcrypto_cipher_get_block_len(data->alg);
  636. ivsize = qcrypto_cipher_get_iv_len(data->alg, data->mode);
  637. if (data->mode == QCRYPTO_CIPHER_MODE_XTS) {
  638. g_assert_cmpint(keysize * 2, ==, nkey);
  639. } else {
  640. g_assert_cmpint(keysize, ==, nkey);
  641. }
  642. g_assert_cmpint(ivsize, ==, niv);
  643. if (niv) {
  644. g_assert_cmpint(blocksize, ==, niv);
  645. }
  646. if (iv) {
  647. g_assert(qcrypto_cipher_setiv(cipher,
  648. iv, niv,
  649. &error_abort) == 0);
  650. }
  651. g_assert(qcrypto_cipher_encrypt(cipher,
  652. plaintext,
  653. outtext,
  654. nplaintext,
  655. &error_abort) == 0);
  656. outtexthex = hex_string(outtext, nciphertext);
  657. g_assert_cmpstr(outtexthex, ==, data->ciphertext);
  658. g_free(outtexthex);
  659. if (iv) {
  660. g_assert(qcrypto_cipher_setiv(cipher,
  661. iv, niv,
  662. &error_abort) == 0);
  663. }
  664. g_assert(qcrypto_cipher_decrypt(cipher,
  665. ciphertext,
  666. outtext,
  667. nplaintext,
  668. &error_abort) == 0);
  669. outtexthex = hex_string(outtext, nplaintext);
  670. g_assert_cmpstr(outtexthex, ==, data->plaintext);
  671. cleanup:
  672. g_free(outtext);
  673. g_free(outtexthex);
  674. g_free(key);
  675. g_free(iv);
  676. g_free(ciphertext);
  677. g_free(plaintext);
  678. qcrypto_cipher_free(cipher);
  679. }
  680. static void test_cipher_null_iv(void)
  681. {
  682. QCryptoCipher *cipher;
  683. uint8_t key[32] = { 0 };
  684. uint8_t plaintext[32] = { 0 };
  685. uint8_t ciphertext[32] = { 0 };
  686. cipher = qcrypto_cipher_new(
  687. QCRYPTO_CIPHER_ALG_AES_256,
  688. QCRYPTO_CIPHER_MODE_CBC,
  689. key, sizeof(key),
  690. &error_abort);
  691. g_assert(cipher != NULL);
  692. /* Don't call qcrypto_cipher_setiv */
  693. qcrypto_cipher_encrypt(cipher,
  694. plaintext,
  695. ciphertext,
  696. sizeof(plaintext),
  697. &error_abort);
  698. qcrypto_cipher_free(cipher);
  699. }
  700. static void test_cipher_short_plaintext(void)
  701. {
  702. Error *err = NULL;
  703. QCryptoCipher *cipher;
  704. uint8_t key[32] = { 0 };
  705. uint8_t plaintext1[20] = { 0 };
  706. uint8_t ciphertext1[20] = { 0 };
  707. uint8_t plaintext2[40] = { 0 };
  708. uint8_t ciphertext2[40] = { 0 };
  709. int ret;
  710. cipher = qcrypto_cipher_new(
  711. QCRYPTO_CIPHER_ALG_AES_256,
  712. QCRYPTO_CIPHER_MODE_CBC,
  713. key, sizeof(key),
  714. &error_abort);
  715. g_assert(cipher != NULL);
  716. /* Should report an error as plaintext is shorter
  717. * than block size
  718. */
  719. ret = qcrypto_cipher_encrypt(cipher,
  720. plaintext1,
  721. ciphertext1,
  722. sizeof(plaintext1),
  723. &err);
  724. g_assert(ret == -1);
  725. g_assert(err != NULL);
  726. error_free(err);
  727. err = NULL;
  728. /* Should report an error as plaintext is larger than
  729. * block size, but not a multiple of block size
  730. */
  731. ret = qcrypto_cipher_encrypt(cipher,
  732. plaintext2,
  733. ciphertext2,
  734. sizeof(plaintext2),
  735. &err);
  736. g_assert(ret == -1);
  737. g_assert(err != NULL);
  738. error_free(err);
  739. qcrypto_cipher_free(cipher);
  740. }
  741. int main(int argc, char **argv)
  742. {
  743. size_t i;
  744. g_test_init(&argc, &argv, NULL);
  745. g_assert(qcrypto_init(NULL) == 0);
  746. for (i = 0; i < G_N_ELEMENTS(test_data); i++) {
  747. if (qcrypto_cipher_supports(test_data[i].alg, test_data[i].mode)) {
  748. g_test_add_data_func(test_data[i].path, &test_data[i], test_cipher);
  749. }
  750. }
  751. g_test_add_func("/crypto/cipher/null-iv",
  752. test_cipher_null_iv);
  753. g_test_add_func("/crypto/cipher/short-plaintext",
  754. test_cipher_short_plaintext);
  755. return g_test_run();
  756. }