Bug Summary

File:libs/srtp/crypto/math/datatypes.c
Location:line 458, column 3
Description:Null pointer passed as an argument to a 'nonnull' parameter

Annotated Source Code

1/*
2 * datatypes.c
3 *
4 * data types for finite fields and functions for input, output, and
5 * manipulation
6 *
7 * David A. McGrew
8 * Cisco Systems, Inc.
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#include "datatypes.h"
47
48int
49octet_weight[256] = {
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
82};
83
84int
85octet_get_weight(uint8_t octet) {
86 extern int octet_weight[256];
87
88 return octet_weight[octet];
89}
90
91/*
92 * bit_string is a buffer that is used to hold output strings, e.g.
93 * for printing.
94 */
95
96/* the value MAX_PRINT_STRING_LEN is defined in datatypes.h */
97
98char bit_string[MAX_PRINT_STRING_LEN1024];
99
100uint8_t
101nibble_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];
105}
106
107char *
108octet_string_hex_string(const void *s, int length) {
109 const uint8_t *str = (const uint8_t *)s;
110 int i;
111
112 /* double length, since one octet takes two hex characters */
113 length *= 2;
114
115 /* truncate string if it would be too long */
116 if (length > MAX_PRINT_STRING_LEN1024)
117 length = MAX_PRINT_STRING_LEN1024-1;
118
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);
122 }
123 bit_string[i] = 0; /* null terminate string */
124 return bit_string;
125}
126
127static inlineinline int
128hex_char_to_nibble(uint8_t c) {
129 switch(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: return -1; /* this flags an error */
153 }
154 /* NOTREACHED */
155#ifndef WIN32
156 return -1; /* this keeps compilers from complaining */
157#endif
158}
159
160int
161is_hex_string(char *s) {
162 while(*s != 0)
163 if (hex_char_to_nibble(*s++) == -1)
164 return 0;
165 return 1;
166}
167
168/*
169 * hex_string_to_octet_string converts a hexadecimal string
170 * of length 2 * len to a raw octet string of length len
171 */
172
173int
174hex_string_to_octet_string(char *raw, char *hex, int len) {
175 uint8_t x;
176 int tmp;
177 int hex_len;
178
179 hex_len = 0;
180 while (hex_len < len) {
181 tmp = hex_char_to_nibble(hex[0]);
182 if (tmp == -1)
183 return hex_len;
184 x = (uint8_t)(tmp << 4);
185 hex_len++;
186 tmp = hex_char_to_nibble(hex[1]);
187 if (tmp == -1)
188 return hex_len;
189 x |= (tmp & 0xff);
190 hex_len++;
191 *raw++ = x;
192 hex += 2;
193 }
194 return hex_len;
195}
196
197char *
198v128_hex_string(v128_t *x) {
199 int i, j;
200
201 for (i=j=0; i < 16; i++) {
202 bit_string[j++] = nibble_to_hex_char(x->v8[i] >> 4);
203 bit_string[j++] = nibble_to_hex_char(x->v8[i] & 0xF);
204 }
205
206 bit_string[j] = 0; /* null terminate string */
207 return bit_string;
208}
209
210char *
211v128_bit_string(v128_t *x) {
212 int j, i;
213 uint32_t mask;
214
215 for (j=i=0; j < 4; j++) {
216 for (mask=0x80000000; mask > 0; mask >>= 1) {
217 if (x->v32[j] & mask)
218 bit_string[i] = '1';
219 else
220 bit_string[i] = '0';
221 ++i;
222 }
223 }
224 bit_string[128] = 0; /* null terminate string */
225
226 return bit_string;
227}
228
229void
230v128_copy_octet_string(v128_t *x, const uint8_t s[16]) {
231#ifdef ALIGNMENT_32BIT_REQUIRED
232 if ((((uint32_t) &s[0]) & 0x3) != 0)
233#endif
234 {
235 x->v8[0] = s[0];
236 x->v8[1] = s[1];
237 x->v8[2] = s[2];
238 x->v8[3] = s[3];
239 x->v8[4] = s[4];
240 x->v8[5] = s[5];
241 x->v8[6] = s[6];
242 x->v8[7] = s[7];
243 x->v8[8] = s[8];
244 x->v8[9] = s[9];
245 x->v8[10] = s[10];
246 x->v8[11] = s[11];
247 x->v8[12] = s[12];
248 x->v8[13] = s[13];
249 x->v8[14] = s[14];
250 x->v8[15] = s[15];
251 }
252#ifdef ALIGNMENT_32BIT_REQUIRED
253 else
254 {
255 v128_t *v = (v128_t *) &s[0];
256
257 v128_copy(x,v)( (x)->v32[0] = (v)->v32[0], (x)->v32[1] = (v)->v32
[1], (x)->v32[2] = (v)->v32[2], (x)->v32[3] = (v)->
v32[3] )
;
258 }
259#endif
260}
261
262#ifndef DATATYPES_USE_MACROS /* little functions are not macros */
263
264void
265v128_set_to_zero(v128_t *x)( (v128_t *x)->v32[0] = 0, (v128_t *x)->v32[1] = 0, (v128_t
*x)->v32[2] = 0, (v128_t *x)->v32[3] = 0 )
{
266 _v128_set_to_zero(x)( (x)->v32[0] = 0, (x)->v32[1] = 0, (x)->v32[2] = 0,
(x)->v32[3] = 0 )
;
267}
268
269void
270v128_copy(v128_t *x, const v128_t *y)( (v128_t *x)->v32[0] = (const v128_t *y)->v32[0], (v128_t
*x)->v32[1] = (const v128_t *y)->v32[1], (v128_t *x)->
v32[2] = (const v128_t *y)->v32[2], (v128_t *x)->v32[3]
= (const v128_t *y)->v32[3] )
{
271 _v128_copy(x, y)( (x)->v32[0] = (y)->v32[0], (x)->v32[1] = (y)->v32
[1], (x)->v32[2] = (y)->v32[2], (x)->v32[3] = (y)->
v32[3] )
;
272}
273
274void
275v128_xor(v128_t *z, v128_t *x, v128_t *y)( (v128_t *z)->v32[0] = (v128_t *x)->v32[0] ^ (v128_t *
y)->v32[0], (v128_t *z)->v32[1] = (v128_t *x)->v32[1
] ^ (v128_t *y)->v32[1], (v128_t *z)->v32[2] = (v128_t *
x)->v32[2] ^ (v128_t *y)->v32[2], (v128_t *z)->v32[3
] = (v128_t *x)->v32[3] ^ (v128_t *y)->v32[3] )
{
276 _v128_xor(z, x, y)( (z)->v32[0] = (x)->v32[0] ^ (y)->v32[0], (z)->v32
[1] = (x)->v32[1] ^ (y)->v32[1], (z)->v32[2] = (x)->
v32[2] ^ (y)->v32[2], (z)->v32[3] = (x)->v32[3] ^ (y
)->v32[3] )
;
277}
278
279void
280v128_and(v128_t *z, v128_t *x, v128_t *y)( (v128_t *z)->v32[0] = (v128_t *x)->v32[0] & (v128_t
*y)->v32[0], (v128_t *z)->v32[1] = (v128_t *x)->v32
[1] & (v128_t *y)->v32[1], (v128_t *z)->v32[2] = (v128_t
*x)->v32[2] & (v128_t *y)->v32[2], (v128_t *z)->
v32[3] = (v128_t *x)->v32[3] & (v128_t *y)->v32[3] )
{
281 _v128_and(z, x, y)( (z)->v32[0] = (x)->v32[0] & (y)->v32[0], (z)->
v32[1] = (x)->v32[1] & (y)->v32[1], (z)->v32[2] =
(x)->v32[2] & (y)->v32[2], (z)->v32[3] = (x)->
v32[3] & (y)->v32[3] )
;
282}
283
284void
285v128_or(v128_t *z, v128_t *x, v128_t *y)( (v128_t *z)->v32[0] = (v128_t *x)->v32[0] | (v128_t *
y)->v32[0], (v128_t *z)->v32[1] = (v128_t *x)->v32[1
] | (v128_t *y)->v32[1], (v128_t *z)->v32[2] = (v128_t *
x)->v32[2] | (v128_t *y)->v32[2], (v128_t *z)->v32[3
] = (v128_t *x)->v32[3] | (v128_t *y)->v32[3] )
{
286 _v128_or(z, x, y)( (z)->v32[0] = (x)->v32[0] | (y)->v32[0], (z)->v32
[1] = (x)->v32[1] | (y)->v32[1], (z)->v32[2] = (x)->
v32[2] | (y)->v32[2], (z)->v32[3] = (x)->v32[3] | (y
)->v32[3] )
;
287}
288
289void
290v128_complement(v128_t *x)( (v128_t *x)->v32[0] = ~(v128_t *x)->v32[0], (v128_t *
x)->v32[1] = ~(v128_t *x)->v32[1], (v128_t *x)->v32[
2] = ~(v128_t *x)->v32[2], (v128_t *x)->v32[3] = ~(v128_t
*x)->v32[3] )
{
291 _v128_complement(x)( (x)->v32[0] = ~(x)->v32[0], (x)->v32[1] = ~(x)->
v32[1], (x)->v32[2] = ~(x)->v32[2], (x)->v32[3] = ~(
x)->v32[3] )
;
292}
293
294int
295v128_is_eq(const v128_t *x, const v128_t *y)(((const v128_t *x)->v64[0] == (const v128_t *y)->v64[0
]) && ((const v128_t *x)->v64[1] == (const v128_t *
y)->v64[1]))
{
296 return _v128_is_eq(x, y)(((x)->v64[0] == (y)->v64[0]) && ((x)->v64[1
] == (y)->v64[1]))
;
297}
298
299int
300v128_xor_eq(v128_t *x, const v128_t *y)( (v128_t *x)->v64[0] ^= (const v128_t *y)->v64[0], (v128_t
*x)->v64[1] ^= (const v128_t *y)->v64[1] )
{
301 return _v128_xor_eq(x, y)( (x)->v64[0] ^= (y)->v64[0], (x)->v64[1] ^= (y)->
v64[1] )
;
302}
303
304int
305v128_get_bit(const v128_t *x, int i)( ((((const v128_t *x)->v32[(int i) >> 5]) >> (
(int i) & 31)) & 1) )
{
306 return _v128_get_bit(x, i)( ((((x)->v32[(i) >> 5]) >> ((i) & 31)) &
1) )
;
307}
308
309void
310v128_set_bit(v128_t *x, int i)( (((v128_t *x)->v32[(int i) >> 5]) |= ((uint32_t)1 <<
((int i) & 31))) )
{
311 _v128_set_bit(x, i)( (((x)->v32[(i) >> 5]) |= ((uint32_t)1 << ((i
) & 31))) )
;
312}
313
314void
315v128_clear_bit(v128_t *x, int i)( (((v128_t *x)->v32[(int i) >> 5]) &= ~((uint32_t
)1 << ((int i) & 31))) )
{
316 _v128_clear_bit(x, i)( (((x)->v32[(i) >> 5]) &= ~((uint32_t)1 <<
((i) & 31))) )
;
317}
318
319void
320v128_set_bit_to(v128_t *x, int i, int y)( (int y) ? ( (((v128_t *x)->v32[(int i) >> 5]) |= (
(uint32_t)1 << ((int i) & 31))) ) : ( (((v128_t *x)
->v32[(int i) >> 5]) &= ~((uint32_t)1 << (
(int i) & 31))) ) )
{
321 _v128_set_bit_to(x, i, y)( (y) ? ( (((x)->v32[(i) >> 5]) |= ((uint32_t)1 <<
((i) & 31))) ) : ( (((x)->v32[(i) >> 5]) &=
~((uint32_t)1 << ((i) & 31))) ) )
;
322}
323
324
325#endif /* DATATYPES_USE_MACROS */
326
327void
328v128_right_shift(v128_t *x, int shift) {
329 const int base_index = shift >> 5;
330 const int bit_index = shift & 31;
331 int i, from;
332 uint32_t b;
333
334 if (shift > 127) {
335 v128_set_to_zero(x)( (x)->v32[0] = 0, (x)->v32[1] = 0, (x)->v32[2] = 0,
(x)->v32[3] = 0 )
;
336 return;
337 }
338
339 if (bit_index == 0) {
340
341 /* copy each word from left size to right side */
342 x->v32[4-1] = x->v32[4-1-base_index];
343 for (i=4-1; i > base_index; i--)
344 x->v32[i-1] = x->v32[i-1-base_index];
345
346 } else {
347
348 /* set each word to the "or" of the two bit-shifted words */
349 for (i = 4; i > base_index; i--) {
350 from = i-1 - base_index;
351 b = x->v32[from] << bit_index;
352 if (from > 0)
353 b |= x->v32[from-1] >> (32-bit_index);
354 x->v32[i-1] = b;
355 }
356
357 }
358
359 /* now wrap up the final portion */
360 for (i=0; i < base_index; i++)
361 x->v32[i] = 0;
362
363}
364
365void
366v128_left_shift(v128_t *x, int shift) {
367 int i;
368 const int base_index = shift >> 5;
369 const int bit_index = shift & 31;
370
371 if (shift > 127) {
372 v128_set_to_zero(x)( (x)->v32[0] = 0, (x)->v32[1] = 0, (x)->v32[2] = 0,
(x)->v32[3] = 0 )
;
373 return;
374 }
375
376 if (bit_index == 0) {
377 for (i=0; i < 4 - base_index; i++)
378 x->v32[i] = x->v32[i+base_index];
379 } else {
380 for (i=0; i < 4 - base_index - 1; i++)
381 x->v32[i] = (x->v32[i+base_index] >> bit_index) ^
382 (x->v32[i+base_index+1] << (32 - bit_index));
383 x->v32[4 - base_index-1] = x->v32[4-1] >> bit_index;
384 }
385
386 /* now wrap up the final portion */
387 for (i = 4 - base_index; i < 4; i++)
388 x->v32[i] = 0;
389
390}
391
392/* functions manipulating bitvector_t */
393
394#ifndef DATATYPES_USE_MACROS /* little functions are not macros */
395
396int
397bitvector_get_bit(const bitvector_t *v, int bit_index)( ((((const bitvector_t *v)->word[((int bit_index) >>
5)]) >> ((int bit_index) & 31)) & 1) )
398{
399 return _bitvector_get_bit(v, bit_index)( ((((v)->word[((bit_index) >> 5)]) >> ((bit_index
) & 31)) & 1) )
;
400}
401
402void
403bitvector_set_bit(bitvector_t *v, int bit_index)( (((bitvector_t *v)->word[((int bit_index) >> 5)] |=
((uint32_t)1 << ((int bit_index) & 31)))) )
404{
405 _bitvector_set_bit(v, bit_index)( (((v)->word[((bit_index) >> 5)] |= ((uint32_t)1 <<
((bit_index) & 31)))) )
;
406}
407
408void
409bitvector_clear_bit(bitvector_t *v, int bit_index)( (((bitvector_t *v)->word[((int bit_index) >> 5)] &=
~((uint32_t)1 << ((int bit_index) & 31)))) )
410{
411 _bitvector_clear_bit(v, bit_index)( (((v)->word[((bit_index) >> 5)] &= ~((uint32_t
)1 << ((bit_index) & 31)))) )
;
412}
413
414
415#endif /* DATATYPES_USE_MACROS */
416
417int
418bitvector_alloc(bitvector_t *v, unsigned long length) {
419 unsigned long l;
420
421 /* Round length up to a multiple of bits_per_word */
422 length = (length + bits_per_word32 - 1) & ~(unsigned long)((bits_per_word32 - 1));
423
424 l = length / bits_per_word32 * bytes_per_word4;
425
426 /* allocate memory, then set parameters */
427 if (l == 0)
1
Assuming 'l' is equal to 0
2
Taking true branch
428 v->word = NULL((void*)0);
3
Null pointer value stored to field 'word'
429 else {
430 v->word = (uint32_t*)crypto_alloc(l);
431 if (v->word == NULL((void*)0)) {
432 v->word = NULL((void*)0);
433 v->length = 0;
434 return -1;
435 }
436 }
437 v->length = length;
438
439 /* initialize bitvector to zero */
440 bitvector_set_to_zero(v);
4
Calling 'bitvector_set_to_zero'
441
442 return 0;
443}
444
445
446void
447bitvector_dealloc(bitvector_t *v) {
448 if (v->word != NULL((void*)0))
449 crypto_free(v->word);
450 v->word = NULL((void*)0);
451 v->length = 0;
452}
453
454void
455bitvector_set_to_zero(bitvector_t *x)
456{
457 /* C99 guarantees that memset(0) will set the value 0 for uint32_t */
458 memset(x->word, 0, x->length >> 3);
5
Null pointer passed as an argument to a 'nonnull' parameter
459}
460
461char *
462bitvector_bit_string(bitvector_t *x, char* buf, int len) {
463 int j, i;
464 uint32_t mask;
465
466 for (j=i=0; j < (int)(x->length>>5) && i < len-1; j++) {
467 for (mask=0x80000000; mask > 0; mask >>= 1) {
468 if (x->word[j] & mask)
469 buf[i] = '1';
470 else
471 buf[i] = '0';
472 ++i;
473 if (i >= len-1)
474 break;
475 }
476 }
477 buf[i] = 0; /* null terminate string */
478
479 return buf;
480}
481
482void
483bitvector_left_shift(bitvector_t *x, int shift) {
484 int i;
485 const int base_index = shift >> 5;
486 const int bit_index = shift & 31;
487 const int word_length = x->length >> 5;
488
489 if (shift >= (int)x->length) {
490 bitvector_set_to_zero(x);
491 return;
492 }
493
494 if (bit_index == 0) {
495 for (i=0; i < word_length - base_index; i++)
496 x->word[i] = x->word[i+base_index];
497 } else {
498 for (i=0; i < word_length - base_index - 1; i++)
499 x->word[i] = (x->word[i+base_index] >> bit_index) ^
500 (x->word[i+base_index+1] << (32 - bit_index));
501 x->word[word_length - base_index-1] = x->word[word_length-1] >> bit_index;
502 }
503
504 /* now wrap up the final portion */
505 for (i = word_length - base_index; i < word_length; i++)
506 x->word[i] = 0;
507
508}
509
510
511int
512octet_string_is_eq(uint8_t *a, uint8_t *b, int len) {
513 uint8_t *end = b + len;
514 while (b < end)
515 if (*a++ != *b++)
516 return 1;
517 return 0;
518}
519
520void
521octet_string_set_to_zero(uint8_t *s, int len) {
522 uint8_t *end = s + len;
523
524 do {
525 *s = 0;
526 } while (++s < end);
527
528}
529
530
531/*
532 * From RFC 1521: The Base64 Alphabet
533 *
534 * Value Encoding Value Encoding Value Encoding Value Encoding
535 * 0 A 17 R 34 i 51 z
536 * 1 B 18 S 35 j 52 0
537 * 2 C 19 T 36 k 53 1
538 * 3 D 20 U 37 l 54 2
539 * 4 E 21 V 38 m 55 3
540 * 5 F 22 W 39 n 56 4
541 * 6 G 23 X 40 o 57 5
542 * 7 H 24 Y 41 p 58 6
543 * 8 I 25 Z 42 q 59 7
544 * 9 J 26 a 43 r 60 8
545 * 10 K 27 b 44 s 61 9
546 * 11 L 28 c 45 t 62 +
547 * 12 M 29 d 46 u 63 /
548 * 13 N 30 e 47 v
549 * 14 O 31 f 48 w (pad) =
550 * 15 P 32 g 49 x
551 * 16 Q 33 h 50 y
552 */
553
554int
555base64_char_to_sextet(uint8_t c) {
556 switch(c) {
557 case 'A':
558 return 0;
559 case 'B':
560 return 1;
561 case 'C':
562 return 2;
563 case 'D':
564 return 3;
565 case 'E':
566 return 4;
567 case 'F':
568 return 5;
569 case 'G':
570 return 6;
571 case 'H':
572 return 7;
573 case 'I':
574 return 8;
575 case 'J':
576 return 9;
577 case 'K':
578 return 10;
579 case 'L':
580 return 11;
581 case 'M':
582 return 12;
583 case 'N':
584 return 13;
585 case 'O':
586 return 14;
587 case 'P':
588 return 15;
589 case 'Q':
590 return 16;
591 case 'R':
592 return 17;
593 case 'S':
594 return 18;
595 case 'T':
596 return 19;
597 case 'U':
598 return 20;
599 case 'V':
600 return 21;
601 case 'W':
602 return 22;
603 case 'X':
604 return 23;
605 case 'Y':
606 return 24;
607 case 'Z':
608 return 25;
609 case 'a':
610 return 26;
611 case 'b':
612 return 27;
613 case 'c':
614 return 28;
615 case 'd':
616 return 29;
617 case 'e':
618 return 30;
619 case 'f':
620 return 31;
621 case 'g':
622 return 32;
623 case 'h':
624 return 33;
625 case 'i':
626 return 34;
627 case 'j':
628 return 35;
629 case 'k':
630 return 36;
631 case 'l':
632 return 37;
633 case 'm':
634 return 38;
635 case 'n':
636 return 39;
637 case 'o':
638 return 40;
639 case 'p':
640 return 41;
641 case 'q':
642 return 42;
643 case 'r':
644 return 43;
645 case 's':
646 return 44;
647 case 't':
648 return 45;
649 case 'u':
650 return 46;
651 case 'v':
652 return 47;
653 case 'w':
654 return 48;
655 case 'x':
656 return 49;
657 case 'y':
658 return 50;
659 case 'z':
660 return 51;
661 case '0':
662 return 52;
663 case '1':
664 return 53;
665 case '2':
666 return 54;
667 case '3':
668 return 55;
669 case '4':
670 return 56;
671 case '5':
672 return 57;
673 case '6':
674 return 58;
675 case '7':
676 return 59;
677 case '8':
678 return 60;
679 case '9':
680 return 61;
681 case '+':
682 return 62;
683 case '/':
684 return 63;
685 case '=':
686 return 64;
687 default:
688 break;
689 }
690 return -1;
691}
692
693/*
694 * base64_string_to_octet_string converts a hexadecimal string
695 * of length 2 * len to a raw octet string of length len
696 */
697
698int
699base64_string_to_octet_string(char *raw, char *base64, int len) {
700 uint8_t x;
701 int tmp;
702 int base64_len;
703
704 base64_len = 0;
705 while (base64_len < len) {
706 tmp = base64_char_to_sextet(base64[0]);
707 if (tmp == -1)
708 return base64_len;
709 x = (uint8_t)(tmp << 6);
710 base64_len++;
711 tmp = base64_char_to_sextet(base64[1]);
712 if (tmp == -1)
713 return base64_len;
714 x |= (tmp & 0xffff);
715 base64_len++;
716 *raw++ = x;
717 base64 += 2;
718 }
719 return base64_len;
720}