I2C toy code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

multi_test.c 9.4 KiB

5 vuotta sitten
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /* LibTomCrypt, modular cryptographic library -- Tom St Denis
  2. *
  3. * LibTomCrypt is a library that provides various cryptographic
  4. * algorithms in a highly modular and flexible manner.
  5. *
  6. * The library is free for all purposes without any express
  7. * guarantee it works.
  8. */
  9. /* test the multi helpers... */
  10. #include <tomcrypt_test.h>
  11. int multi_test(void)
  12. {
  13. unsigned char key[32] = { 0 };
  14. unsigned char buf[2][MAXBLOCKSIZE];
  15. unsigned long len, len2;
  16. /* register algos */
  17. register_hash(&sha256_desc);
  18. register_cipher(&aes_desc);
  19. /* HASH testing */
  20. len = sizeof(buf[0]);
  21. hash_memory(find_hash("sha256"), (unsigned char*)"hello", 5, buf[0], &len);
  22. len2 = sizeof(buf[0]);
  23. hash_memory_multi(find_hash("sha256"), buf[1], &len2, (unsigned char*)"hello", 5, NULL);
  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*)"he", 2UL, "llo", 3UL, NULL, 0);
  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. len2 = sizeof(buf[0]);
  35. hash_memory_multi(find_hash("sha256"), buf[1], &len2, (unsigned char*)"h", 1UL, "e", 1UL, "l", 1UL, "l", 1UL, "o", 1UL, NULL);
  36. if (len != len2 || memcmp(buf[0], buf[1], len)) {
  37. printf("Failed: %d %lu %lu\n", __LINE__, len, len2);
  38. return CRYPT_FAIL_TESTVECTOR;
  39. }
  40. #ifdef LTC_HMAC
  41. len = sizeof(buf[0]);
  42. hmac_memory(find_hash("sha256"), key, 16, (unsigned char*)"hello", 5, buf[0], &len);
  43. len2 = sizeof(buf[0]);
  44. hmac_memory_multi(find_hash("sha256"), key, 16, buf[1], &len2, (unsigned char*)"hello", 5UL, 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*)"he", 2UL, "llo", 3UL, 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. len2 = sizeof(buf[0]);
  56. hmac_memory_multi(find_hash("sha256"), key, 16, buf[1], &len2, (unsigned char*)"h", 1UL, "e", 1UL, "l", 1UL, "l", 1UL, "o", 1UL, NULL);
  57. if (len != len2 || memcmp(buf[0], buf[1], len)) {
  58. printf("Failed: %d %lu %lu\n", __LINE__, len, len2);
  59. return CRYPT_FAIL_TESTVECTOR;
  60. }
  61. #endif
  62. #ifdef LTC_OMAC
  63. len = sizeof(buf[0]);
  64. omac_memory(find_cipher("aes"), key, 16, (unsigned char*)"hello", 5, buf[0], &len);
  65. len2 = sizeof(buf[0]);
  66. omac_memory_multi(find_cipher("aes"), key, 16, buf[1], &len2, (unsigned char*)"hello", 5UL, 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*)"he", 2UL, "llo", 3UL, 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. len2 = sizeof(buf[0]);
  78. omac_memory_multi(find_cipher("aes"), key, 16, buf[1], &len2, (unsigned char*)"h", 1UL, "e", 1UL, "l", 1UL, "l", 1UL, "o", 1UL, NULL);
  79. if (len != len2 || memcmp(buf[0], buf[1], len)) {
  80. printf("Failed: %d %lu %lu\n", __LINE__, len, len2);
  81. return CRYPT_FAIL_TESTVECTOR;
  82. }
  83. #endif
  84. #ifdef LTC_PMAC
  85. len = sizeof(buf[0]);
  86. pmac_memory(find_cipher("aes"), key, 16, (unsigned char*)"hello", 5, buf[0], &len);
  87. len2 = sizeof(buf[0]);
  88. pmac_memory_multi(find_cipher("aes"), key, 16, buf[1], &len2, (unsigned char*)"hello", 5, 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*)"he", 2UL, "llo", 3UL, 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. len2 = sizeof(buf[0]);
  100. pmac_memory_multi(find_cipher("aes"), key, 16, buf[1], &len2, (unsigned char*)"h", 1UL, "e", 1UL, "l", 1UL, "l", 1UL, "o", 1UL, NULL);
  101. if (len != len2 || memcmp(buf[0], buf[1], len)) {
  102. printf("Failed: %d %lu %lu\n", __LINE__, len, len2);
  103. return CRYPT_FAIL_TESTVECTOR;
  104. }
  105. #endif
  106. #ifdef LTC_XCBC
  107. len = sizeof(buf[0]);
  108. xcbc_memory(find_cipher("aes"), key, 16, (unsigned char*)"hello", 5, buf[0], &len);
  109. len2 = sizeof(buf[0]);
  110. xcbc_memory_multi(find_cipher("aes"), key, 16, buf[1], &len2, (unsigned char*)"hello", 5, 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*)"he", 2UL, "llo", 3UL, 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. len2 = sizeof(buf[0]);
  122. xcbc_memory_multi(find_cipher("aes"), key, 16, buf[1], &len2, (unsigned char*)"h", 1UL, "e", 1UL, "l", 1UL, "l", 1UL, "o", 1UL, NULL);
  123. if (len != len2 || memcmp(buf[0], buf[1], len)) {
  124. printf("Failed: %d %lu %lu\n", __LINE__, len, len2);
  125. return CRYPT_FAIL_TESTVECTOR;
  126. }
  127. #endif
  128. #ifdef LTC_F9
  129. len = sizeof(buf[0]);
  130. f9_memory(find_cipher("aes"), key, 16, (unsigned char*)"hello", 5, buf[0], &len);
  131. len2 = sizeof(buf[0]);
  132. f9_memory_multi(find_cipher("aes"), key, 16, buf[1], &len2, (unsigned char*)"hello", 5, 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*)"he", 2UL, "llo", 3UL, 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. len2 = sizeof(buf[0]);
  144. f9_memory_multi(find_cipher("aes"), key, 16, buf[1], &len2, (unsigned char*)"h", 1UL, "e", 1UL, "l", 1UL, "l", 1UL, "o", 1UL, NULL);
  145. if (len != len2 || memcmp(buf[0], buf[1], len)) {
  146. printf("Failed: %d %lu %lu\n", __LINE__, len, len2);
  147. return CRYPT_FAIL_TESTVECTOR;
  148. }
  149. #endif
  150. #ifdef LTC_PELICAN
  151. /* TODO: there is no pelican_memory_multi(..) */
  152. #endif
  153. #ifdef LTC_POLY1305
  154. len = sizeof(buf[0]);
  155. poly1305_memory(key, 32, (unsigned char*)"hello", 5, buf[0], &len);
  156. len2 = sizeof(buf[0]);
  157. poly1305_memory_multi(key, 32, buf[1], &len2, (unsigned char*)"hello", 5, 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*)"he", 2UL, "llo", 3UL, 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. len2 = sizeof(buf[0]);
  169. poly1305_memory_multi(key, 32, buf[1], &len2, (unsigned char*)"h", 1UL, "e", 1UL, "l", 1UL, "l", 1UL, "o", 1UL, NULL);
  170. if (len != len2 || memcmp(buf[0], buf[1], len)) {
  171. printf("Failed: %d %lu %lu\n", __LINE__, len, len2);
  172. return CRYPT_FAIL_TESTVECTOR;
  173. }
  174. #endif
  175. #ifdef LTC_BLAKE2SMAC
  176. len = 32;
  177. blake2smac_memory(key, 16, (unsigned char*)"hello", 5, buf[0], &len);
  178. len2 = 32;
  179. blake2smac_memory_multi(key, 16, buf[1], &len2, (unsigned char*)"hello", 5, 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*)"he", 2UL, "llo", 3UL, 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. len2 = 32;
  191. blake2smac_memory_multi(key, 16, buf[1], &len2, (unsigned char*)"h", 1UL, "e", 1UL, "l", 1UL, "l", 1UL, "o", 1UL, NULL);
  192. if (len != len2 || memcmp(buf[0], buf[1], len)) {
  193. printf("Failed: %d %lu %lu\n", __LINE__, len, len2);
  194. return CRYPT_FAIL_TESTVECTOR;
  195. }
  196. #endif
  197. #ifdef LTC_BLAKE2BMAC
  198. len = 64;
  199. blake2bmac_memory(key, 16, (unsigned char*)"hello", 5, buf[0], &len);
  200. len2 = 64;
  201. blake2bmac_memory_multi(key, 16, buf[1], &len2, (unsigned char*)"hello", 5, 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*)"he", 2UL, "llo", 3UL, 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. len2 = 64;
  213. blake2bmac_memory_multi(key, 16, buf[1], &len2, (unsigned char*)"h", 1UL, "e", 1UL, "l", 1UL, "l", 1UL, "o", 1UL, NULL);
  214. if (len != len2 || memcmp(buf[0], buf[1], len)) {
  215. printf("Failed: %d %lu %lu\n", __LINE__, len, len2);
  216. return CRYPT_FAIL_TESTVECTOR;
  217. }
  218. #endif
  219. return CRYPT_OK;
  220. }
  221. /* ref: $Format:%D$ */
  222. /* git commit: $Format:%H$ */
  223. /* commit time: $Format:%ai$ */