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
|
|
|
|
IVSize128 = 16
|
|
|
|
// IV size in bytes for zuc 256
|
|
|
|
IVSize256 = 23
|
2024-12-06 16:36:24 +08:00
|
|
|
// number of words in a round
|
2024-11-29 15:19:19 +08:00
|
|
|
RoundWords = 32
|
2024-12-06 16:36:24 +08:00
|
|
|
// number of bytes in a word
|
|
|
|
WordSize = 4
|
|
|
|
// number of bytes in a round
|
2024-11-29 15:19:19 +08:00
|
|
|
RoundBytes = RoundWords * WordSize
|
|
|
|
)
|
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
|
|
|
}
|