[BACK]Return to block.c CVS log [TXT][DIR] Up to [local] / OpenXM / src / ox_ntl / crypt / des

Annotation of OpenXM/src/ox_ntl/crypt/des/block.c, Revision 1.2

1.2     ! iwane       1: /* $OpenXM: OpenXM/src/ox_ntl/crypt/des/block.c,v 1.1 2004/07/11 00:32:17 iwane Exp $ */
1.1       iwane       2:
                      3: #include <stdlib.h>
                      4: #include <stdio.h>
                      5: #include <string.h>
                      6:
                      7:
                      8: #define CRYPTO_INPUT_CHECK 1
                      9:
                     10: #include "block.h"
                     11:
                     12: int
                     13: crypt_ecb(void *key, int block, int len,
                     14:     const unsigned char *data, unsigned char *buf,
                     15:    void *func)
                     16: {
                     17:        int i, ret;
                     18:        int (*crypto)(void *key, const unsigned char *, unsigned char *) =
                     19:            (int (*)(void *, const unsigned char *, unsigned char *))func;
                     20:
                     21: #if CRYPTO_INPUT_CHECK
                     22:        if (len % block != 0)
                     23:                return (-1);
                     24: #endif
                     25:
                     26:        for (i = 0; i < len / block; i++) {
                     27:                ret = crypto(key, data + i * block, buf + i * block);
                     28:        }
                     29:
                     30:        return (0);
                     31: }
                     32:
                     33:
                     34:
                     35: int
1.2     ! iwane      36: crypt_enc_cbc(void *key, int block, unsigned char *iv, int len,
1.1       iwane      37:     const unsigned char *data, unsigned char *buf, void *func)
                     38: {
                     39:        int i, j, ret;
                     40:        unsigned char xor[1024];
                     41:        const unsigned char *ivp;
                     42:        int (*crypto)(void *key, const unsigned char *, unsigned char *) =
                     43:            (int (*)(void *, const unsigned char *, unsigned char *))func;
                     44:
                     45: #if CRYPTO_INPUT_CHECK
                     46:        if (len % block != 0)
                     47:                return (-1);
                     48: #endif
                     49:
                     50:        ivp = iv;
                     51:        for (i = 0; i < len; i += block) {
                     52:                for (j = 0; j < block; j++) {
                     53:                        xor[j] = (unsigned char)((data[i + j] ^ ivp[j]) & 0xff);
                     54:                }
                     55:                ret = crypto(key, xor, buf + i);
                     56:                ivp = buf + i;
                     57:        }
                     58:
1.2     ! iwane      59:        memcpy(iv, ivp, block);
        !            60:
1.1       iwane      61:        return (0);
                     62: }
                     63:
                     64:
                     65: int
1.2     ! iwane      66: crypt_dec_cbc(void *key, int block, unsigned char *iv, int len,
1.1       iwane      67:     const unsigned char *enc,
                     68:     unsigned char *buf,
                     69:     void *func)
                     70: {
                     71:        int i, j, ret;
                     72:        unsigned char xor[1024];
                     73:        const unsigned char *ivp;
                     74:        int (*crypto)(void *key, const unsigned char *, unsigned char *) =
                     75:            (int (*)(void *, const unsigned char *, unsigned char *))func;
                     76:
                     77: #if CRYPTO_INPUT_CHECK
                     78:        if (len % block != 0)
                     79:                return (-1);
                     80: #endif
                     81:
                     82:        ivp = iv;
                     83:        for (i = 0; i < len; i += block) {
                     84:                ret = crypto(key, enc + i, xor);
                     85:
                     86:                for (j = 0; j < block; j++) {
                     87:                        buf[i + j] = (unsigned char)((xor[j] ^ ivp[j]) & 0xff);
                     88:                }
                     89:                ivp = enc + i;
                     90:        }
1.2     ! iwane      91:
        !            92:        memcpy(iv, ivp, block);
1.1       iwane      93:
                     94:        return (0);
                     95: }
                     96:
                     97:
                     98:
                     99:
                    100: /*
                    101:  * if (mode == 0) encode. else decode.
                    102:  */
                    103: int
                    104: crypt_cfb(void *key, int mode, int bit, int block,
                    105:     const unsigned char *iv, int len,
                    106:     const unsigned char *data, unsigned char *buf,
                    107:     void *func)
                    108: {
                    109:        int i, j, ret;
                    110:        int b = bit / 8, c;
                    111:        unsigned char enc[1024];   /* sizeof(enc) >= block */
                    112:        unsigned char ivb[1024];
                    113:        unsigned char *ivp;
                    114:        unsigned char *in;
                    115:        const unsigned char *p;
                    116:        int (*crypto)(void *key, const unsigned char *, unsigned char *) =
                    117:            (int (*)(void *, const unsigned char *, unsigned char *))func;
                    118:
                    119: #if CRYPTO_INPUT_CHECK
                    120:        if (len % b != 0)
                    121:                return (-1);
                    122:        if (bit % 8 != 0)
                    123:                return (-3);
                    124: #endif
                    125:
                    126:        if (mode)
                    127:                p = data;
                    128:        else
                    129:                p = buf;
                    130:
                    131:
                    132:        ivp = ivb;
                    133:        memcpy(ivp, iv, block);
                    134:        c = 0;
                    135:        for (i = 0; i < len; i += b) {
                    136:                in = ivp + c;
                    137:                ret = crypto(key, ivp + c, enc);
                    138:                for (j = 0; j < b; j++) {
                    139:                        buf[i + j] = (char)((data[i + j] ^ enc[j]) & 0xff);
                    140:                        ivp[c + j + block] = p[i + j];
                    141:                }
                    142:
                    143:                if (c + b > (int)sizeof(ivb) - block - 2) {
                    144:                        memcpy(ivp, ivp + c + b, block);
                    145:                        c = 0;
                    146:                } else {
                    147:                        c += b;
                    148:                }
                    149:        }
                    150:
                    151:        return (0);
                    152: }
                    153:
                    154:
                    155:
                    156: int
                    157: crypt_ofb(void *key, int bit, int block,
                    158:     const unsigned char *iv, int len,
                    159:     const unsigned char *data, unsigned char *buf,
                    160:     void *func)
                    161: {
                    162:        int i, j, ret;
                    163:        int b = bit / 8, c;
                    164:        unsigned char ivb[1024];
                    165:        unsigned char *ivp;
                    166:        unsigned char enc[1024];
                    167:        int (*crypto)(void *key, const unsigned char *, unsigned char *) =
                    168:            (int (*)(void *, const unsigned char *, unsigned char *))func;
                    169:
                    170: #if CRYPTO_INPUT_CHECK
                    171:        if (len % b != 0)
                    172:                return (-1);
                    173:        if (bit % 8 != 0)
                    174:                return (-3);
                    175: #endif
                    176:
                    177:        memcpy(ivb, iv, block);
                    178:        ivp = ivb;
                    179:        c = 0;
                    180:        for (i = 0; i < len; i += b) {
                    181:                ret = crypto(key, ivp + c, enc);
                    182:                for (j = 0; j < b; j++) {
                    183:                        buf[i + j] = (char)((data[i + j] ^ enc[j]) & 0xff);
                    184:                        ivp[c + j + block] = enc[j];
                    185:                }
                    186:                if (c + b > (int)sizeof(ivb) - block - 2) {
                    187:                        memcpy(ivp, ivp + c + b, block);
                    188:                        c = 0;
                    189:                } else {
                    190:                        c += b;
                    191:                }
                    192:        }
                    193:
                    194:        return (0);
                    195: }
                    196:
                    197:
                    198:
                    199:
                    200:
                    201:
                    202: int
                    203: crypt_enc_cfb_pgp(void *key, int block,
                    204:     const unsigned char *data, int len,
                    205:     unsigned char *buf, int buflen,
                    206:     void *func)
                    207: {
                    208:        int i, j, c, ret;
                    209:        unsigned char enc[1024];
                    210:        const unsigned char *ivp;
                    211:        unsigned char fr[128], fre[128], rndm[128];
                    212:        int (*crypto)(void *key, const unsigned char *, unsigned char *) =
                    213:            (int (*)(void *, const unsigned char *, unsigned char *))func;
                    214:
                    215: #if CRYPTO_INPUT_CHECK
                    216:        if (buflen < len + block + 2)
                    217:                return (-1);
                    218: #endif
                    219:
                    220:        for (j = 0; j < block; j++) {
                    221:                rndm[j] = (char)((random() >> 5) & 0xff);
                    222:        }
                    223:
                    224:        memset(fr, 0x00, block);
                    225:        ret = crypto(key, fr, fre);
                    226:        for (j = 0; j < block; j++) {
                    227:                buf[j] = (char)((fre[j] ^ rndm[j]) & 0xff);
                    228:        }
                    229:        ivp = buf;
                    230:        ret = crypto(key, ivp, enc);
                    231:
                    232:        buf[block] = (char)((enc[0] ^ rndm[block - 2]) & 0xff);
                    233:        buf[block + 1] = (char)((enc[1] ^ rndm[block - 1]) & 0xff);
                    234:
                    235:        ivp = buf + 2;
                    236:        for (i = 0; i < len; i += block) {
                    237:                ret = crypto(key, ivp, enc);
                    238:
                    239:                c = (len - i) / block ? block : len % block;
                    240:
                    241:                for (j = 0; j < c; j++) {
                    242:                        buf[block + 2 + i + j] = (char)((data[i + j] ^ enc[j]) & 0xff);
                    243:                }
                    244:                ivp = buf + i;
                    245:        }
                    246:
                    247:        return (block + len + 2);
                    248: }
                    249:
                    250:
                    251:

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