File: argon2.go

package info (click to toggle)
golang-github-smallstep-cli 0.15.16%2Bds-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,404 kB
  • sloc: sh: 512; makefile: 99
file content (64 lines) | stat: -rw-r--r-- 1,566 bytes parent folder | download | duplicates (2)
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
package kdf

import (
	"fmt"

	"github.com/pkg/errors"
)

const (
	argon2iHash  = "argon2i"
	argon2idHash = "argon2id"
)

var (
	// Argon2MaxMemory indicates the maximum amount of memory that Argon2 KDFs
	// can support. It defines the maximum value for the parameter m.  The
	// current value is set to 16GB.
	Argon2MaxMemory = 16 * 1048576

	// Argon2MaxParallelism is the maximum number of threads used. It's the
	// maximum value for the parameter p.
	Argon2MaxParallelism = 32

	// Argon2MaxIterations is the maximum number of iterations to run. It's the
	// maximum value for the parameter t.
	Argon2MaxIterations = 128
)

type argon2Param struct {
	t, m uint32
	p    uint8
	kl   uint32
}

func (a *argon2Param) getParams() string {
	return fmt.Sprintf("m=%d,t=%d,p=%d", a.m, a.t, a.p)
}

var argon2Params = map[string]argon2Param{
	argon2iHash:  {3, 32768, 4, 32},
	argon2idHash: {1, 65536, 4, 32},
}

func newArgon2Params(s string) (*argon2Param, error) {
	params := phcParamsToMap(s)
	t, err := phcAtoi(params["t"], 3)
	if err != nil || t < 1 || t > Argon2MaxIterations {
		return nil, errors.Errorf("invalid argon2 parameter t=%s", params["t"])
	}
	m, err := phcAtoi(params["m"], 12)
	if err != nil || m < 8 || m > Argon2MaxMemory {
		return nil, errors.Errorf("invalid argon2 parameter m=%s", params["m"])
	}
	p, err := phcAtoi(params["p"], 1)
	if err != nil || p < 1 || p > Argon2MaxParallelism {
		return nil, errors.Errorf("invalid argon2 parameter p=%s", params["p"])
	}

	return &argon2Param{
		t: uint32(t),
		m: uint32(m),
		p: uint8(p),
	}, nil
}