• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
50 unsigned char
51 v32_weight(v32_t a);
52 
53 unsigned char
54 v32_distance(v32_t x, v32_t y);
55 
56 unsigned int
57 v32_dot_product(v32_t a, v32_t b);
58 
59 char *
60 v16_bit_string(v16_t x);
61 
62 char *
63 v32_bit_string(v32_t x);
64 
65 char *
66 v64_bit_string(const v64_t *x);
67 
68 char *
69 octet_hex_string(uint8_t x);
70 
71 char *
72 v16_hex_string(v16_t x);
73 
74 char *
75 v32_hex_string(v32_t x);
76 
77 char *
78 v64_hex_string(const v64_t *x);
79 
80 int
81 hex_char_to_nibble(uint8_t c);
82 
83 int
84 is_hex_string(char *s);
85 
86 v16_t
87 hex_string_to_v16(char *s);
88 
89 v32_t
90 hex_string_to_v32(char *s);
91 
92 v64_t
93 hex_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 
98 uint8_t
99 A_times_x_plus_b(uint8_t A[8], uint8_t x, uint8_t b);
100 
101 void
102 v16_copy_octet_string(v16_t *x, const uint8_t s[2]);
103 
104 void
105 v32_copy_octet_string(v32_t *x, const uint8_t s[4]);
106 
107 void
108 v64_copy_octet_string(v64_t *x, const uint8_t s[8]);
109 
110 void
111 v128_add(v128_t *z, v128_t *x, v128_t *y);
112 
113 int
114 octet_string_is_eq(uint8_t *a, uint8_t *b, int len);
115 
116 void
117 octet_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 */
125 uint8_t
126 A_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 
189 void
190 v128_set_to_zero(v128_t *x);
191 
192 int
193 v128_is_eq(const v128_t *x, const v128_t *y);
194 
195 void
196 v128_copy(v128_t *x, const v128_t *y);
197 
198 void
199 v128_xor(v128_t *z, v128_t *x, v128_t *y);
200 
201 void
202 v128_and(v128_t *z, v128_t *x, v128_t *y);
203 
204 void
205 v128_or(v128_t *z, v128_t *x, v128_t *y);
206 
207 void
208 v128_complement(v128_t *x);
209 
210 int
211 v128_get_bit(const v128_t *x, int i);
212 
213 void
214 v128_set_bit(v128_t *x, int i) ;
215 
216 void
217 v128_clear_bit(v128_t *x, int i);
218 
219 void
220 v128_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 
229 int
230 octet_string_is_eq(uint8_t *a, uint8_t *b, int len);
231 
232 void
233 octet_string_set_to_zero(uint8_t *s, int len);
234 
235 
236 #endif /* MATH_H */
237 
238 
239 
240