github.com/sean-/go@v0.0.0-20151219100004-97f854cd7bb6/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  	aesCipher
    49  }
    50  
    51  // NewGCM returns the AES cipher wrapped in Galois Counter Mode. This is only
    52  // called by crypto/cipher.NewGCM via the gcmAble interface.
    53  func (c *aesCipherGCM) NewGCM(nonceSize int) (cipher.AEAD, error) {
    54  	g := &gcmAsm{ks: c.enc, nonceSize: nonceSize}
    55  	gcmAesInit(&g.productTable, g.ks)
    56  	return g, nil
    57  }
    58  
    59  type gcmAsm struct {
    60  	// ks is the key schedule, the length of which depends on the size of
    61  	// the AES key.
    62  	ks []uint32
    63  	// productTable contains pre-computed multiples of the binary-field
    64  	// element used in GHASH.
    65  	productTable [256]byte
    66  	// nonceSize contains the expected size of the nonce, in bytes.
    67  	nonceSize int
    68  }
    69  
    70  func (g *gcmAsm) NonceSize() int {
    71  	return g.nonceSize
    72  }
    73  
    74  func (*gcmAsm) Overhead() int {
    75  	return gcmTagSize
    76  }
    77  
    78  // sliceForAppend takes a slice and a requested number of bytes. It returns a
    79  // slice with the contents of the given slice followed by that many bytes and a
    80  // second slice that aliases into it and contains only the extra bytes. If the
    81  // original slice has sufficient capacity then no allocation is performed.
    82  func sliceForAppend(in []byte, n int) (head, tail []byte) {
    83  	if total := len(in) + n; cap(in) >= total {
    84  		head = in[:total]
    85  	} else {
    86  		head = make([]byte, total)
    87  		copy(head, in)
    88  	}
    89  	tail = head[len(in):]
    90  	return
    91  }
    92  
    93  // Seal encrypts and authenticates plaintext. See the cipher.AEAD interface for
    94  // details.
    95  func (g *gcmAsm) Seal(dst, nonce, plaintext, data []byte) []byte {
    96  	if len(nonce) != g.nonceSize {
    97  		panic("cipher: incorrect nonce length given to GCM")
    98  	}
    99  
   100  	var counter, tagMask [gcmBlockSize]byte
   101  
   102  	if len(nonce) == gcmStandardNonceSize {
   103  		// Init counter to nonce||1
   104  		copy(counter[:], nonce)
   105  		counter[gcmBlockSize-1] = 1
   106  	} else {
   107  		// Otherwise counter = GHASH(nonce)
   108  		gcmAesData(&g.productTable, nonce, &counter)
   109  		gcmAesFinish(&g.productTable, &tagMask, &counter, uint64(len(nonce)), uint64(0))
   110  	}
   111  
   112  	aesEncBlock(&tagMask, &counter, g.ks)
   113  
   114  	var tagOut [gcmTagSize]byte
   115  	gcmAesData(&g.productTable, data, &tagOut)
   116  
   117  	ret, out := sliceForAppend(dst, len(plaintext)+gcmTagSize)
   118  	if len(plaintext) > 0 {
   119  		gcmAesEnc(&g.productTable, out, plaintext, &counter, &tagOut, g.ks)
   120  	}
   121  	gcmAesFinish(&g.productTable, &tagMask, &tagOut, uint64(len(plaintext)), uint64(len(data)))
   122  	copy(out[len(plaintext):], tagOut[:])
   123  
   124  	return ret
   125  }
   126  
   127  // Open authenticates and decrypts ciphertext. See the cipher.AEAD interface
   128  // for details.
   129  func (g *gcmAsm) Open(dst, nonce, ciphertext, data []byte) ([]byte, error) {
   130  	if len(nonce) != g.nonceSize {
   131  		panic("cipher: incorrect nonce length given to GCM")
   132  	}
   133  
   134  	if len(ciphertext) < gcmTagSize {
   135  		return nil, errOpen
   136  	}
   137  	tag := ciphertext[len(ciphertext)-gcmTagSize:]
   138  	ciphertext = ciphertext[:len(ciphertext)-gcmTagSize]
   139  
   140  	// See GCM spec, section 7.1.
   141  	var counter, tagMask [gcmBlockSize]byte
   142  
   143  	if len(nonce) == gcmStandardNonceSize {
   144  		// Init counter to nonce||1
   145  		copy(counter[:], nonce)
   146  		counter[gcmBlockSize-1] = 1
   147  	} else {
   148  		// Otherwise counter = GHASH(nonce)
   149  		gcmAesData(&g.productTable, nonce, &counter)
   150  		gcmAesFinish(&g.productTable, &tagMask, &counter, uint64(len(nonce)), uint64(0))
   151  	}
   152  
   153  	aesEncBlock(&tagMask, &counter, g.ks)
   154  
   155  	var expectedTag [gcmTagSize]byte
   156  	gcmAesData(&g.productTable, data, &expectedTag)
   157  
   158  	ret, out := sliceForAppend(dst, len(ciphertext))
   159  	if len(ciphertext) > 0 {
   160  		gcmAesDec(&g.productTable, out, ciphertext, &counter, &expectedTag, g.ks)
   161  	}
   162  	gcmAesFinish(&g.productTable, &tagMask, &expectedTag, uint64(len(ciphertext)), uint64(len(data)))
   163  
   164  	if subtle.ConstantTimeCompare(expectedTag[:], tag) != 1 {
   165  		for i := range out {
   166  			out[i] = 0
   167  		}
   168  		return nil, errOpen
   169  	}
   170  
   171  	return ret, nil
   172  }