2015-11-03 15:26:16 -05:00
|
|
|
package crypto
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/aes"
|
|
|
|
"crypto/cipher"
|
2017-04-28 08:48:23 -04:00
|
|
|
|
|
|
|
"v2ray.com/core/common"
|
2015-11-03 15:26:16 -05:00
|
|
|
)
|
|
|
|
|
2016-07-26 15:21:22 -04:00
|
|
|
// NewAesDecryptionStream creates a new AES encryption stream based on given key and IV.
|
|
|
|
// Caller must ensure the length of key and IV is either 16, 24 or 32 bytes.
|
2016-02-25 15:50:10 -05:00
|
|
|
func NewAesDecryptionStream(key []byte, iv []byte) cipher.Stream {
|
2018-07-04 11:48:48 -04:00
|
|
|
return NewAesStreamMethod(key, iv, cipher.NewCFBDecrypter)
|
|
|
|
}
|
|
|
|
|
2016-07-26 15:21:22 -04:00
|
|
|
// NewAesEncryptionStream creates a new AES description stream based on given key and IV.
|
|
|
|
// Caller must ensure the length of key and IV is either 16, 24 or 32 bytes.
|
2016-02-25 15:50:10 -05:00
|
|
|
func NewAesEncryptionStream(key []byte, iv []byte) cipher.Stream {
|
2018-07-04 11:48:48 -04:00
|
|
|
return NewAesStreamMethod(key, iv, cipher.NewCFBEncrypter)
|
|
|
|
}
|
|
|
|
|
2018-07-13 08:36:09 -04:00
|
|
|
func NewAesStreamMethod(key []byte, iv []byte, f func(cipher.Block, []byte) cipher.Stream) cipher.Stream {
|
|
|
|
aesBlock, err := aes.NewCipher(key)
|
|
|
|
common.Must(err)
|
|
|
|
return f(aesBlock, iv)
|
|
|
|
}
|
|
|
|
|
2018-07-04 11:48:48 -04:00
|
|
|
func NewAesCTRStream(key []byte, iv []byte) cipher.Stream {
|
|
|
|
return NewAesStreamMethod(key, iv, cipher.NewCTR)
|
2015-11-03 15:26:16 -05:00
|
|
|
}
|
2018-09-12 09:43:36 -04:00
|
|
|
|
|
|
|
func NewAesGcm(key []byte) cipher.AEAD {
|
|
|
|
block, err := aes.NewCipher(key)
|
|
|
|
common.Must(err)
|
|
|
|
aead, err := cipher.NewGCM(block)
|
|
|
|
common.Must(err)
|
|
|
|
return aead
|
|
|
|
}
|