Home | History | Annotate | Download | only in runner
      1 package runner
      2 
      3 import (
      4 	"bytes"
      5 	"encoding/hex"
      6 	"testing"
      7 )
      8 
      9 // See RFC 7539, section 2.1.1.
     10 func TestChaChaQuarterRound(t *testing.T) {
     11 	state := [16]uint32{0x11111111, 0x01020304, 0x9b8d6f43, 0x01234567}
     12 	chaChaQuarterRound(&state, 0, 1, 2, 3)
     13 
     14 	a, b, c, d := state[0], state[1], state[2], state[3]
     15 	if a != 0xea2a92f4 || b != 0xcb1cf8ce || c != 0x4581472e || d != 0x5881c4bb {
     16 		t.Errorf("Incorrect results: %x", state)
     17 	}
     18 }
     19 
     20 // See RFC 7539, section 2.2.1.
     21 func TestChaChaQuarterRoundState(t *testing.T) {
     22 	state := [16]uint32{
     23 		0x879531e0, 0xc5ecf37d, 0x516461b1, 0xc9a62f8a,
     24 		0x44c20ef3, 0x3390af7f, 0xd9fc690b, 0x2a5f714c,
     25 		0x53372767, 0xb00a5631, 0x974c541a, 0x359e9963,
     26 		0x5c971061, 0x3d631689, 0x2098d9d6, 0x91dbd320,
     27 	}
     28 	chaChaQuarterRound(&state, 2, 7, 8, 13)
     29 
     30 	expected := [16]uint32{
     31 		0x879531e0, 0xc5ecf37d, 0xbdb886dc, 0xc9a62f8a,
     32 		0x44c20ef3, 0x3390af7f, 0xd9fc690b, 0xcfacafd2,
     33 		0xe46bea80, 0xb00a5631, 0x974c541a, 0x359e9963,
     34 		0x5c971061, 0xccc07c79, 0x2098d9d6, 0x91dbd320,
     35 	}
     36 	for i := range state {
     37 		if state[i] != expected[i] {
     38 			t.Errorf("Mismatch at %d: %x vs %x", i, state, expected)
     39 		}
     40 	}
     41 }
     42 
     43 // See RFC 7539, section 2.3.2.
     44 func TestChaCha20Block(t *testing.T) {
     45 	state := [16]uint32{
     46 		0x61707865, 0x3320646e, 0x79622d32, 0x6b206574,
     47 		0x03020100, 0x07060504, 0x0b0a0908, 0x0f0e0d0c,
     48 		0x13121110, 0x17161514, 0x1b1a1918, 0x1f1e1d1c,
     49 		0x00000001, 0x09000000, 0x4a000000, 0x00000000,
     50 	}
     51 	out := make([]byte, 64)
     52 	chaCha20Block(&state, out)
     53 
     54 	expected := []byte{
     55 		0x10, 0xf1, 0xe7, 0xe4, 0xd1, 0x3b, 0x59, 0x15,
     56 		0x50, 0x0f, 0xdd, 0x1f, 0xa3, 0x20, 0x71, 0xc4,
     57 		0xc7, 0xd1, 0xf4, 0xc7, 0x33, 0xc0, 0x68, 0x03,
     58 		0x04, 0x22, 0xaa, 0x9a, 0xc3, 0xd4, 0x6c, 0x4e,
     59 		0xd2, 0x82, 0x64, 0x46, 0x07, 0x9f, 0xaa, 0x09,
     60 		0x14, 0xc2, 0xd7, 0x05, 0xd9, 0x8b, 0x02, 0xa2,
     61 		0xb5, 0x12, 0x9c, 0xd1, 0xde, 0x16, 0x4e, 0xb9,
     62 		0xcb, 0xd0, 0x83, 0xe8, 0xa2, 0x50, 0x3c, 0x4e,
     63 	}
     64 	if !bytes.Equal(out, expected) {
     65 		t.Errorf("Got %x, wanted %x", out, expected)
     66 	}
     67 }
     68 
     69 func decodeHexOrPanic(in string) []byte {
     70 	out, err := hex.DecodeString(in)
     71 	if err != nil {
     72 		panic(err)
     73 	}
     74 	return out
     75 }
     76 
     77 // See draft-agl-tls-chacha20poly1305-04, section 7.
     78 func TestChaCha20Poly1305Old(t *testing.T) {
     79 	key := decodeHexOrPanic("4290bcb154173531f314af57f3be3b5006da371ece272afa1b5dbdd1100a1007")
     80 	input := decodeHexOrPanic("86d09974840bded2a5ca")
     81 	nonce := decodeHexOrPanic("cd7cf67be39c794a")
     82 	ad := decodeHexOrPanic("87e229d4500845a079c0")
     83 	output := decodeHexOrPanic("e3e446f7ede9a19b62a4677dabf4e3d24b876bb284753896e1d6")
     84 
     85 	aead, err := newChaCha20Poly1305Old(key)
     86 	if err != nil {
     87 		t.Fatal(err)
     88 	}
     89 
     90 	out, err := aead.Open(nil, nonce, output, ad)
     91 	if err != nil {
     92 		t.Errorf("Open failed: %s", err)
     93 	} else if !bytes.Equal(out, input) {
     94 		t.Errorf("Open gave %x, wanted %x", out, input)
     95 	}
     96 
     97 	out = aead.Seal(nil, nonce, input, ad)
     98 	if !bytes.Equal(out, output) {
     99 		t.Errorf("Open gave %x, wanted %x", out, output)
    100 	}
    101 
    102 	out[0]++
    103 	_, err = aead.Open(nil, nonce, out, ad)
    104 	if err == nil {
    105 		t.Errorf("Open on malformed data unexpectedly succeeded")
    106 	}
    107 }
    108 
    109 var chaCha20Poly1305TestVectors = []struct {
    110 	key, input, nonce, ad, output string
    111 }{
    112 	{
    113 		// See RFC 7539, section 2.8.2.
    114 		key:    "808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9f",
    115 		input:  "4c616469657320616e642047656e746c656d656e206f662074686520636c617373206f66202739393a204966204920636f756c64206f6666657220796f75206f6e6c79206f6e652074697020666f7220746865206675747572652c2073756e73637265656e20776f756c642062652069742e",
    116 		nonce:  "070000004041424344454647",
    117 		ad:     "50515253c0c1c2c3c4c5c6c7",
    118 		output: "d31a8d34648e60db7b86afbc53ef7ec2a4aded51296e08fea9e2b5a736ee62d63dbea45e8ca9671282fafb69da92728b1a71de0a9e060b2905d6a5b67ecd3b3692ddbd7f2d778b8c9803aee328091b58fab324e4fad675945585808b4831d7bc3ff4def08e4b7a9de576d26586cec64b61161ae10b594f09e26a7e902ecbd0600691",
    119 	},
    120 	{
    121 		// See RFC 7539, section A.5.
    122 		key:    "1c9240a5eb55d38af333888604f6b5f0473917c1402b80099dca5cbc207075c0",
    123 		input:  "496e7465726e65742d4472616674732061726520647261667420646f63756d656e74732076616c696420666f722061206d6178696d756d206f6620736978206d6f6e74687320616e64206d617920626520757064617465642c207265706c616365642c206f72206f62736f6c65746564206279206f7468657220646f63756d656e747320617420616e792074696d652e20497420697320696e617070726f70726961746520746f2075736520496e7465726e65742d447261667473206173207265666572656e6365206d6174657269616c206f7220746f2063697465207468656d206f74686572207468616e206173202fe2809c776f726b20696e2070726f67726573732e2fe2809d",
    124 		nonce:  "000000000102030405060708",
    125 		ad:     "f33388860000000000004e91",
    126 		output: "64a0861575861af460f062c79be643bd5e805cfd345cf389f108670ac76c8cb24c6cfc18755d43eea09ee94e382d26b0bdb7b73c321b0100d4f03b7f355894cf332f830e710b97ce98c8a84abd0b948114ad176e008d33bd60f982b1ff37c8559797a06ef4f0ef61c186324e2b3506383606907b6a7c02b0f9f6157b53c867e4b9166c767b804d46a59b5216cde7a4e99040c5a40433225ee282a1b0a06c523eaf4534d7f83fa1155b0047718cbc546a0d072b04b3564eea1b422273f548271a0bb2316053fa76991955ebd63159434ecebb4e466dae5a1073a6727627097a1049e617d91d361094fa68f0ff77987130305beaba2eda04df997b714d6c6f2c29a6ad5cb4022b02709beead9d67890cbb22392336fea1851f38",
    127 	},
    128 }
    129 
    130 // See draft-agl-tls-chacha20poly1305-04, section 7.
    131 func TestChaCha20Poly1305(t *testing.T) {
    132 	for i, tt := range chaCha20Poly1305TestVectors {
    133 		key := decodeHexOrPanic(tt.key)
    134 		input := decodeHexOrPanic(tt.input)
    135 		nonce := decodeHexOrPanic(tt.nonce)
    136 		ad := decodeHexOrPanic(tt.ad)
    137 		output := decodeHexOrPanic(tt.output)
    138 
    139 		aead, err := newChaCha20Poly1305(key)
    140 		if err != nil {
    141 			t.Fatal(err)
    142 		}
    143 
    144 		out, err := aead.Open(nil, nonce, output, ad)
    145 		if err != nil {
    146 			t.Errorf("%d. Open failed: %s", i, err)
    147 		} else if !bytes.Equal(out, input) {
    148 			t.Errorf("%d. Open gave %x, wanted %x", i, out, input)
    149 		}
    150 
    151 		out = aead.Seal(nil, nonce, input, ad)
    152 		if !bytes.Equal(out, output) {
    153 			t.Errorf("%d. Open gave %x, wanted %x", i, out, output)
    154 		}
    155 
    156 		out[0]++
    157 		_, err = aead.Open(nil, nonce, out, ad)
    158 		if err == nil {
    159 			t.Errorf("%d. Open on malformed data unexpectedly succeeded", i)
    160 		}
    161 	}
    162 }
    163