Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions cng/aes.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,40 @@ func (g *aesGCM) Seal(dst, nonce, plaintext, additionalData []byte) []byte {
return ret
}

func (g *aesGCM) SealWithRandomNonce(out, nonce, plaintext, additionalData []byte) {
if uint64(len(plaintext)) > uint64((1<<32)-2)*aesBlockSize {
panic("crypto/cipher: message too large for GCM")
}
if len(nonce) != gcmStandardNonceSize {
panic("crypto/cipher: incorrect nonce length given to GCMWithRandomNonce")
}
if len(out) != len(plaintext)+gcmTagSize {
panic("crypto/cipher: incorrect output length given to GCMWithRandomNonce")
}
if subtle.InexactOverlap(out, plaintext) {
panic("crypto/cipher: invalid buffer overlap of output and input")
}
if subtle.AnyOverlap(out, additionalData) {
panic("crypto/cipher: invalid buffer overlap of output and additional data")
}

if g.tls != cipherGCMTLSNone {
panic("cipher: TLS 1.2 and 1.3 modes do not support random nonce")
}

RandReader.Read(nonce)
info := bcrypt.NewAUTHENTICATED_CIPHER_MODE_INFO(nonce, additionalData, out[len(out)-gcmTagSize:])
var encSize uint32
err := bcrypt.Encrypt(g.kh, plaintext, unsafe.Pointer(info), nil, out, &encSize, 0)
if err != nil {
panic(err)
}
if int(encSize) != len(plaintext) {
panic("crypto/cipher: plaintext not fully encrypted")
}
runtime.KeepAlive(g)
}

var errOpen = errors.New("cipher: message authentication failed")

func (g *aesGCM) Open(dst, nonce, ciphertext, additionalData []byte) ([]byte, error) {
Expand Down