auth.c 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #define TEST_NAME "auth"
  2. #include "cmptest.h"
  3. /* "Test Case 2" from RFC 4231 */
  4. static unsigned char key[32] = "Jefe";
  5. static unsigned char c[] = "what do ya want for nothing?";
  6. /* Hacker manifesto */
  7. static unsigned char key2[] =
  8. "Another one got caught today, it's all over the papers. \"Teenager "
  9. "Arrested in Computer Crime Scandal\", \"Hacker Arrested after Bank "
  10. "Tampering\"... Damn kids. They're all alike.";
  11. static unsigned char a[crypto_auth_BYTES];
  12. static unsigned char a2[crypto_auth_hmacsha512_BYTES];
  13. int
  14. main(void)
  15. {
  16. crypto_auth_hmacsha512_state st;
  17. crypto_auth_hmacsha256_state st256;
  18. size_t i;
  19. assert(crypto_auth_hmacsha512_statebytes() ==
  20. sizeof(crypto_auth_hmacsha512_state));
  21. crypto_auth(a, c, sizeof c - 1U, key);
  22. for (i = 0; i < sizeof a; ++i) {
  23. printf(",0x%02x", (unsigned int) a[i]);
  24. if (i % 8 == 7)
  25. printf("\n");
  26. }
  27. printf("\n");
  28. crypto_auth_hmacsha512_init(&st, key, sizeof key);
  29. crypto_auth_hmacsha512_update(&st, c, 1U);
  30. crypto_auth_hmacsha512_update(&st, c, sizeof c - 2U);
  31. crypto_auth_hmacsha512_final(&st, a2);
  32. for (i = 0; i < sizeof a2; ++i) {
  33. printf(",0x%02x", (unsigned int) a2[i]);
  34. if (i % 8 == 7)
  35. printf("\n");
  36. }
  37. printf("\n");
  38. crypto_auth_hmacsha512_init(&st, key2, sizeof key2);
  39. crypto_auth_hmacsha512_update(&st, c, 1U);
  40. crypto_auth_hmacsha512_update(&st, c, sizeof c - 2U);
  41. crypto_auth_hmacsha512_final(&st, a2);
  42. for (i = 0; i < sizeof a2; ++i) {
  43. printf(",0x%02x", (unsigned int) a2[i]);
  44. if (i % 8 == 7)
  45. printf("\n");
  46. }
  47. memset(a2, 0, sizeof a2);
  48. crypto_auth_hmacsha256_init(&st256, key2, sizeof key2);
  49. crypto_auth_hmacsha256_update(&st256, NULL, 0U);
  50. crypto_auth_hmacsha256_update(&st256, c, 1U);
  51. crypto_auth_hmacsha256_update(&st256, c, sizeof c - 2U);
  52. crypto_auth_hmacsha256_final(&st256, a2);
  53. for (i = 0; i < sizeof a2; ++i) {
  54. printf(",0x%02x", (unsigned int) a2[i]);
  55. if (i % 8 == 7)
  56. printf("\n");
  57. }
  58. assert(crypto_auth_bytes() > 0U);
  59. assert(crypto_auth_keybytes() > 0U);
  60. assert(strcmp(crypto_auth_primitive(), "hmacsha512256") == 0);
  61. assert(crypto_auth_hmacsha256_bytes() > 0U);
  62. assert(crypto_auth_hmacsha256_keybytes() > 0U);
  63. assert(crypto_auth_hmacsha512_bytes() > 0U);
  64. assert(crypto_auth_hmacsha512_keybytes() > 0U);
  65. assert(crypto_auth_hmacsha512256_bytes() == crypto_auth_bytes());
  66. assert(crypto_auth_hmacsha512256_keybytes() == crypto_auth_keybytes());
  67. assert(crypto_auth_hmacsha512256_statebytes() >=
  68. crypto_auth_hmacsha512256_keybytes());
  69. assert(crypto_auth_hmacsha256_statebytes() ==
  70. sizeof(crypto_auth_hmacsha256_state));
  71. assert(crypto_auth_hmacsha512_statebytes() ==
  72. sizeof(crypto_auth_hmacsha512_state));
  73. return 0;
  74. }