Skip to content

Commit

Permalink
feat: Offer non-cloning options for non-mobile clients
Browse files Browse the repository at this point in the history
  • Loading branch information
lubux committed Dec 8, 2023
1 parent 7e59772 commit 5afb347
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
6 changes: 6 additions & 0 deletions crypto/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,16 @@ func NewKeyFromReaderExplicit(r io.Reader, encoding int8) (key *Key, err error)
}

// NewKey creates a new key from the first key in the unarmored or armored binary data.
// Clones the binKeys data for go-mobile compatibility.
func NewKey(binKeys []byte) (key *Key, err error) {
return NewKeyFromReader(bytes.NewReader(clone(binKeys)))
}

// NewKeyNoClone creates a new key from the first key in the unarmored or armored binary data.
func NewKeyNoClone(binKeys []byte) (key *Key, err error) {
return NewKeyFromReader(bytes.NewReader(binKeys))
}

// NewKeyFromArmored creates a new key from the first key in an armored string.
func NewKeyFromArmored(armored string) (key *Key, err error) {
return NewKeyFromReader(strings.NewReader(armored))
Expand Down
19 changes: 17 additions & 2 deletions crypto/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,21 @@ func NewMetadata(isUTF8 bool) *LiteralMetadata {
}

// NewPGPMessage generates a new PGPMessage from the unarmored binary data.
// Clones the data for go-mobile compatibility.
func NewPGPMessage(data []byte) *PGPMessage {
return NewPGPMessageNoClone(clone(data))
}

// NewPGPMessageNoClone generates a new PGPMessage from the unarmored binary data.
func NewPGPMessageNoClone(data []byte) *PGPMessage {
pgpMessage := &PGPMessage{
DataPacket: clone(data),
DataPacket: data,
}
pgpMessage, err := pgpMessage.splitMessage()
if err != nil {
// If there is an error in split treat the data as data packets.
return &PGPMessage{
DataPacket: clone(data),
DataPacket: data,
}
}
return pgpMessage
Expand Down Expand Up @@ -92,13 +98,22 @@ func NewPGPMessageFromArmored(armored string) (*PGPMessage, error) {
}

// NewPGPSplitMessage generates a new PGPSplitMessage from the binary unarmored keypacket and datapacket.
// Clones the slices for go-mobile compatibility.
func NewPGPSplitMessage(keyPacket []byte, dataPacket []byte) *PGPMessage {
return &PGPMessage{
KeyPacket: clone(keyPacket),
DataPacket: clone(dataPacket),
}
}

// NewPGPSplitMessageNoClone generates a new PGPSplitMessage from the binary unarmored keypacket and datapacket.
func NewPGPSplitMessageNoClone(keyPacket []byte, dataPacket []byte) *PGPMessage {
return &PGPMessage{
KeyPacket: keyPacket,
DataPacket: dataPacket,
}
}

// NewPGPMessageBuffer creates a message buffer.
func NewPGPMessageBuffer() *PGPMessageBuffer {
return &PGPMessageBuffer{
Expand Down
10 changes: 10 additions & 0 deletions crypto/sessionkey.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,23 @@ func generateSessionKey(config *packet.Config) (*SessionKey, error) {
return GenerateSessionKeyAlgo(cf)
}

// NewSessionKeyFromToken creates a SessionKey struct with the given token and algorithm.
// Clones the token for compatibility with go-mobile.
func NewSessionKeyFromToken(token []byte, algo string) *SessionKey {
return &SessionKey{
Key: clone(token),
Algo: algo,
}
}

// NewSessionKeyFromTokenNoClone creates a SessionKey struct with the given token and algorithm.
func NewSessionKeyFromTokenNoClone(token []byte, algo string) *SessionKey {
return &SessionKey{
Key: token,
Algo: algo,
}
}

func newSessionKeyFromEncrypted(ek *packet.EncryptedKey) (*SessionKey, error) {
var algo string
for k, v := range symKeyAlgos {
Expand Down

0 comments on commit 5afb347

Please sign in to comment.