summaryrefslogtreecommitdiffstats
path: root/crypto/modes/ctr128.c
diff options
context:
space:
mode:
authorBernd Edlinger <bernd.edlinger@hotmail.de>2018-04-24 21:10:13 +0200
committerBernd Edlinger <bernd.edlinger@hotmail.de>2020-05-27 20:11:20 +0200
commit77286fe3ec6b9777934e67e35f3b7007143b0734 (patch)
tree47f39fa07cd9979b57ad14f58bc39123133da115 /crypto/modes/ctr128.c
parentc74aaa3920f116fe4c1003153838144c37d6e527 (diff)
Avoid undefined behavior with unaligned accesses
Fixes: #4983 [extended tests] Reviewed-by: Nicola Tuveri <nic.tuv@gmail.com> (Merged from https://github.com/openssl/openssl/pull/6074)
Diffstat (limited to 'crypto/modes/ctr128.c')
-rw-r--r--crypto/modes/ctr128.c11
1 files changed, 9 insertions, 2 deletions
diff --git a/crypto/modes/ctr128.c b/crypto/modes/ctr128.c
index ff7499b34a..fc1db42d7f 100644
--- a/crypto/modes/ctr128.c
+++ b/crypto/modes/ctr128.c
@@ -11,6 +11,12 @@
#include <openssl/crypto.h>
#include "crypto/modes.h"
+#if defined(__GNUC__) && !defined(STRICT_ALIGNMENT)
+typedef size_t size_t_aX __attribute((__aligned__(1)));
+#else
+typedef size_t size_t_aX;
+#endif
+
/*
* NOTE: the IV/counter CTR mode is big-endian. The code itself is
* endian-neutral.
@@ -97,8 +103,9 @@ void CRYPTO_ctr128_encrypt(const unsigned char *in, unsigned char *out,
(*block) (ivec, ecount_buf, key);
ctr128_inc_aligned(ivec);
for (n = 0; n < 16; n += sizeof(size_t))
- *(size_t *)(out + n) =
- *(size_t *)(in + n) ^ *(size_t *)(ecount_buf + n);
+ *(size_t_aX *)(out + n) =
+ *(size_t_aX *)(in + n)
+ ^ *(size_t_aX *)(ecount_buf + n);
len -= 16;
out += 16;
in += 16;