Back to home page

LXR

 
 

    


0001 /*
0002  * Copyright (C) 2011 Nokia Corporation
0003  * Copyright (C) 2011 Intel Corporation
0004  *
0005  * Author:
0006  * Dmitry Kasatkin <dmitry.kasatkin@nokia.com>
0007  *                 <dmitry.kasatkin@intel.com>
0008  *
0009  * This program is free software; you can redistribute it and/or modify
0010  * it under the terms of the GNU General Public License as published by
0011  * the Free Software Foundation, version 2 of the License.
0012  *
0013  * File: sign.c
0014  *  implements signature (RSA) verification
0015  *  pkcs decoding is based on LibTomCrypt code
0016  */
0017 
0018 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0019 
0020 #include <linux/err.h>
0021 #include <linux/module.h>
0022 #include <linux/slab.h>
0023 #include <linux/key.h>
0024 #include <linux/crypto.h>
0025 #include <crypto/hash.h>
0026 #include <crypto/sha.h>
0027 #include <keys/user-type.h>
0028 #include <linux/mpi.h>
0029 #include <linux/digsig.h>
0030 
0031 static struct crypto_shash *shash;
0032 
0033 static const char *pkcs_1_v1_5_decode_emsa(const unsigned char *msg,
0034                         unsigned long  msglen,
0035                         unsigned long  modulus_bitlen,
0036                         unsigned long *outlen)
0037 {
0038     unsigned long modulus_len, ps_len, i;
0039 
0040     modulus_len = (modulus_bitlen >> 3) + (modulus_bitlen & 7 ? 1 : 0);
0041 
0042     /* test message size */
0043     if ((msglen > modulus_len) || (modulus_len < 11))
0044         return NULL;
0045 
0046     /* separate encoded message */
0047     if (msg[0] != 0x00 || msg[1] != 0x01)
0048         return NULL;
0049 
0050     for (i = 2; i < modulus_len - 1; i++)
0051         if (msg[i] != 0xFF)
0052             break;
0053 
0054     /* separator check */
0055     if (msg[i] != 0)
0056         /* There was no octet with hexadecimal value 0x00
0057         to separate ps from m. */
0058         return NULL;
0059 
0060     ps_len = i - 2;
0061 
0062     *outlen = (msglen - (2 + ps_len + 1));
0063 
0064     return msg + 2 + ps_len + 1;
0065 }
0066 
0067 /*
0068  * RSA Signature verification with public key
0069  */
0070 static int digsig_verify_rsa(struct key *key,
0071             const char *sig, int siglen,
0072                const char *h, int hlen)
0073 {
0074     int err = -EINVAL;
0075     unsigned long len;
0076     unsigned long mlen, mblen;
0077     unsigned nret, l;
0078     int head, i;
0079     unsigned char *out1 = NULL;
0080     const char *m;
0081     MPI in = NULL, res = NULL, pkey[2];
0082     uint8_t *p, *datap;
0083     const uint8_t *endp;
0084     const struct user_key_payload *ukp;
0085     struct pubkey_hdr *pkh;
0086 
0087     down_read(&key->sem);
0088     ukp = user_key_payload(key);
0089 
0090     if (ukp->datalen < sizeof(*pkh))
0091         goto err1;
0092 
0093     pkh = (struct pubkey_hdr *)ukp->data;
0094 
0095     if (pkh->version != 1)
0096         goto err1;
0097 
0098     if (pkh->algo != PUBKEY_ALGO_RSA)
0099         goto err1;
0100 
0101     if (pkh->nmpi != 2)
0102         goto err1;
0103 
0104     datap = pkh->mpi;
0105     endp = ukp->data + ukp->datalen;
0106 
0107     for (i = 0; i < pkh->nmpi; i++) {
0108         unsigned int remaining = endp - datap;
0109         pkey[i] = mpi_read_from_buffer(datap, &remaining);
0110         if (IS_ERR(pkey[i])) {
0111             err = PTR_ERR(pkey[i]);
0112             goto err;
0113         }
0114         datap += remaining;
0115     }
0116 
0117     mblen = mpi_get_nbits(pkey[0]);
0118     mlen = DIV_ROUND_UP(mblen, 8);
0119 
0120     if (mlen == 0) {
0121         err = -EINVAL;
0122         goto err;
0123     }
0124 
0125     err = -ENOMEM;
0126 
0127     out1 = kzalloc(mlen, GFP_KERNEL);
0128     if (!out1)
0129         goto err;
0130 
0131     nret = siglen;
0132     in = mpi_read_from_buffer(sig, &nret);
0133     if (IS_ERR(in)) {
0134         err = PTR_ERR(in);
0135         goto err;
0136     }
0137 
0138     res = mpi_alloc(mpi_get_nlimbs(in) * 2);
0139     if (!res)
0140         goto err;
0141 
0142     err = mpi_powm(res, in, pkey[1], pkey[0]);
0143     if (err)
0144         goto err;
0145 
0146     if (mpi_get_nlimbs(res) * BYTES_PER_MPI_LIMB > mlen) {
0147         err = -EINVAL;
0148         goto err;
0149     }
0150 
0151     p = mpi_get_buffer(res, &l, NULL);
0152     if (!p) {
0153         err = -EINVAL;
0154         goto err;
0155     }
0156 
0157     len = mlen;
0158     head = len - l;
0159     memset(out1, 0, head);
0160     memcpy(out1 + head, p, l);
0161 
0162     kfree(p);
0163 
0164     m = pkcs_1_v1_5_decode_emsa(out1, len, mblen, &len);
0165 
0166     if (!m || len != hlen || memcmp(m, h, hlen))
0167         err = -EINVAL;
0168 
0169 err:
0170     mpi_free(in);
0171     mpi_free(res);
0172     kfree(out1);
0173     while (--i >= 0)
0174         mpi_free(pkey[i]);
0175 err1:
0176     up_read(&key->sem);
0177 
0178     return err;
0179 }
0180 
0181 /**
0182  * digsig_verify() - digital signature verification with public key
0183  * @keyring:    keyring to search key in
0184  * @sig:    digital signature
0185  * @siglen: length of the signature
0186  * @data:   data
0187  * @datalen:    length of the data
0188  *
0189  * Returns 0 on success, -EINVAL otherwise
0190  *
0191  * Verifies data integrity against digital signature.
0192  * Currently only RSA is supported.
0193  * Normally hash of the content is used as a data for this function.
0194  *
0195  */
0196 int digsig_verify(struct key *keyring, const char *sig, int siglen,
0197                         const char *data, int datalen)
0198 {
0199     int err = -ENOMEM;
0200     struct signature_hdr *sh = (struct signature_hdr *)sig;
0201     struct shash_desc *desc = NULL;
0202     unsigned char hash[SHA1_DIGEST_SIZE];
0203     struct key *key;
0204     char name[20];
0205 
0206     if (siglen < sizeof(*sh) + 2)
0207         return -EINVAL;
0208 
0209     if (sh->algo != PUBKEY_ALGO_RSA)
0210         return -ENOTSUPP;
0211 
0212     sprintf(name, "%llX", __be64_to_cpup((uint64_t *)sh->keyid));
0213 
0214     if (keyring) {
0215         /* search in specific keyring */
0216         key_ref_t kref;
0217         kref = keyring_search(make_key_ref(keyring, 1UL),
0218                         &key_type_user, name);
0219         if (IS_ERR(kref))
0220             key = ERR_CAST(kref);
0221         else
0222             key = key_ref_to_ptr(kref);
0223     } else {
0224         key = request_key(&key_type_user, name, NULL);
0225     }
0226     if (IS_ERR(key)) {
0227         pr_err("key not found, id: %s\n", name);
0228         return PTR_ERR(key);
0229     }
0230 
0231     desc = kzalloc(sizeof(*desc) + crypto_shash_descsize(shash),
0232                GFP_KERNEL);
0233     if (!desc)
0234         goto err;
0235 
0236     desc->tfm = shash;
0237     desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
0238 
0239     crypto_shash_init(desc);
0240     crypto_shash_update(desc, data, datalen);
0241     crypto_shash_update(desc, sig, sizeof(*sh));
0242     crypto_shash_final(desc, hash);
0243 
0244     kfree(desc);
0245 
0246     /* pass signature mpis address */
0247     err = digsig_verify_rsa(key, sig + sizeof(*sh), siglen - sizeof(*sh),
0248                  hash, sizeof(hash));
0249 
0250 err:
0251     key_put(key);
0252 
0253     return err ? -EINVAL : 0;
0254 }
0255 EXPORT_SYMBOL_GPL(digsig_verify);
0256 
0257 static int __init digsig_init(void)
0258 {
0259     shash = crypto_alloc_shash("sha1", 0, 0);
0260     if (IS_ERR(shash)) {
0261         pr_err("shash allocation failed\n");
0262         return  PTR_ERR(shash);
0263     }
0264 
0265     return 0;
0266 
0267 }
0268 
0269 static void __exit digsig_cleanup(void)
0270 {
0271     crypto_free_shash(shash);
0272 }
0273 
0274 module_init(digsig_init);
0275 module_exit(digsig_cleanup);
0276 
0277 MODULE_LICENSE("GPL");