mirror of
https://github.com/kovidgoyal/kitty
synced 2026-07-19 15:04:50 +02:00
Add some basic RC serialization tests
This commit is contained in:
@@ -6,13 +6,10 @@ import (
|
||||
"crypto/rand"
|
||||
"crypto/sha256"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"golang.org/x/crypto/curve25519"
|
||||
"kitty/tools/base85"
|
||||
"kitty/tools/utils"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -40,8 +37,8 @@ func b85_decode(data string) (decoded []byte, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
func encrypt(plaintext []byte, alice_public_key []byte) (iv []byte, tag []byte, ciphertext []byte, bob_public_key []byte, err error) {
|
||||
bob_private_key, bob_public_key, err := curve25519_key_pair()
|
||||
func encrypt(plaintext []byte, alice_public_key []byte, encryption_protocol string) (iv []byte, tag []byte, ciphertext []byte, bob_public_key []byte, err error) {
|
||||
bob_private_key, bob_public_key, err := KeyPair(encryption_protocol)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@@ -70,31 +67,40 @@ func encrypt(plaintext []byte, alice_public_key []byte) (iv []byte, tag []byte,
|
||||
return
|
||||
}
|
||||
|
||||
func Encrypt_cmd(cmd *utils.RemoteControlCmd, password string, other_pubkey []byte) (encrypted_cmd utils.EncryptedRemoteControlCmd, err error) {
|
||||
if len(other_pubkey) == 0 {
|
||||
raw := os.Getenv("KITTY_PUBLIC_KEY")
|
||||
if len(raw) == 0 {
|
||||
err = errors.New("No KITTY_PUBLIC_KEY environment variable set cannot use passwords")
|
||||
return
|
||||
}
|
||||
if !strings.HasPrefix(raw, "1:") {
|
||||
err = fmt.Errorf("KITTY_PUBLIC_KEY has unknown protocol: %s", raw[:2])
|
||||
return
|
||||
}
|
||||
other_pubkey, err = b85_decode(raw[2:])
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
func KeyPair(encryption_protocol string) (private_key []byte, public_key []byte, err error) {
|
||||
switch encryption_protocol {
|
||||
case "1":
|
||||
return curve25519_key_pair()
|
||||
default:
|
||||
err = fmt.Errorf("Unknown encryption protocol: %s", encryption_protocol)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func EncodePublicKey(pubkey []byte, encryption_protocol string) (ans string, err error) {
|
||||
switch encryption_protocol {
|
||||
case "1":
|
||||
ans = fmt.Sprintf("1:%s", b85_encode(pubkey))
|
||||
default:
|
||||
err = fmt.Errorf("Unknown encryption protocol: %s", encryption_protocol)
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func Encrypt_cmd(cmd *utils.RemoteControlCmd, password string, other_pubkey []byte, encryption_protocol string) (encrypted_cmd utils.EncryptedRemoteControlCmd, err error) {
|
||||
cmd.Password = password
|
||||
cmd.Timestamp = time.Now().UnixNano()
|
||||
plaintext, err := json.Marshal(cmd)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
iv, tag, ciphertext, pubkey, err := encrypt(plaintext, other_pubkey)
|
||||
iv, tag, ciphertext, pubkey, err := encrypt(plaintext, other_pubkey, encryption_protocol)
|
||||
encrypted_cmd = utils.EncryptedRemoteControlCmd{
|
||||
Version: cmd.Version, IV: b85_encode(iv), Tag: b85_encode(tag), Pubkey: b85_encode(pubkey), Encrypted: b85_encode(ciphertext)}
|
||||
if encryption_protocol != "1" {
|
||||
encrypted_cmd.EncProto = encryption_protocol
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user