multi_test.c 9.2 KB

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