diff options
author | Wim <wim@42.be> | 2022-01-31 00:27:37 +0100 |
---|---|---|
committer | Wim <wim@42.be> | 2022-03-20 14:57:48 +0100 |
commit | e3cafeaf9292f67459ff1d186f68283bfaedf2ae (patch) | |
tree | b69c39620aa91dba695b3b935c6651c0fb37ce75 /vendor/go.mau.fi/whatsmeow/util/keys/keypair.go | |
parent | e7b193788a56ee7cdb02a87a9db0ad6724ef66d5 (diff) | |
download | matterbridge-msglm-e3cafeaf9292f67459ff1d186f68283bfaedf2ae.tar.gz matterbridge-msglm-e3cafeaf9292f67459ff1d186f68283bfaedf2ae.tar.bz2 matterbridge-msglm-e3cafeaf9292f67459ff1d186f68283bfaedf2ae.zip |
Add dependencies/vendor (whatsapp)
Diffstat (limited to 'vendor/go.mau.fi/whatsmeow/util/keys/keypair.go')
-rw-r--r-- | vendor/go.mau.fi/whatsmeow/util/keys/keypair.go | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/vendor/go.mau.fi/whatsmeow/util/keys/keypair.go b/vendor/go.mau.fi/whatsmeow/util/keys/keypair.go new file mode 100644 index 00000000..55679ff2 --- /dev/null +++ b/vendor/go.mau.fi/whatsmeow/util/keys/keypair.go @@ -0,0 +1,75 @@ +// Copyright (c) 2021 Tulir Asokan +// +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at http://mozilla.org/MPL/2.0/. + +// Package keys contains a utility struct for elliptic curve keypairs. +package keys + +import ( + "crypto/rand" + "fmt" + + "go.mau.fi/libsignal/ecc" + "golang.org/x/crypto/curve25519" +) + +type KeyPair struct { + Pub *[32]byte + Priv *[32]byte +} + +var _ ecc.ECPublicKeyable + +func NewKeyPairFromPrivateKey(priv [32]byte) *KeyPair { + var kp KeyPair + kp.Priv = &priv + var pub [32]byte + curve25519.ScalarBaseMult(&pub, kp.Priv) + kp.Pub = &pub + return &kp +} + +func NewKeyPair() *KeyPair { + var priv [32]byte + + _, err := rand.Read(priv[:]) + if err != nil { + panic(fmt.Errorf("failed to generate curve25519 private key: %w", err)) + } + + priv[0] &= 248 + priv[31] &= 127 + priv[31] |= 64 + + return NewKeyPairFromPrivateKey(priv) +} + +func (kp *KeyPair) CreateSignedPreKey(keyID uint32) *PreKey { + newKey := NewPreKey(keyID) + newKey.Signature = kp.Sign(&newKey.KeyPair) + return newKey +} + +func (kp *KeyPair) Sign(keyToSign *KeyPair) *[64]byte { + pubKeyForSignature := make([]byte, 33) + pubKeyForSignature[0] = ecc.DjbType + copy(pubKeyForSignature[1:], keyToSign.Pub[:]) + + signature := ecc.CalculateSignature(ecc.NewDjbECPrivateKey(*kp.Priv), pubKeyForSignature) + return &signature +} + +type PreKey struct { + KeyPair + KeyID uint32 + Signature *[64]byte +} + +func NewPreKey(keyID uint32) *PreKey { + return &PreKey{ + KeyPair: *NewKeyPair(), + KeyID: keyID, + } +} |