Debian Codegraph @

dh/dh.go

raw symbols SCIP
1 // Use of this source code is governed by a license
2 // that can be found in the LICENSE file.
3
4 // Package dh implements the Diffie-Hellman key exchange over
5 // multiplicative groups of integers modulo a prime.
6 // This also defines some commen groups described in RFC 3526.
7 package dh
8
9 import (
10 cryptorand "crypto/rand"
11 "errors"
12 "io"
13 "math/big"
14 )
15
16 var zero *big.Int = big.NewInt(0)
17 var one *big.Int = big.NewInt(1)
18 var two *big.Int = big.NewInt(2)
19
20 // IsSafePrime returns true, if the prime of the group is
21 // a so called safe-prime. For a group with a safe-prime prime
22 // number the Decisional-Diffie-Hellman-Problem (DDH) is a
23 // 'hard' problem. The n argument is the number of iterations
24 // for the probabilistic prime test.
25 // It's recommend to use DDH-safe groups for DH-exchanges.
26 func IsSafePrimeGroup(g *Group, n int) bool {
27 q := new(big.Int).Sub(g.P, one)
28 q = q.Div(q, two)
29 return q.ProbablyPrime(n)
30 }
31
32 // PublicKey is the type of DH public keys.
33 type PublicKey *big.Int
34
35 // PrivateKey is the type of DH private keys.
36 type PrivateKey *big.Int
37
38 // Group represents a mathematical group defined
39 // by a large prime and a generator.
40 type Group struct {
41 P *big.Int // The prime
42 G *big.Int // The generator
43 }
44
45 // GenerateKey generates a public/private key pair using entropy from rand.
46 // If rand is nil, crypto/rand.Reader will be used.
47 func (g *Group) GenerateKey(rand io.Reader) (private PrivateKey, public PublicKey, err error) {
48 if g.P == nil {
49 panic("crypto/dh: group prime is nil")
50 }
51 if g.G == nil {
52 panic("crypto/dh: group generator is nil")
53 }
54 if rand == nil {
55 rand = cryptorand.Reader
56 }
57
58 // Ensure, that p.G ^ privateKey > than g.P
59 // (only modulo calculations are safe)
60 // The minimal (and common) value for p.G is 2
61 // So 2 ^ (1 + 'bitsize of p.G') > than g.P
62 min := big.NewInt(int64(g.P.BitLen() + 1))
63 bytes := make([]byte, (g.P.BitLen()+7)/8)
64
65 for private == nil {
66 _, err = io.ReadFull(rand, bytes)
67 if err != nil {
68 private = nil
69 return
70 }
71 // Clear bits in the first byte to increase
72 // the probability that the candidate is < g.P.
73 bytes[0] = 0
74 if private == nil {
75 private = new(big.Int)
76 }
77 (*private).SetBytes(bytes)
78 if (*private).Cmp(min) < 0 {
79 private = nil
80 }
81 }
82
83 public = new(big.Int).Exp(g.G, private, g.P)
84 return
85 }
86
87 // PublicKey returns the public key corresponding to the given private one.
88 func (g *Group) PublicKey(private PrivateKey) (public PublicKey) {
89 public = new(big.Int).Exp(g.G, private, g.P)
90 return
91 }
92
93 //private returns a non-nil error if the given public key is
94 // not a possible element of the group. This means, that the
95 // public key is < 0 or > g.P.
96 func (g *Group) Check(peersPublic PublicKey) (err error) {
97 if !((*peersPublic).Cmp(zero) >= 0 && (*peersPublic).Cmp(g.P) == -1) {
98 err = errors.New("peer's public is not a possible group element")
99 }
100 return
101 }
102
103 // ComputeSecret returns the secret computed from
104 // the own private and the peer's public key.
105 func (g *Group) ComputeSecret(private PrivateKey, peersPublic PublicKey) (secret *big.Int) {
106 secret = new(big.Int).Exp(peersPublic, private, g.P)
107 return
108 }