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

Annotation of OpenXM/src/ox_ntl/crypt/des/des3.c, Revision 1.3

1.3     ! iwane       1: /* $OpenXM: OpenXM/src/ox_ntl/crypt/des/des3.c,v 1.2 2005/06/19 15:45:38 iwane Exp $ */
1.1       iwane       2: /*
                      3:  * Triple-DES
                      4:  *   see des.c
                      5:  */
                      6:
                      7: #include <stdio.h>
                      8:
                      9: #include "des3.h"
                     10: #include "block.h"
                     11:
                     12: #ifdef HAVE_CONFIG_H
                     13: #include "config.h"
                     14: #endif
                     15:
                     16: #define BLOCK 8
                     17:
1.3     ! iwane      18: #define PRT(x) printf("%5s: %02x%02x%02x%02x %02x%02x%02x%02x\n", #x, x[0], x[1], x[2], x[3], x[4], x[5], x[6], x[7])
1.1       iwane      19:
                     20: /*===========================================================*
                     21:  * KEY STRUCT
                     22:  *===========================================================*/
                     23:
                     24: int
                     25: des3_keychk(des3_key *key)
                     26: {
                     27:        int ret = 1;
                     28:        int i;
                     29:
                     30:        for (i = 0; i < 3; i++) {
                     31:                ret = (ret && des_keychk(key->key + i));
                     32:        }
                     33:
                     34:        return (ret);
                     35: }
                     36:
                     37: void
                     38: des3_keyset(const unsigned char *key, des3_key *dkey)
                     39: {
                     40:        int i;
                     41:
                     42:        for (i = 0; i < 3; i++) {
                     43:                des_keyset(key + 8 * i, dkey->key + i);
                     44:        }
                     45: }
                     46:
                     47:
                     48:
                     49:
                     50: /*===========================================================*
                     51:  * BASE CRYPTO
                     52:  *===========================================================*/
                     53: /**
                     54:  * input: data: 8 byte
                     55:  */
                     56: int
                     57: des3_enc_c(const des3_key *key, const unsigned char *data, unsigned char *enc)
                     58: {
                     59:        unsigned char b1[8];
                     60:        unsigned char b2[8];
                     61:
                     62:        des_enc_c(key->key + 0, data, b1);
                     63:        des_dec_c(key->key + 1, b1, b2);
                     64:        des_enc_c(key->key + 2, b2, enc);
                     65:
                     66:        return (0);
                     67: }
                     68:
                     69:
                     70: /**
                     71:  * input: data: 8 byte
                     72:  */
                     73: int
                     74: des3_dec_c(const des3_key *key, const unsigned char *enc, unsigned char *data)
                     75: {
                     76:        unsigned char b1[8];
                     77:        unsigned char b2[8];
                     78:
1.3     ! iwane      79:        des_dec_c(key->key + 2, enc, b1);
1.1       iwane      80:        des_enc_c(key->key + 1, b1, b2);
1.3     ! iwane      81:        des_dec_c(key->key + 0, b2, data);
1.1       iwane      82:
                     83:        return (0);
                     84: }
                     85:
                     86: /*===========================================================*
                     87:  * CRYPTO: length of input data is "64 * n" byte
                     88:  *===========================================================*/
                     89: int
                     90: des3_enc_ecb(const des3_key *key, int len, const unsigned char *data, unsigned char *buf)
                     91: {
                     92:        int ret;
                     93:
                     94:        ret = crypt_ecb((void *)key, BLOCK, len, data, buf, (void *)des3_enc_c);
                     95:
                     96:        return (ret);
                     97: }
                     98:
                     99:
                    100: int
                    101: des3_dec_ecb(const des3_key *key, int len, const unsigned char *enc, unsigned char *data)
                    102: {
                    103:        int ret;
                    104:
                    105:        ret = crypt_ecb((void *)key, BLOCK, len, enc, data, (void *)des3_dec_c);
                    106:
                    107:        return (ret);
                    108: }
                    109:
                    110:
                    111:
                    112: int
1.2       iwane     113: des3_enc_cbc(const des3_key *key, unsigned char *iv, int len,
1.1       iwane     114:     const unsigned char *data, unsigned char *buf)
                    115: {
                    116:        int ret;
                    117:
                    118:        ret = crypt_enc_cbc((void *)key, BLOCK, iv, len, data, buf, (void *)des3_enc_c);
                    119:
                    120:        return (ret);
                    121: }
                    122:
                    123:
                    124: int
                    125: des3_dec_cbc(const des3_key *key, unsigned char *iv, int len,
                    126:     const unsigned char *data, unsigned char *buf)
                    127: {
                    128:        int ret;
                    129:
                    130:        ret = crypt_dec_cbc((void *)key, BLOCK, iv, len, data, buf, (void *)des3_dec_c);
                    131:
                    132:        return (ret);
                    133: }
                    134:
                    135:
                    136: int
                    137: des3_enc_cfb(const des3_key *key, int bit,
                    138:     const unsigned char *iv, int len,
                    139:     const unsigned char *data, unsigned char *buf)
                    140: {
                    141:        int ret;
                    142:
                    143:        ret = crypt_cfb((void *)key, 0, bit, BLOCK, iv, len, data, buf, (void *)des3_enc_c);
                    144:
                    145:        return (ret);
                    146: }
                    147:
                    148: int
                    149: des3_dec_cfb(const des3_key *key, int bit,
                    150:     const unsigned char *iv, int len,
                    151:     const unsigned char *data, unsigned char *buf)
                    152: {
                    153:        int ret;
                    154:
                    155:        ret = crypt_cfb((void *)key, 1, bit, BLOCK, iv, len, data, buf, (void *)des3_enc_c);
                    156:
                    157:        return (ret);
                    158: }
                    159:
                    160:
                    161: int
                    162: des3_ofb(const des3_key *key, int bit,
                    163:     const unsigned char *iv, int len,
                    164:     const unsigned char *data,
                    165:     unsigned char *buf)
                    166: {
                    167:        int ret;
                    168:        ret = crypt_ofb((void *)key, bit, BLOCK, iv, len, data, buf, (void *)des3_enc_c);
                    169:
                    170:        return (ret);
                    171: }
                    172:
                    173:

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