File tree Expand file tree Collapse file tree 3 files changed +47
-1
lines changed Expand file tree Collapse file tree 3 files changed +47
-1
lines changed Original file line number Diff line number Diff line change @@ -260,7 +260,7 @@ impl<C> CryptoBox<C> {
260260 where
261261 C : Kdf ,
262262 {
263- let shared_secret = Zeroizing :: new ( secret_key . scalar * public_key. 0 ) ;
263+ let shared_secret = Zeroizing :: new ( public_key. 0 . mul_clamped ( secret_key . bytes ) ) ;
264264
265265 // Use HChaCha20 to create a uniformly random key from the shared secret
266266 let key = Zeroizing :: new ( C :: kdf (
Original file line number Diff line number Diff line change @@ -152,6 +152,7 @@ mod xsalsa20poly1305 {
152152#[ cfg( feature = "chacha20" ) ]
153153mod xchacha20poly1305 {
154154 use super :: * ;
155+ use aead:: Nonce ;
155156 use crypto_box:: ChaChaBox ;
156157 const CIPHERTEXT : & [ u8 ] = & hex ! (
157158 "0cd5ed093de698c8e410d0d451df2f5283057376b947b9b7392b956e5d675f309218acce8cf85f6c"
@@ -161,6 +162,24 @@ mod xchacha20poly1305 {
161162 ) ;
162163
163164 impl_tests ! ( ChaChaBox , PLAINTEXT , CIPHERTEXT ) ;
165+
166+ /// Implement test against shared secret being all zero
167+ #[ test]
168+ fn test_public_key_on_twist ( ) {
169+ let alice_private_key: [ u8 ; 32 ] =
170+ hex ! ( "78d37f87f45e76aae3b61e0f0b69db96d117f8b5fd8edc73785b64918d2c9f47" ) ;
171+ let bob_public_key: [ u8 ; 32 ] =
172+ hex ! ( "9ec59406d5f9fde97a5c49acb935023ae40fae1499c05d3277cfb9100487e5b8" ) ;
173+ let nonce = hex ! ( "979f38f433649e8aa1ad5a0334223f7c7dabc80231e8233a" ) ;
174+ let plaintext: & [ u8 ] = & [ ] ;
175+ let ciphertext_expected = hex ! ( "171e01986d83c429a2746212464d6782" ) ;
176+
177+ let ciphertext_computed = ChaChaBox :: new ( & bob_public_key. into ( ) , & alice_private_key. into ( ) )
178+ . encrypt ( Nonce :: < ChaChaBox > :: from_slice ( & nonce) , plaintext)
179+ . expect ( "Encryption should work" ) ;
180+
181+ assert_eq ! ( ciphertext_computed, ciphertext_expected)
182+ }
164183}
165184
166185#[ cfg( feature = "seal" ) ]
Original file line number Diff line number Diff line change @@ -32,6 +32,7 @@ const BOXZEROBYTES: usize = 16;
3232
3333pub fn generate ( ) {
3434 generate_xchacha20poly1305 ( ) ;
35+ generate_xchacha20poly1305_public_key_on_twist ( ) ;
3536}
3637
3738fn generate_xchacha20poly1305 ( ) {
@@ -53,3 +54,29 @@ fn generate_xchacha20poly1305() {
5354 hex:: encode( ct)
5455 ) ;
5556}
57+
58+ fn generate_xchacha20poly1305_public_key_on_twist ( ) {
59+ let alice_private_key: [ u8 ; 32 ] =
60+ hex ! ( "78d37f87f45e76aae3b61e0f0b69db96d117f8b5fd8edc73785b64918d2c9f47" ) ;
61+ let bob_public_key: [ u8 ; 32 ] =
62+ hex ! ( "9ec59406d5f9fde97a5c49acb935023ae40fae1499c05d3277cfb9100487e5b8" ) ;
63+ let nonce = hex ! ( "979f38f433649e8aa1ad5a0334223f7c7dabc80231e8233a" ) ;
64+ const PLAINTEXT : [ u8 ; 0 ] = [ ] ;
65+ let mut ct = [ 42u8 ; BOXZEROBYTES + PLAINTEXT . len ( ) ] ;
66+
67+ let ret = unsafe {
68+ libsodium_sys:: crypto_box_curve25519xchacha20poly1305_easy (
69+ ct. as_mut_ptr ( ) ,
70+ PLAINTEXT . as_ptr ( ) ,
71+ PLAINTEXT . len ( ) as u64 ,
72+ nonce. as_ptr ( ) ,
73+ bob_public_key. as_ptr ( ) ,
74+ alice_private_key. as_ptr ( ) ,
75+ )
76+ } ;
77+ assert_eq ! ( ret, 0 ) ;
78+ println ! (
79+ "CHACHA20POLY1305_BOX_CIPHERTEXT_PUBLIC_KEY_ON_TWIST: &[u8] = &hex!(\" {}\" );" ,
80+ hex:: encode( ct)
81+ ) ;
82+ }
You can’t perform that action at this time.
0 commit comments