| 112 | } |
| 113 | |
| 114 | void sha256_final(SHA256_CTX *ctx, BYTE hash[]) |
| 115 | { |
| 116 | WORD i; |
| 117 | |
| 118 | i = ctx->datalen; |
| 119 | |
| 120 | // Pad whatever data is left in the buffer. |
| 121 | if (ctx->datalen < 56) { |
| 122 | ctx->data[i++] = 0x80; |
| 123 | while (i < 56) |
| 124 | ctx->data[i++] = 0x00; |
| 125 | } |
| 126 | else { |
| 127 | ctx->data[i++] = 0x80; |
| 128 | while (i < 64) |
| 129 | ctx->data[i++] = 0x00; |
| 130 | sha256_transform(ctx, ctx->data); |
| 131 | memset(ctx->data, 0, 56); |
| 132 | } |
| 133 | |
| 134 | // Append to the padding the total message's length in bits and transform. |
| 135 | ctx->bitlen += ctx->datalen * 8; |
| 136 | ctx->data[63] = ctx->bitlen; |
| 137 | ctx->data[62] = ctx->bitlen >> 8; |
| 138 | ctx->data[61] = ctx->bitlen >> 16; |
| 139 | ctx->data[60] = ctx->bitlen >> 24; |
| 140 | ctx->data[59] = ctx->bitlen >> 32; |
| 141 | ctx->data[58] = ctx->bitlen >> 40; |
| 142 | ctx->data[57] = ctx->bitlen >> 48; |
| 143 | ctx->data[56] = ctx->bitlen >> 56; |
| 144 | sha256_transform(ctx, ctx->data); |
| 145 | |
| 146 | // Since this implementation uses little endian byte ordering and SHA uses big endian, |
| 147 | // reverse all the bytes when copying the final state to the output hash. |
| 148 | for (i = 0; i < 4; ++i) { |
| 149 | hash[i] = (ctx->state[0] >> (24 - i * 8)) & 0x000000ff; |
| 150 | hash[i + 4] = (ctx->state[1] >> (24 - i * 8)) & 0x000000ff; |
| 151 | hash[i + 8] = (ctx->state[2] >> (24 - i * 8)) & 0x000000ff; |
| 152 | hash[i + 12] = (ctx->state[3] >> (24 - i * 8)) & 0x000000ff; |
| 153 | hash[i + 16] = (ctx->state[4] >> (24 - i * 8)) & 0x000000ff; |
| 154 | hash[i + 20] = (ctx->state[5] >> (24 - i * 8)) & 0x000000ff; |
| 155 | hash[i + 24] = (ctx->state[6] >> (24 - i * 8)) & 0x000000ff; |
| 156 | hash[i + 28] = (ctx->state[7] >> (24 - i * 8)) & 0x000000ff; |
| 157 | } |
| 158 | } |
no test coverage detected