Skip to content

Can't derive ECDH secret #51

@pentiak

Description

@pentiak

I am trying to derive a shared secret between two EC parties, but I have difficulties providing correct CKM. As there is no CK_ECDH1_DERIVE_PARAMS struct defined, I am trying to allocate native memory myself, but without luck so far.

Can you advise what I am doing wrong?

    public long deriveECDH(long session, byte[] otherPublicKey, long privateKeyHandle) {
    CKA[] secretTemplate = new CKA[]{
            new CKA(CKA.TOKEN, true),
            new CKA(CKA.CLASS, CKO.SECRET_KEY),
            new CKA(CKA.KEY_TYPE, CKK.GENERIC_SECRET),
            new CKA(CKA.SENSITIVE, false),
            new CKA(CKA.EXTRACTABLE, true)
    };

    Memory deriveParam = new Memory((long) NativeLong.SIZE + NativeLong.SIZE + Native.POINTER_SIZE + NativeLong.SIZE + Native.POINTER_SIZE);
    int offset = 0;
    deriveParam.setLong(offset, CKD.NULL);
    offset += NativeLong.SIZE;
    deriveParam.setLong(offset, 0L);
    offset += NativeLong.SIZE;
    deriveParam.setPointer(offset, Pointer.NULL);
    offset += Native.POINTER_SIZE;
    deriveParam.setLong(offset, otherPublicKey.length);
    offset += NativeLong.SIZE;
    Memory pubKeyPointer = new Memory(otherPublicKey.length);
    pubKeyPointer.write(0, otherPublicKey, 0, otherPublicKey.length);
    deriveParam.setPointer(offset, pubKeyPointer);

    return CE.DeriveKey(session, new CKM(CKM.ECDH1_DERIVE, deriveParam, (int) deriveParam.size()), privateKeyHandle, secretTemplate);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions