gmsm/zuc/eea.go

56 lines
2.4 KiB
Go
Raw Normal View History

// Package zuc implements ShangMi(SM) zuc stream cipher and integrity algorithm.
2022-04-19 11:25:14 +08:00
package zuc
import (
2024-11-29 11:44:59 +08:00
"github.com/emmansun/gmsm/cipher"
"github.com/emmansun/gmsm/internal/zuc"
2022-04-19 11:25:14 +08:00
)
const (
// IV size in bytes for zuc 128
IVSize128 = zuc.IVSize128
// IV size in bytes for zuc 256
IVSize256 = zuc.IVSize256
// number of words in a round
RoundWords = zuc.RoundWords
// number of bytes in a word
WordSize = zuc.WordSize
// number of bytes in a round
RoundBytes = zuc.RoundBytes
)
2022-06-30 11:29:42 +08:00
2022-04-19 11:25:14 +08:00
// NewCipher create a stream cipher based on key and iv aguments.
2024-11-22 08:33:24 +08:00
// The key must be 16 bytes long and iv must be 16 bytes long for zuc 128;
// or the key must be 32 bytes long and iv must be 23 bytes long for zuc 256;
// otherwise, an error will be returned.
2024-11-29 11:44:59 +08:00
func NewCipher(key, iv []byte) (cipher.SeekableStream, error) {
return zuc.NewCipher(key, iv)
2022-04-19 11:25:14 +08:00
}
// NewEEACipher create a stream cipher based on key, count, bearer and direction arguments according specification.
2024-11-22 08:33:24 +08:00
// The key must be 16 bytes long and iv must be 16 bytes long, otherwise, an error will be returned.
2024-11-29 11:44:59 +08:00
// The count is the 32-bit counter value, the bearer is the 5-bit bearer identity and the direction is the 1-bit
2024-11-22 08:33:24 +08:00
// transmission direction flag.
2024-11-29 11:44:59 +08:00
func NewEEACipher(key []byte, count, bearer, direction uint32) (cipher.SeekableStream, error) {
return zuc.NewEEACipher(key, count, bearer, direction)
2022-04-19 11:25:14 +08:00
}
// NewCipherWithBucketSize create a new instance of the eea cipher with the specified
// bucket size. The bucket size is rounded up to the nearest multiple of RoundBytes.
//
// The implementation of this function is used for XORKeyStreamAt function optimization, which will keep states
// for seekable stream cipher once the bucketSize is greater than 0.
func NewCipherWithBucketSize(key, iv []byte, bucketSize int) (cipher.SeekableStream, error) {
return zuc.NewCipherWithBucketSize(key, iv, bucketSize)
}
// NewEEACipherWithBucketSize creates a new instance of a seekable stream cipher
// for the EEA encryption algorithm with a specified bucket size. This function
// is typically used in mobile communication systems for secure data encryption.
//
// The implementation of this function is used for XORKeyStreamAt function optimization, which will keep states
// for seekable stream cipher once the bucketSize is greater than 0.
func NewEEACipherWithBucketSize(key []byte, count, bearer, direction uint32, bucketSize int) (cipher.SeekableStream, error) {
return zuc.NewEEACipherWithBucketSize(key, count, bearer, direction, bucketSize)
}