11 int make_authobj(const char *id, const char *pass, const char *nonce,
12 const unsigned char *secret, const int secsize,
13 const unsigned char *payload, const int paysize,
14 unsigned char *buffer, int *bufsize)
18 unsigned char datahash[HASHSIZE];
19 int datahashsize = HASHSIZE;
20 unsigned char *hmacdata;
22 unsigned char key[HASHSIZE];
23 int keysize = HASHSIZE;
26 datasize = ((secsize + paysize + HASHSIZE * 4 * sizeof(short) - 1) /
27 CBLKSIZE + 1) * CBLKSIZE;
28 data = alloca(datasize);
29 if (serial_init(&srl, data, datasize)) return -1;
30 if (serial_put(&srl, secret, secsize) != secsize) return -1;
31 if (serial_put(&srl, payload, paysize) != paysize) return -1;
32 if (hash(data, serial_size(&srl), datahash, &datahashsize))
34 if (serial_put(&srl, datahash, datahashsize) != datahashsize)
36 if (serial_put(&srl, NULL, 0) != 0) return -1;
37 datasize = ((serial_size(&srl) -1) / CBLKSIZE + 1) * CBLKSIZE;
39 hmacdatasize = ((strlen(id) + strlen(pass) + strlen(nonce) +
40 4 * sizeof(short) - 1) / CBLKSIZE + 1) * CBLKSIZE;
41 hmacdata = alloca(hmacdatasize);
42 if (serial_init(&srl, hmacdata, hmacdatasize)) return -1;
43 if (serial_put(&srl, id, strlen(id)) != strlen(id)) return -1;
44 if (serial_put(&srl, pass, strlen(pass)) != strlen(pass)) return -1;
45 if (serial_put(&srl, nonce, strlen(nonce)) != strlen(nonce)) return -1;
46 if (serial_put(&srl, NULL, 0) != 0) return -1;
47 hmacdatasize = ((serial_size(&srl) -1) / CBLKSIZE + 1) * CBLKSIZE;
49 if (hmac(secret, secsize, hmacdata, hmacdatasize,
50 key, &keysize)) return -1;
52 if (*bufsize < datasize) return -1;
54 if (encrypt(key, keysize, data, buffer, datasize)) return -1;
59 int parse_authobj(const unsigned char *hmacdata, const int hmacdatasize,
60 const unsigned char *buffer, const int bufsize,
61 unsigned char *secret, int *secsize,
62 unsigned char *payload, int *paysize)
64 int datasize = bufsize;
65 unsigned char *data = alloca(datasize);
68 unsigned char myhash[HASHSIZE];
69 int myhashsize = HASHSIZE;
70 unsigned char theirhash[HASHSIZE];
71 int theirhashsize = HASHSIZE;
73 if (decrypt(hmacdata, hmacdatasize, buffer, data, datasize))
75 if (serial_init(&srl, data, datasize)) return -1;
77 if ((*secsize = serial_get(&srl, secret, tsize)) > tsize) return -1;
79 if ((*paysize = serial_get(&srl, payload, tsize)) > tsize) return -1;
80 if (hash(data, serial_size(&srl), myhash, &myhashsize))
82 if ((theirhashsize = serial_get(&srl, theirhash, theirhashsize)) != HASHSIZE)
84 if ((myhashsize != theirhashsize) ||
85 memcmp(myhash, theirhash, myhashsize))