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
#![allow(unsafe_code)]
use {bssl, c, digest, error, private, signature};
use super::{BIGNUM, PositiveInteger, RSAParameters, parse_public_key};
use untrusted;
impl signature::VerificationAlgorithm for RSAParameters {
fn verify(&self, public_key: untrusted::Input, msg: untrusted::Input,
signature: untrusted::Input)
-> Result<(), error::Unspecified> {
let public_key = try!(parse_public_key(public_key));
verify_rsa(self, public_key, msg, signature)
}
}
impl private::Private for RSAParameters {}
macro_rules! rsa_pkcs1 {
( $VERIFY_ALGORITHM:ident, $min_bits:expr, $PADDING_ALGORITHM:expr,
$doc_str:expr ) => {
#[doc=$doc_str]
pub static $VERIFY_ALGORITHM: RSAParameters =
RSAParameters {
padding_alg: $PADDING_ALGORITHM,
min_bits: $min_bits,
};
}
}
rsa_pkcs1!(RSA_PKCS1_2048_8192_SHA1, 2048, &super::RSA_PKCS1_SHA1,
"Verification of signatures using RSA keys of 2048-8192 bits,
PKCS#1.5 padding, and SHA-1.");
rsa_pkcs1!(RSA_PKCS1_2048_8192_SHA256, 2048, &super::RSA_PKCS1_SHA256,
"Verification of signatures using RSA keys of 2048-8192 bits,
PKCS#1.5 padding, and SHA-256.");
rsa_pkcs1!(RSA_PKCS1_2048_8192_SHA384, 2048, &super::RSA_PKCS1_SHA384,
"Verification of signatures using RSA keys of 2048-8192 bits,
PKCS#1.5 padding, and SHA-384.");
rsa_pkcs1!(RSA_PKCS1_2048_8192_SHA512, 2048, &super::RSA_PKCS1_SHA512,
"Verification of signatures using RSA keys of 2048-8192 bits,
PKCS#1.5 padding, and SHA-512.");
rsa_pkcs1!(RSA_PKCS1_3072_8192_SHA384, 3072, &super::RSA_PKCS1_SHA384,
"Verification of signatures using RSA keys of 3072-8192 bits,
PKCS#1.5 padding, and SHA-384.");
pub fn verify_rsa(params: &RSAParameters,
(n, e): (untrusted::Input, untrusted::Input),
msg: untrusted::Input, signature: untrusted::Input)
-> Result<(), error::Unspecified> {
const MAX_BITS: usize = 8192;
let signature = signature.as_slice_less_safe();
let mut decoded = [0u8; (MAX_BITS + 7) / 8];
if signature.len() > decoded.len() {
return Err(error::Unspecified);
}
let n = try!(PositiveInteger::from_be_bytes(n));
let e = try!(PositiveInteger::from_be_bytes(e));
let decoded = &mut decoded[..signature.len()];
try!(bssl::map_result(unsafe {
GFp_rsa_public_decrypt(decoded.as_mut_ptr(), decoded.len(), n.as_ref(),
e.as_ref(), signature.as_ptr(), signature.len(),
params.min_bits, MAX_BITS)
}));
untrusted::Input::from(decoded).read_all(error::Unspecified, |decoded| {
if try!(decoded.read_byte()) != 0 || try!(decoded.read_byte()) != 1 {
return Err(error::Unspecified);
}
let mut ps_len = 0;
loop {
match try!(decoded.read_byte()) {
0xff => {
ps_len += 1;
},
0x00 => {
break;
},
_ => {
return Err(error::Unspecified);
},
}
}
if ps_len < 8 {
return Err(error::Unspecified);
}
let decoded_digestinfo_prefix = try!(decoded.skip_and_get_input(
params.padding_alg.digestinfo_prefix.len()));
if decoded_digestinfo_prefix != params.padding_alg.digestinfo_prefix {
return Err(error::Unspecified);
}
let digest_alg = params.padding_alg.digest_alg;
let decoded_digest =
try!(decoded.skip_and_get_input(digest_alg.output_len));
let digest = digest::digest(digest_alg, msg.as_slice_less_safe());
if decoded_digest != digest.as_ref() {
return Err(error::Unspecified);
}
Ok(())
})
}
extern {
fn GFp_rsa_public_decrypt(out: *mut u8, out_len: c::size_t,
public_key_n: *const BIGNUM,
public_key_e: *const BIGNUM,
ciphertext: *const u8,
ciphertext_len: c::size_t, min_bits: c::size_t,
max_bits: c::size_t) -> c::int;
}
#[cfg(test)]
mod tests {
use {der, error, signature, test};
use super::*;
use untrusted;
#[test]
fn test_signature_rsa_pkcs1_verify() {
test::from_file("src/rsa/rsa_pkcs1_verify_tests.txt",
|section, test_case| {
assert_eq!(section, "");
let digest_name = test_case.consume_string("Digest");
let alg = if digest_name == "SHA1" {
&RSA_PKCS1_2048_8192_SHA1
} else if digest_name == "SHA256" {
&RSA_PKCS1_2048_8192_SHA256
} else if digest_name == "SHA384" {
&RSA_PKCS1_2048_8192_SHA384
} else if digest_name == "SHA512" {
&RSA_PKCS1_2048_8192_SHA512
} else {
panic!("Unsupported digest: {}", digest_name);
};
let public_key = test_case.consume_bytes("Key");
let public_key = untrusted::Input::from(&public_key);
assert!(public_key.read_all(error::Unspecified, |input| {
der::nested(input, der::Tag::Sequence, error::Unspecified,
|input| {
let _ = try!(der::positive_integer(input));
let _ = try!(der::positive_integer(input));
Ok(())
})
}).is_ok());
let msg = test_case.consume_bytes("Msg");
let msg = untrusted::Input::from(&msg);
let sig = test_case.consume_bytes("Sig");
let sig = untrusted::Input::from(&sig);
let expected_result = test_case.consume_string("Result");
let actual_result = signature::verify(alg, public_key, msg, sig);
assert_eq!(actual_result.is_ok(), expected_result == "P");
Ok(())
});
}
#[test]
fn test_signature_rsa_primitive_verification() {
test::from_file("src/rsa/rsa_primitive_verify_tests.txt",
|section, test_case| {
assert_eq!(section, "");
let n = test_case.consume_bytes("n");
let e = test_case.consume_bytes("e");
let msg = test_case.consume_bytes("Msg");
let sig = test_case.consume_bytes("Sig");
let expected = test_case.consume_string("Result");
let result = verify_rsa(&RSA_PKCS1_2048_8192_SHA256,
(untrusted::Input::from(&n),
untrusted::Input::from(&e)),
untrusted::Input::from(&msg),
untrusted::Input::from(&sig));
assert_eq!(result.is_ok(), expected == "Pass");
Ok(())
})
}
}