digsig.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2011 Nokia Corporation
  4. * Copyright (C) 2011 Intel Corporation
  5. *
  6. * Author:
  7. * Dmitry Kasatkin <dmitry.kasatkin@nokia.com>
  8. * <dmitry.kasatkin@intel.com>
  9. *
  10. * File: sign.c
  11. * implements signature (RSA) verification
  12. * pkcs decoding is based on LibTomCrypt code
  13. */
  14. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  15. #include <linux/err.h>
  16. #include <linux/module.h>
  17. #include <linux/slab.h>
  18. #include <linux/key.h>
  19. #include <linux/crypto.h>
  20. #include <crypto/hash.h>
  21. #include <crypto/sha.h>
  22. #include <keys/user-type.h>
  23. #include <linux/mpi.h>
  24. #include <linux/digsig.h>
  25. static struct crypto_shash *shash;
  26. static const char *pkcs_1_v1_5_decode_emsa(const unsigned char *msg,
  27. unsigned long msglen,
  28. unsigned long modulus_bitlen,
  29. unsigned long *outlen)
  30. {
  31. unsigned long modulus_len, ps_len, i;
  32. modulus_len = (modulus_bitlen >> 3) + (modulus_bitlen & 7 ? 1 : 0);
  33. /* test message size */
  34. if ((msglen > modulus_len) || (modulus_len < 11))
  35. return NULL;
  36. /* separate encoded message */
  37. if (msg[0] != 0x00 || msg[1] != 0x01)
  38. return NULL;
  39. for (i = 2; i < modulus_len - 1; i++)
  40. if (msg[i] != 0xFF)
  41. break;
  42. /* separator check */
  43. if (msg[i] != 0)
  44. /* There was no octet with hexadecimal value 0x00
  45. to separate ps from m. */
  46. return NULL;
  47. ps_len = i - 2;
  48. *outlen = (msglen - (2 + ps_len + 1));
  49. return msg + 2 + ps_len + 1;
  50. }
  51. /*
  52. * RSA Signature verification with public key
  53. */
  54. static int digsig_verify_rsa(struct key *key,
  55. const char *sig, int siglen,
  56. const char *h, int hlen)
  57. {
  58. int err = -EINVAL;
  59. unsigned long len;
  60. unsigned long mlen, mblen;
  61. unsigned nret, l;
  62. int head, i;
  63. unsigned char *out1 = NULL;
  64. const char *m;
  65. MPI in = NULL, res = NULL, pkey[2];
  66. uint8_t *p, *datap;
  67. const uint8_t *endp;
  68. const struct user_key_payload *ukp;
  69. struct pubkey_hdr *pkh;
  70. down_read(&key->sem);
  71. ukp = user_key_payload_locked(key);
  72. if (!ukp) {
  73. /* key was revoked before we acquired its semaphore */
  74. err = -EKEYREVOKED;
  75. goto err1;
  76. }
  77. if (ukp->datalen < sizeof(*pkh))
  78. goto err1;
  79. pkh = (struct pubkey_hdr *)ukp->data;
  80. if (pkh->version != 1)
  81. goto err1;
  82. if (pkh->algo != PUBKEY_ALGO_RSA)
  83. goto err1;
  84. if (pkh->nmpi != 2)
  85. goto err1;
  86. datap = pkh->mpi;
  87. endp = ukp->data + ukp->datalen;
  88. for (i = 0; i < pkh->nmpi; i++) {
  89. unsigned int remaining = endp - datap;
  90. pkey[i] = mpi_read_from_buffer(datap, &remaining);
  91. if (IS_ERR(pkey[i])) {
  92. err = PTR_ERR(pkey[i]);
  93. goto err;
  94. }
  95. datap += remaining;
  96. }
  97. mblen = mpi_get_nbits(pkey[0]);
  98. mlen = DIV_ROUND_UP(mblen, 8);
  99. if (mlen == 0) {
  100. err = -EINVAL;
  101. goto err;
  102. }
  103. err = -ENOMEM;
  104. out1 = kzalloc(mlen, GFP_KERNEL);
  105. if (!out1)
  106. goto err;
  107. nret = siglen;
  108. in = mpi_read_from_buffer(sig, &nret);
  109. if (IS_ERR(in)) {
  110. err = PTR_ERR(in);
  111. goto err;
  112. }
  113. res = mpi_alloc(mpi_get_nlimbs(in) * 2);
  114. if (!res)
  115. goto err;
  116. err = mpi_powm(res, in, pkey[1], pkey[0]);
  117. if (err)
  118. goto err;
  119. if (mpi_get_nlimbs(res) * BYTES_PER_MPI_LIMB > mlen) {
  120. err = -EINVAL;
  121. goto err;
  122. }
  123. p = mpi_get_buffer(res, &l, NULL);
  124. if (!p) {
  125. err = -EINVAL;
  126. goto err;
  127. }
  128. len = mlen;
  129. head = len - l;
  130. memset(out1, 0, head);
  131. memcpy(out1 + head, p, l);
  132. kfree(p);
  133. m = pkcs_1_v1_5_decode_emsa(out1, len, mblen, &len);
  134. if (!m || len != hlen || memcmp(m, h, hlen))
  135. err = -EINVAL;
  136. err:
  137. mpi_free(in);
  138. mpi_free(res);
  139. kfree(out1);
  140. while (--i >= 0)
  141. mpi_free(pkey[i]);
  142. err1:
  143. up_read(&key->sem);
  144. return err;
  145. }
  146. /**
  147. * digsig_verify() - digital signature verification with public key
  148. * @keyring: keyring to search key in
  149. * @sig: digital signature
  150. * @siglen: length of the signature
  151. * @data: data
  152. * @datalen: length of the data
  153. *
  154. * Returns 0 on success, -EINVAL otherwise
  155. *
  156. * Verifies data integrity against digital signature.
  157. * Currently only RSA is supported.
  158. * Normally hash of the content is used as a data for this function.
  159. *
  160. */
  161. int digsig_verify(struct key *keyring, const char *sig, int siglen,
  162. const char *data, int datalen)
  163. {
  164. int err = -ENOMEM;
  165. struct signature_hdr *sh = (struct signature_hdr *)sig;
  166. struct shash_desc *desc = NULL;
  167. unsigned char hash[SHA1_DIGEST_SIZE];
  168. struct key *key;
  169. char name[20];
  170. if (siglen < sizeof(*sh) + 2)
  171. return -EINVAL;
  172. if (sh->algo != PUBKEY_ALGO_RSA)
  173. return -ENOTSUPP;
  174. sprintf(name, "%llX", __be64_to_cpup((uint64_t *)sh->keyid));
  175. if (keyring) {
  176. /* search in specific keyring */
  177. key_ref_t kref;
  178. kref = keyring_search(make_key_ref(keyring, 1UL),
  179. &key_type_user, name, true);
  180. if (IS_ERR(kref))
  181. key = ERR_CAST(kref);
  182. else
  183. key = key_ref_to_ptr(kref);
  184. } else {
  185. key = request_key(&key_type_user, name, NULL);
  186. }
  187. if (IS_ERR(key)) {
  188. pr_err("key not found, id: %s\n", name);
  189. return PTR_ERR(key);
  190. }
  191. desc = kzalloc(sizeof(*desc) + crypto_shash_descsize(shash),
  192. GFP_KERNEL);
  193. if (!desc)
  194. goto err;
  195. desc->tfm = shash;
  196. crypto_shash_init(desc);
  197. crypto_shash_update(desc, data, datalen);
  198. crypto_shash_update(desc, sig, sizeof(*sh));
  199. crypto_shash_final(desc, hash);
  200. kfree(desc);
  201. /* pass signature mpis address */
  202. err = digsig_verify_rsa(key, sig + sizeof(*sh), siglen - sizeof(*sh),
  203. hash, sizeof(hash));
  204. err:
  205. key_put(key);
  206. return err ? -EINVAL : 0;
  207. }
  208. EXPORT_SYMBOL_GPL(digsig_verify);
  209. static int __init digsig_init(void)
  210. {
  211. shash = crypto_alloc_shash("sha1", 0, 0);
  212. if (IS_ERR(shash)) {
  213. pr_err("shash allocation failed\n");
  214. return PTR_ERR(shash);
  215. }
  216. return 0;
  217. }
  218. static void __exit digsig_cleanup(void)
  219. {
  220. crypto_free_shash(shash);
  221. }
  222. module_init(digsig_init);
  223. module_exit(digsig_cleanup);
  224. MODULE_LICENSE("GPL");