OSDN Git Service

5f3d849204716c8dd427e881d08396fcd4258ee9
[bytom/vapor.git] / vendor / github.com / miekg / dns / dnssec_privkey.go
1 package dns
2
3 import (
4         "crypto"
5         "crypto/dsa"
6         "crypto/ecdsa"
7         "crypto/rsa"
8         "math/big"
9         "strconv"
10
11         "github.com/bytom/vapor/crypto/ed25519"
12 )
13
14 const format = "Private-key-format: v1.3\n"
15
16 // PrivateKeyString converts a PrivateKey to a string. This string has the same
17 // format as the private-key-file of BIND9 (Private-key-format: v1.3).
18 // It needs some info from the key (the algorithm), so its a method of the DNSKEY
19 // It supports rsa.PrivateKey, ecdsa.PrivateKey and dsa.PrivateKey
20 func (r *DNSKEY) PrivateKeyString(p crypto.PrivateKey) string {
21         algorithm := strconv.Itoa(int(r.Algorithm))
22         algorithm += " (" + AlgorithmToString[r.Algorithm] + ")"
23
24         switch p := p.(type) {
25         case *rsa.PrivateKey:
26                 modulus := toBase64(p.PublicKey.N.Bytes())
27                 e := big.NewInt(int64(p.PublicKey.E))
28                 publicExponent := toBase64(e.Bytes())
29                 privateExponent := toBase64(p.D.Bytes())
30                 prime1 := toBase64(p.Primes[0].Bytes())
31                 prime2 := toBase64(p.Primes[1].Bytes())
32                 // Calculate Exponent1/2 and Coefficient as per: http://en.wikipedia.org/wiki/RSA#Using_the_Chinese_remainder_algorithm
33                 // and from: http://code.google.com/p/go/issues/detail?id=987
34                 one := big.NewInt(1)
35                 p1 := big.NewInt(0).Sub(p.Primes[0], one)
36                 q1 := big.NewInt(0).Sub(p.Primes[1], one)
37                 exp1 := big.NewInt(0).Mod(p.D, p1)
38                 exp2 := big.NewInt(0).Mod(p.D, q1)
39                 coeff := big.NewInt(0).ModInverse(p.Primes[1], p.Primes[0])
40
41                 exponent1 := toBase64(exp1.Bytes())
42                 exponent2 := toBase64(exp2.Bytes())
43                 coefficient := toBase64(coeff.Bytes())
44
45                 return format +
46                         "Algorithm: " + algorithm + "\n" +
47                         "Modulus: " + modulus + "\n" +
48                         "PublicExponent: " + publicExponent + "\n" +
49                         "PrivateExponent: " + privateExponent + "\n" +
50                         "Prime1: " + prime1 + "\n" +
51                         "Prime2: " + prime2 + "\n" +
52                         "Exponent1: " + exponent1 + "\n" +
53                         "Exponent2: " + exponent2 + "\n" +
54                         "Coefficient: " + coefficient + "\n"
55
56         case *ecdsa.PrivateKey:
57                 var intlen int
58                 switch r.Algorithm {
59                 case ECDSAP256SHA256:
60                         intlen = 32
61                 case ECDSAP384SHA384:
62                         intlen = 48
63                 }
64                 private := toBase64(intToBytes(p.D, intlen))
65                 return format +
66                         "Algorithm: " + algorithm + "\n" +
67                         "PrivateKey: " + private + "\n"
68
69         case *dsa.PrivateKey:
70                 T := divRoundUp(divRoundUp(p.PublicKey.Parameters.G.BitLen(), 8)-64, 8)
71                 prime := toBase64(intToBytes(p.PublicKey.Parameters.P, 64+T*8))
72                 subprime := toBase64(intToBytes(p.PublicKey.Parameters.Q, 20))
73                 base := toBase64(intToBytes(p.PublicKey.Parameters.G, 64+T*8))
74                 priv := toBase64(intToBytes(p.X, 20))
75                 pub := toBase64(intToBytes(p.PublicKey.Y, 64+T*8))
76                 return format +
77                         "Algorithm: " + algorithm + "\n" +
78                         "Prime(p): " + prime + "\n" +
79                         "Subprime(q): " + subprime + "\n" +
80                         "Base(g): " + base + "\n" +
81                         "Private_value(x): " + priv + "\n" +
82                         "Public_value(y): " + pub + "\n"
83
84         case ed25519.PrivateKey:
85                 private := toBase64(p.Seed())
86                 return format +
87                         "Algorithm: " + algorithm + "\n" +
88                         "PrivateKey: " + private + "\n"
89
90         default:
91                 return ""
92         }
93 }