Explorar el Código

Updating code to PuTTY cbbd464f

Source commit: a131c3c2fdfa7147f1891ce4abba4cd09ff594b9
Martin Prikryl hace 6 años
padre
commit
4fbf4913c4
Se han modificado 2 ficheros con 49 adiciones y 20 borrados
  1. 29 10
      source/putty/sshsh256.c
  2. 20 10
      source/putty/sshsha.c

+ 29 - 10
source/putty/sshsh256.c

@@ -31,19 +31,21 @@
 #   endif
 #endif
 
+#undef HW_SHA256
+
 #if defined _FORCE_SOFTWARE_SHA || !defined HW_SHA256
 #   undef HW_SHA256
 #   define HW_SHA256 HW_SHA256_NONE
 #endif
 
+#ifndef WINSCP_VS
+
 /*
  * The actual query function that asks if hardware acceleration is
  * available.
  */
 static bool sha256_hw_available(void);
 
-#ifndef WINSCP_VS
-
 /*
  * The top-level selection function, caching the results of
  * sha256_hw_available() so it only has to run once.
@@ -58,7 +60,6 @@ static bool sha256_hw_available_cached(void)
     }
     return hw_available;
 }
-#endif // !WINSCP_VS
 
 static ssh_hash *sha256_select(const ssh_hashalg *alg)
 {
@@ -73,6 +74,8 @@ const ssh_hashalg ssh_sha256 = {
     32, 64, "SHA-256",
 };
 
+#endif
+
 /* ----------------------------------------------------------------------
  * Definitions likely to be helpful to multiple implementations.
  */
@@ -116,7 +119,9 @@ static inline void sha256_block_setup(sha256_block *blk)
     blk->len = 0;
 }
 
-static inline bool sha256_block_write(
+#ifdef WINSCP_VS
+
+/*WINSCP static inline*/ bool sha256_block_write(
     sha256_block *blk, const void **vdata, size_t *len)
 {
     size_t blkleft = sizeof(blk->block) - blk->used;
@@ -137,7 +142,7 @@ static inline bool sha256_block_write(
     return false;
 }
 
-static inline void sha256_block_pad(sha256_block *blk, BinarySink *bs)
+/*WINSCP static inline*/ void sha256_block_pad(sha256_block *blk, BinarySink *bs)
 {
     uint64_t final_len = blk->len << 3;
     size_t pad = 1 + (63 & (55 - blk->used));
@@ -149,16 +154,14 @@ static inline void sha256_block_pad(sha256_block *blk, BinarySink *bs)
 
     assert(blk->used == 0 && "Should have exactly hit a block boundary");
 }
-#endif // !WINSCP_VS
 
-#ifndef WINSCP_VS
 /* ----------------------------------------------------------------------
  * Software implementation of SHA-256.
  */
 
 static inline uint32_t ror(uint32_t x, unsigned y)
 {
-    return (x << (31 & -y)) | (x >> (31 & y));
+    return (x << (31 & /*WINSCP*/(uint32_t)(-(int32_t)y))) | (x >> (31 & y));
 }
 
 static inline uint32_t Ch(uint32_t ctrl, uint32_t if1, uint32_t if0)
@@ -205,7 +208,7 @@ static inline void sha256_sw_round(
     *h = t1 + t2;
 }
 
-static void sha256_sw_block(uint32_t *core, const uint8_t *block)
+/*WINSCP static*/ void sha256_sw_block(uint32_t *core, const uint8_t *block)
 {
     uint32_t w[SHA256_ROUNDS];
     uint32_t a,b,c,d,e,f,g,h;
@@ -236,6 +239,15 @@ static void sha256_sw_block(uint32_t *core, const uint8_t *block)
     smemclr(w, sizeof(w));
 }
 
+#endif // WINSCP_VS
+
+#ifndef WINSCP_VS
+
+bool sha256_block_write(
+    sha256_block *blk, const void **vdata, size_t *len);
+void sha256_sw_block(uint32_t *core, const uint8_t *block);
+void sha256_block_pad(sha256_block *blk, BinarySink *bs);
+
 typedef struct sha256_sw {
     uint32_t core[8];
     sha256_block blk;
@@ -293,9 +305,12 @@ static void sha256_sw_final(ssh_hash *hash, uint8_t *digest)
     sha256_sw *s = container_of(hash, sha256_sw, hash);
 
     sha256_block_pad(&s->blk, BinarySink_UPCAST(s));
-    for (size_t i = 0; i < 8; i++)
+    { // WINSCP
+    size_t i; // WINSCP
+    for (i = 0; i < 8; i++)
         PUT_32BIT_MSB_FIRST(digest + 4*i, s->core[i]);
     sha256_sw_free(hash);
+    } // WINSCP
 }
 
 const ssh_hashalg ssh_sha256_sw = {
@@ -671,6 +686,8 @@ const ssh_hashalg ssh_sha256_hw = {
 
 #elif HW_SHA256 == HW_SHA256_NONE
 
+#ifndef WINSCP_VS
+
 static bool sha256_hw_available(void)
 {
     return false;
@@ -692,4 +709,6 @@ const ssh_hashalg ssh_sha256_hw = {
     32, 64, "SHA-256",
 };
 
+#endif // !WINSCP_VS
+
 #endif /* HW_SHA256 */

+ 20 - 10
source/putty/sshsha.c

@@ -125,11 +125,14 @@ static inline void sha1_block_pad(sha1_block *blk, BinarySink *bs)
     size_t pad = 1 + (63 & (55 - blk->used));
 
     put_byte(bs, 0x80);
-    for (size_t i = 1; i < pad; i++)
+    { // WINSCP
+    size_t i;
+    for (i = 1; i < pad; i++)
         put_byte(bs, 0);
     put_uint64(bs, final_len);
 
     assert(blk->used == 0 && "Should have exactly hit a block boundary");
+    } // WINSCP
 }
 
 /* ----------------------------------------------------------------------
@@ -138,7 +141,7 @@ static inline void sha1_block_pad(sha1_block *blk, BinarySink *bs)
 
 static inline uint32_t rol(uint32_t x, unsigned y)
 {
-    return (x << (31 & y)) | (x >> (31 & -y));
+    return (x << (31 & y)) | (x >> (31 & (uint32_t)(-(int32_t)y))); // WINSCP
 }
 
 static inline uint32_t Ch(uint32_t ctrl, uint32_t if1, uint32_t if0)
@@ -170,38 +173,41 @@ static void sha1_sw_block(uint32_t *core, const uint8_t *block)
     uint32_t w[SHA1_ROUNDS];
     uint32_t a,b,c,d,e;
 
-    for (size_t t = 0; t < 16; t++)
+    size_t t; // WINSCP
+    for (t = 0; t < 16; t++)
         w[t] = GET_32BIT_MSB_FIRST(block + 4*t);
 
-    for (size_t t = 16; t < SHA1_ROUNDS; t++)
+    for (t = 16; t < SHA1_ROUNDS; t++) // WINSCP
 	w[t] = rol(w[t - 3] ^ w[t - 8] ^ w[t - 14] ^ w[t - 16], 1);
 
     a = core[0]; b = core[1]; c = core[2]; d = core[3];
     e = core[4];
 
-    size_t t = 0;
-    for (size_t u = 0; u < SHA1_ROUNDS_PER_STAGE/5; u++) {
+    t = 0;
+    { // WINSCP
+    size_t u; // WINSCP
+    for (u = 0; u < SHA1_ROUNDS_PER_STAGE/5; u++) {
         sha1_sw_round(t++,w, &a,&b,&c,&d,&e, Ch(b,c,d), SHA1_STAGE0_CONSTANT);
         sha1_sw_round(t++,w, &e,&a,&b,&c,&d, Ch(a,b,c), SHA1_STAGE0_CONSTANT);
         sha1_sw_round(t++,w, &d,&e,&a,&b,&c, Ch(e,a,b), SHA1_STAGE0_CONSTANT);
         sha1_sw_round(t++,w, &c,&d,&e,&a,&b, Ch(d,e,a), SHA1_STAGE0_CONSTANT);
         sha1_sw_round(t++,w, &b,&c,&d,&e,&a, Ch(c,d,e), SHA1_STAGE0_CONSTANT);
     }
-    for (size_t u = 0; u < SHA1_ROUNDS_PER_STAGE/5; u++) {
+    for (u = 0; u < SHA1_ROUNDS_PER_STAGE/5; u++) {
         sha1_sw_round(t++,w, &a,&b,&c,&d,&e, Par(b,c,d), SHA1_STAGE1_CONSTANT);
         sha1_sw_round(t++,w, &e,&a,&b,&c,&d, Par(a,b,c), SHA1_STAGE1_CONSTANT);
         sha1_sw_round(t++,w, &d,&e,&a,&b,&c, Par(e,a,b), SHA1_STAGE1_CONSTANT);
         sha1_sw_round(t++,w, &c,&d,&e,&a,&b, Par(d,e,a), SHA1_STAGE1_CONSTANT);
         sha1_sw_round(t++,w, &b,&c,&d,&e,&a, Par(c,d,e), SHA1_STAGE1_CONSTANT);
     }
-    for (size_t u = 0; u < SHA1_ROUNDS_PER_STAGE/5; u++) {
+    for (u = 0; u < SHA1_ROUNDS_PER_STAGE/5; u++) {
         sha1_sw_round(t++,w, &a,&b,&c,&d,&e, Maj(b,c,d), SHA1_STAGE2_CONSTANT);
         sha1_sw_round(t++,w, &e,&a,&b,&c,&d, Maj(a,b,c), SHA1_STAGE2_CONSTANT);
         sha1_sw_round(t++,w, &d,&e,&a,&b,&c, Maj(e,a,b), SHA1_STAGE2_CONSTANT);
         sha1_sw_round(t++,w, &c,&d,&e,&a,&b, Maj(d,e,a), SHA1_STAGE2_CONSTANT);
         sha1_sw_round(t++,w, &b,&c,&d,&e,&a, Maj(c,d,e), SHA1_STAGE2_CONSTANT);
     }
-    for (size_t u = 0; u < SHA1_ROUNDS_PER_STAGE/5; u++) {
+    for (u = 0; u < SHA1_ROUNDS_PER_STAGE/5; u++) {
         sha1_sw_round(t++,w, &a,&b,&c,&d,&e, Par(b,c,d), SHA1_STAGE3_CONSTANT);
         sha1_sw_round(t++,w, &e,&a,&b,&c,&d, Par(a,b,c), SHA1_STAGE3_CONSTANT);
         sha1_sw_round(t++,w, &d,&e,&a,&b,&c, Par(e,a,b), SHA1_STAGE3_CONSTANT);
@@ -212,6 +218,7 @@ static void sha1_sw_block(uint32_t *core, const uint8_t *block)
     core[0] += a; core[1] += b; core[2] += c; core[3] += d; core[4] += e;
 
     smemclr(w, sizeof(w));
+    } // WINSCP
 }
 
 typedef struct sha1_sw {
@@ -271,9 +278,12 @@ static void sha1_sw_final(ssh_hash *hash, uint8_t *digest)
     sha1_sw *s = container_of(hash, sha1_sw, hash);
 
     sha1_block_pad(&s->blk, BinarySink_UPCAST(s));
-    for (size_t i = 0; i < 5; i++)
+    { // WINSCP
+    size_t i; // WINSCP
+    for (i = 0; i < 5; i++)
         PUT_32BIT_MSB_FIRST(digest + 4*i, s->core[i]);
     sha1_sw_free(hash);
+    } // WINSCP
 }
 
 const ssh_hashalg ssh_sha1_sw = {