You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

64 lines
2.2 KiB

  1. #define TEST_NAME "sodium_utils"
  2. #include "cmptest.h"
  3. int main(void)
  4. {
  5. unsigned char buf1[1000];
  6. unsigned char buf2[1000];
  7. char buf3[33];
  8. unsigned char buf4[4];
  9. const char *hex;
  10. const char *hex_end;
  11. size_t bin_len;
  12. randombytes_buf(buf1, sizeof buf1);
  13. memcpy(buf2, buf1, sizeof buf2);
  14. printf("%d\n", sodium_memcmp(buf1, buf2, sizeof buf1));
  15. sodium_memzero(buf1, 0U);
  16. printf("%d\n", sodium_memcmp(buf1, buf2, sizeof buf1));
  17. sodium_memzero(buf1, sizeof buf1 / 2);
  18. printf("%d\n", sodium_memcmp(buf1, buf2, sizeof buf1));
  19. printf("%d\n", sodium_memcmp(buf1, buf2, 0U));
  20. sodium_memzero(buf2, sizeof buf2 / 2);
  21. printf("%d\n", sodium_memcmp(buf1, buf2, sizeof buf1));
  22. printf("%s\n",
  23. sodium_bin2hex(buf3, 33U, (const unsigned char *)"0123456789ABCDEF",
  24. 16U));
  25. hex = "Cafe : 6942";
  26. sodium_hex2bin(buf4, sizeof buf4, hex, strlen(hex), ": ", &bin_len, &hex_end);
  27. printf("%lu:%02x%02x%02x%02x\n", (unsigned long)bin_len, buf4[0], buf4[1],
  28. buf4[2], buf4[3]);
  29. printf("dt1: %ld\n", (long) (hex_end - hex));
  30. hex = "Cafe : 6942";
  31. sodium_hex2bin(buf4, sizeof buf4, hex, strlen(hex), ": ", &bin_len, NULL);
  32. printf("%lu:%02x%02x%02x%02x\n", (unsigned long)bin_len, buf4[2], buf4[3],
  33. buf4[2], buf4[3]);
  34. hex = "deadbeef";
  35. if (sodium_hex2bin(buf1, 1U, hex, 8U, NULL, &bin_len, &hex_end) != -1) {
  36. printf("sodium_hex2bin() overflow not detected\n");
  37. }
  38. printf("dt2: %ld\n", (long) (hex_end - hex));
  39. hex = "de:ad:be:eff";
  40. if (sodium_hex2bin(buf1, 4U, hex, 12U, ":", &bin_len, &hex_end) != -1) {
  41. printf("sodium_hex2bin() with an odd input length and a short output buffer\n");
  42. }
  43. printf("dt3: %ld\n", (long) (hex_end - hex));
  44. hex = "de:ad:be:eff";
  45. if (sodium_hex2bin(buf1, sizeof buf1, hex, 12U, ":", &bin_len, &hex_end) != 0) {
  46. printf("sodium_hex2bin() with an odd input length\n");
  47. }
  48. printf("dt4: %ld\n", (long) (hex_end - hex));
  49. hex = "de:ad:be:eff";
  50. if (sodium_hex2bin(buf1, sizeof buf1, hex, 13U, ":", &bin_len, &hex_end) != 0) {
  51. printf("sodium_hex2bin() with an odd input length\n");
  52. }
  53. printf("dt5: %ld\n", (long) (hex_end - hex));
  54. return 0;
  55. }