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_free(struct public_key
*key
)
49 EXPORT_SYMBOL_GPL(public_key_free
);
52 * Destroy a public key algorithm key.
54 static void public_key_destroy(void *payload0
, void *payload3
)
56 public_key_free(payload0
);
57 public_key_signature_free(payload3
);
61 * Determine the crypto algorithm name.
64 int software_key_determine_akcipher(const char *encoding
,
65 const char *hash_algo
,
66 const struct public_key
*pkey
,
67 char alg_name
[CRYPTO_MAX_ALG_NAME
])
71 if (strcmp(encoding
, "pkcs1") == 0) {
72 /* The data wangled by the RSA algorithm is typically padded
73 * and encoded in some manner, such as EMSA-PKCS1-1_5 [RFC3447
77 n
= snprintf(alg_name
, CRYPTO_MAX_ALG_NAME
,
81 n
= snprintf(alg_name
, CRYPTO_MAX_ALG_NAME
,
83 pkey
->pkey_algo
, hash_algo
);
84 return n
>= CRYPTO_MAX_ALG_NAME
? -EINVAL
: 0;
87 if (strcmp(encoding
, "raw") == 0) {
88 strcpy(alg_name
, pkey
->pkey_algo
);
96 * Query information about a key.
98 static int software_key_query(const struct kernel_pkey_params
*params
,
99 struct kernel_pkey_query
*info
)
101 struct crypto_akcipher
*tfm
;
102 struct public_key
*pkey
= params
->key
->payload
.data
[asym_crypto
];
103 char alg_name
[CRYPTO_MAX_ALG_NAME
];
106 ret
= software_key_determine_akcipher(params
->encoding
,
112 tfm
= crypto_alloc_akcipher(alg_name
, 0, 0);
116 if (pkey
->key_is_private
)
117 ret
= crypto_akcipher_set_priv_key(tfm
,
118 pkey
->key
, pkey
->keylen
);
120 ret
= crypto_akcipher_set_pub_key(tfm
,
121 pkey
->key
, pkey
->keylen
);
125 len
= crypto_akcipher_maxsize(tfm
);
126 info
->key_size
= len
* 8;
127 info
->max_data_size
= len
;
128 info
->max_sig_size
= len
;
129 info
->max_enc_size
= len
;
130 info
->max_dec_size
= len
;
131 info
->supported_ops
= (KEYCTL_SUPPORTS_ENCRYPT
|
132 KEYCTL_SUPPORTS_VERIFY
);
133 if (pkey
->key_is_private
)
134 info
->supported_ops
|= (KEYCTL_SUPPORTS_DECRYPT
|
135 KEYCTL_SUPPORTS_SIGN
);
139 crypto_free_akcipher(tfm
);
140 pr_devel("<==%s() = %d\n", __func__
, ret
);
144 struct public_key_completion
{
145 struct completion completion
;
149 static void public_key_crypto_done(struct crypto_async_request
*req
, int err
)
151 struct public_key_completion
*compl = req
->data
;
153 if (err
== -EINPROGRESS
)
157 complete(&compl->completion
);
161 * Do encryption, decryption and signing ops.
163 static int software_key_eds_op(struct kernel_pkey_params
*params
,
164 const void *in
, void *out
)
166 struct public_key_completion
compl;
167 const struct public_key
*pkey
= params
->key
->payload
.data
[asym_crypto
];
168 struct akcipher_request
*req
;
169 struct crypto_akcipher
*tfm
;
170 struct scatterlist in_sg
, out_sg
;
171 char alg_name
[CRYPTO_MAX_ALG_NAME
];
174 pr_devel("==>%s()\n", __func__
);
176 ret
= software_key_determine_akcipher(params
->encoding
,
182 tfm
= crypto_alloc_akcipher(alg_name
, 0, 0);
186 req
= akcipher_request_alloc(tfm
, GFP_KERNEL
);
190 if (pkey
->key_is_private
)
191 ret
= crypto_akcipher_set_priv_key(tfm
,
192 pkey
->key
, pkey
->keylen
);
194 ret
= crypto_akcipher_set_pub_key(tfm
,
195 pkey
->key
, pkey
->keylen
);
199 sg_init_one(&in_sg
, in
, params
->in_len
);
200 sg_init_one(&out_sg
, out
, params
->out_len
);
201 akcipher_request_set_crypt(req
, &in_sg
, &out_sg
, params
->in_len
,
203 init_completion(&compl.completion
);
204 akcipher_request_set_callback(req
, CRYPTO_TFM_REQ_MAY_BACKLOG
|
205 CRYPTO_TFM_REQ_MAY_SLEEP
,
206 public_key_crypto_done
, &compl);
208 /* Perform the encryption calculation. */
209 switch (params
->op
) {
210 case kernel_pkey_encrypt
:
211 ret
= crypto_akcipher_encrypt(req
);
213 case kernel_pkey_decrypt
:
214 ret
= crypto_akcipher_decrypt(req
);
216 case kernel_pkey_sign
:
217 ret
= crypto_akcipher_sign(req
);
222 if (ret
== -EINPROGRESS
) {
223 wait_for_completion(&compl.completion
);
231 akcipher_request_free(req
);
233 crypto_free_akcipher(tfm
);
234 pr_devel("<==%s() = %d\n", __func__
, ret
);
239 * Verify a signature using a public key.
241 int public_key_verify_signature(const struct public_key
*pkey
,
242 const struct public_key_signature
*sig
)
244 struct public_key_completion
compl;
245 struct crypto_akcipher
*tfm
;
246 struct akcipher_request
*req
;
247 struct scatterlist sig_sg
, digest_sg
;
248 char alg_name
[CRYPTO_MAX_ALG_NAME
];
253 pr_devel("==>%s()\n", __func__
);
257 BUG_ON(!sig
->digest
);
260 ret
= software_key_determine_akcipher(sig
->encoding
,
266 tfm
= crypto_alloc_akcipher(alg_name
, 0, 0);
270 req
= akcipher_request_alloc(tfm
, GFP_KERNEL
);
274 if (pkey
->key_is_private
)
275 ret
= crypto_akcipher_set_priv_key(tfm
,
276 pkey
->key
, pkey
->keylen
);
278 ret
= crypto_akcipher_set_pub_key(tfm
,
279 pkey
->key
, pkey
->keylen
);
283 outlen
= crypto_akcipher_maxsize(tfm
);
284 output
= kmalloc(outlen
, GFP_KERNEL
);
288 sg_init_one(&sig_sg
, sig
->s
, sig
->s_size
);
289 sg_init_one(&digest_sg
, output
, outlen
);
290 akcipher_request_set_crypt(req
, &sig_sg
, &digest_sg
, sig
->s_size
,
292 init_completion(&compl.completion
);
293 akcipher_request_set_callback(req
, CRYPTO_TFM_REQ_MAY_BACKLOG
|
294 CRYPTO_TFM_REQ_MAY_SLEEP
,
295 public_key_crypto_done
, &compl);
297 /* Perform the verification calculation. This doesn't actually do the
298 * verification, but rather calculates the hash expected by the
299 * signature and returns that to us.
301 ret
= crypto_akcipher_verify(req
);
302 if (ret
== -EINPROGRESS
) {
303 wait_for_completion(&compl.completion
);
307 goto out_free_output
;
309 /* Do the actual verification step. */
310 if (req
->dst_len
!= sig
->digest_size
||
311 memcmp(sig
->digest
, output
, sig
->digest_size
) != 0)
317 akcipher_request_free(req
);
319 crypto_free_akcipher(tfm
);
320 pr_devel("<==%s() = %d\n", __func__
, ret
);
323 EXPORT_SYMBOL_GPL(public_key_verify_signature
);
325 static int public_key_verify_signature_2(const struct key
*key
,
326 const struct public_key_signature
*sig
)
328 const struct public_key
*pk
= key
->payload
.data
[asym_crypto
];
329 return public_key_verify_signature(pk
, sig
);
333 * Public key algorithm asymmetric key subtype
335 struct asymmetric_key_subtype public_key_subtype
= {
336 .owner
= THIS_MODULE
,
337 .name
= "public_key",
338 .name_len
= sizeof("public_key") - 1,
339 .describe
= public_key_describe
,
340 .destroy
= public_key_destroy
,
341 .query
= software_key_query
,
342 .eds_op
= software_key_eds_op
,
343 .verify_signature
= public_key_verify_signature_2
,
345 EXPORT_SYMBOL_GPL(public_key_subtype
);