diff --git a/x/logic/interpreter/registry.go b/x/logic/interpreter/registry.go index c99faad5..169652b9 100644 --- a/x/logic/interpreter/registry.go +++ b/x/logic/interpreter/registry.go @@ -109,7 +109,7 @@ var registry = map[string]any{ "bank_spendable_balances/2": predicate.BankSpendableBalances, "bank_locked_balances/2": predicate.BankLockedBalances, "did_components/2": predicate.DIDComponents, - "crypto_data_hash": predicate.CryptoDataHash, + "crypto_data_hash/3": predicate.CryptoDataHash, "hex_bytes/2": predicate.HexBytes, "bech32_address/2": predicate.Bech32Address, "source_file/1": predicate.SourceFile, diff --git a/x/logic/predicate/crypto.go b/x/logic/predicate/crypto.go index 15c8a591..78e959ad 100644 --- a/x/logic/predicate/crypto.go +++ b/x/logic/predicate/crypto.go @@ -73,6 +73,7 @@ func SHAHash(vm *engine.VM, data, hash engine.Term, cont engine.Cont, env *engin // // - sha256 (default): The SHA-256 algorithm. // - sha512: The SHA-512 algorithm. +// - md5: (insecure) The MD5 algorithm. // // Note: Due to the principles of the hash algorithm (pre-image resistance), this predicate can only compute the hash // value from input data, and cannot compute the original input data from the hash value. diff --git a/x/logic/util/crypto.go b/x/logic/util/crypto.go index 3ad96fdb..4ecffdb6 100644 --- a/x/logic/util/crypto.go +++ b/x/logic/util/crypto.go @@ -5,9 +5,11 @@ import ( "crypto/ecdsa" "crypto/ed25519" "crypto/elliptic" + "crypto/md5" "crypto/sha256" "crypto/sha512" "fmt" + "hash" "github.com/dustinxie/ecc" ) @@ -27,9 +29,23 @@ func (a KeyAlg) String() string { } // HashAlg is the type of hash algorithm supported by the crypto util functions. -// ENUM(sha256,sha512) +// ENUM(md5,sha256,sha512) type HashAlg int +// Hasher returns a new hash.Hash for the given algorithm. +func (a HashAlg) Hasher() (hash.Hash, error) { + switch a { + case HashAlgMd5: + return md5.New(), nil + case HashAlgSha256: + return sha256.New(), nil + case HashAlgSha512: + return sha512.New(), nil + default: + return nil, fmt.Errorf("algo %s not supported", a.String()) + } +} + // VerifySignature verifies the signature of the given message with the given public key using the given algorithm. func VerifySignature(alg KeyAlg, pubKey []byte, msg, sig []byte) (_ bool, err error) { defer func() { @@ -52,18 +68,13 @@ func VerifySignature(alg KeyAlg, pubKey []byte, msg, sig []byte) (_ bool, err er // Hash hashes the given data using the given algorithm. func Hash(alg HashAlg, bytes []byte) ([]byte, error) { - switch alg { - case HashAlgSha256: - hasher := sha256.New() - hasher.Write(bytes) - return hasher.Sum(nil), nil - case HashAlgSha512: - hasher := sha512.New() - hasher.Write(bytes) - return hasher.Sum(nil), nil - default: - return nil, fmt.Errorf("algo %s not supported", alg) + hasher, err := alg.Hasher() + if err != nil { + return nil, err } + + hasher.Write(bytes) + return hasher.Sum(nil), nil } // verifySignatureWithCurve verifies the ASN1 signature of the given message with the given diff --git a/x/logic/util/crypto_enum.go b/x/logic/util/crypto_enum.go index 0b020fef..014eab68 100644 --- a/x/logic/util/crypto_enum.go +++ b/x/logic/util/crypto_enum.go @@ -12,19 +12,22 @@ import ( ) const ( + // HashAlgMd5 is a HashAlg of type Md5. + HashAlgMd5 HashAlg = iota // HashAlgSha256 is a HashAlg of type Sha256. - HashAlgSha256 HashAlg = iota + HashAlgSha256 // HashAlgSha512 is a HashAlg of type Sha512. HashAlgSha512 ) var ErrInvalidHashAlg = fmt.Errorf("not a valid HashAlg, try [%s]", strings.Join(_HashAlgNames, ", ")) -const _HashAlgName = "sha256sha512" +const _HashAlgName = "md5sha256sha512" var _HashAlgNames = []string{ - _HashAlgName[0:6], - _HashAlgName[6:12], + _HashAlgName[0:3], + _HashAlgName[3:9], + _HashAlgName[9:15], } // HashAlgNames returns a list of possible string values of HashAlg. @@ -35,8 +38,9 @@ func HashAlgNames() []string { } var _HashAlgMap = map[HashAlg]string{ - HashAlgSha256: _HashAlgName[0:6], - HashAlgSha512: _HashAlgName[6:12], + HashAlgMd5: _HashAlgName[0:3], + HashAlgSha256: _HashAlgName[3:9], + HashAlgSha512: _HashAlgName[9:15], } // String implements the Stringer interface. @@ -55,8 +59,9 @@ func (x HashAlg) IsValid() bool { } var _HashAlgValue = map[string]HashAlg{ - _HashAlgName[0:6]: HashAlgSha256, - _HashAlgName[6:12]: HashAlgSha512, + _HashAlgName[0:3]: HashAlgMd5, + _HashAlgName[3:9]: HashAlgSha256, + _HashAlgName[9:15]: HashAlgSha512, } // ParseHashAlg attempts to convert a string to a HashAlg.