This repository has been archived on 2021-07-08. You can view files and clone it, but cannot push or open issues or pull requests.
opensend/keyCrypto.go

95 lines
2.5 KiB
Go
Raw Normal View History

/*
Copyright © 2021 Arsen Musayelyan
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
2020-12-03 10:12:43 +00:00
package main
import (
"crypto/rand"
"crypto/rsa"
"crypto/sha256"
2021-06-19 07:01:31 +00:00
"io/ioutil"
"net/http"
2020-12-03 10:12:43 +00:00
"github.com/rs/zerolog/log"
)
// Generate RSA keypair
func GenerateRSAKeypair() (*rsa.PrivateKey, *rsa.PublicKey) {
// Use ConsoleWriter logger
// Generate private/public RSA keypair
privateKey, err := rsa.GenerateKey(rand.Reader, 2048)
2020-12-21 07:18:42 +00:00
if err != nil {
log.Fatal().Err(err).Msg("Error generating RSA keypair")
}
2020-12-03 10:12:43 +00:00
// Get public key
publicKey := privateKey.PublicKey
// Return keypair
return privateKey, &publicKey
}
// Get public key from sender
2021-06-19 07:01:31 +00:00
func GetKey(sender *Sender) []byte {
2020-12-03 10:12:43 +00:00
// Use ConsoleWriter logger
// Send key request to connection
2021-06-19 07:01:31 +00:00
keyReader, code, err := sender.Get("/key")
2020-12-21 07:18:42 +00:00
if err != nil {
log.Fatal().Err(err).Msg("Error sending key request")
}
// If ok code returned
2021-06-19 07:01:31 +00:00
if code == http.StatusOK {
// Read received bytes into key
key, err := ioutil.ReadAll(keyReader)
2020-12-21 07:18:42 +00:00
if err != nil {
log.Fatal().Err(err).Msg("Error reading key")
}
2020-12-03 10:12:43 +00:00
// Return key
return key
2020-12-21 07:18:42 +00:00
// Otherwise
2020-12-03 10:12:43 +00:00
} else {
// Fatally log
2020-12-21 07:18:42 +00:00
if err != nil {
log.Fatal().Msg("Server reported error")
}
2020-12-03 10:12:43 +00:00
}
// Return nil if all else fails
return nil
}
// Encrypt shared key with received public key
func EncryptKey(sharedKey string, recvPubKey *rsa.PublicKey) []byte {
// Use ConsoleWriter logger
// Encrypt shared key using RSA
encryptedSharedKey, err := rsa.EncryptOAEP(sha256.New(), rand.Reader, recvPubKey, []byte(sharedKey), nil)
2020-12-21 07:18:42 +00:00
if err != nil {
log.Fatal().Err(err).Msg("Error encrypting shared key")
}
2020-12-03 10:12:43 +00:00
// Return encrypted key
return encryptedSharedKey
}
// Decrypt shared key using private RSA key
func DecryptKey(encryptedKey []byte, privateKey *rsa.PrivateKey) string {
// Decrypt shared key using RSA
decryptedKey, err := rsa.DecryptOAEP(sha256.New(), rand.Reader, privateKey, encryptedKey, nil)
2020-12-21 07:18:42 +00:00
if err != nil {
log.Fatal().Err(err).Msg("Error decrypting shared key")
}
2020-12-03 10:12:43 +00:00
// Get string of decrypted key
sharedKey := string(decryptedKey)
// Return shared key
return sharedKey
2020-12-21 07:18:42 +00:00
}