File:  [ELWIX - Embedded LightWeight unIX -] / libaitio / src / crypt.c
Revision 1.2: download - view: text, annotated - select for diffs - revision graph
Tue Apr 19 20:00:31 2011 UTC (13 years, 2 months ago) by misho
Branches: MAIN
CVS tags: io1_7, IO1_6, HEAD
release 1.6

    1: /*************************************************************************
    2: * (C) 2010 AITNET ltd - Sofia/Bulgaria - <misho@aitbg.com>
    3: *  by Michael Pounov <misho@openbsd-bg.org>
    4: *
    5: * $Author: misho $
    6: * $Id: crypt.c,v 1.2 2011/04/19 20:00:31 misho Exp $
    7: *
    8: *************************************************************************/
    9: #include "global.h"
   10: 
   11: 
   12: /*
   13:  * ioCipher() Cipher wrapper for all supported crypto algorythms
   14:  * @pInput = input buffer
   15:  * @inLen = input buffer len
   16:  * @ppOutput = output allocated buffe, must be free after use
   17:  * @Cipher = cipher engine, like EVP_bf_cbc() or etc...
   18:  * @pKey = key
   19:  * @pIV = IV, salt (8 bytes)
   20:  * @nMode = Mode 0 - decrypting or 1 - encrypting
   21:  * return: 0 not present data or error!; >0 number of processed and returned bytes into ppOutput
   22: */
   23: int
   24: ioCipher(u_char *pInput, int inLen, u_char **ppOutput, const EVP_CIPHER *Cipher, 
   25: 		u_char *pKey, u_char *pIV, int nMode)
   26: {
   27: 	EVP_CIPHER_CTX ctx;
   28: 	int chunk, buflen, outlen = 0;
   29: 	u_char *pos, *buf = NULL;
   30: 
   31: 	if (!pInput || !inLen || !ppOutput || nMode & 0xFFFFFFFE)
   32: 		return 0;
   33: 	buf = malloc(inLen + EVP_MAX_BLOCK_LENGTH);
   34: 	if (!buf) {
   35: 		LOGERR;
   36: 		goto end;
   37: 	} else
   38: 		memset(buf, 0, inLen + EVP_MAX_BLOCK_LENGTH);
   39: 
   40: 	EVP_CIPHER_CTX_init(&ctx);
   41: 	EVP_CipherInit_ex(&ctx, Cipher, NULL, pKey, pIV, nMode);
   42: 
   43: 	for (outlen = 0, buflen = -1, pos = pInput; inLen; outlen += buflen) {
   44: 		chunk = inLen > 7 ? 8 : inLen;
   45: 
   46: 		if (!EVP_CipherUpdate(&ctx, buf + outlen, &buflen, pos, chunk)) {
   47: 			EVP_CIPHER_CTX_cleanup(&ctx);
   48: 			outlen = 0;
   49: 			free(buf);
   50: 			buf = NULL;
   51: 			goto end;
   52: 		} else {
   53: 			if (nMode && !buflen)
   54: 				break;
   55: 
   56: 			pos += chunk;
   57: 			inLen -= chunk;
   58: 		}
   59: 	}
   60: 	if (!EVP_CipherFinal_ex(&ctx, buf + outlen, &buflen)) {
   61: 		outlen = 0;
   62: 		free(buf);
   63: 		buf = NULL;
   64: 	} else
   65: 		outlen += buflen;
   66: 
   67: 	EVP_CIPHER_CTX_cleanup(&ctx);
   68: end:
   69: 	*ppOutput = buf;
   70: 	return outlen;
   71: }
   72: 
   73: /*
   74:  * io_Blowfish() Blowfish cipher algorythm, work with ASCII hex strings
   75:  * @pInput = input buffer
   76:  * @inLen = input buffer len
   77:  * @ppOutput = output allocated buffe, must be free after use
   78:  * @pKey = key
   79:  * @pIV = IV, salt (8 bytes)
   80:  * @nMode = Mode 0 - decrypting or 1 - encrypting
   81:  * return: 0 not present data or error!; >0 number of processed and returned bytes into ppOutput
   82: */
   83: int
   84: io_Blowfish(u_char *pInput, int inLen, u_char **ppOutput, u_char *pKey, u_char *pIV, int nMode)
   85: {
   86: 	int len, ret;
   87: 	register int i;
   88: 	char bufCH[3], *str = NULL;
   89: 	u_char *buf = NULL;
   90: 
   91: 	if (!pInput || !inLen || !ppOutput || nMode & 0xFFFFFFFE)
   92: 		return 0;
   93: 
   94: 	if (nMode) {
   95: 		len = strlen((char*) pInput);
   96: 		str = strdup((char*) pInput);
   97: 	} else {
   98: 		len = strlen((char*) pInput) / 2;
   99: 		str = malloc(len + 1);
  100: 		if (!str) {
  101: 			LOGERR;
  102: 			return 0;
  103: 		} else
  104: 			memset(str, 0, len + 1);
  105: 
  106: 		for (i = 0; i < len * 2; i += 2) {
  107: 			strlcpy(bufCH, (char*) &pInput[i], 3);
  108: 			str[i / 2] = (u_char) strtol(bufCH, NULL, 16);
  109: 		}
  110: 	}
  111: 
  112: 	ret = len = ioCipher((u_char*) str, len, &buf, EVP_bf_cbc(), pKey, pIV, nMode);
  113: 	free(str);
  114: 
  115: 	if (nMode) {
  116: 		ret *= 2;
  117: 		*ppOutput = malloc(ret + 1);
  118: 		if (!*ppOutput) {
  119: 			LOGERR;
  120: 			return 0;
  121: 		} else
  122: 			memset(*ppOutput, 0, ret + 1);
  123: 
  124: 		for (i = 0; i < len; i++) {
  125: 			memset(bufCH, 0, 3);
  126: 			snprintf(bufCH, 3, "%02x", buf[i]);
  127: 			strlcat((char*) *ppOutput, bufCH, ret + 1);
  128: 		}
  129: 	} else
  130: 		if (ret && buf)
  131: 			*ppOutput = (u_char*) strdup((char*) buf);
  132: 
  133: 	return ret;
  134: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>