sm4test.c 979 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. /*
  2. * SM4/SMS4 algorithm test programme
  3. * 2012-4-21
  4. */
  5. #include <string.h>
  6. #include <stdio.h>
  7. #include "sm4.h"
  8. int main()
  9. {
  10. unsigned char key[16] = {0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,0xfe,0xdc,0xba,0x98,0x76,0x54,0x32,0x10};
  11. unsigned char input[16] = {0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,0xfe,0xdc,0xba,0x98,0x76,0x54,0x32,0x10};
  12. unsigned char output[16];
  13. sm4_context ctx;
  14. unsigned long i;
  15. //encrypt standard testing vector
  16. sm4_setkey_enc(&ctx,key);
  17. sm4_crypt_ecb(&ctx,1,16,input,output);
  18. for(i=0;i<16;i++)
  19. printf("%02x ", output[i]);
  20. printf("\n");
  21. //decrypt testing
  22. sm4_setkey_dec(&ctx,key);
  23. sm4_crypt_ecb(&ctx,0,16,output,output);
  24. for(i=0;i<16;i++)
  25. printf("%02x ", output[i]);
  26. printf("\n");
  27. //decrypt 1M times testing vector based on standards.
  28. i = 0;
  29. sm4_setkey_enc(&ctx,key);
  30. while (i<1000000)
  31. {
  32. sm4_crypt_ecb(&ctx,1,16,input,input);
  33. i++;
  34. }
  35. for(i=0;i<16;i++)
  36. printf("%02x ", input[i]);
  37. printf("\n");
  38. return 0;
  39. }