1
0
mirror of https://github.com/wolfSSL/wolfssl.git synced 2026-10-24 04:09:07 +03:00

Merge pull request #11765 from Arpan0995/mldsa-initid-setparams

ML-DSA: set the device key level in InitId/InitLabel only on success
This commit is contained in:
Tobias Frauenschläger
2026-10-16 10:06:08 +02:00
committed by GitHub
4 changed files with 53 additions and 10 deletions

View File

@@ -84,8 +84,10 @@ int wc_MlDsaKey_Delete(wc_MlDsaKey* key, wc_MlDsaKey** key_p);
idは鍵オブジェクトへコピーされます。呼び出し側はこの関数から戻った直後に自身のバッファを解放して構いません。
デバイス上に保持されている鍵のパラメータセットはここでは不明なため、鍵にはビルド時に有効化されている中で最もセキュリティレベルの高いパラメータセット(無効化されていなければWC_ML_DSA_87)が設定されます。別のパラメータセットを使用する場合はwc_MlDsaKey_SetParams()で選択してください。
\return 0 成功した場合に返されます。
\return BAD_FUNC_ARG keyがNULLの場合に返されます。
\return BAD_FUNC_ARG keyがNULLの場合、またはidがNULLでlenが0より大きい場合に返されます。
\return BUFFER_E lenが負の場合、またはMLDSA_MAX_ID_LENを超える場合に返されます。
\param [in,out] key 初期化するwc_MlDsaKeyへのポインタ。
@@ -108,6 +110,8 @@ int wc_MlDsaKey_InitId(wc_MlDsaKey* key, const unsigned char* id, int len,
ラベルの長さはXSTRLENで取得されるため、途中にNULバイトが含まれるとそこでラベルが終端します。
パラメータセットはwc_MlDsaKey_InitId()と同様に設定されます。
\return 0 成功した場合に返されます。
\return BAD_FUNC_ARG keyまたはlabelがNULLの場合に返されます。
\return BUFFER_E labelが空の場合、またはMLDSA_MAX_LABEL_LENより長い場合に返されます。

View File

@@ -104,6 +104,11 @@ int wc_MlDsaKey_Delete(wc_MlDsaKey* key, wc_MlDsaKey** key_p);
The id is copied into the key object; the caller may free its
buffer immediately after this call returns.
The key is given the highest parameter set compiled in
(WC_ML_DSA_87 unless it was disabled at build time), as the
parameter set of the key held on the device is not known here. Call
wc_MlDsaKey_SetParams() to select a different one.
\return 0 on success.
\return BAD_FUNC_ARG if key is NULL, or if id is NULL and len is
positive.
@@ -138,6 +143,8 @@ int wc_MlDsaKey_InitId(wc_MlDsaKey* key, const unsigned char* id, int len,
The label length is taken via XSTRLEN, so embedded NUL bytes
terminate the label.
The parameter set is chosen as in wc_MlDsaKey_InitId().
\return 0 on success.
\return BAD_FUNC_ARG if key or label is NULL.
\return BUFFER_E if label is empty or longer than

View File

@@ -31430,6 +31430,7 @@ int test_wc_MldsaDecisionCoverage2(void)
byte id[4];
byte oversizeId[MLDSA_MAX_ID_LEN + 1];
char oversizeLabel[MLDSA_MAX_LABEL_LEN + 2];
byte level = 0;
XMEMSET(&idKey, 0, sizeof(idKey));
XMEMSET(id, 'I', sizeof(id));
@@ -31453,9 +31454,11 @@ int test_wc_MldsaDecisionCoverage2(void)
/* InitId: id == NULL with len == 0 -> nothing to store, accepted. */
ExpectIntEQ(wc_MlDsaKey_InitId(&idKey, NULL, 0, NULL, INVALID_DEVID),
0);
/* InitId: valid, id != NULL && len != 0 -> copied in. */
/* InitId: valid, id != NULL && len != 0 -> copied in, and a
* parameter set is selected even when ML-DSA-87 is compiled out. */
ExpectIntEQ(wc_MlDsaKey_InitId(&idKey, id, (int)sizeof(id), NULL,
INVALID_DEVID), 0);
ExpectIntEQ(wc_MlDsaKey_GetParams(&idKey, &level), 0);
wc_MlDsaKey_Free(&idKey);
/* InitId: valid, len == 0 (independence of id!=NULL&&len!=0). */
XMEMSET(&idKey, 0, sizeof(idKey));
@@ -31478,7 +31481,21 @@ int test_wc_MldsaDecisionCoverage2(void)
/* InitLabel: valid. */
ExpectIntEQ(wc_MlDsaKey_InitLabel(&idKey, "label", NULL,
INVALID_DEVID), 0);
ExpectIntEQ(wc_MlDsaKey_GetParams(&idKey, &level), 0);
wc_MlDsaKey_Free(&idKey);
/* An argument error leaves the caller's object untouched: it is not
* initialized yet, so nothing in it may be freed or reset. */
XMEMSET(&idKey, 0x5A, sizeof(idKey));
ExpectIntEQ(wc_MlDsaKey_InitId(&idKey, NULL, (int)sizeof(id), NULL,
INVALID_DEVID), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
ExpectIntEQ(idKey.level, 0x5A);
ExpectIntEQ(wc_MlDsaKey_InitId(&idKey, id, -1, NULL, INVALID_DEVID),
WC_NO_ERR_TRACE(BUFFER_E));
ExpectIntEQ(idKey.level, 0x5A);
ExpectIntEQ(wc_MlDsaKey_InitLabel(&idKey, "", NULL, INVALID_DEVID),
WC_NO_ERR_TRACE(BUFFER_E));
ExpectIntEQ(idKey.level, 0x5A);
}
#endif /* WOLF_PRIVATE_KEY_ID */

View File

@@ -13752,10 +13752,20 @@ int wc_MlDsaKey_Init(wc_MlDsaKey* key, void* heap, int devId)
}
#ifdef WOLF_PRIVATE_KEY_ID
/* Level set on a key held by a device: the highest this build has, as the
* level of the device key is not known here. */
#ifndef WOLFSSL_NO_ML_DSA_87
#define MLDSA_DEV_KEY_LEVEL WC_ML_DSA_87
#elif !defined(WOLFSSL_NO_ML_DSA_65)
#define MLDSA_DEV_KEY_LEVEL WC_ML_DSA_65
#else
#define MLDSA_DEV_KEY_LEVEL WC_ML_DSA_44
#endif
/* Initialize the ML-DSA key and set the id of the device key.
*
* The security level is set to the maximum, WC_ML_DSA_87, as the level of the
* key held by the device is not known here.
* The security level is set to the highest one compiled in, as the level of
* the key held by the device is not known here.
*
* @param [out] key ML-DSA key.
* @param [in] id Identifier of key on device.
@@ -13786,16 +13796,19 @@ int wc_MlDsaKey_InitId(wc_MlDsaKey* key, const unsigned char* id, int len,
key->idLen = len;
}
/* Set the maximum level here */
wc_MlDsaKey_SetParams(key, WC_ML_DSA_87);
/* Set the highest level this build has. Only when the key was
* initialized: on an argument error the caller's object is untouched. */
if (ret == 0) {
ret = wc_MlDsaKey_SetParams(key, MLDSA_DEV_KEY_LEVEL);
}
return ret;
}
/* Initialize the ML-DSA key and set the label of the device key.
*
* The security level is set to the maximum, WC_ML_DSA_87, as the level of the
* key held by the device is not known here.
* The security level is set to the highest one compiled in, as the level of
* the key held by the device is not known here.
*
* @param [out] key ML-DSA key.
* @param [in] label Label of key on device. NUL terminated.
@@ -13829,8 +13842,10 @@ int wc_MlDsaKey_InitLabel(wc_MlDsaKey* key, const char* label, void* heap,
key->labelLen = labelLen;
}
/* Set the maximum level here */
wc_MlDsaKey_SetParams(key, WC_ML_DSA_87);
/* Set the highest level this build has; see wc_MlDsaKey_InitId. */
if (ret == 0) {
ret = wc_MlDsaKey_SetParams(key, MLDSA_DEV_KEY_LEVEL);
}
return ret;
}