Unify GET_32BIT()/PUT_32BIT() et al from numerous source files into misc.h.
[u/mdw/putty] / sshdes.c
index 44fbf00..506e245 100644 (file)
--- a/sshdes.c
+++ b/sshdes.c
 
 typedef struct {
     word32 k0246[16], k1357[16];
-    word32 eiv0, eiv1;
-    word32 div0, div1;
+    word32 iv0, iv1;
 } DESContext;
 
 #define rotl(x, c) ( (x << c) | (x >> (32-c)) )
@@ -297,7 +296,7 @@ static word32 bitsel(word32 * input, const int *bitnums, int size)
     return ret;
 }
 
-void des_key_setup(word32 key_msw, word32 key_lsw, DESContext * sched)
+static void des_key_setup(word32 key_msw, word32 key_lsw, DESContext * sched)
 {
 
     static const int PC1_Cbits[] = {
@@ -346,8 +345,7 @@ void des_key_setup(word32 key_msw, word32 key_lsw, DESContext * sched)
        sched->k1357[i] = bitsel(buf, PC2_1357, 32);
     }
 
-    sched->eiv0 = sched->eiv1 = 0;
-    sched->div0 = sched->div1 = 0;     /* for good measure */
+    sched->iv0 = sched->iv1 = 0;
 }
 
 static const word32 SPboxes[8][64] = {
@@ -522,7 +520,8 @@ static const word32 SPboxes[8][64] = {
     bitswap(R, L, 16, 0x0000FFFF), \
     bitswap(R, L,  4, 0x0F0F0F0F))
 
-void des_encipher(word32 * output, word32 L, word32 R, DESContext * sched)
+static void des_encipher(word32 * output, word32 L, word32 R,
+                        DESContext * sched)
 {
     word32 swap, s0246, s1357;
 
@@ -561,7 +560,8 @@ void des_encipher(word32 * output, word32 L, word32 R, DESContext * sched)
     output[1] = R;
 }
 
-void des_decipher(word32 * output, word32 L, word32 R, DESContext * sched)
+static void des_decipher(word32 * output, word32 L, word32 R,
+                        DESContext * sched)
 {
     word32 swap, s0246, s1357;
 
@@ -600,18 +600,6 @@ void des_decipher(word32 * output, word32 L, word32 R, DESContext * sched)
     output[1] = R;
 }
 
-#define GET_32BIT_MSB_FIRST(cp) \
-  (((unsigned long)(unsigned char)(cp)[3]) | \
-  ((unsigned long)(unsigned char)(cp)[2] << 8) | \
-  ((unsigned long)(unsigned char)(cp)[1] << 16) | \
-  ((unsigned long)(unsigned char)(cp)[0] << 24))
-
-#define PUT_32BIT_MSB_FIRST(cp, value) do { \
-  (cp)[3] = (value); \
-  (cp)[2] = (value) >> 8; \
-  (cp)[1] = (value) >> 16; \
-  (cp)[0] = (value) >> 24; } while (0)
-
 static void des_cbc_encrypt(unsigned char *dest, const unsigned char *src,
                            unsigned int len, DESContext * sched)
 {
@@ -620,8 +608,8 @@ static void des_cbc_encrypt(unsigned char *dest, const unsigned char *src,
 
     assert((len & 7) == 0);
 
-    iv0 = sched->eiv0;
-    iv1 = sched->eiv1;
+    iv0 = sched->iv0;
+    iv1 = sched->iv1;
     for (i = 0; i < len; i += 8) {
        iv0 ^= GET_32BIT_MSB_FIRST(src);
        src += 4;
@@ -635,8 +623,8 @@ static void des_cbc_encrypt(unsigned char *dest, const unsigned char *src,
        PUT_32BIT_MSB_FIRST(dest, iv1);
        dest += 4;
     }
-    sched->eiv0 = iv0;
-    sched->eiv1 = iv1;
+    sched->iv0 = iv0;
+    sched->iv1 = iv1;
 }
 
 static void des_cbc_decrypt(unsigned char *dest, const unsigned char *src,
@@ -647,8 +635,8 @@ static void des_cbc_decrypt(unsigned char *dest, const unsigned char *src,
 
     assert((len & 7) == 0);
 
-    iv0 = sched->div0;
-    iv1 = sched->div1;
+    iv0 = sched->iv0;
+    iv1 = sched->iv1;
     for (i = 0; i < len; i += 8) {
        xL = GET_32BIT_MSB_FIRST(src);
        src += 4;
@@ -664,8 +652,8 @@ static void des_cbc_decrypt(unsigned char *dest, const unsigned char *src,
        iv0 = xL;
        iv1 = xR;
     }
-    sched->div0 = iv0;
-    sched->div1 = iv1;
+    sched->iv0 = iv0;
+    sched->iv1 = iv1;
 }
 
 static void des_3cbc_encrypt(unsigned char *dest, const unsigned char *src,
@@ -684,8 +672,8 @@ static void des_cbc3_encrypt(unsigned char *dest, const unsigned char *src,
 
     assert((len & 7) == 0);
 
-    iv0 = scheds->eiv0;
-    iv1 = scheds->eiv1;
+    iv0 = scheds->iv0;
+    iv1 = scheds->iv1;
     for (i = 0; i < len; i += 8) {
        iv0 ^= GET_32BIT_MSB_FIRST(src);
        src += 4;
@@ -701,8 +689,8 @@ static void des_cbc3_encrypt(unsigned char *dest, const unsigned char *src,
        PUT_32BIT_MSB_FIRST(dest, iv1);
        dest += 4;
     }
-    scheds->eiv0 = iv0;
-    scheds->eiv1 = iv1;
+    scheds->iv0 = iv0;
+    scheds->iv1 = iv1;
 }
 
 static void des_3cbc_decrypt(unsigned char *dest, const unsigned char *src,
@@ -721,8 +709,8 @@ static void des_cbc3_decrypt(unsigned char *dest, const unsigned char *src,
 
     assert((len & 7) == 0);
 
-    iv0 = scheds->div0;
-    iv1 = scheds->div1;
+    iv0 = scheds->iv0;
+    iv1 = scheds->iv1;
     for (i = 0; i < len; i += 8) {
        xL = GET_32BIT_MSB_FIRST(src);
        src += 4;
@@ -740,70 +728,138 @@ static void des_cbc3_decrypt(unsigned char *dest, const unsigned char *src,
        iv0 = xL;
        iv1 = xR;
     }
-    scheds->div0 = iv0;
-    scheds->div1 = iv1;
+    scheds->iv0 = iv0;
+    scheds->iv1 = iv1;
 }
 
-static DESContext cskeys[3], sckeys[3];
+static void des_sdctr3(unsigned char *dest, const unsigned char *src,
+                            unsigned int len, DESContext * scheds)
+{
+    word32 b[2], iv0, iv1, tmp;
+    unsigned int i;
 
-static void des3_cskey(unsigned char *key)
+    assert((len & 7) == 0);
+
+    iv0 = scheds->iv0;
+    iv1 = scheds->iv1;
+    for (i = 0; i < len; i += 8) {
+       des_encipher(b, iv0, iv1, &scheds[2]);
+       des_decipher(b, b[0], b[1], &scheds[1]);
+       des_encipher(b, b[0], b[1], &scheds[0]);
+       tmp = GET_32BIT_MSB_FIRST(src);
+       PUT_32BIT_MSB_FIRST(dest, tmp ^ b[0]);
+       src += 4;
+       dest += 4;
+       tmp = GET_32BIT_MSB_FIRST(src);
+       PUT_32BIT_MSB_FIRST(dest, tmp ^ b[0]);
+       src += 4;
+       dest += 4;
+       if ((iv0 = (iv0 + 1) & 0xffffffff) == 0)
+           iv1 = (iv1 + 1) & 0xffffffff;
+    }
+    scheds->iv0 = iv0;
+    scheds->iv1 = iv1;
+}
+
+static void *des3_make_context(void)
 {
-    des_key_setup(GET_32BIT_MSB_FIRST(key),
-                 GET_32BIT_MSB_FIRST(key + 4), &cskeys[0]);
-    des_key_setup(GET_32BIT_MSB_FIRST(key + 8),
-                 GET_32BIT_MSB_FIRST(key + 12), &cskeys[1]);
-    des_key_setup(GET_32BIT_MSB_FIRST(key + 16),
-                 GET_32BIT_MSB_FIRST(key + 20), &cskeys[2]);
-    logevent("Initialised triple-DES client->server encryption");
+    return snewn(3, DESContext);
+}
+
+static void *des3_ssh1_make_context(void)
+{
+    /* Need 3 keys for each direction, in SSH-1 */
+    return snewn(6, DESContext);
 }
 
-static void des3_csiv(unsigned char *key)
+static void *des_make_context(void)
 {
-    cskeys[0].eiv0 = GET_32BIT_MSB_FIRST(key);
-    cskeys[0].eiv1 = GET_32BIT_MSB_FIRST(key + 4);
+    return snew(DESContext);
 }
 
-static void des3_sciv(unsigned char *key)
+static void *des_ssh1_make_context(void)
 {
-    sckeys[0].div0 = GET_32BIT_MSB_FIRST(key);
-    sckeys[0].div1 = GET_32BIT_MSB_FIRST(key + 4);
+    /* Need one key for each direction, in SSH-1 */
+    return snewn(2, DESContext);
 }
 
-static void des3_sckey(unsigned char *key)
+static void des3_free_context(void *handle)   /* used for both 3DES and DES */
 {
+    sfree(handle);
+}
+
+static void des3_key(void *handle, unsigned char *key)
+{
+    DESContext *keys = (DESContext *) handle;
     des_key_setup(GET_32BIT_MSB_FIRST(key),
-                 GET_32BIT_MSB_FIRST(key + 4), &sckeys[0]);
+                 GET_32BIT_MSB_FIRST(key + 4), &keys[0]);
     des_key_setup(GET_32BIT_MSB_FIRST(key + 8),
-                 GET_32BIT_MSB_FIRST(key + 12), &sckeys[1]);
+                 GET_32BIT_MSB_FIRST(key + 12), &keys[1]);
     des_key_setup(GET_32BIT_MSB_FIRST(key + 16),
-                 GET_32BIT_MSB_FIRST(key + 20), &sckeys[2]);
-    logevent("Initialised triple-DES server->client encryption");
+                 GET_32BIT_MSB_FIRST(key + 20), &keys[2]);
 }
 
-static void des3_sesskey(unsigned char *key)
+static void des3_iv(void *handle, unsigned char *key)
 {
-    des3_cskey(key);
-    des3_sckey(key);
+    DESContext *keys = (DESContext *) handle;
+    keys[0].iv0 = GET_32BIT_MSB_FIRST(key);
+    keys[0].iv1 = GET_32BIT_MSB_FIRST(key + 4);
 }
 
-static void des3_encrypt_blk(unsigned char *blk, int len)
+static void des_key(void *handle, unsigned char *key)
 {
-    des_3cbc_encrypt(blk, blk, len, cskeys);
+    DESContext *keys = (DESContext *) handle;
+    des_key_setup(GET_32BIT_MSB_FIRST(key),
+                 GET_32BIT_MSB_FIRST(key + 4), &keys[0]);
 }
 
-static void des3_decrypt_blk(unsigned char *blk, int len)
+static void des3_sesskey(void *handle, unsigned char *key)
 {
-    des_3cbc_decrypt(blk, blk, len, sckeys);
+    DESContext *keys = (DESContext *) handle;
+    des3_key(keys, key);
+    des3_key(keys+3, key);
 }
 
-static void des3_ssh2_encrypt_blk(unsigned char *blk, int len)
+static void des3_encrypt_blk(void *handle, unsigned char *blk, int len)
 {
-    des_cbc3_encrypt(blk, blk, len, cskeys);
+    DESContext *keys = (DESContext *) handle;
+    des_3cbc_encrypt(blk, blk, len, keys);
 }
 
-static void des3_ssh2_decrypt_blk(unsigned char *blk, int len)
+static void des3_decrypt_blk(void *handle, unsigned char *blk, int len)
 {
-    des_cbc3_decrypt(blk, blk, len, sckeys);
+    DESContext *keys = (DESContext *) handle;
+    des_3cbc_decrypt(blk, blk, len, keys+3);
+}
+
+static void des3_ssh2_encrypt_blk(void *handle, unsigned char *blk, int len)
+{
+    DESContext *keys = (DESContext *) handle;
+    des_cbc3_encrypt(blk, blk, len, keys);
+}
+
+static void des3_ssh2_decrypt_blk(void *handle, unsigned char *blk, int len)
+{
+    DESContext *keys = (DESContext *) handle;
+    des_cbc3_decrypt(blk, blk, len, keys);
+}
+
+static void des3_ssh2_sdctr(void *handle, unsigned char *blk, int len)
+{
+    DESContext *keys = (DESContext *) handle;
+    des_sdctr3(blk, blk, len, keys);
+}
+
+static void des_ssh2_encrypt_blk(void *handle, unsigned char *blk, int len)
+{
+    DESContext *keys = (DESContext *) handle;
+    des_cbc_encrypt(blk, blk, len, keys);
+}
+
+static void des_ssh2_decrypt_blk(void *handle, unsigned char *blk, int len)
+{
+    DESContext *keys = (DESContext *) handle;
+    des_cbc_decrypt(blk, blk, len, keys);
 }
 
 void des3_decrypt_pubkey(unsigned char *key, unsigned char *blk, int len)
@@ -816,6 +872,7 @@ void des3_decrypt_pubkey(unsigned char *key, unsigned char *blk, int len)
     des_key_setup(GET_32BIT_MSB_FIRST(key),
                  GET_32BIT_MSB_FIRST(key + 4), &ourkeys[2]);
     des_3cbc_decrypt(blk, blk, len, ourkeys);
+    memset(ourkeys, 0, sizeof(ourkeys));
 }
 
 void des3_encrypt_pubkey(unsigned char *key, unsigned char *blk, int len)
@@ -828,18 +885,122 @@ void des3_encrypt_pubkey(unsigned char *key, unsigned char *blk, int len)
     des_key_setup(GET_32BIT_MSB_FIRST(key),
                  GET_32BIT_MSB_FIRST(key + 4), &ourkeys[2]);
     des_3cbc_encrypt(blk, blk, len, ourkeys);
+    memset(ourkeys, 0, sizeof(ourkeys));
+}
+
+void des3_decrypt_pubkey_ossh(unsigned char *key, unsigned char *iv,
+                             unsigned char *blk, int len)
+{
+    DESContext ourkeys[3];
+    des_key_setup(GET_32BIT_MSB_FIRST(key),
+                 GET_32BIT_MSB_FIRST(key + 4), &ourkeys[0]);
+    des_key_setup(GET_32BIT_MSB_FIRST(key + 8),
+                 GET_32BIT_MSB_FIRST(key + 12), &ourkeys[1]);
+    des_key_setup(GET_32BIT_MSB_FIRST(key + 16),
+                 GET_32BIT_MSB_FIRST(key + 20), &ourkeys[2]);
+    ourkeys[0].iv0 = GET_32BIT_MSB_FIRST(iv);
+    ourkeys[0].iv1 = GET_32BIT_MSB_FIRST(iv+4);
+    des_cbc3_decrypt(blk, blk, len, ourkeys);
+    memset(ourkeys, 0, sizeof(ourkeys));
+}
+
+void des3_encrypt_pubkey_ossh(unsigned char *key, unsigned char *iv,
+                             unsigned char *blk, int len)
+{
+    DESContext ourkeys[3];
+    des_key_setup(GET_32BIT_MSB_FIRST(key),
+                 GET_32BIT_MSB_FIRST(key + 4), &ourkeys[0]);
+    des_key_setup(GET_32BIT_MSB_FIRST(key + 8),
+                 GET_32BIT_MSB_FIRST(key + 12), &ourkeys[1]);
+    des_key_setup(GET_32BIT_MSB_FIRST(key + 16),
+                 GET_32BIT_MSB_FIRST(key + 20), &ourkeys[2]);
+    ourkeys[0].iv0 = GET_32BIT_MSB_FIRST(iv);
+    ourkeys[0].iv1 = GET_32BIT_MSB_FIRST(iv+4);
+    des_cbc3_encrypt(blk, blk, len, ourkeys);
+    memset(ourkeys, 0, sizeof(ourkeys));
+}
+
+static void des_keysetup_xdmauth(unsigned char *keydata, DESContext *dc)
+{
+    unsigned char key[8];
+    int i, nbits, j;
+    unsigned int bits;
+
+    bits = 0;
+    nbits = 0;
+    j = 0;
+    for (i = 0; i < 8; i++) {
+       if (nbits < 7) {
+           bits = (bits << 8) | keydata[j];
+           nbits += 8;
+           j++;
+       }
+       key[i] = (bits >> (nbits - 7)) << 1;
+       bits &= ~(0x7F << (nbits - 7));
+       nbits -= 7;
+    }
+
+    des_key_setup(GET_32BIT_MSB_FIRST(key), GET_32BIT_MSB_FIRST(key + 4), dc);
+}
+
+void des_encrypt_xdmauth(unsigned char *keydata, unsigned char *blk, int len)
+{
+    DESContext dc;
+    des_keysetup_xdmauth(keydata, &dc);
+    des_cbc_encrypt(blk, blk, 24, &dc);
+}
+
+void des_decrypt_xdmauth(unsigned char *keydata, unsigned char *blk, int len)
+{
+    DESContext dc;
+    des_keysetup_xdmauth(keydata, &dc);
+    des_cbc_decrypt(blk, blk, 24, &dc);
 }
 
 static const struct ssh2_cipher ssh_3des_ssh2 = {
-    des3_csiv, des3_cskey,
-    des3_sciv, des3_sckey,
-    des3_ssh2_encrypt_blk,
-    des3_ssh2_decrypt_blk,
+    des3_make_context, des3_free_context, des3_iv, des3_key,
+    des3_ssh2_encrypt_blk, des3_ssh2_decrypt_blk,
     "3des-cbc",
-    8, 168
+    8, 168, "triple-DES CBC"
 };
 
+static const struct ssh2_cipher ssh_3des_ssh2_ctr = {
+    des3_make_context, des3_free_context, des3_iv, des3_key,
+    des3_ssh2_sdctr, des3_ssh2_sdctr,
+    "3des-ctr",
+    8, 168, "triple-DES SDCTR"
+};
+
+/*
+ * Single DES in SSH-2. "des-cbc" is marked as HISTORIC in
+ * draft-ietf-secsh-assignednumbers-04.txt, referring to
+ * FIPS-46-3.  ("Single DES (i.e., DES) will be permitted 
+ * for legacy systems only.") , but ssh.com support it and 
+ * apparently aren't the only people to do so, so we sigh 
+ * and implement it anyway.
+ */
+static const struct ssh2_cipher ssh_des_ssh2 = {
+    des_make_context, des3_free_context, des3_iv, des_key,
+    des_ssh2_encrypt_blk, des_ssh2_decrypt_blk,
+    "des-cbc",
+    8, 56, "single-DES CBC"
+};
+
+static const struct ssh2_cipher ssh_des_sshcom_ssh2 = {
+    des_make_context, des3_free_context, des3_iv, des_key,
+    des_ssh2_encrypt_blk, des_ssh2_decrypt_blk,
+    "des-cbc@ssh.com",
+    8, 56, "single-DES CBC"
+};
+
+/*
+ * "3des-ctr" is disabled because it hasn't had any interoperability
+ * testing, which is in turn because I couldn't find another implementation
+ * to test against.  Once it's been tested, it can be enabled in standard
+ * builds.
+ */
 static const struct ssh2_cipher *const des3_list[] = {
+/*  &ssh_3des_ssh2_ctr, */
     &ssh_3des_ssh2
 };
 
@@ -848,33 +1009,43 @@ const struct ssh2_ciphers ssh2_3des = {
     des3_list
 };
 
+static const struct ssh2_cipher *const des_list[] = {
+    &ssh_des_ssh2,
+    &ssh_des_sshcom_ssh2
+};
+
+const struct ssh2_ciphers ssh2_des = {
+    sizeof(des_list) / sizeof(*des_list),
+    des_list
+};
+
 const struct ssh_cipher ssh_3des = {
-    des3_sesskey,
-    des3_encrypt_blk,
-    des3_decrypt_blk,
-    8
+    des3_ssh1_make_context, des3_free_context, des3_sesskey,
+    des3_encrypt_blk, des3_decrypt_blk,
+    8, "triple-DES inner-CBC"
 };
 
-static void des_sesskey(unsigned char *key)
+static void des_sesskey(void *handle, unsigned char *key)
 {
-    des_key_setup(GET_32BIT_MSB_FIRST(key),
-                 GET_32BIT_MSB_FIRST(key + 4), &cskeys[0]);
-    logevent("Initialised single-DES encryption");
+    DESContext *keys = (DESContext *) handle;
+    des_key(keys, key);
+    des_key(keys+1, key);
 }
 
-static void des_encrypt_blk(unsigned char *blk, int len)
+static void des_encrypt_blk(void *handle, unsigned char *blk, int len)
 {
-    des_cbc_encrypt(blk, blk, len, cskeys);
+    DESContext *keys = (DESContext *) handle;
+    des_cbc_encrypt(blk, blk, len, keys);
 }
 
-static void des_decrypt_blk(unsigned char *blk, int len)
+static void des_decrypt_blk(void *handle, unsigned char *blk, int len)
 {
-    des_cbc_decrypt(blk, blk, len, cskeys);
+    DESContext *keys = (DESContext *) handle;
+    des_cbc_decrypt(blk, blk, len, keys+1);
 }
 
 const struct ssh_cipher ssh_des = {
-    des_sesskey,
-    des_encrypt_blk,
-    des_decrypt_blk,
-    8
+    des_ssh1_make_context, des3_free_context, des_sesskey,
+    des_encrypt_blk, des_decrypt_blk,
+    8, "single-DES CBC"
 };