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