Debian Codegraph @

crypto/kdf/scrypt.go

raw symbols SCIP
1 package kdf
2
3 import (
4 "fmt"
5 "math"
6
7 "github.com/pkg/errors"
8 )
9
10 const (
11 bcryptHash = "2a"
12 scryptHash = "scrypt"
13 scryptHash16384 = "scrypt-16384"
14 scryptHash32768 = "scrypt-32768"
15 scryptHash65536 = "scrypt-65536"
16 )
17
18 var (
19 // ScryptMaxCost the the maximum value for ln. Maximum is set to avoid
20 // panics due to not enough memory errors. Memory used is ~4*32*(2^ln)*r
21 // bytes.
22 ScryptMaxCost = 20
23 // ScryptMaxBlockSize is the maximum value for r. The maximum is set to
24 // avoid panics due to not enough memory errors. Memory used is
25 // ~4*32*(2^ln)*r bytes.
26 ScryptMaxBlockSize = 32
27 // ScryptMaxParallelism is the maximum value for p.
28 ScryptMaxParallelism = 32
29 )
30
31 var scryptParams = map[string]scryptParam{
32 scryptHash16384: {16384, 8, 1, 32},
33 scryptHash32768: {32768, 8, 1, 32},
34 scryptHash65536: {65536, 8, 1, 32},
35 }
36
37 type scryptParam struct {
38 N, r, p int
39 kl int
40 }
41
42 func newScryptParams(s string) (*scryptParam, error) {
43 params := phcParamsToMap(s)
44 ln, err := phcAtoi(params["ln"], 16)
45 if err != nil || ln < 1 || ln > ScryptMaxCost {
46 return nil, errors.Errorf("invalid scrypt parameter ln=%s", params["ln"])
47 }
48 r, err := phcAtoi(params["r"], 8)
49 if err != nil || r < 1 || r > ScryptMaxBlockSize {
50 return nil, errors.Errorf("invalid scrypt parameter r=%s", params["r"])
51 }
52 p, err := phcAtoi(params["p"], 1)
53 if err != nil || p < 1 || p > ScryptMaxParallelism {
54 return nil, errors.Errorf("invalid scrypt parameter p=%s", params["p"])
55 }
56
57 return &scryptParam{
58 N: int(math.Pow(2, float64(ln))),
59 r: r,
60 p: p,
61 }, nil
62 }
63
64 func (s *scryptParam) getParams() string {
65 return fmt.Sprintf("ln=%d,r=%d,p=%d", int(math.Log2(float64(s.N))), s.r, s.p)
66 }