1
0

CryptoAuth_unit_test.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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/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);
  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, NULL, false, Gcc_FILE);
  80. if (authPassword) {
  81. CryptoAuth_setAuth(String_CONST(authPassword), 1, 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. Message_push(msg, HELLOWORLD, HELLOWORLDLEN, NULL);
  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. "00000000007691d3802a9d04fc400000497a185dabda71739c1f35465fac3448"
  105. "b92a0c36ebff1cf7050383c91e7d56ec2336c09739fa8e91d8dc5bec63e8fad0"
  106. "74bee22a90642a6b4188f374afd90ccc97bb61873b5d8a3b4a6071b60b26a8c7"
  107. "2d6484634df315c4d3ad63de42fe3e4ebfd83bcdab2e1f5f40dc5a08eda4e6c6"
  108. "b7067d3b");
  109. }
  110. static void helloWithAuth()
  111. {
  112. testHello("password",
  113. "0000000001641c99f7719f5780000000497a185dabda71739c1f35465fac3448"
  114. "b92a0c36ebff1cf7050383c91e7d56ec2336c09739fa8e91d8dc5bec63e8fad0"
  115. "74bee22a90642a6b022e089e0550ca84b86884af6a0263fa5fff9ba07583aea4"
  116. "acb000dbe4115623cf335c63981b9645b6c89fbdc3ad757744879751de0f215d"
  117. "2479131d");
  118. }
  119. static void receiveHelloWithNoAuth()
  120. {
  121. struct Allocator* alloc = MallocAllocator_new(1<<20);
  122. struct Context* ctx = setUp(PRIVATEKEY, NULL, NULL, alloc);
  123. struct Message* msg = Message_new(132, 0, alloc);
  124. Assert_true(Hex_decode(msg->bytes, msg->length,
  125. "0000000000ffffffffffffff7fffffffffffffffffffffffffffffffffffffff"
  126. "ffffffffffffffff847c0d2c375234f365e660955187a3735a0f7613d1609d3a"
  127. "6a4d8c53aeaa5a22ea9cf275eee0185edf7f211192f12e8e642a325ed76925fe"
  128. "3c76d313b767a10aca584ca0b979dee990a737da7d68366fa3846d43d541de91"
  129. "29ea3e12", 132*2) > 0);
  130. Assert_true(!CryptoAuth_decrypt(ctx->sess, msg));
  131. Assert_true(msg->length == HELLOWORLDLEN);
  132. Assert_true(Bits_memcmp(HELLOWORLD, msg->bytes, HELLOWORLDLEN) == 0);
  133. Allocator_free(alloc);
  134. //printf("bytes=%s length=%u\n", finalOut->bytes, finalOut->length);
  135. }
  136. static void repeatHello()
  137. {
  138. uint8_t* expectedOutput =
  139. "0000000101641c99f7719f5780000000a693a9fd3f0e27e81ab1100b57b37259"
  140. "4c2adca8671f1fdd050383c91e7d56ec2336c09739fa8e91d8dc5bec63e8fad0"
  141. "74bee22a90642a6ba8555be84c5e35970c5270e8f31f2a5978e0fbdee4542882"
  142. "97568f25a3fc2801aa707d954c78eccb970bcc8cb26867e9dbf0c9d6ef1b3f27"
  143. "24e7e550";
  144. struct Allocator* alloc = MallocAllocator_new(1<<20);
  145. struct Context* ctx = setUp(NULL, HERPUBKEY, "password", alloc);
  146. struct Message* msg = Message_new(0, CryptoHeader_SIZE + HELLOWORLDLEN, alloc);
  147. Message_push(msg, HELLOWORLD, HELLOWORLDLEN, NULL);
  148. Assert_true(!CryptoAuth_encrypt(ctx->sess, msg));
  149. Message_reset(msg);
  150. Message_push(msg, HELLOWORLD, HELLOWORLDLEN, NULL);
  151. Assert_true(!CryptoAuth_encrypt(ctx->sess, msg));
  152. char* actual = Hex_print(msg->bytes, msg->length, alloc);
  153. if (CString_strcmp(actual, expectedOutput)) {
  154. Assert_failure("Test failed.\n"
  155. "Expected %s\n"
  156. " Got %s\n", expectedOutput, actual);
  157. }
  158. Allocator_free(alloc);
  159. }
  160. static void testGetUsers()
  161. {
  162. struct Allocator* allocator = MallocAllocator_new(1<<20);
  163. struct EventBase* base = EventBase_new(allocator);
  164. struct CryptoAuth* ca =
  165. CryptoAuth_new(allocator, NULL, base, NULL, evilRandom(allocator, NULL));
  166. List* users = NULL;
  167. users = CryptoAuth_getUsers(ca, allocator);
  168. Assert_true(List_size(users) == 0);
  169. CryptoAuth_addUser(String_CONST("pass1"), 1, String_CONST("user1"), ca);
  170. users = CryptoAuth_getUsers(ca, allocator);
  171. Assert_true(List_size(users) == 1);
  172. Assert_true(String_equals(String_CONST("user1"),List_getString(users,0)));
  173. CryptoAuth_addUser(String_CONST("pass2"), 1, String_CONST("user2"), ca);
  174. users = CryptoAuth_getUsers(ca, allocator);
  175. Assert_true(List_size(users) == 2);
  176. Assert_true(String_equals(String_CONST("user2"),List_getString(users,0)));
  177. Assert_true(String_equals(String_CONST("user1"),List_getString(users,1)));
  178. Allocator_free(allocator);
  179. }
  180. int main()
  181. {
  182. testGetUsers();
  183. helloNoAuth();
  184. helloWithAuth();
  185. receiveHelloWithNoAuth();
  186. encryptRndNonceTest();
  187. repeatHello();
  188. return 0;
  189. }