github.com/letsencrypt/go@v0.0.0-20160714163537-4054769a31f6/src/crypto/aes/aes_gcm.go (about)

     1  // Copyright 2015 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  // +build amd64
     6  
     7  package aes
     8  
     9  import (
    10  	"crypto/cipher"
    11  	"crypto/subtle"
    12  	"errors"
    13  )
    14  
    15  // The following functions are defined in gcm_amd64.s.
    16  func hasGCMAsm() bool
    17  
    18  //go:noescape
    19  func aesEncBlock(dst, src *[16]byte, ks []uint32)
    20  
    21  //go:noescape
    22  func gcmAesInit(productTable *[256]byte, ks []uint32)
    23  
    24  //go:noescape
    25  func gcmAesData(productTable *[256]byte, data []byte, T *[16]byte)
    26  
    27  //go:noescape
    28  func gcmAesEnc(productTable *[256]byte, dst, src []byte, ctr, T *[16]byte, ks []uint32)
    29  
    30  //go:noescape
    31  func gcmAesDec(productTable *[256]byte, dst, src []byte, ctr, T *[16]byte, ks []uint32)
    32  
    33  //go:noescape
    34  func gcmAesFinish(productTable *[256]byte, tagMask, T *[16]byte, pLen, dLen uint64)
    35  
    36  const (
    37  	gcmBlockSize         = 16
    38  	gcmTagSize           = 16
    39  	gcmStandardNonceSize = 12
    40  )
    41  
    42  var errOpen = errors.New("cipher: message authentication failed")
    43  
    44  // aesCipherGCM implements crypto/cipher.gcmAble so that crypto/cipher.NewGCM
    45  // will use the optimised implementation in this file when possible. Instances
    46  // of this type only exist when hasGCMAsm returns true.
    47  type aesCipherGCM struct {
    48  	aesCipherAsm
    49  }
    50  
    51  // Assert that aesCipherGCM implements the gcmAble interface.
    52  var _ gcmAble = (*aesCipherGCM)(nil)
    53  
    54  // NewGCM returns the AES cipher wrapped in Galois Counter Mode. This is only
    55  // called by crypto/cipher.NewGCM via the gcmAble interface.
    56  func (c *aesCipherGCM) NewGCM(nonceSize int) (cipher.AEAD, error) {
    57  	g := &gcmAsm{ks: c.enc, nonceSize: nonceSize}
    58  	gcmAesInit(&g.productTable, g.ks)
    59  	return g, nil
    60  }
    61  
    62  type gcmAsm struct {
    63  	// ks is the key schedule, the length of which depends on the size of
    64  	// the AES key.
    65  	ks []uint32
    66  	// productTable contains pre-computed multiples of the binary-field
    67  	// element used in GHASH.
    68  	productTable [256]byte
    69  	// nonceSize contains the expected size of the nonce, in bytes.
    70  	nonceSize int
    71  }
    72  
    73  func (g *gcmAsm) NonceSize() int {
    74  	return g.nonceSize
    75  }
    76  
    77  func (*gcmAsm) Overhead() int {
    78  	return gcmTagSize
    79  }
    80  
    81  // sliceForAppend takes a slice and a requested number of bytes. It returns a
    82  // slice with the contents of the given slice followed by that many bytes and a
    83  // second slice that aliases into it and contains only the extra bytes. If the
    84  // original slice has sufficient capacity then no allocation is performed.
    85  func sliceForAppend(in []byte, n int) (head, tail []byte) {
    86  	if total := len(in) + n; cap(in) >= total {
    87  		head = in[:total]
    88  	} else {
    89  		head = make([]byte, total)
    90  		copy(head, in)
    91  	}
    92  	tail = head[len(in):]
    93  	return
    94  }
    95  
    96  // Seal encrypts and authenticates plaintext. See the cipher.AEAD interface for
    97  // details.
    98  func (g *gcmAsm) Seal(dst, nonce, plaintext, data []byte) []byte {
    99  	if len(nonce) != g.nonceSize {
   100  		panic("cipher: incorrect nonce length given to GCM")
   101  	}
   102  
   103  	var counter, tagMask [gcmBlockSize]byte
   104  
   105  	if len(nonce) == gcmStandardNonceSize {
   106  		// Init counter to nonce||1
   107  		copy(counter[:], nonce)
   108  		counter[gcmBlockSize-1] = 1
   109  	} else {
   110  		// Otherwise counter = GHASH(nonce)
   111  		gcmAesData(&g.productTable, nonce, &counter)
   112  		gcmAesFinish(&g.productTable, &tagMask, &counter, uint64(len(nonce)), uint64(0))
   113  	}
   114  
   115  	aesEncBlock(&tagMask, &counter, g.ks)
   116  
   117  	var tagOut [gcmTagSize]byte
   118  	gcmAesData(&g.productTable, data, &tagOut)
   119  
   120  	ret, out := sliceForAppend(dst, len(plaintext)+gcmTagSize)
   121  	if len(plaintext) > 0 {
   122  		gcmAesEnc(&g.productTable, out, plaintext, &counter, &tagOut, g.ks)
   123  	}
   124  	gcmAesFinish(&g.productTable, &tagMask, &tagOut, uint64(len(plaintext)), uint64(len(data)))
   125  	copy(out[len(plaintext):], tagOut[:])
   126  
   127  	return ret
   128  }
   129  
   130  // Open authenticates and decrypts ciphertext. See the cipher.AEAD interface
   131  // for details.
   132  func (g *gcmAsm) Open(dst, nonce, ciphertext, data []byte) ([]byte, error) {
   133  	if len(nonce) != g.nonceSize {
   134  		panic("cipher: incorrect nonce length given to GCM")
   135  	}
   136  
   137  	if len(ciphertext) < gcmTagSize {
   138  		return nil, errOpen
   139  	}
   140  	tag := ciphertext[len(ciphertext)-gcmTagSize:]
   141  	ciphertext = ciphertext[:len(ciphertext)-gcmTagSize]
   142  
   143  	// See GCM spec, section 7.1.
   144  	var counter, tagMask [gcmBlockSize]byte
   145  
   146  	if len(nonce) == gcmStandardNonceSize {
   147  		// Init counter to nonce||1
   148  		copy(counter[:], nonce)
   149  		counter[gcmBlockSize-1] = 1
   150  	} else {
   151  		// Otherwise counter = GHASH(nonce)
   152  		gcmAesData(&g.productTable, nonce, &counter)
   153  		gcmAesFinish(&g.productTable, &tagMask, &counter, uint64(len(nonce)), uint64(0))
   154  	}
   155  
   156  	aesEncBlock(&tagMask, &counter, g.ks)
   157  
   158  	var expectedTag [gcmTagSize]byte
   159  	gcmAesData(&g.productTable, data, &expectedTag)
   160  
   161  	ret, out := sliceForAppend(dst, len(ciphertext))
   162  	if len(ciphertext) > 0 {
   163  		gcmAesDec(&g.productTable, out, ciphertext, &counter, &expectedTag, g.ks)
   164  	}
   165  	gcmAesFinish(&g.productTable, &tagMask, &expectedTag, uint64(len(ciphertext)), uint64(len(data)))
   166  
   167  	if subtle.ConstantTimeCompare(expectedTag[:], tag) != 1 {
   168  		for i := range out {
   169  			out[i] = 0
   170  		}
   171  		return nil, errOpen
   172  	}
   173  
   174  	return ret, nil
   175  }