2
0

sha1s.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. //
  2. // gettsc.inl
  3. //
  4. // gives access to the Pentium's (secret) cycle counter
  5. //
  6. // This software was written by Leonard Janke (janke@unixg.ubc.ca)
  7. // in 1996-7 and is entered, by him, into the public domain.
  8. #if defined(__WATCOMC__)
  9. void GetTSC(unsigned long&);
  10. #pragma aux GetTSC = 0x0f 0x31 "mov [edi], eax" parm [edi] modify [edx eax];
  11. #elif defined(__GNUC__)
  12. inline
  13. void GetTSC(unsigned long& tsc)
  14. {
  15. asm volatile(".byte 15, 49\n\t"
  16. : "=eax" (tsc)
  17. :
  18. : "%edx", "%eax");
  19. }
  20. #elif defined(_MSC_VER)
  21. inline
  22. void GetTSC(unsigned long& tsc)
  23. {
  24. unsigned long a;
  25. __asm _emit 0fh
  26. __asm _emit 31h
  27. __asm mov a, eax;
  28. tsc=a;
  29. }
  30. #endif
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #include <openssl/sha.h>
  34. #define sha1_block_x86 sha1_block_asm_data_order
  35. extern "C" {
  36. void sha1_block_x86(SHA_CTX *ctx, unsigned char *buffer,int num);
  37. }
  38. void main(int argc,char *argv[])
  39. {
  40. unsigned char buffer[64*256];
  41. SHA_CTX ctx;
  42. unsigned long s1,s2,e1,e2;
  43. unsigned char k[16];
  44. unsigned long data[2];
  45. unsigned char iv[8];
  46. int i,num=0,numm;
  47. int j=0;
  48. if (argc >= 2)
  49. num=atoi(argv[1]);
  50. if (num == 0) num=16;
  51. if (num > 250) num=16;
  52. numm=num+2;
  53. #if 0
  54. num*=64;
  55. numm*=64;
  56. #endif
  57. for (j=0; j<6; j++)
  58. {
  59. for (i=0; i<10; i++) /**/
  60. {
  61. sha1_block_x86(&ctx,buffer,numm);
  62. GetTSC(s1);
  63. sha1_block_x86(&ctx,buffer,numm);
  64. GetTSC(e1);
  65. GetTSC(s2);
  66. sha1_block_x86(&ctx,buffer,num);
  67. GetTSC(e2);
  68. sha1_block_x86(&ctx,buffer,num);
  69. }
  70. printf("sha1 (%d bytes) %d %d (%.2f)\n",num*64,
  71. e1-s1,e2-s2,(double)((e1-s1)-(e2-s2))/2);
  72. }
  73. }