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