1
2
3
4
5
6
7
8
9
10
11
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
25MODULE_DESCRIPTION("In-software asymmetric public-key subtype");
26MODULE_AUTHOR("Red Hat, Inc.");
27MODULE_LICENSE("GPL");
28
29
30
31
32static void public_key_describe(const struct key *asymmetric_key,
33 struct seq_file *m)
34{
35 struct public_key *key = asymmetric_key->payload.data[asym_crypto];
36
37 if (key)
38 seq_printf(m, "%s.%s", key->id_type, key->pkey_algo);
39}
40
41
42
43
44void public_key_free(struct public_key *key)
45{
46 if (key) {
47 kfree(key->key);
48 kfree(key);
49 }
50}
51EXPORT_SYMBOL_GPL(public_key_free);
52
53
54
55
56static void public_key_destroy(void *payload0, void *payload3)
57{
58 public_key_free(payload0);
59 public_key_signature_free(payload3);
60}
61
62
63
64
65int public_key_verify_signature(const struct public_key *pkey,
66 const struct public_key_signature *sig)
67{
68 struct crypto_wait cwait;
69 struct crypto_akcipher *tfm;
70 struct akcipher_request *req;
71 struct scatterlist sig_sg, digest_sg;
72 const char *alg_name;
73 char alg_name_buf[CRYPTO_MAX_ALG_NAME];
74 void *output;
75 unsigned int outlen;
76 int ret;
77
78 pr_devel("==>%s()\n", __func__);
79
80 BUG_ON(!pkey);
81 BUG_ON(!sig);
82 BUG_ON(!sig->s);
83
84 if (!sig->digest)
85 return -ENOPKG;
86
87 alg_name = sig->pkey_algo;
88 if (strcmp(sig->pkey_algo, "rsa") == 0) {
89
90
91
92
93 if (snprintf(alg_name_buf, CRYPTO_MAX_ALG_NAME,
94 "pkcs1pad(rsa,%s)", sig->hash_algo
95 ) >= CRYPTO_MAX_ALG_NAME)
96 return -EINVAL;
97 alg_name = alg_name_buf;
98 }
99
100 tfm = crypto_alloc_akcipher(alg_name, 0, 0);
101 if (IS_ERR(tfm))
102 return PTR_ERR(tfm);
103
104 ret = -ENOMEM;
105 req = akcipher_request_alloc(tfm, GFP_KERNEL);
106 if (!req)
107 goto error_free_tfm;
108
109 ret = crypto_akcipher_set_pub_key(tfm, pkey->key, pkey->keylen);
110 if (ret)
111 goto error_free_req;
112
113 ret = -ENOMEM;
114 outlen = crypto_akcipher_maxsize(tfm);
115 output = kmalloc(outlen, GFP_KERNEL);
116 if (!output)
117 goto error_free_req;
118
119 sg_init_one(&sig_sg, sig->s, sig->s_size);
120 sg_init_one(&digest_sg, output, outlen);
121 akcipher_request_set_crypt(req, &sig_sg, &digest_sg, sig->s_size,
122 outlen);
123 crypto_init_wait(&cwait);
124 akcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG |
125 CRYPTO_TFM_REQ_MAY_SLEEP,
126 crypto_req_done, &cwait);
127
128
129
130
131
132 ret = crypto_wait_req(crypto_akcipher_verify(req), &cwait);
133 if (ret)
134 goto out_free_output;
135
136
137 if (req->dst_len != sig->digest_size ||
138 memcmp(sig->digest, output, sig->digest_size) != 0)
139 ret = -EKEYREJECTED;
140
141out_free_output:
142 kfree(output);
143error_free_req:
144 akcipher_request_free(req);
145error_free_tfm:
146 crypto_free_akcipher(tfm);
147 pr_devel("<==%s() = %d\n", __func__, ret);
148 if (WARN_ON_ONCE(ret > 0))
149 ret = -EINVAL;
150 return ret;
151}
152EXPORT_SYMBOL_GPL(public_key_verify_signature);
153
154static int public_key_verify_signature_2(const struct key *key,
155 const struct public_key_signature *sig)
156{
157 const struct public_key *pk = key->payload.data[asym_crypto];
158 return public_key_verify_signature(pk, sig);
159}
160
161
162
163
164struct asymmetric_key_subtype public_key_subtype = {
165 .owner = THIS_MODULE,
166 .name = "public_key",
167 .name_len = sizeof("public_key") - 1,
168 .describe = public_key_describe,
169 .destroy = public_key_destroy,
170 .verify_signature = public_key_verify_signature_2,
171};
172EXPORT_SYMBOL_GPL(public_key_subtype);
173