DsaSig

Struct DsaSig 

Source
pub struct DsaSig(/* private fields */);
Expand description

Object representing DSA signature.

DSA signatures consist of two components: r and s.

§Examples

use std::convert::TryInto;

use openssl::bn::BigNum;
use openssl::dsa::{Dsa, DsaSig};
use openssl::hash::MessageDigest;
use openssl::pkey::PKey;
use openssl::sign::{Signer, Verifier};

const TEST_DATA: &[u8] = &[0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
let dsa_ref = Dsa::generate(1024).unwrap();

let pub_key: PKey<_> = dsa_ref.clone().try_into().unwrap();
let priv_key: PKey<_> = dsa_ref.try_into().unwrap();

let mut signer = if let Ok(signer) = Signer::new(MessageDigest::sha256(), &priv_key) {
    signer
} else {
    // DSA signing is not supported (eg. BoringSSL)
    return;
};

signer.update(TEST_DATA).unwrap();

let signature = signer.sign_to_vec().unwrap();
// Parse DER-encoded DSA signature
let signature = DsaSig::from_der(&signature).unwrap();

// Extract components `r` and `s`
let r = BigNum::from_slice(&signature.r().to_vec()).unwrap();
let s = BigNum::from_slice(&signature.s().to_vec()).unwrap();

// Construct new DSA signature from components
let signature = DsaSig::from_private_components(r, s).unwrap();

// Serialize DSA signature to DER
let signature = signature.to_der().unwrap();

let mut verifier = Verifier::new(MessageDigest::sha256(), &pub_key).unwrap();
verifier.update(TEST_DATA).unwrap();
assert!(verifier.verify(&signature[..]).unwrap());

Implementations§

Source§

impl DsaSig

Source

pub fn from_private_components(r: BigNum, s: BigNum) -> Result<Self, ErrorStack>

Returns a new DsaSig by setting the r and s values associated with an DSA signature.

This corresponds to DSA_SIG_set0.

Source

pub fn from_der(der: &[u8]) -> Result<DsaSig, ErrorStack>

Decodes a DER-encoded DSA signature.

This corresponds to d2i_DSA_SIG.

Methods from Deref<Target = DsaSigRef>§

Source

pub fn to_der(&self) -> Result<Vec<u8>, ErrorStack>

Serializes the DSA signature into a DER-encoded DSASignature structure.

This corresponds to i2d_DSA_SIG.

Source

pub fn r(&self) -> &BigNumRef

Returns internal component r of an DsaSig.

This corresponds to DSA_SIG_get0.

Source

pub fn s(&self) -> &BigNumRef

Returns internal component s of an DsaSig.

This corresponds to DSA_SIG_get0.

Trait Implementations§

Source§

impl AsRef<DsaSigRef> for DsaSig

Source§

fn as_ref(&self) -> &DsaSigRef

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl Borrow<DsaSigRef> for DsaSig

Source§

fn borrow(&self) -> &DsaSigRef

Immutably borrows from an owned value. Read more
Source§

impl Debug for DsaSig

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Deref for DsaSig

Source§

type Target = DsaSigRef

The resulting type after dereferencing.
Source§

fn deref(&self) -> &DsaSigRef

Dereferences the value.
Source§

impl DerefMut for DsaSig

Source§

fn deref_mut(&mut self) -> &mut DsaSigRef

Mutably dereferences the value.
Source§

impl Drop for DsaSig

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl ForeignType for DsaSig

Source§

type CType = DSA_SIG

The raw C type.
Source§

type Ref = DsaSigRef

The type representing a reference to this type.
Source§

unsafe fn from_ptr(ptr: *mut DSA_SIG) -> DsaSig

Constructs an instance of this type from its raw type.
Source§

fn as_ptr(&self) -> *mut DSA_SIG

Returns a raw pointer to the wrapped value.
Source§

impl Send for DsaSig

Source§

impl Sync for DsaSig

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.

Layout§

Note: Most layout information is completely unstable and may even differ between compilations. The only exception is types with certain repr(...) attributes. Please see the Rust Reference's “Type Layout” chapter for details on type layout guarantees.

Size: 8 bytes