2019-08-08 18:11:31 +00:00
|
|
|
package primitives
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rand"
|
2021-04-09 00:55:17 +00:00
|
|
|
"git.openprivacy.ca/cwtch.im/tapir/utils"
|
2020-02-06 23:54:13 +00:00
|
|
|
torProvider "git.openprivacy.ca/openprivacy/connectivity/tor"
|
2019-08-08 18:11:31 +00:00
|
|
|
"golang.org/x/crypto/ed25519"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Identity is an encapsulation of Name, PrivateKey and other features
|
|
|
|
// that make up a Tapir client.
|
|
|
|
// The purpose of Identity is to prevent other classes directly accessing private key
|
|
|
|
// and to ensure the integrity of security-critical functions.
|
|
|
|
type Identity struct {
|
|
|
|
Name string
|
|
|
|
edpk *ed25519.PrivateKey
|
|
|
|
edpubk *ed25519.PublicKey
|
|
|
|
}
|
|
|
|
|
2019-08-08 19:07:13 +00:00
|
|
|
// InitializeIdentity is a courtesy function for initializing a V3 Identity in-code.
|
|
|
|
func InitializeIdentity(name string, pk *ed25519.PrivateKey, pubk *ed25519.PublicKey) Identity {
|
2019-08-08 18:11:31 +00:00
|
|
|
return Identity{name, pk, pubk}
|
|
|
|
}
|
|
|
|
|
2019-08-08 19:07:13 +00:00
|
|
|
// InitializeEphemeralIdentity generates a new ephemeral identity, the private key of this identity is provided in the response.
|
|
|
|
func InitializeEphemeralIdentity() (Identity, ed25519.PrivateKey) {
|
2019-08-08 18:11:31 +00:00
|
|
|
epk, esk, _ := ed25519.GenerateKey(rand.Reader)
|
|
|
|
ephemeralPublicKey := ed25519.PublicKey(epk)
|
|
|
|
ephemeralPrivateKey := ed25519.PrivateKey(esk)
|
2019-08-08 19:07:13 +00:00
|
|
|
ephemeralIdentity := InitializeIdentity("", &ephemeralPrivateKey, &ephemeralPublicKey)
|
2019-08-08 18:11:31 +00:00
|
|
|
return ephemeralIdentity, ephemeralPrivateKey
|
|
|
|
}
|
|
|
|
|
|
|
|
// PublicKeyBytes returns the public key associated with this Identity in serializable-friendly
|
|
|
|
// format.
|
|
|
|
func (i *Identity) PublicKeyBytes() []byte {
|
|
|
|
return *i.edpubk
|
|
|
|
}
|
|
|
|
|
|
|
|
// PublicKey returns the public key associated with this Identity
|
|
|
|
func (i *Identity) PublicKey() ed25519.PublicKey {
|
|
|
|
return *i.edpubk
|
|
|
|
}
|
|
|
|
|
2019-09-14 23:44:19 +00:00
|
|
|
// EDH performs a diffie-hellman operation on this identities private key with the given public key.
|
2020-03-21 19:42:46 +00:00
|
|
|
func (i *Identity) EDH(key ed25519.PublicKey) ([]byte, error) {
|
|
|
|
secret, err := utils.EDH(*i.edpk, key)
|
|
|
|
return secret[:], err
|
2019-08-08 18:11:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Hostname provides the onion address associated with this Identity.
|
|
|
|
func (i *Identity) Hostname() string {
|
2020-02-06 23:54:13 +00:00
|
|
|
return torProvider.GetTorV3Hostname(*i.edpubk)
|
2019-08-08 18:11:31 +00:00
|
|
|
}
|
2019-09-14 23:44:19 +00:00
|
|
|
|
|
|
|
// Sign produces a signature for a given message attributable to the given identity
|
|
|
|
func (i *Identity) Sign(input []byte) []byte {
|
|
|
|
return ed25519.Sign(*i.edpk, input)
|
|
|
|
}
|