Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(12)

Side by Side Diff: crypto/math/datatypes.c

Issue 2344973002: Update libsrtp to version 2.0 (Closed)
Patch Set: Add '.' back to include_dirs Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « crypto/kernel/key.c ('k') | crypto/math/stat.c » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * datatypes.c 2 * datatypes.c
3 * 3 *
4 * data types for finite fields and functions for input, output, and 4 * data types for finite fields and functions for input, output, and
5 * manipulation 5 * manipulation
6 * 6 *
7 * David A. McGrew 7 * David A. McGrew
8 * Cisco Systems, Inc. 8 * Cisco Systems, Inc.
9 */ 9 */
10 /* 10 /*
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
95 /* 95 /*
96 * bit_string is a buffer that is used to hold output strings, e.g. 96 * bit_string is a buffer that is used to hold output strings, e.g.
97 * for printing. 97 * for printing.
98 */ 98 */
99 99
100 /* the value MAX_PRINT_STRING_LEN is defined in datatypes.h */ 100 /* the value MAX_PRINT_STRING_LEN is defined in datatypes.h */
101 101
102 char bit_string[MAX_PRINT_STRING_LEN]; 102 char bit_string[MAX_PRINT_STRING_LEN];
103 103
104 uint8_t 104 uint8_t
105 nibble_to_hex_char(uint8_t nibble) { 105 srtp_nibble_to_hex_char(uint8_t nibble) {
106 char buf[16] = {'0', '1', '2', '3', '4', '5', '6', '7', 106 char buf[16] = {'0', '1', '2', '3', '4', '5', '6', '7',
107 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; 107 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
108 return buf[nibble & 0xF]; 108 return buf[nibble & 0xF];
109 } 109 }
110 110
111 char * 111 char * srtp_octet_string_hex_string(const void *s, int length) {
112 octet_string_hex_string(const void *s, int length) {
113 const uint8_t *str = (const uint8_t *)s; 112 const uint8_t *str = (const uint8_t *)s;
114 int i; 113 int i;
115 114
116 /* double length, since one octet takes two hex characters */ 115 /* double length, since one octet takes two hex characters */
117 length *= 2; 116 length *= 2;
118 117
119 /* truncate string if it would be too long */ 118 /* truncate string if it would be too long */
120 if (length > MAX_PRINT_STRING_LEN) 119 if (length > MAX_PRINT_STRING_LEN)
121 length = MAX_PRINT_STRING_LEN-1; 120 length = MAX_PRINT_STRING_LEN-2;
122 121
123 for (i=0; i < length; i+=2) { 122 for (i=0; i < length; i+=2) {
124 bit_string[i] = nibble_to_hex_char(*str >> 4); 123 bit_string[i] = srtp_nibble_to_hex_char(*str >> 4);
125 bit_string[i+1] = nibble_to_hex_char(*str++ & 0xF); 124 bit_string[i+1] = srtp_nibble_to_hex_char(*str++ & 0xF);
126 } 125 }
127 bit_string[i] = 0; /* null terminate string */ 126 bit_string[i] = 0; /* null terminate string */
128 return bit_string; 127 return bit_string;
129 } 128 }
130 129
131 static inline int
132 hex_char_to_nibble(uint8_t c) {
133 switch(c) {
134 case ('0'): return 0x0;
135 case ('1'): return 0x1;
136 case ('2'): return 0x2;
137 case ('3'): return 0x3;
138 case ('4'): return 0x4;
139 case ('5'): return 0x5;
140 case ('6'): return 0x6;
141 case ('7'): return 0x7;
142 case ('8'): return 0x8;
143 case ('9'): return 0x9;
144 case ('a'): return 0xa;
145 case ('A'): return 0xa;
146 case ('b'): return 0xb;
147 case ('B'): return 0xb;
148 case ('c'): return 0xc;
149 case ('C'): return 0xc;
150 case ('d'): return 0xd;
151 case ('D'): return 0xd;
152 case ('e'): return 0xe;
153 case ('E'): return 0xe;
154 case ('f'): return 0xf;
155 case ('F'): return 0xf;
156 default: return -1; /* this flags an error */
157 }
158 /* NOTREACHED */
159 return -1; /* this keeps compilers from complaining */
160 }
161
162 int
163 is_hex_string(char *s) {
164 while(*s != 0)
165 if (hex_char_to_nibble(*s++) == -1)
166 return 0;
167 return 1;
168 }
169
170 /*
171 * hex_string_to_octet_string converts a hexadecimal string
172 * of length 2 * len to a raw octet string of length len
173 */
174
175 int
176 hex_string_to_octet_string(char *raw, char *hex, int len) {
177 uint8_t x;
178 int tmp;
179 int hex_len;
180
181 hex_len = 0;
182 while (hex_len < len) {
183 tmp = hex_char_to_nibble(hex[0]);
184 if (tmp == -1)
185 return hex_len;
186 x = (tmp << 4);
187 hex_len++;
188 tmp = hex_char_to_nibble(hex[1]);
189 if (tmp == -1)
190 return hex_len;
191 x |= (tmp & 0xff);
192 hex_len++;
193 *raw++ = x;
194 hex += 2;
195 }
196 return hex_len;
197 }
198
199 char * 130 char *
200 v128_hex_string(v128_t *x) { 131 v128_hex_string(v128_t *x) {
201 int i, j; 132 int i, j;
202 133
203 for (i=j=0; i < 16; i++) { 134 for (i=j=0; i < 16; i++) {
204 bit_string[j++] = nibble_to_hex_char(x->v8[i] >> 4); 135 bit_string[j++] = srtp_nibble_to_hex_char(x->v8[i] >> 4);
205 bit_string[j++] = nibble_to_hex_char(x->v8[i] & 0xF); 136 bit_string[j++] = srtp_nibble_to_hex_char(x->v8[i] & 0xF);
206 } 137 }
207 138
208 bit_string[j] = 0; /* null terminate string */ 139 bit_string[j] = 0; /* null terminate string */
209 return bit_string; 140 return bit_string;
210 } 141 }
211 142
212 char * 143 char *
213 v128_bit_string(v128_t *x) { 144 v128_bit_string(v128_t *x) {
214 int j, i; 145 int j, i;
215 uint32_t mask; 146 uint32_t mask;
(...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after
422 353
423 /* Round length up to a multiple of bits_per_word */ 354 /* Round length up to a multiple of bits_per_word */
424 length = (length + bits_per_word - 1) & ~(unsigned long)((bits_per_word - 1)); 355 length = (length + bits_per_word - 1) & ~(unsigned long)((bits_per_word - 1));
425 356
426 l = length / bits_per_word * bytes_per_word; 357 l = length / bits_per_word * bytes_per_word;
427 358
428 /* allocate memory, then set parameters */ 359 /* allocate memory, then set parameters */
429 if (l == 0) 360 if (l == 0)
430 v->word = NULL; 361 v->word = NULL;
431 else { 362 else {
432 v->word = (uint32_t*)crypto_alloc(l); 363 v->word = (uint32_t*)srtp_crypto_alloc(l);
433 if (v->word == NULL) { 364 if (v->word == NULL) {
434 v->word = NULL; 365 v->word = NULL;
435 v->length = 0; 366 v->length = 0;
436 return -1; 367 return -1;
437 } 368 }
438 } 369 }
439 v->length = length; 370 v->length = length;
440 371
441 /* initialize bitvector to zero */ 372 /* initialize bitvector to zero */
442 bitvector_set_to_zero(v); 373 bitvector_set_to_zero(v);
443 374
444 return 0; 375 return 0;
445 } 376 }
446 377
447 378
448 void 379 void
449 bitvector_dealloc(bitvector_t *v) { 380 bitvector_dealloc(bitvector_t *v) {
450 if (v->word != NULL) 381 if (v->word != NULL)
451 crypto_free(v->word); 382 srtp_crypto_free(v->word);
452 v->word = NULL; 383 v->word = NULL;
453 v->length = 0; 384 v->length = 0;
454 } 385 }
455 386
456 void 387 void
457 bitvector_set_to_zero(bitvector_t *x) 388 bitvector_set_to_zero(bitvector_t *x)
458 { 389 {
459 /* C99 guarantees that memset(0) will set the value 0 for uint32_t */ 390 /* C99 guarantees that memset(0) will set the value 0 for uint32_t */
460 memset(x->word, 0, x->length >> 3); 391 memset(x->word, 0, x->length >> 3);
461 } 392 }
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
502 (x->word[i+base_index+1] << (32 - bit_index)); 433 (x->word[i+base_index+1] << (32 - bit_index));
503 x->word[word_length - base_index-1] = x->word[word_length-1] >> bit_index; 434 x->word[word_length - base_index-1] = x->word[word_length-1] >> bit_index;
504 } 435 }
505 436
506 /* now wrap up the final portion */ 437 /* now wrap up the final portion */
507 for (i = word_length - base_index; i < word_length; i++) 438 for (i = word_length - base_index; i < word_length; i++)
508 x->word[i] = 0; 439 x->word[i] = 0;
509 440
510 } 441 }
511 442
512
513 int 443 int
514 octet_string_is_eq(uint8_t *a, uint8_t *b, int len) { 444 octet_string_is_eq(uint8_t *a, uint8_t *b, int len) {
515 uint8_t *end = b + len; 445 uint8_t *end = b + len;
446 uint8_t accumulator = 0;
447
448 /*
449 * We use this somewhat obscure implementation to try to ensure the running
450 * time only depends on len, even accounting for compiler optimizations.
451 * The accumulator ends up zero iff the strings are equal.
452 */
516 while (b < end) 453 while (b < end)
517 if (*a++ != *b++) 454 accumulator |= (*a++ ^ *b++);
518 return 1; 455
519 return 0; 456 /* Return 1 if *not* equal. */
457 return accumulator != 0;
520 } 458 }
521 459
522 void 460 void
523 octet_string_set_to_zero(uint8_t *s, int len) { 461 octet_string_set_to_zero(uint8_t *s, int len) {
524 uint8_t *end = s + len; 462 uint8_t *end = s + len;
525 463
526 do { 464 do {
527 *s = 0; 465 *s = 0;
528 } while (++s < end); 466 } while (++s < end);
529 467
(...skipping 30 matching lines...) Expand all
560 while (i < len && j == 0) { 498 while (i < len && j == 0) {
561 j = base64_block_to_octet_triple(out + k, in + i); 499 j = base64_block_to_octet_triple(out + k, in + i);
562 k += 3; 500 k += 3;
563 i += 4; 501 i += 4;
564 } 502 }
565 *pad = j; 503 *pad = j;
566 return i; 504 return i;
567 } 505 }
568 506
569 #endif 507 #endif
OLDNEW
« no previous file with comments | « crypto/kernel/key.c ('k') | crypto/math/stat.c » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698