00001 /* 00002 * Copyright (C) 2007 Michael Niedermayer <michaelni@gmx.at> 00003 * based on public domain SHA-1 code by Steve Reid <steve@edmweb.com> 00004 * 00005 * This file is part of FFmpeg. 00006 * 00007 * FFmpeg is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU Lesser General Public 00009 * License as published by the Free Software Foundation; either 00010 * version 2.1 of the License, or (at your option) any later version. 00011 * 00012 * FFmpeg is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 * Lesser General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU Lesser General Public 00018 * License along with FFmpeg; if not, write to the Free Software 00019 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 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 const int av_sha1_size = sizeof(AVSHA1); 00033 00034 #define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits)))) 00035 00036 /* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */ 00037 #define blk0(i) (block[i] = be2me_32(((const uint32_t*)buffer)[i])) 00038 #define blk(i) (block[i] = rol(block[i-3]^block[i-8]^block[i-14]^block[i-16],1)) 00039 00040 #define R0(v,w,x,y,z,i) z+=((w&(x^y))^y) +blk0(i)+0x5A827999+rol(v,5);w=rol(w,30); 00041 #define R1(v,w,x,y,z,i) z+=((w&(x^y))^y) +blk (i)+0x5A827999+rol(v,5);w=rol(w,30); 00042 #define R2(v,w,x,y,z,i) z+=( w^x ^y) +blk (i)+0x6ED9EBA1+rol(v,5);w=rol(w,30); 00043 #define R3(v,w,x,y,z,i) z+=(((w|x)&y)|(w&x))+blk (i)+0x8F1BBCDC+rol(v,5);w=rol(w,30); 00044 #define R4(v,w,x,y,z,i) z+=( w^x ^y) +blk (i)+0xCA62C1D6+rol(v,5);w=rol(w,30); 00045 00046 /* Hash a single 512-bit block. This is the core of the algorithm. */ 00047 00048 static void transform(uint32_t state[5], const uint8_t buffer[64]){ 00049 uint32_t block[80]; 00050 unsigned int i, a, b, c, d, e; 00051 00052 a = state[0]; 00053 b = state[1]; 00054 c = state[2]; 00055 d = state[3]; 00056 e = state[4]; 00057 #if CONFIG_SMALL 00058 for(i=0; i<80; i++){ 00059 int t; 00060 if(i<16) t= be2me_32(((uint32_t*)buffer)[i]); 00061 else t= rol(block[i-3]^block[i-8]^block[i-14]^block[i-16],1); 00062 block[i]= t; 00063 t+= e+rol(a,5); 00064 if(i<40){ 00065 if(i<20) t+= ((b&(c^d))^d) +0x5A827999; 00066 else t+= ( b^c ^d) +0x6ED9EBA1; 00067 }else{ 00068 if(i<60) t+= (((b|c)&d)|(b&c))+0x8F1BBCDC; 00069 else t+= ( b^c ^d) +0xCA62C1D6; 00070 } 00071 e= d; 00072 d= c; 00073 c= rol(b,30); 00074 b= a; 00075 a= t; 00076 } 00077 #else 00078 for(i=0; i<15; i+=5){ 00079 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); 00080 } 00081 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); 00082 for(i=20; i<40; i+=5){ 00083 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); 00084 } 00085 for(; i<60; i+=5){ 00086 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); 00087 } 00088 for(; i<80; i+=5){ 00089 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); 00090 } 00091 #endif 00092 state[0] += a; 00093 state[1] += b; 00094 state[2] += c; 00095 state[3] += d; 00096 state[4] += e; 00097 } 00098 00099 void av_sha1_init(AVSHA1* ctx){ 00100 ctx->state[0] = 0x67452301; 00101 ctx->state[1] = 0xEFCDAB89; 00102 ctx->state[2] = 0x98BADCFE; 00103 ctx->state[3] = 0x10325476; 00104 ctx->state[4] = 0xC3D2E1F0; 00105 ctx->count = 0; 00106 } 00107 00108 void av_sha1_update(AVSHA1* ctx, const uint8_t* data, unsigned int len){ 00109 unsigned int i, j; 00110 00111 j = ctx->count & 63; 00112 ctx->count += len; 00113 #if CONFIG_SMALL 00114 for( i = 0; i < len; i++ ){ 00115 ctx->buffer[ j++ ] = data[i]; 00116 if( 64 == j ){ 00117 transform(ctx->state, ctx->buffer); 00118 j = 0; 00119 } 00120 } 00121 #else 00122 if ((j + len) > 63) { 00123 memcpy(&ctx->buffer[j], data, (i = 64-j)); 00124 transform(ctx->state, ctx->buffer); 00125 for ( ; i + 63 < len; i += 64) { 00126 transform(ctx->state, &data[i]); 00127 } 00128 j=0; 00129 } 00130 else i = 0; 00131 memcpy(&ctx->buffer[j], &data[i], len - i); 00132 #endif 00133 } 00134 00135 void av_sha1_final(AVSHA1* ctx, uint8_t digest[20]){ 00136 int i; 00137 uint64_t finalcount= be2me_64(ctx->count<<3); 00138 00139 av_sha1_update(ctx, "200円", 1); 00140 while ((ctx->count & 63) != 56) { 00141 av_sha1_update(ctx, "", 1); 00142 } 00143 av_sha1_update(ctx, (uint8_t *)&finalcount, 8); /* Should cause a transform() */ 00144 for(i=0; i<5; i++) 00145 ((uint32_t*)digest)[i]= be2me_32(ctx->state[i]); 00146 } 00147 00148 #ifdef TEST 00149 #include <stdio.h> 00150 #undef printf 00151 00152 int main(void){ 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 //test vectors (from FIPS PUB 180-1) 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