Documentation
¶
Overview ¶
Package hcrypto implements the Hemmelig encryption scheme.
The scheme matches src/lib/crypto.ts in the web app:
- PBKDF2 with SHA-256 and 1,300,000 iterations derives a 256-bit key from the secret key (or the password) and the salt.
- AES-256-GCM encrypts the data with a random 96-bit IV.
- The encrypted message is the IV followed by the ciphertext and the tag.
- The password verifier is the hex SHA-256 digest of the derived key.
Index ¶
- Constants
- Variables
- func DecryptInboxMessage(private *ecdh.PrivateKey, envelope *InboxEnvelope) ([]byte, error)
- func DeriveKey(secret, salt string) []byte
- func GenerateKey() (string, error)
- func GenerateSalt() (string, error)
- func HexToBytes(value string) ([]byte, bool)
- func IsV2(data []byte) bool
- func ParseInboxPublicKey(value string) (*ecdh.PublicKey, error)
- func PasswordVerifier(password, salt string) string
- func RandomString(n int) (string, error)
- func VerifierFromKey(derived []byte) string
- type Cipher
- func (c *Cipher) Decrypt(message []byte) ([]byte, error)
- func (c *Cipher) DecryptFile(data []byte) ([]byte, error)
- func (c *Cipher) DecryptString(message []byte) (string, error)
- func (c *Cipher) Encrypt(plain []byte) ([]byte, error)
- func (c *Cipher) EncryptFile(plain []byte) ([]byte, error)
- func (c *Cipher) EncryptString(text string) ([]byte, error)
- func (c *Cipher) Verifier() string
- type InboxEnvelope
Constants ¶
const ( // Iterations is the PBKDF2 iteration count of the web app. Iterations = 1300000 // KeyLength is the length of a generated key and of a generated salt. KeyLength = 32 )
const ( // FileFormatV1 is one AES-GCM message: the IV, the ciphertext and the tag. FileFormatV1 = "v1" // FileFormatV2 is the chunked format of the web app. Each chunk has its // own IV and tag, and its index and a final flag in the additional data. FileFormatV2 = "v2" )
File formats of attachments. The server stores the format next to the file.
const (
// DefaultChunkSize is the plaintext size of one v2 chunk, as in the web app.
DefaultChunkSize = 4 * 1024 * 1024
)
Variables ¶
var ErrDecrypt = errors.New("could not decrypt: the key or password is wrong, or the data is damaged")
ErrDecrypt means that the key, the password or the data is wrong.
var ErrFileFormat = errors.New("the file is not in a known Hemmelig format")
ErrFileFormat means that an attachment is not in a known format.
Functions ¶
func DecryptInboxMessage ¶
func DecryptInboxMessage(private *ecdh.PrivateKey, envelope *InboxEnvelope) ([]byte, error)
DecryptInboxMessage decrypts an envelope with the inbox private key. The CLI does not hold inbox keys. Tests use this function to check EncryptForInbox.
func GenerateKey ¶
GenerateKey returns a new random 32-character secret key.
func GenerateSalt ¶
GenerateSalt returns a new random 32-character salt.
func HexToBytes ¶
HexToBytes decodes a hex string. It returns false for empty or invalid input, like hexToBytes in the web app.
func ParseInboxPublicKey ¶
ParseInboxPublicKey reads an inbox public key: a P-256 key in SPKI form, standard base64.
func PasswordVerifier ¶
PasswordVerifier returns the value that the server stores for a password-protected secret. The server never sees the password or the key.
func RandomString ¶
RandomString returns n random characters from the nanoid alphabet.
func VerifierFromKey ¶
VerifierFromKey returns the password verifier for an already derived key.
Types ¶
type Cipher ¶
type Cipher struct {
// contains filtered or unexported fields
}
Cipher encrypts and decrypts with one derived key. Derive the key once per secret, because PBKDF2 with this iteration count is slow on purpose.
func NewCipherFromKey ¶
NewCipherFromKey returns a Cipher for a derived 32-byte key.
func (*Cipher) DecryptFile ¶
DecryptFile decrypts an attachment in the v1 or the v2 format.
func (*Cipher) DecryptString ¶
DecryptString decrypts to UTF-8 text.
func (*Cipher) EncryptFile ¶
EncryptFile encrypts a file in the v2 chunked format of the web app.
func (*Cipher) EncryptString ¶
EncryptString encrypts UTF-8 text.
type InboxEnvelope ¶
type InboxEnvelope struct {
// EphemeralPublicKey is the one-time public key of the sender in SPKI form.
EphemeralPublicKey string `json:"ephemeralPublicKey"`
HKDFSalt string `json:"hkdfSalt"`
IV string `json:"iv"`
Ciphertext string `json:"ciphertext"`
}
InboxEnvelope is an encrypted inbox message. All values are standard base64.
func EncryptForInbox ¶
func EncryptForInbox(recipient *ecdh.PublicKey, plain []byte) (*InboxEnvelope, error)
EncryptForInbox encrypts a message for the inbox with this public key.