1b4d0d230SThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-or-later */
2a9681bf3SDavid Howells /* Asymmetric public-key algorithm definitions
3a9681bf3SDavid Howells *
40efaaa86SMauro Carvalho Chehab * See Documentation/crypto/asymmetric-keys.rst
5a9681bf3SDavid Howells *
6a9681bf3SDavid Howells * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
7a9681bf3SDavid Howells * Written by David Howells (dhowells@redhat.com)
8a9681bf3SDavid Howells */
9a9681bf3SDavid Howells
10a9681bf3SDavid Howells #ifndef _LINUX_PUBLIC_KEY_H
11a9681bf3SDavid Howells #define _LINUX_PUBLIC_KEY_H
12a9681bf3SDavid Howells
135a307718SDavid Howells #include <linux/keyctl.h>
14f1774cb8SVitaly Chikunov #include <linux/oid_registry.h>
155a307718SDavid Howells
16a9681bf3SDavid Howells /*
17a9681bf3SDavid Howells * Cryptographic data for the public-key subtype of the asymmetric key type.
18a9681bf3SDavid Howells *
19a9681bf3SDavid Howells * Note that this may include private part of the key as well as the public
20a9681bf3SDavid Howells * part.
21a9681bf3SDavid Howells */
22a9681bf3SDavid Howells struct public_key {
23db6c43bdSTadeusz Struk void *key;
24db6c43bdSTadeusz Struk u32 keylen;
25f1774cb8SVitaly Chikunov enum OID algo;
26f1774cb8SVitaly Chikunov void *params;
27f1774cb8SVitaly Chikunov u32 paramlen;
28f7c4e06eSDavid Howells bool key_is_private;
294e8ae72aSDavid Howells const char *id_type;
304e8ae72aSDavid Howells const char *pkey_algo;
3130eae2b0SEric Snowberg unsigned long key_eflags; /* key extension flags */
3230eae2b0SEric Snowberg #define KEY_EFLAG_CA 0 /* set if the CA basic constraints is set */
3356767128SEric Snowberg #define KEY_EFLAG_DIGITALSIG 1 /* set if the digitalSignature usage is set */
3456767128SEric Snowberg #define KEY_EFLAG_KEYCERTSIGN 2 /* set if the keyCertSign usage is set */
35a9681bf3SDavid Howells };
36a9681bf3SDavid Howells
373b764563SDavid Howells extern void public_key_free(struct public_key *key);
38a9681bf3SDavid Howells
39a9681bf3SDavid Howells /*
40a9681bf3SDavid Howells * Public key cryptography signature data
41a9681bf3SDavid Howells */
42a9681bf3SDavid Howells struct public_key_signature {
437d30198eSAndrew Zaborowski struct asymmetric_key_id *auth_ids[3];
44db6c43bdSTadeusz Struk u8 *s; /* Signature */
45a9681bf3SDavid Howells u8 *digest;
46f985911bSzhenwei pi u32 s_size; /* Number of bytes in signature */
47f985911bSzhenwei pi u32 digest_size; /* Number of bytes in digest */
484e8ae72aSDavid Howells const char *pkey_algo;
494e8ae72aSDavid Howells const char *hash_algo;
505a307718SDavid Howells const char *encoding;
51a9681bf3SDavid Howells };
52a9681bf3SDavid Howells
533b764563SDavid Howells extern void public_key_signature_free(struct public_key_signature *sig);
543b764563SDavid Howells
55db6c43bdSTadeusz Struk extern struct asymmetric_key_subtype public_key_subtype;
563b764563SDavid Howells
574ae71c1dSDavid Howells struct key;
58a511e1afSDavid Howells struct key_type;
59a511e1afSDavid Howells union key_payload;
60a511e1afSDavid Howells
61aaf66c88SMat Martineau extern int restrict_link_by_signature(struct key *dest_keyring,
62a511e1afSDavid Howells const struct key_type *type,
63aaf66c88SMat Martineau const union key_payload *payload,
64aaf66c88SMat Martineau struct key *trust_keyring);
65a511e1afSDavid Howells
667e3c4d22SMat Martineau extern int restrict_link_by_key_or_keyring(struct key *dest_keyring,
677e3c4d22SMat Martineau const struct key_type *type,
687e3c4d22SMat Martineau const union key_payload *payload,
697e3c4d22SMat Martineau struct key *trusted);
707e3c4d22SMat Martineau
718e323a02SMat Martineau extern int restrict_link_by_key_or_keyring_chain(struct key *trust_keyring,
728e323a02SMat Martineau const struct key_type *type,
738e323a02SMat Martineau const union key_payload *payload,
748e323a02SMat Martineau struct key *trusted);
758e323a02SMat Martineau
7676adb2fbSEric Snowberg #if IS_REACHABLE(CONFIG_ASYMMETRIC_KEY_TYPE)
7776adb2fbSEric Snowberg extern int restrict_link_by_ca(struct key *dest_keyring,
7876adb2fbSEric Snowberg const struct key_type *type,
7976adb2fbSEric Snowberg const union key_payload *payload,
8076adb2fbSEric Snowberg struct key *trust_keyring);
81*4cfb9080SEric Snowberg int restrict_link_by_digsig(struct key *dest_keyring,
82*4cfb9080SEric Snowberg const struct key_type *type,
83*4cfb9080SEric Snowberg const union key_payload *payload,
84*4cfb9080SEric Snowberg struct key *trust_keyring);
8576adb2fbSEric Snowberg #else
restrict_link_by_ca(struct key * dest_keyring,const struct key_type * type,const union key_payload * payload,struct key * trust_keyring)8676adb2fbSEric Snowberg static inline int restrict_link_by_ca(struct key *dest_keyring,
8776adb2fbSEric Snowberg const struct key_type *type,
8876adb2fbSEric Snowberg const union key_payload *payload,
8976adb2fbSEric Snowberg struct key *trust_keyring)
9076adb2fbSEric Snowberg {
9176adb2fbSEric Snowberg return 0;
9276adb2fbSEric Snowberg }
93*4cfb9080SEric Snowberg
restrict_link_by_digsig(struct key * dest_keyring,const struct key_type * type,const union key_payload * payload,struct key * trust_keyring)94*4cfb9080SEric Snowberg static inline int restrict_link_by_digsig(struct key *dest_keyring,
95*4cfb9080SEric Snowberg const struct key_type *type,
96*4cfb9080SEric Snowberg const union key_payload *payload,
97*4cfb9080SEric Snowberg struct key *trust_keyring)
98*4cfb9080SEric Snowberg {
99*4cfb9080SEric Snowberg return 0;
100*4cfb9080SEric Snowberg }
10176adb2fbSEric Snowberg #endif
10276adb2fbSEric Snowberg
1035a307718SDavid Howells extern int query_asymmetric_key(const struct kernel_pkey_params *,
1045a307718SDavid Howells struct kernel_pkey_query *);
1055a307718SDavid Howells
1065a307718SDavid Howells extern int encrypt_blob(struct kernel_pkey_params *, const void *, void *);
1075a307718SDavid Howells extern int decrypt_blob(struct kernel_pkey_params *, const void *, void *);
1085a307718SDavid Howells extern int create_signature(struct kernel_pkey_params *, const void *, void *);
1095a307718SDavid Howells extern int verify_signature(const struct key *,
1105a307718SDavid Howells const struct public_key_signature *);
1114ae71c1dSDavid Howells
1127f8da991SEric Snowberg #if IS_REACHABLE(CONFIG_ASYMMETRIC_PUBLIC_KEY_SUBTYPE)
113db6c43bdSTadeusz Struk int public_key_verify_signature(const struct public_key *pkey,
114db6c43bdSTadeusz Struk const struct public_key_signature *sig);
1157f8da991SEric Snowberg #else
1167f8da991SEric Snowberg static inline
public_key_verify_signature(const struct public_key * pkey,const struct public_key_signature * sig)1177f8da991SEric Snowberg int public_key_verify_signature(const struct public_key *pkey,
1187f8da991SEric Snowberg const struct public_key_signature *sig)
1197f8da991SEric Snowberg {
1207f8da991SEric Snowberg return -EINVAL;
1217f8da991SEric Snowberg }
1227f8da991SEric Snowberg #endif
123db6c43bdSTadeusz Struk
124a9681bf3SDavid Howells #endif /* _LINUX_PUBLIC_KEY_H */
125