Annotation of libaitio/src/crypt.c, revision 1.3

1.2       misho       1: /*************************************************************************
1.3     ! misho       2: * (C) 2010 AITNET ltd - Sofia/Bulgaria - <misho@aitnet.org>
        !             3: *  by Michael Pounov <misho@elwix.org>
1.2       misho       4: *
                      5: * $Author: misho $
1.3     ! misho       6: * $Id: crypt.c,v 1.2.2.2 2011/04/20 22:55:42 misho Exp $
1.2       misho       7: *
1.3     ! misho       8: **************************************************************************
        !             9: The ELWIX and AITNET software is distributed under the following
        !            10: terms:
        !            11: 
        !            12: All of the documentation and software included in the ELWIX and AITNET
        !            13: Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org>
        !            14: 
        !            15: Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
        !            16:        by Michael Pounov <misho@elwix.org>.  All rights reserved.
        !            17: 
        !            18: Redistribution and use in source and binary forms, with or without
        !            19: modification, are permitted provided that the following conditions
        !            20: are met:
        !            21: 1. Redistributions of source code must retain the above copyright
        !            22:    notice, this list of conditions and the following disclaimer.
        !            23: 2. Redistributions in binary form must reproduce the above copyright
        !            24:    notice, this list of conditions and the following disclaimer in the
        !            25:    documentation and/or other materials provided with the distribution.
        !            26: 3. All advertising materials mentioning features or use of this software
        !            27:    must display the following acknowledgement:
        !            28: This product includes software developed by Michael Pounov <misho@elwix.org>
        !            29: ELWIX - Embedded LightWeight unIX and its contributors.
        !            30: 4. Neither the name of AITNET nor the names of its contributors
        !            31:    may be used to endorse or promote products derived from this software
        !            32:    without specific prior written permission.
        !            33: 
        !            34: THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``AS IS'' AND
        !            35: ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
        !            36: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
        !            37: ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
        !            38: FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
        !            39: DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
        !            40: OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
        !            41: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
        !            42: LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
        !            43: OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
        !            44: SUCH DAMAGE.
        !            45: */
1.2       misho      46: #include "global.h"
                     47: 
                     48: 
                     49: /*
                     50:  * ioCipher() Cipher wrapper for all supported crypto algorythms
                     51:  * @pInput = input buffer
                     52:  * @inLen = input buffer len
                     53:  * @ppOutput = output allocated buffe, must be free after use
                     54:  * @Cipher = cipher engine, like EVP_bf_cbc() or etc...
                     55:  * @pKey = key
                     56:  * @pIV = IV, salt (8 bytes)
                     57:  * @nMode = Mode 0 - decrypting or 1 - encrypting
                     58:  * return: 0 not present data or error!; >0 number of processed and returned bytes into ppOutput
                     59: */
                     60: int
                     61: ioCipher(u_char *pInput, int inLen, u_char **ppOutput, const EVP_CIPHER *Cipher, 
                     62:                u_char *pKey, u_char *pIV, int nMode)
                     63: {
                     64:        EVP_CIPHER_CTX ctx;
                     65:        int chunk, buflen, outlen = 0;
                     66:        u_char *pos, *buf = NULL;
                     67: 
                     68:        if (!pInput || !inLen || !ppOutput || nMode & 0xFFFFFFFE)
                     69:                return 0;
                     70:        buf = malloc(inLen + EVP_MAX_BLOCK_LENGTH);
                     71:        if (!buf) {
                     72:                LOGERR;
                     73:                goto end;
                     74:        } else
                     75:                memset(buf, 0, inLen + EVP_MAX_BLOCK_LENGTH);
                     76: 
                     77:        EVP_CIPHER_CTX_init(&ctx);
                     78:        EVP_CipherInit_ex(&ctx, Cipher, NULL, pKey, pIV, nMode);
                     79: 
                     80:        for (outlen = 0, buflen = -1, pos = pInput; inLen; outlen += buflen) {
                     81:                chunk = inLen > 7 ? 8 : inLen;
                     82: 
                     83:                if (!EVP_CipherUpdate(&ctx, buf + outlen, &buflen, pos, chunk)) {
                     84:                        EVP_CIPHER_CTX_cleanup(&ctx);
                     85:                        outlen = 0;
                     86:                        free(buf);
                     87:                        buf = NULL;
                     88:                        goto end;
                     89:                } else {
                     90:                        if (nMode && !buflen)
                     91:                                break;
                     92: 
                     93:                        pos += chunk;
                     94:                        inLen -= chunk;
                     95:                }
                     96:        }
                     97:        if (!EVP_CipherFinal_ex(&ctx, buf + outlen, &buflen)) {
                     98:                outlen = 0;
                     99:                free(buf);
                    100:                buf = NULL;
                    101:        } else
                    102:                outlen += buflen;
                    103: 
                    104:        EVP_CIPHER_CTX_cleanup(&ctx);
                    105: end:
                    106:        *ppOutput = buf;
                    107:        return outlen;
                    108: }
                    109: 
                    110: /*
                    111:  * io_Blowfish() Blowfish cipher algorythm, work with ASCII hex strings
                    112:  * @pInput = input buffer
                    113:  * @inLen = input buffer len
                    114:  * @ppOutput = output allocated buffe, must be free after use
                    115:  * @pKey = key
                    116:  * @pIV = IV, salt (8 bytes)
                    117:  * @nMode = Mode 0 - decrypting or 1 - encrypting
                    118:  * return: 0 not present data or error!; >0 number of processed and returned bytes into ppOutput
                    119: */
                    120: int
                    121: io_Blowfish(u_char *pInput, int inLen, u_char **ppOutput, u_char *pKey, u_char *pIV, int nMode)
                    122: {
                    123:        int len, ret;
                    124:        register int i;
                    125:        char bufCH[3], *str = NULL;
                    126:        u_char *buf = NULL;
                    127: 
                    128:        if (!pInput || !inLen || !ppOutput || nMode & 0xFFFFFFFE)
                    129:                return 0;
                    130: 
                    131:        if (nMode) {
                    132:                len = strlen((char*) pInput);
                    133:                str = strdup((char*) pInput);
                    134:        } else {
                    135:                len = strlen((char*) pInput) / 2;
                    136:                str = malloc(len + 1);
                    137:                if (!str) {
                    138:                        LOGERR;
                    139:                        return 0;
                    140:                } else
                    141:                        memset(str, 0, len + 1);
                    142: 
                    143:                for (i = 0; i < len * 2; i += 2) {
                    144:                        strlcpy(bufCH, (char*) &pInput[i], 3);
                    145:                        str[i / 2] = (u_char) strtol(bufCH, NULL, 16);
                    146:                }
                    147:        }
                    148: 
                    149:        ret = len = ioCipher((u_char*) str, len, &buf, EVP_bf_cbc(), pKey, pIV, nMode);
                    150:        free(str);
                    151: 
                    152:        if (nMode) {
                    153:                ret *= 2;
                    154:                *ppOutput = malloc(ret + 1);
                    155:                if (!*ppOutput) {
                    156:                        LOGERR;
                    157:                        return 0;
                    158:                } else
                    159:                        memset(*ppOutput, 0, ret + 1);
                    160: 
                    161:                for (i = 0; i < len; i++) {
                    162:                        memset(bufCH, 0, 3);
                    163:                        snprintf(bufCH, 3, "%02x", buf[i]);
                    164:                        strlcat((char*) *ppOutput, bufCH, ret + 1);
                    165:                }
                    166:        } else
                    167:                if (ret && buf)
                    168:                        *ppOutput = (u_char*) strdup((char*) buf);
                    169: 
                    170:        return ret;
                    171: }

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