Debian Codegraph @

crypto/kdf/argon2.go

raw symbols SCIP
1 package kdf
2
3 import (
4 "fmt"
5
6 "github.com/pkg/errors"
7 )
8
9 const (
10 argon2iHash = "argon2i"
11 argon2idHash = "argon2id"
12 )
13
14 var (
15 // Argon2MaxMemory indicates the maximum amount of memory that Argon2 KDFs
16 // can support. It defines the maximum value for the parameter m. The
17 // current value is set to 16GB.
18 Argon2MaxMemory = 16 * 1048576
19
20 // Argon2MaxParallelism is the maximum number of threads used. It's the
21 // maximum value for the parameter p.
22 Argon2MaxParallelism = 32
23
24 // Argon2MaxIterations is the maximum number of iterations to run. It's the
25 // maximum value for the parameter t.
26 Argon2MaxIterations = 128
27 )
28
29 type argon2Param struct {
30 t, m uint32
31 p uint8
32 kl uint32
33 }
34
35 func (a *argon2Param) getParams() string {
36 return fmt.Sprintf("m=%d,t=%d,p=%d", a.m, a.t, a.p)
37 }
38
39 var argon2Params = map[string]argon2Param{
40 argon2iHash: {3, 32768, 4, 32},
41 argon2idHash: {1, 65536, 4, 32},
42 }
43
44 func newArgon2Params(s string) (*argon2Param, error) {
45 params := phcParamsToMap(s)
46 t, err := phcAtoi(params["t"], 3)
47 if err != nil || t < 1 || t > Argon2MaxIterations {
48 return nil, errors.Errorf("invalid argon2 parameter t=%s", params["t"])
49 }
50 m, err := phcAtoi(params["m"], 12)
51 if err != nil || m < 8 || m > Argon2MaxMemory {
52 return nil, errors.Errorf("invalid argon2 parameter m=%s", params["m"])
53 }
54 p, err := phcAtoi(params["p"], 1)
55 if err != nil || p < 1 || p > Argon2MaxParallelism {
56 return nil, errors.Errorf("invalid argon2 parameter p=%s", params["p"])
57 }
58
59 return &argon2Param{
60 t: uint32(t),
61 m: uint32(m),
62 p: uint8(p),
63 }, nil
64 }