failed to wrap invalid ecdh public key
This commit is contained in:
parent
51003e492b
commit
719a79a548
11 changed files with 95 additions and 34 deletions
|
@ -7,9 +7,9 @@ import (
|
||||||
"filippo.io/age"
|
"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)
|
input := bytes.NewReader(src)
|
||||||
out, err := age.Decrypt(input, &b.encDec)
|
out, err := age.Decrypt(input, &b.encPriv)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,17 +2,23 @@ package bloat
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
|
||||||
"filippo.io/age"
|
"filippo.io/age"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (b *Bloat[T]) Encrypt(plain []byte) ([]byte, error) {
|
func (b *Bloat[T]) encrypt(plain []byte) ([]byte, error) {
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
w, err := age.Encrypt(buf, &b.EncPub) // my wrapper doesn't support multiple recipients yet
|
w, err := age.Encrypt(buf, &b.EncPub) // my wrapper doesn't support multiple recipients yet
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return []byte{}, err
|
return []byte{}, err
|
||||||
}
|
}
|
||||||
|
_, err = w.Write(plain)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
return []byte{}, err
|
||||||
|
}
|
||||||
var r string
|
var r string
|
||||||
_, err = io.WriteString(w, r)
|
_, err = io.WriteString(w, r)
|
||||||
return []byte(r), err
|
return []byte(r), err
|
||||||
|
|
|
@ -2,7 +2,7 @@ package bloat
|
||||||
|
|
||||||
import "encoding/xml"
|
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)
|
final, err := xml.Marshal(body)
|
||||||
return final, err
|
return final, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +1,10 @@
|
||||||
package bloat
|
package bloat_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"git.bivouac.wiki/go/bloat"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestItem(t *testing.T) {
|
func TestItem(t *testing.T) {
|
||||||
|
@ -14,13 +16,12 @@ func TestItem(t *testing.T) {
|
||||||
A: 1,
|
A: 1,
|
||||||
B: "hello",
|
B: "hello",
|
||||||
}
|
}
|
||||||
trySender, err := NewBloat[typ]()
|
trySender, err := bloat.NewBloat[typ](false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
t.Errorf("error on newbloat")
|
t.Errorf("error on newbloat")
|
||||||
}
|
}
|
||||||
trySender.Obj = typ{}
|
tryReceiver, err := bloat.NewBloat[typ](false)
|
||||||
tryReceiver, err := NewBloat[typ]()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
t.Errorf("error on newbloat")
|
t.Errorf("error on newbloat")
|
||||||
|
@ -30,6 +31,7 @@ func TestItem(t *testing.T) {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
t.Errorf("error on write")
|
t.Errorf("error on write")
|
||||||
}
|
}
|
||||||
|
fmt.Println(cyphertext)
|
||||||
plainobj, err := tryReceiver.Read(cyphertext, trySender)
|
plainobj, err := tryReceiver.Read(cyphertext, trySender)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
|
|
29
obj.go
29
obj.go
|
@ -8,30 +8,31 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type Bloat[T any] struct {
|
type Bloat[T any] struct {
|
||||||
SignPub minisign.PublicKey
|
SignPub minisign.PublicKey
|
||||||
signPriv minisign.PrivateKey
|
signPriv minisign.PrivateKey
|
||||||
EncPub age.X25519Recipient
|
RequireEnc bool
|
||||||
encDec age.X25519Identity // Scrypt is the password one?
|
EncPub age.X25519Recipient
|
||||||
Obj T
|
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)
|
pub, sign, err := minisign.GenerateKey(rand.Reader)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Bloat[T]{}, err
|
return Bloat[T]{}, err
|
||||||
}
|
}
|
||||||
enc, err := age.GenerateX25519Identity()
|
|
||||||
if err != nil {
|
|
||||||
return Bloat[T]{}, err
|
|
||||||
}
|
|
||||||
recip := enc.Recipient()
|
|
||||||
|
|
||||||
res := Bloat[T]{
|
res := Bloat[T]{
|
||||||
SignPub: pub,
|
SignPub: pub,
|
||||||
signPriv: sign,
|
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
|
return res, nil
|
||||||
}
|
}
|
||||||
|
|
40
plain_test.go
Normal file
40
plain_test.go
Normal 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
12
read.go
|
@ -1,20 +1,24 @@
|
||||||
package bloat
|
package bloat
|
||||||
|
|
||||||
import "errors"
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
func (b *Bloat[T]) Read(packed []byte, author Bloat[T]) (T, error) {
|
func (b *Bloat[T]) Read(packed []byte, author Bloat[T]) (T, error) {
|
||||||
var final T
|
var final T
|
||||||
decrypted, err := b.Decrypt(packed) // our own key
|
decrypted, err := b.decrypt(packed) // our own key
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
return final, 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 {
|
if err != nil {
|
||||||
return final, err
|
return final, err
|
||||||
}
|
}
|
||||||
if !verified {
|
if !verified {
|
||||||
return final, errors.New("not verified")
|
return final, errors.New("not verified")
|
||||||
}
|
}
|
||||||
final, err = b.Unmarshal(body)
|
final, err = b.unmarshal(body)
|
||||||
return final, err
|
return final, err
|
||||||
}
|
}
|
||||||
|
|
2
sign.go
2
sign.go
|
@ -7,7 +7,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
// sign the payload and append 4 lines
|
// 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)
|
signature := minisign.Sign(b.signPriv, plaintext)
|
||||||
parts := [][]byte{plaintext, signature}
|
parts := [][]byte{plaintext, signature}
|
||||||
|
|
|
@ -12,7 +12,8 @@ types to decode:
|
||||||
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
func (b *Bloat[T]) Unmarshal(plain []byte) (T, error) {
|
func (b *Bloat[T]) unmarshal(plain []byte) (T, error) {
|
||||||
err := xml.Unmarshal(plain, b.Obj)
|
var res T
|
||||||
return b.Obj, err
|
err := xml.Unmarshal(plain, res)
|
||||||
|
return res, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,7 +7,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
// Verify checks the minisign signature and returns the body if valid.
|
// 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:
|
// signature is last 4 lines:
|
||||||
separated := bytes.Split(message, []byte("\n"))
|
separated := bytes.Split(message, []byte("\n"))
|
||||||
signature := bytes.Join(separated[len(separated)-4:len(separated)], []byte("\n"))
|
signature := bytes.Join(separated[len(separated)-4:len(separated)], []byte("\n"))
|
||||||
|
|
13
write.go
13
write.go
|
@ -1,14 +1,21 @@
|
||||||
package bloat
|
package bloat
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
func (b *Bloat[T]) Write(body T, recip Bloat[T]) ([]byte, error) {
|
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 {
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
signed, err := b.Sign(start) // our key
|
signed, err := b.sign(start) // our key
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
return nil, 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
|
return final, err
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue