CryptoAuth_unit_test.c 11 KB

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