rsa-pkcs1pad.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * RSA padding templates.
  4. *
  5. * Copyright (c) 2015 Intel Corporation
  6. */
  7. #include <crypto/algapi.h>
  8. #include <crypto/akcipher.h>
  9. #include <crypto/internal/akcipher.h>
  10. #include <crypto/internal/rsa.h>
  11. #include <linux/err.h>
  12. #include <linux/init.h>
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/random.h>
  16. #include <linux/scatterlist.h>
  17. struct pkcs1pad_ctx {
  18. struct crypto_akcipher *child;
  19. unsigned int key_size;
  20. };
  21. struct pkcs1pad_inst_ctx {
  22. struct crypto_akcipher_spawn spawn;
  23. };
  24. struct pkcs1pad_request {
  25. struct scatterlist in_sg[2], out_sg[1];
  26. uint8_t *in_buf, *out_buf;
  27. struct akcipher_request child_req;
  28. };
  29. static int pkcs1pad_set_pub_key(struct crypto_akcipher *tfm, const void *key,
  30. unsigned int keylen)
  31. {
  32. struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
  33. return rsa_set_key(ctx->child, &ctx->key_size, RSA_PUB, key, keylen);
  34. }
  35. static int pkcs1pad_set_priv_key(struct crypto_akcipher *tfm, const void *key,
  36. unsigned int keylen)
  37. {
  38. struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
  39. return rsa_set_key(ctx->child, &ctx->key_size, RSA_PRIV, key, keylen);
  40. }
  41. static unsigned int pkcs1pad_get_max_size(struct crypto_akcipher *tfm)
  42. {
  43. struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
  44. /*
  45. * The maximum destination buffer size for the encrypt operation
  46. * will be the same as for RSA, even though it's smaller for
  47. * decrypt.
  48. */
  49. return ctx->key_size;
  50. }
  51. static void pkcs1pad_sg_set_buf(struct scatterlist *sg, void *buf, size_t len,
  52. struct scatterlist *next)
  53. {
  54. int nsegs = next ? 2 : 1;
  55. sg_init_table(sg, nsegs);
  56. sg_set_buf(sg, buf, len);
  57. if (next)
  58. sg_chain(sg, nsegs, next);
  59. }
  60. static int pkcs1pad_encrypt_complete(struct akcipher_request *req, int err)
  61. {
  62. struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
  63. struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
  64. struct pkcs1pad_request *req_ctx = akcipher_request_ctx(req);
  65. unsigned int pad_len;
  66. unsigned int len;
  67. u8 *out_buf;
  68. if (err)
  69. goto out;
  70. len = req_ctx->child_req.dst_len;
  71. pad_len = ctx->key_size - len;
  72. /* Four billion to one */
  73. if (likely(!pad_len))
  74. goto out;
  75. out_buf = kzalloc(ctx->key_size, GFP_ATOMIC);
  76. err = -ENOMEM;
  77. if (!out_buf)
  78. goto out;
  79. sg_copy_to_buffer(req->dst, sg_nents_for_len(req->dst, len),
  80. out_buf + pad_len, len);
  81. sg_copy_from_buffer(req->dst,
  82. sg_nents_for_len(req->dst, ctx->key_size),
  83. out_buf, ctx->key_size);
  84. kfree_sensitive(out_buf);
  85. out:
  86. req->dst_len = ctx->key_size;
  87. kfree(req_ctx->in_buf);
  88. return err;
  89. }
  90. static void pkcs1pad_encrypt_complete_cb(void *data, int err)
  91. {
  92. struct akcipher_request *req = data;
  93. if (err == -EINPROGRESS)
  94. goto out;
  95. err = pkcs1pad_encrypt_complete(req, err);
  96. out:
  97. akcipher_request_complete(req, err);
  98. }
  99. static int pkcs1pad_encrypt(struct akcipher_request *req)
  100. {
  101. struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
  102. struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
  103. struct pkcs1pad_request *req_ctx = akcipher_request_ctx(req);
  104. int err;
  105. unsigned int i, ps_end;
  106. if (!ctx->key_size)
  107. return -EINVAL;
  108. if (req->src_len > ctx->key_size - 11)
  109. return -EOVERFLOW;
  110. if (req->dst_len < ctx->key_size) {
  111. req->dst_len = ctx->key_size;
  112. return -EOVERFLOW;
  113. }
  114. req_ctx->in_buf = kmalloc(ctx->key_size - 1 - req->src_len,
  115. GFP_KERNEL);
  116. if (!req_ctx->in_buf)
  117. return -ENOMEM;
  118. ps_end = ctx->key_size - req->src_len - 2;
  119. req_ctx->in_buf[0] = 0x02;
  120. for (i = 1; i < ps_end; i++)
  121. req_ctx->in_buf[i] = get_random_u32_inclusive(1, 255);
  122. req_ctx->in_buf[ps_end] = 0x00;
  123. pkcs1pad_sg_set_buf(req_ctx->in_sg, req_ctx->in_buf,
  124. ctx->key_size - 1 - req->src_len, req->src);
  125. akcipher_request_set_tfm(&req_ctx->child_req, ctx->child);
  126. akcipher_request_set_callback(&req_ctx->child_req, req->base.flags,
  127. pkcs1pad_encrypt_complete_cb, req);
  128. /* Reuse output buffer */
  129. akcipher_request_set_crypt(&req_ctx->child_req, req_ctx->in_sg,
  130. req->dst, ctx->key_size - 1, req->dst_len);
  131. err = crypto_akcipher_encrypt(&req_ctx->child_req);
  132. if (err != -EINPROGRESS && err != -EBUSY)
  133. return pkcs1pad_encrypt_complete(req, err);
  134. return err;
  135. }
  136. static int pkcs1pad_decrypt_complete(struct akcipher_request *req, int err)
  137. {
  138. struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
  139. struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
  140. struct pkcs1pad_request *req_ctx = akcipher_request_ctx(req);
  141. unsigned int dst_len;
  142. unsigned int pos;
  143. u8 *out_buf;
  144. if (err)
  145. goto done;
  146. err = -EINVAL;
  147. dst_len = req_ctx->child_req.dst_len;
  148. if (dst_len < ctx->key_size - 1)
  149. goto done;
  150. out_buf = req_ctx->out_buf;
  151. if (dst_len == ctx->key_size) {
  152. if (out_buf[0] != 0x00)
  153. /* Decrypted value had no leading 0 byte */
  154. goto done;
  155. dst_len--;
  156. out_buf++;
  157. }
  158. if (out_buf[0] != 0x02)
  159. goto done;
  160. for (pos = 1; pos < dst_len; pos++)
  161. if (out_buf[pos] == 0x00)
  162. break;
  163. if (pos < 9 || pos == dst_len)
  164. goto done;
  165. pos++;
  166. err = 0;
  167. if (req->dst_len < dst_len - pos)
  168. err = -EOVERFLOW;
  169. req->dst_len = dst_len - pos;
  170. if (!err)
  171. sg_copy_from_buffer(req->dst,
  172. sg_nents_for_len(req->dst, req->dst_len),
  173. out_buf + pos, req->dst_len);
  174. done:
  175. kfree_sensitive(req_ctx->out_buf);
  176. return err;
  177. }
  178. static void pkcs1pad_decrypt_complete_cb(void *data, int err)
  179. {
  180. struct akcipher_request *req = data;
  181. if (err == -EINPROGRESS)
  182. goto out;
  183. err = pkcs1pad_decrypt_complete(req, err);
  184. out:
  185. akcipher_request_complete(req, err);
  186. }
  187. static int pkcs1pad_decrypt(struct akcipher_request *req)
  188. {
  189. struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
  190. struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
  191. struct pkcs1pad_request *req_ctx = akcipher_request_ctx(req);
  192. int err;
  193. if (!ctx->key_size || req->src_len != ctx->key_size)
  194. return -EINVAL;
  195. req_ctx->out_buf = kmalloc(ctx->key_size, GFP_KERNEL);
  196. if (!req_ctx->out_buf)
  197. return -ENOMEM;
  198. pkcs1pad_sg_set_buf(req_ctx->out_sg, req_ctx->out_buf,
  199. ctx->key_size, NULL);
  200. akcipher_request_set_tfm(&req_ctx->child_req, ctx->child);
  201. akcipher_request_set_callback(&req_ctx->child_req, req->base.flags,
  202. pkcs1pad_decrypt_complete_cb, req);
  203. /* Reuse input buffer, output to a new buffer */
  204. akcipher_request_set_crypt(&req_ctx->child_req, req->src,
  205. req_ctx->out_sg, req->src_len,
  206. ctx->key_size);
  207. err = crypto_akcipher_decrypt(&req_ctx->child_req);
  208. if (err != -EINPROGRESS && err != -EBUSY)
  209. return pkcs1pad_decrypt_complete(req, err);
  210. return err;
  211. }
  212. static int pkcs1pad_init_tfm(struct crypto_akcipher *tfm)
  213. {
  214. struct akcipher_instance *inst = akcipher_alg_instance(tfm);
  215. struct pkcs1pad_inst_ctx *ictx = akcipher_instance_ctx(inst);
  216. struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
  217. struct crypto_akcipher *child_tfm;
  218. child_tfm = crypto_spawn_akcipher(&ictx->spawn);
  219. if (IS_ERR(child_tfm))
  220. return PTR_ERR(child_tfm);
  221. ctx->child = child_tfm;
  222. akcipher_set_reqsize(tfm, sizeof(struct pkcs1pad_request) +
  223. crypto_akcipher_reqsize(child_tfm));
  224. return 0;
  225. }
  226. static void pkcs1pad_exit_tfm(struct crypto_akcipher *tfm)
  227. {
  228. struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
  229. crypto_free_akcipher(ctx->child);
  230. }
  231. static void pkcs1pad_free(struct akcipher_instance *inst)
  232. {
  233. struct pkcs1pad_inst_ctx *ctx = akcipher_instance_ctx(inst);
  234. struct crypto_akcipher_spawn *spawn = &ctx->spawn;
  235. crypto_drop_akcipher(spawn);
  236. kfree(inst);
  237. }
  238. static int pkcs1pad_create(struct crypto_template *tmpl, struct rtattr **tb)
  239. {
  240. u32 mask;
  241. struct akcipher_instance *inst;
  242. struct pkcs1pad_inst_ctx *ctx;
  243. struct akcipher_alg *rsa_alg;
  244. int err;
  245. err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_AKCIPHER, &mask);
  246. if (err)
  247. return err;
  248. inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
  249. if (!inst)
  250. return -ENOMEM;
  251. ctx = akcipher_instance_ctx(inst);
  252. err = crypto_grab_akcipher(&ctx->spawn, akcipher_crypto_instance(inst),
  253. crypto_attr_alg_name(tb[1]), 0, mask);
  254. if (err)
  255. goto err_free_inst;
  256. rsa_alg = crypto_spawn_akcipher_alg(&ctx->spawn);
  257. if (strcmp(rsa_alg->base.cra_name, "rsa") != 0) {
  258. err = -EINVAL;
  259. goto err_free_inst;
  260. }
  261. err = -ENAMETOOLONG;
  262. if (snprintf(inst->alg.base.cra_name,
  263. CRYPTO_MAX_ALG_NAME, "pkcs1pad(%s)",
  264. rsa_alg->base.cra_name) >= CRYPTO_MAX_ALG_NAME)
  265. goto err_free_inst;
  266. if (snprintf(inst->alg.base.cra_driver_name,
  267. CRYPTO_MAX_ALG_NAME, "pkcs1pad(%s)",
  268. rsa_alg->base.cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
  269. goto err_free_inst;
  270. inst->alg.base.cra_priority = rsa_alg->base.cra_priority;
  271. inst->alg.base.cra_ctxsize = sizeof(struct pkcs1pad_ctx);
  272. inst->alg.init = pkcs1pad_init_tfm;
  273. inst->alg.exit = pkcs1pad_exit_tfm;
  274. inst->alg.encrypt = pkcs1pad_encrypt;
  275. inst->alg.decrypt = pkcs1pad_decrypt;
  276. inst->alg.set_pub_key = pkcs1pad_set_pub_key;
  277. inst->alg.set_priv_key = pkcs1pad_set_priv_key;
  278. inst->alg.max_size = pkcs1pad_get_max_size;
  279. inst->free = pkcs1pad_free;
  280. err = akcipher_register_instance(tmpl, inst);
  281. if (err) {
  282. err_free_inst:
  283. pkcs1pad_free(inst);
  284. }
  285. return err;
  286. }
  287. struct crypto_template rsa_pkcs1pad_tmpl = {
  288. .name = "pkcs1pad",
  289. .create = pkcs1pad_create,
  290. .module = THIS_MODULE,
  291. };
  292. MODULE_ALIAS_CRYPTO("pkcs1pad");