Mercurial > libavutil.hg
annotate sha1.c @ 346:e9937dcd0afd libavutil
Clarify comments about PIX_FMT_MONOWHITE/MONOBLACK
Thread: PIX_FMT_MONOWHITE
author | mbardiaux |
---|---|
date | Wed, 20 Jun 2007 08:18:04 +0000 |
parents | 88e453a0975d |
children | 47335869d5f7 |
rev | line source |
---|---|
288 | 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" | |
332 | 6 #include "bswap.h" |
288 | 7 #include "sha1.h" |
8 | |
9 typedef struct AVSHA1 { | |
10 uint64_t count; | |
11 uint8_t buffer[64]; | |
309
0498b2f2851a
put state[5] last so no padding is needed on arch where uint64_t needs 8byte alignment
michael
parents:
308
diff
changeset
|
12 uint32_t state[5]; |
288 | 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 */ | |
307 | 18 #define blk0(i) (block[i] = be2me_32(((uint32_t*)buffer)[i])) |
301 | 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); | |
288 | 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]){ | |
291
78b11473f66a
avoid silly ring buffer logic (faster with -O2, -O3 is always slower then -O2)
michael
parents:
290
diff
changeset
|
30 uint32_t block[80]; |
308 | 31 unsigned int i, a, b, c, d, e; |
290
8f02801da0f8
its faster to copy the data to the stack it seems ...
michael
parents:
289
diff
changeset
|
32 |
288 | 33 a = state[0]; |
34 b = state[1]; | |
35 c = state[2]; | |
36 d = state[3]; | |
37 e = state[4]; | |
310 | 38 #ifdef CONFIG_SMALL |
302 | 39 for(i=0; i<80; i++){ |
306 | 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); | |
302 | 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 } | |
299 | 52 e= d; |
53 d= c; | |
54 c= rol(b,30); | |
55 b= a; | |
56 a= t; | |
57 } | |
58 #else | |
301 | 59 for(i=0; i<15; i+=5){ |
288 | 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 } | |
301 | 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){ | |
288 | 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 } | |
299 | 72 #endif |
288 | 73 state[0] += a; |
74 state[1] += b; | |
75 state[2] += c; | |
76 state[3] += d; | |
77 state[4] += e; | |
78 } | |
79 | |
315 | 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; | |
288 | 87 } |
88 | |
315 | 89 void av_sha1_update(AVSHA1* ctx, uint8_t* data, unsigned int len){ |
288 | 90 unsigned int i, j; |
91 | |
315 | 92 j = ctx->count & 63; |
93 ctx->count += len; | |
311 | 94 #ifdef CONFIG_SMALL |
95 for( i = 0; i < len; i++ ){ | |
315 | 96 ctx->buffer[ j++ ] = data[i]; |
311 | 97 if( 64 == j ){ |
315 | 98 transform(ctx->state, ctx->buffer); |
311 | 99 j = 0; |
100 } | |
101 } | |
102 #else | |
288 | 103 if ((j + len) > 63) { |
315 | 104 memcpy(&ctx->buffer[j], data, (i = 64-j)); |
105 transform(ctx->state, ctx->buffer); | |
288 | 106 for ( ; i + 63 < len; i += 64) { |
315 | 107 transform(ctx->state, &data[i]); |
288 | 108 } |
304 | 109 j=0; |
288 | 110 } |
111 else i = 0; | |
315 | 112 memcpy(&ctx->buffer[j], &data[i], len - i); |
311 | 113 #endif |
288 | 114 } |
115 | |
315 | 116 void av_sha1_final(AVSHA1* ctx, uint8_t digest[20]){ |
288 | 117 int i; |
315 | 118 uint64_t finalcount= be2me_64(ctx->count<<3); |
288 | 119 |
315 | 120 av_sha1_update(ctx, "\200", 1); |
121 while ((ctx->count & 63) != 56) { | |
122 av_sha1_update(ctx, "", 1); | |
288 | 123 } |
315 | 124 av_sha1_update(ctx, &finalcount, 8); /* Should cause a transform() */ |
295 | 125 for(i=0; i<5; i++) |
315 | 126 ((uint32_t*)digest)[i]= be2me_32(ctx->state[i]); |
288 | 127 } |
128 | |
289 | 129 // use the following to test |
292 | 130 // gcc -DTEST -DHAVE_AV_CONFIG_H -I.. sha1.c -O2 -W -Wall -o sha1 && time ./sha1 |
288 | 131 #ifdef TEST |
132 #include <stdio.h> | |
133 #undef printf | |
134 | |
135 int main(){ | |
136 int i, k; | |
315 | 137 AVSHA1 ctx; |
288 | 138 unsigned char digest[20]; |
139 | |
140 for(k=0; k<3; k++){ | |
315 | 141 av_sha1_init(&ctx); |
288 | 142 if(k==0) |
315 | 143 av_sha1_update(&ctx, "abc", 3); |
288 | 144 else if(k==1) |
315 | 145 av_sha1_update(&ctx, "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", 56); |
288 | 146 else |
147 for(i=0; i<1000*1000; i++) | |
315 | 148 av_sha1_update(&ctx, "a", 1); |
149 av_sha1_final(&ctx, digest); | |
288 | 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 |