blob: 8525fe474abd6fbd60109d670b129114349491f6 [file] [log] [blame]
David Howellsa9681bf2012-09-21 23:24:55 +01001/* 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>
David Howellsd43de6c2016-03-03 21:49:27 +000020#include <linux/scatterlist.h>
David Howellsa9681bf2012-09-21 23:24:55 +010021#include <keys/asymmetric-subtype.h>
Tadeusz Strukdb6c43b2016-02-02 10:08:53 -080022#include <crypto/public_key.h>
David Howellsd43de6c2016-03-03 21:49:27 +000023#include <crypto/akcipher.h>
David Howellsa9681bf2012-09-21 23:24:55 +010024
25MODULE_LICENSE("GPL");
26
David Howellsa9681bf2012-09-21 23:24:55 +010027/*
28 * Provide a part of a description of the key for /proc/keys.
29 */
30static void public_key_describe(const struct key *asymmetric_key,
31 struct seq_file *m)
32{
David Howells146aa8b2015-10-21 14:04:48 +010033 struct public_key *key = asymmetric_key->payload.data[asym_crypto];
David Howellsa9681bf2012-09-21 23:24:55 +010034
35 if (key)
David Howells4e8ae722016-03-03 21:49:27 +000036 seq_printf(m, "%s.%s", key->id_type, key->pkey_algo);
David Howellsa9681bf2012-09-21 23:24:55 +010037}
38
39/*
40 * Destroy a public key algorithm key.
41 */
David Howells3b764562016-04-06 16:13:33 +010042void public_key_free(struct public_key *key)
David Howellsa9681bf2012-09-21 23:24:55 +010043{
David Howells3b764562016-04-06 16:13:33 +010044 if (key) {
Tadeusz Strukdb6c43b2016-02-02 10:08:53 -080045 kfree(key->key);
David Howells3b764562016-04-06 16:13:33 +010046 kfree(key);
47 }
David Howellsa9681bf2012-09-21 23:24:55 +010048}
David Howells3b764562016-04-06 16:13:33 +010049EXPORT_SYMBOL_GPL(public_key_free);
50
51/*
52 * Destroy a public key algorithm key.
53 */
54static void public_key_destroy(void *payload0, void *payload3)
55{
56 public_key_free(payload0);
57 public_key_signature_free(payload3);
58}
David Howellsa9681bf2012-09-21 23:24:55 +010059
David Howellsd43de6c2016-03-03 21:49:27 +000060struct public_key_completion {
61 struct completion completion;
62 int err;
63};
64
65static 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
David Howellsa9681bf2012-09-21 23:24:55 +010076/*
77 * Verify a signature using a public key.
78 */
Tadeusz Strukdb6c43b2016-02-02 10:08:53 -080079int public_key_verify_signature(const struct public_key *pkey,
David Howells3d167d62013-08-30 16:15:30 +010080 const struct public_key_signature *sig)
David Howellsa9681bf2012-09-21 23:24:55 +010081{
David Howellsd43de6c2016-03-03 21:49:27 +000082 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
Tadeusz Strukdb6c43b2016-02-02 10:08:53 -080094 BUG_ON(!pkey);
David Howells3d167d62013-08-30 16:15:30 +010095 BUG_ON(!sig);
Tadeusz Strukdb6c43b2016-02-02 10:08:53 -080096 BUG_ON(!sig->s);
David Howells3d167d62013-08-30 16:15:30 +010097
Eric Biggersc60e2462018-02-22 14:38:33 +000098 if (!sig->digest)
99 return -ENOPKG;
100
David Howells4e8ae722016-03-03 21:49:27 +0000101 alg_name = sig->pkey_algo;
102 if (strcmp(sig->pkey_algo, "rsa") == 0) {
David Howellsd43de6c2016-03-03 21:49:27 +0000103 /* 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,
David Howells4e8ae722016-03-03 21:49:27 +0000108 "pkcs1pad(rsa,%s)", sig->hash_algo
David Howellsd43de6c2016-03-03 21:49:27 +0000109 ) >= CRYPTO_MAX_ALG_NAME)
110 return -EINVAL;
111 alg_name = alg_name_buf;
112 }
David Howells3d167d62013-08-30 16:15:30 +0100113
David Howellsd43de6c2016-03-03 21:49:27 +0000114 tfm = crypto_alloc_akcipher(alg_name, 0, 0);
115 if (IS_ERR(tfm))
116 return PTR_ERR(tfm);
David Howellsa9681bf2012-09-21 23:24:55 +0100117
David Howellsd43de6c2016-03-03 21:49:27 +0000118 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 outlen = crypto_akcipher_maxsize(tfm);
127 output = kmalloc(outlen, GFP_KERNEL);
128 if (!output)
129 goto error_free_req;
130
131 sg_init_one(&sig_sg, sig->s, sig->s_size);
132 sg_init_one(&digest_sg, output, outlen);
133 akcipher_request_set_crypt(req, &sig_sg, &digest_sg, sig->s_size,
134 outlen);
135 init_completion(&compl.completion);
136 akcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG |
137 CRYPTO_TFM_REQ_MAY_SLEEP,
138 public_key_verify_done, &compl);
139
140 /* Perform the verification calculation. This doesn't actually do the
141 * verification, but rather calculates the hash expected by the
142 * signature and returns that to us.
143 */
144 ret = crypto_akcipher_verify(req);
Gilad Ben-Yossef0e479742017-05-18 16:29:23 +0300145 if ((ret == -EINPROGRESS) || (ret == -EBUSY)) {
David Howellsd43de6c2016-03-03 21:49:27 +0000146 wait_for_completion(&compl.completion);
147 ret = compl.err;
148 }
149 if (ret < 0)
150 goto out_free_output;
151
152 /* Do the actual verification step. */
153 if (req->dst_len != sig->digest_size ||
154 memcmp(sig->digest, output, sig->digest_size) != 0)
155 ret = -EKEYREJECTED;
156
157out_free_output:
158 kfree(output);
159error_free_req:
160 akcipher_request_free(req);
161error_free_tfm:
162 crypto_free_akcipher(tfm);
163 pr_devel("<==%s() = %d\n", __func__, ret);
164 return ret;
David Howells3d167d62013-08-30 16:15:30 +0100165}
166EXPORT_SYMBOL_GPL(public_key_verify_signature);
167
168static int public_key_verify_signature_2(const struct key *key,
169 const struct public_key_signature *sig)
170{
David Howells146aa8b2015-10-21 14:04:48 +0100171 const struct public_key *pk = key->payload.data[asym_crypto];
David Howells3d167d62013-08-30 16:15:30 +0100172 return public_key_verify_signature(pk, sig);
David Howellsa9681bf2012-09-21 23:24:55 +0100173}
174
175/*
176 * Public key algorithm asymmetric key subtype
177 */
178struct asymmetric_key_subtype public_key_subtype = {
179 .owner = THIS_MODULE,
180 .name = "public_key",
David Howells876c6e32014-09-02 13:52:10 +0100181 .name_len = sizeof("public_key") - 1,
David Howellsa9681bf2012-09-21 23:24:55 +0100182 .describe = public_key_describe,
183 .destroy = public_key_destroy,
David Howells3d167d62013-08-30 16:15:30 +0100184 .verify_signature = public_key_verify_signature_2,
David Howellsa9681bf2012-09-21 23:24:55 +0100185};
186EXPORT_SYMBOL_GPL(public_key_subtype);