-
Notifications
You must be signed in to change notification settings - Fork 1
/
dsa.go
141 lines (131 loc) · 4.7 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package crypto11
import (
"crypto"
"crypto/dsa"
"io"
"math/big"
"github.com/juju/errors"
pkcs11 "github.com/miekg/pkcs11"
)
// PKCS11PrivateKeyDSA contains a reference to a loaded PKCS#11 DSA private key object.
type PKCS11PrivateKeyDSA struct {
key *PKCS11PrivateKey
lib *PKCS11Lib
}
// Export the public key corresponding to a private DSA key.
func (lib *PKCS11Lib) exportDSAPublicKey(session pkcs11.SessionHandle, pubHandle pkcs11.ObjectHandle) (crypto.PublicKey, error) {
template := []*pkcs11.Attribute{
pkcs11.NewAttribute(pkcs11.CKA_PRIME, nil),
pkcs11.NewAttribute(pkcs11.CKA_SUBPRIME, nil),
pkcs11.NewAttribute(pkcs11.CKA_BASE, nil),
pkcs11.NewAttribute(pkcs11.CKA_VALUE, nil),
}
exported, err := lib.Ctx.GetAttributeValue(session, pubHandle, template)
if err != nil {
return nil, errors.Trace(err)
}
var p, q, g, x big.Int
p.SetBytes(exported[0].Value)
q.SetBytes(exported[1].Value)
g.SetBytes(exported[2].Value)
x.SetBytes(exported[3].Value)
result := dsa.PublicKey{
Parameters: dsa.Parameters{P: &p, Q: &q, G: &g},
Y: &x,
}
return &result, nil
}
// GenerateDSAKeyPair creates a DSA private key on the default slot
//
// The key will have a random label and ID.
func (lib *PKCS11Lib) GenerateDSAKeyPair(params *dsa.Parameters) (*PKCS11PrivateKeyDSA, error) {
return lib.GenerateDSAKeyPairOnSlot(lib.Slot.id, nil, nil, params)
}
// GenerateDSAKeyPairOnSlot creates a DSA private key on a specified slot
//
// Either or both label and/or id can be nil, in which case a random values will be generated.
func (lib *PKCS11Lib) GenerateDSAKeyPairOnSlot(slot uint, id []byte, label []byte, params *dsa.Parameters) (*PKCS11PrivateKeyDSA, error) {
var k *PKCS11PrivateKeyDSA
var err error
if err = lib.setupSessions(slot, 0); err != nil {
return nil, errors.Trace(err)
}
err = lib.withSession(slot, func(session pkcs11.SessionHandle) error {
k, err = lib.GenerateDSAKeyPairOnSession(session, slot, id, label, params)
return err
})
return k, err
}
// GenerateDSAKeyPairOnSession creates a DSA private key using a specified session
//
// Either or both label and/or id can be nil, in which case a random values will be generated.
func (lib *PKCS11Lib) GenerateDSAKeyPairOnSession(session pkcs11.SessionHandle, slot uint, id []byte, label []byte, params *dsa.Parameters) (*PKCS11PrivateKeyDSA, error) {
var err error
var pub crypto.PublicKey
if label == nil {
if label, err = lib.generateKeyLabel(); err != nil {
return nil, errors.Trace(err)
}
}
if id == nil {
if id, err = lib.generateKeyID(); err != nil {
return nil, errors.Trace(err)
}
}
p := params.P.Bytes()
q := params.Q.Bytes()
g := params.G.Bytes()
publicKeyTemplate := []*pkcs11.Attribute{
pkcs11.NewAttribute(pkcs11.CKA_CLASS, pkcs11.CKO_PUBLIC_KEY),
pkcs11.NewAttribute(pkcs11.CKA_KEY_TYPE, pkcs11.CKK_DSA),
pkcs11.NewAttribute(pkcs11.CKA_TOKEN, true),
pkcs11.NewAttribute(pkcs11.CKA_VERIFY, true),
pkcs11.NewAttribute(pkcs11.CKA_PRIME, p),
pkcs11.NewAttribute(pkcs11.CKA_SUBPRIME, q),
pkcs11.NewAttribute(pkcs11.CKA_BASE, g),
pkcs11.NewAttribute(pkcs11.CKA_LABEL, label),
pkcs11.NewAttribute(pkcs11.CKA_ID, id),
}
privateKeyTemplate := []*pkcs11.Attribute{
pkcs11.NewAttribute(pkcs11.CKA_TOKEN, true),
pkcs11.NewAttribute(pkcs11.CKA_SIGN, true),
pkcs11.NewAttribute(pkcs11.CKA_SENSITIVE, true),
pkcs11.NewAttribute(pkcs11.CKA_EXTRACTABLE, false),
pkcs11.NewAttribute(pkcs11.CKA_LABEL, label),
pkcs11.NewAttribute(pkcs11.CKA_ID, id),
}
mech := []*pkcs11.Mechanism{pkcs11.NewMechanism(pkcs11.CKM_DSA_KEY_PAIR_GEN, nil)}
pubHandle, privHandle, err := lib.Ctx.GenerateKeyPair(session,
mech,
publicKeyTemplate,
privateKeyTemplate)
if err != nil {
return nil, errors.Trace(err)
}
if pub, err = lib.exportDSAPublicKey(session, pubHandle); err != nil {
return nil, errors.Trace(err)
}
priv := PKCS11PrivateKeyDSA{
key: &PKCS11PrivateKey{PKCS11Object{privHandle, slot}, pub},
lib: lib,
}
return &priv, nil
}
// Sign signs a message using a DSA key.
//
// This completes the implemention of crypto.Signer for PKCS11PrivateKeyDSA.
//
// PKCS#11 expects to pick its own random data for signatures, so the rand argument is ignored.
//
// The return value is a DER-encoded byteblock.
func (priv *PKCS11PrivateKeyDSA) Sign(rand io.Reader, digest []byte, opts crypto.SignerOpts) (signature []byte, err error) {
return priv.lib.dsaGeneric(priv.lib.Slot.id, priv.key.Handle, pkcs11.CKM_DSA, digest)
}
// Public returns the public half of a private key.
//
// This partially implements the go.crypto.Signer and go.crypto.Decrypter interfaces for
// PKCS11PrivateKey. (The remains of the implementation is in the
// key-specific types.)
func (priv *PKCS11PrivateKeyDSA) Public() crypto.PublicKey {
return priv.key.PubKey
}