blob: 97eb001960b97774e89643f65711d2a68e6a3a8f [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>
20#include <keys/asymmetric-subtype.h>
21#include "public_key.h"
22
23MODULE_LICENSE("GPL");
24
David Howells9abc4e62013-08-30 16:15:10 +010025const char *const pkey_algo_name[PKEY_ALGO__LAST] = {
David Howellsa9681bf2012-09-21 23:24:55 +010026 [PKEY_ALGO_DSA] = "DSA",
27 [PKEY_ALGO_RSA] = "RSA",
28};
David Howells9abc4e62013-08-30 16:15:10 +010029EXPORT_SYMBOL_GPL(pkey_algo_name);
David Howellsa9681bf2012-09-21 23:24:55 +010030
David Howells206ce592013-08-30 16:15:18 +010031const struct public_key_algorithm *pkey_algo[PKEY_ALGO__LAST] = {
32#if defined(CONFIG_PUBLIC_KEY_ALGO_RSA) || \
33 defined(CONFIG_PUBLIC_KEY_ALGO_RSA_MODULE)
34 [PKEY_ALGO_RSA] = &RSA_public_key_algorithm,
35#endif
36};
37EXPORT_SYMBOL_GPL(pkey_algo);
38
David Howells9abc4e62013-08-30 16:15:10 +010039const char *const pkey_id_type_name[PKEY_ID_TYPE__LAST] = {
David Howellsa9681bf2012-09-21 23:24:55 +010040 [PKEY_ID_PGP] = "PGP",
41 [PKEY_ID_X509] = "X509",
42};
David Howells9abc4e62013-08-30 16:15:10 +010043EXPORT_SYMBOL_GPL(pkey_id_type_name);
David Howellsa9681bf2012-09-21 23:24:55 +010044
45/*
46 * Provide a part of a description of the key for /proc/keys.
47 */
48static void public_key_describe(const struct key *asymmetric_key,
49 struct seq_file *m)
50{
51 struct public_key *key = asymmetric_key->payload.data;
52
53 if (key)
54 seq_printf(m, "%s.%s",
David Howells9abc4e62013-08-30 16:15:10 +010055 pkey_id_type_name[key->id_type], key->algo->name);
David Howellsa9681bf2012-09-21 23:24:55 +010056}
57
58/*
59 * Destroy a public key algorithm key.
60 */
61void public_key_destroy(void *payload)
62{
63 struct public_key *key = payload;
64 int i;
65
66 if (key) {
67 for (i = 0; i < ARRAY_SIZE(key->mpi); i++)
68 mpi_free(key->mpi[i]);
69 kfree(key);
70 }
71}
72EXPORT_SYMBOL_GPL(public_key_destroy);
73
74/*
75 * Verify a signature using a public key.
76 */
David Howells3d167d62013-08-30 16:15:30 +010077int public_key_verify_signature(const struct public_key *pk,
78 const struct public_key_signature *sig)
David Howellsa9681bf2012-09-21 23:24:55 +010079{
David Howells3d167d62013-08-30 16:15:30 +010080 const struct public_key_algorithm *algo;
David Howellsa9681bf2012-09-21 23:24:55 +010081
David Howells3d167d62013-08-30 16:15:30 +010082 BUG_ON(!pk);
83 BUG_ON(!pk->mpi[0]);
84 BUG_ON(!pk->mpi[1]);
85 BUG_ON(!sig);
86 BUG_ON(!sig->digest);
87 BUG_ON(!sig->mpi[0]);
88
89 algo = pk->algo;
90 if (!algo) {
91 if (pk->pkey_algo >= PKEY_ALGO__LAST)
92 return -ENOPKG;
93 algo = pkey_algo[pk->pkey_algo];
94 if (!algo)
95 return -ENOPKG;
96 }
97
98 if (!algo->verify_signature)
David Howellsa9681bf2012-09-21 23:24:55 +010099 return -ENOTSUPP;
100
David Howells3d167d62013-08-30 16:15:30 +0100101 if (sig->nr_mpi != algo->n_sig_mpi) {
David Howellsa9681bf2012-09-21 23:24:55 +0100102 pr_debug("Signature has %u MPI not %u\n",
David Howells3d167d62013-08-30 16:15:30 +0100103 sig->nr_mpi, algo->n_sig_mpi);
David Howellsa9681bf2012-09-21 23:24:55 +0100104 return -EINVAL;
105 }
106
David Howells3d167d62013-08-30 16:15:30 +0100107 return algo->verify_signature(pk, sig);
108}
109EXPORT_SYMBOL_GPL(public_key_verify_signature);
110
111static int public_key_verify_signature_2(const struct key *key,
112 const struct public_key_signature *sig)
113{
114 const struct public_key *pk = key->payload.data;
115 return public_key_verify_signature(pk, sig);
David Howellsa9681bf2012-09-21 23:24:55 +0100116}
117
118/*
119 * Public key algorithm asymmetric key subtype
120 */
121struct asymmetric_key_subtype public_key_subtype = {
122 .owner = THIS_MODULE,
123 .name = "public_key",
124 .describe = public_key_describe,
125 .destroy = public_key_destroy,
David Howells3d167d62013-08-30 16:15:30 +0100126 .verify_signature = public_key_verify_signature_2,
David Howellsa9681bf2012-09-21 23:24:55 +0100127};
128EXPORT_SYMBOL_GPL(public_key_subtype);