@@ -5,7 +5,7 @@ use chacha20poly1305::{
5
5
aead:: { AeadInPlace , NewAead } ,
6
6
ChaCha20Poly1305 ,
7
7
} ;
8
- use curve25519_dalek:: { edwards :: EdwardsPoint , montgomery:: MontgomeryPoint , scalar :: Scalar } ;
8
+ use curve25519_dalek:: montgomery:: MontgomeryPoint ;
9
9
#[ cfg( feature = "pqclean_kyber1024" ) ]
10
10
use pqcrypto_kyber:: kyber1024;
11
11
#[ cfg( feature = "pqclean_kyber1024" ) ]
@@ -72,7 +72,7 @@ impl CryptoResolver for DefaultResolver {
72
72
/// Wraps x25519-dalek.
73
73
#[ derive( Default ) ]
74
74
struct Dh25519 {
75
- privkey : Scalar ,
75
+ privkey : [ u8 ; 32 ] ,
76
76
pubkey : [ u8 ; 32 ] ,
77
77
}
78
78
@@ -128,21 +128,11 @@ impl Random for OsRng {}
128
128
129
129
impl Dh25519 {
130
130
fn derive_pubkey ( & mut self ) {
131
- // TODO: use `MontgomeryPoint::mul_base` in final v4 release of curve25519-dalek
132
- // See dalek-cryptography/curve25519-dalek#503
133
- let point = EdwardsPoint :: mul_base ( & self . privkey ) . to_montgomery ( ) ;
131
+ let point = MontgomeryPoint :: mul_base_clamped ( self . privkey ) ;
134
132
self . pubkey = point. to_bytes ( ) ;
135
133
}
136
134
}
137
135
138
- fn clamp_scalar ( mut scalar : [ u8 ; 32 ] ) -> Scalar {
139
- scalar[ 0 ] &= 248 ;
140
- scalar[ 31 ] &= 127 ;
141
- scalar[ 31 ] |= 64 ;
142
-
143
- Scalar :: from_bits ( scalar)
144
- }
145
-
146
136
impl Dh for Dh25519 {
147
137
fn name ( & self ) -> & ' static str {
148
138
"25519"
@@ -159,14 +149,14 @@ impl Dh for Dh25519 {
159
149
fn set ( & mut self , privkey : & [ u8 ] ) {
160
150
let mut bytes = [ 0u8 ; 32 ] ;
161
151
copy_slices ! ( privkey, bytes) ;
162
- self . privkey = clamp_scalar ( bytes) ;
152
+ self . privkey = bytes;
163
153
self . derive_pubkey ( ) ;
164
154
}
165
155
166
156
fn generate ( & mut self , rng : & mut dyn Random ) {
167
157
let mut bytes = [ 0u8 ; 32 ] ;
168
158
rng. fill_bytes ( & mut bytes) ;
169
- self . privkey = clamp_scalar ( bytes) ;
159
+ self . privkey = bytes;
170
160
self . derive_pubkey ( ) ;
171
161
}
172
162
@@ -175,13 +165,13 @@ impl Dh for Dh25519 {
175
165
}
176
166
177
167
fn privkey ( & self ) -> & [ u8 ] {
178
- self . privkey . as_bytes ( )
168
+ & self . privkey
179
169
}
180
170
181
171
fn dh ( & self , pubkey : & [ u8 ] , out : & mut [ u8 ] ) -> Result < ( ) , Error > {
182
172
let mut pubkey_owned = [ 0u8 ; 32 ] ;
183
173
copy_slices ! ( & pubkey[ ..32 ] , pubkey_owned) ;
184
- let result = ( self . privkey * MontgomeryPoint ( pubkey_owned) ) . to_bytes ( ) ;
174
+ let result = MontgomeryPoint ( pubkey_owned) . mul_clamped ( self . privkey ) . to_bytes ( ) ;
185
175
copy_slices ! ( result, out) ;
186
176
Ok ( ( ) )
187
177
}
0 commit comments