Annotation of OpenXM/src/ox_ntl/crypt/cast5/cast5.c, Revision 1.1
1.1 ! iwane 1: /* $OpenXM$ */
! 2: /*
! 3: * rfc 2144
! 4: * The CAST-128 Encryption Algorithm
! 5: */
! 6:
! 7: #include <unistd.h>
! 8:
! 9: #include "cast5.h"
! 10: #include "cast5tbl.h"
! 11:
! 12:
! 13: #if HAVE_CONFIG_H
! 14: #include "config.h"
! 15: #endif /* HAVE_CONFIG_H */
! 16:
! 17: #define leftrot32(x, n) ((((uint32_t)x) << n) | ((uint32_t)x) >> (32 - n))
! 18:
! 19: #ifdef WORDS_BIGENDIAN
! 20: #define ARY(x, i) (((unsigned char *)(x))[i])
! 21: #else
! 22: #define ARY(x, i) (((unsigned char *)(x))[(int)("\x03\x02\x01\x00\x07\x06\x05\x04\x0b\x0a\x09\x08\x0f\x0e\x0d\x0c"[i])])
! 23: #endif
! 24:
! 25:
! 26: /*
! 27: #define ARY(x, i) (((unsigned char *)(x))[((((i) / 4) * 4) + (3 - (i) % 4))])
! 28: * compute: 7.473
! 29: * 15.100
! 30: * 11.805
! 31: *
! 32: #define ARY(x, i) (((unsigned char *)(x))[(int)("\x03\x02\x01\x00\x07\x06\x05\x04\x0b\x0a\x09\x08\x0f\x0e\x0d\x0c"[i])])
! 33: * char array: 7.044
! 34: * 14.124
! 35: * 11.366
! 36: *
! 37: static const int _ar[] = {3, 2, 1, 0, 7, 6, 5, 4, 11, 10, 9, 8, 15, 14, 13, 12};
! 38: #define ARY(x, i) (((unsigned char *)(x))[_ar[i]])
! 39: * int array: 8.679
! 40: * 17.233
! 41: * 12.705
! 42: */
! 43:
! 44:
! 45:
! 46: /*
! 47: * length of key is 16 byte = 128 bit.
! 48: */
! 49: void
! 50: cast128_keyset(const unsigned char *key, int len, cast128_key *ckey)
! 51: {
! 52: #define X(i) (ARY(&x, i))
! 53: #define Z(i) (ARY(&z, i))
! 54: uint32_t x[4], z[4];
! 55: int i;
! 56: uint32_t K[32], *k;
! 57:
! 58: if (len > 16)
! 59: len = 16;
! 60: for (i = 0; i < len; i++) {
! 61: X(i) = key[i];
! 62: }
! 63:
! 64: for (; i < (int)sizeof(x); i++) {
! 65: X(i) = 0;
! 66: }
! 67:
! 68: k = K;
! 69:
! 70: for (i = 0; i < 2; i++) {
! 71: #define s5 __cast128_tbl_s5
! 72: #define s6 __cast128_tbl_s6
! 73: #define s7 __cast128_tbl_s7
! 74: #define s8 __cast128_tbl_s8
! 75: z[0] = x[0] ^ s5[X(0xD)] ^ s6[X(0xF)] ^ s7[X(0xC)] ^ s8[X(0xE)] ^ s7[X(0x8)];
! 76: z[1] = x[2] ^ s5[Z(0x0)] ^ s6[Z(0x2)] ^ s7[Z(0x1)] ^ s8[Z(0x3)] ^ s8[X(0xA)];
! 77: z[2] = x[3] ^ s5[Z(0x7)] ^ s6[Z(0x6)] ^ s7[Z(0x5)] ^ s8[Z(0x4)] ^ s5[X(0x9)];
! 78: z[3] = x[1] ^ s5[Z(0xA)] ^ s6[Z(0x9)] ^ s7[Z(0xB)] ^ s8[Z(0x8)] ^ s6[X(0xB)];
! 79:
! 80: k[ 0] = s5[Z(0x8)] ^ s6[Z(0x9)] ^ s7[Z(0x7)] ^ s8[Z(0x6)] ^ s5[Z(0x2)];
! 81: k[ 1] = s5[Z(0xA)] ^ s6[Z(0xB)] ^ s7[Z(0x5)] ^ s8[Z(0x4)] ^ s6[Z(0x6)];
! 82: k[ 2] = s5[Z(0xC)] ^ s6[Z(0xD)] ^ s7[Z(0x3)] ^ s8[Z(0x2)] ^ s7[Z(0x9)];
! 83: k[ 3] = s5[Z(0xE)] ^ s6[Z(0xF)] ^ s7[Z(0x1)] ^ s8[Z(0x0)] ^ s8[Z(0xC)];
! 84:
! 85: x[0] = z[2] ^ s5[Z(0x5)] ^ s6[Z(0x7)] ^ s7[Z(0x4)] ^ s8[Z(0x6)] ^ s7[Z(0x0)];
! 86: x[1] = z[0] ^ s5[X(0x0)] ^ s6[X(0x2)] ^ s7[X(0x1)] ^ s8[X(0x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^ s8[Z(0x9)] ^ s7[Z(0x2)];
! 103: k[11] = s5[Z(0x5)] ^ s6[Z(0x4)] ^ s7[Z(0xA)] ^ s8[Z(0xB)] ^ s8[Z(0x6)];
! 104:
! 105: x[0] = z[2] ^ s5[Z(0x5)] ^ s6[Z(0x7)] ^ s7[Z(0x4)] ^ s8[Z(0x6)] ^ s7[Z(0x0)];
! 106: x[1] = z[0] ^ s5[X(0x0)] ^ s6[X(0x2)] ^ s7[X(0x1)] ^ s8[X(0x3)] ^ s8[Z(0x2)];
! 107: x[2] = z[1] ^ s5[X(0x7)] ^ s6[X(0x6)] ^ s7[X(0x5)] ^ s8[X(0x4)] ^ s5[Z(0x1)];
! 108: x[3] = z[3] ^ s5[X(0xA)] ^ s6[X(0x9)] ^ s7[X(0xB)] ^ s8[X(0x8)] ^ s6[Z(0x3)];
! 109:
! 110: k[12] = s5[X(0x8)] ^ s6[X(0x9)] ^ s7[X(0x7)] ^ s8[X(0x6)] ^ s5[X(0x3)];
! 111: k[13] = s5[X(0xA)] ^ s6[X(0xB)] ^ s7[X(0x5)] ^ s8[X(0x4)] ^ s6[X(0x7)];
! 112: k[14] = s5[X(0xC)] ^ s6[X(0xD)] ^ s7[X(0x3)] ^ s8[X(0x2)] ^ s7[X(0x8)];
! 113: k[15] = s5[X(0xE)] ^ s6[X(0xF)] ^ s7[X(0x1)] ^ s8[X(0x0)] ^ s8[X(0xD)];
! 114:
! 115: #undef s5
! 116: #undef s6
! 117: #undef s7
! 118: #undef s8
! 119: k += 16;
! 120: }
! 121:
! 122: for (i = 0; i < 16; i++) {
! 123: ckey->km[i] = K[i];
! 124: ckey->kr[i] = K[i + 16] & 0x1f;
! 125: }
! 126: ckey->len = len;
! 127: if (len <= 10) /* < 80 bit */
! 128: ckey->loop = 12;
! 129: else
! 130: ckey->loop = 16;
! 131:
! 132: #undef X
! 133: #undef Z
! 134:
! 135: }
! 136:
! 137:
! 138: #define I(i) (ARY(&j, i))
! 139:
! 140: #define _CAST_TYPE(FUNC, OP1, OP2, OP3) \
! 141: static inline uint32_t \
! 142: FUNC(uint32_t d, uint32_t km, uint32_t kr) \
! 143: { \
! 144: uint32_t j = leftrot32(km OP3 d, kr); \
! 145: return (((__cast128_tbl_s1[I(0)] OP1 __cast128_tbl_s2[I(1)]) OP2 __cast128_tbl_s3[I(2)]) OP3 __cast128_tbl_s4[I(3)]); \
! 146: }
! 147:
! 148: _CAST_TYPE(_type1, ^, -, +)
! 149: _CAST_TYPE(_type2, -, +, ^)
! 150: _CAST_TYPE(_type3, +, ^, -)
! 151:
! 152: #undef I
! 153:
! 154:
! 155: void
! 156: cast128_enc_i(cast128_key *key,
! 157: uint32_t ml, uint32_t mr,
! 158: uint32_t *el, uint32_t *er)
! 159: {
! 160: int i;
! 161:
! 162: uint32_t l;
! 163: uint32_t (*f[3])(uint32_t, uint32_t, uint32_t) = {_type1, _type2, _type3};
! 164:
! 165: for (i = 0; i < key->loop; i++) {
! 166: l = mr;
! 167: mr = ml ^ f[i % 3](mr, key->km[i], key->kr[i]);
! 168: ml = l;
! 169: }
! 170:
! 171: *er = ml;
! 172: *el = mr;
! 173: }
! 174:
! 175:
! 176:
! 177: void
! 178: cast128_enc_c(cast128_key *key,
! 179: const unsigned char *msg, unsigned char *enc)
! 180: {
! 181: uint32_t m[2], e[2];
! 182: int i;
! 183:
! 184: for (i = 0; i < 8; i++) {
! 185: ARY(m, i) = msg[i];
! 186: }
! 187:
! 188: cast128_enc_i(key, m[0], m[1], &e[0], &e[1]);
! 189:
! 190: for (i = 0; i < 8; i++) {
! 191: enc[i] = ARY(e, i);
! 192: }
! 193: }
! 194:
! 195:
! 196:
! 197: void
! 198: cast128_dec_i(cast128_key *key,
! 199: uint32_t ml, uint32_t mr,
! 200: uint32_t *el, uint32_t *er)
! 201: {
! 202: int i;
! 203:
! 204: uint32_t l;
! 205: uint32_t (*f[3])(uint32_t, uint32_t, uint32_t) = {_type1, _type2, _type3};
! 206:
! 207: for (i = key->loop - 1; i >= 0; i--) {
! 208: l = mr;
! 209: mr = ml ^ f[i % 3](mr, key->km[i], key->kr[i]);
! 210: ml = l;
! 211: }
! 212:
! 213: *er = ml;
! 214: *el = mr;
! 215: }
! 216:
! 217:
! 218: void
! 219: cast128_dec_c(cast128_key *key,
! 220: const unsigned char *msg, unsigned char *enc)
! 221: {
! 222: uint32_t m[2], e[2];
! 223: int i;
! 224:
! 225: for (i = 0; i < 8; i++) {
! 226: ARY(m, i) = msg[i];
! 227: }
! 228:
! 229: cast128_dec_i(key, m[0], m[1], &e[0], &e[1]);
! 230:
! 231: for (i = 0; i < 8; i++) {
! 232: enc[i] = ARY(e, i);
! 233: }
! 234: }
! 235:
! 236:
! 237:
! 238:
! 239:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>