Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define PBKDF2_SALT_LEN 16
- struct saltbuffer_t { uint8_t salt[16]; uint32_t block; };
- hashlib_PBKDF2(const uint8_t* password, size_t plen, uint8_t* out, const uint8_t* salt, size_t iter_count, size_t keylen){
- struct saltbuffer_t saltbuffer;
- uint8_t sha_buffer[32];
- size_t outlen;
- SHA256_CTX ctx;
- uint32_t mbuffer[64];
- memcpy(saltbuffer.salt, salt, PBKDF2_SALT_LEN);
- size_t block = 0;
- //HASH = sha256
- // DK = T(i) for i in 0 => (keylen/HASH), concat
- for(outlen = 0; outlen < keylen; outlen+=32, block++){
- //T(i) = F(password, salt, c, i) = U1 xor U2 xor ... xor Uc
- //U1 = PRF1(Password, Salt + INT_32_BE(i))
- //U2 = PRF(Password, U1)
- //⋮
- //Uc = PRF(Password, Uc−1)
- size_t copylen = (keylen-outlen > 32) ? 32 : keylen-outlen;
- saltbuffer.block = block;
- hashlib_Sha256Init(&ctx, mbuffer);
- hashlib_Sha256Update(&ctx, &saltbuffer, PBKDF2_SALT_LEN+4);
- hashlib_Sha256Final(&ctx, sha_buffer);
- for(int i=1; i<iter_count; i++){
- hashlib_Sha256Init(&ctx, NULL);
- hashlib_Sha256Update(&ctx, sha_buffer, 32);
- hashlib_Sha256Final(&ctx, sha_buffer);
- }
- memcpy(&out[outlen], sha_buffer, copylen);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement