]> git.sesse.net Git - ffmpeg/blob - libavutil/sha1.c
82416e8fe73719ab182491348aa25f1b6040a3fd
[ffmpeg] / libavutil / sha1.c
1 // SHA-1 code Copyright 2007 Michael Nidermayer <michaelni@gmx.at>
2 // license LGPL
3 // based on public domain SHA-1 code by Steve Reid <steve@edmweb.com>
4
5 #include "common.h"
6 #include "sha1.h"
7
8 typedef struct AVSHA1 {
9     uint64_t count;
10     uint8_t buffer[64];
11     uint32_t state[5];
12 } AVSHA1;
13
14 #define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
15
16 /* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */
17 #define blk0(i) (block[i] = be2me_32(((uint32_t*)buffer)[i]))
18 #define blk(i) (block[i] = rol(block[i-3]^block[i-8]^block[i-14]^block[i-16],1))
19
20 #define R0(v,w,x,y,z,i) z+=((w&(x^y))^y)    +blk0(i)+0x5A827999+rol(v,5);w=rol(w,30);
21 #define R1(v,w,x,y,z,i) z+=((w&(x^y))^y)    +blk (i)+0x5A827999+rol(v,5);w=rol(w,30);
22 #define R2(v,w,x,y,z,i) z+=( w^x     ^y)    +blk (i)+0x6ED9EBA1+rol(v,5);w=rol(w,30);
23 #define R3(v,w,x,y,z,i) z+=(((w|x)&y)|(w&x))+blk (i)+0x8F1BBCDC+rol(v,5);w=rol(w,30);
24 #define R4(v,w,x,y,z,i) z+=( w^x     ^y)    +blk (i)+0xCA62C1D6+rol(v,5);w=rol(w,30);
25
26 /* Hash a single 512-bit block. This is the core of the algorithm. */
27
28 //#define VARIANT2
29
30 static void transform(uint32_t state[5], uint8_t buffer[64]){
31     uint32_t block[80];
32     unsigned int i, a, b, c, d, e;
33
34     a = state[0];
35     b = state[1];
36     c = state[2];
37     d = state[3];
38     e = state[4];
39 #ifdef VARIANT2
40     for(i=0; i<80; i++){
41         int t;
42         if(i<16) t= be2me_32(((uint32_t*)buffer)[i]);
43         else     t= rol(block[i-3]^block[i-8]^block[i-14]^block[i-16],1);
44         block[i]= t;
45         t+= e+rol(a,5);
46         if(i<40){
47             if(i<20)    t+= ((b&(c^d))^d)    +0x5A827999;
48             else        t+= ( b^c     ^d)    +0x6ED9EBA1;
49         }else{
50             if(i<60)    t+= (((b|c)&d)|(b&c))+0x8F1BBCDC;
51             else        t+= ( b^c     ^d)    +0xCA62C1D6;
52         }
53         e= d;
54         d= c;
55         c= rol(b,30);
56         b= a;
57         a= t;
58     }
59 #else
60     for(i=0; i<15; i+=5){
61         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);
62     }
63     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);
64     for(i=20; i<40; i+=5){
65         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);
66     }
67     for(; i<60; i+=5){
68         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);
69     }
70     for(; i<80; i+=5){
71         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);
72     }
73 #endif
74     state[0] += a;
75     state[1] += b;
76     state[2] += c;
77     state[3] += d;
78     state[4] += e;
79 }
80
81 void av_sha1_init(AVSHA1* context){
82     context->state[0] = 0x67452301;
83     context->state[1] = 0xEFCDAB89;
84     context->state[2] = 0x98BADCFE;
85     context->state[3] = 0x10325476;
86     context->state[4] = 0xC3D2E1F0;
87     context->count    = 0;
88 }
89
90 void av_sha1_update(AVSHA1* context, uint8_t* data, unsigned int len){
91     unsigned int i, j;
92
93     j = context->count & 63;
94     context->count += len;
95     if ((j + len) > 63) {
96         memcpy(&context->buffer[j], data, (i = 64-j));
97         transform(context->state, context->buffer);
98         for ( ; i + 63 < len; i += 64) {
99             transform(context->state, &data[i]);
100         }
101         j=0;
102     }
103     else i = 0;
104     memcpy(&context->buffer[j], &data[i], len - i);
105 }
106
107 void av_sha1_final(AVSHA1* context, uint8_t digest[20]){
108     int i;
109     uint64_t finalcount= be2me_64(context->count<<3);
110
111     av_sha1_update(context, "\200", 1);
112     while ((context->count & 63) != 56) {
113         av_sha1_update(context, "", 1);
114     }
115     av_sha1_update(context, &finalcount, 8);  /* Should cause a transform() */
116     for(i=0; i<5; i++)
117         ((uint32_t*)digest)[i]= be2me_32(context->state[i]);
118 }
119
120 // use the following to test
121 // gcc -DTEST -DHAVE_AV_CONFIG_H -I.. sha1.c -O2 -W -Wall -o sha1 && time ./sha1
122 #ifdef TEST
123 #include <stdio.h>
124 #undef printf
125
126 int main(){
127     int i, k;
128     AVSHA1 context;
129     unsigned char digest[20];
130
131     for(k=0; k<3; k++){
132         av_sha1_init(&context);
133         if(k==0)
134             av_sha1_update(&context, "abc", 3);
135         else if(k==1)
136             av_sha1_update(&context, "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", 56);
137         else
138             for(i=0; i<1000*1000; i++)
139                 av_sha1_update(&context, "a", 1);
140         av_sha1_final(&context, digest);
141         for (i = 0; i < 20; i++)
142             printf("%02X", digest[i]);
143         putchar('\n');
144     }
145     //Test Vectors (from FIPS PUB 180-1)
146     printf("A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D\n"
147            "84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1\n"
148            "34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F\n");
149
150     return 0;
151 }
152 #endif