| 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 |
package argon2 |
| 42 |
|
| 43 |
import ( |
| 44 |
"encoding/binary" |
| 45 |
"sync" |
| 46 |
|
| 47 |
"github.com/go-crypt/x/blake2b" |
| 48 |
) |
| 49 |
|
| 50 |
|
| 51 |
type KeyFunc func(password, salt []byte, time, memory, threads, keyLen uint32) []byte |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
func IDKey(password, salt []byte, time, memory uint32, threads, keyLen uint32) []byte { |
| 74 |
return deriveKey(argon2id, password, salt, nil, nil, time, memory, threads, keyLen) |
| 75 |
} |
| 76 |
|
| 77 |
func deriveKey(mode int, password, salt, secret, data []byte, time, memory uint32, threads, keyLen uint32) []byte { |
| 78 |
if time < 1 { |
| 79 |
panic("argon2: number of rounds too small") |
| 80 |
} |
| 81 |
if threads < 1 { |
| 82 |
panic("argon2: parallelism degree too low") |
| 83 |
} |
| 84 |
h0 := initHash(password, salt, secret, data, time, memory, threads, keyLen, mode) |
| 85 |
|
| 86 |
memory = memory / (syncPoints * uint32(threads)) * (syncPoints * uint32(threads)) |
| 87 |
if memory < 2*syncPoints*uint32(threads) { |
| 88 |
memory = 2 * syncPoints * uint32(threads) |
| 89 |
} |
| 90 |
B := initBlocks(&h0, memory, threads) |
| 91 |
processBlocks(B, time, memory, threads, mode) |
| 92 |
return extractKey(B, memory, threads, keyLen) |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
func IKey(password, salt []byte, time, memory uint32, threads, keyLen uint32) []byte { |
| 115 |
return deriveKey(argon2i, password, salt, nil, nil, time, memory, threads, keyLen) |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
func DKey(password, salt []byte, time, memory, threads, keyLen uint32) []byte { |
| 122 |
return deriveKey(argon2d, password, salt, nil, nil, time, memory, threads, keyLen) |
| 123 |
} |
| 124 |
|
| 125 |
type block [blockLength]uint64 |
| 126 |
|
| 127 |
func initHash(password, salt, key, data []byte, time, memory, threads, keyLen uint32, mode int) [blake2b.Size + 8]byte { |
| 128 |
var ( |
| 129 |
h0 [blake2b.Size + 8]byte |
| 130 |
params [24]byte |
| 131 |
tmp [4]byte |
| 132 |
) |
| 133 |
|
| 134 |
b2, _ := blake2b.New512(nil) |
| 135 |
binary.LittleEndian.PutUint32(params[0:4], threads) |
| 136 |
binary.LittleEndian.PutUint32(params[4:8], keyLen) |
| 137 |
binary.LittleEndian.PutUint32(params[8:12], memory) |
| 138 |
binary.LittleEndian.PutUint32(params[12:16], time) |
| 139 |
binary.LittleEndian.PutUint32(params[16:20], uint32(Version)) |
| 140 |
binary.LittleEndian.PutUint32(params[20:24], uint32(mode)) |
| 141 |
b2.Write(params[:]) |
| 142 |
binary.LittleEndian.PutUint32(tmp[:], uint32(len(password))) |
| 143 |
b2.Write(tmp[:]) |
| 144 |
b2.Write(password) |
| 145 |
binary.LittleEndian.PutUint32(tmp[:], uint32(len(salt))) |
| 146 |
b2.Write(tmp[:]) |
| 147 |
b2.Write(salt) |
| 148 |
binary.LittleEndian.PutUint32(tmp[:], uint32(len(key))) |
| 149 |
b2.Write(tmp[:]) |
| 150 |
b2.Write(key) |
| 151 |
binary.LittleEndian.PutUint32(tmp[:], uint32(len(data))) |
| 152 |
b2.Write(tmp[:]) |
| 153 |
b2.Write(data) |
| 154 |
b2.Sum(h0[:0]) |
| 155 |
return h0 |
| 156 |
} |
| 157 |
|
| 158 |
func initBlocks(h0 *[blake2b.Size + 8]byte, memory, threads uint32) []block { |
| 159 |
var block0 [1024]byte |
| 160 |
B := make([]block, memory) |
| 161 |
for lane := uint32(0); lane < threads; lane++ { |
| 162 |
j := lane * (memory / threads) |
| 163 |
binary.LittleEndian.PutUint32(h0[blake2b.Size+4:], lane) |
| 164 |
|
| 165 |
binary.LittleEndian.PutUint32(h0[blake2b.Size:], 0) |
| 166 |
blake2bHash(block0[:], h0[:]) |
| 167 |
for i := range B[j+0] { |
| 168 |
B[j+0][i] = binary.LittleEndian.Uint64(block0[i*8:]) |
| 169 |
} |
| 170 |
|
| 171 |
binary.LittleEndian.PutUint32(h0[blake2b.Size:], 1) |
| 172 |
blake2bHash(block0[:], h0[:]) |
| 173 |
for i := range B[j+1] { |
| 174 |
B[j+1][i] = binary.LittleEndian.Uint64(block0[i*8:]) |
| 175 |
} |
| 176 |
} |
| 177 |
return B |
| 178 |
} |
| 179 |
|
| 180 |
func processBlocks(B []block, time, memory, threads uint32, mode int) { |
| 181 |
lanes := memory / threads |
| 182 |
segments := lanes / syncPoints |
| 183 |
|
| 184 |
processSegment := func(n, slice, lane uint32, wg *sync.WaitGroup) { |
| 185 |
var addresses, in, zero block |
| 186 |
if mode == argon2i || (mode == argon2id && n == 0 && slice < syncPoints/2) { |
| 187 |
in[0] = uint64(n) |
| 188 |
in[1] = uint64(lane) |
| 189 |
in[2] = uint64(slice) |
| 190 |
in[3] = uint64(memory) |
| 191 |
in[4] = uint64(time) |
| 192 |
in[5] = uint64(mode) |
| 193 |
} |
| 194 |
|
| 195 |
index := uint32(0) |
| 196 |
if n == 0 && slice == 0 { |
| 197 |
index = 2 |
| 198 |
if mode == argon2i || mode == argon2id { |
| 199 |
in[6]++ |
| 200 |
processBlock(&addresses, &in, &zero) |
| 201 |
processBlock(&addresses, &addresses, &zero) |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
offset := lane*lanes + slice*segments + index |
| 206 |
var random uint64 |
| 207 |
for index < segments { |
| 208 |
prev := offset - 1 |
| 209 |
if index == 0 && slice == 0 { |
| 210 |
prev += lanes |
| 211 |
} |
| 212 |
if mode == argon2i || (mode == argon2id && n == 0 && slice < syncPoints/2) { |
| 213 |
if index%blockLength == 0 { |
| 214 |
in[6]++ |
| 215 |
processBlock(&addresses, &in, &zero) |
| 216 |
processBlock(&addresses, &addresses, &zero) |
| 217 |
} |
| 218 |
random = addresses[index%blockLength] |
| 219 |
} else { |
| 220 |
random = B[prev][0] |
| 221 |
} |
| 222 |
newOffset := indexAlpha(random, lanes, segments, threads, n, slice, lane, index) |
| 223 |
processBlockXOR(&B[offset], &B[prev], &B[newOffset]) |
| 224 |
index, offset = index+1, offset+1 |
| 225 |
} |
| 226 |
wg.Done() |
| 227 |
} |
| 228 |
|
| 229 |
for n := uint32(0); n < time; n++ { |
| 230 |
for slice := uint32(0); slice < syncPoints; slice++ { |
| 231 |
var wg sync.WaitGroup |
| 232 |
for lane := uint32(0); lane < threads; lane++ { |
| 233 |
wg.Add(1) |
| 234 |
go processSegment(n, slice, lane, &wg) |
| 235 |
} |
| 236 |
wg.Wait() |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
} |
| 241 |
|
| 242 |
func extractKey(B []block, memory, threads, keyLen uint32) []byte { |
| 243 |
lanes := memory / threads |
| 244 |
for lane := uint32(0); lane < threads-1; lane++ { |
| 245 |
for i, v := range B[(lane*lanes)+lanes-1] { |
| 246 |
B[memory-1][i] ^= v |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
var block [1024]byte |
| 251 |
for i, v := range B[memory-1] { |
| 252 |
binary.LittleEndian.PutUint64(block[i*8:], v) |
| 253 |
} |
| 254 |
key := make([]byte, keyLen) |
| 255 |
blake2bHash(key, block[:]) |
| 256 |
return key |
| 257 |
} |
| 258 |
|
| 259 |
func indexAlpha(rand uint64, lanes, segments, threads, n, slice, lane, index uint32) uint32 { |
| 260 |
refLane := uint32(rand>>32) % threads |
| 261 |
if n == 0 && slice == 0 { |
| 262 |
refLane = lane |
| 263 |
} |
| 264 |
m, s := 3*segments, ((slice+1)%syncPoints)*segments |
| 265 |
if lane == refLane { |
| 266 |
m += index |
| 267 |
} |
| 268 |
if n == 0 { |
| 269 |
m, s = slice*segments, 0 |
| 270 |
if slice == 0 || lane == refLane { |
| 271 |
m += index |
| 272 |
} |
| 273 |
} |
| 274 |
if index == 0 || lane == refLane { |
| 275 |
m-- |
| 276 |
} |
| 277 |
return phi(rand, uint64(m), uint64(s), refLane, lanes) |
| 278 |
} |
| 279 |
|
| 280 |
func phi(rand, m, s uint64, lane, lanes uint32) uint32 { |
| 281 |
p := rand & 0xFFFFFFFF |
| 282 |
p = (p * p) >> 32 |
| 283 |
p = (p * m) >> 32 |
| 284 |
return lane*lanes + uint32((s+m-(p+1))%uint64(lanes)) |
| 285 |
} |