qualia_core_db/crypto/network/
dual_sign.rs1use crate::crypto::network::ed25519::{sign_with_context, verify_with_context};
18use crate::crypto::network::errors::CryptoError;
19use crate::crypto::network::mldsa;
20use crate::crypto::network::types::{
21 ED25519_PK_LEN, ED25519_SIG_LEN, ML_DSA_65_PK_LEN, ML_DSA_65_SIG_LEN, ML_DSA_65_SK_LEN,
22};
23
24#[repr(C)]
26#[derive(Clone, Copy, Debug, PartialEq, Eq)]
27pub struct DualProof {
28 pub mldsa_sig: [u8; ML_DSA_65_SIG_LEN],
29 pub ed25519_sig: [u8; ED25519_SIG_LEN],
30}
31
32pub fn sign_dual(
38 mldsa_sk: &[u8; ML_DSA_65_SK_LEN],
39 ed25519_seed: &[u8; 32],
40 message: &[u8],
41 context: &[u8],
42 out: &mut DualProof,
43) -> Result<(), CryptoError> {
44 mldsa::sign(mldsa_sk, message, context, &mut out.mldsa_sig)?;
45 sign_with_context(ed25519_seed, message, context, &mut out.ed25519_sig)?;
46 Ok(())
47}
48
49pub fn verify_dual(
56 mldsa_pk: &[u8; ML_DSA_65_PK_LEN],
57 ed25519_pk: &[u8; ED25519_PK_LEN],
58 message: &[u8],
59 context: &[u8],
60 proof: &DualProof,
61) -> Result<(), CryptoError> {
62 mldsa::verify(mldsa_pk, message, context, &proof.mldsa_sig)?;
63 verify_with_context(ed25519_pk, message, context, &proof.ed25519_sig)?;
64 Ok(())
65}
66
67pub fn single_algorithm_is_enough() -> bool {
69 false
70}
71
72pub fn cose_sign1_frozen() -> bool {
74 false
75}
76
77#[cfg(test)]
78mod tests {
79 use super::*;
80 use crate::crypto::network::ed25519::public_from_seed;
81 use crate::crypto::network::mldsa::generate_keypair;
82
83 const MSG: &[u8] = b"qdnf-dual-payload";
84 const CTX: &[u8] = b"qlink";
85 const SEED: [u8; 32] = [7u8; 32];
86
87 fn fresh_signed() -> ([u8; ML_DSA_65_PK_LEN], [u8; ED25519_PK_LEN], DualProof) {
88 let (sk, pk) = generate_keypair().unwrap();
89 let ed_pk = public_from_seed(&SEED);
90 let mut proof = DualProof {
91 mldsa_sig: [0u8; ML_DSA_65_SIG_LEN],
92 ed25519_sig: [0u8; ED25519_SIG_LEN],
93 };
94 sign_dual(&sk, &SEED, MSG, CTX, &mut proof).unwrap();
95 (pk, ed_pk, proof)
96 }
97
98 #[test]
99 fn generate_sign_verify_ok() {
100 let (mldsa_pk, ed_pk, proof) = fresh_signed();
101 assert!(verify_dual(&mldsa_pk, &ed_pk, MSG, CTX, &proof).is_ok());
102 }
103
104 #[test]
105 fn wrong_ed25519_sig_is_unauthorized() {
106 let (mldsa_pk, ed_pk, mut proof) = fresh_signed();
107 proof.ed25519_sig[0] ^= 0x01;
108 assert_eq!(
109 verify_dual(&mldsa_pk, &ed_pk, MSG, CTX, &proof),
110 Err(CryptoError::Unauthorized)
111 );
112 assert!(mldsa::verify(&mldsa_pk, MSG, CTX, &proof.mldsa_sig).is_ok());
113 }
114
115 #[test]
116 fn wrong_context_is_unauthorized() {
117 let (mldsa_pk, ed_pk, proof) = fresh_signed();
118 assert_eq!(
119 verify_dual(&mldsa_pk, &ed_pk, MSG, b"qsession", &proof),
120 Err(CryptoError::Unauthorized)
121 );
122 }
123
124 #[test]
125 fn single_algorithm_is_not_enough() {
126 assert!(!single_algorithm_is_enough());
127 }
128
129 #[test]
130 fn cose_sign1_is_not_frozen() {
131 assert!(!cose_sign1_frozen());
132 }
133
134 #[test]
135 fn stripped_ed25519_is_unauthorized() {
136 let (mldsa_pk, ed_pk, mut proof) = fresh_signed();
137 proof.ed25519_sig = [0u8; ED25519_SIG_LEN];
138 assert_eq!(
139 verify_dual(&mldsa_pk, &ed_pk, MSG, CTX, &proof),
140 Err(CryptoError::Unauthorized)
141 );
142 assert!(mldsa::verify(&mldsa_pk, MSG, CTX, &proof.mldsa_sig).is_ok());
143 }
144}