void pbkdf2_hmac_sha512_Init(PBKDF2_HMAC_SHA512_CTX *pctx, const uint8_t *pass, int passlen, const uint8_t *salt, int saltlen) { SHA512_CTX ctx; uint32_t blocknr = 1; #if BYTE_ORDER == LITTLE_ENDIAN REVERSE32(blocknr, blocknr); #endif hmac_sha512_prepare(pass, passlen, pctx->odig, pctx->idig); memset(pctx->g, 0, sizeof(pctx->g)); pctx->g[8] = 0x8000000000000000; pctx->g[15] = (SHA512_BLOCK_LENGTH + SHA512_DIGEST_LENGTH) * 8; memcpy (ctx.state, pctx->idig, sizeof(pctx->idig)); ctx.bitcount[0] = SHA512_BLOCK_LENGTH * 8; ctx.bitcount[1] = 0; sha512_Update(&ctx, salt, saltlen); sha512_Update(&ctx, (uint8_t*)&blocknr, sizeof(blocknr)); sha512_Final(&ctx, (uint8_t*)pctx->g); #if BYTE_ORDER == LITTLE_ENDIAN for (uint32_t k = 0; k < SHA512_DIGEST_LENGTH / sizeof(uint64_t); k++) { REVERSE64(pctx->g[k], pctx->g[k]); } #endif sha512_Transform(pctx->odig, pctx->g, pctx->g); memcpy(pctx->f, pctx->g, SHA512_DIGEST_LENGTH); pctx->first = 1; }
void hmac_sha512_prepare(const uint8_t *key, const uint32_t keylen, uint64_t *opad_digest, uint64_t *ipad_digest) { static CONFIDENTIAL uint64_t key_pad[SHA512_BLOCK_LENGTH / sizeof(uint64_t)]; memzero(key_pad, sizeof(key_pad)); if (keylen > SHA512_BLOCK_LENGTH) { static CONFIDENTIAL SHA512_CTX context; sha512_Init(&context); sha512_Update(&context, key, keylen); sha512_Final(&context, (uint8_t *)key_pad); } else { memcpy(key_pad, key, keylen); } /* compute o_key_pad and its digest */ for (int i = 0; i < SHA512_BLOCK_LENGTH / (int)sizeof(uint64_t); i++) { uint64_t data; #if BYTE_ORDER == LITTLE_ENDIAN REVERSE64(key_pad[i], data); #else data = key_pad[i]; #endif key_pad[i] = data ^ 0x5c5c5c5c5c5c5c5c; } sha512_Transform(sha512_initial_hash_value, key_pad, opad_digest); /* convert o_key_pad to i_key_pad and compute its digest */ for (int i = 0; i < SHA512_BLOCK_LENGTH / (int)sizeof(uint64_t); i++) { key_pad[i] = key_pad[i] ^ 0x5c5c5c5c5c5c5c5c ^ 0x3636363636363636; } sha512_Transform(sha512_initial_hash_value, key_pad, ipad_digest); memzero(key_pad, sizeof(key_pad)); }
void SHA256_Final(sha2_byte digest[], SHA256_CTX* context) { sha2_word32 *d = (sha2_word32*)digest; unsigned int usedspace; /* Sanity check: */ assert(context != (SHA256_CTX*)0); /* If no digest buffer is passed, we don't bother doing this: */ if (digest != (sha2_byte*)0) { usedspace = (unsigned int)((context->bitcount >> 3) % SHA256_BLOCK_LENGTH); #if SIRIKATA_BYTE_ORDER == SIRIKATA_LITTLE_ENDIAN /* Convert FROM host byte order */ REVERSE64(context->bitcount,context->bitcount); #endif if (usedspace > 0) { /* Begin padding with a 1 bit: */ context->buffer[usedspace++] = 0x80; if (usedspace <= SHA256_SHORT_BLOCK_LENGTH) { /* Set-up for the last transform: */ MEMSET_BZERO(&context->buffer[usedspace], SHA256_SHORT_BLOCK_LENGTH - usedspace); } else { if (usedspace < SHA256_BLOCK_LENGTH) { MEMSET_BZERO(&context->buffer[usedspace], SHA256_BLOCK_LENGTH - usedspace); } /* Do second-to-last transform: */ SHA256_Transform(context, (sha2_word32*)context->buffer); /* And set-up for the last transform: */ MEMSET_BZERO(context->buffer, SHA256_SHORT_BLOCK_LENGTH); } } else { /* Set-up for the last transform: */ MEMSET_BZERO(context->buffer, SHA256_SHORT_BLOCK_LENGTH); /* Begin padding with a 1 bit: */ *context->buffer = 0x80; } /* Set the bit count: */ *(sha2_word64*)&context->buffer[SHA256_SHORT_BLOCK_LENGTH] = context->bitcount; /* Final transform: */ SHA256_Transform(context, (sha2_word32*)context->buffer); #if SIRIKATA_BYTE_ORDER == SIRIKATA_LITTLE_ENDIAN { /* Convert TO host byte order */ int j; for (j = 0; j < 8; j++) { REVERSE32(context->state[j],context->state[j]); *d++ = context->state[j]; } } #else MEMCPY_BCOPY(d, context->state, SHA256_DIGEST_LENGTH); #endif }
void pbkdf2_hmac_sha512_Final(PBKDF2_HMAC_SHA512_CTX *pctx, uint8_t *key) { #if BYTE_ORDER == LITTLE_ENDIAN for (uint32_t k = 0; k < SHA512_DIGEST_LENGTH/sizeof(uint64_t); k++) { REVERSE64(pctx->f[k], pctx->f[k]); } #endif memcpy(key, pctx->f, SHA512_DIGEST_LENGTH); memzero(pctx, sizeof(PBKDF2_HMAC_SHA512_CTX)); }