netwerk/srtp/src/crypto/include/crypto_math.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     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  */
    45 #ifndef MATH_H
    46 #define MATH_H
    48 #include "datatypes.h"
    50 unsigned char
    51 v32_weight(v32_t a);
    53 unsigned char
    54 v32_distance(v32_t x, v32_t y);
    56 unsigned int
    57 v32_dot_product(v32_t a, v32_t b);
    59 char *
    60 v16_bit_string(v16_t x);
    62 char *
    63 v32_bit_string(v32_t x);
    65 char *
    66 v64_bit_string(const v64_t *x);
    68 char *
    69 octet_hex_string(uint8_t x);
    71 char *
    72 v16_hex_string(v16_t x);
    74 char *
    75 v32_hex_string(v32_t x);
    77 char *
    78 v64_hex_string(const v64_t *x);
    80 int
    81 hex_char_to_nibble(uint8_t c);
    83 int
    84 is_hex_string(char *s);
    86 v16_t
    87 hex_string_to_v16(char *s);
    89 v32_t
    90 hex_string_to_v32(char *s);
    92 v64_t
    93 hex_string_to_v64(char *s);
    95 /* the matrix A[] is stored in column format, i.e., A[i] is
    96    the ith column of the matrix */
    98 uint8_t 
    99 A_times_x_plus_b(uint8_t A[8], uint8_t x, uint8_t b);
   101 void
   102 v16_copy_octet_string(v16_t *x, const uint8_t s[2]);
   104 void
   105 v32_copy_octet_string(v32_t *x, const uint8_t s[4]);
   107 void
   108 v64_copy_octet_string(v64_t *x, const uint8_t s[8]);
   110 void
   111 v128_add(v128_t *z, v128_t *x, v128_t *y);
   113 int
   114 octet_string_is_eq(uint8_t *a, uint8_t *b, int len);
   116 void
   117 octet_string_set_to_zero(uint8_t *s, int len);
   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);
   129 #if 0
   130 #if WORDS_BIGENDIAN
   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 }
   148 #else /* assume little endian architecture */
   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 }
   169 #endif /* WORDS_BIGENDIAN */                      
   170 #endif
   172 #ifdef DATATYPES_USE_MACROS  /* little functions are really macros */
   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)
   187 #else
   189 void
   190 v128_set_to_zero(v128_t *x);
   192 int
   193 v128_is_eq(const v128_t *x, const v128_t *y);
   195 void
   196 v128_copy(v128_t *x, const v128_t *y);
   198 void
   199 v128_xor(v128_t *z, v128_t *x, v128_t *y);
   201 void
   202 v128_and(v128_t *z, v128_t *x, v128_t *y);
   204 void
   205 v128_or(v128_t *z, v128_t *x, v128_t *y); 
   207 void
   208 v128_complement(v128_t *x);
   210 int
   211 v128_get_bit(const v128_t *x, int i);
   213 void
   214 v128_set_bit(v128_t *x, int i) ;     
   216 void
   217 v128_clear_bit(v128_t *x, int i);    
   219 void
   220 v128_set_bit_to(v128_t *x, int i, int y);
   222 #endif /* DATATYPES_USE_MACROS */
   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  */
   229 int
   230 octet_string_is_eq(uint8_t *a, uint8_t *b, int len);
   232 void
   233 octet_string_set_to_zero(uint8_t *s, int len);
   236 #endif /* MATH_H */

mercurial