loupoo
loupoo

Reputation: 31

CKR_DATA_LEN_RANGE = 0x00000021

I tried to pinvoke a PKCS#11 function in C#. I have a CKR_DATA_LEN_RANGE error in this C# code:

[DllImport("D:/Program Files/Eracom/ProtectToolkit C SDK/bin/sw/cryptoki.dll",
           SetLastError = true)]
private static extern UInt32 C_Encrypt(CK_SESSION_HANDLE hSession,
                                       IntPtr pData, CK_ULONG ulDataLen,
                                       out IntPtr pEncryptedData,out CK_ULONG pulEncryptedData);

/* ...Main... */
CK_BYTE[] text = new CK_BYTE[] {1,2,3,4,5,6,7,8};

System.UInt32 t, tt = (System.UInt32)text.Length;
IntPtr pdata = Marshal.AllocHGlobal(text[0]*text.Length);
Marshal.Copy(text, 0, pdata, text.Length);

IntPtr chif = IntPtr.Zero;
tt = (System.UInt32)Marshal.SizeOf(pdata);
rv = C_Encrypt(h, pdata, tt,out chif,out t);

What could be causing this error?

Upvotes: 1

Views: 2648

Answers (1)

loupoo
loupoo

Reputation: 31

I resolved the problem by my self. The C_Encrypt function takes byte arrays as arguments, not pointers to integers; this made my size computations wrong.

    [DllImport("D:/Program Files/Eracom/ProtectToolkit C SDK/bin/sw/cryptoki.dll",
               SetLastError = true)]
    private static extern UInt32 C_Encrypt(CK_SESSION_HANDLE hSession,
                                           CK_BYTE[] pData, CK_ULONG ulDataLen,
                                           CK_BYTE[] pEncryptedData, ref CK_ULONG pulEncryptedData);

Upvotes: 2

Related Questions