1
0
mirror of https://github.com/wolfSSL/wolfssl.git synced 2026-10-24 04:09:07 +03:00
Files
wolfssl/tests/api/test_mlkem.h
Tobias Frauenschläger 2a37b112b5 Let an ML-KEM key named by id encapsulate on a device
wc_MlKemKey_Init_Id and wc_MlKemKey_Init_Label name a key held by a
crypto callback device. They set no key flags, because the key material
stays on the device. But wc_MlKemKey_Encapsulate returned BAD_STATE_E
for a key without MLKEM_FLAG_PUB_SET before calling the device, so a key
named by id could never encapsulate, on any device. A PKCS#11 token key
reloaded by CKA_ID never reached Pkcs11MlKemEncapsulate, which would
find the token public object by id or label.

Skip that early check for a key named by id, label or device context,
and check MLKEM_FLAG_PUB_SET again after the device call, as
wc_MlKemKey_Decapsulate does for MLKEM_FLAG_PRIV_SET since 55084c9a50.
The device context counts because wolfHSM names a key cached on the HSM
only by devCtx, with no id. A device can then encapsulate with a key it
holds. When it declines, or there is no device, a key without a public
key still gets BAD_STATE_E before software draws randomness or reads
any key material. wc_MlKemKey_EncapsulateWithRandom does not call the
device, so its own check stays where it is.

Any other key without a public key must still fail before the device is
called. With no id, label or device context, Pkcs11MlKemEncapsulate
falls back to the first ML-KEM public key on the token, so a caller that
never loaded the peer's key would get a shared secret for the wrong
recipient. Decapsulation is left as 55084c9a50 made it: a key bound to
a device only by its devId still reaches the device, where the token's
own private key is the one the caller means.

test_wc_mlkem_encapsulate_id_key encapsulates with a key from
wc_MlKemKey_Init_Id through a callback device. It checks that the
device is called, that BAD_STATE_E comes back when the device declines,
that a key from wc_MlKemKey_Init without key material never reaches the
device, and that the same key does once it has a device context.
test_wc_mlkem_decapsulate_id_key now also checks that a key bound to
the device only by its devId reaches it.
2026-10-23 15:23:36 -07:00

71 lines
3.3 KiB
C

/* test_mlkem.h
*
* Copyright (C) 2006-2026 wolfSSL Inc.
*
* This file is part of wolfSSL.
*
* wolfSSL is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* wolfSSL is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
*/
#ifndef WOLFCRYPT_TEST_MLKEM_H
#define WOLFCRYPT_TEST_MLKEM_H
#include <tests/api/api_decl.h>
int test_wc_mlkem_make_key_kats(void);
int test_wc_MlKemKey_seed_service_indicator(void);
int test_wc_mlkem_encapsulate_kats(void);
int test_wc_mlkem_decapsulate_kats(void);
int test_wc_mlkem_decapsulate_pubonly_fails(void);
int test_wc_mlkem_decap_fo_reject(void);
int test_wc_mlkem_decode_privkey_bad_pubhash(void);
int test_wc_MlkemFeatureCoverage(void);
int test_wc_MlkemDecisionCoverage(void);
int test_wc_mlkem_init_id_decision(void);
int test_wc_mlkem_init_label_decision(void);
int test_wc_mlkem_encapsulate_pubkey_unset_decision(void);
int test_wc_mlkem_encode_key_len_decision(void);
int test_wc_mlkem_cb_free(void);
int test_wc_mlkem_cb_pending_rejected(void);
int test_wc_mlkem_decapsulate_id_key(void);
int test_wc_mlkem_encapsulate_id_key(void);
int test_wc_mlkem_decode_pubkey_refused_save(void);
int test_wc_mlkem_decode_reused_key(void);
int test_wc_mlkem_decode_alloc_fail(void);
#define TEST_MLKEM_DECLS \
TEST_DECL_GROUP("mlkem", test_wc_mlkem_make_key_kats), \
TEST_DECL_GROUP("mlkem", test_wc_MlKemKey_seed_service_indicator), \
TEST_DECL_GROUP("mlkem", test_wc_mlkem_encapsulate_kats), \
TEST_DECL_GROUP("mlkem", test_wc_mlkem_decapsulate_kats), \
TEST_DECL_GROUP("mlkem", test_wc_mlkem_decapsulate_pubonly_fails), \
TEST_DECL_GROUP("mlkem", test_wc_mlkem_decap_fo_reject), \
TEST_DECL_GROUP("mlkem", test_wc_mlkem_decode_privkey_bad_pubhash), \
TEST_DECL_GROUP("mlkem", test_wc_MlkemFeatureCoverage), \
TEST_DECL_GROUP("mlkem", test_wc_MlkemDecisionCoverage), \
TEST_DECL_GROUP("mlkem", test_wc_mlkem_init_id_decision), \
TEST_DECL_GROUP("mlkem", test_wc_mlkem_init_label_decision), \
TEST_DECL_GROUP("mlkem", test_wc_mlkem_encapsulate_pubkey_unset_decision), \
TEST_DECL_GROUP("mlkem", test_wc_mlkem_encode_key_len_decision), \
TEST_DECL_GROUP("mlkem", test_wc_mlkem_cb_free), \
TEST_DECL_GROUP("mlkem", test_wc_mlkem_cb_pending_rejected), \
TEST_DECL_GROUP("mlkem", test_wc_mlkem_decapsulate_id_key), \
TEST_DECL_GROUP("mlkem", test_wc_mlkem_encapsulate_id_key), \
TEST_DECL_GROUP("mlkem", test_wc_mlkem_decode_pubkey_refused_save), \
TEST_DECL_GROUP("mlkem", test_wc_mlkem_decode_reused_key), \
TEST_DECL_GROUP("mlkem", test_wc_mlkem_decode_alloc_fail)
#endif /* WOLFCRYPT_TEST_MLKEM_H */