1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#![no_std]
#![cfg_attr(docsrs, feature(doc_cfg))]
#![forbid(unsafe_code)]
#![warn(missing_docs, rust_2018_idioms, intra_doc_link_resolution_failure)]
#![doc(
html_logo_url = "https://raw.githubusercontent.com/RustCrypto/meta/master/logo_small.png",
html_root_url = "https://docs.rs/ecdsa/0.7.2"
)]
pub mod asn1;
#[cfg(feature = "dev")]
#[cfg_attr(docsrs, doc(cfg(feature = "dev")))]
pub mod dev;
#[cfg(feature = "hazmat")]
#[cfg_attr(docsrs, doc(cfg(feature = "hazmat")))]
pub mod hazmat;
#[cfg(feature = "signer")]
#[cfg_attr(docsrs, doc(cfg(feature = "signer")))]
pub mod signer;
#[cfg(feature = "verifier")]
#[cfg_attr(docsrs, doc(cfg(feature = "verifier")))]
pub mod verifier;
pub use elliptic_curve::{
self, generic_array,
weierstrass::{Curve, PublicKey},
SecretKey,
};
pub use signature::{self, Error};
#[cfg(feature = "signer")]
pub use signer::Signer;
#[cfg(feature = "verifier")]
pub use verifier::Verifier;
use core::{
convert::TryFrom,
fmt::{self, Debug},
ops::Add,
};
use elliptic_curve::{Arithmetic, ElementBytes, FromBytes};
use generic_array::{typenum::Unsigned, ArrayLength, GenericArray};
pub type SignatureSize<C> = <<C as elliptic_curve::Curve>::ElementSize as Add>::Output;
pub type SignatureBytes<C> = GenericArray<u8, SignatureSize<C>>;
#[derive(Clone, Eq, PartialEq)]
pub struct Signature<C: Curve>
where
SignatureSize<C>: ArrayLength<u8>,
{
bytes: SignatureBytes<C>,
}
impl<C: Curve> Signature<C>
where
SignatureSize<C>: ArrayLength<u8>,
{
pub fn from_scalars(r: &ElementBytes<C>, s: &ElementBytes<C>) -> Self {
let mut bytes = SignatureBytes::<C>::default();
let scalar_size = C::ElementSize::to_usize();
bytes[..scalar_size].copy_from_slice(r.as_slice());
bytes[scalar_size..].copy_from_slice(s.as_slice());
Signature { bytes }
}
pub fn from_asn1(bytes: &[u8]) -> Result<Self, Error>
where
C::ElementSize: Add + ArrayLength<u8>,
asn1::MaxSize<C>: ArrayLength<u8>,
<C::ElementSize as Add>::Output: Add<asn1::MaxOverhead> + ArrayLength<u8>,
{
asn1::Signature::<C>::try_from(bytes).map(Into::into)
}
pub fn to_asn1(&self) -> asn1::Signature<C>
where
C::ElementSize: Add + ArrayLength<u8>,
asn1::MaxSize<C>: ArrayLength<u8>,
<C::ElementSize as Add>::Output: Add<asn1::MaxOverhead> + ArrayLength<u8>,
{
asn1::Signature::from_scalars(self.r(), self.s())
}
pub fn r(&self) -> &ElementBytes<C> {
ElementBytes::<C>::from_slice(&self.bytes[..C::ElementSize::to_usize()])
}
pub fn s(&self) -> &ElementBytes<C> {
ElementBytes::<C>::from_slice(&self.bytes[C::ElementSize::to_usize()..])
}
}
impl<C> Signature<C>
where
C: Curve + Arithmetic,
C::Scalar: NormalizeLow,
SignatureSize<C>: ArrayLength<u8>,
{
pub fn normalize_s(&mut self) -> Result<bool, Error> {
let s_bytes = GenericArray::from_mut_slice(&mut self.bytes[C::ElementSize::to_usize()..]);
let s_option = C::Scalar::from_bytes(s_bytes);
if s_option.is_some().into() {
let (s_low, was_high) = s_option.unwrap().normalize_low();
if was_high {
s_bytes.copy_from_slice(&s_low.into());
Ok(true)
} else {
Ok(false)
}
} else {
Err(Error::new())
}
}
}
impl<C: Curve> signature::Signature for Signature<C>
where
SignatureSize<C>: ArrayLength<u8>,
{
fn from_bytes(bytes: &[u8]) -> Result<Self, Error> {
Self::try_from(bytes)
}
}
impl<C: Curve> AsRef<[u8]> for Signature<C>
where
SignatureSize<C>: ArrayLength<u8>,
{
fn as_ref(&self) -> &[u8] {
self.bytes.as_slice()
}
}
impl<C: Curve> Copy for Signature<C>
where
SignatureSize<C>: ArrayLength<u8>,
<SignatureSize<C> as ArrayLength<u8>>::ArrayType: Copy,
{
}
impl<C: Curve> Debug for Signature<C>
where
SignatureSize<C>: ArrayLength<u8>,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"ecdsa::Signature<{:?}>({:?})",
C::default(),
self.as_ref()
)
}
}
impl<C: Curve> TryFrom<&[u8]> for Signature<C>
where
SignatureSize<C>: ArrayLength<u8>,
{
type Error = Error;
fn try_from(bytes: &[u8]) -> Result<Self, Error> {
if bytes.len() == <SignatureSize<C>>::to_usize() {
Ok(Self {
bytes: GenericArray::clone_from_slice(bytes),
})
} else {
Err(Error::new())
}
}
}
impl<C> From<asn1::Signature<C>> for Signature<C>
where
C: Curve,
C::ElementSize: Add + ArrayLength<u8>,
asn1::MaxSize<C>: ArrayLength<u8>,
<C::ElementSize as Add>::Output: Add<asn1::MaxOverhead> + ArrayLength<u8>,
{
fn from(doc: asn1::Signature<C>) -> Signature<C> {
let mut bytes = SignatureBytes::<C>::default();
let scalar_size = C::ElementSize::to_usize();
let r_begin = scalar_size.checked_sub(doc.r().len()).unwrap();
let s_begin = bytes.len().checked_sub(doc.s().len()).unwrap();
bytes[r_begin..scalar_size].copy_from_slice(doc.r());
bytes[s_begin..].copy_from_slice(doc.s());
Signature { bytes }
}
}
pub trait NormalizeLow: Sized {
fn normalize_low(&self) -> (Self, bool);
}