145 /*
146 * A corrected version of des_setparity (see bug 1149767).
147 */
148 void
149 des_setparity_g(des_block *p)
150 {
151 int i;
152
153 for (i = 0; i < 8; i++) {
154 (*p).c[i] = partab_g[(*p).c[i]];
155 }
156 }
157
158 /*
159 * Software encrypt or decrypt a block of data (multiple of 8 bytes)
160 * Do the CBC ourselves if needed.
161 */
162 int
163 __des_crypt(char *buf, unsigned len, struct desparams *desp)
164 {
165 /* EXPORT DELETE START */
166 short i;
167 unsigned mode;
168 unsigned dir;
169 char nextiv[8];
170 struct deskeydata softkey;
171
172 mode = (unsigned)desp->des_mode;
173 dir = (unsigned)desp->des_dir;
174 (void) __des_setkey(desp->des_key, &softkey, dir);
175 while (len != 0) {
176 switch (mode) {
177 case CBC:
178 switch (dir) {
179 case ENCRYPT:
180 for (i = 0; i < 8; i++)
181 buf[i] ^= desp->des_ivec[i];
182 (void) __des_encrypt((uchar_t *)buf, &softkey);
183 for (i = 0; i < 8; i++)
184 desp->des_ivec[i] = buf[i];
185 break;
186 case DECRYPT:
187 for (i = 0; i < 8; i++)
188 nextiv[i] = buf[i];
189 (void) __des_encrypt((uchar_t *)buf, &softkey);
190 for (i = 0; i < 8; i++) {
191 buf[i] ^= desp->des_ivec[i];
192 desp->des_ivec[i] = nextiv[i];
193 }
194 break;
195 }
196 break;
197 case ECB:
198 (void) __des_encrypt((uchar_t *)buf, &softkey);
199 break;
200 }
201 buf += 8;
202 len -= 8;
203 }
204 /* EXPORT DELETE END */
205 return (1);
206 }
207
208
209 /*
210 * Set the key and direction for an encryption operation
211 * We build the 16 key entries here
212 */
213 static int
214 __des_setkey(uchar_t userkey[8], struct deskeydata *kd, unsigned dir)
215 {
216 /* EXPORT DELETE START */
217 int32_t C, D;
218 short i;
219
220 /*
221 * First, generate C and D by permuting
222 * the key. The low order bit of each
223 * 8-bit char is not used, so C and D are only 28
224 * bits apiece.
225 */
226 {
227 short bit;
228 const short *pcc = PC1_C, *pcd = PC1_D;
229
230 C = D = 0;
231 for (i = 0; i < 28; i++) {
232 C <<= 1;
233 D <<= 1;
234 bit = *pcc++;
235 if (btst(userkey, bit))
236 C |= 1;
276 case ENCRYPT:
277 c = &kd->keyval[i]; break;
278 case DECRYPT:
279 c = &kd->keyval[15 - i]; break;
280 }
281 c->long0 = 0;
282 c->long1 = 0;
283 bbit = (1 << 5) << 24;
284 for (j = 0; j < 4; j++) {
285 for (k = 0; k < 6; k++) {
286 if (C & (BIT28 >> PC2_C[bit]))
287 c->long0 |= bbit >> k;
288 if (D & (BIT28 >> PC2_D[bit]))
289 c->long1 |= bbit >> k;
290 bit++;
291 }
292 bbit >>= 8;
293 }
294
295 }
296 /* EXPORT DELETE END */
297 return (1);
298 }
299
300
301
302 /*
303 * Do an encryption operation
304 * Much pain is taken (with preprocessor) to avoid loops so the compiler
305 * can do address arithmetic instead of doing it at runtime.
306 * Note that the byte-to-chunk conversion is necessary to guarantee
307 * processor byte-order independence.
308 */
309 static int
310 __des_encrypt(uchar_t *data, struct deskeydata *kd)
311 {
312 /* EXPORT DELETE START */
313 chunk_t work1, work2;
314
315 /*
316 * Initial permutation
317 * and byte to chunk conversion
318 */
319 {
320 const uint32_t *lp;
321 uint32_t l0, l1, w;
322 short i, pbit;
323
324 work1.byte0 = data[0];
325 work1.byte1 = data[1];
326 work1.byte2 = data[2];
327 work1.byte3 = data[3];
328 work1.byte4 = data[4];
329 work1.byte5 = data[5];
330 work1.byte6 = data[6];
331 work1.byte7 = data[7];
332 l0 = l1 = 0;
459 if (w & *lp++) {
460 pbit = FPtab[i];
461 if (pbit < 32)
462 l0 |= longtab[pbit];
463 else
464 l1 |= longtab[pbit-32];
465 }
466 }
467 work2.long0 = l0;
468 work2.long1 = l1;
469 }
470 data[0] = work2.byte0;
471 data[1] = work2.byte1;
472 data[2] = work2.byte2;
473 data[3] = work2.byte3;
474 data[4] = work2.byte4;
475 data[5] = work2.byte5;
476 data[6] = work2.byte6;
477 data[7] = work2.byte7;
478
479 /* EXPORT DELETE END */
480 return (1);
481 }
|
145 /*
146 * A corrected version of des_setparity (see bug 1149767).
147 */
148 void
149 des_setparity_g(des_block *p)
150 {
151 int i;
152
153 for (i = 0; i < 8; i++) {
154 (*p).c[i] = partab_g[(*p).c[i]];
155 }
156 }
157
158 /*
159 * Software encrypt or decrypt a block of data (multiple of 8 bytes)
160 * Do the CBC ourselves if needed.
161 */
162 int
163 __des_crypt(char *buf, unsigned len, struct desparams *desp)
164 {
165 short i;
166 unsigned mode;
167 unsigned dir;
168 char nextiv[8];
169 struct deskeydata softkey;
170
171 mode = (unsigned)desp->des_mode;
172 dir = (unsigned)desp->des_dir;
173 (void) __des_setkey(desp->des_key, &softkey, dir);
174 while (len != 0) {
175 switch (mode) {
176 case CBC:
177 switch (dir) {
178 case ENCRYPT:
179 for (i = 0; i < 8; i++)
180 buf[i] ^= desp->des_ivec[i];
181 (void) __des_encrypt((uchar_t *)buf, &softkey);
182 for (i = 0; i < 8; i++)
183 desp->des_ivec[i] = buf[i];
184 break;
185 case DECRYPT:
186 for (i = 0; i < 8; i++)
187 nextiv[i] = buf[i];
188 (void) __des_encrypt((uchar_t *)buf, &softkey);
189 for (i = 0; i < 8; i++) {
190 buf[i] ^= desp->des_ivec[i];
191 desp->des_ivec[i] = nextiv[i];
192 }
193 break;
194 }
195 break;
196 case ECB:
197 (void) __des_encrypt((uchar_t *)buf, &softkey);
198 break;
199 }
200 buf += 8;
201 len -= 8;
202 }
203 return (1);
204 }
205
206
207 /*
208 * Set the key and direction for an encryption operation
209 * We build the 16 key entries here
210 */
211 static int
212 __des_setkey(uchar_t userkey[8], struct deskeydata *kd, unsigned dir)
213 {
214 int32_t C, D;
215 short i;
216
217 /*
218 * First, generate C and D by permuting
219 * the key. The low order bit of each
220 * 8-bit char is not used, so C and D are only 28
221 * bits apiece.
222 */
223 {
224 short bit;
225 const short *pcc = PC1_C, *pcd = PC1_D;
226
227 C = D = 0;
228 for (i = 0; i < 28; i++) {
229 C <<= 1;
230 D <<= 1;
231 bit = *pcc++;
232 if (btst(userkey, bit))
233 C |= 1;
273 case ENCRYPT:
274 c = &kd->keyval[i]; break;
275 case DECRYPT:
276 c = &kd->keyval[15 - i]; break;
277 }
278 c->long0 = 0;
279 c->long1 = 0;
280 bbit = (1 << 5) << 24;
281 for (j = 0; j < 4; j++) {
282 for (k = 0; k < 6; k++) {
283 if (C & (BIT28 >> PC2_C[bit]))
284 c->long0 |= bbit >> k;
285 if (D & (BIT28 >> PC2_D[bit]))
286 c->long1 |= bbit >> k;
287 bit++;
288 }
289 bbit >>= 8;
290 }
291
292 }
293 return (1);
294 }
295
296
297
298 /*
299 * Do an encryption operation
300 * Much pain is taken (with preprocessor) to avoid loops so the compiler
301 * can do address arithmetic instead of doing it at runtime.
302 * Note that the byte-to-chunk conversion is necessary to guarantee
303 * processor byte-order independence.
304 */
305 static int
306 __des_encrypt(uchar_t *data, struct deskeydata *kd)
307 {
308 chunk_t work1, work2;
309
310 /*
311 * Initial permutation
312 * and byte to chunk conversion
313 */
314 {
315 const uint32_t *lp;
316 uint32_t l0, l1, w;
317 short i, pbit;
318
319 work1.byte0 = data[0];
320 work1.byte1 = data[1];
321 work1.byte2 = data[2];
322 work1.byte3 = data[3];
323 work1.byte4 = data[4];
324 work1.byte5 = data[5];
325 work1.byte6 = data[6];
326 work1.byte7 = data[7];
327 l0 = l1 = 0;
454 if (w & *lp++) {
455 pbit = FPtab[i];
456 if (pbit < 32)
457 l0 |= longtab[pbit];
458 else
459 l1 |= longtab[pbit-32];
460 }
461 }
462 work2.long0 = l0;
463 work2.long1 = l1;
464 }
465 data[0] = work2.byte0;
466 data[1] = work2.byte1;
467 data[2] = work2.byte2;
468 data[3] = work2.byte3;
469 data[4] = work2.byte4;
470 data[5] = work2.byte5;
471 data[6] = work2.byte6;
472 data[7] = work2.byte7;
473
474 return (1);
475 }
|