This repository has been archived by the owner on Jun 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dsa.go
86 lines (71 loc) · 1.6 KB
/
dsa.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package sshkey
import (
"bytes"
"crypto"
"crypto/dsa"
"math/big"
)
type dsaPublicKey struct {
pub *dsa.PublicKey
basePublicKey
}
func (r *dsaPublicKey) Length() int {
return r.pub.P.BitLen()
}
func (r *dsaPublicKey) Public() crypto.PublicKey {
return r.pub
}
func marshalDSAPublicKey(k PublicKey) (prefix string, content []byte, err error) {
key, ok := k.Public().(*dsa.PublicKey)
if !ok {
err = ErrUnsupportedKey
return
}
prefix = "ssh-dss"
buf := bytes.NewBuffer(nil)
buf.Write(encodeByteSlice([]byte(prefix)))
buf.Write(encodeByteSlice([]byte{0}, key.Parameters.P.Bytes()))
buf.Write(encodeByteSlice([]byte{0}, key.Parameters.Q.Bytes()))
buf.Write(encodeByteSlice(key.Parameters.G.Bytes()))
buf.Write(encodeByteSlice(key.Y.Bytes()))
content = buf.Bytes()
return
}
func unmarshalDSAPublicKey(c []byte, comment string) (*dsaPublicKey, error) {
var p, q, g, y []byte
alg, c := decodeByteSlice(c)
if alg == nil || string(alg) != "ssh-dss" {
return nil, ErrMalformedKey
}
p, c = decodeByteSlice(c)
if p == nil {
return nil, ErrMalformedKey
}
q, c = decodeByteSlice(c)
if q == nil {
return nil, ErrMalformedKey
}
g, c = decodeByteSlice(c)
if g == nil {
return nil, ErrMalformedKey
}
y, c = decodeByteSlice(c)
if y == nil {
return nil, ErrMalformedKey
}
key := &dsaPublicKey{
pub: &dsa.PublicKey{
Parameters: dsa.Parameters{
P: new(big.Int).SetBytes(p),
Q: new(big.Int).SetBytes(q),
G: new(big.Int).SetBytes(g),
},
Y: new(big.Int).SetBytes(y),
},
basePublicKey: basePublicKey{
keyType: KEY_DSA,
comment: comment,
},
}
return key, nil
}