CryptoAuth_unit_test.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #define string_strcpy
  16. #define string_strlen
  17. #include "benc/List.h"
  18. #include "benc/String.h"
  19. #include "crypto/CryptoAuth_pvt.h"
  20. #include "crypto/random/Random.h"
  21. #include "crypto/random/test/DeterminentRandomSeed.h"
  22. #include "io/FileWriter.h"
  23. #include "memory/MallocAllocator.h"
  24. #include "memory/Allocator.h"
  25. #include "util/platform/libc/string.h"
  26. #include "util/events/EventBase.h"
  27. #include "util/Assert.h"
  28. #include "util/Bits.h"
  29. #include "util/Hex.h"
  30. #include "util/log/WriterLog.h"
  31. #include "wire/Error.h"
  32. #include "wire/Message.h"
  33. #include <stdio.h>
  34. #define BUFFER_SIZE 8192*4
  35. static uint8_t* privateKey = (uint8_t*) "0123456789abcdefghijklmnopqrstuv";
  36. static uint8_t* publicKey = (uint8_t*)
  37. "\x3f\x5b\x96\x62\x11\x11\xd8\x9c\x7d\x3f\x51\x71\x68\x78\xfa\xb4"
  38. "\xc3\xcf\xd9\x7e\x32\x04\x12\xb4\xaf\x7e\x22\x92\xa5\xdf\x31\x71";
  39. static struct EventBase* eventBase;
  40. static uint8_t* hello = (uint8_t*) "Hello World";
  41. static void encryptRndNonceTest()
  42. {
  43. uint8_t buff[44];
  44. Bits_memset(buff, 0, 44);
  45. uint8_t nonce[24];
  46. Bits_memset(nonce, 0, 24);
  47. uint8_t secret[32];
  48. Bits_memset(secret, 0, 32);
  49. struct Message m = { .bytes=&buff[32], .length=12, .padding=32};
  50. strcpy((char*) m.bytes, "hello world");
  51. CryptoAuth_encryptRndNonce(nonce, &m, secret);
  52. uint8_t* expected = (uint8_t*) "1391ac5d03ba9f7099bffbb6e6c69d67ae5bd79391a5b94399b293dc";
  53. uint8_t output[57];
  54. Hex_encode(output, 57, m.bytes, m.length);
  55. //printf("\n%s\n%s\n", (char*) expected, (char*) output);
  56. Assert_always(!Bits_memcmp(expected, output, 56));
  57. Assert_always(!CryptoAuth_decryptRndNonce(nonce, &m, secret));
  58. Assert_always(m.length == 12 && !Bits_memcmp(m.bytes, "hello world", m.length));
  59. }
  60. static struct Random* evilRandom(struct Allocator* alloc, struct Log* logger)
  61. {
  62. struct RandomSeed* evilSeed = DeterminentRandomSeed_new(alloc);
  63. return Random_newWithSeed(alloc, logger, evilSeed, NULL);
  64. }
  65. static void createNew()
  66. {
  67. struct Allocator* allocator = MallocAllocator_new(BUFFER_SIZE);
  68. struct CryptoAuth* ca =
  69. CryptoAuth_new(allocator, privateKey, eventBase, NULL, evilRandom(allocator, NULL));
  70. /*for (int i = 0; i < 32; i++) {
  71. printf("%.2x", ca->publicKey[i]);
  72. }*/
  73. Assert_always(Bits_memcmp(ca->publicKey, publicKey, 32) == 0);
  74. Allocator_free(allocator);
  75. }
  76. static uint8_t receiveMessage(struct Message* message, struct Interface* iface)
  77. {
  78. *((struct Message**)iface->receiverContext) = message;
  79. return Error_NONE;
  80. }
  81. static uint8_t sendMessage(struct Message* message, struct Interface* iface)
  82. {
  83. *((struct Message**)iface->senderContext) = message;
  84. return Error_NONE;
  85. }
  86. static struct CryptoAuth_Wrapper* setUp(uint8_t* myPrivateKey,
  87. uint8_t* herPublicKey,
  88. uint8_t* authPassword,
  89. struct Message** resultMessage)
  90. {
  91. struct Allocator* allocator = MallocAllocator_new(8192*2);
  92. struct Writer* writer = FileWriter_new(stdout, allocator);
  93. struct Log* logger = WriterLog_new(writer, allocator);
  94. struct CryptoAuth* ca =
  95. CryptoAuth_new(allocator, myPrivateKey, eventBase, logger, evilRandom(allocator, logger));
  96. struct Interface* iface = Allocator_clone(allocator, (&(struct Interface) {
  97. .sendMessage = sendMessage,
  98. .senderContext = resultMessage
  99. }));
  100. struct CryptoAuth_Wrapper* wrapper = Allocator_clone(allocator, (&(struct CryptoAuth_Wrapper) {
  101. .context = (struct CryptoAuth_pvt*) ca,
  102. .wrappedInterface = iface
  103. }));
  104. #ifdef Identity_CHECK
  105. wrapper->Identity_verifier = ((struct CryptoAuth_pvt*)ca)->Identity_verifier;
  106. #endif
  107. if (authPassword) {
  108. struct Interface temp = {
  109. .senderContext = wrapper,
  110. .allocator = allocator
  111. };
  112. String str = { .bytes = (char*) authPassword, .len = strlen((char*)authPassword) };
  113. CryptoAuth_setAuth(&str, 1, &temp);
  114. }
  115. if (herPublicKey) {
  116. Bits_memcpyConst(wrapper->herPerminentPubKey, herPublicKey, 32);
  117. }
  118. return wrapper;
  119. }
  120. static void testHello(uint8_t* password, uint8_t* expectedOutput)
  121. {
  122. Assert_always(strlen((char*)expectedOutput) == 264);
  123. struct Message* outMessage;
  124. struct CryptoAuth_Wrapper* wrapper =
  125. setUp(NULL, (uint8_t*) "wxyzabcdefghijklmnopqrstuv987654", password, &outMessage);
  126. uint8_t msgBuff[Headers_CryptoAuth_SIZE + 12];
  127. struct Message msg = {
  128. .length = 12,
  129. .padding = Headers_CryptoAuth_SIZE,
  130. .bytes = msgBuff + Headers_CryptoAuth_SIZE
  131. };
  132. Bits_memcpyConst(msg.bytes, hello, 12);
  133. CryptoAuth_encryptHandshake(&msg, wrapper, 0);
  134. uint8_t actual[265];
  135. Assert_always(Hex_encode(actual, 265, outMessage->bytes, outMessage->length) > 0);
  136. //printf("%s", actual);
  137. if (Bits_memcmp(actual, expectedOutput, 264)) {
  138. Assert_failure("Test failed.\n"
  139. "Expected %s\n"
  140. " Got %s\n", expectedOutput, actual);
  141. }
  142. }
  143. static void helloNoAuth()
  144. {
  145. uint8_t* expected = (uint8_t*)
  146. "00000000007691d3802a9d04fc403525497a185dabda71739c1f35465fac3448"
  147. "b92a0c36ebff1cf7050383c91e7d56ec2336c09739fa8e91d8dc5bec63e8fad0"
  148. "74bee22a90642a6b4188f374afd90ccc97bb61873b5d8a3b4a6071b60b26a8c7"
  149. "2d6484634df315c4d3ad63de42fe3e4ebfd83bcdab2e1f5f40dc5a08eda4e6c6"
  150. "b7067d3b";
  151. testHello(NULL, expected);
  152. }
  153. static void helloWithAuth()
  154. {
  155. uint8_t* expected = (uint8_t*)
  156. "0000000001641c99f7719f5780003eb1497a185dabda71739c1f35465fac3448"
  157. "b92a0c36ebff1cf7050383c91e7d56ec2336c09739fa8e91d8dc5bec63e8fad0"
  158. "74bee22a90642a6b022e089e0550ca84b86884af6a0263fa5fff9ba07583aea4"
  159. "acb000dbe4115623cf335c63981b9645b6c89fbdc3ad757744879751de0f215d"
  160. "2479131d";
  161. testHello((uint8_t*)"password", expected);
  162. }
  163. static void receiveHelloWithNoAuth()
  164. {
  165. uint8_t* messageHex = (uint8_t*)
  166. "0000000000ffffffffffffff7fffffffffffffffffffffffffffffffffffffff"
  167. "ffffffffffffffff847c0d2c375234f365e660955187a3735a0f7613d1609d3a"
  168. "6a4d8c53aeaa5a22ea9cf275eee0185edf7f211192f12e8e642a325ed76925fe"
  169. "3c76d313b767a10aca584ca0b979dee990a737da7d68366fa3846d43d541de91"
  170. "29ea3e12";
  171. uint8_t message[132];
  172. Assert_always(Hex_decode(message, 132, messageHex, strlen((char*)messageHex)) > 0);
  173. struct Message incoming = {
  174. .length = 132,
  175. .padding = 0,
  176. .bytes = message
  177. };
  178. struct CryptoAuth_Wrapper* wrapper = setUp(privateKey, NULL, NULL, NULL);
  179. struct Message* finalOut = NULL;
  180. wrapper->externalInterface.receiveMessage = receiveMessage;
  181. wrapper->externalInterface.receiverContext = &finalOut;
  182. CryptoAuth_receiveMessage(&incoming, &(struct Interface) { .receiverContext = wrapper } );
  183. Assert_always(finalOut);
  184. Assert_always(finalOut->length == 12);
  185. Assert_always(Bits_memcmp(hello, finalOut->bytes, 12) == 0);
  186. //printf("bytes=%s length=%u\n", finalOut->bytes, finalOut->length);
  187. }
  188. static void repeatHello()
  189. {
  190. struct Allocator* allocator = MallocAllocator_new(1<<20);
  191. struct Writer* logwriter = FileWriter_new(stdout, allocator);
  192. struct Log* logger = WriterLog_new(logwriter, allocator);
  193. struct CryptoAuth* ca =
  194. CryptoAuth_new(allocator, NULL, eventBase, logger, evilRandom(allocator, logger));
  195. struct Message* out = NULL;
  196. struct Interface iface = {
  197. .sendMessage = sendMessage,
  198. .senderContext = &out
  199. };
  200. struct CryptoAuth_Wrapper wrapper = {
  201. .context = (struct CryptoAuth_pvt*) ca,
  202. .wrappedInterface = &iface
  203. };
  204. Bits_memcpyConst(wrapper.herPerminentPubKey, publicKey, 32);
  205. uint8_t* hello = (uint8_t*) "Hello World";
  206. uint8_t msgBuff[Headers_CryptoAuth_SIZE + 12];
  207. struct Message msg = {
  208. .length = 12,
  209. .padding = Headers_CryptoAuth_SIZE,
  210. .bytes = msgBuff + Headers_CryptoAuth_SIZE
  211. };
  212. struct Message msg2;
  213. Bits_memcpyConst(&msg2, &msg, sizeof(struct Message));
  214. Bits_memcpyConst(msg2.bytes, hello, 12);
  215. CryptoAuth_encryptHandshake(&msg, &wrapper, 0);
  216. Bits_memcpyConst(msg2.bytes, hello, 12);
  217. CryptoAuth_encryptHandshake(&msg2, &wrapper, 0);
  218. // Check the nonce
  219. Assert_always(!Bits_memcmp(msg2.bytes, "\0\0\0\1", 4));
  220. ca = CryptoAuth_new(allocator, privateKey, eventBase, logger, evilRandom(allocator, logger));
  221. struct Message* finalOut = NULL;
  222. struct CryptoAuth_Wrapper wrapper2 = {
  223. .context = (struct CryptoAuth_pvt*) ca,
  224. .externalInterface = {
  225. .receiveMessage = receiveMessage,
  226. .receiverContext = &finalOut
  227. },
  228. .wrappedInterface = &iface
  229. };
  230. #ifdef Identity_CHECK
  231. wrapper2.Identity_verifier = ((struct CryptoAuth_pvt*)ca)->Identity_verifier;
  232. #endif
  233. CryptoAuth_receiveMessage(out, &(struct Interface) { .receiverContext = &wrapper2 } );
  234. Assert_always(finalOut);
  235. Assert_always(finalOut->length == 12);
  236. Assert_always(Bits_memcmp(hello, finalOut->bytes, 12) == 0);
  237. //printf("bytes=%s length=%u\n", finalOut->bytes, finalOut->length);
  238. Allocator_free(allocator);
  239. }
  240. static void testGetUsers()
  241. {
  242. struct Allocator* allocator = MallocAllocator_new(1<<20);
  243. struct EventBase* base = EventBase_new(allocator);
  244. struct CryptoAuth* ca =
  245. CryptoAuth_new(allocator, NULL, base, NULL, evilRandom(allocator, NULL));
  246. List* users = NULL;
  247. users = CryptoAuth_getUsers(ca, allocator);
  248. Assert_always(List_size(users) == -1);
  249. CryptoAuth_addUser(String_CONST("pass1"), 1, String_CONST("user1"), ca);
  250. users = CryptoAuth_getUsers(ca, allocator);
  251. Assert_always(List_size(users) == 1);
  252. Assert_always(String_equals(String_CONST("user1"),List_getString(users,0)));
  253. CryptoAuth_addUser(String_CONST("pass2"), 1, String_CONST("user2"), ca);
  254. users = CryptoAuth_getUsers(ca, allocator);
  255. Assert_always(List_size(users) == 2);
  256. Assert_always(String_equals(String_CONST("user2"),List_getString(users,0)));
  257. Assert_always(String_equals(String_CONST("user1"),List_getString(users,1)));
  258. Allocator_free(allocator);
  259. }
  260. int main()
  261. {
  262. testGetUsers();
  263. struct Allocator* allocator = MallocAllocator_new(4096);
  264. eventBase = EventBase_new(allocator);
  265. helloNoAuth();
  266. helloWithAuth();
  267. receiveHelloWithNoAuth();
  268. encryptRndNonceTest();
  269. createNew();
  270. repeatHello();
  271. Allocator_free(allocator);
  272. return 0;
  273. }