Skip to content
This repository has been archived by the owner on Jul 21, 2021. It is now read-only.

Commit

Permalink
ked (encryption/decryption) - internal module
Browse files Browse the repository at this point in the history
  • Loading branch information
rinor committed Oct 13, 2020
1 parent b8fe45c commit 7126ca1
Showing 1 changed file with 109 additions and 0 deletions.
109 changes: 109 additions & 0 deletions internal/ked/ked.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package ked

import (
"crypto/rand"
"crypto/sha512"

"golang.org/x/crypto/chacha20poly1305"
"golang.org/x/crypto/pbkdf2"
)

/*
----------------------------------------------------------
| 0x01 | SALT(16) | NONCE(12) | Encrypted Data | Tag(16) |
----------------------------------------------------------
*/

const (
PROTO_SIZE = 1
SALT_SIZE = 16
NONCE_SIZE = 12 // chacha20poly1305.NonceSize
TAG_SIZE = 16 // poly1305.TagSize
)

var (
PASSWORD_DERIVATION_ITERATIONS = 12_983
PROTO_VERSION = byte(0b0000_0001)
)

// Encrypt ...
func Encrypt(password []byte, data []byte) ([]byte, error) {
salt, err := generate_salt()
if err != nil {
return nil, err
}
nonce, err := generate_nonce()
if err != nil {
return nil, err
}

aead, err := chacha20poly1305.New(
passToKey(password, salt),
)
if err != nil {
return nil, err
}

dataLen := len(data)
encFull := make([]byte, 0, PROTO_SIZE+SALT_SIZE+NONCE_SIZE+dataLen+TAG_SIZE)

encFull = append(encFull, PROTO_VERSION) // | 0x01 |
encFull = append(encFull, salt...) // | 0x01 | SALT(16) |
encFull = append(encFull, nonce...) // | 0x01 | SALT(16) | NONCE(12) |

return aead.Seal(
encFull, // | 0x01 | SALT(16) | NONCE(12) | Encrypted Data | Tag(16) |
nonce,
data,
nil,
), nil
}

// Decrypt ...
func Decrypt(password []byte, data []byte) ([]byte, error) {
var decData []byte

aead, err := chacha20poly1305.New(
passToKey(password, data[PROTO_SIZE:PROTO_SIZE+SALT_SIZE]),
)
if err != nil {
return nil, err
}

return aead.Open(
decData, // dec data
data[PROTO_SIZE+SALT_SIZE:PROTO_SIZE+SALT_SIZE+NONCE_SIZE], // nonce
data[PROTO_SIZE+SALT_SIZE+NONCE_SIZE:], // cipher (enc data + tag)
nil, // aad
)
}

func passToKey(password []byte, salt []byte) []byte {
return pbkdf2.Key(
password,
salt,
PASSWORD_DERIVATION_ITERATIONS,
chacha20poly1305.KeySize,
sha512.New,
)
}

func generate_salt() ([]byte, error) {
salt := make([]byte, SALT_SIZE)
err := randBytes(&salt)

return salt, err
}

func generate_nonce() ([]byte, error) {
nonce := make([]byte, NONCE_SIZE)
err := randBytes(&nonce)

return nonce, err
}

func randBytes(d *[]byte) error {
_, err := rand.Read(*d)

return err
}

0 comments on commit 7126ca1

Please sign in to comment.