digsig.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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 int pkcs_1_v1_5_decode_emsa(const unsigned char *msg,
  30. unsigned long msglen,
  31. unsigned long modulus_bitlen,
  32. unsigned char *out,
  33. unsigned long *outlen,
  34. int *is_valid)
  35. {
  36. unsigned long modulus_len, ps_len, i;
  37. int result;
  38. /* default to invalid packet */
  39. *is_valid = 0;
  40. modulus_len = (modulus_bitlen >> 3) + (modulus_bitlen & 7 ? 1 : 0);
  41. /* test message size */
  42. if ((msglen > modulus_len) || (modulus_len < 11))
  43. return -EINVAL;
  44. /* separate encoded message */
  45. if ((msg[0] != 0x00) || (msg[1] != (unsigned char)1)) {
  46. result = -EINVAL;
  47. goto bail;
  48. }
  49. for (i = 2; i < modulus_len - 1; i++)
  50. if (msg[i] != 0xFF)
  51. break;
  52. /* separator check */
  53. if (msg[i] != 0) {
  54. /* There was no octet with hexadecimal value 0x00
  55. to separate ps from m. */
  56. result = -EINVAL;
  57. goto bail;
  58. }
  59. ps_len = i - 2;
  60. if (*outlen < (msglen - (2 + ps_len + 1))) {
  61. *outlen = msglen - (2 + ps_len + 1);
  62. result = -EOVERFLOW;
  63. goto bail;
  64. }
  65. *outlen = (msglen - (2 + ps_len + 1));
  66. memcpy(out, &msg[2 + ps_len + 1], *outlen);
  67. /* valid packet */
  68. *is_valid = 1;
  69. result = 0;
  70. bail:
  71. return result;
  72. }
  73. /*
  74. * RSA Signature verification with public key
  75. */
  76. static int digsig_verify_rsa(struct key *key,
  77. const char *sig, int siglen,
  78. const char *h, int hlen)
  79. {
  80. int err = -EINVAL;
  81. unsigned long len;
  82. unsigned long mlen, mblen;
  83. unsigned nret, l;
  84. int valid, head, i;
  85. unsigned char *out1 = NULL, *out2 = NULL;
  86. MPI in = NULL, res = NULL, pkey[2];
  87. uint8_t *p, *datap, *endp;
  88. struct user_key_payload *ukp;
  89. struct pubkey_hdr *pkh;
  90. down_read(&key->sem);
  91. ukp = key->payload.data;
  92. if (ukp->datalen < sizeof(*pkh))
  93. goto err1;
  94. pkh = (struct pubkey_hdr *)ukp->data;
  95. if (pkh->version != 1)
  96. goto err1;
  97. if (pkh->algo != PUBKEY_ALGO_RSA)
  98. goto err1;
  99. if (pkh->nmpi != 2)
  100. goto err1;
  101. datap = pkh->mpi;
  102. endp = ukp->data + ukp->datalen;
  103. for (i = 0; i < pkh->nmpi; i++) {
  104. unsigned int remaining = endp - datap;
  105. pkey[i] = mpi_read_from_buffer(datap, &remaining);
  106. datap += remaining;
  107. }
  108. mblen = mpi_get_nbits(pkey[0]);
  109. mlen = (mblen + 7)/8;
  110. if (mlen == 0)
  111. goto err;
  112. out1 = kzalloc(mlen, GFP_KERNEL);
  113. if (!out1)
  114. goto err;
  115. out2 = kzalloc(mlen, GFP_KERNEL);
  116. if (!out2)
  117. goto err;
  118. nret = siglen;
  119. in = mpi_read_from_buffer(sig, &nret);
  120. if (!in)
  121. goto err;
  122. res = mpi_alloc(mpi_get_nlimbs(in) * 2);
  123. if (!res)
  124. goto err;
  125. err = mpi_powm(res, in, pkey[1], pkey[0]);
  126. if (err)
  127. goto err;
  128. if (mpi_get_nlimbs(res) * BYTES_PER_MPI_LIMB > mlen) {
  129. err = -EINVAL;
  130. goto err;
  131. }
  132. p = mpi_get_buffer(res, &l, NULL);
  133. if (!p) {
  134. err = -EINVAL;
  135. goto err;
  136. }
  137. len = mlen;
  138. head = len - l;
  139. memset(out1, 0, head);
  140. memcpy(out1 + head, p, l);
  141. err = -EINVAL;
  142. pkcs_1_v1_5_decode_emsa(out1, len, mblen, out2, &len, &valid);
  143. if (valid && len == hlen)
  144. err = memcmp(out2, h, hlen);
  145. err:
  146. mpi_free(in);
  147. mpi_free(res);
  148. kfree(out1);
  149. kfree(out2);
  150. mpi_free(pkey[0]);
  151. mpi_free(pkey[1]);
  152. err1:
  153. up_read(&key->sem);
  154. return err;
  155. }
  156. /**
  157. * digsig_verify() - digital signature verification with public key
  158. * @keyring: keyring to search key in
  159. * @sig: digital signature
  160. * @sigen: length of the signature
  161. * @data: data
  162. * @datalen: length of the data
  163. * @return: 0 on success, -EINVAL otherwise
  164. *
  165. * Verifies data integrity against digital signature.
  166. * Currently only RSA is supported.
  167. * Normally hash of the content is used as a data for this function.
  168. *
  169. */
  170. int digsig_verify(struct key *keyring, const char *sig, int siglen,
  171. const char *data, int datalen)
  172. {
  173. int err = -ENOMEM;
  174. struct signature_hdr *sh = (struct signature_hdr *)sig;
  175. struct shash_desc *desc = NULL;
  176. unsigned char hash[SHA1_DIGEST_SIZE];
  177. struct key *key;
  178. char name[20];
  179. if (siglen < sizeof(*sh) + 2)
  180. return -EINVAL;
  181. if (sh->algo != PUBKEY_ALGO_RSA)
  182. return -ENOTSUPP;
  183. sprintf(name, "%llX", __be64_to_cpup((uint64_t *)sh->keyid));
  184. if (keyring) {
  185. /* search in specific keyring */
  186. key_ref_t kref;
  187. kref = keyring_search(make_key_ref(keyring, 1UL),
  188. &key_type_user, name);
  189. if (IS_ERR(kref))
  190. key = ERR_PTR(PTR_ERR(kref));
  191. else
  192. key = key_ref_to_ptr(kref);
  193. } else {
  194. key = request_key(&key_type_user, name, NULL);
  195. }
  196. if (IS_ERR(key)) {
  197. pr_err("key not found, id: %s\n", name);
  198. return PTR_ERR(key);
  199. }
  200. desc = kzalloc(sizeof(*desc) + crypto_shash_descsize(shash),
  201. GFP_KERNEL);
  202. if (!desc)
  203. goto err;
  204. desc->tfm = shash;
  205. desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
  206. crypto_shash_init(desc);
  207. crypto_shash_update(desc, data, datalen);
  208. crypto_shash_update(desc, sig, sizeof(*sh));
  209. crypto_shash_final(desc, hash);
  210. kfree(desc);
  211. /* pass signature mpis address */
  212. err = digsig_verify_rsa(key, sig + sizeof(*sh), siglen - sizeof(*sh),
  213. hash, sizeof(hash));
  214. err:
  215. key_put(key);
  216. return err ? -EINVAL : 0;
  217. }
  218. EXPORT_SYMBOL_GPL(digsig_verify);
  219. static int __init digsig_init(void)
  220. {
  221. shash = crypto_alloc_shash("sha1", 0, 0);
  222. if (IS_ERR(shash)) {
  223. pr_err("shash allocation failed\n");
  224. return PTR_ERR(shash);
  225. }
  226. return 0;
  227. }
  228. static void __exit digsig_cleanup(void)
  229. {
  230. crypto_free_shash(shash);
  231. }
  232. module_init(digsig_init);
  233. module_exit(digsig_cleanup);
  234. MODULE_LICENSE("GPL");