sha.h 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #ifndef _SHA_H
  2. #define _SHA_H
  3. #define SHA_LONG unsigned int
  4. #define SHA_LBLOCK 16
  5. #define SHA_CBLOCK (SHA_LBLOCK*4) /* SHA treats input data as a
  6. * contiguous array of 32 bit
  7. * wide big-endian values. */
  8. #define SHA_LAST_BLOCK (SHA_CBLOCK-8)
  9. #define SHA_DIGEST_LENGTH 20
  10. #define HOST_c2l(c,l) (l =(((unsigned long)(*((c)++)))<<24), \
  11. l|=(((unsigned long)(*((c)++)))<<16), \
  12. l|=(((unsigned long)(*((c)++)))<< 8), \
  13. l|=(((unsigned long)(*((c)++))) ), \
  14. l)
  15. #define HOST_p_c2l(c,l,n) { \
  16. switch (n) { \
  17. case 0: l =((unsigned long)(*((c)++)))<<24; \
  18. case 1: l|=((unsigned long)(*((c)++)))<<16; \
  19. case 2: l|=((unsigned long)(*((c)++)))<< 8; \
  20. case 3: l|=((unsigned long)(*((c)++))); \
  21. } }
  22. #define HOST_p_c2l_p(c,l,sc,len) { \
  23. switch (sc) { \
  24. case 0: l =((unsigned long)(*((c)++)))<<24; \
  25. if (--len == 0) break; \
  26. case 1: l|=((unsigned long)(*((c)++)))<<16; \
  27. if (--len == 0) break; \
  28. case 2: l|=((unsigned long)(*((c)++)))<< 8; \
  29. } }
  30. /* NOTE the pointer is not incremented at the end of this */
  31. #define HOST_c2l_p(c,l,n) { \
  32. l=0; (c)+=n; \
  33. switch (n) { \
  34. case 3: l =((unsigned long)(*(--(c))))<< 8; \
  35. case 2: l|=((unsigned long)(*(--(c))))<<16; \
  36. case 1: l|=((unsigned long)(*(--(c))))<<24; \
  37. } }
  38. #define HOST_l2c(l,c) (*((c)++)=(unsigned char)(((l)>>24)&0xff), \
  39. *((c)++)=(unsigned char)(((l)>>16)&0xff), \
  40. *((c)++)=(unsigned char)(((l)>> 8)&0xff), \
  41. *((c)++)=(unsigned char)(((l) )&0xff), \
  42. l)
  43. #define SHA_MAKE_STRING(c,s) do { \
  44. unsigned long ll; \
  45. ll=(c)->h0; HOST_l2c(ll,(s)); \
  46. ll=(c)->h1; HOST_l2c(ll,(s)); \
  47. ll=(c)->h2; HOST_l2c(ll,(s)); \
  48. ll=(c)->h3; HOST_l2c(ll,(s)); \
  49. ll=(c)->h4; HOST_l2c(ll,(s)); \
  50. } while (0)
  51. typedef struct SHAstate_st
  52. {
  53. SHA_LONG h0,h1,h2,h3,h4;
  54. SHA_LONG Nl,Nh;
  55. SHA_LONG data[SHA_LBLOCK];
  56. int num;
  57. } SHA_CTX;
  58. int SHA1_Init(SHA_CTX *);
  59. int SHA1_Update(SHA_CTX *, const void *, unsigned long);
  60. int SHA1_Final(unsigned char *, SHA_CTX *);
  61. void SHA1_Transform(SHA_CTX *, const unsigned char *);
  62. #endif /* !_SHA_H */