GNU Linux-libre 4.14.251-gnu1
[releases.git] / crypto / asymmetric_keys / public_key.c
1 /* In-software asymmetric public-key crypto subtype
2  *
3  * See Documentation/crypto/asymmetric-keys.txt
4  *
5  * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
6  * Written by David Howells (dhowells@redhat.com)
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public Licence
10  * as published by the Free Software Foundation; either version
11  * 2 of the Licence, or (at your option) any later version.
12  */
13
14 #define pr_fmt(fmt) "PKEY: "fmt
15 #include <linux/module.h>
16 #include <linux/export.h>
17 #include <linux/kernel.h>
18 #include <linux/slab.h>
19 #include <linux/seq_file.h>
20 #include <linux/scatterlist.h>
21 #include <keys/asymmetric-subtype.h>
22 #include <crypto/public_key.h>
23 #include <crypto/akcipher.h>
24
25 MODULE_LICENSE("GPL");
26
27 /*
28  * Provide a part of a description of the key for /proc/keys.
29  */
30 static void public_key_describe(const struct key *asymmetric_key,
31                                 struct seq_file *m)
32 {
33         struct public_key *key = asymmetric_key->payload.data[asym_crypto];
34
35         if (key)
36                 seq_printf(m, "%s.%s", key->id_type, key->pkey_algo);
37 }
38
39 /*
40  * Destroy a public key algorithm key.
41  */
42 void public_key_free(struct public_key *key)
43 {
44         if (key) {
45                 kfree(key->key);
46                 kfree(key);
47         }
48 }
49 EXPORT_SYMBOL_GPL(public_key_free);
50
51 /*
52  * Destroy a public key algorithm key.
53  */
54 static void public_key_destroy(void *payload0, void *payload3)
55 {
56         public_key_free(payload0);
57         public_key_signature_free(payload3);
58 }
59
60 struct public_key_completion {
61         struct completion completion;
62         int err;
63 };
64
65 static void public_key_verify_done(struct crypto_async_request *req, int err)
66 {
67         struct public_key_completion *compl = req->data;
68
69         if (err == -EINPROGRESS)
70                 return;
71
72         compl->err = err;
73         complete(&compl->completion);
74 }
75
76 /*
77  * Verify a signature using a public key.
78  */
79 int public_key_verify_signature(const struct public_key *pkey,
80                                 const struct public_key_signature *sig)
81 {
82         struct public_key_completion compl;
83         struct crypto_akcipher *tfm;
84         struct akcipher_request *req;
85         struct scatterlist sig_sg, digest_sg;
86         const char *alg_name;
87         char alg_name_buf[CRYPTO_MAX_ALG_NAME];
88         void *output;
89         unsigned int outlen;
90         int ret = -ENOMEM;
91
92         pr_devel("==>%s()\n", __func__);
93
94         BUG_ON(!pkey);
95         BUG_ON(!sig);
96         BUG_ON(!sig->s);
97
98         if (!sig->digest)
99                 return -ENOPKG;
100
101         alg_name = sig->pkey_algo;
102         if (strcmp(sig->pkey_algo, "rsa") == 0) {
103                 /* The data wangled by the RSA algorithm is typically padded
104                  * and encoded in some manner, such as EMSA-PKCS1-1_5 [RFC3447
105                  * sec 8.2].
106                  */
107                 if (snprintf(alg_name_buf, CRYPTO_MAX_ALG_NAME,
108                              "pkcs1pad(rsa,%s)", sig->hash_algo
109                              ) >= CRYPTO_MAX_ALG_NAME)
110                         return -EINVAL;
111                 alg_name = alg_name_buf;
112         }
113
114         tfm = crypto_alloc_akcipher(alg_name, 0, 0);
115         if (IS_ERR(tfm))
116                 return PTR_ERR(tfm);
117
118         req = akcipher_request_alloc(tfm, GFP_KERNEL);
119         if (!req)
120                 goto error_free_tfm;
121
122         ret = crypto_akcipher_set_pub_key(tfm, pkey->key, pkey->keylen);
123         if (ret)
124                 goto error_free_req;
125
126         ret = -ENOMEM;
127         outlen = crypto_akcipher_maxsize(tfm);
128         output = kmalloc(outlen, GFP_KERNEL);
129         if (!output)
130                 goto error_free_req;
131
132         sg_init_one(&sig_sg, sig->s, sig->s_size);
133         sg_init_one(&digest_sg, output, outlen);
134         akcipher_request_set_crypt(req, &sig_sg, &digest_sg, sig->s_size,
135                                    outlen);
136         init_completion(&compl.completion);
137         akcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG |
138                                       CRYPTO_TFM_REQ_MAY_SLEEP,
139                                       public_key_verify_done, &compl);
140
141         /* Perform the verification calculation.  This doesn't actually do the
142          * verification, but rather calculates the hash expected by the
143          * signature and returns that to us.
144          */
145         ret = crypto_akcipher_verify(req);
146         if ((ret == -EINPROGRESS) || (ret == -EBUSY)) {
147                 wait_for_completion(&compl.completion);
148                 ret = compl.err;
149         }
150         if (ret < 0)
151                 goto out_free_output;
152
153         /* Do the actual verification step. */
154         if (req->dst_len != sig->digest_size ||
155             memcmp(sig->digest, output, sig->digest_size) != 0)
156                 ret = -EKEYREJECTED;
157
158 out_free_output:
159         kfree(output);
160 error_free_req:
161         akcipher_request_free(req);
162 error_free_tfm:
163         crypto_free_akcipher(tfm);
164         pr_devel("<==%s() = %d\n", __func__, ret);
165         return ret;
166 }
167 EXPORT_SYMBOL_GPL(public_key_verify_signature);
168
169 static int public_key_verify_signature_2(const struct key *key,
170                                          const struct public_key_signature *sig)
171 {
172         const struct public_key *pk = key->payload.data[asym_crypto];
173         return public_key_verify_signature(pk, sig);
174 }
175
176 /*
177  * Public key algorithm asymmetric key subtype
178  */
179 struct asymmetric_key_subtype public_key_subtype = {
180         .owner                  = THIS_MODULE,
181         .name                   = "public_key",
182         .name_len               = sizeof("public_key") - 1,
183         .describe               = public_key_describe,
184         .destroy                = public_key_destroy,
185         .verify_signature       = public_key_verify_signature_2,
186 };
187 EXPORT_SYMBOL_GPL(public_key_subtype);