Skip to content

Commit aa40d4e

Browse files
committed
[WIP] der: replace typenum with const generics for BigUInt
BigUInt was previously using `typenum` to define the size of an integer. This is necessary because it maps to a signed ASN.1 INTEGER and needs to add/remove a leading 0 when the first octet is >0x7f. This is perhaps the one thing in RustCrypto right now where I feel that `min_const_generics` stabilization might actually help: it's a trivial usage, doesn't involve `generic-array` or associated constants of a trait definition, it's just using the type system to be generic around an integer value. This eliminates `typenum` as a dependency and with it feature gating `big-uint`. Presently requires `beta` until 1.51 ships on March 25th.
1 parent 910873b commit aa40d4e

File tree

13 files changed

+36
-109
lines changed

13 files changed

+36
-109
lines changed

.github/workflows/der.yml

+4-7
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ jobs:
2323
strategy:
2424
matrix:
2525
rust:
26-
- 1.46.0 # MSRV
27-
- stable
26+
- beta # MSRV
27+
#- stable
2828
target:
2929
- thumbv7em-none-eabi
3030
- wasm32-unknown-unknown
@@ -37,17 +37,15 @@ jobs:
3737
target: ${{ matrix.target }}
3838
override: true
3939
- run: cargo build --target ${{ matrix.target }} --release
40-
- run: cargo build --target ${{ matrix.target }} --release --features big-uint
4140
- run: cargo build --target ${{ matrix.target }} --release --features oid
42-
- run: cargo build --target ${{ matrix.target }} --release --features big-uint,oid
4341

4442
test:
4543
runs-on: ubuntu-latest
4644
strategy:
4745
matrix:
4846
rust:
49-
- 1.46.0 # MSRV
50-
- stable
47+
- beta # MSRV
48+
#- stable
5149
steps:
5250
- uses: actions/checkout@v1
5351
- uses: actions-rs/toolchain@v1
@@ -56,6 +54,5 @@ jobs:
5654
toolchain: ${{ matrix.rust }}
5755
override: true
5856
- run: cargo test --release
59-
- run: cargo test --release --features big-uint
6057
- run: cargo test --release --features oid
6158
- run: cargo test --release --all-features

.github/workflows/pkcs5.yml

+4-4
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ jobs:
2525
strategy:
2626
matrix:
2727
rust:
28-
- 1.47.0 # MSRV
29-
- stable
28+
- beta # MSRV
29+
#- stable
3030
target:
3131
- thumbv7em-none-eabi
3232
- wasm32-unknown-unknown
@@ -48,8 +48,8 @@ jobs:
4848
strategy:
4949
matrix:
5050
rust:
51-
- 1.47.0 # MSRV
52-
- stable
51+
- beta # MSRV
52+
#- stable
5353
steps:
5454
- uses: actions/checkout@v1
5555
- uses: actions-rs/toolchain@v1

.github/workflows/pkcs8.yml

+4-4
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ jobs:
2626
strategy:
2727
matrix:
2828
rust:
29-
- 1.47.0 # MSRV
30-
- stable
29+
- beta # MSRV
30+
#- stable
3131
target:
3232
- thumbv7em-none-eabi
3333
- wasm32-unknown-unknown
@@ -50,8 +50,8 @@ jobs:
5050
strategy:
5151
matrix:
5252
rust:
53-
- 1.47.0 # MSRV
54-
- stable
53+
- beta # MSRV
54+
#- stable
5555
steps:
5656
- uses: actions/checkout@v1
5757
- uses: actions-rs/toolchain@v1

.github/workflows/spki.yml

+4-4
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ jobs:
2525
strategy:
2626
matrix:
2727
rust:
28-
- 1.47.0 # MSRV
29-
- stable
28+
- beta # MSRV
29+
#- stable
3030
target:
3131
- thumbv7em-none-eabi
3232
- wasm32-unknown-unknown
@@ -45,8 +45,8 @@ jobs:
4545
strategy:
4646
matrix:
4747
rust:
48-
- 1.47.0 # MSRV
49-
- stable
48+
- beta # MSRV
49+
#- stable
5050
steps:
5151
- uses: actions/checkout@v1
5252
- uses: actions-rs/toolchain@v1

.github/workflows/workspace.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616
- uses: actions/checkout@v1
1717
- uses: actions-rs/toolchain@v1
1818
with:
19-
toolchain: 1.47.0 # Highest MSRV in repo
19+
toolchain: beta # Highest MSRV in repo
2020
components: clippy
2121
override: true
2222
profile: minimal

Cargo.lock

-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

base64ct/src/encoding.rs

+1
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ impl<T: Variant> Encoding for T {
231231

232232
fn encoded_len(bytes: &[u8]) -> usize {
233233
// TODO: replace with `unwrap_or` on stabilization
234+
#[allow(clippy::manual_unwrap_or)]
234235
match encoded_len_inner(bytes.len(), T::PADDED) {
235236
Some(v) => v,
236237
None => 0,

der/Cargo.toml

-2
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,13 @@ readme = "README.md"
1717
[dependencies]
1818
const-oid = { version = "0.4.4", optional = true, path = "../const-oid" }
1919
der_derive = { version = "0.2", optional = true, path = "derive" }
20-
typenum = { version = "1", optional = true }
2120

2221
[dev-dependencies]
2322
hex-literal = "0.3"
2423

2524
[features]
2625
alloc = []
2726
derive = ["der_derive"]
28-
big-uint = ["typenum"]
2927
oid = ["const-oid"]
3028
std = ["alloc"]
3129

der/src/asn1.rs

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
//! Includes built-in ASN.1 types and helper types for modeling ASN.1 concepts.
44
55
pub(crate) mod any;
6-
#[cfg(feature = "big-uint")]
76
pub(crate) mod big_uint;
87
pub(crate) mod bit_string;
98
pub(crate) mod boolean;

der/src/asn1/big_uint.rs

+13-66
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
use crate::{
44
Any, ByteSlice, Encodable, Encoder, Error, ErrorKind, Header, Length, Result, Tag, Tagged,
55
};
6-
use core::{convert::TryFrom, marker::PhantomData};
7-
use typenum::Unsigned;
6+
use core::convert::TryFrom;
87

98
/// "Big" unsigned ASN.1 `INTEGER` type.
109
///
@@ -19,16 +18,12 @@ use typenum::Unsigned;
1918
///
2019
/// Currently supported sizes are 1 - 512 bytes.
2120
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
22-
#[cfg_attr(docsrs, doc(cfg(feature = "big-uint")))]
23-
pub struct BigUInt<'a, N: BigUIntSize> {
21+
pub struct BigUInt<'a, const N: usize> {
2422
/// Inner value
2523
inner: ByteSlice<'a>,
26-
27-
/// Integer size in bytes
28-
size: PhantomData<N>,
2924
}
3025

31-
impl<'a, N: BigUIntSize> BigUInt<'a, N> {
26+
impl<'a, const N: usize> BigUInt<'a, N> {
3227
/// Create a new [`BigUInt`] from a byte slice.
3328
///
3429
/// Slice may be less than or equal to `N` bytes.
@@ -38,15 +33,12 @@ impl<'a, N: BigUIntSize> BigUInt<'a, N> {
3833
bytes = &bytes[1..];
3934
}
4035

41-
if bytes.len() > N::to_usize() {
36+
if bytes.len() > N {
4237
return Err(ErrorKind::Length { tag: Self::TAG }.into());
4338
}
4439

4540
ByteSlice::new(bytes)
46-
.map(|inner| Self {
47-
inner,
48-
size: PhantomData,
49-
})
41+
.map(|inner| Self { inner })
5042
.map_err(|_| ErrorKind::Length { tag: Self::TAG }.into())
5143
}
5244

@@ -86,13 +78,13 @@ impl<'a, N: BigUIntSize> BigUInt<'a, N> {
8678
}
8779
}
8880

89-
impl<'a, N: BigUIntSize> From<&BigUInt<'a, N>> for BigUInt<'a, N> {
81+
impl<'a, const N: usize> From<&BigUInt<'a, N>> for BigUInt<'a, N> {
9082
fn from(value: &BigUInt<'a, N>) -> BigUInt<'a, N> {
9183
*value
9284
}
9385
}
9486

95-
impl<'a, N: BigUIntSize> TryFrom<Any<'a>> for BigUInt<'a, N> {
87+
impl<'a, const N: usize> TryFrom<Any<'a>> for BigUInt<'a, N> {
9688
type Error = Error;
9789

9890
fn try_from(any: Any<'a>) -> Result<BigUInt<'a, N>> {
@@ -112,8 +104,8 @@ impl<'a, N: BigUIntSize> TryFrom<Any<'a>> for BigUInt<'a, N> {
112104
// The `INTEGER` type always encodes a signed value, so for unsigned
113105
// values the leading `0x00` byte may need to be removed.
114106
// TODO(tarcieri): validate leading 0 byte was required
115-
if bytes.len() > N::to_usize() {
116-
if bytes.len() != N::to_usize().checked_add(1).unwrap() {
107+
if bytes.len() > N {
108+
if bytes.len() != N.checked_add(1).unwrap() {
117109
return Err(ErrorKind::Length { tag: Self::TAG }.into());
118110
}
119111

@@ -128,7 +120,7 @@ impl<'a, N: BigUIntSize> TryFrom<Any<'a>> for BigUInt<'a, N> {
128120
}
129121
}
130122

131-
impl<'a, N: BigUIntSize> Encodable for BigUInt<'a, N> {
123+
impl<'a, const N: usize> Encodable for BigUInt<'a, N> {
132124
fn encoded_len(&self) -> Result<Length> {
133125
self.header()?.encoded_len()? + self.inner_len()?
134126
}
@@ -145,64 +137,19 @@ impl<'a, N: BigUIntSize> Encodable for BigUInt<'a, N> {
145137
}
146138
}
147139

148-
impl<'a, N: BigUIntSize> Tagged for BigUInt<'a, N> {
140+
impl<'a, const N: usize> Tagged for BigUInt<'a, N> {
149141
const TAG: Tag = Tag::Integer;
150142
}
151143

152-
/// Marker trait for allowed [`BigUInt`] sizes.
153-
#[cfg_attr(docsrs, doc(cfg(feature = "big-uint")))]
154-
pub trait BigUIntSize: Unsigned {}
155-
156-
macro_rules! impl_size {
157-
($($int:ident),+) => {
158-
$(impl BigUIntSize for typenum::consts::$int {})+
159-
};
160-
}
161-
162-
// Sizes supported by the current implementation (1 - 512 bytes)
163-
impl_size!(
164-
U1, U2, U3, U4, U5, U6, U7, U8, U9, U10, U11, U12, U13, U14, U15, U16, U17, U18, U19, U20, U21,
165-
U22, U23, U24, U25, U26, U27, U28, U29, U30, U31, U32, U33, U34, U35, U36, U37, U38, U39, U40,
166-
U41, U42, U43, U44, U45, U46, U47, U48, U49, U50, U51, U52, U53, U54, U55, U56, U57, U58, U59,
167-
U60, U61, U62, U63, U64, U65, U66, U67, U68, U69, U70, U71, U72, U73, U74, U75, U76, U77, U78,
168-
U79, U80, U81, U82, U83, U84, U85, U86, U87, U88, U89, U90, U91, U92, U93, U94, U95, U96, U97,
169-
U98, U99, U100, U101, U102, U103, U104, U105, U106, U107, U108, U109, U110, U111, U112, U113,
170-
U114, U115, U116, U117, U118, U119, U120, U121, U122, U123, U124, U125, U126, U127, U128, U129,
171-
U130, U131, U132, U133, U134, U135, U136, U137, U138, U139, U140, U141, U142, U143, U144, U145,
172-
U146, U147, U148, U149, U150, U151, U152, U153, U154, U155, U156, U157, U158, U159, U160, U161,
173-
U162, U163, U164, U165, U166, U167, U168, U169, U170, U171, U172, U173, U174, U175, U176, U177,
174-
U178, U179, U180, U181, U182, U183, U184, U185, U186, U187, U188, U189, U190, U191, U192, U193,
175-
U194, U195, U196, U197, U198, U199, U200, U201, U202, U203, U204, U205, U206, U207, U208, U209,
176-
U210, U211, U212, U213, U214, U215, U216, U217, U218, U219, U220, U221, U222, U223, U224, U225,
177-
U226, U227, U228, U229, U230, U231, U232, U233, U234, U235, U236, U237, U238, U239, U240, U241,
178-
U242, U243, U244, U245, U246, U247, U248, U249, U250, U251, U252, U253, U254, U255, U256, U257,
179-
U258, U259, U260, U261, U262, U263, U264, U265, U266, U267, U268, U269, U270, U271, U272, U273,
180-
U274, U275, U276, U277, U278, U279, U280, U281, U282, U283, U284, U285, U286, U287, U288, U289,
181-
U290, U291, U292, U293, U294, U295, U296, U297, U298, U299, U300, U301, U302, U303, U304, U305,
182-
U306, U307, U308, U309, U310, U311, U312, U313, U314, U315, U316, U317, U318, U319, U320, U321,
183-
U322, U323, U324, U325, U326, U327, U328, U329, U330, U331, U332, U333, U334, U335, U336, U337,
184-
U338, U339, U340, U341, U342, U343, U344, U345, U346, U347, U348, U349, U350, U351, U352, U353,
185-
U354, U355, U356, U357, U358, U359, U360, U361, U362, U363, U364, U365, U366, U367, U368, U369,
186-
U370, U371, U372, U373, U374, U375, U376, U377, U378, U379, U380, U381, U382, U383, U384, U385,
187-
U386, U387, U388, U389, U390, U391, U392, U393, U394, U395, U396, U397, U398, U399, U400, U401,
188-
U402, U403, U404, U405, U406, U407, U408, U409, U410, U411, U412, U413, U414, U415, U416, U417,
189-
U418, U419, U420, U421, U422, U423, U424, U425, U426, U427, U428, U429, U430, U431, U432, U433,
190-
U434, U435, U436, U437, U438, U439, U440, U441, U442, U443, U444, U445, U446, U447, U448, U449,
191-
U450, U451, U452, U453, U454, U455, U456, U457, U458, U459, U460, U461, U462, U463, U464, U465,
192-
U466, U467, U468, U469, U470, U471, U472, U473, U474, U475, U476, U477, U478, U479, U480, U481,
193-
U482, U483, U484, U485, U486, U487, U488, U489, U490, U491, U492, U493, U494, U495, U496, U497,
194-
U498, U499, U500, U501, U502, U503, U504, U505, U506, U507, U508, U509, U510, U511, U512
195-
);
196-
197144
#[cfg(test)]
198145
mod tests {
199146
use super::BigUInt;
200147
use crate::{asn1::integer::tests::*, Any, Decodable, ErrorKind, Result, Tag};
201148
use core::convert::TryInto;
202149

203150
// TODO(tarcieri): tests for more integer sizes
204-
type BigU8<'a> = BigUInt<'a, typenum::U1>;
205-
type BigU16<'a> = BigUInt<'a, typenum::U16>;
151+
type BigU8<'a> = BigUInt<'a, 1>;
152+
type BigU16<'a> = BigUInt<'a, 2>;
206153

207154
/// Parse a `BitU1` from an ASN.1 `Any` value to test decoding behaviors.
208155
fn parse_bigu8_from_any(bytes: &[u8]) -> Result<BigU8<'_>> {

der/src/decoder.rs

+3-11
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
//! DER decoder.
22
33
use crate::{
4-
Any, BitString, Choice, Decodable, ErrorKind, GeneralizedTime, Ia5String, Length, Null,
5-
OctetString, PrintableString, Result, Sequence, UtcTime, Utf8String,
4+
Any, BigUInt, BitString, Choice, Decodable, ErrorKind, GeneralizedTime, Ia5String, Length,
5+
Null, OctetString, PrintableString, Result, Sequence, UtcTime, Utf8String,
66
};
77
use core::convert::TryInto;
88

9-
#[cfg(feature = "big-uint")]
10-
use crate::{BigUInt, BigUIntSize};
11-
129
#[cfg(feature = "oid")]
1310
use crate::ObjectIdentifier;
1411

@@ -108,12 +105,7 @@ impl<'a> Decoder<'a> {
108105
}
109106

110107
/// Attempt to decode an ASN.1 `INTEGER` as a [`BigUInt`].
111-
#[cfg(feature = "big-uint")]
112-
#[cfg_attr(docsrs, doc(cfg(feature = "big-uint")))]
113-
pub fn big_uint<N>(&mut self) -> Result<BigUInt<'a, N>>
114-
where
115-
N: BigUIntSize,
116-
{
108+
pub fn big_uint<const N: usize>(&mut self) -> Result<BigUInt<'a, N>> {
117109
self.decode()
118110
}
119111

der/src/encodable.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub trait Encodable {
2222
fn encode_to_slice<'a>(&self, buf: &'a mut [u8]) -> Result<&'a [u8]> {
2323
let mut encoder = Encoder::new(buf);
2424
self.encode(&mut encoder)?;
25-
Ok(encoder.finish()?)
25+
encoder.finish()
2626
}
2727

2828
/// Encode this message as ASN.1 DER, appending it to the provided

der/src/lib.rs

+1-7
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,7 @@ mod tag;
354354
pub use crate::{
355355
asn1::{
356356
any::Any,
357+
big_uint::BigUInt,
357358
bit_string::BitString,
358359
choice::Choice,
359360
generalized_time::GeneralizedTime,
@@ -378,13 +379,6 @@ pub use crate::{
378379

379380
pub(crate) use crate::byte_slice::ByteSlice;
380381

381-
#[cfg(feature = "big-uint")]
382-
#[cfg_attr(docsrs, doc(cfg(feature = "big-uint")))]
383-
pub use {
384-
crate::asn1::big_uint::{BigUInt, BigUIntSize},
385-
typenum::consts,
386-
};
387-
388382
#[cfg(feature = "derive")]
389383
#[cfg_attr(docsrs, doc(cfg(feature = "derive")))]
390384
pub use der_derive::{Choice, Message};

0 commit comments

Comments
 (0)