Skip to content

Commit de57d7f

Browse files
committed
Fix kyber commands bugs
1 parent 3afd4a0 commit de57d7f

File tree

5 files changed

+19
-14
lines changed

5 files changed

+19
-14
lines changed

src/kyber.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -715,6 +715,7 @@ int kyber_cpa_private_key_to_bytes(const KYBER_CPA_KEY *key, uint8_t **out, size
715715
}
716716
if (out && *out) {
717717
memcpy(*out, key->s, sizeof(key->s));
718+
*out += sizeof(key->s);
718719
}
719720
*outlen += sizeof(key->s);
720721
return 1;

tests/kybertest.c

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -468,31 +468,34 @@ static int test_kyber_cpa(void)
468468

469469
static int test_kyber_kem(void)
470470
{
471+
uint8_t random[32] = {0};
471472
KYBER_KEY key;
472473
KYBER_CIPHERTEXT c;
473474
uint8_t K[32];
474475
uint8_t K_[32];
475476

476-
if (kyber_key_generate_ex(&key, NULL) != 1) {
477+
memset(&key, 0, sizeof(key));
478+
479+
if (kyber_key_generate_ex(&key, random) != 1) {
477480
error_print();
478481
return -1;
479482
}
480483

481-
kyber_public_key_print(stderr, 0, 0, "pk", &key);
482-
kyber_private_key_print(stderr, 0, 0, "sk", &key);
484+
kyber_public_key_print(stderr, 0, 4, "kyber_public_key", &key);
485+
kyber_private_key_print(stderr, 0, 4, "kyber_private_key", &key);
483486

484487
if (kyber_encap(&key, &c, K) != 1) {
485488
error_print();
486489
return -1;
487490
}
488-
kyber_ciphertext_print(stderr, 0, 0, "ciphertext", &c);
489-
format_bytes(stderr, 0, 0, "KEM_K", K, 32);
491+
kyber_ciphertext_print(stderr, 0, 4, "kyber_kem_ciphertext", &c);
492+
format_bytes(stderr, 0, 4, "KEM_K", K, 32);
490493

491494
if (kyber_decap(&key, &c, K_) != 1) {
492495
error_print();
493496
return -1;
494497
}
495-
format_bytes(stderr, 0, 0, "DEC_K", K_, 32);
498+
format_bytes(stderr, 0, 4, "DEC_K", K_, 32);
496499

497500
if (memcmp(K_, K, 32) != 0) {
498501
error_print();
@@ -645,10 +648,10 @@ int main(void)
645648
if (test_kyber_poly_ntt() != 1) goto err;
646649
if (test_kyber_poly_ntt_mul() != 1) goto err;
647650
if (test_kyber_cpa() != 1) goto err;
648-
if (test_kyber_kem() != 1) goto err;
649651
if (test_kyber_cpa_key_to_bytes() != 1) goto err;
650652
if (test_kyber_key_to_bytes() != 1) goto err;
651653
if (test_kyber_cpa_ciphertext_to_bytes() != 1) goto err;
654+
if (test_kyber_kem() != 1) goto err;
652655

653656
printf("%s all tests passed\n", __FILE__);
654657
return 0;

tools/kyberdecap.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2025 The GmSSL Project. All Rights Reserved.
2+
* Copyright 2014-2026 The GmSSL Project. All Rights Reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the License); you may
55
* not use this file except in compliance with the License.
@@ -49,6 +49,8 @@ int kyberdecap_main(int argc, char **argv)
4949

5050
KYBER_CIPHERTEXT ciphertext;
5151

52+
init_zeta();
53+
5254
memset(&key, 0, sizeof(key));
5355

5456
argc--;

tools/kyberencap.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2025 The GmSSL Project. All Rights Reserved.
2+
* Copyright 2014-2026 The GmSSL Project. All Rights Reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the License); you may
55
* not use this file except in compliance with the License.
@@ -19,10 +19,6 @@
1919

2020
static const char *usage = "-pubkey file [-out file] -outkey file [-verbose]\n";
2121

22-
23-
// decap 中的out一定是secret,而in 一定是ciphertext
24-
// encap 中的out 是decap的in,因此encap中的out是ciphertext,而输出的secret是特殊的
25-
2622
static const char *options =
2723
"Options\n"
2824
" -pubkey file Input public key file\n"
@@ -52,6 +48,7 @@ int kyberencap_main(int argc, char **argv)
5248

5349
uint8_t outkey[32];
5450

51+
init_zeta();
5552

5653
argc--;
5754
argv++;

tools/kyberkeygen.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2025 The GmSSL Project. All Rights Reserved.
2+
* Copyright 2014-2026 The GmSSL Project. All Rights Reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the License); you may
55
* not use this file except in compliance with the License.
@@ -42,6 +42,8 @@ int kyberkeygen_main(int argc, char **argv)
4242
uint8_t *ppubout = pubout;
4343
size_t outlen = 0, puboutlen = 0;
4444

45+
init_zeta();
46+
4547
memset(&key, 0, sizeof(key));
4648

4749
argc--;

0 commit comments

Comments
 (0)