// DigestKey continues a multi-part message-digesting // operation, by digesting the value of a secret key as part of // the data already digested. func (c *Ctx) DigestKey(sh SessionHandle, key ObjectHandle) error { e := C.DigestKey(c.ctx, C.CK_SESSION_HANDLE(sh), C.CK_OBJECT_HANDLE(key)) if toError(e) != nil { return toError(e) } return nil }
/* UnwrapKey unwraps (decrypts) a wrapped key, creating a new key object. */ func (c *Ctx) UnwrapKey(sh SessionHandle, m []*Mechanism, unwrappingkey ObjectHandle, wrappedkey []byte, a []*Attribute) (ObjectHandle, error) { var key C.CK_OBJECT_HANDLE ac, aclen := cAttributeList(a) mech, _ := cMechanismList(m) e := C.UnwrapKey(c.ctx, C.CK_SESSION_HANDLE(sh), mech, C.CK_OBJECT_HANDLE(unwrappingkey), C.CK_BYTE_PTR(unsafe.Pointer(&wrappedkey[0])), C.CK_ULONG(len(wrappedkey)), ac, aclen, &key) return ObjectHandle(key), toError(e) } // DeriveKey derives a key from a base key, creating a new key object. */ func (c *Ctx) DeriveKey(sh SessionHandle, m []*Mechanism, basekey ObjectHandle, a []*Attribute) (ObjectHandle, error) { var key C.CK_OBJECT_HANDLE ac, aclen := cAttributeList(a) mech, _ := cMechanismList(m) e := C.DeriveKey(c.ctx, C.CK_SESSION_HANDLE(sh), mech, C.CK_OBJECT_HANDLE(basekey), ac, aclen, &key) return ObjectHandle(key), toError(e) }
/* Logout logs a user out from a token. */ func (c *Ctx) Logout(sh SessionHandle) error { if c.ctx == nil { return toError(CKR_CRYPTOKI_NOT_INITIALIZED) } e := C.Logout(c.ctx, C.CK_SESSION_HANDLE(sh)) return toError(e) }
/* DigestUpdate continues a multiple-part message-digesting operation. */ func (c *Ctx) DigestUpdate(sh SessionHandle, message []byte) error { e := C.DigestUpdate(c.ctx, C.CK_SESSION_HANDLE(sh), C.CK_BYTE_PTR(unsafe.Pointer(&message[0])), C.CK_ULONG(len(message))) if toError(e) != nil { return toError(e) } return nil }
/* UnwrapKey unwraps (decrypts) a wrapped key, creating a new key object. */ func (c *Ctx) UnwrapKey(sh SessionHandle, m []*Mechanism, unwrappingkey ObjectHandle, wrappedkey []byte, a []*Attribute) (ObjectHandle, error) { var key C.CK_OBJECT_HANDLE ac, aclen := cAttributeList(a) mech, _ := cMechanismList(m) e := C.UnwrapKey(c.ctx, C.CK_SESSION_HANDLE(sh), mech, C.CK_OBJECT_HANDLE(unwrappingkey), C.CK_BYTE_PTR(unsafe.Pointer(&wrappedkey[0])), C.CK_ULONG(len(wrappedkey)), ac, aclen, &key) return ObjectHandle(key), toError(e) }
/* SetPIN modifies the PIN of the user who is logged in. */ func (c *Ctx) SetPIN(sh SessionHandle, oldpin string, newpin string) error { old := C.CString(oldpin) defer C.free(unsafe.Pointer(old)) new := C.CString(newpin) defer C.free(unsafe.Pointer(new)) e := C.SetPIN(c.ctx, C.CK_SESSION_HANDLE(sh), old, C.CK_ULONG(len(oldpin)), new, C.CK_ULONG(len(newpin))) return toError(e) }
/* GenerateRandom generates random data. */ func (c *Ctx) GenerateRandom(sh SessionHandle, length int) ([]byte, error) { var rand C.CK_BYTE_PTR e := C.GenerateRandom(c.ctx, C.CK_SESSION_HANDLE(sh), &rand, C.CK_ULONG(length)) if toError(e) != nil { return nil, toError(e) } h := C.GoBytes(unsafe.Pointer(rand), C.int(length)) C.free(unsafe.Pointer(rand)) return h, nil }
/* GetSessionInfo obtains information about the session. */ func (c *Ctx) GetSessionInfo(sh SessionHandle) (SessionInfo, error) { var csi C.CK_SESSION_INFO e := C.GetSessionInfo(c.ctx, C.CK_SESSION_HANDLE(sh), &csi) s := SessionInfo{SlotID: uint(csi.slotID), State: uint(csi.state), Flags: uint(csi.flags), DeviceError: uint(csi.ulDeviceError), } return s, toError(e) }
/* CreateObject creates a new object. */ func (c *Ctx) CreateObject(sh SessionHandle, temp []*Attribute) (ObjectHandle, error) { var obj C.CK_OBJECT_HANDLE t, tcount := cAttributeList(temp) e := C.CreateObject(c.ctx, C.CK_SESSION_HANDLE(sh), t, tcount, C.CK_OBJECT_HANDLE_PTR(&obj)) e1 := toError(e) if e1 == nil { return ObjectHandle(obj), nil } return 0, e1 }
/* GenerateKey generates a secret key, creating a new key object. */ func (c *Ctx) GenerateKey(sh SessionHandle, m []*Mechanism, temp []*Attribute) (ObjectHandle, error) { var key C.CK_OBJECT_HANDLE t, tcount := cAttributeList(temp) mech, _ := cMechanismList(m) e := C.GenerateKey(c.ctx, C.CK_SESSION_HANDLE(sh), mech, t, tcount, C.CK_OBJECT_HANDLE_PTR(&key)) e1 := toError(e) if e1 == nil { return ObjectHandle(key), nil } return 0, e1 }
/* Decrypt decrypts encrypted data in a single part. */ func (c *Ctx) Decrypt(sh SessionHandle, cypher []byte) ([]byte, error) { var ( plain C.CK_BYTE_PTR plainlen C.CK_ULONG ) e := C.Decrypt(c.ctx, C.CK_SESSION_HANDLE(sh), C.CK_BYTE_PTR(unsafe.Pointer(&cypher[0])), C.CK_ULONG(len(cypher)), &plain, &plainlen) if toError(e) != nil { return nil, toError(e) } s := C.GoBytes(unsafe.Pointer(plain), C.int(plainlen)) C.free(unsafe.Pointer(plain)) return s, nil }
/* DecryptFinal finishes a multiple-part decryption operation. */ func (c *Ctx) DecryptFinal(sh SessionHandle) ([]byte, error) { var ( plain C.CK_BYTE_PTR plainlen C.CK_ULONG ) e := C.DecryptFinal(c.ctx, C.CK_SESSION_HANDLE(sh), &plain, &plainlen) if toError(e) != nil { return nil, toError(e) } h := C.GoBytes(unsafe.Pointer(plain), C.int(plainlen)) C.free(unsafe.Pointer(plain)) return h, nil }
/* GetOperationState obtains the state of the cryptographic operation in a session. */ func (c *Ctx) GetOperationState(sh SessionHandle) ([]byte, error) { var ( state C.CK_BYTE_PTR statelen C.CK_ULONG ) e := C.GetOperationState(c.ctx, C.CK_SESSION_HANDLE(sh), &state, &statelen) if toError(e) != nil { return nil, toError(e) } b := C.GoBytes(unsafe.Pointer(state), C.int(statelen)) C.free(unsafe.Pointer(state)) return b, nil }
/* DigestFinal finishes a multiple-part message-digesting operation. */ func (c *Ctx) DigestFinal(sh SessionHandle) ([]byte, error) { var ( hash C.CK_BYTE_PTR hashlen C.CK_ULONG ) e := C.DigestFinal(c.ctx, C.CK_SESSION_HANDLE(sh), &hash, &hashlen) if toError(e) != nil { return nil, toError(e) } h := C.GoBytes(unsafe.Pointer(hash), C.int(hashlen)) C.free(unsafe.Pointer(hash)) return h, nil }
/* Encrypt encrypts single-part data. */ func (c *Ctx) Encrypt(sh SessionHandle, message []byte) ([]byte, error) { var ( enc C.CK_BYTE_PTR enclen C.CK_ULONG ) e := C.Encrypt(c.ctx, C.CK_SESSION_HANDLE(sh), C.CK_BYTE_PTR(unsafe.Pointer(&message[0])), C.CK_ULONG(len(message)), &enc, &enclen) if toError(e) != nil { return nil, toError(e) } s := C.GoBytes(unsafe.Pointer(enc), C.int(enclen)) C.free(unsafe.Pointer(enc)) return s, nil }
/* DecryptVerifyUpdate continues a multiple-part decryption and verify operation. */ func (c *Ctx) DecryptVerifyUpdate(sh SessionHandle, cipher []byte) ([]byte, error) { var ( part C.CK_BYTE_PTR partlen C.CK_ULONG ) e := C.DecryptVerifyUpdate(c.ctx, C.CK_SESSION_HANDLE(sh), C.CK_BYTE_PTR(unsafe.Pointer(&cipher[0])), C.CK_ULONG(len(cipher)), &part, &partlen) if toError(e) != nil { return nil, toError(e) } h := C.GoBytes(unsafe.Pointer(part), C.int(partlen)) C.free(unsafe.Pointer(part)) return h, nil }
// Sign signs (encrypts with private key) data in a single part, where the signature // is (will be) an appendix to the data, and plaintext cannot be recovered from the signature. func (c *Ctx) Sign(sh SessionHandle, message []byte) ([]byte, error) { var ( sig C.CK_BYTE_PTR siglen C.CK_ULONG ) e := C.Sign(c.ctx, C.CK_SESSION_HANDLE(sh), C.CK_BYTE_PTR(unsafe.Pointer(&message[0])), C.CK_ULONG(len(message)), &sig, &siglen) if toError(e) != nil { return nil, toError(e) } s := C.GoBytes(unsafe.Pointer(sig), C.int(siglen)) C.free(unsafe.Pointer(sig)) return s, nil }
/* SignEncryptUpdate continues a multiple-part signing and encryption operation. */ func (c *Ctx) SignEncryptUpdate(sh SessionHandle, part []byte) ([]byte, error) { var ( enc C.CK_BYTE_PTR enclen C.CK_ULONG ) e := C.SignEncryptUpdate(c.ctx, C.CK_SESSION_HANDLE(sh), C.CK_BYTE_PTR(unsafe.Pointer(&part[0])), C.CK_ULONG(len(part)), &enc, &enclen) if toError(e) != nil { return nil, toError(e) } h := C.GoBytes(unsafe.Pointer(enc), C.int(enclen)) C.free(unsafe.Pointer(enc)) return h, nil }
// VerifyRecover verifies a signature in a single-part // operation, where the data is recovered from the signature. func (c *Ctx) VerifyRecover(sh SessionHandle, signature []byte) ([]byte, error) { var ( data C.CK_BYTE_PTR datalen C.CK_ULONG ) e := C.DecryptVerifyUpdate(c.ctx, C.CK_SESSION_HANDLE(sh), C.CK_BYTE_PTR(unsafe.Pointer(&signature[0])), C.CK_ULONG(len(signature)), &data, &datalen) if toError(e) != nil { return nil, toError(e) } h := C.GoBytes(unsafe.Pointer(data), C.int(datalen)) C.free(unsafe.Pointer(data)) return h, nil }
// EncryptFinal finishes a multiple-part encryption operation. func (c *Ctx) EncryptFinal(sh SessionHandle) ([]byte, error) { var ( enc C.CK_BYTE_PTR enclen C.CK_ULONG ) e := C.EncryptFinal(c.ctx, C.CK_SESSION_HANDLE(sh), &enc, &enclen) if toError(e) != nil { return nil, toError(e) } h := C.GoBytes(unsafe.Pointer(enc), C.int(enclen)) C.free(unsafe.Pointer(enc)) return h, nil }
// SignRecover signs data in a single operation, where the // data can be recovered from the signature. func (c *Ctx) SignRecover(sh SessionHandle, data []byte) ([]byte, error) { var ( sig C.CK_BYTE_PTR siglen C.CK_ULONG ) e := C.SignRecover(c.ctx, C.CK_SESSION_HANDLE(sh), C.CK_BYTE_PTR(unsafe.Pointer(&data[0])), C.CK_ULONG(len(data)), &sig, &siglen) if toError(e) != nil { return nil, toError(e) } h := C.GoBytes(unsafe.Pointer(sig), C.int(siglen)) C.free(unsafe.Pointer(sig)) return h, nil }
/* WrapKey wraps (i.e., encrypts) a key. */ func (c *Ctx) WrapKey(sh SessionHandle, m []*Mechanism, wrappingkey, key ObjectHandle) ([]byte, error) { var ( wrappedkey C.CK_BYTE_PTR wrappedkeylen C.CK_ULONG ) mech, _ := cMechanismList(m) e := C.WrapKey(c.ctx, C.CK_SESSION_HANDLE(sh), mech, C.CK_OBJECT_HANDLE(wrappingkey), C.CK_OBJECT_HANDLE(key), &wrappedkey, &wrappedkeylen) if toError(e) != nil { return nil, toError(e) } h := C.GoBytes(unsafe.Pointer(wrappedkey), C.int(wrappedkeylen)) C.free(unsafe.Pointer(wrappedkey)) return h, nil }
/* GenerateKeyPair generates a public-key/private-key pair creating new key objects. */ func (c *Ctx) GenerateKeyPair(sh SessionHandle, m []*Mechanism, public, private []*Attribute) (ObjectHandle, ObjectHandle, error) { var ( pubkey C.CK_OBJECT_HANDLE privkey C.CK_OBJECT_HANDLE ) pub, pubcount := cAttributeList(public) priv, privcount := cAttributeList(private) mech, _ := cMechanismList(m) e := C.GenerateKeyPair(c.ctx, C.CK_SESSION_HANDLE(sh), mech, pub, pubcount, priv, privcount, C.CK_OBJECT_HANDLE_PTR(&pubkey), C.CK_OBJECT_HANDLE_PTR(&privkey)) e1 := toError(e) if e1 == nil { return ObjectHandle(pubkey), ObjectHandle(privkey), nil } return 0, 0, e1 }
// FindObjects continues a search for token and session // objects that match a template, obtaining additional object // handles. The returned boolean indicates if the list would // have been larger than max. func (c *Ctx) FindObjects(sh SessionHandle, max int) ([]ObjectHandle, bool, error) { var ( objectList C.CK_OBJECT_HANDLE_PTR ulCount C.CK_ULONG ) e := C.FindObjects(c.ctx, C.CK_SESSION_HANDLE(sh), &objectList, C.CK_ULONG(max), &ulCount) if toError(e) != nil { return nil, false, toError(e) } l := toList(C.CK_ULONG_PTR(unsafe.Pointer(objectList)), ulCount) // Make again a new list of the correct type. // This is copying data, but this is not an often used function. o := make([]ObjectHandle, len(l)) for i, v := range l { o[i] = ObjectHandle(v) } return o, ulCount > C.CK_ULONG(max), nil }
/* GetAttributeValue obtains the value of one or more object attributes. */ func (c *Ctx) GetAttributeValue(sh SessionHandle, o ObjectHandle, a []*Attribute) ([]*Attribute, error) { // copy the attribute list and make all the values nil, so that // the C function can (allocate) fill them in pa := make([]C.CK_ATTRIBUTE, len(a)) for i := 0; i < len(a); i++ { pa[i]._type = C.CK_ATTRIBUTE_TYPE(a[i].Type) } e := C.GetAttributeValue(c.ctx, C.CK_SESSION_HANDLE(sh), C.CK_OBJECT_HANDLE(o), C.CK_ATTRIBUTE_PTR(&pa[0]), C.CK_ULONG(len(a))) if toError(e) != nil { return nil, toError(e) } a1 := make([]*Attribute, len(a)) for i, c := range pa { x := new(Attribute) x.Type = uint(c._type) if int(c.ulValueLen) != -1 { x.Value = C.GoBytes(unsafe.Pointer(c.pValue), C.int(c.ulValueLen)) C.free(unsafe.Pointer(c.pValue)) } a1[i] = x } return a1, nil }
// SeedRandom mixes additional seed material into the token's // random number generator. func (c *Ctx) SeedRandom(sh SessionHandle, seed []byte) error { e := C.SeedRandom(c.ctx, C.CK_SESSION_HANDLE(sh), C.CK_BYTE_PTR(unsafe.Pointer(&seed[0])), C.CK_ULONG(len(seed))) return toError(e) }
// VerifyRecoverInit initializes a signature verification // operation, where the data is recovered from the signature. func (c *Ctx) VerifyRecoverInit(sh SessionHandle, m []*Mechanism, key ObjectHandle) error { mech, _ := cMechanismList(m) e := C.VerifyRecoverInit(c.ctx, C.CK_SESSION_HANDLE(sh), mech, C.CK_OBJECT_HANDLE(key)) return toError(e) }
/* SetOperationState restores the state of the cryptographic operation in a session. */ func (c *Ctx) SetOperationState(sh SessionHandle, state []byte, encryptKey, authKey ObjectHandle) error { e := C.SetOperationState(c.ctx, C.CK_SESSION_HANDLE(sh), C.CK_BYTE_PTR(unsafe.Pointer(&state[0])), C.CK_ULONG(len(state)), C.CK_OBJECT_HANDLE(encryptKey), C.CK_OBJECT_HANDLE(authKey)) return toError(e) }
/* Login logs a user into a token. */ func (c *Ctx) Login(sh SessionHandle, userType uint, pin string) error { p := C.CString(pin) defer C.free(unsafe.Pointer(p)) e := C.Login(c.ctx, C.CK_SESSION_HANDLE(sh), C.CK_USER_TYPE(userType), p, C.CK_ULONG(len(pin))) return toError(e) }
/* InitPIN initializes the normal user's PIN. */ func (c *Ctx) InitPIN(sh SessionHandle, pin string) error { p := C.CString(pin) defer C.free(unsafe.Pointer(p)) e := C.InitPIN(c.ctx, C.CK_SESSION_HANDLE(sh), p, C.CK_ULONG(len(pin))) return toError(e) }