ledger_device_sdk 1.36.1

Ledger device Rust SDK
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
//! ML-DSA (Module-Lattice Digital Signature Algorithm) support (FIPS 204).
//!
//! Provides safe Rust wrappers around the ML-DSA C implementation from the
//! Ledger C SDK (`lib_cxng`). Supports ML-DSA-44 and ML-DSA-65 parameter sets,
//! plus ML-DSA-87 when the `mldsa_87` Cargo feature is enabled.
//!
//! The `mldsa_optimization` feature enables an alternative implementation that
//! trades RAM for speed.
//!
//! # Memory considerations
//!
//! The underlying C routines use large stack-allocated workspaces. On the
//! memory-constrained Nano X (28 KB of SRAM), the default 8 KB heap leaves too
//! little stack: a key-generation, signing, or verification call can overflow
//! the stack into the heap and corrupt it. Apps enabling `mldsa` on Nano X must
//! therefore budget a smaller heap, for example by setting
//! `HEAP_SIZE="nanox: 2048"` (the per-target syntax only affects Nano X and
//! leaves the default heap on the other devices).
//!
//! This module is only available when the `mldsa` Cargo feature is enabled.

use ledger_secure_sdk_sys::*;

/// ML-DSA parameter set selector.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum MlDsaParam {
    /// ML-DSA-44 (NIST security level 2).
    MlDsa44,
    /// ML-DSA-65 (NIST security level 3).
    MlDsa65,
    /// ML-DSA-87 (NIST security level 5). Requires the `mldsa_87` feature.
    #[cfg(feature = "mldsa_87")]
    MlDsa87,
}

impl MlDsaParam {
    const fn as_c(self) -> MLDSA_param_t {
        match self {
            MlDsaParam::MlDsa44 => MLDSA_44,
            MlDsaParam::MlDsa65 => MLDSA_65,
            #[cfg(feature = "mldsa_87")]
            MlDsaParam::MlDsa87 => MLDSA_87,
        }
    }
}

/// Pre-hash algorithm selector for HashML-DSA (FIPS 204, Section 5.4).
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum MlDsaPrehash {
    Sha256,
    Sha512,
    Sha3_256,
    Sha3_512,
    Shake128,
    Shake256,
}

impl MlDsaPrehash {
    const fn as_c(self) -> MLDSA_prehash_t {
        match self {
            MlDsaPrehash::Sha256 => MLDSA_PREHASH_SHA256,
            MlDsaPrehash::Sha512 => MLDSA_PREHASH_SHA512,
            MlDsaPrehash::Sha3_256 => MLDSA_PREHASH_SHA3_256,
            MlDsaPrehash::Sha3_512 => MLDSA_PREHASH_SHA3_512,
            MlDsaPrehash::Shake128 => MLDSA_PREHASH_SHAKE128,
            MlDsaPrehash::Shake256 => MLDSA_PREHASH_SHAKE256,
        }
    }
}

/// ML-DSA error type.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum MlDsaError {
    InvalidParameter,
    InvalidParameterValue,
    InternalError,
}

impl From<u32> for MlDsaError {
    fn from(code: u32) -> Self {
        match code {
            CX_INVALID_PARAMETER => MlDsaError::InvalidParameter,
            CX_INVALID_PARAMETER_VALUE => MlDsaError::InvalidParameterValue,
            _ => MlDsaError::InternalError,
        }
    }
}

/// ML-DSA-44 public key size in bytes.
pub const MLDSA44_PK_LEN: usize = MLDSA44_PUBLICKEYBYTES as usize;
/// ML-DSA-44 secret key size in bytes.
pub const MLDSA44_SK_LEN: usize = MLDSA44_SECRETKEYBYTES as usize;
/// ML-DSA-44 signature size in bytes.
pub const MLDSA44_SIG_LEN: usize = MLDSA44_SIGBYTES as usize;

/// ML-DSA-65 public key size in bytes.
pub const MLDSA65_PK_LEN: usize = MLDSA65_PUBLICKEYBYTES as usize;
/// ML-DSA-65 secret key size in bytes.
pub const MLDSA65_SK_LEN: usize = MLDSA65_SECRETKEYBYTES as usize;
/// ML-DSA-65 signature size in bytes.
pub const MLDSA65_SIG_LEN: usize = MLDSA65_SIGBYTES as usize;

/// ML-DSA-87 public key size in bytes.
#[cfg(feature = "mldsa_87")]
pub const MLDSA87_PK_LEN: usize = MLDSA87_PUBLICKEYBYTES as usize;
/// ML-DSA-87 secret key size in bytes.
#[cfg(feature = "mldsa_87")]
pub const MLDSA87_SK_LEN: usize = MLDSA87_SECRETKEYBYTES as usize;
/// ML-DSA-87 signature size in bytes.
#[cfg(feature = "mldsa_87")]
pub const MLDSA87_SIG_LEN: usize = MLDSA87_SIGBYTES as usize;

/// Maximum context string length as per FIPS 204.
pub const MAX_CTX_LEN: usize = 255;

impl MlDsaParam {
    /// Returns the public key size in bytes for this parameter set.
    pub const fn pk_len(self) -> usize {
        match self {
            MlDsaParam::MlDsa44 => MLDSA44_PK_LEN,
            MlDsaParam::MlDsa65 => MLDSA65_PK_LEN,
            #[cfg(feature = "mldsa_87")]
            MlDsaParam::MlDsa87 => MLDSA87_PK_LEN,
        }
    }

    /// Returns the secret key size in bytes for this parameter set.
    pub const fn sk_len(self) -> usize {
        match self {
            MlDsaParam::MlDsa44 => MLDSA44_SK_LEN,
            MlDsaParam::MlDsa65 => MLDSA65_SK_LEN,
            #[cfg(feature = "mldsa_87")]
            MlDsaParam::MlDsa87 => MLDSA87_SK_LEN,
        }
    }

    /// Returns the signature size in bytes for this parameter set.
    pub const fn sig_len(self) -> usize {
        match self {
            MlDsaParam::MlDsa44 => MLDSA44_SIG_LEN,
            MlDsaParam::MlDsa65 => MLDSA65_SIG_LEN,
            #[cfg(feature = "mldsa_87")]
            MlDsaParam::MlDsa87 => MLDSA87_SIG_LEN,
        }
    }
}

/// Generates an ML-DSA key pair using internal randomness.
///
/// # Arguments
/// * `pk` - Output buffer for the public key (size must match `param.pk_len()`).
/// * `sk` - Output buffer for the secret key (size must match `param.sk_len()`).
/// * `param` - The ML-DSA parameter set to use.
pub fn keygen(pk: &mut [u8], sk: &mut [u8], param: MlDsaParam) -> Result<(), MlDsaError> {
    let err = unsafe {
        MLDSA_keygen(
            pk.as_mut_ptr(),
            pk.len(),
            sk.as_mut_ptr(),
            sk.len(),
            param.as_c(),
        )
    };
    if err != CX_OK {
        Err(err.into())
    } else {
        Ok(())
    }
}

/// Signs a message using ML-DSA.
///
/// # Arguments
/// * `sig` - Output buffer for the signature (size must match `param.sig_len()`).
/// * `msg` - The message to sign.
/// * `ctx` - Optional context string (at most [`MAX_CTX_LEN`] bytes, or empty slice).
/// * `sk` - The signer's secret key.
/// * `param` - The ML-DSA parameter set to use.
///
/// # Returns
/// The actual signature length on success.
pub fn sign(
    sig: &mut [u8],
    msg: &[u8],
    ctx: &[u8],
    sk: &[u8],
    param: MlDsaParam,
) -> Result<usize, MlDsaError> {
    if ctx.len() > MAX_CTX_LEN {
        return Err(MlDsaError::InvalidParameterValue);
    }
    let mut sig_actual_len: usize = 0;
    let ctx_ptr = if ctx.is_empty() {
        core::ptr::null()
    } else {
        ctx.as_ptr()
    };
    let err = unsafe {
        MLDSA_sign(
            sig.as_mut_ptr(),
            sig.len(),
            &mut sig_actual_len,
            msg.as_ptr(),
            msg.len(),
            ctx_ptr,
            ctx.len(),
            sk.as_ptr(),
            sk.len(),
            param.as_c(),
        )
    };
    if err != CX_OK {
        Err(err.into())
    } else {
        Ok(sig_actual_len)
    }
}

/// Verifies an ML-DSA signature.
///
/// # Arguments
/// * `sig` - The signature to verify.
/// * `msg` - The message that was signed.
/// * `ctx` - Optional context string (must match what was used during signing).
/// * `pk` - The signer's public key.
/// * `param` - The ML-DSA parameter set to use.
pub fn verify(
    sig: &[u8],
    msg: &[u8],
    ctx: &[u8],
    pk: &[u8],
    param: MlDsaParam,
) -> Result<(), MlDsaError> {
    if ctx.len() > MAX_CTX_LEN {
        return Err(MlDsaError::InvalidParameterValue);
    }
    let ctx_ptr = if ctx.is_empty() {
        core::ptr::null()
    } else {
        ctx.as_ptr()
    };
    let err = unsafe {
        MLDSA_verify(
            sig.as_ptr(),
            sig.len(),
            msg.as_ptr(),
            msg.len(),
            ctx_ptr,
            ctx.len(),
            pk.as_ptr(),
            pk.len(),
            param.as_c(),
        )
    };
    if err != CX_OK {
        Err(err.into())
    } else {
        Ok(())
    }
}

/// Signs a pre-hashed message using HashML-DSA (FIPS 204, Algorithm 4).
///
/// The caller must hash the message with the chosen algorithm before calling
/// this function.
///
/// # Arguments
/// * `sig` - Output buffer for the signature (size must match `param.sig_len()`).
/// * `ph` - The pre-hashed message digest.
/// * `ctx` - Optional context string (at most [`MAX_CTX_LEN`] bytes, or empty slice).
/// * `sk` - The signer's secret key.
/// * `prehash_alg` - The hash algorithm used to produce `ph`.
/// * `param` - The ML-DSA parameter set to use.
///
/// # Returns
/// The actual signature length on success.
pub fn sign_prehash(
    sig: &mut [u8],
    ph: &[u8],
    ctx: &[u8],
    sk: &[u8],
    prehash_alg: MlDsaPrehash,
    param: MlDsaParam,
) -> Result<usize, MlDsaError> {
    if ctx.len() > MAX_CTX_LEN {
        return Err(MlDsaError::InvalidParameterValue);
    }
    let mut sig_actual_len: usize = 0;
    let ctx_ptr = if ctx.is_empty() {
        core::ptr::null()
    } else {
        ctx.as_ptr()
    };
    let err = unsafe {
        MLDSA_sign_prehash(
            sig.as_mut_ptr(),
            sig.len(),
            &mut sig_actual_len,
            ph.as_ptr(),
            ph.len(),
            ctx_ptr,
            ctx.len(),
            sk.as_ptr(),
            sk.len(),
            prehash_alg.as_c(),
            param.as_c(),
        )
    };
    if err != CX_OK {
        Err(err.into())
    } else {
        Ok(sig_actual_len)
    }
}

/// Verifies a HashML-DSA pre-hash signature (FIPS 204, Algorithm 5).
///
/// The caller must hash the message with the chosen algorithm before calling
/// this function.
///
/// # Arguments
/// * `sig` - The signature to verify.
/// * `ph` - The pre-hashed message digest.
/// * `ctx` - Optional context string (must match what was used during signing).
/// * `pk` - The signer's public key.
/// * `prehash_alg` - The hash algorithm used to produce `ph`.
/// * `param` - The ML-DSA parameter set to use.
pub fn verify_prehash(
    sig: &[u8],
    ph: &[u8],
    ctx: &[u8],
    pk: &[u8],
    prehash_alg: MlDsaPrehash,
    param: MlDsaParam,
) -> Result<(), MlDsaError> {
    if ctx.len() > MAX_CTX_LEN {
        return Err(MlDsaError::InvalidParameterValue);
    }
    let ctx_ptr = if ctx.is_empty() {
        core::ptr::null()
    } else {
        ctx.as_ptr()
    };
    let err = unsafe {
        MLDSA_verify_prehash(
            sig.as_ptr(),
            sig.len(),
            ph.as_ptr(),
            ph.len(),
            ctx_ptr,
            ctx.len(),
            pk.as_ptr(),
            pk.len(),
            prehash_alg.as_c(),
            param.as_c(),
        )
    };
    if err != CX_OK {
        Err(err.into())
    } else {
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::assert_eq_err as assert_eq;
    use crate::testing::TestType;
    use testmacro::test_item as test;

    const TEST_MSG: &[u8] = b"Test message";

    #[test]
    fn test_mldsa44_sign_verify() {
        let mut pk = [0u8; MLDSA44_PK_LEN];
        let mut sk = [0u8; MLDSA44_SK_LEN];
        keygen(&mut pk, &mut sk, MlDsaParam::MlDsa44).unwrap();

        let mut sig = [0u8; MLDSA44_SIG_LEN];
        let sig_len = sign(&mut sig, TEST_MSG, &[], &sk, MlDsaParam::MlDsa44).unwrap();
        assert_eq!(sig_len, MLDSA44_SIG_LEN);

        verify(&sig[..sig_len], TEST_MSG, &[], &pk, MlDsaParam::MlDsa44).unwrap();
    }

    #[test]
    fn test_mldsa65_sign_verify() {
        let mut pk = [0u8; MLDSA65_PK_LEN];
        let mut sk = [0u8; MLDSA65_SK_LEN];
        keygen(&mut pk, &mut sk, MlDsaParam::MlDsa65).unwrap();

        let mut sig = [0u8; MLDSA65_SIG_LEN];
        let sig_len = sign(&mut sig, TEST_MSG, &[], &sk, MlDsaParam::MlDsa65).unwrap();
        assert_eq!(sig_len, MLDSA65_SIG_LEN);

        verify(&sig[..sig_len], TEST_MSG, &[], &pk, MlDsaParam::MlDsa65).unwrap();
    }

    #[test]
    fn test_mldsa44_sign_verify_with_context() {
        let mut pk = [0u8; MLDSA44_PK_LEN];
        let mut sk = [0u8; MLDSA44_SK_LEN];
        keygen(&mut pk, &mut sk, MlDsaParam::MlDsa44).unwrap();

        let ctx = b"test context";
        let mut sig = [0u8; MLDSA44_SIG_LEN];
        let sig_len = sign(&mut sig, TEST_MSG, ctx, &sk, MlDsaParam::MlDsa44).unwrap();

        verify(&sig[..sig_len], TEST_MSG, ctx, &pk, MlDsaParam::MlDsa44).unwrap();
    }
}