package aes
import (
"crypto/internal/fips140/alias"
"crypto/internal/fips140/subtle"
)
type CBCEncrypter struct {
b Block
iv [BlockSize ]byte
}
func NewCBCEncrypter (b *Block , iv [BlockSize ]byte ) *CBCEncrypter {
return &CBCEncrypter {b : *b , iv : iv }
}
func (c *CBCEncrypter ) BlockSize () int { return BlockSize }
func (c *CBCEncrypter ) CryptBlocks (dst , src []byte ) {
if len (src )%BlockSize != 0 {
panic ("crypto/cipher: input not full blocks" )
}
if len (dst ) < len (src ) {
panic ("crypto/cipher: output smaller than input" )
}
if alias .InexactOverlap (dst [:len (src )], src ) {
panic ("crypto/cipher: invalid buffer overlap" )
}
if len (src ) == 0 {
return
}
cryptBlocksEnc (&c .b , &c .iv , dst , src )
}
func (x *CBCEncrypter ) SetIV (iv []byte ) {
if len (iv ) != len (x .iv ) {
panic ("cipher: incorrect length IV" )
}
copy (x .iv [:], iv )
}
func cryptBlocksEncGeneric(b *Block , civ *[BlockSize ]byte , dst , src []byte ) {
iv := civ [:]
for len (src ) > 0 {
subtle .XORBytes (dst [:BlockSize ], src [:BlockSize ], iv )
b .Encrypt (dst [:BlockSize ], dst [:BlockSize ])
iv = dst [:BlockSize ]
src = src [BlockSize :]
dst = dst [BlockSize :]
}
copy (civ [:], iv )
}
type CBCDecrypter struct {
b Block
iv [BlockSize ]byte
}
func NewCBCDecrypter (b *Block , iv [BlockSize ]byte ) *CBCDecrypter {
return &CBCDecrypter {b : *b , iv : iv }
}
func (c *CBCDecrypter ) BlockSize () int { return BlockSize }
func (c *CBCDecrypter ) CryptBlocks (dst , src []byte ) {
if len (src )%BlockSize != 0 {
panic ("crypto/cipher: input not full blocks" )
}
if len (dst ) < len (src ) {
panic ("crypto/cipher: output smaller than input" )
}
if alias .InexactOverlap (dst [:len (src )], src ) {
panic ("crypto/cipher: invalid buffer overlap" )
}
if len (src ) == 0 {
return
}
cryptBlocksDec (&c .b , &c .iv , dst , src )
}
func (x *CBCDecrypter ) SetIV (iv []byte ) {
if len (iv ) != len (x .iv ) {
panic ("cipher: incorrect length IV" )
}
copy (x .iv [:], iv )
}
func cryptBlocksDecGeneric(b *Block , civ *[BlockSize ]byte , dst , src []byte ) {
end := len (src )
start := end - BlockSize
prev := start - BlockSize
iv := *civ
copy (civ [:], src [start :end ])
for start >= 0 {
b .Decrypt (dst [start :end ], src [start :end ])
if start > 0 {
subtle .XORBytes (dst [start :end ], dst [start :end ], src [prev :start ])
} else {
subtle .XORBytes (dst [start :end ], dst [start :end ], iv [:])
}
end -= BlockSize
start -= BlockSize
prev -= BlockSize
}
}
The pages are generated with Golds v0.7.3-preview . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .