From 2961e53fa16c04cbb2e90015d68a0b9454c2edd6 Mon Sep 17 00:00:00 2001 From: Eugene Crosser Date: Thu, 7 Nov 2013 01:58:39 +0400 Subject: [PATCH] introduce make_challenge() --- authobj.c | 36 +++++++++++++++++++++++------------- authobj.h | 4 +++- 2 files changed, 26 insertions(+), 14 deletions(-) diff --git a/authobj.c b/authobj.c index ec92907..cb6f4f3 100644 --- a/authobj.c +++ b/authobj.c @@ -8,6 +8,20 @@ #include "crypto.h" #include "authobj.h" +int make_challenge(const char *id, const char *pass, const char *nonce, + unsigned char *challenge, int *challengesize) +{ + serializer_t srl; + + if (serial_init(&srl, challenge, *challengesize)) return -1; + if (serial_put(&srl, id, strlen(id)) != strlen(id)) return -1; + if (serial_put(&srl, pass, strlen(pass)) != strlen(pass)) return -1; + if (serial_put(&srl, nonce, strlen(nonce)) != strlen(nonce)) return -1; + if (serial_put(&srl, NULL, 0) != 0) return -1; + *challengesize = ((serial_size(&srl) -1) / CBLKSIZE + 1) * CBLKSIZE; + return 0; +} + int make_authobj(const char *id, const char *pass, const char *nonce, const unsigned char *secret, const int secsize, const unsigned char *payload, const int paysize, @@ -17,8 +31,8 @@ int make_authobj(const char *id, const char *pass, const char *nonce, int datasize; unsigned char datahash[HASHSIZE]; int datahashsize = HASHSIZE; - unsigned char *hmacdata; - int hmacdatasize; + unsigned char *challenge; + int challengesize; unsigned char key[HASHSIZE]; int keysize = HASHSIZE; serializer_t srl; @@ -36,17 +50,13 @@ int make_authobj(const char *id, const char *pass, const char *nonce, if (serial_put(&srl, NULL, 0) != 0) return -1; datasize = ((serial_size(&srl) -1) / CBLKSIZE + 1) * CBLKSIZE; - hmacdatasize = ((strlen(id) + strlen(pass) + strlen(nonce) + + challengesize = ((strlen(id) + strlen(pass) + strlen(nonce) + 4 * sizeof(short) - 1) / CBLKSIZE + 1) * CBLKSIZE; - hmacdata = alloca(hmacdatasize); - if (serial_init(&srl, hmacdata, hmacdatasize)) return -1; - if (serial_put(&srl, id, strlen(id)) != strlen(id)) return -1; - if (serial_put(&srl, pass, strlen(pass)) != strlen(pass)) return -1; - if (serial_put(&srl, nonce, strlen(nonce)) != strlen(nonce)) return -1; - if (serial_put(&srl, NULL, 0) != 0) return -1; - hmacdatasize = ((serial_size(&srl) -1) / CBLKSIZE + 1) * CBLKSIZE; + challenge = alloca(challengesize); + if (make_challenge(id, pass, nonce, challenge, &challengesize)) + return -1; - if (hmac(secret, secsize, hmacdata, hmacdatasize, + if (hmac(secret, secsize, challenge, challengesize, key, &keysize)) return -1; if (*bufsize < datasize) return -1; @@ -56,7 +66,7 @@ int make_authobj(const char *id, const char *pass, const char *nonce, return 0; } -int parse_authobj(const unsigned char *hmacdata, const int hmacdatasize, +int parse_authobj(const unsigned char *key, const int keysize, const unsigned char *buffer, const int bufsize, unsigned char *secret, int *secsize, unsigned char *payload, int *paysize) @@ -70,7 +80,7 @@ int parse_authobj(const unsigned char *hmacdata, const int hmacdatasize, unsigned char theirhash[HASHSIZE]; int theirhashsize = HASHSIZE; - if (decrypt(hmacdata, hmacdatasize, buffer, data, datasize)) + if (decrypt(key, keysize, buffer, data, datasize)) return -1; if (serial_init(&srl, data, datasize)) return -1; tsize = *secsize; diff --git a/authobj.h b/authobj.h index c2da504..8478d95 100644 --- a/authobj.h +++ b/authobj.h @@ -1,11 +1,13 @@ #ifndef _AUTHOBJ_H #define _AUTHOBJ_H +int make_challenge(const char *id, const char *pass, const char *nonce, + unsigned char *challenge, int *challengesize); int make_authobj(const char *id, const char *pass, const char *nonce, const unsigned char *secret, const int secsize, const unsigned char *payload, const int paysize, unsigned char *buffer, int *bufsize); -int parse_authobj(const unsigned char *hmacdata, const int hmacdatasize, +int parse_authobj(const unsigned char *key, const int keysize, const unsigned char *buffer, const int bufsize, unsigned char *secret, int *secsize, unsigned char *payload, int *paysize); -- 2.39.2