dmesg_dumper_crypto.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved.
  4. */
  5. #define pr_fmt(fmt) "dmesg_dumper_crypto: " fmt
  6. #include <crypto/aead.h>
  7. #include <crypto/akcipher.h>
  8. #include <linux/kmsg_dump.h>
  9. #include <linux/of.h>
  10. #include <linux/random.h>
  11. #include <linux/scatterlist.h>
  12. #include <linux/sizes.h>
  13. #include "dmesg_dumper_private.h"
  14. struct pubkey_data {
  15. u8 *pubkey;
  16. u32 pubkeysize;
  17. };
  18. static struct pubkey_data *input_data;
  19. static bool init_done;
  20. static int qcom_ddump_encrypt_key_with_rsa(u8 *nonce, u64 nonce_size,
  21. struct encrypt_data *output_data)
  22. {
  23. struct scatterlist sg_cipher;
  24. struct akcipher_request *req;
  25. struct scatterlist sg_plain;
  26. struct crypto_akcipher *tfm;
  27. DECLARE_CRYPTO_WAIT(wait);
  28. u64 ciphersize;
  29. u64 plainsize;
  30. u8 *cipher;
  31. u8 *plain;
  32. u8 *key;
  33. int err;
  34. plainsize = nonce_size;
  35. ciphersize = AES_256_ENCRYPTED_KEY_SIZE;
  36. key = kmemdup(input_data->pubkey, input_data->pubkeysize, GFP_KERNEL);
  37. if (!key) {
  38. err = -ENOMEM;
  39. return err;
  40. }
  41. plain = kmemdup(nonce, nonce_size, GFP_KERNEL);
  42. if (!plain) {
  43. err = -ENOMEM;
  44. goto free_key;
  45. }
  46. sg_init_one(&sg_plain, plain, plainsize);
  47. cipher = kzalloc(ciphersize, GFP_KERNEL);
  48. if (!cipher) {
  49. err = -ENOMEM;
  50. goto free_plain;
  51. }
  52. sg_init_one(&sg_cipher, cipher, ciphersize);
  53. tfm = crypto_alloc_akcipher("rsa", 0, 0);
  54. if (IS_ERR(tfm)) {
  55. err = PTR_ERR(tfm);
  56. pr_err("Error allocating rsa handle: %ld\n", PTR_ERR(tfm));
  57. goto free_cipher;
  58. }
  59. req = akcipher_request_alloc(tfm, GFP_KERNEL);
  60. if (!req) {
  61. err = -ENOMEM;
  62. goto free_akcipher;
  63. }
  64. /* use public key to encrypt */
  65. err = crypto_akcipher_set_pub_key(tfm, key, input_data->pubkeysize);
  66. if (err) {
  67. pr_err("Error setting public key: %d\n", err);
  68. goto free_akcipher_request;
  69. }
  70. akcipher_request_set_crypt(req, &sg_plain, &sg_cipher, plainsize, ciphersize);
  71. akcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
  72. crypto_req_done, &wait);
  73. /* Wait for encryption result */
  74. err = crypto_wait_req(crypto_akcipher_encrypt(req), &wait);
  75. if (err) {
  76. pr_err("Error rsa encrypting: %d\n", err);
  77. goto free_akcipher_request;
  78. }
  79. memcpy(output_data->key, cipher, AES_256_ENCRYPTED_KEY_SIZE);
  80. free_akcipher_request:
  81. akcipher_request_free(req);
  82. req = NULL;
  83. free_akcipher:
  84. crypto_free_akcipher(tfm);
  85. tfm = NULL;
  86. free_cipher:
  87. kfree(cipher);
  88. cipher = NULL;
  89. free_plain:
  90. memset(plain, 0, plainsize);
  91. kfree(plain);
  92. plain = NULL;
  93. free_key:
  94. kfree(key);
  95. key = NULL;
  96. return err;
  97. }
  98. static int qcom_ddump_encrypt_log_with_gcm(u8 *log, u64 log_size,
  99. u8 *key, u32 key_size, struct encrypt_data *output_data)
  100. {
  101. struct scatterlist sg_cipher;
  102. struct scatterlist sg_plain;
  103. DECLARE_CRYPTO_WAIT(wait);
  104. struct aead_request *req;
  105. struct crypto_aead *tfm;
  106. u64 ciphersize;
  107. u64 plainsize;
  108. u8 iv[IV_LEN];
  109. u8 *cipher;
  110. u8 *plain;
  111. int err;
  112. plain = log;
  113. plainsize = log_size;
  114. ciphersize = plainsize + TAG_LEN;
  115. /* Get random iv */
  116. get_random_bytes(iv, IV_LEN);
  117. /* crypto api needs scatterlist as input */
  118. sg_init_one(&sg_plain, plain, plainsize);
  119. cipher = kzalloc(ciphersize, GFP_KERNEL);
  120. if (!cipher) {
  121. err = -ENOMEM;
  122. return err;
  123. }
  124. sg_init_one(&sg_cipher, cipher, ciphersize);
  125. /* Set aead crypto environment */
  126. tfm = crypto_alloc_aead("gcm(aes)", 0, 0);
  127. if (IS_ERR(tfm)) {
  128. pr_err("Error allocating gcm(aes) handle: %ld\n", PTR_ERR(tfm));
  129. err = PTR_ERR(tfm);
  130. goto free_cipher;
  131. }
  132. req = aead_request_alloc(tfm, GFP_KERNEL);
  133. if (!req) {
  134. err = -ENOMEM;
  135. goto free_akcipher;
  136. }
  137. err = crypto_aead_setkey(tfm, key, key_size);
  138. if (err) {
  139. pr_err("Error setting key: %d\n", err);
  140. goto free_akcipher_request;
  141. }
  142. err = crypto_aead_setauthsize(tfm, TAG_LEN);
  143. if (err) {
  144. pr_err("Error setting authsize: %d\n", err);
  145. goto free_akcipher_request;
  146. }
  147. /* Encrypt plain and get cipher */
  148. aead_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
  149. crypto_req_done, &wait);
  150. aead_request_set_crypt(req, &sg_plain, &sg_cipher, plainsize, iv);
  151. /* Wait for encryption result */
  152. err = crypto_wait_req(crypto_aead_encrypt(req), &wait);
  153. if (err) {
  154. pr_err("Error encrypting: %d\n", err);
  155. goto free_akcipher_request;
  156. }
  157. memcpy(output_data->iv, iv, IV_LEN);
  158. memcpy(output_data->tag, cipher + plainsize, TAG_LEN);
  159. memcpy(output_data->cipher_log, cipher, ciphersize);
  160. free_akcipher_request:
  161. aead_request_free(req);
  162. req = NULL;
  163. free_akcipher:
  164. crypto_free_aead(tfm);
  165. tfm = NULL;
  166. free_cipher:
  167. kfree(cipher);
  168. cipher = NULL;
  169. return err;
  170. }
  171. static int qcom_ddump_do_alive_log_encrypt(u8 *log, u64 log_size,
  172. struct encrypt_data *output_data)
  173. {
  174. u8 key[KEY_LEN];
  175. int ret;
  176. /* Get random key */
  177. get_random_bytes(key, KEY_LEN);
  178. ret = qcom_ddump_encrypt_key_with_rsa(key, KEY_LEN, output_data);
  179. if (ret) {
  180. pr_err("Error encrypt key with rsa: %d\n", ret);
  181. return ret;
  182. }
  183. ret = qcom_ddump_encrypt_log_with_gcm(log, log_size, key, KEY_LEN, output_data);
  184. if (ret) {
  185. pr_err("Error encrypt log with gcm: %d\n", ret);
  186. return ret;
  187. }
  188. output_data->frame_size = sizeof(*output_data) + log_size + TAG_LEN;
  189. return ret;
  190. }
  191. int qcom_ddump_alive_log_to_shm(struct qcom_dmesg_dumper *qdd,
  192. u64 user_size)
  193. {
  194. size_t total_len, line_len;
  195. struct ddump_shm_hdr *hdr;
  196. u64 valid_size;
  197. void *buf;
  198. void *base;
  199. int ret;
  200. if (!init_done) {
  201. pr_err("%s: Driver probe failed\n", __func__);
  202. return -ENXIO;
  203. }
  204. if (!qdd)
  205. return -EINVAL;
  206. total_len = 0;
  207. hdr = qdd->base;
  208. hdr->svm_dump_len = 0;
  209. valid_size = qcom_ddump_get_valid_size(qdd, user_size);
  210. if (valid_size < LOG_LINE_MAX)
  211. return -EINVAL;
  212. buf = vzalloc(valid_size + ALIGN_LEN);
  213. if (!buf)
  214. return -ENOMEM;
  215. base = buf;
  216. while ((total_len < valid_size - LOG_LINE_MAX) &&
  217. kmsg_dump_get_line(&qdd->iter, false, base, valid_size - total_len, &line_len) &&
  218. (line_len > 0)) {
  219. base = base + line_len;
  220. total_len = total_len + line_len;
  221. }
  222. if (total_len == 0) {
  223. vfree(buf);
  224. return 0;
  225. }
  226. /* log len need 16 bytes algin to do encrypt */
  227. total_len = ALIGN(total_len, ALIGN_LEN);
  228. ret = qcom_ddump_do_alive_log_encrypt(buf, total_len, &hdr->data);
  229. vfree(buf);
  230. if (ret)
  231. return ret;
  232. hdr->svm_dump_len = hdr->data.frame_size;
  233. return ret;
  234. }
  235. void qcom_ddump_encrypt_exit(void)
  236. {
  237. if (!init_done) {
  238. pr_err("%s: Driver probe failed\n", __func__);
  239. return;
  240. }
  241. vfree(input_data->pubkey);
  242. input_data->pubkey = NULL;
  243. kfree(input_data);
  244. input_data = NULL;
  245. init_done = false;
  246. }
  247. int qcom_ddump_encrypt_init(struct device_node *node)
  248. {
  249. int ret;
  250. if (!node)
  251. return -EINVAL;
  252. input_data = kzalloc(sizeof(struct pubkey_data), GFP_KERNEL);
  253. if (!input_data)
  254. return -ENOMEM;
  255. ret = of_property_read_u32(node, "ddump-pubkey-size", &input_data->pubkeysize);
  256. if (ret) {
  257. pr_err("Failed to read ddump-pubkey-size %d\n", ret);
  258. goto free_input_data;
  259. }
  260. input_data->pubkey = vzalloc(input_data->pubkeysize);
  261. if (!input_data->pubkey) {
  262. ret = -ENOMEM;
  263. goto free_input_data;
  264. }
  265. ret = of_property_read_u8_array(node, "ddump-pubkey",
  266. input_data->pubkey, input_data->pubkeysize);
  267. if (ret) {
  268. pr_err("Failed to read ddump-pubkey %d\n", ret);
  269. goto free_pubkey;
  270. }
  271. init_done = true;
  272. return ret;
  273. free_pubkey:
  274. vfree(input_data->pubkey);
  275. input_data->pubkey = NULL;
  276. free_input_data:
  277. kfree(input_data);
  278. input_data = NULL;
  279. return ret;
  280. }