1/*
2 * math.h
3 *
4 * crypto math operations and data types
5 *
6 * David A. McGrew
7 * Cisco Systems, Inc.
8 */
9/*
10 *
11 * Copyright (c) 2001-2006 Cisco Systems, Inc.
12 * All rights reserved.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 *
18 *   Redistributions of source code must retain the above copyright
19 *   notice, this list of conditions and the following disclaimer.
20 *
21 *   Redistributions in binary form must reproduce the above
22 *   copyright notice, this list of conditions and the following
23 *   disclaimer in the documentation and/or other materials provided
24 *   with the distribution.
25 *
26 *   Neither the name of the Cisco Systems, Inc. nor the names of its
27 *   contributors may be used to endorse or promote products derived
28 *   from this software without specific prior written permission.
29 *
30 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
31 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
32 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
33 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
34 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
35 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
36 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
37 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
41 * OF THE POSSIBILITY OF SUCH DAMAGE.
42 *
43 */
44
45#ifndef MATH_H
46#define MATH_H
47
48#include "datatypes.h"
49
50unsigned char
51v32_weight(v32_t a);
52
53unsigned char
54v32_distance(v32_t x, v32_t y);
55
56unsigned int
57v32_dot_product(v32_t a, v32_t b);
58
59char *
60v16_bit_string(v16_t x);
61
62char *
63v32_bit_string(v32_t x);
64
65char *
66v64_bit_string(const v64_t *x);
67
68char *
69octet_hex_string(uint8_t x);
70
71char *
72v16_hex_string(v16_t x);
73
74char *
75v32_hex_string(v32_t x);
76
77char *
78v64_hex_string(const v64_t *x);
79
80int
81hex_char_to_nibble(uint8_t c);
82
83int
84is_hex_string(char *s);
85
86v16_t
87hex_string_to_v16(char *s);
88
89v32_t
90hex_string_to_v32(char *s);
91
92v64_t
93hex_string_to_v64(char *s);
94
95/* the matrix A[] is stored in column format, i.e., A[i] is
96   the ith column of the matrix */
97
98uint8_t
99A_times_x_plus_b(uint8_t A[8], uint8_t x, uint8_t b);
100
101void
102v16_copy_octet_string(v16_t *x, const uint8_t s[2]);
103
104void
105v32_copy_octet_string(v32_t *x, const uint8_t s[4]);
106
107void
108v64_copy_octet_string(v64_t *x, const uint8_t s[8]);
109
110void
111v128_add(v128_t *z, v128_t *x, v128_t *y);
112
113int
114octet_string_is_eq(uint8_t *a, uint8_t *b, int len);
115
116void
117octet_string_set_to_zero(uint8_t *s, int len);
118
119
120
121/*
122 * the matrix A[] is stored in column format, i.e., A[i] is the ith
123 * column of the matrix
124*/
125uint8_t
126A_times_x_plus_b(uint8_t A[8], uint8_t x, uint8_t b);
127
128
129#if 0
130#if WORDS_BIGENDIAN
131
132#define _v128_add(z, x, y) {                    \
133  uint64_t tmp;					\
134    						\
135  tmp = x->v32[3] + y->v32[3];                  \
136  z->v32[3] = (uint32_t) tmp;			\
137  						\
138  tmp =  x->v32[2] + y->v32[2] + (tmp >> 32);	\
139  z->v32[2] = (uint32_t) tmp;                   \
140						\
141  tmp =  x->v32[1] + y->v32[1] + (tmp >> 32);	\
142  z->v32[1] = (uint32_t) tmp;			\
143                                                \
144  tmp =  x->v32[0] + y->v32[0] + (tmp >> 32);	\
145  z->v32[0] = (uint32_t) tmp;			\
146}
147
148#else /* assume little endian architecture */
149
150#define _v128_add(z, x, y) {                    \
151  uint64_t tmp;					\
152						\
153  tmp = htonl(x->v32[3]) + htonl(y->v32[3]);	\
154  z->v32[3] = ntohl((uint32_t) tmp);		\
155  						\
156  tmp =  htonl(x->v32[2]) + htonl(y->v32[2])	\
157       + htonl(tmp >> 32);			\
158  z->v32[2] = ntohl((uint32_t) tmp);		\
159                                                \
160  tmp =  htonl(x->v32[1]) + htonl(y->v32[1])	\
161       + htonl(tmp >> 32);			\
162  z->v32[1] = ntohl((uint32_t) tmp);		\
163  						\
164  tmp =  htonl(x->v32[0]) + htonl(y->v32[0])	\
165       + htonl(tmp >> 32);			\
166  z->v32[0] = ntohl((uint32_t) tmp);		\
167}
168
169#endif /* WORDS_BIGENDIAN */
170#endif
171
172#ifdef DATATYPES_USE_MACROS  /* little functions are really macros */
173
174#define v128_set_to_zero(z)       _v128_set_to_zero(z)
175#define v128_copy(z, x)           _v128_copy(z, x)
176#define v128_xor(z, x, y)         _v128_xor(z, x, y)
177#define v128_and(z, x, y)         _v128_and(z, x, y)
178#define v128_or(z, x, y)          _v128_or(z, x, y)
179#define v128_complement(x)        _v128_complement(x)
180#define v128_is_eq(x, y)          _v128_is_eq(x, y)
181#define v128_xor_eq(x, y)         _v128_xor_eq(x, y)
182#define v128_get_bit(x, i)        _v128_get_bit(x, i)
183#define v128_set_bit(x, i)        _v128_set_bit(x, i)
184#define v128_clear_bit(x, i)      _v128_clear_bit(x, i)
185#define v128_set_bit_to(x, i, y)  _v128_set_bit_to(x, i, y)
186
187#else
188
189void
190v128_set_to_zero(v128_t *x);
191
192int
193v128_is_eq(const v128_t *x, const v128_t *y);
194
195void
196v128_copy(v128_t *x, const v128_t *y);
197
198void
199v128_xor(v128_t *z, v128_t *x, v128_t *y);
200
201void
202v128_and(v128_t *z, v128_t *x, v128_t *y);
203
204void
205v128_or(v128_t *z, v128_t *x, v128_t *y);
206
207void
208v128_complement(v128_t *x);
209
210int
211v128_get_bit(const v128_t *x, int i);
212
213void
214v128_set_bit(v128_t *x, int i) ;
215
216void
217v128_clear_bit(v128_t *x, int i);
218
219void
220v128_set_bit_to(v128_t *x, int i, int y);
221
222#endif /* DATATYPES_USE_MACROS */
223
224/*
225 * octet_string_is_eq(a,b, len) returns 1 if the length len strings a
226 * and b are not equal, returns 0 otherwise
227 */
228
229int
230octet_string_is_eq(uint8_t *a, uint8_t *b, int len);
231
232void
233octet_string_set_to_zero(uint8_t *s, int len);
234
235
236#endif /* MATH_H */
237
238
239
240