0

I have a key in HSM

I want to derive another key from it and use it to SHA1 HMAC some data

my problem is that CKM_CONCATENATE_DATA_AND_BASE mechanism does not work as expected.

I think it work as below:

if my key (BASE) is "11 22 33 44 55 66 77 88" and I want to add "00" (DATA) to its start i want to new key be "00 11 22 33 44 55 66 77 88" but generated (and saved) key is "00 11 22 33 44 55 66 77"

derived key length will not change and one byte from end of original key will drop!

what should I do?

I try to add ObjectAttribute VALUE_LEN. but derived key length doesn't change.

    static void PkcsTest()
        {
            string p11lib = "_pkcs11.dll";
            var factories = new Pkcs11InteropFactories();
            var library = factories.Pkcs11LibraryFactory.LoadPkcs11Library(factories, p11lib, AppType.MultiThreaded);
            var slots = library.GetSlotList(SlotsType.WithTokenPresent);
            var slot = slots[0];
            var session = slot.OpenSession(SessionType.ReadWrite);
            session.Login(CKU.CKU_USER, "123456");

            var data = new byte[] { 0x68, 0x65, 0x6c, 0x6c, 0x6f };
            var originalKey = new byte[]{0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99};
            var dataToAddToKey = new byte[] { 0x00 };
            //Import key to HSM
            var objectAttributes = new List<IObjectAttribute>
            {
                session.Factories.ObjectAttributeFactory.Create(CKA.CKA_CLASS, CKO.CKO_SECRET_KEY),
                session.Factories.ObjectAttributeFactory.Create(CKA.CKA_KEY_TYPE, CKK.CKK_GENERIC_SECRET),
                session.Factories.ObjectAttributeFactory.Create(CKA.CKA_VALUE,originalKey ),
                session.Factories.ObjectAttributeFactory.Create(CKA.CKA_DERIVE,true ),
            };
            var originalKeyHandler = session.CreateObject(objectAttributes);
            //creating extended key
            var mechanismParams = session.Factories.MechanismParamsFactory.CreateCkKeyDerivationStringData(dataToAddToKey);
            var mechanismType = session.Factories.MechanismFactory.Create(CKM.CKM_CONCATENATE_DATA_AND_BASE, mechanismParams);
            var extendedKeyHandler = session.DeriveKey(mechanismType, originalKeyHandler, null);
            //generating SHA1 HMAC 
            var hmacMechanism = session.Factories.MechanismFactory.Create(CKM.CKM_SHA_1_HMAC);
            var hash = session.Sign(hmacMechanism, extendedKeyHandler, data);
            //expected : 04FE079DA27D1D8A8EB812A0BCBE1D264D0A6D7C
            //result   : 31A559C06CE465831D354A3EE058A1C519180E87
        }

here's my result using System.Security.Cryptography.HMACSHA1:

data is "0x68, 0x65, 0x6c, 0x6c, 0x6f" in all tests
KEY:1122334455667788112233445566778899 
HASH: F1D20BBBDD3D07D883827F64A55DB58BD2136C9F
KEY:001122334455667788112233445566778899 
HASH: 04FE079DA27D1D8A8EB812A0BCBE1D264D0A6D7C
KEY:0011223344556677881122334455667788 
HASH:31A559C06CE465831D354A3EE058A1C519180E87

and my result using PKCS with "1122334455667788112233445566778899" as original key

Sign with Original Key : F1D20BBBDD3D07D883827F64A55DB58BD2136C9F
sign with derived key  : 31A559C06CE465831D354A3EE058A1C519180E87
expected result        : 04FE079DA27D1D8A8EB812A0BCBE1D264D0A6D7C

UPDATE 1:

key size after Derive with Concatenate mechanism always stays same as original key. I try to add data AFTER key (with CKM_CONCATENATE_BASE_AND_DATA) with any size of data and nothing happened. the derived key and original key are same. because HSM adds n bytes to original key and drop that again (so that key size stay same which I dont want to)

if any one has an example of derive a key using CKM_CONCATENATE_BASE_AND_DATA please let me know

  • Can key be any arbitrary length? If that's the case, then if data were 30 bytes, that would be a much larger key after concatenation. – Anthony Jul 24 '19 at 19:21
  • What happens if you don't specify a key type? Looking at the documentation, specifying a type will limit it to the type's length https://www.cryptsoft.com/pkcs11doc/v211/group__SEC__12__41__3__CONCATENATION__OF__DATA__AND__A__BASE__KEY.html – Anthony Jul 24 '19 at 20:34
  • @Anthony my real usage is 48 byte key as BASE and 2 bytes as DATA to derive. then final key must be be 50 bytes. but I simplified problem for stackoverflow. – misaq saadat Jul 25 '19 at 09:59
  • If I don't specify key type I get inconsistent template error or something – misaq saadat Jul 25 '19 at 10:01

0 Answers0