Skip to content

Commit

Permalink
Update KDF to use SHA3-256
Browse files Browse the repository at this point in the history
  • Loading branch information
Aron Wussler committed May 27, 2024
1 parent 19bbf59 commit 020529d
Show file tree
Hide file tree
Showing 5 changed files with 175 additions and 428 deletions.
144 changes: 0 additions & 144 deletions internal/kmac/kmac.go

This file was deleted.

130 changes: 0 additions & 130 deletions internal/kmac/kmac_test.go

This file was deleted.

26 changes: 16 additions & 10 deletions openpgp/mlkem_ecdh/mlkem_ecdh.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@ import (
"golang.org/x/crypto/sha3"

Check failure on line 9 in openpgp/mlkem_ecdh/mlkem_ecdh.go

View workflow job for this annotation

GitHub Actions / Build gosop from branch v1-api

missing go.sum entry for module providing package golang.org/x/crypto/sha3 (imported by github.com/ProtonMail/go-crypto/openpgp/mlkem_ecdh); to add:
"io"

"github.com/ProtonMail/go-crypto/internal/kmac"
"github.com/ProtonMail/go-crypto/openpgp/aes/keywrap"
"github.com/ProtonMail/go-crypto/openpgp/errors"
"github.com/ProtonMail/go-crypto/openpgp/internal/algorithm"
"github.com/ProtonMail/go-crypto/openpgp/internal/ecc"
"github.com/cloudflare/circl/kem"

Check failure on line 15 in openpgp/mlkem_ecdh/mlkem_ecdh.go

View workflow job for this annotation

GitHub Actions / Build gosop from branch v1-api

missing go.sum entry for module providing package github.com/cloudflare/circl/kem (imported by github.com/ProtonMail/go-crypto/openpgp/packet); to add:
)
Expand Down Expand Up @@ -84,7 +82,7 @@ func Encrypt(rand io.Reader, pub *PublicKey, msg []byte) (kEphemeral, ecEphemera
return nil, nil, nil, err
}

kek, err := buildKey(pub, ecSS, ecEphemeral, pub.PublicPoint, kSS, kEphemeral)
kek, err := buildKey(pub, ecSS, ecEphemeral, pub.PublicPoint, kSS, kEphemeral, pub.PublicMlkem)
if err != nil {
return nil, nil, nil, err
}
Expand All @@ -111,7 +109,7 @@ func Decrypt(priv *PrivateKey, kEphemeral, ecEphemeral, ciphertext []byte) (msg
return nil, err
}

kek, err := buildKey(&priv.PublicKey, ecSS, ecEphemeral, priv.PublicPoint, kSS, kEphemeral)
kek, err := buildKey(&priv.PublicKey, ecSS, ecEphemeral, priv.PublicPoint, kSS, kEphemeral, priv.PublicMlkem)
if err != nil {
return nil, err
}
Expand All @@ -125,7 +123,7 @@ func Decrypt(priv *PrivateKey, kEphemeral, ecEphemeral, ciphertext []byte) (msg

// buildKey implements the composite KDF as specified in
// https://www.ietf.org/archive/id/draft-wussler-openpgp-pqc-03.html#name-key-combiner
func buildKey(pub *PublicKey, eccSecretPoint, eccEphemeral, eccPublicKey, kyberKeyShare, kyberEphemeral []byte) ([]byte, error) {
func buildKey(pub *PublicKey, eccSecretPoint, eccEphemeral, eccPublicKey, mlkemKeyShare, mlkemEphemeral []byte, mlkemPublicKey kem.PublicKey) ([]byte, error) {
h := sha3.New256()

// SHA3 never returns error
Expand All @@ -134,20 +132,28 @@ func buildKey(pub *PublicKey, eccSecretPoint, eccEphemeral, eccPublicKey, kyberK
_, _ = h.Write(eccPublicKey)
eccKeyShare := h.Sum(nil)

serializedMlkemKey, err := mlkemPublicKey.MarshalBinary()
if err != nil {
return nil, err
}

// eccData = eccKeyShare || eccCipherText
// mlkemData = mlkemKeyShare || mlkemCipherText
// encData = counter || eccData || mlkemData || fixedInfo
k := kmac.NewKMAC256([]byte("OpenPGPCompositeKeyDerivationFunction"), algorithm.AES256.KeySize(), []byte("KDF"))
k := sha3.New256()

// KMAC never returns error
// SHA3 never returns error
_, _ = k.Write([]byte{0x00, 0x00, 0x00, 0x01})
_, _ = k.Write(eccKeyShare)
_, _ = k.Write(eccEphemeral)
_, _ = k.Write(kyberKeyShare)
_, _ = k.Write(kyberEphemeral)
_, _ = k.Write(eccPublicKey)
_, _ = k.Write(mlkemKeyShare)
_, _ = k.Write(mlkemEphemeral)
_, _ = k.Write(serializedMlkemKey)
_, _ = k.Write([]byte{pub.AlgId})
_, _ = k.Write([]byte("OpenPGPCompositeKDFv1"))

fmt.Printf("ecc:%x\nkyber:%x\n", eccKeyShare, kyberKeyShare)
fmt.Printf("ecc:%x\nkyber:%x\n", eccKeyShare, mlkemKeyShare)

return k.Sum(nil), nil
}
Expand Down
Loading

0 comments on commit 020529d

Please sign in to comment.