@@ -15,28 +15,28 @@ extern crate std;
1515#[ cfg( feature = "rand_core" ) ]
1616pub use rand_core;
1717
18- pub use generic_array ;
19- pub use generic_array :: typenum;
18+ pub use hybrid_array as array ;
19+ pub use hybrid_array :: typenum;
2020
2121use core:: fmt;
22- use generic_array :: { typenum:: Unsigned , ArrayLength , GenericArray } ;
22+ use hybrid_array :: { typenum:: Unsigned , Array , ArraySize , ByteArray } ;
2323#[ cfg( feature = "rand_core" ) ]
2424use rand_core:: CryptoRngCore ;
2525
2626/// Block on which [`BlockSizeUser`] implementors operate.
27- pub type Block < B > = GenericArray < u8 , <B as BlockSizeUser >:: BlockSize > ;
27+ pub type Block < B > = ByteArray < <B as BlockSizeUser >:: BlockSize > ;
2828
2929/// Parallel blocks on which [`ParBlocksSizeUser`] implementors operate.
30- pub type ParBlocks < T > = GenericArray < Block < T > , <T as ParBlocksSizeUser >:: ParBlocksSize > ;
30+ pub type ParBlocks < T > = Array < Block < T > , <T as ParBlocksSizeUser >:: ParBlocksSize > ;
3131
3232/// Output array of [`OutputSizeUser`] implementors.
33- pub type Output < T > = GenericArray < u8 , <T as OutputSizeUser >:: OutputSize > ;
33+ pub type Output < T > = ByteArray < <T as OutputSizeUser >:: OutputSize > ;
3434
3535/// Key used by [`KeySizeUser`] implementors.
36- pub type Key < B > = GenericArray < u8 , <B as KeySizeUser >:: KeySize > ;
36+ pub type Key < B > = ByteArray < <B as KeySizeUser >:: KeySize > ;
3737
3838/// Initialization vector (nonce) used by [`IvSizeUser`] implementors.
39- pub type Iv < B > = GenericArray < u8 , <B as IvSizeUser >:: IvSize > ;
39+ pub type Iv < B > = ByteArray < <B as IvSizeUser >:: IvSize > ;
4040
4141/// Types which process data in blocks.
4242pub trait BlockSizeUser {
@@ -59,12 +59,12 @@ impl<T: BlockSizeUser> BlockSizeUser for &mut T {
5959}
6060
6161/// Trait implemented for supported block sizes, i.e. for types from `U1` to `U255`.
62- pub trait BlockSizes : ArrayLength < u8 > + sealed:: BlockSizes + ' static { }
62+ pub trait BlockSizes : ArraySize + sealed:: BlockSizes + ' static { }
6363
64- impl < T : ArrayLength < u8 > + sealed:: BlockSizes > BlockSizes for T { }
64+ impl < T : ArraySize + sealed:: BlockSizes > BlockSizes for T { }
6565
6666mod sealed {
67- use generic_array :: typenum:: { Gr , IsGreater , IsLess , Le , NonZero , Unsigned , U1 , U256 } ;
67+ use crate :: typenum:: { Gr , IsGreater , IsLess , Le , NonZero , Unsigned , U1 , U256 } ;
6868
6969 pub trait BlockSizes { }
7070
@@ -80,13 +80,13 @@ mod sealed {
8080/// Types which can process blocks in parallel.
8181pub trait ParBlocksSizeUser : BlockSizeUser {
8282 /// Number of blocks which can be processed in parallel.
83- type ParBlocksSize : ArrayLength < Block < Self > > ;
83+ type ParBlocksSize : ArraySize ;
8484}
8585
8686/// Types which return data with the given size.
8787pub trait OutputSizeUser {
8888 /// Size of the output in bytes.
89- type OutputSize : ArrayLength < u8 > + ' static ;
89+ type OutputSize : ArraySize + ' static ;
9090
9191 /// Return output size in bytes.
9292 #[ inline( always) ]
@@ -100,7 +100,7 @@ pub trait OutputSizeUser {
100100/// Generally it's used indirectly via [`KeyInit`] or [`KeyIvInit`].
101101pub trait KeySizeUser {
102102 /// Key size in bytes.
103- type KeySize : ArrayLength < u8 > + ' static ;
103+ type KeySize : ArraySize + ' static ;
104104
105105 /// Return key size in bytes.
106106 #[ inline( always) ]
@@ -114,7 +114,7 @@ pub trait KeySizeUser {
114114/// Generally it's used indirectly via [`KeyIvInit`] or [`InnerIvInit`].
115115pub trait IvSizeUser {
116116 /// Initialization vector size in bytes.
117- type IvSize : ArrayLength < u8 > + ' static ;
117+ type IvSize : ArraySize + ' static ;
118118
119119 /// Return IV size in bytes.
120120 #[ inline( always) ]
@@ -151,11 +151,9 @@ pub trait KeyInit: KeySizeUser + Sized {
151151 /// Create new value from variable size key.
152152 #[ inline]
153153 fn new_from_slice ( key : & [ u8 ] ) -> Result < Self , InvalidLength > {
154- if key. len ( ) != Self :: KeySize :: to_usize ( ) {
155- Err ( InvalidLength )
156- } else {
157- Ok ( Self :: new ( Key :: < Self > :: from_slice ( key) ) )
158- }
154+ <& Key < Self > >:: try_from ( key)
155+ . map ( Self :: new)
156+ . map_err ( |_| InvalidLength )
159157 }
160158
161159 /// Generate random key using the provided [`CryptoRngCore`].
@@ -177,16 +175,9 @@ pub trait KeyIvInit: KeySizeUser + IvSizeUser + Sized {
177175 /// Create new value from variable length key and nonce.
178176 #[ inline]
179177 fn new_from_slices ( key : & [ u8 ] , iv : & [ u8 ] ) -> Result < Self , InvalidLength > {
180- let key_len = Self :: KeySize :: USIZE ;
181- let iv_len = Self :: IvSize :: USIZE ;
182- if key. len ( ) != key_len || iv. len ( ) != iv_len {
183- Err ( InvalidLength )
184- } else {
185- Ok ( Self :: new (
186- Key :: < Self > :: from_slice ( key) ,
187- Iv :: < Self > :: from_slice ( iv) ,
188- ) )
189- }
178+ let key = <& Key < Self > >:: try_from ( key) . map_err ( |_| InvalidLength ) ?;
179+ let iv = <& Iv < Self > >:: try_from ( iv) . map_err ( |_| InvalidLength ) ?;
180+ Ok ( Self :: new ( key, iv) )
190181 }
191182
192183 /// Generate random key using the provided [`CryptoRngCore`].
@@ -237,11 +228,8 @@ pub trait InnerIvInit: InnerUser + IvSizeUser + Sized {
237228 /// Initialize value using `inner` and `iv` slice.
238229 #[ inline]
239230 fn inner_iv_slice_init ( inner : Self :: Inner , iv : & [ u8 ] ) -> Result < Self , InvalidLength > {
240- if iv. len ( ) != Self :: IvSize :: to_usize ( ) {
241- Err ( InvalidLength )
242- } else {
243- Ok ( Self :: inner_iv_init ( inner, Iv :: < Self > :: from_slice ( iv) ) )
244- }
231+ let iv = <& Iv < Self > >:: try_from ( iv) . map_err ( |_| InvalidLength ) ?;
232+ Ok ( Self :: inner_iv_init ( inner, iv) )
245233 }
246234
247235 /// Generate random IV using the provided [`CryptoRngCore`].
0 commit comments