2017-05-02 23:33:51 +00:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
2017-11-02 23:43:11 +00:00
|
|
|
"crypto/rand"
|
2017-05-02 23:33:51 +00:00
|
|
|
"crypto/rsa"
|
|
|
|
"crypto/x509"
|
|
|
|
"encoding/pem"
|
2018-10-09 19:55:42 +00:00
|
|
|
"github.com/agl/ed25519/extra25519"
|
2018-09-22 20:12:08 +00:00
|
|
|
"golang.org/x/crypto/curve25519"
|
|
|
|
"golang.org/x/crypto/ed25519"
|
2017-11-02 23:43:11 +00:00
|
|
|
"io/ioutil"
|
2018-01-12 18:02:15 +00:00
|
|
|
"math"
|
|
|
|
"math/big"
|
2017-05-02 23:33:51 +00:00
|
|
|
)
|
|
|
|
|
2017-07-04 18:29:11 +00:00
|
|
|
const (
|
2018-01-08 19:01:23 +00:00
|
|
|
// InvalidPrivateKeyFileError is a library error, thrown when the given key file fials to load
|
2017-07-04 18:29:11 +00:00
|
|
|
InvalidPrivateKeyFileError = Error("InvalidPrivateKeyFileError")
|
2018-01-08 19:01:23 +00:00
|
|
|
|
|
|
|
// RicochetKeySize - tor onion services currently use rsa key sizes of 1024 bits
|
|
|
|
RicochetKeySize = 1024
|
2017-07-04 18:29:11 +00:00
|
|
|
)
|
|
|
|
|
2018-05-09 19:40:07 +00:00
|
|
|
// GetRandNumber is a helper function which returns a random integer, this is
|
2018-01-17 18:18:46 +00:00
|
|
|
// currently mostly used to generate messageids
|
2018-01-12 18:02:15 +00:00
|
|
|
func GetRandNumber() *big.Int {
|
2018-01-12 18:04:20 +00:00
|
|
|
num, err := rand.Int(rand.Reader, big.NewInt(math.MaxUint32))
|
|
|
|
// If we can't generate random numbers then panicking is probably
|
|
|
|
// the best option.
|
|
|
|
CheckError(err)
|
|
|
|
return num
|
2018-01-12 18:02:15 +00:00
|
|
|
}
|
|
|
|
|
2018-09-22 20:12:08 +00:00
|
|
|
// EDH implements diffie hellman using curve25519 keys derived from ed25519 keys
|
|
|
|
// NOTE: This uses a 3rd party library extra25519 as the key conversion is not in the core golang lib
|
|
|
|
// as such this definitely needs further review.
|
|
|
|
func EDH(privateKey ed25519.PrivateKey, remotePublicKey ed25519.PublicKey) [32]byte {
|
|
|
|
var privKeyBytes [64]byte
|
|
|
|
var remotePubKeyBytes [32]byte
|
|
|
|
copy(privKeyBytes[:], privateKey[:])
|
|
|
|
copy(remotePubKeyBytes[:], remotePublicKey[:])
|
|
|
|
var secret, curve25519priv, curve25519pub [32]byte
|
|
|
|
extra25519.PrivateKeyToCurve25519(&curve25519priv, &privKeyBytes)
|
|
|
|
extra25519.PublicKeyToCurve25519(&curve25519pub, &remotePubKeyBytes)
|
|
|
|
curve25519.ScalarMult(&secret, &curve25519priv, &curve25519pub)
|
|
|
|
return secret
|
|
|
|
}
|
|
|
|
|
2019-01-09 22:33:33 +00:00
|
|
|
// GeneratePrivateKeyV3 cryptographically creats a new ed25519 key pair.
|
2018-11-09 21:33:35 +00:00
|
|
|
func GeneratePrivateKeyV3() (ed25519.PublicKey, ed25519.PrivateKey, error) {
|
|
|
|
return ed25519.GenerateKey(rand.Reader)
|
2017-08-14 15:43:33 +00:00
|
|
|
}
|
|
|
|
|
2017-05-02 23:33:51 +00:00
|
|
|
// LoadPrivateKeyFromFile loads a private key from a file...
|
|
|
|
func LoadPrivateKeyFromFile(filename string) (*rsa.PrivateKey, error) {
|
|
|
|
pemData, err := ioutil.ReadFile(filename)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-08-14 15:43:33 +00:00
|
|
|
return ParsePrivateKey(pemData)
|
|
|
|
}
|
|
|
|
|
2018-01-08 19:01:23 +00:00
|
|
|
// ParsePrivateKey Convert a private key string to a usable private key
|
2017-08-14 15:43:33 +00:00
|
|
|
func ParsePrivateKey(pemData []byte) (*rsa.PrivateKey, error) {
|
2017-05-02 23:33:51 +00:00
|
|
|
block, _ := pem.Decode(pemData)
|
|
|
|
if block == nil || block.Type != "RSA PRIVATE KEY" {
|
2017-07-04 18:29:11 +00:00
|
|
|
return nil, InvalidPrivateKeyFileError
|
2017-05-02 23:33:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return x509.ParsePKCS1PrivateKey(block.Bytes)
|
|
|
|
}
|
2017-08-14 15:43:33 +00:00
|
|
|
|
2018-01-08 19:01:23 +00:00
|
|
|
// PrivateKeyToString turns a private key into storable string
|
2017-08-14 15:43:33 +00:00
|
|
|
func PrivateKeyToString(privateKey *rsa.PrivateKey) string {
|
|
|
|
privateKeyBlock := pem.Block{
|
2017-11-02 23:43:11 +00:00
|
|
|
Type: "RSA PRIVATE KEY",
|
2017-08-14 15:43:33 +00:00
|
|
|
Headers: nil,
|
2017-11-02 23:43:11 +00:00
|
|
|
Bytes: x509.MarshalPKCS1PrivateKey(privateKey),
|
2017-08-14 15:43:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return string(pem.EncodeToMemory(&privateKeyBlock))
|
|
|
|
}
|