argon2: fix incorrect key derivation if parallelism > 1

This change fixes an incorrect key derivation if the
degree of parallelism is greater than 1.

This change adds additional test vectors generated by the
https://github.com/P-H-C/phc-winner-argon2 CLI.

Fixes golang/go#23200

Change-Id: I8add8382b9e9ebbf9a70493050867c9af4ed6aa7
Reviewed-on: https://go-review.googlesource.com/85055
Reviewed-by: Adam Langley <agl@golang.org>
Run-TryBot: Adam Langley <agl@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
Andreas Auernhammer
2017-12-31 21:46:28 +00:00
committed by Adam Langley
parent 95a4943f35
commit 2b4b18b6aa
2 changed files with 146 additions and 17 deletions
+26 -17
View File
@@ -47,24 +47,28 @@ func deriveKey(mode int, password, salt, secret, data []byte, time, memory uint3
panic("argon2: number of rounds too small")
}
if threads < 1 {
panic("argon2: paralisim degree too low")
panic("argon2: parallelism degree too low")
}
mem := memory / (4 * uint32(threads)) * (4 * uint32(threads))
if mem < 8*uint32(threads) {
mem = 8 * uint32(threads)
h0 := initHash(password, salt, secret, data, time, memory, uint32(threads), keyLen, mode)
memory = memory / (syncPoints * uint32(threads)) * (syncPoints * uint32(threads))
if memory < 2*syncPoints*uint32(threads) {
memory = 2 * syncPoints * uint32(threads)
}
B := initBlocks(password, salt, secret, data, time, mem, uint32(threads), keyLen, mode)
processBlocks(B, time, mem, uint32(threads), mode)
return extractKey(B, mem, uint32(threads), keyLen)
B := initBlocks(&h0, memory, uint32(threads))
processBlocks(B, time, memory, uint32(threads), mode)
return extractKey(B, memory, uint32(threads), keyLen)
}
const blockLength = 128
const (
blockLength = 128
syncPoints = 4
)
type block [blockLength]uint64
func initBlocks(password, salt, key, data []byte, time, memory, threads, keyLen uint32, mode int) []block {
func initHash(password, salt, key, data []byte, time, memory, threads, keyLen uint32, mode int) [blake2b.Size + 8]byte {
var (
block0 [1024]byte
h0 [blake2b.Size + 8]byte
params [24]byte
tmp [4]byte
@@ -91,7 +95,11 @@ func initBlocks(password, salt, key, data []byte, time, memory, threads, keyLen
b2.Write(tmp[:])
b2.Write(data)
b2.Sum(h0[:0])
return h0
}
func initBlocks(h0 *[blake2b.Size + 8]byte, memory, threads uint32) []block {
var block0 [1024]byte
B := make([]block, memory)
for lane := uint32(0); lane < threads; lane++ {
j := lane * (memory / threads)
@@ -99,13 +107,13 @@ func initBlocks(password, salt, key, data []byte, time, memory, threads, keyLen
binary.LittleEndian.PutUint32(h0[blake2b.Size:], 0)
blake2bHash(block0[:], h0[:])
for i := range B[0] {
for i := range B[j+0] {
B[j+0][i] = binary.LittleEndian.Uint64(block0[i*8:])
}
binary.LittleEndian.PutUint32(h0[blake2b.Size:], 1)
blake2bHash(block0[:], h0[:])
for i := range B[0] {
for i := range B[j+1] {
B[j+1][i] = binary.LittleEndian.Uint64(block0[i*8:])
}
}
@@ -113,7 +121,6 @@ func initBlocks(password, salt, key, data []byte, time, memory, threads, keyLen
}
func processBlocks(B []block, time, memory, threads uint32, mode int) {
const syncPoints = 4
lanes := memory / threads
segments := lanes / syncPoints
@@ -131,7 +138,7 @@ func processBlocks(B []block, time, memory, threads uint32, mode int) {
index := uint32(0)
if n == 0 && slice == 0 {
index = 2 // we have already generated the first two blocks
if mode == argon2i || (mode == argon2id && n == 0 && slice < syncPoints/2) {
if mode == argon2i || mode == argon2id {
in[6]++
processBlock(&addresses, &in, &zero)
processBlock(&addresses, &addresses, &zero)
@@ -143,7 +150,7 @@ func processBlocks(B []block, time, memory, threads uint32, mode int) {
for index < segments {
prev := offset - 1
if index == 0 && slice == 0 {
prev = lane*lanes + lanes - 1 // last block in lane
prev += lanes // last block in lane
}
if mode == argon2i || (mode == argon2id && n == 0 && slice < syncPoints/2) {
if index%blockLength == 0 {
@@ -194,8 +201,10 @@ func extractKey(B []block, memory, threads, keyLen uint32) []byte {
func indexAlpha(rand uint64, lanes, segments, threads, n, slice, lane, index uint32) uint32 {
refLane := uint32(rand>>32) % threads
m, s := 3*segments, (slice+1)%4*segments
if n == 0 && slice == 0 {
refLane = lane
}
m, s := 3*segments, ((slice+1)%syncPoints)*segments
if lane == refLane {
m += index
}