mirror of
https://github.com/guanzhi/GmSSL.git
synced 2026-05-06 16:36:16 +08:00
Update SM4 API
This commit is contained in:
@@ -12,6 +12,7 @@
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <time.h>
|
||||
#include <gmssl/sm4.h>
|
||||
#include <gmssl/hex.h>
|
||||
#include <gmssl/rand.h>
|
||||
@@ -357,6 +358,37 @@ static int test_sm4_ctr_ctx_multi_updates(void)
|
||||
return 1;
|
||||
}
|
||||
|
||||
extern void sm4_ctr32_encrypt_4blocks(const SM4_KEY *key, uint8_t iv[16], const uint8_t *in, size_t num_4blocks, uint8_t *out);
|
||||
|
||||
|
||||
static int test_sm4_ctr32_encrypt_blocks_speed(void)
|
||||
{
|
||||
SM4_KEY sm4_key;
|
||||
uint8_t key[16];
|
||||
uint8_t ctr[16];
|
||||
uint8_t buf[4096];
|
||||
clock_t begin, end;
|
||||
double seconds;
|
||||
int i;
|
||||
|
||||
memset(key, 0, sizeof(key));
|
||||
memset(ctr, 0, sizeof(ctr));
|
||||
memset(buf, 0, sizeof(buf));
|
||||
|
||||
sm4_set_encrypt_key(&sm4_key, key);
|
||||
|
||||
begin = clock();
|
||||
for (i = 0; i < 4096; i++) {
|
||||
sm4_ctr32_encrypt_4blocks(&sm4_key, ctr, buf, sizeof(buf)/64, buf);
|
||||
}
|
||||
end = clock();
|
||||
|
||||
seconds = (double)(end - begin)/CLOCKS_PER_SEC;
|
||||
printf("%s: %f MiB per second\n", __FUNCTION__, 16/seconds);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
if (test_sm4_ctr() != 1) goto err;
|
||||
@@ -365,6 +397,7 @@ int main(void)
|
||||
if (test_sm4_ctr_ctx() != 1) goto err;
|
||||
if (test_sm4_ctr_ctx_multi_updates() != 1) goto err;
|
||||
|
||||
if (test_sm4_ctr32_encrypt_blocks_speed() != 1) goto err;
|
||||
printf("%s all tests passed\n", __FILE__);
|
||||
return 0;
|
||||
err:
|
||||
|
||||
@@ -32,10 +32,10 @@ static int test_sm4_ecb(void)
|
||||
rand_bytes(plaintext, sizeof(plaintext));
|
||||
|
||||
sm4_set_encrypt_key(&sm4_key, key);
|
||||
sm4_ecb_encrypt(&sm4_key, plaintext, sizeof(plaintext)/16, encrypted);
|
||||
sm4_ecb_encrypt_blocks(&sm4_key, plaintext, sizeof(plaintext)/16, encrypted);
|
||||
|
||||
sm4_set_decrypt_key(&sm4_key, key);
|
||||
sm4_ecb_encrypt(&sm4_key, encrypted, sizeof(encrypted)/16, decrypted);
|
||||
sm4_ecb_encrypt_blocks(&sm4_key, encrypted, sizeof(encrypted)/16, decrypted);
|
||||
|
||||
if (memcmp(decrypted, plaintext, sizeof(plaintext)) != 0) {
|
||||
error_print();
|
||||
@@ -69,7 +69,7 @@ static int test_sm4_ecb_test_vectors(void)
|
||||
uint8_t decrypted[sizeof(plaintext)] = {0};
|
||||
|
||||
sm4_set_encrypt_key(&sm4_key, key);
|
||||
sm4_ecb_encrypt(&sm4_key, plaintext, sizeof(plaintext)/16, encrypted);
|
||||
sm4_ecb_encrypt_blocks(&sm4_key, plaintext, sizeof(plaintext)/16, encrypted);
|
||||
|
||||
format_bytes(stderr, 0, 0, "", encrypted, sizeof(encrypted));
|
||||
|
||||
@@ -79,7 +79,7 @@ static int test_sm4_ecb_test_vectors(void)
|
||||
}
|
||||
|
||||
sm4_set_decrypt_key(&sm4_key, key);
|
||||
sm4_ecb_encrypt(&sm4_key, encrypted, sizeof(encrypted)/16, decrypted);
|
||||
sm4_ecb_encrypt_blocks(&sm4_key, encrypted, sizeof(encrypted)/16, decrypted);
|
||||
|
||||
if (memcmp(decrypted, plaintext, sizeof(plaintext)) != 0) {
|
||||
error_print();
|
||||
|
||||
@@ -91,49 +91,36 @@ static int test_sm4(void)
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int test_sm4_speed(void)
|
||||
static int test_sm4_encrypt_speed(void)
|
||||
{
|
||||
SM4_KEY sm4_key;
|
||||
uint8_t key[16] = {0};
|
||||
uint8_t block[16] = {0};
|
||||
clock_t start, end;
|
||||
uint8_t buf[16];
|
||||
size_t nbytes = 16 * 1024 * 1024;
|
||||
clock_t begin, end;
|
||||
double seconds;
|
||||
int i;
|
||||
|
||||
sm4_set_encrypt_key(&sm4_key, key);
|
||||
|
||||
start = clock();
|
||||
for (i = 0; i < 1024*1024; i++) {
|
||||
sm4_encrypt(&sm4_key, block, block);
|
||||
begin = clock();
|
||||
for (i = 0; i < nbytes/sizeof(buf); i++) {
|
||||
sm4_encrypt(&sm4_key, buf, buf);
|
||||
}
|
||||
end = clock();
|
||||
|
||||
seconds = (double)(end - start)/ CLOCKS_PER_SEC;
|
||||
|
||||
fprintf(stderr, "sm4_encrypt: %f-MiB per seconds\n", 16/seconds);
|
||||
seconds = (double)(end - begin)/ CLOCKS_PER_SEC;
|
||||
fprintf(stderr, "sm4_encrypt: %f MiB per second\n", nbytes/(1024 * 1024 *seconds));
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
int main(void)
|
||||
{
|
||||
if (test_sm4() != 1) goto err;
|
||||
if (test_sm4_speed() != 1) goto err;
|
||||
|
||||
|
||||
/*
|
||||
if (test_sm4_cbc() != 1) goto err;
|
||||
if (test_sm4_cbc_padding() != 1) goto err;
|
||||
|
||||
if (test_sm4_gcm() != 1) goto err;
|
||||
if (test_sm4_gcm_gbt36624_1() != 1) goto err;
|
||||
if (test_sm4_gcm_gbt36624_2() != 1) goto err;
|
||||
if (test_sm4_cbc_update() != 1) goto err;
|
||||
|
||||
|
||||
*/
|
||||
#if ENABLE_TEST_SPEED
|
||||
if (test_sm4_encrypt_speed() != 1) goto err;
|
||||
#endif
|
||||
printf("%s all tests passed\n", __FILE__);
|
||||
return 0;
|
||||
err:
|
||||
|
||||
Reference in New Issue
Block a user