Lines Matching refs:a

153 int bn_num_bits_consttime(const BIGNUM *a)  in bn_num_bits_consttime()  argument
157 int i = a->top - 1; in bn_num_bits_consttime()
158 bn_check_top(a); in bn_num_bits_consttime()
160 for (j = 0, past_i = 0, ret = 0; j < a->dmax; j++) { in bn_num_bits_consttime()
164 ret += BN_num_bits_word(a->d[j]) & mask; in bn_num_bits_consttime()
178 int BN_num_bits(const BIGNUM *a) in BN_num_bits() argument
180 int i = a->top - 1; in BN_num_bits()
181 bn_check_top(a); in BN_num_bits()
183 if (a->flags & BN_FLG_CONSTTIME) { in BN_num_bits()
193 return bn_num_bits_consttime(a); in BN_num_bits()
196 if (BN_is_zero(a)) in BN_num_bits()
199 return ((i * BN_BITS2) + BN_num_bits_word(a->d[i])); in BN_num_bits()
202 static void bn_free_d(BIGNUM *a, int clear) in bn_free_d() argument
204 if (BN_get_flags(a, BN_FLG_SECURE)) in bn_free_d()
205 OPENSSL_secure_clear_free(a->d, a->dmax * sizeof(a->d[0])); in bn_free_d()
207 OPENSSL_clear_free(a->d, a->dmax * sizeof(a->d[0])); in bn_free_d()
209 OPENSSL_free(a->d); in bn_free_d()
213 void BN_clear_free(BIGNUM *a) in BN_clear_free() argument
215 if (a == NULL) in BN_clear_free()
217 if (a->d != NULL && !BN_get_flags(a, BN_FLG_STATIC_DATA)) in BN_clear_free()
218 bn_free_d(a, 1); in BN_clear_free()
219 if (BN_get_flags(a, BN_FLG_MALLOCED)) { in BN_clear_free()
220 OPENSSL_cleanse(a, sizeof(*a)); in BN_clear_free()
221 OPENSSL_free(a); in BN_clear_free()
225 void BN_free(BIGNUM *a) in BN_free() argument
227 if (a == NULL) in BN_free()
229 if (!BN_get_flags(a, BN_FLG_STATIC_DATA)) in BN_free()
230 bn_free_d(a, 0); in BN_free()
231 if (a->flags & BN_FLG_MALLOCED) in BN_free()
232 OPENSSL_free(a); in BN_free()
235 void bn_init(BIGNUM *a) in bn_init() argument
239 *a = nilbn; in bn_init()
240 bn_check_top(a); in bn_init()
268 BN_ULONG *a = NULL; in bn_expand_internal() local
279 a = OPENSSL_secure_zalloc(words * sizeof(*a)); in bn_expand_internal()
281 a = OPENSSL_zalloc(words * sizeof(*a)); in bn_expand_internal()
282 if (a == NULL) { in bn_expand_internal()
289 memcpy(a, b->d, sizeof(*a) * b->top); in bn_expand_internal()
291 return a; in bn_expand_internal()
305 BN_ULONG *a = bn_expand_internal(b, words); in bn_expand2() local
306 if (!a) in bn_expand2()
310 b->d = a; in bn_expand2()
317 BIGNUM *BN_dup(const BIGNUM *a) in BN_dup() argument
321 if (a == NULL) in BN_dup()
323 bn_check_top(a); in BN_dup()
325 t = BN_get_flags(a, BN_FLG_SECURE) ? BN_secure_new() : BN_new(); in BN_dup()
328 if (!BN_copy(t, a)) { in BN_dup()
336 BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b) in BN_copy() argument
344 if (a == b) in BN_copy()
345 return a; in BN_copy()
346 if (bn_wexpand(a, bn_words) == NULL) in BN_copy()
350 memcpy(a->d, b->d, sizeof(b->d[0]) * bn_words); in BN_copy()
352 a->neg = b->neg; in BN_copy()
353 a->top = b->top; in BN_copy()
354 a->flags |= b->flags & BN_FLG_FIXED_TOP; in BN_copy()
355 bn_check_top(a); in BN_copy()
356 return a; in BN_copy()
365 void BN_swap(BIGNUM *a, BIGNUM *b) in BN_swap() argument
371 bn_check_top(a); in BN_swap()
374 flags_old_a = a->flags; in BN_swap()
377 tmp_d = a->d; in BN_swap()
378 tmp_top = a->top; in BN_swap()
379 tmp_dmax = a->dmax; in BN_swap()
380 tmp_neg = a->neg; in BN_swap()
382 a->d = b->d; in BN_swap()
383 a->top = b->top; in BN_swap()
384 a->dmax = b->dmax; in BN_swap()
385 a->neg = b->neg; in BN_swap()
392 a->flags = FLAGS_STRUCT(flags_old_a) | FLAGS_DATA(flags_old_b); in BN_swap()
394 bn_check_top(a); in BN_swap()
398 void BN_clear(BIGNUM *a) in BN_clear() argument
400 if (a == NULL) in BN_clear()
402 bn_check_top(a); in BN_clear()
403 if (a->d != NULL) in BN_clear()
404 OPENSSL_cleanse(a->d, sizeof(*a->d) * a->dmax); in BN_clear()
405 a->neg = 0; in BN_clear()
406 a->top = 0; in BN_clear()
407 a->flags &= ~BN_FLG_FIXED_TOP; in BN_clear()
410 BN_ULONG BN_get_word(const BIGNUM *a) in BN_get_word() argument
412 if (a->top > 1) in BN_get_word()
414 else if (a->top == 1) in BN_get_word()
415 return a->d[0]; in BN_get_word()
420 int BN_set_word(BIGNUM *a, BN_ULONG w) in BN_set_word() argument
422 bn_check_top(a); in BN_set_word()
423 if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL) in BN_set_word()
425 a->neg = 0; in BN_set_word()
426 a->d[0] = w; in BN_set_word()
427 a->top = (w ? 1 : 0); in BN_set_word()
428 a->flags &= ~BN_FLG_FIXED_TOP; in BN_set_word()
429 bn_check_top(a); in BN_set_word()
535 static int bn2binpad(const BIGNUM *a, unsigned char *to, int tolen, in bn2binpad() argument
549 n8 = BN_num_bits(a); in bn2binpad()
554 xor = a->neg ? 0xff : 0x00; in bn2binpad()
555 carry = a->neg; in bn2binpad()
564 ? !a->neg /* MSbit set on nonnegative bignum */ in bn2binpad()
565 : a->neg; /* MSbit unset on negative bignum */ in bn2binpad()
571 BIGNUM temp = *a; in bn2binpad()
581 atop = a->dmax * BN_BYTES; in bn2binpad()
601 atop = a->top * BN_BYTES; in bn2binpad()
605 l = a->d[i / BN_BYTES]; in bn2binpad()
618 int BN_bn2binpad(const BIGNUM *a, unsigned char *to, int tolen) in BN_bn2binpad() argument
622 return bn2binpad(a, to, tolen, BIG, UNSIGNED); in BN_bn2binpad()
625 int BN_signed_bn2bin(const BIGNUM *a, unsigned char *to, int tolen) in BN_signed_bn2bin() argument
629 return bn2binpad(a, to, tolen, BIG, SIGNED); in BN_signed_bn2bin()
632 int BN_bn2bin(const BIGNUM *a, unsigned char *to) in BN_bn2bin() argument
634 return bn2binpad(a, to, -1, BIG, UNSIGNED); in BN_bn2bin()
647 int BN_bn2lebinpad(const BIGNUM *a, unsigned char *to, int tolen) in BN_bn2lebinpad() argument
651 return bn2binpad(a, to, tolen, LITTLE, UNSIGNED); in BN_bn2lebinpad()
654 int BN_signed_bn2lebin(const BIGNUM *a, unsigned char *to, int tolen) in BN_signed_bn2lebin() argument
658 return bn2binpad(a, to, tolen, LITTLE, SIGNED); in BN_signed_bn2lebin()
679 int BN_bn2nativepad(const BIGNUM *a, unsigned char *to, int tolen) in BN_bn2nativepad() argument
684 return BN_bn2lebinpad(a, to, tolen); in BN_bn2nativepad()
685 return BN_bn2binpad(a, to, tolen); in BN_bn2nativepad()
688 int BN_signed_bn2native(const BIGNUM *a, unsigned char *to, int tolen) in BN_signed_bn2native() argument
693 return BN_signed_bn2lebin(a, to, tolen); in BN_signed_bn2native()
694 return BN_signed_bn2bin(a, to, tolen); in BN_signed_bn2native()
697 int BN_ucmp(const BIGNUM *a, const BIGNUM *b) in BN_ucmp() argument
702 bn_check_top(a); in BN_ucmp()
705 i = a->top - b->top; in BN_ucmp()
708 ap = a->d; in BN_ucmp()
710 for (i = a->top - 1; i >= 0; i--) { in BN_ucmp()
719 int BN_cmp(const BIGNUM *a, const BIGNUM *b) in BN_cmp() argument
725 if ((a == NULL) || (b == NULL)) { in BN_cmp()
726 if (a != NULL) in BN_cmp()
734 bn_check_top(a); in BN_cmp()
737 if (a->neg != b->neg) { in BN_cmp()
738 if (a->neg) in BN_cmp()
743 if (a->neg == 0) { in BN_cmp()
751 if (a->top > b->top) in BN_cmp()
753 if (a->top < b->top) in BN_cmp()
755 for (i = a->top - 1; i >= 0; i--) { in BN_cmp()
756 t1 = a->d[i]; in BN_cmp()
766 int BN_set_bit(BIGNUM *a, int n) in BN_set_bit() argument
775 if (a->top <= i) { in BN_set_bit()
776 if (bn_wexpand(a, i + 1) == NULL) in BN_set_bit()
778 for (k = a->top; k < i + 1; k++) in BN_set_bit()
779 a->d[k] = 0; in BN_set_bit()
780 a->top = i + 1; in BN_set_bit()
781 a->flags &= ~BN_FLG_FIXED_TOP; in BN_set_bit()
784 a->d[i] |= (((BN_ULONG)1) << j); in BN_set_bit()
785 bn_check_top(a); in BN_set_bit()
789 int BN_clear_bit(BIGNUM *a, int n) in BN_clear_bit() argument
793 bn_check_top(a); in BN_clear_bit()
799 if (a->top <= i) in BN_clear_bit()
802 a->d[i] &= (~(((BN_ULONG)1) << j)); in BN_clear_bit()
803 bn_correct_top(a); in BN_clear_bit()
807 int BN_is_bit_set(const BIGNUM *a, int n) in BN_is_bit_set() argument
811 bn_check_top(a); in BN_is_bit_set()
816 if (a->top <= i) in BN_is_bit_set()
818 return (int)(((a->d[i]) >> j) & ((BN_ULONG)1)); in BN_is_bit_set()
821 int BN_mask_bits(BIGNUM *a, int n) in BN_mask_bits() argument
825 bn_check_top(a); in BN_mask_bits()
831 if (w >= a->top) in BN_mask_bits()
834 a->top = w; in BN_mask_bits()
836 a->top = w + 1; in BN_mask_bits()
837 a->d[w] &= ~(BN_MASK2 << b); in BN_mask_bits()
839 bn_correct_top(a); in BN_mask_bits()
843 void BN_set_negative(BIGNUM *a, int b) in BN_set_negative() argument
845 if (b && !BN_is_zero(a)) in BN_set_negative()
846 a->neg = 1; in BN_set_negative()
848 a->neg = 0; in BN_set_negative()
851 int bn_cmp_words(const BN_ULONG *a, const BN_ULONG *b, int n) in bn_cmp_words() argument
859 aa = a[n - 1]; in bn_cmp_words()
864 aa = a[i]; in bn_cmp_words()
881 int bn_cmp_part_words(const BN_ULONG *a, const BN_ULONG *b, int cl, int dl) in bn_cmp_part_words() argument
894 if (a[n + i] != 0) in bn_cmp_part_words()
898 return bn_cmp_words(a, b, cl); in bn_cmp_part_words()
908 void BN_consttime_swap(BN_ULONG condition, BIGNUM *a, BIGNUM *b, int nwords) in BN_consttime_swap() argument
913 bn_wcheck_size(a, nwords); in BN_consttime_swap()
918 t = (a->top ^ b->top) & condition; in BN_consttime_swap()
919 a->top ^= t; in BN_consttime_swap()
922 t = (a->neg ^ b->neg) & condition; in BN_consttime_swap()
923 a->neg ^= t; in BN_consttime_swap()
950 t = ((a->flags ^ b->flags) & BN_CONSTTIME_SWAP_FLAGS) & condition; in BN_consttime_swap()
951 a->flags ^= t; in BN_consttime_swap()
956 t = (a->d[i] ^ b->d[i]) & condition; in BN_consttime_swap()
957 a->d[i] ^= t; in BN_consttime_swap()
989 void BN_zero_ex(BIGNUM *a) in BN_zero_ex() argument
991 a->neg = 0; in BN_zero_ex()
992 a->top = 0; in BN_zero_ex()
993 a->flags &= ~BN_FLG_FIXED_TOP; in BN_zero_ex()
996 int BN_abs_is_word(const BIGNUM *a, const BN_ULONG w) in BN_abs_is_word() argument
998 return ((a->top == 1) && (a->d[0] == w)) || ((w == 0) && (a->top == 0)); in BN_abs_is_word()
1001 int BN_is_zero(const BIGNUM *a) in BN_is_zero() argument
1003 return a->top == 0; in BN_is_zero()
1006 int BN_is_one(const BIGNUM *a) in BN_is_one() argument
1008 return BN_abs_is_word(a, 1) && !a->neg; in BN_is_one()
1011 int BN_is_word(const BIGNUM *a, const BN_ULONG w) in BN_is_word() argument
1013 return BN_abs_is_word(a, w) && (!w || !a->neg); in BN_is_word()
1016 int BN_is_odd(const BIGNUM *a) in BN_is_odd() argument
1018 return (a->top > 0) && (a->d[0] & 1); in BN_is_odd()
1021 int BN_is_negative(const BIGNUM *a) in BN_is_negative() argument
1023 return (a->neg != 0); in BN_is_negative()
1026 int BN_to_montgomery(BIGNUM *r, const BIGNUM *a, BN_MONT_CTX *mont, in BN_to_montgomery() argument
1029 return BN_mod_mul_montgomery(r, a, &(mont->RR), mont, ctx); in BN_to_montgomery()
1097 BIGNUM *bn_wexpand(BIGNUM *a, int words) in bn_wexpand() argument
1099 return (words <= a->dmax) ? a : bn_expand2(a, words); in bn_wexpand()
1102 void bn_correct_top(BIGNUM *a) in bn_correct_top() argument
1105 int tmp_top = a->top; in bn_correct_top()
1108 for (ftl = &(a->d[tmp_top]); tmp_top > 0; tmp_top--) { in bn_correct_top()
1113 a->top = tmp_top; in bn_correct_top()
1115 if (a->top == 0) in bn_correct_top()
1116 a->neg = 0; in bn_correct_top()
1117 a->flags &= ~BN_FLG_FIXED_TOP; in bn_correct_top()
1118 bn_pollute(a); in bn_correct_top()