noxtls-psa 0.1.3

Internal implementation crate for noxtls: PSA Crypto adapter and provider abstractions.
Documentation
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
// Copyright (c) 2019-2026, Argenox Technologies LLC
// All rights reserved.
//
// SPDX-License-Identifier: GPL-2.0-only OR LicenseRef-Argenox-Commercial-License
//
// This file is part of the NoxTLS Library.
//
// This program 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; version 2 of the License.
//
// Alternatively, this file may be used under the terms of a commercial
// license from Argenox Technologies LLC.
//
// See `noxtls/LICENSE` and `noxtls/LICENSE.md` in this repository for full details.
// CONTACT: info@argenox.com

#[cfg(feature = "alloc")]
use alloc::{collections::BTreeMap, vec::Vec};
use noxtls_core::{Error, Result};
use noxtls_crypto::{
    aes_gcm_encrypt, p256_ecdh_shared_secret, p256_ecdsa_sign_sha256, rsaes_oaep_sha256_decrypt,
    rsaes_pkcs1_v15_decrypt, rsassa_pss_sha256_sign, rsassa_sha256_sign, sha256, x25519, AesCipher,
    P256PrivateKey, P256PublicKey, RsaPrivateKey,
};

/// Identifies a backend-managed external key object.
#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct PsaExternalKeyHandle {
    id: Vec<u8>,
}

impl PsaExternalKeyHandle {
    /// Constructs an external key handle from opaque identifier bytes.
    ///
    /// # Arguments
    ///
    /// * `id` - Opaque provider-owned key identifier bytes.
    ///
    /// # Returns
    ///
    /// A new [`PsaExternalKeyHandle`] owning the identifier bytes.
    pub fn new(id: Vec<u8>) -> Self {
        Self { id }
    }

    /// Borrows the opaque key identifier bytes.
    ///
    /// # Arguments
    ///
    /// * `self` - Handle whose stable identifier bytes are required.
    ///
    /// # Returns
    ///
    /// Borrowed opaque bytes identifying the provider key object.
    pub fn as_bytes(&self) -> &[u8] {
        &self.id
    }
}

/// Enumerates signing algorithms supported by PSA provider mappings.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum PsaSignAlgorithm {
    /// RSA PKCS#1 v1.5 signing with SHA-256 digest.
    RsaPkcs1Sha256,
    /// RSA PSS signing with SHA-256 digest.
    RsaPssSha256,
    /// ECDSA signing over P-256 with SHA-256 digest.
    EcdsaP256Sha256,
}

/// Enumerates decrypt algorithms supported by PSA provider mappings.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum PsaDecryptAlgorithm {
    /// RSA PKCS#1 v1.5 decryption.
    RsaPkcs1v15,
    /// RSA OAEP decryption using SHA-256.
    RsaOaepSha256,
}

/// Enumerates derive algorithms supported by PSA provider mappings.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum PsaDeriveAlgorithm {
    /// X25519 shared secret derivation.
    X25519,
    /// P-256 ECDH shared secret derivation.
    EcdhP256,
}

/// Carries data required for backend sign operations.
#[derive(Clone, Debug)]
pub struct KeySignRequest<'a> {
    /// Opaque provider handle to signing key object.
    pub handle: &'a PsaExternalKeyHandle,
    /// Algorithm selector for sign operation.
    pub algorithm: PsaSignAlgorithm,
    /// Input message bytes to be signed.
    pub message: &'a [u8],
    /// Optional PSS salt bytes for algorithms requiring explicit salt.
    pub salt: Option<&'a [u8]>,
}

/// Carries data required for backend decrypt operations.
#[derive(Clone, Debug)]
pub struct KeyDecryptRequest<'a> {
    /// Opaque provider handle to decrypt key object.
    pub handle: &'a PsaExternalKeyHandle,
    /// Algorithm selector for decrypt operation.
    pub algorithm: PsaDecryptAlgorithm,
    /// Ciphertext bytes to decrypt.
    pub ciphertext: &'a [u8],
    /// Optional OAEP label bytes for algorithms requiring explicit label.
    pub label: Option<&'a [u8]>,
}

/// Carries data required for backend derive operations.
#[derive(Clone, Debug)]
pub struct KeyDeriveRequest<'a> {
    /// Opaque provider handle to derive key object.
    pub handle: &'a PsaExternalKeyHandle,
    /// Algorithm selector for derive operation.
    pub algorithm: PsaDeriveAlgorithm,
    /// Peer public-key bytes in algorithm-expected encoding.
    pub peer_public_key: &'a [u8],
}

/// Carries AES-GCM encrypt operation inputs.
#[derive(Clone, Debug)]
pub struct AeadEncryptRequest<'a> {
    /// Symmetric encryption key bytes.
    pub key: &'a [u8],
    /// Nonce bytes for AES-GCM operation.
    pub nonce: &'a [u8],
    /// Associated authenticated data bytes.
    pub aad: &'a [u8],
    /// Plaintext bytes to encrypt.
    pub plaintext: &'a [u8],
}

/// Carries AES-GCM encryption outputs.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct AeadEncryptResponse {
    /// Produced ciphertext bytes.
    pub ciphertext: Vec<u8>,
    /// Produced 16-byte authentication tag.
    pub tag: [u8; 16],
}

/// Defines backend operations required by PSA provider surface.
pub trait PsaCryptoBackend {
    /// Executes an asymmetric signing operation.
    ///
    /// # Arguments
    ///
    /// * `self` - Backend implementation receiving sign request.
    /// * `request` - Sign request containing handle, algorithm, and digest.
    ///
    /// # Returns
    ///
    /// Signature bytes generated by the backend.
    ///
    /// # Errors
    ///
    /// Returns [`Error`] when handle resolution, policy checks, or crypto operation fails.
    fn sign(&self, request: &KeySignRequest<'_>) -> Result<Vec<u8>>;

    /// Executes an asymmetric decrypt operation.
    ///
    /// # Arguments
    ///
    /// * `self` - Backend implementation receiving decrypt request.
    /// * `request` - Decrypt request containing handle, algorithm, and ciphertext.
    ///
    /// # Returns
    ///
    /// Plaintext bytes on successful decrypt.
    ///
    /// # Errors
    ///
    /// Returns [`Error`] when handle resolution, policy checks, or crypto operation fails.
    fn decrypt(&self, request: &KeyDecryptRequest<'_>) -> Result<Vec<u8>>;

    /// Executes a key-agreement derive operation.
    ///
    /// # Arguments
    ///
    /// * `self` - Backend implementation receiving derive request.
    /// * `request` - Derive request containing handle, algorithm, and peer key.
    ///
    /// # Returns
    ///
    /// Shared secret bytes for the selected derive algorithm.
    ///
    /// # Errors
    ///
    /// Returns [`Error`] when handle resolution, policy checks, or derive operation fails.
    fn derive(&self, request: &KeyDeriveRequest<'_>) -> Result<Vec<u8>>;

    /// Fills output bytes with random data.
    ///
    /// # Arguments
    ///
    /// * `self` - Backend implementation receiving random request.
    /// * `out` - Mutable output buffer for random bytes.
    ///
    /// # Returns
    ///
    /// `Ok(())` when the output buffer is fully written.
    ///
    /// # Errors
    ///
    /// Returns [`Error`] when backend cannot provide entropy.
    fn random(&self, out: &mut [u8]) -> Result<()>;

    /// Computes a SHA-256 digest.
    ///
    /// # Arguments
    ///
    /// * `self` - Backend implementation receiving hash request.
    /// * `input` - Bytes to hash.
    ///
    /// # Returns
    ///
    /// A 32-byte SHA-256 digest.
    ///
    /// # Errors
    ///
    /// Returns [`Error`] when hashing fails for the backend.
    fn sha256(&self, input: &[u8]) -> Result<[u8; 32]>;

    /// Encrypts plaintext with AES-GCM.
    ///
    /// # Arguments
    ///
    /// * `self` - Backend implementation receiving AEAD request.
    /// * `request` - AES-GCM request with key, nonce, AAD, and plaintext.
    ///
    /// # Returns
    ///
    /// Ciphertext bytes plus a 16-byte authentication tag.
    ///
    /// # Errors
    ///
    /// Returns [`Error`] when backend lacks AES-GCM support or encryption fails.
    fn aes_gcm_encrypt(&self, request: &AeadEncryptRequest<'_>) -> Result<AeadEncryptResponse>;
}

/// Adapts a concrete backend into a stable PSA provider API.
#[derive(Clone, Debug)]
pub struct PsaProvider<B> {
    backend: B,
}

impl<B> PsaProvider<B> {
    /// Constructs a provider from a concrete backend implementation.
    ///
    /// # Arguments
    ///
    /// * `backend` - Backend implementation used by provider operations.
    ///
    /// # Returns
    ///
    /// A new [`PsaProvider`] owning the backend.
    pub fn new(backend: B) -> Self {
        Self { backend }
    }
}

impl<B: PsaCryptoBackend> PsaProvider<B> {
    /// Dispatches signing requests to the configured backend.
    ///
    /// # Arguments
    ///
    /// * `self` - Provider dispatching to backend.
    /// * `request` - Signing request to execute.
    ///
    /// # Returns
    ///
    /// Signature bytes produced by backend.
    ///
    /// # Errors
    ///
    /// Returns backend-provided [`Error`] on failures.
    pub fn sign(&self, request: &KeySignRequest<'_>) -> Result<Vec<u8>> {
        self.backend.sign(request)
    }

    /// Dispatches decrypt requests with uniform decrypt failure posture.
    ///
    /// # Arguments
    ///
    /// * `self` - Provider dispatching to backend.
    /// * `request` - Decrypt request to execute.
    ///
    /// # Returns
    ///
    /// Plaintext bytes returned by backend.
    ///
    /// # Errors
    ///
    /// Returns [`Error::CryptoFailure`] for decrypt failures to avoid oracle leakage.
    pub fn decrypt(&self, request: &KeyDecryptRequest<'_>) -> Result<Vec<u8>> {
        self.backend
            .decrypt(request)
            .map_err(|_| Error::CryptoFailure("psa cryptographic operation failed"))
    }

    /// Dispatches derive requests to the configured backend.
    ///
    /// # Arguments
    ///
    /// * `self` - Provider dispatching to backend.
    /// * `request` - Derive request to execute.
    ///
    /// # Returns
    ///
    /// Shared secret bytes produced by backend.
    ///
    /// # Errors
    ///
    /// Returns backend-provided [`Error`] on failures.
    pub fn derive(&self, request: &KeyDeriveRequest<'_>) -> Result<Vec<u8>> {
        self.backend.derive(request)
    }

    /// Fills output bytes with backend-provided random data.
    ///
    /// # Arguments
    ///
    /// * `self` - Provider dispatching to backend.
    /// * `out` - Mutable output buffer to fill.
    ///
    /// # Returns
    ///
    /// `Ok(())` when random output was written to the caller buffer.
    ///
    /// # Errors
    ///
    /// Returns backend-provided [`Error`] when random generation fails.
    pub fn random(&self, out: &mut [u8]) -> Result<()> {
        self.backend.random(out)
    }

    /// Computes SHA-256 using backend digest implementation.
    ///
    /// # Arguments
    ///
    /// * `self` - Provider dispatching to backend.
    /// * `input` - Bytes to hash.
    ///
    /// # Returns
    ///
    /// 32-byte SHA-256 digest bytes.
    ///
    /// # Errors
    ///
    /// Returns backend-provided [`Error`] on hashing failures.
    pub fn sha256(&self, input: &[u8]) -> Result<[u8; 32]> {
        self.backend.sha256(input)
    }

    /// Encrypts plaintext with backend AES-GCM implementation.
    ///
    /// # Arguments
    ///
    /// * `self` - Provider dispatching to backend.
    /// * `request` - AES-GCM request with key/nonce/aad/plaintext.
    ///
    /// # Returns
    ///
    /// Ciphertext bytes plus a 16-byte authentication tag.
    ///
    /// # Errors
    ///
    /// Returns backend-provided [`Error`] when encryption fails.
    pub fn aes_gcm_encrypt(&self, request: &AeadEncryptRequest<'_>) -> Result<AeadEncryptResponse> {
        self.backend.aes_gcm_encrypt(request)
    }
}

/// Stores policy flags for registered key handles.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
struct HandlePolicy {
    allow_sign: bool,
    allow_decrypt: bool,
    allow_derive: bool,
}

/// Represents the private key material variants used for software-backed tests.
#[derive(Clone, Debug)]
enum SoftwarePrivateMaterial {
    Rsa(RsaPrivateKey),
    X25519([u8; 32]),
    P256(P256PrivateKey),
}

/// Implements an in-process backend that mirrors PSA handle semantics for tests.
#[derive(Clone, Debug, Default)]
pub struct PsaSoftwareBackend {
    keys: BTreeMap<Vec<u8>, (SoftwarePrivateMaterial, HandlePolicy)>,
}

impl PsaSoftwareBackend {
    /// Constructs an empty software backend.
    ///
    /// # Arguments
    ///
    /// * `()` - This constructor has no parameters.
    ///
    /// # Returns
    ///
    /// A new empty [`PsaSoftwareBackend`] value.
    pub fn new() -> Self {
        Self {
            keys: BTreeMap::new(),
        }
    }

    /// Registers an RSA private key with handle-level usage policy.
    ///
    /// # Arguments
    ///
    /// * `handle` - Opaque key handle used for future operations.
    /// * `key` - RSA private key material owned by backend.
    /// * `allow_sign` - Whether sign operations are authorized for this handle.
    /// * `allow_decrypt` - Whether decrypt operations are authorized for this handle.
    ///
    /// # Returns
    ///
    /// `Ok(())` after key registration succeeds.
    ///
    /// # Errors
    ///
    /// Returns [`Error::PolicyViolation`] if the handle is already registered.
    pub fn register_rsa_key(
        &mut self,
        handle: PsaExternalKeyHandle,
        key: RsaPrivateKey,
        allow_sign: bool,
        allow_decrypt: bool,
    ) -> Result<()> {
        self.insert_key(
            handle,
            SoftwarePrivateMaterial::Rsa(key),
            HandlePolicy {
                allow_sign,
                allow_decrypt,
                allow_derive: false,
            },
        )
    }

    /// Registers an X25519 private key with derive-policy controls.
    ///
    /// # Arguments
    ///
    /// * `handle` - Opaque key handle used for future derive operations.
    /// * `key` - X25519 private scalar bytes.
    /// * `allow_derive` - Whether derive operations are authorized for this handle.
    ///
    /// # Returns
    ///
    /// `Ok(())` after key registration succeeds.
    ///
    /// # Errors
    ///
    /// Returns [`Error::PolicyViolation`] if the handle is already registered.
    pub fn register_x25519_key(
        &mut self,
        handle: PsaExternalKeyHandle,
        key: [u8; 32],
        allow_derive: bool,
    ) -> Result<()> {
        self.insert_key(
            handle,
            SoftwarePrivateMaterial::X25519(key),
            HandlePolicy {
                allow_sign: false,
                allow_decrypt: false,
                allow_derive,
            },
        )
    }

    /// Registers a P-256 private key with derive/sign policy controls.
    ///
    /// # Arguments
    ///
    /// * `handle` - Opaque key handle used for future operations.
    /// * `key` - P-256 private key material.
    /// * `allow_sign` - Whether sign operations are authorized for this handle.
    /// * `allow_derive` - Whether derive operations are authorized for this handle.
    ///
    /// # Returns
    ///
    /// `Ok(())` after key registration succeeds.
    ///
    /// # Errors
    ///
    /// Returns [`Error::StateError`] if the handle is already registered.
    pub fn register_p256_key(
        &mut self,
        handle: PsaExternalKeyHandle,
        key: P256PrivateKey,
        allow_sign: bool,
        allow_derive: bool,
    ) -> Result<()> {
        self.insert_key(
            handle,
            SoftwarePrivateMaterial::P256(key),
            HandlePolicy {
                allow_sign,
                allow_decrypt: false,
                allow_derive,
            },
        )
    }

    /// Inserts a key record while enforcing unique handle ownership.
    ///
    /// # Arguments
    ///
    /// * `handle` - Opaque key handle used as map key.
    /// * `material` - Private material variant to store.
    /// * `policy` - Allowed operation policy for this handle.
    ///
    /// # Returns
    ///
    /// `Ok(())` when the record is inserted.
    ///
    /// # Errors
    ///
    /// Returns [`Error::PolicyViolation`] when handle already exists.
    fn insert_key(
        &mut self,
        handle: PsaExternalKeyHandle,
        material: SoftwarePrivateMaterial,
        policy: HandlePolicy,
    ) -> Result<()> {
        if self.keys.contains_key(handle.as_bytes()) {
            return Err(Error::StateError("psa key handle already registered"));
        }
        self.keys.insert(handle.id, (material, policy));
        Ok(())
    }

    /// Resolves a key record and policy by handle identifier.
    ///
    /// # Arguments
    ///
    /// * `handle` - Opaque handle whose key material is required.
    ///
    /// # Returns
    ///
    /// Borrowed private material and policy tuple for the handle.
    ///
    /// # Errors
    ///
    /// Returns [`Error::PolicyViolation`] when the handle is unknown.
    fn resolve_key(
        &self,
        handle: &PsaExternalKeyHandle,
    ) -> Result<&(SoftwarePrivateMaterial, HandlePolicy)> {
        self.keys
            .get(handle.as_bytes())
            .ok_or(Error::StateError("psa key handle invalid"))
    }
}

impl PsaCryptoBackend for PsaSoftwareBackend {
    /// Executes signing operations using software cryptographic primitives.
    ///
    /// # Arguments
    ///
    /// * `self` - Software backend state containing registered keys.
    /// * `request` - Sign request with handle, algorithm, and digest.
    ///
    /// # Returns
    ///
    /// Signature bytes from RSA sign operations.
    ///
    /// # Errors
    ///
    /// Returns policy or crypto errors for unknown handles, denied usage, or bad key type.
    fn sign(&self, request: &KeySignRequest<'_>) -> Result<Vec<u8>> {
        let (material, policy) = self.resolve_key(request.handle)?;
        if !policy.allow_sign {
            return Err(Error::StateError("psa sign not permitted by key policy"));
        }
        match (request.algorithm, material) {
            (PsaSignAlgorithm::RsaPkcs1Sha256, SoftwarePrivateMaterial::Rsa(key)) => {
                rsassa_sha256_sign(key, request.message)
            }
            (PsaSignAlgorithm::RsaPssSha256, SoftwarePrivateMaterial::Rsa(key)) => {
                let salt = request.salt.ok_or(Error::InvalidLength(
                    "rsa-pss-sha256 signing requires a salt",
                ))?;
                rsassa_pss_sha256_sign(key, request.message, salt)
            }
            (PsaSignAlgorithm::EcdsaP256Sha256, SoftwarePrivateMaterial::P256(key)) => {
                let (r, s) = p256_ecdsa_sign_sha256(key, request.message)?;
                let mut signature = Vec::with_capacity(64);
                signature.extend_from_slice(&r);
                signature.extend_from_slice(&s);
                Ok(signature)
            }
            _ => Err(Error::UnsupportedFeature("psa sign algorithm/key mismatch")),
        }
    }

    /// Executes decrypt operations using software cryptographic primitives.
    ///
    /// # Arguments
    ///
    /// * `self` - Software backend state containing registered keys.
    /// * `request` - Decrypt request with handle, algorithm, and ciphertext.
    ///
    /// # Returns
    ///
    /// Plaintext bytes decrypted from input ciphertext.
    ///
    /// # Errors
    ///
    /// Returns policy or crypto errors for unknown handles, denied usage, or bad key type.
    fn decrypt(&self, request: &KeyDecryptRequest<'_>) -> Result<Vec<u8>> {
        let (material, policy) = self.resolve_key(request.handle)?;
        if !policy.allow_decrypt {
            return Err(Error::StateError("psa decrypt not permitted by key policy"));
        }
        match (request.algorithm, material) {
            (PsaDecryptAlgorithm::RsaPkcs1v15, SoftwarePrivateMaterial::Rsa(key)) => {
                rsaes_pkcs1_v15_decrypt(key, request.ciphertext)
            }
            (PsaDecryptAlgorithm::RsaOaepSha256, SoftwarePrivateMaterial::Rsa(key)) => {
                rsaes_oaep_sha256_decrypt(key, request.ciphertext, request.label.unwrap_or(&[]))
            }
            _ => Err(Error::UnsupportedFeature(
                "psa decrypt algorithm/key mismatch",
            )),
        }
    }

    /// Executes derive operations using software X25519 primitive.
    ///
    /// # Arguments
    ///
    /// * `self` - Software backend state containing registered keys.
    /// * `request` - Derive request with handle, algorithm, and peer key.
    ///
    /// # Returns
    ///
    /// Shared secret bytes from X25519 derive operation.
    ///
    /// # Errors
    ///
    /// Returns policy or parse errors for unknown handles, denied usage, or invalid peer key.
    fn derive(&self, request: &KeyDeriveRequest<'_>) -> Result<Vec<u8>> {
        let (material, policy) = self.resolve_key(request.handle)?;
        if !policy.allow_derive {
            return Err(Error::StateError("psa derive not permitted by key policy"));
        }
        match (request.algorithm, material) {
            (PsaDeriveAlgorithm::X25519, SoftwarePrivateMaterial::X25519(private)) => {
                if request.peer_public_key.len() != 32 {
                    return Err(Error::ParseFailure("x25519 peer public key length invalid"));
                }
                let mut peer = [0u8; 32];
                peer.copy_from_slice(request.peer_public_key);
                Ok(x25519(private, &peer).to_vec())
            }
            (PsaDeriveAlgorithm::EcdhP256, SoftwarePrivateMaterial::P256(private)) => {
                let peer = P256PublicKey::from_uncompressed(request.peer_public_key)?;
                Ok(p256_ecdh_shared_secret(private, &peer)?.to_vec())
            }
            _ => Err(Error::UnsupportedFeature(
                "psa derive algorithm/key mismatch",
            )),
        }
    }

    /// Produces deterministic random bytes for validation-only posture.
    ///
    /// # Arguments
    ///
    /// * `self` - Software backend state (not used by this implementation).
    /// * `out` - Mutable output buffer to fill with deterministic bytes.
    ///
    /// # Returns
    ///
    /// `Ok(())` once all output bytes are filled.
    ///
    /// # Errors
    ///
    /// This function does not return errors in the software backend.
    fn random(&self, out: &mut [u8]) -> Result<()> {
        for (idx, byte) in out.iter_mut().enumerate() {
            *byte = (idx as u8).wrapping_mul(17).wrapping_add(0x5A);
        }
        Ok(())
    }

    /// Computes SHA-256 digest with software primitive implementation.
    ///
    /// # Arguments
    ///
    /// * `self` - Software backend state (not used by this implementation).
    /// * `input` - Input bytes to hash.
    ///
    /// # Returns
    ///
    /// 32-byte SHA-256 digest.
    ///
    /// # Errors
    ///
    /// Returns crypto error if digest primitive reports a failure.
    fn sha256(&self, input: &[u8]) -> Result<[u8; 32]> {
        Ok(sha256(input))
    }

    /// Encrypts using AES-GCM software primitive.
    ///
    /// # Arguments
    ///
    /// * `self` - Software backend state (not used by this implementation).
    /// * `request` - Encryption request with key, nonce, AAD, and plaintext.
    ///
    /// # Returns
    ///
    /// Ciphertext bytes plus 16-byte authentication tag.
    ///
    /// # Errors
    ///
    /// Returns [`Error::UnsupportedFeature`] because software AES-GCM path is not wired here.
    fn aes_gcm_encrypt(&self, request: &AeadEncryptRequest<'_>) -> Result<AeadEncryptResponse> {
        let cipher = AesCipher::new(request.key)?;
        let (ciphertext, tag) =
            aes_gcm_encrypt(&cipher, request.nonce, request.aad, request.plaintext)?;
        Ok(AeadEncryptResponse { ciphertext, tag })
    }
}

/// Type alias for default software-backed PSA provider.
pub type PsaSoftwareProvider = PsaProvider<PsaSoftwareBackend>;