Pkcs11Interop / Pkcs11Interop

Managed .NET wrapper for unmanaged PKCS#11 libraries
Apache License 2.0
255 stars 86 forks source link

Linux : NativeULong as System.UInt32 causes error while accessing CK_GCM_PARAMS structure from PKCS11 standards #225

Closed akhilpvghi closed 1 year ago

akhilpvghi commented 1 year ago

One occurrence of NativeULong as System.UInt32 can be found at https://github.com/Pkcs11Interop/Pkcs11Interop/blob/5.1.0/src/Pkcs11Interop/HighLevelAPI40/MechanismParams/CkGcmParams.cs due to which it always returns 4 bytes whereas in Linux the size of the long is 8 bytes. And hence all the members of the CK_GCM_PARAMS structure from PKCS11 are getting filled with garbage values.

PKCS11 structure typedef struct CK_GCM_PARAMS { CK_BYTE_PTR pIv; CK_ULONG ulIvLen; CK_ULONG ulIvBits; CK_BYTE_PTR pAAD; CK_ULONG ulAADLen; CK_ULONG ulTagBits; } CK_GCM_PARAMS;

Pkcs11Interop structure

public struct CK_GCM_PARAMS { public IntPtr Iv; public NativeULong IvLen; public NativeULong IvBits; // TODO - Fix description when fixed in PKCS#11 specification public IntPtr AAD; public NativeULong AADLen; public NativeULong TagBits; }

Console.WriteLine("sizeof(UInt32): {0}", sizeof(UInt32)); Console.WriteLine("sizeof(long): {0}", sizeof(long)); ====OUTPUT=========== sizeof(UInt32): 4 sizeof(long): 8

jariq commented 1 year ago

Please read this 👉🏻 https://github.com/Pkcs11Interop/Pkcs11Interop/blob/master/doc/ARCHITECTURE.md

akhilpvghi commented 1 year ago

Thanks, We fixed it in our samples.