The implementation of bip39 is questionable. From the first glance the following looks suspicious to me:
mnemonic_to_seed modifies the input mnemonic (which is a secret) and doesn't clean up the local result (which is also secret). I'd advise against modifying secrets, it feels weird. Maybe there should be some sort of validation, or Mnemonic type should encapsulate only valid mnemonics (with unnecessary spaces stripped and in NFKD form).
salt intermediate value is not cleared in mnemonic_to_seed.
Wordlist accepts some "bad" and "incorrect" words and separators. Maybe, there should be a constructor for normalizing and checking words.
data in encode should be called secret_entropy or something to indicate its purpose: it should be handled with care and zeroized after use.
encode should return Mnemonic, not just String.
CS is not zeroized in encode.
decode takes secret mnemonic as input ms of type &str and is modified (normalized to NFKD form). NFKD form should be validated/converted to in a Mnemonic constructor, decode should take a (valid) Mnemonic as input and can't modify it (leak it into stack/heap memory). ms local variable should be zeroized.
separator in decode should already be normalized/valid in Wordlist; no need to normalize it all the time.
Why separator is &str? Why not char? Is it correct to accept different spaces (tabs, space, invisible space, etc.) in one mnemonic?
In decode there's no need for sub_whole_byte_case function and multiple calls to it. Just compute the last argument once and run the function once.
mnemonic_to_seed
modifies the input mnemonic (which is a secret) and doesn't clean up the local result (which is also secret). I'd advise against modifying secrets, it feels weird. Maybe there should be some sort of validation, orMnemonic
type should encapsulate only valid mnemonics (with unnecessary spaces stripped and in NFKD form).salt
intermediate value is not cleared inmnemonic_to_seed
.Wordlist
accepts some "bad" and "incorrect" words and separators. Maybe, there should be a constructor for normalizing and checking words.data
inencode
should be calledsecret_entropy
or something to indicate its purpose: it should be handled with care and zeroized after use.encode
should returnMnemonic
, not justString
.CS
is not zeroized inencode
.decode
takes secret mnemonic as inputms
of type&str
and is modified (normalized to NFKD form). NFKD form should be validated/converted to in aMnemonic
constructor,decode
should take a (valid)Mnemonic
as input and can't modify it (leak it into stack/heap memory).ms
local variable should be zeroized.separator
indecode
should already be normalized/valid inWordlist
; no need to normalize it all the time.separator
is&str
? Why notchar
? Is it correct to accept different spaces (tabs, space, invisible space, etc.) in one mnemonic?decode
there's no need forsub_whole_byte_case
function and multiple calls to it. Just compute the last argument once and run the function once.Originally posted by @semenov-vladyslav in https://github.com/iotaledger/crypto.rs/issues/197#issuecomment-1562917117