forked from ThalesGroup/crypto11
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aead.go
189 lines (158 loc) · 5.72 KB
/
aead.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// Copyright 2018 Thales e-Security, Inc
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package crypto11
import (
"crypto/cipher"
"errors"
"fmt"
"github.com/miekg/pkcs11"
)
// cipher.AEAD ----------------------------------------------------------
// A PaddingMode is used by a block cipher (see NewCBC).
type PaddingMode int
const (
// PaddingNone represents a block cipher with no padding.
PaddingNone PaddingMode = iota
// PaddingPKCS represents a block cipher used with PKCS#7 padding.
PaddingPKCS
)
var errBadGCMNonceSize = errors.New("nonce slice too small to hold IV")
type genericAead struct {
key *SecretKey
overhead int
nonceSize int
// Note - if the GCMParams result is non-nil, the caller must call Free() on the params when
// finished.
makeMech func(nonce []byte, additionalData []byte, encrypt bool) ([]*pkcs11.Mechanism, *pkcs11.GCMParams, error)
}
// NewGCM returns a given cipher wrapped in Galois Counter Mode, with the standard
// nonce length.
//
// This depends on the HSM supporting the CKM_*_GCM mechanism. If it is not supported
// then you must use cipher.NewGCM; it will be slow.
func (key *SecretKey) NewGCM() (cipher.AEAD, error) {
if key.Cipher.GCMMech == 0 {
return nil, fmt.Errorf("GCM not implemented for key type %#x", key.Cipher.GenParams[0].KeyType)
}
g := genericAead{
key: key,
overhead: 16,
nonceSize: key.context.cfg.GCMIVLength,
makeMech: func(nonce []byte, additionalData []byte, encrypt bool) ([]*pkcs11.Mechanism, *pkcs11.GCMParams, error) {
var params *pkcs11.GCMParams
if (encrypt && key.context.cfg.UseGCMIVFromHSM &&
!key.context.cfg.GCMIVFromHSMControl.SupplyIvForHSMGCMEncrypt) || (!encrypt &&
key.context.cfg.UseGCMIVFromHSM && !key.context.cfg.GCMIVFromHSMControl.SupplyIvForHSMGCMDecrypt) {
params = pkcs11.NewGCMParams(nil, additionalData, 16*8 /*bits*/)
} else {
params = pkcs11.NewGCMParams(nonce, additionalData, 16*8 /*bits*/)
}
return []*pkcs11.Mechanism{pkcs11.NewMechanism(key.Cipher.GCMMech, params)}, params, nil
},
}
return g, nil
}
// NewCBC returns a given cipher wrapped in CBC mode.
//
// Despite the cipher.AEAD return type, there is no support for additional data and no authentication.
// This method exists to provide a convenient way to do bulk (possibly padded) CBC encryption.
// Think carefully before passing the cipher.AEAD to any consumer that expects authentication.
func (key *SecretKey) NewCBC(paddingMode PaddingMode) (cipher.AEAD, error) {
var pkcsMech uint
switch paddingMode {
case PaddingNone:
pkcsMech = key.Cipher.CBCMech
case PaddingPKCS:
pkcsMech = key.Cipher.CBCPKCSMech
default:
return nil, errors.New("unrecognized padding mode")
}
g := genericAead{
key: key,
overhead: 0,
nonceSize: key.BlockSize(),
makeMech: func(nonce []byte, additionalData []byte, encrypt bool) ([]*pkcs11.Mechanism, *pkcs11.GCMParams, error) {
if len(additionalData) > 0 {
return nil, nil, errors.New("additional data not supported for CBC mode")
}
return []*pkcs11.Mechanism{pkcs11.NewMechanism(pkcsMech, nonce)}, nil, nil
},
}
return g, nil
}
func (g genericAead) NonceSize() int {
return g.nonceSize
}
func (g genericAead) Overhead() int {
return g.overhead
}
func (g genericAead) Seal(dst, nonce, plaintext, additionalData []byte) []byte {
var result []byte
if err := g.key.context.withSession(func(session *pkcs11Session) (err error) {
mech, params, err := g.makeMech(nonce, additionalData, true)
if err != nil {
return err
}
defer params.Free()
if err = session.ctx.EncryptInit(session.handle, mech, g.key.handle); err != nil {
err = fmt.Errorf("C_EncryptInit: %v", err)
return
}
if result, err = session.ctx.Encrypt(session.handle, plaintext); err != nil {
err = fmt.Errorf("C_Encrypt: %v", err)
return
}
if g.key.context.cfg.UseGCMIVFromHSM && g.key.context.cfg.GCMIVFromHSMControl.SupplyIvForHSMGCMEncrypt {
if len(nonce) != len(params.IV()) {
return errBadGCMNonceSize
}
}
return
}); err != nil {
panic(err)
} else {
dst = append(dst, result...)
}
return dst
}
func (g genericAead) Open(dst, nonce, ciphertext, additionalData []byte) ([]byte, error) {
var result []byte
if err := g.key.context.withSession(func(session *pkcs11Session) (err error) {
mech, params, err := g.makeMech(nonce, additionalData, false)
if err != nil {
return
}
defer params.Free()
if err = session.ctx.DecryptInit(session.handle, mech, g.key.handle); err != nil {
err = fmt.Errorf("C_DecryptInit: %v", err)
return
}
if result, err = session.ctx.Decrypt(session.handle, ciphertext); err != nil {
err = fmt.Errorf("C_Decrypt: %v", err)
return
}
return
}); err != nil {
return nil, err
}
dst = append(dst, result...)
return dst, nil
}