Browse Source

Updating code to PuTTY 0d2d20aa

Source commit: 4527f13376bbbddb296a6adefa44ee37550edfba
Martin Prikryl 6 years ago
parent
commit
965a14838f
4 changed files with 33 additions and 12 deletions
  1. 28 9
      source/core/Cryptography.cpp
  2. 1 1
      source/core/PuttyIntf.cpp
  3. 1 1
      source/putty/ssh.c
  4. 3 1
      source/putty/sshpubk.c

+ 28 - 9
source/core/Cryptography.cpp

@@ -53,20 +53,39 @@
 
 #include <memory.h>
 
-#define sha1_ctx                  SHA_State
-#define sha1_begin(ctx)           putty_SHA_Init(ctx)
+#define sha1_begin(ctx)           ctx = (&ssh_sha1)->_new_(&ssh_sha1)
 #define sha1_hash(buf, len, ctx)  put_data(ctx, buf, len)
-#define sha1_end(dig, ctx)        putty_SHA_Final(ctx, dig)
+#define sha1_end(dig, ctx)        ssh_hash_final(ctx, dig); ctx = NULL
 
 #define IN_BLOCK_LENGTH     64
 #define OUT_BLOCK_LENGTH    20
 #define HMAC_IN_DATA        0xffffffff
 
-typedef struct
+struct hmac_ctx
 {   unsigned char   key[IN_BLOCK_LENGTH];
-    sha1_ctx        ctx[1];
+    ssh_hash       *ctx;
     unsigned int    klen;
-} hmac_ctx;
+    hmac_ctx()
+    {
+        memset(this, 0, sizeof(*this));
+    }
+    ~hmac_ctx()
+    {
+        if (ctx != NULL) ssh_hash_free(ctx);
+    }
+    void CopyFrom(hmac_ctx * Source)
+    {
+        if (ctx != NULL)
+        {
+            ssh_hash_free(ctx);
+        }
+        memmove(this, Source, sizeof(*this));
+        if (Source->ctx != NULL)
+        {
+            ctx = ssh_hash_copy(Source->ctx);
+        }
+    }
+};
 
 /* initialise the HMAC context to zero */
 static void hmac_sha1_begin(hmac_ctx cx[1])
@@ -236,7 +255,7 @@ static void derive_key(const unsigned char pwd[],  /* the PASSWORD     */
     hmac_sha1_key(pwd, pwd_len, c1);
 
     /* set HMAC context (c2) for password and salt      */
-    memmove(c2, c1, sizeof(hmac_ctx));
+    c2->CopyFrom(c1);
     hmac_sha1_data(salt, salt_len, c2);
 
     /* find the number of SHA blocks in the key         */
@@ -248,7 +267,7 @@ static void derive_key(const unsigned char pwd[],  /* the PASSWORD     */
         memset(ux, 0, OUT_BLOCK_LENGTH);
 
         /* set HMAC context (c3) for password and salt  */
-        memmove(c3, c2, sizeof(hmac_ctx));
+        c3->CopyFrom(c2);
 
         /* enter additional data for 1st block into uu  */
         uu[0] = (unsigned char)((i + 1) >> 24);
@@ -270,7 +289,7 @@ static void derive_key(const unsigned char pwd[],  /* the PASSWORD     */
                 ux[k] ^= uu[k];
 
             /* set HMAC context (c3) for password   */
-            memmove(c3, c1, sizeof(hmac_ctx));
+            c3->CopyFrom(c1);
         }
 
         /* compile key blocks into the key output   */

+ 1 - 1
source/core/PuttyIntf.cpp

@@ -810,7 +810,7 @@ UnicodeString __fastcall GetPuTTYVersion()
 UnicodeString __fastcall Sha256(const char * Data, size_t Size)
 {
   unsigned char Digest[32];
-  SHA256_Simple(Data, Size, Digest);
+  hash_simple(&ssh_sha256, make_ptrlen(Data, Size), Digest);
   UnicodeString Result(BytesToHex(Digest, LENOF(Digest)));
   return Result;
 }

+ 1 - 1
source/putty/ssh.c

@@ -1189,6 +1189,6 @@ unsigned int winscp_query(Backend * be, int query)
 
 void md5checksum(const char * buffer, int len, unsigned char output[16])
 {
-  MD5Simple(buffer, len, output);
+  hash_simple(&ssh_md5, make_ptrlen(buffer, len), output);
 }
 #endif

+ 3 - 1
source/putty/sshpubk.c

@@ -1324,6 +1324,7 @@ bool ssh2_save_userkey(
 	put_string(macdata, pub_blob->s, pub_blob->len);
 	put_string(macdata, priv_blob_encrypted, priv_encrypted_len);
 
+        { // WINSCP
         ssh_hash *h = ssh_hash_new(&ssh_sha1);
 	put_data(h, header, sizeof(header)-1);
 	if (passphrase)
@@ -1333,6 +1334,7 @@ bool ssh2_save_userkey(
                    ptrlen_from_strbuf(macdata), priv_mac);
 	strbuf_free(macdata);
 	smemclr(mackey, sizeof(mackey));
+        } // WINSCP
     }
 
     if (passphrase) {
@@ -1521,7 +1523,7 @@ char *ssh2_fingerprint_blob(const void *blob, int bloblen)
     for (i = 0; i < 16; i++)
         sprintf(fingerprint_str_md5 + i*3, "%02x%s", digest[i], i==15 ? "" : ":");
 
-    SHA256_Simple(blob, bloblen, digest);
+    hash_simple(&ssh_sha256, make_ptrlen(blob, bloblen), digest);
     base64_encode_buf(digest, 32, fingerprint_str_sha256);
 
     /*