1/*
2 * Copyright (c) Tim Hockin, Cobalt Networks Inc. and others
3 *
4 * crypto routines used by multiple c files
5 */
6#include <stdio.h>
7#include <ctype.h>
8#include <stdlib.h>
9#include <unistd.h>
10#include <string.h>
11#include <sys/param.h>
12#include "DESSupport.h"
13
14#ifndef USE_CRYPT
15#define DES_CBLOCK_SIZE		8
16
17static unsigned char odd_parity[256] = {
18    1,  1,  2,  2,  4,  4,  7,  7,  8,  8, 11, 11, 13, 13, 14, 14,
19    16, 16, 19, 19, 21, 21, 22, 22, 25, 25, 26, 26, 28, 28, 31, 31,
20    32, 32, 35, 35, 37, 37, 38, 38, 41, 41, 42, 42, 44, 44, 47, 47,
21    49, 49, 50, 50, 52, 52, 55, 55, 56, 56, 59, 59, 61, 61, 62, 62,
22    64, 64, 67, 67, 69, 69, 70, 70, 73, 73, 74, 74, 76, 76, 79, 79,
23    81, 81, 82, 82, 84, 84, 87, 87, 88, 88, 91, 91, 93, 93, 94, 94,
24    97, 97, 98, 98, 100, 100, 103, 103, 104, 104, 107, 107, 109, 109, 110, 110,
25    112, 112, 115, 115, 117, 117, 118, 118, 121, 121, 122, 122, 124, 124, 127, 127,
26    128, 128, 131, 131, 133, 133, 134, 134, 137, 137, 138, 138, 140, 140, 143, 143,
27    145, 145, 146, 146, 148, 148, 151, 151, 152, 152, 155, 155, 157, 157, 158, 158,
28    161, 161, 162, 162, 164, 164, 167, 167, 168, 168, 171, 171, 173, 173, 174, 174,
29    176, 176, 179, 179, 181, 181, 182, 182, 185, 185, 186, 186, 188, 188, 191, 191,
30    193, 193, 194, 194, 196, 196, 199, 199, 200, 200, 203, 203, 205, 205, 206, 206,
31    208, 208, 211, 211, 213, 213, 214, 214, 217, 217, 218, 218, 220, 220, 223, 223,
32    224, 224, 227, 227, 229, 229, 230, 230, 233, 233, 234, 234, 236, 236, 239, 239,
33    241, 241, 242, 242, 244, 244, 247, 247, 248, 248, 251, 251, 253, 253, 254, 254};
34
35static void
36set_odd_parity(unsigned char *key)
37{
38    int idx;
39    for (idx = 0; idx < DES_CBLOCK_SIZE; idx++)
40	key[idx] = odd_parity[key[idx]];
41}
42
43#include <CommonCrypto/CommonCryptor.h>
44#endif
45
46static u_char Get7Bits(const unsigned char *input, int startBit)
47{
48    register unsigned int       word;
49
50    word  = (unsigned)input[startBit / 8] << 8;
51    word |= (unsigned)input[startBit / 8 + 1];
52
53    word >>= 15 - (startBit % 8 + 7);
54
55    return word & 0xFE;
56}
57
58
59static void MakeKey(const unsigned char *key, unsigned char *des_key)
60{
61    des_key[0] = Get7Bits(key,  0);
62    des_key[1] = Get7Bits(key,  7);
63    des_key[2] = Get7Bits(key, 14);
64    des_key[3] = Get7Bits(key, 21);
65    des_key[4] = Get7Bits(key, 28);
66    des_key[5] = Get7Bits(key, 35);
67    des_key[6] = Get7Bits(key, 42);
68    des_key[7] = Get7Bits(key, 49);
69
70#ifndef USE_CRYPT
71    set_odd_parity(des_key);
72#endif
73}
74
75
76#ifdef USE_CRYPT
77/* in == 8-byte string (expanded version of the 56-bit key)
78 * out == 64-byte string where each byte is either 1 or 0
79 * Note that the low-order "bit" is always ignored by by setkey()
80 */
81static void Expand(unsigned char *in, unsigned char *out)
82{
83        int j, c;
84        int i;
85
86        for(i = 0; i < 64; in++){
87		c = *in;
88                for(j = 7; j >= 0; j--)
89                        *out++ = (c >> j) & 01;
90                i += 8;
91        }
92}
93
94/* The inverse of Expand
95 */
96static void Collapse(unsigned char *in, unsigned char *out)
97{
98        int j;
99        int i;
100	unsigned int c;
101
102	for (i = 0; i < 64; i += 8, out++) {
103	    c = 0;
104	    for (j = 7; j >= 0; j--, in++)
105		c |= *in << j;
106	    *out = c & 0xff;
107	}
108}
109
110__private_extern__ void
111DesEncrypt(const unsigned char *clear, const unsigned char *key,
112	   unsigned char *cipher)
113{
114    u_char des_key[8];
115    u_char crypt_key[66];
116    u_char des_input[66];
117
118    MakeKey(key, des_key);
119
120    Expand(des_key, crypt_key);
121    setkey(crypt_key);
122
123    Expand(clear, des_input);
124    encrypt(des_input, 0);
125    Collapse(des_input, cipher);
126}
127#else /* don't USE_CRYPT */
128__private_extern__ void
129DesEncrypt(const unsigned char * clear, const unsigned char * key,
130	   unsigned char * cipher)
131{
132    CCCryptorStatus	c_status;
133    u_char		des_key[DES_CBLOCK_SIZE];
134    size_t		output_bytes;
135
136    MakeKey(key, des_key);
137    c_status = CCCrypt(kCCEncrypt, kCCAlgorithmDES, 0,
138		       des_key, sizeof(des_key),
139		       NULL,
140		       clear, DES_CBLOCK_SIZE,
141		       cipher, DES_CBLOCK_SIZE, &output_bytes);
142    if (c_status != kCCSuccess) {
143	fprintf(stderr,
144		"DESEncrypt: CCCrypt failed with %d\n",
145		c_status);
146    }
147    return;
148}
149#endif /* USE_CRYPT */
150
151
152