Annotation of OpenXM/src/ox_ntl/crypt/des/block.c, Revision 1.1
1.1 ! iwane 1: /* $OpenXM$ */
! 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
! 36: crypt_enc_cbc(void *key, int block, const unsigned char *iv, int len,
! 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:
! 59: return (0);
! 60: }
! 61:
! 62:
! 63: int
! 64: crypt_dec_cbc(void *key, int block, const unsigned char *iv, int len,
! 65: const unsigned char *enc,
! 66: unsigned char *buf,
! 67: void *func)
! 68: {
! 69: int i, j, ret;
! 70: unsigned char xor[1024];
! 71: const unsigned char *ivp;
! 72: int (*crypto)(void *key, const unsigned char *, unsigned char *) =
! 73: (int (*)(void *, const unsigned char *, unsigned char *))func;
! 74:
! 75: #if CRYPTO_INPUT_CHECK
! 76: if (len % block != 0)
! 77: return (-1);
! 78: #endif
! 79:
! 80: ivp = iv;
! 81: for (i = 0; i < len; i += block) {
! 82: ret = crypto(key, enc + i, xor);
! 83:
! 84: for (j = 0; j < block; j++) {
! 85: buf[i + j] = (unsigned char)((xor[j] ^ ivp[j]) & 0xff);
! 86: }
! 87: ivp = enc + i;
! 88: }
! 89:
! 90: return (0);
! 91: }
! 92:
! 93:
! 94:
! 95:
! 96: /*
! 97: * if (mode == 0) encode. else decode.
! 98: */
! 99: int
! 100: crypt_cfb(void *key, int mode, int bit, int block,
! 101: const unsigned char *iv, int len,
! 102: const unsigned char *data, unsigned char *buf,
! 103: void *func)
! 104: {
! 105: int i, j, ret;
! 106: int b = bit / 8, c;
! 107: unsigned char enc[1024]; /* sizeof(enc) >= block */
! 108: unsigned char ivb[1024];
! 109: unsigned char *ivp;
! 110: unsigned char *in;
! 111: const unsigned char *p;
! 112: int (*crypto)(void *key, const unsigned char *, unsigned char *) =
! 113: (int (*)(void *, const unsigned char *, unsigned char *))func;
! 114:
! 115: #if CRYPTO_INPUT_CHECK
! 116: if (len % b != 0)
! 117: return (-1);
! 118: if (bit % 8 != 0)
! 119: return (-3);
! 120: #endif
! 121:
! 122: if (mode)
! 123: p = data;
! 124: else
! 125: p = buf;
! 126:
! 127:
! 128: ivp = ivb;
! 129: memcpy(ivp, iv, block);
! 130: c = 0;
! 131: for (i = 0; i < len; i += b) {
! 132: in = ivp + c;
! 133: ret = crypto(key, ivp + c, enc);
! 134: for (j = 0; j < b; j++) {
! 135: buf[i + j] = (char)((data[i + j] ^ enc[j]) & 0xff);
! 136: ivp[c + j + block] = p[i + j];
! 137: }
! 138:
! 139: if (c + b > (int)sizeof(ivb) - block - 2) {
! 140: memcpy(ivp, ivp + c + b, block);
! 141: c = 0;
! 142: } else {
! 143: c += b;
! 144: }
! 145: }
! 146:
! 147: return (0);
! 148: }
! 149:
! 150:
! 151:
! 152: int
! 153: crypt_ofb(void *key, int bit, int block,
! 154: const unsigned char *iv, int len,
! 155: const unsigned char *data, unsigned char *buf,
! 156: void *func)
! 157: {
! 158: int i, j, ret;
! 159: int b = bit / 8, c;
! 160: unsigned char ivb[1024];
! 161: unsigned char *ivp;
! 162: unsigned char enc[1024];
! 163: int (*crypto)(void *key, const unsigned char *, unsigned char *) =
! 164: (int (*)(void *, const unsigned char *, unsigned char *))func;
! 165:
! 166: #if CRYPTO_INPUT_CHECK
! 167: if (len % b != 0)
! 168: return (-1);
! 169: if (bit % 8 != 0)
! 170: return (-3);
! 171: #endif
! 172:
! 173: memcpy(ivb, iv, block);
! 174: ivp = ivb;
! 175: c = 0;
! 176: for (i = 0; i < len; i += b) {
! 177: ret = crypto(key, ivp + c, enc);
! 178: for (j = 0; j < b; j++) {
! 179: buf[i + j] = (char)((data[i + j] ^ enc[j]) & 0xff);
! 180: ivp[c + j + block] = enc[j];
! 181: }
! 182: if (c + b > (int)sizeof(ivb) - block - 2) {
! 183: memcpy(ivp, ivp + c + b, block);
! 184: c = 0;
! 185: } else {
! 186: c += b;
! 187: }
! 188: }
! 189:
! 190: return (0);
! 191: }
! 192:
! 193:
! 194:
! 195:
! 196:
! 197:
! 198: int
! 199: crypt_enc_cfb_pgp(void *key, int block,
! 200: const unsigned char *data, int len,
! 201: unsigned char *buf, int buflen,
! 202: void *func)
! 203: {
! 204: int i, j, c, ret;
! 205: unsigned char enc[1024];
! 206: const unsigned char *ivp;
! 207: unsigned char fr[128], fre[128], rndm[128];
! 208: int (*crypto)(void *key, const unsigned char *, unsigned char *) =
! 209: (int (*)(void *, const unsigned char *, unsigned char *))func;
! 210:
! 211: #if CRYPTO_INPUT_CHECK
! 212: if (buflen < len + block + 2)
! 213: return (-1);
! 214: #endif
! 215:
! 216: for (j = 0; j < block; j++) {
! 217: rndm[j] = (char)((random() >> 5) & 0xff);
! 218: }
! 219:
! 220: memset(fr, 0x00, block);
! 221: ret = crypto(key, fr, fre);
! 222: for (j = 0; j < block; j++) {
! 223: buf[j] = (char)((fre[j] ^ rndm[j]) & 0xff);
! 224: }
! 225: ivp = buf;
! 226: ret = crypto(key, ivp, enc);
! 227:
! 228: buf[block] = (char)((enc[0] ^ rndm[block - 2]) & 0xff);
! 229: buf[block + 1] = (char)((enc[1] ^ rndm[block - 1]) & 0xff);
! 230:
! 231: ivp = buf + 2;
! 232: for (i = 0; i < len; i += block) {
! 233: ret = crypto(key, ivp, enc);
! 234:
! 235: c = (len - i) / block ? block : len % block;
! 236:
! 237: for (j = 0; j < c; j++) {
! 238: buf[block + 2 + i + j] = (char)((data[i + j] ^ enc[j]) & 0xff);
! 239: }
! 240: ivp = buf + i;
! 241: }
! 242:
! 243: return (block + len + 2);
! 244: }
! 245:
! 246:
! 247:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>