You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

162 lines
4.7KB

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