Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wip: berty bot experiments #4347

Draft
wants to merge 9 commits into
base: master
Choose a base branch
from
Draft
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ debug.log
/js/*.db.d
tool/deployments/*/data
node_modules
.idea/

# Go CMD's build output
go/cmd/berty/berty
Expand Down
2 changes: 1 addition & 1 deletion go/cmd/berty/groupinit.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
qrterminal "github.com/mdp/qrterminal/v3"
"github.com/peterbourgon/ff/v3/ffcli"

"berty.tech/berty/v2/go/internal/bertylinks"
"berty.tech/berty/v2/go/pkg/bertylinks"
"berty.tech/berty/v2/go/pkg/bertyprotocol"
"berty.tech/berty/v2/go/pkg/messengertypes"
)
Expand Down
2 changes: 1 addition & 1 deletion go/cmd/berty/mini/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"encoding/base64"
"fmt"

"berty.tech/berty/v2/go/internal/bertylinks"
"berty.tech/berty/v2/go/pkg/bertylinks"
"berty.tech/berty/v2/go/pkg/errcode"
"berty.tech/berty/v2/go/pkg/protocoltypes"
)
Expand Down
2 changes: 1 addition & 1 deletion go/cmd/berty/mini/view_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (v *groupView) View() tview.Primitive {
func (v *groupView) commandParser(ctx context.Context, input string) error {
input = strings.TrimSpace(input)

if len(input) > 0 && input[0] == '/' {
if len(input) > 1 && input[0] == '/' && input[1] != '/' {
for _, attrs := range commandList() {
if prefix := fmt.Sprintf("/%s", attrs.title); strings.HasPrefix(strings.ToLower(input), prefix) {
if !attrs.hideInLog {
Expand Down
7 changes: 6 additions & 1 deletion go/cmd/berty/mini/view_group_outgoing.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"github.com/mdp/qrterminal/v3"
"moul.io/godev"

"berty.tech/berty/v2/go/internal/bertylinks"
"berty.tech/berty/v2/go/pkg/bertylinks"
"berty.tech/berty/v2/go/pkg/bertyvcissuer"
"berty.tech/berty/v2/go/pkg/errcode"
"berty.tech/berty/v2/go/pkg/messengertypes"
Expand Down Expand Up @@ -959,6 +959,11 @@ func newMessageCommand(ctx context.Context, v *groupView, cmd string) error {
return nil
}

// check for '/' messages
if strings.HasPrefix(cmd, "//") {
cmd = cmd[1:]
}

payload, err := proto.Marshal(&messengertypes.AppMessage_UserMessage{
Body: cmd,
})
Expand Down
2 changes: 1 addition & 1 deletion go/cmd/berty/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (

"github.com/peterbourgon/ff/v3/ffcli"

"berty.tech/berty/v2/go/internal/cryptoutil"
"berty.tech/berty/v2/go/pkg/authtypes"
"berty.tech/berty/v2/go/pkg/bertypushrelay"
"berty.tech/berty/v2/go/pkg/cryptoutil"
"berty.tech/berty/v2/go/pkg/pushtypes"
)

Expand Down
2 changes: 1 addition & 1 deletion go/cmd/berty/servicekey.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/libp2p/go-libp2p-core/crypto"
"github.com/peterbourgon/ff/v3/ffcli"

"berty.tech/berty/v2/go/internal/cryptoutil"
"berty.tech/berty/v2/go/pkg/cryptoutil"
"berty.tech/berty/v2/go/pkg/errcode"
)

Expand Down
2 changes: 1 addition & 1 deletion go/cmd/welcomebot/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
"moul.io/u"
"moul.io/zapconfig"

"berty.tech/berty/v2/go/internal/bertylinks"
"berty.tech/berty/v2/go/pkg/bertylinks"
"berty.tech/berty/v2/go/pkg/bertyversion"
"berty.tech/berty/v2/go/pkg/messengertypes"
)
Expand Down
2 changes: 1 addition & 1 deletion go/internal/accountutils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ import (
"gorm.io/gorm"
"moul.io/zapgorm2"

"berty.tech/berty/v2/go/internal/cryptoutil"
"berty.tech/berty/v2/go/internal/logutil"
"berty.tech/berty/v2/go/pkg/accounttypes"
"berty.tech/berty/v2/go/pkg/cryptoutil"
"berty.tech/berty/v2/go/pkg/errcode"
encrepo "berty.tech/go-ipfs-repo-encrypted"
)
Expand Down
23 changes: 12 additions & 11 deletions go/internal/cryptoutil/attachment_crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"golang.org/x/crypto/sha3"

"berty.tech/berty/v2/go/internal/streamutil"
"berty.tech/berty/v2/go/pkg/cryptoutil"
"berty.tech/berty/v2/go/pkg/errcode"
"berty.tech/berty/v2/go/pkg/protocoltypes"
)
Expand All @@ -36,7 +37,7 @@ type attachmentCipher struct {
}

func attachmentNewCipher(sk libp2pcrypto.PrivKey) (*attachmentCipher, error) {
key, err := SeedFromEd25519PrivateKey(sk)
key, err := cryptoutil.SeedFromEd25519PrivateKey(sk)
if err != nil {
return nil, errcode.ErrInvalidInput.Wrap(err)
}
Expand Down Expand Up @@ -128,35 +129,35 @@ func attachmentKeyUnmarshal(s []byte) (libp2pcrypto.PrivKey, error) {

// - CID ENCRYPTION

func attachmentCIDEncryptionKey(source *[KeySize]byte) (*[KeySize]byte, error) {
func attachmentCIDEncryptionKey(source *[cryptoutil.KeySize]byte) (*[cryptoutil.KeySize]byte, error) {
hkdf := hkdf.New(sha3.New256, source[:], nil, []byte("cid encryption v0"))

var key [KeySize]byte
var key [cryptoutil.KeySize]byte
if _, err := io.ReadFull(hkdf, key[:]); err != nil {
return nil, errcode.ErrStreamRead.Wrap(err)
}

return &key, nil
}

func attachmentCIDEncrypt(sk *[KeySize]byte, cid []byte) ([]byte, error) {
nonce, err := GenerateNonce()
func attachmentCIDEncrypt(sk *[cryptoutil.KeySize]byte, cid []byte) ([]byte, error) {
nonce, err := cryptoutil.GenerateNonce()
if err != nil {
return nil, errcode.ErrCryptoNonceGeneration.Wrap(err)
}

return append(nonce[:], secretbox.Seal(nil, cid, nonce, sk)...), nil
}

func attachmentCIDDecrypt(sk *[KeySize]byte, eCID []byte) ([]byte, error) {
if len(eCID) <= NonceSize {
return nil, errcode.ErrInvalidInput.Wrap(fmt.Errorf("encrypted cid too small, got %v, expected to be > %v", len(eCID), NonceSize))
func attachmentCIDDecrypt(sk *[cryptoutil.KeySize]byte, eCID []byte) ([]byte, error) {
if len(eCID) <= cryptoutil.NonceSize {
return nil, errcode.ErrInvalidInput.Wrap(fmt.Errorf("encrypted cid too small, got %v, expected to be > %v", len(eCID), cryptoutil.NonceSize))
}

var nonce [NonceSize]byte
_ = copy(nonce[:], eCID[:NonceSize])
var nonce [cryptoutil.NonceSize]byte
_ = copy(nonce[:], eCID[:cryptoutil.NonceSize])

cid, ok := secretbox.Open(nil, eCID[NonceSize:], &nonce, sk)
cid, ok := secretbox.Open(nil, eCID[cryptoutil.NonceSize:], &nonce, sk)
if !ok {
return nil, errcode.ErrCryptoDecrypt
}
Expand Down
19 changes: 10 additions & 9 deletions go/internal/cryptoutil/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"golang.org/x/crypto/hkdf"
"golang.org/x/crypto/sha3"

"berty.tech/berty/v2/go/pkg/cryptoutil"
"berty.tech/berty/v2/go/pkg/errcode"
)

Expand All @@ -23,8 +24,8 @@ type GroupWithLinkKey interface {
GetLinkKey() []byte
}

func ComputeLinkKey(publicKey, secret []byte) (*[KeySize]byte, error) {
arr := [KeySize]byte{}
func ComputeLinkKey(publicKey, secret []byte) (*[cryptoutil.KeySize]byte, error) {
arr := [cryptoutil.KeySize]byte{}

kdf := hkdf.New(sha3.New256, secret, nil, publicKey)
if _, err := io.ReadFull(kdf, arr[:]); err != nil {
Expand All @@ -34,9 +35,9 @@ func ComputeLinkKey(publicKey, secret []byte) (*[KeySize]byte, error) {
return &arr, nil
}

func GetLinkKeyArray(m GroupWithLinkKey) (*[KeySize]byte, error) {
if len(m.GetLinkKey()) == KeySize {
arr := [KeySize]byte{}
func GetLinkKeyArray(m GroupWithLinkKey) (*[cryptoutil.KeySize]byte, error) {
if len(m.GetLinkKey()) == cryptoutil.KeySize {
arr := [cryptoutil.KeySize]byte{}

for i, c := range m.GetLinkKey() {
arr[i] = c
Expand All @@ -48,8 +49,8 @@ func GetLinkKeyArray(m GroupWithLinkKey) (*[KeySize]byte, error) {
return ComputeLinkKey(m.GetPublicKey(), m.GetSecret())
}

func GetSharedSecret(m GroupWithLinkKey) *[KeySize]byte {
sharedSecret := [KeySize]byte{}
func GetSharedSecret(m GroupWithLinkKey) *[cryptoutil.KeySize]byte {
sharedSecret := [cryptoutil.KeySize]byte{}
copy(sharedSecret[:], m.GetSecret())

return &sharedSecret
Expand All @@ -60,7 +61,7 @@ func GetGroupPushSecret(m GroupWithSecret) ([]byte, error) {
return nil, errcode.ErrInvalidInput.Wrap(fmt.Errorf("no secret known for group"))
}

arr := [KeySize]byte{}
arr := [cryptoutil.KeySize]byte{}

kdf := hkdf.New(sha3.New256, m.GetSecret(), nil, []byte(PushSecretNamespace))
if _, err := io.ReadFull(kdf, arr[:]); err != nil {
Expand All @@ -71,7 +72,7 @@ func GetGroupPushSecret(m GroupWithSecret) ([]byte, error) {
}

func CreatePushGroupReference(sender []byte, counter uint64, secret []byte) ([]byte, error) {
arr := [KeySize]byte{}
arr := [cryptoutil.KeySize]byte{}

buf := make([]byte, 8)
binary.BigEndian.PutUint64(buf, counter)
Expand Down
5 changes: 3 additions & 2 deletions go/internal/cryptoutil/group_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"golang.org/x/crypto/hkdf"

"berty.tech/berty/v2/go/internal/datastoreutil"
"berty.tech/berty/v2/go/pkg/cryptoutil"
"berty.tech/berty/v2/go/pkg/errcode"
"berty.tech/berty/v2/go/pkg/protocoltypes"
)
Expand Down Expand Up @@ -136,7 +137,7 @@ func GetGroupForContact(contactPairSK crypto.PrivKey) (*protocoltypes.Group, err
return nil, errcode.ErrSerialization.Wrap(err)
}

signingBytes, err := SeedFromEd25519PrivateKey(groupSecretSK)
signingBytes, err := cryptoutil.SeedFromEd25519PrivateKey(groupSecretSK)
if err != nil {
return nil, errcode.ErrSerialization.Wrap(err)
}
Expand All @@ -155,7 +156,7 @@ func GetGroupForAccount(priv, signing crypto.PrivKey) (*protocoltypes.Group, err
return nil, errcode.ErrSerialization.Wrap(err)
}

signingBytes, err := SeedFromEd25519PrivateKey(signing)
signingBytes, err := cryptoutil.SeedFromEd25519PrivateKey(signing)
if err != nil {
return nil, errcode.ErrSerialization.Wrap(err)
}
Expand Down
3 changes: 2 additions & 1 deletion go/internal/cryptoutil/keystore_device.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"go.uber.org/zap"

"berty.tech/berty/v2/go/internal/logutil"
"berty.tech/berty/v2/go/pkg/cryptoutil"
"berty.tech/berty/v2/go/pkg/errcode"
"berty.tech/berty/v2/go/pkg/protocoltypes"
)
Expand Down Expand Up @@ -189,7 +190,7 @@ func (a *deviceKeystore) getOrComputeECDH(nameSpace string, pk crypto.PubKey, ow
return nil, err
}

skB, pkB, err := EdwardsToMontgomery(ownSK, pk)
skB, pkB, err := cryptoutil.EdwardsToMontgomery(ownSK, pk)
if err != nil {
return nil, err
}
Expand Down
5 changes: 3 additions & 2 deletions go/internal/cryptoutil/keystore_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/libp2p/go-libp2p-core/crypto"
"golang.org/x/crypto/nacl/secretbox"

"berty.tech/berty/v2/go/pkg/cryptoutil"
"berty.tech/berty/v2/go/pkg/errcode"
"berty.tech/berty/v2/go/pkg/protocoltypes"
)
Expand Down Expand Up @@ -316,7 +317,7 @@ func (m *MessageKeystore) getPrecomputedKey(ctx context.Context, groupPK, device
return nil, errcode.ErrMessageKeyPersistenceGet.Wrap(err)
}

keyArray, err := KeySliceToArray(key)
keyArray, err := cryptoutil.KeySliceToArray(key)
if err != nil {
return nil, errcode.ErrSerialization
}
Expand Down Expand Up @@ -514,7 +515,7 @@ func (m *MessageKeystore) GetKeyForCID(ctx context.Context, id cid.Cid) (*[32]by
return nil, errcode.ErrInvalidInput
}

keyArray, err := KeySliceToArray(key)
keyArray, err := cryptoutil.KeySliceToArray(key)
if err != nil {
return nil, errcode.ErrSerialization
}
Expand Down
5 changes: 3 additions & 2 deletions go/internal/cryptoutil/keystore_message_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"golang.org/x/crypto/hkdf"
"golang.org/x/crypto/nacl/secretbox"

"berty.tech/berty/v2/go/pkg/cryptoutil"
"berty.tech/berty/v2/go/pkg/errcode"
"berty.tech/berty/v2/go/pkg/protocoltypes"
)
Expand Down Expand Up @@ -59,7 +60,7 @@ func SealEnvelope(payload []byte, ds *protocoltypes.DeviceSecret, deviceSK crypt
return nil, errcode.ErrSerialization.Wrap(err)
}

nonce, err := GenerateNonce()
nonce, err := cryptoutil.GenerateNonce()
if err != nil {
return nil, errcode.ErrCryptoNonceGeneration.Wrap(err)
}
Expand Down Expand Up @@ -91,7 +92,7 @@ func OpenEnvelopeHeaders(data []byte, g *protocoltypes.Group) (*protocoltypes.Me
return nil, nil, errcode.ErrDeserialization.Wrap(err)
}

nonce, err := NonceSliceToArray(env.Nonce)
nonce, err := cryptoutil.NonceSliceToArray(env.Nonce)
if err != nil {
return nil, nil, errcode.ErrSerialization.Wrap(err)
}
Expand Down
2 changes: 1 addition & 1 deletion go/internal/handshake/handshake.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
p2pcrypto "github.com/libp2p/go-libp2p-core/crypto"
"golang.org/x/crypto/nacl/box"

"berty.tech/berty/v2/go/internal/cryptoutil"
"berty.tech/berty/v2/go/pkg/cryptoutil"
"berty.tech/berty/v2/go/pkg/errcode"
"berty.tech/berty/v2/go/pkg/tyber"
)
Expand Down
2 changes: 1 addition & 1 deletion go/internal/handshake/handshake_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import (
"go.uber.org/zap"
"golang.org/x/crypto/nacl/box"

"berty.tech/berty/v2/go/internal/cryptoutil"
"berty.tech/berty/v2/go/internal/ipfsutil"
"berty.tech/berty/v2/go/internal/testutil"
"berty.tech/berty/v2/go/pkg/cryptoutil"
"berty.tech/berty/v2/go/pkg/errcode"
)

Expand Down
2 changes: 1 addition & 1 deletion go/internal/handshake/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"go.uber.org/zap"
"golang.org/x/crypto/nacl/box"

"berty.tech/berty/v2/go/internal/cryptoutil"
"berty.tech/berty/v2/go/pkg/cryptoutil"
"berty.tech/berty/v2/go/pkg/errcode"
"berty.tech/berty/v2/go/pkg/tyber"
)
Expand Down
2 changes: 1 addition & 1 deletion go/internal/handshake/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"go.uber.org/zap"
"golang.org/x/crypto/nacl/box"

"berty.tech/berty/v2/go/internal/cryptoutil"
"berty.tech/berty/v2/go/pkg/cryptoutil"
"berty.tech/berty/v2/go/pkg/errcode"
"berty.tech/berty/v2/go/pkg/tyber"
)
Expand Down
5 changes: 3 additions & 2 deletions go/internal/initutil/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"berty.tech/berty/v2/go/internal/logutil"
"berty.tech/berty/v2/go/pkg/bertymessenger"
"berty.tech/berty/v2/go/pkg/bertyprotocol"
cryptoutil2 "berty.tech/berty/v2/go/pkg/cryptoutil"
"berty.tech/berty/v2/go/pkg/errcode"
"berty.tech/berty/v2/go/pkg/messengertypes"
"berty.tech/berty/v2/go/pkg/protocoltypes"
Expand Down Expand Up @@ -138,8 +139,8 @@ func (m *Manager) GetLocalProtocolServer() (bertyprotocol.Service, error) {
return m.getLocalProtocolServer()
}

func (m *Manager) getPushSecretKey() (*[cryptoutil.KeySize]byte, error) {
pushKey := &[cryptoutil.KeySize]byte{}
func (m *Manager) getPushSecretKey() (*[cryptoutil2.KeySize]byte, error) {
pushKey := &[cryptoutil2.KeySize]byte{}
if m.Node.Protocol.DevicePushKeyPath != "" {
var err error

Expand Down
2 changes: 1 addition & 1 deletion go/internal/omnisearch/berty-parser.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package omnisearch

import (
"berty.tech/berty/v2/go/internal/bertylinks"
"berty.tech/berty/v2/go/pkg/bertylinks"
"berty.tech/berty/v2/go/pkg/messengertypes"
)

Expand Down
2 changes: 1 addition & 1 deletion go/pkg/bertyauth/auth_session.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"crypto/sha256"
"encoding/base64"

"berty.tech/berty/v2/go/internal/cryptoutil"
"berty.tech/berty/v2/go/pkg/cryptoutil"
)

type AuthSession struct {
Expand Down
2 changes: 1 addition & 1 deletion go/pkg/bertyauth/services_auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import (
"google.golang.org/grpc/status"
"gopkg.in/square/go-jose.v2"

"berty.tech/berty/v2/go/internal/cryptoutil"
"berty.tech/berty/v2/go/pkg/authtypes"
"berty.tech/berty/v2/go/pkg/cryptoutil"
"berty.tech/berty/v2/go/pkg/errcode"
"berty.tech/berty/v2/go/pkg/protocoltypes"
)
Expand Down