Skip to content

hkdf using sha256 #136

@blackheart01

Description

@blackheart01

I'm trying to generate an hkdf sha256 key from a master key and a salt. According to https://wiki.openssl.org/index.php/EVP_Key_Derivation it seems openssl has this functionality. Unfortunately, looking through the source code for spacemonkeygo/openssl, I can't find any "EVP_KDF" functions.

I've written my own HKDF function that produces the expected output:

func HKDF(master, salt []byte) ([]byte, error) {
	hmac, err := openssl.NewHMAC(salt, openssl.EVP_SHA256)
	if err != nil {
		return nil, err
	}

	_, err = hmac.Write(master)
	if err != nil {
		return nil, err
	}

	prk, err := hmac.Final()
	if err != nil {
		return nil, err
	}
	hmac.Close()

	hmac, err = openssl.NewHMAC(prk, openssl.EVP_SHA256)
	if err != nil {
		return nil, err
	}

	_, err = hmac.Write([]byte{1})
	if err != nil {
		return nil, err
	}

	key, err := hmac.Final()
	if err != nil {
		return nil, err
	}
	hmac.Close()
	
	return key, nil
}

This function works, but is only a proof of concept right now. Before I commit to making the function production ready, I thought I'd ask if anyone could point me to a spacemonkeygo/openssl function that already does this.

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions