From 719a79a548278d3cac58081e19d8b1d41b82af65 Mon Sep 17 00:00:00 2001 From: risotto Date: Mon, 7 Apr 2025 02:04:28 -0600 Subject: [PATCH] failed to wrap invalid ecdh public key --- decrypt.go | 4 ++-- encrypt.go | 8 +++++++- marshal.go | 2 +- module_test.go | 10 ++++++---- obj.go | 29 +++++++++++++++-------------- plain_test.go | 40 ++++++++++++++++++++++++++++++++++++++++ read.go | 12 ++++++++---- sign.go | 2 +- unmarshal.go | 7 ++++--- verify.go | 2 +- write.go | 13 ++++++++++--- 11 files changed, 95 insertions(+), 34 deletions(-) create mode 100644 plain_test.go diff --git a/decrypt.go b/decrypt.go index 00cd0cc..cd5dbdc 100644 --- a/decrypt.go +++ b/decrypt.go @@ -7,9 +7,9 @@ import ( "filippo.io/age" ) -func (b *Bloat[T]) Decrypt(src []byte) ([]byte, error) { +func (b *Bloat[T]) decrypt(src []byte) ([]byte, error) { input := bytes.NewReader(src) - out, err := age.Decrypt(input, &b.encDec) + out, err := age.Decrypt(input, &b.encPriv) if err != nil { return nil, err } diff --git a/encrypt.go b/encrypt.go index 1d512a9..f3749d7 100644 --- a/encrypt.go +++ b/encrypt.go @@ -2,17 +2,23 @@ package bloat import ( "bytes" + "fmt" "io" "filippo.io/age" ) -func (b *Bloat[T]) Encrypt(plain []byte) ([]byte, error) { +func (b *Bloat[T]) encrypt(plain []byte) ([]byte, error) { buf := &bytes.Buffer{} w, err := age.Encrypt(buf, &b.EncPub) // my wrapper doesn't support multiple recipients yet if err != nil { return []byte{}, err } + _, err = w.Write(plain) + if err != nil { + fmt.Println(err) + return []byte{}, err + } var r string _, err = io.WriteString(w, r) return []byte(r), err diff --git a/marshal.go b/marshal.go index f7b9291..a7c8bb8 100644 --- a/marshal.go +++ b/marshal.go @@ -2,7 +2,7 @@ package bloat import "encoding/xml" -func (b *Bloat[T]) Marshal(body T) ([]byte, error) { +func (b *Bloat[T]) marshal(body T) ([]byte, error) { final, err := xml.Marshal(body) return final, err } diff --git a/module_test.go b/module_test.go index 5e1d7d8..36a1802 100644 --- a/module_test.go +++ b/module_test.go @@ -1,8 +1,10 @@ -package bloat +package bloat_test import ( "fmt" "testing" + + "git.bivouac.wiki/go/bloat" ) func TestItem(t *testing.T) { @@ -14,13 +16,12 @@ func TestItem(t *testing.T) { A: 1, B: "hello", } - trySender, err := NewBloat[typ]() + trySender, err := bloat.NewBloat[typ](false) if err != nil { fmt.Println(err) t.Errorf("error on newbloat") } - trySender.Obj = typ{} - tryReceiver, err := NewBloat[typ]() + tryReceiver, err := bloat.NewBloat[typ](false) if err != nil { fmt.Println(err) t.Errorf("error on newbloat") @@ -30,6 +31,7 @@ func TestItem(t *testing.T) { fmt.Println(err) t.Errorf("error on write") } + fmt.Println(cyphertext) plainobj, err := tryReceiver.Read(cyphertext, trySender) if err != nil { fmt.Println(err) diff --git a/obj.go b/obj.go index a4f4f72..d809bcf 100644 --- a/obj.go +++ b/obj.go @@ -8,30 +8,31 @@ import ( ) type Bloat[T any] struct { - SignPub minisign.PublicKey - signPriv minisign.PrivateKey - EncPub age.X25519Recipient - encDec age.X25519Identity // Scrypt is the password one? - Obj T + SignPub minisign.PublicKey + signPriv minisign.PrivateKey + RequireEnc bool + EncPub age.X25519Recipient + encPriv age.X25519Identity // Scrypt is the password one? } -func NewBloat[T any]() (Bloat[T], error) { +func NewBloat[T any](noEnc bool) (Bloat[T], error) { pub, sign, err := minisign.GenerateKey(rand.Reader) if err != nil { return Bloat[T]{}, err } - enc, err := age.GenerateX25519Identity() - if err != nil { - return Bloat[T]{}, err - } - recip := enc.Recipient() - res := Bloat[T]{ SignPub: pub, signPriv: sign, - EncPub: *recip, - encDec: *enc, + } + if !noEnc { + enc, err := age.GenerateX25519Identity() + if err != nil { + return Bloat[T]{}, err + } + recip := enc.Recipient() + res.encPriv = *enc + res.EncPub = *recip } return res, nil } diff --git a/plain_test.go b/plain_test.go new file mode 100644 index 0000000..337e741 --- /dev/null +++ b/plain_test.go @@ -0,0 +1,40 @@ +package bloat_test + +import ( + "fmt" + "testing" + + "git.bivouac.wiki/go/bloat" +) + +// TestPlain without keys... +func TestPlain(t *testing.T) { + type foo struct { + Abc bool + Plain string + } + sender, err := bloat.NewBloat[foo](true) + if err != nil { + t.Errorf("init failed") + } + data := foo{ + Abc: true, + Plain: "this is plain", + } + recipient, err := bloat.NewBloat[foo](true) + if err != nil { + t.Errorf("recipient init failed") + } + plain, err := sender.Write(data, recipient) + if err != nil { + t.Errorf("writing plain failed") + } + fmt.Println(plain) + res, err := recipient.Read(plain, sender) + if res.Plain != "this is plain" { + t.Errorf("text does not match") + } + if err != nil { + t.Errorf("verification failed") + } +} diff --git a/read.go b/read.go index 7836f42..2ed7478 100644 --- a/read.go +++ b/read.go @@ -1,20 +1,24 @@ package bloat -import "errors" +import ( + "errors" + "fmt" +) func (b *Bloat[T]) Read(packed []byte, author Bloat[T]) (T, error) { var final T - decrypted, err := b.Decrypt(packed) // our own key + decrypted, err := b.decrypt(packed) // our own key if err != nil { + fmt.Println(err) return final, err } - body, verified, err := author.Verify(decrypted) // author's key + body, verified, err := author.verify(decrypted) // author's key if err != nil { return final, err } if !verified { return final, errors.New("not verified") } - final, err = b.Unmarshal(body) + final, err = b.unmarshal(body) return final, err } diff --git a/sign.go b/sign.go index 2cfd1e8..e0963e0 100644 --- a/sign.go +++ b/sign.go @@ -7,7 +7,7 @@ import ( ) // sign the payload and append 4 lines -func (b *Bloat[T]) Sign(plaintext []byte) ([]byte, error) { +func (b *Bloat[T]) sign(plaintext []byte) ([]byte, error) { signature := minisign.Sign(b.signPriv, plaintext) parts := [][]byte{plaintext, signature} diff --git a/unmarshal.go b/unmarshal.go index 323f97e..158c8d2 100644 --- a/unmarshal.go +++ b/unmarshal.go @@ -12,7 +12,8 @@ types to decode: */ -func (b *Bloat[T]) Unmarshal(plain []byte) (T, error) { - err := xml.Unmarshal(plain, b.Obj) - return b.Obj, err +func (b *Bloat[T]) unmarshal(plain []byte) (T, error) { + var res T + err := xml.Unmarshal(plain, res) + return res, err } diff --git a/verify.go b/verify.go index bc07e1b..41377f1 100644 --- a/verify.go +++ b/verify.go @@ -7,7 +7,7 @@ import ( ) // Verify checks the minisign signature and returns the body if valid. -func (b *Bloat[T]) Verify(message []byte) ([]byte, bool, error) { +func (b *Bloat[T]) verify(message []byte) ([]byte, bool, error) { // signature is last 4 lines: separated := bytes.Split(message, []byte("\n")) signature := bytes.Join(separated[len(separated)-4:len(separated)], []byte("\n")) diff --git a/write.go b/write.go index 5ca3a47..b6a8a85 100644 --- a/write.go +++ b/write.go @@ -1,14 +1,21 @@ package bloat +import "fmt" + func (b *Bloat[T]) Write(body T, recip Bloat[T]) ([]byte, error) { - start, err := b.Marshal(body) + start, err := b.marshal(body) if err != nil { + fmt.Println(err) return nil, err } - signed, err := b.Sign(start) // our key + signed, err := b.sign(start) // our key if err != nil { + fmt.Println(err) return nil, err } - final, err := recip.Encrypt(signed) // their key + final, err := recip.encrypt(signed) // their key + if err != nil { + fmt.Println(err) + } return final, err }