2025-03-11 16:11:18 +08:00
|
|
|
// 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"
|
2025-03-11 16:11:18 +08:00
|
|
|
"github.com/emmansun/gmsm/internal/zuc"
|
2022-04-19 11:25:14 +08:00
|
|
|
)
|
|
|
|
|
2024-11-29 15:19:19 +08:00
|
|
|
const (
|
2025-03-11 16:11:18 +08:00
|
|
|
// IV size in bytes for zuc 128
|
2025-03-28 16:53:29 +08:00
|
|
|
IVSize128 = zuc.IVSize128
|
2025-03-11 16:11:18 +08:00
|
|
|
// IV size in bytes for zuc 256
|
2025-03-28 16:53:29 +08:00
|
|
|
IVSize256 = zuc.IVSize256
|
2024-12-06 16:36:24 +08:00
|
|
|
// number of words in a round
|
2025-03-28 16:53:29 +08:00
|
|
|
RoundWords = zuc.RoundWords
|
2024-12-06 16:36:24 +08:00
|
|
|
// number of bytes in a word
|
2025-03-28 16:53:29 +08:00
|
|
|
WordSize = zuc.WordSize
|
2024-12-06 16:36:24 +08:00
|
|
|
// number of bytes in a round
|
2025-03-28 16:53:29 +08:00
|
|
|
RoundBytes = zuc.RoundBytes
|
2024-11-29 15:19:19 +08:00
|
|
|
)
|
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) {
|
2025-03-11 16:11:18 +08:00
|
|
|
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) {
|
2025-03-11 16:11:18 +08:00
|
|
|
return zuc.NewEEACipher(key, count, bearer, direction)
|
2022-04-19 11:25:14 +08:00
|
|
|
}
|
2025-03-28 16:53:29 +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)
|
|
|
|
}
|