niap.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*
  2. * NIAP FPT_TST_EXT.1 cryptographic self-tests.
  3. *
  4. * Copyright (c) 2019 Google LLC.
  5. */
  6. #include <crypto/hash.h>
  7. #include <crypto/skcipher.h>
  8. #include <linux/crypto.h>
  9. #include <linux/module.h>
  10. #include <linux/reboot.h>
  11. #include <linux/scatterlist.h>
  12. static const u8 niap_message[] __initconst = {
  13. 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x00,
  14. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  15. };
  16. static const u8 niap_hmac_sha512_key[] __initconst = {
  17. 0x84, 0xe1, 0xb6, 0x59, 0x69, 0x4f, 0x5e, 0xa5,
  18. 0x07, 0xcc, 0x74, 0x06, 0xb8, 0xec, 0x53, 0x4a
  19. };
  20. static const u8 niap_hmac_sha512_output[] __initconst = {
  21. 0x72, 0x74, 0x73, 0x5e, 0xa5, 0x1d, 0x1c, 0xba,
  22. 0x62, 0xa7, 0x93, 0xbb, 0xa9, 0xc3, 0x97, 0x86,
  23. 0x9d, 0x46, 0x26, 0xe7, 0x1b, 0x8c, 0x73, 0xf7,
  24. 0x1a, 0xcb, 0xae, 0xda, 0x32, 0x77, 0x2d, 0x9b,
  25. 0x73, 0xa1, 0xb1, 0x21, 0x16, 0x00, 0x38, 0xc8,
  26. 0x61, 0x9c, 0xb9, 0x7f, 0x3c, 0x3b, 0x16, 0xe1,
  27. 0xb8, 0x1d, 0x34, 0x8c, 0x82, 0xdc, 0xe6, 0xe3,
  28. 0xc6, 0xaa, 0x4c, 0x10, 0x54, 0x3c, 0x09, 0xf4
  29. };
  30. static const u8 niap_sha256_output[] __initconst = {
  31. 0xa0, 0x90, 0x1c, 0xb8, 0xb4, 0x22, 0xa3, 0xef,
  32. 0x7d, 0x34, 0x45, 0x2d, 0x73, 0x32, 0x6c, 0x1e,
  33. 0x09, 0x2d, 0x9e, 0x82, 0xee, 0x3f, 0x32, 0xd3,
  34. 0xe8, 0x64, 0x30, 0x50, 0xb0, 0x2e, 0x7d, 0xf4
  35. };
  36. static const u8 niap_cbc_aes128_encrypt_key[] __initconst = {
  37. 0x6f, 0x1e, 0x97, 0x12, 0xcf, 0x82, 0x40, 0x79,
  38. 0x2c, 0x4f, 0x8a, 0x82, 0x78, 0x2a, 0x8c, 0xdd
  39. };
  40. static const u8 niap_cbc_aes128_decrypt_key[] __initconst = {
  41. 0x02, 0x52, 0x38, 0x55, 0xa6, 0x88, 0x51, 0xe2,
  42. 0xd0, 0x16, 0xaa, 0x56, 0x95, 0x64, 0x33, 0x2d
  43. };
  44. static const u8 niap_cbc_aes128_iv[] __initconst = {
  45. 0x0c, 0xa7, 0x8b, 0xd2, 0x55, 0x73, 0x85, 0xef,
  46. 0xe2, 0x1b, 0x96, 0xb1, 0x0f, 0x14, 0xec, 0xf0
  47. };
  48. static const u8 niap_cbc_aes128_encrypt_output[] __initconst = {
  49. 0xd1, 0x4f, 0xe0, 0x79, 0xa1, 0x68, 0xe4, 0xa4,
  50. 0x51, 0x96, 0xf6, 0x10, 0x5d, 0x6a, 0x63, 0xa0
  51. };
  52. static const u8 niap_cbc_aes128_decrypt_output[] __initconst = {
  53. 0x59, 0x39, 0x9b, 0xb7, 0x7d, 0xef, 0x86, 0x7b,
  54. 0x0d, 0xe0, 0x77, 0x90, 0x17, 0x7c, 0x2a, 0xdb
  55. };
  56. struct niap_test {
  57. const char *alg;
  58. const u8 *key;
  59. unsigned int key_length;
  60. const u8 *iv;
  61. unsigned int iv_length;
  62. const u8 *input;
  63. unsigned int in_length;
  64. const u8 *output;
  65. unsigned int out_length;
  66. bool encrypt;
  67. int (*func)(const struct niap_test *);
  68. };
  69. static inline void dump(const u8 *buf, unsigned int length)
  70. {
  71. print_hex_dump(KERN_ERR, "", DUMP_PREFIX_NONE, 16, 1, buf, length,
  72. false);
  73. }
  74. static int __init niap_test_cipher(const struct niap_test *test)
  75. {
  76. int err = 0;
  77. DECLARE_CRYPTO_WAIT(wait);
  78. struct crypto_skcipher *tfm = NULL;
  79. struct skcipher_request *req = NULL;
  80. struct scatterlist src;
  81. struct scatterlist dst;
  82. u8 *input = NULL;
  83. u8 *iv = NULL;
  84. u8 *output = NULL;
  85. input = kmemdup(test->input, test->in_length, GFP_KERNEL);
  86. iv = kmemdup(test->iv, test->iv_length, GFP_KERNEL);
  87. output = kzalloc(test->out_length, GFP_KERNEL);
  88. if (!input || !iv || !output) {
  89. err = -ENOMEM;
  90. goto out;
  91. }
  92. tfm = crypto_alloc_skcipher(test->alg, 0, 0);
  93. if (IS_ERR(tfm)) {
  94. err = PTR_ERR(tfm);
  95. tfm = NULL;
  96. goto out;
  97. }
  98. err = crypto_skcipher_setkey(tfm, test->key, test->key_length);
  99. if (err)
  100. goto out;
  101. req = skcipher_request_alloc(tfm, GFP_KERNEL);
  102. if (!req) {
  103. err = -ENOMEM;
  104. goto out;
  105. }
  106. sg_init_one(&src, input, test->in_length);
  107. sg_init_one(&dst, output, test->out_length);
  108. skcipher_request_set_tfm(req, tfm);
  109. skcipher_request_set_callback(req,
  110. CRYPTO_TFM_REQ_MAY_SLEEP | CRYPTO_TFM_REQ_MAY_BACKLOG,
  111. crypto_req_done, &wait);
  112. skcipher_request_set_crypt(req, &src, &dst, test->in_length, iv);
  113. if (test->encrypt)
  114. err = crypto_wait_req(crypto_skcipher_encrypt(req), &wait);
  115. else
  116. err = crypto_wait_req(crypto_skcipher_decrypt(req), &wait);
  117. if (err)
  118. goto out;
  119. if (memcmp(output, test->output, test->out_length)) {
  120. pr_err("niap_test_cipher: invalid result:\n");
  121. dump(output, test->out_length);
  122. err = -EBADMSG;
  123. }
  124. out:
  125. if (err)
  126. pr_err("niap_test_cipher: %s %s failed: %d\n", test->alg,
  127. test->encrypt ? "encryption" : "decryption", err);
  128. else
  129. pr_info("niap_test_cipher: %s %s passed\n", test->alg,
  130. test->encrypt ? "encryption" : "decryption");
  131. skcipher_request_free(req);
  132. crypto_free_skcipher(tfm);
  133. kfree(input);
  134. kfree(iv);
  135. kfree(output);
  136. return err;
  137. }
  138. static int __init niap_test_hash(const struct niap_test *test)
  139. {
  140. int err = 0;
  141. struct crypto_shash *tfm = NULL;
  142. struct shash_desc *desc = NULL;
  143. u8 *output = NULL;
  144. output = kzalloc(test->out_length, GFP_KERNEL);
  145. if (!output) {
  146. err = -ENOMEM;
  147. goto out;
  148. }
  149. tfm = crypto_alloc_shash(test->alg, 0, 0);
  150. if (IS_ERR(tfm)) {
  151. err = PTR_ERR(tfm);
  152. tfm = NULL;
  153. goto out;
  154. }
  155. if (test->key) {
  156. err = crypto_shash_setkey(tfm, test->key, test->key_length);
  157. if (err)
  158. goto out;
  159. }
  160. desc = kzalloc(sizeof(*desc) + crypto_shash_descsize(tfm), GFP_KERNEL);
  161. if (!desc) {
  162. err = -ENOMEM;
  163. goto out;
  164. }
  165. desc->tfm = tfm;
  166. desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
  167. err = crypto_shash_digest(desc, test->input, test->in_length, output);
  168. if (err)
  169. goto out;
  170. if (memcmp(output, test->output, test->out_length)) {
  171. pr_err("niap_test_hash: invalid result:\n");
  172. dump(output, test->out_length);
  173. err = -EBADMSG;
  174. }
  175. out:
  176. if (err)
  177. pr_err("niap_test_hash: %s failed: %d\n", test->alg, err);
  178. else
  179. pr_info("niap_test_hash: %s passed\n", test->alg);
  180. crypto_free_shash(tfm);
  181. kfree(desc);
  182. kfree(output);
  183. return err;
  184. }
  185. static const struct niap_test niap_tests[] __initconst = {
  186. {
  187. .alg = "hmac(sha512)",
  188. .key = niap_hmac_sha512_key,
  189. .key_length = sizeof(niap_hmac_sha512_key),
  190. .input = niap_message,
  191. .in_length = sizeof(niap_message),
  192. .output = niap_hmac_sha512_output,
  193. .out_length = sizeof(niap_hmac_sha512_output),
  194. .func = niap_test_hash,
  195. }, {
  196. .alg = "sha256",
  197. .key = NULL,
  198. .input = niap_message,
  199. .in_length = sizeof(niap_message),
  200. .output = niap_sha256_output,
  201. .out_length = sizeof(niap_sha256_output),
  202. .func = niap_test_hash,
  203. }, {
  204. .alg = "cbc(aes)",
  205. .key = niap_cbc_aes128_encrypt_key,
  206. .key_length = sizeof(niap_cbc_aes128_encrypt_key),
  207. .input = niap_message,
  208. .in_length = sizeof(niap_message),
  209. .iv = niap_cbc_aes128_iv,
  210. .iv_length = sizeof(niap_cbc_aes128_iv),
  211. .output = niap_cbc_aes128_encrypt_output,
  212. .out_length = sizeof(niap_cbc_aes128_encrypt_output),
  213. .encrypt = true,
  214. .func = niap_test_cipher,
  215. }, {
  216. .alg = "cbc(aes)",
  217. .key = niap_cbc_aes128_decrypt_key,
  218. .key_length = sizeof(niap_cbc_aes128_decrypt_key),
  219. .input = niap_message,
  220. .in_length = sizeof(niap_message),
  221. .iv = niap_cbc_aes128_iv,
  222. .iv_length = sizeof(niap_cbc_aes128_iv),
  223. .output = niap_cbc_aes128_decrypt_output,
  224. .out_length = sizeof(niap_cbc_aes128_decrypt_output),
  225. .encrypt = false,
  226. .func = niap_test_cipher,
  227. }
  228. };
  229. static int __init niap_init(void)
  230. {
  231. int i;
  232. for (i = 0; i < ARRAY_SIZE(niap_tests); i++) {
  233. const struct niap_test *test = &niap_tests[i];
  234. if (test->func(test)) {
  235. pr_crit("niap: self-test failed; rebooting\n");
  236. kernel_restart("bootloader");
  237. }
  238. }
  239. return 0;
  240. }
  241. late_initcall(niap_init);
  242. MODULE_LICENSE("GPL");
  243. MODULE_DESCRIPTION("NIAP FPT_TST_EXT.1 cryptographic self-tests");