11 int make_challenge(const char *id, const char *pass, const char *nonce,
12 unsigned char *challenge, int *challengesize)
16 if (serial_init(&srl, challenge, *challengesize)) return -1;
17 if (serial_put(&srl, id, strlen(id)) != strlen(id)) return -1;
18 if (serial_put(&srl, pass, strlen(pass)) != strlen(pass)) return -1;
19 if (serial_put(&srl, nonce, strlen(nonce)) != strlen(nonce)) return -1;
20 if (serial_put(&srl, NULL, 0) != 0) return -1;
21 *challengesize = ((serial_size(&srl) -1) / CBLKSIZE + 1) * CBLKSIZE;
25 int make_authobj(const char *id, const char *pass, const char *nonce,
26 const unsigned char *secret, const int secsize,
27 const unsigned char *payload, const int paysize,
28 unsigned char *buffer, int *bufsize)
32 unsigned char datahash[HASHSIZE];
33 int datahashsize = HASHSIZE;
34 unsigned char *challenge;
36 unsigned char key[HASHSIZE];
37 int keysize = HASHSIZE;
40 datasize = ((secsize + paysize + HASHSIZE * 4 * sizeof(short) - 1) /
41 CBLKSIZE + 1) * CBLKSIZE;
42 data = alloca(datasize);
43 if (serial_init(&srl, data, datasize)) return -1;
44 if (serial_put(&srl, secret, secsize) != secsize) return -1;
45 if (serial_put(&srl, payload, paysize) != paysize) return -1;
46 if (hash(data, serial_size(&srl), datahash, &datahashsize))
48 if (serial_put(&srl, datahash, datahashsize) != datahashsize)
50 if (serial_put(&srl, NULL, 0) != 0) return -1;
51 datasize = ((serial_size(&srl) -1) / CBLKSIZE + 1) * CBLKSIZE;
53 challengesize = ((strlen(id) + strlen(pass) + strlen(nonce) +
54 4 * sizeof(short) - 1) / CBLKSIZE + 1) * CBLKSIZE;
55 challenge = alloca(challengesize);
56 if (make_challenge(id, pass, nonce, challenge, &challengesize))
59 if (hmac(secret, secsize, challenge, challengesize,
60 key, &keysize)) return -1;
63 for (i = 0; i < keysize; i++) printf(", 0x%02x", key[i]);
67 if (*bufsize < datasize) return -1;
69 if (encrypt(key, CBLKSIZE, data, buffer, datasize)) return -1;
74 int parse_authobj(const unsigned char *key, const int keysize,
75 const unsigned char *buffer, const int bufsize,
76 unsigned char *secret, int *secsize,
77 unsigned char *payload, int *paysize)
79 int datasize = bufsize;
80 unsigned char *data = alloca(datasize);
83 unsigned char myhash[HASHSIZE];
84 int myhashsize = HASHSIZE;
85 unsigned char theirhash[HASHSIZE];
86 int theirhashsize = HASHSIZE;
88 if (decrypt(key, CBLKSIZE, buffer, data, datasize))
90 if (serial_init(&srl, data, datasize)) return -1;
92 if ((*secsize = serial_get(&srl, secret, tsize)) > tsize) return -1;
94 if ((*paysize = serial_get(&srl, payload, tsize)) > tsize) return -1;
95 if (hash(data, serial_size(&srl), myhash, &myhashsize))
97 if ((theirhashsize = serial_get(&srl, theirhash, theirhashsize)) != HASHSIZE)
99 if ((myhashsize != theirhashsize) ||
100 memcmp(myhash, theirhash, myhashsize))