{"thread":{"id":"20563","subject":"[PATCH 3/3] block-sha1: support for architectures with memory alignment restrictions","startedAt":"2009-08-12T19:47:55Z","lastAt":"2009-08-12T19:47:55Z","messageCount":1,"participants":["Nicolas Pitre"],"isPatch":true,"patchVersion":1,"patchTotal":3},"messages":[{"id":"120444","messageId":"alpine.LFD.2.00.0908121546480.10633@xanadu.home","threadId":"20563","inReplyTo":null,"subject":"[PATCH 3/3] block-sha1: support for architectures with memory alignment restrictions","fromName":"Nicolas Pitre","fromEmail":"nico@cam.org","sentAt":"2009-08-12T19:47:55Z","receivedAt":"2009-08-12T19:47:55Z","isPatch":true,"sender":{"key":"nico@fluxnic.net","avatar":"https://avatars.githubusercontent.com/u/702790?v=4"},"body":"\nThis is needed on architectures with poor or non-existent unaligned memory\nsupport and/or no fast byte swap instruction (such as ARM) by using byte\naccesses to memory and shifting the result together.\n\nThis also makes the code portable, therefore the byte access methods are\nthe defaults.  Any architecture that properly supports unaligned word\naccesses in hardware simply has to enable the alternative methods.\n\nSigned-off-by: Nicolas Pitre <nico@cam.org>\n---\n block-sha1/sha1.c |   32 ++++++++++++++++++++++++++++++--\n 1 files changed, 30 insertions(+), 2 deletions(-)\n\ndiff --git a/block-sha1/sha1.c b/block-sha1/sha1.c\nindex 67c9bd0..d3121f7 100644\n--- a/block-sha1/sha1.c\n+++ b/block-sha1/sha1.c\n@@ -60,6 +60,34 @@\n   #define setW(x, val) (W(x) = (val))\n #endif\n \n+/*\n+ * Performance might be improved if the CPU architecture is OK with\n+ * unaligned 32-bit loads and a fast ntohl() is available.\n+ * Otherwise fall back to byte loads and shifts which is portable,\n+ * and is faster on architectures with memory alignment issues.\n+ */\n+\n+#if defined(__i386__) || defined(__x86_64__)\n+\n+#define get_be32(p)\tntohl(*(unsigned int *)(p))\n+#define put_be32(p, v)\tdo { *(unsigned int *)(p) = htonl(v); } while (0)\n+\n+#else\n+\n+#define get_be32(p)\t( \\\n+\t(*((unsigned char *)(p) + 0) << 24) | \\\n+\t(*((unsigned char *)(p) + 1) << 16) | \\\n+\t(*((unsigned char *)(p) + 2) <<  8) | \\\n+\t(*((unsigned char *)(p) + 3) <<  0) )\n+#define put_be32(p, v)\tdo { \\\n+\tunsigned int __v = (v); \\\n+\t*((unsigned char *)(p) + 0) = __v >> 24; \\\n+\t*((unsigned char *)(p) + 1) = __v >> 16; \\\n+\t*((unsigned char *)(p) + 2) = __v >>  8; \\\n+\t*((unsigned char *)(p) + 3) = __v >>  0; } while (0)\n+\n+#endif\n+\n /* This \"rolls\" over the 512-bit array */\n #define W(x) (array[(x)&15])\n \n@@ -67,7 +95,7 @@\n  * Where do we get the source from? The first 16 iterations get it from\n  * the input data, the next mix it from the 512-bit array.\n  */\n-#define SHA_SRC(t) htonl(data[t])\n+#define SHA_SRC(t) get_be32(data + t)\n #define SHA_MIX(t) SHA_ROL(W(t+13) ^ W(t+8) ^ W(t+2) ^ W(t), 1)\n \n #define SHA_ROUND(t, input, fn, constant, A, B, C, D, E) do { \\\n@@ -245,5 +273,5 @@ void blk_SHA1_Final(unsigned char hashout[20], blk_SHA_CTX *ctx)\n \n \t/* Output hash */\n \tfor (i = 0; i < 5; i++)\n-\t\t((unsigned int *)hashout)[i] = htonl(ctx->H[i]);\n+\t\tput_be32(hashout + i*4, ctx->H[i]);\n }\n-- \n1.6.4.189.g282fa\n"}]}