Documentation ¶
Overview ¶
Package torcrypto provides cryptographic functions useful in tor.
Index ¶
- Constants
- func CheckPrivateKeyPermissions(filename string) error
- func ExtractRSAPublicKeyFromCertificate(cert *x509.Certificate) (*rsa.PublicKey, error)
- func Fingerprint(k *rsa.PublicKey) ([]byte, error)
- func Fingerprint256(k *rsa.PublicKey) ([]byte, error)
- func GenerateRSA() (*rsa.PrivateKey, error)
- func GenerateRSAWithBits(bits int) (*rsa.PrivateKey, error)
- func HashWrite(h hash.Hash, b []byte)
- func HybridDecrypt(pk *rsa.PrivateKey, z []byte) ([]byte, error)
- func KDFTOR(k []byte, n int) ([]byte, error)
- func LoadRSAPrivateKeyFromPEMFile(filename string) (*rsa.PrivateKey, error)
- func LoadRSAPublicKeyFromPEMFile(filename string) (*rsa.PublicKey, error)
- func MarshalRSAPrivateKeyPKCS1DER(k *rsa.PrivateKey) []byte
- func MarshalRSAPrivateKeyPKCS1PEM(k *rsa.PrivateKey) []byte
- func MarshalRSAPublicKeyPKCS1DER(k *rsa.PublicKey) ([]byte, error)
- func MarshalRSAPublicKeyPKCS1PEM(k *rsa.PublicKey) ([]byte, error)
- func MustRSAPrivateKey(k *rsa.PrivateKey, err error) *rsa.PrivateKey
- func MustRSAPublicKey(k *rsa.PublicKey, err error) *rsa.PublicKey
- func NewStream(key []byte) cipher.Stream
- func ParseRSAPrivateKeyPKCS1DER(b []byte) (*rsa.PrivateKey, error)
- func ParseRSAPrivateKeyPKCS1PEM(b []byte) (*rsa.PrivateKey, error)
- func ParseRSAPublicKeyFromCertificateDER(der []byte) (*rsa.PublicKey, error)
- func ParseRSAPublicKeyPKCS1DER(b []byte) (*rsa.PublicKey, error)
- func ParseRSAPublicKeyPKCS1PEM(b []byte) (*rsa.PublicKey, error)
- func RSAPrivateKeySize(k *rsa.PrivateKey) int
- func RSAPublicKeySize(k *rsa.PublicKey) int
- func RSAPublicKeysEqual(k1, k2 *rsa.PublicKey) bool
- func Rand(n int) []byte
- func SaveCurve25519KeyPairPrivateKeyToFile(k *Curve25519KeyPair, filename, label string) error
- func SaveRSAPrivateKeyToPEMFile(k *rsa.PrivateKey, filename string) error
- func SaveRSAPublicKeyToPEMFile(k *rsa.PublicKey, filename string) error
- func SetPrivateKeyPermissions(filename string) error
- func SignRSASHA1(data []byte, k *rsa.PrivateKey) ([]byte, error)
- func SignRSASHA256(data []byte, k *rsa.PrivateKey) ([]byte, error)
- func VerifyRSASHA1(k *rsa.PublicKey, data, sig []byte) error
- func VerifyRSASHA256(k *rsa.PublicKey, data, sig []byte) error
- type Curve25519KeyPair
- type DiffieHellmanKey
Constants ¶
const ( StreamCipherKeySize = 16 DiffieHellmanPublicSize = 128 DiffieHellmanSecretSize = 40 PublicKeyMessageSize = 128 PublicKeyPaddingSize = 42 HashSize = 20 )
Security parameters.
KEY_LEN=16. DH_LEN=128; DH_SEC_LEN=40. PK_ENC_LEN=128; PK_PAD_LEN=42. HASH_LEN=20.
Variables ¶
This section is empty.
Functions ¶
func CheckPrivateKeyPermissions ¶
CheckPrivateKeyPermissions checks whether the given file has appropriate permissions for a private key.
func ExtractRSAPublicKeyFromCertificate ¶
func ExtractRSAPublicKeyFromCertificate(cert *x509.Certificate) (*rsa.PublicKey, error)
func Fingerprint ¶
Fingerprint computes the SHA-1 hash of a public key referred to as a fingerprint.
func Fingerprint256 ¶
Fingerprint256 computes the SHA-256 hash of a public key.
func GenerateRSA ¶
func GenerateRSA() (*rsa.PrivateKey, error)
GenerateRSA generates an RSA key pair according to the Tor requirements.
Reference: https://github.com/torproject/torspec/blob/master/tor-spec.txt#L77-L80
For a public-key cipher, we use RSA with 1024-bit keys and a fixed exponent of 65537. We use OAEP-MGF1 padding, with SHA-1 as its digest function. We leave the optional "Label" parameter unset. (For OAEP padding, see ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-1/pkcs-1v2-1.pdf)
func GenerateRSAWithBits ¶
func GenerateRSAWithBits(bits int) (*rsa.PrivateKey, error)
GenerateRSAWithBits generates an RSA private key of the given size.
func HashWrite ¶
HashWrite provides a convenience for writing to a hash without tripping error checking linters. The hash.Hash interface satisfies io.Writer but promises to never return an error.
func HybridDecrypt ¶
func HybridDecrypt(pk *rsa.PrivateKey, z []byte) ([]byte, error)
HybridDecrypt decrypts ciphertext z with private key pk accoriding to "legacy hybrid encryption".
func LoadRSAPrivateKeyFromPEMFile ¶
func LoadRSAPrivateKeyFromPEMFile(filename string) (*rsa.PrivateKey, error)
func MarshalRSAPrivateKeyPKCS1DER ¶
func MarshalRSAPrivateKeyPKCS1DER(k *rsa.PrivateKey) []byte
MarshalRSAPrivateKeyPKCS1DER encodes k as PKCS#1 DER.
func MarshalRSAPrivateKeyPKCS1PEM ¶
func MarshalRSAPrivateKeyPKCS1PEM(k *rsa.PrivateKey) []byte
MarshalRSAPrivateKeyPKCS1PEM encodes k as PKCS#1 PEM.
func MarshalRSAPublicKeyPKCS1DER ¶
MarshalRSAPublicKeyPKCS1DER encodes k as PKCS#1 DER.
func MarshalRSAPublicKeyPKCS1PEM ¶
MarshalRSAPublicKeyPKCS1PEM encodes k as PKCS#1 PEM.
func MustRSAPrivateKey ¶
func MustRSAPrivateKey(k *rsa.PrivateKey, err error) *rsa.PrivateKey
func NewStream ¶
NewStream constructs a new stream cipher.
For a stream cipher, unless otherwise specified, we use 128-bit AES in counter mode, with an IV of all 0 bytes. (We also require AES256.)
func ParseRSAPrivateKeyPKCS1DER ¶
func ParseRSAPrivateKeyPKCS1DER(b []byte) (*rsa.PrivateKey, error)
ParseRSAPrivateKeyPKCS1DER decodes PKCS#1 DER encoded private key.
func ParseRSAPrivateKeyPKCS1PEM ¶
func ParseRSAPrivateKeyPKCS1PEM(b []byte) (*rsa.PrivateKey, error)
ParseRSAPrivateKeyPKCS1PEM decodes PKCS#1 PEM encoded private key.
func ParseRSAPublicKeyPKCS1DER ¶
ParseRSAPublicKeyPKCS1DER decodes PKCS#1 DER encoded public key.
func ParseRSAPublicKeyPKCS1PEM ¶
ParseRSAPublicKeyPKCS1PEM decodes PKCS#1 PEM encoded public key.
func RSAPrivateKeySize ¶
func RSAPrivateKeySize(k *rsa.PrivateKey) int
RSAPrivateKeySize returns the modulus size of an RSA key. This is provided for convenience only: it is essentially the same as RSAPublicKeySize.
func RSAPublicKeySize ¶
RSAPublicKeySize returns the modulus size of an RSA key.
func RSAPublicKeysEqual ¶
RSAPublicKeysEqual returns whether two RSA public keys are equal.
func SaveCurve25519KeyPairPrivateKeyToFile ¶
func SaveCurve25519KeyPairPrivateKeyToFile(k *Curve25519KeyPair, filename, label string) error
func SaveRSAPrivateKeyToPEMFile ¶
func SaveRSAPrivateKeyToPEMFile(k *rsa.PrivateKey, filename string) error
func SetPrivateKeyPermissions ¶
SetPrivateKeyPermissions sets permissions on a private key file.
func SignRSASHA1 ¶
func SignRSASHA1(data []byte, k *rsa.PrivateKey) ([]byte, error)
SignRSASHA1 signs data with k. This is the RSA encryption of the SHA-1 hash of data, with PKCS#1 v1.5 padding.
func SignRSASHA256 ¶
func SignRSASHA256(data []byte, k *rsa.PrivateKey) ([]byte, error)
SignRSASHA256 signs data with k. This is the RSA encryption of the SHA-256 hash of data, with PKCS#1 v1.5 padding.
func VerifyRSASHA1 ¶
VerifyRSASHA1 verifies an RSA signature based on SHA1 hash, as produced by SignRSASHA1.
Types ¶
type Curve25519KeyPair ¶
Curve25519KeyPair represents a public/private curve25519 keys.
curve25519 keys are used in the ntor handshake.
Reference: https://github.com/torproject/torspec/blob/master/tor-spec.txt#L82
For the "ntor" handshake, we also use the Curve25519 elliptic curve group.
Reference: https://github.com/torproject/torspec/blob/master/tor-spec.txt#L157-L163
This is Curve25519 key: - A medium-term ntor "Onion key" used to handle onion key handshakes when accepting incoming circuit extend requests. As with TAP onion keys, old ntor keys MUST be accepted for at least one week after they are no longer advertised. Because of this, relays MUST retain old keys for a while after they're rotated.
func GenerateCurve25519KeyPair ¶
func GenerateCurve25519KeyPair() (*Curve25519KeyPair, error)
GenerateCurve25519KeyPair generates a Curve25519KeyPair using crypto/rand as the random source.
func LoadCurve25519KeyPairPrivateKeyFromFile ¶
func LoadCurve25519KeyPairPrivateKeyFromFile(filename, label string) (*Curve25519KeyPair, error)
type DiffieHellmanKey ¶
type DiffieHellmanKey struct { Private [DiffieHellmanSecretSize]byte Public [DiffieHellmanPublicSize]byte }
DiffieHellmanKey is a public/private Diffie-Hellman key pair.
func GenerateDiffieHellmanKey ¶
func GenerateDiffieHellmanKey() (*DiffieHellmanKey, error)
GenerateDiffieHellmanKey generates a Diffie-Hellman key pair..
func (*DiffieHellmanKey) ComputeSharedSecret ¶
func (k *DiffieHellmanKey) ComputeSharedSecret(pub []byte) ([]byte, error)
ComputeSharedSecret computes the shared secret given their public key.