From 1fb61b04544488148032c06475042aec167d6097 Mon Sep 17 00:00:00 2001 From: Eugene Crosser Date: Sat, 9 Nov 2013 23:48:33 +0400 Subject: [PATCH] systematize authobj errors --- authobj.c | 51 ++++++++++++++++++++++++++++----------------------- authobj.h | 2 ++ serial.c | 6 ++---- serial.h | 4 ++-- test_serial.c | 10 ++-------- 5 files changed, 36 insertions(+), 37 deletions(-) diff --git a/authobj.c b/authobj.c index 3bd9da5..2b774ab 100644 --- a/authobj.c +++ b/authobj.c @@ -13,11 +13,15 @@ int make_challenge(const char *id, const char *pass, const char *nonce, { serializer_t srl; - if (serial_init(&srl, challenge, *challengesize)) return -1; - if (serial_put(&srl, id, strlen(id)) != strlen(id)) return -2; - if (serial_put(&srl, pass, strlen(pass)) != strlen(pass)) return -3; - if (serial_put(&srl, nonce, strlen(nonce)) != strlen(nonce)) return -4; - if (serial_put(&srl, NULL, 0) != 0) return -5; + serial_init(&srl, challenge, *challengesize); + if (serial_put(&srl, id, strlen(id)) != strlen(id)) + return aoe_serial; + if (serial_put(&srl, pass, strlen(pass)) != strlen(pass)) + return aoe_serial; + if (serial_put(&srl, nonce, strlen(nonce)) != strlen(nonce)) + return aoe_serial; + if (serial_put(&srl, NULL, 0) != 0) + return aoe_serial; *challengesize = serial_size(&srl); return 0; } @@ -40,27 +44,27 @@ int make_authobj(const char *id, const char *pass, const char *nonce, datasize = ((secsize + paysize + HASHSIZE * 4 * sizeof(short) - 1) / CBLKSIZE + 1) * CBLKSIZE; data = alloca(datasize); - if (serial_init(&srl, data, datasize)) return -1; - if (serial_put(&srl, secret, secsize) != secsize) return -2; - if (serial_put(&srl, payload, paysize) != paysize) return -3; + serial_init(&srl, data, datasize); + if (serial_put(&srl, secret, secsize) != secsize) return aoe_serial; + if (serial_put(&srl, payload, paysize) != paysize) return aoe_serial; if (hash(data, serial_size(&srl), datahash, &datahashsize)) - return -4; + return aoe_size; if (serial_put(&srl, datahash, datahashsize) != datahashsize) - return -5; - if (serial_put(&srl, NULL, 0) != 0) return -6; + return aoe_serial; + if (serial_put(&srl, NULL, 0) != 0) return aoe_serial; datasize = ((serial_size(&srl) -1) / CBLKSIZE + 1) * CBLKSIZE; challengesize = ((strlen(id) + strlen(pass) + strlen(nonce) + 4 * sizeof(short) - 1) / CBLKSIZE + 1) * CBLKSIZE; challenge = alloca(challengesize); if (make_challenge(id, pass, nonce, challenge, &challengesize)) - return -7; + return aoe_serial; if (hmac(secret, secsize, challenge, challengesize, - key, &keysize)) return -8; + key, &keysize)) return aoe_crypt; - if (*bufsize < datasize) return -9; - if (encrypt(key, CBLKSIZE, data, buffer, datasize)) return -10; + if (*bufsize < datasize) return aoe_size; + if (encrypt(key, CBLKSIZE, data, buffer, datasize)) return aoe_crypt; *bufsize = datasize; return 0; @@ -81,19 +85,20 @@ int parse_authobj(const unsigned char *key, const int keysize, int theirhashsize = HASHSIZE; if (decrypt(key, CBLKSIZE, buffer, data, datasize)) - return -1; - if (serial_init(&srl, data, datasize)) return -2; + return aoe_crypt; + serial_init(&srl, data, datasize); tsize = *secsize; *secsize = serial_get(&srl, secret, tsize); - if (*secsize > tsize || *secsize <= 0) return -3; + if (*secsize > tsize || *secsize <= 0) return aoe_serial; tsize = *paysize; *paysize = serial_get(&srl, payload, tsize); - if (*paysize > tsize || *paysize <= 0) return -4; - if (hash(data, serial_size(&srl), myhash, &myhashsize)) return -5; + if (*paysize > tsize || *paysize <= 0) return aoe_serial; + if (hash(data, serial_size(&srl), myhash, &myhashsize)) + return aoe_crypt; theirhashsize = serial_get(&srl, theirhash, theirhashsize); - if (theirhashsize != HASHSIZE) return -6; + if (theirhashsize != HASHSIZE) return aoe_data; if ((myhashsize != theirhashsize) || - memcmp(myhash, theirhash, myhashsize)) - return -7; + memcmp(myhash, theirhash, myhashsize)) + return aoe_data; return 0; } diff --git a/authobj.h b/authobj.h index 8478d95..cc5baed 100644 --- a/authobj.h +++ b/authobj.h @@ -1,6 +1,8 @@ #ifndef _AUTHOBJ_H #define _AUTHOBJ_H +enum aobj_err {aoe_serial, aoe_size, aoe_crypt, aoe_data}; + 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, diff --git a/serial.c b/serial.c index 8a24abd..809ea7c 100644 --- a/serial.c +++ b/serial.c @@ -4,14 +4,13 @@ #include #include "serial.h" -int serial_init(serializer_t *srl, void *buffer, int size) +void serial_init(serializer_t *srl, void *buffer, int size) { srl->buffer = srl->cursor = buffer; srl->bufsize = size; - return 0; } -int serial_switch(serializer_t *srl, void *buffer, int size) +void serial_switch(serializer_t *srl, void *buffer, int size) { int used = srl->cursor - srl->buffer; @@ -19,7 +18,6 @@ int serial_switch(serializer_t *srl, void *buffer, int size) srl->buffer = buffer; srl->bufsize = size; srl->cursor = buffer + used; - return 0; } int serial_put(serializer_t *srl, const void *item, int size) diff --git a/serial.h b/serial.h index a501fa8..9cda40c 100644 --- a/serial.h +++ b/serial.h @@ -7,8 +7,8 @@ typedef struct _serializer { char *cursor; } serializer_t; -int serial_init(serializer_t *srl, void *buffer, int size); -int serial_switch(serializer_t *srl, void *buffer, int size); +void serial_init(serializer_t *srl, void *buffer, int size); +void serial_switch(serializer_t *srl, void *buffer, int size); int serial_put(serializer_t *srl, const void *item, int size); int serial_get(serializer_t *srl, void *item, int bufsize); int serial_size(serializer_t *srl); diff --git a/test_serial.c b/test_serial.c index b9d5e4b..c2a59e6 100644 --- a/test_serial.c +++ b/test_serial.c @@ -14,10 +14,7 @@ int main(int argc, char *argv[]) int i, rc; serializer_t srl; - if ((rc = serial_init(&srl, buffer, sizeof(buffer)))) { - printf("serial_init rc=%d\n", rc); - return 1; - } + serial_init(&srl, buffer, sizeof(buffer)); for (i = 0; in[i]; i++) { int size = strlen(in[i]); if ((rc = serial_put(&srl, in[i], size)) != size) { @@ -31,10 +28,7 @@ int main(int argc, char *argv[]) return 1; } printf("serialized size=%d\n", serial_size(&srl)); - if ((rc = serial_init(&srl, buffer, sizeof(buffer)))) { - printf("second serial_init rc=%d\n", rc); - return 1; - } + serial_init(&srl, buffer, sizeof(buffer)); for (i = 0; i < 4; i++) { char item[32]; memset(item, 0, sizeof(item)); -- 2.39.2