Add sm4_MODE_encrypt_blocks

This commit is contained in:
Zhi Guan
2024-04-29 22:35:25 +08:00
parent 33c546f48a
commit 5cf6f2609e
10 changed files with 618 additions and 121 deletions

View File

@@ -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_blocks(&sm4_key, plaintext, sizeof(plaintext)/16, encrypted);
sm4_encrypt_blocks(&sm4_key, plaintext, sizeof(plaintext)/16, encrypted);
sm4_set_decrypt_key(&sm4_key, key);
sm4_ecb_encrypt_blocks(&sm4_key, encrypted, sizeof(encrypted)/16, decrypted);
sm4_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_blocks(&sm4_key, plaintext, sizeof(plaintext)/16, encrypted);
sm4_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_blocks(&sm4_key, encrypted, sizeof(encrypted)/16, decrypted);
sm4_encrypt_blocks(&sm4_key, encrypted, sizeof(encrypted)/16, decrypted);
if (memcmp(decrypted, plaintext, sizeof(plaintext)) != 0) {
error_print();

View File

@@ -12,6 +12,7 @@
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <time.h>
#include <gmssl/hex.h>
#include <gmssl/sm4.h>
#include <gmssl/error.h>
@@ -312,12 +313,45 @@ static int test_sm4_gcm_ctx(void)
return 1;
}
static int speed_sm4_gcm_encrypt(void)
{
SM4_KEY sm4_key;
uint8_t key[16] = {0};
uint8_t iv[12];
uint8_t aad[16];
uint8_t tag[16];
uint32_t buf[1024];
clock_t begin, end;
double seconds;
int i;
sm4_set_encrypt_key(&sm4_key, key);
for (i = 0; i < 4096; i++) {
sm4_gcm_encrypt(&sm4_key, iv, sizeof(iv), aad, sizeof(aad), (uint8_t *)buf, sizeof(buf), (uint8_t *)buf, 16, tag);
}
begin = clock();
for (i = 0; i < 4096; i++) {
sm4_gcm_encrypt(&sm4_key, iv, sizeof(iv), aad, sizeof(aad), (uint8_t *)buf, sizeof(buf), (uint8_t *)buf, 16, tag);
}
end = clock();
seconds = (double)(end - begin)/ CLOCKS_PER_SEC;
fprintf(stderr, "%s: %f MiB per second\n", __FUNCTION__, 16/seconds);
return 1;
}
int main(void)
{
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_gcm_ctx() != 1) goto err;
#if ENABLE_TEST_SPEED
if (speed_sm4_gcm_encrypt() != 1) goto err;
#endif
printf("%s all tests passed\n", __FILE__);
return 0;
err:

View File

@@ -133,7 +133,7 @@ static int test_sm4_encrypt_blocks(void)
return 1;
}
static int test_sm4_encrypt_speed(void)
static int speed_sm4_encrypt(void)
{
SM4_KEY sm4_key;
uint8_t key[16] = {0};
@@ -155,49 +155,160 @@ static int test_sm4_encrypt_speed(void)
end = clock();
seconds = (double)(end - begin)/ CLOCKS_PER_SEC;
fprintf(stderr, "sm4_encrypt: %f MiB per second\n", nbytes/(1024 * 1024 *seconds));
fprintf(stderr, "%s: %f MiB per second\n", __FUNCTION__, nbytes/(1024 * 1024 *seconds));
return 1;
}
static int test_sm4_encrypt_blocks_speed(void)
static int speed_sm4_encrypt_blocks(void)
{
SM4_KEY sm4_key;
uint8_t key[16] = {0};
//uint32_t buf[1024];
uint8_t buf[4096 + 100] __attribute__((aligned(16)));
uint32_t buf[1024];
clock_t begin, end;
double seconds;
int i;
sm4_set_encrypt_key(&sm4_key, key);
for (i = 0; i < 4096; i++) {
// fprintf(stderr, ".");
sm4_encrypt_blocks(&sm4_key, (uint8_t *)buf, sizeof(buf)/16, (uint8_t *)buf);
}
//fprintf(stderr, "start\n");
begin = clock();
for (i = 0; i < 4096; i++) {
sm4_encrypt_blocks(&sm4_key, (uint8_t *)buf, sizeof(buf)/16, (uint8_t *)buf);
// fprintf(stderr, ".");
}
end = clock();
seconds = (double)(end - begin)/ CLOCKS_PER_SEC;
fprintf(stderr, "sm4_encrypt_blocks: %f MiB per second\n", 16/seconds);
fprintf(stderr, "%s: %f MiB per second\n", __FUNCTION__, 16/seconds);
return 1;
}
static int speed_sm4_cbc_encrypt_blocks(void)
{
SM4_KEY sm4_key;
uint8_t key[16] = {0};
uint8_t iv[16];
uint32_t buf[1024];
clock_t begin, end;
double seconds;
int i;
sm4_set_encrypt_key(&sm4_key, key);
for (i = 0; i < 4096; i++) {
sm4_cbc_encrypt_blocks(&sm4_key, iv, (uint8_t *)buf, sizeof(buf)/16, (uint8_t *)buf);
}
begin = clock();
for (i = 0; i < 4096; i++) {
sm4_cbc_encrypt_blocks(&sm4_key, iv, (uint8_t *)buf, sizeof(buf)/16, (uint8_t *)buf);
}
end = clock();
seconds = (double)(end - begin)/ CLOCKS_PER_SEC;
fprintf(stderr, "%s: %f MiB per second\n", __FUNCTION__, 16/seconds);
return 1;
}
static int speed_sm4_cbc_decrypt_blocks(void)
{
SM4_KEY sm4_key;
uint8_t key[16] = {0};
uint8_t iv[16];
uint32_t buf[1024];
clock_t begin, end;
double seconds;
int i;
sm4_set_decrypt_key(&sm4_key, key);
for (i = 0; i < 4096; i++) {
sm4_cbc_decrypt_blocks(&sm4_key, iv, (uint8_t *)buf, sizeof(buf)/16, (uint8_t *)buf);
}
begin = clock();
for (i = 0; i < 4096; i++) {
sm4_cbc_decrypt_blocks(&sm4_key, iv, (uint8_t *)buf, sizeof(buf)/16, (uint8_t *)buf);
}
end = clock();
seconds = (double)(end - begin)/ CLOCKS_PER_SEC;
fprintf(stderr, "%s: %f MiB per second\n", __FUNCTION__, 16/seconds);
return 1;
}
static int speed_sm4_ctr_encrypt_blocks(void)
{
SM4_KEY sm4_key;
uint8_t key[16] = {0};
uint8_t ctr[16];
uint32_t buf[1024];
clock_t begin, end;
double seconds;
int i;
sm4_set_encrypt_key(&sm4_key, key);
rand_bytes(ctr, sizeof(ctr));
for (i = 0; i < 4096; i++) {
sm4_ctr_encrypt_blocks(&sm4_key, ctr, (uint8_t *)buf, sizeof(buf)/16, (uint8_t *)buf);
}
begin = clock();
for (i = 0; i < 4096; i++) {
sm4_ctr_encrypt_blocks(&sm4_key, ctr, (uint8_t *)buf, sizeof(buf)/16, (uint8_t *)buf);
}
end = clock();
seconds = (double)(end - begin)/ CLOCKS_PER_SEC;
fprintf(stderr, "%s: %f MiB per second\n", __FUNCTION__, 16/seconds);
return 1;
}
static int speed_sm4_ctr32_encrypt_blocks(void)
{
SM4_KEY sm4_key;
uint8_t key[16] = {0};
uint8_t ctr[16];
uint32_t buf[1024];
clock_t begin, end;
double seconds;
int i;
sm4_set_encrypt_key(&sm4_key, key);
rand_bytes(ctr, sizeof(ctr));
for (i = 0; i < 4096; i++) {
sm4_ctr32_encrypt_blocks(&sm4_key, ctr, (uint8_t *)buf, sizeof(buf)/16, (uint8_t *)buf);
}
begin = clock();
for (i = 0; i < 4096; i++) {
sm4_ctr32_encrypt_blocks(&sm4_key, ctr, (uint8_t *)buf, sizeof(buf)/16, (uint8_t *)buf);
}
end = clock();
seconds = (double)(end - begin)/ CLOCKS_PER_SEC;
fprintf(stderr, "%s: %f MiB per second\n", __FUNCTION__, 16/seconds);
return 1;
}
int main(void)
{
if (test_sm4() != 1) goto err;
if (test_sm4_encrypt_blocks() != 1) goto err;
#if ENABLE_TEST_SPEED
if (test_sm4_encrypt_speed() != 1) goto err;
if (test_sm4_encrypt_blocks_speed() != 1) goto err;
if (speed_sm4_encrypt() != 1) goto err;
if (speed_sm4_encrypt_blocks() != 1) goto err;
if (speed_sm4_cbc_encrypt_blocks() != 1) goto err;
if (speed_sm4_cbc_decrypt_blocks() != 1) goto err;
if (speed_sm4_ctr_encrypt_blocks() != 1) goto err;
if (speed_sm4_ctr32_encrypt_blocks() != 1) goto err;
#endif
printf("%s all tests passed\n", __FILE__);
return 0;