diff --git a/CMakeLists.txt b/CMakeLists.txt index 917756f1..191f24ed 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -209,6 +209,11 @@ if (ENABLE_RDRND) set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mrdrnd -mrdseed") endif() +option(ENABLE_GMT_0105_RNG "Enable GM/T 0105 Software RNG" OFF) +if (ENABLE_GMT_0105_RNG) + list(APPEND src src/sm3_rng.c src/sm4_cbc_mac.c src/sm4_rng.c) + list(APPEND tests sm3_rng sm4_cbc_mac sm4_rng) +endif() if (WIN32) list(APPEND src src/rand_win.c) diff --git a/src/sm4_cbc_mac.c b/src/sm4_cbc_mac.c index f3833499..e754f41f 100644 --- a/src/sm4_cbc_mac.c +++ b/src/sm4_cbc_mac.c @@ -47,67 +47,3 @@ void sm4_cbc_mac_finish(SM4_CBC_MAC_CTX *ctx, uint8_t mac[16]) } memcpy(mac, ctx->iv, 16); } - -static int test_sm4_cbc_mac(void) -{ - SM4_KEY sm4_key; - SM4_CBC_MAC_CTX ctx; - uint8_t key[16]; - uint8_t iv[16] = {0}; - uint8_t m[128]; - uint8_t c[128]; - uint8_t mac1[16]; - uint8_t mac2[16]; - uint8_t *p; - size_t len, left; - - rand_bytes(key, sizeof(key)); - rand_bytes(m, sizeof(m)); - sm4_set_encrypt_key(&sm4_key, key); - - // test 1 - sm4_cbc_encrypt(&sm4_key, iv, m, sizeof(m)/16, c); - memcpy(mac1, c + sizeof(m) - 16, 16); - - sm4_cbc_mac_init(&ctx, key); - p = m; - len = 0; - left = sizeof(m); - while (left) { - len = left < len ? left : len; - sm4_cbc_mac_update(&ctx, p, len); - p += len; - left -= len; - len++; - } - sm4_cbc_mac_finish(&ctx, mac2); - if (memcmp(mac1, mac2, 16)) { - error_print(); - return -1; - } - - // test 2 - m[sizeof(m) - 1] = 0; - sm4_cbc_encrypt(&sm4_key, iv, m, sizeof(m)/16, c); - memcpy(mac1, c + sizeof(m) - 16, 16); - - sm4_cbc_mac_init(&ctx, key); - p = m; - len = 0; - left = sizeof(m) - 1; - while (left) { - len = left < len ? left : len; - sm4_cbc_mac_update(&ctx, p, len); - p += len; - left -= len; - len++; - } - sm4_cbc_mac_finish(&ctx, mac2); - if (memcmp(mac1, mac2, 16)) { - error_print(); - return -1; - } - - printf("%s() ok\n", __FUNCTION__); - return 1; -} diff --git a/tests/sm3_rngtest.c b/tests/sm3_rngtest.c new file mode 100644 index 00000000..4beb0d99 --- /dev/null +++ b/tests/sm3_rngtest.c @@ -0,0 +1,23 @@ +/* + * Copyright 2014-2022 The GmSSL Project. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the License); you may + * not use this file except in compliance with the License. + * + * http://www.apache.org/licenses/LICENSE-2.0 + */ + + +#include +#include +#include +#include +#include +#include + + +int main(void) +{ + error_print(); + return 1; +} diff --git a/tests/sm4_cbc_mactest.c b/tests/sm4_cbc_mactest.c new file mode 100644 index 00000000..a898cc15 --- /dev/null +++ b/tests/sm4_cbc_mactest.c @@ -0,0 +1,87 @@ +/* + * Copyright 2014-2022 The GmSSL Project. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the License); you may + * not use this file except in compliance with the License. + * + * http://www.apache.org/licenses/LICENSE-2.0 + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +static int test_sm4_cbc_mac(void) +{ + SM4_KEY sm4_key; + SM4_CBC_MAC_CTX ctx; + uint8_t key[16]; + uint8_t iv[16] = {0}; + uint8_t m[128]; + uint8_t c[128]; + uint8_t mac1[16]; + uint8_t mac2[16]; + uint8_t *p; + size_t len, left; + + rand_bytes(key, sizeof(key)); + rand_bytes(m, sizeof(m)); + sm4_set_encrypt_key(&sm4_key, key); + + // test 1 + sm4_cbc_encrypt(&sm4_key, iv, m, sizeof(m)/16, c); + memcpy(mac1, c + sizeof(m) - 16, 16); + + sm4_cbc_mac_init(&ctx, key); + p = m; + len = 0; + left = sizeof(m); + while (left) { + len = left < len ? left : len; + sm4_cbc_mac_update(&ctx, p, len); + p += len; + left -= len; + len++; + } + sm4_cbc_mac_finish(&ctx, mac2); + if (memcmp(mac1, mac2, 16)) { + error_print(); + return -1; + } + + // test 2 + m[sizeof(m) - 1] = 0; + sm4_cbc_encrypt(&sm4_key, iv, m, sizeof(m)/16, c); + memcpy(mac1, c + sizeof(m) - 16, 16); + + sm4_cbc_mac_init(&ctx, key); + p = m; + len = 0; + left = sizeof(m) - 1; + while (left) { + len = left < len ? left : len; + sm4_cbc_mac_update(&ctx, p, len); + p += len; + left -= len; + len++; + } + sm4_cbc_mac_finish(&ctx, mac2); + if (memcmp(mac1, mac2, 16)) { + error_print(); + return -1; + } + + printf("%s() ok\n", __FUNCTION__); + return 1; +} + +int main(void) +{ + if (test_sm4_cbc_mac() != 1) { error_print(); return -1; } + return 0; +} diff --git a/tests/sm4_rngtest.c b/tests/sm4_rngtest.c new file mode 100644 index 00000000..8e912d5a --- /dev/null +++ b/tests/sm4_rngtest.c @@ -0,0 +1,23 @@ +/* + * Copyright 2014-2022 The GmSSL Project. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the License); you may + * not use this file except in compliance with the License. + * + * http://www.apache.org/licenses/LICENSE-2.0 + */ + + +#include +#include +#include +#include +#include +#include + + +int main(void) +{ + error_print(); + return 1; +}