failed to wrap invalid ecdh public key

This commit is contained in:
Risotto Bias 2025-04-07 02:04:28 -06:00
parent 51003e492b
commit 719a79a548
11 changed files with 95 additions and 34 deletions

View file

@ -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
}

View file

@ -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

View file

@ -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
}

View file

@ -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)

29
obj.go
View file

@ -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
}

40
plain_test.go Normal file
View file

@ -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")
}
}

12
read.go
View file

@ -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
}

View file

@ -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}

View file

@ -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
}

View file

@ -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"))

View file

@ -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
}