--- libaitio/src/crypt.c 2011/04/20 22:56:32 1.3 +++ libaitio/src/crypt.c 2012/07/03 08:51:05 1.7 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: crypt.c,v 1.3 2011/04/20 22:56:32 misho Exp $ +* $Id: crypt.c,v 1.7 2012/07/03 08:51:05 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -12,7 +12,7 @@ terms: All of the documentation and software included in the ELWIX and AITNET Releases is copyrighted by ELWIX - Sofia/Bulgaria -Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 +Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012 by Michael Pounov . All rights reserved. Redistribution and use in source and binary forms, with or without @@ -47,10 +47,11 @@ SUCH DAMAGE. /* - * ioCipher() Cipher wrapper for all supported crypto algorythms + * ioCipher() - Cipher wrapper for all supported crypto algorythms + * * @pInput = input buffer * @inLen = input buffer len - * @ppOutput = output allocated buffe, must be free after use + * @ppOutput = output allocated buffe, must be io_free after use * @Cipher = cipher engine, like EVP_bf_cbc() or etc... * @pKey = key * @pIV = IV, salt (8 bytes) @@ -67,7 +68,7 @@ ioCipher(u_char *pInput, int inLen, u_char **ppOutput, if (!pInput || !inLen || !ppOutput || nMode & 0xFFFFFFFE) return 0; - buf = malloc(inLen + EVP_MAX_BLOCK_LENGTH); + buf = io_malloc(inLen + EVP_MAX_BLOCK_LENGTH); if (!buf) { LOGERR; goto end; @@ -83,7 +84,7 @@ ioCipher(u_char *pInput, int inLen, u_char **ppOutput, if (!EVP_CipherUpdate(&ctx, buf + outlen, &buflen, pos, chunk)) { EVP_CIPHER_CTX_cleanup(&ctx); outlen = 0; - free(buf); + io_free(buf); buf = NULL; goto end; } else { @@ -96,7 +97,7 @@ ioCipher(u_char *pInput, int inLen, u_char **ppOutput, } if (!EVP_CipherFinal_ex(&ctx, buf + outlen, &buflen)) { outlen = 0; - free(buf); + io_free(buf); buf = NULL; } else outlen += buflen; @@ -108,10 +109,11 @@ end: } /* - * io_Blowfish() Blowfish cipher algorythm, work with ASCII hex strings + * io_Blowfish() - Blowfish cipher algorythm, work with ASCII hex strings + * * @pInput = input buffer * @inLen = input buffer len - * @ppOutput = output allocated buffe, must be free after use + * @ppOutput = output allocated buffe, must be io_free after use * @pKey = key * @pIV = IV, salt (8 bytes) * @nMode = Mode 0 - decrypting or 1 - encrypting @@ -133,7 +135,7 @@ io_Blowfish(u_char *pInput, int inLen, u_char **ppOutp str = strdup((char*) pInput); } else { len = strlen((char*) pInput) / 2; - str = malloc(len + 1); + str = io_malloc(len + 1); if (!str) { LOGERR; return 0; @@ -147,11 +149,11 @@ io_Blowfish(u_char *pInput, int inLen, u_char **ppOutp } ret = len = ioCipher((u_char*) str, len, &buf, EVP_bf_cbc(), pKey, pIV, nMode); - free(str); + io_free(str); if (nMode) { ret *= 2; - *ppOutput = malloc(ret + 1); + *ppOutput = io_malloc(ret + 1); if (!*ppOutput) { LOGERR; return 0; @@ -168,4 +170,54 @@ io_Blowfish(u_char *pInput, int inLen, u_char **ppOutp *ppOutput = (u_char*) strdup((char*) buf); return ret; +} + +/* + * io_ctr_AES() - Encrypt/Decrypt stream cipher CTR_AES + * + * @pInput = Input buffer with ASCII + * @inLen = Input buffer data length + * @ppOutput = Output buffer with cipher data, must be io_free after use + * @pKey = Key + * @IV = IVector/Nonce/Counter, Warning: IV must be variable, because we write there!!! + * return: -1 error or >-1 how many cipher blocks proceeded + */ +int +io_ctr_AES(u_char *pInput, int inLen, u_char **ppOutput, u_char *pKey, u_char IV[AES_BLOCK_SIZE]) +{ + u_int num; + AES_KEY key; + u_char ecount_buf[AES_BLOCK_SIZE] = { 0 }; + int total = 0; + + if (!pInput || !inLen || !ppOutput) + return -1; + + *ppOutput = io_malloc(inLen); + if (!*ppOutput) { + LOGERR; + return -1; + } else + memset(*ppOutput, 0, inLen); + + AES_set_encrypt_key(pKey, 128, &key); + + while (inLen) { + num = 0; + memset(ecount_buf, 0, sizeof ecount_buf); + AES_ctr128_encrypt(pInput + total, (*ppOutput) + total, + (inLen / (AES_BLOCK_SIZE - 1)) ? (AES_BLOCK_SIZE - 1) : inLen, + &key, IV, ecount_buf, &num); + if (num < 1) { + io_free(*ppOutput); + *ppOutput = NULL; + total = -1; + break; + } else { + total += num; + inLen -= num; + } + } + + return total; }