|
| 1 | +// Copyright 2024 Contributors to the Parsec project. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +use crate::{structures::EccSignature, Error, Result, WrapperErrorKind}; |
| 5 | + |
| 6 | +use std::convert::TryFrom; |
| 7 | + |
| 8 | +use ecdsa::SignatureSize; |
| 9 | +use elliptic_curve::{ |
| 10 | + generic_array::{typenum::Unsigned, ArrayLength}, |
| 11 | + FieldBytes, FieldBytesSize, PrimeCurve, |
| 12 | +}; |
| 13 | + |
| 14 | +impl<C> TryFrom<EccSignature> for ecdsa::Signature<C> |
| 15 | +where |
| 16 | + C: PrimeCurve, |
| 17 | + SignatureSize<C>: ArrayLength<u8>, |
| 18 | +{ |
| 19 | + type Error = Error; |
| 20 | + |
| 21 | + fn try_from(signature: EccSignature) -> Result<Self> { |
| 22 | + let r = signature.signature_r().as_slice(); |
| 23 | + let s = signature.signature_s().as_slice(); |
| 24 | + |
| 25 | + if r.len() != FieldBytesSize::<C>::USIZE { |
| 26 | + return Err(Error::local_error(WrapperErrorKind::InvalidParam)); |
| 27 | + } |
| 28 | + if s.len() != FieldBytesSize::<C>::USIZE { |
| 29 | + return Err(Error::local_error(WrapperErrorKind::InvalidParam)); |
| 30 | + } |
| 31 | + |
| 32 | + let signature = ecdsa::Signature::from_scalars( |
| 33 | + FieldBytes::<C>::from_slice(r).clone(), |
| 34 | + FieldBytes::<C>::from_slice(s).clone(), |
| 35 | + ) |
| 36 | + .map_err(|_| Error::local_error(WrapperErrorKind::InvalidParam))?; |
| 37 | + Ok(signature) |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +// TODO(baloo): impl TryFrom<RsaSignature> for rsa::pkcs1v15::Signature |
| 42 | +// TODO(baloo): impl TryFrom<RsaSignature> for rsa::pss::Signature |
0 commit comments