Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define PBKDF2_SALT_LEN 16
- #define SHA256_OUTSIZE 32
- hashlib_PBKDF2(const uint8_t* password, size_t plen, uint8_t* out, const uint8_t* salt, size_t iter_count, size_t keylen){
- struct { uint8_t salt[16]; uint32_t block; } saltbuffer;
- uint8_t sha_buffer[SHA256_OUTSIZE];
- size_t outlen;
- SHA256_CTX ctx;
- uint32_t mbuffer[64];
- memcpy(saltbuffer.salt, salt, sizeof saltbuffer.salt);
- 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 > SHA256_OUTSIZE) ? SHA256_OUTSIZE : keylen-outlen;
- saltbuffer.block = block;
- hashlib_Sha256Init(&ctx, mbuffer);
- hashlib_Sha256Update(&ctx, &saltbuffer, sizeof saltbuffer);
- hashlib_Sha256Final(&ctx, sha_buffer);
- for(int i=1; i<iter_count; i++){
- hashlib_Sha256Init(&ctx, NULL);
- hashlib_Sha256Update(&ctx, sha_buffer, SHA256_OUTSIZE);
- hashlib_Sha256Final(&ctx, sha_buffer);
- }
- memcpy(&out[outlen], sha_buffer, copylen);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement