github.com/corona10/go@v0.0.0-20180224231303-7a218942be57/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 gcmMinimumTagSize = 12 // NIST SP 800-38D recommends tags with 12 or more bytes. 40 gcmStandardNonceSize = 12 41 ) 42 43 var errOpen = errors.New("cipher: message authentication failed") 44 45 // aesCipherGCM implements crypto/cipher.gcmAble so that crypto/cipher.NewGCM 46 // will use the optimised implementation in this file when possible. Instances 47 // of this type only exist when hasGCMAsm returns true. 48 type aesCipherGCM struct { 49 aesCipherAsm 50 } 51 52 // Assert that aesCipherGCM implements the gcmAble interface. 53 var _ gcmAble = (*aesCipherGCM)(nil) 54 55 // NewGCM returns the AES cipher wrapped in Galois Counter Mode. This is only 56 // called by crypto/cipher.NewGCM via the gcmAble interface. 57 func (c *aesCipherGCM) NewGCM(nonceSize, tagSize int) (cipher.AEAD, error) { 58 g := &gcmAsm{ks: c.enc, nonceSize: nonceSize, tagSize: tagSize} 59 gcmAesInit(&g.productTable, g.ks) 60 return g, nil 61 } 62 63 type gcmAsm struct { 64 // ks is the key schedule, the length of which depends on the size of 65 // the AES key. 66 ks []uint32 67 // productTable contains pre-computed multiples of the binary-field 68 // element used in GHASH. 69 productTable [256]byte 70 // nonceSize contains the expected size of the nonce, in bytes. 71 nonceSize int 72 // tagSize contains the size of the tag, in bytes. 73 tagSize int 74 } 75 76 func (g *gcmAsm) NonceSize() int { 77 return g.nonceSize 78 } 79 80 func (g *gcmAsm) Overhead() int { 81 return g.tagSize 82 } 83 84 // sliceForAppend takes a slice and a requested number of bytes. It returns a 85 // slice with the contents of the given slice followed by that many bytes and a 86 // second slice that aliases into it and contains only the extra bytes. If the 87 // original slice has sufficient capacity then no allocation is performed. 88 func sliceForAppend(in []byte, n int) (head, tail []byte) { 89 if total := len(in) + n; cap(in) >= total { 90 head = in[:total] 91 } else { 92 head = make([]byte, total) 93 copy(head, in) 94 } 95 tail = head[len(in):] 96 return 97 } 98 99 // Seal encrypts and authenticates plaintext. See the cipher.AEAD interface for 100 // details. 101 func (g *gcmAsm) Seal(dst, nonce, plaintext, data []byte) []byte { 102 if len(nonce) != g.nonceSize { 103 panic("cipher: incorrect nonce length given to GCM") 104 } 105 if uint64(len(plaintext)) > ((1<<32)-2)*BlockSize { 106 panic("cipher: message too large for GCM") 107 } 108 109 var counter, tagMask [gcmBlockSize]byte 110 111 if len(nonce) == gcmStandardNonceSize { 112 // Init counter to nonce||1 113 copy(counter[:], nonce) 114 counter[gcmBlockSize-1] = 1 115 } else { 116 // Otherwise counter = GHASH(nonce) 117 gcmAesData(&g.productTable, nonce, &counter) 118 gcmAesFinish(&g.productTable, &tagMask, &counter, uint64(len(nonce)), uint64(0)) 119 } 120 121 aesEncBlock(&tagMask, &counter, g.ks) 122 123 var tagOut [gcmTagSize]byte 124 gcmAesData(&g.productTable, data, &tagOut) 125 126 ret, out := sliceForAppend(dst, len(plaintext)+g.tagSize) 127 if len(plaintext) > 0 { 128 gcmAesEnc(&g.productTable, out, plaintext, &counter, &tagOut, g.ks) 129 } 130 gcmAesFinish(&g.productTable, &tagMask, &tagOut, uint64(len(plaintext)), uint64(len(data))) 131 copy(out[len(plaintext):], tagOut[:]) 132 133 return ret 134 } 135 136 // Open authenticates and decrypts ciphertext. See the cipher.AEAD interface 137 // for details. 138 func (g *gcmAsm) Open(dst, nonce, ciphertext, data []byte) ([]byte, error) { 139 if len(nonce) != g.nonceSize { 140 panic("cipher: incorrect nonce length given to GCM") 141 } 142 // Sanity check to prevent the authentication from always succeeding if an implementation 143 // leaves tagSize uninitialized, for example. 144 if g.tagSize < gcmMinimumTagSize { 145 panic("cipher: incorrect GCM tag size") 146 } 147 148 if len(ciphertext) < g.tagSize { 149 return nil, errOpen 150 } 151 if uint64(len(ciphertext)) > ((1<<32)-2)*uint64(BlockSize)+uint64(g.tagSize) { 152 return nil, errOpen 153 } 154 155 tag := ciphertext[len(ciphertext)-g.tagSize:] 156 ciphertext = ciphertext[:len(ciphertext)-g.tagSize] 157 158 // See GCM spec, section 7.1. 159 var counter, tagMask [gcmBlockSize]byte 160 161 if len(nonce) == gcmStandardNonceSize { 162 // Init counter to nonce||1 163 copy(counter[:], nonce) 164 counter[gcmBlockSize-1] = 1 165 } else { 166 // Otherwise counter = GHASH(nonce) 167 gcmAesData(&g.productTable, nonce, &counter) 168 gcmAesFinish(&g.productTable, &tagMask, &counter, uint64(len(nonce)), uint64(0)) 169 } 170 171 aesEncBlock(&tagMask, &counter, g.ks) 172 173 var expectedTag [gcmTagSize]byte 174 gcmAesData(&g.productTable, data, &expectedTag) 175 176 ret, out := sliceForAppend(dst, len(ciphertext)) 177 if len(ciphertext) > 0 { 178 gcmAesDec(&g.productTable, out, ciphertext, &counter, &expectedTag, g.ks) 179 } 180 gcmAesFinish(&g.productTable, &tagMask, &expectedTag, uint64(len(ciphertext)), uint64(len(data))) 181 182 if subtle.ConstantTimeCompare(expectedTag[:g.tagSize], tag) != 1 { 183 for i := range out { 184 out[i] = 0 185 } 186 return nil, errOpen 187 } 188 189 return ret, nil 190 }