diff options
author | Sander Vrijders <[email protected]> | 2017-02-15 17:45:49 +0100 |
---|---|---|
committer | Sander Vrijders <[email protected]> | 2017-02-16 11:42:45 +0100 |
commit | 1434ab198b38cbc9096809a6cadfe6759736cc9f (patch) | |
tree | 8d4bb82d156046dc910c3bfbcfb13d52a1c36ccc /src | |
parent | 2fee864da506c1e6944c7caa2b6dcbe746165ca4 (diff) | |
download | ouroboros-1434ab198b38cbc9096809a6cadfe6759736cc9f.tar.gz ouroboros-1434ab198b38cbc9096809a6cadfe6759736cc9f.zip |
lib: Revise endian header and SHA3
This revises the endian header to let the build time checks of
endianness be performed by the standard libraries. We just check for
the OS that is being used and provide the endian functions from
OpenBSD to everyone. It also updates the SHA3 sources to use this new
header. The byte order header is removed.
Diffstat (limited to 'src')
-rw-r--r-- | src/ipcpd/normal/enroll.c | 2 | ||||
-rw-r--r-- | src/lib/byte_order.h | 111 | ||||
-rw-r--r-- | src/lib/sha3.c | 68 |
3 files changed, 40 insertions, 141 deletions
diff --git a/src/ipcpd/normal/enroll.c b/src/ipcpd/normal/enroll.c index d09a62a9..a0d28598 100644 --- a/src/ipcpd/normal/enroll.c +++ b/src/ipcpd/normal/enroll.c @@ -21,12 +21,12 @@ #define OUROBOROS_PREFIX "enrollment" #include <ouroboros/config.h> +#include <ouroboros/endian.h> #include <ouroboros/time_utils.h> #include <ouroboros/cdap.h> #include <ouroboros/dev.h> #include <ouroboros/logs.h> #include <ouroboros/rib.h> -#include <ouroboros/endian.h> #include "ae.h" diff --git a/src/lib/byte_order.h b/src/lib/byte_order.h deleted file mode 100644 index a0c72cf5..00000000 --- a/src/lib/byte_order.h +++ /dev/null @@ -1,111 +0,0 @@ -/* - * Ouroboros - Copyright (C) 2016 - 2017 - * - * Byte order routines for SHA3 function - * - * Dimitri Staessens <[email protected]> - * - * This implementation is adapted and redistributed from the RHASH - * project - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public License - * version 2.1 as published by the Free Software Foundation. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA - * 02110-1301 USA - */ - -/* - * byte_order.h - byte order related platform dependent routines, - * - * Copyright: 2008-2012 Aleksey Kravchenko <[email protected]> - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the - * Software is furnished to do so. - * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY - * or FITNESS FOR A PARTICULAR PURPOSE. Use this program at your own risk! - */ - -#ifndef OUROBOROS_BYTE_ORDER_H -#define OUROBOROS_BYTE_ORDER_H - -#include <ouroboros/endian.h> - -#define IS_ALIGNED_32(p) (0 == (3 & ((const char*)(p) - (const char*)0))) -#define IS_ALIGNED_64(p) (0 == (7 & ((const char*)(p) - (const char*)0))) - -#if defined(__GNUC__) -#define ALIGN_ATTR(n) __attribute__((aligned (n))) -#else -#define ALIGN_ATTR(n) /* nothing */ -#endif - -#define I64(x) x##LL - -/* convert a hash flag to index */ -#if __GNUC__ >= 4 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) /* GCC < 3.4 */ -#define rhash_ctz(x) __builtin_ctz(x) -#else -unsigned rhash_ctz(unsigned); /* define as function */ -#endif - -#ifdef CPU_BIG_ENDIAN -#define be2me_32(x) (x) -#define be2me_64(x) (x) -#define le2me_32(x) bswap_32(x) -#define le2me_64(x) bswap_64(x) - -#define be32_copy(to, index, from, length) \ - memcpy((to) + (index), (from), (length)) -#define le32_copy(to, index, from, length) \ - rhash_swap_copy_str_to_u32((to), (index), (from), (length)) -#define be64_copy(to, index, from, length) \ - memcpy((to) + (index), (from), (length)) -#define le64_copy(to, index, from, length) \ - rhash_swap_copy_str_to_u64((to), (index), (from), (length)) -#define me64_to_be_str(to, from, length) \ - memcpy((to), (from), (length)) -#define me64_to_le_str(to, from, length) \ - rhash_swap_copy_u64_to_str((to), (from), (length)) - -#else /* CPU_BIG_ENDIAN */ -#define be2me_32(x) bswap_32(x) -#define be2me_64(x) bswap_64(x) -#define le2me_32(x) (x) -#define le2me_64(x) (x) - -#define be32_copy(to, index, from, length) \ - rhash_swap_copy_str_to_u32((to), (index), (from), (length)) -#define le32_copy(to, index, from, length) \ - memcpy((to) + (index), (from), (length)) -#define be64_copy(to, index, from, length) \ - rhash_swap_copy_str_to_u64((to), (index), (from), (length)) -#define le64_copy(to, index, from, length) \ - memcpy((to) + (index), (from), (length)) -#define me64_to_be_str(to, from, length) \ - rhash_swap_copy_u64_to_str((to), (from), (length)) -#define me64_to_le_str(to, from, length) \ - memcpy((to), (from), (length)) -#endif /* CPU_BIG_ENDIAN */ - -/* ROTL/ROTR macros rotate a 32/64-bit word left/right by n bits */ -#define ROTL32(dword, n) ((dword) << (n) ^ ((dword) >> (32 - (n)))) -#define ROTR32(dword, n) ((dword) >> (n) ^ ((dword) << (32 - (n)))) -#define ROTL64(qword, n) ((qword) << (n) ^ ((qword) >> (64 - (n)))) -#define ROTR64(qword, n) ((qword) >> (n) ^ ((qword) << (64 - (n)))) - -#endif /* OUROBOROS_BYTE_ORDER_H */ diff --git a/src/lib/sha3.c b/src/lib/sha3.c index 4d9b9b8c..b2f9de57 100644 --- a/src/lib/sha3.c +++ b/src/lib/sha3.c @@ -40,11 +40,16 @@ * or FITNESS FOR A PARTICULAR PURPOSE. Use this program at your own risk! */ +#include <ouroboros/endian.h> + #include <assert.h> #include <string.h> #include "sha3.h" -#include "byte_order.h" + +#define IS_ALIGNED_64(p) (0 == (7 & ((const char*) (p) - (const char*) 0))) +#define I64(x) x##LL +#define ROTL64(qword, n) ((qword) << (n) ^ ((qword) >> (64 - (n)))) #define NumberOfRounds 24 @@ -207,40 +212,40 @@ static void rhash_sha3_process_block(uint64_t hash[25], size_t block_size) { /* expanded loop */ - hash[ 0] ^= le2me_64(block[ 0]); - hash[ 1] ^= le2me_64(block[ 1]); - hash[ 2] ^= le2me_64(block[ 2]); - hash[ 3] ^= le2me_64(block[ 3]); - hash[ 4] ^= le2me_64(block[ 4]); - hash[ 5] ^= le2me_64(block[ 5]); - hash[ 6] ^= le2me_64(block[ 6]); - hash[ 7] ^= le2me_64(block[ 7]); - hash[ 8] ^= le2me_64(block[ 8]); + hash[ 0] ^= htole64(block[ 0]); + hash[ 1] ^= htole64(block[ 1]); + hash[ 2] ^= htole64(block[ 2]); + hash[ 3] ^= htole64(block[ 3]); + hash[ 4] ^= htole64(block[ 4]); + hash[ 5] ^= htole64(block[ 5]); + hash[ 6] ^= htole64(block[ 6]); + hash[ 7] ^= htole64(block[ 7]); + hash[ 8] ^= htole64(block[ 8]); /* if not sha3-512 */ if (block_size > 72) { - hash[ 9] ^= le2me_64(block[ 9]); - hash[10] ^= le2me_64(block[10]); - hash[11] ^= le2me_64(block[11]); - hash[12] ^= le2me_64(block[12]); + hash[ 9] ^= htole64(block[ 9]); + hash[10] ^= htole64(block[10]); + hash[11] ^= htole64(block[11]); + hash[12] ^= htole64(block[12]); /* if not sha3-384 */ if (block_size > 104) { - hash[13] ^= le2me_64(block[13]); - hash[14] ^= le2me_64(block[14]); - hash[15] ^= le2me_64(block[15]); - hash[16] ^= le2me_64(block[16]); + hash[13] ^= htole64(block[13]); + hash[14] ^= htole64(block[14]); + hash[15] ^= htole64(block[15]); + hash[16] ^= htole64(block[16]); /* if not sha3-256 */ if (block_size > 136) { - hash[17] ^= le2me_64(block[17]); + hash[17] ^= htole64(block[17]); #ifdef FULL_SHA3_FAMILY_SUPPORT /* if not sha3-224 */ if (block_size > 144) { - hash[18] ^= le2me_64(block[18]); - hash[19] ^= le2me_64(block[19]); - hash[20] ^= le2me_64(block[20]); - hash[21] ^= le2me_64(block[21]); - hash[22] ^= le2me_64(block[22]); - hash[23] ^= le2me_64(block[23]); - hash[24] ^= le2me_64(block[24]); + hash[18] ^= htole64(block[18]); + hash[19] ^= htole64(block[19]); + hash[20] ^= htole64(block[20]); + hash[21] ^= htole64(block[21]); + hash[22] ^= htole64(block[22]); + hash[23] ^= htole64(block[23]); + hash[24] ^= htole64(block[24]); } #endif } @@ -301,8 +306,9 @@ void rhash_sha3_update(struct sha3_ctx * ctx, void rhash_sha3_final(struct sha3_ctx * ctx, uint8_t * res) { - size_t digest_length = 100 - ctx->block_size / 2; + size_t digest_length = 100 - ctx->block_size / 2; const size_t block_size = ctx->block_size; + unsigned int i = 0; if (!(ctx->rest & SHA3_FINALIZED)) { /* clear the rest of the data queue */ @@ -318,6 +324,10 @@ void rhash_sha3_final(struct sha3_ctx * ctx, assert(block_size > digest_length); - if (res != NULL) - me64_to_le_str(res, ctx->hash, digest_length); + if (res != NULL) { + for (i = 0; i < digest_length; i++) + ctx->hash[i] = htole64(ctx->hash[i]); + + memcpy(res, ctx->hash, digest_length); + } } |