Moved CryptoRecommended out of crypto.go

This commit is contained in:
Ivan Tomilov 2017-06-11 11:16:17 +07:00
parent a66cc1cf11
commit 683613fe6c
2 changed files with 33 additions and 32 deletions

View File

@ -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
}

31
crypto_recommended.go Normal file
View File

@ -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
}