Advertisement
AnthonyCagliano

Untitled

Oct 21st, 2021
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.79 KB | None | 0 0
  1. #define MPRIME_LEN (8 + RSA_SALT_SIZE + RSA_SALT_SIZE)
  2. #define MPRIME_OCTETS 0
  3. #define MPRIME_MHASH (MPRIME_OCTETS + 8)
  4. #define MPRIME_SALT (MPRIME_MHASH + RSA_SALT_SIZE)
  5.  
  6. #define DB_END (-1)
  7. #define DB_MPRIME_HASH ((DB_END) + (-RSA_SALT_SIZE))
  8. #define DB_SALT ((DB_MPRIME_HASH) + (-RSA_SALT_SIZE))
  9. #define DB_MASK_BYTE ((DB_SALT) + (-1))
  10. #define DB_PADDING_END ((DB_MASK_BYTE) + (-1))
  11. size_t hashlib_RSAEncodePSS(
  12. const uint8_t* in,
  13. size_t len,
  14. uint8_t *out,
  15. size_t modulus_len,
  16. uint8_t *salt){
  17.  
  18. uint8_t mprime_buf[MPRIME_LEN];
  19. SHA256_CTX ctx;
  20. uint32_t mbuffer[64];
  21. uint8_t hMprime[RSA_SALT_SIZE];
  22. uint8_t mgf1_digest[RSA_MODULUS_MAX - RSA_SALT_SIZE - 1];
  23. size_t db_len = modulus_len - RSA_SALT_SIZE - 1;
  24. size_t ps_len = db_len - RSA_SALT_SIZE - 1;
  25.  
  26. // errors
  27. if((in == NULL) || (out == NULL)) return 0;
  28. if((modulus_len > 256) || (modulus_len < 128)) return 0;
  29. if(len==0) return 0;
  30.  
  31. // init buffers to 0
  32. memset(out, 0, modulus_len);
  33. memset(mprime_buf, 0, MPRIME_LEN);
  34. // hash message, write to MHASH block
  35. hashlib_Sha256Init(&ctx, mbuffer);
  36. hashlib_Sha256Update(&ctx, in, len);
  37. hashlib_Sha256Final(&ctx, &mprime_buf[MPRIME_MHASH]);
  38.  
  39. // write in random oracle passed, or generate one
  40. if(salt != NULL)
  41. memcpy(&mprime_buf[MPRIME_SALT], salt, RSA_SALT_SIZE);
  42. else
  43. hashlib_RandomBytes(&mprime_buf[MPRIME_SALT], RSA_SALT_SIZE);
  44. // copy salt to DB as well
  45. memcpy(&out[modulus_len + DB_SALT], &mprime_buf[MPRIME_SALT], RSA_SALT_SIZE);
  46.  
  47. // write masking and ending bytes
  48. out[modulus_len + DB_MASK_BYTE] = 0x01;
  49. out[modulus_len + DB_END] = 0xbc;
  50.  
  51. // hash M' buffer
  52. hashlib_Sha256Init(&ctx, mbuffer);
  53. hashlib_Sha256Update(&ctx, mprime_buf, MPRIME_LEN);
  54. hashlib_Sha256Final(&ctx, hMprime);
  55.  
  56. // write hash to output hash block
  57. memcpy(&out[modulus_len + DB_MPRIME_HASH], hMprime, RSA_SALT_SIZE);
  58.  
  59. // MGF1 the hash
  60. hashlib_MGF1Hash(hMprime, RSA_SALT_SIZE, mgf1_digest, db_len);
  61.  
  62. // xor the hash with the output db block
  63. for(size_t i = 0; i < db_len; i++)
  64. out[i] ^= mgf1_digest[i];
  65.  
  66.  
  67. return modulus_len;
  68. }
  69.  
  70. bool hashlib_RSAVerifyPSS(const uint8_t *in, size_t len, const uint8_t *expected, size_t modulus_len){
  71. uint8_t mgf1_digest[RSA_MODULUS_MAX - RSA_SALT_SIZE - 1];
  72. uint8_t self_sig_buf[RSA_MODULUS_MAX];
  73. uint8_t salt[32];
  74. size_t db_len = modulus_len - RSA_SALT_SIZE - 1;
  75. memcpy(self_sig_buf, expected, modulus_len);
  76. hashlib_MGF1Hash(&self_sig_buf[modulus_len + DB_MPRIME_HASH], RSA_SALT_SIZE, mgf1_digest, db_len);
  77. for(size_t i = 0; i < db_len; i++)
  78. self_sig_buf[i] ^= mgf1_digest[i];
  79. memcpy(salt, &self_sig_buf[modulus_len + DB_SALT], 32);
  80. hashlib_RSAEncodePSS(in, len, self_sig_buf, modulus_len, salt);
  81. return hashlib_CompareDigest(self_sig_buf, expected, modulus_len);
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement