| OLD | NEW |
| (Empty) |
| 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-2006, 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 | |
| 47 #ifdef HAVE_CONFIG_H | |
| 48 #include <config.h> | |
| 49 #endif | |
| 50 | |
| 51 #include <stdio.h> /* for printf() */ | |
| 52 #include <string.h> /* for strlen() */ | |
| 53 #include "datatypes.h" | |
| 54 | |
| 55 void | |
| 56 byte_order(void); | |
| 57 | |
| 58 void | |
| 59 test_hex_string_funcs(void); | |
| 60 | |
| 61 void | |
| 62 print_string(char *s); | |
| 63 | |
| 64 void | |
| 65 test_bswap(void); | |
| 66 | |
| 67 int | |
| 68 main (void) { | |
| 69 | |
| 70 /* | |
| 71 * this program includes various and sundry tests for fundamental | |
| 72 * datatypes. it's a grab-bag of throwaway code, retained only in | |
| 73 * case of future problems | |
| 74 */ | |
| 75 | |
| 76 int i, j; | |
| 77 v128_t x; | |
| 78 char *r = | |
| 79 "The Moving Finger writes; and, having writ,\n" | |
| 80 "Moves on: nor all thy Piety nor Wit\n" | |
| 81 "Shall lure it back to cancel half a Line,\n" | |
| 82 "Nor all thy Tears wash out a Word of it."; | |
| 83 char *s = "incomplet"; | |
| 84 | |
| 85 print_string(r); | |
| 86 print_string(s); | |
| 87 | |
| 88 byte_order(); | |
| 89 test_hex_string_funcs(); | |
| 90 | |
| 91 for (j=0; j < 128; j++) { | |
| 92 v128_set_to_zero(&x); | |
| 93 /* x.v32[0] = (1 << j); */ | |
| 94 v128_set_bit(&x, j); | |
| 95 printf("%s\n", v128_bit_string(&x)); | |
| 96 v128_clear_bit(&x, j); | |
| 97 printf("%s\n", v128_bit_string(&x)); | |
| 98 | |
| 99 } | |
| 100 | |
| 101 printf("----------------------------------------------\n"); | |
| 102 v128_set_to_zero(&x); | |
| 103 for (i=0; i < 128; i++) { | |
| 104 v128_set_bit(&x, i); | |
| 105 } | |
| 106 printf("%s\n", v128_bit_string(&x)); | |
| 107 | |
| 108 printf("----------------------------------------------\n"); | |
| 109 v128_set_to_zero(&x); | |
| 110 v128_set_bit(&x, 0); | |
| 111 for (i=0; i < 128; i++) { | |
| 112 printf("%s\n", v128_bit_string(&x)); | |
| 113 v128_right_shift(&x, 1); | |
| 114 } | |
| 115 printf("----------------------------------------------\n"); | |
| 116 v128_set_to_zero(&x); | |
| 117 v128_set_bit(&x, 127); | |
| 118 for (i=0; i < 128; i++) { | |
| 119 printf("%s\n", v128_bit_string(&x)); | |
| 120 v128_left_shift(&x, 1); | |
| 121 } | |
| 122 printf("----------------------------------------------\n"); | |
| 123 for (i=0; i < 128; i++) { | |
| 124 v128_set_to_zero(&x); | |
| 125 v128_set_bit(&x, 127); | |
| 126 v128_left_shift(&x, i); | |
| 127 printf("%s\n", v128_bit_string(&x)); | |
| 128 } | |
| 129 printf("----------------------------------------------\n"); | |
| 130 v128_set_to_zero(&x); | |
| 131 for (i=0; i < 128; i+=2) { | |
| 132 v128_set_bit(&x, i); | |
| 133 } | |
| 134 printf("bit_string: { %s }\n", v128_bit_string(&x)); | |
| 135 printf("get_bit: { "); | |
| 136 for (i=0; i < 128; i++) { | |
| 137 if (v128_get_bit(&x, i) == 1) | |
| 138 printf("1"); | |
| 139 else | |
| 140 printf("0"); | |
| 141 } | |
| 142 printf(" } \n"); | |
| 143 | |
| 144 test_bswap(); | |
| 145 | |
| 146 return 0; | |
| 147 } | |
| 148 | |
| 149 | |
| 150 /* byte_order() prints out byte ordering of datatypes */ | |
| 151 | |
| 152 void | |
| 153 byte_order(void) { | |
| 154 int i; | |
| 155 v128_t e; | |
| 156 #if 0 | |
| 157 v16_t b; | |
| 158 v32_t c; | |
| 159 v64_t d; | |
| 160 | |
| 161 for (i=0; i < sizeof(b); i++) | |
| 162 b.octet[i] = i; | |
| 163 for (i=0; i < sizeof(c); i++) | |
| 164 c.octet[i] = i; | |
| 165 for (i=0; i < sizeof(d); i++) | |
| 166 d.octet[i] = i; | |
| 167 | |
| 168 printf("v128_t:\t%s\n", v128_hex_string(&e)); | |
| 169 printf("v64_t:\t%s\n", v64_hex_string(&d)); | |
| 170 printf("v32_t:\t%s\n", v32_hex_string(c)); | |
| 171 printf("v16_t:\t%s\n", v16_hex_string(b)); | |
| 172 | |
| 173 c.value = 0x01020304; | |
| 174 printf("v32_t:\t%s\n", v32_hex_string(c)); | |
| 175 b.value = 0x0102; | |
| 176 printf("v16_t:\t%s\n", v16_hex_string(b)); | |
| 177 | |
| 178 printf("uint16_t ordering:\n"); | |
| 179 | |
| 180 c.value = 0x00010002; | |
| 181 printf("v32_t:\t%x%x\n", c.v16[0], c.v16[1]); | |
| 182 #endif | |
| 183 | |
| 184 printf("byte ordering of crypto/math datatypes:\n"); | |
| 185 for (i=0; i < sizeof(e); i++) | |
| 186 e.v8[i] = i; | |
| 187 printf("v128_t: %s\n", v128_hex_string(&e)); | |
| 188 | |
| 189 } | |
| 190 | |
| 191 void | |
| 192 test_hex_string_funcs(void) { | |
| 193 char hex1[] = "abadcafe"; | |
| 194 char hex2[] = "0123456789abcdefqqqqq"; | |
| 195 char raw[10]; | |
| 196 int len; | |
| 197 | |
| 198 len = hex_string_to_octet_string(raw, hex1, strlen(hex1)); | |
| 199 printf("computed length: %d\tstring: %s\n", len, | |
| 200 octet_string_hex_string(raw, len/2)); | |
| 201 printf("expected length: %u\tstring: %s\n", (unsigned)strlen(hex1), hex1); | |
| 202 | |
| 203 len = hex_string_to_octet_string(raw, hex2, strlen(hex2)); | |
| 204 printf("computed length: %d\tstring: %s\n", len, | |
| 205 octet_string_hex_string(raw, len/2)); | |
| 206 printf("expected length: %d\tstring: %s\n", 16, "0123456789abcdef"); | |
| 207 | |
| 208 } | |
| 209 | |
| 210 void | |
| 211 print_string(char *s) { | |
| 212 size_t i; | |
| 213 printf("%s\n", s); | |
| 214 printf("strlen(s) = %u\n", (unsigned)strlen(s)); | |
| 215 printf("{ "); | |
| 216 for (i=0; i < strlen(s); i++) { | |
| 217 printf("0x%x, ", s[i]); | |
| 218 if (((i+1) % 8) == 0) | |
| 219 printf("\n "); | |
| 220 } | |
| 221 printf("}\n"); | |
| 222 } | |
| 223 | |
| 224 void | |
| 225 test_bswap(void) { | |
| 226 uint32_t x = 0x11223344; | |
| 227 uint64_t y = 0x1122334455667788LL; | |
| 228 | |
| 229 printf("before: %0x\nafter: %0x\n", x, be32_to_cpu(x)); | |
| 230 printf("before: %0llx\nafter: %0llx\n", (unsigned long long)y, | |
| 231 (unsigned long long)be64_to_cpu(y)); | |
| 232 | |
| 233 y = 1234; | |
| 234 | |
| 235 printf("1234: %0llx\n", (unsigned long long)y); | |
| 236 printf("as octet string: %s\n", | |
| 237 octet_string_hex_string((uint8_t *) &y, 8)); | |
| 238 y = be64_to_cpu(y); | |
| 239 printf("bswapped octet string: %s\n", | |
| 240 octet_string_hex_string((uint8_t *) &y, 8)); | |
| 241 } | |
| OLD | NEW |