ctz / fastpbkdf2

Fast PBKDF2 implementation in C
Creative Commons Zero v1.0 Universal
157 stars 46 forks source link

SHA224 and SHA384 Support? #13

Closed NelsonVides closed 4 years ago

NelsonVides commented 4 years ago

I've tried this:

DECL_PBKDF2(sha224,
            SHA256_CBLOCK,
            SHA224_DIGEST_LENGTH,
            SHA256_CTX,
            SHA224_Init,
            SHA224_Update,
            SHA256_Transform,
            SHA224_Final,
            sha256_cpy,
            sha256_extract,
            sha256_xor)

But it doesn't seem to cut it. Any advise?

NelsonVides commented 4 years ago

Ah, found a solution. The extract function is just slightly different, that's the one that needs to truncate the hash. These are the ones that need to be given to the DECL_PBKDF2 macro:

static inline void sha224_extract(SHA256_CTX *restrict ctx, uint8_t *restrict out)
{
  write32_be(ctx->h[0], out);
  write32_be(ctx->h[1], out + 4);
  write32_be(ctx->h[2], out + 8);
  write32_be(ctx->h[3], out + 12);
  write32_be(ctx->h[4], out + 16);
  write32_be(ctx->h[5], out + 20);
  write32_be(ctx->h[6], out + 24);
}

static inline void sha384_extract(SHA512_CTX *restrict ctx, uint8_t *restrict out)
{
  write64_be(ctx->h[0], out);
  write64_be(ctx->h[1], out + 8);
  write64_be(ctx->h[2], out + 16);
  write64_be(ctx->h[3], out + 24);
  write64_be(ctx->h[4], out + 32);
  write64_be(ctx->h[5], out + 40);
}