I'm building a 32 bits embedded device that an external actor can communicate with through a PKCS#11 interface.
Basically there are 3 components :
- Our embedded device (only supports 32 bits)
- The host (may be 32 or 64 bits, not up to me)
- The host library (can be compiled in 32 or 64 bits)
I'm developing both the device and the host library and my main issue right now is to ensure compatibility with both 32 and 64 bits host applications.
The pkcs11t.h defines CK_ULONG as such :
/* an unsigned value, at least 32 bits long */
typedef unsigned long int CK_ULONG;
The thing is that long int
size won't be the same on the host (8 bytes) and on our device (4 bytes).
I was tempted to use uint32_t
instead of unsigned long int
. But the standard states the following :
It follows that many of the data and pointer types will vary somewhat from one environment to another (e.g., a CK_ULONG will sometimes be 32 bits, and sometimes perhaps 64 bits). However, these details should not affect an application, assuming it is compiled with Cryptoki header files consistent with the Cryptoki library to which the application is linked.
The host will send requests to the embedded device, let's say the host sends a request ID of 0
. How the embedded device will know if it's a 32 or a 64 bits value ?
So my question is the following : what's the best way to handle 64 bits data types in a 32 bits embedded device ? I was thinking about something like that on the host's library :
CK_ULONG data = 0x42; // user input, could be anything
#ifdef HOST_64
assert(data <= 4294967295);
#endif
send_data_to_device((uint32_t)data);
But it feels wrong...