Wez Furlong's patch to add xterm mouse reporting and proper mouse
[u/mdw/putty] / sshdes.c
index 1c35157..1665b3d 100644 (file)
--- a/sshdes.c
+++ b/sshdes.c
@@ -1,6 +1,7 @@
 #include <assert.h>
 #include "ssh.h"
 
+
 /* des.c - implementation of DES
  */
 
@@ -655,6 +656,30 @@ static void des_3cbc_encrypt(unsigned char *dest, const unsigned char *src,
     des_cbc_encrypt(dest, src, len, &scheds[2]);
 }
 
+static void des_cbc3_encrypt(unsigned char *dest, const unsigned char *src,
+                             unsigned int len, DESContext *scheds) {
+    word32 out[2], iv0, iv1;
+    unsigned int i;
+
+    assert((len & 7) == 0);
+
+    iv0 = scheds->eiv0;
+    iv1 = scheds->eiv1;
+    for (i = 0; i < len; i += 8) {
+        iv0 ^= GET_32BIT_MSB_FIRST(src); src += 4;
+        iv1 ^= GET_32BIT_MSB_FIRST(src); src += 4;
+        des_encipher(out, iv0, iv1, &scheds[0]);
+        des_decipher(out, out[0], out[1], &scheds[1]);
+        des_encipher(out, out[0], out[1], &scheds[2]);
+        iv0 = out[0];
+        iv1 = out[1];
+        PUT_32BIT_MSB_FIRST(dest, iv0); dest += 4;
+        PUT_32BIT_MSB_FIRST(dest, iv1); dest += 4;
+    }
+    scheds->eiv0 = iv0;
+    scheds->eiv1 = iv1;
+}
+
 static void des_3cbc_decrypt(unsigned char *dest, const unsigned char *src,
                              unsigned int len, DESContext *scheds) {
     des_cbc_decrypt(dest, src, len, &scheds[2]);
@@ -662,6 +687,32 @@ static void des_3cbc_decrypt(unsigned char *dest, const unsigned char *src,
     des_cbc_decrypt(dest, src, len, &scheds[0]);
 }
 
+static void des_cbc3_decrypt(unsigned char *dest, const unsigned char *src,
+                             unsigned int len, DESContext *scheds) {
+    word32 out[2], iv0, iv1, xL, xR;
+    unsigned int i;
+
+    assert((len & 7) == 0);
+
+    iv0 = scheds->div0;
+    iv1 = scheds->div1;
+    for (i = 0; i < len; i += 8) {
+        xL = GET_32BIT_MSB_FIRST(src); src += 4;
+        xR = GET_32BIT_MSB_FIRST(src); src += 4;
+        des_decipher(out, xL, xR, &scheds[2]);
+        des_encipher(out, out[0], out[1], &scheds[1]);
+        des_decipher(out, out[0], out[1], &scheds[0]);
+        iv0 ^= out[0];
+        iv1 ^= out[1];
+        PUT_32BIT_MSB_FIRST(dest, iv0); dest += 4;
+        PUT_32BIT_MSB_FIRST(dest, iv1); dest += 4;
+        iv0 = xL;
+        iv1 = xR;
+    }
+    scheds->div0 = iv0;
+    scheds->div1 = iv1;
+}
+
 static DESContext cskeys[3], sckeys[3];
 
 static void des3_cskey(unsigned char *key) {
@@ -707,13 +758,60 @@ static void des3_decrypt_blk(unsigned char *blk, int len) {
     des_3cbc_decrypt(blk, blk, len, sckeys);
 }
 
-struct ssh_cipher ssh_3des = {
-    des3_sesskey,
+static void des3_ssh2_encrypt_blk(unsigned char *blk, int len) {
+    des_cbc3_encrypt(blk, blk, len, cskeys);
+}
+
+static void des3_ssh2_decrypt_blk(unsigned char *blk, int len) {
+    des_cbc3_decrypt(blk, blk, len, sckeys);
+}
+
+void des3_decrypt_pubkey(unsigned char *key,
+                         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),
+                  GET_32BIT_MSB_FIRST(key+4), &ourkeys[2]);
+    des_3cbc_decrypt(blk, blk, len, ourkeys);
+}
+
+void des3_encrypt_pubkey(unsigned char *key,
+                         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),
+                  GET_32BIT_MSB_FIRST(key+4), &ourkeys[2]);
+    des_3cbc_encrypt(blk, blk, len, ourkeys);
+}
+
+static const struct ssh2_cipher ssh_3des_ssh2 = {
     des3_csiv, des3_cskey,
     des3_sciv, des3_sckey,
+    des3_ssh2_encrypt_blk,
+    des3_ssh2_decrypt_blk,
+    "3des-cbc",
+    8, 168
+};
+
+static const struct ssh2_cipher *const des3_list[] = {
+    &ssh_3des_ssh2
+};
+
+const struct ssh2_ciphers ssh2_3des = {
+    sizeof(des3_list) / sizeof(*des3_list),
+    des3_list
+};
+
+const struct ssh_cipher ssh_3des = {
+    des3_sesskey,
     des3_encrypt_blk,
     des3_decrypt_blk,
-    "3des-cbc",
     8
 };
 
@@ -731,11 +829,9 @@ static void des_decrypt_blk(unsigned char *blk, int len) {
     des_cbc_decrypt(blk, blk, len, cskeys);
 }
 
-struct ssh_cipher ssh_des = {
+const struct ssh_cipher ssh_des = {
     des_sesskey,
-    NULL, NULL, NULL, NULL,            /* SSH 2 bits - unused */
     des_encrypt_blk,
     des_decrypt_blk,
-    "des-cbc", /* should never be used - not a valid cipher in ssh2 */
     8
 };