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 <linux/scatterlist.h>
21 #include <keys/asymmetric-subtype.h>
22 #include <crypto/public_key.h>
23 #include <crypto/akcipher.h>
25 MODULE_LICENSE("GPL");
28 * Provide a part of a description of the key for /proc/keys.
30 static void public_key_describe(const struct key
*asymmetric_key
,
33 struct public_key
*key
= asymmetric_key
->payload
.data
[asym_crypto
];
36 seq_printf(m
, "%s.%s", key
->id_type
, key
->pkey_algo
);
40 * Destroy a public key algorithm key.
42 void public_key_destroy(void *payload
)
44 struct public_key
*key
= payload
;
50 EXPORT_SYMBOL_GPL(public_key_destroy
);
52 struct public_key_completion
{
53 struct completion completion
;
57 static void public_key_verify_done(struct crypto_async_request
*req
, int err
)
59 struct public_key_completion
*compl = req
->data
;
61 if (err
== -EINPROGRESS
)
65 complete(&compl->completion
);
69 * Verify a signature using a public key.
71 int public_key_verify_signature(const struct public_key
*pkey
,
72 const struct public_key_signature
*sig
)
74 struct public_key_completion
compl;
75 struct crypto_akcipher
*tfm
;
76 struct akcipher_request
*req
;
77 struct scatterlist sig_sg
, digest_sg
;
79 char alg_name_buf
[CRYPTO_MAX_ALG_NAME
];
84 pr_devel("==>%s()\n", __func__
);
91 alg_name
= sig
->pkey_algo
;
92 if (strcmp(sig
->pkey_algo
, "rsa") == 0) {
93 /* The data wangled by the RSA algorithm is typically padded
94 * and encoded in some manner, such as EMSA-PKCS1-1_5 [RFC3447
97 if (snprintf(alg_name_buf
, CRYPTO_MAX_ALG_NAME
,
98 "pkcs1pad(rsa,%s)", sig
->hash_algo
99 ) >= CRYPTO_MAX_ALG_NAME
)
101 alg_name
= alg_name_buf
;
104 tfm
= crypto_alloc_akcipher(alg_name
, 0, 0);
108 req
= akcipher_request_alloc(tfm
, GFP_KERNEL
);
112 ret
= crypto_akcipher_set_pub_key(tfm
, pkey
->key
, pkey
->keylen
);
116 outlen
= crypto_akcipher_maxsize(tfm
);
117 output
= kmalloc(outlen
, GFP_KERNEL
);
121 sg_init_one(&sig_sg
, sig
->s
, sig
->s_size
);
122 sg_init_one(&digest_sg
, output
, outlen
);
123 akcipher_request_set_crypt(req
, &sig_sg
, &digest_sg
, sig
->s_size
,
125 init_completion(&compl.completion
);
126 akcipher_request_set_callback(req
, CRYPTO_TFM_REQ_MAY_BACKLOG
|
127 CRYPTO_TFM_REQ_MAY_SLEEP
,
128 public_key_verify_done
, &compl);
130 /* Perform the verification calculation. This doesn't actually do the
131 * verification, but rather calculates the hash expected by the
132 * signature and returns that to us.
134 ret
= crypto_akcipher_verify(req
);
135 if (ret
== -EINPROGRESS
) {
136 wait_for_completion(&compl.completion
);
140 goto out_free_output
;
142 /* Do the actual verification step. */
143 if (req
->dst_len
!= sig
->digest_size
||
144 memcmp(sig
->digest
, output
, sig
->digest_size
) != 0)
150 akcipher_request_free(req
);
152 crypto_free_akcipher(tfm
);
153 pr_devel("<==%s() = %d\n", __func__
, ret
);
156 EXPORT_SYMBOL_GPL(public_key_verify_signature
);
158 static int public_key_verify_signature_2(const struct key
*key
,
159 const struct public_key_signature
*sig
)
161 const struct public_key
*pk
= key
->payload
.data
[asym_crypto
];
162 return public_key_verify_signature(pk
, sig
);
166 * Public key algorithm asymmetric key subtype
168 struct asymmetric_key_subtype public_key_subtype
= {
169 .owner
= THIS_MODULE
,
170 .name
= "public_key",
171 .name_len
= sizeof("public_key") - 1,
172 .describe
= public_key_describe
,
173 .destroy
= public_key_destroy
,
174 .verify_signature
= public_key_verify_signature_2
,
176 EXPORT_SYMBOL_GPL(public_key_subtype
);