aestst.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. printf("\n%08lx%08lx%08lx%08lx", s0, s1, s2, s3);
  42. }
  43. void oblk(char m[], unsigned char v[], unsigned long n) {
  44. unsigned long i;
  45. printf("\n%s", m);
  46. for(i = 0; i < n; ++i) printf("%02x", v[i]);
  47. }
  48. void message(const char* s) {
  49. printf("%s", s);
  50. }
  51. unsigned char pih[32] = // hex digits of pi
  52. {0x32, 0x43, 0xf6, 0xa8, 0x88, 0x5a, 0x30, 0x8d, 0x31, 0x31, 0x98,
  53. 0xa2, 0xe0, 0x37, 0x07, 0x34, 0x4a, 0x40, 0x93, 0x82, 0x22, 0x99,
  54. 0xf3, 0x1d, 0x00, 0x82, 0xef, 0xa9, 0x8e, 0xc4, 0xe6, 0xc8};
  55. unsigned char exh[32] = // hex digits of e
  56. {0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15,
  57. 0x88, 0x09, 0xcf, 0x4f, 0x3c, 0x76, 0x2e, 0x71, 0x60, 0xf3, 0x8b,
  58. 0x4d, 0xa5, 0x6a, 0x78, 0x4d, 0x90, 0x45, 0x19, 0x0c, 0xfe};
  59. unsigned char res[3][32] = {
  60. {0x39, 0x25, 0x84, 0x1d, 0x02, 0xdc, 0x09, 0xfb, 0xdc, 0x11, 0x85, 0x97, 0x19, 0x6a, 0x0b, 0x32},
  61. {0xf9, 0xfb, 0x29, 0xae, 0xfc, 0x38, 0x4a, 0x25, 0x03, 0x40, 0xd8, 0x33, 0xb8, 0x7e, 0xbc, 0x00},
  62. {0x1a, 0x6e, 0x6c, 0x2c, 0x66, 0x2e, 0x7d, 0xa6, 0x50, 0x1f, 0xfb, 0x62, 0xbc, 0x9e, 0x93, 0xf3}};
  63. // void cycles(volatile uint64_t *rtn)
  64. // {
  65. // #if defined( _MSCVER )
  66. // __asm // read the Pentium Time Stamp Counter
  67. // { cpuid
  68. // rdtsc
  69. // mov ecx,rtn
  70. // mov [ecx],eax
  71. // mov [ecx+4],edx
  72. // cpuid
  73. // }
  74. // #elif defined( __GNUC__ )
  75. // #if defined(__aarch64__)
  76. // __asm__ __volatile__("mrs %0, cntvct_el0": "=r" (*rtn));
  77. // #else
  78. // __asm__ __volatile__("rdtsc": "=A" (*rtn));
  79. // #endif
  80. // #endif
  81. // }
  82. int main(void) {
  83. unsigned char out[32], ret[32], err = 0;
  84. f_ectx alge[1];
  85. f_dctx algd[1];
  86. aes_init();
  87. message("\nRun tests for the AES algorithm");
  88. memset(&alge, 0, sizeof(aes_encrypt_ctx));
  89. memset(&algd, 0, sizeof(aes_decrypt_ctx));
  90. #if defined(AES_128)
  91. memset(out, 0xcc, 16);
  92. memset(ret, 0xcc, 16);
  93. printf("\n\n// lengths: block = 16, bytes, key = 16 bytes");
  94. f_enc_key128(alge, exh);
  95. oblk("// key = ", exh, 16);
  96. oblk("// input = ", pih, 16);
  97. do_enc(alge, pih, out, 1);
  98. oblk("// encrypt = ", out, 16);
  99. if(memcmp(out, res[0], 16)) {
  100. message(" error");
  101. err += 1;
  102. }
  103. f_dec_key128(algd, exh);
  104. do_dec(algd, out, ret, 1);
  105. oblk("// decrypt = ", ret, 16);
  106. if(memcmp(ret, pih, 16)) {
  107. message(" error");
  108. err += 2;
  109. }
  110. #endif
  111. #if defined(AES_192)
  112. memset(out, 0xcc, 16);
  113. memset(ret, 0xcc, 16);
  114. printf("\n\n// lengths: block = 16, bytes, key = 24 bytes");
  115. f_enc_key192(alge, exh);
  116. oblk("// key = ", exh, 24);
  117. oblk("// input = ", pih, 16);
  118. do_enc(alge, pih, out, 1);
  119. oblk("// encrypt = ", out, 16);
  120. if(memcmp(out, res[1], 16)) {
  121. message(" error");
  122. err += 4;
  123. }
  124. f_dec_key192(algd, exh);
  125. do_dec(algd, out, ret, 1);
  126. oblk("// decrypt = ", ret, 16);
  127. if(memcmp(ret, pih, 16)) {
  128. message(" error");
  129. err += 8;
  130. }
  131. #endif
  132. #if defined(AES_256)
  133. memset(out, 0xcc, 16);
  134. memset(ret, 0xcc, 16);
  135. printf("\n\n// lengths: block = 16, bytes, key = 32 bytes");
  136. f_enc_key256(alge, exh);
  137. oblk("// key = ", exh, 32);
  138. oblk("// input = ", pih, 16);
  139. do_enc(alge, pih, out, 1);
  140. oblk("// encrypt = ", out, 16);
  141. if(memcmp(out, res[2], 16)) {
  142. message(" error");
  143. err += 16;
  144. }
  145. f_dec_key256(algd, exh);
  146. do_dec(algd, out, ret, 1);
  147. oblk("// decrypt = ", ret, 16);
  148. if(memcmp(ret, pih, 16)) {
  149. message(" error");
  150. err += 32;
  151. }
  152. #endif
  153. if(!err)
  154. message("\n\nThese values are all correct\n\n");
  155. else
  156. message("\n\nSome values are in error\n\n");
  157. return 0;
  158. }