| 349 | } |
| 350 | |
| 351 | void sha256_traits::sha_update(sha256_ctx* ctx, const unsigned char* message, |
| 352 | unsigned int len) |
| 353 | { |
| 354 | const unsigned int tmp_len = SHA256_BLOCK_SIZE - ctx->len; |
| 355 | unsigned int rem_len = len < tmp_len ? len : tmp_len; |
| 356 | |
| 357 | memcpy(&ctx->block[ctx->len], message, rem_len); |
| 358 | |
| 359 | if (ctx->len + len < SHA256_BLOCK_SIZE) |
| 360 | { |
| 361 | ctx->len += len; |
| 362 | return; |
| 363 | } |
| 364 | |
| 365 | const unsigned int new_len = len - rem_len; |
| 366 | const unsigned int block_nb = new_len / SHA256_BLOCK_SIZE; |
| 367 | |
| 368 | const unsigned char* const shifted_message = message + rem_len; |
| 369 | |
| 370 | ctx->transf(ctx->block, 1); |
| 371 | ctx->transf(shifted_message, block_nb); |
| 372 | |
| 373 | rem_len = new_len % SHA256_BLOCK_SIZE; |
| 374 | |
| 375 | memcpy(ctx->block, &shifted_message[block_nb << 6], rem_len); |
| 376 | |
| 377 | ctx->len = rem_len; |
| 378 | ctx->tot_len += (block_nb + 1) << 6; |
| 379 | } |
| 380 | |
| 381 | void sha256_traits::sha_final(sha256_ctx* ctx, unsigned char* digest) |
| 382 | { |