update copyright year
[oweals/busybox.git] / libbb / pw_encrypt_des.c
index a13e1a5dfbc60bb172c56b2d911d2e8584a1c05b..c8e02ddff82f2424309f0f0b491c441c23e61ebd 100644 (file)
@@ -201,7 +201,7 @@ static const uint32_t bits32[32] =
 static const uint8_t bits8[8] = { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 };
 
 
-static int 
+static int
 ascii_to_bin(char ch)
 {
        if (ch > 'z')
@@ -220,8 +220,8 @@ ascii_to_bin(char ch)
 }
 
 
-/* Static stuff that stays resident and doesn't change after 
- * being initialized, and therefore doesn't need to be made 
+/* Static stuff that stays resident and doesn't change after
+ * being initialized, and therefore doesn't need to be made
  * reentrant. */
 struct const_des_ctx {
 #if USE_ip_mask
@@ -696,13 +696,28 @@ do_des(struct des_ctx *ctx, /*uint32_t l_in, uint32_t r_in,*/ uint32_t *l_out, u
 
 #define DES_OUT_BUFSIZE 21
 
+static void
+to64_msb_first(char *s, unsigned v)
+{
+#if 0
+       *s++ = ascii64[(v >> 18) & 0x3f]; /* bits 23..18 */
+       *s++ = ascii64[(v >> 12) & 0x3f]; /* bits 17..12 */
+       *s++ = ascii64[(v >> 6) & 0x3f]; /* bits 11..6 */
+       *s   = ascii64[v & 0x3f]; /* bits 5..0 */
+#endif
+       *s++ = i64c(v >> 18); /* bits 23..18 */
+       *s++ = i64c(v >> 12); /* bits 17..12 */
+       *s++ = i64c(v >> 6); /* bits 11..6 */
+       *s   = i64c(v); /* bits 5..0 */
+}
+
 static char *
 NOINLINE
 des_crypt(struct des_ctx *ctx, char output[DES_OUT_BUFSIZE],
                const unsigned char *key, const unsigned char *setting)
 {
-       uint32_t salt, l, r0, r1, keybuf[2];
-       uint8_t *p, *q;
+       uint32_t salt, r0, r1, keybuf[2];
+       uint8_t *q;
 
        /*
         * Copy the key, shifting each character up by one bit
@@ -733,34 +748,41 @@ des_crypt(struct des_ctx *ctx, char output[DES_OUT_BUFSIZE],
         */
        output[1] = setting[1] ? setting[1] : output[0];
 
-       p = (uint8_t *)output + 2;
-
        setup_salt(ctx, salt);
-       /*
-        * Do it.
-        */
+       /* Do it. */
        do_des(ctx, /*0, 0,*/ &r0, &r1, 25 /* count */);
 
-       /*
-        * Now encode the result...
-        */
-       l = (r0 >> 8);
-       *p++ = ascii64[(l >> 18) & 0x3f];
-       *p++ = ascii64[(l >> 12) & 0x3f];
-       *p++ = ascii64[(l >> 6) & 0x3f];
-       *p++ = ascii64[l & 0x3f];
-
+       /* Now encode the result. */
+#if 0
+{
+       uint32_t l = (r0 >> 8);
+       q = (uint8_t *)output + 2;
+       *q++ = ascii64[(l >> 18) & 0x3f]; /* bits 31..26 of r0 */
+       *q++ = ascii64[(l >> 12) & 0x3f]; /* bits 25..20 of r0 */
+       *q++ = ascii64[(l >> 6) & 0x3f]; /* bits 19..14 of r0 */
+       *q++ = ascii64[l & 0x3f]; /* bits 13..8 of r0 */
        l = ((r0 << 16) | (r1 >> 16));
-       *p++ = ascii64[(l >> 18) & 0x3f];
-       *p++ = ascii64[(l >> 12) & 0x3f];
-       *p++ = ascii64[(l >> 6) & 0x3f];
-       *p++ = ascii64[l & 0x3f];
-
+       *q++ = ascii64[(l >> 18) & 0x3f]; /* bits 7..2 of r0 */
+       *q++ = ascii64[(l >> 12) & 0x3f]; /* bits 1..2 of r0 and 31..28 of r1 */
+       *q++ = ascii64[(l >> 6) & 0x3f]; /* bits 27..22 of r1 */
+       *q++ = ascii64[l & 0x3f]; /* bits 21..16 of r1 */
        l = r1 << 2;
-       *p++ = ascii64[(l >> 12) & 0x3f];
-       *p++ = ascii64[(l >> 6) & 0x3f];
-       *p++ = ascii64[l & 0x3f];
-       *p = 0;
+       *q++ = ascii64[(l >> 12) & 0x3f]; /* bits 15..10 of r1 */
+       *q++ = ascii64[(l >> 6) & 0x3f]; /* bits 9..4 of r1 */
+       *q++ = ascii64[l & 0x3f]; /* bits 3..0 of r1 + 00 */
+       *q = 0;
+}
+#else
+       /* Each call takes low-order 24 bits and stores 4 chars */
+       /* bits 31..8 of r0 */
+       to64_msb_first(output + 2, (r0 >> 8));
+       /* bits 7..0 of r0 and 31..16 of r1 */
+       to64_msb_first(output + 6, (r0 << 16) | (r1 >> 16));
+       /* bits 15..0 of r1 and two zero bits (plus extra zero byte) */
+       to64_msb_first(output + 10, (r1 << 8));
+       /* extra zero byte is encoded as '.', fixing it */
+       output[13] = '\0';
+#endif
 
        return output;
 }