1 #ifndef CRYPTONITE_ALIGN_H
2 #define CRYPTONITE_ALIGN_H
3
4 #include "cryptonite_bitfn.h"
5
6 #if (defined(__i386__))
7 # define UNALIGNED_ACCESS_OK
8 #elif defined(__x86_64__)
9 # define UNALIGNED_ACCESS_OK
10 #else
11 # define UNALIGNED_ACCESS_FAULT
12 #endif
13
14 /* n need to be power of 2.
15 * IS_ALIGNED(p,8) */
16 #define IS_ALIGNED(p,alignment) (((uintptr_t) (p)) & ((alignment)-1))
17
18 #ifdef WITH_ASSERT_ALIGNMENT
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <inttypes.h>
22 # define ASSERT_ALIGNMENT(up, alignment) \
23 do { if (IS_ALIGNED(up, alignment)) \
24 { printf("ALIGNMENT-ASSERT-FAILURE: %s:%d: ptr=%p alignment=%d\n", __FILE__, __LINE__, (void *) up, (alignment)); \
25 exit(99); \
26 }; } while (0)
27 #else
28 # define ASSERT_ALIGNMENT(p, n) do {} while (0)
29 #endif
30
31 #ifdef UNALIGNED_ACCESS_OK
32 #define need_alignment(p,n) (0)
33 #else
34 #define need_alignment(p,n) IS_ALIGNED(p,n)
35 #endif
36
load_le32_aligned(const uint8_t * p)37 static inline uint32_t load_le32_aligned(const uint8_t *p)
38 {
39 return le32_to_cpu(*((uint32_t *) p));
40 }
41
store_le32_aligned(uint8_t * dst,const uint32_t v)42 static inline void store_le32_aligned(uint8_t *dst, const uint32_t v)
43 {
44 *((uint32_t *) dst) = cpu_to_le32(v);
45 }
46
store_be32_aligned(uint8_t * dst,const uint32_t v)47 static inline void store_be32_aligned(uint8_t *dst, const uint32_t v)
48 {
49 *((uint32_t *) dst) = cpu_to_be32(v);
50 }
51
store_le64_aligned(uint8_t * dst,const uint64_t v)52 static inline void store_le64_aligned(uint8_t *dst, const uint64_t v)
53 {
54 *((uint64_t *) dst) = cpu_to_le64(v);
55 }
56
store_be64_aligned(uint8_t * dst,const uint64_t v)57 static inline void store_be64_aligned(uint8_t *dst, const uint64_t v)
58 {
59 *((uint64_t *) dst) = cpu_to_be64(v);
60 }
61
62 #ifdef UNALIGNED_ACCESS_OK
63 #define load_le32(a) load_le32_aligned(a)
64 #else
load_le32(const uint8_t * p)65 static inline uint32_t load_le32(const uint8_t *p)
66 {
67 return ((uint32_t)p[0]) | ((uint32_t)p[1] << 8) | ((uint32_t)p[2] << 16) | ((uint32_t)p[3] << 24);
68 }
69 #endif
70
71 #ifdef UNALIGNED_ACCESS_OK
72 #define store_le32(a, b) store_le32_aligned(a, b)
73 #else
store_le32(uint8_t * dst,const uint32_t v)74 static inline void store_le32(uint8_t *dst, const uint32_t v)
75 {
76 dst[0] = v; dst[1] = v >> 8; dst[2] = v >> 16; dst[3] = v >> 24;
77 }
78 #endif
79
80 #ifdef UNALIGNED_ACCESS_OK
81 #define store_be32(a, b) store_be32_aligned(a, b)
82 #else
store_be32(uint8_t * dst,const uint32_t v)83 static inline void store_be32(uint8_t *dst, const uint32_t v)
84 {
85 dst[3] = v; dst[2] = v >> 8; dst[1] = v >> 16; dst[0] = v >> 24;
86 }
87 #endif
88
89 #ifdef UNALIGNED_ACCESS_OK
90 #define store_le64(a, b) store_le64_aligned(a, b)
91 #else
store_le64(uint8_t * dst,const uint64_t v)92 static inline void store_le64(uint8_t *dst, const uint64_t v)
93 {
94 dst[0] = v ; dst[1] = v >> 8 ; dst[2] = v >> 16; dst[3] = v >> 24;
95 dst[4] = v >> 32; dst[5] = v >> 40; dst[6] = v >> 48; dst[7] = v >> 56;
96 }
97 #endif
98
99 #ifdef UNALIGNED_ACCESS_OK
100 #define store_be64(a, b) store_be64_aligned(a, b)
101 #else
store_be64(uint8_t * dst,const uint64_t v)102 static inline void store_be64(uint8_t *dst, const uint64_t v)
103 {
104 dst[7] = v ; dst[6] = v >> 8 ; dst[5] = v >> 16; dst[4] = v >> 24;
105 dst[3] = v >> 32; dst[2] = v >> 40; dst[1] = v >> 48; dst[0] = v >> 56;
106 }
107 #endif
108
109 #endif
110