crypto: Add hash param to pkcs1pad
[linux-2.6-block.git] / crypto / asymmetric_keys / public_key.c
CommitLineData
a9681bf3
DH
1/* 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>
db6c43bd 21#include <crypto/public_key.h>
a9681bf3
DH
22
23MODULE_LICENSE("GPL");
24
9abc4e66 25const char *const pkey_algo_name[PKEY_ALGO__LAST] = {
db6c43bd
TS
26 [PKEY_ALGO_DSA] = "dsa",
27 [PKEY_ALGO_RSA] = "rsa",
a9681bf3 28};
9abc4e66 29EXPORT_SYMBOL_GPL(pkey_algo_name);
a9681bf3 30
9abc4e66 31const char *const pkey_id_type_name[PKEY_ID_TYPE__LAST] = {
a9681bf3
DH
32 [PKEY_ID_PGP] = "PGP",
33 [PKEY_ID_X509] = "X509",
f29299b4 34 [PKEY_ID_PKCS7] = "PKCS#7",
a9681bf3 35};
9abc4e66 36EXPORT_SYMBOL_GPL(pkey_id_type_name);
a9681bf3 37
db6c43bd
TS
38static int (*alg_verify[PKEY_ALGO__LAST])(const struct public_key *pkey,
39 const struct public_key_signature *sig) = {
40 NULL,
41 rsa_verify_signature
42};
43
a9681bf3
DH
44/*
45 * Provide a part of a description of the key for /proc/keys.
46 */
47static void public_key_describe(const struct key *asymmetric_key,
48 struct seq_file *m)
49{
146aa8b1 50 struct public_key *key = asymmetric_key->payload.data[asym_crypto];
a9681bf3
DH
51
52 if (key)
53 seq_printf(m, "%s.%s",
db6c43bd
TS
54 pkey_id_type_name[key->id_type],
55 pkey_algo_name[key->pkey_algo]);
a9681bf3
DH
56}
57
58/*
59 * Destroy a public key algorithm key.
60 */
61void public_key_destroy(void *payload)
62{
63 struct public_key *key = payload;
a9681bf3 64
db6c43bd
TS
65 if (key)
66 kfree(key->key);
67 kfree(key);
a9681bf3
DH
68}
69EXPORT_SYMBOL_GPL(public_key_destroy);
70
71/*
72 * Verify a signature using a public key.
73 */
db6c43bd 74int public_key_verify_signature(const struct public_key *pkey,
3d167d68 75 const struct public_key_signature *sig)
a9681bf3 76{
db6c43bd 77 BUG_ON(!pkey);
3d167d68
DH
78 BUG_ON(!sig);
79 BUG_ON(!sig->digest);
db6c43bd 80 BUG_ON(!sig->s);
a9681bf3 81
db6c43bd
TS
82 if (pkey->pkey_algo >= PKEY_ALGO__LAST)
83 return -ENOPKG;
a9681bf3 84
db6c43bd
TS
85 if (!alg_verify[pkey->pkey_algo])
86 return -ENOPKG;
a9681bf3 87
db6c43bd 88 return alg_verify[pkey->pkey_algo](pkey, sig);
3d167d68
DH
89}
90EXPORT_SYMBOL_GPL(public_key_verify_signature);
91
92static int public_key_verify_signature_2(const struct key *key,
93 const struct public_key_signature *sig)
94{
146aa8b1 95 const struct public_key *pk = key->payload.data[asym_crypto];
3d167d68 96 return public_key_verify_signature(pk, sig);
a9681bf3
DH
97}
98
99/*
100 * Public key algorithm asymmetric key subtype
101 */
102struct asymmetric_key_subtype public_key_subtype = {
103 .owner = THIS_MODULE,
104 .name = "public_key",
876c6e3e 105 .name_len = sizeof("public_key") - 1,
a9681bf3
DH
106 .describe = public_key_describe,
107 .destroy = public_key_destroy,
3d167d68 108 .verify_signature = public_key_verify_signature_2,
a9681bf3
DH
109};
110EXPORT_SYMBOL_GPL(public_key_subtype);