digsig.c 5.5 KB

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