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
use crate::{
Any, ByteSlice, Encodable, Encoder, Error, ErrorKind, Header, Length, Result, Tag, Tagged,
};
use core::{convert::TryFrom, marker::PhantomData};
use typenum::{NonZero, Unsigned};
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd)]
#[cfg_attr(docsrs, doc(cfg(feature = "big-uint")))]
pub struct BigUInt<'a, N: Unsigned + NonZero> {
inner: ByteSlice<'a>,
size: PhantomData<N>,
}
impl<'a, N> BigUInt<'a, N>
where
N: Unsigned + NonZero,
{
pub fn new(mut bytes: &'a [u8]) -> Result<Self> {
while bytes.get(0).cloned() == Some(0) {
bytes = &bytes[1..];
}
if bytes.len() > N::to_usize() {
return Err(ErrorKind::Length { tag: Self::TAG }.into());
}
ByteSlice::new(bytes)
.map(|inner| Self {
inner,
size: PhantomData,
})
.map_err(|_| ErrorKind::Length { tag: Self::TAG }.into())
}
pub fn as_bytes(&self) -> &'a [u8] {
self.inner.as_bytes()
}
pub fn len(&self) -> Length {
self.inner.len()
}
pub fn is_empty(&self) -> bool {
self.inner.is_empty()
}
fn inner_len(self) -> Result<Length> {
self.len()
+ match self.inner.as_ref().get(0).cloned() {
Some(n) if n >= 0x80 => 1u8,
None => 1u8,
_ => 0u8,
}
}
}
impl<'a, N> From<&BigUInt<'a, N>> for BigUInt<'a, N>
where
N: Unsigned + NonZero,
{
fn from(value: &BigUInt<'a, N>) -> BigUInt<'a, N> {
*value
}
}
impl<'a, N> TryFrom<Any<'a>> for BigUInt<'a, N>
where
N: Unsigned + NonZero,
{
type Error = Error;
fn try_from(any: Any<'a>) -> Result<BigUInt<'a, N>> {
any.tag().assert_eq(Tag::Integer)?;
let mut bytes = any.as_bytes();
if let Some(byte) = bytes.get(0).cloned() {
if byte > 0x80 {
return Err(ErrorKind::Value { tag: Self::TAG }.into());
}
}
if bytes.len() > N::to_usize() {
if bytes.len() != N::to_usize().checked_add(1).unwrap() {
return Err(ErrorKind::Length { tag: Self::TAG }.into());
}
if bytes.get(0).cloned() != Some(0) {
return Err(ErrorKind::Value { tag: Self::TAG }.into());
}
bytes = &bytes[1..];
}
Self::new(bytes)
}
}
impl<'a, N> Encodable for BigUInt<'a, N>
where
N: Unsigned + NonZero,
{
fn encoded_len(&self) -> Result<Length> {
self.inner_len()?.for_tlv()
}
fn encode(&self, encoder: &mut Encoder<'_>) -> Result<()> {
Header::new(Self::TAG, self.inner_len()?)?.encode(encoder)?;
if self.inner_len()? > self.len() {
encoder.byte(0)?;
}
encoder.bytes(self.as_bytes())
}
}
impl<'a, N> Tagged for BigUInt<'a, N>
where
N: Unsigned + NonZero,
{
const TAG: Tag = Tag::Integer;
}
#[cfg(test)]
mod tests {
use super::BigUInt;
use crate::{asn1::integer::tests::*, Any, Decodable, ErrorKind, Result, Tag};
use core::convert::TryInto;
type BigU8<'a> = BigUInt<'a, typenum::U1>;
type BigU16<'a> = BigUInt<'a, typenum::U2>;
fn parse_bigu8_from_any(bytes: &[u8]) -> Result<BigU8<'_>> {
Any::new(Tag::Integer, bytes)?.try_into()
}
#[test]
fn decode_empty() {
let x = parse_bigu8_from_any(&[]).unwrap();
assert_eq!(x.as_bytes(), &[]);
}
#[test]
fn decode_bigu8() {
assert!(BigU8::from_der(I0_BYTES).unwrap().is_empty());
assert_eq!(&[127], BigU8::from_der(I127_BYTES).unwrap().as_bytes());
assert_eq!(&[128], BigU8::from_der(I128_BYTES).unwrap().as_bytes());
assert_eq!(&[255], BigU8::from_der(I255_BYTES).unwrap().as_bytes());
}
#[test]
fn decode_bigu16() {
assert!(BigU16::from_der(I0_BYTES).unwrap().is_empty());
assert_eq!(&[127], BigU16::from_der(I127_BYTES).unwrap().as_bytes());
assert_eq!(&[128], BigU16::from_der(I128_BYTES).unwrap().as_bytes());
assert_eq!(&[255], BigU16::from_der(I255_BYTES).unwrap().as_bytes());
assert_eq!(
&[0x01, 0x00],
BigU16::from_der(I256_BYTES).unwrap().as_bytes()
);
assert_eq!(
&[0x7F, 0xFF],
BigU16::from_der(I32767_BYTES).unwrap().as_bytes()
);
}
#[test]
fn reject_oversize_without_extra_zero() {
let err = parse_bigu8_from_any(&[0x81]).err().unwrap();
assert_eq!(err.kind(), ErrorKind::Value { tag: Tag::Integer });
}
}