Skip to content

Commit

Permalink
update test and readme
Browse files Browse the repository at this point in the history
  • Loading branch information
convto committed Feb 25, 2024
1 parent 2a4414b commit d3460c0
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 60 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# ChaCha20
ChaCha20 implementation for learning purposes. Do not use in production.

Implemented following [Bernstein, D., "ChaCha, a variant of Salsa20", January 2008](http://cr.yp.to/chacha/chacha-20080128.pdf) and passed through the [RFC8439 Appendix A.1](https://datatracker.ietf.org/doc/html/rfc8439#appendix-A.1) test suite.
88 changes: 28 additions & 60 deletions chacha20_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,81 +20,49 @@ func Test_keyStream(t *testing.T) {
key [32]byte
nonce [12]byte
counter uint32
keyStream func() [64]byte
keyStream [64]byte
}{
{
name: "RFC8439 Appendix A.1 (Key Stream Test Vector) #1",
key: [32]byte(mustDecodeHex("0000000000000000000000000000000000000000000000000000000000000000")),
nonce: [12]byte(mustDecodeHex("000000000000000000000000")),
counter: 0x00000000,
keyStream: func() [64]byte {
b, err := hex.DecodeString("76b8e0ada0f13d90405d6ae55386bd28bdd219b8a08ded1aa836efcc8b770dc7da41597c5157488d7724e03fb8d84a376a43b8f41518a11cc387b669b2ee6586")
if err != nil {
panic(err)
}
return [64]byte(b[:64])
},
name: "RFC8439 Appendix A.1 (Key Stream Test Vector) #1",
key: [32]byte(mustDecodeHex("0000000000000000000000000000000000000000000000000000000000000000")),
nonce: [12]byte(mustDecodeHex("000000000000000000000000")),
counter: 0x00000000,
keyStream: [64]byte(mustDecodeHex("76b8e0ada0f13d90405d6ae55386bd28bdd219b8a08ded1aa836efcc8b770dc7da41597c5157488d7724e03fb8d84a376a43b8f41518a11cc387b669b2ee6586")),
},
{
name: "RFC8439 Appendix A.1 (Key Stream Test Vector) #2",
key: [32]byte(mustDecodeHex("0000000000000000000000000000000000000000000000000000000000000000")),
nonce: [12]byte(mustDecodeHex("000000000000000000000000")),
counter: 0x00000001,
keyStream: func() [64]byte {
b, err := hex.DecodeString("9f07e7be5551387a98ba977c732d080dcb0f29a048e3656912c6533e32ee7aed29b721769ce64e43d57133b074d839d531ed1f28510afb45ace10a1f4b794d6f")
if err != nil {
panic(err)
}
return [64]byte(b[:64])
},
name: "RFC8439 Appendix A.1 (Key Stream Test Vector) #2",
key: [32]byte(mustDecodeHex("0000000000000000000000000000000000000000000000000000000000000000")),
nonce: [12]byte(mustDecodeHex("000000000000000000000000")),
counter: 0x00000001,
keyStream: [64]byte(mustDecodeHex("9f07e7be5551387a98ba977c732d080dcb0f29a048e3656912c6533e32ee7aed29b721769ce64e43d57133b074d839d531ed1f28510afb45ace10a1f4b794d6f")),
},
{
name: "RFC8439 Appendix A.1 (Key Stream Test Vector) #3",
key: [32]byte(mustDecodeHex("0000000000000000000000000000000000000000000000000000000000000001")),
nonce: [12]byte(mustDecodeHex("000000000000000000000000")),
counter: 0x00000001,
keyStream: func() [64]byte {
b, err := hex.DecodeString("3aeb5224ecf849929b9d828db1ced4dd832025e8018b8160b82284f3c949aa5a8eca00bbb4a73bdad192b5c42f73f2fd4e273644c8b36125a64addeb006c13a0")
if err != nil {
panic(err)
}
return [64]byte(b[:64])
},
name: "RFC8439 Appendix A.1 (Key Stream Test Vector) #3",
key: [32]byte(mustDecodeHex("0000000000000000000000000000000000000000000000000000000000000001")),
nonce: [12]byte(mustDecodeHex("000000000000000000000000")),
counter: 0x00000001,
keyStream: [64]byte(mustDecodeHex("3aeb5224ecf849929b9d828db1ced4dd832025e8018b8160b82284f3c949aa5a8eca00bbb4a73bdad192b5c42f73f2fd4e273644c8b36125a64addeb006c13a0")),
},
{
name: "RFC8439 Appendix A.1 (Key Stream Test Vector) #4",
key: [32]byte(mustDecodeHex("00ff000000000000000000000000000000000000000000000000000000000000")),
nonce: [12]byte(mustDecodeHex("000000000000000000000000")),
counter: 0x00000002,
keyStream: func() [64]byte {
b, err := hex.DecodeString("72d54dfbf12ec44b362692df94137f328fea8da73990265ec1bbbea1ae9af0ca13b25aa26cb4a648cb9b9d1be65b2c0924a66c54d545ec1b7374f4872e99f096")
if err != nil {
panic(err)
}
return [64]byte(b[:64])
},
name: "RFC8439 Appendix A.1 (Key Stream Test Vector) #4",
key: [32]byte(mustDecodeHex("00ff000000000000000000000000000000000000000000000000000000000000")),
nonce: [12]byte(mustDecodeHex("000000000000000000000000")),
counter: 0x00000002,
keyStream: [64]byte(mustDecodeHex("72d54dfbf12ec44b362692df94137f328fea8da73990265ec1bbbea1ae9af0ca13b25aa26cb4a648cb9b9d1be65b2c0924a66c54d545ec1b7374f4872e99f096")),
},
{
name: "RFC8439 Appendix A.1 (Key Stream Test Vector) #5",
key: [32]byte(mustDecodeHex("0000000000000000000000000000000000000000000000000000000000000000")),
nonce: [12]byte(mustDecodeHex("000000000000000000000002")),
counter: 0x00000000,
keyStream: func() [64]byte {
b, err := hex.DecodeString("c2c64d378cd536374ae204b9ef933fcd1a8b2288b3dfa49672ab765b54ee27c78a970e0e955c14f3a88e741b97c286f75f8fc299e8148362fa198a39531bed6d")
if err != nil {
panic(err)
}
return [64]byte(b[:64])
},
name: "RFC8439 Appendix A.1 (Key Stream Test Vector) #5",
key: [32]byte(mustDecodeHex("0000000000000000000000000000000000000000000000000000000000000000")),
nonce: [12]byte(mustDecodeHex("000000000000000000000002")),
counter: 0x00000000,
keyStream: [64]byte(mustDecodeHex("c2c64d378cd536374ae204b9ef933fcd1a8b2288b3dfa49672ab765b54ee27c78a970e0e955c14f3a88e741b97c286f75f8fc299e8148362fa198a39531bed6d")),
},
}
for _, v := range keyStreamTestVectors {
t.Run(v.name, func(t *testing.T) {
x := NewCipher(v.key, v.counter, v.nonce)
want := v.keyStream()
got := x.(*state).keyStream()
if !reflect.DeepEqual(got, want) {
t.Errorf("state.keyStream()\ngot: %s\nwant: %s", hex.EncodeToString(got[:64]), hex.EncodeToString(want[:64]))
if got := x.(*state).keyStream(); !reflect.DeepEqual(got, v.keyStream) {
t.Errorf("state.keyStream()\ngot: %s\nwant: %s", hex.EncodeToString(got[:64]), hex.EncodeToString(v.keyStream[:64]))
}
})
}
Expand Down

0 comments on commit d3460c0

Please sign in to comment.