Updated usage example in README.md

This commit is contained in:
Ivan Tomilov 2017-06-19 13:42:14 +07:00
parent 88d085e289
commit 505a37f424
3 changed files with 43 additions and 35 deletions

View File

@ -47,16 +47,22 @@ func main() {
0x1e, 0x2c, 0x9, 0x8d, 0x4b, 0x4d, 0xc1, 0x40,
}
// Bob MUST be created with the shared secret and CAN be initialized
// also with Alice's public key (see below).
bob, err := doubleratchet.New(sk)
// Diffie-Hellman key pair generated by one of the parties during key exchange or
// by any other means. The public key MUST be sent to another party for initialization
// before the communication begins.
keyPair, err := doubleratchet.DefaultCrypto{}.GenerateDH()
if err != nil {
log.Fatal(err)
}
// Alice MUST be created with the shared secret and Bob's public key which
// should be sent to Alice before the session begins.
alice, err := doubleratchet.NewWithRK(sk, bob.PublicKey())
// Bob MUST be created with the shared secret and a DH key pair.
bob, err := doubleratchet.New(sk, keyPair)
if err != nil {
log.Fatal(err)
}
// Alice MUST be created with the shared secret and Bob's public key.
alice, err := doubleratchet.NewWithRemoteKey(sk, keyPair.PublicKey())
if err != nil {
log.Fatal(err)
}
@ -82,7 +88,6 @@ func main() {
1. Skipped messages from a single ratchet step are deleted after 100 ratchet steps.
1. Both parties' sending and receiving chains are initialized with the shared key so that both
of them could message each other from the very beginning.
1. TODO: Header encryption
### Header encryption

View File

@ -17,26 +17,25 @@ type session struct {
}
// New creates session with the shared key.
func New(sharedKey Key, opts ...option) (Session, error) {
func New(sharedKey Key, keyPair DHPair, opts ...option) (Session, error) {
state, err := newState(sharedKey, opts...)
if err != nil {
return nil, err
}
s := &session{state}
return s, nil
state.DHs = keyPair
return &session{state}, nil
}
// NewWithRK creates session with the shared key and public key of the other party.
func NewWithRK(sharedKey, remoteKey Key, opts ...option) (Session, error) {
sI, err := New(sharedKey, opts...)
// NewWithRemoteKey creates session with the shared key and public key of the other party.
func NewWithRemoteKey(sharedKey, remoteKey Key, opts ...option) (Session, error) {
state, err := newState(sharedKey, opts...)
if err != nil {
return nil, err
}
s := sI.(*session)
s.DHr = remoteKey
state.DHr = remoteKey
// FIXME: Where the header key goes?
s.SendCh, _ = s.RootCh.step(s.Crypto.DH(s.DHs, s.DHr))
return s, nil
state.SendCh, _ = state.RootCh.step(state.Crypto.DH(state.DHs, state.DHr))
return &session{state}, nil
}
// RatchetEncrypt performs a symmetric-key ratchet step, then encrypts the message with

View File

@ -9,15 +9,19 @@ import (
var (
sk = Key{0xeb, 0x8, 0x10, 0x7c, 0x33, 0x54, 0x0, 0x20, 0xe9, 0x4f, 0x6c, 0x84, 0xe4, 0x39, 0x50, 0x5a, 0x2f, 0x60, 0xbe, 0x81, 0xa, 0x78, 0x8b, 0xeb, 0x1e, 0x2c, 0x9, 0x8d, 0x4b, 0x4d, 0xc1, 0x40}
bobPair = dhPair{
//privateKey: Key{0xf0, 0x22, 0x54, 0xf4, 0xcb, 0xa2, 0x60, 0xc8, 0xeb, 0xe, 0x83, 0xb, 0xc8, 0xb2, 0xfb, 0x18, 0x6f, 0x1b, 0xa4, 0xa2, 0x6e, 0x45, 0xc, 0xeb, 0xff, 0x74, 0xce, 0x65, 0x8b, 0x6e, 0x4c, 0x5d},
publicKey: Key{0xe3, 0xbe, 0xb9, 0x4e, 0x70, 0x17, 0x37, 0xc, 0x1, 0x8f, 0xa9, 0x7e, 0xef, 0x4, 0xfb, 0x23, 0xac, 0xea, 0x28, 0xf7, 0xa9, 0x56, 0xcc, 0x1d, 0x46, 0xf3, 0xb5, 0x1d, 0x7d, 0x7d, 0x5e, 0x2c},
privateKey: Key{0xf0, 0x22, 0x54, 0xf4, 0xcb, 0xa2, 0x60, 0xc8, 0xeb, 0xe, 0x83, 0xb, 0xc8, 0xb2, 0xfb, 0x18, 0x6f, 0x1b, 0xa4, 0xa2, 0x6e, 0x45, 0xc, 0xeb, 0xff, 0x74, 0xce, 0x65, 0x8b, 0x6e, 0x4c, 0x5d},
publicKey: Key{0xe3, 0xbe, 0xb9, 0x4e, 0x70, 0x17, 0x37, 0xc, 0x1, 0x8f, 0xa9, 0x7e, 0xef, 0x4, 0xfb, 0x23, 0xac, 0xea, 0x28, 0xf7, 0xa9, 0x56, 0xcc, 0x1d, 0x46, 0xf3, 0xb5, 0x1d, 0x7d, 0x7d, 0x5e, 0x2c},
}
alicePair = dhPair{
privateKey: Key{0x78, 0xa1, 0x5e, 0xc7, 0xbe, 0x74, 0x9f, 0x1, 0x4b, 0xdc, 0x21, 0xeb, 0x60, 0xd4, 0xff, 0xac, 0x1e, 0x31, 0x8b, 0x16, 0xf8, 0x12, 0xd4, 0x40, 0xd, 0x82, 0x7a, 0xf0, 0xe, 0xba, 0xc2, 0x7a},
publicKey: Key{0x3b, 0x93, 0x57, 0x64, 0xd1, 0x47, 0xf1, 0xf, 0xc7, 0x13, 0x1, 0xc6, 0xf9, 0xed, 0x49, 0xa4, 0xad, 0x59, 0x92, 0x87, 0xb1, 0x0, 0xf1, 0x4a, 0x8e, 0x43, 0x4d, 0xa7, 0x2e, 0x3d, 0xf8, 0x72},
}
)
func TestNew_Basic(t *testing.T) {
// Act.
var (
si, err = New(sk)
si, err = New(sk, bobPair)
s = si.(*session)
)
@ -42,7 +46,7 @@ func TestNew_Basic(t *testing.T) {
func TestNew_BadSharedKey(t *testing.T) {
// Act.
_, err := New([32]byte{})
_, err := New([32]byte{}, bobPair)
// Assert.
require.NotNil(t, err)
@ -51,7 +55,7 @@ func TestNew_BadSharedKey(t *testing.T) {
func TestNewWithRK(t *testing.T) {
// Act.
var (
si, err = NewWithRK(sk, bobPair.PublicKey())
si, err = NewWithRemoteKey(sk, bobPair.PublicKey())
s = si.(*session)
)
@ -66,7 +70,7 @@ func TestNewWithRK(t *testing.T) {
func TestState_RatchetEncryptDecrypt_Basic(t *testing.T) {
// Arrange.
var (
si, err = NewWithRK(sk, bobPair.PublicKey())
si, err = NewWithRemoteKey(sk, bobPair.PublicKey())
s = si.(*session)
oldCKs = s.SendCh.CK
)
@ -89,8 +93,8 @@ func TestState_RatchetEncryptDecrypt_Basic(t *testing.T) {
func TestState_RatchetDecrypt_CommunicationFailedWithNoPublicKey(t *testing.T) {
// Arrange.
var (
bob, _ = New(sk)
alice, _ = New(sk)
bob, _ = New(sk, bobPair)
alice, _ = New(sk, alicePair)
)
// Act.
@ -106,10 +110,10 @@ func TestState_RatchetDecrypt_CommunicationFailedWithNoPublicKey(t *testing.T) {
func TestState_RatchetDecrypt_CommunicationAliceSends(t *testing.T) {
// Arrange.
var (
bobI, _ = New(sk)
bobI, _ = New(sk, bobPair)
bob = bobI.(*session)
alice, _ = NewWithRK(sk, bob.DHs.PublicKey())
alice, _ = NewWithRemoteKey(sk, bob.DHs.PublicKey())
)
for i := 0; i < 10; i++ {
@ -123,10 +127,10 @@ func TestState_RatchetDecrypt_CommunicationAliceSends(t *testing.T) {
func TestState_RatchetDecrypt_CommunicationBobSends(t *testing.T) {
var (
bobI, _ = New(sk)
bobI, _ = New(sk, bobPair)
bob = bobI.(*session)
alice, _ = NewWithRK(sk, bob.DHs.PublicKey())
alice, _ = NewWithRemoteKey(sk, bob.DHs.PublicKey())
)
for i := 0; i < 10; i++ {
@ -141,10 +145,10 @@ func TestState_RatchetDecrypt_CommunicationBobSends(t *testing.T) {
func TestState_RatchetDecrypt_CommunicationPingPong(t *testing.T) {
// Arrange.
var (
bobI, _ = New(sk)
bobI, _ = New(sk, bobPair)
bob = bobI.(*session)
alice, _ = NewWithRK(sk, bob.DHs.PublicKey())
alice, _ = NewWithRemoteKey(sk, bob.DHs.PublicKey())
)
for i := 0; i < 10; i++ {
@ -161,10 +165,10 @@ func TestState_RatchetDecrypt_CommunicationPingPong(t *testing.T) {
func TestState_RatchetDecrypt_CommunicationSkippedMessages(t *testing.T) {
// Arrange.
var (
bobI, _ = New(sk, WithMaxSkip(1))
bobI, _ = New(sk, bobPair, WithMaxSkip(1))
bob = bobI.(*session)
aliceI, _ = NewWithRK(sk, bob.DHs.PublicKey(), WithMaxSkip(1))
aliceI, _ = NewWithRemoteKey(sk, bob.DHs.PublicKey(), WithMaxSkip(1))
alice = aliceI.(*session)
)
@ -214,10 +218,10 @@ func TestState_RatchetDecrypt_CommunicationSkippedMessages(t *testing.T) {
func TestState_SkippedKeysDeletion(t *testing.T) {
// Arrange.
var (
bobI, _ = New(sk, WithMaxKeep(2))
bobI, _ = New(sk, bobPair, WithMaxKeep(2))
bob = bobI.(*session)
alice, _ = NewWithRK(sk, bob.DHs.PublicKey(), WithMaxKeep(2))
alice, _ = NewWithRemoteKey(sk, bob.DHs.PublicKey(), WithMaxKeep(2))
h = SessionTestHelper{t, alice, bob}
)