@@ -4,17 +4,19 @@ import (
44 "crypto/subtle"
55 "errors"
66
7- "github.com/aead/chacha20/chacha "
8- "github.com/aead /poly1305"
7+ "golang.org/x/crypto/chacha20 "
8+ "golang.org/x/crypto /poly1305"
99)
1010
1111const (
1212 // KeySize is what the name suggests
13- KeySize = 32
13+ KeySize = chacha20 . KeySize
1414 // NonceSize is what the name suggests
15- NonceSize = 24
15+ NonceSize = chacha20 . NonceSizeX
1616 // TagSize is what the name suggests
17- TagSize = 16
17+ TagSize = poly1305 .TagSize
18+ // BlockSize is what the name suggests
19+ BlockSize = 64
1820)
1921
2022// Seal does what the name suggests
@@ -26,22 +28,25 @@ func Seal(out, nonce, message, key []byte) []byte {
2628 panic ("unsupported key size" )
2729 }
2830
29- var firstBlock [64 ]byte
30- cipher , _ := chacha .NewCipher (nonce , key , 20 )
31+ var firstBlock [BlockSize ]byte
32+ cipher , err := chacha20 .NewUnauthenticatedCipher (key , nonce )
33+ if err != nil {
34+ panic (err )
35+ }
3136 cipher .XORKeyStream (firstBlock [:], firstBlock [:])
32- var polyKey [32 ]byte
33- copy (polyKey [:], firstBlock [:32 ])
37+ var polyKey [KeySize ]byte
38+ copy (polyKey [:], firstBlock [:KeySize ])
3439
3540 ret , out := sliceForAppend (out , TagSize + len (message ))
3641 firstMessageBlock := message
37- if len (firstMessageBlock ) > 32 {
38- firstMessageBlock = firstMessageBlock [:32 ]
42+ if len (firstMessageBlock ) > ( BlockSize - KeySize ) {
43+ firstMessageBlock = firstMessageBlock [:( BlockSize - KeySize ) ]
3944 }
4045
4146 tagOut := out
4247 out = out [poly1305 .TagSize :]
4348 for i , x := range firstMessageBlock {
44- out [i ] = firstBlock [32 + i ] ^ x
49+ out [i ] = firstBlock [( BlockSize - KeySize ) + i ] ^ x
4550 }
4651 message = message [len (firstMessageBlock ):]
4752 ciphertext := out
@@ -51,7 +56,7 @@ func Seal(out, nonce, message, key []byte) []byte {
5156 cipher .XORKeyStream (out , message )
5257
5358 var tag [TagSize ]byte
54- hash := poly1305 .New (polyKey )
59+ hash := poly1305 .New (& polyKey )
5560 _ , _ = hash .Write (ciphertext )
5661 hash .Sum (tag [:0 ])
5762 copy (tagOut , tag [:])
@@ -71,15 +76,18 @@ func Open(out, nonce, box, key []byte) ([]byte, error) {
7176 return nil , errors .New ("ciphertext is too short" )
7277 }
7378
74- var firstBlock [64 ]byte
75- cipher , _ := chacha .NewCipher (nonce , key , 20 )
79+ var firstBlock [BlockSize ]byte
80+ cipher , err := chacha20 .NewUnauthenticatedCipher (key , nonce )
81+ if err != nil {
82+ panic (err )
83+ }
7684 cipher .XORKeyStream (firstBlock [:], firstBlock [:])
77- var polyKey [32 ]byte
78- copy (polyKey [:], firstBlock [:32 ])
85+ var polyKey [KeySize ]byte
86+ copy (polyKey [:], firstBlock [:KeySize ])
7987
8088 var tag [TagSize ]byte
8189 ciphertext := box [TagSize :]
82- hash := poly1305 .New (polyKey )
90+ hash := poly1305 .New (& polyKey )
8391 _ , _ = hash .Write (ciphertext )
8492 hash .Sum (tag [:0 ])
8593 if subtle .ConstantTimeCompare (tag [:], box [:TagSize ]) != 1 {
@@ -89,11 +97,11 @@ func Open(out, nonce, box, key []byte) ([]byte, error) {
8997 ret , out := sliceForAppend (out , len (ciphertext ))
9098
9199 firstMessageBlock := ciphertext
92- if len (firstMessageBlock ) > 32 {
93- firstMessageBlock = firstMessageBlock [:32 ]
100+ if len (firstMessageBlock ) > ( BlockSize - KeySize ) {
101+ firstMessageBlock = firstMessageBlock [:( BlockSize - KeySize ) ]
94102 }
95103 for i , x := range firstMessageBlock {
96- out [i ] = firstBlock [32 + i ] ^ x
104+ out [i ] = firstBlock [( BlockSize - KeySize ) + i ] ^ x
97105 }
98106 ciphertext = ciphertext [len (firstMessageBlock ):]
99107 out = out [len (firstMessageBlock ):]
0 commit comments