Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions crypto/err/openssl.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1236,6 +1236,8 @@ SM2_F_SM2_COMPUTE_USERID_DIGEST:101:sm2_compute_userid_digest
SM2_F_SM2_COMPUTE_Z_DIGEST:113:sm2_compute_z_digest
SM2_F_SM2_DECRYPT:102:sm2_decrypt
SM2_F_SM2_ENCRYPT:103:sm2_encrypt
SM2_F_SM2_INTERNAL_SIGN:116:
SM2_F_SM2_INTERNAL_VERIFY:117:
SM2_F_SM2_PLAINTEXT_SIZE:104:sm2_plaintext_size
SM2_F_SM2_SIGN:105:sm2_sign
SM2_F_SM2_SIG_GEN:106:sm2_sig_gen
Expand Down
2 changes: 1 addition & 1 deletion crypto/sm2/build.info
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
LIBS=../../libcrypto
SOURCE[../../libcrypto]=\
sm2_sign.c sm2_crypt.c sm2_err.c sm2_pmeth.c
sm2_sign.c sm2_crypt.c sm2_err.c sm2_pmeth.c sm2_aid.c


45 changes: 45 additions & 0 deletions crypto/sm2/sm2_aid.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems this whole file is not needed anymore, since the new DER part has already handled this.

Copy link
Member

@levitte levitte May 25, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The infrastructure is there, but you will need to add SM2 specific things.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. SM2 specific stuffs are added in the latest commit - hope I have done right...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can help with those details this evening

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! One struggling thing is SM2 has no PublicKey OID. An SM2 key is encoded with id_ecPublickey IIRC at current stage, which is compatible with OpenSSL 1.1.1.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I know. That is a bit of a problem, and means that certain assumptions need to be made...
I will have to look again how I solved that for the legacy implementation. I forget...

* Copyright 2020 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (the "License"). You may not use
* this file except in compliance with the License. You can obtain a copy
* in the file LICENSE in the source distribution or at
* https://www.openssl.org/source/license.html
*/

#include <stdlib.h>

#include <openssl/objects.h>
#include "crypto/sm2.h"

#define ASN1_SEQUENCE 0x30
#define ASN1_OID 0x06
#define OID_FIRST(a, b) a * 40 + b
#define DER_156() 0x81, 0x1C /* DER encoding of number 156 is 2 bytes */
#define DER_10197() 0xCF, 0x55 /* DER encoding of number 10197 is 2 bytes */
#define DER_501() 0x83, 0x75 /* DER encoding of number 501 is 2 bytes */
#define SM3_SZ 8

/* SM2-with-SM3 OID is of the form : (1 2 156 10197 1 501) */
#define ENCODE_ALGORITHMIDENTIFIER_SM3(name) \
static const unsigned char algorithmidentifier_##name##_der[] = { \
ASN1_SEQUENCE, 2 + SM3_SZ, \
ASN1_OID, SM3_SZ, OID_FIRST(1, 2), DER_156(), DER_10197(), 1, DER_501() \
}

/* not decided yet if SM2 should support other MDs */
ENCODE_ALGORITHMIDENTIFIER_SM3(sm3);

#define MD_CASE(name) \
case NID_##name: \
*len = sizeof(algorithmidentifier_##name##_der); \
return algorithmidentifier_##name##_der

const unsigned char *sm2_algorithmidentifier_encoding(int md_nid, size_t *len)
{
switch (md_nid) {
MD_CASE(sm3);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All of this seems a little overkill for one digest. Couldn't this be simplified on the assumption that there is only ever one interesting digest for use with SM2?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, I am struggling with this. It seems that SM2 should only be used with SM3 since the spec says SM2 should only the hash functions designated by the cryptography administration department and, the only hash function defined in SM ciphers is SM3.

But this is the thing that interesting. SM2 actually can use other hash algorithm other than SM3. So what I consider is that if there are educational or experimental use cases that require to use other hash functions - for instance, to have a performance benchmark among different hash functions with SM2

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So maybe we need to provide the flexibility of the hash function choice and let the provider to decide if it will to be complied with Chinese validation programs.

default:
return NULL;
}
}
2 changes: 1 addition & 1 deletion crypto/sm2/sm2_err.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Generated by util/mkerr.pl DO NOT EDIT
* Copyright 1995-2019 The OpenSSL Project Authors. All Rights Reserved.
* Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (the "License"). You may not use
* this file except in compliance with the License. You can obtain a copy
Expand Down
4 changes: 2 additions & 2 deletions crypto/sm2/sm2_pmeth.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ static int pkey_sm2_sign(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
return 0;
}

ret = sm2_sign(tbs, tbslen, sig, &sltmp, ec);
ret = sm2_internal_sign(tbs, tbslen, sig, &sltmp, ec);

if (ret <= 0)
return ret;
Expand All @@ -117,7 +117,7 @@ static int pkey_sm2_verify(EVP_PKEY_CTX *ctx,
{
EC_KEY *ec = ctx->pkey->pkey.ec;

return sm2_verify(tbs, tbslen, sig, siglen, ec);
return sm2_internal_verify(tbs, tbslen, sig, siglen, ec);
}

static int pkey_sm2_encrypt(EVP_PKEY_CTX *ctx,
Expand Down
20 changes: 10 additions & 10 deletions crypto/sm2/sm2_sign.c
Original file line number Diff line number Diff line change
Expand Up @@ -406,8 +406,8 @@ int sm2_do_verify(const EC_KEY *key,
return ret;
}

int sm2_sign(const unsigned char *dgst, int dgstlen,
unsigned char *sig, unsigned int *siglen, EC_KEY *eckey)
int sm2_internal_sign(const unsigned char *dgst, int dgstlen,
unsigned char *sig, unsigned int *siglen, EC_KEY *eckey)
{
BIGNUM *e = NULL;
ECDSA_SIG *s = NULL;
Expand All @@ -416,15 +416,15 @@ int sm2_sign(const unsigned char *dgst, int dgstlen,

e = BN_bin2bn(dgst, dgstlen, NULL);
if (e == NULL) {
SM2err(SM2_F_SM2_SIGN, ERR_R_BN_LIB);
SM2err(SM2_F_SM2_INTERNAL_SIGN, ERR_R_BN_LIB);
goto done;
}

s = sm2_sig_gen(eckey, e);

sigleni = i2d_ECDSA_SIG(s, &sig);
if (sigleni < 0) {
SM2err(SM2_F_SM2_SIGN, ERR_R_INTERNAL_ERROR);
SM2err(SM2_F_SM2_INTERNAL_SIGN, ERR_R_INTERNAL_ERROR);
goto done;
}
*siglen = (unsigned int)sigleni;
Expand All @@ -437,8 +437,8 @@ int sm2_sign(const unsigned char *dgst, int dgstlen,
return ret;
}

int sm2_verify(const unsigned char *dgst, int dgstlen,
const unsigned char *sig, int sig_len, EC_KEY *eckey)
int sm2_internal_verify(const unsigned char *dgst, int dgstlen,
const unsigned char *sig, int sig_len, EC_KEY *eckey)
{
ECDSA_SIG *s = NULL;
BIGNUM *e = NULL;
Expand All @@ -449,23 +449,23 @@ int sm2_verify(const unsigned char *dgst, int dgstlen,

s = ECDSA_SIG_new();
if (s == NULL) {
SM2err(SM2_F_SM2_VERIFY, ERR_R_MALLOC_FAILURE);
SM2err(SM2_F_SM2_INTERNAL_VERIFY, ERR_R_MALLOC_FAILURE);
goto done;
}
if (d2i_ECDSA_SIG(&s, &p, sig_len) == NULL) {
SM2err(SM2_F_SM2_VERIFY, SM2_R_INVALID_ENCODING);
SM2err(SM2_F_SM2_INTERNAL_VERIFY, SM2_R_INVALID_ENCODING);
goto done;
}
/* Ensure signature uses DER and doesn't have trailing garbage */
derlen = i2d_ECDSA_SIG(s, &der);
if (derlen != sig_len || memcmp(sig, der, derlen) != 0) {
SM2err(SM2_F_SM2_VERIFY, SM2_R_INVALID_ENCODING);
SM2err(SM2_F_SM2_INTERNAL_VERIFY, SM2_R_INVALID_ENCODING);
goto done;
}

e = BN_bin2bn(dgst, dgstlen, NULL);
if (e == NULL) {
SM2err(SM2_F_SM2_VERIFY, ERR_R_BN_LIB);
SM2err(SM2_F_SM2_INTERNAL_VERIFY, ERR_R_BN_LIB);
goto done;
}

Expand Down
9 changes: 5 additions & 4 deletions include/crypto/sm2.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@ int sm2_do_verify(const EC_KEY *key,
/*
* SM2 signature generation.
*/
int sm2_sign(const unsigned char *dgst, int dgstlen,
unsigned char *sig, unsigned int *siglen, EC_KEY *eckey);
int sm2_internal_sign(const unsigned char *dgst, int dgstlen,
unsigned char *sig, unsigned int *siglen, EC_KEY *eckey);

/*
* SM2 signature verification.
*/
int sm2_verify(const unsigned char *dgst, int dgstlen,
const unsigned char *sig, int siglen, EC_KEY *eckey);
int sm2_internal_verify(const unsigned char *dgst, int dgstlen,
const unsigned char *sig, int siglen, EC_KEY *eckey);

/*
* SM2 encryption
Expand All @@ -74,5 +74,6 @@ int sm2_decrypt(const EC_KEY *key,
const uint8_t *ciphertext,
size_t ciphertext_len, uint8_t *ptext_buf, size_t *ptext_len);

const unsigned char *sm2_algorithmidentifier_encoding(int md_nid, size_t *len);
# endif /* OPENSSL_NO_SM2 */
#endif
8 changes: 5 additions & 3 deletions include/crypto/sm2err.h
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
/*
* Generated by util/mkerr.pl DO NOT EDIT
* Copyright 1995-2019 The OpenSSL Project Authors. All Rights Reserved.
* Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (the "License"). You may not use
* this file except in compliance with the License. You can obtain a copy
* in the file LICENSE in the source distribution or at
* https://www.openssl.org/source/license.html
*/

#ifndef OSSL_CRYPTO_SM2ERR_H
# define OSSL_CRYPTO_SM2ERR_H
#ifndef OPENSSL_SM2ERR_H
# define OPENSSL_SM2ERR_H

# include <openssl/opensslconf.h>
# include <openssl/symhacks.h>
Expand Down Expand Up @@ -39,6 +39,8 @@ int ERR_load_SM2_strings(void);
# define SM2_F_SM2_COMPUTE_Z_DIGEST 0
# define SM2_F_SM2_DECRYPT 0
# define SM2_F_SM2_ENCRYPT 0
# define SM2_F_SM2_INTERNAL_SIGN 0
# define SM2_F_SM2_INTERNAL_VERIFY 0
# define SM2_F_SM2_PLAINTEXT_SIZE 0
# define SM2_F_SM2_SIGN 0
# define SM2_F_SM2_SIG_GEN 0
Expand Down
1 change: 1 addition & 0 deletions include/openssl/core_names.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ extern "C" {
#define OSSL_DIGEST_NAME_SHA3_512 "SHA3-512"
#define OSSL_DIGEST_NAME_KECCAK_KMAC128 "KECCAK-KMAC-128"
#define OSSL_DIGEST_NAME_KECCAK_KMAC256 "KECCAK-KMAC-256"
#define OSSL_DIGEST_NAME_SM3 "SM3"

/* MAC parameters */
#define OSSL_MAC_PARAM_KEY "key" /* octet string */
Expand Down
11 changes: 11 additions & 0 deletions providers/common/der/SM2.asn1
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
oscca OBJECT IDENTIFIER ::= { iso(1) member-body(2) cn(156) 10197 }

sm-scheme OBJECT IDENTIFIER ::= { oscca 1 }

-- OID for SM2 signatures with SM3

sm2-with-SM3 OBJECT IDENTIFIER ::= { sm-scheme 501 }

-- Named Elliptic Curves of SM2

curveSM2 OBJECT IDENTIFIER ::= { sm-scheme 301 }
16 changes: 15 additions & 1 deletion providers/common/der/build.info
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,19 @@ DEPEND[${DER_EC_GEN/.c/.o}]=$DER_EC_H
GENERATE[$DER_EC_H]=der_ec.h.in
DEPEND[$DER_EC_H]=oids_to_c.pm

#----- SM2
$DER_SM2_H=../include/prov/der_sm2.h
$DER_SM2_GEN=der_sm2_gen.c
$DER_SM2_AUX=der_sm2_key.c der_sm2_sig.c

GENERATE[$DER_SM2_GEN]=der_sm2_gen.c.in
DEPEND[$DER_SM2_GEN]=oids_to_c.pm

DEPEND[${DER_SM2_AUX/.c/.o}]=$DER_SM2_H
DEPEND[${DER_SM2_GEN/.c/.o}]=$DER_SM2_H
GENERATE[$DER_SM2_H]=der_sm2.h.in
DEPEND[$DER_SM2_H]=oids_to_c.pm

#----- Conclusion

# TODO(3.0) $COMMON should go to libcommon.a, but this currently leads
Expand All @@ -59,6 +72,7 @@ $COMMON=\
$DER_RSA_COMMON \
$DER_DSA_GEN $DER_DSA_AUX \
$DER_EC_GEN $DER_EC_AUX \
$DER_DIGESTS_GEN
$DER_DIGESTS_GEN \
$DER_SM2_GEN $DER_SM2_AUX
SOURCE[../../libfips.a]=$COMMON $DER_RSA_FIPSABLE
SOURCE[../../libnonfips.a]=$COMMON $DER_RSA_FIPSABLE
23 changes: 23 additions & 0 deletions providers/common/der/der_sm2.h.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright 2020 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (the "License"). You may not use
* this file except in compliance with the License. You can obtain a copy
* in the file LICENSE in the source distribution or at
* https://www.openssl.org/source/license.html
*/

#include "internal/der.h"

/* Well known OIDs precompiled */
{-
$OUT = oids_to_c::process_leaves('providers/common/der/SM2.asn1',
{ dir => $config{sourcedir},
filter => \&oids_to_c::filter_to_H });
-}

/* Subject Public Key Info */
int DER_w_algorithmIdentifier_SM2(WPACKET *pkt, int cont, EC_KEY *ec);
/* Signature */
int DER_w_algorithmIdentifier_SM2_with_MD(WPACKET *pkt, int cont,
EC_KEY *ec, int mdnid);
17 changes: 17 additions & 0 deletions providers/common/der/der_sm2_gen.c.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* Copyright 2020 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (the "License"). You may not use
* this file except in compliance with the License. You can obtain a copy
* in the file LICENSE in the source distribution or at
* https://www.openssl.org/source/license.html
*/

#include "prov/der_sm2.h"

/* Well known OIDs precompiled */
{-
$OUT = oids_to_c::process_leaves('providers/common/der/SM2.asn1',
{ dir => $config{sourcedir},
filter => \&oids_to_c::filter_to_C });
-}
23 changes: 23 additions & 0 deletions providers/common/der/der_sm2_key.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright 2020 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (the "License"). You may not use
* this file except in compliance with the License. You can obtain a copy
* in the file LICENSE in the source distribution or at
* https://www.openssl.org/source/license.html
*/

#include <openssl/obj_mac.h>
#include "internal/packet.h"
#include "prov/der_ec.h"
#include "prov/der_sm2.h"

int DER_w_algorithmIdentifier_SM2(WPACKET *pkt, int cont, EC_KEY *ec)
{
return DER_w_begin_sequence(pkt, cont)
/* No parameters (yet?) */
/* It seems SM2 identifier is the same to id_ecPublidKey */
&& DER_w_precompiled(pkt, -1, der_oid_id_ecPublicKey,
sizeof(der_oid_id_ecPublicKey))
&& DER_w_end_sequence(pkt, cont);
}
39 changes: 39 additions & 0 deletions providers/common/der/der_sm2_sig.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright 2020 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (the "License"). You may not use
* this file except in compliance with the License. You can obtain a copy
* in the file LICENSE in the source distribution or at
* https://www.openssl.org/source/license.html
*/

#include <openssl/obj_mac.h>
#include "internal/packet.h"
#include "prov/der_sm2.h"

/* Aliases so we can have a uniform MD_CASE */
#define der_oid_id_sm2_with_sm3 der_oid_sm2_with_SM3

#define MD_CASE(name) \
case NID_##name: \
precompiled = der_oid_id_sm2_with_##name; \
precompiled_sz = sizeof(der_oid_id_sm2_with_##name); \
break;

int DER_w_algorithmIdentifier_SM2_with_MD(WPACKET *pkt, int cont,
EC_KEY *ec, int mdnid)
{
const unsigned char *precompiled = NULL;
size_t precompiled_sz = 0;

switch (mdnid) {
MD_CASE(sm3);
default:
return 0;
}

return DER_w_begin_sequence(pkt, cont)
/* No parameters (yet?) */
&& DER_w_precompiled(pkt, -1, precompiled, precompiled_sz)
&& DER_w_end_sequence(pkt, cont);
}
3 changes: 3 additions & 0 deletions providers/defltprov.c
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,9 @@ static const OSSL_ALGORITHM deflt_signature[] = {
{ "ED25519:Ed25519", "provider=default", ed25519_signature_functions },
{ "ED448:Ed448", "provider=default", ed448_signature_functions },
{ "ECDSA", "provider=default", ecdsa_signature_functions },
# ifndef OPENSSL_NO_SM2
{ "SM2", "provider=default", sm2_signature_functions },
# endif
#endif
{ NULL, NULL, NULL }
};
Expand Down
2 changes: 1 addition & 1 deletion providers/implementations/include/prov/implementations.h
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ extern const OSSL_DISPATCH rsa_signature_functions[];
extern const OSSL_DISPATCH ed25519_signature_functions[];
extern const OSSL_DISPATCH ed448_signature_functions[];
extern const OSSL_DISPATCH ecdsa_signature_functions[];

extern const OSSL_DISPATCH sm2_signature_functions[];

/* Asym Cipher */
extern const OSSL_DISPATCH rsa_asym_cipher_functions[];
Expand Down
Loading