sha1.c
changeset 52 7f21abb9ff17
parent 0 d7ee4e2ed49d
child 53 ea43dbad123b
equal deleted inserted replaced
51:30d2d7598591 52:7f21abb9ff17
    46 #define R4(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=rol(w,30);
    46 #define R4(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=rol(w,30);
    47 
    47 
    48 /* Hash a single 512-bit block. This is the core of the algorithm. */
    48 /* Hash a single 512-bit block. This is the core of the algorithm. */
    49 
    49 
    50 void
    50 void
    51 SHA1Transform(u_int32_t state[5], const unsigned char buffer[SHA1_BLOCK_LENGTH])
    51 SHA1Transform(uint32_t state[5], const uint8_t buffer[SHA1_BLOCK_LENGTH])
    52 {
    52 {
    53     u_int32_t a, b, c, d, e;
    53     uint32_t a, b, c, d, e;
    54     typedef union {
    54     typedef union {
    55         unsigned char c[64];
    55         uint8_t c[64];
    56         unsigned int l[16];
    56         uint32_t l[16];
    57     } CHAR64LONG16;
    57     } CHAR64LONG16;
    58     CHAR64LONG16* block;
    58     CHAR64LONG16* block;
    59 #ifdef SHA1HANDSOFF
    59 #ifdef SHA1HANDSOFF
    60     unsigned char workspace[SHA1_BLOCK_LENGTH];
    60     uint8_t workspace[SHA1_BLOCK_LENGTH];
    61 
    61 
    62     block = (CHAR64LONG16 *)workspace;
    62     block = (CHAR64LONG16 *)workspace;
    63     memcpy(block, buffer, SHA1_BLOCK_LENGTH);
    63     memcpy(block, buffer, SHA1_BLOCK_LENGTH);
    64 #else
    64 #else
    65     block = (CHAR64LONG16 *)buffer;
    65     block = (CHAR64LONG16 *)buffer;
   120 
   120 
   121 
   121 
   122 /* Run your data through this. */
   122 /* Run your data through this. */
   123 
   123 
   124 void
   124 void
   125 SHA1Update(SHA1_CTX *context, const unsigned char *data, unsigned int len)
   125 SHA1Update(SHA1_CTX *context, const uint8_t *data, const uint32_t len)
   126 {
   126 {
   127     unsigned int i;
   127     uint32_t i;
   128     unsigned int j;
   128     uint32_t j;
   129 
   129 
   130     j = (u_int32_t)((context->count >> 3) & 63);
   130     j = (uint32_t)((context->count >> 3) & 63);
   131     context->count += (len << 3);
   131     context->count += (len << 3);
   132     if ((j + len) > 63) {
   132     if ((j + len) > 63) {
   133         memcpy(&context->buffer[j], data, (i = 64 - j));
   133         memcpy(&context->buffer[j], data, (i = 64 - j));
   134         SHA1Transform(context->state, context->buffer);
   134         SHA1Transform(context->state, context->buffer);
   135         for ( ; i + 63 < len; i += 64) {
   135         for ( ; i + 63 < len; i += 64) {
   143 
   143 
   144 
   144 
   145 /* Add padding and return the message digest. */
   145 /* Add padding and return the message digest. */
   146 
   146 
   147 void
   147 void
   148 SHA1Final(unsigned char digest[SHA1_DIGEST_LENGTH], SHA1_CTX *context)
   148 SHA1Final(uint8_t digest[SHA1_DIGEST_LENGTH], SHA1_CTX *context)
   149 {
   149 {
   150     unsigned int i;
   150     unsigned int i;
   151     unsigned char finalcount[8];
   151     uint8_t finalcount[8];
   152 
   152 
   153     for (i = 0; i < 8; i++) {
   153     for (i = 0; i < 8; i++) {
   154         finalcount[i] = (unsigned char)((context->count >>
   154         finalcount[i] = (uint8_t)((context->count >>
   155             ((7 - (i & 7)) * 8)) & 255);  /* Endian independent */
   155             ((7 - (i & 7)) * 8)) & 255);  /* Endian independent */
   156     }
   156     }
   157     SHA1Update(context, (unsigned char *)"\200", 1);
   157     SHA1Update(context, (uint8_t *)"\200", 1);
   158     while ((context->count & 504) != 448) {
   158     while ((context->count & 504) != 448) {
   159         SHA1Update(context, (unsigned char *)"\0", 1);
   159         SHA1Update(context, (uint8_t *)"\0", 1);
   160     }
   160     }
   161     SHA1Update(context, finalcount, 8);  /* Should cause a SHA1Transform() */
   161     SHA1Update(context, finalcount, 8);  /* Should cause a SHA1Transform() */
   162 
   162 
   163     if (digest)
   163     if (digest)
   164         for (i = 0; i < SHA1_DIGEST_LENGTH; i++) {
   164         for (i = 0; i < SHA1_DIGEST_LENGTH; i++) {
   165             digest[i] = (unsigned char)((context->state[i >> 2] >>
   165             digest[i] = (uint8_t)((context->state[i >> 2] >>
   166                 ((3 - (i & 3)) * 8)) & 255);
   166                 ((3 - (i & 3)) * 8)) & 255);
   167       }
   167       }
   168     memset(finalcount, '\0', 8);
   168     memset(finalcount, '\0', 8);
   169 #if 0	/* We want to use this for "keyfill" */
   169 #if 0	/* We want to use this for "keyfill" */
   170     /* Wipe variables */
   170     /* Wipe variables */