Skip to content

Commit

Permalink
Update sm2 and zuc
Browse files Browse the repository at this point in the history
  • Loading branch information
guanzhi committed Jan 12, 2019
1 parent 3dfae96 commit 4bab277
Show file tree
Hide file tree
Showing 14 changed files with 8,517 additions and 5,415 deletions.
1 change: 0 additions & 1 deletion .gitignore
Expand Up @@ -209,7 +209,6 @@ apps/gmca/.ca
# engines
/engines/e_skf*
/engines/e_sdf*
/engines/e_gmi*
/engines/sdf
/engines/skf

Expand Down
11 changes: 9 additions & 2 deletions README.md
Expand Up @@ -125,8 +125,8 @@ $ gmssl sms4 -d -in README.sms4
ZUC encryption and decryption:

```sh
$ gmssl zuc -in README.md -out README.sms4
$ gmssl zuc -d -in README.sms4
$ gmssl zuc -in README.md -out README.zuc
$ gmssl zuc -d -in README.zuc
```

SM2 private key generation:
Expand Down Expand Up @@ -157,6 +157,13 @@ $ echo "Top Secret" | gmssl sm2utl -encrypt -pubin -inkey ekey.pem -out cipherte
$ gmssl sm2utl -decrypt -inkey dkey.pem -in ciphertext.sm2
```

Identity-based encryption with SM9

```
$ echo "Message" | gmssl pkeyutl -encrypt -pubin -inkey params.pem -pkeyopt id:Alice -out ciphertext.der
$ gmssl pkeyutl -decrypt -inkey sm9key.pem -in ciphertext.der
```

Self-signed SM2 certificate generation:

```sh
Expand Down
3 changes: 3 additions & 0 deletions apps/progs.h
Expand Up @@ -306,6 +306,9 @@ static FUNCTION functions[] = {
#ifndef OPENSSL_NO_SMS4
{ FT_cipher, "sms4-cfb", enc_main, enc_options },
#endif
#ifndef OPENSSL_NO_ZUC
{ FT_cipher, "zuc", enc_main, enc_options },
#endif
#ifndef OPENSSL_NO_AES
{ FT_cipher, "aes-128-cbc", enc_main, enc_options },
#endif
Expand Down
2 changes: 1 addition & 1 deletion apps/progs.pl
Expand Up @@ -118,7 +118,7 @@
cast5 => "cast",
);
foreach my $cmd (
"sms4", "sms4-cbc", "sms4-ecb", "sms4-ofb", "sms4-cfb",
"sms4", "sms4-cbc", "sms4-ecb", "sms4-ofb", "sms4-cfb", "zuc",
"aes-128-cbc", "aes-128-ecb",
"aes-192-cbc", "aes-192-ecb",
"aes-256-cbc", "aes-256-ecb",
Expand Down
43 changes: 39 additions & 4 deletions apps/speed.c
Expand Up @@ -122,6 +122,9 @@
#ifndef OPENSSL_NO_SMS4
# include <openssl/sms4.h>
#endif
#ifndef OPENSSL_NO_ZUC
# include <openssl/zuc.h>
#endif
#ifndef OPENSSL_NO_SM9
# include <openssl/sm9.h>
#endif
Expand All @@ -145,7 +148,7 @@
#define BUFSIZE (1024*16+1)
#define MAX_MISALIGNMENT 63

#define ALGOR_NUM 32
#define ALGOR_NUM 33
#define SIZE_NUM 6
#define PRIME_NUM 3
#define RSA_NUM 7
Expand Down Expand Up @@ -294,7 +297,7 @@ static const char *names[ALGOR_NUM] = {
"camellia-128 cbc", "camellia-192 cbc", "camellia-256 cbc",
"evp", "sha256", "sha512", "whirlpool",
"aes-128 ige", "aes-192 ige", "aes-256 ige", "ghash",
"sm3", "sms4 cbc"
"sm3", "sms4 cbc", "zuc"
};

static double results[ALGOR_NUM][SIZE_NUM];
Expand Down Expand Up @@ -479,6 +482,7 @@ OPTIONS speed_options[] = {
#define D_GHASH 29
#define D_SM3 30
#define D_CBC_SMS4 31
#define D_ZUC 32
static OPT_PAIR doit_choices[] = {
#ifndef OPENSSL_NO_MD2
{"md2", D_MD2},
Expand Down Expand Up @@ -554,6 +558,9 @@ static OPT_PAIR doit_choices[] = {
#ifndef OPENSSL_NO_SMS4
{"sms4-cbc", D_CBC_SMS4},
{"sms4", D_CBC_SMS4},
#endif
#ifndef OPENSSL_NO_ZUC
{"zuc", D_ZUC},
#endif
{NULL}
};
Expand Down Expand Up @@ -1188,10 +1195,11 @@ static int SM2_encrypt_loop(void *args)
size_t *sm2cipherlen = &tempargs->cipherlen;
int ret, count;
for (count = 0; COND(sm2enc_c[testnum][0]); count++) {
ret = SM2_encrypt(NID_sm3, buf, 32, sm2cipher,
*sm2cipherlen = BUFSIZE;
ret = SM2_encrypt(NID_sm3, buf, 32, sm2cipher,
sm2cipherlen, sm2[testnum]);
if (ret == 0) {
BIO_printf(bio_err, "SM2 sign failure\n");
BIO_printf(bio_err, "SM2 encrypt failure\n");
ERR_print_errors(bio_err);
count = -1;
break;
Expand Down Expand Up @@ -1494,6 +1502,9 @@ int speed_main(int argc, char **argv)
#ifndef OPENSSL_NO_SMS4
sms4_key_t sms4_ks;
#endif
#ifndef OPENSSL_NO_ZUC
ZUC_KEY zuc_ks;
#endif
#ifndef OPENSSL_NO_BF
BF_KEY bf_ks;
#endif
Expand Down Expand Up @@ -1985,6 +1996,9 @@ int speed_main(int argc, char **argv)
#ifndef OPENSSL_NO_SMS4
sms4_set_encrypt_key(&sms4_ks, key16);
#endif
#ifndef OPENSSL_NO_ZUC
ZUC_set_key(&zuc_ks, key16, iv);
#endif
#ifndef OPENSSL_NO_RC4
RC4_set_key(&rc4_ks, 16, key16);
#endif
Expand Down Expand Up @@ -2045,6 +2059,7 @@ int speed_main(int argc, char **argv)
c[D_GHASH][0] = count;
c[D_SM3][0] = count;
c[D_CBC_SMS4][0] = count;
c[D_ZUC][0] = count;

for (i = 1; i < SIZE_NUM; i++) {
long l0, l1;
Expand Down Expand Up @@ -2086,6 +2101,7 @@ int speed_main(int argc, char **argv)
c[D_IGE_192_AES][i] = c[D_IGE_192_AES][i - 1] * l0 / l1;
c[D_IGE_256_AES][i] = c[D_IGE_256_AES][i - 1] * l0 / l1;
c[D_CBC_SMS4][i] = c[D_CBC_SMS4][i - 1] * l0 / l1;
c[D_ZUC][i] = c[D_ZUC][i - 1] * l0 / l1;
}

# ifndef OPENSSL_NO_RSA
Expand Down Expand Up @@ -2588,6 +2604,24 @@ int speed_main(int argc, char **argv)
}
}
#endif
#ifndef OPENSSL_NO_ZUC
if (doit[D_ZUC]) {
if (async_jobs > 0) {
BIO_printf(bio_err, "Async mode is not supported with %s\n",
names[D_ZUC]);
doit[D_ZUC] = 0;
}
for (testnum = 0; testnum < SIZE_NUM && async_init == 0; testnum++) {
print_message(names[D_ZUC], c[D_ZUC][testnum], lengths[testnum]);
Time_F(START);
for (count = 0, run = 1; COND(c[D_ZUC][testnum]); count++)
ZUC_generate_keystream(&zuc_ks, lengths[testnum]/4,
(unsigned int *)loopargs[0].buf);
d = Time_F(STOP);
print_result(D_ZUC, testnum, count, d);
}
}
#endif
#ifndef OPENSSL_NO_RC2
if (doit[D_CBC_RC2]) {
if (async_jobs > 0) {
Expand Down Expand Up @@ -3160,6 +3194,7 @@ int speed_main(int argc, char **argv)
EC_KEY_precompute_mult(loopargs[i].sm2[testnum], NULL);
/* Perform SM2 encryption test */
EC_KEY_generate_key(loopargs[i].sm2[testnum]);
loopargs[i].cipherlen = BUFSIZE;
st = SM2_encrypt(NID_sm3, loopargs[i].buf, 32, loopargs[i].buf2,
&loopargs[i].cipherlen, loopargs[i].sm2[testnum]);
if (st == 0)
Expand Down

0 comments on commit 4bab277

Please sign in to comment.