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
use {c, core, der, digest, error};
use untrusted;
pub struct RSAPadding {
digest_alg: &'static digest::Algorithm,
digestinfo_prefix: &'static [u8],
}
macro_rules! rsa_pkcs1_padding {
( $PADDING_ALGORITHM:ident, $digest_alg:expr, $digestinfo_prefix:expr,
$doc_str:expr ) => {
#[doc=$doc_str]
pub static $PADDING_ALGORITHM: RSAPadding = RSAPadding {
digest_alg: $digest_alg,
digestinfo_prefix: $digestinfo_prefix,
};
}
}
rsa_pkcs1_padding!(RSA_PKCS1_SHA1, &digest::SHA1,
&SHA1_PKCS1_DIGESTINFO_PREFIX,
"Signing using RSA with PKCS#1 1.5 padding and SHA-1.");
rsa_pkcs1_padding!(RSA_PKCS1_SHA256, &digest::SHA256,
&SHA256_PKCS1_DIGESTINFO_PREFIX,
"Signing using RSA with PKCS#1 1.5 padding and SHA-256.");
rsa_pkcs1_padding!(RSA_PKCS1_SHA384, &digest::SHA384,
&SHA384_PKCS1_DIGESTINFO_PREFIX,
"Signing using RSA with PKCS#1 1.5 padding and SHA3846.");
rsa_pkcs1_padding!(RSA_PKCS1_SHA512, &digest::SHA512,
&SHA512_PKCS1_DIGESTINFO_PREFIX,
"Signing using RSA with PKCS#1 1.5 padding and SHA-512.");
macro_rules! pkcs1_digestinfo_prefix {
( $name:ident, $digest_len:expr, $digest_oid_len:expr,
[ $( $digest_oid:expr ),* ] ) => {
static $name: [u8; 2 + 8 + $digest_oid_len] = [
der::Tag::Sequence as u8, 8 + $digest_oid_len + $digest_len,
der::Tag::Sequence as u8, 2 + $digest_oid_len + 2,
der::Tag::OID as u8, $digest_oid_len, $( $digest_oid ),*,
der::Tag::Null as u8, 0,
der::Tag::OctetString as u8, $digest_len,
];
}
}
macro_rules! pkcs1_digestinfo_prefix {
( $name:ident, $digest_len:expr, $digest_oid_len:expr,
[ $( $digest_oid:expr ),* ] ) => {
static $name: [u8; 2 + 8 + $digest_oid_len] = [
der::Tag::Sequence as u8, 8 + $digest_oid_len + $digest_len,
der::Tag::Sequence as u8, 2 + $digest_oid_len + 2,
der::Tag::OID as u8, $digest_oid_len, $( $digest_oid ),*,
der::Tag::Null as u8, 0,
der::Tag::OctetString as u8, $digest_len,
];
}
}
pkcs1_digestinfo_prefix!(
SHA1_PKCS1_DIGESTINFO_PREFIX, 20, 5, [ 0x2b, 0x0e, 0x03, 0x02, 0x1a ]);
pkcs1_digestinfo_prefix!(
SHA256_PKCS1_DIGESTINFO_PREFIX, 32, 9,
[ 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01 ]);
pkcs1_digestinfo_prefix!(
SHA384_PKCS1_DIGESTINFO_PREFIX, 48, 9,
[ 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x02 ]);
pkcs1_digestinfo_prefix!(
SHA512_PKCS1_DIGESTINFO_PREFIX, 64, 9,
[ 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03 ]);
pub struct RSAParameters {
padding_alg: &'static RSAPadding,
min_bits: usize,
}
fn parse_public_key(input: untrusted::Input)
-> Result<(untrusted::Input, untrusted::Input),
error::Unspecified> {
input.read_all(error::Unspecified, |input| {
der::nested(input, der::Tag::Sequence, error::Unspecified, |input| {
let n = try!(der::positive_integer(input));
let e = try!(der::positive_integer(input));
Ok((n, e))
})
})
}
struct PositiveInteger {
value: Option<*mut BIGNUM>,
}
#[allow(unsafe_code)]
impl PositiveInteger {
#[cfg(feature = "rsa_signing")]
fn from_der(input: &mut untrusted::Reader)
-> Result<PositiveInteger, error::Unspecified> {
Self::from_be_bytes(try!(der::positive_integer(input)))
}
fn from_be_bytes(input: untrusted::Input)
-> Result<PositiveInteger, error::Unspecified> {
if input.len() == 0 {
return Err(error::Unspecified);
}
if untrusted::Reader::new(input).peek(0) {
return Err(error::Unspecified);
}
let res = unsafe {
GFp_BN_bin2bn(input.as_slice_less_safe().as_ptr(), input.len(),
core::ptr::null_mut())
};
if res.is_null() {
return Err(error::Unspecified);
}
Ok(PositiveInteger { value: Some(res) })
}
unsafe fn as_ref<'a>(&'a self) -> &'a BIGNUM { &*self.value.unwrap() }
#[cfg(feature = "rsa_signing")]
fn into_raw(&mut self) -> *mut BIGNUM {
let res = self.value.unwrap();
self.value = None;
res
}
}
#[allow(unsafe_code)]
impl Drop for PositiveInteger {
fn drop(&mut self) {
match self.value {
Some(val) => unsafe {
GFp_BN_free(val);
},
None => {},
}
}
}
#[cfg(feature = "rsa_signing")]
#[allow(non_camel_case_types)]
enum BN_MONT_CTX {}
pub mod verification;
enum BIGNUM {}
#[cfg(feature = "rsa_signing")]
pub mod signing;
extern {
fn GFp_BN_bin2bn(in_: *const u8, len: c::size_t, ret: *mut BIGNUM)
-> *mut BIGNUM;
fn GFp_BN_free(bn: *mut BIGNUM);
}
#[cfg(feature = "rsa_signing")]
extern {
fn GFp_BN_MONT_CTX_free(mont: *mut BN_MONT_CTX);
}
#[cfg(test)]
mod tests {
use super::PositiveInteger;
use untrusted;
#[test]
fn test_positive_integer_from_be_bytes_empty() {
assert!(PositiveInteger::from_be_bytes(
untrusted::Input::from(&[])).is_err());
}
#[test]
fn test_positive_integer_from_be_bytes_zero() {
assert!(PositiveInteger::from_be_bytes(
untrusted::Input::from(&[0])).is_err());
assert!(PositiveInteger::from_be_bytes(
untrusted::Input::from(&[0, 0])).is_err());
assert!(PositiveInteger::from_be_bytes(
untrusted::Input::from(&[0, 1])).is_err());
assert!(PositiveInteger::from_be_bytes(
untrusted::Input::from(&[1])).is_ok());
assert!(PositiveInteger::from_be_bytes(
untrusted::Input::from(&[1, 0])).is_ok());
}
}