forked from 1Password/srp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kdf.go
67 lines (55 loc) · 1.56 KB
/
kdf.go
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
65
66
67
package srp
import (
"crypto/sha1"
"math/big"
"strings"
"unicode"
"golang.org/x/text/unicode/norm"
)
/*
* Best to use KDF from github/agilebits/op/cryto
* I will import at some point
*/
/*
KDFRFC5054 is *not* recommended. Instead use a KDF that
involes a hashing scheme designed for password hashing.
The SRP verifier that is stored by the server is like
a password hash with respect to crackability. Choose a KDF
that that makes the server stored verifiers hard to crack.
This computes the client's long term secret, x
from a username, password, and salt as described
in RFC5054 §2.6, which says
x = SHA1(s | SHA1(I | ":" | P))
**/
func KDFRFC5054(salt []byte, username string, password string) (x *big.Int) {
p := []byte(PreparePassword(password))
u := []byte(PreparePassword(username))
innerHasher := sha1.New()
innerHasher.Write(u)
innerHasher.Write([]byte(":"))
innerHasher.Write(p)
ih := innerHasher.Sum(nil)
oHasher := sha1.New()
oHasher.Write(salt)
oHasher.Write(ih)
h := oHasher.Sum(nil)
x = bigIntFromBytes(h)
return x
}
// PreparePassword strips leading and trailing white space
// and normalizes to unicode NFKD
func PreparePassword(s string) string {
var out string
// step #1: normalize `NFKD`
out = string(norm.NFKD.Bytes([]byte(s)))
// step #2: trim left
out = strings.TrimLeftFunc(out, unicode.IsSpace)
// step #3: trim right
out = strings.TrimRightFunc(out, unicode.IsSpace)
// step #4: return the result
return out
}
/**
** Copyright 2017 AgileBits, Inc.
** Licensed under the Apache License, Version 2.0 (the "License").
**/