Repository: bitmaelum/bitmaelum-suite
Pull request #198 (2021-03-26 10:12:39)

making sure the crypto works again


Patch:
diff --git a/pkg/bmcrypto/key_ecdsa.go b/pkg/bmcrypto/key_ecdsa.go
index 83997fa..2634f01 100644
--- a/pkg/bmcrypto/key_ecdsa.go
+++ b/pkg/bmcrypto/key_ecdsa.go
@@ -181,15 +181,23 @@ func (k *KeyEcdsa) KeyExchange(privK PrivKey, pubK PubKey) ([]byte, error) {
 		privK.K.(*ecdsa.PrivateKey).D.Bytes(),
 	)
 
+	// Get the length of the key
+	keyLen := k.Curve.Params().BitSize / 8
+
 	b := ke.Bytes()
-	if len(b) == 32 {
-		// Length is 32 bytes, so we can return as-is
+	if len(b) == keyLen {
+		// Length is 48 bytes, so we can return as-is
 		return b, nil
 	}
 
+	// Sanity check
+	if keyLen > len(b) {
+		panic("key length is larger. This would mean we might have ended up with a truncated key.")
+	}
+
 	// Make sure we zero-extend the result (big.Int) to 32 bytes (big endian)
-	var ret [32]byte
-	copy(ret[32-len(b):], b)
+	var ret = make([]byte, keyLen)
+	copy(ret[keyLen-len(b):], b)
 	return ret[:], nil
 }
 
diff --git a/pkg/bmcrypto/key_ed25519.go b/pkg/bmcrypto/key_ed25519.go
index a8da44a..89664b5 100644
--- a/pkg/bmcrypto/key_ed25519.go
+++ b/pkg/bmcrypto/key_ed25519.go
@@ -81,15 +81,23 @@ func (k *KeyEd25519) ParsePrivateKeyInterface(key interface{}) ([]byte, error) {
 
 // GenerateKeyPair will generate a new keypair for this keytype. io.Reader can be deterministic if needed
 func (k *KeyEd25519) GenerateKeyPair(r io.Reader) (*PrivKey, *PubKey, error) {
-	// Read 256 bits
-	randBuf := make([]byte, 32)
-	_, err := io.ReadFull(r, randBuf)
-	if err != nil {
+	b := make([]byte, 32)
+
+	// Reader could hold either 24 bytes (192bit seed) or new style 32 bytes (256 bit seed)
+	n, err := io.ReadFull(r, b)
+
+	// Something other than EOF happened
+	if err != nil && err != io.ErrUnexpectedEOF {
+		return nil, nil, err
+	}
+
+	// Not the correct number of bytes read
+	if n != 24 && n != 32 {
 		return nil, nil, err
 	}
 
-	// Stretch 192 bits to 256 bits
-	rd := hkdf.New(sha256.New, randBuf[:], []byte{}, []byte{})
+	// Stretch to 256 bits if smaller.. or just hkdf anyways
+	rd := hkdf.New(sha256.New, b[:n], []byte{}, []byte{})
 	expBuf := make([]byte, 32)
 	_, err = io.ReadFull(rd, expBuf)
 	if err != nil {
diff --git a/pkg/bmcrypto/mnemonic.go b/pkg/bmcrypto/mnemonic.go
index 56f047c..7ba5742 100644
--- a/pkg/bmcrypto/mnemonic.go
+++ b/pkg/bmcrypto/mnemonic.go
@@ -54,7 +54,7 @@ func MnemonicToRandomSeed(mnemonic string) ([]byte, error) {
 // GenerateKeypairWithRandomSeed generates a seed and generates a keypair which can be reconstructed again with the same seed
 func GenerateKeypairWithRandomSeed(kt KeyType) (*KeyPair, error) {
 	// Generate large enough random string
-	seed, err := bip39.NewEntropy(192)
+	seed, err := bip39.NewEntropy(256)
 	if err != nil {
 		return nil, err
 	}


Test patch:
diff --git a/internal/config/config_test.go b/internal/config/config_test.go
index 9c6965f..47a0ded 100644
--- a/internal/config/config_test.go
+++ b/internal/config/config_test.go
@@ -160,12 +160,21 @@ func TestLoadClientConfig(t *testing.T) {
 }
 
 func TestGenerateRoutingFromMnemonic(t *testing.T) {
+	// 192 bits key
 	r, err := GenerateRoutingFromMnemonic("ed25519 cluster puppy wash ceiling skate search great angry drift rose undo fragile boring fence stumble shuffle cable praise")
 	assert.NoError(t, err)
 
 	assert.Equal(t, "f5f1dc4eff7237ac0e061a9e8982b7b913fc479138189cc8d6ba5131dee1bde9", r.RoutingID)
 	assert.Equal(t, "ed25519 MC4CAQAwBQYDK2VwBCIEIDLOvf5iUAPWeNIYlbyDffgv+VA2xnS1s1mUYIOmW8XK", r.KeyPair.PrivKey.String())
 	assert.Equal(t, "ed25519 MCowBQYDK2VwAyEAndS2/G3uasbaYO0+89rNzvNJ3gfOi/An1t5xvETeNoc=", r.KeyPair.PubKey.String())
+
+	// 256 bits mnemonic
+	r, err = GenerateRoutingFromMnemonic("ed25519 stadium divert follow urban butter blanket garlic catalog pride angle unlock zoo recipe property tray analyst define blind purpose direct phrase mesh sugar media")
+	assert.NoError(t, err)
+
+	assert.Equal(t, "1dc47da2e7435857498138e8a94300f240806df9f9a9c3c1d6c06c9c5c9ef6d9", r.RoutingID)
+	assert.Equal(t, "ed25519 MC4CAQAwBQYDK2VwBCIEID6iGsb47ZSj9lPUV/9QPDQX7vfXXgiD8veJt5l3w3s1", r.KeyPair.PrivKey.String())
+	assert.Equal(t, "ed25519 MCowBQYDK2VwAyEAnENAkd7gFx3NrkxYRf5aBVtDM4EayslahDsp4J/2yII=", r.KeyPair.PubKey.String())
 }
 
 func init() {