Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modifies the newly-added GCMParams to allow reading IV #72

Merged
merged 2 commits into from
Apr 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 52 additions & 15 deletions params.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,36 +14,73 @@ import "unsafe"

// GCMParams represents the parameters for the AES-GCM mechanism.
type GCMParams struct {
IV []byte
AAD []byte
TagSize int
arena
params *C.CK_GCM_PARAMS
iv []byte
aad []byte
tagSize int
}

// NewGCMParams returns a pointer to the AES-GCM parameters.
// NewGCMParams returns a pointer to AES-GCM parameters.
// This is a convenience function for passing GCM parameters to
// available mechanisms
// available mechanisms.
//
// *NOTE*
// Some HSMs, like CloudHSM, will ignore the IV you pass in and write their
// own. As a result, to support all libraries, memory is not freed
// automatically, so that after the EncryptInit/Encrypt operation the HSM's IV
// can be read back out. It is up to the caller to ensure that Free() is called
// on the GCMParams object at an appropriate time, which is after
// Encrypt/Decrypt. As an example:
//
// gcmParams := pkcs11.NewGCMParams(make([]byte, 12), nil, 128)
// p.ctx.EncryptInit(session, []*pkcs11.Mechanism{pkcs11.NewMechanism(pkcs11.CKM_AES_GCM, gcmParams)}, aesObjHandle)
// ct, _ := p.ctx.Encrypt(session, pt)
// iv := gcmParams.IV()
// gcmParams.Free()
func NewGCMParams(iv, aad []byte, tagSize int) *GCMParams {
return &GCMParams{
IV: iv,
AAD: aad,
TagSize: tagSize,
iv: iv,
aad: aad,
tagSize: tagSize,
}
}

func cGCMParams(p *GCMParams) (arena, []byte) {
func cGCMParams(p *GCMParams) []byte {
params := C.CK_GCM_PARAMS{
ulTagBits: C.CK_ULONG(p.TagSize),
ulTagBits: C.CK_ULONG(p.tagSize),
}
var arena arena
if len(p.IV) > 0 {
iv, ivLen := arena.Allocate(p.IV)
if len(p.iv) > 0 {
iv, ivLen := arena.Allocate(p.iv)
params.pIv = C.CK_BYTE_PTR(iv)
params.ulIvLen = ivLen
}
if len(p.AAD) > 0 {
aad, aadLen := arena.Allocate(p.AAD)
if len(p.aad) > 0 {
aad, aadLen := arena.Allocate(p.aad)
params.pAAD = C.CK_BYTE_PTR(aad)
params.ulAADLen = aadLen
}
return arena, C.GoBytes(unsafe.Pointer(&params), C.int(unsafe.Sizeof(params)))
p.arena = arena
p.params = &params
return C.GoBytes(unsafe.Pointer(&params), C.int(unsafe.Sizeof(params)))
}

func (p *GCMParams) IV() []byte {
if p == nil || p.params == nil {
return nil
}
newIv := C.GoBytes(unsafe.Pointer(p.params.pIv), C.int(p.params.ulIvLen))
iv := make([]byte, len(newIv))
copy(iv, newIv)
return iv
}

func (p *GCMParams) Free() {
if p == nil || p.arena == nil {
return
}
p.arena.Free()
p.params = nil
p.arena = nil
}
4 changes: 1 addition & 3 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,6 @@ func cDate(t time.Time) []byte {
type Mechanism struct {
Mechanism uint
Parameter []byte
arena arena
}

// NewMechanism returns a pointer to an initialized Mechanism.
Expand All @@ -234,7 +233,7 @@ func NewMechanism(mech uint, x interface{}) *Mechanism {

switch x.(type) {
case *GCMParams:
m.arena, m.Parameter = cGCMParams(x.(*GCMParams))
m.Parameter = cGCMParams(x.(*GCMParams))
default:
m.Parameter = x.([]byte)
}
Expand All @@ -256,7 +255,6 @@ func cMechanismList(m []*Mechanism) (arena, C.ckMechPtr, C.CK_ULONG) {
}

pm[i].pParameter, pm[i].ulParameterLen = arena.Allocate(m[i].Parameter)
arena = append(arena, m[i].arena...)
}
return arena, C.ckMechPtr(&pm[0]), C.CK_ULONG(len(m))
}
Expand Down