-
Notifications
You must be signed in to change notification settings - Fork 13
/
argon2.go
151 lines (126 loc) · 3.69 KB
/
argon2.go
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Package argon2 provides low-level bindings for the Argon2 hashing library:
// libargon2. Argon2 specifies three versions: Argon2i, Argon2d, and Argon2id.
// Argon2i is useful for protection against side-channel attacks (key
// derivation), while Argon2d provides the highest resistance against GPU
// cracking attacks (proof-of-work). Argon2id provides good protection against
// both side-channel and GPU cracking attacks.
package argon2
// #cgo CFLAGS: -I/usr/include
// #cgo LDFLAGS: -L/usr/lib -largon2
// #include <stdlib.h>
// #include <argon2.h>
import "C"
import (
"bytes"
"crypto/subtle"
"strings"
"unsafe"
)
const (
ModeArgon2d int = C.Argon2_d
ModeArgon2i int = C.Argon2_i
ModeArgon2id int = C.Argon2_id
)
const (
Version10 int = C.ARGON2_VERSION_10
Version13 int = C.ARGON2_VERSION_13
VersionDefault int = C.ARGON2_VERSION_NUMBER
)
const (
FlagDefault int = C.ARGON2_DEFAULT_FLAGS
FlagClearPassword int = C.ARGON2_FLAG_CLEAR_PASSWORD
FlagClearSecret int = C.ARGON2_FLAG_CLEAR_SECRET
)
// Set C.FLAG_clear_internal_memory = 0 to disable internal memory clearing
// Hash hashes a password given a salt and an initialized Argon2 context. It
// returns the calculated hash as an output of raw bytes.
func Hash(ctx *Context, password, salt []byte) ([]byte, error) {
if ctx == nil {
return nil, ErrContext
}
return ctx.hash(password, salt)
}
// HashEncoded hashes a password and produces a crypt-like encoded string.
func HashEncoded(ctx *Context, password []byte, salt []byte) (string, error) {
if ctx == nil {
return "", ErrContext
}
if len(password) == 0 {
return "", ErrPassword
}
if len(salt) == 0 {
return "", ErrSalt
}
encodedlen := C.argon2_encodedlen(
C.uint32_t(ctx.Iterations),
C.uint32_t(ctx.Memory),
C.uint32_t(ctx.Parallelism),
C.uint32_t(len(salt)),
C.uint32_t(ctx.HashLen),
C.argon2_type(ctx.Mode))
s := make([]byte, encodedlen)
result := C.argon2_hash(
C.uint32_t(ctx.Iterations),
C.uint32_t(ctx.Memory),
C.uint32_t(ctx.Parallelism),
unsafe.Pointer(&password[0]), C.size_t(len(password)),
unsafe.Pointer(&salt[0]), C.size_t(len(salt)),
nil, C.size_t(ctx.HashLen),
(*C.char)(unsafe.Pointer(&s[0])), C.size_t(encodedlen),
C.argon2_type(ctx.Mode),
C.uint32_t(ctx.Version))
if result != C.ARGON2_OK {
return "", Error(result)
}
// Strip trailing null byte(s)
s = bytes.TrimRight(s, "\x00")
return string(s), nil
}
// Verify verifies an Argon2 hash against a plaintext password.
func Verify(ctx *Context, hash, password, salt []byte) (bool, error) {
if ctx == nil {
return false, ErrContext
}
if len(hash) == 0 {
return false, ErrHash
}
hash2, err := ctx.hash(password, salt)
if err != nil {
return false, err
}
return subtle.ConstantTimeCompare(hash, hash2) == 1, nil
}
// VerifyEncoded verifies an encoded Argon2 hash s against a plaintext password.
func VerifyEncoded(s string, password []byte) (bool, error) {
mode, err := getMode(s)
if err != nil {
return false, err
}
cs := C.CString(s)
defer C.free(unsafe.Pointer(cs))
result := C.argon2_verify(
cs,
unsafe.Pointer(&password[0]),
C.size_t(len(password)),
C.argon2_type(mode))
if result == C.ARGON2_OK {
return true, nil
} else if result == C.ARGON2_VERIFY_MISMATCH {
return false, nil
}
// argon2_verify always seems to return an error in this case...
return false, Error(result)
}
// getMode tries to extract the mode from an Argon2 encoded string.
func getMode(s string) (int, error) {
switch {
case strings.HasPrefix(s, "$argon2d"):
return ModeArgon2d, nil
case strings.HasPrefix(s, "$argon2id"):
return ModeArgon2id, nil
case strings.HasPrefix(s, "$argon2i"):
return ModeArgon2i, nil
default:
return -1, ErrDecodingFail
}
}