gmsm/cipher/xts.go

69 lines
3.3 KiB
Go
Raw Normal View History

2022-01-21 11:24:10 +08:00
package cipher
import (
"crypto/cipher"
2024-11-21 14:32:32 +08:00
"github.com/emmansun/gmsm/internal/byteorder"
"github.com/emmansun/gmsm/internal/cipher/xts"
2022-01-21 11:24:10 +08:00
)
2023-08-17 12:48:53 +08:00
// NewXTSEncrypter creates a Cipher given a function for creating the underlying
// block cipher (which must have a block size of 16 bytes).
func NewXTSEncrypter(cipherFunc CipherCreator, key, tweakKey, tweak []byte) (cipher.BlockMode, error) {
return xts.NewXTSEncrypter(cipherFunc, key, tweakKey, tweak, false)
2023-08-17 12:48:53 +08:00
}
// NewXTSEncrypterWithSector creates a Cipher given a function for creating the underlying
// block cipher (which must have a block size of 16 bytes) with sector number.
func NewXTSEncrypterWithSector(cipherFunc CipherCreator, key, tweakKey []byte, sectorNum uint64) (cipher.BlockMode, error) {
2023-08-17 12:48:53 +08:00
tweak := make([]byte, blockSize)
2024-11-21 14:32:32 +08:00
byteorder.LEPutUint64(tweak[:8], sectorNum)
2023-08-17 12:48:53 +08:00
return NewXTSEncrypter(cipherFunc, key, tweakKey, tweak)
}
// NewGBXTSEncrypter creates a Cipher given a function for creating the underlying
// block cipher (which must have a block size of 16 bytes).
2023-08-08 17:26:08 +08:00
// It follows GB/T 17964-2021.
func NewGBXTSEncrypter(cipherFunc CipherCreator, key, tweakKey, tweak []byte) (cipher.BlockMode, error) {
return xts.NewXTSEncrypter(cipherFunc, key, tweakKey, tweak, true)
2022-01-21 11:24:10 +08:00
}
2023-08-17 12:48:53 +08:00
// NewGBXTSEncrypterWithSector creates a Cipher given a function for creating the underlying
// block cipher (which must have a block size of 16 bytes) with sector number.
// It follows GB/T 17964-2021.
func NewGBXTSEncrypterWithSector(cipherFunc CipherCreator, key, tweakKey []byte, sectorNum uint64) (cipher.BlockMode, error) {
2023-08-17 12:48:53 +08:00
tweak := make([]byte, blockSize)
2024-11-21 14:32:32 +08:00
byteorder.LEPutUint64(tweak[:8], sectorNum)
2023-08-17 12:48:53 +08:00
return NewGBXTSEncrypter(cipherFunc, key, tweakKey, tweak)
2023-08-08 17:26:08 +08:00
}
2023-08-17 12:48:53 +08:00
// NewXTSDecrypter creates a Cipher given a function for creating the underlying
// block cipher (which must have a block size of 16 bytes) for decryption.
func NewXTSDecrypter(cipherFunc CipherCreator, key, tweakKey, tweak []byte) (cipher.BlockMode, error) {
return xts.NewXTSDecrypter(cipherFunc, key, tweakKey, tweak, false)
2023-08-17 12:48:53 +08:00
}
// NewXTSDecrypterWithSector creates a Cipher given a function for creating the underlying
// block cipher (which must have a block size of 16 bytes) with sector number for decryption.
func NewXTSDecrypterWithSector(cipherFunc CipherCreator, key, tweakKey []byte, sectorNum uint64) (cipher.BlockMode, error) {
2023-08-17 12:48:53 +08:00
tweak := make([]byte, blockSize)
2024-11-21 14:32:32 +08:00
byteorder.LEPutUint64(tweak[:8], sectorNum)
2023-08-17 12:48:53 +08:00
return NewXTSDecrypter(cipherFunc, key, tweakKey, tweak)
}
// NewGBXTSDecrypter creates a Cipher given a function for creating the underlying
// block cipher (which must have a block size of 16 bytes) for decryption.
// It follows GB/T 17964-2021.
func NewGBXTSDecrypter(cipherFunc CipherCreator, key, tweakKey, tweak []byte) (cipher.BlockMode, error) {
return xts.NewXTSDecrypter(cipherFunc, key, tweakKey, tweak, true)
2023-08-17 12:48:53 +08:00
}
// NewGBXTSDecrypterWithSector creates a Cipher given a function for creating the underlying
// block cipher (which must have a block size of 16 bytes) with sector number for decryption.
// It follows GB/T 17964-2021.
func NewGBXTSDecrypterWithSector(cipherFunc CipherCreator, key, tweakKey []byte, sectorNum uint64) (cipher.BlockMode, error) {
2023-08-17 12:48:53 +08:00
tweak := make([]byte, blockSize)
2024-11-21 14:32:32 +08:00
byteorder.LEPutUint64(tweak[:8], sectorNum)
2023-08-17 12:48:53 +08:00
return NewGBXTSDecrypter(cipherFunc, key, tweakKey, tweak)
2023-08-08 17:26:08 +08:00
}