CKR_DATA_LEN_RANGE = 0x00000021

1

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?

c#
pinvoke
pkcs#11
asked on Stack Overflow May 18, 2011 by loupoo • edited Mar 2, 2012 by Gilles 'SO- stop being evil'

1 Answer

2

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);
answered on Stack Overflow May 19, 2011 by loupoo • edited Mar 2, 2012 by Gilles 'SO- stop being evil'

User contributions licensed under CC BY-SA 3.0