crypto: KEYS: convert public key to akcipher api
Subsystems (5)
v3 cover crypto: KEYS: convert public key to akcipher api
This patch set converts the module verification and digital signature code to the new akcipher API.
RSA implementation has been removed from crypto/asymmetric_keys and the new API is used for cryptographic primitives.
There is no need for MPI above the akcipher API anymore. Modules can be verified with software as well as HW RSA implementations.
Patches generated against cryptodev-2.6
Changes in v3:
- Don't include keys/asymmetric-type.h in crypto/public_key.h
Changes in v2:
- Fix the whey public_key_signature is setup. The pointer s needs to point to the signature instread of the signature_v2_hdr.
- Select CRYPTO_RSA when INTEGRITY_ASYMMETRIC_KEYS is selected.
---
Tadeusz Struk (2):
crypto: KEYS: convert public key to the akcipher api
integrity: convert digsig to akcipher api
crypto/asymmetric_keys/Kconfig | 2 crypto/asymmetric_keys/Makefile | 7 - crypto/asymmetric_keys/pkcs7_parser.c | 12 +- crypto/asymmetric_keys/pkcs7_trust.c | 2 crypto/asymmetric_keys/pkcs7_verify.c | 2 crypto/asymmetric_keys/public_key.c | 64 +++------ crypto/asymmetric_keys/public_key.h | 36 ----- crypto/asymmetric_keys/rsa.c | 213 +++++++++++------------------ crypto/asymmetric_keys/x509_cert_parser.c | 37 +---- crypto/asymmetric_keys/x509_public_key.c | 17 +- crypto/asymmetric_keys/x509_rsakey.asn1 | 4 - include/crypto/public_key.h | 48 ++----- security/integrity/Kconfig | 1 security/integrity/digsig_asymmetric.c | 10 - 14 files changed, 139 insertions(+), 316 deletions(-) delete mode 100644 crypto/asymmetric_keys/public_key.h delete mode 100644 crypto/asymmetric_keys/x509_rsakey.asn1
--
TS
PATCH v3 1/2 crypto: KEYS: convert public key to the akcipher api
This patch converts the module verification code to the new akcipher API.
--- crypto/asymmetric_keys/Kconfig | 2 crypto/asymmetric_keys/Makefile | 7 - crypto/asymmetric_keys/pkcs7_parser.c | 12 +- crypto/asymmetric_keys/pkcs7_trust.c | 2 crypto/asymmetric_keys/pkcs7_verify.c | 2 crypto/asymmetric_keys/public_key.c | 64 +++------ crypto/asymmetric_keys/public_key.h | 36 ----- crypto/asymmetric_keys/rsa.c | 213 +++++++++++------------------ crypto/asymmetric_keys/x509_cert_parser.c | 37 +---- crypto/asymmetric_keys/x509_public_key.c | 17 +- crypto/asymmetric_keys/x509_rsakey.asn1 | 4 - include/crypto/public_key.h | 48 +------ 12 files changed, 135 insertions(+), 309 deletions(-) delete mode 100644 crypto/asymmetric_keys/public_key.h delete mode 100644 crypto/asymmetric_keys/x509_rsakey.asn1
crypto/asymmetric_keys/Kconfig +1 -1
@@ -22,7 +22,7 @@ config ASYMMETRIC_PUBLIC_KEY_SUBTYPE config PUBLIC_KEY_ALGO_RSA tristate "RSA public-key algorithm" - select MPILIB + select CRYPTO_RSA help This option enables support for the RSA algorithm (PKCS#1, RFC3447).
crypto/asymmetric_keys/Makefile +2 -5
@@ -16,21 +16,18 @@ obj-$(CONFIG_X509_CERTIFICATE_PARSER) += x509_key_parser.o x509_key_parser-y := \ x509-asn1.o \ x509_akid-asn1.o \ - x509_rsakey-asn1.o \ x509_cert_parser.o \ x509_public_key.o $(obj)/x509_cert_parser.o: \ $(obj)/x509-asn1.h \ - $(obj)/x509_akid-asn1.h \ - $(obj)/x509_rsakey-asn1.h + $(obj)/x509_akid-asn1.h + $(obj)/x509-asn1.o: $(obj)/x509-asn1.c $(obj)/x509-asn1.h $(obj)/x509_akid-asn1.o: $(obj)/x509_akid-asn1.c $(obj)/x509_akid-asn1.h -$(obj)/x509_rsakey-asn1.o: $(obj)/x509_rsakey-asn1.c $(obj)/x509_rsakey-asn1.h clean-files += x509-asn1.c x509-asn1.h clean-files += x509_akid-asn1.c x509_akid-asn1.h -clean-files += x509_rsakey-asn1.c x509_rsakey-asn1.h # # PKCS#7 message handling
crypto/asymmetric_keys/pkcs7_parser.c +5 -7
@@ -15,7 +15,7 @@#include <linux/slab.h> #include <linux/err.h> #include <linux/oid_registry.h> -#include "public_key.h" +#include <crypto/public_key.h> #include "pkcs7_parser.h" #include "pkcs7-asn1.h"
@@ -44,7 +44,7 @@ struct pkcs7_parse_context {static void pkcs7_free_signed_info (struct pkcs7_signed_info *sinfo) {if (sinfo) { -mpi_free (sinfo->sig.mpi[0 ]); +kfree (sinfo->sig.s);kfree (sinfo->sig.digest);kfree (sinfo->signing_cert_id);kfree (sinfo);
@@ -616,16 +616,14 @@ int pkcs7_sig_note_signature(void *context, size_t hdrlen, const void *value, size_t vlen) {struct pkcs7_parse_context *ctx = context; -MPI mpi;BUG_ON (ctx->sinfo->sig.pkey_algo !=PKEY_ALGO_RSA ); - mpi =mpi_read_raw_data (value, vlen); -if (!mpi) + ctx->sinfo->sig.s =kmemdup (value, vlen,GFP_KERNEL ); +if (!ctx->sinfo->sig.s)return -ENOMEM ; - ctx->sinfo->sig.mpi[0 ] = mpi; - ctx->sinfo->sig.nr_mpi =1 ; + ctx->sinfo->sig.s_size = vlen;return 0 ; }
crypto/asymmetric_keys/pkcs7_trust.c +1 -1
@@ -17,7 +17,7 @@#include <linux/asn1.h> #include <linux/key.h> #include <keys/asymmetric-type.h> -#include "public_key.h" +#include <crypto/public_key.h> #include "pkcs7_parser.h" /**
crypto/asymmetric_keys/pkcs7_verify.c +1 -1
@@ -16,7 +16,7 @@#include <linux/err.h> #include <linux/asn1.h> #include <crypto/hash.h> -#include "public_key.h" +#include <crypto/public_key.h> #include "pkcs7_parser.h" /*
crypto/asymmetric_keys/public_key.c +22 -42
109 lines not on this page · raw message
crypto/asymmetric_keys/public_key.h deleted +0 -36
@@ -1,36 +0,0 @@ -/* Public key algorithm internals -* -* See Documentation/crypto/asymmetric-keys.txt -* -* Copyright (C) 2012 Red Hat, Inc. All Rights Reserved. -* Written by David Howells ([email protected]) -* -* This program is free software; you can redistribute it and/or -* modify it under the terms of the GNU General Public Licence -* as published by the Free Software Foundation; either version -* 2 of the Licence, or (at your option) any later version. -*/ - -#include <crypto/public_key.h> - -extern struct asymmetric_key_subtype public_key_subtype; - -/* -* Public key algorithm definition. -*/ -struct public_key_algorithm { -const char *name; -u8 n_pub_mpi;/* Number of MPIs in public key */ -u8 n_sec_mpi;/* Number of MPIs in secret key */ -u8 n_sig_mpi;/* Number of MPIs in a signature */ -int (*verify_signature)(const struct public_key *key, -const struct public_key_signature *sig); -}; - -extern const struct public_key_algorithm RSA_public_key_algorithm; - -/* -* public_key.c -*/ -extern int public_key_verify_signature (const struct public_key *pk, -const struct public_key_signature *sig);
crypto/asymmetric_keys/rsa.c +79 -134
260 lines not on this page · raw message
crypto/asymmetric_keys/x509_cert_parser.c +7 -30
@@ -15,11 +15,10 @@#include <linux/slab.h> #include <linux/err.h> #include <linux/oid_registry.h> -#include "public_key.h" +#include <crypto/public_key.h> #include "x509_parser.h" #include "x509-asn1.h" #include "x509_akid-asn1.h" -#include "x509_rsakey-asn1.h" struct x509_parse_context {struct x509_certificate *cert;/* Certificate being constructed */
@@ -56,7 +55,7 @@ void x509_free_certificate(struct x509_certificate *cert)kfree (cert->akid_id);kfree (cert->akid_skid);kfree (cert->sig.digest); -mpi_free (cert->sig.rsa.s); +kfree (cert->sig.s);kfree (cert); } }
@@ -103,12 +102,12 @@ struct x509_certificate *x509_cert_parse(const void *data, size_t datalen) } } -/* Decode the public key */ - ret =asn1_ber_decoder (&x509_rsakey_decoder, ctx, - ctx->key, ctx->key_size); -if (ret <0 ) + cert->pub->key =kmemdup (ctx->key, ctx->key_size,GFP_KERNEL ); +if (!cert->pub->key) goto error_decode; + cert->pub->keylen = ctx->key_size; +/* Generate cert issuer + serial number key ID */ kid = asymmetric_key_generate_id(cert->raw_serial, cert->raw_serial_size,
@@ -124,6 +123,7 @@ struct x509_certificate *x509_cert_parse(const void *data, size_t datalen)return cert; error_decode: +kfree (cert->pub->key);kfree (ctx); error_no_ctx:x509_free_certificate (cert);
@@ -404,29 +404,6 @@ int x509_extract_key_data(void *context, size_t hdrlen,return 0 ; } -/* -* Extract a RSA public key value -*/ -int rsa_extract_mpi (void *context,size_t hdrlen, -unsigned char tag, -const void *value,size_t vlen) -{ -struct x509_parse_context *ctx = context; -MPI mpi; - -if (ctx->nr_mpi >=ARRAY_SIZE (ctx->cert->pub->mpi)) { -pr_err ("Too many public key MPIs in certificate\n" ); -return -EBADMSG ; - } - - mpi =mpi_read_raw_data (value, vlen); -if (!mpi) -return -ENOMEM ; - - ctx->cert->pub->mpi[ctx->nr_mpi++] = mpi; -return 0 ; -} -/* The keyIdentifier in AuthorityKeyIdentifier SEQUENCE is tag(CONT,PRIM,0) */ #define SEQ_TAG_KEYID (ASN1_CONT << 6)
crypto/asymmetric_keys/x509_public_key.c +6 -11
@@ -13,15 +13,11 @@#include <linux/module.h> #include <linux/kernel.h> #include <linux/slab.h> -#include <linux/err.h> -#include <linux/mpi.h> -#include <linux/asn1_decoder.h> #include <keys/asymmetric-subtype.h> #include <keys/asymmetric-parser.h> #include <keys/system_keyring.h> #include <crypto/hash.h> #include "asymmetric_keys.h" -#include "public_key.h" #include "x509_parser.h" static bool use_builtin_keys;
@@ -167,13 +163,15 @@ int x509_get_sig_params(struct x509_certificate *cert)if (cert->unsupported_crypto)return -ENOPKG ; -if (cert->sig.rsa.s) +if (cert->sig.s)return 0 ; - cert->sig.rsa.s =mpi_read_raw_data (cert->raw_sig, cert->raw_sig_size); -if (!cert->sig.rsa.s) + cert->sig.s =kmemdup (cert->raw_sig, cert->raw_sig_size, +GFP_KERNEL ); +if (!cert->sig.s)return -ENOMEM ; - cert->sig.nr_mpi =1 ; + + cert->sig.s_size = cert->raw_sig_size;/* Allocate the hashing algorithm we're going to need and find out how * big the hash operational data will be.
@@ -296,8 +294,6 @@ static int x509_key_preparse(struct key_preparsed_payload *prep)if (cert->pub->pkey_algo >=PKEY_ALGO__LAST || cert->sig.pkey_algo >=PKEY_ALGO__LAST || cert->sig.pkey_hash_algo >=PKEY_HASH__LAST || - !pkey_algo[cert->pub->pkey_algo] || - !pkey_algo[cert->sig.pkey_algo] || !hash_algo_name[cert->sig.pkey_hash_algo]) { ret = -ENOPKG ; goto error_free_cert;
@@ -309,7 +305,6 @@ static int x509_key_preparse(struct key_preparsed_payload *prep) pkey_algo_name[cert->sig.pkey_algo], hash_algo_name[cert->sig.pkey_hash_algo]); - cert->pub->algo = pkey_algo[cert->pub->pkey_algo]; cert->pub->id_type =PKEY_ID_X509 ;/* Check the signature on the key if it appears to be self-signed */
crypto/asymmetric_keys/x509_rsakey.asn1 deleted +0 -4
@@ -1,4 +0,0 @@ -RSAPublicKey ::= SEQUENCE { - modulus INTEGER ({ rsa_extract_mpi }), -- n - publicExponent INTEGER ({ rsa_extract_mpi }) -- e - }
include/crypto/public_key.h +11 -37
83 lines not on this page · raw message
kbuild test robot
build botRe: [PATCH v3 1/2] crypto: KEYS: convert public key to the akcipher api
reproduce: wget https://git.kernel.org/cgit/linux/kernel/git/wfg/lkp-tests.git/plain/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross # save the attached .config to linux build tree…
Hi Tadeusz,
[auto build test ERROR on cryptodev/master]
[also build test ERROR on v4.4-rc6 next-20151223]
url: https://github.com/0day-ci/linux/commits/Tadeusz-Struk/crypto-KEYS-convert-public-key-to-akcipher-api/20151223-132001 base: https://git.kernel.org/pub/scm/linux/kernel/git/herbert/cryptodev-2.6.git master config: xtensa-allyesconfig (attached as .config)
reproduce:
wget https://git.kernel.org/cgit/linux/kernel/git/wfg/lkp-tests.git/plain/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
make.cross ARCH=xtensa
Note: the linux-review/Tadeusz-Struk/crypto-KEYS-convert-public-key-to-akcipher-api/20151223-132001 HEAD 06fa55a0feec438fdc649cdac682af7ab99f8099 builds fine.
It only hurts bisectibility.
All errors (new ones prefixed by >>):
security/integrity/digsig_asymmetric.c: In function 'asymmetric_verify':
>> security/integrity/digsig_asymmetric.c:95:5: error: 'struct public_key_signature' has no member named 'nr_mpi'
pks.nr_mpi = 1;
^
>> security/integrity/digsig_asymmetric.c:96:5: error: 'struct public_key_signature' has no member named 'rsa'
pks.rsa.s = mpi_read_raw_data(hdr->sig, siglen);
^
>> security/integrity/digsig_asymmetric.c:96:2: error: implicit declaration of function 'mpi_read_raw_data' [-Werror=implicit-function-declaration]
pks.rsa.s = mpi_read_raw_data(hdr->sig, siglen);
^
security/integrity/digsig_asymmetric.c:98:9: error: 'struct public_key_signature' has no member named 'rsa'
if (pks.rsa.s)
^
>> security/integrity/digsig_asymmetric.c:101:2: error: implicit declaration of function 'mpi_free' [-Werror=implicit-function-declaration]
mpi_free(pks.rsa.s);
^
security/integrity/digsig_asymmetric.c:101:14: error: 'struct public_key_signature' has no member named 'rsa'
mpi_free(pks.rsa.s);
^cc1: some warnings being treated as errors
vim +95 security/integrity/digsig_asymmetric.c
e0751257 Dmitry Kasatkin 2013-02-07 89
e0751257 Dmitry Kasatkin 2013-02-07 90 memset(&pks, 0, sizeof(pks));
e0751257 Dmitry Kasatkin 2013-02-07 91
e0751257 Dmitry Kasatkin 2013-02-07 92 pks.pkey_hash_algo = hdr->hash_algo;
e0751257 Dmitry Kasatkin 2013-02-07 93 pks.digest = (u8 *)data;
e0751257 Dmitry Kasatkin 2013-02-07 94 pks.digest_size = datalen;
e0751257 Dmitry Kasatkin 2013-02-07 @95 pks.nr_mpi = 1;
e0751257 Dmitry Kasatkin 2013-02-07 @96 pks.rsa.s = mpi_read_raw_data(hdr->sig, siglen);
e0751257 Dmitry Kasatkin 2013-02-07 97
e0751257 Dmitry Kasatkin 2013-02-07 98 if (pks.rsa.s)
e0751257 Dmitry Kasatkin 2013-02-07 99 ret = verify_signature(key, &pks);
e0751257 Dmitry Kasatkin 2013-02-07 100
e0751257 Dmitry Kasatkin 2013-02-07 @101 mpi_free(pks.rsa.s);
e0751257 Dmitry Kasatkin 2013-02-07 102 key_put(key);
e0751257 Dmitry Kasatkin 2013-02-07 103 pr_debug("%s() = %d\n", __func__, ret);
e0751257 Dmitry Kasatkin 2013-02-07 104 return ret;:::::: The code at line 95 was first introduced by commit
:::::: e0751257a64ea10cca96ccb06522bfb10e36cb5b ima: digital signature verification using asymmetric keys
:::::: TO: Dmitry Kasatkin <[email protected]> :::::: CC: Mimi Zohar <[email protected]>
---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation
PATCH v3 2/2 integrity: convert digsig to akcipher api
In reply to Tadeusz StrukConvert asymmetric_verify to akcipher api.
--- security/integrity/Kconfig | 1 + security/integrity/digsig_asymmetric.c | 10 +++------- 2 files changed, 4 insertions(+), 7 deletions(-)
security/integrity/Kconfig +1 -0
@@ -36,6 +36,7 @@ config INTEGRITY_ASYMMETRIC_KEYS select ASYMMETRIC_KEY_TYPE select ASYMMETRIC_PUBLIC_KEY_SUBTYPE select PUBLIC_KEY_ALGO_RSA + select CRYPTO_RSA select X509_CERTIFICATE_PARSER help This option enables digital signature verification using
security/integrity/digsig_asymmetric.c +3 -7
@@ -92,13 +92,9 @@ int asymmetric_verify(struct key *keyring, const char *sig, pks.pkey_hash_algo = hdr->hash_algo; pks.digest = (u8 *)data; pks.digest_size = datalen; - pks.nr_mpi =1 ; - pks.rsa.s =mpi_read_raw_data (hdr->sig, siglen); - -if (pks.rsa.s) - ret =verify_signature (key, &pks); - -mpi_free (pks.rsa.s); + pks.s = hdr->sig; + pks.s_size = siglen; + ret =verify_signature (key, &pks);key_put (key);pr_debug ("%s() = %d\n" , __func__, ret);return ret;