Thanks for the interest in my “journey” so to call it…
At the end I came to the conclusion that a NIF is the fastest and safest way to approach it, and I have it running all right and for good .
Since you took the effort to share the pain with me, I’m giving away a couple of “gems” I might have found along the path
- Calling the
SHA1_Init
ACTUALLY does load the h0,h1… etc… with the exact values of the wpa_supplicant code , so I can at least avoid the memcpy hack.
- The very rare man pages I found for
SHA1_transform
say (quote):
The SHA1Transform() function is used by SHA1Update() to hash 512-bit blocks and forms the core of the algorithm. Most programs should use the interface provided by SHA1Init(), SHA1Update() and SHA1Final() instead of calling SHA1Transform() directly.
In other words I can at least avoid the SHA1_Transform (which spits out a fixed 64 bytes output) and use the more broadly common SHA1_Update.
- While I can leverage the SHA1_Init, I cannot use is the SHA1_Final.
I’ve written few tests to verify this
char data[64];
char out[20];
memset(out, 0, 20);
memset(data, 42, 64);
SHA_CTX context;
SHA1_Init(&context);
SHA1_Update(&context, data, 64); // here Nl=0x200
SHA1_Final(out, &context);
printf("\nUpdate+Final");
for (int i = 0; i < 20; i++) printf("0x%02X ", (uint8_t) out[i]);
memset(out, 0, 20);
memset(data, 42, 64);
SHA1_Init(&context);
SHA1_Transform(&context, data); // here Nl=0x0
SHA1_Final(out, &context);
printf("\nTransform+Final");
for (int i = 0; i < 20; i++) printf("0x%02X ", (uint8_t) out[i]);
memset(out, 0, 20);
memset(data, 42, 64);
SHA1_Init(&context);
SHA1_Transform(&context, data); // here Nl=0x0
memcpy(out, &context.h0, 20);
printf("\nTransform+plain memcpy");
for (int i = 0; i < 20; i++) printf("0x%02X ", (uint8_t) out[i]);
memset(out, 0, 20);
memset(data, 42, 64);
SHA1_Init(&context);
SHA1_Update(&context, data, 64); // here Nl=0x0
memcpy(out, &context.h0, 20);
printf("\nUpdate+plain memcpy");
for (int i = 0; i < 20; i++) printf("0x%02X ", (uint8_t) out[i]);
and got these results
Update+Final0xFE 0x5E 0x8D 0x0F 0x87 0x8E 0x68 0xF7 0x53 0x87 0xFE 0x94 0x77 0xC3 0x82 0x77 0x23 0x52 0xB0 0x1C
Transform+Final0xE1 0x95 0x64 0xDB 0xEE 0xC6 0x2C 0x25 0xCB 0x15 0xAA 0x84 0xF7 0x52 0x35 0x33 0x86 0x86 0x3E 0xB9
Transform+plain memcpy0x81 0x71 0x1E 0x1C 0x5F 0xE0 0x67 0x60 0xF7 0x1C 0x6E 0xD4 0x52 0x3C 0x44 0x71 0x31 0xAD 0x27 0x39
Update+plain memcpy0x81 0x71 0x1E 0x1C 0x5F 0xE0 0x67 0x60 0xF7 0x1C 0x6E 0xD4 0x52 0x3C 0x44 0x71 0x31 0xAD 0x27 0x39
So in the end the actual only thing that I cannot do (and for which I need the NIF) is to access the SHA1 context state and use it as an output.