Skip to content

Commit

Permalink
Add sm4_cl speed test
Browse files Browse the repository at this point in the history
  • Loading branch information
guanzhi committed Apr 22, 2024
1 parent 242bbb3 commit fa5bf0e
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 4 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ include_directories(include)
option(ENABLE_SM4_TBOX "Enable SM4 merged S-Box implementation" ON)
option(ENABLE_SM4_AARCH64 "Enable SM4 AARCH64 assembly implementation" OFF)
option(ENABLE_SM4_CTR_AESNI_AVX "Enable SM4 CTR AESNI+AVX assembly implementation" OFF)
option(ENABLE_SM4_CL "Enable SM4 OpenCL" OFF)

option(ENABLE_SM4_ECB "Enable SM4 ECB mode" OFF)
option(ENABLE_SM4_OFB "Enable SM4 OFB mode" OFF)
Expand Down
62 changes: 58 additions & 4 deletions tests/sm4_cltest.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <gmssl/sm4_cl.h>
#include <gmssl/hex.h>
#include <gmssl/rand.h>
Expand Down Expand Up @@ -47,7 +48,6 @@ int test_sm4_cl(void)
for (i = 0; i < nblocks; i++) {
memcpy(buf + 16 * i, plaintext, 16);
}
format_bytes(stderr, 0, 0, "in", buf, nblocks * 16);

if (sm4_cl_set_encrypt_key(&ctx, key) != 1) {
error_print();
Expand All @@ -74,15 +74,69 @@ int test_sm4_cl(void)
return ret;
}

static int test_sm4_cl_ctr(void)

int test_sm4_cl_speed(void)
{
return 1;
const uint8_t key[16] = {
0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
};
const uint8_t plaintext[16] = {
0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
};
const uint8_t ciphertext[16] = {
0x68, 0x1e, 0xdf, 0x34, 0xd2, 0x06, 0x96, 0x5e,
0x86, 0xb3, 0xe9, 0x4f, 0x53, 0x6e, 0x42, 0x46,
};

int ret = -1;
SM4_CL_CTX ctx;
size_t nblocks = 1024*1024;
uint8_t *buf = NULL;
clock_t start, end;
double seconds;
size_t i;


if (!(buf = (uint8_t *)malloc(16 * nblocks))) {
error_print();
return -1;
}
for (i = 0; i < nblocks; i++) {
memcpy(buf + 16 * i, plaintext, 16);
}

if (sm4_cl_set_encrypt_key(&ctx, key) != 1) {
error_print();
goto end;
}

start = clock();
if (sm4_cl_encrypt(&ctx, buf, nblocks, buf) != 1) {
error_print();
goto end;
}
end = clock();

seconds = (double)(end - start)/CLOCKS_PER_SEC;

fprintf(stderr, "sm4_cl_encrypt: %f-MiB per seconds\n", 16/seconds);




ret = 1;
end:
if (buf) free(buf);
sm4_cl_cleanup(&ctx);
return ret;
}

int main(void)
{
if (test_sm4_cl() != 1) goto err;
if (test_sm4_cl_ctr() != 1) goto err;
if (test_sm4_cl_speed() != 1) goto err;
printf("%s all tests passed\n", __FILE__);
return 0;
err:
Expand Down

0 comments on commit fa5bf0e

Please sign in to comment.