mirror of
https://github.com/guanzhi/GmSSL.git
synced 2026-06-24 14:13:45 +08:00
some update
This commit is contained in:
@@ -58,6 +58,7 @@
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef struct CBCMAC_CTX_st CBCMAC_CTX;
|
||||
|
||||
CBCMAC_CTX *CBCMAC_CTX_new(void);
|
||||
void CBCMAC_CTX_cleanup(CBCMAC_CTX *ctx);
|
||||
|
||||
@@ -171,6 +171,7 @@ void OpenSSL_add_all_ciphers(void)
|
||||
EVP_add_cipher(EVP_sms4_cfb8());
|
||||
EVP_add_cipher(EVP_sms4_ofb());
|
||||
EVP_add_cipher(EVP_sms4_ctr());
|
||||
EVP_add_cipher(EVP_sms4_wrap());
|
||||
EVP_add_cipher_alias(SN_sms4_cbc,"SMS4");
|
||||
EVP_add_cipher_alias(SN_sms4_cbc,"sms4");
|
||||
#endif
|
||||
|
||||
@@ -211,7 +211,7 @@ int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *impl)
|
||||
type = ctx->digest;
|
||||
}
|
||||
#endif
|
||||
fprintf(stderr, "%s %d\n", __FILE__, __LINE__);
|
||||
//fprintf(stderr, "%s %d\n", __FILE__, __LINE__);
|
||||
if (ctx->digest != type) {
|
||||
if (ctx->digest && ctx->digest->ctx_size)
|
||||
OPENSSL_free(ctx->md_data);
|
||||
@@ -219,7 +219,7 @@ int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *impl)
|
||||
if (!(ctx->flags & EVP_MD_CTX_FLAG_NO_INIT) && type->ctx_size) {
|
||||
ctx->update = type->update;
|
||||
ctx->md_data = OPENSSL_malloc(type->ctx_size);
|
||||
fprintf(stderr, "%s %d\n", __FILE__, __LINE__);
|
||||
//fprintf(stderr, "%s %d\n", __FILE__, __LINE__);
|
||||
if (ctx->md_data == NULL) {
|
||||
EVPerr(EVP_F_EVP_DIGESTINIT_EX, ERR_R_MALLOC_FAILURE);
|
||||
return 0;
|
||||
@@ -231,7 +231,7 @@ int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *impl)
|
||||
#endif
|
||||
if (ctx->pctx) {
|
||||
int r;
|
||||
fprintf(stderr, "%s %d\n", __FILE__, __LINE__);
|
||||
//fprintf(stderr, "%s %d\n", __FILE__, __LINE__);
|
||||
r = EVP_PKEY_CTX_ctrl(ctx->pctx, -1, EVP_PKEY_OP_TYPE_SIG,
|
||||
EVP_PKEY_CTRL_DIGESTINIT, 0, ctx);
|
||||
if (r <= 0 && (r != -2))
|
||||
@@ -248,7 +248,7 @@ int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *impl)
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
fprintf(stderr, "%s %d\n", __FILE__, __LINE__);
|
||||
//fprintf(stderr, "%s %d\n", __FILE__, __LINE__);
|
||||
return ctx->digest->init(ctx);
|
||||
}
|
||||
|
||||
|
||||
@@ -178,7 +178,7 @@ static int sms4_ctr_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
|
||||
EVP_SMS4_KEY *sms4 = (EVP_SMS4_KEY *)ctx->cipher_data;
|
||||
|
||||
CRYPTO_ctr128_encrypt_ctr32(in, out, len, &sms4->ks, ctx->iv, ctx->buf,
|
||||
&num, sms4_ctr128_encrypt);
|
||||
&num, (ctr128_f)sms4_encrypt);
|
||||
|
||||
ctx->num = (size_t)num;
|
||||
return 1;
|
||||
@@ -240,6 +240,7 @@ static int sms4_gcm_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
|
||||
|
||||
|
||||
}
|
||||
#endif
|
||||
|
||||
typedef struct {
|
||||
union {
|
||||
@@ -253,28 +254,81 @@ typedef struct {
|
||||
static int sms4_wrap_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
|
||||
const unsigned char *iv, int enc)
|
||||
{
|
||||
EVP_SMS4_WRAP_CTX *sms4_wrap = ctx->cipher_data;
|
||||
|
||||
if (!iv && !key)
|
||||
return 1;
|
||||
|
||||
if (key) {
|
||||
if (ctx->encrypt) {
|
||||
sms4_set_encrypt_key(&sms4_wrap->ks.ks, key);
|
||||
} else {
|
||||
sms4_set_decrypt_key(&sms4_wrap->ks.ks, key);
|
||||
}
|
||||
|
||||
if (!iv) {
|
||||
sms4_wrap->iv = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
if (iv) {
|
||||
memcpy(ctx->iv, iv, 8);
|
||||
sms4_wrap->iv = ctx->iv;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
static int sms4_wrap_do_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
|
||||
const unsigned char *in, size_t inlen)
|
||||
{
|
||||
return -1;
|
||||
EVP_SMS4_WRAP_CTX *sms4_wrap = ctx->cipher_data;
|
||||
size_t rv;
|
||||
|
||||
if (!in) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (inlen % 8) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (ctx->encrypt && inlen < 8)
|
||||
return -1;
|
||||
|
||||
if (!ctx->encrypt && inlen < 8)
|
||||
return -1;
|
||||
|
||||
if (!out) {
|
||||
if (ctx->encrypt)
|
||||
return inlen + 8;
|
||||
else return inlen - 8;
|
||||
}
|
||||
|
||||
if (ctx->encrypt)
|
||||
rv = CRYPTO_128_wrap(&sms4_wrap->ks.ks, sms4_wrap->iv,
|
||||
out, in, inlen, (block128_f)sms4_encrypt);
|
||||
else rv = CRYPTO_128_unwrap(&sms4_wrap->ks.ks, sms4_wrap->iv,
|
||||
out, in, inlen, (block128_f)sms4_encrypt);
|
||||
|
||||
return rv ? (int)rv : -1;
|
||||
}
|
||||
|
||||
|
||||
#define WRAP_FLAGS (EVP_CIPH_WRAP_MODE \
|
||||
#define SMS4_WRAP_FLAGS (EVP_CIPH_WRAP_MODE \
|
||||
| EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER \
|
||||
| EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_FLAG_DEFAULT_ASN1)
|
||||
|
||||
|
||||
#define SMS4_WRAP_BLOCK_SIZE 8
|
||||
#define SMS4_WRAP_IV_LENGTH 8
|
||||
|
||||
const EVP_CIPHER sms4_wrap = {
|
||||
NID_sms4_wrap,
|
||||
SMS4_WRAP_BLOCK_SIZE,
|
||||
SMS4_KEY_LENGTH,
|
||||
SMS4_WRAP_IV_LENGTH,
|
||||
WRAP_FLAGS,
|
||||
SMS4_WRAP_FLAGS,
|
||||
sms4_wrap_init_key,
|
||||
sms4_wrap_do_cipher,
|
||||
NULL, /* cleanup() */
|
||||
@@ -290,6 +344,5 @@ const EVP_CIPHER *EVP_sms4_wrap(void)
|
||||
return &sms4_wrap;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
@@ -657,13 +657,6 @@ int EVP_CipherUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
|
||||
int EVP_CipherFinal(EVP_CIPHER_CTX *ctx, unsigned char *outm, int *outl);
|
||||
int EVP_CipherFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *outm, int *outl);
|
||||
|
||||
#ifndef OPENSSL_NO_GMSSL
|
||||
int EVP_Encrypt_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outlen,
|
||||
const unsigned char *in, int inlen);
|
||||
int EVP_Decrypt_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outlen,
|
||||
const unsigned char *in, int inlen);
|
||||
#endif
|
||||
|
||||
int EVP_SignFinal(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *s,
|
||||
EVP_PKEY *pkey);
|
||||
|
||||
|
||||
@@ -87,7 +87,7 @@ static int do_sigver_init(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
|
||||
}
|
||||
}
|
||||
|
||||
fprintf(stderr, "%s %d\n", __FILE__, __LINE__);
|
||||
//fprintf(stderr, "%s %d\n", __FILE__, __LINE__);
|
||||
|
||||
if (ver) {
|
||||
if (ctx->pctx->pmeth->verifyctx_init) {
|
||||
@@ -104,16 +104,16 @@ static int do_sigver_init(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
|
||||
} else if (EVP_PKEY_sign_init(ctx->pctx) <= 0)
|
||||
return 0;
|
||||
}
|
||||
fprintf(stderr, "%s %d\n", __FILE__, __LINE__);
|
||||
//fprintf(stderr, "%s %d\n", __FILE__, __LINE__);
|
||||
if (EVP_PKEY_CTX_set_signature_md(ctx->pctx, type) <= 0)
|
||||
return 0;
|
||||
fprintf(stderr, "%s %d\n", __FILE__, __LINE__);
|
||||
//fprintf(stderr, "%s %d\n", __FILE__, __LINE__);
|
||||
if (pctx)
|
||||
*pctx = ctx->pctx;
|
||||
fprintf(stderr, "%s %d\n", __FILE__, __LINE__);
|
||||
//fprintf(stderr, "%s %d\n", __FILE__, __LINE__);
|
||||
if (ctx->pctx->pmeth->flags & EVP_PKEY_FLAG_SIGCTX_CUSTOM)
|
||||
return 1;
|
||||
fprintf(stderr, "%s %d\n", __FILE__, __LINE__);
|
||||
//fprintf(stderr, "%s %d\n", __FILE__, __LINE__);
|
||||
if (!EVP_DigestInit_ex(ctx, type, e))
|
||||
return 0;
|
||||
return 1;
|
||||
|
||||
@@ -198,16 +198,25 @@ EVP_PKEY *EVP_PKEY_new_mac_key(int type, ENGINE *e,
|
||||
EVP_PKEY_CTX *mac_ctx = NULL;
|
||||
EVP_PKEY *mac_key = NULL;
|
||||
mac_ctx = EVP_PKEY_CTX_new_id(type, e);
|
||||
if (!mac_ctx)
|
||||
if (!mac_ctx) {
|
||||
if (e) fprintf(stderr, "engine is not null\n");
|
||||
fprintf(stderr, "error %s %d\n", __FILE__, __LINE__);
|
||||
return NULL;
|
||||
if (EVP_PKEY_keygen_init(mac_ctx) <= 0)
|
||||
}
|
||||
if (EVP_PKEY_keygen_init(mac_ctx) <= 0) {
|
||||
fprintf(stderr, "error %s %d\n", __FILE__, __LINE__);
|
||||
goto merr;
|
||||
}
|
||||
if (EVP_PKEY_CTX_ctrl(mac_ctx, -1, EVP_PKEY_OP_KEYGEN,
|
||||
EVP_PKEY_CTRL_SET_MAC_KEY,
|
||||
keylen, (void *)key) <= 0)
|
||||
keylen, (void *)key) <= 0) {
|
||||
fprintf(stderr, "error %s %d\n", __FILE__, __LINE__);
|
||||
goto merr;
|
||||
if (EVP_PKEY_keygen(mac_ctx, &mac_key) <= 0)
|
||||
}
|
||||
if (EVP_PKEY_keygen(mac_ctx, &mac_key) <= 0) {
|
||||
fprintf(stderr, "error %s %d\n", __FILE__, __LINE__);
|
||||
goto merr;
|
||||
}
|
||||
merr:
|
||||
if (mac_ctx)
|
||||
EVP_PKEY_CTX_free(mac_ctx);
|
||||
|
||||
@@ -119,15 +119,30 @@ const EVP_PKEY_METHOD *EVP_PKEY_meth_find(int type)
|
||||
tmp.pkey_id = type;
|
||||
if (app_pkey_methods) {
|
||||
int idx;
|
||||
|
||||
//fprintf(stderr, "check %s %d\n", __FILE__, __LINE__);
|
||||
idx = sk_EVP_PKEY_METHOD_find(app_pkey_methods, &tmp);
|
||||
if (idx >= 0)
|
||||
return sk_EVP_PKEY_METHOD_value(app_pkey_methods, idx);
|
||||
//fprintf(stderr, "check %s %d\n", __FILE__, __LINE__);
|
||||
}
|
||||
//fprintf(stderr, "%s %d: t->pkey_id = %d\n", __FILE__, __LINE__, t->pkey_id);
|
||||
int i;
|
||||
for (i = 0; i < sizeof(standard_methods) / sizeof(EVP_PKEY_METHOD *); i++) {
|
||||
if (type == standard_methods[i]->pkey_id) {
|
||||
return standard_methods[i];
|
||||
}
|
||||
}
|
||||
/*
|
||||
ret = OBJ_bsearch_pmeth(&t, standard_methods,
|
||||
sizeof(standard_methods) /
|
||||
sizeof(EVP_PKEY_METHOD *));
|
||||
if (!ret || !*ret)
|
||||
|
||||
*/
|
||||
if (!ret || !*ret) {
|
||||
//fprintf(stderr, "check %s %d\n", __FILE__, __LINE__);
|
||||
return NULL;
|
||||
}
|
||||
return *ret;
|
||||
}
|
||||
|
||||
@@ -136,8 +151,10 @@ static EVP_PKEY_CTX *int_ctx_new(EVP_PKEY *pkey, ENGINE *e, int id)
|
||||
EVP_PKEY_CTX *ret;
|
||||
const EVP_PKEY_METHOD *pmeth;
|
||||
if (id == -1) {
|
||||
if (!pkey || !pkey->ameth)
|
||||
if (!pkey || !pkey->ameth) {
|
||||
fprintf(stderr, "error: %s %d\n", __FILE__, __LINE__);
|
||||
return NULL;
|
||||
}
|
||||
id = pkey->ameth->pkey_id;
|
||||
}
|
||||
#ifndef OPENSSL_NO_ENGINE
|
||||
@@ -159,7 +176,7 @@ static EVP_PKEY_CTX *int_ctx_new(EVP_PKEY *pkey, ENGINE *e, int id)
|
||||
|
||||
if (e)
|
||||
pmeth = ENGINE_get_pkey_meth(e, id);
|
||||
else
|
||||
else
|
||||
#endif
|
||||
pmeth = EVP_PKEY_meth_find(id);
|
||||
|
||||
|
||||
@@ -62,12 +62,12 @@
|
||||
* [including the GNU Public Licence.]
|
||||
*/
|
||||
|
||||
#define NUM_NID 1033
|
||||
#define NUM_SN 1011
|
||||
#define NUM_LN 1011
|
||||
#define NUM_OBJ 949
|
||||
#define NUM_NID 1034
|
||||
#define NUM_SN 1012
|
||||
#define NUM_LN 1012
|
||||
#define NUM_OBJ 950
|
||||
|
||||
static const unsigned char lvalues[6683]={
|
||||
static const unsigned char lvalues[6691]={
|
||||
0x2A,0x86,0x48,0x86,0xF7,0x0D, /* [ 0] OBJ_rsadsi */
|
||||
0x2A,0x86,0x48,0x86,0xF7,0x0D,0x01, /* [ 6] OBJ_pkcs */
|
||||
0x2A,0x86,0x48,0x86,0xF7,0x0D,0x02,0x02, /* [ 13] OBJ_md2 */
|
||||
@@ -1011,6 +1011,7 @@ static const unsigned char lvalues[6683]={
|
||||
0x2A,0x81,0x1C,0xCF,0x55,0x01,0x68,0x0A, /* [6658] OBJ_sms4_xts */
|
||||
0x2A,0x81,0x1C,0xCF,0x55,0x01,0x68,0x05, /* [6666] OBJ_sms4_cfb1 */
|
||||
0x2A,0x81,0x1C,0xCF,0x55,0x01,0x68,0x06, /* [6674] OBJ_sms4_cfb8 */
|
||||
0x2A,0x81,0x1C,0xCF,0x55,0x01,0x68,0x0B, /* [6682] OBJ_sms4_wrap */
|
||||
};
|
||||
|
||||
static const ASN1_OBJECT nid_objs[NUM_NID]={
|
||||
@@ -2667,6 +2668,7 @@ static const ASN1_OBJECT nid_objs[NUM_NID]={
|
||||
{"SM1-CFB","sm1-cfb",NID_sm1_cfb,0,NULL,0},
|
||||
{"SMS4-CFB1","sms4-cfb1",NID_sms4_cfb1,8,&(lvalues[6666]),0},
|
||||
{"SMS4-CFB8","sms4-cfb8",NID_sms4_cfb8,8,&(lvalues[6674]),0},
|
||||
{"SMS4-WRAP","sms4-wrap",NID_sms4_wrap,8,&(lvalues[6682]),0},
|
||||
};
|
||||
|
||||
static const unsigned int sn_objs[NUM_SN]={
|
||||
@@ -2879,6 +2881,7 @@ static const unsigned int sn_objs[NUM_SN]={
|
||||
977, /* "SMS4-ECB" */
|
||||
1027, /* "SMS4-GCM" */
|
||||
981, /* "SMS4-OFB" */
|
||||
1033, /* "SMS4-WRAP" */
|
||||
1029, /* "SMS4-XTS" */
|
||||
100, /* "SN" */
|
||||
1021, /* "SSF33-CBC" */
|
||||
@@ -4643,6 +4646,7 @@ static const unsigned int ln_objs[NUM_LN]={
|
||||
977, /* "sms4-ecb" */
|
||||
1027, /* "sms4-gcm" */
|
||||
981, /* "sms4-ofb" */
|
||||
1033, /* "sms4-wrap" */
|
||||
1029, /* "sms4-xts" */
|
||||
1021, /* "ssf33-cbc" */
|
||||
1023, /* "ssf33-cfb" */
|
||||
@@ -5153,6 +5157,7 @@ static const unsigned int obj_objs[NUM_OBJ]={
|
||||
1027, /* OBJ_sms4_gcm 1 2 156 10197 1 104 8 */
|
||||
1028, /* OBJ_sms4_ccm 1 2 156 10197 1 104 9 */
|
||||
1029, /* OBJ_sms4_xts 1 2 156 10197 1 104 10 */
|
||||
1033, /* OBJ_sms4_wrap 1 2 156 10197 1 104 11 */
|
||||
1006, /* OBJ_sm5 1 2 156 10197 1 201 */
|
||||
958, /* OBJ_sm2p256v1 1 2 156 10197 1 301 */
|
||||
1025, /* OBJ_id_sm9PublicKey 1 2 156 10197 1 302 */
|
||||
|
||||
@@ -4388,6 +4388,11 @@
|
||||
#define NID_sms4_xts 1029
|
||||
#define OBJ_sms4_xts OBJ_sm,104L,10L
|
||||
|
||||
#define SN_sms4_wrap "SMS4-WRAP"
|
||||
#define LN_sms4_wrap "sms4-wrap"
|
||||
#define NID_sms4_wrap 1033
|
||||
#define OBJ_sms4_wrap OBJ_sm,104L,11L
|
||||
|
||||
#define NID_sm7 1004
|
||||
#define OBJ_sm7 OBJ_sm,105L
|
||||
|
||||
|
||||
@@ -1030,3 +1030,4 @@ sms4_xts 1029
|
||||
sm1_cfb 1030
|
||||
sms4_cfb1 1031
|
||||
sms4_cfb8 1032
|
||||
sms4_wrap 1033
|
||||
|
||||
@@ -1409,7 +1409,7 @@ sm 104 7 : SMS4-CTR : sms4-ctr
|
||||
sm 104 8 : SMS4-GCM : sms4-gcm
|
||||
sm 104 9 : SMS4-CCM : sms4-ccm
|
||||
sm 104 10 : SMS4-XTS : sms4-xts
|
||||
|
||||
sm 104 11 : SMS4-WRAP : sms4-wrap
|
||||
|
||||
!Alias sm7 sm 105
|
||||
|
||||
|
||||
@@ -34,7 +34,7 @@ extern "C" {
|
||||
# ifdef OPENSSL_FIPS
|
||||
# define OPENSSL_VERSION_TEXT "OpenSSL 1.0.2d-fips 9 Jul 2015"
|
||||
# else
|
||||
# define OPENSSL_VERSION_TEXT "OpenSSL 1.0.2d 9 Jul 2015"
|
||||
# define OPENSSL_VERSION_TEXT "GmSSL 1.2.1"
|
||||
# endif
|
||||
# define OPENSSL_VERSION_PTEXT " part of " OPENSSL_VERSION_TEXT
|
||||
|
||||
|
||||
54
crypto/sm1/sm1test.c
Normal file
54
crypto/sm1/sm1test.c
Normal file
@@ -0,0 +1,54 @@
|
||||
#include <stdio.h>
|
||||
#include <openssl/evp.h>
|
||||
#include <openssl/engine.h>
|
||||
#include <openssl/assert.h>
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
const char *engine_id = "SKF";
|
||||
ENGINE *engine = NULL;
|
||||
const EVP_CIPHER *cipher;
|
||||
EVP_CIPHER_CTX ctx;
|
||||
unsigned char key[16];
|
||||
unsigned char iv[16];
|
||||
const char *msg1 = "hello world";
|
||||
const char *msg2 = "12345678";
|
||||
unsigned char buf[128];
|
||||
int len;
|
||||
|
||||
ENGINE_load_builtin_engines();
|
||||
engine = ENGINE_by_id(engine_id);
|
||||
|
||||
OPENSSL_assert(engine != NULL);
|
||||
|
||||
rv = ENGINE_init(engine);
|
||||
OPENSSL_assert(rv == 1);
|
||||
|
||||
cipher = ENGINE_get_cipher(engine, NID_sm1_cbc);
|
||||
|
||||
EVP_CIPHER_CTX_init(&ctx);
|
||||
|
||||
rv = RAND_bytes(key, (int)sizeof(key));
|
||||
rv = RAND_bytes(iv, (int)sizeof(iv));
|
||||
|
||||
rv = EVP_EncryptInit_ex(&ctx, cipher, engine, key, iv);
|
||||
|
||||
p = buf;
|
||||
rv = EVP_EncryptUpdate(&ctx, p, &len, (unsigned char *)msg1, (int)strlen(msg1));
|
||||
|
||||
p += len;
|
||||
rv = EVP_EncryptUpdate(&ctx, p, &len, (unsigned char *)msg2, (int)strlen(msg2));
|
||||
|
||||
p += len;
|
||||
rv = EVP_EncryptFinal_ex(&ctx, p, &len);
|
||||
|
||||
p += len;
|
||||
|
||||
|
||||
EVP_CIPHER_CTX_cleanup(&ctx);
|
||||
ENGINE_finish(engine);
|
||||
ENGINE_free(engine);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -35,3 +35,7 @@
|
||||
*/
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -69,12 +69,6 @@ struct sm2sign_method {
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -5,6 +5,393 @@
|
||||
#include <openssl/evp.h>
|
||||
#include <openssl/rand.h>
|
||||
|
||||
|
||||
/*
|
||||
|
||||
GFp192
|
||||
|
||||
p BDB6F4FE3E8B1D9E0DA8C0D46F4C318CEFE4AFE3B6B8551F
|
||||
a BB8E5E8FBC115E139FE6A814FE48AAA6F0ADA1AA5DF91985
|
||||
b 1854BEBDC31B21B7AEFC80AB0ECD10D5B1B3308E6DBF11C1
|
||||
x 4AD5F7048DE709AD51236DE65E4D4B482C836DC6E4106640
|
||||
y 02BB3A02D4AAADACAE24817A4CA3A1B014B5270432DB27D2
|
||||
n BDB6F4FE3E8B1D9E0DA8C0D40FC962195DFAE76F56564677
|
||||
h 1
|
||||
|
||||
GFp256
|
||||
|
||||
p 8542D69E4C044F18E8B92435BF6FF7DE457283915C45517D722EDB8B08F1DFC3
|
||||
a 787968B4FA32C3FD2417842E73BBFEFF2F3C848B6831D7E0EC65228B3937E498
|
||||
b 63E4C6D3B23B0C849CF84241484BFE48F61D59A5B16BA06E6E12D1DA27C5249A
|
||||
x 421DEBD61B62EAB6746434EBC3CC315E32220B3BADD50BDC4C4E6C147FEDD43D
|
||||
y 0680512BCBB42C07D47349D2153B70C4E5D7FDFCBFA36EA1A85841B9E46E09A2
|
||||
n 8542D69E4C044F18E8B92435BF6FF7DD297720630485628D5AE74EE7C32E79B7
|
||||
h 1
|
||||
|
||||
GF2m193 f(x) = x^193 + x^15 + 1
|
||||
|
||||
f 2000000000000000000000000000000000000000000008001
|
||||
a 0
|
||||
b 002FE22037B624DBEBC4C618E13FD998B1A18E1EE0D05C46FB
|
||||
x 00D78D47E85C93644071BC1C212CF994E4D21293AAD8060A84
|
||||
y 00615B9E98A31B7B2FDDEEECB76B5D875586293725F9D2FC0C
|
||||
n 80000000000000000000000043E9885C46BF45D8C5EBF3A1
|
||||
|
||||
GF2m257 f(x) = x^257 + x^12 + 1
|
||||
|
||||
f 20000000000000000000000000000000000000000000000000000000000001001
|
||||
a 0
|
||||
b 00E78BCD09746C202378A7E72B12BCE00266B9627ECB0B5A25367AD1AD4CC6242B
|
||||
x 00CDB9CA7F1E6B0441F658343F4B10297C0EF9B6491082400A62E7A7485735FADD
|
||||
y 013DE74DA65951C4D76DC89220D5F7777A611B1C38BAE260B175951DC8060C2B3E
|
||||
n 7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFBC972CF7E6B6F900945B3C6A0CF6161D
|
||||
|
||||
Signature on GFp256
|
||||
|
||||
M message digest
|
||||
d 128B2FA8BD433C6C068C8D803DFF79792A519A55171B1B650C23661D15897263
|
||||
Z F4A38489E32B45B6F876E3AC2168CA392362DC8F23459C1D1146FC3DBFB7BC9A
|
||||
e B524F552CD82B8B028476E005C377FB19A87E6FC682D48BB5D42E3D9B9EFFE76
|
||||
k 6CB28D99385C175C94F94E934817663FC176D925DD72B727260DBAAE1FB2F96F
|
||||
r 40F1EC59F793D9F49E09DCEF49130D4194F79FB1EED2CAA55BACDB49C4E755D1
|
||||
s 6FC6DAC32C5D5CF10C77DFB20F7C2EB667A457872FB09EC56327A67EC7DEEBE7
|
||||
|
||||
Signature on GF2m257
|
||||
|
||||
M message digest
|
||||
d 771EF3DBFF5F1CDC32B9C572930476191998B2BF7CB981D7F5B39202645F0931
|
||||
Z 26352AF82EC19F207BBC6F9474E11E90CE0F7DDACE03B27F801817E897A81FD5
|
||||
e AD673CBDA311417129A9EAA5F9AB1AA1633AD47718A84DFD46C17C6FA0AA3B12
|
||||
k 36CD79FC8E24B7357A8A7B4A46D454C397703D6498158C605399B341ADA186D6
|
||||
r 6D3FBA26EAB2A1054F5D198332E335817C8AC453ED26D3391CD4439D825BF25B
|
||||
s 3124C5688D95F0A10252A9BED033BEC84439DA384621B6D6FAD77F94B74A9556
|
||||
|
||||
Key Agreement on GFp256
|
||||
|
||||
A ALICE123@YAHOO.COM
|
||||
LA 0090
|
||||
B BILL456@YAHOO.COM
|
||||
LB 0088
|
||||
|
||||
dA 6FCBA2EF9AE0AB902BC3BDE3FF915D44BA4CC78F88E2F8E7F8996D3B8CCEEDEE
|
||||
dB 5E35D7D3F3C54DBAC72E61819E730B019A84208CA3A35E4C2E353DFCCB2A3B53
|
||||
ZA E4D1D0C3CA4C7F11BC8FF8CB3F4C02A78F108FA098E51A668487240F75E20F31
|
||||
ZB 6B4B6D0E276691BD4A11BF72F4FB501AE309FDACB72FA6CC336E6656119ABD67
|
||||
rA 83A2C9C8B96E5AF70BD480B472409A9A327257F1EBB73F5B073354B248668563
|
||||
x1 6CB5633816F4DD560B1DEC458310CBCC6856C09505324A6D23150C408F162BF0
|
||||
y1 0D6FCF62F1036C0A1B6DACCF57399223A65F7D7BF2D9637E5BBBEB857961BF1A
|
||||
rB 33FE21940342161C55619C4A0C060293D543C80AF19748CE176D83477DE71C80
|
||||
x2 1799B2A2C778295300D9A2325C686129B8F2B5337B3DCF4514E8BBC19D900EE5
|
||||
y2 54C9288C82733EFDF7808AE7F27D0E732F7C73A7D9AC98B7D8740A91D0DB3CF4
|
||||
x1' E856C09505324A6D23150C408F162BF0
|
||||
x2' B8F2B5337B3DCF4514E8BBC19D900EE5
|
||||
tB 2B2E11CBF03641FC3D939262FC0B652A70ACAA25B5369AD38B375C0265490C9F
|
||||
|
||||
|
||||
Encrypt on GFp256
|
||||
|
||||
M encryption standard
|
||||
d 1649AB77A00637BD5E2EFE283FBF353534AA7F7CB89463F208DDBC2920BB0DA0
|
||||
k 4C62EEFD6ECFC2B95B92FD6C3D9575148AFA17425546D49018E5388D49DD7B4F
|
||||
C1 0464D20D27D0632957F8028C1E024F6B02EDF23102A566C932AE8BD613A8E865FE58D225ECA784AE300A81A2D48281A828E1CEDF11C4219099840265375077BF78
|
||||
C2 650053A89B41C418B0C3AAD00D886C00286467
|
||||
C3 9C3D7360C30156FAB7C80A0276712DA9D8094A634B766D3A285E07480653426D
|
||||
|
||||
Encrypt on GF2m257
|
||||
|
||||
M encryption standard
|
||||
d 56A270D17377AA9A367CFA82E46FA5267713A9B91101D0777B07FCE018C757EB
|
||||
k 6D3B497153E3E92524E5C122682DBDC8705062E20B917A5F8FCDB8EE4C66663D
|
||||
C1 040083E628CF701EE3141E8873FE55936ADF24963F5DC9C6480566C80F8A1D8CC51B01524C647F0C0412DEFD468BDA3AE0E5A80FCC8F5C990FEE11602929232DCD9F36
|
||||
C2 FD55AC6213C2A8A040E4CAB5B26A9CFCDA7373FCDA7373
|
||||
C3 73A48625D3758FA37B3EAB80E9CFCABA665E3199EA15A1FA8189D96F579125E4
|
||||
|
||||
*/
|
||||
|
||||
RAND_METHOD fake_rand;
|
||||
const RAND_METHOD *old_rand;
|
||||
|
||||
int fbytes(unsigned char *buf, int num)
|
||||
{
|
||||
int ret;
|
||||
BIGNUM *tmp = NULL;
|
||||
|
||||
if (fbytes_counter >= 8)
|
||||
return 0;
|
||||
|
||||
if (!(tmp = BN_new())) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!BN_hex2bn(&tmp, numbers[fbytes_counter])) {
|
||||
BN_free(tmp);
|
||||
return 0;
|
||||
}
|
||||
|
||||
fbytes_counter++;
|
||||
|
||||
|
||||
if (num != BN_num_bytes(tmp) || !BN_bn2bin(tmp, buf))
|
||||
ret = 0;
|
||||
else
|
||||
ret = 1;
|
||||
|
||||
if (tmp)
|
||||
BN_free(tmp);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int change_rand(void)
|
||||
{
|
||||
if (!(old_rand = RAND_get_rand_method())) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
fake_rand.seed = old_rand->seed;
|
||||
fake_rand.cleanup = old_rand->cleanup;
|
||||
fake_rand.add = old_rand->add;
|
||||
fake_rand.status = old_rand->status;
|
||||
fake_rand.bytes = fbytes;
|
||||
fake_rand.pseudorand = old_rand->bytes;
|
||||
|
||||
if (!RAND_set_rand_method(&fake_rand)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int restore_rand(void)
|
||||
{
|
||||
if (!RAND_set_rand_method(rand))
|
||||
return 0;
|
||||
else
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
EC_GROUP *new_ec_group(int is_prime_field,
|
||||
const char *p_hex, const char *a_hex, const char *b_hex,
|
||||
const char *x_hex, const char *y_hex, const char *n_hex, const char *h_hex)
|
||||
{
|
||||
int e = 1;
|
||||
EC_GROUP *ec_group = NULL;
|
||||
BN_CTX *ctx = NULL;
|
||||
BIGNUM *p = NULL;
|
||||
BIGNUM *a = NULL;
|
||||
BIGNUM *b = NULL;
|
||||
BIGNUM *x = NULL;
|
||||
BIGNUM *y = NULL;
|
||||
BIGNUM *n = NULL;
|
||||
BIGNUM *h = NULL;
|
||||
EC_POINT *G = NULL;
|
||||
point_conversion_form_t form = POINT_CONVERSION_UNCOMPRESSED;
|
||||
int flag = 0;
|
||||
|
||||
if (!(ctx = BN_CTX_new())) {
|
||||
ERR_print_errors_fp(stderr);
|
||||
goto err;
|
||||
}
|
||||
|
||||
// FIXME
|
||||
if (!(ec_group = EC_GROUP_new(EC_GFp_mont_method()))) {
|
||||
ERR_print_errors_fp(stderr);
|
||||
goto err;
|
||||
}
|
||||
|
||||
if (!BN_hex2bn(&p, p_hex) ||
|
||||
!BN_hex2bn(&a, a_hex) ||
|
||||
!BN_hex2bn(&b, b_hex) ||
|
||||
!BN_hex2bn(&x, x_hex) ||
|
||||
!BN_hex2bn(&y, y_hex) ||
|
||||
!BN_hex2bn(&n, n_hex) ||
|
||||
!BN_hex2bn(&h, h_hex)) {
|
||||
ERR_print_errors_fp(stderr);
|
||||
goto err;
|
||||
}
|
||||
|
||||
if (is_prime_curve) {
|
||||
if (!EC_GROUP_set_curve_GFp(ec_group, p, a, b, ctx)) {
|
||||
ERR_print_errors_fp(stderr);
|
||||
goto err;
|
||||
}
|
||||
if (!EC_POINT_set_affine_coordinates_GFp(ec_group, G, x, y, ctx)) {
|
||||
ERR_print_errors_fp(stderr);
|
||||
goto err;
|
||||
}
|
||||
|
||||
} else {
|
||||
if (!EC_GROUP_set_curve_GF2m(ec_group, p, a, b, ctx)) {
|
||||
ERR_print_errors_fp(stderr);
|
||||
goto err;
|
||||
}
|
||||
if (!EC_POINT_set_affine_coordinates_GF2m(ec_group, G, x, y, ctx)) {
|
||||
ERR_print_errors_fp(stderr);
|
||||
goto err;
|
||||
}
|
||||
}
|
||||
|
||||
if (!(G = EC_POINT_new(ec_group))) {
|
||||
ERR_print_errors_fp(stderr);
|
||||
goto err;
|
||||
}
|
||||
|
||||
if (!EC_GROUP_set_generator(ec_group, G, n, h)) {
|
||||
ERR_print_errors_fp(stderr);
|
||||
goto err;
|
||||
}
|
||||
|
||||
EC_GROUP_set_asn1_flag(ec_group, flag);
|
||||
EC_GROUP_set_point_conversion_form(ec_group, form);
|
||||
|
||||
e = 0;
|
||||
err:
|
||||
if (ctx) BN_CTX_free(ctx);
|
||||
if (p) BN_free(p);
|
||||
if (a) BN_free(a);
|
||||
if (b) BN_free(b);
|
||||
if (x) BN_free(x);
|
||||
if (y) BN_free(y);
|
||||
if (n) BN_free(n);
|
||||
if (h) BN_free(h);
|
||||
if (G) EC_POINT_free(G);
|
||||
if (e && ec_group) {
|
||||
EC_GROUP_free(ec_group);
|
||||
ec_group = NULL;
|
||||
}
|
||||
return ec_group;
|
||||
}
|
||||
|
||||
EC_KEY *new_ec_key(const EC_GROUP *group, const char *sk, const char *id,
|
||||
const char *xP, const char *yP)
|
||||
{
|
||||
EC_KEY *ec_key = NULL;
|
||||
BIGNUM *x = NULL;
|
||||
BIGNUM *y = NULL;
|
||||
|
||||
|
||||
if (sk) {
|
||||
if (!BN_hex2bn(&d, sk)) {
|
||||
ERR_print_errors_fp(stderr);
|
||||
goto err;
|
||||
}
|
||||
}
|
||||
|
||||
if (id) {
|
||||
if (!SM2_set_id(ec_key, id)) {
|
||||
ERR_print_errors_fp(stderr);
|
||||
goto err;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (xP && yP) {
|
||||
if (!BN_hex2bn(&x, xP)) {
|
||||
ERR_print_errors_fp(stderr);
|
||||
goto err;
|
||||
}
|
||||
if (!BN_hex2bn(&y, yP)) {
|
||||
}
|
||||
|
||||
if (!EC_KEY_set_public_key()) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
err:
|
||||
|
||||
return ec_key;
|
||||
}
|
||||
|
||||
|
||||
static int test_sm2_id(void)
|
||||
{
|
||||
int ret = 0;
|
||||
EC_GROUP *group = NULL;
|
||||
EC_KEY *ec_key = NULL;
|
||||
BIGNUM *bn = NULL;
|
||||
const char *id[] = {
|
||||
"ALICE123@YAHOO.COM",
|
||||
"ALICE123@YAHOO.COM",
|
||||
"ALICE123@YAHOO.COM",
|
||||
"BILL456@YAHOO.COM"};
|
||||
const char *sk[] = {
|
||||
"128B2FA8BD433C6C068C8D803DFF79792A519A55171B1B650C23661D15897263",
|
||||
"771EF3DBFF5F1CDC32B9C572930476191998B2BF7CB981D7F5B39202645F0931",
|
||||
"6FCBA2EF9AE0AB902BC3BDE3FF915D44BA4CC78F88E2F8E7F8996D3B8CCEEDEE",
|
||||
"5E35D7D3F3C54DBAC72E61819E730B019A84208CA3A35E4C2E353DFCCB2A3B53"};
|
||||
const char *Z[] = {
|
||||
"F4A38489E32B45B6F876E3AC2168CA392362DC8F23459C1D1146FC3DBFB7BC9A",
|
||||
"26352AF82EC19F207BBC6F9474E11E90CE0F7DDACE03B27F801817E897A81FD5",
|
||||
"E4D1D0C3CA4C7F11BC8FF8CB3F4C02A78F108FA098E51A668487240F75E20F31",
|
||||
"6B4B6D0E276691BD4A11BF72F4FB501AE309FDACB72FA6CC336E6656119ABD67"};
|
||||
unsigned char dgst[EVP_MAX_MD_SIZE];
|
||||
unsigned char buf[sizeof(dgst) * 2];
|
||||
unsigned int len;
|
||||
int i, j;
|
||||
|
||||
|
||||
if (!(group = new_GFp256test())) {
|
||||
goto err;
|
||||
}
|
||||
|
||||
if (!(ec_key = EC_KEY_new())) {
|
||||
ERR_print_errors_fp(stderr);
|
||||
goto err;
|
||||
}
|
||||
|
||||
if (!EC_KEY_set_group(ec_key, group)) {
|
||||
ERR_print_errors_fp(stderr);
|
||||
goto err;
|
||||
}
|
||||
|
||||
if (!(bn = BN_new())) {
|
||||
ERR_print_errors_fp(stderr);
|
||||
goto err;
|
||||
}
|
||||
|
||||
for (i = 0; i < sizeof(id)/sizeof(id[0]); i++) {
|
||||
|
||||
if (!SM2_set_id(ec_key, id[i])) {
|
||||
ERR_print_errors_fp(stderr);
|
||||
goto err;
|
||||
}
|
||||
|
||||
if (!BN_hex2bn(&bn, sk[i])) {
|
||||
ERR_print_errors_fp(stderr);
|
||||
goto err;
|
||||
}
|
||||
|
||||
if (!EC_KEY_set_private_key(ec_key, bn)) {
|
||||
ERR_print_errors_fp(stderr);
|
||||
goto err;
|
||||
}
|
||||
|
||||
if (!SM2_compute_id_digest(dgst, &dgstlen, EVP_sm3(), ec_key)) {
|
||||
ERR_print_errors_fp(stderr);
|
||||
goto err;
|
||||
}
|
||||
|
||||
for (j = 0; j < SM3_DIGEST_LENGTH; j++) {
|
||||
sprintf(&(buf[j * 2]), "%02X", dgst[j]);
|
||||
}
|
||||
|
||||
if (memcpy(Z[i], buf, strlen(Z[i])) != 0) {
|
||||
goto err;
|
||||
}
|
||||
}
|
||||
|
||||
ret = 1;
|
||||
|
||||
err:
|
||||
EC_GROUP_free(group);
|
||||
EC_KEY_free(ec_key);
|
||||
BN_free(bn);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static void test_sm2_sign(void)
|
||||
{
|
||||
int rv;
|
||||
@@ -19,7 +406,7 @@ static void test_sm2_sign(void)
|
||||
rv = EC_KEY_generate_key(ec_key);
|
||||
OPENSSL_assert(rv == 1);
|
||||
|
||||
RAND_bytes(dgst, sizeof(dgst));
|
||||
RAND_pseudo_bytes(dgst, sizeof(dgst));
|
||||
|
||||
sig = SM2_do_sign(dgst, (int)sizeof(dgst), ec_key);
|
||||
OPENSSL_assert(sig);
|
||||
@@ -33,10 +420,247 @@ static void test_sm2_sign(void)
|
||||
|
||||
EC_KEY_free(ec_key);
|
||||
ECDSA_SIG_free(sig);
|
||||
|
||||
printf("%s() success\n", __FUNCTION__);
|
||||
}
|
||||
|
||||
char *msg = "message digest";
|
||||
char *id = "ALICE123@YAHOO.COM";
|
||||
char *sk = "128B2FA8BD433C6C068C8D803DFF79792A519A55171B1B650C23661D15897263";
|
||||
char *e = "B524F552CD82B8B028476E005C377FB19A87E6FC682D48BB5D42E3D9B9EFFE76";
|
||||
char *k = "6CB28D99385C175C94F94E934817663FC176D925DD72B727260DBAAE1FB2F96F";
|
||||
char *r = "40F1EC59F793D9F49E09DCEF49130D4194F79FB1EED2CAA55BACDB49C4E755D1";
|
||||
char *s = "6FC6DAC32C5D5CF10C77DFB20F7C2EB667A457872FB09EC56327A67EC7DEEBE7";
|
||||
|
||||
|
||||
int test_sm2_sign(const EC_GROUP *group, const char *msg, const char *id,
|
||||
const char *sk, const char *e, const char *s)
|
||||
{
|
||||
int ret = 0;
|
||||
EC_GROUP *group = NULL;
|
||||
unsigned char idgst[32];
|
||||
|
||||
EVP_MD_CTX md_ctx;
|
||||
|
||||
if (!(group = new_GFp256test())) {
|
||||
goto err;
|
||||
}
|
||||
|
||||
if (!(ec_key = EC_KEY_new())) {
|
||||
ERR_print_errors_fp(stderr);
|
||||
goto err;
|
||||
}
|
||||
|
||||
if (!EC_KEY_set_group(ec_key, group)) {
|
||||
ERR_print_errors_fp(stderr);
|
||||
goto err;
|
||||
}
|
||||
|
||||
if (!BN_hex2bn(&bn, sk)) {
|
||||
ERR_print_errors_fp(stderr);
|
||||
goto err;
|
||||
}
|
||||
|
||||
if (!EC_KEY_set_private_key(ec_key, bn)) {
|
||||
ERR_print_errors_fp(stderr);
|
||||
goto err;
|
||||
}
|
||||
|
||||
if (!SM2_set_id(ec_key, id)) {
|
||||
ERR_print_errors_fp(stderr);
|
||||
goto err;
|
||||
}
|
||||
|
||||
|
||||
|
||||
EVP_MD_CTX_init(&md_ctx);
|
||||
|
||||
|
||||
EVP_DigestInit(&md_ctx, iddgst, sizeof(iddgst));
|
||||
|
||||
EVP_DigestInit(&md_ctx, msg, strlen(msg));
|
||||
|
||||
EVP_DigestFinal(&md_ctx, msgdgst, &len);
|
||||
|
||||
|
||||
hexequbin(Z, msgdgst, len);
|
||||
|
||||
|
||||
sig = SM2_do_sign();
|
||||
|
||||
|
||||
hex = BN_bin2hex(sig->r);
|
||||
|
||||
if (strcmp(r, hex)) {
|
||||
}
|
||||
|
||||
hex = BN_bin2hex(sig->s);
|
||||
|
||||
if (strcmp(s, hex)) {
|
||||
}
|
||||
|
||||
|
||||
SM2_do_verify();
|
||||
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int test_sm2_sign_GF2m257(void)
|
||||
{
|
||||
int ret = 0;
|
||||
char *msg = "message digest";
|
||||
char *d = "771EF3DBFF5F1CDC32B9C572930476191998B2BF7CB981D7F5B39202645F0931";
|
||||
char *Z = "26352AF82EC19F207BBC6F9474E11E90CE0F7DDACE03B27F801817E897A81FD5";
|
||||
char *e = "AD673CBDA311417129A9EAA5F9AB1AA1633AD47718A84DFD46C17C6FA0AA3B12";
|
||||
char *k = "36CD79FC8E24B7357A8A7B4A46D454C397703D6498158C605399B341ADA186D6";
|
||||
char *r = "6D3FBA26EAB2A1054F5D198332E335817C8AC453ED26D3391CD4439D825BF25B";
|
||||
char *s = "3124C5688D95F0A10252A9BED033BEC84439DA384621B6D6FAD77F94B74A9556";
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int hexequbin(const char *hex, const unsigned char *bin, size_t binlen)
|
||||
{
|
||||
char *buf = NULL;
|
||||
if (binlen * 2 != strlen(hex)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
buf = OPENSSL_malloc(binlen * 2);
|
||||
|
||||
for (i = 0; i < binlen; i++) {
|
||||
sprintf(buf + i*2, "%02X", bin[i]);
|
||||
}
|
||||
|
||||
if (memcmp(hex, buf, binlen * 2) != 0) {
|
||||
return 0;
|
||||
} else {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
EC_KEY *new_ec_key(const EC_GROUP *group, const char *hex)
|
||||
{
|
||||
}
|
||||
|
||||
int test_sm2_enc(const EC_GROUP *group, const char *msg, const char *sk,
|
||||
const char *c1, const char *c2, const char *c3)
|
||||
{
|
||||
int ret = 0;
|
||||
EC_KEY *ec_key = NULL;
|
||||
SM2_CIPHERTEXT_VALUE *cv = NULL;
|
||||
|
||||
|
||||
cv = SM2_do_encrypt(EVP_sm3(), EVP_sm3(), (unsigned char *)msg, (size_t)strlen(msg), ec_key);
|
||||
|
||||
|
||||
EC_POINT_point2oct(cv->ephem_point);
|
||||
|
||||
|
||||
if (!hexequbin(C2, cv->ciphertext, cv->ciphertext_size)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!hexequbin(C3, cv->mactag, cv->mactag_size)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
int test_sm2()
|
||||
{
|
||||
EC_GROUP *sm2p192test = NULL;
|
||||
EC_GROUP *sm2p256test = NULL;
|
||||
EC_GROUP *sm2b193test = NULL;
|
||||
EC_GROUP *sm2b257test = NULL;
|
||||
|
||||
sm2p192test = new_ec_group(1,
|
||||
"BDB6F4FE3E8B1D9E0DA8C0D46F4C318CEFE4AFE3B6B8551F",
|
||||
"BB8E5E8FBC115E139FE6A814FE48AAA6F0ADA1AA5DF91985",
|
||||
"1854BEBDC31B21B7AEFC80AB0ECD10D5B1B3308E6DBF11C1",
|
||||
"4AD5F7048DE709AD51236DE65E4D4B482C836DC6E4106640",
|
||||
"02BB3A02D4AAADACAE24817A4CA3A1B014B5270432DB27D2",
|
||||
"BDB6F4FE3E8B1D9E0DA8C0D40FC962195DFAE76F56564677",
|
||||
"1");
|
||||
|
||||
sm2p256test = new_ec_group(1,
|
||||
"8542D69E4C044F18E8B92435BF6FF7DE457283915C45517D722EDB8B08F1DFC3",
|
||||
"787968B4FA32C3FD2417842E73BBFEFF2F3C848B6831D7E0EC65228B3937E498",
|
||||
"63E4C6D3B23B0C849CF84241484BFE48F61D59A5B16BA06E6E12D1DA27C5249A",
|
||||
"421DEBD61B62EAB6746434EBC3CC315E32220B3BADD50BDC4C4E6C147FEDD43D",
|
||||
"0680512BCBB42C07D47349D2153B70C4E5D7FDFCBFA36EA1A85841B9E46E09A2",
|
||||
"8542D69E4C044F18E8B92435BF6FF7DD297720630485628D5AE74EE7C32E79B7",
|
||||
"1");
|
||||
|
||||
sm2b193test = new_ec_group(0,
|
||||
"2000000000000000000000000000000000000000000008001",
|
||||
"0",
|
||||
"002FE22037B624DBEBC4C618E13FD998B1A18E1EE0D05C46FB",
|
||||
"00D78D47E85C93644071BC1C212CF994E4D21293AAD8060A84",
|
||||
"00615B9E98A31B7B2FDDEEECB76B5D875586293725F9D2FC0C",
|
||||
"80000000000000000000000043E9885C46BF45D8C5EBF3A1",
|
||||
"1");
|
||||
|
||||
sm2b257test = new_ec_group(0,
|
||||
"20000000000000000000000000000000000000000000000000000000000001001",
|
||||
"0",
|
||||
"00E78BCD09746C202378A7E72B12BCE00266B9627ECB0B5A25367AD1AD4CC6242B",
|
||||
"00CDB9CA7F1E6B0441F658343F4B10297C0EF9B6491082400A62E7A7485735FADD",
|
||||
"013DE74DA65951C4D76DC89220D5F7777A611B1C38BAE260B175951DC8060C2B3E",
|
||||
"7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFBC972CF7E6B6F900945B3C6A0CF6161D",
|
||||
"1");
|
||||
|
||||
|
||||
test_sm2_sign(
|
||||
sm2p256test,
|
||||
"message digest",
|
||||
"128B2FA8BD433C6C068C8D803DFF79792A519A55171B1B650C23661D15897263",
|
||||
"F4A38489E32B45B6F876E3AC2168CA392362DC8F23459C1D1146FC3DBFB7BC9A",
|
||||
"B524F552CD82B8B028476E005C377FB19A87E6FC682D48BB5D42E3D9B9EFFE76",
|
||||
"6CB28D99385C175C94F94E934817663FC176D925DD72B727260DBAAE1FB2F96F",
|
||||
"40F1EC59F793D9F49E09DCEF49130D4194F79FB1EED2CAA55BACDB49C4E755D1",
|
||||
"6FC6DAC32C5D5CF10C77DFB20F7C2EB667A457872FB09EC56327A67EC7DEEBE7");
|
||||
|
||||
test_sm2_sign(
|
||||
sm2b257test,
|
||||
"message digest",
|
||||
"771EF3DBFF5F1CDC32B9C572930476191998B2BF7CB981D7F5B39202645F0931",
|
||||
"26352AF82EC19F207BBC6F9474E11E90CE0F7DDACE03B27F801817E897A81FD5",
|
||||
"AD673CBDA311417129A9EAA5F9AB1AA1633AD47718A84DFD46C17C6FA0AA3B12",
|
||||
"36CD79FC8E24B7357A8A7B4A46D454C397703D6498158C605399B341ADA186D6",
|
||||
"6D3FBA26EAB2A1054F5D198332E335817C8AC453ED26D3391CD4439D825BF25B",
|
||||
"3124C5688D95F0A10252A9BED033BEC84439DA384621B6D6FAD77F94B74A9556");
|
||||
|
||||
test_sm2_enc(
|
||||
sm2p256test,
|
||||
"encryption standard",
|
||||
"1649AB77A00637BD5E2EFE283FBF353534AA7F7CB89463F208DDBC2920BB0DA0",
|
||||
"4C62EEFD6ECFC2B95B92FD6C3D9575148AFA17425546D49018E5388D49DD7B4F",
|
||||
"04"
|
||||
"64D20D27D0632957F8028C1E024F6B02EDF23102A566C932AE8BD613A8E865FE"
|
||||
"58D225ECA784AE300A81A2D48281A828E1CEDF11C4219099840265375077BF78",
|
||||
"650053A89B41C418B0C3AAD00D886C00286467",
|
||||
"9C3D7360C30156FAB7C80A0276712DA9D8094A634B766D3A285E07480653426D");
|
||||
|
||||
test_sm2_enc(
|
||||
sm2b257test,
|
||||
"encryption standard",
|
||||
"56A270D17377AA9A367CFA82E46FA5267713A9B91101D0777B07FCE018C757EB",
|
||||
"6D3B497153E3E92524E5C122682DBDC8705062E20B917A5F8FCDB8EE4C66663D",
|
||||
"04"
|
||||
"0083E628CF701EE3141E8873FE55936ADF24963F5DC9C6480566C80F8A1D8CC51B"
|
||||
"01524C647F0C0412DEFD468BDA3AE0E5A80FCC8F5C990FEE11602929232DCD9F36",
|
||||
"FD55AC6213C2A8A040E4CAB5B26A9CFCDA7373FCDA7373",
|
||||
"73A48625D3758FA37B3EAB80E9CFCABA665E3199EA15A1FA8189D96F579125E4");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static void test_sm2_enc(void)
|
||||
{
|
||||
int rv;
|
||||
@@ -250,6 +874,7 @@ int sm2_test_evp_pkey_encrypt(void)
|
||||
ec_key = EC_KEY_new_by_curve_name(NID_sm2p256v1);
|
||||
pkey = EVP_PKEY_new();
|
||||
EC_KEY_generate_key(ec_key);
|
||||
|
||||
EVP_PKEY_set1_SM2(pkey, ec_key);
|
||||
|
||||
ctx = EVP_PKEY_CTX_new(pkey, NULL);
|
||||
@@ -376,6 +1001,7 @@ int test_sm2_pkey_seal(void)
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
test_sm2_sign();
|
||||
|
||||
@@ -1,65 +1,122 @@
|
||||
//gcc -o test sm3_test.c -L/usr/local/ssl/lib -I/usr/local/ssl/include -lcrypto
|
||||
/* crypto/sm3/sm3test.c */
|
||||
/* ====================================================================
|
||||
* Copyright (c) 2014 - 2015 The GmSSL Project. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. All advertising materials mentioning features or use of this
|
||||
* software must display the following acknowledgment:
|
||||
* "This product includes software developed by the GmSSL Project.
|
||||
* (http://gmssl.org/)"
|
||||
*
|
||||
* 4. The name "GmSSL Project" must not be used to endorse or promote
|
||||
* products derived from this software without prior written
|
||||
* permission. For written permission, please contact
|
||||
* guanzhi1980@gmail.com.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "GmSSL"
|
||||
* nor may "GmSSL" appear in their names without prior written
|
||||
* permission of the GmSSL Project.
|
||||
*
|
||||
* 6. Redistributions of any form whatsoever must retain the following
|
||||
* acknowledgment:
|
||||
* "This product includes software developed by the GmSSL Project
|
||||
* (http://gmssl.org/)"
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY
|
||||
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
||||
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
||||
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
* ====================================================================
|
||||
*
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <openssl/evp.h>
|
||||
#include <openssl/sm3.h>
|
||||
static size_t hash[8] = {0};
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
void out_hex(size_t *list1)
|
||||
{
|
||||
size_t i = 0;
|
||||
for (i = 0; i < 8; i++)
|
||||
{
|
||||
printf("%08x ", list1[i]);
|
||||
}
|
||||
printf("\r\n");
|
||||
}
|
||||
#include "../e_os.h"
|
||||
|
||||
#ifdef OPENSSL_NO_SM3
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
EVP_MD_CTX mdctx;
|
||||
const EVP_MD *md;
|
||||
char mess1[] = "abc";
|
||||
char mess2[] = "abc";
|
||||
unsigned char md_value[EVP_MAX_MD_SIZE];
|
||||
int md_len, i;
|
||||
//使EVP_Digest系列函数支持所有有效的信息摘要算法
|
||||
OpenSSL_add_all_digests();
|
||||
|
||||
argv[1] = "sm3";
|
||||
|
||||
if(!argv[1]) {
|
||||
printf("Usage: mdtest digestname\n");
|
||||
exit(1);
|
||||
}
|
||||
//根据输入的信息摘要函数的名字得到相应的EVP_MD算法结构
|
||||
md = EVP_get_digestbyname(argv[1]);
|
||||
//md = EVP_sm3();
|
||||
|
||||
if(!md) {
|
||||
printf("Unknown message digest %s\n", argv[1]);
|
||||
exit(1);
|
||||
}
|
||||
//初始化信息摘要结构mdctx,这在调用EVP_DigestInit_ex函数的时候是必须的。
|
||||
EVP_MD_CTX_init(&mdctx);
|
||||
//使用md的算法结构设置mdctx结构,impl为NULL,即使用缺省实现的算法(openssl本身提供的信息摘要算法)
|
||||
EVP_DigestInit_ex(&mdctx, md, NULL);
|
||||
//开始真正进行信息摘要运算,可以多次调用该函数,处理更多的数据,这里只调用了两次
|
||||
EVP_DigestUpdate(&mdctx, mess1, strlen(mess1));
|
||||
//EVP_DigestUpdate(&mdctx, mess2, strlen(mess2));
|
||||
//完成信息摘要计算过程,将完成的摘要信息存储在md_value里面,长度信息存储在md_len里面
|
||||
EVP_DigestFinal_ex(&mdctx, md_value, &md_len);
|
||||
//使用该函数释放mdctx占用的资源,如果使用_ex系列函数,这是必须调用的。
|
||||
EVP_MD_CTX_cleanup(&mdctx);
|
||||
|
||||
printf("Digest is: ");
|
||||
for(i = 0; i < md_len; i++) printf("%02x", md_value[i]);
|
||||
printf("\n");
|
||||
|
||||
//SM3("abc",3,hash);
|
||||
//out_hex(hash);
|
||||
|
||||
system("pause");
|
||||
return 0;
|
||||
printf("No SM3 support\n");
|
||||
return (0);
|
||||
}
|
||||
#else
|
||||
# include <openssl/evp.h>
|
||||
# include <openssl/sm3.h>
|
||||
|
||||
static char *test[] = {
|
||||
//"",
|
||||
"abc",
|
||||
"abcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcd",
|
||||
NULL,
|
||||
};
|
||||
|
||||
static char *ret[] = {
|
||||
"66c7f0f462eeedd9d1f2d46bdc10e4e24167c4875cf2f7a2297da02b8f4ba8e0",
|
||||
"debe9ff92275b8a138604889c18e5a4d6fdb70e5387e5765293dcba39c0c5732",
|
||||
};
|
||||
|
||||
static char *pt(unsigned char *md);
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int i, err = 0;
|
||||
char **P, **R;
|
||||
char *p;
|
||||
unsigned char md[SM3_DIGEST_LENGTH];
|
||||
|
||||
P = test;
|
||||
R = ret;
|
||||
i = 1;
|
||||
while (*P != NULL) {
|
||||
EVP_Digest(&(P[0][0]), strlen((char *)*P), md, NULL, EVP_sm3(), NULL);
|
||||
p = pt(md);
|
||||
if (strcmp(p, (char *)*R) != 0) {
|
||||
printf("error calculating SM3 on '%s'\n", *P);
|
||||
printf("got %s instead of %s\n", p, *R);
|
||||
err++;
|
||||
} else
|
||||
printf("test %d ok\n", i);
|
||||
i++;
|
||||
R++;
|
||||
P++;
|
||||
}
|
||||
|
||||
# ifdef OPENSSL_SYS_NETWARE
|
||||
if (err)
|
||||
printf("ERROR: %d\n", err);
|
||||
# endif
|
||||
EXIT(err);
|
||||
return (0);
|
||||
}
|
||||
|
||||
static char *pt(unsigned char *md)
|
||||
{
|
||||
int i;
|
||||
static char buf[80]; //FIXME: 80?
|
||||
|
||||
for (i = 0; i < SM3_DIGEST_LENGTH; i++)
|
||||
sprintf(&(buf[i * 2]), "%02x", md[i]);
|
||||
return (buf);
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#include "../byteorder.h"
|
||||
#include <assert.h>
|
||||
#include "../modes/modes_lcl.h"
|
||||
#include "zuc.h"
|
||||
|
||||
|
||||
@@ -209,26 +210,39 @@ void ZUC_set_key(ZUC_KEY *key, const unsigned char *k, const unsigned char *iv)
|
||||
void ZUC_encrypt(ZUC_KEY *key, size_t inlen, const unsigned char *in, unsigned char *out)
|
||||
{
|
||||
uint32_t word;
|
||||
int n = key->buf_index;
|
||||
|
||||
/*
|
||||
while (key->buf_index < 4 && inlen > 0) {
|
||||
*out++ = *in++ ^ key->buf[key->buf_index++];
|
||||
assert(n < 4);
|
||||
|
||||
while (n && inlen) {
|
||||
*(out++) = *(in++) ^ key->buf[n];
|
||||
n = (n + 1) % 4;
|
||||
inlen--;
|
||||
}
|
||||
|
||||
|
||||
while (inlen >= 4) {
|
||||
BitReorganization(key);
|
||||
word = le32_to_cpu((uint32_t *)in);
|
||||
word = GETU32(in);
|
||||
word ^= F(key) ^ key->BRC_X3;
|
||||
*((uint32_t *)out) = cpu_to_le32(word);
|
||||
PUTU32(out, word);
|
||||
LFSRWithWorkMode(key);
|
||||
inlen -= 4;
|
||||
in += 4;
|
||||
out += 4;
|
||||
}
|
||||
|
||||
while (inlen-- > 0) {
|
||||
*out++ = *in++ ^ *buf++;
|
||||
key->buflen--;
|
||||
if (inlen) {
|
||||
BitReorganization(key);
|
||||
word = F(key) ^ key->BRC_X3;
|
||||
LFSRWithWorkMode(key);
|
||||
PUTU32(key->buf, word);
|
||||
while (inlen-- > 0) {
|
||||
out[n] = in[n] ^ key->buf[n];
|
||||
n++;
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
key->buf_index = n;
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user