auth.c (5084B)
1 2 #define TEST_NAME "auth" 3 #include "cmptest.h" 4 5 /* "Test Case 2" from RFC 4231 */ 6 static unsigned char key[32] = "Jefe"; 7 static unsigned char c[] = "what do ya want for nothing?"; 8 9 /* Hacker manifesto */ 10 static unsigned char key2[] = 11 "Another one got caught today, it's all over the papers. \"Teenager " 12 "Arrested in Computer Crime Scandal\", \"Hacker Arrested after Bank " 13 "Tampering\"... Damn kids. They're all alike."; 14 15 static unsigned char a[crypto_auth_BYTES]; 16 static unsigned char a2[crypto_auth_hmacsha512_BYTES]; 17 static unsigned char a3[crypto_auth_hmacsha512_BYTES]; 18 19 int 20 main(void) 21 { 22 crypto_auth_hmacsha512_state st; 23 crypto_auth_hmacsha256_state st256; 24 crypto_auth_hmacsha512256_state st512_256; 25 size_t i; 26 27 assert(crypto_auth_hmacsha512_statebytes() == 28 sizeof(crypto_auth_hmacsha512_state)); 29 crypto_auth(a, c, sizeof c - 1U, key); 30 for (i = 0; i < sizeof a; ++i) { 31 printf(",0x%02x", (unsigned int) a[i]); 32 if (i % 8 == 7) 33 printf("\n"); 34 } 35 printf("\n"); 36 37 crypto_auth_hmacsha512_init(&st, key, sizeof key); 38 crypto_auth_hmacsha512_update(&st, c, 1U); 39 crypto_auth_hmacsha512_update(&st, c, sizeof c - 2U); 40 crypto_auth_hmacsha512_final(&st, a2); 41 for (i = 0; i < sizeof a2; ++i) { 42 printf(",0x%02x", (unsigned int) a2[i]); 43 if (i % 8 == 7) 44 printf("\n"); 45 } 46 printf("\n"); 47 48 crypto_auth_hmacsha512_init(&st, key2, sizeof key2); 49 crypto_auth_hmacsha512_update(&st, c, 1U); 50 crypto_auth_hmacsha512_update(&st, c, sizeof c - 2U); 51 crypto_auth_hmacsha512_final(&st, a2); 52 for (i = 0; i < sizeof a2; ++i) { 53 printf(",0x%02x", (unsigned int) a2[i]); 54 if (i % 8 == 7) 55 printf("\n"); 56 } 57 58 memset(a2, 0, sizeof a2); 59 crypto_auth_hmacsha256_init(&st256, key2, sizeof key2); 60 crypto_auth_hmacsha256_update(&st256, guard_page, 0U); 61 crypto_auth_hmacsha256_update(&st256, c, 1U); 62 crypto_auth_hmacsha256_update(&st256, c, sizeof c - 2U); 63 crypto_auth_hmacsha256_final(&st256, a2); 64 for (i = 0; i < sizeof a2; ++i) { 65 printf(",0x%02x", (unsigned int) a2[i]); 66 if (i % 8 == 7) 67 printf("\n"); 68 } 69 70 /* Empty message tests: HMAC-SHA512 */ 71 memset(a2, 0, sizeof a2); 72 crypto_auth_hmacsha512_init(&st, key, sizeof key); 73 crypto_auth_hmacsha512_final(&st, a2); 74 75 memset(a3, 0, sizeof a3); 76 crypto_auth_hmacsha512_init(&st, key, sizeof key); 77 crypto_auth_hmacsha512_update(&st, a2, 0U); 78 crypto_auth_hmacsha512_final(&st, a3); 79 assert(sodium_memcmp(a2, a3, sizeof a2) == 0); 80 81 memset(a3, 0, sizeof a3); 82 crypto_auth_hmacsha512_init(&st, key, sizeof key); 83 crypto_auth_hmacsha512_update(&st, guard_page, 0U); 84 crypto_auth_hmacsha512_final(&st, a3); 85 assert(sodium_memcmp(a2, a3, sizeof a2) == 0); 86 87 /* Empty message tests: HMAC-SHA512-256 */ 88 memset(a2, 0, sizeof a2); 89 crypto_auth_hmacsha512256_init(&st512_256, key, sizeof key); 90 crypto_auth_hmacsha512256_final(&st512_256, a2); 91 92 memset(a3, 0, sizeof a3); 93 crypto_auth_hmacsha512256_init(&st512_256, key, sizeof key); 94 crypto_auth_hmacsha512256_update(&st512_256, a2, 0U); 95 crypto_auth_hmacsha512256_final(&st512_256, a3); 96 assert(sodium_memcmp(a2, a3, sizeof a2) == 0); 97 98 memset(a3, 0, sizeof a3); 99 crypto_auth_hmacsha512256_init(&st512_256, key, sizeof key); 100 crypto_auth_hmacsha512256_update(&st512_256, guard_page, 0U); 101 crypto_auth_hmacsha512256_final(&st512_256, a3); 102 assert(sodium_memcmp(a2, a3, sizeof a2) == 0); 103 104 /* Empty message tests: HMAC-SHA256 */ 105 106 memset(a2, 0, sizeof a2); 107 crypto_auth_hmacsha256_init(&st256, key, sizeof key); 108 crypto_auth_hmacsha256_final(&st256, a2); 109 110 memset(a3, 0, sizeof a3); 111 crypto_auth_hmacsha256_init(&st256, key, sizeof key); 112 crypto_auth_hmacsha256_update(&st256, a2, 0U); 113 crypto_auth_hmacsha256_final(&st256, a3); 114 assert(sodium_memcmp(a2, a3, sizeof a2) == 0); 115 116 memset(a3, 0, sizeof a3); 117 crypto_auth_hmacsha256_init(&st256, key, sizeof key); 118 crypto_auth_hmacsha256_update(&st256, guard_page, 0U); 119 crypto_auth_hmacsha256_final(&st256, a3); 120 assert(sodium_memcmp(a2, a3, sizeof a2) == 0); 121 122 /* --- */ 123 124 assert(crypto_auth_bytes() > 0U); 125 assert(crypto_auth_keybytes() > 0U); 126 assert(strcmp(crypto_auth_primitive(), "hmacsha512256") == 0); 127 assert(crypto_auth_hmacsha256_bytes() > 0U); 128 assert(crypto_auth_hmacsha256_keybytes() > 0U); 129 assert(crypto_auth_hmacsha512_bytes() > 0U); 130 assert(crypto_auth_hmacsha512_keybytes() > 0U); 131 assert(crypto_auth_hmacsha512256_bytes() == crypto_auth_bytes()); 132 assert(crypto_auth_hmacsha512256_keybytes() == crypto_auth_keybytes()); 133 assert(crypto_auth_hmacsha512256_statebytes() >= 134 crypto_auth_hmacsha512256_keybytes()); 135 assert(crypto_auth_hmacsha256_statebytes() == 136 sizeof(crypto_auth_hmacsha256_state)); 137 assert(crypto_auth_hmacsha512_statebytes() == 138 sizeof(crypto_auth_hmacsha512_state)); 139 140 return 0; 141 }