mirror of
https://github.com/guanzhi/GmSSL.git
synced 2026-05-06 16:36:16 +08:00
Update Kyber
This commit is contained in:
@@ -77,6 +77,11 @@ extern int xmsskeygen_main(int argc, char **argv);
|
||||
extern int xmsssign_main(int argc, char **argv);
|
||||
extern int xmssverify_main(int argc, char **argv);
|
||||
#endif
|
||||
#ifdef ENABLE_KYBER
|
||||
extern int kyberkeygen_main(int argc, char **argv);
|
||||
extern int kyberencap_main(int argc, char **argv);
|
||||
extern int kyberdecap_main(int argc, char **argv);
|
||||
#endif
|
||||
#ifdef ENABLE_SDF
|
||||
extern int sdfinfo_main(int argc, char **argv);
|
||||
extern int sdfdigest_main(int argc, char **argv);
|
||||
@@ -154,6 +159,11 @@ static const char *options =
|
||||
" xmsssign Generate XMSS-SM3 signature\n"
|
||||
" xmssverify Verify XMSS-SM3 signature\n"
|
||||
#endif
|
||||
#ifdef ENABLE_KYBER
|
||||
" kyberkeygen Generate Kyber keypair\n"
|
||||
" kyberencap Kyber KEM encap\n"
|
||||
" kyberdecap Kyber KEM decap\n"
|
||||
#endif
|
||||
#ifdef ENABLE_SDF
|
||||
" sdfinfo Print SDF device info\n"
|
||||
" sdfdigest Generate SM3 hash with SDF device\n"
|
||||
@@ -330,6 +340,14 @@ int main(int argc, char **argv)
|
||||
} else if (!strcmp(*argv, "xmssverify")) {
|
||||
return xmssverify_main(argc, argv);
|
||||
#endif
|
||||
#ifdef ENABLE_KYBER
|
||||
} else if (!strcmp(*argv, "kyberkeygen")) {
|
||||
return kyberkeygen_main(argc, argv);
|
||||
} else if (!strcmp(*argv, "kyberencap")) {
|
||||
return kyberencap_main(argc, argv);
|
||||
} else if (!strcmp(*argv, "kyberdecap")) {
|
||||
return kyberdecap_main(argc, argv);
|
||||
#endif
|
||||
#ifdef ENABLE_SDF
|
||||
} else if (!strcmp(*argv, "sdfinfo")) {
|
||||
return sdfinfo_main(argc, argv);
|
||||
|
||||
171
tools/kyberdecap.c
Normal file
171
tools/kyberdecap.c
Normal file
@@ -0,0 +1,171 @@
|
||||
/*
|
||||
* Copyright 2014-2025 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/mem.h>
|
||||
#include <gmssl/error.h>
|
||||
#include <gmssl/kyber.h>
|
||||
|
||||
|
||||
static const char *usage = "-key file [-in file] [-out file] [-verbose]\n";
|
||||
|
||||
static const char *options =
|
||||
"Options\n"
|
||||
" -key file Input private key file\n"
|
||||
" -in file Input ciphertext (encapsulated secret)\n"
|
||||
" -out file Output decapsulated secret\n"
|
||||
" -verbose Print public key and ciphertext\n"
|
||||
"\n";
|
||||
|
||||
int kyberdecap_main(int argc, char **argv)
|
||||
{
|
||||
int ret = 1;
|
||||
char *prog = argv[0];
|
||||
char *keyfile = NULL;
|
||||
char *infile = NULL;
|
||||
char *outfile = NULL;
|
||||
int verbose = 0;
|
||||
FILE *keyfp = NULL;
|
||||
FILE *infp = stdin;
|
||||
FILE *outfp = stdout;
|
||||
uint8_t keybuf[KYBER_PRIVATE_KEY_SIZE];
|
||||
size_t keylen = KYBER_PRIVATE_KEY_SIZE;
|
||||
const uint8_t *cp = keybuf;
|
||||
uint8_t *p = keybuf;
|
||||
KYBER_PRIVATE_KEY key;
|
||||
|
||||
uint8_t inbuf[sizeof(KYBER_CIPHERTEXT)];
|
||||
uint8_t outbuf[32];
|
||||
|
||||
KYBER_CIPHERTEXT ciphertext;
|
||||
|
||||
memset(&key, 0, sizeof(key));
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
|
||||
if (argc < 1) {
|
||||
fprintf(stderr, "usage: gmssl %s %s\n", prog, usage);
|
||||
return 1;
|
||||
}
|
||||
|
||||
while (argc > 0) {
|
||||
if (!strcmp(*argv, "-help")) {
|
||||
printf("usage: %s %s\n", prog, usage);
|
||||
printf("%s\n", options);
|
||||
ret = 0;
|
||||
goto end;
|
||||
} else if (!strcmp(*argv, "-key")) {
|
||||
if (--argc < 1) goto bad;
|
||||
keyfile = *(++argv);
|
||||
if (!(keyfp = fopen(keyfile, "rb+"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure: %s\n", prog, keyfile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-in")) {
|
||||
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, "-out")) {
|
||||
if (--argc < 1) goto bad;
|
||||
outfile = *(++argv);
|
||||
if (!(outfp = fopen(outfile, "wb"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure: %s\n", prog, outfile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-verbose")) {
|
||||
verbose = 1;
|
||||
} else {
|
||||
fprintf(stderr, "%s: illegal option '%s'\n", prog, *argv);
|
||||
goto end;
|
||||
bad:
|
||||
fprintf(stderr, "%s: `%s` option value missing\n", prog, *argv);
|
||||
goto end;
|
||||
}
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
}
|
||||
|
||||
if (!keyfile) {
|
||||
fprintf(stderr, "%s: `-key` option required\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (fread(keybuf, 1, keylen, keyfp) != keylen) {
|
||||
fprintf(stderr, "%s: read private key failure\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (kyber_private_key_from_bytes(&key, &cp, &keylen) != 1) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
if (keylen) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (verbose) {
|
||||
kyber_private_key_print(stderr, 0, 0, "kyber_private_key", &key);
|
||||
}
|
||||
|
||||
|
||||
size_t inlen = sizeof(inbuf);
|
||||
|
||||
if (fread(inbuf, 1, inlen, infp) != inlen) {
|
||||
fprintf(stderr, "%s: read ciphertext failure\n", prog);
|
||||
goto end;
|
||||
}
|
||||
cp = inbuf;
|
||||
if (kyber_ciphertext_from_bytes(&ciphertext, &cp, &inlen) != 1) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
if (inlen) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
if (verbose) {
|
||||
kyber_ciphertext_print(stderr, 0, 0, "kyber_ciphertext" ,&ciphertext);
|
||||
}
|
||||
|
||||
|
||||
|
||||
if (kyber_decap(&key, &ciphertext, outbuf) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
if (verbose) {
|
||||
format_bytes(stderr, 0, 0, "key", outbuf, 32);
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
ret = 0;
|
||||
|
||||
end:
|
||||
//kyber_key_cleanup(&key);
|
||||
gmssl_secure_clear(keybuf, sizeof(keybuf));
|
||||
if (keyfp) fclose(keyfp);
|
||||
if (infp && infp != stdin) fclose(infp);
|
||||
if (outfp && outfp != stdout) fclose(outfp);
|
||||
return ret;
|
||||
}
|
||||
165
tools/kyberencap.c
Normal file
165
tools/kyberencap.c
Normal file
@@ -0,0 +1,165 @@
|
||||
/*
|
||||
* Copyright 2014-2025 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/mem.h>
|
||||
#include <gmssl/error.h>
|
||||
#include <gmssl/kyber.h>
|
||||
|
||||
|
||||
static const char *usage = "-pubkey file [-out file] -outkey file [-verbose]\n";
|
||||
|
||||
|
||||
// decap 中的out一定是secret,而in 一定是ciphertext
|
||||
// encap 中的out 是decap的in,因此encap中的out是ciphertext,而输出的secret是特殊的
|
||||
|
||||
static const char *options =
|
||||
"Options\n"
|
||||
" -pubkey file Input public key file\n"
|
||||
" -out file Output ciphertext (decapsulated secret)\n"
|
||||
" -outkey file Output secret\n"
|
||||
" -verbose Print public key and ciphertext\n"
|
||||
"\n";
|
||||
|
||||
int kyberencap_main(int argc, char **argv)
|
||||
{
|
||||
int ret = 1;
|
||||
char *prog = argv[0];
|
||||
char *pubkeyfile = NULL;
|
||||
char *outfile = NULL;
|
||||
char *outkeyfile = NULL;
|
||||
int verbose = 0;
|
||||
FILE *pubkeyfp = NULL;
|
||||
FILE *outfp = stdout;
|
||||
FILE *outkeyfp = NULL;
|
||||
uint8_t pubkeybuf[KYBER_PUBLIC_KEY_SIZE];
|
||||
size_t pubkeylen = KYBER_PUBLIC_KEY_SIZE;
|
||||
const uint8_t *cp = pubkeybuf;
|
||||
uint8_t outbuf[sizeof(KYBER_CIPHERTEXT)];
|
||||
size_t outlen;
|
||||
KYBER_PRIVATE_KEY key;
|
||||
KYBER_CIPHERTEXT ciphertext;
|
||||
|
||||
uint8_t outkey[32];
|
||||
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
|
||||
if (argc < 1) {
|
||||
fprintf(stderr, "usage: gmssl %s %s\n", prog, usage);
|
||||
return 1;
|
||||
}
|
||||
|
||||
while (argc > 0) {
|
||||
if (!strcmp(*argv, "-help")) {
|
||||
printf("usage: %s %s\n", prog, usage);
|
||||
printf("%s\n", options);
|
||||
ret = 0;
|
||||
goto end;
|
||||
} else if (!strcmp(*argv, "-pubkey")) {
|
||||
if (--argc < 1) goto bad;
|
||||
pubkeyfile = *(++argv);
|
||||
if (!(pubkeyfp = fopen(pubkeyfile, "rb"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure: %s\n", prog, pubkeyfile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-out")) {
|
||||
if (--argc < 1) goto bad;
|
||||
outfile = *(++argv);
|
||||
if (!(outfp = fopen(outfile, "wb"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure: %s\n", prog, outfile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-outkey")) {
|
||||
if (--argc < 1) goto bad;
|
||||
outkeyfile = *(++argv);
|
||||
if (!(outkeyfp = fopen(outkeyfile, "wb"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure: %s\n", prog, outkeyfile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-verbose")) {
|
||||
verbose = 1;
|
||||
} else {
|
||||
fprintf(stderr, "%s: illegal option '%s'\n", prog, *argv);
|
||||
goto end;
|
||||
bad:
|
||||
fprintf(stderr, "%s: `%s` option value missing\n", prog, *argv);
|
||||
goto end;
|
||||
}
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
}
|
||||
|
||||
if (!pubkeyfile) {
|
||||
fprintf(stderr, "%s: `-key` option required\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (!outkeyfile) {
|
||||
fprintf(stderr, "%s: `-outkey` option required\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (fread(pubkeybuf, 1, pubkeylen, pubkeyfp) != pubkeylen) {
|
||||
fprintf(stderr, "%s: read public key failure\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (kyber_public_key_from_bytes(&key, &cp, &pubkeylen) != 1) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
if (verbose) {
|
||||
kyber_public_key_print(stderr, 0, 0, "kyber_public_key", &key);
|
||||
}
|
||||
|
||||
|
||||
if (kyber_encap(&key.pk, &ciphertext, outkey) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (verbose) {
|
||||
|
||||
kyber_ciphertext_print(stderr, 0, 0, "kyber_ciphertext", &ciphertext);
|
||||
|
||||
|
||||
format_bytes(stderr, 0, 0, "key", outkey, 32);
|
||||
|
||||
}
|
||||
|
||||
|
||||
uint8_t *p = outbuf;
|
||||
outlen = 0;
|
||||
if (kyber_ciphertext_to_bytes(&ciphertext, &p, &outlen) != 1) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
if (fwrite(outbuf, 1, outlen, outfp) != outlen) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (fwrite(outkey, 1, 32, outkeyfp) != 32) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
|
||||
ret = 0;
|
||||
|
||||
end:
|
||||
if (pubkeyfp) fclose(pubkeyfp);
|
||||
if (outfp && outfp != stdin) fclose(outfp);
|
||||
if (outkeyfp) fclose(outkeyfp);
|
||||
return ret;
|
||||
}
|
||||
129
tools/kyberkeygen.c
Normal file
129
tools/kyberkeygen.c
Normal file
@@ -0,0 +1,129 @@
|
||||
/*
|
||||
* Copyright 2014-2025 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/mem.h>
|
||||
#include <gmssl/error.h>
|
||||
#include <gmssl/kyber.h>
|
||||
|
||||
|
||||
static const char *usage = "-out file [-pubout file] [-verbose]\n";
|
||||
|
||||
static const char *options =
|
||||
"Options\n"
|
||||
" -out file Output private key\n"
|
||||
" -pubout file Output public key\n"
|
||||
" -verbose Print public key\n"
|
||||
"\n";
|
||||
|
||||
int kyberkeygen_main(int argc, char **argv)
|
||||
{
|
||||
int ret = 1;
|
||||
char *prog = argv[0];
|
||||
char *outfile = NULL;
|
||||
char *puboutfile = NULL;
|
||||
int verbose = 0;
|
||||
FILE *outfp = NULL;
|
||||
FILE *puboutfp = stdout;
|
||||
KYBER_PRIVATE_KEY key;
|
||||
uint8_t out[sizeof(KYBER_PRIVATE_KEY)];
|
||||
uint8_t pubout[sizeof(KYBER_PUBLIC_KEY)];
|
||||
uint8_t *pout = out;
|
||||
uint8_t *ppubout = pubout;
|
||||
size_t outlen = 0, puboutlen = 0;
|
||||
|
||||
memset(&key, 0, sizeof(key));
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
|
||||
if (argc < 1) {
|
||||
fprintf(stderr, "usage: gmssl %s %s\n", prog, usage);
|
||||
return 1;
|
||||
}
|
||||
|
||||
while (argc > 0) {
|
||||
if (!strcmp(*argv, "-help")) {
|
||||
printf("usage: gmssl %s %s\n", prog, usage);
|
||||
printf("%s\n", options);
|
||||
ret = 0;
|
||||
goto end;
|
||||
} else if (!strcmp(*argv, "-out")) {
|
||||
if (--argc < 1) goto bad;
|
||||
outfile = *(++argv);
|
||||
if (!(outfp = fopen(outfile, "wb"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, outfile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-pubout")) {
|
||||
if (--argc < 1) goto bad;
|
||||
puboutfile = *(++argv);
|
||||
if (!(puboutfp = fopen(puboutfile, "wb"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, outfile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-verbose")) {
|
||||
verbose = 1;
|
||||
} else {
|
||||
fprintf(stderr, "%s: illegal option '%s'\n", prog, *argv);
|
||||
goto end;
|
||||
bad:
|
||||
fprintf(stderr, "%s: `%s` option value missing\n", prog, *argv);
|
||||
goto end;
|
||||
}
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
}
|
||||
|
||||
if (!outfp) {
|
||||
fprintf(stderr, "%s: `-out` option required\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (kyber_key_generate(&key) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (verbose) {
|
||||
kyber_public_key_print(stderr, 0, 0, "kyber_public_key", &key);
|
||||
|
||||
kyber_private_key_print(stderr, 0, 0, "kyber_private_key", &key);
|
||||
}
|
||||
|
||||
if (kyber_private_key_to_bytes(&key, &pout, &outlen) != 1) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
if (fwrite(out, 1, outlen, outfp) != outlen) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (kyber_public_key_to_bytes(&key, &ppubout, &puboutlen) != 1) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
if (fwrite(pubout, 1, puboutlen, puboutfp) != puboutlen) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
|
||||
ret = 0;
|
||||
end:
|
||||
kyber_key_cleanup(&key);
|
||||
gmssl_secure_clear(out, outlen);
|
||||
if (outfile && outfp) fclose(outfp);
|
||||
if (puboutfile && puboutfp) fclose(puboutfp);
|
||||
return ret;
|
||||
}
|
||||
Reference in New Issue
Block a user