change serialization interface
[pam_pcsc_cr.git] / test_auth.c
1 #include <stdio.h>
2 #include <string.h>
3 #include "authobj.h"
4 #include "crypto.h"
5
6 int main(int argc, char *argv[])
7 {
8         const char *id = "testuser";
9         const char *pass = "testpassword";
10         const char *nonce = "1";
11         const unsigned char secret[] = {0xb4, 0x62, 0xf2, 0x60, 0x87,
12                                         0x78, 0x16, 0x87, 0xde, 0xce,
13                                         0x80, 0x09, 0x24, 0x0b, 0x93,
14                                         0xfc, 0xa0, 0xfc, 0x56, 0x56};
15         const unsigned char *payload = (unsigned char *)
16                                         "To authorize or not to authorize?";
17         unsigned char authobj[128];
18         int authsize = sizeof(authobj);
19         unsigned char challenge[128];
20         int challengesize = sizeof(challenge);
21         int rc;
22         unsigned char key[20];
23         int keysize = sizeof(key);
24         unsigned char newsecret[20];
25         int newsecsize = sizeof(newsecret);
26         unsigned char newload[128];
27         int newloadsize=sizeof(newload);
28
29         rc = make_authobj(id, pass, nonce, secret, sizeof(secret),
30                         payload, strlen((char *)payload),
31                         authobj, &authsize);
32         printf("make_authobj() rc=%d size=%d\n", rc, authsize);
33         if (rc) return rc;
34
35         rc = make_challenge(id, pass, nonce, challenge, &challengesize);
36         printf("make_challenge() rc=%d size=%d\n", rc, challengesize);
37         if (rc) return rc;
38         rc = hmac(secret, sizeof(secret), challenge, challengesize,
39                 &key, &keysize);
40         printf("hmac(secret, challenge) rc=%d new_key_size=%d\n",
41                 rc, keysize);
42         if (rc) return rc;
43
44         rc = parse_authobj(key, sizeof(key), authobj, authsize,
45                         newsecret, &newsecsize, newload, &newloadsize);
46         printf("parse_authobj() rc=%d secretsize=%d payload=\"%.*s\" (%d)\n",
47                 rc, newsecsize, newloadsize, newload, newloadsize);
48         if (memcmp(secret, newsecret, newsecsize)) {
49                 printf("extracted secret does not match\n");
50                 return -1;
51         }
52         return 0;
53 }