ssl/tls_srp.c:306: error: BUFFER_OVERRUN_L3
  Offset added: [8, +oo] Size: [0, 67108856] by call to `SRP_Calc_client_key`.
Showing all 32 steps of the trace


ssl/tls_srp.c:279:1: Parameter `s->srp_ctx.N->top`
277.   
278.   /* client side */
279. > int srp_generate_client_master_secret(SSL *s)
280.   {
281.       BIGNUM *x = NULL, *u = NULL, *K = NULL;

ssl/tls_srp.c:289:9: Call
287.        * Checks if b % n == 0
288.        */
289.       if (SRP_Verify_B_mod_N(s->srp_ctx.B, s->srp_ctx.N) == 0
               ^
290.               || (u = SRP_Calc_u(s->srp_ctx.A, s->srp_ctx.B, s->srp_ctx.N))
291.                  == NULL

crypto/srp/srp_lib.c:216:1: Parameter `N->top`
  214.   }
  215.   
  216. > int SRP_Verify_B_mod_N(const BIGNUM *B, const BIGNUM *N)
  217.   {
  218.       BIGNUM *r;

ssl/tls_srp.c:290:21: Call
288.        */
289.       if (SRP_Verify_B_mod_N(s->srp_ctx.B, s->srp_ctx.N) == 0
290.               || (u = SRP_Calc_u(s->srp_ctx.A, s->srp_ctx.B, s->srp_ctx.N))
                           ^
291.                  == NULL
292.               || s->srp_ctx.SRP_give_srp_client_pwd_callback == NULL) {

crypto/srp/srp_lib.c:51:1: Parameter `N->top`
  49.   }
  50.   
  51. > BIGNUM *SRP_Calc_u(const BIGNUM *A, const BIGNUM *B, const BIGNUM *N)
  52.   {
  53.       /* k = SHA1(PAD(A) || PAD(B) ) -- tls-srp draft 8 */

crypto/srp/srp_lib.c:54:12: Call
  52.   {
  53.       /* k = SHA1(PAD(A) || PAD(B) ) -- tls-srp draft 8 */
  54.       return srp_Calc_xy(A, B, N);
                   ^
  55.   }
  56.   

crypto/srp/srp_lib.c:23:1: Parameter `N->top`
    21.   /* calculate = SHA1(PAD(x) || PAD(y)) */
    22.   
    23. > static BIGNUM *srp_Calc_xy(const BIGNUM *x, const BIGNUM *y, const BIGNUM *N)
    24.   {
    25.       unsigned char digest[SHA_DIGEST_LENGTH];

crypto/srp/srp_lib.c:27:16: Call
    25.       unsigned char digest[SHA_DIGEST_LENGTH];
    26.       unsigned char *tmp = NULL;
    27.       int numN = BN_num_bytes(N);
                         ^
    28.       BIGNUM *res = NULL;
    29.       if (x != N && BN_ucmp(x, N) >= 0)

crypto/bn/bn_lib.c:140:9: Call
      138.       bn_check_top(a);
      139.   
      140.       if (BN_is_zero(a))
                     ^
      141.           return 0;
      142.       return ((i * BN_BITS2) + BN_num_bits_word(a->d[i]));

crypto/bn/bn_lib.c:852:1: Parameter `a->top`
        850.   }
        851.   
        852. > int BN_is_zero(const BIGNUM *a)
        853.   {
        854.       return a->top == 0;

ssl/tls_srp.c:306:21: Call
304.       }
305.       if ((x = SRP_Calc_x(s->srp_ctx.s, s->srp_ctx.login, passwd)) == NULL
306.               || (K = SRP_Calc_client_key(s->srp_ctx.N, s->srp_ctx.B,
                           ^
307.                                           s->srp_ctx.g, x,
308.                                           s->srp_ctx.a, u)) == NULL) {

crypto/srp/srp_lib.c:174:1: Parameter `N->top`
  172.   }
  173.   
  174. > BIGNUM *SRP_Calc_client_key(const BIGNUM *N, const BIGNUM *B, const BIGNUM *g,
  175.                               const BIGNUM *x, const BIGNUM *a, const BIGNUM *u)
  176.   {

crypto/srp/srp_lib.c:189:10: Call
  187.           goto err;
  188.   
  189.       if (!BN_mod_exp(tmp, g, x, N, bn_ctx))
                  ^
  190.           goto err;
  191.       if ((k = srp_Calc_k(N, g)) == NULL)

crypto/bn/bn_exp.c:89:1: Parameter `m->top`
    87.   }
    88.   
    89. > int BN_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m,
    90.                  BN_CTX *ctx)
    91.   {

crypto/bn/bn_exp.c:134:9: Call
    132.   
    133.   #ifdef MONT_MUL_MOD
    134.       if (BN_is_odd(m)) {
                   ^
    135.   # ifdef MONT_EXP_WORD
    136.           if (a->top == 1 && !a->neg

crypto/bn/bn_lib.c:867:1: Parameter `a->top`
      865.   }
      866.   
      867. > int BN_is_odd(const BIGNUM *a)
      868.   {
      869.       return (a->top > 0) && (a->d[0] & 1);

crypto/bn/bn_exp.c:141:19: Call
    139.               && (BN_get_flags(m, BN_FLG_CONSTTIME) == 0)) {
    140.               BN_ULONG A = a->d[0];
    141.               ret = BN_mod_exp_mont_word(r, A, p, m, ctx, NULL);
                             ^
    142.           } else
    143.   # endif

crypto/bn/bn_exp.c:1127:1: Parameter `m->top`
      1125.   }
      1126.   
      1127. > int BN_mod_exp_mont_word(BIGNUM *rr, BN_ULONG a, const BIGNUM *p,
      1128.                            const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont)
      1129.   {

crypto/bn/bn_exp.c:1163:10: Call
      1161.       bn_check_top(m);
      1162.   
      1163.       if (!BN_is_odd(m)) {
                       ^
      1164.           BNerr(BN_F_BN_MOD_EXP_MONT_WORD, BN_R_CALLED_WITH_EVEN_MODULUS);
      1165.           return 0;

crypto/bn/bn_lib.c:867:1: Parameter `a->top`
        865.   }
        866.   
        867. > int BN_is_odd(const BIGNUM *a)
        868.   {
        869.       return (a->top > 0) && (a->d[0] & 1);

crypto/bn/bn_exp.c:1198:14: Call
      1196.           if ((mont = BN_MONT_CTX_new()) == NULL)
      1197.               goto err;
      1198.           if (!BN_MONT_CTX_set(mont, m, ctx))
                           ^
      1199.               goto err;
      1200.       }

crypto/bn/bn_mont.c:251:1: Parameter `mod->top`
        249.   }
        250.   
        251. > int BN_MONT_CTX_set(BN_MONT_CTX *mont, const BIGNUM *mod, BN_CTX *ctx)
        252.   {
        253.       int i, ret = 0;

crypto/bn/bn_mont.c:256:9: Call
        254.       BIGNUM *Ri, *R;
        255.   
        256.       if (BN_is_zero(mod))
                       ^
        257.           return 0;
        258.   

crypto/bn/bn_lib.c:852:1: Parameter `a->top`
          850.   }
          851.   
          852. > int BN_is_zero(const BIGNUM *a)
          853.   {
          854.       return a->top == 0;

crypto/bn/bn_mont.c:263:10: Call
        261.           goto err;
        262.       R = &(mont->RR);            /* grab RR as a temp */
        263.       if (!BN_copy(&(mont->N), mod))
                        ^
        264.           goto err;               /* Set N */
        265.       if (BN_get_flags(mod, BN_FLG_CONSTTIME) != 0)

crypto/bn/bn_lib.c:281:1: <Offset trace>
279.   }
280.   
281. > BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)
282.   {
283.       bn_check_top(b);

crypto/bn/bn_lib.c:281:1: Parameter `b->top`
          279.   }
          280.   
          281. > BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)
          282.   {
          283.       bn_check_top(b);

crypto/bn/bn_lib.c:281:1: <Length trace>
279.   }
280.   
281. > BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)
282.   {
283.       bn_check_top(b);

crypto/bn/bn_lib.c:281:1: Parameter `*a->d`
          279.   }
          280.   
          281. > BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)
          282.   {
          283.       bn_check_top(b);

crypto/bn/bn_lib.c:287:9: Call
          285.       if (a == b)
          286.           return a;
          287.       if (bn_wexpand(a, b->top) == NULL)
                         ^
          288.           return NULL;
          289.   

crypto/bn/bn_lib.c:948:1: Parameter `*a->d`
            946.   }
            947.   
            948. > BIGNUM *bn_wexpand(BIGNUM *a, int words)
            949.   {
            950.       return (words <= a->dmax) ? a : bn_expand2(a, words);

crypto/bn/bn_lib.c:291:9: Array access: Offset added: [8, +oo] Size: [0, 67108856] by call to `SRP_Calc_client_key` 
          289.   
          290.       if (b->top > 0)
          291.           memcpy(a->d, b->d, sizeof(b->d[0]) * b->top);
                         ^
          292.   
          293.       a->neg = b->neg;

BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)
{
    bn_check_top(b);
    if (a == b)
        return a;
    if (bn_wexpand(a, b->top) == NULL)
        return NULL;
    if (b->top > 0)
        memcpy(a->d, b->d, sizeof(b->d[0]) * b->top);
    a->neg = b->neg;
    a->top = b->top;
    a->flags |= b->flags & BN_FLG_FIXED_TOP;
    bn_check_top(a);
    return a;
}
['int srp_generate_client_master_secret(SSL *s)\n{\n    BIGNUM *x = NULL, *u = NULL, *K = NULL;\n    int ret = -1, tmp_len = 0;\n    char *passwd = NULL;\n    unsigned char *tmp = NULL;\n    if (SRP_Verify_B_mod_N(s->srp_ctx.B, s->srp_ctx.N) == 0\n            || (u = SRP_Calc_u(s->srp_ctx.A, s->srp_ctx.B, s->srp_ctx.N))\n               == NULL\n            || s->srp_ctx.SRP_give_srp_client_pwd_callback == NULL) {\n        SSLfatal(s, SSL_AD_INTERNAL_ERROR,\n                 SSL_F_SRP_GENERATE_CLIENT_MASTER_SECRET, ERR_R_INTERNAL_ERROR);\n        goto err;\n    }\n    if ((passwd = s->srp_ctx.SRP_give_srp_client_pwd_callback(s,\n                                                      s->srp_ctx.SRP_cb_arg))\n            == NULL) {\n        SSLfatal(s, SSL_AD_INTERNAL_ERROR,\n                 SSL_F_SRP_GENERATE_CLIENT_MASTER_SECRET,\n                 SSL_R_CALLBACK_FAILED);\n        goto err;\n    }\n    if ((x = SRP_Calc_x(s->srp_ctx.s, s->srp_ctx.login, passwd)) == NULL\n            || (K = SRP_Calc_client_key(s->srp_ctx.N, s->srp_ctx.B,\n                                        s->srp_ctx.g, x,\n                                        s->srp_ctx.a, u)) == NULL) {\n        SSLfatal(s, SSL_AD_INTERNAL_ERROR,\n                 SSL_F_SRP_GENERATE_CLIENT_MASTER_SECRET, ERR_R_INTERNAL_ERROR);\n        goto err;\n    }\n    tmp_len = BN_num_bytes(K);\n    if ((tmp = OPENSSL_malloc(tmp_len)) == NULL) {\n        SSLfatal(s, SSL_AD_INTERNAL_ERROR,\n                 SSL_F_SRP_GENERATE_CLIENT_MASTER_SECRET, ERR_R_MALLOC_FAILURE);\n        goto err;\n    }\n    BN_bn2bin(K, tmp);\n    ret = ssl_generate_master_secret(s, tmp, tmp_len, 1);\n err:\n    BN_clear_free(K);\n    BN_clear_free(x);\n    if (passwd != NULL)\n        OPENSSL_clear_free(passwd, strlen(passwd));\n    BN_clear_free(u);\n    return ret;\n}', 'int SRP_Verify_B_mod_N(const BIGNUM *B, const BIGNUM *N)\n{\n    BIGNUM *r;\n    BN_CTX *bn_ctx;\n    int ret = 0;\n    if (B == NULL || N == NULL || (bn_ctx = BN_CTX_new()) == NULL)\n        return 0;\n    if ((r = BN_new()) == NULL)\n        goto err;\n    if (!BN_nnmod(r, B, N, bn_ctx))\n        goto err;\n    ret = !BN_is_zero(r);\n err:\n    BN_CTX_free(bn_ctx);\n    BN_free(r);\n    return ret;\n}', 'BIGNUM *SRP_Calc_u(const BIGNUM *A, const BIGNUM *B, const BIGNUM *N)\n{\n    return srp_Calc_xy(A, B, N);\n}', 'static BIGNUM *srp_Calc_xy(const BIGNUM *x, const BIGNUM *y, const BIGNUM *N)\n{\n    unsigned char digest[SHA_DIGEST_LENGTH];\n    unsigned char *tmp = NULL;\n    int numN = BN_num_bytes(N);\n    BIGNUM *res = NULL;\n    if (x != N && BN_ucmp(x, N) >= 0)\n        return NULL;\n    if (y != N && BN_ucmp(y, N) >= 0)\n        return NULL;\n    if ((tmp = OPENSSL_malloc(numN * 2)) == NULL)\n        goto err;\n    if (BN_bn2binpad(x, tmp, numN) < 0\n        || BN_bn2binpad(y, tmp + numN, numN) < 0\n        || !EVP_Digest(tmp, numN * 2, digest, NULL, EVP_sha1(), NULL))\n        goto err;\n    res = BN_bin2bn(digest, sizeof(digest), NULL);\n err:\n    OPENSSL_free(tmp);\n    return res;\n}', 'int BN_num_bits(const BIGNUM *a)\n{\n    int i = a->top - 1;\n    bn_check_top(a);\n    if (BN_is_zero(a))\n        return 0;\n    return ((i * BN_BITS2) + BN_num_bits_word(a->d[i]));\n}', 'int BN_is_zero(const BIGNUM *a)\n{\n    return a->top == 0;\n}', 'BIGNUM *SRP_Calc_client_key(const BIGNUM *N, const BIGNUM *B, const BIGNUM *g,\n                            const BIGNUM *x, const BIGNUM *a, const BIGNUM *u)\n{\n    BIGNUM *tmp = NULL, *tmp2 = NULL, *tmp3 = NULL, *k = NULL, *K = NULL;\n    BN_CTX *bn_ctx;\n    if (u == NULL || B == NULL || N == NULL || g == NULL || x == NULL\n        || a == NULL || (bn_ctx = BN_CTX_new()) == NULL)\n        return NULL;\n    if ((tmp = BN_new()) == NULL ||\n        (tmp2 = BN_new()) == NULL ||\n        (tmp3 = BN_new()) == NULL)\n        goto err;\n    if (!BN_mod_exp(tmp, g, x, N, bn_ctx))\n        goto err;\n    if ((k = srp_Calc_k(N, g)) == NULL)\n        goto err;\n    if (!BN_mod_mul(tmp2, tmp, k, N, bn_ctx))\n        goto err;\n    if (!BN_mod_sub(tmp, B, tmp2, N, bn_ctx))\n        goto err;\n    if (!BN_mul(tmp3, u, x, bn_ctx))\n        goto err;\n    if (!BN_add(tmp2, a, tmp3))\n        goto err;\n    K = BN_new();\n    if (K != NULL && !BN_mod_exp(K, tmp, tmp2, N, bn_ctx)) {\n        BN_free(K);\n        K = NULL;\n    }\n err:\n    BN_CTX_free(bn_ctx);\n    BN_clear_free(tmp);\n    BN_clear_free(tmp2);\n    BN_clear_free(tmp3);\n    BN_free(k);\n    return K;\n}', 'int BN_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m,\n               BN_CTX *ctx)\n{\n    int ret;\n    bn_check_top(a);\n    bn_check_top(p);\n    bn_check_top(m);\n#define MONT_MUL_MOD\n#define MONT_EXP_WORD\n#define RECP_MUL_MOD\n#ifdef MONT_MUL_MOD\n    if (BN_is_odd(m)) {\n# ifdef MONT_EXP_WORD\n        if (a->top == 1 && !a->neg\n            && (BN_get_flags(p, BN_FLG_CONSTTIME) == 0)\n            && (BN_get_flags(a, BN_FLG_CONSTTIME) == 0)\n            && (BN_get_flags(m, BN_FLG_CONSTTIME) == 0)) {\n            BN_ULONG A = a->d[0];\n            ret = BN_mod_exp_mont_word(r, A, p, m, ctx, NULL);\n        } else\n# endif\n            ret = BN_mod_exp_mont(r, a, p, m, ctx, NULL);\n    } else\n#endif\n#ifdef RECP_MUL_MOD\n    {\n        ret = BN_mod_exp_recp(r, a, p, m, ctx);\n    }\n#else\n    {\n        ret = BN_mod_exp_simple(r, a, p, m, ctx);\n    }\n#endif\n    bn_check_top(r);\n    return ret;\n}', 'int BN_is_odd(const BIGNUM *a)\n{\n    return (a->top > 0) && (a->d[0] & 1);\n}', 'int BN_mod_exp_mont_word(BIGNUM *rr, BN_ULONG a, const BIGNUM *p,\n                         const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont)\n{\n    BN_MONT_CTX *mont = NULL;\n    int b, bits, ret = 0;\n    int r_is_one;\n    BN_ULONG w, next_w;\n    BIGNUM *r, *t;\n    BIGNUM *swap_tmp;\n#define BN_MOD_MUL_WORD(r, w, m) \\\n                (BN_mul_word(r, (w)) && \\\n                (   \\\n                        (BN_mod(t, r, m, ctx) && (swap_tmp = r, r = t, t = swap_tmp, 1))))\n#define BN_TO_MONTGOMERY_WORD(r, w, mont) \\\n                (BN_set_word(r, (w)) && BN_to_montgomery(r, r, (mont), ctx))\n    if (BN_get_flags(p, BN_FLG_CONSTTIME) != 0\n            || BN_get_flags(m, BN_FLG_CONSTTIME) != 0) {\n        BNerr(BN_F_BN_MOD_EXP_MONT_WORD, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);\n        return 0;\n    }\n    bn_check_top(p);\n    bn_check_top(m);\n    if (!BN_is_odd(m)) {\n        BNerr(BN_F_BN_MOD_EXP_MONT_WORD, BN_R_CALLED_WITH_EVEN_MODULUS);\n        return 0;\n    }\n    if (m->top == 1)\n        a %= m->d[0];\n    bits = BN_num_bits(p);\n    if (bits == 0) {\n        if (BN_abs_is_word(m, 1)) {\n            ret = 1;\n            BN_zero(rr);\n        } else {\n            ret = BN_one(rr);\n        }\n        return ret;\n    }\n    if (a == 0) {\n        BN_zero(rr);\n        ret = 1;\n        return ret;\n    }\n    BN_CTX_start(ctx);\n    r = BN_CTX_get(ctx);\n    t = BN_CTX_get(ctx);\n    if (t == NULL)\n        goto err;\n    if (in_mont != NULL)\n        mont = in_mont;\n    else {\n        if ((mont = BN_MONT_CTX_new()) == NULL)\n            goto err;\n        if (!BN_MONT_CTX_set(mont, m, ctx))\n            goto err;\n    }\n    r_is_one = 1;\n    w = a;\n    for (b = bits - 2; b >= 0; b--) {\n        next_w = w * w;\n        if ((next_w / w) != w) {\n            if (r_is_one) {\n                if (!BN_TO_MONTGOMERY_WORD(r, w, mont))\n                    goto err;\n                r_is_one = 0;\n            } else {\n                if (!BN_MOD_MUL_WORD(r, w, m))\n                    goto err;\n            }\n            next_w = 1;\n        }\n        w = next_w;\n        if (!r_is_one) {\n            if (!BN_mod_mul_montgomery(r, r, r, mont, ctx))\n                goto err;\n        }\n        if (BN_is_bit_set(p, b)) {\n            next_w = w * a;\n            if ((next_w / a) != w) {\n                if (r_is_one) {\n                    if (!BN_TO_MONTGOMERY_WORD(r, w, mont))\n                        goto err;\n                    r_is_one = 0;\n                } else {\n                    if (!BN_MOD_MUL_WORD(r, w, m))\n                        goto err;\n                }\n                next_w = a;\n            }\n            w = next_w;\n        }\n    }\n    if (w != 1) {\n        if (r_is_one) {\n            if (!BN_TO_MONTGOMERY_WORD(r, w, mont))\n                goto err;\n            r_is_one = 0;\n        } else {\n            if (!BN_MOD_MUL_WORD(r, w, m))\n                goto err;\n        }\n    }\n    if (r_is_one) {\n        if (!BN_one(rr))\n            goto err;\n    } else {\n        if (!BN_from_montgomery(rr, r, mont, ctx))\n            goto err;\n    }\n    ret = 1;\n err:\n    if (in_mont == NULL)\n        BN_MONT_CTX_free(mont);\n    BN_CTX_end(ctx);\n    bn_check_top(rr);\n    return ret;\n}', 'int BN_MONT_CTX_set(BN_MONT_CTX *mont, const BIGNUM *mod, BN_CTX *ctx)\n{\n    int i, ret = 0;\n    BIGNUM *Ri, *R;\n    if (BN_is_zero(mod))\n        return 0;\n    BN_CTX_start(ctx);\n    if ((Ri = BN_CTX_get(ctx)) == NULL)\n        goto err;\n    R = &(mont->RR);\n    if (!BN_copy(&(mont->N), mod))\n        goto err;\n    if (BN_get_flags(mod, BN_FLG_CONSTTIME) != 0)\n        BN_set_flags(&(mont->N), BN_FLG_CONSTTIME);\n    mont->N.neg = 0;\n#ifdef MONT_WORD\n    {\n        BIGNUM tmod;\n        BN_ULONG buf[2];\n        bn_init(&tmod);\n        tmod.d = buf;\n        tmod.dmax = 2;\n        tmod.neg = 0;\n        if (BN_get_flags(mod, BN_FLG_CONSTTIME) != 0)\n            BN_set_flags(&tmod, BN_FLG_CONSTTIME);\n        mont->ri = (BN_num_bits(mod) + (BN_BITS2 - 1)) / BN_BITS2 * BN_BITS2;\n# if defined(OPENSSL_BN_ASM_MONT) && (BN_BITS2<=32)\n        BN_zero(R);\n        if (!(BN_set_bit(R, 2 * BN_BITS2)))\n            goto err;\n        tmod.top = 0;\n        if ((buf[0] = mod->d[0]))\n            tmod.top = 1;\n        if ((buf[1] = mod->top > 1 ? mod->d[1] : 0))\n            tmod.top = 2;\n        if (BN_is_one(&tmod))\n            BN_zero(Ri);\n        else if ((BN_mod_inverse(Ri, R, &tmod, ctx)) == NULL)\n            goto err;\n        if (!BN_lshift(Ri, Ri, 2 * BN_BITS2))\n            goto err;\n        if (!BN_is_zero(Ri)) {\n            if (!BN_sub_word(Ri, 1))\n                goto err;\n        } else {\n            if (bn_expand(Ri, (int)sizeof(BN_ULONG) * 2) == NULL)\n                goto err;\n            Ri->neg = 0;\n            Ri->d[0] = BN_MASK2;\n            Ri->d[1] = BN_MASK2;\n            Ri->top = 2;\n        }\n        if (!BN_div(Ri, NULL, Ri, &tmod, ctx))\n            goto err;\n        mont->n0[0] = (Ri->top > 0) ? Ri->d[0] : 0;\n        mont->n0[1] = (Ri->top > 1) ? Ri->d[1] : 0;\n# else\n        BN_zero(R);\n        if (!(BN_set_bit(R, BN_BITS2)))\n            goto err;\n        buf[0] = mod->d[0];\n        buf[1] = 0;\n        tmod.top = buf[0] != 0 ? 1 : 0;\n        if (BN_is_one(&tmod))\n            BN_zero(Ri);\n        else if ((BN_mod_inverse(Ri, R, &tmod, ctx)) == NULL)\n            goto err;\n        if (!BN_lshift(Ri, Ri, BN_BITS2))\n            goto err;\n        if (!BN_is_zero(Ri)) {\n            if (!BN_sub_word(Ri, 1))\n                goto err;\n        } else {\n            if (!BN_set_word(Ri, BN_MASK2))\n                goto err;\n        }\n        if (!BN_div(Ri, NULL, Ri, &tmod, ctx))\n            goto err;\n        mont->n0[0] = (Ri->top > 0) ? Ri->d[0] : 0;\n        mont->n0[1] = 0;\n# endif\n    }\n#else\n    {\n        mont->ri = BN_num_bits(&mont->N);\n        BN_zero(R);\n        if (!BN_set_bit(R, mont->ri))\n            goto err;\n        if ((BN_mod_inverse(Ri, R, &mont->N, ctx)) == NULL)\n            goto err;\n        if (!BN_lshift(Ri, Ri, mont->ri))\n            goto err;\n        if (!BN_sub_word(Ri, 1))\n            goto err;\n        if (!BN_div(&(mont->Ni), NULL, Ri, &mont->N, ctx))\n            goto err;\n    }\n#endif\n    BN_zero(&(mont->RR));\n    if (!BN_set_bit(&(mont->RR), mont->ri * 2))\n        goto err;\n    if (!BN_mod(&(mont->RR), &(mont->RR), &(mont->N), ctx))\n        goto err;\n    for (i = mont->RR.top, ret = mont->N.top; i < ret; i++)\n        mont->RR.d[i] = 0;\n    mont->RR.top = ret;\n    mont->RR.flags |= BN_FLG_FIXED_TOP;\n    ret = 1;\n err:\n    BN_CTX_end(ctx);\n    return ret;\n}', 'BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)\n{\n    bn_check_top(b);\n    if (a == b)\n        return a;\n    if (bn_wexpand(a, b->top) == NULL)\n        return NULL;\n    if (b->top > 0)\n        memcpy(a->d, b->d, sizeof(b->d[0]) * b->top);\n    a->neg = b->neg;\n    a->top = b->top;\n    a->flags |= b->flags & BN_FLG_FIXED_TOP;\n    bn_check_top(a);\n    return a;\n}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n    return (words <= a->dmax) ? a : bn_expand2(a, words);\n}']