NCryptoki

 

NCryptoki Delphi Example

1/17/2018 8:54:39 AM
Gravatar
Total Posts 5

NCryptoki Delphi Example

Good Day

Do you perhaps have or can someone please assist me with an example of how to use the NCryptoki dll in Delphi

Your site mentions it is compatable but provides only examples for C#, VB6 and VB.net

I have tried using these example and modifying them for Delphi with out much success to date

Thank You

Mark

1/19/2018 10:14:59 AM
Gravatar
Total Posts 5

Re: NCryptoki Delphi Example

I have gotten to the point where I can instatiate an session and Login

I am trying to generate a key using GenerateKey but whatever I try I just get returned a NIL object for key with no errors

I have tried to used the C# code examples whereby you create a CrytokiCollection and then objects with attributes for this collection and adding this objects to the collection

Finally calling the session GenerateKey passing it the Mechanism and CrytokiCollection objects

No errors but no key either

please help

 

Mechanism := CoMechanism.Create;
  Mechanism.MechanismType := strtoint(CKM_AES_ECB);

  Template  := CoCryptokiCollection.Create;

  //CKA_CLASS
  ObjectAttribute := CoObjectAttribute.Create;
  ObjectAttribute.AttributeType := strtoint(CKA_CLASS);
  ObjectAttribute.AttributeValue := strtoint(CKO_SECRET_KEY);
  Template.Add(ObjectAttribute);

  //CKA_KEY_TYPE
  ObjectAttribute := CoObjectAttribute.Create;
  ObjectAttribute.AttributeType := strtoint(CKA_KEY_TYPE);
  ObjectAttribute.AttributeValue := strtoint(CKK_AES);
  Template.Add(ObjectAttribute);

  //CKA_LABEL
  ObjectAttribute := CoObjectAttribute.Create;
  ObjectAttribute.AttributeType := strtoint(CKA_LABEL);
  ObjectAttribute.AttributeValue := 'Mark Key';
  Template.Add(ObjectAttribute);

  //CKA_ID
  ObjectAttribute := CoObjectAttribute.Create;
  ObjectAttribute.AttributeType := strtoint(CKA_ID);
  ObjectAttribute.AttributeValue := 2;
  Template.Add(ObjectAttribute);

  //CKA_TOKEN
  ObjectAttribute := CoObjectAttribute.Create;
  ObjectAttribute.AttributeType := strtoint(CKA_TOKEN);
  ObjectAttribute.AttributeValue := True;
  Template.Add(ObjectAttribute);

  //CKA_PRIVATE
  ObjectAttribute := CoObjectAttribute.Create;
  ObjectAttribute.AttributeType := strtoint(CKA_PRIVATE);
  ObjectAttribute.AttributeValue := True;
  Template.Add(ObjectAttribute);

  //CKA_ENCRYPT
  ObjectAttribute := CoObjectAttribute.Create;
  ObjectAttribute.AttributeType := strtoint(CKA_ENCRYPT);
  ObjectAttribute.AttributeValue := True;
  Template.Add(ObjectAttribute);

  //CKA_DECRYPT
  ObjectAttribute := CoObjectAttribute.Create;
  ObjectAttribute.AttributeType := strtoint(CKA_DECRYPT);
  ObjectAttribute.AttributeValue := True;
  Template.Add(ObjectAttribute);

  //CKA_VALUE_LEN
  ObjectAttribute := CoObjectAttribute.Create;
  ObjectAttribute.AttributeType := strtoint(CKA_VALUE_LEN);
  ObjectAttribute.AttributeValue := 32;
  Template.Add(ObjectAttribute); 

  Key := Session.GenerateKey(Mechanism ,Template);

1/19/2018 1:16:05 PM
Gravatar
Total Posts 300
Ugo Chirico http://www.ugochirico.com

Re: NCryptoki Delphi Example

I don't understand if you was you able to generate a key in .NET code.

Consider that NCryptoki is a wrapper to an underlying PKCS#11 module. So it forwards the calls to such an underlying module and returns back the responses. Thus, usually the errors comes from the underlying module.

Which PKCS#11 module are you using?

If you was not able to generate a key in .NET there is something wrong in your code or in the undelying PKCS#11.

the method GenerateKey throws and exception if something fails. Did you get such an exception? It cannot return null without throwing an exception

Finally I don't understand why did you use strtoint  (strtoint(CKM_AES_ECB))

1/22/2018 8:09:06 AM
Gravatar
Total Posts 5

Re: NCryptoki Delphi Example

The NCryptoki.tlb imported into Delphi provides the following GenerateKey wrapper function :

function GenerateKey(const mech: _Mechanism; const attList: _CryptokiCollection): _Key; safecall;

There is no error code/messge return parameter only an _Key interface type

Which when I call the GenerateKey method returns as Nil

I have created a _Mechanism Object. What is a basic Mechanism Type that I can set just to test with? I am using AES_ECB

I have created a CryptokiCollection object and added 2 attribute objects to it, one for class of Secret Key and the other Key_Type of CKA_AES

 

1/22/2018 10:08:16 AM
Gravatar
Total Posts 5

Re: NCryptoki Delphi Example

Whoopeee

Managed to successfull generate a key in Delphi, happy days

First did the code in C# just to make sure that it could work and that my encrption library etc was all fine

Once I confirmed that this was working I then went back to Delphi to try and apply the same principal

Changes that I made were

Instead of storing the constants as Binary and doing String to Int Conversion I simply stored the Integer values instead ( This was questioned before)

I also saw in C# when doing a Key Generation it used a type called AES_KEY_GEN so I declared this variable and used this

Also instead of just assigning the value to the Mechanism Type I called the SET method instead passing it the CKM_AES_KEY_GEN and 1 paramater pair

Not entirely sure what the value 1 above represents?

https://ncryptoki.com/Forums/Thread.aspx?pageid=9&t=330~1