CryptoAuth_randnonce_test.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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 "crypto/CryptoAuth_pvt.h"
  16. #include "memory/Allocator.h"
  17. #include "util/Hex.h"
  18. #include "util/Bits.h"
  19. #include <stdio.h>
  20. #define HELLOWORLDLOWER "hello world"
  21. #define HELLOWORLDLEN 12
  22. static void encryptRndNonceTest()
  23. {
  24. uint8_t nonce[24];
  25. Bits_memset(nonce, 0, 24);
  26. uint8_t secret[32];
  27. Bits_memset(secret, 0, 32);
  28. Allocator_t* alloc = Allocator_new(1<<14);
  29. Message_t* m = Message_new(0, 512, alloc);
  30. Err_assert(Message_epush(m, NULL, 44));
  31. Err_assert(Message_epop(m, NULL, 44));
  32. Err_assert(Message_epush(m, HELLOWORLDLOWER, CString_strlen(HELLOWORLDLOWER)+1));
  33. CryptoAuth_encryptRndNonce(nonce, m, secret);
  34. uint8_t* expected = (uint8_t*) "1391ac5d03ba9f7099bffbb6e6c69d67ae5bd79391a5b94399b293dc";
  35. uint8_t output[57];
  36. Hex_encode(output, 57, Message_bytes(m), Message_getLength(m));
  37. printf("\n%s\n%s\n", (char*) expected, (char*) output);
  38. Assert_true(!Bits_memcmp(expected, output, 56));
  39. Assert_true(!CryptoAuth_decryptRndNonce(nonce, m, secret));
  40. Assert_true(Message_getLength(m) == HELLOWORLDLEN
  41. && !Bits_memcmp(Message_bytes(m), HELLOWORLDLOWER, Message_getLength(m)));
  42. Allocator_free(alloc);
  43. }
  44. int main()
  45. {
  46. encryptRndNonceTest();
  47. return 0;
  48. }