1 /* In-software asymmetric public-key crypto subtype
3 * See Documentation/crypto/asymmetric-keys.txt
5 * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
6 * Written by David Howells (dhowells@redhat.com)
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.
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 <crypto/public_key.h>
23 MODULE_LICENSE("GPL");
25 const char *const pkey_algo_name[PKEY_ALGO__LAST] = {
26 [PKEY_ALGO_DSA] = "dsa",
27 [PKEY_ALGO_RSA] = "rsa",
29 EXPORT_SYMBOL_GPL(pkey_algo_name);
31 const char *const pkey_id_type_name[PKEY_ID_TYPE__LAST] = {
32 [PKEY_ID_PGP] = "PGP",
33 [PKEY_ID_X509] = "X509",
34 [PKEY_ID_PKCS7] = "PKCS#7",
36 EXPORT_SYMBOL_GPL(pkey_id_type_name);
38 static int (*alg_verify[PKEY_ALGO__LAST])(const struct public_key *pkey,
39 const struct public_key_signature *sig) = {
45 * Provide a part of a description of the key for /proc/keys.
47 static void public_key_describe(const struct key *asymmetric_key,
50 struct public_key *key = asymmetric_key->payload.data[asym_crypto];
53 seq_printf(m, "%s.%s",
54 pkey_id_type_name[key->id_type],
55 pkey_algo_name[key->pkey_algo]);
59 * Destroy a public key algorithm key.
61 void public_key_destroy(void *payload)
63 struct public_key *key = payload;
69 EXPORT_SYMBOL_GPL(public_key_destroy);
72 * Verify a signature using a public key.
74 int public_key_verify_signature(const struct public_key *pkey,
75 const struct public_key_signature *sig)
82 if (pkey->pkey_algo >= PKEY_ALGO__LAST)
85 if (!alg_verify[pkey->pkey_algo])
88 return alg_verify[pkey->pkey_algo](pkey, sig);
90 EXPORT_SYMBOL_GPL(public_key_verify_signature);
92 static int public_key_verify_signature_2(const struct key *key,
93 const struct public_key_signature *sig)
95 const struct public_key *pk = key->payload.data[asym_crypto];
96 return public_key_verify_signature(pk, sig);
100 * Public key algorithm asymmetric key subtype
102 struct asymmetric_key_subtype public_key_subtype = {
103 .owner = THIS_MODULE,
104 .name = "public_key",
105 .name_len = sizeof("public_key") - 1,
106 .describe = public_key_describe,
107 .destroy = public_key_destroy,
108 .verify_signature = public_key_verify_signature_2,
110 EXPORT_SYMBOL_GPL(public_key_subtype);