michael@0: /* michael@0: * sha1.c michael@0: * michael@0: * an implementation of the Secure Hash Algorithm v.1 (SHA-1), michael@0: * specified in FIPS 180-1 michael@0: * michael@0: * David A. McGrew michael@0: * Cisco Systems, Inc. michael@0: */ michael@0: michael@0: /* michael@0: * michael@0: * Copyright (c) 2001-2006, Cisco Systems, Inc. michael@0: * All rights reserved. michael@0: * michael@0: * Redistribution and use in source and binary forms, with or without michael@0: * modification, are permitted provided that the following conditions michael@0: * are met: michael@0: * michael@0: * Redistributions of source code must retain the above copyright michael@0: * notice, this list of conditions and the following disclaimer. michael@0: * michael@0: * Redistributions in binary form must reproduce the above michael@0: * copyright notice, this list of conditions and the following michael@0: * disclaimer in the documentation and/or other materials provided michael@0: * with the distribution. michael@0: * michael@0: * Neither the name of the Cisco Systems, Inc. nor the names of its michael@0: * contributors may be used to endorse or promote products derived michael@0: * from this software without specific prior written permission. michael@0: * michael@0: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS michael@0: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT michael@0: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS michael@0: * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE michael@0: * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, michael@0: * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES michael@0: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR michael@0: * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) michael@0: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, michael@0: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) michael@0: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED michael@0: * OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: * michael@0: */ michael@0: michael@0: michael@0: #include "sha1.h" michael@0: michael@0: debug_module_t mod_sha1 = { michael@0: 0, /* debugging is off by default */ michael@0: "sha-1" /* printable module name */ michael@0: }; michael@0: michael@0: /* SN == Rotate left N bits */ michael@0: #define S1(X) ((X << 1) | (X >> 31)) michael@0: #define S5(X) ((X << 5) | (X >> 27)) michael@0: #define S30(X) ((X << 30) | (X >> 2)) michael@0: michael@0: #define f0(B,C,D) ((B & C) | (~B & D)) michael@0: #define f1(B,C,D) (B ^ C ^ D) michael@0: #define f2(B,C,D) ((B & C) | (B & D) | (C & D)) michael@0: #define f3(B,C,D) (B ^ C ^ D) michael@0: michael@0: /* michael@0: * nota bene: the variable K0 appears in the curses library, so we michael@0: * give longer names to these variables to avoid spurious warnings michael@0: * on systems that uses curses michael@0: */ michael@0: michael@0: uint32_t SHA_K0 = 0x5A827999; /* Kt for 0 <= t <= 19 */ michael@0: uint32_t SHA_K1 = 0x6ED9EBA1; /* Kt for 20 <= t <= 39 */ michael@0: uint32_t SHA_K2 = 0x8F1BBCDC; /* Kt for 40 <= t <= 59 */ michael@0: uint32_t SHA_K3 = 0xCA62C1D6; /* Kt for 60 <= t <= 79 */ michael@0: michael@0: void michael@0: sha1(const uint8_t *msg, int octets_in_msg, uint32_t hash_value[5]) { michael@0: sha1_ctx_t ctx; michael@0: michael@0: sha1_init(&ctx); michael@0: sha1_update(&ctx, msg, octets_in_msg); michael@0: sha1_final(&ctx, hash_value); michael@0: michael@0: } michael@0: michael@0: /* michael@0: * sha1_core(M, H) computes the core compression function, where M is michael@0: * the next part of the message (in network byte order) and H is the michael@0: * intermediate state { H0, H1, ...} (in host byte order) michael@0: * michael@0: * this function does not do any of the padding required in the michael@0: * complete SHA1 function michael@0: * michael@0: * this function is used in the SEAL 3.0 key setup routines michael@0: * (crypto/cipher/seal.c) michael@0: */ michael@0: michael@0: void michael@0: sha1_core(const uint32_t M[16], uint32_t hash_value[5]) { michael@0: uint32_t H0; michael@0: uint32_t H1; michael@0: uint32_t H2; michael@0: uint32_t H3; michael@0: uint32_t H4; michael@0: uint32_t W[80]; michael@0: uint32_t A, B, C, D, E, TEMP; michael@0: int t; michael@0: michael@0: /* copy hash_value into H0, H1, H2, H3, H4 */ michael@0: H0 = hash_value[0]; michael@0: H1 = hash_value[1]; michael@0: H2 = hash_value[2]; michael@0: H3 = hash_value[3]; michael@0: H4 = hash_value[4]; michael@0: michael@0: /* copy/xor message into array */ michael@0: michael@0: W[0] = be32_to_cpu(M[0]); michael@0: W[1] = be32_to_cpu(M[1]); michael@0: W[2] = be32_to_cpu(M[2]); michael@0: W[3] = be32_to_cpu(M[3]); michael@0: W[4] = be32_to_cpu(M[4]); michael@0: W[5] = be32_to_cpu(M[5]); michael@0: W[6] = be32_to_cpu(M[6]); michael@0: W[7] = be32_to_cpu(M[7]); michael@0: W[8] = be32_to_cpu(M[8]); michael@0: W[9] = be32_to_cpu(M[9]); michael@0: W[10] = be32_to_cpu(M[10]); michael@0: W[11] = be32_to_cpu(M[11]); michael@0: W[12] = be32_to_cpu(M[12]); michael@0: W[13] = be32_to_cpu(M[13]); michael@0: W[14] = be32_to_cpu(M[14]); michael@0: W[15] = be32_to_cpu(M[15]); michael@0: TEMP = W[13] ^ W[8] ^ W[2] ^ W[0]; W[16] = S1(TEMP); michael@0: TEMP = W[14] ^ W[9] ^ W[3] ^ W[1]; W[17] = S1(TEMP); michael@0: TEMP = W[15] ^ W[10] ^ W[4] ^ W[2]; W[18] = S1(TEMP); michael@0: TEMP = W[16] ^ W[11] ^ W[5] ^ W[3]; W[19] = S1(TEMP); michael@0: TEMP = W[17] ^ W[12] ^ W[6] ^ W[4]; W[20] = S1(TEMP); michael@0: TEMP = W[18] ^ W[13] ^ W[7] ^ W[5]; W[21] = S1(TEMP); michael@0: TEMP = W[19] ^ W[14] ^ W[8] ^ W[6]; W[22] = S1(TEMP); michael@0: TEMP = W[20] ^ W[15] ^ W[9] ^ W[7]; W[23] = S1(TEMP); michael@0: TEMP = W[21] ^ W[16] ^ W[10] ^ W[8]; W[24] = S1(TEMP); michael@0: TEMP = W[22] ^ W[17] ^ W[11] ^ W[9]; W[25] = S1(TEMP); michael@0: TEMP = W[23] ^ W[18] ^ W[12] ^ W[10]; W[26] = S1(TEMP); michael@0: TEMP = W[24] ^ W[19] ^ W[13] ^ W[11]; W[27] = S1(TEMP); michael@0: TEMP = W[25] ^ W[20] ^ W[14] ^ W[12]; W[28] = S1(TEMP); michael@0: TEMP = W[26] ^ W[21] ^ W[15] ^ W[13]; W[29] = S1(TEMP); michael@0: TEMP = W[27] ^ W[22] ^ W[16] ^ W[14]; W[30] = S1(TEMP); michael@0: TEMP = W[28] ^ W[23] ^ W[17] ^ W[15]; W[31] = S1(TEMP); michael@0: michael@0: /* process the remainder of the array */ michael@0: for (t=32; t < 80; t++) { michael@0: TEMP = W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16]; michael@0: W[t] = S1(TEMP); michael@0: } michael@0: michael@0: A = H0; B = H1; C = H2; D = H3; E = H4; michael@0: michael@0: for (t=0; t < 20; t++) { michael@0: TEMP = S5(A) + f0(B,C,D) + E + W[t] + SHA_K0; michael@0: E = D; D = C; C = S30(B); B = A; A = TEMP; michael@0: } michael@0: for ( ; t < 40; t++) { michael@0: TEMP = S5(A) + f1(B,C,D) + E + W[t] + SHA_K1; michael@0: E = D; D = C; C = S30(B); B = A; A = TEMP; michael@0: } michael@0: for ( ; t < 60; t++) { michael@0: TEMP = S5(A) + f2(B,C,D) + E + W[t] + SHA_K2; michael@0: E = D; D = C; C = S30(B); B = A; A = TEMP; michael@0: } michael@0: for ( ; t < 80; t++) { michael@0: TEMP = S5(A) + f3(B,C,D) + E + W[t] + SHA_K3; michael@0: E = D; D = C; C = S30(B); B = A; A = TEMP; michael@0: } michael@0: michael@0: hash_value[0] = H0 + A; michael@0: hash_value[1] = H1 + B; michael@0: hash_value[2] = H2 + C; michael@0: hash_value[3] = H3 + D; michael@0: hash_value[4] = H4 + E; michael@0: michael@0: return; michael@0: } michael@0: michael@0: void michael@0: sha1_init(sha1_ctx_t *ctx) { michael@0: michael@0: /* initialize state vector */ michael@0: ctx->H[0] = 0x67452301; michael@0: ctx->H[1] = 0xefcdab89; michael@0: ctx->H[2] = 0x98badcfe; michael@0: ctx->H[3] = 0x10325476; michael@0: ctx->H[4] = 0xc3d2e1f0; michael@0: michael@0: /* indicate that message buffer is empty */ michael@0: ctx->octets_in_buffer = 0; michael@0: michael@0: /* reset message bit-count to zero */ michael@0: ctx->num_bits_in_msg = 0; michael@0: michael@0: } michael@0: michael@0: void michael@0: sha1_update(sha1_ctx_t *ctx, const uint8_t *msg, int octets_in_msg) { michael@0: int i; michael@0: uint8_t *buf = (uint8_t *)ctx->M; michael@0: michael@0: /* update message bit-count */ michael@0: ctx->num_bits_in_msg += octets_in_msg * 8; michael@0: michael@0: /* loop over 16-word blocks of M */ michael@0: while (octets_in_msg > 0) { michael@0: michael@0: if (octets_in_msg + ctx->octets_in_buffer >= 64) { michael@0: michael@0: /* michael@0: * copy words of M into msg buffer until that buffer is full, michael@0: * converting them into host byte order as needed michael@0: */ michael@0: octets_in_msg -= (64 - ctx->octets_in_buffer); michael@0: for (i=ctx->octets_in_buffer; i < 64; i++) michael@0: buf[i] = *msg++; michael@0: ctx->octets_in_buffer = 0; michael@0: michael@0: /* process a whole block */ michael@0: michael@0: debug_print(mod_sha1, "(update) running sha1_core()", NULL); michael@0: michael@0: sha1_core(ctx->M, ctx->H); michael@0: michael@0: } else { michael@0: michael@0: debug_print(mod_sha1, "(update) not running sha1_core()", NULL); michael@0: michael@0: for (i=ctx->octets_in_buffer; michael@0: i < (ctx->octets_in_buffer + octets_in_msg); i++) michael@0: buf[i] = *msg++; michael@0: ctx->octets_in_buffer += octets_in_msg; michael@0: octets_in_msg = 0; michael@0: } michael@0: michael@0: } michael@0: michael@0: } michael@0: michael@0: /* michael@0: * sha1_final(ctx, output) computes the result for ctx and copies it michael@0: * into the twenty octets located at *output michael@0: */ michael@0: michael@0: void michael@0: sha1_final(sha1_ctx_t *ctx, uint32_t *output) { michael@0: uint32_t A, B, C, D, E, TEMP; michael@0: uint32_t W[80]; michael@0: int i, t; michael@0: michael@0: /* michael@0: * process the remaining octets_in_buffer, padding and terminating as michael@0: * necessary michael@0: */ michael@0: { michael@0: int tail = ctx->octets_in_buffer % 4; michael@0: michael@0: /* copy/xor message into array */ michael@0: for (i=0; i < (ctx->octets_in_buffer+3)/4; i++) michael@0: W[i] = be32_to_cpu(ctx->M[i]); michael@0: michael@0: /* set the high bit of the octet immediately following the message */ michael@0: switch (tail) { michael@0: case (3): michael@0: W[i-1] = (be32_to_cpu(ctx->M[i-1]) & 0xffffff00) | 0x80; michael@0: W[i] = 0x0; michael@0: break; michael@0: case (2): michael@0: W[i-1] = (be32_to_cpu(ctx->M[i-1]) & 0xffff0000) | 0x8000; michael@0: W[i] = 0x0; michael@0: break; michael@0: case (1): michael@0: W[i-1] = (be32_to_cpu(ctx->M[i-1]) & 0xff000000) | 0x800000; michael@0: W[i] = 0x0; michael@0: break; michael@0: case (0): michael@0: W[i] = 0x80000000; michael@0: break; michael@0: } michael@0: michael@0: /* zeroize remaining words */ michael@0: for (i++ ; i < 15; i++) michael@0: W[i] = 0x0; michael@0: michael@0: /* michael@0: * if there is room at the end of the word array, then set the michael@0: * last word to the bit-length of the message; otherwise, set that michael@0: * word to zero and then we need to do one more run of the michael@0: * compression algo. michael@0: */ michael@0: if (ctx->octets_in_buffer < 56) michael@0: W[15] = ctx->num_bits_in_msg; michael@0: else if (ctx->octets_in_buffer < 60) michael@0: W[15] = 0x0; michael@0: michael@0: /* process the word array */ michael@0: for (t=16; t < 80; t++) { michael@0: TEMP = W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16]; michael@0: W[t] = S1(TEMP); michael@0: } michael@0: michael@0: A = ctx->H[0]; michael@0: B = ctx->H[1]; michael@0: C = ctx->H[2]; michael@0: D = ctx->H[3]; michael@0: E = ctx->H[4]; michael@0: michael@0: for (t=0; t < 20; t++) { michael@0: TEMP = S5(A) + f0(B,C,D) + E + W[t] + SHA_K0; michael@0: E = D; D = C; C = S30(B); B = A; A = TEMP; michael@0: } michael@0: for ( ; t < 40; t++) { michael@0: TEMP = S5(A) + f1(B,C,D) + E + W[t] + SHA_K1; michael@0: E = D; D = C; C = S30(B); B = A; A = TEMP; michael@0: } michael@0: for ( ; t < 60; t++) { michael@0: TEMP = S5(A) + f2(B,C,D) + E + W[t] + SHA_K2; michael@0: E = D; D = C; C = S30(B); B = A; A = TEMP; michael@0: } michael@0: for ( ; t < 80; t++) { michael@0: TEMP = S5(A) + f3(B,C,D) + E + W[t] + SHA_K3; michael@0: E = D; D = C; C = S30(B); B = A; A = TEMP; michael@0: } michael@0: michael@0: ctx->H[0] += A; michael@0: ctx->H[1] += B; michael@0: ctx->H[2] += C; michael@0: ctx->H[3] += D; michael@0: ctx->H[4] += E; michael@0: michael@0: } michael@0: michael@0: debug_print(mod_sha1, "(final) running sha1_core()", NULL); michael@0: michael@0: if (ctx->octets_in_buffer >= 56) { michael@0: michael@0: debug_print(mod_sha1, "(final) running sha1_core() again", NULL); michael@0: michael@0: /* we need to do one final run of the compression algo */ michael@0: michael@0: /* michael@0: * set initial part of word array to zeros, and set the michael@0: * final part to the number of bits in the message michael@0: */ michael@0: for (i=0; i < 15; i++) michael@0: W[i] = 0x0; michael@0: W[15] = ctx->num_bits_in_msg; michael@0: michael@0: /* process the word array */ michael@0: for (t=16; t < 80; t++) { michael@0: TEMP = W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16]; michael@0: W[t] = S1(TEMP); michael@0: } michael@0: michael@0: A = ctx->H[0]; michael@0: B = ctx->H[1]; michael@0: C = ctx->H[2]; michael@0: D = ctx->H[3]; michael@0: E = ctx->H[4]; michael@0: michael@0: for (t=0; t < 20; t++) { michael@0: TEMP = S5(A) + f0(B,C,D) + E + W[t] + SHA_K0; michael@0: E = D; D = C; C = S30(B); B = A; A = TEMP; michael@0: } michael@0: for ( ; t < 40; t++) { michael@0: TEMP = S5(A) + f1(B,C,D) + E + W[t] + SHA_K1; michael@0: E = D; D = C; C = S30(B); B = A; A = TEMP; michael@0: } michael@0: for ( ; t < 60; t++) { michael@0: TEMP = S5(A) + f2(B,C,D) + E + W[t] + SHA_K2; michael@0: E = D; D = C; C = S30(B); B = A; A = TEMP; michael@0: } michael@0: for ( ; t < 80; t++) { michael@0: TEMP = S5(A) + f3(B,C,D) + E + W[t] + SHA_K3; michael@0: E = D; D = C; C = S30(B); B = A; A = TEMP; michael@0: } michael@0: michael@0: ctx->H[0] += A; michael@0: ctx->H[1] += B; michael@0: ctx->H[2] += C; michael@0: ctx->H[3] += D; michael@0: ctx->H[4] += E; michael@0: } michael@0: michael@0: /* copy result into output buffer */ michael@0: output[0] = be32_to_cpu(ctx->H[0]); michael@0: output[1] = be32_to_cpu(ctx->H[1]); michael@0: output[2] = be32_to_cpu(ctx->H[2]); michael@0: output[3] = be32_to_cpu(ctx->H[3]); michael@0: output[4] = be32_to_cpu(ctx->H[4]); michael@0: michael@0: /* indicate that message buffer in context is empty */ michael@0: ctx->octets_in_buffer = 0; michael@0: michael@0: return; michael@0: } michael@0: michael@0: michael@0: