* * Copyright 2018 gRPC authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http:www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. *

package conn

import (
	
	

	core 
)

Overflow length n in bytes, never encrypt more than 2^(n*8) frames (in each direction).
aes128gcm is the struct that holds necessary information for ALTS record. The counter value is NOT included in the payload during the encryption and decryption operations.
inCounter is used in ALTS record to check that incoming counters are as expected, since ALTS record guarantees that messages are unwrapped in the same order that the peer wrapped them.
NewAES128GCM creates an instance that uses aes128gcm for ALTS record.
func ( core.Side,  []byte) (ALTSRecordCrypto, error) {
	,  := aes.NewCipher()
	if  != nil {
		return nil, 
	}
	,  := cipher.NewGCM()
	if  != nil {
		return nil, 
	}
	return &aes128gcm{
		inCounter:  NewInCounter(, overflowLenAES128GCM),
		outCounter: NewOutCounter(, overflowLenAES128GCM),
		aead:       ,
	}, nil
}
Encrypt is the encryption function. dst can contain bytes at the beginning of the ciphertext that will not be encrypted but will be authenticated. If dst has enough capacity to hold these bytes, the ciphertext and the tag, no allocation and copy operations will be performed. dst and plaintext do not overlap.
If we need to allocate an output buffer, we want to include space for GCM tag to avoid forcing ALTS record to reallocate as well.
	 := len()
	,  := SliceForAppend(, len()+GcmTagSize)
	,  := .outCounter.Value()
	if  != nil {
		return nil, 
	}
	 := [:len()]
	copy(, ) // data may alias plaintext
Seal appends the ciphertext and the tag to its first argument and returns the updated slice. However, SliceForAppend above ensures that dst has enough capacity to avoid a reallocation and copy due to the append.
	 = .aead.Seal([:], , , nil)
	.outCounter.Inc()
	return , nil
}

func ( *aes128gcm) () int {
	return GcmTagSize
}

func ( *aes128gcm) (,  []byte) ([]byte, error) {
	,  := .inCounter.Value()
	if  != nil {
		return nil, 
If dst is equal to ciphertext[:0], ciphertext storage is reused.
	,  := .aead.Open(, , , nil)
	if  != nil {
		return nil, ErrAuth
	}
	.inCounter.Inc()
	return , nil