blob: e929fe1e4106c7dfaff7c2bcf3186952f449b764 [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
David Howells1e684d32017-11-15 16:38:45 +000025MODULE_DESCRIPTION("In-software asymmetric public-key subtype");
26MODULE_AUTHOR("Red Hat, Inc.");
David Howellsa9681bf2012-09-21 23:24:55 +010027MODULE_LICENSE("GPL");
28
David Howellsa9681bf2012-09-21 23:24:55 +010029/*
30 * Provide a part of a description of the key for /proc/keys.
31 */
32static void public_key_describe(const struct key *asymmetric_key,
33 struct seq_file *m)
34{
David Howells146aa8b2015-10-21 14:04:48 +010035 struct public_key *key = asymmetric_key->payload.data[asym_crypto];
David Howellsa9681bf2012-09-21 23:24:55 +010036
37 if (key)
David Howells4e8ae722016-03-03 21:49:27 +000038 seq_printf(m, "%s.%s", key->id_type, key->pkey_algo);
David Howellsa9681bf2012-09-21 23:24:55 +010039}
40
41/*
42 * Destroy a public key algorithm key.
43 */
David Howells3b764562016-04-06 16:13:33 +010044void public_key_free(struct public_key *key)
David Howellsa9681bf2012-09-21 23:24:55 +010045{
David Howells3b764562016-04-06 16:13:33 +010046 if (key) {
Tadeusz Strukdb6c43b2016-02-02 10:08:53 -080047 kfree(key->key);
David Howells3b764562016-04-06 16:13:33 +010048 kfree(key);
49 }
David Howellsa9681bf2012-09-21 23:24:55 +010050}
David Howells3b764562016-04-06 16:13:33 +010051EXPORT_SYMBOL_GPL(public_key_free);
52
53/*
54 * Destroy a public key algorithm key.
55 */
56static void public_key_destroy(void *payload0, void *payload3)
57{
58 public_key_free(payload0);
59 public_key_signature_free(payload3);
60}
David Howellsa9681bf2012-09-21 23:24:55 +010061
62/*
63 * Verify a signature using a public key.
64 */
Tadeusz Strukdb6c43b2016-02-02 10:08:53 -080065int public_key_verify_signature(const struct public_key *pkey,
David Howells3d167d62013-08-30 16:15:30 +010066 const struct public_key_signature *sig)
David Howellsa9681bf2012-09-21 23:24:55 +010067{
Gilad Ben-Yossef0ca2a042017-10-18 08:00:40 +010068 struct crypto_wait cwait;
David Howellsd43de6c2016-03-03 21:49:27 +000069 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;
Eric Biggers72f9a072017-12-08 15:13:29 +000076 int ret;
David Howellsd43de6c2016-03-03 21:49:27 +000077
78 pr_devel("==>%s()\n", __func__);
79
Tadeusz Strukdb6c43b2016-02-02 10:08:53 -080080 BUG_ON(!pkey);
David Howells3d167d62013-08-30 16:15:30 +010081 BUG_ON(!sig);
Tadeusz Strukdb6c43b2016-02-02 10:08:53 -080082 BUG_ON(!sig->s);
David Howells3d167d62013-08-30 16:15:30 +010083
Eric Biggers437499e2018-02-22 14:38:33 +000084 if (!sig->digest)
85 return -ENOPKG;
86
David Howells4e8ae722016-03-03 21:49:27 +000087 alg_name = sig->pkey_algo;
88 if (strcmp(sig->pkey_algo, "rsa") == 0) {
David Howellsd43de6c2016-03-03 21:49:27 +000089 /* The data wangled by the RSA algorithm is typically padded
90 * and encoded in some manner, such as EMSA-PKCS1-1_5 [RFC3447
91 * sec 8.2].
92 */
93 if (snprintf(alg_name_buf, CRYPTO_MAX_ALG_NAME,
David Howells4e8ae722016-03-03 21:49:27 +000094 "pkcs1pad(rsa,%s)", sig->hash_algo
David Howellsd43de6c2016-03-03 21:49:27 +000095 ) >= CRYPTO_MAX_ALG_NAME)
96 return -EINVAL;
97 alg_name = alg_name_buf;
98 }
David Howells3d167d62013-08-30 16:15:30 +010099
David Howellsd43de6c2016-03-03 21:49:27 +0000100 tfm = crypto_alloc_akcipher(alg_name, 0, 0);
101 if (IS_ERR(tfm))
102 return PTR_ERR(tfm);
David Howellsa9681bf2012-09-21 23:24:55 +0100103
Eric Biggers72f9a072017-12-08 15:13:29 +0000104 ret = -ENOMEM;
David Howellsd43de6c2016-03-03 21:49:27 +0000105 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
Pan Bianfbb72632016-12-13 09:26:18 +0000113 ret = -ENOMEM;
David Howellsd43de6c2016-03-03 21:49:27 +0000114 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);
Gilad Ben-Yossef0ca2a042017-10-18 08:00:40 +0100123 crypto_init_wait(&cwait);
David Howellsd43de6c2016-03-03 21:49:27 +0000124 akcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG |
125 CRYPTO_TFM_REQ_MAY_SLEEP,
Gilad Ben-Yossef0ca2a042017-10-18 08:00:40 +0100126 crypto_req_done, &cwait);
David Howellsd43de6c2016-03-03 21:49:27 +0000127
128 /* Perform the verification calculation. This doesn't actually do the
129 * verification, but rather calculates the hash expected by the
130 * signature and returns that to us.
131 */
Gilad Ben-Yossef0ca2a042017-10-18 08:00:40 +0100132 ret = crypto_wait_req(crypto_akcipher_verify(req), &cwait);
Eric Biggers72f9a072017-12-08 15:13:29 +0000133 if (ret)
David Howellsd43de6c2016-03-03 21:49:27 +0000134 goto out_free_output;
135
136 /* Do the actual verification step. */
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);
Eric Biggers72f9a072017-12-08 15:13:29 +0000148 if (WARN_ON_ONCE(ret > 0))
149 ret = -EINVAL;
David Howellsd43de6c2016-03-03 21:49:27 +0000150 return ret;
David Howells3d167d62013-08-30 16:15:30 +0100151}
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{
David Howells146aa8b2015-10-21 14:04:48 +0100157 const struct public_key *pk = key->payload.data[asym_crypto];
David Howells3d167d62013-08-30 16:15:30 +0100158 return public_key_verify_signature(pk, sig);
David Howellsa9681bf2012-09-21 23:24:55 +0100159}
160
161/*
162 * Public key algorithm asymmetric key subtype
163 */
164struct asymmetric_key_subtype public_key_subtype = {
165 .owner = THIS_MODULE,
166 .name = "public_key",
David Howells876c6e32014-09-02 13:52:10 +0100167 .name_len = sizeof("public_key") - 1,
David Howellsa9681bf2012-09-21 23:24:55 +0100168 .describe = public_key_describe,
169 .destroy = public_key_destroy,
David Howells3d167d62013-08-30 16:15:30 +0100170 .verify_signature = public_key_verify_signature_2,
David Howellsa9681bf2012-09-21 23:24:55 +0100171};
172EXPORT_SYMBOL_GPL(public_key_subtype);