tordam

A library for peer discovery inside the Tor network
git clone https://git.parazyd.org/tordam
Log | Files | Refs | README | LICENSE

commit 08198377ac6a567908f0c4de213d32b02743533a
parent 48b7018b6eedd9592657a0384f4f3a8d270300db
Author: parazyd <parazyd@dyne.org>
Date:   Sat,  9 Dec 2017 20:07:54 +0100

Be explicit in naming of crypto functions

Diffstat:
Mcmd/dam-client/main.go | 16++++++++--------
Mcmd/dam-dir/main.go | 4++--
Mpkg/lib/crypto.go | 42+++++++++++++++++++++---------------------
Mpkg/lib/helpers.go | 4++--
4 files changed, 33 insertions(+), 33 deletions(-)

diff --git a/cmd/dam-client/main.go b/cmd/dam-client/main.go @@ -19,8 +19,8 @@ import ( // Cwd holds the path to the directory where we will Chdir on startup. var Cwd = os.Getenv("HOME") + "/.dam" -// Bits hold the size of our RSA private key. Tor standard is 1024. -const Bits = 1024 +// RsaBits holds the size of our RSA private key. Tor standard is 1024. +const RsaBits = 1024 // Privpath holds the name of where our private key is. const Privpath = "dam-private.key" @@ -69,7 +69,7 @@ func announce(dir string, vals map[string]string, privkey *rsa.PrivateKey) (bool return false, err } - decrypted, err := lib.DecryptMsg([]byte(decodedSecret), privkey) + decrypted, err := lib.DecryptMsgRsa([]byte(decodedSecret), privkey) if err != nil { return false, err } @@ -116,9 +116,9 @@ func main() { lib.CheckError(err) if _, err := os.Stat(Privpath); os.IsNotExist(err) { - key, err := lib.GenRsa(Bits) + key, err := lib.GenRsa(RsaBits) lib.CheckError(err) - _, err = lib.SavePriv(Privpath, key) + _, err = lib.SavePrivRsa(Privpath, key) lib.CheckError(err) } @@ -154,14 +154,14 @@ func main() { } } - key, err := lib.LoadKeyFromFile(Privpath) + key, err := lib.LoadRsaKeyFromFile(Privpath) lib.CheckError(err) - sig, err := lib.SignMsg([]byte(Postmsg), key) + sig, err := lib.SignMsgRsa([]byte(Postmsg), key) lib.CheckError(err) encodedSig := base64.StdEncoding.EncodeToString(sig) - onionAddr, err := lib.OnionFromPubkey(key.PublicKey) + onionAddr, err := lib.OnionFromPubkeyRsa(key.PublicKey) lib.CheckError(err) nodevals := map[string]string{ diff --git a/cmd/dam-dir/main.go b/cmd/dam-dir/main.go @@ -100,7 +100,7 @@ func handlePost(rw http.ResponseWriter, request *http.Request) { return } - pubkey, err := lib.ParsePubkey(pkey) + pubkey, err := lib.ParsePubkeyRsa(pkey) lib.CheckError(err) n.Pubkey = string(pkey) @@ -113,7 +113,7 @@ func handlePost(rw http.ResponseWriter, request *http.Request) { randString, err := lib.GenRandomASCII(64) lib.CheckError(err) - secret, err := lib.EncryptMsg([]byte(randString), pubkey) + secret, err := lib.EncryptMsgRsa([]byte(randString), pubkey) lib.CheckError(err) encodedSecret := base64.StdEncoding.EncodeToString(secret) diff --git a/pkg/lib/crypto.go b/pkg/lib/crypto.go @@ -31,11 +31,11 @@ func GenRsa(bitSize int) (*rsa.PrivateKey, error) { return key, nil } -// SavePub saves a given RSA public key to a given filename. -// SavePub takes the filename to write as a string, and the key as +// SavePubRsa saves a given RSA public key to a given filename. +// SavePubRsa takes the filename to write as a string, and the key as // rsa.PublicKey. It returns a boolean value and an error, depending on whether // it has failed or not. -func SavePub(filename string, pubkey rsa.PublicKey) (bool, error) { +func SavePubRsa(filename string, pubkey rsa.PublicKey) (bool, error) { log.Printf("Writing pubkey to %s\n", filename) // FIXME: worry or not about creating the path if it doesn't exist? outfile, err := os.Create(filename) @@ -62,11 +62,11 @@ func SavePub(filename string, pubkey rsa.PublicKey) (bool, error) { return true, nil } -// SavePriv saves a given RSA private key to a given filename. -// SavePriv takes the filename to write as a string, and the key as +// SavePrivRsa saves a given RSA private key to a given filename. +// SavePrivRsa takes the filename to write as a string, and the key as // *rsa.PrivateKey. It returns a boolean value and an error, depending on whether // it has failed or not. -func SavePriv(filename string, privkey *rsa.PrivateKey) (bool, error) { +func SavePrivRsa(filename string, privkey *rsa.PrivateKey) (bool, error) { log.Printf("Writing private key to %s\n", filename) // FIXME: worry or not about creating the path if it doesn't exist? outfile, err := os.Create(filename) @@ -89,11 +89,11 @@ func SavePriv(filename string, privkey *rsa.PrivateKey) (bool, error) { return true, nil } -// LoadKeyFromFile loads a RSA private key from a given filename. -// LoadKeyFromFile takes a string filename and tries to read from it, parsing +// LoadRsaKeyFromFile loads a RSA private key from a given filename. +// LoadRsaKeyFromFile takes a string filename and tries to read from it, parsing // the private RSA key. It will return a *rsa.PrivateKey on success, and error // on fail. -func LoadKeyFromFile(filename string) (*rsa.PrivateKey, error) { +func LoadRsaKeyFromFile(filename string) (*rsa.PrivateKey, error) { log.Println("Loading RSA private key from", filename) dat, err := ioutil.ReadFile(filename) if err != nil { @@ -110,10 +110,10 @@ func LoadKeyFromFile(filename string) (*rsa.PrivateKey, error) { return priv, nil } -// SignMsg signs a given []byte message using a given RSA private key. +// SignMsgRsa signs a given []byte message using a given RSA private key. // It will return the signature as a slice of bytes on success, and error on // failure. -func SignMsg(message []byte, privkey *rsa.PrivateKey) ([]byte, error) { +func SignMsgRsa(message []byte, privkey *rsa.PrivateKey) ([]byte, error) { log.Println("Signing message...") rng := rand.Reader hashed := sha512.Sum512(message) @@ -124,10 +124,10 @@ func SignMsg(message []byte, privkey *rsa.PrivateKey) ([]byte, error) { return sig, nil } -// EncryptMsg encrypts a given []byte message using a given RSA public key. +// EncryptMsgRsa encrypts a given []byte message using a given RSA public key. // Returns the encrypted message as a slice of bytes on success, and error on // failure. -func EncryptMsg(message []byte, pubkey *rsa.PublicKey) ([]byte, error) { +func EncryptMsgRsa(message []byte, pubkey *rsa.PublicKey) ([]byte, error) { log.Println("Encrypting message...") rng := rand.Reader msg, err := rsa.EncryptPKCS1v15(rng, pubkey, message) @@ -137,10 +137,10 @@ func EncryptMsg(message []byte, pubkey *rsa.PublicKey) ([]byte, error) { return msg, nil } -// DecryptMsg decrypts a given []byte message using a given RSA private key. +// DecryptMsgRsa decrypts a given []byte message using a given RSA private key. // Returns the decrypted message as a slice of bytes on success, and error on // failure. -func DecryptMsg(message []byte, privkey *rsa.PrivateKey) ([]byte, error) { +func DecryptMsgRsa(message []byte, privkey *rsa.PrivateKey) ([]byte, error) { log.Println("Decrypting message...") rng := rand.Reader msg, err := rsa.DecryptPKCS1v15(rng, privkey, message) @@ -150,9 +150,9 @@ func DecryptMsg(message []byte, privkey *rsa.PrivateKey) ([]byte, error) { return msg, nil } -// VerifyMsg verifies a message and signature against a given RSA pubkey. +// VerifyMsgRsa verifies a message and signature against a given RSA pubkey. // Returns a boolean value and error depending on whether it has failed or not. -func VerifyMsg(message []byte, signature []byte, pubkey *rsa.PublicKey) (bool, error) { +func VerifyMsgRsa(message []byte, signature []byte, pubkey *rsa.PublicKey) (bool, error) { log.Println("Verifying message signature") hashed := sha512.Sum512(message) err := rsa.VerifyPKCS1v15(pubkey, crypto.SHA512, hashed[:], signature) @@ -163,10 +163,10 @@ func VerifyMsg(message []byte, signature []byte, pubkey *rsa.PublicKey) (bool, e return true, nil } -// OnionFromPubkey generates a valid onion address from a given RSA pubkey. +// OnionFromPubkeyRsa generates a valid onion address from a given RSA pubkey. // Returns the onion address as a slice of bytes on success and error on // failure. -func OnionFromPubkey(pubkey rsa.PublicKey) ([]byte, error) { +func OnionFromPubkeyRsa(pubkey rsa.PublicKey) ([]byte, error) { asn1Bytes, err := asn1.Marshal(pubkey) if err != nil { return nil, err @@ -182,9 +182,9 @@ func OnionFromPubkey(pubkey rsa.PublicKey) ([]byte, error) { return []byte(encoded), nil } -// ParsePubkey parses a []byte form of a RSA public key and returns it as +// ParsePubkeyRsa parses a []byte form of a RSA public key and returns it as // *rsa.PublicKey on success. Otherwise, error. -func ParsePubkey(pubkey []byte) (*rsa.PublicKey, error) { +func ParsePubkeyRsa(pubkey []byte) (*rsa.PublicKey, error) { var pub rsa.PublicKey var ret *rsa.PublicKey block, _ := pem.Decode(pubkey) diff --git a/pkg/lib/helpers.go b/pkg/lib/helpers.go @@ -102,10 +102,10 @@ func ValidateReq(req map[string]string, pubkey string) ([]byte, bool) { // Validate signature. msg := []byte(req["message"]) sig := []byte(req["signature"]) - pub, err := ParsePubkey([]byte(pubkey)) + pub, err := ParsePubkeyRsa([]byte(pubkey)) CheckError(err) - val, err := VerifyMsg(msg, sig, pub) + val, err := VerifyMsgRsa(msg, sig, pub) CheckError(err) if val != true { return nil, false