diff options
author | Timo Teräs <timo.teras@iki.fi> | 2014-01-13 13:05:14 +0200 |
---|---|---|
committer | Rich Felker <dalias@aerifal.cx> | 2014-02-05 11:09:53 -0500 |
commit | 16a3580ef3a27d7f1fb0f76441a973b71a35b527 (patch) | |
tree | fc156afeb849a600d2ca27bfc71ff545752c27eb | |
parent | 73871ee3f21b13b88f2f20a39a1d9bc751bcc1df (diff) | |
download | musl-16a3580ef3a27d7f1fb0f76441a973b71a35b527.tar.gz |
add legacy functions setkey() and encrypt()
-rw-r--r-- | src/crypt/crypt_des.c | 12 | ||||
-rw-r--r-- | src/crypt/encrypt.c | 60 |
2 files changed, 66 insertions, 6 deletions
diff --git a/src/crypt/crypt_des.c b/src/crypt/crypt_des.c index dc95dcaa..d5766a7a 100644 --- a/src/crypt/crypt_des.c +++ b/src/crypt/crypt_des.c @@ -692,7 +692,7 @@ static uint32_t setup_salt(uint32_t salt) return saltbits; } -static void des_setkey(const unsigned char *key, struct expanded_key *ekey) +void __des_setkey(const unsigned char *key, struct expanded_key *ekey) { uint32_t k0, k1, rawkey0, rawkey1; unsigned int shifts, round, i, ibit; @@ -753,7 +753,7 @@ static void des_setkey(const unsigned char *key, struct expanded_key *ekey) /* * l_in, r_in, l_out, and r_out are in pseudo-"big-endian" format. */ -static void do_des(uint32_t l_in, uint32_t r_in, +void __do_des(uint32_t l_in, uint32_t r_in, uint32_t *l_out, uint32_t *r_out, uint32_t count, uint32_t saltbits, const struct expanded_key *ekey) { @@ -862,7 +862,7 @@ static void des_cipher(const unsigned char *in, unsigned char *out, ((uint32_t)in[5] << 16) | ((uint32_t)in[4] << 24); - do_des(rawl, rawr, &l_out, &r_out, count, saltbits, ekey); + __do_des(rawl, rawr, &l_out, &r_out, count, saltbits, ekey); out[0] = l_out >> 24; out[1] = l_out >> 16; @@ -894,7 +894,7 @@ static char *_crypt_extended_r_uut(const char *_key, const char *_setting, char if (*key) key++; } - des_setkey(keybuf, &ekey); + __des_setkey(keybuf, &ekey); if (*setting == _PASSWORD_EFMT1) { /* @@ -929,7 +929,7 @@ static char *_crypt_extended_r_uut(const char *_key, const char *_setting, char q = keybuf; while (q <= &keybuf[sizeof(keybuf) - 1] && *key) *q++ ^= *key++ << 1; - des_setkey(keybuf, &ekey); + __des_setkey(keybuf, &ekey); } memcpy(output, setting, 9); @@ -957,7 +957,7 @@ static char *_crypt_extended_r_uut(const char *_key, const char *_setting, char /* * Do it. */ - do_des(0, 0, &r0, &r1, count, setup_salt(salt), &ekey); + __do_des(0, 0, &r0, &r1, count, setup_salt(salt), &ekey); /* * Now encode the result... diff --git a/src/crypt/encrypt.c b/src/crypt/encrypt.c new file mode 100644 index 00000000..9332a6de --- /dev/null +++ b/src/crypt/encrypt.c @@ -0,0 +1,60 @@ +#include <stdint.h> +#include <stdlib.h> +#include <unistd.h> + +struct expanded_key { + uint32_t l[16], r[16]; +}; + +void __des_setkey(const unsigned char *key, struct expanded_key *ekey); +void __do_des(uint32_t l_in, uint32_t r_in, + uint32_t *l_out, uint32_t *r_out, + uint32_t count, uint32_t saltbits, const struct expanded_key *ekey); + + +static struct expanded_key __encrypt_key; + +void setkey(const char *key) +{ + unsigned char bkey[8]; + int i, j; + + for (i = 0; i < 8; i++) { + bkey[i] = 0; + for (j = 7; j >= 0; j--, key++) + bkey[i] |= (uint32_t)(*key & 1) << j; + } + + __des_setkey(bkey, &__encrypt_key); +} + +void encrypt(char *block, int edflag) +{ + struct expanded_key decrypt_key, *key; + uint32_t b[2]; + int i, j; + char *p; + + p = block; + for (i = 0; i < 2; i++) { + b[i] = 0; + for (j = 31; j >= 0; j--, p++) + b[i] |= (uint32_t)(*p & 1) << j; + } + + key = &__encrypt_key; + if (edflag) { + key = &decrypt_key; + for (i = 0; i < 16; i++) { + decrypt_key.l[i] = __encrypt_key.l[15-i]; + decrypt_key.r[i] = __encrypt_key.r[15-i]; + } + } + + __do_des(b[0], b[1], b, b + 1, 1, 0, key); + + p = block; + for (i = 0; i < 2; i++) + for (j = 31; j >= 0; j--) + *p++ = b[i]>>j & 1; +} |