apps/dhparam.c:361: error: BUFFER_OVERRUN_L3
  Offset: [16, +oo] (⇐ 1 + [15, +oo]) Size: [0, 8388607] by call to `DH_check`.
Showing all 31 steps of the trace


apps/dhparam.c:298:32: Call
296.                          num, g);
297.               BIO_printf(bio_err, "This is going to take a long time\n");
298.               if (dh == NULL || !DH_generate_parameters_ex(dh, num, g, cb)) {
                                      ^
299.                   BN_GENCB_free(cb);
300.                   ERR_print_errors(bio_err);

crypto/dh/dh_gen.c:72:1: Parameter `ret->p->top`
  70.                                   BN_GENCB *cb);
  71.   
  72. > int DH_generate_parameters_ex(DH *ret, int prime_len, int generator,
  73.                                 BN_GENCB *cb)
  74.   {

apps/dhparam.c:361:14: Call
359.   
360.       if (check) {
361.           if (!DH_check(dh, &i)) {
                    ^
362.               ERR_print_errors(bio_err);
363.               goto end;

crypto/dh/dh_check.c:74:1: Parameter `dh->g->top`
  72.    */
  73.   
  74. > int DH_check(const DH *dh, int *ret)
  75.   {
  76.       int ok = 0;

crypto/dh/dh_check.c:94:13: Call
  92.   
  93.       if (dh->q) {
  94.           if (BN_cmp(dh->g, BN_value_one()) <= 0)
                    ^
  95.               *ret |= DH_NOT_SUITABLE_GENERATOR;
  96.           else if (BN_cmp(dh->g, dh->p) >= 0)

crypto/bn/bn_lib.c:624:1: Parameter `a->top`
    622.   }
    623.   
    624. > int BN_cmp(const BIGNUM *a, const BIGNUM *b)
    625.   {
    626.       int i;

crypto/dh/dh_check.c:96:18: Call
  94.           if (BN_cmp(dh->g, BN_value_one()) <= 0)
  95.               *ret |= DH_NOT_SUITABLE_GENERATOR;
  96.           else if (BN_cmp(dh->g, dh->p) >= 0)
                         ^
  97.               *ret |= DH_NOT_SUITABLE_GENERATOR;
  98.           else {

crypto/bn/bn_lib.c:624:1: Parameter `a->top`
    622.   }
    623.   
    624. > int BN_cmp(const BIGNUM *a, const BIGNUM *b)
    625.   {
    626.       int i;

crypto/dh/dh_check.c:100:18: Call
  98.           else {
  99.               /* Check g^q == 1 mod p */
  100.               if (!BN_mod_exp(t1, dh->g, dh->q, dh->p, ctx))
                          ^
  101.                   goto err;
  102.               if (!BN_is_one(t1))

crypto/bn/bn_exp.c:191:1: Parameter `a->top`
    189.   }
    190.   
    191. > int BN_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m,
    192.                  BN_CTX *ctx)
    193.   {

crypto/bn/bn_exp.c:251:19: Call
    249.           } else
    250.   # endif
    251.               ret = BN_mod_exp_mont(r, a, p, m, ctx, NULL);
                             ^
    252.       } else
    253.   #endif

crypto/bn/bn_exp.c:401:1: Parameter `a->top`
      399.   }
      400.   
      401. > int BN_mod_exp_mont(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p,
      402.                       const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont)
      403.   {

crypto/bn/bn_exp.c:413:16: Call
      411.   
      412.       if (BN_get_flags(p, BN_FLG_CONSTTIME) != 0) {
      413.           return BN_mod_exp_mont_consttime(rr, a, p, m, ctx, in_mont);
                            ^
      414.       }
      415.   

crypto/bn/bn_exp.c:655:1: Parameter `a->top`
        653.    * http://www.daemonology.net/hyperthreading-considered-harmful/)
        654.    */
        655. > int BN_mod_exp_mont_consttime(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p,
        656.                                 const BIGNUM *m, BN_CTX *ctx,
        657.                                 BN_MONT_CTX *in_mont)

crypto/bn/bn_exp.c:812:17: Call
        810.           if (!BN_to_montgomery(&am, &am, mont, ctx))
        811.               goto err;
        812.       } else if (!BN_to_montgomery(&am, a, mont, ctx))
                               ^
        813.           goto err;
        814.   

crypto/bn/bn_lib.c:924:1: Parameter `a->top`
          922.   }
          923.   
          924. > int BN_to_montgomery(BIGNUM *r, const BIGNUM *a, BN_MONT_CTX *mont,
          925.                        BN_CTX *ctx)
          926.   {

crypto/bn/bn_lib.c:927:12: Call
          925.                        BN_CTX *ctx)
          926.   {
          927.       return BN_mod_mul_montgomery(r, a, &(mont->RR), mont, ctx);
                            ^
          928.   }
          929.   

crypto/bn/bn_mont.c:128:1: Parameter `a->top`
            126.   #endif
            127.   
            128. > int BN_mod_mul_montgomery(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
            129.                             BN_MONT_CTX *mont, BN_CTX *ctx)
            130.   {

crypto/bn/bn_mont.c:155:14: Call
            153.       bn_check_top(tmp);
            154.       if (a == b) {
            155.           if (!BN_sqr(tmp, a, ctx))
                                ^
            156.               goto err;
            157.       } else {

crypto/bn/bn_sqr.c:66:1: Parameter `a->top`
              64.    * I've just gone over this and it is now %20 faster on x86 - eay - 27 Jun 96
              65.    */
              66. > int BN_sqr(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx)
              67.   {
              68.       int max, al;

crypto/bn/bn_sqr.c:74:5: Assignment
              72.       bn_check_top(a);
              73.   
              74.       al = a->top;
                        ^
              75.       if (al <= 0) {
              76.           r->top = 0;

crypto/bn/bn_sqr.c:123:17: Call
              121.                   if (bn_wexpand(tmp, max) == NULL)
              122.                       goto err;
              123.                   bn_sqr_normal(rr->d, a->d, al, tmp->d);
                                     ^
              124.               }
              125.           }

crypto/bn/bn_sqr.c:153:1: <Offset trace>
151.   
152.   /* tmp must have 2*n words */
153. > void bn_sqr_normal(BN_ULONG *r, const BN_ULONG *a, int n, BN_ULONG *tmp)
154.   {
155.       int i, j, max;

crypto/bn/bn_sqr.c:153:1: Parameter `n`
                151.   
                152.   /* tmp must have 2*n words */
                153. > void bn_sqr_normal(BN_ULONG *r, const BN_ULONG *a, int n, BN_ULONG *tmp)
                154.   {
                155.       int i, j, max;

crypto/bn/bn_sqr.c:164:5: Assignment
                162.       rp[0] = rp[max - 1] = 0;
                163.       rp++;
                164.       j = n;
                           ^
                165.   
                166.       if (--j > 0) {

crypto/bn/bn_sqr.c:166:9: Assignment
                164.       j = n;
                165.   
                166.       if (--j > 0) {
                               ^
                167.           ap++;
                168.           rp[j] = bn_mul_words(rp, ap, j, ap[-1]);

crypto/bn/bn_sqr.c:153:1: <Length trace>
151.   
152.   /* tmp must have 2*n words */
153. > void bn_sqr_normal(BN_ULONG *r, const BN_ULONG *a, int n, BN_ULONG *tmp)
154.   {
155.       int i, j, max;

crypto/bn/bn_sqr.c:153:1: Parameter `*r`
                151.   
                152.   /* tmp must have 2*n words */
                153. > void bn_sqr_normal(BN_ULONG *r, const BN_ULONG *a, int n, BN_ULONG *tmp)
                154.   {
                155.       int i, j, max;

crypto/bn/bn_sqr.c:161:5: Assignment
                159.       max = n * 2;
                160.       ap = a;
                161.       rp = r;
                           ^
                162.       rp[0] = rp[max - 1] = 0;
                163.       rp++;

crypto/bn/bn_sqr.c:163:5: Assignment
                161.       rp = r;
                162.       rp[0] = rp[max - 1] = 0;
                163.       rp++;
                           ^
                164.       j = n;
                165.   

crypto/bn/bn_sqr.c:168:9: Array access: Offset: [16, +oo] (⇐ 1 + [15, +oo]) Size: [0, 8388607] by call to `DH_check` 
                166.       if (--j > 0) {
                167.           ap++;
                168.           rp[j] = bn_mul_words(rp, ap, j, ap[-1]);
                               ^
                169.           rp += 2;
                170.       }

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);
}
['int dhparam_main(int argc, char **argv)\n{\n    BIO *in = NULL, *out = NULL;\n    DH *dh = NULL;\n    char *infile = NULL, *outfile = NULL, *prog, *inrand = NULL;\n    int dsaparam = 0, i, text = 0, C = 0, ret = 1, num = 0, g = 0;\n    int informat = FORMAT_PEM, outformat = FORMAT_PEM, check = 0, noout = 0;\n    OPTION_CHOICE o;\n    prog = opt_init(argc, argv, dhparam_options);\n    while ((o = opt_next()) != OPT_EOF) {\n        switch (o) {\n        case OPT_EOF:\n        case OPT_ERR:\n opthelp:\n            BIO_printf(bio_err, "%s: Use -help for summary.\\n", prog);\n            goto end;\n        case OPT_HELP:\n            opt_help(dhparam_options);\n            ret = 0;\n            goto end;\n        case OPT_INFORM:\n            if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat))\n                goto opthelp;\n            break;\n        case OPT_OUTFORM:\n            if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))\n                goto opthelp;\n            break;\n        case OPT_IN:\n            infile = opt_arg();\n            break;\n        case OPT_OUT:\n            outfile = opt_arg();\n            break;\n        case OPT_ENGINE:\n            (void)setup_engine(opt_arg(), 0);\n            break;\n        case OPT_CHECK:\n            check = 1;\n            break;\n        case OPT_TEXT:\n            text = 1;\n            break;\n        case OPT_DSAPARAM:\n            dsaparam = 1;\n            break;\n        case OPT_C:\n            C = 1;\n            break;\n        case OPT_2:\n            g = 2;\n            break;\n        case OPT_5:\n            g = 5;\n            break;\n        case OPT_NOOUT:\n            noout = 1;\n            break;\n        case OPT_RAND:\n            inrand = opt_arg();\n            break;\n        }\n    }\n    argc = opt_num_rest();\n    argv = opt_rest();\n    if (argv[0] && (!opt_int(argv[0], &num) || num <= 0))\n        goto end;\n    if (g && !num)\n        num = DEFBITS;\n# ifndef OPENSSL_NO_DSA\n    if (dsaparam && g) {\n        BIO_printf(bio_err,\n                   "generator may not be chosen for DSA parameters\\n");\n        goto end;\n    }\n# endif\n    if (num && !g)\n        g = 2;\n    if (num) {\n        BN_GENCB *cb;\n        cb = BN_GENCB_new();\n        if (cb == NULL) {\n            ERR_print_errors(bio_err);\n            goto end;\n        }\n        BN_GENCB_set(cb, dh_cb, bio_err);\n        if (!app_RAND_load_file(NULL, 1) && inrand == NULL) {\n            BIO_printf(bio_err,\n                       "warning, not much extra random data, consider using the -rand option\\n");\n        }\n        if (inrand != NULL)\n            BIO_printf(bio_err, "%ld semi-random bytes loaded\\n",\n                       app_RAND_load_files(inrand));\n# ifndef OPENSSL_NO_DSA\n        if (dsaparam) {\n            DSA *dsa = DSA_new();\n            BIO_printf(bio_err,\n                       "Generating DSA parameters, %d bit long prime\\n", num);\n            if (dsa == NULL\n                || !DSA_generate_parameters_ex(dsa, num, NULL, 0, NULL, NULL,\n                                               cb)) {\n                DSA_free(dsa);\n                BN_GENCB_free(cb);\n                ERR_print_errors(bio_err);\n                goto end;\n            }\n            dh = DSA_dup_DH(dsa);\n            DSA_free(dsa);\n            if (dh == NULL) {\n                BN_GENCB_free(cb);\n                ERR_print_errors(bio_err);\n                goto end;\n            }\n        } else\n# endif\n        {\n            dh = DH_new();\n            BIO_printf(bio_err,\n                       "Generating DH parameters, %d bit long safe prime, generator %d\\n",\n                       num, g);\n            BIO_printf(bio_err, "This is going to take a long time\\n");\n            if (dh == NULL || !DH_generate_parameters_ex(dh, num, g, cb)) {\n                BN_GENCB_free(cb);\n                ERR_print_errors(bio_err);\n                goto end;\n            }\n        }\n        BN_GENCB_free(cb);\n        app_RAND_write_file(NULL);\n    } else {\n        in = bio_open_default(infile, \'r\', informat);\n        if (in == NULL)\n            goto end;\n# ifndef OPENSSL_NO_DSA\n        if (dsaparam) {\n            DSA *dsa;\n            if (informat == FORMAT_ASN1)\n                dsa = d2i_DSAparams_bio(in, NULL);\n            else\n                dsa = PEM_read_bio_DSAparams(in, NULL, NULL, NULL);\n            if (dsa == NULL) {\n                BIO_printf(bio_err, "unable to load DSA parameters\\n");\n                ERR_print_errors(bio_err);\n                goto end;\n            }\n            dh = DSA_dup_DH(dsa);\n            DSA_free(dsa);\n            if (dh == NULL) {\n                ERR_print_errors(bio_err);\n                goto end;\n            }\n        } else\n# endif\n        {\n            if (informat == FORMAT_ASN1)\n                dh = d2i_DHparams_bio(in, NULL);\n            else\n                dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL);\n            if (dh == NULL) {\n                BIO_printf(bio_err, "unable to load DH parameters\\n");\n                ERR_print_errors(bio_err);\n                goto end;\n            }\n        }\n    }\n    out = bio_open_default(outfile, \'w\', outformat);\n    if (out == NULL)\n        goto end;\n    if (text) {\n        DHparams_print(out, dh);\n    }\n    if (check) {\n        if (!DH_check(dh, &i)) {\n            ERR_print_errors(bio_err);\n            goto end;\n        }\n        if (i & DH_CHECK_P_NOT_PRIME)\n            printf("p value is not prime\\n");\n        if (i & DH_CHECK_P_NOT_SAFE_PRIME)\n            printf("p value is not a safe prime\\n");\n        if (i & DH_UNABLE_TO_CHECK_GENERATOR)\n            printf("unable to check the generator value\\n");\n        if (i & DH_NOT_SUITABLE_GENERATOR)\n            printf("the g value is not a generator\\n");\n        if (i == 0)\n            printf("DH parameters appear to be ok.\\n");\n    }\n    if (C) {\n        unsigned char *data;\n        int len, bits;\n        len = BN_num_bytes(dh->p);\n        bits = BN_num_bits(dh->p);\n        data = app_malloc(len, "print a BN");\n        BIO_printf(out, "#ifndef HEADER_DH_H\\n"\n                        "# include <openssl/dh.h>\\n"\n                        "#endif\\n"\n                        "\\n");\n        BIO_printf(out, "DH *get_dh%d()\\n{\\n", bits);\n        print_bignum_var(out, dh->p, "dhp", bits, data);\n        print_bignum_var(out, dh->g, "dhg", bits, data);\n        BIO_printf(out, "    DH *dh = DN_new();\\n"\n                        "\\n"\n                        "    if (dh == NULL)\\n"\n                        "        return NULL;\\n");\n        BIO_printf(out, "    dh->p = BN_bin2bn(dhp_%d, sizeof (dhp_%d), NULL);\\n",\n               bits, bits);\n        BIO_printf(out, "    dh->g = BN_bin2bn(dhg_%d, sizeof (dhg_%d), NULL);\\n",\n               bits, bits);\n        BIO_printf(out, "    if (!dh->p || !dh->g) {\\n"\n                        "        DH_free(dh);\\n"\n                        "        return NULL;\\n"\n                        "    }\\n");\n        if (dh->length)\n            BIO_printf(out,\n                        "    dh->length = %ld;\\n", dh->length);\n        BIO_printf(out, "    return dh;\\n}\\n");\n        OPENSSL_free(data);\n    }\n    if (!noout) {\n        if (outformat == FORMAT_ASN1)\n            i = i2d_DHparams_bio(out, dh);\n        else if (dh->q)\n            i = PEM_write_bio_DHxparams(out, dh);\n        else\n            i = PEM_write_bio_DHparams(out, dh);\n        if (!i) {\n            BIO_printf(bio_err, "unable to write DH parameters\\n");\n            ERR_print_errors(bio_err);\n            goto end;\n        }\n    }\n    ret = 0;\n end:\n    BIO_free(in);\n    BIO_free_all(out);\n    DH_free(dh);\n    return (ret);\n}', 'int DH_generate_parameters_ex(DH *ret, int prime_len, int generator,\n                              BN_GENCB *cb)\n{\n    if (ret->meth->generate_params)\n        return ret->meth->generate_params(ret, prime_len, generator, cb);\n    return dh_builtin_genparams(ret, prime_len, generator, cb);\n}', 'int DH_check(const DH *dh, int *ret)\n{\n    int ok = 0;\n    BN_CTX *ctx = NULL;\n    BN_ULONG l;\n    BIGNUM *t1 = NULL, *t2 = NULL;\n    *ret = 0;\n    ctx = BN_CTX_new();\n    if (ctx == NULL)\n        goto err;\n    BN_CTX_start(ctx);\n    t1 = BN_CTX_get(ctx);\n    if (t1 == NULL)\n        goto err;\n    t2 = BN_CTX_get(ctx);\n    if (t2 == NULL)\n        goto err;\n    if (dh->q) {\n        if (BN_cmp(dh->g, BN_value_one()) <= 0)\n            *ret |= DH_NOT_SUITABLE_GENERATOR;\n        else if (BN_cmp(dh->g, dh->p) >= 0)\n            *ret |= DH_NOT_SUITABLE_GENERATOR;\n        else {\n            if (!BN_mod_exp(t1, dh->g, dh->q, dh->p, ctx))\n                goto err;\n            if (!BN_is_one(t1))\n                *ret |= DH_NOT_SUITABLE_GENERATOR;\n        }\n        if (!BN_is_prime_ex(dh->q, BN_prime_checks, ctx, NULL))\n            *ret |= DH_CHECK_Q_NOT_PRIME;\n        if (!BN_div(t1, t2, dh->p, dh->q, ctx))\n            goto err;\n        if (!BN_is_one(t2))\n            *ret |= DH_CHECK_INVALID_Q_VALUE;\n        if (dh->j && BN_cmp(dh->j, t1))\n            *ret |= DH_CHECK_INVALID_J_VALUE;\n    } else if (BN_is_word(dh->g, DH_GENERATOR_2)) {\n        l = BN_mod_word(dh->p, 24);\n        if (l != 11)\n            *ret |= DH_NOT_SUITABLE_GENERATOR;\n    } else if (BN_is_word(dh->g, DH_GENERATOR_5)) {\n        l = BN_mod_word(dh->p, 10);\n        if ((l != 3) && (l != 7))\n            *ret |= DH_NOT_SUITABLE_GENERATOR;\n    } else\n        *ret |= DH_UNABLE_TO_CHECK_GENERATOR;\n    if (!BN_is_prime_ex(dh->p, BN_prime_checks, ctx, NULL))\n        *ret |= DH_CHECK_P_NOT_PRIME;\n    else if (!dh->q) {\n        if (!BN_rshift1(t1, dh->p))\n            goto err;\n        if (!BN_is_prime_ex(t1, BN_prime_checks, ctx, NULL))\n            *ret |= DH_CHECK_P_NOT_SAFE_PRIME;\n    }\n    ok = 1;\n err:\n    if (ctx != NULL) {\n        BN_CTX_end(ctx);\n        BN_CTX_free(ctx);\n    }\n    return (ok);\n}', 'int BN_cmp(const BIGNUM *a, const BIGNUM *b)\n{\n    int i;\n    int gt, lt;\n    BN_ULONG t1, t2;\n    if ((a == NULL) || (b == NULL)) {\n        if (a != NULL)\n            return (-1);\n        else if (b != NULL)\n            return (1);\n        else\n            return (0);\n    }\n    bn_check_top(a);\n    bn_check_top(b);\n    if (a->neg != b->neg) {\n        if (a->neg)\n            return (-1);\n        else\n            return (1);\n    }\n    if (a->neg == 0) {\n        gt = 1;\n        lt = -1;\n    } else {\n        gt = -1;\n        lt = 1;\n    }\n    if (a->top > b->top)\n        return (gt);\n    if (a->top < b->top)\n        return (lt);\n    for (i = a->top - 1; i >= 0; i--) {\n        t1 = a->d[i];\n        t2 = b->d[i];\n        if (t1 > t2)\n            return (gt);\n        if (t1 < t2)\n            return (lt);\n    }\n    return (0);\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_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_mod_exp_mont(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p,\n                    const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont)\n{\n    int i, j, bits, ret = 0, wstart, wend, window, wvalue;\n    int start = 1;\n    BIGNUM *d, *r;\n    const BIGNUM *aa;\n    BIGNUM *val[TABLE_SIZE];\n    BN_MONT_CTX *mont = NULL;\n    if (BN_get_flags(p, BN_FLG_CONSTTIME) != 0) {\n        return BN_mod_exp_mont_consttime(rr, a, p, m, ctx, in_mont);\n    }\n    bn_check_top(a);\n    bn_check_top(p);\n    bn_check_top(m);\n    if (!BN_is_odd(m)) {\n        BNerr(BN_F_BN_MOD_EXP_MONT, BN_R_CALLED_WITH_EVEN_MODULUS);\n        return (0);\n    }\n    bits = BN_num_bits(p);\n    if (bits == 0) {\n        if (BN_is_one(m)) {\n            ret = 1;\n            BN_zero(rr);\n        } else {\n            ret = BN_one(rr);\n        }\n        return ret;\n    }\n    BN_CTX_start(ctx);\n    d = BN_CTX_get(ctx);\n    r = BN_CTX_get(ctx);\n    val[0] = BN_CTX_get(ctx);\n    if (!d || !r || !val[0])\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    if (a->neg || BN_ucmp(a, m) >= 0) {\n        if (!BN_nnmod(val[0], a, m, ctx))\n            goto err;\n        aa = val[0];\n    } else\n        aa = a;\n    if (BN_is_zero(aa)) {\n        BN_zero(rr);\n        ret = 1;\n        goto err;\n    }\n    if (!BN_to_montgomery(val[0], aa, mont, ctx))\n        goto err;\n    window = BN_window_bits_for_exponent_size(bits);\n    if (window > 1) {\n        if (!BN_mod_mul_montgomery(d, val[0], val[0], mont, ctx))\n            goto err;\n        j = 1 << (window - 1);\n        for (i = 1; i < j; i++) {\n            if (((val[i] = BN_CTX_get(ctx)) == NULL) ||\n                !BN_mod_mul_montgomery(val[i], val[i - 1], d, mont, ctx))\n                goto err;\n        }\n    }\n    start = 1;\n    wvalue = 0;\n    wstart = bits - 1;\n    wend = 0;\n#if 1\n    j = m->top;\n    if (m->d[j - 1] & (((BN_ULONG)1) << (BN_BITS2 - 1))) {\n        if (bn_wexpand(r, j) == NULL)\n            goto err;\n        r->d[0] = (0 - m->d[0]) & BN_MASK2;\n        for (i = 1; i < j; i++)\n            r->d[i] = (~m->d[i]) & BN_MASK2;\n        r->top = j;\n        bn_correct_top(r);\n    } else\n#endif\n    if (!BN_to_montgomery(r, BN_value_one(), mont, ctx))\n        goto err;\n    for (;;) {\n        if (BN_is_bit_set(p, wstart) == 0) {\n            if (!start) {\n                if (!BN_mod_mul_montgomery(r, r, r, mont, ctx))\n                    goto err;\n            }\n            if (wstart == 0)\n                break;\n            wstart--;\n            continue;\n        }\n        j = wstart;\n        wvalue = 1;\n        wend = 0;\n        for (i = 1; i < window; i++) {\n            if (wstart - i < 0)\n                break;\n            if (BN_is_bit_set(p, wstart - i)) {\n                wvalue <<= (i - wend);\n                wvalue |= 1;\n                wend = i;\n            }\n        }\n        j = wend + 1;\n        if (!start)\n            for (i = 0; i < j; i++) {\n                if (!BN_mod_mul_montgomery(r, r, r, mont, ctx))\n                    goto err;\n            }\n        if (!BN_mod_mul_montgomery(r, r, val[wvalue >> 1], mont, ctx))\n            goto err;\n        wstart -= wend + 1;\n        wvalue = 0;\n        start = 0;\n        if (wstart < 0)\n            break;\n    }\n#if defined(SPARC_T4_MONT)\n    if (OPENSSL_sparcv9cap_P[0] & (SPARCV9_VIS3 | SPARCV9_PREFER_FPU)) {\n        j = mont->N.top;\n        val[0]->d[0] = 1;\n        for (i = 1; i < j; i++)\n            val[0]->d[i] = 0;\n        val[0]->top = j;\n        if (!BN_mod_mul_montgomery(rr, r, val[0], mont, ctx))\n            goto err;\n    } else\n#endif\n    if (!BN_from_montgomery(rr, r, mont, ctx))\n        goto err;\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_mod_exp_mont_consttime(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p,\n                              const BIGNUM *m, BN_CTX *ctx,\n                              BN_MONT_CTX *in_mont)\n{\n    int i, bits, ret = 0, window, wvalue;\n    int top;\n    BN_MONT_CTX *mont = NULL;\n    int numPowers;\n    unsigned char *powerbufFree = NULL;\n    int powerbufLen = 0;\n    unsigned char *powerbuf = NULL;\n    BIGNUM tmp, am;\n#if defined(SPARC_T4_MONT)\n    unsigned int t4 = 0;\n#endif\n    bn_check_top(a);\n    bn_check_top(p);\n    bn_check_top(m);\n    if (!BN_is_odd(m)) {\n        BNerr(BN_F_BN_MOD_EXP_MONT_CONSTTIME, BN_R_CALLED_WITH_EVEN_MODULUS);\n        return (0);\n    }\n    top = m->top;\n    bits = BN_num_bits(p);\n    if (bits == 0) {\n        if (BN_is_one(m)) {\n            ret = 1;\n            BN_zero(rr);\n        } else {\n            ret = BN_one(rr);\n        }\n        return ret;\n    }\n    BN_CTX_start(ctx);\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#ifdef RSAZ_ENABLED\n    if ((16 == a->top) && (16 == p->top) && (BN_num_bits(m) == 1024)\n        && rsaz_avx2_eligible()) {\n        if (NULL == bn_wexpand(rr, 16))\n            goto err;\n        RSAZ_1024_mod_exp_avx2(rr->d, a->d, p->d, m->d, mont->RR.d,\n                               mont->n0[0]);\n        rr->top = 16;\n        rr->neg = 0;\n        bn_correct_top(rr);\n        ret = 1;\n        goto err;\n    } else if ((8 == a->top) && (8 == p->top) && (BN_num_bits(m) == 512)) {\n        if (NULL == bn_wexpand(rr, 8))\n            goto err;\n        RSAZ_512_mod_exp(rr->d, a->d, p->d, m->d, mont->n0[0], mont->RR.d);\n        rr->top = 8;\n        rr->neg = 0;\n        bn_correct_top(rr);\n        ret = 1;\n        goto err;\n    }\n#endif\n    window = BN_window_bits_for_ctime_exponent_size(bits);\n#if defined(SPARC_T4_MONT)\n    if (window >= 5 && (top & 15) == 0 && top <= 64 &&\n        (OPENSSL_sparcv9cap_P[1] & (CFR_MONTMUL | CFR_MONTSQR)) ==\n        (CFR_MONTMUL | CFR_MONTSQR) && (t4 = OPENSSL_sparcv9cap_P[0]))\n        window = 5;\n    else\n#endif\n#if defined(OPENSSL_BN_ASM_MONT5)\n    if (window >= 5) {\n        window = 5;\n        if ((top & 7) == 0)\n            powerbufLen += 2 * top * sizeof(m->d[0]);\n    }\n#endif\n    (void)0;\n    numPowers = 1 << window;\n    powerbufLen += sizeof(m->d[0]) * (top * numPowers +\n                                      ((2 * top) >\n                                       numPowers ? (2 * top) : numPowers));\n#ifdef alloca\n    if (powerbufLen < 3072)\n        powerbufFree =\n            alloca(powerbufLen + MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH);\n    else\n#endif\n        if ((powerbufFree =\n             OPENSSL_malloc(powerbufLen + MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH))\n            == NULL)\n        goto err;\n    powerbuf = MOD_EXP_CTIME_ALIGN(powerbufFree);\n    memset(powerbuf, 0, powerbufLen);\n#ifdef alloca\n    if (powerbufLen < 3072)\n        powerbufFree = NULL;\n#endif\n    tmp.d = (BN_ULONG *)(powerbuf + sizeof(m->d[0]) * top * numPowers);\n    am.d = tmp.d + top;\n    tmp.top = am.top = 0;\n    tmp.dmax = am.dmax = top;\n    tmp.neg = am.neg = 0;\n    tmp.flags = am.flags = BN_FLG_STATIC_DATA;\n#if 1\n    if (m->d[top - 1] & (((BN_ULONG)1) << (BN_BITS2 - 1))) {\n        tmp.d[0] = (0 - m->d[0]) & BN_MASK2;\n        for (i = 1; i < top; i++)\n            tmp.d[i] = (~m->d[i]) & BN_MASK2;\n        tmp.top = top;\n    } else\n#endif\n    if (!BN_to_montgomery(&tmp, BN_value_one(), mont, ctx))\n        goto err;\n    if (a->neg || BN_ucmp(a, m) >= 0) {\n        if (!BN_mod(&am, a, m, ctx))\n            goto err;\n        if (!BN_to_montgomery(&am, &am, mont, ctx))\n            goto err;\n    } else if (!BN_to_montgomery(&am, a, mont, ctx))\n        goto err;\n#if defined(SPARC_T4_MONT)\n    if (t4) {\n        typedef int (*bn_pwr5_mont_f) (BN_ULONG *tp, const BN_ULONG *np,\n                                       const BN_ULONG *n0, const void *table,\n                                       int power, int bits);\n        int bn_pwr5_mont_t4_8(BN_ULONG *tp, const BN_ULONG *np,\n                              const BN_ULONG *n0, const void *table,\n                              int power, int bits);\n        int bn_pwr5_mont_t4_16(BN_ULONG *tp, const BN_ULONG *np,\n                               const BN_ULONG *n0, const void *table,\n                               int power, int bits);\n        int bn_pwr5_mont_t4_24(BN_ULONG *tp, const BN_ULONG *np,\n                               const BN_ULONG *n0, const void *table,\n                               int power, int bits);\n        int bn_pwr5_mont_t4_32(BN_ULONG *tp, const BN_ULONG *np,\n                               const BN_ULONG *n0, const void *table,\n                               int power, int bits);\n        static const bn_pwr5_mont_f pwr5_funcs[4] = {\n            bn_pwr5_mont_t4_8, bn_pwr5_mont_t4_16,\n            bn_pwr5_mont_t4_24, bn_pwr5_mont_t4_32\n        };\n        bn_pwr5_mont_f pwr5_worker = pwr5_funcs[top / 16 - 1];\n        typedef int (*bn_mul_mont_f) (BN_ULONG *rp, const BN_ULONG *ap,\n                                      const void *bp, const BN_ULONG *np,\n                                      const BN_ULONG *n0);\n        int bn_mul_mont_t4_8(BN_ULONG *rp, const BN_ULONG *ap, const void *bp,\n                             const BN_ULONG *np, const BN_ULONG *n0);\n        int bn_mul_mont_t4_16(BN_ULONG *rp, const BN_ULONG *ap,\n                              const void *bp, const BN_ULONG *np,\n                              const BN_ULONG *n0);\n        int bn_mul_mont_t4_24(BN_ULONG *rp, const BN_ULONG *ap,\n                              const void *bp, const BN_ULONG *np,\n                              const BN_ULONG *n0);\n        int bn_mul_mont_t4_32(BN_ULONG *rp, const BN_ULONG *ap,\n                              const void *bp, const BN_ULONG *np,\n                              const BN_ULONG *n0);\n        static const bn_mul_mont_f mul_funcs[4] = {\n            bn_mul_mont_t4_8, bn_mul_mont_t4_16,\n            bn_mul_mont_t4_24, bn_mul_mont_t4_32\n        };\n        bn_mul_mont_f mul_worker = mul_funcs[top / 16 - 1];\n        void bn_mul_mont_vis3(BN_ULONG *rp, const BN_ULONG *ap,\n                              const void *bp, const BN_ULONG *np,\n                              const BN_ULONG *n0, int num);\n        void bn_mul_mont_t4(BN_ULONG *rp, const BN_ULONG *ap,\n                            const void *bp, const BN_ULONG *np,\n                            const BN_ULONG *n0, int num);\n        void bn_mul_mont_gather5_t4(BN_ULONG *rp, const BN_ULONG *ap,\n                                    const void *table, const BN_ULONG *np,\n                                    const BN_ULONG *n0, int num, int power);\n        void bn_flip_n_scatter5_t4(const BN_ULONG *inp, size_t num,\n                                   void *table, size_t power);\n        void bn_gather5_t4(BN_ULONG *out, size_t num,\n                           void *table, size_t power);\n        void bn_flip_t4(BN_ULONG *dst, BN_ULONG *src, size_t num);\n        BN_ULONG *np = mont->N.d, *n0 = mont->n0;\n        int stride = 5 * (6 - (top / 16 - 1));\n        for (i = am.top; i < top; i++)\n            am.d[i] = 0;\n        for (i = tmp.top; i < top; i++)\n            tmp.d[i] = 0;\n        bn_flip_n_scatter5_t4(tmp.d, top, powerbuf, 0);\n        bn_flip_n_scatter5_t4(am.d, top, powerbuf, 1);\n        if (!(*mul_worker) (tmp.d, am.d, am.d, np, n0) &&\n            !(*mul_worker) (tmp.d, am.d, am.d, np, n0))\n            bn_mul_mont_vis3(tmp.d, am.d, am.d, np, n0, top);\n        bn_flip_n_scatter5_t4(tmp.d, top, powerbuf, 2);\n        for (i = 3; i < 32; i++) {\n            if (!(*mul_worker) (tmp.d, tmp.d, am.d, np, n0) &&\n                !(*mul_worker) (tmp.d, tmp.d, am.d, np, n0))\n                bn_mul_mont_vis3(tmp.d, tmp.d, am.d, np, n0, top);\n            bn_flip_n_scatter5_t4(tmp.d, top, powerbuf, i);\n        }\n        np = alloca(top * sizeof(BN_ULONG));\n        top /= 2;\n        bn_flip_t4(np, mont->N.d, top);\n        bits--;\n        for (wvalue = 0, i = bits % 5; i >= 0; i--, bits--)\n            wvalue = (wvalue << 1) + BN_is_bit_set(p, bits);\n        bn_gather5_t4(tmp.d, top, powerbuf, wvalue);\n        while (bits >= 0) {\n            if (bits < stride)\n                stride = bits + 1;\n            bits -= stride;\n            wvalue = bn_get_bits(p, bits + 1);\n            if ((*pwr5_worker) (tmp.d, np, n0, powerbuf, wvalue, stride))\n                continue;\n            if ((*pwr5_worker) (tmp.d, np, n0, powerbuf, wvalue, stride))\n                continue;\n            bits += stride - 5;\n            wvalue >>= stride - 5;\n            wvalue &= 31;\n            bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n            bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n            bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n            bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n            bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n            bn_mul_mont_gather5_t4(tmp.d, tmp.d, powerbuf, np, n0, top,\n                                   wvalue);\n        }\n        bn_flip_t4(tmp.d, tmp.d, top);\n        top *= 2;\n        tmp.top = top;\n        bn_correct_top(&tmp);\n        OPENSSL_cleanse(np, top * sizeof(BN_ULONG));\n    } else\n#endif\n#if defined(OPENSSL_BN_ASM_MONT5)\n    if (window == 5 && top > 1) {\n        void bn_mul_mont_gather5(BN_ULONG *rp, const BN_ULONG *ap,\n                                 const void *table, const BN_ULONG *np,\n                                 const BN_ULONG *n0, int num, int power);\n        void bn_scatter5(const BN_ULONG *inp, size_t num,\n                         void *table, size_t power);\n        void bn_gather5(BN_ULONG *out, size_t num, void *table, size_t power);\n        void bn_power5(BN_ULONG *rp, const BN_ULONG *ap,\n                       const void *table, const BN_ULONG *np,\n                       const BN_ULONG *n0, int num, int power);\n        int bn_get_bits5(const BN_ULONG *ap, int off);\n        int bn_from_montgomery(BN_ULONG *rp, const BN_ULONG *ap,\n                               const BN_ULONG *not_used, const BN_ULONG *np,\n                               const BN_ULONG *n0, int num);\n        BN_ULONG *np = mont->N.d, *n0 = mont->n0, *np2;\n        for (i = am.top; i < top; i++)\n            am.d[i] = 0;\n        for (i = tmp.top; i < top; i++)\n            tmp.d[i] = 0;\n        if (top & 7)\n            np2 = np;\n        else\n            for (np2 = am.d + top, i = 0; i < top; i++)\n                np2[2 * i] = np[i];\n        bn_scatter5(tmp.d, top, powerbuf, 0);\n        bn_scatter5(am.d, am.top, powerbuf, 1);\n        bn_mul_mont(tmp.d, am.d, am.d, np, n0, top);\n        bn_scatter5(tmp.d, top, powerbuf, 2);\n# if 0\n        for (i = 3; i < 32; i++) {\n            bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np2, n0, top, i - 1);\n            bn_scatter5(tmp.d, top, powerbuf, i);\n        }\n# else\n        for (i = 4; i < 32; i *= 2) {\n            bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n            bn_scatter5(tmp.d, top, powerbuf, i);\n        }\n        for (i = 3; i < 8; i += 2) {\n            int j;\n            bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np2, n0, top, i - 1);\n            bn_scatter5(tmp.d, top, powerbuf, i);\n            for (j = 2 * i; j < 32; j *= 2) {\n                bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n                bn_scatter5(tmp.d, top, powerbuf, j);\n            }\n        }\n        for (; i < 16; i += 2) {\n            bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np2, n0, top, i - 1);\n            bn_scatter5(tmp.d, top, powerbuf, i);\n            bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n            bn_scatter5(tmp.d, top, powerbuf, 2 * i);\n        }\n        for (; i < 32; i += 2) {\n            bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np2, n0, top, i - 1);\n            bn_scatter5(tmp.d, top, powerbuf, i);\n        }\n# endif\n        bits--;\n        for (wvalue = 0, i = bits % 5; i >= 0; i--, bits--)\n            wvalue = (wvalue << 1) + BN_is_bit_set(p, bits);\n        bn_gather5(tmp.d, top, powerbuf, wvalue);\n        if (top & 7)\n            while (bits >= 0) {\n                for (wvalue = 0, i = 0; i < 5; i++, bits--)\n                    wvalue = (wvalue << 1) + BN_is_bit_set(p, bits);\n                bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n                bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n                bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n                bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n                bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n                bn_mul_mont_gather5(tmp.d, tmp.d, powerbuf, np, n0, top,\n                                    wvalue);\n        } else {\n            while (bits >= 0) {\n                wvalue = bn_get_bits5(p->d, bits - 4);\n                bits -= 5;\n                bn_power5(tmp.d, tmp.d, powerbuf, np2, n0, top, wvalue);\n            }\n        }\n        ret = bn_from_montgomery(tmp.d, tmp.d, NULL, np2, n0, top);\n        tmp.top = top;\n        bn_correct_top(&tmp);\n        if (ret) {\n            if (!BN_copy(rr, &tmp))\n                ret = 0;\n            goto err;\n        }\n    } else\n#endif\n    {\n        if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&tmp, top, powerbuf, 0, numPowers))\n            goto err;\n        if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&am, top, powerbuf, 1, numPowers))\n            goto err;\n        if (window > 1) {\n            if (!BN_mod_mul_montgomery(&tmp, &am, &am, mont, ctx))\n                goto err;\n            if (!MOD_EXP_CTIME_COPY_TO_PREBUF\n                (&tmp, top, powerbuf, 2, numPowers))\n                goto err;\n            for (i = 3; i < numPowers; i++) {\n                if (!BN_mod_mul_montgomery(&tmp, &am, &tmp, mont, ctx))\n                    goto err;\n                if (!MOD_EXP_CTIME_COPY_TO_PREBUF\n                    (&tmp, top, powerbuf, i, numPowers))\n                    goto err;\n            }\n        }\n        bits--;\n        for (wvalue = 0, i = bits % window; i >= 0; i--, bits--)\n            wvalue = (wvalue << 1) + BN_is_bit_set(p, bits);\n        if (!MOD_EXP_CTIME_COPY_FROM_PREBUF\n            (&tmp, top, powerbuf, wvalue, numPowers))\n            goto err;\n        while (bits >= 0) {\n            wvalue = 0;\n            for (i = 0; i < window; i++, bits--) {\n                if (!BN_mod_mul_montgomery(&tmp, &tmp, &tmp, mont, ctx))\n                    goto err;\n                wvalue = (wvalue << 1) + BN_is_bit_set(p, bits);\n            }\n            if (!MOD_EXP_CTIME_COPY_FROM_PREBUF\n                (&am, top, powerbuf, wvalue, numPowers))\n                goto err;\n            if (!BN_mod_mul_montgomery(&tmp, &tmp, &am, mont, ctx))\n                goto err;\n        }\n    }\n#if defined(SPARC_T4_MONT)\n    if (OPENSSL_sparcv9cap_P[0] & (SPARCV9_VIS3 | SPARCV9_PREFER_FPU)) {\n        am.d[0] = 1;\n        for (i = 1; i < top; i++)\n            am.d[i] = 0;\n        if (!BN_mod_mul_montgomery(rr, &tmp, &am, mont, ctx))\n            goto err;\n    } else\n#endif\n    if (!BN_from_montgomery(rr, &tmp, mont, ctx))\n        goto err;\n    ret = 1;\n err:\n    if (in_mont == NULL)\n        BN_MONT_CTX_free(mont);\n    if (powerbuf != NULL) {\n        OPENSSL_cleanse(powerbuf, powerbufLen);\n        OPENSSL_free(powerbufFree);\n    }\n    BN_CTX_end(ctx);\n    return (ret);\n}', 'int BN_to_montgomery(BIGNUM *r, const BIGNUM *a, BN_MONT_CTX *mont,\n                     BN_CTX *ctx)\n{\n    return BN_mod_mul_montgomery(r, a, &(mont->RR), mont, ctx);\n}', 'int BN_mod_mul_montgomery(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,\n                          BN_MONT_CTX *mont, BN_CTX *ctx)\n{\n    BIGNUM *tmp;\n    int ret = 0;\n#if defined(OPENSSL_BN_ASM_MONT) && defined(MONT_WORD)\n    int num = mont->N.top;\n    if (num > 1 && a->top == num && b->top == num) {\n        if (bn_wexpand(r, num) == NULL)\n            return (0);\n        if (bn_mul_mont(r->d, a->d, b->d, mont->N.d, mont->n0, num)) {\n            r->neg = a->neg ^ b->neg;\n            r->top = num;\n            bn_correct_top(r);\n            return (1);\n        }\n    }\n#endif\n    BN_CTX_start(ctx);\n    tmp = BN_CTX_get(ctx);\n    if (tmp == NULL)\n        goto err;\n    bn_check_top(tmp);\n    if (a == b) {\n        if (!BN_sqr(tmp, a, ctx))\n            goto err;\n    } else {\n        if (!BN_mul(tmp, a, b, ctx))\n            goto err;\n    }\n#ifdef MONT_WORD\n    if (!BN_from_montgomery_word(r, tmp, mont))\n        goto err;\n#else\n    if (!BN_from_montgomery(r, tmp, mont, ctx))\n        goto err;\n#endif\n    bn_check_top(r);\n    ret = 1;\n err:\n    BN_CTX_end(ctx);\n    return (ret);\n}', 'int BN_sqr(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx)\n{\n    int max, al;\n    int ret = 0;\n    BIGNUM *tmp, *rr;\n    bn_check_top(a);\n    al = a->top;\n    if (al <= 0) {\n        r->top = 0;\n        r->neg = 0;\n        return 1;\n    }\n    BN_CTX_start(ctx);\n    rr = (a != r) ? r : BN_CTX_get(ctx);\n    tmp = BN_CTX_get(ctx);\n    if (!rr || !tmp)\n        goto err;\n    max = 2 * al;\n    if (bn_wexpand(rr, max) == NULL)\n        goto err;\n    if (al == 4) {\n#ifndef BN_SQR_COMBA\n        BN_ULONG t[8];\n        bn_sqr_normal(rr->d, a->d, 4, t);\n#else\n        bn_sqr_comba4(rr->d, a->d);\n#endif\n    } else if (al == 8) {\n#ifndef BN_SQR_COMBA\n        BN_ULONG t[16];\n        bn_sqr_normal(rr->d, a->d, 8, t);\n#else\n        bn_sqr_comba8(rr->d, a->d);\n#endif\n    } else {\n#if defined(BN_RECURSION)\n        if (al < BN_SQR_RECURSIVE_SIZE_NORMAL) {\n            BN_ULONG t[BN_SQR_RECURSIVE_SIZE_NORMAL * 2];\n            bn_sqr_normal(rr->d, a->d, al, t);\n        } else {\n            int j, k;\n            j = BN_num_bits_word((BN_ULONG)al);\n            j = 1 << (j - 1);\n            k = j + j;\n            if (al == j) {\n                if (bn_wexpand(tmp, k * 2) == NULL)\n                    goto err;\n                bn_sqr_recursive(rr->d, a->d, al, tmp->d);\n            } else {\n                if (bn_wexpand(tmp, max) == NULL)\n                    goto err;\n                bn_sqr_normal(rr->d, a->d, al, tmp->d);\n            }\n        }\n#else\n        if (bn_wexpand(tmp, max) == NULL)\n            goto err;\n        bn_sqr_normal(rr->d, a->d, al, tmp->d);\n#endif\n    }\n    rr->neg = 0;\n    if (a->d[al - 1] == (a->d[al - 1] & BN_MASK2l))\n        rr->top = max - 1;\n    else\n        rr->top = max;\n    if (rr != r)\n        BN_copy(r, rr);\n    ret = 1;\n err:\n    bn_check_top(rr);\n    bn_check_top(tmp);\n    BN_CTX_end(ctx);\n    return (ret);\n}', 'void bn_sqr_normal(BN_ULONG *r, const BN_ULONG *a, int n, BN_ULONG *tmp)\n{\n    int i, j, max;\n    const BN_ULONG *ap;\n    BN_ULONG *rp;\n    max = n * 2;\n    ap = a;\n    rp = r;\n    rp[0] = rp[max - 1] = 0;\n    rp++;\n    j = n;\n    if (--j > 0) {\n        ap++;\n        rp[j] = bn_mul_words(rp, ap, j, ap[-1]);\n        rp += 2;\n    }\n    for (i = n - 2; i > 0; i--) {\n        j--;\n        ap++;\n        rp[j] = bn_mul_add_words(rp, ap, j, ap[-1]);\n        rp += 2;\n    }\n    bn_add_words(r, r, r, max);\n    bn_sqr_words(tmp, a, n);\n    bn_add_words(r, r, tmp, max);\n}']