Documentation ¶
Index ¶
- func WithCrypto(c Crypto) option
- func WithKeysStorage(ks KeysStorage) option
- func WithMaxKeep(n int) option
- func WithMaxSkip(n int) option
- type Crypto
- type DHPair
- type DefaultCrypto
- func (c DefaultCrypto) DH(dhPair DHPair, dhPub Key) Key
- func (c DefaultCrypto) Decrypt(mk Key, authCiphertext, ad []byte) ([]byte, error)
- func (c DefaultCrypto) Encrypt(mk Key, plaintext, ad []byte) []byte
- func (c DefaultCrypto) GenerateDH() (DHPair, error)
- func (c DefaultCrypto) KdfCK(ck Key) (chainKey Key, msgKey Key)
- func (c DefaultCrypto) KdfRK(rk, dhOut Key) (rootKey, chainKey, headerKey Key)
- type KDFer
- type Key
- type KeysStorage
- type KeysStorageInMemory
- func (s *KeysStorageInMemory) All() map[Key]map[uint]Key
- func (s *KeysStorageInMemory) Count(pubKey Key) uint
- func (s *KeysStorageInMemory) DeleteMk(pubKey Key, msgNum uint)
- func (s *KeysStorageInMemory) DeletePk(pubKey Key)
- func (s *KeysStorageInMemory) Get(pubKey Key, msgNum uint) (Key, bool)
- func (s *KeysStorageInMemory) Put(pubKey Key, msgNum uint, mk Key)
- type Message
- type MessageEncHeader
- type MessageHE
- type MessageHeader
- type Session
- type SessionHE
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func WithCrypto ¶
func WithCrypto(c Crypto) option
WithCrypto replaces the default cryptographic supplement with the specified.
func WithKeysStorage ¶
func WithKeysStorage(ks KeysStorage) option
WithKeysStorage replaces the default keys storage with the specified.
func WithMaxKeep ¶
func WithMaxKeep(n int) option
WithMaxKeep specifies the maximum number of ratchet steps before a message is deleted.
func WithMaxSkip ¶
func WithMaxSkip(n int) option
WithMaxSkip specifies the maximum number of skipped message in a single chain.
Types ¶
type Crypto ¶
type Crypto interface { // GenerateDH creates a new Diffie-Hellman key pair. GenerateDH() (DHPair, error) // DH returns the output from the Diffie-Hellman calculation between // the private key from the DH key pair dhPair and the DH public key dbPub. DH(dhPair DHPair, dhPub Key) Key // Encrypt returns an AEAD encryption of plaintext with message key mk. The associated_data // is authenticated but is not included in the ciphertext. The AEAD nonce may be set to a constant. Encrypt(mk Key, plaintext, ad []byte) (authCiphertext []byte) // Decrypt returns the AEAD decryption of ciphertext with message key mk. Decrypt(mk Key, ciphertext, ad []byte) (plaintext []byte, err error) KDFer }
Crypto is a cryptography supplement for the library.
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 Key) Key
See the Crypto interface.
func (DefaultCrypto) Decrypt ¶
func (c DefaultCrypto) Decrypt(mk Key, authCiphertext, ad []byte) ([]byte, error)
See the Crypto interface.
func (DefaultCrypto) Encrypt ¶
func (c DefaultCrypto) Encrypt(mk Key, plaintext, ad []byte) []byte
Encrypt uses a slightly different approach than in the algorithm specification: it uses AES-256-CTR instead of AES-256-CBC for security, ciphertext length and implementation complexity considerations.
func (DefaultCrypto) GenerateDH ¶
func (c DefaultCrypto) GenerateDH() (DHPair, error)
See the Crypto interface.
func (DefaultCrypto) KdfCK ¶
func (c DefaultCrypto) KdfCK(ck Key) (chainKey Key, msgKey Key)
See the Crypto interface.
func (DefaultCrypto) KdfRK ¶
func (c DefaultCrypto) KdfRK(rk, dhOut Key) (rootKey, chainKey, headerKey Key)
See the Crypto interface.
type KDFer ¶
type KDFer interface { // 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. KdfRK(rk, dhOut Key) (rootKey, chainKey, newHeaderKey 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. KdfCK(ck Key) (chainKey, msgKey Key) }
KDFer performs key derivation functions for chains.
type Key ¶
type Key [32]byte
Keys is any 32-byte key. It's created for the possibility of pretty hex output.
type KeysStorage ¶
type KeysStorage interface { // Get returns a message key by the given key and message number. Get(k Key, msgNum uint) (mk Key, ok bool) // Put saves the given mk under the specified key and msgNum. Put(k Key, msgNum uint, mk Key) // DeleteMk ensures there's no message key under the specified key and msgNum. DeleteMk(k Key, msgNum uint) // DeletePk ensures there's no message keys under the specified key. DeletePk(k Key) // Count returns number of message keys stored under the specified key. Count(k Key) uint // All returns all the keys All() map[Key]map[uint]Key }
KeysStorage is an interface of an abstract in-memory or persistent keys storage.
type KeysStorageInMemory ¶
type KeysStorageInMemory struct {
// contains filtered or unexported fields
}
KeysStorageInMemory is an in-memory message keys storage.
func (*KeysStorageInMemory) All ¶
func (s *KeysStorageInMemory) All() map[Key]map[uint]Key
See KeysStorage.
func (*KeysStorageInMemory) Count ¶
func (s *KeysStorageInMemory) Count(pubKey Key) uint
See KeysStorage.
func (*KeysStorageInMemory) DeleteMk ¶
func (s *KeysStorageInMemory) DeleteMk(pubKey Key, msgNum uint)
See KeysStorage.
func (*KeysStorageInMemory) DeletePk ¶
func (s *KeysStorageInMemory) DeletePk(pubKey Key)
See KeysStorage.
type Message ¶
type Message struct { Header MessageHeader `json:"header"` Ciphertext []byte `json:"ciphertext"` }
Message is a single message exchanged by the parties.
type MessageEncHeader ¶
type MessageEncHeader []byte
MessageEncHeader is a binary-encoded representation of a message header.
func (MessageEncHeader) Decode ¶
func (mh MessageEncHeader) Decode() (MessageHeader, error)
Decode message header out of the binary-encoded representation.
type MessageHeader ¶
type MessageHeader struct { // DHr is the sender's current ratchet public key. DH Key `json:"dh"` // N is the number of the message in the sending chain. N uint32 `json:"n"` // PN is the length of the previous sending chain. PN uint32 `json:"pn"` }
MessageHeader that is prepended to every message.
func (MessageHeader) Encode ¶
func (mh MessageHeader) Encode() MessageEncHeader
Encode the header in the binary format.
type Session ¶
type Session interface { // RatchetEncrypt performs a symmetric-key ratchet step, then AEAD-encrypts the message with // the resulting message key. RatchetEncrypt(plaintext, associatedData []byte) Message // RatchetDecrypt is called to AEAD-decrypt messages. RatchetDecrypt(m Message, associatedData []byte) ([]byte, error) }
Session of the party involved in the Double Ratchet Algorithm.
func NewWithRemoteKey ¶
NewWithRemoteKey creates session with the shared key and public key of the other party.
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(plaintext, associatedData []byte) MessageHE // RatchetDecrypt is called to AEAD-decrypt header-encrypted messages. RatchetDecrypt(m MessageHE, associatedData []byte) ([]byte, error) }
SessionHE is the session of the party involved the Double Ratchet Algorithm with encrypted header modification.
func NewHEWithRemoteKey ¶
func NewHEWithRemoteKey(sharedKey, sharedHka, sharedNhkb, remoteKey Key, opts ...option) (SessionHE, error)
NewHEWithRemoteKey creates session with the shared keys and public key of the other party.