Debian Codegraph @

params.go

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 // Copyright 2013 Miek Gieben. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package pkcs11
6
7 /*
•8 #cgo pkg-config: nss
9 #include <stdlib.h>
10 #include <string.h>
11 #include "pkcs11go.h"
12
13 static inline void putOAEPParams(CK_RSA_PKCS_OAEP_PARAMS_PTR params, CK_VOID_PTR pSourceData, CK_ULONG ulSourceDataLen)
14 {
15 params->pSourceData = pSourceData;
16 params->ulSourceDataLen = ulSourceDataLen;
17 }
18
19 static inline void putECDH1SharedParams(CK_ECDH1_DERIVE_PARAMS_PTR params, CK_VOID_PTR pSharedData, CK_ULONG ulSharedDataLen)
20 {
21 params->pSharedData = pSharedData;
22 params->ulSharedDataLen = ulSharedDataLen;
23 }
24
25 static inline void putECDH1PublicParams(CK_ECDH1_DERIVE_PARAMS_PTR params, CK_VOID_PTR pPublicData, CK_ULONG ulPublicDataLen)
26 {
27 params->pPublicData = pPublicData;
28 params->ulPublicDataLen = ulPublicDataLen;
29 }
30 */
31 import "C"
32 import "unsafe"
33
34 // GCMParams represents the parameters for the AES-GCM mechanism.
35 type GCMParams struct {
36 arena
37 params *C.CK_GCM_PARAMS
38 iv []byte
39 aad []byte
40 tagSize int
41 }
42
43 // NewGCMParams returns a pointer to AES-GCM parameters that can be used with the CKM_AES_GCM mechanism.
44 // The Free() method must be called after the operation is complete.
45 //
46 // Note that some HSMs, like CloudHSM, will ignore the IV you pass in and write their
47 // own. As a result, to support all libraries, memory is not freed
48 // automatically, so that after the EncryptInit/Encrypt operation the HSM's IV
49 // can be read back out. It is up to the caller to ensure that Free() is called
50 // on the GCMParams object at an appropriate time, which is after
51 //
52 // Encrypt/Decrypt. As an example:
53 //
54 // gcmParams := pkcs11.NewGCMParams(make([]byte, 12), nil, 128)
55 // p.ctx.EncryptInit(session, []*pkcs11.Mechanism{pkcs11.NewMechanism(pkcs11.CKM_AES_GCM, gcmParams)},
56 // aesObjHandle)
57 // ct, _ := p.ctx.Encrypt(session, pt)
58 // iv := gcmParams.IV()
59 // gcmParams.Free()
60 //
61 func NewGCMParams(iv, aad []byte, tagSize int) *GCMParams {
62 return &GCMParams{
63 iv: iv,
64 aad: aad,
65 tagSize: tagSize,
66 }
67 }
68
69 func cGCMParams(p *GCMParams) []byte {
70 params := C.CK_GCM_PARAMS{
71 ulTagBits: C.CK_ULONG(p.tagSize),
72 }
73 var arena arena
74 if len(p.iv) > 0 {
75 iv, ivLen := arena.Allocate(p.iv)
76 params.pIv = C.CK_BYTE_PTR(iv)
77 params.ulIvLen = ivLen
78 params.ulIvBits = ivLen * 8
79 }
80 if len(p.aad) > 0 {
81 aad, aadLen := arena.Allocate(p.aad)
82 params.pAAD = C.CK_BYTE_PTR(aad)
83 params.ulAADLen = aadLen
84 }
85 p.Free()
86 p.arena = arena
87 p.params = &params
88 return C.GoBytes(unsafe.Pointer(&params), C.int(unsafe.Sizeof(params)))
89 }
90
91 // IV returns a copy of the actual IV used for the operation.
92 //
93 // Some HSMs may ignore the user-specified IV and write their own at the end of
94 // the encryption operation; this method allows you to retrieve it.
95 func (p *GCMParams) IV() []byte {
96 if p == nil || p.params == nil {
97 return nil
98 }
99 newIv := C.GoBytes(unsafe.Pointer(p.params.pIv), C.int(p.params.ulIvLen))
100 iv := make([]byte, len(newIv))
101 copy(iv, newIv)
102 return iv
103 }
104
105 // Free deallocates the memory reserved for the HSM to write back the actual IV.
106 //
107 // This must be called after the entire operation is complete, i.e. after
108 // Encrypt or EncryptFinal. It is safe to call Free multiple times.
109 func (p *GCMParams) Free() {
110 if p == nil || p.arena == nil {
111 return
112 }
113 p.arena.Free()
114 p.params = nil
115 p.arena = nil
116 }
117
118 // NewPSSParams creates a CK_RSA_PKCS_PSS_PARAMS structure and returns it as a byte array for use with the CKM_RSA_PKCS_PSS mechanism.
119 func NewPSSParams(hashAlg, mgf, saltLength uint) []byte {
120 p := C.CK_RSA_PKCS_PSS_PARAMS{
121 hashAlg: C.CK_MECHANISM_TYPE(hashAlg),
122 mgf: C.CK_RSA_PKCS_MGF_TYPE(mgf),
123 sLen: C.CK_ULONG(saltLength),
124 }
125 return C.GoBytes(unsafe.Pointer(&p), C.int(unsafe.Sizeof(p)))
126 }
127
128 // OAEPParams can be passed to NewMechanism to implement CKM_RSA_PKCS_OAEP.
129 type OAEPParams struct {
130 HashAlg uint
131 MGF uint
132 SourceType uint
133 SourceData []byte
134 }
135
136 // NewOAEPParams creates a CK_RSA_PKCS_OAEP_PARAMS structure suitable for use with the CKM_RSA_PKCS_OAEP mechanism.
137 func NewOAEPParams(hashAlg, mgf, sourceType uint, sourceData []byte) *OAEPParams {
138 return &OAEPParams{
139 HashAlg: hashAlg,
140 MGF: mgf,
141 SourceType: sourceType,
142 SourceData: sourceData,
143 }
144 }
145
146 func cOAEPParams(p *OAEPParams, arena arena) ([]byte, arena) {
147 params := C.CK_RSA_PKCS_OAEP_PARAMS{
148 hashAlg: C.CK_MECHANISM_TYPE(p.HashAlg),
149 mgf: C.CK_RSA_PKCS_MGF_TYPE(p.MGF),
150 source: C.CK_RSA_PKCS_OAEP_SOURCE_TYPE(p.SourceType),
151 }
152 if len(p.SourceData) != 0 {
153 buf, len := arena.Allocate(p.SourceData)
154 // field is unaligned on windows so this has to call into C
155 C.putOAEPParams(&params, buf, len)
156 }
157 return C.GoBytes(unsafe.Pointer(&params), C.int(unsafe.Sizeof(params))), arena
158 }
159
160 // ECDH1DeriveParams can be passed to NewMechanism to implement CK_ECDH1_DERIVE_PARAMS.
161 type ECDH1DeriveParams struct {
162 KDF uint
163 SharedData []byte
164 PublicKeyData []byte
165 }
166
167 // NewECDH1DeriveParams creates a CK_ECDH1_DERIVE_PARAMS structure suitable for use with the CKM_ECDH1_DERIVE mechanism.
168 func NewECDH1DeriveParams(kdf uint, sharedData []byte, publicKeyData []byte) *ECDH1DeriveParams {
169 return &ECDH1DeriveParams{
170 KDF: kdf,
171 SharedData: sharedData,
172 PublicKeyData: publicKeyData,
173 }
174 }
175
176 func cECDH1DeriveParams(p *ECDH1DeriveParams, arena arena) ([]byte, arena) {
177 params := C.CK_ECDH1_DERIVE_PARAMS{
178 kdf: C.CK_EC_KDF_TYPE(p.KDF),
179 }
180
181 // SharedData MUST be null if key derivation function (KDF) is CKD_NULL
182 if len(p.SharedData) != 0 {
183 sharedData, sharedDataLen := arena.Allocate(p.SharedData)
184 C.putECDH1SharedParams(&params, sharedData, sharedDataLen)
185 }
186
187 publicKeyData, publicKeyDataLen := arena.Allocate(p.PublicKeyData)
188 C.putECDH1PublicParams(&params, publicKeyData, publicKeyDataLen)
189
190 return C.GoBytes(unsafe.Pointer(&params), C.int(unsafe.Sizeof(params))), arena
191 }