1 /* $OpenBSD: crypto_internal.h,v 1.6 2023/05/27 09:18:17 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 #ifndef HAVE_CRYPTO_ROL_U32 91 static inline uint32_t 92 crypto_rol_u32(uint32_t v, size_t shift) 93 { 94 return (v << shift) | (v >> (32 - shift)); 95 } 96 #endif 97 98 #ifndef HAVE_CRYPTO_ROR_U32 99 static inline uint32_t 100 crypto_ror_u32(uint32_t v, size_t shift) 101 { 102 return (v << (32 - shift)) | (v >> shift); 103 } 104 #endif 105 106 #ifndef HAVE_CRYPTO_ROL_U64 107 static inline uint64_t 108 crypto_rol_u64(uint64_t v, size_t shift) 109 { 110 return (v << shift) | (v >> (64 - shift)); 111 } 112 #endif 113 114 #ifndef HAVE_CRYPTO_ROR_U64 115 static inline uint64_t 116 crypto_ror_u64(uint64_t v, size_t shift) 117 { 118 return (v << (64 - shift)) | (v >> shift); 119 } 120 #endif 121 122 #endif 123