1 /* $OpenBSD: crypto_internal.h,v 1.5 2023/05/19 00:54:27 deraadt 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 /* 26 * crypto_store_htobe32() stores a 32 bit unsigned host endian value 27 * as a 32 bit unsigned big endian value, at the specified location in 28 * memory. The memory location may have any alignment. 29 */ 30 #ifndef HAVE_CRYPTO_STORE_HTOBE32 31 static inline void 32 crypto_store_htobe32(uint8_t *dst, uint32_t v) 33 { 34 v = htobe32(v); 35 memcpy(dst, &v, sizeof(v)); 36 } 37 #endif 38 39 /* 40 * crypto_store_htobe64() stores a 64 bit unsigned host endian value 41 * as a 64 bit unsigned big endian value, at the specified location in 42 * memory. The memory location may have any alignment. 43 */ 44 #ifndef HAVE_CRYPTO_STORE_HTOBE64 45 static inline void 46 crypto_store_htobe64(uint8_t *dst, uint64_t v) 47 { 48 v = htobe64(v); 49 memcpy(dst, &v, sizeof(v)); 50 } 51 #endif 52 53 #ifndef HAVE_CRYPTO_ROL_U32 54 static inline uint32_t 55 crypto_rol_u32(uint32_t v, size_t shift) 56 { 57 return (v << shift) | (v >> (32 - shift)); 58 } 59 #endif 60 61 #ifndef HAVE_CRYPTO_ROR_U32 62 static inline uint32_t 63 crypto_ror_u32(uint32_t v, size_t shift) 64 { 65 return (v << (32 - shift)) | (v >> shift); 66 } 67 #endif 68 69 #ifndef HAVE_CRYPTO_ROL_U64 70 static inline uint64_t 71 crypto_rol_u64(uint64_t v, size_t shift) 72 { 73 return (v << shift) | (v >> (64 - shift)); 74 } 75 #endif 76 77 #ifndef HAVE_CRYPTO_ROR_U64 78 static inline uint64_t 79 crypto_ror_u64(uint64_t v, size_t shift) 80 { 81 return (v << (64 - shift)) | (v >> shift); 82 } 83 #endif 84 85 #endif 86