Home | History | Annotate | Download | only in libc
      1 /*
      2  * Copyright (C) 2008 The Android Open Source Project
      3  * All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      8  *  * Redistributions of source code must retain the above copyright
      9  *    notice, this list of conditions and the following disclaimer.
     10  *  * Redistributions in binary form must reproduce the above copyright
     11  *    notice, this list of conditions and the following disclaimer in
     12  *    the documentation and/or other materials provided with the
     13  *    distribution.
     14  *
     15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     16  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     17  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
     18  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
     19  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
     22  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
     25  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  * SUCH DAMAGE.
     27  */
     28 
     29 #include "sha.h"
     30 
     31 #define rol(bits, value) (((value) << (bits)) | ((value) >> (32 - (bits))))
     32 
     33 static void SHA1_transform(SHA_CTX *ctx) {
     34     uint32_t W[80];
     35     uint32_t A, B, C, D, E;
     36     uint8_t *p = ctx->buf;
     37     int t;
     38 
     39     for(t = 0; t < 16; ++t) {
     40         uint32_t tmp =  *p++ << 24;
     41         tmp |= *p++ << 16;
     42         tmp |= *p++ << 8;
     43         tmp |= *p++;
     44         W[t] = tmp;
     45     }
     46 
     47     for(; t < 80; t++) {
     48         W[t] = rol(1,W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16]);
     49     }
     50 
     51     A = ctx->state[0];
     52     B = ctx->state[1];
     53     C = ctx->state[2];
     54     D = ctx->state[3];
     55     E = ctx->state[4];
     56 
     57     for(t = 0; t < 80; t++) {
     58         uint32_t tmp = rol(5,A) + E + W[t];
     59 
     60         if (t < 20)
     61             tmp += (D^(B&(C^D))) + 0x5A827999;
     62         else if ( t < 40)
     63             tmp += (B^C^D) + 0x6ED9EBA1;
     64         else if ( t < 60)
     65             tmp += ((B&C)|(D&(B|C))) + 0x8F1BBCDC;
     66         else
     67             tmp += (B^C^D) + 0xCA62C1D6;
     68 
     69         E = D;
     70         D = C;
     71         C = rol(30,B);
     72         B = A;
     73         A = tmp;
     74     }
     75 
     76     ctx->state[0] += A;
     77     ctx->state[1] += B;
     78     ctx->state[2] += C;
     79     ctx->state[3] += D;
     80     ctx->state[4] += E;
     81 }
     82 
     83 void SHA_init(SHA_CTX *ctx) {
     84     ctx->state[0] = 0x67452301;
     85     ctx->state[1] = 0xEFCDAB89;
     86     ctx->state[2] = 0x98BADCFE;
     87     ctx->state[3] = 0x10325476;
     88     ctx->state[4] = 0xC3D2E1F0;
     89     ctx->count = 0;
     90 }
     91 
     92 void SHA_update(SHA_CTX *ctx, const void *data, int len) {
     93     int i = ctx->count % sizeof(ctx->buf);
     94     const uint8_t* p = (const uint8_t*)data;
     95 
     96     ctx->count += len;
     97 
     98     while (len--) {
     99         ctx->buf[i++] = *p++;
    100         if (i == sizeof(ctx->buf)) {
    101             SHA1_transform(ctx);
    102             i = 0;
    103         }
    104     }
    105 }
    106 const uint8_t *SHA_final(SHA_CTX *ctx) {
    107     uint8_t *p = ctx->buf;
    108     uint64_t cnt = ctx->count * 8;
    109     int i;
    110 
    111     SHA_update(ctx, (uint8_t*)"\x80", 1);
    112     while ((ctx->count % sizeof(ctx->buf)) != (sizeof(ctx->buf) - 8)) {
    113         SHA_update(ctx, (uint8_t*)"\0", 1);
    114     }
    115     for (i = 0; i < 8; ++i) {
    116         uint8_t tmp = cnt >> ((7 - i) * 8);
    117         SHA_update(ctx, &tmp, 1);
    118     }
    119 
    120     for (i = 0; i < 5; i++) {
    121         uint32_t tmp = ctx->state[i];
    122         *p++ = tmp >> 24;
    123         *p++ = tmp >> 16;
    124         *p++ = tmp >> 8;
    125         *p++ = tmp >> 0;
    126     }
    127 
    128     return ctx->buf;
    129 }
    130 
    131 /* Convenience function */
    132 const uint8_t* SHA(const void *data, int len, uint8_t *digest) {
    133     const uint8_t *p;
    134     int i;
    135     SHA_CTX ctx;
    136     SHA_init(&ctx);
    137     SHA_update(&ctx, data, len);
    138     p = SHA_final(&ctx);
    139     for (i = 0; i < SHA_DIGEST_SIZE; ++i) {
    140         digest[i] = *p++;
    141     }
    142     return digest;
    143 }
    144