oc-crypto 0.0.5

Cryptographic primitives and key schemes for Open Crate containers
Documentation
// SPDX-License-Identifier: MPL-2.0
//! SOFTWARE RSA-PSS-SHA256 signer: ONLY for probes.
//!
//! # Why it exists at all
//!
//! The production editing key resides in a TPM (`docs/format.md`, "EDITING IS EXECUTABLE",
//! item G), and only the TPM signs. But every run must test editing verification,
//! including Linux containers without TPMs, while PSS signatures are randomized: the live-TPM golden
//! vector (`tests/kat/rsa_pss.kat`) covers one message, whereas
//! probes need signatures over ARBITRARY regions. Hence a signer with HARDCODED
//! test keys and a salt parameter: the same salt gives the same
//! signature, making `tests/kat/edit.kat` reproducible.
//!
//! # Why it is permitted
//!
//! This module exists only under `cfg(test)` or feature `test-signer`, enabled
//! only by `[dev-dependencies]` and testbed builds
//! (guarded in `cc-cli/tests/repository_hygiene.rs`). The private operation here
//! is NOT constant-time, and keys are public in source, precisely why no
//! production path reaches it. The keys were generated for this file
//! (2026-09-17, Miller–Rabin primes, 40 rounds) and live nowhere else.

use crate::CryptoError;
use crate::rsa::MODULUS_LEN;
use crypto_bigint::modular::{BoxedMontyForm, BoxedMontyParams};
use crypto_bigint::{BoxedUint, Odd};
use sha2::{Digest, Sha256};

const BITS: u32 = 2048;
const HLEN: usize = 32;
const DB_LEN: usize = 223;
const PS_LEN: usize = 190;

/// RSA-2048 test key: modulus and private exponent. NOT SECRET.
#[derive(Debug, Clone, Copy)]
pub struct TestRsaKey {
    n: [u8; MODULUS_LEN],
    d: [u8; MODULUS_LEN],
}

/// The "our editor" key in probes.
pub const TEST_KEY_A: TestRsaKey = TestRsaKey {
    n: NA,
    d: DA,
};

/// The "other editor" key in probes.
pub const TEST_KEY_B: TestRsaKey = TestRsaKey {
    n: NB,
    d: DB,
};

const NA: [u8; MODULUS_LEN] = [
        0xa5, 0xb7, 0x8b, 0x91, 0x0c, 0xcd, 0xa8, 0x2a, 0x20, 0xe0, 0xc9, 0x76,
        0xe4, 0x15, 0x6f, 0x9c, 0x49, 0x14, 0xc3, 0x7d, 0xda, 0xa3, 0x92, 0xaa,
        0xd6, 0x8c, 0x83, 0xe7, 0x71, 0xde, 0x77, 0x45, 0x23, 0xd1, 0x18, 0x16,
        0x3e, 0x5e, 0xd2, 0x37, 0x3f, 0xf0, 0xba, 0xdf, 0xaf, 0xb0, 0xae, 0x26,
        0x86, 0xae, 0xfa, 0xa1, 0x6b, 0x7f, 0x90, 0x5b, 0x90, 0x4f, 0xbd, 0x3b,
        0xbb, 0x4f, 0xfb, 0x6c, 0x07, 0x94, 0x9e, 0xcb, 0x7d, 0xc9, 0xa9, 0x63,
        0xfe, 0x1b, 0xf0, 0x33, 0x1e, 0xb1, 0x43, 0x3d, 0x53, 0x1d, 0x4e, 0x74,
        0x20, 0x3b, 0x2f, 0xaa, 0xf7, 0x7b, 0xf2, 0x31, 0x06, 0x14, 0x28, 0x35,
        0x94, 0x29, 0x82, 0x33, 0x24, 0xf1, 0xd0, 0x2d, 0x27, 0x08, 0x02, 0xfb,
        0xc0, 0x80, 0x5c, 0xb9, 0xae, 0x3e, 0x5b, 0x5c, 0xfe, 0xee, 0x5f, 0x2e,
        0x64, 0x33, 0xde, 0xf5, 0xed, 0xd0, 0x9d, 0x18, 0xc4, 0x29, 0x95, 0x22,
        0xc8, 0x15, 0xfe, 0x3d, 0x6f, 0xe4, 0x56, 0x0b, 0x71, 0x57, 0x43, 0x62,
        0xd3, 0xe9, 0x99, 0xf6, 0x55, 0x87, 0x08, 0x0d, 0x91, 0x02, 0x49, 0x13,
        0x74, 0x57, 0xdc, 0x1a, 0x8e, 0x75, 0x2d, 0x1d, 0xa1, 0x9c, 0x70, 0xab,
        0xb3, 0x56, 0x0d, 0xe6, 0x80, 0x8d, 0x37, 0xe2, 0x0c, 0xb5, 0x22, 0x68,
        0x54, 0xd8, 0xaa, 0x81, 0xf3, 0x51, 0xf3, 0x34, 0x85, 0x4a, 0xac, 0xdc,
        0x7c, 0x1d, 0x8b, 0x7c, 0x94, 0xc0, 0xc1, 0xad, 0xe0, 0x20, 0x94, 0x1a,
        0x9c, 0xfb, 0x62, 0x44, 0x9e, 0x06, 0x5a, 0xb0, 0x29, 0xf0, 0x54, 0x75,
        0xe8, 0x2e, 0x05, 0x8c, 0x4f, 0x98, 0xd7, 0x79, 0x58, 0xf0, 0x82, 0x30,
        0x5a, 0xe0, 0x43, 0xc7, 0x2c, 0x8d, 0x4d, 0x1e, 0x09, 0x86, 0x82, 0x93,
        0x76, 0xd3, 0x3d, 0xe0, 0x0d, 0xb3, 0x3e, 0x08, 0x2c, 0x42, 0x35, 0x84,
        0x8f, 0x84, 0x42, 0x53,
    ];
const DA: [u8; MODULUS_LEN] = [
        0xa5, 0x46, 0x43, 0x89, 0xec, 0xad, 0x07, 0xb4, 0xcc, 0x89, 0xa4, 0x24,
        0x51, 0x22, 0x17, 0xe8, 0x54, 0xf2, 0xc2, 0x6e, 0x38, 0xaa, 0xbe, 0xd2,
        0x67, 0x9c, 0x48, 0xc9, 0x81, 0xc5, 0x90, 0x4a, 0x82, 0x88, 0x3d, 0x6f,
        0xa1, 0xd0, 0xcb, 0xf3, 0x92, 0x6f, 0xb2, 0xd0, 0xf8, 0xd6, 0x4b, 0x39,
        0xdd, 0x45, 0x92, 0x6d, 0x7f, 0x5b, 0x00, 0x4c, 0x71, 0xc7, 0x35, 0x41,
        0xe7, 0x74, 0xc3, 0x9a, 0x60, 0x8d, 0x17, 0x3a, 0x78, 0x3f, 0xb2, 0x13,
        0x2a, 0x12, 0x0f, 0x23, 0xcc, 0xce, 0x99, 0xf3, 0x05, 0x7b, 0xcb, 0x0d,
        0x02, 0x40, 0x45, 0x30, 0x4c, 0x17, 0x6f, 0xda, 0xf8, 0xe7, 0x0d, 0x9b,
        0x32, 0x23, 0x9b, 0xd9, 0x0a, 0x55, 0xa9, 0x7b, 0xfe, 0xdb, 0x7c, 0xae,
        0x3c, 0xb5, 0x48, 0xc5, 0x5f, 0x44, 0x8d, 0xb6, 0x58, 0x02, 0xc1, 0x26,
        0xf1, 0x84, 0xd2, 0x2f, 0x90, 0x79, 0x68, 0xd2, 0xf9, 0x27, 0xcc, 0xbf,
        0x65, 0x38, 0x62, 0x0b, 0x1f, 0x62, 0xf7, 0x3a, 0xa7, 0xeb, 0xe2, 0x42,
        0x82, 0x33, 0xbf, 0xfb, 0x10, 0xb2, 0xad, 0xe8, 0x5a, 0x51, 0x06, 0x19,
        0xb7, 0xdf, 0x67, 0x6d, 0x9a, 0xf6, 0x4d, 0x7e, 0xbf, 0xfa, 0x1c, 0x20,
        0x92, 0x7f, 0xeb, 0x62, 0xec, 0xb5, 0xe4, 0xdb, 0x2e, 0x55, 0x4d, 0xb1,
        0xec, 0x1a, 0x05, 0x51, 0x5c, 0x47, 0xb2, 0xca, 0x87, 0xc6, 0x50, 0xfe,
        0x03, 0xf1, 0x8b, 0xbc, 0x13, 0x75, 0x0f, 0xbe, 0xc5, 0x09, 0x38, 0x0f,
        0x0f, 0xed, 0xfa, 0x07, 0xc9, 0x97, 0x13, 0xf9, 0xba, 0xbc, 0x96, 0x3b,
        0x3e, 0x85, 0x13, 0x46, 0xae, 0xd1, 0xee, 0x52, 0xc5, 0xe6, 0xf7, 0x3f,
        0x22, 0x4f, 0x5d, 0x6b, 0x3b, 0x88, 0x4c, 0x38, 0xa5, 0x3d, 0x75, 0x27,
        0xc2, 0xd9, 0x6b, 0x2c, 0x2d, 0xfb, 0x52, 0xfe, 0x25, 0x65, 0x87, 0x6a,
        0x51, 0xc3, 0x02, 0xc1,
    ];
const NB: [u8; MODULUS_LEN] = [
        0xd4, 0xf6, 0xe3, 0xb3, 0xb0, 0x01, 0xa2, 0x6e, 0xbc, 0xa0, 0xea, 0xb5,
        0xb4, 0xaf, 0xb8, 0x62, 0xf6, 0x33, 0x6a, 0xd9, 0x04, 0x78, 0xe8, 0xe8,
        0x93, 0x34, 0x55, 0x39, 0xa9, 0x30, 0x3f, 0xae, 0x20, 0xb8, 0x04, 0xe8,
        0xa9, 0xa2, 0x5b, 0x5c, 0x29, 0xb4, 0x04, 0xad, 0xab, 0xf1, 0xe2, 0xee,
        0xc6, 0xe6, 0xf6, 0xcf, 0xca, 0x59, 0xc1, 0x4e, 0xfd, 0x77, 0x52, 0xf4,
        0xde, 0x57, 0x91, 0x11, 0x01, 0xfe, 0x8c, 0xe8, 0x8d, 0xd5, 0x3f, 0x6b,
        0xa2, 0x79, 0x11, 0x24, 0xf5, 0x6e, 0xc7, 0x5a, 0x0d, 0xe1, 0xac, 0xe7,
        0xfb, 0xf6, 0x35, 0x40, 0x85, 0x74, 0xbf, 0x93, 0x92, 0x7b, 0xfb, 0xe8,
        0x8e, 0xbd, 0x3b, 0xd5, 0x9b, 0x9a, 0x27, 0xc5, 0xb2, 0x15, 0xa4, 0xfd,
        0xba, 0xad, 0x23, 0xe0, 0x0a, 0x37, 0xd3, 0xb8, 0x2a, 0x2d, 0x53, 0xbb,
        0x35, 0x47, 0x77, 0xa5, 0x7a, 0x61, 0xcb, 0x6d, 0x7a, 0x70, 0x86, 0x49,
        0x13, 0xd3, 0x8e, 0xaa, 0x20, 0xd4, 0x41, 0x8b, 0x99, 0x9c, 0xa6, 0x5b,
        0x3d, 0xef, 0xf6, 0x35, 0xbb, 0x80, 0x2f, 0x1b, 0xc4, 0x98, 0xb4, 0x19,
        0xd3, 0xc9, 0x89, 0x83, 0xe3, 0x72, 0x45, 0x77, 0x83, 0xf5, 0x18, 0x3e,
        0x14, 0xa5, 0x56, 0x77, 0xb3, 0x5c, 0xdb, 0xba, 0xe2, 0x24, 0x32, 0x79,
        0x1c, 0x03, 0xdd, 0xd5, 0x13, 0xc5, 0x57, 0x1f, 0xec, 0xdb, 0xb6, 0xb9,
        0xee, 0x71, 0x1b, 0xe1, 0x77, 0xd5, 0x45, 0x32, 0x98, 0x14, 0x9d, 0x38,
        0x72, 0x69, 0x95, 0x10, 0xe7, 0xa7, 0x3b, 0xbd, 0x7d, 0x0f, 0xc8, 0x3b,
        0xd6, 0x86, 0xe2, 0x8f, 0xf8, 0x4a, 0xfb, 0x13, 0xc6, 0x29, 0xc1, 0x9c,
        0xae, 0xc8, 0x23, 0xdf, 0x17, 0xbc, 0x38, 0x5d, 0xbe, 0x0e, 0x05, 0x5d,
        0x93, 0xe5, 0x24, 0xe6, 0x03, 0xfe, 0x7d, 0x9f, 0x45, 0xa9, 0xf3, 0x31,
        0x1d, 0x9c, 0x2f, 0xd1,
    ];
const DB: [u8; MODULUS_LEN] = [
        0xc0, 0x4c, 0xf7, 0xab, 0x87, 0xa0, 0xd2, 0x35, 0xc0, 0x2a, 0x66, 0x06,
        0x11, 0xd4, 0x22, 0x61, 0x6a, 0x03, 0x5f, 0xd5, 0xfa, 0xcd, 0xdb, 0x32,
        0x37, 0xca, 0x27, 0xb2, 0xdb, 0x6d, 0x6f, 0xf3, 0x22, 0x24, 0x48, 0x7a,
        0x76, 0xc2, 0xde, 0x50, 0x48, 0x6f, 0xff, 0x51, 0x7b, 0x53, 0xfe, 0x20,
        0xb2, 0x86, 0x58, 0x13, 0xa5, 0x9a, 0x8a, 0x8d, 0x73, 0x1e, 0xce, 0x4f,
        0xc2, 0x7d, 0xbb, 0xc6, 0x69, 0xd0, 0x9b, 0x18, 0x5b, 0x0a, 0x56, 0x2c,
        0x31, 0x17, 0xb5, 0x3d, 0x74, 0x49, 0xc5, 0xc5, 0x31, 0x87, 0xb8, 0x83,
        0xf9, 0xe6, 0x86, 0x8b, 0x6e, 0x76, 0xcf, 0x8e, 0xc7, 0xd5, 0x20, 0xd1,
        0x0c, 0x23, 0x1f, 0xe6, 0x70, 0x8d, 0x30, 0xa0, 0xb9, 0x80, 0x05, 0xed,
        0xff, 0x24, 0xdc, 0xa2, 0x2f, 0x57, 0xdd, 0xdc, 0x0c, 0x69, 0xc5, 0x93,
        0xe4, 0x36, 0x56, 0x79, 0x9c, 0x1a, 0xb6, 0x7e, 0xcd, 0x0f, 0x7b, 0x92,
        0x41, 0x18, 0xe1, 0xf3, 0xf7, 0x22, 0x1d, 0xfe, 0x1d, 0x29, 0x11, 0xd6,
        0x5e, 0x75, 0xd2, 0x06, 0x39, 0x3a, 0xec, 0x2c, 0xda, 0x07, 0xfa, 0x38,
        0xbb, 0x17, 0x4a, 0x2a, 0x0e, 0x33, 0x8e, 0xd0, 0x36, 0x57, 0xdb, 0x08,
        0x77, 0xca, 0x27, 0x59, 0xc2, 0xc6, 0xec, 0x19, 0x30, 0x62, 0xc0, 0x5e,
        0xbe, 0x46, 0x3a, 0xdf, 0xeb, 0x3e, 0x19, 0xf8, 0xe8, 0x4f, 0x4d, 0x52,
        0xf8, 0x7e, 0x24, 0x43, 0x2b, 0x84, 0x46, 0xc1, 0x65, 0x2d, 0xb6, 0x94,
        0x66, 0x88, 0xd5, 0x13, 0x2a, 0xdd, 0x85, 0x29, 0xc5, 0x77, 0x7c, 0x59,
        0x05, 0x30, 0x4e, 0xec, 0x94, 0x12, 0xf2, 0x39, 0x24, 0x8a, 0x99, 0xcc,
        0x81, 0xf2, 0x12, 0xdc, 0xe4, 0x4a, 0xf0, 0xe8, 0x3c, 0xe3, 0x85, 0x2e,
        0x39, 0x4a, 0xc6, 0x64, 0x07, 0xef, 0xdd, 0xfb, 0xc2, 0xdd, 0xde, 0x75,
        0xa4, 0x85, 0xa4, 0x69,
    ];

fn mgf1(seed: &[u8], out: &mut [u8]) {
    let mut counter: u32 = 0;
    for block in out.chunks_mut(HLEN) {
        let mut hasher = Sha256::new();
        hasher.update(seed);
        hasher.update(counter.to_be_bytes());
        let digest = hasher.finalize();
        for (dst, src) in block.iter_mut().zip(digest.iter()) {
            *dst = *src;
        }
        counter = counter.wrapping_add(1);
    }
}

impl TestRsaKey {
    /// Modulus: stored in the certificate as `editor_key`.
    #[must_use]
    pub const fn modulus(&self) -> [u8; MODULUS_LEN] {
        self.n
    }

    /// EMSA-PSS signature (RFC 8017 §9.1.1) using the supplied salt.
    ///
    /// # Errors
    /// [`CryptoError::BadLength`]: unparseable key (impossible for the hardcoded keys).
    pub fn sign_pss_sha256(&self, message: &[u8], salt: &[u8; 32]) -> Result<[u8; MODULUS_LEN], CryptoError> {
        let m_hash = Sha256::digest(message);
        let mut prime = Sha256::new();
        prime.update([0u8; 8]);
        prime.update(m_hash);
        prime.update(salt);
        let h = prime.finalize();

        let mut db = [0u8; DB_LEN];
        if let Some(slot) = db.get_mut(PS_LEN) {
            *slot = 0x01;
        }
        if let Some(slot) = db.get_mut(PS_LEN.saturating_add(1)..) {
            slot.copy_from_slice(salt);
        }
        let mut mask = [0u8; DB_LEN];
        mgf1(&h, &mut mask);
        for (dst, src) in db.iter_mut().zip(mask.iter()) {
            *dst ^= *src;
        }
        if let Some(first) = db.first_mut() {
            *first &= 0x7f;
        }

        let mut em = [0u8; MODULUS_LEN];
        if let Some(slot) = em.get_mut(..DB_LEN) {
            slot.copy_from_slice(&db);
        }
        if let Some(slot) = em.get_mut(DB_LEN..DB_LEN.saturating_add(HLEN)) {
            slot.copy_from_slice(&h);
        }
        if let Some(last) = em.last_mut() {
            *last = 0xbc;
        }

        let n = BoxedUint::from_be_slice(&self.n, BITS).map_err(|_| CryptoError::BadLength)?;
        let d = BoxedUint::from_be_slice(&self.d, BITS).map_err(|_| CryptoError::BadLength)?;
        let x = BoxedUint::from_be_slice(&em, BITS).map_err(|_| CryptoError::BadLength)?;
        let odd = Odd::new(n).into_option().ok_or(CryptoError::BadLength)?;
        let params = BoxedMontyParams::new(odd);
        let out = BoxedMontyForm::new(x, &params).pow(&d).retrieve().to_be_bytes();
        <[u8; MODULUS_LEN]>::try_from(&*out).map_err(|_| CryptoError::BadLength)
    }
}

#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::indexing_slicing)]
mod tests {
    use super::*;
    use crate::rsa::verify_pss_sha256;

    #[test]
    fn a_test_signature_verifies_and_a_foreign_key_does_not() {
        let sig = TEST_KEY_A.sign_pss_sha256(b"message", &[7u8; 32]).unwrap();
        verify_pss_sha256(&TEST_KEY_A.modulus(), b"message", &sig).unwrap();
        assert!(verify_pss_sha256(&TEST_KEY_B.modulus(), b"message", &sig).is_err());
        assert!(verify_pss_sha256(&TEST_KEY_A.modulus(), b"messagE", &sig).is_err());
        let other = TEST_KEY_A.sign_pss_sha256(b"message", &[8u8; 32]).unwrap();
        assert_ne!(sig, other, "соль не участвует в подписи");
        verify_pss_sha256(&TEST_KEY_A.modulus(), b"message", &other).unwrap();
    }
}