Documentation ¶
Overview ¶
Package sodium is a wrapper for https://github.com/jedisct1/libsodium
Most of the functions is a method to the "Bytes" type. They are grouped below:
Signature ¶
Sender sign a message with its SecretKey and the receiver can verify the Signature by sender's PublicKey
type SignKP struct { PublicKey SignPublicKey SecretKey SignSecretKey } func MakeSignKP() SignKP func SeedSignKP(seed SignSeed) SignKP func (k SignSecretKey) PublicKey() SignPublicKey func (k SignSecretKey) Seed() SignSeed //SignKP can be converted to BoxKP //It is recommended to use separate keys for signing and encrytion. func (p SignKP) ToBox() BoxKP func (k SignSecretKey) ToBox() BoxSecretKey func (k SignPublicKey) ToBox() BoxPublicKey //Message + Signature func (b Bytes) Sign(key SignSecretKey) (sm Bytes) func (b Bytes) SignOpen(key SignPublicKey) (m Bytes, err error) //Detached Signature func (b Bytes) SignDetached(key SignSecretKey) (sig Signature) func (b Bytes) SignVerifyDetached(sig Signature, key SignPublicKey) (err error)
(Ed25519)
//for multi-part messages that can't fit in memory func MakeSignState() SignState func (s SignState) Update(b []byte) func (s SignState) Sign(key SignSecretKey) Signature func (s SignState) Verify(sig Signature, key SignPublicKey) (err error)
(Ed25519ph)
Anonymous Public Key Encryption ¶
An anonymous can encrypt a message with an ephemeral key pair and reveiver's PublicKey. The receiver can decrypt the message with its SecretKey. Only the receiver is authenticated.
type BoxKP struct { PublicKey BoxPublicKey SecretKey BoxSecretKey } func MakeBoxKP() BoxKP func SeedBoxKP(seed BoxSeed) BoxKP func (b Bytes) SealedBox(pk BoxPublicKey) (cm Bytes) func (b Bytes) SealedBoxOpen(kp BoxKP) (m Bytes, err error)
(X25519-XSalsa20-Poly1305)
Authenticated Public Key Encryption ¶
Authenticated Box can be used to pass encrypt message from a known sender to a known receiver. The sender and the receiver are both authenticated to each other.
A one-time shared nonce is also generated and passed to protect the key pairs and messages.
type BoxKP struct { PublicKey BoxPublicKey SecretKey BoxSecretKey } func MakeBoxKP() BoxKP func SeedBoxKP(seed BoxSeed) BoxKP func (b *BoxNonce) Next() //All-in-one box func (b Bytes) Box(n BoxNonce, pk BoxPublicKey, sk BoxSecretKey) (c Bytes) func (b Bytes) BoxOpen(n BoxNonce, pk BoxPublicKey, sk BoxSecretKey) (m Bytes, err error) //Detached MAC func (b Bytes) BoxDetached(n BoxNonce, pk BoxPublicKey, sk BoxSecretKey) (mac BoxMAC, c Bytes) func (b Bytes) BoxOpenDetached(mac BoxMAC, n BoxNonce, pk BoxPublicKey, sk BoxSecretKey) (c Bytes, err error)
(X25519-XSalsa20-Poly1305)
Key Exchanging ¶
Server and Client exchange their public key and calculates a common session key with their own secret key.
type KXKP struct { PublicKey KXPublicKey SecretKey KXSecretKey } func MakeKXKP() KXKP func SeedKXKP(seed KXSeed) KXKP type KXSessionKeys struct { Rx KXSessionKey Tx KXSessionKey } // session keys for client func (kp KXKP) ClientSessionKeys(server_pk KXPublicKey) (*KXSessionKeys, error) // session keys for server func (kp KXKP) ServerSessionKeys(client_pk KXPublicKey) (*KXSessionKeys, error) { // client's rx == server's tx // client's tx == server's rx
(rx || tx = BLAKE2B-512(p.n || client_pk || server_pk))
Secret Key Authentication ¶
One holder of a secret key authenticates the message with MAC.
//Holders of the key can generate a MAC for the message. func (b Bytes) Auth(key MACKey) (mac MAC) //Holders of the key can verify the message's authenticity. func (b Bytes) AuthVerify(mac MAC, key MACKey) (err error)
(HMAC-SHA512256)
Secret Key Encryption ¶
Use a secret key and a nonce to protect the key, messages could be encrypted into a SecretBox. The encrypted data's intergrity is checked when decryption.
func (n *SecretBoxNonce) Next() //encrypted message + MAC. func (b Bytes) SecretBox(n SecretBoxNonce, k SecretBoxKey) (c Bytes) func (b Bytes) SecretBoxOpen(n SecretBoxNonce, k SecretBoxKey) (m Bytes, err error) //Detached version has a separate MAC. func (b Bytes) SecretBoxDetached(n SecretBoxNonce, k SecretBoxKey) (c Bytes, mac SecretBoxMAC) func (b Bytes) SecretBoxOpenDetached(mac SecretBoxMAC, n SecretBoxNonce, k SecretBoxKey) (m Bytes, err error)
(XSalsa20-Poly1305)
Authenticated Encryption with Additional Data ¶
Use a secret key and a nonce to protect the key, messages could be encrypted. Optional additional data and the message is authenticited with an authentication tag. Both intergrity and authenticity is checked when decryption. The decryption would not be performed unless the authentication tag is verified.
func (n *AEADCPNonce) Next() //encrypted message + MAC. func (b Bytes) AEADCPEncrypt(ad Bytes, n AEADCPNonce, k AEADCPKey) (c Bytes) func (b Bytes) AEADCPDecrypt(ad Bytes, n AEADCPNonce, k AEADCPKey) (m Bytes, err error) func (b Bytes) AEADCPVerify(ad Bytes, n AEADCPNonce, k AEADCPKey) (err error) //Detached version has a separate MAC. func (b Bytes) AEADCPEncryptDetached(ad Bytes, n AEADCPNonce, k AEADCPKey) (c Bytes, mac AEADCPMAC) func (b Bytes) AEADCPDecryptDetached(mac AEADCPMAC, ad Bytes, n AEADCPNonce, k AEADCPKey) (m Bytes, err error) func (b Bytes) AEADCPVerifyDetached(mac AEADCPMAC, ad Bytes, n AEADCPNonce, k AEADCPKey) (err error)
AEADCP* (ChaCha20-Poly1305_IETF)
Key Derivation ¶
Deriving subkeys from a single high-entropy key
func MakeMasterKey() MasterKey func MakeKeyContext(s string) KeyContext func (m MasterKey) Derive(length int, id uint64, context KeyContext) SubKey
KDF (BLAKE2B)
Index ¶
- Variables
- func MemCmp(buff1, buff2 Bytes, length int) int
- func MemZero(buff1 Bytes)
- func Randomize(k Typed)
- type AEADCPKey
- type AEADCPMAC
- type AEADCPNonce
- type BoxKP
- type BoxMAC
- type BoxNonce
- type BoxPublicKey
- type BoxSecretKey
- type BoxSeed
- type Bytes
- func (b Bytes) AEADCPDecrypt(ad Bytes, n AEADCPNonce, k AEADCPKey) (m Bytes, err error)
- func (b Bytes) AEADCPDecryptDetached(mac AEADCPMAC, ad Bytes, n AEADCPNonce, k AEADCPKey) (m Bytes, err error)
- func (b Bytes) AEADCPEncrypt(ad Bytes, n AEADCPNonce, k AEADCPKey) (c Bytes)
- func (b Bytes) AEADCPEncryptDetached(ad Bytes, n AEADCPNonce, k AEADCPKey) (c Bytes, mac AEADCPMAC)
- func (b Bytes) AEADCPVerify(ad Bytes, n AEADCPNonce, k AEADCPKey) (err error)
- func (b Bytes) AEADCPVerifyDetached(mac AEADCPMAC, ad Bytes, n AEADCPNonce, k AEADCPKey) (err error)
- func (b Bytes) Auth(key MACKey) (mac MAC)
- func (b Bytes) AuthVerify(mac MAC, key MACKey) (err error)
- func (b Bytes) Box(n BoxNonce, pk BoxPublicKey, sk BoxSecretKey) (c Bytes)
- func (b Bytes) BoxDetached(n BoxNonce, pk BoxPublicKey, sk BoxSecretKey) (mac BoxMAC, c Bytes)
- func (b Bytes) BoxOpen(n BoxNonce, pk BoxPublicKey, sk BoxSecretKey) (m Bytes, err error)
- func (b Bytes) BoxOpenDetached(mac BoxMAC, n BoxNonce, pk BoxPublicKey, sk BoxSecretKey) (m Bytes, err error)
- func (b Bytes) Length() int
- func (b Bytes) SealedBox(pk BoxPublicKey) (cm Bytes)
- func (b Bytes) SealedBoxOpen(kp BoxKP) (m Bytes, err error)
- func (b Bytes) SecretBox(n SecretBoxNonce, k SecretBoxKey) (c Bytes)
- func (b Bytes) SecretBoxDetached(n SecretBoxNonce, k SecretBoxKey) (c Bytes, mac SecretBoxMAC)
- func (b Bytes) SecretBoxOpen(n SecretBoxNonce, k SecretBoxKey) (m Bytes, err error)
- func (b Bytes) SecretBoxOpenDetached(mac SecretBoxMAC, n SecretBoxNonce, k SecretBoxKey) (m Bytes, err error)
- func (b Bytes) Shorthash(key ShortHashKey) (out Bytes)
- func (b Bytes) Sign(key SignSecretKey) (sm Bytes)
- func (b Bytes) SignDetached(key SignSecretKey) (sig Signature)
- func (b Bytes) SignOpen(key SignPublicKey) (m Bytes, err error)
- func (b Bytes) SignVerifyDetached(sig Signature, key SignPublicKey) (err error)
- type GenericHash
- type GenericHashKey
- type KXKP
- type KXPublicKey
- type KXSecretKey
- type KXSeed
- type KXSessionKey
- type KXSessionKeys
- type KeyContext
- type MAC
- type MACKey
- type MasterKey
- type Nonce
- type PWHashSalt
- type PWHashStr
- type Scalar
- type ScalarMult
- type SecretBoxKey
- type SecretBoxMAC
- type SecretBoxNonce
- type ShortHash
- type ShortHashKey
- type SignKP
- type SignPublicKey
- type SignSecretKey
- type SignSeed
- type SignState
- type Signature
- type SubKey
- type Typed
Examples ¶
- BoxSecretKey.PublicKey
- Bytes.AEADCPEncrypt
- Bytes.AEADCPEncryptDetached
- Bytes.AEADCPVerify
- Bytes.AEADCPVerifyDetached
- Bytes.Auth
- Bytes.Box
- Bytes.BoxDetached
- Bytes.SealedBox
- Bytes.SecretBox
- Bytes.SecretBoxDetached
- Bytes.Shorthash
- Bytes.SignDetached
- MakeKXKP
- MakeSignState
- MasterKey.Derive
- NewGenericHashKeyed
- PWHashStore
- SeedBoxKP
- SeedSignKP
- SignKP.ToBox
Constants ¶
This section is empty.
Variables ¶
var ( CryptoKDFBytesMin = int(C.crypto_kdf_bytes_min()) CryptoKDFBytesMax = int(C.crypto_kdf_bytes_max()) CryptoKDFContextBytes = int(C.crypto_kdf_contextbytes()) )
var ( CryptoPWHashOpsLimitInteractive = int(C.crypto_pwhash_opslimit_interactive()) CryptoPWHashMemLimitInteractive = int(C.crypto_pwhash_memlimit_interactive()) CryptoPWHashOpsLimitModerate = int(C.crypto_pwhash_opslimit_moderate()) CryptoPWHashMemLimitModerate = int(C.crypto_pwhash_memlimit_moderate()) CryptoPWHashOpsLimitSensitive = int(C.crypto_pwhash_opslimit_sensitive()) CryptoPWHashMemLimitSensitive = int(C.crypto_pwhash_memlimit_sensitive()) )
var ( RuntimeHasNeon = bool(C.sodium_runtime_has_neon() != 0) RuntimeHasSse2 = bool(C.sodium_runtime_has_sse2() != 0) RuntimeHasSse3 = bool(C.sodium_runtime_has_sse3() != 0) )
var ( ErrAuth = errors.New("sodium: Message forged") ErrOpenBox = errors.New("sodium: Can't open box") ErrOpenSign = errors.New("sodium: Signature forged") ErrDecryptAEAD = errors.New("sodium: Can't decrypt message") ErrPassword = errors.New("sodium: Password not matched") ErrInvalidKey = errors.New("sodium: Invalid key") )
Functions ¶
Types ¶
type AEADCPNonce ¶
type AEADCPNonce struct {
Bytes
}
func (*AEADCPNonce) Next ¶
func (n *AEADCPNonce) Next()
func (AEADCPNonce) Size ¶
func (AEADCPNonce) Size() int
type BoxKP ¶
type BoxKP struct { PublicKey BoxPublicKey SecretKey BoxSecretKey }
func SeedBoxKP ¶
SeedBoxKP generates a keypair for signing from a BoxSeed.
The same pair of keys will be generated with the same 'seed'
Example ¶
seed := BoxSeed{} Randomize(&seed) kp1 := SeedBoxKP(seed) kp2 := SeedBoxKP(seed) s1 := kp1.SecretKey s2 := kp2.SecretKey fmt.Println(MemCmp(s1.Bytes, s2.Bytes, s1.Length()) == 0)
Output: true
type BoxPublicKey ¶
type BoxPublicKey struct {
Bytes
}
func (BoxPublicKey) Size ¶
func (k BoxPublicKey) Size() int
type BoxSecretKey ¶
type BoxSecretKey struct {
Bytes
}
func (BoxSecretKey) PublicKey ¶
func (k BoxSecretKey) PublicKey() BoxPublicKey
PublicKey calculates public key from BoxSecretKey.
Example ¶
kp := MakeBoxKP() pk := kp.SecretKey.PublicKey() fmt.Println(MemCmp(kp.PublicKey.Bytes, pk.Bytes, pk.Length()) == 0)
Output: true
func (BoxSecretKey) Size ¶
func (k BoxSecretKey) Size() int
type Bytes ¶
type Bytes []byte
Bytes warppers around []byte.
func (Bytes) AEADCPDecrypt ¶
AEADCPDecrypt decrypts message with AEADCPKey, and AEADCPNonce. The appended authenticated tag is verified with additional data 'ad' before decryption.
It returns an error if decryption failed.
func (Bytes) AEADCPDecryptDetached ¶
func (b Bytes) AEADCPDecryptDetached(mac AEADCPMAC, ad Bytes, n AEADCPNonce, k AEADCPKey) (m Bytes, err error)
AEADCPDecryptDetached decrypts message with AEADCPKey, and AEADCPNonce. The separated authenticated tag is verified with additional data 'ad' before decryption.
It returns an error if decryption failed.
func (Bytes) AEADCPEncrypt ¶
func (b Bytes) AEADCPEncrypt(ad Bytes, n AEADCPNonce, k AEADCPKey) (c Bytes)
AEADCPEncrypt encrypts message with AEADCPKey, and AEADCPNonce. Message then authenticated with additional data 'ad'. Authentication tag is append to the encrypted data.
Example ¶
key := AEADCPKey{} Randomize(&key) n := AEADCPNonce{} Randomize(&n) ad := Bytes(`addtional data`) e := m.AEADCPEncrypt(ad, n, key) md, err := e.AEADCPDecrypt(ad, n, key) fmt.Println(err) fmt.Println(MemCmp(md, m, m.Length()) == 0)
Output: <nil> true
func (Bytes) AEADCPEncryptDetached ¶
AEADCPEncryptDetached encrypts message with AEADCPKey, and AEADCPNonce. Message then authenticated with additional data 'ad'. Authentication tag is separated saved as 'mac'.
Example ¶
key := AEADCPKey{} Randomize(&key) n := AEADCPNonce{} Randomize(&n) ad := Bytes(`addtional data`) e, mac := m.AEADCPEncryptDetached(ad, n, key) md, err := e.AEADCPDecryptDetached(mac, ad, n, key) fmt.Println(err) fmt.Println(MemCmp(md, m, m.Length()) == 0)
Output: <nil> true
func (Bytes) AEADCPVerify ¶
func (b Bytes) AEADCPVerify(ad Bytes, n AEADCPNonce, k AEADCPKey) (err error)
AEADCPVerify decrypts message with AEADCPKey, and AEADCPNonce without writing decrypted data. The appended authenticated tag is verified with additional data 'ad' before decryption.
It returns an error if decryption failed.
Example ¶
key := AEADCPKey{} Randomize(&key) n := AEADCPNonce{} Randomize(&n) ad := Bytes(`addtional data`) e := m.AEADCPEncrypt(ad, n, key) err := e.AEADCPVerify(ad, n, key) fmt.Println(err)
Output: <nil>
func (Bytes) AEADCPVerifyDetached ¶
func (b Bytes) AEADCPVerifyDetached(mac AEADCPMAC, ad Bytes, n AEADCPNonce, k AEADCPKey) (err error)
AEADCPVerifyDetached decrypts message with AEADCPKey, and AEADCPNonce without writing decrypted data. The separated authenticated tag is verified with additional data 'ad' before decryption.
It returns an error if decryption failed.
Example ¶
key := AEADCPKey{} Randomize(&key) n := AEADCPNonce{} Randomize(&n) ad := Bytes(`addtional data`) e, mac := m.AEADCPEncryptDetached(ad, n, key) err := e.AEADCPVerifyDetached(mac, ad, n, key) fmt.Println(err)
Output: <nil>
func (Bytes) Auth ¶
Auth generates a MAC for the message with the secret 'key'.
Example ¶
key := MACKey{} Randomize(&key) mac := m.Auth(key) err := m.AuthVerify(mac, key) fmt.Println(err)
Output: <nil>
func (Bytes) AuthVerify ¶
AuthVerify verifies a messagee with MAC and the secret 'key'.
It returns an error if verification failed.
func (Bytes) Box ¶
func (b Bytes) Box(n BoxNonce, pk BoxPublicKey, sk BoxSecretKey) (c Bytes)
Box puts message into an authenticated encrypted box using sender's SecretKey and receiver's PublicKey, with a shared one-time nonce is used for each message.
Example ¶
rkp := MakeBoxKP() skp := MakeBoxKP() n := BoxNonce{} Randomize(&n) bc := m.Box(n, rkp.PublicKey, skp.SecretKey) bom, err := bc.BoxOpen(n, skp.PublicKey, rkp.SecretKey) fmt.Println(err) fmt.Println(MemCmp(bom, m, m.Length()) == 0)
Output: <nil> true
func (Bytes) BoxDetached ¶
func (b Bytes) BoxDetached(n BoxNonce, pk BoxPublicKey, sk BoxSecretKey) (mac BoxMAC, c Bytes)
BoxDetached encodes message into an encrypted message using sender's SecretKey and receiver's PublicKey, with a shared one-time nonce is used for each message.
Detached MAC is return along with encrypted message for authentication.
Example ¶
rkp := MakeBoxKP() skp := MakeBoxKP() n := BoxNonce{} Randomize(&n) mac, bcd := m.BoxDetached(n, rkp.PublicKey, skp.SecretKey) bomd, err := bcd.BoxOpenDetached(mac, n, skp.PublicKey, rkp.SecretKey) fmt.Println(err) fmt.Println(MemCmp(bomd, m, m.Length()) == 0)
Output: <nil> true
func (Bytes) BoxOpen ¶
func (b Bytes) BoxOpen(n BoxNonce, pk BoxPublicKey, sk BoxSecretKey) (m Bytes, err error)
BoxOpen reads message from an authenticated encrypted box using receiver's SecretKey and sender's PublicKey with a shared one-time nonce
It returns an error if opening failed.
func (Bytes) BoxOpenDetached ¶
func (b Bytes) BoxOpenDetached(mac BoxMAC, n BoxNonce, pk BoxPublicKey, sk BoxSecretKey) (m Bytes, err error)
BoxOpenDetached decodes message from an encrypted message along with a MAC for authentication, and using receiver's SecretKey and sender's PublicKey with a shared one-time nonce.
It returns an error if opening failed.
func (Bytes) SealedBox ¶
func (b Bytes) SealedBox(pk BoxPublicKey) (cm Bytes)
SealedBox puts message into a sealed box using receiver's PublicKey and an ephemeral key pair of which the SecretKey is destroyed on sender's side right after encryption, and the PublicKey is packed with the Box to the receiver.
The receiver can open the box but can not verify the identity of the sender.
Example ¶
kp := MakeBoxKP() n := BoxNonce{} Randomize(&n) c := m.SealedBox(kp.PublicKey) om, err := c.SealedBoxOpen(kp) fmt.Println(err) fmt.Println(MemCmp(om, m, m.Length()) == 0)
Output: <nil> true
func (Bytes) SealedBoxOpen ¶
SealedBoxOpen reads message from a sealed box using its key pair and ephemeral public packed in the Box.
It returns an error if opening failed.
func (Bytes) SecretBox ¶
func (b Bytes) SecretBox(n SecretBoxNonce, k SecretBoxKey) (c Bytes)
SecretBox use a SecretBoxNonce and a SecretBoxKey to encrypt a message.
Example ¶
key := SecretBoxKey{} Randomize(&key) n := SecretBoxNonce{} Randomize(&n) c := m.SecretBox(n, key) md, err := c.SecretBoxOpen(n, key) fmt.Println(err) fmt.Println(MemCmp(md, m, m.Length()) == 0)
Output: <nil> true
func (Bytes) SecretBoxDetached ¶
func (b Bytes) SecretBoxDetached(n SecretBoxNonce, k SecretBoxKey) (c Bytes, mac SecretBoxMAC)
SecretBoxDetached use a SecretBoxNonce and a SecretBoxKey to encrypt a message. A separate MAC is returned.
Example ¶
key := SecretBoxKey{} Randomize(&key) n := SecretBoxNonce{} Randomize(&n) c, mac := m.SecretBoxDetached(n, key) md, err := c.SecretBoxOpenDetached(mac, n, key) fmt.Println(err) fmt.Println(MemCmp(md, m, m.Length()) == 0)
Output: <nil> true
func (Bytes) SecretBoxOpen ¶
func (b Bytes) SecretBoxOpen(n SecretBoxNonce, k SecretBoxKey) (m Bytes, err error)
SecretBoxOpen opens a SecretBox using SecretBoxKey and SecretBoxNonce.
It returns an error if opening failed.
func (Bytes) SecretBoxOpenDetached ¶
func (b Bytes) SecretBoxOpenDetached(mac SecretBoxMAC, n SecretBoxNonce, k SecretBoxKey) (m Bytes, err error)
SecretBoxOpenDetached opens a SecretBox using SecretBoxKey and SecretBoxNonce. with a separate MAC.
It returns an error if opening failed.
func (Bytes) Shorthash ¶
func (b Bytes) Shorthash(key ShortHashKey) (out Bytes)
Shorthash use a secret key and input to produce a ShortHash. It is protective to short input. And it's output is also too short to be collision-resistent, however it can be used in hash table, Bloom filter or generate MAC for interactive protocol.
Example ¶
key := ShortHashKey{} Randomize(&key) m := Bytes(`short message`) hash := m.Shorthash(key) fmt.Println(hash.Length())
Output: 8
func (Bytes) Sign ¶
func (b Bytes) Sign(key SignSecretKey) (sm Bytes)
Sign returns 'sm': signature+message
func (Bytes) SignDetached ¶
func (b Bytes) SignDetached(key SignSecretKey) (sig Signature)
SignDetached signs the message with 'key' and returns only the signature.
Example ¶
kp := MakeSignKP() sm := m.Sign(kp.SecretKey) om, err := sm.SignOpen(kp.PublicKey) sig := m.SignDetached(kp.SecretKey) fmt.Println(err) fmt.Println(MemCmp(om, m, m.Length()) == 0) fmt.Println(MemCmp(sig.Bytes, sm, sig.Length()) == 0) err = m.SignVerifyDetached(sig, kp.PublicKey) fmt.Println(err)
Output: <nil> true true <nil>
func (Bytes) SignOpen ¶
func (b Bytes) SignOpen(key SignPublicKey) (m Bytes, err error)
SignOpen returns message 'm' from signature+message, verified by 'key'.
It returns an error if verification failed.
func (Bytes) SignVerifyDetached ¶
func (b Bytes) SignVerifyDetached(sig Signature, key SignPublicKey) (err error)
SignVerifyDetached verifies the message and its detached 'sig' with 'key'.
It returns an error if verification failed.
type GenericHash ¶
type GenericHash struct {
// contains filtered or unexported fields
}
GenericHash provides a BLAKE2b (RFC7693) hash, in interface of hash.Hash.
The Hash's and key's size can be any between 16 bytes (128 bits) to 64 bytes (512 bits) based on different application.
func NewGenericHash ¶
func NewGenericHash(outlen int) GenericHash
Unkeyed version, output length should between 16 (128-bit) to 64 (512-bit).
func NewGenericHashDefault ¶
func NewGenericHashDefault() GenericHash
Unkeyed version with default output length.
func NewGenericHashDefaultKeyed ¶
func NewGenericHashDefaultKeyed(key GenericHashKey) GenericHash
Keyed version with default output length.
func NewGenericHashKeyed ¶
func NewGenericHashKeyed(outlen int, key GenericHashKey) GenericHash
Keyed version, output length in bytes should between 16 (128-bit) to 64 (512-bit).
Example ¶
kp := MakeBoxKP() key := GenericHashKey{kp.SecretKey.Bytes} h := NewGenericHashKeyed(48, key) h.Write(m) sh := h.Sum(nil) he := NewGenericHash(48) he.Write(m) she := he.Sum(nil) fmt.Println(len(sh)) fmt.Println(len(she)) fmt.Println((MemCmp(sh, she, len(sh))) == 0)
Output: 48 48 false
func (GenericHash) Size ¶
func (g GenericHash) Size() int
Output length in bytes.
Implements hash.Hash
func (*GenericHash) Sum ¶
func (g *GenericHash) Sum(b []byte) []byte
Return appended the Sum after b.
Implements hash.Hash.
NOTE: Repeated call is allowed. But can't call Write() after Sum(). The underlying state is freed. It MUST be Reset() to use it again after calling Sum(). This behaviour is inconsistent with the definition of hash.Hash.
type GenericHashKey ¶
type GenericHashKey struct {
Bytes
}
func (GenericHashKey) Size ¶
func (GenericHashKey) Size() int
type KXKP ¶
type KXKP struct { PublicKey KXPublicKey SecretKey KXSecretKey }
func MakeKXKP ¶
func MakeKXKP() KXKP
MakeKXKP generates a keypair for signing
Example ¶
skp := MakeKXKP() ckp := MakeKXKP() sss, _ := skp.ServerSessionKeys(ckp.PublicKey) css, _ := ckp.ClientSessionKeys(skp.PublicKey) fmt.Println(MemCmp(sss.Tx.Bytes, css.Rx.Bytes, sss.Tx.Size()) == 0) fmt.Println(MemCmp(sss.Rx.Bytes, css.Tx.Bytes, sss.Rx.Size()) == 0) fmt.Println(MemCmp(sss.Tx.Bytes, sss.Rx.Bytes, sss.Tx.Size()) == 0) fmt.Println(MemCmp(css.Tx.Bytes, css.Rx.Bytes, css.Tx.Size()) == 0)
Output: true true false false
func SeedKXKP ¶
SeedKXKP generates a keypair for exchanging from a KXSeed.
The same pair of keys will be generated with the same 'seed'
func (KXKP) ClientSessionKeys ¶
func (kp KXKP) ClientSessionKeys(server_pk KXPublicKey) (*KXSessionKeys, error)
ClientSessionKeys calculates Rx (for receving) and Tx (for sending) session keys with server's public key. return error when server_pk is not acceptable.
func (KXKP) ServerSessionKeys ¶
func (kp KXKP) ServerSessionKeys(client_pk KXPublicKey) (*KXSessionKeys, error)
ServerSessionKeys calculates Rx (for receving) and Tx (for sending) session keys with client's public key. return error when client_pk is not acceptable.
type KXPublicKey ¶
type KXPublicKey struct {
Bytes
}
func (KXPublicKey) Size ¶
func (k KXPublicKey) Size() int
type KXSecretKey ¶
type KXSecretKey struct {
Bytes
}
func (KXSecretKey) Size ¶
func (k KXSecretKey) Size() int
type KXSessionKey ¶
type KXSessionKey struct {
Bytes
}
func (KXSessionKey) Size ¶
func (k KXSessionKey) Size() int
type KXSessionKeys ¶
type KXSessionKeys struct { Rx KXSessionKey Tx KXSessionKey }
type KeyContext ¶
type KeyContext string
KeyContext is a CryptoKDFContextBytes length string indicating the context for the key. e.g. "username"
func MakeKeyContext ¶
func MakeKeyContext(s string) KeyContext
func (KeyContext) Length ¶
func (k KeyContext) Length() int
func (KeyContext) Size ¶
func (KeyContext) Size() int
type MAC ¶
type MAC struct {
Bytes
}
MAC stores Message Authentication Code produced by HMAC-SHA512256.
type MasterKey ¶
type MasterKey struct {
Bytes
}
MasterKey for deriving SubKeys
func (MasterKey) Derive ¶
func (m MasterKey) Derive(length int, id uint64, context KeyContext) SubKey
Derive SubKey from the MasterKey length should be between CryptoKDFBytesMin and CryptoKDFBytesMax
Example ¶
mk := MakeMasterKey() context := MakeKeyContext("testblablabla") // only first CryptoKDFContextBytes is used fmt.Println(context) sk := mk.Derive(CryptoKDFBytesMin, 0, context) fmt.Println(sk.Length() == CryptoKDFBytesMin)
Output: testblab true
type Nonce ¶
type Nonce interface { Typed Next() //Next unused nonce }
Nonce is used to protect secret key. It is important to not use the same nonce for a given key.
type PWHashSalt ¶
type PWHashSalt struct {
Bytes
}
PWHashSalt implements the Typed interface
func (PWHashSalt) Size ¶
func (s PWHashSalt) Size() int
type PWHashStr ¶
type PWHashStr struct {
// contains filtered or unexported fields
}
PWHashStr implements the Typed interface
func LoadPWHashStr ¶
func PWHashStore ¶
PWHashStore use moderate profile to pack hashed password into PWHashStr.
Example ¶
s := PWHashStore("test") str := s.Value() // str for store t := LoadPWHashStr(str) // load from storage err := t.PWHashVerify("test") fmt.Println(err)
Output: <nil>
func PWHashStoreInteractive ¶
PWHashStoreInteractive use interactive profile to pack hashed password into PWHashStr.
func PWHashStoreSensitive ¶
PWHashStoreSensitive use sensitive profile to pack hashed password into PWHashStr.
func (PWHashStr) PWHashVerify ¶
PWHashVerify verifies password.
type Scalar ¶
type Scalar struct {
Bytes
}
func CryptoScalarmultBase ¶
CryptoScalarmultBase calculates BoxPublicKey 'q' from BoxSecertKey 'n'.
type ScalarMult ¶
type ScalarMult struct {
Bytes
}
ScalarMult is the mulitiplication of two Scalar, used to calculate shared key from BoxSecertKey and other end's BoxPublicKey.
func CryptoScalarmult ¶
func CryptoScalarmult(n, p Scalar) (q ScalarMult)
CryptoScalarmult calculates common key 'q' from private key 'n' and other's public key 'p'
func (ScalarMult) Size ¶
func (s ScalarMult) Size() int
type SecretBoxKey ¶
type SecretBoxKey struct {
Bytes
}
func (SecretBoxKey) Size ¶
func (s SecretBoxKey) Size() int
type SecretBoxMAC ¶
type SecretBoxMAC struct {
Bytes
}
func (SecretBoxMAC) Size ¶
func (s SecretBoxMAC) Size() int
type SecretBoxNonce ¶
type SecretBoxNonce struct {
Bytes
}
func (*SecretBoxNonce) Next ¶
func (n *SecretBoxNonce) Next()
func (SecretBoxNonce) Size ¶
func (n SecretBoxNonce) Size() int
type ShortHashKey ¶
type ShortHashKey struct {
Bytes
}
func (ShortHashKey) Size ¶
func (s ShortHashKey) Size() int
type SignKP ¶
type SignKP struct { PublicKey SignPublicKey SecretKey SignSecretKey }
func SeedSignKP ¶
SeedSignKP generates a keypair for signing from a SignSeed.
The same pair of keys will be generated with the same 'seed'
Example ¶
seed := SignSeed{} Randomize(&seed) kp1 := SeedSignKP(seed) kp2 := SeedSignKP(seed) s1 := kp1.SecretKey s2 := kp2.SecretKey s := s1.Seed() pk1 := s1.PublicKey() fmt.Println(MemCmp(s1.Bytes, s2.Bytes, s1.Length()) == 0) fmt.Println(MemCmp(s.Bytes, seed.Bytes, seed.Length()) == 0) fmt.Println(MemCmp(pk1.Bytes, kp1.PublicKey.Bytes, pk1.Length()) == 0)
Output: true true true
func (SignKP) ToBox ¶
ToBox converts a pair of signing key into a pair of box key - ed25519 to curve25519 - returns BoxKP.
Example ¶
skp := MakeSignKP() bkp := skp.ToBox() rkp := MakeBoxKP() sb := skp.SecretKey.ToBox() pb := skp.PublicKey.ToBox() fmt.Println(MemCmp(bkp.SecretKey.Bytes, sb.Bytes, bkp.SecretKey.Length()) == 0) fmt.Println(MemCmp(bkp.PublicKey.Bytes, pb.Bytes, bkp.PublicKey.Length()) == 0) n := BoxNonce{} Randomize(&n) bc := m.Box(n, rkp.PublicKey, bkp.SecretKey) bom, err := bc.BoxOpen(n, bkp.PublicKey, rkp.SecretKey) fmt.Println(err) fmt.Println(MemCmp(bom, m, m.Length()) == 0)
Output: true true <nil> true
type SignPublicKey ¶
type SignPublicKey struct {
Bytes
}
func (SignPublicKey) Size ¶
func (k SignPublicKey) Size() int
func (SignPublicKey) ToBox ¶
func (k SignPublicKey) ToBox() BoxPublicKey
ToBox converts a signing public key into a box public key - ed25519 to curve25519 - returns BoxPublicKey.
type SignSecretKey ¶
type SignSecretKey struct {
Bytes
}
func (SignSecretKey) PublicKey ¶
func (k SignSecretKey) PublicKey() SignPublicKey
PublicKey extracts the SignPublicKey from the SignSecretKey.
func (SignSecretKey) Seed ¶
func (k SignSecretKey) Seed() SignSeed
Seed extracts the seed used when generating the key pair.
func (SignSecretKey) Size ¶
func (k SignSecretKey) Size() int
func (SignSecretKey) ToBox ¶
func (k SignSecretKey) ToBox() BoxSecretKey
ToBox converts a signing secret key into a box secret key - ed25519 to curve25519 - returns BoxSecretKey.
type SignState ¶
type SignState struct {
// contains filtered or unexported fields
}
func MakeSignState ¶
func MakeSignState() SignState
MakeSignState creates an empty state for multi-part messages that can't fit in memory.
Example ¶
kp := MakeSignKP() s_a := MakeSignState() s_a.Update(m) siga := s_a.Sign(kp.SecretKey) s_b := MakeSignState() s_b.Update(m) err := s_b.Verify(siga, kp.PublicKey) fmt.Println(err)
Output: <nil>
func (SignState) Sign ¶
func (s SignState) Sign(key SignSecretKey) Signature
Sign a signature for the current state.
The underlying state is freed after this call.