Jelajahi Sumber

PuTTY snapshot ced0f191 (Ensure our aes_ni_context is 16-byte aligned - 2019-01-13)

Source commit: af847640ca07dfb719dca8e3ce6f511393b29138
Martin Prikryl 6 tahun lalu
induk
melakukan
383877dc7b
2 mengubah file dengan 29 tambahan dan 2 penghapusan
  1. 12 0
      source/putty/ssh.h
  2. 17 2
      source/putty/sshaes.c

+ 12 - 0
source/putty/ssh.h

@@ -860,11 +860,23 @@ extern const ssh2_cipheralg ssh_3des_ssh2;
 extern const ssh2_cipheralg ssh_des_ssh2;
 extern const ssh2_cipheralg ssh_des_sshcom_ssh2;
 extern const ssh2_cipheralg ssh_aes256_sdctr;
+extern const ssh2_cipheralg ssh_aes256_sdctr_hw;
+extern const ssh2_cipheralg ssh_aes256_sdctr_sw;
 extern const ssh2_cipheralg ssh_aes256_cbc;
+extern const ssh2_cipheralg ssh_aes256_cbc_hw;
+extern const ssh2_cipheralg ssh_aes256_cbc_sw;
 extern const ssh2_cipheralg ssh_aes192_sdctr;
+extern const ssh2_cipheralg ssh_aes192_sdctr_hw;
+extern const ssh2_cipheralg ssh_aes192_sdctr_sw;
 extern const ssh2_cipheralg ssh_aes192_cbc;
+extern const ssh2_cipheralg ssh_aes192_cbc_hw;
+extern const ssh2_cipheralg ssh_aes192_cbc_sw;
 extern const ssh2_cipheralg ssh_aes128_sdctr;
+extern const ssh2_cipheralg ssh_aes128_sdctr_hw;
+extern const ssh2_cipheralg ssh_aes128_sdctr_sw;
 extern const ssh2_cipheralg ssh_aes128_cbc;
+extern const ssh2_cipheralg ssh_aes128_cbc_hw;
+extern const ssh2_cipheralg ssh_aes128_cbc_sw;
 extern const ssh2_cipheralg ssh_blowfish_ssh2_ctr;
 extern const ssh2_cipheralg ssh_blowfish_ssh2;
 extern const ssh2_cipheralg ssh_arcfour256_ssh2;

+ 17 - 2
source/putty/sshaes.c

@@ -1330,6 +1330,7 @@ typedef struct aes_ni_context aes_ni_context;
 struct aes_ni_context {
     __m128i keysched_e[MAXROUNDKEYS], keysched_d[MAXROUNDKEYS], iv;
 
+    void *pointer_to_free;
     ssh2_cipher ciph;
 };
 
@@ -1338,16 +1339,30 @@ static ssh2_cipher *aes_hw_new(const ssh2_cipheralg *alg)
     if (!aes_hw_available_cached())
         return NULL;
 
-    aes_ni_context *ctx = snew(aes_ni_context);
+    /*
+     * The __m128i variables in the context structure need to be
+     * 16-byte aligned, but not all malloc implementations that this
+     * code has to work with will guarantee to return a 16-byte
+     * aligned pointer. So we over-allocate, manually realign the
+     * pointer ourselves, and store the original one inside the
+     * context so we know how to free it later.
+     */
+    void *allocation = smalloc(sizeof(aes_ni_context) + 15);
+    uintptr_t alloc_address = (uintptr_t)allocation;
+    uintptr_t aligned_address = (alloc_address + 15) & ~15;
+    aes_ni_context *ctx = (aes_ni_context *)aligned_address;
+
     ctx->ciph.vt = alg;
+    ctx->pointer_to_free = allocation;
     return &ctx->ciph;
 }
 
 static void aes_hw_free(ssh2_cipher *ciph)
 {
     aes_ni_context *ctx = container_of(ciph, aes_ni_context, ciph);
+    void *allocation = ctx->pointer_to_free;
     smemclr(ctx, sizeof(*ctx));
-    sfree(ctx);
+    sfree(allocation);
 }
 
 static void aes_hw_setkey(ssh2_cipher *ciph, const void *vkey)