multi_test.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /* test the multi helpers... */
  2. #include <tomcrypt.h>
  3. int main(void)
  4. {
  5. unsigned char key[32], buf[2][MAXBLOCKSIZE];
  6. unsigned long len, len2;
  7. /* register algos */
  8. register_hash(&sha256_desc);
  9. register_cipher(&aes_desc);
  10. /* HASH testing */
  11. len = sizeof(buf[0]);
  12. hash_memory(find_hash("sha256"), (unsigned char*)"hello", 5, buf[0], &len);
  13. len2 = sizeof(buf[0]);
  14. hash_memory_multi(find_hash("sha256"), buf[1], &len2, (unsigned char*)"hello", 5, NULL);
  15. if (len != len2 || memcmp(buf[0], buf[1], len)) {
  16. printf("Failed: %d %lu %lu\n", __LINE__, len, len2);
  17. return EXIT_FAILURE;
  18. }
  19. len2 = sizeof(buf[0]);
  20. hash_memory_multi(find_hash("sha256"), buf[1], &len2, (unsigned char*)"he", 2UL, "llo", 3UL, NULL, 0);
  21. if (len != len2 || memcmp(buf[0], buf[1], len)) {
  22. printf("Failed: %d %lu %lu\n", __LINE__, len, len2);
  23. return EXIT_FAILURE;
  24. }
  25. len2 = sizeof(buf[0]);
  26. hash_memory_multi(find_hash("sha256"), buf[1], &len2, (unsigned char*)"h", 1UL, "e", 1UL, "l", 1UL, "l", 1UL, "o", 1UL, NULL);
  27. if (len != len2 || memcmp(buf[0], buf[1], len)) {
  28. printf("Failed: %d %lu %lu\n", __LINE__, len, len2);
  29. return EXIT_FAILURE;
  30. }
  31. #ifdef LTC_HMAC
  32. len = sizeof(buf[0]);
  33. hmac_memory(find_hash("sha256"), key, 16, (unsigned char*)"hello", 5, buf[0], &len);
  34. len2 = sizeof(buf[0]);
  35. hmac_memory_multi(find_hash("sha256"), key, 16, buf[1], &len2, (unsigned char*)"hello", 5UL, NULL);
  36. if (len != len2 || memcmp(buf[0], buf[1], len)) {
  37. printf("Failed: %d %lu %lu\n", __LINE__, len, len2);
  38. return EXIT_FAILURE;
  39. }
  40. len2 = sizeof(buf[0]);
  41. hmac_memory_multi(find_hash("sha256"), key, 16, buf[1], &len2, (unsigned char*)"he", 2UL, "llo", 3UL, NULL);
  42. if (len != len2 || memcmp(buf[0], buf[1], len)) {
  43. printf("Failed: %d %lu %lu\n", __LINE__, len, len2);
  44. return EXIT_FAILURE;
  45. }
  46. len2 = sizeof(buf[0]);
  47. hmac_memory_multi(find_hash("sha256"), key, 16, buf[1], &len2, (unsigned char*)"h", 1UL, "e", 1UL, "l", 1UL, "l", 1UL, "o", 1UL, NULL);
  48. if (len != len2 || memcmp(buf[0], buf[1], len)) {
  49. printf("Failed: %d %lu %lu\n", __LINE__, len, len2);
  50. return EXIT_FAILURE;
  51. }
  52. #endif
  53. #ifdef LTC_OMAC
  54. len = sizeof(buf[0]);
  55. omac_memory(find_cipher("aes"), key, 16, (unsigned char*)"hello", 5, buf[0], &len);
  56. len2 = sizeof(buf[0]);
  57. omac_memory_multi(find_cipher("aes"), key, 16, buf[1], &len2, (unsigned char*)"hello", 5UL, NULL);
  58. if (len != len2 || memcmp(buf[0], buf[1], len)) {
  59. printf("Failed: %d %lu %lu\n", __LINE__, len, len2);
  60. return EXIT_FAILURE;
  61. }
  62. len2 = sizeof(buf[0]);
  63. omac_memory_multi(find_cipher("aes"), key, 16, buf[1], &len2, (unsigned char*)"he", 2UL, "llo", 3UL, NULL);
  64. if (len != len2 || memcmp(buf[0], buf[1], len)) {
  65. printf("Failed: %d %lu %lu\n", __LINE__, len, len2);
  66. return EXIT_FAILURE;
  67. }
  68. len2 = sizeof(buf[0]);
  69. omac_memory_multi(find_cipher("aes"), key, 16, buf[1], &len2, (unsigned char*)"h", 1UL, "e", 1UL, "l", 1UL, "l", 1UL, "o", 1UL, NULL);
  70. if (len != len2 || memcmp(buf[0], buf[1], len)) {
  71. printf("Failed: %d %lu %lu\n", __LINE__, len, len2);
  72. return EXIT_FAILURE;
  73. }
  74. #endif
  75. #ifdef LTC_PMAC
  76. len = sizeof(buf[0]);
  77. pmac_memory(find_cipher("aes"), key, 16, (unsigned char*)"hello", 5, buf[0], &len);
  78. len2 = sizeof(buf[0]);
  79. pmac_memory_multi(find_cipher("aes"), key, 16, buf[1], &len2, (unsigned char*)"hello", 5, NULL);
  80. if (len != len2 || memcmp(buf[0], buf[1], len)) {
  81. printf("Failed: %d %lu %lu\n", __LINE__, len, len2);
  82. return EXIT_FAILURE;
  83. }
  84. len2 = sizeof(buf[0]);
  85. pmac_memory_multi(find_cipher("aes"), key, 16, buf[1], &len2, (unsigned char*)"he", 2UL, "llo", 3UL, NULL);
  86. if (len != len2 || memcmp(buf[0], buf[1], len)) {
  87. printf("Failed: %d %lu %lu\n", __LINE__, len, len2);
  88. return EXIT_FAILURE;
  89. }
  90. len2 = sizeof(buf[0]);
  91. pmac_memory_multi(find_cipher("aes"), key, 16, buf[1], &len2, (unsigned char*)"h", 1UL, "e", 1UL, "l", 1UL, "l", 1UL, "o", 1UL, NULL);
  92. if (len != len2 || memcmp(buf[0], buf[1], len)) {
  93. printf("Failed: %d %lu %lu\n", __LINE__, len, len2);
  94. return EXIT_FAILURE;
  95. }
  96. #endif
  97. #ifdef LTC_XCBC
  98. len = sizeof(buf[0]);
  99. xcbc_memory(find_cipher("aes"), key, 16, (unsigned char*)"hello", 5, buf[0], &len);
  100. len2 = sizeof(buf[0]);
  101. xcbc_memory_multi(find_cipher("aes"), key, 16, buf[1], &len2, (unsigned char*)"hello", 5, NULL);
  102. if (len != len2 || memcmp(buf[0], buf[1], len)) {
  103. printf("Failed: %d %lu %lu\n", __LINE__, len, len2);
  104. return EXIT_FAILURE;
  105. }
  106. len2 = sizeof(buf[0]);
  107. xcbc_memory_multi(find_cipher("aes"), key, 16, buf[1], &len2, (unsigned char*)"he", 2UL, "llo", 3UL, NULL);
  108. if (len != len2 || memcmp(buf[0], buf[1], len)) {
  109. printf("Failed: %d %lu %lu\n", __LINE__, len, len2);
  110. return EXIT_FAILURE;
  111. }
  112. len2 = sizeof(buf[0]);
  113. xcbc_memory_multi(find_cipher("aes"), key, 16, buf[1], &len2, (unsigned char*)"h", 1UL, "e", 1UL, "l", 1UL, "l", 1UL, "o", 1UL, NULL);
  114. if (len != len2 || memcmp(buf[0], buf[1], len)) {
  115. printf("Failed: %d %lu %lu\n", __LINE__, len, len2);
  116. return EXIT_FAILURE;
  117. }
  118. #endif
  119. #ifdef LTC_F9
  120. len = sizeof(buf[0]);
  121. f9_memory(find_cipher("aes"), key, 16, (unsigned char*)"hello", 5, buf[0], &len);
  122. len2 = sizeof(buf[0]);
  123. f9_memory_multi(find_cipher("aes"), key, 16, buf[1], &len2, (unsigned char*)"hello", 5, NULL);
  124. if (len != len2 || memcmp(buf[0], buf[1], len)) {
  125. printf("Failed: %d %lu %lu\n", __LINE__, len, len2);
  126. return EXIT_FAILURE;
  127. }
  128. len2 = sizeof(buf[0]);
  129. f9_memory_multi(find_cipher("aes"), key, 16, buf[1], &len2, (unsigned char*)"he", 2UL, "llo", 3UL, NULL);
  130. if (len != len2 || memcmp(buf[0], buf[1], len)) {
  131. printf("Failed: %d %lu %lu\n", __LINE__, len, len2);
  132. return EXIT_FAILURE;
  133. }
  134. len2 = sizeof(buf[0]);
  135. f9_memory_multi(find_cipher("aes"), key, 16, buf[1], &len2, (unsigned char*)"h", 1UL, "e", 1UL, "l", 1UL, "l", 1UL, "o", 1UL, NULL);
  136. if (len != len2 || memcmp(buf[0], buf[1], len)) {
  137. printf("Failed: %d %lu %lu\n", __LINE__, len, len2);
  138. return EXIT_FAILURE;
  139. }
  140. #endif
  141. #ifdef LTC_PELICAN
  142. /* TODO: there is no pelican_memory_multi(..) */
  143. #endif
  144. #ifdef LTC_POLY1305
  145. len = sizeof(buf[0]);
  146. poly1305_memory(key, 32, (unsigned char*)"hello", 5, buf[0], &len);
  147. len2 = sizeof(buf[0]);
  148. poly1305_memory_multi(key, 32, buf[1], &len2, (unsigned char*)"hello", 5, NULL);
  149. if (len != len2 || memcmp(buf[0], buf[1], len)) {
  150. printf("Failed: %d %lu %lu\n", __LINE__, len, len2);
  151. return EXIT_FAILURE;
  152. }
  153. len2 = sizeof(buf[0]);
  154. poly1305_memory_multi(key, 32, buf[1], &len2, (unsigned char*)"he", 2UL, "llo", 3UL, NULL);
  155. if (len != len2 || memcmp(buf[0], buf[1], len)) {
  156. printf("Failed: %d %lu %lu\n", __LINE__, len, len2);
  157. return EXIT_FAILURE;
  158. }
  159. len2 = sizeof(buf[0]);
  160. poly1305_memory_multi(key, 32, buf[1], &len2, (unsigned char*)"h", 1UL, "e", 1UL, "l", 1UL, "l", 1UL, "o", 1UL, NULL);
  161. if (len != len2 || memcmp(buf[0], buf[1], len)) {
  162. printf("Failed: %d %lu %lu\n", __LINE__, len, len2);
  163. return EXIT_FAILURE;
  164. }
  165. #endif
  166. #ifdef LTC_BLAKE2SMAC
  167. len = 32;
  168. blake2smac_memory(key, 16, (unsigned char*)"hello", 5, buf[0], &len);
  169. len2 = 32;
  170. blake2smac_memory_multi(key, 16, buf[1], &len2, (unsigned char*)"hello", 5, NULL);
  171. if (len != len2 || memcmp(buf[0], buf[1], len)) {
  172. printf("Failed: %d %lu %lu\n", __LINE__, len, len2);
  173. return EXIT_FAILURE;
  174. }
  175. len2 = 32;
  176. blake2smac_memory_multi(key, 16, buf[1], &len2, (unsigned char*)"he", 2UL, "llo", 3UL, NULL);
  177. if (len != len2 || memcmp(buf[0], buf[1], len)) {
  178. printf("Failed: %d %lu %lu\n", __LINE__, len, len2);
  179. return EXIT_FAILURE;
  180. }
  181. len2 = 32;
  182. blake2smac_memory_multi(key, 16, buf[1], &len2, (unsigned char*)"h", 1UL, "e", 1UL, "l", 1UL, "l", 1UL, "o", 1UL, NULL);
  183. if (len != len2 || memcmp(buf[0], buf[1], len)) {
  184. printf("Failed: %d %lu %lu\n", __LINE__, len, len2);
  185. return EXIT_FAILURE;
  186. }
  187. #endif
  188. #ifdef LTC_BLAKE2BMAC
  189. len = 64;
  190. blake2bmac_memory(key, 16, (unsigned char*)"hello", 5, buf[0], &len);
  191. len2 = 64;
  192. blake2bmac_memory_multi(key, 16, buf[1], &len2, (unsigned char*)"hello", 5, NULL);
  193. if (len != len2 || memcmp(buf[0], buf[1], len)) {
  194. printf("Failed: %d %lu %lu\n", __LINE__, len, len2);
  195. return EXIT_FAILURE;
  196. }
  197. len2 = 64;
  198. blake2bmac_memory_multi(key, 16, buf[1], &len2, (unsigned char*)"he", 2UL, "llo", 3UL, NULL);
  199. if (len != len2 || memcmp(buf[0], buf[1], len)) {
  200. printf("Failed: %d %lu %lu\n", __LINE__, len, len2);
  201. return EXIT_FAILURE;
  202. }
  203. len2 = 64;
  204. blake2bmac_memory_multi(key, 16, buf[1], &len2, (unsigned char*)"h", 1UL, "e", 1UL, "l", 1UL, "l", 1UL, "o", 1UL, NULL);
  205. if (len != len2 || memcmp(buf[0], buf[1], len)) {
  206. printf("Failed: %d %lu %lu\n", __LINE__, len, len2);
  207. return EXIT_FAILURE;
  208. }
  209. #endif
  210. printf("All passed\n");
  211. return EXIT_SUCCESS;
  212. }
  213. /* $Source$ */
  214. /* $Revision$ */
  215. /* $Date$ */