| |
| |
| |
|
|
| package cipher |
|
|
| import ( |
| "crypto/internal/fips140/aes" |
| "crypto/internal/fips140/aes/gcm" |
| "crypto/internal/fips140/alias" |
| "crypto/internal/fips140only" |
| "crypto/subtle" |
| "errors" |
| "internal/byteorder" |
| ) |
|
|
| const ( |
| gcmBlockSize = 16 |
| gcmStandardNonceSize = 12 |
| gcmTagSize = 16 |
| gcmMinimumTagSize = 12 |
| ) |
|
|
| |
| |
| |
| |
| |
| |
| func NewGCM(cipher Block) (AEAD, error) { |
| if fips140only.Enforced() { |
| return nil, errors.New("crypto/cipher: use of GCM with arbitrary IVs is not allowed in FIPS 140-only mode, use NewGCMWithRandomNonce") |
| } |
| return newGCM(cipher, gcmStandardNonceSize, gcmTagSize) |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| func NewGCMWithNonceSize(cipher Block, size int) (AEAD, error) { |
| if fips140only.Enforced() { |
| return nil, errors.New("crypto/cipher: use of GCM with arbitrary IVs is not allowed in FIPS 140-only mode, use NewGCMWithRandomNonce") |
| } |
| return newGCM(cipher, size, gcmTagSize) |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| func NewGCMWithTagSize(cipher Block, tagSize int) (AEAD, error) { |
| if fips140only.Enforced() { |
| return nil, errors.New("crypto/cipher: use of GCM with arbitrary IVs is not allowed in FIPS 140-only mode, use NewGCMWithRandomNonce") |
| } |
| return newGCM(cipher, gcmStandardNonceSize, tagSize) |
| } |
|
|
| func newGCM(cipher Block, nonceSize, tagSize int) (AEAD, error) { |
| c, ok := cipher.(*aes.Block) |
| if !ok { |
| if fips140only.Enforced() { |
| return nil, errors.New("crypto/cipher: use of GCM with non-AES ciphers is not allowed in FIPS 140-only mode") |
| } |
| return newGCMFallback(cipher, nonceSize, tagSize) |
| } |
| |
| |
| g, err := gcm.New(c, nonceSize, tagSize) |
| if err != nil { |
| return nil, err |
| } |
| return g, nil |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| func NewGCMWithRandomNonce(cipher Block) (AEAD, error) { |
| c, ok := cipher.(*aes.Block) |
| if !ok { |
| return nil, errors.New("cipher: NewGCMWithRandomNonce requires aes.Block") |
| } |
| g, err := gcm.New(c, gcmStandardNonceSize, gcmTagSize) |
| if err != nil { |
| return nil, err |
| } |
| return gcmWithRandomNonce{g}, nil |
| } |
|
|
| type gcmWithRandomNonce struct { |
| *gcm.GCM |
| } |
|
|
| func (g gcmWithRandomNonce) NonceSize() int { |
| return 0 |
| } |
|
|
| func (g gcmWithRandomNonce) Overhead() int { |
| return gcmStandardNonceSize + gcmTagSize |
| } |
|
|
| func (g gcmWithRandomNonce) Seal(dst, nonce, plaintext, additionalData []byte) []byte { |
| if len(nonce) != 0 { |
| panic("crypto/cipher: non-empty nonce passed to GCMWithRandomNonce") |
| } |
|
|
| ret, out := sliceForAppend(dst, gcmStandardNonceSize+len(plaintext)+gcmTagSize) |
| if alias.InexactOverlap(out, plaintext) { |
| panic("crypto/cipher: invalid buffer overlap of output and input") |
| } |
| if alias.AnyOverlap(out, additionalData) { |
| panic("crypto/cipher: invalid buffer overlap of output and additional data") |
| } |
| nonce = out[:gcmStandardNonceSize] |
| ciphertext := out[gcmStandardNonceSize:] |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| if alias.AnyOverlap(out, plaintext) { |
| copy(ciphertext, plaintext) |
| plaintext = ciphertext[:len(plaintext)] |
| } |
|
|
| gcm.SealWithRandomNonce(g.GCM, nonce, ciphertext, plaintext, additionalData) |
| return ret |
| } |
|
|
| func (g gcmWithRandomNonce) Open(dst, nonce, ciphertext, additionalData []byte) ([]byte, error) { |
| if len(nonce) != 0 { |
| panic("crypto/cipher: non-empty nonce passed to GCMWithRandomNonce") |
| } |
| if len(ciphertext) < gcmStandardNonceSize+gcmTagSize { |
| return nil, errOpen |
| } |
|
|
| ret, out := sliceForAppend(dst, len(ciphertext)-gcmStandardNonceSize-gcmTagSize) |
| if alias.InexactOverlap(out, ciphertext) { |
| panic("crypto/cipher: invalid buffer overlap of output and input") |
| } |
| if alias.AnyOverlap(out, additionalData) { |
| panic("crypto/cipher: invalid buffer overlap of output and additional data") |
| } |
| |
| |
| |
| |
| if alias.AnyOverlap(out, ciphertext) { |
| nonce = make([]byte, gcmStandardNonceSize) |
| copy(nonce, ciphertext) |
| copy(out[:len(ciphertext)], ciphertext[gcmStandardNonceSize:]) |
| ciphertext = out[:len(ciphertext)-gcmStandardNonceSize] |
| } else { |
| nonce = ciphertext[:gcmStandardNonceSize] |
| ciphertext = ciphertext[gcmStandardNonceSize:] |
| } |
|
|
| _, err := g.GCM.Open(out[:0], nonce, ciphertext, additionalData) |
| if err != nil { |
| return nil, err |
| } |
| return ret, nil |
| } |
|
|
| |
| |
| |
| type gcmAble interface { |
| NewGCM(nonceSize, tagSize int) (AEAD, error) |
| } |
|
|
| func newGCMFallback(cipher Block, nonceSize, tagSize int) (AEAD, error) { |
| if tagSize < gcmMinimumTagSize || tagSize > gcmBlockSize { |
| return nil, errors.New("cipher: incorrect tag size given to GCM") |
| } |
| if nonceSize <= 0 { |
| return nil, errors.New("cipher: the nonce can't have zero length") |
| } |
| if cipher, ok := cipher.(gcmAble); ok { |
| return cipher.NewGCM(nonceSize, tagSize) |
| } |
| if cipher.BlockSize() != gcmBlockSize { |
| return nil, errors.New("cipher: NewGCM requires 128-bit block cipher") |
| } |
| return &gcmFallback{cipher: cipher, nonceSize: nonceSize, tagSize: tagSize}, nil |
| } |
|
|
| |
| |
| |
| type gcmFallback struct { |
| cipher Block |
| nonceSize int |
| tagSize int |
| } |
|
|
| func (g *gcmFallback) NonceSize() int { |
| return g.nonceSize |
| } |
|
|
| func (g *gcmFallback) Overhead() int { |
| return g.tagSize |
| } |
|
|
| func (g *gcmFallback) Seal(dst, nonce, plaintext, additionalData []byte) []byte { |
| if len(nonce) != g.nonceSize { |
| panic("crypto/cipher: incorrect nonce length given to GCM") |
| } |
| if g.nonceSize == 0 { |
| panic("crypto/cipher: incorrect GCM nonce size") |
| } |
| if uint64(len(plaintext)) > uint64((1<<32)-2)*gcmBlockSize { |
| panic("crypto/cipher: message too large for GCM") |
| } |
|
|
| ret, out := sliceForAppend(dst, len(plaintext)+g.tagSize) |
| if alias.InexactOverlap(out, plaintext) { |
| panic("crypto/cipher: invalid buffer overlap of output and input") |
| } |
| if alias.AnyOverlap(out, additionalData) { |
| panic("crypto/cipher: invalid buffer overlap of output and additional data") |
| } |
|
|
| var H, counter, tagMask [gcmBlockSize]byte |
| g.cipher.Encrypt(H[:], H[:]) |
| deriveCounter(&H, &counter, nonce) |
| gcmCounterCryptGeneric(g.cipher, tagMask[:], tagMask[:], &counter) |
|
|
| gcmCounterCryptGeneric(g.cipher, out, plaintext, &counter) |
|
|
| var tag [gcmTagSize]byte |
| gcmAuth(tag[:], &H, &tagMask, out[:len(plaintext)], additionalData) |
| copy(out[len(plaintext):], tag[:]) |
|
|
| return ret |
| } |
|
|
| var errOpen = errors.New("cipher: message authentication failed") |
|
|
| func (g *gcmFallback) Open(dst, nonce, ciphertext, additionalData []byte) ([]byte, error) { |
| if len(nonce) != g.nonceSize { |
| panic("crypto/cipher: incorrect nonce length given to GCM") |
| } |
| if g.tagSize < gcmMinimumTagSize { |
| panic("crypto/cipher: incorrect GCM tag size") |
| } |
|
|
| if len(ciphertext) < g.tagSize { |
| return nil, errOpen |
| } |
| if uint64(len(ciphertext)) > uint64((1<<32)-2)*gcmBlockSize+uint64(g.tagSize) { |
| return nil, errOpen |
| } |
|
|
| ret, out := sliceForAppend(dst, len(ciphertext)-g.tagSize) |
| if alias.InexactOverlap(out, ciphertext) { |
| panic("crypto/cipher: invalid buffer overlap of output and input") |
| } |
| if alias.AnyOverlap(out, additionalData) { |
| panic("crypto/cipher: invalid buffer overlap of output and additional data") |
| } |
|
|
| var H, counter, tagMask [gcmBlockSize]byte |
| g.cipher.Encrypt(H[:], H[:]) |
| deriveCounter(&H, &counter, nonce) |
| gcmCounterCryptGeneric(g.cipher, tagMask[:], tagMask[:], &counter) |
|
|
| tag := ciphertext[len(ciphertext)-g.tagSize:] |
| ciphertext = ciphertext[:len(ciphertext)-g.tagSize] |
|
|
| var expectedTag [gcmTagSize]byte |
| gcmAuth(expectedTag[:], &H, &tagMask, ciphertext, additionalData) |
| if subtle.ConstantTimeCompare(expectedTag[:g.tagSize], tag) != 1 { |
| |
| |
| |
| |
| clear(out) |
| return nil, errOpen |
| } |
|
|
| gcmCounterCryptGeneric(g.cipher, out, ciphertext, &counter) |
|
|
| return ret, nil |
| } |
|
|
| func deriveCounter(H, counter *[gcmBlockSize]byte, nonce []byte) { |
| if len(nonce) == gcmStandardNonceSize { |
| copy(counter[:], nonce) |
| counter[gcmBlockSize-1] = 1 |
| } else { |
| lenBlock := make([]byte, 16) |
| byteorder.BEPutUint64(lenBlock[8:], uint64(len(nonce))*8) |
| J := gcm.GHASH(H, nonce, lenBlock) |
| copy(counter[:], J) |
| } |
| } |
|
|
| func gcmCounterCryptGeneric(b Block, out, src []byte, counter *[gcmBlockSize]byte) { |
| var mask [gcmBlockSize]byte |
| for len(src) >= gcmBlockSize { |
| b.Encrypt(mask[:], counter[:]) |
| gcmInc32(counter) |
|
|
| subtle.XORBytes(out, src, mask[:]) |
| out = out[gcmBlockSize:] |
| src = src[gcmBlockSize:] |
| } |
| if len(src) > 0 { |
| b.Encrypt(mask[:], counter[:]) |
| gcmInc32(counter) |
| subtle.XORBytes(out, src, mask[:]) |
| } |
| } |
|
|
| func gcmInc32(counterBlock *[gcmBlockSize]byte) { |
| ctr := counterBlock[len(counterBlock)-4:] |
| byteorder.BEPutUint32(ctr, byteorder.BEUint32(ctr)+1) |
| } |
|
|
| func gcmAuth(out []byte, H, tagMask *[gcmBlockSize]byte, ciphertext, additionalData []byte) { |
| lenBlock := make([]byte, 16) |
| byteorder.BEPutUint64(lenBlock[:8], uint64(len(additionalData))*8) |
| byteorder.BEPutUint64(lenBlock[8:], uint64(len(ciphertext))*8) |
| S := gcm.GHASH(H, additionalData, ciphertext, lenBlock) |
| subtle.XORBytes(out, S, tagMask[:]) |
| } |
|
|
| |
| |
| |
| |
| func sliceForAppend(in []byte, n int) (head, tail []byte) { |
| if total := len(in) + n; cap(in) >= total { |
| head = in[:total] |
| } else { |
| head = make([]byte, total) |
| copy(head, in) |
| } |
| tail = head[len(in):] |
| return |
| } |
|
|