1
0

CryptoAuth_unit_test.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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 <https://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/FileWriterLog.h"
  28. #include "wire/Error.h"
  29. #include "wire/Message.h"
  30. #include <stdio.h>
  31. #define PRIVATEKEY "0123456789abcdefghijklmnopqrstuv"
  32. #define HERPUBKEY "wxyzabcdefghijklmnopqrstuv987654"
  33. #define HELLOWORLD "Hello World"
  34. #define HELLOWORLDLOWER "hello world"
  35. #define HELLOWORLDLEN 12
  36. static void encryptRndNonceTest()
  37. {
  38. uint8_t buff[44];
  39. Bits_memset(buff, 0, 44);
  40. uint8_t nonce[24];
  41. Bits_memset(nonce, 0, 24);
  42. uint8_t secret[32];
  43. Bits_memset(secret, 0, 32);
  44. struct Message m = { .bytes=&buff[32], .length=HELLOWORLDLEN, .padding=32};
  45. CString_strcpy((char*) m.bytes, HELLOWORLDLOWER);
  46. CryptoAuth_encryptRndNonce(nonce, &m, secret);
  47. uint8_t* expected = (uint8_t*) "1391ac5d03ba9f7099bffbb6e6c69d67ae5bd79391a5b94399b293dc";
  48. uint8_t output[57];
  49. Hex_encode(output, 57, m.bytes, m.length);
  50. printf("\n%s\n%s\n", (char*) expected, (char*) output);
  51. Assert_true(!Bits_memcmp(expected, output, 56));
  52. Assert_true(!CryptoAuth_decryptRndNonce(nonce, &m, secret));
  53. Assert_true(m.length == HELLOWORLDLEN && !Bits_memcmp(m.bytes, HELLOWORLDLOWER, m.length));
  54. }
  55. static struct Random* evilRandom(struct Allocator* alloc, struct Log* logger)
  56. {
  57. struct RandomSeed* evilSeed = DeterminentRandomSeed_new(alloc, NULL);
  58. return Random_newWithSeed(alloc, logger, evilSeed, NULL);
  59. }
  60. struct Context
  61. {
  62. struct Allocator* alloc;
  63. struct CryptoAuth* ca;
  64. struct CryptoAuth_Session* sess;
  65. struct Log* log;
  66. struct EventBase* base;
  67. };
  68. static struct Context* setUp(uint8_t* myPrivateKey,
  69. uint8_t* herPublicKey,
  70. uint8_t* authPassword,
  71. struct Allocator* alloc)
  72. {
  73. struct Context* ctx = Allocator_calloc(alloc, sizeof(struct Context), 1);
  74. struct Log* log = ctx->log = FileWriterLog_new(stdout, alloc);
  75. struct EventBase* base = ctx->base = EventBase_new(alloc);
  76. struct CryptoAuth* ca = ctx->ca =
  77. CryptoAuth_new(alloc, myPrivateKey, base, log, evilRandom(alloc, log));
  78. struct CryptoAuth_Session* sess = ctx->sess =
  79. CryptoAuth_newSession(ca, alloc, herPublicKey, false, Gcc_FILE);
  80. if (authPassword) {
  81. CryptoAuth_setAuth(String_CONST(authPassword), NULL, sess);
  82. }
  83. return ctx;
  84. }
  85. static void testHello(uint8_t* password, uint8_t* expectedOutput)
  86. {
  87. Assert_true(CString_strlen((char*)expectedOutput) == 264);
  88. struct Allocator* alloc = MallocAllocator_new(1<<20);
  89. struct Context* ctx = setUp(NULL, HERPUBKEY, password, alloc);
  90. struct Message* msg = Message_new(0, CryptoHeader_SIZE + 12, alloc);
  91. Er_assert(Message_epush(msg, HELLOWORLD, HELLOWORLDLEN));
  92. Assert_true(!CryptoAuth_encrypt(ctx->sess, msg));
  93. char* actual = Hex_print(msg->bytes, msg->length, alloc);
  94. if (CString_strcmp(actual, expectedOutput)) {
  95. Assert_failure("Test failed.\n"
  96. "Expected %s\n"
  97. " Got %s\n", expectedOutput, actual);
  98. }
  99. Allocator_free(alloc);
  100. }
  101. static void helloNoAuth()
  102. {
  103. testHello(NULL,
  104. "00000000007691d3802a9d047c400000497a185dabda71739c1f35465fac3448"
  105. "b92a0c36ebff1cf7050383c91e7d56ec2336c09739fa8e91d8dc5bec63e8fad0"
  106. "74bee22a90642a6b4188f374afd90ccc97bb61873b5d8a3b4a6071b60b26a8c7"
  107. "2d6484634df315c4d3ad63de42fe3e4ebfd83bcdab2e1f5f40dc5a08eda4e6c6"
  108. "b7067d3b");
  109. }
  110. static void helloWithAuth()
  111. {
  112. testHello("password",
  113. "0000000001641c99f7719f5700000000497a185dabda71739c1f35465fac3448"
  114. "b92a0c36ebff1cf7050383c91e7d56ec2336c09739fa8e91d8dc5bec63e8fad0"
  115. "74bee22a90642a6b022e089e0550ca84b86884af6a0263fa5fff9ba07583aea4"
  116. "acb000dbe4115623cf335c63981b9645b6c89fbdc3ad757744879751de0f215d"
  117. "2479131d");
  118. }
  119. static void receiveHelloWithNoAuth()
  120. {
  121. uint8_t herPublic[32];
  122. Assert_true(Hex_decode(herPublic, 32,
  123. "847c0d2c375234f365e660955187a3735a0f7613d1609d3a6a4d8c53aeaa5a22", 64) > 0);
  124. struct Allocator* alloc = MallocAllocator_new(1<<20);
  125. struct Context* ctx = setUp(PRIVATEKEY, herPublic, NULL, alloc);
  126. struct Message* msg = Message_new(132, 0, alloc);
  127. Assert_true(Hex_decode(msg->bytes, msg->length,
  128. "0000000000ffffffffffffff7fffffffffffffffffffffffffffffffffffffff"
  129. "ffffffffffffffff847c0d2c375234f365e660955187a3735a0f7613d1609d3a"
  130. "6a4d8c53aeaa5a22ea9cf275eee0185edf7f211192f12e8e642a325ed76925fe"
  131. "3c76d313b767a10aca584ca0b979dee990a737da7d68366fa3846d43d541de91"
  132. "29ea3e12", 132*2) > 0);
  133. Assert_true(!CryptoAuth_decrypt(ctx->sess, msg));
  134. Assert_true(msg->length == HELLOWORLDLEN);
  135. Assert_true(Bits_memcmp(HELLOWORLD, msg->bytes, HELLOWORLDLEN) == 0);
  136. Allocator_free(alloc);
  137. //printf("bytes=%s length=%u\n", finalOut->bytes, finalOut->length);
  138. }
  139. static void repeatHello()
  140. {
  141. uint8_t* expectedOutput =
  142. "0000000101641c99f7719f5700000000a693a9fd3f0e27e81ab1100b57b37259"
  143. "4c2adca8671f1fdd050383c91e7d56ec2336c09739fa8e91d8dc5bec63e8fad0"
  144. "74bee22a90642a6ba8555be84c5e35970c5270e8f31f2a5978e0fbdee4542882"
  145. "97568f25a3fc2801aa707d954c78eccb970bcc8cb26867e9dbf0c9d6ef1b3f27"
  146. "24e7e550";
  147. struct Allocator* alloc = MallocAllocator_new(1<<20);
  148. struct Context* ctx = setUp(NULL, HERPUBKEY, "password", alloc);
  149. struct Message* msg = Message_new(0, CryptoHeader_SIZE + HELLOWORLDLEN, alloc);
  150. Er_assert(Message_epush(msg, HELLOWORLD, HELLOWORLDLEN));
  151. Assert_true(!CryptoAuth_encrypt(ctx->sess, msg));
  152. Message_reset(msg);
  153. Er_assert(Message_epush(msg, HELLOWORLD, HELLOWORLDLEN));
  154. Assert_true(!CryptoAuth_encrypt(ctx->sess, msg));
  155. char* actual = Hex_print(msg->bytes, msg->length, alloc);
  156. if (CString_strcmp(actual, expectedOutput)) {
  157. Assert_failure("Test failed.\n"
  158. "Expected %s\n"
  159. " Got %s\n", expectedOutput, actual);
  160. }
  161. Allocator_free(alloc);
  162. }
  163. static void testGetUsers()
  164. {
  165. struct Allocator* allocator = MallocAllocator_new(1<<20);
  166. struct EventBase* base = EventBase_new(allocator);
  167. struct CryptoAuth* ca =
  168. CryptoAuth_new(allocator, NULL, base, NULL, evilRandom(allocator, NULL));
  169. struct StringList* users = NULL;
  170. users = CryptoAuth_getUsers(ca, allocator);
  171. Assert_true(users->length == 0);
  172. CryptoAuth_addUser(String_CONST("pass1"), String_CONST("user1"), ca);
  173. users = CryptoAuth_getUsers(ca, allocator);
  174. Assert_true(users->length == 1);
  175. Assert_true(String_equals(String_CONST("user1"), StringList_get(users,0)));
  176. CryptoAuth_addUser(String_CONST("pass2"), String_CONST("user2"), ca);
  177. users = CryptoAuth_getUsers(ca, allocator);
  178. Assert_true(users->length == 2);
  179. Assert_true(String_equals(String_CONST("user2"),StringList_get(users,1)));
  180. Assert_true(String_equals(String_CONST("user1"),StringList_get(users,0)));
  181. Allocator_free(allocator);
  182. }
  183. int main()
  184. {
  185. testGetUsers();
  186. helloNoAuth();
  187. helloWithAuth();
  188. receiveHelloWithNoAuth();
  189. encryptRndNonceTest();
  190. repeatHello();
  191. return 0;
  192. }