Пример #1
0
func TestBadRoundNumber(t *testing.T) {
	key := make([]byte, chacha20.KeySize)
	nonce := make([]byte, chacha20.NonceSize)

	_, err := chacha20.NewWithRounds(key, nonce, 5)

	if err != chacha20.ErrInvalidRounds {
		t.Error("Should have rejected an invalid round number")
	}
}
Пример #2
0
func TestChaCha20WithRounds(t *testing.T) {
	for i, vector := range testVectors {
		t.Logf("Running test vector %d", i)

		key, err := hex.DecodeString(vector.key)
		if err != nil {
			t.Error(err)
		}

		nonce, err := hex.DecodeString(vector.nonce)
		if err != nil {
			t.Error(err)
		}

		c, err := chacha20.NewWithRounds(key, nonce, vector.rounds)
		if err != nil {
			t.Error(err)
		}

		expected, err := hex.DecodeString(vector.keyStream)
		if err != nil {
			t.Error(err)
		}

		src := make([]byte, len(expected))
		dst := make([]byte, len(expected))
		c.XORKeyStream(dst, src)

		if !bytes.Equal(expected, dst) {
			t.Errorf("Bad keystream: expected %x, was %x", expected, dst)

			for i, v := range expected {
				if dst[i] != v {
					t.Logf("Mismatch at offset %d: %x vs %x", i, v, dst[i])
					break
				}
			}
		}
	}
}