Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 2nd, 2012  |  syntax: None  |  size: 6.10 KB  |  hits: 15  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. /************************************************************************
  2.  *   Bahamut / Azzurra src/sha1.c
  3.  *
  4.  *   This program is free software; you can redistribute it and/or modify
  5.  *   it under the terms of the GNU General Public License as published by
  6.  *   the Free Software Foundation; either version 2, or (at your option)
  7.  *   any later version.
  8.  *
  9.  *   This program is distributed in the hope that it will be useful,
  10.  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.  *   GNU General Public License for more details.
  13.  *
  14.  *   You should have received a copy of the GNU General Public License
  15.  *   along with this program; if not, write to the Free Software
  16.  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17.  */
  18.  
  19.  
  20. /*
  21.  * SHA-1 in C
  22.  * By Steve Reid <steve@edmweb.com>
  23.  * 100% Public Domain
  24.  *
  25.  * Test Vectors (from FIPS PUB 180-1)
  26.  * "abc"
  27.  *   A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D
  28.  * "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
  29.  *   84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1
  30.  * A million repetitions of "a"
  31.  *   34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F
  32.  */
  33.  
  34. #include "struct.h"
  35. #include "common.h"
  36. #include "sys.h"
  37.  
  38. /* Hands off, please */
  39.  
  40. #include "sha1.h"
  41.  
  42. #define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
  43.  
  44. /* blk0() and blk() perform the initial expand. */
  45. /* I got the idea of expanding during the round function from SSLeay */
  46. #ifndef WORDS_BIGENDIAN
  47. #define blk0(i) (block->l[i] = (rol(block->l[i],24)&0xFF00FF00) \
  48.     |(rol(block->l[i],8)&0x00FF00FF))
  49. #else
  50. #define blk0(i) block->l[i]
  51. #endif
  52. #define blk(i) (block->l[i&15] = rol(block->l[(i+13)&15]^block->l[(i+8)&15] \
  53.     ^block->l[(i+2)&15]^block->l[i&15],1))
  54.  
  55. /* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */
  56. #define R0(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk0(i)+0x5A827999+rol(v,5);w=rol(w,30);
  57. #define R1(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk(i)+0x5A827999+rol(v,5);w=rol(w,30);
  58. #define R2(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0x6ED9EBA1+rol(v,5);w=rol(w,30);
  59. #define R3(v,w,x,y,z,i) z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=rol(w,30);
  60. #define R4(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=rol(w,30);
  61.  
  62. typedef union
  63. {
  64.     uint8_t c[64];
  65.     uint32_t l[16];
  66. } CHAR64LONG16;
  67.  
  68. /* Hash a single 512-bit block. This is the core of the algorithm. */
  69.  
  70. void
  71. SHA1Transform(uint32_t state[5], const unsigned char buffer[SHA1_BLOCK_LENGTH])
  72. {
  73.     uint32_t a, b, c, d, e;
  74.     CHAR64LONG16 *block;
  75.     unsigned char workspace[SHA1_BLOCK_LENGTH];
  76.  
  77.     block = (CHAR64LONG16 *)workspace;
  78.     memcpy(block, buffer, SHA1_BLOCK_LENGTH);
  79.  
  80.     /* Copy context->state[] to working vars */
  81.     a = state[0];
  82.     b = state[1];
  83.     c = state[2];
  84.     d = state[3];
  85.     e = state[4];
  86.  
  87.     /* 4 rounds of 20 operations each. Loop unrolled. */
  88.     R0(a,b,c,d,e, 0); R0(e,a,b,c,d, 1); R0(d,e,a,b,c, 2); R0(c,d,e,a,b, 3);
  89.     R0(b,c,d,e,a, 4); R0(a,b,c,d,e, 5); R0(e,a,b,c,d, 6); R0(d,e,a,b,c, 7);
  90.     R0(c,d,e,a,b, 8); R0(b,c,d,e,a, 9); R0(a,b,c,d,e,10); R0(e,a,b,c,d,11);
  91.     R0(d,e,a,b,c,12); R0(c,d,e,a,b,13); R0(b,c,d,e,a,14); R0(a,b,c,d,e,15);
  92.     R1(e,a,b,c,d,16); R1(d,e,a,b,c,17); R1(c,d,e,a,b,18); R1(b,c,d,e,a,19);
  93.     R2(a,b,c,d,e,20); R2(e,a,b,c,d,21); R2(d,e,a,b,c,22); R2(c,d,e,a,b,23);
  94.     R2(b,c,d,e,a,24); R2(a,b,c,d,e,25); R2(e,a,b,c,d,26); R2(d,e,a,b,c,27);
  95.     R2(c,d,e,a,b,28); R2(b,c,d,e,a,29); R2(a,b,c,d,e,30); R2(e,a,b,c,d,31);
  96.     R2(d,e,a,b,c,32); R2(c,d,e,a,b,33); R2(b,c,d,e,a,34); R2(a,b,c,d,e,35);
  97.     R2(e,a,b,c,d,36); R2(d,e,a,b,c,37); R2(c,d,e,a,b,38); R2(b,c,d,e,a,39);
  98.     R3(a,b,c,d,e,40); R3(e,a,b,c,d,41); R3(d,e,a,b,c,42); R3(c,d,e,a,b,43);
  99.     R3(b,c,d,e,a,44); R3(a,b,c,d,e,45); R3(e,a,b,c,d,46); R3(d,e,a,b,c,47);
  100.     R3(c,d,e,a,b,48); R3(b,c,d,e,a,49); R3(a,b,c,d,e,50); R3(e,a,b,c,d,51);
  101.     R3(d,e,a,b,c,52); R3(c,d,e,a,b,53); R3(b,c,d,e,a,54); R3(a,b,c,d,e,55);
  102.     R3(e,a,b,c,d,56); R3(d,e,a,b,c,57); R3(c,d,e,a,b,58); R3(b,c,d,e,a,59);
  103.     R4(a,b,c,d,e,60); R4(e,a,b,c,d,61); R4(d,e,a,b,c,62); R4(c,d,e,a,b,63);
  104.     R4(b,c,d,e,a,64); R4(a,b,c,d,e,65); R4(e,a,b,c,d,66); R4(d,e,a,b,c,67);
  105.     R4(c,d,e,a,b,68); R4(b,c,d,e,a,69); R4(a,b,c,d,e,70); R4(e,a,b,c,d,71);
  106.     R4(d,e,a,b,c,72); R4(c,d,e,a,b,73); R4(b,c,d,e,a,74); R4(a,b,c,d,e,75);
  107.     R4(e,a,b,c,d,76); R4(d,e,a,b,c,77); R4(c,d,e,a,b,78); R4(b,c,d,e,a,79);
  108.  
  109.     /* Add the working vars back into context->state[] */
  110.     state[0] += a;
  111.     state[1] += b;
  112.     state[2] += c;
  113.     state[3] += d;
  114.     state[4] += e;
  115.     /* Wipe out variables */
  116.     a = b = c = d = e = 0;
  117. }
  118.  
  119. /* SHA1Init - Initialize new context */
  120.  
  121. void
  122. SHA1Init(SHA1_CTX *context)
  123. {
  124.     /* SHA1 initialization constants */
  125.     context->count = 0;
  126.     context->state[0] = 0x67452301;
  127.     context->state[1] = 0xEFCDAB89;
  128.     context->state[2] = 0x98BADCFE;
  129.     context->state[3] = 0x10325476;
  130.     context->state[4] = 0xC3D2E1F0;
  131. }
  132.  
  133. /* Run your data through this. */
  134.  
  135. void
  136. SHA1Update(SHA1_CTX *context, const unsigned char *data, unsigned int len)
  137. {
  138.     unsigned int i;
  139.     unsigned int j;
  140.  
  141.     j = (uint32_t)((context->count >> 3) & 63);
  142.     context->count += (len << 3);
  143.     if ((j + len) > 63)
  144.     {
  145.         memcpy(&context->buffer[j], data, (i = 64 - j));
  146.         SHA1Transform(context->state, context->buffer);
  147.         for ( ; i + 63 < len; i += 64)
  148.             SHA1Transform(context->state, &data[i]);
  149.         j = 0;
  150.     }
  151.     else
  152.         i = 0;
  153.     memcpy(&context->buffer[j], &data[i], len - i);
  154. }
  155.  
  156. /* Add padding and return the message digest. */
  157.  
  158. void
  159. SHA1Final(unsigned char digest[SHA1_DIGEST_LENGTH], SHA1_CTX *context)
  160. {
  161.     unsigned int i;
  162.     unsigned char finalcount[8];
  163.  
  164.     for (i = 0; i < 8; i++)
  165.         finalcount[i] = (unsigned char)((context->count >>
  166.             ((7 - (i & 7)) * 8)) & 255);    /* Endian independent */
  167.  
  168.     SHA1Update(context, (unsigned char *)"\200", 1);
  169.  
  170.     while ((context->count & 504) != 448)
  171.         SHA1Update(context, (unsigned char *)"\0", 1);
  172.  
  173.     SHA1Update(context, finalcount, 8);
  174.  
  175.     if (digest)
  176.         for (i = 0; i < SHA1_DIGEST_LENGTH; i++)
  177.             digest[i] = (unsigned char)((context->state[i >> 2] >>
  178.                 ((3 - (i & 3)) * 8)) & 255);
  179.  
  180.     memset(&finalcount, '\0', 8);
  181. }