forked from 1Password/srp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
group_test.go
68 lines (59 loc) · 1.45 KB
/
group_test.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
68
package srp
import (
"math/big"
"testing"
"github.com/pkg/errors"
)
var runVerySlowTests = false // run slow tests on groups?
func TestGroups(t *testing.T) {
MinGroupSize = 1024 // We need a 1024 group to test against spec
for _, grp := range KnownGroups {
if err := checkGroup(*grp); err != nil {
t.Errorf("bad group %s: %s", grp.Label, err)
}
if runVerySlowTests {
if err := checkGroupSlow(*grp); err != nil {
t.Errorf("suspicious group %s: %s", grp.Label, err)
}
}
}
}
func checkGroup(group Group) error {
if group.n == nil {
return errors.New("N not set")
}
if group.g == nil {
return errors.New("g not set")
}
if group.n.BitLen() < MinGroupSize {
return errors.New("N too small")
}
if group.g.Cmp(bigOne) != 1 {
return errors.New("g < 2")
}
z := new(big.Int)
if z.GCD(nil, nil, group.g, group.n).Cmp(bigOne) != 0 {
return errors.New("GCD(g, N) != 1")
}
return nil
}
// These tests are very slow. Several seconds per group
// Also they do not defend against maliciously crafted groups
func checkGroupSlow(group Group) error {
if !group.n.ProbablyPrime(2) {
return errors.New("N isn't prime")
}
// is N a safe prime?
// Does N = 2q + 1, where q is prime?
q := new(big.Int)
q.Sub(group.n, bigOne)
q.Div(q, big.NewInt(2))
if !q.ProbablyPrime(2) {
return errors.New("N isn't a safe prime")
}
return nil
}
/**
** Copyright 2017 AgileBits, Inc.
** Licensed under the Apache License, Version 2.0 (the "License").
**/