From 771be514d15097ba032f091298416e6cb419182b Mon Sep 17 00:00:00 2001 From: joshemb <josh@elixxir.io> Date: Mon, 22 Aug 2022 11:18:14 -0700 Subject: [PATCH] Use key residue in Cypher --- e2e/ratchet/partner/session/cypher.go | 31 +++++++++++++--------- e2e/ratchet/partner/session/cypher_test.go | 4 +-- go.mod | 2 +- go.sum | 15 +++++++++++ 4 files changed, 37 insertions(+), 15 deletions(-) diff --git a/e2e/ratchet/partner/session/cypher.go b/e2e/ratchet/partner/session/cypher.go index ef4094a40..395d15fe9 100644 --- a/e2e/ratchet/partner/session/cypher.go +++ b/e2e/ratchet/partner/session/cypher.go @@ -63,13 +63,15 @@ type Cypher interface { // Encrypt uses the E2E key to encrypt the message to its intended // recipient. It also properly populates the associated data, including the - // MAC, fingerprint, and encrypted timestamp. - Encrypt(contents []byte) (ecrContents, mac []byte) + // MAC, fingerprint, and encrypted timestamp. It generates a residue of the + // key used to encrypt the contents. + Encrypt(contents []byte) (ecrContents, mac []byte, residue e2eCrypto.KeyResidue) // Decrypt uses the E2E key to decrypt the message. It returns an error in // case of HMAC verification failure or in case of a decryption error - // (related to padding). - Decrypt(msg format.Message) ([]byte, error) + // (related to padding). It generates a residue of the + // // key used to encrypt the contents. + Decrypt(msg format.Message) (decryptedPayload []byte, residue e2eCrypto.KeyResidue, err error) // Use sets the key as used. It cannot be used again. Use() @@ -110,11 +112,13 @@ func (k *cypher) Fingerprint() format.Fingerprint { // Encrypt uses the E2E key to encrypt the message to its intended recipient. It // also properly populates the associated data, including the MAC, fingerprint, -// and encrypted timestamp. -func (k *cypher) Encrypt(contents []byte) (ecrContents, mac []byte) { +// and encrypted timestamp. It generates a residue of the key used to encrypt the contents. +func (k *cypher) Encrypt(contents []byte) (ecrContents, mac []byte, residue e2eCrypto.KeyResidue) { fp := k.Fingerprint() key := k.generateKey() + residue = e2eCrypto.MakeKeyResidue(key) + // encrypt the payload ecrContents = e2eCrypto.Crypt(key, fp, contents) @@ -122,25 +126,28 @@ func (k *cypher) Encrypt(contents []byte) (ecrContents, mac []byte) { // Currently, the MAC doesn't include any of the associated data mac = hash.CreateHMAC(ecrContents, key[:]) - return ecrContents, mac + return ecrContents, mac, residue } // Decrypt uses the E2E key to decrypt the message. It returns an error in case // of HMAC verification failure or in case of a decryption error (related to -// padding). -func (k *cypher) Decrypt(msg format.Message) ([]byte, error) { +// padding). It generates a residue of the key used to encrypt the contents +func (k *cypher) Decrypt(msg format.Message) (decryptedPayload []byte, residue e2eCrypto.KeyResidue, err error) { fp := k.Fingerprint() key := k.generateKey() // Verify the MAC is correct if !hash.VerifyHMAC(msg.GetContents(), msg.GetMac(), key[:]) { - return nil, errors.New("HMAC verification failed for E2E message") + return nil, e2eCrypto.KeyResidue{}, errors.New("HMAC verification failed for E2E message") } // Decrypt the payload - decryptedPayload := e2eCrypto.Crypt(key, fp, msg.GetContents()) + decryptedPayload = e2eCrypto.Crypt(key, fp, msg.GetContents()) + + // Construct residue + residue = e2eCrypto.MakeKeyResidue(key) - return decryptedPayload, nil + return decryptedPayload, residue, nil } // Use sets the key as used. It cannot be used again. diff --git a/e2e/ratchet/partner/session/cypher_test.go b/e2e/ratchet/partner/session/cypher_test.go index b6c92de98..c46e380a8 100644 --- a/e2e/ratchet/partner/session/cypher_test.go +++ b/e2e/ratchet/partner/session/cypher_test.go @@ -153,7 +153,7 @@ func Test_cypher_EncryptDecrypt(t *testing.T) { msg.SetContents(contents) // Encrypt - contentsEnc, mac := cy.Encrypt(msg.GetContents()) + contentsEnc, mac, _ := cy.Encrypt(msg.GetContents()) // Make the encrypted message ecrMsg := format.NewMessage(grp.GetP().ByteLen()) @@ -162,7 +162,7 @@ func Test_cypher_EncryptDecrypt(t *testing.T) { ecrMsg.SetMac(mac) // Decrypt - contentsDecr, err := cy.Decrypt(ecrMsg) + contentsDecr, _, err := cy.Decrypt(ecrMsg) if err != nil { t.Fatalf("Decrypt error: %+v", err) } diff --git a/go.mod b/go.mod index f6badba6d..edc4b5549 100644 --- a/go.mod +++ b/go.mod @@ -13,7 +13,7 @@ require ( github.com/spf13/viper v1.12.0 gitlab.com/elixxir/bloomfilter v0.0.0-20211222005329-7d931ceead6f gitlab.com/elixxir/comms v0.0.4-0.20220603231314-e47e4af13326 - gitlab.com/elixxir/crypto v0.0.7-0.20220606201132-c370d5039cea + gitlab.com/elixxir/crypto v0.0.7-0.20220822180822-5e1b688a9500 gitlab.com/elixxir/ekv v0.1.7 gitlab.com/elixxir/primitives v0.0.3-0.20220606195757-40f7a589347f gitlab.com/xx_network/comms v0.0.4-0.20220630163702-f3d372ef6acd diff --git a/go.sum b/go.sum index 05b8e2f0e..79f9648c4 100644 --- a/go.sum +++ b/go.sum @@ -271,6 +271,8 @@ github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7V github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM= github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8= github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= +github.com/klauspost/cpuid/v2 v2.0.4/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= +github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= github.com/klauspost/cpuid/v2 v2.0.12/go.mod h1:g2LTdtYhdyuGPqyWyv7qRAmj1WBqxuObKfj5c0PQa7c= github.com/klauspost/cpuid/v2 v2.1.0 h1:eyi1Ad2aNJMW95zcSbmGg7Cg6cq3ADwLpMAP96d8rF0= github.com/klauspost/cpuid/v2 v2.1.0/go.mod h1:RVVoqg1df56z8g3pUjL/3lE5UfnlrJX8tyFgg4nqhuY= @@ -305,6 +307,7 @@ github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27k github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= github.com/miekg/dns v1.1.26/go.mod h1:bPDLeHnStXmXAq1m/Ch/hvfNHr14JKNPMBo3VZKjuso= github.com/miekg/dns v1.1.41/go.mod h1:p6aan82bvRIyn+zDIv9xYNUpwa73JcSh9BKwknJysuI= +github.com/minio/sha256-simd v1.0.0/go.mod h1:OuYzVNI5vcoYIAmbIvHPl3N3jUzVedXbKy5RFepssQM= github.com/mitchellh/cli v1.1.0/go.mod h1:xcISNoH86gajksDmfB23e/pu+B+GeFRMYmoHXxx3xhI= github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y= github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= @@ -319,6 +322,9 @@ github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJ github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= +github.com/mr-tron/base58 v1.2.0/go.mod h1:BinMc/sQntlIE1frQmRFPUoPA1Zkr8VRgBdjWI2mNwc= +github.com/multiformats/go-multihash v0.2.1/go.mod h1:WxoMcYG85AZVQUyRyo9s4wULvW5qrI9vb2Lt6evduFc= +github.com/multiformats/go-varint v0.0.6/go.mod h1:3Ls8CIEsrijN6+B7PbrXRPxHRPuXSrVKRY101jdMZYE= github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 h1:zYyBkD/k9seD2A7fsi6Oo2LfFZAehjjQMERAvZLEDnQ= @@ -372,6 +378,7 @@ github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrf github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e h1:MRM5ITcdelLK2j1vwZ3Je0FKVCfqOLp5zO6trqMLYs0= github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e/go.mod h1:XV66xRDqSt+GTGFMVlhk3ULuV0y9ZmzeVGR4mloJI3M= github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= +github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= github.com/spf13/afero v1.8.2/go.mod h1:CtAatgMJh6bJEIs48Ay/FOnkljP3WeGUG0MC1RfAqwo= github.com/spf13/afero v1.9.2 h1:j49Hj62F0n+DaZ1dDCvhABaPNSGNkt32oRFxI33IEMw= github.com/spf13/afero v1.9.2/go.mod h1:iUV7ddyEEZPO5gA3zD4fJt6iStLlL+Lg4m2cihcDf8Y= @@ -431,6 +438,12 @@ gitlab.com/elixxir/crypto v0.0.3/go.mod h1:ZNgBOblhYToR4m8tj4cMvJ9UsJAUKq+p0gCp0 gitlab.com/elixxir/crypto v0.0.7-0.20220317172048-3de167bd9406/go.mod h1:tD6XjtQh87T2nKZL5I/pYPck5M2wLpkZ1Oz7H/LqO10= gitlab.com/elixxir/crypto v0.0.7-0.20220606201132-c370d5039cea h1:+FjwbKl6X9TDT7qd7gG5N5PSbziPWP3NgjK5ci1b7/8= gitlab.com/elixxir/crypto v0.0.7-0.20220606201132-c370d5039cea/go.mod h1:Oy+VWQ2Sa0Ybata3oTV+Yc46hkaDwAsuIMW0wJ01z2M= +gitlab.com/elixxir/crypto v0.0.7-0.20220822171410-3699212bb2b3 h1:e42j+T2Fr5on1pD/BoDCPFjkYZYPb7Bj1a0bpentSNw= +gitlab.com/elixxir/crypto v0.0.7-0.20220822171410-3699212bb2b3/go.mod h1:0gH41Kb/a9Akdv+2+vMJVdx+REVwR8CZXTr2+BrgeeA= +gitlab.com/elixxir/crypto v0.0.7-0.20220822180551-92d06134b333 h1:V19ek4Hz3k1JB5pdUTpBQ8TewB418TTnvyYByuug2yo= +gitlab.com/elixxir/crypto v0.0.7-0.20220822180551-92d06134b333/go.mod h1:0gH41Kb/a9Akdv+2+vMJVdx+REVwR8CZXTr2+BrgeeA= +gitlab.com/elixxir/crypto v0.0.7-0.20220822180822-5e1b688a9500 h1:KI+7GoG2/9x/J5qdr9VCd2woGwE8RDaSMrt9STYfBKU= +gitlab.com/elixxir/crypto v0.0.7-0.20220822180822-5e1b688a9500/go.mod h1:0gH41Kb/a9Akdv+2+vMJVdx+REVwR8CZXTr2+BrgeeA= gitlab.com/elixxir/ekv v0.1.7 h1:OW2z+N4QCqqMFzouAwFTWWMKz0Y/PDhyYReN7gQ5NiQ= gitlab.com/elixxir/ekv v0.1.7/go.mod h1:e6WPUt97taFZe5PFLPb1Dupk7tqmDCTQu1kkstqJvw4= gitlab.com/elixxir/primitives v0.0.0-20200731184040-494269b53b4d/go.mod h1:OQgUZq7SjnE0b+8+iIAT2eqQF+2IFHn73tOo+aV11mg= @@ -499,6 +512,7 @@ golang.org/x/crypto v0.0.0-20211108221036-ceb1ce70b4fa/go.mod h1:GvvjBRRGRdwPK5y golang.org/x/crypto v0.0.0-20220128200615-198e4374d7ed/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= golang.org/x/crypto v0.0.0-20220315160706-3147a52a75dd/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= golang.org/x/crypto v0.0.0-20220411220226-7b82a4e95df4/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= +golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa h1:zuSxTR4o9y82ebqCUJYNGJbGPo6sKVl54f/TVDObg1c= golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= @@ -991,6 +1005,7 @@ honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWh honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg= honnef.co/go/tools v0.0.1-2020.1.3/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k= honnef.co/go/tools v0.0.1-2020.1.4/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k= +lukechampine.com/blake3 v1.1.6/go.mod h1:tkKEOtDkNtklkXtLNEOGNq5tcV90tJiA1vAA12R78LA= rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8= rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0= rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA= -- GitLab