8 unsigned char secret[] = {
9 0xb4, 0x62, 0xf2, 0x60, 0x87, 0x78, 0x16, 0x87, 0xde, 0xce,
10 0x80, 0x09, 0x24, 0x0b, 0x93, 0xfc, 0xa0, 0xfc, 0x56, 0x56
13 static struct _auth_chunk
14 conjure_key(const unsigned char *challenge, const int challengesize)
16 struct _auth_chunk ho = {0};
18 int keysize = sizeof(ho.data);
20 if ((rc = hmac(secret, sizeof(secret), challenge, challengesize,
21 &ho.data, &keysize))) {
22 ho.err = crypto_errstr(rc);
23 } else if (keysize != sizeof(ho.data)) {
24 ho.err = "make_key: hash size is wrong";
29 static struct _auth_chunk
30 token_key(const unsigned char *challenge, const int challengesize)
32 struct _auth_chunk ho = {0};
34 int keysize = sizeof(ho.data);
36 if ((rc = pcsc_cr(challenge, challengesize, ho.data, &keysize))) {
37 ho.err = pcsc_errstr(rc);
42 int main(int argc, char *argv[])
44 const char *id = "testuser";
45 const char *pass = "testpassword";
46 const char *nonce = "1";
47 const unsigned char *payload = (unsigned char *)
48 "To authorize or not to authorize?";
52 struct _auth_chunk (*fetch_key)(const unsigned char *challenge,
53 const int challengesize);
55 if (argc == 2 && strlen(argv[1]) == 40 &&
56 strspn(argv[1], "0123456789abcdefABCDEF") == 40) {
57 for (i = 0; i < sizeof(secret); i++)
58 sscanf(&argv[1][i*2], "%2hhx", &secret[i]);
59 fetch_key = token_key;
61 fetch_key = conjure_key;
64 ao = authobj(id, pass, NULL, nonce, secret, sizeof(secret),
65 payload, strlen((char *)payload),
67 printf("new_authobj err=%s\n", ao.err?ao.err:"<no error>");
68 printf("data(%d):", ao.datasize);
69 for (i = 0; i < ao.datasize; i++) printf(" %02x", ao.data[i]);
70 printf("\npayload(%d): \"%.*s\"\n", ao.paylsize, ao.paylsize,
71 ao.payload?(char*)ao.payload:"");
73 if (ao.buffer) free(ao.buffer);
77 nao = authobj(id, pass, nonce, nonce, NULL, 0, NULL, 0,
78 ao.data, ao.datasize, fetch_key);
79 printf("verify_authobj err=%s\n", nao.err?nao.err:"<no error>");
80 printf("data(%d):", nao.datasize);
81 for (i = 0; i < nao.datasize; i++) printf(" %02x", nao.data[i]);
82 printf("\npayload(%d): \"%.*s\"\n", nao.paylsize, nao.paylsize,
83 nao.payload?(char*)nao.payload:"");
85 if (nao.buffer) free(nao.buffer);
88 if (ao.paylsize != nao.paylsize ||
89 memcmp(ao.payload, nao.payload, ao.paylsize)) {
90 printf("payload does not match");
94 if (ao.buffer) free(ao.buffer);
95 if (nao.buffer) free(nao.buffer);