crypto

package
v1.1.32 Latest Latest
Warning

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

Go to latest
Published: Aug 14, 2026 License: MIT Imports: 20 Imported by: 0

Documentation

Index

Constants

View Source
const (
	Plan2DefaultProvisionDir = "plan2-provision"
	Plan2KeyFileExt          = ".key"
	Plan2PublicFileExt       = ".pem"
)
View Source
const (
	// MLDSA87SignatureSize ML-DSA-87 裸签名长度(字节)。
	MLDSA87SignatureSize = fmldsa.MLDSA87SignatureSize
	// MLDSA87PublicKeySize ML-DSA-87 公钥编码长度(字节)。
	MLDSA87PublicKeySize = fmldsa.MLDSA87PublicKeySize
	// MLDSA87PrivateKeySeedSize ML-DSA-87 私钥种子长度(字节)。
	MLDSA87PrivateKeySeedSize = fmldsa.PrivateKeySize

	// MLKEM1024EncapsulationKeySize ML-KEM-1024 封装公钥长度(字节)。
	MLKEM1024EncapsulationKeySize = 1568
	// MLKEM1024CiphertextSize ML-KEM-1024 KEM 密文长度(字节)。
	MLKEM1024CiphertextSize = 1568
	// MLKEM1024DecapsulationKeySize ML-KEM-1024 解封装私钥种子长度(字节)。
	MLKEM1024DecapsulationKeySize = 64
)
View Source
const Plan2WrapKeyEnv = "MPC_PLAN2_WRAP_KEY"

Plan2WrapKeyEnv 用户自行设置的 AES 包装口令;程序只读取,不生成、不落盘。

Variables

This section is empty.

Functions

func CheckOuterSignatureB64Valid

func CheckOuterSignatureB64Valid(b64 string) bool

CheckOuterSignatureB64Valid 校验 JsonBody.Valid / JsonResp.Valid 外层签名 Base64 长度(ML-DSA-87)。

func CheckRPCXSignatureValid

func CheckRPCXSignatureValid(sig []byte) bool

CheckRPCXSignatureValid 校验 RPCX protobuf 字段 e 的 ML-DSA-87 裸签名长度。

func DecapsulatePeerCiphertext

func DecapsulatePeerCiphertext(dkB64, kemCtB64 string) ([]byte, error)

DecapsulatePeerCiphertext 服务端用缓存的解封装私钥从客户端 KEM 密文恢复共享秘密。

func EncapsulateToPeer

func EncapsulateToPeer(serverEncapKeyB64 string) (sharedKey, kemCtB64 string, err error)

EncapsulateToPeer 向服务端封装密钥(Plan2 /key 之后客户端调用):返回共享秘密与 KEM 密文。

func GeneratePlan2ClientNo

func GeneratePlan2ClientNo(at time.Time) (int64, error)

GeneratePlan2ClientNo 生成 PQC 证书编号(clientNo):yyyyMMddHHmmss + 4 位随机数(共 18 位)。 算法与 open_scanner/model.GenerateAppCipherNo 一致,供 Plan2 Cipher 路由使用。

func LabelSeedSHA256

func LabelSeedSHA256(label string) []byte

LabelSeedSHA256 将标签转为 32 字节种子(测试夹具用)。

func MLDSA87SignatureB64Len

func MLDSA87SignatureB64Len() int

MLDSA87SignatureB64Len 标准 Base64 编码后的 ML-DSA-87 签名长度(无换行)。

func MLKEM1024CiphertextB64Len

func MLKEM1024CiphertextB64Len() int

MLKEM1024CiphertextB64Len KEM 密文 Base64 长度。

func MLKEM1024EncapsulationKeyB64Len

func MLKEM1024EncapsulationKeyB64Len() int

MLKEM1024EncapsulationKeyB64Len 封装公钥 Base64 长度。

func MaxPlan2AuthorizationB64Len

func MaxPlan2AuthorizationB64Len() int

MaxPlan2AuthorizationB64Len Authorization 头中 base64(PublicKey JSON) 的最大长度。

func MaxPublicKeyJSONLen

func MaxPublicKeyJSONLen() int

MaxPublicKeyJSONLen Plan2 PublicKey JSON 最大长度(key + tag + sig + noc 等)。

func MinFasthttpReadBufferSize

func MinFasthttpReadBufferSize() int

MinFasthttpReadBufferSize fasthttp 单连接读缓冲下限(默认 4096 无法容纳 Plan2 Authorization)。 含 Authorization 与其它常规请求头余量。

func PeerMLDSA87PublicKeyB64FromSeed

func PeerMLDSA87PublicKeyB64FromSeed(seed []byte) (string, error)

PeerMLDSA87PublicKeyB64FromSeed 由种子派生本端密钥对并返回本端公钥 Base64。

func Plan2PrivateFileName

func Plan2PrivateFileName(clientNo int64) string

Plan2PrivateFileName 私钥文件名:{clientNo}.key。

func Plan2PublicFileName

func Plan2PublicFileName(clientNo int64) string

Plan2PublicFileName 公钥文件名:{clientNo}.pem。

func PrintMLDSA87Base64

func PrintMLDSA87Base64()

PrintMLDSA87Base64 本地快速打印一对 Base64 ML-DSA-87 密钥(调试用)。

func ReadPlan2PrivateKey

func ReadPlan2PrivateKey(dir string, clientNo int64, wrapKey string) (string, error)

ReadPlan2PrivateKey 读取 {clientNo}.key(明文或 AES 加密),返回一行 Base64 私钥。

func ReadPlan2PublicKey

func ReadPlan2PublicKey(dir string, clientNo int64) (string, error)

ReadPlan2PublicKey 读取 {clientNo}.pem,返回一行 Base64 公钥。

func ValidateMLDSA87PublicKeyB64

func ValidateMLDSA87PublicKeyB64(pubB64 string) error

ValidateMLDSA87PublicKeyB64 校验 ML-DSA-87 公钥 Base64 格式。

Types

type Cipher

type Cipher interface {
	GetPrivateKey() (interface{}, string)
	GetPublicKey() (interface{}, string)
	Encrypt(msg, aad []byte) (string, error)
	Decrypt(msg string, aad []byte) ([]byte, error)
	Sign(msg []byte) ([]byte, error)
	Verify(msg, sign []byte) error
}

type MLDSA87Object

type MLDSA87Object struct {
	PrivateKeyBase64 string
	PublicKeyBase64  string
	// contains filtered or unexported fields
}

MLDSA87Object 双向身份:Sign 用本端 ML-DSA-87 私钥,Verify 用对端公钥。

func CreateMLDSA87WithBase64

func CreateMLDSA87WithBase64(prkB64, peerPubB64 string) (*MLDSA87Object, error)

CreateMLDSA87WithBase64 按「本端私钥 + 对端公钥」加载身份(Sign/Verify)。

func CreateMLDSA87WithSeed

func CreateMLDSA87WithSeed(seed []byte, peerPubB64 string) (*MLDSA87Object, error)

CreateMLDSA87WithSeed 从 32 字节种子确定性加载私钥并派生对端公钥(测试/主种子派生场景)。

func (*MLDSA87Object) CreateMLDSA87

func (self *MLDSA87Object) CreateMLDSA87() error

func (*MLDSA87Object) Decrypt

func (self *MLDSA87Object) Decrypt(msg string, aad []byte) ([]byte, error)

func (*MLDSA87Object) Encrypt

func (self *MLDSA87Object) Encrypt(msg, aad []byte) (string, error)

func (*MLDSA87Object) GetPrivateKey

func (self *MLDSA87Object) GetPrivateKey() (interface{}, string)

func (*MLDSA87Object) GetPublicKey

func (self *MLDSA87Object) GetPublicKey() (interface{}, string)

func (*MLDSA87Object) LoadMLDSA87FromBase64

func (self *MLDSA87Object) LoadMLDSA87FromBase64(b64 string) error

func (*MLDSA87Object) Sign

func (self *MLDSA87Object) Sign(msg []byte) ([]byte, error)

func (*MLDSA87Object) Verify

func (self *MLDSA87Object) Verify(msg, sign []byte) error

type MLKEM1024Object

type MLKEM1024Object struct {
	PrivateKeyBase64 string
	PublicKeyBase64  string
	// contains filtered or unexported fields
}

MLKEM1024Object Plan2 / 匿名通道:对端封装公钥 + 本端解封装私钥,Encrypt/Decrypt 走 ecc.EncryptMLKEM1024。

func (*MLKEM1024Object) CreateMLKEM1024

func (self *MLKEM1024Object) CreateMLKEM1024() error

func (*MLKEM1024Object) Decrypt

func (self *MLKEM1024Object) Decrypt(msg string, aad []byte) ([]byte, error)

func (*MLKEM1024Object) Encrypt

func (self *MLKEM1024Object) Encrypt(msg, aad []byte) (string, error)

func (*MLKEM1024Object) GetPrivateKey

func (self *MLKEM1024Object) GetPrivateKey() (interface{}, string)

func (*MLKEM1024Object) GetPublicKey

func (self *MLKEM1024Object) GetPublicKey() (interface{}, string)

func (*MLKEM1024Object) LoadMLKEM1024DecapsulationFromBase64

func (self *MLKEM1024Object) LoadMLKEM1024DecapsulationFromBase64(b64 string) error

func (*MLKEM1024Object) SetPeerEncapsulationKeyForEncrypt

func (self *MLKEM1024Object) SetPeerEncapsulationKeyForEncrypt(peerEncapKey []byte)

SetPeerEncapsulationKeyForEncrypt 设置接收方 ML-KEM 封装公钥(1568 字节);Encrypt 前必须调用。

func (*MLKEM1024Object) Sign

func (self *MLKEM1024Object) Sign(msg []byte) ([]byte, error)

func (*MLKEM1024Object) Verify

func (self *MLKEM1024Object) Verify(msg, sign []byte) error

type Plan2Binding

type Plan2Binding struct {
	Client Plan2KeyPair
	Server Plan2KeyPair
}

Plan2Binding Plan2 双向身份:client 持有 Client 私钥 + Server 公钥;server 持有 Server 私钥 + Client 公钥。

func GeneratePlan2Binding

func GeneratePlan2Binding() (*Plan2Binding, error)

GeneratePlan2Binding 生成 Plan2 双向绑定所需的两对 ML-DSA-87 密钥。

type Plan2KeyPair

type Plan2KeyPair struct {
	PrivateKeyB64 string
	PublicKeyB64  string
}

Plan2KeyPair ML-DSA-87 单端身份密钥对(Base64)。

func GeneratePlan2KeyPair

func GeneratePlan2KeyPair() (*Plan2KeyPair, error)

GeneratePlan2KeyPair 随机生成一对 ML-DSA-87 密钥。

type Plan2ProvisionResult

type Plan2ProvisionResult struct {
	Dir       string
	ClientNo  int64
	Encrypted bool
}

Plan2ProvisionResult WritePlan2KeyProvision 落盘结果。

func WritePlan2KeyProvision

func WritePlan2KeyProvision(dir, wrapKey string, key *Plan2KeyPair) (*Plan2ProvisionResult, error)

WritePlan2KeyProvision 写入 {clientNo}.pem、{clientNo}.key(各一行纯 Base64,无 JSON/PEM 头尾)。 clientNo 为 0 时自动生成 18 位编号;wrapKey 非空时私钥文件为 AES-GCM 密文(plan2-provision-v1: 前缀)。

func WritePlan2KeyProvisionWithClientNo

func WritePlan2KeyProvisionWithClientNo(dir, wrapKey string, clientNo int64, key *Plan2KeyPair) (*Plan2ProvisionResult, error)

WritePlan2KeyProvisionWithClientNo 同 WritePlan2KeyProvision,可指定 clientNo(0 表示自动生成)。

func (*Plan2ProvisionResult) PrivateFile

func (r *Plan2ProvisionResult) PrivateFile() string

PrivateFile 私钥文件名({clientNo}.key)。

func (*Plan2ProvisionResult) PrivatePath

func (r *Plan2ProvisionResult) PrivatePath() string

PrivatePath 私钥文件完整路径。

func (*Plan2ProvisionResult) PublicFile

func (r *Plan2ProvisionResult) PublicFile() string

PublicFile 公钥文件名({clientNo}.pem)。

func (*Plan2ProvisionResult) PublicPath

func (r *Plan2ProvisionResult) PublicPath() string

PublicPath 公钥文件完整路径。

type RsaObj

type RsaObj struct {
	// 16字节string字段组
	PrivateKeyBase64 string
	PublicKeyBase64  string
	// contains filtered or unexported fields
}

func (*RsaObj) CreateRsa1024

func (self *RsaObj) CreateRsa1024() error

func (*RsaObj) CreateRsa2048

func (self *RsaObj) CreateRsa2048() error

func (*RsaObj) CreateRsaFile

func (self *RsaObj) CreateRsaFile(keyfile, pemfile string) error

func (*RsaObj) CreateRsaFileBase64

func (self *RsaObj) CreateRsaFileBase64(b ...int) error

func (*RsaObj) CreateRsaPemFile

func (self *RsaObj) CreateRsaPemFile(pemfile string) error

func (*RsaObj) Decrypt

func (self *RsaObj) Decrypt(msg string, aad []byte) ([]byte, error)

func (*RsaObj) Encrypt

func (self *RsaObj) Encrypt(msg, aad []byte) (string, error)

func (*RsaObj) GetPrivateKey

func (self *RsaObj) GetPrivateKey() (interface{}, string)

func (*RsaObj) GetPublicKey

func (self *RsaObj) GetPublicKey() (interface{}, string)

func (*RsaObj) LoadRsaFile

func (self *RsaObj) LoadRsaFile(filePath string) error

func (*RsaObj) LoadRsaKeyFileBase64

func (self *RsaObj) LoadRsaKeyFileBase64(fileBase64 string) error

func (*RsaObj) LoadRsaPemFile

func (self *RsaObj) LoadRsaPemFile(filePath string) error

func (*RsaObj) LoadRsaPemFileBase64

func (self *RsaObj) LoadRsaPemFileBase64(fileBase64 string) error

func (*RsaObj) Sign

func (self *RsaObj) Sign(msg []byte) ([]byte, error)

func (*RsaObj) Verify

func (self *RsaObj) Verify(msg, sign []byte) error

Jump to

Keyboard shortcuts

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