...

Source file src/pkg/vendor/golang.org/x/crypto/chacha20poly1305/chacha20poly1305.go

     1	// Copyright 2016 The Go Authors. All rights reserved.
     2	// Use of this source code is governed by a BSD-style
     3	// license that can be found in the LICENSE file.
     4	
     5	// Package chacha20poly1305 implements the ChaCha20-Poly1305 AEAD as specified in RFC 7539,
     6	// and its extended nonce variant XChaCha20-Poly1305.
     7	package chacha20poly1305 // import "golang.org/x/crypto/chacha20poly1305"
     8	
     9	import (
    10		"crypto/cipher"
    11		"encoding/binary"
    12		"errors"
    13	)
    14	
    15	const (
    16		// KeySize is the size of the key used by this AEAD, in bytes.
    17		KeySize = 32
    18	
    19		// NonceSize is the size of the nonce used with the standard variant of this
    20		// AEAD, in bytes.
    21		//
    22		// Note that this is too short to be safely generated at random if the same
    23		// key is reused more than 2³² times.
    24		NonceSize = 12
    25	
    26		// NonceSizeX is the size of the nonce used with the XChaCha20-Poly1305
    27		// variant of this AEAD, in bytes.
    28		NonceSizeX = 24
    29	)
    30	
    31	type chacha20poly1305 struct {
    32		key [8]uint32
    33	}
    34	
    35	// New returns a ChaCha20-Poly1305 AEAD that uses the given 256-bit key.
    36	func New(key []byte) (cipher.AEAD, error) {
    37		if len(key) != KeySize {
    38			return nil, errors.New("chacha20poly1305: bad key length")
    39		}
    40		ret := new(chacha20poly1305)
    41		ret.key[0] = binary.LittleEndian.Uint32(key[0:4])
    42		ret.key[1] = binary.LittleEndian.Uint32(key[4:8])
    43		ret.key[2] = binary.LittleEndian.Uint32(key[8:12])
    44		ret.key[3] = binary.LittleEndian.Uint32(key[12:16])
    45		ret.key[4] = binary.LittleEndian.Uint32(key[16:20])
    46		ret.key[5] = binary.LittleEndian.Uint32(key[20:24])
    47		ret.key[6] = binary.LittleEndian.Uint32(key[24:28])
    48		ret.key[7] = binary.LittleEndian.Uint32(key[28:32])
    49		return ret, nil
    50	}
    51	
    52	func (c *chacha20poly1305) NonceSize() int {
    53		return NonceSize
    54	}
    55	
    56	func (c *chacha20poly1305) Overhead() int {
    57		return 16
    58	}
    59	
    60	func (c *chacha20poly1305) Seal(dst, nonce, plaintext, additionalData []byte) []byte {
    61		if len(nonce) != NonceSize {
    62			panic("chacha20poly1305: bad nonce length passed to Seal")
    63		}
    64	
    65		if uint64(len(plaintext)) > (1<<38)-64 {
    66			panic("chacha20poly1305: plaintext too large")
    67		}
    68	
    69		return c.seal(dst, nonce, plaintext, additionalData)
    70	}
    71	
    72	var errOpen = errors.New("chacha20poly1305: message authentication failed")
    73	
    74	func (c *chacha20poly1305) Open(dst, nonce, ciphertext, additionalData []byte) ([]byte, error) {
    75		if len(nonce) != NonceSize {
    76			panic("chacha20poly1305: bad nonce length passed to Open")
    77		}
    78		if len(ciphertext) < 16 {
    79			return nil, errOpen
    80		}
    81		if uint64(len(ciphertext)) > (1<<38)-48 {
    82			panic("chacha20poly1305: ciphertext too large")
    83		}
    84	
    85		return c.open(dst, nonce, ciphertext, additionalData)
    86	}
    87	
    88	// sliceForAppend takes a slice and a requested number of bytes. It returns a
    89	// slice with the contents of the given slice followed by that many bytes and a
    90	// second slice that aliases into it and contains only the extra bytes. If the
    91	// original slice has sufficient capacity then no allocation is performed.
    92	func sliceForAppend(in []byte, n int) (head, tail []byte) {
    93		if total := len(in) + n; cap(in) >= total {
    94			head = in[:total]
    95		} else {
    96			head = make([]byte, total)
    97			copy(head, in)
    98		}
    99		tail = head[len(in):]
   100		return
   101	}
   102	

View as plain text