4
* data types for finite fields and functions for input, output, and
12
* Copyright (c) 2001-2006 Cisco Systems, Inc.
13
* All rights reserved.
15
* Redistribution and use in source and binary forms, with or without
16
* modification, are permitted provided that the following conditions
19
* Redistributions of source code must retain the above copyright
20
* notice, this list of conditions and the following disclaimer.
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.
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.
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.
46
#include "datatypes.h"
50
0, 1, 1, 2, 1, 2, 2, 3,
51
1, 2, 2, 3, 2, 3, 3, 4,
52
1, 2, 2, 3, 2, 3, 3, 4,
53
2, 3, 3, 4, 3, 4, 4, 5,
54
1, 2, 2, 3, 2, 3, 3, 4,
55
2, 3, 3, 4, 3, 4, 4, 5,
56
2, 3, 3, 4, 3, 4, 4, 5,
57
3, 4, 4, 5, 4, 5, 5, 6,
58
1, 2, 2, 3, 2, 3, 3, 4,
59
2, 3, 3, 4, 3, 4, 4, 5,
60
2, 3, 3, 4, 3, 4, 4, 5,
61
3, 4, 4, 5, 4, 5, 5, 6,
62
2, 3, 3, 4, 3, 4, 4, 5,
63
3, 4, 4, 5, 4, 5, 5, 6,
64
3, 4, 4, 5, 4, 5, 5, 6,
65
4, 5, 5, 6, 5, 6, 6, 7,
66
1, 2, 2, 3, 2, 3, 3, 4,
67
2, 3, 3, 4, 3, 4, 4, 5,
68
2, 3, 3, 4, 3, 4, 4, 5,
69
3, 4, 4, 5, 4, 5, 5, 6,
70
2, 3, 3, 4, 3, 4, 4, 5,
71
3, 4, 4, 5, 4, 5, 5, 6,
72
3, 4, 4, 5, 4, 5, 5, 6,
73
4, 5, 5, 6, 5, 6, 6, 7,
74
2, 3, 3, 4, 3, 4, 4, 5,
75
3, 4, 4, 5, 4, 5, 5, 6,
76
3, 4, 4, 5, 4, 5, 5, 6,
77
4, 5, 5, 6, 5, 6, 6, 7,
78
3, 4, 4, 5, 4, 5, 5, 6,
79
4, 5, 5, 6, 5, 6, 6, 7,
80
4, 5, 5, 6, 5, 6, 6, 7,
81
5, 6, 6, 7, 6, 7, 7, 8
85
octet_get_weight(uint8_t octet) {
86
extern int octet_weight[256];
88
return octet_weight[octet];
92
* bit_string is a buffer that is used to hold output strings, e.g.
96
/* the value MAX_PRINT_STRING_LEN is defined in datatypes.h */
98
char bit_string[MAX_PRINT_STRING_LEN];
101
nibble_to_hex_char(uint8_t nibble) {
102
char buf[16] = {'0', '1', '2', '3', '4', '5', '6', '7',
103
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
104
return buf[nibble & 0xF];
108
octet_string_hex_string(const void *s, int length) {
109
const uint8_t *str = (const uint8_t *)s;
112
/* double length, since one octet takes two hex characters */
115
/* truncate string if it would be too long */
116
if (length > MAX_PRINT_STRING_LEN)
117
length = MAX_PRINT_STRING_LEN-1;
119
for (i=0; i < length; i+=2) {
120
bit_string[i] = nibble_to_hex_char(*str >> 4);
121
bit_string[i+1] = nibble_to_hex_char(*str++ & 0xF);
123
bit_string[i] = 0; /* null terminate string */
128
hex_char_to_nibble(uint8_t c) {
130
case ('0'): return 0x0;
131
case ('1'): return 0x1;
132
case ('2'): return 0x2;
133
case ('3'): return 0x3;
134
case ('4'): return 0x4;
135
case ('5'): return 0x5;
136
case ('6'): return 0x6;
137
case ('7'): return 0x7;
138
case ('8'): return 0x8;
139
case ('9'): return 0x9;
140
case ('a'): return 0xa;
141
case ('A'): return 0xa;
142
case ('b'): return 0xb;
143
case ('B'): return 0xb;
144
case ('c'): return 0xc;
145
case ('C'): return 0xc;
146
case ('d'): return 0xd;
147
case ('D'): return 0xd;
148
case ('e'): return 0xe;
149
case ('E'): return 0xe;
150
case ('f'): return 0xf;
151
case ('F'): return 0xf;
152
default: break; /* this flags an error */
158
is_hex_string(char *s) {
160
if (hex_char_to_nibble(*s++) == -1)
166
* hex_string_to_octet_string converts a hexadecimal string
167
* of length 2 * len to a raw octet string of length len
171
hex_string_to_octet_string(char *raw, char *hex, int len) {
177
while (hex_len < len) {
178
tmp = hex_char_to_nibble(hex[0]);
183
tmp = hex_char_to_nibble(hex[1]);
195
v128_hex_string(v128_t *x) {
198
for (i=j=0; i < 16; i++) {
199
bit_string[j++] = nibble_to_hex_char(x->v8[i] >> 4);
200
bit_string[j++] = nibble_to_hex_char(x->v8[i] & 0xF);
203
bit_string[j] = 0; /* null terminate string */
208
v128_bit_string(v128_t *x) {
212
for (j=index=0; j < 4; j++) {
213
for (mask=0x80000000; mask > 0; mask >>= 1) {
214
if (x->v32[j] & mask)
215
bit_string[index] = '1';
217
bit_string[index] = '0';
221
bit_string[128] = 0; /* null terminate string */
227
v128_copy_octet_string(v128_t *x, const uint8_t s[16]) {
228
#ifdef ALIGNMENT_32BIT_REQUIRED
229
if ((((uint32_t) &s[0]) & 0x3) != 0)
249
#ifdef ALIGNMENT_32BIT_REQUIRED
252
v128_t *v = (v128_t *) &s[0];
259
#ifndef DATATYPES_USE_MACROS /* little functions are not macros */
262
v128_set_to_zero(v128_t *x) {
263
_v128_set_to_zero(x);
267
v128_copy(v128_t *x, const v128_t *y) {
272
v128_xor(v128_t *z, v128_t *x, v128_t *y) {
277
v128_and(v128_t *z, v128_t *x, v128_t *y) {
282
v128_or(v128_t *z, v128_t *x, v128_t *y) {
287
v128_complement(v128_t *x) {
292
v128_is_eq(const v128_t *x, const v128_t *y) {
293
return _v128_is_eq(x, y);
297
v128_xor_eq(v128_t *x, const v128_t *y) {
298
return _v128_xor_eq(x, y);
302
v128_get_bit(const v128_t *x, int i) {
303
return _v128_get_bit(x, i);
307
v128_set_bit(v128_t *x, int i) {
312
v128_clear_bit(v128_t *x, int i){
313
_v128_clear_bit(x, i);
317
v128_set_bit_to(v128_t *x, int i, int y){
318
_v128_set_bit_to(x, i, y);
322
#endif /* DATATYPES_USE_MACROS */
325
v128_right_shift(v128_t *x, int index) {
326
const int base_index = index >> 5;
327
const int bit_index = index & 31;
336
if (bit_index == 0) {
338
/* copy each word from left size to right side */
339
x->v32[4-1] = x->v32[4-1-base_index];
340
for (i=4-1; i > base_index; i--)
341
x->v32[i-1] = x->v32[i-1-base_index];
345
/* set each word to the "or" of the two bit-shifted words */
346
for (i = 4; i > base_index; i--) {
347
from = i-1 - base_index;
348
b = x->v32[from] << bit_index;
350
b |= x->v32[from-1] >> (32-bit_index);
356
/* now wrap up the final portion */
357
for (i=0; i < base_index; i++)
363
v128_left_shift(v128_t *x, int index) {
365
const int base_index = index >> 5;
366
const int bit_index = index & 31;
373
if (bit_index == 0) {
374
for (i=0; i < 4 - base_index; i++)
375
x->v32[i] = x->v32[i+base_index];
377
for (i=0; i < 4 - base_index - 1; i++)
378
x->v32[i] = (x->v32[i+base_index] >> bit_index) ^
379
(x->v32[i+base_index+1] << (32 - bit_index));
380
x->v32[4 - base_index-1] = x->v32[4-1] >> bit_index;
383
/* now wrap up the final portion */
384
for (i = 4 - base_index; i < 4; i++)
391
octet_string_is_eq(uint8_t *a, uint8_t *b, int len) {
392
uint8_t *end = b + len;
400
octet_string_set_to_zero(uint8_t *s, int len) {
401
uint8_t *end = s + len;
411
* From RFC 1521: The Base64 Alphabet
413
* Value Encoding Value Encoding Value Encoding Value Encoding
424
* 10 K 27 b 44 s 61 9
425
* 11 L 28 c 45 t 62 +
426
* 12 M 29 d 46 u 63 /
428
* 14 O 31 f 48 w (pad) =
434
base64_char_to_sextet(uint8_t c) {
573
* base64_string_to_octet_string converts a hexadecimal string
574
* of length 2 * len to a raw octet string of length len
578
base64_string_to_octet_string(char *raw, char *base64, int len) {
584
while (base64_len < len) {
585
tmp = base64_char_to_sextet(base64[0]);
590
tmp = base64_char_to_sextet(base64[1]);