-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencrypt_test.go
More file actions
105 lines (99 loc) · 4.3 KB
/
encrypt_test.go
File metadata and controls
105 lines (99 loc) · 4.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright (c) 2026, Julian Müller (ChaoticByte)
package main
import (
crand "crypto/rand"
"math/rand"
"slices"
"testing"
"time"
"github.com/awnumar/memguard"
)
func TestKeyDerivation(t *testing.T) {
password1 := []byte("test")
password2 := make([]byte, 20)
crand.Read(password2)
salt1 := [12]byte{}
crand.Read(salt1[:])
salt2 := [12]byte{}
crand.Read(salt2[:])
//
key1 := derive_key(password1, salt1)
key2 := derive_key(password2, salt2)
//
if key1 == key2 { t.Error("derived key1 == key2!") }
//
key1_salt2 := derive_key(password1, salt2)
key2_salt1 := derive_key(password2, salt1)
if key1 == key1_salt2 { t.Error("derived key1 == (key1 with wrong salt)!") }
if key2 == key2_salt1 { t.Error("derived key2 == (key2 with wrong salt)!") }
//
rekey1 := derive_key(password1, salt1)
rekey2 := derive_key(password2, salt2)
if rekey1 != key1 { t.Error("kdf is non-deterministic! derived key1 != re-key1!") }
if rekey2 != key2 { t.Error("kdf is non-deterministic! derived key2 != re-key2!") }
}
func trialWithTamperedInput(t *testing.T, what string, password *memguard.Enclave, ciphertext []byte, salt [12]byte, noncePfx [16]byte, time uint64, original string) {
clt_t, err := DecryptText(password, ciphertext, salt, noncePfx, time)
if err == nil || err.Error() != "chacha20poly1305: message authentication failed" {
t.Errorf("Could decrypt with tampered %v; message authentication not functioning properly!", what)
} else if clt_t == original {
t.Errorf("Could decrypt with tampered %v and no error given by aead.Open()! message authentication not functioning properly!", what)
}
}
func TestCrypto(t *testing.T) {
password1 := memguard.NewEnclave([]byte("test"))
password2_bytes := make([]byte, 20)
crand.Read(password2_bytes)
password2 := memguard.NewEnclave(password2_bytes)
t1 := uint64(time.Now().UnixMicro())
time.Sleep(time.Duration(1.0 + rand.Float64()) * time.Second)
t2 := uint64(time.Now().UnixMicro())
cleartext := "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet."
//
cit1, salt1, noncePfx1, err1 := EncryptText(password1, cleartext, t1)
if err1 != nil { t.Fatalf("Could not encrypt with password1, err: %v", err1) }
cit2, salt2, noncePfx2, err2 := EncryptText(password2, cleartext, t2)
if err2 != nil { t.Fatalf("Could not encrypt with password2, err: %v", err2) }
//
if salt1 == salt2 {
t.Error("salt1 and salt2 are the same!")
}
if noncePfx1 == noncePfx2 {
t.Error("random part of nonce 1 and 2 are the same")
}
if slices.Equal(cit1, []byte(cleartext)) {
t.Error("ciphertext1 == cleartext!")
}
if slices.Equal(cit2, []byte(cleartext)) {
t.Error("ciphertext2 == cleartext!")
}
//
clt_decrypted1, err := DecryptText(password1, cit1, salt1, noncePfx1, t1)
if err != nil {
t.Error("Could not decrypt ciphertext1 using password1!")
}
if clt_decrypted1 != cleartext {
t.Error("Decrypted ciphertext1 does not equal original ciphertext!")
}
clt_decrypted2, err := DecryptText(password2, cit2, salt2, noncePfx2, t2)
if err != nil {
t.Error("Could not decrypt ciphertext2 using password1!")
}
if clt_decrypted2 != cleartext {
t.Error("Decrypted ciphertext2 does not equal original ciphertext!")
}
//
trialWithTamperedInput(t, "wrong password", password2, cit1, salt1, noncePfx1, t1, cleartext)
trialWithTamperedInput(t, "wrong password", password1, cit2, salt2, noncePfx2, t2, cleartext)
trialWithTamperedInput(t, "tampered nonce prefix", password1, cit1, salt1, noncePfx2, t1, cleartext)
trialWithTamperedInput(t, "tampered nonce prefix", password2, cit2, salt2, noncePfx1, t2, cleartext)
//
cit1_tampered := make([]byte, len(cit1))
copy(cit1_tampered, cit1)
if cit1_tampered[3] < 255 {
cit1_tampered[3] += 1
} else {
cit1_tampered[3] -= 1
}
trialWithTamperedInput(t, "tampered ciphertext", password1, cit1_tampered, salt1, noncePfx1, t1, cleartext)
}