crypto_math.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. #ifndef MATH_H
  45. #define MATH_H
  46. #include "datatypes.h"
  47. unsigned char
  48. v32_weight(v32_t a);
  49. unsigned char
  50. v32_distance(v32_t x, v32_t y);
  51. unsigned int
  52. v32_dot_product(v32_t a, v32_t b);
  53. char *
  54. v16_bit_string(v16_t x);
  55. char *
  56. v32_bit_string(v32_t x);
  57. char *
  58. v64_bit_string(const v64_t *x);
  59. char *
  60. octet_hex_string(uint8_t x);
  61. char *
  62. v16_hex_string(v16_t x);
  63. char *
  64. v32_hex_string(v32_t x);
  65. char *
  66. v64_hex_string(const v64_t *x);
  67. int
  68. hex_char_to_nibble(uint8_t c);
  69. int
  70. is_hex_string(char *s);
  71. v16_t
  72. hex_string_to_v16(char *s);
  73. v32_t
  74. hex_string_to_v32(char *s);
  75. v64_t
  76. hex_string_to_v64(char *s);
  77. /* the matrix A[] is stored in column format, i.e., A[i] is
  78. the ith column of the matrix */
  79. uint8_t
  80. A_times_x_plus_b(uint8_t A[8], uint8_t x, uint8_t b);
  81. void
  82. v16_copy_octet_string(v16_t *x, const uint8_t s[2]);
  83. void
  84. v32_copy_octet_string(v32_t *x, const uint8_t s[4]);
  85. void
  86. v64_copy_octet_string(v64_t *x, const uint8_t s[8]);
  87. void
  88. v128_add(v128_t *z, v128_t *x, v128_t *y);
  89. int
  90. octet_string_is_eq(uint8_t *a, uint8_t *b, int len);
  91. void
  92. octet_string_set_to_zero(uint8_t *s, int len);
  93. /*
  94. * the matrix A[] is stored in column format, i.e., A[i] is the ith
  95. * column of the matrix
  96. */
  97. uint8_t
  98. A_times_x_plus_b(uint8_t A[8], uint8_t x, uint8_t b);
  99. #if 0
  100. #if WORDS_BIGENDIAN
  101. #define _v128_add(z, x, y) { \
  102. uint64_t tmp; \
  103. \
  104. tmp = x->v32[3] + y->v32[3]; \
  105. z->v32[3] = (uint32_t) tmp; \
  106. \
  107. tmp = x->v32[2] + y->v32[2] + (tmp >> 32); \
  108. z->v32[2] = (uint32_t) tmp; \
  109. \
  110. tmp = x->v32[1] + y->v32[1] + (tmp >> 32); \
  111. z->v32[1] = (uint32_t) tmp; \
  112. \
  113. tmp = x->v32[0] + y->v32[0] + (tmp >> 32); \
  114. z->v32[0] = (uint32_t) tmp; \
  115. }
  116. #else /* assume little endian architecture */
  117. #define _v128_add(z, x, y) { \
  118. uint64_t tmp; \
  119. \
  120. tmp = htonl(x->v32[3]) + htonl(y->v32[3]); \
  121. z->v32[3] = ntohl((uint32_t) tmp); \
  122. \
  123. tmp = htonl(x->v32[2]) + htonl(y->v32[2]) \
  124. + htonl(tmp >> 32); \
  125. z->v32[2] = ntohl((uint32_t) tmp); \
  126. \
  127. tmp = htonl(x->v32[1]) + htonl(y->v32[1]) \
  128. + htonl(tmp >> 32); \
  129. z->v32[1] = ntohl((uint32_t) tmp); \
  130. \
  131. tmp = htonl(x->v32[0]) + htonl(y->v32[0]) \
  132. + htonl(tmp >> 32); \
  133. z->v32[0] = ntohl((uint32_t) tmp); \
  134. }
  135. #endif /* WORDS_BIGENDIAN */
  136. #endif
  137. #ifdef DATATYPES_USE_MACROS /* little functions are really macros */
  138. #define v128_set_to_zero(z) _v128_set_to_zero(z)
  139. #define v128_copy(z, x) _v128_copy(z, x)
  140. #define v128_xor(z, x, y) _v128_xor(z, x, y)
  141. #define v128_and(z, x, y) _v128_and(z, x, y)
  142. #define v128_or(z, x, y) _v128_or(z, x, y)
  143. #define v128_complement(x) _v128_complement(x)
  144. #define v128_is_eq(x, y) _v128_is_eq(x, y)
  145. #define v128_xor_eq(x, y) _v128_xor_eq(x, y)
  146. #define v128_get_bit(x, i) _v128_get_bit(x, i)
  147. #define v128_set_bit(x, i) _v128_set_bit(x, i)
  148. #define v128_clear_bit(x, i) _v128_clear_bit(x, i)
  149. #define v128_set_bit_to(x, i, y) _v128_set_bit_to(x, i, y)
  150. #else
  151. void
  152. v128_set_to_zero(v128_t *x);
  153. int
  154. v128_is_eq(const v128_t *x, const v128_t *y);
  155. void
  156. v128_copy(v128_t *x, const v128_t *y);
  157. void
  158. v128_xor(v128_t *z, v128_t *x, v128_t *y);
  159. void
  160. v128_and(v128_t *z, v128_t *x, v128_t *y);
  161. void
  162. v128_or(v128_t *z, v128_t *x, v128_t *y);
  163. void
  164. v128_complement(v128_t *x);
  165. int
  166. v128_get_bit(const v128_t *x, int i);
  167. void
  168. v128_set_bit(v128_t *x, int i) ;
  169. void
  170. v128_clear_bit(v128_t *x, int i);
  171. void
  172. v128_set_bit_to(v128_t *x, int i, int y);
  173. #endif /* DATATYPES_USE_MACROS */
  174. /*
  175. * octet_string_is_eq(a,b, len) returns 1 if the length len strings a
  176. * and b are not equal, returns 0 otherwise
  177. */
  178. int
  179. octet_string_is_eq(uint8_t *a, uint8_t *b, int len);
  180. void
  181. octet_string_set_to_zero(uint8_t *s, int len);
  182. #endif /* MATH_H */