summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/crypto_internal.h
diff options
context:
space:
mode:
authorjsing <>2023-05-17 06:37:14 +0000
committerjsing <>2023-05-17 06:37:14 +0000
commit6683fd06977fdaa02290993ccf540e097725fbcb (patch)
tree1719bad5dfd26376b240d5f9e7a9ba134c117d2e /src/lib/libcrypto/crypto_internal.h
parentb36a9f1697cdcaa5ea871949d17a1b748ed8b0a2 (diff)
downloadopenbsd-6683fd06977fdaa02290993ccf540e097725fbcb.tar.gz
openbsd-6683fd06977fdaa02290993ccf540e097725fbcb.tar.bz2
openbsd-6683fd06977fdaa02290993ccf540e097725fbcb.zip
Clean up alignment handling for SHA-512.
All assembly implementations are required to perform their own alignment handling. In the case of the C implementation, on strict alignment platforms, unaligned data will be copied into an aligned buffer. However, most platforms then perform byte-by-byte reads (via the PULL64 macros). Instead, remove SHA512_BLOCK_CAN_MANAGE_UNALIGNED_DATA and alignment handling to sha512_block_data_order() - if the data is aligned then simply perform 64 bit loads and then do endian conversion via be64toh(). If the data is unaligned then use memcpy() and be64toh() (in the form of crypto_load_be64toh()). Overall this reduces complexity and can improve performance (on aarch64 we get a ~10% performance gain with aligned input and about ~1-2% gain on armv7), while the same movq/bswapq is generated for amd64 and movl/bswapl for i386. ok tb@
Diffstat (limited to 'src/lib/libcrypto/crypto_internal.h')
-rw-r--r--src/lib/libcrypto/crypto_internal.h55
1 files changed, 46 insertions, 9 deletions
diff --git a/src/lib/libcrypto/crypto_internal.h b/src/lib/libcrypto/crypto_internal.h
index 24a06256db..2e6ab82692 100644
--- a/src/lib/libcrypto/crypto_internal.h
+++ b/src/lib/libcrypto/crypto_internal.h
@@ -1,4 +1,4 @@
1/* $OpenBSD: crypto_internal.h,v 1.3 2023/04/14 10:42:51 jsing Exp $ */ 1/* $OpenBSD: crypto_internal.h,v 1.4 2023/05/17 06:37:14 jsing Exp $ */
2/* 2/*
3 * Copyright (c) 2023 Joel Sing <jsing@openbsd.org> 3 * Copyright (c) 2023 Joel Sing <jsing@openbsd.org>
4 * 4 *
@@ -22,14 +22,34 @@
22#ifndef HEADER_CRYPTO_INTERNAL_H 22#ifndef HEADER_CRYPTO_INTERNAL_H
23#define HEADER_CRYPTO_INTERNAL_H 23#define HEADER_CRYPTO_INTERNAL_H
24 24
25#define CTASSERT(x) \
26 extern char _ctassert[(x) ? 1 : -1] __attribute__((__unused__))
27
28/*
29 * crypto_load_be32toh() loads a 32 bit unsigned big endian value as a 32 bit
30 * unsigned host endian value, from the specified address in memory. The memory
31 * address may have any alignment.
32 */
33#ifndef HAVE_CRYPTO_LOAD_BE32TOH
34static inline uint32_t
35crypto_load_be32toh(const void *src)
36{
37 uint32_t v;
38
39 memcpy(&v, src, sizeof(v));
40
41 return be32toh(v);
42}
43#endif
44
25/* 45/*
26 * crypto_store_htobe32() stores a 32 bit unsigned host endian value 46 * crypto_store_htobe32() stores a 32 bit unsigned host endian value as a 32 bit
27 * as a 32 bit unsigned big endian value, at the specified location in 47 * unsigned big endian value, at the specified address in memory. The memory
28 * memory. The memory location may have any alignment. 48 * address may have any alignment.
29 */ 49 */
30#ifndef HAVE_CRYPTO_STORE_HTOBE32 50#ifndef HAVE_CRYPTO_STORE_HTOBE32
31static inline void 51static inline void
32crypto_store_htobe32(uint8_t *dst, uint32_t v) 52crypto_store_htobe32(void *dst, uint32_t v)
33{ 53{
34 v = htobe32(v); 54 v = htobe32(v);
35 memcpy(dst, &v, sizeof(v)); 55 memcpy(dst, &v, sizeof(v));
@@ -37,13 +57,30 @@ crypto_store_htobe32(uint8_t *dst, uint32_t v)
37#endif 57#endif
38 58
39/* 59/*
40 * crypto_store_htobe64() stores a 64 bit unsigned host endian value 60 * crypto_load_be64toh() loads a 64 bit unsigned big endian value as a 64 bit
41 * as a 64 bit unsigned big endian value, at the specified location in 61 * unsigned host endian value, from the specified address in memory. The memory
42 * memory. The memory location may have any alignment. 62 * address may have any alignment.
63 */
64#ifndef HAVE_CRYPTO_LOAD_BE64TOH
65static inline uint64_t
66crypto_load_be64toh(const void *src)
67{
68 uint64_t v;
69
70 memcpy(&v, src, sizeof(v));
71
72 return be64toh(v);
73}
74#endif
75
76/*
77 * crypto_store_htobe64() stores a 64 bit unsigned host endian value as a 64 bit
78 * unsigned big endian value, at the specified address in memory. The memory
79 * address may have any alignment.
43 */ 80 */
44#ifndef HAVE_CRYPTO_STORE_HTOBE64 81#ifndef HAVE_CRYPTO_STORE_HTOBE64
45static inline void 82static inline void
46crypto_store_htobe64(uint8_t *dst, uint64_t v) 83crypto_store_htobe64(void *dst, uint64_t v)
47{ 84{
48 v = htobe64(v); 85 v = htobe64(v);
49 memcpy(dst, &v, sizeof(v)); 86 memcpy(dst, &v, sizeof(v));