aestst.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. ---------------------------------------------------------------------------
  3. Copyright (c) 1998-2008, Brian Gladman, Worcester, UK. All rights reserved.
  4. LICENSE TERMS
  5. The redistribution and use of this software (with or without changes)
  6. is allowed without the payment of fees or royalties provided that:
  7. 1. source code distributions include the above copyright notice, this
  8. list of conditions and the following disclaimer;
  9. 2. binary distributions include the above copyright notice, this list
  10. of conditions and the following disclaimer in their documentation;
  11. 3. the name of the copyright holder is not used to endorse products
  12. built using this software without specific written permission.
  13. DISCLAIMER
  14. This software is provided 'as is' with no explicit or implied warranties
  15. in respect of its properties, including, but not limited to, correctness
  16. and/or fitness for purpose.
  17. ---------------------------------------------------------------------------
  18. Issue Date: 20/12/2007
  19. */
  20. // Correct Output (for variable block size - AES_BLOCK_SIZE undefined):
  21. // lengths: block = 16 bytes, key = 16 bytes
  22. // key = 2b7e151628aed2a6abf7158809cf4f3c
  23. // input = 3243f6a8885a308d313198a2e0370734
  24. // encrypt = 3925841d02dc09fbdc118597196a0b32
  25. // decrypt = 3243f6a8885a308d313198a2e0370734
  26. // lengths: block = 16 bytes, key = 24 bytes
  27. // key = 2b7e151628aed2a6abf7158809cf4f3c762e7160f38b4da5
  28. // input = 3243f6a8885a308d313198a2e0370734
  29. // encrypt = f9fb29aefc384a250340d833b87ebc00
  30. // decrypt = 3243f6a8885a308d313198a2e0370734
  31. // lengths: block = 16 bytes, key = 32 bytes
  32. // key = 2b7e151628aed2a6abf7158809cf4f3c762e7160f38b4da56a784d9045190cfe
  33. // input = 3243f6a8885a308d313198a2e0370734
  34. // encrypt = 1a6e6c2c662e7da6501ffb62bc9e93f3
  35. // decrypt = 3243f6a8885a308d313198a2e0370734
  36. #include <stdio.h>
  37. #include <string.h>
  38. #include "aes.h"
  39. #include "aestst.h"
  40. void out_state(long s0, long s1, long s2, long s3)
  41. {
  42. printf("\n%08lx%08lx%08lx%08lx", s0, s1, s2, s3);
  43. }
  44. void oblk(char m[], unsigned char v[], unsigned long n)
  45. { unsigned long i;
  46. printf("\n%s", m);
  47. for(i = 0; i < n; ++i)
  48. printf("%02x", v[i]);
  49. }
  50. void message(const char *s) { printf("%s", s); }
  51. unsigned char pih[32] = // hex digits of pi
  52. {
  53. 0x32, 0x43, 0xf6, 0xa8, 0x88, 0x5a, 0x30, 0x8d,
  54. 0x31, 0x31, 0x98, 0xa2, 0xe0, 0x37, 0x07, 0x34,
  55. 0x4a, 0x40, 0x93, 0x82, 0x22, 0x99, 0xf3, 0x1d,
  56. 0x00, 0x82, 0xef, 0xa9, 0x8e, 0xc4, 0xe6, 0xc8
  57. };
  58. unsigned char exh[32] = // hex digits of e
  59. {
  60. 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
  61. 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c,
  62. 0x76, 0x2e, 0x71, 0x60, 0xf3, 0x8b, 0x4d, 0xa5,
  63. 0x6a, 0x78, 0x4d, 0x90, 0x45, 0x19, 0x0c, 0xfe
  64. };
  65. unsigned char res[3][32] =
  66. {
  67. { 0x39, 0x25, 0x84, 0x1d, 0x02, 0xdc, 0x09, 0xfb,
  68. 0xdc, 0x11, 0x85, 0x97, 0x19, 0x6a, 0x0b, 0x32
  69. },
  70. { 0xf9, 0xfb, 0x29, 0xae, 0xfc, 0x38, 0x4a, 0x25,
  71. 0x03, 0x40, 0xd8, 0x33, 0xb8, 0x7e, 0xbc, 0x00
  72. },
  73. { 0x1a, 0x6e, 0x6c, 0x2c, 0x66, 0x2e, 0x7d, 0xa6,
  74. 0x50, 0x1f, 0xfb, 0x62, 0xbc, 0x9e, 0x93, 0xf3
  75. }
  76. };
  77. // void cycles(volatile uint64_t *rtn)
  78. // {
  79. // #if defined( _MSCVER )
  80. // __asm // read the Pentium Time Stamp Counter
  81. // { cpuid
  82. // rdtsc
  83. // mov ecx,rtn
  84. // mov [ecx],eax
  85. // mov [ecx+4],edx
  86. // cpuid
  87. // }
  88. // #elif defined( __GNUC__ )
  89. // #if defined(__aarch64__)
  90. // __asm__ __volatile__("mrs %0, cntvct_el0": "=r" (*rtn));
  91. // #else
  92. // __asm__ __volatile__("rdtsc": "=A" (*rtn));
  93. // #endif
  94. // #endif
  95. // }
  96. int main(void)
  97. { unsigned char out[32], ret[32], err = 0;
  98. f_ectx alge[1];
  99. f_dctx algd[1];
  100. aes_init();
  101. message("\nRun tests for the AES algorithm");
  102. memset(&alge, 0, sizeof(aes_encrypt_ctx));
  103. memset(&algd, 0, sizeof(aes_decrypt_ctx));
  104. #if defined( AES_128 )
  105. memset(out, 0xcc, 16); memset(ret, 0xcc, 16);
  106. printf("\n\n// lengths: block = 16, bytes, key = 16 bytes");
  107. f_enc_key128(alge, exh);
  108. oblk("// key = ", exh, 16);
  109. oblk("// input = ", pih, 16);
  110. do_enc(alge, pih, out, 1);
  111. oblk("// encrypt = ", out, 16);
  112. if(memcmp(out, res[0], 16)) { message (" error"); err += 1; }
  113. f_dec_key128(algd, exh);
  114. do_dec(algd, out, ret, 1);
  115. oblk("// decrypt = ", ret, 16);
  116. if(memcmp(ret, pih, 16)) { message (" error"); err += 2; }
  117. #endif
  118. #if defined( AES_192 )
  119. memset(out, 0xcc, 16); memset(ret, 0xcc, 16);
  120. printf("\n\n// lengths: block = 16, bytes, key = 24 bytes");
  121. f_enc_key192(alge, exh);
  122. oblk("// key = ", exh, 24);
  123. oblk("// input = ", pih, 16);
  124. do_enc(alge, pih, out, 1);
  125. oblk("// encrypt = ", out, 16);
  126. if(memcmp(out, res[1], 16)) { message (" error"); err += 4; }
  127. f_dec_key192(algd, exh);
  128. do_dec(algd, out, ret, 1);
  129. oblk("// decrypt = ", ret, 16);
  130. if(memcmp(ret, pih, 16)) { message (" error"); err += 8; }
  131. #endif
  132. #if defined( AES_256 )
  133. memset(out, 0xcc, 16); memset(ret, 0xcc, 16);
  134. printf("\n\n// lengths: block = 16, bytes, key = 32 bytes");
  135. f_enc_key256(alge, exh);
  136. oblk("// key = ", exh, 32);
  137. oblk("// input = ", pih, 16);
  138. do_enc(alge, pih, out, 1);
  139. oblk("// encrypt = ", out, 16);
  140. if(memcmp(out, res[2], 16)) { message (" error"); err += 16; }
  141. f_dec_key256(algd, exh);
  142. do_dec(algd, out, ret, 1);
  143. oblk("// decrypt = ", ret, 16);
  144. if(memcmp(ret, pih, 16)) { message (" error"); err += 32; }
  145. #endif
  146. if(!err)
  147. message("\n\nThese values are all correct\n\n");
  148. else
  149. message("\n\nSome values are in error\n\n");
  150. return 0;
  151. }