1 /*
2  * Byte stream functions
3  *
4  * Copyright (C) 2011-2020, Joachim Metz <joachim.metz@gmail.com>
5  *
6  * Refer to AUTHORS for acknowledgements.
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
20  */
21 
22 #if !defined( _LIBHMAC_BYTE_STREAM_H )
23 #define _LIBHMAC_BYTE_STREAM_H
24 
25 #include <common.h>
26 #include <byte_stream.h>
27 
28 #if defined( __cplusplus )
29 extern "C" {
30 #endif
31 
32 #define libhmac_byte_stream_copy_to_1x_uint32_big_endian( byte_stream, values, values_index ) \
33 	values[ values_index ]   = ( byte_stream )[ ( values_index << 2 ) ]; \
34 	values[ values_index ] <<= 8; \
35 	values[ values_index ]  |= ( byte_stream )[ ( values_index << 2 ) + 1 ]; \
36 	values[ values_index ] <<= 8; \
37 	values[ values_index ]  |= ( byte_stream )[ ( values_index << 2 ) + 2 ]; \
38 	values[ values_index ] <<= 8; \
39 	values[ values_index ]  |= ( byte_stream )[ ( values_index << 2 ) + 3 ];
40 
41 #define libhmac_byte_stream_copy_to_1x_uint32_little_endian( byte_stream, values, values_index ) \
42 	values[ values_index ]   = ( byte_stream )[ ( values_index << 2 ) + 3 ]; \
43 	values[ values_index ] <<= 8; \
44 	values[ values_index ]  |= ( byte_stream )[ ( values_index << 2 ) + 2 ]; \
45 	values[ values_index ] <<= 8; \
46 	values[ values_index ]  |= ( byte_stream )[ ( values_index << 2 ) + 1 ]; \
47 	values[ values_index ] <<= 8; \
48 	values[ values_index ]  |= ( byte_stream )[ ( values_index << 2 ) ];
49 
50 #define libhmac_byte_stream_copy_to_16x_uint32_big_endian( byte_stream, values ) \
51 	libhmac_byte_stream_copy_to_1x_uint32_big_endian( byte_stream, values, 0 ) \
52 	libhmac_byte_stream_copy_to_1x_uint32_big_endian( byte_stream, values, 1 ) \
53 	libhmac_byte_stream_copy_to_1x_uint32_big_endian( byte_stream, values, 2 ) \
54 	libhmac_byte_stream_copy_to_1x_uint32_big_endian( byte_stream, values, 3 ) \
55 	libhmac_byte_stream_copy_to_1x_uint32_big_endian( byte_stream, values, 4 ) \
56 	libhmac_byte_stream_copy_to_1x_uint32_big_endian( byte_stream, values, 5 ) \
57 	libhmac_byte_stream_copy_to_1x_uint32_big_endian( byte_stream, values, 6 ) \
58 	libhmac_byte_stream_copy_to_1x_uint32_big_endian( byte_stream, values, 7 ) \
59 	libhmac_byte_stream_copy_to_1x_uint32_big_endian( byte_stream, values, 8 ) \
60 	libhmac_byte_stream_copy_to_1x_uint32_big_endian( byte_stream, values, 9 ) \
61 	libhmac_byte_stream_copy_to_1x_uint32_big_endian( byte_stream, values, 10 ) \
62 	libhmac_byte_stream_copy_to_1x_uint32_big_endian( byte_stream, values, 11 ) \
63 	libhmac_byte_stream_copy_to_1x_uint32_big_endian( byte_stream, values, 12 ) \
64 	libhmac_byte_stream_copy_to_1x_uint32_big_endian( byte_stream, values, 13 ) \
65 	libhmac_byte_stream_copy_to_1x_uint32_big_endian( byte_stream, values, 14 ) \
66 	libhmac_byte_stream_copy_to_1x_uint32_big_endian( byte_stream, values, 15 )
67 
68 #define libhmac_byte_stream_copy_to_16x_uint32_little_endian( byte_stream, values ) \
69 	libhmac_byte_stream_copy_to_1x_uint32_little_endian( byte_stream, values, 0 ) \
70 	libhmac_byte_stream_copy_to_1x_uint32_little_endian( byte_stream, values, 1 ) \
71 	libhmac_byte_stream_copy_to_1x_uint32_little_endian( byte_stream, values, 2 ) \
72 	libhmac_byte_stream_copy_to_1x_uint32_little_endian( byte_stream, values, 3 ) \
73 	libhmac_byte_stream_copy_to_1x_uint32_little_endian( byte_stream, values, 4 ) \
74 	libhmac_byte_stream_copy_to_1x_uint32_little_endian( byte_stream, values, 5 ) \
75 	libhmac_byte_stream_copy_to_1x_uint32_little_endian( byte_stream, values, 6 ) \
76 	libhmac_byte_stream_copy_to_1x_uint32_little_endian( byte_stream, values, 7 ) \
77 	libhmac_byte_stream_copy_to_1x_uint32_little_endian( byte_stream, values, 8 ) \
78 	libhmac_byte_stream_copy_to_1x_uint32_little_endian( byte_stream, values, 9 ) \
79 	libhmac_byte_stream_copy_to_1x_uint32_little_endian( byte_stream, values, 10 ) \
80 	libhmac_byte_stream_copy_to_1x_uint32_little_endian( byte_stream, values, 11 ) \
81 	libhmac_byte_stream_copy_to_1x_uint32_little_endian( byte_stream, values, 12 ) \
82 	libhmac_byte_stream_copy_to_1x_uint32_little_endian( byte_stream, values, 13 ) \
83 	libhmac_byte_stream_copy_to_1x_uint32_little_endian( byte_stream, values, 14 ) \
84 	libhmac_byte_stream_copy_to_1x_uint32_little_endian( byte_stream, values, 15 )
85 
86 #define libhmac_byte_stream_copy_to_1x_uint64_big_endian( byte_stream, values, values_index ) \
87 	values[ values_index ]   = ( byte_stream )[ ( values_index << 3 ) ]; \
88 	values[ values_index ] <<= 8; \
89 	values[ values_index ]  |= ( byte_stream )[ ( values_index << 3 ) + 1 ]; \
90 	values[ values_index ] <<= 8; \
91 	values[ values_index ]  |= ( byte_stream )[ ( values_index << 3 ) + 2 ]; \
92 	values[ values_index ] <<= 8; \
93 	values[ values_index ]  |= ( byte_stream )[ ( values_index << 3 ) + 3 ]; \
94 	values[ values_index ] <<= 8; \
95 	values[ values_index ]  |= ( byte_stream )[ ( values_index << 3 ) + 4 ]; \
96 	values[ values_index ] <<= 8; \
97 	values[ values_index ]  |= ( byte_stream )[ ( values_index << 3 ) + 5 ]; \
98 	values[ values_index ] <<= 8; \
99 	values[ values_index ]  |= ( byte_stream )[ ( values_index << 3 ) + 6 ]; \
100 	values[ values_index ] <<= 8; \
101 	values[ values_index ]  |= ( byte_stream )[ ( values_index << 3 ) + 7 ];
102 
103 #define libhmac_byte_stream_copy_to_1x_uint64_little_endian( byte_stream, values, values_index ) \
104 	values[ values_index ]   = ( byte_stream )[ ( values_index << 3 ) + 7 ]; \
105 	values[ values_index ] <<= 8; \
106 	values[ values_index ]  |= ( byte_stream )[ ( values_index << 3 ) + 6 ]; \
107 	values[ values_index ] <<= 8; \
108 	values[ values_index ]  |= ( byte_stream )[ ( values_index << 3 ) + 5 ]; \
109 	values[ values_index ] <<= 8; \
110 	values[ values_index ]  |= ( byte_stream )[ ( values_index << 3 ) + 4 ]; \
111 	values[ values_index ] <<= 8; \
112 	values[ values_index ]   = ( byte_stream )[ ( values_index << 3 ) + 3 ]; \
113 	values[ values_index ] <<= 8; \
114 	values[ values_index ]  |= ( byte_stream )[ ( values_index << 3 ) + 2 ]; \
115 	values[ values_index ] <<= 8; \
116 	values[ values_index ]  |= ( byte_stream )[ ( values_index << 3 ) + 1 ]; \
117 	values[ values_index ] <<= 8; \
118 	values[ values_index ]  |= ( byte_stream )[ ( values_index << 3 ) ];
119 
120 #define libhmac_byte_stream_copy_to_16x_uint64_big_endian( byte_stream, values ) \
121 	libhmac_byte_stream_copy_to_1x_uint64_big_endian( byte_stream, values, 0 ) \
122 	libhmac_byte_stream_copy_to_1x_uint64_big_endian( byte_stream, values, 1 ) \
123 	libhmac_byte_stream_copy_to_1x_uint64_big_endian( byte_stream, values, 2 ) \
124 	libhmac_byte_stream_copy_to_1x_uint64_big_endian( byte_stream, values, 3 ) \
125 	libhmac_byte_stream_copy_to_1x_uint64_big_endian( byte_stream, values, 4 ) \
126 	libhmac_byte_stream_copy_to_1x_uint64_big_endian( byte_stream, values, 5 ) \
127 	libhmac_byte_stream_copy_to_1x_uint64_big_endian( byte_stream, values, 6 ) \
128 	libhmac_byte_stream_copy_to_1x_uint64_big_endian( byte_stream, values, 7 ) \
129 	libhmac_byte_stream_copy_to_1x_uint64_big_endian( byte_stream, values, 8 ) \
130 	libhmac_byte_stream_copy_to_1x_uint64_big_endian( byte_stream, values, 9 ) \
131 	libhmac_byte_stream_copy_to_1x_uint64_big_endian( byte_stream, values, 10 ) \
132 	libhmac_byte_stream_copy_to_1x_uint64_big_endian( byte_stream, values, 11 ) \
133 	libhmac_byte_stream_copy_to_1x_uint64_big_endian( byte_stream, values, 12 ) \
134 	libhmac_byte_stream_copy_to_1x_uint64_big_endian( byte_stream, values, 13 ) \
135 	libhmac_byte_stream_copy_to_1x_uint64_big_endian( byte_stream, values, 14 ) \
136 	libhmac_byte_stream_copy_to_1x_uint64_big_endian( byte_stream, values, 15 )
137 
138 #define libhmac_byte_stream_copy_to_16x_uint64_little_endian( byte_stream, values ) \
139 	libhmac_byte_stream_copy_to_1x_uint64_little_endian( byte_stream, values, 0 ) \
140 	libhmac_byte_stream_copy_to_1x_uint64_little_endian( byte_stream, values, 1 ) \
141 	libhmac_byte_stream_copy_to_1x_uint64_little_endian( byte_stream, values, 2 ) \
142 	libhmac_byte_stream_copy_to_1x_uint64_little_endian( byte_stream, values, 3 ) \
143 	libhmac_byte_stream_copy_to_1x_uint64_little_endian( byte_stream, values, 4 ) \
144 	libhmac_byte_stream_copy_to_1x_uint64_little_endian( byte_stream, values, 5 ) \
145 	libhmac_byte_stream_copy_to_1x_uint64_little_endian( byte_stream, values, 6 ) \
146 	libhmac_byte_stream_copy_to_1x_uint64_little_endian( byte_stream, values, 7 ) \
147 	libhmac_byte_stream_copy_to_1x_uint64_little_endian( byte_stream, values, 8 ) \
148 	libhmac_byte_stream_copy_to_1x_uint64_little_endian( byte_stream, values, 9 ) \
149 	libhmac_byte_stream_copy_to_1x_uint64_little_endian( byte_stream, values, 10 ) \
150 	libhmac_byte_stream_copy_to_1x_uint64_little_endian( byte_stream, values, 11 ) \
151 	libhmac_byte_stream_copy_to_1x_uint64_little_endian( byte_stream, values, 12 ) \
152 	libhmac_byte_stream_copy_to_1x_uint64_little_endian( byte_stream, values, 13 ) \
153 	libhmac_byte_stream_copy_to_1x_uint64_little_endian( byte_stream, values, 14 ) \
154 	libhmac_byte_stream_copy_to_1x_uint64_little_endian( byte_stream, values, 15 )
155 
156 #if defined( __cplusplus )
157 }
158 #endif
159 
160 #endif /* !defined( _LIBHMAC_BYTE_STREAM_H ) */
161 
162