//Hardcoding this test code for PSS-SHA1 void RsaPssVerification(unsigned int uKeySize, const unsigned char* pMsg, const unsigned char* pSign, unsigned int E, const unsigned char* pN, const unsigned char* pD) { RSA* pRsaKey = NULL; const unsigned char* pDigest = NULL; size_t uDigestLen = 20; unsigned char EM[512]; unsigned char signature[512]; unsigned int uLen = 0; int status = 0; // Generate an RSA key pair pRsaKey = GetRsaKey(E, pN, pD); if (pRsaKey) { //Use the already hashed input message and compute the PSS padded data with max salt size pDigest = pMsg; printbin("HASH", pDigest, 20); status = RSA_padding_add_PKCS1_PSS(pRsaKey, EM, pDigest, EVP_sha1(), -2); printbin("EM", EM, uKeySize); if (status == 1) { //Now do Rsa Signature (RSA private encrypt) status = RSA_private_encrypt(uKeySize, EM, signature, pRsaKey, RSA_NO_PADDING); printbin("Sign", signature, uKeySize); if (status != -1) { //Now its time to verify the signature using RSA public decryption //We could directly use signature, but we are here to verify the signature generated by HW KM1 uLen = hex2bin(signature, pSign); //assert(uLen == uKeySize) printbin("Sign", signature, uLen); status = RSA_public_decrypt(uKeySize, signature, EM, pRsaKey, RSA_NO_PADDING); printbin("EM", EM, uKeySize); if (status != -1) { //Verify the data against the message with expecting max salt length from ssignature status = RSA_verify_PKCS1_PSS(pRsaKey, pDigest, EVP_sha1(), EM, -2); if (status == 1) { printf("GREAT: Signature verification successful\n"); } else { printf("RSA_verify_PKCS1_PSS failed with error %s\n", ERR_error_string(ERR_get_error(), NULL)); } } else { printf("RSA_public_decrypt failed with error %s\n", ERR_error_string(ERR_get_error(), NULL)); } } else { printf("RSA_private_encrypt failed with error %s\n", ERR_error_string(ERR_get_error(), NULL)); } } else { printf("RSA_padding_add_PKCS1_PSS failed with error %s\n", ERR_error_string(ERR_get_error(), NULL)); } } if (pRsaKey) { RSA_free(pRsaKey); } }
int main(char **args) { unsigned int x; printf("number to print in binary: \n"); scanf("%x",&x); printf("The binary format for %x is %s\n", x, printbin((unsigned char)x)); //printf("the for %s\n", printbin((unsigned char)x)); return 0; }
static void printhdr(uint8_t *hdr, int len) { int i, j; j = 0; for(i = 0; i < len; ++i) { printbin(hdr[i], 8); printf(" (0x%0x), ", hdr[i]); ++j; if(j == 10) { printf("\n"); j = 0; } } if(j != 0) { printf("\n"); } }