1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Signature verification with an asymmetric key
4 * See Documentation/crypto/asymmetric-keys.rst
6 * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
7 * Written by David Howells (dhowells@redhat.com)
10 #define pr_fmt(fmt) "SIG: "fmt
11 #include <keys/asymmetric-subtype.h>
12 #include <linux/export.h>
13 #include <linux/err.h>
14 #include <linux/slab.h>
15 #include <linux/keyctl.h>
16 #include <crypto/public_key.h>
17 #include <keys/user-type.h>
18 #include "asymmetric_keys.h"
21 * Destroy a public key signature.
23 void public_key_signature_free(struct public_key_signature *sig)
28 for (i = 0; i < ARRAY_SIZE(sig->auth_ids); i++)
29 kfree(sig->auth_ids[i]);
35 EXPORT_SYMBOL_GPL(public_key_signature_free);
38 * query_asymmetric_key - Get information about an aymmetric key.
39 * @params: Various parameters.
40 * @info: Where to put the information.
42 int query_asymmetric_key(const struct kernel_pkey_params *params,
43 struct kernel_pkey_query *info)
45 const struct asymmetric_key_subtype *subtype;
46 struct key *key = params->key;
49 pr_devel("==>%s()\n", __func__);
51 if (key->type != &key_type_asymmetric)
53 subtype = asymmetric_key_subtype(key);
55 !key->payload.data[0])
60 ret = subtype->query(params, info);
62 pr_devel("<==%s() = %d\n", __func__, ret);
65 EXPORT_SYMBOL_GPL(query_asymmetric_key);
68 * encrypt_blob - Encrypt data using an asymmetric key
69 * @params: Various parameters
70 * @data: Data blob to be encrypted, length params->data_len
71 * @enc: Encrypted data buffer, length params->enc_len
73 * Encrypt the specified data blob using the private key specified by
74 * params->key. The encrypted data is wrapped in an encoding if
75 * params->encoding is specified (eg. "pkcs1").
77 * Returns the length of the data placed in the encrypted data buffer or an
80 int encrypt_blob(struct kernel_pkey_params *params,
81 const void *data, void *enc)
83 params->op = kernel_pkey_encrypt;
84 return asymmetric_key_eds_op(params, data, enc);
86 EXPORT_SYMBOL_GPL(encrypt_blob);
89 * decrypt_blob - Decrypt data using an asymmetric key
90 * @params: Various parameters
91 * @enc: Encrypted data to be decrypted, length params->enc_len
92 * @data: Decrypted data buffer, length params->data_len
94 * Decrypt the specified data blob using the private key specified by
95 * params->key. The decrypted data is wrapped in an encoding if
96 * params->encoding is specified (eg. "pkcs1").
98 * Returns the length of the data placed in the decrypted data buffer or an
101 int decrypt_blob(struct kernel_pkey_params *params,
102 const void *enc, void *data)
104 params->op = kernel_pkey_decrypt;
105 return asymmetric_key_eds_op(params, enc, data);
107 EXPORT_SYMBOL_GPL(decrypt_blob);
110 * create_signature - Sign some data using an asymmetric key
111 * @params: Various parameters
112 * @data: Data blob to be signed, length params->data_len
113 * @enc: Signature buffer, length params->enc_len
115 * Sign the specified data blob using the private key specified by params->key.
116 * The signature is wrapped in an encoding if params->encoding is specified
117 * (eg. "pkcs1"). If the encoding needs to know the digest type, this can be
118 * passed through params->hash_algo (eg. "sha1").
120 * Returns the length of the data placed in the signature buffer or an error.
122 int create_signature(struct kernel_pkey_params *params,
123 const void *data, void *enc)
125 params->op = kernel_pkey_sign;
126 return asymmetric_key_eds_op(params, data, enc);
128 EXPORT_SYMBOL_GPL(create_signature);
131 * verify_signature - Initiate the use of an asymmetric key to verify a signature
132 * @key: The asymmetric key to verify against
133 * @sig: The signature to check
135 * Returns 0 if successful or else an error.
137 int verify_signature(const struct key *key,
138 const struct public_key_signature *sig)
140 const struct asymmetric_key_subtype *subtype;
143 pr_devel("==>%s()\n", __func__);
145 if (key->type != &key_type_asymmetric)
147 subtype = asymmetric_key_subtype(key);
149 !key->payload.data[0])
151 if (!subtype->verify_signature)
154 ret = subtype->verify_signature(key, sig);
156 pr_devel("<==%s() = %d\n", __func__, ret);
159 EXPORT_SYMBOL_GPL(verify_signature);