Fix kyber commands bugs

This commit is contained in:
Zhi Guan
2026-01-12 21:53:39 +08:00
parent 3afd4a047b
commit de57d7fce1
5 changed files with 19 additions and 14 deletions

View File

@@ -715,6 +715,7 @@ int kyber_cpa_private_key_to_bytes(const KYBER_CPA_KEY *key, uint8_t **out, size
} }
if (out && *out) { if (out && *out) {
memcpy(*out, key->s, sizeof(key->s)); memcpy(*out, key->s, sizeof(key->s));
*out += sizeof(key->s);
} }
*outlen += sizeof(key->s); *outlen += sizeof(key->s);
return 1; return 1;

View File

@@ -468,31 +468,34 @@ static int test_kyber_cpa(void)
static int test_kyber_kem(void) static int test_kyber_kem(void)
{ {
uint8_t random[32] = {0};
KYBER_KEY key; KYBER_KEY key;
KYBER_CIPHERTEXT c; KYBER_CIPHERTEXT c;
uint8_t K[32]; uint8_t K[32];
uint8_t K_[32]; uint8_t K_[32];
if (kyber_key_generate_ex(&key, NULL) != 1) { memset(&key, 0, sizeof(key));
if (kyber_key_generate_ex(&key, random) != 1) {
error_print(); error_print();
return -1; return -1;
} }
kyber_public_key_print(stderr, 0, 0, "pk", &key); kyber_public_key_print(stderr, 0, 4, "kyber_public_key", &key);
kyber_private_key_print(stderr, 0, 0, "sk", &key); kyber_private_key_print(stderr, 0, 4, "kyber_private_key", &key);
if (kyber_encap(&key, &c, K) != 1) { if (kyber_encap(&key, &c, K) != 1) {
error_print(); error_print();
return -1; return -1;
} }
kyber_ciphertext_print(stderr, 0, 0, "ciphertext", &c); kyber_ciphertext_print(stderr, 0, 4, "kyber_kem_ciphertext", &c);
format_bytes(stderr, 0, 0, "KEM_K", K, 32); format_bytes(stderr, 0, 4, "KEM_K", K, 32);
if (kyber_decap(&key, &c, K_) != 1) { if (kyber_decap(&key, &c, K_) != 1) {
error_print(); error_print();
return -1; return -1;
} }
format_bytes(stderr, 0, 0, "DEC_K", K_, 32); format_bytes(stderr, 0, 4, "DEC_K", K_, 32);
if (memcmp(K_, K, 32) != 0) { if (memcmp(K_, K, 32) != 0) {
error_print(); error_print();
@@ -645,10 +648,10 @@ int main(void)
if (test_kyber_poly_ntt() != 1) goto err; if (test_kyber_poly_ntt() != 1) goto err;
if (test_kyber_poly_ntt_mul() != 1) goto err; if (test_kyber_poly_ntt_mul() != 1) goto err;
if (test_kyber_cpa() != 1) goto err; if (test_kyber_cpa() != 1) goto err;
if (test_kyber_kem() != 1) goto err;
if (test_kyber_cpa_key_to_bytes() != 1) goto err; if (test_kyber_cpa_key_to_bytes() != 1) goto err;
if (test_kyber_key_to_bytes() != 1) goto err; if (test_kyber_key_to_bytes() != 1) goto err;
if (test_kyber_cpa_ciphertext_to_bytes() != 1) goto err; if (test_kyber_cpa_ciphertext_to_bytes() != 1) goto err;
if (test_kyber_kem() != 1) goto err;
printf("%s all tests passed\n", __FILE__); printf("%s all tests passed\n", __FILE__);
return 0; return 0;

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2014-2025 The GmSSL Project. All Rights Reserved. * Copyright 2014-2026 The GmSSL Project. All Rights Reserved.
* *
* Licensed under the Apache License, Version 2.0 (the License); you may * Licensed under the Apache License, Version 2.0 (the License); you may
* not use this file except in compliance with the License. * not use this file except in compliance with the License.
@@ -49,6 +49,8 @@ int kyberdecap_main(int argc, char **argv)
KYBER_CIPHERTEXT ciphertext; KYBER_CIPHERTEXT ciphertext;
init_zeta();
memset(&key, 0, sizeof(key)); memset(&key, 0, sizeof(key));
argc--; argc--;

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2014-2025 The GmSSL Project. All Rights Reserved. * Copyright 2014-2026 The GmSSL Project. All Rights Reserved.
* *
* Licensed under the Apache License, Version 2.0 (the License); you may * Licensed under the Apache License, Version 2.0 (the License); you may
* not use this file except in compliance with the License. * not use this file except in compliance with the License.
@@ -19,10 +19,6 @@
static const char *usage = "-pubkey file [-out file] -outkey file [-verbose]\n"; 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 = static const char *options =
"Options\n" "Options\n"
" -pubkey file Input public key file\n" " -pubkey file Input public key file\n"
@@ -52,6 +48,7 @@ int kyberencap_main(int argc, char **argv)
uint8_t outkey[32]; uint8_t outkey[32];
init_zeta();
argc--; argc--;
argv++; argv++;

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2014-2025 The GmSSL Project. All Rights Reserved. * Copyright 2014-2026 The GmSSL Project. All Rights Reserved.
* *
* Licensed under the Apache License, Version 2.0 (the License); you may * Licensed under the Apache License, Version 2.0 (the License); you may
* not use this file except in compliance with the License. * not use this file except in compliance with the License.
@@ -42,6 +42,8 @@ int kyberkeygen_main(int argc, char **argv)
uint8_t *ppubout = pubout; uint8_t *ppubout = pubout;
size_t outlen = 0, puboutlen = 0; size_t outlen = 0, puboutlen = 0;
init_zeta();
memset(&key, 0, sizeof(key)); memset(&key, 0, sizeof(key));
argc--; argc--;