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