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

Adding DesCBCEncryptDataParams, and KeyDerivationStringData support #123

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
63 changes: 63 additions & 0 deletions params.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@ static inline void putECDH1PublicParams(CK_ECDH1_DERIVE_PARAMS_PTR params, CK_VO
params->pPublicData = pPublicData;
params->ulPublicDataLen = ulPublicDataLen;
}

static inline void putDesCBCEncryptDataParams(CK_DES_CBC_ENCRYPT_DATA_PARAMS_PTR params, CK_VOID_PTR pData, CK_ULONG length)
{
params->pData = pData;
params->length = length;
}

static inline void putKeyDerivationStringData(CK_KEY_DERIVATION_STRING_DATA_PTR params, CK_VOID_PTR pData, CK_ULONG ulLen)
{
params->pData = pData;
params->ulLen = ulLen;
}
*/
import "C"
import "unsafe"
Expand Down Expand Up @@ -188,3 +200,54 @@ func cECDH1DeriveParams(p *ECDH1DeriveParams, arena arena) ([]byte, arena) {

return C.GoBytes(unsafe.Pointer(&params), C.int(unsafe.Sizeof(params))), arena
}

type KeyDerivationStringData struct {
params *C.CK_KEY_DERIVATION_STRING_DATA
ksn []byte
}

func NewKeyDerivationStringData(ksn []byte) *KeyDerivationStringData {
return &KeyDerivationStringData{
ksn: ksn,
}
}

func cKeyDerivationStringData(p *KeyDerivationStringData, arena arena) ([]byte, arena) {
params := C.CK_KEY_DERIVATION_STRING_DATA{}
ksn, ksnLen := arena.Allocate(p.ksn)
params.pData = C.CK_BYTE_PTR(ksn)
params.ulLen = C.CK_ULONG(ksnLen)

p.params = &params

return C.GoBytes(unsafe.Pointer(&params), C.int(unsafe.Sizeof(params))), arena
}

// DesCBCEncryptDataParams can be passed with CKM_DES3_CBC_ENCRYPT_DATA
type DesCBCEncryptDataParams struct {
params *C.CK_DES_CBC_ENCRYPT_DATA_PARAMS
iv []byte
ksn []byte
}

func NewDesCBCEncryptDataParams(iv, ksn []byte) *DesCBCEncryptDataParams {
return &DesCBCEncryptDataParams{
iv: iv,
ksn: ksn,
}
}

func cDesCBCEncryptDataParams(p *DesCBCEncryptDataParams, ivd []byte, arena arena) ([]byte, arena) {
params := C.CK_DES_CBC_ENCRYPT_DATA_PARAMS{}

pIV := C.CBytes(ivd)
C.memcpy(unsafe.Pointer(&params.iv), pIV, 8)

ksn, ksnLen := arena.Allocate(p.ksn)

C.putDesCBCEncryptDataParams(&params, ksn, ksnLen)

p.params = &params
return C.GoBytes(unsafe.Pointer(&params), C.int(unsafe.Sizeof(params))), arena

}
137 changes: 137 additions & 0 deletions params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,50 @@ func getRSA(t *testing.T, p *Ctx, sh SessionHandle) (pub, priv ObjectHandle) {
return
}

// derveECBKey derives DES3_ECB
func deriveECBKey(p *Ctx, session SessionHandle, bdk ObjectHandle, ksn []byte) ObjectHandle {
template := []*Attribute{
NewAttribute(CKA_KEY_TYPE, CKK_DES2),
NewAttribute(CKA_CLASS, CKO_SECRET_KEY),
NewAttribute(CKA_PRIVATE, false),
NewAttribute(CKA_ENCRYPT, true),
NewAttribute(CKA_DECRYPT, true),
NewAttribute(CKA_SENSITIVE, false),
NewAttribute(CKA_EXTRACTABLE, true),
}
params := NewKeyDerivationStringData(ksn)
mech := []*Mechanism{NewMechanism(CKM_DES3_ECB_ENCRYPT_DATA, params)}
sessionKey, err := p.DeriveKey(session, mech, bdk, template)

if err != nil {
panic(err)
}

return sessionKey
}

// derveKey derives DES3_CBC
func deriveKey(p *Ctx, session SessionHandle, bdk ObjectHandle, ksn []byte) ObjectHandle {
template := []*Attribute{
NewAttribute(CKA_KEY_TYPE, CKK_DES2),
NewAttribute(CKA_CLASS, CKO_SECRET_KEY),
NewAttribute(CKA_PRIVATE, false),
NewAttribute(CKA_ENCRYPT, true),
NewAttribute(CKA_DECRYPT, true),
NewAttribute(CKA_SENSITIVE, false),
NewAttribute(CKA_EXTRACTABLE, true),
}
params := NewDesCBCEncryptDataParams([]byte{0, 0, 0, 0, 0, 0, 0, 0}, ksn)
mech := []*Mechanism{NewMechanism(CKM_DES3_CBC_ENCRYPT_DATA, params)}
sessionKey, err := p.DeriveKey(session, mech, bdk, template)

if err != nil {
panic(err)
}

return sessionKey
}

func TestPSSParams(t *testing.T) {
p := setenv(t)
sh := getSession(p, t)
Expand Down Expand Up @@ -156,3 +200,96 @@ func TestGCMParams(t *testing.T) {
}
params.Free()
}

func TestDesCBCEncryptDataParams(t *testing.T) {
p := setenv(t)
sh := getSession(p, t)
defer finishSession(p, sh)
needMech(t, p, sh, CKM_DES3_CBC_ENCRYPT_DATA)

key, err := p.GenerateKey(sh, []*Mechanism{NewMechanism(CKM_DES2_KEY_GEN, nil)}, []*Attribute{
NewAttribute(CKA_TOKEN, false),
NewAttribute(CKA_DECRYPT, true),
NewAttribute(CKA_ENCRYPT, true),
NewAttribute(CKA_DERIVE, true),
})
if err != nil {
t.Fatal("GenerateKey:", err)
}

ksn := []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16}
data := []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x30, 0x31, 0x32}

dKey := deriveKey(p, sh, key, ksn)
encryptMech := []*Mechanism{NewMechanism(CKM_DES3_ECB, nil)}
if err := p.EncryptInit(sh, encryptMech, dKey); err != nil {
t.Fatal("EncryptInit:", err)
}
cipherText, err := p.Encrypt(sh, data)
if err != nil {
t.Fatal("Encrypt:", err)
}

err = p.DecryptInit(sh, encryptMech, dKey)
if err != nil {
t.Fatalf("Could not initiate Decrypt operation: %v", err)
}
decryptedText, err := p.Decrypt(sh, cipherText)
if err != nil {
t.Fatalf("Could not perform decryption: %v", err)
}
if len(decryptedText) != len(data) {
t.Logf("decrypted string %v, original data string %v does not match", string(decryptedText), string(data))
t.Fatalf("decrypted string %v, original data string %v does not match", string(decryptedText), string(data))
}
if string(decryptedText) != string(data) {
t.Fatalf("decrypted string %v, original data string %v does not match", string(decryptedText), string(data))
}

}

func TestKeyDerivationStringData(t *testing.T) {
p := setenv(t)
sh := getSession(p, t)
defer finishSession(p, sh)
needMech(t, p, sh, CKM_DES3_ECB_ENCRYPT_DATA)

key, err := p.GenerateKey(sh, []*Mechanism{NewMechanism(CKM_DES2_KEY_GEN, nil)}, []*Attribute{
NewAttribute(CKA_TOKEN, false),
NewAttribute(CKA_DECRYPT, true),
NewAttribute(CKA_ENCRYPT, true),
NewAttribute(CKA_DERIVE, true),
})
if err != nil {
t.Fatal("GenerateKey:", err)
}

ksn := []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16}
data := []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x30, 0x31, 0x32}

dKey := deriveECBKey(p, sh, key, ksn)
encryptMech := []*Mechanism{NewMechanism(CKM_DES3_ECB, nil)}
if err := p.EncryptInit(sh, encryptMech, dKey); err != nil {
t.Fatal("EncryptInit:", err)
}
cipherText, err := p.Encrypt(sh, data)
if err != nil {
t.Fatal("Encrypt:", err)
}

err = p.DecryptInit(sh, encryptMech, dKey)
if err != nil {
t.Fatalf("Could not initiate Decrypt operation: %v", err)
}
decryptedText, err := p.Decrypt(sh, cipherText)
if err != nil {
t.Fatalf("Could not perform decryption: %v", err)
}
if len(decryptedText) != len(data) {
t.Logf("decrypted string %v, original data string %v does not match", string(decryptedText), string(data))
t.Fatalf("decrypted string %v, original data string %v does not match", string(decryptedText), string(data))
}
if string(decryptedText) != string(data) {
t.Fatalf("decrypted string %v, original data string %v does not match", string(decryptedText), string(data))
}
}
6 changes: 5 additions & 1 deletion types.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ func NewMechanism(mech uint, x interface{}) *Mechanism {
}

switch p := x.(type) {
case *GCMParams, *OAEPParams, *ECDH1DeriveParams:
case *GCMParams, *OAEPParams, *ECDH1DeriveParams, *DesCBCEncryptDataParams, *KeyDerivationStringData:
// contains pointers; defer serialization until cMechanism
m.generator = p
case []byte:
Expand Down Expand Up @@ -272,6 +272,10 @@ func cMechanism(mechList []*Mechanism) (arena, *C.CK_MECHANISM) {
param, arena = cOAEPParams(p, arena)
case *ECDH1DeriveParams:
param, arena = cECDH1DeriveParams(p, arena)
case *DesCBCEncryptDataParams:
param, arena = cDesCBCEncryptDataParams(p, p.iv, arena)
case *KeyDerivationStringData:
param, arena = cKeyDerivationStringData(p, arena)
}
if len(param) != 0 {
buf, len := arena.Allocate(param)
Expand Down