netwerk/srtp/src/crypto/hash/sha1.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/netwerk/srtp/src/crypto/hash/sha1.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,405 @@
     1.4 +/*
     1.5 + * sha1.c
     1.6 + *
     1.7 + * an implementation of the Secure Hash Algorithm v.1 (SHA-1),
     1.8 + * specified in FIPS 180-1
     1.9 + *
    1.10 + * David A. McGrew
    1.11 + * Cisco Systems, Inc.
    1.12 + */
    1.13 +
    1.14 +/*
    1.15 + *	
    1.16 + * Copyright (c) 2001-2006, Cisco Systems, Inc.
    1.17 + * All rights reserved.
    1.18 + * 
    1.19 + * Redistribution and use in source and binary forms, with or without
    1.20 + * modification, are permitted provided that the following conditions
    1.21 + * are met:
    1.22 + * 
    1.23 + *   Redistributions of source code must retain the above copyright
    1.24 + *   notice, this list of conditions and the following disclaimer.
    1.25 + * 
    1.26 + *   Redistributions in binary form must reproduce the above
    1.27 + *   copyright notice, this list of conditions and the following
    1.28 + *   disclaimer in the documentation and/or other materials provided
    1.29 + *   with the distribution.
    1.30 + * 
    1.31 + *   Neither the name of the Cisco Systems, Inc. nor the names of its
    1.32 + *   contributors may be used to endorse or promote products derived
    1.33 + *   from this software without specific prior written permission.
    1.34 + * 
    1.35 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    1.36 + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    1.37 + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
    1.38 + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
    1.39 + * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
    1.40 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
    1.41 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
    1.42 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    1.43 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
    1.44 + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    1.45 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
    1.46 + * OF THE POSSIBILITY OF SUCH DAMAGE.
    1.47 + *
    1.48 + */
    1.49 +
    1.50 +
    1.51 +#include "sha1.h"
    1.52 +
    1.53 +debug_module_t mod_sha1 = {
    1.54 +  0,                 /* debugging is off by default */
    1.55 +  "sha-1"            /* printable module name       */
    1.56 +};
    1.57 +
    1.58 +/* SN == Rotate left N bits */
    1.59 +#define S1(X)  ((X << 1)  | (X >> 31))
    1.60 +#define S5(X)  ((X << 5)  | (X >> 27))
    1.61 +#define S30(X) ((X << 30) | (X >> 2))
    1.62 +
    1.63 +#define f0(B,C,D) ((B & C) | (~B & D))              
    1.64 +#define f1(B,C,D) (B ^ C ^ D)
    1.65 +#define f2(B,C,D) ((B & C) | (B & D) | (C & D))
    1.66 +#define f3(B,C,D) (B ^ C ^ D)
    1.67 +
    1.68 +/* 
    1.69 + * nota bene: the variable K0 appears in the curses library, so we 
    1.70 + * give longer names to these variables to avoid spurious warnings 
    1.71 + * on systems that uses curses
    1.72 + */
    1.73 +
    1.74 +uint32_t SHA_K0 = 0x5A827999;   /* Kt for 0  <= t <= 19 */
    1.75 +uint32_t SHA_K1 = 0x6ED9EBA1;   /* Kt for 20 <= t <= 39 */
    1.76 +uint32_t SHA_K2 = 0x8F1BBCDC;   /* Kt for 40 <= t <= 59 */
    1.77 +uint32_t SHA_K3 = 0xCA62C1D6;   /* Kt for 60 <= t <= 79 */
    1.78 +
    1.79 +void
    1.80 +sha1(const uint8_t *msg,  int octets_in_msg, uint32_t hash_value[5]) {
    1.81 +  sha1_ctx_t ctx;
    1.82 +
    1.83 +  sha1_init(&ctx);
    1.84 +  sha1_update(&ctx, msg, octets_in_msg);
    1.85 +  sha1_final(&ctx, hash_value);
    1.86 +
    1.87 +}
    1.88 +
    1.89 +/*
    1.90 + *  sha1_core(M, H) computes the core compression function, where M is
    1.91 + *  the next part of the message (in network byte order) and H is the
    1.92 + *  intermediate state { H0, H1, ...} (in host byte order)
    1.93 + *
    1.94 + *  this function does not do any of the padding required in the
    1.95 + *  complete SHA1 function
    1.96 + *
    1.97 + *  this function is used in the SEAL 3.0 key setup routines
    1.98 + *  (crypto/cipher/seal.c)
    1.99 + */
   1.100 +
   1.101 +void
   1.102 +sha1_core(const uint32_t M[16], uint32_t hash_value[5]) {
   1.103 +  uint32_t H0;
   1.104 +  uint32_t H1;
   1.105 +  uint32_t H2;
   1.106 +  uint32_t H3;
   1.107 +  uint32_t H4;
   1.108 +  uint32_t W[80];
   1.109 +  uint32_t A, B, C, D, E, TEMP;
   1.110 +  int t;
   1.111 +
   1.112 +  /* copy hash_value into H0, H1, H2, H3, H4 */
   1.113 +  H0 = hash_value[0];
   1.114 +  H1 = hash_value[1];
   1.115 +  H2 = hash_value[2];
   1.116 +  H3 = hash_value[3];
   1.117 +  H4 = hash_value[4];
   1.118 +
   1.119 +  /* copy/xor message into array */
   1.120 +
   1.121 +  W[0]  = be32_to_cpu(M[0]);
   1.122 +  W[1]  = be32_to_cpu(M[1]);
   1.123 +  W[2]  = be32_to_cpu(M[2]);
   1.124 +  W[3]  = be32_to_cpu(M[3]);
   1.125 +  W[4]  = be32_to_cpu(M[4]);
   1.126 +  W[5]  = be32_to_cpu(M[5]);
   1.127 +  W[6]  = be32_to_cpu(M[6]);
   1.128 +  W[7]  = be32_to_cpu(M[7]);
   1.129 +  W[8]  = be32_to_cpu(M[8]);
   1.130 +  W[9]  = be32_to_cpu(M[9]);
   1.131 +  W[10] = be32_to_cpu(M[10]);
   1.132 +  W[11] = be32_to_cpu(M[11]);
   1.133 +  W[12] = be32_to_cpu(M[12]);
   1.134 +  W[13] = be32_to_cpu(M[13]);
   1.135 +  W[14] = be32_to_cpu(M[14]);
   1.136 +  W[15] = be32_to_cpu(M[15]);
   1.137 +  TEMP = W[13] ^ W[8]  ^ W[2]  ^ W[0];  W[16] = S1(TEMP);
   1.138 +  TEMP = W[14] ^ W[9]  ^ W[3]  ^ W[1];  W[17] = S1(TEMP);
   1.139 +  TEMP = W[15] ^ W[10] ^ W[4]  ^ W[2];  W[18] = S1(TEMP);
   1.140 +  TEMP = W[16] ^ W[11] ^ W[5]  ^ W[3];  W[19] = S1(TEMP);
   1.141 +  TEMP = W[17] ^ W[12] ^ W[6]  ^ W[4];  W[20] = S1(TEMP);
   1.142 +  TEMP = W[18] ^ W[13] ^ W[7]  ^ W[5];  W[21] = S1(TEMP);
   1.143 +  TEMP = W[19] ^ W[14] ^ W[8]  ^ W[6];  W[22] = S1(TEMP);
   1.144 +  TEMP = W[20] ^ W[15] ^ W[9]  ^ W[7];  W[23] = S1(TEMP);
   1.145 +  TEMP = W[21] ^ W[16] ^ W[10] ^ W[8];  W[24] = S1(TEMP);
   1.146 +  TEMP = W[22] ^ W[17] ^ W[11] ^ W[9];  W[25] = S1(TEMP);
   1.147 +  TEMP = W[23] ^ W[18] ^ W[12] ^ W[10]; W[26] = S1(TEMP);
   1.148 +  TEMP = W[24] ^ W[19] ^ W[13] ^ W[11]; W[27] = S1(TEMP);
   1.149 +  TEMP = W[25] ^ W[20] ^ W[14] ^ W[12]; W[28] = S1(TEMP);
   1.150 +  TEMP = W[26] ^ W[21] ^ W[15] ^ W[13]; W[29] = S1(TEMP);
   1.151 +  TEMP = W[27] ^ W[22] ^ W[16] ^ W[14]; W[30] = S1(TEMP);
   1.152 +  TEMP = W[28] ^ W[23] ^ W[17] ^ W[15]; W[31] = S1(TEMP);
   1.153 +
   1.154 +  /* process the remainder of the array */
   1.155 +  for (t=32; t < 80; t++) {
   1.156 +    TEMP = W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16];
   1.157 +    W[t] = S1(TEMP);      
   1.158 +  }
   1.159 +
   1.160 +  A = H0; B = H1; C = H2; D = H3; E = H4;
   1.161 +
   1.162 +  for (t=0; t < 20; t++) {
   1.163 +    TEMP = S5(A) + f0(B,C,D) + E + W[t] + SHA_K0;
   1.164 +    E = D; D = C; C = S30(B); B = A; A = TEMP;
   1.165 +  }
   1.166 +  for (   ; t < 40; t++) {
   1.167 +    TEMP = S5(A) + f1(B,C,D) + E + W[t] + SHA_K1;
   1.168 +    E = D; D = C; C = S30(B); B = A; A = TEMP;
   1.169 +  }
   1.170 +  for (   ; t < 60; t++) {
   1.171 +    TEMP = S5(A) + f2(B,C,D) + E + W[t] + SHA_K2;
   1.172 +    E = D; D = C; C = S30(B); B = A; A = TEMP;
   1.173 +  }
   1.174 +  for (   ; t < 80; t++) {
   1.175 +    TEMP = S5(A) + f3(B,C,D) + E + W[t] + SHA_K3;
   1.176 +    E = D; D = C; C = S30(B); B = A; A = TEMP;
   1.177 +  }
   1.178 +
   1.179 +  hash_value[0] = H0 + A;
   1.180 +  hash_value[1] = H1 + B;
   1.181 +  hash_value[2] = H2 + C;
   1.182 +  hash_value[3] = H3 + D;
   1.183 +  hash_value[4] = H4 + E;
   1.184 +
   1.185 +  return;
   1.186 +}
   1.187 +
   1.188 +void
   1.189 +sha1_init(sha1_ctx_t *ctx) {
   1.190 +
   1.191 +  /* initialize state vector */
   1.192 +  ctx->H[0] = 0x67452301;
   1.193 +  ctx->H[1] = 0xefcdab89;
   1.194 +  ctx->H[2] = 0x98badcfe;
   1.195 +  ctx->H[3] = 0x10325476;
   1.196 +  ctx->H[4] = 0xc3d2e1f0;
   1.197 +
   1.198 +  /* indicate that message buffer is empty */
   1.199 +  ctx->octets_in_buffer = 0;
   1.200 +
   1.201 +  /* reset message bit-count to zero */
   1.202 +  ctx->num_bits_in_msg = 0;
   1.203 +
   1.204 +}
   1.205 +
   1.206 +void
   1.207 +sha1_update(sha1_ctx_t *ctx, const uint8_t *msg, int octets_in_msg) {
   1.208 +  int i;
   1.209 +  uint8_t *buf = (uint8_t *)ctx->M;
   1.210 +
   1.211 +  /* update message bit-count */
   1.212 +  ctx->num_bits_in_msg += octets_in_msg * 8;
   1.213 +
   1.214 +  /* loop over 16-word blocks of M */
   1.215 +  while (octets_in_msg > 0) {
   1.216 +
   1.217 +    if (octets_in_msg + ctx->octets_in_buffer >= 64) {
   1.218 +
   1.219 +      /* 
   1.220 +       * copy words of M into msg buffer until that buffer is full,
   1.221 +       * converting them into host byte order as needed
   1.222 +       */
   1.223 +      octets_in_msg -= (64 - ctx->octets_in_buffer);
   1.224 +      for (i=ctx->octets_in_buffer; i < 64; i++) 
   1.225 +	buf[i] = *msg++;
   1.226 +      ctx->octets_in_buffer = 0;
   1.227 +
   1.228 +      /* process a whole block */
   1.229 +
   1.230 +      debug_print(mod_sha1, "(update) running sha1_core()", NULL);
   1.231 +
   1.232 +      sha1_core(ctx->M, ctx->H);
   1.233 +
   1.234 +    } else {
   1.235 +
   1.236 +      debug_print(mod_sha1, "(update) not running sha1_core()", NULL);
   1.237 +
   1.238 +      for (i=ctx->octets_in_buffer; 
   1.239 +	   i < (ctx->octets_in_buffer + octets_in_msg); i++)
   1.240 +	buf[i] = *msg++;
   1.241 +      ctx->octets_in_buffer += octets_in_msg;
   1.242 +      octets_in_msg = 0;
   1.243 +    }
   1.244 +
   1.245 +  }
   1.246 +
   1.247 +}
   1.248 +
   1.249 +/*
   1.250 + * sha1_final(ctx, output) computes the result for ctx and copies it
   1.251 + * into the twenty octets located at *output
   1.252 + */
   1.253 +
   1.254 +void
   1.255 +sha1_final(sha1_ctx_t *ctx, uint32_t *output) {
   1.256 +  uint32_t A, B, C, D, E, TEMP;
   1.257 +  uint32_t W[80];  
   1.258 +  int i, t;
   1.259 +
   1.260 +  /*
   1.261 +   * process the remaining octets_in_buffer, padding and terminating as
   1.262 +   * necessary
   1.263 +   */
   1.264 +  {
   1.265 +    int tail = ctx->octets_in_buffer % 4;
   1.266 +
   1.267 +    /* copy/xor message into array */
   1.268 +    for (i=0; i < (ctx->octets_in_buffer+3)/4; i++) 
   1.269 +      W[i]  = be32_to_cpu(ctx->M[i]);
   1.270 +
   1.271 +    /* set the high bit of the octet immediately following the message */
   1.272 +    switch (tail) {
   1.273 +    case (3):
   1.274 +      W[i-1] = (be32_to_cpu(ctx->M[i-1]) & 0xffffff00) | 0x80;
   1.275 +      W[i] = 0x0;
   1.276 +      break;
   1.277 +    case (2):      
   1.278 +      W[i-1] = (be32_to_cpu(ctx->M[i-1]) & 0xffff0000) | 0x8000;
   1.279 +      W[i] = 0x0;
   1.280 +      break;
   1.281 +    case (1):
   1.282 +      W[i-1] = (be32_to_cpu(ctx->M[i-1]) & 0xff000000) | 0x800000;
   1.283 +      W[i] = 0x0;
   1.284 +      break;
   1.285 +    case (0):
   1.286 +      W[i] = 0x80000000;
   1.287 +      break;
   1.288 +    }
   1.289 +
   1.290 +    /* zeroize remaining words */
   1.291 +    for (i++   ; i < 15; i++)
   1.292 +      W[i] = 0x0;
   1.293 +
   1.294 +    /* 
   1.295 +     * if there is room at the end of the word array, then set the
   1.296 +     * last word to the bit-length of the message; otherwise, set that
   1.297 +     * word to zero and then we need to do one more run of the
   1.298 +     * compression algo.
   1.299 +     */
   1.300 +    if (ctx->octets_in_buffer < 56) 
   1.301 +      W[15] = ctx->num_bits_in_msg;
   1.302 +    else if (ctx->octets_in_buffer < 60)
   1.303 +      W[15] = 0x0;
   1.304 +
   1.305 +    /* process the word array */
   1.306 +    for (t=16; t < 80; t++) {
   1.307 +      TEMP = W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16];
   1.308 +      W[t] = S1(TEMP);
   1.309 +    }
   1.310 +
   1.311 +    A = ctx->H[0]; 
   1.312 +    B = ctx->H[1]; 
   1.313 +    C = ctx->H[2]; 
   1.314 +    D = ctx->H[3]; 
   1.315 +    E = ctx->H[4];
   1.316 +
   1.317 +    for (t=0; t < 20; t++) {
   1.318 +      TEMP = S5(A) + f0(B,C,D) + E + W[t] + SHA_K0;
   1.319 +      E = D; D = C; C = S30(B); B = A; A = TEMP;
   1.320 +    }
   1.321 +    for (   ; t < 40; t++) {
   1.322 +      TEMP = S5(A) + f1(B,C,D) + E + W[t] + SHA_K1;
   1.323 +      E = D; D = C; C = S30(B); B = A; A = TEMP;
   1.324 +    }
   1.325 +    for (   ; t < 60; t++) {
   1.326 +      TEMP = S5(A) + f2(B,C,D) + E + W[t] + SHA_K2;
   1.327 +      E = D; D = C; C = S30(B); B = A; A = TEMP;
   1.328 +    }
   1.329 +    for (   ; t < 80; t++) {
   1.330 +      TEMP = S5(A) + f3(B,C,D) + E + W[t] + SHA_K3;
   1.331 +      E = D; D = C; C = S30(B); B = A; A = TEMP;
   1.332 +    }
   1.333 +
   1.334 +    ctx->H[0] += A;
   1.335 +    ctx->H[1] += B;
   1.336 +    ctx->H[2] += C;
   1.337 +    ctx->H[3] += D;
   1.338 +    ctx->H[4] += E;
   1.339 +
   1.340 +  }
   1.341 +
   1.342 +  debug_print(mod_sha1, "(final) running sha1_core()", NULL);
   1.343 +
   1.344 +  if (ctx->octets_in_buffer >= 56) {
   1.345 +
   1.346 +    debug_print(mod_sha1, "(final) running sha1_core() again", NULL);
   1.347 +
   1.348 +    /* we need to do one final run of the compression algo */
   1.349 +
   1.350 +    /* 
   1.351 +     * set initial part of word array to zeros, and set the 
   1.352 +     * final part to the number of bits in the message
   1.353 +     */
   1.354 +    for (i=0; i < 15; i++)
   1.355 +      W[i] = 0x0;
   1.356 +    W[15] = ctx->num_bits_in_msg;
   1.357 +
   1.358 +    /* process the word array */
   1.359 +    for (t=16; t < 80; t++) {
   1.360 +      TEMP = W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16];
   1.361 +      W[t] = S1(TEMP);
   1.362 +    }
   1.363 +
   1.364 +    A = ctx->H[0]; 
   1.365 +    B = ctx->H[1]; 
   1.366 +    C = ctx->H[2]; 
   1.367 +    D = ctx->H[3]; 
   1.368 +    E = ctx->H[4];
   1.369 +
   1.370 +    for (t=0; t < 20; t++) {
   1.371 +      TEMP = S5(A) + f0(B,C,D) + E + W[t] + SHA_K0;
   1.372 +      E = D; D = C; C = S30(B); B = A; A = TEMP;
   1.373 +    }
   1.374 +    for (   ; t < 40; t++) {
   1.375 +      TEMP = S5(A) + f1(B,C,D) + E + W[t] + SHA_K1;
   1.376 +      E = D; D = C; C = S30(B); B = A; A = TEMP;
   1.377 +    }
   1.378 +    for (   ; t < 60; t++) {
   1.379 +      TEMP = S5(A) + f2(B,C,D) + E + W[t] + SHA_K2;
   1.380 +      E = D; D = C; C = S30(B); B = A; A = TEMP;
   1.381 +    }
   1.382 +    for (   ; t < 80; t++) {
   1.383 +      TEMP = S5(A) + f3(B,C,D) + E + W[t] + SHA_K3;
   1.384 +      E = D; D = C; C = S30(B); B = A; A = TEMP;
   1.385 +    }
   1.386 +
   1.387 +    ctx->H[0] += A;
   1.388 +    ctx->H[1] += B;
   1.389 +    ctx->H[2] += C;
   1.390 +    ctx->H[3] += D;
   1.391 +    ctx->H[4] += E;
   1.392 +  }
   1.393 +
   1.394 +  /* copy result into output buffer */
   1.395 +  output[0] = be32_to_cpu(ctx->H[0]);
   1.396 +  output[1] = be32_to_cpu(ctx->H[1]);
   1.397 +  output[2] = be32_to_cpu(ctx->H[2]);
   1.398 +  output[3] = be32_to_cpu(ctx->H[3]);
   1.399 +  output[4] = be32_to_cpu(ctx->H[4]);
   1.400 +
   1.401 +  /* indicate that message buffer in context is empty */
   1.402 +  ctx->octets_in_buffer = 0;
   1.403 +
   1.404 +  return;
   1.405 +}
   1.406 +
   1.407 +
   1.408 +

mercurial