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