code
stringlengths
49
1.37M
repo_name
stringclasses
117 values
path
stringlengths
17
73
from
stringclasses
1 value
#include <stdio.h> #include "internal/cryptlib.h" #include "bn_local.h" #define TABLE_SIZE 32 int BN_mod_exp2_mont(BIGNUM *rr, const BIGNUM *a1, const BIGNUM *p1, const BIGNUM *a2, const BIGNUM *p2, const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont) { int i, j, bits, b, bits1, bits2, ret = 0, wpos1, wpos2, window1, window2, wvalue1, wvalue2; int r_is_one = 1; BIGNUM *d, *r; const BIGNUM *a_mod_m; BIGNUM *val1[TABLE_SIZE], *val2[TABLE_SIZE]; BN_MONT_CTX *mont = NULL; bn_check_top(a1); bn_check_top(p1); bn_check_top(a2); bn_check_top(p2); bn_check_top(m); if (!BN_is_odd(m)) { ERR_raise(ERR_LIB_BN, BN_R_CALLED_WITH_EVEN_MODULUS); return 0; } bits1 = BN_num_bits(p1); bits2 = BN_num_bits(p2); if ((bits1 == 0) && (bits2 == 0)) { ret = BN_one(rr); return ret; } bits = (bits1 > bits2) ? bits1 : bits2; BN_CTX_start(ctx); d = BN_CTX_get(ctx); r = BN_CTX_get(ctx); val1[0] = BN_CTX_get(ctx); val2[0] = BN_CTX_get(ctx); if (val2[0] == NULL) goto err; if (in_mont != NULL) mont = in_mont; else { if ((mont = BN_MONT_CTX_new()) == NULL) goto err; if (!BN_MONT_CTX_set(mont, m, ctx)) goto err; } window1 = BN_window_bits_for_exponent_size(bits1); window2 = BN_window_bits_for_exponent_size(bits2); if (a1->neg || BN_ucmp(a1, m) >= 0) { if (!BN_mod(val1[0], a1, m, ctx)) goto err; a_mod_m = val1[0]; } else a_mod_m = a1; if (BN_is_zero(a_mod_m)) { BN_zero(rr); ret = 1; goto err; } if (!BN_to_montgomery(val1[0], a_mod_m, mont, ctx)) goto err; if (window1 > 1) { if (!BN_mod_mul_montgomery(d, val1[0], val1[0], mont, ctx)) goto err; j = 1 << (window1 - 1); for (i = 1; i < j; i++) { if (((val1[i] = BN_CTX_get(ctx)) == NULL) || !BN_mod_mul_montgomery(val1[i], val1[i - 1], d, mont, ctx)) goto err; } } if (a2->neg || BN_ucmp(a2, m) >= 0) { if (!BN_mod(val2[0], a2, m, ctx)) goto err; a_mod_m = val2[0]; } else a_mod_m = a2; if (BN_is_zero(a_mod_m)) { BN_zero(rr); ret = 1; goto err; } if (!BN_to_montgomery(val2[0], a_mod_m, mont, ctx)) goto err; if (window2 > 1) { if (!BN_mod_mul_montgomery(d, val2[0], val2[0], mont, ctx)) goto err; j = 1 << (window2 - 1); for (i = 1; i < j; i++) { if (((val2[i] = BN_CTX_get(ctx)) == NULL) || !BN_mod_mul_montgomery(val2[i], val2[i - 1], d, mont, ctx)) goto err; } } r_is_one = 1; wvalue1 = 0; wvalue2 = 0; wpos1 = 0; wpos2 = 0; if (!BN_to_montgomery(r, BN_value_one(), mont, ctx)) goto err; for (b = bits - 1; b >= 0; b--) { if (!r_is_one) { if (!BN_mod_mul_montgomery(r, r, r, mont, ctx)) goto err; } if (!wvalue1) if (BN_is_bit_set(p1, b)) { i = b - window1 + 1; while (!BN_is_bit_set(p1, i)) i++; wpos1 = i; wvalue1 = 1; for (i = b - 1; i >= wpos1; i--) { wvalue1 <<= 1; if (BN_is_bit_set(p1, i)) wvalue1++; } } if (!wvalue2) if (BN_is_bit_set(p2, b)) { i = b - window2 + 1; while (!BN_is_bit_set(p2, i)) i++; wpos2 = i; wvalue2 = 1; for (i = b - 1; i >= wpos2; i--) { wvalue2 <<= 1; if (BN_is_bit_set(p2, i)) wvalue2++; } } if (wvalue1 && b == wpos1) { if (!BN_mod_mul_montgomery(r, r, val1[wvalue1 >> 1], mont, ctx)) goto err; wvalue1 = 0; r_is_one = 0; } if (wvalue2 && b == wpos2) { if (!BN_mod_mul_montgomery(r, r, val2[wvalue2 >> 1], mont, ctx)) goto err; wvalue2 = 0; r_is_one = 0; } } if (!BN_from_montgomery(rr, r, mont, ctx)) goto err; ret = 1; err: if (in_mont == NULL) BN_MONT_CTX_free(mont); BN_CTX_end(ctx); bn_check_top(rr); return ret; }
bn
openssl/crypto/bn/bn_exp2.c
openssl
#include "internal/cryptlib.h" #include "bn_local.h" BIGNUM *BN_mod_sqrt(BIGNUM *in, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx) { BIGNUM *ret = in; int err = 1; int r; BIGNUM *A, *b, *q, *t, *x, *y; int e, i, j; int used_ctx = 0; if (!BN_is_odd(p) || BN_abs_is_word(p, 1)) { if (BN_abs_is_word(p, 2)) { if (ret == NULL) ret = BN_new(); if (ret == NULL) goto end; if (!BN_set_word(ret, BN_is_bit_set(a, 0))) { if (ret != in) BN_free(ret); return NULL; } bn_check_top(ret); return ret; } ERR_raise(ERR_LIB_BN, BN_R_P_IS_NOT_PRIME); return NULL; } if (BN_is_zero(a) || BN_is_one(a)) { if (ret == NULL) ret = BN_new(); if (ret == NULL) goto end; if (!BN_set_word(ret, BN_is_one(a))) { if (ret != in) BN_free(ret); return NULL; } bn_check_top(ret); return ret; } BN_CTX_start(ctx); used_ctx = 1; A = BN_CTX_get(ctx); b = BN_CTX_get(ctx); q = BN_CTX_get(ctx); t = BN_CTX_get(ctx); x = BN_CTX_get(ctx); y = BN_CTX_get(ctx); if (y == NULL) goto end; if (ret == NULL) ret = BN_new(); if (ret == NULL) goto end; if (!BN_nnmod(A, a, p, ctx)) goto end; e = 1; while (!BN_is_bit_set(p, e)) e++; if (e == 1) { if (!BN_rshift(q, p, 2)) goto end; q->neg = 0; if (!BN_add_word(q, 1)) goto end; if (!BN_mod_exp(ret, A, q, p, ctx)) goto end; err = 0; goto vrfy; } if (e == 2) { if (!BN_mod_lshift1_quick(t, A, p)) goto end; if (!BN_rshift(q, p, 3)) goto end; q->neg = 0; if (!BN_mod_exp(b, t, q, p, ctx)) goto end; if (!BN_mod_sqr(y, b, p, ctx)) goto end; if (!BN_mod_mul(t, t, y, p, ctx)) goto end; if (!BN_sub_word(t, 1)) goto end; if (!BN_mod_mul(x, A, b, p, ctx)) goto end; if (!BN_mod_mul(x, x, t, p, ctx)) goto end; if (!BN_copy(ret, x)) goto end; err = 0; goto vrfy; } if (!BN_copy(q, p)) goto end; q->neg = 0; i = 2; do { if (i < 22) { if (!BN_set_word(y, i)) goto end; } else { if (!BN_priv_rand_ex(y, BN_num_bits(p), 0, 0, 0, ctx)) goto end; if (BN_ucmp(y, p) >= 0) { if (!(p->neg ? BN_add : BN_sub) (y, y, p)) goto end; } if (BN_is_zero(y)) if (!BN_set_word(y, i)) goto end; } r = BN_kronecker(y, q, ctx); if (r < -1) goto end; if (r == 0) { ERR_raise(ERR_LIB_BN, BN_R_P_IS_NOT_PRIME); goto end; } } while (r == 1 && ++i < 82); if (r != -1) { ERR_raise(ERR_LIB_BN, BN_R_TOO_MANY_ITERATIONS); goto end; } if (!BN_rshift(q, q, e)) goto end; if (!BN_mod_exp(y, y, q, p, ctx)) goto end; if (BN_is_one(y)) { ERR_raise(ERR_LIB_BN, BN_R_P_IS_NOT_PRIME); goto end; } if (!BN_rshift1(t, q)) goto end; if (BN_is_zero(t)) { if (!BN_nnmod(t, A, p, ctx)) goto end; if (BN_is_zero(t)) { BN_zero(ret); err = 0; goto end; } else if (!BN_one(x)) goto end; } else { if (!BN_mod_exp(x, A, t, p, ctx)) goto end; if (BN_is_zero(x)) { BN_zero(ret); err = 0; goto end; } } if (!BN_mod_sqr(b, x, p, ctx)) goto end; if (!BN_mod_mul(b, b, A, p, ctx)) goto end; if (!BN_mod_mul(x, x, A, p, ctx)) goto end; while (1) { if (BN_is_one(b)) { if (!BN_copy(ret, x)) goto end; err = 0; goto vrfy; } for (i = 1; i < e; i++) { if (i == 1) { if (!BN_mod_sqr(t, b, p, ctx)) goto end; } else { if (!BN_mod_mul(t, t, t, p, ctx)) goto end; } if (BN_is_one(t)) break; } if (i >= e) { ERR_raise(ERR_LIB_BN, BN_R_NOT_A_SQUARE); goto end; } if (!BN_copy(t, y)) goto end; for (j = e - i - 1; j > 0; j--) { if (!BN_mod_sqr(t, t, p, ctx)) goto end; } if (!BN_mod_mul(y, t, t, p, ctx)) goto end; if (!BN_mod_mul(x, x, t, p, ctx)) goto end; if (!BN_mod_mul(b, b, y, p, ctx)) goto end; e = i; } vrfy: if (!err) { if (!BN_mod_sqr(x, ret, p, ctx)) err = 1; if (!err && 0 != BN_cmp(x, A)) { ERR_raise(ERR_LIB_BN, BN_R_NOT_A_SQUARE); err = 1; } } end: if (err) { if (ret != in) BN_clear_free(ret); ret = NULL; } if (used_ctx) BN_CTX_end(ctx); bn_check_top(ret); return ret; }
bn
openssl/crypto/bn/bn_sqrt.c
openssl
#include <assert.h> #include "internal/cryptlib.h" #include "bn_local.h" #if defined(OPENSSL_NO_ASM) || !defined(OPENSSL_BN_ASM_PART_WORDS) BN_ULONG bn_sub_part_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b, int cl, int dl) { BN_ULONG c, t; assert(cl >= 0); c = bn_sub_words(r, a, b, cl); if (dl == 0) return c; r += cl; a += cl; b += cl; if (dl < 0) { for (;;) { t = b[0]; r[0] = (0 - t - c) & BN_MASK2; if (t != 0) c = 1; if (++dl >= 0) break; t = b[1]; r[1] = (0 - t - c) & BN_MASK2; if (t != 0) c = 1; if (++dl >= 0) break; t = b[2]; r[2] = (0 - t - c) & BN_MASK2; if (t != 0) c = 1; if (++dl >= 0) break; t = b[3]; r[3] = (0 - t - c) & BN_MASK2; if (t != 0) c = 1; if (++dl >= 0) break; b += 4; r += 4; } } else { int save_dl = dl; while (c) { t = a[0]; r[0] = (t - c) & BN_MASK2; if (t != 0) c = 0; if (--dl <= 0) break; t = a[1]; r[1] = (t - c) & BN_MASK2; if (t != 0) c = 0; if (--dl <= 0) break; t = a[2]; r[2] = (t - c) & BN_MASK2; if (t != 0) c = 0; if (--dl <= 0) break; t = a[3]; r[3] = (t - c) & BN_MASK2; if (t != 0) c = 0; if (--dl <= 0) break; save_dl = dl; a += 4; r += 4; } if (dl > 0) { if (save_dl > dl) { switch (save_dl - dl) { case 1: r[1] = a[1]; if (--dl <= 0) break; case 2: r[2] = a[2]; if (--dl <= 0) break; case 3: r[3] = a[3]; if (--dl <= 0) break; } a += 4; r += 4; } } if (dl > 0) { for (;;) { r[0] = a[0]; if (--dl <= 0) break; r[1] = a[1]; if (--dl <= 0) break; r[2] = a[2]; if (--dl <= 0) break; r[3] = a[3]; if (--dl <= 0) break; a += 4; r += 4; } } } return c; } #endif #ifdef BN_RECURSION void bn_mul_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n2, int dna, int dnb, BN_ULONG *t) { int n = n2 / 2, c1, c2; int tna = n + dna, tnb = n + dnb; unsigned int neg, zero; BN_ULONG ln, lo, *p; # ifdef BN_MUL_COMBA # if 0 if (n2 == 4) { bn_mul_comba4(r, a, b); return; } # endif if (n2 == 8 && dna == 0 && dnb == 0) { bn_mul_comba8(r, a, b); return; } # endif if (n2 < BN_MUL_RECURSIVE_SIZE_NORMAL) { bn_mul_normal(r, a, n2 + dna, b, n2 + dnb); if ((dna + dnb) < 0) memset(&r[2 * n2 + dna + dnb], 0, sizeof(BN_ULONG) * -(dna + dnb)); return; } c1 = bn_cmp_part_words(a, &(a[n]), tna, n - tna); c2 = bn_cmp_part_words(&(b[n]), b, tnb, tnb - n); zero = neg = 0; switch (c1 * 3 + c2) { case -4: bn_sub_part_words(t, &(a[n]), a, tna, tna - n); bn_sub_part_words(&(t[n]), b, &(b[n]), tnb, n - tnb); break; case -3: zero = 1; break; case -2: bn_sub_part_words(t, &(a[n]), a, tna, tna - n); bn_sub_part_words(&(t[n]), &(b[n]), b, tnb, tnb - n); neg = 1; break; case -1: case 0: case 1: zero = 1; break; case 2: bn_sub_part_words(t, a, &(a[n]), tna, n - tna); bn_sub_part_words(&(t[n]), b, &(b[n]), tnb, n - tnb); neg = 1; break; case 3: zero = 1; break; case 4: bn_sub_part_words(t, a, &(a[n]), tna, n - tna); bn_sub_part_words(&(t[n]), &(b[n]), b, tnb, tnb - n); break; } # ifdef BN_MUL_COMBA if (n == 4 && dna == 0 && dnb == 0) { if (!zero) bn_mul_comba4(&(t[n2]), t, &(t[n])); else memset(&t[n2], 0, sizeof(*t) * 8); bn_mul_comba4(r, a, b); bn_mul_comba4(&(r[n2]), &(a[n]), &(b[n])); } else if (n == 8 && dna == 0 && dnb == 0) { if (!zero) bn_mul_comba8(&(t[n2]), t, &(t[n])); else memset(&t[n2], 0, sizeof(*t) * 16); bn_mul_comba8(r, a, b); bn_mul_comba8(&(r[n2]), &(a[n]), &(b[n])); } else # endif { p = &(t[n2 * 2]); if (!zero) bn_mul_recursive(&(t[n2]), t, &(t[n]), n, 0, 0, p); else memset(&t[n2], 0, sizeof(*t) * n2); bn_mul_recursive(r, a, b, n, 0, 0, p); bn_mul_recursive(&(r[n2]), &(a[n]), &(b[n]), n, dna, dnb, p); } c1 = (int)(bn_add_words(t, r, &(r[n2]), n2)); if (neg) { c1 -= (int)(bn_sub_words(&(t[n2]), t, &(t[n2]), n2)); } else { c1 += (int)(bn_add_words(&(t[n2]), &(t[n2]), t, n2)); } c1 += (int)(bn_add_words(&(r[n]), &(r[n]), &(t[n2]), n2)); if (c1) { p = &(r[n + n2]); lo = *p; ln = (lo + c1) & BN_MASK2; *p = ln; if (ln < (BN_ULONG)c1) { do { p++; lo = *p; ln = (lo + 1) & BN_MASK2; *p = ln; } while (ln == 0); } } } void bn_mul_part_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n, int tna, int tnb, BN_ULONG *t) { int i, j, n2 = n * 2; int c1, c2, neg; BN_ULONG ln, lo, *p; if (n < 8) { bn_mul_normal(r, a, n + tna, b, n + tnb); return; } c1 = bn_cmp_part_words(a, &(a[n]), tna, n - tna); c2 = bn_cmp_part_words(&(b[n]), b, tnb, tnb - n); neg = 0; switch (c1 * 3 + c2) { case -4: bn_sub_part_words(t, &(a[n]), a, tna, tna - n); bn_sub_part_words(&(t[n]), b, &(b[n]), tnb, n - tnb); break; case -3: case -2: bn_sub_part_words(t, &(a[n]), a, tna, tna - n); bn_sub_part_words(&(t[n]), &(b[n]), b, tnb, tnb - n); neg = 1; break; case -1: case 0: case 1: case 2: bn_sub_part_words(t, a, &(a[n]), tna, n - tna); bn_sub_part_words(&(t[n]), b, &(b[n]), tnb, n - tnb); neg = 1; break; case 3: case 4: bn_sub_part_words(t, a, &(a[n]), tna, n - tna); bn_sub_part_words(&(t[n]), &(b[n]), b, tnb, tnb - n); break; } # if 0 if (n == 4) { bn_mul_comba4(&(t[n2]), t, &(t[n])); bn_mul_comba4(r, a, b); bn_mul_normal(&(r[n2]), &(a[n]), tn, &(b[n]), tn); memset(&r[n2 + tn * 2], 0, sizeof(*r) * (n2 - tn * 2)); } else # endif if (n == 8) { bn_mul_comba8(&(t[n2]), t, &(t[n])); bn_mul_comba8(r, a, b); bn_mul_normal(&(r[n2]), &(a[n]), tna, &(b[n]), tnb); memset(&r[n2 + tna + tnb], 0, sizeof(*r) * (n2 - tna - tnb)); } else { p = &(t[n2 * 2]); bn_mul_recursive(&(t[n2]), t, &(t[n]), n, 0, 0, p); bn_mul_recursive(r, a, b, n, 0, 0, p); i = n / 2; if (tna > tnb) j = tna - i; else j = tnb - i; if (j == 0) { bn_mul_recursive(&(r[n2]), &(a[n]), &(b[n]), i, tna - i, tnb - i, p); memset(&r[n2 + i * 2], 0, sizeof(*r) * (n2 - i * 2)); } else if (j > 0) { bn_mul_part_recursive(&(r[n2]), &(a[n]), &(b[n]), i, tna - i, tnb - i, p); memset(&(r[n2 + tna + tnb]), 0, sizeof(BN_ULONG) * (n2 - tna - tnb)); } else { memset(&r[n2], 0, sizeof(*r) * n2); if (tna < BN_MUL_RECURSIVE_SIZE_NORMAL && tnb < BN_MUL_RECURSIVE_SIZE_NORMAL) { bn_mul_normal(&(r[n2]), &(a[n]), tna, &(b[n]), tnb); } else { for (;;) { i /= 2; if (i < tna || i < tnb) { bn_mul_part_recursive(&(r[n2]), &(a[n]), &(b[n]), i, tna - i, tnb - i, p); break; } else if (i == tna || i == tnb) { bn_mul_recursive(&(r[n2]), &(a[n]), &(b[n]), i, tna - i, tnb - i, p); break; } } } } } c1 = (int)(bn_add_words(t, r, &(r[n2]), n2)); if (neg) { c1 -= (int)(bn_sub_words(&(t[n2]), t, &(t[n2]), n2)); } else { c1 += (int)(bn_add_words(&(t[n2]), &(t[n2]), t, n2)); } c1 += (int)(bn_add_words(&(r[n]), &(r[n]), &(t[n2]), n2)); if (c1) { p = &(r[n + n2]); lo = *p; ln = (lo + c1) & BN_MASK2; *p = ln; if (ln < (BN_ULONG)c1) { do { p++; lo = *p; ln = (lo + 1) & BN_MASK2; *p = ln; } while (ln == 0); } } } void bn_mul_low_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n2, BN_ULONG *t) { int n = n2 / 2; bn_mul_recursive(r, a, b, n, 0, 0, &(t[0])); if (n >= BN_MUL_LOW_RECURSIVE_SIZE_NORMAL) { bn_mul_low_recursive(&(t[0]), &(a[0]), &(b[n]), n, &(t[n2])); bn_add_words(&(r[n]), &(r[n]), &(t[0]), n); bn_mul_low_recursive(&(t[0]), &(a[n]), &(b[0]), n, &(t[n2])); bn_add_words(&(r[n]), &(r[n]), &(t[0]), n); } else { bn_mul_low_normal(&(t[0]), &(a[0]), &(b[n]), n); bn_mul_low_normal(&(t[n]), &(a[n]), &(b[0]), n); bn_add_words(&(r[n]), &(r[n]), &(t[0]), n); bn_add_words(&(r[n]), &(r[n]), &(t[n]), n); } } #endif int BN_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx) { int ret = bn_mul_fixed_top(r, a, b, ctx); bn_correct_top(r); bn_check_top(r); return ret; } int bn_mul_fixed_top(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx) { int ret = 0; int top, al, bl; BIGNUM *rr; #if defined(BN_MUL_COMBA) || defined(BN_RECURSION) int i; #endif #ifdef BN_RECURSION BIGNUM *t = NULL; int j = 0, k; #endif bn_check_top(a); bn_check_top(b); bn_check_top(r); al = a->top; bl = b->top; if ((al == 0) || (bl == 0)) { BN_zero(r); return 1; } top = al + bl; BN_CTX_start(ctx); if ((r == a) || (r == b)) { if ((rr = BN_CTX_get(ctx)) == NULL) goto err; } else rr = r; #if defined(BN_MUL_COMBA) || defined(BN_RECURSION) i = al - bl; #endif #ifdef BN_MUL_COMBA if (i == 0) { # if 0 if (al == 4) { if (bn_wexpand(rr, 8) == NULL) goto err; rr->top = 8; bn_mul_comba4(rr->d, a->d, b->d); goto end; } # endif if (al == 8) { if (bn_wexpand(rr, 16) == NULL) goto err; rr->top = 16; bn_mul_comba8(rr->d, a->d, b->d); goto end; } } #endif #ifdef BN_RECURSION if ((al >= BN_MULL_SIZE_NORMAL) && (bl >= BN_MULL_SIZE_NORMAL)) { if (i >= -1 && i <= 1) { if (i >= 0) { j = BN_num_bits_word((BN_ULONG)al); } if (i == -1) { j = BN_num_bits_word((BN_ULONG)bl); } j = 1 << (j - 1); assert(j <= al || j <= bl); k = j + j; t = BN_CTX_get(ctx); if (t == NULL) goto err; if (al > j || bl > j) { if (bn_wexpand(t, k * 4) == NULL) goto err; if (bn_wexpand(rr, k * 4) == NULL) goto err; bn_mul_part_recursive(rr->d, a->d, b->d, j, al - j, bl - j, t->d); } else { if (bn_wexpand(t, k * 2) == NULL) goto err; if (bn_wexpand(rr, k * 2) == NULL) goto err; bn_mul_recursive(rr->d, a->d, b->d, j, al - j, bl - j, t->d); } rr->top = top; goto end; } } #endif if (bn_wexpand(rr, top) == NULL) goto err; rr->top = top; bn_mul_normal(rr->d, a->d, al, b->d, bl); #if defined(BN_MUL_COMBA) || defined(BN_RECURSION) end: #endif rr->neg = a->neg ^ b->neg; rr->flags |= BN_FLG_FIXED_TOP; if (r != rr && BN_copy(r, rr) == NULL) goto err; ret = 1; err: bn_check_top(r); BN_CTX_end(ctx); return ret; } void bn_mul_normal(BN_ULONG *r, BN_ULONG *a, int na, BN_ULONG *b, int nb) { BN_ULONG *rr; if (na < nb) { int itmp; BN_ULONG *ltmp; itmp = na; na = nb; nb = itmp; ltmp = a; a = b; b = ltmp; } rr = &(r[na]); if (nb <= 0) { (void)bn_mul_words(r, a, na, 0); return; } else rr[0] = bn_mul_words(r, a, na, b[0]); for (;;) { if (--nb <= 0) return; rr[1] = bn_mul_add_words(&(r[1]), a, na, b[1]); if (--nb <= 0) return; rr[2] = bn_mul_add_words(&(r[2]), a, na, b[2]); if (--nb <= 0) return; rr[3] = bn_mul_add_words(&(r[3]), a, na, b[3]); if (--nb <= 0) return; rr[4] = bn_mul_add_words(&(r[4]), a, na, b[4]); rr += 4; r += 4; b += 4; } } void bn_mul_low_normal(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n) { bn_mul_words(r, a, n, b[0]); for (;;) { if (--n <= 0) return; bn_mul_add_words(&(r[1]), a, n, b[1]); if (--n <= 0) return; bn_mul_add_words(&(r[2]), a, n, b[2]); if (--n <= 0) return; bn_mul_add_words(&(r[3]), a, n, b[3]); if (--n <= 0) return; bn_mul_add_words(&(r[4]), a, n, b[4]); r += 4; b += 4; } }
bn
openssl/crypto/bn/bn_mul.c
openssl
#include <openssl/err.h> #include "crypto/ctype.h" #include "bn_local.h" static const char Hex[] = "0123456789ABCDEF"; char *BN_bn2hex(const BIGNUM *a) { int i, j, v, z = 0; char *buf; char *p; if (BN_is_zero(a)) return OPENSSL_strdup("0"); buf = OPENSSL_malloc(a->top * BN_BYTES * 2 + 2); if (buf == NULL) goto err; p = buf; if (a->neg) *p++ = '-'; for (i = a->top - 1; i >= 0; i--) { for (j = BN_BITS2 - 8; j >= 0; j -= 8) { v = (int)((a->d[i] >> j) & 0xff); if (z || v != 0) { *p++ = Hex[v >> 4]; *p++ = Hex[v & 0x0f]; z = 1; } } } *p = '\0'; err: return buf; } #ifndef FIPS_MODULE char *BN_bn2dec(const BIGNUM *a) { int i = 0, num, ok = 0, n, tbytes; char *buf = NULL; char *p; BIGNUM *t = NULL; BN_ULONG *bn_data = NULL, *lp; int bn_data_num; i = BN_num_bits(a) * 3; num = (i / 10 + i / 1000 + 1) + 1; tbytes = num + 3; bn_data_num = num / BN_DEC_NUM + 1; bn_data = OPENSSL_malloc(bn_data_num * sizeof(BN_ULONG)); buf = OPENSSL_malloc(tbytes); if (buf == NULL || bn_data == NULL) goto err; if ((t = BN_dup(a)) == NULL) goto err; p = buf; lp = bn_data; if (BN_is_zero(t)) { *p++ = '0'; *p++ = '\0'; } else { if (BN_is_negative(t)) *p++ = '-'; while (!BN_is_zero(t)) { if (lp - bn_data >= bn_data_num) goto err; *lp = BN_div_word(t, BN_DEC_CONV); if (*lp == (BN_ULONG)-1) goto err; lp++; } lp--; n = BIO_snprintf(p, tbytes - (size_t)(p - buf), BN_DEC_FMT1, *lp); if (n < 0) goto err; p += n; while (lp != bn_data) { lp--; n = BIO_snprintf(p, tbytes - (size_t)(p - buf), BN_DEC_FMT2, *lp); if (n < 0) goto err; p += n; } } ok = 1; err: OPENSSL_free(bn_data); BN_free(t); if (ok) return buf; OPENSSL_free(buf); return NULL; } #endif int BN_hex2bn(BIGNUM **bn, const char *a) { BIGNUM *ret = NULL; BN_ULONG l = 0; int neg = 0, h, m, i, j, k, c; int num; if (a == NULL || *a == '\0') return 0; if (*a == '-') { neg = 1; a++; } for (i = 0; i <= INT_MAX / 4 && ossl_isxdigit(a[i]); i++) continue; if (i == 0 || i > INT_MAX / 4) return 0; num = i + neg; if (bn == NULL) return num; if (*bn == NULL) { if ((ret = BN_new()) == NULL) return 0; } else { ret = *bn; if (BN_get_flags(ret, BN_FLG_STATIC_DATA)) { ERR_raise(ERR_LIB_BN, ERR_R_PASSED_INVALID_ARGUMENT); return 0; } BN_zero(ret); } if (bn_expand(ret, i * 4) == NULL) goto err; j = i; m = 0; h = 0; while (j > 0) { m = (BN_BYTES * 2 <= j) ? BN_BYTES * 2 : j; l = 0; for (;;) { c = a[j - m]; k = OPENSSL_hexchar2int(c); if (k < 0) k = 0; l = (l << 4) | k; if (--m <= 0) { ret->d[h++] = l; break; } } j -= BN_BYTES * 2; } ret->top = h; bn_correct_top(ret); *bn = ret; bn_check_top(ret); if (ret->top != 0) ret->neg = neg; return num; err: if (*bn == NULL) BN_free(ret); return 0; } int BN_dec2bn(BIGNUM **bn, const char *a) { BIGNUM *ret = NULL; BN_ULONG l = 0; int neg = 0, i, j; int num; if (a == NULL || *a == '\0') return 0; if (*a == '-') { neg = 1; a++; } for (i = 0; i <= INT_MAX / 4 && ossl_isdigit(a[i]); i++) continue; if (i == 0 || i > INT_MAX / 4) goto err; num = i + neg; if (bn == NULL) return num; if (*bn == NULL) { if ((ret = BN_new()) == NULL) return 0; } else { ret = *bn; BN_zero(ret); } if (bn_expand(ret, i * 4) == NULL) goto err; j = BN_DEC_NUM - i % BN_DEC_NUM; if (j == BN_DEC_NUM) j = 0; l = 0; while (--i >= 0) { l *= 10; l += *a - '0'; a++; if (++j == BN_DEC_NUM) { if (!BN_mul_word(ret, BN_DEC_CONV) || !BN_add_word(ret, l)) goto err; l = 0; j = 0; } } bn_correct_top(ret); *bn = ret; bn_check_top(ret); if (ret->top != 0) ret->neg = neg; return num; err: if (*bn == NULL) BN_free(ret); return 0; } int BN_asc2bn(BIGNUM **bn, const char *a) { const char *p = a; if (*p == '-') p++; if (p[0] == '0' && (p[1] == 'X' || p[1] == 'x')) { if (!BN_hex2bn(bn, p + 2)) return 0; } else { if (!BN_dec2bn(bn, p)) return 0; } if (*a == '-' && (*bn)->top != 0) (*bn)->neg = 1; return 1; }
bn
openssl/crypto/bn/bn_conv.c
openssl
#include <openssl/opensslconf.h> #include <openssl/crypto.h> #include "rsaz_exp.h" #ifndef RSAZ_ENABLED NON_EMPTY_TRANSLATION_UNIT #else # include <assert.h> # include <string.h> # define ALIGN_OF(ptr, boundary) \ ((unsigned char *)(ptr) + (boundary - (((size_t)(ptr)) & (boundary - 1)))) # define DIGIT_SIZE (52) # define DIGIT_MASK ((uint64_t)0xFFFFFFFFFFFFF) # define BITS2WORD8_SIZE(x) (((x) + 7) >> 3) # define BITS2WORD64_SIZE(x) (((x) + 63) >> 6) # define NUMBER_OF_REGISTERS(digits_num, register_size) \ (((digits_num) * 64 + (register_size) - 1) / (register_size)) static ossl_inline uint64_t get_digit(const uint8_t *in, int in_len); static ossl_inline void put_digit(uint8_t *out, int out_len, uint64_t digit); static void to_words52(BN_ULONG *out, int out_len, const BN_ULONG *in, int in_bitsize); static void from_words52(BN_ULONG *bn_out, int out_bitsize, const BN_ULONG *in); static ossl_inline void set_bit(BN_ULONG *a, int idx); static ossl_inline int number_of_digits(int bitsize, int digit_size) { return (bitsize + digit_size - 1) / digit_size; } void ossl_rsaz_amm52x20_x1_ifma256(BN_ULONG *res, const BN_ULONG *a, const BN_ULONG *b, const BN_ULONG *m, BN_ULONG k0); void ossl_rsaz_amm52x20_x2_ifma256(BN_ULONG *out, const BN_ULONG *a, const BN_ULONG *b, const BN_ULONG *m, const BN_ULONG k0[2]); void ossl_extract_multiplier_2x20_win5(BN_ULONG *red_Y, const BN_ULONG *red_table, int red_table_idx1, int red_table_idx2); void ossl_rsaz_amm52x30_x1_ifma256(BN_ULONG *res, const BN_ULONG *a, const BN_ULONG *b, const BN_ULONG *m, BN_ULONG k0); void ossl_rsaz_amm52x30_x2_ifma256(BN_ULONG *out, const BN_ULONG *a, const BN_ULONG *b, const BN_ULONG *m, const BN_ULONG k0[2]); void ossl_extract_multiplier_2x30_win5(BN_ULONG *red_Y, const BN_ULONG *red_table, int red_table_idx1, int red_table_idx2); void ossl_rsaz_amm52x40_x1_ifma256(BN_ULONG *res, const BN_ULONG *a, const BN_ULONG *b, const BN_ULONG *m, BN_ULONG k0); void ossl_rsaz_amm52x40_x2_ifma256(BN_ULONG *out, const BN_ULONG *a, const BN_ULONG *b, const BN_ULONG *m, const BN_ULONG k0[2]); void ossl_extract_multiplier_2x40_win5(BN_ULONG *red_Y, const BN_ULONG *red_table, int red_table_idx1, int red_table_idx2); static int RSAZ_mod_exp_x2_ifma256(BN_ULONG *res, const BN_ULONG *base, const BN_ULONG *exp[2], const BN_ULONG *m, const BN_ULONG *rr, const BN_ULONG k0[2], int modulus_bitsize); int ossl_rsaz_mod_exp_avx512_x2(BN_ULONG *res1, const BN_ULONG *base1, const BN_ULONG *exp1, const BN_ULONG *m1, const BN_ULONG *rr1, BN_ULONG k0_1, BN_ULONG *res2, const BN_ULONG *base2, const BN_ULONG *exp2, const BN_ULONG *m2, const BN_ULONG *rr2, BN_ULONG k0_2, int factor_size) { typedef void (*AMM)(BN_ULONG *res, const BN_ULONG *a, const BN_ULONG *b, const BN_ULONG *m, BN_ULONG k0); int ret = 0; int exp_digits = number_of_digits(factor_size + 2, DIGIT_SIZE); int coeff_pow = 4 * (DIGIT_SIZE * exp_digits - factor_size); int ymm_regs_num = NUMBER_OF_REGISTERS(exp_digits, 256 ); int regs_capacity = ymm_regs_num * 4; BN_ULONG *base1_red, *m1_red, *rr1_red; BN_ULONG *base2_red, *m2_red, *rr2_red; BN_ULONG *coeff_red; BN_ULONG *storage = NULL; BN_ULONG *storage_aligned = NULL; int storage_len_bytes = 7 * regs_capacity * sizeof(BN_ULONG) + 64 ; const BN_ULONG *exp[2] = {0}; BN_ULONG k0[2] = {0}; AMM amm = NULL; switch (factor_size) { case 1024: amm = ossl_rsaz_amm52x20_x1_ifma256; break; case 1536: amm = ossl_rsaz_amm52x30_x1_ifma256; break; case 2048: amm = ossl_rsaz_amm52x40_x1_ifma256; break; default: goto err; } storage = (BN_ULONG *)OPENSSL_malloc(storage_len_bytes); if (storage == NULL) goto err; storage_aligned = (BN_ULONG *)ALIGN_OF(storage, 64); base1_red = storage_aligned; base2_red = storage_aligned + 1 * regs_capacity; m1_red = storage_aligned + 2 * regs_capacity; m2_red = storage_aligned + 3 * regs_capacity; rr1_red = storage_aligned + 4 * regs_capacity; rr2_red = storage_aligned + 5 * regs_capacity; coeff_red = storage_aligned + 6 * regs_capacity; to_words52(base1_red, regs_capacity, base1, factor_size); to_words52(base2_red, regs_capacity, base2, factor_size); to_words52(m1_red, regs_capacity, m1, factor_size); to_words52(m2_red, regs_capacity, m2, factor_size); to_words52(rr1_red, regs_capacity, rr1, factor_size); to_words52(rr2_red, regs_capacity, rr2, factor_size); memset(coeff_red, 0, exp_digits * sizeof(BN_ULONG)); set_bit(coeff_red, 64 * (int)(coeff_pow / 52) + coeff_pow % 52); amm(rr1_red, rr1_red, rr1_red, m1_red, k0_1); amm(rr1_red, rr1_red, coeff_red, m1_red, k0_1); amm(rr2_red, rr2_red, rr2_red, m2_red, k0_2); amm(rr2_red, rr2_red, coeff_red, m2_red, k0_2); exp[0] = exp1; exp[1] = exp2; k0[0] = k0_1; k0[1] = k0_2; ret = RSAZ_mod_exp_x2_ifma256(rr1_red, base1_red, exp, m1_red, rr1_red, k0, factor_size); if (!ret) goto err; from_words52(res1, factor_size, rr1_red); from_words52(res2, factor_size, rr2_red); factor_size /= sizeof(BN_ULONG) * 8; bn_reduce_once_in_place(res1, 0, m1, storage, factor_size); bn_reduce_once_in_place(res2, 0, m2, storage, factor_size); err: if (storage != NULL) { OPENSSL_cleanse(storage, storage_len_bytes); OPENSSL_free(storage); } return ret; } int RSAZ_mod_exp_x2_ifma256(BN_ULONG *out, const BN_ULONG *base, const BN_ULONG *exp[2], const BN_ULONG *m, const BN_ULONG *rr, const BN_ULONG k0[2], int modulus_bitsize) { typedef void (*DAMM)(BN_ULONG *res, const BN_ULONG *a, const BN_ULONG *b, const BN_ULONG *m, const BN_ULONG k0[2]); typedef void (*DEXTRACT)(BN_ULONG *res, const BN_ULONG *red_table, int red_table_idx, int tbl_idx); int ret = 0; int idx; int exp_win_size = 5; int exp_win_mask = (1U << exp_win_size) - 1; int red_digits = 0; int exp_digits = 0; BN_ULONG *storage = NULL; BN_ULONG *storage_aligned = NULL; int storage_len_bytes = 0; BN_ULONG *red_Y = NULL; BN_ULONG *red_X = NULL; BN_ULONG *red_table = NULL; BN_ULONG *expz = NULL; DAMM damm = NULL; DEXTRACT extract = NULL; # define DAMS(r,a,m,k0) damm((r),(a),(a),(m),(k0)) switch (modulus_bitsize) { case 1024: red_digits = 20; exp_digits = 16; damm = ossl_rsaz_amm52x20_x2_ifma256; extract = ossl_extract_multiplier_2x20_win5; break; case 1536: red_digits = 30 + 2; exp_digits = 24; damm = ossl_rsaz_amm52x30_x2_ifma256; extract = ossl_extract_multiplier_2x30_win5; break; case 2048: red_digits = 40; exp_digits = 32; damm = ossl_rsaz_amm52x40_x2_ifma256; extract = ossl_extract_multiplier_2x40_win5; break; default: goto err; } storage_len_bytes = (2 * red_digits + 2 * red_digits + 2 * red_digits * (1U << exp_win_size) + 2 * (exp_digits + 1)) * sizeof(BN_ULONG) + 64; storage = (BN_ULONG *)OPENSSL_zalloc(storage_len_bytes); if (storage == NULL) goto err; storage_aligned = (BN_ULONG *)ALIGN_OF(storage, 64); red_Y = storage_aligned; red_X = red_Y + 2 * red_digits; red_table = red_X + 2 * red_digits; expz = red_table + 2 * red_digits * (1U << exp_win_size); red_X[0 * red_digits] = 1; red_X[1 * red_digits] = 1; damm(&red_table[0 * 2 * red_digits], (const BN_ULONG*)red_X, rr, m, k0); damm(&red_table[1 * 2 * red_digits], base, rr, m, k0); for (idx = 1; idx < (int)((1U << exp_win_size) / 2); idx++) { DAMS(&red_table[(2 * idx + 0) * 2 * red_digits], &red_table[(1 * idx) * 2 * red_digits], m, k0); damm(&red_table[(2 * idx + 1) * 2 * red_digits], &red_table[(2 * idx) * 2 * red_digits], &red_table[1 * 2 * red_digits], m, k0); } memcpy(&expz[0 * (exp_digits + 1)], exp[0], exp_digits * sizeof(BN_ULONG)); expz[1 * (exp_digits + 1) - 1] = 0; memcpy(&expz[1 * (exp_digits + 1)], exp[1], exp_digits * sizeof(BN_ULONG)); expz[2 * (exp_digits + 1) - 1] = 0; { const int rem = modulus_bitsize % exp_win_size; const BN_ULONG table_idx_mask = exp_win_mask; int exp_bit_no = modulus_bitsize - rem; int exp_chunk_no = exp_bit_no / 64; int exp_chunk_shift = exp_bit_no % 64; BN_ULONG red_table_idx_0, red_table_idx_1; OPENSSL_assert(rem != 0); red_table_idx_0 = expz[exp_chunk_no + 0 * (exp_digits + 1)]; red_table_idx_1 = expz[exp_chunk_no + 1 * (exp_digits + 1)]; red_table_idx_0 >>= exp_chunk_shift; red_table_idx_1 >>= exp_chunk_shift; extract(&red_Y[0 * red_digits], (const BN_ULONG*)red_table, (int)red_table_idx_0, (int)red_table_idx_1); for (exp_bit_no -= exp_win_size; exp_bit_no >= 0; exp_bit_no -= exp_win_size) { { BN_ULONG T; exp_chunk_no = exp_bit_no / 64; exp_chunk_shift = exp_bit_no % 64; { red_table_idx_0 = expz[exp_chunk_no + 0 * (exp_digits + 1)]; T = expz[exp_chunk_no + 1 + 0 * (exp_digits + 1)]; red_table_idx_0 >>= exp_chunk_shift; if (exp_chunk_shift > 64 - exp_win_size) { T <<= (64 - exp_chunk_shift); red_table_idx_0 ^= T; } red_table_idx_0 &= table_idx_mask; } { red_table_idx_1 = expz[exp_chunk_no + 1 * (exp_digits + 1)]; T = expz[exp_chunk_no + 1 + 1 * (exp_digits + 1)]; red_table_idx_1 >>= exp_chunk_shift; if (exp_chunk_shift > 64 - exp_win_size) { T <<= (64 - exp_chunk_shift); red_table_idx_1 ^= T; } red_table_idx_1 &= table_idx_mask; } extract(&red_X[0 * red_digits], (const BN_ULONG*)red_table, (int)red_table_idx_0, (int)red_table_idx_1); } DAMS((BN_ULONG*)red_Y, (const BN_ULONG*)red_Y, m, k0); DAMS((BN_ULONG*)red_Y, (const BN_ULONG*)red_Y, m, k0); DAMS((BN_ULONG*)red_Y, (const BN_ULONG*)red_Y, m, k0); DAMS((BN_ULONG*)red_Y, (const BN_ULONG*)red_Y, m, k0); DAMS((BN_ULONG*)red_Y, (const BN_ULONG*)red_Y, m, k0); damm((BN_ULONG*)red_Y, (const BN_ULONG*)red_Y, (const BN_ULONG*)red_X, m, k0); } } memset(red_X, 0, 2 * red_digits * sizeof(BN_ULONG)); red_X[0 * red_digits] = 1; red_X[1 * red_digits] = 1; damm(out, (const BN_ULONG*)red_Y, (const BN_ULONG*)red_X, m, k0); ret = 1; err: if (storage != NULL) { OPENSSL_cleanse(storage, storage_len_bytes); OPENSSL_free(storage); } #undef DAMS return ret; } static ossl_inline uint64_t get_digit(const uint8_t *in, int in_len) { uint64_t digit = 0; assert(in != NULL); assert(in_len <= 8); for (; in_len > 0; in_len--) { digit <<= 8; digit += (uint64_t)(in[in_len - 1]); } return digit; } static void to_words52(BN_ULONG *out, int out_len, const BN_ULONG *in, int in_bitsize) { uint8_t *in_str = NULL; assert(out != NULL); assert(in != NULL); assert(out_len >= number_of_digits(in_bitsize, DIGIT_SIZE)); in_str = (uint8_t *)in; for (; in_bitsize >= (2 * DIGIT_SIZE); in_bitsize -= (2 * DIGIT_SIZE), out += 2) { uint64_t digit; memcpy(&digit, in_str, sizeof(digit)); out[0] = digit & DIGIT_MASK; in_str += 6; memcpy(&digit, in_str, sizeof(digit)); out[1] = (digit >> 4) & DIGIT_MASK; in_str += 7; out_len -= 2; } if (in_bitsize > DIGIT_SIZE) { uint64_t digit = get_digit(in_str, 7); out[0] = digit & DIGIT_MASK; in_str += 6; in_bitsize -= DIGIT_SIZE; digit = get_digit(in_str, BITS2WORD8_SIZE(in_bitsize)); out[1] = digit >> 4; out += 2; out_len -= 2; } else if (in_bitsize > 0) { out[0] = get_digit(in_str, BITS2WORD8_SIZE(in_bitsize)); out++; out_len--; } while (out_len > 0) { *out = 0; out_len--; out++; } } static ossl_inline void put_digit(uint8_t *out, int out_len, uint64_t digit) { assert(out != NULL); assert(out_len <= 8); for (; out_len > 0; out_len--) { *out++ = (uint8_t)(digit & 0xFF); digit >>= 8; } } static void from_words52(BN_ULONG *out, int out_bitsize, const BN_ULONG *in) { int i; int out_len = BITS2WORD64_SIZE(out_bitsize); assert(out != NULL); assert(in != NULL); for (i = 0; i < out_len; i++) out[i] = 0; { uint8_t *out_str = (uint8_t *)out; for (; out_bitsize >= (2 * DIGIT_SIZE); out_bitsize -= (2 * DIGIT_SIZE), in += 2) { uint64_t digit; digit = in[0]; memcpy(out_str, &digit, sizeof(digit)); out_str += 6; digit = digit >> 48 | in[1] << 4; memcpy(out_str, &digit, sizeof(digit)); out_str += 7; } if (out_bitsize > DIGIT_SIZE) { put_digit(out_str, 7, in[0]); out_str += 6; out_bitsize -= DIGIT_SIZE; put_digit(out_str, BITS2WORD8_SIZE(out_bitsize), (in[1] << 4 | in[0] >> 48)); } else if (out_bitsize) { put_digit(out_str, BITS2WORD8_SIZE(out_bitsize), in[0]); } } } static ossl_inline void set_bit(BN_ULONG *a, int idx) { assert(a != NULL); { int i, j; i = idx / BN_BITS2; j = idx % BN_BITS2; a[i] |= (((BN_ULONG)1) << j); } } #endif
bn
openssl/crypto/bn/rsaz_exp_x2.c
openssl
#include <openssl/trace.h> #include "internal/cryptlib.h" #include "bn_local.h" #define BN_CTX_POOL_SIZE 16 #define BN_CTX_START_FRAMES 32 typedef struct bignum_pool_item { BIGNUM vals[BN_CTX_POOL_SIZE]; struct bignum_pool_item *prev, *next; } BN_POOL_ITEM; typedef struct bignum_pool { BN_POOL_ITEM *head, *current, *tail; unsigned used, size; } BN_POOL; static void BN_POOL_init(BN_POOL *); static void BN_POOL_finish(BN_POOL *); static BIGNUM *BN_POOL_get(BN_POOL *, int); static void BN_POOL_release(BN_POOL *, unsigned int); typedef struct bignum_ctx_stack { unsigned int *indexes; unsigned int depth, size; } BN_STACK; static void BN_STACK_init(BN_STACK *); static void BN_STACK_finish(BN_STACK *); static int BN_STACK_push(BN_STACK *, unsigned int); static unsigned int BN_STACK_pop(BN_STACK *); struct bignum_ctx { BN_POOL pool; BN_STACK stack; unsigned int used; int err_stack; int too_many; int flags; OSSL_LIB_CTX *libctx; }; #ifndef FIPS_MODULE static void ctxdbg(BIO *channel, const char *text, BN_CTX *ctx) { unsigned int bnidx = 0, fpidx = 0; BN_POOL_ITEM *item = ctx->pool.head; BN_STACK *stack = &ctx->stack; BIO_printf(channel, "%s\n", text); BIO_printf(channel, " (%16p): ", (void*)ctx); while (bnidx < ctx->used) { BIO_printf(channel, "%03x ", item->vals[bnidx++ % BN_CTX_POOL_SIZE].dmax); if (!(bnidx % BN_CTX_POOL_SIZE)) item = item->next; } BIO_printf(channel, "\n"); bnidx = 0; BIO_printf(channel, " %16s : ", ""); while (fpidx < stack->depth) { while (bnidx++ < stack->indexes[fpidx]) BIO_printf(channel, " "); BIO_printf(channel, "^^^ "); bnidx++; fpidx++; } BIO_printf(channel, "\n"); } # define CTXDBG(str, ctx) \ OSSL_TRACE_BEGIN(BN_CTX) { \ ctxdbg(trc_out, str, ctx); \ } OSSL_TRACE_END(BN_CTX) #else # define CTXDBG(str, ctx) do {} while(0) #endif BN_CTX *BN_CTX_new_ex(OSSL_LIB_CTX *ctx) { BN_CTX *ret; if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL) return NULL; BN_POOL_init(&ret->pool); BN_STACK_init(&ret->stack); ret->libctx = ctx; return ret; } #ifndef FIPS_MODULE BN_CTX *BN_CTX_new(void) { return BN_CTX_new_ex(NULL); } #endif BN_CTX *BN_CTX_secure_new_ex(OSSL_LIB_CTX *ctx) { BN_CTX *ret = BN_CTX_new_ex(ctx); if (ret != NULL) ret->flags = BN_FLG_SECURE; return ret; } #ifndef FIPS_MODULE BN_CTX *BN_CTX_secure_new(void) { return BN_CTX_secure_new_ex(NULL); } #endif void BN_CTX_free(BN_CTX *ctx) { if (ctx == NULL) return; #ifndef FIPS_MODULE OSSL_TRACE_BEGIN(BN_CTX) { BN_POOL_ITEM *pool = ctx->pool.head; BIO_printf(trc_out, "BN_CTX_free(): stack-size=%d, pool-bignums=%d\n", ctx->stack.size, ctx->pool.size); BIO_printf(trc_out, " dmaxs: "); while (pool) { unsigned loop = 0; while (loop < BN_CTX_POOL_SIZE) BIO_printf(trc_out, "%02x ", pool->vals[loop++].dmax); pool = pool->next; } BIO_printf(trc_out, "\n"); } OSSL_TRACE_END(BN_CTX); #endif BN_STACK_finish(&ctx->stack); BN_POOL_finish(&ctx->pool); OPENSSL_free(ctx); } void BN_CTX_start(BN_CTX *ctx) { CTXDBG("ENTER BN_CTX_start()", ctx); if (ctx->err_stack || ctx->too_many) ctx->err_stack++; else if (!BN_STACK_push(&ctx->stack, ctx->used)) { ERR_raise(ERR_LIB_BN, BN_R_TOO_MANY_TEMPORARY_VARIABLES); ctx->err_stack++; } CTXDBG("LEAVE BN_CTX_start()", ctx); } void BN_CTX_end(BN_CTX *ctx) { if (ctx == NULL) return; CTXDBG("ENTER BN_CTX_end()", ctx); if (ctx->err_stack) ctx->err_stack--; else { unsigned int fp = BN_STACK_pop(&ctx->stack); if (fp < ctx->used) BN_POOL_release(&ctx->pool, ctx->used - fp); ctx->used = fp; ctx->too_many = 0; } CTXDBG("LEAVE BN_CTX_end()", ctx); } BIGNUM *BN_CTX_get(BN_CTX *ctx) { BIGNUM *ret; CTXDBG("ENTER BN_CTX_get()", ctx); if (ctx->err_stack || ctx->too_many) return NULL; if ((ret = BN_POOL_get(&ctx->pool, ctx->flags)) == NULL) { ctx->too_many = 1; ERR_raise(ERR_LIB_BN, BN_R_TOO_MANY_TEMPORARY_VARIABLES); return NULL; } BN_zero(ret); ret->flags &= (~BN_FLG_CONSTTIME); ctx->used++; CTXDBG("LEAVE BN_CTX_get()", ctx); return ret; } OSSL_LIB_CTX *ossl_bn_get_libctx(BN_CTX *ctx) { if (ctx == NULL) return NULL; return ctx->libctx; } static void BN_STACK_init(BN_STACK *st) { st->indexes = NULL; st->depth = st->size = 0; } static void BN_STACK_finish(BN_STACK *st) { OPENSSL_free(st->indexes); st->indexes = NULL; } static int BN_STACK_push(BN_STACK *st, unsigned int idx) { if (st->depth == st->size) { unsigned int newsize = st->size ? (st->size * 3 / 2) : BN_CTX_START_FRAMES; unsigned int *newitems; if ((newitems = OPENSSL_malloc(sizeof(*newitems) * newsize)) == NULL) return 0; if (st->depth) memcpy(newitems, st->indexes, sizeof(*newitems) * st->depth); OPENSSL_free(st->indexes); st->indexes = newitems; st->size = newsize; } st->indexes[(st->depth)++] = idx; return 1; } static unsigned int BN_STACK_pop(BN_STACK *st) { return st->indexes[--(st->depth)]; } static void BN_POOL_init(BN_POOL *p) { p->head = p->current = p->tail = NULL; p->used = p->size = 0; } static void BN_POOL_finish(BN_POOL *p) { unsigned int loop; BIGNUM *bn; while (p->head) { for (loop = 0, bn = p->head->vals; loop++ < BN_CTX_POOL_SIZE; bn++) if (bn->d) BN_clear_free(bn); p->current = p->head->next; OPENSSL_free(p->head); p->head = p->current; } } static BIGNUM *BN_POOL_get(BN_POOL *p, int flag) { BIGNUM *bn; unsigned int loop; if (p->used == p->size) { BN_POOL_ITEM *item; if ((item = OPENSSL_malloc(sizeof(*item))) == NULL) return NULL; for (loop = 0, bn = item->vals; loop++ < BN_CTX_POOL_SIZE; bn++) { bn_init(bn); if ((flag & BN_FLG_SECURE) != 0) BN_set_flags(bn, BN_FLG_SECURE); } item->prev = p->tail; item->next = NULL; if (p->head == NULL) p->head = p->current = p->tail = item; else { p->tail->next = item; p->tail = item; p->current = item; } p->size += BN_CTX_POOL_SIZE; p->used++; return item->vals; } if (!p->used) p->current = p->head; else if ((p->used % BN_CTX_POOL_SIZE) == 0) p->current = p->current->next; return p->current->vals + ((p->used++) % BN_CTX_POOL_SIZE); } static void BN_POOL_release(BN_POOL *p, unsigned int num) { unsigned int offset = (p->used - 1) % BN_CTX_POOL_SIZE; p->used -= num; while (num--) { bn_check_top(p->current->vals + offset); if (offset == 0) { offset = BN_CTX_POOL_SIZE - 1; p->current = p->current->prev; } else offset--; } }
bn
openssl/crypto/bn/bn_ctx.c
openssl
#include "internal/cryptlib.h" #include "bn_local.h" BN_ULONG BN_mod_word(const BIGNUM *a, BN_ULONG w) { #ifndef BN_LLONG BN_ULONG ret = 0; #else BN_ULLONG ret = 0; #endif int i; if (w == 0) return (BN_ULONG)-1; #ifndef BN_LLONG if (w > ((BN_ULONG)1 << BN_BITS4)) { BIGNUM *tmp = BN_dup(a); if (tmp == NULL) return (BN_ULONG)-1; ret = BN_div_word(tmp, w); BN_free(tmp); return ret; } #endif bn_check_top(a); w &= BN_MASK2; for (i = a->top - 1; i >= 0; i--) { #ifndef BN_LLONG ret = ((ret << BN_BITS4) | ((a->d[i] >> BN_BITS4) & BN_MASK2l)) % w; ret = ((ret << BN_BITS4) | (a->d[i] & BN_MASK2l)) % w; #else ret = (BN_ULLONG) (((ret << (BN_ULLONG) BN_BITS2) | a->d[i]) % (BN_ULLONG) w); #endif } return (BN_ULONG)ret; } BN_ULONG BN_div_word(BIGNUM *a, BN_ULONG w) { BN_ULONG ret = 0; int i, j; bn_check_top(a); w &= BN_MASK2; if (!w) return (BN_ULONG)-1; if (a->top == 0) return 0; j = BN_BITS2 - BN_num_bits_word(w); w <<= j; if (!BN_lshift(a, a, j)) return (BN_ULONG)-1; for (i = a->top - 1; i >= 0; i--) { BN_ULONG l, d; l = a->d[i]; d = bn_div_words(ret, l, w); ret = (l - ((d * w) & BN_MASK2)) & BN_MASK2; a->d[i] = d; } if ((a->top > 0) && (a->d[a->top - 1] == 0)) a->top--; ret >>= j; if (!a->top) a->neg = 0; bn_check_top(a); return ret; } int BN_add_word(BIGNUM *a, BN_ULONG w) { BN_ULONG l; int i; bn_check_top(a); w &= BN_MASK2; if (!w) return 1; if (BN_is_zero(a)) return BN_set_word(a, w); if (a->neg) { a->neg = 0; i = BN_sub_word(a, w); if (!BN_is_zero(a)) a->neg = !(a->neg); return i; } for (i = 0; w != 0 && i < a->top; i++) { a->d[i] = l = (a->d[i] + w) & BN_MASK2; w = (w > l) ? 1 : 0; } if (w && i == a->top) { if (bn_wexpand(a, a->top + 1) == NULL) return 0; a->top++; a->d[i] = w; } bn_check_top(a); return 1; } int BN_sub_word(BIGNUM *a, BN_ULONG w) { int i; bn_check_top(a); w &= BN_MASK2; if (!w) return 1; if (BN_is_zero(a)) { i = BN_set_word(a, w); if (i != 0) BN_set_negative(a, 1); return i; } if (a->neg) { a->neg = 0; i = BN_add_word(a, w); a->neg = 1; return i; } if ((a->top == 1) && (a->d[0] < w)) { a->d[0] = w - a->d[0]; a->neg = 1; return 1; } i = 0; for (;;) { if (a->d[i] >= w) { a->d[i] -= w; break; } else { a->d[i] = (a->d[i] - w) & BN_MASK2; i++; w = 1; } } if ((a->d[i] == 0) && (i == (a->top - 1))) a->top--; bn_check_top(a); return 1; } int BN_mul_word(BIGNUM *a, BN_ULONG w) { BN_ULONG ll; bn_check_top(a); w &= BN_MASK2; if (a->top) { if (w == 0) BN_zero(a); else { ll = bn_mul_words(a->d, a->d, a->top, w); if (ll) { if (bn_wexpand(a, a->top + 1) == NULL) return 0; a->d[a->top++] = ll; } } } bn_check_top(a); return 1; }
bn
openssl/crypto/bn/bn_word.c
openssl
#include <stdio.h> #include <time.h> #include "internal/cryptlib.h" #include "crypto/rand.h" #include "bn_local.h" #include <openssl/rand.h> #include <openssl/sha.h> #include <openssl/evp.h> typedef enum bnrand_flag_e { NORMAL, TESTING, PRIVATE } BNRAND_FLAG; static int bnrand(BNRAND_FLAG flag, BIGNUM *rnd, int bits, int top, int bottom, unsigned int strength, BN_CTX *ctx) { unsigned char *buf = NULL; int b, ret = 0, bit, bytes, mask; OSSL_LIB_CTX *libctx = ossl_bn_get_libctx(ctx); if (bits == 0) { if (top != BN_RAND_TOP_ANY || bottom != BN_RAND_BOTTOM_ANY) goto toosmall; BN_zero(rnd); return 1; } if (bits < 0 || (bits == 1 && top > 0)) goto toosmall; bytes = (bits + 7) / 8; bit = (bits - 1) % 8; mask = 0xff << (bit + 1); buf = OPENSSL_malloc(bytes); if (buf == NULL) goto err; b = flag == NORMAL ? RAND_bytes_ex(libctx, buf, bytes, strength) : RAND_priv_bytes_ex(libctx, buf, bytes, strength); if (b <= 0) goto err; if (flag == TESTING) { int i; unsigned char c; for (i = 0; i < bytes; i++) { if (RAND_bytes_ex(libctx, &c, 1, strength) <= 0) goto err; if (c >= 128 && i > 0) buf[i] = buf[i - 1]; else if (c < 42) buf[i] = 0; else if (c < 84) buf[i] = 255; } } if (top >= 0) { if (top) { if (bit == 0) { buf[0] = 1; buf[1] |= 0x80; } else { buf[0] |= (3 << (bit - 1)); } } else { buf[0] |= (1 << bit); } } buf[0] &= ~mask; if (bottom) buf[bytes - 1] |= 1; if (!BN_bin2bn(buf, bytes, rnd)) goto err; ret = 1; err: OPENSSL_clear_free(buf, bytes); bn_check_top(rnd); return ret; toosmall: ERR_raise(ERR_LIB_BN, BN_R_BITS_TOO_SMALL); return 0; } int BN_rand_ex(BIGNUM *rnd, int bits, int top, int bottom, unsigned int strength, BN_CTX *ctx) { return bnrand(NORMAL, rnd, bits, top, bottom, strength, ctx); } #ifndef FIPS_MODULE int BN_rand(BIGNUM *rnd, int bits, int top, int bottom) { return bnrand(NORMAL, rnd, bits, top, bottom, 0, NULL); } int BN_bntest_rand(BIGNUM *rnd, int bits, int top, int bottom) { return bnrand(TESTING, rnd, bits, top, bottom, 0, NULL); } #endif int BN_priv_rand_ex(BIGNUM *rnd, int bits, int top, int bottom, unsigned int strength, BN_CTX *ctx) { return bnrand(PRIVATE, rnd, bits, top, bottom, strength, ctx); } #ifndef FIPS_MODULE int BN_priv_rand(BIGNUM *rnd, int bits, int top, int bottom) { return bnrand(PRIVATE, rnd, bits, top, bottom, 0, NULL); } #endif static int bnrand_range(BNRAND_FLAG flag, BIGNUM *r, const BIGNUM *range, unsigned int strength, BN_CTX *ctx) { int n; int count = 100; if (r == NULL) { ERR_raise(ERR_LIB_BN, ERR_R_PASSED_NULL_PARAMETER); return 0; } if (range->neg || BN_is_zero(range)) { ERR_raise(ERR_LIB_BN, BN_R_INVALID_RANGE); return 0; } n = BN_num_bits(range); if (n == 1) BN_zero(r); else if (!BN_is_bit_set(range, n - 2) && !BN_is_bit_set(range, n - 3)) { do { if (!bnrand(flag, r, n + 1, BN_RAND_TOP_ANY, BN_RAND_BOTTOM_ANY, strength, ctx)) return 0; if (BN_cmp(r, range) >= 0) { if (!BN_sub(r, r, range)) return 0; if (BN_cmp(r, range) >= 0) if (!BN_sub(r, r, range)) return 0; } if (!--count) { ERR_raise(ERR_LIB_BN, BN_R_TOO_MANY_ITERATIONS); return 0; } } while (BN_cmp(r, range) >= 0); } else { do { if (!bnrand(flag, r, n, BN_RAND_TOP_ANY, BN_RAND_BOTTOM_ANY, 0, ctx)) return 0; if (!--count) { ERR_raise(ERR_LIB_BN, BN_R_TOO_MANY_ITERATIONS); return 0; } } while (BN_cmp(r, range) >= 0); } bn_check_top(r); return 1; } int BN_rand_range_ex(BIGNUM *r, const BIGNUM *range, unsigned int strength, BN_CTX *ctx) { return bnrand_range(NORMAL, r, range, strength, ctx); } #ifndef FIPS_MODULE int BN_rand_range(BIGNUM *r, const BIGNUM *range) { return bnrand_range(NORMAL, r, range, 0, NULL); } #endif int BN_priv_rand_range_ex(BIGNUM *r, const BIGNUM *range, unsigned int strength, BN_CTX *ctx) { return bnrand_range(PRIVATE, r, range, strength, ctx); } #ifndef FIPS_MODULE int BN_priv_rand_range(BIGNUM *r, const BIGNUM *range) { return bnrand_range(PRIVATE, r, range, 0, NULL); } # ifndef OPENSSL_NO_DEPRECATED_3_0 int BN_pseudo_rand(BIGNUM *rnd, int bits, int top, int bottom) { return BN_rand(rnd, bits, top, bottom); } int BN_pseudo_rand_range(BIGNUM *r, const BIGNUM *range) { return BN_rand_range(r, range); } # endif #endif int BN_generate_dsa_nonce(BIGNUM *out, const BIGNUM *range, const BIGNUM *priv, const unsigned char *message, size_t message_len, BN_CTX *ctx) { EVP_MD_CTX *mdctx = EVP_MD_CTX_new(); unsigned char random_bytes[64]; unsigned char digest[SHA512_DIGEST_LENGTH]; unsigned done, todo; const unsigned num_k_bytes = BN_num_bytes(range) + 8; unsigned char private_bytes[96]; unsigned char *k_bytes = NULL; int ret = 0; EVP_MD *md = NULL; OSSL_LIB_CTX *libctx = ossl_bn_get_libctx(ctx); if (mdctx == NULL) goto err; k_bytes = OPENSSL_malloc(num_k_bytes); if (k_bytes == NULL) goto err; if (BN_bn2binpad(priv, private_bytes, sizeof(private_bytes)) < 0) { ERR_raise(ERR_LIB_BN, BN_R_PRIVATE_KEY_TOO_LARGE); goto err; } md = EVP_MD_fetch(libctx, "SHA512", NULL); if (md == NULL) { ERR_raise(ERR_LIB_BN, BN_R_NO_SUITABLE_DIGEST); goto err; } for (done = 0; done < num_k_bytes;) { if (RAND_priv_bytes_ex(libctx, random_bytes, sizeof(random_bytes), 0) <= 0) goto err; if (!EVP_DigestInit_ex(mdctx, md, NULL) || !EVP_DigestUpdate(mdctx, &done, sizeof(done)) || !EVP_DigestUpdate(mdctx, private_bytes, sizeof(private_bytes)) || !EVP_DigestUpdate(mdctx, message, message_len) || !EVP_DigestUpdate(mdctx, random_bytes, sizeof(random_bytes)) || !EVP_DigestFinal_ex(mdctx, digest, NULL)) goto err; todo = num_k_bytes - done; if (todo > SHA512_DIGEST_LENGTH) todo = SHA512_DIGEST_LENGTH; memcpy(k_bytes + done, digest, todo); done += todo; } if (!BN_bin2bn(k_bytes, num_k_bytes, out)) goto err; if (BN_mod(out, out, range, ctx) != 1) goto err; ret = 1; err: EVP_MD_CTX_free(mdctx); EVP_MD_free(md); OPENSSL_clear_free(k_bytes, num_k_bytes); OPENSSL_cleanse(digest, sizeof(digest)); OPENSSL_cleanse(random_bytes, sizeof(random_bytes)); OPENSSL_cleanse(private_bytes, sizeof(private_bytes)); return ret; }
bn
openssl/crypto/bn/bn_rand.c
openssl
#include "internal/cryptlib.h" #include "bn_local.h" static ossl_inline BIGNUM *bn_mod_inverse_no_branch(BIGNUM *in, const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx, int *pnoinv) { BIGNUM *A, *B, *X, *Y, *M, *D, *T, *R = NULL; BIGNUM *ret = NULL; int sign; bn_check_top(a); bn_check_top(n); BN_CTX_start(ctx); A = BN_CTX_get(ctx); B = BN_CTX_get(ctx); X = BN_CTX_get(ctx); D = BN_CTX_get(ctx); M = BN_CTX_get(ctx); Y = BN_CTX_get(ctx); T = BN_CTX_get(ctx); if (T == NULL) goto err; if (in == NULL) R = BN_new(); else R = in; if (R == NULL) goto err; if (!BN_one(X)) goto err; BN_zero(Y); if (BN_copy(B, a) == NULL) goto err; if (BN_copy(A, n) == NULL) goto err; A->neg = 0; if (B->neg || (BN_ucmp(B, A) >= 0)) { { BIGNUM local_B; bn_init(&local_B); BN_with_flags(&local_B, B, BN_FLG_CONSTTIME); if (!BN_nnmod(B, &local_B, A, ctx)) goto err; } } sign = -1; while (!BN_is_zero(B)) { BIGNUM *tmp; { BIGNUM local_A; bn_init(&local_A); BN_with_flags(&local_A, A, BN_FLG_CONSTTIME); if (!BN_div(D, M, &local_A, B, ctx)) goto err; } tmp = A; A = B; B = M; if (!BN_mul(tmp, D, X, ctx)) goto err; if (!BN_add(tmp, tmp, Y)) goto err; M = Y; Y = X; X = tmp; sign = -sign; } if (sign < 0) { if (!BN_sub(Y, n, Y)) goto err; } if (BN_is_one(A)) { if (!Y->neg && BN_ucmp(Y, n) < 0) { if (!BN_copy(R, Y)) goto err; } else { if (!BN_nnmod(R, Y, n, ctx)) goto err; } } else { *pnoinv = 1; goto err; } ret = R; *pnoinv = 0; err: if ((ret == NULL) && (in == NULL)) BN_free(R); BN_CTX_end(ctx); bn_check_top(ret); return ret; } BIGNUM *int_bn_mod_inverse(BIGNUM *in, const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx, int *pnoinv) { BIGNUM *A, *B, *X, *Y, *M, *D, *T, *R = NULL; BIGNUM *ret = NULL; int sign; if (BN_abs_is_word(n, 1) || BN_is_zero(n)) { *pnoinv = 1; return NULL; } *pnoinv = 0; if ((BN_get_flags(a, BN_FLG_CONSTTIME) != 0) || (BN_get_flags(n, BN_FLG_CONSTTIME) != 0)) { return bn_mod_inverse_no_branch(in, a, n, ctx, pnoinv); } bn_check_top(a); bn_check_top(n); BN_CTX_start(ctx); A = BN_CTX_get(ctx); B = BN_CTX_get(ctx); X = BN_CTX_get(ctx); D = BN_CTX_get(ctx); M = BN_CTX_get(ctx); Y = BN_CTX_get(ctx); T = BN_CTX_get(ctx); if (T == NULL) goto err; if (in == NULL) R = BN_new(); else R = in; if (R == NULL) goto err; if (!BN_one(X)) goto err; BN_zero(Y); if (BN_copy(B, a) == NULL) goto err; if (BN_copy(A, n) == NULL) goto err; A->neg = 0; if (B->neg || (BN_ucmp(B, A) >= 0)) { if (!BN_nnmod(B, B, A, ctx)) goto err; } sign = -1; if (BN_is_odd(n) && (BN_num_bits(n) <= 2048)) { int shift; while (!BN_is_zero(B)) { shift = 0; while (!BN_is_bit_set(B, shift)) { shift++; if (BN_is_odd(X)) { if (!BN_uadd(X, X, n)) goto err; } if (!BN_rshift1(X, X)) goto err; } if (shift > 0) { if (!BN_rshift(B, B, shift)) goto err; } shift = 0; while (!BN_is_bit_set(A, shift)) { shift++; if (BN_is_odd(Y)) { if (!BN_uadd(Y, Y, n)) goto err; } if (!BN_rshift1(Y, Y)) goto err; } if (shift > 0) { if (!BN_rshift(A, A, shift)) goto err; } if (BN_ucmp(B, A) >= 0) { if (!BN_uadd(X, X, Y)) goto err; if (!BN_usub(B, B, A)) goto err; } else { if (!BN_uadd(Y, Y, X)) goto err; if (!BN_usub(A, A, B)) goto err; } } } else { while (!BN_is_zero(B)) { BIGNUM *tmp; if (BN_num_bits(A) == BN_num_bits(B)) { if (!BN_one(D)) goto err; if (!BN_sub(M, A, B)) goto err; } else if (BN_num_bits(A) == BN_num_bits(B) + 1) { if (!BN_lshift1(T, B)) goto err; if (BN_ucmp(A, T) < 0) { if (!BN_one(D)) goto err; if (!BN_sub(M, A, B)) goto err; } else { if (!BN_sub(M, A, T)) goto err; if (!BN_add(D, T, B)) goto err; if (BN_ucmp(A, D) < 0) { if (!BN_set_word(D, 2)) goto err; } else { if (!BN_set_word(D, 3)) goto err; if (!BN_sub(M, M, B)) goto err; } } } else { if (!BN_div(D, M, A, B, ctx)) goto err; } tmp = A; A = B; B = M; if (BN_is_one(D)) { if (!BN_add(tmp, X, Y)) goto err; } else { if (BN_is_word(D, 2)) { if (!BN_lshift1(tmp, X)) goto err; } else if (BN_is_word(D, 4)) { if (!BN_lshift(tmp, X, 2)) goto err; } else if (D->top == 1) { if (!BN_copy(tmp, X)) goto err; if (!BN_mul_word(tmp, D->d[0])) goto err; } else { if (!BN_mul(tmp, D, X, ctx)) goto err; } if (!BN_add(tmp, tmp, Y)) goto err; } M = Y; Y = X; X = tmp; sign = -sign; } } if (sign < 0) { if (!BN_sub(Y, n, Y)) goto err; } if (BN_is_one(A)) { if (!Y->neg && BN_ucmp(Y, n) < 0) { if (!BN_copy(R, Y)) goto err; } else { if (!BN_nnmod(R, Y, n, ctx)) goto err; } } else { *pnoinv = 1; goto err; } ret = R; err: if ((ret == NULL) && (in == NULL)) BN_free(R); BN_CTX_end(ctx); bn_check_top(ret); return ret; } BIGNUM *BN_mod_inverse(BIGNUM *in, const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx) { BN_CTX *new_ctx = NULL; BIGNUM *rv; int noinv = 0; if (ctx == NULL) { ctx = new_ctx = BN_CTX_new_ex(NULL); if (ctx == NULL) { ERR_raise(ERR_LIB_BN, ERR_R_BN_LIB); return NULL; } } rv = int_bn_mod_inverse(in, a, n, ctx, &noinv); if (noinv) ERR_raise(ERR_LIB_BN, BN_R_NO_INVERSE); BN_CTX_free(new_ctx); return rv; } int BN_are_coprime(BIGNUM *a, const BIGNUM *b, BN_CTX *ctx) { int ret = 0; BIGNUM *tmp; BN_CTX_start(ctx); tmp = BN_CTX_get(ctx); if (tmp == NULL) goto end; ERR_set_mark(); BN_set_flags(a, BN_FLG_CONSTTIME); ret = (BN_mod_inverse(tmp, a, b, ctx) != NULL); ERR_pop_to_mark(); end: BN_CTX_end(ctx); return ret; } int BN_gcd(BIGNUM *r, const BIGNUM *in_a, const BIGNUM *in_b, BN_CTX *ctx) { BIGNUM *g, *temp = NULL; BN_ULONG mask = 0; int i, j, top, rlen, glen, m, bit = 1, delta = 1, cond = 0, shifts = 0, ret = 0; if (BN_is_zero(in_b)) { ret = BN_copy(r, in_a) != NULL; r->neg = 0; return ret; } if (BN_is_zero(in_a)) { ret = BN_copy(r, in_b) != NULL; r->neg = 0; return ret; } bn_check_top(in_a); bn_check_top(in_b); BN_CTX_start(ctx); temp = BN_CTX_get(ctx); g = BN_CTX_get(ctx); if (g == NULL || !BN_lshift1(g, in_b) || !BN_lshift1(r, in_a)) goto err; for (i = 0; i < r->dmax && i < g->dmax; i++) { mask = ~(r->d[i] | g->d[i]); for (j = 0; j < BN_BITS2; j++) { bit &= mask; shifts += bit; mask >>= 1; } } if (!BN_rshift(r, r, shifts) || !BN_rshift(g, g, shifts)) goto err; top = 1 + ((r->top >= g->top) ? r->top : g->top); if (bn_wexpand(r, top) == NULL || bn_wexpand(g, top) == NULL || bn_wexpand(temp, top) == NULL) goto err; BN_consttime_swap((~r->d[0]) & 1, r, g, top); rlen = BN_num_bits(r); glen = BN_num_bits(g); m = 4 + 3 * ((rlen >= glen) ? rlen : glen); for (i = 0; i < m; i++) { cond = ((unsigned int)-delta >> (8 * sizeof(delta) - 1)) & g->d[0] & 1 & (~((unsigned int)(g->top - 1) >> (sizeof(g->top) * 8 - 1))); delta = (-cond & -delta) | ((cond - 1) & delta); r->neg ^= cond; BN_consttime_swap(cond, r, g, top); delta++; if (!BN_add(temp, g, r)) goto err; BN_consttime_swap(g->d[0] & 1 & (~((unsigned int)(g->top - 1) >> (sizeof(g->top) * 8 - 1))), g, temp, top); if (!BN_rshift1(g, g)) goto err; } r->neg = 0; if (!BN_lshift(r, r, shifts) || !BN_rshift1(r, r)) goto err; ret = 1; err: BN_CTX_end(ctx); bn_check_top(r); return ret; }
bn
openssl/crypto/bn/bn_gcd.c
openssl
#include "bn_local.h" #include "internal/nelem.h" # include <openssl/dh.h> # include "crypto/bn_dh.h" # if BN_BITS2 == 64 # define BN_DEF(lo, hi) (BN_ULONG)hi << 32 | lo # else # define BN_DEF(lo, hi) lo, hi # endif # ifndef FIPS_MODULE static const BN_ULONG modp_1536_p[] = { BN_DEF(0xFFFFFFFF, 0xFFFFFFFF), BN_DEF(0xCA237327, 0xF1746C08), BN_DEF(0x4ABC9804, 0x670C354E), BN_DEF(0x7096966D, 0x9ED52907), BN_DEF(0x208552BB, 0x1C62F356), BN_DEF(0xDCA3AD96, 0x83655D23), BN_DEF(0xFD24CF5F, 0x69163FA8), BN_DEF(0x1C55D39A, 0x98DA4836), BN_DEF(0xA163BF05, 0xC2007CB8), BN_DEF(0xECE45B3D, 0x49286651), BN_DEF(0x7C4B1FE6, 0xAE9F2411), BN_DEF(0x5A899FA5, 0xEE386BFB), BN_DEF(0xF406B7ED, 0x0BFF5CB6), BN_DEF(0xA637ED6B, 0xF44C42E9), BN_DEF(0x625E7EC6, 0xE485B576), BN_DEF(0x6D51C245, 0x4FE1356D), BN_DEF(0xF25F1437, 0x302B0A6D), BN_DEF(0xCD3A431B, 0xEF9519B3), BN_DEF(0x8E3404DD, 0x514A0879), BN_DEF(0x3B139B22, 0x020BBEA6), BN_DEF(0x8A67CC74, 0x29024E08), BN_DEF(0x80DC1CD1, 0xC4C6628B), BN_DEF(0x2168C234, 0xC90FDAA2), BN_DEF(0xFFFFFFFF, 0xFFFFFFFF) }; static const BN_ULONG modp_1536_q[] = { BN_DEF(0xFFFFFFFF, 0xFFFFFFFF), BN_DEF(0x6511B993, 0x78BA3604), BN_DEF(0x255E4C02, 0xB3861AA7), BN_DEF(0xB84B4B36, 0xCF6A9483), BN_DEF(0x1042A95D, 0x0E3179AB), BN_DEF(0xEE51D6CB, 0xC1B2AE91), BN_DEF(0x7E9267AF, 0x348B1FD4), BN_DEF(0x0E2AE9CD, 0xCC6D241B), BN_DEF(0x50B1DF82, 0xE1003E5C), BN_DEF(0xF6722D9E, 0x24943328), BN_DEF(0xBE258FF3, 0xD74F9208), BN_DEF(0xAD44CFD2, 0xF71C35FD), BN_DEF(0x7A035BF6, 0x85FFAE5B), BN_DEF(0xD31BF6B5, 0x7A262174), BN_DEF(0x312F3F63, 0xF242DABB), BN_DEF(0xB6A8E122, 0xA7F09AB6), BN_DEF(0xF92F8A1B, 0x98158536), BN_DEF(0xE69D218D, 0xF7CA8CD9), BN_DEF(0xC71A026E, 0x28A5043C), BN_DEF(0x1D89CD91, 0x0105DF53), BN_DEF(0x4533E63A, 0x94812704), BN_DEF(0xC06E0E68, 0x62633145), BN_DEF(0x10B4611A, 0xE487ED51), BN_DEF(0xFFFFFFFF, 0x7FFFFFFF) }; # endif static const BN_ULONG modp_2048_p[] = { BN_DEF(0xFFFFFFFF, 0xFFFFFFFF), BN_DEF(0x8AACAA68, 0x15728E5A), BN_DEF(0x98FA0510, 0x15D22618), BN_DEF(0xEA956AE5, 0x3995497C), BN_DEF(0x95581718, 0xDE2BCBF6), BN_DEF(0x6F4C52C9, 0xB5C55DF0), BN_DEF(0xEC07A28F, 0x9B2783A2), BN_DEF(0x180E8603, 0xE39E772C), BN_DEF(0x2E36CE3B, 0x32905E46), BN_DEF(0xCA18217C, 0xF1746C08), BN_DEF(0x4ABC9804, 0x670C354E), BN_DEF(0x7096966D, 0x9ED52907), BN_DEF(0x208552BB, 0x1C62F356), BN_DEF(0xDCA3AD96, 0x83655D23), BN_DEF(0xFD24CF5F, 0x69163FA8), BN_DEF(0x1C55D39A, 0x98DA4836), BN_DEF(0xA163BF05, 0xC2007CB8), BN_DEF(0xECE45B3D, 0x49286651), BN_DEF(0x7C4B1FE6, 0xAE9F2411), BN_DEF(0x5A899FA5, 0xEE386BFB), BN_DEF(0xF406B7ED, 0x0BFF5CB6), BN_DEF(0xA637ED6B, 0xF44C42E9), BN_DEF(0x625E7EC6, 0xE485B576), BN_DEF(0x6D51C245, 0x4FE1356D), BN_DEF(0xF25F1437, 0x302B0A6D), BN_DEF(0xCD3A431B, 0xEF9519B3), BN_DEF(0x8E3404DD, 0x514A0879), BN_DEF(0x3B139B22, 0x020BBEA6), BN_DEF(0x8A67CC74, 0x29024E08), BN_DEF(0x80DC1CD1, 0xC4C6628B), BN_DEF(0x2168C234, 0xC90FDAA2), BN_DEF(0xFFFFFFFF, 0xFFFFFFFF) }; static const BN_ULONG modp_2048_q[] = { BN_DEF(0xFFFFFFFF, 0x7FFFFFFF), BN_DEF(0x45565534, 0x0AB9472D), BN_DEF(0x4C7D0288, 0x8AE9130C), BN_DEF(0x754AB572, 0x1CCAA4BE), BN_DEF(0x4AAC0B8C, 0xEF15E5FB), BN_DEF(0x37A62964, 0xDAE2AEF8), BN_DEF(0x7603D147, 0xCD93C1D1), BN_DEF(0x0C074301, 0xF1CF3B96), BN_DEF(0x171B671D, 0x19482F23), BN_DEF(0x650C10BE, 0x78BA3604), BN_DEF(0x255E4C02, 0xB3861AA7), BN_DEF(0xB84B4B36, 0xCF6A9483), BN_DEF(0x1042A95D, 0x0E3179AB), BN_DEF(0xEE51D6CB, 0xC1B2AE91), BN_DEF(0x7E9267AF, 0x348B1FD4), BN_DEF(0x0E2AE9CD, 0xCC6D241B), BN_DEF(0x50B1DF82, 0xE1003E5C), BN_DEF(0xF6722D9E, 0x24943328), BN_DEF(0xBE258FF3, 0xD74F9208), BN_DEF(0xAD44CFD2, 0xF71C35FD), BN_DEF(0x7A035BF6, 0x85FFAE5B), BN_DEF(0xD31BF6B5, 0x7A262174), BN_DEF(0x312F3F63, 0xF242DABB), BN_DEF(0xB6A8E122, 0xA7F09AB6), BN_DEF(0xF92F8A1B, 0x98158536), BN_DEF(0xE69D218D, 0xF7CA8CD9), BN_DEF(0xC71A026E, 0x28A5043C), BN_DEF(0x1D89CD91, 0x0105DF53), BN_DEF(0x4533E63A, 0x94812704), BN_DEF(0xC06E0E68, 0x62633145), BN_DEF(0x10B4611A, 0xE487ED51), BN_DEF(0xFFFFFFFF, 0x7FFFFFFF), }; static const BN_ULONG modp_3072_p[] = { BN_DEF(0xFFFFFFFF, 0xFFFFFFFF), BN_DEF(0xA93AD2CA, 0x4B82D120), BN_DEF(0xE0FD108E, 0x43DB5BFC), BN_DEF(0x74E5AB31, 0x08E24FA0), BN_DEF(0xBAD946E2, 0x770988C0), BN_DEF(0x7A615D6C, 0xBBE11757), BN_DEF(0x177B200C, 0x521F2B18), BN_DEF(0x3EC86A64, 0xD8760273), BN_DEF(0xD98A0864, 0xF12FFA06), BN_DEF(0x1AD2EE6B, 0xCEE3D226), BN_DEF(0x4A25619D, 0x1E8C94E0), BN_DEF(0xDB0933D7, 0xABF5AE8C), BN_DEF(0xA6E1E4C7, 0xB3970F85), BN_DEF(0x5D060C7D, 0x8AEA7157), BN_DEF(0x58DBEF0A, 0xECFB8504), BN_DEF(0xDF1CBA64, 0xA85521AB), BN_DEF(0x04507A33, 0xAD33170D), BN_DEF(0x8AAAC42D, 0x15728E5A), BN_DEF(0x98FA0510, 0x15D22618), BN_DEF(0xEA956AE5, 0x3995497C), BN_DEF(0x95581718, 0xDE2BCBF6), BN_DEF(0x6F4C52C9, 0xB5C55DF0), BN_DEF(0xEC07A28F, 0x9B2783A2), BN_DEF(0x180E8603, 0xE39E772C), BN_DEF(0x2E36CE3B, 0x32905E46), BN_DEF(0xCA18217C, 0xF1746C08), BN_DEF(0x4ABC9804, 0x670C354E), BN_DEF(0x7096966D, 0x9ED52907), BN_DEF(0x208552BB, 0x1C62F356), BN_DEF(0xDCA3AD96, 0x83655D23), BN_DEF(0xFD24CF5F, 0x69163FA8), BN_DEF(0x1C55D39A, 0x98DA4836), BN_DEF(0xA163BF05, 0xC2007CB8), BN_DEF(0xECE45B3D, 0x49286651), BN_DEF(0x7C4B1FE6, 0xAE9F2411), BN_DEF(0x5A899FA5, 0xEE386BFB), BN_DEF(0xF406B7ED, 0x0BFF5CB6), BN_DEF(0xA637ED6B, 0xF44C42E9), BN_DEF(0x625E7EC6, 0xE485B576), BN_DEF(0x6D51C245, 0x4FE1356D), BN_DEF(0xF25F1437, 0x302B0A6D), BN_DEF(0xCD3A431B, 0xEF9519B3), BN_DEF(0x8E3404DD, 0x514A0879), BN_DEF(0x3B139B22, 0x020BBEA6), BN_DEF(0x8A67CC74, 0x29024E08), BN_DEF(0x80DC1CD1, 0xC4C6628B), BN_DEF(0x2168C234, 0xC90FDAA2), BN_DEF(0xFFFFFFFF, 0xFFFFFFFF) }; static const BN_ULONG modp_3072_q[] = { BN_DEF(0xFFFFFFFF, 0x7FFFFFFF), BN_DEF(0x549D6965, 0x25C16890), BN_DEF(0x707E8847, 0xA1EDADFE), BN_DEF(0x3A72D598, 0x047127D0), BN_DEF(0x5D6CA371, 0x3B84C460), BN_DEF(0xBD30AEB6, 0x5DF08BAB), BN_DEF(0x0BBD9006, 0x290F958C), BN_DEF(0x9F643532, 0x6C3B0139), BN_DEF(0x6CC50432, 0xF897FD03), BN_DEF(0x0D697735, 0xE771E913), BN_DEF(0x2512B0CE, 0x8F464A70), BN_DEF(0x6D8499EB, 0xD5FAD746), BN_DEF(0xD370F263, 0xD9CB87C2), BN_DEF(0xAE83063E, 0x457538AB), BN_DEF(0x2C6DF785, 0x767DC282), BN_DEF(0xEF8E5D32, 0xD42A90D5), BN_DEF(0x82283D19, 0xD6998B86), BN_DEF(0x45556216, 0x0AB9472D), BN_DEF(0x4C7D0288, 0x8AE9130C), BN_DEF(0x754AB572, 0x1CCAA4BE), BN_DEF(0x4AAC0B8C, 0xEF15E5FB), BN_DEF(0x37A62964, 0xDAE2AEF8), BN_DEF(0x7603D147, 0xCD93C1D1), BN_DEF(0x0C074301, 0xF1CF3B96), BN_DEF(0x171B671D, 0x19482F23), BN_DEF(0x650C10BE, 0x78BA3604), BN_DEF(0x255E4C02, 0xB3861AA7), BN_DEF(0xB84B4B36, 0xCF6A9483), BN_DEF(0x1042A95D, 0x0E3179AB), BN_DEF(0xEE51D6CB, 0xC1B2AE91), BN_DEF(0x7E9267AF, 0x348B1FD4), BN_DEF(0x0E2AE9CD, 0xCC6D241B), BN_DEF(0x50B1DF82, 0xE1003E5C), BN_DEF(0xF6722D9E, 0x24943328), BN_DEF(0xBE258FF3, 0xD74F9208), BN_DEF(0xAD44CFD2, 0xF71C35FD), BN_DEF(0x7A035BF6, 0x85FFAE5B), BN_DEF(0xD31BF6B5, 0x7A262174), BN_DEF(0x312F3F63, 0xF242DABB), BN_DEF(0xB6A8E122, 0xA7F09AB6), BN_DEF(0xF92F8A1B, 0x98158536), BN_DEF(0xE69D218D, 0xF7CA8CD9), BN_DEF(0xC71A026E, 0x28A5043C), BN_DEF(0x1D89CD91, 0x0105DF53), BN_DEF(0x4533E63A, 0x94812704), BN_DEF(0xC06E0E68, 0x62633145), BN_DEF(0x10B4611A, 0xE487ED51), BN_DEF(0xFFFFFFFF, 0x7FFFFFFF), }; static const BN_ULONG modp_4096_p[] = { BN_DEF(0xFFFFFFFF, 0xFFFFFFFF), BN_DEF(0x34063199, 0x4DF435C9), BN_DEF(0x90A6C08F, 0x86FFB7DC), BN_DEF(0x8D8FDDC1, 0x93B4EA98), BN_DEF(0xD5B05AA9, 0xD0069127), BN_DEF(0x2170481C, 0xB81BDD76), BN_DEF(0xCEE2D7AF, 0x1F612970), BN_DEF(0x515BE7ED, 0x233BA186), BN_DEF(0xA090C3A2, 0x99B2964F), BN_DEF(0x4E6BC05D, 0x287C5947), BN_DEF(0x1FBECAA6, 0x2E8EFC14), BN_DEF(0x04DE8EF9, 0xDBBBC2DB), BN_DEF(0x2AD44CE8, 0x2583E9CA), BN_DEF(0xB6150BDA, 0x1A946834), BN_DEF(0x6AF4E23C, 0x99C32718), BN_DEF(0xBDBA5B26, 0x88719A10), BN_DEF(0xA787E6D7, 0x1A723C12), BN_DEF(0xA9210801, 0x4B82D120), BN_DEF(0xE0FD108E, 0x43DB5BFC), BN_DEF(0x74E5AB31, 0x08E24FA0), BN_DEF(0xBAD946E2, 0x770988C0), BN_DEF(0x7A615D6C, 0xBBE11757), BN_DEF(0x177B200C, 0x521F2B18), BN_DEF(0x3EC86A64, 0xD8760273), BN_DEF(0xD98A0864, 0xF12FFA06), BN_DEF(0x1AD2EE6B, 0xCEE3D226), BN_DEF(0x4A25619D, 0x1E8C94E0), BN_DEF(0xDB0933D7, 0xABF5AE8C), BN_DEF(0xA6E1E4C7, 0xB3970F85), BN_DEF(0x5D060C7D, 0x8AEA7157), BN_DEF(0x58DBEF0A, 0xECFB8504), BN_DEF(0xDF1CBA64, 0xA85521AB), BN_DEF(0x04507A33, 0xAD33170D), BN_DEF(0x8AAAC42D, 0x15728E5A), BN_DEF(0x98FA0510, 0x15D22618), BN_DEF(0xEA956AE5, 0x3995497C), BN_DEF(0x95581718, 0xDE2BCBF6), BN_DEF(0x6F4C52C9, 0xB5C55DF0), BN_DEF(0xEC07A28F, 0x9B2783A2), BN_DEF(0x180E8603, 0xE39E772C), BN_DEF(0x2E36CE3B, 0x32905E46), BN_DEF(0xCA18217C, 0xF1746C08), BN_DEF(0x4ABC9804, 0x670C354E), BN_DEF(0x7096966D, 0x9ED52907), BN_DEF(0x208552BB, 0x1C62F356), BN_DEF(0xDCA3AD96, 0x83655D23), BN_DEF(0xFD24CF5F, 0x69163FA8), BN_DEF(0x1C55D39A, 0x98DA4836), BN_DEF(0xA163BF05, 0xC2007CB8), BN_DEF(0xECE45B3D, 0x49286651), BN_DEF(0x7C4B1FE6, 0xAE9F2411), BN_DEF(0x5A899FA5, 0xEE386BFB), BN_DEF(0xF406B7ED, 0x0BFF5CB6), BN_DEF(0xA637ED6B, 0xF44C42E9), BN_DEF(0x625E7EC6, 0xE485B576), BN_DEF(0x6D51C245, 0x4FE1356D), BN_DEF(0xF25F1437, 0x302B0A6D), BN_DEF(0xCD3A431B, 0xEF9519B3), BN_DEF(0x8E3404DD, 0x514A0879), BN_DEF(0x3B139B22, 0x020BBEA6), BN_DEF(0x8A67CC74, 0x29024E08), BN_DEF(0x80DC1CD1, 0xC4C6628B), BN_DEF(0x2168C234, 0xC90FDAA2), BN_DEF(0xFFFFFFFF, 0xFFFFFFFF) }; static const BN_ULONG modp_4096_q[] = { BN_DEF(0xFFFFFFFF, 0xFFFFFFFF), BN_DEF(0x9A0318CC, 0xA6FA1AE4), BN_DEF(0x48536047, 0xC37FDBEE), BN_DEF(0x46C7EEE0, 0xC9DA754C), BN_DEF(0xEAD82D54, 0x68034893), BN_DEF(0x10B8240E, 0xDC0DEEBB), BN_DEF(0x67716BD7, 0x8FB094B8), BN_DEF(0x28ADF3F6, 0x119DD0C3), BN_DEF(0xD04861D1, 0xCCD94B27), BN_DEF(0xA735E02E, 0x143E2CA3), BN_DEF(0x0FDF6553, 0x97477E0A), BN_DEF(0x826F477C, 0x6DDDE16D), BN_DEF(0x156A2674, 0x12C1F4E5), BN_DEF(0x5B0A85ED, 0x0D4A341A), BN_DEF(0x357A711E, 0x4CE1938C), BN_DEF(0x5EDD2D93, 0xC438CD08), BN_DEF(0x53C3F36B, 0x8D391E09), BN_DEF(0x54908400, 0x25C16890), BN_DEF(0x707E8847, 0xA1EDADFE), BN_DEF(0x3A72D598, 0x047127D0), BN_DEF(0x5D6CA371, 0x3B84C460), BN_DEF(0xBD30AEB6, 0x5DF08BAB), BN_DEF(0x0BBD9006, 0x290F958C), BN_DEF(0x9F643532, 0x6C3B0139), BN_DEF(0x6CC50432, 0xF897FD03), BN_DEF(0x0D697735, 0xE771E913), BN_DEF(0x2512B0CE, 0x8F464A70), BN_DEF(0x6D8499EB, 0xD5FAD746), BN_DEF(0xD370F263, 0xD9CB87C2), BN_DEF(0xAE83063E, 0x457538AB), BN_DEF(0x2C6DF785, 0x767DC282), BN_DEF(0xEF8E5D32, 0xD42A90D5), BN_DEF(0x82283D19, 0xD6998B86), BN_DEF(0x45556216, 0x0AB9472D), BN_DEF(0x4C7D0288, 0x8AE9130C), BN_DEF(0x754AB572, 0x1CCAA4BE), BN_DEF(0x4AAC0B8C, 0xEF15E5FB), BN_DEF(0x37A62964, 0xDAE2AEF8), BN_DEF(0x7603D147, 0xCD93C1D1), BN_DEF(0x0C074301, 0xF1CF3B96), BN_DEF(0x171B671D, 0x19482F23), BN_DEF(0x650C10BE, 0x78BA3604), BN_DEF(0x255E4C02, 0xB3861AA7), BN_DEF(0xB84B4B36, 0xCF6A9483), BN_DEF(0x1042A95D, 0x0E3179AB), BN_DEF(0xEE51D6CB, 0xC1B2AE91), BN_DEF(0x7E9267AF, 0x348B1FD4), BN_DEF(0x0E2AE9CD, 0xCC6D241B), BN_DEF(0x50B1DF82, 0xE1003E5C), BN_DEF(0xF6722D9E, 0x24943328), BN_DEF(0xBE258FF3, 0xD74F9208), BN_DEF(0xAD44CFD2, 0xF71C35FD), BN_DEF(0x7A035BF6, 0x85FFAE5B), BN_DEF(0xD31BF6B5, 0x7A262174), BN_DEF(0x312F3F63, 0xF242DABB), BN_DEF(0xB6A8E122, 0xA7F09AB6), BN_DEF(0xF92F8A1B, 0x98158536), BN_DEF(0xE69D218D, 0xF7CA8CD9), BN_DEF(0xC71A026E, 0x28A5043C), BN_DEF(0x1D89CD91, 0x0105DF53), BN_DEF(0x4533E63A, 0x94812704), BN_DEF(0xC06E0E68, 0x62633145), BN_DEF(0x10B4611A, 0xE487ED51), BN_DEF(0xFFFFFFFF, 0x7FFFFFFF), }; static const BN_ULONG modp_6144_p[] = { BN_DEF(0xFFFFFFFF, 0xFFFFFFFF), BN_DEF(0x6DCC4024, 0xE694F91E), BN_DEF(0x0B7474D6, 0x12BF2D5B), BN_DEF(0x3F4860EE, 0x043E8F66), BN_DEF(0x6E3C0468, 0x387FE8D7), BN_DEF(0x2EF29632, 0xDA56C9EC), BN_DEF(0xA313D55C, 0xEB19CCB1), BN_DEF(0x8A1FBFF0, 0xF550AA3D), BN_DEF(0xB7C5DA76, 0x06A1D58B), BN_DEF(0xF29BE328, 0xA79715EE), BN_DEF(0x0F8037E0, 0x14CC5ED2), BN_DEF(0xBF48E1D8, 0xCC8F6D7E), BN_DEF(0x2B4154AA, 0x4BD407B2), BN_DEF(0xFF585AC5, 0x0F1D45B7), BN_DEF(0x36CC88BE, 0x23A97A7E), BN_DEF(0xBEC7E8F3, 0x59E7C97F), BN_DEF(0x900B1C9E, 0xB5A84031), BN_DEF(0x46980C82, 0xD55E702F), BN_DEF(0x6E74FEF6, 0xF482D7CE), BN_DEF(0xD1721D03, 0xF032EA15), BN_DEF(0xC64B92EC, 0x5983CA01), BN_DEF(0x378CD2BF, 0x6FB8F401), BN_DEF(0x2BD7AF42, 0x33205151), BN_DEF(0xE6CC254B, 0xDB7F1447), BN_DEF(0xCED4BB1B, 0x44CE6CBA), BN_DEF(0xCF9B14ED, 0xDA3EDBEB), BN_DEF(0x865A8918, 0x179727B0), BN_DEF(0x9027D831, 0xB06A53ED), BN_DEF(0x413001AE, 0xE5DB382F), BN_DEF(0xAD9E530E, 0xF8FF9406), BN_DEF(0x3DBA37BD, 0xC9751E76), BN_DEF(0x602646DE, 0xC1D4DCB2), BN_DEF(0xD27C7026, 0x36C3FAB4), BN_DEF(0x34028492, 0x4DF435C9), BN_DEF(0x90A6C08F, 0x86FFB7DC), BN_DEF(0x8D8FDDC1, 0x93B4EA98), BN_DEF(0xD5B05AA9, 0xD0069127), BN_DEF(0x2170481C, 0xB81BDD76), BN_DEF(0xCEE2D7AF, 0x1F612970), BN_DEF(0x515BE7ED, 0x233BA186), BN_DEF(0xA090C3A2, 0x99B2964F), BN_DEF(0x4E6BC05D, 0x287C5947), BN_DEF(0x1FBECAA6, 0x2E8EFC14), BN_DEF(0x04DE8EF9, 0xDBBBC2DB), BN_DEF(0x2AD44CE8, 0x2583E9CA), BN_DEF(0xB6150BDA, 0x1A946834), BN_DEF(0x6AF4E23C, 0x99C32718), BN_DEF(0xBDBA5B26, 0x88719A10), BN_DEF(0xA787E6D7, 0x1A723C12), BN_DEF(0xA9210801, 0x4B82D120), BN_DEF(0xE0FD108E, 0x43DB5BFC), BN_DEF(0x74E5AB31, 0x08E24FA0), BN_DEF(0xBAD946E2, 0x770988C0), BN_DEF(0x7A615D6C, 0xBBE11757), BN_DEF(0x177B200C, 0x521F2B18), BN_DEF(0x3EC86A64, 0xD8760273), BN_DEF(0xD98A0864, 0xF12FFA06), BN_DEF(0x1AD2EE6B, 0xCEE3D226), BN_DEF(0x4A25619D, 0x1E8C94E0), BN_DEF(0xDB0933D7, 0xABF5AE8C), BN_DEF(0xA6E1E4C7, 0xB3970F85), BN_DEF(0x5D060C7D, 0x8AEA7157), BN_DEF(0x58DBEF0A, 0xECFB8504), BN_DEF(0xDF1CBA64, 0xA85521AB), BN_DEF(0x04507A33, 0xAD33170D), BN_DEF(0x8AAAC42D, 0x15728E5A), BN_DEF(0x98FA0510, 0x15D22618), BN_DEF(0xEA956AE5, 0x3995497C), BN_DEF(0x95581718, 0xDE2BCBF6), BN_DEF(0x6F4C52C9, 0xB5C55DF0), BN_DEF(0xEC07A28F, 0x9B2783A2), BN_DEF(0x180E8603, 0xE39E772C), BN_DEF(0x2E36CE3B, 0x32905E46), BN_DEF(0xCA18217C, 0xF1746C08), BN_DEF(0x4ABC9804, 0x670C354E), BN_DEF(0x7096966D, 0x9ED52907), BN_DEF(0x208552BB, 0x1C62F356), BN_DEF(0xDCA3AD96, 0x83655D23), BN_DEF(0xFD24CF5F, 0x69163FA8), BN_DEF(0x1C55D39A, 0x98DA4836), BN_DEF(0xA163BF05, 0xC2007CB8), BN_DEF(0xECE45B3D, 0x49286651), BN_DEF(0x7C4B1FE6, 0xAE9F2411), BN_DEF(0x5A899FA5, 0xEE386BFB), BN_DEF(0xF406B7ED, 0x0BFF5CB6), BN_DEF(0xA637ED6B, 0xF44C42E9), BN_DEF(0x625E7EC6, 0xE485B576), BN_DEF(0x6D51C245, 0x4FE1356D), BN_DEF(0xF25F1437, 0x302B0A6D), BN_DEF(0xCD3A431B, 0xEF9519B3), BN_DEF(0x8E3404DD, 0x514A0879), BN_DEF(0x3B139B22, 0x020BBEA6), BN_DEF(0x8A67CC74, 0x29024E08), BN_DEF(0x80DC1CD1, 0xC4C6628B), BN_DEF(0x2168C234, 0xC90FDAA2), BN_DEF(0xFFFFFFFF, 0xFFFFFFFF) }; static const BN_ULONG modp_6144_q[] = { BN_DEF(0xFFFFFFFF, 0x7FFFFFFF), BN_DEF(0x36E62012, 0x734A7C8F), BN_DEF(0x85BA3A6B, 0x095F96AD), BN_DEF(0x1FA43077, 0x021F47B3), BN_DEF(0xB71E0234, 0x1C3FF46B), BN_DEF(0x17794B19, 0x6D2B64F6), BN_DEF(0xD189EAAE, 0x758CE658), BN_DEF(0xC50FDFF8, 0x7AA8551E), BN_DEF(0xDBE2ED3B, 0x0350EAC5), BN_DEF(0x794DF194, 0x53CB8AF7), BN_DEF(0x07C01BF0, 0x0A662F69), BN_DEF(0x5FA470EC, 0x6647B6BF), BN_DEF(0x15A0AA55, 0xA5EA03D9), BN_DEF(0xFFAC2D62, 0x078EA2DB), BN_DEF(0x1B66445F, 0x91D4BD3F), BN_DEF(0xDF63F479, 0x2CF3E4BF), BN_DEF(0xC8058E4F, 0x5AD42018), BN_DEF(0xA34C0641, 0x6AAF3817), BN_DEF(0x373A7F7B, 0xFA416BE7), BN_DEF(0xE8B90E81, 0x7819750A), BN_DEF(0xE325C976, 0xACC1E500), BN_DEF(0x9BC6695F, 0x37DC7A00), BN_DEF(0x95EBD7A1, 0x999028A8), BN_DEF(0xF36612A5, 0xEDBF8A23), BN_DEF(0x676A5D8D, 0xA267365D), BN_DEF(0xE7CD8A76, 0x6D1F6DF5), BN_DEF(0x432D448C, 0x8BCB93D8), BN_DEF(0xC813EC18, 0x583529F6), BN_DEF(0xA09800D7, 0x72ED9C17), BN_DEF(0x56CF2987, 0xFC7FCA03), BN_DEF(0x1EDD1BDE, 0x64BA8F3B), BN_DEF(0x3013236F, 0x60EA6E59), BN_DEF(0x693E3813, 0x1B61FD5A), BN_DEF(0x9A014249, 0xA6FA1AE4), BN_DEF(0x48536047, 0xC37FDBEE), BN_DEF(0x46C7EEE0, 0xC9DA754C), BN_DEF(0xEAD82D54, 0x68034893), BN_DEF(0x10B8240E, 0xDC0DEEBB), BN_DEF(0x67716BD7, 0x8FB094B8), BN_DEF(0x28ADF3F6, 0x119DD0C3), BN_DEF(0xD04861D1, 0xCCD94B27), BN_DEF(0xA735E02E, 0x143E2CA3), BN_DEF(0x0FDF6553, 0x97477E0A), BN_DEF(0x826F477C, 0x6DDDE16D), BN_DEF(0x156A2674, 0x12C1F4E5), BN_DEF(0x5B0A85ED, 0x0D4A341A), BN_DEF(0x357A711E, 0x4CE1938C), BN_DEF(0x5EDD2D93, 0xC438CD08), BN_DEF(0x53C3F36B, 0x8D391E09), BN_DEF(0x54908400, 0x25C16890), BN_DEF(0x707E8847, 0xA1EDADFE), BN_DEF(0x3A72D598, 0x047127D0), BN_DEF(0x5D6CA371, 0x3B84C460), BN_DEF(0xBD30AEB6, 0x5DF08BAB), BN_DEF(0x0BBD9006, 0x290F958C), BN_DEF(0x9F643532, 0x6C3B0139), BN_DEF(0x6CC50432, 0xF897FD03), BN_DEF(0x0D697735, 0xE771E913), BN_DEF(0x2512B0CE, 0x8F464A70), BN_DEF(0x6D8499EB, 0xD5FAD746), BN_DEF(0xD370F263, 0xD9CB87C2), BN_DEF(0xAE83063E, 0x457538AB), BN_DEF(0x2C6DF785, 0x767DC282), BN_DEF(0xEF8E5D32, 0xD42A90D5), BN_DEF(0x82283D19, 0xD6998B86), BN_DEF(0x45556216, 0x0AB9472D), BN_DEF(0x4C7D0288, 0x8AE9130C), BN_DEF(0x754AB572, 0x1CCAA4BE), BN_DEF(0x4AAC0B8C, 0xEF15E5FB), BN_DEF(0x37A62964, 0xDAE2AEF8), BN_DEF(0x7603D147, 0xCD93C1D1), BN_DEF(0x0C074301, 0xF1CF3B96), BN_DEF(0x171B671D, 0x19482F23), BN_DEF(0x650C10BE, 0x78BA3604), BN_DEF(0x255E4C02, 0xB3861AA7), BN_DEF(0xB84B4B36, 0xCF6A9483), BN_DEF(0x1042A95D, 0x0E3179AB), BN_DEF(0xEE51D6CB, 0xC1B2AE91), BN_DEF(0x7E9267AF, 0x348B1FD4), BN_DEF(0x0E2AE9CD, 0xCC6D241B), BN_DEF(0x50B1DF82, 0xE1003E5C), BN_DEF(0xF6722D9E, 0x24943328), BN_DEF(0xBE258FF3, 0xD74F9208), BN_DEF(0xAD44CFD2, 0xF71C35FD), BN_DEF(0x7A035BF6, 0x85FFAE5B), BN_DEF(0xD31BF6B5, 0x7A262174), BN_DEF(0x312F3F63, 0xF242DABB), BN_DEF(0xB6A8E122, 0xA7F09AB6), BN_DEF(0xF92F8A1B, 0x98158536), BN_DEF(0xE69D218D, 0xF7CA8CD9), BN_DEF(0xC71A026E, 0x28A5043C), BN_DEF(0x1D89CD91, 0x0105DF53), BN_DEF(0x4533E63A, 0x94812704), BN_DEF(0xC06E0E68, 0x62633145), BN_DEF(0x10B4611A, 0xE487ED51), BN_DEF(0xFFFFFFFF, 0x7FFFFFFF), }; static const BN_ULONG modp_8192_p[] = { BN_DEF(0xFFFFFFFF, 0xFFFFFFFF), BN_DEF(0x98EDD3DF, 0x60C980DD), BN_DEF(0x80B96E71, 0xC81F56E8), BN_DEF(0x765694DF, 0x9E3050E2), BN_DEF(0x5677E9AA, 0x9558E447), BN_DEF(0xFC026E47, 0xC9190DA6), BN_DEF(0xD5EE382B, 0x889A002E), BN_DEF(0x481C6CD7, 0x4009438B), BN_DEF(0xEB879F92, 0x359046F4), BN_DEF(0x1ECFA268, 0xFAF36BC3), BN_DEF(0x7EE74D73, 0xB1D510BD), BN_DEF(0x5DED7EA1, 0xF9AB4819), BN_DEF(0x0846851D, 0x64F31CC5), BN_DEF(0xA0255DC1, 0x4597E899), BN_DEF(0x74AB6A36, 0xDF310EE0), BN_DEF(0x3F44F82D, 0x6D2A13F8), BN_DEF(0xB3A278A6, 0x062B3CF5), BN_DEF(0xED5BDD3A, 0x79683303), BN_DEF(0xA2C087E8, 0xFA9D4B7F), BN_DEF(0x2F8385DD, 0x4BCBC886), BN_DEF(0x6CEA306B, 0x3473FC64), BN_DEF(0x1A23F0C7, 0x13EB57A8), BN_DEF(0xA4037C07, 0x22222E04), BN_DEF(0xFC848AD9, 0xE3FDB8BE), BN_DEF(0xE39D652D, 0x238F16CB), BN_DEF(0x2BF1C978, 0x3423B474), BN_DEF(0x5AE4F568, 0x3AAB639C), BN_DEF(0x6BA42466, 0x2576F693), BN_DEF(0x8AFC47ED, 0x741FA7BF), BN_DEF(0x8D9DD300, 0x3BC832B6), BN_DEF(0x73B931BA, 0xD8BEC4D0), BN_DEF(0xA932DF8C, 0x38777CB6), BN_DEF(0x12FEE5E4, 0x74A3926F), BN_DEF(0x6DBE1159, 0xE694F91E), BN_DEF(0x0B7474D6, 0x12BF2D5B), BN_DEF(0x3F4860EE, 0x043E8F66), BN_DEF(0x6E3C0468, 0x387FE8D7), BN_DEF(0x2EF29632, 0xDA56C9EC), BN_DEF(0xA313D55C, 0xEB19CCB1), BN_DEF(0x8A1FBFF0, 0xF550AA3D), BN_DEF(0xB7C5DA76, 0x06A1D58B), BN_DEF(0xF29BE328, 0xA79715EE), BN_DEF(0x0F8037E0, 0x14CC5ED2), BN_DEF(0xBF48E1D8, 0xCC8F6D7E), BN_DEF(0x2B4154AA, 0x4BD407B2), BN_DEF(0xFF585AC5, 0x0F1D45B7), BN_DEF(0x36CC88BE, 0x23A97A7E), BN_DEF(0xBEC7E8F3, 0x59E7C97F), BN_DEF(0x900B1C9E, 0xB5A84031), BN_DEF(0x46980C82, 0xD55E702F), BN_DEF(0x6E74FEF6, 0xF482D7CE), BN_DEF(0xD1721D03, 0xF032EA15), BN_DEF(0xC64B92EC, 0x5983CA01), BN_DEF(0x378CD2BF, 0x6FB8F401), BN_DEF(0x2BD7AF42, 0x33205151), BN_DEF(0xE6CC254B, 0xDB7F1447), BN_DEF(0xCED4BB1B, 0x44CE6CBA), BN_DEF(0xCF9B14ED, 0xDA3EDBEB), BN_DEF(0x865A8918, 0x179727B0), BN_DEF(0x9027D831, 0xB06A53ED), BN_DEF(0x413001AE, 0xE5DB382F), BN_DEF(0xAD9E530E, 0xF8FF9406), BN_DEF(0x3DBA37BD, 0xC9751E76), BN_DEF(0x602646DE, 0xC1D4DCB2), BN_DEF(0xD27C7026, 0x36C3FAB4), BN_DEF(0x34028492, 0x4DF435C9), BN_DEF(0x90A6C08F, 0x86FFB7DC), BN_DEF(0x8D8FDDC1, 0x93B4EA98), BN_DEF(0xD5B05AA9, 0xD0069127), BN_DEF(0x2170481C, 0xB81BDD76), BN_DEF(0xCEE2D7AF, 0x1F612970), BN_DEF(0x515BE7ED, 0x233BA186), BN_DEF(0xA090C3A2, 0x99B2964F), BN_DEF(0x4E6BC05D, 0x287C5947), BN_DEF(0x1FBECAA6, 0x2E8EFC14), BN_DEF(0x04DE8EF9, 0xDBBBC2DB), BN_DEF(0x2AD44CE8, 0x2583E9CA), BN_DEF(0xB6150BDA, 0x1A946834), BN_DEF(0x6AF4E23C, 0x99C32718), BN_DEF(0xBDBA5B26, 0x88719A10), BN_DEF(0xA787E6D7, 0x1A723C12), BN_DEF(0xA9210801, 0x4B82D120), BN_DEF(0xE0FD108E, 0x43DB5BFC), BN_DEF(0x74E5AB31, 0x08E24FA0), BN_DEF(0xBAD946E2, 0x770988C0), BN_DEF(0x7A615D6C, 0xBBE11757), BN_DEF(0x177B200C, 0x521F2B18), BN_DEF(0x3EC86A64, 0xD8760273), BN_DEF(0xD98A0864, 0xF12FFA06), BN_DEF(0x1AD2EE6B, 0xCEE3D226), BN_DEF(0x4A25619D, 0x1E8C94E0), BN_DEF(0xDB0933D7, 0xABF5AE8C), BN_DEF(0xA6E1E4C7, 0xB3970F85), BN_DEF(0x5D060C7D, 0x8AEA7157), BN_DEF(0x58DBEF0A, 0xECFB8504), BN_DEF(0xDF1CBA64, 0xA85521AB), BN_DEF(0x04507A33, 0xAD33170D), BN_DEF(0x8AAAC42D, 0x15728E5A), BN_DEF(0x98FA0510, 0x15D22618), BN_DEF(0xEA956AE5, 0x3995497C), BN_DEF(0x95581718, 0xDE2BCBF6), BN_DEF(0x6F4C52C9, 0xB5C55DF0), BN_DEF(0xEC07A28F, 0x9B2783A2), BN_DEF(0x180E8603, 0xE39E772C), BN_DEF(0x2E36CE3B, 0x32905E46), BN_DEF(0xCA18217C, 0xF1746C08), BN_DEF(0x4ABC9804, 0x670C354E), BN_DEF(0x7096966D, 0x9ED52907), BN_DEF(0x208552BB, 0x1C62F356), BN_DEF(0xDCA3AD96, 0x83655D23), BN_DEF(0xFD24CF5F, 0x69163FA8), BN_DEF(0x1C55D39A, 0x98DA4836), BN_DEF(0xA163BF05, 0xC2007CB8), BN_DEF(0xECE45B3D, 0x49286651), BN_DEF(0x7C4B1FE6, 0xAE9F2411), BN_DEF(0x5A899FA5, 0xEE386BFB), BN_DEF(0xF406B7ED, 0x0BFF5CB6), BN_DEF(0xA637ED6B, 0xF44C42E9), BN_DEF(0x625E7EC6, 0xE485B576), BN_DEF(0x6D51C245, 0x4FE1356D), BN_DEF(0xF25F1437, 0x302B0A6D), BN_DEF(0xCD3A431B, 0xEF9519B3), BN_DEF(0x8E3404DD, 0x514A0879), BN_DEF(0x3B139B22, 0x020BBEA6), BN_DEF(0x8A67CC74, 0x29024E08), BN_DEF(0x80DC1CD1, 0xC4C6628B), BN_DEF(0x2168C234, 0xC90FDAA2), BN_DEF(0xFFFFFFFF, 0xFFFFFFFF) }; static const BN_ULONG modp_8192_q[] = { BN_DEF(0xFFFFFFFF, 0xFFFFFFFF), BN_DEF(0xCC76E9EF, 0xB064C06E), BN_DEF(0x405CB738, 0xE40FAB74), BN_DEF(0x3B2B4A6F, 0x4F182871), BN_DEF(0xAB3BF4D5, 0xCAAC7223), BN_DEF(0x7E013723, 0xE48C86D3), BN_DEF(0x6AF71C15, 0xC44D0017), BN_DEF(0xA40E366B, 0x2004A1C5), BN_DEF(0x75C3CFC9, 0x1AC8237A), BN_DEF(0x8F67D134, 0xFD79B5E1), BN_DEF(0xBF73A6B9, 0xD8EA885E), BN_DEF(0xAEF6BF50, 0xFCD5A40C), BN_DEF(0x8423428E, 0xB2798E62), BN_DEF(0xD012AEE0, 0x22CBF44C), BN_DEF(0x3A55B51B, 0xEF988770), BN_DEF(0x1FA27C16, 0x369509FC), BN_DEF(0xD9D13C53, 0x03159E7A), BN_DEF(0xF6ADEE9D, 0x3CB41981), BN_DEF(0xD16043F4, 0xFD4EA5BF), BN_DEF(0x17C1C2EE, 0xA5E5E443), BN_DEF(0x36751835, 0x9A39FE32), BN_DEF(0x0D11F863, 0x89F5ABD4), BN_DEF(0x5201BE03, 0x91111702), BN_DEF(0x7E42456C, 0xF1FEDC5F), BN_DEF(0xF1CEB296, 0x11C78B65), BN_DEF(0x15F8E4BC, 0x1A11DA3A), BN_DEF(0x2D727AB4, 0x1D55B1CE), BN_DEF(0xB5D21233, 0x92BB7B49), BN_DEF(0xC57E23F6, 0x3A0FD3DF), BN_DEF(0x46CEE980, 0x1DE4195B), BN_DEF(0x39DC98DD, 0x6C5F6268), BN_DEF(0x54996FC6, 0x1C3BBE5B), BN_DEF(0x897F72F2, 0xBA51C937), BN_DEF(0x36DF08AC, 0x734A7C8F), BN_DEF(0x85BA3A6B, 0x095F96AD), BN_DEF(0x1FA43077, 0x021F47B3), BN_DEF(0xB71E0234, 0x1C3FF46B), BN_DEF(0x17794B19, 0x6D2B64F6), BN_DEF(0xD189EAAE, 0x758CE658), BN_DEF(0xC50FDFF8, 0x7AA8551E), BN_DEF(0xDBE2ED3B, 0x0350EAC5), BN_DEF(0x794DF194, 0x53CB8AF7), BN_DEF(0x07C01BF0, 0x0A662F69), BN_DEF(0x5FA470EC, 0x6647B6BF), BN_DEF(0x15A0AA55, 0xA5EA03D9), BN_DEF(0xFFAC2D62, 0x078EA2DB), BN_DEF(0x1B66445F, 0x91D4BD3F), BN_DEF(0xDF63F479, 0x2CF3E4BF), BN_DEF(0xC8058E4F, 0x5AD42018), BN_DEF(0xA34C0641, 0x6AAF3817), BN_DEF(0x373A7F7B, 0xFA416BE7), BN_DEF(0xE8B90E81, 0x7819750A), BN_DEF(0xE325C976, 0xACC1E500), BN_DEF(0x9BC6695F, 0x37DC7A00), BN_DEF(0x95EBD7A1, 0x999028A8), BN_DEF(0xF36612A5, 0xEDBF8A23), BN_DEF(0x676A5D8D, 0xA267365D), BN_DEF(0xE7CD8A76, 0x6D1F6DF5), BN_DEF(0x432D448C, 0x8BCB93D8), BN_DEF(0xC813EC18, 0x583529F6), BN_DEF(0xA09800D7, 0x72ED9C17), BN_DEF(0x56CF2987, 0xFC7FCA03), BN_DEF(0x1EDD1BDE, 0x64BA8F3B), BN_DEF(0x3013236F, 0x60EA6E59), BN_DEF(0x693E3813, 0x1B61FD5A), BN_DEF(0x9A014249, 0xA6FA1AE4), BN_DEF(0x48536047, 0xC37FDBEE), BN_DEF(0x46C7EEE0, 0xC9DA754C), BN_DEF(0xEAD82D54, 0x68034893), BN_DEF(0x10B8240E, 0xDC0DEEBB), BN_DEF(0x67716BD7, 0x8FB094B8), BN_DEF(0x28ADF3F6, 0x119DD0C3), BN_DEF(0xD04861D1, 0xCCD94B27), BN_DEF(0xA735E02E, 0x143E2CA3), BN_DEF(0x0FDF6553, 0x97477E0A), BN_DEF(0x826F477C, 0x6DDDE16D), BN_DEF(0x156A2674, 0x12C1F4E5), BN_DEF(0x5B0A85ED, 0x0D4A341A), BN_DEF(0x357A711E, 0x4CE1938C), BN_DEF(0x5EDD2D93, 0xC438CD08), BN_DEF(0x53C3F36B, 0x8D391E09), BN_DEF(0x54908400, 0x25C16890), BN_DEF(0x707E8847, 0xA1EDADFE), BN_DEF(0x3A72D598, 0x047127D0), BN_DEF(0x5D6CA371, 0x3B84C460), BN_DEF(0xBD30AEB6, 0x5DF08BAB), BN_DEF(0x0BBD9006, 0x290F958C), BN_DEF(0x9F643532, 0x6C3B0139), BN_DEF(0x6CC50432, 0xF897FD03), BN_DEF(0x0D697735, 0xE771E913), BN_DEF(0x2512B0CE, 0x8F464A70), BN_DEF(0x6D8499EB, 0xD5FAD746), BN_DEF(0xD370F263, 0xD9CB87C2), BN_DEF(0xAE83063E, 0x457538AB), BN_DEF(0x2C6DF785, 0x767DC282), BN_DEF(0xEF8E5D32, 0xD42A90D5), BN_DEF(0x82283D19, 0xD6998B86), BN_DEF(0x45556216, 0x0AB9472D), BN_DEF(0x4C7D0288, 0x8AE9130C), BN_DEF(0x754AB572, 0x1CCAA4BE), BN_DEF(0x4AAC0B8C, 0xEF15E5FB), BN_DEF(0x37A62964, 0xDAE2AEF8), BN_DEF(0x7603D147, 0xCD93C1D1), BN_DEF(0x0C074301, 0xF1CF3B96), BN_DEF(0x171B671D, 0x19482F23), BN_DEF(0x650C10BE, 0x78BA3604), BN_DEF(0x255E4C02, 0xB3861AA7), BN_DEF(0xB84B4B36, 0xCF6A9483), BN_DEF(0x1042A95D, 0x0E3179AB), BN_DEF(0xEE51D6CB, 0xC1B2AE91), BN_DEF(0x7E9267AF, 0x348B1FD4), BN_DEF(0x0E2AE9CD, 0xCC6D241B), BN_DEF(0x50B1DF82, 0xE1003E5C), BN_DEF(0xF6722D9E, 0x24943328), BN_DEF(0xBE258FF3, 0xD74F9208), BN_DEF(0xAD44CFD2, 0xF71C35FD), BN_DEF(0x7A035BF6, 0x85FFAE5B), BN_DEF(0xD31BF6B5, 0x7A262174), BN_DEF(0x312F3F63, 0xF242DABB), BN_DEF(0xB6A8E122, 0xA7F09AB6), BN_DEF(0xF92F8A1B, 0x98158536), BN_DEF(0xE69D218D, 0xF7CA8CD9), BN_DEF(0xC71A026E, 0x28A5043C), BN_DEF(0x1D89CD91, 0x0105DF53), BN_DEF(0x4533E63A, 0x94812704), BN_DEF(0xC06E0E68, 0x62633145), BN_DEF(0x10B4611A, 0xE487ED51), BN_DEF(0xFFFFFFFF, 0x7FFFFFFF), }; static const BN_ULONG dh1024_160_p[] = { BN_DEF(0x2E4A4371, 0xDF1FB2BC), BN_DEF(0x6D4DA708, 0xE68CFDA7), BN_DEF(0x365C1A65, 0x45BF37DF), BN_DEF(0x0DC8B4BD, 0xA151AF5F), BN_DEF(0xF55BCCC0, 0xFAA31A4F), BN_DEF(0xE5644738, 0x4EFFD6FA), BN_DEF(0x219A7372, 0x98488E9C), BN_DEF(0x90C4BD70, 0xACCBDD7D), BN_DEF(0xD49B83BF, 0x24975C3C), BN_DEF(0xA9061123, 0x13ECB4AE), BN_DEF(0x2EE652C0, 0x9838EF1E), BN_DEF(0x75A23D18, 0x6073E286), BN_DEF(0x52D23B61, 0x9A6A9DCA), BN_DEF(0xFB06A3C6, 0x52C99FBC), BN_DEF(0xAE5D54EC, 0xDE92DE5E), BN_DEF(0xA080E01D, 0xB10B8F96) }; static const BN_ULONG dh1024_160_q[] = { BN_DEF(0x49462353, 0x64B7CB9D), BN_DEF(0x8ABA4E7D, 0x81A8DF27), (BN_ULONG)0xF518AA87 }; static const BN_ULONG dh1024_160_g[] = { BN_DEF(0x22B3B2E5, 0x855E6EEB), BN_DEF(0xF97C2A24, 0x858F4DCE), BN_DEF(0x18D08BC8, 0x2D779D59), BN_DEF(0x8E73AFA3, 0xD662A4D1), BN_DEF(0x69B6A28A, 0x1DBF0A01), BN_DEF(0x7A091F53, 0xA6A24C08), BN_DEF(0x63F80A76, 0x909D0D22), BN_DEF(0xB9A92EE1, 0xD7FBD7D3), BN_DEF(0x9E2749F4, 0x5E91547F), BN_DEF(0xB01B886A, 0x160217B4), BN_DEF(0x5504F213, 0x777E690F), BN_DEF(0x5C41564B, 0x266FEA1E), BN_DEF(0x14266D31, 0xD6406CFF), BN_DEF(0x58AC507F, 0xF8104DD2), BN_DEF(0xEFB99905, 0x6765A442), BN_DEF(0xC3FD3412, 0xA4D1CBD5) }; static const BN_ULONG dh2048_224_p[] = { BN_DEF(0x0C10E64F, 0x0AC4DFFE), BN_DEF(0x4E71B81C, 0xCF9DE538), BN_DEF(0xFFA31F71, 0x7EF363E2), BN_DEF(0x6B8E75B9, 0xE3FB73C1), BN_DEF(0x4BA80A29, 0xC9B53DCF), BN_DEF(0x16E79763, 0x23F10B0E), BN_DEF(0x13042E9B, 0xC52172E4), BN_DEF(0xC928B2B9, 0xBE60E69C), BN_DEF(0xB9E587E8, 0x80CD86A1), BN_DEF(0x98C641A4, 0x315D75E1), BN_DEF(0x44328387, 0xCDF93ACC), BN_DEF(0xDC0A486D, 0x15987D9A), BN_DEF(0x1FD5A074, 0x7310F712), BN_DEF(0xDE31EFDC, 0x278273C7), BN_DEF(0x415D9330, 0x1602E714), BN_DEF(0xBC8985DB, 0x81286130), BN_DEF(0x70918836, 0xB3BF8A31), BN_DEF(0xB9C49708, 0x6A00E0A0), BN_DEF(0x8BBC27BE, 0xC6BA0B2C), BN_DEF(0xED34DBF6, 0xC9F98D11), BN_DEF(0xB6C12207, 0x7AD5B7D0), BN_DEF(0x55B7394B, 0xD91E8FEF), BN_DEF(0xEFDA4DF8, 0x9037C9ED), BN_DEF(0xAD6AC212, 0x6D3F8152), BN_DEF(0x1274A0A6, 0x1DE6B85A), BN_DEF(0x309C180E, 0xEB3D688A), BN_DEF(0x7BA1DF15, 0xAF9A3C40), BN_DEF(0xF95A56DB, 0xE6FA141D), BN_DEF(0xB61D0A75, 0xB54B1597), BN_DEF(0x683B9FD1, 0xA20D64E5), BN_DEF(0x9559C51F, 0xD660FAA7), BN_DEF(0x9123A9D0, 0xAD107E1E) }; static const BN_ULONG dh2048_224_q[] = { BN_DEF(0xB36371EB, 0xBF389A99), BN_DEF(0x4738CEBC, 0x1F80535A), BN_DEF(0x99717710, 0xC58D93FE), (BN_ULONG)0x801C0D34 }; static const BN_ULONG dh2048_224_g[] = { BN_DEF(0x191F2BFA, 0x84B890D3), BN_DEF(0x2A7065B3, 0x81BC087F), BN_DEF(0xF6EC0179, 0x19C418E1), BN_DEF(0x71CFFF4C, 0x7B5A0F1C), BN_DEF(0x9B6AA4BD, 0xEDFE72FE), BN_DEF(0x94B30269, 0x81E1BCFE), BN_DEF(0x8D6C0191, 0x566AFBB4), BN_DEF(0x409D13CD, 0xB539CCE3), BN_DEF(0x5F2FF381, 0x6AA21E7F), BN_DEF(0x770589EF, 0xD9E263E4), BN_DEF(0xD19963DD, 0x10E183ED), BN_DEF(0x150B8EEB, 0xB70A8137), BN_DEF(0x28C8F8AC, 0x051AE3D4), BN_DEF(0x0C1AB15B, 0xBB77A86F), BN_DEF(0x16A330EF, 0x6E3025E3), BN_DEF(0xD6F83456, 0x19529A45), BN_DEF(0x118E98D1, 0xF180EB34), BN_DEF(0x50717CBE, 0xB5F6C6B2), BN_DEF(0xDA7460CD, 0x09939D54), BN_DEF(0x22EA1ED4, 0xE2471504), BN_DEF(0x521BC98A, 0xB8A762D0), BN_DEF(0x5AC1348B, 0xF4D02727), BN_DEF(0x1999024A, 0xC1766910), BN_DEF(0xA8D66AD7, 0xBE5E9001), BN_DEF(0x620A8652, 0xC57DB17C), BN_DEF(0x00C29F52, 0xAB739D77), BN_DEF(0xA70C4AFA, 0xDD921F01), BN_DEF(0x10B9A6F0, 0xA6824A4E), BN_DEF(0xCFE4FFE3, 0x74866A08), BN_DEF(0x89998CAF, 0x6CDEBE7B), BN_DEF(0x8FFDAC50, 0x9DF30B5C), BN_DEF(0x4F2D9AE3, 0xAC4032EF) }; static const BN_ULONG dh2048_256_p[] = { BN_DEF(0x1E1A1597, 0xDB094AE9), BN_DEF(0xD7EF09CA, 0x693877FA), BN_DEF(0x6E11715F, 0x6116D227), BN_DEF(0xC198AF12, 0xA4B54330), BN_DEF(0xD7014103, 0x75F26375), BN_DEF(0x54E710C3, 0xC3A3960A), BN_DEF(0xBD0BE621, 0xDED4010A), BN_DEF(0x89962856, 0xC0B857F6), BN_DEF(0x71506026, 0xB3CA3F79), BN_DEF(0xE6B486F6, 0x1CCACB83), BN_DEF(0x14056425, 0x67E144E5), BN_DEF(0xA41825D9, 0xF6A167B5), BN_DEF(0x96524D8E, 0x3AD83477), BN_DEF(0x51BFA4AB, 0xF13C6D9A), BN_DEF(0x35488A0E, 0x2D525267), BN_DEF(0xCAA6B790, 0xB63ACAE1), BN_DEF(0x81B23F76, 0x4FDB70C5), BN_DEF(0x12307F5C, 0xBC39A0BF), BN_DEF(0xB1E59BB8, 0xB941F54E), BN_DEF(0xD45F9088, 0x6C5BFC11), BN_DEF(0x4275BF7B, 0x22E0B1EF), BN_DEF(0x5B4758C0, 0x91F9E672), BN_DEF(0x6BCF67ED, 0x5A8A9D30), BN_DEF(0x97517ABD, 0x209E0C64), BN_DEF(0x830E9A7C, 0x3BF4296D), BN_DEF(0x34096FAA, 0x16C3D911), BN_DEF(0x61B2AA30, 0xFAF7DF45), BN_DEF(0xD61957D4, 0xE00DF8F1), BN_DEF(0x435E3B00, 0x5D2CEED4), BN_DEF(0x660DD0F2, 0x8CEEF608), BN_DEF(0x65195999, 0xFFBBD19C), BN_DEF(0xB4B6663C, 0x87A8E61D) }; static const BN_ULONG dh2048_256_q[] = { BN_DEF(0x64F5FBD3, 0xA308B0FE), BN_DEF(0x1EB3750B, 0x99B1A47D), BN_DEF(0x40129DA2, 0xB4479976), BN_DEF(0xA709A097, 0x8CF83642) }; static const BN_ULONG dh2048_256_g[] = { BN_DEF(0x6CC41659, 0x664B4C0F), BN_DEF(0xEF98C582, 0x5E2327CF), BN_DEF(0xD4795451, 0xD647D148), BN_DEF(0x90F00EF8, 0x2F630784), BN_DEF(0x1DB246C3, 0x184B523D), BN_DEF(0xCDC67EB6, 0xC7891428), BN_DEF(0x0DF92B52, 0x7FD02837), BN_DEF(0x64E0EC37, 0xB3353BBB), BN_DEF(0x57CD0915, 0xECD06E15), BN_DEF(0xDF016199, 0xB7D2BBD2), BN_DEF(0x052588B9, 0xC8484B1E), BN_DEF(0x13D3FE14, 0xDB2A3B73), BN_DEF(0xD182EA0A, 0xD052B985), BN_DEF(0xE83B9C80, 0xA4BD1BFF), BN_DEF(0xFB3F2E55, 0xDFC967C1), BN_DEF(0x767164E1, 0xB5045AF2), BN_DEF(0x6F2F9193, 0x1D14348F), BN_DEF(0x428EBC83, 0x64E67982), BN_DEF(0x82D6ED38, 0x8AC376D2), BN_DEF(0xAAB8A862, 0x777DE62A), BN_DEF(0xE9EC144B, 0xDDF463E5), BN_DEF(0xC77A57F2, 0x0196F931), BN_DEF(0x41000A65, 0xA55AE313), BN_DEF(0xC28CBB18, 0x901228F8), BN_DEF(0x7E8C6F62, 0xBC3773BF), BN_DEF(0x0C6B47B1, 0xBE3A6C1B), BN_DEF(0xAC0BB555, 0xFF4FED4A), BN_DEF(0x77BE463F, 0x10DBC150), BN_DEF(0x1A0BA125, 0x07F4793A), BN_DEF(0x21EF2054, 0x4CA7B18F), BN_DEF(0x60EDBD48, 0x2E775066), BN_DEF(0x73134D0B, 0x3FB32C9B) }; static const BN_ULONG ffdhe2048_p[] = { BN_DEF(0xFFFFFFFF, 0xFFFFFFFF), BN_DEF(0x61285C97, 0x886B4238), BN_DEF(0xC1B2EFFA, 0xC6F34A26), BN_DEF(0x7D1683B2, 0xC58EF183), BN_DEF(0x2EC22005, 0x3BB5FCBC), BN_DEF(0x4C6FAD73, 0xC3FE3B1B), BN_DEF(0xEEF28183, 0x8E4F1232), BN_DEF(0xE98583FF, 0x9172FE9C), BN_DEF(0x28342F61, 0xC03404CD), BN_DEF(0xCDF7E2EC, 0x9E02FCE1), BN_DEF(0xEE0A6D70, 0x0B07A7C8), BN_DEF(0x6372BB19, 0xAE56EDE7), BN_DEF(0xDE394DF4, 0x1D4F42A3), BN_DEF(0x60D7F468, 0xB96ADAB7), BN_DEF(0xB2C8E3FB, 0xD108A94B), BN_DEF(0xB324FB61, 0xBC0AB182), BN_DEF(0x483A797A, 0x30ACCA4F), BN_DEF(0x36ADE735, 0x1DF158A1), BN_DEF(0xF3EFE872, 0xE2A689DA), BN_DEF(0xE0E68B77, 0x984F0C70), BN_DEF(0x7F57C935, 0xB557135E), BN_DEF(0x3DED1AF3, 0x85636555), BN_DEF(0x5F066ED0, 0x2433F51F), BN_DEF(0xD5FD6561, 0xD3DF1ED5), BN_DEF(0xAEC4617A, 0xF681B202), BN_DEF(0x630C75D8, 0x7D2FE363), BN_DEF(0x249B3EF9, 0xCC939DCE), BN_DEF(0x146433FB, 0xA9E13641), BN_DEF(0xCE2D3695, 0xD8B9C583), BN_DEF(0x273D3CF1, 0xAFDC5620), BN_DEF(0xA2BB4A9A, 0xADF85458), BN_DEF(0xFFFFFFFF, 0xFFFFFFFF) }; static const BN_ULONG ffdhe2048_q[] = { BN_DEF(0xFFFFFFFF, 0xFFFFFFFF), BN_DEF(0x30942E4B, 0x4435A11C), BN_DEF(0x60D977FD, 0x6379A513), BN_DEF(0xBE8B41D9, 0xE2C778C1), BN_DEF(0x17611002, 0x9DDAFE5E), BN_DEF(0xA637D6B9, 0xE1FF1D8D), BN_DEF(0x777940C1, 0xC7278919), BN_DEF(0x74C2C1FF, 0xC8B97F4E), BN_DEF(0x941A17B0, 0x601A0266), BN_DEF(0xE6FBF176, 0x4F017E70), BN_DEF(0x770536B8, 0x8583D3E4), BN_DEF(0xB1B95D8C, 0x572B76F3), BN_DEF(0xEF1CA6FA, 0x0EA7A151), BN_DEF(0xB06BFA34, 0xDCB56D5B), BN_DEF(0xD96471FD, 0xE88454A5), BN_DEF(0x59927DB0, 0x5E0558C1), BN_DEF(0xA41D3CBD, 0x98566527), BN_DEF(0x9B56F39A, 0x0EF8AC50), BN_DEF(0x79F7F439, 0xF15344ED), BN_DEF(0x707345BB, 0xCC278638), BN_DEF(0x3FABE49A, 0xDAAB89AF), BN_DEF(0x9EF68D79, 0x42B1B2AA), BN_DEF(0xAF833768, 0x9219FA8F), BN_DEF(0xEAFEB2B0, 0x69EF8F6A), BN_DEF(0x576230BD, 0x7B40D901), BN_DEF(0xB1863AEC, 0xBE97F1B1), BN_DEF(0x124D9F7C, 0xE649CEE7), BN_DEF(0x8A3219FD, 0xD4F09B20), BN_DEF(0xE7169B4A, 0xEC5CE2C1), BN_DEF(0x139E9E78, 0x57EE2B10), BN_DEF(0x515DA54D, 0xD6FC2A2C), BN_DEF(0xFFFFFFFF, 0x7FFFFFFF), }; static const BN_ULONG ffdhe3072_p[] = { BN_DEF(0xFFFFFFFF, 0xFFFFFFFF), BN_DEF(0x66C62E37, 0x25E41D2B), BN_DEF(0x3FD59D7C, 0x3C1B20EE), BN_DEF(0xFA53DDEF, 0x0ABCD06B), BN_DEF(0xD5C4484E, 0x1DBF9A42), BN_DEF(0x9B0DEADA, 0xABC52197), BN_DEF(0x22363A0D, 0xE86D2BC5), BN_DEF(0x9C9DF69E, 0x5CAE82AB), BN_DEF(0x71F54BFF, 0x64F2E21E), BN_DEF(0xE2D74DD3, 0xF4FD4452), BN_DEF(0xBC437944, 0xB4130C93), BN_DEF(0x85139270, 0xAEFE1309), BN_DEF(0xC186D91C, 0x598CB0FA), BN_DEF(0x91F7F7EE, 0x7AD91D26), BN_DEF(0xD6E6C907, 0x61B46FC9), BN_DEF(0xF99C0238, 0xBC34F4DE), BN_DEF(0x6519035B, 0xDE355B3B), BN_DEF(0x611FCFDC, 0x886B4238), BN_DEF(0xC1B2EFFA, 0xC6F34A26), BN_DEF(0x7D1683B2, 0xC58EF183), BN_DEF(0x2EC22005, 0x3BB5FCBC), BN_DEF(0x4C6FAD73, 0xC3FE3B1B), BN_DEF(0xEEF28183, 0x8E4F1232), BN_DEF(0xE98583FF, 0x9172FE9C), BN_DEF(0x28342F61, 0xC03404CD), BN_DEF(0xCDF7E2EC, 0x9E02FCE1), BN_DEF(0xEE0A6D70, 0x0B07A7C8), BN_DEF(0x6372BB19, 0xAE56EDE7), BN_DEF(0xDE394DF4, 0x1D4F42A3), BN_DEF(0x60D7F468, 0xB96ADAB7), BN_DEF(0xB2C8E3FB, 0xD108A94B), BN_DEF(0xB324FB61, 0xBC0AB182), BN_DEF(0x483A797A, 0x30ACCA4F), BN_DEF(0x36ADE735, 0x1DF158A1), BN_DEF(0xF3EFE872, 0xE2A689DA), BN_DEF(0xE0E68B77, 0x984F0C70), BN_DEF(0x7F57C935, 0xB557135E), BN_DEF(0x3DED1AF3, 0x85636555), BN_DEF(0x5F066ED0, 0x2433F51F), BN_DEF(0xD5FD6561, 0xD3DF1ED5), BN_DEF(0xAEC4617A, 0xF681B202), BN_DEF(0x630C75D8, 0x7D2FE363), BN_DEF(0x249B3EF9, 0xCC939DCE), BN_DEF(0x146433FB, 0xA9E13641), BN_DEF(0xCE2D3695, 0xD8B9C583), BN_DEF(0x273D3CF1, 0xAFDC5620), BN_DEF(0xA2BB4A9A, 0xADF85458), BN_DEF(0xFFFFFFFF, 0xFFFFFFFF) }; static const BN_ULONG ffdhe3072_q[] = { BN_DEF(0xFFFFFFFF, 0xFFFFFFFF), BN_DEF(0xB363171B, 0x12F20E95), BN_DEF(0x1FEACEBE, 0x9E0D9077), BN_DEF(0xFD29EEF7, 0x055E6835), BN_DEF(0x6AE22427, 0x0EDFCD21), BN_DEF(0xCD86F56D, 0xD5E290CB), BN_DEF(0x911B1D06, 0x743695E2), BN_DEF(0xCE4EFB4F, 0xAE574155), BN_DEF(0x38FAA5FF, 0xB279710F), BN_DEF(0x716BA6E9, 0x7A7EA229), BN_DEF(0xDE21BCA2, 0x5A098649), BN_DEF(0xC289C938, 0x577F0984), BN_DEF(0x60C36C8E, 0x2CC6587D), BN_DEF(0x48FBFBF7, 0xBD6C8E93), BN_DEF(0xEB736483, 0x30DA37E4), BN_DEF(0x7CCE011C, 0xDE1A7A6F), BN_DEF(0xB28C81AD, 0x6F1AAD9D), BN_DEF(0x308FE7EE, 0x4435A11C), BN_DEF(0x60D977FD, 0x6379A513), BN_DEF(0xBE8B41D9, 0xE2C778C1), BN_DEF(0x17611002, 0x9DDAFE5E), BN_DEF(0xA637D6B9, 0xE1FF1D8D), BN_DEF(0x777940C1, 0xC7278919), BN_DEF(0x74C2C1FF, 0xC8B97F4E), BN_DEF(0x941A17B0, 0x601A0266), BN_DEF(0xE6FBF176, 0x4F017E70), BN_DEF(0x770536B8, 0x8583D3E4), BN_DEF(0xB1B95D8C, 0x572B76F3), BN_DEF(0xEF1CA6FA, 0x0EA7A151), BN_DEF(0xB06BFA34, 0xDCB56D5B), BN_DEF(0xD96471FD, 0xE88454A5), BN_DEF(0x59927DB0, 0x5E0558C1), BN_DEF(0xA41D3CBD, 0x98566527), BN_DEF(0x9B56F39A, 0x0EF8AC50), BN_DEF(0x79F7F439, 0xF15344ED), BN_DEF(0x707345BB, 0xCC278638), BN_DEF(0x3FABE49A, 0xDAAB89AF), BN_DEF(0x9EF68D79, 0x42B1B2AA), BN_DEF(0xAF833768, 0x9219FA8F), BN_DEF(0xEAFEB2B0, 0x69EF8F6A), BN_DEF(0x576230BD, 0x7B40D901), BN_DEF(0xB1863AEC, 0xBE97F1B1), BN_DEF(0x124D9F7C, 0xE649CEE7), BN_DEF(0x8A3219FD, 0xD4F09B20), BN_DEF(0xE7169B4A, 0xEC5CE2C1), BN_DEF(0x139E9E78, 0x57EE2B10), BN_DEF(0x515DA54D, 0xD6FC2A2C), BN_DEF(0xFFFFFFFF, 0x7FFFFFFF), }; static const BN_ULONG ffdhe4096_p[] = { BN_DEF(0xFFFFFFFF, 0xFFFFFFFF), BN_DEF(0x5E655F6A, 0xC68A007E), BN_DEF(0xF44182E1, 0x4DB5A851), BN_DEF(0x7F88A46B, 0x8EC9B55A), BN_DEF(0xCEC97DCF, 0x0A8291CD), BN_DEF(0xF98D0ACC, 0x2A4ECEA9), BN_DEF(0x7140003C, 0x1A1DB93D), BN_DEF(0x33CB8B7A, 0x092999A3), BN_DEF(0x71AD0038, 0x6DC778F9), BN_DEF(0x918130C4, 0xA907600A), BN_DEF(0x2D9E6832, 0xED6A1E01), BN_DEF(0xEFB4318A, 0x7135C886), BN_DEF(0x7E31CC7A, 0x87F55BA5), BN_DEF(0x55034004, 0x7763CF1D), BN_DEF(0xD69F6D18, 0xAC7D5F42), BN_DEF(0xE58857B6, 0x7930E9E4), BN_DEF(0x164DF4FB, 0x6E6F52C3), BN_DEF(0x669E1EF1, 0x25E41D2B), BN_DEF(0x3FD59D7C, 0x3C1B20EE), BN_DEF(0xFA53DDEF, 0x0ABCD06B), BN_DEF(0xD5C4484E, 0x1DBF9A42), BN_DEF(0x9B0DEADA, 0xABC52197), BN_DEF(0x22363A0D, 0xE86D2BC5), BN_DEF(0x9C9DF69E, 0x5CAE82AB), BN_DEF(0x71F54BFF, 0x64F2E21E), BN_DEF(0xE2D74DD3, 0xF4FD4452), BN_DEF(0xBC437944, 0xB4130C93), BN_DEF(0x85139270, 0xAEFE1309), BN_DEF(0xC186D91C, 0x598CB0FA), BN_DEF(0x91F7F7EE, 0x7AD91D26), BN_DEF(0xD6E6C907, 0x61B46FC9), BN_DEF(0xF99C0238, 0xBC34F4DE), BN_DEF(0x6519035B, 0xDE355B3B), BN_DEF(0x611FCFDC, 0x886B4238), BN_DEF(0xC1B2EFFA, 0xC6F34A26), BN_DEF(0x7D1683B2, 0xC58EF183), BN_DEF(0x2EC22005, 0x3BB5FCBC), BN_DEF(0x4C6FAD73, 0xC3FE3B1B), BN_DEF(0xEEF28183, 0x8E4F1232), BN_DEF(0xE98583FF, 0x9172FE9C), BN_DEF(0x28342F61, 0xC03404CD), BN_DEF(0xCDF7E2EC, 0x9E02FCE1), BN_DEF(0xEE0A6D70, 0x0B07A7C8), BN_DEF(0x6372BB19, 0xAE56EDE7), BN_DEF(0xDE394DF4, 0x1D4F42A3), BN_DEF(0x60D7F468, 0xB96ADAB7), BN_DEF(0xB2C8E3FB, 0xD108A94B), BN_DEF(0xB324FB61, 0xBC0AB182), BN_DEF(0x483A797A, 0x30ACCA4F), BN_DEF(0x36ADE735, 0x1DF158A1), BN_DEF(0xF3EFE872, 0xE2A689DA), BN_DEF(0xE0E68B77, 0x984F0C70), BN_DEF(0x7F57C935, 0xB557135E), BN_DEF(0x3DED1AF3, 0x85636555), BN_DEF(0x5F066ED0, 0x2433F51F), BN_DEF(0xD5FD6561, 0xD3DF1ED5), BN_DEF(0xAEC4617A, 0xF681B202), BN_DEF(0x630C75D8, 0x7D2FE363), BN_DEF(0x249B3EF9, 0xCC939DCE), BN_DEF(0x146433FB, 0xA9E13641), BN_DEF(0xCE2D3695, 0xD8B9C583), BN_DEF(0x273D3CF1, 0xAFDC5620), BN_DEF(0xA2BB4A9A, 0xADF85458), BN_DEF(0xFFFFFFFF, 0xFFFFFFFF) }; static const BN_ULONG ffdhe4096_q[] = { BN_DEF(0xFFFFFFFF, 0x7FFFFFFF), BN_DEF(0x2F32AFB5, 0xE345003F), BN_DEF(0xFA20C170, 0xA6DAD428), BN_DEF(0x3FC45235, 0xC764DAAD), BN_DEF(0xE764BEE7, 0x054148E6), BN_DEF(0xFCC68566, 0x15276754), BN_DEF(0xB8A0001E, 0x0D0EDC9E), BN_DEF(0x99E5C5BD, 0x0494CCD1), BN_DEF(0xB8D6801C, 0x36E3BC7C), BN_DEF(0x48C09862, 0x5483B005), BN_DEF(0x96CF3419, 0x76B50F00), BN_DEF(0x77DA18C5, 0x389AE443), BN_DEF(0xBF18E63D, 0x43FAADD2), BN_DEF(0xAA81A002, 0x3BB1E78E), BN_DEF(0x6B4FB68C, 0x563EAFA1), BN_DEF(0x72C42BDB, 0xBC9874F2), BN_DEF(0x8B26FA7D, 0xB737A961), BN_DEF(0xB34F0F78, 0x12F20E95), BN_DEF(0x1FEACEBE, 0x9E0D9077), BN_DEF(0xFD29EEF7, 0x055E6835), BN_DEF(0x6AE22427, 0x0EDFCD21), BN_DEF(0xCD86F56D, 0xD5E290CB), BN_DEF(0x911B1D06, 0x743695E2), BN_DEF(0xCE4EFB4F, 0xAE574155), BN_DEF(0x38FAA5FF, 0xB279710F), BN_DEF(0x716BA6E9, 0x7A7EA229), BN_DEF(0xDE21BCA2, 0x5A098649), BN_DEF(0xC289C938, 0x577F0984), BN_DEF(0x60C36C8E, 0x2CC6587D), BN_DEF(0x48FBFBF7, 0xBD6C8E93), BN_DEF(0xEB736483, 0x30DA37E4), BN_DEF(0x7CCE011C, 0xDE1A7A6F), BN_DEF(0xB28C81AD, 0x6F1AAD9D), BN_DEF(0x308FE7EE, 0x4435A11C), BN_DEF(0x60D977FD, 0x6379A513), BN_DEF(0xBE8B41D9, 0xE2C778C1), BN_DEF(0x17611002, 0x9DDAFE5E), BN_DEF(0xA637D6B9, 0xE1FF1D8D), BN_DEF(0x777940C1, 0xC7278919), BN_DEF(0x74C2C1FF, 0xC8B97F4E), BN_DEF(0x941A17B0, 0x601A0266), BN_DEF(0xE6FBF176, 0x4F017E70), BN_DEF(0x770536B8, 0x8583D3E4), BN_DEF(0xB1B95D8C, 0x572B76F3), BN_DEF(0xEF1CA6FA, 0x0EA7A151), BN_DEF(0xB06BFA34, 0xDCB56D5B), BN_DEF(0xD96471FD, 0xE88454A5), BN_DEF(0x59927DB0, 0x5E0558C1), BN_DEF(0xA41D3CBD, 0x98566527), BN_DEF(0x9B56F39A, 0x0EF8AC50), BN_DEF(0x79F7F439, 0xF15344ED), BN_DEF(0x707345BB, 0xCC278638), BN_DEF(0x3FABE49A, 0xDAAB89AF), BN_DEF(0x9EF68D79, 0x42B1B2AA), BN_DEF(0xAF833768, 0x9219FA8F), BN_DEF(0xEAFEB2B0, 0x69EF8F6A), BN_DEF(0x576230BD, 0x7B40D901), BN_DEF(0xB1863AEC, 0xBE97F1B1), BN_DEF(0x124D9F7C, 0xE649CEE7), BN_DEF(0x8A3219FD, 0xD4F09B20), BN_DEF(0xE7169B4A, 0xEC5CE2C1), BN_DEF(0x139E9E78, 0x57EE2B10), BN_DEF(0x515DA54D, 0xD6FC2A2C), BN_DEF(0xFFFFFFFF, 0x7FFFFFFF), }; static const BN_ULONG ffdhe6144_p[] = { BN_DEF(0xFFFFFFFF, 0xFFFFFFFF), BN_DEF(0xD0E40E65, 0xA40E329C), BN_DEF(0x7938DAD4, 0xA41D570D), BN_DEF(0xD43161C1, 0x62A69526), BN_DEF(0x9ADB1E69, 0x3FDD4A8E), BN_DEF(0xDC6B80D6, 0x5B3B71F9), BN_DEF(0xC6272B04, 0xEC9D1810), BN_DEF(0xCACEF403, 0x8CCF2DD5), BN_DEF(0xC95B9117, 0xE49F5235), BN_DEF(0xB854338A, 0x505DC82D), BN_DEF(0x1562A846, 0x62292C31), BN_DEF(0x6AE77F5E, 0xD72B0374), BN_DEF(0x462D538C, 0xF9C9091B), BN_DEF(0x47A67CBE, 0x0AE8DB58), BN_DEF(0x22611682, 0xB3A739C1), BN_DEF(0x2A281BF6, 0xEEAAC023), BN_DEF(0x77CAF992, 0x94C6651E), BN_DEF(0x94B2BBC1, 0x763E4E4B), BN_DEF(0x0077D9B4, 0x587E38DA), BN_DEF(0x183023C3, 0x7FB29F8C), BN_DEF(0xF9E3A26E, 0x0ABEC1FF), BN_DEF(0x350511E3, 0xA00EF092), BN_DEF(0xDB6340D8, 0xB855322E), BN_DEF(0xA9A96910, 0xA52471F7), BN_DEF(0x4CFDB477, 0x388147FB), BN_DEF(0x4E46041F, 0x9B1F5C3E), BN_DEF(0xFCCFEC71, 0xCDAD0657), BN_DEF(0x4C701C3A, 0xB38E8C33), BN_DEF(0xB1C0FD4C, 0x917BDD64), BN_DEF(0x9B7624C8, 0x3BB45432), BN_DEF(0xCAF53EA6, 0x23BA4442), BN_DEF(0x38532A3A, 0x4E677D2C), BN_DEF(0x45036C7A, 0x0BFD64B6), BN_DEF(0x5E0DD902, 0xC68A007E), BN_DEF(0xF44182E1, 0x4DB5A851), BN_DEF(0x7F88A46B, 0x8EC9B55A), BN_DEF(0xCEC97DCF, 0x0A8291CD), BN_DEF(0xF98D0ACC, 0x2A4ECEA9), BN_DEF(0x7140003C, 0x1A1DB93D), BN_DEF(0x33CB8B7A, 0x092999A3), BN_DEF(0x71AD0038, 0x6DC778F9), BN_DEF(0x918130C4, 0xA907600A), BN_DEF(0x2D9E6832, 0xED6A1E01), BN_DEF(0xEFB4318A, 0x7135C886), BN_DEF(0x7E31CC7A, 0x87F55BA5), BN_DEF(0x55034004, 0x7763CF1D), BN_DEF(0xD69F6D18, 0xAC7D5F42), BN_DEF(0xE58857B6, 0x7930E9E4), BN_DEF(0x164DF4FB, 0x6E6F52C3), BN_DEF(0x669E1EF1, 0x25E41D2B), BN_DEF(0x3FD59D7C, 0x3C1B20EE), BN_DEF(0xFA53DDEF, 0x0ABCD06B), BN_DEF(0xD5C4484E, 0x1DBF9A42), BN_DEF(0x9B0DEADA, 0xABC52197), BN_DEF(0x22363A0D, 0xE86D2BC5), BN_DEF(0x9C9DF69E, 0x5CAE82AB), BN_DEF(0x71F54BFF, 0x64F2E21E), BN_DEF(0xE2D74DD3, 0xF4FD4452), BN_DEF(0xBC437944, 0xB4130C93), BN_DEF(0x85139270, 0xAEFE1309), BN_DEF(0xC186D91C, 0x598CB0FA), BN_DEF(0x91F7F7EE, 0x7AD91D26), BN_DEF(0xD6E6C907, 0x61B46FC9), BN_DEF(0xF99C0238, 0xBC34F4DE), BN_DEF(0x6519035B, 0xDE355B3B), BN_DEF(0x611FCFDC, 0x886B4238), BN_DEF(0xC1B2EFFA, 0xC6F34A26), BN_DEF(0x7D1683B2, 0xC58EF183), BN_DEF(0x2EC22005, 0x3BB5FCBC), BN_DEF(0x4C6FAD73, 0xC3FE3B1B), BN_DEF(0xEEF28183, 0x8E4F1232), BN_DEF(0xE98583FF, 0x9172FE9C), BN_DEF(0x28342F61, 0xC03404CD), BN_DEF(0xCDF7E2EC, 0x9E02FCE1), BN_DEF(0xEE0A6D70, 0x0B07A7C8), BN_DEF(0x6372BB19, 0xAE56EDE7), BN_DEF(0xDE394DF4, 0x1D4F42A3), BN_DEF(0x60D7F468, 0xB96ADAB7), BN_DEF(0xB2C8E3FB, 0xD108A94B), BN_DEF(0xB324FB61, 0xBC0AB182), BN_DEF(0x483A797A, 0x30ACCA4F), BN_DEF(0x36ADE735, 0x1DF158A1), BN_DEF(0xF3EFE872, 0xE2A689DA), BN_DEF(0xE0E68B77, 0x984F0C70), BN_DEF(0x7F57C935, 0xB557135E), BN_DEF(0x3DED1AF3, 0x85636555), BN_DEF(0x5F066ED0, 0x2433F51F), BN_DEF(0xD5FD6561, 0xD3DF1ED5), BN_DEF(0xAEC4617A, 0xF681B202), BN_DEF(0x630C75D8, 0x7D2FE363), BN_DEF(0x249B3EF9, 0xCC939DCE), BN_DEF(0x146433FB, 0xA9E13641), BN_DEF(0xCE2D3695, 0xD8B9C583), BN_DEF(0x273D3CF1, 0xAFDC5620), BN_DEF(0xA2BB4A9A, 0xADF85458), BN_DEF(0xFFFFFFFF, 0xFFFFFFFF) }; static const BN_ULONG ffdhe6144_q[] = { BN_DEF(0xFFFFFFFF, 0xFFFFFFFF), BN_DEF(0x68720732, 0x5207194E), BN_DEF(0xBC9C6D6A, 0xD20EAB86), BN_DEF(0x6A18B0E0, 0xB1534A93), BN_DEF(0x4D6D8F34, 0x1FEEA547), BN_DEF(0xEE35C06B, 0x2D9DB8FC), BN_DEF(0x63139582, 0xF64E8C08), BN_DEF(0xE5677A01, 0xC66796EA), BN_DEF(0xE4ADC88B, 0x724FA91A), BN_DEF(0xDC2A19C5, 0x282EE416), BN_DEF(0x8AB15423, 0x31149618), BN_DEF(0x3573BFAF, 0x6B9581BA), BN_DEF(0xA316A9C6, 0x7CE4848D), BN_DEF(0x23D33E5F, 0x05746DAC), BN_DEF(0x91308B41, 0x59D39CE0), BN_DEF(0x95140DFB, 0x77556011), BN_DEF(0x3BE57CC9, 0xCA63328F), BN_DEF(0xCA595DE0, 0x3B1F2725), BN_DEF(0x003BECDA, 0xAC3F1C6D), BN_DEF(0x0C1811E1, 0x3FD94FC6), BN_DEF(0xFCF1D137, 0x855F60FF), BN_DEF(0x1A8288F1, 0x50077849), BN_DEF(0x6DB1A06C, 0x5C2A9917), BN_DEF(0xD4D4B488, 0xD29238FB), BN_DEF(0xA67EDA3B, 0x9C40A3FD), BN_DEF(0x2723020F, 0xCD8FAE1F), BN_DEF(0xFE67F638, 0x66D6832B), BN_DEF(0xA6380E1D, 0x59C74619), BN_DEF(0x58E07EA6, 0x48BDEEB2), BN_DEF(0x4DBB1264, 0x1DDA2A19), BN_DEF(0x657A9F53, 0x11DD2221), BN_DEF(0x1C29951D, 0x2733BE96), BN_DEF(0x2281B63D, 0x05FEB25B), BN_DEF(0x2F06EC81, 0xE345003F), BN_DEF(0xFA20C170, 0xA6DAD428), BN_DEF(0x3FC45235, 0xC764DAAD), BN_DEF(0xE764BEE7, 0x054148E6), BN_DEF(0xFCC68566, 0x15276754), BN_DEF(0xB8A0001E, 0x0D0EDC9E), BN_DEF(0x99E5C5BD, 0x0494CCD1), BN_DEF(0xB8D6801C, 0x36E3BC7C), BN_DEF(0x48C09862, 0x5483B005), BN_DEF(0x96CF3419, 0x76B50F00), BN_DEF(0x77DA18C5, 0x389AE443), BN_DEF(0xBF18E63D, 0x43FAADD2), BN_DEF(0xAA81A002, 0x3BB1E78E), BN_DEF(0x6B4FB68C, 0x563EAFA1), BN_DEF(0x72C42BDB, 0xBC9874F2), BN_DEF(0x8B26FA7D, 0xB737A961), BN_DEF(0xB34F0F78, 0x12F20E95), BN_DEF(0x1FEACEBE, 0x9E0D9077), BN_DEF(0xFD29EEF7, 0x055E6835), BN_DEF(0x6AE22427, 0x0EDFCD21), BN_DEF(0xCD86F56D, 0xD5E290CB), BN_DEF(0x911B1D06, 0x743695E2), BN_DEF(0xCE4EFB4F, 0xAE574155), BN_DEF(0x38FAA5FF, 0xB279710F), BN_DEF(0x716BA6E9, 0x7A7EA229), BN_DEF(0xDE21BCA2, 0x5A098649), BN_DEF(0xC289C938, 0x577F0984), BN_DEF(0x60C36C8E, 0x2CC6587D), BN_DEF(0x48FBFBF7, 0xBD6C8E93), BN_DEF(0xEB736483, 0x30DA37E4), BN_DEF(0x7CCE011C, 0xDE1A7A6F), BN_DEF(0xB28C81AD, 0x6F1AAD9D), BN_DEF(0x308FE7EE, 0x4435A11C), BN_DEF(0x60D977FD, 0x6379A513), BN_DEF(0xBE8B41D9, 0xE2C778C1), BN_DEF(0x17611002, 0x9DDAFE5E), BN_DEF(0xA637D6B9, 0xE1FF1D8D), BN_DEF(0x777940C1, 0xC7278919), BN_DEF(0x74C2C1FF, 0xC8B97F4E), BN_DEF(0x941A17B0, 0x601A0266), BN_DEF(0xE6FBF176, 0x4F017E70), BN_DEF(0x770536B8, 0x8583D3E4), BN_DEF(0xB1B95D8C, 0x572B76F3), BN_DEF(0xEF1CA6FA, 0x0EA7A151), BN_DEF(0xB06BFA34, 0xDCB56D5B), BN_DEF(0xD96471FD, 0xE88454A5), BN_DEF(0x59927DB0, 0x5E0558C1), BN_DEF(0xA41D3CBD, 0x98566527), BN_DEF(0x9B56F39A, 0x0EF8AC50), BN_DEF(0x79F7F439, 0xF15344ED), BN_DEF(0x707345BB, 0xCC278638), BN_DEF(0x3FABE49A, 0xDAAB89AF), BN_DEF(0x9EF68D79, 0x42B1B2AA), BN_DEF(0xAF833768, 0x9219FA8F), BN_DEF(0xEAFEB2B0, 0x69EF8F6A), BN_DEF(0x576230BD, 0x7B40D901), BN_DEF(0xB1863AEC, 0xBE97F1B1), BN_DEF(0x124D9F7C, 0xE649CEE7), BN_DEF(0x8A3219FD, 0xD4F09B20), BN_DEF(0xE7169B4A, 0xEC5CE2C1), BN_DEF(0x139E9E78, 0x57EE2B10), BN_DEF(0x515DA54D, 0xD6FC2A2C), BN_DEF(0xFFFFFFFF, 0x7FFFFFFF), }; static const BN_ULONG ffdhe8192_p[] = { BN_DEF(0xFFFFFFFF, 0xFFFFFFFF), BN_DEF(0xC5C6424C, 0xD68C8BB7), BN_DEF(0x838FF88C, 0x011E2A94), BN_DEF(0xA9F4614E, 0x0822E506), BN_DEF(0xF7A8443D, 0x97D11D49), BN_DEF(0x30677F0D, 0xA6BBFDE5), BN_DEF(0xC1FE86FE, 0x2F741EF8), BN_DEF(0x5D71A87E, 0xFAFABE1C), BN_DEF(0xFBE58A30, 0xDED2FBAB), BN_DEF(0x72B0A66E, 0xB6855DFE), BN_DEF(0xBA8A4FE8, 0x1EFC8CE0), BN_DEF(0x3F2FA457, 0x83F81D4A), BN_DEF(0xA577E231, 0xA1FE3075), BN_DEF(0x88D9C0A0, 0xD5B80194), BN_DEF(0xAD9A95F9, 0x624816CD), BN_DEF(0x50C1217B, 0x99E9E316), BN_DEF(0x0E423CFC, 0x51AA691E), BN_DEF(0x3826E52C, 0x1C217E6C), BN_DEF(0x09703FEE, 0x51A8A931), BN_DEF(0x6A460E74, 0xBB709987), BN_DEF(0x9C86B022, 0x541FC68C), BN_DEF(0x46FD8251, 0x59160CC0), BN_DEF(0x35C35F5C, 0x2846C0BA), BN_DEF(0x8B758282, 0x54504AC7), BN_DEF(0xD2AF05E4, 0x29388839), BN_DEF(0xC01BD702, 0xCB2C0F1C), BN_DEF(0x7C932665, 0x555B2F74), BN_DEF(0xA3AB8829, 0x86B63142), BN_DEF(0xF64B10EF, 0x0B8CC3BD), BN_DEF(0xEDD1CC5E, 0x687FEB69), BN_DEF(0xC9509D43, 0xFDB23FCE), BN_DEF(0xD951AE64, 0x1E425A31), BN_DEF(0xF600C838, 0x36AD004C), BN_DEF(0xCFF46AAA, 0xA40E329C), BN_DEF(0x7938DAD4, 0xA41D570D), BN_DEF(0xD43161C1, 0x62A69526), BN_DEF(0x9ADB1E69, 0x3FDD4A8E), BN_DEF(0xDC6B80D6, 0x5B3B71F9), BN_DEF(0xC6272B04, 0xEC9D1810), BN_DEF(0xCACEF403, 0x8CCF2DD5), BN_DEF(0xC95B9117, 0xE49F5235), BN_DEF(0xB854338A, 0x505DC82D), BN_DEF(0x1562A846, 0x62292C31), BN_DEF(0x6AE77F5E, 0xD72B0374), BN_DEF(0x462D538C, 0xF9C9091B), BN_DEF(0x47A67CBE, 0x0AE8DB58), BN_DEF(0x22611682, 0xB3A739C1), BN_DEF(0x2A281BF6, 0xEEAAC023), BN_DEF(0x77CAF992, 0x94C6651E), BN_DEF(0x94B2BBC1, 0x763E4E4B), BN_DEF(0x0077D9B4, 0x587E38DA), BN_DEF(0x183023C3, 0x7FB29F8C), BN_DEF(0xF9E3A26E, 0x0ABEC1FF), BN_DEF(0x350511E3, 0xA00EF092), BN_DEF(0xDB6340D8, 0xB855322E), BN_DEF(0xA9A96910, 0xA52471F7), BN_DEF(0x4CFDB477, 0x388147FB), BN_DEF(0x4E46041F, 0x9B1F5C3E), BN_DEF(0xFCCFEC71, 0xCDAD0657), BN_DEF(0x4C701C3A, 0xB38E8C33), BN_DEF(0xB1C0FD4C, 0x917BDD64), BN_DEF(0x9B7624C8, 0x3BB45432), BN_DEF(0xCAF53EA6, 0x23BA4442), BN_DEF(0x38532A3A, 0x4E677D2C), BN_DEF(0x45036C7A, 0x0BFD64B6), BN_DEF(0x5E0DD902, 0xC68A007E), BN_DEF(0xF44182E1, 0x4DB5A851), BN_DEF(0x7F88A46B, 0x8EC9B55A), BN_DEF(0xCEC97DCF, 0x0A8291CD), BN_DEF(0xF98D0ACC, 0x2A4ECEA9), BN_DEF(0x7140003C, 0x1A1DB93D), BN_DEF(0x33CB8B7A, 0x092999A3), BN_DEF(0x71AD0038, 0x6DC778F9), BN_DEF(0x918130C4, 0xA907600A), BN_DEF(0x2D9E6832, 0xED6A1E01), BN_DEF(0xEFB4318A, 0x7135C886), BN_DEF(0x7E31CC7A, 0x87F55BA5), BN_DEF(0x55034004, 0x7763CF1D), BN_DEF(0xD69F6D18, 0xAC7D5F42), BN_DEF(0xE58857B6, 0x7930E9E4), BN_DEF(0x164DF4FB, 0x6E6F52C3), BN_DEF(0x669E1EF1, 0x25E41D2B), BN_DEF(0x3FD59D7C, 0x3C1B20EE), BN_DEF(0xFA53DDEF, 0x0ABCD06B), BN_DEF(0xD5C4484E, 0x1DBF9A42), BN_DEF(0x9B0DEADA, 0xABC52197), BN_DEF(0x22363A0D, 0xE86D2BC5), BN_DEF(0x9C9DF69E, 0x5CAE82AB), BN_DEF(0x71F54BFF, 0x64F2E21E), BN_DEF(0xE2D74DD3, 0xF4FD4452), BN_DEF(0xBC437944, 0xB4130C93), BN_DEF(0x85139270, 0xAEFE1309), BN_DEF(0xC186D91C, 0x598CB0FA), BN_DEF(0x91F7F7EE, 0x7AD91D26), BN_DEF(0xD6E6C907, 0x61B46FC9), BN_DEF(0xF99C0238, 0xBC34F4DE), BN_DEF(0x6519035B, 0xDE355B3B), BN_DEF(0x611FCFDC, 0x886B4238), BN_DEF(0xC1B2EFFA, 0xC6F34A26), BN_DEF(0x7D1683B2, 0xC58EF183), BN_DEF(0x2EC22005, 0x3BB5FCBC), BN_DEF(0x4C6FAD73, 0xC3FE3B1B), BN_DEF(0xEEF28183, 0x8E4F1232), BN_DEF(0xE98583FF, 0x9172FE9C), BN_DEF(0x28342F61, 0xC03404CD), BN_DEF(0xCDF7E2EC, 0x9E02FCE1), BN_DEF(0xEE0A6D70, 0x0B07A7C8), BN_DEF(0x6372BB19, 0xAE56EDE7), BN_DEF(0xDE394DF4, 0x1D4F42A3), BN_DEF(0x60D7F468, 0xB96ADAB7), BN_DEF(0xB2C8E3FB, 0xD108A94B), BN_DEF(0xB324FB61, 0xBC0AB182), BN_DEF(0x483A797A, 0x30ACCA4F), BN_DEF(0x36ADE735, 0x1DF158A1), BN_DEF(0xF3EFE872, 0xE2A689DA), BN_DEF(0xE0E68B77, 0x984F0C70), BN_DEF(0x7F57C935, 0xB557135E), BN_DEF(0x3DED1AF3, 0x85636555), BN_DEF(0x5F066ED0, 0x2433F51F), BN_DEF(0xD5FD6561, 0xD3DF1ED5), BN_DEF(0xAEC4617A, 0xF681B202), BN_DEF(0x630C75D8, 0x7D2FE363), BN_DEF(0x249B3EF9, 0xCC939DCE), BN_DEF(0x146433FB, 0xA9E13641), BN_DEF(0xCE2D3695, 0xD8B9C583), BN_DEF(0x273D3CF1, 0xAFDC5620), BN_DEF(0xA2BB4A9A, 0xADF85458), BN_DEF(0xFFFFFFFF, 0xFFFFFFFF) }; static const BN_ULONG ffdhe8192_q[] = { BN_DEF(0xFFFFFFFF, 0x7FFFFFFF), BN_DEF(0xE2E32126, 0x6B4645DB), BN_DEF(0x41C7FC46, 0x008F154A), BN_DEF(0x54FA30A7, 0x84117283), BN_DEF(0xFBD4221E, 0xCBE88EA4), BN_DEF(0x9833BF86, 0x535DFEF2), BN_DEF(0x60FF437F, 0x17BA0F7C), BN_DEF(0x2EB8D43F, 0x7D7D5F0E), BN_DEF(0xFDF2C518, 0x6F697DD5), BN_DEF(0x39585337, 0x5B42AEFF), BN_DEF(0x5D4527F4, 0x8F7E4670), BN_DEF(0x1F97D22B, 0xC1FC0EA5), BN_DEF(0xD2BBF118, 0x50FF183A), BN_DEF(0x446CE050, 0xEADC00CA), BN_DEF(0xD6CD4AFC, 0xB1240B66), BN_DEF(0x286090BD, 0x4CF4F18B), BN_DEF(0x07211E7E, 0x28D5348F), BN_DEF(0x1C137296, 0x0E10BF36), BN_DEF(0x84B81FF7, 0x28D45498), BN_DEF(0xB523073A, 0x5DB84CC3), BN_DEF(0x4E435811, 0xAA0FE346), BN_DEF(0x237EC128, 0x2C8B0660), BN_DEF(0x1AE1AFAE, 0x1423605D), BN_DEF(0xC5BAC141, 0x2A282563), BN_DEF(0xE95782F2, 0x149C441C), BN_DEF(0x600DEB81, 0xE596078E), BN_DEF(0x3E499332, 0xAAAD97BA), BN_DEF(0x51D5C414, 0xC35B18A1), BN_DEF(0xFB258877, 0x05C661DE), BN_DEF(0xF6E8E62F, 0xB43FF5B4), BN_DEF(0x64A84EA1, 0x7ED91FE7), BN_DEF(0xECA8D732, 0x0F212D18), BN_DEF(0x7B00641C, 0x1B568026), BN_DEF(0x67FA3555, 0x5207194E), BN_DEF(0xBC9C6D6A, 0xD20EAB86), BN_DEF(0x6A18B0E0, 0xB1534A93), BN_DEF(0x4D6D8F34, 0x1FEEA547), BN_DEF(0xEE35C06B, 0x2D9DB8FC), BN_DEF(0x63139582, 0xF64E8C08), BN_DEF(0xE5677A01, 0xC66796EA), BN_DEF(0xE4ADC88B, 0x724FA91A), BN_DEF(0xDC2A19C5, 0x282EE416), BN_DEF(0x8AB15423, 0x31149618), BN_DEF(0x3573BFAF, 0x6B9581BA), BN_DEF(0xA316A9C6, 0x7CE4848D), BN_DEF(0x23D33E5F, 0x05746DAC), BN_DEF(0x91308B41, 0x59D39CE0), BN_DEF(0x95140DFB, 0x77556011), BN_DEF(0x3BE57CC9, 0xCA63328F), BN_DEF(0xCA595DE0, 0x3B1F2725), BN_DEF(0x003BECDA, 0xAC3F1C6D), BN_DEF(0x0C1811E1, 0x3FD94FC6), BN_DEF(0xFCF1D137, 0x855F60FF), BN_DEF(0x1A8288F1, 0x50077849), BN_DEF(0x6DB1A06C, 0x5C2A9917), BN_DEF(0xD4D4B488, 0xD29238FB), BN_DEF(0xA67EDA3B, 0x9C40A3FD), BN_DEF(0x2723020F, 0xCD8FAE1F), BN_DEF(0xFE67F638, 0x66D6832B), BN_DEF(0xA6380E1D, 0x59C74619), BN_DEF(0x58E07EA6, 0x48BDEEB2), BN_DEF(0x4DBB1264, 0x1DDA2A19), BN_DEF(0x657A9F53, 0x11DD2221), BN_DEF(0x1C29951D, 0x2733BE96), BN_DEF(0x2281B63D, 0x05FEB25B), BN_DEF(0x2F06EC81, 0xE345003F), BN_DEF(0xFA20C170, 0xA6DAD428), BN_DEF(0x3FC45235, 0xC764DAAD), BN_DEF(0xE764BEE7, 0x054148E6), BN_DEF(0xFCC68566, 0x15276754), BN_DEF(0xB8A0001E, 0x0D0EDC9E), BN_DEF(0x99E5C5BD, 0x0494CCD1), BN_DEF(0xB8D6801C, 0x36E3BC7C), BN_DEF(0x48C09862, 0x5483B005), BN_DEF(0x96CF3419, 0x76B50F00), BN_DEF(0x77DA18C5, 0x389AE443), BN_DEF(0xBF18E63D, 0x43FAADD2), BN_DEF(0xAA81A002, 0x3BB1E78E), BN_DEF(0x6B4FB68C, 0x563EAFA1), BN_DEF(0x72C42BDB, 0xBC9874F2), BN_DEF(0x8B26FA7D, 0xB737A961), BN_DEF(0xB34F0F78, 0x12F20E95), BN_DEF(0x1FEACEBE, 0x9E0D9077), BN_DEF(0xFD29EEF7, 0x055E6835), BN_DEF(0x6AE22427, 0x0EDFCD21), BN_DEF(0xCD86F56D, 0xD5E290CB), BN_DEF(0x911B1D06, 0x743695E2), BN_DEF(0xCE4EFB4F, 0xAE574155), BN_DEF(0x38FAA5FF, 0xB279710F), BN_DEF(0x716BA6E9, 0x7A7EA229), BN_DEF(0xDE21BCA2, 0x5A098649), BN_DEF(0xC289C938, 0x577F0984), BN_DEF(0x60C36C8E, 0x2CC6587D), BN_DEF(0x48FBFBF7, 0xBD6C8E93), BN_DEF(0xEB736483, 0x30DA37E4), BN_DEF(0x7CCE011C, 0xDE1A7A6F), BN_DEF(0xB28C81AD, 0x6F1AAD9D), BN_DEF(0x308FE7EE, 0x4435A11C), BN_DEF(0x60D977FD, 0x6379A513), BN_DEF(0xBE8B41D9, 0xE2C778C1), BN_DEF(0x17611002, 0x9DDAFE5E), BN_DEF(0xA637D6B9, 0xE1FF1D8D), BN_DEF(0x777940C1, 0xC7278919), BN_DEF(0x74C2C1FF, 0xC8B97F4E), BN_DEF(0x941A17B0, 0x601A0266), BN_DEF(0xE6FBF176, 0x4F017E70), BN_DEF(0x770536B8, 0x8583D3E4), BN_DEF(0xB1B95D8C, 0x572B76F3), BN_DEF(0xEF1CA6FA, 0x0EA7A151), BN_DEF(0xB06BFA34, 0xDCB56D5B), BN_DEF(0xD96471FD, 0xE88454A5), BN_DEF(0x59927DB0, 0x5E0558C1), BN_DEF(0xA41D3CBD, 0x98566527), BN_DEF(0x9B56F39A, 0x0EF8AC50), BN_DEF(0x79F7F439, 0xF15344ED), BN_DEF(0x707345BB, 0xCC278638), BN_DEF(0x3FABE49A, 0xDAAB89AF), BN_DEF(0x9EF68D79, 0x42B1B2AA), BN_DEF(0xAF833768, 0x9219FA8F), BN_DEF(0xEAFEB2B0, 0x69EF8F6A), BN_DEF(0x576230BD, 0x7B40D901), BN_DEF(0xB1863AEC, 0xBE97F1B1), BN_DEF(0x124D9F7C, 0xE649CEE7), BN_DEF(0x8A3219FD, 0xD4F09B20), BN_DEF(0xE7169B4A, 0xEC5CE2C1), BN_DEF(0x139E9E78, 0x57EE2B10), BN_DEF(0x515DA54D, 0xD6FC2A2C), BN_DEF(0xFFFFFFFF, 0x7FFFFFFF), }; # define make_dh_bn(x) \ extern const BIGNUM ossl_bignum_##x; \ const BIGNUM ossl_bignum_##x = { \ (BN_ULONG *) x, \ OSSL_NELEM(x), \ OSSL_NELEM(x), \ 0, BN_FLG_STATIC_DATA }; static const BN_ULONG value_2 = 2; const BIGNUM ossl_bignum_const_2 = { (BN_ULONG *)&value_2, 1, 1, 0, BN_FLG_STATIC_DATA }; make_dh_bn(dh1024_160_p) make_dh_bn(dh1024_160_q) make_dh_bn(dh1024_160_g) make_dh_bn(dh2048_224_p) make_dh_bn(dh2048_224_q) make_dh_bn(dh2048_224_g) make_dh_bn(dh2048_256_p) make_dh_bn(dh2048_256_q) make_dh_bn(dh2048_256_g) make_dh_bn(ffdhe2048_p) make_dh_bn(ffdhe2048_q) make_dh_bn(ffdhe3072_p) make_dh_bn(ffdhe3072_q) make_dh_bn(ffdhe4096_p) make_dh_bn(ffdhe4096_q) make_dh_bn(ffdhe6144_p) make_dh_bn(ffdhe6144_q) make_dh_bn(ffdhe8192_p) make_dh_bn(ffdhe8192_q) # ifndef FIPS_MODULE make_dh_bn(modp_1536_p) make_dh_bn(modp_1536_q) # endif make_dh_bn(modp_2048_p) make_dh_bn(modp_2048_q) make_dh_bn(modp_3072_p) make_dh_bn(modp_3072_q) make_dh_bn(modp_4096_p) make_dh_bn(modp_4096_q) make_dh_bn(modp_6144_p) make_dh_bn(modp_6144_q) make_dh_bn(modp_8192_p) make_dh_bn(modp_8192_q)
bn
openssl/crypto/bn/bn_dh.c
openssl
#include "internal/cryptlib.h" #include "internal/constant_time.h" #include "bn_local.h" #include <stdlib.h> #ifdef _WIN32 # include <malloc.h> # ifndef alloca # define alloca _alloca # endif #elif defined(__GNUC__) # ifndef alloca # define alloca(s) __builtin_alloca((s)) # endif #elif defined(__sun) # include <alloca.h> #endif #include "rsaz_exp.h" #undef SPARC_T4_MONT #if defined(OPENSSL_BN_ASM_MONT) && (defined(__sparc__) || defined(__sparc)) # include "crypto/sparc_arch.h" # define SPARC_T4_MONT #endif #define TABLE_SIZE 32 #define BN_CONSTTIME_SIZE_LIMIT (INT_MAX / BN_BYTES / 256) int BN_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx) { int i, bits, ret = 0; BIGNUM *v, *rr; if (BN_get_flags(p, BN_FLG_CONSTTIME) != 0 || BN_get_flags(a, BN_FLG_CONSTTIME) != 0) { ERR_raise(ERR_LIB_BN, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); return 0; } BN_CTX_start(ctx); rr = ((r == a) || (r == p)) ? BN_CTX_get(ctx) : r; v = BN_CTX_get(ctx); if (rr == NULL || v == NULL) goto err; if (BN_copy(v, a) == NULL) goto err; bits = BN_num_bits(p); if (BN_is_odd(p)) { if (BN_copy(rr, a) == NULL) goto err; } else { if (!BN_one(rr)) goto err; } for (i = 1; i < bits; i++) { if (!BN_sqr(v, v, ctx)) goto err; if (BN_is_bit_set(p, i)) { if (!BN_mul(rr, rr, v, ctx)) goto err; } } if (r != rr && BN_copy(r, rr) == NULL) goto err; ret = 1; err: BN_CTX_end(ctx); bn_check_top(r); return ret; } int BN_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx) { int ret; bn_check_top(a); bn_check_top(p); bn_check_top(m); #define MONT_MUL_MOD #define MONT_EXP_WORD #define RECP_MUL_MOD #ifdef MONT_MUL_MOD if (BN_is_odd(m)) { # ifdef MONT_EXP_WORD if (a->top == 1 && !a->neg && (BN_get_flags(p, BN_FLG_CONSTTIME) == 0) && (BN_get_flags(a, BN_FLG_CONSTTIME) == 0) && (BN_get_flags(m, BN_FLG_CONSTTIME) == 0)) { BN_ULONG A = a->d[0]; ret = BN_mod_exp_mont_word(r, A, p, m, ctx, NULL); } else # endif ret = BN_mod_exp_mont(r, a, p, m, ctx, NULL); } else #endif #ifdef RECP_MUL_MOD { ret = BN_mod_exp_recp(r, a, p, m, ctx); } #else { ret = BN_mod_exp_simple(r, a, p, m, ctx); } #endif bn_check_top(r); return ret; } int BN_mod_exp_recp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx) { int i, j, bits, ret = 0, wstart, wend, window; int start = 1; BIGNUM *aa; BIGNUM *val[TABLE_SIZE]; BN_RECP_CTX recp; if (BN_get_flags(p, BN_FLG_CONSTTIME) != 0 || BN_get_flags(a, BN_FLG_CONSTTIME) != 0 || BN_get_flags(m, BN_FLG_CONSTTIME) != 0) { ERR_raise(ERR_LIB_BN, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); return 0; } bits = BN_num_bits(p); if (bits == 0) { if (BN_abs_is_word(m, 1)) { ret = 1; BN_zero(r); } else { ret = BN_one(r); } return ret; } BN_RECP_CTX_init(&recp); BN_CTX_start(ctx); aa = BN_CTX_get(ctx); val[0] = BN_CTX_get(ctx); if (val[0] == NULL) goto err; if (m->neg) { if (!BN_copy(aa, m)) goto err; aa->neg = 0; if (BN_RECP_CTX_set(&recp, aa, ctx) <= 0) goto err; } else { if (BN_RECP_CTX_set(&recp, m, ctx) <= 0) goto err; } if (!BN_nnmod(val[0], a, m, ctx)) goto err; if (BN_is_zero(val[0])) { BN_zero(r); ret = 1; goto err; } window = BN_window_bits_for_exponent_size(bits); if (window > 1) { if (!BN_mod_mul_reciprocal(aa, val[0], val[0], &recp, ctx)) goto err; j = 1 << (window - 1); for (i = 1; i < j; i++) { if (((val[i] = BN_CTX_get(ctx)) == NULL) || !BN_mod_mul_reciprocal(val[i], val[i - 1], aa, &recp, ctx)) goto err; } } start = 1; wstart = bits - 1; wend = 0; if (r == p) { BIGNUM *p_dup = BN_CTX_get(ctx); if (p_dup == NULL || BN_copy(p_dup, p) == NULL) goto err; p = p_dup; } if (!BN_one(r)) goto err; for (;;) { int wvalue; if (BN_is_bit_set(p, wstart) == 0) { if (!start) if (!BN_mod_mul_reciprocal(r, r, r, &recp, ctx)) goto err; if (wstart == 0) break; wstart--; continue; } wvalue = 1; wend = 0; for (i = 1; i < window; i++) { if (wstart - i < 0) break; if (BN_is_bit_set(p, wstart - i)) { wvalue <<= (i - wend); wvalue |= 1; wend = i; } } j = wend + 1; if (!start) for (i = 0; i < j; i++) { if (!BN_mod_mul_reciprocal(r, r, r, &recp, ctx)) goto err; } if (!BN_mod_mul_reciprocal(r, r, val[wvalue >> 1], &recp, ctx)) goto err; wstart -= wend + 1; start = 0; if (wstart < 0) break; } ret = 1; err: BN_CTX_end(ctx); BN_RECP_CTX_free(&recp); bn_check_top(r); return ret; } int BN_mod_exp_mont(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont) { int i, j, bits, ret = 0, wstart, wend, window; int start = 1; BIGNUM *d, *r; const BIGNUM *aa; BIGNUM *val[TABLE_SIZE]; BN_MONT_CTX *mont = NULL; bn_check_top(a); bn_check_top(p); bn_check_top(m); if (!BN_is_odd(m)) { ERR_raise(ERR_LIB_BN, BN_R_CALLED_WITH_EVEN_MODULUS); return 0; } if (m->top <= BN_CONSTTIME_SIZE_LIMIT && (BN_get_flags(p, BN_FLG_CONSTTIME) != 0 || BN_get_flags(a, BN_FLG_CONSTTIME) != 0 || BN_get_flags(m, BN_FLG_CONSTTIME) != 0)) { return BN_mod_exp_mont_consttime(rr, a, p, m, ctx, in_mont); } bits = BN_num_bits(p); if (bits == 0) { if (BN_abs_is_word(m, 1)) { ret = 1; BN_zero(rr); } else { ret = BN_one(rr); } return ret; } BN_CTX_start(ctx); d = BN_CTX_get(ctx); r = BN_CTX_get(ctx); val[0] = BN_CTX_get(ctx); if (val[0] == NULL) goto err; if (in_mont != NULL) mont = in_mont; else { if ((mont = BN_MONT_CTX_new()) == NULL) goto err; if (!BN_MONT_CTX_set(mont, m, ctx)) goto err; } if (a->neg || BN_ucmp(a, m) >= 0) { if (!BN_nnmod(val[0], a, m, ctx)) goto err; aa = val[0]; } else aa = a; if (!bn_to_mont_fixed_top(val[0], aa, mont, ctx)) goto err; window = BN_window_bits_for_exponent_size(bits); if (window > 1) { if (!bn_mul_mont_fixed_top(d, val[0], val[0], mont, ctx)) goto err; j = 1 << (window - 1); for (i = 1; i < j; i++) { if (((val[i] = BN_CTX_get(ctx)) == NULL) || !bn_mul_mont_fixed_top(val[i], val[i - 1], d, mont, ctx)) goto err; } } start = 1; wstart = bits - 1; wend = 0; #if 1 j = m->top; if (m->d[j - 1] & (((BN_ULONG)1) << (BN_BITS2 - 1))) { if (bn_wexpand(r, j) == NULL) goto err; r->d[0] = (0 - m->d[0]) & BN_MASK2; for (i = 1; i < j; i++) r->d[i] = (~m->d[i]) & BN_MASK2; r->top = j; r->flags |= BN_FLG_FIXED_TOP; } else #endif if (!bn_to_mont_fixed_top(r, BN_value_one(), mont, ctx)) goto err; for (;;) { int wvalue; if (BN_is_bit_set(p, wstart) == 0) { if (!start) { if (!bn_mul_mont_fixed_top(r, r, r, mont, ctx)) goto err; } if (wstart == 0) break; wstart--; continue; } wvalue = 1; wend = 0; for (i = 1; i < window; i++) { if (wstart - i < 0) break; if (BN_is_bit_set(p, wstart - i)) { wvalue <<= (i - wend); wvalue |= 1; wend = i; } } j = wend + 1; if (!start) for (i = 0; i < j; i++) { if (!bn_mul_mont_fixed_top(r, r, r, mont, ctx)) goto err; } if (!bn_mul_mont_fixed_top(r, r, val[wvalue >> 1], mont, ctx)) goto err; wstart -= wend + 1; start = 0; if (wstart < 0) break; } #if defined(SPARC_T4_MONT) if (OPENSSL_sparcv9cap_P[0] & (SPARCV9_VIS3 | SPARCV9_PREFER_FPU)) { j = mont->N.top; val[0]->d[0] = 1; for (i = 1; i < j; i++) val[0]->d[i] = 0; val[0]->top = j; if (!BN_mod_mul_montgomery(rr, r, val[0], mont, ctx)) goto err; } else #endif if (!BN_from_montgomery(rr, r, mont, ctx)) goto err; ret = 1; err: if (in_mont == NULL) BN_MONT_CTX_free(mont); BN_CTX_end(ctx); bn_check_top(rr); return ret; } static BN_ULONG bn_get_bits(const BIGNUM *a, int bitpos) { BN_ULONG ret = 0; int wordpos; wordpos = bitpos / BN_BITS2; bitpos %= BN_BITS2; if (wordpos >= 0 && wordpos < a->top) { ret = a->d[wordpos] & BN_MASK2; if (bitpos) { ret >>= bitpos; if (++wordpos < a->top) ret |= a->d[wordpos] << (BN_BITS2 - bitpos); } } return ret & BN_MASK2; } static int MOD_EXP_CTIME_COPY_TO_PREBUF(const BIGNUM *b, int top, unsigned char *buf, int idx, int window) { int i, j; int width = 1 << window; BN_ULONG *table = (BN_ULONG *)buf; if (top > b->top) top = b->top; for (i = 0, j = idx; i < top; i++, j += width) { table[j] = b->d[i]; } return 1; } static int MOD_EXP_CTIME_COPY_FROM_PREBUF(BIGNUM *b, int top, unsigned char *buf, int idx, int window) { int i, j; int width = 1 << window; volatile BN_ULONG *table = (volatile BN_ULONG *)buf; if (bn_wexpand(b, top) == NULL) return 0; if (window <= 3) { for (i = 0; i < top; i++, table += width) { BN_ULONG acc = 0; for (j = 0; j < width; j++) { acc |= table[j] & ((BN_ULONG)0 - (constant_time_eq_int(j,idx)&1)); } b->d[i] = acc; } } else { int xstride = 1 << (window - 2); BN_ULONG y0, y1, y2, y3; i = idx >> (window - 2); idx &= xstride - 1; y0 = (BN_ULONG)0 - (constant_time_eq_int(i,0)&1); y1 = (BN_ULONG)0 - (constant_time_eq_int(i,1)&1); y2 = (BN_ULONG)0 - (constant_time_eq_int(i,2)&1); y3 = (BN_ULONG)0 - (constant_time_eq_int(i,3)&1); for (i = 0; i < top; i++, table += width) { BN_ULONG acc = 0; for (j = 0; j < xstride; j++) { acc |= ( (table[j + 0 * xstride] & y0) | (table[j + 1 * xstride] & y1) | (table[j + 2 * xstride] & y2) | (table[j + 3 * xstride] & y3) ) & ((BN_ULONG)0 - (constant_time_eq_int(j,idx)&1)); } b->d[i] = acc; } } b->top = top; b->flags |= BN_FLG_FIXED_TOP; return 1; } #define MOD_EXP_CTIME_ALIGN(x_) \ ((unsigned char*)(x_) + (MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH - (((size_t)(x_)) & (MOD_EXP_CTIME_MIN_CACHE_LINE_MASK)))) int BN_mod_exp_mont_consttime(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont) { int i, bits, ret = 0, window, wvalue, wmask, window0; int top; BN_MONT_CTX *mont = NULL; int numPowers; unsigned char *powerbufFree = NULL; int powerbufLen = 0; unsigned char *powerbuf = NULL; BIGNUM tmp, am; #if defined(SPARC_T4_MONT) unsigned int t4 = 0; #endif bn_check_top(a); bn_check_top(p); bn_check_top(m); if (!BN_is_odd(m)) { ERR_raise(ERR_LIB_BN, BN_R_CALLED_WITH_EVEN_MODULUS); return 0; } top = m->top; if (top > BN_CONSTTIME_SIZE_LIMIT) { return BN_mod_exp_mont(rr, a, p, m, ctx, in_mont); } bits = p->top * BN_BITS2; if (bits == 0) { if (BN_abs_is_word(m, 1)) { ret = 1; BN_zero(rr); } else { ret = BN_one(rr); } return ret; } BN_CTX_start(ctx); if (in_mont != NULL) mont = in_mont; else { if ((mont = BN_MONT_CTX_new()) == NULL) goto err; if (!BN_MONT_CTX_set(mont, m, ctx)) goto err; } if (a->neg || BN_ucmp(a, m) >= 0) { BIGNUM *reduced = BN_CTX_get(ctx); if (reduced == NULL || !BN_nnmod(reduced, a, m, ctx)) { goto err; } a = reduced; } #ifdef RSAZ_ENABLED if ((16 == a->top) && (16 == p->top) && (BN_num_bits(m) == 1024) && rsaz_avx2_eligible()) { if (NULL == bn_wexpand(rr, 16)) goto err; RSAZ_1024_mod_exp_avx2(rr->d, a->d, p->d, m->d, mont->RR.d, mont->n0[0]); rr->top = 16; rr->neg = 0; bn_correct_top(rr); ret = 1; goto err; } else if ((8 == a->top) && (8 == p->top) && (BN_num_bits(m) == 512)) { if (NULL == bn_wexpand(rr, 8)) goto err; RSAZ_512_mod_exp(rr->d, a->d, p->d, m->d, mont->n0[0], mont->RR.d); rr->top = 8; rr->neg = 0; bn_correct_top(rr); ret = 1; goto err; } #endif window = BN_window_bits_for_ctime_exponent_size(bits); #if defined(SPARC_T4_MONT) if (window >= 5 && (top & 15) == 0 && top <= 64 && (OPENSSL_sparcv9cap_P[1] & (CFR_MONTMUL | CFR_MONTSQR)) == (CFR_MONTMUL | CFR_MONTSQR) && (t4 = OPENSSL_sparcv9cap_P[0])) window = 5; else #endif #if defined(OPENSSL_BN_ASM_MONT5) if (window >= 5 && top <= BN_SOFT_LIMIT) { window = 5; powerbufLen += top * sizeof(mont->N.d[0]); } #endif (void)0; numPowers = 1 << window; powerbufLen += sizeof(m->d[0]) * (top * numPowers + ((2 * top) > numPowers ? (2 * top) : numPowers)); #ifdef alloca if (powerbufLen < 3072) powerbufFree = alloca(powerbufLen + MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH); else #endif if ((powerbufFree = OPENSSL_malloc(powerbufLen + MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH)) == NULL) goto err; powerbuf = MOD_EXP_CTIME_ALIGN(powerbufFree); memset(powerbuf, 0, powerbufLen); #ifdef alloca if (powerbufLen < 3072) powerbufFree = NULL; #endif tmp.d = (BN_ULONG *)(powerbuf + sizeof(m->d[0]) * top * numPowers); am.d = tmp.d + top; tmp.top = am.top = 0; tmp.dmax = am.dmax = top; tmp.neg = am.neg = 0; tmp.flags = am.flags = BN_FLG_STATIC_DATA; #if 1 if (m->d[top - 1] & (((BN_ULONG)1) << (BN_BITS2 - 1))) { tmp.d[0] = (0 - m->d[0]) & BN_MASK2; for (i = 1; i < top; i++) tmp.d[i] = (~m->d[i]) & BN_MASK2; tmp.top = top; } else #endif if (!bn_to_mont_fixed_top(&tmp, BN_value_one(), mont, ctx)) goto err; if (!bn_to_mont_fixed_top(&am, a, mont, ctx)) goto err; if (top > BN_SOFT_LIMIT) goto fallback; #if defined(SPARC_T4_MONT) if (t4) { typedef int (*bn_pwr5_mont_f) (BN_ULONG *tp, const BN_ULONG *np, const BN_ULONG *n0, const void *table, int power, int bits); int bn_pwr5_mont_t4_8(BN_ULONG *tp, const BN_ULONG *np, const BN_ULONG *n0, const void *table, int power, int bits); int bn_pwr5_mont_t4_16(BN_ULONG *tp, const BN_ULONG *np, const BN_ULONG *n0, const void *table, int power, int bits); int bn_pwr5_mont_t4_24(BN_ULONG *tp, const BN_ULONG *np, const BN_ULONG *n0, const void *table, int power, int bits); int bn_pwr5_mont_t4_32(BN_ULONG *tp, const BN_ULONG *np, const BN_ULONG *n0, const void *table, int power, int bits); static const bn_pwr5_mont_f pwr5_funcs[4] = { bn_pwr5_mont_t4_8, bn_pwr5_mont_t4_16, bn_pwr5_mont_t4_24, bn_pwr5_mont_t4_32 }; bn_pwr5_mont_f pwr5_worker = pwr5_funcs[top / 16 - 1]; typedef int (*bn_mul_mont_f) (BN_ULONG *rp, const BN_ULONG *ap, const void *bp, const BN_ULONG *np, const BN_ULONG *n0); int bn_mul_mont_t4_8(BN_ULONG *rp, const BN_ULONG *ap, const void *bp, const BN_ULONG *np, const BN_ULONG *n0); int bn_mul_mont_t4_16(BN_ULONG *rp, const BN_ULONG *ap, const void *bp, const BN_ULONG *np, const BN_ULONG *n0); int bn_mul_mont_t4_24(BN_ULONG *rp, const BN_ULONG *ap, const void *bp, const BN_ULONG *np, const BN_ULONG *n0); int bn_mul_mont_t4_32(BN_ULONG *rp, const BN_ULONG *ap, const void *bp, const BN_ULONG *np, const BN_ULONG *n0); static const bn_mul_mont_f mul_funcs[4] = { bn_mul_mont_t4_8, bn_mul_mont_t4_16, bn_mul_mont_t4_24, bn_mul_mont_t4_32 }; bn_mul_mont_f mul_worker = mul_funcs[top / 16 - 1]; void bn_mul_mont_vis3(BN_ULONG *rp, const BN_ULONG *ap, const void *bp, const BN_ULONG *np, const BN_ULONG *n0, int num); void bn_mul_mont_t4(BN_ULONG *rp, const BN_ULONG *ap, const void *bp, const BN_ULONG *np, const BN_ULONG *n0, int num); void bn_mul_mont_gather5_t4(BN_ULONG *rp, const BN_ULONG *ap, const void *table, const BN_ULONG *np, const BN_ULONG *n0, int num, int power); void bn_flip_n_scatter5_t4(const BN_ULONG *inp, size_t num, void *table, size_t power); void bn_gather5_t4(BN_ULONG *out, size_t num, void *table, size_t power); void bn_flip_t4(BN_ULONG *dst, BN_ULONG *src, size_t num); BN_ULONG *np = mont->N.d, *n0 = mont->n0; int stride = 5 * (6 - (top / 16 - 1)); for (i = am.top; i < top; i++) am.d[i] = 0; for (i = tmp.top; i < top; i++) tmp.d[i] = 0; bn_flip_n_scatter5_t4(tmp.d, top, powerbuf, 0); bn_flip_n_scatter5_t4(am.d, top, powerbuf, 1); if (!(*mul_worker) (tmp.d, am.d, am.d, np, n0) && !(*mul_worker) (tmp.d, am.d, am.d, np, n0)) bn_mul_mont_vis3(tmp.d, am.d, am.d, np, n0, top); bn_flip_n_scatter5_t4(tmp.d, top, powerbuf, 2); for (i = 3; i < 32; i++) { if (!(*mul_worker) (tmp.d, tmp.d, am.d, np, n0) && !(*mul_worker) (tmp.d, tmp.d, am.d, np, n0)) bn_mul_mont_vis3(tmp.d, tmp.d, am.d, np, n0, top); bn_flip_n_scatter5_t4(tmp.d, top, powerbuf, i); } np = alloca(top * sizeof(BN_ULONG)); top /= 2; bn_flip_t4(np, mont->N.d, top); window0 = (bits - 1) % 5 + 1; wmask = (1 << window0) - 1; bits -= window0; wvalue = bn_get_bits(p, bits) & wmask; bn_gather5_t4(tmp.d, top, powerbuf, wvalue); while (bits > 0) { if (bits < stride) stride = bits; bits -= stride; wvalue = bn_get_bits(p, bits); if ((*pwr5_worker) (tmp.d, np, n0, powerbuf, wvalue, stride)) continue; if ((*pwr5_worker) (tmp.d, np, n0, powerbuf, wvalue, stride)) continue; bits += stride - 5; wvalue >>= stride - 5; wvalue &= 31; bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top); bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top); bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top); bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top); bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top); bn_mul_mont_gather5_t4(tmp.d, tmp.d, powerbuf, np, n0, top, wvalue); } bn_flip_t4(tmp.d, tmp.d, top); top *= 2; tmp.top = top; bn_correct_top(&tmp); OPENSSL_cleanse(np, top * sizeof(BN_ULONG)); } else #endif #if defined(OPENSSL_BN_ASM_MONT5) if (window == 5 && top > 1) { void bn_mul_mont_gather5(BN_ULONG *rp, const BN_ULONG *ap, const void *table, const BN_ULONG *np, const BN_ULONG *n0, int num, int power); void bn_scatter5(const BN_ULONG *inp, size_t num, void *table, size_t power); void bn_gather5(BN_ULONG *out, size_t num, void *table, size_t power); void bn_power5(BN_ULONG *rp, const BN_ULONG *ap, const void *table, const BN_ULONG *np, const BN_ULONG *n0, int num, int power); int bn_get_bits5(const BN_ULONG *ap, int off); BN_ULONG *n0 = mont->n0, *np; for (i = am.top; i < top; i++) am.d[i] = 0; for (i = tmp.top; i < top; i++) tmp.d[i] = 0; for (np = am.d + top, i = 0; i < top; i++) np[i] = mont->N.d[i]; bn_scatter5(tmp.d, top, powerbuf, 0); bn_scatter5(am.d, am.top, powerbuf, 1); bn_mul_mont(tmp.d, am.d, am.d, np, n0, top); bn_scatter5(tmp.d, top, powerbuf, 2); # if 0 for (i = 3; i < 32; i++) { bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1); bn_scatter5(tmp.d, top, powerbuf, i); } # else for (i = 4; i < 32; i *= 2) { bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top); bn_scatter5(tmp.d, top, powerbuf, i); } for (i = 3; i < 8; i += 2) { int j; bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1); bn_scatter5(tmp.d, top, powerbuf, i); for (j = 2 * i; j < 32; j *= 2) { bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top); bn_scatter5(tmp.d, top, powerbuf, j); } } for (; i < 16; i += 2) { bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1); bn_scatter5(tmp.d, top, powerbuf, i); bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top); bn_scatter5(tmp.d, top, powerbuf, 2 * i); } for (; i < 32; i += 2) { bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1); bn_scatter5(tmp.d, top, powerbuf, i); } # endif window0 = (bits - 1) % 5 + 1; wmask = (1 << window0) - 1; bits -= window0; wvalue = bn_get_bits(p, bits) & wmask; bn_gather5(tmp.d, top, powerbuf, wvalue); if (top & 7) { while (bits > 0) { bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top); bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top); bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top); bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top); bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top); bn_mul_mont_gather5(tmp.d, tmp.d, powerbuf, np, n0, top, bn_get_bits5(p->d, bits -= 5)); } } else { while (bits > 0) { bn_power5(tmp.d, tmp.d, powerbuf, np, n0, top, bn_get_bits5(p->d, bits -= 5)); } } tmp.top = top; } else #endif { fallback: if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&tmp, top, powerbuf, 0, window)) goto err; if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&am, top, powerbuf, 1, window)) goto err; if (window > 1) { if (!bn_mul_mont_fixed_top(&tmp, &am, &am, mont, ctx)) goto err; if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&tmp, top, powerbuf, 2, window)) goto err; for (i = 3; i < numPowers; i++) { if (!bn_mul_mont_fixed_top(&tmp, &am, &tmp, mont, ctx)) goto err; if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&tmp, top, powerbuf, i, window)) goto err; } } window0 = (bits - 1) % window + 1; wmask = (1 << window0) - 1; bits -= window0; wvalue = bn_get_bits(p, bits) & wmask; if (!MOD_EXP_CTIME_COPY_FROM_PREBUF(&tmp, top, powerbuf, wvalue, window)) goto err; wmask = (1 << window) - 1; while (bits > 0) { for (i = 0; i < window; i++) if (!bn_mul_mont_fixed_top(&tmp, &tmp, &tmp, mont, ctx)) goto err; bits -= window; wvalue = bn_get_bits(p, bits) & wmask; if (!MOD_EXP_CTIME_COPY_FROM_PREBUF(&am, top, powerbuf, wvalue, window)) goto err; if (!bn_mul_mont_fixed_top(&tmp, &tmp, &am, mont, ctx)) goto err; } } #if defined(SPARC_T4_MONT) if (OPENSSL_sparcv9cap_P[0] & (SPARCV9_VIS3 | SPARCV9_PREFER_FPU)) { am.d[0] = 1; for (i = 1; i < top; i++) am.d[i] = 0; if (!BN_mod_mul_montgomery(rr, &tmp, &am, mont, ctx)) goto err; } else #endif if (!BN_from_montgomery(rr, &tmp, mont, ctx)) goto err; ret = 1; err: if (in_mont == NULL) BN_MONT_CTX_free(mont); if (powerbuf != NULL) { OPENSSL_cleanse(powerbuf, powerbufLen); OPENSSL_free(powerbufFree); } BN_CTX_end(ctx); return ret; } int BN_mod_exp_mont_word(BIGNUM *rr, BN_ULONG a, const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont) { BN_MONT_CTX *mont = NULL; int b, bits, ret = 0; int r_is_one; BN_ULONG w, next_w; BIGNUM *r, *t; BIGNUM *swap_tmp; #define BN_MOD_MUL_WORD(r, w, m) \ (BN_mul_word(r, (w)) && \ ( \ (BN_mod(t, r, m, ctx) && (swap_tmp = r, r = t, t = swap_tmp, 1)))) #define BN_TO_MONTGOMERY_WORD(r, w, mont) \ (BN_set_word(r, (w)) && BN_to_montgomery(r, r, (mont), ctx)) if (BN_get_flags(p, BN_FLG_CONSTTIME) != 0 || BN_get_flags(m, BN_FLG_CONSTTIME) != 0) { ERR_raise(ERR_LIB_BN, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); return 0; } bn_check_top(p); bn_check_top(m); if (!BN_is_odd(m)) { ERR_raise(ERR_LIB_BN, BN_R_CALLED_WITH_EVEN_MODULUS); return 0; } if (m->top == 1) a %= m->d[0]; bits = BN_num_bits(p); if (bits == 0) { if (BN_abs_is_word(m, 1)) { ret = 1; BN_zero(rr); } else { ret = BN_one(rr); } return ret; } if (a == 0) { BN_zero(rr); ret = 1; return ret; } BN_CTX_start(ctx); r = BN_CTX_get(ctx); t = BN_CTX_get(ctx); if (t == NULL) goto err; if (in_mont != NULL) mont = in_mont; else { if ((mont = BN_MONT_CTX_new()) == NULL) goto err; if (!BN_MONT_CTX_set(mont, m, ctx)) goto err; } r_is_one = 1; w = a; for (b = bits - 2; b >= 0; b--) { next_w = w * w; if ((next_w / w) != w) { if (r_is_one) { if (!BN_TO_MONTGOMERY_WORD(r, w, mont)) goto err; r_is_one = 0; } else { if (!BN_MOD_MUL_WORD(r, w, m)) goto err; } next_w = 1; } w = next_w; if (!r_is_one) { if (!BN_mod_mul_montgomery(r, r, r, mont, ctx)) goto err; } if (BN_is_bit_set(p, b)) { next_w = w * a; if ((next_w / a) != w) { if (r_is_one) { if (!BN_TO_MONTGOMERY_WORD(r, w, mont)) goto err; r_is_one = 0; } else { if (!BN_MOD_MUL_WORD(r, w, m)) goto err; } next_w = a; } w = next_w; } } if (w != 1) { if (r_is_one) { if (!BN_TO_MONTGOMERY_WORD(r, w, mont)) goto err; r_is_one = 0; } else { if (!BN_MOD_MUL_WORD(r, w, m)) goto err; } } if (r_is_one) { if (!BN_one(rr)) goto err; } else { if (!BN_from_montgomery(rr, r, mont, ctx)) goto err; } ret = 1; err: if (in_mont == NULL) BN_MONT_CTX_free(mont); BN_CTX_end(ctx); bn_check_top(rr); return ret; } int BN_mod_exp_simple(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx) { int i, j, bits, ret = 0, wstart, wend, window; int start = 1; BIGNUM *d; BIGNUM *val[TABLE_SIZE]; if (BN_get_flags(p, BN_FLG_CONSTTIME) != 0 || BN_get_flags(a, BN_FLG_CONSTTIME) != 0 || BN_get_flags(m, BN_FLG_CONSTTIME) != 0) { ERR_raise(ERR_LIB_BN, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); return 0; } if (r == m) { ERR_raise(ERR_LIB_BN, ERR_R_PASSED_INVALID_ARGUMENT); return 0; } bits = BN_num_bits(p); if (bits == 0) { if (BN_abs_is_word(m, 1)) { ret = 1; BN_zero(r); } else { ret = BN_one(r); } return ret; } BN_CTX_start(ctx); d = BN_CTX_get(ctx); val[0] = BN_CTX_get(ctx); if (val[0] == NULL) goto err; if (!BN_nnmod(val[0], a, m, ctx)) goto err; if (BN_is_zero(val[0])) { BN_zero(r); ret = 1; goto err; } window = BN_window_bits_for_exponent_size(bits); if (window > 1) { if (!BN_mod_mul(d, val[0], val[0], m, ctx)) goto err; j = 1 << (window - 1); for (i = 1; i < j; i++) { if (((val[i] = BN_CTX_get(ctx)) == NULL) || !BN_mod_mul(val[i], val[i - 1], d, m, ctx)) goto err; } } start = 1; wstart = bits - 1; wend = 0; if (r == p) { BIGNUM *p_dup = BN_CTX_get(ctx); if (p_dup == NULL || BN_copy(p_dup, p) == NULL) goto err; p = p_dup; } if (!BN_one(r)) goto err; for (;;) { int wvalue; if (BN_is_bit_set(p, wstart) == 0) { if (!start) if (!BN_mod_mul(r, r, r, m, ctx)) goto err; if (wstart == 0) break; wstart--; continue; } wvalue = 1; wend = 0; for (i = 1; i < window; i++) { if (wstart - i < 0) break; if (BN_is_bit_set(p, wstart - i)) { wvalue <<= (i - wend); wvalue |= 1; wend = i; } } j = wend + 1; if (!start) for (i = 0; i < j; i++) { if (!BN_mod_mul(r, r, r, m, ctx)) goto err; } if (!BN_mod_mul(r, r, val[wvalue >> 1], m, ctx)) goto err; wstart -= wend + 1; start = 0; if (wstart < 0) break; } ret = 1; err: BN_CTX_end(ctx); bn_check_top(r); return ret; } int BN_mod_exp_mont_consttime_x2(BIGNUM *rr1, const BIGNUM *a1, const BIGNUM *p1, const BIGNUM *m1, BN_MONT_CTX *in_mont1, BIGNUM *rr2, const BIGNUM *a2, const BIGNUM *p2, const BIGNUM *m2, BN_MONT_CTX *in_mont2, BN_CTX *ctx) { int ret = 0; #ifdef RSAZ_ENABLED BN_MONT_CTX *mont1 = NULL; BN_MONT_CTX *mont2 = NULL; if (ossl_rsaz_avx512ifma_eligible() && (((a1->top == 16) && (p1->top == 16) && (BN_num_bits(m1) == 1024) && (a2->top == 16) && (p2->top == 16) && (BN_num_bits(m2) == 1024)) || ((a1->top == 24) && (p1->top == 24) && (BN_num_bits(m1) == 1536) && (a2->top == 24) && (p2->top == 24) && (BN_num_bits(m2) == 1536)) || ((a1->top == 32) && (p1->top == 32) && (BN_num_bits(m1) == 2048) && (a2->top == 32) && (p2->top == 32) && (BN_num_bits(m2) == 2048)))) { int topn = a1->top; int mod_bits = BN_num_bits(m1); if (bn_wexpand(rr1, topn) == NULL) goto err; if (bn_wexpand(rr2, topn) == NULL) goto err; if (in_mont1 != NULL) { mont1 = in_mont1; } else { if ((mont1 = BN_MONT_CTX_new()) == NULL) goto err; if (!BN_MONT_CTX_set(mont1, m1, ctx)) goto err; } if (in_mont2 != NULL) { mont2 = in_mont2; } else { if ((mont2 = BN_MONT_CTX_new()) == NULL) goto err; if (!BN_MONT_CTX_set(mont2, m2, ctx)) goto err; } ret = ossl_rsaz_mod_exp_avx512_x2(rr1->d, a1->d, p1->d, m1->d, mont1->RR.d, mont1->n0[0], rr2->d, a2->d, p2->d, m2->d, mont2->RR.d, mont2->n0[0], mod_bits); rr1->top = topn; rr1->neg = 0; bn_correct_top(rr1); bn_check_top(rr1); rr2->top = topn; rr2->neg = 0; bn_correct_top(rr2); bn_check_top(rr2); goto err; } #endif ret = BN_mod_exp_mont_consttime(rr1, a1, p1, m1, ctx, in_mont1); ret &= BN_mod_exp_mont_consttime(rr2, a2, p2, m2, ctx, in_mont2); #ifdef RSAZ_ENABLED err: if (in_mont2 == NULL) BN_MONT_CTX_free(mont2); if (in_mont1 == NULL) BN_MONT_CTX_free(mont1); #endif return ret; }
bn
openssl/crypto/bn/bn_exp.c
openssl
#include <stdlib.h> #include <openssl/bn.h> #include "internal/cryptlib.h" #include "crypto/sparc_arch.h" #include "bn_local.h" int bn_mul_mont(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp, const BN_ULONG *np, const BN_ULONG *n0, int num) { int bn_mul_mont_vis3(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp, const BN_ULONG *np, const BN_ULONG *n0, int num); int bn_mul_mont_fpu(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp, const BN_ULONG *np, const BN_ULONG *n0, int num); int bn_mul_mont_int(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp, const BN_ULONG *np, const BN_ULONG *n0, int num); if (!(num & 1) && num >= 6) { if ((num & 15) == 0 && num <= 64 && (OPENSSL_sparcv9cap_P[1] & (CFR_MONTMUL | CFR_MONTSQR)) == (CFR_MONTMUL | CFR_MONTSQR)) { typedef int (*bn_mul_mont_f) (BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp, const BN_ULONG *np, const BN_ULONG *n0); int bn_mul_mont_t4_8(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp, const BN_ULONG *np, const BN_ULONG *n0); int bn_mul_mont_t4_16(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp, const BN_ULONG *np, const BN_ULONG *n0); int bn_mul_mont_t4_24(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp, const BN_ULONG *np, const BN_ULONG *n0); int bn_mul_mont_t4_32(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp, const BN_ULONG *np, const BN_ULONG *n0); static const bn_mul_mont_f funcs[4] = { bn_mul_mont_t4_8, bn_mul_mont_t4_16, bn_mul_mont_t4_24, bn_mul_mont_t4_32 }; bn_mul_mont_f worker = funcs[num / 16 - 1]; if ((*worker) (rp, ap, bp, np, n0)) return 1; if ((*worker) (rp, ap, bp, np, n0)) return 1; return bn_mul_mont_vis3(rp, ap, bp, np, n0, num); } if ((OPENSSL_sparcv9cap_P[0] & SPARCV9_VIS3)) return bn_mul_mont_vis3(rp, ap, bp, np, n0, num); else if (num >= 8 && ( (OPENSSL_sparcv9cap_P[0] & SPARCV9_FMADD) || (OPENSSL_sparcv9cap_P[0] & (SPARCV9_PREFER_FPU | SPARCV9_VIS1)) == (SPARCV9_PREFER_FPU | SPARCV9_VIS1) )) return bn_mul_mont_fpu(rp, ap, bp, np, n0, num); } return bn_mul_mont_int(rp, ap, bp, np, n0, num); }
bn
openssl/crypto/bn/bn_sparc.c
openssl
#include "internal/cryptlib.h" #include "bn_local.h" #define MONT_WORD #ifdef MONT_WORD static int bn_from_montgomery_word(BIGNUM *ret, BIGNUM *r, BN_MONT_CTX *mont); #endif int BN_mod_mul_montgomery(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_MONT_CTX *mont, BN_CTX *ctx) { int ret = bn_mul_mont_fixed_top(r, a, b, mont, ctx); bn_correct_top(r); bn_check_top(r); return ret; } int bn_mul_mont_fixed_top(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_MONT_CTX *mont, BN_CTX *ctx) { BIGNUM *tmp; int ret = 0; int num = mont->N.top; #if defined(OPENSSL_BN_ASM_MONT) && defined(MONT_WORD) if (num > 1 && num <= BN_SOFT_LIMIT && a->top == num && b->top == num) { if (bn_wexpand(r, num) == NULL) return 0; if (bn_mul_mont(r->d, a->d, b->d, mont->N.d, mont->n0, num)) { r->neg = a->neg ^ b->neg; r->top = num; r->flags |= BN_FLG_FIXED_TOP; return 1; } } #endif if ((a->top + b->top) > 2 * num) return 0; BN_CTX_start(ctx); tmp = BN_CTX_get(ctx); if (tmp == NULL) goto err; bn_check_top(tmp); if (a == b) { if (!bn_sqr_fixed_top(tmp, a, ctx)) goto err; } else { if (!bn_mul_fixed_top(tmp, a, b, ctx)) goto err; } #ifdef MONT_WORD if (!bn_from_montgomery_word(r, tmp, mont)) goto err; #else if (!BN_from_montgomery(r, tmp, mont, ctx)) goto err; #endif ret = 1; err: BN_CTX_end(ctx); return ret; } #ifdef MONT_WORD static int bn_from_montgomery_word(BIGNUM *ret, BIGNUM *r, BN_MONT_CTX *mont) { BIGNUM *n; BN_ULONG *ap, *np, *rp, n0, v, carry; int nl, max, i; unsigned int rtop; n = &(mont->N); nl = n->top; if (nl == 0) { ret->top = 0; return 1; } max = (2 * nl); if (bn_wexpand(r, max) == NULL) return 0; r->neg ^= n->neg; np = n->d; rp = r->d; for (rtop = r->top, i = 0; i < max; i++) { v = (BN_ULONG)0 - ((i - rtop) >> (8 * sizeof(rtop) - 1)); rp[i] &= v; } r->top = max; r->flags |= BN_FLG_FIXED_TOP; n0 = mont->n0[0]; for (carry = 0, i = 0; i < nl; i++, rp++) { v = bn_mul_add_words(rp, np, nl, (rp[0] * n0) & BN_MASK2); v = (v + carry + rp[nl]) & BN_MASK2; carry |= (v != rp[nl]); carry &= (v <= rp[nl]); rp[nl] = v; } if (bn_wexpand(ret, nl) == NULL) return 0; ret->top = nl; ret->flags |= BN_FLG_FIXED_TOP; ret->neg = r->neg; rp = ret->d; ap = &(r->d[nl]); carry -= bn_sub_words(rp, ap, np, nl); for (i = 0; i < nl; i++) { rp[i] = (carry & ap[i]) | (~carry & rp[i]); ap[i] = 0; } return 1; } #endif int BN_from_montgomery(BIGNUM *ret, const BIGNUM *a, BN_MONT_CTX *mont, BN_CTX *ctx) { int retn; retn = bn_from_mont_fixed_top(ret, a, mont, ctx); bn_correct_top(ret); bn_check_top(ret); return retn; } int bn_from_mont_fixed_top(BIGNUM *ret, const BIGNUM *a, BN_MONT_CTX *mont, BN_CTX *ctx) { int retn = 0; #ifdef MONT_WORD BIGNUM *t; BN_CTX_start(ctx); if ((t = BN_CTX_get(ctx)) && BN_copy(t, a)) { retn = bn_from_montgomery_word(ret, t, mont); } BN_CTX_end(ctx); #else BIGNUM *t1, *t2; BN_CTX_start(ctx); t1 = BN_CTX_get(ctx); t2 = BN_CTX_get(ctx); if (t2 == NULL) goto err; if (!BN_copy(t1, a)) goto err; BN_mask_bits(t1, mont->ri); if (!BN_mul(t2, t1, &mont->Ni, ctx)) goto err; BN_mask_bits(t2, mont->ri); if (!BN_mul(t1, t2, &mont->N, ctx)) goto err; if (!BN_add(t2, a, t1)) goto err; if (!BN_rshift(ret, t2, mont->ri)) goto err; if (BN_ucmp(ret, &(mont->N)) >= 0) { if (!BN_usub(ret, ret, &(mont->N))) goto err; } retn = 1; bn_check_top(ret); err: BN_CTX_end(ctx); #endif return retn; } int bn_to_mont_fixed_top(BIGNUM *r, const BIGNUM *a, BN_MONT_CTX *mont, BN_CTX *ctx) { return bn_mul_mont_fixed_top(r, a, &(mont->RR), mont, ctx); } BN_MONT_CTX *BN_MONT_CTX_new(void) { BN_MONT_CTX *ret; if ((ret = OPENSSL_malloc(sizeof(*ret))) == NULL) return NULL; BN_MONT_CTX_init(ret); ret->flags = BN_FLG_MALLOCED; return ret; } void BN_MONT_CTX_init(BN_MONT_CTX *ctx) { ctx->ri = 0; bn_init(&ctx->RR); bn_init(&ctx->N); bn_init(&ctx->Ni); ctx->n0[0] = ctx->n0[1] = 0; ctx->flags = 0; } void BN_MONT_CTX_free(BN_MONT_CTX *mont) { if (mont == NULL) return; BN_clear_free(&mont->RR); BN_clear_free(&mont->N); BN_clear_free(&mont->Ni); if (mont->flags & BN_FLG_MALLOCED) OPENSSL_free(mont); } int BN_MONT_CTX_set(BN_MONT_CTX *mont, const BIGNUM *mod, BN_CTX *ctx) { int i, ret = 0; BIGNUM *Ri, *R; if (BN_is_zero(mod)) return 0; BN_CTX_start(ctx); if ((Ri = BN_CTX_get(ctx)) == NULL) goto err; R = &(mont->RR); if (!BN_copy(&(mont->N), mod)) goto err; if (BN_get_flags(mod, BN_FLG_CONSTTIME) != 0) BN_set_flags(&(mont->N), BN_FLG_CONSTTIME); mont->N.neg = 0; #ifdef MONT_WORD { BIGNUM tmod; BN_ULONG buf[2]; bn_init(&tmod); tmod.d = buf; tmod.dmax = 2; tmod.neg = 0; if (BN_get_flags(mod, BN_FLG_CONSTTIME) != 0) BN_set_flags(&tmod, BN_FLG_CONSTTIME); mont->ri = (BN_num_bits(mod) + (BN_BITS2 - 1)) / BN_BITS2 * BN_BITS2; # if defined(OPENSSL_BN_ASM_MONT) && (BN_BITS2<=32) BN_zero(R); if (!(BN_set_bit(R, 2 * BN_BITS2))) goto err; tmod.top = 0; if ((buf[0] = mod->d[0])) tmod.top = 1; if ((buf[1] = mod->top > 1 ? mod->d[1] : 0)) tmod.top = 2; if (BN_is_one(&tmod)) BN_zero(Ri); else if ((BN_mod_inverse(Ri, R, &tmod, ctx)) == NULL) goto err; if (!BN_lshift(Ri, Ri, 2 * BN_BITS2)) goto err; if (!BN_is_zero(Ri)) { if (!BN_sub_word(Ri, 1)) goto err; } else { if (bn_expand(Ri, (int)sizeof(BN_ULONG) * 2) == NULL) goto err; Ri->neg = 0; Ri->d[0] = BN_MASK2; Ri->d[1] = BN_MASK2; Ri->top = 2; } if (!BN_div(Ri, NULL, Ri, &tmod, ctx)) goto err; mont->n0[0] = (Ri->top > 0) ? Ri->d[0] : 0; mont->n0[1] = (Ri->top > 1) ? Ri->d[1] : 0; # else BN_zero(R); if (!(BN_set_bit(R, BN_BITS2))) goto err; buf[0] = mod->d[0]; buf[1] = 0; tmod.top = buf[0] != 0 ? 1 : 0; if (BN_is_one(&tmod)) BN_zero(Ri); else if ((BN_mod_inverse(Ri, R, &tmod, ctx)) == NULL) goto err; if (!BN_lshift(Ri, Ri, BN_BITS2)) goto err; if (!BN_is_zero(Ri)) { if (!BN_sub_word(Ri, 1)) goto err; } else { if (!BN_set_word(Ri, BN_MASK2)) goto err; } if (!BN_div(Ri, NULL, Ri, &tmod, ctx)) goto err; mont->n0[0] = (Ri->top > 0) ? Ri->d[0] : 0; mont->n0[1] = 0; # endif } #else { mont->ri = BN_num_bits(&mont->N); BN_zero(R); if (!BN_set_bit(R, mont->ri)) goto err; if ((BN_mod_inverse(Ri, R, &mont->N, ctx)) == NULL) goto err; if (!BN_lshift(Ri, Ri, mont->ri)) goto err; if (!BN_sub_word(Ri, 1)) goto err; if (!BN_div(&(mont->Ni), NULL, Ri, &mont->N, ctx)) goto err; } #endif BN_zero(&(mont->RR)); if (!BN_set_bit(&(mont->RR), mont->ri * 2)) goto err; if (!BN_mod(&(mont->RR), &(mont->RR), &(mont->N), ctx)) goto err; for (i = mont->RR.top, ret = mont->N.top; i < ret; i++) mont->RR.d[i] = 0; mont->RR.top = ret; mont->RR.flags |= BN_FLG_FIXED_TOP; ret = 1; err: BN_CTX_end(ctx); return ret; } BN_MONT_CTX *BN_MONT_CTX_copy(BN_MONT_CTX *to, BN_MONT_CTX *from) { if (to == from) return to; if (!BN_copy(&(to->RR), &(from->RR))) return NULL; if (!BN_copy(&(to->N), &(from->N))) return NULL; if (!BN_copy(&(to->Ni), &(from->Ni))) return NULL; to->ri = from->ri; to->n0[0] = from->n0[0]; to->n0[1] = from->n0[1]; return to; } BN_MONT_CTX *BN_MONT_CTX_set_locked(BN_MONT_CTX **pmont, CRYPTO_RWLOCK *lock, const BIGNUM *mod, BN_CTX *ctx) { BN_MONT_CTX *ret; if (!CRYPTO_THREAD_read_lock(lock)) return NULL; ret = *pmont; CRYPTO_THREAD_unlock(lock); if (ret) return ret; ret = BN_MONT_CTX_new(); if (ret == NULL) return NULL; if (!BN_MONT_CTX_set(ret, mod, ctx)) { BN_MONT_CTX_free(ret); return NULL; } if (!CRYPTO_THREAD_write_lock(lock)) { BN_MONT_CTX_free(ret); return NULL; } if (*pmont) { BN_MONT_CTX_free(ret); ret = *pmont; } else *pmont = ret; CRYPTO_THREAD_unlock(lock); return ret; }
bn
openssl/crypto/bn/bn_mont.c
openssl
#include "internal/cryptlib.h" #include "bn_local.h" signed char *bn_compute_wNAF(const BIGNUM *scalar, int w, size_t *ret_len) { int window_val; signed char *r = NULL; int sign = 1; int bit, next_bit, mask; size_t len = 0, j; if (BN_is_zero(scalar)) { r = OPENSSL_malloc(1); if (r == NULL) goto err; r[0] = 0; *ret_len = 1; return r; } if (w <= 0 || w > 7) { ERR_raise(ERR_LIB_BN, ERR_R_INTERNAL_ERROR); goto err; } bit = 1 << w; next_bit = bit << 1; mask = next_bit - 1; if (BN_is_negative(scalar)) { sign = -1; } if (scalar->d == NULL || scalar->top == 0) { ERR_raise(ERR_LIB_BN, ERR_R_INTERNAL_ERROR); goto err; } len = BN_num_bits(scalar); r = OPENSSL_malloc(len + 1); if (r == NULL) goto err; window_val = scalar->d[0] & mask; j = 0; while ((window_val != 0) || (j + w + 1 < len)) { int digit = 0; if (window_val & 1) { if (window_val & bit) { digit = window_val - next_bit; #if 1 if (j + w + 1 >= len) { digit = window_val & (mask >> 1); } #endif } else { digit = window_val; } if (digit <= -bit || digit >= bit || !(digit & 1)) { ERR_raise(ERR_LIB_BN, ERR_R_INTERNAL_ERROR); goto err; } window_val -= digit; if (window_val != 0 && window_val != next_bit && window_val != bit) { ERR_raise(ERR_LIB_BN, ERR_R_INTERNAL_ERROR); goto err; } } r[j++] = sign * digit; window_val >>= 1; window_val += bit * BN_is_bit_set(scalar, j + w); if (window_val > next_bit) { ERR_raise(ERR_LIB_BN, ERR_R_INTERNAL_ERROR); goto err; } } if (j > len + 1) { ERR_raise(ERR_LIB_BN, ERR_R_INTERNAL_ERROR); goto err; } *ret_len = j; return r; err: OPENSSL_free(r); return NULL; } int bn_get_top(const BIGNUM *a) { return a->top; } int bn_get_dmax(const BIGNUM *a) { return a->dmax; } void bn_set_all_zero(BIGNUM *a) { int i; for (i = a->top; i < a->dmax; i++) a->d[i] = 0; } int bn_copy_words(BN_ULONG *out, const BIGNUM *in, int size) { if (in->top > size) return 0; memset(out, 0, sizeof(*out) * size); if (in->d != NULL) memcpy(out, in->d, sizeof(*out) * in->top); return 1; } BN_ULONG *bn_get_words(const BIGNUM *a) { return a->d; } void bn_set_static_words(BIGNUM *a, const BN_ULONG *words, int size) { a->d = (BN_ULONG *)words; a->dmax = a->top = size; a->neg = 0; a->flags |= BN_FLG_STATIC_DATA; bn_correct_top(a); } int bn_set_words(BIGNUM *a, const BN_ULONG *words, int num_words) { if (bn_wexpand(a, num_words) == NULL) { ERR_raise(ERR_LIB_BN, ERR_R_BN_LIB); return 0; } memcpy(a->d, words, sizeof(BN_ULONG) * num_words); a->top = num_words; bn_correct_top(a); return 1; }
bn
openssl/crypto/bn/bn_intern.c
openssl
#include <assert.h> #include <openssl/bn.h> #include "internal/cryptlib.h" #include "bn_local.h" #if 0 int BN_div(BIGNUM *dv, BIGNUM *rem, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx) { int i, nm, nd; int ret = 0; BIGNUM *D; bn_check_top(m); bn_check_top(d); if (BN_is_zero(d)) { ERR_raise(ERR_LIB_BN, BN_R_DIV_BY_ZERO); return 0; } if (BN_ucmp(m, d) < 0) { if (rem != NULL) { if (BN_copy(rem, m) == NULL) return 0; } if (dv != NULL) BN_zero(dv); return 1; } BN_CTX_start(ctx); D = BN_CTX_get(ctx); if (dv == NULL) dv = BN_CTX_get(ctx); if (rem == NULL) rem = BN_CTX_get(ctx); if (D == NULL || dv == NULL || rem == NULL) goto end; nd = BN_num_bits(d); nm = BN_num_bits(m); if (BN_copy(D, d) == NULL) goto end; if (BN_copy(rem, m) == NULL) goto end; BN_zero(dv); if (bn_wexpand(dv, 1) == NULL) goto end; dv->top = 1; if (!BN_lshift(D, D, nm - nd)) goto end; for (i = nm - nd; i >= 0; i--) { if (!BN_lshift1(dv, dv)) goto end; if (BN_ucmp(rem, D) >= 0) { dv->d[0] |= 1; if (!BN_usub(rem, rem, D)) goto end; } if (!BN_rshift1(D, D)) goto end; } rem->neg = BN_is_zero(rem) ? 0 : m->neg; dv->neg = m->neg ^ d->neg; ret = 1; end: BN_CTX_end(ctx); return ret; } #else # if defined(BN_DIV3W) BN_ULONG bn_div_3_words(const BN_ULONG *m, BN_ULONG d1, BN_ULONG d0); # elif 0 # if BN_BITS2 == 64 && defined(__SIZEOF_INT128__) && __SIZEOF_INT128__==16 # undef BN_ULLONG # define BN_ULLONG uint128_t # define BN_LLONG # endif # ifdef BN_LLONG # define BN_DIV3W static BN_ULONG bn_div_3_words(const BN_ULONG *m, BN_ULONG d1, BN_ULONG d0) { BN_ULLONG R = ((BN_ULLONG)m[0] << BN_BITS2) | m[-1]; BN_ULLONG D = ((BN_ULLONG)d0 << BN_BITS2) | d1; BN_ULONG Q = 0, mask; int i; for (i = 0; i < BN_BITS2; i++) { Q <<= 1; if (R >= D) { Q |= 1; R -= D; } D >>= 1; } mask = 0 - (Q >> (BN_BITS2 - 1)); Q <<= 1; Q |= (R >= D); return (Q | mask) & BN_MASK2; } # endif # endif static int bn_left_align(BIGNUM *num) { BN_ULONG *d = num->d, n, m, rmask; int top = num->top; int rshift = BN_num_bits_word(d[top - 1]), lshift, i; lshift = BN_BITS2 - rshift; rshift %= BN_BITS2; rmask = (BN_ULONG)0 - rshift; rmask |= rmask >> 8; for (i = 0, m = 0; i < top; i++) { n = d[i]; d[i] = ((n << lshift) | m) & BN_MASK2; m = (n >> rshift) & rmask; } return lshift; } # if !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM) \ && !defined(PEDANTIC) && !defined(BN_DIV3W) # if defined(__GNUC__) && __GNUC__>=2 # if defined(__i386) || defined (__i386__) # undef bn_div_words # define bn_div_words(n0,n1,d0) \ ({ asm volatile ( \ "divl %4" \ : "=a"(q), "=d"(rem) \ : "a"(n1), "d"(n0), "r"(d0) \ : "cc"); \ q; \ }) # define REMAINDER_IS_ALREADY_CALCULATED # elif defined(__x86_64) && defined(SIXTY_FOUR_BIT_LONG) # undef bn_div_words # define bn_div_words(n0,n1,d0) \ ({ asm volatile ( \ "divq %4" \ : "=a"(q), "=d"(rem) \ : "a"(n1), "d"(n0), "r"(d0) \ : "cc"); \ q; \ }) # define REMAINDER_IS_ALREADY_CALCULATED # endif # endif # endif int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor, BN_CTX *ctx) { int ret; if (BN_is_zero(divisor)) { ERR_raise(ERR_LIB_BN, BN_R_DIV_BY_ZERO); return 0; } if (divisor->d[divisor->top - 1] == 0) { ERR_raise(ERR_LIB_BN, BN_R_NOT_INITIALIZED); return 0; } ret = bn_div_fixed_top(dv, rm, num, divisor, ctx); if (ret) { if (dv != NULL) bn_correct_top(dv); if (rm != NULL) bn_correct_top(rm); } return ret; } int bn_div_fixed_top(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor, BN_CTX *ctx) { int norm_shift, i, j, loop; BIGNUM *tmp, *snum, *sdiv, *res; BN_ULONG *resp, *wnum, *wnumtop; BN_ULONG d0, d1; int num_n, div_n, num_neg; assert(divisor->top > 0 && divisor->d[divisor->top - 1] != 0); bn_check_top(num); bn_check_top(divisor); bn_check_top(dv); bn_check_top(rm); BN_CTX_start(ctx); res = (dv == NULL) ? BN_CTX_get(ctx) : dv; tmp = BN_CTX_get(ctx); snum = BN_CTX_get(ctx); sdiv = BN_CTX_get(ctx); if (sdiv == NULL) goto err; if (!BN_copy(sdiv, divisor)) goto err; norm_shift = bn_left_align(sdiv); sdiv->neg = 0; if (!(bn_lshift_fixed_top(snum, num, norm_shift))) goto err; div_n = sdiv->top; num_n = snum->top; if (num_n <= div_n) { if (bn_wexpand(snum, div_n + 1) == NULL) goto err; memset(&(snum->d[num_n]), 0, (div_n - num_n + 1) * sizeof(BN_ULONG)); snum->top = num_n = div_n + 1; } loop = num_n - div_n; wnum = &(snum->d[loop]); wnumtop = &(snum->d[num_n - 1]); d0 = sdiv->d[div_n - 1]; d1 = (div_n == 1) ? 0 : sdiv->d[div_n - 2]; if (!bn_wexpand(res, loop)) goto err; num_neg = num->neg; res->neg = (num_neg ^ divisor->neg); res->top = loop; res->flags |= BN_FLG_FIXED_TOP; resp = &(res->d[loop]); if (!bn_wexpand(tmp, (div_n + 1))) goto err; for (i = 0; i < loop; i++, wnumtop--) { BN_ULONG q, l0; # if defined(BN_DIV3W) q = bn_div_3_words(wnumtop, d1, d0); # else BN_ULONG n0, n1, rem = 0; n0 = wnumtop[0]; n1 = wnumtop[-1]; if (n0 == d0) q = BN_MASK2; else { BN_ULONG n2 = (wnumtop == wnum) ? 0 : wnumtop[-2]; # ifdef BN_LLONG BN_ULLONG t2; # if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words) q = (BN_ULONG)(((((BN_ULLONG) n0) << BN_BITS2) | n1) / d0); # else q = bn_div_words(n0, n1, d0); # endif # ifndef REMAINDER_IS_ALREADY_CALCULATED rem = (n1 - q * d0) & BN_MASK2; # endif t2 = (BN_ULLONG) d1 *q; for (;;) { if (t2 <= ((((BN_ULLONG) rem) << BN_BITS2) | n2)) break; q--; rem += d0; if (rem < d0) break; t2 -= d1; } # else BN_ULONG t2l, t2h; q = bn_div_words(n0, n1, d0); # ifndef REMAINDER_IS_ALREADY_CALCULATED rem = (n1 - q * d0) & BN_MASK2; # endif # if defined(BN_UMULT_LOHI) BN_UMULT_LOHI(t2l, t2h, d1, q); # elif defined(BN_UMULT_HIGH) t2l = d1 * q; t2h = BN_UMULT_HIGH(d1, q); # else { BN_ULONG ql, qh; t2l = LBITS(d1); t2h = HBITS(d1); ql = LBITS(q); qh = HBITS(q); mul64(t2l, t2h, ql, qh); } # endif for (;;) { if ((t2h < rem) || ((t2h == rem) && (t2l <= n2))) break; q--; rem += d0; if (rem < d0) break; if (t2l < d1) t2h--; t2l -= d1; } # endif } # endif l0 = bn_mul_words(tmp->d, sdiv->d, div_n, q); tmp->d[div_n] = l0; wnum--; l0 = bn_sub_words(wnum, wnum, tmp->d, div_n + 1); q -= l0; for (l0 = 0 - l0, j = 0; j < div_n; j++) tmp->d[j] = sdiv->d[j] & l0; l0 = bn_add_words(wnum, wnum, tmp->d, div_n); (*wnumtop) += l0; assert((*wnumtop) == 0); *--resp = q; } snum->neg = num_neg; snum->top = div_n; snum->flags |= BN_FLG_FIXED_TOP; if (rm != NULL && bn_rshift_fixed_top(rm, snum, norm_shift) == 0) goto err; BN_CTX_end(ctx); return 1; err: bn_check_top(rm); BN_CTX_end(ctx); return 0; } #endif
bn
openssl/crypto/bn/bn_div.c
openssl
#include <assert.h> #include <limits.h> #include "internal/cryptlib.h" #include "internal/endian.h" #include "bn_local.h" #include <openssl/opensslconf.h> #include "internal/constant_time.h" #ifndef OPENSSL_NO_DEPRECATED_0_9_8 static int bn_limit_bits = 0; static int bn_limit_num = 8; static int bn_limit_bits_low = 0; static int bn_limit_num_low = 8; static int bn_limit_bits_high = 0; static int bn_limit_num_high = 8; static int bn_limit_bits_mont = 0; static int bn_limit_num_mont = 8; void BN_set_params(int mult, int high, int low, int mont) { if (mult >= 0) { if (mult > (int)(sizeof(int) * 8) - 1) mult = sizeof(int) * 8 - 1; bn_limit_bits = mult; bn_limit_num = 1 << mult; } if (high >= 0) { if (high > (int)(sizeof(int) * 8) - 1) high = sizeof(int) * 8 - 1; bn_limit_bits_high = high; bn_limit_num_high = 1 << high; } if (low >= 0) { if (low > (int)(sizeof(int) * 8) - 1) low = sizeof(int) * 8 - 1; bn_limit_bits_low = low; bn_limit_num_low = 1 << low; } if (mont >= 0) { if (mont > (int)(sizeof(int) * 8) - 1) mont = sizeof(int) * 8 - 1; bn_limit_bits_mont = mont; bn_limit_num_mont = 1 << mont; } } int BN_get_params(int which) { if (which == 0) return bn_limit_bits; else if (which == 1) return bn_limit_bits_high; else if (which == 2) return bn_limit_bits_low; else if (which == 3) return bn_limit_bits_mont; else return 0; } #endif const BIGNUM *BN_value_one(void) { static const BN_ULONG data_one = 1L; static const BIGNUM const_one = { (BN_ULONG *)&data_one, 1, 1, 0, BN_FLG_STATIC_DATA }; return &const_one; } #if defined(_MSC_VER) && defined(_ARM_) && defined(_WIN32_WCE) \ && _MSC_VER>=1400 && _MSC_VER<1501 # define MS_BROKEN_BN_num_bits_word # pragma optimize("", off) #endif int BN_num_bits_word(BN_ULONG l) { BN_ULONG x, mask; int bits = (l != 0); #if BN_BITS2 > 32 x = l >> 32; mask = (0 - x) & BN_MASK2; mask = (0 - (mask >> (BN_BITS2 - 1))); bits += 32 & mask; l ^= (x ^ l) & mask; #endif x = l >> 16; mask = (0 - x) & BN_MASK2; mask = (0 - (mask >> (BN_BITS2 - 1))); bits += 16 & mask; l ^= (x ^ l) & mask; x = l >> 8; mask = (0 - x) & BN_MASK2; mask = (0 - (mask >> (BN_BITS2 - 1))); bits += 8 & mask; l ^= (x ^ l) & mask; x = l >> 4; mask = (0 - x) & BN_MASK2; mask = (0 - (mask >> (BN_BITS2 - 1))); bits += 4 & mask; l ^= (x ^ l) & mask; x = l >> 2; mask = (0 - x) & BN_MASK2; mask = (0 - (mask >> (BN_BITS2 - 1))); bits += 2 & mask; l ^= (x ^ l) & mask; x = l >> 1; mask = (0 - x) & BN_MASK2; mask = (0 - (mask >> (BN_BITS2 - 1))); bits += 1 & mask; return bits; } #ifdef MS_BROKEN_BN_num_bits_word # pragma optimize("", on) #endif static ossl_inline int bn_num_bits_consttime(const BIGNUM *a) { int j, ret; unsigned int mask, past_i; int i = a->top - 1; bn_check_top(a); for (j = 0, past_i = 0, ret = 0; j < a->dmax; j++) { mask = constant_time_eq_int(i, j); ret += BN_BITS2 & (~mask & ~past_i); ret += BN_num_bits_word(a->d[j]) & mask; past_i |= mask; } mask = ~(constant_time_eq_int(i, ((int)-1))); return ret & mask; } int BN_num_bits(const BIGNUM *a) { int i = a->top - 1; bn_check_top(a); if (a->flags & BN_FLG_CONSTTIME) { return bn_num_bits_consttime(a); } if (BN_is_zero(a)) return 0; return ((i * BN_BITS2) + BN_num_bits_word(a->d[i])); } static void bn_free_d(BIGNUM *a, int clear) { if (BN_get_flags(a, BN_FLG_SECURE)) OPENSSL_secure_clear_free(a->d, a->dmax * sizeof(a->d[0])); else if (clear != 0) OPENSSL_clear_free(a->d, a->dmax * sizeof(a->d[0])); else OPENSSL_free(a->d); } void BN_clear_free(BIGNUM *a) { if (a == NULL) return; if (a->d != NULL && !BN_get_flags(a, BN_FLG_STATIC_DATA)) bn_free_d(a, 1); if (BN_get_flags(a, BN_FLG_MALLOCED)) { OPENSSL_cleanse(a, sizeof(*a)); OPENSSL_free(a); } } void BN_free(BIGNUM *a) { if (a == NULL) return; if (!BN_get_flags(a, BN_FLG_STATIC_DATA)) bn_free_d(a, 0); if (a->flags & BN_FLG_MALLOCED) OPENSSL_free(a); } void bn_init(BIGNUM *a) { static BIGNUM nilbn; *a = nilbn; bn_check_top(a); } BIGNUM *BN_new(void) { BIGNUM *ret; if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL) return NULL; ret->flags = BN_FLG_MALLOCED; bn_check_top(ret); return ret; } BIGNUM *BN_secure_new(void) { BIGNUM *ret = BN_new(); if (ret != NULL) ret->flags |= BN_FLG_SECURE; return ret; } static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words) { BN_ULONG *a = NULL; if (words > (INT_MAX / (4 * BN_BITS2))) { ERR_raise(ERR_LIB_BN, BN_R_BIGNUM_TOO_LONG); return NULL; } if (BN_get_flags(b, BN_FLG_STATIC_DATA)) { ERR_raise(ERR_LIB_BN, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA); return NULL; } if (BN_get_flags(b, BN_FLG_SECURE)) a = OPENSSL_secure_zalloc(words * sizeof(*a)); else a = OPENSSL_zalloc(words * sizeof(*a)); if (a == NULL) return NULL; assert(b->top <= words); if (b->top > 0) memcpy(a, b->d, sizeof(*a) * b->top); return a; } BIGNUM *bn_expand2(BIGNUM *b, int words) { if (words > b->dmax) { BN_ULONG *a = bn_expand_internal(b, words); if (!a) return NULL; if (b->d != NULL) bn_free_d(b, 1); b->d = a; b->dmax = words; } return b; } BIGNUM *BN_dup(const BIGNUM *a) { BIGNUM *t; if (a == NULL) return NULL; bn_check_top(a); t = BN_get_flags(a, BN_FLG_SECURE) ? BN_secure_new() : BN_new(); if (t == NULL) return NULL; if (!BN_copy(t, a)) { BN_free(t); return NULL; } bn_check_top(t); return t; } BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b) { int bn_words; bn_check_top(b); bn_words = BN_get_flags(b, BN_FLG_CONSTTIME) ? b->dmax : b->top; if (a == b) return a; if (bn_wexpand(a, bn_words) == NULL) return NULL; if (b->top > 0) memcpy(a->d, b->d, sizeof(b->d[0]) * bn_words); a->neg = b->neg; a->top = b->top; a->flags |= b->flags & BN_FLG_FIXED_TOP; bn_check_top(a); return a; } #define FLAGS_DATA(flags) ((flags) & (BN_FLG_STATIC_DATA \ | BN_FLG_CONSTTIME \ | BN_FLG_SECURE \ | BN_FLG_FIXED_TOP)) #define FLAGS_STRUCT(flags) ((flags) & (BN_FLG_MALLOCED)) void BN_swap(BIGNUM *a, BIGNUM *b) { int flags_old_a, flags_old_b; BN_ULONG *tmp_d; int tmp_top, tmp_dmax, tmp_neg; bn_check_top(a); bn_check_top(b); flags_old_a = a->flags; flags_old_b = b->flags; tmp_d = a->d; tmp_top = a->top; tmp_dmax = a->dmax; tmp_neg = a->neg; a->d = b->d; a->top = b->top; a->dmax = b->dmax; a->neg = b->neg; b->d = tmp_d; b->top = tmp_top; b->dmax = tmp_dmax; b->neg = tmp_neg; a->flags = FLAGS_STRUCT(flags_old_a) | FLAGS_DATA(flags_old_b); b->flags = FLAGS_STRUCT(flags_old_b) | FLAGS_DATA(flags_old_a); bn_check_top(a); bn_check_top(b); } void BN_clear(BIGNUM *a) { if (a == NULL) return; bn_check_top(a); if (a->d != NULL) OPENSSL_cleanse(a->d, sizeof(*a->d) * a->dmax); a->neg = 0; a->top = 0; a->flags &= ~BN_FLG_FIXED_TOP; } BN_ULONG BN_get_word(const BIGNUM *a) { if (a->top > 1) return BN_MASK2; else if (a->top == 1) return a->d[0]; return 0; } int BN_set_word(BIGNUM *a, BN_ULONG w) { bn_check_top(a); if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL) return 0; a->neg = 0; a->d[0] = w; a->top = (w ? 1 : 0); a->flags &= ~BN_FLG_FIXED_TOP; bn_check_top(a); return 1; } typedef enum {BIG, LITTLE} endianness_t; typedef enum {SIGNED, UNSIGNED} signedness_t; static BIGNUM *bin2bn(const unsigned char *s, int len, BIGNUM *ret, endianness_t endianness, signedness_t signedness) { int inc; const unsigned char *s2; int inc2; int neg = 0, xor = 0, carry = 0; unsigned int i; unsigned int n; BIGNUM *bn = NULL; if (len < 0) return NULL; if (ret == NULL) ret = bn = BN_new(); if (ret == NULL) return NULL; bn_check_top(ret); if (len == 0) { BN_clear(ret); return ret; } if (endianness == LITTLE) { s2 = s + len - 1; inc2 = -1; inc = 1; } else { s2 = s; inc2 = 1; inc = -1; s += len - 1; } if (signedness == SIGNED) { neg = !!(*s2 & 0x80); xor = neg ? 0xff : 0x00; carry = neg; } for ( ; len > 0 && *s2 == xor; s2 += inc2, len--) continue; if (xor == 0xff) { if (len == 0 || !(*s2 & 0x80)) len++; } if (len == 0) { ret->top = 0; return ret; } n = ((len - 1) / BN_BYTES) + 1; if (bn_wexpand(ret, (int)n) == NULL) { BN_free(bn); return NULL; } ret->top = n; ret->neg = neg; for (i = 0; n-- > 0; i++) { BN_ULONG l = 0; unsigned int m = 0; for (; len > 0 && m < BN_BYTES * 8; len--, s += inc, m += 8) { BN_ULONG byte_xored = *s ^ xor; BN_ULONG byte = (byte_xored + carry) & 0xff; carry = byte_xored > byte; l |= (byte << m); } ret->d[i] = l; } bn_correct_top(ret); return ret; } BIGNUM *BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret) { return bin2bn(s, len, ret, BIG, UNSIGNED); } BIGNUM *BN_signed_bin2bn(const unsigned char *s, int len, BIGNUM *ret) { return bin2bn(s, len, ret, BIG, SIGNED); } static int bn2binpad(const BIGNUM *a, unsigned char *to, int tolen, endianness_t endianness, signedness_t signedness) { int inc; int n, n8; int xor = 0, carry = 0, ext = 0; size_t i, lasti, j, atop, mask; BN_ULONG l; n8 = BN_num_bits(a); n = (n8 + 7) / 8; if (signedness == SIGNED) { xor = a->neg ? 0xff : 0x00; carry = a->neg; ext = (n * 8 == n8) ? !a->neg : a->neg; } if (tolen == -1) { tolen = n + ext; } else if (tolen < n + ext) { BIGNUM temp = *a; bn_correct_top(&temp); n8 = BN_num_bits(&temp); n = (n8 + 7) / 8; if (tolen < n + ext) return -1; } atop = a->dmax * BN_BYTES; if (atop == 0) { if (tolen != 0) memset(to, '\0', tolen); return tolen; } if (endianness == LITTLE) { inc = 1; } else { inc = -1; to += tolen - 1; } lasti = atop - 1; atop = a->top * BN_BYTES; for (i = 0, j = 0; j < (size_t)tolen; j++) { unsigned char byte, byte_xored; l = a->d[i / BN_BYTES]; mask = 0 - ((j - atop) >> (8 * sizeof(i) - 1)); byte = (unsigned char)(l >> (8 * (i % BN_BYTES)) & mask); byte_xored = byte ^ xor; *to = (unsigned char)(byte_xored + carry); carry = byte_xored > *to; to += inc; i += (i - lasti) >> (8 * sizeof(i) - 1); } return tolen; } int BN_bn2binpad(const BIGNUM *a, unsigned char *to, int tolen) { if (tolen < 0) return -1; return bn2binpad(a, to, tolen, BIG, UNSIGNED); } int BN_signed_bn2bin(const BIGNUM *a, unsigned char *to, int tolen) { if (tolen < 0) return -1; return bn2binpad(a, to, tolen, BIG, SIGNED); } int BN_bn2bin(const BIGNUM *a, unsigned char *to) { return bn2binpad(a, to, -1, BIG, UNSIGNED); } BIGNUM *BN_lebin2bn(const unsigned char *s, int len, BIGNUM *ret) { return bin2bn(s, len, ret, LITTLE, UNSIGNED); } BIGNUM *BN_signed_lebin2bn(const unsigned char *s, int len, BIGNUM *ret) { return bin2bn(s, len, ret, LITTLE, SIGNED); } int BN_bn2lebinpad(const BIGNUM *a, unsigned char *to, int tolen) { if (tolen < 0) return -1; return bn2binpad(a, to, tolen, LITTLE, UNSIGNED); } int BN_signed_bn2lebin(const BIGNUM *a, unsigned char *to, int tolen) { if (tolen < 0) return -1; return bn2binpad(a, to, tolen, LITTLE, SIGNED); } BIGNUM *BN_native2bn(const unsigned char *s, int len, BIGNUM *ret) { DECLARE_IS_ENDIAN; if (IS_LITTLE_ENDIAN) return BN_lebin2bn(s, len, ret); return BN_bin2bn(s, len, ret); } BIGNUM *BN_signed_native2bn(const unsigned char *s, int len, BIGNUM *ret) { DECLARE_IS_ENDIAN; if (IS_LITTLE_ENDIAN) return BN_signed_lebin2bn(s, len, ret); return BN_signed_bin2bn(s, len, ret); } int BN_bn2nativepad(const BIGNUM *a, unsigned char *to, int tolen) { DECLARE_IS_ENDIAN; if (IS_LITTLE_ENDIAN) return BN_bn2lebinpad(a, to, tolen); return BN_bn2binpad(a, to, tolen); } int BN_signed_bn2native(const BIGNUM *a, unsigned char *to, int tolen) { DECLARE_IS_ENDIAN; if (IS_LITTLE_ENDIAN) return BN_signed_bn2lebin(a, to, tolen); return BN_signed_bn2bin(a, to, tolen); } int BN_ucmp(const BIGNUM *a, const BIGNUM *b) { int i; BN_ULONG t1, t2, *ap, *bp; bn_check_top(a); bn_check_top(b); i = a->top - b->top; if (i != 0) return i; ap = a->d; bp = b->d; for (i = a->top - 1; i >= 0; i--) { t1 = ap[i]; t2 = bp[i]; if (t1 != t2) return ((t1 > t2) ? 1 : -1); } return 0; } int BN_cmp(const BIGNUM *a, const BIGNUM *b) { int i; int gt, lt; BN_ULONG t1, t2; if ((a == NULL) || (b == NULL)) { if (a != NULL) return -1; else if (b != NULL) return 1; else return 0; } bn_check_top(a); bn_check_top(b); if (a->neg != b->neg) { if (a->neg) return -1; else return 1; } if (a->neg == 0) { gt = 1; lt = -1; } else { gt = -1; lt = 1; } if (a->top > b->top) return gt; if (a->top < b->top) return lt; for (i = a->top - 1; i >= 0; i--) { t1 = a->d[i]; t2 = b->d[i]; if (t1 > t2) return gt; if (t1 < t2) return lt; } return 0; } int BN_set_bit(BIGNUM *a, int n) { int i, j, k; if (n < 0) return 0; i = n / BN_BITS2; j = n % BN_BITS2; if (a->top <= i) { if (bn_wexpand(a, i + 1) == NULL) return 0; for (k = a->top; k < i + 1; k++) a->d[k] = 0; a->top = i + 1; a->flags &= ~BN_FLG_FIXED_TOP; } a->d[i] |= (((BN_ULONG)1) << j); bn_check_top(a); return 1; } int BN_clear_bit(BIGNUM *a, int n) { int i, j; bn_check_top(a); if (n < 0) return 0; i = n / BN_BITS2; j = n % BN_BITS2; if (a->top <= i) return 0; a->d[i] &= (~(((BN_ULONG)1) << j)); bn_correct_top(a); return 1; } int BN_is_bit_set(const BIGNUM *a, int n) { int i, j; bn_check_top(a); if (n < 0) return 0; i = n / BN_BITS2; j = n % BN_BITS2; if (a->top <= i) return 0; return (int)(((a->d[i]) >> j) & ((BN_ULONG)1)); } int BN_mask_bits(BIGNUM *a, int n) { int b, w; bn_check_top(a); if (n < 0) return 0; w = n / BN_BITS2; b = n % BN_BITS2; if (w >= a->top) return 0; if (b == 0) a->top = w; else { a->top = w + 1; a->d[w] &= ~(BN_MASK2 << b); } bn_correct_top(a); return 1; } void BN_set_negative(BIGNUM *a, int b) { if (b && !BN_is_zero(a)) a->neg = 1; else a->neg = 0; } int bn_cmp_words(const BN_ULONG *a, const BN_ULONG *b, int n) { int i; BN_ULONG aa, bb; if (n == 0) return 0; aa = a[n - 1]; bb = b[n - 1]; if (aa != bb) return ((aa > bb) ? 1 : -1); for (i = n - 2; i >= 0; i--) { aa = a[i]; bb = b[i]; if (aa != bb) return ((aa > bb) ? 1 : -1); } return 0; } int bn_cmp_part_words(const BN_ULONG *a, const BN_ULONG *b, int cl, int dl) { int n, i; n = cl - 1; if (dl < 0) { for (i = dl; i < 0; i++) { if (b[n - i] != 0) return -1; } } if (dl > 0) { for (i = dl; i > 0; i--) { if (a[n + i] != 0) return 1; } } return bn_cmp_words(a, b, cl); } void BN_consttime_swap(BN_ULONG condition, BIGNUM *a, BIGNUM *b, int nwords) { BN_ULONG t; int i; bn_wcheck_size(a, nwords); bn_wcheck_size(b, nwords); condition = ((~condition & ((condition - 1))) >> (BN_BITS2 - 1)) - 1; t = (a->top ^ b->top) & condition; a->top ^= t; b->top ^= t; t = (a->neg ^ b->neg) & condition; a->neg ^= t; b->neg ^= t; #define BN_CONSTTIME_SWAP_FLAGS (BN_FLG_CONSTTIME | BN_FLG_FIXED_TOP) t = ((a->flags ^ b->flags) & BN_CONSTTIME_SWAP_FLAGS) & condition; a->flags ^= t; b->flags ^= t; for (i = 0; i < nwords; i++) { t = (a->d[i] ^ b->d[i]) & condition; a->d[i] ^= t; b->d[i] ^= t; } } #undef BN_CONSTTIME_SWAP_FLAGS int BN_security_bits(int L, int N) { int secbits, bits; if (L >= 15360) secbits = 256; else if (L >= 7680) secbits = 192; else if (L >= 3072) secbits = 128; else if (L >= 2048) secbits = 112; else if (L >= 1024) secbits = 80; else return 0; if (N == -1) return secbits; bits = N / 2; if (bits < 80) return 0; return bits >= secbits ? secbits : bits; } void BN_zero_ex(BIGNUM *a) { a->neg = 0; a->top = 0; a->flags &= ~BN_FLG_FIXED_TOP; } int BN_abs_is_word(const BIGNUM *a, const BN_ULONG w) { return ((a->top == 1) && (a->d[0] == w)) || ((w == 0) && (a->top == 0)); } int BN_is_zero(const BIGNUM *a) { return a->top == 0; } int BN_is_one(const BIGNUM *a) { return BN_abs_is_word(a, 1) && !a->neg; } int BN_is_word(const BIGNUM *a, const BN_ULONG w) { return BN_abs_is_word(a, w) && (!w || !a->neg); } int BN_is_odd(const BIGNUM *a) { return (a->top > 0) && (a->d[0] & 1); } int BN_is_negative(const BIGNUM *a) { return (a->neg != 0); } int BN_to_montgomery(BIGNUM *r, const BIGNUM *a, BN_MONT_CTX *mont, BN_CTX *ctx) { return BN_mod_mul_montgomery(r, a, &(mont->RR), mont, ctx); } void BN_with_flags(BIGNUM *dest, const BIGNUM *b, int flags) { dest->d = b->d; dest->top = b->top; dest->dmax = b->dmax; dest->neg = b->neg; dest->flags = ((dest->flags & BN_FLG_MALLOCED) | (b->flags & ~BN_FLG_MALLOCED) | BN_FLG_STATIC_DATA | flags); } BN_GENCB *BN_GENCB_new(void) { BN_GENCB *ret; if ((ret = OPENSSL_malloc(sizeof(*ret))) == NULL) return NULL; return ret; } void BN_GENCB_free(BN_GENCB *cb) { if (cb == NULL) return; OPENSSL_free(cb); } void BN_set_flags(BIGNUM *b, int n) { b->flags |= n; } int BN_get_flags(const BIGNUM *b, int n) { return b->flags & n; } void BN_GENCB_set_old(BN_GENCB *gencb, void (*callback) (int, int, void *), void *cb_arg) { BN_GENCB *tmp_gencb = gencb; tmp_gencb->ver = 1; tmp_gencb->arg = cb_arg; tmp_gencb->cb.cb_1 = callback; } void BN_GENCB_set(BN_GENCB *gencb, int (*callback) (int, int, BN_GENCB *), void *cb_arg) { BN_GENCB *tmp_gencb = gencb; tmp_gencb->ver = 2; tmp_gencb->arg = cb_arg; tmp_gencb->cb.cb_2 = callback; } void *BN_GENCB_get_arg(BN_GENCB *cb) { return cb->arg; } BIGNUM *bn_wexpand(BIGNUM *a, int words) { return (words <= a->dmax) ? a : bn_expand2(a, words); } void bn_correct_top_consttime(BIGNUM *a) { int j, atop; BN_ULONG limb; unsigned int mask; for (j = 0, atop = 0; j < a->dmax; j++) { limb = a->d[j]; limb |= 0 - limb; limb >>= BN_BITS2 - 1; limb = 0 - limb; mask = (unsigned int)limb; mask &= constant_time_msb(j - a->top); atop = constant_time_select_int(mask, j + 1, atop); } mask = constant_time_eq_int(atop, 0); a->top = atop; a->neg = constant_time_select_int(mask, 0, a->neg); a->flags &= ~BN_FLG_FIXED_TOP; } void bn_correct_top(BIGNUM *a) { BN_ULONG *ftl; int tmp_top = a->top; if (tmp_top > 0) { for (ftl = &(a->d[tmp_top]); tmp_top > 0; tmp_top--) { ftl--; if (*ftl != 0) break; } a->top = tmp_top; } if (a->top == 0) a->neg = 0; a->flags &= ~BN_FLG_FIXED_TOP; bn_pollute(a); }
bn
openssl/crypto/bn/bn_lib.c
openssl
#include <openssl/opensslconf.h> #include "internal/cryptlib.h" #include "bn_local.h" #define BN_BLINDING_COUNTER 32 struct bn_blinding_st { BIGNUM *A; BIGNUM *Ai; BIGNUM *e; BIGNUM *mod; CRYPTO_THREAD_ID tid; int counter; unsigned long flags; BN_MONT_CTX *m_ctx; int (*bn_mod_exp) (BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx); CRYPTO_RWLOCK *lock; }; BN_BLINDING *BN_BLINDING_new(const BIGNUM *A, const BIGNUM *Ai, BIGNUM *mod) { BN_BLINDING *ret = NULL; bn_check_top(mod); if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL) return NULL; ret->lock = CRYPTO_THREAD_lock_new(); if (ret->lock == NULL) { ERR_raise(ERR_LIB_BN, ERR_R_CRYPTO_LIB); OPENSSL_free(ret); return NULL; } BN_BLINDING_set_current_thread(ret); if (A != NULL) { if ((ret->A = BN_dup(A)) == NULL) goto err; } if (Ai != NULL) { if ((ret->Ai = BN_dup(Ai)) == NULL) goto err; } if ((ret->mod = BN_dup(mod)) == NULL) goto err; if (BN_get_flags(mod, BN_FLG_CONSTTIME) != 0) BN_set_flags(ret->mod, BN_FLG_CONSTTIME); ret->counter = -1; return ret; err: BN_BLINDING_free(ret); return NULL; } void BN_BLINDING_free(BN_BLINDING *r) { if (r == NULL) return; BN_free(r->A); BN_free(r->Ai); BN_free(r->e); BN_free(r->mod); CRYPTO_THREAD_lock_free(r->lock); OPENSSL_free(r); } int BN_BLINDING_update(BN_BLINDING *b, BN_CTX *ctx) { int ret = 0; if ((b->A == NULL) || (b->Ai == NULL)) { ERR_raise(ERR_LIB_BN, BN_R_NOT_INITIALIZED); goto err; } if (b->counter == -1) b->counter = 0; if (++b->counter == BN_BLINDING_COUNTER && b->e != NULL && !(b->flags & BN_BLINDING_NO_RECREATE)) { if (!BN_BLINDING_create_param(b, NULL, NULL, ctx, NULL, NULL)) goto err; } else if (!(b->flags & BN_BLINDING_NO_UPDATE)) { if (b->m_ctx != NULL) { if (!bn_mul_mont_fixed_top(b->Ai, b->Ai, b->Ai, b->m_ctx, ctx) || !bn_mul_mont_fixed_top(b->A, b->A, b->A, b->m_ctx, ctx)) goto err; } else { if (!BN_mod_mul(b->Ai, b->Ai, b->Ai, b->mod, ctx) || !BN_mod_mul(b->A, b->A, b->A, b->mod, ctx)) goto err; } } ret = 1; err: if (b->counter == BN_BLINDING_COUNTER) b->counter = 0; return ret; } int BN_BLINDING_convert(BIGNUM *n, BN_BLINDING *b, BN_CTX *ctx) { return BN_BLINDING_convert_ex(n, NULL, b, ctx); } int BN_BLINDING_convert_ex(BIGNUM *n, BIGNUM *r, BN_BLINDING *b, BN_CTX *ctx) { int ret = 1; bn_check_top(n); if ((b->A == NULL) || (b->Ai == NULL)) { ERR_raise(ERR_LIB_BN, BN_R_NOT_INITIALIZED); return 0; } if (b->counter == -1) b->counter = 0; else if (!BN_BLINDING_update(b, ctx)) return 0; if (r != NULL && (BN_copy(r, b->Ai) == NULL)) return 0; if (b->m_ctx != NULL) ret = BN_mod_mul_montgomery(n, n, b->A, b->m_ctx, ctx); else ret = BN_mod_mul(n, n, b->A, b->mod, ctx); return ret; } int BN_BLINDING_invert(BIGNUM *n, BN_BLINDING *b, BN_CTX *ctx) { return BN_BLINDING_invert_ex(n, NULL, b, ctx); } int BN_BLINDING_invert_ex(BIGNUM *n, const BIGNUM *r, BN_BLINDING *b, BN_CTX *ctx) { int ret; bn_check_top(n); if (r == NULL && (r = b->Ai) == NULL) { ERR_raise(ERR_LIB_BN, BN_R_NOT_INITIALIZED); return 0; } if (b->m_ctx != NULL) { if (n->dmax >= r->top) { size_t i, rtop = r->top, ntop = n->top; BN_ULONG mask; for (i = 0; i < rtop; i++) { mask = (BN_ULONG)0 - ((i - ntop) >> (8 * sizeof(i) - 1)); n->d[i] &= mask; } mask = (BN_ULONG)0 - ((rtop - ntop) >> (8 * sizeof(ntop) - 1)); n->top = (int)(rtop & ~mask) | (ntop & mask); n->flags |= (BN_FLG_FIXED_TOP & ~mask); } ret = bn_mul_mont_fixed_top(n, n, r, b->m_ctx, ctx); bn_correct_top_consttime(n); } else { ret = BN_mod_mul(n, n, r, b->mod, ctx); } bn_check_top(n); return ret; } int BN_BLINDING_is_current_thread(BN_BLINDING *b) { return CRYPTO_THREAD_compare_id(CRYPTO_THREAD_get_current_id(), b->tid); } void BN_BLINDING_set_current_thread(BN_BLINDING *b) { b->tid = CRYPTO_THREAD_get_current_id(); } int BN_BLINDING_lock(BN_BLINDING *b) { return CRYPTO_THREAD_write_lock(b->lock); } int BN_BLINDING_unlock(BN_BLINDING *b) { return CRYPTO_THREAD_unlock(b->lock); } unsigned long BN_BLINDING_get_flags(const BN_BLINDING *b) { return b->flags; } void BN_BLINDING_set_flags(BN_BLINDING *b, unsigned long flags) { b->flags = flags; } BN_BLINDING *BN_BLINDING_create_param(BN_BLINDING *b, const BIGNUM *e, BIGNUM *m, BN_CTX *ctx, int (*bn_mod_exp) (BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx), BN_MONT_CTX *m_ctx) { int retry_counter = 32; BN_BLINDING *ret = NULL; if (b == NULL) ret = BN_BLINDING_new(NULL, NULL, m); else ret = b; if (ret == NULL) goto err; if (ret->A == NULL && (ret->A = BN_new()) == NULL) goto err; if (ret->Ai == NULL && (ret->Ai = BN_new()) == NULL) goto err; if (e != NULL) { BN_free(ret->e); ret->e = BN_dup(e); } if (ret->e == NULL) goto err; if (bn_mod_exp != NULL) ret->bn_mod_exp = bn_mod_exp; if (m_ctx != NULL) ret->m_ctx = m_ctx; do { int rv; if (!BN_priv_rand_range_ex(ret->A, ret->mod, 0, ctx)) goto err; if (int_bn_mod_inverse(ret->Ai, ret->A, ret->mod, ctx, &rv)) break; if (!rv) goto err; if (retry_counter-- == 0) { ERR_raise(ERR_LIB_BN, BN_R_TOO_MANY_ITERATIONS); goto err; } } while (1); if (ret->bn_mod_exp != NULL && ret->m_ctx != NULL) { if (!ret->bn_mod_exp(ret->A, ret->A, ret->e, ret->mod, ctx, ret->m_ctx)) goto err; } else { if (!BN_mod_exp(ret->A, ret->A, ret->e, ret->mod, ctx)) goto err; } if (ret->m_ctx != NULL) { if (!bn_to_mont_fixed_top(ret->Ai, ret->Ai, ret->m_ctx, ctx) || !bn_to_mont_fixed_top(ret->A, ret->A, ret->m_ctx, ctx)) goto err; } return ret; err: if (b == NULL) { BN_BLINDING_free(ret); ret = NULL; } return ret; }
bn
openssl/crypto/bn/bn_blind.c
openssl
#include <assert.h> #include <openssl/crypto.h> #include "internal/cryptlib.h" #include "bn_local.h" #if defined(BN_LLONG) || defined(BN_UMULT_HIGH) BN_ULONG bn_mul_add_words(BN_ULONG *rp, const BN_ULONG *ap, int num, BN_ULONG w) { BN_ULONG c1 = 0; assert(num >= 0); if (num <= 0) return c1; # ifndef OPENSSL_SMALL_FOOTPRINT while (num & ~3) { mul_add(rp[0], ap[0], w, c1); mul_add(rp[1], ap[1], w, c1); mul_add(rp[2], ap[2], w, c1); mul_add(rp[3], ap[3], w, c1); ap += 4; rp += 4; num -= 4; } # endif while (num) { mul_add(rp[0], ap[0], w, c1); ap++; rp++; num--; } return c1; } BN_ULONG bn_mul_words(BN_ULONG *rp, const BN_ULONG *ap, int num, BN_ULONG w) { BN_ULONG c1 = 0; assert(num >= 0); if (num <= 0) return c1; # ifndef OPENSSL_SMALL_FOOTPRINT while (num & ~3) { mul(rp[0], ap[0], w, c1); mul(rp[1], ap[1], w, c1); mul(rp[2], ap[2], w, c1); mul(rp[3], ap[3], w, c1); ap += 4; rp += 4; num -= 4; } # endif while (num) { mul(rp[0], ap[0], w, c1); ap++; rp++; num--; } return c1; } void bn_sqr_words(BN_ULONG *r, const BN_ULONG *a, int n) { assert(n >= 0); if (n <= 0) return; # ifndef OPENSSL_SMALL_FOOTPRINT while (n & ~3) { sqr(r[0], r[1], a[0]); sqr(r[2], r[3], a[1]); sqr(r[4], r[5], a[2]); sqr(r[6], r[7], a[3]); a += 4; r += 8; n -= 4; } # endif while (n) { sqr(r[0], r[1], a[0]); a++; r += 2; n--; } } #else BN_ULONG bn_mul_add_words(BN_ULONG *rp, const BN_ULONG *ap, int num, BN_ULONG w) { BN_ULONG c = 0; BN_ULONG bl, bh; assert(num >= 0); if (num <= 0) return (BN_ULONG)0; bl = LBITS(w); bh = HBITS(w); # ifndef OPENSSL_SMALL_FOOTPRINT while (num & ~3) { mul_add(rp[0], ap[0], bl, bh, c); mul_add(rp[1], ap[1], bl, bh, c); mul_add(rp[2], ap[2], bl, bh, c); mul_add(rp[3], ap[3], bl, bh, c); ap += 4; rp += 4; num -= 4; } # endif while (num) { mul_add(rp[0], ap[0], bl, bh, c); ap++; rp++; num--; } return c; } BN_ULONG bn_mul_words(BN_ULONG *rp, const BN_ULONG *ap, int num, BN_ULONG w) { BN_ULONG carry = 0; BN_ULONG bl, bh; assert(num >= 0); if (num <= 0) return (BN_ULONG)0; bl = LBITS(w); bh = HBITS(w); # ifndef OPENSSL_SMALL_FOOTPRINT while (num & ~3) { mul(rp[0], ap[0], bl, bh, carry); mul(rp[1], ap[1], bl, bh, carry); mul(rp[2], ap[2], bl, bh, carry); mul(rp[3], ap[3], bl, bh, carry); ap += 4; rp += 4; num -= 4; } # endif while (num) { mul(rp[0], ap[0], bl, bh, carry); ap++; rp++; num--; } return carry; } void bn_sqr_words(BN_ULONG *r, const BN_ULONG *a, int n) { assert(n >= 0); if (n <= 0) return; # ifndef OPENSSL_SMALL_FOOTPRINT while (n & ~3) { sqr64(r[0], r[1], a[0]); sqr64(r[2], r[3], a[1]); sqr64(r[4], r[5], a[2]); sqr64(r[6], r[7], a[3]); a += 4; r += 8; n -= 4; } # endif while (n) { sqr64(r[0], r[1], a[0]); a++; r += 2; n--; } } #endif #if defined(BN_LLONG) && defined(BN_DIV2W) BN_ULONG bn_div_words(BN_ULONG h, BN_ULONG l, BN_ULONG d) { return ((BN_ULONG)(((((BN_ULLONG) h) << BN_BITS2) | l) / (BN_ULLONG) d)); } #else BN_ULONG bn_div_words(BN_ULONG h, BN_ULONG l, BN_ULONG d) { BN_ULONG dh, dl, q, ret = 0, th, tl, t; int i, count = 2; if (d == 0) return BN_MASK2; i = BN_num_bits_word(d); assert((i == BN_BITS2) || (h <= (BN_ULONG)1 << i)); i = BN_BITS2 - i; if (h >= d) h -= d; if (i) { d <<= i; h = (h << i) | (l >> (BN_BITS2 - i)); l <<= i; } dh = (d & BN_MASK2h) >> BN_BITS4; dl = (d & BN_MASK2l); for (;;) { if ((h >> BN_BITS4) == dh) q = BN_MASK2l; else q = h / dh; th = q * dh; tl = dl * q; for (;;) { t = h - th; if ((t & BN_MASK2h) || ((tl) <= ((t << BN_BITS4) | ((l & BN_MASK2h) >> BN_BITS4)))) break; q--; th -= dh; tl -= dl; } t = (tl >> BN_BITS4); tl = (tl << BN_BITS4) & BN_MASK2h; th += t; if (l < tl) th++; l -= tl; if (h < th) { h += d; q--; } h -= th; if (--count == 0) break; ret = q << BN_BITS4; h = ((h << BN_BITS4) | (l >> BN_BITS4)) & BN_MASK2; l = (l & BN_MASK2l) << BN_BITS4; } ret |= q; return ret; } #endif #ifdef BN_LLONG BN_ULONG bn_add_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b, int n) { BN_ULLONG ll = 0; assert(n >= 0); if (n <= 0) return (BN_ULONG)0; # ifndef OPENSSL_SMALL_FOOTPRINT while (n & ~3) { ll += (BN_ULLONG) a[0] + b[0]; r[0] = (BN_ULONG)ll & BN_MASK2; ll >>= BN_BITS2; ll += (BN_ULLONG) a[1] + b[1]; r[1] = (BN_ULONG)ll & BN_MASK2; ll >>= BN_BITS2; ll += (BN_ULLONG) a[2] + b[2]; r[2] = (BN_ULONG)ll & BN_MASK2; ll >>= BN_BITS2; ll += (BN_ULLONG) a[3] + b[3]; r[3] = (BN_ULONG)ll & BN_MASK2; ll >>= BN_BITS2; a += 4; b += 4; r += 4; n -= 4; } # endif while (n) { ll += (BN_ULLONG) a[0] + b[0]; r[0] = (BN_ULONG)ll & BN_MASK2; ll >>= BN_BITS2; a++; b++; r++; n--; } return (BN_ULONG)ll; } #else BN_ULONG bn_add_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b, int n) { BN_ULONG c, l, t; assert(n >= 0); if (n <= 0) return (BN_ULONG)0; c = 0; # ifndef OPENSSL_SMALL_FOOTPRINT while (n & ~3) { t = a[0]; t = (t + c) & BN_MASK2; c = (t < c); l = (t + b[0]) & BN_MASK2; c += (l < t); r[0] = l; t = a[1]; t = (t + c) & BN_MASK2; c = (t < c); l = (t + b[1]) & BN_MASK2; c += (l < t); r[1] = l; t = a[2]; t = (t + c) & BN_MASK2; c = (t < c); l = (t + b[2]) & BN_MASK2; c += (l < t); r[2] = l; t = a[3]; t = (t + c) & BN_MASK2; c = (t < c); l = (t + b[3]) & BN_MASK2; c += (l < t); r[3] = l; a += 4; b += 4; r += 4; n -= 4; } # endif while (n) { t = a[0]; t = (t + c) & BN_MASK2; c = (t < c); l = (t + b[0]) & BN_MASK2; c += (l < t); r[0] = l; a++; b++; r++; n--; } return (BN_ULONG)c; } #endif BN_ULONG bn_sub_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b, int n) { BN_ULONG t1, t2; int c = 0; assert(n >= 0); if (n <= 0) return (BN_ULONG)0; #ifndef OPENSSL_SMALL_FOOTPRINT while (n & ~3) { t1 = a[0]; t2 = (t1 - c) & BN_MASK2; c = (t2 > t1); t1 = b[0]; t1 = (t2 - t1) & BN_MASK2; r[0] = t1; c += (t1 > t2); t1 = a[1]; t2 = (t1 - c) & BN_MASK2; c = (t2 > t1); t1 = b[1]; t1 = (t2 - t1) & BN_MASK2; r[1] = t1; c += (t1 > t2); t1 = a[2]; t2 = (t1 - c) & BN_MASK2; c = (t2 > t1); t1 = b[2]; t1 = (t2 - t1) & BN_MASK2; r[2] = t1; c += (t1 > t2); t1 = a[3]; t2 = (t1 - c) & BN_MASK2; c = (t2 > t1); t1 = b[3]; t1 = (t2 - t1) & BN_MASK2; r[3] = t1; c += (t1 > t2); a += 4; b += 4; r += 4; n -= 4; } #endif while (n) { t1 = a[0]; t2 = (t1 - c) & BN_MASK2; c = (t2 > t1); t1 = b[0]; t1 = (t2 - t1) & BN_MASK2; r[0] = t1; c += (t1 > t2); a++; b++; r++; n--; } return c; } #if defined(BN_MUL_COMBA) && !defined(OPENSSL_SMALL_FOOTPRINT) # ifdef BN_LLONG # define mul_add_c(a,b,c0,c1,c2) do { \ BN_ULONG hi; \ BN_ULLONG t = (BN_ULLONG)(a)*(b); \ t += c0; \ c0 = (BN_ULONG)Lw(t); \ hi = (BN_ULONG)Hw(t); \ c1 = (c1+hi)&BN_MASK2; c2 += (c1<hi); \ } while(0) # define mul_add_c2(a,b,c0,c1,c2) do { \ BN_ULONG hi; \ BN_ULLONG t = (BN_ULLONG)(a)*(b); \ BN_ULLONG tt = t+c0; \ c0 = (BN_ULONG)Lw(tt); \ hi = (BN_ULONG)Hw(tt); \ c1 = (c1+hi)&BN_MASK2; c2 += (c1<hi); \ t += c0; \ c0 = (BN_ULONG)Lw(t); \ hi = (BN_ULONG)Hw(t); \ c1 = (c1+hi)&BN_MASK2; c2 += (c1<hi); \ } while(0) # define sqr_add_c(a,i,c0,c1,c2) do { \ BN_ULONG hi; \ BN_ULLONG t = (BN_ULLONG)a[i]*a[i]; \ t += c0; \ c0 = (BN_ULONG)Lw(t); \ hi = (BN_ULONG)Hw(t); \ c1 = (c1+hi)&BN_MASK2; c2 += (c1<hi); \ } while(0) # define sqr_add_c2(a,i,j,c0,c1,c2) \ mul_add_c2((a)[i],(a)[j],c0,c1,c2) # elif defined(BN_UMULT_LOHI) # define mul_add_c(a,b,c0,c1,c2) do { \ BN_ULONG ta = (a), tb = (b); \ BN_ULONG lo, hi; \ BN_UMULT_LOHI(lo,hi,ta,tb); \ c0 += lo; hi += (c0<lo); \ c1 += hi; c2 += (c1<hi); \ } while(0) # define mul_add_c2(a,b,c0,c1,c2) do { \ BN_ULONG ta = (a), tb = (b); \ BN_ULONG lo, hi, tt; \ BN_UMULT_LOHI(lo,hi,ta,tb); \ c0 += lo; tt = hi + (c0<lo); \ c1 += tt; c2 += (c1<tt); \ c0 += lo; hi += (c0<lo); \ c1 += hi; c2 += (c1<hi); \ } while(0) # define sqr_add_c(a,i,c0,c1,c2) do { \ BN_ULONG ta = (a)[i]; \ BN_ULONG lo, hi; \ BN_UMULT_LOHI(lo,hi,ta,ta); \ c0 += lo; hi += (c0<lo); \ c1 += hi; c2 += (c1<hi); \ } while(0) # define sqr_add_c2(a,i,j,c0,c1,c2) \ mul_add_c2((a)[i],(a)[j],c0,c1,c2) # elif defined(BN_UMULT_HIGH) # define mul_add_c(a,b,c0,c1,c2) do { \ BN_ULONG ta = (a), tb = (b); \ BN_ULONG lo = ta * tb; \ BN_ULONG hi = BN_UMULT_HIGH(ta,tb); \ c0 += lo; hi += (c0<lo); \ c1 += hi; c2 += (c1<hi); \ } while(0) # define mul_add_c2(a,b,c0,c1,c2) do { \ BN_ULONG ta = (a), tb = (b), tt; \ BN_ULONG lo = ta * tb; \ BN_ULONG hi = BN_UMULT_HIGH(ta,tb); \ c0 += lo; tt = hi + (c0<lo); \ c1 += tt; c2 += (c1<tt); \ c0 += lo; hi += (c0<lo); \ c1 += hi; c2 += (c1<hi); \ } while(0) # define sqr_add_c(a,i,c0,c1,c2) do { \ BN_ULONG ta = (a)[i]; \ BN_ULONG lo = ta * ta; \ BN_ULONG hi = BN_UMULT_HIGH(ta,ta); \ c0 += lo; hi += (c0<lo); \ c1 += hi; c2 += (c1<hi); \ } while(0) # define sqr_add_c2(a,i,j,c0,c1,c2) \ mul_add_c2((a)[i],(a)[j],c0,c1,c2) # else # define mul_add_c(a,b,c0,c1,c2) do { \ BN_ULONG lo = LBITS(a), hi = HBITS(a); \ BN_ULONG bl = LBITS(b), bh = HBITS(b); \ mul64(lo,hi,bl,bh); \ c0 = (c0+lo)&BN_MASK2; hi += (c0<lo); \ c1 = (c1+hi)&BN_MASK2; c2 += (c1<hi); \ } while(0) # define mul_add_c2(a,b,c0,c1,c2) do { \ BN_ULONG tt; \ BN_ULONG lo = LBITS(a), hi = HBITS(a); \ BN_ULONG bl = LBITS(b), bh = HBITS(b); \ mul64(lo,hi,bl,bh); \ tt = hi; \ c0 = (c0+lo)&BN_MASK2; tt += (c0<lo); \ c1 = (c1+tt)&BN_MASK2; c2 += (c1<tt); \ c0 = (c0+lo)&BN_MASK2; hi += (c0<lo); \ c1 = (c1+hi)&BN_MASK2; c2 += (c1<hi); \ } while(0) # define sqr_add_c(a,i,c0,c1,c2) do { \ BN_ULONG lo, hi; \ sqr64(lo,hi,(a)[i]); \ c0 = (c0+lo)&BN_MASK2; hi += (c0<lo); \ c1 = (c1+hi)&BN_MASK2; c2 += (c1<hi); \ } while(0) # define sqr_add_c2(a,i,j,c0,c1,c2) \ mul_add_c2((a)[i],(a)[j],c0,c1,c2) # endif void bn_mul_comba8(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b) { BN_ULONG c1, c2, c3; c1 = 0; c2 = 0; c3 = 0; mul_add_c(a[0], b[0], c1, c2, c3); r[0] = c1; c1 = 0; mul_add_c(a[0], b[1], c2, c3, c1); mul_add_c(a[1], b[0], c2, c3, c1); r[1] = c2; c2 = 0; mul_add_c(a[2], b[0], c3, c1, c2); mul_add_c(a[1], b[1], c3, c1, c2); mul_add_c(a[0], b[2], c3, c1, c2); r[2] = c3; c3 = 0; mul_add_c(a[0], b[3], c1, c2, c3); mul_add_c(a[1], b[2], c1, c2, c3); mul_add_c(a[2], b[1], c1, c2, c3); mul_add_c(a[3], b[0], c1, c2, c3); r[3] = c1; c1 = 0; mul_add_c(a[4], b[0], c2, c3, c1); mul_add_c(a[3], b[1], c2, c3, c1); mul_add_c(a[2], b[2], c2, c3, c1); mul_add_c(a[1], b[3], c2, c3, c1); mul_add_c(a[0], b[4], c2, c3, c1); r[4] = c2; c2 = 0; mul_add_c(a[0], b[5], c3, c1, c2); mul_add_c(a[1], b[4], c3, c1, c2); mul_add_c(a[2], b[3], c3, c1, c2); mul_add_c(a[3], b[2], c3, c1, c2); mul_add_c(a[4], b[1], c3, c1, c2); mul_add_c(a[5], b[0], c3, c1, c2); r[5] = c3; c3 = 0; mul_add_c(a[6], b[0], c1, c2, c3); mul_add_c(a[5], b[1], c1, c2, c3); mul_add_c(a[4], b[2], c1, c2, c3); mul_add_c(a[3], b[3], c1, c2, c3); mul_add_c(a[2], b[4], c1, c2, c3); mul_add_c(a[1], b[5], c1, c2, c3); mul_add_c(a[0], b[6], c1, c2, c3); r[6] = c1; c1 = 0; mul_add_c(a[0], b[7], c2, c3, c1); mul_add_c(a[1], b[6], c2, c3, c1); mul_add_c(a[2], b[5], c2, c3, c1); mul_add_c(a[3], b[4], c2, c3, c1); mul_add_c(a[4], b[3], c2, c3, c1); mul_add_c(a[5], b[2], c2, c3, c1); mul_add_c(a[6], b[1], c2, c3, c1); mul_add_c(a[7], b[0], c2, c3, c1); r[7] = c2; c2 = 0; mul_add_c(a[7], b[1], c3, c1, c2); mul_add_c(a[6], b[2], c3, c1, c2); mul_add_c(a[5], b[3], c3, c1, c2); mul_add_c(a[4], b[4], c3, c1, c2); mul_add_c(a[3], b[5], c3, c1, c2); mul_add_c(a[2], b[6], c3, c1, c2); mul_add_c(a[1], b[7], c3, c1, c2); r[8] = c3; c3 = 0; mul_add_c(a[2], b[7], c1, c2, c3); mul_add_c(a[3], b[6], c1, c2, c3); mul_add_c(a[4], b[5], c1, c2, c3); mul_add_c(a[5], b[4], c1, c2, c3); mul_add_c(a[6], b[3], c1, c2, c3); mul_add_c(a[7], b[2], c1, c2, c3); r[9] = c1; c1 = 0; mul_add_c(a[7], b[3], c2, c3, c1); mul_add_c(a[6], b[4], c2, c3, c1); mul_add_c(a[5], b[5], c2, c3, c1); mul_add_c(a[4], b[6], c2, c3, c1); mul_add_c(a[3], b[7], c2, c3, c1); r[10] = c2; c2 = 0; mul_add_c(a[4], b[7], c3, c1, c2); mul_add_c(a[5], b[6], c3, c1, c2); mul_add_c(a[6], b[5], c3, c1, c2); mul_add_c(a[7], b[4], c3, c1, c2); r[11] = c3; c3 = 0; mul_add_c(a[7], b[5], c1, c2, c3); mul_add_c(a[6], b[6], c1, c2, c3); mul_add_c(a[5], b[7], c1, c2, c3); r[12] = c1; c1 = 0; mul_add_c(a[6], b[7], c2, c3, c1); mul_add_c(a[7], b[6], c2, c3, c1); r[13] = c2; c2 = 0; mul_add_c(a[7], b[7], c3, c1, c2); r[14] = c3; r[15] = c1; } void bn_mul_comba4(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b) { BN_ULONG c1, c2, c3; c1 = 0; c2 = 0; c3 = 0; mul_add_c(a[0], b[0], c1, c2, c3); r[0] = c1; c1 = 0; mul_add_c(a[0], b[1], c2, c3, c1); mul_add_c(a[1], b[0], c2, c3, c1); r[1] = c2; c2 = 0; mul_add_c(a[2], b[0], c3, c1, c2); mul_add_c(a[1], b[1], c3, c1, c2); mul_add_c(a[0], b[2], c3, c1, c2); r[2] = c3; c3 = 0; mul_add_c(a[0], b[3], c1, c2, c3); mul_add_c(a[1], b[2], c1, c2, c3); mul_add_c(a[2], b[1], c1, c2, c3); mul_add_c(a[3], b[0], c1, c2, c3); r[3] = c1; c1 = 0; mul_add_c(a[3], b[1], c2, c3, c1); mul_add_c(a[2], b[2], c2, c3, c1); mul_add_c(a[1], b[3], c2, c3, c1); r[4] = c2; c2 = 0; mul_add_c(a[2], b[3], c3, c1, c2); mul_add_c(a[3], b[2], c3, c1, c2); r[5] = c3; c3 = 0; mul_add_c(a[3], b[3], c1, c2, c3); r[6] = c1; r[7] = c2; } void bn_sqr_comba8(BN_ULONG *r, const BN_ULONG *a) { BN_ULONG c1, c2, c3; c1 = 0; c2 = 0; c3 = 0; sqr_add_c(a, 0, c1, c2, c3); r[0] = c1; c1 = 0; sqr_add_c2(a, 1, 0, c2, c3, c1); r[1] = c2; c2 = 0; sqr_add_c(a, 1, c3, c1, c2); sqr_add_c2(a, 2, 0, c3, c1, c2); r[2] = c3; c3 = 0; sqr_add_c2(a, 3, 0, c1, c2, c3); sqr_add_c2(a, 2, 1, c1, c2, c3); r[3] = c1; c1 = 0; sqr_add_c(a, 2, c2, c3, c1); sqr_add_c2(a, 3, 1, c2, c3, c1); sqr_add_c2(a, 4, 0, c2, c3, c1); r[4] = c2; c2 = 0; sqr_add_c2(a, 5, 0, c3, c1, c2); sqr_add_c2(a, 4, 1, c3, c1, c2); sqr_add_c2(a, 3, 2, c3, c1, c2); r[5] = c3; c3 = 0; sqr_add_c(a, 3, c1, c2, c3); sqr_add_c2(a, 4, 2, c1, c2, c3); sqr_add_c2(a, 5, 1, c1, c2, c3); sqr_add_c2(a, 6, 0, c1, c2, c3); r[6] = c1; c1 = 0; sqr_add_c2(a, 7, 0, c2, c3, c1); sqr_add_c2(a, 6, 1, c2, c3, c1); sqr_add_c2(a, 5, 2, c2, c3, c1); sqr_add_c2(a, 4, 3, c2, c3, c1); r[7] = c2; c2 = 0; sqr_add_c(a, 4, c3, c1, c2); sqr_add_c2(a, 5, 3, c3, c1, c2); sqr_add_c2(a, 6, 2, c3, c1, c2); sqr_add_c2(a, 7, 1, c3, c1, c2); r[8] = c3; c3 = 0; sqr_add_c2(a, 7, 2, c1, c2, c3); sqr_add_c2(a, 6, 3, c1, c2, c3); sqr_add_c2(a, 5, 4, c1, c2, c3); r[9] = c1; c1 = 0; sqr_add_c(a, 5, c2, c3, c1); sqr_add_c2(a, 6, 4, c2, c3, c1); sqr_add_c2(a, 7, 3, c2, c3, c1); r[10] = c2; c2 = 0; sqr_add_c2(a, 7, 4, c3, c1, c2); sqr_add_c2(a, 6, 5, c3, c1, c2); r[11] = c3; c3 = 0; sqr_add_c(a, 6, c1, c2, c3); sqr_add_c2(a, 7, 5, c1, c2, c3); r[12] = c1; c1 = 0; sqr_add_c2(a, 7, 6, c2, c3, c1); r[13] = c2; c2 = 0; sqr_add_c(a, 7, c3, c1, c2); r[14] = c3; r[15] = c1; } void bn_sqr_comba4(BN_ULONG *r, const BN_ULONG *a) { BN_ULONG c1, c2, c3; c1 = 0; c2 = 0; c3 = 0; sqr_add_c(a, 0, c1, c2, c3); r[0] = c1; c1 = 0; sqr_add_c2(a, 1, 0, c2, c3, c1); r[1] = c2; c2 = 0; sqr_add_c(a, 1, c3, c1, c2); sqr_add_c2(a, 2, 0, c3, c1, c2); r[2] = c3; c3 = 0; sqr_add_c2(a, 3, 0, c1, c2, c3); sqr_add_c2(a, 2, 1, c1, c2, c3); r[3] = c1; c1 = 0; sqr_add_c(a, 2, c2, c3, c1); sqr_add_c2(a, 3, 1, c2, c3, c1); r[4] = c2; c2 = 0; sqr_add_c2(a, 3, 2, c3, c1, c2); r[5] = c3; c3 = 0; sqr_add_c(a, 3, c1, c2, c3); r[6] = c1; r[7] = c2; } # ifdef OPENSSL_NO_ASM # ifdef OPENSSL_BN_ASM_MONT # include <alloca.h> int bn_mul_mont(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp, const BN_ULONG *np, const BN_ULONG *n0p, int num) { BN_ULONG c0, c1, ml, *tp, n0; # ifdef mul64 BN_ULONG mh; # endif volatile BN_ULONG *vp; int i = 0, j; # if 0 if (ap == bp) return bn_sqr_mont(rp, ap, np, n0p, num); # endif vp = tp = alloca((num + 2) * sizeof(BN_ULONG)); n0 = *n0p; c0 = 0; ml = bp[0]; # ifdef mul64 mh = HBITS(ml); ml = LBITS(ml); for (j = 0; j < num; ++j) mul(tp[j], ap[j], ml, mh, c0); # else for (j = 0; j < num; ++j) mul(tp[j], ap[j], ml, c0); # endif tp[num] = c0; tp[num + 1] = 0; goto enter; for (i = 0; i < num; i++) { c0 = 0; ml = bp[i]; # ifdef mul64 mh = HBITS(ml); ml = LBITS(ml); for (j = 0; j < num; ++j) mul_add(tp[j], ap[j], ml, mh, c0); # else for (j = 0; j < num; ++j) mul_add(tp[j], ap[j], ml, c0); # endif c1 = (tp[num] + c0) & BN_MASK2; tp[num] = c1; tp[num + 1] = (c1 < c0 ? 1 : 0); enter: c1 = tp[0]; ml = (c1 * n0) & BN_MASK2; c0 = 0; # ifdef mul64 mh = HBITS(ml); ml = LBITS(ml); mul_add(c1, np[0], ml, mh, c0); # else mul_add(c1, ml, np[0], c0); # endif for (j = 1; j < num; j++) { c1 = tp[j]; # ifdef mul64 mul_add(c1, np[j], ml, mh, c0); # else mul_add(c1, ml, np[j], c0); # endif tp[j - 1] = c1 & BN_MASK2; } c1 = (tp[num] + c0) & BN_MASK2; tp[num - 1] = c1; tp[num] = tp[num + 1] + (c1 < c0 ? 1 : 0); } if (tp[num] != 0 || tp[num - 1] >= np[num - 1]) { c0 = bn_sub_words(rp, tp, np, num); if (tp[num] != 0 || c0 == 0) { for (i = 0; i < num + 2; i++) vp[i] = 0; return 1; } } for (i = 0; i < num; i++) rp[i] = tp[i], vp[i] = 0; vp[num] = 0; vp[num + 1] = 0; return 1; } # else int bn_mul_mont(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp, const BN_ULONG *np, const BN_ULONG *n0, int num) { return 0; } # endif # endif #else void bn_sqr_comba4(BN_ULONG *r, const BN_ULONG *a) { BN_ULONG t[8]; bn_sqr_normal(r, a, 4, t); } void bn_sqr_comba8(BN_ULONG *r, const BN_ULONG *a) { BN_ULONG t[16]; bn_sqr_normal(r, a, 8, t); } void bn_mul_comba4(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b) { r[4] = bn_mul_words(&(r[0]), a, 4, b[0]); r[5] = bn_mul_add_words(&(r[1]), a, 4, b[1]); r[6] = bn_mul_add_words(&(r[2]), a, 4, b[2]); r[7] = bn_mul_add_words(&(r[3]), a, 4, b[3]); } void bn_mul_comba8(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b) { r[8] = bn_mul_words(&(r[0]), a, 8, b[0]); r[9] = bn_mul_add_words(&(r[1]), a, 8, b[1]); r[10] = bn_mul_add_words(&(r[2]), a, 8, b[2]); r[11] = bn_mul_add_words(&(r[3]), a, 8, b[3]); r[12] = bn_mul_add_words(&(r[4]), a, 8, b[4]); r[13] = bn_mul_add_words(&(r[5]), a, 8, b[5]); r[14] = bn_mul_add_words(&(r[6]), a, 8, b[6]); r[15] = bn_mul_add_words(&(r[7]), a, 8, b[7]); } # ifdef OPENSSL_NO_ASM # ifdef OPENSSL_BN_ASM_MONT # include <alloca.h> int bn_mul_mont(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp, const BN_ULONG *np, const BN_ULONG *n0p, int num) { BN_ULONG c0, c1, *tp, n0 = *n0p; volatile BN_ULONG *vp; int i = 0, j; vp = tp = alloca((num + 2) * sizeof(BN_ULONG)); for (i = 0; i <= num; i++) tp[i] = 0; for (i = 0; i < num; i++) { c0 = bn_mul_add_words(tp, ap, num, bp[i]); c1 = (tp[num] + c0) & BN_MASK2; tp[num] = c1; tp[num + 1] = (c1 < c0 ? 1 : 0); c0 = bn_mul_add_words(tp, np, num, tp[0] * n0); c1 = (tp[num] + c0) & BN_MASK2; tp[num] = c1; tp[num + 1] += (c1 < c0 ? 1 : 0); for (j = 0; j <= num; j++) tp[j] = tp[j + 1]; } if (tp[num] != 0 || tp[num - 1] >= np[num - 1]) { c0 = bn_sub_words(rp, tp, np, num); if (tp[num] != 0 || c0 == 0) { for (i = 0; i < num + 2; i++) vp[i] = 0; return 1; } } for (i = 0; i < num; i++) rp[i] = tp[i], vp[i] = 0; vp[num] = 0; vp[num + 1] = 0; return 1; } # else int bn_mul_mont(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp, const BN_ULONG *np, const BN_ULONG *n0, int num) { return 0; } # endif # endif #endif
bn
openssl/crypto/bn/bn_asm.c
openssl
#include "internal/cryptlib.h" #include "bn_local.h" int BN_nnmod(BIGNUM *r, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx) { if (r == d) { ERR_raise(ERR_LIB_BN, ERR_R_PASSED_INVALID_ARGUMENT); return 0; } if (!(BN_mod(r, m, d, ctx))) return 0; if (!r->neg) return 1; return (d->neg ? BN_sub : BN_add) (r, r, d); } int BN_mod_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m, BN_CTX *ctx) { if (!BN_add(r, a, b)) return 0; return BN_nnmod(r, r, m, ctx); } int bn_mod_add_fixed_top(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m) { size_t i, ai, bi, mtop = m->top; BN_ULONG storage[1024 / BN_BITS2]; BN_ULONG carry, temp, mask, *rp, *tp = storage; const BN_ULONG *ap, *bp; if (bn_wexpand(r, mtop) == NULL) return 0; if (mtop > sizeof(storage) / sizeof(storage[0])) { tp = OPENSSL_malloc(mtop * sizeof(BN_ULONG)); if (tp == NULL) return 0; } ap = a->d != NULL ? a->d : tp; bp = b->d != NULL ? b->d : tp; for (i = 0, ai = 0, bi = 0, carry = 0; i < mtop;) { mask = (BN_ULONG)0 - ((i - a->top) >> (8 * sizeof(i) - 1)); temp = ((ap[ai] & mask) + carry) & BN_MASK2; carry = (temp < carry); mask = (BN_ULONG)0 - ((i - b->top) >> (8 * sizeof(i) - 1)); tp[i] = ((bp[bi] & mask) + temp) & BN_MASK2; carry += (tp[i] < temp); i++; ai += (i - a->dmax) >> (8 * sizeof(i) - 1); bi += (i - b->dmax) >> (8 * sizeof(i) - 1); } rp = r->d; carry -= bn_sub_words(rp, tp, m->d, mtop); for (i = 0; i < mtop; i++) { rp[i] = (carry & tp[i]) | (~carry & rp[i]); ((volatile BN_ULONG *)tp)[i] = 0; } r->top = mtop; r->flags |= BN_FLG_FIXED_TOP; r->neg = 0; if (tp != storage) OPENSSL_free(tp); return 1; } int BN_mod_add_quick(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m) { int ret = bn_mod_add_fixed_top(r, a, b, m); if (ret) bn_correct_top(r); return ret; } int BN_mod_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m, BN_CTX *ctx) { if (!BN_sub(r, a, b)) return 0; return BN_nnmod(r, r, m, ctx); } int bn_mod_sub_fixed_top(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m) { size_t i, ai, bi, mtop = m->top; BN_ULONG borrow, carry, ta, tb, mask, *rp; const BN_ULONG *ap, *bp; if (bn_wexpand(r, mtop) == NULL) return 0; rp = r->d; ap = a->d != NULL ? a->d : rp; bp = b->d != NULL ? b->d : rp; for (i = 0, ai = 0, bi = 0, borrow = 0; i < mtop;) { mask = (BN_ULONG)0 - ((i - a->top) >> (8 * sizeof(i) - 1)); ta = ap[ai] & mask; mask = (BN_ULONG)0 - ((i - b->top) >> (8 * sizeof(i) - 1)); tb = bp[bi] & mask; rp[i] = ta - tb - borrow; if (ta != tb) borrow = (ta < tb); i++; ai += (i - a->dmax) >> (8 * sizeof(i) - 1); bi += (i - b->dmax) >> (8 * sizeof(i) - 1); } ap = m->d; for (i = 0, mask = 0 - borrow, carry = 0; i < mtop; i++) { ta = ((ap[i] & mask) + carry) & BN_MASK2; carry = (ta < carry); rp[i] = (rp[i] + ta) & BN_MASK2; carry += (rp[i] < ta); } borrow -= carry; for (i = 0, mask = 0 - borrow, carry = 0; i < mtop; i++) { ta = ((ap[i] & mask) + carry) & BN_MASK2; carry = (ta < carry); rp[i] = (rp[i] + ta) & BN_MASK2; carry += (rp[i] < ta); } r->top = mtop; r->flags |= BN_FLG_FIXED_TOP; r->neg = 0; return 1; } int BN_mod_sub_quick(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m) { if (r == m) { ERR_raise(ERR_LIB_BN, ERR_R_PASSED_INVALID_ARGUMENT); return 0; } if (!BN_sub(r, a, b)) return 0; if (r->neg) return BN_add(r, r, m); return 1; } int BN_mod_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m, BN_CTX *ctx) { BIGNUM *t; int ret = 0; bn_check_top(a); bn_check_top(b); bn_check_top(m); BN_CTX_start(ctx); if ((t = BN_CTX_get(ctx)) == NULL) goto err; if (a == b) { if (!BN_sqr(t, a, ctx)) goto err; } else { if (!BN_mul(t, a, b, ctx)) goto err; } if (!BN_nnmod(r, t, m, ctx)) goto err; bn_check_top(r); ret = 1; err: BN_CTX_end(ctx); return ret; } int BN_mod_sqr(BIGNUM *r, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx) { if (!BN_sqr(r, a, ctx)) return 0; return BN_mod(r, r, m, ctx); } int BN_mod_lshift1(BIGNUM *r, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx) { if (!BN_lshift1(r, a)) return 0; bn_check_top(r); return BN_nnmod(r, r, m, ctx); } int BN_mod_lshift1_quick(BIGNUM *r, const BIGNUM *a, const BIGNUM *m) { if (!BN_lshift1(r, a)) return 0; bn_check_top(r); if (BN_cmp(r, m) >= 0) return BN_sub(r, r, m); return 1; } int BN_mod_lshift(BIGNUM *r, const BIGNUM *a, int n, const BIGNUM *m, BN_CTX *ctx) { BIGNUM *abs_m = NULL; int ret; if (!BN_nnmod(r, a, m, ctx)) return 0; if (m->neg) { abs_m = BN_dup(m); if (abs_m == NULL) return 0; abs_m->neg = 0; } ret = BN_mod_lshift_quick(r, r, n, (abs_m ? abs_m : m)); bn_check_top(r); BN_free(abs_m); return ret; } int BN_mod_lshift_quick(BIGNUM *r, const BIGNUM *a, int n, const BIGNUM *m) { if (r != a) { if (BN_copy(r, a) == NULL) return 0; } while (n > 0) { int max_shift; max_shift = BN_num_bits(m) - BN_num_bits(r); if (max_shift < 0) { ERR_raise(ERR_LIB_BN, BN_R_INPUT_NOT_REDUCED); return 0; } if (max_shift > n) max_shift = n; if (max_shift) { if (!BN_lshift(r, r, max_shift)) return 0; n -= max_shift; } else { if (!BN_lshift1(r, r)) return 0; --n; } if (BN_cmp(r, m) >= 0) { if (!BN_sub(r, r, m)) return 0; } } bn_check_top(r); return 1; }
bn
openssl/crypto/bn/bn_mod.c
openssl
#include "internal/cryptlib.h" #include "bn_local.h" int BN_sqr(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx) { int ret = bn_sqr_fixed_top(r, a, ctx); bn_correct_top(r); bn_check_top(r); return ret; } int bn_sqr_fixed_top(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx) { int max, al; int ret = 0; BIGNUM *tmp, *rr; bn_check_top(a); al = a->top; if (al <= 0) { r->top = 0; r->neg = 0; return 1; } BN_CTX_start(ctx); rr = (a != r) ? r : BN_CTX_get(ctx); tmp = BN_CTX_get(ctx); if (rr == NULL || tmp == NULL) goto err; max = 2 * al; if (bn_wexpand(rr, max) == NULL) goto err; if (al == 4) { #ifndef BN_SQR_COMBA BN_ULONG t[8]; bn_sqr_normal(rr->d, a->d, 4, t); #else bn_sqr_comba4(rr->d, a->d); #endif } else if (al == 8) { #ifndef BN_SQR_COMBA BN_ULONG t[16]; bn_sqr_normal(rr->d, a->d, 8, t); #else bn_sqr_comba8(rr->d, a->d); #endif } else { #if defined(BN_RECURSION) if (al < BN_SQR_RECURSIVE_SIZE_NORMAL) { BN_ULONG t[BN_SQR_RECURSIVE_SIZE_NORMAL * 2]; bn_sqr_normal(rr->d, a->d, al, t); } else { int j, k; j = BN_num_bits_word((BN_ULONG)al); j = 1 << (j - 1); k = j + j; if (al == j) { if (bn_wexpand(tmp, k * 2) == NULL) goto err; bn_sqr_recursive(rr->d, a->d, al, tmp->d); } else { if (bn_wexpand(tmp, max) == NULL) goto err; bn_sqr_normal(rr->d, a->d, al, tmp->d); } } #else if (bn_wexpand(tmp, max) == NULL) goto err; bn_sqr_normal(rr->d, a->d, al, tmp->d); #endif } rr->neg = 0; rr->top = max; rr->flags |= BN_FLG_FIXED_TOP; if (r != rr && BN_copy(r, rr) == NULL) goto err; ret = 1; err: bn_check_top(rr); bn_check_top(tmp); BN_CTX_end(ctx); return ret; } void bn_sqr_normal(BN_ULONG *r, const BN_ULONG *a, int n, BN_ULONG *tmp) { int i, j, max; const BN_ULONG *ap; BN_ULONG *rp; max = n * 2; ap = a; rp = r; rp[0] = rp[max - 1] = 0; rp++; j = n; if (--j > 0) { ap++; rp[j] = bn_mul_words(rp, ap, j, ap[-1]); rp += 2; } for (i = n - 2; i > 0; i--) { j--; ap++; rp[j] = bn_mul_add_words(rp, ap, j, ap[-1]); rp += 2; } bn_add_words(r, r, r, max); bn_sqr_words(tmp, a, n); bn_add_words(r, r, tmp, max); } #ifdef BN_RECURSION void bn_sqr_recursive(BN_ULONG *r, const BN_ULONG *a, int n2, BN_ULONG *t) { int n = n2 / 2; int zero, c1; BN_ULONG ln, lo, *p; if (n2 == 4) { # ifndef BN_SQR_COMBA bn_sqr_normal(r, a, 4, t); # else bn_sqr_comba4(r, a); # endif return; } else if (n2 == 8) { # ifndef BN_SQR_COMBA bn_sqr_normal(r, a, 8, t); # else bn_sqr_comba8(r, a); # endif return; } if (n2 < BN_SQR_RECURSIVE_SIZE_NORMAL) { bn_sqr_normal(r, a, n2, t); return; } c1 = bn_cmp_words(a, &(a[n]), n); zero = 0; if (c1 > 0) bn_sub_words(t, a, &(a[n]), n); else if (c1 < 0) bn_sub_words(t, &(a[n]), a, n); else zero = 1; p = &(t[n2 * 2]); if (!zero) bn_sqr_recursive(&(t[n2]), t, n, p); else memset(&t[n2], 0, sizeof(*t) * n2); bn_sqr_recursive(r, a, n, p); bn_sqr_recursive(&(r[n2]), &(a[n]), n, p); c1 = (int)(bn_add_words(t, r, &(r[n2]), n2)); c1 -= (int)(bn_sub_words(&(t[n2]), t, &(t[n2]), n2)); c1 += (int)(bn_add_words(&(r[n]), &(r[n]), &(t[n2]), n2)); if (c1) { p = &(r[n + n2]); lo = *p; ln = (lo + c1) & BN_MASK2; *p = ln; if (ln < (BN_ULONG)c1) { do { p++; lo = *p; ln = (lo + 1) & BN_MASK2; *p = ln; } while (ln == 0); } } } #endif
bn
openssl/crypto/bn/bn_sqr.c
openssl
#include "internal/cryptlib.h" #include "bn_local.h" #define BN_lsw(n) (((n)->top == 0) ? (BN_ULONG) 0 : (n)->d[0]) int BN_kronecker(const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx) { int i; int ret = -2; int err = 0; BIGNUM *A, *B, *tmp; static const int tab[8] = { 0, 1, 0, -1, 0, -1, 0, 1 }; bn_check_top(a); bn_check_top(b); BN_CTX_start(ctx); A = BN_CTX_get(ctx); B = BN_CTX_get(ctx); if (B == NULL) goto end; err = !BN_copy(A, a); if (err) goto end; err = !BN_copy(B, b); if (err) goto end; if (BN_is_zero(B)) { ret = BN_abs_is_word(A, 1); goto end; } if (!BN_is_odd(A) && !BN_is_odd(B)) { ret = 0; goto end; } i = 0; while (!BN_is_bit_set(B, i)) i++; err = !BN_rshift(B, B, i); if (err) goto end; if (i & 1) { ret = tab[BN_lsw(A) & 7]; } else { ret = 1; } if (B->neg) { B->neg = 0; if (A->neg) ret = -ret; } while (1) { if (BN_is_zero(A)) { ret = BN_is_one(B) ? ret : 0; goto end; } i = 0; while (!BN_is_bit_set(A, i)) i++; err = !BN_rshift(A, A, i); if (err) goto end; if (i & 1) { ret = ret * tab[BN_lsw(B) & 7]; } if ((A->neg ? ~BN_lsw(A) : BN_lsw(A)) & BN_lsw(B) & 2) ret = -ret; err = !BN_nnmod(B, B, A, ctx); if (err) goto end; tmp = A; A = B; B = tmp; tmp->neg = 0; } end: BN_CTX_end(ctx); if (err) return -2; else return ret; }
bn
openssl/crypto/bn/bn_kron.c
openssl
#include <openssl/err.h> #include <openssl/bnerr.h> #include "crypto/bnerr.h" #ifndef OPENSSL_NO_ERR static const ERR_STRING_DATA BN_str_reasons[] = { {ERR_PACK(ERR_LIB_BN, 0, BN_R_ARG2_LT_ARG3), "arg2 lt arg3"}, {ERR_PACK(ERR_LIB_BN, 0, BN_R_BAD_RECIPROCAL), "bad reciprocal"}, {ERR_PACK(ERR_LIB_BN, 0, BN_R_BIGNUM_TOO_LONG), "bignum too long"}, {ERR_PACK(ERR_LIB_BN, 0, BN_R_BITS_TOO_SMALL), "bits too small"}, {ERR_PACK(ERR_LIB_BN, 0, BN_R_CALLED_WITH_EVEN_MODULUS), "called with even modulus"}, {ERR_PACK(ERR_LIB_BN, 0, BN_R_DIV_BY_ZERO), "div by zero"}, {ERR_PACK(ERR_LIB_BN, 0, BN_R_ENCODING_ERROR), "encoding error"}, {ERR_PACK(ERR_LIB_BN, 0, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA), "expand on static bignum data"}, {ERR_PACK(ERR_LIB_BN, 0, BN_R_INPUT_NOT_REDUCED), "input not reduced"}, {ERR_PACK(ERR_LIB_BN, 0, BN_R_INVALID_LENGTH), "invalid length"}, {ERR_PACK(ERR_LIB_BN, 0, BN_R_INVALID_RANGE), "invalid range"}, {ERR_PACK(ERR_LIB_BN, 0, BN_R_INVALID_SHIFT), "invalid shift"}, {ERR_PACK(ERR_LIB_BN, 0, BN_R_NOT_A_SQUARE), "not a square"}, {ERR_PACK(ERR_LIB_BN, 0, BN_R_NOT_INITIALIZED), "not initialized"}, {ERR_PACK(ERR_LIB_BN, 0, BN_R_NO_INVERSE), "no inverse"}, {ERR_PACK(ERR_LIB_BN, 0, BN_R_NO_PRIME_CANDIDATE), "no prime candidate"}, {ERR_PACK(ERR_LIB_BN, 0, BN_R_NO_SOLUTION), "no solution"}, {ERR_PACK(ERR_LIB_BN, 0, BN_R_NO_SUITABLE_DIGEST), "no suitable digest"}, {ERR_PACK(ERR_LIB_BN, 0, BN_R_PRIVATE_KEY_TOO_LARGE), "private key too large"}, {ERR_PACK(ERR_LIB_BN, 0, BN_R_P_IS_NOT_PRIME), "p is not prime"}, {ERR_PACK(ERR_LIB_BN, 0, BN_R_TOO_MANY_ITERATIONS), "too many iterations"}, {ERR_PACK(ERR_LIB_BN, 0, BN_R_TOO_MANY_TEMPORARY_VARIABLES), "too many temporary variables"}, {0, NULL} }; #endif int ossl_err_load_BN_strings(void) { #ifndef OPENSSL_NO_ERR if (ERR_reason_error_string(BN_str_reasons[0].error) == NULL) ERR_load_strings_const(BN_str_reasons); #endif return 1; }
bn
openssl/crypto/bn/bn_err.c
openssl
#include <assert.h> #include "internal/cryptlib.h" #include "bn_local.h" int BN_lshift1(BIGNUM *r, const BIGNUM *a) { register BN_ULONG *ap, *rp, t, c; int i; bn_check_top(r); bn_check_top(a); if (r != a) { r->neg = a->neg; if (bn_wexpand(r, a->top + 1) == NULL) return 0; r->top = a->top; } else { if (bn_wexpand(r, a->top + 1) == NULL) return 0; } ap = a->d; rp = r->d; c = 0; for (i = 0; i < a->top; i++) { t = *(ap++); *(rp++) = ((t << 1) | c) & BN_MASK2; c = t >> (BN_BITS2 - 1); } *rp = c; r->top += c; bn_check_top(r); return 1; } int BN_rshift1(BIGNUM *r, const BIGNUM *a) { BN_ULONG *ap, *rp, t, c; int i; bn_check_top(r); bn_check_top(a); if (BN_is_zero(a)) { BN_zero(r); return 1; } i = a->top; ap = a->d; if (a != r) { if (bn_wexpand(r, i) == NULL) return 0; r->neg = a->neg; } rp = r->d; r->top = i; t = ap[--i]; rp[i] = t >> 1; c = t << (BN_BITS2 - 1); r->top -= (t == 1); while (i > 0) { t = ap[--i]; rp[i] = ((t >> 1) & BN_MASK2) | c; c = t << (BN_BITS2 - 1); } if (!r->top) r->neg = 0; bn_check_top(r); return 1; } int BN_lshift(BIGNUM *r, const BIGNUM *a, int n) { int ret; if (n < 0) { ERR_raise(ERR_LIB_BN, BN_R_INVALID_SHIFT); return 0; } ret = bn_lshift_fixed_top(r, a, n); bn_correct_top(r); bn_check_top(r); return ret; } int bn_lshift_fixed_top(BIGNUM *r, const BIGNUM *a, int n) { int i, nw; unsigned int lb, rb; BN_ULONG *t, *f; BN_ULONG l, m, rmask = 0; assert(n >= 0); bn_check_top(r); bn_check_top(a); nw = n / BN_BITS2; if (bn_wexpand(r, a->top + nw + 1) == NULL) return 0; if (a->top != 0) { lb = (unsigned int)n % BN_BITS2; rb = BN_BITS2 - lb; rb %= BN_BITS2; rmask = (BN_ULONG)0 - rb; rmask |= rmask >> 8; f = &(a->d[0]); t = &(r->d[nw]); l = f[a->top - 1]; t[a->top] = (l >> rb) & rmask; for (i = a->top - 1; i > 0; i--) { m = l << lb; l = f[i - 1]; t[i] = (m | ((l >> rb) & rmask)) & BN_MASK2; } t[0] = (l << lb) & BN_MASK2; } else { r->d[nw] = 0; } if (nw != 0) memset(r->d, 0, sizeof(*t) * nw); r->neg = a->neg; r->top = a->top + nw + 1; r->flags |= BN_FLG_FIXED_TOP; return 1; } int BN_rshift(BIGNUM *r, const BIGNUM *a, int n) { int ret = 0; if (n < 0) { ERR_raise(ERR_LIB_BN, BN_R_INVALID_SHIFT); return 0; } ret = bn_rshift_fixed_top(r, a, n); bn_correct_top(r); bn_check_top(r); return ret; } int bn_rshift_fixed_top(BIGNUM *r, const BIGNUM *a, int n) { int i, top, nw; unsigned int lb, rb; BN_ULONG *t, *f; BN_ULONG l, m, mask; bn_check_top(r); bn_check_top(a); assert(n >= 0); nw = n / BN_BITS2; if (nw >= a->top) { BN_zero(r); return 1; } rb = (unsigned int)n % BN_BITS2; lb = BN_BITS2 - rb; lb %= BN_BITS2; mask = (BN_ULONG)0 - lb; mask |= mask >> 8; top = a->top - nw; if (r != a && bn_wexpand(r, top) == NULL) return 0; t = &(r->d[0]); f = &(a->d[nw]); l = f[0]; for (i = 0; i < top - 1; i++) { m = f[i + 1]; t[i] = (l >> rb) | ((m << lb) & mask); l = m; } t[i] = l >> rb; r->neg = a->neg; r->top = top; r->flags |= BN_FLG_FIXED_TOP; return 1; }
bn
openssl/crypto/bn/bn_shift.c
openssl
#include "internal/cryptlib.h" #include "bn_local.h" int BN_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b) { int ret, r_neg, cmp_res; bn_check_top(a); bn_check_top(b); if (a->neg == b->neg) { r_neg = a->neg; ret = BN_uadd(r, a, b); } else { cmp_res = BN_ucmp(a, b); if (cmp_res > 0) { r_neg = a->neg; ret = BN_usub(r, a, b); } else if (cmp_res < 0) { r_neg = b->neg; ret = BN_usub(r, b, a); } else { r_neg = 0; BN_zero(r); ret = 1; } } r->neg = r_neg; bn_check_top(r); return ret; } int BN_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b) { int ret, r_neg, cmp_res; bn_check_top(a); bn_check_top(b); if (a->neg != b->neg) { r_neg = a->neg; ret = BN_uadd(r, a, b); } else { cmp_res = BN_ucmp(a, b); if (cmp_res > 0) { r_neg = a->neg; ret = BN_usub(r, a, b); } else if (cmp_res < 0) { r_neg = !b->neg; ret = BN_usub(r, b, a); } else { r_neg = 0; BN_zero(r); ret = 1; } } r->neg = r_neg; bn_check_top(r); return ret; } int BN_uadd(BIGNUM *r, const BIGNUM *a, const BIGNUM *b) { int max, min, dif; const BN_ULONG *ap, *bp; BN_ULONG *rp, carry, t1, t2; bn_check_top(a); bn_check_top(b); if (a->top < b->top) { const BIGNUM *tmp; tmp = a; a = b; b = tmp; } max = a->top; min = b->top; dif = max - min; if (bn_wexpand(r, max + 1) == NULL) return 0; r->top = max; ap = a->d; bp = b->d; rp = r->d; carry = bn_add_words(rp, ap, bp, min); rp += min; ap += min; while (dif) { dif--; t1 = *(ap++); t2 = (t1 + carry) & BN_MASK2; *(rp++) = t2; carry &= (t2 == 0); } *rp = carry; r->top += carry; r->neg = 0; bn_check_top(r); return 1; } int BN_usub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b) { int max, min, dif; BN_ULONG t1, t2, borrow, *rp; const BN_ULONG *ap, *bp; bn_check_top(a); bn_check_top(b); max = a->top; min = b->top; dif = max - min; if (dif < 0) { ERR_raise(ERR_LIB_BN, BN_R_ARG2_LT_ARG3); return 0; } if (bn_wexpand(r, max) == NULL) return 0; ap = a->d; bp = b->d; rp = r->d; borrow = bn_sub_words(rp, ap, bp, min); ap += min; rp += min; while (dif) { dif--; t1 = *(ap++); t2 = (t1 - borrow) & BN_MASK2; *(rp++) = t2; borrow &= (t1 == 0); } while (max && *--rp == 0) max--; r->top = max; r->neg = 0; bn_pollute(r); return 1; }
bn
openssl/crypto/bn/bn_add.c
openssl
#include <openssl/opensslconf.h> #include <stdio.h> #include <time.h> #include "internal/cryptlib.h" #include "bn_local.h" BIGNUM *BN_generate_prime(BIGNUM *ret, int bits, int safe, const BIGNUM *add, const BIGNUM *rem, void (*callback) (int, int, void *), void *cb_arg) { BN_GENCB cb; BIGNUM *rnd = NULL; BN_GENCB_set_old(&cb, callback, cb_arg); if (ret == NULL) { if ((rnd = BN_new()) == NULL) goto err; } else rnd = ret; if (!BN_generate_prime_ex(rnd, bits, safe, add, rem, &cb)) goto err; return rnd; err: BN_free(rnd); return NULL; } int BN_is_prime(const BIGNUM *a, int checks, void (*callback) (int, int, void *), BN_CTX *ctx_passed, void *cb_arg) { BN_GENCB cb; BN_GENCB_set_old(&cb, callback, cb_arg); return ossl_bn_check_prime(a, checks, ctx_passed, 0, &cb); } int BN_is_prime_fasttest(const BIGNUM *a, int checks, void (*callback) (int, int, void *), BN_CTX *ctx_passed, void *cb_arg, int do_trial_division) { BN_GENCB cb; BN_GENCB_set_old(&cb, callback, cb_arg); return ossl_bn_check_prime(a, checks, ctx_passed, do_trial_division, &cb); }
bn
openssl/crypto/bn/bn_depr.c
openssl
#include "bn_local.h" #include "internal/nelem.h" #ifndef OPENSSL_NO_SRP #include <openssl/srp.h> #include "crypto/bn_srp.h" # if (BN_BYTES == 8) # if (defined(_WIN32) || defined(_WIN64)) && !defined(__MINGW32__) # define bn_pack4(a1,a2,a3,a4) ((a1##UI64<<48)|(a2##UI64<<32)|(a3##UI64<<16)|a4##UI64) # elif defined(__arch64__) # define bn_pack4(a1,a2,a3,a4) ((a1##UL<<48)|(a2##UL<<32)|(a3##UL<<16)|a4##UL) # else # define bn_pack4(a1,a2,a3,a4) ((a1##ULL<<48)|(a2##ULL<<32)|(a3##ULL<<16)|a4##ULL) # endif # elif (BN_BYTES == 4) # define bn_pack4(a1,a2,a3,a4) ((a3##UL<<16)|a4##UL), ((a1##UL<<16)|a2##UL) # else # error "unsupported BN_BYTES" # endif static const BN_ULONG bn_group_1024_value[] = { bn_pack4(0x9FC6, 0x1D2F, 0xC0EB, 0x06E3), bn_pack4(0xFD51, 0x38FE, 0x8376, 0x435B), bn_pack4(0x2FD4, 0xCBF4, 0x976E, 0xAA9A), bn_pack4(0x68ED, 0xBC3C, 0x0572, 0x6CC0), bn_pack4(0xC529, 0xF566, 0x660E, 0x57EC), bn_pack4(0x8255, 0x9B29, 0x7BCF, 0x1885), bn_pack4(0xCE8E, 0xF4AD, 0x69B1, 0x5D49), bn_pack4(0x5DC7, 0xD7B4, 0x6154, 0xD6B6), bn_pack4(0x8E49, 0x5C1D, 0x6089, 0xDAD1), bn_pack4(0xE0D5, 0xD8E2, 0x50B9, 0x8BE4), bn_pack4(0x383B, 0x4813, 0xD692, 0xC6E0), bn_pack4(0xD674, 0xDF74, 0x96EA, 0x81D3), bn_pack4(0x9EA2, 0x314C, 0x9C25, 0x6576), bn_pack4(0x6072, 0x6187, 0x75FF, 0x3C0B), bn_pack4(0x9C33, 0xF80A, 0xFA8F, 0xC5E8), bn_pack4(0xEEAF, 0x0AB9, 0xADB3, 0x8DD6) }; const BIGNUM ossl_bn_group_1024 = { (BN_ULONG *)bn_group_1024_value, OSSL_NELEM(bn_group_1024_value), OSSL_NELEM(bn_group_1024_value), 0, BN_FLG_STATIC_DATA }; static const BN_ULONG bn_group_1536_value[] = { bn_pack4(0xCF76, 0xE3FE, 0xD135, 0xF9BB), bn_pack4(0x1518, 0x0F93, 0x499A, 0x234D), bn_pack4(0x8CE7, 0xA28C, 0x2442, 0xC6F3), bn_pack4(0x5A02, 0x1FFF, 0x5E91, 0x479E), bn_pack4(0x7F8A, 0x2FE9, 0xB8B5, 0x292E), bn_pack4(0x837C, 0x264A, 0xE3A9, 0xBEB8), bn_pack4(0xE442, 0x734A, 0xF7CC, 0xB7AE), bn_pack4(0x6577, 0x2E43, 0x7D6C, 0x7F8C), bn_pack4(0xDB2F, 0xD53D, 0x24B7, 0xC486), bn_pack4(0x6EDF, 0x0195, 0x3934, 0x9627), bn_pack4(0x158B, 0xFD3E, 0x2B9C, 0x8CF5), bn_pack4(0x764E, 0x3F4B, 0x53DD, 0x9DA1), bn_pack4(0x4754, 0x8381, 0xDBC5, 0xB1FC), bn_pack4(0x9B60, 0x9E0B, 0xE3BA, 0xB63D), bn_pack4(0x8134, 0xB1C8, 0xB979, 0x8914), bn_pack4(0xDF02, 0x8A7C, 0xEC67, 0xF0D0), bn_pack4(0x80B6, 0x55BB, 0x9A22, 0xE8DC), bn_pack4(0x1558, 0x903B, 0xA0D0, 0xF843), bn_pack4(0x51C6, 0xA94B, 0xE460, 0x7A29), bn_pack4(0x5F4F, 0x5F55, 0x6E27, 0xCBDE), bn_pack4(0xBEEE, 0xA961, 0x4B19, 0xCC4D), bn_pack4(0xDBA5, 0x1DF4, 0x99AC, 0x4C80), bn_pack4(0xB1F1, 0x2A86, 0x17A4, 0x7BBB), bn_pack4(0x9DEF, 0x3CAF, 0xB939, 0x277A) }; const BIGNUM ossl_bn_group_1536 = { (BN_ULONG *)bn_group_1536_value, OSSL_NELEM(bn_group_1536_value), OSSL_NELEM(bn_group_1536_value), 0, BN_FLG_STATIC_DATA }; static const BN_ULONG bn_group_2048_value[] = { bn_pack4(0x0FA7, 0x111F, 0x9E4A, 0xFF73), bn_pack4(0x9B65, 0xE372, 0xFCD6, 0x8EF2), bn_pack4(0x35DE, 0x236D, 0x525F, 0x5475), bn_pack4(0x94B5, 0xC803, 0xD89F, 0x7AE4), bn_pack4(0x71AE, 0x35F8, 0xE9DB, 0xFBB6), bn_pack4(0x2A56, 0x98F3, 0xA8D0, 0xC382), bn_pack4(0x9CCC, 0x041C, 0x7BC3, 0x08D8), bn_pack4(0xAF87, 0x4E73, 0x03CE, 0x5329), bn_pack4(0x6160, 0x2790, 0x04E5, 0x7AE6), bn_pack4(0x032C, 0xFBDB, 0xF52F, 0xB378), bn_pack4(0x5EA7, 0x7A27, 0x75D2, 0xECFA), bn_pack4(0x5445, 0x23B5, 0x24B0, 0xD57D), bn_pack4(0x5B9D, 0x32E6, 0x88F8, 0x7748), bn_pack4(0xF1D2, 0xB907, 0x8717, 0x461A), bn_pack4(0x76BD, 0x207A, 0x436C, 0x6481), bn_pack4(0xCA97, 0xB43A, 0x23FB, 0x8016), bn_pack4(0x1D28, 0x1E44, 0x6B14, 0x773B), bn_pack4(0x7359, 0xD041, 0xD5C3, 0x3EA7), bn_pack4(0xA80D, 0x740A, 0xDBF4, 0xFF74), bn_pack4(0x55F9, 0x7993, 0xEC97, 0x5EEA), bn_pack4(0x2918, 0xA996, 0x2F0B, 0x93B8), bn_pack4(0x661A, 0x05FB, 0xD5FA, 0xAAE8), bn_pack4(0xCF60, 0x9517, 0x9A16, 0x3AB3), bn_pack4(0xE808, 0x3969, 0xEDB7, 0x67B0), bn_pack4(0xCD7F, 0x48A9, 0xDA04, 0xFD50), bn_pack4(0xD523, 0x12AB, 0x4B03, 0x310D), bn_pack4(0x8193, 0xE075, 0x7767, 0xA13D), bn_pack4(0xA373, 0x29CB, 0xB4A0, 0x99ED), bn_pack4(0xFC31, 0x9294, 0x3DB5, 0x6050), bn_pack4(0xAF72, 0xB665, 0x1987, 0xEE07), bn_pack4(0xF166, 0xDE5E, 0x1389, 0x582F), bn_pack4(0xAC6B, 0xDB41, 0x324A, 0x9A9B) }; const BIGNUM ossl_bn_group_2048 = { (BN_ULONG *)bn_group_2048_value, OSSL_NELEM(bn_group_2048_value), OSSL_NELEM(bn_group_2048_value), 0, BN_FLG_STATIC_DATA }; static const BN_ULONG bn_group_3072_value[] = { bn_pack4(0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF), bn_pack4(0x4B82, 0xD120, 0xA93A, 0xD2CA), bn_pack4(0x43DB, 0x5BFC, 0xE0FD, 0x108E), bn_pack4(0x08E2, 0x4FA0, 0x74E5, 0xAB31), bn_pack4(0x7709, 0x88C0, 0xBAD9, 0x46E2), bn_pack4(0xBBE1, 0x1757, 0x7A61, 0x5D6C), bn_pack4(0x521F, 0x2B18, 0x177B, 0x200C), bn_pack4(0xD876, 0x0273, 0x3EC8, 0x6A64), bn_pack4(0xF12F, 0xFA06, 0xD98A, 0x0864), bn_pack4(0xCEE3, 0xD226, 0x1AD2, 0xEE6B), bn_pack4(0x1E8C, 0x94E0, 0x4A25, 0x619D), bn_pack4(0xABF5, 0xAE8C, 0xDB09, 0x33D7), bn_pack4(0xB397, 0x0F85, 0xA6E1, 0xE4C7), bn_pack4(0x8AEA, 0x7157, 0x5D06, 0x0C7D), bn_pack4(0xECFB, 0x8504, 0x58DB, 0xEF0A), bn_pack4(0xA855, 0x21AB, 0xDF1C, 0xBA64), bn_pack4(0xAD33, 0x170D, 0x0450, 0x7A33), bn_pack4(0x1572, 0x8E5A, 0x8AAA, 0xC42D), bn_pack4(0x15D2, 0x2618, 0x98FA, 0x0510), bn_pack4(0x3995, 0x497C, 0xEA95, 0x6AE5), bn_pack4(0xDE2B, 0xCBF6, 0x9558, 0x1718), bn_pack4(0xB5C5, 0x5DF0, 0x6F4C, 0x52C9), bn_pack4(0x9B27, 0x83A2, 0xEC07, 0xA28F), bn_pack4(0xE39E, 0x772C, 0x180E, 0x8603), bn_pack4(0x3290, 0x5E46, 0x2E36, 0xCE3B), bn_pack4(0xF174, 0x6C08, 0xCA18, 0x217C), bn_pack4(0x670C, 0x354E, 0x4ABC, 0x9804), bn_pack4(0x9ED5, 0x2907, 0x7096, 0x966D), bn_pack4(0x1C62, 0xF356, 0x2085, 0x52BB), bn_pack4(0x8365, 0x5D23, 0xDCA3, 0xAD96), bn_pack4(0x6916, 0x3FA8, 0xFD24, 0xCF5F), bn_pack4(0x98DA, 0x4836, 0x1C55, 0xD39A), bn_pack4(0xC200, 0x7CB8, 0xA163, 0xBF05), bn_pack4(0x4928, 0x6651, 0xECE4, 0x5B3D), bn_pack4(0xAE9F, 0x2411, 0x7C4B, 0x1FE6), bn_pack4(0xEE38, 0x6BFB, 0x5A89, 0x9FA5), bn_pack4(0x0BFF, 0x5CB6, 0xF406, 0xB7ED), bn_pack4(0xF44C, 0x42E9, 0xA637, 0xED6B), bn_pack4(0xE485, 0xB576, 0x625E, 0x7EC6), bn_pack4(0x4FE1, 0x356D, 0x6D51, 0xC245), bn_pack4(0x302B, 0x0A6D, 0xF25F, 0x1437), bn_pack4(0xEF95, 0x19B3, 0xCD3A, 0x431B), bn_pack4(0x514A, 0x0879, 0x8E34, 0x04DD), bn_pack4(0x020B, 0xBEA6, 0x3B13, 0x9B22), bn_pack4(0x2902, 0x4E08, 0x8A67, 0xCC74), bn_pack4(0xC4C6, 0x628B, 0x80DC, 0x1CD1), bn_pack4(0xC90F, 0xDAA2, 0x2168, 0xC234), bn_pack4(0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF) }; const BIGNUM ossl_bn_group_3072 = { (BN_ULONG *)bn_group_3072_value, OSSL_NELEM(bn_group_3072_value), OSSL_NELEM(bn_group_3072_value), 0, BN_FLG_STATIC_DATA }; static const BN_ULONG bn_group_4096_value[] = { bn_pack4(0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF), bn_pack4(0x4DF4, 0x35C9, 0x3406, 0x3199), bn_pack4(0x86FF, 0xB7DC, 0x90A6, 0xC08F), bn_pack4(0x93B4, 0xEA98, 0x8D8F, 0xDDC1), bn_pack4(0xD006, 0x9127, 0xD5B0, 0x5AA9), bn_pack4(0xB81B, 0xDD76, 0x2170, 0x481C), bn_pack4(0x1F61, 0x2970, 0xCEE2, 0xD7AF), bn_pack4(0x233B, 0xA186, 0x515B, 0xE7ED), bn_pack4(0x99B2, 0x964F, 0xA090, 0xC3A2), bn_pack4(0x287C, 0x5947, 0x4E6B, 0xC05D), bn_pack4(0x2E8E, 0xFC14, 0x1FBE, 0xCAA6), bn_pack4(0xDBBB, 0xC2DB, 0x04DE, 0x8EF9), bn_pack4(0x2583, 0xE9CA, 0x2AD4, 0x4CE8), bn_pack4(0x1A94, 0x6834, 0xB615, 0x0BDA), bn_pack4(0x99C3, 0x2718, 0x6AF4, 0xE23C), bn_pack4(0x8871, 0x9A10, 0xBDBA, 0x5B26), bn_pack4(0x1A72, 0x3C12, 0xA787, 0xE6D7), bn_pack4(0x4B82, 0xD120, 0xA921, 0x0801), bn_pack4(0x43DB, 0x5BFC, 0xE0FD, 0x108E), bn_pack4(0x08E2, 0x4FA0, 0x74E5, 0xAB31), bn_pack4(0x7709, 0x88C0, 0xBAD9, 0x46E2), bn_pack4(0xBBE1, 0x1757, 0x7A61, 0x5D6C), bn_pack4(0x521F, 0x2B18, 0x177B, 0x200C), bn_pack4(0xD876, 0x0273, 0x3EC8, 0x6A64), bn_pack4(0xF12F, 0xFA06, 0xD98A, 0x0864), bn_pack4(0xCEE3, 0xD226, 0x1AD2, 0xEE6B), bn_pack4(0x1E8C, 0x94E0, 0x4A25, 0x619D), bn_pack4(0xABF5, 0xAE8C, 0xDB09, 0x33D7), bn_pack4(0xB397, 0x0F85, 0xA6E1, 0xE4C7), bn_pack4(0x8AEA, 0x7157, 0x5D06, 0x0C7D), bn_pack4(0xECFB, 0x8504, 0x58DB, 0xEF0A), bn_pack4(0xA855, 0x21AB, 0xDF1C, 0xBA64), bn_pack4(0xAD33, 0x170D, 0x0450, 0x7A33), bn_pack4(0x1572, 0x8E5A, 0x8AAA, 0xC42D), bn_pack4(0x15D2, 0x2618, 0x98FA, 0x0510), bn_pack4(0x3995, 0x497C, 0xEA95, 0x6AE5), bn_pack4(0xDE2B, 0xCBF6, 0x9558, 0x1718), bn_pack4(0xB5C5, 0x5DF0, 0x6F4C, 0x52C9), bn_pack4(0x9B27, 0x83A2, 0xEC07, 0xA28F), bn_pack4(0xE39E, 0x772C, 0x180E, 0x8603), bn_pack4(0x3290, 0x5E46, 0x2E36, 0xCE3B), bn_pack4(0xF174, 0x6C08, 0xCA18, 0x217C), bn_pack4(0x670C, 0x354E, 0x4ABC, 0x9804), bn_pack4(0x9ED5, 0x2907, 0x7096, 0x966D), bn_pack4(0x1C62, 0xF356, 0x2085, 0x52BB), bn_pack4(0x8365, 0x5D23, 0xDCA3, 0xAD96), bn_pack4(0x6916, 0x3FA8, 0xFD24, 0xCF5F), bn_pack4(0x98DA, 0x4836, 0x1C55, 0xD39A), bn_pack4(0xC200, 0x7CB8, 0xA163, 0xBF05), bn_pack4(0x4928, 0x6651, 0xECE4, 0x5B3D), bn_pack4(0xAE9F, 0x2411, 0x7C4B, 0x1FE6), bn_pack4(0xEE38, 0x6BFB, 0x5A89, 0x9FA5), bn_pack4(0x0BFF, 0x5CB6, 0xF406, 0xB7ED), bn_pack4(0xF44C, 0x42E9, 0xA637, 0xED6B), bn_pack4(0xE485, 0xB576, 0x625E, 0x7EC6), bn_pack4(0x4FE1, 0x356D, 0x6D51, 0xC245), bn_pack4(0x302B, 0x0A6D, 0xF25F, 0x1437), bn_pack4(0xEF95, 0x19B3, 0xCD3A, 0x431B), bn_pack4(0x514A, 0x0879, 0x8E34, 0x04DD), bn_pack4(0x020B, 0xBEA6, 0x3B13, 0x9B22), bn_pack4(0x2902, 0x4E08, 0x8A67, 0xCC74), bn_pack4(0xC4C6, 0x628B, 0x80DC, 0x1CD1), bn_pack4(0xC90F, 0xDAA2, 0x2168, 0xC234), bn_pack4(0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF) }; const BIGNUM ossl_bn_group_4096 = { (BN_ULONG *)bn_group_4096_value, OSSL_NELEM(bn_group_4096_value), OSSL_NELEM(bn_group_4096_value), 0, BN_FLG_STATIC_DATA }; static const BN_ULONG bn_group_6144_value[] = { bn_pack4(0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF), bn_pack4(0xE694, 0xF91E, 0x6DCC, 0x4024), bn_pack4(0x12BF, 0x2D5B, 0x0B74, 0x74D6), bn_pack4(0x043E, 0x8F66, 0x3F48, 0x60EE), bn_pack4(0x387F, 0xE8D7, 0x6E3C, 0x0468), bn_pack4(0xDA56, 0xC9EC, 0x2EF2, 0x9632), bn_pack4(0xEB19, 0xCCB1, 0xA313, 0xD55C), bn_pack4(0xF550, 0xAA3D, 0x8A1F, 0xBFF0), bn_pack4(0x06A1, 0xD58B, 0xB7C5, 0xDA76), bn_pack4(0xA797, 0x15EE, 0xF29B, 0xE328), bn_pack4(0x14CC, 0x5ED2, 0x0F80, 0x37E0), bn_pack4(0xCC8F, 0x6D7E, 0xBF48, 0xE1D8), bn_pack4(0x4BD4, 0x07B2, 0x2B41, 0x54AA), bn_pack4(0x0F1D, 0x45B7, 0xFF58, 0x5AC5), bn_pack4(0x23A9, 0x7A7E, 0x36CC, 0x88BE), bn_pack4(0x59E7, 0xC97F, 0xBEC7, 0xE8F3), bn_pack4(0xB5A8, 0x4031, 0x900B, 0x1C9E), bn_pack4(0xD55E, 0x702F, 0x4698, 0x0C82), bn_pack4(0xF482, 0xD7CE, 0x6E74, 0xFEF6), bn_pack4(0xF032, 0xEA15, 0xD172, 0x1D03), bn_pack4(0x5983, 0xCA01, 0xC64B, 0x92EC), bn_pack4(0x6FB8, 0xF401, 0x378C, 0xD2BF), bn_pack4(0x3320, 0x5151, 0x2BD7, 0xAF42), bn_pack4(0xDB7F, 0x1447, 0xE6CC, 0x254B), bn_pack4(0x44CE, 0x6CBA, 0xCED4, 0xBB1B), bn_pack4(0xDA3E, 0xDBEB, 0xCF9B, 0x14ED), bn_pack4(0x1797, 0x27B0, 0x865A, 0x8918), bn_pack4(0xB06A, 0x53ED, 0x9027, 0xD831), bn_pack4(0xE5DB, 0x382F, 0x4130, 0x01AE), bn_pack4(0xF8FF, 0x9406, 0xAD9E, 0x530E), bn_pack4(0xC975, 0x1E76, 0x3DBA, 0x37BD), bn_pack4(0xC1D4, 0xDCB2, 0x6026, 0x46DE), bn_pack4(0x36C3, 0xFAB4, 0xD27C, 0x7026), bn_pack4(0x4DF4, 0x35C9, 0x3402, 0x8492), bn_pack4(0x86FF, 0xB7DC, 0x90A6, 0xC08F), bn_pack4(0x93B4, 0xEA98, 0x8D8F, 0xDDC1), bn_pack4(0xD006, 0x9127, 0xD5B0, 0x5AA9), bn_pack4(0xB81B, 0xDD76, 0x2170, 0x481C), bn_pack4(0x1F61, 0x2970, 0xCEE2, 0xD7AF), bn_pack4(0x233B, 0xA186, 0x515B, 0xE7ED), bn_pack4(0x99B2, 0x964F, 0xA090, 0xC3A2), bn_pack4(0x287C, 0x5947, 0x4E6B, 0xC05D), bn_pack4(0x2E8E, 0xFC14, 0x1FBE, 0xCAA6), bn_pack4(0xDBBB, 0xC2DB, 0x04DE, 0x8EF9), bn_pack4(0x2583, 0xE9CA, 0x2AD4, 0x4CE8), bn_pack4(0x1A94, 0x6834, 0xB615, 0x0BDA), bn_pack4(0x99C3, 0x2718, 0x6AF4, 0xE23C), bn_pack4(0x8871, 0x9A10, 0xBDBA, 0x5B26), bn_pack4(0x1A72, 0x3C12, 0xA787, 0xE6D7), bn_pack4(0x4B82, 0xD120, 0xA921, 0x0801), bn_pack4(0x43DB, 0x5BFC, 0xE0FD, 0x108E), bn_pack4(0x08E2, 0x4FA0, 0x74E5, 0xAB31), bn_pack4(0x7709, 0x88C0, 0xBAD9, 0x46E2), bn_pack4(0xBBE1, 0x1757, 0x7A61, 0x5D6C), bn_pack4(0x521F, 0x2B18, 0x177B, 0x200C), bn_pack4(0xD876, 0x0273, 0x3EC8, 0x6A64), bn_pack4(0xF12F, 0xFA06, 0xD98A, 0x0864), bn_pack4(0xCEE3, 0xD226, 0x1AD2, 0xEE6B), bn_pack4(0x1E8C, 0x94E0, 0x4A25, 0x619D), bn_pack4(0xABF5, 0xAE8C, 0xDB09, 0x33D7), bn_pack4(0xB397, 0x0F85, 0xA6E1, 0xE4C7), bn_pack4(0x8AEA, 0x7157, 0x5D06, 0x0C7D), bn_pack4(0xECFB, 0x8504, 0x58DB, 0xEF0A), bn_pack4(0xA855, 0x21AB, 0xDF1C, 0xBA64), bn_pack4(0xAD33, 0x170D, 0x0450, 0x7A33), bn_pack4(0x1572, 0x8E5A, 0x8AAA, 0xC42D), bn_pack4(0x15D2, 0x2618, 0x98FA, 0x0510), bn_pack4(0x3995, 0x497C, 0xEA95, 0x6AE5), bn_pack4(0xDE2B, 0xCBF6, 0x9558, 0x1718), bn_pack4(0xB5C5, 0x5DF0, 0x6F4C, 0x52C9), bn_pack4(0x9B27, 0x83A2, 0xEC07, 0xA28F), bn_pack4(0xE39E, 0x772C, 0x180E, 0x8603), bn_pack4(0x3290, 0x5E46, 0x2E36, 0xCE3B), bn_pack4(0xF174, 0x6C08, 0xCA18, 0x217C), bn_pack4(0x670C, 0x354E, 0x4ABC, 0x9804), bn_pack4(0x9ED5, 0x2907, 0x7096, 0x966D), bn_pack4(0x1C62, 0xF356, 0x2085, 0x52BB), bn_pack4(0x8365, 0x5D23, 0xDCA3, 0xAD96), bn_pack4(0x6916, 0x3FA8, 0xFD24, 0xCF5F), bn_pack4(0x98DA, 0x4836, 0x1C55, 0xD39A), bn_pack4(0xC200, 0x7CB8, 0xA163, 0xBF05), bn_pack4(0x4928, 0x6651, 0xECE4, 0x5B3D), bn_pack4(0xAE9F, 0x2411, 0x7C4B, 0x1FE6), bn_pack4(0xEE38, 0x6BFB, 0x5A89, 0x9FA5), bn_pack4(0x0BFF, 0x5CB6, 0xF406, 0xB7ED), bn_pack4(0xF44C, 0x42E9, 0xA637, 0xED6B), bn_pack4(0xE485, 0xB576, 0x625E, 0x7EC6), bn_pack4(0x4FE1, 0x356D, 0x6D51, 0xC245), bn_pack4(0x302B, 0x0A6D, 0xF25F, 0x1437), bn_pack4(0xEF95, 0x19B3, 0xCD3A, 0x431B), bn_pack4(0x514A, 0x0879, 0x8E34, 0x04DD), bn_pack4(0x020B, 0xBEA6, 0x3B13, 0x9B22), bn_pack4(0x2902, 0x4E08, 0x8A67, 0xCC74), bn_pack4(0xC4C6, 0x628B, 0x80DC, 0x1CD1), bn_pack4(0xC90F, 0xDAA2, 0x2168, 0xC234), bn_pack4(0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF) }; const BIGNUM ossl_bn_group_6144 = { (BN_ULONG *)bn_group_6144_value, OSSL_NELEM(bn_group_6144_value), OSSL_NELEM(bn_group_6144_value), 0, BN_FLG_STATIC_DATA }; static const BN_ULONG bn_group_8192_value[] = { bn_pack4(0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF), bn_pack4(0x60C9, 0x80DD, 0x98ED, 0xD3DF), bn_pack4(0xC81F, 0x56E8, 0x80B9, 0x6E71), bn_pack4(0x9E30, 0x50E2, 0x7656, 0x94DF), bn_pack4(0x9558, 0xE447, 0x5677, 0xE9AA), bn_pack4(0xC919, 0x0DA6, 0xFC02, 0x6E47), bn_pack4(0x889A, 0x002E, 0xD5EE, 0x382B), bn_pack4(0x4009, 0x438B, 0x481C, 0x6CD7), bn_pack4(0x3590, 0x46F4, 0xEB87, 0x9F92), bn_pack4(0xFAF3, 0x6BC3, 0x1ECF, 0xA268), bn_pack4(0xB1D5, 0x10BD, 0x7EE7, 0x4D73), bn_pack4(0xF9AB, 0x4819, 0x5DED, 0x7EA1), bn_pack4(0x64F3, 0x1CC5, 0x0846, 0x851D), bn_pack4(0x4597, 0xE899, 0xA025, 0x5DC1), bn_pack4(0xDF31, 0x0EE0, 0x74AB, 0x6A36), bn_pack4(0x6D2A, 0x13F8, 0x3F44, 0xF82D), bn_pack4(0x062B, 0x3CF5, 0xB3A2, 0x78A6), bn_pack4(0x7968, 0x3303, 0xED5B, 0xDD3A), bn_pack4(0xFA9D, 0x4B7F, 0xA2C0, 0x87E8), bn_pack4(0x4BCB, 0xC886, 0x2F83, 0x85DD), bn_pack4(0x3473, 0xFC64, 0x6CEA, 0x306B), bn_pack4(0x13EB, 0x57A8, 0x1A23, 0xF0C7), bn_pack4(0x2222, 0x2E04, 0xA403, 0x7C07), bn_pack4(0xE3FD, 0xB8BE, 0xFC84, 0x8AD9), bn_pack4(0x238F, 0x16CB, 0xE39D, 0x652D), bn_pack4(0x3423, 0xB474, 0x2BF1, 0xC978), bn_pack4(0x3AAB, 0x639C, 0x5AE4, 0xF568), bn_pack4(0x2576, 0xF693, 0x6BA4, 0x2466), bn_pack4(0x741F, 0xA7BF, 0x8AFC, 0x47ED), bn_pack4(0x3BC8, 0x32B6, 0x8D9D, 0xD300), bn_pack4(0xD8BE, 0xC4D0, 0x73B9, 0x31BA), bn_pack4(0x3877, 0x7CB6, 0xA932, 0xDF8C), bn_pack4(0x74A3, 0x926F, 0x12FE, 0xE5E4), bn_pack4(0xE694, 0xF91E, 0x6DBE, 0x1159), bn_pack4(0x12BF, 0x2D5B, 0x0B74, 0x74D6), bn_pack4(0x043E, 0x8F66, 0x3F48, 0x60EE), bn_pack4(0x387F, 0xE8D7, 0x6E3C, 0x0468), bn_pack4(0xDA56, 0xC9EC, 0x2EF2, 0x9632), bn_pack4(0xEB19, 0xCCB1, 0xA313, 0xD55C), bn_pack4(0xF550, 0xAA3D, 0x8A1F, 0xBFF0), bn_pack4(0x06A1, 0xD58B, 0xB7C5, 0xDA76), bn_pack4(0xA797, 0x15EE, 0xF29B, 0xE328), bn_pack4(0x14CC, 0x5ED2, 0x0F80, 0x37E0), bn_pack4(0xCC8F, 0x6D7E, 0xBF48, 0xE1D8), bn_pack4(0x4BD4, 0x07B2, 0x2B41, 0x54AA), bn_pack4(0x0F1D, 0x45B7, 0xFF58, 0x5AC5), bn_pack4(0x23A9, 0x7A7E, 0x36CC, 0x88BE), bn_pack4(0x59E7, 0xC97F, 0xBEC7, 0xE8F3), bn_pack4(0xB5A8, 0x4031, 0x900B, 0x1C9E), bn_pack4(0xD55E, 0x702F, 0x4698, 0x0C82), bn_pack4(0xF482, 0xD7CE, 0x6E74, 0xFEF6), bn_pack4(0xF032, 0xEA15, 0xD172, 0x1D03), bn_pack4(0x5983, 0xCA01, 0xC64B, 0x92EC), bn_pack4(0x6FB8, 0xF401, 0x378C, 0xD2BF), bn_pack4(0x3320, 0x5151, 0x2BD7, 0xAF42), bn_pack4(0xDB7F, 0x1447, 0xE6CC, 0x254B), bn_pack4(0x44CE, 0x6CBA, 0xCED4, 0xBB1B), bn_pack4(0xDA3E, 0xDBEB, 0xCF9B, 0x14ED), bn_pack4(0x1797, 0x27B0, 0x865A, 0x8918), bn_pack4(0xB06A, 0x53ED, 0x9027, 0xD831), bn_pack4(0xE5DB, 0x382F, 0x4130, 0x01AE), bn_pack4(0xF8FF, 0x9406, 0xAD9E, 0x530E), bn_pack4(0xC975, 0x1E76, 0x3DBA, 0x37BD), bn_pack4(0xC1D4, 0xDCB2, 0x6026, 0x46DE), bn_pack4(0x36C3, 0xFAB4, 0xD27C, 0x7026), bn_pack4(0x4DF4, 0x35C9, 0x3402, 0x8492), bn_pack4(0x86FF, 0xB7DC, 0x90A6, 0xC08F), bn_pack4(0x93B4, 0xEA98, 0x8D8F, 0xDDC1), bn_pack4(0xD006, 0x9127, 0xD5B0, 0x5AA9), bn_pack4(0xB81B, 0xDD76, 0x2170, 0x481C), bn_pack4(0x1F61, 0x2970, 0xCEE2, 0xD7AF), bn_pack4(0x233B, 0xA186, 0x515B, 0xE7ED), bn_pack4(0x99B2, 0x964F, 0xA090, 0xC3A2), bn_pack4(0x287C, 0x5947, 0x4E6B, 0xC05D), bn_pack4(0x2E8E, 0xFC14, 0x1FBE, 0xCAA6), bn_pack4(0xDBBB, 0xC2DB, 0x04DE, 0x8EF9), bn_pack4(0x2583, 0xE9CA, 0x2AD4, 0x4CE8), bn_pack4(0x1A94, 0x6834, 0xB615, 0x0BDA), bn_pack4(0x99C3, 0x2718, 0x6AF4, 0xE23C), bn_pack4(0x8871, 0x9A10, 0xBDBA, 0x5B26), bn_pack4(0x1A72, 0x3C12, 0xA787, 0xE6D7), bn_pack4(0x4B82, 0xD120, 0xA921, 0x0801), bn_pack4(0x43DB, 0x5BFC, 0xE0FD, 0x108E), bn_pack4(0x08E2, 0x4FA0, 0x74E5, 0xAB31), bn_pack4(0x7709, 0x88C0, 0xBAD9, 0x46E2), bn_pack4(0xBBE1, 0x1757, 0x7A61, 0x5D6C), bn_pack4(0x521F, 0x2B18, 0x177B, 0x200C), bn_pack4(0xD876, 0x0273, 0x3EC8, 0x6A64), bn_pack4(0xF12F, 0xFA06, 0xD98A, 0x0864), bn_pack4(0xCEE3, 0xD226, 0x1AD2, 0xEE6B), bn_pack4(0x1E8C, 0x94E0, 0x4A25, 0x619D), bn_pack4(0xABF5, 0xAE8C, 0xDB09, 0x33D7), bn_pack4(0xB397, 0x0F85, 0xA6E1, 0xE4C7), bn_pack4(0x8AEA, 0x7157, 0x5D06, 0x0C7D), bn_pack4(0xECFB, 0x8504, 0x58DB, 0xEF0A), bn_pack4(0xA855, 0x21AB, 0xDF1C, 0xBA64), bn_pack4(0xAD33, 0x170D, 0x0450, 0x7A33), bn_pack4(0x1572, 0x8E5A, 0x8AAA, 0xC42D), bn_pack4(0x15D2, 0x2618, 0x98FA, 0x0510), bn_pack4(0x3995, 0x497C, 0xEA95, 0x6AE5), bn_pack4(0xDE2B, 0xCBF6, 0x9558, 0x1718), bn_pack4(0xB5C5, 0x5DF0, 0x6F4C, 0x52C9), bn_pack4(0x9B27, 0x83A2, 0xEC07, 0xA28F), bn_pack4(0xE39E, 0x772C, 0x180E, 0x8603), bn_pack4(0x3290, 0x5E46, 0x2E36, 0xCE3B), bn_pack4(0xF174, 0x6C08, 0xCA18, 0x217C), bn_pack4(0x670C, 0x354E, 0x4ABC, 0x9804), bn_pack4(0x9ED5, 0x2907, 0x7096, 0x966D), bn_pack4(0x1C62, 0xF356, 0x2085, 0x52BB), bn_pack4(0x8365, 0x5D23, 0xDCA3, 0xAD96), bn_pack4(0x6916, 0x3FA8, 0xFD24, 0xCF5F), bn_pack4(0x98DA, 0x4836, 0x1C55, 0xD39A), bn_pack4(0xC200, 0x7CB8, 0xA163, 0xBF05), bn_pack4(0x4928, 0x6651, 0xECE4, 0x5B3D), bn_pack4(0xAE9F, 0x2411, 0x7C4B, 0x1FE6), bn_pack4(0xEE38, 0x6BFB, 0x5A89, 0x9FA5), bn_pack4(0x0BFF, 0x5CB6, 0xF406, 0xB7ED), bn_pack4(0xF44C, 0x42E9, 0xA637, 0xED6B), bn_pack4(0xE485, 0xB576, 0x625E, 0x7EC6), bn_pack4(0x4FE1, 0x356D, 0x6D51, 0xC245), bn_pack4(0x302B, 0x0A6D, 0xF25F, 0x1437), bn_pack4(0xEF95, 0x19B3, 0xCD3A, 0x431B), bn_pack4(0x514A, 0x0879, 0x8E34, 0x04DD), bn_pack4(0x020B, 0xBEA6, 0x3B13, 0x9B22), bn_pack4(0x2902, 0x4E08, 0x8A67, 0xCC74), bn_pack4(0xC4C6, 0x628B, 0x80DC, 0x1CD1), bn_pack4(0xC90F, 0xDAA2, 0x2168, 0xC234), bn_pack4(0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF) }; const BIGNUM ossl_bn_group_8192 = { (BN_ULONG *)bn_group_8192_value, OSSL_NELEM(bn_group_8192_value), OSSL_NELEM(bn_group_8192_value), 0, BN_FLG_STATIC_DATA }; static const BN_ULONG bn_generator_19_value[] = { 19 }; const BIGNUM ossl_bn_generator_19 = { (BN_ULONG *)bn_generator_19_value, 1, 1, 0, BN_FLG_STATIC_DATA }; static const BN_ULONG bn_generator_5_value[] = { 5 }; const BIGNUM ossl_bn_generator_5 = { (BN_ULONG *)bn_generator_5_value, 1, 1, 0, BN_FLG_STATIC_DATA }; static const BN_ULONG bn_generator_2_value[] = { 2 }; const BIGNUM ossl_bn_generator_2 = { (BN_ULONG *)bn_generator_2_value, 1, 1, 0, BN_FLG_STATIC_DATA }; #endif
bn
openssl/crypto/bn/bn_srp.c
openssl
#include <assert.h> #include <limits.h> #include <stdio.h> #include "internal/cryptlib.h" #include "bn_local.h" #ifndef OPENSSL_NO_EC2M # define MAX_ITERATIONS 50 # define SQR_nibble(w) ((((w) & 8) << 3) \ | (((w) & 4) << 2) \ | (((w) & 2) << 1) \ | ((w) & 1)) # if defined(SIXTY_FOUR_BIT) || defined(SIXTY_FOUR_BIT_LONG) # define SQR1(w) \ SQR_nibble((w) >> 60) << 56 | SQR_nibble((w) >> 56) << 48 | \ SQR_nibble((w) >> 52) << 40 | SQR_nibble((w) >> 48) << 32 | \ SQR_nibble((w) >> 44) << 24 | SQR_nibble((w) >> 40) << 16 | \ SQR_nibble((w) >> 36) << 8 | SQR_nibble((w) >> 32) # define SQR0(w) \ SQR_nibble((w) >> 28) << 56 | SQR_nibble((w) >> 24) << 48 | \ SQR_nibble((w) >> 20) << 40 | SQR_nibble((w) >> 16) << 32 | \ SQR_nibble((w) >> 12) << 24 | SQR_nibble((w) >> 8) << 16 | \ SQR_nibble((w) >> 4) << 8 | SQR_nibble((w) ) # endif # ifdef THIRTY_TWO_BIT # define SQR1(w) \ SQR_nibble((w) >> 28) << 24 | SQR_nibble((w) >> 24) << 16 | \ SQR_nibble((w) >> 20) << 8 | SQR_nibble((w) >> 16) # define SQR0(w) \ SQR_nibble((w) >> 12) << 24 | SQR_nibble((w) >> 8) << 16 | \ SQR_nibble((w) >> 4) << 8 | SQR_nibble((w) ) # endif # if !defined(OPENSSL_BN_ASM_GF2m) # ifdef THIRTY_TWO_BIT static void bn_GF2m_mul_1x1(BN_ULONG *r1, BN_ULONG *r0, const BN_ULONG a, const BN_ULONG b) { register BN_ULONG h, l, s; BN_ULONG tab[8], top2b = a >> 30; register BN_ULONG a1, a2, a4; a1 = a & (0x3FFFFFFF); a2 = a1 << 1; a4 = a2 << 1; tab[0] = 0; tab[1] = a1; tab[2] = a2; tab[3] = a1 ^ a2; tab[4] = a4; tab[5] = a1 ^ a4; tab[6] = a2 ^ a4; tab[7] = a1 ^ a2 ^ a4; s = tab[b & 0x7]; l = s; s = tab[b >> 3 & 0x7]; l ^= s << 3; h = s >> 29; s = tab[b >> 6 & 0x7]; l ^= s << 6; h ^= s >> 26; s = tab[b >> 9 & 0x7]; l ^= s << 9; h ^= s >> 23; s = tab[b >> 12 & 0x7]; l ^= s << 12; h ^= s >> 20; s = tab[b >> 15 & 0x7]; l ^= s << 15; h ^= s >> 17; s = tab[b >> 18 & 0x7]; l ^= s << 18; h ^= s >> 14; s = tab[b >> 21 & 0x7]; l ^= s << 21; h ^= s >> 11; s = tab[b >> 24 & 0x7]; l ^= s << 24; h ^= s >> 8; s = tab[b >> 27 & 0x7]; l ^= s << 27; h ^= s >> 5; s = tab[b >> 30]; l ^= s << 30; h ^= s >> 2; if (top2b & 01) { l ^= b << 30; h ^= b >> 2; } if (top2b & 02) { l ^= b << 31; h ^= b >> 1; } *r1 = h; *r0 = l; } # endif # if defined(SIXTY_FOUR_BIT) || defined(SIXTY_FOUR_BIT_LONG) static void bn_GF2m_mul_1x1(BN_ULONG *r1, BN_ULONG *r0, const BN_ULONG a, const BN_ULONG b) { register BN_ULONG h, l, s; BN_ULONG tab[16], top3b = a >> 61; register BN_ULONG a1, a2, a4, a8; a1 = a & (0x1FFFFFFFFFFFFFFFULL); a2 = a1 << 1; a4 = a2 << 1; a8 = a4 << 1; tab[0] = 0; tab[1] = a1; tab[2] = a2; tab[3] = a1 ^ a2; tab[4] = a4; tab[5] = a1 ^ a4; tab[6] = a2 ^ a4; tab[7] = a1 ^ a2 ^ a4; tab[8] = a8; tab[9] = a1 ^ a8; tab[10] = a2 ^ a8; tab[11] = a1 ^ a2 ^ a8; tab[12] = a4 ^ a8; tab[13] = a1 ^ a4 ^ a8; tab[14] = a2 ^ a4 ^ a8; tab[15] = a1 ^ a2 ^ a4 ^ a8; s = tab[b & 0xF]; l = s; s = tab[b >> 4 & 0xF]; l ^= s << 4; h = s >> 60; s = tab[b >> 8 & 0xF]; l ^= s << 8; h ^= s >> 56; s = tab[b >> 12 & 0xF]; l ^= s << 12; h ^= s >> 52; s = tab[b >> 16 & 0xF]; l ^= s << 16; h ^= s >> 48; s = tab[b >> 20 & 0xF]; l ^= s << 20; h ^= s >> 44; s = tab[b >> 24 & 0xF]; l ^= s << 24; h ^= s >> 40; s = tab[b >> 28 & 0xF]; l ^= s << 28; h ^= s >> 36; s = tab[b >> 32 & 0xF]; l ^= s << 32; h ^= s >> 32; s = tab[b >> 36 & 0xF]; l ^= s << 36; h ^= s >> 28; s = tab[b >> 40 & 0xF]; l ^= s << 40; h ^= s >> 24; s = tab[b >> 44 & 0xF]; l ^= s << 44; h ^= s >> 20; s = tab[b >> 48 & 0xF]; l ^= s << 48; h ^= s >> 16; s = tab[b >> 52 & 0xF]; l ^= s << 52; h ^= s >> 12; s = tab[b >> 56 & 0xF]; l ^= s << 56; h ^= s >> 8; s = tab[b >> 60]; l ^= s << 60; h ^= s >> 4; if (top3b & 01) { l ^= b << 61; h ^= b >> 3; } if (top3b & 02) { l ^= b << 62; h ^= b >> 2; } if (top3b & 04) { l ^= b << 63; h ^= b >> 1; } *r1 = h; *r0 = l; } # endif static void bn_GF2m_mul_2x2(BN_ULONG *r, const BN_ULONG a1, const BN_ULONG a0, const BN_ULONG b1, const BN_ULONG b0) { BN_ULONG m1, m0; bn_GF2m_mul_1x1(r + 3, r + 2, a1, b1); bn_GF2m_mul_1x1(r + 1, r, a0, b0); bn_GF2m_mul_1x1(&m1, &m0, a0 ^ a1, b0 ^ b1); r[2] ^= m1 ^ r[1] ^ r[3]; r[1] = r[3] ^ r[2] ^ r[0] ^ m1 ^ m0; } # else void bn_GF2m_mul_2x2(BN_ULONG *r, BN_ULONG a1, BN_ULONG a0, BN_ULONG b1, BN_ULONG b0); # endif int BN_GF2m_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b) { int i; const BIGNUM *at, *bt; bn_check_top(a); bn_check_top(b); if (a->top < b->top) { at = b; bt = a; } else { at = a; bt = b; } if (bn_wexpand(r, at->top) == NULL) return 0; for (i = 0; i < bt->top; i++) { r->d[i] = at->d[i] ^ bt->d[i]; } for (; i < at->top; i++) { r->d[i] = at->d[i]; } r->top = at->top; bn_correct_top(r); return 1; } int BN_GF2m_mod_arr(BIGNUM *r, const BIGNUM *a, const int p[]) { int j, k; int n, dN, d0, d1; BN_ULONG zz, *z; bn_check_top(a); if (p[0] == 0) { BN_zero(r); return 1; } if (a != r) { if (!bn_wexpand(r, a->top)) return 0; for (j = 0; j < a->top; j++) { r->d[j] = a->d[j]; } r->top = a->top; } z = r->d; dN = p[0] / BN_BITS2; for (j = r->top - 1; j > dN;) { zz = z[j]; if (z[j] == 0) { j--; continue; } z[j] = 0; for (k = 1; p[k] != 0; k++) { n = p[0] - p[k]; d0 = n % BN_BITS2; d1 = BN_BITS2 - d0; n /= BN_BITS2; z[j - n] ^= (zz >> d0); if (d0) z[j - n - 1] ^= (zz << d1); } n = dN; d0 = p[0] % BN_BITS2; d1 = BN_BITS2 - d0; z[j - n] ^= (zz >> d0); if (d0) z[j - n - 1] ^= (zz << d1); } while (j == dN) { d0 = p[0] % BN_BITS2; zz = z[dN] >> d0; if (zz == 0) break; d1 = BN_BITS2 - d0; if (d0) z[dN] = (z[dN] << d1) >> d1; else z[dN] = 0; z[0] ^= zz; for (k = 1; p[k] != 0; k++) { BN_ULONG tmp_ulong; n = p[k] / BN_BITS2; d0 = p[k] % BN_BITS2; d1 = BN_BITS2 - d0; z[n] ^= (zz << d0); if (d0 && (tmp_ulong = zz >> d1)) z[n + 1] ^= tmp_ulong; } } bn_correct_top(r); return 1; } int BN_GF2m_mod(BIGNUM *r, const BIGNUM *a, const BIGNUM *p) { int ret = 0; int arr[6]; bn_check_top(a); bn_check_top(p); ret = BN_GF2m_poly2arr(p, arr, OSSL_NELEM(arr)); if (!ret || ret > (int)OSSL_NELEM(arr)) { ERR_raise(ERR_LIB_BN, BN_R_INVALID_LENGTH); return 0; } ret = BN_GF2m_mod_arr(r, a, arr); bn_check_top(r); return ret; } int BN_GF2m_mod_mul_arr(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const int p[], BN_CTX *ctx) { int zlen, i, j, k, ret = 0; BIGNUM *s; BN_ULONG x1, x0, y1, y0, zz[4]; bn_check_top(a); bn_check_top(b); if (a == b) { return BN_GF2m_mod_sqr_arr(r, a, p, ctx); } BN_CTX_start(ctx); if ((s = BN_CTX_get(ctx)) == NULL) goto err; zlen = a->top + b->top + 4; if (!bn_wexpand(s, zlen)) goto err; s->top = zlen; for (i = 0; i < zlen; i++) s->d[i] = 0; for (j = 0; j < b->top; j += 2) { y0 = b->d[j]; y1 = ((j + 1) == b->top) ? 0 : b->d[j + 1]; for (i = 0; i < a->top; i += 2) { x0 = a->d[i]; x1 = ((i + 1) == a->top) ? 0 : a->d[i + 1]; bn_GF2m_mul_2x2(zz, x1, x0, y1, y0); for (k = 0; k < 4; k++) s->d[i + j + k] ^= zz[k]; } } bn_correct_top(s); if (BN_GF2m_mod_arr(r, s, p)) ret = 1; bn_check_top(r); err: BN_CTX_end(ctx); return ret; } int BN_GF2m_mod_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *p, BN_CTX *ctx) { int ret = 0; const int max = BN_num_bits(p) + 1; int *arr; bn_check_top(a); bn_check_top(b); bn_check_top(p); arr = OPENSSL_malloc(sizeof(*arr) * max); if (arr == NULL) return 0; ret = BN_GF2m_poly2arr(p, arr, max); if (!ret || ret > max) { ERR_raise(ERR_LIB_BN, BN_R_INVALID_LENGTH); goto err; } ret = BN_GF2m_mod_mul_arr(r, a, b, arr, ctx); bn_check_top(r); err: OPENSSL_free(arr); return ret; } int BN_GF2m_mod_sqr_arr(BIGNUM *r, const BIGNUM *a, const int p[], BN_CTX *ctx) { int i, ret = 0; BIGNUM *s; bn_check_top(a); BN_CTX_start(ctx); if ((s = BN_CTX_get(ctx)) == NULL) goto err; if (!bn_wexpand(s, 2 * a->top)) goto err; for (i = a->top - 1; i >= 0; i--) { s->d[2 * i + 1] = SQR1(a->d[i]); s->d[2 * i] = SQR0(a->d[i]); } s->top = 2 * a->top; bn_correct_top(s); if (!BN_GF2m_mod_arr(r, s, p)) goto err; bn_check_top(r); ret = 1; err: BN_CTX_end(ctx); return ret; } int BN_GF2m_mod_sqr(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx) { int ret = 0; const int max = BN_num_bits(p) + 1; int *arr; bn_check_top(a); bn_check_top(p); arr = OPENSSL_malloc(sizeof(*arr) * max); if (arr == NULL) return 0; ret = BN_GF2m_poly2arr(p, arr, max); if (!ret || ret > max) { ERR_raise(ERR_LIB_BN, BN_R_INVALID_LENGTH); goto err; } ret = BN_GF2m_mod_sqr_arr(r, a, arr, ctx); bn_check_top(r); err: OPENSSL_free(arr); return ret; } static int BN_GF2m_mod_inv_vartime(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx) { BIGNUM *b, *c = NULL, *u = NULL, *v = NULL, *tmp; int ret = 0; bn_check_top(a); bn_check_top(p); BN_CTX_start(ctx); b = BN_CTX_get(ctx); c = BN_CTX_get(ctx); u = BN_CTX_get(ctx); v = BN_CTX_get(ctx); if (v == NULL) goto err; if (!BN_GF2m_mod(u, a, p)) goto err; if (BN_is_zero(u)) goto err; if (!BN_copy(v, p)) goto err; # if 0 if (!BN_one(b)) goto err; while (1) { while (!BN_is_odd(u)) { if (BN_is_zero(u)) goto err; if (!BN_rshift1(u, u)) goto err; if (BN_is_odd(b)) { if (!BN_GF2m_add(b, b, p)) goto err; } if (!BN_rshift1(b, b)) goto err; } if (BN_abs_is_word(u, 1)) break; if (BN_num_bits(u) < BN_num_bits(v)) { tmp = u; u = v; v = tmp; tmp = b; b = c; c = tmp; } if (!BN_GF2m_add(u, u, v)) goto err; if (!BN_GF2m_add(b, b, c)) goto err; } # else { int i; int ubits = BN_num_bits(u); int vbits = BN_num_bits(v); int top = p->top; BN_ULONG *udp, *bdp, *vdp, *cdp; if (!bn_wexpand(u, top)) goto err; udp = u->d; for (i = u->top; i < top; i++) udp[i] = 0; u->top = top; if (!bn_wexpand(b, top)) goto err; bdp = b->d; bdp[0] = 1; for (i = 1; i < top; i++) bdp[i] = 0; b->top = top; if (!bn_wexpand(c, top)) goto err; cdp = c->d; for (i = 0; i < top; i++) cdp[i] = 0; c->top = top; vdp = v->d; while (1) { while (ubits && !(udp[0] & 1)) { BN_ULONG u0, u1, b0, b1, mask; u0 = udp[0]; b0 = bdp[0]; mask = (BN_ULONG)0 - (b0 & 1); b0 ^= p->d[0] & mask; for (i = 0; i < top - 1; i++) { u1 = udp[i + 1]; udp[i] = ((u0 >> 1) | (u1 << (BN_BITS2 - 1))) & BN_MASK2; u0 = u1; b1 = bdp[i + 1] ^ (p->d[i + 1] & mask); bdp[i] = ((b0 >> 1) | (b1 << (BN_BITS2 - 1))) & BN_MASK2; b0 = b1; } udp[i] = u0 >> 1; bdp[i] = b0 >> 1; ubits--; } if (ubits <= BN_BITS2) { if (udp[0] == 0) goto err; if (udp[0] == 1) break; } if (ubits < vbits) { i = ubits; ubits = vbits; vbits = i; tmp = u; u = v; v = tmp; tmp = b; b = c; c = tmp; udp = vdp; vdp = v->d; bdp = cdp; cdp = c->d; } for (i = 0; i < top; i++) { udp[i] ^= vdp[i]; bdp[i] ^= cdp[i]; } if (ubits == vbits) { BN_ULONG ul; int utop = (ubits - 1) / BN_BITS2; while ((ul = udp[utop]) == 0 && utop) utop--; ubits = utop * BN_BITS2 + BN_num_bits_word(ul); } } bn_correct_top(b); } # endif if (!BN_copy(r, b)) goto err; bn_check_top(r); ret = 1; err: # ifdef BN_DEBUG bn_correct_top(c); bn_correct_top(u); bn_correct_top(v); # endif BN_CTX_end(ctx); return ret; } int BN_GF2m_mod_inv(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx) { BIGNUM *b = NULL; int ret = 0; int numbits; BN_CTX_start(ctx); if ((b = BN_CTX_get(ctx)) == NULL) goto err; numbits = BN_num_bits(p); if (numbits <= 1) goto err; do { if (!BN_priv_rand_ex(b, numbits - 1, BN_RAND_TOP_ANY, BN_RAND_BOTTOM_ANY, 0, ctx)) goto err; } while (BN_is_zero(b)); if (!BN_GF2m_mod_mul(r, a, b, p, ctx)) goto err; if (!BN_GF2m_mod_inv_vartime(r, r, p, ctx)) goto err; if (!BN_GF2m_mod_mul(r, r, b, p, ctx)) goto err; ret = 1; err: BN_CTX_end(ctx); return ret; } int BN_GF2m_mod_inv_arr(BIGNUM *r, const BIGNUM *xx, const int p[], BN_CTX *ctx) { BIGNUM *field; int ret = 0; bn_check_top(xx); BN_CTX_start(ctx); if ((field = BN_CTX_get(ctx)) == NULL) goto err; if (!BN_GF2m_arr2poly(p, field)) goto err; ret = BN_GF2m_mod_inv(r, xx, field, ctx); bn_check_top(r); err: BN_CTX_end(ctx); return ret; } int BN_GF2m_mod_div(BIGNUM *r, const BIGNUM *y, const BIGNUM *x, const BIGNUM *p, BN_CTX *ctx) { BIGNUM *xinv = NULL; int ret = 0; bn_check_top(y); bn_check_top(x); bn_check_top(p); BN_CTX_start(ctx); xinv = BN_CTX_get(ctx); if (xinv == NULL) goto err; if (!BN_GF2m_mod_inv(xinv, x, p, ctx)) goto err; if (!BN_GF2m_mod_mul(r, y, xinv, p, ctx)) goto err; bn_check_top(r); ret = 1; err: BN_CTX_end(ctx); return ret; } int BN_GF2m_mod_div_arr(BIGNUM *r, const BIGNUM *yy, const BIGNUM *xx, const int p[], BN_CTX *ctx) { BIGNUM *field; int ret = 0; bn_check_top(yy); bn_check_top(xx); BN_CTX_start(ctx); if ((field = BN_CTX_get(ctx)) == NULL) goto err; if (!BN_GF2m_arr2poly(p, field)) goto err; ret = BN_GF2m_mod_div(r, yy, xx, field, ctx); bn_check_top(r); err: BN_CTX_end(ctx); return ret; } int BN_GF2m_mod_exp_arr(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const int p[], BN_CTX *ctx) { int ret = 0, i, n; BIGNUM *u; bn_check_top(a); bn_check_top(b); if (BN_is_zero(b)) return BN_one(r); if (BN_abs_is_word(b, 1)) return (BN_copy(r, a) != NULL); BN_CTX_start(ctx); if ((u = BN_CTX_get(ctx)) == NULL) goto err; if (!BN_GF2m_mod_arr(u, a, p)) goto err; n = BN_num_bits(b) - 1; for (i = n - 1; i >= 0; i--) { if (!BN_GF2m_mod_sqr_arr(u, u, p, ctx)) goto err; if (BN_is_bit_set(b, i)) { if (!BN_GF2m_mod_mul_arr(u, u, a, p, ctx)) goto err; } } if (!BN_copy(r, u)) goto err; bn_check_top(r); ret = 1; err: BN_CTX_end(ctx); return ret; } int BN_GF2m_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *p, BN_CTX *ctx) { int ret = 0; const int max = BN_num_bits(p) + 1; int *arr; bn_check_top(a); bn_check_top(b); bn_check_top(p); arr = OPENSSL_malloc(sizeof(*arr) * max); if (arr == NULL) return 0; ret = BN_GF2m_poly2arr(p, arr, max); if (!ret || ret > max) { ERR_raise(ERR_LIB_BN, BN_R_INVALID_LENGTH); goto err; } ret = BN_GF2m_mod_exp_arr(r, a, b, arr, ctx); bn_check_top(r); err: OPENSSL_free(arr); return ret; } int BN_GF2m_mod_sqrt_arr(BIGNUM *r, const BIGNUM *a, const int p[], BN_CTX *ctx) { int ret = 0; BIGNUM *u; bn_check_top(a); if (p[0] == 0) { BN_zero(r); return 1; } BN_CTX_start(ctx); if ((u = BN_CTX_get(ctx)) == NULL) goto err; if (!BN_set_bit(u, p[0] - 1)) goto err; ret = BN_GF2m_mod_exp_arr(r, a, u, p, ctx); bn_check_top(r); err: BN_CTX_end(ctx); return ret; } int BN_GF2m_mod_sqrt(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx) { int ret = 0; const int max = BN_num_bits(p) + 1; int *arr; bn_check_top(a); bn_check_top(p); arr = OPENSSL_malloc(sizeof(*arr) * max); if (arr == NULL) return 0; ret = BN_GF2m_poly2arr(p, arr, max); if (!ret || ret > max) { ERR_raise(ERR_LIB_BN, BN_R_INVALID_LENGTH); goto err; } ret = BN_GF2m_mod_sqrt_arr(r, a, arr, ctx); bn_check_top(r); err: OPENSSL_free(arr); return ret; } int BN_GF2m_mod_solve_quad_arr(BIGNUM *r, const BIGNUM *a_, const int p[], BN_CTX *ctx) { int ret = 0, count = 0, j; BIGNUM *a, *z, *rho, *w, *w2, *tmp; bn_check_top(a_); if (p[0] == 0) { BN_zero(r); return 1; } BN_CTX_start(ctx); a = BN_CTX_get(ctx); z = BN_CTX_get(ctx); w = BN_CTX_get(ctx); if (w == NULL) goto err; if (!BN_GF2m_mod_arr(a, a_, p)) goto err; if (BN_is_zero(a)) { BN_zero(r); ret = 1; goto err; } if (p[0] & 0x1) { if (!BN_copy(z, a)) goto err; for (j = 1; j <= (p[0] - 1) / 2; j++) { if (!BN_GF2m_mod_sqr_arr(z, z, p, ctx)) goto err; if (!BN_GF2m_mod_sqr_arr(z, z, p, ctx)) goto err; if (!BN_GF2m_add(z, z, a)) goto err; } } else { rho = BN_CTX_get(ctx); w2 = BN_CTX_get(ctx); tmp = BN_CTX_get(ctx); if (tmp == NULL) goto err; do { if (!BN_priv_rand_ex(rho, p[0], BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY, 0, ctx)) goto err; if (!BN_GF2m_mod_arr(rho, rho, p)) goto err; BN_zero(z); if (!BN_copy(w, rho)) goto err; for (j = 1; j <= p[0] - 1; j++) { if (!BN_GF2m_mod_sqr_arr(z, z, p, ctx)) goto err; if (!BN_GF2m_mod_sqr_arr(w2, w, p, ctx)) goto err; if (!BN_GF2m_mod_mul_arr(tmp, w2, a, p, ctx)) goto err; if (!BN_GF2m_add(z, z, tmp)) goto err; if (!BN_GF2m_add(w, w2, rho)) goto err; } count++; } while (BN_is_zero(w) && (count < MAX_ITERATIONS)); if (BN_is_zero(w)) { ERR_raise(ERR_LIB_BN, BN_R_TOO_MANY_ITERATIONS); goto err; } } if (!BN_GF2m_mod_sqr_arr(w, z, p, ctx)) goto err; if (!BN_GF2m_add(w, z, w)) goto err; if (BN_GF2m_cmp(w, a)) { ERR_raise(ERR_LIB_BN, BN_R_NO_SOLUTION); goto err; } if (!BN_copy(r, z)) goto err; bn_check_top(r); ret = 1; err: BN_CTX_end(ctx); return ret; } int BN_GF2m_mod_solve_quad(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx) { int ret = 0; const int max = BN_num_bits(p) + 1; int *arr; bn_check_top(a); bn_check_top(p); arr = OPENSSL_malloc(sizeof(*arr) * max); if (arr == NULL) goto err; ret = BN_GF2m_poly2arr(p, arr, max); if (!ret || ret > max) { ERR_raise(ERR_LIB_BN, BN_R_INVALID_LENGTH); goto err; } ret = BN_GF2m_mod_solve_quad_arr(r, a, arr, ctx); bn_check_top(r); err: OPENSSL_free(arr); return ret; } int BN_GF2m_poly2arr(const BIGNUM *a, int p[], int max) { int i, j, k = 0; BN_ULONG mask; if (BN_is_zero(a)) return 0; for (i = a->top - 1; i >= 0; i--) { if (!a->d[i]) continue; mask = BN_TBIT; for (j = BN_BITS2 - 1; j >= 0; j--) { if (a->d[i] & mask) { if (k < max) p[k] = BN_BITS2 * i + j; k++; } mask >>= 1; } } if (k < max) { p[k] = -1; k++; } return k; } int BN_GF2m_arr2poly(const int p[], BIGNUM *a) { int i; bn_check_top(a); BN_zero(a); for (i = 0; p[i] != -1; i++) { if (BN_set_bit(a, p[i]) == 0) return 0; } bn_check_top(a); return 1; } #endif
bn
openssl/crypto/bn/bn_gf2m.c
openssl
#include <stdio.h> #include "internal/cryptlib.h" #include "bn_local.h" int BN_bn2mpi(const BIGNUM *a, unsigned char *d) { int bits; int num = 0; int ext = 0; long l; bits = BN_num_bits(a); num = (bits + 7) / 8; if (bits > 0) { ext = ((bits & 0x07) == 0); } if (d == NULL) return (num + 4 + ext); l = num + ext; d[0] = (unsigned char)(l >> 24) & 0xff; d[1] = (unsigned char)(l >> 16) & 0xff; d[2] = (unsigned char)(l >> 8) & 0xff; d[3] = (unsigned char)(l) & 0xff; if (ext) d[4] = 0; num = BN_bn2bin(a, &(d[4 + ext])); if (a->neg) d[4] |= 0x80; return (num + 4 + ext); } BIGNUM *BN_mpi2bn(const unsigned char *d, int n, BIGNUM *ain) { long len; int neg = 0; BIGNUM *a = NULL; if (n < 4 || (d[0] & 0x80) != 0) { ERR_raise(ERR_LIB_BN, BN_R_INVALID_LENGTH); return NULL; } len = ((long)d[0] << 24) | ((long)d[1] << 16) | ((int)d[2] << 8) | (int) d[3]; if ((len + 4) != n) { ERR_raise(ERR_LIB_BN, BN_R_ENCODING_ERROR); return NULL; } if (ain == NULL) a = BN_new(); else a = ain; if (a == NULL) return NULL; if (len == 0) { a->neg = 0; a->top = 0; return a; } d += 4; if ((*d) & 0x80) neg = 1; if (BN_bin2bn(d, (int)len, a) == NULL) { if (ain == NULL) BN_free(a); return NULL; } a->neg = neg; if (neg) { BN_clear_bit(a, BN_num_bits(a) - 1); } bn_check_top(a); return a; }
bn
openssl/crypto/bn/bn_mpi.c
openssl
#include <stdio.h> #include <time.h> #include "internal/cryptlib.h" #include "bn_local.h" #include "bn_prime.h" static int probable_prime(BIGNUM *rnd, int bits, int safe, prime_t *mods, BN_CTX *ctx); static int probable_prime_dh(BIGNUM *rnd, int bits, int safe, prime_t *mods, const BIGNUM *add, const BIGNUM *rem, BN_CTX *ctx); static int bn_is_prime_int(const BIGNUM *w, int checks, BN_CTX *ctx, int do_trial_division, BN_GENCB *cb); #define square(x) ((BN_ULONG)(x) * (BN_ULONG)(x)) #if BN_BITS2 == 64 # define BN_DEF(lo, hi) (BN_ULONG)hi<<32|lo #else # define BN_DEF(lo, hi) lo, hi #endif static const BN_ULONG small_prime_factors[] = { BN_DEF(0x3ef4e3e1, 0xc4309333), BN_DEF(0xcd2d655f, 0x71161eb6), BN_DEF(0x0bf94862, 0x95e2238c), BN_DEF(0x24f7912b, 0x3eb233d3), BN_DEF(0xbf26c483, 0x6b55514b), BN_DEF(0x5a144871, 0x0a84d817), BN_DEF(0x9b82210a, 0x77d12fee), BN_DEF(0x97f050b3, 0xdb5b93c2), BN_DEF(0x4d6c026b, 0x4acad6b9), BN_DEF(0x54aec893, 0xeb7751f3), BN_DEF(0x36bc85c4, 0xdba53368), BN_DEF(0x7f5ec78e, 0xd85a1b28), BN_DEF(0x6b322244, 0x2eb072d8), BN_DEF(0x5e2b3aea, 0xbba51112), BN_DEF(0x0e2486bf, 0x36ed1a6c), BN_DEF(0xec0c5727, 0x5f270460), (BN_ULONG)0x000017b1 }; #define BN_SMALL_PRIME_FACTORS_TOP OSSL_NELEM(small_prime_factors) static const BIGNUM _bignum_small_prime_factors = { (BN_ULONG *)small_prime_factors, BN_SMALL_PRIME_FACTORS_TOP, BN_SMALL_PRIME_FACTORS_TOP, 0, BN_FLG_STATIC_DATA }; const BIGNUM *ossl_bn_get0_small_factors(void) { return &_bignum_small_prime_factors; } static int calc_trial_divisions(int bits) { if (bits <= 512) return 64; else if (bits <= 1024) return 128; else if (bits <= 2048) return 384; else if (bits <= 4096) return 1024; return NUMPRIMES; } static int bn_mr_min_checks(int bits) { if (bits > 2048) return 128; return 64; } int BN_GENCB_call(BN_GENCB *cb, int a, int b) { if (!cb) return 1; switch (cb->ver) { case 1: if (!cb->cb.cb_1) return 1; cb->cb.cb_1(a, b, cb->arg); return 1; case 2: return cb->cb.cb_2(a, b, cb); default: break; } return 0; } int BN_generate_prime_ex2(BIGNUM *ret, int bits, int safe, const BIGNUM *add, const BIGNUM *rem, BN_GENCB *cb, BN_CTX *ctx) { BIGNUM *t; int found = 0; int i, j, c1 = 0; prime_t *mods = NULL; int checks = bn_mr_min_checks(bits); if (bits < 2) { ERR_raise(ERR_LIB_BN, BN_R_BITS_TOO_SMALL); return 0; } else if (add == NULL && safe && bits < 6 && bits != 3) { ERR_raise(ERR_LIB_BN, BN_R_BITS_TOO_SMALL); return 0; } mods = OPENSSL_zalloc(sizeof(*mods) * NUMPRIMES); if (mods == NULL) return 0; BN_CTX_start(ctx); t = BN_CTX_get(ctx); if (t == NULL) goto err; loop: if (add == NULL) { if (!probable_prime(ret, bits, safe, mods, ctx)) goto err; } else { if (!probable_prime_dh(ret, bits, safe, mods, add, rem, ctx)) goto err; } if (!BN_GENCB_call(cb, 0, c1++)) goto err; if (!safe) { i = bn_is_prime_int(ret, checks, ctx, 0, cb); if (i == -1) goto err; if (i == 0) goto loop; } else { if (!BN_rshift1(t, ret)) goto err; for (i = 0; i < checks; i++) { j = bn_is_prime_int(ret, 1, ctx, 0, cb); if (j == -1) goto err; if (j == 0) goto loop; j = bn_is_prime_int(t, 1, ctx, 0, cb); if (j == -1) goto err; if (j == 0) goto loop; if (!BN_GENCB_call(cb, 2, c1 - 1)) goto err; } } found = 1; err: OPENSSL_free(mods); BN_CTX_end(ctx); bn_check_top(ret); return found; } #ifndef FIPS_MODULE int BN_generate_prime_ex(BIGNUM *ret, int bits, int safe, const BIGNUM *add, const BIGNUM *rem, BN_GENCB *cb) { BN_CTX *ctx = BN_CTX_new(); int retval; if (ctx == NULL) return 0; retval = BN_generate_prime_ex2(ret, bits, safe, add, rem, cb, ctx); BN_CTX_free(ctx); return retval; } #endif #ifndef OPENSSL_NO_DEPRECATED_3_0 int BN_is_prime_ex(const BIGNUM *a, int checks, BN_CTX *ctx_passed, BN_GENCB *cb) { return ossl_bn_check_prime(a, checks, ctx_passed, 0, cb); } int BN_is_prime_fasttest_ex(const BIGNUM *w, int checks, BN_CTX *ctx, int do_trial_division, BN_GENCB *cb) { return ossl_bn_check_prime(w, checks, ctx, do_trial_division, cb); } #endif int ossl_bn_check_prime(const BIGNUM *w, int checks, BN_CTX *ctx, int do_trial_division, BN_GENCB *cb) { int min_checks = bn_mr_min_checks(BN_num_bits(w)); if (checks < min_checks) checks = min_checks; return bn_is_prime_int(w, checks, ctx, do_trial_division, cb); } int ossl_bn_check_generated_prime(const BIGNUM *w, int checks, BN_CTX *ctx, BN_GENCB *cb) { return bn_is_prime_int(w, checks, ctx, 1, cb); } int BN_check_prime(const BIGNUM *p, BN_CTX *ctx, BN_GENCB *cb) { return ossl_bn_check_prime(p, 0, ctx, 1, cb); } static int bn_is_prime_int(const BIGNUM *w, int checks, BN_CTX *ctx, int do_trial_division, BN_GENCB *cb) { int i, status, ret = -1; #ifndef FIPS_MODULE BN_CTX *ctxlocal = NULL; #else if (ctx == NULL) return -1; #endif if (BN_cmp(w, BN_value_one()) <= 0) return 0; if (BN_is_odd(w)) { if (BN_is_word(w, 3)) return 1; } else { return BN_is_word(w, 2); } if (do_trial_division) { int trial_divisions = calc_trial_divisions(BN_num_bits(w)); for (i = 1; i < trial_divisions; i++) { BN_ULONG mod = BN_mod_word(w, primes[i]); if (mod == (BN_ULONG)-1) return -1; if (mod == 0) return BN_is_word(w, primes[i]); } if (!BN_GENCB_call(cb, 1, -1)) return -1; } #ifndef FIPS_MODULE if (ctx == NULL && (ctxlocal = ctx = BN_CTX_new()) == NULL) goto err; #endif if (!ossl_bn_miller_rabin_is_prime(w, checks, ctx, cb, 0, &status)) { ret = -1; goto err; } ret = (status == BN_PRIMETEST_PROBABLY_PRIME); err: #ifndef FIPS_MODULE BN_CTX_free(ctxlocal); #endif return ret; } int ossl_bn_miller_rabin_is_prime(const BIGNUM *w, int iterations, BN_CTX *ctx, BN_GENCB *cb, int enhanced, int *status) { int i, j, a, ret = 0; BIGNUM *g, *w1, *w3, *x, *m, *z, *b; BN_MONT_CTX *mont = NULL; if (!BN_is_odd(w)) return 0; BN_CTX_start(ctx); g = BN_CTX_get(ctx); w1 = BN_CTX_get(ctx); w3 = BN_CTX_get(ctx); x = BN_CTX_get(ctx); m = BN_CTX_get(ctx); z = BN_CTX_get(ctx); b = BN_CTX_get(ctx); if (!(b != NULL && BN_copy(w1, w) && BN_sub_word(w1, 1) && BN_copy(w3, w) && BN_sub_word(w3, 3))) goto err; if (BN_is_zero(w3) || BN_is_negative(w3)) goto err; a = 1; while (!BN_is_bit_set(w1, a)) a++; if (!BN_rshift(m, w1, a)) goto err; mont = BN_MONT_CTX_new(); if (mont == NULL || !BN_MONT_CTX_set(mont, w, ctx)) goto err; if (iterations == 0) iterations = bn_mr_min_checks(BN_num_bits(w)); for (i = 0; i < iterations; ++i) { if (!BN_priv_rand_range_ex(b, w3, 0, ctx) || !BN_add_word(b, 2)) goto err; if (enhanced) { if (!BN_gcd(g, b, w, ctx)) goto err; if (!BN_is_one(g)) { *status = BN_PRIMETEST_COMPOSITE_WITH_FACTOR; ret = 1; goto err; } } if (!BN_mod_exp_mont(z, b, m, w, ctx, mont)) goto err; if (BN_is_one(z) || BN_cmp(z, w1) == 0) goto outer_loop; for (j = 1; j < a ; ++j) { if (!BN_copy(x, z) || !BN_mod_mul(z, x, x, w, ctx)) goto err; if (BN_cmp(z, w1) == 0) goto outer_loop; if (BN_is_one(z)) goto composite; } if (!BN_copy(x, z) || !BN_mod_mul(z, x, x, w, ctx)) goto err; if (BN_is_one(z)) goto composite; if (!BN_copy(x, z)) goto err; composite: if (enhanced) { if (!BN_sub_word(x, 1) || !BN_gcd(g, x, w, ctx)) goto err; if (BN_is_one(g)) *status = BN_PRIMETEST_COMPOSITE_NOT_POWER_OF_PRIME; else *status = BN_PRIMETEST_COMPOSITE_WITH_FACTOR; } else { *status = BN_PRIMETEST_COMPOSITE; } ret = 1; goto err; outer_loop: ; if (!BN_GENCB_call(cb, 1, i)) goto err; } *status = BN_PRIMETEST_PROBABLY_PRIME; ret = 1; err: BN_clear(g); BN_clear(w1); BN_clear(w3); BN_clear(x); BN_clear(m); BN_clear(z); BN_clear(b); BN_CTX_end(ctx); BN_MONT_CTX_free(mont); return ret; } static int probable_prime(BIGNUM *rnd, int bits, int safe, prime_t *mods, BN_CTX *ctx) { int i; BN_ULONG delta; int trial_divisions = calc_trial_divisions(bits); BN_ULONG maxdelta = BN_MASK2 - primes[trial_divisions - 1]; again: if (!BN_priv_rand_ex(rnd, bits, BN_RAND_TOP_TWO, BN_RAND_BOTTOM_ODD, 0, ctx)) return 0; if (safe && !BN_set_bit(rnd, 1)) return 0; for (i = 1; i < trial_divisions; i++) { BN_ULONG mod = BN_mod_word(rnd, (BN_ULONG)primes[i]); if (mod == (BN_ULONG)-1) return 0; mods[i] = (prime_t) mod; } delta = 0; loop: for (i = 1; i < trial_divisions; i++) { if (bits <= 31 && delta <= 0x7fffffff && square(primes[i]) > BN_get_word(rnd) + delta) break; if (safe ? (mods[i] + delta) % primes[i] <= 1 : (mods[i] + delta) % primes[i] == 0) { delta += safe ? 4 : 2; if (delta > maxdelta) goto again; goto loop; } } if (!BN_add_word(rnd, delta)) return 0; if (BN_num_bits(rnd) != bits) goto again; bn_check_top(rnd); return 1; } static int probable_prime_dh(BIGNUM *rnd, int bits, int safe, prime_t *mods, const BIGNUM *add, const BIGNUM *rem, BN_CTX *ctx) { int i, ret = 0; BIGNUM *t1; BN_ULONG delta; int trial_divisions = calc_trial_divisions(bits); BN_ULONG maxdelta = BN_MASK2 - primes[trial_divisions - 1]; BN_CTX_start(ctx); if ((t1 = BN_CTX_get(ctx)) == NULL) goto err; if (maxdelta > BN_MASK2 - BN_get_word(add)) maxdelta = BN_MASK2 - BN_get_word(add); again: if (!BN_rand_ex(rnd, bits, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ODD, 0, ctx)) goto err; if (!BN_mod(t1, rnd, add, ctx)) goto err; if (!BN_sub(rnd, rnd, t1)) goto err; if (rem == NULL) { if (!BN_add_word(rnd, safe ? 3u : 1u)) goto err; } else { if (!BN_add(rnd, rnd, rem)) goto err; } if (BN_num_bits(rnd) < bits || BN_get_word(rnd) < (safe ? 5u : 3u)) { if (!BN_add(rnd, rnd, add)) goto err; } for (i = 1; i < trial_divisions; i++) { BN_ULONG mod = BN_mod_word(rnd, (BN_ULONG)primes[i]); if (mod == (BN_ULONG)-1) goto err; mods[i] = (prime_t) mod; } delta = 0; loop: for (i = 1; i < trial_divisions; i++) { if (bits <= 31 && delta <= 0x7fffffff && square(primes[i]) > BN_get_word(rnd) + delta) break; if (safe ? (mods[i] + delta) % primes[i] <= 1 : (mods[i] + delta) % primes[i] == 0) { delta += BN_get_word(add); if (delta > maxdelta) goto again; goto loop; } } if (!BN_add_word(rnd, delta)) goto err; ret = 1; err: BN_CTX_end(ctx); bn_check_top(rnd); return ret; }
bn
openssl/crypto/bn/bn_prime.c
openssl
#include <stdio.h> #include <openssl/bio.h> #include "bn_local.h" static const char Hex[] = "0123456789ABCDEF"; #ifndef OPENSSL_NO_STDIO int BN_print_fp(FILE *fp, const BIGNUM *a) { BIO *b; int ret; if ((b = BIO_new(BIO_s_file())) == NULL) return 0; BIO_set_fp(b, fp, BIO_NOCLOSE); ret = BN_print(b, a); BIO_free(b); return ret; } #endif int BN_print(BIO *bp, const BIGNUM *a) { int i, j, v, z = 0; int ret = 0; if ((a->neg) && BIO_write(bp, "-", 1) != 1) goto end; if (BN_is_zero(a) && BIO_write(bp, "0", 1) != 1) goto end; for (i = a->top - 1; i >= 0; i--) { for (j = BN_BITS2 - 4; j >= 0; j -= 4) { v = (int)((a->d[i] >> j) & 0x0f); if (z || v != 0) { if (BIO_write(bp, &Hex[v], 1) != 1) goto end; z = 1; } } } ret = 1; end: return ret; } char *BN_options(void) { static int init = 0; static char data[16]; if (!init) { init++; #ifdef BN_LLONG BIO_snprintf(data, sizeof(data), "bn(%zu,%zu)", sizeof(BN_ULLONG) * 8, sizeof(BN_ULONG) * 8); #else BIO_snprintf(data, sizeof(data), "bn(%zu,%zu)", sizeof(BN_ULONG) * 8, sizeof(BN_ULONG) * 8); #endif } return data; }
bn
openssl/crypto/bn/bn_print.c
openssl
#include <openssl/crypto.h> #include <openssl/bn.h> #include "crypto/ppc_arch.h" #include "bn_local.h" int bn_mul_mont(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp, const BN_ULONG *np, const BN_ULONG *n0, int num) { int bn_mul_mont_int(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp, const BN_ULONG *np, const BN_ULONG *n0, int num); int bn_mul4x_mont_int(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp, const BN_ULONG *np, const BN_ULONG *n0, int num); int bn_mul_mont_fixed_n6(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp, const BN_ULONG *np, const BN_ULONG *n0, int num); int bn_mul_mont_300_fixed_n6(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp, const BN_ULONG *np, const BN_ULONG *n0, int num); if (num < 4) return 0; if ((num & 3) == 0) return bn_mul4x_mont_int(rp, ap, bp, np, n0, num); #if defined(_ARCH_PPC64) && !defined(__ILP32__) if (num == 6) { if (OPENSSL_ppccap_P & PPC_MADD300) return bn_mul_mont_300_fixed_n6(rp, ap, bp, np, n0, num); else return bn_mul_mont_fixed_n6(rp, ap, bp, np, n0, num); } #endif return bn_mul_mont_int(rp, ap, bp, np, n0, num); }
bn
openssl/crypto/bn/bn_ppc.c
openssl
#define OPENSSL_SUPPRESS_DEPRECATED #include <stdio.h> #include <openssl/bn.h> #include "bn_local.h" static int bn_x931_derive_pi(BIGNUM *pi, const BIGNUM *Xpi, BN_CTX *ctx, BN_GENCB *cb) { int i = 0, is_prime; if (!BN_copy(pi, Xpi)) return 0; if (!BN_is_odd(pi) && !BN_add_word(pi, 1)) return 0; for (;;) { i++; BN_GENCB_call(cb, 0, i); is_prime = BN_check_prime(pi, ctx, cb); if (is_prime < 0) return 0; if (is_prime) break; if (!BN_add_word(pi, 2)) return 0; } BN_GENCB_call(cb, 2, i); return 1; } int BN_X931_derive_prime_ex(BIGNUM *p, BIGNUM *p1, BIGNUM *p2, const BIGNUM *Xp, const BIGNUM *Xp1, const BIGNUM *Xp2, const BIGNUM *e, BN_CTX *ctx, BN_GENCB *cb) { int ret = 0; BIGNUM *t, *p1p2, *pm1; if (!BN_is_odd(e)) return 0; BN_CTX_start(ctx); if (p1 == NULL) p1 = BN_CTX_get(ctx); if (p2 == NULL) p2 = BN_CTX_get(ctx); t = BN_CTX_get(ctx); p1p2 = BN_CTX_get(ctx); pm1 = BN_CTX_get(ctx); if (pm1 == NULL) goto err; if (!bn_x931_derive_pi(p1, Xp1, ctx, cb)) goto err; if (!bn_x931_derive_pi(p2, Xp2, ctx, cb)) goto err; if (!BN_mul(p1p2, p1, p2, ctx)) goto err; if (!BN_mod_inverse(p, p2, p1, ctx)) goto err; if (!BN_mul(p, p, p2, ctx)) goto err; if (!BN_mod_inverse(t, p1, p2, ctx)) goto err; if (!BN_mul(t, t, p1, ctx)) goto err; if (!BN_sub(p, p, t)) goto err; if (p->neg && !BN_add(p, p, p1p2)) goto err; if (!BN_mod_sub(p, p, Xp, p1p2, ctx)) goto err; if (!BN_add(p, p, Xp)) goto err; for (;;) { int i = 1; BN_GENCB_call(cb, 0, i++); if (!BN_copy(pm1, p)) goto err; if (!BN_sub_word(pm1, 1)) goto err; if (!BN_gcd(t, pm1, e, ctx)) goto err; if (BN_is_one(t)) { int r = BN_check_prime(p, ctx, cb); if (r < 0) goto err; if (r) break; } if (!BN_add(p, p, p1p2)) goto err; } BN_GENCB_call(cb, 3, 0); ret = 1; err: BN_CTX_end(ctx); return ret; } int BN_X931_generate_Xpq(BIGNUM *Xp, BIGNUM *Xq, int nbits, BN_CTX *ctx) { BIGNUM *t; int i; if ((nbits < 1024) || (nbits & 0xff)) return 0; nbits >>= 1; if (!BN_priv_rand_ex(Xp, nbits, BN_RAND_TOP_TWO, BN_RAND_BOTTOM_ANY, 0, ctx)) return 0; BN_CTX_start(ctx); t = BN_CTX_get(ctx); if (t == NULL) goto err; for (i = 0; i < 1000; i++) { if (!BN_priv_rand_ex(Xq, nbits, BN_RAND_TOP_TWO, BN_RAND_BOTTOM_ANY, 0, ctx)) goto err; if (!BN_sub(t, Xp, Xq)) goto err; if (BN_num_bits(t) > (nbits - 100)) break; } BN_CTX_end(ctx); if (i < 1000) return 1; return 0; err: BN_CTX_end(ctx); return 0; } int BN_X931_generate_prime_ex(BIGNUM *p, BIGNUM *p1, BIGNUM *p2, BIGNUM *Xp1, BIGNUM *Xp2, const BIGNUM *Xp, const BIGNUM *e, BN_CTX *ctx, BN_GENCB *cb) { int ret = 0; BN_CTX_start(ctx); if (Xp1 == NULL) Xp1 = BN_CTX_get(ctx); if (Xp2 == NULL) Xp2 = BN_CTX_get(ctx); if (Xp1 == NULL || Xp2 == NULL) goto error; if (!BN_priv_rand_ex(Xp1, 101, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY, 0, ctx)) goto error; if (!BN_priv_rand_ex(Xp2, 101, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY, 0, ctx)) goto error; if (!BN_X931_derive_prime_ex(p, p1, p2, Xp, Xp1, Xp2, e, ctx, cb)) goto error; ret = 1; error: BN_CTX_end(ctx); return ret; }
bn
openssl/crypto/bn/bn_x931p.c
openssl
#include "../bn_local.h" #if !(defined(__GNUC__) && __GNUC__>=2) # include "../bn_asm.c" #else # undef mul # undef mul_add # define mul_add(r,a,word,carry) do { \ register BN_ULONG high,low; \ asm ("mulq %3" \ : "=a"(low),"=d"(high) \ : "a"(word),"m"(a) \ : "cc"); \ asm ("addq %2,%0; adcq %3,%1" \ : "+r"(carry),"+d"(high)\ : "a"(low),"g"(0) \ : "cc"); \ asm ("addq %2,%0; adcq %3,%1" \ : "+m"(r),"+d"(high) \ : "r"(carry),"g"(0) \ : "cc"); \ carry=high; \ } while (0) # define mul(r,a,word,carry) do { \ register BN_ULONG high,low; \ asm ("mulq %3" \ : "=a"(low),"=d"(high) \ : "a"(word),"g"(a) \ : "cc"); \ asm ("addq %2,%0; adcq %3,%1" \ : "+r"(carry),"+d"(high)\ : "a"(low),"g"(0) \ : "cc"); \ (r)=carry, carry=high; \ } while (0) # undef sqr # define sqr(r0,r1,a) \ asm ("mulq %2" \ : "=a"(r0),"=d"(r1) \ : "a"(a) \ : "cc"); BN_ULONG bn_mul_add_words(BN_ULONG *rp, const BN_ULONG *ap, int num, BN_ULONG w) { BN_ULONG c1 = 0; if (num <= 0) return c1; while (num & ~3) { mul_add(rp[0], ap[0], w, c1); mul_add(rp[1], ap[1], w, c1); mul_add(rp[2], ap[2], w, c1); mul_add(rp[3], ap[3], w, c1); ap += 4; rp += 4; num -= 4; } if (num) { mul_add(rp[0], ap[0], w, c1); if (--num == 0) return c1; mul_add(rp[1], ap[1], w, c1); if (--num == 0) return c1; mul_add(rp[2], ap[2], w, c1); return c1; } return c1; } BN_ULONG bn_mul_words(BN_ULONG *rp, const BN_ULONG *ap, int num, BN_ULONG w) { BN_ULONG c1 = 0; if (num <= 0) return c1; while (num & ~3) { mul(rp[0], ap[0], w, c1); mul(rp[1], ap[1], w, c1); mul(rp[2], ap[2], w, c1); mul(rp[3], ap[3], w, c1); ap += 4; rp += 4; num -= 4; } if (num) { mul(rp[0], ap[0], w, c1); if (--num == 0) return c1; mul(rp[1], ap[1], w, c1); if (--num == 0) return c1; mul(rp[2], ap[2], w, c1); } return c1; } void bn_sqr_words(BN_ULONG *r, const BN_ULONG *a, int n) { if (n <= 0) return; while (n & ~3) { sqr(r[0], r[1], a[0]); sqr(r[2], r[3], a[1]); sqr(r[4], r[5], a[2]); sqr(r[6], r[7], a[3]); a += 4; r += 8; n -= 4; } if (n) { sqr(r[0], r[1], a[0]); if (--n == 0) return; sqr(r[2], r[3], a[1]); if (--n == 0) return; sqr(r[4], r[5], a[2]); } } BN_ULONG bn_div_words(BN_ULONG h, BN_ULONG l, BN_ULONG d) { BN_ULONG ret, waste; asm("divq %4":"=a"(ret), "=d"(waste) : "a"(l), "d"(h), "r"(d) : "cc"); return ret; } BN_ULONG bn_add_words(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp, int n) { BN_ULONG ret; size_t i = 0; if (n <= 0) return 0; asm volatile (" subq %0,%0 \n" " jmp 1f \n" ".p2align 4 \n" "1: movq (%4,%2,8),%0 \n" " adcq (%5,%2,8),%0 \n" " movq %0,(%3,%2,8) \n" " lea 1(%2),%2 \n" " dec %1 \n" " jnz 1b \n" " sbbq %0,%0 \n" :"=&r" (ret), "+c"(n), "+r"(i) :"r"(rp), "r"(ap), "r"(bp) :"cc", "memory"); return ret & 1; } # ifndef SIMICS BN_ULONG bn_sub_words(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp, int n) { BN_ULONG ret; size_t i = 0; if (n <= 0) return 0; asm volatile (" subq %0,%0 \n" " jmp 1f \n" ".p2align 4 \n" "1: movq (%4,%2,8),%0 \n" " sbbq (%5,%2,8),%0 \n" " movq %0,(%3,%2,8) \n" " lea 1(%2),%2 \n" " dec %1 \n" " jnz 1b \n" " sbbq %0,%0 \n" :"=&r" (ret), "+c"(n), "+r"(i) :"r"(rp), "r"(ap), "r"(bp) :"cc", "memory"); return ret & 1; } # else # define BN_MASK2 0xffffffffffffffffL BN_ULONG bn_sub_words(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n) { BN_ULONG t1, t2; int c = 0; if (n <= 0) return (BN_ULONG)0; for (;;) { t1 = a[0]; t2 = b[0]; r[0] = (t1 - t2 - c) & BN_MASK2; if (t1 != t2) c = (t1 < t2); if (--n <= 0) break; t1 = a[1]; t2 = b[1]; r[1] = (t1 - t2 - c) & BN_MASK2; if (t1 != t2) c = (t1 < t2); if (--n <= 0) break; t1 = a[2]; t2 = b[2]; r[2] = (t1 - t2 - c) & BN_MASK2; if (t1 != t2) c = (t1 < t2); if (--n <= 0) break; t1 = a[3]; t2 = b[3]; r[3] = (t1 - t2 - c) & BN_MASK2; if (t1 != t2) c = (t1 < t2); if (--n <= 0) break; a += 4; b += 4; r += 4; } return c; } # endif # if 0 # define mul_add_c(a,b,c0,c1,c2) do { \ BN_ULONG ta = (a), tb = (b); \ BN_ULONG lo, hi; \ BN_UMULT_LOHI(lo,hi,ta,tb); \ c0 += lo; hi += (c0<lo)?1:0; \ c1 += hi; c2 += (c1<hi)?1:0; \ } while(0) # define mul_add_c2(a,b,c0,c1,c2) do { \ BN_ULONG ta = (a), tb = (b); \ BN_ULONG lo, hi, tt; \ BN_UMULT_LOHI(lo,hi,ta,tb); \ c0 += lo; tt = hi+((c0<lo)?1:0); \ c1 += tt; c2 += (c1<tt)?1:0; \ c0 += lo; hi += (c0<lo)?1:0; \ c1 += hi; c2 += (c1<hi)?1:0; \ } while(0) # define sqr_add_c(a,i,c0,c1,c2) do { \ BN_ULONG ta = (a)[i]; \ BN_ULONG lo, hi; \ BN_UMULT_LOHI(lo,hi,ta,ta); \ c0 += lo; hi += (c0<lo)?1:0; \ c1 += hi; c2 += (c1<hi)?1:0; \ } while(0) # else # define mul_add_c(a,b,c0,c1,c2) do { \ BN_ULONG t1,t2; \ asm ("mulq %3" \ : "=a"(t1),"=d"(t2) \ : "a"(a),"m"(b) \ : "cc"); \ asm ("addq %3,%0; adcq %4,%1; adcq %5,%2" \ : "+r"(c0),"+r"(c1),"+r"(c2) \ : "r"(t1),"r"(t2),"g"(0) \ : "cc"); \ } while (0) # define sqr_add_c(a,i,c0,c1,c2) do { \ BN_ULONG t1,t2; \ asm ("mulq %2" \ : "=a"(t1),"=d"(t2) \ : "a"(a[i]) \ : "cc"); \ asm ("addq %3,%0; adcq %4,%1; adcq %5,%2" \ : "+r"(c0),"+r"(c1),"+r"(c2) \ : "r"(t1),"r"(t2),"g"(0) \ : "cc"); \ } while (0) # define mul_add_c2(a,b,c0,c1,c2) do { \ BN_ULONG t1,t2; \ asm ("mulq %3" \ : "=a"(t1),"=d"(t2) \ : "a"(a),"m"(b) \ : "cc"); \ asm ("addq %3,%0; adcq %4,%1; adcq %5,%2" \ : "+r"(c0),"+r"(c1),"+r"(c2) \ : "r"(t1),"r"(t2),"g"(0) \ : "cc"); \ asm ("addq %3,%0; adcq %4,%1; adcq %5,%2" \ : "+r"(c0),"+r"(c1),"+r"(c2) \ : "r"(t1),"r"(t2),"g"(0) \ : "cc"); \ } while (0) # endif # define sqr_add_c2(a,i,j,c0,c1,c2) \ mul_add_c2((a)[i],(a)[j],c0,c1,c2) void bn_mul_comba8(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b) { BN_ULONG c1, c2, c3; c1 = 0; c2 = 0; c3 = 0; mul_add_c(a[0], b[0], c1, c2, c3); r[0] = c1; c1 = 0; mul_add_c(a[0], b[1], c2, c3, c1); mul_add_c(a[1], b[0], c2, c3, c1); r[1] = c2; c2 = 0; mul_add_c(a[2], b[0], c3, c1, c2); mul_add_c(a[1], b[1], c3, c1, c2); mul_add_c(a[0], b[2], c3, c1, c2); r[2] = c3; c3 = 0; mul_add_c(a[0], b[3], c1, c2, c3); mul_add_c(a[1], b[2], c1, c2, c3); mul_add_c(a[2], b[1], c1, c2, c3); mul_add_c(a[3], b[0], c1, c2, c3); r[3] = c1; c1 = 0; mul_add_c(a[4], b[0], c2, c3, c1); mul_add_c(a[3], b[1], c2, c3, c1); mul_add_c(a[2], b[2], c2, c3, c1); mul_add_c(a[1], b[3], c2, c3, c1); mul_add_c(a[0], b[4], c2, c3, c1); r[4] = c2; c2 = 0; mul_add_c(a[0], b[5], c3, c1, c2); mul_add_c(a[1], b[4], c3, c1, c2); mul_add_c(a[2], b[3], c3, c1, c2); mul_add_c(a[3], b[2], c3, c1, c2); mul_add_c(a[4], b[1], c3, c1, c2); mul_add_c(a[5], b[0], c3, c1, c2); r[5] = c3; c3 = 0; mul_add_c(a[6], b[0], c1, c2, c3); mul_add_c(a[5], b[1], c1, c2, c3); mul_add_c(a[4], b[2], c1, c2, c3); mul_add_c(a[3], b[3], c1, c2, c3); mul_add_c(a[2], b[4], c1, c2, c3); mul_add_c(a[1], b[5], c1, c2, c3); mul_add_c(a[0], b[6], c1, c2, c3); r[6] = c1; c1 = 0; mul_add_c(a[0], b[7], c2, c3, c1); mul_add_c(a[1], b[6], c2, c3, c1); mul_add_c(a[2], b[5], c2, c3, c1); mul_add_c(a[3], b[4], c2, c3, c1); mul_add_c(a[4], b[3], c2, c3, c1); mul_add_c(a[5], b[2], c2, c3, c1); mul_add_c(a[6], b[1], c2, c3, c1); mul_add_c(a[7], b[0], c2, c3, c1); r[7] = c2; c2 = 0; mul_add_c(a[7], b[1], c3, c1, c2); mul_add_c(a[6], b[2], c3, c1, c2); mul_add_c(a[5], b[3], c3, c1, c2); mul_add_c(a[4], b[4], c3, c1, c2); mul_add_c(a[3], b[5], c3, c1, c2); mul_add_c(a[2], b[6], c3, c1, c2); mul_add_c(a[1], b[7], c3, c1, c2); r[8] = c3; c3 = 0; mul_add_c(a[2], b[7], c1, c2, c3); mul_add_c(a[3], b[6], c1, c2, c3); mul_add_c(a[4], b[5], c1, c2, c3); mul_add_c(a[5], b[4], c1, c2, c3); mul_add_c(a[6], b[3], c1, c2, c3); mul_add_c(a[7], b[2], c1, c2, c3); r[9] = c1; c1 = 0; mul_add_c(a[7], b[3], c2, c3, c1); mul_add_c(a[6], b[4], c2, c3, c1); mul_add_c(a[5], b[5], c2, c3, c1); mul_add_c(a[4], b[6], c2, c3, c1); mul_add_c(a[3], b[7], c2, c3, c1); r[10] = c2; c2 = 0; mul_add_c(a[4], b[7], c3, c1, c2); mul_add_c(a[5], b[6], c3, c1, c2); mul_add_c(a[6], b[5], c3, c1, c2); mul_add_c(a[7], b[4], c3, c1, c2); r[11] = c3; c3 = 0; mul_add_c(a[7], b[5], c1, c2, c3); mul_add_c(a[6], b[6], c1, c2, c3); mul_add_c(a[5], b[7], c1, c2, c3); r[12] = c1; c1 = 0; mul_add_c(a[6], b[7], c2, c3, c1); mul_add_c(a[7], b[6], c2, c3, c1); r[13] = c2; c2 = 0; mul_add_c(a[7], b[7], c3, c1, c2); r[14] = c3; r[15] = c1; } void bn_mul_comba4(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b) { BN_ULONG c1, c2, c3; c1 = 0; c2 = 0; c3 = 0; mul_add_c(a[0], b[0], c1, c2, c3); r[0] = c1; c1 = 0; mul_add_c(a[0], b[1], c2, c3, c1); mul_add_c(a[1], b[0], c2, c3, c1); r[1] = c2; c2 = 0; mul_add_c(a[2], b[0], c3, c1, c2); mul_add_c(a[1], b[1], c3, c1, c2); mul_add_c(a[0], b[2], c3, c1, c2); r[2] = c3; c3 = 0; mul_add_c(a[0], b[3], c1, c2, c3); mul_add_c(a[1], b[2], c1, c2, c3); mul_add_c(a[2], b[1], c1, c2, c3); mul_add_c(a[3], b[0], c1, c2, c3); r[3] = c1; c1 = 0; mul_add_c(a[3], b[1], c2, c3, c1); mul_add_c(a[2], b[2], c2, c3, c1); mul_add_c(a[1], b[3], c2, c3, c1); r[4] = c2; c2 = 0; mul_add_c(a[2], b[3], c3, c1, c2); mul_add_c(a[3], b[2], c3, c1, c2); r[5] = c3; c3 = 0; mul_add_c(a[3], b[3], c1, c2, c3); r[6] = c1; r[7] = c2; } void bn_sqr_comba8(BN_ULONG *r, const BN_ULONG *a) { BN_ULONG c1, c2, c3; c1 = 0; c2 = 0; c3 = 0; sqr_add_c(a, 0, c1, c2, c3); r[0] = c1; c1 = 0; sqr_add_c2(a, 1, 0, c2, c3, c1); r[1] = c2; c2 = 0; sqr_add_c(a, 1, c3, c1, c2); sqr_add_c2(a, 2, 0, c3, c1, c2); r[2] = c3; c3 = 0; sqr_add_c2(a, 3, 0, c1, c2, c3); sqr_add_c2(a, 2, 1, c1, c2, c3); r[3] = c1; c1 = 0; sqr_add_c(a, 2, c2, c3, c1); sqr_add_c2(a, 3, 1, c2, c3, c1); sqr_add_c2(a, 4, 0, c2, c3, c1); r[4] = c2; c2 = 0; sqr_add_c2(a, 5, 0, c3, c1, c2); sqr_add_c2(a, 4, 1, c3, c1, c2); sqr_add_c2(a, 3, 2, c3, c1, c2); r[5] = c3; c3 = 0; sqr_add_c(a, 3, c1, c2, c3); sqr_add_c2(a, 4, 2, c1, c2, c3); sqr_add_c2(a, 5, 1, c1, c2, c3); sqr_add_c2(a, 6, 0, c1, c2, c3); r[6] = c1; c1 = 0; sqr_add_c2(a, 7, 0, c2, c3, c1); sqr_add_c2(a, 6, 1, c2, c3, c1); sqr_add_c2(a, 5, 2, c2, c3, c1); sqr_add_c2(a, 4, 3, c2, c3, c1); r[7] = c2; c2 = 0; sqr_add_c(a, 4, c3, c1, c2); sqr_add_c2(a, 5, 3, c3, c1, c2); sqr_add_c2(a, 6, 2, c3, c1, c2); sqr_add_c2(a, 7, 1, c3, c1, c2); r[8] = c3; c3 = 0; sqr_add_c2(a, 7, 2, c1, c2, c3); sqr_add_c2(a, 6, 3, c1, c2, c3); sqr_add_c2(a, 5, 4, c1, c2, c3); r[9] = c1; c1 = 0; sqr_add_c(a, 5, c2, c3, c1); sqr_add_c2(a, 6, 4, c2, c3, c1); sqr_add_c2(a, 7, 3, c2, c3, c1); r[10] = c2; c2 = 0; sqr_add_c2(a, 7, 4, c3, c1, c2); sqr_add_c2(a, 6, 5, c3, c1, c2); r[11] = c3; c3 = 0; sqr_add_c(a, 6, c1, c2, c3); sqr_add_c2(a, 7, 5, c1, c2, c3); r[12] = c1; c1 = 0; sqr_add_c2(a, 7, 6, c2, c3, c1); r[13] = c2; c2 = 0; sqr_add_c(a, 7, c3, c1, c2); r[14] = c3; r[15] = c1; } void bn_sqr_comba4(BN_ULONG *r, const BN_ULONG *a) { BN_ULONG c1, c2, c3; c1 = 0; c2 = 0; c3 = 0; sqr_add_c(a, 0, c1, c2, c3); r[0] = c1; c1 = 0; sqr_add_c2(a, 1, 0, c2, c3, c1); r[1] = c2; c2 = 0; sqr_add_c(a, 1, c3, c1, c2); sqr_add_c2(a, 2, 0, c3, c1, c2); r[2] = c3; c3 = 0; sqr_add_c2(a, 3, 0, c1, c2, c3); sqr_add_c2(a, 2, 1, c1, c2, c3); r[3] = c1; c1 = 0; sqr_add_c(a, 2, c2, c3, c1); sqr_add_c2(a, 3, 1, c2, c3, c1); r[4] = c2; c2 = 0; sqr_add_c2(a, 3, 2, c3, c1, c2); r[5] = c3; c3 = 0; sqr_add_c(a, 3, c1, c2, c3); r[6] = c1; r[7] = c2; } #endif
asm
openssl/crypto/bn/asm/x86_64-gcc.c
openssl
#include "internal/deprecated.h" #include <stdio.h> #include <string.h> #include <openssl/ripemd.h> #include <openssl/crypto.h> unsigned char *RIPEMD160(const unsigned char *d, size_t n, unsigned char *md) { RIPEMD160_CTX c; static unsigned char m[RIPEMD160_DIGEST_LENGTH]; if (md == NULL) md = m; if (!RIPEMD160_Init(&c)) return NULL; RIPEMD160_Update(&c, d, n); RIPEMD160_Final(md, &c); OPENSSL_cleanse(&c, sizeof(c)); return md; }
ripemd
openssl/crypto/ripemd/rmd_one.c
openssl
#include "internal/deprecated.h" #include <stdio.h> #include "rmd_local.h" #include <openssl/opensslv.h> #ifdef RMD160_ASM void ripemd160_block_x86(RIPEMD160_CTX *c, unsigned long *p, size_t num); # define ripemd160_block ripemd160_block_x86 #else void ripemd160_block(RIPEMD160_CTX *c, unsigned long *p, size_t num); #endif int RIPEMD160_Init(RIPEMD160_CTX *c) { memset(c, 0, sizeof(*c)); c->A = RIPEMD160_A; c->B = RIPEMD160_B; c->C = RIPEMD160_C; c->D = RIPEMD160_D; c->E = RIPEMD160_E; return 1; } #ifndef ripemd160_block_data_order # ifdef X # undef X # endif void ripemd160_block_data_order(RIPEMD160_CTX *ctx, const void *p, size_t num) { const unsigned char *data = p; register unsigned MD32_REG_T A, B, C, D, E; unsigned MD32_REG_T a, b, c, d, e, l; # ifndef MD32_XARRAY unsigned MD32_REG_T XX0, XX1, XX2, XX3, XX4, XX5, XX6, XX7, XX8, XX9, XX10, XX11, XX12, XX13, XX14, XX15; # define X(i) XX##i # else RIPEMD160_LONG XX[16]; # define X(i) XX[i] # endif for (; num--;) { A = ctx->A; B = ctx->B; C = ctx->C; D = ctx->D; E = ctx->E; (void)HOST_c2l(data, l); X(0) = l; (void)HOST_c2l(data, l); X(1) = l; RIP1(A, B, C, D, E, WL00, SL00); (void)HOST_c2l(data, l); X(2) = l; RIP1(E, A, B, C, D, WL01, SL01); (void)HOST_c2l(data, l); X(3) = l; RIP1(D, E, A, B, C, WL02, SL02); (void)HOST_c2l(data, l); X(4) = l; RIP1(C, D, E, A, B, WL03, SL03); (void)HOST_c2l(data, l); X(5) = l; RIP1(B, C, D, E, A, WL04, SL04); (void)HOST_c2l(data, l); X(6) = l; RIP1(A, B, C, D, E, WL05, SL05); (void)HOST_c2l(data, l); X(7) = l; RIP1(E, A, B, C, D, WL06, SL06); (void)HOST_c2l(data, l); X(8) = l; RIP1(D, E, A, B, C, WL07, SL07); (void)HOST_c2l(data, l); X(9) = l; RIP1(C, D, E, A, B, WL08, SL08); (void)HOST_c2l(data, l); X(10) = l; RIP1(B, C, D, E, A, WL09, SL09); (void)HOST_c2l(data, l); X(11) = l; RIP1(A, B, C, D, E, WL10, SL10); (void)HOST_c2l(data, l); X(12) = l; RIP1(E, A, B, C, D, WL11, SL11); (void)HOST_c2l(data, l); X(13) = l; RIP1(D, E, A, B, C, WL12, SL12); (void)HOST_c2l(data, l); X(14) = l; RIP1(C, D, E, A, B, WL13, SL13); (void)HOST_c2l(data, l); X(15) = l; RIP1(B, C, D, E, A, WL14, SL14); RIP1(A, B, C, D, E, WL15, SL15); RIP2(E, A, B, C, D, WL16, SL16, KL1); RIP2(D, E, A, B, C, WL17, SL17, KL1); RIP2(C, D, E, A, B, WL18, SL18, KL1); RIP2(B, C, D, E, A, WL19, SL19, KL1); RIP2(A, B, C, D, E, WL20, SL20, KL1); RIP2(E, A, B, C, D, WL21, SL21, KL1); RIP2(D, E, A, B, C, WL22, SL22, KL1); RIP2(C, D, E, A, B, WL23, SL23, KL1); RIP2(B, C, D, E, A, WL24, SL24, KL1); RIP2(A, B, C, D, E, WL25, SL25, KL1); RIP2(E, A, B, C, D, WL26, SL26, KL1); RIP2(D, E, A, B, C, WL27, SL27, KL1); RIP2(C, D, E, A, B, WL28, SL28, KL1); RIP2(B, C, D, E, A, WL29, SL29, KL1); RIP2(A, B, C, D, E, WL30, SL30, KL1); RIP2(E, A, B, C, D, WL31, SL31, KL1); RIP3(D, E, A, B, C, WL32, SL32, KL2); RIP3(C, D, E, A, B, WL33, SL33, KL2); RIP3(B, C, D, E, A, WL34, SL34, KL2); RIP3(A, B, C, D, E, WL35, SL35, KL2); RIP3(E, A, B, C, D, WL36, SL36, KL2); RIP3(D, E, A, B, C, WL37, SL37, KL2); RIP3(C, D, E, A, B, WL38, SL38, KL2); RIP3(B, C, D, E, A, WL39, SL39, KL2); RIP3(A, B, C, D, E, WL40, SL40, KL2); RIP3(E, A, B, C, D, WL41, SL41, KL2); RIP3(D, E, A, B, C, WL42, SL42, KL2); RIP3(C, D, E, A, B, WL43, SL43, KL2); RIP3(B, C, D, E, A, WL44, SL44, KL2); RIP3(A, B, C, D, E, WL45, SL45, KL2); RIP3(E, A, B, C, D, WL46, SL46, KL2); RIP3(D, E, A, B, C, WL47, SL47, KL2); RIP4(C, D, E, A, B, WL48, SL48, KL3); RIP4(B, C, D, E, A, WL49, SL49, KL3); RIP4(A, B, C, D, E, WL50, SL50, KL3); RIP4(E, A, B, C, D, WL51, SL51, KL3); RIP4(D, E, A, B, C, WL52, SL52, KL3); RIP4(C, D, E, A, B, WL53, SL53, KL3); RIP4(B, C, D, E, A, WL54, SL54, KL3); RIP4(A, B, C, D, E, WL55, SL55, KL3); RIP4(E, A, B, C, D, WL56, SL56, KL3); RIP4(D, E, A, B, C, WL57, SL57, KL3); RIP4(C, D, E, A, B, WL58, SL58, KL3); RIP4(B, C, D, E, A, WL59, SL59, KL3); RIP4(A, B, C, D, E, WL60, SL60, KL3); RIP4(E, A, B, C, D, WL61, SL61, KL3); RIP4(D, E, A, B, C, WL62, SL62, KL3); RIP4(C, D, E, A, B, WL63, SL63, KL3); RIP5(B, C, D, E, A, WL64, SL64, KL4); RIP5(A, B, C, D, E, WL65, SL65, KL4); RIP5(E, A, B, C, D, WL66, SL66, KL4); RIP5(D, E, A, B, C, WL67, SL67, KL4); RIP5(C, D, E, A, B, WL68, SL68, KL4); RIP5(B, C, D, E, A, WL69, SL69, KL4); RIP5(A, B, C, D, E, WL70, SL70, KL4); RIP5(E, A, B, C, D, WL71, SL71, KL4); RIP5(D, E, A, B, C, WL72, SL72, KL4); RIP5(C, D, E, A, B, WL73, SL73, KL4); RIP5(B, C, D, E, A, WL74, SL74, KL4); RIP5(A, B, C, D, E, WL75, SL75, KL4); RIP5(E, A, B, C, D, WL76, SL76, KL4); RIP5(D, E, A, B, C, WL77, SL77, KL4); RIP5(C, D, E, A, B, WL78, SL78, KL4); RIP5(B, C, D, E, A, WL79, SL79, KL4); a = A; b = B; c = C; d = D; e = E; A = ctx->A; B = ctx->B; C = ctx->C; D = ctx->D; E = ctx->E; RIP5(A, B, C, D, E, WR00, SR00, KR0); RIP5(E, A, B, C, D, WR01, SR01, KR0); RIP5(D, E, A, B, C, WR02, SR02, KR0); RIP5(C, D, E, A, B, WR03, SR03, KR0); RIP5(B, C, D, E, A, WR04, SR04, KR0); RIP5(A, B, C, D, E, WR05, SR05, KR0); RIP5(E, A, B, C, D, WR06, SR06, KR0); RIP5(D, E, A, B, C, WR07, SR07, KR0); RIP5(C, D, E, A, B, WR08, SR08, KR0); RIP5(B, C, D, E, A, WR09, SR09, KR0); RIP5(A, B, C, D, E, WR10, SR10, KR0); RIP5(E, A, B, C, D, WR11, SR11, KR0); RIP5(D, E, A, B, C, WR12, SR12, KR0); RIP5(C, D, E, A, B, WR13, SR13, KR0); RIP5(B, C, D, E, A, WR14, SR14, KR0); RIP5(A, B, C, D, E, WR15, SR15, KR0); RIP4(E, A, B, C, D, WR16, SR16, KR1); RIP4(D, E, A, B, C, WR17, SR17, KR1); RIP4(C, D, E, A, B, WR18, SR18, KR1); RIP4(B, C, D, E, A, WR19, SR19, KR1); RIP4(A, B, C, D, E, WR20, SR20, KR1); RIP4(E, A, B, C, D, WR21, SR21, KR1); RIP4(D, E, A, B, C, WR22, SR22, KR1); RIP4(C, D, E, A, B, WR23, SR23, KR1); RIP4(B, C, D, E, A, WR24, SR24, KR1); RIP4(A, B, C, D, E, WR25, SR25, KR1); RIP4(E, A, B, C, D, WR26, SR26, KR1); RIP4(D, E, A, B, C, WR27, SR27, KR1); RIP4(C, D, E, A, B, WR28, SR28, KR1); RIP4(B, C, D, E, A, WR29, SR29, KR1); RIP4(A, B, C, D, E, WR30, SR30, KR1); RIP4(E, A, B, C, D, WR31, SR31, KR1); RIP3(D, E, A, B, C, WR32, SR32, KR2); RIP3(C, D, E, A, B, WR33, SR33, KR2); RIP3(B, C, D, E, A, WR34, SR34, KR2); RIP3(A, B, C, D, E, WR35, SR35, KR2); RIP3(E, A, B, C, D, WR36, SR36, KR2); RIP3(D, E, A, B, C, WR37, SR37, KR2); RIP3(C, D, E, A, B, WR38, SR38, KR2); RIP3(B, C, D, E, A, WR39, SR39, KR2); RIP3(A, B, C, D, E, WR40, SR40, KR2); RIP3(E, A, B, C, D, WR41, SR41, KR2); RIP3(D, E, A, B, C, WR42, SR42, KR2); RIP3(C, D, E, A, B, WR43, SR43, KR2); RIP3(B, C, D, E, A, WR44, SR44, KR2); RIP3(A, B, C, D, E, WR45, SR45, KR2); RIP3(E, A, B, C, D, WR46, SR46, KR2); RIP3(D, E, A, B, C, WR47, SR47, KR2); RIP2(C, D, E, A, B, WR48, SR48, KR3); RIP2(B, C, D, E, A, WR49, SR49, KR3); RIP2(A, B, C, D, E, WR50, SR50, KR3); RIP2(E, A, B, C, D, WR51, SR51, KR3); RIP2(D, E, A, B, C, WR52, SR52, KR3); RIP2(C, D, E, A, B, WR53, SR53, KR3); RIP2(B, C, D, E, A, WR54, SR54, KR3); RIP2(A, B, C, D, E, WR55, SR55, KR3); RIP2(E, A, B, C, D, WR56, SR56, KR3); RIP2(D, E, A, B, C, WR57, SR57, KR3); RIP2(C, D, E, A, B, WR58, SR58, KR3); RIP2(B, C, D, E, A, WR59, SR59, KR3); RIP2(A, B, C, D, E, WR60, SR60, KR3); RIP2(E, A, B, C, D, WR61, SR61, KR3); RIP2(D, E, A, B, C, WR62, SR62, KR3); RIP2(C, D, E, A, B, WR63, SR63, KR3); RIP1(B, C, D, E, A, WR64, SR64); RIP1(A, B, C, D, E, WR65, SR65); RIP1(E, A, B, C, D, WR66, SR66); RIP1(D, E, A, B, C, WR67, SR67); RIP1(C, D, E, A, B, WR68, SR68); RIP1(B, C, D, E, A, WR69, SR69); RIP1(A, B, C, D, E, WR70, SR70); RIP1(E, A, B, C, D, WR71, SR71); RIP1(D, E, A, B, C, WR72, SR72); RIP1(C, D, E, A, B, WR73, SR73); RIP1(B, C, D, E, A, WR74, SR74); RIP1(A, B, C, D, E, WR75, SR75); RIP1(E, A, B, C, D, WR76, SR76); RIP1(D, E, A, B, C, WR77, SR77); RIP1(C, D, E, A, B, WR78, SR78); RIP1(B, C, D, E, A, WR79, SR79); D = ctx->B + c + D; ctx->B = ctx->C + d + E; ctx->C = ctx->D + e + A; ctx->D = ctx->E + a + B; ctx->E = ctx->A + b + C; ctx->A = D; } } #endif
ripemd
openssl/crypto/ripemd/rmd_dgst.c
openssl
#include "internal/deprecated.h" #include <openssl/rc4.h> #include "rc4_local.h" void RC4(RC4_KEY *key, size_t len, const unsigned char *indata, unsigned char *outdata) { register RC4_INT *d; register RC4_INT x, y, tx, ty; size_t i; x = key->x; y = key->y; d = key->data; #define LOOP(in,out) \ x=((x+1)&0xff); \ tx=d[x]; \ y=(tx+y)&0xff; \ d[x]=ty=d[y]; \ d[y]=tx; \ (out) = d[(tx+ty)&0xff]^ (in); i = len >> 3; if (i) { for (;;) { LOOP(indata[0], outdata[0]); LOOP(indata[1], outdata[1]); LOOP(indata[2], outdata[2]); LOOP(indata[3], outdata[3]); LOOP(indata[4], outdata[4]); LOOP(indata[5], outdata[5]); LOOP(indata[6], outdata[6]); LOOP(indata[7], outdata[7]); indata += 8; outdata += 8; if (--i == 0) break; } } i = len & 0x07; if (i) { for (;;) { LOOP(indata[0], outdata[0]); if (--i == 0) break; LOOP(indata[1], outdata[1]); if (--i == 0) break; LOOP(indata[2], outdata[2]); if (--i == 0) break; LOOP(indata[3], outdata[3]); if (--i == 0) break; LOOP(indata[4], outdata[4]); if (--i == 0) break; LOOP(indata[5], outdata[5]); if (--i == 0) break; LOOP(indata[6], outdata[6]); if (--i == 0) break; } } key->x = x; key->y = y; }
rc4
openssl/crypto/rc4/rc4_enc.c
openssl
#include "internal/deprecated.h" #include <openssl/rc4.h> #include "rc4_local.h" #include <openssl/opensslv.h> const char *RC4_options(void) { if (sizeof(RC4_INT) == 1) return "rc4(char)"; else return "rc4(int)"; } void RC4_set_key(RC4_KEY *key, int len, const unsigned char *data) { register RC4_INT tmp; register int id1, id2; register RC4_INT *d; unsigned int i; d = &(key->data[0]); key->x = 0; key->y = 0; id1 = id2 = 0; #define SK_LOOP(d,n) { \ tmp=d[(n)]; \ id2 = (data[id1] + tmp + id2) & 0xff; \ if (++id1 == len) id1=0; \ d[(n)]=d[id2]; \ d[id2]=tmp; } for (i = 0; i < 256; i++) d[i] = i; for (i = 0; i < 256; i += 4) { SK_LOOP(d, i + 0); SK_LOOP(d, i + 1); SK_LOOP(d, i + 2); SK_LOOP(d, i + 3); } }
rc4
openssl/crypto/rc4/rc4_skey.c
openssl
#include "internal/deprecated.h" #include <openssl/crypto.h> #include "wp_local.h" #include <string.h> int WHIRLPOOL_Init(WHIRLPOOL_CTX *c) { memset(c, 0, sizeof(*c)); return 1; } int WHIRLPOOL_Update(WHIRLPOOL_CTX *c, const void *_inp, size_t bytes) { size_t chunk = ((size_t)1) << (sizeof(size_t) * 8 - 4); const unsigned char *inp = _inp; while (bytes >= chunk) { WHIRLPOOL_BitUpdate(c, inp, chunk * 8); bytes -= chunk; inp += chunk; } if (bytes) WHIRLPOOL_BitUpdate(c, inp, bytes * 8); return 1; } void WHIRLPOOL_BitUpdate(WHIRLPOOL_CTX *c, const void *_inp, size_t bits) { size_t n; unsigned int bitoff = c->bitoff, bitrem = bitoff % 8, inpgap = (8 - (unsigned int)bits % 8) & 7; const unsigned char *inp = _inp; c->bitlen[0] += bits; if (c->bitlen[0] < bits) { n = 1; do { c->bitlen[n]++; } while (c->bitlen[n] == 0 && ++n < (WHIRLPOOL_COUNTER / sizeof(size_t))); } #ifndef OPENSSL_SMALL_FOOTPRINT reconsider: if (inpgap == 0 && bitrem == 0) { while (bits) { if (bitoff == 0 && (n = bits / WHIRLPOOL_BBLOCK)) { whirlpool_block(c, inp, n); inp += n * WHIRLPOOL_BBLOCK / 8; bits %= WHIRLPOOL_BBLOCK; } else { unsigned int byteoff = bitoff / 8; bitrem = WHIRLPOOL_BBLOCK - bitoff; if (bits >= bitrem) { bits -= bitrem; bitrem /= 8; memcpy(c->data + byteoff, inp, bitrem); inp += bitrem; whirlpool_block(c, c->data, 1); bitoff = 0; } else { memcpy(c->data + byteoff, inp, bits / 8); bitoff += (unsigned int)bits; bits = 0; } c->bitoff = bitoff; } } } else #endif { while (bits) { unsigned int byteoff = bitoff / 8; unsigned char b; #ifndef OPENSSL_SMALL_FOOTPRINT if (bitrem == inpgap) { c->data[byteoff++] |= inp[0] & (0xff >> inpgap); inpgap = 8 - inpgap; bitoff += inpgap; bitrem = 0; bits -= inpgap; inpgap = 0; inp++; if (bitoff == WHIRLPOOL_BBLOCK) { whirlpool_block(c, c->data, 1); bitoff = 0; } c->bitoff = bitoff; goto reconsider; } else #endif if (bits > 8) { b = ((inp[0] << inpgap) | (inp[1] >> (8 - inpgap))); b &= 0xff; if (bitrem) c->data[byteoff++] |= b >> bitrem; else c->data[byteoff++] = b; bitoff += 8; bits -= 8; inp++; if (bitoff >= WHIRLPOOL_BBLOCK) { whirlpool_block(c, c->data, 1); byteoff = 0; bitoff %= WHIRLPOOL_BBLOCK; } if (bitrem) c->data[byteoff] = b << (8 - bitrem); } else { b = (inp[0] << inpgap) & 0xff; if (bitrem) c->data[byteoff++] |= b >> bitrem; else c->data[byteoff++] = b; bitoff += (unsigned int)bits; if (bitoff == WHIRLPOOL_BBLOCK) { whirlpool_block(c, c->data, 1); byteoff = 0; bitoff %= WHIRLPOOL_BBLOCK; } if (bitrem) c->data[byteoff] = b << (8 - bitrem); bits = 0; } c->bitoff = bitoff; } } } int WHIRLPOOL_Final(unsigned char *md, WHIRLPOOL_CTX *c) { unsigned int bitoff = c->bitoff, byteoff = bitoff / 8; size_t i, j, v; unsigned char *p; bitoff %= 8; if (bitoff) c->data[byteoff] |= 0x80 >> bitoff; else c->data[byteoff] = 0x80; byteoff++; if (byteoff > (WHIRLPOOL_BBLOCK / 8 - WHIRLPOOL_COUNTER)) { if (byteoff < WHIRLPOOL_BBLOCK / 8) memset(&c->data[byteoff], 0, WHIRLPOOL_BBLOCK / 8 - byteoff); whirlpool_block(c, c->data, 1); byteoff = 0; } if (byteoff < (WHIRLPOOL_BBLOCK / 8 - WHIRLPOOL_COUNTER)) memset(&c->data[byteoff], 0, (WHIRLPOOL_BBLOCK / 8 - WHIRLPOOL_COUNTER) - byteoff); p = &c->data[WHIRLPOOL_BBLOCK / 8 - 1]; for (i = 0; i < WHIRLPOOL_COUNTER / sizeof(size_t); i++) for (v = c->bitlen[i], j = 0; j < sizeof(size_t); j++, v >>= 8) *p-- = (unsigned char)(v & 0xff); whirlpool_block(c, c->data, 1); if (md) { memcpy(md, c->H.c, WHIRLPOOL_DIGEST_LENGTH); OPENSSL_cleanse(c, sizeof(*c)); return 1; } return 0; } unsigned char *WHIRLPOOL(const void *inp, size_t bytes, unsigned char *md) { WHIRLPOOL_CTX ctx; static unsigned char m[WHIRLPOOL_DIGEST_LENGTH]; if (md == NULL) md = m; WHIRLPOOL_Init(&ctx); WHIRLPOOL_Update(&ctx, inp, bytes); WHIRLPOOL_Final(md, &ctx); return md; }
whrlpool
openssl/crypto/whrlpool/wp_dgst.c
openssl
#include "internal/deprecated.h" #include "internal/cryptlib.h" #include "wp_local.h" #include <string.h> typedef unsigned char u8; #if (defined(_WIN32) || defined(_WIN64)) && !defined(__MINGW32) typedef unsigned __int64 u64; #elif defined(__arch64__) typedef unsigned long u64; #else typedef unsigned long long u64; #endif #define ROUNDS 10 #define STRICT_ALIGNMENT #if !defined(PEDANTIC) && (defined(__i386) || defined(__i386__) || \ defined(__x86_64) || defined(__x86_64__) || \ defined(_M_IX86) || defined(_M_AMD64) || \ defined(_M_X64)) # undef STRICT_ALIGNMENT #endif #ifndef STRICT_ALIGNMENT # ifdef __GNUC__ typedef u64 u64_a1 __attribute((__aligned__(1))); # else typedef u64 u64_a1; # endif #endif #if defined(__GNUC__) && !defined(STRICT_ALIGNMENT) typedef u64 u64_aX __attribute((__aligned__(1))); #else typedef u64 u64_aX; #endif #undef SMALL_REGISTER_BANK #if defined(__i386) || defined(__i386__) || defined(_M_IX86) # define SMALL_REGISTER_BANK # if defined(WHIRLPOOL_ASM) # ifndef OPENSSL_SMALL_FOOTPRINT # define OPENSSL_SMALL_FOOTPRINT # endif # define GO_FOR_MMX(ctx,inp,num) do { \ void whirlpool_block_mmx(void *,const void *,size_t); \ if (!(OPENSSL_ia32cap_P[0] & (1<<23))) break; \ whirlpool_block_mmx(ctx->H.c,inp,num); return; \ } while (0) # endif #endif #undef ROTATE #ifndef PEDANTIC # if defined(_MSC_VER) # if defined(_WIN64) # include <stdlib.h> # pragma intrinsic(_rotl64) # define ROTATE(a,n) _rotl64((a),n) # endif # elif defined(__GNUC__) && __GNUC__>=2 # if defined(__x86_64) || defined(__x86_64__) # if defined(L_ENDIAN) # define ROTATE(a,n) ({ u64 ret; asm ("rolq %1,%0" \ : "=r"(ret) : "J"(n),"0"(a) : "cc"); ret; }) # elif defined(B_ENDIAN) # define ROTATE(a,n) ({ u64 ret; asm ("rorq %1,%0" \ : "=r"(ret) : "J"(n),"0"(a) : "cc"); ret; }) # endif # elif defined(__ia64) || defined(__ia64__) # if defined(L_ENDIAN) # define ROTATE(a,n) ({ u64 ret; asm ("shrp %0=%1,%1,%2" \ : "=r"(ret) : "r"(a),"M"(64-(n))); ret; }) # elif defined(B_ENDIAN) # define ROTATE(a,n) ({ u64 ret; asm ("shrp %0=%1,%1,%2" \ : "=r"(ret) : "r"(a),"M"(n)); ret; }) # endif # endif # endif #endif #if defined(OPENSSL_SMALL_FOOTPRINT) # if !defined(ROTATE) # if defined(L_ENDIAN) # define ROTATE(i,n) ((i)<<(n) ^ (i)>>(64-n)) # elif defined(B_ENDIAN) # define ROTATE(i,n) ((i)>>(n) ^ (i)<<(64-n)) # endif # endif # if defined(ROTATE) && !defined(STRICT_ALIGNMENT) # define STRICT_ALIGNMENT # endif #endif #ifdef STRICT_ALIGNMENT # if defined(ROTATE) # define N 1 # define LL(c0,c1,c2,c3,c4,c5,c6,c7) c0,c1,c2,c3,c4,c5,c6,c7 # define C0(K,i) (Cx.q[K.c[(i)*8+0]]) # define C1(K,i) ROTATE(Cx.q[K.c[(i)*8+1]],8) # define C2(K,i) ROTATE(Cx.q[K.c[(i)*8+2]],16) # define C3(K,i) ROTATE(Cx.q[K.c[(i)*8+3]],24) # define C4(K,i) ROTATE(Cx.q[K.c[(i)*8+4]],32) # define C5(K,i) ROTATE(Cx.q[K.c[(i)*8+5]],40) # define C6(K,i) ROTATE(Cx.q[K.c[(i)*8+6]],48) # define C7(K,i) ROTATE(Cx.q[K.c[(i)*8+7]],56) # else # define N 8 # define LL(c0,c1,c2,c3,c4,c5,c6,c7) c0,c1,c2,c3,c4,c5,c6,c7, \ c7,c0,c1,c2,c3,c4,c5,c6, \ c6,c7,c0,c1,c2,c3,c4,c5, \ c5,c6,c7,c0,c1,c2,c3,c4, \ c4,c5,c6,c7,c0,c1,c2,c3, \ c3,c4,c5,c6,c7,c0,c1,c2, \ c2,c3,c4,c5,c6,c7,c0,c1, \ c1,c2,c3,c4,c5,c6,c7,c0 # define C0(K,i) (Cx.q[0+8*K.c[(i)*8+0]]) # define C1(K,i) (Cx.q[1+8*K.c[(i)*8+1]]) # define C2(K,i) (Cx.q[2+8*K.c[(i)*8+2]]) # define C3(K,i) (Cx.q[3+8*K.c[(i)*8+3]]) # define C4(K,i) (Cx.q[4+8*K.c[(i)*8+4]]) # define C5(K,i) (Cx.q[5+8*K.c[(i)*8+5]]) # define C6(K,i) (Cx.q[6+8*K.c[(i)*8+6]]) # define C7(K,i) (Cx.q[7+8*K.c[(i)*8+7]]) # endif #else # define N 2 # define LL(c0,c1,c2,c3,c4,c5,c6,c7) c0,c1,c2,c3,c4,c5,c6,c7, \ c0,c1,c2,c3,c4,c5,c6,c7 # define C0(K,i) (((u64*)(Cx.c+0))[2*K.c[(i)*8+0]]) # define C1(K,i) (((u64_a1*)(Cx.c+7))[2*K.c[(i)*8+1]]) # define C2(K,i) (((u64_a1*)(Cx.c+6))[2*K.c[(i)*8+2]]) # define C3(K,i) (((u64_a1*)(Cx.c+5))[2*K.c[(i)*8+3]]) # define C4(K,i) (((u64_a1*)(Cx.c+4))[2*K.c[(i)*8+4]]) # define C5(K,i) (((u64_a1*)(Cx.c+3))[2*K.c[(i)*8+5]]) # define C6(K,i) (((u64_a1*)(Cx.c+2))[2*K.c[(i)*8+6]]) # define C7(K,i) (((u64_a1*)(Cx.c+1))[2*K.c[(i)*8+7]]) #endif static const union { u8 c[(256 * N + ROUNDS) * sizeof(u64)]; u64 q[(256 * N + ROUNDS)]; } Cx = { { LL(0x18, 0x18, 0x60, 0x18, 0xc0, 0x78, 0x30, 0xd8), LL(0x23, 0x23, 0x8c, 0x23, 0x05, 0xaf, 0x46, 0x26), LL(0xc6, 0xc6, 0x3f, 0xc6, 0x7e, 0xf9, 0x91, 0xb8), LL(0xe8, 0xe8, 0x87, 0xe8, 0x13, 0x6f, 0xcd, 0xfb), LL(0x87, 0x87, 0x26, 0x87, 0x4c, 0xa1, 0x13, 0xcb), LL(0xb8, 0xb8, 0xda, 0xb8, 0xa9, 0x62, 0x6d, 0x11), LL(0x01, 0x01, 0x04, 0x01, 0x08, 0x05, 0x02, 0x09), LL(0x4f, 0x4f, 0x21, 0x4f, 0x42, 0x6e, 0x9e, 0x0d), LL(0x36, 0x36, 0xd8, 0x36, 0xad, 0xee, 0x6c, 0x9b), LL(0xa6, 0xa6, 0xa2, 0xa6, 0x59, 0x04, 0x51, 0xff), LL(0xd2, 0xd2, 0x6f, 0xd2, 0xde, 0xbd, 0xb9, 0x0c), LL(0xf5, 0xf5, 0xf3, 0xf5, 0xfb, 0x06, 0xf7, 0x0e), LL(0x79, 0x79, 0xf9, 0x79, 0xef, 0x80, 0xf2, 0x96), LL(0x6f, 0x6f, 0xa1, 0x6f, 0x5f, 0xce, 0xde, 0x30), LL(0x91, 0x91, 0x7e, 0x91, 0xfc, 0xef, 0x3f, 0x6d), LL(0x52, 0x52, 0x55, 0x52, 0xaa, 0x07, 0xa4, 0xf8), LL(0x60, 0x60, 0x9d, 0x60, 0x27, 0xfd, 0xc0, 0x47), LL(0xbc, 0xbc, 0xca, 0xbc, 0x89, 0x76, 0x65, 0x35), LL(0x9b, 0x9b, 0x56, 0x9b, 0xac, 0xcd, 0x2b, 0x37), LL(0x8e, 0x8e, 0x02, 0x8e, 0x04, 0x8c, 0x01, 0x8a), LL(0xa3, 0xa3, 0xb6, 0xa3, 0x71, 0x15, 0x5b, 0xd2), LL(0x0c, 0x0c, 0x30, 0x0c, 0x60, 0x3c, 0x18, 0x6c), LL(0x7b, 0x7b, 0xf1, 0x7b, 0xff, 0x8a, 0xf6, 0x84), LL(0x35, 0x35, 0xd4, 0x35, 0xb5, 0xe1, 0x6a, 0x80), LL(0x1d, 0x1d, 0x74, 0x1d, 0xe8, 0x69, 0x3a, 0xf5), LL(0xe0, 0xe0, 0xa7, 0xe0, 0x53, 0x47, 0xdd, 0xb3), LL(0xd7, 0xd7, 0x7b, 0xd7, 0xf6, 0xac, 0xb3, 0x21), LL(0xc2, 0xc2, 0x2f, 0xc2, 0x5e, 0xed, 0x99, 0x9c), LL(0x2e, 0x2e, 0xb8, 0x2e, 0x6d, 0x96, 0x5c, 0x43), LL(0x4b, 0x4b, 0x31, 0x4b, 0x62, 0x7a, 0x96, 0x29), LL(0xfe, 0xfe, 0xdf, 0xfe, 0xa3, 0x21, 0xe1, 0x5d), LL(0x57, 0x57, 0x41, 0x57, 0x82, 0x16, 0xae, 0xd5), LL(0x15, 0x15, 0x54, 0x15, 0xa8, 0x41, 0x2a, 0xbd), LL(0x77, 0x77, 0xc1, 0x77, 0x9f, 0xb6, 0xee, 0xe8), LL(0x37, 0x37, 0xdc, 0x37, 0xa5, 0xeb, 0x6e, 0x92), LL(0xe5, 0xe5, 0xb3, 0xe5, 0x7b, 0x56, 0xd7, 0x9e), LL(0x9f, 0x9f, 0x46, 0x9f, 0x8c, 0xd9, 0x23, 0x13), LL(0xf0, 0xf0, 0xe7, 0xf0, 0xd3, 0x17, 0xfd, 0x23), LL(0x4a, 0x4a, 0x35, 0x4a, 0x6a, 0x7f, 0x94, 0x20), LL(0xda, 0xda, 0x4f, 0xda, 0x9e, 0x95, 0xa9, 0x44), LL(0x58, 0x58, 0x7d, 0x58, 0xfa, 0x25, 0xb0, 0xa2), LL(0xc9, 0xc9, 0x03, 0xc9, 0x06, 0xca, 0x8f, 0xcf), LL(0x29, 0x29, 0xa4, 0x29, 0x55, 0x8d, 0x52, 0x7c), LL(0x0a, 0x0a, 0x28, 0x0a, 0x50, 0x22, 0x14, 0x5a), LL(0xb1, 0xb1, 0xfe, 0xb1, 0xe1, 0x4f, 0x7f, 0x50), LL(0xa0, 0xa0, 0xba, 0xa0, 0x69, 0x1a, 0x5d, 0xc9), LL(0x6b, 0x6b, 0xb1, 0x6b, 0x7f, 0xda, 0xd6, 0x14), LL(0x85, 0x85, 0x2e, 0x85, 0x5c, 0xab, 0x17, 0xd9), LL(0xbd, 0xbd, 0xce, 0xbd, 0x81, 0x73, 0x67, 0x3c), LL(0x5d, 0x5d, 0x69, 0x5d, 0xd2, 0x34, 0xba, 0x8f), LL(0x10, 0x10, 0x40, 0x10, 0x80, 0x50, 0x20, 0x90), LL(0xf4, 0xf4, 0xf7, 0xf4, 0xf3, 0x03, 0xf5, 0x07), LL(0xcb, 0xcb, 0x0b, 0xcb, 0x16, 0xc0, 0x8b, 0xdd), LL(0x3e, 0x3e, 0xf8, 0x3e, 0xed, 0xc6, 0x7c, 0xd3), LL(0x05, 0x05, 0x14, 0x05, 0x28, 0x11, 0x0a, 0x2d), LL(0x67, 0x67, 0x81, 0x67, 0x1f, 0xe6, 0xce, 0x78), LL(0xe4, 0xe4, 0xb7, 0xe4, 0x73, 0x53, 0xd5, 0x97), LL(0x27, 0x27, 0x9c, 0x27, 0x25, 0xbb, 0x4e, 0x02), LL(0x41, 0x41, 0x19, 0x41, 0x32, 0x58, 0x82, 0x73), LL(0x8b, 0x8b, 0x16, 0x8b, 0x2c, 0x9d, 0x0b, 0xa7), LL(0xa7, 0xa7, 0xa6, 0xa7, 0x51, 0x01, 0x53, 0xf6), LL(0x7d, 0x7d, 0xe9, 0x7d, 0xcf, 0x94, 0xfa, 0xb2), LL(0x95, 0x95, 0x6e, 0x95, 0xdc, 0xfb, 0x37, 0x49), LL(0xd8, 0xd8, 0x47, 0xd8, 0x8e, 0x9f, 0xad, 0x56), LL(0xfb, 0xfb, 0xcb, 0xfb, 0x8b, 0x30, 0xeb, 0x70), LL(0xee, 0xee, 0x9f, 0xee, 0x23, 0x71, 0xc1, 0xcd), LL(0x7c, 0x7c, 0xed, 0x7c, 0xc7, 0x91, 0xf8, 0xbb), LL(0x66, 0x66, 0x85, 0x66, 0x17, 0xe3, 0xcc, 0x71), LL(0xdd, 0xdd, 0x53, 0xdd, 0xa6, 0x8e, 0xa7, 0x7b), LL(0x17, 0x17, 0x5c, 0x17, 0xb8, 0x4b, 0x2e, 0xaf), LL(0x47, 0x47, 0x01, 0x47, 0x02, 0x46, 0x8e, 0x45), LL(0x9e, 0x9e, 0x42, 0x9e, 0x84, 0xdc, 0x21, 0x1a), LL(0xca, 0xca, 0x0f, 0xca, 0x1e, 0xc5, 0x89, 0xd4), LL(0x2d, 0x2d, 0xb4, 0x2d, 0x75, 0x99, 0x5a, 0x58), LL(0xbf, 0xbf, 0xc6, 0xbf, 0x91, 0x79, 0x63, 0x2e), LL(0x07, 0x07, 0x1c, 0x07, 0x38, 0x1b, 0x0e, 0x3f), LL(0xad, 0xad, 0x8e, 0xad, 0x01, 0x23, 0x47, 0xac), LL(0x5a, 0x5a, 0x75, 0x5a, 0xea, 0x2f, 0xb4, 0xb0), LL(0x83, 0x83, 0x36, 0x83, 0x6c, 0xb5, 0x1b, 0xef), LL(0x33, 0x33, 0xcc, 0x33, 0x85, 0xff, 0x66, 0xb6), LL(0x63, 0x63, 0x91, 0x63, 0x3f, 0xf2, 0xc6, 0x5c), LL(0x02, 0x02, 0x08, 0x02, 0x10, 0x0a, 0x04, 0x12), LL(0xaa, 0xaa, 0x92, 0xaa, 0x39, 0x38, 0x49, 0x93), LL(0x71, 0x71, 0xd9, 0x71, 0xaf, 0xa8, 0xe2, 0xde), LL(0xc8, 0xc8, 0x07, 0xc8, 0x0e, 0xcf, 0x8d, 0xc6), LL(0x19, 0x19, 0x64, 0x19, 0xc8, 0x7d, 0x32, 0xd1), LL(0x49, 0x49, 0x39, 0x49, 0x72, 0x70, 0x92, 0x3b), LL(0xd9, 0xd9, 0x43, 0xd9, 0x86, 0x9a, 0xaf, 0x5f), LL(0xf2, 0xf2, 0xef, 0xf2, 0xc3, 0x1d, 0xf9, 0x31), LL(0xe3, 0xe3, 0xab, 0xe3, 0x4b, 0x48, 0xdb, 0xa8), LL(0x5b, 0x5b, 0x71, 0x5b, 0xe2, 0x2a, 0xb6, 0xb9), LL(0x88, 0x88, 0x1a, 0x88, 0x34, 0x92, 0x0d, 0xbc), LL(0x9a, 0x9a, 0x52, 0x9a, 0xa4, 0xc8, 0x29, 0x3e), LL(0x26, 0x26, 0x98, 0x26, 0x2d, 0xbe, 0x4c, 0x0b), LL(0x32, 0x32, 0xc8, 0x32, 0x8d, 0xfa, 0x64, 0xbf), LL(0xb0, 0xb0, 0xfa, 0xb0, 0xe9, 0x4a, 0x7d, 0x59), LL(0xe9, 0xe9, 0x83, 0xe9, 0x1b, 0x6a, 0xcf, 0xf2), LL(0x0f, 0x0f, 0x3c, 0x0f, 0x78, 0x33, 0x1e, 0x77), LL(0xd5, 0xd5, 0x73, 0xd5, 0xe6, 0xa6, 0xb7, 0x33), LL(0x80, 0x80, 0x3a, 0x80, 0x74, 0xba, 0x1d, 0xf4), LL(0xbe, 0xbe, 0xc2, 0xbe, 0x99, 0x7c, 0x61, 0x27), LL(0xcd, 0xcd, 0x13, 0xcd, 0x26, 0xde, 0x87, 0xeb), LL(0x34, 0x34, 0xd0, 0x34, 0xbd, 0xe4, 0x68, 0x89), LL(0x48, 0x48, 0x3d, 0x48, 0x7a, 0x75, 0x90, 0x32), LL(0xff, 0xff, 0xdb, 0xff, 0xab, 0x24, 0xe3, 0x54), LL(0x7a, 0x7a, 0xf5, 0x7a, 0xf7, 0x8f, 0xf4, 0x8d), LL(0x90, 0x90, 0x7a, 0x90, 0xf4, 0xea, 0x3d, 0x64), LL(0x5f, 0x5f, 0x61, 0x5f, 0xc2, 0x3e, 0xbe, 0x9d), LL(0x20, 0x20, 0x80, 0x20, 0x1d, 0xa0, 0x40, 0x3d), LL(0x68, 0x68, 0xbd, 0x68, 0x67, 0xd5, 0xd0, 0x0f), LL(0x1a, 0x1a, 0x68, 0x1a, 0xd0, 0x72, 0x34, 0xca), LL(0xae, 0xae, 0x82, 0xae, 0x19, 0x2c, 0x41, 0xb7), LL(0xb4, 0xb4, 0xea, 0xb4, 0xc9, 0x5e, 0x75, 0x7d), LL(0x54, 0x54, 0x4d, 0x54, 0x9a, 0x19, 0xa8, 0xce), LL(0x93, 0x93, 0x76, 0x93, 0xec, 0xe5, 0x3b, 0x7f), LL(0x22, 0x22, 0x88, 0x22, 0x0d, 0xaa, 0x44, 0x2f), LL(0x64, 0x64, 0x8d, 0x64, 0x07, 0xe9, 0xc8, 0x63), LL(0xf1, 0xf1, 0xe3, 0xf1, 0xdb, 0x12, 0xff, 0x2a), LL(0x73, 0x73, 0xd1, 0x73, 0xbf, 0xa2, 0xe6, 0xcc), LL(0x12, 0x12, 0x48, 0x12, 0x90, 0x5a, 0x24, 0x82), LL(0x40, 0x40, 0x1d, 0x40, 0x3a, 0x5d, 0x80, 0x7a), LL(0x08, 0x08, 0x20, 0x08, 0x40, 0x28, 0x10, 0x48), LL(0xc3, 0xc3, 0x2b, 0xc3, 0x56, 0xe8, 0x9b, 0x95), LL(0xec, 0xec, 0x97, 0xec, 0x33, 0x7b, 0xc5, 0xdf), LL(0xdb, 0xdb, 0x4b, 0xdb, 0x96, 0x90, 0xab, 0x4d), LL(0xa1, 0xa1, 0xbe, 0xa1, 0x61, 0x1f, 0x5f, 0xc0), LL(0x8d, 0x8d, 0x0e, 0x8d, 0x1c, 0x83, 0x07, 0x91), LL(0x3d, 0x3d, 0xf4, 0x3d, 0xf5, 0xc9, 0x7a, 0xc8), LL(0x97, 0x97, 0x66, 0x97, 0xcc, 0xf1, 0x33, 0x5b), LL(0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00), LL(0xcf, 0xcf, 0x1b, 0xcf, 0x36, 0xd4, 0x83, 0xf9), LL(0x2b, 0x2b, 0xac, 0x2b, 0x45, 0x87, 0x56, 0x6e), LL(0x76, 0x76, 0xc5, 0x76, 0x97, 0xb3, 0xec, 0xe1), LL(0x82, 0x82, 0x32, 0x82, 0x64, 0xb0, 0x19, 0xe6), LL(0xd6, 0xd6, 0x7f, 0xd6, 0xfe, 0xa9, 0xb1, 0x28), LL(0x1b, 0x1b, 0x6c, 0x1b, 0xd8, 0x77, 0x36, 0xc3), LL(0xb5, 0xb5, 0xee, 0xb5, 0xc1, 0x5b, 0x77, 0x74), LL(0xaf, 0xaf, 0x86, 0xaf, 0x11, 0x29, 0x43, 0xbe), LL(0x6a, 0x6a, 0xb5, 0x6a, 0x77, 0xdf, 0xd4, 0x1d), LL(0x50, 0x50, 0x5d, 0x50, 0xba, 0x0d, 0xa0, 0xea), LL(0x45, 0x45, 0x09, 0x45, 0x12, 0x4c, 0x8a, 0x57), LL(0xf3, 0xf3, 0xeb, 0xf3, 0xcb, 0x18, 0xfb, 0x38), LL(0x30, 0x30, 0xc0, 0x30, 0x9d, 0xf0, 0x60, 0xad), LL(0xef, 0xef, 0x9b, 0xef, 0x2b, 0x74, 0xc3, 0xc4), LL(0x3f, 0x3f, 0xfc, 0x3f, 0xe5, 0xc3, 0x7e, 0xda), LL(0x55, 0x55, 0x49, 0x55, 0x92, 0x1c, 0xaa, 0xc7), LL(0xa2, 0xa2, 0xb2, 0xa2, 0x79, 0x10, 0x59, 0xdb), LL(0xea, 0xea, 0x8f, 0xea, 0x03, 0x65, 0xc9, 0xe9), LL(0x65, 0x65, 0x89, 0x65, 0x0f, 0xec, 0xca, 0x6a), LL(0xba, 0xba, 0xd2, 0xba, 0xb9, 0x68, 0x69, 0x03), LL(0x2f, 0x2f, 0xbc, 0x2f, 0x65, 0x93, 0x5e, 0x4a), LL(0xc0, 0xc0, 0x27, 0xc0, 0x4e, 0xe7, 0x9d, 0x8e), LL(0xde, 0xde, 0x5f, 0xde, 0xbe, 0x81, 0xa1, 0x60), LL(0x1c, 0x1c, 0x70, 0x1c, 0xe0, 0x6c, 0x38, 0xfc), LL(0xfd, 0xfd, 0xd3, 0xfd, 0xbb, 0x2e, 0xe7, 0x46), LL(0x4d, 0x4d, 0x29, 0x4d, 0x52, 0x64, 0x9a, 0x1f), LL(0x92, 0x92, 0x72, 0x92, 0xe4, 0xe0, 0x39, 0x76), LL(0x75, 0x75, 0xc9, 0x75, 0x8f, 0xbc, 0xea, 0xfa), LL(0x06, 0x06, 0x18, 0x06, 0x30, 0x1e, 0x0c, 0x36), LL(0x8a, 0x8a, 0x12, 0x8a, 0x24, 0x98, 0x09, 0xae), LL(0xb2, 0xb2, 0xf2, 0xb2, 0xf9, 0x40, 0x79, 0x4b), LL(0xe6, 0xe6, 0xbf, 0xe6, 0x63, 0x59, 0xd1, 0x85), LL(0x0e, 0x0e, 0x38, 0x0e, 0x70, 0x36, 0x1c, 0x7e), LL(0x1f, 0x1f, 0x7c, 0x1f, 0xf8, 0x63, 0x3e, 0xe7), LL(0x62, 0x62, 0x95, 0x62, 0x37, 0xf7, 0xc4, 0x55), LL(0xd4, 0xd4, 0x77, 0xd4, 0xee, 0xa3, 0xb5, 0x3a), LL(0xa8, 0xa8, 0x9a, 0xa8, 0x29, 0x32, 0x4d, 0x81), LL(0x96, 0x96, 0x62, 0x96, 0xc4, 0xf4, 0x31, 0x52), LL(0xf9, 0xf9, 0xc3, 0xf9, 0x9b, 0x3a, 0xef, 0x62), LL(0xc5, 0xc5, 0x33, 0xc5, 0x66, 0xf6, 0x97, 0xa3), LL(0x25, 0x25, 0x94, 0x25, 0x35, 0xb1, 0x4a, 0x10), LL(0x59, 0x59, 0x79, 0x59, 0xf2, 0x20, 0xb2, 0xab), LL(0x84, 0x84, 0x2a, 0x84, 0x54, 0xae, 0x15, 0xd0), LL(0x72, 0x72, 0xd5, 0x72, 0xb7, 0xa7, 0xe4, 0xc5), LL(0x39, 0x39, 0xe4, 0x39, 0xd5, 0xdd, 0x72, 0xec), LL(0x4c, 0x4c, 0x2d, 0x4c, 0x5a, 0x61, 0x98, 0x16), LL(0x5e, 0x5e, 0x65, 0x5e, 0xca, 0x3b, 0xbc, 0x94), LL(0x78, 0x78, 0xfd, 0x78, 0xe7, 0x85, 0xf0, 0x9f), LL(0x38, 0x38, 0xe0, 0x38, 0xdd, 0xd8, 0x70, 0xe5), LL(0x8c, 0x8c, 0x0a, 0x8c, 0x14, 0x86, 0x05, 0x98), LL(0xd1, 0xd1, 0x63, 0xd1, 0xc6, 0xb2, 0xbf, 0x17), LL(0xa5, 0xa5, 0xae, 0xa5, 0x41, 0x0b, 0x57, 0xe4), LL(0xe2, 0xe2, 0xaf, 0xe2, 0x43, 0x4d, 0xd9, 0xa1), LL(0x61, 0x61, 0x99, 0x61, 0x2f, 0xf8, 0xc2, 0x4e), LL(0xb3, 0xb3, 0xf6, 0xb3, 0xf1, 0x45, 0x7b, 0x42), LL(0x21, 0x21, 0x84, 0x21, 0x15, 0xa5, 0x42, 0x34), LL(0x9c, 0x9c, 0x4a, 0x9c, 0x94, 0xd6, 0x25, 0x08), LL(0x1e, 0x1e, 0x78, 0x1e, 0xf0, 0x66, 0x3c, 0xee), LL(0x43, 0x43, 0x11, 0x43, 0x22, 0x52, 0x86, 0x61), LL(0xc7, 0xc7, 0x3b, 0xc7, 0x76, 0xfc, 0x93, 0xb1), LL(0xfc, 0xfc, 0xd7, 0xfc, 0xb3, 0x2b, 0xe5, 0x4f), LL(0x04, 0x04, 0x10, 0x04, 0x20, 0x14, 0x08, 0x24), LL(0x51, 0x51, 0x59, 0x51, 0xb2, 0x08, 0xa2, 0xe3), LL(0x99, 0x99, 0x5e, 0x99, 0xbc, 0xc7, 0x2f, 0x25), LL(0x6d, 0x6d, 0xa9, 0x6d, 0x4f, 0xc4, 0xda, 0x22), LL(0x0d, 0x0d, 0x34, 0x0d, 0x68, 0x39, 0x1a, 0x65), LL(0xfa, 0xfa, 0xcf, 0xfa, 0x83, 0x35, 0xe9, 0x79), LL(0xdf, 0xdf, 0x5b, 0xdf, 0xb6, 0x84, 0xa3, 0x69), LL(0x7e, 0x7e, 0xe5, 0x7e, 0xd7, 0x9b, 0xfc, 0xa9), LL(0x24, 0x24, 0x90, 0x24, 0x3d, 0xb4, 0x48, 0x19), LL(0x3b, 0x3b, 0xec, 0x3b, 0xc5, 0xd7, 0x76, 0xfe), LL(0xab, 0xab, 0x96, 0xab, 0x31, 0x3d, 0x4b, 0x9a), LL(0xce, 0xce, 0x1f, 0xce, 0x3e, 0xd1, 0x81, 0xf0), LL(0x11, 0x11, 0x44, 0x11, 0x88, 0x55, 0x22, 0x99), LL(0x8f, 0x8f, 0x06, 0x8f, 0x0c, 0x89, 0x03, 0x83), LL(0x4e, 0x4e, 0x25, 0x4e, 0x4a, 0x6b, 0x9c, 0x04), LL(0xb7, 0xb7, 0xe6, 0xb7, 0xd1, 0x51, 0x73, 0x66), LL(0xeb, 0xeb, 0x8b, 0xeb, 0x0b, 0x60, 0xcb, 0xe0), LL(0x3c, 0x3c, 0xf0, 0x3c, 0xfd, 0xcc, 0x78, 0xc1), LL(0x81, 0x81, 0x3e, 0x81, 0x7c, 0xbf, 0x1f, 0xfd), LL(0x94, 0x94, 0x6a, 0x94, 0xd4, 0xfe, 0x35, 0x40), LL(0xf7, 0xf7, 0xfb, 0xf7, 0xeb, 0x0c, 0xf3, 0x1c), LL(0xb9, 0xb9, 0xde, 0xb9, 0xa1, 0x67, 0x6f, 0x18), LL(0x13, 0x13, 0x4c, 0x13, 0x98, 0x5f, 0x26, 0x8b), LL(0x2c, 0x2c, 0xb0, 0x2c, 0x7d, 0x9c, 0x58, 0x51), LL(0xd3, 0xd3, 0x6b, 0xd3, 0xd6, 0xb8, 0xbb, 0x05), LL(0xe7, 0xe7, 0xbb, 0xe7, 0x6b, 0x5c, 0xd3, 0x8c), LL(0x6e, 0x6e, 0xa5, 0x6e, 0x57, 0xcb, 0xdc, 0x39), LL(0xc4, 0xc4, 0x37, 0xc4, 0x6e, 0xf3, 0x95, 0xaa), LL(0x03, 0x03, 0x0c, 0x03, 0x18, 0x0f, 0x06, 0x1b), LL(0x56, 0x56, 0x45, 0x56, 0x8a, 0x13, 0xac, 0xdc), LL(0x44, 0x44, 0x0d, 0x44, 0x1a, 0x49, 0x88, 0x5e), LL(0x7f, 0x7f, 0xe1, 0x7f, 0xdf, 0x9e, 0xfe, 0xa0), LL(0xa9, 0xa9, 0x9e, 0xa9, 0x21, 0x37, 0x4f, 0x88), LL(0x2a, 0x2a, 0xa8, 0x2a, 0x4d, 0x82, 0x54, 0x67), LL(0xbb, 0xbb, 0xd6, 0xbb, 0xb1, 0x6d, 0x6b, 0x0a), LL(0xc1, 0xc1, 0x23, 0xc1, 0x46, 0xe2, 0x9f, 0x87), LL(0x53, 0x53, 0x51, 0x53, 0xa2, 0x02, 0xa6, 0xf1), LL(0xdc, 0xdc, 0x57, 0xdc, 0xae, 0x8b, 0xa5, 0x72), LL(0x0b, 0x0b, 0x2c, 0x0b, 0x58, 0x27, 0x16, 0x53), LL(0x9d, 0x9d, 0x4e, 0x9d, 0x9c, 0xd3, 0x27, 0x01), LL(0x6c, 0x6c, 0xad, 0x6c, 0x47, 0xc1, 0xd8, 0x2b), LL(0x31, 0x31, 0xc4, 0x31, 0x95, 0xf5, 0x62, 0xa4), LL(0x74, 0x74, 0xcd, 0x74, 0x87, 0xb9, 0xe8, 0xf3), LL(0xf6, 0xf6, 0xff, 0xf6, 0xe3, 0x09, 0xf1, 0x15), LL(0x46, 0x46, 0x05, 0x46, 0x0a, 0x43, 0x8c, 0x4c), LL(0xac, 0xac, 0x8a, 0xac, 0x09, 0x26, 0x45, 0xa5), LL(0x89, 0x89, 0x1e, 0x89, 0x3c, 0x97, 0x0f, 0xb5), LL(0x14, 0x14, 0x50, 0x14, 0xa0, 0x44, 0x28, 0xb4), LL(0xe1, 0xe1, 0xa3, 0xe1, 0x5b, 0x42, 0xdf, 0xba), LL(0x16, 0x16, 0x58, 0x16, 0xb0, 0x4e, 0x2c, 0xa6), LL(0x3a, 0x3a, 0xe8, 0x3a, 0xcd, 0xd2, 0x74, 0xf7), LL(0x69, 0x69, 0xb9, 0x69, 0x6f, 0xd0, 0xd2, 0x06), LL(0x09, 0x09, 0x24, 0x09, 0x48, 0x2d, 0x12, 0x41), LL(0x70, 0x70, 0xdd, 0x70, 0xa7, 0xad, 0xe0, 0xd7), LL(0xb6, 0xb6, 0xe2, 0xb6, 0xd9, 0x54, 0x71, 0x6f), LL(0xd0, 0xd0, 0x67, 0xd0, 0xce, 0xb7, 0xbd, 0x1e), LL(0xed, 0xed, 0x93, 0xed, 0x3b, 0x7e, 0xc7, 0xd6), LL(0xcc, 0xcc, 0x17, 0xcc, 0x2e, 0xdb, 0x85, 0xe2), LL(0x42, 0x42, 0x15, 0x42, 0x2a, 0x57, 0x84, 0x68), LL(0x98, 0x98, 0x5a, 0x98, 0xb4, 0xc2, 0x2d, 0x2c), LL(0xa4, 0xa4, 0xaa, 0xa4, 0x49, 0x0e, 0x55, 0xed), LL(0x28, 0x28, 0xa0, 0x28, 0x5d, 0x88, 0x50, 0x75), LL(0x5c, 0x5c, 0x6d, 0x5c, 0xda, 0x31, 0xb8, 0x86), LL(0xf8, 0xf8, 0xc7, 0xf8, 0x93, 0x3f, 0xed, 0x6b), LL(0x86, 0x86, 0x22, 0x86, 0x44, 0xa4, 0x11, 0xc2), #define RC (&(Cx.q[256*N])) 0x18, 0x23, 0xc6, 0xe8, 0x87, 0xb8, 0x01, 0x4f, 0x36, 0xa6, 0xd2, 0xf5, 0x79, 0x6f, 0x91, 0x52, 0x60, 0xbc, 0x9b, 0x8e, 0xa3, 0x0c, 0x7b, 0x35, 0x1d, 0xe0, 0xd7, 0xc2, 0x2e, 0x4b, 0xfe, 0x57, 0x15, 0x77, 0x37, 0xe5, 0x9f, 0xf0, 0x4a, 0xda, 0x58, 0xc9, 0x29, 0x0a, 0xb1, 0xa0, 0x6b, 0x85, 0xbd, 0x5d, 0x10, 0xf4, 0xcb, 0x3e, 0x05, 0x67, 0xe4, 0x27, 0x41, 0x8b, 0xa7, 0x7d, 0x95, 0xd8, 0xfb, 0xee, 0x7c, 0x66, 0xdd, 0x17, 0x47, 0x9e, 0xca, 0x2d, 0xbf, 0x07, 0xad, 0x5a, 0x83, 0x33 } }; void whirlpool_block(WHIRLPOOL_CTX *ctx, const void *inp, size_t n) { int r; const u8 *p = inp; union { u64 q[8]; u8 c[64]; } S, K, *H = (void *)ctx->H.q; #ifdef GO_FOR_MMX GO_FOR_MMX(ctx, inp, n); #endif do { #ifdef OPENSSL_SMALL_FOOTPRINT u64 L[8]; int i; for (i = 0; i < 64; i++) S.c[i] = (K.c[i] = H->c[i]) ^ p[i]; for (r = 0; r < ROUNDS; r++) { for (i = 0; i < 8; i++) { L[i] = i ? 0 : RC[r]; L[i] ^= C0(K, i) ^ C1(K, (i - 1) & 7) ^ C2(K, (i - 2) & 7) ^ C3(K, (i - 3) & 7) ^ C4(K, (i - 4) & 7) ^ C5(K, (i - 5) & 7) ^ C6(K, (i - 6) & 7) ^ C7(K, (i - 7) & 7); } memcpy(K.q, L, 64); for (i = 0; i < 8; i++) { L[i] ^= C0(S, i) ^ C1(S, (i - 1) & 7) ^ C2(S, (i - 2) & 7) ^ C3(S, (i - 3) & 7) ^ C4(S, (i - 4) & 7) ^ C5(S, (i - 5) & 7) ^ C6(S, (i - 6) & 7) ^ C7(S, (i - 7) & 7); } memcpy(S.q, L, 64); } for (i = 0; i < 64; i++) H->c[i] ^= S.c[i] ^ p[i]; #else u64 L0, L1, L2, L3, L4, L5, L6, L7; # ifdef STRICT_ALIGNMENT if ((size_t)p & 7) { memcpy(S.c, p, 64); S.q[0] ^= (K.q[0] = H->q[0]); S.q[1] ^= (K.q[1] = H->q[1]); S.q[2] ^= (K.q[2] = H->q[2]); S.q[3] ^= (K.q[3] = H->q[3]); S.q[4] ^= (K.q[4] = H->q[4]); S.q[5] ^= (K.q[5] = H->q[5]); S.q[6] ^= (K.q[6] = H->q[6]); S.q[7] ^= (K.q[7] = H->q[7]); } else # endif { const u64_aX *pa = (const u64_aX *)p; S.q[0] = (K.q[0] = H->q[0]) ^ pa[0]; S.q[1] = (K.q[1] = H->q[1]) ^ pa[1]; S.q[2] = (K.q[2] = H->q[2]) ^ pa[2]; S.q[3] = (K.q[3] = H->q[3]) ^ pa[3]; S.q[4] = (K.q[4] = H->q[4]) ^ pa[4]; S.q[5] = (K.q[5] = H->q[5]) ^ pa[5]; S.q[6] = (K.q[6] = H->q[6]) ^ pa[6]; S.q[7] = (K.q[7] = H->q[7]) ^ pa[7]; } for (r = 0; r < ROUNDS; r++) { # ifdef SMALL_REGISTER_BANK L0 = C0(K, 0) ^ C1(K, 7) ^ C2(K, 6) ^ C3(K, 5) ^ C4(K, 4) ^ C5(K, 3) ^ C6(K, 2) ^ C7(K, 1) ^ RC[r]; L1 = C0(K, 1) ^ C1(K, 0) ^ C2(K, 7) ^ C3(K, 6) ^ C4(K, 5) ^ C5(K, 4) ^ C6(K, 3) ^ C7(K, 2); L2 = C0(K, 2) ^ C1(K, 1) ^ C2(K, 0) ^ C3(K, 7) ^ C4(K, 6) ^ C5(K, 5) ^ C6(K, 4) ^ C7(K, 3); L3 = C0(K, 3) ^ C1(K, 2) ^ C2(K, 1) ^ C3(K, 0) ^ C4(K, 7) ^ C5(K, 6) ^ C6(K, 5) ^ C7(K, 4); L4 = C0(K, 4) ^ C1(K, 3) ^ C2(K, 2) ^ C3(K, 1) ^ C4(K, 0) ^ C5(K, 7) ^ C6(K, 6) ^ C7(K, 5); L5 = C0(K, 5) ^ C1(K, 4) ^ C2(K, 3) ^ C3(K, 2) ^ C4(K, 1) ^ C5(K, 0) ^ C6(K, 7) ^ C7(K, 6); L6 = C0(K, 6) ^ C1(K, 5) ^ C2(K, 4) ^ C3(K, 3) ^ C4(K, 2) ^ C5(K, 1) ^ C6(K, 0) ^ C7(K, 7); L7 = C0(K, 7) ^ C1(K, 6) ^ C2(K, 5) ^ C3(K, 4) ^ C4(K, 3) ^ C5(K, 2) ^ C6(K, 1) ^ C7(K, 0); K.q[0] = L0; K.q[1] = L1; K.q[2] = L2; K.q[3] = L3; K.q[4] = L4; K.q[5] = L5; K.q[6] = L6; K.q[7] = L7; L0 ^= C0(S, 0) ^ C1(S, 7) ^ C2(S, 6) ^ C3(S, 5) ^ C4(S, 4) ^ C5(S, 3) ^ C6(S, 2) ^ C7(S, 1); L1 ^= C0(S, 1) ^ C1(S, 0) ^ C2(S, 7) ^ C3(S, 6) ^ C4(S, 5) ^ C5(S, 4) ^ C6(S, 3) ^ C7(S, 2); L2 ^= C0(S, 2) ^ C1(S, 1) ^ C2(S, 0) ^ C3(S, 7) ^ C4(S, 6) ^ C5(S, 5) ^ C6(S, 4) ^ C7(S, 3); L3 ^= C0(S, 3) ^ C1(S, 2) ^ C2(S, 1) ^ C3(S, 0) ^ C4(S, 7) ^ C5(S, 6) ^ C6(S, 5) ^ C7(S, 4); L4 ^= C0(S, 4) ^ C1(S, 3) ^ C2(S, 2) ^ C3(S, 1) ^ C4(S, 0) ^ C5(S, 7) ^ C6(S, 6) ^ C7(S, 5); L5 ^= C0(S, 5) ^ C1(S, 4) ^ C2(S, 3) ^ C3(S, 2) ^ C4(S, 1) ^ C5(S, 0) ^ C6(S, 7) ^ C7(S, 6); L6 ^= C0(S, 6) ^ C1(S, 5) ^ C2(S, 4) ^ C3(S, 3) ^ C4(S, 2) ^ C5(S, 1) ^ C6(S, 0) ^ C7(S, 7); L7 ^= C0(S, 7) ^ C1(S, 6) ^ C2(S, 5) ^ C3(S, 4) ^ C4(S, 3) ^ C5(S, 2) ^ C6(S, 1) ^ C7(S, 0); S.q[0] = L0; S.q[1] = L1; S.q[2] = L2; S.q[3] = L3; S.q[4] = L4; S.q[5] = L5; S.q[6] = L6; S.q[7] = L7; # else L0 = C0(K, 0); L1 = C1(K, 0); L2 = C2(K, 0); L3 = C3(K, 0); L4 = C4(K, 0); L5 = C5(K, 0); L6 = C6(K, 0); L7 = C7(K, 0); L0 ^= RC[r]; L1 ^= C0(K, 1); L2 ^= C1(K, 1); L3 ^= C2(K, 1); L4 ^= C3(K, 1); L5 ^= C4(K, 1); L6 ^= C5(K, 1); L7 ^= C6(K, 1); L0 ^= C7(K, 1); L2 ^= C0(K, 2); L3 ^= C1(K, 2); L4 ^= C2(K, 2); L5 ^= C3(K, 2); L6 ^= C4(K, 2); L7 ^= C5(K, 2); L0 ^= C6(K, 2); L1 ^= C7(K, 2); L3 ^= C0(K, 3); L4 ^= C1(K, 3); L5 ^= C2(K, 3); L6 ^= C3(K, 3); L7 ^= C4(K, 3); L0 ^= C5(K, 3); L1 ^= C6(K, 3); L2 ^= C7(K, 3); L4 ^= C0(K, 4); L5 ^= C1(K, 4); L6 ^= C2(K, 4); L7 ^= C3(K, 4); L0 ^= C4(K, 4); L1 ^= C5(K, 4); L2 ^= C6(K, 4); L3 ^= C7(K, 4); L5 ^= C0(K, 5); L6 ^= C1(K, 5); L7 ^= C2(K, 5); L0 ^= C3(K, 5); L1 ^= C4(K, 5); L2 ^= C5(K, 5); L3 ^= C6(K, 5); L4 ^= C7(K, 5); L6 ^= C0(K, 6); L7 ^= C1(K, 6); L0 ^= C2(K, 6); L1 ^= C3(K, 6); L2 ^= C4(K, 6); L3 ^= C5(K, 6); L4 ^= C6(K, 6); L5 ^= C7(K, 6); L7 ^= C0(K, 7); L0 ^= C1(K, 7); L1 ^= C2(K, 7); L2 ^= C3(K, 7); L3 ^= C4(K, 7); L4 ^= C5(K, 7); L5 ^= C6(K, 7); L6 ^= C7(K, 7); K.q[0] = L0; K.q[1] = L1; K.q[2] = L2; K.q[3] = L3; K.q[4] = L4; K.q[5] = L5; K.q[6] = L6; K.q[7] = L7; L0 ^= C0(S, 0); L1 ^= C1(S, 0); L2 ^= C2(S, 0); L3 ^= C3(S, 0); L4 ^= C4(S, 0); L5 ^= C5(S, 0); L6 ^= C6(S, 0); L7 ^= C7(S, 0); L1 ^= C0(S, 1); L2 ^= C1(S, 1); L3 ^= C2(S, 1); L4 ^= C3(S, 1); L5 ^= C4(S, 1); L6 ^= C5(S, 1); L7 ^= C6(S, 1); L0 ^= C7(S, 1); L2 ^= C0(S, 2); L3 ^= C1(S, 2); L4 ^= C2(S, 2); L5 ^= C3(S, 2); L6 ^= C4(S, 2); L7 ^= C5(S, 2); L0 ^= C6(S, 2); L1 ^= C7(S, 2); L3 ^= C0(S, 3); L4 ^= C1(S, 3); L5 ^= C2(S, 3); L6 ^= C3(S, 3); L7 ^= C4(S, 3); L0 ^= C5(S, 3); L1 ^= C6(S, 3); L2 ^= C7(S, 3); L4 ^= C0(S, 4); L5 ^= C1(S, 4); L6 ^= C2(S, 4); L7 ^= C3(S, 4); L0 ^= C4(S, 4); L1 ^= C5(S, 4); L2 ^= C6(S, 4); L3 ^= C7(S, 4); L5 ^= C0(S, 5); L6 ^= C1(S, 5); L7 ^= C2(S, 5); L0 ^= C3(S, 5); L1 ^= C4(S, 5); L2 ^= C5(S, 5); L3 ^= C6(S, 5); L4 ^= C7(S, 5); L6 ^= C0(S, 6); L7 ^= C1(S, 6); L0 ^= C2(S, 6); L1 ^= C3(S, 6); L2 ^= C4(S, 6); L3 ^= C5(S, 6); L4 ^= C6(S, 6); L5 ^= C7(S, 6); L7 ^= C0(S, 7); L0 ^= C1(S, 7); L1 ^= C2(S, 7); L2 ^= C3(S, 7); L3 ^= C4(S, 7); L4 ^= C5(S, 7); L5 ^= C6(S, 7); L6 ^= C7(S, 7); S.q[0] = L0; S.q[1] = L1; S.q[2] = L2; S.q[3] = L3; S.q[4] = L4; S.q[5] = L5; S.q[6] = L6; S.q[7] = L7; # endif } # ifdef STRICT_ALIGNMENT if ((size_t)p & 7) { int i; for (i = 0; i < 64; i++) H->c[i] ^= S.c[i] ^ p[i]; } else # endif { const u64_aX *pa = (const u64_aX *)p; H->q[0] ^= S.q[0] ^ pa[0]; H->q[1] ^= S.q[1] ^ pa[1]; H->q[2] ^= S.q[2] ^ pa[2]; H->q[3] ^= S.q[3] ^ pa[3]; H->q[4] ^= S.q[4] ^ pa[4]; H->q[5] ^= S.q[5] ^ pa[5]; H->q[6] ^= S.q[6] ^ pa[6]; H->q[7] ^= S.q[7] ^ pa[7]; } #endif p += 64; } while (--n); }
whrlpool
openssl/crypto/whrlpool/wp_block.c
openssl
#include <string.h> #include <stdio.h> #include <stdarg.h> #include <openssl/err.h> #include "internal/propertyerr.h" #include "internal/property.h" #include "internal/numbers.h" #include "crypto/ctype.h" #include "internal/nelem.h" #include "property_local.h" #include "internal/e_os.h" DEFINE_STACK_OF(OSSL_PROPERTY_DEFINITION) static const char *skip_space(const char *s) { while (ossl_isspace(*s)) s++; return s; } static int match_ch(const char *t[], char m) { const char *s = *t; if (*s == m) { *t = skip_space(s + 1); return 1; } return 0; } #define MATCH(s, m) match(s, m, sizeof(m) - 1) static int match(const char *t[], const char m[], size_t m_len) { const char *s = *t; if (OPENSSL_strncasecmp(s, m, m_len) == 0) { *t = skip_space(s + m_len); return 1; } return 0; } static int parse_name(OSSL_LIB_CTX *ctx, const char *t[], int create, OSSL_PROPERTY_IDX *idx) { char name[100]; int err = 0; size_t i = 0; const char *s = *t; int user_name = 0; for (;;) { if (!ossl_isalpha(*s)) { ERR_raise_data(ERR_LIB_PROP, PROP_R_NOT_AN_IDENTIFIER, "HERE-->%s", *t); return 0; } do { if (i < sizeof(name) - 1) name[i++] = ossl_tolower(*s); else err = 1; } while (*++s == '_' || ossl_isalnum(*s)); if (*s != '.') break; user_name = 1; if (i < sizeof(name) - 1) name[i++] = *s; else err = 1; s++; } name[i] = '\0'; if (err) { ERR_raise_data(ERR_LIB_PROP, PROP_R_NAME_TOO_LONG, "HERE-->%s", *t); return 0; } *t = skip_space(s); *idx = ossl_property_name(ctx, name, user_name && create); return 1; } static int parse_number(const char *t[], OSSL_PROPERTY_DEFINITION *res) { const char *s = *t; int64_t v = 0; do { if (!ossl_isdigit(*s)) { ERR_raise_data(ERR_LIB_PROP, PROP_R_NOT_A_DECIMAL_DIGIT, "HERE-->%s", *t); return 0; } if (v > ((INT64_MAX - (*s - '0')) / 10)) { ERR_raise_data(ERR_LIB_PROP, PROP_R_PARSE_FAILED, "Property %s overflows", *t); return 0; } v = v * 10 + (*s++ - '0'); } while (ossl_isdigit(*s)); if (!ossl_isspace(*s) && *s != '\0' && *s != ',') { ERR_raise_data(ERR_LIB_PROP, PROP_R_NOT_A_DECIMAL_DIGIT, "HERE-->%s", *t); return 0; } *t = skip_space(s); res->type = OSSL_PROPERTY_TYPE_NUMBER; res->v.int_val = v; return 1; } static int parse_hex(const char *t[], OSSL_PROPERTY_DEFINITION *res) { const char *s = *t; int64_t v = 0; int sval; do { if (ossl_isdigit(*s)) { sval = *s - '0'; } else if (ossl_isxdigit(*s)) { sval = ossl_tolower(*s) - 'a' + 10; } else { ERR_raise_data(ERR_LIB_PROP, PROP_R_NOT_AN_HEXADECIMAL_DIGIT, "%s", *t); return 0; } if (v > ((INT64_MAX - sval) / 16)) { ERR_raise_data(ERR_LIB_PROP, PROP_R_PARSE_FAILED, "Property %s overflows", *t); return 0; } v <<= 4; v += sval; } while (ossl_isxdigit(*++s)); if (!ossl_isspace(*s) && *s != '\0' && *s != ',') { ERR_raise_data(ERR_LIB_PROP, PROP_R_NOT_AN_HEXADECIMAL_DIGIT, "HERE-->%s", *t); return 0; } *t = skip_space(s); res->type = OSSL_PROPERTY_TYPE_NUMBER; res->v.int_val = v; return 1; } static int parse_oct(const char *t[], OSSL_PROPERTY_DEFINITION *res) { const char *s = *t; int64_t v = 0; do { if (*s == '9' || *s == '8' || !ossl_isdigit(*s)) { ERR_raise_data(ERR_LIB_PROP, PROP_R_NOT_AN_OCTAL_DIGIT, "HERE-->%s", *t); return 0; } if (v > ((INT64_MAX - (*s - '0')) / 8)) { ERR_raise_data(ERR_LIB_PROP, PROP_R_PARSE_FAILED, "Property %s overflows", *t); return 0; } v = (v << 3) + (*s - '0'); } while (ossl_isdigit(*++s) && *s != '9' && *s != '8'); if (!ossl_isspace(*s) && *s != '\0' && *s != ',') { ERR_raise_data(ERR_LIB_PROP, PROP_R_NOT_AN_OCTAL_DIGIT, "HERE-->%s", *t); return 0; } *t = skip_space(s); res->type = OSSL_PROPERTY_TYPE_NUMBER; res->v.int_val = v; return 1; } static int parse_string(OSSL_LIB_CTX *ctx, const char *t[], char delim, OSSL_PROPERTY_DEFINITION *res, const int create) { char v[1000]; const char *s = *t; size_t i = 0; int err = 0; while (*s != '\0' && *s != delim) { if (i < sizeof(v) - 1) v[i++] = *s; else err = 1; s++; } if (*s == '\0') { ERR_raise_data(ERR_LIB_PROP, PROP_R_NO_MATCHING_STRING_DELIMITER, "HERE-->%c%s", delim, *t); return 0; } v[i] = '\0'; if (err) { ERR_raise_data(ERR_LIB_PROP, PROP_R_STRING_TOO_LONG, "HERE-->%s", *t); } else { res->v.str_val = ossl_property_value(ctx, v, create); } *t = skip_space(s + 1); res->type = OSSL_PROPERTY_TYPE_STRING; return !err; } static int parse_unquoted(OSSL_LIB_CTX *ctx, const char *t[], OSSL_PROPERTY_DEFINITION *res, const int create) { char v[1000]; const char *s = *t; size_t i = 0; int err = 0; if (*s == '\0' || *s == ',') return 0; while (ossl_isprint(*s) && !ossl_isspace(*s) && *s != ',') { if (i < sizeof(v) - 1) v[i++] = ossl_tolower(*s); else err = 1; s++; } if (!ossl_isspace(*s) && *s != '\0' && *s != ',') { ERR_raise_data(ERR_LIB_PROP, PROP_R_NOT_AN_ASCII_CHARACTER, "HERE-->%s", s); return 0; } v[i] = 0; if (err) ERR_raise_data(ERR_LIB_PROP, PROP_R_STRING_TOO_LONG, "HERE-->%s", *t); else if ((res->v.str_val = ossl_property_value(ctx, v, create)) == 0) err = 1; *t = skip_space(s); res->type = OSSL_PROPERTY_TYPE_STRING; return !err; } static int parse_value(OSSL_LIB_CTX *ctx, const char *t[], OSSL_PROPERTY_DEFINITION *res, int create) { const char *s = *t; int r = 0; if (*s == '"' || *s == '\'') { s++; r = parse_string(ctx, &s, s[-1], res, create); } else if (*s == '+') { s++; r = parse_number(&s, res); } else if (*s == '-') { s++; r = parse_number(&s, res); res->v.int_val = -res->v.int_val; } else if (*s == '0' && s[1] == 'x') { s += 2; r = parse_hex(&s, res); } else if (*s == '0' && ossl_isdigit(s[1])) { s++; r = parse_oct(&s, res); } else if (ossl_isdigit(*s)) { return parse_number(t, res); } else if (ossl_isalpha(*s)) return parse_unquoted(ctx, t, res, create); if (r) *t = s; return r; } static int pd_compare(const OSSL_PROPERTY_DEFINITION *const *p1, const OSSL_PROPERTY_DEFINITION *const *p2) { const OSSL_PROPERTY_DEFINITION *pd1 = *p1; const OSSL_PROPERTY_DEFINITION *pd2 = *p2; if (pd1->name_idx < pd2->name_idx) return -1; if (pd1->name_idx > pd2->name_idx) return 1; return 0; } static void pd_free(OSSL_PROPERTY_DEFINITION *pd) { OPENSSL_free(pd); } static OSSL_PROPERTY_LIST * stack_to_property_list(OSSL_LIB_CTX *ctx, STACK_OF(OSSL_PROPERTY_DEFINITION) *sk) { const int n = sk_OSSL_PROPERTY_DEFINITION_num(sk); OSSL_PROPERTY_LIST *r; OSSL_PROPERTY_IDX prev_name_idx = 0; int i; r = OPENSSL_malloc(sizeof(*r) + (n <= 0 ? 0 : n - 1) * sizeof(r->properties[0])); if (r != NULL) { sk_OSSL_PROPERTY_DEFINITION_sort(sk); r->has_optional = 0; for (i = 0; i < n; i++) { r->properties[i] = *sk_OSSL_PROPERTY_DEFINITION_value(sk, i); r->has_optional |= r->properties[i].optional; if (i > 0 && r->properties[i].name_idx == prev_name_idx) { OPENSSL_free(r); ERR_raise_data(ERR_LIB_PROP, PROP_R_PARSE_FAILED, "Duplicated name `%s'", ossl_property_name_str(ctx, prev_name_idx)); return NULL; } prev_name_idx = r->properties[i].name_idx; } r->num_properties = n; } return r; } OSSL_PROPERTY_LIST *ossl_parse_property(OSSL_LIB_CTX *ctx, const char *defn) { OSSL_PROPERTY_DEFINITION *prop = NULL; OSSL_PROPERTY_LIST *res = NULL; STACK_OF(OSSL_PROPERTY_DEFINITION) *sk; const char *s = defn; int done; if (s == NULL || (sk = sk_OSSL_PROPERTY_DEFINITION_new(&pd_compare)) == NULL) return NULL; s = skip_space(s); done = *s == '\0'; while (!done) { const char *start = s; prop = OPENSSL_malloc(sizeof(*prop)); if (prop == NULL) goto err; memset(&prop->v, 0, sizeof(prop->v)); prop->optional = 0; if (!parse_name(ctx, &s, 1, &prop->name_idx)) goto err; prop->oper = OSSL_PROPERTY_OPER_EQ; if (prop->name_idx == 0) { ERR_raise_data(ERR_LIB_PROP, PROP_R_PARSE_FAILED, "Unknown name HERE-->%s", start); goto err; } if (match_ch(&s, '=')) { if (!parse_value(ctx, &s, prop, 1)) { ERR_raise_data(ERR_LIB_PROP, PROP_R_NO_VALUE, "HERE-->%s", start); goto err; } } else { prop->type = OSSL_PROPERTY_TYPE_STRING; prop->v.str_val = OSSL_PROPERTY_TRUE; } if (!sk_OSSL_PROPERTY_DEFINITION_push(sk, prop)) goto err; prop = NULL; done = !match_ch(&s, ','); } if (*s != '\0') { ERR_raise_data(ERR_LIB_PROP, PROP_R_TRAILING_CHARACTERS, "HERE-->%s", s); goto err; } res = stack_to_property_list(ctx, sk); err: OPENSSL_free(prop); sk_OSSL_PROPERTY_DEFINITION_pop_free(sk, &pd_free); return res; } OSSL_PROPERTY_LIST *ossl_parse_query(OSSL_LIB_CTX *ctx, const char *s, int create_values) { STACK_OF(OSSL_PROPERTY_DEFINITION) *sk; OSSL_PROPERTY_LIST *res = NULL; OSSL_PROPERTY_DEFINITION *prop = NULL; int done; if (s == NULL || (sk = sk_OSSL_PROPERTY_DEFINITION_new(&pd_compare)) == NULL) return NULL; s = skip_space(s); done = *s == '\0'; while (!done) { prop = OPENSSL_malloc(sizeof(*prop)); if (prop == NULL) goto err; memset(&prop->v, 0, sizeof(prop->v)); if (match_ch(&s, '-')) { prop->oper = OSSL_PROPERTY_OVERRIDE; prop->optional = 0; if (!parse_name(ctx, &s, 1, &prop->name_idx)) goto err; goto skip_value; } prop->optional = match_ch(&s, '?'); if (!parse_name(ctx, &s, 1, &prop->name_idx)) goto err; if (match_ch(&s, '=')) { prop->oper = OSSL_PROPERTY_OPER_EQ; } else if (MATCH(&s, "!=")) { prop->oper = OSSL_PROPERTY_OPER_NE; } else { prop->oper = OSSL_PROPERTY_OPER_EQ; prop->type = OSSL_PROPERTY_TYPE_STRING; prop->v.str_val = OSSL_PROPERTY_TRUE; goto skip_value; } if (!parse_value(ctx, &s, prop, create_values)) prop->type = OSSL_PROPERTY_TYPE_VALUE_UNDEFINED; skip_value: if (!sk_OSSL_PROPERTY_DEFINITION_push(sk, prop)) goto err; prop = NULL; done = !match_ch(&s, ','); } if (*s != '\0') { ERR_raise_data(ERR_LIB_PROP, PROP_R_TRAILING_CHARACTERS, "HERE-->%s", s); goto err; } res = stack_to_property_list(ctx, sk); err: OPENSSL_free(prop); sk_OSSL_PROPERTY_DEFINITION_pop_free(sk, &pd_free); return res; } int ossl_property_match_count(const OSSL_PROPERTY_LIST *query, const OSSL_PROPERTY_LIST *defn) { const OSSL_PROPERTY_DEFINITION *const q = query->properties; const OSSL_PROPERTY_DEFINITION *const d = defn->properties; int i = 0, j = 0, matches = 0; OSSL_PROPERTY_OPER oper; while (i < query->num_properties) { if ((oper = q[i].oper) == OSSL_PROPERTY_OVERRIDE) { i++; continue; } if (j < defn->num_properties) { if (q[i].name_idx > d[j].name_idx) { j++; continue; } if (q[i].name_idx == d[j].name_idx) { const int eq = q[i].type == d[j].type && memcmp(&q[i].v, &d[j].v, sizeof(q[i].v)) == 0; if ((eq && oper == OSSL_PROPERTY_OPER_EQ) || (!eq && oper == OSSL_PROPERTY_OPER_NE)) matches++; else if (!q[i].optional) return -1; i++; j++; continue; } } if (q[i].type == OSSL_PROPERTY_TYPE_VALUE_UNDEFINED) { if (oper == OSSL_PROPERTY_OPER_NE) matches++; else if (!q[i].optional) return -1; } else if (q[i].type != OSSL_PROPERTY_TYPE_STRING || (oper == OSSL_PROPERTY_OPER_EQ && q[i].v.str_val != OSSL_PROPERTY_FALSE) || (oper == OSSL_PROPERTY_OPER_NE && q[i].v.str_val == OSSL_PROPERTY_FALSE)) { if (!q[i].optional) return -1; } else { matches++; } i++; } return matches; } void ossl_property_free(OSSL_PROPERTY_LIST *p) { OPENSSL_free(p); } OSSL_PROPERTY_LIST *ossl_property_merge(const OSSL_PROPERTY_LIST *a, const OSSL_PROPERTY_LIST *b) { const OSSL_PROPERTY_DEFINITION *const ap = a->properties; const OSSL_PROPERTY_DEFINITION *const bp = b->properties; const OSSL_PROPERTY_DEFINITION *copy; OSSL_PROPERTY_LIST *r; int i, j, n; const int t = a->num_properties + b->num_properties; r = OPENSSL_malloc(sizeof(*r) + (t == 0 ? 0 : t - 1) * sizeof(r->properties[0])); if (r == NULL) return NULL; r->has_optional = 0; for (i = j = n = 0; i < a->num_properties || j < b->num_properties; n++) { if (i >= a->num_properties) { copy = &bp[j++]; } else if (j >= b->num_properties) { copy = &ap[i++]; } else if (ap[i].name_idx <= bp[j].name_idx) { if (ap[i].name_idx == bp[j].name_idx) j++; copy = &ap[i++]; } else { copy = &bp[j++]; } memcpy(r->properties + n, copy, sizeof(r->properties[0])); r->has_optional |= copy->optional; } r->num_properties = n; if (n != t) r = OPENSSL_realloc(r, sizeof(*r) + (n - 1) * sizeof(r->properties[0])); return r; } int ossl_property_parse_init(OSSL_LIB_CTX *ctx) { static const char *const predefined_names[] = { "provider", "version", "fips", "output", "input", "structure", }; size_t i; for (i = 0; i < OSSL_NELEM(predefined_names); i++) if (ossl_property_name(ctx, predefined_names[i], 1) == 0) goto err; if ((ossl_property_value(ctx, "yes", 1) != OSSL_PROPERTY_TRUE) || (ossl_property_value(ctx, "no", 1) != OSSL_PROPERTY_FALSE)) goto err; return 1; err: return 0; } static void put_char(char ch, char **buf, size_t *remain, size_t *needed) { if (*remain == 0) { ++*needed; return; } if (*remain == 1) **buf = '\0'; else **buf = ch; ++*buf; ++*needed; --*remain; } static void put_str(const char *str, char **buf, size_t *remain, size_t *needed) { size_t olen, len, i; char quote = '\0'; int quotes; len = olen = strlen(str); *needed += len; for (i = 0; i < len; i++) if (!ossl_isalnum(str[i]) && str[i] != '.' && str[i] != '_') { if (quote == '\0') quote = '\''; if (str[i] == '\'') quote = '"'; } quotes = quote != '\0'; if (*remain == 0) { *needed += 2 * quotes; return; } if (quotes) put_char(quote, buf, remain, needed); if (*remain < len + 1 + quotes) len = *remain - 1; if (len > 0) { memcpy(*buf, str, len); *buf += len; *remain -= len; } if (quotes) put_char(quote, buf, remain, needed); if (len < olen && *remain == 1) { **buf = '\0'; ++*buf; --*remain; } } static void put_num(int64_t val, char **buf, size_t *remain, size_t *needed) { int64_t tmpval = val; size_t len = 1; if (tmpval < 0) { len++; tmpval = -tmpval; } for (; tmpval > 9; len++, tmpval /= 10); *needed += len; if (*remain == 0) return; BIO_snprintf(*buf, *remain, "%lld", (long long int)val); if (*remain < len) { *buf += *remain; *remain = 0; } else { *buf += len; *remain -= len; } } size_t ossl_property_list_to_string(OSSL_LIB_CTX *ctx, const OSSL_PROPERTY_LIST *list, char *buf, size_t bufsize) { int i; const OSSL_PROPERTY_DEFINITION *prop = NULL; size_t needed = 0; const char *val; if (list == NULL) { if (bufsize > 0) *buf = '\0'; return 1; } if (list->num_properties != 0) prop = &list->properties[list->num_properties - 1]; for (i = 0; i < list->num_properties; i++, prop--) { if (prop->name_idx == 0) continue; if (needed > 0) put_char(',', &buf, &bufsize, &needed); if (prop->optional) put_char('?', &buf, &bufsize, &needed); else if (prop->oper == OSSL_PROPERTY_OVERRIDE) put_char('-', &buf, &bufsize, &needed); val = ossl_property_name_str(ctx, prop->name_idx); if (val == NULL) return 0; put_str(val, &buf, &bufsize, &needed); switch (prop->oper) { case OSSL_PROPERTY_OPER_NE: put_char('!', &buf, &bufsize, &needed); case OSSL_PROPERTY_OPER_EQ: put_char('=', &buf, &bufsize, &needed); switch (prop->type) { case OSSL_PROPERTY_TYPE_STRING: val = ossl_property_value_str(ctx, prop->v.str_val); if (val == NULL) return 0; put_str(val, &buf, &bufsize, &needed); break; case OSSL_PROPERTY_TYPE_NUMBER: put_num(prop->v.int_val, &buf, &bufsize, &needed); break; default: return 0; } break; default: break; } } put_char('\0', &buf, &bufsize, &needed); return needed; }
property
openssl/crypto/property/property_parse.c
openssl
#include <openssl/err.h> #include "internal/propertyerr.h" #ifndef OPENSSL_NO_ERR static const ERR_STRING_DATA PROP_str_reasons[] = { {ERR_PACK(ERR_LIB_PROP, 0, PROP_R_NAME_TOO_LONG), "name too long"}, {ERR_PACK(ERR_LIB_PROP, 0, PROP_R_NOT_AN_ASCII_CHARACTER), "not an ascii character"}, {ERR_PACK(ERR_LIB_PROP, 0, PROP_R_NOT_AN_HEXADECIMAL_DIGIT), "not an hexadecimal digit"}, {ERR_PACK(ERR_LIB_PROP, 0, PROP_R_NOT_AN_IDENTIFIER), "not an identifier"}, {ERR_PACK(ERR_LIB_PROP, 0, PROP_R_NOT_AN_OCTAL_DIGIT), "not an octal digit"}, {ERR_PACK(ERR_LIB_PROP, 0, PROP_R_NOT_A_DECIMAL_DIGIT), "not a decimal digit"}, {ERR_PACK(ERR_LIB_PROP, 0, PROP_R_NO_MATCHING_STRING_DELIMITER), "no matching string delimiter"}, {ERR_PACK(ERR_LIB_PROP, 0, PROP_R_NO_VALUE), "no value"}, {ERR_PACK(ERR_LIB_PROP, 0, PROP_R_PARSE_FAILED), "parse failed"}, {ERR_PACK(ERR_LIB_PROP, 0, PROP_R_STRING_TOO_LONG), "string too long"}, {ERR_PACK(ERR_LIB_PROP, 0, PROP_R_TRAILING_CHARACTERS), "trailing characters"}, {0, NULL} }; #endif int ossl_err_load_PROP_strings(void) { #ifndef OPENSSL_NO_ERR if (ERR_reason_error_string(PROP_str_reasons[0].error) == NULL) ERR_load_strings_const(PROP_str_reasons); #endif return 1; }
property
openssl/crypto/property/property_err.c
openssl
#include <string.h> #include <openssl/crypto.h> #include <openssl/lhash.h> #include "crypto/lhash.h" #include "property_local.h" #include "crypto/context.h" typedef struct { const char *s; OSSL_PROPERTY_IDX idx; char body[1]; } PROPERTY_STRING; DEFINE_LHASH_OF_EX(PROPERTY_STRING); typedef LHASH_OF(PROPERTY_STRING) PROP_TABLE; typedef struct { CRYPTO_RWLOCK *lock; PROP_TABLE *prop_names; PROP_TABLE *prop_values; OSSL_PROPERTY_IDX prop_name_idx; OSSL_PROPERTY_IDX prop_value_idx; #ifndef OPENSSL_SMALL_FOOTPRINT STACK_OF(OPENSSL_CSTRING) *prop_namelist; STACK_OF(OPENSSL_CSTRING) *prop_valuelist; #endif } PROPERTY_STRING_DATA; static unsigned long property_hash(const PROPERTY_STRING *a) { return OPENSSL_LH_strhash(a->s); } static int property_cmp(const PROPERTY_STRING *a, const PROPERTY_STRING *b) { return strcmp(a->s, b->s); } static void property_free(PROPERTY_STRING *ps) { OPENSSL_free(ps); } static void property_table_free(PROP_TABLE **pt) { PROP_TABLE *t = *pt; if (t != NULL) { lh_PROPERTY_STRING_doall(t, &property_free); lh_PROPERTY_STRING_free(t); *pt = NULL; } } void ossl_property_string_data_free(void *vpropdata) { PROPERTY_STRING_DATA *propdata = vpropdata; if (propdata == NULL) return; CRYPTO_THREAD_lock_free(propdata->lock); property_table_free(&propdata->prop_names); property_table_free(&propdata->prop_values); #ifndef OPENSSL_SMALL_FOOTPRINT sk_OPENSSL_CSTRING_free(propdata->prop_namelist); sk_OPENSSL_CSTRING_free(propdata->prop_valuelist); propdata->prop_namelist = propdata->prop_valuelist = NULL; #endif propdata->prop_name_idx = propdata->prop_value_idx = 0; OPENSSL_free(propdata); } void *ossl_property_string_data_new(OSSL_LIB_CTX *ctx) { PROPERTY_STRING_DATA *propdata = OPENSSL_zalloc(sizeof(*propdata)); if (propdata == NULL) return NULL; propdata->lock = CRYPTO_THREAD_lock_new(); propdata->prop_names = lh_PROPERTY_STRING_new(&property_hash, &property_cmp); propdata->prop_values = lh_PROPERTY_STRING_new(&property_hash, &property_cmp); #ifndef OPENSSL_SMALL_FOOTPRINT propdata->prop_namelist = sk_OPENSSL_CSTRING_new_null(); propdata->prop_valuelist = sk_OPENSSL_CSTRING_new_null(); #endif if (propdata->lock == NULL #ifndef OPENSSL_SMALL_FOOTPRINT || propdata->prop_namelist == NULL || propdata->prop_valuelist == NULL #endif || propdata->prop_names == NULL || propdata->prop_values == NULL) { ossl_property_string_data_free(propdata); return NULL; } return propdata; } static PROPERTY_STRING *new_property_string(const char *s, OSSL_PROPERTY_IDX *pidx) { const size_t l = strlen(s); PROPERTY_STRING *ps = OPENSSL_malloc(sizeof(*ps) + l); if (ps != NULL) { memcpy(ps->body, s, l + 1); ps->s = ps->body; ps->idx = ++*pidx; if (ps->idx == 0) { OPENSSL_free(ps); return NULL; } } return ps; } static OSSL_PROPERTY_IDX ossl_property_string(OSSL_LIB_CTX *ctx, int name, int create, const char *s) { PROPERTY_STRING p, *ps, *ps_new; PROP_TABLE *t; OSSL_PROPERTY_IDX *pidx; PROPERTY_STRING_DATA *propdata = ossl_lib_ctx_get_data(ctx, OSSL_LIB_CTX_PROPERTY_STRING_INDEX); if (propdata == NULL) return 0; t = name ? propdata->prop_names : propdata->prop_values; p.s = s; if (!CRYPTO_THREAD_read_lock(propdata->lock)) { ERR_raise(ERR_LIB_CRYPTO, ERR_R_UNABLE_TO_GET_READ_LOCK); return 0; } ps = lh_PROPERTY_STRING_retrieve(t, &p); if (ps == NULL && create) { CRYPTO_THREAD_unlock(propdata->lock); if (!CRYPTO_THREAD_write_lock(propdata->lock)) { ERR_raise(ERR_LIB_CRYPTO, ERR_R_UNABLE_TO_GET_WRITE_LOCK); return 0; } pidx = name ? &propdata->prop_name_idx : &propdata->prop_value_idx; ps = lh_PROPERTY_STRING_retrieve(t, &p); if (ps == NULL && (ps_new = new_property_string(s, pidx)) != NULL) { #ifndef OPENSSL_SMALL_FOOTPRINT STACK_OF(OPENSSL_CSTRING) *slist; slist = name ? propdata->prop_namelist : propdata->prop_valuelist; if (sk_OPENSSL_CSTRING_push(slist, ps_new->s) <= 0) { property_free(ps_new); CRYPTO_THREAD_unlock(propdata->lock); return 0; } #endif lh_PROPERTY_STRING_insert(t, ps_new); if (lh_PROPERTY_STRING_error(t)) { #ifndef OPENSSL_SMALL_FOOTPRINT sk_OPENSSL_CSTRING_pop(slist); #endif property_free(ps_new); --*pidx; CRYPTO_THREAD_unlock(propdata->lock); return 0; } ps = ps_new; } } CRYPTO_THREAD_unlock(propdata->lock); return ps != NULL ? ps->idx : 0; } #ifdef OPENSSL_SMALL_FOOTPRINT struct find_str_st { const char *str; OSSL_PROPERTY_IDX idx; }; static void find_str_fn(PROPERTY_STRING *prop, void *vfindstr) { struct find_str_st *findstr = vfindstr; if (prop->idx == findstr->idx) findstr->str = prop->s; } #endif static const char *ossl_property_str(int name, OSSL_LIB_CTX *ctx, OSSL_PROPERTY_IDX idx) { const char *r; PROPERTY_STRING_DATA *propdata = ossl_lib_ctx_get_data(ctx, OSSL_LIB_CTX_PROPERTY_STRING_INDEX); if (propdata == NULL) return NULL; if (!CRYPTO_THREAD_read_lock(propdata->lock)) { ERR_raise(ERR_LIB_CRYPTO, ERR_R_UNABLE_TO_GET_READ_LOCK); return NULL; } #ifdef OPENSSL_SMALL_FOOTPRINT { struct find_str_st findstr; findstr.str = NULL; findstr.idx = idx; lh_PROPERTY_STRING_doall_arg(name ? propdata->prop_names : propdata->prop_values, find_str_fn, &findstr); r = findstr.str; } #else r = sk_OPENSSL_CSTRING_value(name ? propdata->prop_namelist : propdata->prop_valuelist, idx - 1); #endif CRYPTO_THREAD_unlock(propdata->lock); return r; } OSSL_PROPERTY_IDX ossl_property_name(OSSL_LIB_CTX *ctx, const char *s, int create) { return ossl_property_string(ctx, 1, create, s); } const char *ossl_property_name_str(OSSL_LIB_CTX *ctx, OSSL_PROPERTY_IDX idx) { return ossl_property_str(1, ctx, idx); } OSSL_PROPERTY_IDX ossl_property_value(OSSL_LIB_CTX *ctx, const char *s, int create) { return ossl_property_string(ctx, 0, create, s); } const char *ossl_property_value_str(OSSL_LIB_CTX *ctx, OSSL_PROPERTY_IDX idx) { return ossl_property_str(0, ctx, idx); }
property
openssl/crypto/property/property_string.c
openssl
#include "internal/propertyerr.h" #include "internal/property.h" #include "property_local.h" static int property_idx_cmp(const void *keyp, const void *compare) { OSSL_PROPERTY_IDX key = *(const OSSL_PROPERTY_IDX *)keyp; const OSSL_PROPERTY_DEFINITION *defn = (const OSSL_PROPERTY_DEFINITION *)compare; return key - defn->name_idx; } const OSSL_PROPERTY_DEFINITION * ossl_property_find_property(const OSSL_PROPERTY_LIST *list, OSSL_LIB_CTX *libctx, const char *name) { OSSL_PROPERTY_IDX name_idx; if (list == NULL || name == NULL || (name_idx = ossl_property_name(libctx, name, 0)) == 0) return NULL; return ossl_bsearch(&name_idx, list->properties, list->num_properties, sizeof(*list->properties), &property_idx_cmp, 0); } OSSL_PROPERTY_TYPE ossl_property_get_type(const OSSL_PROPERTY_DEFINITION *prop) { return prop->type; } const char *ossl_property_get_string_value(OSSL_LIB_CTX *libctx, const OSSL_PROPERTY_DEFINITION *prop) { const char *value = NULL; if (prop != NULL && prop->type == OSSL_PROPERTY_TYPE_STRING) value = ossl_property_value_str(libctx, prop->v.str_val); return value; } int64_t ossl_property_get_number_value(const OSSL_PROPERTY_DEFINITION *prop) { int64_t value = 0; if (prop != NULL && prop->type == OSSL_PROPERTY_TYPE_NUMBER) value = prop->v.int_val; return value; } int ossl_property_has_optional(const OSSL_PROPERTY_LIST *query) { return query->has_optional ? 1 : 0; } int ossl_property_is_enabled(OSSL_LIB_CTX *ctx, const char *property_name, const OSSL_PROPERTY_LIST *prop_list) { const OSSL_PROPERTY_DEFINITION *prop; prop = ossl_property_find_property(prop_list, ctx, property_name); if (prop == NULL || prop->optional || prop->oper == OSSL_PROPERTY_OVERRIDE) return 0; return (prop->type == OSSL_PROPERTY_TYPE_STRING && ((prop->oper == OSSL_PROPERTY_OPER_EQ && prop->v.str_val == OSSL_PROPERTY_TRUE) || (prop->oper == OSSL_PROPERTY_OPER_NE && prop->v.str_val != OSSL_PROPERTY_TRUE))); }
property
openssl/crypto/property/property_query.c
openssl
#include <string.h> #include <stdio.h> #include <stdarg.h> #include <openssl/crypto.h> #include "internal/core.h" #include "internal/property.h" #include "internal/provider.h" #include "internal/tsan_assist.h" #include "crypto/ctype.h" #include <openssl/lhash.h> #include <openssl/rand.h> #include "internal/thread_once.h" #include "crypto/lhash.h" #include "crypto/sparse_array.h" #include "property_local.h" #include "crypto/context.h" #define IMPL_CACHE_FLUSH_THRESHOLD 500 typedef struct { void *method; int (*up_ref)(void *); void (*free)(void *); } METHOD; typedef struct { const OSSL_PROVIDER *provider; OSSL_PROPERTY_LIST *properties; METHOD method; } IMPLEMENTATION; DEFINE_STACK_OF(IMPLEMENTATION) typedef struct { const OSSL_PROVIDER *provider; const char *query; METHOD method; char body[1]; } QUERY; DEFINE_LHASH_OF_EX(QUERY); typedef struct { int nid; STACK_OF(IMPLEMENTATION) *impls; LHASH_OF(QUERY) *cache; } ALGORITHM; struct ossl_method_store_st { OSSL_LIB_CTX *ctx; SPARSE_ARRAY_OF(ALGORITHM) *algs; CRYPTO_RWLOCK *lock; CRYPTO_RWLOCK *biglock; size_t cache_nelem; int cache_need_flush; }; typedef struct { LHASH_OF(QUERY) *cache; size_t nelem; uint32_t seed; unsigned char using_global_seed; } IMPL_CACHE_FLUSH; DEFINE_SPARSE_ARRAY_OF(ALGORITHM); typedef struct ossl_global_properties_st { OSSL_PROPERTY_LIST *list; #ifndef FIPS_MODULE unsigned int no_mirrored : 1; #endif } OSSL_GLOBAL_PROPERTIES; static void ossl_method_cache_flush_alg(OSSL_METHOD_STORE *store, ALGORITHM *alg); static void ossl_method_cache_flush(OSSL_METHOD_STORE *store, int nid); void ossl_ctx_global_properties_free(void *vglobp) { OSSL_GLOBAL_PROPERTIES *globp = vglobp; if (globp != NULL) { ossl_property_free(globp->list); OPENSSL_free(globp); } } void *ossl_ctx_global_properties_new(OSSL_LIB_CTX *ctx) { return OPENSSL_zalloc(sizeof(OSSL_GLOBAL_PROPERTIES)); } OSSL_PROPERTY_LIST **ossl_ctx_global_properties(OSSL_LIB_CTX *libctx, ossl_unused int loadconfig) { OSSL_GLOBAL_PROPERTIES *globp; #if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_AUTOLOAD_CONFIG) if (loadconfig && !OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL)) return NULL; #endif globp = ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_GLOBAL_PROPERTIES); return globp != NULL ? &globp->list : NULL; } #ifndef FIPS_MODULE int ossl_global_properties_no_mirrored(OSSL_LIB_CTX *libctx) { OSSL_GLOBAL_PROPERTIES *globp = ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_GLOBAL_PROPERTIES); return globp != NULL && globp->no_mirrored ? 1 : 0; } void ossl_global_properties_stop_mirroring(OSSL_LIB_CTX *libctx) { OSSL_GLOBAL_PROPERTIES *globp = ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_GLOBAL_PROPERTIES); if (globp != NULL) globp->no_mirrored = 1; } #endif static int ossl_method_up_ref(METHOD *method) { return (*method->up_ref)(method->method); } static void ossl_method_free(METHOD *method) { (*method->free)(method->method); } static __owur int ossl_property_read_lock(OSSL_METHOD_STORE *p) { return p != NULL ? CRYPTO_THREAD_read_lock(p->lock) : 0; } static __owur int ossl_property_write_lock(OSSL_METHOD_STORE *p) { return p != NULL ? CRYPTO_THREAD_write_lock(p->lock) : 0; } static int ossl_property_unlock(OSSL_METHOD_STORE *p) { return p != 0 ? CRYPTO_THREAD_unlock(p->lock) : 0; } static unsigned long query_hash(const QUERY *a) { return OPENSSL_LH_strhash(a->query); } static int query_cmp(const QUERY *a, const QUERY *b) { int res = strcmp(a->query, b->query); if (res == 0 && a->provider != NULL && b->provider != NULL) res = b->provider > a->provider ? 1 : b->provider < a->provider ? -1 : 0; return res; } static void impl_free(IMPLEMENTATION *impl) { if (impl != NULL) { ossl_method_free(&impl->method); OPENSSL_free(impl); } } static void impl_cache_free(QUERY *elem) { if (elem != NULL) { ossl_method_free(&elem->method); OPENSSL_free(elem); } } static void impl_cache_flush_alg(ossl_uintmax_t idx, ALGORITHM *alg) { lh_QUERY_doall(alg->cache, &impl_cache_free); lh_QUERY_flush(alg->cache); } static void alg_cleanup(ossl_uintmax_t idx, ALGORITHM *a, void *arg) { OSSL_METHOD_STORE *store = arg; if (a != NULL) { sk_IMPLEMENTATION_pop_free(a->impls, &impl_free); lh_QUERY_doall(a->cache, &impl_cache_free); lh_QUERY_free(a->cache); OPENSSL_free(a); } if (store != NULL) ossl_sa_ALGORITHM_set(store->algs, idx, NULL); } OSSL_METHOD_STORE *ossl_method_store_new(OSSL_LIB_CTX *ctx) { OSSL_METHOD_STORE *res; res = OPENSSL_zalloc(sizeof(*res)); if (res != NULL) { res->ctx = ctx; if ((res->algs = ossl_sa_ALGORITHM_new()) == NULL || (res->lock = CRYPTO_THREAD_lock_new()) == NULL || (res->biglock = CRYPTO_THREAD_lock_new()) == NULL) { ossl_method_store_free(res); return NULL; } } return res; } void ossl_method_store_free(OSSL_METHOD_STORE *store) { if (store != NULL) { if (store->algs != NULL) ossl_sa_ALGORITHM_doall_arg(store->algs, &alg_cleanup, store); ossl_sa_ALGORITHM_free(store->algs); CRYPTO_THREAD_lock_free(store->lock); CRYPTO_THREAD_lock_free(store->biglock); OPENSSL_free(store); } } int ossl_method_lock_store(OSSL_METHOD_STORE *store) { return store != NULL ? CRYPTO_THREAD_write_lock(store->biglock) : 0; } int ossl_method_unlock_store(OSSL_METHOD_STORE *store) { return store != NULL ? CRYPTO_THREAD_unlock(store->biglock) : 0; } static ALGORITHM *ossl_method_store_retrieve(OSSL_METHOD_STORE *store, int nid) { return ossl_sa_ALGORITHM_get(store->algs, nid); } static int ossl_method_store_insert(OSSL_METHOD_STORE *store, ALGORITHM *alg) { return ossl_sa_ALGORITHM_set(store->algs, alg->nid, alg); } int ossl_method_store_add(OSSL_METHOD_STORE *store, const OSSL_PROVIDER *prov, int nid, const char *properties, void *method, int (*method_up_ref)(void *), void (*method_destruct)(void *)) { ALGORITHM *alg = NULL; IMPLEMENTATION *impl; int ret = 0; int i; if (nid <= 0 || method == NULL || store == NULL) return 0; if (properties == NULL) properties = ""; if (!ossl_assert(prov != NULL)) return 0; impl = OPENSSL_malloc(sizeof(*impl)); if (impl == NULL) return 0; impl->method.method = method; impl->method.up_ref = method_up_ref; impl->method.free = method_destruct; if (!ossl_method_up_ref(&impl->method)) { OPENSSL_free(impl); return 0; } impl->provider = prov; if (!ossl_property_write_lock(store)) { OPENSSL_free(impl); return 0; } ossl_method_cache_flush(store, nid); if ((impl->properties = ossl_prop_defn_get(store->ctx, properties)) == NULL) { impl->properties = ossl_parse_property(store->ctx, properties); if (impl->properties == NULL) goto err; if (!ossl_prop_defn_set(store->ctx, properties, &impl->properties)) { ossl_property_free(impl->properties); impl->properties = NULL; goto err; } } alg = ossl_method_store_retrieve(store, nid); if (alg == NULL) { if ((alg = OPENSSL_zalloc(sizeof(*alg))) == NULL || (alg->impls = sk_IMPLEMENTATION_new_null()) == NULL || (alg->cache = lh_QUERY_new(&query_hash, &query_cmp)) == NULL) goto err; alg->nid = nid; if (!ossl_method_store_insert(store, alg)) goto err; } for (i = 0; i < sk_IMPLEMENTATION_num(alg->impls); i++) { const IMPLEMENTATION *tmpimpl = sk_IMPLEMENTATION_value(alg->impls, i); if (tmpimpl->provider == impl->provider && tmpimpl->properties == impl->properties) break; } if (i == sk_IMPLEMENTATION_num(alg->impls) && sk_IMPLEMENTATION_push(alg->impls, impl)) ret = 1; ossl_property_unlock(store); if (ret == 0) impl_free(impl); return ret; err: ossl_property_unlock(store); alg_cleanup(0, alg, NULL); impl_free(impl); return 0; } int ossl_method_store_remove(OSSL_METHOD_STORE *store, int nid, const void *method) { ALGORITHM *alg = NULL; int i; if (nid <= 0 || method == NULL || store == NULL) return 0; if (!ossl_property_write_lock(store)) return 0; ossl_method_cache_flush(store, nid); alg = ossl_method_store_retrieve(store, nid); if (alg == NULL) { ossl_property_unlock(store); return 0; } for (i = 0; i < sk_IMPLEMENTATION_num(alg->impls); i++) { IMPLEMENTATION *impl = sk_IMPLEMENTATION_value(alg->impls, i); if (impl->method.method == method) { impl_free(impl); (void)sk_IMPLEMENTATION_delete(alg->impls, i); ossl_property_unlock(store); return 1; } } ossl_property_unlock(store); return 0; } struct alg_cleanup_by_provider_data_st { OSSL_METHOD_STORE *store; const OSSL_PROVIDER *prov; }; static void alg_cleanup_by_provider(ossl_uintmax_t idx, ALGORITHM *alg, void *arg) { struct alg_cleanup_by_provider_data_st *data = arg; int i, count; for (count = 0, i = sk_IMPLEMENTATION_num(alg->impls); i-- > 0;) { IMPLEMENTATION *impl = sk_IMPLEMENTATION_value(alg->impls, i); if (impl->provider == data->prov) { impl_free(impl); (void)sk_IMPLEMENTATION_delete(alg->impls, i); count++; } } if (count > 0) ossl_method_cache_flush_alg(data->store, alg); } int ossl_method_store_remove_all_provided(OSSL_METHOD_STORE *store, const OSSL_PROVIDER *prov) { struct alg_cleanup_by_provider_data_st data; if (!ossl_property_write_lock(store)) return 0; data.prov = prov; data.store = store; ossl_sa_ALGORITHM_doall_arg(store->algs, &alg_cleanup_by_provider, &data); ossl_property_unlock(store); return 1; } static void alg_do_one(ALGORITHM *alg, IMPLEMENTATION *impl, void (*fn)(int id, void *method, void *fnarg), void *fnarg) { fn(alg->nid, impl->method.method, fnarg); } struct alg_do_each_data_st { void (*fn)(int id, void *method, void *fnarg); void *fnarg; }; static void alg_do_each(ossl_uintmax_t idx, ALGORITHM *alg, void *arg) { struct alg_do_each_data_st *data = arg; int i, end = sk_IMPLEMENTATION_num(alg->impls); for (i = 0; i < end; i++) { IMPLEMENTATION *impl = sk_IMPLEMENTATION_value(alg->impls, i); alg_do_one(alg, impl, data->fn, data->fnarg); } } void ossl_method_store_do_all(OSSL_METHOD_STORE *store, void (*fn)(int id, void *method, void *fnarg), void *fnarg) { struct alg_do_each_data_st data; data.fn = fn; data.fnarg = fnarg; if (store != NULL) ossl_sa_ALGORITHM_doall_arg(store->algs, alg_do_each, &data); } int ossl_method_store_fetch(OSSL_METHOD_STORE *store, int nid, const char *prop_query, const OSSL_PROVIDER **prov_rw, void **method) { OSSL_PROPERTY_LIST **plp; ALGORITHM *alg; IMPLEMENTATION *impl, *best_impl = NULL; OSSL_PROPERTY_LIST *pq = NULL, *p2 = NULL; const OSSL_PROVIDER *prov = prov_rw != NULL ? *prov_rw : NULL; int ret = 0; int j, best = -1, score, optional; if (nid <= 0 || method == NULL || store == NULL) return 0; #if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_AUTOLOAD_CONFIG) if (ossl_lib_ctx_is_default(store->ctx) && !OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL)) return 0; #endif if (!ossl_property_read_lock(store)) return 0; alg = ossl_method_store_retrieve(store, nid); if (alg == NULL) { ossl_property_unlock(store); return 0; } if (prop_query != NULL) p2 = pq = ossl_parse_query(store->ctx, prop_query, 0); plp = ossl_ctx_global_properties(store->ctx, 0); if (plp != NULL && *plp != NULL) { if (pq == NULL) { pq = *plp; } else { p2 = ossl_property_merge(pq, *plp); ossl_property_free(pq); if (p2 == NULL) goto fin; pq = p2; } } if (pq == NULL) { for (j = 0; j < sk_IMPLEMENTATION_num(alg->impls); j++) { if ((impl = sk_IMPLEMENTATION_value(alg->impls, j)) != NULL && (prov == NULL || impl->provider == prov)) { best_impl = impl; ret = 1; break; } } goto fin; } optional = ossl_property_has_optional(pq); for (j = 0; j < sk_IMPLEMENTATION_num(alg->impls); j++) { if ((impl = sk_IMPLEMENTATION_value(alg->impls, j)) != NULL && (prov == NULL || impl->provider == prov)) { score = ossl_property_match_count(pq, impl->properties); if (score > best) { best_impl = impl; best = score; ret = 1; if (!optional) goto fin; } } } fin: if (ret && ossl_method_up_ref(&best_impl->method)) { *method = best_impl->method.method; if (prov_rw != NULL) *prov_rw = best_impl->provider; } else { ret = 0; } ossl_property_unlock(store); ossl_property_free(p2); return ret; } static void ossl_method_cache_flush_alg(OSSL_METHOD_STORE *store, ALGORITHM *alg) { store->cache_nelem -= lh_QUERY_num_items(alg->cache); impl_cache_flush_alg(0, alg); } static void ossl_method_cache_flush(OSSL_METHOD_STORE *store, int nid) { ALGORITHM *alg = ossl_method_store_retrieve(store, nid); if (alg != NULL) ossl_method_cache_flush_alg(store, alg); } int ossl_method_store_cache_flush_all(OSSL_METHOD_STORE *store) { if (!ossl_property_write_lock(store)) return 0; ossl_sa_ALGORITHM_doall(store->algs, &impl_cache_flush_alg); store->cache_nelem = 0; ossl_property_unlock(store); return 1; } IMPLEMENT_LHASH_DOALL_ARG(QUERY, IMPL_CACHE_FLUSH); static void impl_cache_flush_cache(QUERY *c, IMPL_CACHE_FLUSH *state) { uint32_t n; n = state->seed; n ^= n << 13; n ^= n >> 17; n ^= n << 5; state->seed = n; if ((n & 1) != 0) impl_cache_free(lh_QUERY_delete(state->cache, c)); else state->nelem++; } static void impl_cache_flush_one_alg(ossl_uintmax_t idx, ALGORITHM *alg, void *v) { IMPL_CACHE_FLUSH *state = (IMPL_CACHE_FLUSH *)v; state->cache = alg->cache; lh_QUERY_doall_IMPL_CACHE_FLUSH(state->cache, &impl_cache_flush_cache, state); } static void ossl_method_cache_flush_some(OSSL_METHOD_STORE *store) { IMPL_CACHE_FLUSH state; static TSAN_QUALIFIER uint32_t global_seed = 1; state.nelem = 0; state.using_global_seed = 0; if ((state.seed = OPENSSL_rdtsc()) == 0) { state.using_global_seed = 1; state.seed = tsan_load(&global_seed); } store->cache_need_flush = 0; ossl_sa_ALGORITHM_doall_arg(store->algs, &impl_cache_flush_one_alg, &state); store->cache_nelem = state.nelem; if (state.using_global_seed) tsan_add(&global_seed, state.seed); } int ossl_method_store_cache_get(OSSL_METHOD_STORE *store, OSSL_PROVIDER *prov, int nid, const char *prop_query, void **method) { ALGORITHM *alg; QUERY elem, *r; int res = 0; if (nid <= 0 || store == NULL || prop_query == NULL) return 0; if (!ossl_property_read_lock(store)) return 0; alg = ossl_method_store_retrieve(store, nid); if (alg == NULL) goto err; elem.query = prop_query; elem.provider = prov; r = lh_QUERY_retrieve(alg->cache, &elem); if (r == NULL) goto err; if (ossl_method_up_ref(&r->method)) { *method = r->method.method; res = 1; } err: ossl_property_unlock(store); return res; } int ossl_method_store_cache_set(OSSL_METHOD_STORE *store, OSSL_PROVIDER *prov, int nid, const char *prop_query, void *method, int (*method_up_ref)(void *), void (*method_destruct)(void *)) { QUERY elem, *old, *p = NULL; ALGORITHM *alg; size_t len; int res = 1; if (nid <= 0 || store == NULL || prop_query == NULL) return 0; if (!ossl_assert(prov != NULL)) return 0; if (!ossl_property_write_lock(store)) return 0; if (store->cache_need_flush) ossl_method_cache_flush_some(store); alg = ossl_method_store_retrieve(store, nid); if (alg == NULL) goto err; if (method == NULL) { elem.query = prop_query; elem.provider = prov; if ((old = lh_QUERY_delete(alg->cache, &elem)) != NULL) { impl_cache_free(old); store->cache_nelem--; } goto end; } p = OPENSSL_malloc(sizeof(*p) + (len = strlen(prop_query))); if (p != NULL) { p->query = p->body; p->provider = prov; p->method.method = method; p->method.up_ref = method_up_ref; p->method.free = method_destruct; if (!ossl_method_up_ref(&p->method)) goto err; memcpy((char *)p->query, prop_query, len + 1); if ((old = lh_QUERY_insert(alg->cache, p)) != NULL) { impl_cache_free(old); goto end; } if (!lh_QUERY_error(alg->cache)) { if (++store->cache_nelem >= IMPL_CACHE_FLUSH_THRESHOLD) store->cache_need_flush = 1; goto end; } ossl_method_free(&p->method); } err: res = 0; OPENSSL_free(p); end: ossl_property_unlock(store); return res; }
property
openssl/crypto/property/property.c
openssl
#include <string.h> #include <openssl/err.h> #include <openssl/lhash.h> #include "internal/propertyerr.h" #include "internal/property.h" #include "internal/core.h" #include "property_local.h" #include "crypto/context.h" typedef struct { const char *prop; OSSL_PROPERTY_LIST *defn; char body[1]; } PROPERTY_DEFN_ELEM; DEFINE_LHASH_OF_EX(PROPERTY_DEFN_ELEM); static unsigned long property_defn_hash(const PROPERTY_DEFN_ELEM *a) { return OPENSSL_LH_strhash(a->prop); } static int property_defn_cmp(const PROPERTY_DEFN_ELEM *a, const PROPERTY_DEFN_ELEM *b) { return strcmp(a->prop, b->prop); } static void property_defn_free(PROPERTY_DEFN_ELEM *elem) { ossl_property_free(elem->defn); OPENSSL_free(elem); } void ossl_property_defns_free(void *vproperty_defns) { LHASH_OF(PROPERTY_DEFN_ELEM) *property_defns = vproperty_defns; if (property_defns != NULL) { lh_PROPERTY_DEFN_ELEM_doall(property_defns, &property_defn_free); lh_PROPERTY_DEFN_ELEM_free(property_defns); } } void *ossl_property_defns_new(OSSL_LIB_CTX *ctx) { return lh_PROPERTY_DEFN_ELEM_new(&property_defn_hash, &property_defn_cmp); } OSSL_PROPERTY_LIST *ossl_prop_defn_get(OSSL_LIB_CTX *ctx, const char *prop) { PROPERTY_DEFN_ELEM elem, *r; LHASH_OF(PROPERTY_DEFN_ELEM) *property_defns; property_defns = ossl_lib_ctx_get_data(ctx, OSSL_LIB_CTX_PROPERTY_DEFN_INDEX); if (!ossl_assert(property_defns != NULL) || !ossl_lib_ctx_read_lock(ctx)) return NULL; elem.prop = prop; r = lh_PROPERTY_DEFN_ELEM_retrieve(property_defns, &elem); ossl_lib_ctx_unlock(ctx); if (r == NULL || !ossl_assert(r->defn != NULL)) return NULL; return r->defn; } int ossl_prop_defn_set(OSSL_LIB_CTX *ctx, const char *prop, OSSL_PROPERTY_LIST **pl) { PROPERTY_DEFN_ELEM elem, *old, *p = NULL; size_t len; LHASH_OF(PROPERTY_DEFN_ELEM) *property_defns; int res = 1; property_defns = ossl_lib_ctx_get_data(ctx, OSSL_LIB_CTX_PROPERTY_DEFN_INDEX); if (property_defns == NULL) return 0; if (prop == NULL) return 1; if (!ossl_lib_ctx_write_lock(ctx)) return 0; elem.prop = prop; if (pl == NULL) { lh_PROPERTY_DEFN_ELEM_delete(property_defns, &elem); goto end; } if ((p = lh_PROPERTY_DEFN_ELEM_retrieve(property_defns, &elem)) != NULL) { ossl_property_free(*pl); *pl = p->defn; goto end; } len = strlen(prop); p = OPENSSL_malloc(sizeof(*p) + len); if (p != NULL) { p->prop = p->body; p->defn = *pl; memcpy(p->body, prop, len + 1); old = lh_PROPERTY_DEFN_ELEM_insert(property_defns, p); if (!ossl_assert(old == NULL)) goto end; if (!lh_PROPERTY_DEFN_ELEM_error(property_defns)) goto end; } OPENSSL_free(p); res = 0; end: ossl_lib_ctx_unlock(ctx); return res; }
property
openssl/crypto/property/defn_cache.c
openssl
#include <string.h> #include <openssl/crypto.h> #include "internal/cryptlib.h" #include "internal/endian.h" #include "crypto/modes.h" #if defined(__GNUC__) && !defined(STRICT_ALIGNMENT) typedef size_t size_t_aX __attribute((__aligned__(1))); #else typedef size_t size_t_aX; #endif #if defined(BSWAP4) && defined(STRICT_ALIGNMENT) # undef GETU32 # define GETU32(p) BSWAP4(*(const u32 *)(p)) # undef PUTU32 # define PUTU32(p,v) *(u32 *)(p) = BSWAP4(v) #endif #if defined(__riscv) # define INCLUDE_C_GMULT_4BIT # define INCLUDE_C_GHASH_4BIT #endif #define PACK(s) ((size_t)(s)<<(sizeof(size_t)*8-16)) #define REDUCE1BIT(V) do { \ if (sizeof(size_t)==8) { \ u64 T = U64(0xe100000000000000) & (0-(V.lo&1)); \ V.lo = (V.hi<<63)|(V.lo>>1); \ V.hi = (V.hi>>1 )^T; \ } \ else { \ u32 T = 0xe1000000U & (0-(u32)(V.lo&1)); \ V.lo = (V.hi<<63)|(V.lo>>1); \ V.hi = (V.hi>>1 )^((u64)T<<32); \ } \ } while(0) static void gcm_init_4bit(u128 Htable[16], const u64 H[2]) { u128 V; # if defined(OPENSSL_SMALL_FOOTPRINT) int i; # endif Htable[0].hi = 0; Htable[0].lo = 0; V.hi = H[0]; V.lo = H[1]; # if defined(OPENSSL_SMALL_FOOTPRINT) for (Htable[8] = V, i = 4; i > 0; i >>= 1) { REDUCE1BIT(V); Htable[i] = V; } for (i = 2; i < 16; i <<= 1) { u128 *Hi = Htable + i; int j; for (V = *Hi, j = 1; j < i; ++j) { Hi[j].hi = V.hi ^ Htable[j].hi; Hi[j].lo = V.lo ^ Htable[j].lo; } } # else Htable[8] = V; REDUCE1BIT(V); Htable[4] = V; REDUCE1BIT(V); Htable[2] = V; REDUCE1BIT(V); Htable[1] = V; Htable[3].hi = V.hi ^ Htable[2].hi, Htable[3].lo = V.lo ^ Htable[2].lo; V = Htable[4]; Htable[5].hi = V.hi ^ Htable[1].hi, Htable[5].lo = V.lo ^ Htable[1].lo; Htable[6].hi = V.hi ^ Htable[2].hi, Htable[6].lo = V.lo ^ Htable[2].lo; Htable[7].hi = V.hi ^ Htable[3].hi, Htable[7].lo = V.lo ^ Htable[3].lo; V = Htable[8]; Htable[9].hi = V.hi ^ Htable[1].hi, Htable[9].lo = V.lo ^ Htable[1].lo; Htable[10].hi = V.hi ^ Htable[2].hi, Htable[10].lo = V.lo ^ Htable[2].lo; Htable[11].hi = V.hi ^ Htable[3].hi, Htable[11].lo = V.lo ^ Htable[3].lo; Htable[12].hi = V.hi ^ Htable[4].hi, Htable[12].lo = V.lo ^ Htable[4].lo; Htable[13].hi = V.hi ^ Htable[5].hi, Htable[13].lo = V.lo ^ Htable[5].lo; Htable[14].hi = V.hi ^ Htable[6].hi, Htable[14].lo = V.lo ^ Htable[6].lo; Htable[15].hi = V.hi ^ Htable[7].hi, Htable[15].lo = V.lo ^ Htable[7].lo; # endif # if defined(GHASH_ASM) && (defined(__arm__) || defined(__arm)) { int j; DECLARE_IS_ENDIAN; if (IS_LITTLE_ENDIAN) for (j = 0; j < 16; ++j) { V = Htable[j]; Htable[j].hi = V.lo; Htable[j].lo = V.hi; } else for (j = 0; j < 16; ++j) { V = Htable[j]; Htable[j].hi = V.lo << 32 | V.lo >> 32; Htable[j].lo = V.hi << 32 | V.hi >> 32; } } # endif } # if !defined(GHASH_ASM) || defined(INCLUDE_C_GMULT_4BIT) static const size_t rem_4bit[16] = { PACK(0x0000), PACK(0x1C20), PACK(0x3840), PACK(0x2460), PACK(0x7080), PACK(0x6CA0), PACK(0x48C0), PACK(0x54E0), PACK(0xE100), PACK(0xFD20), PACK(0xD940), PACK(0xC560), PACK(0x9180), PACK(0x8DA0), PACK(0xA9C0), PACK(0xB5E0) }; static void gcm_gmult_4bit(u64 Xi[2], const u128 Htable[16]) { u128 Z; int cnt = 15; size_t rem, nlo, nhi; DECLARE_IS_ENDIAN; nlo = ((const u8 *)Xi)[15]; nhi = nlo >> 4; nlo &= 0xf; Z.hi = Htable[nlo].hi; Z.lo = Htable[nlo].lo; while (1) { rem = (size_t)Z.lo & 0xf; Z.lo = (Z.hi << 60) | (Z.lo >> 4); Z.hi = (Z.hi >> 4); if (sizeof(size_t) == 8) Z.hi ^= rem_4bit[rem]; else Z.hi ^= (u64)rem_4bit[rem] << 32; Z.hi ^= Htable[nhi].hi; Z.lo ^= Htable[nhi].lo; if (--cnt < 0) break; nlo = ((const u8 *)Xi)[cnt]; nhi = nlo >> 4; nlo &= 0xf; rem = (size_t)Z.lo & 0xf; Z.lo = (Z.hi << 60) | (Z.lo >> 4); Z.hi = (Z.hi >> 4); if (sizeof(size_t) == 8) Z.hi ^= rem_4bit[rem]; else Z.hi ^= (u64)rem_4bit[rem] << 32; Z.hi ^= Htable[nlo].hi; Z.lo ^= Htable[nlo].lo; } if (IS_LITTLE_ENDIAN) { # ifdef BSWAP8 Xi[0] = BSWAP8(Z.hi); Xi[1] = BSWAP8(Z.lo); # else u8 *p = (u8 *)Xi; u32 v; v = (u32)(Z.hi >> 32); PUTU32(p, v); v = (u32)(Z.hi); PUTU32(p + 4, v); v = (u32)(Z.lo >> 32); PUTU32(p + 8, v); v = (u32)(Z.lo); PUTU32(p + 12, v); # endif } else { Xi[0] = Z.hi; Xi[1] = Z.lo; } } # endif # if !defined(GHASH_ASM) || defined(INCLUDE_C_GHASH_4BIT) # if !defined(OPENSSL_SMALL_FOOTPRINT) static void gcm_ghash_4bit(u64 Xi[2], const u128 Htable[16], const u8 *inp, size_t len) { u128 Z; int cnt; size_t rem, nlo, nhi; DECLARE_IS_ENDIAN; do { cnt = 15; nlo = ((const u8 *)Xi)[15]; nlo ^= inp[15]; nhi = nlo >> 4; nlo &= 0xf; Z.hi = Htable[nlo].hi; Z.lo = Htable[nlo].lo; while (1) { rem = (size_t)Z.lo & 0xf; Z.lo = (Z.hi << 60) | (Z.lo >> 4); Z.hi = (Z.hi >> 4); if (sizeof(size_t) == 8) Z.hi ^= rem_4bit[rem]; else Z.hi ^= (u64)rem_4bit[rem] << 32; Z.hi ^= Htable[nhi].hi; Z.lo ^= Htable[nhi].lo; if (--cnt < 0) break; nlo = ((const u8 *)Xi)[cnt]; nlo ^= inp[cnt]; nhi = nlo >> 4; nlo &= 0xf; rem = (size_t)Z.lo & 0xf; Z.lo = (Z.hi << 60) | (Z.lo >> 4); Z.hi = (Z.hi >> 4); if (sizeof(size_t) == 8) Z.hi ^= rem_4bit[rem]; else Z.hi ^= (u64)rem_4bit[rem] << 32; Z.hi ^= Htable[nlo].hi; Z.lo ^= Htable[nlo].lo; } if (IS_LITTLE_ENDIAN) { # ifdef BSWAP8 Xi[0] = BSWAP8(Z.hi); Xi[1] = BSWAP8(Z.lo); # else u8 *p = (u8 *)Xi; u32 v; v = (u32)(Z.hi >> 32); PUTU32(p, v); v = (u32)(Z.hi); PUTU32(p + 4, v); v = (u32)(Z.lo >> 32); PUTU32(p + 8, v); v = (u32)(Z.lo); PUTU32(p + 12, v); # endif } else { Xi[0] = Z.hi; Xi[1] = Z.lo; } inp += 16; len -= 16; } while (len > 0); } # endif # else void gcm_gmult_4bit(u64 Xi[2], const u128 Htable[16]); void gcm_ghash_4bit(u64 Xi[2], const u128 Htable[16], const u8 *inp, size_t len); # endif # define GCM_MUL(ctx) ctx->funcs.gmult(ctx->Xi.u,ctx->Htable) # if defined(GHASH_ASM) || !defined(OPENSSL_SMALL_FOOTPRINT) # define GHASH(ctx,in,len) ctx->funcs.ghash((ctx)->Xi.u,(ctx)->Htable,in,len) # define GHASH_CHUNK (3*1024) # endif #if (defined(GHASH_ASM) || defined(OPENSSL_CPUID_OBJ)) # if !defined(I386_ONLY) && \ (defined(__i386) || defined(__i386__) || \ defined(__x86_64) || defined(__x86_64__) || \ defined(_M_IX86) || defined(_M_AMD64) || defined(_M_X64)) # define GHASH_ASM_X86_OR_64 void gcm_init_clmul(u128 Htable[16], const u64 Xi[2]); void gcm_gmult_clmul(u64 Xi[2], const u128 Htable[16]); void gcm_ghash_clmul(u64 Xi[2], const u128 Htable[16], const u8 *inp, size_t len); # if defined(__i386) || defined(__i386__) || defined(_M_IX86) # define gcm_init_avx gcm_init_clmul # define gcm_gmult_avx gcm_gmult_clmul # define gcm_ghash_avx gcm_ghash_clmul # else void gcm_init_avx(u128 Htable[16], const u64 Xi[2]); void gcm_gmult_avx(u64 Xi[2], const u128 Htable[16]); void gcm_ghash_avx(u64 Xi[2], const u128 Htable[16], const u8 *inp, size_t len); # endif # if defined(__i386) || defined(__i386__) || defined(_M_IX86) # define GHASH_ASM_X86 void gcm_gmult_4bit_mmx(u64 Xi[2], const u128 Htable[16]); void gcm_ghash_4bit_mmx(u64 Xi[2], const u128 Htable[16], const u8 *inp, size_t len); void gcm_gmult_4bit_x86(u64 Xi[2], const u128 Htable[16]); void gcm_ghash_4bit_x86(u64 Xi[2], const u128 Htable[16], const u8 *inp, size_t len); # endif # elif defined(__arm__) || defined(__arm) || defined(__aarch64__) || defined(_M_ARM64) # include "arm_arch.h" # if __ARM_MAX_ARCH__>=7 # define GHASH_ASM_ARM # define PMULL_CAPABLE (OPENSSL_armcap_P & ARMV8_PMULL) # if defined(__arm__) || defined(__arm) # define NEON_CAPABLE (OPENSSL_armcap_P & ARMV7_NEON) # endif void gcm_init_neon(u128 Htable[16], const u64 Xi[2]); void gcm_gmult_neon(u64 Xi[2], const u128 Htable[16]); void gcm_ghash_neon(u64 Xi[2], const u128 Htable[16], const u8 *inp, size_t len); void gcm_init_v8(u128 Htable[16], const u64 Xi[2]); void gcm_gmult_v8(u64 Xi[2], const u128 Htable[16]); void gcm_ghash_v8(u64 Xi[2], const u128 Htable[16], const u8 *inp, size_t len); # endif # elif defined(__sparc__) || defined(__sparc) # include "crypto/sparc_arch.h" # define GHASH_ASM_SPARC void gcm_init_vis3(u128 Htable[16], const u64 Xi[2]); void gcm_gmult_vis3(u64 Xi[2], const u128 Htable[16]); void gcm_ghash_vis3(u64 Xi[2], const u128 Htable[16], const u8 *inp, size_t len); # elif defined(OPENSSL_CPUID_OBJ) && (defined(__powerpc__) || defined(__POWERPC__) || defined(_ARCH_PPC)) # include "crypto/ppc_arch.h" # define GHASH_ASM_PPC void gcm_init_p8(u128 Htable[16], const u64 Xi[2]); void gcm_gmult_p8(u64 Xi[2], const u128 Htable[16]); void gcm_ghash_p8(u64 Xi[2], const u128 Htable[16], const u8 *inp, size_t len); # elif defined(OPENSSL_CPUID_OBJ) && defined(__riscv) && __riscv_xlen == 64 # include "crypto/riscv_arch.h" # define GHASH_ASM_RV64I void gcm_init_rv64i_zbc(u128 Htable[16], const u64 Xi[2]); void gcm_init_rv64i_zbc__zbb(u128 Htable[16], const u64 Xi[2]); void gcm_init_rv64i_zbc__zbkb(u128 Htable[16], const u64 Xi[2]); void gcm_gmult_rv64i_zbc(u64 Xi[2], const u128 Htable[16]); void gcm_gmult_rv64i_zbc__zbkb(u64 Xi[2], const u128 Htable[16]); void gcm_ghash_rv64i_zbc(u64 Xi[2], const u128 Htable[16], const u8 *inp, size_t len); void gcm_ghash_rv64i_zbc__zbkb(u64 Xi[2], const u128 Htable[16], const u8 *inp, size_t len); void gcm_init_rv64i_zvkb_zvbc(u128 Htable[16], const u64 Xi[2]); void gcm_gmult_rv64i_zvkb_zvbc(u64 Xi[2], const u128 Htable[16]); void gcm_ghash_rv64i_zvkb_zvbc(u64 Xi[2], const u128 Htable[16], const u8 *inp, size_t len); void gcm_init_rv64i_zvkg(u128 Htable[16], const u64 Xi[2]); void gcm_init_rv64i_zvkg_zvkb(u128 Htable[16], const u64 Xi[2]); void gcm_gmult_rv64i_zvkg(u64 Xi[2], const u128 Htable[16]); void gcm_ghash_rv64i_zvkg(u64 Xi[2], const u128 Htable[16], const u8 *inp, size_t len); # endif #endif static void gcm_get_funcs(struct gcm_funcs_st *ctx) { ctx->ginit = gcm_init_4bit; #if !defined(GHASH_ASM) ctx->gmult = gcm_gmult_4bit; #else ctx->gmult = NULL; #endif #if !defined(GHASH_ASM) && !defined(OPENSSL_SMALL_FOOTPRINT) ctx->ghash = gcm_ghash_4bit; #else ctx->ghash = NULL; #endif #if defined(GHASH_ASM_X86_OR_64) # if !defined(GHASH_ASM_X86) || defined(OPENSSL_IA32_SSE2) if (OPENSSL_ia32cap_P[1] & (1 << 1)) { if (((OPENSSL_ia32cap_P[1] >> 22) & 0x41) == 0x41) { ctx->ginit = gcm_init_avx; ctx->gmult = gcm_gmult_avx; ctx->ghash = gcm_ghash_avx; } else { ctx->ginit = gcm_init_clmul; ctx->gmult = gcm_gmult_clmul; ctx->ghash = gcm_ghash_clmul; } return; } # endif # if defined(GHASH_ASM_X86) # if defined(OPENSSL_IA32_SSE2) if (OPENSSL_ia32cap_P[0] & (1 << 25)) { ctx->gmult = gcm_gmult_4bit_mmx; ctx->ghash = gcm_ghash_4bit_mmx; return; } # else if (OPENSSL_ia32cap_P[0] & (1 << 23)) { ctx->gmult = gcm_gmult_4bit_mmx; ctx->ghash = gcm_ghash_4bit_mmx; return; } # endif ctx->gmult = gcm_gmult_4bit_x86; ctx->ghash = gcm_ghash_4bit_x86; return; # else ctx->gmult = gcm_gmult_4bit; ctx->ghash = gcm_ghash_4bit; return; # endif #elif defined(GHASH_ASM_ARM) ctx->gmult = gcm_gmult_4bit; ctx->ghash = gcm_ghash_4bit; # ifdef PMULL_CAPABLE if (PMULL_CAPABLE) { ctx->ginit = (gcm_init_fn)gcm_init_v8; ctx->gmult = gcm_gmult_v8; ctx->ghash = gcm_ghash_v8; } # elif defined(NEON_CAPABLE) if (NEON_CAPABLE) { ctx->ginit = gcm_init_neon; ctx->gmult = gcm_gmult_neon; ctx->ghash = gcm_ghash_neon; } # endif return; #elif defined(GHASH_ASM_SPARC) ctx->gmult = gcm_gmult_4bit; ctx->ghash = gcm_ghash_4bit; if (OPENSSL_sparcv9cap_P[0] & SPARCV9_VIS3) { ctx->ginit = gcm_init_vis3; ctx->gmult = gcm_gmult_vis3; ctx->ghash = gcm_ghash_vis3; } return; #elif defined(GHASH_ASM_PPC) if (OPENSSL_ppccap_P & PPC_CRYPTO207) { ctx->ginit = gcm_init_p8; ctx->gmult = gcm_gmult_p8; ctx->ghash = gcm_ghash_p8; } return; #elif defined(GHASH_ASM_RV64I) ctx->gmult = gcm_gmult_4bit; ctx->ghash = gcm_ghash_4bit; if (RISCV_HAS_ZVKG() && riscv_vlen() >= 128) { if (RISCV_HAS_ZVKB()) ctx->ginit = gcm_init_rv64i_zvkg_zvkb; else ctx->ginit = gcm_init_rv64i_zvkg; ctx->gmult = gcm_gmult_rv64i_zvkg; ctx->ghash = gcm_ghash_rv64i_zvkg; } else if (RISCV_HAS_ZVKB() && RISCV_HAS_ZVBC() && riscv_vlen() >= 128) { ctx->ginit = gcm_init_rv64i_zvkb_zvbc; ctx->gmult = gcm_gmult_rv64i_zvkb_zvbc; ctx->ghash = gcm_ghash_rv64i_zvkb_zvbc; } else if (RISCV_HAS_ZBC()) { if (RISCV_HAS_ZBKB()) { ctx->ginit = gcm_init_rv64i_zbc__zbkb; ctx->gmult = gcm_gmult_rv64i_zbc__zbkb; ctx->ghash = gcm_ghash_rv64i_zbc__zbkb; } else if (RISCV_HAS_ZBB()) { ctx->ginit = gcm_init_rv64i_zbc__zbb; ctx->gmult = gcm_gmult_rv64i_zbc; ctx->ghash = gcm_ghash_rv64i_zbc; } else { ctx->ginit = gcm_init_rv64i_zbc; ctx->gmult = gcm_gmult_rv64i_zbc; ctx->ghash = gcm_ghash_rv64i_zbc; } } return; #elif defined(GHASH_ASM) ctx->gmult = gcm_gmult_4bit; ctx->ghash = gcm_ghash_4bit; return; #endif } void ossl_gcm_init_4bit(u128 Htable[16], const u64 H[2]) { struct gcm_funcs_st funcs; gcm_get_funcs(&funcs); funcs.ginit(Htable, H); } void ossl_gcm_gmult_4bit(u64 Xi[2], const u128 Htable[16]) { struct gcm_funcs_st funcs; gcm_get_funcs(&funcs); funcs.gmult(Xi, Htable); } void ossl_gcm_ghash_4bit(u64 Xi[2], const u128 Htable[16], const u8 *inp, size_t len) { struct gcm_funcs_st funcs; u64 tmp[2]; size_t i; gcm_get_funcs(&funcs); if (funcs.ghash != NULL) { funcs.ghash(Xi, Htable, inp, len); } else { for (i = 0; i < len; i += 16) { memcpy(tmp, &inp[i], sizeof(tmp)); Xi[0] ^= tmp[0]; Xi[1] ^= tmp[1]; funcs.gmult(Xi, Htable); } } } void CRYPTO_gcm128_init(GCM128_CONTEXT *ctx, void *key, block128_f block) { DECLARE_IS_ENDIAN; memset(ctx, 0, sizeof(*ctx)); ctx->block = block; ctx->key = key; (*block) (ctx->H.c, ctx->H.c, key); if (IS_LITTLE_ENDIAN) { #ifdef BSWAP8 ctx->H.u[0] = BSWAP8(ctx->H.u[0]); ctx->H.u[1] = BSWAP8(ctx->H.u[1]); #else u8 *p = ctx->H.c; u64 hi, lo; hi = (u64)GETU32(p) << 32 | GETU32(p + 4); lo = (u64)GETU32(p + 8) << 32 | GETU32(p + 12); ctx->H.u[0] = hi; ctx->H.u[1] = lo; #endif } gcm_get_funcs(&ctx->funcs); ctx->funcs.ginit(ctx->Htable, ctx->H.u); } void CRYPTO_gcm128_setiv(GCM128_CONTEXT *ctx, const unsigned char *iv, size_t len) { DECLARE_IS_ENDIAN; unsigned int ctr; ctx->len.u[0] = 0; ctx->len.u[1] = 0; ctx->ares = 0; ctx->mres = 0; if (len == 12) { memcpy(ctx->Yi.c, iv, 12); ctx->Yi.c[12] = 0; ctx->Yi.c[13] = 0; ctx->Yi.c[14] = 0; ctx->Yi.c[15] = 1; ctr = 1; } else { size_t i; u64 len0 = len; ctx->Xi.u[0] = 0; ctx->Xi.u[1] = 0; while (len >= 16) { for (i = 0; i < 16; ++i) ctx->Xi.c[i] ^= iv[i]; GCM_MUL(ctx); iv += 16; len -= 16; } if (len) { for (i = 0; i < len; ++i) ctx->Xi.c[i] ^= iv[i]; GCM_MUL(ctx); } len0 <<= 3; if (IS_LITTLE_ENDIAN) { #ifdef BSWAP8 ctx->Xi.u[1] ^= BSWAP8(len0); #else ctx->Xi.c[8] ^= (u8)(len0 >> 56); ctx->Xi.c[9] ^= (u8)(len0 >> 48); ctx->Xi.c[10] ^= (u8)(len0 >> 40); ctx->Xi.c[11] ^= (u8)(len0 >> 32); ctx->Xi.c[12] ^= (u8)(len0 >> 24); ctx->Xi.c[13] ^= (u8)(len0 >> 16); ctx->Xi.c[14] ^= (u8)(len0 >> 8); ctx->Xi.c[15] ^= (u8)(len0); #endif } else { ctx->Xi.u[1] ^= len0; } GCM_MUL(ctx); if (IS_LITTLE_ENDIAN) #ifdef BSWAP4 ctr = BSWAP4(ctx->Xi.d[3]); #else ctr = GETU32(ctx->Xi.c + 12); #endif else ctr = ctx->Xi.d[3]; ctx->Yi.u[0] = ctx->Xi.u[0]; ctx->Yi.u[1] = ctx->Xi.u[1]; } ctx->Xi.u[0] = 0; ctx->Xi.u[1] = 0; (*ctx->block) (ctx->Yi.c, ctx->EK0.c, ctx->key); ++ctr; if (IS_LITTLE_ENDIAN) #ifdef BSWAP4 ctx->Yi.d[3] = BSWAP4(ctr); #else PUTU32(ctx->Yi.c + 12, ctr); #endif else ctx->Yi.d[3] = ctr; } int CRYPTO_gcm128_aad(GCM128_CONTEXT *ctx, const unsigned char *aad, size_t len) { size_t i; unsigned int n; u64 alen = ctx->len.u[0]; if (ctx->len.u[1]) return -2; alen += len; if (alen > (U64(1) << 61) || (sizeof(len) == 8 && alen < len)) return -1; ctx->len.u[0] = alen; n = ctx->ares; if (n) { while (n && len) { ctx->Xi.c[n] ^= *(aad++); --len; n = (n + 1) % 16; } if (n == 0) GCM_MUL(ctx); else { ctx->ares = n; return 0; } } #ifdef GHASH if ((i = (len & (size_t)-16))) { GHASH(ctx, aad, i); aad += i; len -= i; } #else while (len >= 16) { for (i = 0; i < 16; ++i) ctx->Xi.c[i] ^= aad[i]; GCM_MUL(ctx); aad += 16; len -= 16; } #endif if (len) { n = (unsigned int)len; for (i = 0; i < len; ++i) ctx->Xi.c[i] ^= aad[i]; } ctx->ares = n; return 0; } int CRYPTO_gcm128_encrypt(GCM128_CONTEXT *ctx, const unsigned char *in, unsigned char *out, size_t len) { DECLARE_IS_ENDIAN; unsigned int n, ctr, mres; size_t i; u64 mlen = ctx->len.u[1]; block128_f block = ctx->block; void *key = ctx->key; mlen += len; if (mlen > ((U64(1) << 36) - 32) || (sizeof(len) == 8 && mlen < len)) return -1; ctx->len.u[1] = mlen; mres = ctx->mres; if (ctx->ares) { #if defined(GHASH) && !defined(OPENSSL_SMALL_FOOTPRINT) if (len == 0) { GCM_MUL(ctx); ctx->ares = 0; return 0; } memcpy(ctx->Xn, ctx->Xi.c, sizeof(ctx->Xi)); ctx->Xi.u[0] = 0; ctx->Xi.u[1] = 0; mres = sizeof(ctx->Xi); #else GCM_MUL(ctx); #endif ctx->ares = 0; } if (IS_LITTLE_ENDIAN) #ifdef BSWAP4 ctr = BSWAP4(ctx->Yi.d[3]); #else ctr = GETU32(ctx->Yi.c + 12); #endif else ctr = ctx->Yi.d[3]; n = mres % 16; #if !defined(OPENSSL_SMALL_FOOTPRINT) if (16 % sizeof(size_t) == 0) { do { if (n) { # if defined(GHASH) while (n && len) { ctx->Xn[mres++] = *(out++) = *(in++) ^ ctx->EKi.c[n]; --len; n = (n + 1) % 16; } if (n == 0) { GHASH(ctx, ctx->Xn, mres); mres = 0; } else { ctx->mres = mres; return 0; } # else while (n && len) { ctx->Xi.c[n] ^= *(out++) = *(in++) ^ ctx->EKi.c[n]; --len; n = (n + 1) % 16; } if (n == 0) { GCM_MUL(ctx); mres = 0; } else { ctx->mres = n; return 0; } # endif } # if defined(STRICT_ALIGNMENT) if (((size_t)in | (size_t)out) % sizeof(size_t) != 0) break; # endif # if defined(GHASH) if (len >= 16 && mres) { GHASH(ctx, ctx->Xn, mres); mres = 0; } # if defined(GHASH_CHUNK) while (len >= GHASH_CHUNK) { size_t j = GHASH_CHUNK; while (j) { size_t_aX *out_t = (size_t_aX *)out; const size_t_aX *in_t = (const size_t_aX *)in; (*block) (ctx->Yi.c, ctx->EKi.c, key); ++ctr; if (IS_LITTLE_ENDIAN) # ifdef BSWAP4 ctx->Yi.d[3] = BSWAP4(ctr); # else PUTU32(ctx->Yi.c + 12, ctr); # endif else ctx->Yi.d[3] = ctr; for (i = 0; i < 16 / sizeof(size_t); ++i) out_t[i] = in_t[i] ^ ctx->EKi.t[i]; out += 16; in += 16; j -= 16; } GHASH(ctx, out - GHASH_CHUNK, GHASH_CHUNK); len -= GHASH_CHUNK; } # endif if ((i = (len & (size_t)-16))) { size_t j = i; while (len >= 16) { size_t_aX *out_t = (size_t_aX *)out; const size_t_aX *in_t = (const size_t_aX *)in; (*block) (ctx->Yi.c, ctx->EKi.c, key); ++ctr; if (IS_LITTLE_ENDIAN) # ifdef BSWAP4 ctx->Yi.d[3] = BSWAP4(ctr); # else PUTU32(ctx->Yi.c + 12, ctr); # endif else ctx->Yi.d[3] = ctr; for (i = 0; i < 16 / sizeof(size_t); ++i) out_t[i] = in_t[i] ^ ctx->EKi.t[i]; out += 16; in += 16; len -= 16; } GHASH(ctx, out - j, j); } # else while (len >= 16) { size_t *out_t = (size_t *)out; const size_t *in_t = (const size_t *)in; (*block) (ctx->Yi.c, ctx->EKi.c, key); ++ctr; if (IS_LITTLE_ENDIAN) # ifdef BSWAP4 ctx->Yi.d[3] = BSWAP4(ctr); # else PUTU32(ctx->Yi.c + 12, ctr); # endif else ctx->Yi.d[3] = ctr; for (i = 0; i < 16 / sizeof(size_t); ++i) ctx->Xi.t[i] ^= out_t[i] = in_t[i] ^ ctx->EKi.t[i]; GCM_MUL(ctx); out += 16; in += 16; len -= 16; } # endif if (len) { (*block) (ctx->Yi.c, ctx->EKi.c, key); ++ctr; if (IS_LITTLE_ENDIAN) # ifdef BSWAP4 ctx->Yi.d[3] = BSWAP4(ctr); # else PUTU32(ctx->Yi.c + 12, ctr); # endif else ctx->Yi.d[3] = ctr; # if defined(GHASH) while (len--) { ctx->Xn[mres++] = out[n] = in[n] ^ ctx->EKi.c[n]; ++n; } # else while (len--) { ctx->Xi.c[n] ^= out[n] = in[n] ^ ctx->EKi.c[n]; ++n; } mres = n; # endif } ctx->mres = mres; return 0; } while (0); } #endif for (i = 0; i < len; ++i) { if (n == 0) { (*block) (ctx->Yi.c, ctx->EKi.c, key); ++ctr; if (IS_LITTLE_ENDIAN) #ifdef BSWAP4 ctx->Yi.d[3] = BSWAP4(ctr); #else PUTU32(ctx->Yi.c + 12, ctr); #endif else ctx->Yi.d[3] = ctr; } #if defined(GHASH) && !defined(OPENSSL_SMALL_FOOTPRINT) ctx->Xn[mres++] = out[i] = in[i] ^ ctx->EKi.c[n]; n = (n + 1) % 16; if (mres == sizeof(ctx->Xn)) { GHASH(ctx,ctx->Xn,sizeof(ctx->Xn)); mres = 0; } #else ctx->Xi.c[n] ^= out[i] = in[i] ^ ctx->EKi.c[n]; mres = n = (n + 1) % 16; if (n == 0) GCM_MUL(ctx); #endif } ctx->mres = mres; return 0; } int CRYPTO_gcm128_decrypt(GCM128_CONTEXT *ctx, const unsigned char *in, unsigned char *out, size_t len) { DECLARE_IS_ENDIAN; unsigned int n, ctr, mres; size_t i; u64 mlen = ctx->len.u[1]; block128_f block = ctx->block; void *key = ctx->key; mlen += len; if (mlen > ((U64(1) << 36) - 32) || (sizeof(len) == 8 && mlen < len)) return -1; ctx->len.u[1] = mlen; mres = ctx->mres; if (ctx->ares) { #if defined(GHASH) && !defined(OPENSSL_SMALL_FOOTPRINT) if (len == 0) { GCM_MUL(ctx); ctx->ares = 0; return 0; } memcpy(ctx->Xn, ctx->Xi.c, sizeof(ctx->Xi)); ctx->Xi.u[0] = 0; ctx->Xi.u[1] = 0; mres = sizeof(ctx->Xi); #else GCM_MUL(ctx); #endif ctx->ares = 0; } if (IS_LITTLE_ENDIAN) #ifdef BSWAP4 ctr = BSWAP4(ctx->Yi.d[3]); #else ctr = GETU32(ctx->Yi.c + 12); #endif else ctr = ctx->Yi.d[3]; n = mres % 16; #if !defined(OPENSSL_SMALL_FOOTPRINT) if (16 % sizeof(size_t) == 0) { do { if (n) { # if defined(GHASH) while (n && len) { *(out++) = (ctx->Xn[mres++] = *(in++)) ^ ctx->EKi.c[n]; --len; n = (n + 1) % 16; } if (n == 0) { GHASH(ctx, ctx->Xn, mres); mres = 0; } else { ctx->mres = mres; return 0; } # else while (n && len) { u8 c = *(in++); *(out++) = c ^ ctx->EKi.c[n]; ctx->Xi.c[n] ^= c; --len; n = (n + 1) % 16; } if (n == 0) { GCM_MUL(ctx); mres = 0; } else { ctx->mres = n; return 0; } # endif } # if defined(STRICT_ALIGNMENT) if (((size_t)in | (size_t)out) % sizeof(size_t) != 0) break; # endif # if defined(GHASH) if (len >= 16 && mres) { GHASH(ctx, ctx->Xn, mres); mres = 0; } # if defined(GHASH_CHUNK) while (len >= GHASH_CHUNK) { size_t j = GHASH_CHUNK; GHASH(ctx, in, GHASH_CHUNK); while (j) { size_t_aX *out_t = (size_t_aX *)out; const size_t_aX *in_t = (const size_t_aX *)in; (*block) (ctx->Yi.c, ctx->EKi.c, key); ++ctr; if (IS_LITTLE_ENDIAN) # ifdef BSWAP4 ctx->Yi.d[3] = BSWAP4(ctr); # else PUTU32(ctx->Yi.c + 12, ctr); # endif else ctx->Yi.d[3] = ctr; for (i = 0; i < 16 / sizeof(size_t); ++i) out_t[i] = in_t[i] ^ ctx->EKi.t[i]; out += 16; in += 16; j -= 16; } len -= GHASH_CHUNK; } # endif if ((i = (len & (size_t)-16))) { GHASH(ctx, in, i); while (len >= 16) { size_t_aX *out_t = (size_t_aX *)out; const size_t_aX *in_t = (const size_t_aX *)in; (*block) (ctx->Yi.c, ctx->EKi.c, key); ++ctr; if (IS_LITTLE_ENDIAN) # ifdef BSWAP4 ctx->Yi.d[3] = BSWAP4(ctr); # else PUTU32(ctx->Yi.c + 12, ctr); # endif else ctx->Yi.d[3] = ctr; for (i = 0; i < 16 / sizeof(size_t); ++i) out_t[i] = in_t[i] ^ ctx->EKi.t[i]; out += 16; in += 16; len -= 16; } } # else while (len >= 16) { size_t *out_t = (size_t *)out; const size_t *in_t = (const size_t *)in; (*block) (ctx->Yi.c, ctx->EKi.c, key); ++ctr; if (IS_LITTLE_ENDIAN) # ifdef BSWAP4 ctx->Yi.d[3] = BSWAP4(ctr); # else PUTU32(ctx->Yi.c + 12, ctr); # endif else ctx->Yi.d[3] = ctr; for (i = 0; i < 16 / sizeof(size_t); ++i) { size_t c = in_t[i]; out_t[i] = c ^ ctx->EKi.t[i]; ctx->Xi.t[i] ^= c; } GCM_MUL(ctx); out += 16; in += 16; len -= 16; } # endif if (len) { (*block) (ctx->Yi.c, ctx->EKi.c, key); ++ctr; if (IS_LITTLE_ENDIAN) # ifdef BSWAP4 ctx->Yi.d[3] = BSWAP4(ctr); # else PUTU32(ctx->Yi.c + 12, ctr); # endif else ctx->Yi.d[3] = ctr; # if defined(GHASH) while (len--) { out[n] = (ctx->Xn[mres++] = in[n]) ^ ctx->EKi.c[n]; ++n; } # else while (len--) { u8 c = in[n]; ctx->Xi.c[n] ^= c; out[n] = c ^ ctx->EKi.c[n]; ++n; } mres = n; # endif } ctx->mres = mres; return 0; } while (0); } #endif for (i = 0; i < len; ++i) { u8 c; if (n == 0) { (*block) (ctx->Yi.c, ctx->EKi.c, key); ++ctr; if (IS_LITTLE_ENDIAN) #ifdef BSWAP4 ctx->Yi.d[3] = BSWAP4(ctr); #else PUTU32(ctx->Yi.c + 12, ctr); #endif else ctx->Yi.d[3] = ctr; } #if defined(GHASH) && !defined(OPENSSL_SMALL_FOOTPRINT) out[i] = (ctx->Xn[mres++] = c = in[i]) ^ ctx->EKi.c[n]; n = (n + 1) % 16; if (mres == sizeof(ctx->Xn)) { GHASH(ctx,ctx->Xn,sizeof(ctx->Xn)); mres = 0; } #else c = in[i]; out[i] = c ^ ctx->EKi.c[n]; ctx->Xi.c[n] ^= c; mres = n = (n + 1) % 16; if (n == 0) GCM_MUL(ctx); #endif } ctx->mres = mres; return 0; } int CRYPTO_gcm128_encrypt_ctr32(GCM128_CONTEXT *ctx, const unsigned char *in, unsigned char *out, size_t len, ctr128_f stream) { #if defined(OPENSSL_SMALL_FOOTPRINT) return CRYPTO_gcm128_encrypt(ctx, in, out, len); #else DECLARE_IS_ENDIAN; unsigned int n, ctr, mres; size_t i; u64 mlen = ctx->len.u[1]; void *key = ctx->key; mlen += len; if (mlen > ((U64(1) << 36) - 32) || (sizeof(len) == 8 && mlen < len)) return -1; ctx->len.u[1] = mlen; mres = ctx->mres; if (ctx->ares) { #if defined(GHASH) if (len == 0) { GCM_MUL(ctx); ctx->ares = 0; return 0; } memcpy(ctx->Xn, ctx->Xi.c, sizeof(ctx->Xi)); ctx->Xi.u[0] = 0; ctx->Xi.u[1] = 0; mres = sizeof(ctx->Xi); #else GCM_MUL(ctx); #endif ctx->ares = 0; } if (IS_LITTLE_ENDIAN) # ifdef BSWAP4 ctr = BSWAP4(ctx->Yi.d[3]); # else ctr = GETU32(ctx->Yi.c + 12); # endif else ctr = ctx->Yi.d[3]; n = mres % 16; if (n) { # if defined(GHASH) while (n && len) { ctx->Xn[mres++] = *(out++) = *(in++) ^ ctx->EKi.c[n]; --len; n = (n + 1) % 16; } if (n == 0) { GHASH(ctx, ctx->Xn, mres); mres = 0; } else { ctx->mres = mres; return 0; } # else while (n && len) { ctx->Xi.c[n] ^= *(out++) = *(in++) ^ ctx->EKi.c[n]; --len; n = (n + 1) % 16; } if (n == 0) { GCM_MUL(ctx); mres = 0; } else { ctx->mres = n; return 0; } # endif } # if defined(GHASH) if (len >= 16 && mres) { GHASH(ctx, ctx->Xn, mres); mres = 0; } # if defined(GHASH_CHUNK) while (len >= GHASH_CHUNK) { (*stream) (in, out, GHASH_CHUNK / 16, key, ctx->Yi.c); ctr += GHASH_CHUNK / 16; if (IS_LITTLE_ENDIAN) # ifdef BSWAP4 ctx->Yi.d[3] = BSWAP4(ctr); # else PUTU32(ctx->Yi.c + 12, ctr); # endif else ctx->Yi.d[3] = ctr; GHASH(ctx, out, GHASH_CHUNK); out += GHASH_CHUNK; in += GHASH_CHUNK; len -= GHASH_CHUNK; } # endif # endif if ((i = (len & (size_t)-16))) { size_t j = i / 16; (*stream) (in, out, j, key, ctx->Yi.c); ctr += (unsigned int)j; if (IS_LITTLE_ENDIAN) # ifdef BSWAP4 ctx->Yi.d[3] = BSWAP4(ctr); # else PUTU32(ctx->Yi.c + 12, ctr); # endif else ctx->Yi.d[3] = ctr; in += i; len -= i; # if defined(GHASH) GHASH(ctx, out, i); out += i; # else while (j--) { for (i = 0; i < 16; ++i) ctx->Xi.c[i] ^= out[i]; GCM_MUL(ctx); out += 16; } # endif } if (len) { (*ctx->block) (ctx->Yi.c, ctx->EKi.c, key); ++ctr; if (IS_LITTLE_ENDIAN) # ifdef BSWAP4 ctx->Yi.d[3] = BSWAP4(ctr); # else PUTU32(ctx->Yi.c + 12, ctr); # endif else ctx->Yi.d[3] = ctr; while (len--) { # if defined(GHASH) ctx->Xn[mres++] = out[n] = in[n] ^ ctx->EKi.c[n]; # else ctx->Xi.c[mres++] ^= out[n] = in[n] ^ ctx->EKi.c[n]; # endif ++n; } } ctx->mres = mres; return 0; #endif } int CRYPTO_gcm128_decrypt_ctr32(GCM128_CONTEXT *ctx, const unsigned char *in, unsigned char *out, size_t len, ctr128_f stream) { #if defined(OPENSSL_SMALL_FOOTPRINT) return CRYPTO_gcm128_decrypt(ctx, in, out, len); #else DECLARE_IS_ENDIAN; unsigned int n, ctr, mres; size_t i; u64 mlen = ctx->len.u[1]; void *key = ctx->key; mlen += len; if (mlen > ((U64(1) << 36) - 32) || (sizeof(len) == 8 && mlen < len)) return -1; ctx->len.u[1] = mlen; mres = ctx->mres; if (ctx->ares) { # if defined(GHASH) if (len == 0) { GCM_MUL(ctx); ctx->ares = 0; return 0; } memcpy(ctx->Xn, ctx->Xi.c, sizeof(ctx->Xi)); ctx->Xi.u[0] = 0; ctx->Xi.u[1] = 0; mres = sizeof(ctx->Xi); # else GCM_MUL(ctx); # endif ctx->ares = 0; } if (IS_LITTLE_ENDIAN) # ifdef BSWAP4 ctr = BSWAP4(ctx->Yi.d[3]); # else ctr = GETU32(ctx->Yi.c + 12); # endif else ctr = ctx->Yi.d[3]; n = mres % 16; if (n) { # if defined(GHASH) while (n && len) { *(out++) = (ctx->Xn[mres++] = *(in++)) ^ ctx->EKi.c[n]; --len; n = (n + 1) % 16; } if (n == 0) { GHASH(ctx, ctx->Xn, mres); mres = 0; } else { ctx->mres = mres; return 0; } # else while (n && len) { u8 c = *(in++); *(out++) = c ^ ctx->EKi.c[n]; ctx->Xi.c[n] ^= c; --len; n = (n + 1) % 16; } if (n == 0) { GCM_MUL(ctx); mres = 0; } else { ctx->mres = n; return 0; } # endif } # if defined(GHASH) if (len >= 16 && mres) { GHASH(ctx, ctx->Xn, mres); mres = 0; } # if defined(GHASH_CHUNK) while (len >= GHASH_CHUNK) { GHASH(ctx, in, GHASH_CHUNK); (*stream) (in, out, GHASH_CHUNK / 16, key, ctx->Yi.c); ctr += GHASH_CHUNK / 16; if (IS_LITTLE_ENDIAN) # ifdef BSWAP4 ctx->Yi.d[3] = BSWAP4(ctr); # else PUTU32(ctx->Yi.c + 12, ctr); # endif else ctx->Yi.d[3] = ctr; out += GHASH_CHUNK; in += GHASH_CHUNK; len -= GHASH_CHUNK; } # endif # endif if ((i = (len & (size_t)-16))) { size_t j = i / 16; # if defined(GHASH) GHASH(ctx, in, i); # else while (j--) { size_t k; for (k = 0; k < 16; ++k) ctx->Xi.c[k] ^= in[k]; GCM_MUL(ctx); in += 16; } j = i / 16; in -= i; # endif (*stream) (in, out, j, key, ctx->Yi.c); ctr += (unsigned int)j; if (IS_LITTLE_ENDIAN) # ifdef BSWAP4 ctx->Yi.d[3] = BSWAP4(ctr); # else PUTU32(ctx->Yi.c + 12, ctr); # endif else ctx->Yi.d[3] = ctr; out += i; in += i; len -= i; } if (len) { (*ctx->block) (ctx->Yi.c, ctx->EKi.c, key); ++ctr; if (IS_LITTLE_ENDIAN) # ifdef BSWAP4 ctx->Yi.d[3] = BSWAP4(ctr); # else PUTU32(ctx->Yi.c + 12, ctr); # endif else ctx->Yi.d[3] = ctr; while (len--) { # if defined(GHASH) out[n] = (ctx->Xn[mres++] = in[n]) ^ ctx->EKi.c[n]; # else u8 c = in[n]; ctx->Xi.c[mres++] ^= c; out[n] = c ^ ctx->EKi.c[n]; # endif ++n; } } ctx->mres = mres; return 0; #endif } int CRYPTO_gcm128_finish(GCM128_CONTEXT *ctx, const unsigned char *tag, size_t len) { DECLARE_IS_ENDIAN; u64 alen = ctx->len.u[0] << 3; u64 clen = ctx->len.u[1] << 3; #if defined(GHASH) && !defined(OPENSSL_SMALL_FOOTPRINT) u128 bitlen; unsigned int mres = ctx->mres; if (mres) { unsigned blocks = (mres + 15) & -16; memset(ctx->Xn + mres, 0, blocks - mres); mres = blocks; if (mres == sizeof(ctx->Xn)) { GHASH(ctx, ctx->Xn, mres); mres = 0; } } else if (ctx->ares) { GCM_MUL(ctx); } #else if (ctx->mres || ctx->ares) GCM_MUL(ctx); #endif if (IS_LITTLE_ENDIAN) { #ifdef BSWAP8 alen = BSWAP8(alen); clen = BSWAP8(clen); #else u8 *p = ctx->len.c; ctx->len.u[0] = alen; ctx->len.u[1] = clen; alen = (u64)GETU32(p) << 32 | GETU32(p + 4); clen = (u64)GETU32(p + 8) << 32 | GETU32(p + 12); #endif } #if defined(GHASH) && !defined(OPENSSL_SMALL_FOOTPRINT) bitlen.hi = alen; bitlen.lo = clen; memcpy(ctx->Xn + mres, &bitlen, sizeof(bitlen)); mres += sizeof(bitlen); GHASH(ctx, ctx->Xn, mres); #else ctx->Xi.u[0] ^= alen; ctx->Xi.u[1] ^= clen; GCM_MUL(ctx); #endif ctx->Xi.u[0] ^= ctx->EK0.u[0]; ctx->Xi.u[1] ^= ctx->EK0.u[1]; if (tag && len <= sizeof(ctx->Xi)) return CRYPTO_memcmp(ctx->Xi.c, tag, len); else return -1; } void CRYPTO_gcm128_tag(GCM128_CONTEXT *ctx, unsigned char *tag, size_t len) { CRYPTO_gcm128_finish(ctx, NULL, 0); memcpy(tag, ctx->Xi.c, len <= sizeof(ctx->Xi.c) ? len : sizeof(ctx->Xi.c)); } GCM128_CONTEXT *CRYPTO_gcm128_new(void *key, block128_f block) { GCM128_CONTEXT *ret; if ((ret = OPENSSL_malloc(sizeof(*ret))) != NULL) CRYPTO_gcm128_init(ret, key, block); return ret; } void CRYPTO_gcm128_release(GCM128_CONTEXT *ctx) { OPENSSL_clear_free(ctx, sizeof(*ctx)); }
modes
openssl/crypto/modes/gcm128.c
openssl
#include <string.h> #include <stdlib.h> #include <openssl/crypto.h> #include <openssl/evp.h> #include <openssl/core_names.h> #include <openssl/params.h> #include "internal/endian.h" #include "crypto/modes.h" #include "crypto/siv.h" #ifndef OPENSSL_NO_SIV __owur static ossl_inline uint32_t rotl8(uint32_t x) { return (x << 8) | (x >> 24); } __owur static ossl_inline uint32_t rotr8(uint32_t x) { return (x >> 8) | (x << 24); } __owur static ossl_inline uint64_t byteswap8(uint64_t x) { uint32_t high = (uint32_t)(x >> 32); uint32_t low = (uint32_t)x; high = (rotl8(high) & 0x00ff00ff) | (rotr8(high) & 0xff00ff00); low = (rotl8(low) & 0x00ff00ff) | (rotr8(low) & 0xff00ff00); return ((uint64_t)low) << 32 | (uint64_t)high; } __owur static ossl_inline uint64_t siv128_getword(SIV_BLOCK const *b, size_t i) { DECLARE_IS_ENDIAN; if (IS_LITTLE_ENDIAN) return byteswap8(b->word[i]); return b->word[i]; } static ossl_inline void siv128_putword(SIV_BLOCK *b, size_t i, uint64_t x) { DECLARE_IS_ENDIAN; if (IS_LITTLE_ENDIAN) b->word[i] = byteswap8(x); else b->word[i] = x; } static ossl_inline void siv128_xorblock(SIV_BLOCK *x, SIV_BLOCK const *y) { x->word[0] ^= y->word[0]; x->word[1] ^= y->word[1]; } static ossl_inline void siv128_dbl(SIV_BLOCK *b) { uint64_t high = siv128_getword(b, 0); uint64_t low = siv128_getword(b, 1); uint64_t high_carry = high & (((uint64_t)1) << 63); uint64_t low_carry = low & (((uint64_t)1) << 63); int64_t low_mask = -((int64_t)(high_carry >> 63)) & 0x87; uint64_t high_mask = low_carry >> 63; high = (high << 1) | high_mask; low = (low << 1) ^ (uint64_t)low_mask; siv128_putword(b, 0, high); siv128_putword(b, 1, low); } __owur static ossl_inline int siv128_do_s2v_p(SIV128_CONTEXT *ctx, SIV_BLOCK *out, unsigned char const* in, size_t len) { SIV_BLOCK t; size_t out_len = sizeof(out->byte); EVP_MAC_CTX *mac_ctx; int ret = 0; mac_ctx = EVP_MAC_CTX_dup(ctx->mac_ctx_init); if (mac_ctx == NULL) return 0; if (len >= SIV_LEN) { if (!EVP_MAC_update(mac_ctx, in, len - SIV_LEN)) goto err; memcpy(&t, in + (len-SIV_LEN), SIV_LEN); siv128_xorblock(&t, &ctx->d); if (!EVP_MAC_update(mac_ctx, t.byte, SIV_LEN)) goto err; } else { memset(&t, 0, sizeof(t)); memcpy(&t, in, len); t.byte[len] = 0x80; siv128_dbl(&ctx->d); siv128_xorblock(&t, &ctx->d); if (!EVP_MAC_update(mac_ctx, t.byte, SIV_LEN)) goto err; } if (!EVP_MAC_final(mac_ctx, out->byte, &out_len, sizeof(out->byte)) || out_len != SIV_LEN) goto err; ret = 1; err: EVP_MAC_CTX_free(mac_ctx); return ret; } __owur static ossl_inline int siv128_do_encrypt(EVP_CIPHER_CTX *ctx, unsigned char *out, unsigned char const *in, size_t len, SIV_BLOCK *icv) { int out_len = (int)len; if (!EVP_CipherInit_ex(ctx, NULL, NULL, NULL, icv->byte, 1)) return 0; return EVP_EncryptUpdate(ctx, out, &out_len, in, out_len); } SIV128_CONTEXT *ossl_siv128_new(const unsigned char *key, int klen, EVP_CIPHER *cbc, EVP_CIPHER *ctr, OSSL_LIB_CTX *libctx, const char *propq) { SIV128_CONTEXT *ctx; int ret; if ((ctx = OPENSSL_malloc(sizeof(*ctx))) != NULL) { ret = ossl_siv128_init(ctx, key, klen, cbc, ctr, libctx, propq); if (ret) return ctx; OPENSSL_free(ctx); } return NULL; } int ossl_siv128_init(SIV128_CONTEXT *ctx, const unsigned char *key, int klen, const EVP_CIPHER *cbc, const EVP_CIPHER *ctr, OSSL_LIB_CTX *libctx, const char *propq) { static const unsigned char zero[SIV_LEN] = { 0 }; size_t out_len = SIV_LEN; EVP_MAC_CTX *mac_ctx = NULL; OSSL_PARAM params[3]; const char *cbc_name; if (ctx == NULL) return 0; memset(&ctx->d, 0, sizeof(ctx->d)); EVP_CIPHER_CTX_free(ctx->cipher_ctx); EVP_MAC_CTX_free(ctx->mac_ctx_init); EVP_MAC_free(ctx->mac); ctx->mac = NULL; ctx->cipher_ctx = NULL; ctx->mac_ctx_init = NULL; if (key == NULL || cbc == NULL || ctr == NULL) return 0; cbc_name = EVP_CIPHER_get0_name(cbc); params[0] = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_CIPHER, (char *)cbc_name, 0); params[1] = OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY, (void *)key, klen); params[2] = OSSL_PARAM_construct_end(); if ((ctx->cipher_ctx = EVP_CIPHER_CTX_new()) == NULL || (ctx->mac = EVP_MAC_fetch(libctx, OSSL_MAC_NAME_CMAC, propq)) == NULL || (ctx->mac_ctx_init = EVP_MAC_CTX_new(ctx->mac)) == NULL || !EVP_MAC_CTX_set_params(ctx->mac_ctx_init, params) || !EVP_EncryptInit_ex(ctx->cipher_ctx, ctr, NULL, key + klen, NULL) || (mac_ctx = EVP_MAC_CTX_dup(ctx->mac_ctx_init)) == NULL || !EVP_MAC_update(mac_ctx, zero, sizeof(zero)) || !EVP_MAC_final(mac_ctx, ctx->d.byte, &out_len, sizeof(ctx->d.byte))) { EVP_CIPHER_CTX_free(ctx->cipher_ctx); EVP_MAC_CTX_free(ctx->mac_ctx_init); EVP_MAC_CTX_free(mac_ctx); EVP_MAC_free(ctx->mac); return 0; } EVP_MAC_CTX_free(mac_ctx); ctx->final_ret = -1; ctx->crypto_ok = 1; return 1; } int ossl_siv128_copy_ctx(SIV128_CONTEXT *dest, SIV128_CONTEXT *src) { memcpy(&dest->d, &src->d, sizeof(src->d)); if (dest->cipher_ctx == NULL) { dest->cipher_ctx = EVP_CIPHER_CTX_new(); if (dest->cipher_ctx == NULL) return 0; } if (!EVP_CIPHER_CTX_copy(dest->cipher_ctx, src->cipher_ctx)) return 0; EVP_MAC_CTX_free(dest->mac_ctx_init); dest->mac_ctx_init = EVP_MAC_CTX_dup(src->mac_ctx_init); if (dest->mac_ctx_init == NULL) return 0; dest->mac = src->mac; if (dest->mac != NULL) EVP_MAC_up_ref(dest->mac); return 1; } int ossl_siv128_aad(SIV128_CONTEXT *ctx, const unsigned char *aad, size_t len) { SIV_BLOCK mac_out; size_t out_len = SIV_LEN; EVP_MAC_CTX *mac_ctx; siv128_dbl(&ctx->d); if ((mac_ctx = EVP_MAC_CTX_dup(ctx->mac_ctx_init)) == NULL || !EVP_MAC_update(mac_ctx, aad, len) || !EVP_MAC_final(mac_ctx, mac_out.byte, &out_len, sizeof(mac_out.byte)) || out_len != SIV_LEN) { EVP_MAC_CTX_free(mac_ctx); return 0; } EVP_MAC_CTX_free(mac_ctx); siv128_xorblock(&ctx->d, &mac_out); return 1; } int ossl_siv128_encrypt(SIV128_CONTEXT *ctx, const unsigned char *in, unsigned char *out, size_t len) { SIV_BLOCK q; if (ctx->crypto_ok == 0) return 0; ctx->crypto_ok--; if (!siv128_do_s2v_p(ctx, &q, in, len)) return 0; memcpy(ctx->tag.byte, &q, SIV_LEN); q.byte[8] &= 0x7f; q.byte[12] &= 0x7f; if (!siv128_do_encrypt(ctx->cipher_ctx, out, in, len, &q)) return 0; ctx->final_ret = 0; return len; } int ossl_siv128_decrypt(SIV128_CONTEXT *ctx, const unsigned char *in, unsigned char *out, size_t len) { unsigned char* p; SIV_BLOCK t, q; int i; if (ctx->crypto_ok == 0) return 0; ctx->crypto_ok--; memcpy(&q, ctx->tag.byte, SIV_LEN); q.byte[8] &= 0x7f; q.byte[12] &= 0x7f; if (!siv128_do_encrypt(ctx->cipher_ctx, out, in, len, &q) || !siv128_do_s2v_p(ctx, &t, out, len)) return 0; p = ctx->tag.byte; for (i = 0; i < SIV_LEN; i++) t.byte[i] ^= p[i]; if ((t.word[0] | t.word[1]) != 0) { OPENSSL_cleanse(out, len); return 0; } ctx->final_ret = 0; return len; } int ossl_siv128_finish(SIV128_CONTEXT *ctx) { return ctx->final_ret; } int ossl_siv128_set_tag(SIV128_CONTEXT *ctx, const unsigned char *tag, size_t len) { if (len != SIV_LEN) return 0; memcpy(ctx->tag.byte, tag, len); return 1; } int ossl_siv128_get_tag(SIV128_CONTEXT *ctx, unsigned char *tag, size_t len) { if (len != SIV_LEN) return 0; memcpy(tag, ctx->tag.byte, len); return 1; } int ossl_siv128_cleanup(SIV128_CONTEXT *ctx) { if (ctx != NULL) { EVP_CIPHER_CTX_free(ctx->cipher_ctx); ctx->cipher_ctx = NULL; EVP_MAC_CTX_free(ctx->mac_ctx_init); ctx->mac_ctx_init = NULL; EVP_MAC_free(ctx->mac); ctx->mac = NULL; OPENSSL_cleanse(&ctx->d, sizeof(ctx->d)); OPENSSL_cleanse(&ctx->tag, sizeof(ctx->tag)); ctx->final_ret = -1; ctx->crypto_ok = 1; } return 1; } int ossl_siv128_speed(SIV128_CONTEXT *ctx, int arg) { ctx->crypto_ok = (arg == 1) ? -1 : 1; return 1; } #endif
modes
openssl/crypto/modes/siv128.c
openssl
#include <string.h> #include <openssl/crypto.h> #include <openssl/err.h> #include "crypto/modes.h" #ifndef OPENSSL_NO_OCB static u32 ocb_ntz(u64 n) { u32 cnt = 0; while (!(n & 1)) { n >>= 1; cnt++; } return cnt; } static void ocb_block_lshift(const unsigned char *in, size_t shift, unsigned char *out) { int i; unsigned char carry = 0, carry_next; for (i = 15; i >= 0; i--) { carry_next = in[i] >> (8 - shift); out[i] = (in[i] << shift) | carry; carry = carry_next; } } static void ocb_double(OCB_BLOCK *in, OCB_BLOCK *out) { unsigned char mask; mask = in->c[0] & 0x80; mask >>= 7; mask = (0 - mask) & 0x87; ocb_block_lshift(in->c, 1, out->c); out->c[15] ^= mask; } static void ocb_block_xor(const unsigned char *in1, const unsigned char *in2, size_t len, unsigned char *out) { size_t i; for (i = 0; i < len; i++) { out[i] = in1[i] ^ in2[i]; } } static OCB_BLOCK *ocb_lookup_l(OCB128_CONTEXT *ctx, size_t idx) { size_t l_index = ctx->l_index; if (idx <= l_index) { return ctx->l + idx; } if (idx >= ctx->max_l_index) { void *tmp_ptr; ctx->max_l_index += (idx - ctx->max_l_index + 4) & ~3; tmp_ptr = OPENSSL_realloc(ctx->l, ctx->max_l_index * sizeof(OCB_BLOCK)); if (tmp_ptr == NULL) return NULL; ctx->l = tmp_ptr; } while (l_index < idx) { ocb_double(ctx->l + l_index, ctx->l + l_index + 1); l_index++; } ctx->l_index = l_index; return ctx->l + idx; } OCB128_CONTEXT *CRYPTO_ocb128_new(void *keyenc, void *keydec, block128_f encrypt, block128_f decrypt, ocb128_f stream) { OCB128_CONTEXT *octx; int ret; if ((octx = OPENSSL_malloc(sizeof(*octx))) != NULL) { ret = CRYPTO_ocb128_init(octx, keyenc, keydec, encrypt, decrypt, stream); if (ret) return octx; OPENSSL_free(octx); } return NULL; } int CRYPTO_ocb128_init(OCB128_CONTEXT *ctx, void *keyenc, void *keydec, block128_f encrypt, block128_f decrypt, ocb128_f stream) { memset(ctx, 0, sizeof(*ctx)); ctx->l_index = 0; ctx->max_l_index = 5; if ((ctx->l = OPENSSL_malloc(ctx->max_l_index * 16)) == NULL) return 0; ctx->encrypt = encrypt; ctx->decrypt = decrypt; ctx->stream = stream; ctx->keyenc = keyenc; ctx->keydec = keydec; ctx->encrypt(ctx->l_star.c, ctx->l_star.c, ctx->keyenc); ocb_double(&ctx->l_star, &ctx->l_dollar); ocb_double(&ctx->l_dollar, ctx->l); ocb_double(ctx->l, ctx->l+1); ocb_double(ctx->l+1, ctx->l+2); ocb_double(ctx->l+2, ctx->l+3); ocb_double(ctx->l+3, ctx->l+4); ctx->l_index = 4; return 1; } int CRYPTO_ocb128_copy_ctx(OCB128_CONTEXT *dest, OCB128_CONTEXT *src, void *keyenc, void *keydec) { memcpy(dest, src, sizeof(OCB128_CONTEXT)); if (keyenc) dest->keyenc = keyenc; if (keydec) dest->keydec = keydec; if (src->l) { if ((dest->l = OPENSSL_malloc(src->max_l_index * 16)) == NULL) return 0; memcpy(dest->l, src->l, (src->l_index + 1) * 16); } return 1; } int CRYPTO_ocb128_setiv(OCB128_CONTEXT *ctx, const unsigned char *iv, size_t len, size_t taglen) { unsigned char ktop[16], tmp[16], mask; unsigned char stretch[24], nonce[16]; size_t bottom, shift; if ((len > 15) || (len < 1) || (taglen > 16) || (taglen < 1)) { return -1; } memset(&ctx->sess, 0, sizeof(ctx->sess)); nonce[0] = ((taglen * 8) % 128) << 1; memset(nonce + 1, 0, 15); memcpy(nonce + 16 - len, iv, len); nonce[15 - len] |= 1; memcpy(tmp, nonce, 16); tmp[15] &= 0xc0; ctx->encrypt(tmp, ktop, ctx->keyenc); memcpy(stretch, ktop, 16); ocb_block_xor(ktop, ktop + 1, 8, stretch + 16); bottom = nonce[15] & 0x3f; shift = bottom % 8; ocb_block_lshift(stretch + (bottom / 8), shift, ctx->sess.offset.c); mask = 0xff; mask <<= 8 - shift; ctx->sess.offset.c[15] |= (*(stretch + (bottom / 8) + 16) & mask) >> (8 - shift); return 1; } int CRYPTO_ocb128_aad(OCB128_CONTEXT *ctx, const unsigned char *aad, size_t len) { u64 i, all_num_blocks; size_t num_blocks, last_len; OCB_BLOCK tmp; num_blocks = len / 16; all_num_blocks = num_blocks + ctx->sess.blocks_hashed; for (i = ctx->sess.blocks_hashed + 1; i <= all_num_blocks; i++) { OCB_BLOCK *lookup; lookup = ocb_lookup_l(ctx, ocb_ntz(i)); if (lookup == NULL) return 0; ocb_block16_xor(&ctx->sess.offset_aad, lookup, &ctx->sess.offset_aad); memcpy(tmp.c, aad, 16); aad += 16; ocb_block16_xor(&ctx->sess.offset_aad, &tmp, &tmp); ctx->encrypt(tmp.c, tmp.c, ctx->keyenc); ocb_block16_xor(&tmp, &ctx->sess.sum, &ctx->sess.sum); } last_len = len % 16; if (last_len > 0) { ocb_block16_xor(&ctx->sess.offset_aad, &ctx->l_star, &ctx->sess.offset_aad); memset(tmp.c, 0, 16); memcpy(tmp.c, aad, last_len); tmp.c[last_len] = 0x80; ocb_block16_xor(&ctx->sess.offset_aad, &tmp, &tmp); ctx->encrypt(tmp.c, tmp.c, ctx->keyenc); ocb_block16_xor(&tmp, &ctx->sess.sum, &ctx->sess.sum); } ctx->sess.blocks_hashed = all_num_blocks; return 1; } int CRYPTO_ocb128_encrypt(OCB128_CONTEXT *ctx, const unsigned char *in, unsigned char *out, size_t len) { u64 i, all_num_blocks; size_t num_blocks, last_len; num_blocks = len / 16; all_num_blocks = num_blocks + ctx->sess.blocks_processed; if (num_blocks && all_num_blocks == (size_t)all_num_blocks && ctx->stream != NULL) { size_t max_idx = 0, top = (size_t)all_num_blocks; while (top >>= 1) max_idx++; if (ocb_lookup_l(ctx, max_idx) == NULL) return 0; ctx->stream(in, out, num_blocks, ctx->keyenc, (size_t)ctx->sess.blocks_processed + 1, ctx->sess.offset.c, (const unsigned char (*)[16])ctx->l, ctx->sess.checksum.c); } else { for (i = ctx->sess.blocks_processed + 1; i <= all_num_blocks; i++) { OCB_BLOCK *lookup; OCB_BLOCK tmp; lookup = ocb_lookup_l(ctx, ocb_ntz(i)); if (lookup == NULL) return 0; ocb_block16_xor(&ctx->sess.offset, lookup, &ctx->sess.offset); memcpy(tmp.c, in, 16); in += 16; ocb_block16_xor(&tmp, &ctx->sess.checksum, &ctx->sess.checksum); ocb_block16_xor(&ctx->sess.offset, &tmp, &tmp); ctx->encrypt(tmp.c, tmp.c, ctx->keyenc); ocb_block16_xor(&ctx->sess.offset, &tmp, &tmp); memcpy(out, tmp.c, 16); out += 16; } } last_len = len % 16; if (last_len > 0) { OCB_BLOCK pad; ocb_block16_xor(&ctx->sess.offset, &ctx->l_star, &ctx->sess.offset); ctx->encrypt(ctx->sess.offset.c, pad.c, ctx->keyenc); ocb_block_xor(in, pad.c, last_len, out); memset(pad.c, 0, 16); memcpy(pad.c, in, last_len); pad.c[last_len] = 0x80; ocb_block16_xor(&pad, &ctx->sess.checksum, &ctx->sess.checksum); } ctx->sess.blocks_processed = all_num_blocks; return 1; } int CRYPTO_ocb128_decrypt(OCB128_CONTEXT *ctx, const unsigned char *in, unsigned char *out, size_t len) { u64 i, all_num_blocks; size_t num_blocks, last_len; num_blocks = len / 16; all_num_blocks = num_blocks + ctx->sess.blocks_processed; if (num_blocks && all_num_blocks == (size_t)all_num_blocks && ctx->stream != NULL) { size_t max_idx = 0, top = (size_t)all_num_blocks; while (top >>= 1) max_idx++; if (ocb_lookup_l(ctx, max_idx) == NULL) return 0; ctx->stream(in, out, num_blocks, ctx->keydec, (size_t)ctx->sess.blocks_processed + 1, ctx->sess.offset.c, (const unsigned char (*)[16])ctx->l, ctx->sess.checksum.c); } else { OCB_BLOCK tmp; for (i = ctx->sess.blocks_processed + 1; i <= all_num_blocks; i++) { OCB_BLOCK *lookup = ocb_lookup_l(ctx, ocb_ntz(i)); if (lookup == NULL) return 0; ocb_block16_xor(&ctx->sess.offset, lookup, &ctx->sess.offset); memcpy(tmp.c, in, 16); in += 16; ocb_block16_xor(&ctx->sess.offset, &tmp, &tmp); ctx->decrypt(tmp.c, tmp.c, ctx->keydec); ocb_block16_xor(&ctx->sess.offset, &tmp, &tmp); ocb_block16_xor(&tmp, &ctx->sess.checksum, &ctx->sess.checksum); memcpy(out, tmp.c, 16); out += 16; } } last_len = len % 16; if (last_len > 0) { OCB_BLOCK pad; ocb_block16_xor(&ctx->sess.offset, &ctx->l_star, &ctx->sess.offset); ctx->encrypt(ctx->sess.offset.c, pad.c, ctx->keyenc); ocb_block_xor(in, pad.c, last_len, out); memset(pad.c, 0, 16); memcpy(pad.c, out, last_len); pad.c[last_len] = 0x80; ocb_block16_xor(&pad, &ctx->sess.checksum, &ctx->sess.checksum); } ctx->sess.blocks_processed = all_num_blocks; return 1; } static int ocb_finish(OCB128_CONTEXT *ctx, unsigned char *tag, size_t len, int write) { OCB_BLOCK tmp; if (len > 16 || len < 1) { return -1; } ocb_block16_xor(&ctx->sess.checksum, &ctx->sess.offset, &tmp); ocb_block16_xor(&ctx->l_dollar, &tmp, &tmp); ctx->encrypt(tmp.c, tmp.c, ctx->keyenc); ocb_block16_xor(&tmp, &ctx->sess.sum, &tmp); if (write) { memcpy(tag, &tmp, len); return 1; } else { return CRYPTO_memcmp(&tmp, tag, len); } } int CRYPTO_ocb128_finish(OCB128_CONTEXT *ctx, const unsigned char *tag, size_t len) { return ocb_finish(ctx, (unsigned char*)tag, len, 0); } int CRYPTO_ocb128_tag(OCB128_CONTEXT *ctx, unsigned char *tag, size_t len) { return ocb_finish(ctx, tag, len, 1); } void CRYPTO_ocb128_cleanup(OCB128_CONTEXT *ctx) { if (ctx) { OPENSSL_clear_free(ctx->l, ctx->max_l_index * 16); OPENSSL_cleanse(ctx, sizeof(*ctx)); } } #endif
modes
openssl/crypto/modes/ocb128.c
openssl
#include <string.h> #include <openssl/crypto.h> #include "crypto/modes.h" #if defined(__GNUC__) && !defined(STRICT_ALIGNMENT) typedef size_t size_t_aX __attribute((__aligned__(1))); #else typedef size_t size_t_aX; #endif void CRYPTO_cfb128_encrypt(const unsigned char *in, unsigned char *out, size_t len, const void *key, unsigned char ivec[16], int *num, int enc, block128_f block) { unsigned int n; size_t l = 0; if (*num < 0) { *num = -1; return; } n = *num; if (enc) { #if !defined(OPENSSL_SMALL_FOOTPRINT) if (16 % sizeof(size_t) == 0) { do { while (n && len) { *(out++) = ivec[n] ^= *(in++); --len; n = (n + 1) % 16; } # if defined(STRICT_ALIGNMENT) if (((size_t)in | (size_t)out | (size_t)ivec) % sizeof(size_t) != 0) break; # endif while (len >= 16) { (*block) (ivec, ivec, key); for (; n < 16; n += sizeof(size_t)) { *(size_t_aX *)(out + n) = *(size_t_aX *)(ivec + n) ^= *(size_t_aX *)(in + n); } len -= 16; out += 16; in += 16; n = 0; } if (len) { (*block) (ivec, ivec, key); while (len--) { out[n] = ivec[n] ^= in[n]; ++n; } } *num = n; return; } while (0); } #endif while (l < len) { if (n == 0) { (*block) (ivec, ivec, key); } out[l] = ivec[n] ^= in[l]; ++l; n = (n + 1) % 16; } *num = n; } else { #if !defined(OPENSSL_SMALL_FOOTPRINT) if (16 % sizeof(size_t) == 0) { do { while (n && len) { unsigned char c; *(out++) = ivec[n] ^ (c = *(in++)); ivec[n] = c; --len; n = (n + 1) % 16; } # if defined(STRICT_ALIGNMENT) if (((size_t)in | (size_t)out | (size_t)ivec) % sizeof(size_t) != 0) break; # endif while (len >= 16) { (*block) (ivec, ivec, key); for (; n < 16; n += sizeof(size_t)) { size_t t = *(size_t_aX *)(in + n); *(size_t_aX *)(out + n) = *(size_t_aX *)(ivec + n) ^ t; *(size_t_aX *)(ivec + n) = t; } len -= 16; out += 16; in += 16; n = 0; } if (len) { (*block) (ivec, ivec, key); while (len--) { unsigned char c; out[n] = ivec[n] ^ (c = in[n]); ivec[n] = c; ++n; } } *num = n; return; } while (0); } #endif while (l < len) { unsigned char c; if (n == 0) { (*block) (ivec, ivec, key); } out[l] = ivec[n] ^ (c = in[l]); ivec[n] = c; ++l; n = (n + 1) % 16; } *num = n; } } static void cfbr_encrypt_block(const unsigned char *in, unsigned char *out, int nbits, const void *key, unsigned char ivec[16], int enc, block128_f block) { int n, rem, num; unsigned char ovec[16 * 2 + 1]; if (nbits <= 0 || nbits > 128) return; memcpy(ovec, ivec, 16); (*block) (ivec, ivec, key); num = (nbits + 7) / 8; if (enc) for (n = 0; n < num; ++n) out[n] = (ovec[16 + n] = in[n] ^ ivec[n]); else for (n = 0; n < num; ++n) out[n] = (ovec[16 + n] = in[n]) ^ ivec[n]; rem = nbits % 8; num = nbits / 8; if (rem == 0) memcpy(ivec, ovec + num, 16); else for (n = 0; n < 16; ++n) ivec[n] = ovec[n + num] << rem | ovec[n + num + 1] >> (8 - rem); } void CRYPTO_cfb128_1_encrypt(const unsigned char *in, unsigned char *out, size_t bits, const void *key, unsigned char ivec[16], int *num, int enc, block128_f block) { size_t n; unsigned char c[1], d[1]; for (n = 0; n < bits; ++n) { c[0] = (in[n / 8] & (1 << (7 - n % 8))) ? 0x80 : 0; cfbr_encrypt_block(c, d, 1, key, ivec, enc, block); out[n / 8] = (out[n / 8] & ~(1 << (unsigned int)(7 - n % 8))) | ((d[0] & 0x80) >> (unsigned int)(n % 8)); } } void CRYPTO_cfb128_8_encrypt(const unsigned char *in, unsigned char *out, size_t length, const void *key, unsigned char ivec[16], int *num, int enc, block128_f block) { size_t n; for (n = 0; n < length; ++n) cfbr_encrypt_block(&in[n], &out[n], 8, key, ivec, enc, block); }
modes
openssl/crypto/modes/cfb128.c
openssl
#include <string.h> #include <openssl/crypto.h> #include "crypto/modes.h" #if defined(__GNUC__) && !defined(STRICT_ALIGNMENT) typedef size_t size_t_aX __attribute((__aligned__(1))); #else typedef size_t size_t_aX; #endif void CRYPTO_ofb128_encrypt(const unsigned char *in, unsigned char *out, size_t len, const void *key, unsigned char ivec[16], int *num, block128_f block) { unsigned int n; size_t l = 0; if (*num < 0) { *num = -1; return; } n = *num; #if !defined(OPENSSL_SMALL_FOOTPRINT) if (16 % sizeof(size_t) == 0) { do { while (n && len) { *(out++) = *(in++) ^ ivec[n]; --len; n = (n + 1) % 16; } # if defined(STRICT_ALIGNMENT) if (((size_t)in | (size_t)out | (size_t)ivec) % sizeof(size_t) != 0) break; # endif while (len >= 16) { (*block) (ivec, ivec, key); for (; n < 16; n += sizeof(size_t)) *(size_t_aX *)(out + n) = *(size_t_aX *)(in + n) ^ *(size_t_aX *)(ivec + n); len -= 16; out += 16; in += 16; n = 0; } if (len) { (*block) (ivec, ivec, key); while (len--) { out[n] = in[n] ^ ivec[n]; ++n; } } *num = n; return; } while (0); } #endif while (l < len) { if (n == 0) { (*block) (ivec, ivec, key); } out[l] = in[l] ^ ivec[n]; ++l; n = (n + 1) % 16; } *num = n; }
modes
openssl/crypto/modes/ofb128.c
openssl
#include <string.h> #include <openssl/crypto.h> #include "internal/endian.h" #include "crypto/modes.h" #ifndef STRICT_ALIGNMENT # ifdef __GNUC__ typedef u64 u64_a1 __attribute((__aligned__(1))); # else typedef u64 u64_a1; # endif #endif int CRYPTO_xts128_encrypt(const XTS128_CONTEXT *ctx, const unsigned char iv[16], const unsigned char *inp, unsigned char *out, size_t len, int enc) { DECLARE_IS_ENDIAN; union { u64 u[2]; u32 d[4]; u8 c[16]; } tweak, scratch; unsigned int i; if (len < 16) return -1; memcpy(tweak.c, iv, 16); (*ctx->block2) (tweak.c, tweak.c, ctx->key2); if (!enc && (len % 16)) len -= 16; while (len >= 16) { #if defined(STRICT_ALIGNMENT) memcpy(scratch.c, inp, 16); scratch.u[0] ^= tweak.u[0]; scratch.u[1] ^= tweak.u[1]; #else scratch.u[0] = ((u64_a1 *)inp)[0] ^ tweak.u[0]; scratch.u[1] = ((u64_a1 *)inp)[1] ^ tweak.u[1]; #endif (*ctx->block1) (scratch.c, scratch.c, ctx->key1); #if defined(STRICT_ALIGNMENT) scratch.u[0] ^= tweak.u[0]; scratch.u[1] ^= tweak.u[1]; memcpy(out, scratch.c, 16); #else ((u64_a1 *)out)[0] = scratch.u[0] ^= tweak.u[0]; ((u64_a1 *)out)[1] = scratch.u[1] ^= tweak.u[1]; #endif inp += 16; out += 16; len -= 16; if (len == 0) return 0; if (IS_LITTLE_ENDIAN) { unsigned int carry, res; res = 0x87 & (((int)tweak.d[3]) >> 31); carry = (unsigned int)(tweak.u[0] >> 63); tweak.u[0] = (tweak.u[0] << 1) ^ res; tweak.u[1] = (tweak.u[1] << 1) | carry; } else { size_t c; for (c = 0, i = 0; i < 16; ++i) { c += ((size_t)tweak.c[i]) << 1; tweak.c[i] = (u8)c; c = c >> 8; } tweak.c[0] ^= (u8)(0x87 & (0 - c)); } } if (enc) { for (i = 0; i < len; ++i) { u8 c = inp[i]; out[i] = scratch.c[i]; scratch.c[i] = c; } scratch.u[0] ^= tweak.u[0]; scratch.u[1] ^= tweak.u[1]; (*ctx->block1) (scratch.c, scratch.c, ctx->key1); scratch.u[0] ^= tweak.u[0]; scratch.u[1] ^= tweak.u[1]; memcpy(out - 16, scratch.c, 16); } else { union { u64 u[2]; u8 c[16]; } tweak1; if (IS_LITTLE_ENDIAN) { unsigned int carry, res; res = 0x87 & (((int)tweak.d[3]) >> 31); carry = (unsigned int)(tweak.u[0] >> 63); tweak1.u[0] = (tweak.u[0] << 1) ^ res; tweak1.u[1] = (tweak.u[1] << 1) | carry; } else { size_t c; for (c = 0, i = 0; i < 16; ++i) { c += ((size_t)tweak.c[i]) << 1; tweak1.c[i] = (u8)c; c = c >> 8; } tweak1.c[0] ^= (u8)(0x87 & (0 - c)); } #if defined(STRICT_ALIGNMENT) memcpy(scratch.c, inp, 16); scratch.u[0] ^= tweak1.u[0]; scratch.u[1] ^= tweak1.u[1]; #else scratch.u[0] = ((u64_a1 *)inp)[0] ^ tweak1.u[0]; scratch.u[1] = ((u64_a1 *)inp)[1] ^ tweak1.u[1]; #endif (*ctx->block1) (scratch.c, scratch.c, ctx->key1); scratch.u[0] ^= tweak1.u[0]; scratch.u[1] ^= tweak1.u[1]; for (i = 0; i < len; ++i) { u8 c = inp[16 + i]; out[16 + i] = scratch.c[i]; scratch.c[i] = c; } scratch.u[0] ^= tweak.u[0]; scratch.u[1] ^= tweak.u[1]; (*ctx->block1) (scratch.c, scratch.c, ctx->key1); #if defined(STRICT_ALIGNMENT) scratch.u[0] ^= tweak.u[0]; scratch.u[1] ^= tweak.u[1]; memcpy(out, scratch.c, 16); #else ((u64_a1 *)out)[0] = scratch.u[0] ^ tweak.u[0]; ((u64_a1 *)out)[1] = scratch.u[1] ^ tweak.u[1]; #endif } return 0; }
modes
openssl/crypto/modes/xts128.c
openssl
#include <string.h> #include <openssl/crypto.h> #include "crypto/modes.h" #ifndef STRICT_ALIGNMENT # ifdef __GNUC__ typedef u64 u64_a1 __attribute((__aligned__(1))); # else typedef u64 u64_a1; # endif #endif void CRYPTO_ccm128_init(CCM128_CONTEXT *ctx, unsigned int M, unsigned int L, void *key, block128_f block) { memset(ctx->nonce.c, 0, sizeof(ctx->nonce.c)); ctx->nonce.c[0] = ((u8)(L - 1) & 7) | (u8)(((M - 2) / 2) & 7) << 3; ctx->blocks = 0; ctx->block = block; ctx->key = key; } int CRYPTO_ccm128_setiv(CCM128_CONTEXT *ctx, const unsigned char *nonce, size_t nlen, size_t mlen) { unsigned int L = ctx->nonce.c[0] & 7; if (nlen < (14 - L)) return -1; if (sizeof(mlen) == 8 && L >= 3) { ctx->nonce.c[8] = (u8)(mlen >> (56 % (sizeof(mlen) * 8))); ctx->nonce.c[9] = (u8)(mlen >> (48 % (sizeof(mlen) * 8))); ctx->nonce.c[10] = (u8)(mlen >> (40 % (sizeof(mlen) * 8))); ctx->nonce.c[11] = (u8)(mlen >> (32 % (sizeof(mlen) * 8))); } else ctx->nonce.u[1] = 0; ctx->nonce.c[12] = (u8)(mlen >> 24); ctx->nonce.c[13] = (u8)(mlen >> 16); ctx->nonce.c[14] = (u8)(mlen >> 8); ctx->nonce.c[15] = (u8)mlen; ctx->nonce.c[0] &= ~0x40; memcpy(&ctx->nonce.c[1], nonce, 14 - L); return 0; } void CRYPTO_ccm128_aad(CCM128_CONTEXT *ctx, const unsigned char *aad, size_t alen) { unsigned int i; block128_f block = ctx->block; if (alen == 0) return; ctx->nonce.c[0] |= 0x40; (*block) (ctx->nonce.c, ctx->cmac.c, ctx->key), ctx->blocks++; if (alen < (0x10000 - 0x100)) { ctx->cmac.c[0] ^= (u8)(alen >> 8); ctx->cmac.c[1] ^= (u8)alen; i = 2; } else if (sizeof(alen) == 8 && alen >= (size_t)1 << (32 % (sizeof(alen) * 8))) { ctx->cmac.c[0] ^= 0xFF; ctx->cmac.c[1] ^= 0xFF; ctx->cmac.c[2] ^= (u8)(alen >> (56 % (sizeof(alen) * 8))); ctx->cmac.c[3] ^= (u8)(alen >> (48 % (sizeof(alen) * 8))); ctx->cmac.c[4] ^= (u8)(alen >> (40 % (sizeof(alen) * 8))); ctx->cmac.c[5] ^= (u8)(alen >> (32 % (sizeof(alen) * 8))); ctx->cmac.c[6] ^= (u8)(alen >> 24); ctx->cmac.c[7] ^= (u8)(alen >> 16); ctx->cmac.c[8] ^= (u8)(alen >> 8); ctx->cmac.c[9] ^= (u8)alen; i = 10; } else { ctx->cmac.c[0] ^= 0xFF; ctx->cmac.c[1] ^= 0xFE; ctx->cmac.c[2] ^= (u8)(alen >> 24); ctx->cmac.c[3] ^= (u8)(alen >> 16); ctx->cmac.c[4] ^= (u8)(alen >> 8); ctx->cmac.c[5] ^= (u8)alen; i = 6; } do { for (; i < 16 && alen; ++i, ++aad, --alen) ctx->cmac.c[i] ^= *aad; (*block) (ctx->cmac.c, ctx->cmac.c, ctx->key), ctx->blocks++; i = 0; } while (alen); } static void ctr64_inc(unsigned char *counter) { unsigned int n = 8; u8 c; counter += 8; do { --n; c = counter[n]; ++c; counter[n] = c; if (c) return; } while (n); } int CRYPTO_ccm128_encrypt(CCM128_CONTEXT *ctx, const unsigned char *inp, unsigned char *out, size_t len) { size_t n; unsigned int i, L; unsigned char flags0 = ctx->nonce.c[0]; block128_f block = ctx->block; void *key = ctx->key; union { u64 u[2]; u8 c[16]; } scratch; if (!(flags0 & 0x40)) (*block) (ctx->nonce.c, ctx->cmac.c, key), ctx->blocks++; ctx->nonce.c[0] = L = flags0 & 7; for (n = 0, i = 15 - L; i < 15; ++i) { n |= ctx->nonce.c[i]; ctx->nonce.c[i] = 0; n <<= 8; } n |= ctx->nonce.c[15]; ctx->nonce.c[15] = 1; if (n != len) return -1; ctx->blocks += ((len + 15) >> 3) | 1; if (ctx->blocks > (U64(1) << 61)) return -2; while (len >= 16) { #if defined(STRICT_ALIGNMENT) union { u64 u[2]; u8 c[16]; } temp; memcpy(temp.c, inp, 16); ctx->cmac.u[0] ^= temp.u[0]; ctx->cmac.u[1] ^= temp.u[1]; #else ctx->cmac.u[0] ^= ((u64_a1 *)inp)[0]; ctx->cmac.u[1] ^= ((u64_a1 *)inp)[1]; #endif (*block) (ctx->cmac.c, ctx->cmac.c, key); (*block) (ctx->nonce.c, scratch.c, key); ctr64_inc(ctx->nonce.c); #if defined(STRICT_ALIGNMENT) temp.u[0] ^= scratch.u[0]; temp.u[1] ^= scratch.u[1]; memcpy(out, temp.c, 16); #else ((u64_a1 *)out)[0] = scratch.u[0] ^ ((u64_a1 *)inp)[0]; ((u64_a1 *)out)[1] = scratch.u[1] ^ ((u64_a1 *)inp)[1]; #endif inp += 16; out += 16; len -= 16; } if (len) { for (i = 0; i < len; ++i) ctx->cmac.c[i] ^= inp[i]; (*block) (ctx->cmac.c, ctx->cmac.c, key); (*block) (ctx->nonce.c, scratch.c, key); for (i = 0; i < len; ++i) out[i] = scratch.c[i] ^ inp[i]; } for (i = 15 - L; i < 16; ++i) ctx->nonce.c[i] = 0; (*block) (ctx->nonce.c, scratch.c, key); ctx->cmac.u[0] ^= scratch.u[0]; ctx->cmac.u[1] ^= scratch.u[1]; ctx->nonce.c[0] = flags0; return 0; } int CRYPTO_ccm128_decrypt(CCM128_CONTEXT *ctx, const unsigned char *inp, unsigned char *out, size_t len) { size_t n; unsigned int i, L; unsigned char flags0 = ctx->nonce.c[0]; block128_f block = ctx->block; void *key = ctx->key; union { u64 u[2]; u8 c[16]; } scratch; if (!(flags0 & 0x40)) (*block) (ctx->nonce.c, ctx->cmac.c, key); ctx->nonce.c[0] = L = flags0 & 7; for (n = 0, i = 15 - L; i < 15; ++i) { n |= ctx->nonce.c[i]; ctx->nonce.c[i] = 0; n <<= 8; } n |= ctx->nonce.c[15]; ctx->nonce.c[15] = 1; if (n != len) return -1; while (len >= 16) { #if defined(STRICT_ALIGNMENT) union { u64 u[2]; u8 c[16]; } temp; #endif (*block) (ctx->nonce.c, scratch.c, key); ctr64_inc(ctx->nonce.c); #if defined(STRICT_ALIGNMENT) memcpy(temp.c, inp, 16); ctx->cmac.u[0] ^= (scratch.u[0] ^= temp.u[0]); ctx->cmac.u[1] ^= (scratch.u[1] ^= temp.u[1]); memcpy(out, scratch.c, 16); #else ctx->cmac.u[0] ^= (((u64_a1 *)out)[0] = scratch.u[0] ^ ((u64_a1 *)inp)[0]); ctx->cmac.u[1] ^= (((u64_a1 *)out)[1] = scratch.u[1] ^ ((u64_a1 *)inp)[1]); #endif (*block) (ctx->cmac.c, ctx->cmac.c, key); inp += 16; out += 16; len -= 16; } if (len) { (*block) (ctx->nonce.c, scratch.c, key); for (i = 0; i < len; ++i) ctx->cmac.c[i] ^= (out[i] = scratch.c[i] ^ inp[i]); (*block) (ctx->cmac.c, ctx->cmac.c, key); } for (i = 15 - L; i < 16; ++i) ctx->nonce.c[i] = 0; (*block) (ctx->nonce.c, scratch.c, key); ctx->cmac.u[0] ^= scratch.u[0]; ctx->cmac.u[1] ^= scratch.u[1]; ctx->nonce.c[0] = flags0; return 0; } static void ctr64_add(unsigned char *counter, size_t inc) { size_t n = 8, val = 0; counter += 8; do { --n; val += counter[n] + (inc & 0xff); counter[n] = (unsigned char)val; val >>= 8; inc >>= 8; } while (n && (inc || val)); } int CRYPTO_ccm128_encrypt_ccm64(CCM128_CONTEXT *ctx, const unsigned char *inp, unsigned char *out, size_t len, ccm128_f stream) { size_t n; unsigned int i, L; unsigned char flags0 = ctx->nonce.c[0]; block128_f block = ctx->block; void *key = ctx->key; union { u64 u[2]; u8 c[16]; } scratch; if (!(flags0 & 0x40)) (*block) (ctx->nonce.c, ctx->cmac.c, key), ctx->blocks++; ctx->nonce.c[0] = L = flags0 & 7; for (n = 0, i = 15 - L; i < 15; ++i) { n |= ctx->nonce.c[i]; ctx->nonce.c[i] = 0; n <<= 8; } n |= ctx->nonce.c[15]; ctx->nonce.c[15] = 1; if (n != len) return -1; ctx->blocks += ((len + 15) >> 3) | 1; if (ctx->blocks > (U64(1) << 61)) return -2; if ((n = len / 16)) { (*stream) (inp, out, n, key, ctx->nonce.c, ctx->cmac.c); n *= 16; inp += n; out += n; len -= n; if (len) ctr64_add(ctx->nonce.c, n / 16); } if (len) { for (i = 0; i < len; ++i) ctx->cmac.c[i] ^= inp[i]; (*block) (ctx->cmac.c, ctx->cmac.c, key); (*block) (ctx->nonce.c, scratch.c, key); for (i = 0; i < len; ++i) out[i] = scratch.c[i] ^ inp[i]; } for (i = 15 - L; i < 16; ++i) ctx->nonce.c[i] = 0; (*block) (ctx->nonce.c, scratch.c, key); ctx->cmac.u[0] ^= scratch.u[0]; ctx->cmac.u[1] ^= scratch.u[1]; ctx->nonce.c[0] = flags0; return 0; } int CRYPTO_ccm128_decrypt_ccm64(CCM128_CONTEXT *ctx, const unsigned char *inp, unsigned char *out, size_t len, ccm128_f stream) { size_t n; unsigned int i, L; unsigned char flags0 = ctx->nonce.c[0]; block128_f block = ctx->block; void *key = ctx->key; union { u64 u[2]; u8 c[16]; } scratch; if (!(flags0 & 0x40)) (*block) (ctx->nonce.c, ctx->cmac.c, key); ctx->nonce.c[0] = L = flags0 & 7; for (n = 0, i = 15 - L; i < 15; ++i) { n |= ctx->nonce.c[i]; ctx->nonce.c[i] = 0; n <<= 8; } n |= ctx->nonce.c[15]; ctx->nonce.c[15] = 1; if (n != len) return -1; if ((n = len / 16)) { (*stream) (inp, out, n, key, ctx->nonce.c, ctx->cmac.c); n *= 16; inp += n; out += n; len -= n; if (len) ctr64_add(ctx->nonce.c, n / 16); } if (len) { (*block) (ctx->nonce.c, scratch.c, key); for (i = 0; i < len; ++i) ctx->cmac.c[i] ^= (out[i] = scratch.c[i] ^ inp[i]); (*block) (ctx->cmac.c, ctx->cmac.c, key); } for (i = 15 - L; i < 16; ++i) ctx->nonce.c[i] = 0; (*block) (ctx->nonce.c, scratch.c, key); ctx->cmac.u[0] ^= scratch.u[0]; ctx->cmac.u[1] ^= scratch.u[1]; ctx->nonce.c[0] = flags0; return 0; } size_t CRYPTO_ccm128_tag(CCM128_CONTEXT *ctx, unsigned char *tag, size_t len) { unsigned int M = (ctx->nonce.c[0] >> 3) & 7; M *= 2; M += 2; if (len != M) return 0; memcpy(tag, ctx->cmac.c, M); return M; }
modes
openssl/crypto/modes/ccm128.c
openssl
#include <string.h> #include <openssl/crypto.h> #include "internal/endian.h" #include "crypto/modes.h" #ifndef STRICT_ALIGNMENT # ifdef __GNUC__ typedef u64 u64_a1 __attribute((__aligned__(1))); # else typedef u64 u64_a1; # endif #endif int ossl_crypto_xts128gb_encrypt(const XTS128_CONTEXT *ctx, const unsigned char iv[16], const unsigned char *inp, unsigned char *out, size_t len, int enc) { DECLARE_IS_ENDIAN; union { u64 u[2]; u32 d[4]; u8 c[16]; } tweak, scratch; unsigned int i; if (len < 16) return -1; memcpy(tweak.c, iv, 16); (*ctx->block2) (tweak.c, tweak.c, ctx->key2); if (!enc && (len % 16)) len -= 16; while (len >= 16) { #if defined(STRICT_ALIGNMENT) memcpy(scratch.c, inp, 16); scratch.u[0] ^= tweak.u[0]; scratch.u[1] ^= tweak.u[1]; #else scratch.u[0] = ((u64_a1 *)inp)[0] ^ tweak.u[0]; scratch.u[1] = ((u64_a1 *)inp)[1] ^ tweak.u[1]; #endif (*ctx->block1) (scratch.c, scratch.c, ctx->key1); #if defined(STRICT_ALIGNMENT) scratch.u[0] ^= tweak.u[0]; scratch.u[1] ^= tweak.u[1]; memcpy(out, scratch.c, 16); #else ((u64_a1 *)out)[0] = scratch.u[0] ^= tweak.u[0]; ((u64_a1 *)out)[1] = scratch.u[1] ^= tweak.u[1]; #endif inp += 16; out += 16; len -= 16; if (len == 0) return 0; if (IS_LITTLE_ENDIAN) { u8 res; u64 hi, lo; #ifdef BSWAP8 hi = BSWAP8(tweak.u[0]); lo = BSWAP8(tweak.u[1]); #else u8 *p = tweak.c; hi = (u64)GETU32(p) << 32 | GETU32(p + 4); lo = (u64)GETU32(p + 8) << 32 | GETU32(p + 12); #endif res = (u8)lo & 1; tweak.u[0] = (lo >> 1) | (hi << 63); tweak.u[1] = hi >> 1; if (res) tweak.c[15] ^= 0xe1; #ifdef BSWAP8 hi = BSWAP8(tweak.u[0]); lo = BSWAP8(tweak.u[1]); #else p = tweak.c; hi = (u64)GETU32(p) << 32 | GETU32(p + 4); lo = (u64)GETU32(p + 8) << 32 | GETU32(p + 12); #endif tweak.u[0] = lo; tweak.u[1] = hi; } else { u8 carry, res; carry = 0; for (i = 0; i < 16; ++i) { res = (tweak.c[i] << 7) & 0x80; tweak.c[i] = ((tweak.c[i] >> 1) + carry) & 0xff; carry = res; } if (res) tweak.c[0] ^= 0xe1; } } if (enc) { for (i = 0; i < len; ++i) { u8 c = inp[i]; out[i] = scratch.c[i]; scratch.c[i] = c; } scratch.u[0] ^= tweak.u[0]; scratch.u[1] ^= tweak.u[1]; (*ctx->block1) (scratch.c, scratch.c, ctx->key1); scratch.u[0] ^= tweak.u[0]; scratch.u[1] ^= tweak.u[1]; memcpy(out - 16, scratch.c, 16); } else { union { u64 u[2]; u8 c[16]; } tweak1; if (IS_LITTLE_ENDIAN) { u8 res; u64 hi, lo; #ifdef BSWAP8 hi = BSWAP8(tweak.u[0]); lo = BSWAP8(tweak.u[1]); #else u8 *p = tweak.c; hi = (u64)GETU32(p) << 32 | GETU32(p + 4); lo = (u64)GETU32(p + 8) << 32 | GETU32(p + 12); #endif res = (u8)lo & 1; tweak1.u[0] = (lo >> 1) | (hi << 63); tweak1.u[1] = hi >> 1; if (res) tweak1.c[15] ^= 0xe1; #ifdef BSWAP8 hi = BSWAP8(tweak1.u[0]); lo = BSWAP8(tweak1.u[1]); #else p = tweak1.c; hi = (u64)GETU32(p) << 32 | GETU32(p + 4); lo = (u64)GETU32(p + 8) << 32 | GETU32(p + 12); #endif tweak1.u[0] = lo; tweak1.u[1] = hi; } else { u8 carry, res; carry = 0; for (i = 0; i < 16; ++i) { res = (tweak.c[i] << 7) & 0x80; tweak1.c[i] = ((tweak.c[i] >> 1) + carry) & 0xff; carry = res; } if (res) tweak1.c[0] ^= 0xe1; } #if defined(STRICT_ALIGNMENT) memcpy(scratch.c, inp, 16); scratch.u[0] ^= tweak1.u[0]; scratch.u[1] ^= tweak1.u[1]; #else scratch.u[0] = ((u64_a1 *)inp)[0] ^ tweak1.u[0]; scratch.u[1] = ((u64_a1 *)inp)[1] ^ tweak1.u[1]; #endif (*ctx->block1) (scratch.c, scratch.c, ctx->key1); scratch.u[0] ^= tweak1.u[0]; scratch.u[1] ^= tweak1.u[1]; for (i = 0; i < len; ++i) { u8 c = inp[16 + i]; out[16 + i] = scratch.c[i]; scratch.c[i] = c; } scratch.u[0] ^= tweak.u[0]; scratch.u[1] ^= tweak.u[1]; (*ctx->block1) (scratch.c, scratch.c, ctx->key1); #if defined(STRICT_ALIGNMENT) scratch.u[0] ^= tweak.u[0]; scratch.u[1] ^= tweak.u[1]; memcpy(out, scratch.c, 16); #else ((u64_a1 *)out)[0] = scratch.u[0] ^ tweak.u[0]; ((u64_a1 *)out)[1] = scratch.u[1] ^ tweak.u[1]; #endif } return 0; }
modes
openssl/crypto/modes/xts128gb.c
openssl
#include <string.h> #include <openssl/crypto.h> #include "internal/endian.h" #include "crypto/modes.h" #if defined(__GNUC__) && !defined(STRICT_ALIGNMENT) typedef size_t size_t_aX __attribute((__aligned__(1))); #else typedef size_t size_t_aX; #endif static void ctr128_inc(unsigned char *counter) { u32 n = 16, c = 1; do { --n; c += counter[n]; counter[n] = (u8)c; c >>= 8; } while (n); } #if !defined(OPENSSL_SMALL_FOOTPRINT) static void ctr128_inc_aligned(unsigned char *counter) { size_t *data, c, d, n; DECLARE_IS_ENDIAN; if (IS_LITTLE_ENDIAN || ((size_t)counter % sizeof(size_t)) != 0) { ctr128_inc(counter); return; } data = (size_t *)counter; c = 1; n = 16 / sizeof(size_t); do { --n; d = data[n] += c; c = ((d - c) & ~d) >> (sizeof(size_t) * 8 - 1); } while (n); } #endif void CRYPTO_ctr128_encrypt(const unsigned char *in, unsigned char *out, size_t len, const void *key, unsigned char ivec[16], unsigned char ecount_buf[16], unsigned int *num, block128_f block) { unsigned int n; size_t l = 0; n = *num; #if !defined(OPENSSL_SMALL_FOOTPRINT) if (16 % sizeof(size_t) == 0) { do { while (n && len) { *(out++) = *(in++) ^ ecount_buf[n]; --len; n = (n + 1) % 16; } # if defined(STRICT_ALIGNMENT) if (((size_t)in | (size_t)out | (size_t)ecount_buf) % sizeof(size_t) != 0) break; # endif while (len >= 16) { (*block) (ivec, ecount_buf, key); ctr128_inc_aligned(ivec); for (n = 0; n < 16; n += sizeof(size_t)) *(size_t_aX *)(out + n) = *(size_t_aX *)(in + n) ^ *(size_t_aX *)(ecount_buf + n); len -= 16; out += 16; in += 16; n = 0; } if (len) { (*block) (ivec, ecount_buf, key); ctr128_inc_aligned(ivec); while (len--) { out[n] = in[n] ^ ecount_buf[n]; ++n; } } *num = n; return; } while (0); } #endif while (l < len) { if (n == 0) { (*block) (ivec, ecount_buf, key); ctr128_inc(ivec); } out[l] = in[l] ^ ecount_buf[n]; ++l; n = (n + 1) % 16; } *num = n; } static void ctr96_inc(unsigned char *counter) { u32 n = 12, c = 1; do { --n; c += counter[n]; counter[n] = (u8)c; c >>= 8; } while (n); } void CRYPTO_ctr128_encrypt_ctr32(const unsigned char *in, unsigned char *out, size_t len, const void *key, unsigned char ivec[16], unsigned char ecount_buf[16], unsigned int *num, ctr128_f func) { unsigned int n, ctr32; n = *num; while (n && len) { *(out++) = *(in++) ^ ecount_buf[n]; --len; n = (n + 1) % 16; } ctr32 = GETU32(ivec + 12); while (len >= 16) { size_t blocks = len / 16; if (sizeof(size_t) > sizeof(unsigned int) && blocks > (1U << 28)) blocks = (1U << 28); ctr32 += (u32)blocks; if (ctr32 < blocks) { blocks -= ctr32; ctr32 = 0; } (*func) (in, out, blocks, key, ivec); PUTU32(ivec + 12, ctr32); if (ctr32 == 0) ctr96_inc(ivec); blocks *= 16; len -= blocks; out += blocks; in += blocks; } if (len) { memset(ecount_buf, 0, 16); (*func) (ecount_buf, ecount_buf, 1, key, ivec); ++ctr32; PUTU32(ivec + 12, ctr32); if (ctr32 == 0) ctr96_inc(ivec); while (len--) { out[n] = in[n] ^ ecount_buf[n]; ++n; } } *num = n; }
modes
openssl/crypto/modes/ctr128.c
openssl
#include <string.h> #include <openssl/crypto.h> #include "crypto/modes.h" #if !defined(STRICT_ALIGNMENT) && !defined(PEDANTIC) # define STRICT_ALIGNMENT 0 #endif #if defined(__GNUC__) && !STRICT_ALIGNMENT typedef size_t size_t_aX __attribute((__aligned__(1))); #else typedef size_t size_t_aX; #endif void CRYPTO_cbc128_encrypt(const unsigned char *in, unsigned char *out, size_t len, const void *key, unsigned char ivec[16], block128_f block) { size_t n; const unsigned char *iv = ivec; if (len == 0) return; #if !defined(OPENSSL_SMALL_FOOTPRINT) if (STRICT_ALIGNMENT && ((size_t)in | (size_t)out | (size_t)ivec) % sizeof(size_t) != 0) { while (len >= 16) { for (n = 0; n < 16; ++n) out[n] = in[n] ^ iv[n]; (*block) (out, out, key); iv = out; len -= 16; in += 16; out += 16; } } else { while (len >= 16) { for (n = 0; n < 16; n += sizeof(size_t)) *(size_t_aX *)(out + n) = *(size_t_aX *)(in + n) ^ *(size_t_aX *)(iv + n); (*block) (out, out, key); iv = out; len -= 16; in += 16; out += 16; } } #endif while (len) { for (n = 0; n < 16 && n < len; ++n) out[n] = in[n] ^ iv[n]; for (; n < 16; ++n) out[n] = iv[n]; (*block) (out, out, key); iv = out; if (len <= 16) break; len -= 16; in += 16; out += 16; } if (ivec != iv) memcpy(ivec, iv, 16); } void CRYPTO_cbc128_decrypt(const unsigned char *in, unsigned char *out, size_t len, const void *key, unsigned char ivec[16], block128_f block) { size_t n; union { size_t t[16 / sizeof(size_t)]; unsigned char c[16]; } tmp; if (len == 0) return; #if !defined(OPENSSL_SMALL_FOOTPRINT) if (in != out) { const unsigned char *iv = ivec; if (STRICT_ALIGNMENT && ((size_t)in | (size_t)out | (size_t)ivec) % sizeof(size_t) != 0) { while (len >= 16) { (*block) (in, out, key); for (n = 0; n < 16; ++n) out[n] ^= iv[n]; iv = in; len -= 16; in += 16; out += 16; } } else if (16 % sizeof(size_t) == 0) { while (len >= 16) { size_t_aX *out_t = (size_t_aX *)out; size_t_aX *iv_t = (size_t_aX *)iv; (*block) (in, out, key); for (n = 0; n < 16 / sizeof(size_t); n++) out_t[n] ^= iv_t[n]; iv = in; len -= 16; in += 16; out += 16; } } if (ivec != iv) memcpy(ivec, iv, 16); } else { if (STRICT_ALIGNMENT && ((size_t)in | (size_t)out | (size_t)ivec) % sizeof(size_t) != 0) { unsigned char c; while (len >= 16) { (*block) (in, tmp.c, key); for (n = 0; n < 16; ++n) { c = in[n]; out[n] = tmp.c[n] ^ ivec[n]; ivec[n] = c; } len -= 16; in += 16; out += 16; } } else if (16 % sizeof(size_t) == 0) { while (len >= 16) { size_t c; size_t_aX *out_t = (size_t_aX *)out; size_t_aX *ivec_t = (size_t_aX *)ivec; const size_t_aX *in_t = (const size_t_aX *)in; (*block) (in, tmp.c, key); for (n = 0; n < 16 / sizeof(size_t); n++) { c = in_t[n]; out_t[n] = tmp.t[n] ^ ivec_t[n]; ivec_t[n] = c; } len -= 16; in += 16; out += 16; } } } #endif while (len) { unsigned char c; (*block) (in, tmp.c, key); for (n = 0; n < 16 && n < len; ++n) { c = in[n]; out[n] = tmp.c[n] ^ ivec[n]; ivec[n] = c; } if (len <= 16) { for (; n < 16; ++n) ivec[n] = in[n]; break; } len -= 16; in += 16; out += 16; } }
modes
openssl/crypto/modes/cbc128.c
openssl
#include <string.h> #include <openssl/crypto.h> #include "crypto/modes.h" size_t CRYPTO_cts128_encrypt_block(const unsigned char *in, unsigned char *out, size_t len, const void *key, unsigned char ivec[16], block128_f block) { size_t residue, n; if (len <= 16) return 0; if ((residue = len % 16) == 0) residue = 16; len -= residue; CRYPTO_cbc128_encrypt(in, out, len, key, ivec, block); in += len; out += len; for (n = 0; n < residue; ++n) ivec[n] ^= in[n]; (*block) (ivec, ivec, key); memcpy(out, out - 16, residue); memcpy(out - 16, ivec, 16); return len + residue; } size_t CRYPTO_nistcts128_encrypt_block(const unsigned char *in, unsigned char *out, size_t len, const void *key, unsigned char ivec[16], block128_f block) { size_t residue, n; if (len < 16) return 0; residue = len % 16; len -= residue; CRYPTO_cbc128_encrypt(in, out, len, key, ivec, block); if (residue == 0) return len; in += len; out += len; for (n = 0; n < residue; ++n) ivec[n] ^= in[n]; (*block) (ivec, ivec, key); memcpy(out - 16 + residue, ivec, 16); return len + residue; } size_t CRYPTO_cts128_encrypt(const unsigned char *in, unsigned char *out, size_t len, const void *key, unsigned char ivec[16], cbc128_f cbc) { size_t residue; union { size_t align; unsigned char c[16]; } tmp; if (len <= 16) return 0; if ((residue = len % 16) == 0) residue = 16; len -= residue; (*cbc) (in, out, len, key, ivec, 1); in += len; out += len; #if defined(CBC_HANDLES_TRUNCATED_IO) memcpy(tmp.c, out - 16, 16); (*cbc) (in, out - 16, residue, key, ivec, 1); memcpy(out, tmp.c, residue); #else memset(tmp.c, 0, sizeof(tmp)); memcpy(tmp.c, in, residue); memcpy(out, out - 16, residue); (*cbc) (tmp.c, out - 16, 16, key, ivec, 1); #endif return len + residue; } size_t CRYPTO_nistcts128_encrypt(const unsigned char *in, unsigned char *out, size_t len, const void *key, unsigned char ivec[16], cbc128_f cbc) { size_t residue; union { size_t align; unsigned char c[16]; } tmp; if (len < 16) return 0; residue = len % 16; len -= residue; (*cbc) (in, out, len, key, ivec, 1); if (residue == 0) return len; in += len; out += len; #if defined(CBC_HANDLES_TRUNCATED_IO) (*cbc) (in, out - 16 + residue, residue, key, ivec, 1); #else memset(tmp.c, 0, sizeof(tmp)); memcpy(tmp.c, in, residue); (*cbc) (tmp.c, out - 16 + residue, 16, key, ivec, 1); #endif return len + residue; } size_t CRYPTO_cts128_decrypt_block(const unsigned char *in, unsigned char *out, size_t len, const void *key, unsigned char ivec[16], block128_f block) { size_t residue, n; union { size_t align; unsigned char c[32]; } tmp; if (len <= 16) return 0; if ((residue = len % 16) == 0) residue = 16; len -= 16 + residue; if (len) { CRYPTO_cbc128_decrypt(in, out, len, key, ivec, block); in += len; out += len; } (*block) (in, tmp.c + 16, key); memcpy(tmp.c, tmp.c + 16, 16); memcpy(tmp.c, in + 16, residue); (*block) (tmp.c, tmp.c, key); for (n = 0; n < 16; ++n) { unsigned char c = in[n]; out[n] = tmp.c[n] ^ ivec[n]; ivec[n] = c; } for (residue += 16; n < residue; ++n) out[n] = tmp.c[n] ^ in[n]; return 16 + len + residue; } size_t CRYPTO_nistcts128_decrypt_block(const unsigned char *in, unsigned char *out, size_t len, const void *key, unsigned char ivec[16], block128_f block) { size_t residue, n; union { size_t align; unsigned char c[32]; } tmp; if (len < 16) return 0; residue = len % 16; if (residue == 0) { CRYPTO_cbc128_decrypt(in, out, len, key, ivec, block); return len; } len -= 16 + residue; if (len) { CRYPTO_cbc128_decrypt(in, out, len, key, ivec, block); in += len; out += len; } (*block) (in + residue, tmp.c + 16, key); memcpy(tmp.c, tmp.c + 16, 16); memcpy(tmp.c, in, residue); (*block) (tmp.c, tmp.c, key); for (n = 0; n < 16; ++n) { unsigned char c = in[n]; out[n] = tmp.c[n] ^ ivec[n]; ivec[n] = in[n + residue]; tmp.c[n] = c; } for (residue += 16; n < residue; ++n) out[n] = tmp.c[n] ^ tmp.c[n - 16]; return 16 + len + residue; } size_t CRYPTO_cts128_decrypt(const unsigned char *in, unsigned char *out, size_t len, const void *key, unsigned char ivec[16], cbc128_f cbc) { size_t residue; union { size_t align; unsigned char c[32]; } tmp; if (len <= 16) return 0; if ((residue = len % 16) == 0) residue = 16; len -= 16 + residue; if (len) { (*cbc) (in, out, len, key, ivec, 0); in += len; out += len; } memset(tmp.c, 0, sizeof(tmp)); (*cbc) (in, tmp.c, 16, key, tmp.c + 16, 0); memcpy(tmp.c, in + 16, residue); #if defined(CBC_HANDLES_TRUNCATED_IO) (*cbc) (tmp.c, out, 16 + residue, key, ivec, 0); #else (*cbc) (tmp.c, tmp.c, 32, key, ivec, 0); memcpy(out, tmp.c, 16 + residue); #endif return 16 + len + residue; } size_t CRYPTO_nistcts128_decrypt(const unsigned char *in, unsigned char *out, size_t len, const void *key, unsigned char ivec[16], cbc128_f cbc) { size_t residue; union { size_t align; unsigned char c[32]; } tmp; if (len < 16) return 0; residue = len % 16; if (residue == 0) { (*cbc) (in, out, len, key, ivec, 0); return len; } len -= 16 + residue; if (len) { (*cbc) (in, out, len, key, ivec, 0); in += len; out += len; } memset(tmp.c, 0, sizeof(tmp)); (*cbc) (in + residue, tmp.c, 16, key, tmp.c + 16, 0); memcpy(tmp.c, in, residue); #if defined(CBC_HANDLES_TRUNCATED_IO) (*cbc) (tmp.c, out, 16 + residue, key, ivec, 0); #else (*cbc) (tmp.c, tmp.c, 32, key, ivec, 0); memcpy(out, tmp.c, 16 + residue); #endif return 16 + len + residue; }
modes
openssl/crypto/modes/cts128.c
openssl
#include "internal/cryptlib.h" #include <openssl/modes.h> static const unsigned char default_iv[] = { 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, }; static const unsigned char default_aiv[] = { 0xA6, 0x59, 0x59, 0xA6 }; #define CRYPTO128_WRAP_MAX (1UL << 31) size_t CRYPTO_128_wrap(void *key, const unsigned char *iv, unsigned char *out, const unsigned char *in, size_t inlen, block128_f block) { unsigned char *A, B[16], *R; size_t i, j, t; if ((inlen & 0x7) || (inlen < 16) || (inlen > CRYPTO128_WRAP_MAX)) return 0; A = B; t = 1; memmove(out + 8, in, inlen); if (!iv) iv = default_iv; memcpy(A, iv, 8); for (j = 0; j < 6; j++) { R = out + 8; for (i = 0; i < inlen; i += 8, t++, R += 8) { memcpy(B + 8, R, 8); block(B, B, key); A[7] ^= (unsigned char)(t & 0xff); if (t > 0xff) { A[6] ^= (unsigned char)((t >> 8) & 0xff); A[5] ^= (unsigned char)((t >> 16) & 0xff); A[4] ^= (unsigned char)((t >> 24) & 0xff); } memcpy(R, B + 8, 8); } } memcpy(out, A, 8); return inlen + 8; } static size_t crypto_128_unwrap_raw(void *key, unsigned char *iv, unsigned char *out, const unsigned char *in, size_t inlen, block128_f block) { unsigned char *A, B[16], *R; size_t i, j, t; inlen -= 8; if ((inlen & 0x7) || (inlen < 16) || (inlen > CRYPTO128_WRAP_MAX)) return 0; A = B; t = 6 * (inlen >> 3); memcpy(A, in, 8); memmove(out, in + 8, inlen); for (j = 0; j < 6; j++) { R = out + inlen - 8; for (i = 0; i < inlen; i += 8, t--, R -= 8) { A[7] ^= (unsigned char)(t & 0xff); if (t > 0xff) { A[6] ^= (unsigned char)((t >> 8) & 0xff); A[5] ^= (unsigned char)((t >> 16) & 0xff); A[4] ^= (unsigned char)((t >> 24) & 0xff); } memcpy(B + 8, R, 8); block(B, B, key); memcpy(R, B + 8, 8); } } memcpy(iv, A, 8); return inlen; } size_t CRYPTO_128_unwrap(void *key, const unsigned char *iv, unsigned char *out, const unsigned char *in, size_t inlen, block128_f block) { size_t ret; unsigned char got_iv[8]; ret = crypto_128_unwrap_raw(key, got_iv, out, in, inlen, block); if (ret == 0) return 0; if (!iv) iv = default_iv; if (CRYPTO_memcmp(got_iv, iv, 8)) { OPENSSL_cleanse(out, ret); return 0; } return ret; } size_t CRYPTO_128_wrap_pad(void *key, const unsigned char *icv, unsigned char *out, const unsigned char *in, size_t inlen, block128_f block) { const size_t blocks_padded = (inlen + 7) / 8; const size_t padded_len = blocks_padded * 8; const size_t padding_len = padded_len - inlen; unsigned char aiv[8]; int ret; if (inlen == 0 || inlen >= CRYPTO128_WRAP_MAX) return 0; if (!icv) memcpy(aiv, default_aiv, 4); else memcpy(aiv, icv, 4); aiv[4] = (inlen >> 24) & 0xFF; aiv[5] = (inlen >> 16) & 0xFF; aiv[6] = (inlen >> 8) & 0xFF; aiv[7] = inlen & 0xFF; if (padded_len == 8) { memmove(out + 8, in, inlen); memcpy(out, aiv, 8); memset(out + 8 + inlen, 0, padding_len); block(out, out, key); ret = 16; } else { memmove(out, in, inlen); memset(out + inlen, 0, padding_len); ret = CRYPTO_128_wrap(key, aiv, out, out, padded_len, block); } return ret; } size_t CRYPTO_128_unwrap_pad(void *key, const unsigned char *icv, unsigned char *out, const unsigned char *in, size_t inlen, block128_f block) { size_t n = inlen / 8 - 1; size_t padded_len; size_t padding_len; size_t ptext_len; unsigned char aiv[8]; static unsigned char zeros[8] = { 0x0 }; size_t ret; if ((inlen & 0x7) != 0 || inlen < 16 || inlen >= CRYPTO128_WRAP_MAX) return 0; if (inlen == 16) { unsigned char buff[16]; block(in, buff, key); memcpy(aiv, buff, 8); memcpy(out, buff + 8, 8); padded_len = 8; OPENSSL_cleanse(buff, inlen); } else { padded_len = inlen - 8; ret = crypto_128_unwrap_raw(key, aiv, out, in, inlen, block); if (padded_len != ret) { OPENSSL_cleanse(out, inlen); return 0; } } if ((!icv && CRYPTO_memcmp(aiv, default_aiv, 4)) || (icv && CRYPTO_memcmp(aiv, icv, 4))) { OPENSSL_cleanse(out, inlen); return 0; } ptext_len = ((unsigned int)aiv[4] << 24) | ((unsigned int)aiv[5] << 16) | ((unsigned int)aiv[6] << 8) | (unsigned int)aiv[7]; if (8 * (n - 1) >= ptext_len || ptext_len > 8 * n) { OPENSSL_cleanse(out, inlen); return 0; } padding_len = padded_len - ptext_len; if (CRYPTO_memcmp(out + ptext_len, zeros, padding_len) != 0) { OPENSSL_cleanse(out, inlen); return 0; } return ptext_len; }
modes
openssl/crypto/modes/wrap128.c
openssl
#include <openssl/ocsp.h> #include <openssl/http.h> #ifndef OPENSSL_NO_OCSP OSSL_HTTP_REQ_CTX *OCSP_sendreq_new(BIO *io, const char *path, const OCSP_REQUEST *req, int buf_size) { OSSL_HTTP_REQ_CTX *rctx = OSSL_HTTP_REQ_CTX_new(io, io, buf_size); if (rctx == NULL) return NULL; if (!OSSL_HTTP_REQ_CTX_set_request_line(rctx, 1 , NULL, NULL, path)) goto err; if (!OSSL_HTTP_REQ_CTX_set_expected(rctx, NULL , 1 , 0 , 0 )) goto err; if (req != NULL && !OSSL_HTTP_REQ_CTX_set1_req(rctx, "application/ocsp-request", ASN1_ITEM_rptr(OCSP_REQUEST), (const ASN1_VALUE *)req)) goto err; return rctx; err: OSSL_HTTP_REQ_CTX_free(rctx); return NULL; } OCSP_RESPONSE *OCSP_sendreq_bio(BIO *b, const char *path, OCSP_REQUEST *req) { OCSP_RESPONSE *resp = NULL; OSSL_HTTP_REQ_CTX *ctx; BIO *mem; ctx = OCSP_sendreq_new(b, path, req, 0 ); if (ctx == NULL) return NULL; mem = OSSL_HTTP_REQ_CTX_exchange(ctx); resp = (OCSP_RESPONSE *)ASN1_item_d2i_bio(ASN1_ITEM_rptr(OCSP_RESPONSE), mem, NULL); OSSL_HTTP_REQ_CTX_free(ctx); return resp; } #endif
ocsp
openssl/crypto/ocsp/ocsp_http.c
openssl
#include <openssl/bio.h> #include <openssl/err.h> #include <openssl/ocsp.h> #include "ocsp_local.h" #include "internal/cryptlib.h" #include <openssl/pem.h> static int ocsp_certid_print(BIO *bp, OCSP_CERTID *a, int indent) { BIO_printf(bp, "%*sCertificate ID:\n", indent, ""); indent += 2; BIO_printf(bp, "%*sHash Algorithm: ", indent, ""); i2a_ASN1_OBJECT(bp, a->hashAlgorithm.algorithm); BIO_printf(bp, "\n%*sIssuer Name Hash: ", indent, ""); i2a_ASN1_STRING(bp, &a->issuerNameHash, 0); BIO_printf(bp, "\n%*sIssuer Key Hash: ", indent, ""); i2a_ASN1_STRING(bp, &a->issuerKeyHash, 0); BIO_printf(bp, "\n%*sSerial Number: ", indent, ""); i2a_ASN1_INTEGER(bp, &a->serialNumber); BIO_printf(bp, "\n"); return 1; } typedef struct { long t; const char *m; } OCSP_TBLSTR; static const char *do_table2string(long s, const OCSP_TBLSTR *ts, size_t len) { size_t i; for (i = 0; i < len; i++, ts++) if (ts->t == s) return ts->m; return "(UNKNOWN)"; } #define table2string(s, tbl) do_table2string(s, tbl, OSSL_NELEM(tbl)) const char *OCSP_response_status_str(long s) { static const OCSP_TBLSTR rstat_tbl[] = { {OCSP_RESPONSE_STATUS_SUCCESSFUL, "successful"}, {OCSP_RESPONSE_STATUS_MALFORMEDREQUEST, "malformedrequest"}, {OCSP_RESPONSE_STATUS_INTERNALERROR, "internalerror"}, {OCSP_RESPONSE_STATUS_TRYLATER, "trylater"}, {OCSP_RESPONSE_STATUS_SIGREQUIRED, "sigrequired"}, {OCSP_RESPONSE_STATUS_UNAUTHORIZED, "unauthorized"} }; return table2string(s, rstat_tbl); } const char *OCSP_cert_status_str(long s) { static const OCSP_TBLSTR cstat_tbl[] = { {V_OCSP_CERTSTATUS_GOOD, "good"}, {V_OCSP_CERTSTATUS_REVOKED, "revoked"}, {V_OCSP_CERTSTATUS_UNKNOWN, "unknown"} }; return table2string(s, cstat_tbl); } const char *OCSP_crl_reason_str(long s) { static const OCSP_TBLSTR reason_tbl[] = { {OCSP_REVOKED_STATUS_UNSPECIFIED, "unspecified"}, {OCSP_REVOKED_STATUS_KEYCOMPROMISE, "keyCompromise"}, {OCSP_REVOKED_STATUS_CACOMPROMISE, "cACompromise"}, {OCSP_REVOKED_STATUS_AFFILIATIONCHANGED, "affiliationChanged"}, {OCSP_REVOKED_STATUS_SUPERSEDED, "superseded"}, {OCSP_REVOKED_STATUS_CESSATIONOFOPERATION, "cessationOfOperation"}, {OCSP_REVOKED_STATUS_CERTIFICATEHOLD, "certificateHold"}, {OCSP_REVOKED_STATUS_REMOVEFROMCRL, "removeFromCRL"}, {OCSP_REVOKED_STATUS_PRIVILEGEWITHDRAWN, "privilegeWithdrawn"}, {OCSP_REVOKED_STATUS_AACOMPROMISE, "aACompromise"} }; return table2string(s, reason_tbl); } int OCSP_REQUEST_print(BIO *bp, OCSP_REQUEST *o, unsigned long flags) { int i; long l; OCSP_CERTID *cid = NULL; OCSP_ONEREQ *one = NULL; OCSP_REQINFO *inf = &o->tbsRequest; OCSP_SIGNATURE *sig = o->optionalSignature; if (BIO_write(bp, "OCSP Request Data:\n", 19) <= 0) goto err; l = ASN1_INTEGER_get(inf->version); if (BIO_printf(bp, " Version: %lu (0x%lx)", l + 1, l) <= 0) goto err; if (inf->requestorName != NULL) { if (BIO_write(bp, "\n Requestor Name: ", 21) <= 0) goto err; GENERAL_NAME_print(bp, inf->requestorName); } if (BIO_write(bp, "\n Requestor List:\n", 21) <= 0) goto err; for (i = 0; i < sk_OCSP_ONEREQ_num(inf->requestList); i++) { one = sk_OCSP_ONEREQ_value(inf->requestList, i); cid = one->reqCert; ocsp_certid_print(bp, cid, 8); if (!X509V3_extensions_print(bp, "Request Single Extensions", one->singleRequestExtensions, flags, 8)) goto err; } if (!X509V3_extensions_print(bp, "Request Extensions", inf->requestExtensions, flags, 4)) goto err; if (sig) { X509_signature_print(bp, &sig->signatureAlgorithm, sig->signature); for (i = 0; i < sk_X509_num(sig->certs); i++) { X509_print(bp, sk_X509_value(sig->certs, i)); PEM_write_bio_X509(bp, sk_X509_value(sig->certs, i)); } } return 1; err: return 0; } int OCSP_RESPONSE_print(BIO *bp, OCSP_RESPONSE *o, unsigned long flags) { int i, ret = 0; long l; OCSP_CERTID *cid = NULL; OCSP_BASICRESP *br = NULL; OCSP_RESPID *rid = NULL; OCSP_RESPDATA *rd = NULL; OCSP_CERTSTATUS *cst = NULL; OCSP_REVOKEDINFO *rev = NULL; OCSP_SINGLERESP *single = NULL; OCSP_RESPBYTES *rb = o->responseBytes; if (BIO_puts(bp, "OCSP Response Data:\n") <= 0) goto err; l = ASN1_ENUMERATED_get(o->responseStatus); if (BIO_printf(bp, " OCSP Response Status: %s (0x%lx)\n", OCSP_response_status_str(l), l) <= 0) goto err; if (rb == NULL) return 1; if (BIO_puts(bp, " Response Type: ") <= 0) goto err; if (i2a_ASN1_OBJECT(bp, rb->responseType) <= 0) goto err; if (OBJ_obj2nid(rb->responseType) != NID_id_pkix_OCSP_basic) { BIO_puts(bp, " (unknown response type)\n"); return 1; } if ((br = OCSP_response_get1_basic(o)) == NULL) goto err; rd = &br->tbsResponseData; l = ASN1_INTEGER_get(rd->version); if (BIO_printf(bp, "\n Version: %lu (0x%lx)\n", l + 1, l) <= 0) goto err; if (BIO_puts(bp, " Responder Id: ") <= 0) goto err; rid = &rd->responderId; switch (rid->type) { case V_OCSP_RESPID_NAME: X509_NAME_print_ex(bp, rid->value.byName, 0, XN_FLAG_ONELINE); break; case V_OCSP_RESPID_KEY: i2a_ASN1_STRING(bp, rid->value.byKey, 0); break; } if (BIO_printf(bp, "\n Produced At: ") <= 0) goto err; if (!ASN1_GENERALIZEDTIME_print(bp, rd->producedAt)) goto err; if (BIO_printf(bp, "\n Responses:\n") <= 0) goto err; for (i = 0; i < sk_OCSP_SINGLERESP_num(rd->responses); i++) { if (!sk_OCSP_SINGLERESP_value(rd->responses, i)) continue; single = sk_OCSP_SINGLERESP_value(rd->responses, i); cid = single->certId; if (ocsp_certid_print(bp, cid, 4) <= 0) goto err; cst = single->certStatus; if (BIO_printf(bp, " Cert Status: %s", OCSP_cert_status_str(cst->type)) <= 0) goto err; if (cst->type == V_OCSP_CERTSTATUS_REVOKED) { rev = cst->value.revoked; if (BIO_printf(bp, "\n Revocation Time: ") <= 0) goto err; if (!ASN1_GENERALIZEDTIME_print(bp, rev->revocationTime)) goto err; if (rev->revocationReason) { l = ASN1_ENUMERATED_get(rev->revocationReason); if (BIO_printf(bp, "\n Revocation Reason: %s (0x%lx)", OCSP_crl_reason_str(l), l) <= 0) goto err; } } if (BIO_printf(bp, "\n This Update: ") <= 0) goto err; if (!ASN1_GENERALIZEDTIME_print(bp, single->thisUpdate)) goto err; if (single->nextUpdate) { if (BIO_printf(bp, "\n Next Update: ") <= 0) goto err; if (!ASN1_GENERALIZEDTIME_print(bp, single->nextUpdate)) goto err; } if (BIO_write(bp, "\n", 1) <= 0) goto err; if (!X509V3_extensions_print(bp, "Response Single Extensions", single->singleExtensions, flags, 8)) goto err; if (BIO_write(bp, "\n", 1) <= 0) goto err; } if (!X509V3_extensions_print(bp, "Response Extensions", rd->responseExtensions, flags, 4)) goto err; if (X509_signature_print(bp, &br->signatureAlgorithm, br->signature) <= 0) goto err; for (i = 0; i < sk_X509_num(br->certs); i++) { X509_print(bp, sk_X509_value(br->certs, i)); PEM_write_bio_X509(bp, sk_X509_value(br->certs, i)); } ret = 1; err: OCSP_BASICRESP_free(br); return ret; }
ocsp
openssl/crypto/ocsp/ocsp_prn.c
openssl
#include <stdio.h> #include "internal/cryptlib.h" #include <openssl/objects.h> #include <openssl/x509.h> #include <openssl/ocsp.h> #include "ocsp_local.h" #include <openssl/rand.h> #include <openssl/x509v3.h> int OCSP_REQUEST_get_ext_count(OCSP_REQUEST *x) { return X509v3_get_ext_count(x->tbsRequest.requestExtensions); } int OCSP_REQUEST_get_ext_by_NID(OCSP_REQUEST *x, int nid, int lastpos) { return (X509v3_get_ext_by_NID (x->tbsRequest.requestExtensions, nid, lastpos)); } int OCSP_REQUEST_get_ext_by_OBJ(OCSP_REQUEST *x, const ASN1_OBJECT *obj, int lastpos) { return (X509v3_get_ext_by_OBJ (x->tbsRequest.requestExtensions, obj, lastpos)); } int OCSP_REQUEST_get_ext_by_critical(OCSP_REQUEST *x, int crit, int lastpos) { return (X509v3_get_ext_by_critical (x->tbsRequest.requestExtensions, crit, lastpos)); } X509_EXTENSION *OCSP_REQUEST_get_ext(OCSP_REQUEST *x, int loc) { return X509v3_get_ext(x->tbsRequest.requestExtensions, loc); } X509_EXTENSION *OCSP_REQUEST_delete_ext(OCSP_REQUEST *x, int loc) { return X509v3_delete_ext(x->tbsRequest.requestExtensions, loc); } void *OCSP_REQUEST_get1_ext_d2i(OCSP_REQUEST *x, int nid, int *crit, int *idx) { return X509V3_get_d2i(x->tbsRequest.requestExtensions, nid, crit, idx); } int OCSP_REQUEST_add1_ext_i2d(OCSP_REQUEST *x, int nid, void *value, int crit, unsigned long flags) { return X509V3_add1_i2d(&x->tbsRequest.requestExtensions, nid, value, crit, flags); } int OCSP_REQUEST_add_ext(OCSP_REQUEST *x, X509_EXTENSION *ex, int loc) { return (X509v3_add_ext(&(x->tbsRequest.requestExtensions), ex, loc) != NULL); } int OCSP_ONEREQ_get_ext_count(OCSP_ONEREQ *x) { return X509v3_get_ext_count(x->singleRequestExtensions); } int OCSP_ONEREQ_get_ext_by_NID(OCSP_ONEREQ *x, int nid, int lastpos) { return X509v3_get_ext_by_NID(x->singleRequestExtensions, nid, lastpos); } int OCSP_ONEREQ_get_ext_by_OBJ(OCSP_ONEREQ *x, const ASN1_OBJECT *obj, int lastpos) { return X509v3_get_ext_by_OBJ(x->singleRequestExtensions, obj, lastpos); } int OCSP_ONEREQ_get_ext_by_critical(OCSP_ONEREQ *x, int crit, int lastpos) { return (X509v3_get_ext_by_critical (x->singleRequestExtensions, crit, lastpos)); } X509_EXTENSION *OCSP_ONEREQ_get_ext(OCSP_ONEREQ *x, int loc) { return X509v3_get_ext(x->singleRequestExtensions, loc); } X509_EXTENSION *OCSP_ONEREQ_delete_ext(OCSP_ONEREQ *x, int loc) { return X509v3_delete_ext(x->singleRequestExtensions, loc); } void *OCSP_ONEREQ_get1_ext_d2i(OCSP_ONEREQ *x, int nid, int *crit, int *idx) { return X509V3_get_d2i(x->singleRequestExtensions, nid, crit, idx); } int OCSP_ONEREQ_add1_ext_i2d(OCSP_ONEREQ *x, int nid, void *value, int crit, unsigned long flags) { return X509V3_add1_i2d(&x->singleRequestExtensions, nid, value, crit, flags); } int OCSP_ONEREQ_add_ext(OCSP_ONEREQ *x, X509_EXTENSION *ex, int loc) { return (X509v3_add_ext(&(x->singleRequestExtensions), ex, loc) != NULL); } int OCSP_BASICRESP_get_ext_count(OCSP_BASICRESP *x) { return X509v3_get_ext_count(x->tbsResponseData.responseExtensions); } int OCSP_BASICRESP_get_ext_by_NID(OCSP_BASICRESP *x, int nid, int lastpos) { return (X509v3_get_ext_by_NID (x->tbsResponseData.responseExtensions, nid, lastpos)); } int OCSP_BASICRESP_get_ext_by_OBJ(OCSP_BASICRESP *x, const ASN1_OBJECT *obj, int lastpos) { return (X509v3_get_ext_by_OBJ (x->tbsResponseData.responseExtensions, obj, lastpos)); } int OCSP_BASICRESP_get_ext_by_critical(OCSP_BASICRESP *x, int crit, int lastpos) { return (X509v3_get_ext_by_critical (x->tbsResponseData.responseExtensions, crit, lastpos)); } X509_EXTENSION *OCSP_BASICRESP_get_ext(OCSP_BASICRESP *x, int loc) { return X509v3_get_ext(x->tbsResponseData.responseExtensions, loc); } X509_EXTENSION *OCSP_BASICRESP_delete_ext(OCSP_BASICRESP *x, int loc) { return X509v3_delete_ext(x->tbsResponseData.responseExtensions, loc); } void *OCSP_BASICRESP_get1_ext_d2i(OCSP_BASICRESP *x, int nid, int *crit, int *idx) { return X509V3_get_d2i(x->tbsResponseData.responseExtensions, nid, crit, idx); } int OCSP_BASICRESP_add1_ext_i2d(OCSP_BASICRESP *x, int nid, void *value, int crit, unsigned long flags) { return X509V3_add1_i2d(&x->tbsResponseData.responseExtensions, nid, value, crit, flags); } int OCSP_BASICRESP_add_ext(OCSP_BASICRESP *x, X509_EXTENSION *ex, int loc) { return (X509v3_add_ext(&(x->tbsResponseData.responseExtensions), ex, loc) != NULL); } int OCSP_SINGLERESP_get_ext_count(OCSP_SINGLERESP *x) { return X509v3_get_ext_count(x->singleExtensions); } int OCSP_SINGLERESP_get_ext_by_NID(OCSP_SINGLERESP *x, int nid, int lastpos) { return X509v3_get_ext_by_NID(x->singleExtensions, nid, lastpos); } int OCSP_SINGLERESP_get_ext_by_OBJ(OCSP_SINGLERESP *x, const ASN1_OBJECT *obj, int lastpos) { return X509v3_get_ext_by_OBJ(x->singleExtensions, obj, lastpos); } int OCSP_SINGLERESP_get_ext_by_critical(OCSP_SINGLERESP *x, int crit, int lastpos) { return X509v3_get_ext_by_critical(x->singleExtensions, crit, lastpos); } X509_EXTENSION *OCSP_SINGLERESP_get_ext(OCSP_SINGLERESP *x, int loc) { return X509v3_get_ext(x->singleExtensions, loc); } X509_EXTENSION *OCSP_SINGLERESP_delete_ext(OCSP_SINGLERESP *x, int loc) { return X509v3_delete_ext(x->singleExtensions, loc); } void *OCSP_SINGLERESP_get1_ext_d2i(OCSP_SINGLERESP *x, int nid, int *crit, int *idx) { return X509V3_get_d2i(x->singleExtensions, nid, crit, idx); } int OCSP_SINGLERESP_add1_ext_i2d(OCSP_SINGLERESP *x, int nid, void *value, int crit, unsigned long flags) { return X509V3_add1_i2d(&x->singleExtensions, nid, value, crit, flags); } int OCSP_SINGLERESP_add_ext(OCSP_SINGLERESP *x, X509_EXTENSION *ex, int loc) { return (X509v3_add_ext(&(x->singleExtensions), ex, loc) != NULL); } static int ocsp_add1_nonce(STACK_OF(X509_EXTENSION) **exts, unsigned char *val, int len) { unsigned char *tmpval; ASN1_OCTET_STRING os; int ret = 0; if (len <= 0) len = OCSP_DEFAULT_NONCE_LENGTH; os.length = ASN1_object_size(0, len, V_ASN1_OCTET_STRING); if (os.length < 0) return 0; os.data = OPENSSL_malloc(os.length); if (os.data == NULL) goto err; tmpval = os.data; ASN1_put_object(&tmpval, 0, len, V_ASN1_OCTET_STRING, V_ASN1_UNIVERSAL); if (val) memcpy(tmpval, val, len); else if (RAND_bytes(tmpval, len) <= 0) goto err; if (X509V3_add1_i2d(exts, NID_id_pkix_OCSP_Nonce, &os, 0, X509V3_ADD_REPLACE) <= 0) goto err; ret = 1; err: OPENSSL_free(os.data); return ret; } int OCSP_request_add1_nonce(OCSP_REQUEST *req, unsigned char *val, int len) { return ocsp_add1_nonce(&req->tbsRequest.requestExtensions, val, len); } int OCSP_basic_add1_nonce(OCSP_BASICRESP *resp, unsigned char *val, int len) { return ocsp_add1_nonce(&resp->tbsResponseData.responseExtensions, val, len); } int OCSP_check_nonce(OCSP_REQUEST *req, OCSP_BASICRESP *bs) { int req_idx, resp_idx; X509_EXTENSION *req_ext, *resp_ext; req_idx = OCSP_REQUEST_get_ext_by_NID(req, NID_id_pkix_OCSP_Nonce, -1); resp_idx = OCSP_BASICRESP_get_ext_by_NID(bs, NID_id_pkix_OCSP_Nonce, -1); if ((req_idx < 0) && (resp_idx < 0)) return 2; if ((req_idx >= 0) && (resp_idx < 0)) return -1; if ((req_idx < 0) && (resp_idx >= 0)) return 3; req_ext = OCSP_REQUEST_get_ext(req, req_idx); resp_ext = OCSP_BASICRESP_get_ext(bs, resp_idx); if (ASN1_OCTET_STRING_cmp(X509_EXTENSION_get_data(req_ext), X509_EXTENSION_get_data(resp_ext))) return 0; return 1; } int OCSP_copy_nonce(OCSP_BASICRESP *resp, OCSP_REQUEST *req) { X509_EXTENSION *req_ext; int req_idx; req_idx = OCSP_REQUEST_get_ext_by_NID(req, NID_id_pkix_OCSP_Nonce, -1); if (req_idx < 0) return 2; req_ext = OCSP_REQUEST_get_ext(req, req_idx); return OCSP_BASICRESP_add_ext(resp, req_ext, -1); } X509_EXTENSION *OCSP_crlID_new(const char *url, long *n, char *tim) { X509_EXTENSION *x = NULL; OCSP_CRLID *cid = NULL; if ((cid = OCSP_CRLID_new()) == NULL) goto err; if (url) { if ((cid->crlUrl = ASN1_IA5STRING_new()) == NULL) goto err; if (!(ASN1_STRING_set(cid->crlUrl, url, -1))) goto err; } if (n) { if ((cid->crlNum = ASN1_INTEGER_new()) == NULL) goto err; if (!(ASN1_INTEGER_set(cid->crlNum, *n))) goto err; } if (tim) { if ((cid->crlTime = ASN1_GENERALIZEDTIME_new()) == NULL) goto err; if (!(ASN1_GENERALIZEDTIME_set_string(cid->crlTime, tim))) goto err; } x = X509V3_EXT_i2d(NID_id_pkix_OCSP_CrlID, 0, cid); err: OCSP_CRLID_free(cid); return x; } X509_EXTENSION *OCSP_accept_responses_new(char **oids) { int nid; STACK_OF(ASN1_OBJECT) *sk = NULL; ASN1_OBJECT *o = NULL; X509_EXTENSION *x = NULL; if ((sk = sk_ASN1_OBJECT_new_null()) == NULL) goto err; while (oids && *oids) { if ((nid = OBJ_txt2nid(*oids)) != NID_undef && (o = OBJ_nid2obj(nid))) sk_ASN1_OBJECT_push(sk, o); oids++; } x = X509V3_EXT_i2d(NID_id_pkix_OCSP_acceptableResponses, 0, sk); err: sk_ASN1_OBJECT_pop_free(sk, ASN1_OBJECT_free); return x; } X509_EXTENSION *OCSP_archive_cutoff_new(char *tim) { X509_EXTENSION *x = NULL; ASN1_GENERALIZEDTIME *gt = NULL; if ((gt = ASN1_GENERALIZEDTIME_new()) == NULL) goto err; if (!(ASN1_GENERALIZEDTIME_set_string(gt, tim))) goto err; x = X509V3_EXT_i2d(NID_id_pkix_OCSP_archiveCutoff, 0, gt); err: ASN1_GENERALIZEDTIME_free(gt); return x; } X509_EXTENSION *OCSP_url_svcloc_new(const X509_NAME *issuer, const char **urls) { X509_EXTENSION *x = NULL; ASN1_IA5STRING *ia5 = NULL; OCSP_SERVICELOC *sloc = NULL; ACCESS_DESCRIPTION *ad = NULL; if ((sloc = OCSP_SERVICELOC_new()) == NULL) goto err; X509_NAME_free(sloc->issuer); if ((sloc->issuer = X509_NAME_dup(issuer)) == NULL) goto err; if (urls && *urls && (sloc->locator = sk_ACCESS_DESCRIPTION_new_null()) == NULL) goto err; while (urls && *urls) { if ((ad = ACCESS_DESCRIPTION_new()) == NULL) goto err; if ((ad->method = OBJ_nid2obj(NID_ad_OCSP)) == NULL) goto err; if ((ia5 = ASN1_IA5STRING_new()) == NULL) goto err; if (!ASN1_STRING_set((ASN1_STRING *)ia5, *urls, -1)) goto err; ad->location->type = GEN_URI; ad->location->d.ia5 = ia5; ia5 = NULL; if (!sk_ACCESS_DESCRIPTION_push(sloc->locator, ad)) goto err; ad = NULL; urls++; } x = X509V3_EXT_i2d(NID_id_pkix_OCSP_serviceLocator, 0, sloc); err: ASN1_IA5STRING_free(ia5); ACCESS_DESCRIPTION_free(ad); OCSP_SERVICELOC_free(sloc); return x; }
ocsp
openssl/crypto/ocsp/ocsp_ext.c
openssl
#include <openssl/asn1.h> #include <openssl/asn1t.h> #include <openssl/ocsp.h> #include "ocsp_local.h" ASN1_SEQUENCE(OCSP_SIGNATURE) = { ASN1_EMBED(OCSP_SIGNATURE, signatureAlgorithm, X509_ALGOR), ASN1_SIMPLE(OCSP_SIGNATURE, signature, ASN1_BIT_STRING), ASN1_EXP_SEQUENCE_OF_OPT(OCSP_SIGNATURE, certs, X509, 0) } ASN1_SEQUENCE_END(OCSP_SIGNATURE) IMPLEMENT_ASN1_FUNCTIONS(OCSP_SIGNATURE) ASN1_SEQUENCE(OCSP_CERTID) = { ASN1_EMBED(OCSP_CERTID, hashAlgorithm, X509_ALGOR), ASN1_EMBED(OCSP_CERTID, issuerNameHash, ASN1_OCTET_STRING), ASN1_EMBED(OCSP_CERTID, issuerKeyHash, ASN1_OCTET_STRING), ASN1_EMBED(OCSP_CERTID, serialNumber, ASN1_INTEGER) } ASN1_SEQUENCE_END(OCSP_CERTID) IMPLEMENT_ASN1_FUNCTIONS(OCSP_CERTID) ASN1_SEQUENCE(OCSP_ONEREQ) = { ASN1_SIMPLE(OCSP_ONEREQ, reqCert, OCSP_CERTID), ASN1_EXP_SEQUENCE_OF_OPT(OCSP_ONEREQ, singleRequestExtensions, X509_EXTENSION, 0) } ASN1_SEQUENCE_END(OCSP_ONEREQ) IMPLEMENT_ASN1_FUNCTIONS(OCSP_ONEREQ) ASN1_SEQUENCE(OCSP_REQINFO) = { ASN1_EXP_OPT(OCSP_REQINFO, version, ASN1_INTEGER, 0), ASN1_EXP_OPT(OCSP_REQINFO, requestorName, GENERAL_NAME, 1), ASN1_SEQUENCE_OF(OCSP_REQINFO, requestList, OCSP_ONEREQ), ASN1_EXP_SEQUENCE_OF_OPT(OCSP_REQINFO, requestExtensions, X509_EXTENSION, 2) } ASN1_SEQUENCE_END(OCSP_REQINFO) IMPLEMENT_ASN1_FUNCTIONS(OCSP_REQINFO) ASN1_SEQUENCE(OCSP_REQUEST) = { ASN1_EMBED(OCSP_REQUEST, tbsRequest, OCSP_REQINFO), ASN1_EXP_OPT(OCSP_REQUEST, optionalSignature, OCSP_SIGNATURE, 0) } ASN1_SEQUENCE_END(OCSP_REQUEST) IMPLEMENT_ASN1_FUNCTIONS(OCSP_REQUEST) ASN1_SEQUENCE(OCSP_RESPBYTES) = { ASN1_SIMPLE(OCSP_RESPBYTES, responseType, ASN1_OBJECT), ASN1_SIMPLE(OCSP_RESPBYTES, response, ASN1_OCTET_STRING) } ASN1_SEQUENCE_END(OCSP_RESPBYTES) IMPLEMENT_ASN1_FUNCTIONS(OCSP_RESPBYTES) ASN1_SEQUENCE(OCSP_RESPONSE) = { ASN1_SIMPLE(OCSP_RESPONSE, responseStatus, ASN1_ENUMERATED), ASN1_EXP_OPT(OCSP_RESPONSE, responseBytes, OCSP_RESPBYTES, 0) } ASN1_SEQUENCE_END(OCSP_RESPONSE) IMPLEMENT_ASN1_FUNCTIONS(OCSP_RESPONSE) ASN1_CHOICE(OCSP_RESPID) = { ASN1_EXP(OCSP_RESPID, value.byName, X509_NAME, 1), ASN1_EXP(OCSP_RESPID, value.byKey, ASN1_OCTET_STRING, 2) } ASN1_CHOICE_END(OCSP_RESPID) IMPLEMENT_ASN1_FUNCTIONS(OCSP_RESPID) ASN1_SEQUENCE(OCSP_REVOKEDINFO) = { ASN1_SIMPLE(OCSP_REVOKEDINFO, revocationTime, ASN1_GENERALIZEDTIME), ASN1_EXP_OPT(OCSP_REVOKEDINFO, revocationReason, ASN1_ENUMERATED, 0) } ASN1_SEQUENCE_END(OCSP_REVOKEDINFO) IMPLEMENT_ASN1_FUNCTIONS(OCSP_REVOKEDINFO) ASN1_CHOICE(OCSP_CERTSTATUS) = { ASN1_IMP(OCSP_CERTSTATUS, value.good, ASN1_NULL, 0), ASN1_IMP(OCSP_CERTSTATUS, value.revoked, OCSP_REVOKEDINFO, 1), ASN1_IMP(OCSP_CERTSTATUS, value.unknown, ASN1_NULL, 2) } ASN1_CHOICE_END(OCSP_CERTSTATUS) IMPLEMENT_ASN1_FUNCTIONS(OCSP_CERTSTATUS) ASN1_SEQUENCE(OCSP_SINGLERESP) = { ASN1_SIMPLE(OCSP_SINGLERESP, certId, OCSP_CERTID), ASN1_SIMPLE(OCSP_SINGLERESP, certStatus, OCSP_CERTSTATUS), ASN1_SIMPLE(OCSP_SINGLERESP, thisUpdate, ASN1_GENERALIZEDTIME), ASN1_EXP_OPT(OCSP_SINGLERESP, nextUpdate, ASN1_GENERALIZEDTIME, 0), ASN1_EXP_SEQUENCE_OF_OPT(OCSP_SINGLERESP, singleExtensions, X509_EXTENSION, 1) } ASN1_SEQUENCE_END(OCSP_SINGLERESP) IMPLEMENT_ASN1_FUNCTIONS(OCSP_SINGLERESP) ASN1_SEQUENCE(OCSP_RESPDATA) = { ASN1_EXP_OPT(OCSP_RESPDATA, version, ASN1_INTEGER, 0), ASN1_EMBED(OCSP_RESPDATA, responderId, OCSP_RESPID), ASN1_SIMPLE(OCSP_RESPDATA, producedAt, ASN1_GENERALIZEDTIME), ASN1_SEQUENCE_OF(OCSP_RESPDATA, responses, OCSP_SINGLERESP), ASN1_EXP_SEQUENCE_OF_OPT(OCSP_RESPDATA, responseExtensions, X509_EXTENSION, 1) } ASN1_SEQUENCE_END(OCSP_RESPDATA) IMPLEMENT_ASN1_FUNCTIONS(OCSP_RESPDATA) ASN1_SEQUENCE(OCSP_BASICRESP) = { ASN1_EMBED(OCSP_BASICRESP, tbsResponseData, OCSP_RESPDATA), ASN1_EMBED(OCSP_BASICRESP, signatureAlgorithm, X509_ALGOR), ASN1_SIMPLE(OCSP_BASICRESP, signature, ASN1_BIT_STRING), ASN1_EXP_SEQUENCE_OF_OPT(OCSP_BASICRESP, certs, X509, 0) } ASN1_SEQUENCE_END(OCSP_BASICRESP) IMPLEMENT_ASN1_FUNCTIONS(OCSP_BASICRESP) ASN1_SEQUENCE(OCSP_CRLID) = { ASN1_EXP_OPT(OCSP_CRLID, crlUrl, ASN1_IA5STRING, 0), ASN1_EXP_OPT(OCSP_CRLID, crlNum, ASN1_INTEGER, 1), ASN1_EXP_OPT(OCSP_CRLID, crlTime, ASN1_GENERALIZEDTIME, 2) } ASN1_SEQUENCE_END(OCSP_CRLID) IMPLEMENT_ASN1_FUNCTIONS(OCSP_CRLID) ASN1_SEQUENCE(OCSP_SERVICELOC) = { ASN1_SIMPLE(OCSP_SERVICELOC, issuer, X509_NAME), ASN1_SEQUENCE_OF_OPT(OCSP_SERVICELOC, locator, ACCESS_DESCRIPTION) } ASN1_SEQUENCE_END(OCSP_SERVICELOC) IMPLEMENT_ASN1_FUNCTIONS(OCSP_SERVICELOC)
ocsp
openssl/crypto/ocsp/ocsp_asn.c
openssl
#include <stdio.h> #include "internal/cryptlib.h" #include <openssl/objects.h> #include <openssl/x509.h> #include <openssl/pem.h> #include <openssl/x509v3.h> #include <openssl/ocsp.h> #include "ocsp_local.h" #include <openssl/asn1t.h> OCSP_CERTID *OCSP_cert_to_id(const EVP_MD *dgst, const X509 *subject, const X509 *issuer) { const X509_NAME *iname; const ASN1_INTEGER *serial; ASN1_BIT_STRING *ikey; if (!dgst) dgst = EVP_sha1(); if (subject) { iname = X509_get_issuer_name(subject); serial = X509_get0_serialNumber(subject); } else { iname = X509_get_subject_name(issuer); serial = NULL; } ikey = X509_get0_pubkey_bitstr(issuer); return OCSP_cert_id_new(dgst, iname, ikey, serial); } OCSP_CERTID *OCSP_cert_id_new(const EVP_MD *dgst, const X509_NAME *issuerName, const ASN1_BIT_STRING *issuerKey, const ASN1_INTEGER *serialNumber) { int nid; unsigned int i; X509_ALGOR *alg; OCSP_CERTID *cid = NULL; unsigned char md[EVP_MAX_MD_SIZE]; if ((cid = OCSP_CERTID_new()) == NULL) goto err; alg = &cid->hashAlgorithm; ASN1_OBJECT_free(alg->algorithm); if ((nid = EVP_MD_get_type(dgst)) == NID_undef) { ERR_raise(ERR_LIB_OCSP, OCSP_R_UNKNOWN_NID); goto err; } if ((alg->algorithm = OBJ_nid2obj(nid)) == NULL) goto err; if ((alg->parameter = ASN1_TYPE_new()) == NULL) goto err; alg->parameter->type = V_ASN1_NULL; if (!X509_NAME_digest(issuerName, dgst, md, &i)) goto digerr; if (!(ASN1_OCTET_STRING_set(&cid->issuerNameHash, md, i))) goto err; if (!EVP_Digest(issuerKey->data, issuerKey->length, md, &i, dgst, NULL)) goto err; if (!(ASN1_OCTET_STRING_set(&cid->issuerKeyHash, md, i))) goto err; if (serialNumber) { if (ASN1_STRING_copy(&cid->serialNumber, serialNumber) == 0) goto err; } return cid; digerr: ERR_raise(ERR_LIB_OCSP, OCSP_R_DIGEST_ERR); err: OCSP_CERTID_free(cid); return NULL; } int OCSP_id_issuer_cmp(const OCSP_CERTID *a, const OCSP_CERTID *b) { int ret; ret = OBJ_cmp(a->hashAlgorithm.algorithm, b->hashAlgorithm.algorithm); if (ret) return ret; ret = ASN1_OCTET_STRING_cmp(&a->issuerNameHash, &b->issuerNameHash); if (ret) return ret; return ASN1_OCTET_STRING_cmp(&a->issuerKeyHash, &b->issuerKeyHash); } int OCSP_id_cmp(const OCSP_CERTID *a, const OCSP_CERTID *b) { int ret; ret = OCSP_id_issuer_cmp(a, b); if (ret) return ret; return ASN1_INTEGER_cmp(&a->serialNumber, &b->serialNumber); } IMPLEMENT_ASN1_DUP_FUNCTION(OCSP_CERTID)
ocsp
openssl/crypto/ocsp/ocsp_lib.c
openssl
#include <stdio.h> #include <time.h> #include "internal/cryptlib.h" #include <openssl/asn1.h> #include <openssl/objects.h> #include <openssl/x509.h> #include <openssl/pem.h> #include <openssl/x509v3.h> #include <openssl/ocsp.h> #include "ocsp_local.h" OCSP_ONEREQ *OCSP_request_add0_id(OCSP_REQUEST *req, OCSP_CERTID *cid) { OCSP_ONEREQ *one = NULL; if ((one = OCSP_ONEREQ_new()) == NULL) return NULL; OCSP_CERTID_free(one->reqCert); one->reqCert = cid; if (req && !sk_OCSP_ONEREQ_push(req->tbsRequest.requestList, one)) { one->reqCert = NULL; OCSP_ONEREQ_free(one); return NULL; } return one; } int OCSP_request_set1_name(OCSP_REQUEST *req, const X509_NAME *nm) { GENERAL_NAME *gen = GENERAL_NAME_new(); if (gen == NULL) return 0; if (!X509_NAME_set(&gen->d.directoryName, nm)) { GENERAL_NAME_free(gen); return 0; } gen->type = GEN_DIRNAME; GENERAL_NAME_free(req->tbsRequest.requestorName); req->tbsRequest.requestorName = gen; return 1; } int OCSP_request_add1_cert(OCSP_REQUEST *req, X509 *cert) { if (req->optionalSignature == NULL && (req->optionalSignature = OCSP_SIGNATURE_new()) == NULL) return 0; if (cert == NULL) return 1; return ossl_x509_add_cert_new(&req->optionalSignature->certs, cert, X509_ADD_FLAG_UP_REF); } int OCSP_request_sign(OCSP_REQUEST *req, X509 *signer, EVP_PKEY *key, const EVP_MD *dgst, STACK_OF(X509) *certs, unsigned long flags) { if (!OCSP_request_set1_name(req, X509_get_subject_name(signer))) goto err; if ((req->optionalSignature = OCSP_SIGNATURE_new()) == NULL) goto err; if (key != NULL) { if (!X509_check_private_key(signer, key)) { ERR_raise(ERR_LIB_OCSP, OCSP_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE); goto err; } if (!OCSP_REQUEST_sign(req, key, dgst, signer->libctx, signer->propq)) goto err; } if ((flags & OCSP_NOCERTS) == 0) { if (!OCSP_request_add1_cert(req, signer) || !X509_add_certs(req->optionalSignature->certs, certs, X509_ADD_FLAG_UP_REF)) goto err; } return 1; err: OCSP_SIGNATURE_free(req->optionalSignature); req->optionalSignature = NULL; return 0; } int OCSP_response_status(OCSP_RESPONSE *resp) { return ASN1_ENUMERATED_get(resp->responseStatus); } OCSP_BASICRESP *OCSP_response_get1_basic(OCSP_RESPONSE *resp) { OCSP_RESPBYTES *rb = resp->responseBytes; if (rb == NULL) { ERR_raise(ERR_LIB_OCSP, OCSP_R_NO_RESPONSE_DATA); return NULL; } if (OBJ_obj2nid(rb->responseType) != NID_id_pkix_OCSP_basic) { ERR_raise(ERR_LIB_OCSP, OCSP_R_NOT_BASIC_RESPONSE); return NULL; } return ASN1_item_unpack(rb->response, ASN1_ITEM_rptr(OCSP_BASICRESP)); } const ASN1_OCTET_STRING *OCSP_resp_get0_signature(const OCSP_BASICRESP *bs) { return bs->signature; } const X509_ALGOR *OCSP_resp_get0_tbs_sigalg(const OCSP_BASICRESP *bs) { return &bs->signatureAlgorithm; } const OCSP_RESPDATA *OCSP_resp_get0_respdata(const OCSP_BASICRESP *bs) { return &bs->tbsResponseData; } int OCSP_resp_count(OCSP_BASICRESP *bs) { if (bs == NULL) return -1; return sk_OCSP_SINGLERESP_num(bs->tbsResponseData.responses); } OCSP_SINGLERESP *OCSP_resp_get0(OCSP_BASICRESP *bs, int idx) { if (bs == NULL) return NULL; return sk_OCSP_SINGLERESP_value(bs->tbsResponseData.responses, idx); } const ASN1_GENERALIZEDTIME *OCSP_resp_get0_produced_at(const OCSP_BASICRESP *bs) { return bs->tbsResponseData.producedAt; } const STACK_OF(X509) *OCSP_resp_get0_certs(const OCSP_BASICRESP *bs) { return bs->certs; } int OCSP_resp_get0_id(const OCSP_BASICRESP *bs, const ASN1_OCTET_STRING **pid, const X509_NAME **pname) { const OCSP_RESPID *rid = &bs->tbsResponseData.responderId; if (rid->type == V_OCSP_RESPID_NAME) { *pname = rid->value.byName; *pid = NULL; } else if (rid->type == V_OCSP_RESPID_KEY) { *pid = rid->value.byKey; *pname = NULL; } else { return 0; } return 1; } int OCSP_resp_get1_id(const OCSP_BASICRESP *bs, ASN1_OCTET_STRING **pid, X509_NAME **pname) { const OCSP_RESPID *rid = &bs->tbsResponseData.responderId; if (rid->type == V_OCSP_RESPID_NAME) { *pname = X509_NAME_dup(rid->value.byName); *pid = NULL; } else if (rid->type == V_OCSP_RESPID_KEY) { *pid = ASN1_OCTET_STRING_dup(rid->value.byKey); *pname = NULL; } else { return 0; } if (*pname == NULL && *pid == NULL) return 0; return 1; } int OCSP_resp_find(OCSP_BASICRESP *bs, OCSP_CERTID *id, int last) { int i; STACK_OF(OCSP_SINGLERESP) *sresp; OCSP_SINGLERESP *single; if (bs == NULL) return -1; if (last < 0) last = 0; else last++; sresp = bs->tbsResponseData.responses; for (i = last; i < sk_OCSP_SINGLERESP_num(sresp); i++) { single = sk_OCSP_SINGLERESP_value(sresp, i); if (!OCSP_id_cmp(id, single->certId)) return i; } return -1; } int OCSP_single_get0_status(OCSP_SINGLERESP *single, int *reason, ASN1_GENERALIZEDTIME **revtime, ASN1_GENERALIZEDTIME **thisupd, ASN1_GENERALIZEDTIME **nextupd) { int ret; OCSP_CERTSTATUS *cst; if (single == NULL) return -1; cst = single->certStatus; ret = cst->type; if (ret == V_OCSP_CERTSTATUS_REVOKED) { OCSP_REVOKEDINFO *rev = cst->value.revoked; if (revtime) *revtime = rev->revocationTime; if (reason) { if (rev->revocationReason) *reason = ASN1_ENUMERATED_get(rev->revocationReason); else *reason = -1; } } if (thisupd != NULL) *thisupd = single->thisUpdate; if (nextupd != NULL) *nextupd = single->nextUpdate; return ret; } int OCSP_resp_find_status(OCSP_BASICRESP *bs, OCSP_CERTID *id, int *status, int *reason, ASN1_GENERALIZEDTIME **revtime, ASN1_GENERALIZEDTIME **thisupd, ASN1_GENERALIZEDTIME **nextupd) { int i = OCSP_resp_find(bs, id, -1); OCSP_SINGLERESP *single; if (i < 0) return 0; single = OCSP_resp_get0(bs, i); i = OCSP_single_get0_status(single, reason, revtime, thisupd, nextupd); if (status != NULL) *status = i; return 1; } int OCSP_check_validity(ASN1_GENERALIZEDTIME *thisupd, ASN1_GENERALIZEDTIME *nextupd, long nsec, long maxsec) { int ret = 1; time_t t_now, t_tmp; time(&t_now); if (!ASN1_GENERALIZEDTIME_check(thisupd)) { ERR_raise(ERR_LIB_OCSP, OCSP_R_ERROR_IN_THISUPDATE_FIELD); ret = 0; } else { t_tmp = t_now + nsec; if (X509_cmp_time(thisupd, &t_tmp) > 0) { ERR_raise(ERR_LIB_OCSP, OCSP_R_STATUS_NOT_YET_VALID); ret = 0; } if (maxsec >= 0) { t_tmp = t_now - maxsec; if (X509_cmp_time(thisupd, &t_tmp) < 0) { ERR_raise(ERR_LIB_OCSP, OCSP_R_STATUS_TOO_OLD); ret = 0; } } } if (nextupd == NULL) return ret; if (!ASN1_GENERALIZEDTIME_check(nextupd)) { ERR_raise(ERR_LIB_OCSP, OCSP_R_ERROR_IN_NEXTUPDATE_FIELD); ret = 0; } else { t_tmp = t_now - nsec; if (X509_cmp_time(nextupd, &t_tmp) < 0) { ERR_raise(ERR_LIB_OCSP, OCSP_R_STATUS_EXPIRED); ret = 0; } } if (ASN1_STRING_cmp(nextupd, thisupd) < 0) { ERR_raise(ERR_LIB_OCSP, OCSP_R_NEXTUPDATE_BEFORE_THISUPDATE); ret = 0; } return ret; } const OCSP_CERTID *OCSP_SINGLERESP_get0_id(const OCSP_SINGLERESP *single) { return single->certId; }
ocsp
openssl/crypto/ocsp/ocsp_cl.c
openssl
#include <stdio.h> #include "internal/cryptlib.h" #include <openssl/conf.h> #include <openssl/asn1.h> #include <openssl/ocsp.h> #include "ocsp_local.h" #include <openssl/x509v3.h> #include "../x509/ext_dat.h" static int i2r_ocsp_crlid(const X509V3_EXT_METHOD *method, void *nonce, BIO *out, int indent); static int i2r_ocsp_acutoff(const X509V3_EXT_METHOD *method, void *nonce, BIO *out, int indent); static int i2r_object(const X509V3_EXT_METHOD *method, void *obj, BIO *out, int indent); static void *ocsp_nonce_new(void); static int i2d_ocsp_nonce(const void *a, unsigned char **pp); static void *d2i_ocsp_nonce(void *a, const unsigned char **pp, long length); static void ocsp_nonce_free(void *a); static int i2r_ocsp_nonce(const X509V3_EXT_METHOD *method, void *nonce, BIO *out, int indent); static int i2r_ocsp_nocheck(const X509V3_EXT_METHOD *method, void *nocheck, BIO *out, int indent); static void *s2i_ocsp_nocheck(const X509V3_EXT_METHOD *method, X509V3_CTX *ctx, const char *str); static int i2r_ocsp_serviceloc(const X509V3_EXT_METHOD *method, void *in, BIO *bp, int ind); const X509V3_EXT_METHOD ossl_v3_ocsp_crlid = { NID_id_pkix_OCSP_CrlID, 0, ASN1_ITEM_ref(OCSP_CRLID), 0, 0, 0, 0, 0, 0, 0, 0, i2r_ocsp_crlid, 0, NULL }; const X509V3_EXT_METHOD ossl_v3_ocsp_acutoff = { NID_id_pkix_OCSP_archiveCutoff, 0, ASN1_ITEM_ref(ASN1_GENERALIZEDTIME), 0, 0, 0, 0, 0, 0, 0, 0, i2r_ocsp_acutoff, 0, NULL }; const X509V3_EXT_METHOD ossl_v3_crl_invdate = { NID_invalidity_date, 0, ASN1_ITEM_ref(ASN1_GENERALIZEDTIME), 0, 0, 0, 0, 0, 0, 0, 0, i2r_ocsp_acutoff, 0, NULL }; const X509V3_EXT_METHOD ossl_v3_crl_hold = { NID_hold_instruction_code, 0, ASN1_ITEM_ref(ASN1_OBJECT), 0, 0, 0, 0, 0, 0, 0, 0, i2r_object, 0, NULL }; const X509V3_EXT_METHOD ossl_v3_ocsp_nonce = { NID_id_pkix_OCSP_Nonce, 0, NULL, ocsp_nonce_new, ocsp_nonce_free, d2i_ocsp_nonce, i2d_ocsp_nonce, 0, 0, 0, 0, i2r_ocsp_nonce, 0, NULL }; const X509V3_EXT_METHOD ossl_v3_ocsp_nocheck = { NID_id_pkix_OCSP_noCheck, 0, ASN1_ITEM_ref(ASN1_NULL), 0, 0, 0, 0, 0, s2i_ocsp_nocheck, 0, 0, i2r_ocsp_nocheck, 0, NULL }; const X509V3_EXT_METHOD ossl_v3_ocsp_serviceloc = { NID_id_pkix_OCSP_serviceLocator, 0, ASN1_ITEM_ref(OCSP_SERVICELOC), 0, 0, 0, 0, 0, 0, 0, 0, i2r_ocsp_serviceloc, 0, NULL }; static int i2r_ocsp_crlid(const X509V3_EXT_METHOD *method, void *in, BIO *bp, int ind) { OCSP_CRLID *a = in; if (a->crlUrl) { if (BIO_printf(bp, "%*scrlUrl: ", ind, "") <= 0) goto err; if (!ASN1_STRING_print(bp, (ASN1_STRING *)a->crlUrl)) goto err; if (BIO_write(bp, "\n", 1) <= 0) goto err; } if (a->crlNum) { if (BIO_printf(bp, "%*scrlNum: ", ind, "") <= 0) goto err; if (i2a_ASN1_INTEGER(bp, a->crlNum) <= 0) goto err; if (BIO_write(bp, "\n", 1) <= 0) goto err; } if (a->crlTime) { if (BIO_printf(bp, "%*scrlTime: ", ind, "") <= 0) goto err; if (!ASN1_GENERALIZEDTIME_print(bp, a->crlTime)) goto err; if (BIO_write(bp, "\n", 1) <= 0) goto err; } return 1; err: return 0; } static int i2r_ocsp_acutoff(const X509V3_EXT_METHOD *method, void *cutoff, BIO *bp, int ind) { if (BIO_printf(bp, "%*s", ind, "") <= 0) return 0; if (!ASN1_GENERALIZEDTIME_print(bp, cutoff)) return 0; return 1; } static int i2r_object(const X509V3_EXT_METHOD *method, void *oid, BIO *bp, int ind) { if (BIO_printf(bp, "%*s", ind, "") <= 0) return 0; if (i2a_ASN1_OBJECT(bp, oid) <= 0) return 0; return 1; } static void *ocsp_nonce_new(void) { return ASN1_OCTET_STRING_new(); } static int i2d_ocsp_nonce(const void *a, unsigned char **pp) { const ASN1_OCTET_STRING *os = a; if (pp) { memcpy(*pp, os->data, os->length); *pp += os->length; } return os->length; } static void *d2i_ocsp_nonce(void *a, const unsigned char **pp, long length) { ASN1_OCTET_STRING *os, **pos; pos = a; if (pos == NULL || *pos == NULL) { os = ASN1_OCTET_STRING_new(); if (os == NULL) goto err; } else { os = *pos; } if (!ASN1_OCTET_STRING_set(os, *pp, length)) goto err; *pp += length; if (pos) *pos = os; return os; err: if ((pos == NULL) || (*pos != os)) ASN1_OCTET_STRING_free(os); ERR_raise(ERR_LIB_OCSP, ERR_R_ASN1_LIB); return NULL; } static void ocsp_nonce_free(void *a) { ASN1_OCTET_STRING_free(a); } static int i2r_ocsp_nonce(const X509V3_EXT_METHOD *method, void *nonce, BIO *out, int indent) { if (BIO_printf(out, "%*s", indent, "") <= 0) return 0; if (i2a_ASN1_STRING(out, nonce, V_ASN1_OCTET_STRING) <= 0) return 0; return 1; } static int i2r_ocsp_nocheck(const X509V3_EXT_METHOD *method, void *nocheck, BIO *out, int indent) { return 1; } static void *s2i_ocsp_nocheck(const X509V3_EXT_METHOD *method, X509V3_CTX *ctx, const char *str) { return ASN1_NULL_new(); } static int i2r_ocsp_serviceloc(const X509V3_EXT_METHOD *method, void *in, BIO *bp, int ind) { int i; OCSP_SERVICELOC *a = in; ACCESS_DESCRIPTION *ad; if (BIO_printf(bp, "%*sIssuer: ", ind, "") <= 0) goto err; if (X509_NAME_print_ex(bp, a->issuer, 0, XN_FLAG_ONELINE) <= 0) goto err; for (i = 0; i < sk_ACCESS_DESCRIPTION_num(a->locator); i++) { ad = sk_ACCESS_DESCRIPTION_value(a->locator, i); if (BIO_printf(bp, "\n%*s", (2 * ind), "") <= 0) goto err; if (i2a_ASN1_OBJECT(bp, ad->method) <= 0) goto err; if (BIO_puts(bp, " - ") <= 0) goto err; if (GENERAL_NAME_print(bp, ad->location) <= 0) goto err; } return 1; err: return 0; }
ocsp
openssl/crypto/ocsp/v3_ocsp.c
openssl
#include <stdio.h> #include "internal/cryptlib.h" #include <openssl/objects.h> #include <openssl/x509.h> #include <openssl/pem.h> #include <openssl/x509v3.h> #include <openssl/ocsp.h> #include "ocsp_local.h" int OCSP_request_onereq_count(OCSP_REQUEST *req) { return sk_OCSP_ONEREQ_num(req->tbsRequest.requestList); } OCSP_ONEREQ *OCSP_request_onereq_get0(OCSP_REQUEST *req, int i) { return sk_OCSP_ONEREQ_value(req->tbsRequest.requestList, i); } OCSP_CERTID *OCSP_onereq_get0_id(OCSP_ONEREQ *one) { return one->reqCert; } int OCSP_id_get0_info(ASN1_OCTET_STRING **piNameHash, ASN1_OBJECT **pmd, ASN1_OCTET_STRING **pikeyHash, ASN1_INTEGER **pserial, OCSP_CERTID *cid) { if (!cid) return 0; if (pmd) *pmd = cid->hashAlgorithm.algorithm; if (piNameHash) *piNameHash = &cid->issuerNameHash; if (pikeyHash) *pikeyHash = &cid->issuerKeyHash; if (pserial) *pserial = &cid->serialNumber; return 1; } int OCSP_request_is_signed(OCSP_REQUEST *req) { if (req->optionalSignature) return 1; return 0; } OCSP_RESPONSE *OCSP_response_create(int status, OCSP_BASICRESP *bs) { OCSP_RESPONSE *rsp = NULL; if ((rsp = OCSP_RESPONSE_new()) == NULL) goto err; if (!(ASN1_ENUMERATED_set(rsp->responseStatus, status))) goto err; if (!bs) return rsp; if ((rsp->responseBytes = OCSP_RESPBYTES_new()) == NULL) goto err; rsp->responseBytes->responseType = OBJ_nid2obj(NID_id_pkix_OCSP_basic); if (!ASN1_item_pack (bs, ASN1_ITEM_rptr(OCSP_BASICRESP), &rsp->responseBytes->response)) goto err; return rsp; err: OCSP_RESPONSE_free(rsp); return NULL; } OCSP_SINGLERESP *OCSP_basic_add1_status(OCSP_BASICRESP *rsp, OCSP_CERTID *cid, int status, int reason, ASN1_TIME *revtime, ASN1_TIME *thisupd, ASN1_TIME *nextupd) { OCSP_SINGLERESP *single = NULL; OCSP_CERTSTATUS *cs; OCSP_REVOKEDINFO *ri; if (rsp->tbsResponseData.responses == NULL && (rsp->tbsResponseData.responses = sk_OCSP_SINGLERESP_new_null()) == NULL) goto err; if ((single = OCSP_SINGLERESP_new()) == NULL) goto err; if (!ASN1_TIME_to_generalizedtime(thisupd, &single->thisUpdate)) goto err; if (nextupd && !ASN1_TIME_to_generalizedtime(nextupd, &single->nextUpdate)) goto err; OCSP_CERTID_free(single->certId); if ((single->certId = OCSP_CERTID_dup(cid)) == NULL) goto err; cs = single->certStatus; switch (cs->type = status) { case V_OCSP_CERTSTATUS_REVOKED: if (!revtime) { ERR_raise(ERR_LIB_OCSP, OCSP_R_NO_REVOKED_TIME); goto err; } if ((cs->value.revoked = ri = OCSP_REVOKEDINFO_new()) == NULL) goto err; if (!ASN1_TIME_to_generalizedtime(revtime, &ri->revocationTime)) goto err; if (reason != OCSP_REVOKED_STATUS_NOSTATUS) { if ((ri->revocationReason = ASN1_ENUMERATED_new()) == NULL) goto err; if (!(ASN1_ENUMERATED_set(ri->revocationReason, reason))) goto err; } break; case V_OCSP_CERTSTATUS_GOOD: if ((cs->value.good = ASN1_NULL_new()) == NULL) goto err; break; case V_OCSP_CERTSTATUS_UNKNOWN: if ((cs->value.unknown = ASN1_NULL_new()) == NULL) goto err; break; default: goto err; } if (!(sk_OCSP_SINGLERESP_push(rsp->tbsResponseData.responses, single))) goto err; return single; err: OCSP_SINGLERESP_free(single); return NULL; } int OCSP_basic_add1_cert(OCSP_BASICRESP *resp, X509 *cert) { return ossl_x509_add_cert_new(&resp->certs, cert, X509_ADD_FLAG_UP_REF); } int OCSP_basic_sign_ctx(OCSP_BASICRESP *brsp, X509 *signer, EVP_MD_CTX *ctx, STACK_OF(X509) *certs, unsigned long flags) { OCSP_RESPID *rid; EVP_PKEY *pkey; if (ctx == NULL || EVP_MD_CTX_get_pkey_ctx(ctx) == NULL) { ERR_raise(ERR_LIB_OCSP, OCSP_R_NO_SIGNER_KEY); goto err; } pkey = EVP_PKEY_CTX_get0_pkey(EVP_MD_CTX_get_pkey_ctx(ctx)); if (pkey == NULL || !X509_check_private_key(signer, pkey)) { ERR_raise(ERR_LIB_OCSP, OCSP_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE); goto err; } if (!(flags & OCSP_NOCERTS)) { if (!OCSP_basic_add1_cert(brsp, signer) || !X509_add_certs(brsp->certs, certs, X509_ADD_FLAG_UP_REF)) goto err; } rid = &brsp->tbsResponseData.responderId; if (flags & OCSP_RESPID_KEY) { if (!OCSP_RESPID_set_by_key(rid, signer)) goto err; } else if (!OCSP_RESPID_set_by_name(rid, signer)) { goto err; } if (!(flags & OCSP_NOTIME) && !X509_gmtime_adj(brsp->tbsResponseData.producedAt, 0)) goto err; if (!OCSP_BASICRESP_sign_ctx(brsp, ctx, 0)) goto err; return 1; err: return 0; } int OCSP_basic_sign(OCSP_BASICRESP *brsp, X509 *signer, EVP_PKEY *key, const EVP_MD *dgst, STACK_OF(X509) *certs, unsigned long flags) { EVP_MD_CTX *ctx = EVP_MD_CTX_new(); EVP_PKEY_CTX *pkctx = NULL; int i; if (ctx == NULL) return 0; if (!EVP_DigestSignInit_ex(ctx, &pkctx, EVP_MD_get0_name(dgst), signer->libctx, signer->propq, key, NULL)) { EVP_MD_CTX_free(ctx); return 0; } i = OCSP_basic_sign_ctx(brsp, signer, ctx, certs, flags); EVP_MD_CTX_free(ctx); return i; } int OCSP_RESPID_set_by_name(OCSP_RESPID *respid, X509 *cert) { if (!X509_NAME_set(&respid->value.byName, X509_get_subject_name(cert))) return 0; respid->type = V_OCSP_RESPID_NAME; return 1; } int OCSP_RESPID_set_by_key_ex(OCSP_RESPID *respid, X509 *cert, OSSL_LIB_CTX *libctx, const char *propq) { ASN1_OCTET_STRING *byKey = NULL; unsigned char md[SHA_DIGEST_LENGTH]; EVP_MD *sha1 = EVP_MD_fetch(libctx, "SHA1", propq); int ret = 0; if (sha1 == NULL) return 0; if (!X509_pubkey_digest(cert, sha1, md, NULL)) goto err; byKey = ASN1_OCTET_STRING_new(); if (byKey == NULL) goto err; if (!(ASN1_OCTET_STRING_set(byKey, md, SHA_DIGEST_LENGTH))) { ASN1_OCTET_STRING_free(byKey); goto err; } respid->type = V_OCSP_RESPID_KEY; respid->value.byKey = byKey; ret = 1; err: EVP_MD_free(sha1); return ret; } int OCSP_RESPID_set_by_key(OCSP_RESPID *respid, X509 *cert) { if (cert == NULL) return 0; return OCSP_RESPID_set_by_key_ex(respid, cert, cert->libctx, cert->propq); } int OCSP_RESPID_match_ex(OCSP_RESPID *respid, X509 *cert, OSSL_LIB_CTX *libctx, const char *propq) { EVP_MD *sha1 = NULL; int ret = 0; if (respid->type == V_OCSP_RESPID_KEY) { unsigned char md[SHA_DIGEST_LENGTH]; sha1 = EVP_MD_fetch(libctx, "SHA1", propq); if (sha1 == NULL) goto err; if (respid->value.byKey == NULL) goto err; if (!X509_pubkey_digest(cert, sha1, md, NULL)) goto err; ret = (ASN1_STRING_length(respid->value.byKey) == SHA_DIGEST_LENGTH) && (memcmp(ASN1_STRING_get0_data(respid->value.byKey), md, SHA_DIGEST_LENGTH) == 0); } else if (respid->type == V_OCSP_RESPID_NAME) { if (respid->value.byName == NULL) return 0; return X509_NAME_cmp(respid->value.byName, X509_get_subject_name(cert)) == 0; } err: EVP_MD_free(sha1); return ret; } int OCSP_RESPID_match(OCSP_RESPID *respid, X509 *cert) { if (cert == NULL) return 0; return OCSP_RESPID_match_ex(respid, cert, cert->libctx, cert->propq); }
ocsp
openssl/crypto/ocsp/ocsp_srv.c
openssl
#include <string.h> #include <openssl/ocsp.h> #include <openssl/err.h> #include "internal/sizes.h" #include "ocsp_local.h" static int ocsp_find_signer(X509 **psigner, OCSP_BASICRESP *bs, STACK_OF(X509) *certs, unsigned long flags); static X509 *ocsp_find_signer_sk(STACK_OF(X509) *certs, OCSP_RESPID *id); static int ocsp_check_issuer(OCSP_BASICRESP *bs, STACK_OF(X509) *chain); static int ocsp_check_ids(STACK_OF(OCSP_SINGLERESP) *sresp, OCSP_CERTID **ret); static int ocsp_match_issuerid(X509 *cert, OCSP_CERTID *cid, STACK_OF(OCSP_SINGLERESP) *sresp); static int ocsp_check_delegated(X509 *x); static int ocsp_req_find_signer(X509 **psigner, OCSP_REQUEST *req, const X509_NAME *nm, STACK_OF(X509) *certs, unsigned long flags); static int ocsp_verify_signer(X509 *signer, int response, X509_STORE *st, unsigned long flags, STACK_OF(X509) *untrusted, STACK_OF(X509) **chain) { X509_STORE_CTX *ctx = X509_STORE_CTX_new(); X509_VERIFY_PARAM *vp; int ret = -1; if (ctx == NULL) { ERR_raise(ERR_LIB_OCSP, ERR_R_X509_LIB); goto end; } if (!X509_STORE_CTX_init(ctx, st, signer, untrusted)) { ERR_raise(ERR_LIB_OCSP, ERR_R_X509_LIB); goto end; } if ((vp = X509_STORE_CTX_get0_param(ctx)) == NULL) goto end; if ((flags & OCSP_PARTIAL_CHAIN) != 0) X509_VERIFY_PARAM_set_flags(vp, X509_V_FLAG_PARTIAL_CHAIN); if (response && X509_get_ext_by_NID(signer, NID_id_pkix_OCSP_noCheck, -1) >= 0) X509_VERIFY_PARAM_clear_flags(vp, X509_V_FLAG_CRL_CHECK); X509_STORE_CTX_set_purpose(ctx, X509_PURPOSE_OCSP_HELPER); X509_STORE_CTX_set_trust(ctx, X509_TRUST_OCSP_REQUEST); ret = X509_verify_cert(ctx); if (ret <= 0) { int err = X509_STORE_CTX_get_error(ctx); ERR_raise_data(ERR_LIB_OCSP, OCSP_R_CERTIFICATE_VERIFY_ERROR, "Verify error: %s", X509_verify_cert_error_string(err)); goto end; } if (chain != NULL) *chain = X509_STORE_CTX_get1_chain(ctx); end: X509_STORE_CTX_free(ctx); return ret; } static int ocsp_verify(OCSP_REQUEST *req, OCSP_BASICRESP *bs, X509 *signer, unsigned long flags) { EVP_PKEY *skey; int ret = 1; if ((flags & OCSP_NOSIGS) == 0) { if ((skey = X509_get0_pubkey(signer)) == NULL) { ERR_raise(ERR_LIB_OCSP, OCSP_R_NO_SIGNER_KEY); return -1; } if (req != NULL) ret = OCSP_REQUEST_verify(req, skey, signer->libctx, signer->propq); else ret = OCSP_BASICRESP_verify(bs, skey, signer->libctx, signer->propq); if (ret <= 0) ERR_raise(ERR_LIB_OCSP, OCSP_R_SIGNATURE_FAILURE); } return ret; } int OCSP_basic_verify(OCSP_BASICRESP *bs, STACK_OF(X509) *certs, X509_STORE *st, unsigned long flags) { X509 *signer, *x; STACK_OF(X509) *chain = NULL; STACK_OF(X509) *untrusted = NULL; int ret = ocsp_find_signer(&signer, bs, certs, flags); if (ret == 0) { ERR_raise(ERR_LIB_OCSP, OCSP_R_SIGNER_CERTIFICATE_NOT_FOUND); goto end; } if ((ret == 2) && (flags & OCSP_TRUSTOTHER) != 0) flags |= OCSP_NOVERIFY; if ((ret = ocsp_verify(NULL, bs, signer, flags)) <= 0) goto end; if ((flags & OCSP_NOVERIFY) == 0) { ret = -1; if ((flags & OCSP_NOCHAIN) == 0) { if ((untrusted = sk_X509_dup(bs->certs)) == NULL) goto end; if (!X509_add_certs(untrusted, certs, X509_ADD_FLAG_DEFAULT)) goto end; } ret = ocsp_verify_signer(signer, 1, st, flags, untrusted, &chain); if (ret <= 0) goto end; if ((flags & OCSP_NOCHECKS) != 0) { ret = 1; goto end; } ret = ocsp_check_issuer(bs, chain); if (ret != 0) goto end; if ((flags & OCSP_NOEXPLICIT) != 0) goto end; x = sk_X509_value(chain, sk_X509_num(chain) - 1); if (X509_check_trust(x, NID_OCSP_sign, 0) != X509_TRUST_TRUSTED) { ERR_raise(ERR_LIB_OCSP, OCSP_R_ROOT_CA_NOT_TRUSTED); ret = 0; goto end; } ret = 1; } end: OSSL_STACK_OF_X509_free(chain); sk_X509_free(untrusted); return ret; } int OCSP_resp_get0_signer(OCSP_BASICRESP *bs, X509 **signer, STACK_OF(X509) *extra_certs) { return ocsp_find_signer(signer, bs, extra_certs, 0) > 0; } static int ocsp_find_signer(X509 **psigner, OCSP_BASICRESP *bs, STACK_OF(X509) *certs, unsigned long flags) { X509 *signer; OCSP_RESPID *rid = &bs->tbsResponseData.responderId; if ((signer = ocsp_find_signer_sk(certs, rid)) != NULL) { *psigner = signer; return 2; } if ((flags & OCSP_NOINTERN) == 0 && (signer = ocsp_find_signer_sk(bs->certs, rid))) { *psigner = signer; return 1; } *psigner = NULL; return 0; } static X509 *ocsp_find_signer_sk(STACK_OF(X509) *certs, OCSP_RESPID *id) { int i, r; unsigned char tmphash[SHA_DIGEST_LENGTH], *keyhash; EVP_MD *md; X509 *x; if (id->type == V_OCSP_RESPID_NAME) return X509_find_by_subject(certs, id->value.byName); if (id->value.byKey->length != SHA_DIGEST_LENGTH) return NULL; keyhash = id->value.byKey->data; for (i = 0; i < sk_X509_num(certs); i++) { if ((x = sk_X509_value(certs, i)) != NULL) { if ((md = EVP_MD_fetch(x->libctx, SN_sha1, x->propq)) == NULL) break; r = X509_pubkey_digest(x, md, tmphash, NULL); EVP_MD_free(md); if (!r) break; if (memcmp(keyhash, tmphash, SHA_DIGEST_LENGTH) == 0) return x; } } return NULL; } static int ocsp_check_issuer(OCSP_BASICRESP *bs, STACK_OF(X509) *chain) { STACK_OF(OCSP_SINGLERESP) *sresp = bs->tbsResponseData.responses; X509 *signer, *sca; OCSP_CERTID *caid = NULL; int ret; if (sk_X509_num(chain) <= 0) { ERR_raise(ERR_LIB_OCSP, OCSP_R_NO_CERTIFICATES_IN_CHAIN); return -1; } ret = ocsp_check_ids(sresp, &caid); if (ret <= 0) return ret; signer = sk_X509_value(chain, 0); if (sk_X509_num(chain) > 1) { sca = sk_X509_value(chain, 1); ret = ocsp_match_issuerid(sca, caid, sresp); if (ret < 0) return ret; if (ret != 0) { if (ocsp_check_delegated(signer)) return 1; return 0; } } return ocsp_match_issuerid(signer, caid, sresp); } static int ocsp_check_ids(STACK_OF(OCSP_SINGLERESP) *sresp, OCSP_CERTID **ret) { OCSP_CERTID *tmpid, *cid; int i, idcount; idcount = sk_OCSP_SINGLERESP_num(sresp); if (idcount <= 0) { ERR_raise(ERR_LIB_OCSP, OCSP_R_RESPONSE_CONTAINS_NO_REVOCATION_DATA); return -1; } cid = sk_OCSP_SINGLERESP_value(sresp, 0)->certId; *ret = NULL; for (i = 1; i < idcount; i++) { tmpid = sk_OCSP_SINGLERESP_value(sresp, i)->certId; if (OCSP_id_issuer_cmp(cid, tmpid)) { if (OBJ_cmp(tmpid->hashAlgorithm.algorithm, cid->hashAlgorithm.algorithm)) return 2; return 0; } } *ret = cid; return 1; } static int ocsp_match_issuerid(X509 *cert, OCSP_CERTID *cid, STACK_OF(OCSP_SINGLERESP) *sresp) { int ret = -1; EVP_MD *dgst = NULL; if (cid != NULL) { char name[OSSL_MAX_NAME_SIZE]; const X509_NAME *iname; int mdlen; unsigned char md[EVP_MAX_MD_SIZE]; OBJ_obj2txt(name, sizeof(name), cid->hashAlgorithm.algorithm, 0); (void)ERR_set_mark(); dgst = EVP_MD_fetch(NULL, name, NULL); if (dgst == NULL) dgst = (EVP_MD *)EVP_get_digestbyname(name); if (dgst == NULL) { (void)ERR_clear_last_mark(); ERR_raise(ERR_LIB_OCSP, OCSP_R_UNKNOWN_MESSAGE_DIGEST); goto end; } (void)ERR_pop_to_mark(); mdlen = EVP_MD_get_size(dgst); if (mdlen < 0) { ERR_raise(ERR_LIB_OCSP, OCSP_R_DIGEST_SIZE_ERR); goto end; } if (cid->issuerNameHash.length != mdlen || cid->issuerKeyHash.length != mdlen) { ret = 0; goto end; } iname = X509_get_subject_name(cert); if (!X509_NAME_digest(iname, dgst, md, NULL)) goto end; if (memcmp(md, cid->issuerNameHash.data, mdlen) != 0) { ret = 0; goto end; } if (!X509_pubkey_digest(cert, dgst, md, NULL)) { ERR_raise(ERR_LIB_OCSP, OCSP_R_DIGEST_ERR); goto end; } ret = memcmp(md, cid->issuerKeyHash.data, mdlen) == 0; goto end; } else { int i; OCSP_CERTID *tmpid; for (i = 0; i < sk_OCSP_SINGLERESP_num(sresp); i++) { tmpid = sk_OCSP_SINGLERESP_value(sresp, i)->certId; ret = ocsp_match_issuerid(cert, tmpid, NULL); if (ret <= 0) return ret; } } return 1; end: EVP_MD_free(dgst); return ret; } static int ocsp_check_delegated(X509 *x) { if ((X509_get_extension_flags(x) & EXFLAG_XKUSAGE) && (X509_get_extended_key_usage(x) & XKU_OCSP_SIGN)) return 1; ERR_raise(ERR_LIB_OCSP, OCSP_R_MISSING_OCSPSIGNING_USAGE); return 0; } int OCSP_request_verify(OCSP_REQUEST *req, STACK_OF(X509) *certs, X509_STORE *store, unsigned long flags) { X509 *signer; const X509_NAME *nm; GENERAL_NAME *gen; int ret; if (!req->optionalSignature) { ERR_raise(ERR_LIB_OCSP, OCSP_R_REQUEST_NOT_SIGNED); return 0; } gen = req->tbsRequest.requestorName; if (!gen || gen->type != GEN_DIRNAME) { ERR_raise(ERR_LIB_OCSP, OCSP_R_UNSUPPORTED_REQUESTORNAME_TYPE); return 0; } nm = gen->d.directoryName; ret = ocsp_req_find_signer(&signer, req, nm, certs, flags); if (ret <= 0) { ERR_raise(ERR_LIB_OCSP, OCSP_R_SIGNER_CERTIFICATE_NOT_FOUND); return 0; } if ((ret == 2) && (flags & OCSP_TRUSTOTHER) != 0) flags |= OCSP_NOVERIFY; if ((ret = ocsp_verify(req, NULL, signer, flags)) <= 0) return 0; if ((flags & OCSP_NOVERIFY) != 0) return 1; return ocsp_verify_signer(signer, 0, store, flags, (flags & OCSP_NOCHAIN) != 0 ? NULL : req->optionalSignature->certs, NULL) > 0; } static int ocsp_req_find_signer(X509 **psigner, OCSP_REQUEST *req, const X509_NAME *nm, STACK_OF(X509) *certs, unsigned long flags) { X509 *signer; if ((flags & OCSP_NOINTERN) == 0) { signer = X509_find_by_subject(req->optionalSignature->certs, nm); if (signer != NULL) { *psigner = signer; return 1; } } if ((signer = X509_find_by_subject(certs, nm)) != NULL) { *psigner = signer; return 2; } return 0; }
ocsp
openssl/crypto/ocsp/ocsp_vfy.c
openssl
#include <openssl/err.h> #include <openssl/ocsperr.h> #include "crypto/ocsperr.h" #ifndef OPENSSL_NO_OCSP # ifndef OPENSSL_NO_ERR static const ERR_STRING_DATA OCSP_str_reasons[] = { {ERR_PACK(ERR_LIB_OCSP, 0, OCSP_R_CERTIFICATE_VERIFY_ERROR), "certificate verify error"}, {ERR_PACK(ERR_LIB_OCSP, 0, OCSP_R_DIGEST_ERR), "digest err"}, {ERR_PACK(ERR_LIB_OCSP, 0, OCSP_R_DIGEST_NAME_ERR), "digest name err"}, {ERR_PACK(ERR_LIB_OCSP, 0, OCSP_R_DIGEST_SIZE_ERR), "digest size err"}, {ERR_PACK(ERR_LIB_OCSP, 0, OCSP_R_ERROR_IN_NEXTUPDATE_FIELD), "error in nextupdate field"}, {ERR_PACK(ERR_LIB_OCSP, 0, OCSP_R_ERROR_IN_THISUPDATE_FIELD), "error in thisupdate field"}, {ERR_PACK(ERR_LIB_OCSP, 0, OCSP_R_MISSING_OCSPSIGNING_USAGE), "missing ocspsigning usage"}, {ERR_PACK(ERR_LIB_OCSP, 0, OCSP_R_NEXTUPDATE_BEFORE_THISUPDATE), "nextupdate before thisupdate"}, {ERR_PACK(ERR_LIB_OCSP, 0, OCSP_R_NOT_BASIC_RESPONSE), "not basic response"}, {ERR_PACK(ERR_LIB_OCSP, 0, OCSP_R_NO_CERTIFICATES_IN_CHAIN), "no certificates in chain"}, {ERR_PACK(ERR_LIB_OCSP, 0, OCSP_R_NO_RESPONSE_DATA), "no response data"}, {ERR_PACK(ERR_LIB_OCSP, 0, OCSP_R_NO_REVOKED_TIME), "no revoked time"}, {ERR_PACK(ERR_LIB_OCSP, 0, OCSP_R_NO_SIGNER_KEY), "no signer key"}, {ERR_PACK(ERR_LIB_OCSP, 0, OCSP_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE), "private key does not match certificate"}, {ERR_PACK(ERR_LIB_OCSP, 0, OCSP_R_REQUEST_NOT_SIGNED), "request not signed"}, {ERR_PACK(ERR_LIB_OCSP, 0, OCSP_R_RESPONSE_CONTAINS_NO_REVOCATION_DATA), "response contains no revocation data"}, {ERR_PACK(ERR_LIB_OCSP, 0, OCSP_R_ROOT_CA_NOT_TRUSTED), "root ca not trusted"}, {ERR_PACK(ERR_LIB_OCSP, 0, OCSP_R_SIGNATURE_FAILURE), "signature failure"}, {ERR_PACK(ERR_LIB_OCSP, 0, OCSP_R_SIGNER_CERTIFICATE_NOT_FOUND), "signer certificate not found"}, {ERR_PACK(ERR_LIB_OCSP, 0, OCSP_R_STATUS_EXPIRED), "status expired"}, {ERR_PACK(ERR_LIB_OCSP, 0, OCSP_R_STATUS_NOT_YET_VALID), "status not yet valid"}, {ERR_PACK(ERR_LIB_OCSP, 0, OCSP_R_STATUS_TOO_OLD), "status too old"}, {ERR_PACK(ERR_LIB_OCSP, 0, OCSP_R_UNKNOWN_MESSAGE_DIGEST), "unknown message digest"}, {ERR_PACK(ERR_LIB_OCSP, 0, OCSP_R_UNKNOWN_NID), "unknown nid"}, {ERR_PACK(ERR_LIB_OCSP, 0, OCSP_R_UNSUPPORTED_REQUESTORNAME_TYPE), "unsupported requestorname type"}, {0, NULL} }; # endif int ossl_err_load_OCSP_strings(void) { # ifndef OPENSSL_NO_ERR if (ERR_reason_error_string(OCSP_str_reasons[0].error) == NULL) ERR_load_strings_const(OCSP_str_reasons); # endif return 1; } #else NON_EMPTY_TRANSLATION_UNIT #endif
ocsp
openssl/crypto/ocsp/ocsp_err.c
openssl
#include <stdio.h> #include "internal/cryptlib.h" #include <openssl/core.h> #include <openssl/core_names.h> #include <openssl/pkcs12.h> #include "p12_local.h" #include "crypto/pkcs7/pk7_local.h" PKCS12_SAFEBAG *PKCS12_item_pack_safebag(void *obj, const ASN1_ITEM *it, int nid1, int nid2) { PKCS12_BAGS *bag; PKCS12_SAFEBAG *safebag; if ((bag = PKCS12_BAGS_new()) == NULL) { ERR_raise(ERR_LIB_PKCS12, ERR_R_ASN1_LIB); return NULL; } bag->type = OBJ_nid2obj(nid1); if (!ASN1_item_pack(obj, it, &bag->value.octet)) { ERR_raise(ERR_LIB_PKCS12, ERR_R_ASN1_LIB); goto err; } if ((safebag = PKCS12_SAFEBAG_new()) == NULL) { ERR_raise(ERR_LIB_PKCS12, ERR_R_ASN1_LIB); goto err; } safebag->value.bag = bag; safebag->type = OBJ_nid2obj(nid2); return safebag; err: PKCS12_BAGS_free(bag); return NULL; } PKCS7 *PKCS12_pack_p7data(STACK_OF(PKCS12_SAFEBAG) *sk) { PKCS7 *p7; if ((p7 = PKCS7_new()) == NULL) { ERR_raise(ERR_LIB_PKCS12, ERR_R_ASN1_LIB); return NULL; } p7->type = OBJ_nid2obj(NID_pkcs7_data); if ((p7->d.data = ASN1_OCTET_STRING_new()) == NULL) { ERR_raise(ERR_LIB_PKCS12, ERR_R_ASN1_LIB); goto err; } if (!ASN1_item_pack(sk, ASN1_ITEM_rptr(PKCS12_SAFEBAGS), &p7->d.data)) { ERR_raise(ERR_LIB_PKCS12, PKCS12_R_CANT_PACK_STRUCTURE); goto err; } return p7; err: PKCS7_free(p7); return NULL; } STACK_OF(PKCS12_SAFEBAG) *PKCS12_unpack_p7data(PKCS7 *p7) { if (!PKCS7_type_is_data(p7)) { ERR_raise(ERR_LIB_PKCS12, PKCS12_R_CONTENT_TYPE_NOT_DATA); return NULL; } if (p7->d.data == NULL) { ERR_raise(ERR_LIB_PKCS12, PKCS12_R_DECODE_ERROR); return NULL; } return ASN1_item_unpack_ex(p7->d.data, ASN1_ITEM_rptr(PKCS12_SAFEBAGS), ossl_pkcs7_ctx_get0_libctx(&p7->ctx), ossl_pkcs7_ctx_get0_propq(&p7->ctx)); } PKCS7 *PKCS12_pack_p7encdata_ex(int pbe_nid, const char *pass, int passlen, unsigned char *salt, int saltlen, int iter, STACK_OF(PKCS12_SAFEBAG) *bags, OSSL_LIB_CTX *ctx, const char *propq) { PKCS7 *p7; X509_ALGOR *pbe; const EVP_CIPHER *pbe_ciph = NULL; EVP_CIPHER *pbe_ciph_fetch = NULL; if ((p7 = PKCS7_new_ex(ctx, propq)) == NULL) { ERR_raise(ERR_LIB_PKCS12, ERR_R_ASN1_LIB); return NULL; } if (!PKCS7_set_type(p7, NID_pkcs7_encrypted)) { ERR_raise(ERR_LIB_PKCS12, PKCS12_R_ERROR_SETTING_ENCRYPTED_DATA_TYPE); goto err; } ERR_set_mark(); pbe_ciph = pbe_ciph_fetch = EVP_CIPHER_fetch(ctx, OBJ_nid2sn(pbe_nid), propq); if (pbe_ciph == NULL) pbe_ciph = EVP_get_cipherbynid(pbe_nid); ERR_pop_to_mark(); if (pbe_ciph != NULL) { pbe = PKCS5_pbe2_set_iv_ex(pbe_ciph, iter, salt, saltlen, NULL, -1, ctx); } else { pbe = PKCS5_pbe_set_ex(pbe_nid, iter, salt, saltlen, ctx); } if (pbe == NULL) { ERR_raise(ERR_LIB_PKCS12, ERR_R_ASN1_LIB); goto err; } X509_ALGOR_free(p7->d.encrypted->enc_data->algorithm); p7->d.encrypted->enc_data->algorithm = pbe; ASN1_OCTET_STRING_free(p7->d.encrypted->enc_data->enc_data); if (!(p7->d.encrypted->enc_data->enc_data = PKCS12_item_i2d_encrypt_ex(pbe, ASN1_ITEM_rptr(PKCS12_SAFEBAGS), pass, passlen, bags, 1, ctx, propq))) { ERR_raise(ERR_LIB_PKCS12, PKCS12_R_ENCRYPT_ERROR); goto err; } EVP_CIPHER_free(pbe_ciph_fetch); return p7; err: PKCS7_free(p7); EVP_CIPHER_free(pbe_ciph_fetch); return NULL; } PKCS7 *PKCS12_pack_p7encdata(int pbe_nid, const char *pass, int passlen, unsigned char *salt, int saltlen, int iter, STACK_OF(PKCS12_SAFEBAG) *bags) { return PKCS12_pack_p7encdata_ex(pbe_nid, pass, passlen, salt, saltlen, iter, bags, NULL, NULL); } STACK_OF(PKCS12_SAFEBAG) *PKCS12_unpack_p7encdata(PKCS7 *p7, const char *pass, int passlen) { if (!PKCS7_type_is_encrypted(p7)) return NULL; if (p7->d.encrypted == NULL) { ERR_raise(ERR_LIB_PKCS12, PKCS12_R_DECODE_ERROR); return NULL; } return PKCS12_item_decrypt_d2i_ex(p7->d.encrypted->enc_data->algorithm, ASN1_ITEM_rptr(PKCS12_SAFEBAGS), pass, passlen, p7->d.encrypted->enc_data->enc_data, 1, p7->ctx.libctx, p7->ctx.propq); } PKCS8_PRIV_KEY_INFO *PKCS12_decrypt_skey_ex(const PKCS12_SAFEBAG *bag, const char *pass, int passlen, OSSL_LIB_CTX *ctx, const char *propq) { return PKCS8_decrypt_ex(bag->value.shkeybag, pass, passlen, ctx, propq); } PKCS8_PRIV_KEY_INFO *PKCS12_decrypt_skey(const PKCS12_SAFEBAG *bag, const char *pass, int passlen) { return PKCS12_decrypt_skey_ex(bag, pass, passlen, NULL, NULL); } int PKCS12_pack_authsafes(PKCS12 *p12, STACK_OF(PKCS7) *safes) { if (ASN1_item_pack(safes, ASN1_ITEM_rptr(PKCS12_AUTHSAFES), &p12->authsafes->d.data)) return 1; return 0; } STACK_OF(PKCS7) *PKCS12_unpack_authsafes(const PKCS12 *p12) { STACK_OF(PKCS7) *p7s; PKCS7_CTX *p7ctx; PKCS7 *p7; int i; if (!PKCS7_type_is_data(p12->authsafes)) { ERR_raise(ERR_LIB_PKCS12, PKCS12_R_CONTENT_TYPE_NOT_DATA); return NULL; } if (p12->authsafes->d.data == NULL) { ERR_raise(ERR_LIB_PKCS12, PKCS12_R_DECODE_ERROR); return NULL; } p7ctx = &p12->authsafes->ctx; p7s = ASN1_item_unpack_ex(p12->authsafes->d.data, ASN1_ITEM_rptr(PKCS12_AUTHSAFES), ossl_pkcs7_ctx_get0_libctx(p7ctx), ossl_pkcs7_ctx_get0_propq(p7ctx)); if (p7s != NULL) { for (i = 0; i < sk_PKCS7_num(p7s); i++) { p7 = sk_PKCS7_value(p7s, i); if (!ossl_pkcs7_ctx_propagate(p12->authsafes, p7)) goto err; } } return p7s; err: sk_PKCS7_free(p7s); return NULL; }
pkcs12
openssl/crypto/pkcs12/p12_add.c
openssl
#include <stdio.h> #include "internal/cryptlib.h" #include <openssl/asn1t.h> #include <openssl/pkcs12.h> #include "p12_local.h" #include "crypto/pkcs7.h" ASN1_SEQUENCE(PKCS12) = { ASN1_SIMPLE(PKCS12, version, ASN1_INTEGER), ASN1_SIMPLE(PKCS12, authsafes, PKCS7), ASN1_OPT(PKCS12, mac, PKCS12_MAC_DATA) } ASN1_SEQUENCE_END(PKCS12) IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(PKCS12, PKCS12, PKCS12) PKCS12 *PKCS12_new(void) { return (PKCS12 *)ASN1_item_new(ASN1_ITEM_rptr(PKCS12)); } void PKCS12_free(PKCS12 *p12) { if (p12 != NULL && p12->authsafes != NULL) { OPENSSL_free(p12->authsafes->ctx.propq); p12->authsafes->ctx.propq = NULL; } ASN1_item_free((ASN1_VALUE *)p12, ASN1_ITEM_rptr(PKCS12)); } ASN1_SEQUENCE(PKCS12_MAC_DATA) = { ASN1_SIMPLE(PKCS12_MAC_DATA, dinfo, X509_SIG), ASN1_SIMPLE(PKCS12_MAC_DATA, salt, ASN1_OCTET_STRING), ASN1_OPT(PKCS12_MAC_DATA, iter, ASN1_INTEGER) } ASN1_SEQUENCE_END(PKCS12_MAC_DATA) IMPLEMENT_ASN1_FUNCTIONS(PKCS12_MAC_DATA) ASN1_ADB_TEMPLATE(bag_default) = ASN1_EXP(PKCS12_BAGS, value.other, ASN1_ANY, 0); ASN1_ADB(PKCS12_BAGS) = { ADB_ENTRY(NID_x509Certificate, ASN1_EXP(PKCS12_BAGS, value.x509cert, ASN1_OCTET_STRING, 0)), ADB_ENTRY(NID_x509Crl, ASN1_EXP(PKCS12_BAGS, value.x509crl, ASN1_OCTET_STRING, 0)), ADB_ENTRY(NID_sdsiCertificate, ASN1_EXP(PKCS12_BAGS, value.sdsicert, ASN1_IA5STRING, 0)), } ASN1_ADB_END(PKCS12_BAGS, 0, type, 0, &bag_default_tt, NULL); ASN1_SEQUENCE(PKCS12_BAGS) = { ASN1_SIMPLE(PKCS12_BAGS, type, ASN1_OBJECT), ASN1_ADB_OBJECT(PKCS12_BAGS), } ASN1_SEQUENCE_END(PKCS12_BAGS) IMPLEMENT_ASN1_FUNCTIONS(PKCS12_BAGS) ASN1_ADB_TEMPLATE(safebag_default) = ASN1_EXP(PKCS12_SAFEBAG, value.other, ASN1_ANY, 0); ASN1_ADB(PKCS12_SAFEBAG) = { ADB_ENTRY(NID_keyBag, ASN1_EXP(PKCS12_SAFEBAG, value.keybag, PKCS8_PRIV_KEY_INFO, 0)), ADB_ENTRY(NID_pkcs8ShroudedKeyBag, ASN1_EXP(PKCS12_SAFEBAG, value.shkeybag, X509_SIG, 0)), ADB_ENTRY(NID_safeContentsBag, ASN1_EXP_SEQUENCE_OF(PKCS12_SAFEBAG, value.safes, PKCS12_SAFEBAG, 0)), ADB_ENTRY(NID_certBag, ASN1_EXP(PKCS12_SAFEBAG, value.bag, PKCS12_BAGS, 0)), ADB_ENTRY(NID_crlBag, ASN1_EXP(PKCS12_SAFEBAG, value.bag, PKCS12_BAGS, 0)), ADB_ENTRY(NID_secretBag, ASN1_EXP(PKCS12_SAFEBAG, value.bag, PKCS12_BAGS, 0)) } ASN1_ADB_END(PKCS12_SAFEBAG, 0, type, 0, &safebag_default_tt, NULL); ASN1_SEQUENCE(PKCS12_SAFEBAG) = { ASN1_SIMPLE(PKCS12_SAFEBAG, type, ASN1_OBJECT), ASN1_ADB_OBJECT(PKCS12_SAFEBAG), ASN1_SET_OF_OPT(PKCS12_SAFEBAG, attrib, X509_ATTRIBUTE) } ASN1_SEQUENCE_END(PKCS12_SAFEBAG) IMPLEMENT_ASN1_FUNCTIONS(PKCS12_SAFEBAG) ASN1_ITEM_TEMPLATE(PKCS12_SAFEBAGS) = ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, PKCS12_SAFEBAGS, PKCS12_SAFEBAG) ASN1_ITEM_TEMPLATE_END(PKCS12_SAFEBAGS) ASN1_ITEM_TEMPLATE(PKCS12_AUTHSAFES) = ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, PKCS12_AUTHSAFES, PKCS7) ASN1_ITEM_TEMPLATE_END(PKCS12_AUTHSAFES)
pkcs12
openssl/crypto/pkcs12/p12_asn.c
openssl
#include <stdio.h> #include "internal/cryptlib.h" #include <openssl/pkcs12.h> #include <openssl/trace.h> unsigned char *PKCS12_pbe_crypt_ex(const X509_ALGOR *algor, const char *pass, int passlen, const unsigned char *in, int inlen, unsigned char **data, int *datalen, int en_de, OSSL_LIB_CTX *libctx, const char *propq) { unsigned char *out = NULL; int outlen, i; EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new(); int max_out_len, mac_len = 0; int block_size; if (ctx == NULL) { ERR_raise(ERR_LIB_PKCS12, ERR_R_EVP_LIB); goto err; } if (!EVP_PBE_CipherInit_ex(algor->algorithm, pass, passlen, algor->parameter, ctx, en_de, libctx, propq)) goto err; block_size = EVP_CIPHER_CTX_get_block_size(ctx); if (block_size == 0) { ERR_raise(ERR_LIB_PKCS12, ERR_R_PASSED_NULL_PARAMETER); goto err; } max_out_len = inlen + block_size; if ((EVP_CIPHER_get_flags(EVP_CIPHER_CTX_get0_cipher(ctx)) & EVP_CIPH_FLAG_CIPHER_WITH_MAC) != 0) { if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_TLS1_AAD, 0, &mac_len) < 0) { ERR_raise(ERR_LIB_PKCS12, ERR_R_INTERNAL_ERROR); goto err; } if (EVP_CIPHER_CTX_is_encrypting(ctx)) { max_out_len += mac_len; } else { if (inlen < mac_len) { ERR_raise(ERR_LIB_PKCS12, PKCS12_R_UNSUPPORTED_PKCS12_MODE); goto err; } inlen -= mac_len; if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, (int)mac_len, (unsigned char *)in+inlen) < 0) { ERR_raise(ERR_LIB_PKCS12, ERR_R_INTERNAL_ERROR); goto err; } } } if ((out = OPENSSL_malloc(max_out_len)) == NULL) goto err; if (!EVP_CipherUpdate(ctx, out, &i, in, inlen)) { OPENSSL_free(out); out = NULL; ERR_raise(ERR_LIB_PKCS12, ERR_R_EVP_LIB); goto err; } outlen = i; if (!EVP_CipherFinal_ex(ctx, out + i, &i)) { OPENSSL_free(out); out = NULL; ERR_raise_data(ERR_LIB_PKCS12, PKCS12_R_PKCS12_CIPHERFINAL_ERROR, passlen == 0 ? "empty password" : "maybe wrong password"); goto err; } outlen += i; if ((EVP_CIPHER_get_flags(EVP_CIPHER_CTX_get0_cipher(ctx)) & EVP_CIPH_FLAG_CIPHER_WITH_MAC) != 0) { if (EVP_CIPHER_CTX_is_encrypting(ctx)) { if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, (int)mac_len, out+outlen) < 0) { OPENSSL_free(out); out = NULL; ERR_raise(ERR_LIB_PKCS12, ERR_R_INTERNAL_ERROR); goto err; } outlen += mac_len; } } if (datalen) *datalen = outlen; if (data) *data = out; err: EVP_CIPHER_CTX_free(ctx); return out; } unsigned char *PKCS12_pbe_crypt(const X509_ALGOR *algor, const char *pass, int passlen, const unsigned char *in, int inlen, unsigned char **data, int *datalen, int en_de) { return PKCS12_pbe_crypt_ex(algor, pass, passlen, in, inlen, data, datalen, en_de, NULL, NULL); } void *PKCS12_item_decrypt_d2i_ex(const X509_ALGOR *algor, const ASN1_ITEM *it, const char *pass, int passlen, const ASN1_OCTET_STRING *oct, int zbuf, OSSL_LIB_CTX *libctx, const char *propq) { unsigned char *out = NULL; const unsigned char *p; void *ret; int outlen = 0; if (!PKCS12_pbe_crypt_ex(algor, pass, passlen, oct->data, oct->length, &out, &outlen, 0, libctx, propq)) return NULL; p = out; OSSL_TRACE_BEGIN(PKCS12_DECRYPT) { BIO_printf(trc_out, "\n"); BIO_dump(trc_out, out, outlen); BIO_printf(trc_out, "\n"); } OSSL_TRACE_END(PKCS12_DECRYPT); ret = ASN1_item_d2i(NULL, &p, outlen, it); if (zbuf) OPENSSL_cleanse(out, outlen); if (!ret) ERR_raise(ERR_LIB_PKCS12, PKCS12_R_DECODE_ERROR); OPENSSL_free(out); return ret; } void *PKCS12_item_decrypt_d2i(const X509_ALGOR *algor, const ASN1_ITEM *it, const char *pass, int passlen, const ASN1_OCTET_STRING *oct, int zbuf) { return PKCS12_item_decrypt_d2i_ex(algor, it, pass, passlen, oct, zbuf, NULL, NULL); } ASN1_OCTET_STRING *PKCS12_item_i2d_encrypt_ex(X509_ALGOR *algor, const ASN1_ITEM *it, const char *pass, int passlen, void *obj, int zbuf, OSSL_LIB_CTX *ctx, const char *propq) { ASN1_OCTET_STRING *oct = NULL; unsigned char *in = NULL; int inlen; if ((oct = ASN1_OCTET_STRING_new()) == NULL) { ERR_raise(ERR_LIB_PKCS12, ERR_R_ASN1_LIB); goto err; } inlen = ASN1_item_i2d(obj, &in, it); if (!in) { ERR_raise(ERR_LIB_PKCS12, PKCS12_R_ENCODE_ERROR); goto err; } if (!PKCS12_pbe_crypt_ex(algor, pass, passlen, in, inlen, &oct->data, &oct->length, 1, ctx, propq)) { ERR_raise(ERR_LIB_PKCS12, PKCS12_R_ENCRYPT_ERROR); OPENSSL_free(in); goto err; } if (zbuf) OPENSSL_cleanse(in, inlen); OPENSSL_free(in); return oct; err: ASN1_OCTET_STRING_free(oct); return NULL; } ASN1_OCTET_STRING *PKCS12_item_i2d_encrypt(X509_ALGOR *algor, const ASN1_ITEM *it, const char *pass, int passlen, void *obj, int zbuf) { return PKCS12_item_i2d_encrypt_ex(algor, it, pass, passlen, obj, zbuf, NULL, NULL); }
pkcs12
openssl/crypto/pkcs12/p12_decr.c
openssl
#include "internal/deprecated.h" #include <stdio.h> #include "internal/cryptlib.h" #include <openssl/crypto.h> #include <openssl/hmac.h> #include <openssl/rand.h> #include <openssl/pkcs12.h> #include "p12_local.h" int PKCS12_mac_present(const PKCS12 *p12) { return p12->mac ? 1 : 0; } void PKCS12_get0_mac(const ASN1_OCTET_STRING **pmac, const X509_ALGOR **pmacalg, const ASN1_OCTET_STRING **psalt, const ASN1_INTEGER **piter, const PKCS12 *p12) { if (p12->mac) { X509_SIG_get0(p12->mac->dinfo, pmacalg, pmac); if (psalt) *psalt = p12->mac->salt; if (piter) *piter = p12->mac->iter; } else { if (pmac) *pmac = NULL; if (pmacalg) *pmacalg = NULL; if (psalt) *psalt = NULL; if (piter) *piter = NULL; } } #define TK26_MAC_KEY_LEN 32 static int pkcs12_gen_gost_mac_key(const char *pass, int passlen, const unsigned char *salt, int saltlen, int iter, int keylen, unsigned char *key, const EVP_MD *digest) { unsigned char out[96]; if (keylen != TK26_MAC_KEY_LEN) { return 0; } if (!PKCS5_PBKDF2_HMAC(pass, passlen, salt, saltlen, iter, digest, sizeof(out), out)) { return 0; } memcpy(key, out + sizeof(out) - TK26_MAC_KEY_LEN, TK26_MAC_KEY_LEN); OPENSSL_cleanse(out, sizeof(out)); return 1; } static int pkcs12_gen_mac(PKCS12 *p12, const char *pass, int passlen, unsigned char *mac, unsigned int *maclen, int (*pkcs12_key_gen)(const char *pass, int passlen, unsigned char *salt, int slen, int id, int iter, int n, unsigned char *out, const EVP_MD *md_type)) { int ret = 0; const EVP_MD *md; EVP_MD *md_fetch; HMAC_CTX *hmac = NULL; unsigned char key[EVP_MAX_MD_SIZE], *salt; int saltlen, iter; char md_name[80]; int md_size = 0; int md_nid; const X509_ALGOR *macalg; const ASN1_OBJECT *macoid; if (!PKCS7_type_is_data(p12->authsafes)) { ERR_raise(ERR_LIB_PKCS12, PKCS12_R_CONTENT_TYPE_NOT_DATA); return 0; } if (p12->authsafes->d.data == NULL) { ERR_raise(ERR_LIB_PKCS12, PKCS12_R_DECODE_ERROR); return 0; } salt = p12->mac->salt->data; saltlen = p12->mac->salt->length; if (p12->mac->iter == NULL) iter = 1; else iter = ASN1_INTEGER_get(p12->mac->iter); X509_SIG_get0(p12->mac->dinfo, &macalg, NULL); X509_ALGOR_get0(&macoid, NULL, NULL, macalg); if (OBJ_obj2txt(md_name, sizeof(md_name), macoid, 0) < 0) return 0; (void)ERR_set_mark(); md = md_fetch = EVP_MD_fetch(p12->authsafes->ctx.libctx, md_name, p12->authsafes->ctx.propq); if (md == NULL) md = EVP_get_digestbynid(OBJ_obj2nid(macoid)); if (md == NULL) { (void)ERR_clear_last_mark(); ERR_raise(ERR_LIB_PKCS12, PKCS12_R_UNKNOWN_DIGEST_ALGORITHM); return 0; } (void)ERR_pop_to_mark(); md_size = EVP_MD_get_size(md); md_nid = EVP_MD_get_type(md); if (md_size < 0) goto err; if ((md_nid == NID_id_GostR3411_94 || md_nid == NID_id_GostR3411_2012_256 || md_nid == NID_id_GostR3411_2012_512) && ossl_safe_getenv("LEGACY_GOST_PKCS12") == NULL) { md_size = TK26_MAC_KEY_LEN; if (!pkcs12_gen_gost_mac_key(pass, passlen, salt, saltlen, iter, md_size, key, md)) { ERR_raise(ERR_LIB_PKCS12, PKCS12_R_KEY_GEN_ERROR); goto err; } } else { if (pkcs12_key_gen != NULL) { if (!(*pkcs12_key_gen)(pass, passlen, salt, saltlen, PKCS12_MAC_ID, iter, md_size, key, md)) { ERR_raise(ERR_LIB_PKCS12, PKCS12_R_KEY_GEN_ERROR); goto err; } } else { if (!PKCS12_key_gen_utf8_ex(pass, passlen, salt, saltlen, PKCS12_MAC_ID, iter, md_size, key, md, p12->authsafes->ctx.libctx, p12->authsafes->ctx.propq)) { ERR_raise(ERR_LIB_PKCS12, PKCS12_R_KEY_GEN_ERROR); goto err; } } } if ((hmac = HMAC_CTX_new()) == NULL || !HMAC_Init_ex(hmac, key, md_size, md, NULL) || !HMAC_Update(hmac, p12->authsafes->d.data->data, p12->authsafes->d.data->length) || !HMAC_Final(hmac, mac, maclen)) { goto err; } ret = 1; err: OPENSSL_cleanse(key, sizeof(key)); HMAC_CTX_free(hmac); EVP_MD_free(md_fetch); return ret; } int PKCS12_gen_mac(PKCS12 *p12, const char *pass, int passlen, unsigned char *mac, unsigned int *maclen) { return pkcs12_gen_mac(p12, pass, passlen, mac, maclen, NULL); } int PKCS12_verify_mac(PKCS12 *p12, const char *pass, int passlen) { unsigned char mac[EVP_MAX_MD_SIZE]; unsigned int maclen; const ASN1_OCTET_STRING *macoct; if (p12->mac == NULL) { ERR_raise(ERR_LIB_PKCS12, PKCS12_R_MAC_ABSENT); return 0; } if (!pkcs12_gen_mac(p12, pass, passlen, mac, &maclen, NULL)) { ERR_raise(ERR_LIB_PKCS12, PKCS12_R_MAC_GENERATION_ERROR); return 0; } X509_SIG_get0(p12->mac->dinfo, NULL, &macoct); if ((maclen != (unsigned int)ASN1_STRING_length(macoct)) || CRYPTO_memcmp(mac, ASN1_STRING_get0_data(macoct), maclen) != 0) return 0; return 1; } int PKCS12_set_mac(PKCS12 *p12, const char *pass, int passlen, unsigned char *salt, int saltlen, int iter, const EVP_MD *md_type) { unsigned char mac[EVP_MAX_MD_SIZE]; unsigned int maclen; ASN1_OCTET_STRING *macoct; if (md_type == NULL) md_type = EVP_sha256(); if (!iter) iter = PKCS12_DEFAULT_ITER; if (PKCS12_setup_mac(p12, iter, salt, saltlen, md_type) == PKCS12_ERROR) { ERR_raise(ERR_LIB_PKCS12, PKCS12_R_MAC_SETUP_ERROR); return 0; } if (!pkcs12_gen_mac(p12, pass, passlen, mac, &maclen, NULL)) { ERR_raise(ERR_LIB_PKCS12, PKCS12_R_MAC_GENERATION_ERROR); return 0; } X509_SIG_getm(p12->mac->dinfo, NULL, &macoct); if (!ASN1_OCTET_STRING_set(macoct, mac, maclen)) { ERR_raise(ERR_LIB_PKCS12, PKCS12_R_MAC_STRING_SET_ERROR); return 0; } return 1; } int PKCS12_setup_mac(PKCS12 *p12, int iter, unsigned char *salt, int saltlen, const EVP_MD *md_type) { X509_ALGOR *macalg; PKCS12_MAC_DATA_free(p12->mac); p12->mac = NULL; if ((p12->mac = PKCS12_MAC_DATA_new()) == NULL) return PKCS12_ERROR; if (iter > 1) { if ((p12->mac->iter = ASN1_INTEGER_new()) == NULL) { ERR_raise(ERR_LIB_PKCS12, ERR_R_ASN1_LIB); return 0; } if (!ASN1_INTEGER_set(p12->mac->iter, iter)) { ERR_raise(ERR_LIB_PKCS12, ERR_R_ASN1_LIB); return 0; } } if (saltlen == 0) saltlen = PKCS12_SALT_LEN; else if (saltlen < 0) return 0; if ((p12->mac->salt->data = OPENSSL_malloc(saltlen)) == NULL) return 0; p12->mac->salt->length = saltlen; if (salt == NULL) { if (RAND_bytes_ex(p12->authsafes->ctx.libctx, p12->mac->salt->data, (size_t)saltlen, 0) <= 0) return 0; } else { memcpy(p12->mac->salt->data, salt, saltlen); } X509_SIG_getm(p12->mac->dinfo, &macalg, NULL); if (!X509_ALGOR_set0(macalg, OBJ_nid2obj(EVP_MD_get_type(md_type)), V_ASN1_NULL, NULL)) { ERR_raise(ERR_LIB_PKCS12, ERR_R_ASN1_LIB); return 0; } return 1; }
pkcs12
openssl/crypto/pkcs12/p12_mutl.c
openssl
#include <stdio.h> #include "internal/cryptlib.h" #include <openssl/pkcs12.h> PKCS8_PRIV_KEY_INFO *PKCS8_decrypt_ex(const X509_SIG *p8, const char *pass, int passlen, OSSL_LIB_CTX *ctx, const char *propq) { const X509_ALGOR *dalg; const ASN1_OCTET_STRING *doct; X509_SIG_get0(p8, &dalg, &doct); return PKCS12_item_decrypt_d2i_ex(dalg, ASN1_ITEM_rptr(PKCS8_PRIV_KEY_INFO), pass, passlen, doct, 1, ctx, propq); } PKCS8_PRIV_KEY_INFO *PKCS8_decrypt(const X509_SIG *p8, const char *pass, int passlen) { return PKCS8_decrypt_ex(p8, pass, passlen, NULL, NULL); }
pkcs12
openssl/crypto/pkcs12/p12_p8d.c
openssl
#include <openssl/err.h> #include <openssl/pkcs12err.h> #include "crypto/pkcs12err.h" #ifndef OPENSSL_NO_ERR static const ERR_STRING_DATA PKCS12_str_reasons[] = { {ERR_PACK(ERR_LIB_PKCS12, 0, PKCS12_R_CALLBACK_FAILED), "callback failed"}, {ERR_PACK(ERR_LIB_PKCS12, 0, PKCS12_R_CANT_PACK_STRUCTURE), "can't pack structure"}, {ERR_PACK(ERR_LIB_PKCS12, 0, PKCS12_R_CONTENT_TYPE_NOT_DATA), "content type not data"}, {ERR_PACK(ERR_LIB_PKCS12, 0, PKCS12_R_DECODE_ERROR), "decode error"}, {ERR_PACK(ERR_LIB_PKCS12, 0, PKCS12_R_ENCODE_ERROR), "encode error"}, {ERR_PACK(ERR_LIB_PKCS12, 0, PKCS12_R_ENCRYPT_ERROR), "encrypt error"}, {ERR_PACK(ERR_LIB_PKCS12, 0, PKCS12_R_ERROR_SETTING_ENCRYPTED_DATA_TYPE), "error setting encrypted data type"}, {ERR_PACK(ERR_LIB_PKCS12, 0, PKCS12_R_INVALID_NULL_ARGUMENT), "invalid null argument"}, {ERR_PACK(ERR_LIB_PKCS12, 0, PKCS12_R_INVALID_NULL_PKCS12_POINTER), "invalid null pkcs12 pointer"}, {ERR_PACK(ERR_LIB_PKCS12, 0, PKCS12_R_INVALID_TYPE), "invalid type"}, {ERR_PACK(ERR_LIB_PKCS12, 0, PKCS12_R_IV_GEN_ERROR), "iv gen error"}, {ERR_PACK(ERR_LIB_PKCS12, 0, PKCS12_R_KEY_GEN_ERROR), "key gen error"}, {ERR_PACK(ERR_LIB_PKCS12, 0, PKCS12_R_MAC_ABSENT), "mac absent"}, {ERR_PACK(ERR_LIB_PKCS12, 0, PKCS12_R_MAC_GENERATION_ERROR), "mac generation error"}, {ERR_PACK(ERR_LIB_PKCS12, 0, PKCS12_R_MAC_SETUP_ERROR), "mac setup error"}, {ERR_PACK(ERR_LIB_PKCS12, 0, PKCS12_R_MAC_STRING_SET_ERROR), "mac string set error"}, {ERR_PACK(ERR_LIB_PKCS12, 0, PKCS12_R_MAC_VERIFY_FAILURE), "mac verify failure"}, {ERR_PACK(ERR_LIB_PKCS12, 0, PKCS12_R_PARSE_ERROR), "parse error"}, {ERR_PACK(ERR_LIB_PKCS12, 0, PKCS12_R_PKCS12_CIPHERFINAL_ERROR), "pkcs12 cipherfinal error"}, {ERR_PACK(ERR_LIB_PKCS12, 0, PKCS12_R_UNKNOWN_DIGEST_ALGORITHM), "unknown digest algorithm"}, {ERR_PACK(ERR_LIB_PKCS12, 0, PKCS12_R_UNSUPPORTED_PKCS12_MODE), "unsupported pkcs12 mode"}, {0, NULL} }; #endif int ossl_err_load_PKCS12_strings(void) { #ifndef OPENSSL_NO_ERR if (ERR_reason_error_string(PKCS12_str_reasons[0].error) == NULL) ERR_load_strings_const(PKCS12_str_reasons); #endif return 1; }
pkcs12
openssl/crypto/pkcs12/pk12err.c
openssl
#include <stdio.h> #include "internal/cryptlib.h" #include <openssl/core.h> #include <openssl/core_names.h> #include "crypto/evp.h" #include <openssl/pkcs12.h> void PKCS12_PBE_add(void) { } int PKCS12_PBE_keyivgen_ex(EVP_CIPHER_CTX *ctx, const char *pass, int passlen, ASN1_TYPE *param, const EVP_CIPHER *cipher, const EVP_MD *md, int en_de, OSSL_LIB_CTX *libctx, const char *propq) { PBEPARAM *pbe; int saltlen, iter, ret; unsigned char *salt; unsigned char key[EVP_MAX_KEY_LENGTH], iv[EVP_MAX_IV_LENGTH]; unsigned char *piv = iv; if (cipher == NULL) return 0; pbe = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(PBEPARAM), param); if (pbe == NULL) { ERR_raise(ERR_LIB_PKCS12, PKCS12_R_DECODE_ERROR); return 0; } if (pbe->iter == NULL) iter = 1; else iter = ASN1_INTEGER_get(pbe->iter); salt = pbe->salt->data; saltlen = pbe->salt->length; if (!PKCS12_key_gen_utf8_ex(pass, passlen, salt, saltlen, PKCS12_KEY_ID, iter, EVP_CIPHER_get_key_length(cipher), key, md, libctx, propq)) { ERR_raise(ERR_LIB_PKCS12, PKCS12_R_KEY_GEN_ERROR); PBEPARAM_free(pbe); return 0; } if (EVP_CIPHER_get_iv_length(cipher) > 0) { if (!PKCS12_key_gen_utf8_ex(pass, passlen, salt, saltlen, PKCS12_IV_ID, iter, EVP_CIPHER_get_iv_length(cipher), iv, md, libctx, propq)) { ERR_raise(ERR_LIB_PKCS12, PKCS12_R_IV_GEN_ERROR); PBEPARAM_free(pbe); return 0; } } else { piv = NULL; } PBEPARAM_free(pbe); ret = EVP_CipherInit_ex(ctx, cipher, NULL, key, piv, en_de); OPENSSL_cleanse(key, EVP_MAX_KEY_LENGTH); OPENSSL_cleanse(iv, EVP_MAX_IV_LENGTH); return ret; } int PKCS12_PBE_keyivgen(EVP_CIPHER_CTX *ctx, const char *pass, int passlen, ASN1_TYPE *param, const EVP_CIPHER *cipher, const EVP_MD *md, int en_de) { return PKCS12_PBE_keyivgen_ex(ctx, pass, passlen, param, cipher, md, en_de, NULL, NULL); }
pkcs12
openssl/crypto/pkcs12/p12_crpt.c
openssl
#include <stdio.h> #include "internal/cryptlib.h" #include <openssl/pkcs12.h> #include "p12_local.h" static int pkcs12_add_bag(STACK_OF(PKCS12_SAFEBAG) **pbags, PKCS12_SAFEBAG *bag); static int pkcs12_remove_bag(STACK_OF(PKCS12_SAFEBAG) **pbags, PKCS12_SAFEBAG *bag); static PKCS12_SAFEBAG *pkcs12_add_cert_bag(STACK_OF(PKCS12_SAFEBAG) **pbags, X509 *cert, const char *name, int namelen, unsigned char *keyid, int keyidlen); static int copy_bag_attr(PKCS12_SAFEBAG *bag, EVP_PKEY *pkey, int nid) { int idx = EVP_PKEY_get_attr_by_NID(pkey, nid, -1); if (idx < 0) return 1; return X509at_add1_attr(&bag->attrib, EVP_PKEY_get_attr(pkey, idx)) != NULL; } PKCS12 *PKCS12_create_ex2(const char *pass, const char *name, EVP_PKEY *pkey, X509 *cert, STACK_OF(X509) *ca, int nid_key, int nid_cert, int iter, int mac_iter, int keytype, OSSL_LIB_CTX *ctx, const char *propq, PKCS12_create_cb *cb, void *cbarg) { PKCS12 *p12 = NULL; STACK_OF(PKCS7) *safes = NULL; STACK_OF(PKCS12_SAFEBAG) *bags = NULL; PKCS12_SAFEBAG *bag = NULL; int i, cbret; unsigned char keyid[EVP_MAX_MD_SIZE]; unsigned int keyidlen = 0; int namelen = -1; unsigned char *pkeyid = NULL; int pkeyidlen = -1; if (nid_cert == NID_undef) nid_cert = NID_aes_256_cbc; if (nid_key == NID_undef) nid_key = NID_aes_256_cbc; if (!iter) iter = PKCS12_DEFAULT_ITER; if (!mac_iter) mac_iter = PKCS12_DEFAULT_ITER; if (pkey == NULL && cert == NULL && ca == NULL) { ERR_raise(ERR_LIB_PKCS12, PKCS12_R_INVALID_NULL_ARGUMENT); return NULL; } if (pkey && cert) { if (!X509_check_private_key(cert, pkey)) return NULL; if (!X509_digest(cert, EVP_sha1(), keyid, &keyidlen)) return NULL; } if (cert) { if (name == NULL) name = (char *)X509_alias_get0(cert, &namelen); if (keyidlen > 0) { pkeyid = keyid; pkeyidlen = keyidlen; } else { pkeyid = X509_keyid_get0(cert, &pkeyidlen); } bag = pkcs12_add_cert_bag(&bags, cert, name, namelen, pkeyid, pkeyidlen); if (cb != NULL) { cbret = cb(bag, cbarg); if (cbret == -1) { ERR_raise(ERR_LIB_PKCS12, PKCS12_R_CALLBACK_FAILED); goto err; } else if (cbret == 0) { pkcs12_remove_bag(&bags, bag); } } } for (i = 0; i < sk_X509_num(ca); i++) { if ((bag = PKCS12_add_cert(&bags, sk_X509_value(ca, i))) == NULL) goto err; if (cb != NULL) { cbret = cb(bag, cbarg); if (cbret == -1) { ERR_raise(ERR_LIB_PKCS12, PKCS12_R_CALLBACK_FAILED); goto err; } else if (cbret == 0) { pkcs12_remove_bag(&bags, bag); } } } if (bags && !PKCS12_add_safe_ex(&safes, bags, nid_cert, iter, pass, ctx, propq)) goto err; sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free); bags = NULL; if (pkey) { bag = PKCS12_add_key_ex(&bags, pkey, keytype, iter, nid_key, pass, ctx, propq); if (!bag) goto err; if (!copy_bag_attr(bag, pkey, NID_ms_csp_name)) goto err; if (!copy_bag_attr(bag, pkey, NID_LocalKeySet)) goto err; if (name && !PKCS12_add_friendlyname(bag, name, -1)) goto err; if (keyidlen && !PKCS12_add_localkeyid(bag, keyid, keyidlen)) goto err; if (cb != NULL) { cbret = cb(bag, cbarg); if (cbret == -1) { ERR_raise(ERR_LIB_PKCS12, PKCS12_R_CALLBACK_FAILED); goto err; } else if (cbret == 0) { pkcs12_remove_bag(&bags, bag); } } } if (bags && !PKCS12_add_safe(&safes, bags, -1, 0, NULL)) goto err; sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free); bags = NULL; p12 = PKCS12_add_safes_ex(safes, 0, ctx, propq); if (p12 == NULL) goto err; sk_PKCS7_pop_free(safes, PKCS7_free); safes = NULL; if ((mac_iter != -1) && !PKCS12_set_mac(p12, pass, -1, NULL, 0, mac_iter, NULL)) goto err; return p12; err: PKCS12_free(p12); sk_PKCS7_pop_free(safes, PKCS7_free); sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free); return NULL; } PKCS12 *PKCS12_create_ex(const char *pass, const char *name, EVP_PKEY *pkey, X509 *cert, STACK_OF(X509) *ca, int nid_key, int nid_cert, int iter, int mac_iter, int keytype, OSSL_LIB_CTX *ctx, const char *propq) { return PKCS12_create_ex2(pass, name, pkey, cert, ca, nid_key, nid_cert, iter, mac_iter, keytype, ctx, propq, NULL, NULL); } PKCS12 *PKCS12_create(const char *pass, const char *name, EVP_PKEY *pkey, X509 *cert, STACK_OF(X509) *ca, int nid_key, int nid_cert, int iter, int mac_iter, int keytype) { return PKCS12_create_ex(pass, name, pkey, cert, ca, nid_key, nid_cert, iter, mac_iter, keytype, NULL, NULL); } static PKCS12_SAFEBAG *pkcs12_add_cert_bag(STACK_OF(PKCS12_SAFEBAG) **pbags, X509 *cert, const char *name, int namelen, unsigned char *keyid, int keyidlen) { PKCS12_SAFEBAG *bag = NULL; if ((bag = PKCS12_SAFEBAG_create_cert(cert)) == NULL) goto err; if (name != NULL && !PKCS12_add_friendlyname(bag, name, namelen)) goto err; if (keyid != NULL && !PKCS12_add_localkeyid(bag, keyid, keyidlen)) goto err; if (!pkcs12_add_bag(pbags, bag)) goto err; return bag; err: PKCS12_SAFEBAG_free(bag); return NULL; } PKCS12_SAFEBAG *PKCS12_add_cert(STACK_OF(PKCS12_SAFEBAG) **pbags, X509 *cert) { char *name = NULL; int namelen = -1; unsigned char *keyid = NULL; int keyidlen = -1; name = (char *)X509_alias_get0(cert, &namelen); keyid = X509_keyid_get0(cert, &keyidlen); return pkcs12_add_cert_bag(pbags, cert, name, namelen, keyid, keyidlen); } PKCS12_SAFEBAG *PKCS12_add_key_ex(STACK_OF(PKCS12_SAFEBAG) **pbags, EVP_PKEY *key, int key_usage, int iter, int nid_key, const char *pass, OSSL_LIB_CTX *ctx, const char *propq) { PKCS12_SAFEBAG *bag = NULL; PKCS8_PRIV_KEY_INFO *p8 = NULL; if ((p8 = EVP_PKEY2PKCS8(key)) == NULL) goto err; if (key_usage && !PKCS8_add_keyusage(p8, key_usage)) goto err; if (nid_key != -1) { bag = PKCS12_SAFEBAG_create_pkcs8_encrypt_ex(nid_key, pass, -1, NULL, 0, iter, p8, ctx, propq); PKCS8_PRIV_KEY_INFO_free(p8); } else bag = PKCS12_SAFEBAG_create0_p8inf(p8); if (!bag) goto err; if (!pkcs12_add_bag(pbags, bag)) goto err; return bag; err: PKCS12_SAFEBAG_free(bag); return NULL; } PKCS12_SAFEBAG *PKCS12_add_key(STACK_OF(PKCS12_SAFEBAG) **pbags, EVP_PKEY *key, int key_usage, int iter, int nid_key, const char *pass) { return PKCS12_add_key_ex(pbags, key, key_usage, iter, nid_key, pass, NULL, NULL); } PKCS12_SAFEBAG *PKCS12_add_secret(STACK_OF(PKCS12_SAFEBAG) **pbags, int nid_type, const unsigned char *value, int len) { PKCS12_SAFEBAG *bag = NULL; if ((bag = PKCS12_SAFEBAG_create_secret(nid_type, V_ASN1_OCTET_STRING, value, len)) == NULL) goto err; if (!pkcs12_add_bag(pbags, bag)) goto err; return bag; err: PKCS12_SAFEBAG_free(bag); return NULL; } int PKCS12_add_safe_ex(STACK_OF(PKCS7) **psafes, STACK_OF(PKCS12_SAFEBAG) *bags, int nid_safe, int iter, const char *pass, OSSL_LIB_CTX *ctx, const char *propq) { PKCS7 *p7 = NULL; int free_safes = 0; if (*psafes == NULL) { *psafes = sk_PKCS7_new_null(); if (*psafes == NULL) return 0; free_safes = 1; } if (nid_safe == 0) #ifdef OPENSSL_NO_RC2 nid_safe = NID_pbe_WithSHA1And3_Key_TripleDES_CBC; #else nid_safe = NID_pbe_WithSHA1And40BitRC2_CBC; #endif if (nid_safe == -1) p7 = PKCS12_pack_p7data(bags); else p7 = PKCS12_pack_p7encdata_ex(nid_safe, pass, -1, NULL, 0, iter, bags, ctx, propq); if (p7 == NULL) goto err; if (!sk_PKCS7_push(*psafes, p7)) goto err; return 1; err: if (free_safes) { sk_PKCS7_free(*psafes); *psafes = NULL; } PKCS7_free(p7); return 0; } int PKCS12_add_safe(STACK_OF(PKCS7) **psafes, STACK_OF(PKCS12_SAFEBAG) *bags, int nid_safe, int iter, const char *pass) { return PKCS12_add_safe_ex(psafes, bags, nid_safe, iter, pass, NULL, NULL); } static int pkcs12_remove_bag(STACK_OF(PKCS12_SAFEBAG) **pbags, PKCS12_SAFEBAG *bag) { PKCS12_SAFEBAG *tmp; if (pbags == NULL || bag == NULL) return 1; if ((tmp = sk_PKCS12_SAFEBAG_delete_ptr(*pbags, bag)) == NULL) return 0; PKCS12_SAFEBAG_free(tmp); return 1; } static int pkcs12_add_bag(STACK_OF(PKCS12_SAFEBAG) **pbags, PKCS12_SAFEBAG *bag) { int free_bags = 0; if (pbags == NULL) return 1; if (*pbags == NULL) { *pbags = sk_PKCS12_SAFEBAG_new_null(); if (*pbags == NULL) return 0; free_bags = 1; } if (!sk_PKCS12_SAFEBAG_push(*pbags, bag)) { if (free_bags) { sk_PKCS12_SAFEBAG_free(*pbags); *pbags = NULL; } return 0; } return 1; } PKCS12 *PKCS12_add_safes_ex(STACK_OF(PKCS7) *safes, int nid_p7, OSSL_LIB_CTX *ctx, const char *propq) { PKCS12 *p12; if (nid_p7 <= 0) nid_p7 = NID_pkcs7_data; p12 = PKCS12_init_ex(nid_p7, ctx, propq); if (p12 == NULL) return NULL; if (!PKCS12_pack_authsafes(p12, safes)) { PKCS12_free(p12); return NULL; } return p12; } PKCS12 *PKCS12_add_safes(STACK_OF(PKCS7) *safes, int nid_p7) { return PKCS12_add_safes_ex(safes, nid_p7, NULL, NULL); }
pkcs12
openssl/crypto/pkcs12/p12_crt.c
openssl
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <openssl/pem.h> #include <openssl/err.h> #include <openssl/pkcs12.h> #include "p12_local.h" static int newpass_p12(PKCS12 *p12, const char *oldpass, const char *newpass); static int newpass_bags(STACK_OF(PKCS12_SAFEBAG) *bags, const char *oldpass, const char *newpass, OSSL_LIB_CTX *libctx, const char *propq); static int newpass_bag(PKCS12_SAFEBAG *bag, const char *oldpass, const char *newpass, OSSL_LIB_CTX *libctx, const char *propq); static int alg_get(const X509_ALGOR *alg, int *pnid, int *piter, int *psaltlen, int *cipherid); int PKCS12_newpass(PKCS12 *p12, const char *oldpass, const char *newpass) { if (p12 == NULL) { ERR_raise(ERR_LIB_PKCS12, PKCS12_R_INVALID_NULL_PKCS12_POINTER); return 0; } if (p12->mac != NULL) { if (!PKCS12_verify_mac(p12, oldpass, -1)) { ERR_raise(ERR_LIB_PKCS12, PKCS12_R_MAC_VERIFY_FAILURE); return 0; } } if (!newpass_p12(p12, oldpass, newpass)) { ERR_raise(ERR_LIB_PKCS12, PKCS12_R_PARSE_ERROR); return 0; } return 1; } static int newpass_p12(PKCS12 *p12, const char *oldpass, const char *newpass) { STACK_OF(PKCS7) *asafes = NULL, *newsafes = NULL; STACK_OF(PKCS12_SAFEBAG) *bags = NULL; int i, bagnid, pbe_nid = 0, pbe_iter = 0, pbe_saltlen = 0, cipherid = NID_undef; PKCS7 *p7, *p7new; ASN1_OCTET_STRING *p12_data_tmp = NULL, *macoct = NULL; unsigned char mac[EVP_MAX_MD_SIZE]; unsigned int maclen; int rv = 0; if ((asafes = PKCS12_unpack_authsafes(p12)) == NULL) goto err; if ((newsafes = sk_PKCS7_new_null()) == NULL) goto err; for (i = 0; i < sk_PKCS7_num(asafes); i++) { p7 = sk_PKCS7_value(asafes, i); bagnid = OBJ_obj2nid(p7->type); if (bagnid == NID_pkcs7_data) { bags = PKCS12_unpack_p7data(p7); } else if (bagnid == NID_pkcs7_encrypted) { bags = PKCS12_unpack_p7encdata(p7, oldpass, -1); if (p7->d.encrypted == NULL || !alg_get(p7->d.encrypted->enc_data->algorithm, &pbe_nid, &pbe_iter, &pbe_saltlen, &cipherid)) goto err; } else { continue; } if (bags == NULL) goto err; if (!newpass_bags(bags, oldpass, newpass, p7->ctx.libctx, p7->ctx.propq)) goto err; if (bagnid == NID_pkcs7_data) p7new = PKCS12_pack_p7data(bags); else p7new = PKCS12_pack_p7encdata_ex(pbe_nid, newpass, -1, NULL, pbe_saltlen, pbe_iter, bags, p7->ctx.libctx, p7->ctx.propq); if (p7new == NULL || !sk_PKCS7_push(newsafes, p7new)) goto err; sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free); bags = NULL; } p12_data_tmp = p12->authsafes->d.data; if ((p12->authsafes->d.data = ASN1_OCTET_STRING_new()) == NULL) goto err; if (!PKCS12_pack_authsafes(p12, newsafes)) goto err; if (p12->mac != NULL) { if (!PKCS12_gen_mac(p12, newpass, -1, mac, &maclen)) goto err; X509_SIG_getm(p12->mac->dinfo, NULL, &macoct); if (!ASN1_OCTET_STRING_set(macoct, mac, maclen)) goto err; } rv = 1; err: if (rv == 1) { ASN1_OCTET_STRING_free(p12_data_tmp); } else if (p12_data_tmp != NULL) { ASN1_OCTET_STRING_free(p12->authsafes->d.data); p12->authsafes->d.data = p12_data_tmp; } sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free); sk_PKCS7_pop_free(asafes, PKCS7_free); sk_PKCS7_pop_free(newsafes, PKCS7_free); return rv; } static int newpass_bags(STACK_OF(PKCS12_SAFEBAG) *bags, const char *oldpass, const char *newpass, OSSL_LIB_CTX *libctx, const char *propq) { int i; for (i = 0; i < sk_PKCS12_SAFEBAG_num(bags); i++) { if (!newpass_bag(sk_PKCS12_SAFEBAG_value(bags, i), oldpass, newpass, libctx, propq)) return 0; } return 1; } static int newpass_bag(PKCS12_SAFEBAG *bag, const char *oldpass, const char *newpass, OSSL_LIB_CTX *libctx, const char *propq) { EVP_CIPHER *cipher = NULL; PKCS8_PRIV_KEY_INFO *p8; X509_SIG *p8new; int p8_nid, p8_saltlen, p8_iter, cipherid = 0; const X509_ALGOR *shalg; if (PKCS12_SAFEBAG_get_nid(bag) != NID_pkcs8ShroudedKeyBag) return 1; if ((p8 = PKCS8_decrypt_ex(bag->value.shkeybag, oldpass, -1, libctx, propq)) == NULL) return 0; X509_SIG_get0(bag->value.shkeybag, &shalg, NULL); if (!alg_get(shalg, &p8_nid, &p8_iter, &p8_saltlen, &cipherid)) { PKCS8_PRIV_KEY_INFO_free(p8); return 0; } if (cipherid != NID_undef) { cipher = EVP_CIPHER_fetch(libctx, OBJ_nid2sn(cipherid), propq); if (cipher == NULL) { PKCS8_PRIV_KEY_INFO_free(p8); return 0; } } p8new = PKCS8_encrypt_ex(p8_nid, cipher, newpass, -1, NULL, p8_saltlen, p8_iter, p8, libctx, propq); PKCS8_PRIV_KEY_INFO_free(p8); EVP_CIPHER_free(cipher); if (p8new == NULL) return 0; X509_SIG_free(bag->value.shkeybag); bag->value.shkeybag = p8new; return 1; } static int alg_get(const X509_ALGOR *alg, int *pnid, int *piter, int *psaltlen, int *cipherid) { int ret = 0, pbenid, aparamtype; int encnid, prfnid; const ASN1_OBJECT *aoid; const void *aparam; PBEPARAM *pbe = NULL; PBE2PARAM *pbe2 = NULL; PBKDF2PARAM *kdf = NULL; X509_ALGOR_get0(&aoid, &aparamtype, &aparam, alg); pbenid = OBJ_obj2nid(aoid); switch (pbenid) { case NID_pbes2: if (aparamtype == V_ASN1_SEQUENCE) pbe2 = ASN1_item_unpack(aparam, ASN1_ITEM_rptr(PBE2PARAM)); if (pbe2 == NULL) goto done; X509_ALGOR_get0(&aoid, &aparamtype, &aparam, pbe2->keyfunc); pbenid = OBJ_obj2nid(aoid); X509_ALGOR_get0(&aoid, NULL, NULL, pbe2->encryption); encnid = OBJ_obj2nid(aoid); if (aparamtype == V_ASN1_SEQUENCE) kdf = ASN1_item_unpack(aparam, ASN1_ITEM_rptr(PBKDF2PARAM)); if (kdf == NULL) goto done; if (kdf->salt->type != V_ASN1_OCTET_STRING) goto done; if (kdf->prf == NULL) { prfnid = NID_hmacWithSHA1; } else { X509_ALGOR_get0(&aoid, NULL, NULL, kdf->prf); prfnid = OBJ_obj2nid(aoid); } *psaltlen = kdf->salt->value.octet_string->length; *piter = ASN1_INTEGER_get(kdf->iter); *pnid = prfnid; *cipherid = encnid; break; default: pbe = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(PBEPARAM), alg->parameter); if (pbe == NULL) goto done; *pnid = OBJ_obj2nid(alg->algorithm); *piter = ASN1_INTEGER_get(pbe->iter); *psaltlen = pbe->salt->length; *cipherid = NID_undef; ret = 1; break; } ret = 1; done: if (kdf != NULL) PBKDF2PARAM_free(kdf); if (pbe2 != NULL) PBE2PARAM_free(pbe2); if (pbe != NULL) PBEPARAM_free(pbe); return ret; }
pkcs12
openssl/crypto/pkcs12/p12_npas.c
openssl
#include <stdio.h> #include "internal/cryptlib.h" #include <openssl/pkcs12.h> #include "crypto/pkcs7.h" #include "p12_local.h" PKCS12 *PKCS12_init_ex(int mode, OSSL_LIB_CTX *ctx, const char *propq) { PKCS12 *pkcs12; if ((pkcs12 = PKCS12_new()) == NULL) { ERR_raise(ERR_LIB_PKCS12, ERR_R_ASN1_LIB); return NULL; } if (!ASN1_INTEGER_set(pkcs12->version, 3)) goto err; pkcs12->authsafes->type = OBJ_nid2obj(mode); ossl_pkcs7_set0_libctx(pkcs12->authsafes, ctx); if (!ossl_pkcs7_set1_propq(pkcs12->authsafes, propq)) { ERR_raise(ERR_LIB_PKCS12, ERR_R_PKCS7_LIB); goto err; } switch (mode) { case NID_pkcs7_data: if ((pkcs12->authsafes->d.data = ASN1_OCTET_STRING_new()) == NULL) { ERR_raise(ERR_LIB_PKCS12, ERR_R_ASN1_LIB); goto err; } break; default: ERR_raise(ERR_LIB_PKCS12, PKCS12_R_UNSUPPORTED_PKCS12_MODE); goto err; } return pkcs12; err: PKCS12_free(pkcs12); return NULL; } PKCS12 *PKCS12_init(int mode) { return PKCS12_init_ex(mode, NULL, NULL); } const PKCS7_CTX *ossl_pkcs12_get0_pkcs7ctx(const PKCS12 *p12) { if (p12 == NULL || p12->authsafes == NULL) return NULL; return &p12->authsafes->ctx; }
pkcs12
openssl/crypto/pkcs12/p12_init.c
openssl
#include <stdio.h> #include "internal/cryptlib.h" #include <openssl/core.h> #include <openssl/pkcs12.h> #include "crypto/x509.h" X509_SIG *PKCS8_encrypt_ex(int pbe_nid, const EVP_CIPHER *cipher, const char *pass, int passlen, unsigned char *salt, int saltlen, int iter, PKCS8_PRIV_KEY_INFO *p8inf, OSSL_LIB_CTX *libctx, const char *propq) { X509_SIG *p8 = NULL; X509_ALGOR *pbe; if (pbe_nid == -1) { if (cipher == NULL) { ERR_raise(ERR_LIB_PKCS12, ERR_R_PASSED_NULL_PARAMETER); return NULL; } pbe = PKCS5_pbe2_set_iv_ex(cipher, iter, salt, saltlen, NULL, -1, libctx); } else { ERR_set_mark(); if (EVP_PBE_find(EVP_PBE_TYPE_PRF, pbe_nid, NULL, NULL, 0)) { ERR_clear_last_mark(); if (cipher == NULL) { ERR_raise(ERR_LIB_PKCS12, ERR_R_PASSED_NULL_PARAMETER); return NULL; } pbe = PKCS5_pbe2_set_iv_ex(cipher, iter, salt, saltlen, NULL, pbe_nid, libctx); } else { ERR_pop_to_mark(); pbe = PKCS5_pbe_set_ex(pbe_nid, iter, salt, saltlen, libctx); } } if (pbe == NULL) { ERR_raise(ERR_LIB_PKCS12, ERR_R_ASN1_LIB); return NULL; } p8 = PKCS8_set0_pbe_ex(pass, passlen, p8inf, pbe, libctx, propq); if (p8 == NULL) { X509_ALGOR_free(pbe); return NULL; } return p8; } X509_SIG *PKCS8_encrypt(int pbe_nid, const EVP_CIPHER *cipher, const char *pass, int passlen, unsigned char *salt, int saltlen, int iter, PKCS8_PRIV_KEY_INFO *p8inf) { return PKCS8_encrypt_ex(pbe_nid, cipher, pass, passlen, salt, saltlen, iter, p8inf, NULL, NULL); } X509_SIG *PKCS8_set0_pbe_ex(const char *pass, int passlen, PKCS8_PRIV_KEY_INFO *p8inf, X509_ALGOR *pbe, OSSL_LIB_CTX *ctx, const char *propq) { X509_SIG *p8; ASN1_OCTET_STRING *enckey; enckey = PKCS12_item_i2d_encrypt_ex(pbe, ASN1_ITEM_rptr(PKCS8_PRIV_KEY_INFO), pass, passlen, p8inf, 1, ctx, propq); if (!enckey) { ERR_raise(ERR_LIB_PKCS12, PKCS12_R_ENCRYPT_ERROR); return NULL; } p8 = OPENSSL_zalloc(sizeof(*p8)); if (p8 == NULL) { ASN1_OCTET_STRING_free(enckey); return NULL; } p8->algor = pbe; p8->digest = enckey; return p8; } X509_SIG *PKCS8_set0_pbe(const char *pass, int passlen, PKCS8_PRIV_KEY_INFO *p8inf, X509_ALGOR *pbe) { return PKCS8_set0_pbe_ex(pass, passlen, p8inf, pbe, NULL, NULL); }
pkcs12
openssl/crypto/pkcs12/p12_p8e.c
openssl
#include <stdio.h> #include "internal/cryptlib.h" #include <openssl/pkcs12.h> #include "crypto/x509.h" static int parse_pk12(PKCS12 *p12, const char *pass, int passlen, EVP_PKEY **pkey, STACK_OF(X509) *ocerts); static int parse_bags(const STACK_OF(PKCS12_SAFEBAG) *bags, const char *pass, int passlen, EVP_PKEY **pkey, STACK_OF(X509) *ocerts, OSSL_LIB_CTX *libctx, const char *propq); static int parse_bag(PKCS12_SAFEBAG *bag, const char *pass, int passlen, EVP_PKEY **pkey, STACK_OF(X509) *ocerts, OSSL_LIB_CTX *libctx, const char *propq); int PKCS12_parse(PKCS12 *p12, const char *pass, EVP_PKEY **pkey, X509 **cert, STACK_OF(X509) **ca) { STACK_OF(X509) *ocerts = NULL; X509 *x = NULL; if (pkey != NULL) *pkey = NULL; if (cert != NULL) *cert = NULL; if (p12 == NULL) { ERR_raise(ERR_LIB_PKCS12, PKCS12_R_INVALID_NULL_PKCS12_POINTER); return 0; } if (PKCS12_mac_present(p12)) { if (pass == NULL || *pass == '\0') { if (PKCS12_verify_mac(p12, NULL, 0)) pass = NULL; else if (PKCS12_verify_mac(p12, "", 0)) pass = ""; else { ERR_raise(ERR_LIB_PKCS12, PKCS12_R_MAC_VERIFY_FAILURE); goto err; } } else if (!PKCS12_verify_mac(p12, pass, -1)) { ERR_raise(ERR_LIB_PKCS12, PKCS12_R_MAC_VERIFY_FAILURE); goto err; } } else if (pass == NULL || *pass == '\0') { pass = NULL; } if ((cert != NULL || ca != NULL) && (ocerts = sk_X509_new_null()) == NULL) { ERR_raise(ERR_LIB_PKCS12, ERR_R_CRYPTO_LIB); goto err; } if (!parse_pk12(p12, pass, -1, pkey, ocerts)) { int err = ERR_peek_last_error(); if (ERR_GET_LIB(err) != ERR_LIB_EVP && ERR_GET_REASON(err) != EVP_R_UNSUPPORTED_ALGORITHM) ERR_raise(ERR_LIB_PKCS12, PKCS12_R_PARSE_ERROR); goto err; } while ((x = sk_X509_shift(ocerts)) != NULL) { if (pkey != NULL && *pkey != NULL && cert != NULL && *cert == NULL) { int match; ERR_set_mark(); match = X509_check_private_key(x, *pkey); ERR_pop_to_mark(); if (match) { *cert = x; continue; } } if (ca != NULL) { if (!ossl_x509_add_cert_new(ca, x, X509_ADD_FLAG_DEFAULT)) goto err; continue; } X509_free(x); } sk_X509_free(ocerts); return 1; err: if (pkey != NULL) { EVP_PKEY_free(*pkey); *pkey = NULL; } if (cert != NULL) { X509_free(*cert); *cert = NULL; } X509_free(x); OSSL_STACK_OF_X509_free(ocerts); return 0; } static int parse_pk12(PKCS12 *p12, const char *pass, int passlen, EVP_PKEY **pkey, STACK_OF(X509) *ocerts) { STACK_OF(PKCS7) *asafes; STACK_OF(PKCS12_SAFEBAG) *bags; int i, bagnid; PKCS7 *p7; if ((asafes = PKCS12_unpack_authsafes(p12)) == NULL) return 0; for (i = 0; i < sk_PKCS7_num(asafes); i++) { p7 = sk_PKCS7_value(asafes, i); bagnid = OBJ_obj2nid(p7->type); if (bagnid == NID_pkcs7_data) { bags = PKCS12_unpack_p7data(p7); } else if (bagnid == NID_pkcs7_encrypted) { bags = PKCS12_unpack_p7encdata(p7, pass, passlen); } else continue; if (!bags) { sk_PKCS7_pop_free(asafes, PKCS7_free); return 0; } if (!parse_bags(bags, pass, passlen, pkey, ocerts, p7->ctx.libctx, p7->ctx.propq)) { sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free); sk_PKCS7_pop_free(asafes, PKCS7_free); return 0; } sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free); } sk_PKCS7_pop_free(asafes, PKCS7_free); return 1; } static int parse_bags(const STACK_OF(PKCS12_SAFEBAG) *bags, const char *pass, int passlen, EVP_PKEY **pkey, STACK_OF(X509) *ocerts, OSSL_LIB_CTX *libctx, const char *propq) { int i; for (i = 0; i < sk_PKCS12_SAFEBAG_num(bags); i++) { if (!parse_bag(sk_PKCS12_SAFEBAG_value(bags, i), pass, passlen, pkey, ocerts, libctx, propq)) return 0; } return 1; } static int parse_bag(PKCS12_SAFEBAG *bag, const char *pass, int passlen, EVP_PKEY **pkey, STACK_OF(X509) *ocerts, OSSL_LIB_CTX *libctx, const char *propq) { PKCS8_PRIV_KEY_INFO *p8; X509 *x509; const ASN1_TYPE *attrib; ASN1_BMPSTRING *fname = NULL; ASN1_OCTET_STRING *lkid = NULL; if ((attrib = PKCS12_SAFEBAG_get0_attr(bag, NID_friendlyName))) fname = attrib->value.bmpstring; if ((attrib = PKCS12_SAFEBAG_get0_attr(bag, NID_localKeyID))) lkid = attrib->value.octet_string; switch (PKCS12_SAFEBAG_get_nid(bag)) { case NID_keyBag: if (pkey == NULL || *pkey != NULL) return 1; *pkey = EVP_PKCS82PKEY_ex(PKCS12_SAFEBAG_get0_p8inf(bag), libctx, propq); if (*pkey == NULL) return 0; break; case NID_pkcs8ShroudedKeyBag: if (pkey == NULL || *pkey != NULL) return 1; if ((p8 = PKCS12_decrypt_skey_ex(bag, pass, passlen, libctx, propq)) == NULL) return 0; *pkey = EVP_PKCS82PKEY_ex(p8, libctx, propq); PKCS8_PRIV_KEY_INFO_free(p8); if (!(*pkey)) return 0; break; case NID_certBag: if (ocerts == NULL || PKCS12_SAFEBAG_get_bag_nid(bag) != NID_x509Certificate) return 1; if ((x509 = PKCS12_SAFEBAG_get1_cert_ex(bag, libctx, propq)) == NULL) return 0; if (lkid && !X509_keyid_set1(x509, lkid->data, lkid->length)) { X509_free(x509); return 0; } if (fname) { int len, r; unsigned char *data; len = ASN1_STRING_to_UTF8(&data, fname); if (len >= 0) { r = X509_alias_set1(x509, data, len); OPENSSL_free(data); if (!r) { X509_free(x509); return 0; } } } if (!sk_X509_push(ocerts, x509)) { X509_free(x509); return 0; } break; case NID_safeContentsBag: return parse_bags(PKCS12_SAFEBAG_get0_safes(bag), pass, passlen, pkey, ocerts, libctx, propq); default: return 1; } return 1; }
pkcs12
openssl/crypto/pkcs12/p12_kiss.c
openssl
#include <stdio.h> #include "internal/cryptlib.h" #include <openssl/pkcs12.h> #include <openssl/bn.h> #include <openssl/trace.h> #include <openssl/kdf.h> #include <openssl/core_names.h> #include "internal/provider.h" int PKCS12_key_gen_asc_ex(const char *pass, int passlen, unsigned char *salt, int saltlen, int id, int iter, int n, unsigned char *out, const EVP_MD *md_type, OSSL_LIB_CTX *ctx, const char *propq) { int ret; unsigned char *unipass; int uniplen; if (pass == NULL) { unipass = NULL; uniplen = 0; } else if (!OPENSSL_asc2uni(pass, passlen, &unipass, &uniplen)) { ERR_raise(ERR_LIB_PKCS12, ERR_R_PKCS12_LIB); return 0; } ret = PKCS12_key_gen_uni_ex(unipass, uniplen, salt, saltlen, id, iter, n, out, md_type, ctx, propq); OPENSSL_clear_free(unipass, uniplen); return ret > 0; } int PKCS12_key_gen_asc(const char *pass, int passlen, unsigned char *salt, int saltlen, int id, int iter, int n, unsigned char *out, const EVP_MD *md_type) { return PKCS12_key_gen_asc_ex(pass, passlen, salt, saltlen, id, iter, n, out, md_type, NULL, NULL); } int PKCS12_key_gen_utf8_ex(const char *pass, int passlen, unsigned char *salt, int saltlen, int id, int iter, int n, unsigned char *out, const EVP_MD *md_type, OSSL_LIB_CTX *ctx, const char *propq) { int ret; unsigned char *unipass; int uniplen; if (pass == NULL) { unipass = NULL; uniplen = 0; } else if (!OPENSSL_utf82uni(pass, passlen, &unipass, &uniplen)) { ERR_raise(ERR_LIB_PKCS12, ERR_R_PKCS12_LIB); return 0; } ret = PKCS12_key_gen_uni_ex(unipass, uniplen, salt, saltlen, id, iter, n, out, md_type, ctx, propq); OPENSSL_clear_free(unipass, uniplen); return ret > 0; } int PKCS12_key_gen_utf8(const char *pass, int passlen, unsigned char *salt, int saltlen, int id, int iter, int n, unsigned char *out, const EVP_MD *md_type) { return PKCS12_key_gen_utf8_ex(pass, passlen, salt, saltlen, id, iter, n, out, md_type, NULL, NULL); } int PKCS12_key_gen_uni_ex(unsigned char *pass, int passlen, unsigned char *salt, int saltlen, int id, int iter, int n, unsigned char *out, const EVP_MD *md_type, OSSL_LIB_CTX *libctx, const char *propq) { int res = 0; EVP_KDF *kdf; EVP_KDF_CTX *ctx; OSSL_PARAM params[6], *p = params; if (n <= 0) return 0; kdf = EVP_KDF_fetch(libctx, "PKCS12KDF", propq); if (kdf == NULL) return 0; ctx = EVP_KDF_CTX_new(kdf); EVP_KDF_free(kdf); if (ctx == NULL) return 0; *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST, (char *)EVP_MD_get0_name(md_type), 0); *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_PASSWORD, pass, passlen); *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SALT, salt, saltlen); *p++ = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_PKCS12_ID, &id); *p++ = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_ITER, &iter); *p = OSSL_PARAM_construct_end(); OSSL_TRACE_BEGIN(PKCS12_KEYGEN) { BIO_printf(trc_out, "PKCS12_key_gen_uni_ex(): ID %d, ITER %d\n", id, iter); BIO_printf(trc_out, "Password (length %d):\n", passlen); BIO_hex_string(trc_out, 0, passlen, pass, passlen); BIO_printf(trc_out, "\n"); BIO_printf(trc_out, "Salt (length %d):\n", saltlen); BIO_hex_string(trc_out, 0, saltlen, salt, saltlen); BIO_printf(trc_out, "\n"); } OSSL_TRACE_END(PKCS12_KEYGEN); if (EVP_KDF_derive(ctx, out, (size_t)n, params)) { res = 1; OSSL_TRACE_BEGIN(PKCS12_KEYGEN) { BIO_printf(trc_out, "Output KEY (length %d)\n", n); BIO_hex_string(trc_out, 0, n, out, n); BIO_printf(trc_out, "\n"); } OSSL_TRACE_END(PKCS12_KEYGEN); } EVP_KDF_CTX_free(ctx); return res; } int PKCS12_key_gen_uni(unsigned char *pass, int passlen, unsigned char *salt, int saltlen, int id, int iter, int n, unsigned char *out, const EVP_MD *md_type) { return PKCS12_key_gen_uni_ex(pass, passlen, salt, saltlen, id, iter, n, out, md_type, NULL, NULL); }
pkcs12
openssl/crypto/pkcs12/p12_key.c
openssl
#include <stdio.h> #include "internal/cryptlib.h" #include <openssl/pkcs12.h> #include "p12_local.h" #include "crypto/pkcs7/pk7_local.h" unsigned char *OPENSSL_asc2uni(const char *asc, int asclen, unsigned char **uni, int *unilen) { int ulen, i; unsigned char *unitmp; if (asclen == -1) asclen = strlen(asc); if (asclen < 0) return NULL; ulen = asclen * 2 + 2; if ((unitmp = OPENSSL_malloc(ulen)) == NULL) return NULL; for (i = 0; i < ulen - 2; i += 2) { unitmp[i] = 0; unitmp[i + 1] = asc[i >> 1]; } unitmp[ulen - 2] = 0; unitmp[ulen - 1] = 0; if (unilen) *unilen = ulen; if (uni) *uni = unitmp; return unitmp; } char *OPENSSL_uni2asc(const unsigned char *uni, int unilen) { int asclen, i; char *asctmp; if (unilen & 1) return NULL; if (unilen < 0) return NULL; asclen = unilen / 2; if (!unilen || uni[unilen - 1]) asclen++; uni++; if ((asctmp = OPENSSL_malloc(asclen)) == NULL) return NULL; for (i = 0; i < unilen; i += 2) asctmp[i >> 1] = uni[i]; asctmp[asclen - 1] = 0; return asctmp; } unsigned char *OPENSSL_utf82uni(const char *asc, int asclen, unsigned char **uni, int *unilen) { int ulen, i, j; unsigned char *unitmp, *ret; unsigned long utf32chr = 0; if (asclen == -1) asclen = strlen(asc); for (ulen = 0, i = 0; i < asclen; i += j) { j = UTF8_getc((const unsigned char *)asc+i, asclen-i, &utf32chr); if (j < 0) return OPENSSL_asc2uni(asc, asclen, uni, unilen); if (utf32chr > 0x10FFFF) return NULL; if (utf32chr >= 0x10000) ulen += 2*2; else ulen += 2; } ulen += 2; if ((ret = OPENSSL_malloc(ulen)) == NULL) return NULL; for (unitmp = ret, i = 0; i < asclen; i += j) { j = UTF8_getc((const unsigned char *)asc+i, asclen-i, &utf32chr); if (utf32chr >= 0x10000) { unsigned int hi, lo; utf32chr -= 0x10000; hi = 0xD800 + (utf32chr>>10); lo = 0xDC00 + (utf32chr&0x3ff); *unitmp++ = (unsigned char)(hi>>8); *unitmp++ = (unsigned char)(hi); *unitmp++ = (unsigned char)(lo>>8); *unitmp++ = (unsigned char)(lo); } else { *unitmp++ = (unsigned char)(utf32chr>>8); *unitmp++ = (unsigned char)(utf32chr); } } *unitmp++ = 0; *unitmp++ = 0; if (unilen) *unilen = ulen; if (uni) *uni = ret; return ret; } static int bmp_to_utf8(char *str, const unsigned char *utf16, int len) { unsigned long utf32chr; if (len == 0) return 0; if (len < 2) return -1; utf32chr = (utf16[0]<<8) | utf16[1]; if (utf32chr >= 0xD800 && utf32chr < 0xE000) { unsigned int lo; if (len < 4) return -1; utf32chr -= 0xD800; utf32chr <<= 10; lo = (utf16[2]<<8) | utf16[3]; if (lo < 0xDC00 || lo >= 0xE000) return -1; utf32chr |= lo-0xDC00; utf32chr += 0x10000; } return UTF8_putc((unsigned char *)str, len > 4 ? 4 : len, utf32chr); } char *OPENSSL_uni2utf8(const unsigned char *uni, int unilen) { int asclen, i, j; char *asctmp; if (unilen & 1) return NULL; for (asclen = 0, i = 0; i < unilen; ) { j = bmp_to_utf8(NULL, uni+i, unilen-i); if (j < 0) return OPENSSL_uni2asc(uni, unilen); if (j == 4) i += 4; else i += 2; asclen += j; } if (!unilen || (uni[unilen-2]||uni[unilen - 1])) asclen++; if ((asctmp = OPENSSL_malloc(asclen)) == NULL) return NULL; for (asclen = 0, i = 0; i < unilen; ) { j = bmp_to_utf8(asctmp+asclen, uni+i, unilen-i); if (j == 4) i += 4; else i += 2; asclen += j; } if (!unilen || (uni[unilen-2]||uni[unilen - 1])) asctmp[asclen] = '\0'; return asctmp; } int i2d_PKCS12_bio(BIO *bp, const PKCS12 *p12) { return ASN1_item_i2d_bio(ASN1_ITEM_rptr(PKCS12), bp, p12); } #ifndef OPENSSL_NO_STDIO int i2d_PKCS12_fp(FILE *fp, const PKCS12 *p12) { return ASN1_item_i2d_fp(ASN1_ITEM_rptr(PKCS12), fp, p12); } #endif PKCS12 *d2i_PKCS12_bio(BIO *bp, PKCS12 **p12) { OSSL_LIB_CTX *libctx = NULL; const char *propq = NULL; const PKCS7_CTX *p7ctx = NULL; if (p12 != NULL) { p7ctx = ossl_pkcs12_get0_pkcs7ctx(*p12); if (p7ctx != NULL) { libctx = ossl_pkcs7_ctx_get0_libctx(p7ctx); propq = ossl_pkcs7_ctx_get0_propq(p7ctx); } } return ASN1_item_d2i_bio_ex(ASN1_ITEM_rptr(PKCS12), bp, p12, libctx, propq); } #ifndef OPENSSL_NO_STDIO PKCS12 *d2i_PKCS12_fp(FILE *fp, PKCS12 **p12) { OSSL_LIB_CTX *libctx = NULL; const char *propq = NULL; const PKCS7_CTX *p7ctx = NULL; if (p12 != NULL) { p7ctx = ossl_pkcs12_get0_pkcs7ctx(*p12); if (p7ctx != NULL) { libctx = ossl_pkcs7_ctx_get0_libctx(p7ctx); propq = ossl_pkcs7_ctx_get0_propq(p7ctx); } } return ASN1_item_d2i_fp_ex(ASN1_ITEM_rptr(PKCS12), fp, p12, libctx, propq); } #endif
pkcs12
openssl/crypto/pkcs12/p12_utl.c
openssl
#include <stdio.h> #include "internal/cryptlib.h" #include <openssl/pkcs12.h> #include "p12_local.h" int PKCS12_add_localkeyid(PKCS12_SAFEBAG *bag, unsigned char *name, int namelen) { if (X509at_add1_attr_by_NID(&bag->attrib, NID_localKeyID, V_ASN1_OCTET_STRING, name, namelen) != NULL) return 1; else return 0; } int PKCS8_add_keyusage(PKCS8_PRIV_KEY_INFO *p8, int usage) { unsigned char us_val = (unsigned char)usage; return PKCS8_pkey_add1_attr_by_NID(p8, NID_key_usage, V_ASN1_BIT_STRING, &us_val, 1); } int PKCS12_add_friendlyname_asc(PKCS12_SAFEBAG *bag, const char *name, int namelen) { if (X509at_add1_attr_by_NID(&bag->attrib, NID_friendlyName, MBSTRING_ASC, (unsigned char *)name, namelen) != NULL) return 1; else return 0; } int PKCS12_add_friendlyname_utf8(PKCS12_SAFEBAG *bag, const char *name, int namelen) { if (X509at_add1_attr_by_NID(&bag->attrib, NID_friendlyName, MBSTRING_UTF8, (unsigned char *)name, namelen) != NULL) return 1; else return 0; } int PKCS12_add_friendlyname_uni(PKCS12_SAFEBAG *bag, const unsigned char *name, int namelen) { if (X509at_add1_attr_by_NID(&bag->attrib, NID_friendlyName, MBSTRING_BMP, name, namelen) != NULL) return 1; else return 0; } int PKCS12_add_CSPName_asc(PKCS12_SAFEBAG *bag, const char *name, int namelen) { if (X509at_add1_attr_by_NID(&bag->attrib, NID_ms_csp_name, MBSTRING_ASC, (unsigned char *)name, namelen) != NULL) return 1; else return 0; } int PKCS12_add1_attr_by_NID(PKCS12_SAFEBAG *bag, int nid, int type, const unsigned char *bytes, int len) { if (X509at_add1_attr_by_NID(&bag->attrib, nid, type, bytes, len) != NULL) return 1; else return 0; } int PKCS12_add1_attr_by_txt(PKCS12_SAFEBAG *bag, const char *attrname, int type, const unsigned char *bytes, int len) { if (X509at_add1_attr_by_txt(&bag->attrib, attrname, type, bytes, len) != NULL) return 1; else return 0; } ASN1_TYPE *PKCS12_get_attr_gen(const STACK_OF(X509_ATTRIBUTE) *attrs, int attr_nid) { int i = X509at_get_attr_by_NID(attrs, attr_nid, -1); if (i < 0) return NULL; return X509_ATTRIBUTE_get0_type(X509at_get_attr(attrs, i), 0); } char *PKCS12_get_friendlyname(PKCS12_SAFEBAG *bag) { const ASN1_TYPE *atype; if ((atype = PKCS12_SAFEBAG_get0_attr(bag, NID_friendlyName)) == NULL) return NULL; if (atype->type != V_ASN1_BMPSTRING) return NULL; return OPENSSL_uni2utf8(atype->value.bmpstring->data, atype->value.bmpstring->length); } const STACK_OF(X509_ATTRIBUTE) * PKCS12_SAFEBAG_get0_attrs(const PKCS12_SAFEBAG *bag) { return bag->attrib; } void PKCS12_SAFEBAG_set0_attrs(PKCS12_SAFEBAG *bag, STACK_OF(X509_ATTRIBUTE) *attrs) { if (bag->attrib != attrs) sk_X509_ATTRIBUTE_free(bag->attrib); bag->attrib = attrs; }
pkcs12
openssl/crypto/pkcs12/p12_attr.c
openssl
#include <stdio.h> #include "internal/cryptlib.h" #include <openssl/pkcs12.h> #include "p12_local.h" #include "crypto/x509.h" #ifndef OPENSSL_NO_DEPRECATED_1_1_0 ASN1_TYPE *PKCS12_get_attr(const PKCS12_SAFEBAG *bag, int attr_nid) { return PKCS12_get_attr_gen(bag->attrib, attr_nid); } #endif const ASN1_TYPE *PKCS12_SAFEBAG_get0_attr(const PKCS12_SAFEBAG *bag, int attr_nid) { return PKCS12_get_attr_gen(bag->attrib, attr_nid); } ASN1_TYPE *PKCS8_get_attr(PKCS8_PRIV_KEY_INFO *p8, int attr_nid) { return PKCS12_get_attr_gen(PKCS8_pkey_get0_attrs(p8), attr_nid); } const PKCS8_PRIV_KEY_INFO *PKCS12_SAFEBAG_get0_p8inf(const PKCS12_SAFEBAG *bag) { if (PKCS12_SAFEBAG_get_nid(bag) != NID_keyBag) return NULL; return bag->value.keybag; } const X509_SIG *PKCS12_SAFEBAG_get0_pkcs8(const PKCS12_SAFEBAG *bag) { if (OBJ_obj2nid(bag->type) != NID_pkcs8ShroudedKeyBag) return NULL; return bag->value.shkeybag; } const STACK_OF(PKCS12_SAFEBAG) * PKCS12_SAFEBAG_get0_safes(const PKCS12_SAFEBAG *bag) { if (OBJ_obj2nid(bag->type) != NID_safeContentsBag) return NULL; return bag->value.safes; } const ASN1_OBJECT *PKCS12_SAFEBAG_get0_type(const PKCS12_SAFEBAG *bag) { return bag->type; } int PKCS12_SAFEBAG_get_nid(const PKCS12_SAFEBAG *bag) { return OBJ_obj2nid(bag->type); } int PKCS12_SAFEBAG_get_bag_nid(const PKCS12_SAFEBAG *bag) { int btype = PKCS12_SAFEBAG_get_nid(bag); if (btype != NID_certBag && btype != NID_crlBag && btype != NID_secretBag) return -1; return OBJ_obj2nid(bag->value.bag->type); } const ASN1_OBJECT *PKCS12_SAFEBAG_get0_bag_type(const PKCS12_SAFEBAG *bag) { return bag->value.bag->type; } const ASN1_TYPE *PKCS12_SAFEBAG_get0_bag_obj(const PKCS12_SAFEBAG *bag) { return bag->value.bag->value.other; } X509 *PKCS12_SAFEBAG_get1_cert(const PKCS12_SAFEBAG *bag) { if (PKCS12_SAFEBAG_get_nid(bag) != NID_certBag) return NULL; if (OBJ_obj2nid(bag->value.bag->type) != NID_x509Certificate) return NULL; return ASN1_item_unpack(bag->value.bag->value.octet, ASN1_ITEM_rptr(X509)); } X509_CRL *PKCS12_SAFEBAG_get1_crl(const PKCS12_SAFEBAG *bag) { if (PKCS12_SAFEBAG_get_nid(bag) != NID_crlBag) return NULL; if (OBJ_obj2nid(bag->value.bag->type) != NID_x509Crl) return NULL; return ASN1_item_unpack(bag->value.bag->value.octet, ASN1_ITEM_rptr(X509_CRL)); } X509 *PKCS12_SAFEBAG_get1_cert_ex(const PKCS12_SAFEBAG *bag, OSSL_LIB_CTX *libctx, const char *propq) { X509 *ret = NULL; if (PKCS12_SAFEBAG_get_nid(bag) != NID_certBag) return NULL; if (OBJ_obj2nid(bag->value.bag->type) != NID_x509Certificate) return NULL; ret = ASN1_item_unpack_ex(bag->value.bag->value.octet, ASN1_ITEM_rptr(X509), libctx, propq); if (!ossl_x509_set0_libctx(ret, libctx, propq)) { X509_free(ret); return NULL; } return ret; } X509_CRL *PKCS12_SAFEBAG_get1_crl_ex(const PKCS12_SAFEBAG *bag, OSSL_LIB_CTX *libctx, const char *propq) { X509_CRL *ret = NULL; if (PKCS12_SAFEBAG_get_nid(bag) != NID_crlBag) return NULL; if (OBJ_obj2nid(bag->value.bag->type) != NID_x509Crl) return NULL; ret = ASN1_item_unpack_ex(bag->value.bag->value.octet, ASN1_ITEM_rptr(X509_CRL), libctx, propq); if (!ossl_x509_crl_set0_libctx(ret, libctx, propq)) { X509_CRL_free(ret); return NULL; } return ret; } PKCS12_SAFEBAG *PKCS12_SAFEBAG_create_cert(X509 *x509) { return PKCS12_item_pack_safebag(x509, ASN1_ITEM_rptr(X509), NID_x509Certificate, NID_certBag); } PKCS12_SAFEBAG *PKCS12_SAFEBAG_create_crl(X509_CRL *crl) { return PKCS12_item_pack_safebag(crl, ASN1_ITEM_rptr(X509_CRL), NID_x509Crl, NID_crlBag); } PKCS12_SAFEBAG *PKCS12_SAFEBAG_create_secret(int type, int vtype, const unsigned char *value, int len) { PKCS12_BAGS *bag; PKCS12_SAFEBAG *safebag; if ((bag = PKCS12_BAGS_new()) == NULL) { ERR_raise(ERR_LIB_PKCS12, ERR_R_ASN1_LIB); return NULL; } bag->type = OBJ_nid2obj(type); switch (vtype) { case V_ASN1_OCTET_STRING: { ASN1_OCTET_STRING *strtmp = ASN1_OCTET_STRING_new(); if (strtmp == NULL) { ERR_raise(ERR_LIB_PKCS12, ERR_R_ASN1_LIB); goto err; } if (!ASN1_OCTET_STRING_set(strtmp, value, len)) { ASN1_OCTET_STRING_free(strtmp); ERR_raise(ERR_LIB_PKCS12, PKCS12_R_ENCODE_ERROR); goto err; } bag->value.other = ASN1_TYPE_new(); if (bag->value.other == NULL) { ASN1_OCTET_STRING_free(strtmp); ERR_raise(ERR_LIB_PKCS12, ERR_R_ASN1_LIB); goto err; } ASN1_TYPE_set(bag->value.other, vtype, strtmp); } break; default: ERR_raise(ERR_LIB_PKCS12, PKCS12_R_INVALID_TYPE); goto err; } if ((safebag = PKCS12_SAFEBAG_new()) == NULL) { ERR_raise(ERR_LIB_PKCS12, ERR_R_ASN1_LIB); goto err; } safebag->value.bag = bag; safebag->type = OBJ_nid2obj(NID_secretBag); return safebag; err: PKCS12_BAGS_free(bag); return NULL; } PKCS12_SAFEBAG *PKCS12_SAFEBAG_create0_p8inf(PKCS8_PRIV_KEY_INFO *p8) { PKCS12_SAFEBAG *bag = PKCS12_SAFEBAG_new(); if (bag == NULL) { ERR_raise(ERR_LIB_PKCS12, ERR_R_ASN1_LIB); return NULL; } bag->type = OBJ_nid2obj(NID_keyBag); bag->value.keybag = p8; return bag; } PKCS12_SAFEBAG *PKCS12_SAFEBAG_create0_pkcs8(X509_SIG *p8) { PKCS12_SAFEBAG *bag = PKCS12_SAFEBAG_new(); if (bag == NULL) { ERR_raise(ERR_LIB_PKCS12, ERR_R_ASN1_LIB); return NULL; } bag->type = OBJ_nid2obj(NID_pkcs8ShroudedKeyBag); bag->value.shkeybag = p8; return bag; } PKCS12_SAFEBAG *PKCS12_SAFEBAG_create_pkcs8_encrypt_ex(int pbe_nid, const char *pass, int passlen, unsigned char *salt, int saltlen, int iter, PKCS8_PRIV_KEY_INFO *p8inf, OSSL_LIB_CTX *ctx, const char *propq) { PKCS12_SAFEBAG *bag = NULL; const EVP_CIPHER *pbe_ciph = NULL; EVP_CIPHER *pbe_ciph_fetch = NULL; X509_SIG *p8; ERR_set_mark(); pbe_ciph = pbe_ciph_fetch = EVP_CIPHER_fetch(ctx, OBJ_nid2sn(pbe_nid), propq); if (pbe_ciph == NULL) pbe_ciph = EVP_get_cipherbynid(pbe_nid); ERR_pop_to_mark(); if (pbe_ciph != NULL) pbe_nid = -1; p8 = PKCS8_encrypt_ex(pbe_nid, pbe_ciph, pass, passlen, salt, saltlen, iter, p8inf, ctx, propq); if (p8 == NULL) goto err; bag = PKCS12_SAFEBAG_create0_pkcs8(p8); if (bag == NULL) X509_SIG_free(p8); err: EVP_CIPHER_free(pbe_ciph_fetch); return bag; } PKCS12_SAFEBAG *PKCS12_SAFEBAG_create_pkcs8_encrypt(int pbe_nid, const char *pass, int passlen, unsigned char *salt, int saltlen, int iter, PKCS8_PRIV_KEY_INFO *p8inf) { return PKCS12_SAFEBAG_create_pkcs8_encrypt_ex(pbe_nid, pass, passlen, salt, saltlen, iter, p8inf, NULL, NULL); }
pkcs12
openssl/crypto/pkcs12/p12_sbag.c
openssl
#include "internal/deprecated.h" #include <stdio.h> #include <stdlib.h> #include <string.h> #include "internal/cryptlib.h" #include <openssl/cmac.h> #include <openssl/err.h> #define LOCAL_BUF_SIZE 2048 struct CMAC_CTX_st { EVP_CIPHER_CTX *cctx; unsigned char k1[EVP_MAX_BLOCK_LENGTH]; unsigned char k2[EVP_MAX_BLOCK_LENGTH]; unsigned char tbl[EVP_MAX_BLOCK_LENGTH]; unsigned char last_block[EVP_MAX_BLOCK_LENGTH]; int nlast_block; }; static void make_kn(unsigned char *k1, const unsigned char *l, int bl) { int i; unsigned char c = l[0], carry = c >> 7, cnext; for (i = 0; i < bl - 1; i++, c = cnext) k1[i] = (c << 1) | ((cnext = l[i + 1]) >> 7); k1[i] = (c << 1) ^ ((0 - carry) & (bl == 16 ? 0x87 : 0x1b)); } CMAC_CTX *CMAC_CTX_new(void) { CMAC_CTX *ctx; if ((ctx = OPENSSL_malloc(sizeof(*ctx))) == NULL) return NULL; ctx->cctx = EVP_CIPHER_CTX_new(); if (ctx->cctx == NULL) { OPENSSL_free(ctx); return NULL; } ctx->nlast_block = -1; return ctx; } void CMAC_CTX_cleanup(CMAC_CTX *ctx) { EVP_CIPHER_CTX_reset(ctx->cctx); OPENSSL_cleanse(ctx->tbl, EVP_MAX_BLOCK_LENGTH); OPENSSL_cleanse(ctx->k1, EVP_MAX_BLOCK_LENGTH); OPENSSL_cleanse(ctx->k2, EVP_MAX_BLOCK_LENGTH); OPENSSL_cleanse(ctx->last_block, EVP_MAX_BLOCK_LENGTH); ctx->nlast_block = -1; } EVP_CIPHER_CTX *CMAC_CTX_get0_cipher_ctx(CMAC_CTX *ctx) { return ctx->cctx; } void CMAC_CTX_free(CMAC_CTX *ctx) { if (!ctx) return; CMAC_CTX_cleanup(ctx); EVP_CIPHER_CTX_free(ctx->cctx); OPENSSL_free(ctx); } int CMAC_CTX_copy(CMAC_CTX *out, const CMAC_CTX *in) { int bl; if (in->nlast_block == -1) return 0; if ((bl = EVP_CIPHER_CTX_get_block_size(in->cctx)) == 0) return 0; if (!EVP_CIPHER_CTX_copy(out->cctx, in->cctx)) return 0; memcpy(out->k1, in->k1, bl); memcpy(out->k2, in->k2, bl); memcpy(out->tbl, in->tbl, bl); memcpy(out->last_block, in->last_block, bl); out->nlast_block = in->nlast_block; return 1; } int CMAC_Init(CMAC_CTX *ctx, const void *key, size_t keylen, const EVP_CIPHER *cipher, ENGINE *impl) { static const unsigned char zero_iv[EVP_MAX_BLOCK_LENGTH] = { 0 }; int block_len; if (!key && !cipher && !impl && keylen == 0) { if (ctx->nlast_block == -1) return 0; if (!EVP_EncryptInit_ex(ctx->cctx, NULL, NULL, NULL, zero_iv)) return 0; block_len = EVP_CIPHER_CTX_get_block_size(ctx->cctx); if (block_len == 0) return 0; memset(ctx->tbl, 0, block_len); ctx->nlast_block = 0; return 1; } if (cipher != NULL) { ctx->nlast_block = -1; if (!EVP_EncryptInit_ex(ctx->cctx, cipher, impl, NULL, NULL)) return 0; } if (key != NULL) { int bl; ctx->nlast_block = -1; if (EVP_CIPHER_CTX_get0_cipher(ctx->cctx) == NULL) return 0; if (EVP_CIPHER_CTX_set_key_length(ctx->cctx, keylen) <= 0) return 0; if (!EVP_EncryptInit_ex(ctx->cctx, NULL, NULL, key, zero_iv)) return 0; if ((bl = EVP_CIPHER_CTX_get_block_size(ctx->cctx)) < 0) return 0; if (EVP_Cipher(ctx->cctx, ctx->tbl, zero_iv, bl) <= 0) return 0; make_kn(ctx->k1, ctx->tbl, bl); make_kn(ctx->k2, ctx->k1, bl); OPENSSL_cleanse(ctx->tbl, bl); if (!EVP_EncryptInit_ex(ctx->cctx, NULL, NULL, NULL, zero_iv)) return 0; memset(ctx->tbl, 0, bl); ctx->nlast_block = 0; } return 1; } int CMAC_Update(CMAC_CTX *ctx, const void *in, size_t dlen) { const unsigned char *data = in; int bl; size_t max_burst_blocks, cipher_blocks; unsigned char buf[LOCAL_BUF_SIZE]; if (ctx->nlast_block == -1) return 0; if (dlen == 0) return 1; if ((bl = EVP_CIPHER_CTX_get_block_size(ctx->cctx)) == 0) return 0; if (ctx->nlast_block > 0) { size_t nleft; nleft = bl - ctx->nlast_block; if (dlen < nleft) nleft = dlen; memcpy(ctx->last_block + ctx->nlast_block, data, nleft); dlen -= nleft; ctx->nlast_block += nleft; if (dlen == 0) return 1; data += nleft; if (EVP_Cipher(ctx->cctx, ctx->tbl, ctx->last_block, bl) <= 0) return 0; } max_burst_blocks = LOCAL_BUF_SIZE / bl; cipher_blocks = (dlen - 1) / bl; if (max_burst_blocks == 0) { while (dlen > (size_t)bl) { if (EVP_Cipher(ctx->cctx, ctx->tbl, data, bl) <= 0) return 0; dlen -= bl; data += bl; } } else { while (cipher_blocks > max_burst_blocks) { if (EVP_Cipher(ctx->cctx, buf, data, max_burst_blocks * bl) <= 0) return 0; dlen -= max_burst_blocks * bl; data += max_burst_blocks * bl; cipher_blocks -= max_burst_blocks; } if (cipher_blocks > 0) { if (EVP_Cipher(ctx->cctx, buf, data, cipher_blocks * bl) <= 0) return 0; dlen -= cipher_blocks * bl; data += cipher_blocks * bl; memcpy(ctx->tbl, &buf[(cipher_blocks - 1) * bl], bl); } } memcpy(ctx->last_block, data, dlen); ctx->nlast_block = dlen; return 1; } int CMAC_Final(CMAC_CTX *ctx, unsigned char *out, size_t *poutlen) { int i, bl, lb; if (ctx->nlast_block == -1) return 0; if ((bl = EVP_CIPHER_CTX_get_block_size(ctx->cctx)) == 0) return 0; if (poutlen != NULL) *poutlen = (size_t)bl; if (!out) return 1; lb = ctx->nlast_block; if (lb == bl) { for (i = 0; i < bl; i++) out[i] = ctx->last_block[i] ^ ctx->k1[i]; } else { ctx->last_block[lb] = 0x80; if (bl - lb > 1) memset(ctx->last_block + lb + 1, 0, bl - lb - 1); for (i = 0; i < bl; i++) out[i] = ctx->last_block[i] ^ ctx->k2[i]; } if (EVP_Cipher(ctx->cctx, out, out, bl) <= 0) { OPENSSL_cleanse(out, bl); return 0; } return 1; } int CMAC_resume(CMAC_CTX *ctx) { if (ctx->nlast_block == -1) return 0; return EVP_EncryptInit_ex(ctx->cctx, NULL, NULL, NULL, ctx->tbl); }
cmac
openssl/crypto/cmac/cmac.c
openssl
#include "internal/deprecated.h" #include "internal/cryptlib.h" #include <openssl/aes.h> #include "aes_local.h" #if defined(__i386__) || defined(__x86_64__) # define UNALIGNED_MEMOPS_ARE_FAST 1 #else # define UNALIGNED_MEMOPS_ARE_FAST 0 #endif #define N_WORDS (AES_BLOCK_SIZE / sizeof(unsigned long)) typedef struct { unsigned long data[N_WORDS]; #if defined(__GNUC__) && UNALIGNED_MEMOPS_ARE_FAST } aes_block_t __attribute((__aligned__(1))); #else } aes_block_t; #endif #if UNALIGNED_MEMOPS_ARE_FAST # define load_block(d, s) (d) = *(const aes_block_t *)(s) # define store_block(d, s) *(aes_block_t *)(d) = (s) #else # define load_block(d, s) memcpy((d).data, (s), AES_BLOCK_SIZE) # define store_block(d, s) memcpy((d), (s).data, AES_BLOCK_SIZE) #endif void AES_ige_encrypt(const unsigned char *in, unsigned char *out, size_t length, const AES_KEY *key, unsigned char *ivec, const int enc) { size_t n; size_t len = length / AES_BLOCK_SIZE; if (length == 0) return; OPENSSL_assert(in && out && key && ivec); OPENSSL_assert((AES_ENCRYPT == enc) || (AES_DECRYPT == enc)); OPENSSL_assert((length % AES_BLOCK_SIZE) == 0); if (AES_ENCRYPT == enc) { if (in != out && (UNALIGNED_MEMOPS_ARE_FAST || ((size_t)in | (size_t)out | (size_t)ivec) % sizeof(long) == 0)) { aes_block_t *ivp = (aes_block_t *) ivec; aes_block_t *iv2p = (aes_block_t *) (ivec + AES_BLOCK_SIZE); while (len) { aes_block_t *inp = (aes_block_t *) in; aes_block_t *outp = (aes_block_t *) out; for (n = 0; n < N_WORDS; ++n) outp->data[n] = inp->data[n] ^ ivp->data[n]; AES_encrypt((unsigned char *)outp->data, (unsigned char *)outp->data, key); for (n = 0; n < N_WORDS; ++n) outp->data[n] ^= iv2p->data[n]; ivp = outp; iv2p = inp; --len; in += AES_BLOCK_SIZE; out += AES_BLOCK_SIZE; } memcpy(ivec, ivp->data, AES_BLOCK_SIZE); memcpy(ivec + AES_BLOCK_SIZE, iv2p->data, AES_BLOCK_SIZE); } else { aes_block_t tmp, tmp2; aes_block_t iv; aes_block_t iv2; load_block(iv, ivec); load_block(iv2, ivec + AES_BLOCK_SIZE); while (len) { load_block(tmp, in); for (n = 0; n < N_WORDS; ++n) tmp2.data[n] = tmp.data[n] ^ iv.data[n]; AES_encrypt((unsigned char *)tmp2.data, (unsigned char *)tmp2.data, key); for (n = 0; n < N_WORDS; ++n) tmp2.data[n] ^= iv2.data[n]; store_block(out, tmp2); iv = tmp2; iv2 = tmp; --len; in += AES_BLOCK_SIZE; out += AES_BLOCK_SIZE; } memcpy(ivec, iv.data, AES_BLOCK_SIZE); memcpy(ivec + AES_BLOCK_SIZE, iv2.data, AES_BLOCK_SIZE); } } else { if (in != out && (UNALIGNED_MEMOPS_ARE_FAST || ((size_t)in | (size_t)out | (size_t)ivec) % sizeof(long) == 0)) { aes_block_t *ivp = (aes_block_t *) ivec; aes_block_t *iv2p = (aes_block_t *) (ivec + AES_BLOCK_SIZE); while (len) { aes_block_t tmp; aes_block_t *inp = (aes_block_t *) in; aes_block_t *outp = (aes_block_t *) out; for (n = 0; n < N_WORDS; ++n) tmp.data[n] = inp->data[n] ^ iv2p->data[n]; AES_decrypt((unsigned char *)tmp.data, (unsigned char *)outp->data, key); for (n = 0; n < N_WORDS; ++n) outp->data[n] ^= ivp->data[n]; ivp = inp; iv2p = outp; --len; in += AES_BLOCK_SIZE; out += AES_BLOCK_SIZE; } memcpy(ivec, ivp->data, AES_BLOCK_SIZE); memcpy(ivec + AES_BLOCK_SIZE, iv2p->data, AES_BLOCK_SIZE); } else { aes_block_t tmp, tmp2; aes_block_t iv; aes_block_t iv2; load_block(iv, ivec); load_block(iv2, ivec + AES_BLOCK_SIZE); while (len) { load_block(tmp, in); tmp2 = tmp; for (n = 0; n < N_WORDS; ++n) tmp.data[n] ^= iv2.data[n]; AES_decrypt((unsigned char *)tmp.data, (unsigned char *)tmp.data, key); for (n = 0; n < N_WORDS; ++n) tmp.data[n] ^= iv.data[n]; store_block(out, tmp); iv = tmp2; iv2 = tmp; --len; in += AES_BLOCK_SIZE; out += AES_BLOCK_SIZE; } memcpy(ivec, iv.data, AES_BLOCK_SIZE); memcpy(ivec + AES_BLOCK_SIZE, iv2.data, AES_BLOCK_SIZE); } } } void AES_bi_ige_encrypt(const unsigned char *in, unsigned char *out, size_t length, const AES_KEY *key, const AES_KEY *key2, const unsigned char *ivec, const int enc) { size_t n; size_t len = length; unsigned char tmp[AES_BLOCK_SIZE]; unsigned char tmp2[AES_BLOCK_SIZE]; unsigned char tmp3[AES_BLOCK_SIZE]; unsigned char prev[AES_BLOCK_SIZE]; const unsigned char *iv; const unsigned char *iv2; OPENSSL_assert(in && out && key && ivec); OPENSSL_assert((AES_ENCRYPT == enc) || (AES_DECRYPT == enc)); OPENSSL_assert((length % AES_BLOCK_SIZE) == 0); if (AES_ENCRYPT == enc) { iv = ivec; iv2 = ivec + AES_BLOCK_SIZE; while (len >= AES_BLOCK_SIZE) { for (n = 0; n < AES_BLOCK_SIZE; ++n) out[n] = in[n] ^ iv[n]; AES_encrypt(out, out, key); for (n = 0; n < AES_BLOCK_SIZE; ++n) out[n] ^= iv2[n]; iv = out; memcpy(prev, in, AES_BLOCK_SIZE); iv2 = prev; len -= AES_BLOCK_SIZE; in += AES_BLOCK_SIZE; out += AES_BLOCK_SIZE; } iv = ivec + AES_BLOCK_SIZE * 2; iv2 = ivec + AES_BLOCK_SIZE * 3; len = length; while (len >= AES_BLOCK_SIZE) { out -= AES_BLOCK_SIZE; memcpy(tmp, out, AES_BLOCK_SIZE); for (n = 0; n < AES_BLOCK_SIZE; ++n) out[n] ^= iv[n]; AES_encrypt(out, out, key); for (n = 0; n < AES_BLOCK_SIZE; ++n) out[n] ^= iv2[n]; iv = out; memcpy(prev, tmp, AES_BLOCK_SIZE); iv2 = prev; len -= AES_BLOCK_SIZE; } } else { iv = ivec + AES_BLOCK_SIZE * 2; iv2 = ivec + AES_BLOCK_SIZE * 3; in += length; out += length; while (len >= AES_BLOCK_SIZE) { in -= AES_BLOCK_SIZE; out -= AES_BLOCK_SIZE; memcpy(tmp, in, AES_BLOCK_SIZE); memcpy(tmp2, in, AES_BLOCK_SIZE); for (n = 0; n < AES_BLOCK_SIZE; ++n) tmp[n] ^= iv2[n]; AES_decrypt(tmp, out, key); for (n = 0; n < AES_BLOCK_SIZE; ++n) out[n] ^= iv[n]; memcpy(tmp3, tmp2, AES_BLOCK_SIZE); iv = tmp3; iv2 = out; len -= AES_BLOCK_SIZE; } iv = ivec; iv2 = ivec + AES_BLOCK_SIZE; len = length; while (len >= AES_BLOCK_SIZE) { memcpy(tmp, out, AES_BLOCK_SIZE); memcpy(tmp2, out, AES_BLOCK_SIZE); for (n = 0; n < AES_BLOCK_SIZE; ++n) tmp[n] ^= iv2[n]; AES_decrypt(tmp, out, key); for (n = 0; n < AES_BLOCK_SIZE; ++n) out[n] ^= iv[n]; memcpy(tmp3, tmp2, AES_BLOCK_SIZE); iv = tmp3; iv2 = out; len -= AES_BLOCK_SIZE; in += AES_BLOCK_SIZE; out += AES_BLOCK_SIZE; } } }
aes
openssl/crypto/aes/aes_ige.c
openssl
#include <openssl/opensslv.h> #include <openssl/aes.h> #include "aes_local.h" #ifndef OPENSSL_NO_DEPRECATED_3_0 const char *AES_options(void) { # ifdef FULL_UNROLL return "aes(full)"; # else return "aes(partial)"; # endif } #endif
aes
openssl/crypto/aes/aes_misc.c
openssl
#include <assert.h> #include <stdlib.h> #include <openssl/aes.h> #include "aes_local.h" #define AES_COMPACT_IN_OUTER_ROUNDS #ifdef AES_COMPACT_IN_OUTER_ROUNDS # undef AES_COMPACT_IN_INNER_ROUNDS #endif #if 1 static void prefetch256(const void *table) { volatile unsigned long *t = (void *)table, ret; unsigned long sum; int i; for (sum = 0, i = 0; i < 256/sizeof(t[0]); i += 32/sizeof(t[0])) sum ^= t[i]; ret = sum; } #else # define prefetch256(t) #endif #undef GETU32 #define GETU32(p) (*((u32*)(p))) #if (defined(_WIN32) || defined(_WIN64)) && !defined(__MINGW32__) #define U64(C) C##UI64 #elif defined(__arch64__) #define U64(C) C##UL #else #define U64(C) C##ULL #endif #undef ROTATE #if defined(_MSC_VER) # define ROTATE(a,n) _lrotl(a,n) #elif defined(__ICC) # define ROTATE(a,n) _rotl(a,n) #elif defined(__GNUC__) && __GNUC__>=2 # if defined(__i386) || defined(__i386__) || defined(__x86_64) || defined(__x86_64__) # define ROTATE(a,n) ({ register unsigned int ret; \ asm ( \ "roll %1,%0" \ : "=r"(ret) \ : "I"(n), "0"(a) \ : "cc"); \ ret; \ }) # endif #endif #define Te0 (u32)((u64*)((u8*)Te+0)) #define Te1 (u32)((u64*)((u8*)Te+3)) #define Te2 (u32)((u64*)((u8*)Te+2)) #define Te3 (u32)((u64*)((u8*)Te+1)) #define Td0 (u32)((u64*)((u8*)Td+0)) #define Td1 (u32)((u64*)((u8*)Td+3)) #define Td2 (u32)((u64*)((u8*)Td+2)) #define Td3 (u32)((u64*)((u8*)Td+1)) static const u64 Te[256] = { U64(0xa56363c6a56363c6), U64(0x847c7cf8847c7cf8), U64(0x997777ee997777ee), U64(0x8d7b7bf68d7b7bf6), U64(0x0df2f2ff0df2f2ff), U64(0xbd6b6bd6bd6b6bd6), U64(0xb16f6fdeb16f6fde), U64(0x54c5c59154c5c591), U64(0x5030306050303060), U64(0x0301010203010102), U64(0xa96767cea96767ce), U64(0x7d2b2b567d2b2b56), U64(0x19fefee719fefee7), U64(0x62d7d7b562d7d7b5), U64(0xe6abab4de6abab4d), U64(0x9a7676ec9a7676ec), U64(0x45caca8f45caca8f), U64(0x9d82821f9d82821f), U64(0x40c9c98940c9c989), U64(0x877d7dfa877d7dfa), U64(0x15fafaef15fafaef), U64(0xeb5959b2eb5959b2), U64(0xc947478ec947478e), U64(0x0bf0f0fb0bf0f0fb), U64(0xecadad41ecadad41), U64(0x67d4d4b367d4d4b3), U64(0xfda2a25ffda2a25f), U64(0xeaafaf45eaafaf45), U64(0xbf9c9c23bf9c9c23), U64(0xf7a4a453f7a4a453), U64(0x967272e4967272e4), U64(0x5bc0c09b5bc0c09b), U64(0xc2b7b775c2b7b775), U64(0x1cfdfde11cfdfde1), U64(0xae93933dae93933d), U64(0x6a26264c6a26264c), U64(0x5a36366c5a36366c), U64(0x413f3f7e413f3f7e), U64(0x02f7f7f502f7f7f5), U64(0x4fcccc834fcccc83), U64(0x5c3434685c343468), U64(0xf4a5a551f4a5a551), U64(0x34e5e5d134e5e5d1), U64(0x08f1f1f908f1f1f9), U64(0x937171e2937171e2), U64(0x73d8d8ab73d8d8ab), U64(0x5331316253313162), U64(0x3f15152a3f15152a), U64(0x0c0404080c040408), U64(0x52c7c79552c7c795), U64(0x6523234665232346), U64(0x5ec3c39d5ec3c39d), U64(0x2818183028181830), U64(0xa1969637a1969637), U64(0x0f05050a0f05050a), U64(0xb59a9a2fb59a9a2f), U64(0x0907070e0907070e), U64(0x3612122436121224), U64(0x9b80801b9b80801b), U64(0x3de2e2df3de2e2df), U64(0x26ebebcd26ebebcd), U64(0x6927274e6927274e), U64(0xcdb2b27fcdb2b27f), U64(0x9f7575ea9f7575ea), U64(0x1b0909121b090912), U64(0x9e83831d9e83831d), U64(0x742c2c58742c2c58), U64(0x2e1a1a342e1a1a34), U64(0x2d1b1b362d1b1b36), U64(0xb26e6edcb26e6edc), U64(0xee5a5ab4ee5a5ab4), U64(0xfba0a05bfba0a05b), U64(0xf65252a4f65252a4), U64(0x4d3b3b764d3b3b76), U64(0x61d6d6b761d6d6b7), U64(0xceb3b37dceb3b37d), U64(0x7b2929527b292952), U64(0x3ee3e3dd3ee3e3dd), U64(0x712f2f5e712f2f5e), U64(0x9784841397848413), U64(0xf55353a6f55353a6), U64(0x68d1d1b968d1d1b9), U64(0x0000000000000000), U64(0x2cededc12cededc1), U64(0x6020204060202040), U64(0x1ffcfce31ffcfce3), U64(0xc8b1b179c8b1b179), U64(0xed5b5bb6ed5b5bb6), U64(0xbe6a6ad4be6a6ad4), U64(0x46cbcb8d46cbcb8d), U64(0xd9bebe67d9bebe67), U64(0x4b3939724b393972), U64(0xde4a4a94de4a4a94), U64(0xd44c4c98d44c4c98), U64(0xe85858b0e85858b0), U64(0x4acfcf854acfcf85), U64(0x6bd0d0bb6bd0d0bb), U64(0x2aefefc52aefefc5), U64(0xe5aaaa4fe5aaaa4f), U64(0x16fbfbed16fbfbed), U64(0xc5434386c5434386), U64(0xd74d4d9ad74d4d9a), U64(0x5533336655333366), U64(0x9485851194858511), U64(0xcf45458acf45458a), U64(0x10f9f9e910f9f9e9), U64(0x0602020406020204), U64(0x817f7ffe817f7ffe), U64(0xf05050a0f05050a0), U64(0x443c3c78443c3c78), U64(0xba9f9f25ba9f9f25), U64(0xe3a8a84be3a8a84b), U64(0xf35151a2f35151a2), U64(0xfea3a35dfea3a35d), U64(0xc0404080c0404080), U64(0x8a8f8f058a8f8f05), U64(0xad92923fad92923f), U64(0xbc9d9d21bc9d9d21), U64(0x4838387048383870), U64(0x04f5f5f104f5f5f1), U64(0xdfbcbc63dfbcbc63), U64(0xc1b6b677c1b6b677), U64(0x75dadaaf75dadaaf), U64(0x6321214263212142), U64(0x3010102030101020), U64(0x1affffe51affffe5), U64(0x0ef3f3fd0ef3f3fd), U64(0x6dd2d2bf6dd2d2bf), U64(0x4ccdcd814ccdcd81), U64(0x140c0c18140c0c18), U64(0x3513132635131326), U64(0x2fececc32fececc3), U64(0xe15f5fbee15f5fbe), U64(0xa2979735a2979735), U64(0xcc444488cc444488), U64(0x3917172e3917172e), U64(0x57c4c49357c4c493), U64(0xf2a7a755f2a7a755), U64(0x827e7efc827e7efc), U64(0x473d3d7a473d3d7a), U64(0xac6464c8ac6464c8), U64(0xe75d5dbae75d5dba), U64(0x2b1919322b191932), U64(0x957373e6957373e6), U64(0xa06060c0a06060c0), U64(0x9881811998818119), U64(0xd14f4f9ed14f4f9e), U64(0x7fdcdca37fdcdca3), U64(0x6622224466222244), U64(0x7e2a2a547e2a2a54), U64(0xab90903bab90903b), U64(0x8388880b8388880b), U64(0xca46468cca46468c), U64(0x29eeeec729eeeec7), U64(0xd3b8b86bd3b8b86b), U64(0x3c1414283c141428), U64(0x79dedea779dedea7), U64(0xe25e5ebce25e5ebc), U64(0x1d0b0b161d0b0b16), U64(0x76dbdbad76dbdbad), U64(0x3be0e0db3be0e0db), U64(0x5632326456323264), U64(0x4e3a3a744e3a3a74), U64(0x1e0a0a141e0a0a14), U64(0xdb494992db494992), U64(0x0a06060c0a06060c), U64(0x6c2424486c242448), U64(0xe45c5cb8e45c5cb8), U64(0x5dc2c29f5dc2c29f), U64(0x6ed3d3bd6ed3d3bd), U64(0xefacac43efacac43), U64(0xa66262c4a66262c4), U64(0xa8919139a8919139), U64(0xa4959531a4959531), U64(0x37e4e4d337e4e4d3), U64(0x8b7979f28b7979f2), U64(0x32e7e7d532e7e7d5), U64(0x43c8c88b43c8c88b), U64(0x5937376e5937376e), U64(0xb76d6ddab76d6dda), U64(0x8c8d8d018c8d8d01), U64(0x64d5d5b164d5d5b1), U64(0xd24e4e9cd24e4e9c), U64(0xe0a9a949e0a9a949), U64(0xb46c6cd8b46c6cd8), U64(0xfa5656acfa5656ac), U64(0x07f4f4f307f4f4f3), U64(0x25eaeacf25eaeacf), U64(0xaf6565caaf6565ca), U64(0x8e7a7af48e7a7af4), U64(0xe9aeae47e9aeae47), U64(0x1808081018080810), U64(0xd5baba6fd5baba6f), U64(0x887878f0887878f0), U64(0x6f25254a6f25254a), U64(0x722e2e5c722e2e5c), U64(0x241c1c38241c1c38), U64(0xf1a6a657f1a6a657), U64(0xc7b4b473c7b4b473), U64(0x51c6c69751c6c697), U64(0x23e8e8cb23e8e8cb), U64(0x7cdddda17cdddda1), U64(0x9c7474e89c7474e8), U64(0x211f1f3e211f1f3e), U64(0xdd4b4b96dd4b4b96), U64(0xdcbdbd61dcbdbd61), U64(0x868b8b0d868b8b0d), U64(0x858a8a0f858a8a0f), U64(0x907070e0907070e0), U64(0x423e3e7c423e3e7c), U64(0xc4b5b571c4b5b571), U64(0xaa6666ccaa6666cc), U64(0xd8484890d8484890), U64(0x0503030605030306), U64(0x01f6f6f701f6f6f7), U64(0x120e0e1c120e0e1c), U64(0xa36161c2a36161c2), U64(0x5f35356a5f35356a), U64(0xf95757aef95757ae), U64(0xd0b9b969d0b9b969), U64(0x9186861791868617), U64(0x58c1c19958c1c199), U64(0x271d1d3a271d1d3a), U64(0xb99e9e27b99e9e27), U64(0x38e1e1d938e1e1d9), U64(0x13f8f8eb13f8f8eb), U64(0xb398982bb398982b), U64(0x3311112233111122), U64(0xbb6969d2bb6969d2), U64(0x70d9d9a970d9d9a9), U64(0x898e8e07898e8e07), U64(0xa7949433a7949433), U64(0xb69b9b2db69b9b2d), U64(0x221e1e3c221e1e3c), U64(0x9287871592878715), U64(0x20e9e9c920e9e9c9), U64(0x49cece8749cece87), U64(0xff5555aaff5555aa), U64(0x7828285078282850), U64(0x7adfdfa57adfdfa5), U64(0x8f8c8c038f8c8c03), U64(0xf8a1a159f8a1a159), U64(0x8089890980898909), U64(0x170d0d1a170d0d1a), U64(0xdabfbf65dabfbf65), U64(0x31e6e6d731e6e6d7), U64(0xc6424284c6424284), U64(0xb86868d0b86868d0), U64(0xc3414182c3414182), U64(0xb0999929b0999929), U64(0x772d2d5a772d2d5a), U64(0x110f0f1e110f0f1e), U64(0xcbb0b07bcbb0b07b), U64(0xfc5454a8fc5454a8), U64(0xd6bbbb6dd6bbbb6d), U64(0x3a16162c3a16162c) }; static const u8 Te4[256] = { 0x63U, 0x7cU, 0x77U, 0x7bU, 0xf2U, 0x6bU, 0x6fU, 0xc5U, 0x30U, 0x01U, 0x67U, 0x2bU, 0xfeU, 0xd7U, 0xabU, 0x76U, 0xcaU, 0x82U, 0xc9U, 0x7dU, 0xfaU, 0x59U, 0x47U, 0xf0U, 0xadU, 0xd4U, 0xa2U, 0xafU, 0x9cU, 0xa4U, 0x72U, 0xc0U, 0xb7U, 0xfdU, 0x93U, 0x26U, 0x36U, 0x3fU, 0xf7U, 0xccU, 0x34U, 0xa5U, 0xe5U, 0xf1U, 0x71U, 0xd8U, 0x31U, 0x15U, 0x04U, 0xc7U, 0x23U, 0xc3U, 0x18U, 0x96U, 0x05U, 0x9aU, 0x07U, 0x12U, 0x80U, 0xe2U, 0xebU, 0x27U, 0xb2U, 0x75U, 0x09U, 0x83U, 0x2cU, 0x1aU, 0x1bU, 0x6eU, 0x5aU, 0xa0U, 0x52U, 0x3bU, 0xd6U, 0xb3U, 0x29U, 0xe3U, 0x2fU, 0x84U, 0x53U, 0xd1U, 0x00U, 0xedU, 0x20U, 0xfcU, 0xb1U, 0x5bU, 0x6aU, 0xcbU, 0xbeU, 0x39U, 0x4aU, 0x4cU, 0x58U, 0xcfU, 0xd0U, 0xefU, 0xaaU, 0xfbU, 0x43U, 0x4dU, 0x33U, 0x85U, 0x45U, 0xf9U, 0x02U, 0x7fU, 0x50U, 0x3cU, 0x9fU, 0xa8U, 0x51U, 0xa3U, 0x40U, 0x8fU, 0x92U, 0x9dU, 0x38U, 0xf5U, 0xbcU, 0xb6U, 0xdaU, 0x21U, 0x10U, 0xffU, 0xf3U, 0xd2U, 0xcdU, 0x0cU, 0x13U, 0xecU, 0x5fU, 0x97U, 0x44U, 0x17U, 0xc4U, 0xa7U, 0x7eU, 0x3dU, 0x64U, 0x5dU, 0x19U, 0x73U, 0x60U, 0x81U, 0x4fU, 0xdcU, 0x22U, 0x2aU, 0x90U, 0x88U, 0x46U, 0xeeU, 0xb8U, 0x14U, 0xdeU, 0x5eU, 0x0bU, 0xdbU, 0xe0U, 0x32U, 0x3aU, 0x0aU, 0x49U, 0x06U, 0x24U, 0x5cU, 0xc2U, 0xd3U, 0xacU, 0x62U, 0x91U, 0x95U, 0xe4U, 0x79U, 0xe7U, 0xc8U, 0x37U, 0x6dU, 0x8dU, 0xd5U, 0x4eU, 0xa9U, 0x6cU, 0x56U, 0xf4U, 0xeaU, 0x65U, 0x7aU, 0xaeU, 0x08U, 0xbaU, 0x78U, 0x25U, 0x2eU, 0x1cU, 0xa6U, 0xb4U, 0xc6U, 0xe8U, 0xddU, 0x74U, 0x1fU, 0x4bU, 0xbdU, 0x8bU, 0x8aU, 0x70U, 0x3eU, 0xb5U, 0x66U, 0x48U, 0x03U, 0xf6U, 0x0eU, 0x61U, 0x35U, 0x57U, 0xb9U, 0x86U, 0xc1U, 0x1dU, 0x9eU, 0xe1U, 0xf8U, 0x98U, 0x11U, 0x69U, 0xd9U, 0x8eU, 0x94U, 0x9bU, 0x1eU, 0x87U, 0xe9U, 0xceU, 0x55U, 0x28U, 0xdfU, 0x8cU, 0xa1U, 0x89U, 0x0dU, 0xbfU, 0xe6U, 0x42U, 0x68U, 0x41U, 0x99U, 0x2dU, 0x0fU, 0xb0U, 0x54U, 0xbbU, 0x16U }; static const u64 Td[256] = { U64(0x50a7f45150a7f451), U64(0x5365417e5365417e), U64(0xc3a4171ac3a4171a), U64(0x965e273a965e273a), U64(0xcb6bab3bcb6bab3b), U64(0xf1459d1ff1459d1f), U64(0xab58faacab58faac), U64(0x9303e34b9303e34b), U64(0x55fa302055fa3020), U64(0xf66d76adf66d76ad), U64(0x9176cc889176cc88), U64(0x254c02f5254c02f5), U64(0xfcd7e54ffcd7e54f), U64(0xd7cb2ac5d7cb2ac5), U64(0x8044352680443526), U64(0x8fa362b58fa362b5), U64(0x495ab1de495ab1de), U64(0x671bba25671bba25), U64(0x980eea45980eea45), U64(0xe1c0fe5de1c0fe5d), U64(0x02752fc302752fc3), U64(0x12f04c8112f04c81), U64(0xa397468da397468d), U64(0xc6f9d36bc6f9d36b), U64(0xe75f8f03e75f8f03), U64(0x959c9215959c9215), U64(0xeb7a6dbfeb7a6dbf), U64(0xda595295da595295), U64(0x2d83bed42d83bed4), U64(0xd3217458d3217458), U64(0x2969e0492969e049), U64(0x44c8c98e44c8c98e), U64(0x6a89c2756a89c275), U64(0x78798ef478798ef4), U64(0x6b3e58996b3e5899), U64(0xdd71b927dd71b927), U64(0xb64fe1beb64fe1be), U64(0x17ad88f017ad88f0), U64(0x66ac20c966ac20c9), U64(0xb43ace7db43ace7d), U64(0x184adf63184adf63), U64(0x82311ae582311ae5), U64(0x6033519760335197), U64(0x457f5362457f5362), U64(0xe07764b1e07764b1), U64(0x84ae6bbb84ae6bbb), U64(0x1ca081fe1ca081fe), U64(0x942b08f9942b08f9), U64(0x5868487058684870), U64(0x19fd458f19fd458f), U64(0x876cde94876cde94), U64(0xb7f87b52b7f87b52), U64(0x23d373ab23d373ab), U64(0xe2024b72e2024b72), U64(0x578f1fe3578f1fe3), U64(0x2aab55662aab5566), U64(0x0728ebb20728ebb2), U64(0x03c2b52f03c2b52f), U64(0x9a7bc5869a7bc586), U64(0xa50837d3a50837d3), U64(0xf2872830f2872830), U64(0xb2a5bf23b2a5bf23), U64(0xba6a0302ba6a0302), U64(0x5c8216ed5c8216ed), U64(0x2b1ccf8a2b1ccf8a), U64(0x92b479a792b479a7), U64(0xf0f207f3f0f207f3), U64(0xa1e2694ea1e2694e), U64(0xcdf4da65cdf4da65), U64(0xd5be0506d5be0506), U64(0x1f6234d11f6234d1), U64(0x8afea6c48afea6c4), U64(0x9d532e349d532e34), U64(0xa055f3a2a055f3a2), U64(0x32e18a0532e18a05), U64(0x75ebf6a475ebf6a4), U64(0x39ec830b39ec830b), U64(0xaaef6040aaef6040), U64(0x069f715e069f715e), U64(0x51106ebd51106ebd), U64(0xf98a213ef98a213e), U64(0x3d06dd963d06dd96), U64(0xae053eddae053edd), U64(0x46bde64d46bde64d), U64(0xb58d5491b58d5491), U64(0x055dc471055dc471), U64(0x6fd406046fd40604), U64(0xff155060ff155060), U64(0x24fb981924fb9819), U64(0x97e9bdd697e9bdd6), U64(0xcc434089cc434089), U64(0x779ed967779ed967), U64(0xbd42e8b0bd42e8b0), U64(0x888b8907888b8907), U64(0x385b19e7385b19e7), U64(0xdbeec879dbeec879), U64(0x470a7ca1470a7ca1), U64(0xe90f427ce90f427c), U64(0xc91e84f8c91e84f8), U64(0x0000000000000000), U64(0x8386800983868009), U64(0x48ed2b3248ed2b32), U64(0xac70111eac70111e), U64(0x4e725a6c4e725a6c), U64(0xfbff0efdfbff0efd), U64(0x5638850f5638850f), U64(0x1ed5ae3d1ed5ae3d), U64(0x27392d3627392d36), U64(0x64d90f0a64d90f0a), U64(0x21a65c6821a65c68), U64(0xd1545b9bd1545b9b), U64(0x3a2e36243a2e3624), U64(0xb1670a0cb1670a0c), U64(0x0fe757930fe75793), U64(0xd296eeb4d296eeb4), U64(0x9e919b1b9e919b1b), U64(0x4fc5c0804fc5c080), U64(0xa220dc61a220dc61), U64(0x694b775a694b775a), U64(0x161a121c161a121c), U64(0x0aba93e20aba93e2), U64(0xe52aa0c0e52aa0c0), U64(0x43e0223c43e0223c), U64(0x1d171b121d171b12), U64(0x0b0d090e0b0d090e), U64(0xadc78bf2adc78bf2), U64(0xb9a8b62db9a8b62d), U64(0xc8a91e14c8a91e14), U64(0x8519f1578519f157), U64(0x4c0775af4c0775af), U64(0xbbdd99eebbdd99ee), U64(0xfd607fa3fd607fa3), U64(0x9f2601f79f2601f7), U64(0xbcf5725cbcf5725c), U64(0xc53b6644c53b6644), U64(0x347efb5b347efb5b), U64(0x7629438b7629438b), U64(0xdcc623cbdcc623cb), U64(0x68fcedb668fcedb6), U64(0x63f1e4b863f1e4b8), U64(0xcadc31d7cadc31d7), U64(0x1085634210856342), U64(0x4022971340229713), U64(0x2011c6842011c684), U64(0x7d244a857d244a85), U64(0xf83dbbd2f83dbbd2), U64(0x1132f9ae1132f9ae), U64(0x6da129c76da129c7), U64(0x4b2f9e1d4b2f9e1d), U64(0xf330b2dcf330b2dc), U64(0xec52860dec52860d), U64(0xd0e3c177d0e3c177), U64(0x6c16b32b6c16b32b), U64(0x99b970a999b970a9), U64(0xfa489411fa489411), U64(0x2264e9472264e947), U64(0xc48cfca8c48cfca8), U64(0x1a3ff0a01a3ff0a0), U64(0xd82c7d56d82c7d56), U64(0xef903322ef903322), U64(0xc74e4987c74e4987), U64(0xc1d138d9c1d138d9), U64(0xfea2ca8cfea2ca8c), U64(0x360bd498360bd498), U64(0xcf81f5a6cf81f5a6), U64(0x28de7aa528de7aa5), U64(0x268eb7da268eb7da), U64(0xa4bfad3fa4bfad3f), U64(0xe49d3a2ce49d3a2c), U64(0x0d9278500d927850), U64(0x9bcc5f6a9bcc5f6a), U64(0x62467e5462467e54), U64(0xc2138df6c2138df6), U64(0xe8b8d890e8b8d890), U64(0x5ef7392e5ef7392e), U64(0xf5afc382f5afc382), U64(0xbe805d9fbe805d9f), U64(0x7c93d0697c93d069), U64(0xa92dd56fa92dd56f), U64(0xb31225cfb31225cf), U64(0x3b99acc83b99acc8), U64(0xa77d1810a77d1810), U64(0x6e639ce86e639ce8), U64(0x7bbb3bdb7bbb3bdb), U64(0x097826cd097826cd), U64(0xf418596ef418596e), U64(0x01b79aec01b79aec), U64(0xa89a4f83a89a4f83), U64(0x656e95e6656e95e6), U64(0x7ee6ffaa7ee6ffaa), U64(0x08cfbc2108cfbc21), U64(0xe6e815efe6e815ef), U64(0xd99be7bad99be7ba), U64(0xce366f4ace366f4a), U64(0xd4099fead4099fea), U64(0xd67cb029d67cb029), U64(0xafb2a431afb2a431), U64(0x31233f2a31233f2a), U64(0x3094a5c63094a5c6), U64(0xc066a235c066a235), U64(0x37bc4e7437bc4e74), U64(0xa6ca82fca6ca82fc), U64(0xb0d090e0b0d090e0), U64(0x15d8a73315d8a733), U64(0x4a9804f14a9804f1), U64(0xf7daec41f7daec41), U64(0x0e50cd7f0e50cd7f), U64(0x2ff691172ff69117), U64(0x8dd64d768dd64d76), U64(0x4db0ef434db0ef43), U64(0x544daacc544daacc), U64(0xdf0496e4df0496e4), U64(0xe3b5d19ee3b5d19e), U64(0x1b886a4c1b886a4c), U64(0xb81f2cc1b81f2cc1), U64(0x7f5165467f516546), U64(0x04ea5e9d04ea5e9d), U64(0x5d358c015d358c01), U64(0x737487fa737487fa), U64(0x2e410bfb2e410bfb), U64(0x5a1d67b35a1d67b3), U64(0x52d2db9252d2db92), U64(0x335610e9335610e9), U64(0x1347d66d1347d66d), U64(0x8c61d79a8c61d79a), U64(0x7a0ca1377a0ca137), U64(0x8e14f8598e14f859), U64(0x893c13eb893c13eb), U64(0xee27a9ceee27a9ce), U64(0x35c961b735c961b7), U64(0xede51ce1ede51ce1), U64(0x3cb1477a3cb1477a), U64(0x59dfd29c59dfd29c), U64(0x3f73f2553f73f255), U64(0x79ce141879ce1418), U64(0xbf37c773bf37c773), U64(0xeacdf753eacdf753), U64(0x5baafd5f5baafd5f), U64(0x146f3ddf146f3ddf), U64(0x86db447886db4478), U64(0x81f3afca81f3afca), U64(0x3ec468b93ec468b9), U64(0x2c3424382c342438), U64(0x5f40a3c25f40a3c2), U64(0x72c31d1672c31d16), U64(0x0c25e2bc0c25e2bc), U64(0x8b493c288b493c28), U64(0x41950dff41950dff), U64(0x7101a8397101a839), U64(0xdeb30c08deb30c08), U64(0x9ce4b4d89ce4b4d8), U64(0x90c1566490c15664), U64(0x6184cb7b6184cb7b), U64(0x70b632d570b632d5), U64(0x745c6c48745c6c48), U64(0x4257b8d04257b8d0) }; static const u8 Td4[256] = { 0x52U, 0x09U, 0x6aU, 0xd5U, 0x30U, 0x36U, 0xa5U, 0x38U, 0xbfU, 0x40U, 0xa3U, 0x9eU, 0x81U, 0xf3U, 0xd7U, 0xfbU, 0x7cU, 0xe3U, 0x39U, 0x82U, 0x9bU, 0x2fU, 0xffU, 0x87U, 0x34U, 0x8eU, 0x43U, 0x44U, 0xc4U, 0xdeU, 0xe9U, 0xcbU, 0x54U, 0x7bU, 0x94U, 0x32U, 0xa6U, 0xc2U, 0x23U, 0x3dU, 0xeeU, 0x4cU, 0x95U, 0x0bU, 0x42U, 0xfaU, 0xc3U, 0x4eU, 0x08U, 0x2eU, 0xa1U, 0x66U, 0x28U, 0xd9U, 0x24U, 0xb2U, 0x76U, 0x5bU, 0xa2U, 0x49U, 0x6dU, 0x8bU, 0xd1U, 0x25U, 0x72U, 0xf8U, 0xf6U, 0x64U, 0x86U, 0x68U, 0x98U, 0x16U, 0xd4U, 0xa4U, 0x5cU, 0xccU, 0x5dU, 0x65U, 0xb6U, 0x92U, 0x6cU, 0x70U, 0x48U, 0x50U, 0xfdU, 0xedU, 0xb9U, 0xdaU, 0x5eU, 0x15U, 0x46U, 0x57U, 0xa7U, 0x8dU, 0x9dU, 0x84U, 0x90U, 0xd8U, 0xabU, 0x00U, 0x8cU, 0xbcU, 0xd3U, 0x0aU, 0xf7U, 0xe4U, 0x58U, 0x05U, 0xb8U, 0xb3U, 0x45U, 0x06U, 0xd0U, 0x2cU, 0x1eU, 0x8fU, 0xcaU, 0x3fU, 0x0fU, 0x02U, 0xc1U, 0xafU, 0xbdU, 0x03U, 0x01U, 0x13U, 0x8aU, 0x6bU, 0x3aU, 0x91U, 0x11U, 0x41U, 0x4fU, 0x67U, 0xdcU, 0xeaU, 0x97U, 0xf2U, 0xcfU, 0xceU, 0xf0U, 0xb4U, 0xe6U, 0x73U, 0x96U, 0xacU, 0x74U, 0x22U, 0xe7U, 0xadU, 0x35U, 0x85U, 0xe2U, 0xf9U, 0x37U, 0xe8U, 0x1cU, 0x75U, 0xdfU, 0x6eU, 0x47U, 0xf1U, 0x1aU, 0x71U, 0x1dU, 0x29U, 0xc5U, 0x89U, 0x6fU, 0xb7U, 0x62U, 0x0eU, 0xaaU, 0x18U, 0xbeU, 0x1bU, 0xfcU, 0x56U, 0x3eU, 0x4bU, 0xc6U, 0xd2U, 0x79U, 0x20U, 0x9aU, 0xdbU, 0xc0U, 0xfeU, 0x78U, 0xcdU, 0x5aU, 0xf4U, 0x1fU, 0xddU, 0xa8U, 0x33U, 0x88U, 0x07U, 0xc7U, 0x31U, 0xb1U, 0x12U, 0x10U, 0x59U, 0x27U, 0x80U, 0xecU, 0x5fU, 0x60U, 0x51U, 0x7fU, 0xa9U, 0x19U, 0xb5U, 0x4aU, 0x0dU, 0x2dU, 0xe5U, 0x7aU, 0x9fU, 0x93U, 0xc9U, 0x9cU, 0xefU, 0xa0U, 0xe0U, 0x3bU, 0x4dU, 0xaeU, 0x2aU, 0xf5U, 0xb0U, 0xc8U, 0xebU, 0xbbU, 0x3cU, 0x83U, 0x53U, 0x99U, 0x61U, 0x17U, 0x2bU, 0x04U, 0x7eU, 0xbaU, 0x77U, 0xd6U, 0x26U, 0xe1U, 0x69U, 0x14U, 0x63U, 0x55U, 0x21U, 0x0cU, 0x7dU }; static const u32 rcon[] = { 0x00000001U, 0x00000002U, 0x00000004U, 0x00000008U, 0x00000010U, 0x00000020U, 0x00000040U, 0x00000080U, 0x0000001bU, 0x00000036U, }; int AES_set_encrypt_key(const unsigned char *userKey, const int bits, AES_KEY *key) { u32 *rk; int i = 0; u32 temp; if (!userKey || !key) return -1; if (bits != 128 && bits != 192 && bits != 256) return -2; rk = key->rd_key; if (bits==128) key->rounds = 10; else if (bits==192) key->rounds = 12; else key->rounds = 14; rk[0] = GETU32(userKey ); rk[1] = GETU32(userKey + 4); rk[2] = GETU32(userKey + 8); rk[3] = GETU32(userKey + 12); if (bits == 128) { while (1) { temp = rk[3]; rk[4] = rk[0] ^ ((u32)Te4[(temp >> 8) & 0xff] ) ^ ((u32)Te4[(temp >> 16) & 0xff] << 8) ^ ((u32)Te4[(temp >> 24) ] << 16) ^ ((u32)Te4[(temp ) & 0xff] << 24) ^ rcon[i]; rk[5] = rk[1] ^ rk[4]; rk[6] = rk[2] ^ rk[5]; rk[7] = rk[3] ^ rk[6]; if (++i == 10) { return 0; } rk += 4; } } rk[4] = GETU32(userKey + 16); rk[5] = GETU32(userKey + 20); if (bits == 192) { while (1) { temp = rk[ 5]; rk[ 6] = rk[ 0] ^ ((u32)Te4[(temp >> 8) & 0xff] ) ^ ((u32)Te4[(temp >> 16) & 0xff] << 8) ^ ((u32)Te4[(temp >> 24) ] << 16) ^ ((u32)Te4[(temp ) & 0xff] << 24) ^ rcon[i]; rk[ 7] = rk[ 1] ^ rk[ 6]; rk[ 8] = rk[ 2] ^ rk[ 7]; rk[ 9] = rk[ 3] ^ rk[ 8]; if (++i == 8) { return 0; } rk[10] = rk[ 4] ^ rk[ 9]; rk[11] = rk[ 5] ^ rk[10]; rk += 6; } } rk[6] = GETU32(userKey + 24); rk[7] = GETU32(userKey + 28); if (bits == 256) { while (1) { temp = rk[ 7]; rk[ 8] = rk[ 0] ^ ((u32)Te4[(temp >> 8) & 0xff] ) ^ ((u32)Te4[(temp >> 16) & 0xff] << 8) ^ ((u32)Te4[(temp >> 24) ] << 16) ^ ((u32)Te4[(temp ) & 0xff] << 24) ^ rcon[i]; rk[ 9] = rk[ 1] ^ rk[ 8]; rk[10] = rk[ 2] ^ rk[ 9]; rk[11] = rk[ 3] ^ rk[10]; if (++i == 7) { return 0; } temp = rk[11]; rk[12] = rk[ 4] ^ ((u32)Te4[(temp ) & 0xff] ) ^ ((u32)Te4[(temp >> 8) & 0xff] << 8) ^ ((u32)Te4[(temp >> 16) & 0xff] << 16) ^ ((u32)Te4[(temp >> 24) ] << 24); rk[13] = rk[ 5] ^ rk[12]; rk[14] = rk[ 6] ^ rk[13]; rk[15] = rk[ 7] ^ rk[14]; rk += 8; } } return 0; } int AES_set_decrypt_key(const unsigned char *userKey, const int bits, AES_KEY *key) { u32 *rk; int i, j, status; u32 temp; status = AES_set_encrypt_key(userKey, bits, key); if (status < 0) return status; rk = key->rd_key; for (i = 0, j = 4*(key->rounds); i < j; i += 4, j -= 4) { temp = rk[i ]; rk[i ] = rk[j ]; rk[j ] = temp; temp = rk[i + 1]; rk[i + 1] = rk[j + 1]; rk[j + 1] = temp; temp = rk[i + 2]; rk[i + 2] = rk[j + 2]; rk[j + 2] = temp; temp = rk[i + 3]; rk[i + 3] = rk[j + 3]; rk[j + 3] = temp; } for (i = 1; i < (key->rounds); i++) { rk += 4; #if 1 for (j = 0; j < 4; j++) { u32 tp1, tp2, tp4, tp8, tp9, tpb, tpd, tpe, m; tp1 = rk[j]; m = tp1 & 0x80808080; tp2 = ((tp1 & 0x7f7f7f7f) << 1) ^ ((m - (m >> 7)) & 0x1b1b1b1b); m = tp2 & 0x80808080; tp4 = ((tp2 & 0x7f7f7f7f) << 1) ^ ((m - (m >> 7)) & 0x1b1b1b1b); m = tp4 & 0x80808080; tp8 = ((tp4 & 0x7f7f7f7f) << 1) ^ ((m - (m >> 7)) & 0x1b1b1b1b); tp9 = tp8 ^ tp1; tpb = tp9 ^ tp2; tpd = tp9 ^ tp4; tpe = tp8 ^ tp4 ^ tp2; #if defined(ROTATE) rk[j] = tpe ^ ROTATE(tpd,16) ^ ROTATE(tp9,8) ^ ROTATE(tpb,24); #else rk[j] = tpe ^ (tpd >> 16) ^ (tpd << 16) ^ (tp9 >> 24) ^ (tp9 << 8) ^ (tpb >> 8) ^ (tpb << 24); #endif } #else rk[0] = Td0[Te2[(rk[0] ) & 0xff] & 0xff] ^ Td1[Te2[(rk[0] >> 8) & 0xff] & 0xff] ^ Td2[Te2[(rk[0] >> 16) & 0xff] & 0xff] ^ Td3[Te2[(rk[0] >> 24) ] & 0xff]; rk[1] = Td0[Te2[(rk[1] ) & 0xff] & 0xff] ^ Td1[Te2[(rk[1] >> 8) & 0xff] & 0xff] ^ Td2[Te2[(rk[1] >> 16) & 0xff] & 0xff] ^ Td3[Te2[(rk[1] >> 24) ] & 0xff]; rk[2] = Td0[Te2[(rk[2] ) & 0xff] & 0xff] ^ Td1[Te2[(rk[2] >> 8) & 0xff] & 0xff] ^ Td2[Te2[(rk[2] >> 16) & 0xff] & 0xff] ^ Td3[Te2[(rk[2] >> 24) ] & 0xff]; rk[3] = Td0[Te2[(rk[3] ) & 0xff] & 0xff] ^ Td1[Te2[(rk[3] >> 8) & 0xff] & 0xff] ^ Td2[Te2[(rk[3] >> 16) & 0xff] & 0xff] ^ Td3[Te2[(rk[3] >> 24) ] & 0xff]; #endif } return 0; } void AES_encrypt(const unsigned char *in, unsigned char *out, const AES_KEY *key) { const u32 *rk; u32 s0, s1, s2, s3, t[4]; int r; assert(in && out && key); rk = key->rd_key; s0 = GETU32(in ) ^ rk[0]; s1 = GETU32(in + 4) ^ rk[1]; s2 = GETU32(in + 8) ^ rk[2]; s3 = GETU32(in + 12) ^ rk[3]; #if defined(AES_COMPACT_IN_OUTER_ROUNDS) prefetch256(Te4); t[0] = (u32)Te4[(s0 ) & 0xff] ^ (u32)Te4[(s1 >> 8) & 0xff] << 8 ^ (u32)Te4[(s2 >> 16) & 0xff] << 16 ^ (u32)Te4[(s3 >> 24) ] << 24; t[1] = (u32)Te4[(s1 ) & 0xff] ^ (u32)Te4[(s2 >> 8) & 0xff] << 8 ^ (u32)Te4[(s3 >> 16) & 0xff] << 16 ^ (u32)Te4[(s0 >> 24) ] << 24; t[2] = (u32)Te4[(s2 ) & 0xff] ^ (u32)Te4[(s3 >> 8) & 0xff] << 8 ^ (u32)Te4[(s0 >> 16) & 0xff] << 16 ^ (u32)Te4[(s1 >> 24) ] << 24; t[3] = (u32)Te4[(s3 ) & 0xff] ^ (u32)Te4[(s0 >> 8) & 0xff] << 8 ^ (u32)Te4[(s1 >> 16) & 0xff] << 16 ^ (u32)Te4[(s2 >> 24) ] << 24; { int i; u32 r0, r1, r2; for (i = 0; i < 4; i++) { r0 = t[i]; r1 = r0 & 0x80808080; r2 = ((r0 & 0x7f7f7f7f) << 1) ^ ((r1 - (r1 >> 7)) & 0x1b1b1b1b); #if defined(ROTATE) t[i] = r2 ^ ROTATE(r2,24) ^ ROTATE(r0,24) ^ ROTATE(r0,16) ^ ROTATE(r0,8); #else t[i] = r2 ^ ((r2 ^ r0) << 24) ^ ((r2 ^ r0) >> 8) ^ (r0 << 16) ^ (r0 >> 16) ^ (r0 << 8) ^ (r0 >> 24); #endif t[i] ^= rk[4+i]; } } #else t[0] = Te0[(s0 ) & 0xff] ^ Te1[(s1 >> 8) & 0xff] ^ Te2[(s2 >> 16) & 0xff] ^ Te3[(s3 >> 24) ] ^ rk[4]; t[1] = Te0[(s1 ) & 0xff] ^ Te1[(s2 >> 8) & 0xff] ^ Te2[(s3 >> 16) & 0xff] ^ Te3[(s0 >> 24) ] ^ rk[5]; t[2] = Te0[(s2 ) & 0xff] ^ Te1[(s3 >> 8) & 0xff] ^ Te2[(s0 >> 16) & 0xff] ^ Te3[(s1 >> 24) ] ^ rk[6]; t[3] = Te0[(s3 ) & 0xff] ^ Te1[(s0 >> 8) & 0xff] ^ Te2[(s1 >> 16) & 0xff] ^ Te3[(s2 >> 24) ] ^ rk[7]; #endif s0 = t[0]; s1 = t[1]; s2 = t[2]; s3 = t[3]; for (rk+=8,r=key->rounds-2; r>0; rk+=4,r--) { #if defined(AES_COMPACT_IN_INNER_ROUNDS) t[0] = (u32)Te4[(s0 ) & 0xff] ^ (u32)Te4[(s1 >> 8) & 0xff] << 8 ^ (u32)Te4[(s2 >> 16) & 0xff] << 16 ^ (u32)Te4[(s3 >> 24) ] << 24; t[1] = (u32)Te4[(s1 ) & 0xff] ^ (u32)Te4[(s2 >> 8) & 0xff] << 8 ^ (u32)Te4[(s3 >> 16) & 0xff] << 16 ^ (u32)Te4[(s0 >> 24) ] << 24; t[2] = (u32)Te4[(s2 ) & 0xff] ^ (u32)Te4[(s3 >> 8) & 0xff] << 8 ^ (u32)Te4[(s0 >> 16) & 0xff] << 16 ^ (u32)Te4[(s1 >> 24) ] << 24; t[3] = (u32)Te4[(s3 ) & 0xff] ^ (u32)Te4[(s0 >> 8) & 0xff] << 8 ^ (u32)Te4[(s1 >> 16) & 0xff] << 16 ^ (u32)Te4[(s2 >> 24) ] << 24; { int i; u32 r0, r1, r2; for (i = 0; i < 4; i++) { r0 = t[i]; r1 = r0 & 0x80808080; r2 = ((r0 & 0x7f7f7f7f) << 1) ^ ((r1 - (r1 >> 7)) & 0x1b1b1b1b); #if defined(ROTATE) t[i] = r2 ^ ROTATE(r2,24) ^ ROTATE(r0,24) ^ ROTATE(r0,16) ^ ROTATE(r0,8); #else t[i] = r2 ^ ((r2 ^ r0) << 24) ^ ((r2 ^ r0) >> 8) ^ (r0 << 16) ^ (r0 >> 16) ^ (r0 << 8) ^ (r0 >> 24); #endif t[i] ^= rk[i]; } } #else t[0] = Te0[(s0 ) & 0xff] ^ Te1[(s1 >> 8) & 0xff] ^ Te2[(s2 >> 16) & 0xff] ^ Te3[(s3 >> 24) ] ^ rk[0]; t[1] = Te0[(s1 ) & 0xff] ^ Te1[(s2 >> 8) & 0xff] ^ Te2[(s3 >> 16) & 0xff] ^ Te3[(s0 >> 24) ] ^ rk[1]; t[2] = Te0[(s2 ) & 0xff] ^ Te1[(s3 >> 8) & 0xff] ^ Te2[(s0 >> 16) & 0xff] ^ Te3[(s1 >> 24) ] ^ rk[2]; t[3] = Te0[(s3 ) & 0xff] ^ Te1[(s0 >> 8) & 0xff] ^ Te2[(s1 >> 16) & 0xff] ^ Te3[(s2 >> 24) ] ^ rk[3]; #endif s0 = t[0]; s1 = t[1]; s2 = t[2]; s3 = t[3]; } #if defined(AES_COMPACT_IN_OUTER_ROUNDS) prefetch256(Te4); *(u32*)(out+0) = (u32)Te4[(s0 ) & 0xff] ^ (u32)Te4[(s1 >> 8) & 0xff] << 8 ^ (u32)Te4[(s2 >> 16) & 0xff] << 16 ^ (u32)Te4[(s3 >> 24) ] << 24 ^ rk[0]; *(u32*)(out+4) = (u32)Te4[(s1 ) & 0xff] ^ (u32)Te4[(s2 >> 8) & 0xff] << 8 ^ (u32)Te4[(s3 >> 16) & 0xff] << 16 ^ (u32)Te4[(s0 >> 24) ] << 24 ^ rk[1]; *(u32*)(out+8) = (u32)Te4[(s2 ) & 0xff] ^ (u32)Te4[(s3 >> 8) & 0xff] << 8 ^ (u32)Te4[(s0 >> 16) & 0xff] << 16 ^ (u32)Te4[(s1 >> 24) ] << 24 ^ rk[2]; *(u32*)(out+12) = (u32)Te4[(s3 ) & 0xff] ^ (u32)Te4[(s0 >> 8) & 0xff] << 8 ^ (u32)Te4[(s1 >> 16) & 0xff] << 16 ^ (u32)Te4[(s2 >> 24) ] << 24 ^ rk[3]; #else *(u32*)(out+0) = (Te2[(s0 ) & 0xff] & 0x000000ffU) ^ (Te3[(s1 >> 8) & 0xff] & 0x0000ff00U) ^ (Te0[(s2 >> 16) & 0xff] & 0x00ff0000U) ^ (Te1[(s3 >> 24) ] & 0xff000000U) ^ rk[0]; *(u32*)(out+4) = (Te2[(s1 ) & 0xff] & 0x000000ffU) ^ (Te3[(s2 >> 8) & 0xff] & 0x0000ff00U) ^ (Te0[(s3 >> 16) & 0xff] & 0x00ff0000U) ^ (Te1[(s0 >> 24) ] & 0xff000000U) ^ rk[1]; *(u32*)(out+8) = (Te2[(s2 ) & 0xff] & 0x000000ffU) ^ (Te3[(s3 >> 8) & 0xff] & 0x0000ff00U) ^ (Te0[(s0 >> 16) & 0xff] & 0x00ff0000U) ^ (Te1[(s1 >> 24) ] & 0xff000000U) ^ rk[2]; *(u32*)(out+12) = (Te2[(s3 ) & 0xff] & 0x000000ffU) ^ (Te3[(s0 >> 8) & 0xff] & 0x0000ff00U) ^ (Te0[(s1 >> 16) & 0xff] & 0x00ff0000U) ^ (Te1[(s2 >> 24) ] & 0xff000000U) ^ rk[3]; #endif } void AES_decrypt(const unsigned char *in, unsigned char *out, const AES_KEY *key) { const u32 *rk; u32 s0, s1, s2, s3, t[4]; int r; assert(in && out && key); rk = key->rd_key; s0 = GETU32(in ) ^ rk[0]; s1 = GETU32(in + 4) ^ rk[1]; s2 = GETU32(in + 8) ^ rk[2]; s3 = GETU32(in + 12) ^ rk[3]; #if defined(AES_COMPACT_IN_OUTER_ROUNDS) prefetch256(Td4); t[0] = (u32)Td4[(s0 ) & 0xff] ^ (u32)Td4[(s3 >> 8) & 0xff] << 8 ^ (u32)Td4[(s2 >> 16) & 0xff] << 16 ^ (u32)Td4[(s1 >> 24) ] << 24; t[1] = (u32)Td4[(s1 ) & 0xff] ^ (u32)Td4[(s0 >> 8) & 0xff] << 8 ^ (u32)Td4[(s3 >> 16) & 0xff] << 16 ^ (u32)Td4[(s2 >> 24) ] << 24; t[2] = (u32)Td4[(s2 ) & 0xff] ^ (u32)Td4[(s1 >> 8) & 0xff] << 8 ^ (u32)Td4[(s0 >> 16) & 0xff] << 16 ^ (u32)Td4[(s3 >> 24) ] << 24; t[3] = (u32)Td4[(s3 ) & 0xff] ^ (u32)Td4[(s2 >> 8) & 0xff] << 8 ^ (u32)Td4[(s1 >> 16) & 0xff] << 16 ^ (u32)Td4[(s0 >> 24) ] << 24; { int i; u32 tp1, tp2, tp4, tp8, tp9, tpb, tpd, tpe, m; for (i = 0; i < 4; i++) { tp1 = t[i]; m = tp1 & 0x80808080; tp2 = ((tp1 & 0x7f7f7f7f) << 1) ^ ((m - (m >> 7)) & 0x1b1b1b1b); m = tp2 & 0x80808080; tp4 = ((tp2 & 0x7f7f7f7f) << 1) ^ ((m - (m >> 7)) & 0x1b1b1b1b); m = tp4 & 0x80808080; tp8 = ((tp4 & 0x7f7f7f7f) << 1) ^ ((m - (m >> 7)) & 0x1b1b1b1b); tp9 = tp8 ^ tp1; tpb = tp9 ^ tp2; tpd = tp9 ^ tp4; tpe = tp8 ^ tp4 ^ tp2; #if defined(ROTATE) t[i] = tpe ^ ROTATE(tpd,16) ^ ROTATE(tp9,8) ^ ROTATE(tpb,24); #else t[i] = tpe ^ (tpd >> 16) ^ (tpd << 16) ^ (tp9 >> 24) ^ (tp9 << 8) ^ (tpb >> 8) ^ (tpb << 24); #endif t[i] ^= rk[4+i]; } } #else t[0] = Td0[(s0 ) & 0xff] ^ Td1[(s3 >> 8) & 0xff] ^ Td2[(s2 >> 16) & 0xff] ^ Td3[(s1 >> 24) ] ^ rk[4]; t[1] = Td0[(s1 ) & 0xff] ^ Td1[(s0 >> 8) & 0xff] ^ Td2[(s3 >> 16) & 0xff] ^ Td3[(s2 >> 24) ] ^ rk[5]; t[2] = Td0[(s2 ) & 0xff] ^ Td1[(s1 >> 8) & 0xff] ^ Td2[(s0 >> 16) & 0xff] ^ Td3[(s3 >> 24) ] ^ rk[6]; t[3] = Td0[(s3 ) & 0xff] ^ Td1[(s2 >> 8) & 0xff] ^ Td2[(s1 >> 16) & 0xff] ^ Td3[(s0 >> 24) ] ^ rk[7]; #endif s0 = t[0]; s1 = t[1]; s2 = t[2]; s3 = t[3]; for (rk+=8,r=key->rounds-2; r>0; rk+=4,r--) { #if defined(AES_COMPACT_IN_INNER_ROUNDS) t[0] = (u32)Td4[(s0 ) & 0xff] ^ (u32)Td4[(s3 >> 8) & 0xff] << 8 ^ (u32)Td4[(s2 >> 16) & 0xff] << 16 ^ (u32)Td4[(s1 >> 24) ] << 24; t[1] = (u32)Td4[(s1 ) & 0xff] ^ (u32)Td4[(s0 >> 8) & 0xff] << 8 ^ (u32)Td4[(s3 >> 16) & 0xff] << 16 ^ (u32)Td4[(s2 >> 24) ] << 24; t[2] = (u32)Td4[(s2 ) & 0xff] ^ (u32)Td4[(s1 >> 8) & 0xff] << 8 ^ (u32)Td4[(s0 >> 16) & 0xff] << 16 ^ (u32)Td4[(s3 >> 24) ] << 24; t[3] = (u32)Td4[(s3 ) & 0xff] ^ (u32)Td4[(s2 >> 8) & 0xff] << 8 ^ (u32)Td4[(s1 >> 16) & 0xff] << 16 ^ (u32)Td4[(s0 >> 24) ] << 24; { int i; u32 tp1, tp2, tp4, tp8, tp9, tpb, tpd, tpe, m; for (i = 0; i < 4; i++) { tp1 = t[i]; m = tp1 & 0x80808080; tp2 = ((tp1 & 0x7f7f7f7f) << 1) ^ ((m - (m >> 7)) & 0x1b1b1b1b); m = tp2 & 0x80808080; tp4 = ((tp2 & 0x7f7f7f7f) << 1) ^ ((m - (m >> 7)) & 0x1b1b1b1b); m = tp4 & 0x80808080; tp8 = ((tp4 & 0x7f7f7f7f) << 1) ^ ((m - (m >> 7)) & 0x1b1b1b1b); tp9 = tp8 ^ tp1; tpb = tp9 ^ tp2; tpd = tp9 ^ tp4; tpe = tp8 ^ tp4 ^ tp2; #if defined(ROTATE) t[i] = tpe ^ ROTATE(tpd,16) ^ ROTATE(tp9,8) ^ ROTATE(tpb,24); #else t[i] = tpe ^ (tpd >> 16) ^ (tpd << 16) ^ (tp9 >> 24) ^ (tp9 << 8) ^ (tpb >> 8) ^ (tpb << 24); #endif t[i] ^= rk[i]; } } #else t[0] = Td0[(s0 ) & 0xff] ^ Td1[(s3 >> 8) & 0xff] ^ Td2[(s2 >> 16) & 0xff] ^ Td3[(s1 >> 24) ] ^ rk[0]; t[1] = Td0[(s1 ) & 0xff] ^ Td1[(s0 >> 8) & 0xff] ^ Td2[(s3 >> 16) & 0xff] ^ Td3[(s2 >> 24) ] ^ rk[1]; t[2] = Td0[(s2 ) & 0xff] ^ Td1[(s1 >> 8) & 0xff] ^ Td2[(s0 >> 16) & 0xff] ^ Td3[(s3 >> 24) ] ^ rk[2]; t[3] = Td0[(s3 ) & 0xff] ^ Td1[(s2 >> 8) & 0xff] ^ Td2[(s1 >> 16) & 0xff] ^ Td3[(s0 >> 24) ] ^ rk[3]; #endif s0 = t[0]; s1 = t[1]; s2 = t[2]; s3 = t[3]; } prefetch256(Td4); *(u32*)(out+0) = ((u32)Td4[(s0 ) & 0xff]) ^ ((u32)Td4[(s3 >> 8) & 0xff] << 8) ^ ((u32)Td4[(s2 >> 16) & 0xff] << 16) ^ ((u32)Td4[(s1 >> 24) ] << 24) ^ rk[0]; *(u32*)(out+4) = ((u32)Td4[(s1 ) & 0xff]) ^ ((u32)Td4[(s0 >> 8) & 0xff] << 8) ^ ((u32)Td4[(s3 >> 16) & 0xff] << 16) ^ ((u32)Td4[(s2 >> 24) ] << 24) ^ rk[1]; *(u32*)(out+8) = ((u32)Td4[(s2 ) & 0xff]) ^ ((u32)Td4[(s1 >> 8) & 0xff] << 8) ^ ((u32)Td4[(s0 >> 16) & 0xff] << 16) ^ ((u32)Td4[(s3 >> 24) ] << 24) ^ rk[2]; *(u32*)(out+12) = ((u32)Td4[(s3 ) & 0xff]) ^ ((u32)Td4[(s2 >> 8) & 0xff] << 8) ^ ((u32)Td4[(s1 >> 16) & 0xff] << 16) ^ ((u32)Td4[(s0 >> 24) ] << 24) ^ rk[3]; }
aes
openssl/crypto/aes/aes_x86core.c
openssl
#include <assert.h> #include "internal/deprecated.h" #include <openssl/aes.h> #include "aes_local.h" void AES_ecb_encrypt(const unsigned char *in, unsigned char *out, const AES_KEY *key, const int enc) { assert(in && out && key); assert((AES_ENCRYPT == enc) || (AES_DECRYPT == enc)); if (AES_ENCRYPT == enc) AES_encrypt(in, out, key); else AES_decrypt(in, out, key); }
aes
openssl/crypto/aes/aes_ecb.c
openssl
#include "internal/deprecated.h" #include "internal/cryptlib.h" #include <openssl/aes.h> #include <openssl/modes.h> int AES_wrap_key(AES_KEY *key, const unsigned char *iv, unsigned char *out, const unsigned char *in, unsigned int inlen) { return CRYPTO_128_wrap(key, iv, out, in, inlen, (block128_f) AES_encrypt); } int AES_unwrap_key(AES_KEY *key, const unsigned char *iv, unsigned char *out, const unsigned char *in, unsigned int inlen) { return CRYPTO_128_unwrap(key, iv, out, in, inlen, (block128_f) AES_decrypt); }
aes
openssl/crypto/aes/aes_wrap.c
openssl
#include "internal/deprecated.h" #include <openssl/aes.h> #include <openssl/modes.h> void AES_ofb128_encrypt(const unsigned char *in, unsigned char *out, size_t length, const AES_KEY *key, unsigned char *ivec, int *num) { CRYPTO_ofb128_encrypt(in, out, length, key, ivec, num, (block128_f) AES_encrypt); }
aes
openssl/crypto/aes/aes_ofb.c
openssl
#include "internal/deprecated.h" #include <openssl/aes.h> #include <openssl/modes.h> void AES_cbc_encrypt(const unsigned char *in, unsigned char *out, size_t len, const AES_KEY *key, unsigned char *ivec, const int enc) { if (enc) CRYPTO_cbc128_encrypt(in, out, len, key, ivec, (block128_f) AES_encrypt); else CRYPTO_cbc128_decrypt(in, out, len, key, ivec, (block128_f) AES_decrypt); }
aes
openssl/crypto/aes/aes_cbc.c
openssl
#include "internal/deprecated.h" #include <assert.h> #include <stdlib.h> #include <openssl/crypto.h> #include <openssl/aes.h> #include "aes_local.h" #if defined(OPENSSL_AES_CONST_TIME) && !defined(AES_ASM) # if (defined(_WIN32) || defined(_WIN64)) && !defined(__MINGW32__) # define U64(C) C##UI64 # elif defined(__arch64__) # define U64(C) C##UL # else # define U64(C) C##ULL # endif typedef union { unsigned char b[8]; u32 w[2]; u64 d; } uni; static void XtimeWord(u32 *w) { u32 a, b; a = *w; b = a & 0x80808080u; a ^= b; b -= b >> 7; b &= 0x1B1B1B1Bu; b ^= a << 1; *w = b; } static void XtimeLong(u64 *w) { u64 a, b; a = *w; b = a & U64(0x8080808080808080); a ^= b; b -= b >> 7; b &= U64(0x1B1B1B1B1B1B1B1B); b ^= a << 1; *w = b; } static void SubWord(u32 *w) { u32 x, y, a1, a2, a3, a4, a5, a6; x = *w; y = ((x & 0xFEFEFEFEu) >> 1) | ((x & 0x01010101u) << 7); x &= 0xDDDDDDDDu; x ^= y & 0x57575757u; y = ((y & 0xFEFEFEFEu) >> 1) | ((y & 0x01010101u) << 7); x ^= y & 0x1C1C1C1Cu; y = ((y & 0xFEFEFEFEu) >> 1) | ((y & 0x01010101u) << 7); x ^= y & 0x4A4A4A4Au; y = ((y & 0xFEFEFEFEu) >> 1) | ((y & 0x01010101u) << 7); x ^= y & 0x42424242u; y = ((y & 0xFEFEFEFEu) >> 1) | ((y & 0x01010101u) << 7); x ^= y & 0x64646464u; y = ((y & 0xFEFEFEFEu) >> 1) | ((y & 0x01010101u) << 7); x ^= y & 0xE0E0E0E0u; a1 = x; a1 ^= (x & 0xF0F0F0F0u) >> 4; a2 = ((x & 0xCCCCCCCCu) >> 2) | ((x & 0x33333333u) << 2); a3 = x & a1; a3 ^= (a3 & 0xAAAAAAAAu) >> 1; a3 ^= (((x << 1) & a1) ^ ((a1 << 1) & x)) & 0xAAAAAAAAu; a4 = a2 & a1; a4 ^= (a4 & 0xAAAAAAAAu) >> 1; a4 ^= (((a2 << 1) & a1) ^ ((a1 << 1) & a2)) & 0xAAAAAAAAu; a5 = (a3 & 0xCCCCCCCCu) >> 2; a3 ^= ((a4 << 2) ^ a4) & 0xCCCCCCCCu; a4 = a5 & 0x22222222u; a4 |= a4 >> 1; a4 ^= (a5 << 1) & 0x22222222u; a3 ^= a4; a5 = a3 & 0xA0A0A0A0u; a5 |= a5 >> 1; a5 ^= (a3 << 1) & 0xA0A0A0A0u; a4 = a5 & 0xC0C0C0C0u; a6 = a4 >> 2; a4 ^= (a5 << 2) & 0xC0C0C0C0u; a5 = a6 & 0x20202020u; a5 |= a5 >> 1; a5 ^= (a6 << 1) & 0x20202020u; a4 |= a5; a3 ^= a4 >> 4; a3 &= 0x0F0F0F0Fu; a2 = a3; a2 ^= (a3 & 0x0C0C0C0Cu) >> 2; a4 = a3 & a2; a4 ^= (a4 & 0x0A0A0A0A0Au) >> 1; a4 ^= (((a3 << 1) & a2) ^ ((a2 << 1) & a3)) & 0x0A0A0A0Au; a5 = a4 & 0x08080808u; a5 |= a5 >> 1; a5 ^= (a4 << 1) & 0x08080808u; a4 ^= a5 >> 2; a4 &= 0x03030303u; a4 ^= (a4 & 0x02020202u) >> 1; a4 |= a4 << 2; a3 = a2 & a4; a3 ^= (a3 & 0x0A0A0A0Au) >> 1; a3 ^= (((a2 << 1) & a4) ^ ((a4 << 1) & a2)) & 0x0A0A0A0Au; a3 |= a3 << 4; a2 = ((a1 & 0xCCCCCCCCu) >> 2) | ((a1 & 0x33333333u) << 2); x = a1 & a3; x ^= (x & 0xAAAAAAAAu) >> 1; x ^= (((a1 << 1) & a3) ^ ((a3 << 1) & a1)) & 0xAAAAAAAAu; a4 = a2 & a3; a4 ^= (a4 & 0xAAAAAAAAu) >> 1; a4 ^= (((a2 << 1) & a3) ^ ((a3 << 1) & a2)) & 0xAAAAAAAAu; a5 = (x & 0xCCCCCCCCu) >> 2; x ^= ((a4 << 2) ^ a4) & 0xCCCCCCCCu; a4 = a5 & 0x22222222u; a4 |= a4 >> 1; a4 ^= (a5 << 1) & 0x22222222u; x ^= a4; y = ((x & 0xFEFEFEFEu) >> 1) | ((x & 0x01010101u) << 7); x &= 0x39393939u; x ^= y & 0x3F3F3F3Fu; y = ((y & 0xFCFCFCFCu) >> 2) | ((y & 0x03030303u) << 6); x ^= y & 0x97979797u; y = ((y & 0xFEFEFEFEu) >> 1) | ((y & 0x01010101u) << 7); x ^= y & 0x9B9B9B9Bu; y = ((y & 0xFEFEFEFEu) >> 1) | ((y & 0x01010101u) << 7); x ^= y & 0x3C3C3C3Cu; y = ((y & 0xFEFEFEFEu) >> 1) | ((y & 0x01010101u) << 7); x ^= y & 0xDDDDDDDDu; y = ((y & 0xFEFEFEFEu) >> 1) | ((y & 0x01010101u) << 7); x ^= y & 0x72727272u; x ^= 0x63636363u; *w = x; } static void SubLong(u64 *w) { u64 x, y, a1, a2, a3, a4, a5, a6; x = *w; y = ((x & U64(0xFEFEFEFEFEFEFEFE)) >> 1) | ((x & U64(0x0101010101010101)) << 7); x &= U64(0xDDDDDDDDDDDDDDDD); x ^= y & U64(0x5757575757575757); y = ((y & U64(0xFEFEFEFEFEFEFEFE)) >> 1) | ((y & U64(0x0101010101010101)) << 7); x ^= y & U64(0x1C1C1C1C1C1C1C1C); y = ((y & U64(0xFEFEFEFEFEFEFEFE)) >> 1) | ((y & U64(0x0101010101010101)) << 7); x ^= y & U64(0x4A4A4A4A4A4A4A4A); y = ((y & U64(0xFEFEFEFEFEFEFEFE)) >> 1) | ((y & U64(0x0101010101010101)) << 7); x ^= y & U64(0x4242424242424242); y = ((y & U64(0xFEFEFEFEFEFEFEFE)) >> 1) | ((y & U64(0x0101010101010101)) << 7); x ^= y & U64(0x6464646464646464); y = ((y & U64(0xFEFEFEFEFEFEFEFE)) >> 1) | ((y & U64(0x0101010101010101)) << 7); x ^= y & U64(0xE0E0E0E0E0E0E0E0); a1 = x; a1 ^= (x & U64(0xF0F0F0F0F0F0F0F0)) >> 4; a2 = ((x & U64(0xCCCCCCCCCCCCCCCC)) >> 2) | ((x & U64(0x3333333333333333)) << 2); a3 = x & a1; a3 ^= (a3 & U64(0xAAAAAAAAAAAAAAAA)) >> 1; a3 ^= (((x << 1) & a1) ^ ((a1 << 1) & x)) & U64(0xAAAAAAAAAAAAAAAA); a4 = a2 & a1; a4 ^= (a4 & U64(0xAAAAAAAAAAAAAAAA)) >> 1; a4 ^= (((a2 << 1) & a1) ^ ((a1 << 1) & a2)) & U64(0xAAAAAAAAAAAAAAAA); a5 = (a3 & U64(0xCCCCCCCCCCCCCCCC)) >> 2; a3 ^= ((a4 << 2) ^ a4) & U64(0xCCCCCCCCCCCCCCCC); a4 = a5 & U64(0x2222222222222222); a4 |= a4 >> 1; a4 ^= (a5 << 1) & U64(0x2222222222222222); a3 ^= a4; a5 = a3 & U64(0xA0A0A0A0A0A0A0A0); a5 |= a5 >> 1; a5 ^= (a3 << 1) & U64(0xA0A0A0A0A0A0A0A0); a4 = a5 & U64(0xC0C0C0C0C0C0C0C0); a6 = a4 >> 2; a4 ^= (a5 << 2) & U64(0xC0C0C0C0C0C0C0C0); a5 = a6 & U64(0x2020202020202020); a5 |= a5 >> 1; a5 ^= (a6 << 1) & U64(0x2020202020202020); a4 |= a5; a3 ^= a4 >> 4; a3 &= U64(0x0F0F0F0F0F0F0F0F); a2 = a3; a2 ^= (a3 & U64(0x0C0C0C0C0C0C0C0C)) >> 2; a4 = a3 & a2; a4 ^= (a4 & U64(0x0A0A0A0A0A0A0A0A)) >> 1; a4 ^= (((a3 << 1) & a2) ^ ((a2 << 1) & a3)) & U64(0x0A0A0A0A0A0A0A0A); a5 = a4 & U64(0x0808080808080808); a5 |= a5 >> 1; a5 ^= (a4 << 1) & U64(0x0808080808080808); a4 ^= a5 >> 2; a4 &= U64(0x0303030303030303); a4 ^= (a4 & U64(0x0202020202020202)) >> 1; a4 |= a4 << 2; a3 = a2 & a4; a3 ^= (a3 & U64(0x0A0A0A0A0A0A0A0A)) >> 1; a3 ^= (((a2 << 1) & a4) ^ ((a4 << 1) & a2)) & U64(0x0A0A0A0A0A0A0A0A); a3 |= a3 << 4; a2 = ((a1 & U64(0xCCCCCCCCCCCCCCCC)) >> 2) | ((a1 & U64(0x3333333333333333)) << 2); x = a1 & a3; x ^= (x & U64(0xAAAAAAAAAAAAAAAA)) >> 1; x ^= (((a1 << 1) & a3) ^ ((a3 << 1) & a1)) & U64(0xAAAAAAAAAAAAAAAA); a4 = a2 & a3; a4 ^= (a4 & U64(0xAAAAAAAAAAAAAAAA)) >> 1; a4 ^= (((a2 << 1) & a3) ^ ((a3 << 1) & a2)) & U64(0xAAAAAAAAAAAAAAAA); a5 = (x & U64(0xCCCCCCCCCCCCCCCC)) >> 2; x ^= ((a4 << 2) ^ a4) & U64(0xCCCCCCCCCCCCCCCC); a4 = a5 & U64(0x2222222222222222); a4 |= a4 >> 1; a4 ^= (a5 << 1) & U64(0x2222222222222222); x ^= a4; y = ((x & U64(0xFEFEFEFEFEFEFEFE)) >> 1) | ((x & U64(0x0101010101010101)) << 7); x &= U64(0x3939393939393939); x ^= y & U64(0x3F3F3F3F3F3F3F3F); y = ((y & U64(0xFCFCFCFCFCFCFCFC)) >> 2) | ((y & U64(0x0303030303030303)) << 6); x ^= y & U64(0x9797979797979797); y = ((y & U64(0xFEFEFEFEFEFEFEFE)) >> 1) | ((y & U64(0x0101010101010101)) << 7); x ^= y & U64(0x9B9B9B9B9B9B9B9B); y = ((y & U64(0xFEFEFEFEFEFEFEFE)) >> 1) | ((y & U64(0x0101010101010101)) << 7); x ^= y & U64(0x3C3C3C3C3C3C3C3C); y = ((y & U64(0xFEFEFEFEFEFEFEFE)) >> 1) | ((y & U64(0x0101010101010101)) << 7); x ^= y & U64(0xDDDDDDDDDDDDDDDD); y = ((y & U64(0xFEFEFEFEFEFEFEFE)) >> 1) | ((y & U64(0x0101010101010101)) << 7); x ^= y & U64(0x7272727272727272); x ^= U64(0x6363636363636363); *w = x; } static void InvSubLong(u64 *w) { u64 x, y, a1, a2, a3, a4, a5, a6; x = *w; x ^= U64(0x6363636363636363); y = ((x & U64(0xFEFEFEFEFEFEFEFE)) >> 1) | ((x & U64(0x0101010101010101)) << 7); x &= U64(0xFDFDFDFDFDFDFDFD); x ^= y & U64(0x5E5E5E5E5E5E5E5E); y = ((y & U64(0xFEFEFEFEFEFEFEFE)) >> 1) | ((y & U64(0x0101010101010101)) << 7); x ^= y & U64(0xF3F3F3F3F3F3F3F3); y = ((y & U64(0xFEFEFEFEFEFEFEFE)) >> 1) | ((y & U64(0x0101010101010101)) << 7); x ^= y & U64(0xF5F5F5F5F5F5F5F5); y = ((y & U64(0xFEFEFEFEFEFEFEFE)) >> 1) | ((y & U64(0x0101010101010101)) << 7); x ^= y & U64(0x7878787878787878); y = ((y & U64(0xFEFEFEFEFEFEFEFE)) >> 1) | ((y & U64(0x0101010101010101)) << 7); x ^= y & U64(0x7777777777777777); y = ((y & U64(0xFEFEFEFEFEFEFEFE)) >> 1) | ((y & U64(0x0101010101010101)) << 7); x ^= y & U64(0x1515151515151515); y = ((y & U64(0xFEFEFEFEFEFEFEFE)) >> 1) | ((y & U64(0x0101010101010101)) << 7); x ^= y & U64(0xA5A5A5A5A5A5A5A5); a1 = x; a1 ^= (x & U64(0xF0F0F0F0F0F0F0F0)) >> 4; a2 = ((x & U64(0xCCCCCCCCCCCCCCCC)) >> 2) | ((x & U64(0x3333333333333333)) << 2); a3 = x & a1; a3 ^= (a3 & U64(0xAAAAAAAAAAAAAAAA)) >> 1; a3 ^= (((x << 1) & a1) ^ ((a1 << 1) & x)) & U64(0xAAAAAAAAAAAAAAAA); a4 = a2 & a1; a4 ^= (a4 & U64(0xAAAAAAAAAAAAAAAA)) >> 1; a4 ^= (((a2 << 1) & a1) ^ ((a1 << 1) & a2)) & U64(0xAAAAAAAAAAAAAAAA); a5 = (a3 & U64(0xCCCCCCCCCCCCCCCC)) >> 2; a3 ^= ((a4 << 2) ^ a4) & U64(0xCCCCCCCCCCCCCCCC); a4 = a5 & U64(0x2222222222222222); a4 |= a4 >> 1; a4 ^= (a5 << 1) & U64(0x2222222222222222); a3 ^= a4; a5 = a3 & U64(0xA0A0A0A0A0A0A0A0); a5 |= a5 >> 1; a5 ^= (a3 << 1) & U64(0xA0A0A0A0A0A0A0A0); a4 = a5 & U64(0xC0C0C0C0C0C0C0C0); a6 = a4 >> 2; a4 ^= (a5 << 2) & U64(0xC0C0C0C0C0C0C0C0); a5 = a6 & U64(0x2020202020202020); a5 |= a5 >> 1; a5 ^= (a6 << 1) & U64(0x2020202020202020); a4 |= a5; a3 ^= a4 >> 4; a3 &= U64(0x0F0F0F0F0F0F0F0F); a2 = a3; a2 ^= (a3 & U64(0x0C0C0C0C0C0C0C0C)) >> 2; a4 = a3 & a2; a4 ^= (a4 & U64(0x0A0A0A0A0A0A0A0A)) >> 1; a4 ^= (((a3 << 1) & a2) ^ ((a2 << 1) & a3)) & U64(0x0A0A0A0A0A0A0A0A); a5 = a4 & U64(0x0808080808080808); a5 |= a5 >> 1; a5 ^= (a4 << 1) & U64(0x0808080808080808); a4 ^= a5 >> 2; a4 &= U64(0x0303030303030303); a4 ^= (a4 & U64(0x0202020202020202)) >> 1; a4 |= a4 << 2; a3 = a2 & a4; a3 ^= (a3 & U64(0x0A0A0A0A0A0A0A0A)) >> 1; a3 ^= (((a2 << 1) & a4) ^ ((a4 << 1) & a2)) & U64(0x0A0A0A0A0A0A0A0A); a3 |= a3 << 4; a2 = ((a1 & U64(0xCCCCCCCCCCCCCCCC)) >> 2) | ((a1 & U64(0x3333333333333333)) << 2); x = a1 & a3; x ^= (x & U64(0xAAAAAAAAAAAAAAAA)) >> 1; x ^= (((a1 << 1) & a3) ^ ((a3 << 1) & a1)) & U64(0xAAAAAAAAAAAAAAAA); a4 = a2 & a3; a4 ^= (a4 & U64(0xAAAAAAAAAAAAAAAA)) >> 1; a4 ^= (((a2 << 1) & a3) ^ ((a3 << 1) & a2)) & U64(0xAAAAAAAAAAAAAAAA); a5 = (x & U64(0xCCCCCCCCCCCCCCCC)) >> 2; x ^= ((a4 << 2) ^ a4) & U64(0xCCCCCCCCCCCCCCCC); a4 = a5 & U64(0x2222222222222222); a4 |= a4 >> 1; a4 ^= (a5 << 1) & U64(0x2222222222222222); x ^= a4; y = ((x & U64(0xFEFEFEFEFEFEFEFE)) >> 1) | ((x & U64(0x0101010101010101)) << 7); x &= U64(0xB5B5B5B5B5B5B5B5); x ^= y & U64(0x4040404040404040); y = ((y & U64(0xFEFEFEFEFEFEFEFE)) >> 1) | ((y & U64(0x0101010101010101)) << 7); x ^= y & U64(0x8080808080808080); y = ((y & U64(0xFEFEFEFEFEFEFEFE)) >> 1) | ((y & U64(0x0101010101010101)) << 7); x ^= y & U64(0x1616161616161616); y = ((y & U64(0xFEFEFEFEFEFEFEFE)) >> 1) | ((y & U64(0x0101010101010101)) << 7); x ^= y & U64(0xEBEBEBEBEBEBEBEB); y = ((y & U64(0xFEFEFEFEFEFEFEFE)) >> 1) | ((y & U64(0x0101010101010101)) << 7); x ^= y & U64(0x9797979797979797); y = ((y & U64(0xFEFEFEFEFEFEFEFE)) >> 1) | ((y & U64(0x0101010101010101)) << 7); x ^= y & U64(0xFBFBFBFBFBFBFBFB); y = ((y & U64(0xFEFEFEFEFEFEFEFE)) >> 1) | ((y & U64(0x0101010101010101)) << 7); x ^= y & U64(0x7D7D7D7D7D7D7D7D); *w = x; } static void ShiftRows(u64 *state) { unsigned char s[4]; unsigned char *s0; int r; s0 = (unsigned char *)state; for (r = 0; r < 4; r++) { s[0] = s0[0*4 + r]; s[1] = s0[1*4 + r]; s[2] = s0[2*4 + r]; s[3] = s0[3*4 + r]; s0[0*4 + r] = s[(r+0) % 4]; s0[1*4 + r] = s[(r+1) % 4]; s0[2*4 + r] = s[(r+2) % 4]; s0[3*4 + r] = s[(r+3) % 4]; } } static void InvShiftRows(u64 *state) { unsigned char s[4]; unsigned char *s0; int r; s0 = (unsigned char *)state; for (r = 0; r < 4; r++) { s[0] = s0[0*4 + r]; s[1] = s0[1*4 + r]; s[2] = s0[2*4 + r]; s[3] = s0[3*4 + r]; s0[0*4 + r] = s[(4-r) % 4]; s0[1*4 + r] = s[(5-r) % 4]; s0[2*4 + r] = s[(6-r) % 4]; s0[3*4 + r] = s[(7-r) % 4]; } } static void MixColumns(u64 *state) { uni s1; uni s; int c; for (c = 0; c < 2; c++) { s1.d = state[c]; s.d = s1.d; s.d ^= ((s.d & U64(0xFFFF0000FFFF0000)) >> 16) | ((s.d & U64(0x0000FFFF0000FFFF)) << 16); s.d ^= ((s.d & U64(0xFF00FF00FF00FF00)) >> 8) | ((s.d & U64(0x00FF00FF00FF00FF)) << 8); s.d ^= s1.d; XtimeLong(&s1.d); s.d ^= s1.d; s.b[0] ^= s1.b[1]; s.b[1] ^= s1.b[2]; s.b[2] ^= s1.b[3]; s.b[3] ^= s1.b[0]; s.b[4] ^= s1.b[5]; s.b[5] ^= s1.b[6]; s.b[6] ^= s1.b[7]; s.b[7] ^= s1.b[4]; state[c] = s.d; } } static void InvMixColumns(u64 *state) { uni s1; uni s; int c; for (c = 0; c < 2; c++) { s1.d = state[c]; s.d = s1.d; s.d ^= ((s.d & U64(0xFFFF0000FFFF0000)) >> 16) | ((s.d & U64(0x0000FFFF0000FFFF)) << 16); s.d ^= ((s.d & U64(0xFF00FF00FF00FF00)) >> 8) | ((s.d & U64(0x00FF00FF00FF00FF)) << 8); s.d ^= s1.d; XtimeLong(&s1.d); s.d ^= s1.d; s.b[0] ^= s1.b[1]; s.b[1] ^= s1.b[2]; s.b[2] ^= s1.b[3]; s.b[3] ^= s1.b[0]; s.b[4] ^= s1.b[5]; s.b[5] ^= s1.b[6]; s.b[6] ^= s1.b[7]; s.b[7] ^= s1.b[4]; XtimeLong(&s1.d); s1.d ^= ((s1.d & U64(0xFFFF0000FFFF0000)) >> 16) | ((s1.d & U64(0x0000FFFF0000FFFF)) << 16); s.d ^= s1.d; XtimeLong(&s1.d); s1.d ^= ((s1.d & U64(0xFF00FF00FF00FF00)) >> 8) | ((s1.d & U64(0x00FF00FF00FF00FF)) << 8); s.d ^= s1.d; state[c] = s.d; } } static void AddRoundKey(u64 *state, const u64 *w) { state[0] ^= w[0]; state[1] ^= w[1]; } static void Cipher(const unsigned char *in, unsigned char *out, const u64 *w, int nr) { u64 state[2]; int i; memcpy(state, in, 16); AddRoundKey(state, w); for (i = 1; i < nr; i++) { SubLong(&state[0]); SubLong(&state[1]); ShiftRows(state); MixColumns(state); AddRoundKey(state, w + i*2); } SubLong(&state[0]); SubLong(&state[1]); ShiftRows(state); AddRoundKey(state, w + nr*2); memcpy(out, state, 16); } static void InvCipher(const unsigned char *in, unsigned char *out, const u64 *w, int nr) { u64 state[2]; int i; memcpy(state, in, 16); AddRoundKey(state, w + nr*2); for (i = nr - 1; i > 0; i--) { InvShiftRows(state); InvSubLong(&state[0]); InvSubLong(&state[1]); AddRoundKey(state, w + i*2); InvMixColumns(state); } InvShiftRows(state); InvSubLong(&state[0]); InvSubLong(&state[1]); AddRoundKey(state, w); memcpy(out, state, 16); } static void RotWord(u32 *x) { unsigned char *w0; unsigned char tmp; w0 = (unsigned char *)x; tmp = w0[0]; w0[0] = w0[1]; w0[1] = w0[2]; w0[2] = w0[3]; w0[3] = tmp; } static void KeyExpansion(const unsigned char *key, u64 *w, int nr, int nk) { u32 rcon; uni prev; u32 temp; int i, n; memcpy(w, key, nk*4); memcpy(&rcon, "\1\0\0\0", 4); n = nk/2; prev.d = w[n-1]; for (i = n; i < (nr+1)*2; i++) { temp = prev.w[1]; if (i % n == 0) { RotWord(&temp); SubWord(&temp); temp ^= rcon; XtimeWord(&rcon); } else if (nk > 6 && i % n == 2) { SubWord(&temp); } prev.d = w[i-n]; prev.w[0] ^= temp; prev.w[1] ^= prev.w[0]; w[i] = prev.d; } } int AES_set_encrypt_key(const unsigned char *userKey, const int bits, AES_KEY *key) { u64 *rk; if (!userKey || !key) return -1; if (bits != 128 && bits != 192 && bits != 256) return -2; rk = (u64*)key->rd_key; if (bits == 128) key->rounds = 10; else if (bits == 192) key->rounds = 12; else key->rounds = 14; KeyExpansion(userKey, rk, key->rounds, bits/32); return 0; } int AES_set_decrypt_key(const unsigned char *userKey, const int bits, AES_KEY *key) { return AES_set_encrypt_key(userKey, bits, key); } void AES_encrypt(const unsigned char *in, unsigned char *out, const AES_KEY *key) { const u64 *rk; assert(in && out && key); rk = (u64*)key->rd_key; Cipher(in, out, rk, key->rounds); } void AES_decrypt(const unsigned char *in, unsigned char *out, const AES_KEY *key) { const u64 *rk; assert(in && out && key); rk = (u64*)key->rd_key; InvCipher(in, out, rk, key->rounds); } #elif !defined(AES_ASM) static const u32 Te0[256] = { 0xc66363a5U, 0xf87c7c84U, 0xee777799U, 0xf67b7b8dU, 0xfff2f20dU, 0xd66b6bbdU, 0xde6f6fb1U, 0x91c5c554U, 0x60303050U, 0x02010103U, 0xce6767a9U, 0x562b2b7dU, 0xe7fefe19U, 0xb5d7d762U, 0x4dababe6U, 0xec76769aU, 0x8fcaca45U, 0x1f82829dU, 0x89c9c940U, 0xfa7d7d87U, 0xeffafa15U, 0xb25959ebU, 0x8e4747c9U, 0xfbf0f00bU, 0x41adadecU, 0xb3d4d467U, 0x5fa2a2fdU, 0x45afafeaU, 0x239c9cbfU, 0x53a4a4f7U, 0xe4727296U, 0x9bc0c05bU, 0x75b7b7c2U, 0xe1fdfd1cU, 0x3d9393aeU, 0x4c26266aU, 0x6c36365aU, 0x7e3f3f41U, 0xf5f7f702U, 0x83cccc4fU, 0x6834345cU, 0x51a5a5f4U, 0xd1e5e534U, 0xf9f1f108U, 0xe2717193U, 0xabd8d873U, 0x62313153U, 0x2a15153fU, 0x0804040cU, 0x95c7c752U, 0x46232365U, 0x9dc3c35eU, 0x30181828U, 0x379696a1U, 0x0a05050fU, 0x2f9a9ab5U, 0x0e070709U, 0x24121236U, 0x1b80809bU, 0xdfe2e23dU, 0xcdebeb26U, 0x4e272769U, 0x7fb2b2cdU, 0xea75759fU, 0x1209091bU, 0x1d83839eU, 0x582c2c74U, 0x341a1a2eU, 0x361b1b2dU, 0xdc6e6eb2U, 0xb45a5aeeU, 0x5ba0a0fbU, 0xa45252f6U, 0x763b3b4dU, 0xb7d6d661U, 0x7db3b3ceU, 0x5229297bU, 0xdde3e33eU, 0x5e2f2f71U, 0x13848497U, 0xa65353f5U, 0xb9d1d168U, 0x00000000U, 0xc1eded2cU, 0x40202060U, 0xe3fcfc1fU, 0x79b1b1c8U, 0xb65b5bedU, 0xd46a6abeU, 0x8dcbcb46U, 0x67bebed9U, 0x7239394bU, 0x944a4adeU, 0x984c4cd4U, 0xb05858e8U, 0x85cfcf4aU, 0xbbd0d06bU, 0xc5efef2aU, 0x4faaaae5U, 0xedfbfb16U, 0x864343c5U, 0x9a4d4dd7U, 0x66333355U, 0x11858594U, 0x8a4545cfU, 0xe9f9f910U, 0x04020206U, 0xfe7f7f81U, 0xa05050f0U, 0x783c3c44U, 0x259f9fbaU, 0x4ba8a8e3U, 0xa25151f3U, 0x5da3a3feU, 0x804040c0U, 0x058f8f8aU, 0x3f9292adU, 0x219d9dbcU, 0x70383848U, 0xf1f5f504U, 0x63bcbcdfU, 0x77b6b6c1U, 0xafdada75U, 0x42212163U, 0x20101030U, 0xe5ffff1aU, 0xfdf3f30eU, 0xbfd2d26dU, 0x81cdcd4cU, 0x180c0c14U, 0x26131335U, 0xc3ecec2fU, 0xbe5f5fe1U, 0x359797a2U, 0x884444ccU, 0x2e171739U, 0x93c4c457U, 0x55a7a7f2U, 0xfc7e7e82U, 0x7a3d3d47U, 0xc86464acU, 0xba5d5de7U, 0x3219192bU, 0xe6737395U, 0xc06060a0U, 0x19818198U, 0x9e4f4fd1U, 0xa3dcdc7fU, 0x44222266U, 0x542a2a7eU, 0x3b9090abU, 0x0b888883U, 0x8c4646caU, 0xc7eeee29U, 0x6bb8b8d3U, 0x2814143cU, 0xa7dede79U, 0xbc5e5ee2U, 0x160b0b1dU, 0xaddbdb76U, 0xdbe0e03bU, 0x64323256U, 0x743a3a4eU, 0x140a0a1eU, 0x924949dbU, 0x0c06060aU, 0x4824246cU, 0xb85c5ce4U, 0x9fc2c25dU, 0xbdd3d36eU, 0x43acacefU, 0xc46262a6U, 0x399191a8U, 0x319595a4U, 0xd3e4e437U, 0xf279798bU, 0xd5e7e732U, 0x8bc8c843U, 0x6e373759U, 0xda6d6db7U, 0x018d8d8cU, 0xb1d5d564U, 0x9c4e4ed2U, 0x49a9a9e0U, 0xd86c6cb4U, 0xac5656faU, 0xf3f4f407U, 0xcfeaea25U, 0xca6565afU, 0xf47a7a8eU, 0x47aeaee9U, 0x10080818U, 0x6fbabad5U, 0xf0787888U, 0x4a25256fU, 0x5c2e2e72U, 0x381c1c24U, 0x57a6a6f1U, 0x73b4b4c7U, 0x97c6c651U, 0xcbe8e823U, 0xa1dddd7cU, 0xe874749cU, 0x3e1f1f21U, 0x964b4bddU, 0x61bdbddcU, 0x0d8b8b86U, 0x0f8a8a85U, 0xe0707090U, 0x7c3e3e42U, 0x71b5b5c4U, 0xcc6666aaU, 0x904848d8U, 0x06030305U, 0xf7f6f601U, 0x1c0e0e12U, 0xc26161a3U, 0x6a35355fU, 0xae5757f9U, 0x69b9b9d0U, 0x17868691U, 0x99c1c158U, 0x3a1d1d27U, 0x279e9eb9U, 0xd9e1e138U, 0xebf8f813U, 0x2b9898b3U, 0x22111133U, 0xd26969bbU, 0xa9d9d970U, 0x078e8e89U, 0x339494a7U, 0x2d9b9bb6U, 0x3c1e1e22U, 0x15878792U, 0xc9e9e920U, 0x87cece49U, 0xaa5555ffU, 0x50282878U, 0xa5dfdf7aU, 0x038c8c8fU, 0x59a1a1f8U, 0x09898980U, 0x1a0d0d17U, 0x65bfbfdaU, 0xd7e6e631U, 0x844242c6U, 0xd06868b8U, 0x824141c3U, 0x299999b0U, 0x5a2d2d77U, 0x1e0f0f11U, 0x7bb0b0cbU, 0xa85454fcU, 0x6dbbbbd6U, 0x2c16163aU, }; static const u32 Te1[256] = { 0xa5c66363U, 0x84f87c7cU, 0x99ee7777U, 0x8df67b7bU, 0x0dfff2f2U, 0xbdd66b6bU, 0xb1de6f6fU, 0x5491c5c5U, 0x50603030U, 0x03020101U, 0xa9ce6767U, 0x7d562b2bU, 0x19e7fefeU, 0x62b5d7d7U, 0xe64dababU, 0x9aec7676U, 0x458fcacaU, 0x9d1f8282U, 0x4089c9c9U, 0x87fa7d7dU, 0x15effafaU, 0xebb25959U, 0xc98e4747U, 0x0bfbf0f0U, 0xec41adadU, 0x67b3d4d4U, 0xfd5fa2a2U, 0xea45afafU, 0xbf239c9cU, 0xf753a4a4U, 0x96e47272U, 0x5b9bc0c0U, 0xc275b7b7U, 0x1ce1fdfdU, 0xae3d9393U, 0x6a4c2626U, 0x5a6c3636U, 0x417e3f3fU, 0x02f5f7f7U, 0x4f83ccccU, 0x5c683434U, 0xf451a5a5U, 0x34d1e5e5U, 0x08f9f1f1U, 0x93e27171U, 0x73abd8d8U, 0x53623131U, 0x3f2a1515U, 0x0c080404U, 0x5295c7c7U, 0x65462323U, 0x5e9dc3c3U, 0x28301818U, 0xa1379696U, 0x0f0a0505U, 0xb52f9a9aU, 0x090e0707U, 0x36241212U, 0x9b1b8080U, 0x3ddfe2e2U, 0x26cdebebU, 0x694e2727U, 0xcd7fb2b2U, 0x9fea7575U, 0x1b120909U, 0x9e1d8383U, 0x74582c2cU, 0x2e341a1aU, 0x2d361b1bU, 0xb2dc6e6eU, 0xeeb45a5aU, 0xfb5ba0a0U, 0xf6a45252U, 0x4d763b3bU, 0x61b7d6d6U, 0xce7db3b3U, 0x7b522929U, 0x3edde3e3U, 0x715e2f2fU, 0x97138484U, 0xf5a65353U, 0x68b9d1d1U, 0x00000000U, 0x2cc1ededU, 0x60402020U, 0x1fe3fcfcU, 0xc879b1b1U, 0xedb65b5bU, 0xbed46a6aU, 0x468dcbcbU, 0xd967bebeU, 0x4b723939U, 0xde944a4aU, 0xd4984c4cU, 0xe8b05858U, 0x4a85cfcfU, 0x6bbbd0d0U, 0x2ac5efefU, 0xe54faaaaU, 0x16edfbfbU, 0xc5864343U, 0xd79a4d4dU, 0x55663333U, 0x94118585U, 0xcf8a4545U, 0x10e9f9f9U, 0x06040202U, 0x81fe7f7fU, 0xf0a05050U, 0x44783c3cU, 0xba259f9fU, 0xe34ba8a8U, 0xf3a25151U, 0xfe5da3a3U, 0xc0804040U, 0x8a058f8fU, 0xad3f9292U, 0xbc219d9dU, 0x48703838U, 0x04f1f5f5U, 0xdf63bcbcU, 0xc177b6b6U, 0x75afdadaU, 0x63422121U, 0x30201010U, 0x1ae5ffffU, 0x0efdf3f3U, 0x6dbfd2d2U, 0x4c81cdcdU, 0x14180c0cU, 0x35261313U, 0x2fc3ececU, 0xe1be5f5fU, 0xa2359797U, 0xcc884444U, 0x392e1717U, 0x5793c4c4U, 0xf255a7a7U, 0x82fc7e7eU, 0x477a3d3dU, 0xacc86464U, 0xe7ba5d5dU, 0x2b321919U, 0x95e67373U, 0xa0c06060U, 0x98198181U, 0xd19e4f4fU, 0x7fa3dcdcU, 0x66442222U, 0x7e542a2aU, 0xab3b9090U, 0x830b8888U, 0xca8c4646U, 0x29c7eeeeU, 0xd36bb8b8U, 0x3c281414U, 0x79a7dedeU, 0xe2bc5e5eU, 0x1d160b0bU, 0x76addbdbU, 0x3bdbe0e0U, 0x56643232U, 0x4e743a3aU, 0x1e140a0aU, 0xdb924949U, 0x0a0c0606U, 0x6c482424U, 0xe4b85c5cU, 0x5d9fc2c2U, 0x6ebdd3d3U, 0xef43acacU, 0xa6c46262U, 0xa8399191U, 0xa4319595U, 0x37d3e4e4U, 0x8bf27979U, 0x32d5e7e7U, 0x438bc8c8U, 0x596e3737U, 0xb7da6d6dU, 0x8c018d8dU, 0x64b1d5d5U, 0xd29c4e4eU, 0xe049a9a9U, 0xb4d86c6cU, 0xfaac5656U, 0x07f3f4f4U, 0x25cfeaeaU, 0xafca6565U, 0x8ef47a7aU, 0xe947aeaeU, 0x18100808U, 0xd56fbabaU, 0x88f07878U, 0x6f4a2525U, 0x725c2e2eU, 0x24381c1cU, 0xf157a6a6U, 0xc773b4b4U, 0x5197c6c6U, 0x23cbe8e8U, 0x7ca1ddddU, 0x9ce87474U, 0x213e1f1fU, 0xdd964b4bU, 0xdc61bdbdU, 0x860d8b8bU, 0x850f8a8aU, 0x90e07070U, 0x427c3e3eU, 0xc471b5b5U, 0xaacc6666U, 0xd8904848U, 0x05060303U, 0x01f7f6f6U, 0x121c0e0eU, 0xa3c26161U, 0x5f6a3535U, 0xf9ae5757U, 0xd069b9b9U, 0x91178686U, 0x5899c1c1U, 0x273a1d1dU, 0xb9279e9eU, 0x38d9e1e1U, 0x13ebf8f8U, 0xb32b9898U, 0x33221111U, 0xbbd26969U, 0x70a9d9d9U, 0x89078e8eU, 0xa7339494U, 0xb62d9b9bU, 0x223c1e1eU, 0x92158787U, 0x20c9e9e9U, 0x4987ceceU, 0xffaa5555U, 0x78502828U, 0x7aa5dfdfU, 0x8f038c8cU, 0xf859a1a1U, 0x80098989U, 0x171a0d0dU, 0xda65bfbfU, 0x31d7e6e6U, 0xc6844242U, 0xb8d06868U, 0xc3824141U, 0xb0299999U, 0x775a2d2dU, 0x111e0f0fU, 0xcb7bb0b0U, 0xfca85454U, 0xd66dbbbbU, 0x3a2c1616U, }; static const u32 Te2[256] = { 0x63a5c663U, 0x7c84f87cU, 0x7799ee77U, 0x7b8df67bU, 0xf20dfff2U, 0x6bbdd66bU, 0x6fb1de6fU, 0xc55491c5U, 0x30506030U, 0x01030201U, 0x67a9ce67U, 0x2b7d562bU, 0xfe19e7feU, 0xd762b5d7U, 0xabe64dabU, 0x769aec76U, 0xca458fcaU, 0x829d1f82U, 0xc94089c9U, 0x7d87fa7dU, 0xfa15effaU, 0x59ebb259U, 0x47c98e47U, 0xf00bfbf0U, 0xadec41adU, 0xd467b3d4U, 0xa2fd5fa2U, 0xafea45afU, 0x9cbf239cU, 0xa4f753a4U, 0x7296e472U, 0xc05b9bc0U, 0xb7c275b7U, 0xfd1ce1fdU, 0x93ae3d93U, 0x266a4c26U, 0x365a6c36U, 0x3f417e3fU, 0xf702f5f7U, 0xcc4f83ccU, 0x345c6834U, 0xa5f451a5U, 0xe534d1e5U, 0xf108f9f1U, 0x7193e271U, 0xd873abd8U, 0x31536231U, 0x153f2a15U, 0x040c0804U, 0xc75295c7U, 0x23654623U, 0xc35e9dc3U, 0x18283018U, 0x96a13796U, 0x050f0a05U, 0x9ab52f9aU, 0x07090e07U, 0x12362412U, 0x809b1b80U, 0xe23ddfe2U, 0xeb26cdebU, 0x27694e27U, 0xb2cd7fb2U, 0x759fea75U, 0x091b1209U, 0x839e1d83U, 0x2c74582cU, 0x1a2e341aU, 0x1b2d361bU, 0x6eb2dc6eU, 0x5aeeb45aU, 0xa0fb5ba0U, 0x52f6a452U, 0x3b4d763bU, 0xd661b7d6U, 0xb3ce7db3U, 0x297b5229U, 0xe33edde3U, 0x2f715e2fU, 0x84971384U, 0x53f5a653U, 0xd168b9d1U, 0x00000000U, 0xed2cc1edU, 0x20604020U, 0xfc1fe3fcU, 0xb1c879b1U, 0x5bedb65bU, 0x6abed46aU, 0xcb468dcbU, 0xbed967beU, 0x394b7239U, 0x4ade944aU, 0x4cd4984cU, 0x58e8b058U, 0xcf4a85cfU, 0xd06bbbd0U, 0xef2ac5efU, 0xaae54faaU, 0xfb16edfbU, 0x43c58643U, 0x4dd79a4dU, 0x33556633U, 0x85941185U, 0x45cf8a45U, 0xf910e9f9U, 0x02060402U, 0x7f81fe7fU, 0x50f0a050U, 0x3c44783cU, 0x9fba259fU, 0xa8e34ba8U, 0x51f3a251U, 0xa3fe5da3U, 0x40c08040U, 0x8f8a058fU, 0x92ad3f92U, 0x9dbc219dU, 0x38487038U, 0xf504f1f5U, 0xbcdf63bcU, 0xb6c177b6U, 0xda75afdaU, 0x21634221U, 0x10302010U, 0xff1ae5ffU, 0xf30efdf3U, 0xd26dbfd2U, 0xcd4c81cdU, 0x0c14180cU, 0x13352613U, 0xec2fc3ecU, 0x5fe1be5fU, 0x97a23597U, 0x44cc8844U, 0x17392e17U, 0xc45793c4U, 0xa7f255a7U, 0x7e82fc7eU, 0x3d477a3dU, 0x64acc864U, 0x5de7ba5dU, 0x192b3219U, 0x7395e673U, 0x60a0c060U, 0x81981981U, 0x4fd19e4fU, 0xdc7fa3dcU, 0x22664422U, 0x2a7e542aU, 0x90ab3b90U, 0x88830b88U, 0x46ca8c46U, 0xee29c7eeU, 0xb8d36bb8U, 0x143c2814U, 0xde79a7deU, 0x5ee2bc5eU, 0x0b1d160bU, 0xdb76addbU, 0xe03bdbe0U, 0x32566432U, 0x3a4e743aU, 0x0a1e140aU, 0x49db9249U, 0x060a0c06U, 0x246c4824U, 0x5ce4b85cU, 0xc25d9fc2U, 0xd36ebdd3U, 0xacef43acU, 0x62a6c462U, 0x91a83991U, 0x95a43195U, 0xe437d3e4U, 0x798bf279U, 0xe732d5e7U, 0xc8438bc8U, 0x37596e37U, 0x6db7da6dU, 0x8d8c018dU, 0xd564b1d5U, 0x4ed29c4eU, 0xa9e049a9U, 0x6cb4d86cU, 0x56faac56U, 0xf407f3f4U, 0xea25cfeaU, 0x65afca65U, 0x7a8ef47aU, 0xaee947aeU, 0x08181008U, 0xbad56fbaU, 0x7888f078U, 0x256f4a25U, 0x2e725c2eU, 0x1c24381cU, 0xa6f157a6U, 0xb4c773b4U, 0xc65197c6U, 0xe823cbe8U, 0xdd7ca1ddU, 0x749ce874U, 0x1f213e1fU, 0x4bdd964bU, 0xbddc61bdU, 0x8b860d8bU, 0x8a850f8aU, 0x7090e070U, 0x3e427c3eU, 0xb5c471b5U, 0x66aacc66U, 0x48d89048U, 0x03050603U, 0xf601f7f6U, 0x0e121c0eU, 0x61a3c261U, 0x355f6a35U, 0x57f9ae57U, 0xb9d069b9U, 0x86911786U, 0xc15899c1U, 0x1d273a1dU, 0x9eb9279eU, 0xe138d9e1U, 0xf813ebf8U, 0x98b32b98U, 0x11332211U, 0x69bbd269U, 0xd970a9d9U, 0x8e89078eU, 0x94a73394U, 0x9bb62d9bU, 0x1e223c1eU, 0x87921587U, 0xe920c9e9U, 0xce4987ceU, 0x55ffaa55U, 0x28785028U, 0xdf7aa5dfU, 0x8c8f038cU, 0xa1f859a1U, 0x89800989U, 0x0d171a0dU, 0xbfda65bfU, 0xe631d7e6U, 0x42c68442U, 0x68b8d068U, 0x41c38241U, 0x99b02999U, 0x2d775a2dU, 0x0f111e0fU, 0xb0cb7bb0U, 0x54fca854U, 0xbbd66dbbU, 0x163a2c16U, }; static const u32 Te3[256] = { 0x6363a5c6U, 0x7c7c84f8U, 0x777799eeU, 0x7b7b8df6U, 0xf2f20dffU, 0x6b6bbdd6U, 0x6f6fb1deU, 0xc5c55491U, 0x30305060U, 0x01010302U, 0x6767a9ceU, 0x2b2b7d56U, 0xfefe19e7U, 0xd7d762b5U, 0xababe64dU, 0x76769aecU, 0xcaca458fU, 0x82829d1fU, 0xc9c94089U, 0x7d7d87faU, 0xfafa15efU, 0x5959ebb2U, 0x4747c98eU, 0xf0f00bfbU, 0xadadec41U, 0xd4d467b3U, 0xa2a2fd5fU, 0xafafea45U, 0x9c9cbf23U, 0xa4a4f753U, 0x727296e4U, 0xc0c05b9bU, 0xb7b7c275U, 0xfdfd1ce1U, 0x9393ae3dU, 0x26266a4cU, 0x36365a6cU, 0x3f3f417eU, 0xf7f702f5U, 0xcccc4f83U, 0x34345c68U, 0xa5a5f451U, 0xe5e534d1U, 0xf1f108f9U, 0x717193e2U, 0xd8d873abU, 0x31315362U, 0x15153f2aU, 0x04040c08U, 0xc7c75295U, 0x23236546U, 0xc3c35e9dU, 0x18182830U, 0x9696a137U, 0x05050f0aU, 0x9a9ab52fU, 0x0707090eU, 0x12123624U, 0x80809b1bU, 0xe2e23ddfU, 0xebeb26cdU, 0x2727694eU, 0xb2b2cd7fU, 0x75759feaU, 0x09091b12U, 0x83839e1dU, 0x2c2c7458U, 0x1a1a2e34U, 0x1b1b2d36U, 0x6e6eb2dcU, 0x5a5aeeb4U, 0xa0a0fb5bU, 0x5252f6a4U, 0x3b3b4d76U, 0xd6d661b7U, 0xb3b3ce7dU, 0x29297b52U, 0xe3e33eddU, 0x2f2f715eU, 0x84849713U, 0x5353f5a6U, 0xd1d168b9U, 0x00000000U, 0xeded2cc1U, 0x20206040U, 0xfcfc1fe3U, 0xb1b1c879U, 0x5b5bedb6U, 0x6a6abed4U, 0xcbcb468dU, 0xbebed967U, 0x39394b72U, 0x4a4ade94U, 0x4c4cd498U, 0x5858e8b0U, 0xcfcf4a85U, 0xd0d06bbbU, 0xefef2ac5U, 0xaaaae54fU, 0xfbfb16edU, 0x4343c586U, 0x4d4dd79aU, 0x33335566U, 0x85859411U, 0x4545cf8aU, 0xf9f910e9U, 0x02020604U, 0x7f7f81feU, 0x5050f0a0U, 0x3c3c4478U, 0x9f9fba25U, 0xa8a8e34bU, 0x5151f3a2U, 0xa3a3fe5dU, 0x4040c080U, 0x8f8f8a05U, 0x9292ad3fU, 0x9d9dbc21U, 0x38384870U, 0xf5f504f1U, 0xbcbcdf63U, 0xb6b6c177U, 0xdada75afU, 0x21216342U, 0x10103020U, 0xffff1ae5U, 0xf3f30efdU, 0xd2d26dbfU, 0xcdcd4c81U, 0x0c0c1418U, 0x13133526U, 0xecec2fc3U, 0x5f5fe1beU, 0x9797a235U, 0x4444cc88U, 0x1717392eU, 0xc4c45793U, 0xa7a7f255U, 0x7e7e82fcU, 0x3d3d477aU, 0x6464acc8U, 0x5d5de7baU, 0x19192b32U, 0x737395e6U, 0x6060a0c0U, 0x81819819U, 0x4f4fd19eU, 0xdcdc7fa3U, 0x22226644U, 0x2a2a7e54U, 0x9090ab3bU, 0x8888830bU, 0x4646ca8cU, 0xeeee29c7U, 0xb8b8d36bU, 0x14143c28U, 0xdede79a7U, 0x5e5ee2bcU, 0x0b0b1d16U, 0xdbdb76adU, 0xe0e03bdbU, 0x32325664U, 0x3a3a4e74U, 0x0a0a1e14U, 0x4949db92U, 0x06060a0cU, 0x24246c48U, 0x5c5ce4b8U, 0xc2c25d9fU, 0xd3d36ebdU, 0xacacef43U, 0x6262a6c4U, 0x9191a839U, 0x9595a431U, 0xe4e437d3U, 0x79798bf2U, 0xe7e732d5U, 0xc8c8438bU, 0x3737596eU, 0x6d6db7daU, 0x8d8d8c01U, 0xd5d564b1U, 0x4e4ed29cU, 0xa9a9e049U, 0x6c6cb4d8U, 0x5656faacU, 0xf4f407f3U, 0xeaea25cfU, 0x6565afcaU, 0x7a7a8ef4U, 0xaeaee947U, 0x08081810U, 0xbabad56fU, 0x787888f0U, 0x25256f4aU, 0x2e2e725cU, 0x1c1c2438U, 0xa6a6f157U, 0xb4b4c773U, 0xc6c65197U, 0xe8e823cbU, 0xdddd7ca1U, 0x74749ce8U, 0x1f1f213eU, 0x4b4bdd96U, 0xbdbddc61U, 0x8b8b860dU, 0x8a8a850fU, 0x707090e0U, 0x3e3e427cU, 0xb5b5c471U, 0x6666aaccU, 0x4848d890U, 0x03030506U, 0xf6f601f7U, 0x0e0e121cU, 0x6161a3c2U, 0x35355f6aU, 0x5757f9aeU, 0xb9b9d069U, 0x86869117U, 0xc1c15899U, 0x1d1d273aU, 0x9e9eb927U, 0xe1e138d9U, 0xf8f813ebU, 0x9898b32bU, 0x11113322U, 0x6969bbd2U, 0xd9d970a9U, 0x8e8e8907U, 0x9494a733U, 0x9b9bb62dU, 0x1e1e223cU, 0x87879215U, 0xe9e920c9U, 0xcece4987U, 0x5555ffaaU, 0x28287850U, 0xdfdf7aa5U, 0x8c8c8f03U, 0xa1a1f859U, 0x89898009U, 0x0d0d171aU, 0xbfbfda65U, 0xe6e631d7U, 0x4242c684U, 0x6868b8d0U, 0x4141c382U, 0x9999b029U, 0x2d2d775aU, 0x0f0f111eU, 0xb0b0cb7bU, 0x5454fca8U, 0xbbbbd66dU, 0x16163a2cU, }; static const u32 Td0[256] = { 0x51f4a750U, 0x7e416553U, 0x1a17a4c3U, 0x3a275e96U, 0x3bab6bcbU, 0x1f9d45f1U, 0xacfa58abU, 0x4be30393U, 0x2030fa55U, 0xad766df6U, 0x88cc7691U, 0xf5024c25U, 0x4fe5d7fcU, 0xc52acbd7U, 0x26354480U, 0xb562a38fU, 0xdeb15a49U, 0x25ba1b67U, 0x45ea0e98U, 0x5dfec0e1U, 0xc32f7502U, 0x814cf012U, 0x8d4697a3U, 0x6bd3f9c6U, 0x038f5fe7U, 0x15929c95U, 0xbf6d7aebU, 0x955259daU, 0xd4be832dU, 0x587421d3U, 0x49e06929U, 0x8ec9c844U, 0x75c2896aU, 0xf48e7978U, 0x99583e6bU, 0x27b971ddU, 0xbee14fb6U, 0xf088ad17U, 0xc920ac66U, 0x7dce3ab4U, 0x63df4a18U, 0xe51a3182U, 0x97513360U, 0x62537f45U, 0xb16477e0U, 0xbb6bae84U, 0xfe81a01cU, 0xf9082b94U, 0x70486858U, 0x8f45fd19U, 0x94de6c87U, 0x527bf8b7U, 0xab73d323U, 0x724b02e2U, 0xe31f8f57U, 0x6655ab2aU, 0xb2eb2807U, 0x2fb5c203U, 0x86c57b9aU, 0xd33708a5U, 0x302887f2U, 0x23bfa5b2U, 0x02036abaU, 0xed16825cU, 0x8acf1c2bU, 0xa779b492U, 0xf307f2f0U, 0x4e69e2a1U, 0x65daf4cdU, 0x0605bed5U, 0xd134621fU, 0xc4a6fe8aU, 0x342e539dU, 0xa2f355a0U, 0x058ae132U, 0xa4f6eb75U, 0x0b83ec39U, 0x4060efaaU, 0x5e719f06U, 0xbd6e1051U, 0x3e218af9U, 0x96dd063dU, 0xdd3e05aeU, 0x4de6bd46U, 0x91548db5U, 0x71c45d05U, 0x0406d46fU, 0x605015ffU, 0x1998fb24U, 0xd6bde997U, 0x894043ccU, 0x67d99e77U, 0xb0e842bdU, 0x07898b88U, 0xe7195b38U, 0x79c8eedbU, 0xa17c0a47U, 0x7c420fe9U, 0xf8841ec9U, 0x00000000U, 0x09808683U, 0x322bed48U, 0x1e1170acU, 0x6c5a724eU, 0xfd0efffbU, 0x0f853856U, 0x3daed51eU, 0x362d3927U, 0x0a0fd964U, 0x685ca621U, 0x9b5b54d1U, 0x24362e3aU, 0x0c0a67b1U, 0x9357e70fU, 0xb4ee96d2U, 0x1b9b919eU, 0x80c0c54fU, 0x61dc20a2U, 0x5a774b69U, 0x1c121a16U, 0xe293ba0aU, 0xc0a02ae5U, 0x3c22e043U, 0x121b171dU, 0x0e090d0bU, 0xf28bc7adU, 0x2db6a8b9U, 0x141ea9c8U, 0x57f11985U, 0xaf75074cU, 0xee99ddbbU, 0xa37f60fdU, 0xf701269fU, 0x5c72f5bcU, 0x44663bc5U, 0x5bfb7e34U, 0x8b432976U, 0xcb23c6dcU, 0xb6edfc68U, 0xb8e4f163U, 0xd731dccaU, 0x42638510U, 0x13972240U, 0x84c61120U, 0x854a247dU, 0xd2bb3df8U, 0xaef93211U, 0xc729a16dU, 0x1d9e2f4bU, 0xdcb230f3U, 0x0d8652ecU, 0x77c1e3d0U, 0x2bb3166cU, 0xa970b999U, 0x119448faU, 0x47e96422U, 0xa8fc8cc4U, 0xa0f03f1aU, 0x567d2cd8U, 0x223390efU, 0x87494ec7U, 0xd938d1c1U, 0x8ccaa2feU, 0x98d40b36U, 0xa6f581cfU, 0xa57ade28U, 0xdab78e26U, 0x3fadbfa4U, 0x2c3a9de4U, 0x5078920dU, 0x6a5fcc9bU, 0x547e4662U, 0xf68d13c2U, 0x90d8b8e8U, 0x2e39f75eU, 0x82c3aff5U, 0x9f5d80beU, 0x69d0937cU, 0x6fd52da9U, 0xcf2512b3U, 0xc8ac993bU, 0x10187da7U, 0xe89c636eU, 0xdb3bbb7bU, 0xcd267809U, 0x6e5918f4U, 0xec9ab701U, 0x834f9aa8U, 0xe6956e65U, 0xaaffe67eU, 0x21bccf08U, 0xef15e8e6U, 0xbae79bd9U, 0x4a6f36ceU, 0xea9f09d4U, 0x29b07cd6U, 0x31a4b2afU, 0x2a3f2331U, 0xc6a59430U, 0x35a266c0U, 0x744ebc37U, 0xfc82caa6U, 0xe090d0b0U, 0x33a7d815U, 0xf104984aU, 0x41ecdaf7U, 0x7fcd500eU, 0x1791f62fU, 0x764dd68dU, 0x43efb04dU, 0xccaa4d54U, 0xe49604dfU, 0x9ed1b5e3U, 0x4c6a881bU, 0xc12c1fb8U, 0x4665517fU, 0x9d5eea04U, 0x018c355dU, 0xfa877473U, 0xfb0b412eU, 0xb3671d5aU, 0x92dbd252U, 0xe9105633U, 0x6dd64713U, 0x9ad7618cU, 0x37a10c7aU, 0x59f8148eU, 0xeb133c89U, 0xcea927eeU, 0xb761c935U, 0xe11ce5edU, 0x7a47b13cU, 0x9cd2df59U, 0x55f2733fU, 0x1814ce79U, 0x73c737bfU, 0x53f7cdeaU, 0x5ffdaa5bU, 0xdf3d6f14U, 0x7844db86U, 0xcaaff381U, 0xb968c43eU, 0x3824342cU, 0xc2a3405fU, 0x161dc372U, 0xbce2250cU, 0x283c498bU, 0xff0d9541U, 0x39a80171U, 0x080cb3deU, 0xd8b4e49cU, 0x6456c190U, 0x7bcb8461U, 0xd532b670U, 0x486c5c74U, 0xd0b85742U, }; static const u32 Td1[256] = { 0x5051f4a7U, 0x537e4165U, 0xc31a17a4U, 0x963a275eU, 0xcb3bab6bU, 0xf11f9d45U, 0xabacfa58U, 0x934be303U, 0x552030faU, 0xf6ad766dU, 0x9188cc76U, 0x25f5024cU, 0xfc4fe5d7U, 0xd7c52acbU, 0x80263544U, 0x8fb562a3U, 0x49deb15aU, 0x6725ba1bU, 0x9845ea0eU, 0xe15dfec0U, 0x02c32f75U, 0x12814cf0U, 0xa38d4697U, 0xc66bd3f9U, 0xe7038f5fU, 0x9515929cU, 0xebbf6d7aU, 0xda955259U, 0x2dd4be83U, 0xd3587421U, 0x2949e069U, 0x448ec9c8U, 0x6a75c289U, 0x78f48e79U, 0x6b99583eU, 0xdd27b971U, 0xb6bee14fU, 0x17f088adU, 0x66c920acU, 0xb47dce3aU, 0x1863df4aU, 0x82e51a31U, 0x60975133U, 0x4562537fU, 0xe0b16477U, 0x84bb6baeU, 0x1cfe81a0U, 0x94f9082bU, 0x58704868U, 0x198f45fdU, 0x8794de6cU, 0xb7527bf8U, 0x23ab73d3U, 0xe2724b02U, 0x57e31f8fU, 0x2a6655abU, 0x07b2eb28U, 0x032fb5c2U, 0x9a86c57bU, 0xa5d33708U, 0xf2302887U, 0xb223bfa5U, 0xba02036aU, 0x5ced1682U, 0x2b8acf1cU, 0x92a779b4U, 0xf0f307f2U, 0xa14e69e2U, 0xcd65daf4U, 0xd50605beU, 0x1fd13462U, 0x8ac4a6feU, 0x9d342e53U, 0xa0a2f355U, 0x32058ae1U, 0x75a4f6ebU, 0x390b83ecU, 0xaa4060efU, 0x065e719fU, 0x51bd6e10U, 0xf93e218aU, 0x3d96dd06U, 0xaedd3e05U, 0x464de6bdU, 0xb591548dU, 0x0571c45dU, 0x6f0406d4U, 0xff605015U, 0x241998fbU, 0x97d6bde9U, 0xcc894043U, 0x7767d99eU, 0xbdb0e842U, 0x8807898bU, 0x38e7195bU, 0xdb79c8eeU, 0x47a17c0aU, 0xe97c420fU, 0xc9f8841eU, 0x00000000U, 0x83098086U, 0x48322bedU, 0xac1e1170U, 0x4e6c5a72U, 0xfbfd0effU, 0x560f8538U, 0x1e3daed5U, 0x27362d39U, 0x640a0fd9U, 0x21685ca6U, 0xd19b5b54U, 0x3a24362eU, 0xb10c0a67U, 0x0f9357e7U, 0xd2b4ee96U, 0x9e1b9b91U, 0x4f80c0c5U, 0xa261dc20U, 0x695a774bU, 0x161c121aU, 0x0ae293baU, 0xe5c0a02aU, 0x433c22e0U, 0x1d121b17U, 0x0b0e090dU, 0xadf28bc7U, 0xb92db6a8U, 0xc8141ea9U, 0x8557f119U, 0x4caf7507U, 0xbbee99ddU, 0xfda37f60U, 0x9ff70126U, 0xbc5c72f5U, 0xc544663bU, 0x345bfb7eU, 0x768b4329U, 0xdccb23c6U, 0x68b6edfcU, 0x63b8e4f1U, 0xcad731dcU, 0x10426385U, 0x40139722U, 0x2084c611U, 0x7d854a24U, 0xf8d2bb3dU, 0x11aef932U, 0x6dc729a1U, 0x4b1d9e2fU, 0xf3dcb230U, 0xec0d8652U, 0xd077c1e3U, 0x6c2bb316U, 0x99a970b9U, 0xfa119448U, 0x2247e964U, 0xc4a8fc8cU, 0x1aa0f03fU, 0xd8567d2cU, 0xef223390U, 0xc787494eU, 0xc1d938d1U, 0xfe8ccaa2U, 0x3698d40bU, 0xcfa6f581U, 0x28a57adeU, 0x26dab78eU, 0xa43fadbfU, 0xe42c3a9dU, 0x0d507892U, 0x9b6a5fccU, 0x62547e46U, 0xc2f68d13U, 0xe890d8b8U, 0x5e2e39f7U, 0xf582c3afU, 0xbe9f5d80U, 0x7c69d093U, 0xa96fd52dU, 0xb3cf2512U, 0x3bc8ac99U, 0xa710187dU, 0x6ee89c63U, 0x7bdb3bbbU, 0x09cd2678U, 0xf46e5918U, 0x01ec9ab7U, 0xa8834f9aU, 0x65e6956eU, 0x7eaaffe6U, 0x0821bccfU, 0xe6ef15e8U, 0xd9bae79bU, 0xce4a6f36U, 0xd4ea9f09U, 0xd629b07cU, 0xaf31a4b2U, 0x312a3f23U, 0x30c6a594U, 0xc035a266U, 0x37744ebcU, 0xa6fc82caU, 0xb0e090d0U, 0x1533a7d8U, 0x4af10498U, 0xf741ecdaU, 0x0e7fcd50U, 0x2f1791f6U, 0x8d764dd6U, 0x4d43efb0U, 0x54ccaa4dU, 0xdfe49604U, 0xe39ed1b5U, 0x1b4c6a88U, 0xb8c12c1fU, 0x7f466551U, 0x049d5eeaU, 0x5d018c35U, 0x73fa8774U, 0x2efb0b41U, 0x5ab3671dU, 0x5292dbd2U, 0x33e91056U, 0x136dd647U, 0x8c9ad761U, 0x7a37a10cU, 0x8e59f814U, 0x89eb133cU, 0xeecea927U, 0x35b761c9U, 0xede11ce5U, 0x3c7a47b1U, 0x599cd2dfU, 0x3f55f273U, 0x791814ceU, 0xbf73c737U, 0xea53f7cdU, 0x5b5ffdaaU, 0x14df3d6fU, 0x867844dbU, 0x81caaff3U, 0x3eb968c4U, 0x2c382434U, 0x5fc2a340U, 0x72161dc3U, 0x0cbce225U, 0x8b283c49U, 0x41ff0d95U, 0x7139a801U, 0xde080cb3U, 0x9cd8b4e4U, 0x906456c1U, 0x617bcb84U, 0x70d532b6U, 0x74486c5cU, 0x42d0b857U, }; static const u32 Td2[256] = { 0xa75051f4U, 0x65537e41U, 0xa4c31a17U, 0x5e963a27U, 0x6bcb3babU, 0x45f11f9dU, 0x58abacfaU, 0x03934be3U, 0xfa552030U, 0x6df6ad76U, 0x769188ccU, 0x4c25f502U, 0xd7fc4fe5U, 0xcbd7c52aU, 0x44802635U, 0xa38fb562U, 0x5a49deb1U, 0x1b6725baU, 0x0e9845eaU, 0xc0e15dfeU, 0x7502c32fU, 0xf012814cU, 0x97a38d46U, 0xf9c66bd3U, 0x5fe7038fU, 0x9c951592U, 0x7aebbf6dU, 0x59da9552U, 0x832dd4beU, 0x21d35874U, 0x692949e0U, 0xc8448ec9U, 0x896a75c2U, 0x7978f48eU, 0x3e6b9958U, 0x71dd27b9U, 0x4fb6bee1U, 0xad17f088U, 0xac66c920U, 0x3ab47dceU, 0x4a1863dfU, 0x3182e51aU, 0x33609751U, 0x7f456253U, 0x77e0b164U, 0xae84bb6bU, 0xa01cfe81U, 0x2b94f908U, 0x68587048U, 0xfd198f45U, 0x6c8794deU, 0xf8b7527bU, 0xd323ab73U, 0x02e2724bU, 0x8f57e31fU, 0xab2a6655U, 0x2807b2ebU, 0xc2032fb5U, 0x7b9a86c5U, 0x08a5d337U, 0x87f23028U, 0xa5b223bfU, 0x6aba0203U, 0x825ced16U, 0x1c2b8acfU, 0xb492a779U, 0xf2f0f307U, 0xe2a14e69U, 0xf4cd65daU, 0xbed50605U, 0x621fd134U, 0xfe8ac4a6U, 0x539d342eU, 0x55a0a2f3U, 0xe132058aU, 0xeb75a4f6U, 0xec390b83U, 0xefaa4060U, 0x9f065e71U, 0x1051bd6eU, 0x8af93e21U, 0x063d96ddU, 0x05aedd3eU, 0xbd464de6U, 0x8db59154U, 0x5d0571c4U, 0xd46f0406U, 0x15ff6050U, 0xfb241998U, 0xe997d6bdU, 0x43cc8940U, 0x9e7767d9U, 0x42bdb0e8U, 0x8b880789U, 0x5b38e719U, 0xeedb79c8U, 0x0a47a17cU, 0x0fe97c42U, 0x1ec9f884U, 0x00000000U, 0x86830980U, 0xed48322bU, 0x70ac1e11U, 0x724e6c5aU, 0xfffbfd0eU, 0x38560f85U, 0xd51e3daeU, 0x3927362dU, 0xd9640a0fU, 0xa621685cU, 0x54d19b5bU, 0x2e3a2436U, 0x67b10c0aU, 0xe70f9357U, 0x96d2b4eeU, 0x919e1b9bU, 0xc54f80c0U, 0x20a261dcU, 0x4b695a77U, 0x1a161c12U, 0xba0ae293U, 0x2ae5c0a0U, 0xe0433c22U, 0x171d121bU, 0x0d0b0e09U, 0xc7adf28bU, 0xa8b92db6U, 0xa9c8141eU, 0x198557f1U, 0x074caf75U, 0xddbbee99U, 0x60fda37fU, 0x269ff701U, 0xf5bc5c72U, 0x3bc54466U, 0x7e345bfbU, 0x29768b43U, 0xc6dccb23U, 0xfc68b6edU, 0xf163b8e4U, 0xdccad731U, 0x85104263U, 0x22401397U, 0x112084c6U, 0x247d854aU, 0x3df8d2bbU, 0x3211aef9U, 0xa16dc729U, 0x2f4b1d9eU, 0x30f3dcb2U, 0x52ec0d86U, 0xe3d077c1U, 0x166c2bb3U, 0xb999a970U, 0x48fa1194U, 0x642247e9U, 0x8cc4a8fcU, 0x3f1aa0f0U, 0x2cd8567dU, 0x90ef2233U, 0x4ec78749U, 0xd1c1d938U, 0xa2fe8ccaU, 0x0b3698d4U, 0x81cfa6f5U, 0xde28a57aU, 0x8e26dab7U, 0xbfa43fadU, 0x9de42c3aU, 0x920d5078U, 0xcc9b6a5fU, 0x4662547eU, 0x13c2f68dU, 0xb8e890d8U, 0xf75e2e39U, 0xaff582c3U, 0x80be9f5dU, 0x937c69d0U, 0x2da96fd5U, 0x12b3cf25U, 0x993bc8acU, 0x7da71018U, 0x636ee89cU, 0xbb7bdb3bU, 0x7809cd26U, 0x18f46e59U, 0xb701ec9aU, 0x9aa8834fU, 0x6e65e695U, 0xe67eaaffU, 0xcf0821bcU, 0xe8e6ef15U, 0x9bd9bae7U, 0x36ce4a6fU, 0x09d4ea9fU, 0x7cd629b0U, 0xb2af31a4U, 0x23312a3fU, 0x9430c6a5U, 0x66c035a2U, 0xbc37744eU, 0xcaa6fc82U, 0xd0b0e090U, 0xd81533a7U, 0x984af104U, 0xdaf741ecU, 0x500e7fcdU, 0xf62f1791U, 0xd68d764dU, 0xb04d43efU, 0x4d54ccaaU, 0x04dfe496U, 0xb5e39ed1U, 0x881b4c6aU, 0x1fb8c12cU, 0x517f4665U, 0xea049d5eU, 0x355d018cU, 0x7473fa87U, 0x412efb0bU, 0x1d5ab367U, 0xd25292dbU, 0x5633e910U, 0x47136dd6U, 0x618c9ad7U, 0x0c7a37a1U, 0x148e59f8U, 0x3c89eb13U, 0x27eecea9U, 0xc935b761U, 0xe5ede11cU, 0xb13c7a47U, 0xdf599cd2U, 0x733f55f2U, 0xce791814U, 0x37bf73c7U, 0xcdea53f7U, 0xaa5b5ffdU, 0x6f14df3dU, 0xdb867844U, 0xf381caafU, 0xc43eb968U, 0x342c3824U, 0x405fc2a3U, 0xc372161dU, 0x250cbce2U, 0x498b283cU, 0x9541ff0dU, 0x017139a8U, 0xb3de080cU, 0xe49cd8b4U, 0xc1906456U, 0x84617bcbU, 0xb670d532U, 0x5c74486cU, 0x5742d0b8U, }; static const u32 Td3[256] = { 0xf4a75051U, 0x4165537eU, 0x17a4c31aU, 0x275e963aU, 0xab6bcb3bU, 0x9d45f11fU, 0xfa58abacU, 0xe303934bU, 0x30fa5520U, 0x766df6adU, 0xcc769188U, 0x024c25f5U, 0xe5d7fc4fU, 0x2acbd7c5U, 0x35448026U, 0x62a38fb5U, 0xb15a49deU, 0xba1b6725U, 0xea0e9845U, 0xfec0e15dU, 0x2f7502c3U, 0x4cf01281U, 0x4697a38dU, 0xd3f9c66bU, 0x8f5fe703U, 0x929c9515U, 0x6d7aebbfU, 0x5259da95U, 0xbe832dd4U, 0x7421d358U, 0xe0692949U, 0xc9c8448eU, 0xc2896a75U, 0x8e7978f4U, 0x583e6b99U, 0xb971dd27U, 0xe14fb6beU, 0x88ad17f0U, 0x20ac66c9U, 0xce3ab47dU, 0xdf4a1863U, 0x1a3182e5U, 0x51336097U, 0x537f4562U, 0x6477e0b1U, 0x6bae84bbU, 0x81a01cfeU, 0x082b94f9U, 0x48685870U, 0x45fd198fU, 0xde6c8794U, 0x7bf8b752U, 0x73d323abU, 0x4b02e272U, 0x1f8f57e3U, 0x55ab2a66U, 0xeb2807b2U, 0xb5c2032fU, 0xc57b9a86U, 0x3708a5d3U, 0x2887f230U, 0xbfa5b223U, 0x036aba02U, 0x16825cedU, 0xcf1c2b8aU, 0x79b492a7U, 0x07f2f0f3U, 0x69e2a14eU, 0xdaf4cd65U, 0x05bed506U, 0x34621fd1U, 0xa6fe8ac4U, 0x2e539d34U, 0xf355a0a2U, 0x8ae13205U, 0xf6eb75a4U, 0x83ec390bU, 0x60efaa40U, 0x719f065eU, 0x6e1051bdU, 0x218af93eU, 0xdd063d96U, 0x3e05aeddU, 0xe6bd464dU, 0x548db591U, 0xc45d0571U, 0x06d46f04U, 0x5015ff60U, 0x98fb2419U, 0xbde997d6U, 0x4043cc89U, 0xd99e7767U, 0xe842bdb0U, 0x898b8807U, 0x195b38e7U, 0xc8eedb79U, 0x7c0a47a1U, 0x420fe97cU, 0x841ec9f8U, 0x00000000U, 0x80868309U, 0x2bed4832U, 0x1170ac1eU, 0x5a724e6cU, 0x0efffbfdU, 0x8538560fU, 0xaed51e3dU, 0x2d392736U, 0x0fd9640aU, 0x5ca62168U, 0x5b54d19bU, 0x362e3a24U, 0x0a67b10cU, 0x57e70f93U, 0xee96d2b4U, 0x9b919e1bU, 0xc0c54f80U, 0xdc20a261U, 0x774b695aU, 0x121a161cU, 0x93ba0ae2U, 0xa02ae5c0U, 0x22e0433cU, 0x1b171d12U, 0x090d0b0eU, 0x8bc7adf2U, 0xb6a8b92dU, 0x1ea9c814U, 0xf1198557U, 0x75074cafU, 0x99ddbbeeU, 0x7f60fda3U, 0x01269ff7U, 0x72f5bc5cU, 0x663bc544U, 0xfb7e345bU, 0x4329768bU, 0x23c6dccbU, 0xedfc68b6U, 0xe4f163b8U, 0x31dccad7U, 0x63851042U, 0x97224013U, 0xc6112084U, 0x4a247d85U, 0xbb3df8d2U, 0xf93211aeU, 0x29a16dc7U, 0x9e2f4b1dU, 0xb230f3dcU, 0x8652ec0dU, 0xc1e3d077U, 0xb3166c2bU, 0x70b999a9U, 0x9448fa11U, 0xe9642247U, 0xfc8cc4a8U, 0xf03f1aa0U, 0x7d2cd856U, 0x3390ef22U, 0x494ec787U, 0x38d1c1d9U, 0xcaa2fe8cU, 0xd40b3698U, 0xf581cfa6U, 0x7ade28a5U, 0xb78e26daU, 0xadbfa43fU, 0x3a9de42cU, 0x78920d50U, 0x5fcc9b6aU, 0x7e466254U, 0x8d13c2f6U, 0xd8b8e890U, 0x39f75e2eU, 0xc3aff582U, 0x5d80be9fU, 0xd0937c69U, 0xd52da96fU, 0x2512b3cfU, 0xac993bc8U, 0x187da710U, 0x9c636ee8U, 0x3bbb7bdbU, 0x267809cdU, 0x5918f46eU, 0x9ab701ecU, 0x4f9aa883U, 0x956e65e6U, 0xffe67eaaU, 0xbccf0821U, 0x15e8e6efU, 0xe79bd9baU, 0x6f36ce4aU, 0x9f09d4eaU, 0xb07cd629U, 0xa4b2af31U, 0x3f23312aU, 0xa59430c6U, 0xa266c035U, 0x4ebc3774U, 0x82caa6fcU, 0x90d0b0e0U, 0xa7d81533U, 0x04984af1U, 0xecdaf741U, 0xcd500e7fU, 0x91f62f17U, 0x4dd68d76U, 0xefb04d43U, 0xaa4d54ccU, 0x9604dfe4U, 0xd1b5e39eU, 0x6a881b4cU, 0x2c1fb8c1U, 0x65517f46U, 0x5eea049dU, 0x8c355d01U, 0x877473faU, 0x0b412efbU, 0x671d5ab3U, 0xdbd25292U, 0x105633e9U, 0xd647136dU, 0xd7618c9aU, 0xa10c7a37U, 0xf8148e59U, 0x133c89ebU, 0xa927eeceU, 0x61c935b7U, 0x1ce5ede1U, 0x47b13c7aU, 0xd2df599cU, 0xf2733f55U, 0x14ce7918U, 0xc737bf73U, 0xf7cdea53U, 0xfdaa5b5fU, 0x3d6f14dfU, 0x44db8678U, 0xaff381caU, 0x68c43eb9U, 0x24342c38U, 0xa3405fc2U, 0x1dc37216U, 0xe2250cbcU, 0x3c498b28U, 0x0d9541ffU, 0xa8017139U, 0x0cb3de08U, 0xb4e49cd8U, 0x56c19064U, 0xcb84617bU, 0x32b670d5U, 0x6c5c7448U, 0xb85742d0U, }; static const u8 Td4[256] = { 0x52U, 0x09U, 0x6aU, 0xd5U, 0x30U, 0x36U, 0xa5U, 0x38U, 0xbfU, 0x40U, 0xa3U, 0x9eU, 0x81U, 0xf3U, 0xd7U, 0xfbU, 0x7cU, 0xe3U, 0x39U, 0x82U, 0x9bU, 0x2fU, 0xffU, 0x87U, 0x34U, 0x8eU, 0x43U, 0x44U, 0xc4U, 0xdeU, 0xe9U, 0xcbU, 0x54U, 0x7bU, 0x94U, 0x32U, 0xa6U, 0xc2U, 0x23U, 0x3dU, 0xeeU, 0x4cU, 0x95U, 0x0bU, 0x42U, 0xfaU, 0xc3U, 0x4eU, 0x08U, 0x2eU, 0xa1U, 0x66U, 0x28U, 0xd9U, 0x24U, 0xb2U, 0x76U, 0x5bU, 0xa2U, 0x49U, 0x6dU, 0x8bU, 0xd1U, 0x25U, 0x72U, 0xf8U, 0xf6U, 0x64U, 0x86U, 0x68U, 0x98U, 0x16U, 0xd4U, 0xa4U, 0x5cU, 0xccU, 0x5dU, 0x65U, 0xb6U, 0x92U, 0x6cU, 0x70U, 0x48U, 0x50U, 0xfdU, 0xedU, 0xb9U, 0xdaU, 0x5eU, 0x15U, 0x46U, 0x57U, 0xa7U, 0x8dU, 0x9dU, 0x84U, 0x90U, 0xd8U, 0xabU, 0x00U, 0x8cU, 0xbcU, 0xd3U, 0x0aU, 0xf7U, 0xe4U, 0x58U, 0x05U, 0xb8U, 0xb3U, 0x45U, 0x06U, 0xd0U, 0x2cU, 0x1eU, 0x8fU, 0xcaU, 0x3fU, 0x0fU, 0x02U, 0xc1U, 0xafU, 0xbdU, 0x03U, 0x01U, 0x13U, 0x8aU, 0x6bU, 0x3aU, 0x91U, 0x11U, 0x41U, 0x4fU, 0x67U, 0xdcU, 0xeaU, 0x97U, 0xf2U, 0xcfU, 0xceU, 0xf0U, 0xb4U, 0xe6U, 0x73U, 0x96U, 0xacU, 0x74U, 0x22U, 0xe7U, 0xadU, 0x35U, 0x85U, 0xe2U, 0xf9U, 0x37U, 0xe8U, 0x1cU, 0x75U, 0xdfU, 0x6eU, 0x47U, 0xf1U, 0x1aU, 0x71U, 0x1dU, 0x29U, 0xc5U, 0x89U, 0x6fU, 0xb7U, 0x62U, 0x0eU, 0xaaU, 0x18U, 0xbeU, 0x1bU, 0xfcU, 0x56U, 0x3eU, 0x4bU, 0xc6U, 0xd2U, 0x79U, 0x20U, 0x9aU, 0xdbU, 0xc0U, 0xfeU, 0x78U, 0xcdU, 0x5aU, 0xf4U, 0x1fU, 0xddU, 0xa8U, 0x33U, 0x88U, 0x07U, 0xc7U, 0x31U, 0xb1U, 0x12U, 0x10U, 0x59U, 0x27U, 0x80U, 0xecU, 0x5fU, 0x60U, 0x51U, 0x7fU, 0xa9U, 0x19U, 0xb5U, 0x4aU, 0x0dU, 0x2dU, 0xe5U, 0x7aU, 0x9fU, 0x93U, 0xc9U, 0x9cU, 0xefU, 0xa0U, 0xe0U, 0x3bU, 0x4dU, 0xaeU, 0x2aU, 0xf5U, 0xb0U, 0xc8U, 0xebU, 0xbbU, 0x3cU, 0x83U, 0x53U, 0x99U, 0x61U, 0x17U, 0x2bU, 0x04U, 0x7eU, 0xbaU, 0x77U, 0xd6U, 0x26U, 0xe1U, 0x69U, 0x14U, 0x63U, 0x55U, 0x21U, 0x0cU, 0x7dU, }; static const u32 rcon[] = { 0x01000000, 0x02000000, 0x04000000, 0x08000000, 0x10000000, 0x20000000, 0x40000000, 0x80000000, 0x1B000000, 0x36000000, }; int AES_set_encrypt_key(const unsigned char *userKey, const int bits, AES_KEY *key) { u32 *rk; int i = 0; u32 temp; if (!userKey || !key) return -1; if (bits != 128 && bits != 192 && bits != 256) return -2; rk = key->rd_key; if (bits == 128) key->rounds = 10; else if (bits == 192) key->rounds = 12; else key->rounds = 14; rk[0] = GETU32(userKey ); rk[1] = GETU32(userKey + 4); rk[2] = GETU32(userKey + 8); rk[3] = GETU32(userKey + 12); if (bits == 128) { while (1) { temp = rk[3]; rk[4] = rk[0] ^ (Te2[(temp >> 16) & 0xff] & 0xff000000) ^ (Te3[(temp >> 8) & 0xff] & 0x00ff0000) ^ (Te0[(temp ) & 0xff] & 0x0000ff00) ^ (Te1[(temp >> 24) ] & 0x000000ff) ^ rcon[i]; rk[5] = rk[1] ^ rk[4]; rk[6] = rk[2] ^ rk[5]; rk[7] = rk[3] ^ rk[6]; if (++i == 10) { return 0; } rk += 4; } } rk[4] = GETU32(userKey + 16); rk[5] = GETU32(userKey + 20); if (bits == 192) { while (1) { temp = rk[ 5]; rk[ 6] = rk[ 0] ^ (Te2[(temp >> 16) & 0xff] & 0xff000000) ^ (Te3[(temp >> 8) & 0xff] & 0x00ff0000) ^ (Te0[(temp ) & 0xff] & 0x0000ff00) ^ (Te1[(temp >> 24) ] & 0x000000ff) ^ rcon[i]; rk[ 7] = rk[ 1] ^ rk[ 6]; rk[ 8] = rk[ 2] ^ rk[ 7]; rk[ 9] = rk[ 3] ^ rk[ 8]; if (++i == 8) { return 0; } rk[10] = rk[ 4] ^ rk[ 9]; rk[11] = rk[ 5] ^ rk[10]; rk += 6; } } rk[6] = GETU32(userKey + 24); rk[7] = GETU32(userKey + 28); if (bits == 256) { while (1) { temp = rk[ 7]; rk[ 8] = rk[ 0] ^ (Te2[(temp >> 16) & 0xff] & 0xff000000) ^ (Te3[(temp >> 8) & 0xff] & 0x00ff0000) ^ (Te0[(temp ) & 0xff] & 0x0000ff00) ^ (Te1[(temp >> 24) ] & 0x000000ff) ^ rcon[i]; rk[ 9] = rk[ 1] ^ rk[ 8]; rk[10] = rk[ 2] ^ rk[ 9]; rk[11] = rk[ 3] ^ rk[10]; if (++i == 7) { return 0; } temp = rk[11]; rk[12] = rk[ 4] ^ (Te2[(temp >> 24) ] & 0xff000000) ^ (Te3[(temp >> 16) & 0xff] & 0x00ff0000) ^ (Te0[(temp >> 8) & 0xff] & 0x0000ff00) ^ (Te1[(temp ) & 0xff] & 0x000000ff); rk[13] = rk[ 5] ^ rk[12]; rk[14] = rk[ 6] ^ rk[13]; rk[15] = rk[ 7] ^ rk[14]; rk += 8; } } return 0; } int AES_set_decrypt_key(const unsigned char *userKey, const int bits, AES_KEY *key) { u32 *rk; int i, j, status; u32 temp; status = AES_set_encrypt_key(userKey, bits, key); if (status < 0) return status; rk = key->rd_key; for (i = 0, j = 4*(key->rounds); i < j; i += 4, j -= 4) { temp = rk[i ]; rk[i ] = rk[j ]; rk[j ] = temp; temp = rk[i + 1]; rk[i + 1] = rk[j + 1]; rk[j + 1] = temp; temp = rk[i + 2]; rk[i + 2] = rk[j + 2]; rk[j + 2] = temp; temp = rk[i + 3]; rk[i + 3] = rk[j + 3]; rk[j + 3] = temp; } for (i = 1; i < (key->rounds); i++) { rk += 4; rk[0] = Td0[Te1[(rk[0] >> 24) ] & 0xff] ^ Td1[Te1[(rk[0] >> 16) & 0xff] & 0xff] ^ Td2[Te1[(rk[0] >> 8) & 0xff] & 0xff] ^ Td3[Te1[(rk[0] ) & 0xff] & 0xff]; rk[1] = Td0[Te1[(rk[1] >> 24) ] & 0xff] ^ Td1[Te1[(rk[1] >> 16) & 0xff] & 0xff] ^ Td2[Te1[(rk[1] >> 8) & 0xff] & 0xff] ^ Td3[Te1[(rk[1] ) & 0xff] & 0xff]; rk[2] = Td0[Te1[(rk[2] >> 24) ] & 0xff] ^ Td1[Te1[(rk[2] >> 16) & 0xff] & 0xff] ^ Td2[Te1[(rk[2] >> 8) & 0xff] & 0xff] ^ Td3[Te1[(rk[2] ) & 0xff] & 0xff]; rk[3] = Td0[Te1[(rk[3] >> 24) ] & 0xff] ^ Td1[Te1[(rk[3] >> 16) & 0xff] & 0xff] ^ Td2[Te1[(rk[3] >> 8) & 0xff] & 0xff] ^ Td3[Te1[(rk[3] ) & 0xff] & 0xff]; } return 0; } void AES_encrypt(const unsigned char *in, unsigned char *out, const AES_KEY *key) { const u32 *rk; u32 s0, s1, s2, s3, t0, t1, t2, t3; #ifndef FULL_UNROLL int r; #endif assert(in && out && key); rk = key->rd_key; s0 = GETU32(in ) ^ rk[0]; s1 = GETU32(in + 4) ^ rk[1]; s2 = GETU32(in + 8) ^ rk[2]; s3 = GETU32(in + 12) ^ rk[3]; #ifdef FULL_UNROLL t0 = Te0[s0 >> 24] ^ Te1[(s1 >> 16) & 0xff] ^ Te2[(s2 >> 8) & 0xff] ^ Te3[s3 & 0xff] ^ rk[ 4]; t1 = Te0[s1 >> 24] ^ Te1[(s2 >> 16) & 0xff] ^ Te2[(s3 >> 8) & 0xff] ^ Te3[s0 & 0xff] ^ rk[ 5]; t2 = Te0[s2 >> 24] ^ Te1[(s3 >> 16) & 0xff] ^ Te2[(s0 >> 8) & 0xff] ^ Te3[s1 & 0xff] ^ rk[ 6]; t3 = Te0[s3 >> 24] ^ Te1[(s0 >> 16) & 0xff] ^ Te2[(s1 >> 8) & 0xff] ^ Te3[s2 & 0xff] ^ rk[ 7]; s0 = Te0[t0 >> 24] ^ Te1[(t1 >> 16) & 0xff] ^ Te2[(t2 >> 8) & 0xff] ^ Te3[t3 & 0xff] ^ rk[ 8]; s1 = Te0[t1 >> 24] ^ Te1[(t2 >> 16) & 0xff] ^ Te2[(t3 >> 8) & 0xff] ^ Te3[t0 & 0xff] ^ rk[ 9]; s2 = Te0[t2 >> 24] ^ Te1[(t3 >> 16) & 0xff] ^ Te2[(t0 >> 8) & 0xff] ^ Te3[t1 & 0xff] ^ rk[10]; s3 = Te0[t3 >> 24] ^ Te1[(t0 >> 16) & 0xff] ^ Te2[(t1 >> 8) & 0xff] ^ Te3[t2 & 0xff] ^ rk[11]; t0 = Te0[s0 >> 24] ^ Te1[(s1 >> 16) & 0xff] ^ Te2[(s2 >> 8) & 0xff] ^ Te3[s3 & 0xff] ^ rk[12]; t1 = Te0[s1 >> 24] ^ Te1[(s2 >> 16) & 0xff] ^ Te2[(s3 >> 8) & 0xff] ^ Te3[s0 & 0xff] ^ rk[13]; t2 = Te0[s2 >> 24] ^ Te1[(s3 >> 16) & 0xff] ^ Te2[(s0 >> 8) & 0xff] ^ Te3[s1 & 0xff] ^ rk[14]; t3 = Te0[s3 >> 24] ^ Te1[(s0 >> 16) & 0xff] ^ Te2[(s1 >> 8) & 0xff] ^ Te3[s2 & 0xff] ^ rk[15]; s0 = Te0[t0 >> 24] ^ Te1[(t1 >> 16) & 0xff] ^ Te2[(t2 >> 8) & 0xff] ^ Te3[t3 & 0xff] ^ rk[16]; s1 = Te0[t1 >> 24] ^ Te1[(t2 >> 16) & 0xff] ^ Te2[(t3 >> 8) & 0xff] ^ Te3[t0 & 0xff] ^ rk[17]; s2 = Te0[t2 >> 24] ^ Te1[(t3 >> 16) & 0xff] ^ Te2[(t0 >> 8) & 0xff] ^ Te3[t1 & 0xff] ^ rk[18]; s3 = Te0[t3 >> 24] ^ Te1[(t0 >> 16) & 0xff] ^ Te2[(t1 >> 8) & 0xff] ^ Te3[t2 & 0xff] ^ rk[19]; t0 = Te0[s0 >> 24] ^ Te1[(s1 >> 16) & 0xff] ^ Te2[(s2 >> 8) & 0xff] ^ Te3[s3 & 0xff] ^ rk[20]; t1 = Te0[s1 >> 24] ^ Te1[(s2 >> 16) & 0xff] ^ Te2[(s3 >> 8) & 0xff] ^ Te3[s0 & 0xff] ^ rk[21]; t2 = Te0[s2 >> 24] ^ Te1[(s3 >> 16) & 0xff] ^ Te2[(s0 >> 8) & 0xff] ^ Te3[s1 & 0xff] ^ rk[22]; t3 = Te0[s3 >> 24] ^ Te1[(s0 >> 16) & 0xff] ^ Te2[(s1 >> 8) & 0xff] ^ Te3[s2 & 0xff] ^ rk[23]; s0 = Te0[t0 >> 24] ^ Te1[(t1 >> 16) & 0xff] ^ Te2[(t2 >> 8) & 0xff] ^ Te3[t3 & 0xff] ^ rk[24]; s1 = Te0[t1 >> 24] ^ Te1[(t2 >> 16) & 0xff] ^ Te2[(t3 >> 8) & 0xff] ^ Te3[t0 & 0xff] ^ rk[25]; s2 = Te0[t2 >> 24] ^ Te1[(t3 >> 16) & 0xff] ^ Te2[(t0 >> 8) & 0xff] ^ Te3[t1 & 0xff] ^ rk[26]; s3 = Te0[t3 >> 24] ^ Te1[(t0 >> 16) & 0xff] ^ Te2[(t1 >> 8) & 0xff] ^ Te3[t2 & 0xff] ^ rk[27]; t0 = Te0[s0 >> 24] ^ Te1[(s1 >> 16) & 0xff] ^ Te2[(s2 >> 8) & 0xff] ^ Te3[s3 & 0xff] ^ rk[28]; t1 = Te0[s1 >> 24] ^ Te1[(s2 >> 16) & 0xff] ^ Te2[(s3 >> 8) & 0xff] ^ Te3[s0 & 0xff] ^ rk[29]; t2 = Te0[s2 >> 24] ^ Te1[(s3 >> 16) & 0xff] ^ Te2[(s0 >> 8) & 0xff] ^ Te3[s1 & 0xff] ^ rk[30]; t3 = Te0[s3 >> 24] ^ Te1[(s0 >> 16) & 0xff] ^ Te2[(s1 >> 8) & 0xff] ^ Te3[s2 & 0xff] ^ rk[31]; s0 = Te0[t0 >> 24] ^ Te1[(t1 >> 16) & 0xff] ^ Te2[(t2 >> 8) & 0xff] ^ Te3[t3 & 0xff] ^ rk[32]; s1 = Te0[t1 >> 24] ^ Te1[(t2 >> 16) & 0xff] ^ Te2[(t3 >> 8) & 0xff] ^ Te3[t0 & 0xff] ^ rk[33]; s2 = Te0[t2 >> 24] ^ Te1[(t3 >> 16) & 0xff] ^ Te2[(t0 >> 8) & 0xff] ^ Te3[t1 & 0xff] ^ rk[34]; s3 = Te0[t3 >> 24] ^ Te1[(t0 >> 16) & 0xff] ^ Te2[(t1 >> 8) & 0xff] ^ Te3[t2 & 0xff] ^ rk[35]; t0 = Te0[s0 >> 24] ^ Te1[(s1 >> 16) & 0xff] ^ Te2[(s2 >> 8) & 0xff] ^ Te3[s3 & 0xff] ^ rk[36]; t1 = Te0[s1 >> 24] ^ Te1[(s2 >> 16) & 0xff] ^ Te2[(s3 >> 8) & 0xff] ^ Te3[s0 & 0xff] ^ rk[37]; t2 = Te0[s2 >> 24] ^ Te1[(s3 >> 16) & 0xff] ^ Te2[(s0 >> 8) & 0xff] ^ Te3[s1 & 0xff] ^ rk[38]; t3 = Te0[s3 >> 24] ^ Te1[(s0 >> 16) & 0xff] ^ Te2[(s1 >> 8) & 0xff] ^ Te3[s2 & 0xff] ^ rk[39]; if (key->rounds > 10) { s0 = Te0[t0 >> 24] ^ Te1[(t1 >> 16) & 0xff] ^ Te2[(t2 >> 8) & 0xff] ^ Te3[t3 & 0xff] ^ rk[40]; s1 = Te0[t1 >> 24] ^ Te1[(t2 >> 16) & 0xff] ^ Te2[(t3 >> 8) & 0xff] ^ Te3[t0 & 0xff] ^ rk[41]; s2 = Te0[t2 >> 24] ^ Te1[(t3 >> 16) & 0xff] ^ Te2[(t0 >> 8) & 0xff] ^ Te3[t1 & 0xff] ^ rk[42]; s3 = Te0[t3 >> 24] ^ Te1[(t0 >> 16) & 0xff] ^ Te2[(t1 >> 8) & 0xff] ^ Te3[t2 & 0xff] ^ rk[43]; t0 = Te0[s0 >> 24] ^ Te1[(s1 >> 16) & 0xff] ^ Te2[(s2 >> 8) & 0xff] ^ Te3[s3 & 0xff] ^ rk[44]; t1 = Te0[s1 >> 24] ^ Te1[(s2 >> 16) & 0xff] ^ Te2[(s3 >> 8) & 0xff] ^ Te3[s0 & 0xff] ^ rk[45]; t2 = Te0[s2 >> 24] ^ Te1[(s3 >> 16) & 0xff] ^ Te2[(s0 >> 8) & 0xff] ^ Te3[s1 & 0xff] ^ rk[46]; t3 = Te0[s3 >> 24] ^ Te1[(s0 >> 16) & 0xff] ^ Te2[(s1 >> 8) & 0xff] ^ Te3[s2 & 0xff] ^ rk[47]; if (key->rounds > 12) { s0 = Te0[t0 >> 24] ^ Te1[(t1 >> 16) & 0xff] ^ Te2[(t2 >> 8) & 0xff] ^ Te3[t3 & 0xff] ^ rk[48]; s1 = Te0[t1 >> 24] ^ Te1[(t2 >> 16) & 0xff] ^ Te2[(t3 >> 8) & 0xff] ^ Te3[t0 & 0xff] ^ rk[49]; s2 = Te0[t2 >> 24] ^ Te1[(t3 >> 16) & 0xff] ^ Te2[(t0 >> 8) & 0xff] ^ Te3[t1 & 0xff] ^ rk[50]; s3 = Te0[t3 >> 24] ^ Te1[(t0 >> 16) & 0xff] ^ Te2[(t1 >> 8) & 0xff] ^ Te3[t2 & 0xff] ^ rk[51]; t0 = Te0[s0 >> 24] ^ Te1[(s1 >> 16) & 0xff] ^ Te2[(s2 >> 8) & 0xff] ^ Te3[s3 & 0xff] ^ rk[52]; t1 = Te0[s1 >> 24] ^ Te1[(s2 >> 16) & 0xff] ^ Te2[(s3 >> 8) & 0xff] ^ Te3[s0 & 0xff] ^ rk[53]; t2 = Te0[s2 >> 24] ^ Te1[(s3 >> 16) & 0xff] ^ Te2[(s0 >> 8) & 0xff] ^ Te3[s1 & 0xff] ^ rk[54]; t3 = Te0[s3 >> 24] ^ Te1[(s0 >> 16) & 0xff] ^ Te2[(s1 >> 8) & 0xff] ^ Te3[s2 & 0xff] ^ rk[55]; } } rk += key->rounds << 2; #else r = key->rounds >> 1; for (;;) { t0 = Te0[(s0 >> 24) ] ^ Te1[(s1 >> 16) & 0xff] ^ Te2[(s2 >> 8) & 0xff] ^ Te3[(s3 ) & 0xff] ^ rk[4]; t1 = Te0[(s1 >> 24) ] ^ Te1[(s2 >> 16) & 0xff] ^ Te2[(s3 >> 8) & 0xff] ^ Te3[(s0 ) & 0xff] ^ rk[5]; t2 = Te0[(s2 >> 24) ] ^ Te1[(s3 >> 16) & 0xff] ^ Te2[(s0 >> 8) & 0xff] ^ Te3[(s1 ) & 0xff] ^ rk[6]; t3 = Te0[(s3 >> 24) ] ^ Te1[(s0 >> 16) & 0xff] ^ Te2[(s1 >> 8) & 0xff] ^ Te3[(s2 ) & 0xff] ^ rk[7]; rk += 8; if (--r == 0) { break; } s0 = Te0[(t0 >> 24) ] ^ Te1[(t1 >> 16) & 0xff] ^ Te2[(t2 >> 8) & 0xff] ^ Te3[(t3 ) & 0xff] ^ rk[0]; s1 = Te0[(t1 >> 24) ] ^ Te1[(t2 >> 16) & 0xff] ^ Te2[(t3 >> 8) & 0xff] ^ Te3[(t0 ) & 0xff] ^ rk[1]; s2 = Te0[(t2 >> 24) ] ^ Te1[(t3 >> 16) & 0xff] ^ Te2[(t0 >> 8) & 0xff] ^ Te3[(t1 ) & 0xff] ^ rk[2]; s3 = Te0[(t3 >> 24) ] ^ Te1[(t0 >> 16) & 0xff] ^ Te2[(t1 >> 8) & 0xff] ^ Te3[(t2 ) & 0xff] ^ rk[3]; } #endif s0 = (Te2[(t0 >> 24) ] & 0xff000000) ^ (Te3[(t1 >> 16) & 0xff] & 0x00ff0000) ^ (Te0[(t2 >> 8) & 0xff] & 0x0000ff00) ^ (Te1[(t3 ) & 0xff] & 0x000000ff) ^ rk[0]; PUTU32(out , s0); s1 = (Te2[(t1 >> 24) ] & 0xff000000) ^ (Te3[(t2 >> 16) & 0xff] & 0x00ff0000) ^ (Te0[(t3 >> 8) & 0xff] & 0x0000ff00) ^ (Te1[(t0 ) & 0xff] & 0x000000ff) ^ rk[1]; PUTU32(out + 4, s1); s2 = (Te2[(t2 >> 24) ] & 0xff000000) ^ (Te3[(t3 >> 16) & 0xff] & 0x00ff0000) ^ (Te0[(t0 >> 8) & 0xff] & 0x0000ff00) ^ (Te1[(t1 ) & 0xff] & 0x000000ff) ^ rk[2]; PUTU32(out + 8, s2); s3 = (Te2[(t3 >> 24) ] & 0xff000000) ^ (Te3[(t0 >> 16) & 0xff] & 0x00ff0000) ^ (Te0[(t1 >> 8) & 0xff] & 0x0000ff00) ^ (Te1[(t2 ) & 0xff] & 0x000000ff) ^ rk[3]; PUTU32(out + 12, s3); } void AES_decrypt(const unsigned char *in, unsigned char *out, const AES_KEY *key) { const u32 *rk; u32 s0, s1, s2, s3, t0, t1, t2, t3; #ifndef FULL_UNROLL int r; #endif assert(in && out && key); rk = key->rd_key; s0 = GETU32(in ) ^ rk[0]; s1 = GETU32(in + 4) ^ rk[1]; s2 = GETU32(in + 8) ^ rk[2]; s3 = GETU32(in + 12) ^ rk[3]; #ifdef FULL_UNROLL t0 = Td0[s0 >> 24] ^ Td1[(s3 >> 16) & 0xff] ^ Td2[(s2 >> 8) & 0xff] ^ Td3[s1 & 0xff] ^ rk[ 4]; t1 = Td0[s1 >> 24] ^ Td1[(s0 >> 16) & 0xff] ^ Td2[(s3 >> 8) & 0xff] ^ Td3[s2 & 0xff] ^ rk[ 5]; t2 = Td0[s2 >> 24] ^ Td1[(s1 >> 16) & 0xff] ^ Td2[(s0 >> 8) & 0xff] ^ Td3[s3 & 0xff] ^ rk[ 6]; t3 = Td0[s3 >> 24] ^ Td1[(s2 >> 16) & 0xff] ^ Td2[(s1 >> 8) & 0xff] ^ Td3[s0 & 0xff] ^ rk[ 7]; s0 = Td0[t0 >> 24] ^ Td1[(t3 >> 16) & 0xff] ^ Td2[(t2 >> 8) & 0xff] ^ Td3[t1 & 0xff] ^ rk[ 8]; s1 = Td0[t1 >> 24] ^ Td1[(t0 >> 16) & 0xff] ^ Td2[(t3 >> 8) & 0xff] ^ Td3[t2 & 0xff] ^ rk[ 9]; s2 = Td0[t2 >> 24] ^ Td1[(t1 >> 16) & 0xff] ^ Td2[(t0 >> 8) & 0xff] ^ Td3[t3 & 0xff] ^ rk[10]; s3 = Td0[t3 >> 24] ^ Td1[(t2 >> 16) & 0xff] ^ Td2[(t1 >> 8) & 0xff] ^ Td3[t0 & 0xff] ^ rk[11]; t0 = Td0[s0 >> 24] ^ Td1[(s3 >> 16) & 0xff] ^ Td2[(s2 >> 8) & 0xff] ^ Td3[s1 & 0xff] ^ rk[12]; t1 = Td0[s1 >> 24] ^ Td1[(s0 >> 16) & 0xff] ^ Td2[(s3 >> 8) & 0xff] ^ Td3[s2 & 0xff] ^ rk[13]; t2 = Td0[s2 >> 24] ^ Td1[(s1 >> 16) & 0xff] ^ Td2[(s0 >> 8) & 0xff] ^ Td3[s3 & 0xff] ^ rk[14]; t3 = Td0[s3 >> 24] ^ Td1[(s2 >> 16) & 0xff] ^ Td2[(s1 >> 8) & 0xff] ^ Td3[s0 & 0xff] ^ rk[15]; s0 = Td0[t0 >> 24] ^ Td1[(t3 >> 16) & 0xff] ^ Td2[(t2 >> 8) & 0xff] ^ Td3[t1 & 0xff] ^ rk[16]; s1 = Td0[t1 >> 24] ^ Td1[(t0 >> 16) & 0xff] ^ Td2[(t3 >> 8) & 0xff] ^ Td3[t2 & 0xff] ^ rk[17]; s2 = Td0[t2 >> 24] ^ Td1[(t1 >> 16) & 0xff] ^ Td2[(t0 >> 8) & 0xff] ^ Td3[t3 & 0xff] ^ rk[18]; s3 = Td0[t3 >> 24] ^ Td1[(t2 >> 16) & 0xff] ^ Td2[(t1 >> 8) & 0xff] ^ Td3[t0 & 0xff] ^ rk[19]; t0 = Td0[s0 >> 24] ^ Td1[(s3 >> 16) & 0xff] ^ Td2[(s2 >> 8) & 0xff] ^ Td3[s1 & 0xff] ^ rk[20]; t1 = Td0[s1 >> 24] ^ Td1[(s0 >> 16) & 0xff] ^ Td2[(s3 >> 8) & 0xff] ^ Td3[s2 & 0xff] ^ rk[21]; t2 = Td0[s2 >> 24] ^ Td1[(s1 >> 16) & 0xff] ^ Td2[(s0 >> 8) & 0xff] ^ Td3[s3 & 0xff] ^ rk[22]; t3 = Td0[s3 >> 24] ^ Td1[(s2 >> 16) & 0xff] ^ Td2[(s1 >> 8) & 0xff] ^ Td3[s0 & 0xff] ^ rk[23]; s0 = Td0[t0 >> 24] ^ Td1[(t3 >> 16) & 0xff] ^ Td2[(t2 >> 8) & 0xff] ^ Td3[t1 & 0xff] ^ rk[24]; s1 = Td0[t1 >> 24] ^ Td1[(t0 >> 16) & 0xff] ^ Td2[(t3 >> 8) & 0xff] ^ Td3[t2 & 0xff] ^ rk[25]; s2 = Td0[t2 >> 24] ^ Td1[(t1 >> 16) & 0xff] ^ Td2[(t0 >> 8) & 0xff] ^ Td3[t3 & 0xff] ^ rk[26]; s3 = Td0[t3 >> 24] ^ Td1[(t2 >> 16) & 0xff] ^ Td2[(t1 >> 8) & 0xff] ^ Td3[t0 & 0xff] ^ rk[27]; t0 = Td0[s0 >> 24] ^ Td1[(s3 >> 16) & 0xff] ^ Td2[(s2 >> 8) & 0xff] ^ Td3[s1 & 0xff] ^ rk[28]; t1 = Td0[s1 >> 24] ^ Td1[(s0 >> 16) & 0xff] ^ Td2[(s3 >> 8) & 0xff] ^ Td3[s2 & 0xff] ^ rk[29]; t2 = Td0[s2 >> 24] ^ Td1[(s1 >> 16) & 0xff] ^ Td2[(s0 >> 8) & 0xff] ^ Td3[s3 & 0xff] ^ rk[30]; t3 = Td0[s3 >> 24] ^ Td1[(s2 >> 16) & 0xff] ^ Td2[(s1 >> 8) & 0xff] ^ Td3[s0 & 0xff] ^ rk[31]; s0 = Td0[t0 >> 24] ^ Td1[(t3 >> 16) & 0xff] ^ Td2[(t2 >> 8) & 0xff] ^ Td3[t1 & 0xff] ^ rk[32]; s1 = Td0[t1 >> 24] ^ Td1[(t0 >> 16) & 0xff] ^ Td2[(t3 >> 8) & 0xff] ^ Td3[t2 & 0xff] ^ rk[33]; s2 = Td0[t2 >> 24] ^ Td1[(t1 >> 16) & 0xff] ^ Td2[(t0 >> 8) & 0xff] ^ Td3[t3 & 0xff] ^ rk[34]; s3 = Td0[t3 >> 24] ^ Td1[(t2 >> 16) & 0xff] ^ Td2[(t1 >> 8) & 0xff] ^ Td3[t0 & 0xff] ^ rk[35]; t0 = Td0[s0 >> 24] ^ Td1[(s3 >> 16) & 0xff] ^ Td2[(s2 >> 8) & 0xff] ^ Td3[s1 & 0xff] ^ rk[36]; t1 = Td0[s1 >> 24] ^ Td1[(s0 >> 16) & 0xff] ^ Td2[(s3 >> 8) & 0xff] ^ Td3[s2 & 0xff] ^ rk[37]; t2 = Td0[s2 >> 24] ^ Td1[(s1 >> 16) & 0xff] ^ Td2[(s0 >> 8) & 0xff] ^ Td3[s3 & 0xff] ^ rk[38]; t3 = Td0[s3 >> 24] ^ Td1[(s2 >> 16) & 0xff] ^ Td2[(s1 >> 8) & 0xff] ^ Td3[s0 & 0xff] ^ rk[39]; if (key->rounds > 10) { s0 = Td0[t0 >> 24] ^ Td1[(t3 >> 16) & 0xff] ^ Td2[(t2 >> 8) & 0xff] ^ Td3[t1 & 0xff] ^ rk[40]; s1 = Td0[t1 >> 24] ^ Td1[(t0 >> 16) & 0xff] ^ Td2[(t3 >> 8) & 0xff] ^ Td3[t2 & 0xff] ^ rk[41]; s2 = Td0[t2 >> 24] ^ Td1[(t1 >> 16) & 0xff] ^ Td2[(t0 >> 8) & 0xff] ^ Td3[t3 & 0xff] ^ rk[42]; s3 = Td0[t3 >> 24] ^ Td1[(t2 >> 16) & 0xff] ^ Td2[(t1 >> 8) & 0xff] ^ Td3[t0 & 0xff] ^ rk[43]; t0 = Td0[s0 >> 24] ^ Td1[(s3 >> 16) & 0xff] ^ Td2[(s2 >> 8) & 0xff] ^ Td3[s1 & 0xff] ^ rk[44]; t1 = Td0[s1 >> 24] ^ Td1[(s0 >> 16) & 0xff] ^ Td2[(s3 >> 8) & 0xff] ^ Td3[s2 & 0xff] ^ rk[45]; t2 = Td0[s2 >> 24] ^ Td1[(s1 >> 16) & 0xff] ^ Td2[(s0 >> 8) & 0xff] ^ Td3[s3 & 0xff] ^ rk[46]; t3 = Td0[s3 >> 24] ^ Td1[(s2 >> 16) & 0xff] ^ Td2[(s1 >> 8) & 0xff] ^ Td3[s0 & 0xff] ^ rk[47]; if (key->rounds > 12) { s0 = Td0[t0 >> 24] ^ Td1[(t3 >> 16) & 0xff] ^ Td2[(t2 >> 8) & 0xff] ^ Td3[t1 & 0xff] ^ rk[48]; s1 = Td0[t1 >> 24] ^ Td1[(t0 >> 16) & 0xff] ^ Td2[(t3 >> 8) & 0xff] ^ Td3[t2 & 0xff] ^ rk[49]; s2 = Td0[t2 >> 24] ^ Td1[(t1 >> 16) & 0xff] ^ Td2[(t0 >> 8) & 0xff] ^ Td3[t3 & 0xff] ^ rk[50]; s3 = Td0[t3 >> 24] ^ Td1[(t2 >> 16) & 0xff] ^ Td2[(t1 >> 8) & 0xff] ^ Td3[t0 & 0xff] ^ rk[51]; t0 = Td0[s0 >> 24] ^ Td1[(s3 >> 16) & 0xff] ^ Td2[(s2 >> 8) & 0xff] ^ Td3[s1 & 0xff] ^ rk[52]; t1 = Td0[s1 >> 24] ^ Td1[(s0 >> 16) & 0xff] ^ Td2[(s3 >> 8) & 0xff] ^ Td3[s2 & 0xff] ^ rk[53]; t2 = Td0[s2 >> 24] ^ Td1[(s1 >> 16) & 0xff] ^ Td2[(s0 >> 8) & 0xff] ^ Td3[s3 & 0xff] ^ rk[54]; t3 = Td0[s3 >> 24] ^ Td1[(s2 >> 16) & 0xff] ^ Td2[(s1 >> 8) & 0xff] ^ Td3[s0 & 0xff] ^ rk[55]; } } rk += key->rounds << 2; #else r = key->rounds >> 1; for (;;) { t0 = Td0[(s0 >> 24) ] ^ Td1[(s3 >> 16) & 0xff] ^ Td2[(s2 >> 8) & 0xff] ^ Td3[(s1 ) & 0xff] ^ rk[4]; t1 = Td0[(s1 >> 24) ] ^ Td1[(s0 >> 16) & 0xff] ^ Td2[(s3 >> 8) & 0xff] ^ Td3[(s2 ) & 0xff] ^ rk[5]; t2 = Td0[(s2 >> 24) ] ^ Td1[(s1 >> 16) & 0xff] ^ Td2[(s0 >> 8) & 0xff] ^ Td3[(s3 ) & 0xff] ^ rk[6]; t3 = Td0[(s3 >> 24) ] ^ Td1[(s2 >> 16) & 0xff] ^ Td2[(s1 >> 8) & 0xff] ^ Td3[(s0 ) & 0xff] ^ rk[7]; rk += 8; if (--r == 0) { break; } s0 = Td0[(t0 >> 24) ] ^ Td1[(t3 >> 16) & 0xff] ^ Td2[(t2 >> 8) & 0xff] ^ Td3[(t1 ) & 0xff] ^ rk[0]; s1 = Td0[(t1 >> 24) ] ^ Td1[(t0 >> 16) & 0xff] ^ Td2[(t3 >> 8) & 0xff] ^ Td3[(t2 ) & 0xff] ^ rk[1]; s2 = Td0[(t2 >> 24) ] ^ Td1[(t1 >> 16) & 0xff] ^ Td2[(t0 >> 8) & 0xff] ^ Td3[(t3 ) & 0xff] ^ rk[2]; s3 = Td0[(t3 >> 24) ] ^ Td1[(t2 >> 16) & 0xff] ^ Td2[(t1 >> 8) & 0xff] ^ Td3[(t0 ) & 0xff] ^ rk[3]; } #endif s0 = ((u32)Td4[(t0 >> 24) ] << 24) ^ ((u32)Td4[(t3 >> 16) & 0xff] << 16) ^ ((u32)Td4[(t2 >> 8) & 0xff] << 8) ^ ((u32)Td4[(t1 ) & 0xff]) ^ rk[0]; PUTU32(out , s0); s1 = ((u32)Td4[(t1 >> 24) ] << 24) ^ ((u32)Td4[(t0 >> 16) & 0xff] << 16) ^ ((u32)Td4[(t3 >> 8) & 0xff] << 8) ^ ((u32)Td4[(t2 ) & 0xff]) ^ rk[1]; PUTU32(out + 4, s1); s2 = ((u32)Td4[(t2 >> 24) ] << 24) ^ ((u32)Td4[(t1 >> 16) & 0xff] << 16) ^ ((u32)Td4[(t0 >> 8) & 0xff] << 8) ^ ((u32)Td4[(t3 ) & 0xff]) ^ rk[2]; PUTU32(out + 8, s2); s3 = ((u32)Td4[(t3 >> 24) ] << 24) ^ ((u32)Td4[(t2 >> 16) & 0xff] << 16) ^ ((u32)Td4[(t1 >> 8) & 0xff] << 8) ^ ((u32)Td4[(t0 ) & 0xff]) ^ rk[3]; PUTU32(out + 12, s3); } #else static const u8 Te4[256] = { 0x63U, 0x7cU, 0x77U, 0x7bU, 0xf2U, 0x6bU, 0x6fU, 0xc5U, 0x30U, 0x01U, 0x67U, 0x2bU, 0xfeU, 0xd7U, 0xabU, 0x76U, 0xcaU, 0x82U, 0xc9U, 0x7dU, 0xfaU, 0x59U, 0x47U, 0xf0U, 0xadU, 0xd4U, 0xa2U, 0xafU, 0x9cU, 0xa4U, 0x72U, 0xc0U, 0xb7U, 0xfdU, 0x93U, 0x26U, 0x36U, 0x3fU, 0xf7U, 0xccU, 0x34U, 0xa5U, 0xe5U, 0xf1U, 0x71U, 0xd8U, 0x31U, 0x15U, 0x04U, 0xc7U, 0x23U, 0xc3U, 0x18U, 0x96U, 0x05U, 0x9aU, 0x07U, 0x12U, 0x80U, 0xe2U, 0xebU, 0x27U, 0xb2U, 0x75U, 0x09U, 0x83U, 0x2cU, 0x1aU, 0x1bU, 0x6eU, 0x5aU, 0xa0U, 0x52U, 0x3bU, 0xd6U, 0xb3U, 0x29U, 0xe3U, 0x2fU, 0x84U, 0x53U, 0xd1U, 0x00U, 0xedU, 0x20U, 0xfcU, 0xb1U, 0x5bU, 0x6aU, 0xcbU, 0xbeU, 0x39U, 0x4aU, 0x4cU, 0x58U, 0xcfU, 0xd0U, 0xefU, 0xaaU, 0xfbU, 0x43U, 0x4dU, 0x33U, 0x85U, 0x45U, 0xf9U, 0x02U, 0x7fU, 0x50U, 0x3cU, 0x9fU, 0xa8U, 0x51U, 0xa3U, 0x40U, 0x8fU, 0x92U, 0x9dU, 0x38U, 0xf5U, 0xbcU, 0xb6U, 0xdaU, 0x21U, 0x10U, 0xffU, 0xf3U, 0xd2U, 0xcdU, 0x0cU, 0x13U, 0xecU, 0x5fU, 0x97U, 0x44U, 0x17U, 0xc4U, 0xa7U, 0x7eU, 0x3dU, 0x64U, 0x5dU, 0x19U, 0x73U, 0x60U, 0x81U, 0x4fU, 0xdcU, 0x22U, 0x2aU, 0x90U, 0x88U, 0x46U, 0xeeU, 0xb8U, 0x14U, 0xdeU, 0x5eU, 0x0bU, 0xdbU, 0xe0U, 0x32U, 0x3aU, 0x0aU, 0x49U, 0x06U, 0x24U, 0x5cU, 0xc2U, 0xd3U, 0xacU, 0x62U, 0x91U, 0x95U, 0xe4U, 0x79U, 0xe7U, 0xc8U, 0x37U, 0x6dU, 0x8dU, 0xd5U, 0x4eU, 0xa9U, 0x6cU, 0x56U, 0xf4U, 0xeaU, 0x65U, 0x7aU, 0xaeU, 0x08U, 0xbaU, 0x78U, 0x25U, 0x2eU, 0x1cU, 0xa6U, 0xb4U, 0xc6U, 0xe8U, 0xddU, 0x74U, 0x1fU, 0x4bU, 0xbdU, 0x8bU, 0x8aU, 0x70U, 0x3eU, 0xb5U, 0x66U, 0x48U, 0x03U, 0xf6U, 0x0eU, 0x61U, 0x35U, 0x57U, 0xb9U, 0x86U, 0xc1U, 0x1dU, 0x9eU, 0xe1U, 0xf8U, 0x98U, 0x11U, 0x69U, 0xd9U, 0x8eU, 0x94U, 0x9bU, 0x1eU, 0x87U, 0xe9U, 0xceU, 0x55U, 0x28U, 0xdfU, 0x8cU, 0xa1U, 0x89U, 0x0dU, 0xbfU, 0xe6U, 0x42U, 0x68U, 0x41U, 0x99U, 0x2dU, 0x0fU, 0xb0U, 0x54U, 0xbbU, 0x16U }; static const u32 rcon[] = { 0x01000000, 0x02000000, 0x04000000, 0x08000000, 0x10000000, 0x20000000, 0x40000000, 0x80000000, 0x1B000000, 0x36000000, }; int AES_set_encrypt_key(const unsigned char *userKey, const int bits, AES_KEY *key) { u32 *rk; int i = 0; u32 temp; if (!userKey || !key) return -1; if (bits != 128 && bits != 192 && bits != 256) return -2; rk = key->rd_key; if (bits == 128) key->rounds = 10; else if (bits == 192) key->rounds = 12; else key->rounds = 14; rk[0] = GETU32(userKey ); rk[1] = GETU32(userKey + 4); rk[2] = GETU32(userKey + 8); rk[3] = GETU32(userKey + 12); if (bits == 128) { while (1) { temp = rk[3]; rk[4] = rk[0] ^ ((u32)Te4[(temp >> 16) & 0xff] << 24) ^ ((u32)Te4[(temp >> 8) & 0xff] << 16) ^ ((u32)Te4[(temp ) & 0xff] << 8) ^ ((u32)Te4[(temp >> 24) ]) ^ rcon[i]; rk[5] = rk[1] ^ rk[4]; rk[6] = rk[2] ^ rk[5]; rk[7] = rk[3] ^ rk[6]; if (++i == 10) { return 0; } rk += 4; } } rk[4] = GETU32(userKey + 16); rk[5] = GETU32(userKey + 20); if (bits == 192) { while (1) { temp = rk[ 5]; rk[ 6] = rk[ 0] ^ ((u32)Te4[(temp >> 16) & 0xff] << 24) ^ ((u32)Te4[(temp >> 8) & 0xff] << 16) ^ ((u32)Te4[(temp ) & 0xff] << 8) ^ ((u32)Te4[(temp >> 24) ]) ^ rcon[i]; rk[ 7] = rk[ 1] ^ rk[ 6]; rk[ 8] = rk[ 2] ^ rk[ 7]; rk[ 9] = rk[ 3] ^ rk[ 8]; if (++i == 8) { return 0; } rk[10] = rk[ 4] ^ rk[ 9]; rk[11] = rk[ 5] ^ rk[10]; rk += 6; } } rk[6] = GETU32(userKey + 24); rk[7] = GETU32(userKey + 28); if (bits == 256) { while (1) { temp = rk[ 7]; rk[ 8] = rk[ 0] ^ ((u32)Te4[(temp >> 16) & 0xff] << 24) ^ ((u32)Te4[(temp >> 8) & 0xff] << 16) ^ ((u32)Te4[(temp ) & 0xff] << 8) ^ ((u32)Te4[(temp >> 24) ]) ^ rcon[i]; rk[ 9] = rk[ 1] ^ rk[ 8]; rk[10] = rk[ 2] ^ rk[ 9]; rk[11] = rk[ 3] ^ rk[10]; if (++i == 7) { return 0; } temp = rk[11]; rk[12] = rk[ 4] ^ ((u32)Te4[(temp >> 24) ] << 24) ^ ((u32)Te4[(temp >> 16) & 0xff] << 16) ^ ((u32)Te4[(temp >> 8) & 0xff] << 8) ^ ((u32)Te4[(temp ) & 0xff]); rk[13] = rk[ 5] ^ rk[12]; rk[14] = rk[ 6] ^ rk[13]; rk[15] = rk[ 7] ^ rk[14]; rk += 8; } } return 0; } int AES_set_decrypt_key(const unsigned char *userKey, const int bits, AES_KEY *key) { u32 *rk; int i, j, status; u32 temp; status = AES_set_encrypt_key(userKey, bits, key); if (status < 0) return status; rk = key->rd_key; for (i = 0, j = 4*(key->rounds); i < j; i += 4, j -= 4) { temp = rk[i ]; rk[i ] = rk[j ]; rk[j ] = temp; temp = rk[i + 1]; rk[i + 1] = rk[j + 1]; rk[j + 1] = temp; temp = rk[i + 2]; rk[i + 2] = rk[j + 2]; rk[j + 2] = temp; temp = rk[i + 3]; rk[i + 3] = rk[j + 3]; rk[j + 3] = temp; } for (i = 1; i < (key->rounds); i++) { rk += 4; for (j = 0; j < 4; j++) { u32 tp1, tp2, tp4, tp8, tp9, tpb, tpd, tpe, m; tp1 = rk[j]; m = tp1 & 0x80808080; tp2 = ((tp1 & 0x7f7f7f7f) << 1) ^ ((m - (m >> 7)) & 0x1b1b1b1b); m = tp2 & 0x80808080; tp4 = ((tp2 & 0x7f7f7f7f) << 1) ^ ((m - (m >> 7)) & 0x1b1b1b1b); m = tp4 & 0x80808080; tp8 = ((tp4 & 0x7f7f7f7f) << 1) ^ ((m - (m >> 7)) & 0x1b1b1b1b); tp9 = tp8 ^ tp1; tpb = tp9 ^ tp2; tpd = tp9 ^ tp4; tpe = tp8 ^ tp4 ^ tp2; #if defined(ROTATE) rk[j] = tpe ^ ROTATE(tpd,16) ^ ROTATE(tp9,24) ^ ROTATE(tpb,8); #else rk[j] = tpe ^ (tpd >> 16) ^ (tpd << 16) ^ (tp9 >> 8) ^ (tp9 << 24) ^ (tpb >> 24) ^ (tpb << 8); #endif } } return 0; } #endif
aes
openssl/crypto/aes/aes_core.c
openssl
#include "internal/deprecated.h" #include <openssl/aes.h> #include <openssl/modes.h> void AES_cfb128_encrypt(const unsigned char *in, unsigned char *out, size_t length, const AES_KEY *key, unsigned char *ivec, int *num, const int enc) { CRYPTO_cfb128_encrypt(in, out, length, key, ivec, num, enc, (block128_f) AES_encrypt); } void AES_cfb1_encrypt(const unsigned char *in, unsigned char *out, size_t length, const AES_KEY *key, unsigned char *ivec, int *num, const int enc) { CRYPTO_cfb128_1_encrypt(in, out, length, key, ivec, num, enc, (block128_f) AES_encrypt); } void AES_cfb8_encrypt(const unsigned char *in, unsigned char *out, size_t length, const AES_KEY *key, unsigned char *ivec, int *num, const int enc) { CRYPTO_cfb128_8_encrypt(in, out, length, key, ivec, num, enc, (block128_f) AES_encrypt); }
aes
openssl/crypto/aes/aes_cfb.c
openssl
#include <stdio.h> #include "internal/cryptlib.h" #include "internal/numbers.h" #include "internal/safe_math.h" #include <openssl/stack.h> #include <errno.h> #include <openssl/e_os2.h> OSSL_SAFE_MATH_SIGNED(int, int) static const int min_nodes = 4; static const int max_nodes = SIZE_MAX / sizeof(void *) < INT_MAX ? (int)(SIZE_MAX / sizeof(void *)) : INT_MAX; struct stack_st { int num; const void **data; int sorted; int num_alloc; OPENSSL_sk_compfunc comp; }; OPENSSL_sk_compfunc OPENSSL_sk_set_cmp_func(OPENSSL_STACK *sk, OPENSSL_sk_compfunc c) { OPENSSL_sk_compfunc old = sk->comp; if (sk->comp != c) sk->sorted = 0; sk->comp = c; return old; } OPENSSL_STACK *OPENSSL_sk_dup(const OPENSSL_STACK *sk) { OPENSSL_STACK *ret; if ((ret = OPENSSL_malloc(sizeof(*ret))) == NULL) goto err; if (sk == NULL) { ret->num = 0; ret->sorted = 0; ret->comp = NULL; } else { *ret = *sk; } if (sk == NULL || sk->num == 0) { ret->data = NULL; ret->num_alloc = 0; return ret; } ret->data = OPENSSL_malloc(sizeof(*ret->data) * sk->num_alloc); if (ret->data == NULL) goto err; memcpy(ret->data, sk->data, sizeof(void *) * sk->num); return ret; err: OPENSSL_sk_free(ret); return NULL; } OPENSSL_STACK *OPENSSL_sk_deep_copy(const OPENSSL_STACK *sk, OPENSSL_sk_copyfunc copy_func, OPENSSL_sk_freefunc free_func) { OPENSSL_STACK *ret; int i; if ((ret = OPENSSL_malloc(sizeof(*ret))) == NULL) goto err; if (sk == NULL) { ret->num = 0; ret->sorted = 0; ret->comp = NULL; } else { *ret = *sk; } if (sk == NULL || sk->num == 0) { ret->data = NULL; ret->num_alloc = 0; return ret; } ret->num_alloc = sk->num > min_nodes ? sk->num : min_nodes; ret->data = OPENSSL_zalloc(sizeof(*ret->data) * ret->num_alloc); if (ret->data == NULL) goto err; for (i = 0; i < ret->num; ++i) { if (sk->data[i] == NULL) continue; if ((ret->data[i] = copy_func(sk->data[i])) == NULL) { while (--i >= 0) if (ret->data[i] != NULL) free_func((void *)ret->data[i]); goto err; } } return ret; err: OPENSSL_sk_free(ret); return NULL; } OPENSSL_STACK *OPENSSL_sk_new_null(void) { return OPENSSL_sk_new_reserve(NULL, 0); } OPENSSL_STACK *OPENSSL_sk_new(OPENSSL_sk_compfunc c) { return OPENSSL_sk_new_reserve(c, 0); } static ossl_inline int compute_growth(int target, int current) { int err = 0; while (current < target) { if (current >= max_nodes) return 0; current = safe_muldiv_int(current, 8, 5, &err); if (err != 0) return 0; if (current >= max_nodes) current = max_nodes; } return current; } static int sk_reserve(OPENSSL_STACK *st, int n, int exact) { const void **tmpdata; int num_alloc; if (n > max_nodes - st->num) { ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_TOO_MANY_RECORDS); return 0; } num_alloc = st->num + n; if (num_alloc < min_nodes) num_alloc = min_nodes; if (st->data == NULL) { if ((st->data = OPENSSL_zalloc(sizeof(void *) * num_alloc)) == NULL) return 0; st->num_alloc = num_alloc; return 1; } if (!exact) { if (num_alloc <= st->num_alloc) return 1; num_alloc = compute_growth(num_alloc, st->num_alloc); if (num_alloc == 0) { ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_TOO_MANY_RECORDS); return 0; } } else if (num_alloc == st->num_alloc) { return 1; } tmpdata = OPENSSL_realloc((void *)st->data, sizeof(void *) * num_alloc); if (tmpdata == NULL) return 0; st->data = tmpdata; st->num_alloc = num_alloc; return 1; } OPENSSL_STACK *OPENSSL_sk_new_reserve(OPENSSL_sk_compfunc c, int n) { OPENSSL_STACK *st = OPENSSL_zalloc(sizeof(OPENSSL_STACK)); if (st == NULL) return NULL; st->comp = c; if (n <= 0) return st; if (!sk_reserve(st, n, 1)) { OPENSSL_sk_free(st); return NULL; } return st; } int OPENSSL_sk_reserve(OPENSSL_STACK *st, int n) { if (st == NULL) { ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER); return 0; } if (n < 0) return 1; return sk_reserve(st, n, 1); } int OPENSSL_sk_insert(OPENSSL_STACK *st, const void *data, int loc) { if (st == NULL) { ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER); return 0; } if (st->num == max_nodes) { ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_TOO_MANY_RECORDS); return 0; } if (!sk_reserve(st, 1, 0)) return 0; if ((loc >= st->num) || (loc < 0)) { st->data[st->num] = data; } else { memmove(&st->data[loc + 1], &st->data[loc], sizeof(st->data[0]) * (st->num - loc)); st->data[loc] = data; } st->num++; st->sorted = 0; return st->num; } static ossl_inline void *internal_delete(OPENSSL_STACK *st, int loc) { const void *ret = st->data[loc]; if (loc != st->num - 1) memmove(&st->data[loc], &st->data[loc + 1], sizeof(st->data[0]) * (st->num - loc - 1)); st->num--; return (void *)ret; } void *OPENSSL_sk_delete_ptr(OPENSSL_STACK *st, const void *p) { int i; if (st == NULL) return NULL; for (i = 0; i < st->num; i++) if (st->data[i] == p) return internal_delete(st, i); return NULL; } void *OPENSSL_sk_delete(OPENSSL_STACK *st, int loc) { if (st == NULL || loc < 0 || loc >= st->num) return NULL; return internal_delete(st, loc); } static int internal_find(OPENSSL_STACK *st, const void *data, int ret_val_options, int *pnum_matched) { const void *r; int i, count = 0; int *pnum = pnum_matched; if (st == NULL || st->num == 0) return -1; if (pnum == NULL) pnum = &count; if (st->comp == NULL) { for (i = 0; i < st->num; i++) if (st->data[i] == data) { *pnum = 1; return i; } *pnum = 0; return -1; } if (data == NULL) return -1; if (!st->sorted) { int res = -1; for (i = 0; i < st->num; i++) if (st->comp(&data, st->data + i) == 0) { if (res == -1) res = i; ++*pnum; if (pnum_matched == NULL) return i; } if (res == -1) *pnum = 0; return res; } if (pnum_matched != NULL) ret_val_options |= OSSL_BSEARCH_FIRST_VALUE_ON_MATCH; r = ossl_bsearch(&data, st->data, st->num, sizeof(void *), st->comp, ret_val_options); if (pnum_matched != NULL) { *pnum = 0; if (r != NULL) { const void **p = (const void **)r; while (p < st->data + st->num) { if (st->comp(&data, p) != 0) break; ++*pnum; ++p; } } } return r == NULL ? -1 : (int)((const void **)r - st->data); } int OPENSSL_sk_find(OPENSSL_STACK *st, const void *data) { return internal_find(st, data, OSSL_BSEARCH_FIRST_VALUE_ON_MATCH, NULL); } int OPENSSL_sk_find_ex(OPENSSL_STACK *st, const void *data) { return internal_find(st, data, OSSL_BSEARCH_VALUE_ON_NOMATCH, NULL); } int OPENSSL_sk_find_all(OPENSSL_STACK *st, const void *data, int *pnum) { return internal_find(st, data, OSSL_BSEARCH_FIRST_VALUE_ON_MATCH, pnum); } int OPENSSL_sk_push(OPENSSL_STACK *st, const void *data) { if (st == NULL) return 0; return OPENSSL_sk_insert(st, data, st->num); } int OPENSSL_sk_unshift(OPENSSL_STACK *st, const void *data) { return OPENSSL_sk_insert(st, data, 0); } void *OPENSSL_sk_shift(OPENSSL_STACK *st) { if (st == NULL || st->num == 0) return NULL; return internal_delete(st, 0); } void *OPENSSL_sk_pop(OPENSSL_STACK *st) { if (st == NULL || st->num == 0) return NULL; return internal_delete(st, st->num - 1); } void OPENSSL_sk_zero(OPENSSL_STACK *st) { if (st == NULL || st->num == 0) return; memset(st->data, 0, sizeof(*st->data) * st->num); st->num = 0; } void OPENSSL_sk_pop_free(OPENSSL_STACK *st, OPENSSL_sk_freefunc func) { int i; if (st == NULL) return; for (i = 0; i < st->num; i++) if (st->data[i] != NULL) func((char *)st->data[i]); OPENSSL_sk_free(st); } void OPENSSL_sk_free(OPENSSL_STACK *st) { if (st == NULL) return; OPENSSL_free(st->data); OPENSSL_free(st); } int OPENSSL_sk_num(const OPENSSL_STACK *st) { return st == NULL ? -1 : st->num; } void *OPENSSL_sk_value(const OPENSSL_STACK *st, int i) { if (st == NULL || i < 0 || i >= st->num) return NULL; return (void *)st->data[i]; } void *OPENSSL_sk_set(OPENSSL_STACK *st, int i, const void *data) { if (st == NULL) { ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER); return NULL; } if (i < 0 || i >= st->num) { ERR_raise_data(ERR_LIB_CRYPTO, ERR_R_PASSED_INVALID_ARGUMENT, "i=%d", i); return NULL; } st->data[i] = data; st->sorted = 0; return (void *)st->data[i]; } void OPENSSL_sk_sort(OPENSSL_STACK *st) { if (st != NULL && !st->sorted && st->comp != NULL) { if (st->num > 1) qsort(st->data, st->num, sizeof(void *), st->comp); st->sorted = 1; } } int OPENSSL_sk_is_sorted(const OPENSSL_STACK *st) { return st == NULL ? 1 : st->sorted; }
stack
openssl/crypto/stack/stack.c
openssl
#include <openssl/err.h> #include <openssl/httperr.h> #include "crypto/httperr.h" #ifndef OPENSSL_NO_HTTP # ifndef OPENSSL_NO_ERR static const ERR_STRING_DATA HTTP_str_reasons[] = { {ERR_PACK(ERR_LIB_HTTP, 0, HTTP_R_ASN1_LEN_EXCEEDS_MAX_RESP_LEN), "asn1 len exceeds max resp len"}, {ERR_PACK(ERR_LIB_HTTP, 0, HTTP_R_CONNECT_FAILURE), "connect failure"}, {ERR_PACK(ERR_LIB_HTTP, 0, HTTP_R_ERROR_PARSING_ASN1_LENGTH), "error parsing asn1 length"}, {ERR_PACK(ERR_LIB_HTTP, 0, HTTP_R_ERROR_PARSING_CONTENT_LENGTH), "error parsing content length"}, {ERR_PACK(ERR_LIB_HTTP, 0, HTTP_R_ERROR_PARSING_URL), "error parsing url"}, {ERR_PACK(ERR_LIB_HTTP, 0, HTTP_R_ERROR_RECEIVING), "error receiving"}, {ERR_PACK(ERR_LIB_HTTP, 0, HTTP_R_ERROR_SENDING), "error sending"}, {ERR_PACK(ERR_LIB_HTTP, 0, HTTP_R_FAILED_READING_DATA), "failed reading data"}, {ERR_PACK(ERR_LIB_HTTP, 0, HTTP_R_HEADER_PARSE_ERROR), "header parse error"}, {ERR_PACK(ERR_LIB_HTTP, 0, HTTP_R_INCONSISTENT_CONTENT_LENGTH), "inconsistent content length"}, {ERR_PACK(ERR_LIB_HTTP, 0, HTTP_R_INVALID_PORT_NUMBER), "invalid port number"}, {ERR_PACK(ERR_LIB_HTTP, 0, HTTP_R_INVALID_URL_PATH), "invalid url path"}, {ERR_PACK(ERR_LIB_HTTP, 0, HTTP_R_INVALID_URL_SCHEME), "invalid url scheme"}, {ERR_PACK(ERR_LIB_HTTP, 0, HTTP_R_MAX_RESP_LEN_EXCEEDED), "max resp len exceeded"}, {ERR_PACK(ERR_LIB_HTTP, 0, HTTP_R_MISSING_ASN1_ENCODING), "missing asn1 encoding"}, {ERR_PACK(ERR_LIB_HTTP, 0, HTTP_R_MISSING_CONTENT_TYPE), "missing content type"}, {ERR_PACK(ERR_LIB_HTTP, 0, HTTP_R_MISSING_REDIRECT_LOCATION), "missing redirect location"}, {ERR_PACK(ERR_LIB_HTTP, 0, HTTP_R_RECEIVED_ERROR), "received error"}, {ERR_PACK(ERR_LIB_HTTP, 0, HTTP_R_RECEIVED_WRONG_HTTP_VERSION), "received wrong http version"}, {ERR_PACK(ERR_LIB_HTTP, 0, HTTP_R_REDIRECTION_FROM_HTTPS_TO_HTTP), "redirection from https to http"}, {ERR_PACK(ERR_LIB_HTTP, 0, HTTP_R_REDIRECTION_NOT_ENABLED), "redirection not enabled"}, {ERR_PACK(ERR_LIB_HTTP, 0, HTTP_R_RESPONSE_LINE_TOO_LONG), "response line too long"}, {ERR_PACK(ERR_LIB_HTTP, 0, HTTP_R_RESPONSE_PARSE_ERROR), "response parse error"}, {ERR_PACK(ERR_LIB_HTTP, 0, HTTP_R_RESPONSE_TOO_MANY_HDRLINES), "response too many hdrlines"}, {ERR_PACK(ERR_LIB_HTTP, 0, HTTP_R_RETRY_TIMEOUT), "retry timeout"}, {ERR_PACK(ERR_LIB_HTTP, 0, HTTP_R_SERVER_CANCELED_CONNECTION), "server canceled connection"}, {ERR_PACK(ERR_LIB_HTTP, 0, HTTP_R_SOCK_NOT_SUPPORTED), "sock not supported"}, {ERR_PACK(ERR_LIB_HTTP, 0, HTTP_R_STATUS_CODE_UNSUPPORTED), "status code unsupported"}, {ERR_PACK(ERR_LIB_HTTP, 0, HTTP_R_TLS_NOT_ENABLED), "tls not enabled"}, {ERR_PACK(ERR_LIB_HTTP, 0, HTTP_R_TOO_MANY_REDIRECTIONS), "too many redirections"}, {ERR_PACK(ERR_LIB_HTTP, 0, HTTP_R_UNEXPECTED_CONTENT_TYPE), "unexpected content type"}, {0, NULL} }; # endif int ossl_err_load_HTTP_strings(void) { # ifndef OPENSSL_NO_ERR if (ERR_reason_error_string(HTTP_str_reasons[0].error) == NULL) ERR_load_strings_const(HTTP_str_reasons); # endif return 1; } #else NON_EMPTY_TRANSLATION_UNIT #endif
http
openssl/crypto/http/http_err.c
openssl
#include "internal/e_os.h" #include <stdio.h> #include <stdlib.h> #include "crypto/ctype.h" #include <string.h> #include <openssl/asn1.h> #include <openssl/evp.h> #include <openssl/err.h> #include <openssl/httperr.h> #include <openssl/cmperr.h> #include <openssl/buffer.h> #include <openssl/http.h> #include <openssl/trace.h> #include "internal/sockets.h" #include "internal/common.h" #define HTTP_PREFIX "HTTP/" #define HTTP_VERSION_PATT "1." #define HTTP_VERSION_STR_LEN sizeof(HTTP_VERSION_PATT) #define HTTP_PREFIX_VERSION HTTP_PREFIX""HTTP_VERSION_PATT #define HTTP_1_0 HTTP_PREFIX_VERSION"0" #define HTTP_LINE1_MINLEN (sizeof(HTTP_PREFIX_VERSION "x 200\n") - 1) #define HTTP_VERSION_MAX_REDIRECTIONS 50 #define HTTP_STATUS_CODE_OK 200 #define HTTP_STATUS_CODE_MOVED_PERMANENTLY 301 #define HTTP_STATUS_CODE_FOUND 302 struct ossl_http_req_ctx_st { int state; unsigned char *buf; int buf_size; int free_wbio; BIO *wbio; BIO *rbio; OSSL_HTTP_bio_cb_t upd_fn; void *upd_arg; int use_ssl; char *proxy; char *server; char *port; BIO *mem; BIO *req; int method_POST; int text; char *expected_ct; int expect_asn1; unsigned char *pos; long len_to_send; size_t resp_len; size_t max_resp_len; int keep_alive; time_t max_time; time_t max_total_time; char *redirection_url; size_t max_hdr_lines; }; #define OHS_NOREAD 0x1000 #define OHS_ERROR (0 | OHS_NOREAD) #define OHS_ADD_HEADERS (1 | OHS_NOREAD) #define OHS_WRITE_INIT (2 | OHS_NOREAD) #define OHS_WRITE_HDR1 (3 | OHS_NOREAD) #define OHS_WRITE_HDR (4 | OHS_NOREAD) #define OHS_WRITE_REQ (5 | OHS_NOREAD) #define OHS_FLUSH (6 | OHS_NOREAD) #define OHS_FIRSTLINE 1 #define OHS_HEADERS 2 #define OHS_HEADERS_ERROR 3 #define OHS_REDIRECT 4 #define OHS_ASN1_HEADER 5 #define OHS_ASN1_CONTENT 6 #define OHS_ASN1_DONE (7 | OHS_NOREAD) #define OHS_STREAM (8 | OHS_NOREAD) OSSL_HTTP_REQ_CTX *OSSL_HTTP_REQ_CTX_new(BIO *wbio, BIO *rbio, int buf_size) { OSSL_HTTP_REQ_CTX *rctx; if (wbio == NULL || rbio == NULL) { ERR_raise(ERR_LIB_HTTP, ERR_R_PASSED_NULL_PARAMETER); return NULL; } if ((rctx = OPENSSL_zalloc(sizeof(*rctx))) == NULL) return NULL; rctx->state = OHS_ERROR; rctx->buf_size = buf_size > 0 ? buf_size : OSSL_HTTP_DEFAULT_MAX_LINE_LEN; rctx->buf = OPENSSL_malloc(rctx->buf_size); rctx->wbio = wbio; rctx->rbio = rbio; rctx->max_hdr_lines = OSSL_HTTP_DEFAULT_MAX_RESP_HDR_LINES; if (rctx->buf == NULL) { OPENSSL_free(rctx); return NULL; } rctx->max_resp_len = OSSL_HTTP_DEFAULT_MAX_RESP_LEN; return rctx; } void OSSL_HTTP_REQ_CTX_free(OSSL_HTTP_REQ_CTX *rctx) { if (rctx == NULL) return; if (rctx->free_wbio) BIO_free_all(rctx->wbio); BIO_free(rctx->mem); BIO_free(rctx->req); OPENSSL_free(rctx->buf); OPENSSL_free(rctx->proxy); OPENSSL_free(rctx->server); OPENSSL_free(rctx->port); OPENSSL_free(rctx->expected_ct); OPENSSL_free(rctx); } BIO *OSSL_HTTP_REQ_CTX_get0_mem_bio(const OSSL_HTTP_REQ_CTX *rctx) { if (rctx == NULL) { ERR_raise(ERR_LIB_HTTP, ERR_R_PASSED_NULL_PARAMETER); return NULL; } return rctx->mem; } size_t OSSL_HTTP_REQ_CTX_get_resp_len(const OSSL_HTTP_REQ_CTX *rctx) { if (rctx == NULL) { ERR_raise(ERR_LIB_HTTP, ERR_R_PASSED_NULL_PARAMETER); return 0; } return rctx->resp_len; } void OSSL_HTTP_REQ_CTX_set_max_response_length(OSSL_HTTP_REQ_CTX *rctx, unsigned long len) { if (rctx == NULL) { ERR_raise(ERR_LIB_HTTP, ERR_R_PASSED_NULL_PARAMETER); return; } rctx->max_resp_len = len != 0 ? (size_t)len : OSSL_HTTP_DEFAULT_MAX_RESP_LEN; } int OSSL_HTTP_REQ_CTX_set_request_line(OSSL_HTTP_REQ_CTX *rctx, int method_POST, const char *server, const char *port, const char *path) { if (rctx == NULL) { ERR_raise(ERR_LIB_HTTP, ERR_R_PASSED_NULL_PARAMETER); return 0; } BIO_free(rctx->mem); if ((rctx->mem = BIO_new(BIO_s_mem())) == NULL) return 0; rctx->method_POST = method_POST != 0; if (BIO_printf(rctx->mem, "%s ", rctx->method_POST ? "POST" : "GET") <= 0) return 0; if (server != NULL) { if (BIO_printf(rctx->mem, OSSL_HTTP_PREFIX"%s", server) <= 0) return 0; if (port != NULL && BIO_printf(rctx->mem, ":%s", port) <= 0) return 0; } if (path == NULL) { path = "/"; } else if (HAS_PREFIX(path, "http: if (server != NULL) { ERR_raise(ERR_LIB_HTTP, ERR_R_PASSED_INVALID_ARGUMENT); return 0; } } else if (path[0] != '/' && BIO_printf(rctx->mem, "/") <= 0) { return 0; } if (BIO_printf(rctx->mem, "%s "HTTP_1_0"\r\n", path) <= 0) return 0; rctx->resp_len = 0; rctx->state = OHS_ADD_HEADERS; return 1; } int OSSL_HTTP_REQ_CTX_add1_header(OSSL_HTTP_REQ_CTX *rctx, const char *name, const char *value) { if (rctx == NULL || name == NULL) { ERR_raise(ERR_LIB_HTTP, ERR_R_PASSED_NULL_PARAMETER); return 0; } if (rctx->mem == NULL) { ERR_raise(ERR_LIB_HTTP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); return 0; } if (BIO_puts(rctx->mem, name) <= 0) return 0; if (value != NULL) { if (BIO_write(rctx->mem, ": ", 2) != 2) return 0; if (BIO_puts(rctx->mem, value) <= 0) return 0; } return BIO_write(rctx->mem, "\r\n", 2) == 2; } int OSSL_HTTP_REQ_CTX_set_expected(OSSL_HTTP_REQ_CTX *rctx, const char *content_type, int asn1, int timeout, int keep_alive) { if (rctx == NULL) { ERR_raise(ERR_LIB_HTTP, ERR_R_PASSED_NULL_PARAMETER); return 0; } if (keep_alive != 0 && rctx->state != OHS_ERROR && rctx->state != OHS_ADD_HEADERS) { ERR_raise(ERR_LIB_HTTP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); return 0; } OPENSSL_free(rctx->expected_ct); rctx->expected_ct = NULL; if (content_type != NULL && (rctx->expected_ct = OPENSSL_strdup(content_type)) == NULL) return 0; rctx->expect_asn1 = asn1; if (timeout >= 0) rctx->max_time = timeout > 0 ? time(NULL) + timeout : 0; else rctx->max_time = rctx->max_total_time; rctx->keep_alive = keep_alive; return 1; } static int set1_content(OSSL_HTTP_REQ_CTX *rctx, const char *content_type, BIO *req) { long req_len = 0; #ifndef OPENSSL_NO_STDIO FILE *fp = NULL; #endif if (rctx == NULL || (req == NULL && content_type != NULL)) { ERR_raise(ERR_LIB_HTTP, ERR_R_PASSED_NULL_PARAMETER); return 0; } if (rctx->keep_alive != 0 && !OSSL_HTTP_REQ_CTX_add1_header(rctx, "Connection", "keep-alive")) return 0; BIO_free(rctx->req); rctx->req = NULL; if (req == NULL) return 1; if (!rctx->method_POST) { ERR_raise(ERR_LIB_HTTP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); return 0; } if (content_type == NULL) { rctx->text = 1; } else { if (OPENSSL_strncasecmp(content_type, "text/", 5) == 0) rctx->text = 1; if (BIO_printf(rctx->mem, "Content-Type: %s\r\n", content_type) <= 0) return 0; } if (BIO_method_type(req) == BIO_TYPE_FILE) { #ifndef OPENSSL_NO_STDIO if (BIO_get_fp(req, &fp) == 1 && fseek(fp, 0, SEEK_END) == 0) { req_len = ftell(fp); (void)fseek(fp, 0, SEEK_SET); } else { fp = NULL; } #endif } else { req_len = BIO_ctrl(req, BIO_CTRL_INFO, 0, NULL); } if (( #ifndef OPENSSL_NO_STDIO fp != NULL || #endif req_len > 0) && BIO_printf(rctx->mem, "Content-Length: %ld\r\n", req_len) < 0) return 0; if (!BIO_up_ref(req)) return 0; rctx->req = req; return 1; } int OSSL_HTTP_REQ_CTX_set1_req(OSSL_HTTP_REQ_CTX *rctx, const char *content_type, const ASN1_ITEM *it, const ASN1_VALUE *req) { BIO *mem = NULL; int res = 1; if (req != NULL) res = (mem = ASN1_item_i2d_mem_bio(it, req)) != NULL; res = res && set1_content(rctx, content_type, mem); BIO_free(mem); return res; } void OSSL_HTTP_REQ_CTX_set_max_response_hdr_lines(OSSL_HTTP_REQ_CTX *rctx, size_t count) { if (rctx == NULL) { ERR_raise(ERR_LIB_HTTP, ERR_R_PASSED_NULL_PARAMETER); return; } rctx->max_hdr_lines = count; } static int add1_headers(OSSL_HTTP_REQ_CTX *rctx, const STACK_OF(CONF_VALUE) *headers, const char *host) { int i; int add_host = host != NULL && *host != '\0'; CONF_VALUE *hdr; for (i = 0; i < sk_CONF_VALUE_num(headers); i++) { hdr = sk_CONF_VALUE_value(headers, i); if (add_host && OPENSSL_strcasecmp("host", hdr->name) == 0) add_host = 0; if (!OSSL_HTTP_REQ_CTX_add1_header(rctx, hdr->name, hdr->value)) return 0; } if (add_host && !OSSL_HTTP_REQ_CTX_add1_header(rctx, "Host", host)) return 0; return 1; } static OSSL_HTTP_REQ_CTX *http_req_ctx_new(int free_wbio, BIO *wbio, BIO *rbio, OSSL_HTTP_bio_cb_t bio_update_fn, void *arg, int use_ssl, const char *proxy, const char *server, const char *port, int buf_size, int overall_timeout) { OSSL_HTTP_REQ_CTX *rctx = OSSL_HTTP_REQ_CTX_new(wbio, rbio, buf_size); if (rctx == NULL) return NULL; rctx->free_wbio = free_wbio; rctx->upd_fn = bio_update_fn; rctx->upd_arg = arg; rctx->use_ssl = use_ssl; if (proxy != NULL && (rctx->proxy = OPENSSL_strdup(proxy)) == NULL) goto err; if (server != NULL && (rctx->server = OPENSSL_strdup(server)) == NULL) goto err; if (port != NULL && (rctx->port = OPENSSL_strdup(port)) == NULL) goto err; rctx->max_total_time = overall_timeout > 0 ? time(NULL) + overall_timeout : 0; return rctx; err: OSSL_HTTP_REQ_CTX_free(rctx); return NULL; } static int parse_http_line1(char *line, int *found_keep_alive) { int i, retcode, err; char *code, *reason, *end; if (!CHECK_AND_SKIP_PREFIX(line, HTTP_PREFIX_VERSION)) goto err; *found_keep_alive = *line > '0'; for (code = line; *code != '\0' && !ossl_isspace(*code); code++) continue; if (*code == '\0') goto err; while (*code != '\0' && ossl_isspace(*code)) code++; if (*code == '\0') goto err; for (reason = code; *reason != '\0' && !ossl_isspace(*reason); reason++) continue; if (*reason == '\0') goto err; *reason++ = '\0'; retcode = strtoul(code, &end, 10); if (*end != '\0') goto err; while (*reason != '\0' && ossl_isspace(*reason)) reason++; if (*reason != '\0') { for (end = reason + strlen(reason) - 1; ossl_isspace(*end); end--) *end = '\0'; } switch (retcode) { case HTTP_STATUS_CODE_OK: case HTTP_STATUS_CODE_MOVED_PERMANENTLY: case HTTP_STATUS_CODE_FOUND: return retcode; default: err = HTTP_R_RECEIVED_ERROR; if (retcode < 400) err = HTTP_R_STATUS_CODE_UNSUPPORTED; if (*reason == '\0') ERR_raise_data(ERR_LIB_HTTP, err, "code=%s", code); else ERR_raise_data(ERR_LIB_HTTP, err, "code=%s, reason=%s", code, reason); return retcode; } err: for (i = 0; i < 60 && line[i] != '\0'; i++) if (!ossl_isprint(line[i])) line[i] = ' '; line[i] = '\0'; ERR_raise_data(ERR_LIB_HTTP, HTTP_R_HEADER_PARSE_ERROR, "content=%s", line); return 0; } static int check_set_resp_len(OSSL_HTTP_REQ_CTX *rctx, size_t len) { if (rctx->max_resp_len != 0 && len > rctx->max_resp_len) { ERR_raise_data(ERR_LIB_HTTP, HTTP_R_MAX_RESP_LEN_EXCEEDED, "length=%zu, max=%zu", len, rctx->max_resp_len); return 0; } if (rctx->resp_len != 0 && rctx->resp_len != len) { ERR_raise_data(ERR_LIB_HTTP, HTTP_R_INCONSISTENT_CONTENT_LENGTH, "ASN.1 length=%zu, Content-Length=%zu", len, rctx->resp_len); return 0; } rctx->resp_len = len; return 1; } static int may_still_retry(time_t max_time, int *ptimeout) { time_t time_diff, now = time(NULL); if (max_time != 0) { if (max_time < now) { ERR_raise(ERR_LIB_HTTP, HTTP_R_RETRY_TIMEOUT); return 0; } time_diff = max_time - now; *ptimeout = time_diff > INT_MAX ? INT_MAX : (int)time_diff; } return 1; } int OSSL_HTTP_REQ_CTX_nbio(OSSL_HTTP_REQ_CTX *rctx) { int i, found_expected_ct = 0, found_keep_alive = 0; int got_text = 1; long n; size_t resp_len; const unsigned char *p; char *buf, *key, *value, *line_end = NULL; size_t resp_hdr_lines = 0; if (rctx == NULL) { ERR_raise(ERR_LIB_HTTP, ERR_R_PASSED_NULL_PARAMETER); return 0; } if (rctx->mem == NULL || rctx->wbio == NULL || rctx->rbio == NULL) { ERR_raise(ERR_LIB_HTTP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); return 0; } rctx->redirection_url = NULL; next_io: buf = (char *)rctx->buf; if ((rctx->state & OHS_NOREAD) == 0) { if (rctx->expect_asn1) { n = BIO_read(rctx->rbio, rctx->buf, rctx->buf_size); } else { (void)ERR_set_mark(); n = BIO_gets(rctx->rbio, buf, rctx->buf_size); if (n == -2) { (void)ERR_pop_to_mark(); n = BIO_get_line(rctx->rbio, buf, rctx->buf_size); } else { (void)ERR_clear_last_mark(); } } if (n <= 0) { if (BIO_should_retry(rctx->rbio)) return -1; ERR_raise(ERR_LIB_HTTP, HTTP_R_FAILED_READING_DATA); return 0; } if (BIO_write(rctx->mem, rctx->buf, n) != n) return 0; } switch (rctx->state) { case OHS_ADD_HEADERS: if (BIO_write(rctx->mem, "\r\n", 2) != 2) { rctx->state = OHS_ERROR; return 0; } rctx->state = OHS_WRITE_INIT; case OHS_WRITE_INIT: rctx->len_to_send = BIO_get_mem_data(rctx->mem, &rctx->pos); rctx->state = OHS_WRITE_HDR1; case OHS_WRITE_HDR1: case OHS_WRITE_HDR: case OHS_WRITE_REQ: if (rctx->len_to_send > 0) { size_t sz; if (!BIO_write_ex(rctx->wbio, rctx->pos, rctx->len_to_send, &sz)) { if (BIO_should_retry(rctx->wbio)) return -1; rctx->state = OHS_ERROR; return 0; } if (OSSL_TRACE_ENABLED(HTTP) && rctx->state == OHS_WRITE_HDR1) OSSL_TRACE(HTTP, "Sending request: [\n"); OSSL_TRACE_STRING(HTTP, rctx->state != OHS_WRITE_REQ || rctx->text, rctx->state != OHS_WRITE_REQ, rctx->pos, sz); if (rctx->state == OHS_WRITE_HDR1) rctx->state = OHS_WRITE_HDR; rctx->pos += sz; rctx->len_to_send -= sz; goto next_io; } if (rctx->state == OHS_WRITE_HDR) { (void)BIO_reset(rctx->mem); rctx->state = OHS_WRITE_REQ; } if (rctx->req != NULL && !BIO_eof(rctx->req)) { n = BIO_read(rctx->req, rctx->buf, rctx->buf_size); if (n <= 0) { if (BIO_should_retry(rctx->req)) return -1; ERR_raise(ERR_LIB_HTTP, HTTP_R_FAILED_READING_DATA); return 0; } rctx->pos = rctx->buf; rctx->len_to_send = n; goto next_io; } if (OSSL_TRACE_ENABLED(HTTP)) OSSL_TRACE(HTTP, "]\n"); rctx->state = OHS_FLUSH; case OHS_FLUSH: i = BIO_flush(rctx->wbio); if (i > 0) { rctx->state = OHS_FIRSTLINE; goto next_io; } if (BIO_should_retry(rctx->wbio)) return -1; rctx->state = OHS_ERROR; return 0; case OHS_ERROR: return 0; case OHS_FIRSTLINE: case OHS_HEADERS: case OHS_REDIRECT: next_line: n = BIO_get_mem_data(rctx->mem, &p); if (n <= 0 || memchr(p, '\n', n) == 0) { if (n >= rctx->buf_size) { rctx->state = OHS_ERROR; return 0; } goto next_io; } n = BIO_gets(rctx->mem, buf, rctx->buf_size); if (n <= 0) { if (BIO_should_retry(rctx->mem)) goto next_io; rctx->state = OHS_ERROR; return 0; } resp_hdr_lines++; if (rctx->max_hdr_lines != 0 && rctx->max_hdr_lines < resp_hdr_lines) { ERR_raise(ERR_LIB_HTTP, HTTP_R_RESPONSE_TOO_MANY_HDRLINES); OSSL_TRACE(HTTP, "Received too many headers\n"); rctx->state = OHS_ERROR; return 0; } if (n == rctx->buf_size) { ERR_raise(ERR_LIB_HTTP, HTTP_R_RESPONSE_LINE_TOO_LONG); rctx->state = OHS_ERROR; return 0; } if (OSSL_TRACE_ENABLED(HTTP)) { if (rctx->state == OHS_FIRSTLINE) OSSL_TRACE(HTTP, "Received response header: [\n"); OSSL_TRACE1(HTTP, "%s", buf); } if (rctx->state == OHS_FIRSTLINE) { switch (parse_http_line1(buf, &found_keep_alive)) { case HTTP_STATUS_CODE_OK: rctx->state = OHS_HEADERS; goto next_line; case HTTP_STATUS_CODE_MOVED_PERMANENTLY: case HTTP_STATUS_CODE_FOUND: if (!rctx->method_POST) { rctx->state = OHS_REDIRECT; goto next_line; } ERR_raise(ERR_LIB_HTTP, HTTP_R_REDIRECTION_NOT_ENABLED); default: rctx->state = OHS_HEADERS_ERROR; goto next_line; } } key = buf; value = strchr(key, ':'); if (value != NULL) { *(value++) = '\0'; while (ossl_isspace(*value)) value++; line_end = strchr(value, '\r'); if (line_end == NULL) line_end = strchr(value, '\n'); if (line_end != NULL) *line_end = '\0'; } if (value != NULL && line_end != NULL) { if (rctx->state == OHS_REDIRECT && OPENSSL_strcasecmp(key, "Location") == 0) { rctx->redirection_url = value; return 0; } if (OPENSSL_strcasecmp(key, "Content-Type") == 0) { got_text = OPENSSL_strncasecmp(value, "text/", 5) == 0; if (rctx->state == OHS_HEADERS && rctx->expected_ct != NULL) { const char *semicolon; if (OPENSSL_strcasecmp(rctx->expected_ct, value) != 0 && (strchr(rctx->expected_ct, ';') != NULL || (semicolon = strchr(value, ';')) == NULL || (size_t)(semicolon - value) != strlen(rctx->expected_ct) || OPENSSL_strncasecmp(rctx->expected_ct, value, semicolon - value) != 0)) { ERR_raise_data(ERR_LIB_HTTP, HTTP_R_UNEXPECTED_CONTENT_TYPE, "expected=%s, actual=%s", rctx->expected_ct, value); return 0; } found_expected_ct = 1; } } if (OPENSSL_strcasecmp(key, "Connection") == 0) { if (OPENSSL_strcasecmp(value, "keep-alive") == 0) found_keep_alive = 1; else if (OPENSSL_strcasecmp(value, "close") == 0) found_keep_alive = 0; } else if (OPENSSL_strcasecmp(key, "Content-Length") == 0) { resp_len = (size_t)strtoul(value, &line_end, 10); if (line_end == value || *line_end != '\0') { ERR_raise_data(ERR_LIB_HTTP, HTTP_R_ERROR_PARSING_CONTENT_LENGTH, "input=%s", value); return 0; } if (!check_set_resp_len(rctx, resp_len)) return 0; } } for (p = rctx->buf; *p != '\0'; p++) { if (*p != '\r' && *p != '\n') break; } if (*p != '\0') goto next_line; if (OSSL_TRACE_ENABLED(HTTP)) OSSL_TRACE(HTTP, "]\n"); resp_hdr_lines = 0; if (rctx->keep_alive != 0 && !found_keep_alive ) { if (rctx->keep_alive == 2) { rctx->keep_alive = 0; ERR_raise(ERR_LIB_HTTP, HTTP_R_SERVER_CANCELED_CONNECTION); return 0; } rctx->keep_alive = 0; } if (rctx->state == OHS_HEADERS_ERROR) { if (OSSL_TRACE_ENABLED(HTTP)) { int printed_final_nl = 0; OSSL_TRACE(HTTP, "Received error response body: [\n"); while ((n = BIO_read(rctx->rbio, rctx->buf, rctx->buf_size)) > 0 || (OSSL_sleep(100), BIO_should_retry(rctx->rbio))) { OSSL_TRACE_STRING(HTTP, got_text, 1, rctx->buf, n); if (n > 0) printed_final_nl = rctx->buf[n - 1] == '\n'; } OSSL_TRACE1(HTTP, "%s]\n", printed_final_nl ? "" : "\n"); (void)printed_final_nl; } return 0; } if (rctx->expected_ct != NULL && !found_expected_ct) { ERR_raise_data(ERR_LIB_HTTP, HTTP_R_MISSING_CONTENT_TYPE, "expected=%s", rctx->expected_ct); return 0; } if (rctx->state == OHS_REDIRECT) { ERR_raise(ERR_LIB_HTTP, HTTP_R_MISSING_REDIRECT_LOCATION); return 0; } if (!rctx->expect_asn1) { rctx->state = OHS_STREAM; return 1; } rctx->state = OHS_ASN1_HEADER; case OHS_ASN1_HEADER: n = BIO_get_mem_data(rctx->mem, &p); if (n < 2) goto next_io; if (*p++ != (V_ASN1_SEQUENCE | V_ASN1_CONSTRUCTED)) { ERR_raise(ERR_LIB_HTTP, HTTP_R_MISSING_ASN1_ENCODING); return 0; } if ((*p & 0x80) != 0) { if (n < 6) goto next_io; n = *p & 0x7F; if (n == 0 || (n > 4)) { ERR_raise(ERR_LIB_HTTP, HTTP_R_ERROR_PARSING_ASN1_LENGTH); return 0; } p++; resp_len = 0; for (i = 0; i < n; i++) { resp_len <<= 8; resp_len |= *p++; } resp_len += n + 2; } else { resp_len = *p + 2; } if (!check_set_resp_len(rctx, resp_len)) return 0; rctx->state = OHS_ASN1_CONTENT; case OHS_ASN1_CONTENT: default: n = BIO_get_mem_data(rctx->mem, NULL); if (n < 0 || (size_t)n < rctx->resp_len) goto next_io; rctx->state = OHS_ASN1_DONE; return 1; } } int OSSL_HTTP_REQ_CTX_nbio_d2i(OSSL_HTTP_REQ_CTX *rctx, ASN1_VALUE **pval, const ASN1_ITEM *it) { const unsigned char *p; int rv; *pval = NULL; if ((rv = OSSL_HTTP_REQ_CTX_nbio(rctx)) != 1) return rv; *pval = ASN1_item_d2i(NULL, &p, BIO_get_mem_data(rctx->mem, &p), it); return *pval != NULL; } #ifndef OPENSSL_NO_SOCK static BIO *http_new_bio(const char *server , const char *server_port , int use_ssl, const char *proxy , const char *proxy_port ) { const char *host = server; const char *port = server_port; BIO *cbio; if (!ossl_assert(server != NULL)) return NULL; if (proxy != NULL) { host = proxy; port = proxy_port; } if (port == NULL && strchr(host, ':') == NULL) port = use_ssl ? OSSL_HTTPS_PORT : OSSL_HTTP_PORT; cbio = BIO_new_connect(host ); if (cbio == NULL) goto end; if (port != NULL) (void)BIO_set_conn_port(cbio, port); end: return cbio; } #endif BIO *OSSL_HTTP_REQ_CTX_exchange(OSSL_HTTP_REQ_CTX *rctx) { int rv; if (rctx == NULL) { ERR_raise(ERR_LIB_HTTP, ERR_R_PASSED_NULL_PARAMETER); return NULL; } for (;;) { rv = OSSL_HTTP_REQ_CTX_nbio(rctx); if (rv != -1) break; if (BIO_wait(rctx->rbio, rctx->max_time, 100 ) <= 0) return NULL; } if (rv == 0) { if (rctx->redirection_url == NULL) { if (rctx->len_to_send > 0) ERR_raise(ERR_LIB_HTTP, HTTP_R_ERROR_SENDING); else ERR_raise(ERR_LIB_HTTP, HTTP_R_ERROR_RECEIVING); } return NULL; } return rctx->state == OHS_STREAM ? rctx->rbio : rctx->mem; } int OSSL_HTTP_is_alive(const OSSL_HTTP_REQ_CTX *rctx) { return rctx != NULL && rctx->keep_alive != 0; } OSSL_HTTP_REQ_CTX *OSSL_HTTP_open(const char *server, const char *port, const char *proxy, const char *no_proxy, int use_ssl, BIO *bio, BIO *rbio, OSSL_HTTP_bio_cb_t bio_update_fn, void *arg, int buf_size, int overall_timeout) { BIO *cbio; OSSL_HTTP_REQ_CTX *rctx = NULL; if (use_ssl && bio_update_fn == NULL) { ERR_raise(ERR_LIB_HTTP, HTTP_R_TLS_NOT_ENABLED); return NULL; } if (rbio != NULL && (bio == NULL || bio_update_fn != NULL)) { ERR_raise(ERR_LIB_HTTP, ERR_R_PASSED_INVALID_ARGUMENT); return NULL; } if (bio != NULL) { cbio = bio; if (proxy != NULL || no_proxy != NULL) { ERR_raise(ERR_LIB_HTTP, ERR_R_PASSED_INVALID_ARGUMENT); return NULL; } } else { #ifndef OPENSSL_NO_SOCK char *proxy_host = NULL, *proxy_port = NULL; if (server == NULL) { ERR_raise(ERR_LIB_HTTP, ERR_R_PASSED_NULL_PARAMETER); return NULL; } if (port != NULL && *port == '\0') port = NULL; if (port == NULL && strchr(server, ':') == NULL) port = use_ssl ? OSSL_HTTPS_PORT : OSSL_HTTP_PORT; proxy = OSSL_HTTP_adapt_proxy(proxy, no_proxy, server, use_ssl); if (proxy != NULL && !OSSL_HTTP_parse_url(proxy, NULL , NULL , &proxy_host, &proxy_port, NULL , NULL , NULL, NULL)) return NULL; cbio = http_new_bio(server, port, use_ssl, proxy_host, proxy_port); OPENSSL_free(proxy_host); OPENSSL_free(proxy_port); if (cbio == NULL) return NULL; #else ERR_raise(ERR_LIB_HTTP, HTTP_R_SOCK_NOT_SUPPORTED); return NULL; #endif } (void)ERR_set_mark(); if (rbio == NULL && BIO_do_connect_retry(cbio, overall_timeout, -1) <= 0) { if (bio == NULL) BIO_free_all(cbio); goto end; } if (bio_update_fn != NULL) { BIO *orig_bio = cbio; cbio = (*bio_update_fn)(cbio, arg, 1 , use_ssl != 0); if (cbio == NULL) { if (bio == NULL) BIO_free_all(orig_bio); goto end; } } rctx = http_req_ctx_new(bio == NULL, cbio, rbio != NULL ? rbio : cbio, bio_update_fn, arg, use_ssl, proxy, server, port, buf_size, overall_timeout); end: if (rctx != NULL) (void)ERR_pop_to_mark(); else (void)ERR_clear_last_mark(); return rctx; } int OSSL_HTTP_set1_request(OSSL_HTTP_REQ_CTX *rctx, const char *path, const STACK_OF(CONF_VALUE) *headers, const char *content_type, BIO *req, const char *expected_content_type, int expect_asn1, size_t max_resp_len, int timeout, int keep_alive) { int use_http_proxy; if (rctx == NULL) { ERR_raise(ERR_LIB_HTTP, ERR_R_PASSED_NULL_PARAMETER); return 0; } use_http_proxy = rctx->proxy != NULL && !rctx->use_ssl; if (use_http_proxy && rctx->server == NULL) { ERR_raise(ERR_LIB_HTTP, ERR_R_PASSED_INVALID_ARGUMENT); return 0; } rctx->max_resp_len = max_resp_len; return OSSL_HTTP_REQ_CTX_set_request_line(rctx, req != NULL, use_http_proxy ? rctx->server : NULL, rctx->port, path) && add1_headers(rctx, headers, rctx->server) && OSSL_HTTP_REQ_CTX_set_expected(rctx, expected_content_type, expect_asn1, timeout, keep_alive) && set1_content(rctx, content_type, req); } BIO *OSSL_HTTP_exchange(OSSL_HTTP_REQ_CTX *rctx, char **redirection_url) { BIO *resp; if (rctx == NULL) { ERR_raise(ERR_LIB_HTTP, ERR_R_PASSED_NULL_PARAMETER); return NULL; } if (redirection_url != NULL) *redirection_url = NULL; resp = OSSL_HTTP_REQ_CTX_exchange(rctx); if (resp == NULL) { if (rctx->redirection_url != NULL) { if (redirection_url == NULL) ERR_raise(ERR_LIB_HTTP, HTTP_R_REDIRECTION_NOT_ENABLED); else *redirection_url = OPENSSL_strdup(rctx->redirection_url); } else { char buf[200]; unsigned long err = ERR_peek_error(); int lib = ERR_GET_LIB(err); int reason = ERR_GET_REASON(err); if (lib == ERR_LIB_SSL || lib == ERR_LIB_HTTP || (lib == ERR_LIB_BIO && reason == BIO_R_CONNECT_TIMEOUT) || (lib == ERR_LIB_BIO && reason == BIO_R_CONNECT_ERROR) #ifndef OPENSSL_NO_CMP || (lib == ERR_LIB_CMP && reason == CMP_R_POTENTIALLY_INVALID_CERTIFICATE) #endif ) { if (rctx->server != NULL) { BIO_snprintf(buf, sizeof(buf), "server=http%s: rctx->use_ssl ? "s" : "", rctx->server, rctx->port != NULL ? ":" : "", rctx->port != NULL ? rctx->port : ""); ERR_add_error_data(1, buf); } if (rctx->proxy != NULL) ERR_add_error_data(2, " proxy=", rctx->proxy); if (err == 0) { BIO_snprintf(buf, sizeof(buf), " peer has disconnected%s", rctx->use_ssl ? " violating the protocol" : ", likely because it requires the use of TLS"); ERR_add_error_data(1, buf); } } } } if (resp != NULL && !BIO_up_ref(resp)) resp = NULL; return resp; } static int redirection_ok(int n_redir, const char *old_url, const char *new_url) { if (n_redir >= HTTP_VERSION_MAX_REDIRECTIONS) { ERR_raise(ERR_LIB_HTTP, HTTP_R_TOO_MANY_REDIRECTIONS); return 0; } if (*new_url == '/') return 1; if (HAS_PREFIX(old_url, OSSL_HTTPS_NAME":") && !HAS_PREFIX(new_url, OSSL_HTTPS_NAME":")) { ERR_raise(ERR_LIB_HTTP, HTTP_R_REDIRECTION_FROM_HTTPS_TO_HTTP); return 0; } return 1; } BIO *OSSL_HTTP_get(const char *url, const char *proxy, const char *no_proxy, BIO *bio, BIO *rbio, OSSL_HTTP_bio_cb_t bio_update_fn, void *arg, int buf_size, const STACK_OF(CONF_VALUE) *headers, const char *expected_ct, int expect_asn1, size_t max_resp_len, int timeout) { char *current_url; int n_redirs = 0; char *host; char *port; char *path; int use_ssl; BIO *resp = NULL; time_t max_time = timeout > 0 ? time(NULL) + timeout : 0; if (url == NULL) { ERR_raise(ERR_LIB_HTTP, ERR_R_PASSED_NULL_PARAMETER); return NULL; } if ((current_url = OPENSSL_strdup(url)) == NULL) return NULL; for (;;) { OSSL_HTTP_REQ_CTX *rctx; char *redirection_url; if (!OSSL_HTTP_parse_url(current_url, &use_ssl, NULL , &host, &port, NULL , &path, NULL, NULL)) break; rctx = OSSL_HTTP_open(host, port, proxy, no_proxy, use_ssl, bio, rbio, bio_update_fn, arg, buf_size, timeout); new_rpath: redirection_url = NULL; if (rctx != NULL) { if (!OSSL_HTTP_set1_request(rctx, path, headers, NULL , NULL , expected_ct, expect_asn1, max_resp_len, -1 , 0 )) { OSSL_HTTP_REQ_CTX_free(rctx); rctx = NULL; } else { resp = OSSL_HTTP_exchange(rctx, &redirection_url); } } OPENSSL_free(path); if (resp == NULL && redirection_url != NULL) { if (redirection_ok(++n_redirs, current_url, redirection_url) && may_still_retry(max_time, &timeout)) { (void)BIO_reset(bio); OPENSSL_free(current_url); current_url = redirection_url; if (*redirection_url == '/') { path = OPENSSL_strdup(redirection_url); if (path == NULL) { OPENSSL_free(host); OPENSSL_free(port); (void)OSSL_HTTP_close(rctx, 1); BIO_free(resp); OPENSSL_free(current_url); return NULL; } goto new_rpath; } OPENSSL_free(host); OPENSSL_free(port); (void)OSSL_HTTP_close(rctx, 1); continue; } OPENSSL_free(redirection_url); } OPENSSL_free(host); OPENSSL_free(port); if (!OSSL_HTTP_close(rctx, resp != NULL)) { BIO_free(resp); resp = NULL; } break; } OPENSSL_free(current_url); return resp; } BIO *OSSL_HTTP_transfer(OSSL_HTTP_REQ_CTX **prctx, const char *server, const char *port, const char *path, int use_ssl, const char *proxy, const char *no_proxy, BIO *bio, BIO *rbio, OSSL_HTTP_bio_cb_t bio_update_fn, void *arg, int buf_size, const STACK_OF(CONF_VALUE) *headers, const char *content_type, BIO *req, const char *expected_ct, int expect_asn1, size_t max_resp_len, int timeout, int keep_alive) { OSSL_HTTP_REQ_CTX *rctx = prctx == NULL ? NULL : *prctx; BIO *resp = NULL; if (rctx == NULL) { rctx = OSSL_HTTP_open(server, port, proxy, no_proxy, use_ssl, bio, rbio, bio_update_fn, arg, buf_size, timeout); timeout = -1; } if (rctx != NULL) { if (OSSL_HTTP_set1_request(rctx, path, headers, content_type, req, expected_ct, expect_asn1, max_resp_len, timeout, keep_alive)) resp = OSSL_HTTP_exchange(rctx, NULL); if (resp == NULL || !OSSL_HTTP_is_alive(rctx)) { if (!OSSL_HTTP_close(rctx, resp != NULL)) { BIO_free(resp); resp = NULL; } rctx = NULL; } } if (prctx != NULL) *prctx = rctx; return resp; } int OSSL_HTTP_close(OSSL_HTTP_REQ_CTX *rctx, int ok) { BIO *wbio; int ret = 1; if (rctx != NULL && rctx->upd_fn != NULL) { wbio = (*rctx->upd_fn)(rctx->wbio, rctx->upd_arg, 0 , ok); ret = wbio != NULL; if (ret) rctx->wbio = wbio; } OSSL_HTTP_REQ_CTX_free(rctx); return ret; } static char *base64encode(const void *buf, size_t len) { int i; size_t outl; char *out; outl = (len / 3); if (len % 3 > 0) outl++; outl <<= 2; out = OPENSSL_malloc(outl + 1); if (out == NULL) return 0; i = EVP_EncodeBlock((unsigned char *)out, buf, len); if (!ossl_assert(0 <= i && (size_t)i <= outl)) { OPENSSL_free(out); return NULL; } return out; } int OSSL_HTTP_proxy_connect(BIO *bio, const char *server, const char *port, const char *proxyuser, const char *proxypass, int timeout, BIO *bio_err, const char *prog) { #undef BUF_SIZE #define BUF_SIZE (8 * 1024) char *mbuf = OPENSSL_malloc(BUF_SIZE); char *mbufp; int read_len = 0; int ret = 0; BIO *fbio = BIO_new(BIO_f_buffer()); int rv; time_t max_time = timeout > 0 ? time(NULL) + timeout : 0; if (bio == NULL || server == NULL || (bio_err != NULL && prog == NULL)) { ERR_raise(ERR_LIB_HTTP, ERR_R_PASSED_NULL_PARAMETER); goto end; } if (port == NULL || *port == '\0') port = OSSL_HTTPS_PORT; if (mbuf == NULL || fbio == NULL) { BIO_printf(bio_err , "%s: out of memory", prog); goto end; } BIO_push(fbio, bio); BIO_printf(fbio, "CONNECT %s:%s "HTTP_1_0"\r\n", server, port); BIO_printf(fbio, "Proxy-Connection: Keep-Alive\r\n"); if (proxyuser != NULL) { size_t len = strlen(proxyuser) + 1; char *proxyauth, *proxyauthenc = NULL; if (proxypass != NULL) len += strlen(proxypass); proxyauth = OPENSSL_malloc(len + 1); if (proxyauth == NULL) goto end; if (BIO_snprintf(proxyauth, len + 1, "%s:%s", proxyuser, proxypass != NULL ? proxypass : "") != (int)len) goto proxy_end; proxyauthenc = base64encode(proxyauth, len); if (proxyauthenc != NULL) { BIO_printf(fbio, "Proxy-Authorization: Basic %s\r\n", proxyauthenc); OPENSSL_clear_free(proxyauthenc, strlen(proxyauthenc)); } proxy_end: OPENSSL_clear_free(proxyauth, len); if (proxyauthenc == NULL) goto end; } BIO_printf(fbio, "\r\n"); for (;;) { if (BIO_flush(fbio) != 0) break; if (!BIO_should_retry(fbio)) break; } for (;;) { rv = BIO_wait(fbio, max_time, 100 ); if (rv <= 0) { BIO_printf(bio_err, "%s: HTTP CONNECT %s\n", prog, rv == 0 ? "timed out" : "failed waiting for data"); goto end; } read_len = BIO_gets(fbio, mbuf, BUF_SIZE); if (read_len < (int)HTTP_LINE1_MINLEN) continue; mbufp = mbuf; if (!CHECK_AND_SKIP_PREFIX(mbufp, HTTP_PREFIX)) { ERR_raise(ERR_LIB_HTTP, HTTP_R_HEADER_PARSE_ERROR); BIO_printf(bio_err, "%s: HTTP CONNECT failed, non-HTTP response\n", prog); goto end; } if (!HAS_PREFIX(mbufp, HTTP_VERSION_PATT)) { ERR_raise(ERR_LIB_HTTP, HTTP_R_RECEIVED_WRONG_HTTP_VERSION); BIO_printf(bio_err, "%s: HTTP CONNECT failed, bad HTTP version %.*s\n", prog, (int)HTTP_VERSION_STR_LEN, mbufp); goto end; } mbufp += HTTP_VERSION_STR_LEN; if (!HAS_PREFIX(mbufp, " 2")) { if (ossl_isspace(*mbufp)) mbufp++; while (read_len > 0 && ossl_isspace(mbuf[read_len - 1])) read_len--; mbuf[read_len] = '\0'; ERR_raise_data(ERR_LIB_HTTP, HTTP_R_CONNECT_FAILURE, "reason=%s", mbufp); BIO_printf(bio_err, "%s: HTTP CONNECT failed, reason=%s\n", prog, mbufp); goto end; } ret = 1; break; } do { read_len = BIO_gets(fbio, mbuf, BUF_SIZE); } while (read_len > 2); end: if (fbio != NULL) { (void)BIO_flush(fbio); BIO_pop(fbio); BIO_free(fbio); } OPENSSL_free(mbuf); return ret; #undef BUF_SIZE }
http
openssl/crypto/http/http_client.c
openssl
#include <stdio.h> #include <string.h> #include <openssl/http.h> #include <openssl/httperr.h> #include <openssl/bio.h> #include <openssl/err.h> #include "internal/cryptlib.h" static void init_pstring(char **pstr) { if (pstr != NULL) { *pstr = NULL; } } static void init_pint(int *pint) { if (pint != NULL) { *pint = 0; } } static int copy_substring(char **dest, const char *start, const char *end) { return dest == NULL || (*dest = OPENSSL_strndup(start, end - start)) != NULL; } static void free_pstring(char **pstr) { if (pstr != NULL) { OPENSSL_free(*pstr); *pstr = NULL; } } int OSSL_parse_url(const char *url, char **pscheme, char **puser, char **phost, char **pport, int *pport_num, char **ppath, char **pquery, char **pfrag) { const char *p, *tmp; const char *scheme, *scheme_end; const char *user, *user_end; const char *host, *host_end; const char *port, *port_end; unsigned int portnum; const char *path, *path_end; const char *query, *query_end; const char *frag, *frag_end; init_pstring(pscheme); init_pstring(puser); init_pstring(phost); init_pstring(pport); init_pint(pport_num); init_pstring(ppath); init_pstring(pfrag); init_pstring(pquery); if (url == NULL) { ERR_raise(ERR_LIB_HTTP, ERR_R_PASSED_NULL_PARAMETER); return 0; } scheme = scheme_end = url; p = strstr(url, ": if (p == NULL) { p = url; } else { scheme_end = p; if (scheme_end == scheme) goto parse_err; p += strlen(": } user = user_end = host = p; host = strchr(p, '@'); if (host != NULL) user_end = host++; else host = p; if (host[0] == '[') { host_end = strchr(host + 1, ']'); if (host_end == NULL) goto parse_err; p = ++host_end; } else { host_end = strchr(host, ':'); if (host_end == NULL) host_end = strchr(host, '/'); if (host_end == NULL) host_end = strchr(host, '?'); if (host_end == NULL) host_end = strchr(host, '#'); if (host_end == NULL) host_end = host + strlen(host); p = host_end; } port = "0"; if (*p == ':') port = ++p; if (sscanf(port, "%u", &portnum) <= 0 || portnum > 65535) { ERR_raise_data(ERR_LIB_HTTP, HTTP_R_INVALID_PORT_NUMBER, "%s", port); goto err; } for (port_end = port; '0' <= *port_end && *port_end <= '9'; port_end++) ; if (port == p) p += port_end - port; path = p; if (*path != '\0' && *path != '/' && *path != '?' && *path != '#') { ERR_raise(ERR_LIB_HTTP, HTTP_R_INVALID_URL_PATH); goto parse_err; } path_end = query = query_end = frag = frag_end = path + strlen(path); tmp = strchr(p, '?'); if (tmp != NULL) { p = tmp; if (pquery != NULL) { path_end = p; query = p + 1; } } tmp = strchr(p, '#'); if (tmp != NULL) { if (query == path_end) path_end = tmp; query_end = tmp; frag = tmp + 1; } if (!copy_substring(pscheme, scheme, scheme_end) || !copy_substring(phost, host, host_end) || !copy_substring(pport, port, port_end) || !copy_substring(puser, user, user_end) || !copy_substring(pquery, query, query_end) || !copy_substring(pfrag, frag, frag_end)) goto err; if (pport_num != NULL) *pport_num = (int)portnum; if (*path == '/') { if (!copy_substring(ppath, path, path_end)) goto err; } else if (ppath != NULL) { size_t buflen = 1 + path_end - path + 1; if ((*ppath = OPENSSL_malloc(buflen)) == NULL) goto err; BIO_snprintf(*ppath, buflen, "/%s", path); } return 1; parse_err: ERR_raise(ERR_LIB_HTTP, HTTP_R_ERROR_PARSING_URL); err: free_pstring(pscheme); free_pstring(puser); free_pstring(phost); free_pstring(pport); free_pstring(ppath); free_pstring(pquery); free_pstring(pfrag); return 0; } int OSSL_HTTP_parse_url(const char *url, int *pssl, char **puser, char **phost, char **pport, int *pport_num, char **ppath, char **pquery, char **pfrag) { char *scheme, *port; int ssl = 0, portnum; init_pstring(pport); if (pssl != NULL) *pssl = 0; if (!OSSL_parse_url(url, &scheme, puser, phost, &port, pport_num, ppath, pquery, pfrag)) return 0; if (strcmp(scheme, OSSL_HTTPS_NAME) == 0) { ssl = 1; if (pssl != NULL) *pssl = ssl; } else if (*scheme != '\0' && strcmp(scheme, OSSL_HTTP_NAME) != 0) { ERR_raise(ERR_LIB_HTTP, HTTP_R_INVALID_URL_SCHEME); OPENSSL_free(scheme); OPENSSL_free(port); goto err; } OPENSSL_free(scheme); if (strcmp(port, "0") == 0) { OPENSSL_free(port); port = ssl ? OSSL_HTTPS_PORT : OSSL_HTTP_PORT; if (!ossl_assert(sscanf(port, "%d", &portnum) == 1)) goto err; if (pport_num != NULL) *pport_num = portnum; if (pport != NULL) { *pport = OPENSSL_strdup(port); if (*pport == NULL) goto err; } } else { if (pport != NULL) *pport = port; else OPENSSL_free(port); } return 1; err: free_pstring(puser); free_pstring(phost); free_pstring(ppath); free_pstring(pquery); free_pstring(pfrag); return 0; } static int use_proxy(const char *no_proxy, const char *server) { size_t sl; const char *found = NULL; if (!ossl_assert(server != NULL)) return 0; sl = strlen(server); if (no_proxy == NULL) no_proxy = ossl_safe_getenv("no_proxy"); if (no_proxy == NULL) no_proxy = ossl_safe_getenv(OPENSSL_NO_PROXY); if (no_proxy != NULL) found = strstr(no_proxy, server); while (found != NULL && ((found != no_proxy && found[-1] != ' ' && found[-1] != ',') || (found[sl] != '\0' && found[sl] != ' ' && found[sl] != ','))) found = strstr(found + 1, server); return found == NULL; } const char *OSSL_HTTP_adapt_proxy(const char *proxy, const char *no_proxy, const char *server, int use_ssl) { if (proxy == NULL) proxy = ossl_safe_getenv(use_ssl ? "https_proxy" : "http_proxy"); if (proxy == NULL) proxy = ossl_safe_getenv(use_ssl ? OPENSSL_HTTP_PROXY : OPENSSL_HTTPS_PROXY); if (proxy == NULL || *proxy == '\0' || !use_proxy(no_proxy, server)) return NULL; return proxy; }
http
openssl/crypto/http/http_lib.c
openssl
#ifndef _GNU_SOURCE # define _GNU_SOURCE #endif #undef UNICODE #include <assert.h> #include <string.h> #include "bio_local.h" #include <openssl/crypto.h> #ifndef OPENSSL_NO_SOCK #include <openssl/err.h> #include <openssl/buffer.h> #include "internal/thread_once.h" CRYPTO_RWLOCK *bio_lookup_lock; static CRYPTO_ONCE bio_lookup_init = CRYPTO_ONCE_STATIC_INIT; BIO_ADDR *BIO_ADDR_new(void) { BIO_ADDR *ret = OPENSSL_zalloc(sizeof(*ret)); if (ret == NULL) return NULL; ret->sa.sa_family = AF_UNSPEC; return ret; } void BIO_ADDR_free(BIO_ADDR *ap) { OPENSSL_free(ap); } int BIO_ADDR_copy(BIO_ADDR *dst, const BIO_ADDR *src) { if (dst == NULL || src == NULL) return 0; if (src->sa.sa_family == AF_UNSPEC) { BIO_ADDR_clear(dst); return 1; } return BIO_ADDR_make(dst, &src->sa); } BIO_ADDR *BIO_ADDR_dup(const BIO_ADDR *ap) { BIO_ADDR *ret = NULL; if (ap != NULL) { ret = BIO_ADDR_new(); if (ret != NULL && !BIO_ADDR_copy(ret, ap)) { BIO_ADDR_free(ret); ret = NULL; } } return ret; } void BIO_ADDR_clear(BIO_ADDR *ap) { memset(ap, 0, sizeof(*ap)); ap->sa.sa_family = AF_UNSPEC; } int BIO_ADDR_make(BIO_ADDR *ap, const struct sockaddr *sa) { if (sa->sa_family == AF_INET) { memcpy(&(ap->s_in), sa, sizeof(struct sockaddr_in)); return 1; } #if OPENSSL_USE_IPV6 if (sa->sa_family == AF_INET6) { memcpy(&(ap->s_in6), sa, sizeof(struct sockaddr_in6)); return 1; } #endif #ifndef OPENSSL_NO_UNIX_SOCK if (sa->sa_family == AF_UNIX) { memcpy(&(ap->s_un), sa, sizeof(struct sockaddr_un)); return 1; } #endif return 0; } int BIO_ADDR_rawmake(BIO_ADDR *ap, int family, const void *where, size_t wherelen, unsigned short port) { #ifndef OPENSSL_NO_UNIX_SOCK if (family == AF_UNIX) { if (wherelen + 1 > sizeof(ap->s_un.sun_path)) return 0; memset(&ap->s_un, 0, sizeof(ap->s_un)); ap->s_un.sun_family = family; strncpy(ap->s_un.sun_path, where, sizeof(ap->s_un.sun_path) - 1); return 1; } #endif if (family == AF_INET) { if (wherelen != sizeof(struct in_addr)) return 0; memset(&ap->s_in, 0, sizeof(ap->s_in)); ap->s_in.sin_family = family; ap->s_in.sin_port = port; ap->s_in.sin_addr = *(struct in_addr *)where; return 1; } #if OPENSSL_USE_IPV6 if (family == AF_INET6) { if (wherelen != sizeof(struct in6_addr)) return 0; memset(&ap->s_in6, 0, sizeof(ap->s_in6)); ap->s_in6.sin6_family = family; ap->s_in6.sin6_port = port; ap->s_in6.sin6_addr = *(struct in6_addr *)where; return 1; } #endif return 0; } int BIO_ADDR_family(const BIO_ADDR *ap) { return ap->sa.sa_family; } int BIO_ADDR_rawaddress(const BIO_ADDR *ap, void *p, size_t *l) { size_t len = 0; const void *addrptr = NULL; if (ap->sa.sa_family == AF_INET) { len = sizeof(ap->s_in.sin_addr); addrptr = &ap->s_in.sin_addr; } #if OPENSSL_USE_IPV6 else if (ap->sa.sa_family == AF_INET6) { len = sizeof(ap->s_in6.sin6_addr); addrptr = &ap->s_in6.sin6_addr; } #endif #ifndef OPENSSL_NO_UNIX_SOCK else if (ap->sa.sa_family == AF_UNIX) { len = strlen(ap->s_un.sun_path); addrptr = &ap->s_un.sun_path; } #endif if (addrptr == NULL) return 0; if (p != NULL) { memcpy(p, addrptr, len); } if (l != NULL) *l = len; return 1; } unsigned short BIO_ADDR_rawport(const BIO_ADDR *ap) { if (ap->sa.sa_family == AF_INET) return ap->s_in.sin_port; #if OPENSSL_USE_IPV6 if (ap->sa.sa_family == AF_INET6) return ap->s_in6.sin6_port; #endif return 0; } static int addr_strings(const BIO_ADDR *ap, int numeric, char **hostname, char **service) { if (BIO_sock_init() != 1) return 0; if (1) { #ifdef AI_PASSIVE int ret = 0; char host[NI_MAXHOST] = "", serv[NI_MAXSERV] = ""; int flags = 0; if (numeric) flags |= NI_NUMERICHOST | NI_NUMERICSERV; if ((ret = getnameinfo(BIO_ADDR_sockaddr(ap), BIO_ADDR_sockaddr_size(ap), host, sizeof(host), serv, sizeof(serv), flags)) != 0) { # ifdef EAI_SYSTEM if (ret == EAI_SYSTEM) { ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(), "calling getnameinfo()"); } else # endif { ERR_raise_data(ERR_LIB_BIO, ERR_R_SYS_LIB, gai_strerror(ret)); } return 0; } if (serv[0] == '\0') { BIO_snprintf(serv, sizeof(serv), "%d", ntohs(BIO_ADDR_rawport(ap))); } if (hostname != NULL) *hostname = OPENSSL_strdup(host); if (service != NULL) *service = OPENSSL_strdup(serv); } else { #endif if (hostname != NULL) *hostname = OPENSSL_strdup(inet_ntoa(ap->s_in.sin_addr)); if (service != NULL) { char serv[6]; BIO_snprintf(serv, sizeof(serv), "%d", ntohs(ap->s_in.sin_port)); *service = OPENSSL_strdup(serv); } } if ((hostname != NULL && *hostname == NULL) || (service != NULL && *service == NULL)) { if (hostname != NULL) { OPENSSL_free(*hostname); *hostname = NULL; } if (service != NULL) { OPENSSL_free(*service); *service = NULL; } return 0; } return 1; } char *BIO_ADDR_hostname_string(const BIO_ADDR *ap, int numeric) { char *hostname = NULL; if (addr_strings(ap, numeric, &hostname, NULL)) return hostname; return NULL; } char *BIO_ADDR_service_string(const BIO_ADDR *ap, int numeric) { char *service = NULL; if (addr_strings(ap, numeric, NULL, &service)) return service; return NULL; } char *BIO_ADDR_path_string(const BIO_ADDR *ap) { #ifndef OPENSSL_NO_UNIX_SOCK if (ap->sa.sa_family == AF_UNIX) return OPENSSL_strdup(ap->s_un.sun_path); #endif return NULL; } const struct sockaddr *BIO_ADDR_sockaddr(const BIO_ADDR *ap) { return &(ap->sa); } struct sockaddr *BIO_ADDR_sockaddr_noconst(BIO_ADDR *ap) { return &(ap->sa); } socklen_t BIO_ADDR_sockaddr_size(const BIO_ADDR *ap) { if (ap->sa.sa_family == AF_INET) return sizeof(ap->s_in); #if OPENSSL_USE_IPV6 if (ap->sa.sa_family == AF_INET6) return sizeof(ap->s_in6); #endif #ifndef OPENSSL_NO_UNIX_SOCK if (ap->sa.sa_family == AF_UNIX) return sizeof(ap->s_un); #endif return sizeof(*ap); } const BIO_ADDRINFO *BIO_ADDRINFO_next(const BIO_ADDRINFO *bai) { if (bai != NULL) return bai->bai_next; return NULL; } int BIO_ADDRINFO_family(const BIO_ADDRINFO *bai) { if (bai != NULL) return bai->bai_family; return 0; } int BIO_ADDRINFO_socktype(const BIO_ADDRINFO *bai) { if (bai != NULL) return bai->bai_socktype; return 0; } int BIO_ADDRINFO_protocol(const BIO_ADDRINFO *bai) { if (bai != NULL) { if (bai->bai_protocol != 0) return bai->bai_protocol; #ifndef OPENSSL_NO_UNIX_SOCK if (bai->bai_family == AF_UNIX) return 0; #endif switch (bai->bai_socktype) { case SOCK_STREAM: return IPPROTO_TCP; case SOCK_DGRAM: return IPPROTO_UDP; default: break; } } return 0; } socklen_t BIO_ADDRINFO_sockaddr_size(const BIO_ADDRINFO *bai) { if (bai != NULL) return bai->bai_addrlen; return 0; } const struct sockaddr *BIO_ADDRINFO_sockaddr(const BIO_ADDRINFO *bai) { if (bai != NULL) return bai->bai_addr; return NULL; } const BIO_ADDR *BIO_ADDRINFO_address(const BIO_ADDRINFO *bai) { if (bai != NULL) return (BIO_ADDR *)bai->bai_addr; return NULL; } void BIO_ADDRINFO_free(BIO_ADDRINFO *bai) { if (bai == NULL) return; #ifdef AI_PASSIVE # ifndef OPENSSL_NO_UNIX_SOCK # define _cond bai->bai_family != AF_UNIX # else # define _cond 1 # endif if (_cond) { freeaddrinfo(bai); return; } #endif while (bai != NULL) { BIO_ADDRINFO *next = bai->bai_next; OPENSSL_free(bai->bai_addr); OPENSSL_free(bai); bai = next; } } int BIO_parse_hostserv(const char *hostserv, char **host, char **service, enum BIO_hostserv_priorities hostserv_prio) { const char *h = NULL; size_t hl = 0; const char *p = NULL; size_t pl = 0; if (*hostserv == '[') { if ((p = strchr(hostserv, ']')) == NULL) goto spec_err; h = hostserv + 1; hl = p - h; p++; if (*p == '\0') p = NULL; else if (*p != ':') goto spec_err; else { p++; pl = strlen(p); } } else { const char *p2 = strrchr(hostserv, ':'); p = strchr(hostserv, ':'); if (p != p2) goto amb_err; if (p != NULL) { h = hostserv; hl = p - h; p++; pl = strlen(p); } else if (hostserv_prio == BIO_PARSE_PRIO_HOST) { h = hostserv; hl = strlen(h); } else { p = hostserv; pl = strlen(p); } } if (p != NULL && strchr(p, ':')) goto spec_err; if (h != NULL && host != NULL) { if (hl == 0 || (hl == 1 && h[0] == '*')) { *host = NULL; } else { *host = OPENSSL_strndup(h, hl); if (*host == NULL) return 0; } } if (p != NULL && service != NULL) { if (pl == 0 || (pl == 1 && p[0] == '*')) { *service = NULL; } else { *service = OPENSSL_strndup(p, pl); if (*service == NULL) return 0; } } return 1; amb_err: ERR_raise(ERR_LIB_BIO, BIO_R_AMBIGUOUS_HOST_OR_SERVICE); return 0; spec_err: ERR_raise(ERR_LIB_BIO, BIO_R_MALFORMED_HOST_OR_SERVICE); return 0; } static int addrinfo_wrap(int family, int socktype, const void *where, size_t wherelen, unsigned short port, BIO_ADDRINFO **bai) { if ((*bai = OPENSSL_zalloc(sizeof(**bai))) == NULL) return 0; (*bai)->bai_family = family; (*bai)->bai_socktype = socktype; if (socktype == SOCK_STREAM) (*bai)->bai_protocol = IPPROTO_TCP; if (socktype == SOCK_DGRAM) (*bai)->bai_protocol = IPPROTO_UDP; #ifndef OPENSSL_NO_UNIX_SOCK if (family == AF_UNIX) (*bai)->bai_protocol = 0; #endif { BIO_ADDR *addr = BIO_ADDR_new(); if (addr != NULL) { BIO_ADDR_rawmake(addr, family, where, wherelen, port); (*bai)->bai_addr = BIO_ADDR_sockaddr_noconst(addr); } } (*bai)->bai_next = NULL; if ((*bai)->bai_addr == NULL) { BIO_ADDRINFO_free(*bai); *bai = NULL; return 0; } return 1; } DEFINE_RUN_ONCE_STATIC(do_bio_lookup_init) { bio_lookup_lock = CRYPTO_THREAD_lock_new(); return bio_lookup_lock != NULL; } int BIO_lookup(const char *host, const char *service, enum BIO_lookup_type lookup_type, int family, int socktype, BIO_ADDRINFO **res) { return BIO_lookup_ex(host, service, lookup_type, family, socktype, 0, res); } int BIO_lookup_ex(const char *host, const char *service, int lookup_type, int family, int socktype, int protocol, BIO_ADDRINFO **res) { int ret = 0; switch (family) { case AF_INET: #if OPENSSL_USE_IPV6 case AF_INET6: #endif #ifndef OPENSSL_NO_UNIX_SOCK case AF_UNIX: #endif #ifdef AF_UNSPEC case AF_UNSPEC: #endif break; default: ERR_raise(ERR_LIB_BIO, BIO_R_UNSUPPORTED_PROTOCOL_FAMILY); return 0; } #ifndef OPENSSL_NO_UNIX_SOCK if (family == AF_UNIX) { if (addrinfo_wrap(family, socktype, host, strlen(host), 0, res)) return 1; else ERR_raise(ERR_LIB_BIO, ERR_R_BIO_LIB); return 0; } #endif if (BIO_sock_init() != 1) return 0; if (1) { #ifdef AI_PASSIVE int gai_ret = 0, old_ret = 0; struct addrinfo hints; memset(&hints, 0, sizeof(hints)); hints.ai_family = family; hints.ai_socktype = socktype; hints.ai_protocol = protocol; # ifdef AI_ADDRCONFIG # ifdef AF_UNSPEC if (host != NULL && family == AF_UNSPEC) # endif hints.ai_flags |= AI_ADDRCONFIG; # endif if (lookup_type == BIO_LOOKUP_SERVER) hints.ai_flags |= AI_PASSIVE; # if defined(AI_ADDRCONFIG) && defined(AI_NUMERICHOST) retry: # endif switch ((gai_ret = getaddrinfo(host, service, &hints, res))) { # ifdef EAI_SYSTEM case EAI_SYSTEM: ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(), "calling getaddrinfo()"); ERR_raise(ERR_LIB_BIO, ERR_R_SYS_LIB); break; # endif # ifdef EAI_MEMORY case EAI_MEMORY: ERR_raise_data(ERR_LIB_BIO, ERR_R_SYS_LIB, gai_strerror(old_ret ? old_ret : gai_ret)); break; # endif case 0: ret = 1; break; default: # if defined(AI_ADDRCONFIG) && defined(AI_NUMERICHOST) if (hints.ai_flags & AI_ADDRCONFIG) { hints.ai_flags &= ~AI_ADDRCONFIG; hints.ai_flags |= AI_NUMERICHOST; old_ret = gai_ret; goto retry; } # endif ERR_raise_data(ERR_LIB_BIO, ERR_R_SYS_LIB, gai_strerror(old_ret ? old_ret : gai_ret)); break; } } else { #endif const struct hostent *he; #if defined(OPENSSL_SYS_VMS) && defined(__DECC) # pragma pointer_size save # pragma pointer_size 32 #endif #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS) static uint32_t he_fallback_address; static const char *he_fallback_addresses[] = { (char *)&he_fallback_address, NULL }; #else static in_addr_t he_fallback_address; static const char *he_fallback_addresses[] = { (char *)&he_fallback_address, NULL }; #endif static const struct hostent he_fallback = { NULL, NULL, AF_INET, sizeof(he_fallback_address), (char **)&he_fallback_addresses }; #if defined(OPENSSL_SYS_VMS) && defined(__DECC) # pragma pointer_size restore #endif struct servent *se; #ifdef _WIN64 struct servent se_fallback = { NULL, NULL, NULL, 0 }; #else struct servent se_fallback = { NULL, NULL, 0, NULL }; #endif if (!RUN_ONCE(&bio_lookup_init, do_bio_lookup_init)) { ERR_raise(ERR_LIB_BIO, ERR_R_CRYPTO_LIB); ret = 0; goto err; } if (!CRYPTO_THREAD_write_lock(bio_lookup_lock)) { ret = 0; goto err; } he_fallback_address = INADDR_ANY; if (host == NULL) { he = &he_fallback; switch (lookup_type) { case BIO_LOOKUP_CLIENT: he_fallback_address = INADDR_LOOPBACK; break; case BIO_LOOKUP_SERVER: he_fallback_address = INADDR_ANY; break; default: assert("We forgot to handle a lookup type!" == NULL); ERR_raise(ERR_LIB_BIO, ERR_R_INTERNAL_ERROR); ret = 0; goto err; } } else { he = gethostbyname(host); if (he == NULL) { #ifndef OPENSSL_SYS_WINDOWS # if defined(OPENSSL_SYS_VXWORKS) ERR_raise_data(ERR_LIB_SYS, 1000, "calling gethostbyname()"); # else ERR_raise_data(ERR_LIB_SYS, 1000 + h_errno, "calling gethostbyname()"); # endif #else ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(), "calling gethostbyname()"); #endif ret = 0; goto err; } } if (service == NULL) { se_fallback.s_port = 0; se_fallback.s_proto = NULL; se = &se_fallback; } else { char *endp = NULL; long portnum = strtol(service, &endp, 10); #if defined(OPENSSL_SYS_VMS) && defined(__DECC) # pragma pointer_size save # pragma pointer_size 32 #endif char *proto = NULL; #if defined(OPENSSL_SYS_VMS) && defined(__DECC) # pragma pointer_size restore #endif switch (socktype) { case SOCK_STREAM: proto = "tcp"; break; case SOCK_DGRAM: proto = "udp"; break; } if (endp != service && *endp == '\0' && portnum > 0 && portnum < 65536) { se_fallback.s_port = htons((unsigned short)portnum); se_fallback.s_proto = proto; se = &se_fallback; } else if (endp == service) { se = getservbyname(service, proto); if (se == NULL) { ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(), "calling getservbyname()"); goto err; } } else { ERR_raise(ERR_LIB_BIO, BIO_R_MALFORMED_HOST_OR_SERVICE); goto err; } } *res = NULL; { #if defined(OPENSSL_SYS_VMS) && defined(__DECC) # pragma pointer_size save # pragma pointer_size 32 #endif char **addrlistp; #if defined(OPENSSL_SYS_VMS) && defined(__DECC) # pragma pointer_size restore #endif size_t addresses; BIO_ADDRINFO *tmp_bai = NULL; for (addrlistp = he->h_addr_list; *addrlistp != NULL; addrlistp++) ; for (addresses = addrlistp - he->h_addr_list; addrlistp--, addresses-- > 0; ) { if (!addrinfo_wrap(he->h_addrtype, socktype, *addrlistp, he->h_length, se->s_port, &tmp_bai)) goto addrinfo_wrap_err; tmp_bai->bai_next = *res; *res = tmp_bai; continue; addrinfo_wrap_err: BIO_ADDRINFO_free(*res); *res = NULL; ERR_raise(ERR_LIB_BIO, ERR_R_BIO_LIB); ret = 0; goto err; } ret = 1; } err: CRYPTO_THREAD_unlock(bio_lookup_lock); } return ret; } #endif
bio
openssl/crypto/bio/bio_addr.c
openssl
#include <stdio.h> #include <errno.h> #include "bio_local.h" #include "internal/bio_tfo.h" #include "internal/ktls.h" #ifndef OPENSSL_NO_SOCK typedef struct bio_connect_st { int state; int connect_family; int connect_sock_type; char *param_hostname; char *param_service; int connect_mode; # ifndef OPENSSL_NO_KTLS unsigned char record_type; # endif int tfo_first; BIO_ADDRINFO *addr_first; const BIO_ADDRINFO *addr_iter; BIO_info_cb *info_callback; BIO *dgram_bio; } BIO_CONNECT; static int conn_write(BIO *h, const char *buf, int num); static int conn_read(BIO *h, char *buf, int size); static int conn_puts(BIO *h, const char *str); static int conn_gets(BIO *h, char *buf, int size); static long conn_ctrl(BIO *h, int cmd, long arg1, void *arg2); static int conn_new(BIO *h); static int conn_free(BIO *data); static long conn_callback_ctrl(BIO *h, int cmd, BIO_info_cb *); static int conn_sendmmsg(BIO *h, BIO_MSG *m, size_t s, size_t n, uint64_t f, size_t *mp); static int conn_recvmmsg(BIO *h, BIO_MSG *m, size_t s, size_t n, uint64_t f, size_t *mp); static int conn_state(BIO *b, BIO_CONNECT *c); static void conn_close_socket(BIO *data); static BIO_CONNECT *BIO_CONNECT_new(void); static void BIO_CONNECT_free(BIO_CONNECT *a); #define BIO_CONN_S_BEFORE 1 #define BIO_CONN_S_GET_ADDR 2 #define BIO_CONN_S_CREATE_SOCKET 3 #define BIO_CONN_S_CONNECT 4 #define BIO_CONN_S_OK 5 #define BIO_CONN_S_BLOCKED_CONNECT 6 #define BIO_CONN_S_CONNECT_ERROR 7 static const BIO_METHOD methods_connectp = { BIO_TYPE_CONNECT, "socket connect", bwrite_conv, conn_write, bread_conv, conn_read, conn_puts, conn_gets, conn_ctrl, conn_new, conn_free, conn_callback_ctrl, conn_sendmmsg, conn_recvmmsg, }; static int conn_create_dgram_bio(BIO *b, BIO_CONNECT *c) { if (c->connect_sock_type != SOCK_DGRAM) return 1; #ifndef OPENSSL_NO_DGRAM c->dgram_bio = BIO_new_dgram(b->num, 0); if (c->dgram_bio == NULL) goto err; return 1; err: #endif c->state = BIO_CONN_S_CONNECT_ERROR; return 0; } static int conn_state(BIO *b, BIO_CONNECT *c) { int ret = -1, i; BIO_info_cb *cb = NULL; if (c->info_callback != NULL) cb = c->info_callback; for (;;) { switch (c->state) { case BIO_CONN_S_BEFORE: if (c->param_hostname == NULL && c->param_service == NULL) { ERR_raise_data(ERR_LIB_BIO, BIO_R_NO_HOSTNAME_OR_SERVICE_SPECIFIED, "hostname=%s service=%s", c->param_hostname, c->param_service); goto exit_loop; } c->state = BIO_CONN_S_GET_ADDR; break; case BIO_CONN_S_GET_ADDR: { int family = AF_UNSPEC; switch (c->connect_family) { case BIO_FAMILY_IPV6: if (1) { #if OPENSSL_USE_IPV6 family = AF_INET6; } else { #endif ERR_raise(ERR_LIB_BIO, BIO_R_UNAVAILABLE_IP_FAMILY); goto exit_loop; } break; case BIO_FAMILY_IPV4: family = AF_INET; break; case BIO_FAMILY_IPANY: family = AF_UNSPEC; break; default: ERR_raise(ERR_LIB_BIO, BIO_R_UNSUPPORTED_IP_FAMILY); goto exit_loop; } if (BIO_lookup(c->param_hostname, c->param_service, BIO_LOOKUP_CLIENT, family, c->connect_sock_type, &c->addr_first) == 0) goto exit_loop; } if (c->addr_first == NULL) { ERR_raise(ERR_LIB_BIO, BIO_R_LOOKUP_RETURNED_NOTHING); goto exit_loop; } c->addr_iter = c->addr_first; c->state = BIO_CONN_S_CREATE_SOCKET; break; case BIO_CONN_S_CREATE_SOCKET: ret = BIO_socket(BIO_ADDRINFO_family(c->addr_iter), BIO_ADDRINFO_socktype(c->addr_iter), BIO_ADDRINFO_protocol(c->addr_iter), 0); if (ret == (int)INVALID_SOCKET) { ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(), "calling socket(%s, %s)", c->param_hostname, c->param_service); ERR_raise(ERR_LIB_BIO, BIO_R_UNABLE_TO_CREATE_SOCKET); goto exit_loop; } b->num = ret; c->state = BIO_CONN_S_CONNECT; break; case BIO_CONN_S_CONNECT: BIO_clear_retry_flags(b); ERR_set_mark(); ret = BIO_connect(b->num, BIO_ADDRINFO_address(c->addr_iter), BIO_SOCK_KEEPALIVE | c->connect_mode); b->retry_reason = 0; if (ret == 0) { if (BIO_sock_should_retry(ret)) { BIO_set_retry_special(b); c->state = BIO_CONN_S_BLOCKED_CONNECT; b->retry_reason = BIO_RR_CONNECT; ERR_pop_to_mark(); } else if ((c->addr_iter = BIO_ADDRINFO_next(c->addr_iter)) != NULL) { BIO_closesocket(b->num); c->state = BIO_CONN_S_CREATE_SOCKET; ERR_pop_to_mark(); break; } else { ERR_clear_last_mark(); ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(), "calling connect(%s, %s)", c->param_hostname, c->param_service); c->state = BIO_CONN_S_CONNECT_ERROR; break; } goto exit_loop; } else { ERR_clear_last_mark(); if (!conn_create_dgram_bio(b, c)) break; c->state = BIO_CONN_S_OK; } break; case BIO_CONN_S_BLOCKED_CONNECT: if (BIO_socket_wait(b->num, 0, time(NULL)) == 0) break; i = BIO_sock_error(b->num); if (i != 0) { BIO_clear_retry_flags(b); if ((c->addr_iter = BIO_ADDRINFO_next(c->addr_iter)) != NULL) { BIO_closesocket(b->num); c->state = BIO_CONN_S_CREATE_SOCKET; break; } ERR_raise_data(ERR_LIB_SYS, i, "calling connect(%s, %s)", c->param_hostname, c->param_service); ERR_raise(ERR_LIB_BIO, BIO_R_NBIO_CONNECT_ERROR); ret = 0; goto exit_loop; } else { if (!conn_create_dgram_bio(b, c)) break; c->state = BIO_CONN_S_OK; # ifndef OPENSSL_NO_KTLS ktls_enable(b->num); # endif } break; case BIO_CONN_S_CONNECT_ERROR: ERR_raise(ERR_LIB_BIO, BIO_R_CONNECT_ERROR); ret = 0; goto exit_loop; case BIO_CONN_S_OK: ret = 1; goto exit_loop; default: goto exit_loop; } if (cb != NULL) { if ((ret = cb((BIO *)b, c->state, ret)) == 0) goto end; } } exit_loop: if (cb != NULL) ret = cb((BIO *)b, c->state, ret); end: return ret; } static BIO_CONNECT *BIO_CONNECT_new(void) { BIO_CONNECT *ret; if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL) return NULL; ret->state = BIO_CONN_S_BEFORE; ret->connect_family = BIO_FAMILY_IPANY; ret->connect_sock_type = SOCK_STREAM; return ret; } static void BIO_CONNECT_free(BIO_CONNECT *a) { if (a == NULL) return; OPENSSL_free(a->param_hostname); OPENSSL_free(a->param_service); BIO_ADDRINFO_free(a->addr_first); OPENSSL_free(a); } const BIO_METHOD *BIO_s_connect(void) { return &methods_connectp; } static int conn_new(BIO *bi) { bi->init = 0; bi->num = (int)INVALID_SOCKET; bi->flags = 0; if ((bi->ptr = (char *)BIO_CONNECT_new()) == NULL) return 0; else return 1; } static void conn_close_socket(BIO *bio) { BIO_CONNECT *c; c = (BIO_CONNECT *)bio->ptr; if (bio->num != (int)INVALID_SOCKET) { if (c->state == BIO_CONN_S_OK) shutdown(bio->num, 2); BIO_closesocket(bio->num); bio->num = (int)INVALID_SOCKET; } } static int conn_free(BIO *a) { BIO_CONNECT *data; if (a == NULL) return 0; data = (BIO_CONNECT *)a->ptr; BIO_free(data->dgram_bio); if (a->shutdown) { conn_close_socket(a); BIO_CONNECT_free(data); a->ptr = NULL; a->flags = 0; a->init = 0; } return 1; } static int conn_read(BIO *b, char *out, int outl) { int ret = 0; BIO_CONNECT *data; data = (BIO_CONNECT *)b->ptr; if (data->state != BIO_CONN_S_OK) { ret = conn_state(b, data); if (ret <= 0) return ret; } if (data->dgram_bio != NULL) { BIO_clear_retry_flags(b); ret = BIO_read(data->dgram_bio, out, outl); BIO_set_flags(b, BIO_get_retry_flags(data->dgram_bio)); return ret; } if (out != NULL) { clear_socket_error(); # ifndef OPENSSL_NO_KTLS if (BIO_get_ktls_recv(b)) ret = ktls_read_record(b->num, out, outl); else # endif ret = readsocket(b->num, out, outl); BIO_clear_retry_flags(b); if (ret <= 0) { if (BIO_sock_should_retry(ret)) BIO_set_retry_read(b); else if (ret == 0) b->flags |= BIO_FLAGS_IN_EOF; } } return ret; } static int conn_write(BIO *b, const char *in, int inl) { int ret; BIO_CONNECT *data; data = (BIO_CONNECT *)b->ptr; if (data->state != BIO_CONN_S_OK) { ret = conn_state(b, data); if (ret <= 0) return ret; } if (data->dgram_bio != NULL) { BIO_clear_retry_flags(b); ret = BIO_write(data->dgram_bio, in, inl); BIO_set_flags(b, BIO_get_retry_flags(data->dgram_bio)); return ret; } clear_socket_error(); # ifndef OPENSSL_NO_KTLS if (BIO_should_ktls_ctrl_msg_flag(b)) { ret = ktls_send_ctrl_message(b->num, data->record_type, in, inl); if (ret >= 0) { ret = inl; BIO_clear_ktls_ctrl_msg_flag(b); } } else # endif # if defined(OSSL_TFO_SENDTO) if (data->tfo_first) { int peerlen = BIO_ADDRINFO_sockaddr_size(data->addr_iter); ret = sendto(b->num, in, inl, OSSL_TFO_SENDTO, BIO_ADDRINFO_sockaddr(data->addr_iter), peerlen); data->tfo_first = 0; } else # endif ret = writesocket(b->num, in, inl); BIO_clear_retry_flags(b); if (ret <= 0) { if (BIO_sock_should_retry(ret)) BIO_set_retry_write(b); } return ret; } static long conn_ctrl(BIO *b, int cmd, long num, void *ptr) { BIO *dbio; int *ip; const char **pptr = NULL; long ret = 1; BIO_CONNECT *data; const BIO_ADDR *dg_addr; # ifndef OPENSSL_NO_KTLS ktls_crypto_info_t *crypto_info; # endif data = (BIO_CONNECT *)b->ptr; switch (cmd) { case BIO_CTRL_RESET: ret = 0; data->state = BIO_CONN_S_BEFORE; conn_close_socket(b); BIO_ADDRINFO_free(data->addr_first); data->addr_first = NULL; b->flags = 0; break; case BIO_C_DO_STATE_MACHINE: if (data->state != BIO_CONN_S_OK) ret = (long)conn_state(b, data); else ret = 1; break; case BIO_C_GET_CONNECT: if (ptr != NULL) { pptr = (const char **)ptr; if (num == 0) { *pptr = data->param_hostname; } else if (num == 1) { *pptr = data->param_service; } else if (num == 2) { *pptr = (const char *)BIO_ADDRINFO_address(data->addr_iter); } else if (num == 3) { switch (BIO_ADDRINFO_family(data->addr_iter)) { # if OPENSSL_USE_IPV6 case AF_INET6: ret = BIO_FAMILY_IPV6; break; # endif case AF_INET: ret = BIO_FAMILY_IPV4; break; case 0: ret = data->connect_family; break; default: ret = -1; break; } } else if (num == 4) { ret = data->connect_mode; } else { ret = 0; } } else { ret = 0; } break; case BIO_C_SET_CONNECT: if (ptr != NULL) { b->init = 1; if (num == 0) { char *hold_service = data->param_service; OPENSSL_free(data->param_hostname); data->param_hostname = NULL; ret = BIO_parse_hostserv(ptr, &data->param_hostname, &data->param_service, BIO_PARSE_PRIO_HOST); if (hold_service != data->param_service) OPENSSL_free(hold_service); } else if (num == 1) { OPENSSL_free(data->param_service); if ((data->param_service = OPENSSL_strdup(ptr)) == NULL) ret = 0; } else if (num == 2) { const BIO_ADDR *addr = (const BIO_ADDR *)ptr; char *host = BIO_ADDR_hostname_string(addr, 1); char *service = BIO_ADDR_service_string(addr, 1); ret = host != NULL && service != NULL; if (ret) { OPENSSL_free(data->param_hostname); data->param_hostname = host; OPENSSL_free(data->param_service); data->param_service = service; BIO_ADDRINFO_free(data->addr_first); data->addr_first = NULL; data->addr_iter = NULL; } else { OPENSSL_free(host); OPENSSL_free(service); } } else if (num == 3) { data->connect_family = *(int *)ptr; } else { ret = 0; } } break; case BIO_C_SET_SOCK_TYPE: if ((num != SOCK_STREAM && num != SOCK_DGRAM) || data->state >= BIO_CONN_S_GET_ADDR) { ret = 0; break; } data->connect_sock_type = (int)num; ret = 1; break; case BIO_C_GET_SOCK_TYPE: ret = data->connect_sock_type; break; case BIO_C_GET_DGRAM_BIO: if (data->dgram_bio != NULL) { *(BIO **)ptr = data->dgram_bio; ret = 1; } else { ret = 0; } break; case BIO_CTRL_DGRAM_GET_PEER: case BIO_CTRL_DGRAM_DETECT_PEER_ADDR: if (data->state != BIO_CONN_S_OK) conn_state(b, data); if (data->state >= BIO_CONN_S_CREATE_SOCKET && data->addr_iter != NULL && (dg_addr = BIO_ADDRINFO_address(data->addr_iter)) != NULL) { ret = BIO_ADDR_sockaddr_size(dg_addr); if (num == 0 || num > ret) num = ret; memcpy(ptr, dg_addr, num); ret = num; } else { ret = 0; } break; case BIO_CTRL_GET_RPOLL_DESCRIPTOR: case BIO_CTRL_GET_WPOLL_DESCRIPTOR: { BIO_POLL_DESCRIPTOR *pd = ptr; if (data->state != BIO_CONN_S_OK) conn_state(b, data); if (data->state >= BIO_CONN_S_CREATE_SOCKET) { pd->type = BIO_POLL_DESCRIPTOR_TYPE_SOCK_FD; pd->value.fd = b->num; } else { ret = 0; } } break; case BIO_C_SET_NBIO: if (num != 0) data->connect_mode |= BIO_SOCK_NONBLOCK; else data->connect_mode &= ~BIO_SOCK_NONBLOCK; if (data->dgram_bio != NULL) ret = BIO_set_nbio(data->dgram_bio, num); break; #if defined(TCP_FASTOPEN) && !defined(OPENSSL_NO_TFO) case BIO_C_SET_TFO: if (num != 0) { data->connect_mode |= BIO_SOCK_TFO; data->tfo_first = 1; } else { data->connect_mode &= ~BIO_SOCK_TFO; data->tfo_first = 0; } break; #endif case BIO_C_SET_CONNECT_MODE: data->connect_mode = (int)num; if (num & BIO_SOCK_TFO) data->tfo_first = 1; else data->tfo_first = 0; break; case BIO_C_GET_FD: if (b->init) { ip = (int *)ptr; if (ip != NULL) *ip = b->num; ret = b->num; } else ret = -1; break; case BIO_CTRL_GET_CLOSE: ret = b->shutdown; break; case BIO_CTRL_SET_CLOSE: b->shutdown = (int)num; break; case BIO_CTRL_PENDING: case BIO_CTRL_WPENDING: ret = 0; break; case BIO_CTRL_FLUSH: break; case BIO_CTRL_DUP: { dbio = (BIO *)ptr; if (data->param_hostname) BIO_set_conn_hostname(dbio, data->param_hostname); if (data->param_service) BIO_set_conn_port(dbio, data->param_service); BIO_set_conn_ip_family(dbio, data->connect_family); BIO_set_conn_mode(dbio, data->connect_mode); (void)BIO_set_info_callback(dbio, data->info_callback); } break; case BIO_CTRL_SET_CALLBACK: ret = 0; break; case BIO_CTRL_GET_CALLBACK: { BIO_info_cb **fptr; fptr = (BIO_info_cb **)ptr; *fptr = data->info_callback; } break; case BIO_CTRL_EOF: ret = (b->flags & BIO_FLAGS_IN_EOF) != 0; break; # ifndef OPENSSL_NO_KTLS case BIO_CTRL_SET_KTLS: crypto_info = (ktls_crypto_info_t *)ptr; ret = ktls_start(b->num, crypto_info, num); if (ret) BIO_set_ktls_flag(b, num); break; case BIO_CTRL_GET_KTLS_SEND: return BIO_should_ktls_flag(b, 1) != 0; case BIO_CTRL_GET_KTLS_RECV: return BIO_should_ktls_flag(b, 0) != 0; case BIO_CTRL_SET_KTLS_TX_SEND_CTRL_MSG: BIO_set_ktls_ctrl_msg_flag(b); data->record_type = num; ret = 0; break; case BIO_CTRL_CLEAR_KTLS_TX_CTRL_MSG: BIO_clear_ktls_ctrl_msg_flag(b); ret = 0; break; case BIO_CTRL_SET_KTLS_TX_ZEROCOPY_SENDFILE: ret = ktls_enable_tx_zerocopy_sendfile(b->num); if (ret) BIO_set_ktls_zerocopy_sendfile_flag(b); break; # endif default: ret = 0; break; } return ret; } static long conn_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp) { long ret = 1; BIO_CONNECT *data; data = (BIO_CONNECT *)b->ptr; switch (cmd) { case BIO_CTRL_SET_CALLBACK: { data->info_callback = fp; } break; default: ret = 0; break; } return ret; } static int conn_puts(BIO *bp, const char *str) { int n, ret; n = strlen(str); ret = conn_write(bp, str, n); return ret; } int conn_gets(BIO *bio, char *buf, int size) { BIO_CONNECT *data; char *ptr = buf; int ret = 0; if (buf == NULL) { ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER); return -1; } if (size <= 0) { ERR_raise(ERR_LIB_BIO, BIO_R_INVALID_ARGUMENT); return -1; } *buf = '\0'; if (bio == NULL || bio->ptr == NULL) { ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER); return -1; } data = (BIO_CONNECT *)bio->ptr; if (data->state != BIO_CONN_S_OK) { ret = conn_state(bio, data); if (ret <= 0) return ret; } if (data->dgram_bio != NULL) { ERR_raise(ERR_LIB_BIO, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); return -1; } clear_socket_error(); while (size-- > 1) { # ifndef OPENSSL_NO_KTLS if (BIO_get_ktls_recv(bio)) ret = ktls_read_record(bio->num, ptr, 1); else # endif ret = readsocket(bio->num, ptr, 1); BIO_clear_retry_flags(bio); if (ret <= 0) { if (BIO_sock_should_retry(ret)) BIO_set_retry_read(bio); else if (ret == 0) bio->flags |= BIO_FLAGS_IN_EOF; break; } if (*ptr++ == '\n') break; } *ptr = '\0'; return ret > 0 || (bio->flags & BIO_FLAGS_IN_EOF) != 0 ? ptr - buf : ret; } static int conn_sendmmsg(BIO *bio, BIO_MSG *msg, size_t stride, size_t num_msgs, uint64_t flags, size_t *msgs_processed) { int ret; BIO_CONNECT *data; if (bio == NULL) { *msgs_processed = 0; ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER); return 0; } data = (BIO_CONNECT *)bio->ptr; if (data->state != BIO_CONN_S_OK) { ret = conn_state(bio, data); if (ret <= 0) { *msgs_processed = 0; return 0; } } if (data->dgram_bio == NULL) { *msgs_processed = 0; ERR_raise(ERR_LIB_BIO, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); return 0; } return BIO_sendmmsg(data->dgram_bio, msg, stride, num_msgs, flags, msgs_processed); } static int conn_recvmmsg(BIO *bio, BIO_MSG *msg, size_t stride, size_t num_msgs, uint64_t flags, size_t *msgs_processed) { int ret; BIO_CONNECT *data; if (bio == NULL) { *msgs_processed = 0; ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER); return 0; } data = (BIO_CONNECT *)bio->ptr; if (data->state != BIO_CONN_S_OK) { ret = conn_state(bio, data); if (ret <= 0) { *msgs_processed = 0; return 0; } } if (data->dgram_bio == NULL) { *msgs_processed = 0; ERR_raise(ERR_LIB_BIO, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); return 0; } return BIO_recvmmsg(data->dgram_bio, msg, stride, num_msgs, flags, msgs_processed); } BIO *BIO_new_connect(const char *str) { BIO *ret; ret = BIO_new(BIO_s_connect()); if (ret == NULL) return NULL; if (BIO_set_conn_hostname(ret, str)) return ret; BIO_free(ret); return NULL; } #endif
bio
openssl/crypto/bio/bss_conn.c
openssl
#define OPENSSL_SUPPRESS_DEPRECATED #include <stdio.h> #include <errno.h> #include "bio_local.h" #ifndef OPENSSL_NO_SOCK typedef struct bio_accept_st { int state; int accept_family; int bind_mode; int accepted_mode; char *param_addr; char *param_serv; int accept_sock; BIO_ADDRINFO *addr_first; const BIO_ADDRINFO *addr_iter; BIO_ADDR cache_accepting_addr; char *cache_accepting_name, *cache_accepting_serv; BIO_ADDR cache_peer_addr; char *cache_peer_name, *cache_peer_serv; BIO *bio_chain; } BIO_ACCEPT; static int acpt_write(BIO *h, const char *buf, int num); static int acpt_read(BIO *h, char *buf, int size); static int acpt_puts(BIO *h, const char *str); static long acpt_ctrl(BIO *h, int cmd, long arg1, void *arg2); static int acpt_new(BIO *h); static int acpt_free(BIO *data); static int acpt_state(BIO *b, BIO_ACCEPT *c); static void acpt_close_socket(BIO *data); static BIO_ACCEPT *BIO_ACCEPT_new(void); static void BIO_ACCEPT_free(BIO_ACCEPT *a); # define ACPT_S_BEFORE 1 # define ACPT_S_GET_ADDR 2 # define ACPT_S_CREATE_SOCKET 3 # define ACPT_S_LISTEN 4 # define ACPT_S_ACCEPT 5 # define ACPT_S_OK 6 static const BIO_METHOD methods_acceptp = { BIO_TYPE_ACCEPT, "socket accept", bwrite_conv, acpt_write, bread_conv, acpt_read, acpt_puts, NULL, acpt_ctrl, acpt_new, acpt_free, NULL, }; const BIO_METHOD *BIO_s_accept(void) { return &methods_acceptp; } static int acpt_new(BIO *bi) { BIO_ACCEPT *ba; bi->init = 0; bi->num = (int)INVALID_SOCKET; bi->flags = 0; if ((ba = BIO_ACCEPT_new()) == NULL) return 0; bi->ptr = (char *)ba; ba->state = ACPT_S_BEFORE; bi->shutdown = 1; return 1; } static BIO_ACCEPT *BIO_ACCEPT_new(void) { BIO_ACCEPT *ret; if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL) return NULL; ret->accept_family = BIO_FAMILY_IPANY; ret->accept_sock = (int)INVALID_SOCKET; return ret; } static void BIO_ACCEPT_free(BIO_ACCEPT *a) { if (a == NULL) return; OPENSSL_free(a->param_addr); OPENSSL_free(a->param_serv); BIO_ADDRINFO_free(a->addr_first); OPENSSL_free(a->cache_accepting_name); OPENSSL_free(a->cache_accepting_serv); OPENSSL_free(a->cache_peer_name); OPENSSL_free(a->cache_peer_serv); BIO_free(a->bio_chain); OPENSSL_free(a); } static void acpt_close_socket(BIO *bio) { BIO_ACCEPT *c; c = (BIO_ACCEPT *)bio->ptr; if (c->accept_sock != (int)INVALID_SOCKET) { shutdown(c->accept_sock, 2); closesocket(c->accept_sock); c->accept_sock = (int)INVALID_SOCKET; bio->num = (int)INVALID_SOCKET; } } static int acpt_free(BIO *a) { BIO_ACCEPT *data; if (a == NULL) return 0; data = (BIO_ACCEPT *)a->ptr; if (a->shutdown) { acpt_close_socket(a); BIO_ACCEPT_free(data); a->ptr = NULL; a->flags = 0; a->init = 0; } return 1; } static int acpt_state(BIO *b, BIO_ACCEPT *c) { BIO *bio = NULL, *dbio; int s = -1, ret = -1; for (;;) { switch (c->state) { case ACPT_S_BEFORE: if (c->param_addr == NULL && c->param_serv == NULL) { ERR_raise_data(ERR_LIB_BIO, BIO_R_NO_ACCEPT_ADDR_OR_SERVICE_SPECIFIED, "hostname=%s, service=%s", c->param_addr, c->param_serv); goto exit_loop; } OPENSSL_free(c->cache_accepting_name); c->cache_accepting_name = NULL; OPENSSL_free(c->cache_accepting_serv); c->cache_accepting_serv = NULL; OPENSSL_free(c->cache_peer_name); c->cache_peer_name = NULL; OPENSSL_free(c->cache_peer_serv); c->cache_peer_serv = NULL; c->state = ACPT_S_GET_ADDR; break; case ACPT_S_GET_ADDR: { int family = AF_UNSPEC; switch (c->accept_family) { case BIO_FAMILY_IPV6: if (1) { #if OPENSSL_USE_IPV6 family = AF_INET6; } else { #endif ERR_raise(ERR_LIB_BIO, BIO_R_UNAVAILABLE_IP_FAMILY); goto exit_loop; } break; case BIO_FAMILY_IPV4: family = AF_INET; break; case BIO_FAMILY_IPANY: family = AF_UNSPEC; break; default: ERR_raise(ERR_LIB_BIO, BIO_R_UNSUPPORTED_IP_FAMILY); goto exit_loop; } if (BIO_lookup(c->param_addr, c->param_serv, BIO_LOOKUP_SERVER, family, SOCK_STREAM, &c->addr_first) == 0) goto exit_loop; } if (c->addr_first == NULL) { ERR_raise(ERR_LIB_BIO, BIO_R_LOOKUP_RETURNED_NOTHING); goto exit_loop; } c->addr_iter = c->addr_first; c->state = ACPT_S_CREATE_SOCKET; break; case ACPT_S_CREATE_SOCKET: ERR_set_mark(); s = BIO_socket(BIO_ADDRINFO_family(c->addr_iter), BIO_ADDRINFO_socktype(c->addr_iter), BIO_ADDRINFO_protocol(c->addr_iter), 0); if (s == (int)INVALID_SOCKET) { if ((c->addr_iter = BIO_ADDRINFO_next(c->addr_iter)) != NULL) { ERR_pop_to_mark(); break; } ERR_clear_last_mark(); ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(), "calling socket(%s, %s)", c->param_addr, c->param_serv); ERR_raise(ERR_LIB_BIO, BIO_R_UNABLE_TO_CREATE_SOCKET); goto exit_loop; } c->accept_sock = s; b->num = s; c->state = ACPT_S_LISTEN; s = -1; break; case ACPT_S_LISTEN: { if (!BIO_listen(c->accept_sock, BIO_ADDRINFO_address(c->addr_iter), c->bind_mode)) { BIO_closesocket(c->accept_sock); goto exit_loop; } } { union BIO_sock_info_u info; info.addr = &c->cache_accepting_addr; if (!BIO_sock_info(c->accept_sock, BIO_SOCK_INFO_ADDRESS, &info)) { BIO_closesocket(c->accept_sock); goto exit_loop; } } c->cache_accepting_name = BIO_ADDR_hostname_string(&c->cache_accepting_addr, 1); c->cache_accepting_serv = BIO_ADDR_service_string(&c->cache_accepting_addr, 1); c->state = ACPT_S_ACCEPT; s = -1; ret = 1; goto end; case ACPT_S_ACCEPT: if (b->next_bio != NULL) { c->state = ACPT_S_OK; break; } BIO_clear_retry_flags(b); b->retry_reason = 0; OPENSSL_free(c->cache_peer_name); c->cache_peer_name = NULL; OPENSSL_free(c->cache_peer_serv); c->cache_peer_serv = NULL; s = BIO_accept_ex(c->accept_sock, &c->cache_peer_addr, c->accepted_mode); if (s < 0) { if (BIO_sock_should_retry(s)) { BIO_set_retry_special(b); b->retry_reason = BIO_RR_ACCEPT; goto end; } } if (s < 0) { ret = s; goto exit_loop; } bio = BIO_new_socket(s, BIO_CLOSE); if (bio == NULL) goto exit_loop; BIO_set_callback_ex(bio, BIO_get_callback_ex(b)); #ifndef OPENSSL_NO_DEPRECATED_3_0 BIO_set_callback(bio, BIO_get_callback(b)); #endif BIO_set_callback_arg(bio, BIO_get_callback_arg(b)); if (c->bio_chain != NULL) { if ((dbio = BIO_dup_chain(c->bio_chain)) == NULL) goto exit_loop; if (!BIO_push(dbio, bio)) goto exit_loop; bio = dbio; } if (BIO_push(b, bio) == NULL) goto exit_loop; c->cache_peer_name = BIO_ADDR_hostname_string(&c->cache_peer_addr, 1); c->cache_peer_serv = BIO_ADDR_service_string(&c->cache_peer_addr, 1); c->state = ACPT_S_OK; bio = NULL; ret = 1; goto end; case ACPT_S_OK: if (b->next_bio == NULL) { c->state = ACPT_S_ACCEPT; break; } ret = 1; goto end; default: ret = 0; goto end; } } exit_loop: if (bio != NULL) BIO_free(bio); else if (s >= 0) BIO_closesocket(s); end: return ret; } static int acpt_read(BIO *b, char *out, int outl) { int ret = 0; BIO_ACCEPT *data; BIO_clear_retry_flags(b); data = (BIO_ACCEPT *)b->ptr; while (b->next_bio == NULL) { ret = acpt_state(b, data); if (ret <= 0) return ret; } ret = BIO_read(b->next_bio, out, outl); BIO_copy_next_retry(b); return ret; } static int acpt_write(BIO *b, const char *in, int inl) { int ret; BIO_ACCEPT *data; BIO_clear_retry_flags(b); data = (BIO_ACCEPT *)b->ptr; while (b->next_bio == NULL) { ret = acpt_state(b, data); if (ret <= 0) return ret; } ret = BIO_write(b->next_bio, in, inl); BIO_copy_next_retry(b); return ret; } static long acpt_ctrl(BIO *b, int cmd, long num, void *ptr) { int *ip; long ret = 1; BIO_ACCEPT *data; char **pp; data = (BIO_ACCEPT *)b->ptr; switch (cmd) { case BIO_CTRL_RESET: ret = 0; data->state = ACPT_S_BEFORE; acpt_close_socket(b); BIO_ADDRINFO_free(data->addr_first); data->addr_first = NULL; b->flags = 0; break; case BIO_C_DO_STATE_MACHINE: ret = (long)acpt_state(b, data); break; case BIO_C_SET_ACCEPT: if (ptr != NULL) { if (num == 0) { char *hold_serv = data->param_serv; OPENSSL_free(data->param_addr); data->param_addr = NULL; ret = BIO_parse_hostserv(ptr, &data->param_addr, &data->param_serv, BIO_PARSE_PRIO_SERV); if (hold_serv != data->param_serv) OPENSSL_free(hold_serv); b->init = 1; } else if (num == 1) { OPENSSL_free(data->param_serv); if ((data->param_serv = OPENSSL_strdup(ptr)) == NULL) ret = 0; else b->init = 1; } else if (num == 2) { data->bind_mode |= BIO_SOCK_NONBLOCK; } else if (num == 3) { BIO_free(data->bio_chain); data->bio_chain = (BIO *)ptr; } else if (num == 4) { data->accept_family = *(int *)ptr; } else if (num == 5) { data->bind_mode |= BIO_SOCK_TFO; } } else { if (num == 2) { data->bind_mode &= ~BIO_SOCK_NONBLOCK; } else if (num == 5) { data->bind_mode &= ~BIO_SOCK_TFO; } } break; case BIO_C_SET_NBIO: if (num != 0) data->accepted_mode |= BIO_SOCK_NONBLOCK; else data->accepted_mode &= ~BIO_SOCK_NONBLOCK; break; case BIO_C_SET_FD: b->num = *((int *)ptr); data->accept_sock = b->num; data->state = ACPT_S_ACCEPT; b->shutdown = (int)num; b->init = 1; break; case BIO_C_GET_FD: if (b->init) { ip = (int *)ptr; if (ip != NULL) *ip = data->accept_sock; ret = data->accept_sock; } else ret = -1; break; case BIO_C_GET_ACCEPT: if (b->init) { if (num == 0 && ptr != NULL) { pp = (char **)ptr; *pp = data->cache_accepting_name; } else if (num == 1 && ptr != NULL) { pp = (char **)ptr; *pp = data->cache_accepting_serv; } else if (num == 2 && ptr != NULL) { pp = (char **)ptr; *pp = data->cache_peer_name; } else if (num == 3 && ptr != NULL) { pp = (char **)ptr; *pp = data->cache_peer_serv; } else if (num == 4) { switch (BIO_ADDRINFO_family(data->addr_iter)) { #if OPENSSL_USE_IPV6 case AF_INET6: ret = BIO_FAMILY_IPV6; break; #endif case AF_INET: ret = BIO_FAMILY_IPV4; break; case 0: ret = data->accept_family; break; default: ret = -1; break; } } else ret = -1; } else ret = -1; break; case BIO_CTRL_GET_CLOSE: ret = b->shutdown; break; case BIO_CTRL_SET_CLOSE: b->shutdown = (int)num; break; case BIO_CTRL_PENDING: case BIO_CTRL_WPENDING: ret = 0; break; case BIO_CTRL_FLUSH: break; case BIO_C_SET_BIND_MODE: data->bind_mode = (int)num; break; case BIO_C_GET_BIND_MODE: ret = (long)data->bind_mode; break; case BIO_CTRL_DUP: break; case BIO_CTRL_EOF: if (b->next_bio == NULL) ret = 0; else ret = BIO_ctrl(b->next_bio, cmd, num, ptr); break; default: ret = 0; break; } return ret; } static int acpt_puts(BIO *bp, const char *str) { int n, ret; n = strlen(str); ret = acpt_write(bp, str, n); return ret; } BIO *BIO_new_accept(const char *str) { BIO *ret; ret = BIO_new(BIO_s_accept()); if (ret == NULL) return NULL; if (BIO_set_accept_name(ret, str) > 0) return ret; BIO_free(ret); return NULL; } #endif
bio
openssl/crypto/bio/bss_acpt.c
openssl