• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * datatypes_driver.c
3  *
4  * a test driver for crypto/math datatypes
5  *
6  * David A. McGrew
7  * Cisco Systems, Inc.
8  */
9 
10 /*
11  *
12  * Copyright (c) 2001-2017, Cisco Systems, Inc.
13  * All rights reserved.
14  *
15  * Redistribution and use in source and binary forms, with or without
16  * modification, are permitted provided that the following conditions
17  * are met:
18  *
19  *   Redistributions of source code must retain the above copyright
20  *   notice, this list of conditions and the following disclaimer.
21  *
22  *   Redistributions in binary form must reproduce the above
23  *   copyright notice, this list of conditions and the following
24  *   disclaimer in the documentation and/or other materials provided
25  *   with the distribution.
26  *
27  *   Neither the name of the Cisco Systems, Inc. nor the names of its
28  *   contributors may be used to endorse or promote products derived
29  *   from this software without specific prior written permission.
30  *
31  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
34  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
35  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
36  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
37  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
38  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
41  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
42  * OF THE POSSIBILITY OF SUCH DAMAGE.
43  *
44  */
45 
46 #ifdef HAVE_CONFIG_H
47 #include <config.h>
48 #endif
49 
50 #include <stdio.h>  /* for printf() */
51 #include <string.h> /* for strlen() */
52 #include "datatypes.h"
53 #include "util.h"
54 
55 void byte_order(void);
56 
57 void test_hex_string_funcs(void);
58 
59 void print_string(char *s);
60 
61 void test_bswap(void);
62 
63 void test_set_to_zero(void);
64 
main(void)65 int main(void)
66 {
67     /*
68      * this program includes various and sundry tests for fundamental
69      * datatypes.  it's a grab-bag of throwaway code, retained only in
70      * case of future problems
71      */
72 
73     int i, j;
74     v128_t x;
75     char *r = "The Moving Finger writes; and, having writ,\n"
76               "Moves on: nor all thy Piety nor Wit\n"
77               "Shall lure it back to cancel half a Line,\n"
78               "Nor all thy Tears wash out a Word of it.";
79     char *s = "incomplet";
80 
81     print_string(r);
82     print_string(s);
83 
84     byte_order();
85     test_hex_string_funcs();
86 
87     for (j = 0; j < 128; j++) {
88         v128_set_to_zero(&x);
89         /*      x.v32[0] = (1 << j); */
90         v128_set_bit(&x, j);
91         printf("%s\n", v128_bit_string(&x));
92         v128_clear_bit(&x, j);
93         printf("%s\n", v128_bit_string(&x));
94     }
95 
96     printf("----------------------------------------------\n");
97     v128_set_to_zero(&x);
98     for (i = 0; i < 128; i++) {
99         v128_set_bit(&x, i);
100     }
101     printf("%s\n", v128_bit_string(&x));
102 
103     printf("----------------------------------------------\n");
104     v128_set_to_zero(&x);
105     v128_set_bit(&x, 0);
106     for (i = 0; i < 128; i++) {
107         printf("%s\n", v128_bit_string(&x));
108         v128_right_shift(&x, 1);
109     }
110     printf("----------------------------------------------\n");
111     v128_set_to_zero(&x);
112     v128_set_bit(&x, 127);
113     for (i = 0; i < 128; i++) {
114         printf("%s\n", v128_bit_string(&x));
115         v128_left_shift(&x, 1);
116     }
117     printf("----------------------------------------------\n");
118     for (i = 0; i < 128; i++) {
119         v128_set_to_zero(&x);
120         v128_set_bit(&x, 127);
121         v128_left_shift(&x, i);
122         printf("%s\n", v128_bit_string(&x));
123     }
124     printf("----------------------------------------------\n");
125     v128_set_to_zero(&x);
126     for (i = 0; i < 128; i += 2) {
127         v128_set_bit(&x, i);
128     }
129     printf("bit_string: { %s }\n", v128_bit_string(&x));
130     printf("get_bit:    { ");
131     for (i = 0; i < 128; i++) {
132         if (v128_get_bit(&x, i) == 1)
133             printf("1");
134         else
135             printf("0");
136     }
137     printf(" } \n");
138 
139     test_bswap();
140     test_set_to_zero();
141 
142     return 0;
143 }
144 
145 /* byte_order() prints out byte ordering of datatypes */
146 
byte_order(void)147 void byte_order(void)
148 {
149     int i;
150     v128_t e;
151 #if 0
152   v16_t b;
153   v32_t c;
154   v64_t d;
155 
156   for (i=0; i < sizeof(b); i++)
157     b.octet[i] = i;
158   for (i=0; i < sizeof(c); i++)
159     c.octet[i] = i;
160   for (i=0; i < sizeof(d); i++)
161     d.octet[i] = i;
162 
163   printf("v128_t:\t%s\n", v128_hex_string(&e));
164   printf("v64_t:\t%s\n", v64_hex_string(&d));
165   printf("v32_t:\t%s\n", v32_hex_string(c));
166   printf("v16_t:\t%s\n", v16_hex_string(b));
167 
168   c.value = 0x01020304;
169   printf("v32_t:\t%s\n", v32_hex_string(c));
170   b.value = 0x0102;
171   printf("v16_t:\t%s\n", v16_hex_string(b));
172 
173   printf("uint16_t ordering:\n");
174 
175   c.value = 0x00010002;
176   printf("v32_t:\t%x%x\n", c.v16[0], c.v16[1]);
177 #endif
178 
179     printf("byte ordering of crypto/math datatypes:\n");
180     for (i = 0; i < sizeof(e); i++)
181         e.v8[i] = i;
182     printf("v128_t: %s\n", v128_hex_string(&e));
183 }
184 
test_hex_string_funcs(void)185 void test_hex_string_funcs(void)
186 {
187     char hex1[] = "abadcafe";
188     char hex2[] = "0123456789abcdefqqqqq";
189     char raw[10];
190     int len;
191 
192     len = hex_string_to_octet_string(raw, hex1, strlen(hex1));
193     printf("computed length: %d\tstring: %s\n", len,
194            octet_string_hex_string(raw, len / 2));
195     printf("expected length: %u\tstring: %s\n", (unsigned)strlen(hex1), hex1);
196 
197     len = hex_string_to_octet_string(raw, hex2, strlen(hex2));
198     printf("computed length: %d\tstring: %s\n", len,
199            octet_string_hex_string(raw, len / 2));
200     printf("expected length: %d\tstring: %s\n", 16, "0123456789abcdef");
201 }
202 
print_string(char * s)203 void print_string(char *s)
204 {
205     size_t i;
206     printf("%s\n", s);
207     printf("strlen(s) = %u\n", (unsigned)strlen(s));
208     printf("{ ");
209     for (i = 0; i < strlen(s); i++) {
210         printf("0x%x, ", s[i]);
211         if (((i + 1) % 8) == 0)
212             printf("\n   ");
213     }
214     printf("}\n");
215 }
216 
test_bswap(void)217 void test_bswap(void)
218 {
219     uint32_t x = 0x11223344;
220     uint64_t y = 0x1122334455667788LL;
221 
222     printf("before: %0x\nafter:  %0x\n", x, (unsigned int)be32_to_cpu(x));
223     printf("before: %0llx\nafter:  %0llx\n", (unsigned long long)y,
224            (unsigned long long)be64_to_cpu(y));
225 
226     y = 1234;
227 
228     printf("1234: %0llx\n", (unsigned long long)y);
229     printf("as octet string: %s\n", octet_string_hex_string((uint8_t *)&y, 8));
230     y = be64_to_cpu(y);
231     printf("bswapped octet string: %s\n",
232            octet_string_hex_string((uint8_t *)&y, 8));
233 }
234 
test_set_to_zero(void)235 void test_set_to_zero(void)
236 {
237 #define BUFFER_SIZE (16)
238     uint8_t buffer[BUFFER_SIZE];
239     size_t i;
240 
241     for (i = 0; i < BUFFER_SIZE; i++) {
242         buffer[i] = i & 0xff;
243     }
244     printf("Buffer before: %s\n", octet_string_hex_string(buffer, BUFFER_SIZE));
245     octet_string_set_to_zero(buffer, BUFFER_SIZE);
246     printf("Buffer after: %s\n", octet_string_hex_string(buffer, BUFFER_SIZE));
247     for (i = 0; i < BUFFER_SIZE; i++) {
248         if (buffer[i]) {
249             fprintf(stderr,
250                     "Buffer contents not zero at position %zu (is %d)\n", i,
251                     buffer[i]);
252             abort();
253         }
254     }
255 #undef BUFFER_SIZE
256 }
257