crypto

package
v0.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 9, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	PRF_HMAC_MD5      = &hmacPRF{newHash: md5.New, keyLen: 16}
	PRF_HMAC_SHA1     = &hmacPRF{newHash: sha1.New, keyLen: 20}
	PRF_HMAC_SHA2_256 = &hmacPRF{newHash: sha256.New, keyLen: 32}
	PRF_HMAC_SHA2_384 = &hmacPRF{newHash: sha512.New384, keyLen: 48}
	PRF_HMAC_SHA2_512 = &hmacPRF{newHash: sha512.New, keyLen: 64}
)

Functions

func ComputeHMAC

func ComputeHMAC(hashFunc func() hash.Hash, key, data []byte) []byte

ComputeHMAC 通用 HMAC 计算函数

func EncodeSQN

func EncodeSQN(sqn uint64) []byte

EncodeSQN 将 SQN 编码为 6 字节

func PrfPlus

func PrfPlus(prf PRF, key []byte, seed []byte, totalBytes int) ([]byte, error)

RFC 7296 2.13 节. 生成密钥材料 prf+ (K,S) = T1 | T2 | T3 | T4 | ... T1 = prf (K, S | 0x01) T2 = prf (K, T1 | S | 0x02) T3 = prf (K, T2 | S | 0x03)

func RandomBytes

func RandomBytes(n int) ([]byte, error)

随机数生成

func VerifyHMAC

func VerifyHMAC(hashFunc func() hash.Hash, key, data, expectedMAC []byte) bool

VerifyHMAC 通用 HMAC 验证函数

Types

type DiffieHellman

type DiffieHellman struct {
	Group      uint16
	PrivateKey *big.Int
	PublicKey  *big.Int
	SharedKey  []byte
	P          *big.Int
	G          *big.Int
}

func NewDiffieHellman

func NewDiffieHellman(group uint16) (*DiffieHellman, error)

func (*DiffieHellman) ComputeSharedSecret

func (dh *DiffieHellman) ComputeSharedSecret(peerPubKeyBytes []byte) ([]byte, error)

func (*DiffieHellman) GenerateKey

func (dh *DiffieHellman) GenerateKey() error

func (*DiffieHellman) PublicKeyBytes

func (dh *DiffieHellman) PublicKeyBytes() []byte

type Encrypter

type Encrypter interface {
	Encrypt(plaintext []byte, key []byte, iv []byte, aad []byte) ([]byte, error)
	Decrypt(ciphertext []byte, key []byte, iv []byte, aad []byte) ([]byte, error)
	IVSize() int
	BlockSize() int
	KeySize() int // 返回密钥长度 (不含盐)
}

加密接口

func GetEncrypter

func GetEncrypter(id uint16) (Encrypter, error)

工厂函数

func GetEncrypterWithKeyLen

func GetEncrypterWithKeyLen(id uint16, keyLenBits int) (Encrypter, error)

type FIPS1862PRFSHA1

type FIPS1862PRFSHA1 struct {
	// contains filtered or unexported fields
}

func NewFIPS1862PRFSHA1

func NewFIPS1862PRFSHA1(key []byte) *FIPS1862PRFSHA1

func (*FIPS1862PRFSHA1) Bytes

func (p *FIPS1862PRFSHA1) Bytes(seed []byte, outLen int) []byte

type IntegrityAlgorithm

type IntegrityAlgorithm interface {
	// Compute 计算 MAC
	Compute(key, data []byte) []byte
	// Verify 验证 MAC
	Verify(key, data, expectedMAC []byte) bool
	// Output 长度
	OutputSize() int
	// Key 长度
	KeySize() int
}

IntegrityAlgorithm 完整性算法接口

func GetIntegrityAlgorithm

func GetIntegrityAlgorithm(id uint16) (IntegrityAlgorithm, error)

GetIntegrityAlgorithm 根据 ID 获取完整性算法

type Milenage

type Milenage struct {
	K   [16]byte // 128 位用户密钥
	OP  [16]byte // 128 位运营商变体算法配置字段
	OPc [16]byte // 派生的 OPc = AES_K(OP) ⊕ OP
}

Milenage 实现 3GPP TS 35.206 规范的认证算法 用于 EAP-AKA 认证,支持软件 SIM (不需要物理 SIM 卡)

func NewMilenage

func NewMilenage(k, op []byte, useOPc bool) (*Milenage, error)

NewMilenage 创建 Milenage 实例 k: 128 位用户密钥 op: 128 位运营商密钥 (OP 或 OPc) useOPc: 如果为 true,则 op 参数是 OPc,否则是 OP

func (*Milenage) F1

func (m *Milenage) F1(rand, sqn, amf []byte) (macA, macS []byte, err error)

F1 计算网络认证码 MAC-A 和重同步认证码 MAC-S 输入: RAND (16字节), SQN (6字节), AMF (2字节) 输出: MAC-A (8字节), MAC-S (8字节)

func (*Milenage) F2F5

func (m *Milenage) F2F5(rand []byte) (res, ak []byte, err error)

F2F5 计算响应 RES 和匿名密钥 AK 输入: RAND (16字节) 输出: RES (8字节), AK (6字节)

func (*Milenage) F3

func (m *Milenage) F3(rand []byte) (ck []byte, err error)

F3 计算加密密钥 CK 输入: RAND (16字节) 输出: CK (16字节)

func (*Milenage) F4

func (m *Milenage) F4(rand []byte) (ik []byte, err error)

F4 计算完整性密钥 IK 输入: RAND (16字节) 输出: IK (16字节)

func (*Milenage) F5Star

func (m *Milenage) F5Star(rand []byte) (akStar []byte, err error)

F5Star 计算重同步用的匿名密钥 AK* 输入: RAND (16字节) 输出: AK* (6字节)

func (*Milenage) GenerateAUTN

func (m *Milenage) GenerateAUTN(rand, sqn, amf []byte) (autn []byte, err error)

GenerateAUTN 生成认证令牌 AUTN AUTN = (SQN ⊕ AK) || AMF || MAC-A

func (*Milenage) GenerateAUTS

func (m *Milenage) GenerateAUTS(rand, sqn []byte) ([]byte, error)

GenerateAUTS 生成重同步参数 AUTS = (SQN ⊕ AK*) || MAC-S

func (*Milenage) VerifyAUTN

func (m *Milenage) VerifyAUTN(rand, autn []byte, expectedSQN uint64) (res, ck, ik, auts []byte, err error)

VerifyAUTN 验证 AUTN 并返回 RES, CK, IK 如果验证失败返回 AUTS (用于重同步)

type PRF

type PRF interface {
	Hash() hash.Hash
	KeyLen() int
}

PRF (伪随机函数) 接口

func GetPRF

func GetPRF(id uint16) (PRF, error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL