doubleratchet

package
v0.0.0-...-58a2b35 Latest Latest
Warning

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

Go to latest
Published: Mar 29, 2024 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WithCrypto

func WithCrypto(c Crypto) option

WithCrypto将默认加密替换为指定的加密方法。 nolint: golint

func WithKeysStorage

func WithKeysStorage(ks KeysStorage) option

withkeystorage用指定的替换默认密钥存储。 nolint: golint

func WithMaxKeep

func WithMaxKeep(n int) option

withmaxkeep指定保留消息密钥的时间,以接收的消息数计 nolint: golint

func WithMaxMessageKeysPerSession

func WithMaxMessageKeysPerSession(n int) option

withmaxmessagekeyspersession指定每个会话的最大消息密钥数 nolint: golint

func WithMaxSkip

func WithMaxSkip(n int) option

withmaxskip指定单个链中跳过消息的最大数目。 nolint: golint

Types

type Crypto

type Crypto interface {
	//创建新的dh密钥对
	GenerateDH() (DHPair, error)

	//计算公共密钥
	DH(dhPair DHPair, dhPub dh.Key) dh.Key

	//使用消息密钥 mk加密内容
	Encrypt(mk dh.Key, plaintext, ad []byte) (authCiphertext []byte)

	// Decrypt returns the AEAD decryption of ciphertext with message key mk.
	//通过消息密钥mk解密内容
	Decrypt(mk dh.Key, ciphertext, ad []byte) (plaintext []byte, err error)

	KDFer
}

Crypto是对加密库的补充。

type DHPair

type DHPair interface {
	GetPrivateKey() dh.Key
	GetPublicKey() dh.Key
}

dh密钥对

type DefaultCrypto

type DefaultCrypto struct{}

DefaultCrypto is an implementation of Crypto with cryptographic primitives recommended by the Double Ratchet Algorithm specification. However, some details are different, see function comments for details. 是双棘轮算法规范推荐使用加密原语的加密实现。但是,有些细节不同,有关详细信息,请参见功能注释。

func (DefaultCrypto) DH

func (c DefaultCrypto) DH(dhPair DHPair, dhPub dh.Key) dh.Key

计算公共密钥

func (DefaultCrypto) Decrypt

func (c DefaultCrypto) Decrypt(mk dh.Key, authCiphertext, ad []byte) ([]byte, error)

Decrypt returns the AEAD decryption of ciphertext with message key mk.

func (DefaultCrypto) Encrypt

func (this DefaultCrypto) Encrypt(mk dh.Key, plaintext, ad []byte) []byte

Encrypt使用与算法规范稍有不同的方法:它使用aes-256-ctr而不是aes-256-cbc来考虑安全性、密文长度和实现复杂性。

func (DefaultCrypto) GenerateDH

func (c DefaultCrypto) GenerateDH() (DHPair, error)

创建一个新的dh密钥对

func (DefaultCrypto) KdfCK

func (c DefaultCrypto) KdfCK(ck dh.Key) (chainKey dh.Key, msgKey dh.Key)

KdfCK returns a pair (32-byte chain key, 32-byte message key) as the output of applying a KDF keyed by a 32-byte chain key ck to some constant.

func (DefaultCrypto) KdfRK

func (c DefaultCrypto) KdfRK(rk, dhOut dh.Key) (rootKey, chainKey, headerKey dh.Key)

KdfRK returns a pair (32-byte root key, 32-byte chain key) as the output of applying a KDF keyed by a 32-byte root key rk to a Diffie-Hellman output dhOut.

type InMemoryKey

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

type KDFer

type KDFer interface {
	// KdfRK 返回一对密钥(根密钥key, 链密钥key)
	KdfRK(rk, dhOut dh.Key) (rootKey, chainKey, newHeaderKey dh.Key) //

	// KdfCK returns a pair (32-byte chain key, 32-byte message key) as the output of applying
	// a KDF keyed by a 32-byte chain key ck to some constant.
	//通过链密钥ck作为常量,作为kdf的密钥,返回一对密钥(链密钥,消息密钥)
	KdfCK(ck dh.Key) (chainKey, msgKey dh.Key)
}

KDFer为链执行键派生函数。

type KeysStorage

type KeysStorage interface {
	// get按给定的键和消息编号返回消息键。
	Get(k dh.Key, msgNum uint) (mk dh.Key, ok bool, err error)

	// 将给定的mk保存在指定的键和msgnum下。
	Put(sessionID []byte, k dh.Key, msgNum uint, mk dh.Key, keySeqNum uint) error

	// DeleteMk ensures there's no message key under the specified key and msgNum.
	//deletemk确保在指定的密钥和msgnum下没有消息密钥。
	DeleteMk(k dh.Key, msgNum uint) error

	// DeleteOldMKeys deletes old message keys for a session.
	//deleteoldmkeys删除会话的旧消息键。
	DeleteOldMks(sessionID []byte, deleteUntilSeqKey uint) error

	// TruncateMks truncates the number of keys to maxKeys.
	//truncatemks将键数截断为maxkeys。
	TruncateMks(sessionID []byte, maxKeys int) error

	// Count returns number of message keys stored under the specified key.
	//count返回存储在指定密钥下的消息密钥数。
	Count(k dh.Key) (uint, error)

	// 全部返回所有键
	All() (map[dh.Key]map[uint]dh.Key, error)
}

密钥存储是抽象内存或持久密钥存储的接口。

type KeysStorageInMemory

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

keystorageinmemory是内存中的消息密钥存储。

func (*KeysStorageInMemory) All

func (s *KeysStorageInMemory) All() (map[dh.Key]map[uint]dh.Key, error)

全部返回所有键

func (*KeysStorageInMemory) Count

func (s *KeysStorageInMemory) Count(pubKey dh.Key) (uint, error)

count返回存储在指定密钥下的消息密钥数。

func (*KeysStorageInMemory) DeleteMk

func (s *KeysStorageInMemory) DeleteMk(pubKey dh.Key, msgNum uint) error

deletemk确保在指定的密钥和msgnum下没有消息密钥。

func (*KeysStorageInMemory) DeleteOldMks

func (s *KeysStorageInMemory) DeleteOldMks(sessionID []byte, deleteUntilSeqKey uint) error

deleteoldmkeys删除会话的旧消息键。

func (*KeysStorageInMemory) Get

func (this *KeysStorageInMemory) Get(pubKey dh.Key, msgNum uint) (dh.Key, bool, error)

get按给定的键和消息编号返回消息键。

func (*KeysStorageInMemory) Put

func (s *KeysStorageInMemory) Put(sessionID []byte, pubKey dh.Key, msgNum uint, mk dh.Key, seqNum uint) error

将给定的mk保存在指定的键和msgnum下。

func (*KeysStorageInMemory) TruncateMks

func (s *KeysStorageInMemory) TruncateMks(sessionID []byte, maxKeys int) error

truncatemks将键数截断为maxkeys。

type Message

type Message struct {
	Header     MessageHeader `json:"header"`
	Ciphertext []byte        `json:"ciphertext"`
}

消息是双方交换的单个消息。

type MessageEncHeader

type MessageEncHeader []byte

messageencheader是消息头的二进制编码表示。

func (MessageEncHeader) Decode

func (mh MessageEncHeader) Decode() (MessageHeader, error)

从二进制编码表示中解码消息头。

type MessageHE

type MessageHE struct {
	Header     []byte `json:"header"`
	Ciphertext []byte `json:"ciphertext"`
}

message包含密文和加密的头。

type MessageHeader

type MessageHeader struct {
	// DHR是发送方当前的棘轮公钥。
	DH dh.Key `json:"dh"`

	// n是发送链中消息的编号。
	N uint32 `json:"n"`

	// pn是上一个发送链的长度。
	PN uint32 `json:"pn"`
}

每个消息前面都有消息头。

func (MessageHeader) Encode

func (mh MessageHeader) Encode() MessageEncHeader

以二进制格式对头文件进行编码。

type Session

type Session interface {
	// RatchetEncrypt performs a symmetric-key ratchet step, then AEAD-encrypts the message with
	// the resulting message key.
	//Ratchetencrypt执行对称的密钥棘轮步骤,然后用AEAD-encrypts加密消息
	//返回消息键。
	RatchetEncrypt(plaintext, associatedData []byte) (Message, error)

	// 调用ratchetdecrypt来对消息进行AEAD解密。
	RatchetDecrypt(m Message, associatedData []byte) ([]byte, error)

	//DeleteMk 从数据库中删除消息密钥
	DeleteMk(dh.Key, uint32) error
}

参与双棘轮算法的缔约方会议。

func Load

func Load(id []byte, store SessionStorage, opts ...option) (Session, error)

从sessionstorage实现加载会话并应用选项。

func New

func New(id []byte, sharedKey dh.Key, keyPair DHPair, storage SessionStorage, opts ...option) (Session, error)

新建使用共享密钥创建会话。

func NewWithRemoteKey

func NewWithRemoteKey(id []byte, sharedKey, remoteKey dh.Key, storage SessionStorage, opts ...option) (Session, error)

newWithRemoteKey创建与另一方的共享密钥和公钥的会话。

type SessionHE

type SessionHE interface {
	// RatchetEncrypt performs a symmetric-key ratchet step, then AEAD-encrypts
	// the header-encrypted message with the resulting message key.
	//Ratchetencrypt执行对称的密钥棘轮步骤,然后AEAD加密
	//头用结果消息密钥加密的消息。
	RatchetEncrypt(plaintext, associatedData []byte) MessageHE

	// RatchetDecrypt is called to AEAD-decrypt header-encrypted messages.
	RatchetDecrypt(m MessageHE, associatedData []byte) ([]byte, error)
}

会话这是一个涉及加密头修改的双棘轮算法的会话。

func NewHE

func NewHE(sharedKey, sharedHka, sharedNhkb dh.Key, keyPair DHPair, opts ...option) (SessionHE, error)

NewHE creates session with the shared keys.

func NewHEWithRemoteKey

func NewHEWithRemoteKey(sharedKey, sharedHka, sharedNhkb, remoteKey dh.Key, opts ...option) (SessionHE, error)

NewHEWithRemoteKey creates session with the shared keys and public key of the other party.

type SessionStorage

type SessionStorage interface {
	// Save state keyed by id
	Save(id []byte, state *State) error

	// Load state by id
	Load(id []byte) (*State, error)
}

type State

type State struct {
	Crypto Crypto

	// DH 棘轮公钥 (远端的 key).
	DHr dh.Key

	// DH 棘轮公司约对(自己的棘轮对 key).
	DHs DHPair

	// 对称棘轮根链。
	RootCh kdfRootChain

	// 对称棘轮发送和接收链。
	SendCh, RecvCh kdfChain

	// 发送链中上一个消息的编号
	PN uint32

	//跳过消息密钥的字典,由棘轮公钥或头键和消息编号编制索引。
	MkSkipped KeysStorage

	//单个链中可以跳过的最大消息键数。应该设置得足够高,以允许例程丢失或延迟消息,但是设置得足够低,以至于恶意发送者不能触发过多的收件人计算。
	MaxSkip uint

	//接收头部key和下一个头部key。仅用于头部加密
	HKr, NHKr dh.Key

	//发送头部key和下一个头部key,仅用于头部加密。
	HKs, NHKs dh.Key

	//保留消息键的时间,以接收的消息数计,例如,如果maxkeep为5,我们只保留最后5个消息键,删除所有n-5。
	MaxKeep uint

	// 每个会话的最大消息密钥数,旧密钥将以FIFO方式删除
	MaxMessageKeysPerSession int

	// 当前棘轮步进的编号。
	Step uint

	// KeysCount 已经生成的key数量
	KeysCount uint
}

双棘轮状态

func DefaultState

func DefaultState(sharedKey dh.Key) State

Jump to

Keyboard shortcuts

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