use the right stride
[libav.git] / libavutil / sha1.c
CommitLineData
7c60e55b
MN
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"
69ae9478 6#include "bswap.h"
7c60e55b
MN
7#include "sha1.h"
8
9typedef struct AVSHA1 {
7c60e55b
MN
10 uint64_t count;
11 uint8_t buffer[64];
20013045 12 uint32_t state[5];
7c60e55b
MN
13} AVSHA1;
14
15#define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
16
7c60e55b 17/* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */
15b6cae2 18#define blk0(i) (block[i] = be2me_32(((uint32_t*)buffer)[i]))
d6cf7804
MN
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);
7c60e55b
MN
26
27/* Hash a single 512-bit block. This is the core of the algorithm. */
28
29static void transform(uint32_t state[5], uint8_t buffer[64]){
aa59433a 30 uint32_t block[80];
5fd7f87b 31 unsigned int i, a, b, c, d, e;
3479b72b 32
7c60e55b
MN
33 a = state[0];
34 b = state[1];
35 c = state[2];
36 d = state[3];
37 e = state[4];
104c30ee 38#ifdef CONFIG_SMALL
2fa3a22d 39 for(i=0; i<80; i++){
476f9b74
MN
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);
2fa3a22d
MN
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 }
6573578d
MN
52 e= d;
53 d= c;
54 c= rol(b,30);
55 b= a;
56 a= t;
57 }
58#else
d6cf7804 59 for(i=0; i<15; i+=5){
7c60e55b
MN
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 }
d6cf7804
MN
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){
7c60e55b
MN
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 }
6573578d 72#endif
7c60e55b
MN
73 state[0] += a;
74 state[1] += b;
75 state[2] += c;
76 state[3] += d;
77 state[4] += e;
78}
79
248b25f8
MN
80void 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;
7c60e55b
MN
87}
88
248b25f8 89void av_sha1_update(AVSHA1* ctx, uint8_t* data, unsigned int len){
7c60e55b
MN
90 unsigned int i, j;
91
248b25f8
MN
92 j = ctx->count & 63;
93 ctx->count += len;
36c7fa7e
MN
94#ifdef CONFIG_SMALL
95 for( i = 0; i < len; i++ ){
248b25f8 96 ctx->buffer[ j++ ] = data[i];
36c7fa7e 97 if( 64 == j ){
248b25f8 98 transform(ctx->state, ctx->buffer);
36c7fa7e
MN
99 j = 0;
100 }
101 }
102#else
7c60e55b 103 if ((j + len) > 63) {
248b25f8
MN
104 memcpy(&ctx->buffer[j], data, (i = 64-j));
105 transform(ctx->state, ctx->buffer);
7c60e55b 106 for ( ; i + 63 < len; i += 64) {
248b25f8 107 transform(ctx->state, &data[i]);
7c60e55b 108 }
965ea9ba 109 j=0;
7c60e55b
MN
110 }
111 else i = 0;
248b25f8 112 memcpy(&ctx->buffer[j], &data[i], len - i);
36c7fa7e 113#endif
7c60e55b
MN
114}
115
248b25f8 116void av_sha1_final(AVSHA1* ctx, uint8_t digest[20]){
7c60e55b 117 int i;
248b25f8 118 uint64_t finalcount= be2me_64(ctx->count<<3);
7c60e55b 119
248b25f8
MN
120 av_sha1_update(ctx, "\200", 1);
121 while ((ctx->count & 63) != 56) {
122 av_sha1_update(ctx, "", 1);
7c60e55b 123 }
248b25f8 124 av_sha1_update(ctx, &finalcount, 8); /* Should cause a transform() */
c6fd617a 125 for(i=0; i<5; i++)
248b25f8 126 ((uint32_t*)digest)[i]= be2me_32(ctx->state[i]);
7c60e55b
MN
127}
128
ae76034c 129// use the following to test
82da03f7 130// gcc -DTEST -DHAVE_AV_CONFIG_H -I.. sha1.c -O2 -W -Wall -o sha1 && time ./sha1
7c60e55b
MN
131#ifdef TEST
132#include <stdio.h>
133#undef printf
134
135int main(){
136 int i, k;
248b25f8 137 AVSHA1 ctx;
7c60e55b
MN
138 unsigned char digest[20];
139
140 for(k=0; k<3; k++){
248b25f8 141 av_sha1_init(&ctx);
7c60e55b 142 if(k==0)
248b25f8 143 av_sha1_update(&ctx, "abc", 3);
7c60e55b 144 else if(k==1)
248b25f8 145 av_sha1_update(&ctx, "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", 56);
7c60e55b
MN
146 else
147 for(i=0; i<1000*1000; i++)
248b25f8
MN
148 av_sha1_update(&ctx, "a", 1);
149 av_sha1_final(&ctx, digest);
7c60e55b
MN
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