]> git.saurik.com Git - apple/xnu.git/blame - osfmk/corecrypto/ccsha1/src/ccdigest_final_64be.c
xnu-2782.40.9.tar.gz
[apple/xnu.git] / osfmk / corecrypto / ccsha1 / src / ccdigest_final_64be.c
CommitLineData
fe8ab488
A
1/*
2 * ccdigest_final_64be.c
3 * corecrypto
4 *
5 * Created by Michael Brouwer on 12/1/10.
6 * Copyright 2010,2011 Apple Inc. All rights reserved.
7 *
8 */
9
10#include <corecrypto/ccdigest_priv.h>
11#include <corecrypto/cc_priv.h>
12
13/* This can be used for SHA1, SHA256 and SHA224 */
14void ccdigest_final_64be(const struct ccdigest_info *di, ccdigest_ctx_t ctx,
15 unsigned char *digest) {
16 ccdigest_nbits(di, ctx) += ccdigest_num(di, ctx) * 8;
17 ccdigest_data(di, ctx)[ccdigest_num(di, ctx)++] = 0x80;
18
19 /* If we don't have at least 8 bytes (for the length) left we need to add
20 a second block. */
21 if (ccdigest_num(di, ctx) > 64 - 8) {
22 while (ccdigest_num(di, ctx) < 64) {
23 ccdigest_data(di, ctx)[ccdigest_num(di, ctx)++] = 0;
24 }
25 di->compress(ccdigest_state(di, ctx), 1, ccdigest_data(di, ctx));
26 ccdigest_num(di, ctx) = 0;
27 }
28
29 /* pad upto block_size minus 8 with 0s */
30 while (ccdigest_num(di, ctx) < 64 - 8) {
31 ccdigest_data(di, ctx)[ccdigest_num(di, ctx)++] = 0;
32 }
33
34 CC_STORE64_BE(ccdigest_nbits(di, ctx), ccdigest_data(di, ctx) + 64 - 8);
35 di->compress(ccdigest_state(di, ctx), 1, ccdigest_data(di, ctx));
36
37 /* copy output */
38 for (unsigned int i = 0; i < di->output_size / 4; i++) {
39 CC_STORE32_BE(ccdigest_state_u32(di, ctx)[i], digest+(4*i));
40 }
41}