From 683613fe6c497041841a0a42b097f98ede340117 Mon Sep 17 00:00:00 2001 From: Ivan Tomilov Date: Sun, 11 Jun 2017 11:16:17 +0700 Subject: [PATCH] Moved CryptoRecommended out of crypto.go --- crypto.go | 34 ++-------------------------------- crypto_recommended.go | 31 +++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 32 deletions(-) create mode 100644 crypto_recommended.go diff --git a/crypto.go b/crypto.go index ba1c0e0..6a72b91 100644 --- a/crypto.go +++ b/crypto.go @@ -1,13 +1,7 @@ package doubleratchet -import ( - "crypto/rand" - "fmt" - "golang.org/x/crypto/curve25519" -) - -// TODO: Replace []byte with meaningful types. -// TODO: Constant for +// TODO: Replace []byte with meaningful types? +// TODO: Constant for nonces? // Crypto is a cryptography supplement for the library. type Crypto interface { @@ -50,27 +44,3 @@ type DHKeyPair struct { PrivateKey []byte PublicKey []byte } - -// TODO: Implement. -// CryptoRecommended is an implementation of Crypto with cryptographic primitives recommended -// by the Double Ratchet Algorithm specification. -type CryptoRecommended struct { - Crypto -} - -func (c CryptoRecommended) GenerateDH() (DHKeyPair, error) { - var privkey [32]byte - if _, err := rand.Read(privkey[:]); err != nil { - return DHKeyPair{}, fmt.Errorf("couldn't generate privkey: %s", err) - } - privkey[0] &= 248 - privkey[31] &= 127 - privkey[31] |= 64 - - var pubkey [32]byte - curve25519.ScalarBaseMult(&pubkey, &privkey) - return DHKeyPair{ - PrivateKey: privkey[:], - PublicKey: pubkey[:], - }, nil -} diff --git a/crypto_recommended.go b/crypto_recommended.go new file mode 100644 index 0000000..f226c14 --- /dev/null +++ b/crypto_recommended.go @@ -0,0 +1,31 @@ +package doubleratchet + +import ( + "crypto/rand" + "fmt" + + "golang.org/x/crypto/curve25519" +) + +// CryptoRecommended is an implementation of Crypto with cryptographic primitives recommended +// by the Double Ratchet Algorithm specification. +type CryptoRecommended struct { + Crypto +} + +func (c CryptoRecommended) GenerateDH() (DHKeyPair, error) { + var privkey [32]byte + if _, err := rand.Read(privkey[:]); err != nil { + return DHKeyPair{}, fmt.Errorf("couldn't generate privkey: %s", err) + } + privkey[0] &= 248 + privkey[31] &= 127 + privkey[31] |= 64 + + var pubkey [32]byte + curve25519.ScalarBaseMult(&pubkey, &privkey) + return DHKeyPair{ + PrivateKey: privkey[:], + PublicKey: pubkey[:], + }, nil +}