Introduce OSCCA certificate and SM2 asymmetric algorithm
Subsystems (3)
PATCH 1/1 Introduce OSCCA certificate and SM2 asymmetric algorithm
Hello all,
This new module implement the OSCCA certificate and SM2 public key algorithm. It was published by State Encryption Management Bureau, China. List of specifications for OSCCA certificate and SM2 elliptic curve public key cryptography:
* GM/T 0003.1-2012
* GM/T 0003.2-2012
* GM/T 0003.3-2012
* GM/T 0003.4-2012
* GM/T 0003.5-2012
* GM/T 0015-2012
* GM/T 0009-2012
IETF: https://tools.ietf.org/html/draft-shen-sm2-ecdsa-02
oscca: http://www.oscca.gov.cn/sca/xxgk/2010-12/17/content_1002386.shtml
scctc: http://www.gmbz.org.cn/main/bzlb.html
These patchs add the OID object identifier defined by OSCCA. The x509 certificate supports sm2-with-sm3 type certificate parsing and verification.
The sm2 algorithm is based on libgcrypt's mpi implementation, and has made some additions to the kernel's original mpi library, and added the implementation of ec to better support elliptic curve-like algorithms.
sm2 has good support in both openssl and gnupg projects, and sm3 and sm4 of the OSCCA algorithm family have also been implemented in the kernel.
Thanks,
Tianjia
PATCH 1/7 lib/mpi: Extend the MPI library
Expand the mpi library based on libgcrypt, and the ECC algorithm of mpi based on libgcrypt requires these functions.
Some other algorithms will be developed based on mpi ecc, such as SM2.
--- include/linux/mpi.h | 88 +++++++++++ lib/mpi/Makefile | 5 + lib/mpi/mpi-add.c | 207 +++++++++++++++++++++++++ lib/mpi/mpi-bit.c | 251 ++++++++++++++++++++++++++++++ lib/mpi/mpi-cmp.c | 46 ++++-- lib/mpi/mpi-div.c | 259 +++++++++++++++++++++++++++++++ lib/mpi/mpi-internal.h | 53 +++++++ lib/mpi/mpi-inv.c | 143 ++++++++++++++++++ lib/mpi/mpi-mod.c | 155 +++++++++++++++++++ lib/mpi/mpi-mul.c | 166 ++++++++++++++++++++ lib/mpi/mpicoder.c | 336 +++++++++++++++++++++++++++++++++++++++++ lib/mpi/mpih-div.c | 294 ++++++++++++++++++++++++++++++++++++ lib/mpi/mpih-mul.c | 25 +++ lib/mpi/mpiutil.c | 204 +++++++++++++++++++++++++ 14 files changed, 2222 insertions(+), 10 deletions(-) create mode 100644 lib/mpi/mpi-add.c create mode 100644 lib/mpi/mpi-div.c create mode 100644 lib/mpi/mpi-inv.c create mode 100644 lib/mpi/mpi-mod.c create mode 100644 lib/mpi/mpi-mul.c
include/linux/mpi.h +88 -0
@@ -40,21 +40,79 @@ struct gcry_mpi {typedef struct gcry_mpi *MPI ;#define mpi_get_nlimbs (a) ((a)->nlimbs) +#define mpi_has_sign (a) ((a)->sign)/*-- mpiutil.c --*/ MPI mpi_alloc (unsigned nlimbs); +void mpi_clear (MPI a);void mpi_free (MPI a);int mpi_resize (MPI a,unsigned nlimbs); +static inline MPI mpi_new (unsigned int nbits) +{ +return mpi_alloc ((nbits +BITS_PER_MPI_LIMB -1 ) /BITS_PER_MPI_LIMB ); +} + +MPI mpi_copy (MPI a); +MPI mpi_alloc_like (MPI a); +void mpi_snatch (MPI w,MPI u); +MPI mpi_set (MPI w,MPI u); +MPI mpi_set_ui (MPI w,unsigned long u); +MPI mpi_alloc_set_ui (unsigned long u); +void mpi_swap_cond (MPI a,MPI b,unsigned long swap); + +/* Constants used to return constant MPIs. See mpi_init if you +* want to add more constants. +*/ +#define MPI_NUMBER_OF_CONSTANTS 6 +enum gcry_mpi_constants { +MPI_C_ZERO , +MPI_C_ONE , +MPI_C_TWO , +MPI_C_THREE , +MPI_C_FOUR , +MPI_C_EIGHT +}; + +MPI mpi_const (enum gcry_mpi_constants no); +/*-- mpicoder.c --*/ + +/* Different formats of external big integer representation. */ +enum gcry_mpi_format { +GCRYMPI_FMT_NONE =0 , +GCRYMPI_FMT_STD =1 ,/* Twos complement stored without length. */ +GCRYMPI_FMT_PGP =2 ,/* As used by OpenPGP (unsigned only). */ +GCRYMPI_FMT_SSH =3 ,/* As used by SSH (like STD but with length). */ +GCRYMPI_FMT_HEX =4 ,/* Hex format. */ +GCRYMPI_FMT_USG =5 ,/* Like STD but unsigned. */ +GCRYMPI_FMT_OPAQUE =8 /* Opaque format (some functions only). */ +}; +MPI mpi_read_raw_data (const void *xbuffer,size_t nbytes);MPI mpi_read_from_buffer (const void *buffer,unsigned *ret_nread); +int mpi_fromstr (MPI val,const char *str); +MPI mpi_scanval (const char *string);MPI mpi_read_raw_from_sgl (struct scatterlist *sgl,unsigned int len);void *mpi_get_buffer (MPI a,unsigned *nbytes,int *sign);int mpi_read_buffer (MPI a,uint8_t *buf,unsigned buf_len,unsigned *nbytes,int *sign);int mpi_write_to_sgl (MPI a,struct scatterlist *sg,unsigned nbytes,int *sign); +int mpi_print (enum gcry_mpi_format format,unsigned char *buffer, +size_t buflen,size_t *nwritten,MPI a); + +/*-- mpi-mod.c --*/ +void mpi_mod (MPI rem,MPI dividend,MPI divisor); + +/* Context used with Barrett reduction. */ +struct barrett_ctx_s ; +typedef struct barrett_ctx_s *mpi_barrett_t ; + +mpi_barrett_t mpi_barrett_init (MPI m,int copy); +void mpi_barrett_free (mpi_barrett_t ctx); +void mpi_mod_barrett (MPI r,MPI x,mpi_barrett_t ctx); +void mpi_mul_barrett (MPI w,MPI u,MPI v,mpi_barrett_t ctx);/*-- mpi-pow.c --*/ int mpi_powm (MPI res,MPI base,MPI exp,MPI mod);
@@ -62,10 +120,40 @@ int mpi_powm(MPI res, MPI base, MPI exp, MPI mod);/*-- mpi-cmp.c --*/ int mpi_cmp_ui (MPI u,ulong v);int mpi_cmp (MPI u,MPI v); +int mpi_cmpabs (MPI u,MPI v);/*-- mpi-bit.c --*/ void mpi_normalize (MPI a);unsigned mpi_get_nbits (MPI a); +int mpi_test_bit (MPI a,unsigned int n); +void mpi_set_bit (MPI a,unsigned int n); +void mpi_set_highbit (MPI a,unsigned int n); +void mpi_clear_highbit (MPI a,unsigned int n); +void mpi_clear_bit (MPI a,unsigned int n); +void mpi_rshift_limbs (MPI a,unsigned int count); +void mpi_rshift (MPI x,MPI a,unsigned int n); +void mpi_lshift_limbs (MPI a,unsigned int count); +void mpi_lshift (MPI x,MPI a,unsigned int n); + +/*-- mpi-add.c --*/ +void mpi_add_ui (MPI w,MPI u,unsigned long v); +void mpi_add (MPI w,MPI u,MPI v); +void mpi_sub_ui (MPI w,MPI u,unsigned long v); +void mpi_sub (MPI w,MPI u,MPI v); +void mpi_addm (MPI w,MPI u,MPI v,MPI m); +void mpi_subm (MPI w,MPI u,MPI v,MPI m); + +/*-- mpi-mul.c --*/ +void mpi_mul (MPI w,MPI u,MPI v); +void mpi_mulm (MPI w,MPI u,MPI v,MPI m); + +/*-- mpi-div.c --*/ +void mpi_tdiv_r (MPI rem,MPI num,MPI den); +void mpi_fdiv_r (MPI rem,MPI dividend,MPI divisor); +void mpi_fdiv_q (MPI quot,MPI dividend,MPI divisor); + +/*-- mpi-inv.c --*/ +int mpi_invm (MPI x,MPI a,MPI n);/* inline functions */
lib/mpi/Makefile +5 -0
@@ -14,8 +14,13 @@ mpi-y = \ generic_mpih-sub1.o \ generic_mpih-add1.o \ mpicoder.o \ + mpi-add.o \ mpi-bit.o \ mpi-cmp.o \ + mpi-div.o \ + mpi-inv.o \ + mpi-mod.o \ + mpi-mul.o \ mpih-cmp.o \ mpih-div.o \ mpih-mul.o \
lib/mpi/mpi-add.c new file +207 -0
207 lines not on this page · raw message
lib/mpi/mpi-bit.c +251 -0
260 lines not on this page · raw message
lib/mpi/mpi-cmp.c +36 -10
64 lines not on this page · raw message
lib/mpi/mpi-div.c new file +259 -0
259 lines not on this page · raw message
lib/mpi/mpi-internal.h +53 -0
86 lines not on this page · raw message
lib/mpi/mpi-inv.c new file +143 -0
143 lines not on this page · raw message
lib/mpi/mpi-mod.c new file +155 -0
155 lines not on this page · raw message
lib/mpi/mpi-mul.c new file +166 -0
166 lines not on this page · raw message
lib/mpi/mpicoder.c +336 -0
351 lines not on this page · raw message
lib/mpi/mpih-div.c +294 -0
303 lines not on this page · raw message
lib/mpi/mpih-mul.c +25 -0
@@ -317,6 +317,31 @@ mpih_sqr_n(mpi_ptr_t prodp, mpi_ptr_t up, mpi_size_t size, mpi_ptr_t tspace) } } + +void mpihelp_mul_n (mpi_ptr_t prodp, +mpi_ptr_t up,mpi_ptr_t vp,mpi_size_t size) +{ +if (up == vp) { +if (size <KARATSUBA_THRESHOLD ) +mpih_sqr_n_basecase (prodp, up, size); +else { +mpi_ptr_t tspace; + tspace =mpi_alloc_limb_space (2 * size); +mpih_sqr_n (prodp, up, size, tspace); +mpi_free_limb_space (tspace); + } + }else { +if (size <KARATSUBA_THRESHOLD ) +mul_n_basecase (prodp, up, vp, size); +else { +mpi_ptr_t tspace; + tspace =mpi_alloc_limb_space (2 * size); +mul_n (prodp, up, vp, size, tspace); +mpi_free_limb_space (tspace); + } + } +} + int mpihelp_mul_karatsuba_case(mpi_ptr_t prodp, mpi_ptr_t up, mpi_size_t usize,
lib/mpi/mpiutil.c +204 -0
221 lines not on this page · raw message
Signature · 1 line
2.17.1
PATCH 2/7 lib/mpi: Introduce ec implementation to MPI library
In reply to Tianjia ZhangThe implementation of EC is introduced from libgcrypt as the basic algorithm of elliptic curve, which can be more perfectly integrated with MPI implementation.
Some other algorithms will be developed based on mpi ecc, such as SM2.
--- include/linux/mpi.h | 105 +++ lib/mpi/Makefile | 1 + lib/mpi/ec.c | 1538 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 1644 insertions(+) create mode 100644 lib/mpi/ec.c
include/linux/mpi.h +105 -0
@@ -155,6 +155,111 @@ void mpi_fdiv_q(MPI quot, MPI dividend, MPI divisor);/*-- mpi-inv.c --*/ int mpi_invm (MPI x,MPI a,MPI n); +/*-- ec.c --*/ + +/* Object to represent a point in projective coordinates */ +struct gcry_mpi_point { +MPI x; +MPI y; +MPI z; +}; + +typedef struct gcry_mpi_point *MPI_POINT ; + +/* Models describing an elliptic curve */ +enum gcry_mpi_ec_models { +/* The Short Weierstrass equation is +* y^2 = x^3 + ax + b +*/ +MPI_EC_WEIERSTRASS =0 , +/* The Montgomery equation is +* by^2 = x^3 + ax^2 + x +*/ +MPI_EC_MONTGOMERY , +/* The Twisted Edwards equation is +* ax^2 + y^2 = 1 + bx^2y^2 +* Note that we use 'b' instead of the commonly used 'd'. +*/ +MPI_EC_EDWARDS +}; + +/* Dialects used with elliptic curves */ +enum ecc_dialects { +ECC_DIALECT_STANDARD =0 , +ECC_DIALECT_ED25519 , +ECC_DIALECT_SAFECURVE +}; + +/* This context is used with all our EC functions. */ +struct mpi_ec_ctx { +enum gcry_mpi_ec_models model;/* The model describing this curve. */ +enum ecc_dialects dialect;/* The ECC dialect used with the curve. */ +int flags;/* Public key flags (not always used). */ +unsigned int nbits;/* Number of bits. */ + +/* Domain parameters. Note that they may not all be set and if set +* the MPIs may be flaged as constant. +*/ +MPI p;/* Prime specifying the field GF(p). */ +MPI a;/* First coefficient of the Weierstrass equation. */ +MPI b;/* Second coefficient of the Weierstrass equation. */ +MPI_POINT G;/* Base point (generator). */ +MPI n;/* Order of G. */ +unsigned int h;/* Cofactor. */ + +/* The actual key. May not be set. */ +MPI_POINT Q;/* Public key. */ +MPI d;/* Private key. */ + +const char *name;/* Name of the curve. */ + +/* This structure is private to mpi/ec.c! */ +struct { +struct { +unsigned int a_is_pminus3:1 ; +unsigned int two_inv_p:1 ; + } valid;/* Flags to help setting the helper vars below. */ + +int a_is_pminus3;/* True if A = P - 3. */ + +MPI two_inv_p; + +mpi_barrett_t p_barrett; + +/* Scratch variables. */ +MPI scratch[11 ]; + +/* Helper for fast reduction. */ +/* int nist_nbits; /\* If this is a NIST curve, the # of bits. *\/ */ +/* MPI s[10]; */ +/* MPI c; */ + } t; + +/* Curve specific computation routines for the field. */ +void (*addm)(MPI w,MPI u,MPI v,struct mpi_ec_ctx *ctx); +void (*subm)(MPI w,MPI u,MPI v,struct mpi_ec_ctx *ec); +void (*mulm)(MPI w,MPI u,MPI v,struct mpi_ec_ctx *ctx); +void (*pow2)(MPI w,const MPI b,struct mpi_ec_ctx *ctx); +void (*mul2)(MPI w,MPI u,struct mpi_ec_ctx *ctx); +}; + +void mpi_ec_init (struct mpi_ec_ctx *ctx,enum gcry_mpi_ec_models model, +enum ecc_dialects dialect, +int flags,MPI p,MPI a,MPI b); +void mpi_ec_deinit (struct mpi_ec_ctx *ctx); +MPI_POINT mpi_point_new (unsigned int nbits); +void mpi_point_release (MPI_POINT p); +void mpi_point_init (MPI_POINT p); +void mpi_point_free_parts (MPI_POINT p); +int mpi_ec_get_affine (MPI x,MPI y,MPI_POINT point,struct mpi_ec_ctx *ctx); +void mpi_ec_add_points (MPI_POINT result, +MPI_POINT p1,MPI_POINT p2, +struct mpi_ec_ctx *ctx); +void mpi_ec_mul_point (MPI_POINT result, +MPI scalar,MPI_POINT point, +struct mpi_ec_ctx *ctx); +int mpi_ec_curve_point (MPI_POINT point,struct mpi_ec_ctx *ctx); +/* inline functions */ /**
lib/mpi/Makefile +1 -0
@@ -13,6 +13,7 @@ mpi-y = \ generic_mpih-rshift.o \ generic_mpih-sub1.o \ generic_mpih-add1.o \ + ec.o \ mpicoder.o \ mpi-add.o \ mpi-bit.o \
lib/mpi/ec.c new file +1538 -0
1,538 lines not on this page · raw message
Signature · 1 line
2.17.1
PATCH 3/7 crypto: sm2 - introduce OSCCA SM2 asymmetric cipher algorithm
In reply to Tianjia ZhangThis new module implement the SM2 public key algorithm. It was published by State Encryption Management Bureau, China. List of specifications for SM2 elliptic curve public key cryptography:
* GM/T 0003.1-2012
* GM/T 0003.2-2012
* GM/T 0003.3-2012
* GM/T 0003.4-2012
* GM/T 0003.5-2012
IETF: https://tools.ietf.org/html/draft-shen-sm2-ecdsa-02
oscca: http://www.oscca.gov.cn/sca/xxgk/2010-12/17/content_1002386.shtml
scctc: http://www.gmbz.org.cn/main/bzlb.html
--- crypto/Kconfig | 17 + crypto/Makefile | 8 + crypto/sm2.c | 1145 ++++++++++++++++++++++++++++++++++++++ crypto/sm2signature.asn1 | 4 + include/crypto/sm2.h | 25 + 5 files changed, 1199 insertions(+) create mode 100644 crypto/sm2.c create mode 100644 crypto/sm2signature.asn1 create mode 100644 include/crypto/sm2.h
crypto/Kconfig +17 -0
@@ -264,6 +264,23 @@ config CRYPTO_ECRDSA standard algorithms (called GOST algorithms). Only signature verification is implemented. +config CRYPTO_SM2 + tristate "SM2 algorithm" + select CRYPTO_SM3 + select CRYPTO_AKCIPHER + select CRYPTO_MANAGER + select MPILIB + select ASN1 + help + Generic implementation of the SM2 public key algorithm. It was + published by State Encryption Management Bureau, China. + as specified by OSCCA GM/T 0003.1-2012 -- 0003.5-2012. + + References: + https://tools.ietf.org/html/draft-shen-sm2-ecdsa-02 + http://www.oscca.gov.cn/sca/xxgk/2010-12/17/content_1002386.shtml + http://www.gmbz.org.cn/main/bzlb.html + config CRYPTO_CURVE25519 tristate "Curve25519 algorithm" select CRYPTO_KPP
crypto/Makefile +8 -0
@@ -42,6 +42,14 @@ rsa_generic-y += rsa_helper.o rsa_generic-y += rsa-pkcs1pad.o obj-$(CONFIG_CRYPTO_RSA) += rsa_generic.o +$(obj)/sm2signature.asn1.o: $(obj)/sm2signature.asn1.c $(obj)/sm2signature.asn1.h +$(obj)/sm2.o: $(obj)/sm2signature.asn1.h + +sm2_generic-y += sm2signature.asn1.o +sm2_generic-y += sm2.o + +obj-$(CONFIG_CRYPTO_SM2) += sm2_generic.o + crypto_acompress-y := acompress.o crypto_acompress-y += scompress.o obj-$(CONFIG_CRYPTO_ACOMP2) += crypto_acompress.o
crypto/sm2.c new file +1145 -0
1,145 lines not on this page · raw message
crypto/sm2signature.asn1 new file +4 -0
@@ -0,0 +1,4 @@ +Sm2Signature ::= SEQUENCE { + sig_r INTEGER ({ sm2_get_signature_r }), + sig_s INTEGER ({ sm2_get_signature_s }) +}
include/crypto/sm2.h new file +25 -0
@@ -0,0 +1,25 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* +* sm2.h - SM2 asymmetric public-key algorithm +* as specified by OSCCA GM/T 0003.1-2012 -- 0003.5-2012 SM2 and +* described at https://tools.ietf.org/html/draft-shen-sm2-ecdsa-02 +* +* Copyright (c) 2020, Alibaba Group. +* Written by Tianjia Zhang <[email protected]> +*/ + +#ifndef _CRYPTO_SM2_H +#define _CRYPTO_SM2_H + +#include <crypto/sm3.h> +#include <crypto/akcipher.h> + +/* The default user id as specified in GM/T 0009-2012 */ +#define SM2_DEFAULT_USERID "1234567812345678" +#define SM2_DEFAULT_USERID_LEN 16 + +extern int sm2_compute_z_digest (struct crypto_akcipher *tfm, +const unsigned char *id,size_t id_len, +unsigned char dgst[SM3_DIGEST_SIZE ]); + +#endif /* _CRYPTO_SM2_H */
Signature · 1 line
2.17.1
PATCH 4/7 crypto: testmgr - support test with different ciphertext per encryption
In reply to Tianjia ZhangSome asymmetric algorithms will get different ciphertext after each encryption, such as SM2, and let testmgr support the testing of such algorithms.
In struct akcipher_testvec, set c and c_size to be empty, skip the comparison of the ciphertext, and compare the decrypted plaintext with m to achieve the test purpose.
--- crypto/testmgr.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
crypto/testmgr.c +6 -1
@@ -3741,7 +3741,7 @@ static int test_akcipher_one(struct crypto_akcipher *tfm,pr_err ("alg: akcipher: %s test failed. err %d\n" , op, err); goto free_all; } -if (!vecs->siggen_sigver_test) { +if (!vecs->siggen_sigver_test && c) {if (req->dst_len != c_size) {pr_err ("alg: akcipher: %s test failed. Invalid output len\n" , op);
@@ -3772,6 +3772,11 @@ static int test_akcipher_one(struct crypto_akcipher *tfm, goto free_all; } +if (!vecs->siggen_sigver_test && !c) { + c = outbuf_enc; + c_size = req->dst_len; + } + op = vecs->siggen_sigver_test ?"sign" :"decrypt" ;if (WARN_ON (c_size >PAGE_SIZE )) goto free_all;
Signature · 1 line
2.17.1
PATCH 5/7 crypto: testmgr - Add SM2 test vectors
In reply to Tianjia ZhangAdd testmgr tests and vectors for SM2 asymmetric cipher.
--- crypto/testmgr.c | 7 +++++++ crypto/testmgr.h | 16 ++++++++++++++++ 2 files changed, 23 insertions(+)
crypto/testmgr.c +7 -0
@@ -5050,6 +5050,13 @@ static const struct alg_test_desc alg_test_descs[] = { .suite = { .hash = __VECS(sha512_tv_template) } + }, { + .alg = "sm2", + .test = alg_test_akcipher, + .fips_allowed = 1, + .suite = { + .akcipher = __VECS(sm2_tv_template) + } }, { .alg = "sm3", .test = alg_test_hash,
crypto/testmgr.h +16 -0
@@ -809,6 +809,22 @@ static const struct akcipher_testvec pkcs1pad_rsa_tv_template[] = { } }; +/* +* SM2 test vectors. +*/ +static const struct akcipher_testvec sm2_tv_template[] = { + { + .key = +"\xbd\xca\x64\x55\xa5\x5b\x9c\x27\x22\xd0\xf5\x80\xf7\xf3\xc5\x63" +"\x3c\xbf\xce\xe8\x55\x17\xaa\xa5\x7f\x11\x9b\x4b\x25\x56\x9b\x43" , + .m ="\x39\xb3\x2c\x59\x82\xc7\xdf\x11\x8a\x64\x2d" , + .c =NULL , + .key_len =32 , + .m_size =11 , + .c_size =0 , + } +}; +static const struct kpp_testvec dh_tv_template[] = { { .secret =
Signature · 1 line
2.17.1
PATCH 6/7 X.509: support OSCCA certificate parse
In reply to Tianjia ZhangThe digital certificate format based on SM2 crypto algorithm as specified in GM/T 0015-2012. It was published by State Encryption Management Bureau, China.
This patch adds the OID object identifier defined by OSCCA. The x509 certificate supports sm2-with-sm3 type certificate parsing. It uses the standard elliptic curve public key, and the sm2 algorithm signs the hash generated by sm3.
--- crypto/asymmetric_keys/x509_cert_parser.c | 14 +++++++++++++- include/linux/oid_registry.h | 6 ++++++ 2 files changed, 19 insertions(+), 1 deletion(-)
crypto/asymmetric_keys/x509_cert_parser.c +13 -1
@@ -234,6 +234,10 @@ int x509_note_pkey_algo(void *context, size_t hdrlen,case OID_gost2012Signature512: ctx->cert->sig->hash_algo ="streebog512" ; goto ecrdsa; + +case OID_sm2_with_sm3: + ctx->cert->sig->hash_algo ="sm3" ; + goto sm2; } rsa_pkcs1:
@@ -246,6 +250,11 @@ int x509_note_pkey_algo(void *context, size_t hdrlen, ctx->cert->sig->encoding ="raw" ; ctx->algo_oid = ctx->last_oid;return 0 ; +sm2: + ctx->cert->sig->pkey_algo ="sm2" ; + ctx->cert->sig->encoding ="raw" ; + ctx->algo_oid = ctx->last_oid; +return 0 ; }/*
@@ -266,7 +275,8 @@ int x509_note_signature(void *context, size_t hdrlen, }if (strcmp (ctx->cert->sig->pkey_algo,"rsa" ) ==0 || -strcmp (ctx->cert->sig->pkey_algo,"ecrdsa" ) ==0 ) { +strcmp (ctx->cert->sig->pkey_algo,"ecrdsa" ) ==0 || +strcmp (ctx->cert->sig->pkey_algo,"sm2" ) ==0 ) {/* Discard the BIT STRING metadata */ if (vlen <1 || *(const u8 *)value !=0 )return -EBADMSG ;
@@ -456,6 +466,8 @@ int x509_extract_key_data(void *context, size_t hdrlen,else if (ctx->last_oid == OID_gost2012PKey256 || ctx->last_oid == OID_gost2012PKey512) ctx->cert->pub->pkey_algo ="ecrdsa" ; +else if (ctx->last_oid == OID_id_ecPublicKey) + ctx->cert->pub->pkey_algo ="sm2" ;else return -ENOPKG ;
include/linux/oid_registry.h +6 -0
@@ -107,6 +107,12 @@ enum OID { OID_gostTC26Sign512B,/* 1.2.643.7.1.2.1.2.2 */ OID_gostTC26Sign512C,/* 1.2.643.7.1.2.1.2.3 */ +/* OSCCA */ + OID_sm2,/* 1.2.156.10197.1.301 */ + OID_sm3,/* 1.2.156.10197.1.401 */ + OID_sm2_with_sm3,/* 1.2.156.10197.1.501 */ + OID_sm3WithRSAEncryption,/* 1.2.156.10197.1.504 */ + OID__NR };
Signature · 1 line
2.17.1
PATCH 7/7 X.509: support OSCCA sm2-with-sm3 certificate verification
In reply to Tianjia ZhangThe digital certificate format based on SM2 crypto algorithm as specified in GM/T 0015-2012. It was published by State Encryption Management Bureau, China.
The method of generating Other User Information is defined as ZA=H256(ENTLA || IDA || a || b || xG || yG || xA || yA), it also specified in https://tools.ietf.org/html/draft-shen-sm2-ecdsa-02.
The x509 certificate supports sm2-with-sm3 type certificate verification. Because certificate verification requires ZA in addition to tbs data, ZA also depends on elliptic curve parameters and public key data, so you need to access tbs in sig and calculate ZA. Finally calculate the digest of the signature and complete the verification work. The calculation process of ZA is declared in specifications GM/T 0009-2012 and GM/T 0003.2-2012.
--- crypto/asymmetric_keys/public_key.c | 61 ++++++++++++++++++++++++ crypto/asymmetric_keys/x509_public_key.c | 2 + include/crypto/public_key.h | 1 + 3 files changed, 64 insertions(+)
crypto/asymmetric_keys/public_key.c +61 -0
@@ -17,6 +17,11 @@#include <keys/asymmetric-subtype.h> #include <crypto/public_key.h> #include <crypto/akcipher.h> +#ifdef CONFIG_CRYPTO_SM2 +#include <crypto/sm3_base.h> +#include <crypto/sm2.h> +#include "x509_parser.h" +#endif MODULE_DESCRIPTION ("In-software asymmetric public-key subtype" );MODULE_AUTHOR ("Red Hat, Inc." );
@@ -245,6 +250,54 @@ static int software_key_eds_op(struct kernel_pkey_params *params,return ret; } +#ifdef CONFIG_CRYPTO_SM2 +static int cert_sig_digest_update (const struct public_key_signature *sig, +struct crypto_akcipher *tfm_pkey) +{ +struct x509_certificate *cert = sig->cert; +struct crypto_shash *tfm; +struct shash_desc *desc; +size_t desc_size; +unsigned char dgst[SM3_DIGEST_SIZE ]; +int ret; + +if (!cert) +return -EINVAL ; + + ret =sm2_compute_z_digest (tfm_pkey,SM2_DEFAULT_USERID , +SM2_DEFAULT_USERID_LEN , dgst); +if (ret) +return ret; + + tfm =crypto_alloc_shash (sig->hash_algo,0 ,0 ); +if (IS_ERR (tfm)) +return PTR_ERR (tfm); + + desc_size =crypto_shash_descsize (tfm) +sizeof (*desc); + desc =kzalloc (desc_size,GFP_KERNEL ); +if (!desc) + goto error_free_tfm; + + desc->tfm = tfm; + + ret =crypto_shash_init (desc); +if (ret <0 ) + goto error_free_desc; + + ret =crypto_shash_update (desc, dgst,SM3_DIGEST_SIZE ); +if (ret <0 ) + goto error_free_desc; + + ret =crypto_shash_finup (desc, cert->tbs, cert->tbs_size, sig->digest); + +error_free_desc: +kfree (desc); +error_free_tfm: +crypto_free_shash (tfm); +return ret; +} +#endif +/* * Verify a signature using a public key. */
@@ -298,6 +351,14 @@ int public_key_verify_signature(const struct public_key *pkey,if (ret) goto error_free_key; +#ifdef CONFIG_CRYPTO_SM2 +if (strcmp (sig->pkey_algo,"sm2" ) ==0 ) { + ret =cert_sig_digest_update (sig, tfm); +if (ret) + goto error_free_key; + } +#endif +sg_init_table (src_sg,2 );sg_set_buf (&src_sg[0 ], sig->s, sig->s_size);sg_set_buf (&src_sg[1 ], sig->digest, sig->digest_size);
crypto/asymmetric_keys/x509_public_key.c +2 -0
@@ -30,6 +30,8 @@ int x509_get_sig_params(struct x509_certificate *cert)pr_devel ("==>%s()\n" , __func__); + sig->cert = cert; +if (!cert->pub->pkey_algo) cert->unsupported_key = true;
include/crypto/public_key.h +1 -0
@@ -44,6 +44,7 @@ struct public_key_signature {const char *pkey_algo;const char *hash_algo;const char *encoding; +void *cert;/* For certificate */ };extern void public_key_signature_free (struct public_key_signature *sig);
Signature · 1 line
2.17.1
Re: [PATCH 7/7] X.509: support OSCCA sm2-with-sm3 certificate verification
Hi,
Quoted 30 lines · Tianjia Zhang
The digital certificate format based on SM2 crypto algorithm as specified in GM/T 0015-2012. It was published by State Encryption Management Bureau, China.
The method of generating Other User Information is defined as ZA=H256(ENTLA || IDA || a || b || xG || yG || xA || yA), it also specified in https://tools.ietf.org/html/draft-shen-sm2-ecdsa-02.
The x509 certificate supports sm2-with-sm3 type certificate verification. Because certificate verification requires ZA in addition to tbs data, ZA also depends on elliptic curve parameters and public key data, so you need to access tbs in sig and calculate ZA. Finally calculate the digest of the signature and complete the verification work. The calculation process of ZA is declared in specifications GM/T 0009-2012 and GM/T 0003.2-2012.
Signed-off-by Tianjia Zhang <[email protected]>--- crypto/asymmetric_keys/public_key.c | 61 ++++++++++++++++++++++++ crypto/asymmetric_keys/x509_public_key.c | 2 + include/crypto/public_key.h | 1 + 3 files changed, 64 insertions(+)crypto/asymmetric_keys/public_key.c +0
-0
crypto/asymmetric_keys/public_key.c line 19
Quoted 3 lines
crypto/asymmetric_keys/public_key.c +0
-0@@ -17,6 +17,11 @@#include <keys/asymmetric-subtype.h> #include <crypto/public_key.h> #include <crypto/akcipher.h>
hmmm... ifdefs like these are kind of ugly.
Quoted 64 lines
crypto/asymmetric_keys/public_key.c +53
-0+#ifdef CONFIG_CRYPTO_SM2 +#include <crypto/sm3_base.h> +#include <crypto/sm2.h> +#include "x509_parser.h" +#endif MODULE_DESCRIPTION ("In-software asymmetric public-key subtype" );MODULE_AUTHOR ("Red Hat, Inc." );@@ -245,6 +250,54 @@ static int software_key_eds_op(struct kernel_pkey_params *params,return ret; } +#ifdef CONFIG_CRYPTO_SM2 +static int cert_sig_digest_update (const struct public_key_signature *sig, +struct crypto_akcipher *tfm_pkey) +{ +struct x509_certificate *cert = sig->cert; +struct crypto_shash *tfm; +struct shash_desc *desc; +size_t desc_size; +unsigned char dgst[SM3_DIGEST_SIZE ]; +int ret; + +if (!cert) +return -EINVAL ; + + ret =sm2_compute_z_digest (tfm_pkey,SM2_DEFAULT_USERID , +SM2_DEFAULT_USERID_LEN , dgst); +if (ret) +return ret; + + tfm =crypto_alloc_shash (sig->hash_algo,0 ,0 ); +if (IS_ERR (tfm)) +return PTR_ERR (tfm); + + desc_size =crypto_shash_descsize (tfm) +sizeof (*desc); + desc =kzalloc (desc_size,GFP_KERNEL ); +if (!desc) + goto error_free_tfm; + + desc->tfm = tfm; + + ret =crypto_shash_init (desc); +if (ret <0 ) + goto error_free_desc; + + ret =crypto_shash_update (desc, dgst,SM3_DIGEST_SIZE ); +if (ret <0 ) + goto error_free_desc; + + ret =crypto_shash_finup (desc, cert->tbs, cert->tbs_size, sig->digest); + +error_free_desc: +kfree (desc); +error_free_tfm: +crypto_free_shash (tfm); +return ret; +} +#endif +/* * Verify a signature using a public key. */
crypto/asymmetric_keys/public_key.c int public_key_verify_signature(const struct public_key *pkey, · line 353
Quoted 3 lines
crypto/asymmetric_keys/public_key.c +0
-0@@ -298,6 +351,14 @@ int public_key_verify_signature(const struct public_key *pkey,if (ret) goto error_free_key;
OK, how about you put cert_sig_digest_update() in a separate file that only gets compiled with CONFIG_CRYPTO_SM2 and have a static inline version that returns -ENOTSUPP otherwise?
or at least something in this spirit.
Done right it will allow you to drop the ifdefs and make for a much cleaner code.
Quoted 39 lines
crypto/asymmetric_keys/public_key.c +8
-0+#ifdef CONFIG_CRYPTO_SM2 +if (strcmp (sig->pkey_algo,"sm2" ) ==0 ) { + ret =cert_sig_digest_update (sig, tfm); +if (ret) + goto error_free_key; + } +#endif +sg_init_table (src_sg,2 );sg_set_buf (&src_sg[0 ], sig->s, sig->s_size);sg_set_buf (&src_sg[1 ], sig->digest, sig->digest_size);crypto/asymmetric_keys/x509_public_key.c +2
-0@@ -30,6 +30,8 @@ int x509_get_sig_params(struct x509_certificate *cert)pr_devel ("==>%s()\n" , __func__); + sig->cert = cert; +if (!cert->pub->pkey_algo) cert->unsupported_key = true;include/crypto/public_key.h +1
-1@@ -44,6 +44,7 @@ struct public_key_signature {const char *pkey_algo;const char *hash_algo;const char *encoding; +void *cert;/* For certificate */ };extern void public_key_signature_free (struct public_key_signature *sig); --2.17.1
Signature · 4 lines
Gilad Ben-Yossef Chief Coffee Drinker values of β will give rise to dom!
Re: [PATCH 7/7] X.509: support OSCCA sm2-with-sm3 certificate verification
Quoted 150 lines · Gilad Ben-Yossef
Hi,
Quoted 30 lines · Tianjia Zhang
The digital certificate format based on SM2 crypto algorithm as specified in GM/T 0015-2012. It was published by State Encryption Management Bureau, China.
The method of generating Other User Information is defined as ZA=H256(ENTLA || IDA || a || b || xG || yG || xA || yA), it also specified in https://tools.ietf.org/html/draft-shen-sm2-ecdsa-02.
The x509 certificate supports sm2-with-sm3 type certificate verification. Because certificate verification requires ZA in addition to tbs data, ZA also depends on elliptic curve parameters and public key data, so you need to access tbs in sig and calculate ZA. Finally calculate the digest of the signature and complete the verification work. The calculation process of ZA is declared in specifications GM/T 0009-2012 and GM/T 0003.2-2012.
Signed-off-by Tianjia Zhang <[email protected]>--- crypto/asymmetric_keys/public_key.c | 61 ++++++++++++++++++++++++ crypto/asymmetric_keys/x509_public_key.c | 2 + include/crypto/public_key.h | 1 + 3 files changed, 64 insertions(+)crypto/asymmetric_keys/public_key.c +0
-0crypto/asymmetric_keys/public_key.c line 19
Quoted 3 lines
crypto/asymmetric_keys/public_key.c +0
-0@@ -17,6 +17,11 @@#include <keys/asymmetric-subtype.h> #include <crypto/public_key.h> #include <crypto/akcipher.h> hmmm... ifdefs like these are kind of ugly.
Quoted 64 lines
crypto/asymmetric_keys/public_key.c +53
-0+#ifdef CONFIG_CRYPTO_SM2 +#include <crypto/sm3_base.h> +#include <crypto/sm2.h> +#include "x509_parser.h" +#endif MODULE_DESCRIPTION ("In-software asymmetric public-key subtype" );MODULE_AUTHOR ("Red Hat, Inc." );@@ -245,6 +250,54 @@ static int software_key_eds_op(struct kernel_pkey_params *params,return ret; } +#ifdef CONFIG_CRYPTO_SM2 +static int cert_sig_digest_update (const struct public_key_signature *sig, +struct crypto_akcipher *tfm_pkey) +{ +struct x509_certificate *cert = sig->cert; +struct crypto_shash *tfm; +struct shash_desc *desc; +size_t desc_size; +unsigned char dgst[SM3_DIGEST_SIZE ]; +int ret; + +if (!cert) +return -EINVAL ; + + ret =sm2_compute_z_digest (tfm_pkey,SM2_DEFAULT_USERID , +SM2_DEFAULT_USERID_LEN , dgst); +if (ret) +return ret; + + tfm =crypto_alloc_shash (sig->hash_algo,0 ,0 ); +if (IS_ERR (tfm)) +return PTR_ERR (tfm); + + desc_size =crypto_shash_descsize (tfm) +sizeof (*desc); + desc =kzalloc (desc_size,GFP_KERNEL ); +if (!desc) + goto error_free_tfm; + + desc->tfm = tfm; + + ret =crypto_shash_init (desc); +if (ret <0 ) + goto error_free_desc; + + ret =crypto_shash_update (desc, dgst,SM3_DIGEST_SIZE ); +if (ret <0 ) + goto error_free_desc; + + ret =crypto_shash_finup (desc, cert->tbs, cert->tbs_size, sig->digest); + +error_free_desc: +kfree (desc); +error_free_tfm: +crypto_free_shash (tfm); +return ret; +} +#endif +/* * Verify a signature using a public key. */ crypto/asymmetric_keys/public_key.c int public_key_verify_signature(const struct public_key *pkey, · line 353
Quoted 3 lines
crypto/asymmetric_keys/public_key.c +0
-0@@ -298,6 +351,14 @@ int public_key_verify_signature(const struct public_key *pkey,if (ret) goto error_free_key;OK, how about you put
cert_sig_digest_update()in a separate file that only gets compiled withCONFIG_CRYPTO_SM2and have a static inline version that returns-ENOTSUPPotherwise?
or at least something in this spirit.
Done right it will allow you to drop the ifdefs and make for a much cleaner code.Quoted 35 lines
crypto/asymmetric_keys/public_key.c +8
-0+#ifdef CONFIG_CRYPTO_SM2 +if (strcmp (sig->pkey_algo,"sm2" ) ==0 ) { + ret =cert_sig_digest_update (sig, tfm); +if (ret) + goto error_free_key; + } +#endif +sg_init_table (src_sg,2 );sg_set_buf (&src_sg[0 ], sig->s, sig->s_size);sg_set_buf (&src_sg[1 ], sig->digest, sig->digest_size);crypto/asymmetric_keys/x509_public_key.c +2
-0@@ -30,6 +30,8 @@ int x509_get_sig_params(struct x509_certificate *cert)pr_devel ("==>%s()\n" , __func__); + sig->cert = cert; +if (!cert->pub->pkey_algo) cert->unsupported_key = true;include/crypto/public_key.h +1
-0@@ -44,6 +44,7 @@ struct public_key_signature {const char *pkey_algo;const char *hash_algo;const char *encoding; +void *cert;/* For certificate */ };
Quoted 6 lines
Quoted 4 lines
include/crypto/public_key.h +0
-1extern void public_key_signature_free (struct public_key_signature *sig); --2.17.1
Hi,
Thanks for your suggestion, it is indeed appropriate to unify the SM2 implementation with the public code, I will implement it.
Thanks,
Tianjia
Quoted 27 lines · Tianjia Zhang
Hello all,
This new module implement the OSCCA certificate and SM2 public key algorithm. It was published by State Encryption Management Bureau, China. List of specifications for OSCCA certificate and SM2 elliptic curve public key cryptography:
* GM/T 0003.1-2012
* GM/T 0003.2-2012
* GM/T 0003.3-2012
* GM/T 0003.4-2012
* GM/T 0003.5-2012
* GM/T 0015-2012
* GM/T 0009-2012IETF: https://tools.ietf.org/html/draft-shen-sm2-ecdsa-02
oscca: http://www.oscca.gov.cn/sca/xxgk/2010-12/17/content_1002386.shtml
scctc: http://www.gmbz.org.cn/main/bzlb.htmlThese patchs add the OID object identifier defined by OSCCA. The x509 certificate supports sm2-with-sm3 type certificate parsing and verification.
The sm2 algorithm is based on libgcrypt's mpi implementation, and has made some additions to the kernel's original mpi library, and added the implementation of ec to better support elliptic curve-like algorithms.
Quoted 8 lines
sm2 has good support in both openssl and gnupg projects, and sm3 and sm4 of the OSCCA algorithm family have also been implemented in the kernel.
Signed-off-by Tianjia Zhang <[email protected]>Thanks,
Tianjia
Hello all,
This is the review request.
The OSCCA certificate and related algorithms used to verify the certificate are newly introduced. Among them, sm3 and sm4 have been well implemented in the kernel. This group of patches has newly introduced sm2.
In order to implement sm2 more perfectly, I expanded the mpi library and introduced the ec implementation of the mpi library as the basic algorithm. Compared to the kernel's crypto/ecc.c, the implementation of mpi/ec.c is more complete and elegant, sm2 is implemented based on these algorithms. At this point, the kernel can parse and verify sm2-with-sm3 certificates normally.
Thanks,
Tianjia