Advertisement
AnthonyCagliano

Untitled

Jan 12th, 2022
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1. #define PBKDF2_SALT_LEN 16
  2. #define SHA256_OUTSIZE 32
  3. hashlib_PBKDF2(const uint8_t* password, size_t plen, uint8_t* out, const uint8_t* salt, size_t iter_count, size_t keylen){
  4.  
  5. struct { uint8_t salt[16]; uint32_t block; } saltbuffer;
  6. uint8_t sha_buffer[SHA256_OUTSIZE];
  7. size_t outlen;
  8. SHA256_CTX ctx;
  9. uint32_t mbuffer[64];
  10. memcpy(saltbuffer.salt, salt, sizeof saltbuffer.salt);
  11. size_t block = 0;
  12.  
  13. //HASH = sha256
  14. // DK = T(i) for i in 0 => (keylen/HASH), concat
  15.  
  16. for(outlen = 0; outlen < keylen; outlen+=32, block++){
  17. //T(i) = F(password, salt, c, i) = U1 xor U2 xor ... xor Uc
  18. //U1 = PRF1(Password, Salt + INT_32_BE(i))
  19. //U2 = PRF(Password, U1)
  20. //⋮
  21. //Uc = PRF(Password, Uc−1)
  22. size_t copylen = (keylen-outlen > SHA256_OUTSIZE) ? SHA256_OUTSIZE : keylen-outlen;
  23. saltbuffer.block = block;
  24. hashlib_Sha256Init(&ctx, mbuffer);
  25. hashlib_Sha256Update(&ctx, &saltbuffer, sizeof saltbuffer);
  26. hashlib_Sha256Final(&ctx, sha_buffer);
  27. for(int i=1; i<iter_count; i++){
  28. hashlib_Sha256Init(&ctx, NULL);
  29. hashlib_Sha256Update(&ctx, sha_buffer, SHA256_OUTSIZE);
  30. hashlib_Sha256Final(&ctx, sha_buffer);
  31. }
  32. memcpy(&out[outlen], sha_buffer, copylen);
  33. }
  34.  
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement