Skip to content

Commit

Permalink
Merge pull request #37 from Bit-Nation/fix/public_key
Browse files Browse the repository at this point in the history
Fix/public key
  • Loading branch information
florianlenz authored Jun 22, 2018
2 parents 942fab6 + 2b081e1 commit ba3fee6
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 25 deletions.
46 changes: 28 additions & 18 deletions chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,6 @@ func NewPreKeyBundle() (string, error) {
return "", err
}

// marshal public part
publicPart, err := bundle.PublicPart.Marshal()
if err != nil {
return "", err
}

// marshal private part and encrypt
privatePart, err := bundle.PrivatePart.Marshal()
if err != nil {
Expand All @@ -40,20 +34,18 @@ func NewPreKeyBundle() (string, error) {
return "", err
}

// marshal pre key bundle
preKeyBundle, err := json.Marshal(struct {
PublicPart string `json:"public_part"`
PrivatePart aes.CipherText `json:"private_part"`
}{
PublicPart: string(publicPart),
exportedBundle := chat.ExportedPreKeyBundle{
PublicPart: bundle.PublicPart,
PrivatePart: privtePartCipherText,
})
}

// marshal pre key bundle
marshaledExportedBundle, err := json.Marshal(exportedBundle)
if err != nil {
return "", err
}

return string(preKeyBundle), nil
return string(marshaledExportedBundle), nil

}

Expand All @@ -73,13 +65,31 @@ func InitializeChat(identityPublicKey, preKeyBundle string) (string, error) {
return "", errors.New("public key must have length of 32 bytes")
}

// decode pre key bundle
bundle, err := chat.UnmarshalPreKeyBundle([]byte(preKeyBundle))
// decode exported pre key bundle
b := chat.ExportedPreKeyBundle{}
if err := json.Unmarshal([]byte(preKeyBundle), &b); err != nil {
return "", err
}

// decrypt private part
rawPrivatePart, err := panthalassaInstance.km.AESDecrypt(b.PrivatePart)
if err != nil {
return "", err
}

msg, initializedProtocol, err := panthalassaInstance.chat.InitializeChat(pubKey, bundle)
// unmarshal private part
pp := chat.PreKeyBundlePrivate{}
if err := json.Unmarshal(rawPrivatePart, &pp); err != nil {
return "", err
}

// plain private part
privatePart := chat.PanthalassaPreKeyBundle{
PublicPart: b.PublicPart,
PrivatePart: pp,
}

msg, initializedProtocol, err := panthalassaInstance.chat.InitializeChat(pubKey, privatePart)
if err != nil {
return "", err
}
Expand All @@ -91,7 +101,7 @@ func InitializeChat(identityPublicKey, preKeyBundle string) (string, error) {

initialProtocol, err := json.Marshal(struct {
Message chat.Message `json:"message"`
Secret aes.CipherText `json:"secret"`
Secret aes.CipherText `json:"shared_chat_secret"`
}{
Message: msg,
Secret: exportedSecret,
Expand Down
2 changes: 1 addition & 1 deletion chat/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"encoding/hex"
"errors"

"github.com/Bit-Nation/panthalassa/crypto/aes"
aes "github.com/Bit-Nation/panthalassa/crypto/aes"
keyManager "github.com/Bit-Nation/panthalassa/keyManager"
x3dh "github.com/Bit-Nation/x3dh"
doubleratchet "github.com/tiabc/doubleratchet"
Expand Down
14 changes: 8 additions & 6 deletions chat/pre_key_bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"crypto/sha256"
"encoding/json"

aes "github.com/Bit-Nation/panthalassa/crypto/aes"
keyManager "github.com/Bit-Nation/panthalassa/keyManager"
x3dh "github.com/Bit-Nation/x3dh"
ed25519 "golang.org/x/crypto/ed25519"
Expand All @@ -22,6 +23,11 @@ type PreKeyBundlePrivate struct {
SignedPreKey x3dh.PrivateKey `json:"signed_one_time_pre_key"`
}

type ExportedPreKeyBundle struct {
PublicPart PreKeyBundlePublic `json:"public_part"`
PrivatePart aes.CipherText `json:"private_part"`
}

// marshal private part of pre key bundle
func (b *PreKeyBundlePrivate) Marshal() ([]byte, error) {
return json.Marshal(b)
Expand Down Expand Up @@ -82,13 +88,9 @@ func (b *PanthalassaPreKeyBundle) Marshal() ([]byte, error) {

// unmarshal pre key bundle
func UnmarshalPreKeyBundle(preKeyBundle []byte) (PanthalassaPreKeyBundle, error) {

var b PanthalassaPreKeyBundle
err := json.Unmarshal(preKeyBundle, &b)
if err != nil {
return PanthalassaPreKeyBundle{}, nil
if err := json.Unmarshal(preKeyBundle, &b); err != nil {
return PanthalassaPreKeyBundle{}, err
}

return b, nil

}

0 comments on commit ba3fee6

Please sign in to comment.