Skip to content

Commit baa888d

Browse files
committed
Merge branch 'next-keys2' of git://git.kernel.org/pub/scm/linux/kernel/git/jmorris/linux-security
Pull keys updates from James Morris: "Provide five new operations in the key_type struct that can be used to provide access to asymmetric key operations. These will be implemented for the asymmetric key type in a later patch and may refer to a key retained in RAM by the kernel or a key retained in crypto hardware. int (*asym_query)(const struct kernel_pkey_params *params, struct kernel_pkey_query *info); int (*asym_eds_op)(struct kernel_pkey_params *params, const void *in, void *out); int (*asym_verify_signature)(struct kernel_pkey_params *params, const void *in, const void *in2); Since encrypt, decrypt and sign are identical in their interfaces, they're rolled together in the asym_eds_op() operation and there's an operation ID in the params argument to distinguish them. Verify is different in that we supply the data and the signature instead and get an error value (or 0) as the only result on the expectation that this may well be how a hardware crypto device may work" * 'next-keys2' of git://git.kernel.org/pub/scm/linux/kernel/git/jmorris/linux-security: (22 commits) KEYS: asym_tpm: Add support for the sign operation [ver raspberrypi#2] KEYS: asym_tpm: Implement tpm_sign [ver raspberrypi#2] KEYS: asym_tpm: Implement signature verification [ver raspberrypi#2] KEYS: asym_tpm: Implement the decrypt operation [ver raspberrypi#2] KEYS: asym_tpm: Implement tpm_unbind [ver raspberrypi#2] KEYS: asym_tpm: Add loadkey2 and flushspecific [ver raspberrypi#2] KEYS: Move trusted.h to include/keys [ver raspberrypi#2] KEYS: trusted: Expose common functionality [ver raspberrypi#2] KEYS: asym_tpm: Implement encryption operation [ver raspberrypi#2] KEYS: asym_tpm: Implement pkey_query [ver raspberrypi#2] KEYS: Add parser for TPM-based keys [ver raspberrypi#2] KEYS: asym_tpm: extract key size & public key [ver raspberrypi#2] KEYS: asym_tpm: add skeleton for asym_tpm [ver raspberrypi#2] crypto: rsa-pkcs1pad: Allow hash to be optional [ver raspberrypi#2] KEYS: Implement PKCS#8 RSA Private Key parser [ver raspberrypi#2] KEYS: Implement encrypt, decrypt and sign for software asymmetric key [ver raspberrypi#2] KEYS: Allow the public_key struct to hold a private key [ver raspberrypi#2] KEYS: Provide software public key query function [ver raspberrypi#2] KEYS: Make the X.509 and PKCS7 parsers supply the sig encoding type [ver raspberrypi#2] KEYS: Provide missing asymmetric key subops for new key type ops [ver raspberrypi#2] ...
2 parents 7260935 + 64ae16d commit baa888d

29 files changed

Lines changed: 2516 additions & 61 deletions

‎Documentation/crypto/asymmetric-keys.txt‎

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,10 @@ and looks like the following:
183183

184184
void (*describe)(const struct key *key, struct seq_file *m);
185185
void (*destroy)(void *payload);
186+
int (*query)(const struct kernel_pkey_params *params,
187+
struct kernel_pkey_query *info);
188+
int (*eds_op)(struct kernel_pkey_params *params,
189+
const void *in, void *out);
186190
int (*verify_signature)(const struct key *key,
187191
const struct public_key_signature *sig);
188192
};
@@ -207,12 +211,22 @@ There are a number of operations defined by the subtype:
207211
asymmetric key will look after freeing the fingerprint and releasing the
208212
reference on the subtype module.
209213

210-
(3) verify_signature().
214+
(3) query().
211215

212-
Optional. These are the entry points for the key usage operations.
213-
Currently there is only the one defined. If not set, the caller will be
214-
given -ENOTSUPP. The subtype may do anything it likes to implement an
215-
operation, including offloading to hardware.
216+
Mandatory. This is a function for querying the capabilities of a key.
217+
218+
(4) eds_op().
219+
220+
Optional. This is the entry point for the encryption, decryption and
221+
signature creation operations (which are distinguished by the operation ID
222+
in the parameter struct). The subtype may do anything it likes to
223+
implement an operation, including offloading to hardware.
224+
225+
(5) verify_signature().
226+
227+
Optional. This is the entry point for signature verification. The
228+
subtype may do anything it likes to implement an operation, including
229+
offloading to hardware.
216230

217231

218232
==========================
@@ -234,6 +248,8 @@ Examples of blob formats for which parsers could be implemented include:
234248
- X.509 ASN.1 stream.
235249
- Pointer to TPM key.
236250
- Pointer to UEFI key.
251+
- PKCS#8 private key [RFC 5208].
252+
- PKCS#5 encrypted private key [RFC 2898].
237253

238254
During key instantiation each parser in the list is tried until one doesn't
239255
return -EBADMSG.

‎Documentation/security/keys/core.rst‎

Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -859,6 +859,7 @@ The keyctl syscall functions are:
859859
and either the buffer length or the OtherInfo length exceeds the
860860
allowed length.
861861

862+
862863
* Restrict keyring linkage::
863864

864865
long keyctl(KEYCTL_RESTRICT_KEYRING, key_serial_t keyring,
@@ -890,6 +891,116 @@ The keyctl syscall functions are:
890891
applicable to the asymmetric key type.
891892

892893

894+
* Query an asymmetric key::
895+
896+
long keyctl(KEYCTL_PKEY_QUERY,
897+
key_serial_t key_id, unsigned long reserved,
898+
struct keyctl_pkey_query *info);
899+
900+
Get information about an asymmetric key. The information is returned in
901+
the keyctl_pkey_query struct::
902+
903+
__u32 supported_ops;
904+
__u32 key_size;
905+
__u16 max_data_size;
906+
__u16 max_sig_size;
907+
__u16 max_enc_size;
908+
__u16 max_dec_size;
909+
__u32 __spare[10];
910+
911+
``supported_ops`` contains a bit mask of flags indicating which ops are
912+
supported. This is constructed from a bitwise-OR of::
913+
914+
KEYCTL_SUPPORTS_{ENCRYPT,DECRYPT,SIGN,VERIFY}
915+
916+
``key_size`` indicated the size of the key in bits.
917+
918+
``max_*_size`` indicate the maximum sizes in bytes of a blob of data to be
919+
signed, a signature blob, a blob to be encrypted and a blob to be
920+
decrypted.
921+
922+
``__spare[]`` must be set to 0. This is intended for future use to hand
923+
over one or more passphrases needed unlock a key.
924+
925+
If successful, 0 is returned. If the key is not an asymmetric key,
926+
EOPNOTSUPP is returned.
927+
928+
929+
* Encrypt, decrypt, sign or verify a blob using an asymmetric key::
930+
931+
long keyctl(KEYCTL_PKEY_ENCRYPT,
932+
const struct keyctl_pkey_params *params,
933+
const char *info,
934+
const void *in,
935+
void *out);
936+
937+
long keyctl(KEYCTL_PKEY_DECRYPT,
938+
const struct keyctl_pkey_params *params,
939+
const char *info,
940+
const void *in,
941+
void *out);
942+
943+
long keyctl(KEYCTL_PKEY_SIGN,
944+
const struct keyctl_pkey_params *params,
945+
const char *info,
946+
const void *in,
947+
void *out);
948+
949+
long keyctl(KEYCTL_PKEY_VERIFY,
950+
const struct keyctl_pkey_params *params,
951+
const char *info,
952+
const void *in,
953+
const void *in2);
954+
955+
Use an asymmetric key to perform a public-key cryptographic operation a
956+
blob of data. For encryption and verification, the asymmetric key may
957+
only need the public parts to be available, but for decryption and signing
958+
the private parts are required also.
959+
960+
The parameter block pointed to by params contains a number of integer
961+
values::
962+
963+
__s32 key_id;
964+
__u32 in_len;
965+
__u32 out_len;
966+
__u32 in2_len;
967+
968+
``key_id`` is the ID of the asymmetric key to be used. ``in_len`` and
969+
``in2_len`` indicate the amount of data in the in and in2 buffers and
970+
``out_len`` indicates the size of the out buffer as appropriate for the
971+
above operations.
972+
973+
For a given operation, the in and out buffers are used as follows::
974+
975+
Operation ID in,in_len out,out_len in2,in2_len
976+
======================= =============== =============== ===============
977+
KEYCTL_PKEY_ENCRYPT Raw data Encrypted data -
978+
KEYCTL_PKEY_DECRYPT Encrypted data Raw data -
979+
KEYCTL_PKEY_SIGN Raw data Signature -
980+
KEYCTL_PKEY_VERIFY Raw data - Signature
981+
982+
``info`` is a string of key=value pairs that supply supplementary
983+
information. These include:
984+
985+
``enc=<encoding>`` The encoding of the encrypted/signature blob. This
986+
can be "pkcs1" for RSASSA-PKCS1-v1.5 or
987+
RSAES-PKCS1-v1.5; "pss" for "RSASSA-PSS"; "oaep" for
988+
"RSAES-OAEP". If omitted or is "raw", the raw output
989+
of the encryption function is specified.
990+
991+
``hash=<algo>`` If the data buffer contains the output of a hash
992+
function and the encoding includes some indication of
993+
which hash function was used, the hash function can be
994+
specified with this, eg. "hash=sha256".
995+
996+
The ``__spare[]`` space in the parameter block must be set to 0. This is
997+
intended, amongst other things, to allow the passing of passphrases
998+
required to unlock a key.
999+
1000+
If successful, encrypt, decrypt and sign all return the amount of data
1001+
written into the output buffer. Verification returns 0 on success.
1002+
1003+
8931004
Kernel Services
8941005
===============
8951006

@@ -1483,6 +1594,112 @@ The structure has a number of fields, some of which are mandatory:
14831594
attempted key link operation. If there is no match, -EINVAL is returned.
14841595

14851596

1597+
* ``int (*asym_eds_op)(struct kernel_pkey_params *params,
1598+
const void *in, void *out);``
1599+
``int (*asym_verify_signature)(struct kernel_pkey_params *params,
1600+
const void *in, const void *in2);``
1601+
1602+
These methods are optional. If provided the first allows a key to be
1603+
used to encrypt, decrypt or sign a blob of data, and the second allows a
1604+
key to verify a signature.
1605+
1606+
In all cases, the following information is provided in the params block::
1607+
1608+
struct kernel_pkey_params {
1609+
struct key *key;
1610+
const char *encoding;
1611+
const char *hash_algo;
1612+
char *info;
1613+
__u32 in_len;
1614+
union {
1615+
__u32 out_len;
1616+
__u32 in2_len;
1617+
};
1618+
enum kernel_pkey_operation op : 8;
1619+
};
1620+
1621+
This includes the key to be used; a string indicating the encoding to use
1622+
(for instance, "pkcs1" may be used with an RSA key to indicate
1623+
RSASSA-PKCS1-v1.5 or RSAES-PKCS1-v1.5 encoding or "raw" if no encoding);
1624+
the name of the hash algorithm used to generate the data for a signature
1625+
(if appropriate); the sizes of the input and output (or second input)
1626+
buffers; and the ID of the operation to be performed.
1627+
1628+
For a given operation ID, the input and output buffers are used as
1629+
follows::
1630+
1631+
Operation ID in,in_len out,out_len in2,in2_len
1632+
======================= =============== =============== ===============
1633+
kernel_pkey_encrypt Raw data Encrypted data -
1634+
kernel_pkey_decrypt Encrypted data Raw data -
1635+
kernel_pkey_sign Raw data Signature -
1636+
kernel_pkey_verify Raw data - Signature
1637+
1638+
asym_eds_op() deals with encryption, decryption and signature creation as
1639+
specified by params->op. Note that params->op is also set for
1640+
asym_verify_signature().
1641+
1642+
Encrypting and signature creation both take raw data in the input buffer
1643+
and return the encrypted result in the output buffer. Padding may have
1644+
been added if an encoding was set. In the case of signature creation,
1645+
depending on the encoding, the padding created may need to indicate the
1646+
digest algorithm - the name of which should be supplied in hash_algo.
1647+
1648+
Decryption takes encrypted data in the input buffer and returns the raw
1649+
data in the output buffer. Padding will get checked and stripped off if
1650+
an encoding was set.
1651+
1652+
Verification takes raw data in the input buffer and the signature in the
1653+
second input buffer and checks that the one matches the other. Padding
1654+
will be validated. Depending on the encoding, the digest algorithm used
1655+
to generate the raw data may need to be indicated in hash_algo.
1656+
1657+
If successful, asym_eds_op() should return the number of bytes written
1658+
into the output buffer. asym_verify_signature() should return 0.
1659+
1660+
A variety of errors may be returned, including EOPNOTSUPP if the operation
1661+
is not supported; EKEYREJECTED if verification fails; ENOPKG if the
1662+
required crypto isn't available.
1663+
1664+
1665+
* ``int (*asym_query)(const struct kernel_pkey_params *params,
1666+
struct kernel_pkey_query *info);``
1667+
1668+
This method is optional. If provided it allows information about the
1669+
public or asymmetric key held in the key to be determined.
1670+
1671+
The parameter block is as for asym_eds_op() and co. but in_len and out_len
1672+
are unused. The encoding and hash_algo fields should be used to reduce
1673+
the returned buffer/data sizes as appropriate.
1674+
1675+
If successful, the following information is filled in::
1676+
1677+
struct kernel_pkey_query {
1678+
__u32 supported_ops;
1679+
__u32 key_size;
1680+
__u16 max_data_size;
1681+
__u16 max_sig_size;
1682+
__u16 max_enc_size;
1683+
__u16 max_dec_size;
1684+
};
1685+
1686+
The supported_ops field will contain a bitmask indicating what operations
1687+
are supported by the key, including encryption of a blob, decryption of a
1688+
blob, signing a blob and verifying the signature on a blob. The following
1689+
constants are defined for this::
1690+
1691+
KEYCTL_SUPPORTS_{ENCRYPT,DECRYPT,SIGN,VERIFY}
1692+
1693+
The key_size field is the size of the key in bits. max_data_size and
1694+
max_sig_size are the maximum raw data and signature sizes for creation and
1695+
verification of a signature; max_enc_size and max_dec_size are the maximum
1696+
raw data and signature sizes for encryption and decryption. The
1697+
max_*_size fields are measured in bytes.
1698+
1699+
If successful, 0 will be returned. If the key doesn't support this,
1700+
EOPNOTSUPP will be returned.
1701+
1702+
14861703
Request-Key Callback Service
14871704
============================
14881705

‎crypto/asymmetric_keys/Kconfig‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,18 @@ config ASYMMETRIC_PUBLIC_KEY_SUBTYPE
2121
appropriate hash algorithms (such as SHA-1) must be available.
2222
ENOPKG will be reported if the requisite algorithm is unavailable.
2323

24+
config ASYMMETRIC_TPM_KEY_SUBTYPE
25+
tristate "Asymmetric TPM backed private key subtype"
26+
depends on TCG_TPM
27+
depends on TRUSTED_KEYS
28+
select CRYPTO_HMAC
29+
select CRYPTO_SHA1
30+
select CRYPTO_HASH_INFO
31+
help
32+
This option provides support for TPM backed private key type handling.
33+
Operations such as sign, verify, encrypt, decrypt are performed by
34+
the TPM after the private key is loaded.
35+
2436
config X509_CERTIFICATE_PARSER
2537
tristate "X.509 certificate parser"
2638
depends on ASYMMETRIC_PUBLIC_KEY_SUBTYPE
@@ -31,6 +43,25 @@ config X509_CERTIFICATE_PARSER
3143
data and provides the ability to instantiate a crypto key from a
3244
public key packet found inside the certificate.
3345

46+
config PKCS8_PRIVATE_KEY_PARSER
47+
tristate "PKCS#8 private key parser"
48+
depends on ASYMMETRIC_PUBLIC_KEY_SUBTYPE
49+
select ASN1
50+
select OID_REGISTRY
51+
help
52+
This option provides support for parsing PKCS#8 format blobs for
53+
private key data and provides the ability to instantiate a crypto key
54+
from that data.
55+
56+
config TPM_KEY_PARSER
57+
tristate "TPM private key parser"
58+
depends on ASYMMETRIC_TPM_KEY_SUBTYPE
59+
select ASN1
60+
help
61+
This option provides support for parsing TPM format blobs for
62+
private key data and provides the ability to instantiate a crypto key
63+
from that data.
64+
3465
config PKCS7_MESSAGE_PARSER
3566
tristate "PKCS#7 message parser"
3667
depends on X509_CERTIFICATE_PARSER

‎crypto/asymmetric_keys/Makefile‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ asymmetric_keys-y := \
1111
signature.o
1212

1313
obj-$(CONFIG_ASYMMETRIC_PUBLIC_KEY_SUBTYPE) += public_key.o
14+
obj-$(CONFIG_ASYMMETRIC_TPM_KEY_SUBTYPE) += asym_tpm.o
1415

1516
#
1617
# X.509 Certificate handling
@@ -29,6 +30,19 @@ $(obj)/x509_cert_parser.o: \
2930
$(obj)/x509.asn1.o: $(obj)/x509.asn1.c $(obj)/x509.asn1.h
3031
$(obj)/x509_akid.asn1.o: $(obj)/x509_akid.asn1.c $(obj)/x509_akid.asn1.h
3132

33+
#
34+
# PKCS#8 private key handling
35+
#
36+
obj-$(CONFIG_PKCS8_PRIVATE_KEY_PARSER) += pkcs8_key_parser.o
37+
pkcs8_key_parser-y := \
38+
pkcs8.asn1.o \
39+
pkcs8_parser.o
40+
41+
$(obj)/pkcs8_parser.o: $(obj)/pkcs8.asn1.h
42+
$(obj)/pkcs8-asn1.o: $(obj)/pkcs8.asn1.c $(obj)/pkcs8.asn1.h
43+
44+
clean-files += pkcs8.asn1.c pkcs8.asn1.h
45+
3246
#
3347
# PKCS#7 message handling
3448
#
@@ -61,3 +75,14 @@ verify_signed_pefile-y := \
6175

6276
$(obj)/mscode_parser.o: $(obj)/mscode.asn1.h $(obj)/mscode.asn1.h
6377
$(obj)/mscode.asn1.o: $(obj)/mscode.asn1.c $(obj)/mscode.asn1.h
78+
79+
#
80+
# TPM private key parsing
81+
#
82+
obj-$(CONFIG_TPM_KEY_PARSER) += tpm_key_parser.o
83+
tpm_key_parser-y := \
84+
tpm.asn1.o \
85+
tpm_parser.o
86+
87+
$(obj)/tpm_parser.o: $(obj)/tpm.asn1.h
88+
$(obj)/tpm.asn1.o: $(obj)/tpm.asn1.c $(obj)/tpm.asn1.h

0 commit comments

Comments
 (0)