poly1305_aead.mx raw

   1  // Regression: RFC 8439's poly1305 and AEAD_CHACHA20_POLY1305 test vectors.
   2  //
   3  // The MAC's buffering scratch is a slice now. Nothing else changed, and a
   4  // zero MAC has a nil buffer - so `New` building `&MAC{}` and calling
   5  // initialize directly dropped every buffered message chunk and produced a
   6  // stable but wrong tag (df33cbbe... instead of a8061dc1...), while Seal/Open
   7  // stayed self-consistent and a flipped tag was still rejected. Only the
   8  // published vector saw it. `New` goes through newMACGeneric now, which is
   9  // where the buffer is allocated.
  10  //
  11  // Covers poly1305 over its own vector, the AEAD ciphertext and tag byte for
  12  // byte (so chacha20, poly1305 and the padded AAD/length framing are all in
  13  // play), the Open round trip and a corrupted tag being refused.
  14  package main
  15  
  16  import (
  17  	"golang.org/x/crypto/chacha20poly1305"
  18  	"golang.org/x/crypto/internal/poly1305"
  19  )
  20  
  21  func hex(b []byte) (out []byte) {
  22  	const d = "0123456789abcdef"
  23  	out = []byte{:len(b) * 2}
  24  	for i := 0; i < len(b); i++ {
  25  		out[i*2] = d[b[i]>>4]
  26  		out[i*2+1] = d[b[i]&0xf]
  27  	}
  28  	return out
  29  }
  30  
  31  func unhex(s []byte) (out []byte) {
  32  	nib := func(c byte) byte {
  33  		if c >= '0' && c <= '9' {
  34  			return c - '0'
  35  		}
  36  		return c - 'a' + 10
  37  	}
  38  	out = []byte{:len(s) / 2}
  39  	for i := 0; i < len(out); i++ {
  40  		out[i] = nib(s[i*2])<<4 | nib(s[i*2+1])
  41  	}
  42  	return out
  43  }
  44  
  45  func main() {
  46  	// Termination: a batch program must still be stoppable, so the entry
  47  	// handles the channel the runtime closes on SIGINT/SIGTERM.
  48  	select {
  49  	case <-shutdown:
  50  		return
  51  	default:
  52  	}
  53  
  54  	key := unhex([]byte("85d6be7857556d337f4452fe42d506a80103808afb0db2fd4abff6af4149f51b"))
  55  	msg := []byte("Cryptographic Forum Research Group")
  56  	tag := []byte{:16}
  57  	poly1305.Sum(tag, msg, key)
  58  	if string(hex(tag)) != "a8061dc1305136c6c22b8baf0c0127a9" {
  59  		panic("poly1305 tag: " | string(hex(tag)))
  60  	}
  61  	if !poly1305.Verify(unhex([]byte("a8061dc1305136c6c22b8baf0c0127a9")), msg, key) {
  62  		panic("poly1305 Verify rejected the RFC tag")
  63  	}
  64  	if poly1305.Verify(unhex([]byte("a8061dc1305136c6c22b8baf0c0127a8")), msg, key) {
  65  		panic("poly1305 Verify accepted a wrong tag")
  66  	}
  67  
  68  	key2 := unhex([]byte("808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9f"))
  69  	nonce := unhex([]byte("070000004041424344454647"))
  70  	aad := unhex([]byte("50515253c0c1c2c3c4c5c6c7"))
  71  	pt := []byte("Ladies and Gentlemen of the class of '99: If I could offer you only one tip for the future, sunscreen would be it.")
  72  	ctHex := []byte("d31a8d34648e60db7b86afbc53ef7ec2a4aded51296e08fea9e2b5a736ee62d63dbea45e8ca9671282fafb69da92728b1a71de0a9e060b2905d6a5b67ecd3b3692ddbd7f2d778b8c9803aee328091b58fab324e4fad675945585808b4831d7bc3ff4def08e4b7a9de576d26586cec64b6116")
  73  	tagHex := []byte("1ae10b594f09e26a7e902ecbd0600691")
  74  	want := unhex(ctHex | tagHex)
  75  
  76  	aead, err := chacha20poly1305.New(key2)
  77  	if err != nil {
  78  		panic(err.Error())
  79  	}
  80  	got := aead.Seal(nil, nonce, pt, aad)
  81  	if string(hex(got)) != string(hex(want)) {
  82  		panic("aead mismatch: " | string(hex(got)))
  83  	}
  84  	back, oerr := aead.Open(nil, nonce, got, aad)
  85  	if oerr != nil || string(back) != string(pt) {
  86  		panic("aead Open failed")
  87  	}
  88  	bad := []byte{:len(got)}
  89  	copy(bad, got)
  90  	bad[len(bad)-1] ^= 1
  91  	if _, berr := aead.Open(nil, nonce, bad, aad); berr == nil {
  92  		panic("aead Open accepted a corrupted tag")
  93  	}
  94  	println("ok")
  95  }
  96