Skip to content

Commit

Permalink
Add sm4_xts and ghash command
Browse files Browse the repository at this point in the history
  • Loading branch information
guanzhi committed May 15, 2024
1 parent 8fb8dd0 commit 7cc5b77
Show file tree
Hide file tree
Showing 8 changed files with 431 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ set(tools
tools/sm9decrypt.c
tools/zuc.c
tools/rand.c
tools/ghash.c
tools/pbkdf2.c
tools/certgen.c
tools/certparse.c
Expand Down Expand Up @@ -353,6 +354,7 @@ if (ENABLE_SM4_XTS)
message(STATUS "ENABLE_SM4_XTS is ON")
add_definitions(-DENABLE_SM4_XTS)
list(APPEND src src/sm4_xts.c)
list(APPEND tools tools/sm4_xts.c)
list(APPEND tests sm4_xts)
endif()

Expand Down
1 change: 1 addition & 0 deletions include/gmssl/ghash.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ extern "C" {
#define GHASH_SIZE (16)


// h = ENC_k(0^128)
void ghash(const uint8_t h[16], const uint8_t *aad, size_t aadlen,
const uint8_t *c, size_t clen, uint8_t out[16]);

Expand Down
2 changes: 0 additions & 2 deletions src/sm4_xts.c
Original file line number Diff line number Diff line change
Expand Up @@ -311,5 +311,3 @@ int sm4_xts_decrypt_finish(SM4_XTS_CTX *ctx, uint8_t *out, size_t *outlen)
*outlen = 0;
return 1;
}


190 changes: 190 additions & 0 deletions tools/ghash.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
/*
* Copyright 2014-2024 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 <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <gmssl/ghash.h>
#include <gmssl/hex.h>
#include <gmssl/error.h>


static const char *usage = "[-in_str str|-in file] [-hex|-bin] [-out file]";

static const char *help =
"Options\n"
"\n"
" -h hex H value of GHASH, a ciphertext block of encrypted zeros\n"
" -aad str Authenticated-only message\n"
" -aad_hex hex Authenticated-only data in HEX format\n"
" -in_str str To be hashed string\n"
" -in file | stdin To be hashed file path\n"
" `-in_str` and `-in` should not be used together\n"
" If neither `-in` nor `-in_str` specified, read from stdin\n"
" -hex Output hash value as hex string (by default)\n"
" -bin Output hash value as binary\n"
" -out file | stdout Output file path. If not specified, output to stdout\n"
"\n"
"Examples\n"
"\n"
" $ TEXT=`gmssl rand -outlen 20 -hex`\n"
" $ KEY=`gmssl rand -outlen 16 -hex`\n"
" $ AAD=\"The AAD Data\"\n"
" $ gmssl ghash -h $KEY -aad $AAD -in_str $TEXT\n"
"\n";


int ghash_main(int argc, char **argv)
{
int ret = 1;
char *prog = argv[0];
char *hhex = NULL;
uint8_t *aad = NULL;
uint8_t *aad_buf = NULL;
size_t aadlen = 0;
char *in_str = NULL;
char *infile = NULL;
int outformat = 0;
char *outfile = NULL;
uint8_t h[16];
size_t hlen;
FILE *infp = stdin;
FILE *outfp = stdout;
GHASH_CTX ghash_ctx;
uint8_t dgst[GHASH_SIZE];
int i;

argc--;
argv++;

while (argc > 0) {
if (!strcmp(*argv, "-help")) {
printf("usage: gmssl %s %s\n", prog, usage);
printf("%s\n", help);
ret = 0;
goto end;
} else if (!strcmp(*argv, "-h")) {
if (--argc < 1) goto bad;
hhex = *(++argv);
if (strlen(hhex) != sizeof(h) * 2) {
fprintf(stderr, "gmssl %s: invalid H value length\n", prog);
goto end;
}
if (hex_to_bytes(hhex, strlen(hhex), h, &hlen) != 1) {
fprintf(stderr, "gmssl %s: invalid H hex digits\n", prog);
goto end;
}
} else if (!strcmp(*argv, "-aad")) {
if (--argc < 1) goto bad;
if (aad) {
fprintf(stderr, "gmssl %s: `-aad` or `aad_hex` has been specified\n", prog);
goto bad;
}
aad = (uint8_t *)(*(++argv));
aadlen = strlen((char *)aad);
} else if (!strcmp(*argv, "-aad_hex")) {
if (--argc < 1) goto bad;
if (aad) {
fprintf(stderr, "gmssl %s: `-aad` or `aad_hex` has been specified\n", prog);
goto bad;
}
aad = (uint8_t *)(*(++argv));
if (!(aad_buf = malloc(strlen((char *)aad)/2 + 1))) {
fprintf(stderr, "gmssl %s: malloc failure\n", prog);
goto end;
}
if (hex_to_bytes((char *)aad, strlen((char *)aad), aad_buf, &aadlen) != 1) {
fprintf(stderr, "gmssl %s: `-aad_hex` invalid HEX format argument\n", prog);
goto end;
}
aad = aad_buf;
} else if (!strcmp(*argv, "-in_str")) {
if (infile) {
fprintf(stderr, "gmssl %s: `-in` and `-in_str` should not be used together\n", prog);
goto end;
}
if (--argc < 1) goto bad;
in_str = *(++argv);
} else if (!strcmp(*argv, "-in")) {
if (in_str) {
fprintf(stderr, "gmssl %s: `-in` and `-in_str` should not be used together\n", prog);
goto end;
}
if (--argc < 1) goto bad;
infile = *(++argv);
if (!(infp = fopen(infile, "rb"))) {
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, infile, strerror(errno));
goto end;
}
} else if (!strcmp(*argv, "-hex")) {
if (outformat > 0) {
fprintf(stderr, "gmssl %s: `-hex` and `-bin` should not be used together\n", prog);
goto end;
}
outformat = 1;
} else if (!strcmp(*argv, "-bin")) {
if (outformat > 0) {
fprintf(stderr, "gmssl %s: `-hex` and `-bin` should not be used together\n", prog);
goto end;
}
outformat = 2;
} else if (!strcmp(*argv, "-out")) {
if (--argc < 1) goto bad;
outfile = *(++argv);
if (!(outfp = fopen(outfile, "wb"))) {
fprintf(stderr, "gmssl %s: open '%s' failure : %s\n", prog, outfile, strerror(errno));
goto end;
}
} else {
fprintf(stderr, "gmssl %s: illegal option '%s'\n", prog, *argv);
goto end;
bad:
fprintf(stderr, "gmssl %s: '%s' option value missing\n", prog, *argv);
goto end;
}

argc--;
argv++;
}

ghash_init(&ghash_ctx, h, aad, aadlen);

if (in_str) {
ghash_update(&ghash_ctx, (uint8_t *)in_str, strlen(in_str));

} else {
uint8_t buf[4096];
size_t len;
while ((len = fread(buf, 1, sizeof(buf), infp)) > 0) {
ghash_update(&ghash_ctx, buf, len);
}
memset(buf, 0, sizeof(buf));
}
ghash_finish(&ghash_ctx, dgst);
memset(&ghash_ctx, 0, sizeof(ghash_ctx));

if (outformat > 1) {
if (fwrite(dgst, 1, sizeof(dgst), outfp) != sizeof(dgst)) {
fprintf(stderr, "gmssl %s: output failure : %s\n", prog, strerror(errno));
goto end;
}
} else {
for (i = 0; i < sizeof(dgst); i++) {
fprintf(outfp, "%02x", dgst[i]);
}
fprintf(outfp, "\n");
}
ret = 0;
end:
if (infile && infp) fclose(infp);
if (outfile && outfp) fclose(outfp);
return ret;
}
12 changes: 12 additions & 0 deletions tools/gmssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,12 @@ extern int sm4_cfb_main(int argc, char **argv);
extern int sm4_ofb_main(int argc, char **argv);
extern int sm4_ccm_main(int argc, char **argv);
extern int sm4_gcm_main(int argc, char **argv);
extern int sm4_xts_main(int argc, char **argv);
extern int sm4_cbc_sm3_hmac_main(int argc, char **argv);
extern int sm4_ctr_sm3_hmac_main(int argc, char **argv);
extern int sm4_cbc_mac_main(int argc, char **argv);
extern int zuc_main(int argc, char **argv);
extern int ghash_main(int argc, char **argv);
extern int sm9setup_main(int argc, char **argv);
extern int sm9keygen_main(int argc, char **argv);
extern int sm9sign_main(int argc, char **argv);
Expand Down Expand Up @@ -95,9 +97,11 @@ static const char *options =
" sm4_ofb Encrypt or decrypt with SM4 OFB\n"
" sm4_ccm Encrypt or decrypt with SM4 CCM\n"
" sm4_gcm Encrypt or decrypt with SM4 GCM\n"
" sm4_xts Encrypt or decrypt with SM4 XTS\n"
" sm4_cbc_sm3_hmac Encrypt or decrypt with SM4 CBC with SM3-HMAC\n"
" sm4_ctr_sm3_hmac Encrypt or decrypt with SM4 CTR with SM3-HMAC\n"
" sm4_cbc_mac Generate SM4 CBC-MAC\n"
" ghash Generate GHASH\n"
" zuc Encrypt or decrypt with ZUC\n"
" sm9setup Generate SM9 master secret\n"
" sm9keygen Generate SM9 private key\n"
Expand Down Expand Up @@ -224,12 +228,20 @@ int main(int argc, char **argv)
#endif
} else if (!strcmp(*argv, "sm4_gcm")) {
return sm4_gcm_main(argc, argv);
#if ENABLE_SM4_XTS
} else if (!strcmp(*argv, "sm4_xts")) {
return sm4_xts_main(argc, argv);
#endif
} else if (!strcmp(*argv, "sm4_cbc_sm3_hmac")) {
return sm4_cbc_sm3_hmac_main(argc, argv);
} else if (!strcmp(*argv, "sm4_ctr_sm3_hmac")) {
return sm4_ctr_sm3_hmac_main(argc, argv);
} else if (!strcmp(*argv, "ghash")) {
return ghash_main(argc, argv);
#if ENABLE_SM4_CBC_MAC
} else if (!strcmp(*argv, "sm4_cbc_mac")) {
return sm4_cbc_mac_main(argc, argv);
#endif
} else if (!strcmp(*argv, "zuc")) {
return zuc_main(argc, argv);
} else if (!strcmp(*argv, "sm9setup")) {
Expand Down
6 changes: 3 additions & 3 deletions tools/sm4_ccm.c
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ int sm4_ccm_main(int argc, char **argv)
if (enc) {
outlen = inlen + taglen;
if (!(outbuf = (uint8_t *)malloc(outlen))) {
fprintf(stderr, "%s: malloc failure\n", prog);
fprintf(stderr, "gmssl %s: malloc failure\n", prog);
goto end;
}
tag = outbuf + inlen;
Expand All @@ -268,14 +268,14 @@ int sm4_ccm_main(int argc, char **argv)

} else {
if (inlen < taglen) {
fprintf(stderr, "%s: input length (%zu bytes) shorter than tag length (%d bytes)\n",
fprintf(stderr, "gmssl %s: input length (%zu bytes) shorter than tag length (%d bytes)\n",
prog, inlen, taglen);
goto end;
}
outlen = inlen - taglen;
tag = inbuf + inlen - taglen;
if (!(outbuf = (uint8_t *)malloc(outlen))) {
fprintf(stderr, "%s: malloc failure\n", prog);
fprintf(stderr, "gmssl %s: malloc failure\n", prog);
goto end;
}
if (sm4_ccm_decrypt(&sm4_key, iv, ivlen, aad, aadlen, inbuf, inlen - taglen,
Expand Down
2 changes: 1 addition & 1 deletion tools/sm4_gcm.c
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ int sm4_gcm_main(int argc, char **argv)
if (--argc < 1) goto bad;
taglen = atoi(*(++argv));
if (taglen < SM4_GCM_MIN_TAG_SIZE || taglen > SM4_GCM_MAX_TAG_SIZE) {
fprintf(stderr, "%s: `-taglen` invalid integer argument\n", prog);
fprintf(stderr, "gmssl %s: `-taglen` invalid integer argument\n", prog);
goto end;
}
} else if (!strcmp(*argv, "-in")) {
Expand Down

0 comments on commit 7cc5b77

Please sign in to comment.