mirror of
https://github.com/guanzhi/GmSSL.git
synced 2026-09-24 06:13:43 +08:00
Update tools help
This commit is contained in:
@@ -1026,7 +1026,7 @@ endif()
|
|||||||
#
|
#
|
||||||
set(CPACK_PACKAGE_NAME "GmSSL")
|
set(CPACK_PACKAGE_NAME "GmSSL")
|
||||||
set(CPACK_PACKAGE_VENDOR "GmSSL develop team")
|
set(CPACK_PACKAGE_VENDOR "GmSSL develop team")
|
||||||
set(CPACK_PACKAGE_VERSION "3.3.0-dev.1173")
|
set(CPACK_PACKAGE_VERSION "3.3.0-dev.1174")
|
||||||
set(CPACK_PACKAGE_DESCRIPTION_FILE ${PROJECT_SOURCE_DIR}/README.md)
|
set(CPACK_PACKAGE_DESCRIPTION_FILE ${PROJECT_SOURCE_DIR}/README.md)
|
||||||
set(CPACK_NSIS_MODIFY_PATH ON)
|
set(CPACK_NSIS_MODIFY_PATH ON)
|
||||||
include(CPack)
|
include(CPack)
|
||||||
|
|||||||
@@ -25,6 +25,18 @@ function(gmssl_run_capture out_var)
|
|||||||
set(${out_var} "${TEST_STDOUT}" PARENT_SCOPE)
|
set(${out_var} "${TEST_STDOUT}" PARENT_SCOPE)
|
||||||
endfunction()
|
endfunction()
|
||||||
|
|
||||||
|
function(gmssl_expect_fail)
|
||||||
|
execute_process(
|
||||||
|
COMMAND ${GMSSL_BIN} ${ARGN}
|
||||||
|
RESULT_VARIABLE TEST_RESULT
|
||||||
|
ERROR_VARIABLE TEST_STDERR
|
||||||
|
OUTPUT_VARIABLE TEST_STDOUT
|
||||||
|
)
|
||||||
|
if(TEST_RESULT EQUAL 0)
|
||||||
|
message(FATAL_ERROR "command unexpectedly succeeded: ${GMSSL_BIN} ${ARGN}\nstderr: ${TEST_STDERR}\nstdout: ${TEST_STDOUT}")
|
||||||
|
endif()
|
||||||
|
endfunction()
|
||||||
|
|
||||||
function(gmssl_expect_stdout expected)
|
function(gmssl_expect_stdout expected)
|
||||||
gmssl_run_capture(TEST_STDOUT ${ARGN})
|
gmssl_run_capture(TEST_STDOUT ${ARGN})
|
||||||
if(NOT TEST_STDOUT STREQUAL "${expected}")
|
if(NOT TEST_STDOUT STREQUAL "${expected}")
|
||||||
|
|||||||
@@ -17,11 +17,29 @@ file(WRITE tool_sm4_cbc_kat.plain "0123456789abcdef")
|
|||||||
gmssl_run(sm4_cbc -encrypt -key ${SM4_KEY} -iv ${SM4_IV}
|
gmssl_run(sm4_cbc -encrypt -key ${SM4_KEY} -iv ${SM4_IV}
|
||||||
-in tool_sm4_cbc_kat.plain -out tool_sm4_cbc_kat.cipher)
|
-in tool_sm4_cbc_kat.plain -out tool_sm4_cbc_kat.cipher)
|
||||||
gmssl_expect_file_hex(tool_sm4_cbc_kat.cipher
|
gmssl_expect_file_hex(tool_sm4_cbc_kat.cipher
|
||||||
"e6887b77dbabb572ffa07fed7548b192ceaace11f2b90b94c2b7a4d9382e471e")
|
"e6887b77dbabb572ffa07fed7548b192")
|
||||||
gmssl_run(sm4_cbc -decrypt -key ${SM4_KEY} -iv ${SM4_IV}
|
gmssl_run(sm4_cbc -decrypt -key ${SM4_KEY} -iv ${SM4_IV}
|
||||||
-in tool_sm4_cbc_kat.cipher -out tool_sm4_cbc_kat.decrypt)
|
-in tool_sm4_cbc_kat.cipher -out tool_sm4_cbc_kat.decrypt)
|
||||||
gmssl_files_equal(tool_sm4_cbc_kat.plain tool_sm4_cbc_kat.decrypt)
|
gmssl_files_equal(tool_sm4_cbc_kat.plain tool_sm4_cbc_kat.decrypt)
|
||||||
|
|
||||||
|
file(WRITE tool_sm4_cbc_short.plain "abc")
|
||||||
|
gmssl_expect_fail(sm4_cbc -encrypt -key ${SM4_KEY} -iv ${SM4_IV}
|
||||||
|
-in tool_sm4_cbc_short.plain -out tool_sm4_cbc_short.cipher)
|
||||||
|
gmssl_run(sm4_cbc -encrypt -pkcs7_padding -key ${SM4_KEY} -iv ${SM4_IV}
|
||||||
|
-in tool_sm4_cbc_short.plain -out tool_sm4_cbc_short_pkcs7.cipher)
|
||||||
|
gmssl_run(sm4_cbc -decrypt -pkcs7_padding -key ${SM4_KEY} -iv ${SM4_IV}
|
||||||
|
-in tool_sm4_cbc_short_pkcs7.cipher -out tool_sm4_cbc_short_pkcs7.decrypt)
|
||||||
|
gmssl_files_equal(tool_sm4_cbc_short.plain tool_sm4_cbc_short_pkcs7.decrypt)
|
||||||
|
|
||||||
|
file(WRITE tool_sm4_cbc_pkcs7_kat.plain "0123456789abcdef")
|
||||||
|
gmssl_run(sm4_cbc -encrypt -pkcs7_padding -key ${SM4_KEY} -iv ${SM4_IV}
|
||||||
|
-in tool_sm4_cbc_pkcs7_kat.plain -out tool_sm4_cbc_pkcs7_kat.cipher)
|
||||||
|
gmssl_expect_file_hex(tool_sm4_cbc_pkcs7_kat.cipher
|
||||||
|
"e6887b77dbabb572ffa07fed7548b192ceaace11f2b90b94c2b7a4d9382e471e")
|
||||||
|
gmssl_run(sm4_cbc -decrypt -pkcs7_padding -key ${SM4_KEY} -iv ${SM4_IV}
|
||||||
|
-in tool_sm4_cbc_pkcs7_kat.cipher -out tool_sm4_cbc_pkcs7_kat.decrypt)
|
||||||
|
gmssl_files_equal(tool_sm4_cbc_pkcs7_kat.plain tool_sm4_cbc_pkcs7_kat.decrypt)
|
||||||
|
|
||||||
file(WRITE tool_sm4_unified_cbc_kat.plain "0123456789abcdef")
|
file(WRITE tool_sm4_unified_cbc_kat.plain "0123456789abcdef")
|
||||||
gmssl_run(sm4 -cbc -encrypt -key ${SM4_KEY} -iv ${SM4_IV}
|
gmssl_run(sm4 -cbc -encrypt -key ${SM4_KEY} -iv ${SM4_IV}
|
||||||
-in tool_sm4_unified_cbc_kat.plain -out tool_sm4_unified_cbc_kat.cipher)
|
-in tool_sm4_unified_cbc_kat.plain -out tool_sm4_unified_cbc_kat.cipher)
|
||||||
@@ -32,6 +50,7 @@ gmssl_run(sm4 -cbc -decrypt -key ${SM4_KEY} -iv ${SM4_IV}
|
|||||||
gmssl_files_equal(tool_sm4_unified_cbc_kat.plain tool_sm4_unified_cbc_kat.decrypt)
|
gmssl_files_equal(tool_sm4_unified_cbc_kat.plain tool_sm4_unified_cbc_kat.decrypt)
|
||||||
|
|
||||||
gmssl_symmetric_roundtrip(tool_sm4_cbc sm4_cbc -key ${SM4_KEY} -iv ${SM4_IV})
|
gmssl_symmetric_roundtrip(tool_sm4_cbc sm4_cbc -key ${SM4_KEY} -iv ${SM4_IV})
|
||||||
|
gmssl_symmetric_roundtrip(tool_sm4_cbc_pkcs7 sm4_cbc -pkcs7_padding -key ${SM4_KEY} -iv ${SM4_IV})
|
||||||
gmssl_symmetric_roundtrip(tool_sm4_ctr sm4_ctr -key ${SM4_KEY} -iv ${SM4_IV})
|
gmssl_symmetric_roundtrip(tool_sm4_ctr sm4_ctr -key ${SM4_KEY} -iv ${SM4_IV})
|
||||||
gmssl_symmetric_roundtrip(tool_sm4_gcm sm4_gcm -key ${SM4_KEY} -iv 000000000000000000000000 -aad_hex 001122 -taglen 16)
|
gmssl_symmetric_roundtrip(tool_sm4_gcm sm4_gcm -key ${SM4_KEY} -iv 000000000000000000000000 -aad_hex 001122 -taglen 16)
|
||||||
|
|
||||||
|
|||||||
@@ -12,13 +12,13 @@ gmssl_files_equal(tool_zuc.plain tool_zuc.decrypt)
|
|||||||
gmssl_run(zuc_128_eea3
|
gmssl_run(zuc_128_eea3
|
||||||
-key 173d14ba5003731d7a60049470f00a29
|
-key 173d14ba5003731d7a60049470f00a29
|
||||||
-count 0x66035492 -bearer 15 -direction 0
|
-count 0x66035492 -bearer 15 -direction 0
|
||||||
-in_hex 6cf65340735552ab0c9752fa6f9025fe0bd675d9005875b2
|
-in tool_zuc.plain
|
||||||
-out tool_zuc_128_eea3.cipher)
|
-out tool_zuc_128_eea3.cipher)
|
||||||
gmssl_expect_file_hex(tool_zuc_128_eea3.cipher
|
gmssl_expect_file_hex(tool_zuc_128_eea3.cipher
|
||||||
"a6c85fc66afb8533aafc2518dfe784940ee1e4b030238cc8")
|
"fa0f3eb52d9be1af9e521680d313c40c")
|
||||||
gmssl_expect_stdout("390a91b7\n" zuc_128_eia3
|
gmssl_expect_stdout("b0361765\n" zuc_128_eia3
|
||||||
-key 00000000000000000000000000000000
|
-key 00000000000000000000000000000000
|
||||||
-count 0 -bearer 0 -direction 0 -in_hex 00)
|
-count 0 -bearer 0 -direction 0 -in tool_zuc.plain)
|
||||||
|
|
||||||
gmssl_run(zuc256
|
gmssl_run(zuc256
|
||||||
-key 0000000000000000000000000000000000000000000000000000000000000000
|
-key 0000000000000000000000000000000000000000000000000000000000000000
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ extern "C" {
|
|||||||
|
|
||||||
|
|
||||||
#define GMSSL_VERSION_NUM 30300
|
#define GMSSL_VERSION_NUM 30300
|
||||||
#define GMSSL_VERSION_STR "GmSSL 3.3.0-dev.1173"
|
#define GMSSL_VERSION_STR "GmSSL 3.3.0-dev.1174"
|
||||||
|
|
||||||
int gmssl_version_num(void);
|
int gmssl_version_num(void);
|
||||||
const char *gmssl_version_str(void);
|
const char *gmssl_version_str(void);
|
||||||
|
|||||||
@@ -160,10 +160,18 @@ static const char *options =
|
|||||||
" sm3 Generate SM3 hash\n"
|
" sm3 Generate SM3 hash\n"
|
||||||
" sm3hmac Generate SM3 HMAC tag\n"
|
" sm3hmac Generate SM3 HMAC tag\n"
|
||||||
" sm3_pbkdf2 Hash password into key using PBKDF2 algoritm\n"
|
" sm3_pbkdf2 Hash password into key using PBKDF2 algoritm\n"
|
||||||
" sm4 Encrypt or decrypt with SM4\n"
|
#ifdef ENABLE_SM4_ECB
|
||||||
" sm4_gcm Encrypt or decrypt with SM4 GCM\n"
|
" sm4_ecb Encrypt or decrypt with SM4 ECB\n"
|
||||||
|
#endif
|
||||||
" sm4_cbc Encrypt or decrypt with SM4 CBC\n"
|
" sm4_cbc Encrypt or decrypt with SM4 CBC\n"
|
||||||
|
#ifdef ENABLE_SM4_CFB
|
||||||
|
" sm4_cfb Encrypt or decrypt with SM4 CFB\n"
|
||||||
|
#endif
|
||||||
|
#ifdef ENABLE_SM4_OFB
|
||||||
|
" sm4_ofb Encrypt or decrypt with SM4 OFB\n"
|
||||||
|
#endif
|
||||||
" sm4_ctr Encrypt or decrypt with SM4 CTR\n"
|
" sm4_ctr Encrypt or decrypt with SM4 CTR\n"
|
||||||
|
" sm4_gcm Encrypt or decrypt with SM4 GCM\n"
|
||||||
#ifdef ENABLE_SM4_CCM
|
#ifdef ENABLE_SM4_CCM
|
||||||
" sm4_ccm Encrypt or decrypt with SM4 CCM\n"
|
" sm4_ccm Encrypt or decrypt with SM4 CCM\n"
|
||||||
#endif
|
#endif
|
||||||
@@ -173,15 +181,6 @@ static const char *options =
|
|||||||
#ifdef ENABLE_SM4_FF1
|
#ifdef ENABLE_SM4_FF1
|
||||||
" sm4_ff1 Encrypt or decrypt digits with SM4 FF1\n"
|
" sm4_ff1 Encrypt or decrypt digits with SM4 FF1\n"
|
||||||
#endif
|
#endif
|
||||||
#ifdef ENABLE_SM4_ECB
|
|
||||||
" sm4_ecb Encrypt or decrypt with SM4 ECB\n"
|
|
||||||
#endif
|
|
||||||
#ifdef ENABLE_SM4_CFB
|
|
||||||
" sm4_cfb Encrypt or decrypt with SM4 CFB\n"
|
|
||||||
#endif
|
|
||||||
#ifdef ENABLE_SM4_OFB
|
|
||||||
" sm4_ofb Encrypt or decrypt with SM4 OFB\n"
|
|
||||||
#endif
|
|
||||||
#ifdef ENABLE_SM4_CBC_MAC
|
#ifdef ENABLE_SM4_CBC_MAC
|
||||||
" sm4_cbc_mac Generate SM4 CBC-MAC\n"
|
" sm4_cbc_mac Generate SM4 CBC-MAC\n"
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -18,7 +18,7 @@
|
|||||||
#include <gmssl/error.h>
|
#include <gmssl/error.h>
|
||||||
|
|
||||||
|
|
||||||
static const char *usage = "{-encrypt|-decrypt} -key hex -iv hex [-in file] [-out file]";
|
static const char *usage = "{-encrypt|-decrypt} -key hex -iv hex [-pkcs7_padding] [-in file] [-out file]";
|
||||||
|
|
||||||
static const char *options =
|
static const char *options =
|
||||||
"\n"
|
"\n"
|
||||||
@@ -28,16 +28,20 @@ static const char *options =
|
|||||||
" -decrypt Decrypt\n"
|
" -decrypt Decrypt\n"
|
||||||
" -key hex Symmetric key in HEX format\n"
|
" -key hex Symmetric key in HEX format\n"
|
||||||
" -iv hex IV in HEX format\n"
|
" -iv hex IV in HEX format\n"
|
||||||
" -in file | stdin Input data\n"
|
" -pkcs7_padding Enable PKCS#7 padding\n"
|
||||||
|
" Encrypt input can be any byte length; decrypt input must be a multiple of 16 bytes\n"
|
||||||
|
" -in file | stdin Input data. Without `-pkcs7_padding`, input length must be a multiple of 16 bytes\n"
|
||||||
" -out file | stdout Output data\n"
|
" -out file | stdout Output data\n"
|
||||||
"\n"
|
"\n"
|
||||||
"Examples\n"
|
"Examples\n"
|
||||||
"\n"
|
"\n"
|
||||||
" $ TEXT=`gmssl rand -outlen 20 -hex`\n"
|
" KEY=`gmssl rand -outlen 16 -hex`\n"
|
||||||
" $ KEY=`gmssl rand -outlen 16 -hex`\n"
|
" IV=`gmssl rand -outlen 16 -hex`\n"
|
||||||
" $ IV=`gmssl rand -outlen 16 -hex`\n"
|
" echo -n 0123456789abcdef | gmssl sm4_cbc -encrypt -key $KEY -iv $IV -out sm4_cbc_ciphertext.bin\n"
|
||||||
" $ echo -n $TEXT | gmssl sm4_cbc -encrypt -key $KEY -iv $IV -out sm4_cbc_ciphertext.bin\n"
|
" gmssl sm4_cbc -decrypt -key $KEY -iv $IV -in sm4_cbc_ciphertext.bin\n"
|
||||||
" $ gmssl sm4_cbc -decrypt -key $KEY -iv $IV -in sm4_cbc_ciphertext.bin\n"
|
"\n"
|
||||||
|
" echo -n abc | gmssl sm4_cbc -encrypt -pkcs7_padding -key $KEY -iv $IV -out sm4_cbc_ciphertext.bin\n"
|
||||||
|
" gmssl sm4_cbc -decrypt -pkcs7_padding -key $KEY -iv $IV -in sm4_cbc_ciphertext.bin\n"
|
||||||
"\n";
|
"\n";
|
||||||
|
|
||||||
int sm4_cbc_main(int argc, char **argv)
|
int sm4_cbc_main(int argc, char **argv)
|
||||||
@@ -49,16 +53,25 @@ int sm4_cbc_main(int argc, char **argv)
|
|||||||
char *ivhex = NULL;
|
char *ivhex = NULL;
|
||||||
char *infile = NULL;
|
char *infile = NULL;
|
||||||
char *outfile = NULL;
|
char *outfile = NULL;
|
||||||
|
int pkcs7_padding = 0;
|
||||||
uint8_t key[16];
|
uint8_t key[16];
|
||||||
size_t keylen;
|
size_t keylen;
|
||||||
uint8_t iv[16];
|
uint8_t iv[16];
|
||||||
size_t ivlen;
|
size_t ivlen;
|
||||||
FILE *infp = stdin;
|
FILE *infp = stdin;
|
||||||
FILE *outfp = stdout;
|
FILE *outfp = stdout;
|
||||||
|
SM4_KEY sm4_key;
|
||||||
SM4_CBC_CTX ctx;
|
SM4_CBC_CTX ctx;
|
||||||
uint8_t buf[4096];
|
uint8_t buf[4096];
|
||||||
|
uint8_t outbuf[4096];
|
||||||
|
uint8_t block[16];
|
||||||
|
size_t block_nbytes = 0;
|
||||||
size_t inlen;
|
size_t inlen;
|
||||||
size_t outlen;
|
size_t outlen;
|
||||||
|
size_t nblocks;
|
||||||
|
size_t len;
|
||||||
|
size_t left;
|
||||||
|
size_t inpos;
|
||||||
|
|
||||||
argc--;
|
argc--;
|
||||||
argv++;
|
argv++;
|
||||||
@@ -108,6 +121,8 @@ int sm4_cbc_main(int argc, char **argv)
|
|||||||
fprintf(stderr, "gmssl %s: invalid IV hex digits\n", prog);
|
fprintf(stderr, "gmssl %s: invalid IV hex digits\n", prog);
|
||||||
goto end;
|
goto end;
|
||||||
}
|
}
|
||||||
|
} else if (!strcmp(*argv, "-pkcs7_padding")) {
|
||||||
|
pkcs7_padding = 1;
|
||||||
} else if (!strcmp(*argv, "-in")) {
|
} else if (!strcmp(*argv, "-in")) {
|
||||||
if (--argc < 1) goto bad;
|
if (--argc < 1) goto bad;
|
||||||
infile = *(++argv);
|
infile = *(++argv);
|
||||||
@@ -147,33 +162,74 @@ bad:
|
|||||||
goto end;
|
goto end;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (enc) {
|
if (pkcs7_padding && enc) {
|
||||||
if (sm4_cbc_encrypt_init(&ctx, key, iv) != 1) {
|
if (sm4_cbc_encrypt_init(&ctx, key, iv) != 1) {
|
||||||
error_print();
|
error_print();
|
||||||
goto end;
|
goto end;
|
||||||
}
|
}
|
||||||
} else {
|
} else if (pkcs7_padding) {
|
||||||
if (sm4_cbc_decrypt_init(&ctx, key, iv) != 1) {
|
if (sm4_cbc_decrypt_init(&ctx, key, iv) != 1) {
|
||||||
error_print();
|
error_print();
|
||||||
goto end;
|
goto end;
|
||||||
}
|
}
|
||||||
|
} else if (enc) {
|
||||||
|
sm4_set_encrypt_key(&sm4_key, key);
|
||||||
|
} else {
|
||||||
|
sm4_set_decrypt_key(&sm4_key, key);
|
||||||
}
|
}
|
||||||
|
|
||||||
while ((inlen = fread(buf, 1, sizeof(buf), infp)) > 0) {
|
while ((inlen = fread(buf, 1, sizeof(buf), infp)) > 0) {
|
||||||
|
|
||||||
if (enc) {
|
if (pkcs7_padding && enc) {
|
||||||
if (sm4_cbc_encrypt_update(&ctx, buf, inlen, buf, &outlen) != 1) {
|
if (sm4_cbc_encrypt_update(&ctx, buf, inlen, buf, &outlen) != 1) {
|
||||||
error_print();
|
error_print();
|
||||||
goto end;
|
goto end;
|
||||||
}
|
}
|
||||||
} else {
|
} else if (pkcs7_padding) {
|
||||||
if (sm4_cbc_decrypt_update(&ctx, buf, inlen, buf, &outlen) != 1) {
|
if (sm4_cbc_decrypt_update(&ctx, buf, inlen, buf, &outlen) != 1) {
|
||||||
error_print();
|
error_print();
|
||||||
goto end;
|
goto end;
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
outlen = 0;
|
||||||
|
inpos = 0;
|
||||||
|
|
||||||
|
if (block_nbytes) {
|
||||||
|
left = sizeof(block) - block_nbytes;
|
||||||
|
if (inlen < left) {
|
||||||
|
memcpy(block + block_nbytes, buf, inlen);
|
||||||
|
block_nbytes += inlen;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
memcpy(block + block_nbytes, buf, left);
|
||||||
|
if (enc) {
|
||||||
|
sm4_cbc_encrypt_blocks(&sm4_key, iv, block, 1, outbuf);
|
||||||
|
} else {
|
||||||
|
sm4_cbc_decrypt_blocks(&sm4_key, iv, block, 1, outbuf);
|
||||||
|
}
|
||||||
|
outlen = sizeof(block);
|
||||||
|
inpos = left;
|
||||||
|
block_nbytes = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fwrite(buf, 1, outlen, outfp) != outlen) {
|
nblocks = (inlen - inpos) / sizeof(block);
|
||||||
|
len = nblocks * sizeof(block);
|
||||||
|
if (len) {
|
||||||
|
if (enc) {
|
||||||
|
sm4_cbc_encrypt_blocks(&sm4_key, iv, buf + inpos, nblocks, outbuf + outlen);
|
||||||
|
} else {
|
||||||
|
sm4_cbc_decrypt_blocks(&sm4_key, iv, buf + inpos, nblocks, outbuf + outlen);
|
||||||
|
}
|
||||||
|
outlen += len;
|
||||||
|
inpos += len;
|
||||||
|
}
|
||||||
|
if (inlen > inpos) {
|
||||||
|
block_nbytes = inlen - inpos;
|
||||||
|
memcpy(block, buf + inpos, block_nbytes);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (fwrite(pkcs7_padding ? buf : outbuf, 1, outlen, outfp) != outlen) {
|
||||||
fprintf(stderr, "gmssl %s: output failure : %s\n", prog, strerror(errno));
|
fprintf(stderr, "gmssl %s: output failure : %s\n", prog, strerror(errno));
|
||||||
goto end;
|
goto end;
|
||||||
}
|
}
|
||||||
@@ -183,16 +239,22 @@ bad:
|
|||||||
goto end;
|
goto end;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (enc) {
|
if (pkcs7_padding && enc) {
|
||||||
if (sm4_cbc_encrypt_finish(&ctx, buf, &outlen) != 1) {
|
if (sm4_cbc_encrypt_finish(&ctx, buf, &outlen) != 1) {
|
||||||
error_print();
|
error_print();
|
||||||
goto end;
|
goto end;
|
||||||
}
|
}
|
||||||
} else {
|
} else if (pkcs7_padding) {
|
||||||
if (sm4_cbc_decrypt_finish(&ctx, buf, &outlen) != 1) {
|
if (sm4_cbc_decrypt_finish(&ctx, buf, &outlen) != 1) {
|
||||||
error_print();
|
error_print();
|
||||||
goto end;
|
goto end;
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
if (block_nbytes) {
|
||||||
|
fprintf(stderr, "gmssl %s: input length must be multiple of 16 bytes when PKCS#7 padding is not enabled\n", prog);
|
||||||
|
goto end;
|
||||||
|
}
|
||||||
|
outlen = 0;
|
||||||
}
|
}
|
||||||
if (fwrite(buf, 1, outlen, outfp) != outlen) {
|
if (fwrite(buf, 1, outlen, outfp) != outlen) {
|
||||||
fprintf(stderr, "gmssl %s: output failure : %s\n", prog, strerror(errno));
|
fprintf(stderr, "gmssl %s: output failure : %s\n", prog, strerror(errno));
|
||||||
@@ -204,7 +266,10 @@ bad:
|
|||||||
end:
|
end:
|
||||||
gmssl_secure_clear(key, sizeof(key));
|
gmssl_secure_clear(key, sizeof(key));
|
||||||
gmssl_secure_clear(iv, sizeof(iv));
|
gmssl_secure_clear(iv, sizeof(iv));
|
||||||
|
gmssl_secure_clear(&sm4_key, sizeof(sm4_key));
|
||||||
gmssl_secure_clear(&ctx, sizeof(ctx));
|
gmssl_secure_clear(&ctx, sizeof(ctx));
|
||||||
|
gmssl_secure_clear(outbuf, sizeof(outbuf));
|
||||||
|
gmssl_secure_clear(block, sizeof(block));
|
||||||
gmssl_secure_clear(buf, sizeof(buf));
|
gmssl_secure_clear(buf, sizeof(buf));
|
||||||
if (infile && infp) fclose(infp);
|
if (infile && infp) fclose(infp);
|
||||||
if (outfile && outfp) fclose(outfp);
|
if (outfile && outfp) fclose(outfp);
|
||||||
|
|||||||
@@ -20,11 +20,12 @@
|
|||||||
static const char *usage = "-key hex [-in file | -in_str str] [-bin|-hex] [-out file]";
|
static const char *usage = "-key hex [-in file | -in_str str] [-bin|-hex] [-out file]";
|
||||||
|
|
||||||
static const char *help =
|
static const char *help =
|
||||||
|
"\n"
|
||||||
"Options\n"
|
"Options\n"
|
||||||
"\n"
|
"\n"
|
||||||
" -key hex Hex string of the MAC key, 16 bytes\n"
|
" -key hex Hex string of the MAC key, 16 bytes\n"
|
||||||
" -in_str str Input as text string\n"
|
" -in_str str Input as text string, any byte length is accepted\n"
|
||||||
" -in file | stdin Input file path\n"
|
" -in file | stdin Input file path, any byte length is accepted\n"
|
||||||
" `-in_str` and `-in` should not be used together\n"
|
" `-in_str` and `-in` should not be used together\n"
|
||||||
" If neither `-in` nor `-in_str` specified, read from stdin\n"
|
" If neither `-in` nor `-in_str` specified, read from stdin\n"
|
||||||
" -hex Output MAC-tag as hex string (by default)\n"
|
" -hex Output MAC-tag as hex string (by default)\n"
|
||||||
@@ -34,20 +35,20 @@ static const char *help =
|
|||||||
"\n"
|
"\n"
|
||||||
"Examples\n"
|
"Examples\n"
|
||||||
"\n"
|
"\n"
|
||||||
" KEY_HEX=`gmssl rand -outlen 16 -hex`\n"
|
" KEY=`gmssl rand -outlen 16 -hex`\n"
|
||||||
" gmssl sm4_cbc_mac -key $KEY_HEX -in_str abc\n"
|
" gmssl sm4_cbc_mac -key $KEY -in_str abc\n"
|
||||||
"\n"
|
"\n"
|
||||||
" gmssl sm4_cbc_mac -key $KEY_HEX -in_str abc -bin\n"
|
" gmssl sm4_cbc_mac -key $KEY -in_str abc -bin\n"
|
||||||
"\n"
|
"\n"
|
||||||
" gmssl sm4_cbc_mac -key $KEY_HEX -in /path/to/file\n"
|
" gmssl sm4_cbc_mac -key $KEY -in /path/to/file\n"
|
||||||
"\n"
|
"\n"
|
||||||
" When reading from stdin, make sure the trailing newline character is removed\n"
|
" When reading from stdin, make sure the trailing newline character is removed\n"
|
||||||
"\n"
|
"\n"
|
||||||
" Linux/Mac:\n"
|
" Linux/Mac:\n"
|
||||||
" echo -n abc | gmssl sm4_cbc_mac -key $KEY_HEX\n"
|
" echo -n abc | gmssl sm4_cbc_mac -key $KEY\n"
|
||||||
"\n"
|
"\n"
|
||||||
" Windows:\n"
|
" Windows:\n"
|
||||||
" C:\\> echo |set/p=\"abc\" | gmssl sm4_cbc_mac -key 11223344556677881122334455667788\n"
|
" echo |set/p=\"abc\" | gmssl sm4_cbc_mac -key 11223344556677881122334455667788\n"
|
||||||
"\n";
|
"\n";
|
||||||
|
|
||||||
int sm4_cbc_mac_main(int argc, char **argv)
|
int sm4_cbc_mac_main(int argc, char **argv)
|
||||||
|
|||||||
@@ -21,6 +21,7 @@
|
|||||||
static const char *usage = "{-encrypt|-decrypt} -key hex -iv hex [-aad str| -aad_hex hex] [-taglen num] [-in file] [-out file]";
|
static const char *usage = "{-encrypt|-decrypt} -key hex -iv hex [-aad str| -aad_hex hex] [-taglen num] [-in file] [-out file]";
|
||||||
|
|
||||||
static const char *options =
|
static const char *options =
|
||||||
|
"\n"
|
||||||
"Options\n"
|
"Options\n"
|
||||||
"\n"
|
"\n"
|
||||||
" -encrypt Encrypt\n"
|
" -encrypt Encrypt\n"
|
||||||
@@ -30,17 +31,16 @@ static const char *options =
|
|||||||
" -aad str Authenticated-only message\n"
|
" -aad str Authenticated-only message\n"
|
||||||
" -aad_hex hex Authenticated-only data in HEX format\n"
|
" -aad_hex hex Authenticated-only data in HEX format\n"
|
||||||
" -taglen num MAC tag length, 4 to 16 bytes\n"
|
" -taglen num MAC tag length, 4 to 16 bytes\n"
|
||||||
" -in file | stdin Input data\n"
|
" -in file | stdin Input data. Encrypt input can be any byte length within CCM limits\n"
|
||||||
|
" Decrypt input is ciphertext || tag and must be at least taglen bytes\n"
|
||||||
" -out file | stdout Output data\n"
|
" -out file | stdout Output data\n"
|
||||||
"\n"
|
"\n"
|
||||||
"Examples\n"
|
"Examples\n"
|
||||||
"\n"
|
"\n"
|
||||||
" $ TEXT=`gmssl rand -outlen 20 -hex`\n"
|
" KEY=`gmssl rand -outlen 16 -hex`\n"
|
||||||
" $ KEY=`gmssl rand -outlen 16 -hex`\n"
|
" IV=`gmssl rand -outlen 12 -hex`\n"
|
||||||
" $ IV=`gmssl rand -outlen 12 -hex`\n"
|
" echo -n abc | gmssl sm4_ccm -encrypt -key $KEY -iv $IV -aad_hex 001122 -out sm4_ccm_ciphertext.bin\n"
|
||||||
" $ AAD=\"The AAD Data\"\n"
|
" gmssl sm4_ccm -decrypt -key $KEY -iv $IV -aad_hex 001122 -in sm4_ccm_ciphertext.bin\n"
|
||||||
" $ echo -n $TEXT | gmssl sm4_ccm -encrypt -key $KEY -iv $IV -aad $AAD -out sm4_ccm_ciphertext.bin\n"
|
|
||||||
" $ gmssl sm4_ccm -decrypt -key $KEY -iv $IV -aad $AAD -in sm4_ccm_ciphertext.bin\n"
|
|
||||||
"\n";
|
"\n";
|
||||||
|
|
||||||
static uint8_t *read_content(FILE *infp, size_t *outlen, const char *prog)
|
static uint8_t *read_content(FILE *infp, size_t *outlen, const char *prog)
|
||||||
|
|||||||
@@ -29,22 +29,21 @@ static const char *options =
|
|||||||
" -decrypt Decrypt\n"
|
" -decrypt Decrypt\n"
|
||||||
" -key hex Symmetric key in HEX format\n"
|
" -key hex Symmetric key in HEX format\n"
|
||||||
" -iv hex IV in HEX format\n"
|
" -iv hex IV in HEX format\n"
|
||||||
" -in file | stdin Input data\n"
|
" -in file | stdin Input data, any byte length is accepted\n"
|
||||||
" -out file | stdout Output data\n"
|
" -out file | stdout Output data\n"
|
||||||
"\n"
|
"\n"
|
||||||
"Examples\n"
|
"Examples\n"
|
||||||
"\n"
|
"\n"
|
||||||
" $ TEXT=`gmssl rand -outlen 20 -hex`\n"
|
" KEY=`gmssl rand -outlen 16 -hex`\n"
|
||||||
" $ KEY=`gmssl rand -outlen 16 -hex`\n"
|
" IV=`gmssl rand -outlen 16 -hex`\n"
|
||||||
" $ IV=`gmssl rand -outlen 16 -hex`\n"
|
" echo -n abc | gmssl sm4_cfb -encrypt -key $KEY -iv $IV -out sm4_cfb128_ciphertext.bin\n"
|
||||||
" $ echo -n $TEXT | gmssl sm4_cfb -encrypt -key $KEY -iv $IV -out sm4_cfb128_ciphertext.bin\n"
|
" gmssl sm4_cfb -decrypt -key $KEY -iv $IV -in sm4_cfb128_ciphertext.bin\n"
|
||||||
" $ gmssl sm4_cfb -decrypt -key $KEY -iv $IV -in sm4_cfb128_ciphertext.bin\n"
|
|
||||||
"\n"
|
"\n"
|
||||||
" $ echo -n $TEXT | gmssl sm4_cfb -sbytes 1 -encrypt -key $KEY -iv $IV -out sm4_cfb8_ciphertext.bin\n"
|
" echo -n abc | gmssl sm4_cfb -sbytes 1 -encrypt -key $KEY -iv $IV -out sm4_cfb8_ciphertext.bin\n"
|
||||||
" $ gmssl sm4_cfb -sbytes 1 -decrypt -key $KEY -iv $IV -in sm4_cfb8_ciphertext.bin\n"
|
" gmssl sm4_cfb -sbytes 1 -decrypt -key $KEY -iv $IV -in sm4_cfb8_ciphertext.bin\n"
|
||||||
"\n"
|
"\n"
|
||||||
" $ echo -n $TEXT | gmssl sm4_cfb -sbytes 8 -encrypt -key $KEY -iv $IV -out sm4_cfb64_ciphertext.bin\n"
|
" echo -n 01234567 | gmssl sm4_cfb -sbytes 8 -encrypt -key $KEY -iv $IV -out sm4_cfb64_ciphertext.bin\n"
|
||||||
" $ gmssl sm4_cfb -sbytes 8 -decrypt -key $KEY -iv $IV -in sm4_cfb64_ciphertext.bin\n"
|
" gmssl sm4_cfb -sbytes 8 -decrypt -key $KEY -iv $IV -in sm4_cfb64_ciphertext.bin\n"
|
||||||
"\n";
|
"\n";
|
||||||
|
|
||||||
int sm4_cfb_main(int argc, char **argv)
|
int sm4_cfb_main(int argc, char **argv)
|
||||||
|
|||||||
@@ -28,16 +28,15 @@ static const char *options =
|
|||||||
" -decrypt Decrypt\n"
|
" -decrypt Decrypt\n"
|
||||||
" -key hex Symmetric key in HEX format\n"
|
" -key hex Symmetric key in HEX format\n"
|
||||||
" -iv hex IV in HEX format\n"
|
" -iv hex IV in HEX format\n"
|
||||||
" -in file | stdin Input data\n"
|
" -in file | stdin Input data, any byte length is accepted\n"
|
||||||
" -out file | stdout Output data\n"
|
" -out file | stdout Output data\n"
|
||||||
"\n"
|
"\n"
|
||||||
"Examples\n"
|
"Examples\n"
|
||||||
"\n"
|
"\n"
|
||||||
" $ TEXT=`gmssl rand -outlen 20 -hex`\n"
|
" KEY=`gmssl rand -outlen 16 -hex`\n"
|
||||||
" $ KEY=`gmssl rand -outlen 16 -hex`\n"
|
" IV=`gmssl rand -outlen 16 -hex`\n"
|
||||||
" $ IV=`gmssl rand -outlen 16 -hex`\n"
|
" echo -n abc | gmssl sm4_ctr -encrypt -key $KEY -iv $IV -out sm4_ctr_ciphertext.bin\n"
|
||||||
" $ echo -n $TEXT | gmssl sm4_ctr -key $KEY -iv $IV -out sm4_ctr_ciphertext.bin\n"
|
" gmssl sm4_ctr -decrypt -key $KEY -iv $IV -in sm4_ctr_ciphertext.bin\n"
|
||||||
" $ gmssl sm4_ctr -key $KEY -iv $IV -in sm4_ctr_ciphertext.bin\n"
|
|
||||||
"\n";
|
"\n";
|
||||||
|
|
||||||
int sm4_ctr_main(int argc, char **argv)
|
int sm4_ctr_main(int argc, char **argv)
|
||||||
|
|||||||
@@ -27,15 +27,14 @@ static const char *options =
|
|||||||
" -encrypt Encrypt\n"
|
" -encrypt Encrypt\n"
|
||||||
" -decrypt Decrypt\n"
|
" -decrypt Decrypt\n"
|
||||||
" -key hex Symmetric key in HEX format\n"
|
" -key hex Symmetric key in HEX format\n"
|
||||||
" -in file | stdin Input data\n"
|
" -in file | stdin Input data, length must be a multiple of 16 bytes\n"
|
||||||
" -out file | stdout Output data\n"
|
" -out file | stdout Output data\n"
|
||||||
"\n"
|
"\n"
|
||||||
"Examples\n"
|
"Examples\n"
|
||||||
"\n"
|
"\n"
|
||||||
" $ TEXT=`gmssl rand -outlen 32 -hex`\n"
|
" KEY=`gmssl rand -outlen 16 -hex`\n"
|
||||||
" $ KEY=`gmssl rand -outlen 16 -hex`\n"
|
" echo -n 0123456789abcdef | gmssl sm4_ecb -encrypt -key $KEY -out sm4_ecb_ciphertext.bin\n"
|
||||||
" $ echo -n $TEXT | gmssl sm4_ecb -encrypt -key $KEY -out sm4_ecb_ciphertext.bin\n"
|
" gmssl sm4_ecb -decrypt -key $KEY -in sm4_ecb_ciphertext.bin\n"
|
||||||
" $ gmssl sm4_ecb -decrypt -key $KEY -in sm4_ecb_ciphertext.bin\n"
|
|
||||||
"\n";
|
"\n";
|
||||||
|
|
||||||
int sm4_ecb_main(int argc, char **argv)
|
int sm4_ecb_main(int argc, char **argv)
|
||||||
|
|||||||
@@ -34,9 +34,15 @@ static const char *options =
|
|||||||
" -decrypt Decrypt\n"
|
" -decrypt Decrypt\n"
|
||||||
" -key hex SM4 key in HEX format\n"
|
" -key hex SM4 key in HEX format\n"
|
||||||
" -tweak hex FF1 tweak in HEX format\n"
|
" -tweak hex FF1 tweak in HEX format\n"
|
||||||
" -digits digits Input digits, default from stdin\n"
|
" -digits digits Input digits, default from stdin. Length must be 8 to 18 digits\n"
|
||||||
" -idcard Input is a Chinese resident identity card number\n"
|
" -idcard Input is a Chinese resident identity card number, 18 characters\n"
|
||||||
" -bankcard Input is a bank card number with Luhn check digit\n"
|
" -bankcard Input is a bank card number with Luhn check digit, 9 to 19 digits\n"
|
||||||
|
"\n"
|
||||||
|
"Examples\n"
|
||||||
|
"\n"
|
||||||
|
" KEY=`gmssl rand -outlen 16 -hex`\n"
|
||||||
|
" CIPHERTEXT=`gmssl sm4_ff1 -encrypt -key $KEY -digits 0123456789012345`\n"
|
||||||
|
" gmssl sm4_ff1 -decrypt -key $KEY -digits $CIPHERTEXT\n"
|
||||||
"\n";
|
"\n";
|
||||||
|
|
||||||
static uint8_t *read_content(FILE *infp, size_t *outlen, const char *prog)
|
static uint8_t *read_content(FILE *infp, size_t *outlen, const char *prog)
|
||||||
|
|||||||
@@ -21,6 +21,7 @@
|
|||||||
static const char *usage = "{-encrypt|-decrypt} -key hex -iv hex [-aad str| -aad_hex hex] [-taglen num] [-in file] [-out file]";
|
static const char *usage = "{-encrypt|-decrypt} -key hex -iv hex [-aad str| -aad_hex hex] [-taglen num] [-in file] [-out file]";
|
||||||
|
|
||||||
static const char *options =
|
static const char *options =
|
||||||
|
"\n"
|
||||||
"Options\n"
|
"Options\n"
|
||||||
"\n"
|
"\n"
|
||||||
" -encrypt Encrypt\n"
|
" -encrypt Encrypt\n"
|
||||||
@@ -30,17 +31,16 @@ static const char *options =
|
|||||||
" -aad str Authenticated-only message\n"
|
" -aad str Authenticated-only message\n"
|
||||||
" -aad_hex hex Authenticated-only data in HEX format\n"
|
" -aad_hex hex Authenticated-only data in HEX format\n"
|
||||||
" -taglen num MAC tag length, default 16 bytes\n"
|
" -taglen num MAC tag length, default 16 bytes\n"
|
||||||
" -in file | stdin Input data\n"
|
" -in file | stdin Input data. Encrypt input can be any byte length\n"
|
||||||
|
" Decrypt input is ciphertext || tag and must be at least taglen bytes\n"
|
||||||
" -out file | stdout Output data\n"
|
" -out file | stdout Output data\n"
|
||||||
"\n"
|
"\n"
|
||||||
"Examples\n"
|
"Examples\n"
|
||||||
"\n"
|
"\n"
|
||||||
" $ TEXT=`gmssl rand -outlen 20 -hex`\n"
|
" KEY=`gmssl rand -outlen 16 -hex`\n"
|
||||||
" $ KEY=`gmssl rand -outlen 16 -hex`\n"
|
" IV=`gmssl rand -outlen 12 -hex`\n"
|
||||||
" $ IV=`gmssl rand -outlen 12 -hex`\n"
|
" echo -n abc | gmssl sm4_gcm -encrypt -key $KEY -iv $IV -aad_hex 001122 -out sm4_gcm_ciphertext.bin\n"
|
||||||
" $ AAD=\"The AAD Data\"\n"
|
" gmssl sm4_gcm -decrypt -key $KEY -iv $IV -aad_hex 001122 -in sm4_gcm_ciphertext.bin\n"
|
||||||
" $ echo -n $TEXT | gmssl sm4_gcm -encrypt -key $KEY -iv $IV -aad $AAD -out sm4_gcm_ciphertext.bin\n"
|
|
||||||
" $ gmssl sm4_gcm -decrypt -key $KEY -iv $IV -aad $AAD -in sm4_gcm_ciphertext.bin\n"
|
|
||||||
"\n";
|
"\n";
|
||||||
|
|
||||||
int sm4_gcm_main(int argc, char **argv)
|
int sm4_gcm_main(int argc, char **argv)
|
||||||
|
|||||||
@@ -28,16 +28,15 @@ static const char *options =
|
|||||||
" -decrypt Decrypt\n"
|
" -decrypt Decrypt\n"
|
||||||
" -key hex Symmetric key in HEX format\n"
|
" -key hex Symmetric key in HEX format\n"
|
||||||
" -iv hex IV in HEX format\n"
|
" -iv hex IV in HEX format\n"
|
||||||
" -in file | stdin Input data\n"
|
" -in file | stdin Input data, any byte length is accepted\n"
|
||||||
" -out file | stdout Output data\n"
|
" -out file | stdout Output data\n"
|
||||||
"\n"
|
"\n"
|
||||||
"Examples\n"
|
"Examples\n"
|
||||||
"\n"
|
"\n"
|
||||||
" $ TEXT=`gmssl rand -outlen 32 -hex`\n"
|
" KEY=`gmssl rand -outlen 16 -hex`\n"
|
||||||
" $ KEY=`gmssl rand -outlen 16 -hex`\n"
|
" IV=`gmssl rand -outlen 16 -hex`\n"
|
||||||
" $ IV=`gmssl rand -outlen 16 -hex`\n"
|
" echo -n abc | gmssl sm4_ofb -encrypt -key $KEY -iv $IV -out sm4_ofb_ciphertext.bin\n"
|
||||||
" $ echo -n $TEXT | gmssl sm4_ofb -encrypt -key $KEY -iv $IV -out sm4_ofb_ciphertext.bin\n"
|
" gmssl sm4_ofb -decrypt -key $KEY -iv $IV -in sm4_ofb_ciphertext.bin\n"
|
||||||
" $ gmssl sm4_ofb -decrypt -key $KEY -iv $IV -in sm4_ofb_ciphertext.bin\n"
|
|
||||||
"\n";
|
"\n";
|
||||||
|
|
||||||
int sm4_ofb_main(int argc, char **argv)
|
int sm4_ofb_main(int argc, char **argv)
|
||||||
|
|||||||
@@ -21,6 +21,7 @@
|
|||||||
static const char *usage = "{-encrypt|-decrypt} -key hex -iv hex -data_unit_size num [-in file] [-out file]";
|
static const char *usage = "{-encrypt|-decrypt} -key hex -iv hex -data_unit_size num [-in file] [-out file]";
|
||||||
|
|
||||||
static const char *options =
|
static const char *options =
|
||||||
|
"\n"
|
||||||
"Options\n"
|
"Options\n"
|
||||||
"\n"
|
"\n"
|
||||||
" -encrypt Encrypt\n"
|
" -encrypt Encrypt\n"
|
||||||
@@ -28,16 +29,17 @@ static const char *options =
|
|||||||
" -key hex Symmetric key in HEX format, 32 bytes\n"
|
" -key hex Symmetric key in HEX format, 32 bytes\n"
|
||||||
" -iv hex IV (tweak in XTS), 16 bytes\n"
|
" -iv hex IV (tweak in XTS), 16 bytes\n"
|
||||||
" -data_unit_size num Encrypted disk sector size, typically 512 or 4096 bytes\n"
|
" -data_unit_size num Encrypted disk sector size, typically 512 or 4096 bytes\n"
|
||||||
" -in file | stdin Input data\n"
|
" Input and output lengths must be multiples of this size\n"
|
||||||
|
" -in file | stdin Input data, length must be a multiple of data_unit_size\n"
|
||||||
" -out file | stdout Output data\n"
|
" -out file | stdout Output data\n"
|
||||||
"\n"
|
"\n"
|
||||||
"Examples\n"
|
"Examples\n"
|
||||||
"\n"
|
"\n"
|
||||||
" $ DATA=`gmssl rand -outlen 2048`\n"
|
" KEY=`gmssl rand -outlen 32 -hex`\n"
|
||||||
" $ KEY=`gmssl rand -outlen 32 -hex`\n"
|
" IV=`gmssl rand -outlen 16 -hex`\n"
|
||||||
" $ IV=`gmssl rand -outlen 16 -hex`\n"
|
" gmssl rand -outlen 512 -out sm4_xts_plaintext.bin\n"
|
||||||
" $ echo -n $DATA | gmssl sm4_xts -encrypt -key $KEY -iv $IV -data_unit_size 512 -out sm4_xts_ciphertext.bin\n"
|
" gmssl sm4_xts -encrypt -key $KEY -iv $IV -data_unit_size 512 -in sm4_xts_plaintext.bin -out sm4_xts_ciphertext.bin\n"
|
||||||
" $ gmssl sm4_xts -decrypt -key $KEY -iv $IV -data_unit_size 512 -in sm4_xts_ciphertext.bin\n"
|
" gmssl sm4_xts -decrypt -key $KEY -iv $IV -data_unit_size 512 -in sm4_xts_ciphertext.bin -out sm4_xts_decrypted.bin\n"
|
||||||
"\n";
|
"\n";
|
||||||
|
|
||||||
int sm4_xts_main(int argc, char **argv)
|
int sm4_xts_main(int argc, char **argv)
|
||||||
|
|||||||
@@ -38,8 +38,7 @@ static const char *help =
|
|||||||
" -server_name str Send server_name (SNI) request\n"
|
" -server_name str Send server_name (SNI) request\n"
|
||||||
" -trusted_ca_keys Send trusted_ca_keys request\n"
|
" -trusted_ca_keys Send trusted_ca_keys request\n"
|
||||||
" -renegotiation_info Send renegotiation_info extension\n"
|
" -renegotiation_info Send renegotiation_info extension\n"
|
||||||
" -renegotiation_info_scsv\n"
|
" -renegotiation_info_scsv Send TLS_EMPTY_RENEGOTIATION_INFO_SCSV\n"
|
||||||
" Send TLS_EMPTY_RENEGOTIATION_INFO_SCSV\n"
|
|
||||||
" -status_request Send status_request (OCSP Stapling) request\n"
|
" -status_request Send status_request (OCSP Stapling) request\n"
|
||||||
" -get path Send a HTTP GET request and read response until close or timeout\n"
|
" -get path Send a HTTP GET request and read response until close or timeout\n"
|
||||||
" -in file | stdin Send input data and read response until close or timeout\n"
|
" -in file | stdin Send input data and read response until close or timeout\n"
|
||||||
|
|||||||
@@ -21,7 +21,7 @@
|
|||||||
|
|
||||||
|
|
||||||
static const char *usage =
|
static const char *usage =
|
||||||
"-key hex -count num -bearer num -direction num [-in file|-in_hex hex] [-out file]";
|
"-key hex -count num -bearer num -direction num [-in file] [-out file]";
|
||||||
|
|
||||||
static const char *help =
|
static const char *help =
|
||||||
"Options\n"
|
"Options\n"
|
||||||
@@ -30,17 +30,14 @@ static const char *help =
|
|||||||
" -count num COUNT parameter, 32-bit integer, decimal or 0x-prefixed hex\n"
|
" -count num COUNT parameter, 32-bit integer, decimal or 0x-prefixed hex\n"
|
||||||
" -bearer num BEARER parameter, 5-bit integer in [0, 31]\n"
|
" -bearer num BEARER parameter, 5-bit integer in [0, 31]\n"
|
||||||
" -direction num DIRECTION parameter, 0 or 1\n"
|
" -direction num DIRECTION parameter, 0 or 1\n"
|
||||||
" -in_hex hex Input bytes in HEX format\n"
|
" -in file | stdin Input file path. If not specified, read from stdin\n"
|
||||||
" -in file | stdin Input file path\n"
|
|
||||||
" `-in_hex` and `-in` should not be used together\n"
|
|
||||||
" If neither `-in_hex` nor `-in` specified, read from stdin\n"
|
|
||||||
" -out file | stdout Output ciphertext bytes. If not specified, output to stdout\n"
|
" -out file | stdout Output ciphertext bytes. If not specified, output to stdout\n"
|
||||||
"\n"
|
"\n"
|
||||||
"Examples\n"
|
"Examples\n"
|
||||||
"\n"
|
"\n"
|
||||||
" gmssl zuc_128_eea3 -key 173d14ba5003731d7a60049470f00a29 \\\n"
|
" gmssl zuc_128_eea3 -key 173d14ba5003731d7a60049470f00a29 \\\n"
|
||||||
" -count 0x66035492 -bearer 15 -direction 0 \\\n"
|
" -count 0x66035492 -bearer 15 -direction 0 \\\n"
|
||||||
" -in_hex 6cf65340735552ab0c9752fa6f9025fe0bd675d9005875b2 -out ciphertext.bin\n"
|
" -in plaintext.bin -out ciphertext.bin\n"
|
||||||
"\n";
|
"\n";
|
||||||
|
|
||||||
static int parse_uint64(const char *s, uint64_t max, uint64_t *out)
|
static int parse_uint64(const char *s, uint64_t max, uint64_t *out)
|
||||||
@@ -117,7 +114,6 @@ int zuc_128_eea3_main(int argc, char **argv)
|
|||||||
int ret = 1;
|
int ret = 1;
|
||||||
char *prog = argv[0];
|
char *prog = argv[0];
|
||||||
char *keyhex = NULL;
|
char *keyhex = NULL;
|
||||||
char *inhex = NULL;
|
|
||||||
char *infile = NULL;
|
char *infile = NULL;
|
||||||
char *outfile = NULL;
|
char *outfile = NULL;
|
||||||
uint8_t key[ZUC_KEY_SIZE];
|
uint8_t key[ZUC_KEY_SIZE];
|
||||||
@@ -191,18 +187,7 @@ int zuc_128_eea3_main(int argc, char **argv)
|
|||||||
}
|
}
|
||||||
direction = (ZUC_BIT)v;
|
direction = (ZUC_BIT)v;
|
||||||
direction_set = 1;
|
direction_set = 1;
|
||||||
} else if (!strcmp(*argv, "-in_hex")) {
|
|
||||||
if (infile) {
|
|
||||||
fprintf(stderr, "gmssl %s: `-in` and `-in_hex` should not be used together\n", prog);
|
|
||||||
goto end;
|
|
||||||
}
|
|
||||||
if (--argc < 1) goto bad;
|
|
||||||
inhex = *(++argv);
|
|
||||||
} else if (!strcmp(*argv, "-in")) {
|
} else if (!strcmp(*argv, "-in")) {
|
||||||
if (inhex) {
|
|
||||||
fprintf(stderr, "gmssl %s: `-in` and `-in_hex` should not be used together\n", prog);
|
|
||||||
goto end;
|
|
||||||
}
|
|
||||||
if (--argc < 1) goto bad;
|
if (--argc < 1) goto bad;
|
||||||
infile = *(++argv);
|
infile = *(++argv);
|
||||||
if (!(infp = fopen(infile, "rb"))) {
|
if (!(infp = fopen(infile, "rb"))) {
|
||||||
@@ -237,21 +222,7 @@ bad:
|
|||||||
goto end;
|
goto end;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (inhex) {
|
if (!(in = read_content(infp, &inlen, prog))) {
|
||||||
if (strlen(inhex) % 2) {
|
|
||||||
fprintf(stderr, "gmssl %s: invalid input hex length\n", prog);
|
|
||||||
goto end;
|
|
||||||
}
|
|
||||||
nbytes = strlen(inhex) / 2;
|
|
||||||
if (!(in = (uint8_t *)malloc(nbytes ? nbytes : 1))) {
|
|
||||||
fprintf(stderr, "gmssl %s: malloc failure\n", prog);
|
|
||||||
goto end;
|
|
||||||
}
|
|
||||||
if (hex_to_bytes(inhex, strlen(inhex), in, &inlen) != 1) {
|
|
||||||
fprintf(stderr, "gmssl %s: invalid input hex digits\n", prog);
|
|
||||||
goto end;
|
|
||||||
}
|
|
||||||
} else if (!(in = read_content(infp, &inlen, prog))) {
|
|
||||||
goto end;
|
goto end;
|
||||||
}
|
}
|
||||||
nbytes = inlen;
|
nbytes = inlen;
|
||||||
|
|||||||
@@ -21,7 +21,7 @@
|
|||||||
|
|
||||||
|
|
||||||
static const char *usage =
|
static const char *usage =
|
||||||
"-key hex -count num -bearer num -direction num [-in file|-in_hex hex] [-hex|-bin] [-out file]";
|
"-key hex -count num -bearer num -direction num [-in file] [-hex|-bin] [-out file]";
|
||||||
|
|
||||||
static const char *help =
|
static const char *help =
|
||||||
"Options\n"
|
"Options\n"
|
||||||
@@ -30,10 +30,7 @@ static const char *help =
|
|||||||
" -count num COUNT parameter, 32-bit integer, decimal or 0x-prefixed hex\n"
|
" -count num COUNT parameter, 32-bit integer, decimal or 0x-prefixed hex\n"
|
||||||
" -bearer num BEARER parameter, 5-bit integer in [0, 31]\n"
|
" -bearer num BEARER parameter, 5-bit integer in [0, 31]\n"
|
||||||
" -direction num DIRECTION parameter, 0 or 1\n"
|
" -direction num DIRECTION parameter, 0 or 1\n"
|
||||||
" -in_hex hex Input message bytes in HEX format\n"
|
" -in file | stdin Input file path. If not specified, read from stdin\n"
|
||||||
" -in file | stdin Input file path\n"
|
|
||||||
" `-in_hex` and `-in` should not be used together\n"
|
|
||||||
" If neither `-in_hex` nor `-in` specified, read from stdin\n"
|
|
||||||
" -hex Output MAC as hex string (by default)\n"
|
" -hex Output MAC as hex string (by default)\n"
|
||||||
" -bin Output MAC as binary\n"
|
" -bin Output MAC as binary\n"
|
||||||
" `-hex` and `-bin` should not be used together\n"
|
" `-hex` and `-bin` should not be used together\n"
|
||||||
@@ -42,7 +39,7 @@ static const char *help =
|
|||||||
"Examples\n"
|
"Examples\n"
|
||||||
"\n"
|
"\n"
|
||||||
" gmssl zuc_128_eia3 -key 00000000000000000000000000000000 \\\n"
|
" gmssl zuc_128_eia3 -key 00000000000000000000000000000000 \\\n"
|
||||||
" -count 0 -bearer 0 -direction 0 -in_hex 00\n"
|
" -count 0 -bearer 0 -direction 0 -in message.bin\n"
|
||||||
"\n";
|
"\n";
|
||||||
|
|
||||||
static int parse_uint64(const char *s, uint64_t max, uint64_t *out)
|
static int parse_uint64(const char *s, uint64_t max, uint64_t *out)
|
||||||
@@ -119,7 +116,6 @@ int zuc_128_eia3_main(int argc, char **argv)
|
|||||||
int ret = 1;
|
int ret = 1;
|
||||||
char *prog = argv[0];
|
char *prog = argv[0];
|
||||||
char *keyhex = NULL;
|
char *keyhex = NULL;
|
||||||
char *inhex = NULL;
|
|
||||||
char *infile = NULL;
|
char *infile = NULL;
|
||||||
char *outfile = NULL;
|
char *outfile = NULL;
|
||||||
int outformat = 0;
|
int outformat = 0;
|
||||||
@@ -128,7 +124,6 @@ int zuc_128_eia3_main(int argc, char **argv)
|
|||||||
uint8_t *in = NULL;
|
uint8_t *in = NULL;
|
||||||
size_t inlen = 0;
|
size_t inlen = 0;
|
||||||
size_t nbits;
|
size_t nbits;
|
||||||
size_t nbytes = 0;
|
|
||||||
uint64_t v;
|
uint64_t v;
|
||||||
ZUC_UINT32 count = 0;
|
ZUC_UINT32 count = 0;
|
||||||
ZUC_UINT5 bearer = 0;
|
ZUC_UINT5 bearer = 0;
|
||||||
@@ -191,18 +186,7 @@ int zuc_128_eia3_main(int argc, char **argv)
|
|||||||
}
|
}
|
||||||
direction = (ZUC_BIT)v;
|
direction = (ZUC_BIT)v;
|
||||||
direction_set = 1;
|
direction_set = 1;
|
||||||
} else if (!strcmp(*argv, "-in_hex")) {
|
|
||||||
if (infile) {
|
|
||||||
fprintf(stderr, "gmssl %s: `-in` and `-in_hex` should not be used together\n", prog);
|
|
||||||
goto end;
|
|
||||||
}
|
|
||||||
if (--argc < 1) goto bad;
|
|
||||||
inhex = *(++argv);
|
|
||||||
} else if (!strcmp(*argv, "-in")) {
|
} else if (!strcmp(*argv, "-in")) {
|
||||||
if (inhex) {
|
|
||||||
fprintf(stderr, "gmssl %s: `-in` and `-in_hex` should not be used together\n", prog);
|
|
||||||
goto end;
|
|
||||||
}
|
|
||||||
if (--argc < 1) goto bad;
|
if (--argc < 1) goto bad;
|
||||||
infile = *(++argv);
|
infile = *(++argv);
|
||||||
if (!(infp = fopen(infile, "rb"))) {
|
if (!(infp = fopen(infile, "rb"))) {
|
||||||
@@ -249,24 +233,9 @@ bad:
|
|||||||
goto end;
|
goto end;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (inhex) {
|
if (!(in = read_content(infp, &inlen, prog))) {
|
||||||
if (strlen(inhex) % 2) {
|
|
||||||
fprintf(stderr, "gmssl %s: invalid input hex length\n", prog);
|
|
||||||
goto end;
|
goto end;
|
||||||
}
|
}
|
||||||
nbytes = strlen(inhex) / 2;
|
|
||||||
if (!(in = (uint8_t *)malloc(nbytes ? nbytes : 1))) {
|
|
||||||
fprintf(stderr, "gmssl %s: malloc failure\n", prog);
|
|
||||||
goto end;
|
|
||||||
}
|
|
||||||
if (hex_to_bytes(inhex, strlen(inhex), in, &inlen) != 1) {
|
|
||||||
fprintf(stderr, "gmssl %s: invalid input hex digits\n", prog);
|
|
||||||
goto end;
|
|
||||||
}
|
|
||||||
} else if (!(in = read_content(infp, &inlen, prog))) {
|
|
||||||
goto end;
|
|
||||||
}
|
|
||||||
nbytes = inlen;
|
|
||||||
nbits = inlen * 8;
|
nbits = inlen * 8;
|
||||||
|
|
||||||
macword = zuc_eia_generate_mac((ZUC_UINT32 *)in, nbits, key, count, bearer, direction);
|
macword = zuc_eia_generate_mac((ZUC_UINT32 *)in, nbits, key, count, bearer, direction);
|
||||||
|
|||||||
Reference in New Issue
Block a user