@@ -57,19 +57,17 @@ fn sign_pss_with_salt<T: Rng, SK: PrivateKey>(
57
57
digest : & mut dyn DynDigest ,
58
58
) -> Result < Vec < u8 > > {
59
59
let em_bits = priv_key. n ( ) . bits ( ) - 1 ;
60
- let mut em = vec ! [ 0 ; ( em_bits + 7 ) / 8 ] ;
61
- emsa_pss_encode ( & mut em, hashed, em_bits, salt, digest) ?;
60
+ let em = emsa_pss_encode ( hashed, em_bits, salt, digest) ?;
62
61
63
62
priv_key. raw_decryption_primitive ( blind_rng, & em, priv_key. size ( ) )
64
63
}
65
64
66
65
fn emsa_pss_encode (
67
- em : & mut [ u8 ] ,
68
66
m_hash : & [ u8 ] ,
69
67
em_bits : usize ,
70
68
salt : & [ u8 ] ,
71
69
hash : & mut dyn DynDigest ,
72
- ) -> Result < ( ) > {
70
+ ) -> Result < Vec < u8 > > {
73
71
// See [1], section 9.1.1
74
72
let h_len = hash. output_size ( ) ;
75
73
let s_len = salt. len ( ) ;
@@ -90,11 +88,9 @@ fn emsa_pss_encode(
90
88
return Err ( Error :: Internal ) ;
91
89
}
92
90
93
- if em. len ( ) != em_len {
94
- return Err ( Error :: Internal ) ;
95
- }
91
+ let mut em = vec ! [ 0 ; em_len] ;
96
92
97
- let ( db, h) = em. split_at_mut ( em_len - s_len - h_len - 2 + 1 + s_len ) ;
93
+ let ( db, h) = em. split_at_mut ( em_len - h_len - 1 ) ;
98
94
let h = & mut h[ ..( em_len - 1 ) - db. len ( ) ] ;
99
95
100
96
// 4. Generate a random octet string salt of length s_len; if s_len = 0,
@@ -136,7 +132,7 @@ fn emsa_pss_encode(
136
132
// 12. Let EM = maskedDB || H || 0xbc.
137
133
em[ em_len - 1 ] = 0xBC ;
138
134
139
- return Ok ( ( ) ) ;
135
+ Ok ( em )
140
136
}
141
137
142
138
fn emsa_pss_verify (
@@ -158,7 +154,7 @@ fn emsa_pss_verify(
158
154
159
155
// 3. If emLen < hLen + sLen + 2, output "inconsistent" and stop.
160
156
let em_len = em. len ( ) ; //(em_bits + 7) / 8;
161
- if em_len < h_len + 2 {
157
+ if em_len < h_len + s_len . unwrap_or_default ( ) + 2 {
162
158
return Err ( Error :: Verification ) ;
163
159
}
164
160
@@ -171,7 +167,7 @@ fn emsa_pss_verify(
171
167
// 5. Let maskedDB be the leftmost emLen - hLen - 1 octets of EM, and
172
168
// let H be the next hLen octets.
173
169
let ( db, h) = em. split_at_mut ( em_len - h_len - 1 ) ;
174
- let h = & mut h[ ..( em_len - 1 ) - ( em_len - h_len - 1 ) ] ;
170
+ let h = & mut h[ ..h_len] ;
175
171
176
172
// 6. If the leftmost 8 * em_len - em_bits bits of the leftmost octet in
177
173
// maskedDB are not all equal to zero, output "inconsistent" and
@@ -204,14 +200,11 @@ fn emsa_pss_verify(
204
200
// or if the octet at position emLen - hLen - sLen - 1 (the leftmost
205
201
// position is "position 1") does not have hexadecimal value 0x01,
206
202
// output "inconsistent" and stop.
207
- for e in & db[ ..em_len - h_len - s_len - 2 ] {
208
- if * e != 0x00 {
209
- return Err ( Error :: Verification ) ;
210
- }
211
- }
212
- if db[ em_len - h_len - s_len - 2 ] != 0x01 {
203
+ let ( zeroes, rest) = db. split_at ( em_len - h_len - s_len - 2 ) ;
204
+ if zeroes. iter ( ) . any ( |e| * e != 0x00 ) || rest[ 0 ] != 0x01 {
213
205
return Err ( Error :: Verification ) ;
214
206
}
207
+
215
208
s_len
216
209
}
217
210
} ;
@@ -233,7 +226,7 @@ fn emsa_pss_verify(
233
226
let h0 = hash. finalize_reset ( ) ;
234
227
235
228
// 14. If H = H', output "consistent." Otherwise, output "inconsistent."
236
- if Into :: < bool > :: into ( h0. ct_eq ( h) ) {
229
+ if h0. ct_eq ( h) . into ( ) {
237
230
Ok ( ( ) )
238
231
} else {
239
232
Err ( Error :: Verification )
0 commit comments