00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "common.h"
00023 #include "bswap.h"
00024 #include "sha1.h"
00025
00026 typedef struct AVSHA1 {
00027 uint64_t count;
00028 uint8_t buffer[64];
00029 uint32_t state[5];
00030 } AVSHA1;
00031
00032 #define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
00033
00034
00035 #define blk0(i) (block[i] = be2me_32(((uint32_t*)buffer)[i]))
00036 #define blk(i) (block[i] = rol(block[i-3]^block[i-8]^block[i-14]^block[i-16],1))
00037
00038 #define R0(v,w,x,y,z,i) z+=((w&(x^y))^y) +blk0(i)+0x5A827999+rol(v,5);w=rol(w,30);
00039 #define R1(v,w,x,y,z,i) z+=((w&(x^y))^y) +blk (i)+0x5A827999+rol(v,5);w=rol(w,30);
00040 #define R2(v,w,x,y,z,i) z+=( w^x ^y) +blk (i)+0x6ED9EBA1+rol(v,5);w=rol(w,30);
00041 #define R3(v,w,x,y,z,i) z+=(((w|x)&y)|(w&x))+blk (i)+0x8F1BBCDC+rol(v,5);w=rol(w,30);
00042 #define R4(v,w,x,y,z,i) z+=( w^x ^y) +blk (i)+0xCA62C1D6+rol(v,5);w=rol(w,30);
00043
00044
00045
00046 static void transform(uint32_t state[5], uint8_t buffer[64]){
00047 uint32_t block[80];
00048 unsigned int i, a, b, c, d, e;
00049
00050 a = state[0];
00051 b = state[1];
00052 c = state[2];
00053 d = state[3];
00054 e = state[4];
00055 #ifdef CONFIG_SMALL
00056 for(i=0; i<80; i++){
00057 int t;
00058 if(i<16) t= be2me_32(((uint32_t*)buffer)[i]);
00059 else t= rol(block[i-3]^block[i-8]^block[i-14]^block[i-16],1);
00060 block[i]= t;
00061 t+= e+rol(a,5);
00062 if(i<40){
00063 if(i<20) t+= ((b&(c^d))^d) +0x5A827999;
00064 else t+= ( b^c ^d) +0x6ED9EBA1;
00065 }else{
00066 if(i<60) t+= (((b|c)&d)|(b&c))+0x8F1BBCDC;
00067 else t+= ( b^c ^d) +0xCA62C1D6;
00068 }
00069 e= d;
00070 d= c;
00071 c= rol(b,30);
00072 b= a;
00073 a= t;
00074 }
00075 #else
00076 for(i=0; i<15; i+=5){
00077 R0(a,b,c,d,e,0+i); R0(e,a,b,c,d,1+i); R0(d,e,a,b,c,2+i); R0(c,d,e,a,b,3+i); R0(b,c,d,e,a,4+i);
00078 }
00079 R0(a,b,c,d,e,15); 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);
00080 for(i=20; i<40; i+=5){
00081 R2(a,b,c,d,e,0+i); R2(e,a,b,c,d,1+i); R2(d,e,a,b,c,2+i); R2(c,d,e,a,b,3+i); R2(b,c,d,e,a,4+i);
00082 }
00083 for(; i<60; i+=5){
00084 R3(a,b,c,d,e,0+i); R3(e,a,b,c,d,1+i); R3(d,e,a,b,c,2+i); R3(c,d,e,a,b,3+i); R3(b,c,d,e,a,4+i);
00085 }
00086 for(; i<80; i+=5){
00087 R4(a,b,c,d,e,0+i); R4(e,a,b,c,d,1+i); R4(d,e,a,b,c,2+i); R4(c,d,e,a,b,3+i); R4(b,c,d,e,a,4+i);
00088 }
00089 #endif
00090 state[0] += a;
00091 state[1] += b;
00092 state[2] += c;
00093 state[3] += d;
00094 state[4] += e;
00095 }
00096
00097 void av_sha1_init(AVSHA1* ctx){
00098 ctx->state[0] = 0x67452301;
00099 ctx->state[1] = 0xEFCDAB89;
00100 ctx->state[2] = 0x98BADCFE;
00101 ctx->state[3] = 0x10325476;
00102 ctx->state[4] = 0xC3D2E1F0;
00103 ctx->count = 0;
00104 }
00105
00106 void av_sha1_update(AVSHA1* ctx, uint8_t* data, unsigned int len){
00107 unsigned int i, j;
00108
00109 j = ctx->count & 63;
00110 ctx->count += len;
00111 #ifdef CONFIG_SMALL
00112 for( i = 0; i < len; i++ ){
00113 ctx->buffer[ j++ ] = data[i];
00114 if( 64 == j ){
00115 transform(ctx->state, ctx->buffer);
00116 j = 0;
00117 }
00118 }
00119 #else
00120 if ((j + len) > 63) {
00121 memcpy(&ctx->buffer[j], data, (i = 64-j));
00122 transform(ctx->state, ctx->buffer);
00123 for ( ; i + 63 < len; i += 64) {
00124 transform(ctx->state, &data[i]);
00125 }
00126 j=0;
00127 }
00128 else i = 0;
00129 memcpy(&ctx->buffer[j], &data[i], len - i);
00130 #endif
00131 }
00132
00133 void av_sha1_final(AVSHA1* ctx, uint8_t digest[20]){
00134 int i;
00135 uint64_t finalcount= be2me_64(ctx->count<<3);
00136
00137 av_sha1_update(ctx, "\200", 1);
00138 while ((ctx->count & 63) != 56) {
00139 av_sha1_update(ctx, "", 1);
00140 }
00141 av_sha1_update(ctx, &finalcount, 8);
00142 for(i=0; i<5; i++)
00143 ((uint32_t*)digest)[i]= be2me_32(ctx->state[i]);
00144 }
00145
00146
00147
00148 #ifdef TEST
00149 #include <stdio.h>
00150 #undef printf
00151
00152 int main(){
00153 int i, k;
00154 AVSHA1 ctx;
00155 unsigned char digest[20];
00156
00157 for(k=0; k<3; k++){
00158 av_sha1_init(&ctx);
00159 if(k==0)
00160 av_sha1_update(&ctx, "abc", 3);
00161 else if(k==1)
00162 av_sha1_update(&ctx, "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", 56);
00163 else
00164 for(i=0; i<1000*1000; i++)
00165 av_sha1_update(&ctx, "a", 1);
00166 av_sha1_final(&ctx, digest);
00167 for (i = 0; i < 20; i++)
00168 printf("%02X", digest[i]);
00169 putchar('\n');
00170 }
00171
00172 printf("A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D\n"
00173 "84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1\n"
00174 "34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F\n");
00175
00176 return 0;
00177 }
00178 #endif