1 #include <stdint.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <unsupported_api.h>
5
6 #include "crypt_des.h"
7
8 static struct expanded_key __encrypt_key;
9
setkey(const char * key)10 void setkey(const char *key)
11 {
12 unsigned char bkey[8];
13 int i, j;
14
15 unsupported_api(__FUNCTION__);
16 for (i = 0; i < 8; i++) {
17 bkey[i] = 0;
18 for (j = 7; j >= 0; j--, key++)
19 bkey[i] |= (uint32_t)(*key & 1) << j;
20 }
21
22 __des_setkey(bkey, &__encrypt_key);
23 }
24
encrypt(char * block,int edflag)25 void encrypt(char *block, int edflag)
26 {
27 struct expanded_key decrypt_key, *key;
28 uint32_t b[2];
29 int i, j;
30 char *p;
31
32 unsupported_api(__FUNCTION__);
33 p = block;
34 for (i = 0; i < 2; i++) {
35 b[i] = 0;
36 for (j = 31; j >= 0; j--, p++)
37 b[i] |= (uint32_t)(*p & 1) << j;
38 }
39
40 key = &__encrypt_key;
41 if (edflag) {
42 key = &decrypt_key;
43 for (i = 0; i < 16; i++) {
44 decrypt_key.l[i] = __encrypt_key.l[15-i];
45 decrypt_key.r[i] = __encrypt_key.r[15-i];
46 }
47 }
48
49 __do_des(b[0], b[1], b, b + 1, 1, 0, key);
50
51 p = block;
52 for (i = 0; i < 2; i++)
53 for (j = 31; j >= 0; j--)
54 *p++ = b[i]>>j & 1;
55 }
56