1
1
//! Multiple precision integer
2
2
3
- use crate :: { Error , Result } ;
3
+ use crate :: { CheckedSum , Decode , Encode , Error , Reader , Result , Writer } ;
4
4
use alloc:: { boxed:: Box , vec:: Vec } ;
5
5
use core:: fmt;
6
- use encoding:: { CheckedSum , Decode , Encode , Reader , Writer } ;
6
+
7
+ #[ cfg( feature = "subtle" ) ]
7
8
use subtle:: { Choice , ConstantTimeEq } ;
8
- use zeroize:: Zeroize ;
9
9
10
- #[ cfg( any( feature = "dsa" , feature = "rsa" ) ) ]
10
+ #[ cfg( any( feature = "bigint" , feature = "zeroize" ) ) ]
11
+ use zeroize:: Zeroize ;
12
+ #[ cfg( feature = "bigint" ) ]
11
13
use zeroize:: Zeroizing ;
12
14
13
- /// Multiple precision integer, a.k.a. "mpint".
14
- ///
15
- /// This type is used for representing the big integer components of
16
- /// DSA and RSA keys.
15
+ /// Multiple precision integer, a.k.a. `mpint`.
17
16
///
18
17
/// Described in [RFC4251 § 5](https://datatracker.ietf.org/doc/html/rfc4251#section-5):
19
18
///
@@ -38,7 +37,8 @@ use zeroize::Zeroizing;
38
37
/// | 80 | `00 00 00 02 00 80`
39
38
/// |-1234 | `00 00 00 02 ed cc`
40
39
/// | -deadbeef | `00 00 00 05 ff 21 52 41 11`
41
- #[ derive( Clone , PartialOrd , Ord ) ]
40
+ #[ cfg_attr( not( feature = "subtle" ) , derive( Clone ) ) ]
41
+ #[ cfg_attr( feature = "subtle" , derive( Clone , Ord , PartialOrd ) ) ] // TODO: constant time (Partial)`Ord`?
42
42
pub struct Mpint {
43
43
/// Inner big endian-serialized integer value
44
44
inner : Box < [ u8 ] > ,
@@ -109,14 +109,17 @@ impl AsRef<[u8]> for Mpint {
109
109
}
110
110
}
111
111
112
+ #[ cfg( feature = "subtle" ) ]
112
113
impl ConstantTimeEq for Mpint {
113
114
fn ct_eq ( & self , other : & Self ) -> Choice {
114
115
self . as_ref ( ) . ct_eq ( other. as_ref ( ) )
115
116
}
116
117
}
117
118
119
+ #[ cfg( feature = "subtle" ) ]
118
120
impl Eq for Mpint { }
119
121
122
+ #[ cfg( feature = "subtle" ) ]
120
123
impl PartialEq for Mpint {
121
124
fn eq ( & self , other : & Self ) -> bool {
122
125
self . ct_eq ( other) . into ( )
@@ -132,11 +135,11 @@ impl Decode for Mpint {
132
135
}
133
136
134
137
impl Encode for Mpint {
135
- fn encoded_len ( & self ) -> encoding :: Result < usize > {
138
+ fn encoded_len ( & self ) -> Result < usize > {
136
139
[ 4 , self . as_bytes ( ) . len ( ) ] . checked_sum ( )
137
140
}
138
141
139
- fn encode ( & self , writer : & mut impl Writer ) -> encoding :: Result < ( ) > {
142
+ fn encode ( & self , writer : & mut impl Writer ) -> Result < ( ) > {
140
143
self . as_bytes ( ) . encode ( writer) ?;
141
144
Ok ( ( ) )
142
145
}
@@ -156,14 +159,15 @@ impl TryFrom<Box<[u8]>> for Mpint {
156
159
fn try_from ( bytes : Box < [ u8 ] > ) -> Result < Self > {
157
160
match & * bytes {
158
161
// Unnecessary leading 0
159
- [ 0x00 ] => Err ( Error :: FormatEncoding ) ,
162
+ [ 0x00 ] => Err ( Error :: MpintEncoding ) ,
160
163
// Unnecessary leading 0
161
- [ 0x00 , n, ..] if * n < 0x80 => Err ( Error :: FormatEncoding ) ,
164
+ [ 0x00 , n, ..] if * n < 0x80 => Err ( Error :: MpintEncoding ) ,
162
165
_ => Ok ( Self { inner : bytes } ) ,
163
166
}
164
167
}
165
168
}
166
169
170
+ #[ cfg( feature = "zeroize" ) ]
167
171
impl Zeroize for Mpint {
168
172
fn zeroize ( & mut self ) {
169
173
self . inner . zeroize ( ) ;
@@ -200,7 +204,7 @@ impl fmt::UpperHex for Mpint {
200
204
}
201
205
}
202
206
203
- #[ cfg( any ( feature = "dsa" , feature = "rsa" ) ) ]
207
+ #[ cfg( feature = "bigint" ) ]
204
208
impl TryFrom < bigint:: BigUint > for Mpint {
205
209
type Error = Error ;
206
210
@@ -209,7 +213,7 @@ impl TryFrom<bigint::BigUint> for Mpint {
209
213
}
210
214
}
211
215
212
- #[ cfg( any ( feature = "dsa" , feature = "rsa" ) ) ]
216
+ #[ cfg( feature = "bigint" ) ]
213
217
impl TryFrom < & bigint:: BigUint > for Mpint {
214
218
type Error = Error ;
215
219
@@ -219,7 +223,7 @@ impl TryFrom<&bigint::BigUint> for Mpint {
219
223
}
220
224
}
221
225
222
- #[ cfg( any ( feature = "dsa" , feature = "rsa" ) ) ]
226
+ #[ cfg( feature = "bigint" ) ]
223
227
impl TryFrom < Mpint > for bigint:: BigUint {
224
228
type Error = Error ;
225
229
@@ -228,15 +232,15 @@ impl TryFrom<Mpint> for bigint::BigUint {
228
232
}
229
233
}
230
234
231
- #[ cfg( any ( feature = "dsa" , feature = "rsa" ) ) ]
235
+ #[ cfg( feature = "bigint" ) ]
232
236
impl TryFrom < & Mpint > for bigint:: BigUint {
233
237
type Error = Error ;
234
238
235
239
fn try_from ( mpint : & Mpint ) -> Result < bigint:: BigUint > {
236
240
mpint
237
241
. as_positive_bytes ( )
238
242
. map ( bigint:: BigUint :: from_bytes_be)
239
- . ok_or ( Error :: Crypto )
243
+ . ok_or ( Error :: MpintEncoding )
240
244
}
241
245
}
242
246
0 commit comments