33//!
44
55use crate :: { size:: KeySize , two} ;
6- use num_bigint:: BigUint ;
7- use num_traits:: Zero ;
6+ use crypto_bigint:: { BoxedUint , NonZero } ;
87use pkcs8:: der:: {
98 self , asn1:: UintRef , DecodeValue , Encode , EncodeValue , Header , Length , Reader , Sequence , Tag ,
109 Writer ,
@@ -18,19 +17,23 @@ use signature::rand_core::CryptoRngCore;
1817#[ must_use]
1918pub struct Components {
2019 /// Prime p
21- p : BigUint ,
20+ p : NonZero < BoxedUint > ,
2221
2322 /// Quotient q
24- q : BigUint ,
23+ q : NonZero < BoxedUint > ,
2524
2625 /// Generator g
27- g : BigUint ,
26+ g : NonZero < BoxedUint > ,
2827}
2928
3029impl Components {
3130 /// Construct the common components container from its inner values (p, q and g)
32- pub fn from_components ( p : BigUint , q : BigUint , g : BigUint ) -> signature:: Result < Self > {
33- if p < two ( ) || q < two ( ) || g. is_zero ( ) || g > p {
31+ pub fn from_components (
32+ p : NonZero < BoxedUint > ,
33+ q : NonZero < BoxedUint > ,
34+ g : NonZero < BoxedUint > ,
35+ ) -> signature:: Result < Self > {
36+ if * p < two ( ) || * q < two ( ) || g > p {
3437 return Err ( signature:: Error :: new ( ) ) ;
3538 }
3639
@@ -45,19 +48,19 @@ impl Components {
4548
4649 /// DSA prime p
4750 #[ must_use]
48- pub const fn p ( & self ) -> & BigUint {
51+ pub const fn p ( & self ) -> & NonZero < BoxedUint > {
4952 & self . p
5053 }
5154
5255 /// DSA quotient q
5356 #[ must_use]
54- pub const fn q ( & self ) -> & BigUint {
57+ pub const fn q ( & self ) -> & NonZero < BoxedUint > {
5558 & self . q
5659 }
5760
5861 /// DSA generator g
5962 #[ must_use]
60- pub const fn g ( & self ) -> & BigUint {
63+ pub const fn g ( & self ) -> & NonZero < BoxedUint > {
6164 & self . g
6265 }
6366}
@@ -68,25 +71,29 @@ impl<'a> DecodeValue<'a> for Components {
6871 let q = reader. decode :: < UintRef < ' _ > > ( ) ?;
6972 let g = reader. decode :: < UintRef < ' _ > > ( ) ?;
7073
71- let p = BigUint :: from_bytes_be ( p. as_bytes ( ) ) ;
72- let q = BigUint :: from_bytes_be ( q. as_bytes ( ) ) ;
73- let g = BigUint :: from_bytes_be ( g. as_bytes ( ) ) ;
74+ let p = BoxedUint :: from_be_slice ( p. as_bytes ( ) , ( p. as_bytes ( ) . len ( ) * 8 ) as u32 ) . unwrap ( ) ;
75+ let q = BoxedUint :: from_be_slice ( q. as_bytes ( ) , ( q. as_bytes ( ) . len ( ) * 8 ) as u32 ) . unwrap ( ) ;
76+ let g = BoxedUint :: from_be_slice ( g. as_bytes ( ) , ( g. as_bytes ( ) . len ( ) * 8 ) as u32 ) . unwrap ( ) ;
77+
78+ let p = NonZero :: new ( p) . unwrap ( ) ;
79+ let q = NonZero :: new ( q) . unwrap ( ) ;
80+ let g = NonZero :: new ( g) . unwrap ( ) ;
7481
7582 Self :: from_components ( p, q, g) . map_err ( |_| Tag :: Integer . value_error ( ) )
7683 }
7784}
7885
7986impl EncodeValue for Components {
8087 fn value_len ( & self ) -> der:: Result < Length > {
81- UintRef :: new ( & self . p . to_bytes_be ( ) ) ?. encoded_len ( ) ?
82- + UintRef :: new ( & self . q . to_bytes_be ( ) ) ?. encoded_len ( ) ?
83- + UintRef :: new ( & self . g . to_bytes_be ( ) ) ?. encoded_len ( ) ?
88+ UintRef :: new ( & self . p . to_be_bytes ( ) ) ?. encoded_len ( ) ?
89+ + UintRef :: new ( & self . q . to_be_bytes ( ) ) ?. encoded_len ( ) ?
90+ + UintRef :: new ( & self . g . to_be_bytes ( ) ) ?. encoded_len ( ) ?
8491 }
8592
8693 fn encode_value ( & self , writer : & mut impl Writer ) -> der:: Result < ( ) > {
87- UintRef :: new ( & self . p . to_bytes_be ( ) ) ?. encode ( writer) ?;
88- UintRef :: new ( & self . q . to_bytes_be ( ) ) ?. encode ( writer) ?;
89- UintRef :: new ( & self . g . to_bytes_be ( ) ) ?. encode ( writer) ?;
94+ UintRef :: new ( & self . p . to_be_bytes ( ) ) ?. encode ( writer) ?;
95+ UintRef :: new ( & self . q . to_be_bytes ( ) ) ?. encode ( writer) ?;
96+ UintRef :: new ( & self . g . to_be_bytes ( ) ) ?. encode ( writer) ?;
9097 Ok ( ( ) )
9198 }
9299}
0 commit comments