1 /* $OpenBSD: crypto_internal.h,v 1.7 2023/08/15 08:39:27 jsing Exp $ */ 2 /* 3 * Copyright (c) 2023 Joel Sing <jsing@openbsd.org> 4 * 5 * Permission to use, copy, modify, and distribute this software for any 6 * purpose with or without fee is hereby granted, provided that the above 7 * copyright notice and this permission notice appear in all copies. 8 * 9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 */ 17 18 #include <endian.h> 19 #include <stddef.h> 20 #include <string.h> 21 22 #ifndef HEADER_CRYPTO_INTERNAL_H 23 #define HEADER_CRYPTO_INTERNAL_H 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 34 static inline uint32_t 35 crypto_load_be32toh(const uint8_t *src) 36 { 37 uint32_t v; 38 39 memcpy(&v, src, sizeof(v)); 40 41 return be32toh(v); 42 } 43 #endif 44 45 /* 46 * crypto_store_htobe32() stores a 32 bit unsigned host endian value as a 32 bit 47 * unsigned big endian value, at the specified address in memory. The memory 48 * address may have any alignment. 49 */ 50 #ifndef HAVE_CRYPTO_STORE_HTOBE32 51 static inline void 52 crypto_store_htobe32(uint8_t *dst, uint32_t v) 53 { 54 v = htobe32(v); 55 memcpy(dst, &v, sizeof(v)); 56 } 57 #endif 58 59 /* 60 * crypto_load_be64toh() loads a 64 bit unsigned big endian value as a 64 bit 61 * unsigned host endian value, from the specified address in memory. The memory 62 * address may have any alignment. 63 */ 64 #ifndef HAVE_CRYPTO_LOAD_BE64TOH 65 static inline uint64_t 66 crypto_load_be64toh(const uint8_t *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. 80 */ 81 #ifndef HAVE_CRYPTO_STORE_HTOBE64 82 static inline void 83 crypto_store_htobe64(uint8_t *dst, uint64_t v) 84 { 85 v = htobe64(v); 86 memcpy(dst, &v, sizeof(v)); 87 } 88 #endif 89 90 /* 91 * crypto_load_le32toh() loads a 32 bit unsigned little endian value as a 32 bit 92 * unsigned host endian value, from the specified address in memory. The memory 93 * address may have any alignment. 94 */ 95 #ifndef HAVE_CRYPTO_LOAD_BE32TOH 96 static inline uint32_t 97 crypto_load_le32toh(const uint8_t *src) 98 { 99 uint32_t v; 100 101 memcpy(&v, src, sizeof(v)); 102 103 return le32toh(v); 104 } 105 #endif 106 107 /* 108 * crypto_store_htole32() stores a 32 bit unsigned host endian value as a 32 bit 109 * unsigned little endian value, at the specified address in memory. The memory 110 * address may have any alignment. 111 */ 112 #ifndef HAVE_CRYPTO_STORE_HTOBE32 113 static inline void 114 crypto_store_htole32(uint8_t *dst, uint32_t v) 115 { 116 v = htole32(v); 117 memcpy(dst, &v, sizeof(v)); 118 } 119 #endif 120 121 #ifndef HAVE_CRYPTO_ROL_U32 122 static inline uint32_t 123 crypto_rol_u32(uint32_t v, size_t shift) 124 { 125 return (v << shift) | (v >> (32 - shift)); 126 } 127 #endif 128 129 #ifndef HAVE_CRYPTO_ROR_U32 130 static inline uint32_t 131 crypto_ror_u32(uint32_t v, size_t shift) 132 { 133 return (v << (32 - shift)) | (v >> shift); 134 } 135 #endif 136 137 #ifndef HAVE_CRYPTO_ROL_U64 138 static inline uint64_t 139 crypto_rol_u64(uint64_t v, size_t shift) 140 { 141 return (v << shift) | (v >> (64 - shift)); 142 } 143 #endif 144 145 #ifndef HAVE_CRYPTO_ROR_U64 146 static inline uint64_t 147 crypto_ror_u64(uint64_t v, size_t shift) 148 { 149 return (v << (64 - shift)) | (v >> shift); 150 } 151 #endif 152 153 #endif 154