Skip to content
This repository has been archived by the owner on Apr 17, 2024. It is now read-only.

Commit

Permalink
fixes a typo: it should be XChaCha20 instead of XChacha20.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 211995874
GitOrigin-RevId: b38a452ba733994acfc11b8a2458ab60bb39a0f6
  • Loading branch information
ise-crypto authored and chuckx committed Sep 11, 2018
1 parent c627491 commit e6ac043
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 59 deletions.
28 changes: 14 additions & 14 deletions go/subtle/aead/chacha20poly1305.go
Expand Up @@ -24,29 +24,29 @@ import (
"github.com/google/tink/go/tink"
)

// Chacha20poly1305Aead is an implementation of Aead interface.
type Chacha20poly1305Aead struct {
key []byte
// ChaCha20Poly1305 is an implementation of Aead interface.
type ChaCha20Poly1305 struct {
Key []byte
}

// Assert that Chacha20poly1305Aead implements the Aead interface.
var _ tink.Aead = (*Chacha20poly1305Aead)(nil)
// Assert that ChaCha20Poly1305 implements the Aead interface.
var _ tink.Aead = (*ChaCha20Poly1305)(nil)

// NewChacha20poly1305Aead returns an Chacha20poly1305Aead instance.
// NewChaCha20Poly1305 returns an ChaCha20Poly1305 instance.
// The key argument should be a 32-bytes key.
func NewChacha20poly1305Aead(key []byte) (*Chacha20poly1305Aead, error) {
func NewChaCha20Poly1305(key []byte) (*ChaCha20Poly1305, error) {
if len(key) != chacha20poly1305.KeySize {
return nil, errors.New("chacha20poly1305: bad key length")
}

return &Chacha20poly1305Aead{key: key}, nil
return &ChaCha20Poly1305{Key: key}, nil
}

// Encrypt encrypts {@code pt} with {@code aad} as additional
// authenticated data. The resulting ciphertext consists of two parts:
// (1) the nonce used for encryption and (2) the actual ciphertext.
func (ca *Chacha20poly1305Aead) Encrypt(pt []byte, aad []byte) ([]byte, error) {
c, err := chacha20poly1305.New(ca.key)
func (ca *ChaCha20Poly1305) Encrypt(pt []byte, aad []byte) ([]byte, error) {
c, err := chacha20poly1305.New(ca.Key)
if err != nil {
return nil, err
}
Expand All @@ -60,21 +60,21 @@ func (ca *Chacha20poly1305Aead) Encrypt(pt []byte, aad []byte) ([]byte, error) {
}

// Decrypt decrypts {@code ct} with {@code aad} as the additionalauthenticated data.
func (ca *Chacha20poly1305Aead) Decrypt(ct []byte, aad []byte) ([]byte, error) {
c, err := chacha20poly1305.New(ca.key)
func (ca *ChaCha20Poly1305) Decrypt(ct []byte, aad []byte) ([]byte, error) {
c, err := chacha20poly1305.New(ca.Key)
if err != nil {
return nil, err
}

n := ct[:chacha20poly1305.NonceSize]
pt, err := c.Open(nil, n, ct[chacha20poly1305.NonceSize:], aad)
if err != nil {
return nil, fmt.Errorf("Chacha20poly1305Aead.Decrypt: %s", err)
return nil, fmt.Errorf("ChaCha20Poly1305.Decrypt: %s", err)
}
return pt, nil
}

// newNonce creates a new nonce for encryption.
func (ca *Chacha20poly1305Aead) newNonce() []byte {
func (ca *ChaCha20Poly1305) newNonce() []byte {
return random.GetRandomBytes(chacha20poly1305.NonceSize)
}
32 changes: 16 additions & 16 deletions go/subtle/aead/chacha20poly1305_test.go
Expand Up @@ -27,17 +27,17 @@ import (
"github.com/google/tink/go/subtle/random"
)

func TestChacha20poly1305EncryptDecrypt(t *testing.T) {
for i, test := range chacha20Poly1305Tests {
func TestChaCha20Poly1305EncryptDecrypt(t *testing.T) {
for i, test := range chaCha20Poly1305Tests {
key, _ := hex.DecodeString(test.key)
pt, _ := hex.DecodeString(test.plaintext)
aad, _ := hex.DecodeString(test.aad)
nonce, _ := hex.DecodeString(test.nonce)
out, _ := hex.DecodeString(test.out)

ca, err := aead.NewChacha20poly1305Aead(key)
ca, err := aead.NewChaCha20Poly1305(key)
if err != nil {
t.Errorf("#%d, cannot create new instance of Chacha20poly1305Aead: %s", i, err)
t.Errorf("#%d, cannot create new instance of ChaCha20Poly1305: %s", i, err)
continue
}

Expand All @@ -60,12 +60,12 @@ func TestChacha20poly1305EncryptDecrypt(t *testing.T) {
}
}

func TestChacha20poly1305EmptyAssociatedData(t *testing.T) {
func TestChaCha20Poly1305EmptyAssociatedData(t *testing.T) {
key := random.GetRandomBytes(chacha20poly1305.KeySize)
aad := []byte{}
badAad := []byte{1, 2, 3}

ca, err := aead.NewChacha20poly1305Aead(key)
ca, err := aead.NewChaCha20Poly1305(key)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -110,15 +110,15 @@ func TestChacha20poly1305EmptyAssociatedData(t *testing.T) {
}
}

func TestChacha20poly1305LongMessages(t *testing.T) {
func TestChaCha20Poly1305LongMessages(t *testing.T) {
dataSize := uint32(16)
// Encrypts and decrypts messages of size <= 8192.
for dataSize <= 1<<24 {
pt := random.GetRandomBytes(dataSize)
aad := random.GetRandomBytes(dataSize / 3)
key := random.GetRandomBytes(chacha20poly1305.KeySize)

ca, err := aead.NewChacha20poly1305Aead(key)
ca, err := aead.NewChaCha20Poly1305(key)
if err != nil {
t.Fatal(err)
}
Expand All @@ -137,13 +137,13 @@ func TestChacha20poly1305LongMessages(t *testing.T) {
}
}

func TestChacha20poly1305ModifyCiphertext(t *testing.T) {
for i, test := range chacha20Poly1305Tests {
func TestChaCha20Poly1305ModifyCiphertext(t *testing.T) {
for i, test := range chaCha20Poly1305Tests {
key, _ := hex.DecodeString(test.key)
pt, _ := hex.DecodeString(test.plaintext)
aad, _ := hex.DecodeString(test.aad)

ca, err := aead.NewChacha20poly1305Aead(key)
ca, err := aead.NewChaCha20Poly1305(key)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -176,9 +176,9 @@ func TestChacha20poly1305ModifyCiphertext(t *testing.T) {

// This is a very simple test for the randomness of the nonce.
// The test simply checks that the multiple ciphertexts of the same message are distinct.
func TestChacha20poly1305RandomNonce(t *testing.T) {
func TestChaCha20Poly1305RandomNonce(t *testing.T) {
key := random.GetRandomBytes(chacha20poly1305.KeySize)
ca, err := aead.NewChacha20poly1305Aead(key)
ca, err := aead.NewChaCha20Poly1305(key)
if err != nil {
t.Fatal(err)
}
Expand All @@ -196,7 +196,7 @@ func TestChacha20poly1305RandomNonce(t *testing.T) {
}
}

func TestChacha20poly1305WycheproofVectors(t *testing.T) {
func TestChaCha20Poly1305WycheproofVectors(t *testing.T) {
f, err := os.Open("../../../../wycheproof/testvectors/chacha20_poly1305_test.json")
if err != nil {
t.Fatalf("cannot open file: %s, make sure that github.com/google/wycheproof is in your gopath", err)
Expand Down Expand Up @@ -246,9 +246,9 @@ func TestChacha20poly1305WycheproofVectors(t *testing.T) {
combinedCt = append(combinedCt, ct...)
combinedCt = append(combinedCt, tag...)

ca, err := aead.NewChacha20poly1305Aead(key)
ca, err := aead.NewChaCha20Poly1305(key)
if err != nil {
t.Errorf("#%d, cannot create new instance of Chacha20poly1305Aead: %s", tc.TcID, err)
t.Errorf("#%d, cannot create new instance of ChaCha20Poly1305: %s", tc.TcID, err)
continue
}

Expand Down
2 changes: 1 addition & 1 deletion go/subtle/aead/chacha20poly1305_vectors_test.go
Expand Up @@ -14,7 +14,7 @@

package aead_test

var chacha20Poly1305Tests = []struct {
var chaCha20Poly1305Tests = []struct {
plaintext, aad, key, nonce, out string
}{
{
Expand Down
28 changes: 14 additions & 14 deletions go/subtle/aead/xchacha20poly1305.go
Expand Up @@ -24,29 +24,29 @@ import (
"github.com/google/tink/go/tink"
)

// XChacha20poly1305Aead is an implementation of Aead interface.
type XChacha20poly1305Aead struct {
key []byte
// XChaCha20Poly1305 is an implementation of Aead interface.
type XChaCha20Poly1305 struct {
Key []byte
}

// Assert that XChacha20poly1305Aead implements the Aead interface.
var _ tink.Aead = (*XChacha20poly1305Aead)(nil)
// Assert that XChaCha20Poly1305 implements the Aead interface.
var _ tink.Aead = (*XChaCha20Poly1305)(nil)

// NewXChacha20poly1305Aead returns an XChacha20poly1305Aead instance.
// NewXChaCha20Poly1305 returns an XChaCha20Poly1305 instance.
// The key argument should be a 32-bytes key.
func NewXChacha20poly1305Aead(key []byte) (*XChacha20poly1305Aead, error) {
func NewXChaCha20Poly1305(key []byte) (*XChaCha20Poly1305, error) {
if len(key) != chacha20poly1305.KeySize {
return nil, errors.New("xchacha20poly1305: bad key length")
}

return &XChacha20poly1305Aead{key: key}, nil
return &XChaCha20Poly1305{Key: key}, nil
}

// Encrypt encrypts {@code pt} with {@code aad} as additional
// authenticated data. The resulting ciphertext consists of two parts:
// (1) the nonce used for encryption and (2) the actual ciphertext.
func (x *XChacha20poly1305Aead) Encrypt(pt []byte, aad []byte) ([]byte, error) {
c, err := chacha20poly1305.NewX(x.key)
func (x *XChaCha20Poly1305) Encrypt(pt []byte, aad []byte) ([]byte, error) {
c, err := chacha20poly1305.NewX(x.Key)
if err != nil {
return nil, err
}
Expand All @@ -60,21 +60,21 @@ func (x *XChacha20poly1305Aead) Encrypt(pt []byte, aad []byte) ([]byte, error) {
}

// Decrypt decrypts {@code ct} with {@code aad} as the additionalauthenticated data.
func (x *XChacha20poly1305Aead) Decrypt(ct []byte, aad []byte) ([]byte, error) {
c, err := chacha20poly1305.NewX(x.key)
func (x *XChaCha20Poly1305) Decrypt(ct []byte, aad []byte) ([]byte, error) {
c, err := chacha20poly1305.NewX(x.Key)
if err != nil {
return nil, err
}

n := ct[:chacha20poly1305.NonceSizeX]
pt, err := c.Open(nil, n, ct[chacha20poly1305.NonceSizeX:], aad)
if err != nil {
return nil, fmt.Errorf("XChacha20poly1305Aead.Decrypt: %s", err)
return nil, fmt.Errorf("XChaCha20Poly1305.Decrypt: %s", err)
}
return pt, nil
}

// newNonce creates a new nonce for encryption.
func (x *XChacha20poly1305Aead) newNonce() []byte {
func (x *XChaCha20Poly1305) newNonce() []byte {
return random.GetRandomBytes(chacha20poly1305.NonceSizeX)
}
26 changes: 13 additions & 13 deletions go/subtle/aead/xchacha20poly1305_test.go
Expand Up @@ -25,18 +25,18 @@ import (
"github.com/google/tink/go/subtle/random"
)

func TestXChacha20poly1305EncryptDecrypt(t *testing.T) {
for i, test := range xChacha20Poly1305Tests {
func TestXChaCha20Poly1305EncryptDecrypt(t *testing.T) {
for i, test := range xChaCha20Poly1305Tests {
key, _ := hex.DecodeString(test.key)
pt, _ := hex.DecodeString(test.plaintext)
aad, _ := hex.DecodeString(test.aad)
nonce, _ := hex.DecodeString(test.nonce)
out, _ := hex.DecodeString(test.out)
tag, _ := hex.DecodeString(test.tag)

x, err := aead.NewXChacha20poly1305Aead(key)
x, err := aead.NewXChaCha20Poly1305(key)
if err != nil {
t.Errorf("#%d, cannot create new instance of XChacha20poly1305Aead: %s", i, err)
t.Errorf("#%d, cannot create new instance of XChaCha20Poly1305: %s", i, err)
continue
}

Expand All @@ -60,12 +60,12 @@ func TestXChacha20poly1305EncryptDecrypt(t *testing.T) {
}
}

func TestXChacha20poly1305EmptyAssociatedData(t *testing.T) {
func TestXChaCha20Poly1305EmptyAssociatedData(t *testing.T) {
key := random.GetRandomBytes(chacha20poly1305.KeySize)
aad := []byte{}
badAad := []byte{1, 2, 3}

x, err := aead.NewXChacha20poly1305Aead(key)
x, err := aead.NewXChaCha20Poly1305(key)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -110,15 +110,15 @@ func TestXChacha20poly1305EmptyAssociatedData(t *testing.T) {
}
}

func TestXChacha20poly1305LongMessages(t *testing.T) {
func TestXChaCha20Poly1305LongMessages(t *testing.T) {
dataSize := uint32(16)
// Encrypts and decrypts messages of size <= 8192.
for dataSize <= 1<<24 {
pt := random.GetRandomBytes(dataSize)
aad := random.GetRandomBytes(dataSize / 3)
key := random.GetRandomBytes(chacha20poly1305.KeySize)

x, err := aead.NewXChacha20poly1305Aead(key)
x, err := aead.NewXChaCha20Poly1305(key)
if err != nil {
t.Fatal(err)
}
Expand All @@ -137,13 +137,13 @@ func TestXChacha20poly1305LongMessages(t *testing.T) {
}
}

func TestXChacha20poly1305ModifyCiphertext(t *testing.T) {
for i, test := range xChacha20Poly1305Tests {
func TestXChaCha20Poly1305ModifyCiphertext(t *testing.T) {
for i, test := range xChaCha20Poly1305Tests {
key, _ := hex.DecodeString(test.key)
pt, _ := hex.DecodeString(test.plaintext)
aad, _ := hex.DecodeString(test.aad)

x, err := aead.NewXChacha20poly1305Aead(key)
x, err := aead.NewXChaCha20Poly1305(key)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -176,9 +176,9 @@ func TestXChacha20poly1305ModifyCiphertext(t *testing.T) {

// This is a very simple test for the randomness of the nonce.
// The test simply checks that the multiple ciphertexts of the same message are distinct.
func TestXChacha20poly1305RandomNonce(t *testing.T) {
func TestXChaCha20Poly1305RandomNonce(t *testing.T) {
key := random.GetRandomBytes(chacha20poly1305.KeySize)
x, err := aead.NewXChacha20poly1305Aead(key)
x, err := aead.NewXChaCha20Poly1305(key)
if err != nil {
t.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion go/subtle/aead/xchacha20poly1305_vectors_test.go
Expand Up @@ -66,7 +66,7 @@ package aead_test
// return 0;
// }

var xChacha20Poly1305Tests = []struct {
var xChaCha20Poly1305Tests = []struct {
key, nonce, plaintext, aad, out, tag string
}{
{
Expand Down

0 comments on commit e6ac043

Please sign in to comment.