|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * SPDX-FileCopyrightText: 2026 VEY-OSS Developers. |
| 4 | + */ |
| 5 | + |
| 6 | +use std::ptr; |
| 7 | + |
| 8 | +use openssl::bn::{BigNum, BigNumContext}; |
| 9 | +use openssl::ecdsa::EcdsaSig; |
| 10 | +use openssl::foreign_types::{ForeignType, ForeignTypeRef}; |
| 11 | +use openssl::pkey::{PKeyRef, Private}; |
| 12 | + |
| 13 | +use crate::MbStatus; |
| 14 | +use crate::ffi::{self, BATCH_SIZE, MBX_STATUS_OK}; |
| 15 | +use crate::openssl_ffi; |
| 16 | + |
| 17 | +/// Largest NIST curve field size we support (P-521). |
| 18 | +pub const MAX_FIELD_LEN: usize = 66; |
| 19 | + |
| 20 | +#[derive(Clone, Copy)] |
| 21 | +pub enum Curve { |
| 22 | + P256, |
| 23 | + P384, |
| 24 | + P521, |
| 25 | +} |
| 26 | + |
| 27 | +impl Curve { |
| 28 | + pub fn field_len(self) -> usize { |
| 29 | + match self { |
| 30 | + Curve::P256 => 32, |
| 31 | + Curve::P384 => 48, |
| 32 | + Curve::P521 => 66, |
| 33 | + } |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +pub struct EcdsaSlot { |
| 38 | + field_len: usize, |
| 39 | + msg: [u8; MAX_FIELD_LEN], |
| 40 | + sign_r: [u8; MAX_FIELD_LEN], |
| 41 | + sign_s: [u8; MAX_FIELD_LEN], |
| 42 | + eph: BigNum, |
| 43 | + /// Owned copy of the EC private scalar; must outlive `sign_mb8`. |
| 44 | + reg: BigNum, |
| 45 | +} |
| 46 | + |
| 47 | +impl EcdsaSlot { |
| 48 | + pub fn prepare(curve: Curve, key: &PKeyRef<Private>, digest: &[u8]) -> Option<Self> { |
| 49 | + let field_len = curve.field_len(); |
| 50 | + let ec = key.ec_key().ok()?; |
| 51 | + let mut order = BigNum::new().ok()?; |
| 52 | + let mut ctx = BigNumContext::new().ok()?; |
| 53 | + ec.group().order(&mut order, &mut ctx).ok()?; |
| 54 | + // `ec_key()` returns an owned key; copy the scalar so it stays valid |
| 55 | + // after `ec` is dropped at the end of this function. |
| 56 | + let reg = ec.private_key().to_owned().ok()?; |
| 57 | + |
| 58 | + let mut msg = [0u8; MAX_FIELD_LEN]; |
| 59 | + if digest.len() >= field_len { |
| 60 | + msg[..field_len].copy_from_slice(&digest[..field_len]); |
| 61 | + } else { |
| 62 | + msg[field_len - digest.len()..field_len].copy_from_slice(digest); |
| 63 | + } |
| 64 | + |
| 65 | + let eph = priv_rand_range(&order)?; |
| 66 | + Some(EcdsaSlot { |
| 67 | + field_len, |
| 68 | + msg, |
| 69 | + sign_r: [0u8; MAX_FIELD_LEN], |
| 70 | + sign_s: [0u8; MAX_FIELD_LEN], |
| 71 | + eph, |
| 72 | + reg, |
| 73 | + }) |
| 74 | + } |
| 75 | + |
| 76 | + pub fn field_len(&self) -> usize { |
| 77 | + self.field_len |
| 78 | + } |
| 79 | + |
| 80 | + pub fn sign_r(&self) -> &[u8] { |
| 81 | + &self.sign_r[..self.field_len] |
| 82 | + } |
| 83 | + |
| 84 | + pub fn sign_s(&self) -> &[u8] { |
| 85 | + &self.sign_s[..self.field_len] |
| 86 | + } |
| 87 | + |
| 88 | + /// Encode the signature as DER. |
| 89 | + pub fn der_signature(&self) -> Option<Vec<u8>> { |
| 90 | + let r_bn = BigNum::from_slice(self.sign_r()).ok()?; |
| 91 | + let s_bn = BigNum::from_slice(self.sign_s()).ok()?; |
| 92 | + let sig = EcdsaSig::from_private_components(r_bn, s_bn).ok()?; |
| 93 | + sig.to_der().ok() |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +/// Sign up to [`BATCH_SIZE`] slots. Returns per-lane status; only the first |
| 98 | +/// `slots.len().min(BATCH_SIZE)` entries are meaningful. |
| 99 | +pub fn sign_mb8(curve: Curve, slots: &mut [EcdsaSlot]) -> [MbStatus; BATCH_SIZE] { |
| 100 | + let n = slots.len().min(BATCH_SIZE); |
| 101 | + let mut statuses = [MBX_STATUS_OK; BATCH_SIZE]; |
| 102 | + if n == 0 { |
| 103 | + return statuses; |
| 104 | + } |
| 105 | + |
| 106 | + let mut pa_sign_r = [ptr::null_mut(); BATCH_SIZE]; |
| 107 | + let mut pa_sign_s = [ptr::null_mut(); BATCH_SIZE]; |
| 108 | + let mut pa_msg = [ptr::null(); BATCH_SIZE]; |
| 109 | + let mut pa_eph = [ptr::null(); BATCH_SIZE]; |
| 110 | + let mut pa_reg = [ptr::null(); BATCH_SIZE]; |
| 111 | + |
| 112 | + for (i, slot) in slots.iter_mut().take(n).enumerate() { |
| 113 | + pa_sign_r[i] = slot.sign_r.as_mut_ptr(); |
| 114 | + pa_sign_s[i] = slot.sign_s.as_mut_ptr(); |
| 115 | + pa_msg[i] = slot.msg.as_ptr(); |
| 116 | + pa_eph[i] = slot.eph.as_ptr(); |
| 117 | + pa_reg[i] = slot.reg.as_ptr(); |
| 118 | + } |
| 119 | + |
| 120 | + let status = unsafe { |
| 121 | + match curve { |
| 122 | + Curve::P256 => ffi::mbx_nistp256_ecdsa_sign_ssl_mb8( |
| 123 | + pa_sign_r.as_ptr(), |
| 124 | + pa_sign_s.as_ptr(), |
| 125 | + pa_msg.as_ptr(), |
| 126 | + pa_eph.as_ptr(), |
| 127 | + pa_reg.as_ptr(), |
| 128 | + ptr::null_mut(), |
| 129 | + ), |
| 130 | + Curve::P384 => ffi::mbx_nistp384_ecdsa_sign_ssl_mb8( |
| 131 | + pa_sign_r.as_ptr(), |
| 132 | + pa_sign_s.as_ptr(), |
| 133 | + pa_msg.as_ptr(), |
| 134 | + pa_eph.as_ptr(), |
| 135 | + pa_reg.as_ptr(), |
| 136 | + ptr::null_mut(), |
| 137 | + ), |
| 138 | + Curve::P521 => ffi::mbx_nistp521_ecdsa_sign_ssl_mb8( |
| 139 | + pa_sign_r.as_ptr(), |
| 140 | + pa_sign_s.as_ptr(), |
| 141 | + pa_msg.as_ptr(), |
| 142 | + pa_eph.as_ptr(), |
| 143 | + pa_reg.as_ptr(), |
| 144 | + ptr::null_mut(), |
| 145 | + ), |
| 146 | + } |
| 147 | + }; |
| 148 | + for (i, sts) in statuses.iter_mut().take(n).enumerate() { |
| 149 | + *sts = ffi::mbx_get_sts(status, i); |
| 150 | + } |
| 151 | + statuses |
| 152 | +} |
| 153 | + |
| 154 | +fn priv_rand_range(order: &openssl::bn::BigNumRef) -> Option<BigNum> { |
| 155 | + let eph = BigNum::new().ok()?; |
| 156 | + for _ in 0..64 { |
| 157 | + let rc = unsafe { openssl_ffi::BN_priv_rand_range(eph.as_ptr(), order.as_ptr()) }; |
| 158 | + let is_zero = unsafe { openssl_ffi::BN_is_zero(eph.as_ptr()) == 1 }; |
| 159 | + if rc == 1 && !is_zero { |
| 160 | + return Some(eph); |
| 161 | + } |
| 162 | + } |
| 163 | + None |
| 164 | +} |
0 commit comments