id
int64
1
36.7k
label
int64
0
1
bug_url
stringlengths
91
134
bug_function
stringlengths
13
72.7k
functions
stringlengths
17
79.2k
801
0
https://github.com/openssl/openssl/blob/04485c5bc0dc7f49940e6d91b27cdcc7b83a8ab5/crypto/bn/bn_ctx.c/#L355
static unsigned int BN_STACK_pop(BN_STACK *st) { return st->indexes[--(st->depth)]; }
['int ec_GFp_simple_set_Jprojective_coordinates_GFp(const EC_GROUP *group, EC_POINT *point,\n\tconst BIGNUM *x, const BIGNUM *y, const BIGNUM *z, BN_CTX *ctx)\n\t{\n\tBN_CTX *new_ctx = NULL;\n\tint ret = 0;\n\tif (ctx == NULL)\n\t\t{\n\t\tctx = new_ctx = BN_CTX_new();\n\t\tif (ctx == NULL)\n\t\t\treturn 0;\n\t\t}\n\tif (x != NULL)\n\t\t{\n\t\tif (!BN_nnmod(&point->X, x, &group->field, ctx)) goto err;\n\t\tif (group->meth->field_encode)\n\t\t\t{\n\t\t\tif (!group->meth->field_encode(group, &point->X, &point->X, ctx)) goto err;\n\t\t\t}\n\t\t}\n\tif (y != NULL)\n\t\t{\n\t\tif (!BN_nnmod(&point->Y, y, &group->field, ctx)) goto err;\n\t\tif (group->meth->field_encode)\n\t\t\t{\n\t\t\tif (!group->meth->field_encode(group, &point->Y, &point->Y, ctx)) goto err;\n\t\t\t}\n\t\t}\n\tif (z != NULL)\n\t\t{\n\t\tint Z_is_one;\n\t\tif (!BN_nnmod(&point->Z, z, &group->field, ctx)) goto err;\n\t\tZ_is_one = BN_is_one(&point->Z);\n\t\tif (group->meth->field_encode)\n\t\t\t{\n\t\t\tif (Z_is_one && (group->meth->field_set_to_one != 0))\n\t\t\t\t{\n\t\t\t\tif (!group->meth->field_set_to_one(group, &point->Z, ctx)) goto err;\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\tif (!group->meth->field_encode(group, &point->Z, &point->Z, ctx)) goto err;\n\t\t\t\t}\n\t\t\t}\n\t\tpoint->Z_is_one = Z_is_one;\n\t\t}\n\tret = 1;\n err:\n\tif (new_ctx != NULL)\n\t\tBN_CTX_free(new_ctx);\n\treturn ret;\n\t}', 'int BN_nnmod(BIGNUM *r, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx)\n\t{\n\tif (!(BN_mod(r,m,d,ctx)))\n\t\treturn 0;\n\tif (!r->neg)\n\t\treturn 1;\n\treturn (d->neg ? BN_sub : BN_add)(r, r, d);\n}', 'int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,\n\t BN_CTX *ctx)\n\t{\n\tint norm_shift,i,loop;\n\tBIGNUM *tmp,wnum,*snum,*sdiv,*res;\n\tBN_ULONG *resp,*wnump;\n\tBN_ULONG d0,d1;\n\tint num_n,div_n;\n\tint no_branch=0;\n\tif (num->top > 0 && num->d[num->top - 1] == 0)\n\t\t{\n\t\tBNerr(BN_F_BN_DIV,BN_R_NOT_INITIALIZED);\n\t\treturn 0;\n\t\t}\n\tbn_check_top(num);\n\tif ((BN_get_flags(num, BN_FLG_CONSTTIME) != 0) || (BN_get_flags(divisor, BN_FLG_CONSTTIME) != 0))\n\t\t{\n\t\tno_branch=1;\n\t\t}\n\tbn_check_top(dv);\n\tbn_check_top(rm);\n\tbn_check_top(divisor);\n\tif (BN_is_zero(divisor))\n\t\t{\n\t\tBNerr(BN_F_BN_DIV,BN_R_DIV_BY_ZERO);\n\t\treturn(0);\n\t\t}\n\tif (!no_branch && BN_ucmp(num,divisor) < 0)\n\t\t{\n\t\tif (rm != NULL)\n\t\t\t{ if (BN_copy(rm,num) == NULL) return(0); }\n\t\tif (dv != NULL) BN_zero(dv);\n\t\treturn(1);\n\t\t}\n\tBN_CTX_start(ctx);\n\ttmp=BN_CTX_get(ctx);\n\tsnum=BN_CTX_get(ctx);\n\tsdiv=BN_CTX_get(ctx);\n\tif (dv == NULL)\n\t\tres=BN_CTX_get(ctx);\n\telse\tres=dv;\n\tif (sdiv == NULL || res == NULL || tmp == NULL || snum == NULL)\n\t\tgoto err;\n\tnorm_shift=BN_BITS2-((BN_num_bits(divisor))%BN_BITS2);\n\tif (!(BN_lshift(sdiv,divisor,norm_shift))) goto err;\n\tsdiv->neg=0;\n\tnorm_shift+=BN_BITS2;\n\tif (!(BN_lshift(snum,num,norm_shift))) goto err;\n\tsnum->neg=0;\n\tif (no_branch)\n\t\t{\n\t\tif (snum->top <= sdiv->top+1)\n\t\t\t{\n\t\t\tif (bn_wexpand(snum, sdiv->top + 2) == NULL) goto err;\n\t\t\tfor (i = snum->top; i < sdiv->top + 2; i++) snum->d[i] = 0;\n\t\t\tsnum->top = sdiv->top + 2;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tif (bn_wexpand(snum, snum->top + 1) == NULL) goto err;\n\t\t\tsnum->d[snum->top] = 0;\n\t\t\tsnum->top ++;\n\t\t\t}\n\t\t}\n\tdiv_n=sdiv->top;\n\tnum_n=snum->top;\n\tloop=num_n-div_n;\n\twnum.neg = 0;\n\twnum.d = &(snum->d[loop]);\n\twnum.top = div_n;\n\twnum.dmax = snum->dmax - loop;\n\td0=sdiv->d[div_n-1];\n\td1=(div_n == 1)?0:sdiv->d[div_n-2];\n\twnump= &(snum->d[num_n-1]);\n\tres->neg= (num->neg^divisor->neg);\n\tif (!bn_wexpand(res,(loop+1))) goto err;\n\tres->top=loop-no_branch;\n\tresp= &(res->d[loop-1]);\n\tif (!bn_wexpand(tmp,(div_n+1))) goto err;\n\tif (!no_branch)\n\t\t{\n\t\tif (BN_ucmp(&wnum,sdiv) >= 0)\n\t\t\t{\n\t\t\tbn_clear_top2max(&wnum);\n\t\t\tbn_sub_words(wnum.d, wnum.d, sdiv->d, div_n);\n\t\t\t*resp=1;\n\t\t\t}\n\t\telse\n\t\t\tres->top--;\n\t\t}\n\tif (res->top == 0)\n\t\tres->neg = 0;\n\telse\n\t\tresp--;\n\tfor (i=0; i<loop-1; i++, wnump--, resp--)\n\t\t{\n\t\tBN_ULONG q,l0;\n#if defined(BN_DIV3W) && !defined(OPENSSL_NO_ASM)\n\t\tBN_ULONG bn_div_3_words(BN_ULONG*,BN_ULONG,BN_ULONG);\n\t\tq=bn_div_3_words(wnump,d1,d0);\n#else\n\t\tBN_ULONG n0,n1,rem=0;\n\t\tn0=wnump[0];\n\t\tn1=wnump[-1];\n\t\tif (n0 == d0)\n\t\t\tq=BN_MASK2;\n\t\telse\n\t\t\t{\n#ifdef BN_LLONG\n\t\t\tBN_ULLONG t2;\n#if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)\n\t\t\tq=(BN_ULONG)(((((BN_ULLONG)n0)<<BN_BITS2)|n1)/d0);\n#else\n\t\t\tq=bn_div_words(n0,n1,d0);\n#ifdef BN_DEBUG_LEVITTE\n\t\t\tfprintf(stderr,"DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\\\nX) -> 0x%08X\\n",\n\t\t\t\tn0, n1, d0, q);\n#endif\n#endif\n#ifndef REMAINDER_IS_ALREADY_CALCULATED\n\t\t\trem=(n1-q*d0)&BN_MASK2;\n#endif\n\t\t\tt2=(BN_ULLONG)d1*q;\n\t\t\tfor (;;)\n\t\t\t\t{\n\t\t\t\tif (t2 <= ((((BN_ULLONG)rem)<<BN_BITS2)|wnump[-2]))\n\t\t\t\t\tbreak;\n\t\t\t\tq--;\n\t\t\t\trem += d0;\n\t\t\t\tif (rem < d0) break;\n\t\t\t\tt2 -= d1;\n\t\t\t\t}\n#else\n\t\t\tBN_ULONG t2l,t2h;\n\t\t\tq=bn_div_words(n0,n1,d0);\n#ifdef BN_DEBUG_LEVITTE\n\t\t\tfprintf(stderr,"DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\\\nX) -> 0x%08X\\n",\n\t\t\t\tn0, n1, d0, q);\n#endif\n#ifndef REMAINDER_IS_ALREADY_CALCULATED\n\t\t\trem=(n1-q*d0)&BN_MASK2;\n#endif\n#if defined(BN_UMULT_LOHI)\n\t\t\tBN_UMULT_LOHI(t2l,t2h,d1,q);\n#elif defined(BN_UMULT_HIGH)\n\t\t\tt2l = d1 * q;\n\t\t\tt2h = BN_UMULT_HIGH(d1,q);\n#else\n\t\t\t{\n\t\t\tBN_ULONG ql, qh;\n\t\t\tt2l=LBITS(d1); t2h=HBITS(d1);\n\t\t\tql =LBITS(q); qh =HBITS(q);\n\t\t\tmul64(t2l,t2h,ql,qh);\n\t\t\t}\n#endif\n\t\t\tfor (;;)\n\t\t\t\t{\n\t\t\t\tif ((t2h < rem) ||\n\t\t\t\t\t((t2h == rem) && (t2l <= wnump[-2])))\n\t\t\t\t\tbreak;\n\t\t\t\tq--;\n\t\t\t\trem += d0;\n\t\t\t\tif (rem < d0) break;\n\t\t\t\tif (t2l < d1) t2h--; t2l -= d1;\n\t\t\t\t}\n#endif\n\t\t\t}\n#endif\n\t\tl0=bn_mul_words(tmp->d,sdiv->d,div_n,q);\n\t\ttmp->d[div_n]=l0;\n\t\twnum.d--;\n\t\tif (bn_sub_words(wnum.d, wnum.d, tmp->d, div_n+1))\n\t\t\t{\n\t\t\tq--;\n\t\t\tif (bn_add_words(wnum.d, wnum.d, sdiv->d, div_n))\n\t\t\t\t(*wnump)++;\n\t\t\t}\n\t\t*resp = q;\n\t\t}\n\tbn_correct_top(snum);\n\tif (rm != NULL)\n\t\t{\n\t\tint neg = num->neg;\n\t\tBN_rshift(rm,snum,norm_shift);\n\t\tif (!BN_is_zero(rm))\n\t\t\trm->neg = neg;\n\t\tbn_check_top(rm);\n\t\t}\n\tif (no_branch)\tbn_correct_top(res);\n\tBN_CTX_end(ctx);\n\treturn(1);\nerr:\n\tbn_check_top(rm);\n\tBN_CTX_end(ctx);\n\treturn(0);\n\t}', 'void BN_CTX_start(BN_CTX *ctx)\n\t{\n\tCTXDBG_ENTRY("BN_CTX_start", ctx);\n\tif(ctx->err_stack || ctx->too_many)\n\t\tctx->err_stack++;\n\telse if(!BN_STACK_push(&ctx->stack, ctx->used))\n\t\t{\n\t\tBNerr(BN_F_BN_CTX_START,BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n\t\tctx->err_stack++;\n\t\t}\n\tCTXDBG_EXIT(ctx);\n\t}', 'void BN_CTX_end(BN_CTX *ctx)\n\t{\n\tCTXDBG_ENTRY("BN_CTX_end", ctx);\n\tif(ctx->err_stack)\n\t\tctx->err_stack--;\n\telse\n\t\t{\n\t\tunsigned int fp = BN_STACK_pop(&ctx->stack);\n\t\tif(fp < ctx->used)\n\t\t\tBN_POOL_release(&ctx->pool, ctx->used - fp);\n\t\tctx->used = fp;\n\t\tctx->too_many = 0;\n\t\t}\n\tCTXDBG_EXIT(ctx);\n\t}', 'static unsigned int BN_STACK_pop(BN_STACK *st)\n\t{\n\treturn st->indexes[--(st->depth)];\n\t}']
802
0
https://github.com/libav/libav/blob/645e75992d8876a5e0aa056739885d3afd08d431/libavformat/rtsp.c/#L2114
static int sdp_read_header(AVFormatContext *s, AVFormatParameters *ap) { RTSPState *rt = s->priv_data; RTSPStream *rtsp_st; int size, i, err; char *content; char url[1024]; if (!ff_network_init()) return AVERROR(EIO); content = av_malloc(SDP_MAX_SIZE); size = get_buffer(s->pb, content, SDP_MAX_SIZE - 1); if (size <= 0) { av_free(content); return AVERROR_INVALIDDATA; } content[size] ='\0'; sdp_parse(s, content); av_free(content); for (i = 0; i < rt->nb_rtsp_streams; i++) { char namebuf[50]; rtsp_st = rt->rtsp_streams[i]; getnameinfo((struct sockaddr*) &rtsp_st->sdp_ip, sizeof(rtsp_st->sdp_ip), namebuf, sizeof(namebuf), NULL, 0, NI_NUMERICHOST); ff_url_join(url, sizeof(url), "rtp", NULL, namebuf, rtsp_st->sdp_port, "?localport=%d&ttl=%d", rtsp_st->sdp_port, rtsp_st->sdp_ttl); if (url_open(&rtsp_st->rtp_handle, url, URL_RDWR) < 0) { err = AVERROR_INVALIDDATA; goto fail; } if ((err = rtsp_open_transport_ctx(s, rtsp_st))) goto fail; } return 0; fail: ff_rtsp_close_streams(s); ff_network_close(); return err; }
['static int sdp_read_header(AVFormatContext *s, AVFormatParameters *ap)\n{\n RTSPState *rt = s->priv_data;\n RTSPStream *rtsp_st;\n int size, i, err;\n char *content;\n char url[1024];\n if (!ff_network_init())\n return AVERROR(EIO);\n content = av_malloc(SDP_MAX_SIZE);\n size = get_buffer(s->pb, content, SDP_MAX_SIZE - 1);\n if (size <= 0) {\n av_free(content);\n return AVERROR_INVALIDDATA;\n }\n content[size] =\'\\0\';\n sdp_parse(s, content);\n av_free(content);\n for (i = 0; i < rt->nb_rtsp_streams; i++) {\n char namebuf[50];\n rtsp_st = rt->rtsp_streams[i];\n getnameinfo((struct sockaddr*) &rtsp_st->sdp_ip, sizeof(rtsp_st->sdp_ip),\n namebuf, sizeof(namebuf), NULL, 0, NI_NUMERICHOST);\n ff_url_join(url, sizeof(url), "rtp", NULL,\n namebuf, rtsp_st->sdp_port,\n "?localport=%d&ttl=%d", rtsp_st->sdp_port,\n rtsp_st->sdp_ttl);\n if (url_open(&rtsp_st->rtp_handle, url, URL_RDWR) < 0) {\n err = AVERROR_INVALIDDATA;\n goto fail;\n }\n if ((err = rtsp_open_transport_ctx(s, rtsp_st)))\n goto fail;\n }\n return 0;\nfail:\n ff_rtsp_close_streams(s);\n ff_network_close();\n return err;\n}', 'static inline int ff_network_init(void)\n{\n#if HAVE_WINSOCK2_H\n WSADATA wsaData;\n if (WSAStartup(MAKEWORD(1,1), &wsaData))\n return 0;\n#endif\n return 1;\n}', 'void *av_malloc(unsigned int size)\n{\n void *ptr = NULL;\n#if CONFIG_MEMALIGN_HACK\n long diff;\n#endif\n if(size > (INT_MAX-16) )\n return NULL;\n#if CONFIG_MEMALIGN_HACK\n ptr = malloc(size+16);\n if(!ptr)\n return ptr;\n diff= ((-(long)ptr - 1)&15) + 1;\n ptr = (char*)ptr + diff;\n ((char*)ptr)[-1]= diff;\n#elif HAVE_POSIX_MEMALIGN\n if (posix_memalign(&ptr,16,size))\n ptr = NULL;\n#elif HAVE_MEMALIGN\n ptr = memalign(16,size);\n#else\n ptr = malloc(size);\n#endif\n return ptr;\n}']
803
1
https://github.com/openssl/openssl/blob/b8c32081e02b7008a90d878eccce46da256dfe86/crypto/bn/bn_lib.c/#L295
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->top = b->top; a->neg = b->neg; bn_check_top(a); return a; }
['int BN_is_prime_fasttest_ex(const BIGNUM *a, int checks, BN_CTX *ctx_passed,\n int do_trial_division, BN_GENCB *cb)\n{\n int i, j, ret = -1;\n int k;\n BN_CTX *ctx = NULL;\n BIGNUM *A1, *A1_odd, *check;\n BN_MONT_CTX *mont = NULL;\n if (BN_cmp(a, BN_value_one()) <= 0)\n return 0;\n if (checks == BN_prime_checks)\n checks = BN_prime_checks_for_size(BN_num_bits(a));\n if (!BN_is_odd(a))\n return BN_is_word(a, 2);\n if (do_trial_division) {\n for (i = 1; i < NUMPRIMES; i++) {\n BN_ULONG mod = BN_mod_word(a, primes[i]);\n if (mod == (BN_ULONG)-1)\n goto err;\n if (mod == 0)\n return BN_is_word(a, primes[i]);\n }\n if (!BN_GENCB_call(cb, 1, -1))\n goto err;\n }\n if (ctx_passed != NULL)\n ctx = ctx_passed;\n else if ((ctx = BN_CTX_new()) == NULL)\n goto err;\n BN_CTX_start(ctx);\n A1 = BN_CTX_get(ctx);\n A1_odd = BN_CTX_get(ctx);\n check = BN_CTX_get(ctx);\n if (check == NULL)\n goto err;\n if (!BN_copy(A1, a))\n goto err;\n if (!BN_sub_word(A1, 1))\n goto err;\n if (BN_is_zero(A1)) {\n ret = 0;\n goto err;\n }\n k = 1;\n while (!BN_is_bit_set(A1, k))\n k++;\n if (!BN_rshift(A1_odd, A1, k))\n goto err;\n mont = BN_MONT_CTX_new();\n if (mont == NULL)\n goto err;\n if (!BN_MONT_CTX_set(mont, a, ctx))\n goto err;\n for (i = 0; i < checks; i++) {\n if (!BN_priv_rand_range(check, A1))\n goto err;\n if (!BN_add_word(check, 1))\n goto err;\n j = witness(check, a, A1, A1_odd, k, ctx, mont);\n if (j == -1)\n goto err;\n if (j) {\n ret = 0;\n goto err;\n }\n if (!BN_GENCB_call(cb, 1, i))\n goto err;\n }\n ret = 1;\n err:\n if (ctx != NULL) {\n BN_CTX_end(ctx);\n if (ctx_passed == NULL)\n BN_CTX_free(ctx);\n }\n BN_MONT_CTX_free(mont);\n return ret;\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_is_odd(const BIGNUM *a)\n{\n return (a->top > 0) && (a->d[0] & 1);\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->top = b->top;\n a->neg = b->neg;\n bn_check_top(a);\n return a;\n}', 'int BN_MONT_CTX_set(BN_MONT_CTX *mont, const BIGNUM *mod, BN_CTX *ctx)\n{\n int 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 ret = 1;\n err:\n BN_CTX_end(ctx);\n return ret;\n}', 'int BN_is_zero(const BIGNUM *a)\n{\n return a->top == 0;\n}', 'static int witness(BIGNUM *w, const BIGNUM *a, const BIGNUM *a1,\n const BIGNUM *a1_odd, int k, BN_CTX *ctx,\n BN_MONT_CTX *mont)\n{\n if (!BN_mod_exp_mont(w, w, a1_odd, a, ctx, mont))\n return -1;\n if (BN_is_one(w))\n return 0;\n if (BN_cmp(w, a1) == 0)\n return 0;\n while (--k) {\n if (!BN_mod_mul(w, w, w, a, ctx))\n return -1;\n if (BN_is_one(w))\n return 1;\n if (BN_cmp(w, a1) == 0)\n return 0;\n }\n bn_check_top(w);\n return 1;\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 || BN_get_flags(a, BN_FLG_CONSTTIME) != 0\n || BN_get_flags(m, 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_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 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 (val[0] == 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 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, wmask, window0;\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 = p->top * BN_BITS2;\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 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 (!a->neg) {\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 }\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 powerbufLen += top * sizeof(mont->N.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_nnmod(&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 window0 = (bits - 1) % 5 + 1;\n wmask = (1 << window0) - 1;\n bits -= window0;\n wvalue = bn_get_bits(p, bits) & wmask;\n bn_gather5_t4(tmp.d, top, powerbuf, wvalue);\n while (bits > 0) {\n if (bits < stride)\n stride = bits;\n bits -= stride;\n wvalue = bn_get_bits(p, bits);\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 *n0 = mont->n0, *np;\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 for (np = am.d + top, i = 0; i < top; i++)\n np[i] = mont->N.d[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, np, 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, np, 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, np, 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, np, n0, top, i - 1);\n bn_scatter5(tmp.d, top, powerbuf, i);\n }\n# endif\n window0 = (bits - 1) % 5 + 1;\n wmask = (1 << window0) - 1;\n bits -= window0;\n wvalue = bn_get_bits(p, bits) & wmask;\n bn_gather5(tmp.d, top, powerbuf, wvalue);\n if (top & 7) {\n while (bits > 0) {\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 bn_get_bits5(p->d, bits -= 5));\n }\n } else {\n while (bits > 0) {\n bn_power5(tmp.d, tmp.d, powerbuf, np, n0, top,\n bn_get_bits5(p->d, bits -= 5));\n }\n }\n ret = bn_from_montgomery(tmp.d, tmp.d, NULL, np, 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, window))\n goto err;\n if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&am, top, powerbuf, 1, window))\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(&tmp, top, powerbuf, 2,\n window))\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(&tmp, top, powerbuf, i,\n window))\n goto err;\n }\n }\n window0 = (bits - 1) % window + 1;\n wmask = (1 << window0) - 1;\n bits -= window0;\n wvalue = bn_get_bits(p, bits) & wmask;\n if (!MOD_EXP_CTIME_COPY_FROM_PREBUF(&tmp, top, powerbuf, wvalue,\n window))\n goto err;\n wmask = (1 << window) - 1;\n while (bits > 0) {\n for (i = 0; i < window; i++)\n if (!BN_mod_mul_montgomery(&tmp, &tmp, &tmp, mont, ctx))\n goto err;\n bits -= window;\n wvalue = bn_get_bits(p, bits) & wmask;\n if (!MOD_EXP_CTIME_COPY_FROM_PREBUF(&am, top, powerbuf, wvalue,\n window))\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}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n return (words <= a->dmax) ? a : bn_expand2(a, words);\n}']
804
0
https://github.com/openssl/openssl/blob/8da94770f0a049497b1a52ee469cca1f4a13b1a7/test/bntest.c/#L638
int test_div_recp(BIO *bp, BN_CTX *ctx) { BIGNUM *a, *b, *c, *d, *e; BN_RECP_CTX *recp; int i; recp = BN_RECP_CTX_new(); a = BN_new(); b = BN_new(); c = BN_new(); d = BN_new(); e = BN_new(); for (i = 0; i < num0 + num1; i++) { if (i < num1) { BN_bntest_rand(a, 400, 0, 0); BN_copy(b, a); BN_lshift(a, a, i); BN_add_word(a, i); } else BN_bntest_rand(b, 50 + 3 * (i - num1), 0, 0); a->neg = rand_neg(); b->neg = rand_neg(); BN_RECP_CTX_set(recp, b, ctx); BN_div_recp(d, c, a, recp, ctx); if (bp != NULL) { if (!results) { BN_print(bp, a); BIO_puts(bp, " / "); BN_print(bp, b); BIO_puts(bp, " - "); } BN_print(bp, d); BIO_puts(bp, "\n"); if (!results) { BN_print(bp, a); BIO_puts(bp, " % "); BN_print(bp, b); BIO_puts(bp, " - "); } BN_print(bp, c); BIO_puts(bp, "\n"); } BN_mul(e, d, b, ctx); BN_add(d, e, c); BN_sub(d, d, a); if (!BN_is_zero(d)) { fprintf(stderr, "Reciprocal division test failed!\n"); fprintf(stderr, "a="); BN_print_fp(stderr, a); fprintf(stderr, "\nb="); BN_print_fp(stderr, b); fprintf(stderr, "\n"); return 0; } } BN_free(a); BN_free(b); BN_free(c); BN_free(d); BN_free(e); BN_RECP_CTX_free(recp); return (1); }
['int test_div_recp(BIO *bp, BN_CTX *ctx)\n{\n BIGNUM *a, *b, *c, *d, *e;\n BN_RECP_CTX *recp;\n int i;\n recp = BN_RECP_CTX_new();\n a = BN_new();\n b = BN_new();\n c = BN_new();\n d = BN_new();\n e = BN_new();\n for (i = 0; i < num0 + num1; i++) {\n if (i < num1) {\n BN_bntest_rand(a, 400, 0, 0);\n BN_copy(b, a);\n BN_lshift(a, a, i);\n BN_add_word(a, i);\n } else\n BN_bntest_rand(b, 50 + 3 * (i - num1), 0, 0);\n a->neg = rand_neg();\n b->neg = rand_neg();\n BN_RECP_CTX_set(recp, b, ctx);\n BN_div_recp(d, c, a, recp, ctx);\n if (bp != NULL) {\n if (!results) {\n BN_print(bp, a);\n BIO_puts(bp, " / ");\n BN_print(bp, b);\n BIO_puts(bp, " - ");\n }\n BN_print(bp, d);\n BIO_puts(bp, "\\n");\n if (!results) {\n BN_print(bp, a);\n BIO_puts(bp, " % ");\n BN_print(bp, b);\n BIO_puts(bp, " - ");\n }\n BN_print(bp, c);\n BIO_puts(bp, "\\n");\n }\n BN_mul(e, d, b, ctx);\n BN_add(d, e, c);\n BN_sub(d, d, a);\n if (!BN_is_zero(d)) {\n fprintf(stderr, "Reciprocal division test failed!\\n");\n fprintf(stderr, "a=");\n BN_print_fp(stderr, a);\n fprintf(stderr, "\\nb=");\n BN_print_fp(stderr, b);\n fprintf(stderr, "\\n");\n return 0;\n }\n }\n BN_free(a);\n BN_free(b);\n BN_free(c);\n BN_free(d);\n BN_free(e);\n BN_RECP_CTX_free(recp);\n return (1);\n}', 'BN_RECP_CTX *BN_RECP_CTX_new(void)\n{\n BN_RECP_CTX *ret;\n if ((ret = OPENSSL_malloc(sizeof(*ret))) == NULL)\n return (NULL);\n BN_RECP_CTX_init(ret);\n ret->flags = BN_FLG_MALLOCED;\n return (ret);\n}', 'void *CRYPTO_malloc(size_t num, const char *file, int line)\n{\n void *ret = NULL;\n if (num <= 0)\n return NULL;\n allow_customize = 0;\n#ifdef CRYPTO_MDEBUG\n if (call_malloc_debug) {\n CRYPTO_mem_debug_malloc(NULL, num, 0, file, line);\n ret = malloc(num);\n CRYPTO_mem_debug_malloc(ret, num, 1, file, line);\n } else {\n ret = malloc(num);\n }\n#else\n (void)file;\n (void)line;\n ret = malloc(num);\n#endif\n#ifndef OPENSSL_CPUID_OBJ\n if (ret && (num > 2048)) {\n extern unsigned char cleanse_ctr;\n ((unsigned char *)ret)[0] = cleanse_ctr;\n }\n#endif\n return ret;\n}', 'void BN_RECP_CTX_init(BN_RECP_CTX *recp)\n{\n bn_init(&(recp->N));\n bn_init(&(recp->Nr));\n recp->num_bits = 0;\n recp->flags = 0;\n}', 'BIGNUM *BN_new(void)\n{\n BIGNUM *ret;\n if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL) {\n BNerr(BN_F_BN_NEW, ERR_R_MALLOC_FAILURE);\n return (NULL);\n }\n ret->flags = BN_FLG_MALLOCED;\n bn_check_top(ret);\n return (ret);\n}', 'void *CRYPTO_zalloc(size_t num, const char *file, int line)\n{\n void *ret = CRYPTO_malloc(num, file, line);\n if (ret != NULL)\n memset(ret, 0, num);\n return ret;\n}', 'void BN_free(BIGNUM *a)\n{\n if (a == NULL)\n return;\n bn_check_top(a);\n if (!BN_get_flags(a, BN_FLG_STATIC_DATA))\n bn_free_d(a);\n if (a->flags & BN_FLG_MALLOCED)\n OPENSSL_free(a);\n else {\n#if OPENSSL_API_COMPAT < 0x00908000L\n a->flags |= BN_FLG_FREE;\n#endif\n a->d = NULL;\n }\n}', 'int BN_get_flags(const BIGNUM *b, int n)\n{\n return b->flags & n;\n}']
805
0
https://github.com/openssl/openssl/blob/67dc995eaf538ea309c6292a1a5073465201f55b/crypto/evp/evp_pbe.c/#L182
int EVP_PBE_alg_add_type(int pbe_type, int pbe_nid, int cipher_nid, int md_nid, EVP_PBE_KEYGEN *keygen) { EVP_PBE_CTL *pbe_tmp; if (pbe_algs == NULL) { pbe_algs = sk_EVP_PBE_CTL_new(pbe_cmp); if (pbe_algs == NULL) goto err; } if ((pbe_tmp = OPENSSL_malloc(sizeof(*pbe_tmp))) == NULL) goto err; pbe_tmp->pbe_type = pbe_type; pbe_tmp->pbe_nid = pbe_nid; pbe_tmp->cipher_nid = cipher_nid; pbe_tmp->md_nid = md_nid; pbe_tmp->keygen = keygen; if (!sk_EVP_PBE_CTL_push(pbe_algs, pbe_tmp)) { OPENSSL_free(pbe_tmp); goto err; } return 1; err: EVPerr(EVP_F_EVP_PBE_ALG_ADD_TYPE, ERR_R_MALLOC_FAILURE); return 0; }
['int EVP_PBE_alg_add_type(int pbe_type, int pbe_nid, int cipher_nid,\n int md_nid, EVP_PBE_KEYGEN *keygen)\n{\n EVP_PBE_CTL *pbe_tmp;\n if (pbe_algs == NULL) {\n pbe_algs = sk_EVP_PBE_CTL_new(pbe_cmp);\n if (pbe_algs == NULL)\n goto err;\n }\n if ((pbe_tmp = OPENSSL_malloc(sizeof(*pbe_tmp))) == NULL)\n goto err;\n pbe_tmp->pbe_type = pbe_type;\n pbe_tmp->pbe_nid = pbe_nid;\n pbe_tmp->cipher_nid = cipher_nid;\n pbe_tmp->md_nid = md_nid;\n pbe_tmp->keygen = keygen;\n if (!sk_EVP_PBE_CTL_push(pbe_algs, pbe_tmp)) {\n OPENSSL_free(pbe_tmp);\n goto err;\n }\n return 1;\n err:\n EVPerr(EVP_F_EVP_PBE_ALG_ADD_TYPE, ERR_R_MALLOC_FAILURE);\n return 0;\n}', 'DEFINE_STACK_OF(EVP_PBE_CTL)', 'void *CRYPTO_malloc(size_t num, const char *file, int line)\n{\n void *ret = NULL;\n if (malloc_impl != NULL && malloc_impl != CRYPTO_malloc)\n return malloc_impl(num, file, line);\n if (num == 0)\n return NULL;\n FAILTEST();\n allow_customize = 0;\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n if (call_malloc_debug) {\n CRYPTO_mem_debug_malloc(NULL, num, 0, file, line);\n ret = malloc(num);\n CRYPTO_mem_debug_malloc(ret, num, 1, file, line);\n } else {\n ret = malloc(num);\n }\n#else\n osslargused(file); osslargused(line);\n ret = malloc(num);\n#endif\n return ret;\n}', 'int OPENSSL_sk_push(OPENSSL_STACK *st, const void *data)\n{\n return (OPENSSL_sk_insert(st, data, st->num));\n}']
806
0
https://github.com/openssl/openssl/blob/38f5c30b311f0e736081e0b64b22e917b651536a/ssl/t1_lib.c/#L275
int tls_curve_allowed(SSL *s, const unsigned char *curve, int op) { const tls_curve_info *cinfo; if (curve[0]) return 1; if ((curve[1] < 1) || ((size_t)curve[1] > OSSL_NELEM(nid_list))) return 0; cinfo = &nid_list[curve[1] - 1]; # ifdef OPENSSL_NO_EC2M if (cinfo->flags & TLS_CURVE_CHAR2) return 0; # endif return ssl_security(s, op, cinfo->secbits, cinfo->nid, (void *)curve); }
['int tls_construct_ctos_supported_groups(SSL *s, WPACKET *pkt,\n unsigned int context, X509 *x,\n size_t chainidx, int *al)\n{\n const unsigned char *pcurves = NULL, *pcurvestmp;\n size_t num_curves = 0, i;\n if (!use_ecc(s))\n return 1;\n pcurves = s->ext.supportedgroups;\n if (!tls1_get_curvelist(s, 0, &pcurves, &num_curves)) {\n SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_SUPPORTED_GROUPS,\n ERR_R_INTERNAL_ERROR);\n return 0;\n }\n pcurvestmp = pcurves;\n if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_supported_groups)\n || !WPACKET_start_sub_packet_u16(pkt)\n || !WPACKET_start_sub_packet_u16(pkt)) {\n SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_SUPPORTED_GROUPS,\n ERR_R_INTERNAL_ERROR);\n return 0;\n }\n for (i = 0; i < num_curves; i++, pcurvestmp += 2) {\n if (tls_curve_allowed(s, pcurvestmp, SSL_SECOP_CURVE_SUPPORTED)) {\n if (!WPACKET_put_bytes_u8(pkt, pcurvestmp[0])\n || !WPACKET_put_bytes_u8(pkt, pcurvestmp[1])) {\n SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_SUPPORTED_GROUPS,\n ERR_R_INTERNAL_ERROR);\n return 0;\n }\n }\n }\n if (!WPACKET_close(pkt) || !WPACKET_close(pkt)) {\n SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_SUPPORTED_GROUPS,\n ERR_R_INTERNAL_ERROR);\n return 0;\n }\n return 1;\n}', 'int WPACKET_put_bytes__(WPACKET *pkt, unsigned int val, size_t size)\n{\n unsigned char *data;\n assert(size <= sizeof(unsigned int));\n if (size > sizeof(unsigned int)\n || !WPACKET_allocate_bytes(pkt, size, &data)\n || !put_value(data, val, size))\n return 0;\n return 1;\n}', 'int tls_curve_allowed(SSL *s, const unsigned char *curve, int op)\n{\n const tls_curve_info *cinfo;\n if (curve[0])\n return 1;\n if ((curve[1] < 1) || ((size_t)curve[1] > OSSL_NELEM(nid_list)))\n return 0;\n cinfo = &nid_list[curve[1] - 1];\n# ifdef OPENSSL_NO_EC2M\n if (cinfo->flags & TLS_CURVE_CHAR2)\n return 0;\n# endif\n return ssl_security(s, op, cinfo->secbits, cinfo->nid, (void *)curve);\n}']
807
0
https://github.com/libav/libav/blob/688417399c69aadd4c287bdb0dec82ef8799011c/libavcodec/hevcdsp_template.c/#L907
PUT_HEVC_QPEL_HV(3, 1)
['QPEL(64)', 'PUT_HEVC_QPEL_HV(3, 1)']
808
1
https://github.com/libav/libav/blob/63e8d9760f23a4edf81e9ae58c4f6d3baa6ff4dd/libavcodec/mpegaudiodec.c/#L681
static void apply_window_mp3_c(MPA_INT *synth_buf, MPA_INT *window, int *dither_state, OUT_INT *samples, int incr) { register const MPA_INT *w, *w2, *p; int j; OUT_INT *samples2; #if CONFIG_FLOAT float sum, sum2; #elif FRAC_BITS <= 15 int sum, sum2; #else int64_t sum, sum2; #endif memcpy(synth_buf + 512, synth_buf, 32 * sizeof(*synth_buf)); samples2 = samples + 31 * incr; w = window; w2 = window + 31; sum = *dither_state; p = synth_buf + 16; SUM8(MACS, sum, w, p); p = synth_buf + 48; SUM8(MLSS, sum, w + 32, p); *samples = round_sample(&sum); samples += incr; w++; for(j=1;j<16;j++) { sum2 = 0; p = synth_buf + 16 + j; SUM8P2(sum, MACS, sum2, MLSS, w, w2, p); p = synth_buf + 48 - j; SUM8P2(sum, MLSS, sum2, MLSS, w + 32, w2 + 32, p); *samples = round_sample(&sum); samples += incr; sum += sum2; *samples2 = round_sample(&sum); samples2 -= incr; w++; w2--; } p = synth_buf + 32; SUM8(MLSS, sum, w + 32, p); *samples = round_sample(&sum); *dither_state= sum; }
['static void mpc_synth(MPCContext *c, int16_t *out)\n{\n int dither_state = 0;\n int i, ch;\n OUT_INT samples[MPA_MAX_CHANNELS * MPA_FRAME_SIZE], *samples_ptr;\n for(ch = 0; ch < 2; ch++){\n samples_ptr = samples + ch;\n for(i = 0; i < SAMPLES_PER_BAND; i++) {\n ff_mpa_synth_filter(c->synth_buf[ch], &(c->synth_buf_offset[ch]),\n ff_mpa_synth_window, &dither_state,\n samples_ptr, 2,\n c->sb_samples[ch][i]);\n samples_ptr += 64;\n }\n }\n for(i = 0; i < MPC_FRAME_SIZE*2; i++)\n *out++=samples[i];\n}', 'void ff_mpa_synth_filter(MPA_INT *synth_buf_ptr, int *synth_buf_offset,\n MPA_INT *window, int *dither_state,\n OUT_INT *samples, int incr,\n INTFLOAT sb_samples[SBLIMIT])\n{\n register MPA_INT *synth_buf;\n int offset;\n#if FRAC_BITS <= 15\n int32_t tmp[32];\n int j;\n#endif\n offset = *synth_buf_offset;\n synth_buf = synth_buf_ptr + offset;\n#if FRAC_BITS <= 15\n dct32(tmp, sb_samples);\n for(j=0;j<32;j++) {\n synth_buf[j] = av_clip_int16(tmp[j]);\n }\n#else\n dct32(synth_buf, sb_samples);\n#endif\n apply_window_mp3_c(synth_buf, window, dither_state, samples, incr);\n offset = (offset - 32) & 511;\n *synth_buf_offset = offset;\n}', 'static void apply_window_mp3_c(MPA_INT *synth_buf, MPA_INT *window,\n int *dither_state, OUT_INT *samples, int incr)\n{\n register const MPA_INT *w, *w2, *p;\n int j;\n OUT_INT *samples2;\n#if CONFIG_FLOAT\n float sum, sum2;\n#elif FRAC_BITS <= 15\n int sum, sum2;\n#else\n int64_t sum, sum2;\n#endif\n memcpy(synth_buf + 512, synth_buf, 32 * sizeof(*synth_buf));\n samples2 = samples + 31 * incr;\n w = window;\n w2 = window + 31;\n sum = *dither_state;\n p = synth_buf + 16;\n SUM8(MACS, sum, w, p);\n p = synth_buf + 48;\n SUM8(MLSS, sum, w + 32, p);\n *samples = round_sample(&sum);\n samples += incr;\n w++;\n for(j=1;j<16;j++) {\n sum2 = 0;\n p = synth_buf + 16 + j;\n SUM8P2(sum, MACS, sum2, MLSS, w, w2, p);\n p = synth_buf + 48 - j;\n SUM8P2(sum, MLSS, sum2, MLSS, w + 32, w2 + 32, p);\n *samples = round_sample(&sum);\n samples += incr;\n sum += sum2;\n *samples2 = round_sample(&sum);\n samples2 -= incr;\n w++;\n w2--;\n }\n p = synth_buf + 32;\n SUM8(MLSS, sum, w + 32, p);\n *samples = round_sample(&sum);\n *dither_state= sum;\n}']
809
0
https://github.com/openssl/openssl/blob/e02c519cd32a55e6ad39a0cfbeeda775f9115f28/crypto/bn/bn_lib.c/#L232
static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words) { BN_ULONG *a = NULL; if (words > (INT_MAX / (4 * BN_BITS2))) { BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG); return NULL; } if (BN_get_flags(b, BN_FLG_STATIC_DATA)) { BNerr(BN_F_BN_EXPAND_INTERNAL, 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) { BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE); return NULL; } assert(b->top <= words); if (b->top > 0) memcpy(a, b->d, sizeof(*a) * b->top); return a; }
['int bn_mul_mont_fixed_top(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 int num = mont->N.top;\n#if defined(OPENSSL_BN_ASM_MONT) && defined(MONT_WORD)\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 r->flags |= BN_FLG_FIXED_TOP;\n return 1;\n }\n }\n#endif\n if ((a->top + b->top) > 2 * num)\n return 0;\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_fixed_top(tmp, a, ctx))\n goto err;\n } else {\n if (!bn_mul_fixed_top(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 ret = 1;\n err:\n BN_CTX_end(ctx);\n return ret;\n}', 'int bn_sqr_fixed_top(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 == NULL || tmp == NULL)\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 rr->top = max;\n rr->flags |= BN_FLG_FIXED_TOP;\n if (r != rr && BN_copy(r, rr) == NULL)\n goto err;\n ret = 1;\n err:\n bn_check_top(rr);\n bn_check_top(tmp);\n BN_CTX_end(ctx);\n return ret;\n}', 'static int bn_from_montgomery_word(BIGNUM *ret, BIGNUM *r, BN_MONT_CTX *mont)\n{\n BIGNUM *n;\n BN_ULONG *ap, *np, *rp, n0, v, carry;\n int nl, max, i;\n unsigned int rtop;\n n = &(mont->N);\n nl = n->top;\n if (nl == 0) {\n ret->top = 0;\n return 1;\n }\n max = (2 * nl);\n if (bn_wexpand(r, max) == NULL)\n return 0;\n r->neg ^= n->neg;\n np = n->d;\n rp = r->d;\n for (rtop = r->top, i = 0; i < max; i++) {\n v = (BN_ULONG)0 - ((i - rtop) >> (8 * sizeof(rtop) - 1));\n rp[i] &= v;\n }\n r->top = max;\n r->flags |= BN_FLG_FIXED_TOP;\n n0 = mont->n0[0];\n for (carry = 0, i = 0; i < nl; i++, rp++) {\n v = bn_mul_add_words(rp, np, nl, (rp[0] * n0) & BN_MASK2);\n v = (v + carry + rp[nl]) & BN_MASK2;\n carry |= (v != rp[nl]);\n carry &= (v <= rp[nl]);\n rp[nl] = v;\n }\n if (bn_wexpand(ret, nl) == NULL)\n return 0;\n ret->top = nl;\n ret->flags |= BN_FLG_FIXED_TOP;\n ret->neg = r->neg;\n rp = ret->d;\n ap = &(r->d[nl]);\n carry -= bn_sub_words(rp, ap, np, nl);\n for (i = 0; i < nl; i++) {\n rp[i] = (carry & ap[i]) | (~carry & rp[i]);\n ap[i] = 0;\n }\n return 1;\n}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n return (words <= a->dmax) ? a : bn_expand2(a, words);\n}', 'BIGNUM *bn_expand2(BIGNUM *b, int words)\n{\n if (words > b->dmax) {\n BN_ULONG *a = bn_expand_internal(b, words);\n if (!a)\n return NULL;\n if (b->d) {\n OPENSSL_cleanse(b->d, b->dmax * sizeof(b->d[0]));\n bn_free_d(b);\n }\n b->d = a;\n b->dmax = words;\n }\n return b;\n}', 'static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)\n{\n BN_ULONG *a = NULL;\n if (words > (INT_MAX / (4 * BN_BITS2))) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);\n return NULL;\n }\n if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);\n return NULL;\n }\n if (BN_get_flags(b, BN_FLG_SECURE))\n a = OPENSSL_secure_zalloc(words * sizeof(*a));\n else\n a = OPENSSL_zalloc(words * sizeof(*a));\n if (a == NULL) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);\n return NULL;\n }\n assert(b->top <= words);\n if (b->top > 0)\n memcpy(a, b->d, sizeof(*a) * b->top);\n return a;\n}']
810
0
https://github.com/libav/libav/blob/a5ba798c16d0614d982a76755fdd72b37d437170/libavformat/movenc.c/#L1084
static int mov_write_ctts_tag(AVIOContext *pb, MOVTrack *track) { MOVStts *ctts_entries; uint32_t entries = 0; uint32_t atom_size; int i; ctts_entries = av_malloc((track->entry + 1) * sizeof(*ctts_entries)); ctts_entries[0].count = 1; ctts_entries[0].duration = track->cluster[0].cts; for (i=1; i<track->entry; i++) { if (track->cluster[i].cts == ctts_entries[entries].duration) { ctts_entries[entries].count++; } else { entries++; ctts_entries[entries].duration = track->cluster[i].cts; ctts_entries[entries].count = 1; } } entries++; atom_size = 16 + (entries * 8); avio_wb32(pb, atom_size); ffio_wfourcc(pb, "ctts"); avio_wb32(pb, 0); avio_wb32(pb, entries); for (i=0; i<entries; i++) { avio_wb32(pb, ctts_entries[i].count); avio_wb32(pb, ctts_entries[i].duration); } av_free(ctts_entries); return atom_size; }
['static int mov_write_ctts_tag(AVIOContext *pb, MOVTrack *track)\n{\n MOVStts *ctts_entries;\n uint32_t entries = 0;\n uint32_t atom_size;\n int i;\n ctts_entries = av_malloc((track->entry + 1) * sizeof(*ctts_entries));\n ctts_entries[0].count = 1;\n ctts_entries[0].duration = track->cluster[0].cts;\n for (i=1; i<track->entry; i++) {\n if (track->cluster[i].cts == ctts_entries[entries].duration) {\n ctts_entries[entries].count++;\n } else {\n entries++;\n ctts_entries[entries].duration = track->cluster[i].cts;\n ctts_entries[entries].count = 1;\n }\n }\n entries++;\n atom_size = 16 + (entries * 8);\n avio_wb32(pb, atom_size);\n ffio_wfourcc(pb, "ctts");\n avio_wb32(pb, 0);\n avio_wb32(pb, entries);\n for (i=0; i<entries; i++) {\n avio_wb32(pb, ctts_entries[i].count);\n avio_wb32(pb, ctts_entries[i].duration);\n }\n av_free(ctts_entries);\n return atom_size;\n}', 'void *av_malloc(size_t size)\n{\n void *ptr = NULL;\n#if CONFIG_MEMALIGN_HACK\n long diff;\n#endif\n if (size > (INT_MAX - 32) || !size)\n return NULL;\n#if CONFIG_MEMALIGN_HACK\n ptr = malloc(size + 32);\n if (!ptr)\n return ptr;\n diff = ((-(long)ptr - 1) & 31) + 1;\n ptr = (char *)ptr + diff;\n ((char *)ptr)[-1] = diff;\n#elif HAVE_POSIX_MEMALIGN\n if (posix_memalign(&ptr, 32, size))\n ptr = NULL;\n#elif HAVE_ALIGNED_MALLOC\n ptr = _aligned_malloc(size, 32);\n#elif HAVE_MEMALIGN\n ptr = memalign(32, size);\n#else\n ptr = malloc(size);\n#endif\n return ptr;\n}']
811
0
https://github.com/openssl/openssl/blob/05ec6a25f80ac8edfb7d7cb764d2dd68156a6965/crypto/bn/bn_sqr.c/#L119
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); }
['static void prime_field_tests(void)\n{\n BN_CTX *ctx = NULL;\n BIGNUM *p, *a, *b;\n EC_GROUP *group;\n EC_GROUP *P_160 = NULL, *P_192 = NULL, *P_224 = NULL, *P_256 =\n NULL, *P_384 = NULL, *P_521 = NULL;\n EC_POINT *P, *Q, *R;\n BIGNUM *x, *y, *z, *yplusone;\n unsigned char buf[100];\n size_t i, len;\n int k;\n ctx = BN_CTX_new();\n if (!ctx)\n ABORT;\n p = BN_new();\n a = BN_new();\n b = BN_new();\n if (!p || !a || !b)\n ABORT;\n if (!BN_hex2bn(&p, "17"))\n ABORT;\n if (!BN_hex2bn(&a, "1"))\n ABORT;\n if (!BN_hex2bn(&b, "1"))\n ABORT;\n group = EC_GROUP_new(EC_GFp_mont_method());\n if (!group)\n ABORT;\n if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx))\n ABORT;\n {\n EC_GROUP *tmp;\n tmp = EC_GROUP_new(EC_GROUP_method_of(group));\n if (!tmp)\n ABORT;\n if (!EC_GROUP_copy(tmp, group))\n ABORT;\n EC_GROUP_free(group);\n group = tmp;\n }\n if (!EC_GROUP_get_curve_GFp(group, p, a, b, ctx))\n ABORT;\n fprintf(stdout,\n "Curve defined by Weierstrass equation\\n y^2 = x^3 + a*x + b (mod 0x");\n BN_print_fp(stdout, p);\n fprintf(stdout, ")\\n a = 0x");\n BN_print_fp(stdout, a);\n fprintf(stdout, "\\n b = 0x");\n BN_print_fp(stdout, b);\n fprintf(stdout, "\\n");\n P = EC_POINT_new(group);\n Q = EC_POINT_new(group);\n R = EC_POINT_new(group);\n if (!P || !Q || !R)\n ABORT;\n if (!EC_POINT_set_to_infinity(group, P))\n ABORT;\n if (!EC_POINT_is_at_infinity(group, P))\n ABORT;\n buf[0] = 0;\n if (!EC_POINT_oct2point(group, Q, buf, 1, ctx))\n ABORT;\n if (!EC_POINT_add(group, P, P, Q, ctx))\n ABORT;\n if (!EC_POINT_is_at_infinity(group, P))\n ABORT;\n x = BN_new();\n y = BN_new();\n z = BN_new();\n yplusone = BN_new();\n if (x == NULL || y == NULL || z == NULL || yplusone == NULL)\n ABORT;\n if (!BN_hex2bn(&x, "D"))\n ABORT;\n if (!EC_POINT_set_compressed_coordinates_GFp(group, Q, x, 1, ctx))\n ABORT;\n if (EC_POINT_is_on_curve(group, Q, ctx) <= 0) {\n if (!EC_POINT_get_affine_coordinates_GFp(group, Q, x, y, ctx))\n ABORT;\n fprintf(stderr, "Point is not on curve: x = 0x");\n BN_print_fp(stderr, x);\n fprintf(stderr, ", y = 0x");\n BN_print_fp(stderr, y);\n fprintf(stderr, "\\n");\n ABORT;\n }\n fprintf(stdout, "A cyclic subgroup:\\n");\n k = 100;\n do {\n if (k-- == 0)\n ABORT;\n if (EC_POINT_is_at_infinity(group, P))\n fprintf(stdout, " point at infinity\\n");\n else {\n if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx))\n ABORT;\n fprintf(stdout, " x = 0x");\n BN_print_fp(stdout, x);\n fprintf(stdout, ", y = 0x");\n BN_print_fp(stdout, y);\n fprintf(stdout, "\\n");\n }\n if (!EC_POINT_copy(R, P))\n ABORT;\n if (!EC_POINT_add(group, P, P, Q, ctx))\n ABORT;\n }\n while (!EC_POINT_is_at_infinity(group, P));\n if (!EC_POINT_add(group, P, Q, R, ctx))\n ABORT;\n if (!EC_POINT_is_at_infinity(group, P))\n ABORT;\n len =\n EC_POINT_point2oct(group, Q, POINT_CONVERSION_COMPRESSED, buf,\n sizeof buf, ctx);\n if (len == 0)\n ABORT;\n if (!EC_POINT_oct2point(group, P, buf, len, ctx))\n ABORT;\n if (0 != EC_POINT_cmp(group, P, Q, ctx))\n ABORT;\n fprintf(stdout, "Generator as octet string, compressed form:\\n ");\n for (i = 0; i < len; i++)\n fprintf(stdout, "%02X", buf[i]);\n len =\n EC_POINT_point2oct(group, Q, POINT_CONVERSION_UNCOMPRESSED, buf,\n sizeof buf, ctx);\n if (len == 0)\n ABORT;\n if (!EC_POINT_oct2point(group, P, buf, len, ctx))\n ABORT;\n if (0 != EC_POINT_cmp(group, P, Q, ctx))\n ABORT;\n fprintf(stdout, "\\nGenerator as octet string, uncompressed form:\\n ");\n for (i = 0; i < len; i++)\n fprintf(stdout, "%02X", buf[i]);\n len =\n EC_POINT_point2oct(group, Q, POINT_CONVERSION_HYBRID, buf, sizeof buf,\n ctx);\n if (len == 0)\n ABORT;\n if (!EC_POINT_oct2point(group, P, buf, len, ctx))\n ABORT;\n if (0 != EC_POINT_cmp(group, P, Q, ctx))\n ABORT;\n fprintf(stdout, "\\nGenerator as octet string, hybrid form:\\n ");\n for (i = 0; i < len; i++)\n fprintf(stdout, "%02X", buf[i]);\n if (!EC_POINT_get_Jprojective_coordinates_GFp(group, R, x, y, z, ctx))\n ABORT;\n fprintf(stdout,\n "\\nA representation of the inverse of that generator in\\nJacobian projective coordinates:\\n X = 0x");\n BN_print_fp(stdout, x);\n fprintf(stdout, ", Y = 0x");\n BN_print_fp(stdout, y);\n fprintf(stdout, ", Z = 0x");\n BN_print_fp(stdout, z);\n fprintf(stdout, "\\n");\n if (!EC_POINT_invert(group, P, ctx))\n ABORT;\n if (0 != EC_POINT_cmp(group, P, R, ctx))\n ABORT;\n if (!BN_hex2bn(&p, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF7FFFFFFF"))\n ABORT;\n if (1 != BN_is_prime_ex(p, BN_prime_checks, ctx, NULL))\n ABORT;\n if (!BN_hex2bn(&a, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF7FFFFFFC"))\n ABORT;\n if (!BN_hex2bn(&b, "1C97BEFC54BD7A8B65ACF89F81D4D4ADC565FA45"))\n ABORT;\n if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx))\n ABORT;\n if (!BN_hex2bn(&x, "4A96B5688EF573284664698968C38BB913CBFC82"))\n ABORT;\n if (!BN_hex2bn(&y, "23a628553168947d59dcc912042351377ac5fb32"))\n ABORT;\n if (!BN_add(yplusone, y, BN_value_one()))\n ABORT;\n if (EC_POINT_set_affine_coordinates_GFp(group, P, x, yplusone, ctx))\n ABORT;\n if (!EC_POINT_set_affine_coordinates_GFp(group, P, x, y, ctx))\n ABORT;\n if (EC_POINT_is_on_curve(group, P, ctx) <= 0)\n ABORT;\n if (!BN_hex2bn(&z, "0100000000000000000001F4C8F927AED3CA752257"))\n ABORT;\n if (!EC_GROUP_set_generator(group, P, z, BN_value_one()))\n ABORT;\n if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx))\n ABORT;\n fprintf(stdout, "\\nSEC2 curve secp160r1 -- Generator:\\n x = 0x");\n BN_print_fp(stdout, x);\n fprintf(stdout, "\\n y = 0x");\n BN_print_fp(stdout, y);\n fprintf(stdout, "\\n");\n if (!BN_hex2bn(&z, "23a628553168947d59dcc912042351377ac5fb32"))\n ABORT;\n if (0 != BN_cmp(y, z))\n ABORT;\n fprintf(stdout, "verify degree ...");\n if (EC_GROUP_get_degree(group) != 160)\n ABORT;\n fprintf(stdout, " ok\\n");\n group_order_tests(group);\n if ((P_160 = EC_GROUP_new(EC_GROUP_method_of(group))) == NULL)\n ABORT;\n if (!EC_GROUP_copy(P_160, group))\n ABORT;\n if (!BN_hex2bn(&p, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFF"))\n ABORT;\n if (1 != BN_is_prime_ex(p, BN_prime_checks, ctx, NULL))\n ABORT;\n if (!BN_hex2bn(&a, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFC"))\n ABORT;\n if (!BN_hex2bn(&b, "64210519E59C80E70FA7E9AB72243049FEB8DEECC146B9B1"))\n ABORT;\n if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx))\n ABORT;\n if (!BN_hex2bn(&x, "188DA80EB03090F67CBF20EB43A18800F4FF0AFD82FF1012"))\n ABORT;\n if (!EC_POINT_set_compressed_coordinates_GFp(group, P, x, 1, ctx))\n ABORT;\n if (EC_POINT_is_on_curve(group, P, ctx) <= 0)\n ABORT;\n if (!BN_hex2bn(&z, "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22831"))\n ABORT;\n if (!EC_GROUP_set_generator(group, P, z, BN_value_one()))\n ABORT;\n if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx))\n ABORT;\n fprintf(stdout, "\\nNIST curve P-192 -- Generator:\\n x = 0x");\n BN_print_fp(stdout, x);\n fprintf(stdout, "\\n y = 0x");\n BN_print_fp(stdout, y);\n fprintf(stdout, "\\n");\n if (!BN_hex2bn(&z, "07192B95FFC8DA78631011ED6B24CDD573F977A11E794811"))\n ABORT;\n if (0 != BN_cmp(y, z))\n ABORT;\n if (!BN_add(yplusone, y, BN_value_one()))\n ABORT;\n if (EC_POINT_set_affine_coordinates_GFp(group, P, x, yplusone, ctx))\n ABORT;\n fprintf(stdout, "verify degree ...");\n if (EC_GROUP_get_degree(group) != 192)\n ABORT;\n fprintf(stdout, " ok\\n");\n group_order_tests(group);\n if ((P_192 = EC_GROUP_new(EC_GROUP_method_of(group))) == NULL)\n ABORT;\n if (!EC_GROUP_copy(P_192, group))\n ABORT;\n if (!BN_hex2bn\n (&p, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF000000000000000000000001"))\n ABORT;\n if (1 != BN_is_prime_ex(p, BN_prime_checks, ctx, NULL))\n ABORT;\n if (!BN_hex2bn\n (&a, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFE"))\n ABORT;\n if (!BN_hex2bn\n (&b, "B4050A850C04B3ABF54132565044B0B7D7BFD8BA270B39432355FFB4"))\n ABORT;\n if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx))\n ABORT;\n if (!BN_hex2bn\n (&x, "B70E0CBD6BB4BF7F321390B94A03C1D356C21122343280D6115C1D21"))\n ABORT;\n if (!EC_POINT_set_compressed_coordinates_GFp(group, P, x, 0, ctx))\n ABORT;\n if (EC_POINT_is_on_curve(group, P, ctx) <= 0)\n ABORT;\n if (!BN_hex2bn\n (&z, "FFFFFFFFFFFFFFFFFFFFFFFFFFFF16A2E0B8F03E13DD29455C5C2A3D"))\n ABORT;\n if (!EC_GROUP_set_generator(group, P, z, BN_value_one()))\n ABORT;\n if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx))\n ABORT;\n fprintf(stdout, "\\nNIST curve P-224 -- Generator:\\n x = 0x");\n BN_print_fp(stdout, x);\n fprintf(stdout, "\\n y = 0x");\n BN_print_fp(stdout, y);\n fprintf(stdout, "\\n");\n if (!BN_hex2bn\n (&z, "BD376388B5F723FB4C22DFE6CD4375A05A07476444D5819985007E34"))\n ABORT;\n if (0 != BN_cmp(y, z))\n ABORT;\n if (!BN_add(yplusone, y, BN_value_one()))\n ABORT;\n if (EC_POINT_set_affine_coordinates_GFp(group, P, x, yplusone, ctx))\n ABORT;\n fprintf(stdout, "verify degree ...");\n if (EC_GROUP_get_degree(group) != 224)\n ABORT;\n fprintf(stdout, " ok\\n");\n group_order_tests(group);\n if ((P_224 = EC_GROUP_new(EC_GROUP_method_of(group))) == NULL)\n ABORT;\n if (!EC_GROUP_copy(P_224, group))\n ABORT;\n if (!BN_hex2bn\n (&p,\n "FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF"))\n ABORT;\n if (1 != BN_is_prime_ex(p, BN_prime_checks, ctx, NULL))\n ABORT;\n if (!BN_hex2bn\n (&a,\n "FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFC"))\n ABORT;\n if (!BN_hex2bn\n (&b,\n "5AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B"))\n ABORT;\n if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx))\n ABORT;\n if (!BN_hex2bn\n (&x,\n "6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296"))\n ABORT;\n if (!EC_POINT_set_compressed_coordinates_GFp(group, P, x, 1, ctx))\n ABORT;\n if (EC_POINT_is_on_curve(group, P, ctx) <= 0)\n ABORT;\n if (!BN_hex2bn(&z, "FFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E"\n "84F3B9CAC2FC632551"))\n ABORT;\n if (!EC_GROUP_set_generator(group, P, z, BN_value_one()))\n ABORT;\n if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx))\n ABORT;\n fprintf(stdout, "\\nNIST curve P-256 -- Generator:\\n x = 0x");\n BN_print_fp(stdout, x);\n fprintf(stdout, "\\n y = 0x");\n BN_print_fp(stdout, y);\n fprintf(stdout, "\\n");\n if (!BN_hex2bn\n (&z,\n "4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5"))\n ABORT;\n if (0 != BN_cmp(y, z))\n ABORT;\n if (!BN_add(yplusone, y, BN_value_one()))\n ABORT;\n if (EC_POINT_set_affine_coordinates_GFp(group, P, x, yplusone, ctx))\n ABORT;\n fprintf(stdout, "verify degree ...");\n if (EC_GROUP_get_degree(group) != 256)\n ABORT;\n fprintf(stdout, " ok\\n");\n group_order_tests(group);\n if ((P_256 = EC_GROUP_new(EC_GROUP_method_of(group))) == NULL)\n ABORT;\n if (!EC_GROUP_copy(P_256, group))\n ABORT;\n if (!BN_hex2bn(&p, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"\n "FFFFFFFFFFFFFFFFFEFFFFFFFF0000000000000000FFFFFFFF"))\n ABORT;\n if (1 != BN_is_prime_ex(p, BN_prime_checks, ctx, NULL))\n ABORT;\n if (!BN_hex2bn(&a, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"\n "FFFFFFFFFFFFFFFFFEFFFFFFFF0000000000000000FFFFFFFC"))\n ABORT;\n if (!BN_hex2bn(&b, "B3312FA7E23EE7E4988E056BE3F82D19181D9C6EFE8141"\n "120314088F5013875AC656398D8A2ED19D2A85C8EDD3EC2AEF"))\n ABORT;\n if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx))\n ABORT;\n if (!BN_hex2bn(&x, "AA87CA22BE8B05378EB1C71EF320AD746E1D3B628BA79B"\n "9859F741E082542A385502F25DBF55296C3A545E3872760AB7"))\n ABORT;\n if (!EC_POINT_set_compressed_coordinates_GFp(group, P, x, 1, ctx))\n ABORT;\n if (EC_POINT_is_on_curve(group, P, ctx) <= 0)\n ABORT;\n if (!BN_hex2bn(&z, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"\n "FFC7634D81F4372DDF581A0DB248B0A77AECEC196ACCC52973"))\n ABORT;\n if (!EC_GROUP_set_generator(group, P, z, BN_value_one()))\n ABORT;\n if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx))\n ABORT;\n fprintf(stdout, "\\nNIST curve P-384 -- Generator:\\n x = 0x");\n BN_print_fp(stdout, x);\n fprintf(stdout, "\\n y = 0x");\n BN_print_fp(stdout, y);\n fprintf(stdout, "\\n");\n if (!BN_hex2bn(&z, "3617DE4A96262C6F5D9E98BF9292DC29F8F41DBD289A14"\n "7CE9DA3113B5F0B8C00A60B1CE1D7E819D7A431D7C90EA0E5F"))\n ABORT;\n if (0 != BN_cmp(y, z))\n ABORT;\n if (!BN_add(yplusone, y, BN_value_one()))\n ABORT;\n if (EC_POINT_set_affine_coordinates_GFp(group, P, x, yplusone, ctx))\n ABORT;\n fprintf(stdout, "verify degree ...");\n if (EC_GROUP_get_degree(group) != 384)\n ABORT;\n fprintf(stdout, " ok\\n");\n group_order_tests(group);\n if ((P_384 = EC_GROUP_new(EC_GROUP_method_of(group))) == NULL)\n ABORT;\n if (!EC_GROUP_copy(P_384, group))\n ABORT;\n if (!BN_hex2bn(&p, "1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"\n "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"\n "FFFFFFFFFFFFFFFFFFFFFFFFFFFF"))\n ABORT;\n if (1 != BN_is_prime_ex(p, BN_prime_checks, ctx, NULL))\n ABORT;\n if (!BN_hex2bn(&a, "1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"\n "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"\n "FFFFFFFFFFFFFFFFFFFFFFFFFFFC"))\n ABORT;\n if (!BN_hex2bn(&b, "051953EB9618E1C9A1F929A21A0B68540EEA2DA725B99B"\n "315F3B8B489918EF109E156193951EC7E937B1652C0BD3BB1BF073573"\n "DF883D2C34F1EF451FD46B503F00"))\n ABORT;\n if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx))\n ABORT;\n if (!BN_hex2bn(&x, "C6858E06B70404E9CD9E3ECB662395B4429C648139053F"\n "B521F828AF606B4D3DBAA14B5E77EFE75928FE1DC127A2FFA8DE3348B"\n "3C1856A429BF97E7E31C2E5BD66"))\n ABORT;\n if (!EC_POINT_set_compressed_coordinates_GFp(group, P, x, 0, ctx))\n ABORT;\n if (EC_POINT_is_on_curve(group, P, ctx) <= 0)\n ABORT;\n if (!BN_hex2bn(&z, "1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"\n "FFFFFFFFFFFFFFFFFFFFA51868783BF2F966B7FCC0148F709A5D03BB5"\n "C9B8899C47AEBB6FB71E91386409"))\n ABORT;\n if (!EC_GROUP_set_generator(group, P, z, BN_value_one()))\n ABORT;\n if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx))\n ABORT;\n fprintf(stdout, "\\nNIST curve P-521 -- Generator:\\n x = 0x");\n BN_print_fp(stdout, x);\n fprintf(stdout, "\\n y = 0x");\n BN_print_fp(stdout, y);\n fprintf(stdout, "\\n");\n if (!BN_hex2bn(&z, "11839296A789A3BC0045C8A5FB42C7D1BD998F54449579"\n "B446817AFBD17273E662C97EE72995EF42640C550B9013FAD0761353C"\n "7086A272C24088BE94769FD16650"))\n ABORT;\n if (0 != BN_cmp(y, z))\n ABORT;\n if (!BN_add(yplusone, y, BN_value_one()))\n ABORT;\n if (EC_POINT_set_affine_coordinates_GFp(group, P, x, yplusone, ctx))\n ABORT;\n fprintf(stdout, "verify degree ...");\n if (EC_GROUP_get_degree(group) != 521)\n ABORT;\n fprintf(stdout, " ok\\n");\n group_order_tests(group);\n if ((P_521 = EC_GROUP_new(EC_GROUP_method_of(group))) == NULL)\n ABORT;\n if (!EC_GROUP_copy(P_521, group))\n ABORT;\n if (!EC_POINT_set_affine_coordinates_GFp(group, P, x, y, ctx))\n ABORT;\n if (!EC_POINT_copy(Q, P))\n ABORT;\n if (EC_POINT_is_at_infinity(group, Q))\n ABORT;\n if (!EC_POINT_dbl(group, P, P, ctx))\n ABORT;\n if (EC_POINT_is_on_curve(group, P, ctx) <= 0)\n ABORT;\n if (!EC_POINT_invert(group, Q, ctx))\n ABORT;\n if (!EC_POINT_add(group, R, P, Q, ctx))\n ABORT;\n if (!EC_POINT_add(group, R, R, Q, ctx))\n ABORT;\n if (!EC_POINT_is_at_infinity(group, R))\n ABORT;\n {\n const EC_POINT *points[4];\n const BIGNUM *scalars[4];\n BIGNUM *scalar3;\n if (EC_POINT_is_at_infinity(group, Q))\n ABORT;\n points[0] = Q;\n points[1] = Q;\n points[2] = Q;\n points[3] = Q;\n if (!EC_GROUP_get_order(group, z, ctx))\n ABORT;\n if (!BN_add(y, z, BN_value_one()))\n ABORT;\n if (BN_is_odd(y))\n ABORT;\n if (!BN_rshift1(y, y))\n ABORT;\n scalars[0] = y;\n scalars[1] = y;\n fprintf(stdout, "combined multiplication ...");\n fflush(stdout);\n if (!EC_POINTs_mul(group, P, NULL, 2, points, scalars, ctx))\n ABORT;\n if (!EC_POINTs_mul(group, R, z, 2, points, scalars, ctx))\n ABORT;\n if (0 != EC_POINT_cmp(group, P, R, ctx))\n ABORT;\n if (0 != EC_POINT_cmp(group, R, Q, ctx))\n ABORT;\n fprintf(stdout, ".");\n fflush(stdout);\n if (!BN_pseudo_rand(y, BN_num_bits(y), 0, 0))\n ABORT;\n if (!BN_add(z, z, y))\n ABORT;\n BN_set_negative(z, 1);\n scalars[0] = y;\n scalars[1] = z;\n if (!EC_POINTs_mul(group, P, NULL, 2, points, scalars, ctx))\n ABORT;\n if (!EC_POINT_is_at_infinity(group, P))\n ABORT;\n fprintf(stdout, ".");\n fflush(stdout);\n if (!BN_pseudo_rand(x, BN_num_bits(y) - 1, 0, 0))\n ABORT;\n if (!BN_add(z, x, y))\n ABORT;\n BN_set_negative(z, 1);\n scalars[0] = x;\n scalars[1] = y;\n scalars[2] = z;\n scalar3 = BN_new();\n if (!scalar3)\n ABORT;\n BN_zero(scalar3);\n scalars[3] = scalar3;\n if (!EC_POINTs_mul(group, P, NULL, 4, points, scalars, ctx))\n ABORT;\n if (!EC_POINT_is_at_infinity(group, P))\n ABORT;\n fprintf(stdout, " ok\\n\\n");\n BN_free(scalar3);\n }\n BN_CTX_free(ctx);\n BN_free(p);\n BN_free(a);\n BN_free(b);\n EC_GROUP_free(group);\n EC_POINT_free(P);\n EC_POINT_free(Q);\n EC_POINT_free(R);\n BN_free(x);\n BN_free(y);\n BN_free(z);\n BN_free(yplusone);\n EC_GROUP_free(P_160);\n EC_GROUP_free(P_192);\n EC_GROUP_free(P_224);\n EC_GROUP_free(P_256);\n EC_GROUP_free(P_384);\n EC_GROUP_free(P_521);\n}', "int BN_hex2bn(BIGNUM **bn, const char *a)\n{\n BIGNUM *ret = NULL;\n BN_ULONG l = 0;\n int neg = 0, h, m, i, j, k, c;\n int num;\n if ((a == NULL) || (*a == '\\0'))\n return (0);\n if (*a == '-') {\n neg = 1;\n a++;\n }\n for (i = 0; i <= (INT_MAX/4) && isxdigit((unsigned char)a[i]); i++)\n continue;\n if (i == 0 || i > INT_MAX/4)\n goto err;\n num = i + neg;\n if (bn == NULL)\n return (num);\n if (*bn == NULL) {\n if ((ret = BN_new()) == NULL)\n return (0);\n } else {\n ret = *bn;\n BN_zero(ret);\n }\n if (bn_expand(ret, i * 4) == NULL)\n goto err;\n j = i;\n m = 0;\n h = 0;\n while (j > 0) {\n m = ((BN_BYTES * 2) <= j) ? (BN_BYTES * 2) : j;\n l = 0;\n for (;;) {\n c = a[j - m];\n k = OPENSSL_hexchar2int(c);\n if (k < 0)\n k = 0;\n l = (l << 4) | k;\n if (--m <= 0) {\n ret->d[h++] = l;\n break;\n }\n }\n j -= (BN_BYTES * 2);\n }\n ret->top = h;\n bn_correct_top(ret);\n ret->neg = neg;\n *bn = ret;\n bn_check_top(ret);\n return (num);\n err:\n if (*bn == NULL)\n BN_free(ret);\n return (0);\n}", 'int EC_POINT_set_compressed_coordinates_GFp(const EC_GROUP *group,\n EC_POINT *point, const BIGNUM *x,\n int y_bit, BN_CTX *ctx)\n{\n if (group->meth->point_set_compressed_coordinates == 0\n && !(group->meth->flags & EC_FLAGS_DEFAULT_OCT)) {\n ECerr(EC_F_EC_POINT_SET_COMPRESSED_COORDINATES_GFP,\n ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);\n return 0;\n }\n if (group->meth != point->meth) {\n ECerr(EC_F_EC_POINT_SET_COMPRESSED_COORDINATES_GFP,\n EC_R_INCOMPATIBLE_OBJECTS);\n return 0;\n }\n if (group->meth->flags & EC_FLAGS_DEFAULT_OCT) {\n if (group->meth->field_type == NID_X9_62_prime_field)\n return ec_GFp_simple_set_compressed_coordinates(group, point, x,\n y_bit, ctx);\n else\n#ifdef OPENSSL_NO_EC2M\n {\n ECerr(EC_F_EC_POINT_SET_COMPRESSED_COORDINATES_GFP,\n EC_R_GF2M_NOT_SUPPORTED);\n return 0;\n }\n#else\n return ec_GF2m_simple_set_compressed_coordinates(group, point, x,\n y_bit, ctx);\n#endif\n }\n return group->meth->point_set_compressed_coordinates(group, point, x,\n y_bit, ctx);\n}', 'int ec_GFp_simple_set_compressed_coordinates(const EC_GROUP *group,\n EC_POINT *point,\n const BIGNUM *x_, int y_bit,\n BN_CTX *ctx)\n{\n BN_CTX *new_ctx = NULL;\n BIGNUM *tmp1, *tmp2, *x, *y;\n int ret = 0;\n ERR_clear_error();\n if (ctx == NULL) {\n ctx = new_ctx = BN_CTX_new();\n if (ctx == NULL)\n return 0;\n }\n y_bit = (y_bit != 0);\n BN_CTX_start(ctx);\n tmp1 = BN_CTX_get(ctx);\n tmp2 = BN_CTX_get(ctx);\n x = BN_CTX_get(ctx);\n y = BN_CTX_get(ctx);\n if (y == NULL)\n goto err;\n if (!BN_nnmod(x, x_, group->field, ctx))\n goto err;\n if (group->meth->field_decode == 0) {\n if (!group->meth->field_sqr(group, tmp2, x_, ctx))\n goto err;\n if (!group->meth->field_mul(group, tmp1, tmp2, x_, ctx))\n goto err;\n } else {\n if (!BN_mod_sqr(tmp2, x_, group->field, ctx))\n goto err;\n if (!BN_mod_mul(tmp1, tmp2, x_, group->field, ctx))\n goto err;\n }\n if (group->a_is_minus3) {\n if (!BN_mod_lshift1_quick(tmp2, x, group->field))\n goto err;\n if (!BN_mod_add_quick(tmp2, tmp2, x, group->field))\n goto err;\n if (!BN_mod_sub_quick(tmp1, tmp1, tmp2, group->field))\n goto err;\n } else {\n if (group->meth->field_decode) {\n if (!group->meth->field_decode(group, tmp2, group->a, ctx))\n goto err;\n if (!BN_mod_mul(tmp2, tmp2, x, group->field, ctx))\n goto err;\n } else {\n if (!group->meth->field_mul(group, tmp2, group->a, x, ctx))\n goto err;\n }\n if (!BN_mod_add_quick(tmp1, tmp1, tmp2, group->field))\n goto err;\n }\n if (group->meth->field_decode) {\n if (!group->meth->field_decode(group, tmp2, group->b, ctx))\n goto err;\n if (!BN_mod_add_quick(tmp1, tmp1, tmp2, group->field))\n goto err;\n } else {\n if (!BN_mod_add_quick(tmp1, tmp1, group->b, group->field))\n goto err;\n }\n if (!BN_mod_sqrt(y, tmp1, group->field, ctx)) {\n unsigned long err = ERR_peek_last_error();\n if (ERR_GET_LIB(err) == ERR_LIB_BN\n && ERR_GET_REASON(err) == BN_R_NOT_A_SQUARE) {\n ERR_clear_error();\n ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,\n EC_R_INVALID_COMPRESSED_POINT);\n } else\n ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,\n ERR_R_BN_LIB);\n goto err;\n }\n if (y_bit != BN_is_odd(y)) {\n if (BN_is_zero(y)) {\n int kron;\n kron = BN_kronecker(x, group->field, ctx);\n if (kron == -2)\n goto err;\n if (kron == 1)\n ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,\n EC_R_INVALID_COMPRESSION_BIT);\n else\n ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,\n EC_R_INVALID_COMPRESSED_POINT);\n goto err;\n }\n if (!BN_usub(y, group->field, y))\n goto err;\n }\n if (y_bit != BN_is_odd(y)) {\n ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,\n ERR_R_INTERNAL_ERROR);\n goto err;\n }\n if (!EC_POINT_set_affine_coordinates_GFp(group, point, x, y, ctx))\n goto err;\n ret = 1;\n err:\n BN_CTX_end(ctx);\n BN_CTX_free(new_ctx);\n return ret;\n}', 'BIGNUM *BN_CTX_get(BN_CTX *ctx)\n{\n BIGNUM *ret;\n CTXDBG_ENTRY("BN_CTX_get", ctx);\n if (ctx->err_stack || ctx->too_many)\n return NULL;\n if ((ret = BN_POOL_get(&ctx->pool, ctx->flags)) == NULL) {\n ctx->too_many = 1;\n BNerr(BN_F_BN_CTX_GET, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n return NULL;\n }\n BN_zero(ret);\n ctx->used++;\n CTXDBG_RET(ctx, ret);\n return ret;\n}', 'int BN_set_word(BIGNUM *a, BN_ULONG w)\n{\n bn_check_top(a);\n if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)\n return (0);\n a->neg = 0;\n a->d[0] = w;\n a->top = (w ? 1 : 0);\n bn_check_top(a);\n return (1);\n}', 'static ossl_inline BIGNUM *bn_expand(BIGNUM *a, int bits)\n{\n if (bits > (INT_MAX - BN_BITS2 + 1))\n return NULL;\n if(((bits+BN_BITS2-1)/BN_BITS2) <= (a)->dmax)\n return a;\n return bn_expand2((a),(bits+BN_BITS2-1)/BN_BITS2);\n}', 'int BN_mod_sqr(BIGNUM *r, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx)\n{\n if (!BN_sqr(r, a, ctx))\n return 0;\n return BN_mod(r, r, m, ctx);\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}']
812
0
https://github.com/openssl/openssl/blob/0f3e6045898e9aa5d0249e61c874b1f153ae54fa/crypto/lhash/lhash.c/#L243
char *lh_delete(LHASH *lh, char *data) { unsigned long hash; LHASH_NODE *nn,**rn; char *ret; lh->error=0; rn=getrn(lh,data,&hash); if (*rn == NULL) { lh->num_no_delete++; return(NULL); } else { nn= *rn; *rn=nn->next; ret=nn->data; Free((char *)nn); lh->num_delete++; } lh->num_items--; if ((lh->num_nodes > MIN_NODES) && (lh->down_load >= (lh->num_items*LH_LOAD_MULT/lh->num_nodes))) contract(lh); return(ret); }
['int main(int argc, char *argv[])\n\t{\n\tchar *CApath=NULL,*CAfile=NULL;\n\tint badop=0;\n\tint tls1=0,ssl2=0,ssl3=0,ret=1;\n\tint client_auth=0;\n\tint server_auth=0,i;\n\tchar *server_cert=TEST_SERVER_CERT;\n\tchar *client_cert=TEST_CLIENT_CERT;\n\tSSL_CTX *s_ctx=NULL;\n\tSSL_CTX *c_ctx=NULL;\n\tSSL_METHOD *meth=NULL;\n\tSSL *c_ssl,*s_ssl;\n\tint number=1,reuse=0;\n\tlong bytes=1L;\n\tSSL_CIPHER *ciph;\n#ifndef NO_DH\n\tDH *dh;\n#endif\n\tbio_err=BIO_new_fp(stderr,BIO_NOCLOSE);\n\tbio_stdout=BIO_new_fp(stdout,BIO_NOCLOSE);\n\tCRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);\n\targc--;\n\targv++;\n\twhile (argc >= 1)\n\t\t{\n\t\tif\t(strcmp(*argv,"-server_auth") == 0)\n\t\t\tserver_auth=1;\n\t\telse if\t(strcmp(*argv,"-client_auth") == 0)\n\t\t\tclient_auth=1;\n\t\telse if\t(strcmp(*argv,"-v") == 0)\n\t\t\tverbose=1;\n\t\telse if\t(strcmp(*argv,"-d") == 0)\n\t\t\tdebug=1;\n\t\telse if\t(strcmp(*argv,"-reuse") == 0)\n\t\t\treuse=1;\n\t\telse if\t(strcmp(*argv,"-ssl2") == 0)\n\t\t\tssl2=1;\n\t\telse if\t(strcmp(*argv,"-tls1") == 0)\n\t\t\ttls1=1;\n\t\telse if\t(strcmp(*argv,"-ssl3") == 0)\n\t\t\tssl3=1;\n\t\telse if\t(strncmp(*argv,"-num",4) == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\tnumber= atoi(*(++argv));\n\t\t\tif (number == 0) number=1;\n\t\t\t}\n\t\telse if\t(strcmp(*argv,"-bytes") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\tbytes= atol(*(++argv));\n\t\t\tif (bytes == 0L) bytes=1L;\n\t\t\ti=strlen(argv[0]);\n\t\t\tif (argv[0][i-1] == \'k\') bytes*=1024L;\n\t\t\tif (argv[0][i-1] == \'m\') bytes*=1024L*1024L;\n\t\t\t}\n\t\telse if\t(strcmp(*argv,"-cert") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\tserver_cert= *(++argv);\n\t\t\t}\n\t\telse if\t(strcmp(*argv,"-s_cert") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\tserver_cert= *(++argv);\n\t\t\t}\n\t\telse if\t(strcmp(*argv,"-c_cert") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\tclient_cert= *(++argv);\n\t\t\t}\n\t\telse if\t(strcmp(*argv,"-cipher") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\tcipher= *(++argv);\n\t\t\t}\n\t\telse if\t(strcmp(*argv,"-CApath") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\tCApath= *(++argv);\n\t\t\t}\n\t\telse if\t(strcmp(*argv,"-CAfile") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\tCAfile= *(++argv);\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tfprintf(stderr,"unknown option %s\\n",*argv);\n\t\t\tbadop=1;\n\t\t\tbreak;\n\t\t\t}\n\t\targc--;\n\t\targv++;\n\t\t}\n\tif (badop)\n\t\t{\nbad:\n\t\tsv_usage();\n\t\tgoto end;\n\t\t}\n\tSSL_library_init();\n\tSSL_load_error_strings();\n#if !defined(NO_SSL2) && !defined(NO_SSL3)\n\tif (ssl2)\n\t\tmeth=SSLv2_method();\n\telse\n\tif (tls1)\n\t\tmeth=TLSv1_method();\n\telse\n\tif (ssl3)\n\t\tmeth=SSLv3_method();\n\telse\n\t\tmeth=SSLv23_method();\n#else\n#ifdef NO_SSL2\n\tmeth=SSLv3_method();\n#else\n\tmeth=SSLv2_method();\n#endif\n#endif\n\tc_ctx=SSL_CTX_new(meth);\n\ts_ctx=SSL_CTX_new(meth);\n\tif ((c_ctx == NULL) || (s_ctx == NULL))\n\t\t{\n\t\tERR_print_errors(bio_err);\n\t\tgoto end;\n\t\t}\n\tif (cipher != NULL)\n\t\t{\n\t\tSSL_CTX_set_cipher_list(c_ctx,cipher);\n\t\tSSL_CTX_set_cipher_list(s_ctx,cipher);\n\t\t}\n#ifndef NO_DH\n\tdh=get_dh512();\n\tSSL_CTX_set_tmp_dh(s_ctx,dh);\n\tDH_free(dh);\n#endif\n#ifndef NO_RSA\n\tSSL_CTX_set_tmp_rsa_callback(s_ctx,tmp_rsa_cb);\n#endif\n\tif (!SSL_CTX_use_certificate_file(s_ctx,server_cert,SSL_FILETYPE_PEM))\n\t\t{\n\t\tERR_print_errors(bio_err);\n\t\t}\n\telse if (!SSL_CTX_use_PrivateKey_file(s_ctx,server_cert,\n\t\tSSL_FILETYPE_PEM))\n\t\t{\n\t\tERR_print_errors(bio_err);\n\t\tgoto end;\n\t\t}\n\tif (client_auth)\n\t\t{\n\t\tSSL_CTX_use_certificate_file(c_ctx,client_cert,\n\t\t\tSSL_FILETYPE_PEM);\n\t\tSSL_CTX_use_PrivateKey_file(c_ctx,client_cert,\n\t\t\tSSL_FILETYPE_PEM);\n\t\t}\n\tif (\t(!SSL_CTX_load_verify_locations(s_ctx,CAfile,CApath)) ||\n\t\t(!SSL_CTX_set_default_verify_paths(s_ctx)) ||\n\t\t(!SSL_CTX_load_verify_locations(c_ctx,CAfile,CApath)) ||\n\t\t(!SSL_CTX_set_default_verify_paths(c_ctx)))\n\t\t{\n\t\tERR_print_errors(bio_err);\n\t\t}\n\tif (client_auth)\n\t\t{\n\t\tfprintf(stderr,"client authentication\\n");\n\t\tSSL_CTX_set_verify(s_ctx,\n\t\t\tSSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT,\n\t\t\tverify_callback);\n\t\t}\n\tif (server_auth)\n\t\t{\n\t\tfprintf(stderr,"server authentication\\n");\n\t\tSSL_CTX_set_verify(c_ctx,SSL_VERIFY_PEER,\n\t\t\tverify_callback);\n\t\t}\n\tc_ssl=SSL_new(c_ctx);\n\ts_ssl=SSL_new(s_ctx);\n\tfor (i=0; i<number; i++)\n\t\t{\n\t\tif (!reuse) SSL_set_session(c_ssl,NULL);\n\t\tret=doit(s_ssl,c_ssl,bytes);\n\t\t}\n\tif (!verbose)\n\t\t{\n\t\tciph=SSL_get_current_cipher(c_ssl);\n\t\tfprintf(stdout,"Protocol %s, cipher %s, %s\\n",\n\t\t\tSSL_get_version(c_ssl),\n\t\t\tSSL_CIPHER_get_version(ciph),\n\t\t\tSSL_CIPHER_get_name(ciph));\n\t\t}\n\tif ((number > 1) || (bytes > 1L))\n\t\tprintf("%d handshakes of %ld bytes done\\n",number,bytes);\n\tSSL_free(s_ssl);\n\tSSL_free(c_ssl);\nend:\n\tif (s_ctx != NULL) SSL_CTX_free(s_ctx);\n\tif (c_ctx != NULL) SSL_CTX_free(c_ctx);\n\tif (bio_stdout != NULL) BIO_free(bio_stdout);\n\tERR_free_strings();\n\tERR_remove_state(0);\n\tEVP_cleanup();\n\tCRYPTO_mem_leaks(bio_err);\n\tEXIT(ret);\n\t}', 'SSL_CTX *SSL_CTX_new(SSL_METHOD *meth)\n\t{\n\tSSL_CTX *ret=NULL;\n\tif (meth == NULL)\n\t\t{\n\t\tSSLerr(SSL_F_SSL_CTX_NEW,SSL_R_NULL_SSL_METHOD_PASSED);\n\t\treturn(NULL);\n\t\t}\n\tif (SSL_get_ex_data_X509_STORE_CTX_idx() < 0)\n\t\t{\n\t\tSSLerr(SSL_F_SSL_CTX_NEW,SSL_R_X509_VERIFICATION_SETUP_PROBLEMS);\n\t\tgoto err;\n\t\t}\n\tret=(SSL_CTX *)Malloc(sizeof(SSL_CTX));\n\tif (ret == NULL)\n\t\tgoto err;\n\tmemset(ret,0,sizeof(SSL_CTX));\n\tret->method=meth;\n\tret->cert_store=NULL;\n\tret->session_cache_mode=SSL_SESS_CACHE_SERVER;\n\tret->session_cache_size=SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;\n\tret->session_cache_head=NULL;\n\tret->session_cache_tail=NULL;\n\tret->session_timeout=meth->get_timeout();\n\tret->new_session_cb=NULL;\n\tret->remove_session_cb=NULL;\n\tret->get_session_cb=NULL;\n\tmemset((char *)&ret->stats,0,sizeof(ret->stats));\n\tret->references=1;\n\tret->quiet_shutdown=0;\n\tret->info_callback=NULL;\n\tret->app_verify_callback=NULL;\n\tret->app_verify_arg=NULL;\n\tret->read_ahead=0;\n\tret->verify_mode=SSL_VERIFY_NONE;\n\tret->verify_depth=-1;\n\tret->default_verify_callback=NULL;\n\tif ((ret->default_cert=ssl_cert_new()) == NULL)\n\t\tgoto err;\n\tret->default_passwd_callback=NULL;\n\tret->client_cert_cb=NULL;\n\tret->sessions=lh_new(SSL_SESSION_hash,SSL_SESSION_cmp);\n\tif (ret->sessions == NULL) goto err;\n\tret->cert_store=X509_STORE_new();\n\tif (ret->cert_store == NULL) goto err;\n\tssl_create_cipher_list(ret->method,\n\t\t&ret->cipher_list,&ret->cipher_list_by_id,\n\t\tSSL_DEFAULT_CIPHER_LIST);\n\tif (ret->cipher_list == NULL\n\t || sk_SSL_CIPHER_num(ret->cipher_list) <= 0)\n\t\t{\n\t\tSSLerr(SSL_F_SSL_CTX_NEW,SSL_R_LIBRARY_HAS_NO_CIPHERS);\n\t\tgoto err2;\n\t\t}\n\tif ((ret->rsa_md5=EVP_get_digestbyname("ssl2-md5")) == NULL)\n\t\t{\n\t\tSSLerr(SSL_F_SSL_CTX_NEW,SSL_R_UNABLE_TO_LOAD_SSL2_MD5_ROUTINES);\n\t\tgoto err2;\n\t\t}\n\tif ((ret->md5=EVP_get_digestbyname("ssl3-md5")) == NULL)\n\t\t{\n\t\tSSLerr(SSL_F_SSL_CTX_NEW,SSL_R_UNABLE_TO_LOAD_SSL3_MD5_ROUTINES);\n\t\tgoto err2;\n\t\t}\n\tif ((ret->sha1=EVP_get_digestbyname("ssl3-sha1")) == NULL)\n\t\t{\n\t\tSSLerr(SSL_F_SSL_CTX_NEW,SSL_R_UNABLE_TO_LOAD_SSL3_SHA1_ROUTINES);\n\t\tgoto err2;\n\t\t}\n\tif ((ret->client_CA=sk_X509_NAME_new_null()) == NULL)\n\t\tgoto err;\n\tCRYPTO_new_ex_data(ssl_ctx_meth,(char *)ret,&ret->ex_data);\n\tret->extra_certs=NULL;\n\tret->comp_methods=SSL_COMP_get_compression_methods();\n\treturn(ret);\nerr:\n\tSSLerr(SSL_F_SSL_CTX_NEW,ERR_R_MALLOC_FAILURE);\nerr2:\n\tif (ret != NULL) SSL_CTX_free(ret);\n\treturn(NULL);\n\t}', 'LHASH *lh_new(unsigned long (*h)(), int (*c)())\n\t{\n\tLHASH *ret;\n\tint i;\n\tif ((ret=(LHASH *)Malloc(sizeof(LHASH))) == NULL)\n\t\tgoto err0;\n\tif ((ret->b=(LHASH_NODE **)Malloc(sizeof(LHASH_NODE *)*MIN_NODES)) == NULL)\n\t\tgoto err1;\n\tfor (i=0; i<MIN_NODES; i++)\n\t\tret->b[i]=NULL;\n\tret->comp=((c == NULL)?(int (*)())strcmp:c);\n\tret->hash=((h == NULL)?(unsigned long (*)())lh_strhash:h);\n\tret->num_nodes=MIN_NODES/2;\n\tret->num_alloc_nodes=MIN_NODES;\n\tret->p=0;\n\tret->pmax=MIN_NODES/2;\n\tret->up_load=UP_LOAD;\n\tret->down_load=DOWN_LOAD;\n\tret->num_items=0;\n\tret->num_expands=0;\n\tret->num_expand_reallocs=0;\n\tret->num_contracts=0;\n\tret->num_contract_reallocs=0;\n\tret->num_hash_calls=0;\n\tret->num_comp_calls=0;\n\tret->num_insert=0;\n\tret->num_replace=0;\n\tret->num_delete=0;\n\tret->num_no_delete=0;\n\tret->num_retrieve=0;\n\tret->num_retrieve_miss=0;\n\tret->num_hash_comps=0;\n\tret->error=0;\n\treturn(ret);\nerr1:\n\tFree((char *)ret);\nerr0:\n\treturn(NULL);\n\t}', 'SSL *SSL_new(SSL_CTX *ctx)\n\t{\n\tSSL *s;\n\tif (ctx == NULL)\n\t\t{\n\t\tSSLerr(SSL_F_SSL_NEW,SSL_R_NULL_SSL_CTX);\n\t\treturn(NULL);\n\t\t}\n\tif (ctx->method == NULL)\n\t\t{\n\t\tSSLerr(SSL_F_SSL_NEW,SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);\n\t\treturn(NULL);\n\t\t}\n\ts=(SSL *)Malloc(sizeof(SSL));\n\tif (s == NULL) goto err;\n\tmemset(s,0,sizeof(SSL));\n\tif (ctx->default_cert != NULL)\n\t\t{\n\t\tCRYPTO_add(&ctx->default_cert->references,1,\n\t\t\t CRYPTO_LOCK_SSL_CERT);\n\t\ts->cert=ctx->default_cert;\n\t\t}\n\telse\n\t\ts->cert=NULL;\n\ts->sid_ctx_length=ctx->sid_ctx_length;\n\tmemcpy(&s->sid_ctx,&ctx->sid_ctx,sizeof(s->sid_ctx));\n\ts->verify_mode=ctx->verify_mode;\n\ts->verify_depth=ctx->verify_depth;\n\ts->verify_callback=ctx->default_verify_callback;\n\tCRYPTO_add(&ctx->references,1,CRYPTO_LOCK_SSL_CTX);\n\ts->ctx=ctx;\n\ts->verify_result=X509_V_OK;\n\ts->method=ctx->method;\n\tif (!s->method->ssl_new(s))\n\t\t{\n\t\tSSL_CTX_free(ctx);\n\t\tFree(s);\n\t\tgoto err;\n\t\t}\n\ts->quiet_shutdown=ctx->quiet_shutdown;\n\ts->references=1;\n\ts->server=(ctx->method->ssl_accept == ssl_undefined_function)?0:1;\n\ts->options=ctx->options;\n\tSSL_clear(s);\n\tCRYPTO_new_ex_data(ssl_meth,(char *)s,&s->ex_data);\n\treturn(s);\nerr:\n\tSSLerr(SSL_F_SSL_NEW,ERR_R_MALLOC_FAILURE);\n\treturn(NULL);\n\t}', 'int SSL_clear(SSL *s)\n\t{\n\tint state;\n\tif (s->method == NULL)\n\t\t{\n\t\tSSLerr(SSL_F_SSL_CLEAR,SSL_R_NO_METHOD_SPECIFIED);\n\t\treturn(0);\n\t\t}\n\ts->error=0;\n\ts->hit=0;\n\ts->shutdown=0;\n#if 0\n\tif (s->new_session) return(1);\n#endif\n\tstate=s->state;\n\ts->type=0;\n\ts->state=SSL_ST_BEFORE|((s->server)?SSL_ST_ACCEPT:SSL_ST_CONNECT);\n\ts->version=s->method->version;\n\ts->client_version=s->version;\n\ts->rwstate=SSL_NOTHING;\n\ts->rstate=SSL_ST_READ_HEADER;\n\ts->read_ahead=s->ctx->read_ahead;\n\tif (s->init_buf != NULL)\n\t\t{\n\t\tBUF_MEM_free(s->init_buf);\n\t\ts->init_buf=NULL;\n\t\t}\n\tssl_clear_cipher_ctx(s);\n\tif (ssl_clear_bad_session(s))\n\t\t{\n\t\tSSL_SESSION_free(s->session);\n\t\ts->session=NULL;\n\t\t}\n\ts->first_packet=0;\n#if 1\n\tif ((s->session == NULL) && (s->method != s->ctx->method))\n\t\t{\n\t\ts->method->ssl_free(s);\n\t\ts->method=s->ctx->method;\n\t\tif (!s->method->ssl_new(s))\n\t\t\treturn(0);\n\t\t}\n\telse\n#endif\n\t\ts->method->ssl_clear(s);\n\treturn(1);\n\t}', 'int ssl_clear_bad_session(SSL *s)\n\t{\n\tif (\t(s->session != NULL) &&\n\t\t!(s->shutdown & SSL_SENT_SHUTDOWN) &&\n\t\t!(SSL_in_init(s) || SSL_in_before(s)))\n\t\t{\n\t\tSSL_CTX_remove_session(s->ctx,s->session);\n\t\treturn(1);\n\t\t}\n\telse\n\t\treturn(0);\n\t}', 'int SSL_CTX_remove_session(SSL_CTX *ctx, SSL_SESSION *c)\n{\n\treturn remove_session_lock(ctx, c, 1);\n}', 'static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lck)\n\t{\n\tSSL_SESSION *r;\n\tint ret=0;\n\tif ((c != NULL) && (c->session_id_length != 0))\n\t\t{\n\t\tif(lck) CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);\n\t\tr=(SSL_SESSION *)lh_delete(ctx->sessions,(char *)c);\n\t\tif (r != NULL)\n\t\t\t{\n\t\t\tret=1;\n\t\t\tSSL_SESSION_list_remove(ctx,c);\n\t\t\t}\n\t\tif(lck) CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);\n\t\tif (ret)\n\t\t\t{\n\t\t\tr->not_resumable=1;\n\t\t\tif (ctx->remove_session_cb != NULL)\n\t\t\t\tctx->remove_session_cb(ctx,r);\n\t\t\tSSL_SESSION_free(r);\n\t\t\t}\n\t\t}\n\telse\n\t\tret=0;\n\treturn(ret);\n\t}', 'char *lh_delete(LHASH *lh, char *data)\n\t{\n\tunsigned long hash;\n\tLHASH_NODE *nn,**rn;\n\tchar *ret;\n\tlh->error=0;\n\trn=getrn(lh,data,&hash);\n\tif (*rn == NULL)\n\t\t{\n\t\tlh->num_no_delete++;\n\t\treturn(NULL);\n\t\t}\n\telse\n\t\t{\n\t\tnn= *rn;\n\t\t*rn=nn->next;\n\t\tret=nn->data;\n\t\tFree((char *)nn);\n\t\tlh->num_delete++;\n\t\t}\n\tlh->num_items--;\n\tif ((lh->num_nodes > MIN_NODES) &&\n\t\t(lh->down_load >= (lh->num_items*LH_LOAD_MULT/lh->num_nodes)))\n\t\tcontract(lh);\n\treturn(ret);\n\t}']
813
0
https://github.com/libav/libav/blob/645e75992d8876a5e0aa056739885d3afd08d431/libavformat/rtsp.c/#L580
static int rtsp_open_transport_ctx(AVFormatContext *s, RTSPStream *rtsp_st) { RTSPState *rt = s->priv_data; AVStream *st = NULL; if (rtsp_st->stream_index >= 0) st = s->streams[rtsp_st->stream_index]; if (!st) s->ctx_flags |= AVFMTCTX_NOHEADER; if (s->oformat) { rtsp_st->transport_priv = rtsp_rtp_mux_open(s, st, rtsp_st->rtp_handle); rtsp_st->rtp_handle = NULL; } else if (rt->transport == RTSP_TRANSPORT_RDT) rtsp_st->transport_priv = ff_rdt_parse_open(s, st->index, rtsp_st->dynamic_protocol_context, rtsp_st->dynamic_handler); else rtsp_st->transport_priv = rtp_parse_open(s, st, rtsp_st->rtp_handle, rtsp_st->sdp_payload_type, (rt->lower_transport == RTSP_LOWER_TRANSPORT_TCP || !s->max_delay) ? 0 : RTP_REORDER_QUEUE_DEFAULT_SIZE); if (!rtsp_st->transport_priv) { return AVERROR(ENOMEM); } else if (rt->transport != RTSP_TRANSPORT_RDT) { if (rtsp_st->dynamic_handler) { rtp_parse_set_dynamic_protocol(rtsp_st->transport_priv, rtsp_st->dynamic_protocol_context, rtsp_st->dynamic_handler); } } return 0; }
['static int rtsp_open_transport_ctx(AVFormatContext *s, RTSPStream *rtsp_st)\n{\n RTSPState *rt = s->priv_data;\n AVStream *st = NULL;\n if (rtsp_st->stream_index >= 0)\n st = s->streams[rtsp_st->stream_index];\n if (!st)\n s->ctx_flags |= AVFMTCTX_NOHEADER;\n if (s->oformat) {\n rtsp_st->transport_priv = rtsp_rtp_mux_open(s, st, rtsp_st->rtp_handle);\n rtsp_st->rtp_handle = NULL;\n } else if (rt->transport == RTSP_TRANSPORT_RDT)\n rtsp_st->transport_priv = ff_rdt_parse_open(s, st->index,\n rtsp_st->dynamic_protocol_context,\n rtsp_st->dynamic_handler);\n else\n rtsp_st->transport_priv = rtp_parse_open(s, st, rtsp_st->rtp_handle,\n rtsp_st->sdp_payload_type,\n (rt->lower_transport == RTSP_LOWER_TRANSPORT_TCP || !s->max_delay)\n ? 0 : RTP_REORDER_QUEUE_DEFAULT_SIZE);\n if (!rtsp_st->transport_priv) {\n return AVERROR(ENOMEM);\n } else if (rt->transport != RTSP_TRANSPORT_RDT) {\n if (rtsp_st->dynamic_handler) {\n rtp_parse_set_dynamic_protocol(rtsp_st->transport_priv,\n rtsp_st->dynamic_protocol_context,\n rtsp_st->dynamic_handler);\n }\n }\n return 0;\n}']
814
0
https://github.com/openssl/openssl/blob/95dc05bc6d0dfe0f3f3681f5e27afbc3f7a35eea/apps/testdsa.h/#L41
DSA *get_dsa512() { DSA *dsa; if ((dsa=DSA_new()) == NULL) return(NULL); dsa->p=BN_bin2bn(dsa512_p,sizeof(dsa512_p),NULL); dsa->q=BN_bin2bn(dsa512_q,sizeof(dsa512_q),NULL); dsa->g=BN_bin2bn(dsa512_g,sizeof(dsa512_g),NULL); if ((dsa->p == NULL) || (dsa->q == NULL) || (dsa->g == NULL)) return(NULL); return(dsa); }
['DSA *get_dsa512()\n\t{\n\tDSA *dsa;\n\tif ((dsa=DSA_new()) == NULL) return(NULL);\n\tdsa->p=BN_bin2bn(dsa512_p,sizeof(dsa512_p),NULL);\n\tdsa->q=BN_bin2bn(dsa512_q,sizeof(dsa512_q),NULL);\n\tdsa->g=BN_bin2bn(dsa512_g,sizeof(dsa512_g),NULL);\n\tif ((dsa->p == NULL) || (dsa->q == NULL) || (dsa->g == NULL))\n\t\treturn(NULL);\n\treturn(dsa);\n\t}', 'DSA *DSA_new(void)\n\t{\n\tDSA *ret;\n\tret=(DSA *)Malloc(sizeof(DSA));\n\tif (ret == NULL)\n\t\t{\n\t\tDSAerr(DSA_F_DSA_NEW,ERR_R_MALLOC_FAILURE);\n\t\treturn(NULL);\n\t\t}\n\tret->pad=0;\n\tret->version=0;\n\tret->write_params=1;\n\tret->p=NULL;\n\tret->q=NULL;\n\tret->g=NULL;\n\tret->flags=DSA_FLAG_CACHE_MONT_P;\n\tret->pub_key=NULL;\n\tret->priv_key=NULL;\n\tret->kinv=NULL;\n\tret->r=NULL;\n\tret->method_mont_p=NULL;\n\tret->references=1;\n\treturn(ret);\n\t}']
815
0
https://github.com/libav/libav/blob/e5b0fc170f85b00f7dd0ac514918fb5c95253d39/libavcodec/bitstream.h/#L139
static inline uint64_t get_val(BitstreamContext *bc, unsigned n) { #ifdef BITSTREAM_READER_LE uint64_t ret = bc->bits & ((UINT64_C(1) << n) - 1); bc->bits >>= n; #else uint64_t ret = bc->bits >> (64 - n); bc->bits <<= n; #endif bc->bits_left -= n; return ret; }
['static int decode_channel_sf_idx(BitstreamContext *bc, Atrac3pChanUnitCtx *ctx,\n int ch_num, AVCodecContext *avctx)\n{\n int i, weight_idx = 0, delta, diff, num_long_vals,\n delta_bits, min_val, vlc_sel, start_val;\n VLC *vlc_tab;\n Atrac3pChanParams *chan = &ctx->channels[ch_num];\n Atrac3pChanParams *ref_chan = &ctx->channels[0];\n switch (bitstream_read(bc, 2)) {\n case 0:\n for (i = 0; i < ctx->used_quant_units; i++)\n chan->qu_sf_idx[i] = bitstream_read(bc, 6);\n break;\n case 1:\n if (ch_num) {\n vlc_tab = &sf_vlc_tabs[bitstream_read(bc, 2)];\n for (i = 0; i < ctx->used_quant_units; i++) {\n delta = bitstream_read_vlc(bc, vlc_tab->table, vlc_tab->bits, 1);\n chan->qu_sf_idx[i] = (ref_chan->qu_sf_idx[i] + delta) & 0x3F;\n }\n } else {\n weight_idx = bitstream_read(bc, 2);\n if (weight_idx == 3) {\n UNPACK_SF_VQ_SHAPE(bc, chan->qu_sf_idx, ctx->used_quant_units);\n num_long_vals = bitstream_read(bc, 5);\n delta_bits = bitstream_read(bc, 2);\n min_val = bitstream_read(bc, 4) - 7;\n for (i = 0; i < num_long_vals; i++)\n chan->qu_sf_idx[i] = (chan->qu_sf_idx[i] +\n bitstream_read(bc, 4) - 7) & 0x3F;\n for (i = num_long_vals; i < ctx->used_quant_units; i++)\n chan->qu_sf_idx[i] = (chan->qu_sf_idx[i] + min_val +\n bitstream_read(bc, delta_bits)) & 0x3F;\n } else {\n num_long_vals = bitstream_read(bc, 5);\n delta_bits = bitstream_read(bc, 3);\n min_val = bitstream_read(bc, 6);\n if (num_long_vals > ctx->used_quant_units || delta_bits == 7) {\n av_log(avctx, AV_LOG_ERROR,\n "SF mode 1: invalid parameters!\\n");\n return AVERROR_INVALIDDATA;\n }\n for (i = 0; i < num_long_vals; i++)\n chan->qu_sf_idx[i] = bitstream_read(bc, 6);\n for (i = num_long_vals; i < ctx->used_quant_units; i++)\n chan->qu_sf_idx[i] = (min_val +\n bitstream_read(bc, delta_bits)) & 0x3F;\n }\n }\n break;\n case 2:\n if (ch_num) {\n vlc_tab = &sf_vlc_tabs[bitstream_read(bc, 2)];\n delta = bitstream_read_vlc(bc, vlc_tab->table, vlc_tab->bits, 1);\n chan->qu_sf_idx[0] = (ref_chan->qu_sf_idx[0] + delta) & 0x3F;\n for (i = 1; i < ctx->used_quant_units; i++) {\n diff = ref_chan->qu_sf_idx[i] - ref_chan->qu_sf_idx[i - 1];\n delta = bitstream_read_vlc(bc, vlc_tab->table, vlc_tab->bits, 1);\n chan->qu_sf_idx[i] = (chan->qu_sf_idx[i - 1] + diff + delta) & 0x3F;\n }\n } else {\n vlc_tab = &sf_vlc_tabs[bitstream_read(bc, 2) + 4];\n UNPACK_SF_VQ_SHAPE(bc, chan->qu_sf_idx, ctx->used_quant_units);\n for (i = 0; i < ctx->used_quant_units; i++) {\n delta = bitstream_read_vlc(bc, vlc_tab->table, vlc_tab->bits, 1);\n chan->qu_sf_idx[i] = (chan->qu_sf_idx[i] +\n sign_extend(delta, 4)) & 0x3F;\n }\n }\n break;\n case 3:\n if (ch_num) {\n for (i = 0; i < ctx->used_quant_units; i++)\n chan->qu_sf_idx[i] = ref_chan->qu_sf_idx[i];\n } else {\n weight_idx = bitstream_read(bc, 2);\n vlc_sel = bitstream_read(bc, 2);\n vlc_tab = &sf_vlc_tabs[vlc_sel];\n if (weight_idx == 3) {\n vlc_tab = &sf_vlc_tabs[vlc_sel + 4];\n UNPACK_SF_VQ_SHAPE(bc, chan->qu_sf_idx, ctx->used_quant_units);\n diff = (bitstream_read(bc, 4) + 56) & 0x3F;\n chan->qu_sf_idx[0] = (chan->qu_sf_idx[0] + diff) & 0x3F;\n for (i = 1; i < ctx->used_quant_units; i++) {\n delta = bitstream_read_vlc(bc, vlc_tab->table, vlc_tab->bits, 1);\n diff = (diff + sign_extend(delta, 4)) & 0x3F;\n chan->qu_sf_idx[i] = (diff + chan->qu_sf_idx[i]) & 0x3F;\n }\n } else {\n chan->qu_sf_idx[0] = bitstream_read(bc, 6);\n for (i = 1; i < ctx->used_quant_units; i++) {\n delta = bitstream_read_vlc(bc, vlc_tab->table, vlc_tab->bits, 1);\n chan->qu_sf_idx[i] = (chan->qu_sf_idx[i - 1] + delta) & 0x3F;\n }\n }\n }\n break;\n }\n if (weight_idx && weight_idx < 3)\n return subtract_sf_weights(ctx, chan, weight_idx, avctx);\n return 0;\n}', 'static inline uint32_t bitstream_read(BitstreamContext *bc, unsigned n)\n{\n if (!n)\n return 0;\n if (n > bc->bits_left) {\n refill_32(bc);\n if (bc->bits_left < 32)\n bc->bits_left = n;\n }\n return get_val(bc, n);\n}', 'static inline uint64_t get_val(BitstreamContext *bc, unsigned n)\n{\n#ifdef BITSTREAM_READER_LE\n uint64_t ret = bc->bits & ((UINT64_C(1) << n) - 1);\n bc->bits >>= n;\n#else\n uint64_t ret = bc->bits >> (64 - n);\n bc->bits <<= n;\n#endif\n bc->bits_left -= n;\n return ret;\n}']
816
0
https://github.com/openssl/openssl/blob/2d5d70b15559f9813054ddb11b30b816daf62ebe/crypto/bn/bn_lib.c/#L679
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->d[i] |= (((BN_ULONG)1) << j); bn_check_top(a); return (1); }
['EC_KEY *d2i_ECPrivateKey(EC_KEY **a, const unsigned char **in, long len)\n{\n EC_KEY *ret = NULL;\n EC_PRIVATEKEY *priv_key = NULL;\n if ((priv_key = d2i_EC_PRIVATEKEY(NULL, in, len)) == NULL) {\n ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);\n return NULL;\n }\n if (a == NULL || *a == NULL) {\n if ((ret = EC_KEY_new()) == NULL) {\n ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n } else\n ret = *a;\n if (priv_key->parameters) {\n EC_GROUP_clear_free(ret->group);\n ret->group = ec_asn1_pkparameters2group(priv_key->parameters);\n }\n if (ret->group == NULL) {\n ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);\n goto err;\n }\n ret->version = priv_key->version;\n if (priv_key->privateKey) {\n if (ret->priv_key == NULL)\n ret->priv_key = BN_secure_new();\n ret->priv_key = BN_bin2bn(ASN1_STRING_data(priv_key->privateKey),\n ASN1_STRING_length(priv_key->privateKey),\n ret->priv_key);\n if (ret->priv_key == NULL) {\n ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_BN_LIB);\n goto err;\n }\n } else {\n ECerr(EC_F_D2I_ECPRIVATEKEY, EC_R_MISSING_PRIVATE_KEY);\n goto err;\n }\n EC_POINT_clear_free(ret->pub_key);\n ret->pub_key = EC_POINT_new(ret->group);\n if (ret->pub_key == NULL) {\n ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);\n goto err;\n }\n if (priv_key->publicKey) {\n const unsigned char *pub_oct;\n int pub_oct_len;\n pub_oct = ASN1_STRING_data(priv_key->publicKey);\n pub_oct_len = ASN1_STRING_length(priv_key->publicKey);\n if (pub_oct_len <= 0) {\n ECerr(EC_F_D2I_ECPRIVATEKEY, EC_R_BUFFER_TOO_SMALL);\n goto err;\n }\n ret->conv_form = (point_conversion_form_t) (pub_oct[0] & ~0x01);\n if (!EC_POINT_oct2point(ret->group, ret->pub_key,\n pub_oct, (size_t)(pub_oct_len), NULL)) {\n ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);\n goto err;\n }\n } else {\n if (!EC_POINT_mul\n (ret->group, ret->pub_key, ret->priv_key, NULL, NULL, NULL)) {\n ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);\n goto err;\n }\n ret->enc_flag |= EC_PKEY_NO_PUBKEY;\n }\n if (a)\n *a = ret;\n EC_PRIVATEKEY_free(priv_key);\n return (ret);\n err:\n if (a == NULL || *a != ret)\n EC_KEY_free(ret);\n EC_PRIVATEKEY_free(priv_key);\n return NULL;\n}', 'EC_GROUP *ec_asn1_pkparameters2group(const ECPKPARAMETERS *params)\n{\n EC_GROUP *ret = NULL;\n int tmp = 0;\n if (params == NULL) {\n ECerr(EC_F_EC_ASN1_PKPARAMETERS2GROUP, EC_R_MISSING_PARAMETERS);\n return NULL;\n }\n if (params->type == 0) {\n tmp = OBJ_obj2nid(params->value.named_curve);\n if ((ret = EC_GROUP_new_by_curve_name(tmp)) == NULL) {\n ECerr(EC_F_EC_ASN1_PKPARAMETERS2GROUP,\n EC_R_EC_GROUP_NEW_BY_NAME_FAILURE);\n return NULL;\n }\n EC_GROUP_set_asn1_flag(ret, OPENSSL_EC_NAMED_CURVE);\n } else if (params->type == 1) {\n ret = ec_asn1_parameters2group(params->value.parameters);\n if (!ret) {\n ECerr(EC_F_EC_ASN1_PKPARAMETERS2GROUP, ERR_R_EC_LIB);\n return NULL;\n }\n EC_GROUP_set_asn1_flag(ret, 0x0);\n } else if (params->type == 2) {\n return NULL;\n } else {\n ECerr(EC_F_EC_ASN1_PKPARAMETERS2GROUP, EC_R_ASN1_ERROR);\n return NULL;\n }\n return ret;\n}', 'static EC_GROUP *ec_asn1_parameters2group(const ECPARAMETERS *params)\n{\n int ok = 0, tmp;\n EC_GROUP *ret = NULL;\n BIGNUM *p = NULL, *a = NULL, *b = NULL;\n EC_POINT *point = NULL;\n long field_bits;\n if (!params->fieldID || !params->fieldID->fieldType ||\n !params->fieldID->p.ptr) {\n ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);\n goto err;\n }\n if (!params->curve || !params->curve->a ||\n !params->curve->a->data || !params->curve->b ||\n !params->curve->b->data) {\n ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);\n goto err;\n }\n a = BN_bin2bn(params->curve->a->data, params->curve->a->length, NULL);\n if (a == NULL) {\n ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_BN_LIB);\n goto err;\n }\n b = BN_bin2bn(params->curve->b->data, params->curve->b->length, NULL);\n if (b == NULL) {\n ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_BN_LIB);\n goto err;\n }\n tmp = OBJ_obj2nid(params->fieldID->fieldType);\n if (tmp == NID_X9_62_characteristic_two_field)\n#ifdef OPENSSL_NO_EC2M\n {\n ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_GF2M_NOT_SUPPORTED);\n goto err;\n }\n#else\n {\n X9_62_CHARACTERISTIC_TWO *char_two;\n char_two = params->fieldID->p.char_two;\n field_bits = char_two->m;\n if (field_bits > OPENSSL_ECC_MAX_FIELD_BITS) {\n ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_FIELD_TOO_LARGE);\n goto err;\n }\n if ((p = BN_new()) == NULL) {\n ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n tmp = OBJ_obj2nid(char_two->type);\n if (tmp == NID_X9_62_tpBasis) {\n long tmp_long;\n if (!char_two->p.tpBasis) {\n ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);\n goto err;\n }\n tmp_long = ASN1_INTEGER_get(char_two->p.tpBasis);\n if (!(char_two->m > tmp_long && tmp_long > 0)) {\n ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP,\n EC_R_INVALID_TRINOMIAL_BASIS);\n goto err;\n }\n if (!BN_set_bit(p, (int)char_two->m))\n goto err;\n if (!BN_set_bit(p, (int)tmp_long))\n goto err;\n if (!BN_set_bit(p, 0))\n goto err;\n } else if (tmp == NID_X9_62_ppBasis) {\n X9_62_PENTANOMIAL *penta;\n penta = char_two->p.ppBasis;\n if (!penta) {\n ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);\n goto err;\n }\n if (!\n (char_two->m > penta->k3 && penta->k3 > penta->k2\n && penta->k2 > penta->k1 && penta->k1 > 0)) {\n ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP,\n EC_R_INVALID_PENTANOMIAL_BASIS);\n goto err;\n }\n if (!BN_set_bit(p, (int)char_two->m))\n goto err;\n if (!BN_set_bit(p, (int)penta->k1))\n goto err;\n if (!BN_set_bit(p, (int)penta->k2))\n goto err;\n if (!BN_set_bit(p, (int)penta->k3))\n goto err;\n if (!BN_set_bit(p, 0))\n goto err;\n } else if (tmp == NID_X9_62_onBasis) {\n ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_NOT_IMPLEMENTED);\n goto err;\n } else {\n ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);\n goto err;\n }\n ret = EC_GROUP_new_curve_GF2m(p, a, b, NULL);\n }\n#endif\n else if (tmp == NID_X9_62_prime_field) {\n if (!params->fieldID->p.prime) {\n ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);\n goto err;\n }\n p = ASN1_INTEGER_to_BN(params->fieldID->p.prime, NULL);\n if (p == NULL) {\n ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_ASN1_LIB);\n goto err;\n }\n if (BN_is_negative(p) || BN_is_zero(p)) {\n ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_INVALID_FIELD);\n goto err;\n }\n field_bits = BN_num_bits(p);\n if (field_bits > OPENSSL_ECC_MAX_FIELD_BITS) {\n ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_FIELD_TOO_LARGE);\n goto err;\n }\n ret = EC_GROUP_new_curve_GFp(p, a, b, NULL);\n } else {\n ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_INVALID_FIELD);\n goto err;\n }\n if (ret == NULL) {\n ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_EC_LIB);\n goto err;\n }\n if (params->curve->seed != NULL) {\n OPENSSL_free(ret->seed);\n if ((ret->seed = OPENSSL_malloc(params->curve->seed->length)) == NULL) {\n ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n memcpy(ret->seed, params->curve->seed->data,\n params->curve->seed->length);\n ret->seed_len = params->curve->seed->length;\n }\n if (!params->order || !params->base || !params->base->data) {\n ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_ASN1_ERROR);\n goto err;\n }\n if ((point = EC_POINT_new(ret)) == NULL)\n goto err;\n EC_GROUP_set_point_conversion_form(ret, (point_conversion_form_t)\n (params->base->data[0] & ~0x01));\n if (!EC_POINT_oct2point(ret, point, params->base->data,\n params->base->length, NULL)) {\n ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_EC_LIB);\n goto err;\n }\n if ((a = ASN1_INTEGER_to_BN(params->order, a)) == NULL) {\n ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_ASN1_LIB);\n goto err;\n }\n if (BN_is_negative(a) || BN_is_zero(a)) {\n ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_INVALID_GROUP_ORDER);\n goto err;\n }\n if (BN_num_bits(a) > (int)field_bits + 1) {\n ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, EC_R_INVALID_GROUP_ORDER);\n goto err;\n }\n if (params->cofactor == NULL) {\n BN_free(b);\n b = NULL;\n } else if ((b = ASN1_INTEGER_to_BN(params->cofactor, b)) == NULL) {\n ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_ASN1_LIB);\n goto err;\n }\n if (!EC_GROUP_set_generator(ret, point, a, b)) {\n ECerr(EC_F_EC_ASN1_PARAMETERS2GROUP, ERR_R_EC_LIB);\n goto err;\n }\n ok = 1;\n err:\n if (!ok) {\n EC_GROUP_clear_free(ret);\n ret = NULL;\n }\n BN_free(p);\n BN_free(a);\n BN_free(b);\n EC_POINT_free(point);\n return (ret);\n}', 'int BN_set_bit(BIGNUM *a, int n)\n{\n int i, j, k;\n if (n < 0)\n return 0;\n i = n / BN_BITS2;\n j = n % BN_BITS2;\n if (a->top <= i) {\n if (bn_wexpand(a, i + 1) == NULL)\n return (0);\n for (k = a->top; k < i + 1; k++)\n a->d[k] = 0;\n a->top = i + 1;\n }\n a->d[i] |= (((BN_ULONG)1) << j);\n bn_check_top(a);\n return (1);\n}']
817
1
https://github.com/openssl/openssl/blob/305b68f1a2b6d4d0aa07a6ab47ac372f067a40bb/crypto/bn/bn_sqr.c/#L114
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); }
['static int dsa_priv_decode(EVP_PKEY *pkey, const PKCS8_PRIV_KEY_INFO *p8)\n{\n const unsigned char *p, *pm;\n int pklen, pmlen;\n int ptype;\n const void *pval;\n const ASN1_STRING *pstr;\n const X509_ALGOR *palg;\n ASN1_INTEGER *privkey = NULL;\n BN_CTX *ctx = NULL;\n DSA *dsa = NULL;\n int ret = 0;\n if (!PKCS8_pkey_get0(NULL, &p, &pklen, &palg, p8))\n return 0;\n X509_ALGOR_get0(NULL, &ptype, &pval, palg);\n if ((privkey = d2i_ASN1_INTEGER(NULL, &p, pklen)) == NULL)\n goto decerr;\n if (privkey->type == V_ASN1_NEG_INTEGER || ptype != V_ASN1_SEQUENCE)\n goto decerr;\n pstr = pval;\n pm = pstr->data;\n pmlen = pstr->length;\n if ((dsa = d2i_DSAparams(NULL, &pm, pmlen)) == NULL)\n goto decerr;\n if ((dsa->priv_key = BN_secure_new()) == NULL\n || !ASN1_INTEGER_to_BN(privkey, dsa->priv_key)) {\n DSAerr(DSA_F_DSA_PRIV_DECODE, DSA_R_BN_ERROR);\n goto dsaerr;\n }\n if ((dsa->pub_key = BN_new()) == NULL) {\n DSAerr(DSA_F_DSA_PRIV_DECODE, ERR_R_MALLOC_FAILURE);\n goto dsaerr;\n }\n if ((ctx = BN_CTX_new()) == NULL) {\n DSAerr(DSA_F_DSA_PRIV_DECODE, ERR_R_MALLOC_FAILURE);\n goto dsaerr;\n }\n BN_set_flags(dsa->priv_key, BN_FLG_CONSTTIME);\n if (!BN_mod_exp(dsa->pub_key, dsa->g, dsa->priv_key, dsa->p, ctx)) {\n DSAerr(DSA_F_DSA_PRIV_DECODE, DSA_R_BN_ERROR);\n goto dsaerr;\n }\n EVP_PKEY_assign_DSA(pkey, dsa);\n ret = 1;\n goto done;\n decerr:\n DSAerr(DSA_F_DSA_PRIV_DECODE, DSA_R_DECODE_ERROR);\n dsaerr:\n DSA_free(dsa);\n done:\n BN_CTX_free(ctx);\n ASN1_STRING_clear_free(privkey);\n return ret;\n}', 'BIGNUM *ASN1_INTEGER_to_BN(const ASN1_INTEGER *ai, BIGNUM *bn)\n{\n return asn1_string_to_bn(ai, bn, V_ASN1_INTEGER);\n}', 'static BIGNUM *asn1_string_to_bn(const ASN1_INTEGER *ai, BIGNUM *bn,\n int itype)\n{\n BIGNUM *ret;\n if ((ai->type & ~V_ASN1_NEG) != itype) {\n ASN1err(ASN1_F_ASN1_STRING_TO_BN, ASN1_R_WRONG_INTEGER_TYPE);\n return NULL;\n }\n ret = BN_bin2bn(ai->data, ai->length, bn);\n if (ret == NULL) {\n ASN1err(ASN1_F_ASN1_STRING_TO_BN, ASN1_R_BN_LIB);\n return NULL;\n }\n if (ai->type & V_ASN1_NEG)\n BN_set_negative(ret, 1);\n return ret;\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(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 || BN_get_flags(a, BN_FLG_CONSTTIME) != 0\n || BN_get_flags(m, 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_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 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 (val[0] == 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 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, wmask, window0;\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 = p->top * BN_BITS2;\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 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 (!a->neg) {\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 }\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 powerbufLen += top * sizeof(mont->N.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_nnmod(&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 window0 = (bits - 1) % 5 + 1;\n wmask = (1 << window0) - 1;\n bits -= window0;\n wvalue = bn_get_bits(p, bits) & wmask;\n bn_gather5_t4(tmp.d, top, powerbuf, wvalue);\n while (bits > 0) {\n if (bits < stride)\n stride = bits;\n bits -= stride;\n wvalue = bn_get_bits(p, bits);\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 *n0 = mont->n0, *np;\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 for (np = am.d + top, i = 0; i < top; i++)\n np[i] = mont->N.d[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, np, 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, np, 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, np, 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, np, n0, top, i - 1);\n bn_scatter5(tmp.d, top, powerbuf, i);\n }\n# endif\n window0 = (bits - 1) % 5 + 1;\n wmask = (1 << window0) - 1;\n bits -= window0;\n wvalue = bn_get_bits(p, bits) & wmask;\n bn_gather5(tmp.d, top, powerbuf, wvalue);\n if (top & 7) {\n while (bits > 0) {\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 bn_get_bits5(p->d, bits -= 5));\n }\n } else {\n while (bits > 0) {\n bn_power5(tmp.d, tmp.d, powerbuf, np, n0, top,\n bn_get_bits5(p->d, bits -= 5));\n }\n }\n ret = bn_from_montgomery(tmp.d, tmp.d, NULL, np, 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, window))\n goto err;\n if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&am, top, powerbuf, 1, window))\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(&tmp, top, powerbuf, 2,\n window))\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(&tmp, top, powerbuf, i,\n window))\n goto err;\n }\n }\n window0 = (bits - 1) % window + 1;\n wmask = (1 << window0) - 1;\n bits -= window0;\n wvalue = bn_get_bits(p, bits) & wmask;\n if (!MOD_EXP_CTIME_COPY_FROM_PREBUF(&tmp, top, powerbuf, wvalue,\n window))\n goto err;\n wmask = (1 << window) - 1;\n while (bits > 0) {\n for (i = 0; i < window; i++)\n if (!BN_mod_mul_montgomery(&tmp, &tmp, &tmp, mont, ctx))\n goto err;\n bits -= window;\n wvalue = bn_get_bits(p, bits) & wmask;\n if (!MOD_EXP_CTIME_COPY_FROM_PREBUF(&am, top, powerbuf, wvalue,\n window))\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}', 'static int MOD_EXP_CTIME_COPY_FROM_PREBUF(BIGNUM *b, int top,\n unsigned char *buf, int idx,\n int window)\n{\n int i, j;\n int width = 1 << window;\n volatile BN_ULONG *table = (volatile BN_ULONG *)buf;\n if (bn_wexpand(b, top) == NULL)\n return 0;\n if (window <= 3) {\n for (i = 0; i < top; i++, table += width) {\n BN_ULONG acc = 0;\n for (j = 0; j < width; j++) {\n acc |= table[j] &\n ((BN_ULONG)0 - (constant_time_eq_int(j,idx)&1));\n }\n b->d[i] = acc;\n }\n } else {\n int xstride = 1 << (window - 2);\n BN_ULONG y0, y1, y2, y3;\n i = idx >> (window - 2);\n idx &= xstride - 1;\n y0 = (BN_ULONG)0 - (constant_time_eq_int(i,0)&1);\n y1 = (BN_ULONG)0 - (constant_time_eq_int(i,1)&1);\n y2 = (BN_ULONG)0 - (constant_time_eq_int(i,2)&1);\n y3 = (BN_ULONG)0 - (constant_time_eq_int(i,3)&1);\n for (i = 0; i < top; i++, table += width) {\n BN_ULONG acc = 0;\n for (j = 0; j < xstride; j++) {\n acc |= ( (table[j + 0 * xstride] & y0) |\n (table[j + 1 * xstride] & y1) |\n (table[j + 2 * xstride] & y2) |\n (table[j + 3 * xstride] & y3) )\n & ((BN_ULONG)0 - (constant_time_eq_int(j,idx)&1));\n }\n b->d[i] = acc;\n }\n }\n b->top = top;\n bn_correct_top(b);\n return 1;\n}', 'void bn_correct_top(BIGNUM *a)\n{\n BN_ULONG *ftl;\n int tmp_top = a->top;\n if (tmp_top > 0) {\n for (ftl = &(a->d[tmp_top]); tmp_top > 0; tmp_top--) {\n ftl--;\n if (*ftl != 0)\n break;\n }\n a->top = tmp_top;\n }\n if (a->top == 0)\n a->neg = 0;\n a->flags &= ~BN_FLG_FIXED_TOP;\n bn_pollute(a);\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 int num = mont->N.top;\n#if defined(OPENSSL_BN_ASM_MONT) && defined(MONT_WORD)\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 if ((a->top + b->top) > 2 * num)\n return 0;\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 == NULL || tmp == NULL)\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 rr->top = max;\n bn_correct_top(rr);\n if (r != rr && BN_copy(r, rr) == NULL)\n goto err;\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}']
818
0
https://github.com/openssl/openssl/blob/d40a1b865fddc3d67f8c06ff1f1466fad331c8f7/crypto/bn/bn_lib.c/#L250
int BN_num_bits(const BIGNUM *a) { int i = a->top - 1; bn_check_top(a); if (BN_is_zero(a)) return 0; return ((i*BN_BITS2) + BN_num_bits_word(a->d[i])); }
['int BN_GF2m_mod_inv(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx)\n\t{\n\tBIGNUM *b, *c, *u, *v, *tmp;\n\tint ret = 0;\n\tbn_check_top(a);\n\tbn_check_top(p);\n\tBN_CTX_start(ctx);\n\tb = BN_CTX_get(ctx);\n\tc = BN_CTX_get(ctx);\n\tu = BN_CTX_get(ctx);\n\tv = BN_CTX_get(ctx);\n\tif (v == NULL) goto err;\n\tif (!BN_one(b)) goto err;\n\tif (!BN_GF2m_mod(u, a, p)) goto err;\n\tif (!BN_copy(v, p)) goto err;\n\tif (BN_is_zero(u)) goto err;\n\twhile (1)\n\t\t{\n\t\twhile (!BN_is_odd(u))\n\t\t\t{\n\t\t\tif (!BN_rshift1(u, u)) goto err;\n\t\t\tif (BN_is_odd(b))\n\t\t\t\t{\n\t\t\t\tif (!BN_GF2m_add(b, b, p)) goto err;\n\t\t\t\t}\n\t\t\tif (!BN_rshift1(b, b)) goto err;\n\t\t\t}\n\t\tif (BN_abs_is_word(u, 1)) break;\n\t\tif (BN_num_bits(u) < BN_num_bits(v))\n\t\t\t{\n\t\t\ttmp = u; u = v; v = tmp;\n\t\t\ttmp = b; b = c; c = tmp;\n\t\t\t}\n\t\tif (!BN_GF2m_add(u, u, v)) goto err;\n\t\tif (!BN_GF2m_add(b, b, c)) goto err;\n\t\t}\n\tif (!BN_copy(r, b)) goto err;\n\tbn_check_top(r);\n\tret = 1;\nerr:\n \tBN_CTX_end(ctx);\n\treturn ret;\n\t}', 'BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)\n\t{\n\tint i;\n\tBN_ULONG *A;\n\tconst BN_ULONG *B;\n\tbn_check_top(b);\n\tif (a == b) return(a);\n\tif (bn_wexpand(a,b->top) == NULL) return(NULL);\n#if 1\n\tA=a->d;\n\tB=b->d;\n\tfor (i=b->top>>2; i>0; i--,A+=4,B+=4)\n\t\t{\n\t\tBN_ULONG a0,a1,a2,a3;\n\t\ta0=B[0]; a1=B[1]; a2=B[2]; a3=B[3];\n\t\tA[0]=a0; A[1]=a1; A[2]=a2; A[3]=a3;\n\t\t}\n\tswitch (b->top&3)\n\t\t{\n\t\tcase 3: A[2]=B[2];\n\t\tcase 2: A[1]=B[1];\n\t\tcase 1: A[0]=B[0];\n\t\tcase 0: ;\n\t\t}\n#else\n\tmemcpy(a->d,b->d,sizeof(b->d[0])*b->top);\n#endif\n\ta->top=b->top;\n\ta->neg=b->neg;\n\tbn_check_top(a);\n\treturn(a);\n\t}', 'int BN_num_bits(const BIGNUM *a)\n\t{\n\tint i = a->top - 1;\n\tbn_check_top(a);\n\tif (BN_is_zero(a)) return 0;\n\treturn ((i*BN_BITS2) + BN_num_bits_word(a->d[i]));\n\t}']
819
0
https://github.com/openssl/openssl/blob/4b1043ef1b54b0cf27d00cff9ff9a63f2c523e63/ssl/d1_lib.c/#L772
int DTLSv1_listen(SSL *s, BIO_ADDR *client) { int next, n, ret = 0, clearpkt = 0; unsigned char cookie[DTLS1_COOKIE_LENGTH]; unsigned char seq[SEQ_NUM_SIZE]; const unsigned char *data; unsigned char *p, *buf; unsigned long reclen, fragoff, fraglen, msglen; unsigned int rectype, versmajor, msgseq, msgtype, clientvers, cookielen; BIO *rbio, *wbio; BUF_MEM *bufm; BIO_ADDR *tmpclient = NULL; PACKET pkt, msgpkt, msgpayload, session, cookiepkt; if (!SSL_clear(s)) return -1; ERR_clear_error(); rbio = SSL_get_rbio(s); wbio = SSL_get_wbio(s); if(!rbio || !wbio) { SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_BIO_NOT_SET); return -1; } BIO_ctrl(SSL_get_rbio(s), BIO_CTRL_DGRAM_SET_PEEK_MODE, 1, NULL); if ((s->version & 0xff00) != (DTLS1_VERSION & 0xff00)) { SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_UNSUPPORTED_SSL_VERSION); return -1; } if (s->init_buf == NULL) { if ((bufm = BUF_MEM_new()) == NULL) { SSLerr(SSL_F_DTLSV1_LISTEN, ERR_R_MALLOC_FAILURE); return -1; } if (!BUF_MEM_grow(bufm, SSL3_RT_MAX_PLAIN_LENGTH)) { BUF_MEM_free(bufm); SSLerr(SSL_F_DTLSV1_LISTEN, ERR_R_MALLOC_FAILURE); return -1; } s->init_buf = bufm; } buf = (unsigned char *)s->init_buf->data; do { clear_sys_error(); n = BIO_read(rbio, buf, SSL3_RT_MAX_PLAIN_LENGTH); if (n <= 0) { if(BIO_should_retry(rbio)) { goto end; } return -1; } clearpkt = 1; if (!PACKET_buf_init(&pkt, buf, n)) { SSLerr(SSL_F_DTLSV1_LISTEN, ERR_R_INTERNAL_ERROR); return -1; } if (n < DTLS1_RT_HEADER_LENGTH) { SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_RECORD_TOO_SMALL); goto end; } if (s->msg_callback) s->msg_callback(0, 0, SSL3_RT_HEADER, buf, DTLS1_RT_HEADER_LENGTH, s, s->msg_callback_arg); if (!PACKET_get_1(&pkt, &rectype) || !PACKET_get_1(&pkt, &versmajor)) { SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_LENGTH_MISMATCH); goto end; } if (rectype != SSL3_RT_HANDSHAKE) { SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_UNEXPECTED_MESSAGE); goto end; } if (versmajor != DTLS1_VERSION_MAJOR) { SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_BAD_PROTOCOL_VERSION_NUMBER); goto end; } if (!PACKET_forward(&pkt, 1) || !PACKET_copy_bytes(&pkt, seq, SEQ_NUM_SIZE) || !PACKET_get_length_prefixed_2(&pkt, &msgpkt)) { SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_LENGTH_MISMATCH); goto end; } if (seq[0] != 0 || seq[1] != 0) { SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_UNEXPECTED_MESSAGE); goto end; } data = PACKET_data(&msgpkt); if (!PACKET_get_1(&msgpkt, &msgtype) || !PACKET_get_net_3(&msgpkt, &msglen) || !PACKET_get_net_2(&msgpkt, &msgseq) || !PACKET_get_net_3(&msgpkt, &fragoff) || !PACKET_get_net_3(&msgpkt, &fraglen) || !PACKET_get_sub_packet(&msgpkt, &msgpayload, fraglen) || PACKET_remaining(&msgpkt) != 0) { SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_LENGTH_MISMATCH); goto end; } if (msgtype != SSL3_MT_CLIENT_HELLO) { SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_UNEXPECTED_MESSAGE); goto end; } if(msgseq > 2) { SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_INVALID_SEQUENCE_NUMBER); goto end; } if (fragoff != 0 || fraglen > msglen) { SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_FRAGMENTED_CLIENT_HELLO); goto end; } if (s->msg_callback) s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE, data, fraglen + DTLS1_HM_HEADER_LENGTH, s, s->msg_callback_arg); if (!PACKET_get_net_2(&msgpayload, &clientvers)) { SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_LENGTH_MISMATCH); goto end; } if (DTLS_VERSION_LT(clientvers, (unsigned int)s->method->version) && s->method->version != DTLS_ANY_VERSION) { SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_WRONG_VERSION_NUMBER); goto end; } if (!PACKET_forward(&msgpayload, SSL3_RANDOM_SIZE) || !PACKET_get_length_prefixed_1(&msgpayload, &session) || !PACKET_get_length_prefixed_1(&msgpayload, &cookiepkt)) { SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_LENGTH_MISMATCH); goto end; } if (PACKET_remaining(&cookiepkt) == 0) { next = LISTEN_SEND_VERIFY_REQUEST; } else { if (s->ctx->app_verify_cookie_cb == NULL) { SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_NO_VERIFY_COOKIE_CALLBACK); return -1; } if (s->ctx->app_verify_cookie_cb(s, PACKET_data(&cookiepkt), PACKET_remaining(&cookiepkt)) == 0) { next = LISTEN_SEND_VERIFY_REQUEST; } else { next = LISTEN_SUCCESS; } } if (next == LISTEN_SEND_VERIFY_REQUEST) { BIO_ctrl(SSL_get_rbio(s), BIO_CTRL_DGRAM_SET_PEEK_MODE, 0, NULL); BIO_read(rbio, buf, SSL3_RT_MAX_PLAIN_LENGTH); BIO_ctrl(SSL_get_rbio(s), BIO_CTRL_DGRAM_SET_PEEK_MODE, 1, NULL); if (s->ctx->app_gen_cookie_cb == NULL || s->ctx->app_gen_cookie_cb(s, cookie, &cookielen) == 0 || cookielen > 255) { SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_COOKIE_GEN_CALLBACK_FAILURE); return -1; } p = &buf[DTLS1_RT_HEADER_LENGTH]; msglen = dtls_raw_hello_verify_request(p + DTLS1_HM_HEADER_LENGTH, cookie, cookielen); *p++ = DTLS1_MT_HELLO_VERIFY_REQUEST; l2n3(msglen, p); s2n(0, p); l2n3(0, p); l2n3(msglen, p); reclen = msglen + DTLS1_HM_HEADER_LENGTH; p = buf; *(p++) = SSL3_RT_HANDSHAKE; if (s->method->version == DTLS_ANY_VERSION) { *(p++) = DTLS1_VERSION >> 8; *(p++) = DTLS1_VERSION & 0xff; } else { *(p++) = s->version >> 8; *(p++) = s->version & 0xff; } memcpy(p, seq, SEQ_NUM_SIZE); p += SEQ_NUM_SIZE; s2n(reclen, p); reclen += DTLS1_RT_HEADER_LENGTH; if (s->msg_callback) s->msg_callback(1, 0, SSL3_RT_HEADER, buf, DTLS1_RT_HEADER_LENGTH, s, s->msg_callback_arg); if ((tmpclient = BIO_ADDR_new()) == NULL || BIO_dgram_get_peer(rbio, tmpclient) <= 0 || BIO_dgram_set_peer(wbio, tmpclient) <= 0) { SSLerr(SSL_F_DTLSV1_LISTEN, ERR_R_INTERNAL_ERROR); goto end; } BIO_ADDR_free(tmpclient); tmpclient = NULL; if (BIO_write(wbio, buf, reclen) < (int)reclen) { if(BIO_should_retry(wbio)) { goto end; } return -1; } if (BIO_flush(wbio) <= 0) { if(BIO_should_retry(wbio)) { goto end; } return -1; } } } while (next != LISTEN_SUCCESS); s->d1->handshake_read_seq = 1; s->d1->handshake_write_seq = 1; s->d1->next_handshake_write_seq = 1; DTLS_RECORD_LAYER_set_write_sequence(&s->rlayer, seq); SSL_set_options(s, SSL_OP_COOKIE_EXCHANGE); ossl_statem_set_hello_verify_done(s); if(BIO_dgram_get_peer(rbio, client) <= 0) { SSLerr(SSL_F_DTLSV1_LISTEN, ERR_R_INTERNAL_ERROR); return -1; } ret = 1; clearpkt = 0; end: BIO_ADDR_free(tmpclient); BIO_ctrl(SSL_get_rbio(s), BIO_CTRL_DGRAM_SET_PEEK_MODE, 0, NULL); if (clearpkt) { BIO_read(rbio, buf, SSL3_RT_MAX_PLAIN_LENGTH); } return ret; }
['int DTLSv1_listen(SSL *s, BIO_ADDR *client)\n{\n int next, n, ret = 0, clearpkt = 0;\n unsigned char cookie[DTLS1_COOKIE_LENGTH];\n unsigned char seq[SEQ_NUM_SIZE];\n const unsigned char *data;\n unsigned char *p, *buf;\n unsigned long reclen, fragoff, fraglen, msglen;\n unsigned int rectype, versmajor, msgseq, msgtype, clientvers, cookielen;\n BIO *rbio, *wbio;\n BUF_MEM *bufm;\n BIO_ADDR *tmpclient = NULL;\n PACKET pkt, msgpkt, msgpayload, session, cookiepkt;\n if (!SSL_clear(s))\n return -1;\n ERR_clear_error();\n rbio = SSL_get_rbio(s);\n wbio = SSL_get_wbio(s);\n if(!rbio || !wbio) {\n SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_BIO_NOT_SET);\n return -1;\n }\n BIO_ctrl(SSL_get_rbio(s), BIO_CTRL_DGRAM_SET_PEEK_MODE, 1, NULL);\n if ((s->version & 0xff00) != (DTLS1_VERSION & 0xff00)) {\n SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_UNSUPPORTED_SSL_VERSION);\n return -1;\n }\n if (s->init_buf == NULL) {\n if ((bufm = BUF_MEM_new()) == NULL) {\n SSLerr(SSL_F_DTLSV1_LISTEN, ERR_R_MALLOC_FAILURE);\n return -1;\n }\n if (!BUF_MEM_grow(bufm, SSL3_RT_MAX_PLAIN_LENGTH)) {\n BUF_MEM_free(bufm);\n SSLerr(SSL_F_DTLSV1_LISTEN, ERR_R_MALLOC_FAILURE);\n return -1;\n }\n s->init_buf = bufm;\n }\n buf = (unsigned char *)s->init_buf->data;\n do {\n clear_sys_error();\n n = BIO_read(rbio, buf, SSL3_RT_MAX_PLAIN_LENGTH);\n if (n <= 0) {\n if(BIO_should_retry(rbio)) {\n goto end;\n }\n return -1;\n }\n clearpkt = 1;\n if (!PACKET_buf_init(&pkt, buf, n)) {\n SSLerr(SSL_F_DTLSV1_LISTEN, ERR_R_INTERNAL_ERROR);\n return -1;\n }\n if (n < DTLS1_RT_HEADER_LENGTH) {\n SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_RECORD_TOO_SMALL);\n goto end;\n }\n if (s->msg_callback)\n s->msg_callback(0, 0, SSL3_RT_HEADER, buf,\n DTLS1_RT_HEADER_LENGTH, s, s->msg_callback_arg);\n if (!PACKET_get_1(&pkt, &rectype)\n || !PACKET_get_1(&pkt, &versmajor)) {\n SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_LENGTH_MISMATCH);\n goto end;\n }\n if (rectype != SSL3_RT_HANDSHAKE) {\n SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_UNEXPECTED_MESSAGE);\n goto end;\n }\n if (versmajor != DTLS1_VERSION_MAJOR) {\n SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_BAD_PROTOCOL_VERSION_NUMBER);\n goto end;\n }\n if (!PACKET_forward(&pkt, 1)\n || !PACKET_copy_bytes(&pkt, seq, SEQ_NUM_SIZE)\n || !PACKET_get_length_prefixed_2(&pkt, &msgpkt)) {\n SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_LENGTH_MISMATCH);\n goto end;\n }\n if (seq[0] != 0 || seq[1] != 0) {\n SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_UNEXPECTED_MESSAGE);\n goto end;\n }\n data = PACKET_data(&msgpkt);\n if (!PACKET_get_1(&msgpkt, &msgtype)\n || !PACKET_get_net_3(&msgpkt, &msglen)\n || !PACKET_get_net_2(&msgpkt, &msgseq)\n || !PACKET_get_net_3(&msgpkt, &fragoff)\n || !PACKET_get_net_3(&msgpkt, &fraglen)\n || !PACKET_get_sub_packet(&msgpkt, &msgpayload, fraglen)\n || PACKET_remaining(&msgpkt) != 0) {\n SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_LENGTH_MISMATCH);\n goto end;\n }\n if (msgtype != SSL3_MT_CLIENT_HELLO) {\n SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_UNEXPECTED_MESSAGE);\n goto end;\n }\n if(msgseq > 2) {\n SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_INVALID_SEQUENCE_NUMBER);\n goto end;\n }\n if (fragoff != 0 || fraglen > msglen) {\n SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_FRAGMENTED_CLIENT_HELLO);\n goto end;\n }\n if (s->msg_callback)\n s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE, data,\n fraglen + DTLS1_HM_HEADER_LENGTH, s,\n s->msg_callback_arg);\n if (!PACKET_get_net_2(&msgpayload, &clientvers)) {\n SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_LENGTH_MISMATCH);\n goto end;\n }\n if (DTLS_VERSION_LT(clientvers, (unsigned int)s->method->version) &&\n s->method->version != DTLS_ANY_VERSION) {\n SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_WRONG_VERSION_NUMBER);\n goto end;\n }\n if (!PACKET_forward(&msgpayload, SSL3_RANDOM_SIZE)\n || !PACKET_get_length_prefixed_1(&msgpayload, &session)\n || !PACKET_get_length_prefixed_1(&msgpayload, &cookiepkt)) {\n SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_LENGTH_MISMATCH);\n goto end;\n }\n if (PACKET_remaining(&cookiepkt) == 0) {\n next = LISTEN_SEND_VERIFY_REQUEST;\n } else {\n if (s->ctx->app_verify_cookie_cb == NULL) {\n SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_NO_VERIFY_COOKIE_CALLBACK);\n return -1;\n }\n if (s->ctx->app_verify_cookie_cb(s, PACKET_data(&cookiepkt),\n PACKET_remaining(&cookiepkt)) ==\n 0) {\n next = LISTEN_SEND_VERIFY_REQUEST;\n } else {\n next = LISTEN_SUCCESS;\n }\n }\n if (next == LISTEN_SEND_VERIFY_REQUEST) {\n BIO_ctrl(SSL_get_rbio(s), BIO_CTRL_DGRAM_SET_PEEK_MODE, 0, NULL);\n BIO_read(rbio, buf, SSL3_RT_MAX_PLAIN_LENGTH);\n BIO_ctrl(SSL_get_rbio(s), BIO_CTRL_DGRAM_SET_PEEK_MODE, 1, NULL);\n if (s->ctx->app_gen_cookie_cb == NULL ||\n s->ctx->app_gen_cookie_cb(s, cookie, &cookielen) == 0 ||\n cookielen > 255) {\n SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_COOKIE_GEN_CALLBACK_FAILURE);\n return -1;\n }\n p = &buf[DTLS1_RT_HEADER_LENGTH];\n msglen = dtls_raw_hello_verify_request(p + DTLS1_HM_HEADER_LENGTH,\n cookie, cookielen);\n *p++ = DTLS1_MT_HELLO_VERIFY_REQUEST;\n l2n3(msglen, p);\n s2n(0, p);\n l2n3(0, p);\n l2n3(msglen, p);\n reclen = msglen + DTLS1_HM_HEADER_LENGTH;\n p = buf;\n *(p++) = SSL3_RT_HANDSHAKE;\n if (s->method->version == DTLS_ANY_VERSION) {\n *(p++) = DTLS1_VERSION >> 8;\n *(p++) = DTLS1_VERSION & 0xff;\n } else {\n *(p++) = s->version >> 8;\n *(p++) = s->version & 0xff;\n }\n memcpy(p, seq, SEQ_NUM_SIZE);\n p += SEQ_NUM_SIZE;\n s2n(reclen, p);\n reclen += DTLS1_RT_HEADER_LENGTH;\n if (s->msg_callback)\n s->msg_callback(1, 0, SSL3_RT_HEADER, buf,\n DTLS1_RT_HEADER_LENGTH, s, s->msg_callback_arg);\n if ((tmpclient = BIO_ADDR_new()) == NULL\n || BIO_dgram_get_peer(rbio, tmpclient) <= 0\n || BIO_dgram_set_peer(wbio, tmpclient) <= 0) {\n SSLerr(SSL_F_DTLSV1_LISTEN, ERR_R_INTERNAL_ERROR);\n goto end;\n }\n BIO_ADDR_free(tmpclient);\n tmpclient = NULL;\n if (BIO_write(wbio, buf, reclen) < (int)reclen) {\n if(BIO_should_retry(wbio)) {\n goto end;\n }\n return -1;\n }\n if (BIO_flush(wbio) <= 0) {\n if(BIO_should_retry(wbio)) {\n goto end;\n }\n return -1;\n }\n }\n } while (next != LISTEN_SUCCESS);\n s->d1->handshake_read_seq = 1;\n s->d1->handshake_write_seq = 1;\n s->d1->next_handshake_write_seq = 1;\n DTLS_RECORD_LAYER_set_write_sequence(&s->rlayer, seq);\n SSL_set_options(s, SSL_OP_COOKIE_EXCHANGE);\n ossl_statem_set_hello_verify_done(s);\n if(BIO_dgram_get_peer(rbio, client) <= 0) {\n SSLerr(SSL_F_DTLSV1_LISTEN, ERR_R_INTERNAL_ERROR);\n return -1;\n }\n ret = 1;\n clearpkt = 0;\nend:\n BIO_ADDR_free(tmpclient);\n BIO_ctrl(SSL_get_rbio(s), BIO_CTRL_DGRAM_SET_PEEK_MODE, 0, NULL);\n if (clearpkt) {\n BIO_read(rbio, buf, SSL3_RT_MAX_PLAIN_LENGTH);\n }\n return ret;\n}', 'size_t BUF_MEM_grow(BUF_MEM *str, size_t len)\n{\n char *ret;\n size_t n;\n if (str->length >= len) {\n str->length = len;\n return (len);\n }\n if (str->max >= len) {\n memset(&str->data[str->length], 0, len - str->length);\n str->length = len;\n return (len);\n }\n if (len > LIMIT_BEFORE_EXPANSION) {\n BUFerr(BUF_F_BUF_MEM_GROW, ERR_R_MALLOC_FAILURE);\n return 0;\n }\n n = (len + 3) / 3 * 4;\n if ((str->flags & BUF_MEM_FLAG_SECURE))\n ret = sec_alloc_realloc(str, n);\n else\n ret = OPENSSL_realloc(str->data, n);\n if (ret == NULL) {\n BUFerr(BUF_F_BUF_MEM_GROW, ERR_R_MALLOC_FAILURE);\n len = 0;\n } else {\n str->data = ret;\n str->max = n;\n memset(&str->data[str->length], 0, len - str->length);\n str->length = len;\n }\n return (len);\n}']
820
0
https://github.com/libav/libav/blob/48aef27f5232794e70ecef0d347b9f65e27a9bad/libavcodec/dca_xll.c/#L392
static void dca_xll_inv_adapt_pred(int *samples, int nsamples, unsigned order, const int *prev, const uint8_t *q_ind) { static const uint16_t table[0x81] = { 0, 3070, 5110, 7140, 9156, 11154, 13132, 15085, 17010, 18904, 20764, 22588, 24373, 26117, 27818, 29474, 31085, 32648, 34164, 35631, 37049, 38418, 39738, 41008, 42230, 43404, 44530, 45609, 46642, 47630, 48575, 49477, 50337, 51157, 51937, 52681, 53387, 54059, 54697, 55302, 55876, 56421, 56937, 57426, 57888, 58326, 58741, 59132, 59502, 59852, 60182, 60494, 60789, 61066, 61328, 61576, 61809, 62029, 62236, 62431, 62615, 62788, 62951, 63105, 63250, 63386, 63514, 63635, 63749, 63855, 63956, 64051, 64140, 64224, 64302, 64376, 64446, 64512, 64573, 64631, 64686, 64737, 64785, 64830, 64873, 64913, 64950, 64986, 65019, 65050, 65079, 65107, 65133, 65157, 65180, 65202, 65222, 65241, 65259, 65275, 65291, 65306, 65320, 65333, 65345, 65357, 65368, 65378, 65387, 65396, 65405, 65413, 65420, 65427, 65434, 65440, 65446, 65451, 65456, 65461, 65466, 65470, 65474, 65478, 65481, 65485, 65488, 65491, 65535, }; int c[DCA_XLL_AORDER_MAX]; int64_t s; unsigned i, j; for (i = 0; i < order; i++) { if (q_ind[i] & 1) c[i] = -table[(q_ind[i] >> 1) + 1]; else c[i] = table[q_ind[i] >> 1]; } for (i = 1; i < order; i++) { if (i & 1) c[i / 2] += ((int64_t) c[i] * c[i / 2] + 0x8000) >> 16; for (j = 0; j < i / 2; j++) { int r0 = c[j]; int r1 = c[i - j - 1]; c[j] += ((int64_t) c[i] * r1 + 0x8000) >> 16; c[i - j - 1] += ((int64_t) c[i] * r0 + 0x8000) >> 16; } } if (prev) { for (i = 0; i < order; i++) { for (j = s = 0; j < i; j++) s += (int64_t) c[j] * samples[i - 1 - j]; for (; j < order; j++) s += (int64_t) c[j] * prev[DCA_XLL_AORDER_MAX + i - 1 - j]; samples[i] -= av_clip((s + 0x8000) >> 16, -0x1000000, 0xffffff); } } for (i = order; i < nsamples; i++) { for (j = s = 0; j < order; j++) s += (int64_t) c[j] * samples[i - 1 - j]; samples[i] -= av_clip((s + 0x8000) >> 16, -0x1000000, 0xffffff); } }
['static void dca_xll_inv_adapt_pred(int *samples, int nsamples, unsigned order,\n const int *prev, const uint8_t *q_ind)\n{\n static const uint16_t table[0x81] = {\n 0, 3070, 5110, 7140, 9156, 11154, 13132, 15085,\n 17010, 18904, 20764, 22588, 24373, 26117, 27818, 29474,\n 31085, 32648, 34164, 35631, 37049, 38418, 39738, 41008,\n 42230, 43404, 44530, 45609, 46642, 47630, 48575, 49477,\n 50337, 51157, 51937, 52681, 53387, 54059, 54697, 55302,\n 55876, 56421, 56937, 57426, 57888, 58326, 58741, 59132,\n 59502, 59852, 60182, 60494, 60789, 61066, 61328, 61576,\n 61809, 62029, 62236, 62431, 62615, 62788, 62951, 63105,\n 63250, 63386, 63514, 63635, 63749, 63855, 63956, 64051,\n 64140, 64224, 64302, 64376, 64446, 64512, 64573, 64631,\n 64686, 64737, 64785, 64830, 64873, 64913, 64950, 64986,\n 65019, 65050, 65079, 65107, 65133, 65157, 65180, 65202,\n 65222, 65241, 65259, 65275, 65291, 65306, 65320, 65333,\n 65345, 65357, 65368, 65378, 65387, 65396, 65405, 65413,\n 65420, 65427, 65434, 65440, 65446, 65451, 65456, 65461,\n 65466, 65470, 65474, 65478, 65481, 65485, 65488, 65491,\n 65535,\n };\n int c[DCA_XLL_AORDER_MAX];\n int64_t s;\n unsigned i, j;\n for (i = 0; i < order; i++) {\n if (q_ind[i] & 1)\n c[i] = -table[(q_ind[i] >> 1) + 1];\n else\n c[i] = table[q_ind[i] >> 1];\n }\n for (i = 1; i < order; i++) {\n if (i & 1)\n c[i / 2] += ((int64_t) c[i] * c[i / 2] + 0x8000) >> 16;\n for (j = 0; j < i / 2; j++) {\n int r0 = c[j];\n int r1 = c[i - j - 1];\n c[j] += ((int64_t) c[i] * r1 + 0x8000) >> 16;\n c[i - j - 1] += ((int64_t) c[i] * r0 + 0x8000) >> 16;\n }\n }\n if (prev) {\n for (i = 0; i < order; i++) {\n for (j = s = 0; j < i; j++)\n s += (int64_t) c[j] * samples[i - 1 - j];\n for (; j < order; j++)\n s += (int64_t) c[j] * prev[DCA_XLL_AORDER_MAX + i - 1 - j];\n samples[i] -= av_clip((s + 0x8000) >> 16, -0x1000000, 0xffffff);\n }\n }\n for (i = order; i < nsamples; i++) {\n for (j = s = 0; j < order; j++)\n s += (int64_t) c[j] * samples[i - 1 - j];\n samples[i] -= av_clip((s + 0x8000) >> 16, -0x1000000, 0xffffff);\n }\n}']
821
0
https://github.com/openssl/openssl/blob/a8140a42f5ee9e4e1423b5b6b319dc4657659f6f/crypto/bn/bn_lib.c/#L232
static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words) { BN_ULONG *a = NULL; if (words > (INT_MAX / (4 * BN_BITS2))) { BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG); return NULL; } if (BN_get_flags(b, BN_FLG_STATIC_DATA)) { BNerr(BN_F_BN_EXPAND_INTERNAL, 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) { BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE); return NULL; } assert(b->top <= words); if (b->top > 0) memcpy(a, b->d, sizeof(*a) * b->top); return a; }
['int RSA_check_key_ex(const RSA *key, BN_GENCB *cb)\n{\n#ifdef FIPS_MODE\n return rsa_sp800_56b_check_public(key)\n && rsa_sp800_56b_check_private(key)\n && rsa_sp800_56b_check_keypair(key, NULL, -1, RSA_bits(key));\n#else\n BIGNUM *i, *j, *k, *l, *m;\n BN_CTX *ctx;\n int ret = 1, ex_primes = 0, idx;\n RSA_PRIME_INFO *pinfo;\n if (key->p == NULL || key->q == NULL || key->n == NULL\n || key->e == NULL || key->d == NULL) {\n RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_VALUE_MISSING);\n return 0;\n }\n if (key->version == RSA_ASN1_VERSION_MULTI) {\n ex_primes = sk_RSA_PRIME_INFO_num(key->prime_infos);\n if (ex_primes <= 0\n || (ex_primes + 2) > rsa_multip_cap(BN_num_bits(key->n))) {\n RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_INVALID_MULTI_PRIME_KEY);\n return 0;\n }\n }\n i = BN_new();\n j = BN_new();\n k = BN_new();\n l = BN_new();\n m = BN_new();\n ctx = BN_CTX_new();\n if (i == NULL || j == NULL || k == NULL || l == NULL\n || m == NULL || ctx == NULL) {\n ret = -1;\n RSAerr(RSA_F_RSA_CHECK_KEY_EX, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n if (BN_is_one(key->e)) {\n ret = 0;\n RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_BAD_E_VALUE);\n }\n if (!BN_is_odd(key->e)) {\n ret = 0;\n RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_BAD_E_VALUE);\n }\n if (BN_is_prime_ex(key->p, BN_prime_checks, NULL, cb) != 1) {\n ret = 0;\n RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_P_NOT_PRIME);\n }\n if (BN_is_prime_ex(key->q, BN_prime_checks, NULL, cb) != 1) {\n ret = 0;\n RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_Q_NOT_PRIME);\n }\n for (idx = 0; idx < ex_primes; idx++) {\n pinfo = sk_RSA_PRIME_INFO_value(key->prime_infos, idx);\n if (BN_is_prime_ex(pinfo->r, BN_prime_checks, NULL, cb) != 1) {\n ret = 0;\n RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_MP_R_NOT_PRIME);\n }\n }\n if (!BN_mul(i, key->p, key->q, ctx)) {\n ret = -1;\n goto err;\n }\n for (idx = 0; idx < ex_primes; idx++) {\n pinfo = sk_RSA_PRIME_INFO_value(key->prime_infos, idx);\n if (!BN_mul(i, i, pinfo->r, ctx)) {\n ret = -1;\n goto err;\n }\n }\n if (BN_cmp(i, key->n) != 0) {\n ret = 0;\n if (ex_primes)\n RSAerr(RSA_F_RSA_CHECK_KEY_EX,\n RSA_R_N_DOES_NOT_EQUAL_PRODUCT_OF_PRIMES);\n else\n RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_N_DOES_NOT_EQUAL_P_Q);\n }\n if (!BN_sub(i, key->p, BN_value_one())) {\n ret = -1;\n goto err;\n }\n if (!BN_sub(j, key->q, BN_value_one())) {\n ret = -1;\n goto err;\n }\n if (!BN_mul(l, i, j, ctx)) {\n ret = -1;\n goto err;\n }\n if (!BN_gcd(m, i, j, ctx)) {\n ret = -1;\n goto err;\n }\n for (idx = 0; idx < ex_primes; idx++) {\n pinfo = sk_RSA_PRIME_INFO_value(key->prime_infos, idx);\n if (!BN_sub(k, pinfo->r, BN_value_one())) {\n ret = -1;\n goto err;\n }\n if (!BN_mul(l, l, k, ctx)) {\n ret = -1;\n goto err;\n }\n if (!BN_gcd(m, m, k, ctx)) {\n ret = -1;\n goto err;\n }\n }\n if (!BN_div(k, NULL, l, m, ctx)) {\n ret = -1;\n goto err;\n }\n if (!BN_mod_mul(i, key->d, key->e, k, ctx)) {\n ret = -1;\n goto err;\n }\n if (!BN_is_one(i)) {\n ret = 0;\n RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_D_E_NOT_CONGRUENT_TO_1);\n }\n if (key->dmp1 != NULL && key->dmq1 != NULL && key->iqmp != NULL) {\n if (!BN_sub(i, key->p, BN_value_one())) {\n ret = -1;\n goto err;\n }\n if (!BN_mod(j, key->d, i, ctx)) {\n ret = -1;\n goto err;\n }\n if (BN_cmp(j, key->dmp1) != 0) {\n ret = 0;\n RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_DMP1_NOT_CONGRUENT_TO_D);\n }\n if (!BN_sub(i, key->q, BN_value_one())) {\n ret = -1;\n goto err;\n }\n if (!BN_mod(j, key->d, i, ctx)) {\n ret = -1;\n goto err;\n }\n if (BN_cmp(j, key->dmq1) != 0) {\n ret = 0;\n RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_DMQ1_NOT_CONGRUENT_TO_D);\n }\n if (!BN_mod_inverse(i, key->q, key->p, ctx)) {\n ret = -1;\n goto err;\n }\n if (BN_cmp(i, key->iqmp) != 0) {\n ret = 0;\n RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_IQMP_NOT_INVERSE_OF_Q);\n }\n }\n for (idx = 0; idx < ex_primes; idx++) {\n pinfo = sk_RSA_PRIME_INFO_value(key->prime_infos, idx);\n if (!BN_sub(i, pinfo->r, BN_value_one())) {\n ret = -1;\n goto err;\n }\n if (!BN_mod(j, key->d, i, ctx)) {\n ret = -1;\n goto err;\n }\n if (BN_cmp(j, pinfo->d) != 0) {\n ret = 0;\n RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_MP_EXPONENT_NOT_CONGRUENT_TO_D);\n }\n if (!BN_mod_inverse(i, pinfo->pp, pinfo->r, ctx)) {\n ret = -1;\n goto err;\n }\n if (BN_cmp(i, pinfo->t) != 0) {\n ret = 0;\n RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_MP_COEFFICIENT_NOT_INVERSE_OF_R);\n }\n }\n err:\n BN_free(i);\n BN_free(j);\n BN_free(k);\n BN_free(l);\n BN_free(m);\n BN_CTX_free(ctx);\n return ret;\n#endif\n}', 'int BN_gcd(BIGNUM *r, const BIGNUM *in_a, const BIGNUM *in_b, BN_CTX *ctx)\n{\n BIGNUM *a, *b, *t;\n int ret = 0;\n bn_check_top(in_a);\n bn_check_top(in_b);\n BN_CTX_start(ctx);\n a = BN_CTX_get(ctx);\n b = BN_CTX_get(ctx);\n if (b == NULL)\n goto err;\n if (BN_copy(a, in_a) == NULL)\n goto err;\n if (BN_copy(b, in_b) == NULL)\n goto err;\n a->neg = 0;\n b->neg = 0;\n if (BN_cmp(a, b) < 0) {\n t = a;\n a = b;\n b = t;\n }\n t = euclid(a, b);\n if (t == NULL)\n goto err;\n if (BN_copy(r, t) == NULL)\n goto err;\n ret = 1;\n err:\n BN_CTX_end(ctx);\n bn_check_top(r);\n return ret;\n}', 'int BN_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)\n{\n int ret, r_neg, cmp_res;\n bn_check_top(a);\n bn_check_top(b);\n if (a->neg != b->neg) {\n r_neg = a->neg;\n ret = BN_uadd(r, a, b);\n } else {\n cmp_res = BN_ucmp(a, b);\n if (cmp_res > 0) {\n r_neg = a->neg;\n ret = BN_usub(r, a, b);\n } else if (cmp_res < 0) {\n r_neg = !b->neg;\n ret = BN_usub(r, b, a);\n } else {\n r_neg = 0;\n BN_zero(r);\n ret = 1;\n }\n }\n r->neg = r_neg;\n bn_check_top(r);\n return ret;\n}', 'int BN_usub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)\n{\n int max, min, dif;\n BN_ULONG t1, t2, borrow, *rp;\n const BN_ULONG *ap, *bp;\n bn_check_top(a);\n bn_check_top(b);\n max = a->top;\n min = b->top;\n dif = max - min;\n if (dif < 0) {\n BNerr(BN_F_BN_USUB, BN_R_ARG2_LT_ARG3);\n return 0;\n }\n if (bn_wexpand(r, max) == NULL)\n return 0;\n ap = a->d;\n bp = b->d;\n rp = r->d;\n borrow = bn_sub_words(rp, ap, bp, min);\n ap += min;\n rp += min;\n while (dif) {\n dif--;\n t1 = *(ap++);\n t2 = (t1 - borrow) & BN_MASK2;\n *(rp++) = t2;\n borrow &= (t1 == 0);\n }\n while (max && *--rp == 0)\n max--;\n r->top = max;\n r->neg = 0;\n bn_pollute(r);\n return 1;\n}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n return (words <= a->dmax) ? a : bn_expand2(a, words);\n}', 'BIGNUM *bn_expand2(BIGNUM *b, int words)\n{\n if (words > b->dmax) {\n BN_ULONG *a = bn_expand_internal(b, words);\n if (!a)\n return NULL;\n if (b->d) {\n OPENSSL_cleanse(b->d, b->dmax * sizeof(b->d[0]));\n bn_free_d(b);\n }\n b->d = a;\n b->dmax = words;\n }\n return b;\n}', 'static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)\n{\n BN_ULONG *a = NULL;\n if (words > (INT_MAX / (4 * BN_BITS2))) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);\n return NULL;\n }\n if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);\n return NULL;\n }\n if (BN_get_flags(b, BN_FLG_SECURE))\n a = OPENSSL_secure_zalloc(words * sizeof(*a));\n else\n a = OPENSSL_zalloc(words * sizeof(*a));\n if (a == NULL) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);\n return NULL;\n }\n assert(b->top <= words);\n if (b->top > 0)\n memcpy(a, b->d, sizeof(*a) * b->top);\n return a;\n}']
822
0
https://github.com/libav/libav/blob/645d26520a1a1900a89f2811eb78a5d637ca7877/ffmpeg.c/#L3399
static void opt_output_file(const char *filename) { AVFormatContext *oc; int use_video, use_audio, use_subtitle; int input_has_video, input_has_audio, input_has_subtitle; AVFormatParameters params, *ap = &params; AVOutputFormat *file_oformat; if (!strcmp(filename, "-")) filename = "pipe:"; oc = avformat_alloc_context(); if (!oc) { print_error(filename, AVERROR(ENOMEM)); av_exit(1); } if (last_asked_format) { file_oformat = av_guess_format(last_asked_format, NULL, NULL); if (!file_oformat) { fprintf(stderr, "Requested output format '%s' is not a suitable output format\n", last_asked_format); av_exit(1); } last_asked_format = NULL; } else { file_oformat = av_guess_format(NULL, filename, NULL); if (!file_oformat) { fprintf(stderr, "Unable to find a suitable output format for '%s'\n", filename); av_exit(1); } } oc->oformat = file_oformat; av_strlcpy(oc->filename, filename, sizeof(oc->filename)); if (!strcmp(file_oformat->name, "ffm") && av_strstart(filename, "http:", NULL)) { int err = read_ffserver_streams(oc, filename); if (err < 0) { print_error(filename, err); av_exit(1); } } else { use_video = file_oformat->video_codec != CODEC_ID_NONE || video_stream_copy || video_codec_name; use_audio = file_oformat->audio_codec != CODEC_ID_NONE || audio_stream_copy || audio_codec_name; use_subtitle = file_oformat->subtitle_codec != CODEC_ID_NONE || subtitle_stream_copy || subtitle_codec_name; if (nb_input_files > 0) { check_audio_video_sub_inputs(&input_has_video, &input_has_audio, &input_has_subtitle); if (!input_has_video) use_video = 0; if (!input_has_audio) use_audio = 0; if (!input_has_subtitle) use_subtitle = 0; } if (audio_disable) { use_audio = 0; } if (video_disable) { use_video = 0; } if (subtitle_disable) { use_subtitle = 0; } if (use_video) { new_video_stream(oc); } if (use_audio) { new_audio_stream(oc); } if (use_subtitle) { new_subtitle_stream(oc); } oc->timestamp = rec_timestamp; for(; metadata_count>0; metadata_count--){ av_metadata_set(&oc->metadata, metadata[metadata_count-1].key, metadata[metadata_count-1].value); } av_metadata_conv(oc, oc->oformat->metadata_conv, NULL); } output_files[nb_output_files++] = oc; if (oc->oformat->flags & AVFMT_NEEDNUMBER) { if (!av_filename_number_test(oc->filename)) { print_error(oc->filename, AVERROR_NUMEXPECTED); av_exit(1); } } if (!(oc->oformat->flags & AVFMT_NOFILE)) { if (!file_overwrite && (strchr(filename, ':') == NULL || filename[1] == ':' || av_strstart(filename, "file:", NULL))) { if (url_exist(filename)) { if (!using_stdin) { fprintf(stderr,"File '%s' already exists. Overwrite ? [y/N] ", filename); fflush(stderr); if (!read_yesno()) { fprintf(stderr, "Not overwriting - exiting\n"); av_exit(1); } } else { fprintf(stderr,"File '%s' already exists. Exiting.\n", filename); av_exit(1); } } } if (url_fopen(&oc->pb, filename, URL_WRONLY) < 0) { fprintf(stderr, "Could not open '%s'\n", filename); av_exit(1); } } memset(ap, 0, sizeof(*ap)); if (av_set_parameters(oc, ap) < 0) { fprintf(stderr, "%s: Invalid encoding parameters\n", oc->filename); av_exit(1); } oc->preload= (int)(mux_preload*AV_TIME_BASE); oc->max_delay= (int)(mux_max_delay*AV_TIME_BASE); oc->loop_output = loop_output; oc->flags |= AVFMT_FLAG_NONBLOCK; set_context_opts(oc, avformat_opts, AV_OPT_FLAG_ENCODING_PARAM); }
['static void opt_output_file(const char *filename)\n{\n AVFormatContext *oc;\n int use_video, use_audio, use_subtitle;\n int input_has_video, input_has_audio, input_has_subtitle;\n AVFormatParameters params, *ap = &params;\n AVOutputFormat *file_oformat;\n if (!strcmp(filename, "-"))\n filename = "pipe:";\n oc = avformat_alloc_context();\n if (!oc) {\n print_error(filename, AVERROR(ENOMEM));\n av_exit(1);\n }\n if (last_asked_format) {\n file_oformat = av_guess_format(last_asked_format, NULL, NULL);\n if (!file_oformat) {\n fprintf(stderr, "Requested output format \'%s\' is not a suitable output format\\n", last_asked_format);\n av_exit(1);\n }\n last_asked_format = NULL;\n } else {\n file_oformat = av_guess_format(NULL, filename, NULL);\n if (!file_oformat) {\n fprintf(stderr, "Unable to find a suitable output format for \'%s\'\\n",\n filename);\n av_exit(1);\n }\n }\n oc->oformat = file_oformat;\n av_strlcpy(oc->filename, filename, sizeof(oc->filename));\n if (!strcmp(file_oformat->name, "ffm") &&\n av_strstart(filename, "http:", NULL)) {\n int err = read_ffserver_streams(oc, filename);\n if (err < 0) {\n print_error(filename, err);\n av_exit(1);\n }\n } else {\n use_video = file_oformat->video_codec != CODEC_ID_NONE || video_stream_copy || video_codec_name;\n use_audio = file_oformat->audio_codec != CODEC_ID_NONE || audio_stream_copy || audio_codec_name;\n use_subtitle = file_oformat->subtitle_codec != CODEC_ID_NONE || subtitle_stream_copy || subtitle_codec_name;\n if (nb_input_files > 0) {\n check_audio_video_sub_inputs(&input_has_video, &input_has_audio,\n &input_has_subtitle);\n if (!input_has_video)\n use_video = 0;\n if (!input_has_audio)\n use_audio = 0;\n if (!input_has_subtitle)\n use_subtitle = 0;\n }\n if (audio_disable) {\n use_audio = 0;\n }\n if (video_disable) {\n use_video = 0;\n }\n if (subtitle_disable) {\n use_subtitle = 0;\n }\n if (use_video) {\n new_video_stream(oc);\n }\n if (use_audio) {\n new_audio_stream(oc);\n }\n if (use_subtitle) {\n new_subtitle_stream(oc);\n }\n oc->timestamp = rec_timestamp;\n for(; metadata_count>0; metadata_count--){\n av_metadata_set(&oc->metadata, metadata[metadata_count-1].key,\n metadata[metadata_count-1].value);\n }\n av_metadata_conv(oc, oc->oformat->metadata_conv, NULL);\n }\n output_files[nb_output_files++] = oc;\n if (oc->oformat->flags & AVFMT_NEEDNUMBER) {\n if (!av_filename_number_test(oc->filename)) {\n print_error(oc->filename, AVERROR_NUMEXPECTED);\n av_exit(1);\n }\n }\n if (!(oc->oformat->flags & AVFMT_NOFILE)) {\n if (!file_overwrite &&\n (strchr(filename, \':\') == NULL ||\n filename[1] == \':\' ||\n av_strstart(filename, "file:", NULL))) {\n if (url_exist(filename)) {\n if (!using_stdin) {\n fprintf(stderr,"File \'%s\' already exists. Overwrite ? [y/N] ", filename);\n fflush(stderr);\n if (!read_yesno()) {\n fprintf(stderr, "Not overwriting - exiting\\n");\n av_exit(1);\n }\n }\n else {\n fprintf(stderr,"File \'%s\' already exists. Exiting.\\n", filename);\n av_exit(1);\n }\n }\n }\n if (url_fopen(&oc->pb, filename, URL_WRONLY) < 0) {\n fprintf(stderr, "Could not open \'%s\'\\n", filename);\n av_exit(1);\n }\n }\n memset(ap, 0, sizeof(*ap));\n if (av_set_parameters(oc, ap) < 0) {\n fprintf(stderr, "%s: Invalid encoding parameters\\n",\n oc->filename);\n av_exit(1);\n }\n oc->preload= (int)(mux_preload*AV_TIME_BASE);\n oc->max_delay= (int)(mux_max_delay*AV_TIME_BASE);\n oc->loop_output = loop_output;\n oc->flags |= AVFMT_FLAG_NONBLOCK;\n set_context_opts(oc, avformat_opts, AV_OPT_FLAG_ENCODING_PARAM);\n}', 'AVFormatContext *avformat_alloc_context(void)\n{\n AVFormatContext *ic;\n ic = av_malloc(sizeof(AVFormatContext));\n if (!ic) return ic;\n avformat_get_context_defaults(ic);\n ic->av_class = &av_format_context_class;\n return ic;\n}', 'void *av_malloc(unsigned int size)\n{\n void *ptr = NULL;\n#if CONFIG_MEMALIGN_HACK\n long diff;\n#endif\n if(size > (INT_MAX-16) )\n return NULL;\n#if CONFIG_MEMALIGN_HACK\n ptr = malloc(size+16);\n if(!ptr)\n return ptr;\n diff= ((-(long)ptr - 1)&15) + 1;\n ptr = (char*)ptr + diff;\n ((char*)ptr)[-1]= diff;\n#elif HAVE_POSIX_MEMALIGN\n if (posix_memalign(&ptr,16,size))\n ptr = NULL;\n#elif HAVE_MEMALIGN\n ptr = memalign(16,size);\n#else\n ptr = malloc(size);\n#endif\n return ptr;\n}', 'size_t av_strlcpy(char *dst, const char *src, size_t size)\n{\n size_t len = 0;\n while (++len < size && *src)\n *dst++ = *src++;\n if (len <= size)\n *dst = 0;\n return len + strlen(src) - 1;\n}']
823
0
https://github.com/openssl/openssl/blob/2d5d70b15559f9813054ddb11b30b816daf62ebe/apps/speed.c/#L2324
static int do_multi(int multi) { int n; int fd[2]; int *fds; static char sep[] = ":"; fds = malloc(sizeof(*fds) * multi); for (n = 0; n < multi; ++n) { if (pipe(fd) == -1) { BIO_printf(bio_err, "pipe failure\n"); exit(1); } fflush(stdout); (void)BIO_flush(bio_err); if (fork()) { close(fd[1]); fds[n] = fd[0]; } else { close(fd[0]); close(1); if (dup(fd[1]) == -1) { BIO_printf(bio_err, "dup failed\n"); exit(1); } close(fd[1]); mr = 1; usertime = 0; free(fds); return 0; } printf("Forked child %d\n", n); } for (n = 0; n < multi; ++n) { FILE *f; char buf[1024]; char *p; f = fdopen(fds[n], "r"); while (fgets(buf, sizeof buf, f)) { p = strchr(buf, '\n'); if (p) *p = '\0'; if (buf[0] != '+') { BIO_printf(bio_err, "Don't understand line '%s' from child %d\n", buf, n); continue; } printf("Got: %s from %d\n", buf, n); if (strncmp(buf, "+F:", 3) == 0) { int alg; int j; p = buf + 3; alg = atoi(sstrsep(&p, sep)); sstrsep(&p, sep); for (j = 0; j < SIZE_NUM; ++j) results[alg][j] += atof(sstrsep(&p, sep)); } else if (strncmp(buf, "+F2:", 4) == 0) { int k; double d; p = buf + 4; k = atoi(sstrsep(&p, sep)); sstrsep(&p, sep); d = atof(sstrsep(&p, sep)); if (n) rsa_results[k][0] = 1 / (1 / rsa_results[k][0] + 1 / d); else rsa_results[k][0] = d; d = atof(sstrsep(&p, sep)); if (n) rsa_results[k][1] = 1 / (1 / rsa_results[k][1] + 1 / d); else rsa_results[k][1] = d; } # ifndef OPENSSL_NO_DSA else if (strncmp(buf, "+F3:", 4) == 0) { int k; double d; p = buf + 4; k = atoi(sstrsep(&p, sep)); sstrsep(&p, sep); d = atof(sstrsep(&p, sep)); if (n) dsa_results[k][0] = 1 / (1 / dsa_results[k][0] + 1 / d); else dsa_results[k][0] = d; d = atof(sstrsep(&p, sep)); if (n) dsa_results[k][1] = 1 / (1 / dsa_results[k][1] + 1 / d); else dsa_results[k][1] = d; } # endif # ifndef OPENSSL_NO_EC else if (strncmp(buf, "+F4:", 4) == 0) { int k; double d; p = buf + 4; k = atoi(sstrsep(&p, sep)); sstrsep(&p, sep); d = atof(sstrsep(&p, sep)); if (n) ecdsa_results[k][0] = 1 / (1 / ecdsa_results[k][0] + 1 / d); else ecdsa_results[k][0] = d; d = atof(sstrsep(&p, sep)); if (n) ecdsa_results[k][1] = 1 / (1 / ecdsa_results[k][1] + 1 / d); else ecdsa_results[k][1] = d; } # endif # ifndef OPENSSL_NO_EC else if (strncmp(buf, "+F5:", 4) == 0) { int k; double d; p = buf + 4; k = atoi(sstrsep(&p, sep)); sstrsep(&p, sep); d = atof(sstrsep(&p, sep)); if (n) ecdh_results[k][0] = 1 / (1 / ecdh_results[k][0] + 1 / d); else ecdh_results[k][0] = d; } # endif else if (strncmp(buf, "+H:", 3) == 0) { ; } else BIO_printf(bio_err, "Unknown type '%s' from child %d\n", buf, n); } fclose(f); } free(fds); return 1; }
['static int do_multi(int multi)\n{\n int n;\n int fd[2];\n int *fds;\n static char sep[] = ":";\n fds = malloc(sizeof(*fds) * multi);\n for (n = 0; n < multi; ++n) {\n if (pipe(fd) == -1) {\n BIO_printf(bio_err, "pipe failure\\n");\n exit(1);\n }\n fflush(stdout);\n (void)BIO_flush(bio_err);\n if (fork()) {\n close(fd[1]);\n fds[n] = fd[0];\n } else {\n close(fd[0]);\n close(1);\n if (dup(fd[1]) == -1) {\n BIO_printf(bio_err, "dup failed\\n");\n exit(1);\n }\n close(fd[1]);\n mr = 1;\n usertime = 0;\n free(fds);\n return 0;\n }\n printf("Forked child %d\\n", n);\n }\n for (n = 0; n < multi; ++n) {\n FILE *f;\n char buf[1024];\n char *p;\n f = fdopen(fds[n], "r");\n while (fgets(buf, sizeof buf, f)) {\n p = strchr(buf, \'\\n\');\n if (p)\n *p = \'\\0\';\n if (buf[0] != \'+\') {\n BIO_printf(bio_err, "Don\'t understand line \'%s\' from child %d\\n",\n buf, n);\n continue;\n }\n printf("Got: %s from %d\\n", buf, n);\n if (strncmp(buf, "+F:", 3) == 0) {\n int alg;\n int j;\n p = buf + 3;\n alg = atoi(sstrsep(&p, sep));\n sstrsep(&p, sep);\n for (j = 0; j < SIZE_NUM; ++j)\n results[alg][j] += atof(sstrsep(&p, sep));\n } else if (strncmp(buf, "+F2:", 4) == 0) {\n int k;\n double d;\n p = buf + 4;\n k = atoi(sstrsep(&p, sep));\n sstrsep(&p, sep);\n d = atof(sstrsep(&p, sep));\n if (n)\n rsa_results[k][0] = 1 / (1 / rsa_results[k][0] + 1 / d);\n else\n rsa_results[k][0] = d;\n d = atof(sstrsep(&p, sep));\n if (n)\n rsa_results[k][1] = 1 / (1 / rsa_results[k][1] + 1 / d);\n else\n rsa_results[k][1] = d;\n }\n# ifndef OPENSSL_NO_DSA\n else if (strncmp(buf, "+F3:", 4) == 0) {\n int k;\n double d;\n p = buf + 4;\n k = atoi(sstrsep(&p, sep));\n sstrsep(&p, sep);\n d = atof(sstrsep(&p, sep));\n if (n)\n dsa_results[k][0] = 1 / (1 / dsa_results[k][0] + 1 / d);\n else\n dsa_results[k][0] = d;\n d = atof(sstrsep(&p, sep));\n if (n)\n dsa_results[k][1] = 1 / (1 / dsa_results[k][1] + 1 / d);\n else\n dsa_results[k][1] = d;\n }\n# endif\n# ifndef OPENSSL_NO_EC\n else if (strncmp(buf, "+F4:", 4) == 0) {\n int k;\n double d;\n p = buf + 4;\n k = atoi(sstrsep(&p, sep));\n sstrsep(&p, sep);\n d = atof(sstrsep(&p, sep));\n if (n)\n ecdsa_results[k][0] =\n 1 / (1 / ecdsa_results[k][0] + 1 / d);\n else\n ecdsa_results[k][0] = d;\n d = atof(sstrsep(&p, sep));\n if (n)\n ecdsa_results[k][1] =\n 1 / (1 / ecdsa_results[k][1] + 1 / d);\n else\n ecdsa_results[k][1] = d;\n }\n# endif\n# ifndef OPENSSL_NO_EC\n else if (strncmp(buf, "+F5:", 4) == 0) {\n int k;\n double d;\n p = buf + 4;\n k = atoi(sstrsep(&p, sep));\n sstrsep(&p, sep);\n d = atof(sstrsep(&p, sep));\n if (n)\n ecdh_results[k][0] = 1 / (1 / ecdh_results[k][0] + 1 / d);\n else\n ecdh_results[k][0] = d;\n }\n# endif\n else if (strncmp(buf, "+H:", 3) == 0) {\n ;\n } else\n BIO_printf(bio_err, "Unknown type \'%s\' from child %d\\n", buf, n);\n }\n fclose(f);\n }\n free(fds);\n return 1;\n}', 'long BIO_ctrl(BIO *b, int cmd, long larg, void *parg)\n{\n long ret;\n long (*cb) (BIO *, int, const char *, int, long, long);\n if (b == NULL)\n return (0);\n if ((b->method == NULL) || (b->method->ctrl == NULL)) {\n BIOerr(BIO_F_BIO_CTRL, BIO_R_UNSUPPORTED_METHOD);\n return (-2);\n }\n cb = b->callback;\n if ((cb != NULL) &&\n ((ret = cb(b, BIO_CB_CTRL, parg, cmd, larg, 1L)) <= 0))\n return (ret);\n ret = b->method->ctrl(b, cmd, larg, parg);\n if (cb != NULL)\n ret = cb(b, BIO_CB_CTRL | BIO_CB_RETURN, parg, cmd, larg, ret);\n return (ret);\n}']
824
0
https://github.com/openssl/openssl/blob/84cf97af0691290d53c0a51807fa15f0843219ef/ssl/t1_lib.c/#L4183
DH *ssl_get_auto_dh(SSL *s) { int dh_secbits = 80; if (s->cert->dh_tmp_auto == 2) return DH_get_1024_160(); if (s->s3->tmp.new_cipher->algorithm_auth & (SSL_aNULL | SSL_aPSK)) { if (s->s3->tmp.new_cipher->strength_bits == 256) dh_secbits = 128; else dh_secbits = 80; } else { CERT_PKEY *cpk = ssl_get_server_send_pkey(s); dh_secbits = EVP_PKEY_security_bits(cpk->privatekey); } if (dh_secbits >= 128) { DH *dhp = DH_new(); if (!dhp) return NULL; dhp->g = BN_new(); if (dhp->g) BN_set_word(dhp->g, 2); if (dh_secbits >= 192) dhp->p = get_rfc3526_prime_8192(NULL); else dhp->p = get_rfc3526_prime_3072(NULL); if (!dhp->p || !dhp->g) { DH_free(dhp); return NULL; } return dhp; } if (dh_secbits >= 112) return DH_get_2048_224(); return DH_get_1024_160(); }
['DH *ssl_get_auto_dh(SSL *s)\n{\n int dh_secbits = 80;\n if (s->cert->dh_tmp_auto == 2)\n return DH_get_1024_160();\n if (s->s3->tmp.new_cipher->algorithm_auth & (SSL_aNULL | SSL_aPSK)) {\n if (s->s3->tmp.new_cipher->strength_bits == 256)\n dh_secbits = 128;\n else\n dh_secbits = 80;\n } else {\n CERT_PKEY *cpk = ssl_get_server_send_pkey(s);\n dh_secbits = EVP_PKEY_security_bits(cpk->privatekey);\n }\n if (dh_secbits >= 128) {\n DH *dhp = DH_new();\n if (!dhp)\n return NULL;\n dhp->g = BN_new();\n if (dhp->g)\n BN_set_word(dhp->g, 2);\n if (dh_secbits >= 192)\n dhp->p = get_rfc3526_prime_8192(NULL);\n else\n dhp->p = get_rfc3526_prime_3072(NULL);\n if (!dhp->p || !dhp->g) {\n DH_free(dhp);\n return NULL;\n }\n return dhp;\n }\n if (dh_secbits >= 112)\n return DH_get_2048_224();\n return DH_get_1024_160();\n}', 'CERT_PKEY *ssl_get_server_send_pkey(SSL *s)\n{\n CERT *c;\n int i;\n c = s->cert;\n if (!s->s3 || !s->s3->tmp.new_cipher)\n return NULL;\n ssl_set_masks(s, s->s3->tmp.new_cipher);\n#ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL\n if (c->cert_flags & SSL_CERT_FLAG_BROKEN_PROTOCOL)\n return c->key;\n#endif\n i = ssl_get_server_cert_index(s);\n if (i < 0)\n return NULL;\n return &c->pkeys[i];\n}']
825
0
https://github.com/openssl/openssl/blob/8da94770f0a049497b1a52ee469cca1f4a13b1a7/crypto/bn/bn_sqr.c/#L162
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); }
['char *SRP_create_verifier(const char *user, const char *pass, char **salt,\n char **verifier, const char *N, const char *g)\n{\n int len;\n char *result = NULL, *vf = NULL;\n BIGNUM *N_bn = NULL, *g_bn = NULL, *s = NULL, *v = NULL;\n unsigned char tmp[MAX_LEN];\n unsigned char tmp2[MAX_LEN];\n char *defgNid = NULL;\n int vfsize = 0;\n if ((user == NULL) ||\n (pass == NULL) || (salt == NULL) || (verifier == NULL))\n goto err;\n if (N) {\n if ((len = t_fromb64(tmp, N)) == 0)\n goto err;\n N_bn = BN_bin2bn(tmp, len, NULL);\n if ((len = t_fromb64(tmp, g)) == 0)\n goto err;\n g_bn = BN_bin2bn(tmp, len, NULL);\n defgNid = "*";\n } else {\n SRP_gN *gN = SRP_get_gN_by_id(g, NULL);\n if (gN == NULL)\n goto err;\n N_bn = gN->N;\n g_bn = gN->g;\n defgNid = gN->id;\n }\n if (*salt == NULL) {\n if (RAND_bytes(tmp2, SRP_RANDOM_SALT_LEN) <= 0)\n goto err;\n s = BN_bin2bn(tmp2, SRP_RANDOM_SALT_LEN, NULL);\n } else {\n if ((len = t_fromb64(tmp2, *salt)) == 0)\n goto err;\n s = BN_bin2bn(tmp2, len, NULL);\n }\n if (!SRP_create_verifier_BN(user, pass, &s, &v, N_bn, g_bn))\n goto err;\n BN_bn2bin(v, tmp);\n vfsize = BN_num_bytes(v) * 2;\n if (((vf = OPENSSL_malloc(vfsize)) == NULL))\n goto err;\n t_tob64(vf, tmp, BN_num_bytes(v));\n if (*salt == NULL) {\n char *tmp_salt;\n if ((tmp_salt = OPENSSL_malloc(SRP_RANDOM_SALT_LEN * 2)) == NULL) {\n goto err;\n }\n t_tob64(tmp_salt, tmp2, SRP_RANDOM_SALT_LEN);\n *salt = tmp_salt;\n }\n *verifier = vf;\n vf = NULL;\n result = defgNid;\n err:\n if (N) {\n BN_free(N_bn);\n BN_free(g_bn);\n }\n OPENSSL_clear_free(vf, vfsize);\n BN_clear_free(s);\n BN_clear_free(v);\n return result;\n}', 'BIGNUM *BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret)\n{\n unsigned int i, m;\n unsigned int n;\n BN_ULONG l;\n BIGNUM *bn = NULL;\n if (ret == NULL)\n ret = bn = BN_new();\n if (ret == NULL)\n return (NULL);\n bn_check_top(ret);\n for ( ; len > 0 && *s == 0; s++, len--)\n continue;\n n = len;\n if (n == 0) {\n ret->top = 0;\n return (ret);\n }\n i = ((n - 1) / BN_BYTES) + 1;\n m = ((n - 1) % (BN_BYTES));\n if (bn_wexpand(ret, (int)i) == NULL) {\n BN_free(bn);\n return NULL;\n }\n ret->top = i;\n ret->neg = 0;\n l = 0;\n while (n--) {\n l = (l << 8L) | *(s++);\n if (m-- == 0) {\n ret->d[--i] = l;\n l = 0;\n m = BN_BYTES - 1;\n }\n }\n bn_correct_top(ret);\n return (ret);\n}', 'int SRP_create_verifier_BN(const char *user, const char *pass, BIGNUM **salt,\n BIGNUM **verifier, const BIGNUM *N,\n const BIGNUM *g)\n{\n int result = 0;\n BIGNUM *x = NULL;\n BN_CTX *bn_ctx = BN_CTX_new();\n unsigned char tmp2[MAX_LEN];\n BIGNUM *salttmp = NULL;\n if ((user == NULL) ||\n (pass == NULL) ||\n (salt == NULL) ||\n (verifier == NULL) || (N == NULL) || (g == NULL) || (bn_ctx == NULL))\n goto err;\n if (*salt == NULL) {\n if (RAND_bytes(tmp2, SRP_RANDOM_SALT_LEN) <= 0)\n goto err;\n salttmp = BN_bin2bn(tmp2, SRP_RANDOM_SALT_LEN, NULL);\n } else {\n salttmp = *salt;\n }\n x = SRP_Calc_x(salttmp, user, pass);\n *verifier = BN_new();\n if (*verifier == NULL)\n goto err;\n if (!BN_mod_exp(*verifier, g, x, N, bn_ctx)) {\n BN_clear_free(*verifier);\n goto err;\n }\n result = 1;\n *salt = salttmp;\n err:\n if (salt != NULL && *salt != salttmp)\n BN_clear_free(salttmp);\n BN_clear_free(x);\n BN_CTX_free(bn_ctx);\n return result;\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}']
826
1
https://github.com/openssl/openssl/blob/3da2e9c4ee45989a426ff513dc6c6250d1e460de/crypto/bn/bn_shift.c/#L112
int BN_lshift(BIGNUM *r, const BIGNUM *a, int n) { int i, nw, lb, rb; BN_ULONG *t, *f; BN_ULONG l; bn_check_top(r); bn_check_top(a); if (n < 0) { BNerr(BN_F_BN_LSHIFT, BN_R_INVALID_SHIFT); return 0; } nw = n / BN_BITS2; if (bn_wexpand(r, a->top + nw + 1) == NULL) return 0; r->neg = a->neg; lb = n % BN_BITS2; rb = BN_BITS2 - lb; f = a->d; t = r->d; t[a->top + nw] = 0; if (lb == 0) for (i = a->top - 1; i >= 0; i--) t[nw + i] = f[i]; else for (i = a->top - 1; i >= 0; i--) { l = f[i]; t[nw + i + 1] |= (l >> rb) & BN_MASK2; t[nw + i] = (l << lb) & BN_MASK2; } memset(t, 0, sizeof(*t) * nw); r->top = a->top + nw + 1; bn_correct_top(r); bn_check_top(r); return 1; }
['static int file_lshift(STANZA *s)\n{\n BIGNUM *a = NULL, *lshift = NULL, *ret = NULL;\n int n = 0, st = 0;\n if (!TEST_ptr(a = getBN(s, "A"))\n || !TEST_ptr(lshift = getBN(s, "LShift"))\n || !TEST_ptr(ret = BN_new())\n || !getint(s, &n, "N"))\n goto err;\n if (!TEST_true(BN_lshift(ret, a, n))\n || !equalBN("A << N", lshift, ret)\n || !TEST_true(BN_rshift(ret, lshift, n))\n || !equalBN("A >> N", a, ret))\n goto err;\n st = 1;\nerr:\n BN_free(a);\n BN_free(lshift);\n BN_free(ret);\n return st;\n}', 'static int getint(STANZA *s, int *out, const char *attribute)\n{\n BIGNUM *ret;\n BN_ULONG word;\n int st = 0;\n if (!TEST_ptr(ret = getBN(s, attribute))\n || !TEST_ulong_le(word = BN_get_word(ret), INT_MAX))\n goto err;\n *out = (int)word;\n st = 1;\nerr:\n BN_free(ret);\n return st;\n}', 'int BN_lshift(BIGNUM *r, const BIGNUM *a, int n)\n{\n int i, nw, lb, rb;\n BN_ULONG *t, *f;\n BN_ULONG l;\n bn_check_top(r);\n bn_check_top(a);\n if (n < 0) {\n BNerr(BN_F_BN_LSHIFT, BN_R_INVALID_SHIFT);\n return 0;\n }\n nw = n / BN_BITS2;\n if (bn_wexpand(r, a->top + nw + 1) == NULL)\n return 0;\n r->neg = a->neg;\n lb = n % BN_BITS2;\n rb = BN_BITS2 - lb;\n f = a->d;\n t = r->d;\n t[a->top + nw] = 0;\n if (lb == 0)\n for (i = a->top - 1; i >= 0; i--)\n t[nw + i] = f[i];\n else\n for (i = a->top - 1; i >= 0; i--) {\n l = f[i];\n t[nw + i + 1] |= (l >> rb) & BN_MASK2;\n t[nw + i] = (l << lb) & BN_MASK2;\n }\n memset(t, 0, sizeof(*t) * nw);\n r->top = a->top + nw + 1;\n bn_correct_top(r);\n bn_check_top(r);\n return 1;\n}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n return (words <= a->dmax) ? a : bn_expand2(a, words);\n}']
827
0
https://github.com/openssl/openssl/blob/1a50eedf2a1fbb1e0e009ad616d8be678e4c6340/crypto/ec/ec_mult.c/#L618
int ec_wNAF_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar, size_t num, const EC_POINT *points[], const BIGNUM *scalars[], BN_CTX *ctx) { const EC_POINT *generator = NULL; EC_POINT *tmp = NULL; size_t totalnum; size_t blocksize = 0, numblocks = 0; size_t pre_points_per_block = 0; size_t i, j; int k; int r_is_inverted = 0; int r_is_at_infinity = 1; size_t *wsize = NULL; signed char **wNAF = NULL; size_t *wNAF_len = NULL; size_t max_len = 0; size_t num_val; EC_POINT **val = NULL; EC_POINT **v; EC_POINT ***val_sub = NULL; const EC_PRE_COMP *pre_comp = NULL; int num_scalar = 0; int ret = 0; if (!BN_is_zero(group->order) && !BN_is_zero(group->cofactor)) { if ((scalar != NULL) && (num == 0)) { return ec_scalar_mul_ladder(group, r, scalar, NULL, ctx); } if ((scalar == NULL) && (num == 1)) { return ec_scalar_mul_ladder(group, r, scalars[0], points[0], ctx); } } if (scalar != NULL) { generator = EC_GROUP_get0_generator(group); if (generator == NULL) { ECerr(EC_F_EC_WNAF_MUL, EC_R_UNDEFINED_GENERATOR); goto err; } pre_comp = group->pre_comp.ec; if (pre_comp && pre_comp->numblocks && (EC_POINT_cmp(group, generator, pre_comp->points[0], ctx) == 0)) { blocksize = pre_comp->blocksize; numblocks = (BN_num_bits(scalar) / blocksize) + 1; if (numblocks > pre_comp->numblocks) numblocks = pre_comp->numblocks; pre_points_per_block = (size_t)1 << (pre_comp->w - 1); if (pre_comp->num != (pre_comp->numblocks * pre_points_per_block)) { ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR); goto err; } } else { pre_comp = NULL; numblocks = 1; num_scalar = 1; } } totalnum = num + numblocks; wsize = OPENSSL_malloc(totalnum * sizeof(wsize[0])); wNAF_len = OPENSSL_malloc(totalnum * sizeof(wNAF_len[0])); wNAF = OPENSSL_malloc((totalnum + 1) * sizeof(wNAF[0])); val_sub = OPENSSL_malloc(totalnum * sizeof(val_sub[0])); if (wNAF != NULL) wNAF[0] = NULL; if (wsize == NULL || wNAF_len == NULL || wNAF == NULL || val_sub == NULL) { ECerr(EC_F_EC_WNAF_MUL, ERR_R_MALLOC_FAILURE); goto err; } num_val = 0; for (i = 0; i < num + num_scalar; i++) { size_t bits; bits = i < num ? BN_num_bits(scalars[i]) : BN_num_bits(scalar); wsize[i] = EC_window_bits_for_scalar_size(bits); num_val += (size_t)1 << (wsize[i] - 1); wNAF[i + 1] = NULL; wNAF[i] = bn_compute_wNAF((i < num ? scalars[i] : scalar), wsize[i], &wNAF_len[i]); if (wNAF[i] == NULL) goto err; if (wNAF_len[i] > max_len) max_len = wNAF_len[i]; } if (numblocks) { if (pre_comp == NULL) { if (num_scalar != 1) { ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR); goto err; } } else { signed char *tmp_wNAF = NULL; size_t tmp_len = 0; if (num_scalar != 0) { ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR); goto err; } wsize[num] = pre_comp->w; tmp_wNAF = bn_compute_wNAF(scalar, wsize[num], &tmp_len); if (!tmp_wNAF) goto err; if (tmp_len <= max_len) { numblocks = 1; totalnum = num + 1; wNAF[num] = tmp_wNAF; wNAF[num + 1] = NULL; wNAF_len[num] = tmp_len; val_sub[num] = pre_comp->points; } else { signed char *pp; EC_POINT **tmp_points; if (tmp_len < numblocks * blocksize) { numblocks = (tmp_len + blocksize - 1) / blocksize; if (numblocks > pre_comp->numblocks) { ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR); OPENSSL_free(tmp_wNAF); goto err; } totalnum = num + numblocks; } pp = tmp_wNAF; tmp_points = pre_comp->points; for (i = num; i < totalnum; i++) { if (i < totalnum - 1) { wNAF_len[i] = blocksize; if (tmp_len < blocksize) { ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR); OPENSSL_free(tmp_wNAF); goto err; } tmp_len -= blocksize; } else wNAF_len[i] = tmp_len; wNAF[i + 1] = NULL; wNAF[i] = OPENSSL_malloc(wNAF_len[i]); if (wNAF[i] == NULL) { ECerr(EC_F_EC_WNAF_MUL, ERR_R_MALLOC_FAILURE); OPENSSL_free(tmp_wNAF); goto err; } memcpy(wNAF[i], pp, wNAF_len[i]); if (wNAF_len[i] > max_len) max_len = wNAF_len[i]; if (*tmp_points == NULL) { ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR); OPENSSL_free(tmp_wNAF); goto err; } val_sub[i] = tmp_points; tmp_points += pre_points_per_block; pp += blocksize; } OPENSSL_free(tmp_wNAF); } } } val = OPENSSL_malloc((num_val + 1) * sizeof(val[0])); if (val == NULL) { ECerr(EC_F_EC_WNAF_MUL, ERR_R_MALLOC_FAILURE); goto err; } val[num_val] = NULL; v = val; for (i = 0; i < num + num_scalar; i++) { val_sub[i] = v; for (j = 0; j < ((size_t)1 << (wsize[i] - 1)); j++) { *v = EC_POINT_new(group); if (*v == NULL) goto err; v++; } } if (!(v == val + num_val)) { ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR); goto err; } if ((tmp = EC_POINT_new(group)) == NULL) goto err; for (i = 0; i < num + num_scalar; i++) { if (i < num) { if (!EC_POINT_copy(val_sub[i][0], points[i])) goto err; } else { if (!EC_POINT_copy(val_sub[i][0], generator)) goto err; } if (wsize[i] > 1) { if (!EC_POINT_dbl(group, tmp, val_sub[i][0], ctx)) goto err; for (j = 1; j < ((size_t)1 << (wsize[i] - 1)); j++) { if (!EC_POINT_add (group, val_sub[i][j], val_sub[i][j - 1], tmp, ctx)) goto err; } } } if (!EC_POINTs_make_affine(group, num_val, val, ctx)) goto err; r_is_at_infinity = 1; for (k = max_len - 1; k >= 0; k--) { if (!r_is_at_infinity) { if (!EC_POINT_dbl(group, r, r, ctx)) goto err; } for (i = 0; i < totalnum; i++) { if (wNAF_len[i] > (size_t)k) { int digit = wNAF[i][k]; int is_neg; if (digit) { is_neg = digit < 0; if (is_neg) digit = -digit; if (is_neg != r_is_inverted) { if (!r_is_at_infinity) { if (!EC_POINT_invert(group, r, ctx)) goto err; } r_is_inverted = !r_is_inverted; } if (r_is_at_infinity) { if (!EC_POINT_copy(r, val_sub[i][digit >> 1])) goto err; r_is_at_infinity = 0; } else { if (!EC_POINT_add (group, r, r, val_sub[i][digit >> 1], ctx)) goto err; } } } } } if (r_is_at_infinity) { if (!EC_POINT_set_to_infinity(group, r)) goto err; } else { if (r_is_inverted) if (!EC_POINT_invert(group, r, ctx)) goto err; } ret = 1; err: EC_POINT_free(tmp); OPENSSL_free(wsize); OPENSSL_free(wNAF_len); if (wNAF != NULL) { signed char **w; for (w = wNAF; *w != NULL; w++) OPENSSL_free(*w); OPENSSL_free(wNAF); } if (val != NULL) { for (v = val; *v != NULL; v++) EC_POINT_clear_free(*v); OPENSSL_free(val); } OPENSSL_free(val_sub); return ret; }
['static int group_order_tests(EC_GROUP *group)\n{\n BIGNUM *n1 = NULL, *n2 = NULL, *order = NULL;\n EC_POINT *P = NULL, *Q = NULL, *R = NULL, *S = NULL;\n const EC_POINT *G = NULL;\n BN_CTX *ctx = NULL;\n int i = 0, r = 0;\n if (!TEST_ptr(n1 = BN_new())\n || !TEST_ptr(n2 = BN_new())\n || !TEST_ptr(order = BN_new())\n || !TEST_ptr(ctx = BN_CTX_new())\n || !TEST_ptr(G = EC_GROUP_get0_generator(group))\n || !TEST_ptr(P = EC_POINT_new(group))\n || !TEST_ptr(Q = EC_POINT_new(group))\n || !TEST_ptr(R = EC_POINT_new(group))\n || !TEST_ptr(S = EC_POINT_new(group)))\n goto err;\n if (!TEST_true(EC_GROUP_get_order(group, order, ctx))\n || !TEST_true(EC_POINT_mul(group, Q, order, NULL, NULL, ctx))\n || !TEST_true(EC_POINT_is_at_infinity(group, Q))\n || !TEST_true(EC_GROUP_precompute_mult(group, ctx))\n || !TEST_true(EC_POINT_mul(group, Q, order, NULL, NULL, ctx))\n || !TEST_true(EC_POINT_is_at_infinity(group, Q))\n || !TEST_true(EC_POINT_copy(P, G))\n || !TEST_true(BN_one(n1))\n || !TEST_true(EC_POINT_mul(group, Q, n1, NULL, NULL, ctx))\n || !TEST_int_eq(0, EC_POINT_cmp(group, Q, P, ctx))\n || !TEST_true(BN_sub(n1, order, n1))\n || !TEST_true(EC_POINT_mul(group, Q, n1, NULL, NULL, ctx))\n || !TEST_true(EC_POINT_invert(group, Q, ctx))\n || !TEST_int_eq(0, EC_POINT_cmp(group, Q, P, ctx)))\n goto err;\n for (i = 1; i <= 2; i++) {\n const BIGNUM *scalars[6];\n const EC_POINT *points[6];\n if (!TEST_true(BN_set_word(n1, i))\n || !TEST_true(EC_POINT_mul(group, P, n1, NULL, NULL, ctx))\n || (i == 1 && !TEST_int_eq(0, EC_POINT_cmp(group, P, G, ctx)))\n || !TEST_true(BN_one(n1))\n || !TEST_true(BN_sub(n1, n1, order))\n || !TEST_true(EC_POINT_mul(group, Q, NULL, P, n1, ctx))\n || !TEST_int_eq(0, EC_POINT_cmp(group, Q, P, ctx))\n || !TEST_true(BN_add(n2, order, BN_value_one()))\n || !TEST_true(EC_POINT_mul(group, Q, NULL, P, n2, ctx))\n || !TEST_int_eq(0, EC_POINT_cmp(group, Q, P, ctx))\n || !TEST_true(BN_mul(n2, n1, n2, ctx))\n || !TEST_true(EC_POINT_mul(group, Q, NULL, P, n2, ctx))\n || !TEST_int_eq(0, EC_POINT_cmp(group, Q, P, ctx)))\n goto err;\n BN_set_negative(n2, 0);\n if (!TEST_true(EC_POINT_mul(group, Q, NULL, P, n2, ctx))\n || !TEST_true(EC_POINT_add(group, Q, Q, P, ctx))\n || !TEST_true(EC_POINT_is_at_infinity(group, Q))\n || !TEST_false(EC_POINT_is_at_infinity(group, P)))\n goto err;\n scalars[0] = scalars[1] = BN_value_one();\n points[0] = points[1] = P;\n if (!TEST_true(EC_POINTs_mul(group, R, NULL, 2, points, scalars, ctx))\n || !TEST_true(EC_POINT_dbl(group, S, points[0], ctx))\n || !TEST_int_eq(0, EC_POINT_cmp(group, R, S, ctx)))\n goto err;\n scalars[0] = n1;\n points[0] = Q;\n scalars[1] = n2;\n points[1] = P;\n scalars[2] = n1;\n points[2] = Q;\n scalars[3] = n2;\n points[3] = Q;\n scalars[4] = n1;\n points[4] = P;\n scalars[5] = n2;\n points[5] = Q;\n if (!TEST_true(EC_POINTs_mul(group, P, NULL, 6, points, scalars, ctx))\n || !TEST_true(EC_POINT_is_at_infinity(group, P)))\n goto err;\n }\n r = 1;\nerr:\n if (r == 0 && i != 0)\n TEST_info(i == 1 ? "allowing precomputation" :\n "without precomputation");\n EC_POINT_free(P);\n EC_POINT_free(Q);\n EC_POINT_free(R);\n EC_POINT_free(S);\n BN_free(n1);\n BN_free(n2);\n BN_free(order);\n BN_CTX_free(ctx);\n return r;\n}', 'int EC_POINTs_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,\n size_t num, const EC_POINT *points[],\n const BIGNUM *scalars[], BN_CTX *ctx)\n{\n int ret = 0;\n size_t i = 0;\n BN_CTX *new_ctx = NULL;\n if ((scalar == NULL) && (num == 0)) {\n return EC_POINT_set_to_infinity(group, r);\n }\n if (!ec_point_is_compat(r, group)) {\n ECerr(EC_F_EC_POINTS_MUL, EC_R_INCOMPATIBLE_OBJECTS);\n return 0;\n }\n for (i = 0; i < num; i++) {\n if (!ec_point_is_compat(points[i], group)) {\n ECerr(EC_F_EC_POINTS_MUL, EC_R_INCOMPATIBLE_OBJECTS);\n return 0;\n }\n }\n if (ctx == NULL && (ctx = new_ctx = BN_CTX_secure_new()) == NULL) {\n ECerr(EC_F_EC_POINTS_MUL, ERR_R_INTERNAL_ERROR);\n return 0;\n }\n if (group->meth->mul != NULL)\n ret = group->meth->mul(group, r, scalar, num, points, scalars, ctx);\n else\n ret = ec_wNAF_mul(group, r, scalar, num, points, scalars, ctx);\n BN_CTX_free(new_ctx);\n return ret;\n}', 'int ec_wNAF_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,\n size_t num, const EC_POINT *points[], const BIGNUM *scalars[],\n BN_CTX *ctx)\n{\n const EC_POINT *generator = NULL;\n EC_POINT *tmp = NULL;\n size_t totalnum;\n size_t blocksize = 0, numblocks = 0;\n size_t pre_points_per_block = 0;\n size_t i, j;\n int k;\n int r_is_inverted = 0;\n int r_is_at_infinity = 1;\n size_t *wsize = NULL;\n signed char **wNAF = NULL;\n size_t *wNAF_len = NULL;\n size_t max_len = 0;\n size_t num_val;\n EC_POINT **val = NULL;\n EC_POINT **v;\n EC_POINT ***val_sub = NULL;\n const EC_PRE_COMP *pre_comp = NULL;\n int num_scalar = 0;\n int ret = 0;\n if (!BN_is_zero(group->order) && !BN_is_zero(group->cofactor)) {\n if ((scalar != NULL) && (num == 0)) {\n return ec_scalar_mul_ladder(group, r, scalar, NULL, ctx);\n }\n if ((scalar == NULL) && (num == 1)) {\n return ec_scalar_mul_ladder(group, r, scalars[0], points[0], ctx);\n }\n }\n if (scalar != NULL) {\n generator = EC_GROUP_get0_generator(group);\n if (generator == NULL) {\n ECerr(EC_F_EC_WNAF_MUL, EC_R_UNDEFINED_GENERATOR);\n goto err;\n }\n pre_comp = group->pre_comp.ec;\n if (pre_comp && pre_comp->numblocks\n && (EC_POINT_cmp(group, generator, pre_comp->points[0], ctx) ==\n 0)) {\n blocksize = pre_comp->blocksize;\n numblocks = (BN_num_bits(scalar) / blocksize) + 1;\n if (numblocks > pre_comp->numblocks)\n numblocks = pre_comp->numblocks;\n pre_points_per_block = (size_t)1 << (pre_comp->w - 1);\n if (pre_comp->num != (pre_comp->numblocks * pre_points_per_block)) {\n ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);\n goto err;\n }\n } else {\n pre_comp = NULL;\n numblocks = 1;\n num_scalar = 1;\n }\n }\n totalnum = num + numblocks;\n wsize = OPENSSL_malloc(totalnum * sizeof(wsize[0]));\n wNAF_len = OPENSSL_malloc(totalnum * sizeof(wNAF_len[0]));\n wNAF = OPENSSL_malloc((totalnum + 1) * sizeof(wNAF[0]));\n val_sub = OPENSSL_malloc(totalnum * sizeof(val_sub[0]));\n if (wNAF != NULL)\n wNAF[0] = NULL;\n if (wsize == NULL || wNAF_len == NULL || wNAF == NULL || val_sub == NULL) {\n ECerr(EC_F_EC_WNAF_MUL, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n num_val = 0;\n for (i = 0; i < num + num_scalar; i++) {\n size_t bits;\n bits = i < num ? BN_num_bits(scalars[i]) : BN_num_bits(scalar);\n wsize[i] = EC_window_bits_for_scalar_size(bits);\n num_val += (size_t)1 << (wsize[i] - 1);\n wNAF[i + 1] = NULL;\n wNAF[i] =\n bn_compute_wNAF((i < num ? scalars[i] : scalar), wsize[i],\n &wNAF_len[i]);\n if (wNAF[i] == NULL)\n goto err;\n if (wNAF_len[i] > max_len)\n max_len = wNAF_len[i];\n }\n if (numblocks) {\n if (pre_comp == NULL) {\n if (num_scalar != 1) {\n ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);\n goto err;\n }\n } else {\n signed char *tmp_wNAF = NULL;\n size_t tmp_len = 0;\n if (num_scalar != 0) {\n ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);\n goto err;\n }\n wsize[num] = pre_comp->w;\n tmp_wNAF = bn_compute_wNAF(scalar, wsize[num], &tmp_len);\n if (!tmp_wNAF)\n goto err;\n if (tmp_len <= max_len) {\n numblocks = 1;\n totalnum = num + 1;\n wNAF[num] = tmp_wNAF;\n wNAF[num + 1] = NULL;\n wNAF_len[num] = tmp_len;\n val_sub[num] = pre_comp->points;\n } else {\n signed char *pp;\n EC_POINT **tmp_points;\n if (tmp_len < numblocks * blocksize) {\n numblocks = (tmp_len + blocksize - 1) / blocksize;\n if (numblocks > pre_comp->numblocks) {\n ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);\n OPENSSL_free(tmp_wNAF);\n goto err;\n }\n totalnum = num + numblocks;\n }\n pp = tmp_wNAF;\n tmp_points = pre_comp->points;\n for (i = num; i < totalnum; i++) {\n if (i < totalnum - 1) {\n wNAF_len[i] = blocksize;\n if (tmp_len < blocksize) {\n ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);\n OPENSSL_free(tmp_wNAF);\n goto err;\n }\n tmp_len -= blocksize;\n } else\n wNAF_len[i] = tmp_len;\n wNAF[i + 1] = NULL;\n wNAF[i] = OPENSSL_malloc(wNAF_len[i]);\n if (wNAF[i] == NULL) {\n ECerr(EC_F_EC_WNAF_MUL, ERR_R_MALLOC_FAILURE);\n OPENSSL_free(tmp_wNAF);\n goto err;\n }\n memcpy(wNAF[i], pp, wNAF_len[i]);\n if (wNAF_len[i] > max_len)\n max_len = wNAF_len[i];\n if (*tmp_points == NULL) {\n ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);\n OPENSSL_free(tmp_wNAF);\n goto err;\n }\n val_sub[i] = tmp_points;\n tmp_points += pre_points_per_block;\n pp += blocksize;\n }\n OPENSSL_free(tmp_wNAF);\n }\n }\n }\n val = OPENSSL_malloc((num_val + 1) * sizeof(val[0]));\n if (val == NULL) {\n ECerr(EC_F_EC_WNAF_MUL, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n val[num_val] = NULL;\n v = val;\n for (i = 0; i < num + num_scalar; i++) {\n val_sub[i] = v;\n for (j = 0; j < ((size_t)1 << (wsize[i] - 1)); j++) {\n *v = EC_POINT_new(group);\n if (*v == NULL)\n goto err;\n v++;\n }\n }\n if (!(v == val + num_val)) {\n ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);\n goto err;\n }\n if ((tmp = EC_POINT_new(group)) == NULL)\n goto err;\n for (i = 0; i < num + num_scalar; i++) {\n if (i < num) {\n if (!EC_POINT_copy(val_sub[i][0], points[i]))\n goto err;\n } else {\n if (!EC_POINT_copy(val_sub[i][0], generator))\n goto err;\n }\n if (wsize[i] > 1) {\n if (!EC_POINT_dbl(group, tmp, val_sub[i][0], ctx))\n goto err;\n for (j = 1; j < ((size_t)1 << (wsize[i] - 1)); j++) {\n if (!EC_POINT_add\n (group, val_sub[i][j], val_sub[i][j - 1], tmp, ctx))\n goto err;\n }\n }\n }\n if (!EC_POINTs_make_affine(group, num_val, val, ctx))\n goto err;\n r_is_at_infinity = 1;\n for (k = max_len - 1; k >= 0; k--) {\n if (!r_is_at_infinity) {\n if (!EC_POINT_dbl(group, r, r, ctx))\n goto err;\n }\n for (i = 0; i < totalnum; i++) {\n if (wNAF_len[i] > (size_t)k) {\n int digit = wNAF[i][k];\n int is_neg;\n if (digit) {\n is_neg = digit < 0;\n if (is_neg)\n digit = -digit;\n if (is_neg != r_is_inverted) {\n if (!r_is_at_infinity) {\n if (!EC_POINT_invert(group, r, ctx))\n goto err;\n }\n r_is_inverted = !r_is_inverted;\n }\n if (r_is_at_infinity) {\n if (!EC_POINT_copy(r, val_sub[i][digit >> 1]))\n goto err;\n r_is_at_infinity = 0;\n } else {\n if (!EC_POINT_add\n (group, r, r, val_sub[i][digit >> 1], ctx))\n goto err;\n }\n }\n }\n }\n }\n if (r_is_at_infinity) {\n if (!EC_POINT_set_to_infinity(group, r))\n goto err;\n } else {\n if (r_is_inverted)\n if (!EC_POINT_invert(group, r, ctx))\n goto err;\n }\n ret = 1;\n err:\n EC_POINT_free(tmp);\n OPENSSL_free(wsize);\n OPENSSL_free(wNAF_len);\n if (wNAF != NULL) {\n signed char **w;\n for (w = wNAF; *w != NULL; w++)\n OPENSSL_free(*w);\n OPENSSL_free(wNAF);\n }\n if (val != NULL) {\n for (v = val; *v != NULL; v++)\n EC_POINT_clear_free(*v);\n OPENSSL_free(val);\n }\n OPENSSL_free(val_sub);\n return ret;\n}', 'void *CRYPTO_malloc(size_t num, const char *file, int line)\n{\n void *ret = NULL;\n INCREMENT(malloc_count);\n if (malloc_impl != NULL && malloc_impl != CRYPTO_malloc)\n return malloc_impl(num, file, line);\n if (num == 0)\n return NULL;\n FAILTEST();\n if (allow_customize) {\n allow_customize = 0;\n }\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n if (call_malloc_debug) {\n CRYPTO_mem_debug_malloc(NULL, num, 0, file, line);\n ret = malloc(num);\n CRYPTO_mem_debug_malloc(ret, num, 1, file, line);\n } else {\n ret = malloc(num);\n }\n#else\n (void)(file); (void)(line);\n ret = malloc(num);\n#endif\n return ret;\n}']
828
0
https://github.com/openssl/openssl/blob/ecbe07817ab8fff2aca97eeb69fabdd5c54b4bda/crypto/x509/x509_vfy.c/#L384
static int check_chain_purpose(X509_STORE_CTX *ctx) { #ifdef NO_CHAIN_VERIFY return 1; #else int i, ok=0; X509 *x; int (*cb)(); cb=ctx->verify_cb; if (cb == NULL) cb=null_callback; for (i = 0; i < ctx->last_untrusted; i++) { x = sk_X509_value(ctx->chain, i); if (!X509_check_purpose(x, ctx->purpose, i)) { if (i) ctx->error = X509_V_ERR_INVALID_CA; else ctx->error = X509_V_ERR_INVALID_PURPOSE; ctx->error_depth = i; ctx->current_cert = x; ok=cb(0,ctx); if (!ok) goto end; } if ((i > 1) && (x->ex_pathlen != -1) && (i > (x->ex_pathlen + 1))) { ctx->error = X509_V_ERR_PATH_LENGTH_EXCEEDED; ctx->error_depth = i; ctx->current_cert = x; ok=cb(0,ctx); if (!ok) goto end; } } ok = 1; end: return ok; #endif }
['static int check_chain_purpose(X509_STORE_CTX *ctx)\n{\n#ifdef NO_CHAIN_VERIFY\n\treturn 1;\n#else\n\tint i, ok=0;\n\tX509 *x;\n\tint (*cb)();\n\tcb=ctx->verify_cb;\n\tif (cb == NULL) cb=null_callback;\n\tfor (i = 0; i < ctx->last_untrusted; i++)\n\t\t{\n\t\tx = sk_X509_value(ctx->chain, i);\n\t\tif (!X509_check_purpose(x, ctx->purpose, i))\n\t\t\t{\n\t\t\tif (i)\n\t\t\t\tctx->error = X509_V_ERR_INVALID_CA;\n\t\t\telse\n\t\t\t\tctx->error = X509_V_ERR_INVALID_PURPOSE;\n\t\t\tctx->error_depth = i;\n\t\t\tctx->current_cert = x;\n\t\t\tok=cb(0,ctx);\n\t\t\tif (!ok) goto end;\n\t\t\t}\n\t\tif ((i > 1) && (x->ex_pathlen != -1)\n\t\t\t && (i > (x->ex_pathlen + 1)))\n\t\t\t{\n\t\t\tctx->error = X509_V_ERR_PATH_LENGTH_EXCEEDED;\n\t\t\tctx->error_depth = i;\n\t\t\tctx->current_cert = x;\n\t\t\tok=cb(0,ctx);\n\t\t\tif (!ok) goto end;\n\t\t\t}\n\t\t}\n\tok = 1;\n end:\n\treturn ok;\n#endif\n}', 'char *sk_value(const STACK *st, int i)\n{\n\tif(st == NULL) return NULL;\n\treturn st->data[i];\n}', 'int X509_check_purpose(X509 *x, int id, int ca)\n{\n\tint idx;\n\tconst X509_PURPOSE *pt;\n\tif(!(x->ex_flags & EXFLAG_SET)) {\n\t\tCRYPTO_w_lock(CRYPTO_LOCK_X509);\n\t\tx509v3_cache_extensions(x);\n\t\tCRYPTO_w_unlock(CRYPTO_LOCK_X509);\n\t}\n\tif(id == -1) return 1;\n\tidx = X509_PURPOSE_get_by_id(id);\n\tif(idx == -1) return -1;\n\tpt = X509_PURPOSE_get0(idx);\n\treturn pt->check_purpose(pt, x, ca);\n}']
829
0
https://github.com/libav/libav/blob/47399ccdfd93d337c96c76fbf591f0e3637131ef/libavcodec/bitstream.h/#L236
static inline void skip_remaining(BitstreamContext *bc, unsigned n) { #ifdef BITSTREAM_READER_LE bc->bits >>= n; #else bc->bits <<= n; #endif bc->bits_left -= n; }
['static av_cold int mpc7_decode_init(AVCodecContext * avctx)\n{\n int i, j;\n MPCContext *c = avctx->priv_data;\n BitstreamContext bc;\n LOCAL_ALIGNED_16(uint8_t, buf, [16]);\n static int vlc_initialized = 0;\n static VLC_TYPE scfi_table[1 << MPC7_SCFI_BITS][2];\n static VLC_TYPE dscf_table[1 << MPC7_DSCF_BITS][2];\n static VLC_TYPE hdr_table[1 << MPC7_HDR_BITS][2];\n static VLC_TYPE quant_tables[7224][2];\n if (avctx->channels != 2) {\n avpriv_request_sample(avctx, "%d channels", avctx->channels);\n return AVERROR_PATCHWELCOME;\n }\n if(avctx->extradata_size < 16){\n av_log(avctx, AV_LOG_ERROR, "Too small extradata size (%i)!\\n", avctx->extradata_size);\n return -1;\n }\n memset(c->oldDSCF, 0, sizeof(c->oldDSCF));\n av_lfg_init(&c->rnd, 0xDEADBEEF);\n ff_bswapdsp_init(&c->bdsp);\n ff_mpadsp_init(&c->mpadsp);\n c->bdsp.bswap_buf((uint32_t *) buf, (const uint32_t *) avctx->extradata, 4);\n ff_mpc_init();\n bitstream_init(&bc, buf, 128);\n c->IS = bitstream_read_bit(&bc);\n c->MSS = bitstream_read_bit(&bc);\n c->maxbands = bitstream_read(&bc, 6);\n if(c->maxbands >= BANDS){\n av_log(avctx, AV_LOG_ERROR, "Too many bands: %i\\n", c->maxbands);\n return -1;\n }\n bitstream_skip(&bc, 88);\n c->gapless = bitstream_read_bit(&bc);\n c->lastframelen = bitstream_read(&bc, 11);\n av_log(avctx, AV_LOG_DEBUG, "IS: %d, MSS: %d, TG: %d, LFL: %d, bands: %d\\n",\n c->IS, c->MSS, c->gapless, c->lastframelen, c->maxbands);\n c->frames_to_skip = 0;\n avctx->sample_fmt = AV_SAMPLE_FMT_S16P;\n avctx->channel_layout = AV_CH_LAYOUT_STEREO;\n if(vlc_initialized) return 0;\n av_log(avctx, AV_LOG_DEBUG, "Initing VLC\\n");\n scfi_vlc.table = scfi_table;\n scfi_vlc.table_allocated = 1 << MPC7_SCFI_BITS;\n if(init_vlc(&scfi_vlc, MPC7_SCFI_BITS, MPC7_SCFI_SIZE,\n &mpc7_scfi[1], 2, 1,\n &mpc7_scfi[0], 2, 1, INIT_VLC_USE_NEW_STATIC)){\n av_log(avctx, AV_LOG_ERROR, "Cannot init SCFI VLC\\n");\n return -1;\n }\n dscf_vlc.table = dscf_table;\n dscf_vlc.table_allocated = 1 << MPC7_DSCF_BITS;\n if(init_vlc(&dscf_vlc, MPC7_DSCF_BITS, MPC7_DSCF_SIZE,\n &mpc7_dscf[1], 2, 1,\n &mpc7_dscf[0], 2, 1, INIT_VLC_USE_NEW_STATIC)){\n av_log(avctx, AV_LOG_ERROR, "Cannot init DSCF VLC\\n");\n return -1;\n }\n hdr_vlc.table = hdr_table;\n hdr_vlc.table_allocated = 1 << MPC7_HDR_BITS;\n if(init_vlc(&hdr_vlc, MPC7_HDR_BITS, MPC7_HDR_SIZE,\n &mpc7_hdr[1], 2, 1,\n &mpc7_hdr[0], 2, 1, INIT_VLC_USE_NEW_STATIC)){\n av_log(avctx, AV_LOG_ERROR, "Cannot init HDR VLC\\n");\n return -1;\n }\n for(i = 0; i < MPC7_QUANT_VLC_TABLES; i++){\n for(j = 0; j < 2; j++){\n quant_vlc[i][j].table = &quant_tables[quant_offsets[i*2 + j]];\n quant_vlc[i][j].table_allocated = quant_offsets[i*2 + j + 1] - quant_offsets[i*2 + j];\n if(init_vlc(&quant_vlc[i][j], 9, mpc7_quant_vlc_sizes[i],\n &mpc7_quant_vlc[i][j][1], 4, 2,\n &mpc7_quant_vlc[i][j][0], 4, 2, INIT_VLC_USE_NEW_STATIC)){\n av_log(avctx, AV_LOG_ERROR, "Cannot init QUANT VLC %i,%i\\n",i,j);\n return -1;\n }\n }\n }\n vlc_initialized = 1;\n return 0;\n}', 'static inline uint32_t bitstream_read(BitstreamContext *bc, unsigned n)\n{\n if (!n)\n return 0;\n if (n > bc->bits_left) {\n refill_32(bc);\n if (bc->bits_left < 32)\n bc->bits_left = n;\n }\n return get_val(bc, n);\n}', 'static inline void bitstream_skip(BitstreamContext *bc, unsigned n)\n{\n if (n < bc->bits_left)\n skip_remaining(bc, n);\n else {\n n -= bc->bits_left;\n bc->bits = 0;\n bc->bits_left = 0;\n if (n >= 64) {\n unsigned skip = n / 8;\n n -= skip * 8;\n bc->ptr += skip;\n }\n refill_64(bc);\n if (n)\n skip_remaining(bc, n);\n }\n}', 'static inline void skip_remaining(BitstreamContext *bc, unsigned n)\n{\n#ifdef BITSTREAM_READER_LE\n bc->bits >>= n;\n#else\n bc->bits <<= n;\n#endif\n bc->bits_left -= n;\n}']
830
0
https://github.com/openssl/openssl/blob/1fac96e4d6484a517f2ebe99b72016726391723c/crypto/bn/bn_asm.c/#L551
void bn_mul_comba8(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b) { #ifdef BN_LLONG BN_ULLONG t; #else BN_ULONG bl,bh; #endif BN_ULONG t1,t2; 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; }
['int DSA_do_verify(const unsigned char *dgst, int dgst_len, DSA_SIG *sig,\n\t\t DSA *dsa)\n\t{\n\tBN_CTX *ctx;\n\tBIGNUM u1,u2,t1;\n\tBN_MONT_CTX *mont=NULL;\n\tint ret = -1;\n\tif ((ctx=BN_CTX_new()) == NULL) goto err;\n\tBN_init(&u1);\n\tBN_init(&u2);\n\tBN_init(&t1);\n\tif ((BN_mod_inverse(&u2,sig->s,dsa->q,ctx)) == NULL) goto err;\n\tif (BN_bin2bn(dgst,dgst_len,&u1) == NULL) goto err;\n\tif (!BN_mod_mul(&u1,&u1,&u2,dsa->q,ctx)) goto err;\n\tif (!BN_mod_mul(&u2,sig->r,&u2,dsa->q,ctx)) goto err;\n\tif ((dsa->method_mont_p == NULL) && (dsa->flags & DSA_FLAG_CACHE_MONT_P))\n\t\t{\n\t\tif ((dsa->method_mont_p=(char *)BN_MONT_CTX_new()) != NULL)\n\t\t\tif (!BN_MONT_CTX_set((BN_MONT_CTX *)dsa->method_mont_p,\n\t\t\t\tdsa->p,ctx)) goto err;\n\t\t}\n\tmont=(BN_MONT_CTX *)dsa->method_mont_p;\n#if 0\n\t{\n\tBIGNUM t2;\n\tBN_init(&t2);\n\tif (!BN_mod_exp_mont(&t1,dsa->g,&u1,dsa->p,ctx,mont)) goto err;\n\tif (!BN_mod_exp_mont(&t2,dsa->pub_key,&u2,dsa->p,ctx,mont)) goto err;\n\tif (!BN_mod_mul(&u1,&t1,&t2,dsa->p,ctx)) goto err_bn;\n\tBN_free(&t2);\n\t}\n\tif (!BN_mod(&u1,&u1,dsa->q,ctx)) goto err;\n#else\n\t{\n\tif (!BN_mod_exp2_mont(&t1,dsa->g,&u1,dsa->pub_key,&u2,dsa->p,ctx,mont))\n\t\tgoto err;\n\tif (!BN_mod(&u1,&t1,dsa->q,ctx)) goto err;\n\t}\n#endif\n\tret=(BN_ucmp(&u1, sig->r) == 0);\n\terr:\n\tif (ret != 1) DSAerr(DSA_F_DSA_DO_VERIFY,ERR_R_BN_LIB);\n\tif (ctx != NULL) BN_CTX_free(ctx);\n\tBN_free(&u1);\n\tBN_free(&u2);\n\tBN_free(&t1);\n\treturn(ret);\n\t}', 'BIGNUM *BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret)\n\t{\n\tunsigned int i,m;\n\tunsigned int n;\n\tBN_ULONG l;\n\tif (ret == NULL) ret=BN_new();\n\tif (ret == NULL) return(NULL);\n\tl=0;\n\tn=len;\n\tif (n == 0)\n\t\t{\n\t\tret->top=0;\n\t\treturn(ret);\n\t\t}\n\tif (bn_expand(ret,(int)(n+2)*8) == NULL)\n\t\treturn(NULL);\n\ti=((n-1)/BN_BYTES)+1;\n\tm=((n-1)%(BN_BYTES));\n\tret->top=i;\n\twhile (n-- > 0)\n\t\t{\n\t\tl=(l<<8L)| *(s++);\n\t\tif (m-- == 0)\n\t\t\t{\n\t\t\tret->d[--i]=l;\n\t\t\tl=0;\n\t\t\tm=BN_BYTES-1;\n\t\t\t}\n\t\t}\n\tbn_fix_top(ret);\n\treturn(ret);\n\t}', 'int BN_mod_mul(BIGNUM *ret, BIGNUM *a, BIGNUM *b, BIGNUM *m, BN_CTX *ctx)\n\t{\n\tBIGNUM *t;\n\tint r=0;\n\tbn_check_top(a);\n\tbn_check_top(b);\n\tbn_check_top(m);\n\tt= &(ctx->bn[ctx->tos++]);\n\tif (a == b)\n\t\t{ if (!BN_sqr(t,a,ctx)) goto err; }\n\telse\n\t\t{ if (!BN_mul(t,a,b,ctx)) goto err; }\n\tif (!BN_mod(ret,t,m,ctx)) goto err;\n\tr=1;\nerr:\n\tctx->tos--;\n\treturn(r);\n\t}', 'int BN_mul(BIGNUM *r, BIGNUM *a, BIGNUM *b, BN_CTX *ctx)\n\t{\n\tint top,al,bl;\n\tBIGNUM *rr;\n#ifdef BN_RECURSION\n\tBIGNUM *t;\n\tint i,j,k;\n#endif\n#ifdef BN_COUNT\nprintf("BN_mul %d * %d\\n",a->top,b->top);\n#endif\n\tbn_check_top(a);\n\tbn_check_top(b);\n\tbn_check_top(r);\n\tal=a->top;\n\tbl=b->top;\n\tr->neg=a->neg^b->neg;\n\tif ((al == 0) || (bl == 0))\n\t\t{\n\t\tBN_zero(r);\n\t\treturn(1);\n\t\t}\n\ttop=al+bl;\n\tif ((r == a) || (r == b))\n\t\trr= &(ctx->bn[ctx->tos+1]);\n\telse\n\t\trr=r;\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n\tif (al == bl)\n\t\t{\n# ifdef BN_MUL_COMBA\n if (al == 8)\n\t\t\t{\n\t\t\tif (bn_wexpand(rr,16) == NULL) return(0);\n\t\t\tr->top=16;\n\t\t\tbn_mul_comba8(rr->d,a->d,b->d);\n\t\t\tgoto end;\n\t\t\t}\n\t\telse\n# endif\n#ifdef BN_RECURSION\n\t\tif (al < BN_MULL_SIZE_NORMAL)\n#endif\n\t\t\t{\n\t\t\tif (bn_wexpand(rr,top) == NULL) return(0);\n\t\t\trr->top=top;\n\t\t\tbn_mul_normal(rr->d,a->d,al,b->d,bl);\n\t\t\tgoto end;\n\t\t\t}\n# ifdef BN_RECURSION\n\t\tgoto symetric;\n# endif\n\t\t}\n#endif\n#ifdef BN_RECURSION\n\telse if ((al < BN_MULL_SIZE_NORMAL) || (bl < BN_MULL_SIZE_NORMAL))\n\t\t{\n\t\tif (bn_wexpand(rr,top) == NULL) return(0);\n\t\trr->top=top;\n\t\tbn_mul_normal(rr->d,a->d,al,b->d,bl);\n\t\tgoto end;\n\t\t}\n\telse\n\t\t{\n\t\ti=(al-bl);\n\t\tif ((i == 1) && !BN_get_flags(b,BN_FLG_STATIC_DATA))\n\t\t\t{\n\t\t\tbn_wexpand(b,al);\n\t\t\tb->d[bl]=0;\n\t\t\tbl++;\n\t\t\tgoto symetric;\n\t\t\t}\n\t\telse if ((i == -1) && !BN_get_flags(a,BN_FLG_STATIC_DATA))\n\t\t\t{\n\t\t\tbn_wexpand(a,bl);\n\t\t\ta->d[al]=0;\n\t\t\tal++;\n\t\t\tgoto symetric;\n\t\t\t}\n\t\t}\n#endif\n\tif (bn_wexpand(rr,top) == NULL) return(0);\n\trr->top=top;\n\tbn_mul_normal(rr->d,a->d,al,b->d,bl);\n#ifdef BN_RECURSION\n\tif (0)\n\t\t{\nsymetric:\n\t\tj=BN_num_bits_word((BN_ULONG)al);\n\t\tj=1<<(j-1);\n\t\tk=j+j;\n\t\tt= &(ctx->bn[ctx->tos]);\n\t\tif (al == j)\n\t\t\t{\n\t\t\tbn_wexpand(t,k*2);\n\t\t\tbn_wexpand(rr,k*2);\n\t\t\tbn_mul_recursive(rr->d,a->d,b->d,al,t->d);\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tbn_wexpand(a,k);\n\t\t\tbn_wexpand(b,k);\n\t\t\tbn_wexpand(t,k*4);\n\t\t\tbn_wexpand(rr,k*4);\n\t\t\tfor (i=a->top; i<k; i++)\n\t\t\t\ta->d[i]=0;\n\t\t\tfor (i=b->top; i<k; i++)\n\t\t\t\tb->d[i]=0;\n\t\t\tbn_mul_part_recursive(rr->d,a->d,b->d,al-j,j,t->d);\n\t\t\t}\n\t\trr->top=top;\n\t\t}\n#endif\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\nend:\n#endif\n\tbn_fix_top(rr);\n\tif (r != rr) BN_copy(r,rr);\n\treturn(1);\n\t}', 'void bn_mul_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n2,\n\t BN_ULONG *t)\n\t{\n\tint n=n2/2,c1,c2;\n\tunsigned int neg,zero;\n\tBN_ULONG ln,lo,*p;\n#ifdef BN_COUNT\nprintf(" bn_mul_recursive %d * %d\\n",n2,n2);\n#endif\n#ifdef BN_MUL_COMBA\n if (n2 == 8)\n\t\t{\n\t\tbn_mul_comba8(r,a,b);\n\t\treturn;\n\t\t}\n#endif\n\tif (n2 < BN_MUL_RECURSIVE_SIZE_NORMAL)\n\t\t{\n\t\tbn_mul_normal(r,a,n2,b,n2);\n\t\treturn;\n\t\t}\n\tc1=bn_cmp_words(a,&(a[n]),n);\n\tc2=bn_cmp_words(&(b[n]),b,n);\n\tzero=neg=0;\n\tswitch (c1*3+c2)\n\t\t{\n\tcase -4:\n\t\tbn_sub_words(t, &(a[n]),a, n);\n\t\tbn_sub_words(&(t[n]),b, &(b[n]),n);\n\t\tbreak;\n\tcase -3:\n\t\tzero=1;\n\t\tbreak;\n\tcase -2:\n\t\tbn_sub_words(t, &(a[n]),a, n);\n\t\tbn_sub_words(&(t[n]),&(b[n]),b, n);\n\t\tneg=1;\n\t\tbreak;\n\tcase -1:\n\tcase 0:\n\tcase 1:\n\t\tzero=1;\n\t\tbreak;\n\tcase 2:\n\t\tbn_sub_words(t, a, &(a[n]),n);\n\t\tbn_sub_words(&(t[n]),b, &(b[n]),n);\n\t\tneg=1;\n\t\tbreak;\n\tcase 3:\n\t\tzero=1;\n\t\tbreak;\n\tcase 4:\n\t\tbn_sub_words(t, a, &(a[n]),n);\n\t\tbn_sub_words(&(t[n]),&(b[n]),b, n);\n\t\tbreak;\n\t\t}\n#ifdef BN_MUL_COMBA\n\tif (n == 4)\n\t\t{\n\t\tif (!zero)\n\t\t\tbn_mul_comba4(&(t[n2]),t,&(t[n]));\n\t\telse\n\t\t\tmemset(&(t[n2]),0,8*sizeof(BN_ULONG));\n\t\tbn_mul_comba4(r,a,b);\n\t\tbn_mul_comba4(&(r[n2]),&(a[n]),&(b[n]));\n\t\t}\n\telse if (n == 8)\n\t\t{\n\t\tif (!zero)\n\t\t\tbn_mul_comba8(&(t[n2]),t,&(t[n]));\n\t\telse\n\t\t\tmemset(&(t[n2]),0,16*sizeof(BN_ULONG));\n\t\tbn_mul_comba8(r,a,b);\n\t\tbn_mul_comba8(&(r[n2]),&(a[n]),&(b[n]));\n\t\t}\n\telse\n#endif\n\t\t{\n\t\tp= &(t[n2*2]);\n\t\tif (!zero)\n\t\t\tbn_mul_recursive(&(t[n2]),t,&(t[n]),n,p);\n\t\telse\n\t\t\tmemset(&(t[n2]),0,n2*sizeof(BN_ULONG));\n\t\tbn_mul_recursive(r,a,b,n,p);\n\t\tbn_mul_recursive(&(r[n2]),&(a[n]),&(b[n]),n,p);\n\t\t}\n\tc1=(int)(bn_add_words(t,r,&(r[n2]),n2));\n\tif (neg)\n\t\t{\n\t\tc1-=(int)(bn_sub_words(&(t[n2]),t,&(t[n2]),n2));\n\t\t}\n\telse\n\t\t{\n\t\tc1+=(int)(bn_add_words(&(t[n2]),&(t[n2]),t,n2));\n\t\t}\n\tc1+=(int)(bn_add_words(&(r[n]),&(r[n]),&(t[n2]),n2));\n\tif (c1)\n\t\t{\n\t\tp= &(r[n+n2]);\n\t\tlo= *p;\n\t\tln=(lo+c1)&BN_MASK2;\n\t\t*p=ln;\n\t\tif (ln < (BN_ULONG)c1)\n\t\t\t{\n\t\t\tdo\t{\n\t\t\t\tp++;\n\t\t\t\tlo= *p;\n\t\t\t\tln=(lo+1)&BN_MASK2;\n\t\t\t\t*p=ln;\n\t\t\t\t} while (ln == 0);\n\t\t\t}\n\t\t}\n\t}', 'void bn_mul_comba8(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b)\n\t{\n#ifdef BN_LLONG\n\tBN_ULLONG t;\n#else\n\tBN_ULONG bl,bh;\n#endif\n\tBN_ULONG t1,t2;\n\tBN_ULONG c1,c2,c3;\n\tc1=0;\n\tc2=0;\n\tc3=0;\n\tmul_add_c(a[0],b[0],c1,c2,c3);\n\tr[0]=c1;\n\tc1=0;\n\tmul_add_c(a[0],b[1],c2,c3,c1);\n\tmul_add_c(a[1],b[0],c2,c3,c1);\n\tr[1]=c2;\n\tc2=0;\n\tmul_add_c(a[2],b[0],c3,c1,c2);\n\tmul_add_c(a[1],b[1],c3,c1,c2);\n\tmul_add_c(a[0],b[2],c3,c1,c2);\n\tr[2]=c3;\n\tc3=0;\n\tmul_add_c(a[0],b[3],c1,c2,c3);\n\tmul_add_c(a[1],b[2],c1,c2,c3);\n\tmul_add_c(a[2],b[1],c1,c2,c3);\n\tmul_add_c(a[3],b[0],c1,c2,c3);\n\tr[3]=c1;\n\tc1=0;\n\tmul_add_c(a[4],b[0],c2,c3,c1);\n\tmul_add_c(a[3],b[1],c2,c3,c1);\n\tmul_add_c(a[2],b[2],c2,c3,c1);\n\tmul_add_c(a[1],b[3],c2,c3,c1);\n\tmul_add_c(a[0],b[4],c2,c3,c1);\n\tr[4]=c2;\n\tc2=0;\n\tmul_add_c(a[0],b[5],c3,c1,c2);\n\tmul_add_c(a[1],b[4],c3,c1,c2);\n\tmul_add_c(a[2],b[3],c3,c1,c2);\n\tmul_add_c(a[3],b[2],c3,c1,c2);\n\tmul_add_c(a[4],b[1],c3,c1,c2);\n\tmul_add_c(a[5],b[0],c3,c1,c2);\n\tr[5]=c3;\n\tc3=0;\n\tmul_add_c(a[6],b[0],c1,c2,c3);\n\tmul_add_c(a[5],b[1],c1,c2,c3);\n\tmul_add_c(a[4],b[2],c1,c2,c3);\n\tmul_add_c(a[3],b[3],c1,c2,c3);\n\tmul_add_c(a[2],b[4],c1,c2,c3);\n\tmul_add_c(a[1],b[5],c1,c2,c3);\n\tmul_add_c(a[0],b[6],c1,c2,c3);\n\tr[6]=c1;\n\tc1=0;\n\tmul_add_c(a[0],b[7],c2,c3,c1);\n\tmul_add_c(a[1],b[6],c2,c3,c1);\n\tmul_add_c(a[2],b[5],c2,c3,c1);\n\tmul_add_c(a[3],b[4],c2,c3,c1);\n\tmul_add_c(a[4],b[3],c2,c3,c1);\n\tmul_add_c(a[5],b[2],c2,c3,c1);\n\tmul_add_c(a[6],b[1],c2,c3,c1);\n\tmul_add_c(a[7],b[0],c2,c3,c1);\n\tr[7]=c2;\n\tc2=0;\n\tmul_add_c(a[7],b[1],c3,c1,c2);\n\tmul_add_c(a[6],b[2],c3,c1,c2);\n\tmul_add_c(a[5],b[3],c3,c1,c2);\n\tmul_add_c(a[4],b[4],c3,c1,c2);\n\tmul_add_c(a[3],b[5],c3,c1,c2);\n\tmul_add_c(a[2],b[6],c3,c1,c2);\n\tmul_add_c(a[1],b[7],c3,c1,c2);\n\tr[8]=c3;\n\tc3=0;\n\tmul_add_c(a[2],b[7],c1,c2,c3);\n\tmul_add_c(a[3],b[6],c1,c2,c3);\n\tmul_add_c(a[4],b[5],c1,c2,c3);\n\tmul_add_c(a[5],b[4],c1,c2,c3);\n\tmul_add_c(a[6],b[3],c1,c2,c3);\n\tmul_add_c(a[7],b[2],c1,c2,c3);\n\tr[9]=c1;\n\tc1=0;\n\tmul_add_c(a[7],b[3],c2,c3,c1);\n\tmul_add_c(a[6],b[4],c2,c3,c1);\n\tmul_add_c(a[5],b[5],c2,c3,c1);\n\tmul_add_c(a[4],b[6],c2,c3,c1);\n\tmul_add_c(a[3],b[7],c2,c3,c1);\n\tr[10]=c2;\n\tc2=0;\n\tmul_add_c(a[4],b[7],c3,c1,c2);\n\tmul_add_c(a[5],b[6],c3,c1,c2);\n\tmul_add_c(a[6],b[5],c3,c1,c2);\n\tmul_add_c(a[7],b[4],c3,c1,c2);\n\tr[11]=c3;\n\tc3=0;\n\tmul_add_c(a[7],b[5],c1,c2,c3);\n\tmul_add_c(a[6],b[6],c1,c2,c3);\n\tmul_add_c(a[5],b[7],c1,c2,c3);\n\tr[12]=c1;\n\tc1=0;\n\tmul_add_c(a[6],b[7],c2,c3,c1);\n\tmul_add_c(a[7],b[6],c2,c3,c1);\n\tr[13]=c2;\n\tc2=0;\n\tmul_add_c(a[7],b[7],c3,c1,c2);\n\tr[14]=c3;\n\tr[15]=c1;\n\t}']
831
0
https://github.com/openssl/openssl/blob/e02c519cd32a55e6ad39a0cfbeeda775f9115f28/crypto/bn/bn_ctx.c/#L276
static unsigned int BN_STACK_pop(BN_STACK *st) { return st->indexes[--(st->depth)]; }
['int BN_mod_exp_simple(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,\n const BIGNUM *m, BN_CTX *ctx)\n{\n int i, j, bits, ret = 0, wstart, wend, window, wvalue;\n int start = 1;\n BIGNUM *d;\n BIGNUM *val[TABLE_SIZE];\n if (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 BNerr(BN_F_BN_MOD_EXP_SIMPLE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);\n return 0;\n }\n bits = BN_num_bits(p);\n if (bits == 0) {\n if (BN_abs_is_word(m, 1)) {\n ret = 1;\n BN_zero(r);\n } else {\n ret = BN_one(r);\n }\n return ret;\n }\n BN_CTX_start(ctx);\n d = BN_CTX_get(ctx);\n val[0] = BN_CTX_get(ctx);\n if (val[0] == NULL)\n goto err;\n if (!BN_nnmod(val[0], a, m, ctx))\n goto err;\n if (BN_is_zero(val[0])) {\n BN_zero(r);\n ret = 1;\n goto err;\n }\n window = BN_window_bits_for_exponent_size(bits);\n if (window > 1) {\n if (!BN_mod_mul(d, val[0], val[0], m, 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(val[i], val[i - 1], d, m, ctx))\n goto err;\n }\n }\n start = 1;\n wvalue = 0;\n wstart = bits - 1;\n wend = 0;\n if (!BN_one(r))\n goto err;\n for (;;) {\n if (BN_is_bit_set(p, wstart) == 0) {\n if (!start)\n if (!BN_mod_mul(r, r, r, m, ctx))\n goto err;\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(r, r, r, m, ctx))\n goto err;\n }\n if (!BN_mod_mul(r, r, val[wvalue >> 1], m, ctx))\n goto err;\n wstart -= wend + 1;\n wvalue = 0;\n start = 0;\n if (wstart < 0)\n break;\n }\n ret = 1;\n err:\n BN_CTX_end(ctx);\n bn_check_top(r);\n return ret;\n}', 'void BN_CTX_start(BN_CTX *ctx)\n{\n CTXDBG_ENTRY("BN_CTX_start", ctx);\n if (ctx->err_stack || ctx->too_many)\n ctx->err_stack++;\n else if (!BN_STACK_push(&ctx->stack, ctx->used)) {\n BNerr(BN_F_BN_CTX_START, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n ctx->err_stack++;\n }\n CTXDBG_EXIT(ctx);\n}', 'int BN_nnmod(BIGNUM *r, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx)\n{\n if (!(BN_mod(r, m, d, ctx)))\n return 0;\n if (!r->neg)\n return 1;\n return (d->neg ? BN_sub : BN_add) (r, r, d);\n}', 'int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,\n BN_CTX *ctx)\n{\n int norm_shift, i, loop;\n BIGNUM *tmp, wnum, *snum, *sdiv, *res;\n BN_ULONG *resp, *wnump;\n BN_ULONG d0, d1;\n int num_n, div_n;\n int no_branch = 0;\n if ((num->top > 0 && num->d[num->top - 1] == 0) ||\n (divisor->top > 0 && divisor->d[divisor->top - 1] == 0)) {\n BNerr(BN_F_BN_DIV, BN_R_NOT_INITIALIZED);\n return 0;\n }\n bn_check_top(num);\n bn_check_top(divisor);\n if ((BN_get_flags(num, BN_FLG_CONSTTIME) != 0)\n || (BN_get_flags(divisor, BN_FLG_CONSTTIME) != 0)) {\n no_branch = 1;\n }\n bn_check_top(dv);\n bn_check_top(rm);\n if (BN_is_zero(divisor)) {\n BNerr(BN_F_BN_DIV, BN_R_DIV_BY_ZERO);\n return 0;\n }\n if (!no_branch && BN_ucmp(num, divisor) < 0) {\n if (rm != NULL) {\n if (BN_copy(rm, num) == NULL)\n return 0;\n }\n if (dv != NULL)\n BN_zero(dv);\n return 1;\n }\n BN_CTX_start(ctx);\n res = (dv == NULL) ? BN_CTX_get(ctx) : dv;\n tmp = BN_CTX_get(ctx);\n snum = BN_CTX_get(ctx);\n sdiv = BN_CTX_get(ctx);\n if (sdiv == NULL)\n goto err;\n norm_shift = BN_BITS2 - ((BN_num_bits(divisor)) % BN_BITS2);\n if (!(BN_lshift(sdiv, divisor, norm_shift)))\n goto err;\n sdiv->neg = 0;\n norm_shift += BN_BITS2;\n if (!(BN_lshift(snum, num, norm_shift)))\n goto err;\n snum->neg = 0;\n if (no_branch) {\n if (snum->top <= sdiv->top + 1) {\n if (bn_wexpand(snum, sdiv->top + 2) == NULL)\n goto err;\n for (i = snum->top; i < sdiv->top + 2; i++)\n snum->d[i] = 0;\n snum->top = sdiv->top + 2;\n } else {\n if (bn_wexpand(snum, snum->top + 1) == NULL)\n goto err;\n snum->d[snum->top] = 0;\n snum->top++;\n }\n }\n div_n = sdiv->top;\n num_n = snum->top;\n loop = num_n - div_n;\n wnum.neg = 0;\n wnum.d = &(snum->d[loop]);\n wnum.top = div_n;\n wnum.flags = BN_FLG_STATIC_DATA;\n wnum.dmax = snum->dmax - loop;\n d0 = sdiv->d[div_n - 1];\n d1 = (div_n == 1) ? 0 : sdiv->d[div_n - 2];\n wnump = &(snum->d[num_n - 1]);\n if (!bn_wexpand(res, (loop + 1)))\n goto err;\n res->neg = (num->neg ^ divisor->neg);\n res->top = loop - no_branch;\n resp = &(res->d[loop - 1]);\n if (!bn_wexpand(tmp, (div_n + 1)))\n goto err;\n if (!no_branch) {\n if (BN_ucmp(&wnum, sdiv) >= 0) {\n bn_clear_top2max(&wnum);\n bn_sub_words(wnum.d, wnum.d, sdiv->d, div_n);\n *resp = 1;\n } else\n res->top--;\n }\n resp++;\n if (res->top == 0)\n res->neg = 0;\n else\n resp--;\n for (i = 0; i < loop - 1; i++, wnump--) {\n BN_ULONG q, l0;\n# if defined(BN_DIV3W) && !defined(OPENSSL_NO_ASM)\n BN_ULONG bn_div_3_words(BN_ULONG *, BN_ULONG, BN_ULONG);\n q = bn_div_3_words(wnump, d1, d0);\n# else\n BN_ULONG n0, n1, rem = 0;\n n0 = wnump[0];\n n1 = wnump[-1];\n if (n0 == d0)\n q = BN_MASK2;\n else {\n# ifdef BN_LLONG\n BN_ULLONG t2;\n# if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)\n q = (BN_ULONG)(((((BN_ULLONG) n0) << BN_BITS2) | n1) / d0);\n# else\n q = bn_div_words(n0, n1, d0);\n# endif\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n t2 = (BN_ULLONG) d1 *q;\n for (;;) {\n if (t2 <= ((((BN_ULLONG) rem) << BN_BITS2) | wnump[-2]))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n t2 -= d1;\n }\n# else\n BN_ULONG t2l, t2h;\n q = bn_div_words(n0, n1, d0);\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n# if defined(BN_UMULT_LOHI)\n BN_UMULT_LOHI(t2l, t2h, d1, q);\n# elif defined(BN_UMULT_HIGH)\n t2l = d1 * q;\n t2h = BN_UMULT_HIGH(d1, q);\n# else\n {\n BN_ULONG ql, qh;\n t2l = LBITS(d1);\n t2h = HBITS(d1);\n ql = LBITS(q);\n qh = HBITS(q);\n mul64(t2l, t2h, ql, qh);\n }\n# endif\n for (;;) {\n if ((t2h < rem) || ((t2h == rem) && (t2l <= wnump[-2])))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n if (t2l < d1)\n t2h--;\n t2l -= d1;\n }\n# endif\n }\n# endif\n l0 = bn_mul_words(tmp->d, sdiv->d, div_n, q);\n tmp->d[div_n] = l0;\n wnum.d--;\n if (bn_sub_words(wnum.d, wnum.d, tmp->d, div_n + 1)) {\n q--;\n if (bn_add_words(wnum.d, wnum.d, sdiv->d, div_n))\n (*wnump)++;\n }\n resp--;\n *resp = q;\n }\n bn_correct_top(snum);\n if (rm != NULL) {\n int neg = num->neg;\n BN_rshift(rm, snum, norm_shift);\n if (!BN_is_zero(rm))\n rm->neg = neg;\n bn_check_top(rm);\n }\n if (no_branch)\n bn_correct_top(res);\n BN_CTX_end(ctx);\n return 1;\n err:\n bn_check_top(rm);\n BN_CTX_end(ctx);\n return 0;\n}', 'int BN_mod_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m,\n BN_CTX *ctx)\n{\n BIGNUM *t;\n int ret = 0;\n bn_check_top(a);\n bn_check_top(b);\n bn_check_top(m);\n BN_CTX_start(ctx);\n if ((t = BN_CTX_get(ctx)) == NULL)\n goto err;\n if (a == b) {\n if (!BN_sqr(t, a, ctx))\n goto err;\n } else {\n if (!BN_mul(t, a, b, ctx))\n goto err;\n }\n if (!BN_nnmod(r, t, m, ctx))\n goto err;\n bn_check_top(r);\n ret = 1;\n err:\n BN_CTX_end(ctx);\n return ret;\n}', 'int BN_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)\n{\n int ret = bn_mul_fixed_top(r, a, b, ctx);\n bn_correct_top(r);\n bn_check_top(r);\n return ret;\n}', 'int bn_mul_fixed_top(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)\n{\n int ret = 0;\n int top, al, bl;\n BIGNUM *rr;\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n int i;\n#endif\n#ifdef BN_RECURSION\n BIGNUM *t = NULL;\n int j = 0, k;\n#endif\n bn_check_top(a);\n bn_check_top(b);\n bn_check_top(r);\n al = a->top;\n bl = b->top;\n if ((al == 0) || (bl == 0)) {\n BN_zero(r);\n return 1;\n }\n top = al + bl;\n BN_CTX_start(ctx);\n if ((r == a) || (r == b)) {\n if ((rr = BN_CTX_get(ctx)) == NULL)\n goto err;\n } else\n rr = r;\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n i = al - bl;\n#endif\n#ifdef BN_MUL_COMBA\n if (i == 0) {\n# if 0\n if (al == 4) {\n if (bn_wexpand(rr, 8) == NULL)\n goto err;\n rr->top = 8;\n bn_mul_comba4(rr->d, a->d, b->d);\n goto end;\n }\n# endif\n if (al == 8) {\n if (bn_wexpand(rr, 16) == NULL)\n goto err;\n rr->top = 16;\n bn_mul_comba8(rr->d, a->d, b->d);\n goto end;\n }\n }\n#endif\n#ifdef BN_RECURSION\n if ((al >= BN_MULL_SIZE_NORMAL) && (bl >= BN_MULL_SIZE_NORMAL)) {\n if (i >= -1 && i <= 1) {\n if (i >= 0) {\n j = BN_num_bits_word((BN_ULONG)al);\n }\n if (i == -1) {\n j = BN_num_bits_word((BN_ULONG)bl);\n }\n j = 1 << (j - 1);\n assert(j <= al || j <= bl);\n k = j + j;\n t = BN_CTX_get(ctx);\n if (t == NULL)\n goto err;\n if (al > j || bl > j) {\n if (bn_wexpand(t, k * 4) == NULL)\n goto err;\n if (bn_wexpand(rr, k * 4) == NULL)\n goto err;\n bn_mul_part_recursive(rr->d, a->d, b->d,\n j, al - j, bl - j, t->d);\n } else {\n if (bn_wexpand(t, k * 2) == NULL)\n goto err;\n if (bn_wexpand(rr, k * 2) == NULL)\n goto err;\n bn_mul_recursive(rr->d, a->d, b->d, j, al - j, bl - j, t->d);\n }\n rr->top = top;\n goto end;\n }\n }\n#endif\n if (bn_wexpand(rr, top) == NULL)\n goto err;\n rr->top = top;\n bn_mul_normal(rr->d, a->d, al, b->d, bl);\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n end:\n#endif\n rr->neg = a->neg ^ b->neg;\n rr->flags |= BN_FLG_FIXED_TOP;\n if (r != rr && BN_copy(r, rr) == NULL)\n goto err;\n ret = 1;\n err:\n bn_check_top(r);\n BN_CTX_end(ctx);\n return ret;\n}', 'void BN_CTX_end(BN_CTX *ctx)\n{\n CTXDBG_ENTRY("BN_CTX_end", ctx);\n if (ctx->err_stack)\n ctx->err_stack--;\n else {\n unsigned int fp = BN_STACK_pop(&ctx->stack);\n if (fp < ctx->used)\n BN_POOL_release(&ctx->pool, ctx->used - fp);\n ctx->used = fp;\n ctx->too_many = 0;\n }\n CTXDBG_EXIT(ctx);\n}', 'static unsigned int BN_STACK_pop(BN_STACK *st)\n{\n return st->indexes[--(st->depth)];\n}']
832
0
https://github.com/openssl/openssl/blob/9639515871c73722de3fff04d3c50d54aa6b1477/crypto/rsa/rsa_chk.c/#L180
int RSA_check_key(RSA *key) { BIGNUM *i, *j, *k, *l, *m; BN_CTX *ctx; int r; int ret=1; i = BN_new(); j = BN_new(); k = BN_new(); l = BN_new(); m = BN_new(); ctx = BN_CTX_new(); if (i == NULL || j == NULL || k == NULL || l == NULL || m == NULL || ctx == NULL) { ret = -1; RSAerr(RSA_F_RSA_CHECK_KEY, ERR_R_MALLOC_FAILURE); goto err; } r = BN_is_prime(key->p, BN_prime_checks, NULL, NULL, NULL); if (r != 1) { ret = r; if (r != 0) goto err; RSAerr(RSA_F_RSA_CHECK_KEY, RSA_R_P_NOT_PRIME); } r = BN_is_prime(key->q, BN_prime_checks, NULL, NULL, NULL); if (r != 1) { ret = r; if (r != 0) goto err; RSAerr(RSA_F_RSA_CHECK_KEY, RSA_R_Q_NOT_PRIME); } r = BN_mul(i, key->p, key->q, ctx); if (!r) { ret = -1; goto err; } if (BN_cmp(i, key->n) != 0) { ret = 0; RSAerr(RSA_F_RSA_CHECK_KEY, RSA_R_N_DOES_NOT_EQUAL_P_Q); } r = BN_sub(i, key->p, BN_value_one()); if (!r) { ret = -1; goto err; } r = BN_sub(j, key->q, BN_value_one()); if (!r) { ret = -1; goto err; } r = BN_mul(l, i, j, ctx); if (!r) { ret = -1; goto err; } r = BN_gcd(m, i, j, ctx); if (!r) { ret = -1; goto err; } r = BN_div(k, NULL, l, m, ctx); if (!r) { ret = -1; goto err; } r = BN_mod_mul(i, key->d, key->e, k, ctx); if (!r) { ret = -1; goto err; } if (!BN_is_one(i)) { ret = 0; RSAerr(RSA_F_RSA_CHECK_KEY, RSA_R_D_E_NOT_CONGRUENT_TO_1); } if (key->dmp1 != NULL && key->dmq1 != NULL && key->iqmp != NULL) { r = BN_sub(i, key->p, BN_value_one()); if (!r) { ret = -1; goto err; } r = BN_mod(j, key->d, i, ctx); if (!r) { ret = -1; goto err; } if (BN_cmp(j, key->dmp1) != 0) { ret = 0; RSAerr(RSA_F_RSA_CHECK_KEY, RSA_R_DMP1_NOT_CONGRUENT_TO_D); } r = BN_sub(i, key->q, BN_value_one()); if (!r) { ret = -1; goto err; } r = BN_mod(j, key->d, i, ctx); if (!r) { ret = -1; goto err; } if (BN_cmp(j, key->dmq1) != 0) { ret = 0; RSAerr(RSA_F_RSA_CHECK_KEY, RSA_R_DMQ1_NOT_CONGRUENT_TO_D); } if(!BN_mod_inverse(i, key->q, key->p, ctx)) { ret = -1; goto err; } if (BN_cmp(i, key->iqmp) != 0) { ret = 0; RSAerr(RSA_F_RSA_CHECK_KEY, RSA_R_IQMP_NOT_INVERSE_OF_Q); } } err: if (i != NULL) BN_free(i); if (j != NULL) BN_free(j); if (k != NULL) BN_free(k); if (l != NULL) BN_free(l); if (m != NULL) BN_free(m); if (ctx != NULL) BN_CTX_free(ctx); return (ret); }
['int RSA_check_key(RSA *key)\n\t{\n\tBIGNUM *i, *j, *k, *l, *m;\n\tBN_CTX *ctx;\n\tint r;\n\tint ret=1;\n\ti = BN_new();\n\tj = BN_new();\n\tk = BN_new();\n\tl = BN_new();\n\tm = BN_new();\n\tctx = BN_CTX_new();\n\tif (i == NULL || j == NULL || k == NULL || l == NULL ||\n\t\tm == NULL || ctx == NULL)\n\t\t{\n\t\tret = -1;\n\t\tRSAerr(RSA_F_RSA_CHECK_KEY, ERR_R_MALLOC_FAILURE);\n\t\tgoto err;\n\t\t}\n\tr = BN_is_prime(key->p, BN_prime_checks, NULL, NULL, NULL);\n\tif (r != 1)\n\t\t{\n\t\tret = r;\n\t\tif (r != 0)\n\t\t\tgoto err;\n\t\tRSAerr(RSA_F_RSA_CHECK_KEY, RSA_R_P_NOT_PRIME);\n\t\t}\n\tr = BN_is_prime(key->q, BN_prime_checks, NULL, NULL, NULL);\n\tif (r != 1)\n\t\t{\n\t\tret = r;\n\t\tif (r != 0)\n\t\t\tgoto err;\n\t\tRSAerr(RSA_F_RSA_CHECK_KEY, RSA_R_Q_NOT_PRIME);\n\t\t}\n\tr = BN_mul(i, key->p, key->q, ctx);\n\tif (!r) { ret = -1; goto err; }\n\tif (BN_cmp(i, key->n) != 0)\n\t\t{\n\t\tret = 0;\n\t\tRSAerr(RSA_F_RSA_CHECK_KEY, RSA_R_N_DOES_NOT_EQUAL_P_Q);\n\t\t}\n\tr = BN_sub(i, key->p, BN_value_one());\n\tif (!r) { ret = -1; goto err; }\n\tr = BN_sub(j, key->q, BN_value_one());\n\tif (!r) { ret = -1; goto err; }\n\tr = BN_mul(l, i, j, ctx);\n\tif (!r) { ret = -1; goto err; }\n\tr = BN_gcd(m, i, j, ctx);\n\tif (!r) { ret = -1; goto err; }\n\tr = BN_div(k, NULL, l, m, ctx);\n\tif (!r) { ret = -1; goto err; }\n\tr = BN_mod_mul(i, key->d, key->e, k, ctx);\n\tif (!r) { ret = -1; goto err; }\n\tif (!BN_is_one(i))\n\t\t{\n\t\tret = 0;\n\t\tRSAerr(RSA_F_RSA_CHECK_KEY, RSA_R_D_E_NOT_CONGRUENT_TO_1);\n\t\t}\n\tif (key->dmp1 != NULL && key->dmq1 != NULL && key->iqmp != NULL)\n\t\t{\n\t\tr = BN_sub(i, key->p, BN_value_one());\n\t\tif (!r) { ret = -1; goto err; }\n\t\tr = BN_mod(j, key->d, i, ctx);\n\t\tif (!r) { ret = -1; goto err; }\n\t\tif (BN_cmp(j, key->dmp1) != 0)\n\t\t\t{\n\t\t\tret = 0;\n\t\t\tRSAerr(RSA_F_RSA_CHECK_KEY,\n\t\t\t\tRSA_R_DMP1_NOT_CONGRUENT_TO_D);\n\t\t\t}\n\t\tr = BN_sub(i, key->q, BN_value_one());\n\t\tif (!r) { ret = -1; goto err; }\n\t\tr = BN_mod(j, key->d, i, ctx);\n\t\tif (!r) { ret = -1; goto err; }\n\t\tif (BN_cmp(j, key->dmq1) != 0)\n\t\t\t{\n\t\t\tret = 0;\n\t\t\tRSAerr(RSA_F_RSA_CHECK_KEY,\n\t\t\t\tRSA_R_DMQ1_NOT_CONGRUENT_TO_D);\n\t\t\t}\n\t\tif(!BN_mod_inverse(i, key->q, key->p, ctx))\n\t\t\t{\n\t\t\tret = -1;\n\t\t\tgoto err;\n\t\t\t}\n\t\tif (BN_cmp(i, key->iqmp) != 0)\n\t\t\t{\n\t\t\tret = 0;\n\t\t\tRSAerr(RSA_F_RSA_CHECK_KEY,\n\t\t\t\tRSA_R_IQMP_NOT_INVERSE_OF_Q);\n\t\t\t}\n\t\t}\n err:\n\tif (i != NULL) BN_free(i);\n\tif (j != NULL) BN_free(j);\n\tif (k != NULL) BN_free(k);\n\tif (l != NULL) BN_free(l);\n\tif (m != NULL) BN_free(m);\n\tif (ctx != NULL) BN_CTX_free(ctx);\n\treturn (ret);\n\t}', 'BIGNUM *BN_new(void)\n\t{\n\tBIGNUM *ret;\n\tif ((ret=(BIGNUM *)Malloc(sizeof(BIGNUM))) == NULL)\n\t\t{\n\t\tBNerr(BN_F_BN_NEW,ERR_R_MALLOC_FAILURE);\n\t\treturn(NULL);\n\t\t}\n\tret->flags=BN_FLG_MALLOCED;\n\tret->top=0;\n\tret->neg=0;\n\tret->max=0;\n\tret->d=NULL;\n\treturn(ret);\n\t}', 'BN_CTX *BN_CTX_new(void)\n\t{\n\tBN_CTX *ret;\n\tret=(BN_CTX *)Malloc(sizeof(BN_CTX));\n\tif (ret == NULL)\n\t\t{\n\t\tBNerr(BN_F_BN_CTX_NEW,ERR_R_MALLOC_FAILURE);\n\t\treturn(NULL);\n\t\t}\n\tBN_CTX_init(ret);\n\tret->flags=BN_FLG_MALLOCED;\n\treturn(ret);\n\t}', 'int BN_is_prime(BIGNUM *a, int checks, void (*callback)(int,int,void *),\n\t BN_CTX *ctx_passed, void *cb_arg)\n\t{\n\tint i,j,c2=0,ret= -1;\n\tBIGNUM *check;\n\tBN_CTX *ctx=NULL,*ctx2=NULL;\n\tBN_MONT_CTX *mont=NULL;\n\tif (!BN_is_odd(a))\n\t\treturn(0);\n\tif (ctx_passed != NULL)\n\t\tctx=ctx_passed;\n\telse\n\t\tif ((ctx=BN_CTX_new()) == NULL) goto err;\n\tif ((ctx2=BN_CTX_new()) == NULL) goto err;\n\tif ((mont=BN_MONT_CTX_new()) == NULL) goto err;\n\tcheck= &(ctx->bn[ctx->tos++]);\n\tif (!BN_MONT_CTX_set(mont,a,ctx2)) goto err;\n\tfor (i=0; i<checks; i++)\n\t\t{\n\t\tif (!BN_rand(check,BN_num_bits(a)-1,0,0)) goto err;\n\t\tj=witness(check,a,ctx,ctx2,mont);\n\t\tif (j == -1) goto err;\n\t\tif (j)\n\t\t\t{\n\t\t\tret=0;\n\t\t\tgoto err;\n\t\t\t}\n\t\tif (callback != NULL) callback(1,c2++,cb_arg);\n\t\t}\n\tret=1;\nerr:\n\tctx->tos--;\n\tif ((ctx_passed == NULL) && (ctx != NULL))\n\t\tBN_CTX_free(ctx);\n\tif (ctx2 != NULL)\n\t\tBN_CTX_free(ctx2);\n\tif (mont != NULL) BN_MONT_CTX_free(mont);\n\treturn(ret);\n\t}', 'BN_MONT_CTX *BN_MONT_CTX_new(void)\n\t{\n\tBN_MONT_CTX *ret;\n\tif ((ret=(BN_MONT_CTX *)Malloc(sizeof(BN_MONT_CTX))) == NULL)\n\t\treturn(NULL);\n\tBN_MONT_CTX_init(ret);\n\tret->flags=BN_FLG_MALLOCED;\n\treturn(ret);\n\t}', 'void BN_free(BIGNUM *a)\n\t{\n\tif (a == NULL) return;\n\tif ((a->d != NULL) && !(BN_get_flags(a,BN_FLG_STATIC_DATA)))\n\t\tFree(a->d);\n\ta->flags|=BN_FLG_FREE;\n\tif (a->flags & BN_FLG_MALLOCED)\n\t\tFree(a);\n\t}']
833
0
https://github.com/openssl/openssl/blob/877e8e970c3c94c43ce1db50fdbb8e9b0342b90e/crypto/bn/bn_asm.c/#L355
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); }
['void bn_mul_part_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n,\n\t int tna, int tnb, BN_ULONG *t)\n\t{\n\tint i,j,n2=n*2;\n\tint c1,c2,neg,zero;\n\tBN_ULONG ln,lo,*p;\n# ifdef BN_COUNT\n\tfprintf(stderr," bn_mul_part_recursive (%d+%d) * (%d+%d)\\n",\n\t\ttna, n, tnb, n);\n# endif\n\tif (n < 8)\n\t\t{\n\t\tbn_mul_normal(r,a,n+tna,b,n+tnb);\n\t\treturn;\n\t\t}\n\tc1=bn_cmp_part_words(a,&(a[n]),tna,n-tna);\n\tc2=bn_cmp_part_words(&(b[n]),b,tnb,tnb-n);\n\tzero=neg=0;\n\tswitch (c1*3+c2)\n\t\t{\n\tcase -4:\n\t\tbn_sub_part_words(t, &(a[n]),a, tna,tna-n);\n\t\tbn_sub_part_words(&(t[n]),b, &(b[n]),tnb,n-tnb);\n\t\tbreak;\n\tcase -3:\n\t\tzero=1;\n\tcase -2:\n\t\tbn_sub_part_words(t, &(a[n]),a, tna,tna-n);\n\t\tbn_sub_part_words(&(t[n]),&(b[n]),b, tnb,tnb-n);\n\t\tneg=1;\n\t\tbreak;\n\tcase -1:\n\tcase 0:\n\tcase 1:\n\t\tzero=1;\n\tcase 2:\n\t\tbn_sub_part_words(t, a, &(a[n]),tna,n-tna);\n\t\tbn_sub_part_words(&(t[n]),b, &(b[n]),tnb,n-tnb);\n\t\tneg=1;\n\t\tbreak;\n\tcase 3:\n\t\tzero=1;\n\tcase 4:\n\t\tbn_sub_part_words(t, a, &(a[n]),tna,n-tna);\n\t\tbn_sub_part_words(&(t[n]),&(b[n]),b, tnb,tnb-n);\n\t\tbreak;\n\t\t}\n# if 0\n\tif (n == 4)\n\t\t{\n\t\tbn_mul_comba4(&(t[n2]),t,&(t[n]));\n\t\tbn_mul_comba4(r,a,b);\n\t\tbn_mul_normal(&(r[n2]),&(a[n]),tn,&(b[n]),tn);\n\t\tmemset(&(r[n2+tn*2]),0,sizeof(BN_ULONG)*(n2-tn*2));\n\t\t}\n\telse\n# endif\n\tif (n == 8)\n\t\t{\n\t\tbn_mul_comba8(&(t[n2]),t,&(t[n]));\n\t\tbn_mul_comba8(r,a,b);\n\t\tbn_mul_normal(&(r[n2]),&(a[n]),tna,&(b[n]),tnb);\n\t\tmemset(&(r[n2+tna+tnb]),0,sizeof(BN_ULONG)*(n2-tna-tnb));\n\t\t}\n\telse\n\t\t{\n\t\tp= &(t[n2*2]);\n\t\tbn_mul_recursive(&(t[n2]),t,&(t[n]),n,0,0,p);\n\t\tbn_mul_recursive(r,a,b,n,0,0,p);\n\t\ti=n/2;\n\t\tif (tna > tnb)\n\t\t\tj = tna - i;\n\t\telse\n\t\t\tj = tnb - i;\n\t\tif (j == 0)\n\t\t\t{\n\t\t\tbn_mul_recursive(&(r[n2]),&(a[n]),&(b[n]),\n\t\t\t\ti,tna-i,tnb-i,p);\n\t\t\tmemset(&(r[n2+i*2]),0,sizeof(BN_ULONG)*(n2-i*2));\n\t\t\t}\n\t\telse if (j > 0)\n\t\t\t\t{\n\t\t\t\tbn_mul_part_recursive(&(r[n2]),&(a[n]),&(b[n]),\n\t\t\t\t\ti,tna-i,tnb-i,p);\n\t\t\t\tmemset(&(r[n2+tna+tnb]),0,\n\t\t\t\t\tsizeof(BN_ULONG)*(n2-tna-tnb));\n\t\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tmemset(&(r[n2]),0,sizeof(BN_ULONG)*n2);\n\t\t\tif (tna < BN_MUL_RECURSIVE_SIZE_NORMAL\n\t\t\t\t&& tnb < BN_MUL_RECURSIVE_SIZE_NORMAL)\n\t\t\t\t{\n\t\t\t\tbn_mul_normal(&(r[n2]),&(a[n]),tna,&(b[n]),tnb);\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\tfor (;;)\n\t\t\t\t\t{\n\t\t\t\t\ti/=2;\n\t\t\t\t\tif (i < tna && i < tnb)\n\t\t\t\t\t\t{\n\t\t\t\t\t\tbn_mul_part_recursive(&(r[n2]),\n\t\t\t\t\t\t\t&(a[n]),&(b[n]),\n\t\t\t\t\t\t\ti,tna-i,tnb-i,p);\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t}\n\t\t\t\t\telse if (i <= tna && i <= tnb)\n\t\t\t\t\t\t{\n\t\t\t\t\t\tbn_mul_recursive(&(r[n2]),\n\t\t\t\t\t\t\t&(a[n]),&(b[n]),\n\t\t\t\t\t\t\ti,tna-i,tnb-i,p);\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\tc1=(int)(bn_add_words(t,r,&(r[n2]),n2));\n\tif (neg)\n\t\t{\n\t\tc1-=(int)(bn_sub_words(&(t[n2]),t,&(t[n2]),n2));\n\t\t}\n\telse\n\t\t{\n\t\tc1+=(int)(bn_add_words(&(t[n2]),&(t[n2]),t,n2));\n\t\t}\n\tc1+=(int)(bn_add_words(&(r[n]),&(r[n]),&(t[n2]),n2));\n\tif (c1)\n\t\t{\n\t\tp= &(r[n+n2]);\n\t\tlo= *p;\n\t\tln=(lo+c1)&BN_MASK2;\n\t\t*p=ln;\n\t\tif (ln < (BN_ULONG)c1)\n\t\t\t{\n\t\t\tdo\t{\n\t\t\t\tp++;\n\t\t\t\tlo= *p;\n\t\t\t\tln=(lo+1)&BN_MASK2;\n\t\t\t\t*p=ln;\n\t\t\t\t} while (ln == 0);\n\t\t\t}\n\t\t}\n\t}', 'void bn_mul_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n2,\n\tint dna, int dnb, BN_ULONG *t)\n\t{\n\tint n=n2/2,c1,c2;\n\tint tna=n+dna, tnb=n+dnb;\n\tunsigned int neg,zero;\n\tBN_ULONG ln,lo,*p;\n# ifdef BN_COUNT\n\tfprintf(stderr," bn_mul_recursive %d * %d\\n",n2,n2);\n# endif\n# ifdef BN_MUL_COMBA\n# if 0\n\tif (n2 == 4)\n\t\t{\n\t\tbn_mul_comba4(r,a,b);\n\t\treturn;\n\t\t}\n# endif\n\tif (n2 == 8 && dna == 0 && dnb == 0)\n\t\t{\n\t\tbn_mul_comba8(r,a,b);\n\t\treturn;\n\t\t}\n# endif\n\tif (n2 < BN_MUL_RECURSIVE_SIZE_NORMAL)\n\t\t{\n\t\tbn_mul_normal(r,a,n2+dna,b,n2+dnb);\n\t\tif ((dna + dnb) < 0)\n\t\t\tmemset(&r[2*n2 + dna + dnb], 0,\n\t\t\t\tsizeof(BN_ULONG) * -(dna + dnb));\n\t\treturn;\n\t\t}\n\tc1=bn_cmp_part_words(a,&(a[n]),tna,n-tna);\n\tc2=bn_cmp_part_words(&(b[n]),b,tnb,tnb-n);\n\tzero=neg=0;\n\tswitch (c1*3+c2)\n\t\t{\n\tcase -4:\n\t\tbn_sub_part_words(t, &(a[n]),a, tna,tna-n);\n\t\tbn_sub_part_words(&(t[n]),b, &(b[n]),tnb,n-tnb);\n\t\tbreak;\n\tcase -3:\n\t\tzero=1;\n\t\tbreak;\n\tcase -2:\n\t\tbn_sub_part_words(t, &(a[n]),a, tna,tna-n);\n\t\tbn_sub_part_words(&(t[n]),&(b[n]),b, tnb,tnb-n);\n\t\tneg=1;\n\t\tbreak;\n\tcase -1:\n\tcase 0:\n\tcase 1:\n\t\tzero=1;\n\t\tbreak;\n\tcase 2:\n\t\tbn_sub_part_words(t, a, &(a[n]),tna,n-tna);\n\t\tbn_sub_part_words(&(t[n]),b, &(b[n]),tnb,n-tnb);\n\t\tneg=1;\n\t\tbreak;\n\tcase 3:\n\t\tzero=1;\n\t\tbreak;\n\tcase 4:\n\t\tbn_sub_part_words(t, a, &(a[n]),tna,n-tna);\n\t\tbn_sub_part_words(&(t[n]),&(b[n]),b, tnb,tnb-n);\n\t\tbreak;\n\t\t}\n# ifdef BN_MUL_COMBA\n\tif (n == 4 && dna == 0 && dnb == 0)\n\t\t{\n\t\tif (!zero)\n\t\t\tbn_mul_comba4(&(t[n2]),t,&(t[n]));\n\t\telse\n\t\t\tmemset(&(t[n2]),0,8*sizeof(BN_ULONG));\n\t\tbn_mul_comba4(r,a,b);\n\t\tbn_mul_comba4(&(r[n2]),&(a[n]),&(b[n]));\n\t\t}\n\telse if (n == 8 && dna == 0 && dnb == 0)\n\t\t{\n\t\tif (!zero)\n\t\t\tbn_mul_comba8(&(t[n2]),t,&(t[n]));\n\t\telse\n\t\t\tmemset(&(t[n2]),0,16*sizeof(BN_ULONG));\n\t\tbn_mul_comba8(r,a,b);\n\t\tbn_mul_comba8(&(r[n2]),&(a[n]),&(b[n]));\n\t\t}\n\telse\n# endif\n\t\t{\n\t\tp= &(t[n2*2]);\n\t\tif (!zero)\n\t\t\tbn_mul_recursive(&(t[n2]),t,&(t[n]),n,0,0,p);\n\t\telse\n\t\t\tmemset(&(t[n2]),0,n2*sizeof(BN_ULONG));\n\t\tbn_mul_recursive(r,a,b,n,0,0,p);\n\t\tbn_mul_recursive(&(r[n2]),&(a[n]),&(b[n]),n,dna,dnb,p);\n\t\t}\n\tc1=(int)(bn_add_words(t,r,&(r[n2]),n2));\n\tif (neg)\n\t\t{\n\t\tc1-=(int)(bn_sub_words(&(t[n2]),t,&(t[n2]),n2));\n\t\t}\n\telse\n\t\t{\n\t\tc1+=(int)(bn_add_words(&(t[n2]),&(t[n2]),t,n2));\n\t\t}\n\tc1+=(int)(bn_add_words(&(r[n]),&(r[n]),&(t[n2]),n2));\n\tif (c1)\n\t\t{\n\t\tp= &(r[n+n2]);\n\t\tlo= *p;\n\t\tln=(lo+c1)&BN_MASK2;\n\t\t*p=ln;\n\t\tif (ln < (BN_ULONG)c1)\n\t\t\t{\n\t\t\tdo\t{\n\t\t\t\tp++;\n\t\t\t\tlo= *p;\n\t\t\t\tln=(lo+1)&BN_MASK2;\n\t\t\t\t*p=ln;\n\t\t\t\t} while (ln == 0);\n\t\t\t}\n\t\t}\n\t}', 'BN_ULONG bn_add_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b, int n)\n {\n\tBN_ULONG c,l,t;\n\tassert(n >= 0);\n\tif (n <= 0) return((BN_ULONG)0);\n\tc=0;\n#ifndef OPENSSL_SMALL_FOOTPRINT\n\twhile (n&~3)\n\t\t{\n\t\tt=a[0];\n\t\tt=(t+c)&BN_MASK2;\n\t\tc=(t < c);\n\t\tl=(t+b[0])&BN_MASK2;\n\t\tc+=(l < t);\n\t\tr[0]=l;\n\t\tt=a[1];\n\t\tt=(t+c)&BN_MASK2;\n\t\tc=(t < c);\n\t\tl=(t+b[1])&BN_MASK2;\n\t\tc+=(l < t);\n\t\tr[1]=l;\n\t\tt=a[2];\n\t\tt=(t+c)&BN_MASK2;\n\t\tc=(t < c);\n\t\tl=(t+b[2])&BN_MASK2;\n\t\tc+=(l < t);\n\t\tr[2]=l;\n\t\tt=a[3];\n\t\tt=(t+c)&BN_MASK2;\n\t\tc=(t < c);\n\t\tl=(t+b[3])&BN_MASK2;\n\t\tc+=(l < t);\n\t\tr[3]=l;\n\t\ta+=4; b+=4; r+=4; n-=4;\n\t\t}\n#endif\n\twhile(n)\n\t\t{\n\t\tt=a[0];\n\t\tt=(t+c)&BN_MASK2;\n\t\tc=(t < c);\n\t\tl=(t+b[0])&BN_MASK2;\n\t\tc+=(l < t);\n\t\tr[0]=l;\n\t\ta++; b++; r++; n--;\n\t\t}\n\treturn((BN_ULONG)c);\n\t}']
834
0
https://github.com/openssl/openssl/blob/305b68f1a2b6d4d0aa07a6ab47ac372f067a40bb/crypto/bn/bn_lib.c/#L231
static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words) { BN_ULONG *a = NULL; if (words > (INT_MAX / (4 * BN_BITS2))) { BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG); return NULL; } if (BN_get_flags(b, BN_FLG_STATIC_DATA)) { BNerr(BN_F_BN_EXPAND_INTERNAL, 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) { BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE); return NULL; } assert(b->top <= words); if (b->top > 0) memcpy(a, b->d, sizeof(*a) * b->top); return a; }
['BIGNUM *BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret)\n{\n unsigned int i, m;\n unsigned int n;\n BN_ULONG l;\n BIGNUM *bn = NULL;\n if (ret == NULL)\n ret = bn = BN_new();\n if (ret == NULL)\n return NULL;\n bn_check_top(ret);\n for ( ; len > 0 && *s == 0; s++, len--)\n continue;\n n = len;\n if (n == 0) {\n ret->top = 0;\n return ret;\n }\n i = ((n - 1) / BN_BYTES) + 1;\n m = ((n - 1) % (BN_BYTES));\n if (bn_wexpand(ret, (int)i) == NULL) {\n BN_free(bn);\n return NULL;\n }\n ret->top = i;\n ret->neg = 0;\n l = 0;\n while (n--) {\n l = (l << 8L) | *(s++);\n if (m-- == 0) {\n ret->d[--i] = l;\n l = 0;\n m = BN_BYTES - 1;\n }\n }\n bn_correct_top(ret);\n return ret;\n}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n return (words <= a->dmax) ? a : bn_expand2(a, words);\n}', 'BIGNUM *bn_expand2(BIGNUM *b, int words)\n{\n if (words > b->dmax) {\n BN_ULONG *a = bn_expand_internal(b, words);\n if (!a)\n return NULL;\n if (b->d) {\n OPENSSL_cleanse(b->d, b->dmax * sizeof(b->d[0]));\n bn_free_d(b);\n }\n b->d = a;\n b->dmax = words;\n }\n return b;\n}', 'static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)\n{\n BN_ULONG *a = NULL;\n if (words > (INT_MAX / (4 * BN_BITS2))) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);\n return NULL;\n }\n if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);\n return NULL;\n }\n if (BN_get_flags(b, BN_FLG_SECURE))\n a = OPENSSL_secure_zalloc(words * sizeof(*a));\n else\n a = OPENSSL_zalloc(words * sizeof(*a));\n if (a == NULL) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);\n return NULL;\n }\n assert(b->top <= words);\n if (b->top > 0)\n memcpy(a, b->d, sizeof(*a) * b->top);\n return a;\n}', 'void *CRYPTO_zalloc(size_t num, const char *file, int line)\n{\n void *ret = CRYPTO_malloc(num, file, line);\n FAILTEST();\n if (ret != NULL)\n memset(ret, 0, num);\n return ret;\n}', 'void *CRYPTO_malloc(size_t num, const char *file, int line)\n{\n void *ret = NULL;\n INCREMENT(malloc_count);\n if (malloc_impl != NULL && malloc_impl != CRYPTO_malloc)\n return malloc_impl(num, file, line);\n if (num == 0)\n return NULL;\n FAILTEST();\n if (allow_customize) {\n allow_customize = 0;\n }\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n if (call_malloc_debug) {\n CRYPTO_mem_debug_malloc(NULL, num, 0, file, line);\n ret = malloc(num);\n CRYPTO_mem_debug_malloc(ret, num, 1, file, line);\n } else {\n ret = malloc(num);\n }\n#else\n (void)(file); (void)(line);\n ret = malloc(num);\n#endif\n return ret;\n}']
835
0
https://github.com/libav/libav/blob/4cd19f6e7851ee6afb08eb346c82d5574fa2b699/libavcodec/ra144.c/#L315
static int ra144_decode_frame(AVCodecContext * avctx, void *vdata, int *data_size, const uint8_t *buf, int buf_size) { static const uint8_t sizes[10] = {6, 5, 5, 4, 4, 3, 3, 3, 3, 2}; unsigned int refl_rms[4]; uint16_t block_coefs[4][30]; unsigned int lpc_refl[10]; int i, j; int16_t *data = vdata; unsigned int energy; RA144Context *ractx = avctx->priv_data; GetBitContext gb; if (*data_size < 2*160) return -1; if(buf_size < 20) { av_log(avctx, AV_LOG_ERROR, "Frame too small (%d bytes). Truncated file?\n", buf_size); *data_size = 0; return buf_size; } init_get_bits(&gb, buf, 20 * 8); for (i=0; i<10; i++) lpc_refl[i] = lpc_refl_cb[i][get_bits(&gb, sizes[i])]; eval_coefs(ractx->lpc_coef[0], lpc_refl); ractx->lpc_refl_rms[0] = rms(lpc_refl); energy = energy_tab[get_bits(&gb, 5)]; refl_rms[0] = interp(ractx, block_coefs[0], 1, 1, ractx->old_energy); refl_rms[1] = interp(ractx, block_coefs[1], 2, energy <= ractx->old_energy, t_sqrt(energy*ractx->old_energy) >> 12); refl_rms[2] = interp(ractx, block_coefs[2], 3, 0, energy); refl_rms[3] = rescale_rms(ractx->lpc_refl_rms[0], energy); int_to_int16(block_coefs[3], ractx->lpc_coef[0]); for (i=0; i < 4; i++) { do_output_subblock(ractx, block_coefs[i], refl_rms[i], &gb); for (j=0; j < BLOCKSIZE; j++) *data++ = av_clip_int16(ractx->curr_sblock[j + 10] << 2); } ractx->old_energy = energy; ractx->lpc_refl_rms[1] = ractx->lpc_refl_rms[0]; FFSWAP(unsigned int *, ractx->lpc_coef[0], ractx->lpc_coef[1]); *data_size = 2*160; return 20; }
['static int ra144_decode_frame(AVCodecContext * avctx, void *vdata,\n int *data_size, const uint8_t *buf, int buf_size)\n{\n static const uint8_t sizes[10] = {6, 5, 5, 4, 4, 3, 3, 3, 3, 2};\n unsigned int refl_rms[4];\n uint16_t block_coefs[4][30];\n unsigned int lpc_refl[10];\n int i, j;\n int16_t *data = vdata;\n unsigned int energy;\n RA144Context *ractx = avctx->priv_data;\n GetBitContext gb;\n if (*data_size < 2*160)\n return -1;\n if(buf_size < 20) {\n av_log(avctx, AV_LOG_ERROR,\n "Frame too small (%d bytes). Truncated file?\\n", buf_size);\n *data_size = 0;\n return buf_size;\n }\n init_get_bits(&gb, buf, 20 * 8);\n for (i=0; i<10; i++)\n lpc_refl[i] = lpc_refl_cb[i][get_bits(&gb, sizes[i])];\n eval_coefs(ractx->lpc_coef[0], lpc_refl);\n ractx->lpc_refl_rms[0] = rms(lpc_refl);\n energy = energy_tab[get_bits(&gb, 5)];\n refl_rms[0] = interp(ractx, block_coefs[0], 1, 1, ractx->old_energy);\n refl_rms[1] = interp(ractx, block_coefs[1], 2, energy <= ractx->old_energy,\n t_sqrt(energy*ractx->old_energy) >> 12);\n refl_rms[2] = interp(ractx, block_coefs[2], 3, 0, energy);\n refl_rms[3] = rescale_rms(ractx->lpc_refl_rms[0], energy);\n int_to_int16(block_coefs[3], ractx->lpc_coef[0]);\n for (i=0; i < 4; i++) {\n do_output_subblock(ractx, block_coefs[i], refl_rms[i], &gb);\n for (j=0; j < BLOCKSIZE; j++)\n *data++ = av_clip_int16(ractx->curr_sblock[j + 10] << 2);\n }\n ractx->old_energy = energy;\n ractx->lpc_refl_rms[1] = ractx->lpc_refl_rms[0];\n FFSWAP(unsigned int *, ractx->lpc_coef[0], ractx->lpc_coef[1]);\n *data_size = 2*160;\n return 20;\n}', 'static inline void init_get_bits(GetBitContext *s,\n const uint8_t *buffer, int bit_size)\n{\n int buffer_size= (bit_size+7)>>3;\n if(buffer_size < 0 || bit_size < 0) {\n buffer_size = bit_size = 0;\n buffer = NULL;\n }\n s->buffer= buffer;\n s->size_in_bits= bit_size;\n s->buffer_end= buffer + buffer_size;\n#ifdef ALT_BITSTREAM_READER\n s->index=0;\n#elif defined LIBMPEG2_BITSTREAM_READER\n s->buffer_ptr = (uint8_t*)((intptr_t)buffer&(~1));\n s->bit_count = 16 + 8*((intptr_t)buffer&1);\n skip_bits_long(s, 0);\n#elif defined A32_BITSTREAM_READER\n s->buffer_ptr = (uint32_t*)((intptr_t)buffer&(~3));\n s->bit_count = 32 + 8*((intptr_t)buffer&3);\n skip_bits_long(s, 0);\n#endif\n}', 'static inline unsigned int get_bits(GetBitContext *s, int n){\n register int tmp;\n OPEN_READER(re, s)\n UPDATE_CACHE(re, s)\n tmp= SHOW_UBITS(re, s, n);\n LAST_SKIP_BITS(re, s, n)\n CLOSE_READER(re, s)\n return tmp;\n}']
836
0
https://github.com/openssl/openssl/blob/2d5d70b15559f9813054ddb11b30b816daf62ebe/crypto/bn/bn_ctx.c/#L332
static unsigned int BN_STACK_pop(BN_STACK *st) { return st->indexes[--(st->depth)]; }
['int gost2001_compute_public(EC_KEY *ec)\n{\n const EC_GROUP *group = EC_KEY_get0_group(ec);\n EC_POINT *pub_key = NULL;\n const BIGNUM *priv_key = NULL;\n BN_CTX *ctx = NULL;\n int ok = 0;\n if (!group) {\n GOSTerr(GOST_F_GOST2001_COMPUTE_PUBLIC,\n GOST_R_KEY_IS_NOT_INITIALIZED);\n return 0;\n }\n ctx = BN_CTX_new();\n if (!ctx) {\n GOSTerr(GOST_F_GOST2001_COMPUTE_PUBLIC, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n BN_CTX_start(ctx);\n if ((priv_key = EC_KEY_get0_private_key(ec)) == NULL) {\n GOSTerr(GOST_F_GOST2001_COMPUTE_PUBLIC, ERR_R_EC_LIB);\n goto err;\n }\n pub_key = EC_POINT_new(group);\n if (!pub_key) {\n GOSTerr(GOST_F_GOST2001_COMPUTE_PUBLIC, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n if (!EC_POINT_mul(group, pub_key, priv_key, NULL, NULL, ctx)) {\n GOSTerr(GOST_F_GOST2001_COMPUTE_PUBLIC, ERR_R_EC_LIB);\n goto err;\n }\n if (!EC_KEY_set_public_key(ec, pub_key)) {\n GOSTerr(GOST_F_GOST2001_COMPUTE_PUBLIC, ERR_R_EC_LIB);\n goto err;\n }\n ok = 256;\n err:\n EC_POINT_free(pub_key);\n if (ctx)\n BN_CTX_end(ctx);\n BN_CTX_free(ctx);\n return ok;\n}', 'void BN_CTX_start(BN_CTX *ctx)\n{\n CTXDBG_ENTRY("BN_CTX_start", ctx);\n if (ctx->err_stack || ctx->too_many)\n ctx->err_stack++;\n else if (!BN_STACK_push(&ctx->stack, ctx->used)) {\n BNerr(BN_F_BN_CTX_START, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n ctx->err_stack++;\n }\n CTXDBG_EXIT(ctx);\n}', 'void BN_CTX_end(BN_CTX *ctx)\n{\n CTXDBG_ENTRY("BN_CTX_end", ctx);\n if (ctx->err_stack)\n ctx->err_stack--;\n else {\n unsigned int fp = BN_STACK_pop(&ctx->stack);\n if (fp < ctx->used)\n BN_POOL_release(&ctx->pool, ctx->used - fp);\n ctx->used = fp;\n ctx->too_many = 0;\n }\n CTXDBG_EXIT(ctx);\n}', 'static unsigned int BN_STACK_pop(BN_STACK *st)\n{\n return st->indexes[--(st->depth)];\n}']
837
0
https://github.com/openssl/openssl/blob/848113a30b431c2fe21ae8de2a366b9b6146fb92/crypto/bn/bn_sqr.c/#L120
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); }
['static int file_modsqrt(STANZA *s)\n{\n BIGNUM *a = NULL, *p = NULL, *mod_sqrt = NULL, *ret = NULL, *ret2 = NULL;\n int st = 0;\n if (!TEST_ptr(a = getBN(s, "A"))\n || !TEST_ptr(p = getBN(s, "P"))\n || !TEST_ptr(mod_sqrt = getBN(s, "ModSqrt"))\n || !TEST_ptr(ret = BN_new())\n || !TEST_ptr(ret2 = BN_new()))\n goto err;\n if (!TEST_true(BN_mod_sqrt(ret, a, p, ctx))\n || !TEST_true(BN_sub(ret2, p, ret)))\n goto err;\n if (BN_cmp(ret2, mod_sqrt) != 0\n && !equalBN("sqrt(A) (mod P)", mod_sqrt, ret))\n goto err;\n st = 1;\nerr:\n BN_free(a);\n BN_free(p);\n BN_free(mod_sqrt);\n BN_free(ret);\n BN_free(ret2);\n return st;\n}', 'static BIGNUM *getBN(STANZA *s, const char *attribute)\n{\n const char *hex;\n BIGNUM *ret = NULL;\n if ((hex = findattr(s, attribute)) == NULL) {\n TEST_error("%s:%d: Can\'t find %s", s->test_file, s->start, attribute);\n return NULL;\n }\n if (parseBN(&ret, hex) != (int)strlen(hex)) {\n TEST_error("Could not decode \'%s\'", hex);\n return NULL;\n }\n return ret;\n}', 'static int parseBN(BIGNUM **out, const char *in)\n{\n *out = NULL;\n return BN_hex2bn(out, in);\n}', "int BN_hex2bn(BIGNUM **bn, const char *a)\n{\n BIGNUM *ret = NULL;\n BN_ULONG l = 0;\n int neg = 0, h, m, i, j, k, c;\n int num;\n if (a == NULL || *a == '\\0')\n return 0;\n if (*a == '-') {\n neg = 1;\n a++;\n }\n for (i = 0; i <= INT_MAX / 4 && ossl_isxdigit(a[i]); i++)\n continue;\n if (i == 0 || i > INT_MAX / 4)\n goto err;\n num = i + neg;\n if (bn == NULL)\n return num;\n if (*bn == NULL) {\n if ((ret = BN_new()) == NULL)\n return 0;\n } else {\n ret = *bn;\n BN_zero(ret);\n }\n if (bn_expand(ret, i * 4) == NULL)\n goto err;\n j = i;\n m = 0;\n h = 0;\n while (j > 0) {\n m = (BN_BYTES * 2 <= j) ? BN_BYTES * 2 : j;\n l = 0;\n for (;;) {\n c = a[j - m];\n k = OPENSSL_hexchar2int(c);\n if (k < 0)\n k = 0;\n l = (l << 4) | k;\n if (--m <= 0) {\n ret->d[h++] = l;\n break;\n }\n }\n j -= BN_BYTES * 2;\n }\n ret->top = h;\n bn_correct_top(ret);\n *bn = ret;\n bn_check_top(ret);\n if (ret->top != 0)\n ret->neg = neg;\n return num;\n err:\n if (*bn == NULL)\n BN_free(ret);\n return 0;\n}", 'BIGNUM *BN_mod_sqrt(BIGNUM *in, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx)\n{\n BIGNUM *ret = in;\n int err = 1;\n int r;\n BIGNUM *A, *b, *q, *t, *x, *y;\n int e, i, j;\n if (!BN_is_odd(p) || BN_abs_is_word(p, 1)) {\n if (BN_abs_is_word(p, 2)) {\n if (ret == NULL)\n ret = BN_new();\n if (ret == NULL)\n goto end;\n if (!BN_set_word(ret, BN_is_bit_set(a, 0))) {\n if (ret != in)\n BN_free(ret);\n return NULL;\n }\n bn_check_top(ret);\n return ret;\n }\n BNerr(BN_F_BN_MOD_SQRT, BN_R_P_IS_NOT_PRIME);\n return NULL;\n }\n if (BN_is_zero(a) || BN_is_one(a)) {\n if (ret == NULL)\n ret = BN_new();\n if (ret == NULL)\n goto end;\n if (!BN_set_word(ret, BN_is_one(a))) {\n if (ret != in)\n BN_free(ret);\n return NULL;\n }\n bn_check_top(ret);\n return ret;\n }\n BN_CTX_start(ctx);\n A = BN_CTX_get(ctx);\n b = BN_CTX_get(ctx);\n q = BN_CTX_get(ctx);\n t = BN_CTX_get(ctx);\n x = BN_CTX_get(ctx);\n y = BN_CTX_get(ctx);\n if (y == NULL)\n goto end;\n if (ret == NULL)\n ret = BN_new();\n if (ret == NULL)\n goto end;\n if (!BN_nnmod(A, a, p, ctx))\n goto end;\n e = 1;\n while (!BN_is_bit_set(p, e))\n e++;\n if (e == 1) {\n if (!BN_rshift(q, p, 2))\n goto end;\n q->neg = 0;\n if (!BN_add_word(q, 1))\n goto end;\n if (!BN_mod_exp(ret, A, q, p, ctx))\n goto end;\n err = 0;\n goto vrfy;\n }\n if (e == 2) {\n if (!BN_mod_lshift1_quick(t, A, p))\n goto end;\n if (!BN_rshift(q, p, 3))\n goto end;\n q->neg = 0;\n if (!BN_mod_exp(b, t, q, p, ctx))\n goto end;\n if (!BN_mod_sqr(y, b, p, ctx))\n goto end;\n if (!BN_mod_mul(t, t, y, p, ctx))\n goto end;\n if (!BN_sub_word(t, 1))\n goto end;\n if (!BN_mod_mul(x, A, b, p, ctx))\n goto end;\n if (!BN_mod_mul(x, x, t, p, ctx))\n goto end;\n if (!BN_copy(ret, x))\n goto end;\n err = 0;\n goto vrfy;\n }\n if (!BN_copy(q, p))\n goto end;\n q->neg = 0;\n i = 2;\n do {\n if (i < 22) {\n if (!BN_set_word(y, i))\n goto end;\n } else {\n if (!BN_priv_rand(y, BN_num_bits(p), 0, 0))\n goto end;\n if (BN_ucmp(y, p) >= 0) {\n if (!(p->neg ? BN_add : BN_sub) (y, y, p))\n goto end;\n }\n if (BN_is_zero(y))\n if (!BN_set_word(y, i))\n goto end;\n }\n r = BN_kronecker(y, q, ctx);\n if (r < -1)\n goto end;\n if (r == 0) {\n BNerr(BN_F_BN_MOD_SQRT, BN_R_P_IS_NOT_PRIME);\n goto end;\n }\n }\n while (r == 1 && ++i < 82);\n if (r != -1) {\n BNerr(BN_F_BN_MOD_SQRT, BN_R_TOO_MANY_ITERATIONS);\n goto end;\n }\n if (!BN_rshift(q, q, e))\n goto end;\n if (!BN_mod_exp(y, y, q, p, ctx))\n goto end;\n if (BN_is_one(y)) {\n BNerr(BN_F_BN_MOD_SQRT, BN_R_P_IS_NOT_PRIME);\n goto end;\n }\n if (!BN_rshift1(t, q))\n goto end;\n if (BN_is_zero(t)) {\n if (!BN_nnmod(t, A, p, ctx))\n goto end;\n if (BN_is_zero(t)) {\n BN_zero(ret);\n err = 0;\n goto end;\n } else if (!BN_one(x))\n goto end;\n } else {\n if (!BN_mod_exp(x, A, t, p, ctx))\n goto end;\n if (BN_is_zero(x)) {\n BN_zero(ret);\n err = 0;\n goto end;\n }\n }\n if (!BN_mod_sqr(b, x, p, ctx))\n goto end;\n if (!BN_mod_mul(b, b, A, p, ctx))\n goto end;\n if (!BN_mod_mul(x, x, A, p, ctx))\n goto end;\n while (1) {\n if (BN_is_one(b)) {\n if (!BN_copy(ret, x))\n goto end;\n err = 0;\n goto vrfy;\n }\n i = 1;\n if (!BN_mod_sqr(t, b, p, ctx))\n goto end;\n while (!BN_is_one(t)) {\n i++;\n if (i == e) {\n BNerr(BN_F_BN_MOD_SQRT, BN_R_NOT_A_SQUARE);\n goto end;\n }\n if (!BN_mod_mul(t, t, t, p, ctx))\n goto end;\n }\n if (!BN_copy(t, y))\n goto end;\n for (j = e - i - 1; j > 0; j--) {\n if (!BN_mod_sqr(t, t, p, ctx))\n goto end;\n }\n if (!BN_mod_mul(y, t, t, p, ctx))\n goto end;\n if (!BN_mod_mul(x, x, t, p, ctx))\n goto end;\n if (!BN_mod_mul(b, b, y, p, ctx))\n goto end;\n e = i;\n }\n vrfy:\n if (!err) {\n if (!BN_mod_sqr(x, ret, p, ctx))\n err = 1;\n if (!err && 0 != BN_cmp(x, A)) {\n BNerr(BN_F_BN_MOD_SQRT, BN_R_NOT_A_SQUARE);\n err = 1;\n }\n }\n end:\n if (err) {\n if (ret != in)\n BN_clear_free(ret);\n ret = NULL;\n }\n BN_CTX_end(ctx);\n bn_check_top(ret);\n return ret;\n}', 'int BN_is_odd(const BIGNUM *a)\n{\n return (a->top > 0) && (a->d[0] & 1);\n}', 'int BN_abs_is_word(const BIGNUM *a, const BN_ULONG w)\n{\n return ((a->top == 1) && (a->d[0] == w)) || ((w == 0) && (a->top == 0));\n}', 'int BN_nnmod(BIGNUM *r, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx)\n{\n if (!(BN_mod(r, m, d, ctx)))\n return 0;\n if (!r->neg)\n return 1;\n return (d->neg ? BN_sub : BN_add) (r, r, d);\n}', 'int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,\n BN_CTX *ctx)\n{\n int norm_shift, i, loop;\n BIGNUM *tmp, wnum, *snum, *sdiv, *res;\n BN_ULONG *resp, *wnump;\n BN_ULONG d0, d1;\n int num_n, div_n;\n int no_branch = 0;\n if ((num->top > 0 && num->d[num->top - 1] == 0) ||\n (divisor->top > 0 && divisor->d[divisor->top - 1] == 0)) {\n BNerr(BN_F_BN_DIV, BN_R_NOT_INITIALIZED);\n return 0;\n }\n bn_check_top(num);\n bn_check_top(divisor);\n if ((BN_get_flags(num, BN_FLG_CONSTTIME) != 0)\n || (BN_get_flags(divisor, BN_FLG_CONSTTIME) != 0)) {\n no_branch = 1;\n }\n bn_check_top(dv);\n bn_check_top(rm);\n if (BN_is_zero(divisor)) {\n BNerr(BN_F_BN_DIV, BN_R_DIV_BY_ZERO);\n return 0;\n }\n if (!no_branch && BN_ucmp(num, divisor) < 0) {\n if (rm != NULL) {\n if (BN_copy(rm, num) == NULL)\n return 0;\n }\n if (dv != NULL)\n BN_zero(dv);\n return 1;\n }\n BN_CTX_start(ctx);\n res = (dv == NULL) ? BN_CTX_get(ctx) : dv;\n tmp = BN_CTX_get(ctx);\n snum = BN_CTX_get(ctx);\n sdiv = BN_CTX_get(ctx);\n if (sdiv == NULL)\n goto err;\n norm_shift = BN_BITS2 - ((BN_num_bits(divisor)) % BN_BITS2);\n if (!(BN_lshift(sdiv, divisor, norm_shift)))\n goto err;\n sdiv->neg = 0;\n norm_shift += BN_BITS2;\n if (!(BN_lshift(snum, num, norm_shift)))\n goto err;\n snum->neg = 0;\n if (no_branch) {\n if (snum->top <= sdiv->top + 1) {\n if (bn_wexpand(snum, sdiv->top + 2) == NULL)\n goto err;\n for (i = snum->top; i < sdiv->top + 2; i++)\n snum->d[i] = 0;\n snum->top = sdiv->top + 2;\n } else {\n if (bn_wexpand(snum, snum->top + 1) == NULL)\n goto err;\n snum->d[snum->top] = 0;\n snum->top++;\n }\n }\n div_n = sdiv->top;\n num_n = snum->top;\n loop = num_n - div_n;\n wnum.neg = 0;\n wnum.d = &(snum->d[loop]);\n wnum.top = div_n;\n wnum.dmax = snum->dmax - loop;\n d0 = sdiv->d[div_n - 1];\n d1 = (div_n == 1) ? 0 : sdiv->d[div_n - 2];\n wnump = &(snum->d[num_n - 1]);\n if (!bn_wexpand(res, (loop + 1)))\n goto err;\n res->neg = (num->neg ^ divisor->neg);\n res->top = loop - no_branch;\n resp = &(res->d[loop - 1]);\n if (!bn_wexpand(tmp, (div_n + 1)))\n goto err;\n if (!no_branch) {\n if (BN_ucmp(&wnum, sdiv) >= 0) {\n bn_clear_top2max(&wnum);\n bn_sub_words(wnum.d, wnum.d, sdiv->d, div_n);\n *resp = 1;\n } else\n res->top--;\n }\n resp++;\n if (res->top == 0)\n res->neg = 0;\n else\n resp--;\n for (i = 0; i < loop - 1; i++, wnump--) {\n BN_ULONG q, l0;\n# if defined(BN_DIV3W) && !defined(OPENSSL_NO_ASM)\n BN_ULONG bn_div_3_words(BN_ULONG *, BN_ULONG, BN_ULONG);\n q = bn_div_3_words(wnump, d1, d0);\n# else\n BN_ULONG n0, n1, rem = 0;\n n0 = wnump[0];\n n1 = wnump[-1];\n if (n0 == d0)\n q = BN_MASK2;\n else {\n# ifdef BN_LLONG\n BN_ULLONG t2;\n# if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)\n q = (BN_ULONG)(((((BN_ULLONG) n0) << BN_BITS2) | n1) / d0);\n# else\n q = bn_div_words(n0, n1, d0);\n# endif\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n t2 = (BN_ULLONG) d1 *q;\n for (;;) {\n if (t2 <= ((((BN_ULLONG) rem) << BN_BITS2) | wnump[-2]))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n t2 -= d1;\n }\n# else\n BN_ULONG t2l, t2h;\n q = bn_div_words(n0, n1, d0);\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n# if defined(BN_UMULT_LOHI)\n BN_UMULT_LOHI(t2l, t2h, d1, q);\n# elif defined(BN_UMULT_HIGH)\n t2l = d1 * q;\n t2h = BN_UMULT_HIGH(d1, q);\n# else\n {\n BN_ULONG ql, qh;\n t2l = LBITS(d1);\n t2h = HBITS(d1);\n ql = LBITS(q);\n qh = HBITS(q);\n mul64(t2l, t2h, ql, qh);\n }\n# endif\n for (;;) {\n if ((t2h < rem) || ((t2h == rem) && (t2l <= wnump[-2])))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n if (t2l < d1)\n t2h--;\n t2l -= d1;\n }\n# endif\n }\n# endif\n l0 = bn_mul_words(tmp->d, sdiv->d, div_n, q);\n tmp->d[div_n] = l0;\n wnum.d--;\n if (bn_sub_words(wnum.d, wnum.d, tmp->d, div_n + 1)) {\n q--;\n if (bn_add_words(wnum.d, wnum.d, sdiv->d, div_n))\n (*wnump)++;\n }\n resp--;\n *resp = q;\n }\n bn_correct_top(snum);\n if (rm != NULL) {\n int neg = num->neg;\n BN_rshift(rm, snum, norm_shift);\n if (!BN_is_zero(rm))\n rm->neg = neg;\n bn_check_top(rm);\n }\n if (no_branch)\n bn_correct_top(res);\n BN_CTX_end(ctx);\n return 1;\n err:\n bn_check_top(rm);\n BN_CTX_end(ctx);\n return 0;\n}', 'int BN_is_bit_set(const BIGNUM *a, int n)\n{\n int i, j;\n bn_check_top(a);\n if (n < 0)\n return 0;\n i = n / BN_BITS2;\n j = n % BN_BITS2;\n if (a->top <= i)\n return 0;\n return (int)(((a->d[i]) >> j) & ((BN_ULONG)1));\n}', 'int BN_rshift(BIGNUM *r, const BIGNUM *a, int n)\n{\n int i, j, nw, lb, rb;\n BN_ULONG *t, *f;\n BN_ULONG l, tmp;\n bn_check_top(r);\n bn_check_top(a);\n if (n < 0) {\n BNerr(BN_F_BN_RSHIFT, BN_R_INVALID_SHIFT);\n return 0;\n }\n nw = n / BN_BITS2;\n rb = n % BN_BITS2;\n lb = BN_BITS2 - rb;\n if (nw >= a->top || a->top == 0) {\n BN_zero(r);\n return 1;\n }\n i = (BN_num_bits(a) - n + (BN_BITS2 - 1)) / BN_BITS2;\n if (r != a) {\n if (bn_wexpand(r, i) == NULL)\n return 0;\n r->neg = a->neg;\n } else {\n if (n == 0)\n return 1;\n }\n f = &(a->d[nw]);\n t = r->d;\n j = a->top - nw;\n r->top = i;\n if (rb == 0) {\n for (i = j; i != 0; i--)\n *(t++) = *(f++);\n } else {\n l = *(f++);\n for (i = j - 1; i != 0; i--) {\n tmp = (l >> rb) & BN_MASK2;\n l = *(f++);\n *(t++) = (tmp | (l << lb)) & BN_MASK2;\n }\n if ((l = (l >> rb) & BN_MASK2))\n *(t) = l;\n }\n if (!r->top)\n r->neg = 0;\n bn_check_top(r);\n return 1;\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_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 || BN_get_flags(a, BN_FLG_CONSTTIME) != 0\n || BN_get_flags(m, 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_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 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 (val[0] == 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 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, wmask, window0;\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 = p->top * BN_BITS2;\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 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 (!a->neg) {\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 }\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 powerbufLen += top * sizeof(mont->N.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_nnmod(&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 *n0 = mont->n0, *np;\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 for (np = am.d + top, i = 0; i < top; i++)\n np[i] = mont->N.d[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, np, 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, np, 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, np, 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, np, 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, np, n0, top, wvalue);\n }\n }\n ret = bn_from_montgomery(tmp.d, tmp.d, NULL, np, 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, window))\n goto err;\n if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&am, top, powerbuf, 1, window))\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(&tmp, top, powerbuf, 2,\n window))\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(&tmp, top, powerbuf, i,\n window))\n goto err;\n }\n }\n window0 = (bits - 1) % window + 1;\n wmask = (1 << window0) - 1;\n bits -= window0;\n wvalue = bn_get_bits(p, bits) & wmask;\n if (!MOD_EXP_CTIME_COPY_FROM_PREBUF(&tmp, top, powerbuf, wvalue,\n window))\n goto err;\n wmask = (1 << window) - 1;\n while (bits > 0) {\n for (i = 0; i < window; i++)\n if (!BN_mod_mul_montgomery(&tmp, &tmp, &tmp, mont, ctx))\n goto err;\n bits -= window;\n wvalue = bn_get_bits(p, bits) & wmask;\n if (!MOD_EXP_CTIME_COPY_FROM_PREBUF(&am, top, powerbuf, wvalue,\n window))\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}', 'static int MOD_EXP_CTIME_COPY_FROM_PREBUF(BIGNUM *b, int top,\n unsigned char *buf, int idx,\n int window)\n{\n int i, j;\n int width = 1 << window;\n volatile BN_ULONG *table = (volatile BN_ULONG *)buf;\n if (bn_wexpand(b, top) == NULL)\n return 0;\n if (window <= 3) {\n for (i = 0; i < top; i++, table += width) {\n BN_ULONG acc = 0;\n for (j = 0; j < width; j++) {\n acc |= table[j] &\n ((BN_ULONG)0 - (constant_time_eq_int(j,idx)&1));\n }\n b->d[i] = acc;\n }\n } else {\n int xstride = 1 << (window - 2);\n BN_ULONG y0, y1, y2, y3;\n i = idx >> (window - 2);\n idx &= xstride - 1;\n y0 = (BN_ULONG)0 - (constant_time_eq_int(i,0)&1);\n y1 = (BN_ULONG)0 - (constant_time_eq_int(i,1)&1);\n y2 = (BN_ULONG)0 - (constant_time_eq_int(i,2)&1);\n y3 = (BN_ULONG)0 - (constant_time_eq_int(i,3)&1);\n for (i = 0; i < top; i++, table += width) {\n BN_ULONG acc = 0;\n for (j = 0; j < xstride; j++) {\n acc |= ( (table[j + 0 * xstride] & y0) |\n (table[j + 1 * xstride] & y1) |\n (table[j + 2 * xstride] & y2) |\n (table[j + 3 * xstride] & y3) )\n & ((BN_ULONG)0 - (constant_time_eq_int(j,idx)&1));\n }\n b->d[i] = acc;\n }\n }\n b->top = top;\n bn_correct_top(b);\n return 1;\n}', 'void bn_correct_top(BIGNUM *a)\n{\n BN_ULONG *ftl;\n int tmp_top = a->top;\n if (tmp_top > 0) {\n for (ftl = &(a->d[tmp_top]); tmp_top > 0; tmp_top--) {\n ftl--;\n if (*ftl != 0)\n break;\n }\n a->top = tmp_top;\n }\n if (a->top == 0)\n a->neg = 0;\n bn_pollute(a);\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 == NULL || tmp == NULL)\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 (r != rr && BN_copy(r, rr) == NULL)\n goto err;\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}']
838
0
https://github.com/libav/libav/blob/688417399c69aadd4c287bdb0dec82ef8799011c/libavcodec/hevcdsp_template.c/#L901
PUT_HEVC_QPEL_HV(1, 1)
['QPEL(48)', 'PUT_HEVC_QPEL_HV(1, 1)']
839
0
https://github.com/libav/libav/blob/a1c1c7801918c46da5525cfddb99f3467c522b02/libavcodec/rv40.c/#L369
static inline void rv40_adaptive_loop_filter(uint8_t *src, const int step, const int stride, const int dmode, const int lim_q1, const int lim_p1, const int alpha, const int beta, const int beta2, const int chroma, const int edge) { int diff_p1p0[4], diff_q1q0[4], diff_p1p2[4], diff_q1q2[4]; int sum_p1p0 = 0, sum_q1q0 = 0, sum_p1p2 = 0, sum_q1q2 = 0; uint8_t *ptr; int flag_strong0 = 1, flag_strong1 = 1; int filter_p1, filter_q1; int i; int lims; for(i = 0, ptr = src; i < 4; i++, ptr += stride){ diff_p1p0[i] = ptr[-2*step] - ptr[-1*step]; diff_q1q0[i] = ptr[ 1*step] - ptr[ 0*step]; sum_p1p0 += diff_p1p0[i]; sum_q1q0 += diff_q1q0[i]; } filter_p1 = FFABS(sum_p1p0) < (beta<<2); filter_q1 = FFABS(sum_q1q0) < (beta<<2); if(!filter_p1 && !filter_q1) return; for(i = 0, ptr = src; i < 4; i++, ptr += stride){ diff_p1p2[i] = ptr[-2*step] - ptr[-3*step]; diff_q1q2[i] = ptr[ 1*step] - ptr[ 2*step]; sum_p1p2 += diff_p1p2[i]; sum_q1q2 += diff_q1q2[i]; } if(edge){ flag_strong0 = filter_p1 && (FFABS(sum_p1p2) < beta2); flag_strong1 = filter_q1 && (FFABS(sum_q1q2) < beta2); }else{ flag_strong0 = flag_strong1 = 0; } lims = filter_p1 + filter_q1 + ((lim_q1 + lim_p1) >> 1) + 1; if(flag_strong0 && flag_strong1){ for(i = 0; i < 4; i++, src += stride){ int sflag, p0, q0, p1, q1; int t = src[0*step] - src[-1*step]; if(!t) continue; sflag = (alpha * FFABS(t)) >> 7; if(sflag > 1) continue; p0 = (25*src[-3*step] + 26*src[-2*step] + 26*src[-1*step] + 26*src[ 0*step] + 25*src[ 1*step] + rv40_dither_l[dmode + i]) >> 7; q0 = (25*src[-2*step] + 26*src[-1*step] + 26*src[ 0*step] + 26*src[ 1*step] + 25*src[ 2*step] + rv40_dither_r[dmode + i]) >> 7; if(sflag){ p0 = av_clip(p0, src[-1*step] - lims, src[-1*step] + lims); q0 = av_clip(q0, src[ 0*step] - lims, src[ 0*step] + lims); } p1 = (25*src[-4*step] + 26*src[-3*step] + 26*src[-2*step] + 26*p0 + 25*src[ 0*step] + rv40_dither_l[dmode + i]) >> 7; q1 = (25*src[-1*step] + 26*q0 + 26*src[ 1*step] + 26*src[ 2*step] + 25*src[ 3*step] + rv40_dither_r[dmode + i]) >> 7; if(sflag){ p1 = av_clip(p1, src[-2*step] - lims, src[-2*step] + lims); q1 = av_clip(q1, src[ 1*step] - lims, src[ 1*step] + lims); } src[-2*step] = p1; src[-1*step] = p0; src[ 0*step] = q0; src[ 1*step] = q1; if(!chroma){ src[-3*step] = (25*src[-1*step] + 26*src[-2*step] + 51*src[-3*step] + 26*src[-4*step] + 64) >> 7; src[ 2*step] = (25*src[ 0*step] + 26*src[ 1*step] + 51*src[ 2*step] + 26*src[ 3*step] + 64) >> 7; } } }else if(filter_p1 && filter_q1){ for(i = 0; i < 4; i++, src += stride) rv40_weak_loop_filter(src, step, 1, 1, alpha, beta, lims, lim_q1, lim_p1, diff_p1p0[i], diff_q1q0[i], diff_p1p2[i], diff_q1q2[i]); }else{ for(i = 0; i < 4; i++, src += stride) rv40_weak_loop_filter(src, step, filter_p1, filter_q1, alpha, beta, lims>>1, lim_q1>>1, lim_p1>>1, diff_p1p0[i], diff_q1q0[i], diff_p1p2[i], diff_q1q2[i]); } }
['static inline void rv40_adaptive_loop_filter(uint8_t *src, const int step,\n const int stride, const int dmode,\n const int lim_q1, const int lim_p1,\n const int alpha,\n const int beta, const int beta2,\n const int chroma, const int edge)\n{\n int diff_p1p0[4], diff_q1q0[4], diff_p1p2[4], diff_q1q2[4];\n int sum_p1p0 = 0, sum_q1q0 = 0, sum_p1p2 = 0, sum_q1q2 = 0;\n uint8_t *ptr;\n int flag_strong0 = 1, flag_strong1 = 1;\n int filter_p1, filter_q1;\n int i;\n int lims;\n for(i = 0, ptr = src; i < 4; i++, ptr += stride){\n diff_p1p0[i] = ptr[-2*step] - ptr[-1*step];\n diff_q1q0[i] = ptr[ 1*step] - ptr[ 0*step];\n sum_p1p0 += diff_p1p0[i];\n sum_q1q0 += diff_q1q0[i];\n }\n filter_p1 = FFABS(sum_p1p0) < (beta<<2);\n filter_q1 = FFABS(sum_q1q0) < (beta<<2);\n if(!filter_p1 && !filter_q1)\n return;\n for(i = 0, ptr = src; i < 4; i++, ptr += stride){\n diff_p1p2[i] = ptr[-2*step] - ptr[-3*step];\n diff_q1q2[i] = ptr[ 1*step] - ptr[ 2*step];\n sum_p1p2 += diff_p1p2[i];\n sum_q1q2 += diff_q1q2[i];\n }\n if(edge){\n flag_strong0 = filter_p1 && (FFABS(sum_p1p2) < beta2);\n flag_strong1 = filter_q1 && (FFABS(sum_q1q2) < beta2);\n }else{\n flag_strong0 = flag_strong1 = 0;\n }\n lims = filter_p1 + filter_q1 + ((lim_q1 + lim_p1) >> 1) + 1;\n if(flag_strong0 && flag_strong1){\n for(i = 0; i < 4; i++, src += stride){\n int sflag, p0, q0, p1, q1;\n int t = src[0*step] - src[-1*step];\n if(!t) continue;\n sflag = (alpha * FFABS(t)) >> 7;\n if(sflag > 1) continue;\n p0 = (25*src[-3*step] + 26*src[-2*step]\n + 26*src[-1*step]\n + 26*src[ 0*step] + 25*src[ 1*step] + rv40_dither_l[dmode + i]) >> 7;\n q0 = (25*src[-2*step] + 26*src[-1*step]\n + 26*src[ 0*step]\n + 26*src[ 1*step] + 25*src[ 2*step] + rv40_dither_r[dmode + i]) >> 7;\n if(sflag){\n p0 = av_clip(p0, src[-1*step] - lims, src[-1*step] + lims);\n q0 = av_clip(q0, src[ 0*step] - lims, src[ 0*step] + lims);\n }\n p1 = (25*src[-4*step] + 26*src[-3*step]\n + 26*src[-2*step]\n + 26*p0 + 25*src[ 0*step] + rv40_dither_l[dmode + i]) >> 7;\n q1 = (25*src[-1*step] + 26*q0\n + 26*src[ 1*step]\n + 26*src[ 2*step] + 25*src[ 3*step] + rv40_dither_r[dmode + i]) >> 7;\n if(sflag){\n p1 = av_clip(p1, src[-2*step] - lims, src[-2*step] + lims);\n q1 = av_clip(q1, src[ 1*step] - lims, src[ 1*step] + lims);\n }\n src[-2*step] = p1;\n src[-1*step] = p0;\n src[ 0*step] = q0;\n src[ 1*step] = q1;\n if(!chroma){\n src[-3*step] = (25*src[-1*step] + 26*src[-2*step] + 51*src[-3*step] + 26*src[-4*step] + 64) >> 7;\n src[ 2*step] = (25*src[ 0*step] + 26*src[ 1*step] + 51*src[ 2*step] + 26*src[ 3*step] + 64) >> 7;\n }\n }\n }else if(filter_p1 && filter_q1){\n for(i = 0; i < 4; i++, src += stride)\n rv40_weak_loop_filter(src, step, 1, 1, alpha, beta, lims, lim_q1, lim_p1,\n diff_p1p0[i], diff_q1q0[i], diff_p1p2[i], diff_q1q2[i]);\n }else{\n for(i = 0; i < 4; i++, src += stride)\n rv40_weak_loop_filter(src, step, filter_p1, filter_q1,\n alpha, beta, lims>>1, lim_q1>>1, lim_p1>>1,\n diff_p1p0[i], diff_q1q0[i], diff_p1p2[i], diff_q1q2[i]);\n }\n}']
840
0
https://github.com/libav/libav/blob/cbfe5bee2e20df90c581937b2cb4b1535acbf726/ffmpeg.c/#L3249
static void new_subtitle_stream(AVFormatContext *oc) { AVStream *st; AVCodecContext *subtitle_enc; st = av_new_stream(oc, oc->nb_streams); if (!st) { fprintf(stderr, "Could not alloc stream\n"); av_exit(1); } avcodec_get_context_defaults2(st->codec, CODEC_TYPE_SUBTITLE); bitstream_filters[nb_output_files][oc->nb_streams - 1]= subtitle_bitstream_filters; subtitle_bitstream_filters= NULL; subtitle_enc = st->codec; subtitle_enc->codec_type = CODEC_TYPE_SUBTITLE; if(subtitle_codec_tag) subtitle_enc->codec_tag= subtitle_codec_tag; if (subtitle_stream_copy) { st->stream_copy = 1; } else { set_context_opts(avcodec_opts[CODEC_TYPE_SUBTITLE], subtitle_enc, AV_OPT_FLAG_SUBTITLE_PARAM | AV_OPT_FLAG_ENCODING_PARAM); subtitle_enc->codec_id = find_codec_or_die(subtitle_codec_name, CODEC_TYPE_SUBTITLE, 1); output_codecs[nb_ocodecs] = avcodec_find_encoder_by_name(subtitle_codec_name); } nb_ocodecs++; if (subtitle_language) { av_metadata_set(&st->metadata, "language", subtitle_language); av_free(subtitle_language); subtitle_language = NULL; } subtitle_disable = 0; av_freep(&subtitle_codec_name); subtitle_stream_copy = 0; }
['static void new_subtitle_stream(AVFormatContext *oc)\n{\n AVStream *st;\n AVCodecContext *subtitle_enc;\n st = av_new_stream(oc, oc->nb_streams);\n if (!st) {\n fprintf(stderr, "Could not alloc stream\\n");\n av_exit(1);\n }\n avcodec_get_context_defaults2(st->codec, CODEC_TYPE_SUBTITLE);\n bitstream_filters[nb_output_files][oc->nb_streams - 1]= subtitle_bitstream_filters;\n subtitle_bitstream_filters= NULL;\n subtitle_enc = st->codec;\n subtitle_enc->codec_type = CODEC_TYPE_SUBTITLE;\n if(subtitle_codec_tag)\n subtitle_enc->codec_tag= subtitle_codec_tag;\n if (subtitle_stream_copy) {\n st->stream_copy = 1;\n } else {\n set_context_opts(avcodec_opts[CODEC_TYPE_SUBTITLE], subtitle_enc, AV_OPT_FLAG_SUBTITLE_PARAM | AV_OPT_FLAG_ENCODING_PARAM);\n subtitle_enc->codec_id = find_codec_or_die(subtitle_codec_name, CODEC_TYPE_SUBTITLE, 1);\n output_codecs[nb_ocodecs] = avcodec_find_encoder_by_name(subtitle_codec_name);\n }\n nb_ocodecs++;\n if (subtitle_language) {\n av_metadata_set(&st->metadata, "language", subtitle_language);\n av_free(subtitle_language);\n subtitle_language = NULL;\n }\n subtitle_disable = 0;\n av_freep(&subtitle_codec_name);\n subtitle_stream_copy = 0;\n}', 'AVStream *av_new_stream(AVFormatContext *s, int id)\n{\n AVStream *st;\n int i;\n if (s->nb_streams >= MAX_STREAMS)\n return NULL;\n st = av_mallocz(sizeof(AVStream));\n if (!st)\n return NULL;\n st->codec= avcodec_alloc_context();\n if (s->iformat) {\n st->codec->bit_rate = 0;\n }\n st->index = s->nb_streams;\n st->id = id;\n st->start_time = AV_NOPTS_VALUE;\n st->duration = AV_NOPTS_VALUE;\n st->cur_dts = 0;\n st->first_dts = AV_NOPTS_VALUE;\n av_set_pts_info(st, 33, 1, 90000);\n st->last_IP_pts = AV_NOPTS_VALUE;\n for(i=0; i<MAX_REORDER_DELAY+1; i++)\n st->pts_buffer[i]= AV_NOPTS_VALUE;\n st->reference_dts = AV_NOPTS_VALUE;\n st->sample_aspect_ratio = (AVRational){0,1};\n s->streams[s->nb_streams++] = st;\n return st;\n}']
841
0
https://github.com/nginx/nginx/blob/ea9a1d745b5db26cac2d4b6e231ca6c4af0b4e5a/src/http/ngx_http_core_module.c/#L1763
void ngx_http_set_exten(ngx_http_request_t *r) { ngx_int_t i; ngx_str_null(&r->exten); for (i = r->uri.len - 1; i > 1; i--) { if (r->uri.data[i] == '.' && r->uri.data[i - 1] != '/') { r->exten.len = r->uri.len - i - 1; r->exten.data = &r->uri.data[i + 1]; return; } else if (r->uri.data[i] == '/') { return; } } return; }
['static void\nngx_http_upstream_resolve_handler(ngx_resolver_ctx_t *ctx)\n{\n ngx_connection_t *c;\n ngx_http_request_t *r;\n ngx_http_upstream_t *u;\n ngx_http_upstream_resolved_t *ur;\n r = ctx->data;\n c = r->connection;\n u = r->upstream;\n ur = u->resolved;\n ngx_http_set_log_request(c->log, r);\n ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,\n "http upstream resolve: \\"%V?%V\\"", &r->uri, &r->args);\n if (ctx->state) {\n ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,\n "%V could not be resolved (%i: %s)",\n &ctx->name, ctx->state,\n ngx_resolver_strerror(ctx->state));\n ngx_http_upstream_finalize_request(r, u, NGX_HTTP_BAD_GATEWAY);\n goto failed;\n }\n ur->naddrs = ctx->naddrs;\n ur->addrs = ctx->addrs;\n#if (NGX_DEBUG)\n {\n u_char text[NGX_SOCKADDR_STRLEN];\n ngx_str_t addr;\n ngx_uint_t i;\n addr.data = text;\n for (i = 0; i < ctx->naddrs; i++) {\n addr.len = ngx_sock_ntop(ur->addrs[i].sockaddr, ur->addrs[i].socklen,\n text, NGX_SOCKADDR_STRLEN, 0);\n ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,\n "name was resolved to %V", &addr);\n }\n }\n#endif\n if (ngx_http_upstream_create_round_robin_peer(r, ur) != NGX_OK) {\n ngx_http_upstream_finalize_request(r, u,\n NGX_HTTP_INTERNAL_SERVER_ERROR);\n goto failed;\n }\n ngx_resolve_name_done(ctx);\n ur->ctx = NULL;\n u->peer.start_time = ngx_current_msec;\n if (u->conf->next_upstream_tries\n && u->peer.tries > u->conf->next_upstream_tries)\n {\n u->peer.tries = u->conf->next_upstream_tries;\n }\n ngx_http_upstream_connect(r, u);\nfailed:\n ngx_http_run_posted_requests(c);\n}', 'static void\nngx_http_upstream_connect(ngx_http_request_t *r, ngx_http_upstream_t *u)\n{\n ngx_int_t rc;\n ngx_connection_t *c;\n r->connection->log->action = "connecting to upstream";\n if (u->state && u->state->response_time) {\n u->state->response_time = ngx_current_msec - u->state->response_time;\n }\n u->state = ngx_array_push(r->upstream_states);\n if (u->state == NULL) {\n ngx_http_upstream_finalize_request(r, u,\n NGX_HTTP_INTERNAL_SERVER_ERROR);\n return;\n }\n ngx_memzero(u->state, sizeof(ngx_http_upstream_state_t));\n u->state->response_time = ngx_current_msec;\n u->state->connect_time = (ngx_msec_t) -1;\n u->state->header_time = (ngx_msec_t) -1;\n rc = ngx_event_connect_peer(&u->peer);\n ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,\n "http upstream connect: %i", rc);\n if (rc == NGX_ERROR) {\n ngx_http_upstream_finalize_request(r, u,\n NGX_HTTP_INTERNAL_SERVER_ERROR);\n return;\n }\n u->state->peer = u->peer.name;\n if (rc == NGX_BUSY) {\n ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "no live upstreams");\n ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_NOLIVE);\n return;\n }\n if (rc == NGX_DECLINED) {\n ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_ERROR);\n return;\n }\n c = u->peer.connection;\n c->data = r;\n c->write->handler = ngx_http_upstream_handler;\n c->read->handler = ngx_http_upstream_handler;\n u->write_event_handler = ngx_http_upstream_send_request_handler;\n u->read_event_handler = ngx_http_upstream_process_header;\n c->sendfile &= r->connection->sendfile;\n u->output.sendfile = c->sendfile;\n if (c->pool == NULL) {\n c->pool = ngx_create_pool(128, r->connection->log);\n if (c->pool == NULL) {\n ngx_http_upstream_finalize_request(r, u,\n NGX_HTTP_INTERNAL_SERVER_ERROR);\n return;\n }\n }\n c->log = r->connection->log;\n c->pool->log = c->log;\n c->read->log = c->log;\n c->write->log = c->log;\n u->writer.out = NULL;\n u->writer.last = &u->writer.out;\n u->writer.connection = c;\n u->writer.limit = 0;\n if (u->request_sent) {\n if (ngx_http_upstream_reinit(r, u) != NGX_OK) {\n ngx_http_upstream_finalize_request(r, u,\n NGX_HTTP_INTERNAL_SERVER_ERROR);\n return;\n }\n }\n if (r->request_body\n && r->request_body->buf\n && r->request_body->temp_file\n && r == r->main)\n {\n u->output.free = ngx_alloc_chain_link(r->pool);\n if (u->output.free == NULL) {\n ngx_http_upstream_finalize_request(r, u,\n NGX_HTTP_INTERNAL_SERVER_ERROR);\n return;\n }\n u->output.free->buf = r->request_body->buf;\n u->output.free->next = NULL;\n u->output.allocated = 1;\n r->request_body->buf->pos = r->request_body->buf->start;\n r->request_body->buf->last = r->request_body->buf->start;\n r->request_body->buf->tag = u->output.tag;\n }\n u->request_sent = 0;\n u->request_body_sent = 0;\n if (rc == NGX_AGAIN) {\n ngx_add_timer(c->write, u->conf->connect_timeout);\n return;\n }\n#if (NGX_HTTP_SSL)\n if (u->ssl && c->ssl == NULL) {\n ngx_http_upstream_ssl_init_connection(r, u, c);\n return;\n }\n#endif\n ngx_http_upstream_send_request(r, u, 1);\n}', 'static void\nngx_http_upstream_send_request(ngx_http_request_t *r, ngx_http_upstream_t *u,\n ngx_uint_t do_write)\n{\n ngx_int_t rc;\n ngx_connection_t *c;\n c = u->peer.connection;\n ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,\n "http upstream send request");\n if (u->state->connect_time == (ngx_msec_t) -1) {\n u->state->connect_time = ngx_current_msec - u->state->response_time;\n }\n if (!u->request_sent && ngx_http_upstream_test_connect(c) != NGX_OK) {\n ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_ERROR);\n return;\n }\n c->log->action = "sending request to upstream";\n rc = ngx_http_upstream_send_request_body(r, u, do_write);\n if (rc == NGX_ERROR) {\n ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_ERROR);\n return;\n }\n if (rc >= NGX_HTTP_SPECIAL_RESPONSE) {\n ngx_http_upstream_finalize_request(r, u, rc);\n return;\n }\n if (rc == NGX_AGAIN) {\n if (!c->write->ready) {\n ngx_add_timer(c->write, u->conf->send_timeout);\n } else if (c->write->timer_set) {\n ngx_del_timer(c->write);\n }\n if (ngx_handle_write_event(c->write, u->conf->send_lowat) != NGX_OK) {\n ngx_http_upstream_finalize_request(r, u,\n NGX_HTTP_INTERNAL_SERVER_ERROR);\n return;\n }\n return;\n }\n u->request_body_sent = 1;\n if (c->write->timer_set) {\n ngx_del_timer(c->write);\n }\n if (c->tcp_nopush == NGX_TCP_NOPUSH_SET) {\n if (ngx_tcp_push(c->fd) == NGX_ERROR) {\n ngx_log_error(NGX_LOG_CRIT, c->log, ngx_socket_errno,\n ngx_tcp_push_n " failed");\n ngx_http_upstream_finalize_request(r, u,\n NGX_HTTP_INTERNAL_SERVER_ERROR);\n return;\n }\n c->tcp_nopush = NGX_TCP_NOPUSH_UNSET;\n }\n u->write_event_handler = ngx_http_upstream_dummy_handler;\n if (ngx_handle_write_event(c->write, 0) != NGX_OK) {\n ngx_http_upstream_finalize_request(r, u,\n NGX_HTTP_INTERNAL_SERVER_ERROR);\n return;\n }\n ngx_add_timer(c->read, u->conf->read_timeout);\n if (c->read->ready) {\n ngx_http_upstream_process_header(r, u);\n return;\n }\n}', 'static void\nngx_http_upstream_next(ngx_http_request_t *r, ngx_http_upstream_t *u,\n ngx_uint_t ft_type)\n{\n ngx_msec_t timeout;\n ngx_uint_t status, state;\n ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,\n "http next upstream, %xi", ft_type);\n if (u->peer.sockaddr) {\n if (ft_type == NGX_HTTP_UPSTREAM_FT_HTTP_403\n || ft_type == NGX_HTTP_UPSTREAM_FT_HTTP_404)\n {\n state = NGX_PEER_NEXT;\n } else {\n state = NGX_PEER_FAILED;\n }\n u->peer.free(&u->peer, u->peer.data, state);\n u->peer.sockaddr = NULL;\n }\n if (ft_type == NGX_HTTP_UPSTREAM_FT_TIMEOUT) {\n ngx_log_error(NGX_LOG_ERR, r->connection->log, NGX_ETIMEDOUT,\n "upstream timed out");\n }\n if (u->peer.cached && ft_type == NGX_HTTP_UPSTREAM_FT_ERROR) {\n u->peer.tries++;\n }\n switch (ft_type) {\n case NGX_HTTP_UPSTREAM_FT_TIMEOUT:\n status = NGX_HTTP_GATEWAY_TIME_OUT;\n break;\n case NGX_HTTP_UPSTREAM_FT_HTTP_500:\n status = NGX_HTTP_INTERNAL_SERVER_ERROR;\n break;\n case NGX_HTTP_UPSTREAM_FT_HTTP_403:\n status = NGX_HTTP_FORBIDDEN;\n break;\n case NGX_HTTP_UPSTREAM_FT_HTTP_404:\n status = NGX_HTTP_NOT_FOUND;\n break;\n default:\n status = NGX_HTTP_BAD_GATEWAY;\n }\n if (r->connection->error) {\n ngx_http_upstream_finalize_request(r, u,\n NGX_HTTP_CLIENT_CLOSED_REQUEST);\n return;\n }\n u->state->status = status;\n timeout = u->conf->next_upstream_timeout;\n if (u->request_sent\n && (r->method & (NGX_HTTP_POST|NGX_HTTP_LOCK|NGX_HTTP_PATCH)))\n {\n ft_type |= NGX_HTTP_UPSTREAM_FT_NON_IDEMPOTENT;\n }\n if (u->peer.tries == 0\n || ((u->conf->next_upstream & ft_type) != ft_type)\n || (u->request_sent && r->request_body_no_buffering)\n || (timeout && ngx_current_msec - u->peer.start_time >= timeout))\n {\n#if (NGX_HTTP_CACHE)\n if (u->cache_status == NGX_HTTP_CACHE_EXPIRED\n && (u->conf->cache_use_stale & ft_type))\n {\n ngx_int_t rc;\n rc = u->reinit_request(r);\n if (rc == NGX_OK) {\n u->cache_status = NGX_HTTP_CACHE_STALE;\n rc = ngx_http_upstream_cache_send(r, u);\n }\n ngx_http_upstream_finalize_request(r, u, rc);\n return;\n }\n#endif\n ngx_http_upstream_finalize_request(r, u, status);\n return;\n }\n if (u->peer.connection) {\n ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,\n "close http upstream connection: %d",\n u->peer.connection->fd);\n#if (NGX_HTTP_SSL)\n if (u->peer.connection->ssl) {\n u->peer.connection->ssl->no_wait_shutdown = 1;\n u->peer.connection->ssl->no_send_shutdown = 1;\n (void) ngx_ssl_shutdown(u->peer.connection);\n }\n#endif\n if (u->peer.connection->pool) {\n ngx_destroy_pool(u->peer.connection->pool);\n }\n ngx_close_connection(u->peer.connection);\n u->peer.connection = NULL;\n }\n ngx_http_upstream_connect(r, u);\n}', 'static void\nngx_http_upstream_finalize_request(ngx_http_request_t *r,\n ngx_http_upstream_t *u, ngx_int_t rc)\n{\n ngx_uint_t flush;\n ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,\n "finalize http upstream request: %i", rc);\n if (u->cleanup == NULL) {\n ngx_http_finalize_request(r, NGX_DONE);\n return;\n }\n *u->cleanup = NULL;\n u->cleanup = NULL;\n if (u->resolved && u->resolved->ctx) {\n ngx_resolve_name_done(u->resolved->ctx);\n u->resolved->ctx = NULL;\n }\n if (u->state && u->state->response_time) {\n u->state->response_time = ngx_current_msec - u->state->response_time;\n if (u->pipe && u->pipe->read_length) {\n u->state->bytes_received += u->pipe->read_length\n - u->pipe->preread_size;\n u->state->response_length = u->pipe->read_length;\n }\n }\n u->finalize_request(r, rc);\n if (u->peer.free && u->peer.sockaddr) {\n u->peer.free(&u->peer, u->peer.data, 0);\n u->peer.sockaddr = NULL;\n }\n if (u->peer.connection) {\n#if (NGX_HTTP_SSL)\n if (u->peer.connection->ssl) {\n u->peer.connection->ssl->no_wait_shutdown = 1;\n (void) ngx_ssl_shutdown(u->peer.connection);\n }\n#endif\n ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,\n "close http upstream connection: %d",\n u->peer.connection->fd);\n if (u->peer.connection->pool) {\n ngx_destroy_pool(u->peer.connection->pool);\n }\n ngx_close_connection(u->peer.connection);\n }\n u->peer.connection = NULL;\n if (u->pipe && u->pipe->temp_file) {\n ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,\n "http upstream temp fd: %d",\n u->pipe->temp_file->file.fd);\n }\n if (u->store && u->pipe && u->pipe->temp_file\n && u->pipe->temp_file->file.fd != NGX_INVALID_FILE)\n {\n if (ngx_delete_file(u->pipe->temp_file->file.name.data)\n == NGX_FILE_ERROR)\n {\n ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno,\n ngx_delete_file_n " \\"%s\\" failed",\n u->pipe->temp_file->file.name.data);\n }\n }\n#if (NGX_HTTP_CACHE)\n if (r->cache) {\n if (u->cacheable) {\n if (rc == NGX_HTTP_BAD_GATEWAY || rc == NGX_HTTP_GATEWAY_TIME_OUT) {\n time_t valid;\n valid = ngx_http_file_cache_valid(u->conf->cache_valid, rc);\n if (valid) {\n r->cache->valid_sec = ngx_time() + valid;\n r->cache->error = rc;\n }\n }\n }\n ngx_http_file_cache_free(r->cache, u->pipe->temp_file);\n }\n#endif\n if (r->subrequest_in_memory\n && u->headers_in.status_n >= NGX_HTTP_SPECIAL_RESPONSE)\n {\n u->buffer.last = u->buffer.pos;\n }\n if (rc == NGX_DECLINED) {\n return;\n }\n r->connection->log->action = "sending to client";\n if (!u->header_sent\n || rc == NGX_HTTP_REQUEST_TIME_OUT\n || rc == NGX_HTTP_CLIENT_CLOSED_REQUEST\n || (u->pipe && u->pipe->downstream_error))\n {\n ngx_http_finalize_request(r, rc);\n return;\n }\n flush = 0;\n if (rc >= NGX_HTTP_SPECIAL_RESPONSE) {\n rc = NGX_ERROR;\n flush = 1;\n }\n if (r->header_only) {\n ngx_http_finalize_request(r, rc);\n return;\n }\n if (rc == 0) {\n rc = ngx_http_send_special(r, NGX_HTTP_LAST);\n } else if (flush) {\n r->keepalive = 0;\n rc = ngx_http_send_special(r, NGX_HTTP_FLUSH);\n }\n ngx_http_finalize_request(r, rc);\n}', 'void\nngx_http_finalize_request(ngx_http_request_t *r, ngx_int_t rc)\n{\n ngx_connection_t *c;\n ngx_http_request_t *pr;\n ngx_http_core_loc_conf_t *clcf;\n c = r->connection;\n ngx_log_debug5(NGX_LOG_DEBUG_HTTP, c->log, 0,\n "http finalize request: %i, \\"%V?%V\\" a:%d, c:%d",\n rc, &r->uri, &r->args, r == c->data, r->main->count);\n if (rc == NGX_DONE) {\n ngx_http_finalize_connection(r);\n return;\n }\n if (rc == NGX_OK && r->filter_finalize) {\n c->error = 1;\n }\n if (rc == NGX_DECLINED) {\n r->content_handler = NULL;\n r->write_event_handler = ngx_http_core_run_phases;\n ngx_http_core_run_phases(r);\n return;\n }\n if (r != r->main && r->post_subrequest) {\n rc = r->post_subrequest->handler(r, r->post_subrequest->data, rc);\n }\n if (rc == NGX_ERROR\n || rc == NGX_HTTP_REQUEST_TIME_OUT\n || rc == NGX_HTTP_CLIENT_CLOSED_REQUEST\n || c->error)\n {\n if (ngx_http_post_action(r) == NGX_OK) {\n return;\n }\n if (r->main->blocked) {\n r->write_event_handler = ngx_http_request_finalizer;\n }\n ngx_http_terminate_request(r, rc);\n return;\n }\n if (rc >= NGX_HTTP_SPECIAL_RESPONSE\n || rc == NGX_HTTP_CREATED\n || rc == NGX_HTTP_NO_CONTENT)\n {\n if (rc == NGX_HTTP_CLOSE) {\n ngx_http_terminate_request(r, rc);\n return;\n }\n if (r == r->main) {\n if (c->read->timer_set) {\n ngx_del_timer(c->read);\n }\n if (c->write->timer_set) {\n ngx_del_timer(c->write);\n }\n }\n c->read->handler = ngx_http_request_handler;\n c->write->handler = ngx_http_request_handler;\n ngx_http_finalize_request(r, ngx_http_special_response_handler(r, rc));\n return;\n }\n if (r != r->main) {\n if (r->buffered || r->postponed) {\n if (ngx_http_set_write_handler(r) != NGX_OK) {\n ngx_http_terminate_request(r, 0);\n }\n return;\n }\n pr = r->parent;\n if (r == c->data) {\n r->main->count--;\n if (!r->logged) {\n clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);\n if (clcf->log_subrequest) {\n ngx_http_log_request(r);\n }\n r->logged = 1;\n } else {\n ngx_log_error(NGX_LOG_ALERT, c->log, 0,\n "subrequest: \\"%V?%V\\" logged again",\n &r->uri, &r->args);\n }\n r->done = 1;\n if (pr->postponed && pr->postponed->request == r) {\n pr->postponed = pr->postponed->next;\n }\n c->data = pr;\n } else {\n ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,\n "http finalize non-active request: \\"%V?%V\\"",\n &r->uri, &r->args);\n r->write_event_handler = ngx_http_request_finalizer;\n if (r->waited) {\n r->done = 1;\n }\n }\n if (ngx_http_post_request(pr, NULL) != NGX_OK) {\n r->main->count++;\n ngx_http_terminate_request(r, 0);\n return;\n }\n ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,\n "http wake parent request: \\"%V?%V\\"",\n &pr->uri, &pr->args);\n return;\n }\n if (r->buffered || c->buffered || r->postponed || r->blocked) {\n if (ngx_http_set_write_handler(r) != NGX_OK) {\n ngx_http_terminate_request(r, 0);\n }\n return;\n }\n if (r != c->data) {\n ngx_log_error(NGX_LOG_ALERT, c->log, 0,\n "http finalize non-active request: \\"%V?%V\\"",\n &r->uri, &r->args);\n return;\n }\n r->done = 1;\n r->write_event_handler = ngx_http_request_empty_handler;\n if (!r->post_action) {\n r->request_complete = 1;\n }\n if (ngx_http_post_action(r) == NGX_OK) {\n return;\n }\n if (c->read->timer_set) {\n ngx_del_timer(c->read);\n }\n if (c->write->timer_set) {\n c->write->delayed = 0;\n ngx_del_timer(c->write);\n }\n if (c->read->eof) {\n ngx_http_close_request(r, 0);\n return;\n }\n ngx_http_finalize_connection(r);\n}', 'ngx_int_t\nngx_http_special_response_handler(ngx_http_request_t *r, ngx_int_t error)\n{\n ngx_uint_t i, err;\n ngx_http_err_page_t *err_page;\n ngx_http_core_loc_conf_t *clcf;\n ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,\n "http special response: %i, \\"%V?%V\\"",\n error, &r->uri, &r->args);\n r->err_status = error;\n if (r->keepalive) {\n switch (error) {\n case NGX_HTTP_BAD_REQUEST:\n case NGX_HTTP_REQUEST_ENTITY_TOO_LARGE:\n case NGX_HTTP_REQUEST_URI_TOO_LARGE:\n case NGX_HTTP_TO_HTTPS:\n case NGX_HTTPS_CERT_ERROR:\n case NGX_HTTPS_NO_CERT:\n case NGX_HTTP_INTERNAL_SERVER_ERROR:\n case NGX_HTTP_NOT_IMPLEMENTED:\n r->keepalive = 0;\n }\n }\n if (r->lingering_close) {\n switch (error) {\n case NGX_HTTP_BAD_REQUEST:\n case NGX_HTTP_TO_HTTPS:\n case NGX_HTTPS_CERT_ERROR:\n case NGX_HTTPS_NO_CERT:\n r->lingering_close = 0;\n }\n }\n r->headers_out.content_type.len = 0;\n clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);\n if (!r->error_page && clcf->error_pages && r->uri_changes != 0) {\n if (clcf->recursive_error_pages == 0) {\n r->error_page = 1;\n }\n err_page = clcf->error_pages->elts;\n for (i = 0; i < clcf->error_pages->nelts; i++) {\n if (err_page[i].status == error) {\n return ngx_http_send_error_page(r, &err_page[i]);\n }\n }\n }\n r->expect_tested = 1;\n if (ngx_http_discard_request_body(r) != NGX_OK) {\n r->keepalive = 0;\n }\n if (clcf->msie_refresh\n && r->headers_in.msie\n && (error == NGX_HTTP_MOVED_PERMANENTLY\n || error == NGX_HTTP_MOVED_TEMPORARILY))\n {\n return ngx_http_send_refresh(r);\n }\n if (error == NGX_HTTP_CREATED) {\n err = 0;\n } else if (error == NGX_HTTP_NO_CONTENT) {\n err = 0;\n } else if (error >= NGX_HTTP_MOVED_PERMANENTLY\n && error < NGX_HTTP_LAST_3XX)\n {\n err = error - NGX_HTTP_MOVED_PERMANENTLY + NGX_HTTP_OFF_3XX;\n } else if (error >= NGX_HTTP_BAD_REQUEST\n && error < NGX_HTTP_LAST_4XX)\n {\n err = error - NGX_HTTP_BAD_REQUEST + NGX_HTTP_OFF_4XX;\n } else if (error >= NGX_HTTP_NGINX_CODES\n && error < NGX_HTTP_LAST_5XX)\n {\n err = error - NGX_HTTP_NGINX_CODES + NGX_HTTP_OFF_5XX;\n switch (error) {\n case NGX_HTTP_TO_HTTPS:\n case NGX_HTTPS_CERT_ERROR:\n case NGX_HTTPS_NO_CERT:\n case NGX_HTTP_REQUEST_HEADER_TOO_LARGE:\n r->err_status = NGX_HTTP_BAD_REQUEST;\n break;\n }\n } else {\n err = 0;\n }\n return ngx_http_send_special_response(r, clcf, err);\n}', 'static ngx_int_t\nngx_http_send_error_page(ngx_http_request_t *r, ngx_http_err_page_t *err_page)\n{\n ngx_int_t overwrite;\n ngx_str_t uri, args;\n ngx_table_elt_t *location;\n ngx_http_core_loc_conf_t *clcf;\n overwrite = err_page->overwrite;\n if (overwrite && overwrite != NGX_HTTP_OK) {\n r->expect_tested = 1;\n }\n if (overwrite >= 0) {\n r->err_status = overwrite;\n }\n if (ngx_http_complex_value(r, &err_page->value, &uri) != NGX_OK) {\n return NGX_ERROR;\n }\n if (uri.len && uri.data[0] == \'/\') {\n if (err_page->value.lengths) {\n ngx_http_split_args(r, &uri, &args);\n } else {\n args = err_page->args;\n }\n if (r->method != NGX_HTTP_HEAD) {\n r->method = NGX_HTTP_GET;\n r->method_name = ngx_http_core_get_method;\n }\n return ngx_http_internal_redirect(r, &uri, &args);\n }\n if (uri.len && uri.data[0] == \'@\') {\n return ngx_http_named_location(r, &uri);\n }\n location = ngx_list_push(&r->headers_out.headers);\n if (location == NULL) {\n return NGX_ERROR;\n }\n if (overwrite != NGX_HTTP_MOVED_PERMANENTLY\n && overwrite != NGX_HTTP_MOVED_TEMPORARILY\n && overwrite != NGX_HTTP_SEE_OTHER\n && overwrite != NGX_HTTP_TEMPORARY_REDIRECT)\n {\n r->err_status = NGX_HTTP_MOVED_TEMPORARILY;\n }\n location->hash = 1;\n ngx_str_set(&location->key, "Location");\n location->value = uri;\n ngx_http_clear_location(r);\n r->headers_out.location = location;\n clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);\n if (clcf->msie_refresh && r->headers_in.msie) {\n return ngx_http_send_refresh(r);\n }\n return ngx_http_send_special_response(r, clcf, r->err_status\n - NGX_HTTP_MOVED_PERMANENTLY\n + NGX_HTTP_OFF_3XX);\n}', 'ngx_int_t\nngx_http_internal_redirect(ngx_http_request_t *r,\n ngx_str_t *uri, ngx_str_t *args)\n{\n ngx_http_core_srv_conf_t *cscf;\n r->uri_changes--;\n if (r->uri_changes == 0) {\n ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,\n "rewrite or internal redirection cycle "\n "while internally redirecting to \\"%V\\"", uri);\n r->main->count++;\n ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);\n return NGX_DONE;\n }\n r->uri = *uri;\n if (args) {\n r->args = *args;\n } else {\n ngx_str_null(&r->args);\n }\n ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,\n "internal redirect: \\"%V?%V\\"", uri, &r->args);\n ngx_http_set_exten(r);\n ngx_memzero(r->ctx, sizeof(void *) * ngx_http_max_module);\n cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);\n r->loc_conf = cscf->ctx->loc_conf;\n ngx_http_update_location_config(r);\n#if (NGX_HTTP_CACHE)\n r->cache = NULL;\n#endif\n r->internal = 1;\n r->valid_unparsed_uri = 0;\n r->add_uri_to_alias = 0;\n r->main->count++;\n ngx_http_handler(r);\n return NGX_DONE;\n}', "void\nngx_http_set_exten(ngx_http_request_t *r)\n{\n ngx_int_t i;\n ngx_str_null(&r->exten);\n for (i = r->uri.len - 1; i > 1; i--) {\n if (r->uri.data[i] == '.' && r->uri.data[i - 1] != '/') {\n r->exten.len = r->uri.len - i - 1;\n r->exten.data = &r->uri.data[i + 1];\n return;\n } else if (r->uri.data[i] == '/') {\n return;\n }\n }\n return;\n}"]
842
0
https://github.com/libav/libav/blob/3ec394ea823c2a6e65a6abbdb2041ce1c66964f8/libavcodec/h264pred.c/#L354
static void pred4x4_horizontal_up_rv40_c(uint8_t *src, uint8_t *topright, int stride){ LOAD_LEFT_EDGE LOAD_DOWN_LEFT_EDGE LOAD_TOP_EDGE LOAD_TOP_RIGHT_EDGE src[0+0*stride]=(t1 + 2*t2 + t3 + 2*l0 + 2*l1 + 4)>>3; src[1+0*stride]=(t2 + 2*t3 + t4 + l0 + 2*l1 + l2 + 4)>>3; src[2+0*stride]= src[0+1*stride]=(t3 + 2*t4 + t5 + 2*l1 + 2*l2 + 4)>>3; src[3+0*stride]= src[1+1*stride]=(t4 + 2*t5 + t6 + l1 + 2*l2 + l3 + 4)>>3; src[2+1*stride]= src[0+2*stride]=(t5 + 2*t6 + t7 + 2*l2 + 2*l3 + 4)>>3; src[3+1*stride]= src[1+2*stride]=(t6 + 3*t7 + l2 + 3*l3 + 4)>>3; src[3+2*stride]= src[1+3*stride]=(l3 + 2*l4 + l5 + 2)>>2; src[0+3*stride]= src[2+2*stride]=(t6 + t7 + l3 + l4 + 2)>>2; src[2+3*stride]=(l4 + l5 + 1)>>1; src[3+3*stride]=(l4 + 2*l5 + l6 + 2)>>2; }
['static void pred4x4_horizontal_up_rv40_c(uint8_t *src, uint8_t *topright, int stride){\n LOAD_LEFT_EDGE\n LOAD_DOWN_LEFT_EDGE\n LOAD_TOP_EDGE\n LOAD_TOP_RIGHT_EDGE\n src[0+0*stride]=(t1 + 2*t2 + t3 + 2*l0 + 2*l1 + 4)>>3;\n src[1+0*stride]=(t2 + 2*t3 + t4 + l0 + 2*l1 + l2 + 4)>>3;\n src[2+0*stride]=\n src[0+1*stride]=(t3 + 2*t4 + t5 + 2*l1 + 2*l2 + 4)>>3;\n src[3+0*stride]=\n src[1+1*stride]=(t4 + 2*t5 + t6 + l1 + 2*l2 + l3 + 4)>>3;\n src[2+1*stride]=\n src[0+2*stride]=(t5 + 2*t6 + t7 + 2*l2 + 2*l3 + 4)>>3;\n src[3+1*stride]=\n src[1+2*stride]=(t6 + 3*t7 + l2 + 3*l3 + 4)>>3;\n src[3+2*stride]=\n src[1+3*stride]=(l3 + 2*l4 + l5 + 2)>>2;\n src[0+3*stride]=\n src[2+2*stride]=(t6 + t7 + l3 + l4 + 2)>>2;\n src[2+3*stride]=(l4 + l5 + 1)>>1;\n src[3+3*stride]=(l4 + 2*l5 + l6 + 2)>>2;\n}']
843
0
https://github.com/libav/libav/blob/555000c7d5c1e13043a948ebc48d2939b0ba6536/libavcodec/h264_refs.c/#L145
int ff_h264_fill_default_ref_list(H264Context *h) { int i, len; if (h->slice_type_nos == AV_PICTURE_TYPE_B) { Picture *sorted[32]; int cur_poc, list; int lens[2]; if (FIELD_PICTURE) cur_poc = h->cur_pic_ptr->field_poc[h->picture_structure == PICT_BOTTOM_FIELD]; else cur_poc = h->cur_pic_ptr->poc; for (list = 0; list < 2; list++) { len = add_sorted(sorted, h->short_ref, h->short_ref_count, cur_poc, 1 ^ list); len += add_sorted(sorted + len, h->short_ref, h->short_ref_count, cur_poc, 0 ^ list); assert(len <= 32); len = build_def_list(h->default_ref_list[list], sorted, len, 0, h->picture_structure); len += build_def_list(h->default_ref_list[list] + len, h->long_ref, 16, 1, h->picture_structure); assert(len <= 32); if (len < h->ref_count[list]) memset(&h->default_ref_list[list][len], 0, sizeof(Picture) * (h->ref_count[list] - len)); lens[list] = len; } if (lens[0] == lens[1] && lens[1] > 1) { for (i = 0; h->default_ref_list[0][i].f.data[0] == h->default_ref_list[1][i].f.data[0] && i < lens[0]; i++); if (i == lens[0]) { Picture tmp; COPY_PICTURE(&tmp, &h->default_ref_list[1][0]); COPY_PICTURE(&h->default_ref_list[1][0], &h->default_ref_list[1][1]); COPY_PICTURE(&h->default_ref_list[1][1], &tmp); } } } else { len = build_def_list(h->default_ref_list[0], h->short_ref, h->short_ref_count, 0, h->picture_structure); len += build_def_list(h->default_ref_list[0] + len, h-> long_ref, 16, 1, h->picture_structure); assert(len <= 32); if (len < h->ref_count[0]) memset(&h->default_ref_list[0][len], 0, sizeof(Picture) * (h->ref_count[0] - len)); } #ifdef TRACE for (i = 0; i < h->ref_count[0]; i++) { tprintf(h->avctx, "List0: %s fn:%d 0x%p\n", (h->default_ref_list[0][i].long_ref ? "LT" : "ST"), h->default_ref_list[0][i].pic_id, h->default_ref_list[0][i].f.data[0]); } if (h->slice_type_nos == AV_PICTURE_TYPE_B) { for (i = 0; i < h->ref_count[1]; i++) { tprintf(h->avctx, "List1: %s fn:%d 0x%p\n", (h->default_ref_list[1][i].long_ref ? "LT" : "ST"), h->default_ref_list[1][i].pic_id, h->default_ref_list[1][i].f.data[0]); } } #endif return 0; }
['int ff_h264_fill_default_ref_list(H264Context *h)\n{\n int i, len;\n if (h->slice_type_nos == AV_PICTURE_TYPE_B) {\n Picture *sorted[32];\n int cur_poc, list;\n int lens[2];\n if (FIELD_PICTURE)\n cur_poc = h->cur_pic_ptr->field_poc[h->picture_structure == PICT_BOTTOM_FIELD];\n else\n cur_poc = h->cur_pic_ptr->poc;\n for (list = 0; list < 2; list++) {\n len = add_sorted(sorted, h->short_ref, h->short_ref_count, cur_poc, 1 ^ list);\n len += add_sorted(sorted + len, h->short_ref, h->short_ref_count, cur_poc, 0 ^ list);\n assert(len <= 32);\n len = build_def_list(h->default_ref_list[list], sorted, len, 0, h->picture_structure);\n len += build_def_list(h->default_ref_list[list] + len, h->long_ref, 16, 1, h->picture_structure);\n assert(len <= 32);\n if (len < h->ref_count[list])\n memset(&h->default_ref_list[list][len], 0, sizeof(Picture) * (h->ref_count[list] - len));\n lens[list] = len;\n }\n if (lens[0] == lens[1] && lens[1] > 1) {\n for (i = 0; h->default_ref_list[0][i].f.data[0] == h->default_ref_list[1][i].f.data[0] && i < lens[0]; i++);\n if (i == lens[0]) {\n Picture tmp;\n COPY_PICTURE(&tmp, &h->default_ref_list[1][0]);\n COPY_PICTURE(&h->default_ref_list[1][0], &h->default_ref_list[1][1]);\n COPY_PICTURE(&h->default_ref_list[1][1], &tmp);\n }\n }\n } else {\n len = build_def_list(h->default_ref_list[0], h->short_ref, h->short_ref_count, 0, h->picture_structure);\n len += build_def_list(h->default_ref_list[0] + len, h-> long_ref, 16, 1, h->picture_structure);\n assert(len <= 32);\n if (len < h->ref_count[0])\n memset(&h->default_ref_list[0][len], 0, sizeof(Picture) * (h->ref_count[0] - len));\n }\n#ifdef TRACE\n for (i = 0; i < h->ref_count[0]; i++) {\n tprintf(h->avctx, "List0: %s fn:%d 0x%p\\n",\n (h->default_ref_list[0][i].long_ref ? "LT" : "ST"),\n h->default_ref_list[0][i].pic_id,\n h->default_ref_list[0][i].f.data[0]);\n }\n if (h->slice_type_nos == AV_PICTURE_TYPE_B) {\n for (i = 0; i < h->ref_count[1]; i++) {\n tprintf(h->avctx, "List1: %s fn:%d 0x%p\\n",\n (h->default_ref_list[1][i].long_ref ? "LT" : "ST"),\n h->default_ref_list[1][i].pic_id,\n h->default_ref_list[1][i].f.data[0]);\n }\n }\n#endif\n return 0;\n}']
844
0
https://github.com/openssl/openssl/blob/47d216940c454b8c6d229565051116c1108ddd2d/crypto/conf/conf.c/#L475
static void value_free_stack(CONF_VALUE *a, LHASH *conf) { CONF_VALUE *vv; STACK *sk; int i; if (a->name != NULL) return; sk=(STACK *)a->value; for (i=sk_num(sk)-1; i>=0; i--) { vv=(CONF_VALUE *)sk_value(sk,i); Free(vv->value); Free(vv->name); Free(vv); } if (sk != NULL) sk_free(sk); Free(a->section); Free(a); }
['static void value_free_stack(CONF_VALUE *a, LHASH *conf)\n\t{\n\tCONF_VALUE *vv;\n\tSTACK *sk;\n\tint i;\n\tif (a->name != NULL) return;\n\tsk=(STACK *)a->value;\n\tfor (i=sk_num(sk)-1; i>=0; i--)\n\t\t{\n\t\tvv=(CONF_VALUE *)sk_value(sk,i);\n\t\tFree(vv->value);\n\t\tFree(vv->name);\n\t\tFree(vv);\n\t\t}\n\tif (sk != NULL) sk_free(sk);\n\tFree(a->section);\n\tFree(a);\n\t}']
845
0
https://github.com/openssl/openssl/blob/1fac96e4d6484a517f2ebe99b72016726391723c/crypto/bn/bn_lib.c/#L470
BIGNUM *bn_expand2(BIGNUM *b, int words) { BN_ULONG *A,*a; const BN_ULONG *B; int i; bn_check_top(b); if (words > b->max) { bn_check_top(b); if (BN_get_flags(b,BN_FLG_STATIC_DATA)) { BNerr(BN_F_BN_EXPAND2,BN_R_EXPAND_ON_STATIC_BIGNUM_DATA); return(NULL); } a=A=(BN_ULONG *)Malloc(sizeof(BN_ULONG)*(words+1)); if (A == NULL) { BNerr(BN_F_BN_EXPAND2,ERR_R_MALLOC_FAILURE); return(NULL); } #if 1 B=b->d; if (B != NULL) { #if 0 for (i=b->top&(~7); i>0; i-=8) { A[0]=B[0]; A[1]=B[1]; A[2]=B[2]; A[3]=B[3]; A[4]=B[4]; A[5]=B[5]; A[6]=B[6]; A[7]=B[7]; A+=8; B+=8; } switch (b->top&7) { case 7: A[6]=B[6]; case 6: A[5]=B[5]; case 5: A[4]=B[4]; case 4: A[3]=B[3]; case 3: A[2]=B[2]; case 2: A[1]=B[1]; case 1: A[0]=B[0]; case 0: ; } #else for (i=b->top>>2; i>0; i--,A+=4,B+=4) { BN_ULONG a0,a1,a2,a3; a0=B[0]; a1=B[1]; a2=B[2]; a3=B[3]; A[0]=a0; A[1]=a1; A[2]=a2; A[3]=a3; } switch (b->top&3) { case 3: A[2]=B[2]; case 2: A[1]=B[1]; case 1: A[0]=B[0]; case 0: ; } #endif Free(b->d); } b->d=a; b->max=words; A= &(b->d[b->top]); for (i=(b->max - b->top)>>3; i>0; i--,A+=8) { A[0]=0; A[1]=0; A[2]=0; A[3]=0; A[4]=0; A[5]=0; A[6]=0; A[7]=0; } for (i=(b->max - b->top)&7; i>0; i--,A++) A[0]=0; #else memset(A,0,sizeof(BN_ULONG)*(words+1)); memcpy(A,b->d,sizeof(b->d[0])*b->top); b->d=a; b->max=words; #endif } return(b); }
['int BN_add_word(BIGNUM *a, BN_ULONG w)\n\t{\n\tBN_ULONG l;\n\tint i;\n\tif (a->neg)\n\t\t{\n\t\ta->neg=0;\n\t\ti=BN_sub_word(a,w);\n\t\tif (!BN_is_zero(a))\n\t\t\ta->neg=1;\n\t\treturn(i);\n\t\t}\n\tw&=BN_MASK2;\n\tif (bn_wexpand(a,a->top+1) == NULL) return(0);\n\ti=0;\n\tfor (;;)\n\t\t{\n\t\tl=(a->d[i]+(BN_ULONG)w)&BN_MASK2;\n\t\ta->d[i]=l;\n\t\tif (w > l)\n\t\t\tw=1;\n\t\telse\n\t\t\tbreak;\n\t\ti++;\n\t\t}\n\tif (i >= a->top)\n\t\ta->top++;\n\treturn(1);\n\t}', 'BIGNUM *bn_expand2(BIGNUM *b, int words)\n\t{\n\tBN_ULONG *A,*a;\n\tconst BN_ULONG *B;\n\tint i;\n\tbn_check_top(b);\n\tif (words > b->max)\n\t\t{\n\t\tbn_check_top(b);\n\t\tif (BN_get_flags(b,BN_FLG_STATIC_DATA))\n\t\t\t{\n\t\t\tBNerr(BN_F_BN_EXPAND2,BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);\n\t\t\treturn(NULL);\n\t\t\t}\n\t\ta=A=(BN_ULONG *)Malloc(sizeof(BN_ULONG)*(words+1));\n\t\tif (A == NULL)\n\t\t\t{\n\t\t\tBNerr(BN_F_BN_EXPAND2,ERR_R_MALLOC_FAILURE);\n\t\t\treturn(NULL);\n\t\t\t}\n#if 1\n\t\tB=b->d;\n\t\tif (B != NULL)\n\t\t\t{\n#if 0\n\t\t\tfor (i=b->top&(~7); i>0; i-=8)\n\t\t\t\t{\n\t\t\t\tA[0]=B[0]; A[1]=B[1]; A[2]=B[2]; A[3]=B[3];\n\t\t\t\tA[4]=B[4]; A[5]=B[5]; A[6]=B[6]; A[7]=B[7];\n\t\t\t\tA+=8;\n\t\t\t\tB+=8;\n\t\t\t\t}\n\t\t\tswitch (b->top&7)\n\t\t\t\t{\n\t\t\tcase 7:\n\t\t\t\tA[6]=B[6];\n\t\t\tcase 6:\n\t\t\t\tA[5]=B[5];\n\t\t\tcase 5:\n\t\t\t\tA[4]=B[4];\n\t\t\tcase 4:\n\t\t\t\tA[3]=B[3];\n\t\t\tcase 3:\n\t\t\t\tA[2]=B[2];\n\t\t\tcase 2:\n\t\t\t\tA[1]=B[1];\n\t\t\tcase 1:\n\t\t\t\tA[0]=B[0];\n\t\t\tcase 0:\n\t\t\t\t;\n\t\t\t\t}\n#else\n\t\t\tfor (i=b->top>>2; i>0; i--,A+=4,B+=4)\n\t\t\t\t{\n\t\t\t\tBN_ULONG a0,a1,a2,a3;\n\t\t\t\ta0=B[0]; a1=B[1]; a2=B[2]; a3=B[3];\n\t\t\t\tA[0]=a0; A[1]=a1; A[2]=a2; A[3]=a3;\n\t\t\t\t}\n\t\t\tswitch (b->top&3)\n\t\t\t\t{\n\t\t\t\tcase 3:\tA[2]=B[2];\n\t\t\t\tcase 2:\tA[1]=B[1];\n\t\t\t\tcase 1:\tA[0]=B[0];\n\t\t\t\tcase 0:\t;\n\t\t\t\t}\n#endif\n\t\t\tFree(b->d);\n\t\t\t}\n\t\tb->d=a;\n\t\tb->max=words;\n\t\tA= &(b->d[b->top]);\n\t\tfor (i=(b->max - b->top)>>3; i>0; i--,A+=8)\n\t\t\t{\n\t\t\tA[0]=0; A[1]=0; A[2]=0; A[3]=0;\n\t\t\tA[4]=0; A[5]=0; A[6]=0; A[7]=0;\n\t\t\t}\n\t\tfor (i=(b->max - b->top)&7; i>0; i--,A++)\n\t\t\tA[0]=0;\n#else\n\t\t\tmemset(A,0,sizeof(BN_ULONG)*(words+1));\n\t\t\tmemcpy(A,b->d,sizeof(b->d[0])*b->top);\n\t\t\tb->d=a;\n\t\t\tb->max=words;\n#endif\n\t\t}\n\treturn(b);\n\t}']
846
0
https://github.com/openssl/openssl/blob/183733f882056ea3e6fe95e665b85fcc6a45dcb4/test/bntest.c/#L2000
int test_rshift1(BIO *bp) { BIGNUM *a, *b, *c; int i; a = BN_new(); b = BN_new(); c = BN_new(); BN_bntest_rand(a, 200, 0, 0); a->neg = rand_neg(); for (i = 0; i < num0; i++) { BN_rshift1(b, a); if (bp != NULL) { if (!results) { BN_print(bp, a); BIO_puts(bp, " / 2"); BIO_puts(bp, " - "); } BN_print(bp, b); BIO_puts(bp, "\n"); } BN_sub(c, a, b); BN_sub(c, c, b); if (!BN_is_zero(c) && !BN_abs_is_word(c, 1)) { fprintf(stderr, "Right shift one test failed!\n"); return 0; } BN_copy(a, b); } BN_free(a); BN_free(b); BN_free(c); return (1); }
['int test_rshift1(BIO *bp)\n{\n BIGNUM *a, *b, *c;\n int i;\n a = BN_new();\n b = BN_new();\n c = BN_new();\n BN_bntest_rand(a, 200, 0, 0);\n a->neg = rand_neg();\n for (i = 0; i < num0; i++) {\n BN_rshift1(b, a);\n if (bp != NULL) {\n if (!results) {\n BN_print(bp, a);\n BIO_puts(bp, " / 2");\n BIO_puts(bp, " - ");\n }\n BN_print(bp, b);\n BIO_puts(bp, "\\n");\n }\n BN_sub(c, a, b);\n BN_sub(c, c, b);\n if (!BN_is_zero(c) && !BN_abs_is_word(c, 1)) {\n fprintf(stderr, "Right shift one test failed!\\n");\n return 0;\n }\n BN_copy(a, b);\n }\n BN_free(a);\n BN_free(b);\n BN_free(c);\n return (1);\n}', 'BIGNUM *BN_new(void)\n{\n BIGNUM *ret;\n if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL) {\n BNerr(BN_F_BN_NEW, ERR_R_MALLOC_FAILURE);\n return (NULL);\n }\n ret->flags = BN_FLG_MALLOCED;\n bn_check_top(ret);\n return (ret);\n}', 'void *CRYPTO_zalloc(size_t num, const char *file, int line)\n{\n void *ret = CRYPTO_malloc(num, file, line);\n if (ret != NULL)\n memset(ret, 0, num);\n return ret;\n}', 'void *CRYPTO_malloc(size_t num, const char *file, int line)\n{\n void *ret = NULL;\n if (num <= 0)\n return NULL;\n allow_customize = 0;\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n if (call_malloc_debug) {\n CRYPTO_mem_debug_malloc(NULL, num, 0, file, line);\n ret = malloc(num);\n CRYPTO_mem_debug_malloc(ret, num, 1, file, line);\n } else {\n ret = malloc(num);\n }\n#else\n (void)file;\n (void)line;\n ret = malloc(num);\n#endif\n#ifndef OPENSSL_CPUID_OBJ\n if (ret && (num > 2048)) {\n extern unsigned char cleanse_ctr;\n ((unsigned char *)ret)[0] = cleanse_ctr;\n }\n#endif\n return ret;\n}', 'int BN_bntest_rand(BIGNUM *rnd, int bits, int top, int bottom)\n{\n return bnrand(2, rnd, bits, top, bottom);\n}', 'static int bnrand(int pseudorand, BIGNUM *rnd, int bits, int top, int bottom)\n{\n unsigned char *buf = NULL;\n int ret = 0, bit, bytes, mask;\n time_t tim;\n if (bits < 0 || (bits == 1 && top > 0)) {\n BNerr(BN_F_BNRAND, BN_R_BITS_TOO_SMALL);\n return 0;\n }\n if (bits == 0) {\n BN_zero(rnd);\n return 1;\n }\n bytes = (bits + 7) / 8;\n bit = (bits - 1) % 8;\n mask = 0xff << (bit + 1);\n buf = OPENSSL_malloc(bytes);\n if (buf == NULL) {\n BNerr(BN_F_BNRAND, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n time(&tim);\n RAND_add(&tim, sizeof(tim), 0.0);\n if (pseudorand) {\n if (RAND_bytes(buf, bytes) <= 0)\n goto err;\n } else {\n if (RAND_bytes(buf, bytes) <= 0)\n goto err;\n }\n if (pseudorand == 2) {\n int i;\n unsigned char c;\n for (i = 0; i < bytes; i++) {\n if (RAND_bytes(&c, 1) <= 0)\n goto err;\n if (c >= 128 && i > 0)\n buf[i] = buf[i - 1];\n else if (c < 42)\n buf[i] = 0;\n else if (c < 84)\n buf[i] = 255;\n }\n }\n if (top >= 0) {\n if (top) {\n if (bit == 0) {\n buf[0] = 1;\n buf[1] |= 0x80;\n } else {\n buf[0] |= (3 << (bit - 1));\n }\n } else {\n buf[0] |= (1 << bit);\n }\n }\n buf[0] &= ~mask;\n if (bottom)\n buf[bytes - 1] |= 1;\n if (!BN_bin2bn(buf, bytes, rnd))\n goto err;\n ret = 1;\n err:\n OPENSSL_clear_free(buf, bytes);\n bn_check_top(rnd);\n return (ret);\n}', 'void CRYPTO_clear_free(void *str, size_t num)\n{\n if (str == NULL)\n return;\n if (num)\n OPENSSL_cleanse(str, num);\n CRYPTO_free(str);\n}', 'int rand_neg(void)\n{\n static unsigned int neg = 0;\n static int sign[8] = { 0, 0, 0, 1, 1, 0, 1, 1 };\n return (sign[(neg++) % 8]);\n}']
847
0
https://github.com/libav/libav/blob/490a022d86ef1c506a79744c5a95368af356fc69/libavcodec/noise_bsf.c/#L33
static int noise(AVBitStreamFilterContext *bsfc, AVCodecContext *avctx, const char *args, uint8_t **poutbuf, int *poutbuf_size, const uint8_t *buf, int buf_size, int keyframe){ unsigned int *state= bsfc->priv_data; int amount= args ? atoi(args) : (*state % 10001+1); int i; *poutbuf= av_malloc(buf_size + FF_INPUT_BUFFER_PADDING_SIZE); memcpy(*poutbuf, buf, buf_size + FF_INPUT_BUFFER_PADDING_SIZE); for(i=0; i<buf_size; i++){ (*state) += (*poutbuf)[i] + 1; if(*state % amount == 0) (*poutbuf)[i] = *state; } return 1; }
['static int noise(AVBitStreamFilterContext *bsfc, AVCodecContext *avctx, const char *args,\n uint8_t **poutbuf, int *poutbuf_size,\n const uint8_t *buf, int buf_size, int keyframe){\n unsigned int *state= bsfc->priv_data;\n int amount= args ? atoi(args) : (*state % 10001+1);\n int i;\n *poutbuf= av_malloc(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);\n memcpy(*poutbuf, buf, buf_size + FF_INPUT_BUFFER_PADDING_SIZE);\n for(i=0; i<buf_size; i++){\n (*state) += (*poutbuf)[i] + 1;\n if(*state % amount == 0)\n (*poutbuf)[i] = *state;\n }\n return 1;\n}', 'void *av_malloc(size_t size)\n{\n void *ptr = NULL;\n#if CONFIG_MEMALIGN_HACK\n long diff;\n#endif\n if(size > (INT_MAX-16) )\n return NULL;\n#if CONFIG_MEMALIGN_HACK\n ptr = malloc(size+16);\n if(!ptr)\n return ptr;\n diff= ((-(long)ptr - 1)&15) + 1;\n ptr = (char*)ptr + diff;\n ((char*)ptr)[-1]= diff;\n#elif HAVE_POSIX_MEMALIGN\n if (posix_memalign(&ptr,16,size))\n ptr = NULL;\n#elif HAVE_MEMALIGN\n ptr = memalign(16,size);\n#else\n ptr = malloc(size);\n#endif\n return ptr;\n}']
848
0
https://github.com/openssl/openssl/blob/d40a1b865fddc3d67f8c06ff1f1466fad331c8f7/crypto/bn/bn_lib.c/#L250
int BN_num_bits(const BIGNUM *a) { int i = a->top - 1; bn_check_top(a); if (BN_is_zero(a)) return 0; return ((i*BN_BITS2) + BN_num_bits_word(a->d[i])); }
['int store_bignum(BIGNUM *bn, unsigned char *buf,int len)\n\t{\n\tint bytes = BN_num_bytes(bn);\n\tif (bytes>len) return 0;\n\tmemset(buf,0,len);\n\tBN_bn2bin(bn,buf+len-bytes);\n\treturn 1;\n\t}', 'int BN_num_bits(const BIGNUM *a)\n\t{\n\tint i = a->top - 1;\n\tbn_check_top(a);\n\tif (BN_is_zero(a)) return 0;\n\treturn ((i*BN_BITS2) + BN_num_bits_word(a->d[i]));\n\t}', 'int BN_bn2bin(const BIGNUM *a, unsigned char *to)\n\t{\n\tint n,i;\n\tBN_ULONG l;\n\tbn_check_top(a);\n\tn=i=BN_num_bytes(a);\n\twhile (i--)\n\t\t{\n\t\tl=a->d[i/BN_BYTES];\n\t\t*(to++)=(unsigned char)(l>>(8*(i%BN_BYTES)))&0xff;\n\t\t}\n\treturn(n);\n\t}']
849
0
https://github.com/openssl/openssl/blob/e02c519cd32a55e6ad39a0cfbeeda775f9115f28/crypto/bn/bn_mul.c/#L657
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; } }
['static int test_expmodzero(void)\n{\n BIGNUM *a = NULL, *r = NULL, *zero = NULL;\n int st = 0;\n if (!TEST_ptr(zero = BN_new())\n || !TEST_ptr(a = BN_new())\n || !TEST_ptr(r = BN_new()))\n goto err;\n BN_zero(zero);\n if (!TEST_true(BN_mod_exp(r, a, zero, BN_value_one(), NULL))\n || !TEST_BN_eq_zero(r)\n || !TEST_true(BN_mod_exp_mont(r, a, zero, BN_value_one(),\n NULL, NULL))\n || !TEST_BN_eq_zero(r)\n || !TEST_true(BN_mod_exp_mont_consttime(r, a, zero,\n BN_value_one(),\n NULL, NULL))\n || !TEST_BN_eq_zero(r)\n || !TEST_true(BN_mod_exp_mont_word(r, 42, zero,\n BN_value_one(), NULL, NULL))\n || !TEST_BN_eq_zero(r))\n goto err;\n st = 1;\nerr:\n BN_free(zero);\n BN_free(a);\n BN_free(r);\n return st;\n}', 'int BN_set_word(BIGNUM *a, BN_ULONG w)\n{\n bn_check_top(a);\n if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)\n return 0;\n a->neg = 0;\n a->d[0] = w;\n a->top = (w ? 1 : 0);\n a->flags &= ~BN_FLG_FIXED_TOP;\n bn_check_top(a);\n return 1;\n}', 'static ossl_inline BIGNUM *bn_expand(BIGNUM *a, int bits)\n{\n if (bits > (INT_MAX - BN_BITS2 + 1))\n return NULL;\n if (((bits+BN_BITS2-1)/BN_BITS2) <= (a)->dmax)\n return a;\n return bn_expand2((a),(bits+BN_BITS2-1)/BN_BITS2);\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_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 || BN_get_flags(a, BN_FLG_CONSTTIME) != 0\n || BN_get_flags(m, 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_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 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 (val[0] == 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 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_to_mont_fixed_top(val[0], aa, mont, ctx))\n goto err;\n window = BN_window_bits_for_exponent_size(bits);\n if (window > 1) {\n if (!bn_mul_mont_fixed_top(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_mul_mont_fixed_top(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 r->flags |= BN_FLG_FIXED_TOP;\n } else\n#endif\n if (!bn_to_mont_fixed_top(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_mul_mont_fixed_top(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_mul_mont_fixed_top(r, r, r, mont, ctx))\n goto err;\n }\n if (!bn_mul_mont_fixed_top(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_to_mont_fixed_top(BIGNUM *r, const BIGNUM *a, BN_MONT_CTX *mont,\n BN_CTX *ctx)\n{\n return bn_mul_mont_fixed_top(r, a, &(mont->RR), mont, ctx);\n}', 'int bn_mul_mont_fixed_top(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 int num = mont->N.top;\n#if defined(OPENSSL_BN_ASM_MONT) && defined(MONT_WORD)\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 r->flags |= BN_FLG_FIXED_TOP;\n return 1;\n }\n }\n#endif\n if ((a->top + b->top) > 2 * num)\n return 0;\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_fixed_top(tmp, a, ctx))\n goto err;\n } else {\n if (!bn_mul_fixed_top(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 ret = 1;\n err:\n BN_CTX_end(ctx);\n return ret;\n}', 'int bn_mul_fixed_top(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)\n{\n int ret = 0;\n int top, al, bl;\n BIGNUM *rr;\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n int i;\n#endif\n#ifdef BN_RECURSION\n BIGNUM *t = NULL;\n int j = 0, k;\n#endif\n bn_check_top(a);\n bn_check_top(b);\n bn_check_top(r);\n al = a->top;\n bl = b->top;\n if ((al == 0) || (bl == 0)) {\n BN_zero(r);\n return 1;\n }\n top = al + bl;\n BN_CTX_start(ctx);\n if ((r == a) || (r == b)) {\n if ((rr = BN_CTX_get(ctx)) == NULL)\n goto err;\n } else\n rr = r;\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n i = al - bl;\n#endif\n#ifdef BN_MUL_COMBA\n if (i == 0) {\n# if 0\n if (al == 4) {\n if (bn_wexpand(rr, 8) == NULL)\n goto err;\n rr->top = 8;\n bn_mul_comba4(rr->d, a->d, b->d);\n goto end;\n }\n# endif\n if (al == 8) {\n if (bn_wexpand(rr, 16) == NULL)\n goto err;\n rr->top = 16;\n bn_mul_comba8(rr->d, a->d, b->d);\n goto end;\n }\n }\n#endif\n#ifdef BN_RECURSION\n if ((al >= BN_MULL_SIZE_NORMAL) && (bl >= BN_MULL_SIZE_NORMAL)) {\n if (i >= -1 && i <= 1) {\n if (i >= 0) {\n j = BN_num_bits_word((BN_ULONG)al);\n }\n if (i == -1) {\n j = BN_num_bits_word((BN_ULONG)bl);\n }\n j = 1 << (j - 1);\n assert(j <= al || j <= bl);\n k = j + j;\n t = BN_CTX_get(ctx);\n if (t == NULL)\n goto err;\n if (al > j || bl > j) {\n if (bn_wexpand(t, k * 4) == NULL)\n goto err;\n if (bn_wexpand(rr, k * 4) == NULL)\n goto err;\n bn_mul_part_recursive(rr->d, a->d, b->d,\n j, al - j, bl - j, t->d);\n } else {\n if (bn_wexpand(t, k * 2) == NULL)\n goto err;\n if (bn_wexpand(rr, k * 2) == NULL)\n goto err;\n bn_mul_recursive(rr->d, a->d, b->d, j, al - j, bl - j, t->d);\n }\n rr->top = top;\n goto end;\n }\n }\n#endif\n if (bn_wexpand(rr, top) == NULL)\n goto err;\n rr->top = top;\n bn_mul_normal(rr->d, a->d, al, b->d, bl);\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n end:\n#endif\n rr->neg = a->neg ^ b->neg;\n rr->flags |= BN_FLG_FIXED_TOP;\n if (r != rr && BN_copy(r, rr) == NULL)\n goto err;\n ret = 1;\n err:\n bn_check_top(r);\n BN_CTX_end(ctx);\n return ret;\n}', 'void bn_mul_part_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n,\n int tna, int tnb, BN_ULONG *t)\n{\n int i, j, n2 = n * 2;\n int c1, c2, neg;\n BN_ULONG ln, lo, *p;\n if (n < 8) {\n bn_mul_normal(r, a, n + tna, b, n + tnb);\n return;\n }\n c1 = bn_cmp_part_words(a, &(a[n]), tna, n - tna);\n c2 = bn_cmp_part_words(&(b[n]), b, tnb, tnb - n);\n neg = 0;\n switch (c1 * 3 + c2) {\n case -4:\n bn_sub_part_words(t, &(a[n]), a, tna, tna - n);\n bn_sub_part_words(&(t[n]), b, &(b[n]), tnb, n - tnb);\n break;\n case -3:\n case -2:\n bn_sub_part_words(t, &(a[n]), a, tna, tna - n);\n bn_sub_part_words(&(t[n]), &(b[n]), b, tnb, tnb - n);\n neg = 1;\n break;\n case -1:\n case 0:\n case 1:\n case 2:\n bn_sub_part_words(t, a, &(a[n]), tna, n - tna);\n bn_sub_part_words(&(t[n]), b, &(b[n]), tnb, n - tnb);\n neg = 1;\n break;\n case 3:\n case 4:\n bn_sub_part_words(t, a, &(a[n]), tna, n - tna);\n bn_sub_part_words(&(t[n]), &(b[n]), b, tnb, tnb - n);\n break;\n }\n# if 0\n if (n == 4) {\n bn_mul_comba4(&(t[n2]), t, &(t[n]));\n bn_mul_comba4(r, a, b);\n bn_mul_normal(&(r[n2]), &(a[n]), tn, &(b[n]), tn);\n memset(&r[n2 + tn * 2], 0, sizeof(*r) * (n2 - tn * 2));\n } else\n# endif\n if (n == 8) {\n bn_mul_comba8(&(t[n2]), t, &(t[n]));\n bn_mul_comba8(r, a, b);\n bn_mul_normal(&(r[n2]), &(a[n]), tna, &(b[n]), tnb);\n memset(&r[n2 + tna + tnb], 0, sizeof(*r) * (n2 - tna - tnb));\n } else {\n p = &(t[n2 * 2]);\n bn_mul_recursive(&(t[n2]), t, &(t[n]), n, 0, 0, p);\n bn_mul_recursive(r, a, b, n, 0, 0, p);\n i = n / 2;\n if (tna > tnb)\n j = tna - i;\n else\n j = tnb - i;\n if (j == 0) {\n bn_mul_recursive(&(r[n2]), &(a[n]), &(b[n]),\n i, tna - i, tnb - i, p);\n memset(&r[n2 + i * 2], 0, sizeof(*r) * (n2 - i * 2));\n } else if (j > 0) {\n bn_mul_part_recursive(&(r[n2]), &(a[n]), &(b[n]),\n i, tna - i, tnb - i, p);\n memset(&(r[n2 + tna + tnb]), 0,\n sizeof(BN_ULONG) * (n2 - tna - tnb));\n } else {\n memset(&r[n2], 0, sizeof(*r) * n2);\n if (tna < BN_MUL_RECURSIVE_SIZE_NORMAL\n && tnb < BN_MUL_RECURSIVE_SIZE_NORMAL) {\n bn_mul_normal(&(r[n2]), &(a[n]), tna, &(b[n]), tnb);\n } else {\n for (;;) {\n i /= 2;\n if (i < tna || i < tnb) {\n bn_mul_part_recursive(&(r[n2]),\n &(a[n]), &(b[n]),\n i, tna - i, tnb - i, p);\n break;\n } else if (i == tna || i == tnb) {\n bn_mul_recursive(&(r[n2]),\n &(a[n]), &(b[n]),\n i, tna - i, tnb - i, p);\n break;\n }\n }\n }\n }\n }\n c1 = (int)(bn_add_words(t, r, &(r[n2]), n2));\n if (neg) {\n c1 -= (int)(bn_sub_words(&(t[n2]), t, &(t[n2]), n2));\n } else {\n c1 += (int)(bn_add_words(&(t[n2]), &(t[n2]), t, n2));\n }\n c1 += (int)(bn_add_words(&(r[n]), &(r[n]), &(t[n2]), n2));\n if (c1) {\n p = &(r[n + n2]);\n lo = *p;\n ln = (lo + c1) & BN_MASK2;\n *p = ln;\n if (ln < (BN_ULONG)c1) {\n do {\n p++;\n lo = *p;\n ln = (lo + 1) & BN_MASK2;\n *p = ln;\n } while (ln == 0);\n }\n }\n}', 'void bn_mul_normal(BN_ULONG *r, BN_ULONG *a, int na, BN_ULONG *b, int nb)\n{\n BN_ULONG *rr;\n if (na < nb) {\n int itmp;\n BN_ULONG *ltmp;\n itmp = na;\n na = nb;\n nb = itmp;\n ltmp = a;\n a = b;\n b = ltmp;\n }\n rr = &(r[na]);\n if (nb <= 0) {\n (void)bn_mul_words(r, a, na, 0);\n return;\n } else\n rr[0] = bn_mul_words(r, a, na, b[0]);\n for (;;) {\n if (--nb <= 0)\n return;\n rr[1] = bn_mul_add_words(&(r[1]), a, na, b[1]);\n if (--nb <= 0)\n return;\n rr[2] = bn_mul_add_words(&(r[2]), a, na, b[2]);\n if (--nb <= 0)\n return;\n rr[3] = bn_mul_add_words(&(r[3]), a, na, b[3]);\n if (--nb <= 0)\n return;\n rr[4] = bn_mul_add_words(&(r[4]), a, na, b[4]);\n rr += 4;\n r += 4;\n b += 4;\n }\n}']
850
0
https://github.com/openssl/openssl/blob/305b68f1a2b6d4d0aa07a6ab47ac372f067a40bb/crypto/bn/bn_lib.c/#L290
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; }
['static int ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in,\n BIGNUM **kinvp, BIGNUM **rp,\n const unsigned char *dgst, int dlen)\n{\n BN_CTX *ctx = NULL;\n BIGNUM *k = NULL, *r = NULL, *X = NULL;\n const BIGNUM *order;\n EC_POINT *tmp_point = NULL;\n const EC_GROUP *group;\n int ret = 0;\n int order_bits;\n if (eckey == NULL || (group = EC_KEY_get0_group(eckey)) == NULL) {\n ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_PASSED_NULL_PARAMETER);\n return 0;\n }\n if (!EC_KEY_can_sign(eckey)) {\n ECerr(EC_F_ECDSA_SIGN_SETUP, EC_R_CURVE_DOES_NOT_SUPPORT_SIGNING);\n return 0;\n }\n if (ctx_in == NULL) {\n if ((ctx = BN_CTX_new()) == NULL) {\n ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_MALLOC_FAILURE);\n return 0;\n }\n } else\n ctx = ctx_in;\n k = BN_new();\n r = BN_new();\n X = BN_new();\n if (k == NULL || r == NULL || X == NULL) {\n ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n if ((tmp_point = EC_POINT_new(group)) == NULL) {\n ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);\n goto err;\n }\n order = EC_GROUP_get0_order(group);\n if (order == NULL) {\n ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);\n goto err;\n }\n order_bits = BN_num_bits(order);\n if (!BN_set_bit(k, order_bits)\n || !BN_set_bit(r, order_bits)\n || !BN_set_bit(X, order_bits))\n goto err;\n do {\n do\n if (dgst != NULL) {\n if (!BN_generate_dsa_nonce\n (k, order, EC_KEY_get0_private_key(eckey), dgst, dlen,\n ctx)) {\n ECerr(EC_F_ECDSA_SIGN_SETUP,\n EC_R_RANDOM_NUMBER_GENERATION_FAILED);\n goto err;\n }\n } else {\n if (!BN_priv_rand_range(k, order)) {\n ECerr(EC_F_ECDSA_SIGN_SETUP,\n EC_R_RANDOM_NUMBER_GENERATION_FAILED);\n goto err;\n }\n }\n while (BN_is_zero(k));\n if (!EC_POINT_mul(group, tmp_point, k, NULL, NULL, ctx)) {\n ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);\n goto err;\n }\n if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) ==\n NID_X9_62_prime_field) {\n if (!EC_POINT_get_affine_coordinates_GFp\n (group, tmp_point, X, NULL, ctx)) {\n ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);\n goto err;\n }\n }\n#ifndef OPENSSL_NO_EC2M\n else {\n if (!EC_POINT_get_affine_coordinates_GF2m(group,\n tmp_point, X, NULL,\n ctx)) {\n ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);\n goto err;\n }\n }\n#endif\n if (!BN_nnmod(r, X, order, ctx)) {\n ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);\n goto err;\n }\n }\n while (BN_is_zero(r));\n if (!ec_group_do_inverse_ord(group, k, k, ctx)) {\n ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);\n goto err;\n }\n BN_clear_free(*rp);\n BN_clear_free(*kinvp);\n *rp = r;\n *kinvp = k;\n ret = 1;\n err:\n if (!ret) {\n BN_clear_free(k);\n BN_clear_free(r);\n }\n if (ctx != ctx_in)\n BN_CTX_free(ctx);\n EC_POINT_free(tmp_point);\n BN_clear_free(X);\n return ret;\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}', 'int BN_priv_rand_range(BIGNUM *r, const BIGNUM *range)\n{\n return bnrand_range(PRIVATE, r, range);\n}', 'static int bnrand_range(BNRAND_FLAG flag, BIGNUM *r, const BIGNUM *range)\n{\n int n;\n int count = 100;\n if (range->neg || BN_is_zero(range)) {\n BNerr(BN_F_BNRAND_RANGE, BN_R_INVALID_RANGE);\n return 0;\n }\n n = BN_num_bits(range);\n if (n == 1)\n BN_zero(r);\n else if (!BN_is_bit_set(range, n - 2) && !BN_is_bit_set(range, n - 3)) {\n do {\n if (!bnrand(flag, r, n + 1, BN_RAND_TOP_ANY, BN_RAND_BOTTOM_ANY))\n return 0;\n if (BN_cmp(r, range) >= 0) {\n if (!BN_sub(r, r, range))\n return 0;\n if (BN_cmp(r, range) >= 0)\n if (!BN_sub(r, r, range))\n return 0;\n }\n if (!--count) {\n BNerr(BN_F_BNRAND_RANGE, BN_R_TOO_MANY_ITERATIONS);\n return 0;\n }\n }\n while (BN_cmp(r, range) >= 0);\n } else {\n do {\n if (!bnrand(flag, r, n, BN_RAND_TOP_ANY, BN_RAND_BOTTOM_ANY))\n return 0;\n if (!--count) {\n BNerr(BN_F_BNRAND_RANGE, BN_R_TOO_MANY_ITERATIONS);\n return 0;\n }\n }\n while (BN_cmp(r, range) >= 0);\n }\n bn_check_top(r);\n return 1;\n}', 'int EC_POINT_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *g_scalar,\n const EC_POINT *point, const BIGNUM *p_scalar, BN_CTX *ctx)\n{\n const EC_POINT *points[1];\n const BIGNUM *scalars[1];\n points[0] = point;\n scalars[0] = p_scalar;\n return EC_POINTs_mul(group, r, g_scalar,\n (point != NULL\n && p_scalar != NULL), points, scalars, ctx);\n}', 'int EC_POINTs_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,\n size_t num, const EC_POINT *points[],\n const BIGNUM *scalars[], BN_CTX *ctx)\n{\n if (group->meth->mul == 0)\n return ec_wNAF_mul(group, r, scalar, num, points, scalars, ctx);\n return group->meth->mul(group, r, scalar, num, points, scalars, ctx);\n}', 'int BN_nnmod(BIGNUM *r, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx)\n{\n if (!(BN_mod(r, m, d, ctx)))\n return 0;\n if (!r->neg)\n return 1;\n return (d->neg ? BN_sub : BN_add) (r, r, d);\n}', 'int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,\n BN_CTX *ctx)\n{\n int norm_shift, i, loop;\n BIGNUM *tmp, wnum, *snum, *sdiv, *res;\n BN_ULONG *resp, *wnump;\n BN_ULONG d0, d1;\n int num_n, div_n;\n int no_branch = 0;\n if ((num->top > 0 && num->d[num->top - 1] == 0) ||\n (divisor->top > 0 && divisor->d[divisor->top - 1] == 0)) {\n BNerr(BN_F_BN_DIV, BN_R_NOT_INITIALIZED);\n return 0;\n }\n bn_check_top(num);\n bn_check_top(divisor);\n if ((BN_get_flags(num, BN_FLG_CONSTTIME) != 0)\n || (BN_get_flags(divisor, BN_FLG_CONSTTIME) != 0)) {\n no_branch = 1;\n }\n bn_check_top(dv);\n bn_check_top(rm);\n if (BN_is_zero(divisor)) {\n BNerr(BN_F_BN_DIV, BN_R_DIV_BY_ZERO);\n return 0;\n }\n if (!no_branch && BN_ucmp(num, divisor) < 0) {\n if (rm != NULL) {\n if (BN_copy(rm, num) == NULL)\n return 0;\n }\n if (dv != NULL)\n BN_zero(dv);\n return 1;\n }\n BN_CTX_start(ctx);\n res = (dv == NULL) ? BN_CTX_get(ctx) : dv;\n tmp = BN_CTX_get(ctx);\n snum = BN_CTX_get(ctx);\n sdiv = BN_CTX_get(ctx);\n if (sdiv == NULL)\n goto err;\n norm_shift = BN_BITS2 - ((BN_num_bits(divisor)) % BN_BITS2);\n if (!(BN_lshift(sdiv, divisor, norm_shift)))\n goto err;\n sdiv->neg = 0;\n norm_shift += BN_BITS2;\n if (!(BN_lshift(snum, num, norm_shift)))\n goto err;\n snum->neg = 0;\n if (no_branch) {\n if (snum->top <= sdiv->top + 1) {\n if (bn_wexpand(snum, sdiv->top + 2) == NULL)\n goto err;\n for (i = snum->top; i < sdiv->top + 2; i++)\n snum->d[i] = 0;\n snum->top = sdiv->top + 2;\n } else {\n if (bn_wexpand(snum, snum->top + 1) == NULL)\n goto err;\n snum->d[snum->top] = 0;\n snum->top++;\n }\n }\n div_n = sdiv->top;\n num_n = snum->top;\n loop = num_n - div_n;\n wnum.neg = 0;\n wnum.d = &(snum->d[loop]);\n wnum.top = div_n;\n wnum.flags = BN_FLG_STATIC_DATA;\n wnum.dmax = snum->dmax - loop;\n d0 = sdiv->d[div_n - 1];\n d1 = (div_n == 1) ? 0 : sdiv->d[div_n - 2];\n wnump = &(snum->d[num_n - 1]);\n if (!bn_wexpand(res, (loop + 1)))\n goto err;\n res->neg = (num->neg ^ divisor->neg);\n res->top = loop - no_branch;\n resp = &(res->d[loop - 1]);\n if (!bn_wexpand(tmp, (div_n + 1)))\n goto err;\n if (!no_branch) {\n if (BN_ucmp(&wnum, sdiv) >= 0) {\n bn_clear_top2max(&wnum);\n bn_sub_words(wnum.d, wnum.d, sdiv->d, div_n);\n *resp = 1;\n } else\n res->top--;\n }\n resp++;\n if (res->top == 0)\n res->neg = 0;\n else\n resp--;\n for (i = 0; i < loop - 1; i++, wnump--) {\n BN_ULONG q, l0;\n# if defined(BN_DIV3W) && !defined(OPENSSL_NO_ASM)\n BN_ULONG bn_div_3_words(BN_ULONG *, BN_ULONG, BN_ULONG);\n q = bn_div_3_words(wnump, d1, d0);\n# else\n BN_ULONG n0, n1, rem = 0;\n n0 = wnump[0];\n n1 = wnump[-1];\n if (n0 == d0)\n q = BN_MASK2;\n else {\n# ifdef BN_LLONG\n BN_ULLONG t2;\n# if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)\n q = (BN_ULONG)(((((BN_ULLONG) n0) << BN_BITS2) | n1) / d0);\n# else\n q = bn_div_words(n0, n1, d0);\n# endif\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n t2 = (BN_ULLONG) d1 *q;\n for (;;) {\n if (t2 <= ((((BN_ULLONG) rem) << BN_BITS2) | wnump[-2]))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n t2 -= d1;\n }\n# else\n BN_ULONG t2l, t2h;\n q = bn_div_words(n0, n1, d0);\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n# if defined(BN_UMULT_LOHI)\n BN_UMULT_LOHI(t2l, t2h, d1, q);\n# elif defined(BN_UMULT_HIGH)\n t2l = d1 * q;\n t2h = BN_UMULT_HIGH(d1, q);\n# else\n {\n BN_ULONG ql, qh;\n t2l = LBITS(d1);\n t2h = HBITS(d1);\n ql = LBITS(q);\n qh = HBITS(q);\n mul64(t2l, t2h, ql, qh);\n }\n# endif\n for (;;) {\n if ((t2h < rem) || ((t2h == rem) && (t2l <= wnump[-2])))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n if (t2l < d1)\n t2h--;\n t2l -= d1;\n }\n# endif\n }\n# endif\n l0 = bn_mul_words(tmp->d, sdiv->d, div_n, q);\n tmp->d[div_n] = l0;\n wnum.d--;\n if (bn_sub_words(wnum.d, wnum.d, tmp->d, div_n + 1)) {\n q--;\n if (bn_add_words(wnum.d, wnum.d, sdiv->d, div_n))\n (*wnump)++;\n }\n resp--;\n *resp = q;\n }\n bn_correct_top(snum);\n if (rm != NULL) {\n int neg = num->neg;\n BN_rshift(rm, snum, norm_shift);\n if (!BN_is_zero(rm))\n rm->neg = neg;\n bn_check_top(rm);\n }\n if (no_branch)\n bn_correct_top(res);\n BN_CTX_end(ctx);\n return 1;\n err:\n bn_check_top(rm);\n BN_CTX_end(ctx);\n return 0;\n}', 'int ec_group_do_inverse_ord(const EC_GROUP *group, BIGNUM *res,\n const BIGNUM *x, BN_CTX *ctx)\n{\n if (group->meth->field_inverse_mod_ord != NULL)\n return group->meth->field_inverse_mod_ord(group, res, x, ctx);\n else\n return ec_field_inverse_mod_ord(group, res, x, ctx);\n}', 'static int ec_field_inverse_mod_ord(const EC_GROUP *group, BIGNUM *r,\n const BIGNUM *x, BN_CTX *ctx)\n{\n BIGNUM *e = NULL;\n BN_CTX *new_ctx = NULL;\n int ret = 0;\n if (group->mont_data == NULL)\n return 0;\n if (ctx == NULL && (ctx = new_ctx = BN_CTX_secure_new()) == NULL)\n return 0;\n BN_CTX_start(ctx);\n if ((e = BN_CTX_get(ctx)) == NULL)\n goto err;\n if (!BN_set_word(e, 2))\n goto err;\n if (!BN_sub(e, group->order, e))\n goto err;\n if (!BN_mod_exp_mont(r, x, e, group->order, ctx, group->mont_data))\n goto err;\n ret = 1;\n err:\n if (ctx != NULL)\n BN_CTX_end(ctx);\n BN_CTX_free(new_ctx);\n return ret;\n}', 'int BN_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)\n{\n int ret, r_neg, cmp_res;\n bn_check_top(a);\n bn_check_top(b);\n if (a->neg != b->neg) {\n r_neg = a->neg;\n ret = BN_uadd(r, a, b);\n } else {\n cmp_res = BN_ucmp(a, b);\n if (cmp_res > 0) {\n r_neg = a->neg;\n ret = BN_usub(r, a, b);\n } else if (cmp_res < 0) {\n r_neg = !b->neg;\n ret = BN_usub(r, b, a);\n } else {\n r_neg = 0;\n BN_zero(r);\n ret = 1;\n }\n }\n r->neg = r_neg;\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 || BN_get_flags(a, BN_FLG_CONSTTIME) != 0\n || BN_get_flags(m, 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_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 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 (val[0] == 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 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, wmask, window0;\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 = p->top * BN_BITS2;\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 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 (!a->neg) {\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 }\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 powerbufLen += top * sizeof(mont->N.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_nnmod(&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 window0 = (bits - 1) % 5 + 1;\n wmask = (1 << window0) - 1;\n bits -= window0;\n wvalue = bn_get_bits(p, bits) & wmask;\n bn_gather5_t4(tmp.d, top, powerbuf, wvalue);\n while (bits > 0) {\n if (bits < stride)\n stride = bits;\n bits -= stride;\n wvalue = bn_get_bits(p, bits);\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 *n0 = mont->n0, *np;\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 for (np = am.d + top, i = 0; i < top; i++)\n np[i] = mont->N.d[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, np, 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, np, 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, np, 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, np, n0, top, i - 1);\n bn_scatter5(tmp.d, top, powerbuf, i);\n }\n# endif\n window0 = (bits - 1) % 5 + 1;\n wmask = (1 << window0) - 1;\n bits -= window0;\n wvalue = bn_get_bits(p, bits) & wmask;\n bn_gather5(tmp.d, top, powerbuf, wvalue);\n if (top & 7) {\n while (bits > 0) {\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 bn_get_bits5(p->d, bits -= 5));\n }\n } else {\n while (bits > 0) {\n bn_power5(tmp.d, tmp.d, powerbuf, np, n0, top,\n bn_get_bits5(p->d, bits -= 5));\n }\n }\n ret = bn_from_montgomery(tmp.d, tmp.d, NULL, np, 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, window))\n goto err;\n if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&am, top, powerbuf, 1, window))\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(&tmp, top, powerbuf, 2,\n window))\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(&tmp, top, powerbuf, i,\n window))\n goto err;\n }\n }\n window0 = (bits - 1) % window + 1;\n wmask = (1 << window0) - 1;\n bits -= window0;\n wvalue = bn_get_bits(p, bits) & wmask;\n if (!MOD_EXP_CTIME_COPY_FROM_PREBUF(&tmp, top, powerbuf, wvalue,\n window))\n goto err;\n wmask = (1 << window) - 1;\n while (bits > 0) {\n for (i = 0; i < window; i++)\n if (!BN_mod_mul_montgomery(&tmp, &tmp, &tmp, mont, ctx))\n goto err;\n bits -= window;\n wvalue = bn_get_bits(p, bits) & wmask;\n if (!MOD_EXP_CTIME_COPY_FROM_PREBUF(&am, top, powerbuf, wvalue,\n window))\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_is_odd(const BIGNUM *a)\n{\n return (a->top > 0) && (a->d[0] & 1);\n}', 'int BN_MONT_CTX_set(BN_MONT_CTX *mont, const BIGNUM *mod, BN_CTX *ctx)\n{\n int 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 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}']
851
0
https://github.com/openssl/openssl/blob/f305ecdac0b7048e7ef38a7196f4393fa7ceff38/ssl/t1_lib.c/#L296
int tls_valid_group(SSL *s, uint16_t group_id, int version) { const TLS_GROUP_INFO *ginfo = tls1_group_id_lookup(group_id); if (version < TLS1_3_VERSION) { if ((ginfo->flags & TLS_GROUP_ONLY_FOR_TLS1_3) != 0) return 0; } return 1; }
['int tls_valid_group(SSL *s, uint16_t group_id, int version)\n{\n const TLS_GROUP_INFO *ginfo = tls1_group_id_lookup(group_id);\n if (version < TLS1_3_VERSION) {\n if ((ginfo->flags & TLS_GROUP_ONLY_FOR_TLS1_3) != 0)\n return 0;\n }\n return 1;\n}', 'const TLS_GROUP_INFO *tls1_group_id_lookup(uint16_t group_id)\n{\n#if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_EC)\n size_t i;\n for (i = 0; i < OSSL_NELEM(nid_list); i++) {\n if (nid_list[i].group_id == group_id)\n return &nid_list[i];\n }\n#endif\n return NULL;\n}']
852
0
https://github.com/openssl/openssl/blob/2d5d70b15559f9813054ddb11b30b816daf62ebe/crypto/bn/bn_ctx.c/#L401
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--; } }
['int bn_probable_prime_dh(BIGNUM *rnd, int bits,\n const BIGNUM *add, const BIGNUM *rem, BN_CTX *ctx)\n{\n int i, ret = 0;\n BIGNUM *t1;\n BN_CTX_start(ctx);\n if ((t1 = BN_CTX_get(ctx)) == NULL)\n goto err;\n if (!BN_rand(rnd, bits, 0, 1))\n goto err;\n if (!BN_mod(t1, rnd, add, ctx))\n goto err;\n if (!BN_sub(rnd, rnd, t1))\n goto err;\n if (rem == NULL) {\n if (!BN_add_word(rnd, 1))\n goto err;\n } else {\n if (!BN_add(rnd, rnd, rem))\n goto err;\n }\n loop:\n for (i = 1; i < NUMPRIMES; i++) {\n if (BN_mod_word(rnd, (BN_ULONG)primes[i]) <= 1) {\n if (!BN_add(rnd, rnd, add))\n goto err;\n goto loop;\n }\n }\n ret = 1;\n err:\n BN_CTX_end(ctx);\n bn_check_top(rnd);\n return (ret);\n}', 'int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,\n BN_CTX *ctx)\n{\n int norm_shift, i, loop;\n BIGNUM *tmp, wnum, *snum, *sdiv, *res;\n BN_ULONG *resp, *wnump;\n BN_ULONG d0, d1;\n int num_n, div_n;\n int no_branch = 0;\n if ((num->top > 0 && num->d[num->top - 1] == 0) ||\n (divisor->top > 0 && divisor->d[divisor->top - 1] == 0)) {\n BNerr(BN_F_BN_DIV, BN_R_NOT_INITIALIZED);\n return 0;\n }\n bn_check_top(num);\n bn_check_top(divisor);\n if ((BN_get_flags(num, BN_FLG_CONSTTIME) != 0)\n || (BN_get_flags(divisor, BN_FLG_CONSTTIME) != 0)) {\n no_branch = 1;\n }\n bn_check_top(dv);\n bn_check_top(rm);\n if (BN_is_zero(divisor)) {\n BNerr(BN_F_BN_DIV, BN_R_DIV_BY_ZERO);\n return (0);\n }\n if (!no_branch && BN_ucmp(num, divisor) < 0) {\n if (rm != NULL) {\n if (BN_copy(rm, num) == NULL)\n return (0);\n }\n if (dv != NULL)\n BN_zero(dv);\n return (1);\n }\n BN_CTX_start(ctx);\n tmp = BN_CTX_get(ctx);\n snum = BN_CTX_get(ctx);\n sdiv = BN_CTX_get(ctx);\n if (dv == NULL)\n res = BN_CTX_get(ctx);\n else\n res = dv;\n if (sdiv == NULL || res == NULL || tmp == NULL || snum == NULL)\n goto err;\n norm_shift = BN_BITS2 - ((BN_num_bits(divisor)) % BN_BITS2);\n if (!(BN_lshift(sdiv, divisor, norm_shift)))\n goto err;\n sdiv->neg = 0;\n norm_shift += BN_BITS2;\n if (!(BN_lshift(snum, num, norm_shift)))\n goto err;\n snum->neg = 0;\n if (no_branch) {\n if (snum->top <= sdiv->top + 1) {\n if (bn_wexpand(snum, sdiv->top + 2) == NULL)\n goto err;\n for (i = snum->top; i < sdiv->top + 2; i++)\n snum->d[i] = 0;\n snum->top = sdiv->top + 2;\n } else {\n if (bn_wexpand(snum, snum->top + 1) == NULL)\n goto err;\n snum->d[snum->top] = 0;\n snum->top++;\n }\n }\n div_n = sdiv->top;\n num_n = snum->top;\n loop = num_n - div_n;\n wnum.neg = 0;\n wnum.d = &(snum->d[loop]);\n wnum.top = div_n;\n wnum.dmax = snum->dmax - loop;\n d0 = sdiv->d[div_n - 1];\n d1 = (div_n == 1) ? 0 : sdiv->d[div_n - 2];\n wnump = &(snum->d[num_n - 1]);\n res->neg = (num->neg ^ divisor->neg);\n if (!bn_wexpand(res, (loop + 1)))\n goto err;\n res->top = loop - no_branch;\n resp = &(res->d[loop - 1]);\n if (!bn_wexpand(tmp, (div_n + 1)))\n goto err;\n if (!no_branch) {\n if (BN_ucmp(&wnum, sdiv) >= 0) {\n bn_clear_top2max(&wnum);\n bn_sub_words(wnum.d, wnum.d, sdiv->d, div_n);\n *resp = 1;\n } else\n res->top--;\n }\n if (res->top == 0)\n res->neg = 0;\n else\n resp--;\n for (i = 0; i < loop - 1; i++, wnump--, resp--) {\n BN_ULONG q, l0;\n# if defined(BN_DIV3W) && !defined(OPENSSL_NO_ASM)\n BN_ULONG bn_div_3_words(BN_ULONG *, BN_ULONG, BN_ULONG);\n q = bn_div_3_words(wnump, d1, d0);\n# else\n BN_ULONG n0, n1, rem = 0;\n n0 = wnump[0];\n n1 = wnump[-1];\n if (n0 == d0)\n q = BN_MASK2;\n else {\n# ifdef BN_LLONG\n BN_ULLONG t2;\n# if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)\n q = (BN_ULONG)(((((BN_ULLONG) n0) << BN_BITS2) | n1) / d0);\n# else\n q = bn_div_words(n0, n1, d0);\n# ifdef BN_DEBUG_LEVITTE\n fprintf(stderr, "DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\\\nX) -> 0x%08X\\n", n0, n1, d0, q);\n# endif\n# endif\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n t2 = (BN_ULLONG) d1 *q;\n for (;;) {\n if (t2 <= ((((BN_ULLONG) rem) << BN_BITS2) | wnump[-2]))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n t2 -= d1;\n }\n# else\n BN_ULONG t2l, t2h;\n q = bn_div_words(n0, n1, d0);\n# ifdef BN_DEBUG_LEVITTE\n fprintf(stderr, "DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\\\nX) -> 0x%08X\\n", n0, n1, d0, q);\n# endif\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n# if defined(BN_UMULT_LOHI)\n BN_UMULT_LOHI(t2l, t2h, d1, q);\n# elif defined(BN_UMULT_HIGH)\n t2l = d1 * q;\n t2h = BN_UMULT_HIGH(d1, q);\n# else\n {\n BN_ULONG ql, qh;\n t2l = LBITS(d1);\n t2h = HBITS(d1);\n ql = LBITS(q);\n qh = HBITS(q);\n mul64(t2l, t2h, ql, qh);\n }\n# endif\n for (;;) {\n if ((t2h < rem) || ((t2h == rem) && (t2l <= wnump[-2])))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n if (t2l < d1)\n t2h--;\n t2l -= d1;\n }\n# endif\n }\n# endif\n l0 = bn_mul_words(tmp->d, sdiv->d, div_n, q);\n tmp->d[div_n] = l0;\n wnum.d--;\n if (bn_sub_words(wnum.d, wnum.d, tmp->d, div_n + 1)) {\n q--;\n if (bn_add_words(wnum.d, wnum.d, sdiv->d, div_n))\n (*wnump)++;\n }\n *resp = q;\n }\n bn_correct_top(snum);\n if (rm != NULL) {\n int neg = num->neg;\n BN_rshift(rm, snum, norm_shift);\n if (!BN_is_zero(rm))\n rm->neg = neg;\n bn_check_top(rm);\n }\n if (no_branch)\n bn_correct_top(res);\n BN_CTX_end(ctx);\n return (1);\n err:\n bn_check_top(rm);\n BN_CTX_end(ctx);\n return (0);\n}', 'void BN_CTX_end(BN_CTX *ctx)\n{\n CTXDBG_ENTRY("BN_CTX_end", ctx);\n if (ctx->err_stack)\n ctx->err_stack--;\n else {\n unsigned int fp = BN_STACK_pop(&ctx->stack);\n if (fp < ctx->used)\n BN_POOL_release(&ctx->pool, ctx->used - fp);\n ctx->used = fp;\n ctx->too_many = 0;\n }\n CTXDBG_EXIT(ctx);\n}', 'static void BN_POOL_release(BN_POOL *p, unsigned int num)\n{\n unsigned int offset = (p->used - 1) % BN_CTX_POOL_SIZE;\n p->used -= num;\n while (num--) {\n bn_check_top(p->current->vals + offset);\n if (offset == 0) {\n offset = BN_CTX_POOL_SIZE - 1;\n p->current = p->current->prev;\n } else\n offset--;\n }\n}']
853
0
https://github.com/openssl/openssl/blob/5dfc369ffcdc4722482c818e6ba6cf6e704c2cb5/crypto/bn/bn_lib.c/#L377
BIGNUM *bn_expand2(BIGNUM *b, int words) { BN_ULONG *A,*B,*a; int i,j; bn_check_top(b); if (words > b->max) { bn_check_top(b); if (BN_get_flags(b,BN_FLG_STATIC_DATA)) { BNerr(BN_F_BN_EXPAND2,BN_R_EXPAND_ON_STATIC_BIGNUM_DATA); return(NULL); } a=A=(BN_ULONG *)Malloc(sizeof(BN_ULONG)*(words+1)); if (A == NULL) { BNerr(BN_F_BN_EXPAND2,ERR_R_MALLOC_FAILURE); return(NULL); } memset(A,0x5c,sizeof(BN_ULONG)*(words+1)); #if 1 B=b->d; if (B != NULL) { for (i=b->top&(~7); i>0; i-=8) { A[0]=B[0]; A[1]=B[1]; A[2]=B[2]; A[3]=B[3]; A[4]=B[4]; A[5]=B[5]; A[6]=B[6]; A[7]=B[7]; A+=8; B+=8; } switch (b->top&7) { case 7: A[6]=B[6]; case 6: A[5]=B[5]; case 5: A[4]=B[4]; case 4: A[3]=B[3]; case 3: A[2]=B[2]; case 2: A[1]=B[1]; case 1: A[0]=B[0]; case 0: ; } Free(b->d); } b->d=a; b->max=words; B= &(b->d[b->top]); j=(b->max - b->top) & ~7; for (i=0; i<j; i+=8) { B[0]=0; B[1]=0; B[2]=0; B[3]=0; B[4]=0; B[5]=0; B[6]=0; B[7]=0; B+=8; } j=(b->max - b->top) & 7; for (i=0; i<j; i++) { B[0]=0; B++; } #else memcpy(a->d,b->d,sizeof(b->d[0])*b->top); #endif } return(b); }
['int MAIN(int argc, char **argv)\n\t{\n#ifndef NO_DSA\n\tDSA *dsa_params=NULL;\n#endif\n\tint ex=1,x509=0,days=30;\n\tX509 *x509ss=NULL;\n\tX509_REQ *req=NULL;\n\tEVP_PKEY *pkey=NULL;\n\tint i,badops=0,newreq=0,newkey= -1,pkey_type=0;\n\tBIO *in=NULL,*out=NULL;\n\tint informat,outformat,verify=0,noout=0,text=0,keyform=FORMAT_PEM;\n\tint nodes=0,kludge=0;\n\tchar *infile,*outfile,*prog,*keyfile=NULL,*template=NULL,*keyout=NULL;\n\tchar *extensions = NULL;\n\tEVP_CIPHER *cipher=NULL;\n\tint modulus=0;\n\tchar *p;\n\tconst EVP_MD *md_alg=NULL,*digest=EVP_md5();\n#ifndef MONOLITH\n\tMS_STATIC char config_name[256];\n#endif\n#ifndef NO_DES\n\tcipher=EVP_des_ede3_cbc();\n#endif\n\tapps_startup();\n\tif (bio_err == NULL)\n\t\tif ((bio_err=BIO_new(BIO_s_file())) != NULL)\n\t\t\tBIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT);\n\tinfile=NULL;\n\toutfile=NULL;\n\tinformat=FORMAT_PEM;\n\toutformat=FORMAT_PEM;\n\tprog=argv[0];\n\targc--;\n\targv++;\n\twhile (argc >= 1)\n\t\t{\n\t\tif \t(strcmp(*argv,"-inform") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\tinformat=str2fmt(*(++argv));\n\t\t\t}\n\t\telse if (strcmp(*argv,"-outform") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\toutformat=str2fmt(*(++argv));\n\t\t\t}\n\t\telse if (strcmp(*argv,"-key") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\tkeyfile= *(++argv);\n\t\t\t}\n\t\telse if (strcmp(*argv,"-new") == 0)\n\t\t\t{\n\t\t\tpkey_type=TYPE_RSA;\n\t\t\tnewreq=1;\n\t\t\t}\n\t\telse if (strcmp(*argv,"-config") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\ttemplate= *(++argv);\n\t\t\t}\n\t\telse if (strcmp(*argv,"-keyform") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\tkeyform=str2fmt(*(++argv));\n\t\t\t}\n\t\telse if (strcmp(*argv,"-in") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\tinfile= *(++argv);\n\t\t\t}\n\t\telse if (strcmp(*argv,"-out") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\toutfile= *(++argv);\n\t\t\t}\n\t\telse if (strcmp(*argv,"-keyout") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\tkeyout= *(++argv);\n\t\t\t}\n\t\telse if (strcmp(*argv,"-newkey") == 0)\n\t\t\t{\n\t\t\tint is_numeric;\n\t\t\tif (--argc < 1) goto bad;\n\t\t\tp= *(++argv);\n\t\t\tis_numeric = p[0] >= \'0\' && p[0] <= \'9\';\n\t\t\tif (strncmp("rsa:",p,4) == 0 || is_numeric)\n\t\t\t\t{\n\t\t\t\tpkey_type=TYPE_RSA;\n\t\t\t\tif(!is_numeric)\n\t\t\t\t p+=4;\n\t\t\t\tnewkey= atoi(p);\n\t\t\t\t}\n\t\t\telse\n#ifndef NO_DSA\n\t\t\t\tif (strncmp("dsa:",p,4) == 0)\n\t\t\t\t{\n\t\t\t\tX509 *xtmp=NULL;\n\t\t\t\tEVP_PKEY *dtmp;\n\t\t\t\tpkey_type=TYPE_DSA;\n\t\t\t\tp+=4;\n\t\t\t\tif ((in=BIO_new_file(p,"r")) == NULL)\n\t\t\t\t\t{\n\t\t\t\t\tperror(p);\n\t\t\t\t\tgoto end;\n\t\t\t\t\t}\n\t\t\t\tif ((dsa_params=PEM_read_bio_DSAparams(in,NULL,NULL)) == NULL)\n\t\t\t\t\t{\n\t\t\t\t\tERR_clear_error();\n\t\t\t\t\tBIO_reset(in);\n\t\t\t\t\tif ((xtmp=PEM_read_bio_X509(in,NULL,NULL)) == NULL)\n\t\t\t\t\t\t{\n\t\t\t\t\t\tBIO_printf(bio_err,"unable to load DSA parameters from file\\n");\n\t\t\t\t\t\tgoto end;\n\t\t\t\t\t\t}\n\t\t\t\t\tdtmp=X509_get_pubkey(xtmp);\n\t\t\t\t\tif (dtmp->type == EVP_PKEY_DSA)\n\t\t\t\t\t\tdsa_params=DSAparams_dup(dtmp->pkey.dsa);\n\t\t\t\t\tEVP_PKEY_free(dtmp);\n\t\t\t\t\tX509_free(xtmp);\n\t\t\t\t\tif (dsa_params == NULL)\n\t\t\t\t\t\t{\n\t\t\t\t\t\tBIO_printf(bio_err,"Certificate does not contain DSA parameters\\n");\n\t\t\t\t\t\tgoto end;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\tBIO_free(in);\n\t\t\t\tnewkey=BN_num_bits(dsa_params->p);\n\t\t\t\tin=NULL;\n\t\t\t\t}\n\t\t\telse\n#endif\n#ifndef NO_DH\n\t\t\t\tif (strncmp("dh:",p,4) == 0)\n\t\t\t\t{\n\t\t\t\tpkey_type=TYPE_DH;\n\t\t\t\tp+=3;\n\t\t\t\t}\n\t\t\telse\n#endif\n\t\t\t\tpkey_type=TYPE_RSA;\n\t\t\tnewreq=1;\n\t\t\t}\n\t\telse if (strcmp(*argv,"-modulus") == 0)\n\t\t\tmodulus=1;\n\t\telse if (strcmp(*argv,"-verify") == 0)\n\t\t\tverify=1;\n\t\telse if (strcmp(*argv,"-nodes") == 0)\n\t\t\tnodes=1;\n\t\telse if (strcmp(*argv,"-noout") == 0)\n\t\t\tnoout=1;\n\t\telse if (strcmp(*argv,"-text") == 0)\n\t\t\ttext=1;\n\t\telse if (strcmp(*argv,"-x509") == 0)\n\t\t\tx509=1;\n\t\telse if (strcmp(*argv,"-asn1-kludge") == 0)\n\t\t\tkludge=1;\n\t\telse if (strcmp(*argv,"-no-asn1-kludge") == 0)\n\t\t\tkludge=0;\n\t\telse if (strcmp(*argv,"-days") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\tdays= atoi(*(++argv));\n\t\t\tif (days == 0) days=30;\n\t\t\t}\n\t\telse if ((md_alg=EVP_get_digestbyname(&((*argv)[1]))) != NULL)\n\t\t\t{\n\t\t\tdigest=md_alg;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tBIO_printf(bio_err,"unknown option %s\\n",*argv);\n\t\t\tbadops=1;\n\t\t\tbreak;\n\t\t\t}\n\t\targc--;\n\t\targv++;\n\t\t}\n\tif (badops)\n\t\t{\nbad:\n\t\tBIO_printf(bio_err,"%s [options] <infile >outfile\\n",prog);\n\t\tBIO_printf(bio_err,"where options are\\n");\n\t\tBIO_printf(bio_err," -inform arg input format - one of DER TXT PEM\\n");\n\t\tBIO_printf(bio_err," -outform arg output format - one of DER TXT PEM\\n");\n\t\tBIO_printf(bio_err," -in arg input file\\n");\n\t\tBIO_printf(bio_err," -out arg output file\\n");\n\t\tBIO_printf(bio_err," -text text form of request\\n");\n\t\tBIO_printf(bio_err," -noout do not output REQ\\n");\n\t\tBIO_printf(bio_err," -verify verify signature on REQ\\n");\n\t\tBIO_printf(bio_err," -modulus RSA modulus\\n");\n\t\tBIO_printf(bio_err," -nodes don\'t encrypt the output key\\n");\n\t\tBIO_printf(bio_err," -key file\tuse the private key contained in file\\n");\n\t\tBIO_printf(bio_err," -keyform arg key file format\\n");\n\t\tBIO_printf(bio_err," -keyout arg file to send the key to\\n");\n\t\tBIO_printf(bio_err," -newkey rsa:bits generate a new RSA key of \'bits\' in size\\n");\n\t\tBIO_printf(bio_err," -newkey dsa:file generate a new DSA key, parameters taken from CA in \'file\'\\n");\n\t\tBIO_printf(bio_err," -[digest] Digest to sign with (md5, sha1, md2, mdc2)\\n");\n\t\tBIO_printf(bio_err," -config file request template file.\\n");\n\t\tBIO_printf(bio_err," -new new request.\\n");\n\t\tBIO_printf(bio_err," -x509 output a x509 structure instead of a cert. req.\\n");\n\t\tBIO_printf(bio_err," -days number of days a x509 generated by -x509 is valid for.\\n");\n\t\tBIO_printf(bio_err," -asn1-kludge Output the \'request\' in a format that is wrong but some CA\'s\\n");\n\t\tBIO_printf(bio_err," have been reported as requiring\\n");\n\t\tBIO_printf(bio_err," [ It is now always turned on but can be turned off with -no-asn1-kludge ]\\n");\n\t\tgoto end;\n\t\t}\n\tERR_load_crypto_strings();\n\tX509V3_add_standard_extensions();\n#ifndef MONOLITH\n\tp=getenv("OPENSSL_CONF");\n\tif (p == NULL)\n\t\tp=getenv("SSLEAY_CONF");\n\tif (p == NULL)\n\t\t{\n\t\tstrcpy(config_name,X509_get_default_cert_area());\n\t\tstrcat(config_name,"/lib/");\n\t\tstrcat(config_name,OPENSSL_CONF);\n\t\tp=config_name;\n\t\t}\n default_config_file=p;\n\tconfig=CONF_load(config,p,NULL);\n#endif\n\tif (template != NULL)\n\t\t{\n\t\tlong errline;\n\t\tBIO_printf(bio_err,"Using configuration from %s\\n",template);\n\t\treq_conf=CONF_load(NULL,template,&errline);\n\t\tif (req_conf == NULL)\n\t\t\t{\n\t\t\tBIO_printf(bio_err,"error on line %ld of %s\\n",errline,template);\n\t\t\tgoto end;\n\t\t\t}\n\t\t}\n\telse\n\t\t{\n\t\treq_conf=config;\n\t\tBIO_printf(bio_err,"Using configuration from %s\\n",\n\t\t\tdefault_config_file);\n\t\tif (req_conf == NULL)\n\t\t\t{\n\t\t\tBIO_printf(bio_err,"Unable to load config info\\n");\n\t\t\t}\n\t\t}\n\tif (req_conf != NULL)\n\t\t{\n\t\tp=CONF_get_string(req_conf,NULL,"oid_file");\n\t\tif (p != NULL)\n\t\t\t{\n\t\t\tBIO *oid_bio;\n\t\t\toid_bio=BIO_new_file(p,"r");\n\t\t\tif (oid_bio == NULL)\n\t\t\t\t{\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\tOBJ_create_objects(oid_bio);\n\t\t\t\tBIO_free(oid_bio);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif(!add_oid_section(req_conf)) goto end;\n\tif ((md_alg == NULL) &&\n\t\t((p=CONF_get_string(req_conf,SECTION,"default_md")) != NULL))\n\t\t{\n\t\tif ((md_alg=EVP_get_digestbyname(p)) != NULL)\n\t\t\tdigest=md_alg;\n\t\t}\n\textensions = CONF_get_string(req_conf, SECTION, V3_EXTENSIONS);\n\tif(extensions) {\n\t\tX509V3_CTX ctx;\n\t\tX509V3_set_ctx_test(&ctx);\n\t\tX509V3_set_conf_lhash(&ctx, req_conf);\n\t\tif(!X509V3_EXT_add_conf(req_conf, &ctx, extensions, NULL)) {\n\t\t\tBIO_printf(bio_err,\n\t\t\t "Error Loading extension section %s\\n", extensions);\n\t\t\tgoto end;\n\t\t}\n\t}\n\tin=BIO_new(BIO_s_file());\n\tout=BIO_new(BIO_s_file());\n\tif ((in == NULL) || (out == NULL))\n\t\tgoto end;\n\tif (keyfile != NULL)\n\t\t{\n\t\tif (BIO_read_filename(in,keyfile) <= 0)\n\t\t\t{\n\t\t\tperror(keyfile);\n\t\t\tgoto end;\n\t\t\t}\n\t\tif (keyform == FORMAT_PEM)\n\t\t\tpkey=PEM_read_bio_PrivateKey(in,NULL,NULL);\n\t\telse\n\t\t\t{\n\t\t\tBIO_printf(bio_err,"bad input format specified for X509 request\\n");\n\t\t\tgoto end;\n\t\t\t}\n\t\tif (pkey == NULL)\n\t\t\t{\n\t\t\tBIO_printf(bio_err,"unable to load Private key\\n");\n\t\t\tgoto end;\n\t\t\t}\n\t\t}\n\tif (newreq && (pkey == NULL))\n\t\t{\n\t\tchar *randfile;\n\t\tchar buffer[200];\n\t\tif ((randfile=CONF_get_string(req_conf,SECTION,"RANDFILE")) == NULL)\n\t\t\trandfile=RAND_file_name(buffer,200);\n#ifdef WINDOWS\n\t\tBIO_printf(bio_err,"Loading \'screen\' into random state -");\n\t\tBIO_flush(bio_err);\n\t\tRAND_screen();\n\t\tBIO_printf(bio_err," done\\n");\n#endif\n\t\tif ((randfile == NULL) || !RAND_load_file(randfile,1024L*1024L))\n\t\t\t{\n\t\t\tBIO_printf(bio_err,"unable to load \'random state\'\\n");\n\t\t\tBIO_printf(bio_err,"What this means is that the random number generator has not been seeded\\n");\n\t\t\tBIO_printf(bio_err,"with much random data.\\n");\n\t\t\tBIO_printf(bio_err,"Consider setting the RANDFILE environment variable to point at a file that\\n");\n\t\t\tBIO_printf(bio_err,"\'random\' data can be kept in.\\n");\n\t\t\t}\n\t\tif (newkey <= 0)\n\t\t\t{\n\t\t\tnewkey=(int)CONF_get_number(req_conf,SECTION,BITS);\n\t\t\tif (newkey <= 0)\n\t\t\t\tnewkey=DEFAULT_KEY_LENGTH;\n\t\t\t}\n\t\tif (newkey < MIN_KEY_LENGTH)\n\t\t\t{\n\t\t\tBIO_printf(bio_err,"private key length is too short,\\n");\n\t\t\tBIO_printf(bio_err,"it needs to be at least %d bits, not %d\\n",MIN_KEY_LENGTH,newkey);\n\t\t\tgoto end;\n\t\t\t}\n\t\tBIO_printf(bio_err,"Generating a %d bit %s private key\\n",\n\t\t\tnewkey,(pkey_type == TYPE_RSA)?"RSA":"DSA");\n\t\tif ((pkey=EVP_PKEY_new()) == NULL) goto end;\n#ifndef NO_RSA\n\t\tif (pkey_type == TYPE_RSA)\n\t\t\t{\n\t\t\tif (!EVP_PKEY_assign_RSA(pkey,\n\t\t\t\tRSA_generate_key(newkey,0x10001,\n\t\t\t\t\treq_cb,(char *)bio_err)))\n\t\t\t\tgoto end;\n\t\t\t}\n\t\telse\n#endif\n#ifndef NO_DSA\n\t\t\tif (pkey_type == TYPE_DSA)\n\t\t\t{\n\t\t\tif (!DSA_generate_key(dsa_params)) goto end;\n\t\t\tif (!EVP_PKEY_assign_DSA(pkey,dsa_params)) goto end;\n\t\t\tdsa_params=NULL;\n\t\t\t}\n#endif\n\t\tif ((randfile == NULL) || (RAND_write_file(randfile) == 0))\n\t\t\tBIO_printf(bio_err,"unable to write \'random state\'\\n");\n\t\tif (pkey == NULL) goto end;\n\t\tif (keyout == NULL)\n\t\t\tkeyout=CONF_get_string(req_conf,SECTION,KEYFILE);\n\t\tif (keyout == NULL)\n\t\t\t{\n\t\t\tBIO_printf(bio_err,"writing new private key to stdout\\n");\n\t\t\tBIO_set_fp(out,stdout,BIO_NOCLOSE);\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tBIO_printf(bio_err,"writing new private key to \'%s\'\\n",keyout);\n\t\t\tif (BIO_write_filename(out,keyout) <= 0)\n\t\t\t\t{\n\t\t\t\tperror(keyout);\n\t\t\t\tgoto end;\n\t\t\t\t}\n\t\t\t}\n\t\tp=CONF_get_string(req_conf,SECTION,"encrypt_rsa_key");\n\t\tif (p == NULL)\n\t\t\tp=CONF_get_string(req_conf,SECTION,"encrypt_key");\n\t\tif ((p != NULL) && (strcmp(p,"no") == 0))\n\t\t\tcipher=NULL;\n\t\tif (nodes) cipher=NULL;\n\t\ti=0;\nloop:\n\t\tif (!PEM_write_bio_PrivateKey(out,pkey,cipher,\n\t\t\tNULL,0,NULL))\n\t\t\t{\n\t\t\tif ((ERR_GET_REASON(ERR_peek_error()) ==\n\t\t\t\tPEM_R_PROBLEMS_GETTING_PASSWORD) && (i < 3))\n\t\t\t\t{\n\t\t\t\tERR_clear_error();\n\t\t\t\ti++;\n\t\t\t\tgoto loop;\n\t\t\t\t}\n\t\t\tgoto end;\n\t\t\t}\n\t\tBIO_printf(bio_err,"-----\\n");\n\t\t}\n\tif (!newreq)\n\t\t{\n\t\tkludge= -1;\n\t\tif (infile == NULL)\n\t\t\tBIO_set_fp(in,stdin,BIO_NOCLOSE);\n\t\telse\n\t\t\t{\n\t\t\tif (BIO_read_filename(in,infile) <= 0)\n\t\t\t\t{\n\t\t\t\tperror(infile);\n\t\t\t\tgoto end;\n\t\t\t\t}\n\t\t\t}\n\t\tif\t(informat == FORMAT_ASN1)\n\t\t\treq=d2i_X509_REQ_bio(in,NULL);\n\t\telse if (informat == FORMAT_PEM)\n\t\t\treq=PEM_read_bio_X509_REQ(in,NULL,NULL);\n\t\telse\n\t\t\t{\n\t\t\tBIO_printf(bio_err,"bad input format specified for X509 request\\n");\n\t\t\tgoto end;\n\t\t\t}\n\t\tif (req == NULL)\n\t\t\t{\n\t\t\tBIO_printf(bio_err,"unable to load X509 request\\n");\n\t\t\tgoto end;\n\t\t\t}\n\t\t}\n\tif (newreq || x509)\n\t\t{\n#ifndef NO_DSA\n\t\tif (pkey->type == EVP_PKEY_DSA)\n\t\t\tdigest=EVP_dss1();\n#endif\n\t\tif (pkey == NULL)\n\t\t\t{\n\t\t\tBIO_printf(bio_err,"you need to specify a private key\\n");\n\t\t\tgoto end;\n\t\t\t}\n\t\tif (req == NULL)\n\t\t\t{\n\t\t\treq=X509_REQ_new();\n\t\t\tif (req == NULL)\n\t\t\t\t{\n\t\t\t\tgoto end;\n\t\t\t\t}\n\t\t\ti=make_REQ(req,pkey,!x509);\n\t\t\tif (kludge >= 0)\n\t\t\t\treq->req_info->req_kludge=kludge;\n\t\t\tif (!i)\n\t\t\t\t{\n\t\t\t\tBIO_printf(bio_err,"problems making Certificate Request\\n");\n\t\t\t\tgoto end;\n\t\t\t\t}\n\t\t\t}\n\t\tif (x509)\n\t\t\t{\n\t\t\tEVP_PKEY *tmppkey;\n\t\t\tX509V3_CTX ext_ctx;\n\t\t\tif ((x509ss=X509_new()) == NULL) goto end;\n\t\t\tif(!X509_set_version(x509ss, 2)) goto end;\n\t\t\tASN1_INTEGER_set(X509_get_serialNumber(x509ss),0L);\n\t\t\tX509_set_issuer_name(x509ss,\n\t\t\t\tX509_REQ_get_subject_name(req));\n\t\t\tX509_gmtime_adj(X509_get_notBefore(x509ss),0);\n\t\t\tX509_gmtime_adj(X509_get_notAfter(x509ss),\n\t\t\t\t(long)60*60*24*days);\n\t\t\tX509_set_subject_name(x509ss,\n\t\t\t\tX509_REQ_get_subject_name(req));\n\t\t\ttmppkey = X509_REQ_get_pubkey(req);\n\t\t\tX509_set_pubkey(x509ss,tmppkey);\n\t\t\tEVP_PKEY_free(tmppkey);\n\t\t\tX509V3_set_ctx(&ext_ctx, x509ss, x509ss, NULL, NULL, 0);\n\t\t\tX509V3_set_conf_lhash(&ext_ctx, req_conf);\n\t\t\tif(extensions && !X509V3_EXT_add_conf(req_conf,\n\t\t\t\t \t&ext_ctx, extensions, x509ss))\n\t\t\t {\n\t\t\t BIO_printf(bio_err,\n\t\t\t\t "Error Loading extension section %s\\n",\n\t\t\t\t extensions);\n\t\t\t goto end;\n\t\t\t }\n\t\t\tif (!(i=X509_sign(x509ss,pkey,digest)))\n\t\t\t\tgoto end;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tif (!(i=X509_REQ_sign(req,pkey,digest)))\n\t\t\t\tgoto end;\n\t\t\t}\n\t\t}\n\tif (verify && !x509)\n\t\t{\n\t\tint tmp=0;\n\t\tif (pkey == NULL)\n\t\t\t{\n\t\t\tpkey=X509_REQ_get_pubkey(req);\n\t\t\ttmp=1;\n\t\t\tif (pkey == NULL) goto end;\n\t\t\t}\n\t\ti=X509_REQ_verify(req,pkey);\n\t\tif (tmp) {\n\t\t\tEVP_PKEY_free(pkey);\n\t\t\tpkey=NULL;\n\t\t}\n\t\tif (i < 0)\n\t\t\t{\n\t\t\tgoto end;\n\t\t\t}\n\t\telse if (i == 0)\n\t\t\t{\n\t\t\tBIO_printf(bio_err,"verify failure\\n");\n\t\t\t}\n\t\telse\n\t\t\tBIO_printf(bio_err,"verify OK\\n");\n\t\t}\n\tif (noout && !text && !modulus)\n\t\t{\n\t\tex=0;\n\t\tgoto end;\n\t\t}\n\tif (outfile == NULL)\n\t\tBIO_set_fp(out,stdout,BIO_NOCLOSE);\n\telse\n\t\t{\n\t\tif ((keyout != NULL) && (strcmp(outfile,keyout) == 0))\n\t\t\ti=(int)BIO_append_filename(out,outfile);\n\t\telse\n\t\t\ti=(int)BIO_write_filename(out,outfile);\n\t\tif (!i)\n\t\t\t{\n\t\t\tperror(outfile);\n\t\t\tgoto end;\n\t\t\t}\n\t\t}\n\tif (text)\n\t\t{\n\t\tif (x509)\n\t\t\tX509_print(out,x509ss);\n\t\telse\n\t\t\tX509_REQ_print(out,req);\n\t\t}\n\tif (modulus)\n\t\t{\n\t\tEVP_PKEY *pubkey;\n\t\tif (x509)\n\t\t\tpubkey=X509_get_pubkey(x509ss);\n\t\telse\n\t\t\tpubkey=X509_REQ_get_pubkey(req);\n\t\tif (pubkey == NULL)\n\t\t\t{\n\t\t\tfprintf(stdout,"Modulus=unavailable\\n");\n\t\t\tgoto end;\n\t\t\t}\n\t\tfprintf(stdout,"Modulus=");\n#ifndef NO_RSA\n\t\tif (pubkey->type == EVP_PKEY_RSA)\n\t\t\tBN_print(out,pubkey->pkey.rsa->n);\n\t\telse\n#endif\n\t\t\tfprintf(stdout,"Wrong Algorithm type");\n\t\tfprintf(stdout,"\\n");\n\t\t}\n\tif (!noout && !x509)\n\t\t{\n\t\tif \t(outformat == FORMAT_ASN1)\n\t\t\ti=i2d_X509_REQ_bio(out,req);\n\t\telse if (outformat == FORMAT_PEM)\n\t\t\ti=PEM_write_bio_X509_REQ(out,req);\n\t\telse\t{\n\t\t\tBIO_printf(bio_err,"bad output format specified for outfile\\n");\n\t\t\tgoto end;\n\t\t\t}\n\t\tif (!i)\n\t\t\t{\n\t\t\tBIO_printf(bio_err,"unable to write X509 request\\n");\n\t\t\tgoto end;\n\t\t\t}\n\t\t}\n\tif (!noout && x509 && (x509ss != NULL))\n\t\t{\n\t\tif \t(outformat == FORMAT_ASN1)\n\t\t\ti=i2d_X509_bio(out,x509ss);\n\t\telse if (outformat == FORMAT_PEM)\n\t\t\ti=PEM_write_bio_X509(out,x509ss);\n\t\telse\t{\n\t\t\tBIO_printf(bio_err,"bad output format specified for outfile\\n");\n\t\t\tgoto end;\n\t\t\t}\n\t\tif (!i)\n\t\t\t{\n\t\t\tBIO_printf(bio_err,"unable to write X509 certificate\\n");\n\t\t\tgoto end;\n\t\t\t}\n\t\t}\n\tex=0;\nend:\n\tif (ex)\n\t\t{\n\t\tERR_print_errors(bio_err);\n\t\t}\n\tif ((req_conf != NULL) && (req_conf != config)) CONF_free(req_conf);\n\tBIO_free(in);\n\tBIO_free(out);\n\tEVP_PKEY_free(pkey);\n\tX509_REQ_free(req);\n\tX509_free(x509ss);\n\tX509V3_EXT_cleanup();\n\tOBJ_cleanup();\n#ifndef NO_DSA\n\tif (dsa_params != NULL) DSA_free(dsa_params);\n#endif\n\tEXIT(ex);\n\t}', 'EVP_PKEY *X509_get_pubkey(X509 *x)\n\t{\n\tif ((x == NULL) || (x->cert_info == NULL))\n\t\treturn(NULL);\n\treturn(X509_PUBKEY_get(x->cert_info->key));\n\t}', 'EVP_PKEY *X509_PUBKEY_get(X509_PUBKEY *key)\n\t{\n\tEVP_PKEY *ret=NULL;\n\tlong j;\n\tint type;\n\tunsigned char *p;\n#ifndef NO_DSA\n\tX509_ALGOR *a;\n#endif\n\tif (key == NULL) goto err;\n\tif (key->pkey != NULL)\n\t {\n\t CRYPTO_add(&key->pkey->references,1,CRYPTO_LOCK_EVP_PKEY);\n\t return(key->pkey);\n\t }\n\tif (key->public_key == NULL) goto err;\n\ttype=OBJ_obj2nid(key->algor->algorithm);\n\tp=key->public_key->data;\n j=key->public_key->length;\n if ((ret=d2i_PublicKey(type,NULL,&p,(long)j)) == NULL)\n\t\t{\n\t\tX509err(X509_F_X509_PUBKEY_GET,X509_R_ERR_ASN1_LIB);\n\t\tgoto err;\n\t\t}\n\tret->save_parameters=0;\n#ifndef NO_DSA\n\ta=key->algor;\n\tif (ret->type == EVP_PKEY_DSA)\n\t\t{\n\t\tif (a->parameter->type == V_ASN1_SEQUENCE)\n\t\t\t{\n\t\t\tret->pkey.dsa->write_params=0;\n\t\t\tp=a->parameter->value.sequence->data;\n\t\t\tj=a->parameter->value.sequence->length;\n\t\t\tif (!d2i_DSAparams(&ret->pkey.dsa,&p,(long)j))\n\t\t\t\tgoto err;\n\t\t\t}\n\t\tret->save_parameters=1;\n\t\t}\n#endif\n\tkey->pkey=ret;\n\tCRYPTO_add(&ret->references,1,CRYPTO_LOCK_EVP_PKEY);\n\treturn(ret);\nerr:\n\tif (ret != NULL)\n\t\tEVP_PKEY_free(ret);\n\treturn(NULL);\n\t}', 'DSA *d2i_DSAparams(DSA **a, unsigned char **pp, long length)\n\t{\n\tint i=ERR_R_NESTED_ASN1_ERROR;\n\tASN1_INTEGER *bs=NULL;\n\tM_ASN1_D2I_vars(a,DSA *,DSA_new);\n\tM_ASN1_D2I_Init();\n\tM_ASN1_D2I_start_sequence();\n\tM_ASN1_D2I_get(bs,d2i_ASN1_INTEGER);\n\tif ((ret->p=BN_bin2bn(bs->data,bs->length,ret->p)) == NULL) goto err_bn;\n\tM_ASN1_D2I_get(bs,d2i_ASN1_INTEGER);\n\tif ((ret->q=BN_bin2bn(bs->data,bs->length,ret->q)) == NULL) goto err_bn;\n\tM_ASN1_D2I_get(bs,d2i_ASN1_INTEGER);\n\tif ((ret->g=BN_bin2bn(bs->data,bs->length,ret->g)) == NULL) goto err_bn;\n\tASN1_BIT_STRING_free(bs);\n\tM_ASN1_D2I_Finish_2(a);\nerr_bn:\n\ti=ERR_R_BN_LIB;\nerr:\n\tASN1err(ASN1_F_D2I_DSAPARAMS,i);\n\tif ((ret != NULL) && ((a == NULL) || (*a != ret))) DSA_free(ret);\n\tif (bs != NULL) ASN1_BIT_STRING_free(bs);\n\treturn(NULL);\n\t}', 'int BN_num_bits(BIGNUM *a)\n\t{\n\tBN_ULONG l;\n\tint i;\n\tbn_check_top(a);\n\tif (a->top == 0) return(0);\n\tl=a->d[a->top-1];\n\ti=(a->top-1)*BN_BITS2;\n\tif (l == 0)\n\t\t{\n#if !defined(NO_STDIO) && !defined(WIN16)\n\t\tfprintf(stderr,"BAD TOP VALUE\\n");\n#endif\n\t\tabort();\n\t\t}\n\treturn(i+BN_num_bits_word(l));\n\t}', 'int DSA_generate_key(DSA *dsa)\n\t{\n\tint ok=0;\n\tunsigned int i;\n\tBN_CTX *ctx=NULL;\n\tBIGNUM *pub_key=NULL,*priv_key=NULL;\n\tif ((ctx=BN_CTX_new()) == NULL) goto err;\n\tif (dsa->priv_key == NULL)\n\t\t{\n\t\tif ((priv_key=BN_new()) == NULL) goto err;\n\t\t}\n\telse\n\t\tpriv_key=dsa->priv_key;\n\ti=BN_num_bits(dsa->q);\n\tfor (;;)\n\t\t{\n\t\tBN_rand(priv_key,i,1,0);\n\t\tif (BN_cmp(priv_key,dsa->q) >= 0)\n\t\t\tBN_sub(priv_key,priv_key,dsa->q);\n\t\tif (!BN_is_zero(priv_key)) break;\n\t\t}\n\tif (dsa->pub_key == NULL)\n\t\t{\n\t\tif ((pub_key=BN_new()) == NULL) goto err;\n\t\t}\n\telse\n\t\tpub_key=dsa->pub_key;\n\tif (!BN_mod_exp(pub_key,dsa->g,priv_key,dsa->p,ctx)) goto err;\n\tdsa->priv_key=priv_key;\n\tdsa->pub_key=pub_key;\n\tok=1;\nerr:\n\tif ((pub_key != NULL) && (dsa->pub_key == NULL)) BN_free(pub_key);\n\tif ((priv_key != NULL) && (dsa->priv_key == NULL)) BN_free(priv_key);\n\tif (ctx != NULL) BN_CTX_free(ctx);\n\treturn(ok);\n\t}', 'int BN_mod_exp(BIGNUM *r, BIGNUM *a, BIGNUM *p, BIGNUM *m, BN_CTX *ctx)\n\t{\n\tint ret;\n\tbn_check_top(a);\n\tbn_check_top(p);\n\tbn_check_top(m);\n#ifdef MONT_MUL_MOD\n\tif (BN_is_odd(m))\n\t\t{ ret=BN_mod_exp_mont(r,a,p,m,ctx,NULL); }\n\telse\n#endif\n#ifdef RECP_MUL_MOD\n\t\t{ ret=BN_mod_exp_recp(r,a,p,m,ctx); }\n#else\n\t\t{ ret=BN_mod_exp_simple(r,a,p,m,ctx); }\n#endif\n\treturn(ret);\n\t}', 'int BN_mod_exp_recp(BIGNUM *r, BIGNUM *a, BIGNUM *p, BIGNUM *m, BN_CTX *ctx)\n\t{\n\tint i,j,bits,ret=0,wstart,wend,window,wvalue;\n\tint start=1,ts=0;\n\tBIGNUM *aa;\n\tBIGNUM val[TABLE_SIZE];\n\tBN_RECP_CTX recp;\n\taa= &(ctx->bn[ctx->tos++]);\n\tbits=BN_num_bits(p);\n\tif (bits == 0)\n\t\t{\n\t\tBN_one(r);\n\t\treturn(1);\n\t\t}\n\tBN_RECP_CTX_init(&recp);\n\tif (BN_RECP_CTX_set(&recp,m,ctx) <= 0) goto err;\n\tBN_init(&(val[0]));\n\tts=1;\n\tif (!BN_mod(&(val[0]),a,m,ctx)) goto err;\n\tif (!BN_mod_mul_reciprocal(aa,&(val[0]),&(val[0]),&recp,ctx))\n\t\tgoto err;\n\tif (bits <= 17)\n\t\twindow=1;\n\telse if (bits >= 256)\n\t\twindow=5;\n\telse if (bits >= 128)\n\t\twindow=4;\n\telse\n\t\twindow=3;\n\tj=1<<(window-1);\n\tfor (i=1; i<j; i++)\n\t\t{\n\t\tBN_init(&val[i]);\n\t\tif (!BN_mod_mul_reciprocal(&(val[i]),&(val[i-1]),aa,&recp,ctx))\n\t\t\tgoto err;\n\t\t}\n\tts=i;\n\tstart=1;\n\twvalue=0;\n\twstart=bits-1;\n\twend=0;\n\tif (!BN_one(r)) goto err;\n\tfor (;;)\n\t\t{\n\t\tif (BN_is_bit_set(p,wstart) == 0)\n\t\t\t{\n\t\t\tif (!start)\n\t\t\t\tif (!BN_mod_mul_reciprocal(r,r,r,&recp,ctx))\n\t\t\t\tgoto err;\n\t\t\tif (wstart == 0) break;\n\t\t\twstart--;\n\t\t\tcontinue;\n\t\t\t}\n\t\tj=wstart;\n\t\twvalue=1;\n\t\twend=0;\n\t\tfor (i=1; i<window; i++)\n\t\t\t{\n\t\t\tif (wstart-i < 0) break;\n\t\t\tif (BN_is_bit_set(p,wstart-i))\n\t\t\t\t{\n\t\t\t\twvalue<<=(i-wend);\n\t\t\t\twvalue|=1;\n\t\t\t\twend=i;\n\t\t\t\t}\n\t\t\t}\n\t\tj=wend+1;\n\t\tif (!start)\n\t\t\tfor (i=0; i<j; i++)\n\t\t\t\t{\n\t\t\t\tif (!BN_mod_mul_reciprocal(r,r,r,&recp,ctx))\n\t\t\t\t\tgoto err;\n\t\t\t\t}\n\t\tif (!BN_mod_mul_reciprocal(r,r,&(val[wvalue>>1]),&recp,ctx))\n\t\t\tgoto err;\n\t\twstart-=wend+1;\n\t\twvalue=0;\n\t\tstart=0;\n\t\tif (wstart < 0) break;\n\t\t}\n\tret=1;\nerr:\n\tctx->tos--;\n\tfor (i=0; i<ts; i++)\n\t\tBN_clear_free(&(val[i]));\n\tBN_RECP_CTX_free(&recp);\n\treturn(ret);\n\t}', 'int BN_RECP_CTX_set(BN_RECP_CTX *recp, BIGNUM *d, BN_CTX *ctx)\n\t{\n\tBN_copy(&(recp->N),d);\n\tBN_zero(&(recp->Nr));\n\trecp->num_bits=BN_num_bits(d);\n\trecp->shift=0;\n\treturn(1);\n\t}', 'BIGNUM *BN_copy(BIGNUM *a, BIGNUM *b)\n\t{\n\tint i;\n\tBN_ULONG *A,*B;\n\tbn_check_top(b);\n\tif (a == b) return(a);\n\tif (bn_wexpand(a,b->top) == NULL) return(NULL);\n#if 1\n\tA=a->d;\n\tB=b->d;\n\tfor (i=b->top&(~7); i>0; i-=8)\n\t\t{\n\t\tA[0]=B[0];\n\t\tA[1]=B[1];\n\t\tA[2]=B[2];\n\t\tA[3]=B[3];\n\t\tA[4]=B[4];\n\t\tA[5]=B[5];\n\t\tA[6]=B[6];\n\t\tA[7]=B[7];\n\t\tA+=8;\n\t\tB+=8;\n\t\t}\n\tswitch (b->top&7)\n\t\t{\n\tcase 7:\n\t\tA[6]=B[6];\n\tcase 6:\n\t\tA[5]=B[5];\n\tcase 5:\n\t\tA[4]=B[4];\n\tcase 4:\n\t\tA[3]=B[3];\n\tcase 3:\n\t\tA[2]=B[2];\n\tcase 2:\n\t\tA[1]=B[1];\n\tcase 1:\n\t\tA[0]=B[0];\n case 0:\n\t\t;\n\t\t}\n#else\n\tmemcpy(a->d,b->d,sizeof(b->d[0])*b->top);\n#endif\n\ta->top=b->top;\n\tif ((a->top == 0) && (a->d != NULL))\n\t\ta->d[0]=0;\n\ta->neg=b->neg;\n\treturn(a);\n\t}', 'int BN_mod_mul_reciprocal(BIGNUM *r, BIGNUM *x, BIGNUM *y, BN_RECP_CTX *recp,\n\t BN_CTX *ctx)\n\t{\n\tint ret=0;\n\tBIGNUM *a;\n\ta= &(ctx->bn[ctx->tos++]);\n\tif (y != NULL)\n\t\t{\n\t\tif (x == y)\n\t\t\t{ if (!BN_sqr(a,x,ctx)) goto err; }\n\t\telse\n\t\t\t{ if (!BN_mul(a,x,y,ctx)) goto err; }\n\t\t}\n\telse\n\t\ta=x;\n\tBN_div_recp(NULL,r,a,recp,ctx);\n\tret=1;\nerr:\n\tctx->tos--;\n\treturn(ret);\n\t}', 'int BN_div_recp(BIGNUM *dv, BIGNUM *rem, BIGNUM *m, BN_RECP_CTX *recp,\n\t BN_CTX *ctx)\n\t{\n\tint i,j,tos,ret=0,ex;\n\tBIGNUM *a,*b,*d,*r;\n\ttos=ctx->tos;\n\ta= &(ctx->bn[ctx->tos++]);\n\tb= &(ctx->bn[ctx->tos++]);\n\tif (dv != NULL)\n\t\td=dv;\n\telse\n\t\td= &(ctx->bn[ctx->tos++]);\n\tif (rem != NULL)\n\t\tr=rem;\n\telse\n\t\tr= &(ctx->bn[ctx->tos++]);\n\tif (BN_ucmp(m,&(recp->N)) < 0)\n\t\t{\n\t\tBN_zero(d);\n\t\tBN_copy(r,m);\n\t\tctx->tos=tos;\n\t\treturn(1);\n\t\t}\n\ti=BN_num_bits(m);\n\tj=recp->num_bits*2;\n\tif (j > i)\n\t\t{\n\t\ti=j;\n\t\tex=0;\n\t\t}\n\telse\n\t\t{\n\t\tex=(i-j)/2;\n\t\t}\n\tj=i/2;\n\tif (i != recp->shift)\n\t\trecp->shift=BN_reciprocal(&(recp->Nr),&(recp->N),\n\t\t\ti,ctx);\n\tif (!BN_rshift(a,m,j-ex)) goto err;\n\tif (!BN_mul(b,a,&(recp->Nr),ctx)) goto err;\n\tif (!BN_rshift(d,b,j+ex)) goto err;\n\td->neg=0;\n\tif (!BN_mul(b,&(recp->N),d,ctx)) goto err;\n\tif (!BN_usub(r,m,b)) goto err;\n\tr->neg=0;\n\tj=0;\n#if 1\n\twhile (BN_ucmp(r,&(recp->N)) >= 0)\n\t\t{\n\t\tif (j++ > 2)\n\t\t\t{\n\t\t\tBNerr(BN_F_BN_MOD_MUL_RECIPROCAL,BN_R_BAD_RECIPROCAL);\n\t\t\tgoto err;\n\t\t\t}\n\t\tif (!BN_usub(r,r,&(recp->N))) goto err;\n\t\tif (!BN_add_word(d,1)) goto err;\n\t\t}\n#endif\n\tr->neg=BN_is_zero(r)?0:m->neg;\n\td->neg=m->neg^recp->N.neg;\n\tret=1;\nerr:\n\tctx->tos=tos;\n\treturn(ret);\n\t}', 'int BN_mul(BIGNUM *r, BIGNUM *a, BIGNUM *b, BN_CTX *ctx)\n\t{\n\tint top,al,bl;\n\tBIGNUM *rr;\n#ifdef BN_RECURSION\n\tBIGNUM *t;\n\tint i,j,k;\n#endif\n#ifdef BN_COUNT\nprintf("BN_mul %d * %d\\n",a->top,b->top);\n#endif\n\tbn_check_top(a);\n\tbn_check_top(b);\n\tbn_check_top(r);\n\tal=a->top;\n\tbl=b->top;\n\tr->neg=a->neg^b->neg;\n\tif ((al == 0) || (bl == 0))\n\t\t{\n\t\tBN_zero(r);\n\t\treturn(1);\n\t\t}\n\ttop=al+bl;\n\tif ((r == a) || (r == b))\n\t\trr= &(ctx->bn[ctx->tos+1]);\n\telse\n\t\trr=r;\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n\tif (al == bl)\n\t\t{\n# ifdef BN_MUL_COMBA\n if (al == 8)\n\t\t\t{\n\t\t\tif (bn_wexpand(rr,16) == NULL) return(0);\n\t\t\tr->top=16;\n\t\t\tbn_mul_comba8(rr->d,a->d,b->d);\n\t\t\tgoto end;\n\t\t\t}\n\t\telse\n# endif\n#ifdef BN_RECURSION\n\t\tif (al < BN_MULL_SIZE_NORMAL)\n#endif\n\t\t\t{\n\t\t\tif (bn_wexpand(rr,top) == NULL) return(0);\n\t\t\trr->top=top;\n\t\t\tbn_mul_normal(rr->d,a->d,al,b->d,bl);\n\t\t\tgoto end;\n\t\t\t}\n# ifdef BN_RECURSION\n\t\tgoto symetric;\n# endif\n\t\t}\n#endif\n#ifdef BN_RECURSION\n\telse if ((al < BN_MULL_SIZE_NORMAL) || (bl < BN_MULL_SIZE_NORMAL))\n\t\t{\n\t\tif (bn_wexpand(rr,top) == NULL) return(0);\n\t\trr->top=top;\n\t\tbn_mul_normal(rr->d,a->d,al,b->d,bl);\n\t\tgoto end;\n\t\t}\n\telse\n\t\t{\n\t\ti=(al-bl);\n\t\tif ((i == 1) && !BN_get_flags(b,BN_FLG_STATIC_DATA))\n\t\t\t{\n\t\t\tbn_wexpand(b,al);\n\t\t\tb->d[bl]=0;\n\t\t\tbl++;\n\t\t\tgoto symetric;\n\t\t\t}\n\t\telse if ((i == -1) && !BN_get_flags(a,BN_FLG_STATIC_DATA))\n\t\t\t{\n\t\t\tbn_wexpand(a,bl);\n\t\t\ta->d[al]=0;\n\t\t\tal++;\n\t\t\tgoto symetric;\n\t\t\t}\n\t\t}\n#endif\n\tif (bn_wexpand(rr,top) == NULL) return(0);\n\trr->top=top;\n\tbn_mul_normal(rr->d,a->d,al,b->d,bl);\n#ifdef BN_RECURSION\n\tif (0)\n\t\t{\nsymetric:\n\t\tj=BN_num_bits_word((BN_ULONG)al);\n\t\tj=1<<(j-1);\n\t\tk=j+j;\n\t\tt= &(ctx->bn[ctx->tos]);\n\t\tif (al == j)\n\t\t\t{\n\t\t\tbn_wexpand(t,k*2);\n\t\t\tbn_wexpand(rr,k*2);\n\t\t\tbn_mul_recursive(rr->d,a->d,b->d,al,t->d);\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tbn_wexpand(a,k);\n\t\t\tbn_wexpand(b,k);\n\t\t\tbn_wexpand(t,k*4);\n\t\t\tbn_wexpand(rr,k*4);\n\t\t\tfor (i=a->top; i<k; i++)\n\t\t\t\ta->d[i]=0;\n\t\t\tfor (i=b->top; i<k; i++)\n\t\t\t\tb->d[i]=0;\n\t\t\tbn_mul_part_recursive(rr->d,a->d,b->d,al-j,j,t->d);\n\t\t\t}\n\t\trr->top=top;\n\t\t}\n#endif\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\nend:\n#endif\n\tbn_fix_top(rr);\n\tif (r != rr) BN_copy(r,rr);\n\treturn(1);\n\t}', 'BIGNUM *bn_expand2(BIGNUM *b, int words)\n\t{\n\tBN_ULONG *A,*B,*a;\n\tint i,j;\n\tbn_check_top(b);\n\tif (words > b->max)\n\t\t{\n\t\tbn_check_top(b);\n\t\tif (BN_get_flags(b,BN_FLG_STATIC_DATA))\n\t\t\t{\n\t\t\tBNerr(BN_F_BN_EXPAND2,BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);\n\t\t\treturn(NULL);\n\t\t\t}\n\t\ta=A=(BN_ULONG *)Malloc(sizeof(BN_ULONG)*(words+1));\n\t\tif (A == NULL)\n\t\t\t{\n\t\t\tBNerr(BN_F_BN_EXPAND2,ERR_R_MALLOC_FAILURE);\n\t\t\treturn(NULL);\n\t\t\t}\nmemset(A,0x5c,sizeof(BN_ULONG)*(words+1));\n#if 1\n\t\tB=b->d;\n\t\tif (B != NULL)\n\t\t\t{\n\t\t\tfor (i=b->top&(~7); i>0; i-=8)\n\t\t\t\t{\n\t\t\t\tA[0]=B[0]; A[1]=B[1]; A[2]=B[2]; A[3]=B[3];\n\t\t\t\tA[4]=B[4]; A[5]=B[5]; A[6]=B[6]; A[7]=B[7];\n\t\t\t\tA+=8;\n\t\t\t\tB+=8;\n\t\t\t\t}\n\t\t\tswitch (b->top&7)\n\t\t\t\t{\n\t\t\tcase 7:\n\t\t\t\tA[6]=B[6];\n\t\t\tcase 6:\n\t\t\t\tA[5]=B[5];\n\t\t\tcase 5:\n\t\t\t\tA[4]=B[4];\n\t\t\tcase 4:\n\t\t\t\tA[3]=B[3];\n\t\t\tcase 3:\n\t\t\t\tA[2]=B[2];\n\t\t\tcase 2:\n\t\t\t\tA[1]=B[1];\n\t\t\tcase 1:\n\t\t\t\tA[0]=B[0];\n\t\t\tcase 0:\n\t\t\t\t;\n\t\t\t\t}\n\t\t\tFree(b->d);\n\t\t\t}\n\t\tb->d=a;\n\t\tb->max=words;\n\t\tB= &(b->d[b->top]);\n\t\tj=(b->max - b->top) & ~7;\n\t\tfor (i=0; i<j; i+=8)\n\t\t\t{\n\t\t\tB[0]=0; B[1]=0; B[2]=0; B[3]=0;\n\t\t\tB[4]=0; B[5]=0; B[6]=0; B[7]=0;\n\t\t\tB+=8;\n\t\t\t}\n\t\tj=(b->max - b->top) & 7;\n\t\tfor (i=0; i<j; i++)\n\t\t\t{\n\t\t\tB[0]=0;\n\t\t\tB++;\n\t\t\t}\n#else\n\t\t\tmemcpy(a->d,b->d,sizeof(b->d[0])*b->top);\n#endif\n\t\t}\n\treturn(b);\n\t}']
854
0
https://github.com/libav/libav/blob/9f54e461fecec7a97ec1b97ae4468135ea770609/ffmpeg.c/#L2784
static int opt_metadata(const char *opt, const char *arg) { char *mid= strchr(arg, '='); if(!mid){ fprintf(stderr, "Missing =\n"); ffmpeg_exit(1); } *mid++= 0; av_metadata_set2(&metadata, arg, mid, 0); return 0; }
['static int opt_metadata(const char *opt, const char *arg)\n{\n char *mid= strchr(arg, \'=\');\n if(!mid){\n fprintf(stderr, "Missing =\\n");\n ffmpeg_exit(1);\n }\n *mid++= 0;\n av_metadata_set2(&metadata, arg, mid, 0);\n return 0;\n}']
855
0
https://github.com/openssl/openssl/blob/f006217bb628d05a2d5b866ff252bd94e3477e1f/crypto/x509/x509_vfy.c/#L2643
static int build_chain(X509_STORE_CTX *ctx) { struct dane_st *dane = (struct dane_st *)ctx->dane; int num = sk_X509_num(ctx->chain); X509 *cert = sk_X509_value(ctx->chain, num - 1); int ss = cert_self_signed(cert); STACK_OF(X509) *sktmp = NULL; unsigned int search; int may_trusted = 0; int may_alternate = 0; int trust = X509_TRUST_UNTRUSTED; int alt_untrusted = 0; int depth; int ok = 0; int i; OPENSSL_assert(num == 1 && ctx->num_untrusted == num); #define S_DOUNTRUSTED (1 << 0) #define S_DOTRUSTED (1 << 1) #define S_DOALTERNATE (1 << 2) search = (ctx->untrusted != NULL) ? S_DOUNTRUSTED : 0; if (DANETLS_HAS_PKIX(dane) || !DANETLS_HAS_DANE(dane)) { if (search == 0 || ctx->param->flags & X509_V_FLAG_TRUSTED_FIRST) search |= S_DOTRUSTED; else if (!(ctx->param->flags & X509_V_FLAG_NO_ALT_CHAINS)) may_alternate = 1; may_trusted = 1; } if (ctx->untrusted && (sktmp = sk_X509_dup(ctx->untrusted)) == NULL) { X509err(X509_F_BUILD_CHAIN, ERR_R_MALLOC_FAILURE); return 0; } if (DANETLS_ENABLED(dane) && dane->certs != NULL) { for (i = 0; i < sk_X509_num(dane->certs); ++i) { if (!sk_X509_push(sktmp, sk_X509_value(dane->certs, i))) { sk_X509_free(sktmp); X509err(X509_F_BUILD_CHAIN, ERR_R_MALLOC_FAILURE); return 0; } } } if (ctx->param->depth > INT_MAX/2) ctx->param->depth = INT_MAX/2; depth = ctx->param->depth + 1; while (search != 0) { X509 *x; X509 *xtmp = NULL; if ((search & S_DOTRUSTED) != 0) { STACK_OF(X509) *hide = ctx->chain; i = num = sk_X509_num(ctx->chain); if ((search & S_DOALTERNATE) != 0) { i = alt_untrusted; } x = sk_X509_value(ctx->chain, i-1); ctx->chain = NULL; ok = (depth < num) ? 0 : ctx->get_issuer(&xtmp, ctx, x); ctx->chain = hide; if (ok < 0) { trust = X509_TRUST_REJECTED; search = 0; continue; } if (ok > 0) { if ((search & S_DOALTERNATE) != 0) { OPENSSL_assert(num > i && i > 0 && ss == 0); search &= ~S_DOALTERNATE; for (; num > i; --num) X509_free(sk_X509_pop(ctx->chain)); ctx->num_untrusted = num; if (DANETLS_ENABLED(dane) && dane->mdpth >= ctx->num_untrusted) { dane->mdpth = -1; X509_free(dane->mcert); dane->mcert = NULL; } if (DANETLS_ENABLED(dane) && dane->pdpth >= ctx->num_untrusted) dane->pdpth = -1; } if (ss == 0) { if (!sk_X509_push(ctx->chain, x = xtmp)) { X509_free(xtmp); X509err(X509_F_BUILD_CHAIN, ERR_R_MALLOC_FAILURE); trust = X509_TRUST_REJECTED; search = 0; continue; } ss = cert_self_signed(x); } else if (num == ctx->num_untrusted) { if (X509_cmp(x, xtmp) != 0) { X509_free(xtmp); ok = 0; } else { X509_free(x); ctx->num_untrusted = --num; (void) sk_X509_set(ctx->chain, num, x = xtmp); } } if (ok) { OPENSSL_assert(ctx->num_untrusted <= num); search &= ~S_DOUNTRUSTED; switch (trust = check_trust(ctx, num)) { case X509_TRUST_TRUSTED: case X509_TRUST_REJECTED: search = 0; continue; } if (ss == 0) continue; } } if ((search & S_DOUNTRUSTED) == 0) { if ((search & S_DOALTERNATE) != 0 && --alt_untrusted > 0) continue; if (!may_alternate || (search & S_DOALTERNATE) != 0 || ctx->num_untrusted < 2) break; search |= S_DOALTERNATE; alt_untrusted = ctx->num_untrusted - 1; ss = 0; } } if ((search & S_DOUNTRUSTED) != 0) { num = sk_X509_num(ctx->chain); OPENSSL_assert(num == ctx->num_untrusted); x = sk_X509_value(ctx->chain, num-1); xtmp = (depth < num) ? NULL : find_issuer(ctx, sktmp, x); if (xtmp == NULL) { search &= ~S_DOUNTRUSTED; if (may_trusted) search |= S_DOTRUSTED; continue; } if (!sk_X509_push(ctx->chain, x = xtmp)) { X509err(X509_F_BUILD_CHAIN, ERR_R_MALLOC_FAILURE); trust = X509_TRUST_REJECTED; search = 0; continue; } X509_up_ref(x); ++ctx->num_untrusted; ss = cert_self_signed(xtmp); (void) sk_X509_delete_ptr(sktmp, x); switch (trust = check_dane_issuer(ctx, ctx->num_untrusted - 1)) { case X509_TRUST_TRUSTED: case X509_TRUST_REJECTED: search = 0; continue; } } } sk_X509_free(sktmp); num = sk_X509_num(ctx->chain); if (num <= depth) { if (trust == X509_TRUST_UNTRUSTED && DANETLS_HAS_DANE_TA(dane)) trust = check_dane_pkeys(ctx); if (trust == X509_TRUST_UNTRUSTED && num == ctx->num_untrusted) trust = check_trust(ctx, num); } switch (trust) { case X509_TRUST_TRUSTED: return 1; case X509_TRUST_REJECTED: return 0; case X509_TRUST_UNTRUSTED: default: num = sk_X509_num(ctx->chain); ctx->current_cert = sk_X509_value(ctx->chain, num - 1); ctx->error_depth = num-1; if (num > depth) ctx->error = X509_V_ERR_CERT_CHAIN_TOO_LONG; else if (DANETLS_ENABLED(dane) && (!DANETLS_HAS_PKIX(dane) || dane->pdpth >= 0)) ctx->error = X509_V_ERR_CERT_UNTRUSTED; else if (ss && sk_X509_num(ctx->chain) == 1) ctx->error = X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT; else if (ss) ctx->error = X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN; else if (ctx->num_untrusted == num) ctx->error = X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY; else ctx->error = X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT; if (DANETLS_ENABLED(dane)) dane_reset(dane); return ctx->verify_cb(0, ctx); } }
['static int build_chain(X509_STORE_CTX *ctx)\n{\n struct dane_st *dane = (struct dane_st *)ctx->dane;\n int num = sk_X509_num(ctx->chain);\n X509 *cert = sk_X509_value(ctx->chain, num - 1);\n int ss = cert_self_signed(cert);\n STACK_OF(X509) *sktmp = NULL;\n unsigned int search;\n int may_trusted = 0;\n int may_alternate = 0;\n int trust = X509_TRUST_UNTRUSTED;\n int alt_untrusted = 0;\n int depth;\n int ok = 0;\n int i;\n OPENSSL_assert(num == 1 && ctx->num_untrusted == num);\n#define S_DOUNTRUSTED (1 << 0)\n#define S_DOTRUSTED (1 << 1)\n#define S_DOALTERNATE (1 << 2)\n search = (ctx->untrusted != NULL) ? S_DOUNTRUSTED : 0;\n if (DANETLS_HAS_PKIX(dane) || !DANETLS_HAS_DANE(dane)) {\n if (search == 0 || ctx->param->flags & X509_V_FLAG_TRUSTED_FIRST)\n search |= S_DOTRUSTED;\n else if (!(ctx->param->flags & X509_V_FLAG_NO_ALT_CHAINS))\n may_alternate = 1;\n may_trusted = 1;\n }\n if (ctx->untrusted && (sktmp = sk_X509_dup(ctx->untrusted)) == NULL) {\n X509err(X509_F_BUILD_CHAIN, ERR_R_MALLOC_FAILURE);\n return 0;\n }\n if (DANETLS_ENABLED(dane) && dane->certs != NULL) {\n for (i = 0; i < sk_X509_num(dane->certs); ++i) {\n if (!sk_X509_push(sktmp, sk_X509_value(dane->certs, i))) {\n sk_X509_free(sktmp);\n X509err(X509_F_BUILD_CHAIN, ERR_R_MALLOC_FAILURE);\n return 0;\n }\n }\n }\n if (ctx->param->depth > INT_MAX/2)\n ctx->param->depth = INT_MAX/2;\n depth = ctx->param->depth + 1;\n while (search != 0) {\n X509 *x;\n X509 *xtmp = NULL;\n if ((search & S_DOTRUSTED) != 0) {\n STACK_OF(X509) *hide = ctx->chain;\n i = num = sk_X509_num(ctx->chain);\n if ((search & S_DOALTERNATE) != 0) {\n i = alt_untrusted;\n }\n x = sk_X509_value(ctx->chain, i-1);\n ctx->chain = NULL;\n ok = (depth < num) ? 0 : ctx->get_issuer(&xtmp, ctx, x);\n ctx->chain = hide;\n if (ok < 0) {\n trust = X509_TRUST_REJECTED;\n search = 0;\n continue;\n }\n if (ok > 0) {\n if ((search & S_DOALTERNATE) != 0) {\n OPENSSL_assert(num > i && i > 0 && ss == 0);\n search &= ~S_DOALTERNATE;\n for (; num > i; --num)\n X509_free(sk_X509_pop(ctx->chain));\n ctx->num_untrusted = num;\n if (DANETLS_ENABLED(dane) &&\n dane->mdpth >= ctx->num_untrusted) {\n dane->mdpth = -1;\n X509_free(dane->mcert);\n dane->mcert = NULL;\n }\n if (DANETLS_ENABLED(dane) &&\n dane->pdpth >= ctx->num_untrusted)\n dane->pdpth = -1;\n }\n if (ss == 0) {\n if (!sk_X509_push(ctx->chain, x = xtmp)) {\n X509_free(xtmp);\n X509err(X509_F_BUILD_CHAIN, ERR_R_MALLOC_FAILURE);\n trust = X509_TRUST_REJECTED;\n search = 0;\n continue;\n }\n ss = cert_self_signed(x);\n } else if (num == ctx->num_untrusted) {\n if (X509_cmp(x, xtmp) != 0) {\n X509_free(xtmp);\n ok = 0;\n } else {\n X509_free(x);\n ctx->num_untrusted = --num;\n (void) sk_X509_set(ctx->chain, num, x = xtmp);\n }\n }\n if (ok) {\n OPENSSL_assert(ctx->num_untrusted <= num);\n search &= ~S_DOUNTRUSTED;\n switch (trust = check_trust(ctx, num)) {\n case X509_TRUST_TRUSTED:\n case X509_TRUST_REJECTED:\n search = 0;\n continue;\n }\n if (ss == 0)\n continue;\n }\n }\n if ((search & S_DOUNTRUSTED) == 0) {\n if ((search & S_DOALTERNATE) != 0 && --alt_untrusted > 0)\n continue;\n if (!may_alternate || (search & S_DOALTERNATE) != 0 ||\n ctx->num_untrusted < 2)\n break;\n search |= S_DOALTERNATE;\n alt_untrusted = ctx->num_untrusted - 1;\n ss = 0;\n }\n }\n if ((search & S_DOUNTRUSTED) != 0) {\n num = sk_X509_num(ctx->chain);\n OPENSSL_assert(num == ctx->num_untrusted);\n x = sk_X509_value(ctx->chain, num-1);\n xtmp = (depth < num) ? NULL : find_issuer(ctx, sktmp, x);\n if (xtmp == NULL) {\n search &= ~S_DOUNTRUSTED;\n if (may_trusted)\n search |= S_DOTRUSTED;\n continue;\n }\n if (!sk_X509_push(ctx->chain, x = xtmp)) {\n X509err(X509_F_BUILD_CHAIN, ERR_R_MALLOC_FAILURE);\n trust = X509_TRUST_REJECTED;\n search = 0;\n continue;\n }\n X509_up_ref(x);\n ++ctx->num_untrusted;\n ss = cert_self_signed(xtmp);\n (void) sk_X509_delete_ptr(sktmp, x);\n switch (trust = check_dane_issuer(ctx, ctx->num_untrusted - 1)) {\n case X509_TRUST_TRUSTED:\n case X509_TRUST_REJECTED:\n search = 0;\n continue;\n }\n }\n }\n sk_X509_free(sktmp);\n num = sk_X509_num(ctx->chain);\n if (num <= depth) {\n if (trust == X509_TRUST_UNTRUSTED && DANETLS_HAS_DANE_TA(dane))\n trust = check_dane_pkeys(ctx);\n if (trust == X509_TRUST_UNTRUSTED && num == ctx->num_untrusted)\n trust = check_trust(ctx, num);\n }\n switch (trust) {\n case X509_TRUST_TRUSTED:\n return 1;\n case X509_TRUST_REJECTED:\n return 0;\n case X509_TRUST_UNTRUSTED:\n default:\n num = sk_X509_num(ctx->chain);\n ctx->current_cert = sk_X509_value(ctx->chain, num - 1);\n ctx->error_depth = num-1;\n if (num > depth)\n ctx->error = X509_V_ERR_CERT_CHAIN_TOO_LONG;\n else if (DANETLS_ENABLED(dane) &&\n (!DANETLS_HAS_PKIX(dane) || dane->pdpth >= 0))\n ctx->error = X509_V_ERR_CERT_UNTRUSTED;\n else if (ss && sk_X509_num(ctx->chain) == 1)\n ctx->error = X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT;\n else if (ss)\n ctx->error = X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN;\n else if (ctx->num_untrusted == num)\n ctx->error = X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY;\n else\n ctx->error = X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT;\n if (DANETLS_ENABLED(dane))\n dane_reset(dane);\n return ctx->verify_cb(0, ctx);\n }\n}', 'DEFINE_STACK_OF(X509)', 'int sk_num(const _STACK *st)\n{\n if (st == NULL)\n return -1;\n return st->num;\n}', 'void *sk_value(const _STACK *st, int i)\n{\n if (!st || (i < 0) || (i >= st->num))\n return NULL;\n return st->data[i];\n}', 'static int cert_self_signed(X509 *x)\n{\n X509_check_purpose(x, -1, 0);\n if (x->ex_flags & EXFLAG_SS)\n return 1;\n else\n return 0;\n}']
856
0
https://github.com/libav/libav/blob/cb4cb7b0ea12b791dde587b1acd504dbb4ec8f41/libavcodec/hqx.c/#L147
static inline void idct_row(int16_t *blk) { int t0, t1, t2, t3, t4, t5, t6, t7, t8, t9, tA, tB, tC, tD, tE, tF; int t10, t11, t12, t13; t0 = (blk[3] * 19266 + blk[5] * 12873) >> 14; t1 = (blk[5] * 19266 - blk[3] * 12873) >> 14; t2 = ((blk[7] * 4520 + blk[1] * 22725) >> 14) - t0; t3 = ((blk[1] * 4520 - blk[7] * 22725) >> 14) - t1; t4 = t0 * 2 + t2; t5 = t1 * 2 + t3; t6 = t2 - t3; t7 = t3 * 2 + t6; t8 = (t6 * 11585) >> 14; t9 = (t7 * 11585) >> 14; tA = (blk[2] * 8867 - blk[6] * 21407) >> 14; tB = (blk[6] * 8867 + blk[2] * 21407) >> 14; tC = blk[0] - blk[4]; tD = blk[4] * 2 + tC; tE = tC - tA; tF = tD - tB; t10 = tF - t5; t11 = tE - t8; t12 = tE + tA * 2 - t9; t13 = tF + tB * 2 - t4; blk[0] = (t13 + t4 * 2 + 4) >> 3; blk[1] = (t12 + t9 * 2 + 4) >> 3; blk[2] = (t11 + t8 * 2 + 4) >> 3; blk[3] = (t10 + t5 * 2 + 4) >> 3; blk[4] = (t10 + 4) >> 3; blk[5] = (t11 + 4) >> 3; blk[6] = (t12 + 4) >> 3; blk[7] = (t13 + 4) >> 3; }
['static int hqx_decode_444a(HQXContext *ctx, AVFrame *pic,\n GetBitContext *gb, int x, int y)\n{\n const int *quants;\n int flag = 0;\n int last_dc;\n int i, ret;\n int cbp;\n cbp = get_vlc2(gb, ctx->cbp_vlc.table, ctx->cbp_vlc.bits, 1);\n for (i = 0; i < 16; i++)\n memset(ctx->block[i], 0, sizeof(**ctx->block) * 64);\n for (i = 0; i < 16; i++)\n ctx->block[i][0] = -0x800;\n if (cbp) {\n if (ctx->interlaced)\n flag = get_bits1(gb);\n quants = hqx_quants[get_bits(gb, 4)];\n cbp |= cbp << 4;\n cbp |= cbp << 8;\n for (i = 0; i < 16; i++) {\n if (i == 0 || i == 4 || i == 8 || i == 12)\n last_dc = 0;\n if (cbp & (1 << i)) {\n int vlc_index = ctx->dcb - 9;\n ret = decode_block(gb, &ctx->dc_vlc[vlc_index], quants,\n ctx->dcb, ctx->block[i], &last_dc);\n if (ret < 0)\n return ret;\n }\n }\n }\n put_blocks(pic, 3, x, y, flag, ctx->block[ 0], ctx->block[ 2], hqx_quant_luma);\n put_blocks(pic, 3, x + 8, y, flag, ctx->block[ 1], ctx->block[ 3], hqx_quant_luma);\n put_blocks(pic, 0, x, y, flag, ctx->block[ 4], ctx->block[ 6], hqx_quant_luma);\n put_blocks(pic, 0, x + 8, y, flag, ctx->block[ 5], ctx->block[ 7], hqx_quant_luma);\n put_blocks(pic, 2, x, y, flag, ctx->block[ 8], ctx->block[10], hqx_quant_chroma);\n put_blocks(pic, 2, x + 8, y, flag, ctx->block[ 9], ctx->block[11], hqx_quant_chroma);\n put_blocks(pic, 1, x, y, flag, ctx->block[12], ctx->block[14], hqx_quant_chroma);\n put_blocks(pic, 1, x + 8, y, flag, ctx->block[13], ctx->block[15], hqx_quant_chroma);\n return 0;\n}', 'static inline void put_blocks(AVFrame *pic, int plane,\n int x, int y, int ilace,\n int16_t *block0, int16_t *block1,\n const uint8_t *quant)\n{\n int fields = ilace ? 2 : 1;\n int lsize = pic->linesize[plane];\n uint8_t *p = pic->data[plane] + x * 2;\n hqx_idct_put((uint16_t *)(p + y * lsize), lsize * fields, block0, quant);\n hqx_idct_put((uint16_t *)(p + (y + (ilace ? 1 : 8)) * lsize),\n lsize * fields, block1, quant);\n}', 'static void hqx_idct_put(uint16_t *dst, ptrdiff_t stride,\n int16_t *block, const uint8_t *quant)\n{\n int i, j;\n hqx_idct(block, quant);\n for (i = 0; i < 8; i++) {\n for (j = 0; j < 8; j++) {\n int v = av_clip(block[j + i * 8] + 0x800, 0, 0x1000);\n dst[j] = (v << 4) | (v >> 8);\n }\n dst += stride >> 1;\n }\n}', 'static void hqx_idct(int16_t *block, const uint8_t *quant)\n{\n int i;\n for (i = 0; i < 8; i++)\n idct_col(block + i, quant + i);\n for (i = 0; i < 8; i++)\n idct_row(block + i * 8);\n}', 'static inline void idct_row(int16_t *blk)\n{\n int t0, t1, t2, t3, t4, t5, t6, t7, t8, t9, tA, tB, tC, tD, tE, tF;\n int t10, t11, t12, t13;\n t0 = (blk[3] * 19266 + blk[5] * 12873) >> 14;\n t1 = (blk[5] * 19266 - blk[3] * 12873) >> 14;\n t2 = ((blk[7] * 4520 + blk[1] * 22725) >> 14) - t0;\n t3 = ((blk[1] * 4520 - blk[7] * 22725) >> 14) - t1;\n t4 = t0 * 2 + t2;\n t5 = t1 * 2 + t3;\n t6 = t2 - t3;\n t7 = t3 * 2 + t6;\n t8 = (t6 * 11585) >> 14;\n t9 = (t7 * 11585) >> 14;\n tA = (blk[2] * 8867 - blk[6] * 21407) >> 14;\n tB = (blk[6] * 8867 + blk[2] * 21407) >> 14;\n tC = blk[0] - blk[4];\n tD = blk[4] * 2 + tC;\n tE = tC - tA;\n tF = tD - tB;\n t10 = tF - t5;\n t11 = tE - t8;\n t12 = tE + tA * 2 - t9;\n t13 = tF + tB * 2 - t4;\n blk[0] = (t13 + t4 * 2 + 4) >> 3;\n blk[1] = (t12 + t9 * 2 + 4) >> 3;\n blk[2] = (t11 + t8 * 2 + 4) >> 3;\n blk[3] = (t10 + t5 * 2 + 4) >> 3;\n blk[4] = (t10 + 4) >> 3;\n blk[5] = (t11 + 4) >> 3;\n blk[6] = (t12 + 4) >> 3;\n blk[7] = (t13 + 4) >> 3;\n}']
857
0
https://github.com/libav/libav/blob/7ff018c1cb43a5fe5ee2049d325cdd785852067a/libavcodec/bitstream.h/#L237
static inline void skip_remaining(BitstreamContext *bc, unsigned n) { #ifdef BITSTREAM_READER_LE bc->bits >>= n; #else bc->bits <<= n; #endif bc->bits_left -= n; }
['static int qt_rtp_parse_packet(AVFormatContext *s, PayloadContext *qt,\n AVStream *st, AVPacket *pkt,\n uint32_t *timestamp, const uint8_t *buf,\n int len, uint16_t seq, int flags)\n{\n AVIOContext pb;\n BitstreamContext bc;\n int packing_scheme, has_payload_desc, has_packet_info, alen,\n has_marker_bit = flags & RTP_FLAG_MARKER,\n keyframe;\n if (qt->remaining) {\n int num = qt->pkt.size / qt->bytes_per_frame;\n if (av_new_packet(pkt, qt->bytes_per_frame))\n return AVERROR(ENOMEM);\n pkt->stream_index = st->index;\n pkt->flags = qt->pkt.flags;\n memcpy(pkt->data,\n &qt->pkt.data[(num - qt->remaining) * qt->bytes_per_frame],\n qt->bytes_per_frame);\n if (--qt->remaining == 0) {\n av_freep(&qt->pkt.data);\n qt->pkt.size = 0;\n }\n return qt->remaining > 0;\n }\n bitstream_init(&bc, buf, len << 3);\n ffio_init_context(&pb, buf, len, 0, NULL, NULL, NULL, NULL);\n if (len < 4)\n return AVERROR_INVALIDDATA;\n bitstream_skip(&bc, 4);\n if ((packing_scheme = bitstream_read(&bc, 2)) == 0)\n return AVERROR_INVALIDDATA;\n keyframe = bitstream_read_bit(&bc);\n has_payload_desc = bitstream_read_bit(&bc);\n has_packet_info = bitstream_read_bit(&bc);\n bitstream_skip(&bc, 23);\n if (has_payload_desc) {\n int data_len, pos, is_start, is_finish;\n uint32_t tag;\n pos = bitstream_tell(&bc) >> 3;\n if (pos + 12 > len)\n return AVERROR_INVALIDDATA;\n bitstream_skip(&bc, 2);\n is_start = bitstream_read_bit(&bc);\n is_finish = bitstream_read_bit(&bc);\n if (!is_start || !is_finish) {\n avpriv_request_sample(s, "RTP-X-QT with payload description "\n "split over several packets");\n return AVERROR_PATCHWELCOME;\n }\n bitstream_skip(&bc, 12);\n data_len = bitstream_read(&bc, 16);\n avio_seek(&pb, pos + 4, SEEK_SET);\n tag = avio_rl32(&pb);\n if ((st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&\n tag != MKTAG(\'v\',\'i\',\'d\',\'e\')) ||\n (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&\n tag != MKTAG(\'s\',\'o\',\'u\',\'n\')))\n return AVERROR_INVALIDDATA;\n avpriv_set_pts_info(st, 32, 1, avio_rb32(&pb));\n if (pos + data_len > len)\n return AVERROR_INVALIDDATA;\n while (avio_tell(&pb) + 4 < pos + data_len) {\n int tlv_len = avio_rb16(&pb);\n tag = avio_rl16(&pb);\n if (avio_tell(&pb) + tlv_len > pos + data_len)\n return AVERROR_INVALIDDATA;\n#define MKTAG16(a,b) MKTAG(a,b,0,0)\n switch (tag) {\n case MKTAG16(\'s\',\'d\'): {\n MOVStreamContext *msc;\n void *priv_data = st->priv_data;\n int nb_streams = s->nb_streams;\n MOVContext *mc = av_mallocz(sizeof(*mc));\n if (!mc)\n return AVERROR(ENOMEM);\n mc->fc = s;\n st->priv_data = msc = av_mallocz(sizeof(MOVStreamContext));\n if (!msc) {\n av_free(mc);\n st->priv_data = priv_data;\n return AVERROR(ENOMEM);\n }\n s->nb_streams = st->index + 1;\n ff_mov_read_stsd_entries(mc, &pb, 1);\n qt->bytes_per_frame = msc->bytes_per_frame;\n av_free(msc);\n av_free(mc);\n st->priv_data = priv_data;\n s->nb_streams = nb_streams;\n break;\n }\n default:\n avio_skip(&pb, tlv_len);\n break;\n }\n }\n avio_skip(&pb, ((avio_tell(&pb) + 3) & ~3) - avio_tell(&pb));\n } else\n avio_seek(&pb, 4, SEEK_SET);\n if (has_packet_info) {\n avpriv_request_sample(s, "RTP-X-QT with packet-specific info");\n return AVERROR_PATCHWELCOME;\n }\n alen = len - avio_tell(&pb);\n if (alen <= 0)\n return AVERROR_INVALIDDATA;\n switch (packing_scheme) {\n case 3:\n if (qt->pkt.size > 0 && qt->timestamp == *timestamp) {\n int err;\n if ((err = av_reallocp(&qt->pkt.data, qt->pkt.size + alen +\n AV_INPUT_BUFFER_PADDING_SIZE)) < 0) {\n qt->pkt.size = 0;\n return err;\n }\n } else {\n av_freep(&qt->pkt.data);\n av_init_packet(&qt->pkt);\n qt->pkt.data = av_realloc(NULL, alen + AV_INPUT_BUFFER_PADDING_SIZE);\n if (!qt->pkt.data)\n return AVERROR(ENOMEM);\n qt->pkt.size = 0;\n qt->timestamp = *timestamp;\n }\n memcpy(qt->pkt.data + qt->pkt.size, buf + avio_tell(&pb), alen);\n qt->pkt.size += alen;\n if (has_marker_bit) {\n int ret = av_packet_from_data(pkt, qt->pkt.data, qt->pkt.size);\n if (ret < 0)\n return ret;\n qt->pkt.size = 0;\n qt->pkt.data = NULL;\n pkt->flags = keyframe ? AV_PKT_FLAG_KEY : 0;\n pkt->stream_index = st->index;\n memset(pkt->data + pkt->size, 0, AV_INPUT_BUFFER_PADDING_SIZE);\n return 0;\n }\n return AVERROR(EAGAIN);\n case 1:\n if (qt->bytes_per_frame == 0 ||\n alen % qt->bytes_per_frame != 0)\n return AVERROR_INVALIDDATA;\n qt->remaining = (alen / qt->bytes_per_frame) - 1;\n if (av_new_packet(pkt, qt->bytes_per_frame))\n return AVERROR(ENOMEM);\n memcpy(pkt->data, buf + avio_tell(&pb), qt->bytes_per_frame);\n pkt->flags = keyframe ? AV_PKT_FLAG_KEY : 0;\n pkt->stream_index = st->index;\n if (qt->remaining > 0) {\n av_freep(&qt->pkt.data);\n qt->pkt.data = av_realloc(NULL, qt->remaining * qt->bytes_per_frame);\n if (!qt->pkt.data) {\n av_packet_unref(pkt);\n return AVERROR(ENOMEM);\n }\n qt->pkt.size = qt->remaining * qt->bytes_per_frame;\n memcpy(qt->pkt.data,\n buf + avio_tell(&pb) + qt->bytes_per_frame,\n qt->remaining * qt->bytes_per_frame);\n qt->pkt.flags = pkt->flags;\n return 1;\n }\n return 0;\n default:\n avpriv_request_sample(NULL, "RTP-X-QT with packing scheme 2");\n return AVERROR_PATCHWELCOME;\n }\n}', 'static inline uint32_t bitstream_read(BitstreamContext *bc, unsigned n)\n{\n if (!n)\n return 0;\n if (n > bc->bits_left) {\n refill_32(bc);\n if (bc->bits_left < 32)\n bc->bits_left = n;\n }\n return get_val(bc, n);\n}', 'static inline unsigned bitstream_read_bit(BitstreamContext *bc)\n{\n if (!bc->bits_left)\n refill_64(bc);\n return get_val(bc, 1);\n}', 'static inline uint64_t get_val(BitstreamContext *bc, unsigned n)\n{\n#ifdef BITSTREAM_READER_LE\n uint64_t ret = bc->bits & ((UINT64_C(1) << n) - 1);\n bc->bits >>= n;\n#else\n uint64_t ret = bc->bits >> (64 - n);\n bc->bits <<= n;\n#endif\n bc->bits_left -= n;\n return ret;\n}', 'static inline void bitstream_skip(BitstreamContext *bc, unsigned n)\n{\n if (n <= bc->bits_left)\n skip_remaining(bc, n);\n else {\n n -= bc->bits_left;\n skip_remaining(bc, bc->bits_left);\n if (n >= 64) {\n unsigned skip = n / 8;\n n -= skip * 8;\n bc->ptr += skip;\n }\n refill_64(bc);\n if (n)\n skip_remaining(bc, n);\n }\n}', 'static inline void skip_remaining(BitstreamContext *bc, unsigned n)\n{\n#ifdef BITSTREAM_READER_LE\n bc->bits >>= n;\n#else\n bc->bits <<= n;\n#endif\n bc->bits_left -= n;\n}']
858
0
https://github.com/libav/libav/blob/cb4cb7b0ea12b791dde587b1acd504dbb4ec8f41/libavcodec/hqx.c/#L91
static inline void idct_col(int16_t *blk, const uint8_t *quant) { int t0, t1, t2, t3, t4, t5, t6, t7, t8, t9, tA, tB, tC, tD, tE, tF; int t10, t11, t12, t13; int s0, s1, s2, s3, s4, s5, s6, s7; s0 = (int) blk[0 * 8] * quant[0 * 8]; s1 = (int) blk[1 * 8] * quant[1 * 8]; s2 = (int) blk[2 * 8] * quant[2 * 8]; s3 = (int) blk[3 * 8] * quant[3 * 8]; s4 = (int) blk[4 * 8] * quant[4 * 8]; s5 = (int) blk[5 * 8] * quant[5 * 8]; s6 = (int) blk[6 * 8] * quant[6 * 8]; s7 = (int) blk[7 * 8] * quant[7 * 8]; t0 = (s3 * 19266 + s5 * 12873) >> 15; t1 = (s5 * 19266 - s3 * 12873) >> 15; t2 = ((s7 * 4520 + s1 * 22725) >> 15) - t0; t3 = ((s1 * 4520 - s7 * 22725) >> 15) - t1; t4 = t0 * 2 + t2; t5 = t1 * 2 + t3; t6 = t2 - t3; t7 = t3 * 2 + t6; t8 = (t6 * 11585) >> 14; t9 = (t7 * 11585) >> 14; tA = (s2 * 8867 - s6 * 21407) >> 14; tB = (s6 * 8867 + s2 * 21407) >> 14; tC = (s0 >> 1) - (s4 >> 1); tD = (s4 >> 1) * 2 + tC; tE = tC - (tA >> 1); tF = tD - (tB >> 1); t10 = tF - t5; t11 = tE - t8; t12 = tE + (tA >> 1) * 2 - t9; t13 = tF + (tB >> 1) * 2 - t4; blk[0 * 8] = t13 + t4 * 2; blk[1 * 8] = t12 + t9 * 2; blk[2 * 8] = t11 + t8 * 2; blk[3 * 8] = t10 + t5 * 2; blk[4 * 8] = t10; blk[5 * 8] = t11; blk[6 * 8] = t12; blk[7 * 8] = t13; }
['static int hqx_decode_422(HQXContext *ctx, AVFrame *pic,\n GetBitContext *gb, int x, int y)\n{\n const int *quants;\n int flag;\n int last_dc;\n int i, ret;\n if (ctx->interlaced)\n flag = get_bits1(gb);\n else\n flag = 0;\n quants = hqx_quants[get_bits(gb, 4)];\n for (i = 0; i < 8; i++) {\n int vlc_index = ctx->dcb - 9;\n if (i == 0 || i == 4 || i == 6)\n last_dc = 0;\n ret = decode_block(gb, &ctx->dc_vlc[vlc_index], quants,\n ctx->dcb, ctx->block[i], &last_dc);\n if (ret < 0)\n return ret;\n }\n put_blocks(pic, 0, x, y, flag, ctx->block[0], ctx->block[2], hqx_quant_luma);\n put_blocks(pic, 0, x + 8, y, flag, ctx->block[1], ctx->block[3], hqx_quant_luma);\n put_blocks(pic, 2, x >> 1, y, flag, ctx->block[4], ctx->block[5], hqx_quant_chroma);\n put_blocks(pic, 1, x >> 1, y, flag, ctx->block[6], ctx->block[7], hqx_quant_chroma);\n return 0;\n}', 'static inline void put_blocks(AVFrame *pic, int plane,\n int x, int y, int ilace,\n int16_t *block0, int16_t *block1,\n const uint8_t *quant)\n{\n int fields = ilace ? 2 : 1;\n int lsize = pic->linesize[plane];\n uint8_t *p = pic->data[plane] + x * 2;\n hqx_idct_put((uint16_t *)(p + y * lsize), lsize * fields, block0, quant);\n hqx_idct_put((uint16_t *)(p + (y + (ilace ? 1 : 8)) * lsize),\n lsize * fields, block1, quant);\n}', 'static void hqx_idct_put(uint16_t *dst, ptrdiff_t stride,\n int16_t *block, const uint8_t *quant)\n{\n int i, j;\n hqx_idct(block, quant);\n for (i = 0; i < 8; i++) {\n for (j = 0; j < 8; j++) {\n int v = av_clip(block[j + i * 8] + 0x800, 0, 0x1000);\n dst[j] = (v << 4) | (v >> 8);\n }\n dst += stride >> 1;\n }\n}', 'static void hqx_idct(int16_t *block, const uint8_t *quant)\n{\n int i;\n for (i = 0; i < 8; i++)\n idct_col(block + i, quant + i);\n for (i = 0; i < 8; i++)\n idct_row(block + i * 8);\n}', 'static inline void idct_col(int16_t *blk, const uint8_t *quant)\n{\n int t0, t1, t2, t3, t4, t5, t6, t7, t8, t9, tA, tB, tC, tD, tE, tF;\n int t10, t11, t12, t13;\n int s0, s1, s2, s3, s4, s5, s6, s7;\n s0 = (int) blk[0 * 8] * quant[0 * 8];\n s1 = (int) blk[1 * 8] * quant[1 * 8];\n s2 = (int) blk[2 * 8] * quant[2 * 8];\n s3 = (int) blk[3 * 8] * quant[3 * 8];\n s4 = (int) blk[4 * 8] * quant[4 * 8];\n s5 = (int) blk[5 * 8] * quant[5 * 8];\n s6 = (int) blk[6 * 8] * quant[6 * 8];\n s7 = (int) blk[7 * 8] * quant[7 * 8];\n t0 = (s3 * 19266 + s5 * 12873) >> 15;\n t1 = (s5 * 19266 - s3 * 12873) >> 15;\n t2 = ((s7 * 4520 + s1 * 22725) >> 15) - t0;\n t3 = ((s1 * 4520 - s7 * 22725) >> 15) - t1;\n t4 = t0 * 2 + t2;\n t5 = t1 * 2 + t3;\n t6 = t2 - t3;\n t7 = t3 * 2 + t6;\n t8 = (t6 * 11585) >> 14;\n t9 = (t7 * 11585) >> 14;\n tA = (s2 * 8867 - s6 * 21407) >> 14;\n tB = (s6 * 8867 + s2 * 21407) >> 14;\n tC = (s0 >> 1) - (s4 >> 1);\n tD = (s4 >> 1) * 2 + tC;\n tE = tC - (tA >> 1);\n tF = tD - (tB >> 1);\n t10 = tF - t5;\n t11 = tE - t8;\n t12 = tE + (tA >> 1) * 2 - t9;\n t13 = tF + (tB >> 1) * 2 - t4;\n blk[0 * 8] = t13 + t4 * 2;\n blk[1 * 8] = t12 + t9 * 2;\n blk[2 * 8] = t11 + t8 * 2;\n blk[3 * 8] = t10 + t5 * 2;\n blk[4 * 8] = t10;\n blk[5 * 8] = t11;\n blk[6 * 8] = t12;\n blk[7 * 8] = t13;\n}']
859
0
https://github.com/openssl/openssl/blob/47ddf355b46eae8c846e411f44531e928e04adf5/crypto/des/read_pwd.c/#L339
int des_read_pw(char *buf, char *buff, int size, const char *prompt, int verify) { #ifdef OPENSSL_SYS_VMS struct IOSB iosb; $DESCRIPTOR(terminal,"TT"); long tty_orig[3], tty_new[3]; long status; unsigned short channel = 0; #else #ifndef OPENSSL_SYS_MSDOS TTY_STRUCT tty_orig,tty_new; #endif #endif int number; int ok; static int ps; int is_a_tty; static FILE *tty; char *p; if (setjmp(save)) { ok=0; goto error; } number=5; ok=0; ps=0; is_a_tty=1; tty=NULL; #ifdef OPENSSL_SYS_MSDOS if ((tty=fopen("con","r")) == NULL) tty=stdin; #elif defined(MAC_OS_pre_X) tty=stdin; #else #ifndef OPENSSL_SYS_MPE if ((tty=fopen("/dev/tty","r")) == NULL) #endif tty=stdin; #endif #if defined(TTY_get) && !defined(OPENSSL_SYS_VMS) if (TTY_get(fileno(tty),&tty_orig) == -1) { #ifdef ENOTTY if (errno == ENOTTY) is_a_tty=0; else #endif #ifdef EINVAL if (errno == EINVAL) is_a_tty=0; else #endif return(-1); } memcpy(&(tty_new),&(tty_orig),sizeof(tty_orig)); #endif #ifdef OPENSSL_SYS_VMS status = sys$assign(&terminal,&channel,0,0); if (status != SS$_NORMAL) return(-1); status=sys$qiow(0,channel,IO$_SENSEMODE,&iosb,0,0,tty_orig,12,0,0,0,0); if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL)) return(-1); #endif pushsig(); ps=1; #ifdef TTY_FLAGS tty_new.TTY_FLAGS &= ~ECHO; #endif #if defined(TTY_set) && !defined(OPENSSL_SYS_VMS) if (is_a_tty && (TTY_set(fileno(tty),&tty_new) == -1)) #ifdef OPENSSL_SYS_MPE ; #else return(-1); #endif #endif #ifdef OPENSSL_SYS_VMS tty_new[0] = tty_orig[0]; tty_new[1] = tty_orig[1] | TT$M_NOECHO; tty_new[2] = tty_orig[2]; status = sys$qiow(0,channel,IO$_SETMODE,&iosb,0,0,tty_new,12,0,0,0,0); if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL)) return(-1); #endif ps=2; while ((!ok) && (number--)) { fputs(prompt,stderr); fflush(stderr); buf[0]='\0'; fgets(buf,size,tty); if (feof(tty)) goto error; if (ferror(tty)) goto error; if ((p=(char *)strchr(buf,'\n')) != NULL) *p='\0'; else read_till_nl(tty); if (verify) { fprintf(stderr,"\nVerifying password - %s",prompt); fflush(stderr); buff[0]='\0'; fgets(buff,size,tty); if (feof(tty)) goto error; if ((p=(char *)strchr(buff,'\n')) != NULL) *p='\0'; else read_till_nl(tty); if (strcmp(buf,buff) != 0) { fprintf(stderr,"\nVerify failure"); fflush(stderr); break; } } ok=1; } error: fprintf(stderr,"\n"); #ifdef DEBUG perror("fgets(tty)"); #endif #if defined(TTY_set) && !defined(OPENSSL_SYS_VMS) if (ps >= 2) TTY_set(fileno(tty),&tty_orig); #endif #ifdef OPENSSL_SYS_VMS if (ps >= 2) status = sys$qiow(0,channel,IO$_SETMODE,&iosb,0,0 ,tty_orig,12,0,0,0,0); #endif if (ps >= 1) popsig(); if (stdin != tty) fclose(tty); #ifdef OPENSSL_SYS_VMS status = sys$dassgn(channel); #endif return(!ok); }
['int MAIN(int argc, char **argv)\n{\n ENGINE *e = NULL;\n char *infile=NULL, *outfile=NULL, *keyname = NULL;\n char *certfile=NULL;\n BIO *in=NULL, *out = NULL, *inkey = NULL, *certsin = NULL;\n char **args;\n char *name = NULL;\n PKCS12 *p12 = NULL;\n char pass[50], macpass[50];\n int export_cert = 0;\n int options = 0;\n int chain = 0;\n int badarg = 0;\n int iter = PKCS12_DEFAULT_ITER;\n int maciter = PKCS12_DEFAULT_ITER;\n int twopass = 0;\n int keytype = 0;\n int cert_pbe = NID_pbe_WithSHA1And40BitRC2_CBC;\n int key_pbe = NID_pbe_WithSHA1And3_Key_TripleDES_CBC;\n int ret = 1;\n int macver = 1;\n int noprompt = 0;\n STACK *canames = NULL;\n char *cpass = NULL, *mpass = NULL;\n char *passargin = NULL, *passargout = NULL, *passarg = NULL;\n char *passin = NULL, *passout = NULL;\n char *inrand = NULL;\n char *CApath = NULL, *CAfile = NULL;\n char *engine=NULL;\n apps_startup();\n enc = EVP_des_ede3_cbc();\n if (bio_err == NULL ) bio_err = BIO_new_fp (stderr, BIO_NOCLOSE);\n args = argv + 1;\n while (*args) {\n\tif (*args[0] == \'-\') {\n\t\tif (!strcmp (*args, "-nokeys")) options |= NOKEYS;\n\t\telse if (!strcmp (*args, "-keyex")) keytype = KEY_EX;\n\t\telse if (!strcmp (*args, "-keysig")) keytype = KEY_SIG;\n\t\telse if (!strcmp (*args, "-nocerts")) options |= NOCERTS;\n\t\telse if (!strcmp (*args, "-clcerts")) options |= CLCERTS;\n\t\telse if (!strcmp (*args, "-cacerts")) options |= CACERTS;\n\t\telse if (!strcmp (*args, "-noout")) options |= (NOKEYS|NOCERTS);\n\t\telse if (!strcmp (*args, "-info")) options |= INFO;\n\t\telse if (!strcmp (*args, "-chain")) chain = 1;\n\t\telse if (!strcmp (*args, "-twopass")) twopass = 1;\n\t\telse if (!strcmp (*args, "-nomacver")) macver = 0;\n\t\telse if (!strcmp (*args, "-descert"))\n \t\t\tcert_pbe = NID_pbe_WithSHA1And3_Key_TripleDES_CBC;\n\t\telse if (!strcmp (*args, "-export")) export_cert = 1;\n\t\telse if (!strcmp (*args, "-des")) enc=EVP_des_cbc();\n#ifndef OPENSSL_NO_IDEA\n\t\telse if (!strcmp (*args, "-idea")) enc=EVP_idea_cbc();\n#endif\n\t\telse if (!strcmp (*args, "-des3")) enc = EVP_des_ede3_cbc();\n\t\telse if (!strcmp (*args, "-noiter")) iter = 1;\n\t\telse if (!strcmp (*args, "-maciter"))\n\t\t\t\t\t maciter = PKCS12_DEFAULT_ITER;\n\t\telse if (!strcmp (*args, "-nomaciter"))\n\t\t\t\t\t maciter = 1;\n\t\telse if (!strcmp (*args, "-nodes")) enc=NULL;\n\t\telse if (!strcmp (*args, "-certpbe")) {\n\t\t\tif (args[1]) {\n\t\t\t\targs++;\n\t\t\t\tcert_pbe=OBJ_txt2nid(*args);\n\t\t\t\tif(cert_pbe == NID_undef) {\n\t\t\t\t\tBIO_printf(bio_err,\n\t\t\t\t\t\t "Unknown PBE algorithm %s\\n", *args);\n\t\t\t\t\tbadarg = 1;\n\t\t\t\t}\n\t\t\t} else badarg = 1;\n\t\t} else if (!strcmp (*args, "-keypbe")) {\n\t\t\tif (args[1]) {\n\t\t\t\targs++;\n\t\t\t\tkey_pbe=OBJ_txt2nid(*args);\n\t\t\t\tif(key_pbe == NID_undef) {\n\t\t\t\t\tBIO_printf(bio_err,\n\t\t\t\t\t\t "Unknown PBE algorithm %s\\n", *args);\n\t\t\t\t\tbadarg = 1;\n\t\t\t\t}\n\t\t\t} else badarg = 1;\n\t\t} else if (!strcmp (*args, "-rand")) {\n\t\t if (args[1]) {\n\t\t\targs++;\n\t\t\tinrand = *args;\n\t\t } else badarg = 1;\n\t\t} else if (!strcmp (*args, "-inkey")) {\n\t\t if (args[1]) {\n\t\t\targs++;\n\t\t\tkeyname = *args;\n\t\t } else badarg = 1;\n\t\t} else if (!strcmp (*args, "-certfile")) {\n\t\t if (args[1]) {\n\t\t\targs++;\n\t\t\tcertfile = *args;\n\t\t } else badarg = 1;\n\t\t} else if (!strcmp (*args, "-name")) {\n\t\t if (args[1]) {\n\t\t\targs++;\n\t\t\tname = *args;\n\t\t } else badarg = 1;\n\t\t} else if (!strcmp (*args, "-caname")) {\n\t\t if (args[1]) {\n\t\t\targs++;\n\t\t\tif (!canames) canames = sk_new_null();\n\t\t\tsk_push(canames, *args);\n\t\t } else badarg = 1;\n\t\t} else if (!strcmp (*args, "-in")) {\n\t\t if (args[1]) {\n\t\t\targs++;\n\t\t\tinfile = *args;\n\t\t } else badarg = 1;\n\t\t} else if (!strcmp (*args, "-out")) {\n\t\t if (args[1]) {\n\t\t\targs++;\n\t\t\toutfile = *args;\n\t\t } else badarg = 1;\n\t\t} else if (!strcmp(*args,"-passin")) {\n\t\t if (args[1]) {\n\t\t\targs++;\n\t\t\tpassargin = *args;\n\t\t } else badarg = 1;\n\t\t} else if (!strcmp(*args,"-passout")) {\n\t\t if (args[1]) {\n\t\t\targs++;\n\t\t\tpassargout = *args;\n\t\t } else badarg = 1;\n\t\t} else if (!strcmp (*args, "-password")) {\n\t\t if (args[1]) {\n\t\t\targs++;\n\t\t\tpassarg = *args;\n\t\t \tnoprompt = 1;\n\t\t } else badarg = 1;\n\t\t} else if (!strcmp(*args,"-CApath")) {\n\t\t if (args[1]) {\n\t\t\targs++;\n\t\t\tCApath = *args;\n\t\t } else badarg = 1;\n\t\t} else if (!strcmp(*args,"-CAfile")) {\n\t\t if (args[1]) {\n\t\t\targs++;\n\t\t\tCAfile = *args;\n\t\t } else badarg = 1;\n\t\t} else if (!strcmp(*args,"-engine")) {\n\t\t if (args[1]) {\n\t\t\targs++;\n\t\t\tengine = *args;\n\t\t } else badarg = 1;\n\t\t} else badarg = 1;\n\t} else badarg = 1;\n\targs++;\n }\n if (badarg) {\n\tBIO_printf (bio_err, "Usage: pkcs12 [options]\\n");\n\tBIO_printf (bio_err, "where options are\\n");\n\tBIO_printf (bio_err, "-export output PKCS12 file\\n");\n\tBIO_printf (bio_err, "-chain add certificate chain\\n");\n\tBIO_printf (bio_err, "-inkey file private key if not infile\\n");\n\tBIO_printf (bio_err, "-certfile f add all certs in f\\n");\n\tBIO_printf (bio_err, "-CApath arg - PEM format directory of CA\'s\\n");\n\tBIO_printf (bio_err, "-CAfile arg - PEM format file of CA\'s\\n");\n\tBIO_printf (bio_err, "-name \\"name\\" use name as friendly name\\n");\n\tBIO_printf (bio_err, "-caname \\"nm\\" use nm as CA friendly name (can be used more than once).\\n");\n\tBIO_printf (bio_err, "-in infile input filename\\n");\n\tBIO_printf (bio_err, "-out outfile output filename\\n");\n\tBIO_printf (bio_err, "-noout don\'t output anything, just verify.\\n");\n\tBIO_printf (bio_err, "-nomacver don\'t verify MAC.\\n");\n\tBIO_printf (bio_err, "-nocerts don\'t output certificates.\\n");\n\tBIO_printf (bio_err, "-clcerts only output client certificates.\\n");\n\tBIO_printf (bio_err, "-cacerts only output CA certificates.\\n");\n\tBIO_printf (bio_err, "-nokeys don\'t output private keys.\\n");\n\tBIO_printf (bio_err, "-info give info about PKCS#12 structure.\\n");\n\tBIO_printf (bio_err, "-des encrypt private keys with DES\\n");\n\tBIO_printf (bio_err, "-des3 encrypt private keys with triple DES (default)\\n");\n#ifndef OPENSSL_NO_IDEA\n\tBIO_printf (bio_err, "-idea encrypt private keys with idea\\n");\n#endif\n\tBIO_printf (bio_err, "-nodes don\'t encrypt private keys\\n");\n\tBIO_printf (bio_err, "-noiter don\'t use encryption iteration\\n");\n\tBIO_printf (bio_err, "-maciter use MAC iteration\\n");\n\tBIO_printf (bio_err, "-twopass separate MAC, encryption passwords\\n");\n\tBIO_printf (bio_err, "-descert encrypt PKCS#12 certificates with triple DES (default RC2-40)\\n");\n\tBIO_printf (bio_err, "-certpbe alg specify certificate PBE algorithm (default RC2-40)\\n");\n\tBIO_printf (bio_err, "-keypbe alg specify private key PBE algorithm (default 3DES)\\n");\n\tBIO_printf (bio_err, "-keyex set MS key exchange type\\n");\n\tBIO_printf (bio_err, "-keysig set MS key signature type\\n");\n\tBIO_printf (bio_err, "-password p set import/export password source\\n");\n\tBIO_printf (bio_err, "-passin p input file pass phrase source\\n");\n\tBIO_printf (bio_err, "-passout p output file pass phrase source\\n");\n\tBIO_printf (bio_err, "-engine e use engine e, possibly a hardware device.\\n");\n\tBIO_printf(bio_err, "-rand file%cfile%c...\\n", LIST_SEPARATOR_CHAR, LIST_SEPARATOR_CHAR);\n\tBIO_printf(bio_err, " load the file (or the files in the directory) into\\n");\n\tBIO_printf(bio_err, " the random number generator\\n");\n \tgoto end;\n }\n if (engine != NULL) {\n\tif((e = ENGINE_by_id(engine)) == NULL) {\n\t BIO_printf(bio_err,"invalid engine \\"%s\\"\\n", engine);\n\t goto end;\n\t}\n\tif(!ENGINE_set_default(e, ENGINE_METHOD_ALL)) {\n\t BIO_printf(bio_err,"can\'t use that engine\\n");\n\t goto end;\n\t}\n\tBIO_printf(bio_err,"engine \\"%s\\" set.\\n", engine);\n\tENGINE_free(e);\n }\n if(passarg) {\n\tif(export_cert) passargout = passarg;\n\telse passargin = passarg;\n }\n if(!app_passwd(bio_err, passargin, passargout, &passin, &passout)) {\n\tBIO_printf(bio_err, "Error getting passwords\\n");\n\tgoto end;\n }\n if(!cpass) {\n \tif(export_cert) cpass = passout;\n \telse cpass = passin;\n }\n if(cpass) {\n\tmpass = cpass;\n\tnoprompt = 1;\n } else {\n\tcpass = pass;\n\tmpass = macpass;\n }\n if(export_cert || inrand) {\n \tapp_RAND_load_file(NULL, bio_err, (inrand != NULL));\n if (inrand != NULL)\n\t\tBIO_printf(bio_err,"%ld semi-random bytes loaded\\n",\n\t\t\tapp_RAND_load_files(inrand));\n }\n ERR_load_crypto_strings();\n#ifdef CRYPTO_MDEBUG\n CRYPTO_push_info("read files");\n#endif\n if (!infile) in = BIO_new_fp(stdin, BIO_NOCLOSE);\n else in = BIO_new_file(infile, "rb");\n if (!in) {\n\t BIO_printf(bio_err, "Error opening input file %s\\n",\n\t\t\t\t\t\tinfile ? infile : "<stdin>");\n\t perror (infile);\n\t goto end;\n }\n if (certfile) {\n \tif(!(certsin = BIO_new_file(certfile, "r"))) {\n\t BIO_printf(bio_err, "Can\'t open certificate file %s\\n", certfile);\n\t perror (certfile);\n\t goto end;\n\t}\n }\n if (keyname) {\n \tif(!(inkey = BIO_new_file(keyname, "r"))) {\n\t BIO_printf(bio_err, "Can\'t key certificate file %s\\n", keyname);\n\t perror (keyname);\n\t goto end;\n\t}\n }\n#ifdef CRYPTO_MDEBUG\n CRYPTO_pop_info();\n CRYPTO_push_info("write files");\n#endif\n if (!outfile) {\n\tout = BIO_new_fp(stdout, BIO_NOCLOSE);\n#ifdef OPENSSL_SYS_VMS\n\t{\n\t BIO *tmpbio = BIO_new(BIO_f_linebuffer());\n\t out = BIO_push(tmpbio, out);\n\t}\n#endif\n } else out = BIO_new_file(outfile, "wb");\n if (!out) {\n\tBIO_printf(bio_err, "Error opening output file %s\\n",\n\t\t\t\t\t\toutfile ? outfile : "<stdout>");\n\tperror (outfile);\n\tgoto end;\n }\n if (twopass) {\n#ifdef CRYPTO_MDEBUG\n CRYPTO_push_info("read MAC password");\n#endif\n\tif(EVP_read_pw_string (macpass, 50, "Enter MAC Password:", export_cert))\n\t{\n \t BIO_printf (bio_err, "Can\'t read Password\\n");\n \t goto end;\n \t}\n#ifdef CRYPTO_MDEBUG\n CRYPTO_pop_info();\n#endif\n }\n if (export_cert) {\n\tEVP_PKEY *key = NULL;\n\tSTACK_OF(PKCS12_SAFEBAG) *bags = NULL;\n\tSTACK_OF(PKCS7) *safes = NULL;\n\tPKCS12_SAFEBAG *bag = NULL;\n\tPKCS8_PRIV_KEY_INFO *p8 = NULL;\n\tPKCS7 *authsafe = NULL;\n\tX509 *ucert = NULL;\n\tSTACK_OF(X509) *certs=NULL;\n\tchar *catmp = NULL;\n\tint i;\n\tunsigned char keyid[EVP_MAX_MD_SIZE];\n\tunsigned int keyidlen = 0;\n#ifdef CRYPTO_MDEBUG\n\tCRYPTO_push_info("process -export_cert");\n\tCRYPTO_push_info("reading private key");\n#endif\n\tkey = PEM_read_bio_PrivateKey(inkey ? inkey : in, NULL, NULL, passin);\n\tif (!inkey) (void) BIO_reset(in);\n\telse BIO_free(inkey);\n\tif (!key) {\n\t\tBIO_printf (bio_err, "Error loading private key\\n");\n\t\tERR_print_errors(bio_err);\n\t\tgoto export_end;\n\t}\n#ifdef CRYPTO_MDEBUG\n\tCRYPTO_pop_info();\n\tCRYPTO_push_info("reading certs from input");\n#endif\n\tcerts = sk_X509_new_null();\n\tif(!cert_load(in, certs)) {\n\t\tBIO_printf(bio_err, "Error loading certificates from input\\n");\n\t\tERR_print_errors(bio_err);\n\t\tgoto export_end;\n\t}\n#ifdef CRYPTO_MDEBUG\n\tCRYPTO_pop_info();\n\tCRYPTO_push_info("reading certs from input 2");\n#endif\n\tfor(i = 0; i < sk_X509_num(certs); i++) {\n\t\tucert = sk_X509_value(certs, i);\n\t\tif(X509_check_private_key(ucert, key)) {\n\t\t\tX509_digest(ucert, EVP_sha1(), keyid, &keyidlen);\n\t\t\tbreak;\n\t\t}\n\t}\n\tif(!keyidlen) {\n\t\tucert = NULL;\n\t\tBIO_printf(bio_err, "No certificate matches private key\\n");\n\t\tgoto export_end;\n\t}\n#ifdef CRYPTO_MDEBUG\n\tCRYPTO_pop_info();\n\tCRYPTO_push_info("reading certs from certfile");\n#endif\n\tbags = sk_PKCS12_SAFEBAG_new_null ();\n\tif (certsin) {\n\t\tif(!cert_load(certsin, certs)) {\n\t\t\tBIO_printf(bio_err, "Error loading certificates from certfile\\n");\n\t\t\tERR_print_errors(bio_err);\n\t\t\tgoto export_end;\n\t\t}\n\t \tBIO_free(certsin);\n \t}\n#ifdef CRYPTO_MDEBUG\n\tCRYPTO_pop_info();\n\tCRYPTO_push_info("building chain");\n#endif\n\tif (chain) {\n \tint vret;\n\t\tSTACK_OF(X509) *chain2;\n\t\tX509_STORE *store = X509_STORE_new();\n\t\tif (!store)\n\t\t\t{\n\t\t\tBIO_printf (bio_err, "Memory allocation error\\n");\n\t\t\tgoto export_end;\n\t\t\t}\n\t\tif (!X509_STORE_load_locations(store, CAfile, CApath))\n\t\t\tX509_STORE_set_default_paths (store);\n\t\tvret = get_cert_chain (ucert, store, &chain2);\n\t\tX509_STORE_free(store);\n\t\tif (!vret) {\n\t\t for (i = 1; i < sk_X509_num (chain2) ; i++)\n\t\t\tsk_X509_push(certs, sk_X509_value (chain2, i));\n\t\t}\n\t\tsk_X509_free(chain2);\n\t\tif (vret) {\n\t\t\tBIO_printf (bio_err, "Error %s getting chain.\\n",\n\t\t\t\t\tX509_verify_cert_error_string(vret));\n\t\t\tgoto export_end;\n\t\t}\n \t}\n#ifdef CRYPTO_MDEBUG\n\tCRYPTO_pop_info();\n\tCRYPTO_push_info("building bags");\n#endif\n\tfor(i = 0; i < sk_X509_num(certs); i++) {\n\t\tX509 *cert = NULL;\n\t\tcert = sk_X509_value(certs, i);\n\t\tbag = PKCS12_x5092certbag(cert);\n\t\tif(cert == ucert) {\n\t\t\tif(name) PKCS12_add_friendlyname(bag, name, -1);\n\t\t\tPKCS12_add_localkeyid(bag, keyid, keyidlen);\n\t\t} else if((catmp = sk_shift(canames)))\n\t\t\t\tPKCS12_add_friendlyname(bag, catmp, -1);\n\t\tsk_PKCS12_SAFEBAG_push(bags, bag);\n\t}\n\tsk_X509_pop_free(certs, X509_free);\n\tcerts = NULL;\n\tucert = NULL;\n#ifdef CRYPTO_MDEBUG\n\tCRYPTO_pop_info();\n\tCRYPTO_push_info("encrypting bags");\n#endif\n\tif(!noprompt &&\n\t\tEVP_read_pw_string(pass, 50, "Enter Export Password:", 1)) {\n\t BIO_printf (bio_err, "Can\'t read Password\\n");\n\t goto export_end;\n }\n\tif (!twopass) strcpy(macpass, pass);\n\tauthsafe = PKCS12_pack_p7encdata(cert_pbe, cpass, -1, NULL, 0,\n\t\t\t\t\t\t\t\t iter, bags);\n\tsk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);\n\tbags = NULL;\n\tif (!authsafe) {\n\t\tERR_print_errors (bio_err);\n\t\tgoto export_end;\n\t}\n\tsafes = sk_PKCS7_new_null ();\n\tsk_PKCS7_push (safes, authsafe);\n#ifdef CRYPTO_MDEBUG\n\tCRYPTO_pop_info();\n\tCRYPTO_push_info("building shrouded key bag");\n#endif\n\tp8 = EVP_PKEY2PKCS8 (key);\n\tif(keytype) PKCS8_add_keyusage(p8, keytype);\n\tbag = PKCS12_MAKE_SHKEYBAG(key_pbe, cpass, -1, NULL, 0, iter, p8);\n\tPKCS8_PRIV_KEY_INFO_free(p8);\n\tp8 = NULL;\n if (name) PKCS12_add_friendlyname (bag, name, -1);\n\tPKCS12_add_localkeyid (bag, keyid, keyidlen);\n\tbags = sk_PKCS12_SAFEBAG_new_null();\n\tsk_PKCS12_SAFEBAG_push (bags, bag);\n#ifdef CRYPTO_MDEBUG\n\tCRYPTO_pop_info();\n\tCRYPTO_push_info("encrypting shrouded key bag");\n#endif\n\tauthsafe = PKCS12_pack_p7data (bags);\n\tsk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);\n\tbags = NULL;\n\tsk_PKCS7_push (safes, authsafe);\n#ifdef CRYPTO_MDEBUG\n\tCRYPTO_pop_info();\n\tCRYPTO_push_info("building pkcs12");\n#endif\n\tp12 = PKCS12_init(NID_pkcs7_data);\n\tPKCS12_pack_authsafes(p12, safes);\n\tsk_PKCS7_pop_free(safes, PKCS7_free);\n\tsafes = NULL;\n\tPKCS12_set_mac (p12, mpass, -1, NULL, 0, maciter, NULL);\n#ifdef CRYPTO_MDEBUG\n\tCRYPTO_pop_info();\n\tCRYPTO_push_info("writing pkcs12");\n#endif\n\ti2d_PKCS12_bio (out, p12);\n\tret = 0;\n export_end:\n#ifdef CRYPTO_MDEBUG\n\tCRYPTO_pop_info();\n\tCRYPTO_pop_info();\n\tCRYPTO_push_info("process -export_cert: freeing");\n#endif\n\tif (key) EVP_PKEY_free(key);\n\tif (certs) sk_X509_pop_free(certs, X509_free);\n\tif (safes) sk_PKCS7_pop_free(safes, PKCS7_free);\n\tif (bags) sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);\n\tif (ucert) X509_free(ucert);\n#ifdef CRYPTO_MDEBUG\n\tCRYPTO_pop_info();\n#endif\n\tgoto end;\n }\n if (!(p12 = d2i_PKCS12_bio (in, NULL))) {\n\tERR_print_errors(bio_err);\n\tgoto end;\n }\n#ifdef CRYPTO_MDEBUG\n CRYPTO_push_info("read import password");\n#endif\n if(!noprompt && EVP_read_pw_string(pass, 50, "Enter Import Password:", 0)) {\n\tBIO_printf (bio_err, "Can\'t read Password\\n");\n\tgoto end;\n }\n#ifdef CRYPTO_MDEBUG\n CRYPTO_pop_info();\n#endif\n if (!twopass) strcpy(macpass, pass);\n if (options & INFO) BIO_printf (bio_err, "MAC Iteration %ld\\n", p12->mac->iter ? ASN1_INTEGER_get (p12->mac->iter) : 1);\n if(macver) {\n#ifdef CRYPTO_MDEBUG\n CRYPTO_push_info("verify MAC");\n#endif\n\tif(!macpass[0] && PKCS12_verify_mac(p12, NULL, 0)) {\n\t\tif(!twopass) cpass = NULL;\n\t} else if (!PKCS12_verify_mac(p12, mpass, -1)) {\n\t BIO_printf (bio_err, "Mac verify error: invalid password?\\n");\n\t ERR_print_errors (bio_err);\n\t goto end;\n\t}\n\tBIO_printf (bio_err, "MAC verified OK\\n");\n#ifdef CRYPTO_MDEBUG\n CRYPTO_pop_info();\n#endif\n }\n#ifdef CRYPTO_MDEBUG\n CRYPTO_push_info("output keys and certificates");\n#endif\n if (!dump_certs_keys_p12 (out, p12, cpass, -1, options, passout)) {\n\tBIO_printf(bio_err, "Error outputting keys and certificates\\n");\n\tERR_print_errors (bio_err);\n\tgoto end;\n }\n#ifdef CRYPTO_MDEBUG\n CRYPTO_pop_info();\n#endif\n ret = 0;\n end:\n if (p12) PKCS12_free(p12);\n if(export_cert || inrand) app_RAND_write_file(NULL, bio_err);\n#ifdef CRYPTO_MDEBUG\n CRYPTO_remove_all_info();\n#endif\n BIO_free(in);\n BIO_free_all(out);\n if (canames) sk_free(canames);\n if(passin) OPENSSL_free(passin);\n if(passout) OPENSSL_free(passout);\n EXIT(ret);\n}', "int EVP_read_pw_string(char *buf, int len, const char *prompt, int verify)\n\t{\n#ifndef OPENSSL_NO_DES\n\tif ((prompt == NULL) && (prompt_string[0] != '\\0'))\n\t\tprompt=prompt_string;\n\treturn(des_read_pw_string(buf,len,prompt,verify));\n#else\n\treturn -1;\n#endif\n\t}", 'int des_read_pw_string(char *buf, int length, const char *prompt,\n\t int verify)\n\t{\n\tchar buff[BUFSIZ];\n\tint ret;\n\tret=des_read_pw(buf,buff,(length>BUFSIZ)?BUFSIZ:length,prompt,verify);\n\tmemset(buff,0,BUFSIZ);\n\treturn(ret);\n\t}', 'int des_read_pw(char *buf, char *buff, int size, const char *prompt,\n\t int verify)\n\t{\n#ifdef OPENSSL_SYS_VMS\n\tstruct IOSB iosb;\n\t$DESCRIPTOR(terminal,"TT");\n\tlong tty_orig[3], tty_new[3];\n\tlong status;\n\tunsigned short channel = 0;\n#else\n#ifndef OPENSSL_SYS_MSDOS\n\tTTY_STRUCT tty_orig,tty_new;\n#endif\n#endif\n\tint number;\n\tint ok;\n\tstatic int ps;\n\tint is_a_tty;\n\tstatic FILE *tty;\n\tchar *p;\n\tif (setjmp(save))\n\t\t{\n\t\tok=0;\n\t\tgoto error;\n\t\t}\n\tnumber=5;\n\tok=0;\n\tps=0;\n\tis_a_tty=1;\n\ttty=NULL;\n#ifdef OPENSSL_SYS_MSDOS\n\tif ((tty=fopen("con","r")) == NULL)\n\t\ttty=stdin;\n#elif defined(MAC_OS_pre_X)\n\ttty=stdin;\n#else\n#ifndef OPENSSL_SYS_MPE\n\tif ((tty=fopen("/dev/tty","r")) == NULL)\n#endif\n\t\ttty=stdin;\n#endif\n#if defined(TTY_get) && !defined(OPENSSL_SYS_VMS)\n\tif (TTY_get(fileno(tty),&tty_orig) == -1)\n\t\t{\n#ifdef ENOTTY\n\t\tif (errno == ENOTTY)\n\t\t\tis_a_tty=0;\n\t\telse\n#endif\n#ifdef EINVAL\n\t\tif (errno == EINVAL)\n\t\t\tis_a_tty=0;\n\t\telse\n#endif\n\t\t\treturn(-1);\n\t\t}\n\tmemcpy(&(tty_new),&(tty_orig),sizeof(tty_orig));\n#endif\n#ifdef OPENSSL_SYS_VMS\n\tstatus = sys$assign(&terminal,&channel,0,0);\n\tif (status != SS$_NORMAL)\n\t\treturn(-1);\n\tstatus=sys$qiow(0,channel,IO$_SENSEMODE,&iosb,0,0,tty_orig,12,0,0,0,0);\n\tif ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL))\n\t\treturn(-1);\n#endif\n\tpushsig();\n\tps=1;\n#ifdef TTY_FLAGS\n\ttty_new.TTY_FLAGS &= ~ECHO;\n#endif\n#if defined(TTY_set) && !defined(OPENSSL_SYS_VMS)\n\tif (is_a_tty && (TTY_set(fileno(tty),&tty_new) == -1))\n#ifdef OPENSSL_SYS_MPE\n\t\t;\n#else\n\t\treturn(-1);\n#endif\n#endif\n#ifdef OPENSSL_SYS_VMS\n\ttty_new[0] = tty_orig[0];\n\ttty_new[1] = tty_orig[1] | TT$M_NOECHO;\n\ttty_new[2] = tty_orig[2];\n\tstatus = sys$qiow(0,channel,IO$_SETMODE,&iosb,0,0,tty_new,12,0,0,0,0);\n\tif ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL))\n\t\treturn(-1);\n#endif\n\tps=2;\n\twhile ((!ok) && (number--))\n\t\t{\n\t\tfputs(prompt,stderr);\n\t\tfflush(stderr);\n\t\tbuf[0]=\'\\0\';\n\t\tfgets(buf,size,tty);\n\t\tif (feof(tty)) goto error;\n\t\tif (ferror(tty)) goto error;\n\t\tif ((p=(char *)strchr(buf,\'\\n\')) != NULL)\n\t\t\t*p=\'\\0\';\n\t\telse\tread_till_nl(tty);\n\t\tif (verify)\n\t\t\t{\n\t\t\tfprintf(stderr,"\\nVerifying password - %s",prompt);\n\t\t\tfflush(stderr);\n\t\t\tbuff[0]=\'\\0\';\n\t\t\tfgets(buff,size,tty);\n\t\t\tif (feof(tty)) goto error;\n\t\t\tif ((p=(char *)strchr(buff,\'\\n\')) != NULL)\n\t\t\t\t*p=\'\\0\';\n\t\t\telse\tread_till_nl(tty);\n\t\t\tif (strcmp(buf,buff) != 0)\n\t\t\t\t{\n\t\t\t\tfprintf(stderr,"\\nVerify failure");\n\t\t\t\tfflush(stderr);\n\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\tok=1;\n\t\t}\nerror:\n\tfprintf(stderr,"\\n");\n#ifdef DEBUG\n\tperror("fgets(tty)");\n#endif\n#if defined(TTY_set) && !defined(OPENSSL_SYS_VMS)\n\tif (ps >= 2) TTY_set(fileno(tty),&tty_orig);\n#endif\n#ifdef OPENSSL_SYS_VMS\n\tif (ps >= 2)\n\t\tstatus = sys$qiow(0,channel,IO$_SETMODE,&iosb,0,0\n\t\t\t,tty_orig,12,0,0,0,0);\n#endif\n\tif (ps >= 1) popsig();\n\tif (stdin != tty) fclose(tty);\n#ifdef OPENSSL_SYS_VMS\n\tstatus = sys$dassgn(channel);\n#endif\n\treturn(!ok);\n\t}']
860
0
https://github.com/libav/libav/blob/ddf5fb71ee9c8b2d9a23c0f661a84896cd7050fc/libavcodec/dv.c/#L142
static inline void dv_calc_mb_coordinates(const AVDVProfile *d, int chan, int seq, int slot, uint16_t *tbl) { static const uint8_t off[] = { 2, 6, 8, 0, 4 }; static const uint8_t shuf1[] = { 36, 18, 54, 0, 72 }; static const uint8_t shuf2[] = { 24, 12, 36, 0, 48 }; static const uint8_t shuf3[] = { 18, 9, 27, 0, 36 }; static const uint8_t l_start[] = { 0, 4, 9, 13, 18, 22, 27, 31, 36, 40 }; static const uint8_t l_start_shuffled[] = { 9, 4, 13, 0, 18 }; static const uint8_t serpent1[] = { 0, 1, 2, 2, 1, 0, 0, 1, 2, 2, 1, 0, 0, 1, 2, 2, 1, 0, 0, 1, 2, 2, 1, 0, 0, 1, 2 }; static const uint8_t serpent2[] = { 0, 1, 2, 3, 4, 5, 5, 4, 3, 2, 1, 0, 0, 1, 2, 3, 4, 5, 5, 4, 3, 2, 1, 0, 0, 1, 2, 3, 4, 5 }; static const uint8_t remap[][2] = { { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 1 }, { 0, 2 }, { 0, 3 }, { 10, 0 }, { 10, 1 }, { 10, 2 }, { 10, 3 }, { 20, 0 }, { 20, 1 }, { 20, 2 }, { 20, 3 }, { 30, 0 }, { 30, 1 }, { 30, 2 }, { 30, 3 }, { 40, 0 }, { 40, 1 }, { 40, 2 }, { 40, 3 }, { 50, 0 }, { 50, 1 }, { 50, 2 }, { 50, 3 }, { 60, 0 }, { 60, 1 }, { 60, 2 }, { 60, 3 }, { 70, 0 }, { 70, 1 }, { 70, 2 }, { 70, 3 }, { 0, 64 }, { 0, 65 }, { 0, 66 }, { 10, 64 }, { 10, 65 }, { 10, 66 }, { 20, 64 }, { 20, 65 }, { 20, 66 }, { 30, 64 }, { 30, 65 }, { 30, 66 }, { 40, 64 }, { 40, 65 }, { 40, 66 }, { 50, 64 }, { 50, 65 }, { 50, 66 }, { 60, 64 }, { 60, 65 }, { 60, 66 }, { 70, 64 }, { 70, 65 }, { 70, 66 }, { 0, 67 }, { 20, 67 }, { 40, 67 }, { 60, 67 } }; int i, k, m; int x, y, blk; for (m = 0; m < 5; m++) { switch (d->width) { case 1440: blk = (chan * 11 + seq) * 27 + slot; if (chan == 0 && seq == 11) { x = m * 27 + slot; if (x < 90) { y = 0; } else { x = (x - 90) * 2; y = 67; } } else { i = (4 * chan + blk + off[m]) % 11; k = (blk / 11) % 27; x = shuf1[m] + (chan & 1) * 9 + k % 9; y = (i * 3 + k / 9) * 2 + (chan >> 1) + 1; } tbl[m] = (x << 1) | (y << 9); break; case 1280: blk = (chan * 10 + seq) * 27 + slot; i = (4 * chan + (seq / 5) + 2 * blk + off[m]) % 10; k = (blk / 5) % 27; x = shuf1[m] + (chan & 1) * 9 + k % 9; y = (i * 3 + k / 9) * 2 + (chan >> 1) + 4; if (x >= 80) { x = remap[y][0] + ((x - 80) << (y > 59)); y = remap[y][1]; } tbl[m] = (x << 1) | (y << 9); break; case 960: blk = (chan * 10 + seq) * 27 + slot; i = (4 * chan + (seq / 5) + 2 * blk + off[m]) % 10; k = (blk / 5) % 27 + (i & 1) * 3; x = shuf2[m] + k % 6 + 6 * (chan & 1); y = l_start[i] + k / 6 + 45 * (chan >> 1); tbl[m] = (x << 1) | (y << 9); break; case 720: switch (d->pix_fmt) { case AV_PIX_FMT_YUV422P: x = shuf3[m] + slot / 3; y = serpent1[slot] + ((((seq + off[m]) % d->difseg_size) << 1) + chan) * 3; tbl[m] = (x << 1) | (y << 8); break; case AV_PIX_FMT_YUV420P: x = shuf3[m] + slot / 3; y = serpent1[slot] + ((seq + off[m]) % d->difseg_size) * 3; tbl[m] = (x << 1) | (y << 9); break; case AV_PIX_FMT_YUV411P: i = (seq + off[m]) % d->difseg_size; k = slot + ((m == 1 || m == 2) ? 3 : 0); x = l_start_shuffled[m] + k / 6; y = serpent2[k] + i * 6; if (x > 21) y = y * 2 - i * 6; tbl[m] = (x << 2) | (y << 8); break; } default: break; } } }
['static inline void dv_calc_mb_coordinates(const AVDVProfile *d, int chan,\n int seq, int slot, uint16_t *tbl)\n{\n static const uint8_t off[] = { 2, 6, 8, 0, 4 };\n static const uint8_t shuf1[] = { 36, 18, 54, 0, 72 };\n static const uint8_t shuf2[] = { 24, 12, 36, 0, 48 };\n static const uint8_t shuf3[] = { 18, 9, 27, 0, 36 };\n static const uint8_t l_start[] = { 0, 4, 9, 13, 18, 22, 27, 31, 36, 40 };\n static const uint8_t l_start_shuffled[] = { 9, 4, 13, 0, 18 };\n static const uint8_t serpent1[] = {\n 0, 1, 2, 2, 1, 0,\n 0, 1, 2, 2, 1, 0,\n 0, 1, 2, 2, 1, 0,\n 0, 1, 2, 2, 1, 0,\n 0, 1, 2\n };\n static const uint8_t serpent2[] = {\n 0, 1, 2, 3, 4, 5, 5, 4, 3, 2, 1, 0,\n 0, 1, 2, 3, 4, 5, 5, 4, 3, 2, 1, 0,\n 0, 1, 2, 3, 4, 5\n };\n static const uint8_t remap[][2] = {\n { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },\n { 0, 0 }, { 0, 1 }, { 0, 2 }, { 0, 3 }, { 10, 0 },\n { 10, 1 }, { 10, 2 }, { 10, 3 }, { 20, 0 }, { 20, 1 },\n { 20, 2 }, { 20, 3 }, { 30, 0 }, { 30, 1 }, { 30, 2 },\n { 30, 3 }, { 40, 0 }, { 40, 1 }, { 40, 2 }, { 40, 3 },\n { 50, 0 }, { 50, 1 }, { 50, 2 }, { 50, 3 }, { 60, 0 },\n { 60, 1 }, { 60, 2 }, { 60, 3 }, { 70, 0 }, { 70, 1 },\n { 70, 2 }, { 70, 3 }, { 0, 64 }, { 0, 65 }, { 0, 66 },\n { 10, 64 }, { 10, 65 }, { 10, 66 }, { 20, 64 }, { 20, 65 },\n { 20, 66 }, { 30, 64 }, { 30, 65 }, { 30, 66 }, { 40, 64 },\n { 40, 65 }, { 40, 66 }, { 50, 64 }, { 50, 65 }, { 50, 66 },\n { 60, 64 }, { 60, 65 }, { 60, 66 }, { 70, 64 }, { 70, 65 },\n { 70, 66 }, { 0, 67 }, { 20, 67 }, { 40, 67 }, { 60, 67 }\n };\n int i, k, m;\n int x, y, blk;\n for (m = 0; m < 5; m++) {\n switch (d->width) {\n case 1440:\n blk = (chan * 11 + seq) * 27 + slot;\n if (chan == 0 && seq == 11) {\n x = m * 27 + slot;\n if (x < 90) {\n y = 0;\n } else {\n x = (x - 90) * 2;\n y = 67;\n }\n } else {\n i = (4 * chan + blk + off[m]) % 11;\n k = (blk / 11) % 27;\n x = shuf1[m] + (chan & 1) * 9 + k % 9;\n y = (i * 3 + k / 9) * 2 + (chan >> 1) + 1;\n }\n tbl[m] = (x << 1) | (y << 9);\n break;\n case 1280:\n blk = (chan * 10 + seq) * 27 + slot;\n i = (4 * chan + (seq / 5) + 2 * blk + off[m]) % 10;\n k = (blk / 5) % 27;\n x = shuf1[m] + (chan & 1) * 9 + k % 9;\n y = (i * 3 + k / 9) * 2 + (chan >> 1) + 4;\n if (x >= 80) {\n x = remap[y][0] + ((x - 80) << (y > 59));\n y = remap[y][1];\n }\n tbl[m] = (x << 1) | (y << 9);\n break;\n case 960:\n blk = (chan * 10 + seq) * 27 + slot;\n i = (4 * chan + (seq / 5) + 2 * blk + off[m]) % 10;\n k = (blk / 5) % 27 + (i & 1) * 3;\n x = shuf2[m] + k % 6 + 6 * (chan & 1);\n y = l_start[i] + k / 6 + 45 * (chan >> 1);\n tbl[m] = (x << 1) | (y << 9);\n break;\n case 720:\n switch (d->pix_fmt) {\n case AV_PIX_FMT_YUV422P:\n x = shuf3[m] + slot / 3;\n y = serpent1[slot] +\n ((((seq + off[m]) % d->difseg_size) << 1) + chan) * 3;\n tbl[m] = (x << 1) | (y << 8);\n break;\n case AV_PIX_FMT_YUV420P:\n x = shuf3[m] + slot / 3;\n y = serpent1[slot] +\n ((seq + off[m]) % d->difseg_size) * 3;\n tbl[m] = (x << 1) | (y << 9);\n break;\n case AV_PIX_FMT_YUV411P:\n i = (seq + off[m]) % d->difseg_size;\n k = slot + ((m == 1 || m == 2) ? 3 : 0);\n x = l_start_shuffled[m] + k / 6;\n y = serpent2[k] + i * 6;\n if (x > 21)\n y = y * 2 - i * 6;\n tbl[m] = (x << 2) | (y << 8);\n break;\n }\n default:\n break;\n }\n }\n}']
861
0
https://github.com/libav/libav/blob/b4cfb8254eeeb2fc0aa2c0c36a5ede208af47a79/libavcodec/imgconvert.c/#L1004
static void deinterlace_bottom_field_inplace(uint8_t *src1, int src_wrap, int width, int height) { uint8_t *src_m1, *src_0, *src_p1, *src_p2; int y; uint8_t *buf; buf = (uint8_t*)av_malloc(width); src_m1 = src1; memcpy(buf,src_m1,width); src_0=&src_m1[src_wrap]; src_p1=&src_0[src_wrap]; src_p2=&src_p1[src_wrap]; for(y=0;y<(height-2);y+=2) { deinterlace_line_inplace(buf,src_m1,src_0,src_p1,src_p2,width); src_m1 = src_p1; src_0 = src_p2; src_p1 += 2*src_wrap; src_p2 += 2*src_wrap; } deinterlace_line_inplace(buf,src_m1,src_0,src_0,src_0,width); av_free(buf); }
['static void deinterlace_bottom_field_inplace(uint8_t *src1, int src_wrap,\n int width, int height)\n{\n uint8_t *src_m1, *src_0, *src_p1, *src_p2;\n int y;\n uint8_t *buf;\n buf = (uint8_t*)av_malloc(width);\n src_m1 = src1;\n memcpy(buf,src_m1,width);\n src_0=&src_m1[src_wrap];\n src_p1=&src_0[src_wrap];\n src_p2=&src_p1[src_wrap];\n for(y=0;y<(height-2);y+=2) {\n deinterlace_line_inplace(buf,src_m1,src_0,src_p1,src_p2,width);\n src_m1 = src_p1;\n src_0 = src_p2;\n src_p1 += 2*src_wrap;\n src_p2 += 2*src_wrap;\n }\n deinterlace_line_inplace(buf,src_m1,src_0,src_0,src_0,width);\n av_free(buf);\n}', 'void *av_malloc(size_t size)\n{\n void *ptr = NULL;\n#if CONFIG_MEMALIGN_HACK\n long diff;\n#endif\n if(size > (INT_MAX-32) )\n return NULL;\n#if CONFIG_MEMALIGN_HACK\n ptr = malloc(size+32);\n if(!ptr)\n return ptr;\n diff= ((-(long)ptr - 1)&31) + 1;\n ptr = (char*)ptr + diff;\n ((char*)ptr)[-1]= diff;\n#elif HAVE_POSIX_MEMALIGN\n if (posix_memalign(&ptr,32,size))\n ptr = NULL;\n#elif HAVE_MEMALIGN\n ptr = memalign(32,size);\n#else\n ptr = malloc(size);\n#endif\n return ptr;\n}']
862
0
https://github.com/openssl/openssl/blob/09977dd095f3c655c99b9e1810a213f7eafa7364/crypto/bn/bn_lib.c/#L342
static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words) { BN_ULONG *A, *a = NULL; const BN_ULONG *B; int i; bn_check_top(b); if (words > (INT_MAX / (4 * BN_BITS2))) { BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG); return NULL; } if (BN_get_flags(b, BN_FLG_STATIC_DATA)) { BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA); return (NULL); } if (BN_get_flags(b,BN_FLG_SECURE)) a = A = OPENSSL_secure_zalloc(words * sizeof(*a)); else a = A = OPENSSL_zalloc(words * sizeof(*a)); if (A == NULL) { BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE); return (NULL); } #if 1 B = b->d; if (B != NULL) { for (i = b->top >> 2; i > 0; i--, A += 4, B += 4) { BN_ULONG a0, a1, a2, a3; a0 = B[0]; a1 = B[1]; a2 = B[2]; a3 = B[3]; A[0] = a0; A[1] = a1; A[2] = a2; A[3] = a3; } switch (b->top & 3) { case 3: A[2] = B[2]; case 2: A[1] = B[1]; case 1: A[0] = B[0]; case 0: ; } } #else memset(A, 0, sizeof(*A) * words); memcpy(A, b->d, sizeof(b->d[0]) * b->top); #endif return (a); }
['int ec_GFp_simple_set_compressed_coordinates(const EC_GROUP *group,\n EC_POINT *point,\n const BIGNUM *x_, int y_bit,\n BN_CTX *ctx)\n{\n BN_CTX *new_ctx = NULL;\n BIGNUM *tmp1, *tmp2, *x, *y;\n int ret = 0;\n ERR_clear_error();\n if (ctx == NULL) {\n ctx = new_ctx = BN_CTX_new();\n if (ctx == NULL)\n return 0;\n }\n y_bit = (y_bit != 0);\n BN_CTX_start(ctx);\n tmp1 = BN_CTX_get(ctx);\n tmp2 = BN_CTX_get(ctx);\n x = BN_CTX_get(ctx);\n y = BN_CTX_get(ctx);\n if (y == NULL)\n goto err;\n if (!BN_nnmod(x, x_, group->field, ctx))\n goto err;\n if (group->meth->field_decode == 0) {\n if (!group->meth->field_sqr(group, tmp2, x_, ctx))\n goto err;\n if (!group->meth->field_mul(group, tmp1, tmp2, x_, ctx))\n goto err;\n } else {\n if (!BN_mod_sqr(tmp2, x_, group->field, ctx))\n goto err;\n if (!BN_mod_mul(tmp1, tmp2, x_, group->field, ctx))\n goto err;\n }\n if (group->a_is_minus3) {\n if (!BN_mod_lshift1_quick(tmp2, x, group->field))\n goto err;\n if (!BN_mod_add_quick(tmp2, tmp2, x, group->field))\n goto err;\n if (!BN_mod_sub_quick(tmp1, tmp1, tmp2, group->field))\n goto err;\n } else {\n if (group->meth->field_decode) {\n if (!group->meth->field_decode(group, tmp2, group->a, ctx))\n goto err;\n if (!BN_mod_mul(tmp2, tmp2, x, group->field, ctx))\n goto err;\n } else {\n if (!group->meth->field_mul(group, tmp2, group->a, x, ctx))\n goto err;\n }\n if (!BN_mod_add_quick(tmp1, tmp1, tmp2, group->field))\n goto err;\n }\n if (group->meth->field_decode) {\n if (!group->meth->field_decode(group, tmp2, group->b, ctx))\n goto err;\n if (!BN_mod_add_quick(tmp1, tmp1, tmp2, group->field))\n goto err;\n } else {\n if (!BN_mod_add_quick(tmp1, tmp1, group->b, group->field))\n goto err;\n }\n if (!BN_mod_sqrt(y, tmp1, group->field, ctx)) {\n unsigned long err = ERR_peek_last_error();\n if (ERR_GET_LIB(err) == ERR_LIB_BN\n && ERR_GET_REASON(err) == BN_R_NOT_A_SQUARE) {\n ERR_clear_error();\n ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,\n EC_R_INVALID_COMPRESSED_POINT);\n } else\n ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,\n ERR_R_BN_LIB);\n goto err;\n }\n if (y_bit != BN_is_odd(y)) {\n if (BN_is_zero(y)) {\n int kron;\n kron = BN_kronecker(x, group->field, ctx);\n if (kron == -2)\n goto err;\n if (kron == 1)\n ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,\n EC_R_INVALID_COMPRESSION_BIT);\n else\n ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,\n EC_R_INVALID_COMPRESSED_POINT);\n goto err;\n }\n if (!BN_usub(y, group->field, y))\n goto err;\n }\n if (y_bit != BN_is_odd(y)) {\n ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,\n ERR_R_INTERNAL_ERROR);\n goto err;\n }\n if (!EC_POINT_set_affine_coordinates_GFp(group, point, x, y, ctx))\n goto err;\n ret = 1;\n err:\n BN_CTX_end(ctx);\n BN_CTX_free(new_ctx);\n return ret;\n}', 'BIGNUM *BN_CTX_get(BN_CTX *ctx)\n{\n BIGNUM *ret;\n CTXDBG_ENTRY("BN_CTX_get", ctx);\n if (ctx->err_stack || ctx->too_many)\n return NULL;\n if ((ret = BN_POOL_get(&ctx->pool, ctx->flags)) == NULL) {\n ctx->too_many = 1;\n BNerr(BN_F_BN_CTX_GET, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n return NULL;\n }\n BN_zero(ret);\n ctx->used++;\n CTXDBG_RET(ctx, ret);\n return ret;\n}', 'int BN_set_word(BIGNUM *a, BN_ULONG w)\n{\n bn_check_top(a);\n if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)\n return (0);\n a->neg = 0;\n a->d[0] = w;\n a->top = (w ? 1 : 0);\n bn_check_top(a);\n return (1);\n}', 'static ossl_inline BIGNUM *bn_expand(BIGNUM *a, int bits)\n{\n if (bits > (INT_MAX - BN_BITS2 + 1))\n return NULL;\n if(((bits+BN_BITS2-1)/BN_BITS2) <= (a)->dmax)\n return a;\n return bn_expand2((a),(bits+BN_BITS2-1)/BN_BITS2);\n}', 'int BN_mod_sqr(BIGNUM *r, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx)\n{\n if (!BN_sqr(r, a, ctx))\n return 0;\n return BN_mod(r, r, m, ctx);\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}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n return (words <= a->dmax) ? a : bn_expand2(a, words);\n}', 'BIGNUM *bn_expand2(BIGNUM *b, int words)\n{\n bn_check_top(b);\n if (words > b->dmax) {\n BN_ULONG *a = bn_expand_internal(b, words);\n if (!a)\n return NULL;\n if (b->d) {\n OPENSSL_cleanse(b->d, b->dmax * sizeof(b->d[0]));\n bn_free_d(b);\n }\n b->d = a;\n b->dmax = words;\n }\n bn_check_top(b);\n return b;\n}', 'static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)\n{\n BN_ULONG *A, *a = NULL;\n const BN_ULONG *B;\n int i;\n bn_check_top(b);\n if (words > (INT_MAX / (4 * BN_BITS2))) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);\n return NULL;\n }\n if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);\n return (NULL);\n }\n if (BN_get_flags(b,BN_FLG_SECURE))\n a = A = OPENSSL_secure_zalloc(words * sizeof(*a));\n else\n a = A = OPENSSL_zalloc(words * sizeof(*a));\n if (A == NULL) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);\n return (NULL);\n }\n#if 1\n B = b->d;\n if (B != NULL) {\n for (i = b->top >> 2; i > 0; i--, A += 4, B += 4) {\n BN_ULONG a0, a1, a2, a3;\n a0 = B[0];\n a1 = B[1];\n a2 = B[2];\n a3 = B[3];\n A[0] = a0;\n A[1] = a1;\n A[2] = a2;\n A[3] = a3;\n }\n switch (b->top & 3) {\n case 3:\n A[2] = B[2];\n case 2:\n A[1] = B[1];\n case 1:\n A[0] = B[0];\n case 0:\n ;\n }\n }\n#else\n memset(A, 0, sizeof(*A) * words);\n memcpy(A, b->d, sizeof(b->d[0]) * b->top);\n#endif\n return (a);\n}']
863
0
https://github.com/openssl/openssl/blob/3aa8d3a7f11fdcef71240a1ae0c4f6000986cc45/apps/ca.c/#L3023
int unpack_revinfo(ASN1_TIME **prevtm, int *preason, ASN1_OBJECT **phold, ASN1_GENERALIZEDTIME **pinvtm, char *str) { char *tmp = NULL; char *rtime_str, *reason_str = NULL, *arg_str = NULL, *p; int reason_code = -1; int i, ret = 0; ASN1_OBJECT *hold = NULL; ASN1_GENERALIZEDTIME *comp_time = NULL; tmp = BUF_strdup(str); p = strchr(tmp, ','); rtime_str = tmp; if (p) { *p = '\0'; p++; reason_str = p; p = strchr(p, ','); if (p) { *p = '\0'; arg_str = p + 1; } } if (prevtm) { *prevtm = ASN1_UTCTIME_new(); if (!ASN1_UTCTIME_set_string(*prevtm, rtime_str)) { BIO_printf(bio_err, "invalid revocation date %s\n", rtime_str); goto err; } } if (reason_str) { for (i = 0; i < NUM_REASONS; i++) { if(!strcasecmp(reason_str, crl_reasons[i])) { reason_code = i; break; } } if (reason_code == OCSP_REVOKED_STATUS_NOSTATUS) { BIO_printf(bio_err, "invalid reason code %s\n", reason_str); goto err; } if (reason_code == 7) reason_code = OCSP_REVOKED_STATUS_REMOVEFROMCRL; else if (reason_code == 8) { if (!arg_str) { BIO_printf(bio_err, "missing hold instruction\n"); goto err; } reason_code = OCSP_REVOKED_STATUS_CERTIFICATEHOLD; hold = OBJ_txt2obj(arg_str, 0); if (!hold) { BIO_printf(bio_err, "invalid object identifier %s\n", arg_str); goto err; } if (phold) *phold = hold; } else if ((reason_code == 9) || (reason_code == 10)) { if (!arg_str) { BIO_printf(bio_err, "missing compromised time\n"); goto err; } comp_time = ASN1_GENERALIZEDTIME_new(); if (!ASN1_GENERALIZEDTIME_set_string(comp_time, arg_str)) { BIO_printf(bio_err, "invalid compromised time %s\n", arg_str); goto err; } if (reason_code == 9) reason_code = OCSP_REVOKED_STATUS_KEYCOMPROMISE; else reason_code = OCSP_REVOKED_STATUS_CACOMPROMISE; } } if (preason) *preason = reason_code; if (pinvtm) *pinvtm = comp_time; else ASN1_GENERALIZEDTIME_free(comp_time); ret = 1; err: if (tmp) OPENSSL_free(tmp); if (!phold) ASN1_OBJECT_free(hold); if (!pinvtm) ASN1_GENERALIZEDTIME_free(comp_time); return ret; }
['int unpack_revinfo(ASN1_TIME **prevtm, int *preason, ASN1_OBJECT **phold, ASN1_GENERALIZEDTIME **pinvtm, char *str)\n\t{\n\tchar *tmp = NULL;\n\tchar *rtime_str, *reason_str = NULL, *arg_str = NULL, *p;\n\tint reason_code = -1;\n\tint i, ret = 0;\n\tASN1_OBJECT *hold = NULL;\n\tASN1_GENERALIZEDTIME *comp_time = NULL;\n\ttmp = BUF_strdup(str);\n\tp = strchr(tmp, \',\');\n\trtime_str = tmp;\n\tif (p)\n\t\t{\n\t\t*p = \'\\0\';\n\t\tp++;\n\t\treason_str = p;\n\t\tp = strchr(p, \',\');\n\t\tif (p)\n\t\t\t{\n\t\t\t*p = \'\\0\';\n\t\t\targ_str = p + 1;\n\t\t\t}\n\t\t}\n\tif (prevtm)\n\t\t{\n\t\t*prevtm = ASN1_UTCTIME_new();\n\t\tif (!ASN1_UTCTIME_set_string(*prevtm, rtime_str))\n\t\t\t{\n\t\t\tBIO_printf(bio_err, "invalid revocation date %s\\n", rtime_str);\n\t\t\tgoto err;\n\t\t\t}\n\t\t}\n\tif (reason_str)\n\t\t{\n\t\tfor (i = 0; i < NUM_REASONS; i++)\n\t\t\t{\n\t\t\tif(!strcasecmp(reason_str, crl_reasons[i]))\n\t\t\t\t{\n\t\t\t\treason_code = i;\n\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\tif (reason_code == OCSP_REVOKED_STATUS_NOSTATUS)\n\t\t\t{\n\t\t\tBIO_printf(bio_err, "invalid reason code %s\\n", reason_str);\n\t\t\tgoto err;\n\t\t\t}\n\t\tif (reason_code == 7)\n\t\t\treason_code = OCSP_REVOKED_STATUS_REMOVEFROMCRL;\n\t\telse if (reason_code == 8)\n\t\t\t{\n\t\t\tif (!arg_str)\n\t\t\t\t{\n\t\t\t\tBIO_printf(bio_err, "missing hold instruction\\n");\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\treason_code = OCSP_REVOKED_STATUS_CERTIFICATEHOLD;\n\t\t\thold = OBJ_txt2obj(arg_str, 0);\n\t\t\tif (!hold)\n\t\t\t\t{\n\t\t\t\tBIO_printf(bio_err, "invalid object identifier %s\\n", arg_str);\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\tif (phold) *phold = hold;\n\t\t\t}\n\t\telse if ((reason_code == 9) || (reason_code == 10))\n\t\t\t{\n\t\t\tif (!arg_str)\n\t\t\t\t{\n\t\t\t\tBIO_printf(bio_err, "missing compromised time\\n");\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\tcomp_time = ASN1_GENERALIZEDTIME_new();\n\t\t\tif (!ASN1_GENERALIZEDTIME_set_string(comp_time, arg_str))\n\t\t\t\t{\n\t\t\t\tBIO_printf(bio_err, "invalid compromised time %s\\n", arg_str);\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\tif (reason_code == 9)\n\t\t\t\treason_code = OCSP_REVOKED_STATUS_KEYCOMPROMISE;\n\t\t\telse\n\t\t\t\treason_code = OCSP_REVOKED_STATUS_CACOMPROMISE;\n\t\t\t}\n\t\t}\n\tif (preason) *preason = reason_code;\n\tif (pinvtm) *pinvtm = comp_time;\n\telse ASN1_GENERALIZEDTIME_free(comp_time);\n\tret = 1;\n\terr:\n\tif (tmp) OPENSSL_free(tmp);\n\tif (!phold) ASN1_OBJECT_free(hold);\n\tif (!pinvtm) ASN1_GENERALIZEDTIME_free(comp_time);\n\treturn ret;\n\t}', 'char *BUF_strdup(const char *str)\n\t{\n\tif (str == NULL) return(NULL);\n\treturn BUF_strndup(str, strlen(str));\n\t}', 'char *BUF_strndup(const char *str, size_t siz)\n\t{\n\tchar *ret;\n\tif (str == NULL) return(NULL);\n\tret=OPENSSL_malloc(siz+1);\n\tif (ret == NULL)\n\t\t{\n\t\tBUFerr(BUF_F_BUF_STRNDUP,ERR_R_MALLOC_FAILURE);\n\t\treturn(NULL);\n\t\t}\n\tBUF_strlcpy(ret,str,siz+1);\n\treturn(ret);\n\t}', 'void *CRYPTO_malloc(int num, const char *file, int line)\n\t{\n\tvoid *ret = NULL;\n\textern unsigned char cleanse_ctr;\n\tif (num < 0) return NULL;\n\tallow_customize = 0;\n\tif (malloc_debug_func != NULL)\n\t\t{\n\t\tallow_customize_debug = 0;\n\t\tmalloc_debug_func(NULL, num, file, line, 0);\n\t\t}\n\tret = malloc_ex_func(num,file,line);\n#ifdef LEVITTE_DEBUG_MEM\n\tfprintf(stderr, "LEVITTE_DEBUG_MEM: > 0x%p (%d)\\n", ret, num);\n#endif\n\tif (malloc_debug_func != NULL)\n\t\tmalloc_debug_func(ret, num, file, line, 1);\n if(ret && (num > 2048))\n ((unsigned char *)ret)[0] = cleanse_ctr;\n\treturn ret;\n\t}', 'void ERR_put_error(int lib, int func, int reason, const char *file,\n\t int line)\n\t{\n\tERR_STATE *es;\n#ifdef _OSD_POSIX\n\tif (strncmp(file,"*POSIX(", sizeof("*POSIX(")-1) == 0) {\n\t\tchar *end;\n\t\tfile += sizeof("*POSIX(")-1;\n\t\tend = &file[strlen(file)-1];\n\t\tif (*end == \')\')\n\t\t\t*end = \'\\0\';\n\t\tif ((end = strrchr(file, \'/\')) != NULL)\n\t\t\tfile = &end[1];\n\t}\n#endif\n\tes=ERR_get_state();\n\tes->top=(es->top+1)%ERR_NUM_ERRORS;\n\tif (es->top == es->bottom)\n\t\tes->bottom=(es->bottom+1)%ERR_NUM_ERRORS;\n\tes->err_flags[es->top]=0;\n\tes->err_buffer[es->top]=ERR_PACK(lib,func,reason);\n\tes->err_file[es->top]=file;\n\tes->err_line[es->top]=line;\n\terr_clear_data(es,es->top);\n\t}', 'void CRYPTO_free(void *str)\n\t{\n\tif (free_debug_func != NULL)\n\t\tfree_debug_func(str, 0);\n#ifdef LEVITTE_DEBUG_MEM\n\tfprintf(stderr, "LEVITTE_DEBUG_MEM: < 0x%p\\n", str);\n#endif\n\tfree_func(str);\n\tif (free_debug_func != NULL)\n\t\tfree_debug_func(NULL, 1);\n\t}']
864
0
https://github.com/libav/libav/blob/613a4e3f6d9c52251e1449c656ea0a85b424d001/libavformat/matroskadec.c/#L926
static int matroska_decode_buffer(uint8_t** buf, int* buf_size, MatroskaTrack *track) { MatroskaTrackEncoding *encodings = track->encodings.elem; uint8_t* data = *buf; int isize = *buf_size; uint8_t* pkt_data = NULL; int pkt_size = isize; int result = 0; int olen; if (pkt_size >= 10000000) return -1; switch (encodings[0].compression.algo) { case MATROSKA_TRACK_ENCODING_COMP_HEADERSTRIP: return encodings[0].compression.settings.size; case MATROSKA_TRACK_ENCODING_COMP_LZO: do { olen = pkt_size *= 3; pkt_data = av_realloc(pkt_data, pkt_size+AV_LZO_OUTPUT_PADDING); result = av_lzo1x_decode(pkt_data, &olen, data, &isize); } while (result==AV_LZO_OUTPUT_FULL && pkt_size<10000000); if (result) goto failed; pkt_size -= olen; break; #if CONFIG_ZLIB case MATROSKA_TRACK_ENCODING_COMP_ZLIB: { z_stream zstream = {0}; if (inflateInit(&zstream) != Z_OK) return -1; zstream.next_in = data; zstream.avail_in = isize; do { pkt_size *= 3; pkt_data = av_realloc(pkt_data, pkt_size); zstream.avail_out = pkt_size - zstream.total_out; zstream.next_out = pkt_data + zstream.total_out; result = inflate(&zstream, Z_NO_FLUSH); } while (result==Z_OK && pkt_size<10000000); pkt_size = zstream.total_out; inflateEnd(&zstream); if (result != Z_STREAM_END) goto failed; break; } #endif #if CONFIG_BZLIB case MATROSKA_TRACK_ENCODING_COMP_BZLIB: { bz_stream bzstream = {0}; if (BZ2_bzDecompressInit(&bzstream, 0, 0) != BZ_OK) return -1; bzstream.next_in = data; bzstream.avail_in = isize; do { pkt_size *= 3; pkt_data = av_realloc(pkt_data, pkt_size); bzstream.avail_out = pkt_size - bzstream.total_out_lo32; bzstream.next_out = pkt_data + bzstream.total_out_lo32; result = BZ2_bzDecompress(&bzstream); } while (result==BZ_OK && pkt_size<10000000); pkt_size = bzstream.total_out_lo32; BZ2_bzDecompressEnd(&bzstream); if (result != BZ_STREAM_END) goto failed; break; } #endif default: return -1; } *buf = pkt_data; *buf_size = pkt_size; return 0; failed: av_free(pkt_data); return -1; }
['static int matroska_decode_buffer(uint8_t** buf, int* buf_size,\n MatroskaTrack *track)\n{\n MatroskaTrackEncoding *encodings = track->encodings.elem;\n uint8_t* data = *buf;\n int isize = *buf_size;\n uint8_t* pkt_data = NULL;\n int pkt_size = isize;\n int result = 0;\n int olen;\n if (pkt_size >= 10000000)\n return -1;\n switch (encodings[0].compression.algo) {\n case MATROSKA_TRACK_ENCODING_COMP_HEADERSTRIP:\n return encodings[0].compression.settings.size;\n case MATROSKA_TRACK_ENCODING_COMP_LZO:\n do {\n olen = pkt_size *= 3;\n pkt_data = av_realloc(pkt_data, pkt_size+AV_LZO_OUTPUT_PADDING);\n result = av_lzo1x_decode(pkt_data, &olen, data, &isize);\n } while (result==AV_LZO_OUTPUT_FULL && pkt_size<10000000);\n if (result)\n goto failed;\n pkt_size -= olen;\n break;\n#if CONFIG_ZLIB\n case MATROSKA_TRACK_ENCODING_COMP_ZLIB: {\n z_stream zstream = {0};\n if (inflateInit(&zstream) != Z_OK)\n return -1;\n zstream.next_in = data;\n zstream.avail_in = isize;\n do {\n pkt_size *= 3;\n pkt_data = av_realloc(pkt_data, pkt_size);\n zstream.avail_out = pkt_size - zstream.total_out;\n zstream.next_out = pkt_data + zstream.total_out;\n result = inflate(&zstream, Z_NO_FLUSH);\n } while (result==Z_OK && pkt_size<10000000);\n pkt_size = zstream.total_out;\n inflateEnd(&zstream);\n if (result != Z_STREAM_END)\n goto failed;\n break;\n }\n#endif\n#if CONFIG_BZLIB\n case MATROSKA_TRACK_ENCODING_COMP_BZLIB: {\n bz_stream bzstream = {0};\n if (BZ2_bzDecompressInit(&bzstream, 0, 0) != BZ_OK)\n return -1;\n bzstream.next_in = data;\n bzstream.avail_in = isize;\n do {\n pkt_size *= 3;\n pkt_data = av_realloc(pkt_data, pkt_size);\n bzstream.avail_out = pkt_size - bzstream.total_out_lo32;\n bzstream.next_out = pkt_data + bzstream.total_out_lo32;\n result = BZ2_bzDecompress(&bzstream);\n } while (result==BZ_OK && pkt_size<10000000);\n pkt_size = bzstream.total_out_lo32;\n BZ2_bzDecompressEnd(&bzstream);\n if (result != BZ_STREAM_END)\n goto failed;\n break;\n }\n#endif\n default:\n return -1;\n }\n *buf = pkt_data;\n *buf_size = pkt_size;\n return 0;\n failed:\n av_free(pkt_data);\n return -1;\n}', 'void *av_realloc(void *ptr, unsigned int size)\n{\n#if CONFIG_MEMALIGN_HACK\n int diff;\n#endif\n if(size > (INT_MAX-16) )\n return NULL;\n#if CONFIG_MEMALIGN_HACK\n if(!ptr) return av_malloc(size);\n diff= ((char*)ptr)[-1];\n return (char*)realloc((char*)ptr - diff, size + diff) + diff;\n#else\n return realloc(ptr, size);\n#endif\n}']
865
0
https://github.com/libav/libav/blob/67ce33162aa93bee1a5f9e8d6f00060329fa67da/libavcodec/sonic.c/#L545
static av_cold int sonic_encode_init(AVCodecContext *avctx) { SonicContext *s = avctx->priv_data; PutBitContext pb; int i, version = 0; if (avctx->channels > MAX_CHANNELS) { av_log(avctx, AV_LOG_ERROR, "Only mono and stereo streams are supported by now\n"); return -1; } if (avctx->channels == 2) s->decorrelation = MID_SIDE; if (avctx->codec->id == CODEC_ID_SONIC_LS) { s->lossless = 1; s->num_taps = 32; s->downsampling = 1; s->quantization = 0.0; } else { s->num_taps = 128; s->downsampling = 2; s->quantization = 1.0; } if ((s->num_taps < 32) || (s->num_taps > 1024) || ((s->num_taps>>5)<<5 != s->num_taps)) { av_log(avctx, AV_LOG_ERROR, "Invalid number of taps\n"); return -1; } s->tap_quant = av_mallocz(4* s->num_taps); for (i = 0; i < s->num_taps; i++) s->tap_quant[i] = (int)(sqrt(i+1)); s->channels = avctx->channels; s->samplerate = avctx->sample_rate; s->block_align = (int)(2048.0*s->samplerate/44100)/s->downsampling; s->frame_size = s->channels*s->block_align*s->downsampling; s->tail = av_mallocz(4* s->num_taps*s->channels); if (!s->tail) return -1; s->tail_size = s->num_taps*s->channels; s->predictor_k = av_mallocz(4 * s->num_taps); if (!s->predictor_k) return -1; for (i = 0; i < s->channels; i++) { s->coded_samples[i] = av_mallocz(4* s->block_align); if (!s->coded_samples[i]) return -1; } s->int_samples = av_mallocz(4* s->frame_size); s->window_size = ((2*s->tail_size)+s->frame_size); s->window = av_mallocz(4* s->window_size); if (!s->window) return -1; avctx->extradata = av_mallocz(16); if (!avctx->extradata) return -1; init_put_bits(&pb, avctx->extradata, 16*8); put_bits(&pb, 2, version); if (version == 1) { put_bits(&pb, 2, s->channels); put_bits(&pb, 4, code_samplerate(s->samplerate)); } put_bits(&pb, 1, s->lossless); if (!s->lossless) put_bits(&pb, 3, SAMPLE_SHIFT); put_bits(&pb, 2, s->decorrelation); put_bits(&pb, 2, s->downsampling); put_bits(&pb, 5, (s->num_taps >> 5)-1); put_bits(&pb, 1, 0); flush_put_bits(&pb); avctx->extradata_size = put_bits_count(&pb)/8; av_log(avctx, AV_LOG_INFO, "Sonic: ver: %d ls: %d dr: %d taps: %d block: %d frame: %d downsamp: %d\n", version, s->lossless, s->decorrelation, s->num_taps, s->block_align, s->frame_size, s->downsampling); avctx->coded_frame = avcodec_alloc_frame(); if (!avctx->coded_frame) return AVERROR(ENOMEM); avctx->coded_frame->key_frame = 1; avctx->frame_size = s->block_align*s->downsampling; return 0; }
['static av_cold int sonic_encode_init(AVCodecContext *avctx)\n{\n SonicContext *s = avctx->priv_data;\n PutBitContext pb;\n int i, version = 0;\n if (avctx->channels > MAX_CHANNELS)\n {\n av_log(avctx, AV_LOG_ERROR, "Only mono and stereo streams are supported by now\\n");\n return -1;\n }\n if (avctx->channels == 2)\n s->decorrelation = MID_SIDE;\n if (avctx->codec->id == CODEC_ID_SONIC_LS)\n {\n s->lossless = 1;\n s->num_taps = 32;\n s->downsampling = 1;\n s->quantization = 0.0;\n }\n else\n {\n s->num_taps = 128;\n s->downsampling = 2;\n s->quantization = 1.0;\n }\n if ((s->num_taps < 32) || (s->num_taps > 1024) ||\n ((s->num_taps>>5)<<5 != s->num_taps))\n {\n av_log(avctx, AV_LOG_ERROR, "Invalid number of taps\\n");\n return -1;\n }\n s->tap_quant = av_mallocz(4* s->num_taps);\n for (i = 0; i < s->num_taps; i++)\n s->tap_quant[i] = (int)(sqrt(i+1));\n s->channels = avctx->channels;\n s->samplerate = avctx->sample_rate;\n s->block_align = (int)(2048.0*s->samplerate/44100)/s->downsampling;\n s->frame_size = s->channels*s->block_align*s->downsampling;\n s->tail = av_mallocz(4* s->num_taps*s->channels);\n if (!s->tail)\n return -1;\n s->tail_size = s->num_taps*s->channels;\n s->predictor_k = av_mallocz(4 * s->num_taps);\n if (!s->predictor_k)\n return -1;\n for (i = 0; i < s->channels; i++)\n {\n s->coded_samples[i] = av_mallocz(4* s->block_align);\n if (!s->coded_samples[i])\n return -1;\n }\n s->int_samples = av_mallocz(4* s->frame_size);\n s->window_size = ((2*s->tail_size)+s->frame_size);\n s->window = av_mallocz(4* s->window_size);\n if (!s->window)\n return -1;\n avctx->extradata = av_mallocz(16);\n if (!avctx->extradata)\n return -1;\n init_put_bits(&pb, avctx->extradata, 16*8);\n put_bits(&pb, 2, version);\n if (version == 1)\n {\n put_bits(&pb, 2, s->channels);\n put_bits(&pb, 4, code_samplerate(s->samplerate));\n }\n put_bits(&pb, 1, s->lossless);\n if (!s->lossless)\n put_bits(&pb, 3, SAMPLE_SHIFT);\n put_bits(&pb, 2, s->decorrelation);\n put_bits(&pb, 2, s->downsampling);\n put_bits(&pb, 5, (s->num_taps >> 5)-1);\n put_bits(&pb, 1, 0);\n flush_put_bits(&pb);\n avctx->extradata_size = put_bits_count(&pb)/8;\n av_log(avctx, AV_LOG_INFO, "Sonic: ver: %d ls: %d dr: %d taps: %d block: %d frame: %d downsamp: %d\\n",\n version, s->lossless, s->decorrelation, s->num_taps, s->block_align, s->frame_size, s->downsampling);\n avctx->coded_frame = avcodec_alloc_frame();\n if (!avctx->coded_frame)\n return AVERROR(ENOMEM);\n avctx->coded_frame->key_frame = 1;\n avctx->frame_size = s->block_align*s->downsampling;\n return 0;\n}', 'void *av_mallocz(unsigned int size)\n{\n void *ptr = av_malloc(size);\n if (ptr)\n memset(ptr, 0, size);\n return ptr;\n}', 'void *av_malloc(unsigned int size)\n{\n void *ptr;\n#ifdef CONFIG_MEMALIGN_HACK\n long diff;\n#endif\n if(size > (INT_MAX-16) )\n return NULL;\n#ifdef CONFIG_MEMALIGN_HACK\n ptr = malloc(size+16);\n if(!ptr)\n return ptr;\n diff= ((-(long)ptr - 1)&15) + 1;\n ptr = (char*)ptr + diff;\n ((char*)ptr)[-1]= diff;\n#elif defined (HAVE_MEMALIGN)\n ptr = memalign(16,size);\n#else\n ptr = malloc(size);\n#endif\n return ptr;\n}']
866
0
https://github.com/openssl/openssl/blob/9b02dc97e4963969da69675a871dbe80e6d31cda/crypto/bn/bn_lib.c/#L260
static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words) { BN_ULONG *a = NULL; bn_check_top(b); if (words > (INT_MAX / (4 * BN_BITS2))) { BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG); return NULL; } if (BN_get_flags(b, BN_FLG_STATIC_DATA)) { BNerr(BN_F_BN_EXPAND_INTERNAL, 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) { BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE); return NULL; } assert(b->top <= words); if (b->top > 0) memcpy(a, b->d, sizeof(*a) * b->top); return a; }
['int BN_generate_prime_ex(BIGNUM *ret, int bits, int safe,\n const BIGNUM *add, const BIGNUM *rem, BN_GENCB *cb)\n{\n BIGNUM *t;\n int found = 0;\n int i, j, c1 = 0;\n BN_CTX *ctx = NULL;\n prime_t *mods = NULL;\n int checks = BN_prime_checks_for_size(bits);\n if (bits < 2) {\n BNerr(BN_F_BN_GENERATE_PRIME_EX, BN_R_BITS_TOO_SMALL);\n return 0;\n } else if (bits == 2 && safe) {\n BNerr(BN_F_BN_GENERATE_PRIME_EX, BN_R_BITS_TOO_SMALL);\n return 0;\n }\n mods = OPENSSL_zalloc(sizeof(*mods) * NUMPRIMES);\n if (mods == NULL)\n goto err;\n ctx = BN_CTX_new();\n if (ctx == NULL)\n goto err;\n BN_CTX_start(ctx);\n t = BN_CTX_get(ctx);\n if (t == NULL)\n goto err;\n loop:\n if (add == NULL) {\n if (!probable_prime(ret, bits, mods))\n goto err;\n } else {\n if (safe) {\n if (!probable_prime_dh_safe(ret, bits, add, rem, ctx))\n goto err;\n } else {\n if (!bn_probable_prime_dh(ret, bits, add, rem, ctx))\n goto err;\n }\n }\n if (!BN_GENCB_call(cb, 0, c1++))\n goto err;\n if (!safe) {\n i = BN_is_prime_fasttest_ex(ret, checks, ctx, 0, cb);\n if (i == -1)\n goto err;\n if (i == 0)\n goto loop;\n } else {\n if (!BN_rshift1(t, ret))\n goto err;\n for (i = 0; i < checks; i++) {\n j = BN_is_prime_fasttest_ex(ret, 1, ctx, 0, cb);\n if (j == -1)\n goto err;\n if (j == 0)\n goto loop;\n j = BN_is_prime_fasttest_ex(t, 1, ctx, 0, cb);\n if (j == -1)\n goto err;\n if (j == 0)\n goto loop;\n if (!BN_GENCB_call(cb, 2, c1 - 1))\n goto err;\n }\n }\n found = 1;\n err:\n OPENSSL_free(mods);\n if (ctx != NULL)\n BN_CTX_end(ctx);\n BN_CTX_free(ctx);\n bn_check_top(ret);\n return found;\n}', 'static int probable_prime_dh_safe(BIGNUM *p, int bits, const BIGNUM *padd,\n const BIGNUM *rem, BN_CTX *ctx)\n{\n int i, ret = 0;\n BIGNUM *t1, *qadd, *q;\n bits--;\n BN_CTX_start(ctx);\n t1 = BN_CTX_get(ctx);\n q = BN_CTX_get(ctx);\n qadd = BN_CTX_get(ctx);\n if (qadd == NULL)\n goto err;\n if (!BN_rshift1(qadd, padd))\n goto err;\n if (!BN_priv_rand(q, bits, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ODD))\n goto err;\n if (!BN_mod(t1, q, qadd, ctx))\n goto err;\n if (!BN_sub(q, q, t1))\n goto err;\n if (rem == NULL) {\n if (!BN_add_word(q, 1))\n goto err;\n } else {\n if (!BN_rshift1(t1, rem))\n goto err;\n if (!BN_add(q, q, t1))\n goto err;\n }\n if (!BN_lshift1(p, q))\n goto err;\n if (!BN_add_word(p, 1))\n goto err;\n loop:\n for (i = 1; i < NUMPRIMES; i++) {\n BN_ULONG pmod = BN_mod_word(p, (BN_ULONG)primes[i]);\n BN_ULONG qmod = BN_mod_word(q, (BN_ULONG)primes[i]);\n if (pmod == (BN_ULONG)-1 || qmod == (BN_ULONG)-1)\n goto err;\n if (pmod == 0 || qmod == 0) {\n if (!BN_add(p, p, padd))\n goto err;\n if (!BN_add(q, q, qadd))\n goto err;\n goto loop;\n }\n }\n ret = 1;\n err:\n BN_CTX_end(ctx);\n bn_check_top(p);\n return ret;\n}', 'int BN_lshift1(BIGNUM *r, const BIGNUM *a)\n{\n register BN_ULONG *ap, *rp, t, c;\n int i;\n bn_check_top(r);\n bn_check_top(a);\n if (r != a) {\n r->neg = a->neg;\n if (bn_wexpand(r, a->top + 1) == NULL)\n return 0;\n r->top = a->top;\n } else {\n if (bn_wexpand(r, a->top + 1) == NULL)\n return 0;\n }\n ap = a->d;\n rp = r->d;\n c = 0;\n for (i = 0; i < a->top; i++) {\n t = *(ap++);\n *(rp++) = ((t << 1) | c) & BN_MASK2;\n c = (t & BN_TBIT) ? 1 : 0;\n }\n if (c) {\n *rp = 1;\n r->top++;\n }\n bn_check_top(r);\n return 1;\n}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n return (words <= a->dmax) ? a : bn_expand2(a, words);\n}', 'BIGNUM *bn_expand2(BIGNUM *b, int words)\n{\n bn_check_top(b);\n if (words > b->dmax) {\n BN_ULONG *a = bn_expand_internal(b, words);\n if (!a)\n return NULL;\n if (b->d) {\n OPENSSL_cleanse(b->d, b->dmax * sizeof(b->d[0]));\n bn_free_d(b);\n }\n b->d = a;\n b->dmax = words;\n }\n bn_check_top(b);\n return b;\n}', 'static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)\n{\n BN_ULONG *a = NULL;\n bn_check_top(b);\n if (words > (INT_MAX / (4 * BN_BITS2))) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);\n return NULL;\n }\n if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);\n return NULL;\n }\n if (BN_get_flags(b, BN_FLG_SECURE))\n a = OPENSSL_secure_zalloc(words * sizeof(*a));\n else\n a = OPENSSL_zalloc(words * sizeof(*a));\n if (a == NULL) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);\n return NULL;\n }\n assert(b->top <= words);\n if (b->top > 0)\n memcpy(a, b->d, sizeof(*a) * b->top);\n return a;\n}', 'void *CRYPTO_zalloc(size_t num, const char *file, int line)\n{\n void *ret = CRYPTO_malloc(num, file, line);\n FAILTEST();\n if (ret != NULL)\n memset(ret, 0, num);\n return ret;\n}', 'void *CRYPTO_malloc(size_t num, const char *file, int line)\n{\n void *ret = NULL;\n INCREMENT(malloc_count);\n if (malloc_impl != NULL && malloc_impl != CRYPTO_malloc)\n return malloc_impl(num, file, line);\n if (num == 0)\n return NULL;\n FAILTEST();\n allow_customize = 0;\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n if (call_malloc_debug) {\n CRYPTO_mem_debug_malloc(NULL, num, 0, file, line);\n ret = malloc(num);\n CRYPTO_mem_debug_malloc(ret, num, 1, file, line);\n } else {\n ret = malloc(num);\n }\n#else\n (void)(file); (void)(line);\n ret = malloc(num);\n#endif\n return ret;\n}']
867
0
https://github.com/openssl/openssl/blob/01b7851aa27aa144372f5484da916be042d9aa4f/ssl/packet_locl.h/#L82
static inline void packet_forward(PACKET *pkt, size_t len) { pkt->curr += len; pkt->remaining -= len; }
['static int test_PACKET_get_net_3(unsigned char buf[BUF_LEN])\n{\n unsigned long i;\n PACKET pkt;\n if ( !PACKET_buf_init(&pkt, buf, BUF_LEN)\n || !PACKET_get_net_3(&pkt, &i)\n || i != 0x020406UL\n || !PACKET_forward(&pkt, BUF_LEN - 6)\n || !PACKET_get_net_3(&pkt, &i)\n || i != 0xfafcfeUL\n || PACKET_get_net_3(&pkt, &i)) {\n fprintf(stderr, "test_PACKET_get_net_3() failed\\n");\n return 0;\n }\n return 1;\n}', 'static inline void packet_forward(PACKET *pkt, size_t len)\n{\n pkt->curr += len;\n pkt->remaining -= len;\n}']
868
0
https://github.com/openssl/openssl/blob/1fac96e4d6484a517f2ebe99b72016726391723c/crypto/bn/bn_mul.c/#L641
int BN_mul(BIGNUM *r, BIGNUM *a, BIGNUM *b, BN_CTX *ctx) { int top,al,bl; BIGNUM *rr; #ifdef BN_RECURSION BIGNUM *t; int i,j,k; #endif #ifdef BN_COUNT printf("BN_mul %d * %d\n",a->top,b->top); #endif bn_check_top(a); bn_check_top(b); bn_check_top(r); al=a->top; bl=b->top; r->neg=a->neg^b->neg; if ((al == 0) || (bl == 0)) { BN_zero(r); return(1); } top=al+bl; if ((r == a) || (r == b)) rr= &(ctx->bn[ctx->tos+1]); else rr=r; #if defined(BN_MUL_COMBA) || defined(BN_RECURSION) if (al == bl) { # ifdef BN_MUL_COMBA if (al == 8) { if (bn_wexpand(rr,16) == NULL) return(0); r->top=16; bn_mul_comba8(rr->d,a->d,b->d); goto end; } else # endif #ifdef BN_RECURSION if (al < BN_MULL_SIZE_NORMAL) #endif { if (bn_wexpand(rr,top) == NULL) return(0); rr->top=top; bn_mul_normal(rr->d,a->d,al,b->d,bl); goto end; } # ifdef BN_RECURSION goto symetric; # endif } #endif #ifdef BN_RECURSION else if ((al < BN_MULL_SIZE_NORMAL) || (bl < BN_MULL_SIZE_NORMAL)) { if (bn_wexpand(rr,top) == NULL) return(0); rr->top=top; bn_mul_normal(rr->d,a->d,al,b->d,bl); goto end; } else { i=(al-bl); if ((i == 1) && !BN_get_flags(b,BN_FLG_STATIC_DATA)) { bn_wexpand(b,al); b->d[bl]=0; bl++; goto symetric; } else if ((i == -1) && !BN_get_flags(a,BN_FLG_STATIC_DATA)) { bn_wexpand(a,bl); a->d[al]=0; al++; goto symetric; } } #endif if (bn_wexpand(rr,top) == NULL) return(0); rr->top=top; bn_mul_normal(rr->d,a->d,al,b->d,bl); #ifdef BN_RECURSION if (0) { symetric: j=BN_num_bits_word((BN_ULONG)al); j=1<<(j-1); k=j+j; t= &(ctx->bn[ctx->tos]); if (al == j) { bn_wexpand(t,k*2); bn_wexpand(rr,k*2); bn_mul_recursive(rr->d,a->d,b->d,al,t->d); } else { bn_wexpand(a,k); bn_wexpand(b,k); bn_wexpand(t,k*4); bn_wexpand(rr,k*4); for (i=a->top; i<k; i++) a->d[i]=0; for (i=b->top; i<k; i++) b->d[i]=0; bn_mul_part_recursive(rr->d,a->d,b->d,al-j,j,t->d); } rr->top=top; } #endif #if defined(BN_MUL_COMBA) || defined(BN_RECURSION) end: #endif bn_fix_top(rr); if (r != rr) BN_copy(r,rr); return(1); }
['int test_mul(BIO *bp)\n\t{\n\tBIGNUM a,b,c,d,e;\n\tint i;\n\tint j;\n\tBN_CTX ctx;\n\tBN_CTX_init(&ctx);\n\tBN_init(&a);\n\tBN_init(&b);\n\tBN_init(&c);\n\tBN_init(&d);\n\tBN_init(&e);\n\tBN_rand(&a,200,0,0);\n\tfor (i=0; i<100; i++)\n\t\t{\n\t\tBN_rand(&b,250+i,0,0);\n\t\tBN_rand(&b,200,0,0);\n\t\ta.neg=rand_neg();\n\t\tb.neg=rand_neg();\n\t\tif (bp == NULL)\n\t\t\tfor (j=0; j<100; j++)\n\t\t\t\tBN_mul(&c,&a,&b,&ctx);\n\t\tBN_mul(&c,&a,&b,&ctx);\n\t\tif (bp != NULL)\n\t\t\t{\n\t\t\tif (!results)\n\t\t\t\t{\n\t\t\t\tBN_print(bp,&a);\n\t\t\t\tBIO_puts(bp," * ");\n\t\t\t\tBN_print(bp,&b);\n\t\t\t\tBIO_puts(bp," - ");\n\t\t\t\t}\n\t\t\tBN_print(bp,&c);\n\t\t\tBIO_puts(bp,"\\n");\n\t\t\t}\n\t\tBN_div(&d,&e,&c,&a,&ctx);\n\t\tBN_sub(&d,&d,&b);\n\t\tif(!BN_is_zero(&d) || !BN_is_zero(&e))\n\t\t {\n\t\t BIO_puts(bp,"Multiplication test failed!\\n");\n\t\t return 0;\n\t\t }\n\t\t}\n\tBN_free(&a);\n\tBN_free(&b);\n\tBN_free(&c);\n\tBN_free(&d);\n\tBN_free(&e);\n\tBN_CTX_free(&ctx);\n\treturn(1);\n\t}', 'int BN_rand(BIGNUM *rnd, int bits, int top, int bottom)\n\t{\n\tunsigned char *buf=NULL;\n\tint ret=0,bit,bytes,mask;\n\ttime_t tim;\n\tbytes=(bits+7)/8;\n\tbit=(bits-1)%8;\n\tmask=0xff<<bit;\n\tbuf=(unsigned char *)Malloc(bytes);\n\tif (buf == NULL)\n\t\t{\n\t\tBNerr(BN_F_BN_RAND,ERR_R_MALLOC_FAILURE);\n\t\tgoto err;\n\t\t}\n\ttime(&tim);\n\tRAND_seed(&tim,sizeof(tim));\n\tRAND_bytes(buf,(int)bytes);\n\tif (top)\n\t\t{\n\t\tif (bit == 0)\n\t\t\t{\n\t\t\tbuf[0]=1;\n\t\t\tbuf[1]|=0x80;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tbuf[0]|=(3<<(bit-1));\n\t\t\tbuf[0]&= ~(mask<<1);\n\t\t\t}\n\t\t}\n\telse\n\t\t{\n\t\tbuf[0]|=(1<<bit);\n\t\tbuf[0]&= ~(mask<<1);\n\t\t}\n\tif (bottom)\n\t\tbuf[bytes-1]|=1;\n\tif (!BN_bin2bn(buf,bytes,rnd)) goto err;\n\tret=1;\nerr:\n\tif (buf != NULL)\n\t\t{\n\t\tmemset(buf,0,bytes);\n\t\tFree(buf);\n\t\t}\n\treturn(ret);\n\t}', 'int BN_mul(BIGNUM *r, BIGNUM *a, BIGNUM *b, BN_CTX *ctx)\n\t{\n\tint top,al,bl;\n\tBIGNUM *rr;\n#ifdef BN_RECURSION\n\tBIGNUM *t;\n\tint i,j,k;\n#endif\n#ifdef BN_COUNT\nprintf("BN_mul %d * %d\\n",a->top,b->top);\n#endif\n\tbn_check_top(a);\n\tbn_check_top(b);\n\tbn_check_top(r);\n\tal=a->top;\n\tbl=b->top;\n\tr->neg=a->neg^b->neg;\n\tif ((al == 0) || (bl == 0))\n\t\t{\n\t\tBN_zero(r);\n\t\treturn(1);\n\t\t}\n\ttop=al+bl;\n\tif ((r == a) || (r == b))\n\t\trr= &(ctx->bn[ctx->tos+1]);\n\telse\n\t\trr=r;\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n\tif (al == bl)\n\t\t{\n# ifdef BN_MUL_COMBA\n if (al == 8)\n\t\t\t{\n\t\t\tif (bn_wexpand(rr,16) == NULL) return(0);\n\t\t\tr->top=16;\n\t\t\tbn_mul_comba8(rr->d,a->d,b->d);\n\t\t\tgoto end;\n\t\t\t}\n\t\telse\n# endif\n#ifdef BN_RECURSION\n\t\tif (al < BN_MULL_SIZE_NORMAL)\n#endif\n\t\t\t{\n\t\t\tif (bn_wexpand(rr,top) == NULL) return(0);\n\t\t\trr->top=top;\n\t\t\tbn_mul_normal(rr->d,a->d,al,b->d,bl);\n\t\t\tgoto end;\n\t\t\t}\n# ifdef BN_RECURSION\n\t\tgoto symetric;\n# endif\n\t\t}\n#endif\n#ifdef BN_RECURSION\n\telse if ((al < BN_MULL_SIZE_NORMAL) || (bl < BN_MULL_SIZE_NORMAL))\n\t\t{\n\t\tif (bn_wexpand(rr,top) == NULL) return(0);\n\t\trr->top=top;\n\t\tbn_mul_normal(rr->d,a->d,al,b->d,bl);\n\t\tgoto end;\n\t\t}\n\telse\n\t\t{\n\t\ti=(al-bl);\n\t\tif ((i == 1) && !BN_get_flags(b,BN_FLG_STATIC_DATA))\n\t\t\t{\n\t\t\tbn_wexpand(b,al);\n\t\t\tb->d[bl]=0;\n\t\t\tbl++;\n\t\t\tgoto symetric;\n\t\t\t}\n\t\telse if ((i == -1) && !BN_get_flags(a,BN_FLG_STATIC_DATA))\n\t\t\t{\n\t\t\tbn_wexpand(a,bl);\n\t\t\ta->d[al]=0;\n\t\t\tal++;\n\t\t\tgoto symetric;\n\t\t\t}\n\t\t}\n#endif\n\tif (bn_wexpand(rr,top) == NULL) return(0);\n\trr->top=top;\n\tbn_mul_normal(rr->d,a->d,al,b->d,bl);\n#ifdef BN_RECURSION\n\tif (0)\n\t\t{\nsymetric:\n\t\tj=BN_num_bits_word((BN_ULONG)al);\n\t\tj=1<<(j-1);\n\t\tk=j+j;\n\t\tt= &(ctx->bn[ctx->tos]);\n\t\tif (al == j)\n\t\t\t{\n\t\t\tbn_wexpand(t,k*2);\n\t\t\tbn_wexpand(rr,k*2);\n\t\t\tbn_mul_recursive(rr->d,a->d,b->d,al,t->d);\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tbn_wexpand(a,k);\n\t\t\tbn_wexpand(b,k);\n\t\t\tbn_wexpand(t,k*4);\n\t\t\tbn_wexpand(rr,k*4);\n\t\t\tfor (i=a->top; i<k; i++)\n\t\t\t\ta->d[i]=0;\n\t\t\tfor (i=b->top; i<k; i++)\n\t\t\t\tb->d[i]=0;\n\t\t\tbn_mul_part_recursive(rr->d,a->d,b->d,al-j,j,t->d);\n\t\t\t}\n\t\trr->top=top;\n\t\t}\n#endif\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\nend:\n#endif\n\tbn_fix_top(rr);\n\tif (r != rr) BN_copy(r,rr);\n\treturn(1);\n\t}']
869
0
https://github.com/libav/libav/blob/8e3d8a82e6eb8ef37daecddf651fe6cdadaab7e8/libavcodec/mpc8.c/#L110
static av_cold int mpc8_decode_init(AVCodecContext * avctx) { int i; MPCContext *c = avctx->priv_data; GetBitContext gb; static int vlc_initialized = 0; if(avctx->extradata_size < 2){ av_log(avctx, AV_LOG_ERROR, "Too small extradata size (%i)!\n", avctx->extradata_size); return -1; } memset(c->oldDSCF, 0, sizeof(c->oldDSCF)); av_lfg_init(&c->rnd, 0xDEADBEEF); dsputil_init(&c->dsp, avctx); ff_mpc_init(); init_get_bits(&gb, avctx->extradata, 16); skip_bits(&gb, 3); c->maxbands = get_bits(&gb, 5) + 1; skip_bits(&gb, 4); c->MSS = get_bits1(&gb); c->frames = 1 << (get_bits(&gb, 3) * 2); if(vlc_initialized) return 0; av_log(avctx, AV_LOG_DEBUG, "Initing VLC\n"); init_vlc(&band_vlc, MPC8_BANDS_BITS, MPC8_BANDS_SIZE, mpc8_bands_bits, 1, 1, mpc8_bands_codes, 1, 1, INIT_VLC_USE_STATIC); init_vlc(&q1_vlc, MPC8_Q1_BITS, MPC8_Q1_SIZE, mpc8_q1_bits, 1, 1, mpc8_q1_codes, 1, 1, INIT_VLC_USE_STATIC); init_vlc(&q9up_vlc, MPC8_Q9UP_BITS, MPC8_Q9UP_SIZE, mpc8_q9up_bits, 1, 1, mpc8_q9up_codes, 1, 1, INIT_VLC_USE_STATIC); init_vlc(&scfi_vlc[0], MPC8_SCFI0_BITS, MPC8_SCFI0_SIZE, mpc8_scfi0_bits, 1, 1, mpc8_scfi0_codes, 1, 1, INIT_VLC_USE_STATIC); init_vlc(&scfi_vlc[1], MPC8_SCFI1_BITS, MPC8_SCFI1_SIZE, mpc8_scfi1_bits, 1, 1, mpc8_scfi1_codes, 1, 1, INIT_VLC_USE_STATIC); init_vlc(&dscf_vlc[0], MPC8_DSCF0_BITS, MPC8_DSCF0_SIZE, mpc8_dscf0_bits, 1, 1, mpc8_dscf0_codes, 1, 1, INIT_VLC_USE_STATIC); init_vlc(&dscf_vlc[1], MPC8_DSCF1_BITS, MPC8_DSCF1_SIZE, mpc8_dscf1_bits, 1, 1, mpc8_dscf1_codes, 1, 1, INIT_VLC_USE_STATIC); init_vlc_sparse(&q3_vlc[0], MPC8_Q3_BITS, MPC8_Q3_SIZE, mpc8_q3_bits, 1, 1, mpc8_q3_codes, 1, 1, mpc8_q3_syms, 1, 1, INIT_VLC_USE_STATIC); init_vlc_sparse(&q3_vlc[1], MPC8_Q4_BITS, MPC8_Q4_SIZE, mpc8_q4_bits, 1, 1, mpc8_q4_codes, 1, 1, mpc8_q4_syms, 1, 1, INIT_VLC_USE_STATIC); for(i = 0; i < 2; i++){ init_vlc(&res_vlc[i], MPC8_RES_BITS, MPC8_RES_SIZE, &mpc8_res_bits[i], 1, 1, &mpc8_res_codes[i], 1, 1, INIT_VLC_USE_STATIC); init_vlc(&q2_vlc[i], MPC8_Q2_BITS, MPC8_Q2_SIZE, &mpc8_q2_bits[i], 1, 1, &mpc8_q2_codes[i], 1, 1, INIT_VLC_USE_STATIC); init_vlc(&quant_vlc[0][i], MPC8_Q5_BITS, MPC8_Q5_SIZE, &mpc8_q5_bits[i], 1, 1, &mpc8_q5_codes[i], 1, 1, INIT_VLC_USE_STATIC); init_vlc(&quant_vlc[1][i], MPC8_Q6_BITS, MPC8_Q6_SIZE, &mpc8_q6_bits[i], 1, 1, &mpc8_q6_codes[i], 1, 1, INIT_VLC_USE_STATIC); init_vlc(&quant_vlc[2][i], MPC8_Q7_BITS, MPC8_Q7_SIZE, &mpc8_q7_bits[i], 1, 1, &mpc8_q7_codes[i], 1, 1, INIT_VLC_USE_STATIC); init_vlc(&quant_vlc[3][i], MPC8_Q8_BITS, MPC8_Q8_SIZE, &mpc8_q8_bits[i], 1, 1, &mpc8_q8_codes[i], 1, 1, INIT_VLC_USE_STATIC); } vlc_initialized = 1; avctx->sample_fmt = SAMPLE_FMT_S16; avctx->channel_layout = (avctx->channels==2) ? CH_LAYOUT_STEREO : CH_LAYOUT_MONO; return 0; }
['static av_cold int mpc8_decode_init(AVCodecContext * avctx)\n{\n int i;\n MPCContext *c = avctx->priv_data;\n GetBitContext gb;\n static int vlc_initialized = 0;\n if(avctx->extradata_size < 2){\n av_log(avctx, AV_LOG_ERROR, "Too small extradata size (%i)!\\n", avctx->extradata_size);\n return -1;\n }\n memset(c->oldDSCF, 0, sizeof(c->oldDSCF));\n av_lfg_init(&c->rnd, 0xDEADBEEF);\n dsputil_init(&c->dsp, avctx);\n ff_mpc_init();\n init_get_bits(&gb, avctx->extradata, 16);\n skip_bits(&gb, 3);\n c->maxbands = get_bits(&gb, 5) + 1;\n skip_bits(&gb, 4);\n c->MSS = get_bits1(&gb);\n c->frames = 1 << (get_bits(&gb, 3) * 2);\n if(vlc_initialized) return 0;\n av_log(avctx, AV_LOG_DEBUG, "Initing VLC\\n");\n init_vlc(&band_vlc, MPC8_BANDS_BITS, MPC8_BANDS_SIZE,\n mpc8_bands_bits, 1, 1,\n mpc8_bands_codes, 1, 1, INIT_VLC_USE_STATIC);\n init_vlc(&q1_vlc, MPC8_Q1_BITS, MPC8_Q1_SIZE,\n mpc8_q1_bits, 1, 1,\n mpc8_q1_codes, 1, 1, INIT_VLC_USE_STATIC);\n init_vlc(&q9up_vlc, MPC8_Q9UP_BITS, MPC8_Q9UP_SIZE,\n mpc8_q9up_bits, 1, 1,\n mpc8_q9up_codes, 1, 1, INIT_VLC_USE_STATIC);\n init_vlc(&scfi_vlc[0], MPC8_SCFI0_BITS, MPC8_SCFI0_SIZE,\n mpc8_scfi0_bits, 1, 1,\n mpc8_scfi0_codes, 1, 1, INIT_VLC_USE_STATIC);\n init_vlc(&scfi_vlc[1], MPC8_SCFI1_BITS, MPC8_SCFI1_SIZE,\n mpc8_scfi1_bits, 1, 1,\n mpc8_scfi1_codes, 1, 1, INIT_VLC_USE_STATIC);\n init_vlc(&dscf_vlc[0], MPC8_DSCF0_BITS, MPC8_DSCF0_SIZE,\n mpc8_dscf0_bits, 1, 1,\n mpc8_dscf0_codes, 1, 1, INIT_VLC_USE_STATIC);\n init_vlc(&dscf_vlc[1], MPC8_DSCF1_BITS, MPC8_DSCF1_SIZE,\n mpc8_dscf1_bits, 1, 1,\n mpc8_dscf1_codes, 1, 1, INIT_VLC_USE_STATIC);\n init_vlc_sparse(&q3_vlc[0], MPC8_Q3_BITS, MPC8_Q3_SIZE,\n mpc8_q3_bits, 1, 1,\n mpc8_q3_codes, 1, 1,\n mpc8_q3_syms, 1, 1, INIT_VLC_USE_STATIC);\n init_vlc_sparse(&q3_vlc[1], MPC8_Q4_BITS, MPC8_Q4_SIZE,\n mpc8_q4_bits, 1, 1,\n mpc8_q4_codes, 1, 1,\n mpc8_q4_syms, 1, 1, INIT_VLC_USE_STATIC);\n for(i = 0; i < 2; i++){\n init_vlc(&res_vlc[i], MPC8_RES_BITS, MPC8_RES_SIZE,\n &mpc8_res_bits[i], 1, 1,\n &mpc8_res_codes[i], 1, 1, INIT_VLC_USE_STATIC);\n init_vlc(&q2_vlc[i], MPC8_Q2_BITS, MPC8_Q2_SIZE,\n &mpc8_q2_bits[i], 1, 1,\n &mpc8_q2_codes[i], 1, 1, INIT_VLC_USE_STATIC);\n init_vlc(&quant_vlc[0][i], MPC8_Q5_BITS, MPC8_Q5_SIZE,\n &mpc8_q5_bits[i], 1, 1,\n &mpc8_q5_codes[i], 1, 1, INIT_VLC_USE_STATIC);\n init_vlc(&quant_vlc[1][i], MPC8_Q6_BITS, MPC8_Q6_SIZE,\n &mpc8_q6_bits[i], 1, 1,\n &mpc8_q6_codes[i], 1, 1, INIT_VLC_USE_STATIC);\n init_vlc(&quant_vlc[2][i], MPC8_Q7_BITS, MPC8_Q7_SIZE,\n &mpc8_q7_bits[i], 1, 1,\n &mpc8_q7_codes[i], 1, 1, INIT_VLC_USE_STATIC);\n init_vlc(&quant_vlc[3][i], MPC8_Q8_BITS, MPC8_Q8_SIZE,\n &mpc8_q8_bits[i], 1, 1,\n &mpc8_q8_codes[i], 1, 1, INIT_VLC_USE_STATIC);\n }\n vlc_initialized = 1;\n avctx->sample_fmt = SAMPLE_FMT_S16;\n avctx->channel_layout = (avctx->channels==2) ? CH_LAYOUT_STEREO : CH_LAYOUT_MONO;\n return 0;\n}', 'void ff_mpc_init(void)\n{\n ff_mpa_synth_init(mpa_window);\n}', 'static inline void init_get_bits(GetBitContext *s,\n const uint8_t *buffer, int bit_size)\n{\n int buffer_size= (bit_size+7)>>3;\n if(buffer_size < 0 || bit_size < 0) {\n buffer_size = bit_size = 0;\n buffer = NULL;\n }\n s->buffer= buffer;\n s->size_in_bits= bit_size;\n s->buffer_end= buffer + buffer_size;\n#ifdef ALT_BITSTREAM_READER\n s->index=0;\n#elif defined LIBMPEG2_BITSTREAM_READER\n s->buffer_ptr = (uint8_t*)((intptr_t)buffer&(~1));\n s->bit_count = 16 + 8*((intptr_t)buffer&1);\n skip_bits_long(s, 0);\n#elif defined A32_BITSTREAM_READER\n s->buffer_ptr = (uint32_t*)((intptr_t)buffer&(~3));\n s->bit_count = 32 + 8*((intptr_t)buffer&3);\n skip_bits_long(s, 0);\n#endif\n}', 'static inline void skip_bits(GetBitContext *s, int n){\n OPEN_READER(re, s)\n UPDATE_CACHE(re, s)\n LAST_SKIP_BITS(re, s, n)\n CLOSE_READER(re, s)\n}']
870
0
https://github.com/libav/libav/blob/cf6bae6883607f83f3b042b7b9d711197f736e2a/libavcodec/vc1.c/#L2538
static int vc1_decode_i_block(VC1Context *v, DCTELEM block[64], int n, int coded, int codingset) { GetBitContext *gb = &v->s.gb; MpegEncContext *s = &v->s; int dc_pred_dir = 0; int i; int16_t *dc_val; int16_t *ac_val, *ac_val2; int dcdiff; if (n < 4) { dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3); } else { dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3); } if (dcdiff < 0){ av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n"); return -1; } if (dcdiff) { if (dcdiff == 119 ) { if (v->pq == 1) dcdiff = get_bits(gb, 10); else if (v->pq == 2) dcdiff = get_bits(gb, 9); else dcdiff = get_bits(gb, 8); } else { if (v->pq == 1) dcdiff = (dcdiff<<2) + get_bits(gb, 2) - 3; else if (v->pq == 2) dcdiff = (dcdiff<<1) + get_bits1(gb) - 1; } if (get_bits1(gb)) dcdiff = -dcdiff; } dcdiff += vc1_i_pred_dc(&v->s, v->overlap, v->pq, n, &dc_val, &dc_pred_dir); *dc_val = dcdiff; if (n < 4) { block[0] = dcdiff * s->y_dc_scale; } else { block[0] = dcdiff * s->c_dc_scale; } if (!coded) { goto not_coded; } i = 1; { int last = 0, skip, value; const int8_t *zz_table; int scale; int k; scale = v->pq * 2 + v->halfpq; if(v->s.ac_pred) { if(!dc_pred_dir) zz_table = wmv1_scantable[2]; else zz_table = wmv1_scantable[3]; } else zz_table = wmv1_scantable[1]; ac_val = s->ac_val[0][0] + s->block_index[n] * 16; ac_val2 = ac_val; if(dc_pred_dir) ac_val -= 16; else ac_val -= 16 * s->block_wrap[n]; while (!last) { vc1_decode_ac_coeff(v, &last, &skip, &value, codingset); i += skip; if(i > 63) break; block[zz_table[i++]] = value; } if(s->ac_pred) { if(dc_pred_dir) { for(k = 1; k < 8; k++) block[k << 3] += ac_val[k]; } else { for(k = 1; k < 8; k++) block[k] += ac_val[k + 8]; } } for(k = 1; k < 8; k++) { ac_val2[k] = block[k << 3]; ac_val2[k + 8] = block[k]; } for(k = 1; k < 64; k++) if(block[k]) { block[k] *= scale; if(!v->pquantizer) block[k] += (block[k] < 0) ? -v->pq : v->pq; } if(s->ac_pred) i = 63; } not_coded: if(!coded) { int k, scale; ac_val = s->ac_val[0][0] + s->block_index[n] * 16; ac_val2 = ac_val; i = 0; scale = v->pq * 2 + v->halfpq; memset(ac_val2, 0, 16 * 2); if(dc_pred_dir) { ac_val -= 16; if(s->ac_pred) memcpy(ac_val2, ac_val, 8 * 2); } else { ac_val -= 16 * s->block_wrap[n]; if(s->ac_pred) memcpy(ac_val2 + 8, ac_val + 8, 8 * 2); } if(s->ac_pred) { if(dc_pred_dir) { for(k = 1; k < 8; k++) { block[k << 3] = ac_val[k] * scale; if(!v->pquantizer && block[k << 3]) block[k << 3] += (block[k << 3] < 0) ? -v->pq : v->pq; } } else { for(k = 1; k < 8; k++) { block[k] = ac_val[k + 8] * scale; if(!v->pquantizer && block[k]) block[k] += (block[k] < 0) ? -v->pq : v->pq; } } i = 63; } } s->block_last_index[n] = i; return 0; }
['static int vc1_decode_i_block(VC1Context *v, DCTELEM block[64], int n, int coded, int codingset)\n{\n GetBitContext *gb = &v->s.gb;\n MpegEncContext *s = &v->s;\n int dc_pred_dir = 0;\n int i;\n int16_t *dc_val;\n int16_t *ac_val, *ac_val2;\n int dcdiff;\n if (n < 4) {\n dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);\n } else {\n dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);\n }\n if (dcdiff < 0){\n av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\\n");\n return -1;\n }\n if (dcdiff)\n {\n if (dcdiff == 119 )\n {\n if (v->pq == 1) dcdiff = get_bits(gb, 10);\n else if (v->pq == 2) dcdiff = get_bits(gb, 9);\n else dcdiff = get_bits(gb, 8);\n }\n else\n {\n if (v->pq == 1)\n dcdiff = (dcdiff<<2) + get_bits(gb, 2) - 3;\n else if (v->pq == 2)\n dcdiff = (dcdiff<<1) + get_bits1(gb) - 1;\n }\n if (get_bits1(gb))\n dcdiff = -dcdiff;\n }\n dcdiff += vc1_i_pred_dc(&v->s, v->overlap, v->pq, n, &dc_val, &dc_pred_dir);\n *dc_val = dcdiff;\n if (n < 4) {\n block[0] = dcdiff * s->y_dc_scale;\n } else {\n block[0] = dcdiff * s->c_dc_scale;\n }\n if (!coded) {\n goto not_coded;\n }\n i = 1;\n {\n int last = 0, skip, value;\n const int8_t *zz_table;\n int scale;\n int k;\n scale = v->pq * 2 + v->halfpq;\n if(v->s.ac_pred) {\n if(!dc_pred_dir)\n zz_table = wmv1_scantable[2];\n else\n zz_table = wmv1_scantable[3];\n } else\n zz_table = wmv1_scantable[1];\n ac_val = s->ac_val[0][0] + s->block_index[n] * 16;\n ac_val2 = ac_val;\n if(dc_pred_dir)\n ac_val -= 16;\n else\n ac_val -= 16 * s->block_wrap[n];\n while (!last) {\n vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);\n i += skip;\n if(i > 63)\n break;\n block[zz_table[i++]] = value;\n }\n if(s->ac_pred) {\n if(dc_pred_dir) {\n for(k = 1; k < 8; k++)\n block[k << 3] += ac_val[k];\n } else {\n for(k = 1; k < 8; k++)\n block[k] += ac_val[k + 8];\n }\n }\n for(k = 1; k < 8; k++) {\n ac_val2[k] = block[k << 3];\n ac_val2[k + 8] = block[k];\n }\n for(k = 1; k < 64; k++)\n if(block[k]) {\n block[k] *= scale;\n if(!v->pquantizer)\n block[k] += (block[k] < 0) ? -v->pq : v->pq;\n }\n if(s->ac_pred) i = 63;\n }\nnot_coded:\n if(!coded) {\n int k, scale;\n ac_val = s->ac_val[0][0] + s->block_index[n] * 16;\n ac_val2 = ac_val;\n i = 0;\n scale = v->pq * 2 + v->halfpq;\n memset(ac_val2, 0, 16 * 2);\n if(dc_pred_dir) {\n ac_val -= 16;\n if(s->ac_pred)\n memcpy(ac_val2, ac_val, 8 * 2);\n } else {\n ac_val -= 16 * s->block_wrap[n];\n if(s->ac_pred)\n memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);\n }\n if(s->ac_pred) {\n if(dc_pred_dir) {\n for(k = 1; k < 8; k++) {\n block[k << 3] = ac_val[k] * scale;\n if(!v->pquantizer && block[k << 3])\n block[k << 3] += (block[k << 3] < 0) ? -v->pq : v->pq;\n }\n } else {\n for(k = 1; k < 8; k++) {\n block[k] = ac_val[k + 8] * scale;\n if(!v->pquantizer && block[k])\n block[k] += (block[k] < 0) ? -v->pq : v->pq;\n }\n }\n i = 63;\n }\n }\n s->block_last_index[n] = i;\n return 0;\n}']
871
0
https://github.com/openssl/openssl/blob/2eb2b4f3a12d0b8807447913a3b16f21104c701b/crypto/evp/evp_enc.c/#L292
int is_partially_overlapping(const void *ptr1, const void *ptr2, int len) { PTRDIFF_T diff = (PTRDIFF_T)ptr1-(PTRDIFF_T)ptr2; int overlapped = (len > 0) & (diff != 0) & ((diff < (PTRDIFF_T)len) | (diff > (0 - (PTRDIFF_T)len))); return overlapped; }
['static int test_EVP_Enveloped(void)\n{\n int ret = 0;\n EVP_CIPHER_CTX *ctx = NULL;\n EVP_PKEY *keypair = NULL;\n unsigned char *kek = NULL;\n unsigned char iv[EVP_MAX_IV_LENGTH];\n static const unsigned char msg[] = { 1, 2, 3, 4, 5, 6, 7, 8 };\n int len, kek_len, ciphertext_len, plaintext_len;\n unsigned char ciphertext[32], plaintext[16];\n const EVP_CIPHER *type = EVP_aes_256_cbc();\n if (!TEST_ptr(keypair = load_example_rsa_key())\n || !TEST_ptr(kek = OPENSSL_zalloc(EVP_PKEY_size(keypair)))\n || !TEST_ptr(ctx = EVP_CIPHER_CTX_new())\n || !TEST_true(EVP_SealInit(ctx, type, &kek, &kek_len, iv,\n &keypair, 1))\n || !TEST_true(EVP_SealUpdate(ctx, ciphertext, &ciphertext_len,\n msg, sizeof(msg)))\n || !TEST_true(EVP_SealFinal(ctx, ciphertext + ciphertext_len,\n &len)))\n goto err;\n ciphertext_len += len;\n if (!TEST_true(EVP_OpenInit(ctx, type, kek, kek_len, iv, keypair))\n || !TEST_true(EVP_OpenUpdate(ctx, plaintext, &plaintext_len,\n ciphertext, ciphertext_len))\n || !TEST_true(EVP_OpenFinal(ctx, plaintext + plaintext_len, &len)))\n goto err;\n plaintext_len += len;\n if (!TEST_mem_eq(msg, sizeof(msg), plaintext, plaintext_len))\n goto err;\n ret = 1;\nerr:\n OPENSSL_free(kek);\n EVP_PKEY_free(keypair);\n EVP_CIPHER_CTX_free(ctx);\n return ret;\n}', 'int EVP_DecryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,\n const unsigned char *in, int inl)\n{\n int fix_len, cmpl = inl;\n unsigned int b;\n b = ctx->cipher->block_size;\n if (EVP_CIPHER_CTX_test_flags(ctx, EVP_CIPH_FLAG_LENGTH_BITS))\n cmpl = (cmpl + 7) / 8;\n if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {\n if (b == 1 && is_partially_overlapping(out, in, cmpl)) {\n EVPerr(EVP_F_EVP_DECRYPTUPDATE, EVP_R_PARTIALLY_OVERLAPPING);\n return 0;\n }\n fix_len = ctx->cipher->do_cipher(ctx, out, in, inl);\n if (fix_len < 0) {\n *outl = 0;\n return 0;\n } else\n *outl = fix_len;\n return 1;\n }\n if (inl <= 0) {\n *outl = 0;\n return inl == 0;\n }\n if (ctx->flags & EVP_CIPH_NO_PADDING)\n return EVP_EncryptUpdate(ctx, out, outl, in, inl);\n OPENSSL_assert(b <= sizeof(ctx->final));\n if (ctx->final_used) {\n if (((PTRDIFF_T)out == (PTRDIFF_T)in)\n || is_partially_overlapping(out, in, b)) {\n EVPerr(EVP_F_EVP_DECRYPTUPDATE, EVP_R_PARTIALLY_OVERLAPPING);\n return 0;\n }\n memcpy(out, ctx->final, b);\n out += b;\n fix_len = 1;\n } else\n fix_len = 0;\n if (!EVP_EncryptUpdate(ctx, out, outl, in, inl))\n return 0;\n if (b > 1 && !ctx->buf_len) {\n *outl -= b;\n ctx->final_used = 1;\n memcpy(ctx->final, &out[*outl], b);\n } else\n ctx->final_used = 0;\n if (fix_len)\n *outl += b;\n return 1;\n}', 'int is_partially_overlapping(const void *ptr1, const void *ptr2, int len)\n{\n PTRDIFF_T diff = (PTRDIFF_T)ptr1-(PTRDIFF_T)ptr2;\n int overlapped = (len > 0) & (diff != 0) & ((diff < (PTRDIFF_T)len) |\n (diff > (0 - (PTRDIFF_T)len)));\n return overlapped;\n}']
872
0
https://github.com/openssl/openssl/blob/55442b8a5b719f54578083fae0fcc814b599cd84/crypto/bn/bn_lib.c/#L295
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->top = b->top; a->neg = b->neg; bn_check_top(a); return a; }
['int RSA_check_key_ex(const RSA *key, BN_GENCB *cb)\n{\n BIGNUM *i, *j, *k, *l, *m;\n BN_CTX *ctx;\n int ret = 1, ex_primes = 0, idx;\n RSA_PRIME_INFO *pinfo;\n if (key->p == NULL || key->q == NULL || key->n == NULL\n || key->e == NULL || key->d == NULL) {\n RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_VALUE_MISSING);\n return 0;\n }\n if (key->version == RSA_ASN1_VERSION_MULTI) {\n ex_primes = sk_RSA_PRIME_INFO_num(key->prime_infos);\n if (ex_primes <= 0\n || (ex_primes + 2) > rsa_multip_cap(BN_num_bits(key->n))) {\n RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_INVALID_MULTI_PRIME_KEY);\n return 0;\n }\n }\n i = BN_new();\n j = BN_new();\n k = BN_new();\n l = BN_new();\n m = BN_new();\n ctx = BN_CTX_new();\n if (i == NULL || j == NULL || k == NULL || l == NULL\n || m == NULL || ctx == NULL) {\n ret = -1;\n RSAerr(RSA_F_RSA_CHECK_KEY_EX, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n if (BN_is_one(key->e)) {\n ret = 0;\n RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_BAD_E_VALUE);\n }\n if (!BN_is_odd(key->e)) {\n ret = 0;\n RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_BAD_E_VALUE);\n }\n if (BN_is_prime_ex(key->p, BN_prime_checks, NULL, cb) != 1) {\n ret = 0;\n RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_P_NOT_PRIME);\n }\n if (BN_is_prime_ex(key->q, BN_prime_checks, NULL, cb) != 1) {\n ret = 0;\n RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_Q_NOT_PRIME);\n }\n for (idx = 0; idx < ex_primes; idx++) {\n pinfo = sk_RSA_PRIME_INFO_value(key->prime_infos, idx);\n if (BN_is_prime_ex(pinfo->r, BN_prime_checks, NULL, cb) != 1) {\n ret = 0;\n RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_MP_R_NOT_PRIME);\n }\n }\n if (!BN_mul(i, key->p, key->q, ctx)) {\n ret = -1;\n goto err;\n }\n for (idx = 0; idx < ex_primes; idx++) {\n pinfo = sk_RSA_PRIME_INFO_value(key->prime_infos, idx);\n if (!BN_mul(i, i, pinfo->r, ctx)) {\n ret = -1;\n goto err;\n }\n }\n if (BN_cmp(i, key->n) != 0) {\n ret = 0;\n if (ex_primes)\n RSAerr(RSA_F_RSA_CHECK_KEY_EX,\n RSA_R_N_DOES_NOT_EQUAL_PRODUCT_OF_PRIMES);\n else\n RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_N_DOES_NOT_EQUAL_P_Q);\n }\n if (!BN_sub(i, key->p, BN_value_one())) {\n ret = -1;\n goto err;\n }\n if (!BN_sub(j, key->q, BN_value_one())) {\n ret = -1;\n goto err;\n }\n if (!BN_mul(l, i, j, ctx)) {\n ret = -1;\n goto err;\n }\n if (!BN_gcd(m, i, j, ctx)) {\n ret = -1;\n goto err;\n }\n for (idx = 0; idx < ex_primes; idx++) {\n pinfo = sk_RSA_PRIME_INFO_value(key->prime_infos, idx);\n if (!BN_sub(k, pinfo->r, BN_value_one())) {\n ret = -1;\n goto err;\n }\n if (!BN_mul(l, l, k, ctx)) {\n ret = -1;\n goto err;\n }\n if (!BN_gcd(m, m, k, ctx)) {\n ret = -1;\n goto err;\n }\n }\n if (!BN_div(k, NULL, l, m, ctx)) {\n ret = -1;\n goto err;\n }\n if (!BN_mod_mul(i, key->d, key->e, k, ctx)) {\n ret = -1;\n goto err;\n }\n if (!BN_is_one(i)) {\n ret = 0;\n RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_D_E_NOT_CONGRUENT_TO_1);\n }\n if (key->dmp1 != NULL && key->dmq1 != NULL && key->iqmp != NULL) {\n if (!BN_sub(i, key->p, BN_value_one())) {\n ret = -1;\n goto err;\n }\n if (!BN_mod(j, key->d, i, ctx)) {\n ret = -1;\n goto err;\n }\n if (BN_cmp(j, key->dmp1) != 0) {\n ret = 0;\n RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_DMP1_NOT_CONGRUENT_TO_D);\n }\n if (!BN_sub(i, key->q, BN_value_one())) {\n ret = -1;\n goto err;\n }\n if (!BN_mod(j, key->d, i, ctx)) {\n ret = -1;\n goto err;\n }\n if (BN_cmp(j, key->dmq1) != 0) {\n ret = 0;\n RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_DMQ1_NOT_CONGRUENT_TO_D);\n }\n if (!BN_mod_inverse(i, key->q, key->p, ctx)) {\n ret = -1;\n goto err;\n }\n if (BN_cmp(i, key->iqmp) != 0) {\n ret = 0;\n RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_IQMP_NOT_INVERSE_OF_Q);\n }\n }\n for (idx = 0; idx < ex_primes; idx++) {\n pinfo = sk_RSA_PRIME_INFO_value(key->prime_infos, idx);\n if (!BN_sub(i, pinfo->r, BN_value_one())) {\n ret = -1;\n goto err;\n }\n if (!BN_mod(j, key->d, i, ctx)) {\n ret = -1;\n goto err;\n }\n if (BN_cmp(j, pinfo->d) != 0) {\n ret = 0;\n RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_MP_EXPONENT_NOT_CONGRUENT_TO_D);\n }\n if (!BN_mod_inverse(i, pinfo->pp, pinfo->r, ctx)) {\n ret = -1;\n goto err;\n }\n if (BN_cmp(i, pinfo->t) != 0) {\n ret = 0;\n RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_MP_COEFFICIENT_NOT_INVERSE_OF_R);\n }\n }\n err:\n BN_free(i);\n BN_free(j);\n BN_free(k);\n BN_free(l);\n BN_free(m);\n BN_CTX_free(ctx);\n return ret;\n}', 'int BN_is_prime_ex(const BIGNUM *a, int checks, BN_CTX *ctx_passed,\n BN_GENCB *cb)\n{\n return BN_is_prime_fasttest_ex(a, checks, ctx_passed, 0, cb);\n}', 'int BN_is_prime_fasttest_ex(const BIGNUM *a, int checks, BN_CTX *ctx_passed,\n int do_trial_division, BN_GENCB *cb)\n{\n int i, j, ret = -1;\n int k;\n BN_CTX *ctx = NULL;\n BIGNUM *A1, *A1_odd, *check;\n BN_MONT_CTX *mont = NULL;\n if (BN_cmp(a, BN_value_one()) <= 0)\n return 0;\n if (checks == BN_prime_checks)\n checks = BN_prime_checks_for_size(BN_num_bits(a));\n if (!BN_is_odd(a))\n return BN_is_word(a, 2);\n if (do_trial_division) {\n for (i = 1; i < NUMPRIMES; i++) {\n BN_ULONG mod = BN_mod_word(a, primes[i]);\n if (mod == (BN_ULONG)-1)\n goto err;\n if (mod == 0)\n return BN_is_word(a, primes[i]);\n }\n if (!BN_GENCB_call(cb, 1, -1))\n goto err;\n }\n if (ctx_passed != NULL)\n ctx = ctx_passed;\n else if ((ctx = BN_CTX_new()) == NULL)\n goto err;\n BN_CTX_start(ctx);\n A1 = BN_CTX_get(ctx);\n A1_odd = BN_CTX_get(ctx);\n check = BN_CTX_get(ctx);\n if (check == NULL)\n goto err;\n if (!BN_copy(A1, a))\n goto err;\n if (!BN_sub_word(A1, 1))\n goto err;\n if (BN_is_zero(A1)) {\n ret = 0;\n goto err;\n }\n k = 1;\n while (!BN_is_bit_set(A1, k))\n k++;\n if (!BN_rshift(A1_odd, A1, k))\n goto err;\n mont = BN_MONT_CTX_new();\n if (mont == NULL)\n goto err;\n if (!BN_MONT_CTX_set(mont, a, ctx))\n goto err;\n for (i = 0; i < checks; i++) {\n if (!BN_priv_rand_range(check, A1))\n goto err;\n if (!BN_add_word(check, 1))\n goto err;\n j = witness(check, a, A1, A1_odd, k, ctx, mont);\n if (j == -1)\n goto err;\n if (j) {\n ret = 0;\n goto err;\n }\n if (!BN_GENCB_call(cb, 1, i))\n goto err;\n }\n ret = 1;\n err:\n if (ctx != NULL) {\n BN_CTX_end(ctx);\n if (ctx_passed == NULL)\n BN_CTX_free(ctx);\n }\n BN_MONT_CTX_free(mont);\n return ret;\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_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)\n{\n int ret, r_neg, cmp_res;\n bn_check_top(a);\n bn_check_top(b);\n if (a->neg != b->neg) {\n r_neg = a->neg;\n ret = BN_uadd(r, a, b);\n } else {\n cmp_res = BN_ucmp(a, b);\n if (cmp_res > 0) {\n r_neg = a->neg;\n ret = BN_usub(r, a, b);\n } else if (cmp_res < 0) {\n r_neg = !b->neg;\n ret = BN_usub(r, b, a);\n } else {\n r_neg = 0;\n BN_zero(r);\n ret = 1;\n }\n }\n r->neg = r_neg;\n bn_check_top(r);\n return ret;\n}', 'BIGNUM *BN_mod_inverse(BIGNUM *in,\n const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx)\n{\n BIGNUM *rv;\n int noinv;\n rv = int_bn_mod_inverse(in, a, n, ctx, &noinv);\n if (noinv)\n BNerr(BN_F_BN_MOD_INVERSE, BN_R_NO_INVERSE);\n return rv;\n}', 'BIGNUM *int_bn_mod_inverse(BIGNUM *in,\n const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx,\n int *pnoinv)\n{\n BIGNUM *A, *B, *X, *Y, *M, *D, *T, *R = NULL;\n BIGNUM *ret = NULL;\n int sign;\n if (pnoinv)\n *pnoinv = 0;\n if ((BN_get_flags(a, BN_FLG_CONSTTIME) != 0)\n || (BN_get_flags(n, BN_FLG_CONSTTIME) != 0)) {\n return BN_mod_inverse_no_branch(in, a, n, ctx);\n }\n bn_check_top(a);\n bn_check_top(n);\n BN_CTX_start(ctx);\n A = BN_CTX_get(ctx);\n B = BN_CTX_get(ctx);\n X = BN_CTX_get(ctx);\n D = BN_CTX_get(ctx);\n M = BN_CTX_get(ctx);\n Y = BN_CTX_get(ctx);\n T = BN_CTX_get(ctx);\n if (T == NULL)\n goto err;\n if (in == NULL)\n R = BN_new();\n else\n R = in;\n if (R == NULL)\n goto err;\n BN_one(X);\n BN_zero(Y);\n if (BN_copy(B, a) == NULL)\n goto err;\n if (BN_copy(A, n) == NULL)\n goto err;\n A->neg = 0;\n if (B->neg || (BN_ucmp(B, A) >= 0)) {\n if (!BN_nnmod(B, B, A, ctx))\n goto err;\n }\n sign = -1;\n if (BN_is_odd(n) && (BN_num_bits(n) <= 2048)) {\n int shift;\n while (!BN_is_zero(B)) {\n shift = 0;\n while (!BN_is_bit_set(B, shift)) {\n shift++;\n if (BN_is_odd(X)) {\n if (!BN_uadd(X, X, n))\n goto err;\n }\n if (!BN_rshift1(X, X))\n goto err;\n }\n if (shift > 0) {\n if (!BN_rshift(B, B, shift))\n goto err;\n }\n shift = 0;\n while (!BN_is_bit_set(A, shift)) {\n shift++;\n if (BN_is_odd(Y)) {\n if (!BN_uadd(Y, Y, n))\n goto err;\n }\n if (!BN_rshift1(Y, Y))\n goto err;\n }\n if (shift > 0) {\n if (!BN_rshift(A, A, shift))\n goto err;\n }\n if (BN_ucmp(B, A) >= 0) {\n if (!BN_uadd(X, X, Y))\n goto err;\n if (!BN_usub(B, B, A))\n goto err;\n } else {\n if (!BN_uadd(Y, Y, X))\n goto err;\n if (!BN_usub(A, A, B))\n goto err;\n }\n }\n } else {\n while (!BN_is_zero(B)) {\n BIGNUM *tmp;\n if (BN_num_bits(A) == BN_num_bits(B)) {\n if (!BN_one(D))\n goto err;\n if (!BN_sub(M, A, B))\n goto err;\n } else if (BN_num_bits(A) == BN_num_bits(B) + 1) {\n if (!BN_lshift1(T, B))\n goto err;\n if (BN_ucmp(A, T) < 0) {\n if (!BN_one(D))\n goto err;\n if (!BN_sub(M, A, B))\n goto err;\n } else {\n if (!BN_sub(M, A, T))\n goto err;\n if (!BN_add(D, T, B))\n goto err;\n if (BN_ucmp(A, D) < 0) {\n if (!BN_set_word(D, 2))\n goto err;\n } else {\n if (!BN_set_word(D, 3))\n goto err;\n if (!BN_sub(M, M, B))\n goto err;\n }\n }\n } else {\n if (!BN_div(D, M, A, B, ctx))\n goto err;\n }\n tmp = A;\n A = B;\n B = M;\n if (BN_is_one(D)) {\n if (!BN_add(tmp, X, Y))\n goto err;\n } else {\n if (BN_is_word(D, 2)) {\n if (!BN_lshift1(tmp, X))\n goto err;\n } else if (BN_is_word(D, 4)) {\n if (!BN_lshift(tmp, X, 2))\n goto err;\n } else if (D->top == 1) {\n if (!BN_copy(tmp, X))\n goto err;\n if (!BN_mul_word(tmp, D->d[0]))\n goto err;\n } else {\n if (!BN_mul(tmp, D, X, ctx))\n goto err;\n }\n if (!BN_add(tmp, tmp, Y))\n goto err;\n }\n M = Y;\n Y = X;\n X = tmp;\n sign = -sign;\n }\n }\n if (sign < 0) {\n if (!BN_sub(Y, n, Y))\n goto err;\n }\n if (BN_is_one(A)) {\n if (!Y->neg && BN_ucmp(Y, n) < 0) {\n if (!BN_copy(R, Y))\n goto err;\n } else {\n if (!BN_nnmod(R, Y, n, ctx))\n goto err;\n }\n } else {\n if (pnoinv)\n *pnoinv = 1;\n goto err;\n }\n ret = R;\n err:\n if ((ret == NULL) && (in == NULL))\n BN_free(R);\n BN_CTX_end(ctx);\n bn_check_top(ret);\n return ret;\n}', 'static BIGNUM *BN_mod_inverse_no_branch(BIGNUM *in,\n const BIGNUM *a, const BIGNUM *n,\n BN_CTX *ctx)\n{\n BIGNUM *A, *B, *X, *Y, *M, *D, *T, *R = NULL;\n BIGNUM *ret = NULL;\n int sign;\n bn_check_top(a);\n bn_check_top(n);\n BN_CTX_start(ctx);\n A = BN_CTX_get(ctx);\n B = BN_CTX_get(ctx);\n X = BN_CTX_get(ctx);\n D = BN_CTX_get(ctx);\n M = BN_CTX_get(ctx);\n Y = BN_CTX_get(ctx);\n T = BN_CTX_get(ctx);\n if (T == NULL)\n goto err;\n if (in == NULL)\n R = BN_new();\n else\n R = in;\n if (R == NULL)\n goto err;\n BN_one(X);\n BN_zero(Y);\n if (BN_copy(B, a) == NULL)\n goto err;\n if (BN_copy(A, n) == NULL)\n goto err;\n A->neg = 0;\n if (B->neg || (BN_ucmp(B, A) >= 0)) {\n {\n BIGNUM local_B;\n bn_init(&local_B);\n BN_with_flags(&local_B, B, BN_FLG_CONSTTIME);\n if (!BN_nnmod(B, &local_B, A, ctx))\n goto err;\n }\n }\n sign = -1;\n while (!BN_is_zero(B)) {\n BIGNUM *tmp;\n {\n BIGNUM local_A;\n bn_init(&local_A);\n BN_with_flags(&local_A, A, BN_FLG_CONSTTIME);\n if (!BN_div(D, M, &local_A, B, ctx))\n goto err;\n }\n tmp = A;\n A = B;\n B = M;\n if (!BN_mul(tmp, D, X, ctx))\n goto err;\n if (!BN_add(tmp, tmp, Y))\n goto err;\n M = Y;\n Y = X;\n X = tmp;\n sign = -sign;\n }\n if (sign < 0) {\n if (!BN_sub(Y, n, Y))\n goto err;\n }\n if (BN_is_one(A)) {\n if (!Y->neg && BN_ucmp(Y, n) < 0) {\n if (!BN_copy(R, Y))\n goto err;\n } else {\n if (!BN_nnmod(R, Y, n, ctx))\n goto err;\n }\n } else {\n BNerr(BN_F_BN_MOD_INVERSE_NO_BRANCH, BN_R_NO_INVERSE);\n goto err;\n }\n ret = R;\n err:\n if ((ret == NULL) && (in == NULL))\n BN_free(R);\n BN_CTX_end(ctx);\n bn_check_top(ret);\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->top = b->top;\n a->neg = b->neg;\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}']
873
0
https://github.com/libav/libav/blob/1ef8ff4534706de0b1da3442f380be58a650acf2/libavcodec/mjpegdec.c/#L676
static int ljpeg_decode_rgb_scan(MJpegDecodeContext *s, int predictor, int point_transform) { int i, mb_x, mb_y; uint16_t (*buffer)[4]; int left[3], top[3], topleft[3]; const int linesize = s->linesize[0]; const int mask = (1 << s->bits) - 1; av_fast_malloc(&s->ljpeg_buffer, &s->ljpeg_buffer_size, (unsigned)s->mb_width * 4 * sizeof(s->ljpeg_buffer[0][0])); buffer = s->ljpeg_buffer; for (i = 0; i < 3; i++) buffer[0][i] = 1 << (s->bits + point_transform - 1); for (mb_y = 0; mb_y < s->mb_height; mb_y++) { const int modified_predictor = mb_y ? predictor : 1; uint8_t *ptr = s->picture_ptr->data[0] + (linesize * mb_y); if (s->interlaced && s->bottom_field) ptr += linesize >> 1; for (i = 0; i < 3; i++) top[i] = left[i] = topleft[i] = buffer[0][i]; for (mb_x = 0; mb_x < s->mb_width; mb_x++) { if (s->restart_interval && !s->restart_count) s->restart_count = s->restart_interval; for (i = 0; i < 3; i++) { int pred; topleft[i] = top[i]; top[i] = buffer[mb_x][i]; PREDICT(pred, topleft[i], top[i], left[i], modified_predictor); left[i] = buffer[mb_x][i] = mask & (pred + (mjpeg_decode_dc(s, s->dc_index[i]) << point_transform)); } if (s->restart_interval && !--s->restart_count) { align_get_bits(&s->gb); skip_bits(&s->gb, 16); } } if (s->rct) { for (mb_x = 0; mb_x < s->mb_width; mb_x++) { ptr[4 * mb_x + 1] = buffer[mb_x][0] - ((buffer[mb_x][1] + buffer[mb_x][2] - 0x200) >> 2); ptr[4 * mb_x + 0] = buffer[mb_x][1] + ptr[4 * mb_x + 1]; ptr[4 * mb_x + 2] = buffer[mb_x][2] + ptr[4 * mb_x + 1]; } } else if (s->pegasus_rct) { for (mb_x = 0; mb_x < s->mb_width; mb_x++) { ptr[4 * mb_x + 1] = buffer[mb_x][0] - ((buffer[mb_x][1] + buffer[mb_x][2]) >> 2); ptr[4 * mb_x + 0] = buffer[mb_x][1] + ptr[4 * mb_x + 1]; ptr[4 * mb_x + 2] = buffer[mb_x][2] + ptr[4 * mb_x + 1]; } } else { for (mb_x = 0; mb_x < s->mb_width; mb_x++) { ptr[4 * mb_x + 0] = buffer[mb_x][2]; ptr[4 * mb_x + 1] = buffer[mb_x][1]; ptr[4 * mb_x + 2] = buffer[mb_x][0]; } } } return 0; }
['static int ljpeg_decode_rgb_scan(MJpegDecodeContext *s, int predictor,\n int point_transform)\n{\n int i, mb_x, mb_y;\n uint16_t (*buffer)[4];\n int left[3], top[3], topleft[3];\n const int linesize = s->linesize[0];\n const int mask = (1 << s->bits) - 1;\n av_fast_malloc(&s->ljpeg_buffer, &s->ljpeg_buffer_size,\n (unsigned)s->mb_width * 4 * sizeof(s->ljpeg_buffer[0][0]));\n buffer = s->ljpeg_buffer;\n for (i = 0; i < 3; i++)\n buffer[0][i] = 1 << (s->bits + point_transform - 1);\n for (mb_y = 0; mb_y < s->mb_height; mb_y++) {\n const int modified_predictor = mb_y ? predictor : 1;\n uint8_t *ptr = s->picture_ptr->data[0] + (linesize * mb_y);\n if (s->interlaced && s->bottom_field)\n ptr += linesize >> 1;\n for (i = 0; i < 3; i++)\n top[i] = left[i] = topleft[i] = buffer[0][i];\n for (mb_x = 0; mb_x < s->mb_width; mb_x++) {\n if (s->restart_interval && !s->restart_count)\n s->restart_count = s->restart_interval;\n for (i = 0; i < 3; i++) {\n int pred;\n topleft[i] = top[i];\n top[i] = buffer[mb_x][i];\n PREDICT(pred, topleft[i], top[i], left[i], modified_predictor);\n left[i] = buffer[mb_x][i] =\n mask & (pred + (mjpeg_decode_dc(s, s->dc_index[i]) << point_transform));\n }\n if (s->restart_interval && !--s->restart_count) {\n align_get_bits(&s->gb);\n skip_bits(&s->gb, 16);\n }\n }\n if (s->rct) {\n for (mb_x = 0; mb_x < s->mb_width; mb_x++) {\n ptr[4 * mb_x + 1] = buffer[mb_x][0] - ((buffer[mb_x][1] + buffer[mb_x][2] - 0x200) >> 2);\n ptr[4 * mb_x + 0] = buffer[mb_x][1] + ptr[4 * mb_x + 1];\n ptr[4 * mb_x + 2] = buffer[mb_x][2] + ptr[4 * mb_x + 1];\n }\n } else if (s->pegasus_rct) {\n for (mb_x = 0; mb_x < s->mb_width; mb_x++) {\n ptr[4 * mb_x + 1] = buffer[mb_x][0] - ((buffer[mb_x][1] + buffer[mb_x][2]) >> 2);\n ptr[4 * mb_x + 0] = buffer[mb_x][1] + ptr[4 * mb_x + 1];\n ptr[4 * mb_x + 2] = buffer[mb_x][2] + ptr[4 * mb_x + 1];\n }\n } else {\n for (mb_x = 0; mb_x < s->mb_width; mb_x++) {\n ptr[4 * mb_x + 0] = buffer[mb_x][2];\n ptr[4 * mb_x + 1] = buffer[mb_x][1];\n ptr[4 * mb_x + 2] = buffer[mb_x][0];\n }\n }\n }\n return 0;\n}']
874
0
https://github.com/libav/libav/blob/6a2176aac05e1edbcdf8fb9c26d572d092a00c3c/libavcodec/h264.c/#L348
static void await_references(H264Context *h){ MpegEncContext * const s = &h->s; const int mb_xy= h->mb_xy; const int mb_type= s->current_picture.mb_type[mb_xy]; int refs[2][48]; int nrefs[2] = {0}; int ref, list; memset(refs, -1, sizeof(refs)); if(IS_16X16(mb_type)){ get_lowest_part_y(h, refs, 0, 16, 0, IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1), nrefs); }else if(IS_16X8(mb_type)){ get_lowest_part_y(h, refs, 0, 8, 0, IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1), nrefs); get_lowest_part_y(h, refs, 8, 8, 8, IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1), nrefs); }else if(IS_8X16(mb_type)){ get_lowest_part_y(h, refs, 0, 16, 0, IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1), nrefs); get_lowest_part_y(h, refs, 4, 16, 0, IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1), nrefs); }else{ int i; assert(IS_8X8(mb_type)); for(i=0; i<4; i++){ const int sub_mb_type= h->sub_mb_type[i]; const int n= 4*i; int y_offset= (i&2)<<2; if(IS_SUB_8X8(sub_mb_type)){ get_lowest_part_y(h, refs, n , 8, y_offset, IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1), nrefs); }else if(IS_SUB_8X4(sub_mb_type)){ get_lowest_part_y(h, refs, n , 4, y_offset, IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1), nrefs); get_lowest_part_y(h, refs, n+2, 4, y_offset+4, IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1), nrefs); }else if(IS_SUB_4X8(sub_mb_type)){ get_lowest_part_y(h, refs, n , 8, y_offset, IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1), nrefs); get_lowest_part_y(h, refs, n+1, 8, y_offset, IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1), nrefs); }else{ int j; assert(IS_SUB_4X4(sub_mb_type)); for(j=0; j<4; j++){ int sub_y_offset= y_offset + 2*(j&2); get_lowest_part_y(h, refs, n+j, 4, sub_y_offset, IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1), nrefs); } } } } for(list=h->list_count-1; list>=0; list--){ for(ref=0; ref<48 && nrefs[list]; ref++){ int row = refs[list][ref]; if(row >= 0){ Picture *ref_pic = &h->ref_list[list][ref]; int ref_field = ref_pic->reference - 1; int ref_field_picture = ref_pic->field_picture; int pic_height = 16*s->mb_height >> ref_field_picture; row <<= MB_MBAFF; nrefs[list]--; if(!FIELD_PICTURE && ref_field_picture){ ff_thread_await_progress((AVFrame*)ref_pic, FFMIN((row >> 1) - !(row&1), pic_height-1), 1); ff_thread_await_progress((AVFrame*)ref_pic, FFMIN((row >> 1) , pic_height-1), 0); }else if(FIELD_PICTURE && !ref_field_picture){ ff_thread_await_progress((AVFrame*)ref_pic, FFMIN(row*2 + ref_field , pic_height-1), 0); }else if(FIELD_PICTURE){ ff_thread_await_progress((AVFrame*)ref_pic, FFMIN(row, pic_height-1), ref_field); }else{ ff_thread_await_progress((AVFrame*)ref_pic, FFMIN(row, pic_height-1), 0); } } } } }
['static int slice_end(AVCodecContext *avctx, AVFrame *pict)\n{\n Mpeg1Context *s1 = avctx->priv_data;\n MpegEncContext *s = &s1->mpeg_enc_ctx;\n if (!s1->mpeg_enc_ctx_allocated || !s->current_picture_ptr)\n return 0;\n if (s->avctx->hwaccel) {\n if (s->avctx->hwaccel->end_frame(s->avctx) < 0)\n av_log(avctx, AV_LOG_ERROR, "hardware accelerator failed to decode picture\\n");\n }\n if(CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)\n ff_xvmc_field_end(s);\n if ( !s->first_field) {\n s->current_picture_ptr->qscale_type= FF_QSCALE_TYPE_MPEG2;\n ff_er_frame_end(s);\n MPV_frame_end(s);\n if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) {\n *pict= *(AVFrame*)s->current_picture_ptr;\n ff_print_debug_info(s, pict);\n } else {\n if (avctx->active_thread_type & FF_THREAD_FRAME)\n s->picture_number++;\n if (s->last_picture_ptr != NULL) {\n *pict= *(AVFrame*)s->last_picture_ptr;\n ff_print_debug_info(s, pict);\n }\n }\n return 1;\n } else {\n return 0;\n }\n}', 'void ff_er_frame_end(MpegEncContext *s){\n int i, mb_x, mb_y, error, error_type, dc_error, mv_error, ac_error;\n int distance;\n int threshold_part[4]= {100,100,100};\n int threshold= 50;\n int is_intra_likely;\n int size = s->b8_stride * 2 * s->mb_height;\n Picture *pic= s->current_picture_ptr;\n if(!s->error_recognition || s->error_count==0 || s->avctx->lowres ||\n s->avctx->hwaccel ||\n s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU ||\n s->picture_structure != PICT_FRAME ||\n s->error_count==3*s->mb_width*(s->avctx->skip_top + s->avctx->skip_bottom)) return;\n if(s->current_picture.motion_val[0] == NULL){\n av_log(s->avctx, AV_LOG_ERROR, "Warning MVs not available\\n");\n for(i=0; i<2; i++){\n pic->ref_index[i]= av_mallocz(s->mb_stride * s->mb_height * 4 * sizeof(uint8_t));\n pic->motion_val_base[i]= av_mallocz((size+4) * 2 * sizeof(uint16_t));\n pic->motion_val[i]= pic->motion_val_base[i]+4;\n }\n pic->motion_subsample_log2= 3;\n s->current_picture= *s->current_picture_ptr;\n }\n if(s->avctx->debug&FF_DEBUG_ER){\n for(mb_y=0; mb_y<s->mb_height; mb_y++){\n for(mb_x=0; mb_x<s->mb_width; mb_x++){\n int status= s->error_status_table[mb_x + mb_y*s->mb_stride];\n av_log(s->avctx, AV_LOG_DEBUG, "%2X ", status);\n }\n av_log(s->avctx, AV_LOG_DEBUG, "\\n");\n }\n }\n for(error_type=1; error_type<=3; error_type++){\n int end_ok=0;\n for(i=s->mb_num-1; i>=0; i--){\n const int mb_xy= s->mb_index2xy[i];\n int error= s->error_status_table[mb_xy];\n if(error&(1<<error_type))\n end_ok=1;\n if(error&(8<<error_type))\n end_ok=1;\n if(!end_ok)\n s->error_status_table[mb_xy]|= 1<<error_type;\n if(error&VP_START)\n end_ok=0;\n }\n }\n if(s->partitioned_frame){\n int end_ok=0;\n for(i=s->mb_num-1; i>=0; i--){\n const int mb_xy= s->mb_index2xy[i];\n int error= s->error_status_table[mb_xy];\n if(error&AC_END)\n end_ok=0;\n if((error&MV_END) || (error&DC_END) || (error&AC_ERROR))\n end_ok=1;\n if(!end_ok)\n s->error_status_table[mb_xy]|= AC_ERROR;\n if(error&VP_START)\n end_ok=0;\n }\n }\n if(s->error_recognition>=4){\n int end_ok=1;\n for(i=s->mb_num-2; i>=s->mb_width+100; i--){\n const int mb_xy= s->mb_index2xy[i];\n int error1= s->error_status_table[mb_xy ];\n int error2= s->error_status_table[s->mb_index2xy[i+1]];\n if(error1&VP_START)\n end_ok=1;\n if( error2==(VP_START|DC_ERROR|AC_ERROR|MV_ERROR|AC_END|DC_END|MV_END)\n && error1!=(VP_START|DC_ERROR|AC_ERROR|MV_ERROR|AC_END|DC_END|MV_END)\n && ((error1&AC_END) || (error1&DC_END) || (error1&MV_END))){\n end_ok=0;\n }\n if(!end_ok)\n s->error_status_table[mb_xy]|= DC_ERROR|AC_ERROR|MV_ERROR;\n }\n }\n distance=9999999;\n for(error_type=1; error_type<=3; error_type++){\n for(i=s->mb_num-1; i>=0; i--){\n const int mb_xy= s->mb_index2xy[i];\n int error= s->error_status_table[mb_xy];\n if(!s->mbskip_table[mb_xy])\n distance++;\n if(error&(1<<error_type))\n distance= 0;\n if(s->partitioned_frame){\n if(distance < threshold_part[error_type-1])\n s->error_status_table[mb_xy]|= 1<<error_type;\n }else{\n if(distance < threshold)\n s->error_status_table[mb_xy]|= 1<<error_type;\n }\n if(error&VP_START)\n distance= 9999999;\n }\n }\n error=0;\n for(i=0; i<s->mb_num; i++){\n const int mb_xy= s->mb_index2xy[i];\n int old_error= s->error_status_table[mb_xy];\n if(old_error&VP_START)\n error= old_error& (DC_ERROR|AC_ERROR|MV_ERROR);\n else{\n error|= old_error& (DC_ERROR|AC_ERROR|MV_ERROR);\n s->error_status_table[mb_xy]|= error;\n }\n }\n if(!s->partitioned_frame){\n for(i=0; i<s->mb_num; i++){\n const int mb_xy= s->mb_index2xy[i];\n error= s->error_status_table[mb_xy];\n if(error&(AC_ERROR|DC_ERROR|MV_ERROR))\n error|= AC_ERROR|DC_ERROR|MV_ERROR;\n s->error_status_table[mb_xy]= error;\n }\n }\n dc_error= ac_error= mv_error=0;\n for(i=0; i<s->mb_num; i++){\n const int mb_xy= s->mb_index2xy[i];\n error= s->error_status_table[mb_xy];\n if(error&DC_ERROR) dc_error ++;\n if(error&AC_ERROR) ac_error ++;\n if(error&MV_ERROR) mv_error ++;\n }\n av_log(s->avctx, AV_LOG_INFO, "concealing %d DC, %d AC, %d MV errors\\n", dc_error, ac_error, mv_error);\n is_intra_likely= is_intra_more_likely(s);\n for(i=0; i<s->mb_num; i++){\n const int mb_xy= s->mb_index2xy[i];\n error= s->error_status_table[mb_xy];\n if(!((error&DC_ERROR) && (error&MV_ERROR)))\n continue;\n if(is_intra_likely)\n s->current_picture.mb_type[mb_xy]= MB_TYPE_INTRA4x4;\n else\n s->current_picture.mb_type[mb_xy]= MB_TYPE_16x16 | MB_TYPE_L0;\n }\n if (!s->last_picture.data[0] && !s->next_picture.data[0])\n for(i=0; i<s->mb_num; i++){\n const int mb_xy= s->mb_index2xy[i];\n if(!IS_INTRA(s->current_picture.mb_type[mb_xy]))\n s->current_picture.mb_type[mb_xy]= MB_TYPE_INTRA4x4;\n }\n for(mb_y=0; mb_y<s->mb_height; mb_y++){\n for(mb_x=0; mb_x<s->mb_width; mb_x++){\n const int mb_xy= mb_x + mb_y * s->mb_stride;\n const int mb_type= s->current_picture.mb_type[mb_xy];\n int dir = !s->last_picture.data[0];\n error= s->error_status_table[mb_xy];\n if(IS_INTRA(mb_type)) continue;\n if(error&MV_ERROR) continue;\n if(!(error&AC_ERROR)) continue;\n s->mv_dir = dir ? MV_DIR_BACKWARD : MV_DIR_FORWARD;\n s->mb_intra=0;\n s->mb_skipped=0;\n if(IS_8X8(mb_type)){\n int mb_index= mb_x*2 + mb_y*2*s->b8_stride;\n int j;\n s->mv_type = MV_TYPE_8X8;\n for(j=0; j<4; j++){\n s->mv[0][j][0] = s->current_picture.motion_val[dir][ mb_index + (j&1) + (j>>1)*s->b8_stride ][0];\n s->mv[0][j][1] = s->current_picture.motion_val[dir][ mb_index + (j&1) + (j>>1)*s->b8_stride ][1];\n }\n }else{\n s->mv_type = MV_TYPE_16X16;\n s->mv[0][0][0] = s->current_picture.motion_val[dir][ mb_x*2 + mb_y*2*s->b8_stride ][0];\n s->mv[0][0][1] = s->current_picture.motion_val[dir][ mb_x*2 + mb_y*2*s->b8_stride ][1];\n }\n s->dsp.clear_blocks(s->block[0]);\n s->mb_x= mb_x;\n s->mb_y= mb_y;\n decode_mb(s, 0 );\n }\n }\n if(s->pict_type==AV_PICTURE_TYPE_B){\n for(mb_y=0; mb_y<s->mb_height; mb_y++){\n for(mb_x=0; mb_x<s->mb_width; mb_x++){\n int xy= mb_x*2 + mb_y*2*s->b8_stride;\n const int mb_xy= mb_x + mb_y * s->mb_stride;\n const int mb_type= s->current_picture.mb_type[mb_xy];\n error= s->error_status_table[mb_xy];\n if(IS_INTRA(mb_type)) continue;\n if(!(error&MV_ERROR)) continue;\n if(!(error&AC_ERROR)) continue;\n s->mv_dir = MV_DIR_FORWARD|MV_DIR_BACKWARD;\n if(!s->last_picture.data[0]) s->mv_dir &= ~MV_DIR_FORWARD;\n if(!s->next_picture.data[0]) s->mv_dir &= ~MV_DIR_BACKWARD;\n s->mb_intra=0;\n s->mv_type = MV_TYPE_16X16;\n s->mb_skipped=0;\n if(s->pp_time){\n int time_pp= s->pp_time;\n int time_pb= s->pb_time;\n if (s->avctx->codec_id == CODEC_ID_H264) {\n } else {\n ff_thread_await_progress((AVFrame *) s->next_picture_ptr,\n mb_y, 0);\n }\n s->mv[0][0][0] = s->next_picture.motion_val[0][xy][0]*time_pb/time_pp;\n s->mv[0][0][1] = s->next_picture.motion_val[0][xy][1]*time_pb/time_pp;\n s->mv[1][0][0] = s->next_picture.motion_val[0][xy][0]*(time_pb - time_pp)/time_pp;\n s->mv[1][0][1] = s->next_picture.motion_val[0][xy][1]*(time_pb - time_pp)/time_pp;\n }else{\n s->mv[0][0][0]= 0;\n s->mv[0][0][1]= 0;\n s->mv[1][0][0]= 0;\n s->mv[1][0][1]= 0;\n }\n s->dsp.clear_blocks(s->block[0]);\n s->mb_x= mb_x;\n s->mb_y= mb_y;\n decode_mb(s, 0);\n }\n }\n }else\n guess_mv(s);\n if(CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)\n goto ec_clean;\n for(mb_y=0; mb_y<s->mb_height; mb_y++){\n for(mb_x=0; mb_x<s->mb_width; mb_x++){\n int dc, dcu, dcv, y, n;\n int16_t *dc_ptr;\n uint8_t *dest_y, *dest_cb, *dest_cr;\n const int mb_xy= mb_x + mb_y * s->mb_stride;\n const int mb_type= s->current_picture.mb_type[mb_xy];\n error= s->error_status_table[mb_xy];\n if(IS_INTRA(mb_type) && s->partitioned_frame) continue;\n dest_y = s->current_picture.data[0] + mb_x*16 + mb_y*16*s->linesize;\n dest_cb= s->current_picture.data[1] + mb_x*8 + mb_y*8 *s->uvlinesize;\n dest_cr= s->current_picture.data[2] + mb_x*8 + mb_y*8 *s->uvlinesize;\n dc_ptr= &s->dc_val[0][mb_x*2 + mb_y*2*s->b8_stride];\n for(n=0; n<4; n++){\n dc=0;\n for(y=0; y<8; y++){\n int x;\n for(x=0; x<8; x++){\n dc+= dest_y[x + (n&1)*8 + (y + (n>>1)*8)*s->linesize];\n }\n }\n dc_ptr[(n&1) + (n>>1)*s->b8_stride]= (dc+4)>>3;\n }\n dcu=dcv=0;\n for(y=0; y<8; y++){\n int x;\n for(x=0; x<8; x++){\n dcu+=dest_cb[x + y*(s->uvlinesize)];\n dcv+=dest_cr[x + y*(s->uvlinesize)];\n }\n }\n s->dc_val[1][mb_x + mb_y*s->mb_stride]= (dcu+4)>>3;\n s->dc_val[2][mb_x + mb_y*s->mb_stride]= (dcv+4)>>3;\n }\n }\n guess_dc(s, s->dc_val[0], s->mb_width*2, s->mb_height*2, s->b8_stride, 1);\n guess_dc(s, s->dc_val[1], s->mb_width , s->mb_height , s->mb_stride, 0);\n guess_dc(s, s->dc_val[2], s->mb_width , s->mb_height , s->mb_stride, 0);\n filter181(s->dc_val[0], s->mb_width*2, s->mb_height*2, s->b8_stride);\n for(mb_y=0; mb_y<s->mb_height; mb_y++){\n for(mb_x=0; mb_x<s->mb_width; mb_x++){\n uint8_t *dest_y, *dest_cb, *dest_cr;\n const int mb_xy= mb_x + mb_y * s->mb_stride;\n const int mb_type= s->current_picture.mb_type[mb_xy];\n error= s->error_status_table[mb_xy];\n if(IS_INTER(mb_type)) continue;\n if(!(error&AC_ERROR)) continue;\n dest_y = s->current_picture.data[0] + mb_x*16 + mb_y*16*s->linesize;\n dest_cb= s->current_picture.data[1] + mb_x*8 + mb_y*8 *s->uvlinesize;\n dest_cr= s->current_picture.data[2] + mb_x*8 + mb_y*8 *s->uvlinesize;\n put_dc(s, dest_y, dest_cb, dest_cr, mb_x, mb_y);\n }\n }\n if(s->avctx->error_concealment&FF_EC_DEBLOCK){\n h_block_filter(s, s->current_picture.data[0], s->mb_width*2, s->mb_height*2, s->linesize , 1);\n h_block_filter(s, s->current_picture.data[1], s->mb_width , s->mb_height , s->uvlinesize, 0);\n h_block_filter(s, s->current_picture.data[2], s->mb_width , s->mb_height , s->uvlinesize, 0);\n v_block_filter(s, s->current_picture.data[0], s->mb_width*2, s->mb_height*2, s->linesize , 1);\n v_block_filter(s, s->current_picture.data[1], s->mb_width , s->mb_height , s->uvlinesize, 0);\n v_block_filter(s, s->current_picture.data[2], s->mb_width , s->mb_height , s->uvlinesize, 0);\n }\nec_clean:\n for(i=0; i<s->mb_num; i++){\n const int mb_xy= s->mb_index2xy[i];\n int error= s->error_status_table[mb_xy];\n if(s->pict_type!=AV_PICTURE_TYPE_B && (error&(DC_ERROR|MV_ERROR|AC_ERROR))){\n s->mbskip_table[mb_xy]=0;\n }\n s->mbintra_table[mb_xy]=1;\n }\n}', 'static void decode_mb(MpegEncContext *s, int ref){\n s->dest[0] = s->current_picture.data[0] + (s->mb_y * 16* s->linesize ) + s->mb_x * 16;\n s->dest[1] = s->current_picture.data[1] + (s->mb_y * (16>>s->chroma_y_shift) * s->uvlinesize) + s->mb_x * (16>>s->chroma_x_shift);\n s->dest[2] = s->current_picture.data[2] + (s->mb_y * (16>>s->chroma_y_shift) * s->uvlinesize) + s->mb_x * (16>>s->chroma_x_shift);\n if(CONFIG_H264_DECODER && s->codec_id == CODEC_ID_H264){\n H264Context *h= (void*)s;\n h->mb_xy= s->mb_x + s->mb_y*s->mb_stride;\n memset(h->non_zero_count_cache, 0, sizeof(h->non_zero_count_cache));\n assert(ref>=0);\n if(ref >= h->ref_count[0])\n ref=0;\n fill_rectangle(&s->current_picture.ref_index[0][4*h->mb_xy], 2, 2, 2, ref, 1);\n fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, ref, 1);\n fill_rectangle(h->mv_cache[0][ scan8[0] ], 4, 4, 8, pack16to32(s->mv[0][0][0],s->mv[0][0][1]), 4);\n assert(!FRAME_MBAFF);\n ff_h264_hl_decode_mb(h);\n }else{\n assert(ref==0);\n MPV_decode_mb(s, s->block);\n }\n}', 'void ff_h264_hl_decode_mb(H264Context *h){\n MpegEncContext * const s = &h->s;\n const int mb_xy= h->mb_xy;\n const int mb_type= s->current_picture.mb_type[mb_xy];\n int is_complex = CONFIG_SMALL || h->is_complex || IS_INTRA_PCM(mb_type) || s->qscale == 0;\n if (CHROMA444) {\n if(is_complex || h->pixel_shift)\n hl_decode_mb_444_complex(h);\n else\n hl_decode_mb_444_simple(h);\n } else if (is_complex) {\n hl_decode_mb_complex(h);\n } else if (h->pixel_shift) {\n hl_decode_mb_simple_16(h);\n } else\n hl_decode_mb_simple_8(h);\n}', 'static void av_noinline hl_decode_mb_444_complex(H264Context *h){\n hl_decode_mb_444_internal(h, 0, h->pixel_shift);\n}', 'static av_always_inline void hl_decode_mb_444_internal(H264Context *h, int simple, int pixel_shift){\n MpegEncContext * const s = &h->s;\n const int mb_x= s->mb_x;\n const int mb_y= s->mb_y;\n const int mb_xy= h->mb_xy;\n const int mb_type= s->current_picture.mb_type[mb_xy];\n uint8_t *dest[3];\n int linesize;\n int i, j, p;\n int *block_offset = &h->block_offset[0];\n const int transform_bypass = !simple && (s->qscale == 0 && h->sps.transform_bypass);\n const int plane_count = (simple || !CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)) ? 3 : 1;\n for (p = 0; p < plane_count; p++)\n {\n dest[p] = s->current_picture.data[p] + ((mb_x << pixel_shift) + mb_y * s->linesize) * 16;\n s->dsp.prefetch(dest[p] + (s->mb_x&3)*4*s->linesize + (64 << pixel_shift), s->linesize, 4);\n }\n h->list_counts[mb_xy]= h->list_count;\n if (!simple && MB_FIELD) {\n linesize = h->mb_linesize = h->mb_uvlinesize = s->linesize * 2;\n block_offset = &h->block_offset[48];\n if(mb_y&1)\n for (p = 0; p < 3; p++)\n dest[p] -= s->linesize*15;\n if(FRAME_MBAFF) {\n int list;\n for(list=0; list<h->list_count; list++){\n if(!USES_LIST(mb_type, list))\n continue;\n if(IS_16X16(mb_type)){\n int8_t *ref = &h->ref_cache[list][scan8[0]];\n fill_rectangle(ref, 4, 4, 8, (16+*ref)^(s->mb_y&1), 1);\n }else{\n for(i=0; i<16; i+=4){\n int ref = h->ref_cache[list][scan8[i]];\n if(ref >= 0)\n fill_rectangle(&h->ref_cache[list][scan8[i]], 2, 2, 8, (16+ref)^(s->mb_y&1), 1);\n }\n }\n }\n }\n } else {\n linesize = h->mb_linesize = h->mb_uvlinesize = s->linesize;\n }\n if (!simple && IS_INTRA_PCM(mb_type)) {\n if (pixel_shift) {\n const int bit_depth = h->sps.bit_depth_luma;\n GetBitContext gb;\n init_get_bits(&gb, (uint8_t*)h->mb, 768*bit_depth);\n for (p = 0; p < plane_count; p++) {\n for (i = 0; i < 16; i++) {\n uint16_t *tmp = (uint16_t*)(dest[p] + i*linesize);\n for (j = 0; j < 16; j++)\n tmp[j] = get_bits(&gb, bit_depth);\n }\n }\n } else {\n for (p = 0; p < plane_count; p++) {\n for (i = 0; i < 16; i++) {\n memcpy(dest[p] + i*linesize, h->mb + p*128 + i*8, 16);\n }\n }\n }\n } else {\n if(IS_INTRA(mb_type)){\n if(h->deblocking_filter)\n xchg_mb_border(h, dest[0], dest[1], dest[2], linesize, linesize, 1, 1, simple, pixel_shift);\n for (p = 0; p < plane_count; p++)\n hl_decode_mb_predict_luma(h, mb_type, 1, simple, transform_bypass, pixel_shift, block_offset, linesize, dest[p], p);\n if(h->deblocking_filter)\n xchg_mb_border(h, dest[0], dest[1], dest[2], linesize, linesize, 0, 1, simple, pixel_shift);\n }else{\n hl_motion(h, dest[0], dest[1], dest[2],\n s->me.qpel_put, s->dsp.put_h264_chroma_pixels_tab,\n s->me.qpel_avg, s->dsp.avg_h264_chroma_pixels_tab,\n h->h264dsp.weight_h264_pixels_tab,\n h->h264dsp.biweight_h264_pixels_tab, pixel_shift, 1);\n }\n for (p = 0; p < plane_count; p++)\n hl_decode_mb_idct_luma(h, mb_type, 1, simple, transform_bypass, pixel_shift, block_offset, linesize, dest[p], p);\n }\n if(h->cbp || IS_INTRA(mb_type))\n {\n s->dsp.clear_blocks(h->mb);\n s->dsp.clear_blocks(h->mb+(24*16<<pixel_shift));\n }\n}', 'static av_always_inline void hl_motion(H264Context *h, uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,\n qpel_mc_func (*qpix_put)[16], h264_chroma_mc_func (*chroma_put),\n qpel_mc_func (*qpix_avg)[16], h264_chroma_mc_func (*chroma_avg),\n h264_weight_func *weight_op, h264_biweight_func *weight_avg,\n int pixel_shift, int chroma444){\n MpegEncContext * const s = &h->s;\n const int mb_xy= h->mb_xy;\n const int mb_type= s->current_picture.mb_type[mb_xy];\n assert(IS_INTER(mb_type));\n if(HAVE_PTHREADS && (s->avctx->active_thread_type & FF_THREAD_FRAME))\n await_references(h);\n prefetch_motion(h, 0, pixel_shift, chroma444);\n if(IS_16X16(mb_type)){\n mc_part(h, 0, 1, 8, 0, dest_y, dest_cb, dest_cr, 0, 0,\n qpix_put[0], chroma_put[0], qpix_avg[0], chroma_avg[0],\n weight_op, weight_avg,\n IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1),\n pixel_shift, chroma444);\n }else if(IS_16X8(mb_type)){\n mc_part(h, 0, 0, 4, 8 << pixel_shift, dest_y, dest_cb, dest_cr, 0, 0,\n qpix_put[1], chroma_put[0], qpix_avg[1], chroma_avg[0],\n &weight_op[1], &weight_avg[1],\n IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1),\n pixel_shift, chroma444);\n mc_part(h, 8, 0, 4, 8 << pixel_shift, dest_y, dest_cb, dest_cr, 0, 4,\n qpix_put[1], chroma_put[0], qpix_avg[1], chroma_avg[0],\n &weight_op[1], &weight_avg[1],\n IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1),\n pixel_shift, chroma444);\n }else if(IS_8X16(mb_type)){\n mc_part(h, 0, 0, 8, 8*h->mb_linesize, dest_y, dest_cb, dest_cr, 0, 0,\n qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],\n &weight_op[2], &weight_avg[2],\n IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1),\n pixel_shift, chroma444);\n mc_part(h, 4, 0, 8, 8*h->mb_linesize, dest_y, dest_cb, dest_cr, 4, 0,\n qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],\n &weight_op[2], &weight_avg[2],\n IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1),\n pixel_shift, chroma444);\n }else{\n int i;\n assert(IS_8X8(mb_type));\n for(i=0; i<4; i++){\n const int sub_mb_type= h->sub_mb_type[i];\n const int n= 4*i;\n int x_offset= (i&1)<<2;\n int y_offset= (i&2)<<1;\n if(IS_SUB_8X8(sub_mb_type)){\n mc_part(h, n, 1, 4, 0, dest_y, dest_cb, dest_cr, x_offset, y_offset,\n qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],\n &weight_op[3], &weight_avg[3],\n IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1),\n pixel_shift, chroma444);\n }else if(IS_SUB_8X4(sub_mb_type)){\n mc_part(h, n , 0, 2, 4 << pixel_shift, dest_y, dest_cb, dest_cr, x_offset, y_offset,\n qpix_put[2], chroma_put[1], qpix_avg[2], chroma_avg[1],\n &weight_op[4], &weight_avg[4],\n IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1),\n pixel_shift, chroma444);\n mc_part(h, n+2, 0, 2, 4 << pixel_shift, dest_y, dest_cb, dest_cr, x_offset, y_offset+2,\n qpix_put[2], chroma_put[1], qpix_avg[2], chroma_avg[1],\n &weight_op[4], &weight_avg[4],\n IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1),\n pixel_shift, chroma444);\n }else if(IS_SUB_4X8(sub_mb_type)){\n mc_part(h, n , 0, 4, 4*h->mb_linesize, dest_y, dest_cb, dest_cr, x_offset, y_offset,\n qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],\n &weight_op[5], &weight_avg[5],\n IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1),\n pixel_shift, chroma444);\n mc_part(h, n+1, 0, 4, 4*h->mb_linesize, dest_y, dest_cb, dest_cr, x_offset+2, y_offset,\n qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],\n &weight_op[5], &weight_avg[5],\n IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1),\n pixel_shift, chroma444);\n }else{\n int j;\n assert(IS_SUB_4X4(sub_mb_type));\n for(j=0; j<4; j++){\n int sub_x_offset= x_offset + 2*(j&1);\n int sub_y_offset= y_offset + (j&2);\n mc_part(h, n+j, 1, 2, 0, dest_y, dest_cb, dest_cr, sub_x_offset, sub_y_offset,\n qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],\n &weight_op[6], &weight_avg[6],\n IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1),\n pixel_shift, chroma444);\n }\n }\n }\n }\n prefetch_motion(h, 1, pixel_shift, chroma444);\n}', 'static void await_references(H264Context *h){\n MpegEncContext * const s = &h->s;\n const int mb_xy= h->mb_xy;\n const int mb_type= s->current_picture.mb_type[mb_xy];\n int refs[2][48];\n int nrefs[2] = {0};\n int ref, list;\n memset(refs, -1, sizeof(refs));\n if(IS_16X16(mb_type)){\n get_lowest_part_y(h, refs, 0, 16, 0,\n IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1), nrefs);\n }else if(IS_16X8(mb_type)){\n get_lowest_part_y(h, refs, 0, 8, 0,\n IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1), nrefs);\n get_lowest_part_y(h, refs, 8, 8, 8,\n IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1), nrefs);\n }else if(IS_8X16(mb_type)){\n get_lowest_part_y(h, refs, 0, 16, 0,\n IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1), nrefs);\n get_lowest_part_y(h, refs, 4, 16, 0,\n IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1), nrefs);\n }else{\n int i;\n assert(IS_8X8(mb_type));\n for(i=0; i<4; i++){\n const int sub_mb_type= h->sub_mb_type[i];\n const int n= 4*i;\n int y_offset= (i&2)<<2;\n if(IS_SUB_8X8(sub_mb_type)){\n get_lowest_part_y(h, refs, n , 8, y_offset,\n IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1), nrefs);\n }else if(IS_SUB_8X4(sub_mb_type)){\n get_lowest_part_y(h, refs, n , 4, y_offset,\n IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1), nrefs);\n get_lowest_part_y(h, refs, n+2, 4, y_offset+4,\n IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1), nrefs);\n }else if(IS_SUB_4X8(sub_mb_type)){\n get_lowest_part_y(h, refs, n , 8, y_offset,\n IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1), nrefs);\n get_lowest_part_y(h, refs, n+1, 8, y_offset,\n IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1), nrefs);\n }else{\n int j;\n assert(IS_SUB_4X4(sub_mb_type));\n for(j=0; j<4; j++){\n int sub_y_offset= y_offset + 2*(j&2);\n get_lowest_part_y(h, refs, n+j, 4, sub_y_offset,\n IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1), nrefs);\n }\n }\n }\n }\n for(list=h->list_count-1; list>=0; list--){\n for(ref=0; ref<48 && nrefs[list]; ref++){\n int row = refs[list][ref];\n if(row >= 0){\n Picture *ref_pic = &h->ref_list[list][ref];\n int ref_field = ref_pic->reference - 1;\n int ref_field_picture = ref_pic->field_picture;\n int pic_height = 16*s->mb_height >> ref_field_picture;\n row <<= MB_MBAFF;\n nrefs[list]--;\n if(!FIELD_PICTURE && ref_field_picture){\n ff_thread_await_progress((AVFrame*)ref_pic, FFMIN((row >> 1) - !(row&1), pic_height-1), 1);\n ff_thread_await_progress((AVFrame*)ref_pic, FFMIN((row >> 1) , pic_height-1), 0);\n }else if(FIELD_PICTURE && !ref_field_picture){\n ff_thread_await_progress((AVFrame*)ref_pic, FFMIN(row*2 + ref_field , pic_height-1), 0);\n }else if(FIELD_PICTURE){\n ff_thread_await_progress((AVFrame*)ref_pic, FFMIN(row, pic_height-1), ref_field);\n }else{\n ff_thread_await_progress((AVFrame*)ref_pic, FFMIN(row, pic_height-1), 0);\n }\n }\n }\n }\n}']
875
0
https://github.com/libav/libav/blob/391ecc961ced2bde7aecb3053ac35191f838fae8/libavformat/matroskaenc.c/#L239
static void put_ebml_void(AVIOContext *pb, uint64_t size) { int64_t currentpos = avio_tell(pb); assert(size >= 2); put_ebml_id(pb, EBML_ID_VOID); if (size < 10) put_ebml_num(pb, size - 1, 0); else put_ebml_num(pb, size - 9, 8); while (avio_tell(pb) < currentpos + size) avio_w8(pb, 0); }
['static int mkv_write_trailer(AVFormatContext *s)\n{\n MatroskaMuxContext *mkv = s->priv_data;\n AVIOContext *pb = s->pb;\n int64_t currentpos, cuespos;\n int ret;\n if (mkv->cur_audio_pkt.size > 0) {\n ret = mkv_write_packet_internal(s, &mkv->cur_audio_pkt);\n av_free_packet(&mkv->cur_audio_pkt);\n if (ret < 0) {\n av_log(s, AV_LOG_ERROR,\n "Could not write cached audio packet ret:%d\\n", ret);\n return ret;\n }\n }\n if (mkv->dyn_bc) {\n end_ebml_master(mkv->dyn_bc, mkv->cluster);\n mkv_flush_dynbuf(s);\n } else if (mkv->cluster_pos) {\n end_ebml_master(pb, mkv->cluster);\n }\n if (mkv->mode != MODE_WEBM) {\n ret = mkv_write_chapters(s);\n if (ret < 0)\n return ret;\n }\n if (pb->seekable) {\n if (mkv->cues->num_entries) {\n if (mkv->reserve_cues_space) {\n int64_t cues_end;\n currentpos = avio_tell(pb);\n avio_seek(pb, mkv->cues_pos, SEEK_SET);\n cuespos = mkv_write_cues(pb, mkv->cues, s->nb_streams);\n cues_end = avio_tell(pb);\n if (cues_end > cuespos + mkv->reserve_cues_space) {\n av_log(s, AV_LOG_ERROR,\n "Insufficient space reserved for cues: %d "\n "(needed: %" PRId64 ").\\n",\n mkv->reserve_cues_space, cues_end - cuespos);\n return AVERROR(EINVAL);\n }\n if (cues_end < cuespos + mkv->reserve_cues_space)\n put_ebml_void(pb, mkv->reserve_cues_space -\n (cues_end - cuespos));\n avio_seek(pb, currentpos, SEEK_SET);\n } else {\n cuespos = mkv_write_cues(pb, mkv->cues, s->nb_streams);\n }\n ret = mkv_add_seekhead_entry(mkv->main_seekhead, MATROSKA_ID_CUES,\n cuespos);\n if (ret < 0)\n return ret;\n }\n mkv_write_seekhead(pb, mkv->main_seekhead);\n av_log(s, AV_LOG_DEBUG, "end duration = %" PRIu64 "\\n", mkv->duration);\n currentpos = avio_tell(pb);\n avio_seek(pb, mkv->duration_offset, SEEK_SET);\n put_ebml_float(pb, MATROSKA_ID_DURATION, mkv->duration);\n avio_seek(pb, currentpos, SEEK_SET);\n }\n end_ebml_master(pb, mkv->segment);\n av_free(mkv->tracks);\n av_freep(&mkv->cues->entries);\n av_freep(&mkv->cues);\n return 0;\n}', 'static void end_ebml_master(AVIOContext *pb, ebml_master master)\n{\n int64_t pos = avio_tell(pb);\n if (avio_seek(pb, master.pos - master.sizebytes, SEEK_SET) < 0)\n return;\n put_ebml_num(pb, pos - master.pos, master.sizebytes);\n avio_seek(pb, pos, SEEK_SET);\n}', 'static int64_t mkv_write_seekhead(AVIOContext *pb, mkv_seekhead *seekhead)\n{\n ebml_master metaseek, seekentry;\n int64_t currentpos;\n int i;\n currentpos = avio_tell(pb);\n if (seekhead->reserved_size > 0) {\n if (avio_seek(pb, seekhead->filepos, SEEK_SET) < 0) {\n currentpos = -1;\n goto fail;\n }\n }\n metaseek = start_ebml_master(pb, MATROSKA_ID_SEEKHEAD, seekhead->reserved_size);\n for (i = 0; i < seekhead->num_entries; i++) {\n mkv_seekhead_entry *entry = &seekhead->entries[i];\n seekentry = start_ebml_master(pb, MATROSKA_ID_SEEKENTRY, MAX_SEEKENTRY_SIZE);\n put_ebml_id(pb, MATROSKA_ID_SEEKID);\n put_ebml_num(pb, ebml_id_size(entry->elementid), 0);\n put_ebml_id(pb, entry->elementid);\n put_ebml_uint(pb, MATROSKA_ID_SEEKPOSITION, entry->segmentpos);\n end_ebml_master(pb, seekentry);\n }\n end_ebml_master(pb, metaseek);\n if (seekhead->reserved_size > 0) {\n uint64_t remaining = seekhead->filepos + seekhead->reserved_size - avio_tell(pb);\n put_ebml_void(pb, remaining);\n avio_seek(pb, currentpos, SEEK_SET);\n currentpos = seekhead->filepos;\n }\nfail:\n av_free(seekhead->entries);\n av_free(seekhead);\n return currentpos;\n}', 'static void put_ebml_void(AVIOContext *pb, uint64_t size)\n{\n int64_t currentpos = avio_tell(pb);\n assert(size >= 2);\n put_ebml_id(pb, EBML_ID_VOID);\n if (size < 10)\n put_ebml_num(pb, size - 1, 0);\n else\n put_ebml_num(pb, size - 9, 8);\n while (avio_tell(pb) < currentpos + size)\n avio_w8(pb, 0);\n}']
876
0
https://github.com/libav/libav/blob/608be2acef3b69070f66dd539edd2197f93d6daf/libavformat/spdif.c/#L192
static int spdif_header_aac(AVFormatContext *s, AVPacket *pkt) { IEC958Context *ctx = s->priv_data; AACADTSHeaderInfo hdr; GetBitContext gbc; int ret; init_get_bits(&gbc, pkt->data, AAC_ADTS_HEADER_SIZE * 8); ret = ff_aac_parse_header(&gbc, &hdr); if (ret < 0) { av_log(s, AV_LOG_ERROR, "Wrong AAC file format\n"); return -1; } ctx->pkt_offset = hdr.samples << 2; switch (hdr.num_aac_frames) { case 1: ctx->data_type = IEC958_MPEG2_AAC; break; case 2: ctx->data_type = IEC958_MPEG2_AAC_LSF_2048; break; case 4: ctx->data_type = IEC958_MPEG2_AAC_LSF_4096; break; default: av_log(s, AV_LOG_ERROR, "%i samples in AAC frame not supported\n", hdr.samples); return -1; } return 0; }
['static int spdif_header_aac(AVFormatContext *s, AVPacket *pkt)\n{\n IEC958Context *ctx = s->priv_data;\n AACADTSHeaderInfo hdr;\n GetBitContext gbc;\n int ret;\n init_get_bits(&gbc, pkt->data, AAC_ADTS_HEADER_SIZE * 8);\n ret = ff_aac_parse_header(&gbc, &hdr);\n if (ret < 0) {\n av_log(s, AV_LOG_ERROR, "Wrong AAC file format\\n");\n return -1;\n }\n ctx->pkt_offset = hdr.samples << 2;\n switch (hdr.num_aac_frames) {\n case 1:\n ctx->data_type = IEC958_MPEG2_AAC;\n break;\n case 2:\n ctx->data_type = IEC958_MPEG2_AAC_LSF_2048;\n break;\n case 4:\n ctx->data_type = IEC958_MPEG2_AAC_LSF_4096;\n break;\n default:\n av_log(s, AV_LOG_ERROR, "%i samples in AAC frame not supported\\n",\n hdr.samples);\n return -1;\n }\n return 0;\n}', 'static inline void init_get_bits(GetBitContext *s,\n const uint8_t *buffer, int bit_size)\n{\n int buffer_size= (bit_size+7)>>3;\n if(buffer_size < 0 || bit_size < 0) {\n buffer_size = bit_size = 0;\n buffer = NULL;\n }\n s->buffer= buffer;\n s->size_in_bits= bit_size;\n s->buffer_end= buffer + buffer_size;\n#ifdef ALT_BITSTREAM_READER\n s->index=0;\n#elif defined LIBMPEG2_BITSTREAM_READER\n s->buffer_ptr = (uint8_t*)((intptr_t)buffer&(~1));\n s->bit_count = 16 + 8*((intptr_t)buffer&1);\n skip_bits_long(s, 0);\n#elif defined A32_BITSTREAM_READER\n s->buffer_ptr = (uint32_t*)((intptr_t)buffer&(~3));\n s->bit_count = 32 + 8*((intptr_t)buffer&3);\n skip_bits_long(s, 0);\n#endif\n}', 'int ff_aac_parse_header(GetBitContext *gbc, AACADTSHeaderInfo *hdr)\n{\n int size, rdb, ch, sr;\n int aot, crc_abs;\n if(get_bits(gbc, 12) != 0xfff)\n return AAC_AC3_PARSE_ERROR_SYNC;\n skip_bits1(gbc);\n skip_bits(gbc, 2);\n crc_abs = get_bits1(gbc);\n aot = get_bits(gbc, 2);\n sr = get_bits(gbc, 4);\n if(!ff_mpeg4audio_sample_rates[sr])\n return AAC_AC3_PARSE_ERROR_SAMPLE_RATE;\n skip_bits1(gbc);\n ch = get_bits(gbc, 3);\n skip_bits1(gbc);\n skip_bits1(gbc);\n skip_bits1(gbc);\n skip_bits1(gbc);\n size = get_bits(gbc, 13);\n if(size < AAC_ADTS_HEADER_SIZE)\n return AAC_AC3_PARSE_ERROR_FRAME_SIZE;\n skip_bits(gbc, 11);\n rdb = get_bits(gbc, 2);\n hdr->object_type = aot + 1;\n hdr->chan_config = ch;\n hdr->crc_absent = crc_abs;\n hdr->num_aac_frames = rdb + 1;\n hdr->sampling_index = sr;\n hdr->sample_rate = ff_mpeg4audio_sample_rates[sr];\n hdr->samples = (rdb + 1) * 1024;\n hdr->bit_rate = size * 8 * hdr->sample_rate / hdr->samples;\n return size;\n}', 'static inline unsigned int get_bits(GetBitContext *s, int n){\n register int tmp;\n OPEN_READER(re, s)\n UPDATE_CACHE(re, s)\n tmp= SHOW_UBITS(re, s, n);\n LAST_SKIP_BITS(re, s, n)\n CLOSE_READER(re, s)\n return tmp;\n}', 'static av_always_inline av_const uint32_t bswap_32(uint32_t x)\n{\n x= ((x<<8)&0xFF00FF00) | ((x>>8)&0x00FF00FF);\n x= (x>>16) | (x<<16);\n return x;\n}', 'static inline void skip_bits1(GetBitContext *s){\n skip_bits(s, 1);\n}', 'static inline void skip_bits(GetBitContext *s, int n){\n OPEN_READER(re, s)\n UPDATE_CACHE(re, s)\n LAST_SKIP_BITS(re, s, n)\n CLOSE_READER(re, s)\n}', 'static inline unsigned int get_bits1(GetBitContext *s){\n#ifdef ALT_BITSTREAM_READER\n int index= s->index;\n uint8_t result= s->buffer[ index>>3 ];\n#ifdef ALT_BITSTREAM_READER_LE\n result>>= (index&0x07);\n result&= 1;\n#else\n result<<= (index&0x07);\n result>>= 8 - 1;\n#endif\n index++;\n s->index= index;\n return result;\n#else\n return get_bits(s, 1);\n#endif\n}']
877
0
https://github.com/libav/libav/blob/e652cc9606068189cb512a36f0335a5cf2ecf287/libavcodec/bmp.c/#L173
static int bmp_decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt) { const uint8_t *buf = avpkt->data; int buf_size = avpkt->size; BMPContext *s = avctx->priv_data; AVFrame *picture = data; AVFrame *p = &s->picture; unsigned int fsize, hsize; int width, height; unsigned int depth; BiCompression comp; unsigned int ihsize; int i, j, n, linesize; uint32_t rgb[3]; uint8_t *ptr; int dsize; const uint8_t *buf0 = buf; if(buf_size < 14){ av_log(avctx, AV_LOG_ERROR, "buf size too small (%d)\n", buf_size); return -1; } if(bytestream_get_byte(&buf) != 'B' || bytestream_get_byte(&buf) != 'M') { av_log(avctx, AV_LOG_ERROR, "bad magic number\n"); return -1; } fsize = bytestream_get_le32(&buf); if(buf_size < fsize){ av_log(avctx, AV_LOG_ERROR, "not enough data (%d < %d), trying to decode anyway\n", buf_size, fsize); fsize = buf_size; } buf += 2; buf += 2; hsize = bytestream_get_le32(&buf); ihsize = bytestream_get_le32(&buf); if(ihsize + 14 > hsize){ av_log(avctx, AV_LOG_ERROR, "invalid header size %d\n", hsize); return -1; } if(fsize == 14 || fsize == ihsize + 14) fsize = buf_size - 2; if(fsize <= hsize){ av_log(avctx, AV_LOG_ERROR, "declared file size is less than header size (%d < %d)\n", fsize, hsize); return -1; } switch(ihsize){ case 40: case 64: case 108: case 124: width = bytestream_get_le32(&buf); height = bytestream_get_le32(&buf); break; case 12: width = bytestream_get_le16(&buf); height = bytestream_get_le16(&buf); break; default: av_log(avctx, AV_LOG_ERROR, "unsupported BMP file, patch welcome\n"); return -1; } if(bytestream_get_le16(&buf) != 1){ av_log(avctx, AV_LOG_ERROR, "invalid BMP header\n"); return -1; } depth = bytestream_get_le16(&buf); if(ihsize == 40) comp = bytestream_get_le32(&buf); else comp = BMP_RGB; if(comp != BMP_RGB && comp != BMP_BITFIELDS && comp != BMP_RLE4 && comp != BMP_RLE8){ av_log(avctx, AV_LOG_ERROR, "BMP coding %d not supported\n", comp); return -1; } if(comp == BMP_BITFIELDS){ buf += 20; rgb[0] = bytestream_get_le32(&buf); rgb[1] = bytestream_get_le32(&buf); rgb[2] = bytestream_get_le32(&buf); } avctx->width = width; avctx->height = height > 0? height: -height; avctx->pix_fmt = PIX_FMT_NONE; switch(depth){ case 32: if(comp == BMP_BITFIELDS){ rgb[0] = (rgb[0] >> 15) & 3; rgb[1] = (rgb[1] >> 15) & 3; rgb[2] = (rgb[2] >> 15) & 3; if(rgb[0] + rgb[1] + rgb[2] != 3 || rgb[0] == rgb[1] || rgb[0] == rgb[2] || rgb[1] == rgb[2]){ break; } } else { rgb[0] = 2; rgb[1] = 1; rgb[2] = 0; } avctx->pix_fmt = PIX_FMT_BGR24; break; case 24: avctx->pix_fmt = PIX_FMT_BGR24; break; case 16: if(comp == BMP_RGB) avctx->pix_fmt = PIX_FMT_RGB555; else if (comp == BMP_BITFIELDS) { if (rgb[0] == 0xF800 && rgb[1] == 0x07E0 && rgb[2] == 0x001F) avctx->pix_fmt = PIX_FMT_RGB565; else if (rgb[0] == 0x7C00 && rgb[1] == 0x03E0 && rgb[2] == 0x001F) avctx->pix_fmt = PIX_FMT_RGB555; else if (rgb[0] == 0x0F00 && rgb[1] == 0x00F0 && rgb[2] == 0x000F) avctx->pix_fmt = PIX_FMT_RGB444; else { av_log(avctx, AV_LOG_ERROR, "Unknown bitfields %0X %0X %0X\n", rgb[0], rgb[1], rgb[2]); return AVERROR(EINVAL); } } break; case 8: if(hsize - ihsize - 14 > 0) avctx->pix_fmt = PIX_FMT_PAL8; else avctx->pix_fmt = PIX_FMT_GRAY8; break; case 1: case 4: if(hsize - ihsize - 14 > 0){ avctx->pix_fmt = PIX_FMT_PAL8; }else{ av_log(avctx, AV_LOG_ERROR, "Unknown palette for %d-colour BMP\n", 1<<depth); return -1; } break; default: av_log(avctx, AV_LOG_ERROR, "depth %d not supported\n", depth); return -1; } if(avctx->pix_fmt == PIX_FMT_NONE){ av_log(avctx, AV_LOG_ERROR, "unsupported pixel format\n"); return -1; } if(p->data[0]) avctx->release_buffer(avctx, p); p->reference = 0; if(avctx->get_buffer(avctx, p) < 0){ av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n"); return -1; } p->pict_type = AV_PICTURE_TYPE_I; p->key_frame = 1; buf = buf0 + hsize; dsize = buf_size - hsize; n = ((avctx->width * depth) / 8 + 3) & ~3; if(n * avctx->height > dsize && comp != BMP_RLE4 && comp != BMP_RLE8){ av_log(avctx, AV_LOG_ERROR, "not enough data (%d < %d)\n", dsize, n * avctx->height); return -1; } if(comp == BMP_RLE4 || comp == BMP_RLE8) memset(p->data[0], 0, avctx->height * p->linesize[0]); if(depth == 4 || depth == 8) memset(p->data[1], 0, 1024); if(height > 0){ ptr = p->data[0] + (avctx->height - 1) * p->linesize[0]; linesize = -p->linesize[0]; } else { ptr = p->data[0]; linesize = p->linesize[0]; } if(avctx->pix_fmt == PIX_FMT_PAL8){ int colors = 1 << depth; if(ihsize >= 36){ int t; buf = buf0 + 46; t = bytestream_get_le32(&buf); if(t < 0 || t > (1 << depth)){ av_log(avctx, AV_LOG_ERROR, "Incorrect number of colors - %X for bitdepth %d\n", t, depth); }else if(t){ colors = t; } } buf = buf0 + 14 + ihsize; if((hsize-ihsize-14) < (colors << 2)){ for(i = 0; i < colors; i++) ((uint32_t*)p->data[1])[i] = bytestream_get_le24(&buf); }else{ for(i = 0; i < colors; i++) ((uint32_t*)p->data[1])[i] = bytestream_get_le32(&buf); } buf = buf0 + hsize; } if(comp == BMP_RLE4 || comp == BMP_RLE8){ if(height < 0){ p->data[0] += p->linesize[0] * (avctx->height - 1); p->linesize[0] = -p->linesize[0]; } ff_msrle_decode(avctx, (AVPicture*)p, depth, buf, dsize); if(height < 0){ p->data[0] += p->linesize[0] * (avctx->height - 1); p->linesize[0] = -p->linesize[0]; } }else{ switch(depth){ case 1: for (i = 0; i < avctx->height; i++) { int j; for (j = 0; j < n; j++) { ptr[j*8+0] = buf[j] >> 7; ptr[j*8+1] = (buf[j] >> 6) & 1; ptr[j*8+2] = (buf[j] >> 5) & 1; ptr[j*8+3] = (buf[j] >> 4) & 1; ptr[j*8+4] = (buf[j] >> 3) & 1; ptr[j*8+5] = (buf[j] >> 2) & 1; ptr[j*8+6] = (buf[j] >> 1) & 1; ptr[j*8+7] = buf[j] & 1; } buf += n; ptr += linesize; } break; case 8: case 24: for(i = 0; i < avctx->height; i++){ memcpy(ptr, buf, n); buf += n; ptr += linesize; } break; case 4: for(i = 0; i < avctx->height; i++){ int j; for(j = 0; j < n; j++){ ptr[j*2+0] = (buf[j] >> 4) & 0xF; ptr[j*2+1] = buf[j] & 0xF; } buf += n; ptr += linesize; } break; case 16: for(i = 0; i < avctx->height; i++){ const uint16_t *src = (const uint16_t *) buf; uint16_t *dst = (uint16_t *) ptr; for(j = 0; j < avctx->width; j++) *dst++ = av_le2ne16(*src++); buf += n; ptr += linesize; } break; case 32: for(i = 0; i < avctx->height; i++){ const uint8_t *src = buf; uint8_t *dst = ptr; for(j = 0; j < avctx->width; j++){ dst[0] = src[rgb[2]]; dst[1] = src[rgb[1]]; dst[2] = src[rgb[0]]; dst += 3; src += 4; } buf += n; ptr += linesize; } break; default: av_log(avctx, AV_LOG_ERROR, "BMP decoder is broken\n"); return -1; } } *picture = s->picture; *data_size = sizeof(AVPicture); return buf_size; }
['static int bmp_decode_frame(AVCodecContext *avctx,\n void *data, int *data_size,\n AVPacket *avpkt)\n{\n const uint8_t *buf = avpkt->data;\n int buf_size = avpkt->size;\n BMPContext *s = avctx->priv_data;\n AVFrame *picture = data;\n AVFrame *p = &s->picture;\n unsigned int fsize, hsize;\n int width, height;\n unsigned int depth;\n BiCompression comp;\n unsigned int ihsize;\n int i, j, n, linesize;\n uint32_t rgb[3];\n uint8_t *ptr;\n int dsize;\n const uint8_t *buf0 = buf;\n if(buf_size < 14){\n av_log(avctx, AV_LOG_ERROR, "buf size too small (%d)\\n", buf_size);\n return -1;\n }\n if(bytestream_get_byte(&buf) != \'B\' ||\n bytestream_get_byte(&buf) != \'M\') {\n av_log(avctx, AV_LOG_ERROR, "bad magic number\\n");\n return -1;\n }\n fsize = bytestream_get_le32(&buf);\n if(buf_size < fsize){\n av_log(avctx, AV_LOG_ERROR, "not enough data (%d < %d), trying to decode anyway\\n",\n buf_size, fsize);\n fsize = buf_size;\n }\n buf += 2;\n buf += 2;\n hsize = bytestream_get_le32(&buf);\n ihsize = bytestream_get_le32(&buf);\n if(ihsize + 14 > hsize){\n av_log(avctx, AV_LOG_ERROR, "invalid header size %d\\n", hsize);\n return -1;\n }\n if(fsize == 14 || fsize == ihsize + 14)\n fsize = buf_size - 2;\n if(fsize <= hsize){\n av_log(avctx, AV_LOG_ERROR, "declared file size is less than header size (%d < %d)\\n",\n fsize, hsize);\n return -1;\n }\n switch(ihsize){\n case 40:\n case 64:\n case 108:\n case 124:\n width = bytestream_get_le32(&buf);\n height = bytestream_get_le32(&buf);\n break;\n case 12:\n width = bytestream_get_le16(&buf);\n height = bytestream_get_le16(&buf);\n break;\n default:\n av_log(avctx, AV_LOG_ERROR, "unsupported BMP file, patch welcome\\n");\n return -1;\n }\n if(bytestream_get_le16(&buf) != 1){\n av_log(avctx, AV_LOG_ERROR, "invalid BMP header\\n");\n return -1;\n }\n depth = bytestream_get_le16(&buf);\n if(ihsize == 40)\n comp = bytestream_get_le32(&buf);\n else\n comp = BMP_RGB;\n if(comp != BMP_RGB && comp != BMP_BITFIELDS && comp != BMP_RLE4 && comp != BMP_RLE8){\n av_log(avctx, AV_LOG_ERROR, "BMP coding %d not supported\\n", comp);\n return -1;\n }\n if(comp == BMP_BITFIELDS){\n buf += 20;\n rgb[0] = bytestream_get_le32(&buf);\n rgb[1] = bytestream_get_le32(&buf);\n rgb[2] = bytestream_get_le32(&buf);\n }\n avctx->width = width;\n avctx->height = height > 0? height: -height;\n avctx->pix_fmt = PIX_FMT_NONE;\n switch(depth){\n case 32:\n if(comp == BMP_BITFIELDS){\n rgb[0] = (rgb[0] >> 15) & 3;\n rgb[1] = (rgb[1] >> 15) & 3;\n rgb[2] = (rgb[2] >> 15) & 3;\n if(rgb[0] + rgb[1] + rgb[2] != 3 ||\n rgb[0] == rgb[1] || rgb[0] == rgb[2] || rgb[1] == rgb[2]){\n break;\n }\n } else {\n rgb[0] = 2;\n rgb[1] = 1;\n rgb[2] = 0;\n }\n avctx->pix_fmt = PIX_FMT_BGR24;\n break;\n case 24:\n avctx->pix_fmt = PIX_FMT_BGR24;\n break;\n case 16:\n if(comp == BMP_RGB)\n avctx->pix_fmt = PIX_FMT_RGB555;\n else if (comp == BMP_BITFIELDS) {\n if (rgb[0] == 0xF800 && rgb[1] == 0x07E0 && rgb[2] == 0x001F)\n avctx->pix_fmt = PIX_FMT_RGB565;\n else if (rgb[0] == 0x7C00 && rgb[1] == 0x03E0 && rgb[2] == 0x001F)\n avctx->pix_fmt = PIX_FMT_RGB555;\n else if (rgb[0] == 0x0F00 && rgb[1] == 0x00F0 && rgb[2] == 0x000F)\n avctx->pix_fmt = PIX_FMT_RGB444;\n else {\n av_log(avctx, AV_LOG_ERROR, "Unknown bitfields %0X %0X %0X\\n", rgb[0], rgb[1], rgb[2]);\n return AVERROR(EINVAL);\n }\n }\n break;\n case 8:\n if(hsize - ihsize - 14 > 0)\n avctx->pix_fmt = PIX_FMT_PAL8;\n else\n avctx->pix_fmt = PIX_FMT_GRAY8;\n break;\n case 1:\n case 4:\n if(hsize - ihsize - 14 > 0){\n avctx->pix_fmt = PIX_FMT_PAL8;\n }else{\n av_log(avctx, AV_LOG_ERROR, "Unknown palette for %d-colour BMP\\n", 1<<depth);\n return -1;\n }\n break;\n default:\n av_log(avctx, AV_LOG_ERROR, "depth %d not supported\\n", depth);\n return -1;\n }\n if(avctx->pix_fmt == PIX_FMT_NONE){\n av_log(avctx, AV_LOG_ERROR, "unsupported pixel format\\n");\n return -1;\n }\n if(p->data[0])\n avctx->release_buffer(avctx, p);\n p->reference = 0;\n if(avctx->get_buffer(avctx, p) < 0){\n av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\\n");\n return -1;\n }\n p->pict_type = AV_PICTURE_TYPE_I;\n p->key_frame = 1;\n buf = buf0 + hsize;\n dsize = buf_size - hsize;\n n = ((avctx->width * depth) / 8 + 3) & ~3;\n if(n * avctx->height > dsize && comp != BMP_RLE4 && comp != BMP_RLE8){\n av_log(avctx, AV_LOG_ERROR, "not enough data (%d < %d)\\n",\n dsize, n * avctx->height);\n return -1;\n }\n if(comp == BMP_RLE4 || comp == BMP_RLE8)\n memset(p->data[0], 0, avctx->height * p->linesize[0]);\n if(depth == 4 || depth == 8)\n memset(p->data[1], 0, 1024);\n if(height > 0){\n ptr = p->data[0] + (avctx->height - 1) * p->linesize[0];\n linesize = -p->linesize[0];\n } else {\n ptr = p->data[0];\n linesize = p->linesize[0];\n }\n if(avctx->pix_fmt == PIX_FMT_PAL8){\n int colors = 1 << depth;\n if(ihsize >= 36){\n int t;\n buf = buf0 + 46;\n t = bytestream_get_le32(&buf);\n if(t < 0 || t > (1 << depth)){\n av_log(avctx, AV_LOG_ERROR, "Incorrect number of colors - %X for bitdepth %d\\n", t, depth);\n }else if(t){\n colors = t;\n }\n }\n buf = buf0 + 14 + ihsize;\n if((hsize-ihsize-14) < (colors << 2)){\n for(i = 0; i < colors; i++)\n ((uint32_t*)p->data[1])[i] = bytestream_get_le24(&buf);\n }else{\n for(i = 0; i < colors; i++)\n ((uint32_t*)p->data[1])[i] = bytestream_get_le32(&buf);\n }\n buf = buf0 + hsize;\n }\n if(comp == BMP_RLE4 || comp == BMP_RLE8){\n if(height < 0){\n p->data[0] += p->linesize[0] * (avctx->height - 1);\n p->linesize[0] = -p->linesize[0];\n }\n ff_msrle_decode(avctx, (AVPicture*)p, depth, buf, dsize);\n if(height < 0){\n p->data[0] += p->linesize[0] * (avctx->height - 1);\n p->linesize[0] = -p->linesize[0];\n }\n }else{\n switch(depth){\n case 1:\n for (i = 0; i < avctx->height; i++) {\n int j;\n for (j = 0; j < n; j++) {\n ptr[j*8+0] = buf[j] >> 7;\n ptr[j*8+1] = (buf[j] >> 6) & 1;\n ptr[j*8+2] = (buf[j] >> 5) & 1;\n ptr[j*8+3] = (buf[j] >> 4) & 1;\n ptr[j*8+4] = (buf[j] >> 3) & 1;\n ptr[j*8+5] = (buf[j] >> 2) & 1;\n ptr[j*8+6] = (buf[j] >> 1) & 1;\n ptr[j*8+7] = buf[j] & 1;\n }\n buf += n;\n ptr += linesize;\n }\n break;\n case 8:\n case 24:\n for(i = 0; i < avctx->height; i++){\n memcpy(ptr, buf, n);\n buf += n;\n ptr += linesize;\n }\n break;\n case 4:\n for(i = 0; i < avctx->height; i++){\n int j;\n for(j = 0; j < n; j++){\n ptr[j*2+0] = (buf[j] >> 4) & 0xF;\n ptr[j*2+1] = buf[j] & 0xF;\n }\n buf += n;\n ptr += linesize;\n }\n break;\n case 16:\n for(i = 0; i < avctx->height; i++){\n const uint16_t *src = (const uint16_t *) buf;\n uint16_t *dst = (uint16_t *) ptr;\n for(j = 0; j < avctx->width; j++)\n *dst++ = av_le2ne16(*src++);\n buf += n;\n ptr += linesize;\n }\n break;\n case 32:\n for(i = 0; i < avctx->height; i++){\n const uint8_t *src = buf;\n uint8_t *dst = ptr;\n for(j = 0; j < avctx->width; j++){\n dst[0] = src[rgb[2]];\n dst[1] = src[rgb[1]];\n dst[2] = src[rgb[0]];\n dst += 3;\n src += 4;\n }\n buf += n;\n ptr += linesize;\n }\n break;\n default:\n av_log(avctx, AV_LOG_ERROR, "BMP decoder is broken\\n");\n return -1;\n }\n }\n *picture = s->picture;\n *data_size = sizeof(AVPicture);\n return buf_size;\n}']
878
0
https://github.com/openssl/openssl/blob/a8140a42f5ee9e4e1423b5b6b319dc4657659f6f/crypto/bn/bn_conv.c/#L141
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) 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 = 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; }
['static int test_bn_output(int n)\n{\n BIGNUM *b = NULL;\n if (bn_output_tests[n] != NULL\n && !TEST_true(BN_hex2bn(&b, bn_output_tests[n])))\n return 0;\n test_output_bignum(bn_output_tests[n], b);\n BN_free(b);\n return 1;\n}', "int BN_hex2bn(BIGNUM **bn, const char *a)\n{\n BIGNUM *ret = NULL;\n BN_ULONG l = 0;\n int neg = 0, h, m, i, j, k, c;\n int num;\n if (a == NULL || *a == '\\0')\n return 0;\n if (*a == '-') {\n neg = 1;\n a++;\n }\n for (i = 0; i <= INT_MAX / 4 && ossl_isxdigit(a[i]); i++)\n continue;\n if (i == 0 || i > INT_MAX / 4)\n goto err;\n num = i + neg;\n if (bn == NULL)\n return num;\n if (*bn == NULL) {\n if ((ret = BN_new()) == NULL)\n return 0;\n } else {\n ret = *bn;\n BN_zero(ret);\n }\n if (bn_expand(ret, i * 4) == NULL)\n goto err;\n j = i;\n m = 0;\n h = 0;\n while (j > 0) {\n m = (BN_BYTES * 2 <= j) ? BN_BYTES * 2 : j;\n l = 0;\n for (;;) {\n c = a[j - m];\n k = OPENSSL_hexchar2int(c);\n if (k < 0)\n k = 0;\n l = (l << 4) | k;\n if (--m <= 0) {\n ret->d[h++] = l;\n break;\n }\n }\n j -= BN_BYTES * 2;\n }\n ret->top = h;\n bn_correct_top(ret);\n *bn = ret;\n bn_check_top(ret);\n if (ret->top != 0)\n ret->neg = neg;\n return num;\n err:\n if (*bn == NULL)\n BN_free(ret);\n return 0;\n}"]
879
0
https://github.com/libav/libav/blob/e5b0fc170f85b00f7dd0ac514918fb5c95253d39/libavcodec/bitstream.h/#L139
static inline uint64_t get_val(BitstreamContext *bc, unsigned n) { #ifdef BITSTREAM_READER_LE uint64_t ret = bc->bits & ((UINT64_C(1) << n) - 1); bc->bits >>= n; #else uint64_t ret = bc->bits >> (64 - n); bc->bits <<= n; #endif bc->bits_left -= n; return ret; }
['static int decode_gainc_data(BitstreamContext *bc, Atrac3pChanUnitCtx *ctx,\n int num_channels, AVCodecContext *avctx)\n{\n int ch_num, coded_subbands, sb, ret;\n for (ch_num = 0; ch_num < num_channels; ch_num++) {\n memset(ctx->channels[ch_num].gain_data, 0,\n sizeof(*ctx->channels[ch_num].gain_data) * ATRAC3P_SUBBANDS);\n if (bitstream_read_bit(bc)) {\n coded_subbands = bitstream_read(bc, 4) + 1;\n if (bitstream_read_bit(bc))\n ctx->channels[ch_num].num_gain_subbands = bitstream_read(bc, 4) + 1;\n else\n ctx->channels[ch_num].num_gain_subbands = coded_subbands;\n if ((ret = decode_gainc_npoints(bc, ctx, ch_num, coded_subbands)) < 0 ||\n (ret = decode_gainc_levels(bc, ctx, ch_num, coded_subbands)) < 0 ||\n (ret = decode_gainc_loc_codes(bc, ctx, ch_num, coded_subbands, avctx)) < 0)\n return ret;\n if (coded_subbands > 0) {\n for (sb = coded_subbands; sb < ctx->channels[ch_num].num_gain_subbands; sb++)\n ctx->channels[ch_num].gain_data[sb] =\n ctx->channels[ch_num].gain_data[sb - 1];\n }\n } else {\n ctx->channels[ch_num].num_gain_subbands = 0;\n }\n }\n return 0;\n}', 'static inline unsigned bitstream_read_bit(BitstreamContext *bc)\n{\n if (!bc->bits_left)\n refill_64(bc);\n return get_val(bc, 1);\n}', 'static inline uint64_t get_val(BitstreamContext *bc, unsigned n)\n{\n#ifdef BITSTREAM_READER_LE\n uint64_t ret = bc->bits & ((UINT64_C(1) << n) - 1);\n bc->bits >>= n;\n#else\n uint64_t ret = bc->bits >> (64 - n);\n bc->bits <<= n;\n#endif\n bc->bits_left -= n;\n return ret;\n}']
880
0
https://github.com/libav/libav/blob/fd7f59639c43f0ab6b83ad2c1ceccafc553d7845/libavformat/avidec.c/#L545
static int avi_read_header(AVFormatContext *s, AVFormatParameters *ap) { AVIContext *avi = s->priv_data; ByteIOContext *pb = s->pb; uint32_t tag, tag1, handler; int codec_type, stream_index, frame_period, bit_rate; unsigned int size, nb_frames; int i; AVStream *st; AVIStream *ast = NULL; int avih_width=0, avih_height=0; int amv_file_format=0; avi->stream_index= -1; if (get_riff(avi, pb) < 0) return -1; avi->fsize = url_fsize(pb); if(avi->fsize<=0) avi->fsize= avi->riff_end; stream_index = -1; codec_type = -1; frame_period = 0; for(;;) { if (url_feof(pb)) goto fail; tag = get_le32(pb); size = get_le32(pb); #ifdef DEBUG print_tag("tag", tag, size); #endif switch(tag) { case MKTAG('L', 'I', 'S', 'T'): tag1 = get_le32(pb); #ifdef DEBUG print_tag("list", tag1, 0); #endif if (tag1 == MKTAG('m', 'o', 'v', 'i')) { avi->movi_list = url_ftell(pb) - 4; if(size) avi->movi_end = avi->movi_list + size + (size & 1); else avi->movi_end = url_fsize(pb); #ifdef DEBUG printf("movi end=%"PRIx64"\n", avi->movi_end); #endif goto end_of_header; } break; case MKTAG('d', 'm', 'l', 'h'): avi->is_odml = 1; url_fskip(pb, size + (size & 1)); break; case MKTAG('a', 'm', 'v', 'h'): amv_file_format=1; case MKTAG('a', 'v', 'i', 'h'): frame_period = get_le32(pb); bit_rate = get_le32(pb) * 8; get_le32(pb); avi->non_interleaved |= get_le32(pb) & AVIF_MUSTUSEINDEX; url_fskip(pb, 2 * 4); get_le32(pb); get_le32(pb); avih_width=get_le32(pb); avih_height=get_le32(pb); url_fskip(pb, size - 10 * 4); break; case MKTAG('s', 't', 'r', 'h'): tag1 = get_le32(pb); handler = get_le32(pb); if(tag1 == MKTAG('p', 'a', 'd', 's')){ url_fskip(pb, size - 8); break; }else{ stream_index++; st = av_new_stream(s, stream_index); if (!st) goto fail; ast = av_mallocz(sizeof(AVIStream)); if (!ast) goto fail; st->priv_data = ast; } if(amv_file_format) tag1 = stream_index ? MKTAG('a','u','d','s') : MKTAG('v','i','d','s'); #ifdef DEBUG print_tag("strh", tag1, -1); #endif if(tag1 == MKTAG('i', 'a', 'v', 's') || tag1 == MKTAG('i', 'v', 'a', 's')){ int64_t dv_dur; if (s->nb_streams != 1) goto fail; if (handler != MKTAG('d', 'v', 's', 'd') && handler != MKTAG('d', 'v', 'h', 'd') && handler != MKTAG('d', 'v', 's', 'l')) goto fail; ast = s->streams[0]->priv_data; av_freep(&s->streams[0]->codec->extradata); av_freep(&s->streams[0]); s->nb_streams = 0; if (ENABLE_DV_DEMUXER) { avi->dv_demux = dv_init_demux(s); if (!avi->dv_demux) goto fail; } s->streams[0]->priv_data = ast; url_fskip(pb, 3 * 4); ast->scale = get_le32(pb); ast->rate = get_le32(pb); url_fskip(pb, 4); dv_dur = get_le32(pb); if (ast->scale > 0 && ast->rate > 0 && dv_dur > 0) { dv_dur *= AV_TIME_BASE; s->duration = av_rescale(dv_dur, ast->scale, ast->rate); } stream_index = s->nb_streams - 1; url_fskip(pb, size - 9*4); break; } assert(stream_index < s->nb_streams); st->codec->stream_codec_tag= handler; get_le32(pb); get_le16(pb); get_le16(pb); get_le32(pb); ast->scale = get_le32(pb); ast->rate = get_le32(pb); if(!(ast->scale && ast->rate)){ av_log(s, AV_LOG_WARNING, "scale/rate is %u/%u which is invalid. (This file has been generated by broken software.)\n", ast->scale, ast->rate); if(frame_period){ ast->rate = 1000000; ast->scale = frame_period; }else{ ast->rate = 25; ast->scale = 1; } } av_set_pts_info(st, 64, ast->scale, ast->rate); ast->cum_len=get_le32(pb); nb_frames = get_le32(pb); st->start_time = 0; st->duration = nb_frames; get_le32(pb); get_le32(pb); ast->sample_size = get_le32(pb); ast->cum_len *= FFMAX(1, ast->sample_size); switch(tag1) { case MKTAG('v', 'i', 'd', 's'): codec_type = CODEC_TYPE_VIDEO; ast->sample_size = 0; break; case MKTAG('a', 'u', 'd', 's'): codec_type = CODEC_TYPE_AUDIO; break; case MKTAG('t', 'x', 't', 's'): codec_type = CODEC_TYPE_DATA; break; case MKTAG('d', 'a', 't', 's'): codec_type = CODEC_TYPE_DATA; break; default: av_log(s, AV_LOG_ERROR, "unknown stream type %X\n", tag1); goto fail; } ast->frame_offset= ast->cum_len; url_fskip(pb, size - 12 * 4); break; case MKTAG('s', 't', 'r', 'f'): if (stream_index >= (unsigned)s->nb_streams || avi->dv_demux) { url_fskip(pb, size); } else { st = s->streams[stream_index]; switch(codec_type) { case CODEC_TYPE_VIDEO: if(amv_file_format){ st->codec->width=avih_width; st->codec->height=avih_height; st->codec->codec_type = CODEC_TYPE_VIDEO; st->codec->codec_id = CODEC_ID_AMV; url_fskip(pb, size); break; } get_le32(pb); st->codec->width = get_le32(pb); st->codec->height = get_le32(pb); get_le16(pb); st->codec->bits_per_coded_sample= get_le16(pb); tag1 = get_le32(pb); get_le32(pb); get_le32(pb); get_le32(pb); get_le32(pb); get_le32(pb); if (tag1 == MKTAG('D', 'X', 'S', 'B')) { st->codec->codec_type = CODEC_TYPE_SUBTITLE; st->codec->codec_tag = tag1; st->codec->codec_id = CODEC_ID_XSUB; break; } if(size > 10*4 && size<(1<<30)){ st->codec->extradata_size= size - 10*4; st->codec->extradata= av_malloc(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE); get_buffer(pb, st->codec->extradata, st->codec->extradata_size); } if(st->codec->extradata_size & 1) get_byte(pb); if (st->codec->extradata_size && (st->codec->bits_per_coded_sample <= 8)) { st->codec->palctrl = av_mallocz(sizeof(AVPaletteControl)); #ifdef WORDS_BIGENDIAN for (i = 0; i < FFMIN(st->codec->extradata_size, AVPALETTE_SIZE)/4; i++) st->codec->palctrl->palette[i] = bswap_32(((uint32_t*)st->codec->extradata)[i]); #else memcpy(st->codec->palctrl->palette, st->codec->extradata, FFMIN(st->codec->extradata_size, AVPALETTE_SIZE)); #endif st->codec->palctrl->palette_changed = 1; } #ifdef DEBUG print_tag("video", tag1, 0); #endif st->codec->codec_type = CODEC_TYPE_VIDEO; st->codec->codec_tag = tag1; st->codec->codec_id = codec_get_id(codec_bmp_tags, tag1); st->need_parsing = AVSTREAM_PARSE_HEADERS; break; case CODEC_TYPE_AUDIO: get_wav_header(pb, st->codec, size); if(ast->sample_size && st->codec->block_align && ast->sample_size != st->codec->block_align){ av_log(s, AV_LOG_WARNING, "sample size (%d) != block align (%d)\n", ast->sample_size, st->codec->block_align); ast->sample_size= st->codec->block_align; } if (size%2) url_fskip(pb, 1); st->need_parsing = AVSTREAM_PARSE_TIMESTAMPS; if (st->codec->codec_id == CODEC_ID_AAC && st->codec->extradata_size) st->need_parsing = AVSTREAM_PARSE_NONE; if (st->codec->stream_codec_tag == ff_get_fourcc("Axan")){ st->codec->codec_id = CODEC_ID_XAN_DPCM; st->codec->codec_tag = 0; } if (amv_file_format) st->codec->codec_id = CODEC_ID_ADPCM_IMA_AMV; break; default: st->codec->codec_type = CODEC_TYPE_DATA; st->codec->codec_id= CODEC_ID_NONE; st->codec->codec_tag= 0; url_fskip(pb, size); break; } } break; case MKTAG('i', 'n', 'd', 'x'): i= url_ftell(pb); if(!url_is_streamed(pb) && !(s->flags & AVFMT_FLAG_IGNIDX)){ read_braindead_odml_indx(s, 0); } url_fseek(pb, i+size, SEEK_SET); break; case MKTAG('v', 'p', 'r', 'p'): if(stream_index < (unsigned)s->nb_streams && size > 9*4){ AVRational active, active_aspect; st = s->streams[stream_index]; get_le32(pb); get_le32(pb); get_le32(pb); get_le32(pb); get_le32(pb); active_aspect.den= get_le16(pb); active_aspect.num= get_le16(pb); active.num = get_le32(pb); active.den = get_le32(pb); get_le32(pb); if(active_aspect.num && active_aspect.den && active.num && active.den){ st->sample_aspect_ratio= av_div_q(active_aspect, active); } size -= 9*4; } url_fseek(pb, size, SEEK_CUR); break; case MKTAG('I', 'N', 'A', 'M'): avi_read_tag(s, "Title", size); break; case MKTAG('I', 'A', 'R', 'T'): avi_read_tag(s, "Artist", size); break; case MKTAG('I', 'C', 'O', 'P'): avi_read_tag(s, "Copyright", size); break; case MKTAG('I', 'C', 'M', 'T'): avi_read_tag(s, "Comment", size); break; case MKTAG('I', 'G', 'N', 'R'): avi_read_tag(s, "Genre", size); break; case MKTAG('I', 'P', 'R', 'D'): avi_read_tag(s, "Album", size); break; case MKTAG('I', 'P', 'R', 'T'): avi_read_tag(s, "Track", size); break; default: if(size > 1000000){ av_log(s, AV_LOG_ERROR, "Something went wrong during header parsing, " "I will ignore it and try to continue anyway.\n"); avi->movi_list = url_ftell(pb) - 4; avi->movi_end = url_fsize(pb); goto end_of_header; } size += (size & 1); url_fskip(pb, size); break; } } end_of_header: if (stream_index != s->nb_streams - 1) { fail: return -1; } if(!avi->index_loaded && !url_is_streamed(pb)) avi_load_index(s); avi->index_loaded = 1; avi->non_interleaved |= guess_ni_flag(s); if(avi->non_interleaved) { av_log(s, AV_LOG_INFO, "non-interleaved AVI\n"); clean_index(s); } return 0; }
['static int avi_read_header(AVFormatContext *s, AVFormatParameters *ap)\n{\n AVIContext *avi = s->priv_data;\n ByteIOContext *pb = s->pb;\n uint32_t tag, tag1, handler;\n int codec_type, stream_index, frame_period, bit_rate;\n unsigned int size, nb_frames;\n int i;\n AVStream *st;\n AVIStream *ast = NULL;\n int avih_width=0, avih_height=0;\n int amv_file_format=0;\n avi->stream_index= -1;\n if (get_riff(avi, pb) < 0)\n return -1;\n avi->fsize = url_fsize(pb);\n if(avi->fsize<=0)\n avi->fsize= avi->riff_end;\n stream_index = -1;\n codec_type = -1;\n frame_period = 0;\n for(;;) {\n if (url_feof(pb))\n goto fail;\n tag = get_le32(pb);\n size = get_le32(pb);\n#ifdef DEBUG\n print_tag("tag", tag, size);\n#endif\n switch(tag) {\n case MKTAG(\'L\', \'I\', \'S\', \'T\'):\n tag1 = get_le32(pb);\n#ifdef DEBUG\n print_tag("list", tag1, 0);\n#endif\n if (tag1 == MKTAG(\'m\', \'o\', \'v\', \'i\')) {\n avi->movi_list = url_ftell(pb) - 4;\n if(size) avi->movi_end = avi->movi_list + size + (size & 1);\n else avi->movi_end = url_fsize(pb);\n#ifdef DEBUG\n printf("movi end=%"PRIx64"\\n", avi->movi_end);\n#endif\n goto end_of_header;\n }\n break;\n case MKTAG(\'d\', \'m\', \'l\', \'h\'):\n avi->is_odml = 1;\n url_fskip(pb, size + (size & 1));\n break;\n case MKTAG(\'a\', \'m\', \'v\', \'h\'):\n amv_file_format=1;\n case MKTAG(\'a\', \'v\', \'i\', \'h\'):\n frame_period = get_le32(pb);\n bit_rate = get_le32(pb) * 8;\n get_le32(pb);\n avi->non_interleaved |= get_le32(pb) & AVIF_MUSTUSEINDEX;\n url_fskip(pb, 2 * 4);\n get_le32(pb);\n get_le32(pb);\n avih_width=get_le32(pb);\n avih_height=get_le32(pb);\n url_fskip(pb, size - 10 * 4);\n break;\n case MKTAG(\'s\', \'t\', \'r\', \'h\'):\n tag1 = get_le32(pb);\n handler = get_le32(pb);\n if(tag1 == MKTAG(\'p\', \'a\', \'d\', \'s\')){\n url_fskip(pb, size - 8);\n break;\n }else{\n stream_index++;\n st = av_new_stream(s, stream_index);\n if (!st)\n goto fail;\n ast = av_mallocz(sizeof(AVIStream));\n if (!ast)\n goto fail;\n st->priv_data = ast;\n }\n if(amv_file_format)\n tag1 = stream_index ? MKTAG(\'a\',\'u\',\'d\',\'s\') : MKTAG(\'v\',\'i\',\'d\',\'s\');\n#ifdef DEBUG\n print_tag("strh", tag1, -1);\n#endif\n if(tag1 == MKTAG(\'i\', \'a\', \'v\', \'s\') || tag1 == MKTAG(\'i\', \'v\', \'a\', \'s\')){\n int64_t dv_dur;\n if (s->nb_streams != 1)\n goto fail;\n if (handler != MKTAG(\'d\', \'v\', \'s\', \'d\') &&\n handler != MKTAG(\'d\', \'v\', \'h\', \'d\') &&\n handler != MKTAG(\'d\', \'v\', \'s\', \'l\'))\n goto fail;\n ast = s->streams[0]->priv_data;\n av_freep(&s->streams[0]->codec->extradata);\n av_freep(&s->streams[0]);\n s->nb_streams = 0;\n if (ENABLE_DV_DEMUXER) {\n avi->dv_demux = dv_init_demux(s);\n if (!avi->dv_demux)\n goto fail;\n }\n s->streams[0]->priv_data = ast;\n url_fskip(pb, 3 * 4);\n ast->scale = get_le32(pb);\n ast->rate = get_le32(pb);\n url_fskip(pb, 4);\n dv_dur = get_le32(pb);\n if (ast->scale > 0 && ast->rate > 0 && dv_dur > 0) {\n dv_dur *= AV_TIME_BASE;\n s->duration = av_rescale(dv_dur, ast->scale, ast->rate);\n }\n stream_index = s->nb_streams - 1;\n url_fskip(pb, size - 9*4);\n break;\n }\n assert(stream_index < s->nb_streams);\n st->codec->stream_codec_tag= handler;\n get_le32(pb);\n get_le16(pb);\n get_le16(pb);\n get_le32(pb);\n ast->scale = get_le32(pb);\n ast->rate = get_le32(pb);\n if(!(ast->scale && ast->rate)){\n av_log(s, AV_LOG_WARNING, "scale/rate is %u/%u which is invalid. (This file has been generated by broken software.)\\n", ast->scale, ast->rate);\n if(frame_period){\n ast->rate = 1000000;\n ast->scale = frame_period;\n }else{\n ast->rate = 25;\n ast->scale = 1;\n }\n }\n av_set_pts_info(st, 64, ast->scale, ast->rate);\n ast->cum_len=get_le32(pb);\n nb_frames = get_le32(pb);\n st->start_time = 0;\n st->duration = nb_frames;\n get_le32(pb);\n get_le32(pb);\n ast->sample_size = get_le32(pb);\n ast->cum_len *= FFMAX(1, ast->sample_size);\n switch(tag1) {\n case MKTAG(\'v\', \'i\', \'d\', \'s\'):\n codec_type = CODEC_TYPE_VIDEO;\n ast->sample_size = 0;\n break;\n case MKTAG(\'a\', \'u\', \'d\', \'s\'):\n codec_type = CODEC_TYPE_AUDIO;\n break;\n case MKTAG(\'t\', \'x\', \'t\', \'s\'):\n codec_type = CODEC_TYPE_DATA;\n break;\n case MKTAG(\'d\', \'a\', \'t\', \'s\'):\n codec_type = CODEC_TYPE_DATA;\n break;\n default:\n av_log(s, AV_LOG_ERROR, "unknown stream type %X\\n", tag1);\n goto fail;\n }\n ast->frame_offset= ast->cum_len;\n url_fskip(pb, size - 12 * 4);\n break;\n case MKTAG(\'s\', \'t\', \'r\', \'f\'):\n if (stream_index >= (unsigned)s->nb_streams || avi->dv_demux) {\n url_fskip(pb, size);\n } else {\n st = s->streams[stream_index];\n switch(codec_type) {\n case CODEC_TYPE_VIDEO:\n if(amv_file_format){\n st->codec->width=avih_width;\n st->codec->height=avih_height;\n st->codec->codec_type = CODEC_TYPE_VIDEO;\n st->codec->codec_id = CODEC_ID_AMV;\n url_fskip(pb, size);\n break;\n }\n get_le32(pb);\n st->codec->width = get_le32(pb);\n st->codec->height = get_le32(pb);\n get_le16(pb);\n st->codec->bits_per_coded_sample= get_le16(pb);\n tag1 = get_le32(pb);\n get_le32(pb);\n get_le32(pb);\n get_le32(pb);\n get_le32(pb);\n get_le32(pb);\n if (tag1 == MKTAG(\'D\', \'X\', \'S\', \'B\')) {\n st->codec->codec_type = CODEC_TYPE_SUBTITLE;\n st->codec->codec_tag = tag1;\n st->codec->codec_id = CODEC_ID_XSUB;\n break;\n }\n if(size > 10*4 && size<(1<<30)){\n st->codec->extradata_size= size - 10*4;\n st->codec->extradata= av_malloc(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);\n get_buffer(pb, st->codec->extradata, st->codec->extradata_size);\n }\n if(st->codec->extradata_size & 1)\n get_byte(pb);\n if (st->codec->extradata_size && (st->codec->bits_per_coded_sample <= 8)) {\n st->codec->palctrl = av_mallocz(sizeof(AVPaletteControl));\n#ifdef WORDS_BIGENDIAN\n for (i = 0; i < FFMIN(st->codec->extradata_size, AVPALETTE_SIZE)/4; i++)\n st->codec->palctrl->palette[i] = bswap_32(((uint32_t*)st->codec->extradata)[i]);\n#else\n memcpy(st->codec->palctrl->palette, st->codec->extradata,\n FFMIN(st->codec->extradata_size, AVPALETTE_SIZE));\n#endif\n st->codec->palctrl->palette_changed = 1;\n }\n#ifdef DEBUG\n print_tag("video", tag1, 0);\n#endif\n st->codec->codec_type = CODEC_TYPE_VIDEO;\n st->codec->codec_tag = tag1;\n st->codec->codec_id = codec_get_id(codec_bmp_tags, tag1);\n st->need_parsing = AVSTREAM_PARSE_HEADERS;\n break;\n case CODEC_TYPE_AUDIO:\n get_wav_header(pb, st->codec, size);\n if(ast->sample_size && st->codec->block_align && ast->sample_size != st->codec->block_align){\n av_log(s, AV_LOG_WARNING, "sample size (%d) != block align (%d)\\n", ast->sample_size, st->codec->block_align);\n ast->sample_size= st->codec->block_align;\n }\n if (size%2)\n url_fskip(pb, 1);\n st->need_parsing = AVSTREAM_PARSE_TIMESTAMPS;\n if (st->codec->codec_id == CODEC_ID_AAC && st->codec->extradata_size)\n st->need_parsing = AVSTREAM_PARSE_NONE;\n if (st->codec->stream_codec_tag == ff_get_fourcc("Axan")){\n st->codec->codec_id = CODEC_ID_XAN_DPCM;\n st->codec->codec_tag = 0;\n }\n if (amv_file_format)\n st->codec->codec_id = CODEC_ID_ADPCM_IMA_AMV;\n break;\n default:\n st->codec->codec_type = CODEC_TYPE_DATA;\n st->codec->codec_id= CODEC_ID_NONE;\n st->codec->codec_tag= 0;\n url_fskip(pb, size);\n break;\n }\n }\n break;\n case MKTAG(\'i\', \'n\', \'d\', \'x\'):\n i= url_ftell(pb);\n if(!url_is_streamed(pb) && !(s->flags & AVFMT_FLAG_IGNIDX)){\n read_braindead_odml_indx(s, 0);\n }\n url_fseek(pb, i+size, SEEK_SET);\n break;\n case MKTAG(\'v\', \'p\', \'r\', \'p\'):\n if(stream_index < (unsigned)s->nb_streams && size > 9*4){\n AVRational active, active_aspect;\n st = s->streams[stream_index];\n get_le32(pb);\n get_le32(pb);\n get_le32(pb);\n get_le32(pb);\n get_le32(pb);\n active_aspect.den= get_le16(pb);\n active_aspect.num= get_le16(pb);\n active.num = get_le32(pb);\n active.den = get_le32(pb);\n get_le32(pb);\n if(active_aspect.num && active_aspect.den && active.num && active.den){\n st->sample_aspect_ratio= av_div_q(active_aspect, active);\n }\n size -= 9*4;\n }\n url_fseek(pb, size, SEEK_CUR);\n break;\n case MKTAG(\'I\', \'N\', \'A\', \'M\'):\n avi_read_tag(s, "Title", size);\n break;\n case MKTAG(\'I\', \'A\', \'R\', \'T\'):\n avi_read_tag(s, "Artist", size);\n break;\n case MKTAG(\'I\', \'C\', \'O\', \'P\'):\n avi_read_tag(s, "Copyright", size);\n break;\n case MKTAG(\'I\', \'C\', \'M\', \'T\'):\n avi_read_tag(s, "Comment", size);\n break;\n case MKTAG(\'I\', \'G\', \'N\', \'R\'):\n avi_read_tag(s, "Genre", size);\n break;\n case MKTAG(\'I\', \'P\', \'R\', \'D\'):\n avi_read_tag(s, "Album", size);\n break;\n case MKTAG(\'I\', \'P\', \'R\', \'T\'):\n avi_read_tag(s, "Track", size);\n break;\n default:\n if(size > 1000000){\n av_log(s, AV_LOG_ERROR, "Something went wrong during header parsing, "\n "I will ignore it and try to continue anyway.\\n");\n avi->movi_list = url_ftell(pb) - 4;\n avi->movi_end = url_fsize(pb);\n goto end_of_header;\n }\n size += (size & 1);\n url_fskip(pb, size);\n break;\n }\n }\n end_of_header:\n if (stream_index != s->nb_streams - 1) {\n fail:\n return -1;\n }\n if(!avi->index_loaded && !url_is_streamed(pb))\n avi_load_index(s);\n avi->index_loaded = 1;\n avi->non_interleaved |= guess_ni_flag(s);\n if(avi->non_interleaved) {\n av_log(s, AV_LOG_INFO, "non-interleaved AVI\\n");\n clean_index(s);\n }\n return 0;\n}']
881
0
https://github.com/openssl/openssl/blob/5f97f508e450af9d53e3a01b59b13e9e7b540720/crypto/lhash/lhash.c/#L240
void *lh_delete(LHASH *lh, void *data) { unsigned long hash; LHASH_NODE *nn,**rn; void *ret; lh->error=0; rn=getrn(lh,data,&hash); if (*rn == NULL) { lh->num_no_delete++; return(NULL); } else { nn= *rn; *rn=nn->next; ret=nn->data; OPENSSL_free(nn); lh->num_delete++; } lh->num_items--; if ((lh->num_nodes > MIN_NODES) && (lh->down_load >= (lh->num_items*LH_LOAD_MULT/lh->num_nodes))) contract(lh); return(ret); }
['int MAIN(int argc, char **argv)\n\t{\n\tint ret=1,i;\n\tint verbose=0;\n\tchar **pp;\n\tconst char *p;\n\tint badops=0;\n\tSSL_CTX *ctx=NULL;\n\tSSL *ssl=NULL;\n\tchar *ciphers=NULL;\n\tSSL_METHOD *meth=NULL;\n\tSTACK_OF(SSL_CIPHER) *sk;\n\tchar buf[512];\n\tBIO *STDout=NULL;\n#if !defined(NO_SSL2) && !defined(NO_SSL3)\n\tmeth=SSLv23_server_method();\n#elif !defined(NO_SSL3)\n\tmeth=SSLv3_server_method();\n#elif !defined(NO_SSL2)\n\tmeth=SSLv2_server_method();\n#endif\n\tapps_startup();\n\tif (bio_err == NULL)\n\t\tbio_err=BIO_new_fp(stderr,BIO_NOCLOSE);\n\tSTDout=BIO_new_fp(stdout,BIO_NOCLOSE);\n#ifdef VMS\n\t{\n\tBIO *tmpbio = BIO_new(BIO_f_linebuffer());\n\tSTDout = BIO_push(tmpbio, STDout);\n\t}\n#endif\n\targc--;\n\targv++;\n\twhile (argc >= 1)\n\t\t{\n\t\tif (strcmp(*argv,"-v") == 0)\n\t\t\tverbose=1;\n#ifndef NO_SSL2\n\t\telse if (strcmp(*argv,"-ssl2") == 0)\n\t\t\tmeth=SSLv2_client_method();\n#endif\n#ifndef NO_SSL3\n\t\telse if (strcmp(*argv,"-ssl3") == 0)\n\t\t\tmeth=SSLv3_client_method();\n#endif\n#ifndef NO_TLS1\n\t\telse if (strcmp(*argv,"-tls1") == 0)\n\t\t\tmeth=TLSv1_client_method();\n#endif\n\t\telse if ((strncmp(*argv,"-h",2) == 0) ||\n\t\t\t (strcmp(*argv,"-?") == 0))\n\t\t\t{\n\t\t\tbadops=1;\n\t\t\tbreak;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tciphers= *argv;\n\t\t\t}\n\t\targc--;\n\t\targv++;\n\t\t}\n\tif (badops)\n\t\t{\n\t\tfor (pp=ciphers_usage; (*pp != NULL); pp++)\n\t\t\tBIO_printf(bio_err,*pp);\n\t\tgoto end;\n\t\t}\n\tOpenSSL_add_ssl_algorithms();\n\tctx=SSL_CTX_new(meth);\n\tif (ctx == NULL) goto err;\n\tif (ciphers != NULL) {\n\t\tif(!SSL_CTX_set_cipher_list(ctx,ciphers)) {\n\t\t\tBIO_printf(bio_err, "Error in cipher list\\n");\n\t\t\tgoto err;\n\t\t}\n\t}\n\tssl=SSL_new(ctx);\n\tif (ssl == NULL) goto err;\n\tif (!verbose)\n\t\t{\n\t\tfor (i=0; ; i++)\n\t\t\t{\n\t\t\tp=SSL_get_cipher_list(ssl,i);\n\t\t\tif (p == NULL) break;\n\t\t\tif (i != 0) BIO_printf(STDout,":");\n\t\t\tBIO_printf(STDout,"%s",p);\n\t\t\t}\n\t\tBIO_printf(STDout,"\\n");\n\t\t}\n\telse\n\t\t{\n\t\tsk=SSL_get_ciphers(ssl);\n\t\tfor (i=0; i<sk_SSL_CIPHER_num(sk); i++)\n\t\t\t{\n\t\t\tBIO_puts(STDout,SSL_CIPHER_description(\n\t\t\t\tsk_SSL_CIPHER_value(sk,i),\n\t\t\t\tbuf,512));\n\t\t\t}\n\t\t}\n\tret=0;\n\tif (0)\n\t\t{\nerr:\n\t\tSSL_load_error_strings();\n\t\tERR_print_errors(bio_err);\n\t\t}\nend:\n\tif (ctx != NULL) SSL_CTX_free(ctx);\n\tif (ssl != NULL) SSL_free(ssl);\n\tif (STDout != NULL) BIO_free_all(STDout);\n\tEXIT(ret);\n\t}', 'SSL_CTX *SSL_CTX_new(SSL_METHOD *meth)\n\t{\n\tSSL_CTX *ret=NULL;\n\tif (meth == NULL)\n\t\t{\n\t\tSSLerr(SSL_F_SSL_CTX_NEW,SSL_R_NULL_SSL_METHOD_PASSED);\n\t\treturn(NULL);\n\t\t}\n\tif (SSL_get_ex_data_X509_STORE_CTX_idx() < 0)\n\t\t{\n\t\tSSLerr(SSL_F_SSL_CTX_NEW,SSL_R_X509_VERIFICATION_SETUP_PROBLEMS);\n\t\tgoto err;\n\t\t}\n\tret=(SSL_CTX *)OPENSSL_malloc(sizeof(SSL_CTX));\n\tif (ret == NULL)\n\t\tgoto err;\n\tmemset(ret,0,sizeof(SSL_CTX));\n\tret->method=meth;\n\tret->cert_store=NULL;\n\tret->session_cache_mode=SSL_SESS_CACHE_SERVER;\n\tret->session_cache_size=SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;\n\tret->session_cache_head=NULL;\n\tret->session_cache_tail=NULL;\n\tret->session_timeout=meth->get_timeout();\n\tret->new_session_cb=NULL;\n\tret->remove_session_cb=NULL;\n\tret->get_session_cb=NULL;\n\tmemset((char *)&ret->stats,0,sizeof(ret->stats));\n\tret->references=1;\n\tret->quiet_shutdown=0;\n\tret->info_callback=NULL;\n\tret->app_verify_callback=NULL;\n\tret->app_verify_arg=NULL;\n\tret->read_ahead=0;\n\tret->verify_mode=SSL_VERIFY_NONE;\n\tret->verify_depth=-1;\n\tret->default_verify_callback=NULL;\n\tif ((ret->cert=ssl_cert_new()) == NULL)\n\t\tgoto err;\n\tret->default_passwd_callback=NULL;\n\tret->default_passwd_callback_userdata=NULL;\n\tret->client_cert_cb=NULL;\n\tret->sessions=lh_new(SSL_SESSION_hash,SSL_SESSION_cmp);\n\tif (ret->sessions == NULL) goto err;\n\tret->cert_store=X509_STORE_new();\n\tif (ret->cert_store == NULL) goto err;\n\tssl_create_cipher_list(ret->method,\n\t\t&ret->cipher_list,&ret->cipher_list_by_id,\n\t\tSSL_DEFAULT_CIPHER_LIST);\n\tif (ret->cipher_list == NULL\n\t || sk_SSL_CIPHER_num(ret->cipher_list) <= 0)\n\t\t{\n\t\tSSLerr(SSL_F_SSL_CTX_NEW,SSL_R_LIBRARY_HAS_NO_CIPHERS);\n\t\tgoto err2;\n\t\t}\n\tif ((ret->rsa_md5=EVP_get_digestbyname("ssl2-md5")) == NULL)\n\t\t{\n\t\tSSLerr(SSL_F_SSL_CTX_NEW,SSL_R_UNABLE_TO_LOAD_SSL2_MD5_ROUTINES);\n\t\tgoto err2;\n\t\t}\n\tif ((ret->md5=EVP_get_digestbyname("ssl3-md5")) == NULL)\n\t\t{\n\t\tSSLerr(SSL_F_SSL_CTX_NEW,SSL_R_UNABLE_TO_LOAD_SSL3_MD5_ROUTINES);\n\t\tgoto err2;\n\t\t}\n\tif ((ret->sha1=EVP_get_digestbyname("ssl3-sha1")) == NULL)\n\t\t{\n\t\tSSLerr(SSL_F_SSL_CTX_NEW,SSL_R_UNABLE_TO_LOAD_SSL3_SHA1_ROUTINES);\n\t\tgoto err2;\n\t\t}\n\tif ((ret->client_CA=sk_X509_NAME_new_null()) == NULL)\n\t\tgoto err;\n\tCRYPTO_new_ex_data(ssl_ctx_meth,(char *)ret,&ret->ex_data);\n\tret->extra_certs=NULL;\n\tret->comp_methods=SSL_COMP_get_compression_methods();\n\treturn(ret);\nerr:\n\tSSLerr(SSL_F_SSL_CTX_NEW,ERR_R_MALLOC_FAILURE);\nerr2:\n\tif (ret != NULL) SSL_CTX_free(ret);\n\treturn(NULL);\n\t}', 'LHASH *lh_new(unsigned long (*h)(), int (*c)())\n\t{\n\tLHASH *ret;\n\tint i;\n\tif ((ret=(LHASH *)OPENSSL_malloc(sizeof(LHASH))) == NULL)\n\t\tgoto err0;\n\tif ((ret->b=(LHASH_NODE **)OPENSSL_malloc(sizeof(LHASH_NODE *)*MIN_NODES)) == NULL)\n\t\tgoto err1;\n\tfor (i=0; i<MIN_NODES; i++)\n\t\tret->b[i]=NULL;\n\tret->comp=((c == NULL)?(int (*)())strcmp:c);\n\tret->hash=((h == NULL)?(unsigned long (*)())lh_strhash:h);\n\tret->num_nodes=MIN_NODES/2;\n\tret->num_alloc_nodes=MIN_NODES;\n\tret->p=0;\n\tret->pmax=MIN_NODES/2;\n\tret->up_load=UP_LOAD;\n\tret->down_load=DOWN_LOAD;\n\tret->num_items=0;\n\tret->num_expands=0;\n\tret->num_expand_reallocs=0;\n\tret->num_contracts=0;\n\tret->num_contract_reallocs=0;\n\tret->num_hash_calls=0;\n\tret->num_comp_calls=0;\n\tret->num_insert=0;\n\tret->num_replace=0;\n\tret->num_delete=0;\n\tret->num_no_delete=0;\n\tret->num_retrieve=0;\n\tret->num_retrieve_miss=0;\n\tret->num_hash_comps=0;\n\tret->error=0;\n\treturn(ret);\nerr1:\n\tOPENSSL_free(ret);\nerr0:\n\treturn(NULL);\n\t}', 'SSL *SSL_new(SSL_CTX *ctx)\n\t{\n\tSSL *s;\n\tif (ctx == NULL)\n\t\t{\n\t\tSSLerr(SSL_F_SSL_NEW,SSL_R_NULL_SSL_CTX);\n\t\treturn(NULL);\n\t\t}\n\tif (ctx->method == NULL)\n\t\t{\n\t\tSSLerr(SSL_F_SSL_NEW,SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);\n\t\treturn(NULL);\n\t\t}\n\ts=(SSL *)OPENSSL_malloc(sizeof(SSL));\n\tif (s == NULL) goto err;\n\tmemset(s,0,sizeof(SSL));\n\tif (ctx->cert != NULL)\n\t\t{\n\t\ts->cert = ssl_cert_dup(ctx->cert);\n\t\tif (s->cert == NULL)\n\t\t\tgoto err;\n\t\t}\n\telse\n\t\ts->cert=NULL;\n\ts->sid_ctx_length=ctx->sid_ctx_length;\n\tmemcpy(&s->sid_ctx,&ctx->sid_ctx,sizeof(s->sid_ctx));\n\ts->verify_mode=ctx->verify_mode;\n\ts->verify_depth=ctx->verify_depth;\n\ts->verify_callback=ctx->default_verify_callback;\n\ts->purpose = ctx->purpose;\n\ts->trust = ctx->trust;\n\tCRYPTO_add(&ctx->references,1,CRYPTO_LOCK_SSL_CTX);\n\ts->ctx=ctx;\n\ts->verify_result=X509_V_OK;\n\ts->method=ctx->method;\n\tif (!s->method->ssl_new(s))\n\t\tgoto err;\n\ts->quiet_shutdown=ctx->quiet_shutdown;\n\ts->references=1;\n\ts->server=(ctx->method->ssl_accept == ssl_undefined_function)?0:1;\n\ts->options=ctx->options;\n\ts->mode=ctx->mode;\n\ts->read_ahead=ctx->read_ahead;\n\tSSL_clear(s);\n\tCRYPTO_new_ex_data(ssl_meth,s,&s->ex_data);\n\treturn(s);\nerr:\n\tif (s != NULL)\n\t\t{\n\t\tif (s->cert != NULL)\n\t\t\tssl_cert_free(s->cert);\n\t\tif (s->ctx != NULL)\n\t\t\tSSL_CTX_free(s->ctx);\n\t\tOPENSSL_free(s);\n\t\t}\n\tSSLerr(SSL_F_SSL_NEW,ERR_R_MALLOC_FAILURE);\n\treturn(NULL);\n\t}', 'int SSL_clear(SSL *s)\n\t{\n\tint state;\n\tif (s->method == NULL)\n\t\t{\n\t\tSSLerr(SSL_F_SSL_CLEAR,SSL_R_NO_METHOD_SPECIFIED);\n\t\treturn(0);\n\t\t}\n\ts->error=0;\n\ts->hit=0;\n\ts->shutdown=0;\n#if 0\n\tif (s->new_session) return(1);\n#else\n\tif (s->new_session)\n\t\t{\n\t\tSSLerr(SSL_F_SSL_CLEAR,SSL_R_INTERNAL_ERROR);\n\t\treturn 0;\n\t\t}\n#endif\n\tstate=s->state;\n\ts->type=0;\n\ts->state=SSL_ST_BEFORE|((s->server)?SSL_ST_ACCEPT:SSL_ST_CONNECT);\n\ts->version=s->method->version;\n\ts->client_version=s->version;\n\ts->rwstate=SSL_NOTHING;\n\ts->rstate=SSL_ST_READ_HEADER;\n#if 0\n\ts->read_ahead=s->ctx->read_ahead;\n#endif\n\tif (s->init_buf != NULL)\n\t\t{\n\t\tBUF_MEM_free(s->init_buf);\n\t\ts->init_buf=NULL;\n\t\t}\n\tssl_clear_cipher_ctx(s);\n\tif (ssl_clear_bad_session(s))\n\t\t{\n\t\tSSL_SESSION_free(s->session);\n\t\ts->session=NULL;\n\t\t}\n\ts->first_packet=0;\n#if 1\n\tif ((s->session == NULL) && (s->method != s->ctx->method))\n\t\t{\n\t\ts->method->ssl_free(s);\n\t\ts->method=s->ctx->method;\n\t\tif (!s->method->ssl_new(s))\n\t\t\treturn(0);\n\t\t}\n\telse\n#endif\n\t\ts->method->ssl_clear(s);\n\treturn(1);\n\t}', 'int ssl_clear_bad_session(SSL *s)\n\t{\n\tif (\t(s->session != NULL) &&\n\t\t!(s->shutdown & SSL_SENT_SHUTDOWN) &&\n\t\t!(SSL_in_init(s) || SSL_in_before(s)))\n\t\t{\n\t\tSSL_CTX_remove_session(s->ctx,s->session);\n\t\treturn(1);\n\t\t}\n\telse\n\t\treturn(0);\n\t}', 'int SSL_CTX_remove_session(SSL_CTX *ctx, SSL_SESSION *c)\n{\n\treturn remove_session_lock(ctx, c, 1);\n}', 'static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lck)\n\t{\n\tSSL_SESSION *r;\n\tint ret=0;\n\tif ((c != NULL) && (c->session_id_length != 0))\n\t\t{\n\t\tif(lck) CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);\n\t\tr=(SSL_SESSION *)lh_delete(ctx->sessions,c);\n\t\tif (r != NULL)\n\t\t\t{\n\t\t\tret=1;\n\t\t\tSSL_SESSION_list_remove(ctx,c);\n\t\t\t}\n\t\tif(lck) CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);\n\t\tif (ret)\n\t\t\t{\n\t\t\tr->not_resumable=1;\n\t\t\tif (ctx->remove_session_cb != NULL)\n\t\t\t\tctx->remove_session_cb(ctx,r);\n\t\t\tSSL_SESSION_free(r);\n\t\t\t}\n\t\t}\n\telse\n\t\tret=0;\n\treturn(ret);\n\t}', 'void *lh_delete(LHASH *lh, void *data)\n\t{\n\tunsigned long hash;\n\tLHASH_NODE *nn,**rn;\n\tvoid *ret;\n\tlh->error=0;\n\trn=getrn(lh,data,&hash);\n\tif (*rn == NULL)\n\t\t{\n\t\tlh->num_no_delete++;\n\t\treturn(NULL);\n\t\t}\n\telse\n\t\t{\n\t\tnn= *rn;\n\t\t*rn=nn->next;\n\t\tret=nn->data;\n\t\tOPENSSL_free(nn);\n\t\tlh->num_delete++;\n\t\t}\n\tlh->num_items--;\n\tif ((lh->num_nodes > MIN_NODES) &&\n\t\t(lh->down_load >= (lh->num_items*LH_LOAD_MULT/lh->num_nodes)))\n\t\tcontract(lh);\n\treturn(ret);\n\t}']
882
0
https://github.com/openssl/openssl/blob/7a71af86ce75751f3cb2e9e9e3f2e0715b39b101/crypto/mem.c/#L330
char *CRYPTO_strdup(const char *str, const char *file, int line) { char *ret = CRYPTO_malloc(strlen(str)+1, file, line); strcpy(ret, str); return ret; }
['char *CRYPTO_strdup(const char *str, const char *file, int line)\n\t{\n\tchar *ret = CRYPTO_malloc(strlen(str)+1, file, line);\n\tstrcpy(ret, str);\n\treturn ret;\n\t}', 'void *CRYPTO_malloc(int num, const char *file, int line)\n\t{\n\tvoid *ret = NULL;\n\tif (num <= 0) return NULL;\n\tallow_customize = 0;\n\tif (malloc_debug_func != NULL)\n\t\t{\n\t\tallow_customize_debug = 0;\n\t\tmalloc_debug_func(NULL, num, file, line, 0);\n\t\t}\n\tret = malloc_ex_func(num,file,line);\n#ifdef LEVITTE_DEBUG_MEM\n\tfprintf(stderr, "LEVITTE_DEBUG_MEM: > 0x%p (%d)\\n", ret, num);\n#endif\n\tif (malloc_debug_func != NULL)\n\t\tmalloc_debug_func(ret, num, file, line, 1);\n#ifndef OPENSSL_CPUID_OBJ\n if(ret && (num > 2048))\n\t{\textern unsigned char cleanse_ctr;\n ((unsigned char *)ret)[0] = cleanse_ctr;\n\t}\n#endif\n\treturn ret;\n\t}']
883
0
https://github.com/openssl/openssl/blob/abc9400e100632760727d150946f5269a0a5227b/crypto/pkcs12/p12_npas.c/#L139
static int newpass_p12(PKCS12 *p12, char *oldpass, char *newpass) { STACK_OF(PKCS7) *asafes, *newsafes; STACK_OF(PKCS12_SAFEBAG) *bags; int i, bagnid, pbe_nid, pbe_iter, pbe_saltlen; PKCS7 *p7, *p7new; ASN1_OCTET_STRING *p12_data_tmp = NULL, *macnew = NULL; unsigned char mac[EVP_MAX_MD_SIZE]; unsigned int maclen; if (!(asafes = M_PKCS12_unpack_authsafes(p12))) return 0; if(!(newsafes = sk_PKCS7_new(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 = M_PKCS12_unpack_p7data(p7); } else if (bagnid == NID_pkcs7_encrypted) { bags = M_PKCS12_unpack_p7encdata(p7, oldpass, -1); alg_get(p7->d.encrypted->enc_data->algorithm, &pbe_nid, &pbe_iter, &pbe_saltlen); } else continue; if (!bags) { sk_PKCS7_pop_free(asafes, PKCS7_free); return 0; } if (!newpass_bags(bags, oldpass, newpass)) { sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free); sk_PKCS7_pop_free(asafes, PKCS7_free); return 0; } if (bagnid == NID_pkcs7_data) p7new = PKCS12_pack_p7data(bags); else p7new = PKCS12_pack_p7encdata(pbe_nid, newpass, -1, NULL, pbe_saltlen, pbe_iter, bags); sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free); if(!p7new) { sk_PKCS7_pop_free(asafes, PKCS7_free); return 0; } sk_PKCS7_push(newsafes, p7new); } sk_PKCS7_pop_free(asafes, PKCS7_free); p12_data_tmp = p12->authsafes->d.data; if(!(p12->authsafes->d.data = ASN1_OCTET_STRING_new())) goto saferr; if(!M_PKCS12_pack_authsafes(p12, newsafes)) goto saferr; if(!PKCS12_gen_mac(p12, newpass, -1, mac, &maclen)) goto saferr; if(!(macnew = ASN1_OCTET_STRING_new())) goto saferr; if(!ASN1_OCTET_STRING_set(macnew, mac, maclen)) goto saferr; ASN1_OCTET_STRING_free(p12->mac->dinfo->digest); p12->mac->dinfo->digest = macnew; ASN1_OCTET_STRING_free(p12_data_tmp); return 1; saferr: ASN1_OCTET_STRING_free(p12->authsafes->d.data); ASN1_OCTET_STRING_free(macnew); p12->authsafes->d.data = p12_data_tmp; return 0; }
['static int newpass_p12(PKCS12 *p12, char *oldpass, char *newpass)\n{\n\tSTACK_OF(PKCS7) *asafes, *newsafes;\n\tSTACK_OF(PKCS12_SAFEBAG) *bags;\n\tint i, bagnid, pbe_nid, pbe_iter, pbe_saltlen;\n\tPKCS7 *p7, *p7new;\n\tASN1_OCTET_STRING *p12_data_tmp = NULL, *macnew = NULL;\n\tunsigned char mac[EVP_MAX_MD_SIZE];\n\tunsigned int maclen;\n\tif (!(asafes = M_PKCS12_unpack_authsafes(p12))) return 0;\n\tif(!(newsafes = sk_PKCS7_new(NULL))) return 0;\n\tfor (i = 0; i < sk_PKCS7_num (asafes); i++) {\n\t\tp7 = sk_PKCS7_value(asafes, i);\n\t\tbagnid = OBJ_obj2nid(p7->type);\n\t\tif (bagnid == NID_pkcs7_data) {\n\t\t\tbags = M_PKCS12_unpack_p7data(p7);\n\t\t} else if (bagnid == NID_pkcs7_encrypted) {\n\t\t\tbags = M_PKCS12_unpack_p7encdata(p7, oldpass, -1);\n\t\t\talg_get(p7->d.encrypted->enc_data->algorithm,\n\t\t\t\t&pbe_nid, &pbe_iter, &pbe_saltlen);\n\t\t} else continue;\n\t\tif (!bags) {\n\t\t\tsk_PKCS7_pop_free(asafes, PKCS7_free);\n\t\t\treturn 0;\n\t\t}\n\t \tif (!newpass_bags(bags, oldpass, newpass)) {\n\t\t\tsk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);\n\t\t\tsk_PKCS7_pop_free(asafes, PKCS7_free);\n\t\t\treturn 0;\n\t\t}\n\t\tif (bagnid == NID_pkcs7_data) p7new = PKCS12_pack_p7data(bags);\n\t\telse p7new = PKCS12_pack_p7encdata(pbe_nid, newpass, -1, NULL,\n\t\t\t\t\t\t pbe_saltlen, pbe_iter, bags);\n\t\tsk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);\n\t\tif(!p7new) {\n\t\t\tsk_PKCS7_pop_free(asafes, PKCS7_free);\n\t\t\treturn 0;\n\t\t}\n\t\tsk_PKCS7_push(newsafes, p7new);\n\t}\n\tsk_PKCS7_pop_free(asafes, PKCS7_free);\n\tp12_data_tmp = p12->authsafes->d.data;\n\tif(!(p12->authsafes->d.data = ASN1_OCTET_STRING_new())) goto saferr;\n\tif(!M_PKCS12_pack_authsafes(p12, newsafes)) goto saferr;\n\tif(!PKCS12_gen_mac(p12, newpass, -1, mac, &maclen)) goto saferr;\n\tif(!(macnew = ASN1_OCTET_STRING_new())) goto saferr;\n\tif(!ASN1_OCTET_STRING_set(macnew, mac, maclen)) goto saferr;\n\tASN1_OCTET_STRING_free(p12->mac->dinfo->digest);\n\tp12->mac->dinfo->digest = macnew;\n\tASN1_OCTET_STRING_free(p12_data_tmp);\n\treturn 1;\n\tsaferr:\n\tASN1_OCTET_STRING_free(p12->authsafes->d.data);\n\tASN1_OCTET_STRING_free(macnew);\n\tp12->authsafes->d.data = p12_data_tmp;\n\treturn 0;\n}']
884
0
https://github.com/libav/libav/blob/43bacd5b7d3d265a77cd29d8abb131057796aecc/libavformat/mpegts.c/#L691
static int read_sl_header(PESContext *pes, SLConfigDescr *sl, const uint8_t *buf, int buf_size) { GetBitContext gb; int au_start_flag = 0, au_end_flag = 0, ocr_flag = 0, idle_flag = 0; int padding_flag = 0, padding_bits = 0, inst_bitrate_flag = 0; int dts_flag = -1, cts_flag = -1; int64_t dts = AV_NOPTS_VALUE, cts = AV_NOPTS_VALUE; init_get_bits(&gb, buf, buf_size*8); if (sl->use_au_start) au_start_flag = get_bits1(&gb); if (sl->use_au_end) au_end_flag = get_bits1(&gb); if (!sl->use_au_start && !sl->use_au_end) au_start_flag = au_end_flag = 1; if (sl->ocr_len > 0) ocr_flag = get_bits1(&gb); if (sl->use_idle) idle_flag = get_bits1(&gb); if (sl->use_padding) padding_flag = get_bits1(&gb); if (padding_flag) padding_bits = get_bits(&gb, 3); if (!idle_flag && (!padding_flag || padding_bits != 0)) { if (sl->packet_seq_num_len) skip_bits_long(&gb, sl->packet_seq_num_len); if (sl->degr_prior_len) if (get_bits1(&gb)) skip_bits(&gb, sl->degr_prior_len); if (ocr_flag) skip_bits_long(&gb, sl->ocr_len); if (au_start_flag) { if (sl->use_rand_acc_pt) get_bits1(&gb); if (sl->au_seq_num_len > 0) skip_bits_long(&gb, sl->au_seq_num_len); if (sl->use_timestamps) { dts_flag = get_bits1(&gb); cts_flag = get_bits1(&gb); } } if (sl->inst_bitrate_len) inst_bitrate_flag = get_bits1(&gb); if (dts_flag == 1) dts = get_bits64(&gb, sl->timestamp_len); if (cts_flag == 1) cts = get_bits64(&gb, sl->timestamp_len); if (sl->au_len > 0) skip_bits_long(&gb, sl->au_len); if (inst_bitrate_flag) skip_bits_long(&gb, sl->inst_bitrate_len); } if (dts != AV_NOPTS_VALUE) pes->dts = dts; if (cts != AV_NOPTS_VALUE) pes->pts = cts; if (sl->timestamp_len && sl->timestamp_res) avpriv_set_pts_info(pes->st, sl->timestamp_len, 1, sl->timestamp_res); return (get_bits_count(&gb) + 7) >> 3; }
['static int read_sl_header(PESContext *pes, SLConfigDescr *sl, const uint8_t *buf, int buf_size)\n{\n GetBitContext gb;\n int au_start_flag = 0, au_end_flag = 0, ocr_flag = 0, idle_flag = 0;\n int padding_flag = 0, padding_bits = 0, inst_bitrate_flag = 0;\n int dts_flag = -1, cts_flag = -1;\n int64_t dts = AV_NOPTS_VALUE, cts = AV_NOPTS_VALUE;\n init_get_bits(&gb, buf, buf_size*8);\n if (sl->use_au_start)\n au_start_flag = get_bits1(&gb);\n if (sl->use_au_end)\n au_end_flag = get_bits1(&gb);\n if (!sl->use_au_start && !sl->use_au_end)\n au_start_flag = au_end_flag = 1;\n if (sl->ocr_len > 0)\n ocr_flag = get_bits1(&gb);\n if (sl->use_idle)\n idle_flag = get_bits1(&gb);\n if (sl->use_padding)\n padding_flag = get_bits1(&gb);\n if (padding_flag)\n padding_bits = get_bits(&gb, 3);\n if (!idle_flag && (!padding_flag || padding_bits != 0)) {\n if (sl->packet_seq_num_len)\n skip_bits_long(&gb, sl->packet_seq_num_len);\n if (sl->degr_prior_len)\n if (get_bits1(&gb))\n skip_bits(&gb, sl->degr_prior_len);\n if (ocr_flag)\n skip_bits_long(&gb, sl->ocr_len);\n if (au_start_flag) {\n if (sl->use_rand_acc_pt)\n get_bits1(&gb);\n if (sl->au_seq_num_len > 0)\n skip_bits_long(&gb, sl->au_seq_num_len);\n if (sl->use_timestamps) {\n dts_flag = get_bits1(&gb);\n cts_flag = get_bits1(&gb);\n }\n }\n if (sl->inst_bitrate_len)\n inst_bitrate_flag = get_bits1(&gb);\n if (dts_flag == 1)\n dts = get_bits64(&gb, sl->timestamp_len);\n if (cts_flag == 1)\n cts = get_bits64(&gb, sl->timestamp_len);\n if (sl->au_len > 0)\n skip_bits_long(&gb, sl->au_len);\n if (inst_bitrate_flag)\n skip_bits_long(&gb, sl->inst_bitrate_len);\n }\n if (dts != AV_NOPTS_VALUE)\n pes->dts = dts;\n if (cts != AV_NOPTS_VALUE)\n pes->pts = cts;\n if (sl->timestamp_len && sl->timestamp_res)\n avpriv_set_pts_info(pes->st, sl->timestamp_len, 1, sl->timestamp_res);\n return (get_bits_count(&gb) + 7) >> 3;\n}', 'static inline int init_get_bits(GetBitContext *s, const uint8_t *buffer,\n int bit_size)\n{\n int buffer_size;\n int ret = 0;\n if (bit_size > INT_MAX - 7 || bit_size < 0 || !buffer) {\n buffer_size = bit_size = 0;\n buffer = NULL;\n ret = AVERROR_INVALIDDATA;\n }\n buffer_size = (bit_size + 7) >> 3;\n s->buffer = buffer;\n s->size_in_bits = bit_size;\n#if !UNCHECKED_BITSTREAM_READER\n s->size_in_bits_plus8 = bit_size + 8;\n#endif\n s->buffer_end = buffer + buffer_size;\n s->index = 0;\n return ret;\n}', 'static inline unsigned int get_bits1(GetBitContext *s)\n{\n unsigned int index = s->index;\n uint8_t result = s->buffer[index >> 3];\n#ifdef BITSTREAM_READER_LE\n result >>= index & 7;\n result &= 1;\n#else\n result <<= index & 7;\n result >>= 8 - 1;\n#endif\n#if !UNCHECKED_BITSTREAM_READER\n if (s->index < s->size_in_bits_plus8)\n#endif\n index++;\n s->index = index;\n return result;\n}']
885
0
https://github.com/openssl/openssl/blob/8da94770f0a049497b1a52ee469cca1f4a13b1a7/crypto/dh/dh_pmeth.c/#L367
static int pkey_dh_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey) { DH *dh = NULL; DH_PKEY_CTX *dctx = ctx->data; BN_GENCB *pcb; int ret; if (dctx->rfc5114_param) { switch (dctx->rfc5114_param) { case 1: dh = DH_get_1024_160(); break; case 2: dh = DH_get_2048_224(); break; case 3: dh = DH_get_2048_256(); break; default: return -2; } EVP_PKEY_assign(pkey, EVP_PKEY_DHX, dh); return 1; } if (ctx->pkey_gencb) { pcb = BN_GENCB_new(); if (pcb == NULL) return 0; evp_pkey_set_cb_translate(pcb, ctx); } else pcb = NULL; #ifndef OPENSSL_NO_DSA if (dctx->use_dsa) { DSA *dsa_dh; dsa_dh = dsa_dh_generate(dctx, pcb); BN_GENCB_free(pcb); if (dsa_dh == NULL) return 0; dh = DSA_dup_DH(dsa_dh); DSA_free(dsa_dh); if (!dh) return 0; EVP_PKEY_assign(pkey, EVP_PKEY_DHX, dh); return 1; } #endif dh = DH_new(); if (dh == NULL) { BN_GENCB_free(pcb); return 0; } ret = DH_generate_parameters_ex(dh, dctx->prime_len, dctx->generator, pcb); BN_GENCB_free(pcb); if (ret) EVP_PKEY_assign_DH(pkey, dh); else DH_free(dh); return ret; }
['static int pkey_dh_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)\n{\n DH *dh = NULL;\n DH_PKEY_CTX *dctx = ctx->data;\n BN_GENCB *pcb;\n int ret;\n if (dctx->rfc5114_param) {\n switch (dctx->rfc5114_param) {\n case 1:\n dh = DH_get_1024_160();\n break;\n case 2:\n dh = DH_get_2048_224();\n break;\n case 3:\n dh = DH_get_2048_256();\n break;\n default:\n return -2;\n }\n EVP_PKEY_assign(pkey, EVP_PKEY_DHX, dh);\n return 1;\n }\n if (ctx->pkey_gencb) {\n pcb = BN_GENCB_new();\n if (pcb == NULL)\n return 0;\n evp_pkey_set_cb_translate(pcb, ctx);\n } else\n pcb = NULL;\n#ifndef OPENSSL_NO_DSA\n if (dctx->use_dsa) {\n DSA *dsa_dh;\n dsa_dh = dsa_dh_generate(dctx, pcb);\n BN_GENCB_free(pcb);\n if (dsa_dh == NULL)\n return 0;\n dh = DSA_dup_DH(dsa_dh);\n DSA_free(dsa_dh);\n if (!dh)\n return 0;\n EVP_PKEY_assign(pkey, EVP_PKEY_DHX, dh);\n return 1;\n }\n#endif\n dh = DH_new();\n if (dh == NULL) {\n BN_GENCB_free(pcb);\n return 0;\n }\n ret = DH_generate_parameters_ex(dh,\n dctx->prime_len, dctx->generator, pcb);\n BN_GENCB_free(pcb);\n if (ret)\n EVP_PKEY_assign_DH(pkey, dh);\n else\n DH_free(dh);\n return ret;\n}', 'make_dh(1024_160)', 'BIGNUM *BN_dup(const BIGNUM *a)\n{\n BIGNUM *t;\n if (a == NULL)\n return NULL;\n bn_check_top(a);\n t = BN_get_flags(a, BN_FLG_SECURE) ? BN_secure_new() : BN_new();\n if (t == NULL)\n return NULL;\n if (!BN_copy(t, a)) {\n BN_free(t);\n return NULL;\n }\n bn_check_top(t);\n return t;\n}', 'int BN_get_flags(const BIGNUM *b, int n)\n{\n return b->flags & n;\n}', 'BIGNUM *BN_new(void)\n{\n BIGNUM *ret;\n if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL) {\n BNerr(BN_F_BN_NEW, ERR_R_MALLOC_FAILURE);\n return (NULL);\n }\n ret->flags = BN_FLG_MALLOCED;\n bn_check_top(ret);\n return (ret);\n}', 'void *CRYPTO_zalloc(size_t num, const char *file, int line)\n{\n void *ret = CRYPTO_malloc(num, file, line);\n if (ret != NULL)\n memset(ret, 0, num);\n return ret;\n}', 'void *CRYPTO_malloc(size_t num, const char *file, int line)\n{\n void *ret = NULL;\n if (num <= 0)\n return NULL;\n allow_customize = 0;\n#ifdef CRYPTO_MDEBUG\n if (call_malloc_debug) {\n CRYPTO_mem_debug_malloc(NULL, num, 0, file, line);\n ret = malloc(num);\n CRYPTO_mem_debug_malloc(ret, num, 1, file, line);\n } else {\n ret = malloc(num);\n }\n#else\n (void)file;\n (void)line;\n ret = malloc(num);\n#endif\n#ifndef OPENSSL_CPUID_OBJ\n if (ret && (num > 2048)) {\n extern unsigned char cleanse_ctr;\n ((unsigned char *)ret)[0] = cleanse_ctr;\n }\n#endif\n return ret;\n}', 'int EVP_PKEY_assign(EVP_PKEY *pkey, int type, void *key)\n{\n if (pkey == NULL || !EVP_PKEY_set_type(pkey, type))\n return 0;\n pkey->pkey.ptr = key;\n return (key != NULL);\n}', 'int EVP_PKEY_set_type(EVP_PKEY *pkey, int type)\n{\n return pkey_set_type(pkey, type, NULL, -1);\n}']
886
0
https://github.com/openssl/openssl/blob/e0a8d1f94e018f8761cd026ff317b7e0cd80242e/crypto/x509/x509_vfy.c/#L190
int X509_verify_cert(X509_STORE_CTX *ctx) { X509 *x,*xtmp,*chain_ss=NULL; X509_NAME *xn; int depth,i,ok=0; int num; int (*cb)(); STACK_OF(X509) *sktmp=NULL; if (ctx->cert == NULL) { X509err(X509_F_X509_VERIFY_CERT,X509_R_NO_CERT_SET_FOR_US_TO_VERIFY); return -1; } cb=ctx->verify_cb; if (ctx->chain == NULL) { if ( ((ctx->chain=sk_X509_new_null()) == NULL) || (!sk_X509_push(ctx->chain,ctx->cert))) { X509err(X509_F_X509_VERIFY_CERT,ERR_R_MALLOC_FAILURE); goto end; } CRYPTO_add(&ctx->cert->references,1,CRYPTO_LOCK_X509); ctx->last_untrusted=1; } if (ctx->untrusted != NULL && (sktmp=sk_X509_dup(ctx->untrusted)) == NULL) { X509err(X509_F_X509_VERIFY_CERT,ERR_R_MALLOC_FAILURE); goto end; } num=sk_X509_num(ctx->chain); x=sk_X509_value(ctx->chain,num-1); depth=ctx->depth; for (;;) { if (depth < num) break; xn=X509_get_issuer_name(x); if (ctx->check_issued(ctx, x,x)) break; if (ctx->untrusted != NULL) { xtmp=find_issuer(ctx, sktmp,x); if (xtmp != NULL) { if (!sk_X509_push(ctx->chain,xtmp)) { X509err(X509_F_X509_VERIFY_CERT,ERR_R_MALLOC_FAILURE); goto end; } CRYPTO_add(&xtmp->references,1,CRYPTO_LOCK_X509); sk_X509_delete_ptr(sktmp,xtmp); ctx->last_untrusted++; x=xtmp; num++; continue; } } break; } i=sk_X509_num(ctx->chain); x=sk_X509_value(ctx->chain,i-1); xn = X509_get_subject_name(x); if (ctx->check_issued(ctx, x, x)) { if (sk_X509_num(ctx->chain) == 1) { ok = ctx->get_issuer(&xtmp, ctx, x); if ((ok <= 0) || X509_cmp(x, xtmp)) { ctx->error=X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT; ctx->current_cert=x; ctx->error_depth=i-1; if (ok == 1) X509_free(xtmp); ok=cb(0,ctx); if (!ok) goto end; } else { X509_free(x); x = xtmp; sk_X509_set(ctx->chain, i - 1, x); ctx->last_untrusted=0; } } else { chain_ss=sk_X509_pop(ctx->chain); ctx->last_untrusted--; num--; x=sk_X509_value(ctx->chain,num-1); } } for (;;) { if (depth < num) break; xn=X509_get_issuer_name(x); if (ctx->check_issued(ctx,x,x)) break; ok = ctx->get_issuer(&xtmp, ctx, x); if (ok < 0) return ok; if (ok == 0) break; x = xtmp; if (!sk_X509_push(ctx->chain,x)) { X509_free(xtmp); X509err(X509_F_X509_VERIFY_CERT,ERR_R_MALLOC_FAILURE); return 0; } num++; } xn=X509_get_issuer_name(x); if (!ctx->check_issued(ctx,x,x)) { if ((chain_ss == NULL) || !ctx->check_issued(ctx, x, chain_ss)) { if (ctx->last_untrusted >= num) ctx->error=X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY; else ctx->error=X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT; ctx->current_cert=x; } else { sk_X509_push(ctx->chain,chain_ss); num++; ctx->last_untrusted=num; ctx->current_cert=chain_ss; ctx->error=X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN; chain_ss=NULL; } ctx->error_depth=num-1; ok=cb(0,ctx); if (!ok) goto end; } if (ctx->purpose > 0) ok = check_chain_purpose(ctx); if (!ok) goto end; if (ctx->trust > 0) ok = check_trust(ctx); if (!ok) goto end; X509_get_pubkey_parameters(NULL,ctx->chain); ok = ctx->check_revocation(ctx); if(!ok) goto end; if (ctx->verify != NULL) ok=ctx->verify(ctx); else ok=internal_verify(ctx); if (0) { end: X509_get_pubkey_parameters(NULL,ctx->chain); } if (sktmp != NULL) sk_X509_free(sktmp); if (chain_ss != NULL) X509_free(chain_ss); return ok; }
['int X509_verify_cert(X509_STORE_CTX *ctx)\n\t{\n\tX509 *x,*xtmp,*chain_ss=NULL;\n\tX509_NAME *xn;\n\tint depth,i,ok=0;\n\tint num;\n\tint (*cb)();\n\tSTACK_OF(X509) *sktmp=NULL;\n\tif (ctx->cert == NULL)\n\t\t{\n\t\tX509err(X509_F_X509_VERIFY_CERT,X509_R_NO_CERT_SET_FOR_US_TO_VERIFY);\n\t\treturn -1;\n\t\t}\n\tcb=ctx->verify_cb;\n\tif (ctx->chain == NULL)\n\t\t{\n\t\tif (\t((ctx->chain=sk_X509_new_null()) == NULL) ||\n\t\t\t(!sk_X509_push(ctx->chain,ctx->cert)))\n\t\t\t{\n\t\t\tX509err(X509_F_X509_VERIFY_CERT,ERR_R_MALLOC_FAILURE);\n\t\t\tgoto end;\n\t\t\t}\n\t\tCRYPTO_add(&ctx->cert->references,1,CRYPTO_LOCK_X509);\n\t\tctx->last_untrusted=1;\n\t\t}\n\tif (ctx->untrusted != NULL\n\t && (sktmp=sk_X509_dup(ctx->untrusted)) == NULL)\n\t\t{\n\t\tX509err(X509_F_X509_VERIFY_CERT,ERR_R_MALLOC_FAILURE);\n\t\tgoto end;\n\t\t}\n\tnum=sk_X509_num(ctx->chain);\n\tx=sk_X509_value(ctx->chain,num-1);\n\tdepth=ctx->depth;\n\tfor (;;)\n\t\t{\n\t\tif (depth < num) break;\n\t\txn=X509_get_issuer_name(x);\n\t\tif (ctx->check_issued(ctx, x,x)) break;\n\t\tif (ctx->untrusted != NULL)\n\t\t\t{\n\t\t\txtmp=find_issuer(ctx, sktmp,x);\n\t\t\tif (xtmp != NULL)\n\t\t\t\t{\n\t\t\t\tif (!sk_X509_push(ctx->chain,xtmp))\n\t\t\t\t\t{\n\t\t\t\t\tX509err(X509_F_X509_VERIFY_CERT,ERR_R_MALLOC_FAILURE);\n\t\t\t\t\tgoto end;\n\t\t\t\t\t}\n\t\t\t\tCRYPTO_add(&xtmp->references,1,CRYPTO_LOCK_X509);\n\t\t\t\tsk_X509_delete_ptr(sktmp,xtmp);\n\t\t\t\tctx->last_untrusted++;\n\t\t\t\tx=xtmp;\n\t\t\t\tnum++;\n\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t}\n\t\tbreak;\n\t\t}\n\ti=sk_X509_num(ctx->chain);\n\tx=sk_X509_value(ctx->chain,i-1);\n\txn = X509_get_subject_name(x);\n\tif (ctx->check_issued(ctx, x, x))\n\t\t{\n\t\tif (sk_X509_num(ctx->chain) == 1)\n\t\t\t{\n\t\t\tok = ctx->get_issuer(&xtmp, ctx, x);\n\t\t\tif ((ok <= 0) || X509_cmp(x, xtmp))\n\t\t\t\t{\n\t\t\t\tctx->error=X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT;\n\t\t\t\tctx->current_cert=x;\n\t\t\t\tctx->error_depth=i-1;\n\t\t\t\tif (ok == 1) X509_free(xtmp);\n\t\t\t\tok=cb(0,ctx);\n\t\t\t\tif (!ok) goto end;\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\tX509_free(x);\n\t\t\t\tx = xtmp;\n\t\t\t\tsk_X509_set(ctx->chain, i - 1, x);\n\t\t\t\tctx->last_untrusted=0;\n\t\t\t\t}\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tchain_ss=sk_X509_pop(ctx->chain);\n\t\t\tctx->last_untrusted--;\n\t\t\tnum--;\n\t\t\tx=sk_X509_value(ctx->chain,num-1);\n\t\t\t}\n\t\t}\n\tfor (;;)\n\t\t{\n\t\tif (depth < num) break;\n\t\txn=X509_get_issuer_name(x);\n\t\tif (ctx->check_issued(ctx,x,x)) break;\n\t\tok = ctx->get_issuer(&xtmp, ctx, x);\n\t\tif (ok < 0) return ok;\n\t\tif (ok == 0) break;\n\t\tx = xtmp;\n\t\tif (!sk_X509_push(ctx->chain,x))\n\t\t\t{\n\t\t\tX509_free(xtmp);\n\t\t\tX509err(X509_F_X509_VERIFY_CERT,ERR_R_MALLOC_FAILURE);\n\t\t\treturn 0;\n\t\t\t}\n\t\tnum++;\n\t\t}\n\txn=X509_get_issuer_name(x);\n\tif (!ctx->check_issued(ctx,x,x))\n\t\t{\n\t\tif ((chain_ss == NULL) || !ctx->check_issued(ctx, x, chain_ss))\n\t\t\t{\n\t\t\tif (ctx->last_untrusted >= num)\n\t\t\t\tctx->error=X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY;\n\t\t\telse\n\t\t\t\tctx->error=X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT;\n\t\t\tctx->current_cert=x;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tsk_X509_push(ctx->chain,chain_ss);\n\t\t\tnum++;\n\t\t\tctx->last_untrusted=num;\n\t\t\tctx->current_cert=chain_ss;\n\t\t\tctx->error=X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN;\n\t\t\tchain_ss=NULL;\n\t\t\t}\n\t\tctx->error_depth=num-1;\n\t\tok=cb(0,ctx);\n\t\tif (!ok) goto end;\n\t\t}\n\tif (ctx->purpose > 0) ok = check_chain_purpose(ctx);\n\tif (!ok) goto end;\n\tif (ctx->trust > 0) ok = check_trust(ctx);\n\tif (!ok) goto end;\n\tX509_get_pubkey_parameters(NULL,ctx->chain);\n\tok = ctx->check_revocation(ctx);\n\tif(!ok) goto end;\n\tif (ctx->verify != NULL)\n\t\tok=ctx->verify(ctx);\n\telse\n\t\tok=internal_verify(ctx);\n\tif (0)\n\t\t{\nend:\n\t\tX509_get_pubkey_parameters(NULL,ctx->chain);\n\t\t}\n\tif (sktmp != NULL) sk_X509_free(sktmp);\n\tif (chain_ss != NULL) X509_free(chain_ss);\n\treturn ok;\n\t}', 'STACK *sk_dup(STACK *sk)\n\t{\n\tSTACK *ret;\n\tchar **s;\n\tif ((ret=sk_new(sk->comp)) == NULL) goto err;\n\ts=(char **)OPENSSL_realloc((char *)ret->data,\n\t\t(unsigned int)sizeof(char *)*sk->num_alloc);\n\tif (s == NULL) goto err;\n\tret->data=s;\n\tret->num=sk->num;\n\tmemcpy(ret->data,sk->data,sizeof(char *)*sk->num);\n\tret->sorted=sk->sorted;\n\tret->num_alloc=sk->num_alloc;\n\tret->comp=sk->comp;\n\treturn(ret);\nerr:\n\tif(ret)\n\t\tsk_free(ret);\n\treturn(NULL);\n\t}', 'void *CRYPTO_realloc(void *str, int num, const char *file, int line)\n\t{\n\tvoid *ret = NULL;\n\tif (realloc_debug_func != NULL)\n\t\trealloc_debug_func(str, NULL, num, file, line, 0);\n\tret = realloc_ex_func(str,num,file,line);\n#ifdef LEVITTE_DEBUG_MEM\n\tfprintf(stderr, "LEVITTE_DEBUG_MEM: | 0x%p -> 0x%p (%d)\\n", str, ret, num);\n#endif\n\tif (realloc_debug_func != NULL)\n\t\trealloc_debug_func(str, ret, num, file, line, 1);\n\treturn ret;\n\t}', 'int sk_num(const STACK *st)\n{\n\tif(st == NULL) return -1;\n\treturn st->num;\n}', 'char *sk_value(const STACK *st, int i)\n{\n\tif(st == NULL) return NULL;\n\treturn st->data[i];\n}', 'X509_NAME *X509_get_issuer_name(X509 *a)\n\t{\n\treturn(a->cert_info->issuer);\n\t}', 'X509_NAME *X509_get_subject_name(X509 *a)\n\t{\n\treturn(a->cert_info->subject);\n\t}']
887
0
https://github.com/openssl/openssl/blob/8da94770f0a049497b1a52ee469cca1f4a13b1a7/crypto/bn/bn_lib.c/#L765
int bn_cmp_words(const BN_ULONG *a, const BN_ULONG *b, int n) { int i; BN_ULONG aa, bb; 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_mod_exp2_mont(BIGNUM *rr, const BIGNUM *a1, const BIGNUM *p1,\n const BIGNUM *a2, const BIGNUM *p2, const BIGNUM *m,\n BN_CTX *ctx, BN_MONT_CTX *in_mont)\n{\n int i, j, bits, b, bits1, bits2, ret =\n 0, wpos1, wpos2, window1, window2, wvalue1, wvalue2;\n int r_is_one = 1;\n BIGNUM *d, *r;\n const BIGNUM *a_mod_m;\n BIGNUM *val1[TABLE_SIZE], *val2[TABLE_SIZE];\n BN_MONT_CTX *mont = NULL;\n bn_check_top(a1);\n bn_check_top(p1);\n bn_check_top(a2);\n bn_check_top(p2);\n bn_check_top(m);\n if (!(m->d[0] & 1)) {\n BNerr(BN_F_BN_MOD_EXP2_MONT, BN_R_CALLED_WITH_EVEN_MODULUS);\n return (0);\n }\n bits1 = BN_num_bits(p1);\n bits2 = BN_num_bits(p2);\n if ((bits1 == 0) && (bits2 == 0)) {\n ret = BN_one(rr);\n return ret;\n }\n bits = (bits1 > bits2) ? bits1 : bits2;\n BN_CTX_start(ctx);\n d = BN_CTX_get(ctx);\n r = BN_CTX_get(ctx);\n val1[0] = BN_CTX_get(ctx);\n val2[0] = BN_CTX_get(ctx);\n if (!d || !r || !val1[0] || !val2[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 window1 = BN_window_bits_for_exponent_size(bits1);\n window2 = BN_window_bits_for_exponent_size(bits2);\n if (a1->neg || BN_ucmp(a1, m) >= 0) {\n if (!BN_mod(val1[0], a1, m, ctx))\n goto err;\n a_mod_m = val1[0];\n } else\n a_mod_m = a1;\n if (BN_is_zero(a_mod_m)) {\n BN_zero(rr);\n ret = 1;\n goto err;\n }\n if (!BN_to_montgomery(val1[0], a_mod_m, mont, ctx))\n goto err;\n if (window1 > 1) {\n if (!BN_mod_mul_montgomery(d, val1[0], val1[0], mont, ctx))\n goto err;\n j = 1 << (window1 - 1);\n for (i = 1; i < j; i++) {\n if (((val1[i] = BN_CTX_get(ctx)) == NULL) ||\n !BN_mod_mul_montgomery(val1[i], val1[i - 1], d, mont, ctx))\n goto err;\n }\n }\n if (a2->neg || BN_ucmp(a2, m) >= 0) {\n if (!BN_mod(val2[0], a2, m, ctx))\n goto err;\n a_mod_m = val2[0];\n } else\n a_mod_m = a2;\n if (BN_is_zero(a_mod_m)) {\n BN_zero(rr);\n ret = 1;\n goto err;\n }\n if (!BN_to_montgomery(val2[0], a_mod_m, mont, ctx))\n goto err;\n if (window2 > 1) {\n if (!BN_mod_mul_montgomery(d, val2[0], val2[0], mont, ctx))\n goto err;\n j = 1 << (window2 - 1);\n for (i = 1; i < j; i++) {\n if (((val2[i] = BN_CTX_get(ctx)) == NULL) ||\n !BN_mod_mul_montgomery(val2[i], val2[i - 1], d, mont, ctx))\n goto err;\n }\n }\n r_is_one = 1;\n wvalue1 = 0;\n wvalue2 = 0;\n wpos1 = 0;\n wpos2 = 0;\n if (!BN_to_montgomery(r, BN_value_one(), mont, ctx))\n goto err;\n for (b = bits - 1; b >= 0; b--) {\n if (!r_is_one) {\n if (!BN_mod_mul_montgomery(r, r, r, mont, ctx))\n goto err;\n }\n if (!wvalue1)\n if (BN_is_bit_set(p1, b)) {\n i = b - window1 + 1;\n while (!BN_is_bit_set(p1, i))\n i++;\n wpos1 = i;\n wvalue1 = 1;\n for (i = b - 1; i >= wpos1; i--) {\n wvalue1 <<= 1;\n if (BN_is_bit_set(p1, i))\n wvalue1++;\n }\n }\n if (!wvalue2)\n if (BN_is_bit_set(p2, b)) {\n i = b - window2 + 1;\n while (!BN_is_bit_set(p2, i))\n i++;\n wpos2 = i;\n wvalue2 = 1;\n for (i = b - 1; i >= wpos2; i--) {\n wvalue2 <<= 1;\n if (BN_is_bit_set(p2, i))\n wvalue2++;\n }\n }\n if (wvalue1 && b == wpos1) {\n if (!BN_mod_mul_montgomery(r, r, val1[wvalue1 >> 1], mont, ctx))\n goto err;\n wvalue1 = 0;\n r_is_one = 0;\n }\n if (wvalue2 && b == wpos2) {\n if (!BN_mod_mul_montgomery(r, r, val2[wvalue2 >> 1], mont, ctx))\n goto err;\n wvalue2 = 0;\n r_is_one = 0;\n }\n }\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}', 'BIGNUM *BN_CTX_get(BN_CTX *ctx)\n{\n BIGNUM *ret;\n CTXDBG_ENTRY("BN_CTX_get", ctx);\n if (ctx->err_stack || ctx->too_many)\n return NULL;\n if ((ret = BN_POOL_get(&ctx->pool, ctx->flags)) == NULL) {\n ctx->too_many = 1;\n BNerr(BN_F_BN_CTX_GET, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n return NULL;\n }\n BN_zero(ret);\n ctx->used++;\n CTXDBG_RET(ctx, ret);\n return ret;\n}', 'int BN_set_word(BIGNUM *a, BN_ULONG w)\n{\n bn_check_top(a);\n if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)\n return (0);\n a->neg = 0;\n a->d[0] = w;\n a->top = (w ? 1 : 0);\n bn_check_top(a);\n return (1);\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_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)\n{\n int ret = 0;\n int top, al, bl;\n BIGNUM *rr;\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n int i;\n#endif\n#ifdef BN_RECURSION\n BIGNUM *t = NULL;\n int j = 0, k;\n#endif\n bn_check_top(a);\n bn_check_top(b);\n bn_check_top(r);\n al = a->top;\n bl = b->top;\n if ((al == 0) || (bl == 0)) {\n BN_zero(r);\n return (1);\n }\n top = al + bl;\n BN_CTX_start(ctx);\n if ((r == a) || (r == b)) {\n if ((rr = BN_CTX_get(ctx)) == NULL)\n goto err;\n } else\n rr = r;\n rr->neg = a->neg ^ b->neg;\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n i = al - bl;\n#endif\n#ifdef BN_MUL_COMBA\n if (i == 0) {\n# if 0\n if (al == 4) {\n if (bn_wexpand(rr, 8) == NULL)\n goto err;\n rr->top = 8;\n bn_mul_comba4(rr->d, a->d, b->d);\n goto end;\n }\n# endif\n if (al == 8) {\n if (bn_wexpand(rr, 16) == NULL)\n goto err;\n rr->top = 16;\n bn_mul_comba8(rr->d, a->d, b->d);\n goto end;\n }\n }\n#endif\n#ifdef BN_RECURSION\n if ((al >= BN_MULL_SIZE_NORMAL) && (bl >= BN_MULL_SIZE_NORMAL)) {\n if (i >= -1 && i <= 1) {\n if (i >= 0) {\n j = BN_num_bits_word((BN_ULONG)al);\n }\n if (i == -1) {\n j = BN_num_bits_word((BN_ULONG)bl);\n }\n j = 1 << (j - 1);\n assert(j <= al || j <= bl);\n k = j + j;\n t = BN_CTX_get(ctx);\n if (t == NULL)\n goto err;\n if (al > j || bl > j) {\n if (bn_wexpand(t, k * 4) == NULL)\n goto err;\n if (bn_wexpand(rr, k * 4) == NULL)\n goto err;\n bn_mul_part_recursive(rr->d, a->d, b->d,\n j, al - j, bl - j, t->d);\n } else {\n if (bn_wexpand(t, k * 2) == NULL)\n goto err;\n if (bn_wexpand(rr, k * 2) == NULL)\n goto err;\n bn_mul_recursive(rr->d, a->d, b->d, j, al - j, bl - j, t->d);\n }\n rr->top = top;\n goto end;\n }\n# if 0\n if (i == 1 && !BN_get_flags(b, BN_FLG_STATIC_DATA)) {\n BIGNUM *tmp_bn = (BIGNUM *)b;\n if (bn_wexpand(tmp_bn, al) == NULL)\n goto err;\n tmp_bn->d[bl] = 0;\n bl++;\n i--;\n } else if (i == -1 && !BN_get_flags(a, BN_FLG_STATIC_DATA)) {\n BIGNUM *tmp_bn = (BIGNUM *)a;\n if (bn_wexpand(tmp_bn, bl) == NULL)\n goto err;\n tmp_bn->d[al] = 0;\n al++;\n i++;\n }\n if (i == 0) {\n j = BN_num_bits_word((BN_ULONG)al);\n j = 1 << (j - 1);\n k = j + j;\n t = BN_CTX_get(ctx);\n if (al == j) {\n if (bn_wexpand(t, k * 2) == NULL)\n goto err;\n if (bn_wexpand(rr, k * 2) == NULL)\n goto err;\n bn_mul_recursive(rr->d, a->d, b->d, al, t->d);\n } else {\n if (bn_wexpand(t, k * 4) == NULL)\n goto err;\n if (bn_wexpand(rr, k * 4) == NULL)\n goto err;\n bn_mul_part_recursive(rr->d, a->d, b->d, al - j, j, t->d);\n }\n rr->top = top;\n goto end;\n }\n# endif\n }\n#endif\n if (bn_wexpand(rr, top) == NULL)\n goto err;\n rr->top = top;\n bn_mul_normal(rr->d, a->d, al, b->d, bl);\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n end:\n#endif\n bn_correct_top(rr);\n if (r != rr)\n BN_copy(r, rr);\n ret = 1;\n err:\n bn_check_top(r);\n BN_CTX_end(ctx);\n return (ret);\n}', 'void bn_mul_part_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n,\n int tna, int tnb, BN_ULONG *t)\n{\n int i, j, n2 = n * 2;\n int c1, c2, neg;\n BN_ULONG ln, lo, *p;\n if (n < 8) {\n bn_mul_normal(r, a, n + tna, b, n + tnb);\n return;\n }\n c1 = bn_cmp_part_words(a, &(a[n]), tna, n - tna);\n c2 = bn_cmp_part_words(&(b[n]), b, tnb, tnb - n);\n neg = 0;\n switch (c1 * 3 + c2) {\n case -4:\n bn_sub_part_words(t, &(a[n]), a, tna, tna - n);\n bn_sub_part_words(&(t[n]), b, &(b[n]), tnb, n - tnb);\n break;\n case -3:\n case -2:\n bn_sub_part_words(t, &(a[n]), a, tna, tna - n);\n bn_sub_part_words(&(t[n]), &(b[n]), b, tnb, tnb - n);\n neg = 1;\n break;\n case -1:\n case 0:\n case 1:\n case 2:\n bn_sub_part_words(t, a, &(a[n]), tna, n - tna);\n bn_sub_part_words(&(t[n]), b, &(b[n]), tnb, n - tnb);\n neg = 1;\n break;\n case 3:\n case 4:\n bn_sub_part_words(t, a, &(a[n]), tna, n - tna);\n bn_sub_part_words(&(t[n]), &(b[n]), b, tnb, tnb - n);\n break;\n }\n# if 0\n if (n == 4) {\n bn_mul_comba4(&(t[n2]), t, &(t[n]));\n bn_mul_comba4(r, a, b);\n bn_mul_normal(&(r[n2]), &(a[n]), tn, &(b[n]), tn);\n memset(&r[n2 + tn * 2], 0, sizeof(*r) * (n2 - tn * 2));\n } else\n# endif\n if (n == 8) {\n bn_mul_comba8(&(t[n2]), t, &(t[n]));\n bn_mul_comba8(r, a, b);\n bn_mul_normal(&(r[n2]), &(a[n]), tna, &(b[n]), tnb);\n memset(&r[n2 + tna + tnb], 0, sizeof(*r) * (n2 - tna - tnb));\n } else {\n p = &(t[n2 * 2]);\n bn_mul_recursive(&(t[n2]), t, &(t[n]), n, 0, 0, p);\n bn_mul_recursive(r, a, b, n, 0, 0, p);\n i = n / 2;\n if (tna > tnb)\n j = tna - i;\n else\n j = tnb - i;\n if (j == 0) {\n bn_mul_recursive(&(r[n2]), &(a[n]), &(b[n]),\n i, tna - i, tnb - i, p);\n memset(&r[n2 + i * 2], 0, sizeof(*r) * (n2 - i * 2));\n } else if (j > 0) {\n bn_mul_part_recursive(&(r[n2]), &(a[n]), &(b[n]),\n i, tna - i, tnb - i, p);\n memset(&(r[n2 + tna + tnb]), 0,\n sizeof(BN_ULONG) * (n2 - tna - tnb));\n } else {\n memset(&r[n2], 0, sizeof(*r) * n2);\n if (tna < BN_MUL_RECURSIVE_SIZE_NORMAL\n && tnb < BN_MUL_RECURSIVE_SIZE_NORMAL) {\n bn_mul_normal(&(r[n2]), &(a[n]), tna, &(b[n]), tnb);\n } else {\n for (;;) {\n i /= 2;\n if (i < tna || i < tnb) {\n bn_mul_part_recursive(&(r[n2]),\n &(a[n]), &(b[n]),\n i, tna - i, tnb - i, p);\n break;\n } else if (i == tna || i == tnb) {\n bn_mul_recursive(&(r[n2]),\n &(a[n]), &(b[n]),\n i, tna - i, tnb - i, p);\n break;\n }\n }\n }\n }\n }\n c1 = (int)(bn_add_words(t, r, &(r[n2]), n2));\n if (neg) {\n c1 -= (int)(bn_sub_words(&(t[n2]), t, &(t[n2]), n2));\n } else {\n c1 += (int)(bn_add_words(&(t[n2]), &(t[n2]), t, n2));\n }\n c1 += (int)(bn_add_words(&(r[n]), &(r[n]), &(t[n2]), n2));\n if (c1) {\n p = &(r[n + n2]);\n lo = *p;\n ln = (lo + c1) & BN_MASK2;\n *p = ln;\n if (ln < (BN_ULONG)c1) {\n do {\n p++;\n lo = *p;\n ln = (lo + 1) & BN_MASK2;\n *p = ln;\n } while (ln == 0);\n }\n }\n}', 'int bn_cmp_part_words(const BN_ULONG *a, const BN_ULONG *b, int cl, int dl)\n{\n int n, i;\n n = cl - 1;\n if (dl < 0) {\n for (i = dl; i < 0; i++) {\n if (b[n - i] != 0)\n return -1;\n }\n }\n if (dl > 0) {\n for (i = dl; i > 0; i--) {\n if (a[n + i] != 0)\n return 1;\n }\n }\n return bn_cmp_words(a, b, cl);\n}', 'int bn_cmp_words(const BN_ULONG *a, const BN_ULONG *b, int n)\n{\n int i;\n BN_ULONG aa, bb;\n aa = a[n - 1];\n bb = b[n - 1];\n if (aa != bb)\n return ((aa > bb) ? 1 : -1);\n for (i = n - 2; i >= 0; i--) {\n aa = a[i];\n bb = b[i];\n if (aa != bb)\n return ((aa > bb) ? 1 : -1);\n }\n return (0);\n}']
888
0
https://github.com/openssl/openssl/blob/d65c3615f6c658478503f4862f8055203a98038c/crypto/bn/bn_rand.c/#L82
static int bnrand(int pseudorand, BIGNUM *rnd, int bits, int top, int bottom) { unsigned char *buf = NULL; int ret = 0, bit, bytes, mask; time_t tim; 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) { BNerr(BN_F_BNRAND, ERR_R_MALLOC_FAILURE); goto err; } time(&tim); RAND_add(&tim, sizeof(tim), 0.0); if (RAND_bytes(buf, bytes) <= 0) goto err; if (pseudorand == 2) { int i; unsigned char c; for (i = 0; i < bytes; i++) { if (RAND_bytes(&c, 1) <= 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: BNerr(BN_F_BNRAND, BN_R_BITS_TOO_SMALL); return 0; }
['int test_gf2m_add(BIO *bp)\n{\n BIGNUM *a, *b, *c;\n int i, ret = 0;\n a = BN_new();\n b = BN_new();\n c = BN_new();\n for (i = 0; i < num0; i++) {\n BN_rand(a, 512, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY);\n BN_copy(b, BN_value_one());\n a->neg = rand_neg();\n b->neg = rand_neg();\n BN_GF2m_add(c, a, b);\n if ((BN_is_odd(a) && BN_is_odd(c))\n || (!BN_is_odd(a) && !BN_is_odd(c))) {\n fprintf(stderr, "GF(2^m) addition test (a) failed!\\n");\n goto err;\n }\n BN_GF2m_add(c, c, c);\n if (!BN_is_zero(c)) {\n fprintf(stderr, "GF(2^m) addition test (b) failed!\\n");\n goto err;\n }\n }\n ret = 1;\n err:\n BN_free(a);\n BN_free(b);\n BN_free(c);\n return ret;\n}', 'int BN_rand(BIGNUM *rnd, int bits, int top, int bottom)\n{\n return bnrand(0, rnd, bits, top, bottom);\n}', 'static int bnrand(int pseudorand, BIGNUM *rnd, int bits, int top, int bottom)\n{\n unsigned char *buf = NULL;\n int ret = 0, bit, bytes, mask;\n time_t tim;\n if (bits == 0) {\n if (top != BN_RAND_TOP_ANY || bottom != BN_RAND_BOTTOM_ANY)\n goto toosmall;\n BN_zero(rnd);\n return 1;\n }\n if (bits < 0 || (bits == 1 && top > 0))\n goto toosmall;\n bytes = (bits + 7) / 8;\n bit = (bits - 1) % 8;\n mask = 0xff << (bit + 1);\n buf = OPENSSL_malloc(bytes);\n if (buf == NULL) {\n BNerr(BN_F_BNRAND, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n time(&tim);\n RAND_add(&tim, sizeof(tim), 0.0);\n if (RAND_bytes(buf, bytes) <= 0)\n goto err;\n if (pseudorand == 2) {\n int i;\n unsigned char c;\n for (i = 0; i < bytes; i++) {\n if (RAND_bytes(&c, 1) <= 0)\n goto err;\n if (c >= 128 && i > 0)\n buf[i] = buf[i - 1];\n else if (c < 42)\n buf[i] = 0;\n else if (c < 84)\n buf[i] = 255;\n }\n }\n if (top >= 0) {\n if (top) {\n if (bit == 0) {\n buf[0] = 1;\n buf[1] |= 0x80;\n } else {\n buf[0] |= (3 << (bit - 1));\n }\n } else {\n buf[0] |= (1 << bit);\n }\n }\n buf[0] &= ~mask;\n if (bottom)\n buf[bytes - 1] |= 1;\n if (!BN_bin2bn(buf, bytes, rnd))\n goto err;\n ret = 1;\n err:\n OPENSSL_clear_free(buf, bytes);\n bn_check_top(rnd);\n return (ret);\ntoosmall:\n BNerr(BN_F_BNRAND, BN_R_BITS_TOO_SMALL);\n return 0;\n}', 'void *CRYPTO_malloc(size_t num, const char *file, int line)\n{\n void *ret = NULL;\n if (malloc_impl != NULL && malloc_impl != CRYPTO_malloc)\n return malloc_impl(num, file, line);\n if (num <= 0)\n return NULL;\n allow_customize = 0;\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n if (call_malloc_debug) {\n CRYPTO_mem_debug_malloc(NULL, num, 0, file, line);\n ret = malloc(num);\n CRYPTO_mem_debug_malloc(ret, num, 1, file, line);\n } else {\n ret = malloc(num);\n }\n#else\n osslargused(file); osslargused(line);\n ret = malloc(num);\n#endif\n return ret;\n}']
889
0
https://github.com/libav/libav/blob/2f99117f6ff24ce5be2abb9e014cb8b86c2aa0e0/libavcodec/bitstream.h/#L68
static inline void refill_32(BitstreamContext *bc) { if (bc->ptr >= bc->buffer_end) return; #ifdef BITSTREAM_READER_LE bc->bits = (uint64_t)AV_RL32(bc->ptr) << bc->bits_left | bc->bits; #else bc->bits = bc->bits | (uint64_t)AV_RB32(bc->ptr) << (32 - bc->bits_left); #endif bc->ptr += 4; bc->bits_left += 32; }
['static int jpg_decode_block(JPGContext *c, BitstreamContext *bc,\n int plane, int16_t *block)\n{\n int dc, val, pos;\n const int is_chroma = !!plane;\n const uint8_t *qmat = is_chroma ? chroma_quant : luma_quant;\n c->bdsp.clear_block(block);\n dc = bitstream_read_vlc(bc, c->dc_vlc[is_chroma].table, 9, 3);\n if (dc < 0)\n return AVERROR_INVALIDDATA;\n if (dc)\n dc = bitstream_read_xbits(bc, dc);\n dc = dc * qmat[0] + c->prev_dc[plane];\n block[0] = dc;\n c->prev_dc[plane] = dc;\n pos = 0;\n while (pos < 63) {\n val = bitstream_read_vlc(bc, c->ac_vlc[is_chroma].table, 9, 3);\n if (val < 0)\n return AVERROR_INVALIDDATA;\n pos += val >> 4;\n val &= 0xF;\n if (pos > 63)\n return val ? AVERROR_INVALIDDATA : 0;\n if (val) {\n int nbits = val;\n val = bitstream_read_xbits(bc, nbits);\n val *= qmat[ff_zigzag_direct[pos]];\n block[c->scantable.permutated[pos]] = val;\n }\n }\n return 0;\n}', 'static inline int bitstream_read_vlc(BitstreamContext *bc, VLC_TYPE (*table)[2],\n int bits, int max_depth)\n{\n int nb_bits;\n unsigned idx = bitstream_peek(bc, bits);\n int code = table[idx][0];\n int n = table[idx][1];\n if (max_depth > 1 && n < 0) {\n skip_remaining(bc, bits);\n code = set_idx(bc, code, &n, &nb_bits, table);\n if (max_depth > 2 && n < 0) {\n skip_remaining(bc, nb_bits);\n code = set_idx(bc, code, &n, &nb_bits, table);\n }\n }\n skip_remaining(bc, n);\n return code;\n}', 'static inline void skip_remaining(BitstreamContext *bc, unsigned n)\n{\n#ifdef BITSTREAM_READER_LE\n bc->bits >>= n;\n#else\n bc->bits <<= n;\n#endif\n bc->bits_left -= n;\n}', 'static inline int bitstream_read_xbits(BitstreamContext *bc, unsigned length)\n{\n int32_t cache = bitstream_peek(bc, 32);\n int sign = ~cache >> 31;\n skip_remaining(bc, length);\n return ((((uint32_t)(sign ^ cache)) >> (32 - length)) ^ sign) - sign;\n}', 'static inline unsigned bitstream_peek(BitstreamContext *bc, unsigned n)\n{\n if (n > bc->bits_left)\n refill_32(bc);\n return show_val(bc, n);\n}', 'static inline void refill_32(BitstreamContext *bc)\n{\n if (bc->ptr >= bc->buffer_end)\n return;\n#ifdef BITSTREAM_READER_LE\n bc->bits = (uint64_t)AV_RL32(bc->ptr) << bc->bits_left | bc->bits;\n#else\n bc->bits = bc->bits | (uint64_t)AV_RB32(bc->ptr) << (32 - bc->bits_left);\n#endif\n bc->ptr += 4;\n bc->bits_left += 32;\n}']
890
0
https://github.com/libav/libav/blob/7ed63ca2e7817e837facd29b01d25a1a69087916/libavcodec/h264_loopfilter.c/#L151
static void filter_mb_mbaff_edgev( H264Context *h, uint8_t *pix, int stride, int16_t bS[4], int bsi, int qp ) { int i; int index_a = qp + h->slice_alpha_c0_offset; int alpha = alpha_table[index_a]; int beta = beta_table[qp + h->slice_beta_offset]; for( i = 0; i < 8; i++, pix += stride) { const int bS_index = (i >> 1) * bsi; if( bS[bS_index] == 0 ) { continue; } if( bS[bS_index] < 4 ) { const int tc0 = tc0_table[index_a][bS[bS_index]]; const int p0 = pix[-1]; const int p1 = pix[-2]; const int p2 = pix[-3]; const int q0 = pix[0]; const int q1 = pix[1]; const int q2 = pix[2]; if( FFABS( p0 - q0 ) < alpha && FFABS( p1 - p0 ) < beta && FFABS( q1 - q0 ) < beta ) { int tc = tc0; int i_delta; if( FFABS( p2 - p0 ) < beta ) { if(tc0) pix[-2] = p1 + av_clip( ( p2 + ( ( p0 + q0 + 1 ) >> 1 ) - ( p1 << 1 ) ) >> 1, -tc0, tc0 ); tc++; } if( FFABS( q2 - q0 ) < beta ) { if(tc0) pix[1] = q1 + av_clip( ( q2 + ( ( p0 + q0 + 1 ) >> 1 ) - ( q1 << 1 ) ) >> 1, -tc0, tc0 ); tc++; } i_delta = av_clip( (((q0 - p0 ) << 2) + (p1 - q1) + 4) >> 3, -tc, tc ); pix[-1] = av_clip_uint8( p0 + i_delta ); pix[0] = av_clip_uint8( q0 - i_delta ); tprintf(h->s.avctx, "filter_mb_mbaff_edgev i:%d, qp:%d, indexA:%d, alpha:%d, beta:%d, tc:%d\n# bS:%d -> [%02x, %02x, %02x, %02x, %02x, %02x] =>[%02x, %02x, %02x, %02x]\n", i, qp[qp_index], index_a, alpha, beta, tc, bS[bS_index], pix[-3], p1, p0, q0, q1, pix[2], p1, pix[-1], pix[0], q1); } }else{ const int p0 = pix[-1]; const int p1 = pix[-2]; const int p2 = pix[-3]; const int q0 = pix[0]; const int q1 = pix[1]; const int q2 = pix[2]; if( FFABS( p0 - q0 ) < alpha && FFABS( p1 - p0 ) < beta && FFABS( q1 - q0 ) < beta ) { if(FFABS( p0 - q0 ) < (( alpha >> 2 ) + 2 )){ if( FFABS( p2 - p0 ) < beta) { const int p3 = pix[-4]; pix[-1] = ( p2 + 2*p1 + 2*p0 + 2*q0 + q1 + 4 ) >> 3; pix[-2] = ( p2 + p1 + p0 + q0 + 2 ) >> 2; pix[-3] = ( 2*p3 + 3*p2 + p1 + p0 + q0 + 4 ) >> 3; } else { pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2; } if( FFABS( q2 - q0 ) < beta) { const int q3 = pix[3]; pix[0] = ( p1 + 2*p0 + 2*q0 + 2*q1 + q2 + 4 ) >> 3; pix[1] = ( p0 + q0 + q1 + q2 + 2 ) >> 2; pix[2] = ( 2*q3 + 3*q2 + q1 + q0 + p0 + 4 ) >> 3; } else { pix[0] = ( 2*q1 + q0 + p1 + 2 ) >> 2; } }else{ pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2; pix[ 0] = ( 2*q1 + q0 + p1 + 2 ) >> 2; } tprintf(h->s.avctx, "filter_mb_mbaff_edgev i:%d, qp:%d, indexA:%d, alpha:%d, beta:%d\n# bS:4 -> [%02x, %02x, %02x, %02x, %02x, %02x] =>[%02x, %02x, %02x, %02x, %02x, %02x]\n", i, qp[qp_index], index_a, alpha, beta, p2, p1, p0, q0, q1, q2, pix[-3], pix[-2], pix[-1], pix[0], pix[1], pix[2]); } } } }
['void ff_h264_filter_mb( H264Context *h, int mb_x, int mb_y, uint8_t *img_y, uint8_t *img_cb, uint8_t *img_cr, unsigned int linesize, unsigned int uvlinesize) {\n MpegEncContext * const s = &h->s;\n const int mb_xy= mb_x + mb_y*s->mb_stride;\n const int mb_type = s->current_picture.mb_type[mb_xy];\n const int mvy_limit = IS_INTERLACED(mb_type) ? 2 : 4;\n int first_vertical_edge_done = 0;\n av_unused int dir;\n if (FRAME_MBAFF\n && IS_INTERLACED(mb_type^h->left_type[0])\n && h->left_type[0]) {\n DECLARE_ALIGNED_8(int16_t, bS)[8];\n int qp[2];\n int bqp[2];\n int rqp[2];\n int mb_qp, mbn0_qp, mbn1_qp;\n int i;\n first_vertical_edge_done = 1;\n if( IS_INTRA(mb_type) ) {\n AV_WN64A(&bS[0], 0x0004000400040004ULL);\n AV_WN64A(&bS[4], 0x0004000400040004ULL);\n } else {\n static const uint8_t offset[2][2][8]={\n {\n {7+8*0, 7+8*0, 7+8*0, 7+8*0, 7+8*1, 7+8*1, 7+8*1, 7+8*1},\n {7+8*2, 7+8*2, 7+8*2, 7+8*2, 7+8*3, 7+8*3, 7+8*3, 7+8*3},\n },{\n {7+8*0, 7+8*1, 7+8*2, 7+8*3, 7+8*0, 7+8*1, 7+8*2, 7+8*3},\n {7+8*0, 7+8*1, 7+8*2, 7+8*3, 7+8*0, 7+8*1, 7+8*2, 7+8*3},\n }\n };\n const uint8_t *off= offset[MB_FIELD][mb_y&1];\n for( i = 0; i < 8; i++ ) {\n int j= MB_FIELD ? i>>2 : i&1;\n int mbn_xy = h->left_mb_xy[j];\n int mbn_type= h->left_type[j];\n if( IS_INTRA( mbn_type ) )\n bS[i] = 4;\n else{\n bS[i] = 1 + !!(h->non_zero_count_cache[12+8*(i>>1)] |\n ((!h->pps.cabac && IS_8x8DCT(mbn_type)) ?\n (h->cbp_table[mbn_xy] & ((MB_FIELD ? (i&2) : (mb_y&1)) ? 8 : 2))\n :\n h->non_zero_count[mbn_xy][ off[i] ]));\n }\n }\n }\n mb_qp = s->current_picture.qscale_table[mb_xy];\n mbn0_qp = s->current_picture.qscale_table[h->left_mb_xy[0]];\n mbn1_qp = s->current_picture.qscale_table[h->left_mb_xy[1]];\n qp[0] = ( mb_qp + mbn0_qp + 1 ) >> 1;\n bqp[0] = ( get_chroma_qp( h, 0, mb_qp ) +\n get_chroma_qp( h, 0, mbn0_qp ) + 1 ) >> 1;\n rqp[0] = ( get_chroma_qp( h, 1, mb_qp ) +\n get_chroma_qp( h, 1, mbn0_qp ) + 1 ) >> 1;\n qp[1] = ( mb_qp + mbn1_qp + 1 ) >> 1;\n bqp[1] = ( get_chroma_qp( h, 0, mb_qp ) +\n get_chroma_qp( h, 0, mbn1_qp ) + 1 ) >> 1;\n rqp[1] = ( get_chroma_qp( h, 1, mb_qp ) +\n get_chroma_qp( h, 1, mbn1_qp ) + 1 ) >> 1;\n tprintf(s->avctx, "filter mb:%d/%d MBAFF, QPy:%d/%d, QPb:%d/%d QPr:%d/%d ls:%d uvls:%d", mb_x, mb_y, qp[0], qp[1], bqp[0], bqp[1], rqp[0], rqp[1], linesize, uvlinesize);\n { int i; for (i = 0; i < 8; i++) tprintf(s->avctx, " bS[%d]:%d", i, bS[i]); tprintf(s->avctx, "\\n"); }\n if(MB_FIELD){\n filter_mb_mbaff_edgev ( h, img_y , linesize, bS , 1, qp [0] );\n filter_mb_mbaff_edgev ( h, img_y + 8* linesize, linesize, bS+4, 1, qp [1] );\n filter_mb_mbaff_edgecv( h, img_cb, uvlinesize, bS , 1, bqp[0] );\n filter_mb_mbaff_edgecv( h, img_cb + 4*uvlinesize, uvlinesize, bS+4, 1, bqp[1] );\n filter_mb_mbaff_edgecv( h, img_cr, uvlinesize, bS , 1, rqp[0] );\n filter_mb_mbaff_edgecv( h, img_cr + 4*uvlinesize, uvlinesize, bS+4, 1, rqp[1] );\n }else{\n filter_mb_mbaff_edgev ( h, img_y , 2* linesize, bS , 2, qp [0] );\n filter_mb_mbaff_edgev ( h, img_y + linesize, 2* linesize, bS+1, 2, qp [1] );\n filter_mb_mbaff_edgecv( h, img_cb, 2*uvlinesize, bS , 2, bqp[0] );\n filter_mb_mbaff_edgecv( h, img_cb + uvlinesize, 2*uvlinesize, bS+1, 2, bqp[1] );\n filter_mb_mbaff_edgecv( h, img_cr, 2*uvlinesize, bS , 2, rqp[0] );\n filter_mb_mbaff_edgecv( h, img_cr + uvlinesize, 2*uvlinesize, bS+1, 2, rqp[1] );\n }\n }\n#if CONFIG_SMALL\n for( dir = 0; dir < 2; dir++ )\n filter_mb_dir(h, mb_x, mb_y, img_y, img_cb, img_cr, linesize, uvlinesize, mb_xy, mb_type, mvy_limit, dir ? 0 : first_vertical_edge_done, dir);\n#else\n filter_mb_dir(h, mb_x, mb_y, img_y, img_cb, img_cr, linesize, uvlinesize, mb_xy, mb_type, mvy_limit, first_vertical_edge_done, 0);\n filter_mb_dir(h, mb_x, mb_y, img_y, img_cb, img_cr, linesize, uvlinesize, mb_xy, mb_type, mvy_limit, 0, 1);\n#endif\n}', 'static void filter_mb_mbaff_edgev( H264Context *h, uint8_t *pix, int stride, int16_t bS[4], int bsi, int qp ) {\n int i;\n int index_a = qp + h->slice_alpha_c0_offset;\n int alpha = alpha_table[index_a];\n int beta = beta_table[qp + h->slice_beta_offset];\n for( i = 0; i < 8; i++, pix += stride) {\n const int bS_index = (i >> 1) * bsi;\n if( bS[bS_index] == 0 ) {\n continue;\n }\n if( bS[bS_index] < 4 ) {\n const int tc0 = tc0_table[index_a][bS[bS_index]];\n const int p0 = pix[-1];\n const int p1 = pix[-2];\n const int p2 = pix[-3];\n const int q0 = pix[0];\n const int q1 = pix[1];\n const int q2 = pix[2];\n if( FFABS( p0 - q0 ) < alpha &&\n FFABS( p1 - p0 ) < beta &&\n FFABS( q1 - q0 ) < beta ) {\n int tc = tc0;\n int i_delta;\n if( FFABS( p2 - p0 ) < beta ) {\n if(tc0)\n pix[-2] = p1 + av_clip( ( p2 + ( ( p0 + q0 + 1 ) >> 1 ) - ( p1 << 1 ) ) >> 1, -tc0, tc0 );\n tc++;\n }\n if( FFABS( q2 - q0 ) < beta ) {\n if(tc0)\n pix[1] = q1 + av_clip( ( q2 + ( ( p0 + q0 + 1 ) >> 1 ) - ( q1 << 1 ) ) >> 1, -tc0, tc0 );\n tc++;\n }\n i_delta = av_clip( (((q0 - p0 ) << 2) + (p1 - q1) + 4) >> 3, -tc, tc );\n pix[-1] = av_clip_uint8( p0 + i_delta );\n pix[0] = av_clip_uint8( q0 - i_delta );\n tprintf(h->s.avctx, "filter_mb_mbaff_edgev i:%d, qp:%d, indexA:%d, alpha:%d, beta:%d, tc:%d\\n# bS:%d -> [%02x, %02x, %02x, %02x, %02x, %02x] =>[%02x, %02x, %02x, %02x]\\n", i, qp[qp_index], index_a, alpha, beta, tc, bS[bS_index], pix[-3], p1, p0, q0, q1, pix[2], p1, pix[-1], pix[0], q1);\n }\n }else{\n const int p0 = pix[-1];\n const int p1 = pix[-2];\n const int p2 = pix[-3];\n const int q0 = pix[0];\n const int q1 = pix[1];\n const int q2 = pix[2];\n if( FFABS( p0 - q0 ) < alpha &&\n FFABS( p1 - p0 ) < beta &&\n FFABS( q1 - q0 ) < beta ) {\n if(FFABS( p0 - q0 ) < (( alpha >> 2 ) + 2 )){\n if( FFABS( p2 - p0 ) < beta)\n {\n const int p3 = pix[-4];\n pix[-1] = ( p2 + 2*p1 + 2*p0 + 2*q0 + q1 + 4 ) >> 3;\n pix[-2] = ( p2 + p1 + p0 + q0 + 2 ) >> 2;\n pix[-3] = ( 2*p3 + 3*p2 + p1 + p0 + q0 + 4 ) >> 3;\n } else {\n pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2;\n }\n if( FFABS( q2 - q0 ) < beta)\n {\n const int q3 = pix[3];\n pix[0] = ( p1 + 2*p0 + 2*q0 + 2*q1 + q2 + 4 ) >> 3;\n pix[1] = ( p0 + q0 + q1 + q2 + 2 ) >> 2;\n pix[2] = ( 2*q3 + 3*q2 + q1 + q0 + p0 + 4 ) >> 3;\n } else {\n pix[0] = ( 2*q1 + q0 + p1 + 2 ) >> 2;\n }\n }else{\n pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2;\n pix[ 0] = ( 2*q1 + q0 + p1 + 2 ) >> 2;\n }\n tprintf(h->s.avctx, "filter_mb_mbaff_edgev i:%d, qp:%d, indexA:%d, alpha:%d, beta:%d\\n# bS:4 -> [%02x, %02x, %02x, %02x, %02x, %02x] =>[%02x, %02x, %02x, %02x, %02x, %02x]\\n", i, qp[qp_index], index_a, alpha, beta, p2, p1, p0, q0, q1, q2, pix[-3], pix[-2], pix[-1], pix[0], pix[1], pix[2]);\n }\n }\n }\n}']
891
0
https://github.com/openssl/openssl/blob/2864df8f9d3264e19b49a246e272fb513f4c1be3/crypto/bn/bn_ctx.c/#L270
static unsigned int BN_STACK_pop(BN_STACK *st) { return st->indexes[--(st->depth)]; }
['static BIGNUM *dsa_mod_inverse_fermat(const BIGNUM *k, const BIGNUM *q,\n BN_CTX *ctx)\n{\n BIGNUM *res = NULL;\n BIGNUM *r, *e;\n if ((r = BN_new()) == NULL)\n return NULL;\n BN_CTX_start(ctx);\n if ((e = BN_CTX_get(ctx)) != NULL\n && BN_set_word(r, 2)\n && BN_sub(e, q, r)\n && BN_mod_exp_mont(r, k, e, q, ctx, NULL))\n res = r;\n else\n BN_free(r);\n BN_CTX_end(ctx);\n return res;\n}', 'void BN_CTX_start(BN_CTX *ctx)\n{\n CTXDBG("ENTER BN_CTX_start()", ctx);\n if (ctx->err_stack || ctx->too_many)\n ctx->err_stack++;\n else if (!BN_STACK_push(&ctx->stack, ctx->used)) {\n BNerr(BN_F_BN_CTX_START, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n ctx->err_stack++;\n }\n CTXDBG("LEAVE BN_CTX_start()", ctx);\n}', 'void BN_CTX_end(BN_CTX *ctx)\n{\n if (ctx == NULL)\n return;\n CTXDBG("ENTER BN_CTX_end()", ctx);\n if (ctx->err_stack)\n ctx->err_stack--;\n else {\n unsigned int fp = BN_STACK_pop(&ctx->stack);\n if (fp < ctx->used)\n BN_POOL_release(&ctx->pool, ctx->used - fp);\n ctx->used = fp;\n ctx->too_many = 0;\n }\n CTXDBG("LEAVE BN_CTX_end()", ctx);\n}', 'static unsigned int BN_STACK_pop(BN_STACK *st)\n{\n return st->indexes[--(st->depth)];\n}']
892
0
https://github.com/openssl/openssl/blob/6bc62a620e715f7580651ca932eab052aa527886/crypto/bn/bn_ctx.c/#L268
static unsigned int BN_STACK_pop(BN_STACK *st) { return st->indexes[--(st->depth)]; }
['int bn_miller_rabin_is_prime(const BIGNUM *w, int iterations, BN_CTX *ctx,\n BN_GENCB *cb, int enhanced, int *status)\n{\n int i, j, a, ret = 0;\n BIGNUM *g, *w1, *w3, *x, *m, *z, *b;\n BN_MONT_CTX *mont = NULL;\n if (!BN_is_odd(w))\n return 0;\n BN_CTX_start(ctx);\n g = BN_CTX_get(ctx);\n w1 = BN_CTX_get(ctx);\n w3 = BN_CTX_get(ctx);\n x = BN_CTX_get(ctx);\n m = BN_CTX_get(ctx);\n z = BN_CTX_get(ctx);\n b = BN_CTX_get(ctx);\n if (!(b != NULL\n && BN_copy(w1, w)\n && BN_sub_word(w1, 1)\n && BN_copy(w3, w)\n && BN_sub_word(w3, 3)))\n goto err;\n if (BN_is_zero(w3) || BN_is_negative(w3))\n goto err;\n a = 1;\n while (!BN_is_bit_set(w1, a))\n a++;\n if (!BN_rshift(m, w1, a))\n goto err;\n mont = BN_MONT_CTX_new();\n if (mont == NULL || !BN_MONT_CTX_set(mont, w, ctx))\n goto err;\n if (iterations == BN_prime_checks)\n iterations = BN_prime_checks_for_size(BN_num_bits(w));\n for (i = 0; i < iterations; ++i) {\n if (!BN_priv_rand_range(b, w3) || !BN_add_word(b, 2))\n goto err;\n if (enhanced) {\n if (!BN_gcd(g, b, w, ctx))\n goto err;\n if (!BN_is_one(g)) {\n *status = BN_PRIMETEST_COMPOSITE_WITH_FACTOR;\n ret = 1;\n goto err;\n }\n }\n if (!BN_mod_exp_mont(z, b, m, w, ctx, mont))\n goto err;\n if (BN_is_one(z) || BN_cmp(z, w1) == 0)\n goto outer_loop;\n for (j = 1; j < a ; ++j) {\n if (!BN_copy(x, z) || !BN_mod_mul(z, x, x, w, ctx))\n goto err;\n if (BN_cmp(z, w1) == 0)\n goto outer_loop;\n if (BN_is_one(z))\n goto composite;\n }\n if (!BN_GENCB_call(cb, 1, i))\n goto err;\n if (!BN_copy(x, z) || !BN_mod_mul(z, x, x, w, ctx))\n goto err;\n if (BN_is_one(z))\n goto composite;\n if (!BN_copy(x, z))\n goto err;\ncomposite:\n if (enhanced) {\n if (!BN_sub_word(x, 1) || !BN_gcd(g, x, w, ctx))\n goto err;\n if (BN_is_one(g))\n *status = BN_PRIMETEST_COMPOSITE_NOT_POWER_OF_PRIME;\n else\n *status = BN_PRIMETEST_COMPOSITE_WITH_FACTOR;\n } else {\n *status = BN_PRIMETEST_COMPOSITE;\n }\n ret = 1;\n goto err;\nouter_loop: ;\n }\n *status = BN_PRIMETEST_PROBABLY_PRIME;\n ret = 1;\nerr:\n BN_clear(g);\n BN_clear(w1);\n BN_clear(w3);\n BN_clear(x);\n BN_clear(m);\n BN_clear(z);\n BN_clear(b);\n BN_CTX_end(ctx);\n BN_MONT_CTX_free(mont);\n return ret;\n}', 'void BN_CTX_start(BN_CTX *ctx)\n{\n CTXDBG("ENTER BN_CTX_start()", ctx);\n if (ctx->err_stack || ctx->too_many)\n ctx->err_stack++;\n else if (!BN_STACK_push(&ctx->stack, ctx->used)) {\n BNerr(BN_F_BN_CTX_START, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n ctx->err_stack++;\n }\n CTXDBG("LEAVE BN_CTX_start()", ctx);\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}', '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 || BN_get_flags(a, BN_FLG_CONSTTIME) != 0\n || BN_get_flags(m, 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_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 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 (val[0] == 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 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_to_mont_fixed_top(val[0], aa, mont, ctx))\n goto err;\n window = BN_window_bits_for_exponent_size(bits);\n if (window > 1) {\n if (!bn_mul_mont_fixed_top(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_mul_mont_fixed_top(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 r->flags |= BN_FLG_FIXED_TOP;\n } else\n#endif\n if (!bn_to_mont_fixed_top(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_mul_mont_fixed_top(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_mul_mont_fixed_top(r, r, r, mont, ctx))\n goto err;\n }\n if (!bn_mul_mont_fixed_top(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_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m,\n BN_CTX *ctx)\n{\n BIGNUM *t;\n int ret = 0;\n bn_check_top(a);\n bn_check_top(b);\n bn_check_top(m);\n BN_CTX_start(ctx);\n if ((t = BN_CTX_get(ctx)) == NULL)\n goto err;\n if (a == b) {\n if (!BN_sqr(t, a, ctx))\n goto err;\n } else {\n if (!BN_mul(t, a, b, ctx))\n goto err;\n }\n if (!BN_nnmod(r, t, m, ctx))\n goto err;\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 ret = bn_sqr_fixed_top(r, a, ctx);\n bn_correct_top(r);\n bn_check_top(r);\n return ret;\n}', 'int bn_sqr_fixed_top(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 == NULL || tmp == NULL)\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 rr->top = max;\n rr->flags |= BN_FLG_FIXED_TOP;\n if (r != rr && BN_copy(r, rr) == NULL)\n goto err;\n ret = 1;\n err:\n bn_check_top(rr);\n bn_check_top(tmp);\n BN_CTX_end(ctx);\n return ret;\n}', 'int BN_nnmod(BIGNUM *r, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx)\n{\n if (!(BN_mod(r, m, d, ctx)))\n return 0;\n if (!r->neg)\n return 1;\n return (d->neg ? BN_sub : BN_add) (r, r, d);\n}', 'int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,\n BN_CTX *ctx)\n{\n int ret;\n if (BN_is_zero(divisor)) {\n BNerr(BN_F_BN_DIV, BN_R_DIV_BY_ZERO);\n return 0;\n }\n if (divisor->d[divisor->top - 1] == 0) {\n BNerr(BN_F_BN_DIV, BN_R_NOT_INITIALIZED);\n return 0;\n }\n ret = bn_div_fixed_top(dv, rm, num, divisor, ctx);\n if (ret) {\n if (dv != NULL)\n bn_correct_top(dv);\n if (rm != NULL)\n bn_correct_top(rm);\n }\n return ret;\n}', 'int bn_div_fixed_top(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num,\n const BIGNUM *divisor, BN_CTX *ctx)\n{\n int norm_shift, i, j, loop;\n BIGNUM *tmp, *snum, *sdiv, *res;\n BN_ULONG *resp, *wnum, *wnumtop;\n BN_ULONG d0, d1;\n int num_n, div_n;\n assert(divisor->top > 0 && divisor->d[divisor->top - 1] != 0);\n bn_check_top(num);\n bn_check_top(divisor);\n bn_check_top(dv);\n bn_check_top(rm);\n BN_CTX_start(ctx);\n res = (dv == NULL) ? BN_CTX_get(ctx) : dv;\n tmp = BN_CTX_get(ctx);\n snum = BN_CTX_get(ctx);\n sdiv = BN_CTX_get(ctx);\n if (sdiv == NULL)\n goto err;\n if (!BN_copy(sdiv, divisor))\n goto err;\n norm_shift = bn_left_align(sdiv);\n sdiv->neg = 0;\n if (!(bn_lshift_fixed_top(snum, num, norm_shift)))\n goto err;\n div_n = sdiv->top;\n num_n = snum->top;\n if (num_n <= div_n) {\n if (bn_wexpand(snum, div_n + 1) == NULL)\n goto err;\n memset(&(snum->d[num_n]), 0, (div_n - num_n + 1) * sizeof(BN_ULONG));\n snum->top = num_n = div_n + 1;\n }\n loop = num_n - div_n;\n wnum = &(snum->d[loop]);\n wnumtop = &(snum->d[num_n - 1]);\n d0 = sdiv->d[div_n - 1];\n d1 = (div_n == 1) ? 0 : sdiv->d[div_n - 2];\n if (!bn_wexpand(res, loop))\n goto err;\n res->neg = (num->neg ^ divisor->neg);\n res->top = loop;\n res->flags |= BN_FLG_FIXED_TOP;\n resp = &(res->d[loop]);\n if (!bn_wexpand(tmp, (div_n + 1)))\n goto err;\n for (i = 0; i < loop; i++, wnumtop--) {\n BN_ULONG q, l0;\n# if defined(BN_DIV3W)\n q = bn_div_3_words(wnumtop, d1, d0);\n# else\n BN_ULONG n0, n1, rem = 0;\n n0 = wnumtop[0];\n n1 = wnumtop[-1];\n if (n0 == d0)\n q = BN_MASK2;\n else {\n BN_ULONG n2 = (wnumtop == wnum) ? 0 : wnumtop[-2];\n# ifdef BN_LLONG\n BN_ULLONG t2;\n# if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)\n q = (BN_ULONG)(((((BN_ULLONG) n0) << BN_BITS2) | n1) / d0);\n# else\n q = bn_div_words(n0, n1, d0);\n# endif\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n t2 = (BN_ULLONG) d1 *q;\n for (;;) {\n if (t2 <= ((((BN_ULLONG) rem) << BN_BITS2) | n2))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n t2 -= d1;\n }\n# else\n BN_ULONG t2l, t2h;\n q = bn_div_words(n0, n1, d0);\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n# if defined(BN_UMULT_LOHI)\n BN_UMULT_LOHI(t2l, t2h, d1, q);\n# elif defined(BN_UMULT_HIGH)\n t2l = d1 * q;\n t2h = BN_UMULT_HIGH(d1, q);\n# else\n {\n BN_ULONG ql, qh;\n t2l = LBITS(d1);\n t2h = HBITS(d1);\n ql = LBITS(q);\n qh = HBITS(q);\n mul64(t2l, t2h, ql, qh);\n }\n# endif\n for (;;) {\n if ((t2h < rem) || ((t2h == rem) && (t2l <= n2)))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n if (t2l < d1)\n t2h--;\n t2l -= d1;\n }\n# endif\n }\n# endif\n l0 = bn_mul_words(tmp->d, sdiv->d, div_n, q);\n tmp->d[div_n] = l0;\n wnum--;\n l0 = bn_sub_words(wnum, wnum, tmp->d, div_n + 1);\n q -= l0;\n for (l0 = 0 - l0, j = 0; j < div_n; j++)\n tmp->d[j] = sdiv->d[j] & l0;\n l0 = bn_add_words(wnum, wnum, tmp->d, div_n);\n (*wnumtop) += l0;\n assert((*wnumtop) == 0);\n *--resp = q;\n }\n snum->neg = num->neg;\n snum->top = div_n;\n snum->flags |= BN_FLG_FIXED_TOP;\n if (rm != NULL)\n bn_rshift_fixed_top(rm, snum, norm_shift);\n BN_CTX_end(ctx);\n return 1;\n err:\n bn_check_top(rm);\n BN_CTX_end(ctx);\n return 0;\n}', 'void BN_CTX_end(BN_CTX *ctx)\n{\n CTXDBG("ENTER BN_CTX_end()", ctx);\n if (ctx->err_stack)\n ctx->err_stack--;\n else {\n unsigned int fp = BN_STACK_pop(&ctx->stack);\n if (fp < ctx->used)\n BN_POOL_release(&ctx->pool, ctx->used - fp);\n ctx->used = fp;\n ctx->too_many = 0;\n }\n CTXDBG("LEAVE BN_CTX_end()", ctx);\n}', 'static unsigned int BN_STACK_pop(BN_STACK *st)\n{\n return st->indexes[--(st->depth)];\n}']
893
0
https://github.com/openssl/openssl/blob/9c46f4b9cd4912b61cb546c48b678488d7f26ed6/apps/speed.c/#L1622
int MAIN(int argc, char **argv) { unsigned char *buf_malloc = NULL, *buf2_malloc = NULL; unsigned char *buf = NULL, *buf2 = NULL; int mret = 1; long count = 0, save_count = 0; int i, j, k; # if !defined(OPENSSL_NO_RSA) || !defined(OPENSSL_NO_DSA) long rsa_count; # endif # ifndef OPENSSL_NO_RSA unsigned rsa_num; # endif unsigned char md[EVP_MAX_MD_SIZE]; # ifndef OPENSSL_NO_MD2 unsigned char md2[MD2_DIGEST_LENGTH]; # endif # ifndef OPENSSL_NO_MDC2 unsigned char mdc2[MDC2_DIGEST_LENGTH]; # endif # ifndef OPENSSL_NO_MD4 unsigned char md4[MD4_DIGEST_LENGTH]; # endif # ifndef OPENSSL_NO_MD5 unsigned char md5[MD5_DIGEST_LENGTH]; unsigned char hmac[MD5_DIGEST_LENGTH]; # endif # ifndef OPENSSL_NO_SHA unsigned char sha[SHA_DIGEST_LENGTH]; # ifndef OPENSSL_NO_SHA256 unsigned char sha256[SHA256_DIGEST_LENGTH]; # endif # ifndef OPENSSL_NO_SHA512 unsigned char sha512[SHA512_DIGEST_LENGTH]; # endif # endif # ifndef OPENSSL_NO_WHIRLPOOL unsigned char whirlpool[WHIRLPOOL_DIGEST_LENGTH]; # endif # ifndef OPENSSL_NO_RMD160 unsigned char rmd160[RIPEMD160_DIGEST_LENGTH]; # endif # ifndef OPENSSL_NO_RC4 RC4_KEY rc4_ks; # endif # ifndef OPENSSL_NO_RC5 RC5_32_KEY rc5_ks; # endif # ifndef OPENSSL_NO_RC2 RC2_KEY rc2_ks; # endif # ifndef OPENSSL_NO_IDEA IDEA_KEY_SCHEDULE idea_ks; # endif # ifndef OPENSSL_NO_SEED SEED_KEY_SCHEDULE seed_ks; # endif # ifndef OPENSSL_NO_BF BF_KEY bf_ks; # endif # ifndef OPENSSL_NO_CAST CAST_KEY cast_ks; # endif static const unsigned char key16[16] = { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12 }; # ifndef OPENSSL_NO_AES static const unsigned char key24[24] = { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34 }; static const unsigned char key32[32] = { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56 }; # endif # ifndef OPENSSL_NO_CAMELLIA static const unsigned char ckey24[24] = { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34 }; static const unsigned char ckey32[32] = { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56 }; # endif # ifndef OPENSSL_NO_AES # define MAX_BLOCK_SIZE 128 # else # define MAX_BLOCK_SIZE 64 # endif unsigned char DES_iv[8]; unsigned char iv[2 * MAX_BLOCK_SIZE / 8]; # ifndef OPENSSL_NO_DES static DES_cblock key = { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0 }; static DES_cblock key2 = { 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12 }; static DES_cblock key3 = { 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34 }; DES_key_schedule sch; DES_key_schedule sch2; DES_key_schedule sch3; # endif # ifndef OPENSSL_NO_AES AES_KEY aes_ks1, aes_ks2, aes_ks3; # endif # ifndef OPENSSL_NO_CAMELLIA CAMELLIA_KEY camellia_ks1, camellia_ks2, camellia_ks3; # endif # define D_MD2 0 # define D_MDC2 1 # define D_MD4 2 # define D_MD5 3 # define D_HMAC 4 # define D_SHA1 5 # define D_RMD160 6 # define D_RC4 7 # define D_CBC_DES 8 # define D_EDE3_DES 9 # define D_CBC_IDEA 10 # define D_CBC_SEED 11 # define D_CBC_RC2 12 # define D_CBC_RC5 13 # define D_CBC_BF 14 # define D_CBC_CAST 15 # define D_CBC_128_AES 16 # define D_CBC_192_AES 17 # define D_CBC_256_AES 18 # define D_CBC_128_CML 19 # define D_CBC_192_CML 20 # define D_CBC_256_CML 21 # define D_EVP 22 # define D_SHA256 23 # define D_SHA512 24 # define D_WHIRLPOOL 25 # define D_IGE_128_AES 26 # define D_IGE_192_AES 27 # define D_IGE_256_AES 28 # define D_GHASH 29 double d = 0.0; long c[ALGOR_NUM][SIZE_NUM]; # ifndef OPENSSL_SYS_WIN32 # endif # define R_DSA_512 0 # define R_DSA_1024 1 # define R_DSA_2048 2 # define R_RSA_512 0 # define R_RSA_1024 1 # define R_RSA_2048 2 # define R_RSA_3072 3 # define R_RSA_4096 4 # define R_RSA_7680 5 # define R_RSA_15360 6 # define R_EC_P160 0 # define R_EC_P192 1 # define R_EC_P224 2 # define R_EC_P256 3 # define R_EC_P384 4 # define R_EC_P521 5 # define R_EC_K163 6 # define R_EC_K233 7 # define R_EC_K283 8 # define R_EC_K409 9 # define R_EC_K571 10 # define R_EC_B163 11 # define R_EC_B233 12 # define R_EC_B283 13 # define R_EC_B409 14 # define R_EC_B571 15 # ifndef OPENSSL_NO_RSA RSA *rsa_key[RSA_NUM]; long rsa_c[RSA_NUM][2]; static unsigned int rsa_bits[RSA_NUM] = { 512, 1024, 2048, 3072, 4096, 7680, 15360 }; static unsigned char *rsa_data[RSA_NUM] = { test512, test1024, test2048, test3072, test4096, test7680, test15360 }; static int rsa_data_length[RSA_NUM] = { sizeof(test512), sizeof(test1024), sizeof(test2048), sizeof(test3072), sizeof(test4096), sizeof(test7680), sizeof(test15360) }; # endif # ifndef OPENSSL_NO_DSA DSA *dsa_key[DSA_NUM]; long dsa_c[DSA_NUM][2]; static unsigned int dsa_bits[DSA_NUM] = { 512, 1024, 2048 }; # endif # ifndef OPENSSL_NO_EC static unsigned int test_curves[EC_NUM] = { NID_secp160r1, NID_X9_62_prime192v1, NID_secp224r1, NID_X9_62_prime256v1, NID_secp384r1, NID_secp521r1, NID_sect163k1, NID_sect233k1, NID_sect283k1, NID_sect409k1, NID_sect571k1, NID_sect163r2, NID_sect233r1, NID_sect283r1, NID_sect409r1, NID_sect571r1 }; static const char *test_curves_names[EC_NUM] = { "secp160r1", "nistp192", "nistp224", "nistp256", "nistp384", "nistp521", "nistk163", "nistk233", "nistk283", "nistk409", "nistk571", "nistb163", "nistb233", "nistb283", "nistb409", "nistb571" }; static int test_curves_bits[EC_NUM] = { 160, 192, 224, 256, 384, 521, 163, 233, 283, 409, 571, 163, 233, 283, 409, 571 }; # endif # ifndef OPENSSL_NO_ECDSA unsigned char ecdsasig[256]; unsigned int ecdsasiglen; EC_KEY *ecdsa[EC_NUM]; long ecdsa_c[EC_NUM][2]; # endif # ifndef OPENSSL_NO_ECDH EC_KEY *ecdh_a[EC_NUM], *ecdh_b[EC_NUM]; unsigned char secret_a[MAX_ECDH_SIZE], secret_b[MAX_ECDH_SIZE]; int secret_size_a, secret_size_b; int ecdh_checks = 0; int secret_idx = 0; long ecdh_c[EC_NUM][2]; # endif int rsa_doit[RSA_NUM]; int dsa_doit[DSA_NUM]; # ifndef OPENSSL_NO_ECDSA int ecdsa_doit[EC_NUM]; # endif # ifndef OPENSSL_NO_ECDH int ecdh_doit[EC_NUM]; # endif int doit[ALGOR_NUM]; int pr_header = 0; const EVP_CIPHER *evp_cipher = NULL; const EVP_MD *evp_md = NULL; int decrypt = 0; # ifndef NO_FORK int multi = 0; # endif int multiblock = 0; int misalign = MAX_MISALIGNMENT + 1; # ifndef TIMES usertime = -1; # endif apps_startup(); memset(results, 0, sizeof(results)); # ifndef OPENSSL_NO_DSA memset(dsa_key, 0, sizeof(dsa_key)); # endif # ifndef OPENSSL_NO_ECDSA for (i = 0; i < EC_NUM; i++) ecdsa[i] = NULL; # endif # ifndef OPENSSL_NO_ECDH for (i = 0; i < EC_NUM; i++) { ecdh_a[i] = NULL; ecdh_b[i] = NULL; } # endif if (bio_err == NULL) if ((bio_err = BIO_new(BIO_s_file())) != NULL) BIO_set_fp(bio_err, stderr, BIO_NOCLOSE | BIO_FP_TEXT); if (!load_config(bio_err, NULL)) goto end; # ifndef OPENSSL_NO_RSA memset(rsa_key, 0, sizeof(rsa_key)); for (i = 0; i < RSA_NUM; i++) rsa_key[i] = NULL; # endif if ((buf_malloc = (unsigned char *)OPENSSL_malloc(BUFSIZE + misalign)) == NULL) { BIO_printf(bio_err, "out of memory\n"); goto end; } if ((buf2_malloc = (unsigned char *)OPENSSL_malloc(BUFSIZE + misalign)) == NULL) { BIO_printf(bio_err, "out of memory\n"); goto end; } misalign = 0; buf = buf_malloc; buf2 = buf2_malloc; memset(c, 0, sizeof(c)); memset(DES_iv, 0, sizeof(DES_iv)); memset(iv, 0, sizeof(iv)); for (i = 0; i < ALGOR_NUM; i++) doit[i] = 0; for (i = 0; i < RSA_NUM; i++) rsa_doit[i] = 0; for (i = 0; i < DSA_NUM; i++) dsa_doit[i] = 0; # ifndef OPENSSL_NO_ECDSA for (i = 0; i < EC_NUM; i++) ecdsa_doit[i] = 0; # endif # ifndef OPENSSL_NO_ECDH for (i = 0; i < EC_NUM; i++) ecdh_doit[i] = 0; # endif j = 0; argc--; argv++; while (argc) { if ((argc > 0) && (strcmp(*argv, "-elapsed") == 0)) { usertime = 0; j--; } else if ((argc > 0) && (strcmp(*argv, "-evp") == 0)) { argc--; argv++; if (argc == 0) { BIO_printf(bio_err, "no EVP given\n"); goto end; } evp_cipher = EVP_get_cipherbyname(*argv); if (!evp_cipher) { evp_md = EVP_get_digestbyname(*argv); } if (!evp_cipher && !evp_md) { BIO_printf(bio_err, "%s is an unknown cipher or digest\n", *argv); goto end; } doit[D_EVP] = 1; } else if (argc > 0 && !strcmp(*argv, "-decrypt")) { decrypt = 1; j--; } # ifndef OPENSSL_NO_ENGINE else if ((argc > 0) && (strcmp(*argv, "-engine") == 0)) { argc--; argv++; if (argc == 0) { BIO_printf(bio_err, "no engine given\n"); goto end; } setup_engine(bio_err, *argv, 0); j--; } # endif # ifndef NO_FORK else if ((argc > 0) && (strcmp(*argv, "-multi") == 0)) { argc--; argv++; if (argc == 0) { BIO_printf(bio_err, "no multi count given\n"); goto end; } multi = atoi(argv[0]); if (multi <= 0) { BIO_printf(bio_err, "bad multi count\n"); goto end; } j--; } # endif else if (argc > 0 && !strcmp(*argv, "-mr")) { mr = 1; j--; } else if (argc > 0 && !strcmp(*argv, "-mb")) { multiblock = 1; j--; } else if (argc > 0 && !strcmp(*argv, "-misalign")) { argc--; argv++; if (argc == 0) { BIO_printf(bio_err, "no misalignment given\n"); goto end; } misalign = atoi(argv[0]); if (misalign < 0 || misalign > MAX_MISALIGNMENT) { BIO_printf(bio_err, "misalignment is outsize permitted range 0-%d\n", MAX_MISALIGNMENT); goto end; } buf = buf_malloc + misalign; buf2 = buf2_malloc + misalign; j--; } else # ifndef OPENSSL_NO_MD2 if (strcmp(*argv, "md2") == 0) doit[D_MD2] = 1; else # endif # ifndef OPENSSL_NO_MDC2 if (strcmp(*argv, "mdc2") == 0) doit[D_MDC2] = 1; else # endif # ifndef OPENSSL_NO_MD4 if (strcmp(*argv, "md4") == 0) doit[D_MD4] = 1; else # endif # ifndef OPENSSL_NO_MD5 if (strcmp(*argv, "md5") == 0) doit[D_MD5] = 1; else # endif # ifndef OPENSSL_NO_MD5 if (strcmp(*argv, "hmac") == 0) doit[D_HMAC] = 1; else # endif # ifndef OPENSSL_NO_SHA if (strcmp(*argv, "sha1") == 0) doit[D_SHA1] = 1; else if (strcmp(*argv, "sha") == 0) doit[D_SHA1] = 1, doit[D_SHA256] = 1, doit[D_SHA512] = 1; else # ifndef OPENSSL_NO_SHA256 if (strcmp(*argv, "sha256") == 0) doit[D_SHA256] = 1; else # endif # ifndef OPENSSL_NO_SHA512 if (strcmp(*argv, "sha512") == 0) doit[D_SHA512] = 1; else # endif # endif # ifndef OPENSSL_NO_WHIRLPOOL if (strcmp(*argv, "whirlpool") == 0) doit[D_WHIRLPOOL] = 1; else # endif # ifndef OPENSSL_NO_RMD160 if (strcmp(*argv, "ripemd") == 0) doit[D_RMD160] = 1; else if (strcmp(*argv, "rmd160") == 0) doit[D_RMD160] = 1; else if (strcmp(*argv, "ripemd160") == 0) doit[D_RMD160] = 1; else # endif # ifndef OPENSSL_NO_RC4 if (strcmp(*argv, "rc4") == 0) doit[D_RC4] = 1; else # endif # ifndef OPENSSL_NO_DES if (strcmp(*argv, "des-cbc") == 0) doit[D_CBC_DES] = 1; else if (strcmp(*argv, "des-ede3") == 0) doit[D_EDE3_DES] = 1; else # endif # ifndef OPENSSL_NO_AES if (strcmp(*argv, "aes-128-cbc") == 0) doit[D_CBC_128_AES] = 1; else if (strcmp(*argv, "aes-192-cbc") == 0) doit[D_CBC_192_AES] = 1; else if (strcmp(*argv, "aes-256-cbc") == 0) doit[D_CBC_256_AES] = 1; else if (strcmp(*argv, "aes-128-ige") == 0) doit[D_IGE_128_AES] = 1; else if (strcmp(*argv, "aes-192-ige") == 0) doit[D_IGE_192_AES] = 1; else if (strcmp(*argv, "aes-256-ige") == 0) doit[D_IGE_256_AES] = 1; else # endif # ifndef OPENSSL_NO_CAMELLIA if (strcmp(*argv, "camellia-128-cbc") == 0) doit[D_CBC_128_CML] = 1; else if (strcmp(*argv, "camellia-192-cbc") == 0) doit[D_CBC_192_CML] = 1; else if (strcmp(*argv, "camellia-256-cbc") == 0) doit[D_CBC_256_CML] = 1; else # endif # ifndef OPENSSL_NO_RSA # if 0 if (strcmp(*argv, "rsaref") == 0) { RSA_set_default_openssl_method(RSA_PKCS1_RSAref()); j--; } else # endif # ifndef RSA_NULL if (strcmp(*argv, "openssl") == 0) { RSA_set_default_method(RSA_PKCS1_SSLeay()); j--; } else # endif # endif if (strcmp(*argv, "dsa512") == 0) dsa_doit[R_DSA_512] = 2; else if (strcmp(*argv, "dsa1024") == 0) dsa_doit[R_DSA_1024] = 2; else if (strcmp(*argv, "dsa2048") == 0) dsa_doit[R_DSA_2048] = 2; else if (strcmp(*argv, "rsa512") == 0) rsa_doit[R_RSA_512] = 2; else if (strcmp(*argv, "rsa1024") == 0) rsa_doit[R_RSA_1024] = 2; else if (strcmp(*argv, "rsa2048") == 0) rsa_doit[R_RSA_2048] = 2; else if (strcmp(*argv, "rsa3072") == 0) rsa_doit[R_RSA_3072] = 2; else if (strcmp(*argv, "rsa4096") == 0) rsa_doit[R_RSA_4096] = 2; else if (strcmp(*argv, "rsa7680") == 0) rsa_doit[R_RSA_7680] = 2; else if (strcmp(*argv, "rsa15360") == 0) rsa_doit[R_RSA_15360] = 2; else # ifndef OPENSSL_NO_RC2 if (strcmp(*argv, "rc2-cbc") == 0) doit[D_CBC_RC2] = 1; else if (strcmp(*argv, "rc2") == 0) doit[D_CBC_RC2] = 1; else # endif # ifndef OPENSSL_NO_RC5 if (strcmp(*argv, "rc5-cbc") == 0) doit[D_CBC_RC5] = 1; else if (strcmp(*argv, "rc5") == 0) doit[D_CBC_RC5] = 1; else # endif # ifndef OPENSSL_NO_IDEA if (strcmp(*argv, "idea-cbc") == 0) doit[D_CBC_IDEA] = 1; else if (strcmp(*argv, "idea") == 0) doit[D_CBC_IDEA] = 1; else # endif # ifndef OPENSSL_NO_SEED if (strcmp(*argv, "seed-cbc") == 0) doit[D_CBC_SEED] = 1; else if (strcmp(*argv, "seed") == 0) doit[D_CBC_SEED] = 1; else # endif # ifndef OPENSSL_NO_BF if (strcmp(*argv, "bf-cbc") == 0) doit[D_CBC_BF] = 1; else if (strcmp(*argv, "blowfish") == 0) doit[D_CBC_BF] = 1; else if (strcmp(*argv, "bf") == 0) doit[D_CBC_BF] = 1; else # endif # ifndef OPENSSL_NO_CAST if (strcmp(*argv, "cast-cbc") == 0) doit[D_CBC_CAST] = 1; else if (strcmp(*argv, "cast") == 0) doit[D_CBC_CAST] = 1; else if (strcmp(*argv, "cast5") == 0) doit[D_CBC_CAST] = 1; else # endif # ifndef OPENSSL_NO_DES if (strcmp(*argv, "des") == 0) { doit[D_CBC_DES] = 1; doit[D_EDE3_DES] = 1; } else # endif # ifndef OPENSSL_NO_AES if (strcmp(*argv, "aes") == 0) { doit[D_CBC_128_AES] = 1; doit[D_CBC_192_AES] = 1; doit[D_CBC_256_AES] = 1; } else if (strcmp(*argv, "ghash") == 0) { doit[D_GHASH] = 1; } else # endif # ifndef OPENSSL_NO_CAMELLIA if (strcmp(*argv, "camellia") == 0) { doit[D_CBC_128_CML] = 1; doit[D_CBC_192_CML] = 1; doit[D_CBC_256_CML] = 1; } else # endif # ifndef OPENSSL_NO_RSA if (strcmp(*argv, "rsa") == 0) { rsa_doit[R_RSA_512] = 1; rsa_doit[R_RSA_1024] = 1; rsa_doit[R_RSA_2048] = 1; rsa_doit[R_RSA_3072] = 1; rsa_doit[R_RSA_4096] = 1; rsa_doit[R_RSA_7680] = 1; rsa_doit[R_RSA_15360] = 1; } else # endif # ifndef OPENSSL_NO_DSA if (strcmp(*argv, "dsa") == 0) { dsa_doit[R_DSA_512] = 1; dsa_doit[R_DSA_1024] = 1; dsa_doit[R_DSA_2048] = 1; } else # endif # ifndef OPENSSL_NO_ECDSA if (strcmp(*argv, "ecdsap160") == 0) ecdsa_doit[R_EC_P160] = 2; else if (strcmp(*argv, "ecdsap192") == 0) ecdsa_doit[R_EC_P192] = 2; else if (strcmp(*argv, "ecdsap224") == 0) ecdsa_doit[R_EC_P224] = 2; else if (strcmp(*argv, "ecdsap256") == 0) ecdsa_doit[R_EC_P256] = 2; else if (strcmp(*argv, "ecdsap384") == 0) ecdsa_doit[R_EC_P384] = 2; else if (strcmp(*argv, "ecdsap521") == 0) ecdsa_doit[R_EC_P521] = 2; else if (strcmp(*argv, "ecdsak163") == 0) ecdsa_doit[R_EC_K163] = 2; else if (strcmp(*argv, "ecdsak233") == 0) ecdsa_doit[R_EC_K233] = 2; else if (strcmp(*argv, "ecdsak283") == 0) ecdsa_doit[R_EC_K283] = 2; else if (strcmp(*argv, "ecdsak409") == 0) ecdsa_doit[R_EC_K409] = 2; else if (strcmp(*argv, "ecdsak571") == 0) ecdsa_doit[R_EC_K571] = 2; else if (strcmp(*argv, "ecdsab163") == 0) ecdsa_doit[R_EC_B163] = 2; else if (strcmp(*argv, "ecdsab233") == 0) ecdsa_doit[R_EC_B233] = 2; else if (strcmp(*argv, "ecdsab283") == 0) ecdsa_doit[R_EC_B283] = 2; else if (strcmp(*argv, "ecdsab409") == 0) ecdsa_doit[R_EC_B409] = 2; else if (strcmp(*argv, "ecdsab571") == 0) ecdsa_doit[R_EC_B571] = 2; else if (strcmp(*argv, "ecdsa") == 0) { for (i = 0; i < EC_NUM; i++) ecdsa_doit[i] = 1; } else # endif # ifndef OPENSSL_NO_ECDH if (strcmp(*argv, "ecdhp160") == 0) ecdh_doit[R_EC_P160] = 2; else if (strcmp(*argv, "ecdhp192") == 0) ecdh_doit[R_EC_P192] = 2; else if (strcmp(*argv, "ecdhp224") == 0) ecdh_doit[R_EC_P224] = 2; else if (strcmp(*argv, "ecdhp256") == 0) ecdh_doit[R_EC_P256] = 2; else if (strcmp(*argv, "ecdhp384") == 0) ecdh_doit[R_EC_P384] = 2; else if (strcmp(*argv, "ecdhp521") == 0) ecdh_doit[R_EC_P521] = 2; else if (strcmp(*argv, "ecdhk163") == 0) ecdh_doit[R_EC_K163] = 2; else if (strcmp(*argv, "ecdhk233") == 0) ecdh_doit[R_EC_K233] = 2; else if (strcmp(*argv, "ecdhk283") == 0) ecdh_doit[R_EC_K283] = 2; else if (strcmp(*argv, "ecdhk409") == 0) ecdh_doit[R_EC_K409] = 2; else if (strcmp(*argv, "ecdhk571") == 0) ecdh_doit[R_EC_K571] = 2; else if (strcmp(*argv, "ecdhb163") == 0) ecdh_doit[R_EC_B163] = 2; else if (strcmp(*argv, "ecdhb233") == 0) ecdh_doit[R_EC_B233] = 2; else if (strcmp(*argv, "ecdhb283") == 0) ecdh_doit[R_EC_B283] = 2; else if (strcmp(*argv, "ecdhb409") == 0) ecdh_doit[R_EC_B409] = 2; else if (strcmp(*argv, "ecdhb571") == 0) ecdh_doit[R_EC_B571] = 2; else if (strcmp(*argv, "ecdh") == 0) { for (i = 0; i < EC_NUM; i++) ecdh_doit[i] = 1; } else # endif { BIO_printf(bio_err, "Error: bad option or value\n"); BIO_printf(bio_err, "\n"); BIO_printf(bio_err, "Available values:\n"); # ifndef OPENSSL_NO_MD2 BIO_printf(bio_err, "md2 "); # endif # ifndef OPENSSL_NO_MDC2 BIO_printf(bio_err, "mdc2 "); # endif # ifndef OPENSSL_NO_MD4 BIO_printf(bio_err, "md4 "); # endif # ifndef OPENSSL_NO_MD5 BIO_printf(bio_err, "md5 "); # ifndef OPENSSL_NO_HMAC BIO_printf(bio_err, "hmac "); # endif # endif # ifndef OPENSSL_NO_SHA1 BIO_printf(bio_err, "sha1 "); # endif # ifndef OPENSSL_NO_SHA256 BIO_printf(bio_err, "sha256 "); # endif # ifndef OPENSSL_NO_SHA512 BIO_printf(bio_err, "sha512 "); # endif # ifndef OPENSSL_NO_WHIRLPOOL BIO_printf(bio_err, "whirlpool"); # endif # ifndef OPENSSL_NO_RMD160 BIO_printf(bio_err, "rmd160"); # endif # if !defined(OPENSSL_NO_MD2) || !defined(OPENSSL_NO_MDC2) || \ !defined(OPENSSL_NO_MD4) || !defined(OPENSSL_NO_MD5) || \ !defined(OPENSSL_NO_SHA1) || !defined(OPENSSL_NO_RMD160) || \ !defined(OPENSSL_NO_WHIRLPOOL) BIO_printf(bio_err, "\n"); # endif # ifndef OPENSSL_NO_IDEA BIO_printf(bio_err, "idea-cbc "); # endif # ifndef OPENSSL_NO_SEED BIO_printf(bio_err, "seed-cbc "); # endif # ifndef OPENSSL_NO_RC2 BIO_printf(bio_err, "rc2-cbc "); # endif # ifndef OPENSSL_NO_RC5 BIO_printf(bio_err, "rc5-cbc "); # endif # ifndef OPENSSL_NO_BF BIO_printf(bio_err, "bf-cbc"); # endif # if !defined(OPENSSL_NO_IDEA) || !defined(OPENSSL_NO_SEED) || !defined(OPENSSL_NO_RC2) || \ !defined(OPENSSL_NO_BF) || !defined(OPENSSL_NO_RC5) BIO_printf(bio_err, "\n"); # endif # ifndef OPENSSL_NO_DES BIO_printf(bio_err, "des-cbc des-ede3 "); # endif # ifndef OPENSSL_NO_AES BIO_printf(bio_err, "aes-128-cbc aes-192-cbc aes-256-cbc "); BIO_printf(bio_err, "aes-128-ige aes-192-ige aes-256-ige "); # endif # ifndef OPENSSL_NO_CAMELLIA BIO_printf(bio_err, "\n"); BIO_printf(bio_err, "camellia-128-cbc camellia-192-cbc camellia-256-cbc "); # endif # ifndef OPENSSL_NO_RC4 BIO_printf(bio_err, "rc4"); # endif BIO_printf(bio_err, "\n"); # ifndef OPENSSL_NO_RSA BIO_printf(bio_err, "rsa512 rsa1024 rsa2048 rsa3072 rsa4096\n"); BIO_printf(bio_err, "rsa7680 rsa15360\n"); # endif # ifndef OPENSSL_NO_DSA BIO_printf(bio_err, "dsa512 dsa1024 dsa2048\n"); # endif # ifndef OPENSSL_NO_ECDSA BIO_printf(bio_err, "ecdsap160 ecdsap192 ecdsap224 " "ecdsap256 ecdsap384 ecdsap521\n"); BIO_printf(bio_err, "ecdsak163 ecdsak233 ecdsak283 ecdsak409 ecdsak571\n"); BIO_printf(bio_err, "ecdsab163 ecdsab233 ecdsab283 ecdsab409 ecdsab571\n"); BIO_printf(bio_err, "ecdsa\n"); # endif # ifndef OPENSSL_NO_ECDH BIO_printf(bio_err, "ecdhp160 ecdhp192 ecdhp224 " "ecdhp256 ecdhp384 ecdhp521\n"); BIO_printf(bio_err, "ecdhk163 ecdhk233 ecdhk283 ecdhk409 ecdhk571\n"); BIO_printf(bio_err, "ecdhb163 ecdhb233 ecdhb283 ecdhb409 ecdhb571\n"); BIO_printf(bio_err, "ecdh\n"); # endif # ifndef OPENSSL_NO_IDEA BIO_printf(bio_err, "idea "); # endif # ifndef OPENSSL_NO_SEED BIO_printf(bio_err, "seed "); # endif # ifndef OPENSSL_NO_RC2 BIO_printf(bio_err, "rc2 "); # endif # ifndef OPENSSL_NO_DES BIO_printf(bio_err, "des "); # endif # ifndef OPENSSL_NO_AES BIO_printf(bio_err, "aes "); # endif # ifndef OPENSSL_NO_CAMELLIA BIO_printf(bio_err, "camellia "); # endif # ifndef OPENSSL_NO_RSA BIO_printf(bio_err, "rsa "); # endif # ifndef OPENSSL_NO_BF BIO_printf(bio_err, "blowfish"); # endif # if !defined(OPENSSL_NO_IDEA) || !defined(OPENSSL_NO_SEED) || \ !defined(OPENSSL_NO_RC2) || !defined(OPENSSL_NO_DES) || \ !defined(OPENSSL_NO_RSA) || !defined(OPENSSL_NO_BF) || \ !defined(OPENSSL_NO_AES) || !defined(OPENSSL_NO_CAMELLIA) BIO_printf(bio_err, "\n"); # endif BIO_printf(bio_err, "\n"); BIO_printf(bio_err, "Available options:\n"); # if defined(TIMES) || defined(USE_TOD) BIO_printf(bio_err, "-elapsed " "measure time in real time instead of CPU user time.\n"); # endif # ifndef OPENSSL_NO_ENGINE BIO_printf(bio_err, "-engine e " "use engine e, possibly a hardware device.\n"); # endif BIO_printf(bio_err, "-evp e " "use EVP e.\n"); BIO_printf(bio_err, "-decrypt " "time decryption instead of encryption (only EVP).\n"); BIO_printf(bio_err, "-mr " "produce machine readable output.\n"); BIO_printf(bio_err, "-mb " "perform multi-block benchmark (for specific ciphers)\n"); BIO_printf(bio_err, "-misalign n " "perform benchmark with misaligned data\n"); # ifndef NO_FORK BIO_printf(bio_err, "-multi n " "run n benchmarks in parallel.\n"); # endif goto end; } argc--; argv++; j++; } # ifndef NO_FORK if (multi && do_multi(multi)) goto show_res; # endif if (j == 0) { for (i = 0; i < ALGOR_NUM; i++) { if (i != D_EVP) doit[i] = 1; } for (i = 0; i < RSA_NUM; i++) rsa_doit[i] = 1; for (i = 0; i < DSA_NUM; i++) dsa_doit[i] = 1; # ifndef OPENSSL_NO_ECDSA for (i = 0; i < EC_NUM; i++) ecdsa_doit[i] = 1; # endif # ifndef OPENSSL_NO_ECDH for (i = 0; i < EC_NUM; i++) ecdh_doit[i] = 1; # endif } for (i = 0; i < ALGOR_NUM; i++) if (doit[i]) pr_header++; if (usertime == 0 && !mr) BIO_printf(bio_err, "You have chosen to measure elapsed time " "instead of user CPU time.\n"); # ifndef OPENSSL_NO_RSA for (i = 0; i < RSA_NUM; i++) { const unsigned char *p; p = rsa_data[i]; rsa_key[i] = d2i_RSAPrivateKey(NULL, &p, rsa_data_length[i]); if (rsa_key[i] == NULL) { BIO_printf(bio_err, "internal error loading RSA key number %d\n", i); goto end; } # if 0 else { BIO_printf(bio_err, mr ? "+RK:%d:" : "Loaded RSA key, %d bit modulus and e= 0x", BN_num_bits(rsa_key[i]->n)); BN_print(bio_err, rsa_key[i]->e); BIO_printf(bio_err, "\n"); } # endif } # endif # ifndef OPENSSL_NO_DSA dsa_key[0] = get_dsa512(); dsa_key[1] = get_dsa1024(); dsa_key[2] = get_dsa2048(); # endif # ifndef OPENSSL_NO_DES DES_set_key_unchecked(&key, &sch); DES_set_key_unchecked(&key2, &sch2); DES_set_key_unchecked(&key3, &sch3); # endif # ifndef OPENSSL_NO_AES AES_set_encrypt_key(key16, 128, &aes_ks1); AES_set_encrypt_key(key24, 192, &aes_ks2); AES_set_encrypt_key(key32, 256, &aes_ks3); # endif # ifndef OPENSSL_NO_CAMELLIA Camellia_set_key(key16, 128, &camellia_ks1); Camellia_set_key(ckey24, 192, &camellia_ks2); Camellia_set_key(ckey32, 256, &camellia_ks3); # endif # ifndef OPENSSL_NO_IDEA idea_set_encrypt_key(key16, &idea_ks); # endif # ifndef OPENSSL_NO_SEED SEED_set_key(key16, &seed_ks); # endif # ifndef OPENSSL_NO_RC4 RC4_set_key(&rc4_ks, 16, key16); # endif # ifndef OPENSSL_NO_RC2 RC2_set_key(&rc2_ks, 16, key16, 128); # endif # ifndef OPENSSL_NO_RC5 RC5_32_set_key(&rc5_ks, 16, key16, 12); # endif # ifndef OPENSSL_NO_BF BF_set_key(&bf_ks, 16, key16); # endif # ifndef OPENSSL_NO_CAST CAST_set_key(&cast_ks, 16, key16); # endif # ifndef OPENSSL_NO_RSA memset(rsa_c, 0, sizeof(rsa_c)); # endif # ifndef SIGALRM # ifndef OPENSSL_NO_DES BIO_printf(bio_err, "First we calculate the approximate speed ...\n"); count = 10; do { long it; count *= 2; Time_F(START); for (it = count; it; it--) DES_ecb_encrypt((DES_cblock *)buf, (DES_cblock *)buf, &sch, DES_ENCRYPT); d = Time_F(STOP); } while (d < 3); save_count = count; c[D_MD2][0] = count / 10; c[D_MDC2][0] = count / 10; c[D_MD4][0] = count; c[D_MD5][0] = count; c[D_HMAC][0] = count; c[D_SHA1][0] = count; c[D_RMD160][0] = count; c[D_RC4][0] = count * 5; c[D_CBC_DES][0] = count; c[D_EDE3_DES][0] = count / 3; c[D_CBC_IDEA][0] = count; c[D_CBC_SEED][0] = count; c[D_CBC_RC2][0] = count; c[D_CBC_RC5][0] = count; c[D_CBC_BF][0] = count; c[D_CBC_CAST][0] = count; c[D_CBC_128_AES][0] = count; c[D_CBC_192_AES][0] = count; c[D_CBC_256_AES][0] = count; c[D_CBC_128_CML][0] = count; c[D_CBC_192_CML][0] = count; c[D_CBC_256_CML][0] = count; c[D_SHA256][0] = count; c[D_SHA512][0] = count; c[D_WHIRLPOOL][0] = count; c[D_IGE_128_AES][0] = count; c[D_IGE_192_AES][0] = count; c[D_IGE_256_AES][0] = count; c[D_GHASH][0] = count; for (i = 1; i < SIZE_NUM; i++) { long l0, l1; l0 = (long)lengths[0]; l1 = (long)lengths[i]; c[D_MD2][i] = c[D_MD2][0] * 4 * l0 / l1; c[D_MDC2][i] = c[D_MDC2][0] * 4 * l0 / l1; c[D_MD4][i] = c[D_MD4][0] * 4 * l0 / l1; c[D_MD5][i] = c[D_MD5][0] * 4 * l0 / l1; c[D_HMAC][i] = c[D_HMAC][0] * 4 * l0 / l1; c[D_SHA1][i] = c[D_SHA1][0] * 4 * l0 / l1; c[D_RMD160][i] = c[D_RMD160][0] * 4 * l0 / l1; c[D_SHA256][i] = c[D_SHA256][0] * 4 * l0 / l1; c[D_SHA512][i] = c[D_SHA512][0] * 4 * l0 / l1; c[D_WHIRLPOOL][i] = c[D_WHIRLPOOL][0] * 4 * l0 / l1; l0 = (long)lengths[i - 1]; c[D_RC4][i] = c[D_RC4][i - 1] * l0 / l1; c[D_CBC_DES][i] = c[D_CBC_DES][i - 1] * l0 / l1; c[D_EDE3_DES][i] = c[D_EDE3_DES][i - 1] * l0 / l1; c[D_CBC_IDEA][i] = c[D_CBC_IDEA][i - 1] * l0 / l1; c[D_CBC_SEED][i] = c[D_CBC_SEED][i - 1] * l0 / l1; c[D_CBC_RC2][i] = c[D_CBC_RC2][i - 1] * l0 / l1; c[D_CBC_RC5][i] = c[D_CBC_RC5][i - 1] * l0 / l1; c[D_CBC_BF][i] = c[D_CBC_BF][i - 1] * l0 / l1; c[D_CBC_CAST][i] = c[D_CBC_CAST][i - 1] * l0 / l1; c[D_CBC_128_AES][i] = c[D_CBC_128_AES][i - 1] * l0 / l1; c[D_CBC_192_AES][i] = c[D_CBC_192_AES][i - 1] * l0 / l1; c[D_CBC_256_AES][i] = c[D_CBC_256_AES][i - 1] * l0 / l1; c[D_CBC_128_CML][i] = c[D_CBC_128_CML][i - 1] * l0 / l1; c[D_CBC_192_CML][i] = c[D_CBC_192_CML][i - 1] * l0 / l1; c[D_CBC_256_CML][i] = c[D_CBC_256_CML][i - 1] * l0 / l1; c[D_IGE_128_AES][i] = c[D_IGE_128_AES][i - 1] * l0 / l1; c[D_IGE_192_AES][i] = c[D_IGE_192_AES][i - 1] * l0 / l1; c[D_IGE_256_AES][i] = c[D_IGE_256_AES][i - 1] * l0 / l1; } # ifndef OPENSSL_NO_RSA rsa_c[R_RSA_512][0] = count / 2000; rsa_c[R_RSA_512][1] = count / 400; for (i = 1; i < RSA_NUM; i++) { rsa_c[i][0] = rsa_c[i - 1][0] / 8; rsa_c[i][1] = rsa_c[i - 1][1] / 4; if ((rsa_doit[i] <= 1) && (rsa_c[i][0] == 0)) rsa_doit[i] = 0; else { if (rsa_c[i][0] == 0) { rsa_c[i][0] = 1; rsa_c[i][1] = 20; } } } # endif # ifndef OPENSSL_NO_DSA dsa_c[R_DSA_512][0] = count / 1000; dsa_c[R_DSA_512][1] = count / 1000 / 2; for (i = 1; i < DSA_NUM; i++) { dsa_c[i][0] = dsa_c[i - 1][0] / 4; dsa_c[i][1] = dsa_c[i - 1][1] / 4; if ((dsa_doit[i] <= 1) && (dsa_c[i][0] == 0)) dsa_doit[i] = 0; else { if (dsa_c[i] == 0) { dsa_c[i][0] = 1; dsa_c[i][1] = 1; } } } # endif # ifndef OPENSSL_NO_ECDSA ecdsa_c[R_EC_P160][0] = count / 1000; ecdsa_c[R_EC_P160][1] = count / 1000 / 2; for (i = R_EC_P192; i <= R_EC_P521; i++) { ecdsa_c[i][0] = ecdsa_c[i - 1][0] / 2; ecdsa_c[i][1] = ecdsa_c[i - 1][1] / 2; if ((ecdsa_doit[i] <= 1) && (ecdsa_c[i][0] == 0)) ecdsa_doit[i] = 0; else { if (ecdsa_c[i] == 0) { ecdsa_c[i][0] = 1; ecdsa_c[i][1] = 1; } } } ecdsa_c[R_EC_K163][0] = count / 1000; ecdsa_c[R_EC_K163][1] = count / 1000 / 2; for (i = R_EC_K233; i <= R_EC_K571; i++) { ecdsa_c[i][0] = ecdsa_c[i - 1][0] / 2; ecdsa_c[i][1] = ecdsa_c[i - 1][1] / 2; if ((ecdsa_doit[i] <= 1) && (ecdsa_c[i][0] == 0)) ecdsa_doit[i] = 0; else { if (ecdsa_c[i] == 0) { ecdsa_c[i][0] = 1; ecdsa_c[i][1] = 1; } } } ecdsa_c[R_EC_B163][0] = count / 1000; ecdsa_c[R_EC_B163][1] = count / 1000 / 2; for (i = R_EC_B233; i <= R_EC_B571; i++) { ecdsa_c[i][0] = ecdsa_c[i - 1][0] / 2; ecdsa_c[i][1] = ecdsa_c[i - 1][1] / 2; if ((ecdsa_doit[i] <= 1) && (ecdsa_c[i][0] == 0)) ecdsa_doit[i] = 0; else { if (ecdsa_c[i] == 0) { ecdsa_c[i][0] = 1; ecdsa_c[i][1] = 1; } } } # endif # ifndef OPENSSL_NO_ECDH ecdh_c[R_EC_P160][0] = count / 1000; ecdh_c[R_EC_P160][1] = count / 1000; for (i = R_EC_P192; i <= R_EC_P521; i++) { ecdh_c[i][0] = ecdh_c[i - 1][0] / 2; ecdh_c[i][1] = ecdh_c[i - 1][1] / 2; if ((ecdh_doit[i] <= 1) && (ecdh_c[i][0] == 0)) ecdh_doit[i] = 0; else { if (ecdh_c[i] == 0) { ecdh_c[i][0] = 1; ecdh_c[i][1] = 1; } } } ecdh_c[R_EC_K163][0] = count / 1000; ecdh_c[R_EC_K163][1] = count / 1000; for (i = R_EC_K233; i <= R_EC_K571; i++) { ecdh_c[i][0] = ecdh_c[i - 1][0] / 2; ecdh_c[i][1] = ecdh_c[i - 1][1] / 2; if ((ecdh_doit[i] <= 1) && (ecdh_c[i][0] == 0)) ecdh_doit[i] = 0; else { if (ecdh_c[i] == 0) { ecdh_c[i][0] = 1; ecdh_c[i][1] = 1; } } } ecdh_c[R_EC_B163][0] = count / 1000; ecdh_c[R_EC_B163][1] = count / 1000; for (i = R_EC_B233; i <= R_EC_B571; i++) { ecdh_c[i][0] = ecdh_c[i - 1][0] / 2; ecdh_c[i][1] = ecdh_c[i - 1][1] / 2; if ((ecdh_doit[i] <= 1) && (ecdh_c[i][0] == 0)) ecdh_doit[i] = 0; else { if (ecdh_c[i] == 0) { ecdh_c[i][0] = 1; ecdh_c[i][1] = 1; } } } # endif # define COND(d) (count < (d)) # define COUNT(d) (d) # else # error "You cannot disable DES on systems without SIGALRM." # endif # else # define COND(c) (run && count<0x7fffffff) # define COUNT(d) (count) # ifndef _WIN32 signal(SIGALRM, sig_done); # endif # endif # ifndef OPENSSL_NO_MD2 if (doit[D_MD2]) { for (j = 0; j < SIZE_NUM; j++) { print_message(names[D_MD2], c[D_MD2][j], lengths[j]); Time_F(START); for (count = 0, run = 1; COND(c[D_MD2][j]); count++) EVP_Digest(buf, (unsigned long)lengths[j], &(md2[0]), NULL, EVP_md2(), NULL); d = Time_F(STOP); print_result(D_MD2, j, count, d); } } # endif # ifndef OPENSSL_NO_MDC2 if (doit[D_MDC2]) { for (j = 0; j < SIZE_NUM; j++) { print_message(names[D_MDC2], c[D_MDC2][j], lengths[j]); Time_F(START); for (count = 0, run = 1; COND(c[D_MDC2][j]); count++) EVP_Digest(buf, (unsigned long)lengths[j], &(mdc2[0]), NULL, EVP_mdc2(), NULL); d = Time_F(STOP); print_result(D_MDC2, j, count, d); } } # endif # ifndef OPENSSL_NO_MD4 if (doit[D_MD4]) { for (j = 0; j < SIZE_NUM; j++) { print_message(names[D_MD4], c[D_MD4][j], lengths[j]); Time_F(START); for (count = 0, run = 1; COND(c[D_MD4][j]); count++) EVP_Digest(&(buf[0]), (unsigned long)lengths[j], &(md4[0]), NULL, EVP_md4(), NULL); d = Time_F(STOP); print_result(D_MD4, j, count, d); } } # endif # ifndef OPENSSL_NO_MD5 if (doit[D_MD5]) { for (j = 0; j < SIZE_NUM; j++) { print_message(names[D_MD5], c[D_MD5][j], lengths[j]); Time_F(START); for (count = 0, run = 1; COND(c[D_MD5][j]); count++) MD5(buf, lengths[j], md5); d = Time_F(STOP); print_result(D_MD5, j, count, d); } } # endif # if !defined(OPENSSL_NO_MD5) && !defined(OPENSSL_NO_HMAC) if (doit[D_HMAC]) { HMAC_CTX hctx; HMAC_CTX_init(&hctx); HMAC_Init_ex(&hctx, (unsigned char *)"This is a key...", 16, EVP_md5(), NULL); for (j = 0; j < SIZE_NUM; j++) { print_message(names[D_HMAC], c[D_HMAC][j], lengths[j]); Time_F(START); for (count = 0, run = 1; COND(c[D_HMAC][j]); count++) { HMAC_Init_ex(&hctx, NULL, 0, NULL, NULL); HMAC_Update(&hctx, buf, lengths[j]); HMAC_Final(&hctx, &(hmac[0]), NULL); } d = Time_F(STOP); print_result(D_HMAC, j, count, d); } HMAC_CTX_cleanup(&hctx); } # endif # ifndef OPENSSL_NO_SHA if (doit[D_SHA1]) { for (j = 0; j < SIZE_NUM; j++) { print_message(names[D_SHA1], c[D_SHA1][j], lengths[j]); Time_F(START); for (count = 0, run = 1; COND(c[D_SHA1][j]); count++) # if 0 EVP_Digest(buf, (unsigned long)lengths[j], &(sha[0]), NULL, EVP_sha1(), NULL); # else SHA1(buf, lengths[j], sha); # endif d = Time_F(STOP); print_result(D_SHA1, j, count, d); } } # ifndef OPENSSL_NO_SHA256 if (doit[D_SHA256]) { for (j = 0; j < SIZE_NUM; j++) { print_message(names[D_SHA256], c[D_SHA256][j], lengths[j]); Time_F(START); for (count = 0, run = 1; COND(c[D_SHA256][j]); count++) SHA256(buf, lengths[j], sha256); d = Time_F(STOP); print_result(D_SHA256, j, count, d); } } # endif # ifndef OPENSSL_NO_SHA512 if (doit[D_SHA512]) { for (j = 0; j < SIZE_NUM; j++) { print_message(names[D_SHA512], c[D_SHA512][j], lengths[j]); Time_F(START); for (count = 0, run = 1; COND(c[D_SHA512][j]); count++) SHA512(buf, lengths[j], sha512); d = Time_F(STOP); print_result(D_SHA512, j, count, d); } } # endif # endif # ifndef OPENSSL_NO_WHIRLPOOL if (doit[D_WHIRLPOOL]) { for (j = 0; j < SIZE_NUM; j++) { print_message(names[D_WHIRLPOOL], c[D_WHIRLPOOL][j], lengths[j]); Time_F(START); for (count = 0, run = 1; COND(c[D_WHIRLPOOL][j]); count++) WHIRLPOOL(buf, lengths[j], whirlpool); d = Time_F(STOP); print_result(D_WHIRLPOOL, j, count, d); } } # endif # ifndef OPENSSL_NO_RMD160 if (doit[D_RMD160]) { for (j = 0; j < SIZE_NUM; j++) { print_message(names[D_RMD160], c[D_RMD160][j], lengths[j]); Time_F(START); for (count = 0, run = 1; COND(c[D_RMD160][j]); count++) EVP_Digest(buf, (unsigned long)lengths[j], &(rmd160[0]), NULL, EVP_ripemd160(), NULL); d = Time_F(STOP); print_result(D_RMD160, j, count, d); } } # endif # ifndef OPENSSL_NO_RC4 if (doit[D_RC4]) { for (j = 0; j < SIZE_NUM; j++) { print_message(names[D_RC4], c[D_RC4][j], lengths[j]); Time_F(START); for (count = 0, run = 1; COND(c[D_RC4][j]); count++) RC4(&rc4_ks, (unsigned int)lengths[j], buf, buf); d = Time_F(STOP); print_result(D_RC4, j, count, d); } } # endif # ifndef OPENSSL_NO_DES if (doit[D_CBC_DES]) { for (j = 0; j < SIZE_NUM; j++) { print_message(names[D_CBC_DES], c[D_CBC_DES][j], lengths[j]); Time_F(START); for (count = 0, run = 1; COND(c[D_CBC_DES][j]); count++) DES_ncbc_encrypt(buf, buf, lengths[j], &sch, &DES_iv, DES_ENCRYPT); d = Time_F(STOP); print_result(D_CBC_DES, j, count, d); } } if (doit[D_EDE3_DES]) { for (j = 0; j < SIZE_NUM; j++) { print_message(names[D_EDE3_DES], c[D_EDE3_DES][j], lengths[j]); Time_F(START); for (count = 0, run = 1; COND(c[D_EDE3_DES][j]); count++) DES_ede3_cbc_encrypt(buf, buf, lengths[j], &sch, &sch2, &sch3, &DES_iv, DES_ENCRYPT); d = Time_F(STOP); print_result(D_EDE3_DES, j, count, d); } } # endif # ifndef OPENSSL_NO_AES if (doit[D_CBC_128_AES]) { for (j = 0; j < SIZE_NUM; j++) { print_message(names[D_CBC_128_AES], c[D_CBC_128_AES][j], lengths[j]); Time_F(START); for (count = 0, run = 1; COND(c[D_CBC_128_AES][j]); count++) AES_cbc_encrypt(buf, buf, (unsigned long)lengths[j], &aes_ks1, iv, AES_ENCRYPT); d = Time_F(STOP); print_result(D_CBC_128_AES, j, count, d); } } if (doit[D_CBC_192_AES]) { for (j = 0; j < SIZE_NUM; j++) { print_message(names[D_CBC_192_AES], c[D_CBC_192_AES][j], lengths[j]); Time_F(START); for (count = 0, run = 1; COND(c[D_CBC_192_AES][j]); count++) AES_cbc_encrypt(buf, buf, (unsigned long)lengths[j], &aes_ks2, iv, AES_ENCRYPT); d = Time_F(STOP); print_result(D_CBC_192_AES, j, count, d); } } if (doit[D_CBC_256_AES]) { for (j = 0; j < SIZE_NUM; j++) { print_message(names[D_CBC_256_AES], c[D_CBC_256_AES][j], lengths[j]); Time_F(START); for (count = 0, run = 1; COND(c[D_CBC_256_AES][j]); count++) AES_cbc_encrypt(buf, buf, (unsigned long)lengths[j], &aes_ks3, iv, AES_ENCRYPT); d = Time_F(STOP); print_result(D_CBC_256_AES, j, count, d); } } if (doit[D_IGE_128_AES]) { for (j = 0; j < SIZE_NUM; j++) { print_message(names[D_IGE_128_AES], c[D_IGE_128_AES][j], lengths[j]); Time_F(START); for (count = 0, run = 1; COND(c[D_IGE_128_AES][j]); count++) AES_ige_encrypt(buf, buf2, (unsigned long)lengths[j], &aes_ks1, iv, AES_ENCRYPT); d = Time_F(STOP); print_result(D_IGE_128_AES, j, count, d); } } if (doit[D_IGE_192_AES]) { for (j = 0; j < SIZE_NUM; j++) { print_message(names[D_IGE_192_AES], c[D_IGE_192_AES][j], lengths[j]); Time_F(START); for (count = 0, run = 1; COND(c[D_IGE_192_AES][j]); count++) AES_ige_encrypt(buf, buf2, (unsigned long)lengths[j], &aes_ks2, iv, AES_ENCRYPT); d = Time_F(STOP); print_result(D_IGE_192_AES, j, count, d); } } if (doit[D_IGE_256_AES]) { for (j = 0; j < SIZE_NUM; j++) { print_message(names[D_IGE_256_AES], c[D_IGE_256_AES][j], lengths[j]); Time_F(START); for (count = 0, run = 1; COND(c[D_IGE_256_AES][j]); count++) AES_ige_encrypt(buf, buf2, (unsigned long)lengths[j], &aes_ks3, iv, AES_ENCRYPT); d = Time_F(STOP); print_result(D_IGE_256_AES, j, count, d); } } if (doit[D_GHASH]) { GCM128_CONTEXT *ctx = CRYPTO_gcm128_new(&aes_ks1, (block128_f) AES_encrypt); CRYPTO_gcm128_setiv(ctx, (unsigned char *)"0123456789ab", 12); for (j = 0; j < SIZE_NUM; j++) { print_message(names[D_GHASH], c[D_GHASH][j], lengths[j]); Time_F(START); for (count = 0, run = 1; COND(c[D_GHASH][j]); count++) CRYPTO_gcm128_aad(ctx, buf, lengths[j]); d = Time_F(STOP); print_result(D_GHASH, j, count, d); } CRYPTO_gcm128_release(ctx); } # endif # ifndef OPENSSL_NO_CAMELLIA if (doit[D_CBC_128_CML]) { for (j = 0; j < SIZE_NUM; j++) { print_message(names[D_CBC_128_CML], c[D_CBC_128_CML][j], lengths[j]); Time_F(START); for (count = 0, run = 1; COND(c[D_CBC_128_CML][j]); count++) Camellia_cbc_encrypt(buf, buf, (unsigned long)lengths[j], &camellia_ks1, iv, CAMELLIA_ENCRYPT); d = Time_F(STOP); print_result(D_CBC_128_CML, j, count, d); } } if (doit[D_CBC_192_CML]) { for (j = 0; j < SIZE_NUM; j++) { print_message(names[D_CBC_192_CML], c[D_CBC_192_CML][j], lengths[j]); Time_F(START); for (count = 0, run = 1; COND(c[D_CBC_192_CML][j]); count++) Camellia_cbc_encrypt(buf, buf, (unsigned long)lengths[j], &camellia_ks2, iv, CAMELLIA_ENCRYPT); d = Time_F(STOP); print_result(D_CBC_192_CML, j, count, d); } } if (doit[D_CBC_256_CML]) { for (j = 0; j < SIZE_NUM; j++) { print_message(names[D_CBC_256_CML], c[D_CBC_256_CML][j], lengths[j]); Time_F(START); for (count = 0, run = 1; COND(c[D_CBC_256_CML][j]); count++) Camellia_cbc_encrypt(buf, buf, (unsigned long)lengths[j], &camellia_ks3, iv, CAMELLIA_ENCRYPT); d = Time_F(STOP); print_result(D_CBC_256_CML, j, count, d); } } # endif # ifndef OPENSSL_NO_IDEA if (doit[D_CBC_IDEA]) { for (j = 0; j < SIZE_NUM; j++) { print_message(names[D_CBC_IDEA], c[D_CBC_IDEA][j], lengths[j]); Time_F(START); for (count = 0, run = 1; COND(c[D_CBC_IDEA][j]); count++) idea_cbc_encrypt(buf, buf, (unsigned long)lengths[j], &idea_ks, iv, IDEA_ENCRYPT); d = Time_F(STOP); print_result(D_CBC_IDEA, j, count, d); } } # endif # ifndef OPENSSL_NO_SEED if (doit[D_CBC_SEED]) { for (j = 0; j < SIZE_NUM; j++) { print_message(names[D_CBC_SEED], c[D_CBC_SEED][j], lengths[j]); Time_F(START); for (count = 0, run = 1; COND(c[D_CBC_SEED][j]); count++) SEED_cbc_encrypt(buf, buf, (unsigned long)lengths[j], &seed_ks, iv, 1); d = Time_F(STOP); print_result(D_CBC_SEED, j, count, d); } } # endif # ifndef OPENSSL_NO_RC2 if (doit[D_CBC_RC2]) { for (j = 0; j < SIZE_NUM; j++) { print_message(names[D_CBC_RC2], c[D_CBC_RC2][j], lengths[j]); Time_F(START); for (count = 0, run = 1; COND(c[D_CBC_RC2][j]); count++) RC2_cbc_encrypt(buf, buf, (unsigned long)lengths[j], &rc2_ks, iv, RC2_ENCRYPT); d = Time_F(STOP); print_result(D_CBC_RC2, j, count, d); } } # endif # ifndef OPENSSL_NO_RC5 if (doit[D_CBC_RC5]) { for (j = 0; j < SIZE_NUM; j++) { print_message(names[D_CBC_RC5], c[D_CBC_RC5][j], lengths[j]); Time_F(START); for (count = 0, run = 1; COND(c[D_CBC_RC5][j]); count++) RC5_32_cbc_encrypt(buf, buf, (unsigned long)lengths[j], &rc5_ks, iv, RC5_ENCRYPT); d = Time_F(STOP); print_result(D_CBC_RC5, j, count, d); } } # endif # ifndef OPENSSL_NO_BF if (doit[D_CBC_BF]) { for (j = 0; j < SIZE_NUM; j++) { print_message(names[D_CBC_BF], c[D_CBC_BF][j], lengths[j]); Time_F(START); for (count = 0, run = 1; COND(c[D_CBC_BF][j]); count++) BF_cbc_encrypt(buf, buf, (unsigned long)lengths[j], &bf_ks, iv, BF_ENCRYPT); d = Time_F(STOP); print_result(D_CBC_BF, j, count, d); } } # endif # ifndef OPENSSL_NO_CAST if (doit[D_CBC_CAST]) { for (j = 0; j < SIZE_NUM; j++) { print_message(names[D_CBC_CAST], c[D_CBC_CAST][j], lengths[j]); Time_F(START); for (count = 0, run = 1; COND(c[D_CBC_CAST][j]); count++) CAST_cbc_encrypt(buf, buf, (unsigned long)lengths[j], &cast_ks, iv, CAST_ENCRYPT); d = Time_F(STOP); print_result(D_CBC_CAST, j, count, d); } } # endif if (doit[D_EVP]) { # ifdef EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK if (multiblock && evp_cipher) { if (! (EVP_CIPHER_flags(evp_cipher) & EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK)) { fprintf(stderr, "%s is not multi-block capable\n", OBJ_nid2ln(evp_cipher->nid)); goto end; } multiblock_speed(evp_cipher); mret = 0; goto end; } # endif for (j = 0; j < SIZE_NUM; j++) { if (evp_cipher) { EVP_CIPHER_CTX ctx; int outl; names[D_EVP] = OBJ_nid2ln(evp_cipher->nid); print_message(names[D_EVP], save_count, lengths[j]); EVP_CIPHER_CTX_init(&ctx); if (decrypt) EVP_DecryptInit_ex(&ctx, evp_cipher, NULL, key16, iv); else EVP_EncryptInit_ex(&ctx, evp_cipher, NULL, key16, iv); EVP_CIPHER_CTX_set_padding(&ctx, 0); Time_F(START); if (decrypt) for (count = 0, run = 1; COND(save_count * 4 * lengths[0] / lengths[j]); count++) EVP_DecryptUpdate(&ctx, buf, &outl, buf, lengths[j]); else for (count = 0, run = 1; COND(save_count * 4 * lengths[0] / lengths[j]); count++) EVP_EncryptUpdate(&ctx, buf, &outl, buf, lengths[j]); if (decrypt) EVP_DecryptFinal_ex(&ctx, buf, &outl); else EVP_EncryptFinal_ex(&ctx, buf, &outl); d = Time_F(STOP); EVP_CIPHER_CTX_cleanup(&ctx); } if (evp_md) { names[D_EVP] = OBJ_nid2ln(evp_md->type); print_message(names[D_EVP], save_count, lengths[j]); Time_F(START); for (count = 0, run = 1; COND(save_count * 4 * lengths[0] / lengths[j]); count++) EVP_Digest(buf, lengths[j], &(md[0]), NULL, evp_md, NULL); d = Time_F(STOP); } print_result(D_EVP, j, count, d); } } # ifndef OPENSSL_SYS_WIN32 # endif RAND_pseudo_bytes(buf, 36); # ifndef OPENSSL_NO_RSA for (j = 0; j < RSA_NUM; j++) { int ret; if (!rsa_doit[j]) continue; ret = RSA_sign(NID_md5_sha1, buf, 36, buf2, &rsa_num, rsa_key[j]); if (ret == 0) { BIO_printf(bio_err, "RSA sign failure. No RSA sign will be done.\n"); ERR_print_errors(bio_err); rsa_count = 1; } else { pkey_print_message("private", "rsa", rsa_c[j][0], rsa_bits[j], RSA_SECONDS); Time_F(START); for (count = 0, run = 1; COND(rsa_c[j][0]); count++) { ret = RSA_sign(NID_md5_sha1, buf, 36, buf2, &rsa_num, rsa_key[j]); if (ret == 0) { BIO_printf(bio_err, "RSA sign failure\n"); ERR_print_errors(bio_err); count = 1; break; } } d = Time_F(STOP); BIO_printf(bio_err, mr ? "+R1:%ld:%d:%.2f\n" : "%ld %d bit private RSA's in %.2fs\n", count, rsa_bits[j], d); rsa_results[j][0] = d / (double)count; rsa_count = count; } # if 1 ret = RSA_verify(NID_md5_sha1, buf, 36, buf2, rsa_num, rsa_key[j]); if (ret <= 0) { BIO_printf(bio_err, "RSA verify failure. No RSA verify will be done.\n"); ERR_print_errors(bio_err); rsa_doit[j] = 0; } else { pkey_print_message("public", "rsa", rsa_c[j][1], rsa_bits[j], RSA_SECONDS); Time_F(START); for (count = 0, run = 1; COND(rsa_c[j][1]); count++) { ret = RSA_verify(NID_md5_sha1, buf, 36, buf2, rsa_num, rsa_key[j]); if (ret <= 0) { BIO_printf(bio_err, "RSA verify failure\n"); ERR_print_errors(bio_err); count = 1; break; } } d = Time_F(STOP); BIO_printf(bio_err, mr ? "+R2:%ld:%d:%.2f\n" : "%ld %d bit public RSA's in %.2fs\n", count, rsa_bits[j], d); rsa_results[j][1] = d / (double)count; } # endif if (rsa_count <= 1) { for (j++; j < RSA_NUM; j++) rsa_doit[j] = 0; } } # endif RAND_pseudo_bytes(buf, 20); # ifndef OPENSSL_NO_DSA if (RAND_status() != 1) { RAND_seed(rnd_seed, sizeof rnd_seed); rnd_fake = 1; } for (j = 0; j < DSA_NUM; j++) { unsigned int kk; int ret; if (!dsa_doit[j]) continue; ret = DSA_sign(EVP_PKEY_DSA, buf, 20, buf2, &kk, dsa_key[j]); if (ret == 0) { BIO_printf(bio_err, "DSA sign failure. No DSA sign will be done.\n"); ERR_print_errors(bio_err); rsa_count = 1; } else { pkey_print_message("sign", "dsa", dsa_c[j][0], dsa_bits[j], DSA_SECONDS); Time_F(START); for (count = 0, run = 1; COND(dsa_c[j][0]); count++) { ret = DSA_sign(EVP_PKEY_DSA, buf, 20, buf2, &kk, dsa_key[j]); if (ret == 0) { BIO_printf(bio_err, "DSA sign failure\n"); ERR_print_errors(bio_err); count = 1; break; } } d = Time_F(STOP); BIO_printf(bio_err, mr ? "+R3:%ld:%d:%.2f\n" : "%ld %d bit DSA signs in %.2fs\n", count, dsa_bits[j], d); dsa_results[j][0] = d / (double)count; rsa_count = count; } ret = DSA_verify(EVP_PKEY_DSA, buf, 20, buf2, kk, dsa_key[j]); if (ret <= 0) { BIO_printf(bio_err, "DSA verify failure. No DSA verify will be done.\n"); ERR_print_errors(bio_err); dsa_doit[j] = 0; } else { pkey_print_message("verify", "dsa", dsa_c[j][1], dsa_bits[j], DSA_SECONDS); Time_F(START); for (count = 0, run = 1; COND(dsa_c[j][1]); count++) { ret = DSA_verify(EVP_PKEY_DSA, buf, 20, buf2, kk, dsa_key[j]); if (ret <= 0) { BIO_printf(bio_err, "DSA verify failure\n"); ERR_print_errors(bio_err); count = 1; break; } } d = Time_F(STOP); BIO_printf(bio_err, mr ? "+R4:%ld:%d:%.2f\n" : "%ld %d bit DSA verify in %.2fs\n", count, dsa_bits[j], d); dsa_results[j][1] = d / (double)count; } if (rsa_count <= 1) { for (j++; j < DSA_NUM; j++) dsa_doit[j] = 0; } } if (rnd_fake) RAND_cleanup(); # endif # ifndef OPENSSL_NO_ECDSA if (RAND_status() != 1) { RAND_seed(rnd_seed, sizeof rnd_seed); rnd_fake = 1; } for (j = 0; j < EC_NUM; j++) { int ret; if (!ecdsa_doit[j]) continue; ecdsa[j] = EC_KEY_new_by_curve_name(test_curves[j]); if (ecdsa[j] == NULL) { BIO_printf(bio_err, "ECDSA failure.\n"); ERR_print_errors(bio_err); rsa_count = 1; } else { # if 1 EC_KEY_precompute_mult(ecdsa[j], NULL); # endif EC_KEY_generate_key(ecdsa[j]); ret = ECDSA_sign(0, buf, 20, ecdsasig, &ecdsasiglen, ecdsa[j]); if (ret == 0) { BIO_printf(bio_err, "ECDSA sign failure. No ECDSA sign will be done.\n"); ERR_print_errors(bio_err); rsa_count = 1; } else { pkey_print_message("sign", "ecdsa", ecdsa_c[j][0], test_curves_bits[j], ECDSA_SECONDS); Time_F(START); for (count = 0, run = 1; COND(ecdsa_c[j][0]); count++) { ret = ECDSA_sign(0, buf, 20, ecdsasig, &ecdsasiglen, ecdsa[j]); if (ret == 0) { BIO_printf(bio_err, "ECDSA sign failure\n"); ERR_print_errors(bio_err); count = 1; break; } } d = Time_F(STOP); BIO_printf(bio_err, mr ? "+R5:%ld:%d:%.2f\n" : "%ld %d bit ECDSA signs in %.2fs \n", count, test_curves_bits[j], d); ecdsa_results[j][0] = d / (double)count; rsa_count = count; } ret = ECDSA_verify(0, buf, 20, ecdsasig, ecdsasiglen, ecdsa[j]); if (ret != 1) { BIO_printf(bio_err, "ECDSA verify failure. No ECDSA verify will be done.\n"); ERR_print_errors(bio_err); ecdsa_doit[j] = 0; } else { pkey_print_message("verify", "ecdsa", ecdsa_c[j][1], test_curves_bits[j], ECDSA_SECONDS); Time_F(START); for (count = 0, run = 1; COND(ecdsa_c[j][1]); count++) { ret = ECDSA_verify(0, buf, 20, ecdsasig, ecdsasiglen, ecdsa[j]); if (ret != 1) { BIO_printf(bio_err, "ECDSA verify failure\n"); ERR_print_errors(bio_err); count = 1; break; } } d = Time_F(STOP); BIO_printf(bio_err, mr ? "+R6:%ld:%d:%.2f\n" : "%ld %d bit ECDSA verify in %.2fs\n", count, test_curves_bits[j], d); ecdsa_results[j][1] = d / (double)count; } if (rsa_count <= 1) { for (j++; j < EC_NUM; j++) ecdsa_doit[j] = 0; } } } if (rnd_fake) RAND_cleanup(); # endif # ifndef OPENSSL_NO_ECDH if (RAND_status() != 1) { RAND_seed(rnd_seed, sizeof rnd_seed); rnd_fake = 1; } for (j = 0; j < EC_NUM; j++) { if (!ecdh_doit[j]) continue; ecdh_a[j] = EC_KEY_new_by_curve_name(test_curves[j]); ecdh_b[j] = EC_KEY_new_by_curve_name(test_curves[j]); if ((ecdh_a[j] == NULL) || (ecdh_b[j] == NULL)) { BIO_printf(bio_err, "ECDH failure.\n"); ERR_print_errors(bio_err); rsa_count = 1; } else { if (!EC_KEY_generate_key(ecdh_a[j]) || !EC_KEY_generate_key(ecdh_b[j])) { BIO_printf(bio_err, "ECDH key generation failure.\n"); ERR_print_errors(bio_err); rsa_count = 1; } else { int field_size, outlen; void *(*kdf) (const void *in, size_t inlen, void *out, size_t *xoutlen); field_size = EC_GROUP_get_degree(EC_KEY_get0_group(ecdh_a[j])); if (field_size <= 24 * 8) { outlen = KDF1_SHA1_len; kdf = KDF1_SHA1; } else { outlen = (field_size + 7) / 8; kdf = NULL; } secret_size_a = ECDH_compute_key(secret_a, outlen, EC_KEY_get0_public_key(ecdh_b[j]), ecdh_a[j], kdf); secret_size_b = ECDH_compute_key(secret_b, outlen, EC_KEY_get0_public_key(ecdh_a[j]), ecdh_b[j], kdf); if (secret_size_a != secret_size_b) ecdh_checks = 0; else ecdh_checks = 1; for (secret_idx = 0; (secret_idx < secret_size_a) && (ecdh_checks == 1); secret_idx++) { if (secret_a[secret_idx] != secret_b[secret_idx]) ecdh_checks = 0; } if (ecdh_checks == 0) { BIO_printf(bio_err, "ECDH computations don't match.\n"); ERR_print_errors(bio_err); rsa_count = 1; } pkey_print_message("", "ecdh", ecdh_c[j][0], test_curves_bits[j], ECDH_SECONDS); Time_F(START); for (count = 0, run = 1; COND(ecdh_c[j][0]); count++) { ECDH_compute_key(secret_a, outlen, EC_KEY_get0_public_key(ecdh_b[j]), ecdh_a[j], kdf); } d = Time_F(STOP); BIO_printf(bio_err, mr ? "+R7:%ld:%d:%.2f\n" : "%ld %d-bit ECDH ops in %.2fs\n", count, test_curves_bits[j], d); ecdh_results[j][0] = d / (double)count; rsa_count = count; } } if (rsa_count <= 1) { for (j++; j < EC_NUM; j++) ecdh_doit[j] = 0; } } if (rnd_fake) RAND_cleanup(); # endif # ifndef NO_FORK show_res: # endif if (!mr) { fprintf(stdout, "%s\n", SSLeay_version(SSLEAY_VERSION)); fprintf(stdout, "%s\n", SSLeay_version(SSLEAY_BUILT_ON)); printf("options:"); printf("%s ", BN_options()); # ifndef OPENSSL_NO_MD2 printf("%s ", MD2_options()); # endif # ifndef OPENSSL_NO_RC4 printf("%s ", RC4_options()); # endif # ifndef OPENSSL_NO_DES printf("%s ", DES_options()); # endif # ifndef OPENSSL_NO_AES printf("%s ", AES_options()); # endif # ifndef OPENSSL_NO_IDEA printf("%s ", idea_options()); # endif # ifndef OPENSSL_NO_BF printf("%s ", BF_options()); # endif fprintf(stdout, "\n%s\n", SSLeay_version(SSLEAY_CFLAGS)); } if (pr_header) { if (mr) fprintf(stdout, "+H"); else { fprintf(stdout, "The 'numbers' are in 1000s of bytes per second processed.\n"); fprintf(stdout, "type "); } for (j = 0; j < SIZE_NUM; j++) fprintf(stdout, mr ? ":%d" : "%7d bytes", lengths[j]); fprintf(stdout, "\n"); } for (k = 0; k < ALGOR_NUM; k++) { if (!doit[k]) continue; if (mr) fprintf(stdout, "+F:%d:%s", k, names[k]); else fprintf(stdout, "%-13s", names[k]); for (j = 0; j < SIZE_NUM; j++) { if (results[k][j] > 10000 && !mr) fprintf(stdout, " %11.2fk", results[k][j] / 1e3); else fprintf(stdout, mr ? ":%.2f" : " %11.2f ", results[k][j]); } fprintf(stdout, "\n"); } # ifndef OPENSSL_NO_RSA j = 1; for (k = 0; k < RSA_NUM; k++) { if (!rsa_doit[k]) continue; if (j && !mr) { printf("%18ssign verify sign/s verify/s\n", " "); j = 0; } if (mr) fprintf(stdout, "+F2:%u:%u:%f:%f\n", k, rsa_bits[k], rsa_results[k][0], rsa_results[k][1]); else fprintf(stdout, "rsa %4u bits %8.6fs %8.6fs %8.1f %8.1f\n", rsa_bits[k], rsa_results[k][0], rsa_results[k][1], 1.0 / rsa_results[k][0], 1.0 / rsa_results[k][1]); } # endif # ifndef OPENSSL_NO_DSA j = 1; for (k = 0; k < DSA_NUM; k++) { if (!dsa_doit[k]) continue; if (j && !mr) { printf("%18ssign verify sign/s verify/s\n", " "); j = 0; } if (mr) fprintf(stdout, "+F3:%u:%u:%f:%f\n", k, dsa_bits[k], dsa_results[k][0], dsa_results[k][1]); else fprintf(stdout, "dsa %4u bits %8.6fs %8.6fs %8.1f %8.1f\n", dsa_bits[k], dsa_results[k][0], dsa_results[k][1], 1.0 / dsa_results[k][0], 1.0 / dsa_results[k][1]); } # endif # ifndef OPENSSL_NO_ECDSA j = 1; for (k = 0; k < EC_NUM; k++) { if (!ecdsa_doit[k]) continue; if (j && !mr) { printf("%30ssign verify sign/s verify/s\n", " "); j = 0; } if (mr) fprintf(stdout, "+F4:%u:%u:%f:%f\n", k, test_curves_bits[k], ecdsa_results[k][0], ecdsa_results[k][1]); else fprintf(stdout, "%4u bit ecdsa (%s) %8.4fs %8.4fs %8.1f %8.1f\n", test_curves_bits[k], test_curves_names[k], ecdsa_results[k][0], ecdsa_results[k][1], 1.0 / ecdsa_results[k][0], 1.0 / ecdsa_results[k][1]); } # endif # ifndef OPENSSL_NO_ECDH j = 1; for (k = 0; k < EC_NUM; k++) { if (!ecdh_doit[k]) continue; if (j && !mr) { printf("%30sop op/s\n", " "); j = 0; } if (mr) fprintf(stdout, "+F5:%u:%u:%f:%f\n", k, test_curves_bits[k], ecdh_results[k][0], 1.0 / ecdh_results[k][0]); else fprintf(stdout, "%4u bit ecdh (%s) %8.4fs %8.1f\n", test_curves_bits[k], test_curves_names[k], ecdh_results[k][0], 1.0 / ecdh_results[k][0]); } # endif mret = 0; end: ERR_print_errors(bio_err); if (buf_malloc != NULL) OPENSSL_free(buf_malloc); if (buf2_malloc != NULL) OPENSSL_free(buf2_malloc); # ifndef OPENSSL_NO_RSA for (i = 0; i < RSA_NUM; i++) if (rsa_key[i] != NULL) RSA_free(rsa_key[i]); # endif # ifndef OPENSSL_NO_DSA for (i = 0; i < DSA_NUM; i++) if (dsa_key[i] != NULL) DSA_free(dsa_key[i]); # endif # ifndef OPENSSL_NO_ECDSA for (i = 0; i < EC_NUM; i++) if (ecdsa[i] != NULL) EC_KEY_free(ecdsa[i]); # endif # ifndef OPENSSL_NO_ECDH for (i = 0; i < EC_NUM; i++) { if (ecdh_a[i] != NULL) EC_KEY_free(ecdh_a[i]); if (ecdh_b[i] != NULL) EC_KEY_free(ecdh_b[i]); } # endif apps_shutdown(); OPENSSL_EXIT(mret); }
['int MAIN(int argc, char **argv)\n{\n unsigned char *buf_malloc = NULL, *buf2_malloc = NULL;\n unsigned char *buf = NULL, *buf2 = NULL;\n int mret = 1;\n long count = 0, save_count = 0;\n int i, j, k;\n# if !defined(OPENSSL_NO_RSA) || !defined(OPENSSL_NO_DSA)\n long rsa_count;\n# endif\n# ifndef OPENSSL_NO_RSA\n unsigned rsa_num;\n# endif\n unsigned char md[EVP_MAX_MD_SIZE];\n# ifndef OPENSSL_NO_MD2\n unsigned char md2[MD2_DIGEST_LENGTH];\n# endif\n# ifndef OPENSSL_NO_MDC2\n unsigned char mdc2[MDC2_DIGEST_LENGTH];\n# endif\n# ifndef OPENSSL_NO_MD4\n unsigned char md4[MD4_DIGEST_LENGTH];\n# endif\n# ifndef OPENSSL_NO_MD5\n unsigned char md5[MD5_DIGEST_LENGTH];\n unsigned char hmac[MD5_DIGEST_LENGTH];\n# endif\n# ifndef OPENSSL_NO_SHA\n unsigned char sha[SHA_DIGEST_LENGTH];\n# ifndef OPENSSL_NO_SHA256\n unsigned char sha256[SHA256_DIGEST_LENGTH];\n# endif\n# ifndef OPENSSL_NO_SHA512\n unsigned char sha512[SHA512_DIGEST_LENGTH];\n# endif\n# endif\n# ifndef OPENSSL_NO_WHIRLPOOL\n unsigned char whirlpool[WHIRLPOOL_DIGEST_LENGTH];\n# endif\n# ifndef OPENSSL_NO_RMD160\n unsigned char rmd160[RIPEMD160_DIGEST_LENGTH];\n# endif\n# ifndef OPENSSL_NO_RC4\n RC4_KEY rc4_ks;\n# endif\n# ifndef OPENSSL_NO_RC5\n RC5_32_KEY rc5_ks;\n# endif\n# ifndef OPENSSL_NO_RC2\n RC2_KEY rc2_ks;\n# endif\n# ifndef OPENSSL_NO_IDEA\n IDEA_KEY_SCHEDULE idea_ks;\n# endif\n# ifndef OPENSSL_NO_SEED\n SEED_KEY_SCHEDULE seed_ks;\n# endif\n# ifndef OPENSSL_NO_BF\n BF_KEY bf_ks;\n# endif\n# ifndef OPENSSL_NO_CAST\n CAST_KEY cast_ks;\n# endif\n static const unsigned char key16[16] = {\n 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,\n 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12\n };\n# ifndef OPENSSL_NO_AES\n static const unsigned char key24[24] = {\n 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,\n 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12,\n 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34\n };\n static const unsigned char key32[32] = {\n 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,\n 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12,\n 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34,\n 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56\n };\n# endif\n# ifndef OPENSSL_NO_CAMELLIA\n static const unsigned char ckey24[24] = {\n 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,\n 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12,\n 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34\n };\n static const unsigned char ckey32[32] = {\n 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,\n 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12,\n 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34,\n 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56\n };\n# endif\n# ifndef OPENSSL_NO_AES\n# define MAX_BLOCK_SIZE 128\n# else\n# define MAX_BLOCK_SIZE 64\n# endif\n unsigned char DES_iv[8];\n unsigned char iv[2 * MAX_BLOCK_SIZE / 8];\n# ifndef OPENSSL_NO_DES\n static DES_cblock key =\n { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0 };\n static DES_cblock key2 =\n { 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12 };\n static DES_cblock key3 =\n { 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34 };\n DES_key_schedule sch;\n DES_key_schedule sch2;\n DES_key_schedule sch3;\n# endif\n# ifndef OPENSSL_NO_AES\n AES_KEY aes_ks1, aes_ks2, aes_ks3;\n# endif\n# ifndef OPENSSL_NO_CAMELLIA\n CAMELLIA_KEY camellia_ks1, camellia_ks2, camellia_ks3;\n# endif\n# define D_MD2 0\n# define D_MDC2 1\n# define D_MD4 2\n# define D_MD5 3\n# define D_HMAC 4\n# define D_SHA1 5\n# define D_RMD160 6\n# define D_RC4 7\n# define D_CBC_DES 8\n# define D_EDE3_DES 9\n# define D_CBC_IDEA 10\n# define D_CBC_SEED 11\n# define D_CBC_RC2 12\n# define D_CBC_RC5 13\n# define D_CBC_BF 14\n# define D_CBC_CAST 15\n# define D_CBC_128_AES 16\n# define D_CBC_192_AES 17\n# define D_CBC_256_AES 18\n# define D_CBC_128_CML 19\n# define D_CBC_192_CML 20\n# define D_CBC_256_CML 21\n# define D_EVP 22\n# define D_SHA256 23\n# define D_SHA512 24\n# define D_WHIRLPOOL 25\n# define D_IGE_128_AES 26\n# define D_IGE_192_AES 27\n# define D_IGE_256_AES 28\n# define D_GHASH 29\n double d = 0.0;\n long c[ALGOR_NUM][SIZE_NUM];\n# ifndef OPENSSL_SYS_WIN32\n# endif\n# define R_DSA_512 0\n# define R_DSA_1024 1\n# define R_DSA_2048 2\n# define R_RSA_512 0\n# define R_RSA_1024 1\n# define R_RSA_2048 2\n# define R_RSA_3072 3\n# define R_RSA_4096 4\n# define R_RSA_7680 5\n# define R_RSA_15360 6\n# define R_EC_P160 0\n# define R_EC_P192 1\n# define R_EC_P224 2\n# define R_EC_P256 3\n# define R_EC_P384 4\n# define R_EC_P521 5\n# define R_EC_K163 6\n# define R_EC_K233 7\n# define R_EC_K283 8\n# define R_EC_K409 9\n# define R_EC_K571 10\n# define R_EC_B163 11\n# define R_EC_B233 12\n# define R_EC_B283 13\n# define R_EC_B409 14\n# define R_EC_B571 15\n# ifndef OPENSSL_NO_RSA\n RSA *rsa_key[RSA_NUM];\n long rsa_c[RSA_NUM][2];\n static unsigned int rsa_bits[RSA_NUM] = {\n 512, 1024, 2048, 3072, 4096, 7680, 15360\n };\n static unsigned char *rsa_data[RSA_NUM] = {\n test512, test1024, test2048, test3072, test4096, test7680, test15360\n };\n static int rsa_data_length[RSA_NUM] = {\n sizeof(test512), sizeof(test1024),\n sizeof(test2048), sizeof(test3072),\n sizeof(test4096), sizeof(test7680),\n sizeof(test15360)\n };\n# endif\n# ifndef OPENSSL_NO_DSA\n DSA *dsa_key[DSA_NUM];\n long dsa_c[DSA_NUM][2];\n static unsigned int dsa_bits[DSA_NUM] = { 512, 1024, 2048 };\n# endif\n# ifndef OPENSSL_NO_EC\n static unsigned int test_curves[EC_NUM] = {\n NID_secp160r1,\n NID_X9_62_prime192v1,\n NID_secp224r1,\n NID_X9_62_prime256v1,\n NID_secp384r1,\n NID_secp521r1,\n NID_sect163k1,\n NID_sect233k1,\n NID_sect283k1,\n NID_sect409k1,\n NID_sect571k1,\n NID_sect163r2,\n NID_sect233r1,\n NID_sect283r1,\n NID_sect409r1,\n NID_sect571r1\n };\n static const char *test_curves_names[EC_NUM] = {\n "secp160r1",\n "nistp192",\n "nistp224",\n "nistp256",\n "nistp384",\n "nistp521",\n "nistk163",\n "nistk233",\n "nistk283",\n "nistk409",\n "nistk571",\n "nistb163",\n "nistb233",\n "nistb283",\n "nistb409",\n "nistb571"\n };\n static int test_curves_bits[EC_NUM] = {\n 160, 192, 224, 256, 384, 521,\n 163, 233, 283, 409, 571,\n 163, 233, 283, 409, 571\n };\n# endif\n# ifndef OPENSSL_NO_ECDSA\n unsigned char ecdsasig[256];\n unsigned int ecdsasiglen;\n EC_KEY *ecdsa[EC_NUM];\n long ecdsa_c[EC_NUM][2];\n# endif\n# ifndef OPENSSL_NO_ECDH\n EC_KEY *ecdh_a[EC_NUM], *ecdh_b[EC_NUM];\n unsigned char secret_a[MAX_ECDH_SIZE], secret_b[MAX_ECDH_SIZE];\n int secret_size_a, secret_size_b;\n int ecdh_checks = 0;\n int secret_idx = 0;\n long ecdh_c[EC_NUM][2];\n# endif\n int rsa_doit[RSA_NUM];\n int dsa_doit[DSA_NUM];\n# ifndef OPENSSL_NO_ECDSA\n int ecdsa_doit[EC_NUM];\n# endif\n# ifndef OPENSSL_NO_ECDH\n int ecdh_doit[EC_NUM];\n# endif\n int doit[ALGOR_NUM];\n int pr_header = 0;\n const EVP_CIPHER *evp_cipher = NULL;\n const EVP_MD *evp_md = NULL;\n int decrypt = 0;\n# ifndef NO_FORK\n int multi = 0;\n# endif\n int multiblock = 0;\n int misalign = MAX_MISALIGNMENT + 1;\n# ifndef TIMES\n usertime = -1;\n# endif\n apps_startup();\n memset(results, 0, sizeof(results));\n# ifndef OPENSSL_NO_DSA\n memset(dsa_key, 0, sizeof(dsa_key));\n# endif\n# ifndef OPENSSL_NO_ECDSA\n for (i = 0; i < EC_NUM; i++)\n ecdsa[i] = NULL;\n# endif\n# ifndef OPENSSL_NO_ECDH\n for (i = 0; i < EC_NUM; i++) {\n ecdh_a[i] = NULL;\n ecdh_b[i] = NULL;\n }\n# endif\n if (bio_err == NULL)\n if ((bio_err = BIO_new(BIO_s_file())) != NULL)\n BIO_set_fp(bio_err, stderr, BIO_NOCLOSE | BIO_FP_TEXT);\n if (!load_config(bio_err, NULL))\n goto end;\n# ifndef OPENSSL_NO_RSA\n memset(rsa_key, 0, sizeof(rsa_key));\n for (i = 0; i < RSA_NUM; i++)\n rsa_key[i] = NULL;\n# endif\n if ((buf_malloc =\n (unsigned char *)OPENSSL_malloc(BUFSIZE + misalign)) == NULL) {\n BIO_printf(bio_err, "out of memory\\n");\n goto end;\n }\n if ((buf2_malloc =\n (unsigned char *)OPENSSL_malloc(BUFSIZE + misalign)) == NULL) {\n BIO_printf(bio_err, "out of memory\\n");\n goto end;\n }\n misalign = 0;\n buf = buf_malloc;\n buf2 = buf2_malloc;\n memset(c, 0, sizeof(c));\n memset(DES_iv, 0, sizeof(DES_iv));\n memset(iv, 0, sizeof(iv));\n for (i = 0; i < ALGOR_NUM; i++)\n doit[i] = 0;\n for (i = 0; i < RSA_NUM; i++)\n rsa_doit[i] = 0;\n for (i = 0; i < DSA_NUM; i++)\n dsa_doit[i] = 0;\n# ifndef OPENSSL_NO_ECDSA\n for (i = 0; i < EC_NUM; i++)\n ecdsa_doit[i] = 0;\n# endif\n# ifndef OPENSSL_NO_ECDH\n for (i = 0; i < EC_NUM; i++)\n ecdh_doit[i] = 0;\n# endif\n j = 0;\n argc--;\n argv++;\n while (argc) {\n if ((argc > 0) && (strcmp(*argv, "-elapsed") == 0)) {\n usertime = 0;\n j--;\n } else if ((argc > 0) && (strcmp(*argv, "-evp") == 0)) {\n argc--;\n argv++;\n if (argc == 0) {\n BIO_printf(bio_err, "no EVP given\\n");\n goto end;\n }\n evp_cipher = EVP_get_cipherbyname(*argv);\n if (!evp_cipher) {\n evp_md = EVP_get_digestbyname(*argv);\n }\n if (!evp_cipher && !evp_md) {\n BIO_printf(bio_err, "%s is an unknown cipher or digest\\n",\n *argv);\n goto end;\n }\n doit[D_EVP] = 1;\n } else if (argc > 0 && !strcmp(*argv, "-decrypt")) {\n decrypt = 1;\n j--;\n }\n# ifndef OPENSSL_NO_ENGINE\n else if ((argc > 0) && (strcmp(*argv, "-engine") == 0)) {\n argc--;\n argv++;\n if (argc == 0) {\n BIO_printf(bio_err, "no engine given\\n");\n goto end;\n }\n setup_engine(bio_err, *argv, 0);\n j--;\n }\n# endif\n# ifndef NO_FORK\n else if ((argc > 0) && (strcmp(*argv, "-multi") == 0)) {\n argc--;\n argv++;\n if (argc == 0) {\n BIO_printf(bio_err, "no multi count given\\n");\n goto end;\n }\n multi = atoi(argv[0]);\n if (multi <= 0) {\n BIO_printf(bio_err, "bad multi count\\n");\n goto end;\n }\n j--;\n }\n# endif\n else if (argc > 0 && !strcmp(*argv, "-mr")) {\n mr = 1;\n j--;\n } else if (argc > 0 && !strcmp(*argv, "-mb")) {\n multiblock = 1;\n j--;\n } else if (argc > 0 && !strcmp(*argv, "-misalign")) {\n argc--;\n argv++;\n if (argc == 0) {\n BIO_printf(bio_err, "no misalignment given\\n");\n goto end;\n }\n misalign = atoi(argv[0]);\n if (misalign < 0 || misalign > MAX_MISALIGNMENT) {\n BIO_printf(bio_err,\n "misalignment is outsize permitted range 0-%d\\n",\n MAX_MISALIGNMENT);\n goto end;\n }\n buf = buf_malloc + misalign;\n buf2 = buf2_malloc + misalign;\n j--;\n } else\n# ifndef OPENSSL_NO_MD2\n if (strcmp(*argv, "md2") == 0)\n doit[D_MD2] = 1;\n else\n# endif\n# ifndef OPENSSL_NO_MDC2\n if (strcmp(*argv, "mdc2") == 0)\n doit[D_MDC2] = 1;\n else\n# endif\n# ifndef OPENSSL_NO_MD4\n if (strcmp(*argv, "md4") == 0)\n doit[D_MD4] = 1;\n else\n# endif\n# ifndef OPENSSL_NO_MD5\n if (strcmp(*argv, "md5") == 0)\n doit[D_MD5] = 1;\n else\n# endif\n# ifndef OPENSSL_NO_MD5\n if (strcmp(*argv, "hmac") == 0)\n doit[D_HMAC] = 1;\n else\n# endif\n# ifndef OPENSSL_NO_SHA\n if (strcmp(*argv, "sha1") == 0)\n doit[D_SHA1] = 1;\n else if (strcmp(*argv, "sha") == 0)\n doit[D_SHA1] = 1, doit[D_SHA256] = 1, doit[D_SHA512] = 1;\n else\n# ifndef OPENSSL_NO_SHA256\n if (strcmp(*argv, "sha256") == 0)\n doit[D_SHA256] = 1;\n else\n# endif\n# ifndef OPENSSL_NO_SHA512\n if (strcmp(*argv, "sha512") == 0)\n doit[D_SHA512] = 1;\n else\n# endif\n# endif\n# ifndef OPENSSL_NO_WHIRLPOOL\n if (strcmp(*argv, "whirlpool") == 0)\n doit[D_WHIRLPOOL] = 1;\n else\n# endif\n# ifndef OPENSSL_NO_RMD160\n if (strcmp(*argv, "ripemd") == 0)\n doit[D_RMD160] = 1;\n else if (strcmp(*argv, "rmd160") == 0)\n doit[D_RMD160] = 1;\n else if (strcmp(*argv, "ripemd160") == 0)\n doit[D_RMD160] = 1;\n else\n# endif\n# ifndef OPENSSL_NO_RC4\n if (strcmp(*argv, "rc4") == 0)\n doit[D_RC4] = 1;\n else\n# endif\n# ifndef OPENSSL_NO_DES\n if (strcmp(*argv, "des-cbc") == 0)\n doit[D_CBC_DES] = 1;\n else if (strcmp(*argv, "des-ede3") == 0)\n doit[D_EDE3_DES] = 1;\n else\n# endif\n# ifndef OPENSSL_NO_AES\n if (strcmp(*argv, "aes-128-cbc") == 0)\n doit[D_CBC_128_AES] = 1;\n else if (strcmp(*argv, "aes-192-cbc") == 0)\n doit[D_CBC_192_AES] = 1;\n else if (strcmp(*argv, "aes-256-cbc") == 0)\n doit[D_CBC_256_AES] = 1;\n else if (strcmp(*argv, "aes-128-ige") == 0)\n doit[D_IGE_128_AES] = 1;\n else if (strcmp(*argv, "aes-192-ige") == 0)\n doit[D_IGE_192_AES] = 1;\n else if (strcmp(*argv, "aes-256-ige") == 0)\n doit[D_IGE_256_AES] = 1;\n else\n# endif\n# ifndef OPENSSL_NO_CAMELLIA\n if (strcmp(*argv, "camellia-128-cbc") == 0)\n doit[D_CBC_128_CML] = 1;\n else if (strcmp(*argv, "camellia-192-cbc") == 0)\n doit[D_CBC_192_CML] = 1;\n else if (strcmp(*argv, "camellia-256-cbc") == 0)\n doit[D_CBC_256_CML] = 1;\n else\n# endif\n# ifndef OPENSSL_NO_RSA\n# if 0\n if (strcmp(*argv, "rsaref") == 0) {\n RSA_set_default_openssl_method(RSA_PKCS1_RSAref());\n j--;\n } else\n# endif\n# ifndef RSA_NULL\n if (strcmp(*argv, "openssl") == 0) {\n RSA_set_default_method(RSA_PKCS1_SSLeay());\n j--;\n } else\n# endif\n# endif\n if (strcmp(*argv, "dsa512") == 0)\n dsa_doit[R_DSA_512] = 2;\n else if (strcmp(*argv, "dsa1024") == 0)\n dsa_doit[R_DSA_1024] = 2;\n else if (strcmp(*argv, "dsa2048") == 0)\n dsa_doit[R_DSA_2048] = 2;\n else if (strcmp(*argv, "rsa512") == 0)\n rsa_doit[R_RSA_512] = 2;\n else if (strcmp(*argv, "rsa1024") == 0)\n rsa_doit[R_RSA_1024] = 2;\n else if (strcmp(*argv, "rsa2048") == 0)\n rsa_doit[R_RSA_2048] = 2;\n else if (strcmp(*argv, "rsa3072") == 0)\n rsa_doit[R_RSA_3072] = 2;\n else if (strcmp(*argv, "rsa4096") == 0)\n rsa_doit[R_RSA_4096] = 2;\n else if (strcmp(*argv, "rsa7680") == 0)\n rsa_doit[R_RSA_7680] = 2;\n else if (strcmp(*argv, "rsa15360") == 0)\n rsa_doit[R_RSA_15360] = 2;\n else\n# ifndef OPENSSL_NO_RC2\n if (strcmp(*argv, "rc2-cbc") == 0)\n doit[D_CBC_RC2] = 1;\n else if (strcmp(*argv, "rc2") == 0)\n doit[D_CBC_RC2] = 1;\n else\n# endif\n# ifndef OPENSSL_NO_RC5\n if (strcmp(*argv, "rc5-cbc") == 0)\n doit[D_CBC_RC5] = 1;\n else if (strcmp(*argv, "rc5") == 0)\n doit[D_CBC_RC5] = 1;\n else\n# endif\n# ifndef OPENSSL_NO_IDEA\n if (strcmp(*argv, "idea-cbc") == 0)\n doit[D_CBC_IDEA] = 1;\n else if (strcmp(*argv, "idea") == 0)\n doit[D_CBC_IDEA] = 1;\n else\n# endif\n# ifndef OPENSSL_NO_SEED\n if (strcmp(*argv, "seed-cbc") == 0)\n doit[D_CBC_SEED] = 1;\n else if (strcmp(*argv, "seed") == 0)\n doit[D_CBC_SEED] = 1;\n else\n# endif\n# ifndef OPENSSL_NO_BF\n if (strcmp(*argv, "bf-cbc") == 0)\n doit[D_CBC_BF] = 1;\n else if (strcmp(*argv, "blowfish") == 0)\n doit[D_CBC_BF] = 1;\n else if (strcmp(*argv, "bf") == 0)\n doit[D_CBC_BF] = 1;\n else\n# endif\n# ifndef OPENSSL_NO_CAST\n if (strcmp(*argv, "cast-cbc") == 0)\n doit[D_CBC_CAST] = 1;\n else if (strcmp(*argv, "cast") == 0)\n doit[D_CBC_CAST] = 1;\n else if (strcmp(*argv, "cast5") == 0)\n doit[D_CBC_CAST] = 1;\n else\n# endif\n# ifndef OPENSSL_NO_DES\n if (strcmp(*argv, "des") == 0) {\n doit[D_CBC_DES] = 1;\n doit[D_EDE3_DES] = 1;\n } else\n# endif\n# ifndef OPENSSL_NO_AES\n if (strcmp(*argv, "aes") == 0) {\n doit[D_CBC_128_AES] = 1;\n doit[D_CBC_192_AES] = 1;\n doit[D_CBC_256_AES] = 1;\n } else if (strcmp(*argv, "ghash") == 0) {\n doit[D_GHASH] = 1;\n } else\n# endif\n# ifndef OPENSSL_NO_CAMELLIA\n if (strcmp(*argv, "camellia") == 0) {\n doit[D_CBC_128_CML] = 1;\n doit[D_CBC_192_CML] = 1;\n doit[D_CBC_256_CML] = 1;\n } else\n# endif\n# ifndef OPENSSL_NO_RSA\n if (strcmp(*argv, "rsa") == 0) {\n rsa_doit[R_RSA_512] = 1;\n rsa_doit[R_RSA_1024] = 1;\n rsa_doit[R_RSA_2048] = 1;\n rsa_doit[R_RSA_3072] = 1;\n rsa_doit[R_RSA_4096] = 1;\n rsa_doit[R_RSA_7680] = 1;\n rsa_doit[R_RSA_15360] = 1;\n } else\n# endif\n# ifndef OPENSSL_NO_DSA\n if (strcmp(*argv, "dsa") == 0) {\n dsa_doit[R_DSA_512] = 1;\n dsa_doit[R_DSA_1024] = 1;\n dsa_doit[R_DSA_2048] = 1;\n } else\n# endif\n# ifndef OPENSSL_NO_ECDSA\n if (strcmp(*argv, "ecdsap160") == 0)\n ecdsa_doit[R_EC_P160] = 2;\n else if (strcmp(*argv, "ecdsap192") == 0)\n ecdsa_doit[R_EC_P192] = 2;\n else if (strcmp(*argv, "ecdsap224") == 0)\n ecdsa_doit[R_EC_P224] = 2;\n else if (strcmp(*argv, "ecdsap256") == 0)\n ecdsa_doit[R_EC_P256] = 2;\n else if (strcmp(*argv, "ecdsap384") == 0)\n ecdsa_doit[R_EC_P384] = 2;\n else if (strcmp(*argv, "ecdsap521") == 0)\n ecdsa_doit[R_EC_P521] = 2;\n else if (strcmp(*argv, "ecdsak163") == 0)\n ecdsa_doit[R_EC_K163] = 2;\n else if (strcmp(*argv, "ecdsak233") == 0)\n ecdsa_doit[R_EC_K233] = 2;\n else if (strcmp(*argv, "ecdsak283") == 0)\n ecdsa_doit[R_EC_K283] = 2;\n else if (strcmp(*argv, "ecdsak409") == 0)\n ecdsa_doit[R_EC_K409] = 2;\n else if (strcmp(*argv, "ecdsak571") == 0)\n ecdsa_doit[R_EC_K571] = 2;\n else if (strcmp(*argv, "ecdsab163") == 0)\n ecdsa_doit[R_EC_B163] = 2;\n else if (strcmp(*argv, "ecdsab233") == 0)\n ecdsa_doit[R_EC_B233] = 2;\n else if (strcmp(*argv, "ecdsab283") == 0)\n ecdsa_doit[R_EC_B283] = 2;\n else if (strcmp(*argv, "ecdsab409") == 0)\n ecdsa_doit[R_EC_B409] = 2;\n else if (strcmp(*argv, "ecdsab571") == 0)\n ecdsa_doit[R_EC_B571] = 2;\n else if (strcmp(*argv, "ecdsa") == 0) {\n for (i = 0; i < EC_NUM; i++)\n ecdsa_doit[i] = 1;\n } else\n# endif\n# ifndef OPENSSL_NO_ECDH\n if (strcmp(*argv, "ecdhp160") == 0)\n ecdh_doit[R_EC_P160] = 2;\n else if (strcmp(*argv, "ecdhp192") == 0)\n ecdh_doit[R_EC_P192] = 2;\n else if (strcmp(*argv, "ecdhp224") == 0)\n ecdh_doit[R_EC_P224] = 2;\n else if (strcmp(*argv, "ecdhp256") == 0)\n ecdh_doit[R_EC_P256] = 2;\n else if (strcmp(*argv, "ecdhp384") == 0)\n ecdh_doit[R_EC_P384] = 2;\n else if (strcmp(*argv, "ecdhp521") == 0)\n ecdh_doit[R_EC_P521] = 2;\n else if (strcmp(*argv, "ecdhk163") == 0)\n ecdh_doit[R_EC_K163] = 2;\n else if (strcmp(*argv, "ecdhk233") == 0)\n ecdh_doit[R_EC_K233] = 2;\n else if (strcmp(*argv, "ecdhk283") == 0)\n ecdh_doit[R_EC_K283] = 2;\n else if (strcmp(*argv, "ecdhk409") == 0)\n ecdh_doit[R_EC_K409] = 2;\n else if (strcmp(*argv, "ecdhk571") == 0)\n ecdh_doit[R_EC_K571] = 2;\n else if (strcmp(*argv, "ecdhb163") == 0)\n ecdh_doit[R_EC_B163] = 2;\n else if (strcmp(*argv, "ecdhb233") == 0)\n ecdh_doit[R_EC_B233] = 2;\n else if (strcmp(*argv, "ecdhb283") == 0)\n ecdh_doit[R_EC_B283] = 2;\n else if (strcmp(*argv, "ecdhb409") == 0)\n ecdh_doit[R_EC_B409] = 2;\n else if (strcmp(*argv, "ecdhb571") == 0)\n ecdh_doit[R_EC_B571] = 2;\n else if (strcmp(*argv, "ecdh") == 0) {\n for (i = 0; i < EC_NUM; i++)\n ecdh_doit[i] = 1;\n } else\n# endif\n {\n BIO_printf(bio_err, "Error: bad option or value\\n");\n BIO_printf(bio_err, "\\n");\n BIO_printf(bio_err, "Available values:\\n");\n# ifndef OPENSSL_NO_MD2\n BIO_printf(bio_err, "md2 ");\n# endif\n# ifndef OPENSSL_NO_MDC2\n BIO_printf(bio_err, "mdc2 ");\n# endif\n# ifndef OPENSSL_NO_MD4\n BIO_printf(bio_err, "md4 ");\n# endif\n# ifndef OPENSSL_NO_MD5\n BIO_printf(bio_err, "md5 ");\n# ifndef OPENSSL_NO_HMAC\n BIO_printf(bio_err, "hmac ");\n# endif\n# endif\n# ifndef OPENSSL_NO_SHA1\n BIO_printf(bio_err, "sha1 ");\n# endif\n# ifndef OPENSSL_NO_SHA256\n BIO_printf(bio_err, "sha256 ");\n# endif\n# ifndef OPENSSL_NO_SHA512\n BIO_printf(bio_err, "sha512 ");\n# endif\n# ifndef OPENSSL_NO_WHIRLPOOL\n BIO_printf(bio_err, "whirlpool");\n# endif\n# ifndef OPENSSL_NO_RMD160\n BIO_printf(bio_err, "rmd160");\n# endif\n# if !defined(OPENSSL_NO_MD2) || !defined(OPENSSL_NO_MDC2) || \\\n !defined(OPENSSL_NO_MD4) || !defined(OPENSSL_NO_MD5) || \\\n !defined(OPENSSL_NO_SHA1) || !defined(OPENSSL_NO_RMD160) || \\\n !defined(OPENSSL_NO_WHIRLPOOL)\n BIO_printf(bio_err, "\\n");\n# endif\n# ifndef OPENSSL_NO_IDEA\n BIO_printf(bio_err, "idea-cbc ");\n# endif\n# ifndef OPENSSL_NO_SEED\n BIO_printf(bio_err, "seed-cbc ");\n# endif\n# ifndef OPENSSL_NO_RC2\n BIO_printf(bio_err, "rc2-cbc ");\n# endif\n# ifndef OPENSSL_NO_RC5\n BIO_printf(bio_err, "rc5-cbc ");\n# endif\n# ifndef OPENSSL_NO_BF\n BIO_printf(bio_err, "bf-cbc");\n# endif\n# if !defined(OPENSSL_NO_IDEA) || !defined(OPENSSL_NO_SEED) || !defined(OPENSSL_NO_RC2) || \\\n !defined(OPENSSL_NO_BF) || !defined(OPENSSL_NO_RC5)\n BIO_printf(bio_err, "\\n");\n# endif\n# ifndef OPENSSL_NO_DES\n BIO_printf(bio_err, "des-cbc des-ede3 ");\n# endif\n# ifndef OPENSSL_NO_AES\n BIO_printf(bio_err, "aes-128-cbc aes-192-cbc aes-256-cbc ");\n BIO_printf(bio_err, "aes-128-ige aes-192-ige aes-256-ige ");\n# endif\n# ifndef OPENSSL_NO_CAMELLIA\n BIO_printf(bio_err, "\\n");\n BIO_printf(bio_err,\n "camellia-128-cbc camellia-192-cbc camellia-256-cbc ");\n# endif\n# ifndef OPENSSL_NO_RC4\n BIO_printf(bio_err, "rc4");\n# endif\n BIO_printf(bio_err, "\\n");\n# ifndef OPENSSL_NO_RSA\n BIO_printf(bio_err,\n "rsa512 rsa1024 rsa2048 rsa3072 rsa4096\\n");\n BIO_printf(bio_err, "rsa7680 rsa15360\\n");\n# endif\n# ifndef OPENSSL_NO_DSA\n BIO_printf(bio_err, "dsa512 dsa1024 dsa2048\\n");\n# endif\n# ifndef OPENSSL_NO_ECDSA\n BIO_printf(bio_err, "ecdsap160 ecdsap192 ecdsap224 "\n "ecdsap256 ecdsap384 ecdsap521\\n");\n BIO_printf(bio_err,\n "ecdsak163 ecdsak233 ecdsak283 ecdsak409 ecdsak571\\n");\n BIO_printf(bio_err,\n "ecdsab163 ecdsab233 ecdsab283 ecdsab409 ecdsab571\\n");\n BIO_printf(bio_err, "ecdsa\\n");\n# endif\n# ifndef OPENSSL_NO_ECDH\n BIO_printf(bio_err, "ecdhp160 ecdhp192 ecdhp224 "\n "ecdhp256 ecdhp384 ecdhp521\\n");\n BIO_printf(bio_err,\n "ecdhk163 ecdhk233 ecdhk283 ecdhk409 ecdhk571\\n");\n BIO_printf(bio_err,\n "ecdhb163 ecdhb233 ecdhb283 ecdhb409 ecdhb571\\n");\n BIO_printf(bio_err, "ecdh\\n");\n# endif\n# ifndef OPENSSL_NO_IDEA\n BIO_printf(bio_err, "idea ");\n# endif\n# ifndef OPENSSL_NO_SEED\n BIO_printf(bio_err, "seed ");\n# endif\n# ifndef OPENSSL_NO_RC2\n BIO_printf(bio_err, "rc2 ");\n# endif\n# ifndef OPENSSL_NO_DES\n BIO_printf(bio_err, "des ");\n# endif\n# ifndef OPENSSL_NO_AES\n BIO_printf(bio_err, "aes ");\n# endif\n# ifndef OPENSSL_NO_CAMELLIA\n BIO_printf(bio_err, "camellia ");\n# endif\n# ifndef OPENSSL_NO_RSA\n BIO_printf(bio_err, "rsa ");\n# endif\n# ifndef OPENSSL_NO_BF\n BIO_printf(bio_err, "blowfish");\n# endif\n# if !defined(OPENSSL_NO_IDEA) || !defined(OPENSSL_NO_SEED) || \\\n !defined(OPENSSL_NO_RC2) || !defined(OPENSSL_NO_DES) || \\\n !defined(OPENSSL_NO_RSA) || !defined(OPENSSL_NO_BF) || \\\n !defined(OPENSSL_NO_AES) || !defined(OPENSSL_NO_CAMELLIA)\n BIO_printf(bio_err, "\\n");\n# endif\n BIO_printf(bio_err, "\\n");\n BIO_printf(bio_err, "Available options:\\n");\n# if defined(TIMES) || defined(USE_TOD)\n BIO_printf(bio_err, "-elapsed "\n "measure time in real time instead of CPU user time.\\n");\n# endif\n# ifndef OPENSSL_NO_ENGINE\n BIO_printf(bio_err,\n "-engine e "\n "use engine e, possibly a hardware device.\\n");\n# endif\n BIO_printf(bio_err, "-evp e " "use EVP e.\\n");\n BIO_printf(bio_err,\n "-decrypt "\n "time decryption instead of encryption (only EVP).\\n");\n BIO_printf(bio_err,\n "-mr "\n "produce machine readable output.\\n");\n BIO_printf(bio_err,\n "-mb "\n "perform multi-block benchmark (for specific ciphers)\\n");\n BIO_printf(bio_err,\n "-misalign n "\n "perform benchmark with misaligned data\\n");\n# ifndef NO_FORK\n BIO_printf(bio_err,\n "-multi n " "run n benchmarks in parallel.\\n");\n# endif\n goto end;\n }\n argc--;\n argv++;\n j++;\n }\n# ifndef NO_FORK\n if (multi && do_multi(multi))\n goto show_res;\n# endif\n if (j == 0) {\n for (i = 0; i < ALGOR_NUM; i++) {\n if (i != D_EVP)\n doit[i] = 1;\n }\n for (i = 0; i < RSA_NUM; i++)\n rsa_doit[i] = 1;\n for (i = 0; i < DSA_NUM; i++)\n dsa_doit[i] = 1;\n# ifndef OPENSSL_NO_ECDSA\n for (i = 0; i < EC_NUM; i++)\n ecdsa_doit[i] = 1;\n# endif\n# ifndef OPENSSL_NO_ECDH\n for (i = 0; i < EC_NUM; i++)\n ecdh_doit[i] = 1;\n# endif\n }\n for (i = 0; i < ALGOR_NUM; i++)\n if (doit[i])\n pr_header++;\n if (usertime == 0 && !mr)\n BIO_printf(bio_err,\n "You have chosen to measure elapsed time "\n "instead of user CPU time.\\n");\n# ifndef OPENSSL_NO_RSA\n for (i = 0; i < RSA_NUM; i++) {\n const unsigned char *p;\n p = rsa_data[i];\n rsa_key[i] = d2i_RSAPrivateKey(NULL, &p, rsa_data_length[i]);\n if (rsa_key[i] == NULL) {\n BIO_printf(bio_err, "internal error loading RSA key number %d\\n",\n i);\n goto end;\n }\n# if 0\n else {\n BIO_printf(bio_err,\n mr ? "+RK:%d:"\n : "Loaded RSA key, %d bit modulus and e= 0x",\n BN_num_bits(rsa_key[i]->n));\n BN_print(bio_err, rsa_key[i]->e);\n BIO_printf(bio_err, "\\n");\n }\n# endif\n }\n# endif\n# ifndef OPENSSL_NO_DSA\n dsa_key[0] = get_dsa512();\n dsa_key[1] = get_dsa1024();\n dsa_key[2] = get_dsa2048();\n# endif\n# ifndef OPENSSL_NO_DES\n DES_set_key_unchecked(&key, &sch);\n DES_set_key_unchecked(&key2, &sch2);\n DES_set_key_unchecked(&key3, &sch3);\n# endif\n# ifndef OPENSSL_NO_AES\n AES_set_encrypt_key(key16, 128, &aes_ks1);\n AES_set_encrypt_key(key24, 192, &aes_ks2);\n AES_set_encrypt_key(key32, 256, &aes_ks3);\n# endif\n# ifndef OPENSSL_NO_CAMELLIA\n Camellia_set_key(key16, 128, &camellia_ks1);\n Camellia_set_key(ckey24, 192, &camellia_ks2);\n Camellia_set_key(ckey32, 256, &camellia_ks3);\n# endif\n# ifndef OPENSSL_NO_IDEA\n idea_set_encrypt_key(key16, &idea_ks);\n# endif\n# ifndef OPENSSL_NO_SEED\n SEED_set_key(key16, &seed_ks);\n# endif\n# ifndef OPENSSL_NO_RC4\n RC4_set_key(&rc4_ks, 16, key16);\n# endif\n# ifndef OPENSSL_NO_RC2\n RC2_set_key(&rc2_ks, 16, key16, 128);\n# endif\n# ifndef OPENSSL_NO_RC5\n RC5_32_set_key(&rc5_ks, 16, key16, 12);\n# endif\n# ifndef OPENSSL_NO_BF\n BF_set_key(&bf_ks, 16, key16);\n# endif\n# ifndef OPENSSL_NO_CAST\n CAST_set_key(&cast_ks, 16, key16);\n# endif\n# ifndef OPENSSL_NO_RSA\n memset(rsa_c, 0, sizeof(rsa_c));\n# endif\n# ifndef SIGALRM\n# ifndef OPENSSL_NO_DES\n BIO_printf(bio_err, "First we calculate the approximate speed ...\\n");\n count = 10;\n do {\n long it;\n count *= 2;\n Time_F(START);\n for (it = count; it; it--)\n DES_ecb_encrypt((DES_cblock *)buf,\n (DES_cblock *)buf, &sch, DES_ENCRYPT);\n d = Time_F(STOP);\n } while (d < 3);\n save_count = count;\n c[D_MD2][0] = count / 10;\n c[D_MDC2][0] = count / 10;\n c[D_MD4][0] = count;\n c[D_MD5][0] = count;\n c[D_HMAC][0] = count;\n c[D_SHA1][0] = count;\n c[D_RMD160][0] = count;\n c[D_RC4][0] = count * 5;\n c[D_CBC_DES][0] = count;\n c[D_EDE3_DES][0] = count / 3;\n c[D_CBC_IDEA][0] = count;\n c[D_CBC_SEED][0] = count;\n c[D_CBC_RC2][0] = count;\n c[D_CBC_RC5][0] = count;\n c[D_CBC_BF][0] = count;\n c[D_CBC_CAST][0] = count;\n c[D_CBC_128_AES][0] = count;\n c[D_CBC_192_AES][0] = count;\n c[D_CBC_256_AES][0] = count;\n c[D_CBC_128_CML][0] = count;\n c[D_CBC_192_CML][0] = count;\n c[D_CBC_256_CML][0] = count;\n c[D_SHA256][0] = count;\n c[D_SHA512][0] = count;\n c[D_WHIRLPOOL][0] = count;\n c[D_IGE_128_AES][0] = count;\n c[D_IGE_192_AES][0] = count;\n c[D_IGE_256_AES][0] = count;\n c[D_GHASH][0] = count;\n for (i = 1; i < SIZE_NUM; i++) {\n long l0, l1;\n l0 = (long)lengths[0];\n l1 = (long)lengths[i];\n c[D_MD2][i] = c[D_MD2][0] * 4 * l0 / l1;\n c[D_MDC2][i] = c[D_MDC2][0] * 4 * l0 / l1;\n c[D_MD4][i] = c[D_MD4][0] * 4 * l0 / l1;\n c[D_MD5][i] = c[D_MD5][0] * 4 * l0 / l1;\n c[D_HMAC][i] = c[D_HMAC][0] * 4 * l0 / l1;\n c[D_SHA1][i] = c[D_SHA1][0] * 4 * l0 / l1;\n c[D_RMD160][i] = c[D_RMD160][0] * 4 * l0 / l1;\n c[D_SHA256][i] = c[D_SHA256][0] * 4 * l0 / l1;\n c[D_SHA512][i] = c[D_SHA512][0] * 4 * l0 / l1;\n c[D_WHIRLPOOL][i] = c[D_WHIRLPOOL][0] * 4 * l0 / l1;\n l0 = (long)lengths[i - 1];\n c[D_RC4][i] = c[D_RC4][i - 1] * l0 / l1;\n c[D_CBC_DES][i] = c[D_CBC_DES][i - 1] * l0 / l1;\n c[D_EDE3_DES][i] = c[D_EDE3_DES][i - 1] * l0 / l1;\n c[D_CBC_IDEA][i] = c[D_CBC_IDEA][i - 1] * l0 / l1;\n c[D_CBC_SEED][i] = c[D_CBC_SEED][i - 1] * l0 / l1;\n c[D_CBC_RC2][i] = c[D_CBC_RC2][i - 1] * l0 / l1;\n c[D_CBC_RC5][i] = c[D_CBC_RC5][i - 1] * l0 / l1;\n c[D_CBC_BF][i] = c[D_CBC_BF][i - 1] * l0 / l1;\n c[D_CBC_CAST][i] = c[D_CBC_CAST][i - 1] * l0 / l1;\n c[D_CBC_128_AES][i] = c[D_CBC_128_AES][i - 1] * l0 / l1;\n c[D_CBC_192_AES][i] = c[D_CBC_192_AES][i - 1] * l0 / l1;\n c[D_CBC_256_AES][i] = c[D_CBC_256_AES][i - 1] * l0 / l1;\n c[D_CBC_128_CML][i] = c[D_CBC_128_CML][i - 1] * l0 / l1;\n c[D_CBC_192_CML][i] = c[D_CBC_192_CML][i - 1] * l0 / l1;\n c[D_CBC_256_CML][i] = c[D_CBC_256_CML][i - 1] * l0 / l1;\n c[D_IGE_128_AES][i] = c[D_IGE_128_AES][i - 1] * l0 / l1;\n c[D_IGE_192_AES][i] = c[D_IGE_192_AES][i - 1] * l0 / l1;\n c[D_IGE_256_AES][i] = c[D_IGE_256_AES][i - 1] * l0 / l1;\n }\n# ifndef OPENSSL_NO_RSA\n rsa_c[R_RSA_512][0] = count / 2000;\n rsa_c[R_RSA_512][1] = count / 400;\n for (i = 1; i < RSA_NUM; i++) {\n rsa_c[i][0] = rsa_c[i - 1][0] / 8;\n rsa_c[i][1] = rsa_c[i - 1][1] / 4;\n if ((rsa_doit[i] <= 1) && (rsa_c[i][0] == 0))\n rsa_doit[i] = 0;\n else {\n if (rsa_c[i][0] == 0) {\n rsa_c[i][0] = 1;\n rsa_c[i][1] = 20;\n }\n }\n }\n# endif\n# ifndef OPENSSL_NO_DSA\n dsa_c[R_DSA_512][0] = count / 1000;\n dsa_c[R_DSA_512][1] = count / 1000 / 2;\n for (i = 1; i < DSA_NUM; i++) {\n dsa_c[i][0] = dsa_c[i - 1][0] / 4;\n dsa_c[i][1] = dsa_c[i - 1][1] / 4;\n if ((dsa_doit[i] <= 1) && (dsa_c[i][0] == 0))\n dsa_doit[i] = 0;\n else {\n if (dsa_c[i] == 0) {\n dsa_c[i][0] = 1;\n dsa_c[i][1] = 1;\n }\n }\n }\n# endif\n# ifndef OPENSSL_NO_ECDSA\n ecdsa_c[R_EC_P160][0] = count / 1000;\n ecdsa_c[R_EC_P160][1] = count / 1000 / 2;\n for (i = R_EC_P192; i <= R_EC_P521; i++) {\n ecdsa_c[i][0] = ecdsa_c[i - 1][0] / 2;\n ecdsa_c[i][1] = ecdsa_c[i - 1][1] / 2;\n if ((ecdsa_doit[i] <= 1) && (ecdsa_c[i][0] == 0))\n ecdsa_doit[i] = 0;\n else {\n if (ecdsa_c[i] == 0) {\n ecdsa_c[i][0] = 1;\n ecdsa_c[i][1] = 1;\n }\n }\n }\n ecdsa_c[R_EC_K163][0] = count / 1000;\n ecdsa_c[R_EC_K163][1] = count / 1000 / 2;\n for (i = R_EC_K233; i <= R_EC_K571; i++) {\n ecdsa_c[i][0] = ecdsa_c[i - 1][0] / 2;\n ecdsa_c[i][1] = ecdsa_c[i - 1][1] / 2;\n if ((ecdsa_doit[i] <= 1) && (ecdsa_c[i][0] == 0))\n ecdsa_doit[i] = 0;\n else {\n if (ecdsa_c[i] == 0) {\n ecdsa_c[i][0] = 1;\n ecdsa_c[i][1] = 1;\n }\n }\n }\n ecdsa_c[R_EC_B163][0] = count / 1000;\n ecdsa_c[R_EC_B163][1] = count / 1000 / 2;\n for (i = R_EC_B233; i <= R_EC_B571; i++) {\n ecdsa_c[i][0] = ecdsa_c[i - 1][0] / 2;\n ecdsa_c[i][1] = ecdsa_c[i - 1][1] / 2;\n if ((ecdsa_doit[i] <= 1) && (ecdsa_c[i][0] == 0))\n ecdsa_doit[i] = 0;\n else {\n if (ecdsa_c[i] == 0) {\n ecdsa_c[i][0] = 1;\n ecdsa_c[i][1] = 1;\n }\n }\n }\n# endif\n# ifndef OPENSSL_NO_ECDH\n ecdh_c[R_EC_P160][0] = count / 1000;\n ecdh_c[R_EC_P160][1] = count / 1000;\n for (i = R_EC_P192; i <= R_EC_P521; i++) {\n ecdh_c[i][0] = ecdh_c[i - 1][0] / 2;\n ecdh_c[i][1] = ecdh_c[i - 1][1] / 2;\n if ((ecdh_doit[i] <= 1) && (ecdh_c[i][0] == 0))\n ecdh_doit[i] = 0;\n else {\n if (ecdh_c[i] == 0) {\n ecdh_c[i][0] = 1;\n ecdh_c[i][1] = 1;\n }\n }\n }\n ecdh_c[R_EC_K163][0] = count / 1000;\n ecdh_c[R_EC_K163][1] = count / 1000;\n for (i = R_EC_K233; i <= R_EC_K571; i++) {\n ecdh_c[i][0] = ecdh_c[i - 1][0] / 2;\n ecdh_c[i][1] = ecdh_c[i - 1][1] / 2;\n if ((ecdh_doit[i] <= 1) && (ecdh_c[i][0] == 0))\n ecdh_doit[i] = 0;\n else {\n if (ecdh_c[i] == 0) {\n ecdh_c[i][0] = 1;\n ecdh_c[i][1] = 1;\n }\n }\n }\n ecdh_c[R_EC_B163][0] = count / 1000;\n ecdh_c[R_EC_B163][1] = count / 1000;\n for (i = R_EC_B233; i <= R_EC_B571; i++) {\n ecdh_c[i][0] = ecdh_c[i - 1][0] / 2;\n ecdh_c[i][1] = ecdh_c[i - 1][1] / 2;\n if ((ecdh_doit[i] <= 1) && (ecdh_c[i][0] == 0))\n ecdh_doit[i] = 0;\n else {\n if (ecdh_c[i] == 0) {\n ecdh_c[i][0] = 1;\n ecdh_c[i][1] = 1;\n }\n }\n }\n# endif\n# define COND(d) (count < (d))\n# define COUNT(d) (d)\n# else\n# error "You cannot disable DES on systems without SIGALRM."\n# endif\n# else\n# define COND(c) (run && count<0x7fffffff)\n# define COUNT(d) (count)\n# ifndef _WIN32\n signal(SIGALRM, sig_done);\n# endif\n# endif\n# ifndef OPENSSL_NO_MD2\n if (doit[D_MD2]) {\n for (j = 0; j < SIZE_NUM; j++) {\n print_message(names[D_MD2], c[D_MD2][j], lengths[j]);\n Time_F(START);\n for (count = 0, run = 1; COND(c[D_MD2][j]); count++)\n EVP_Digest(buf, (unsigned long)lengths[j], &(md2[0]), NULL,\n EVP_md2(), NULL);\n d = Time_F(STOP);\n print_result(D_MD2, j, count, d);\n }\n }\n# endif\n# ifndef OPENSSL_NO_MDC2\n if (doit[D_MDC2]) {\n for (j = 0; j < SIZE_NUM; j++) {\n print_message(names[D_MDC2], c[D_MDC2][j], lengths[j]);\n Time_F(START);\n for (count = 0, run = 1; COND(c[D_MDC2][j]); count++)\n EVP_Digest(buf, (unsigned long)lengths[j], &(mdc2[0]), NULL,\n EVP_mdc2(), NULL);\n d = Time_F(STOP);\n print_result(D_MDC2, j, count, d);\n }\n }\n# endif\n# ifndef OPENSSL_NO_MD4\n if (doit[D_MD4]) {\n for (j = 0; j < SIZE_NUM; j++) {\n print_message(names[D_MD4], c[D_MD4][j], lengths[j]);\n Time_F(START);\n for (count = 0, run = 1; COND(c[D_MD4][j]); count++)\n EVP_Digest(&(buf[0]), (unsigned long)lengths[j], &(md4[0]),\n NULL, EVP_md4(), NULL);\n d = Time_F(STOP);\n print_result(D_MD4, j, count, d);\n }\n }\n# endif\n# ifndef OPENSSL_NO_MD5\n if (doit[D_MD5]) {\n for (j = 0; j < SIZE_NUM; j++) {\n print_message(names[D_MD5], c[D_MD5][j], lengths[j]);\n Time_F(START);\n for (count = 0, run = 1; COND(c[D_MD5][j]); count++)\n MD5(buf, lengths[j], md5);\n d = Time_F(STOP);\n print_result(D_MD5, j, count, d);\n }\n }\n# endif\n# if !defined(OPENSSL_NO_MD5) && !defined(OPENSSL_NO_HMAC)\n if (doit[D_HMAC]) {\n HMAC_CTX hctx;\n HMAC_CTX_init(&hctx);\n HMAC_Init_ex(&hctx, (unsigned char *)"This is a key...",\n 16, EVP_md5(), NULL);\n for (j = 0; j < SIZE_NUM; j++) {\n print_message(names[D_HMAC], c[D_HMAC][j], lengths[j]);\n Time_F(START);\n for (count = 0, run = 1; COND(c[D_HMAC][j]); count++) {\n HMAC_Init_ex(&hctx, NULL, 0, NULL, NULL);\n HMAC_Update(&hctx, buf, lengths[j]);\n HMAC_Final(&hctx, &(hmac[0]), NULL);\n }\n d = Time_F(STOP);\n print_result(D_HMAC, j, count, d);\n }\n HMAC_CTX_cleanup(&hctx);\n }\n# endif\n# ifndef OPENSSL_NO_SHA\n if (doit[D_SHA1]) {\n for (j = 0; j < SIZE_NUM; j++) {\n print_message(names[D_SHA1], c[D_SHA1][j], lengths[j]);\n Time_F(START);\n for (count = 0, run = 1; COND(c[D_SHA1][j]); count++)\n# if 0\n EVP_Digest(buf, (unsigned long)lengths[j], &(sha[0]), NULL,\n EVP_sha1(), NULL);\n# else\n SHA1(buf, lengths[j], sha);\n# endif\n d = Time_F(STOP);\n print_result(D_SHA1, j, count, d);\n }\n }\n# ifndef OPENSSL_NO_SHA256\n if (doit[D_SHA256]) {\n for (j = 0; j < SIZE_NUM; j++) {\n print_message(names[D_SHA256], c[D_SHA256][j], lengths[j]);\n Time_F(START);\n for (count = 0, run = 1; COND(c[D_SHA256][j]); count++)\n SHA256(buf, lengths[j], sha256);\n d = Time_F(STOP);\n print_result(D_SHA256, j, count, d);\n }\n }\n# endif\n# ifndef OPENSSL_NO_SHA512\n if (doit[D_SHA512]) {\n for (j = 0; j < SIZE_NUM; j++) {\n print_message(names[D_SHA512], c[D_SHA512][j], lengths[j]);\n Time_F(START);\n for (count = 0, run = 1; COND(c[D_SHA512][j]); count++)\n SHA512(buf, lengths[j], sha512);\n d = Time_F(STOP);\n print_result(D_SHA512, j, count, d);\n }\n }\n# endif\n# endif\n# ifndef OPENSSL_NO_WHIRLPOOL\n if (doit[D_WHIRLPOOL]) {\n for (j = 0; j < SIZE_NUM; j++) {\n print_message(names[D_WHIRLPOOL], c[D_WHIRLPOOL][j], lengths[j]);\n Time_F(START);\n for (count = 0, run = 1; COND(c[D_WHIRLPOOL][j]); count++)\n WHIRLPOOL(buf, lengths[j], whirlpool);\n d = Time_F(STOP);\n print_result(D_WHIRLPOOL, j, count, d);\n }\n }\n# endif\n# ifndef OPENSSL_NO_RMD160\n if (doit[D_RMD160]) {\n for (j = 0; j < SIZE_NUM; j++) {\n print_message(names[D_RMD160], c[D_RMD160][j], lengths[j]);\n Time_F(START);\n for (count = 0, run = 1; COND(c[D_RMD160][j]); count++)\n EVP_Digest(buf, (unsigned long)lengths[j], &(rmd160[0]), NULL,\n EVP_ripemd160(), NULL);\n d = Time_F(STOP);\n print_result(D_RMD160, j, count, d);\n }\n }\n# endif\n# ifndef OPENSSL_NO_RC4\n if (doit[D_RC4]) {\n for (j = 0; j < SIZE_NUM; j++) {\n print_message(names[D_RC4], c[D_RC4][j], lengths[j]);\n Time_F(START);\n for (count = 0, run = 1; COND(c[D_RC4][j]); count++)\n RC4(&rc4_ks, (unsigned int)lengths[j], buf, buf);\n d = Time_F(STOP);\n print_result(D_RC4, j, count, d);\n }\n }\n# endif\n# ifndef OPENSSL_NO_DES\n if (doit[D_CBC_DES]) {\n for (j = 0; j < SIZE_NUM; j++) {\n print_message(names[D_CBC_DES], c[D_CBC_DES][j], lengths[j]);\n Time_F(START);\n for (count = 0, run = 1; COND(c[D_CBC_DES][j]); count++)\n DES_ncbc_encrypt(buf, buf, lengths[j], &sch,\n &DES_iv, DES_ENCRYPT);\n d = Time_F(STOP);\n print_result(D_CBC_DES, j, count, d);\n }\n }\n if (doit[D_EDE3_DES]) {\n for (j = 0; j < SIZE_NUM; j++) {\n print_message(names[D_EDE3_DES], c[D_EDE3_DES][j], lengths[j]);\n Time_F(START);\n for (count = 0, run = 1; COND(c[D_EDE3_DES][j]); count++)\n DES_ede3_cbc_encrypt(buf, buf, lengths[j],\n &sch, &sch2, &sch3,\n &DES_iv, DES_ENCRYPT);\n d = Time_F(STOP);\n print_result(D_EDE3_DES, j, count, d);\n }\n }\n# endif\n# ifndef OPENSSL_NO_AES\n if (doit[D_CBC_128_AES]) {\n for (j = 0; j < SIZE_NUM; j++) {\n print_message(names[D_CBC_128_AES], c[D_CBC_128_AES][j],\n lengths[j]);\n Time_F(START);\n for (count = 0, run = 1; COND(c[D_CBC_128_AES][j]); count++)\n AES_cbc_encrypt(buf, buf,\n (unsigned long)lengths[j], &aes_ks1,\n iv, AES_ENCRYPT);\n d = Time_F(STOP);\n print_result(D_CBC_128_AES, j, count, d);\n }\n }\n if (doit[D_CBC_192_AES]) {\n for (j = 0; j < SIZE_NUM; j++) {\n print_message(names[D_CBC_192_AES], c[D_CBC_192_AES][j],\n lengths[j]);\n Time_F(START);\n for (count = 0, run = 1; COND(c[D_CBC_192_AES][j]); count++)\n AES_cbc_encrypt(buf, buf,\n (unsigned long)lengths[j], &aes_ks2,\n iv, AES_ENCRYPT);\n d = Time_F(STOP);\n print_result(D_CBC_192_AES, j, count, d);\n }\n }\n if (doit[D_CBC_256_AES]) {\n for (j = 0; j < SIZE_NUM; j++) {\n print_message(names[D_CBC_256_AES], c[D_CBC_256_AES][j],\n lengths[j]);\n Time_F(START);\n for (count = 0, run = 1; COND(c[D_CBC_256_AES][j]); count++)\n AES_cbc_encrypt(buf, buf,\n (unsigned long)lengths[j], &aes_ks3,\n iv, AES_ENCRYPT);\n d = Time_F(STOP);\n print_result(D_CBC_256_AES, j, count, d);\n }\n }\n if (doit[D_IGE_128_AES]) {\n for (j = 0; j < SIZE_NUM; j++) {\n print_message(names[D_IGE_128_AES], c[D_IGE_128_AES][j],\n lengths[j]);\n Time_F(START);\n for (count = 0, run = 1; COND(c[D_IGE_128_AES][j]); count++)\n AES_ige_encrypt(buf, buf2,\n (unsigned long)lengths[j], &aes_ks1,\n iv, AES_ENCRYPT);\n d = Time_F(STOP);\n print_result(D_IGE_128_AES, j, count, d);\n }\n }\n if (doit[D_IGE_192_AES]) {\n for (j = 0; j < SIZE_NUM; j++) {\n print_message(names[D_IGE_192_AES], c[D_IGE_192_AES][j],\n lengths[j]);\n Time_F(START);\n for (count = 0, run = 1; COND(c[D_IGE_192_AES][j]); count++)\n AES_ige_encrypt(buf, buf2,\n (unsigned long)lengths[j], &aes_ks2,\n iv, AES_ENCRYPT);\n d = Time_F(STOP);\n print_result(D_IGE_192_AES, j, count, d);\n }\n }\n if (doit[D_IGE_256_AES]) {\n for (j = 0; j < SIZE_NUM; j++) {\n print_message(names[D_IGE_256_AES], c[D_IGE_256_AES][j],\n lengths[j]);\n Time_F(START);\n for (count = 0, run = 1; COND(c[D_IGE_256_AES][j]); count++)\n AES_ige_encrypt(buf, buf2,\n (unsigned long)lengths[j], &aes_ks3,\n iv, AES_ENCRYPT);\n d = Time_F(STOP);\n print_result(D_IGE_256_AES, j, count, d);\n }\n }\n if (doit[D_GHASH]) {\n GCM128_CONTEXT *ctx =\n CRYPTO_gcm128_new(&aes_ks1, (block128_f) AES_encrypt);\n CRYPTO_gcm128_setiv(ctx, (unsigned char *)"0123456789ab", 12);\n for (j = 0; j < SIZE_NUM; j++) {\n print_message(names[D_GHASH], c[D_GHASH][j], lengths[j]);\n Time_F(START);\n for (count = 0, run = 1; COND(c[D_GHASH][j]); count++)\n CRYPTO_gcm128_aad(ctx, buf, lengths[j]);\n d = Time_F(STOP);\n print_result(D_GHASH, j, count, d);\n }\n CRYPTO_gcm128_release(ctx);\n }\n# endif\n# ifndef OPENSSL_NO_CAMELLIA\n if (doit[D_CBC_128_CML]) {\n for (j = 0; j < SIZE_NUM; j++) {\n print_message(names[D_CBC_128_CML], c[D_CBC_128_CML][j],\n lengths[j]);\n Time_F(START);\n for (count = 0, run = 1; COND(c[D_CBC_128_CML][j]); count++)\n Camellia_cbc_encrypt(buf, buf,\n (unsigned long)lengths[j], &camellia_ks1,\n iv, CAMELLIA_ENCRYPT);\n d = Time_F(STOP);\n print_result(D_CBC_128_CML, j, count, d);\n }\n }\n if (doit[D_CBC_192_CML]) {\n for (j = 0; j < SIZE_NUM; j++) {\n print_message(names[D_CBC_192_CML], c[D_CBC_192_CML][j],\n lengths[j]);\n Time_F(START);\n for (count = 0, run = 1; COND(c[D_CBC_192_CML][j]); count++)\n Camellia_cbc_encrypt(buf, buf,\n (unsigned long)lengths[j], &camellia_ks2,\n iv, CAMELLIA_ENCRYPT);\n d = Time_F(STOP);\n print_result(D_CBC_192_CML, j, count, d);\n }\n }\n if (doit[D_CBC_256_CML]) {\n for (j = 0; j < SIZE_NUM; j++) {\n print_message(names[D_CBC_256_CML], c[D_CBC_256_CML][j],\n lengths[j]);\n Time_F(START);\n for (count = 0, run = 1; COND(c[D_CBC_256_CML][j]); count++)\n Camellia_cbc_encrypt(buf, buf,\n (unsigned long)lengths[j], &camellia_ks3,\n iv, CAMELLIA_ENCRYPT);\n d = Time_F(STOP);\n print_result(D_CBC_256_CML, j, count, d);\n }\n }\n# endif\n# ifndef OPENSSL_NO_IDEA\n if (doit[D_CBC_IDEA]) {\n for (j = 0; j < SIZE_NUM; j++) {\n print_message(names[D_CBC_IDEA], c[D_CBC_IDEA][j], lengths[j]);\n Time_F(START);\n for (count = 0, run = 1; COND(c[D_CBC_IDEA][j]); count++)\n idea_cbc_encrypt(buf, buf,\n (unsigned long)lengths[j], &idea_ks,\n iv, IDEA_ENCRYPT);\n d = Time_F(STOP);\n print_result(D_CBC_IDEA, j, count, d);\n }\n }\n# endif\n# ifndef OPENSSL_NO_SEED\n if (doit[D_CBC_SEED]) {\n for (j = 0; j < SIZE_NUM; j++) {\n print_message(names[D_CBC_SEED], c[D_CBC_SEED][j], lengths[j]);\n Time_F(START);\n for (count = 0, run = 1; COND(c[D_CBC_SEED][j]); count++)\n SEED_cbc_encrypt(buf, buf,\n (unsigned long)lengths[j], &seed_ks, iv, 1);\n d = Time_F(STOP);\n print_result(D_CBC_SEED, j, count, d);\n }\n }\n# endif\n# ifndef OPENSSL_NO_RC2\n if (doit[D_CBC_RC2]) {\n for (j = 0; j < SIZE_NUM; j++) {\n print_message(names[D_CBC_RC2], c[D_CBC_RC2][j], lengths[j]);\n Time_F(START);\n for (count = 0, run = 1; COND(c[D_CBC_RC2][j]); count++)\n RC2_cbc_encrypt(buf, buf,\n (unsigned long)lengths[j], &rc2_ks,\n iv, RC2_ENCRYPT);\n d = Time_F(STOP);\n print_result(D_CBC_RC2, j, count, d);\n }\n }\n# endif\n# ifndef OPENSSL_NO_RC5\n if (doit[D_CBC_RC5]) {\n for (j = 0; j < SIZE_NUM; j++) {\n print_message(names[D_CBC_RC5], c[D_CBC_RC5][j], lengths[j]);\n Time_F(START);\n for (count = 0, run = 1; COND(c[D_CBC_RC5][j]); count++)\n RC5_32_cbc_encrypt(buf, buf,\n (unsigned long)lengths[j], &rc5_ks,\n iv, RC5_ENCRYPT);\n d = Time_F(STOP);\n print_result(D_CBC_RC5, j, count, d);\n }\n }\n# endif\n# ifndef OPENSSL_NO_BF\n if (doit[D_CBC_BF]) {\n for (j = 0; j < SIZE_NUM; j++) {\n print_message(names[D_CBC_BF], c[D_CBC_BF][j], lengths[j]);\n Time_F(START);\n for (count = 0, run = 1; COND(c[D_CBC_BF][j]); count++)\n BF_cbc_encrypt(buf, buf,\n (unsigned long)lengths[j], &bf_ks,\n iv, BF_ENCRYPT);\n d = Time_F(STOP);\n print_result(D_CBC_BF, j, count, d);\n }\n }\n# endif\n# ifndef OPENSSL_NO_CAST\n if (doit[D_CBC_CAST]) {\n for (j = 0; j < SIZE_NUM; j++) {\n print_message(names[D_CBC_CAST], c[D_CBC_CAST][j], lengths[j]);\n Time_F(START);\n for (count = 0, run = 1; COND(c[D_CBC_CAST][j]); count++)\n CAST_cbc_encrypt(buf, buf,\n (unsigned long)lengths[j], &cast_ks,\n iv, CAST_ENCRYPT);\n d = Time_F(STOP);\n print_result(D_CBC_CAST, j, count, d);\n }\n }\n# endif\n if (doit[D_EVP]) {\n# ifdef EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK\n if (multiblock && evp_cipher) {\n if (!\n (EVP_CIPHER_flags(evp_cipher) &\n EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK)) {\n fprintf(stderr, "%s is not multi-block capable\\n",\n OBJ_nid2ln(evp_cipher->nid));\n goto end;\n }\n multiblock_speed(evp_cipher);\n mret = 0;\n goto end;\n }\n# endif\n for (j = 0; j < SIZE_NUM; j++) {\n if (evp_cipher) {\n EVP_CIPHER_CTX ctx;\n int outl;\n names[D_EVP] = OBJ_nid2ln(evp_cipher->nid);\n print_message(names[D_EVP], save_count, lengths[j]);\n EVP_CIPHER_CTX_init(&ctx);\n if (decrypt)\n EVP_DecryptInit_ex(&ctx, evp_cipher, NULL, key16, iv);\n else\n EVP_EncryptInit_ex(&ctx, evp_cipher, NULL, key16, iv);\n EVP_CIPHER_CTX_set_padding(&ctx, 0);\n Time_F(START);\n if (decrypt)\n for (count = 0, run = 1;\n COND(save_count * 4 * lengths[0] / lengths[j]);\n count++)\n EVP_DecryptUpdate(&ctx, buf, &outl, buf, lengths[j]);\n else\n for (count = 0, run = 1;\n COND(save_count * 4 * lengths[0] / lengths[j]);\n count++)\n EVP_EncryptUpdate(&ctx, buf, &outl, buf, lengths[j]);\n if (decrypt)\n EVP_DecryptFinal_ex(&ctx, buf, &outl);\n else\n EVP_EncryptFinal_ex(&ctx, buf, &outl);\n d = Time_F(STOP);\n EVP_CIPHER_CTX_cleanup(&ctx);\n }\n if (evp_md) {\n names[D_EVP] = OBJ_nid2ln(evp_md->type);\n print_message(names[D_EVP], save_count, lengths[j]);\n Time_F(START);\n for (count = 0, run = 1;\n COND(save_count * 4 * lengths[0] / lengths[j]); count++)\n EVP_Digest(buf, lengths[j], &(md[0]), NULL, evp_md, NULL);\n d = Time_F(STOP);\n }\n print_result(D_EVP, j, count, d);\n }\n }\n# ifndef OPENSSL_SYS_WIN32\n# endif\n RAND_pseudo_bytes(buf, 36);\n# ifndef OPENSSL_NO_RSA\n for (j = 0; j < RSA_NUM; j++) {\n int ret;\n if (!rsa_doit[j])\n continue;\n ret = RSA_sign(NID_md5_sha1, buf, 36, buf2, &rsa_num, rsa_key[j]);\n if (ret == 0) {\n BIO_printf(bio_err,\n "RSA sign failure. No RSA sign will be done.\\n");\n ERR_print_errors(bio_err);\n rsa_count = 1;\n } else {\n pkey_print_message("private", "rsa",\n rsa_c[j][0], rsa_bits[j], RSA_SECONDS);\n Time_F(START);\n for (count = 0, run = 1; COND(rsa_c[j][0]); count++) {\n ret = RSA_sign(NID_md5_sha1, buf, 36, buf2,\n &rsa_num, rsa_key[j]);\n if (ret == 0) {\n BIO_printf(bio_err, "RSA sign failure\\n");\n ERR_print_errors(bio_err);\n count = 1;\n break;\n }\n }\n d = Time_F(STOP);\n BIO_printf(bio_err,\n mr ? "+R1:%ld:%d:%.2f\\n"\n : "%ld %d bit private RSA\'s in %.2fs\\n",\n count, rsa_bits[j], d);\n rsa_results[j][0] = d / (double)count;\n rsa_count = count;\n }\n# if 1\n ret = RSA_verify(NID_md5_sha1, buf, 36, buf2, rsa_num, rsa_key[j]);\n if (ret <= 0) {\n BIO_printf(bio_err,\n "RSA verify failure. No RSA verify will be done.\\n");\n ERR_print_errors(bio_err);\n rsa_doit[j] = 0;\n } else {\n pkey_print_message("public", "rsa",\n rsa_c[j][1], rsa_bits[j], RSA_SECONDS);\n Time_F(START);\n for (count = 0, run = 1; COND(rsa_c[j][1]); count++) {\n ret = RSA_verify(NID_md5_sha1, buf, 36, buf2,\n rsa_num, rsa_key[j]);\n if (ret <= 0) {\n BIO_printf(bio_err, "RSA verify failure\\n");\n ERR_print_errors(bio_err);\n count = 1;\n break;\n }\n }\n d = Time_F(STOP);\n BIO_printf(bio_err,\n mr ? "+R2:%ld:%d:%.2f\\n"\n : "%ld %d bit public RSA\'s in %.2fs\\n",\n count, rsa_bits[j], d);\n rsa_results[j][1] = d / (double)count;\n }\n# endif\n if (rsa_count <= 1) {\n for (j++; j < RSA_NUM; j++)\n rsa_doit[j] = 0;\n }\n }\n# endif\n RAND_pseudo_bytes(buf, 20);\n# ifndef OPENSSL_NO_DSA\n if (RAND_status() != 1) {\n RAND_seed(rnd_seed, sizeof rnd_seed);\n rnd_fake = 1;\n }\n for (j = 0; j < DSA_NUM; j++) {\n unsigned int kk;\n int ret;\n if (!dsa_doit[j])\n continue;\n ret = DSA_sign(EVP_PKEY_DSA, buf, 20, buf2, &kk, dsa_key[j]);\n if (ret == 0) {\n BIO_printf(bio_err,\n "DSA sign failure. No DSA sign will be done.\\n");\n ERR_print_errors(bio_err);\n rsa_count = 1;\n } else {\n pkey_print_message("sign", "dsa",\n dsa_c[j][0], dsa_bits[j], DSA_SECONDS);\n Time_F(START);\n for (count = 0, run = 1; COND(dsa_c[j][0]); count++) {\n ret = DSA_sign(EVP_PKEY_DSA, buf, 20, buf2, &kk, dsa_key[j]);\n if (ret == 0) {\n BIO_printf(bio_err, "DSA sign failure\\n");\n ERR_print_errors(bio_err);\n count = 1;\n break;\n }\n }\n d = Time_F(STOP);\n BIO_printf(bio_err,\n mr ? "+R3:%ld:%d:%.2f\\n"\n : "%ld %d bit DSA signs in %.2fs\\n",\n count, dsa_bits[j], d);\n dsa_results[j][0] = d / (double)count;\n rsa_count = count;\n }\n ret = DSA_verify(EVP_PKEY_DSA, buf, 20, buf2, kk, dsa_key[j]);\n if (ret <= 0) {\n BIO_printf(bio_err,\n "DSA verify failure. No DSA verify will be done.\\n");\n ERR_print_errors(bio_err);\n dsa_doit[j] = 0;\n } else {\n pkey_print_message("verify", "dsa",\n dsa_c[j][1], dsa_bits[j], DSA_SECONDS);\n Time_F(START);\n for (count = 0, run = 1; COND(dsa_c[j][1]); count++) {\n ret = DSA_verify(EVP_PKEY_DSA, buf, 20, buf2, kk, dsa_key[j]);\n if (ret <= 0) {\n BIO_printf(bio_err, "DSA verify failure\\n");\n ERR_print_errors(bio_err);\n count = 1;\n break;\n }\n }\n d = Time_F(STOP);\n BIO_printf(bio_err,\n mr ? "+R4:%ld:%d:%.2f\\n"\n : "%ld %d bit DSA verify in %.2fs\\n",\n count, dsa_bits[j], d);\n dsa_results[j][1] = d / (double)count;\n }\n if (rsa_count <= 1) {\n for (j++; j < DSA_NUM; j++)\n dsa_doit[j] = 0;\n }\n }\n if (rnd_fake)\n RAND_cleanup();\n# endif\n# ifndef OPENSSL_NO_ECDSA\n if (RAND_status() != 1) {\n RAND_seed(rnd_seed, sizeof rnd_seed);\n rnd_fake = 1;\n }\n for (j = 0; j < EC_NUM; j++) {\n int ret;\n if (!ecdsa_doit[j])\n continue;\n ecdsa[j] = EC_KEY_new_by_curve_name(test_curves[j]);\n if (ecdsa[j] == NULL) {\n BIO_printf(bio_err, "ECDSA failure.\\n");\n ERR_print_errors(bio_err);\n rsa_count = 1;\n } else {\n# if 1\n EC_KEY_precompute_mult(ecdsa[j], NULL);\n# endif\n EC_KEY_generate_key(ecdsa[j]);\n ret = ECDSA_sign(0, buf, 20, ecdsasig, &ecdsasiglen, ecdsa[j]);\n if (ret == 0) {\n BIO_printf(bio_err,\n "ECDSA sign failure. No ECDSA sign will be done.\\n");\n ERR_print_errors(bio_err);\n rsa_count = 1;\n } else {\n pkey_print_message("sign", "ecdsa",\n ecdsa_c[j][0],\n test_curves_bits[j], ECDSA_SECONDS);\n Time_F(START);\n for (count = 0, run = 1; COND(ecdsa_c[j][0]); count++) {\n ret = ECDSA_sign(0, buf, 20,\n ecdsasig, &ecdsasiglen, ecdsa[j]);\n if (ret == 0) {\n BIO_printf(bio_err, "ECDSA sign failure\\n");\n ERR_print_errors(bio_err);\n count = 1;\n break;\n }\n }\n d = Time_F(STOP);\n BIO_printf(bio_err,\n mr ? "+R5:%ld:%d:%.2f\\n" :\n "%ld %d bit ECDSA signs in %.2fs \\n",\n count, test_curves_bits[j], d);\n ecdsa_results[j][0] = d / (double)count;\n rsa_count = count;\n }\n ret = ECDSA_verify(0, buf, 20, ecdsasig, ecdsasiglen, ecdsa[j]);\n if (ret != 1) {\n BIO_printf(bio_err,\n "ECDSA verify failure. No ECDSA verify will be done.\\n");\n ERR_print_errors(bio_err);\n ecdsa_doit[j] = 0;\n } else {\n pkey_print_message("verify", "ecdsa",\n ecdsa_c[j][1],\n test_curves_bits[j], ECDSA_SECONDS);\n Time_F(START);\n for (count = 0, run = 1; COND(ecdsa_c[j][1]); count++) {\n ret =\n ECDSA_verify(0, buf, 20, ecdsasig, ecdsasiglen,\n ecdsa[j]);\n if (ret != 1) {\n BIO_printf(bio_err, "ECDSA verify failure\\n");\n ERR_print_errors(bio_err);\n count = 1;\n break;\n }\n }\n d = Time_F(STOP);\n BIO_printf(bio_err,\n mr ? "+R6:%ld:%d:%.2f\\n"\n : "%ld %d bit ECDSA verify in %.2fs\\n",\n count, test_curves_bits[j], d);\n ecdsa_results[j][1] = d / (double)count;\n }\n if (rsa_count <= 1) {\n for (j++; j < EC_NUM; j++)\n ecdsa_doit[j] = 0;\n }\n }\n }\n if (rnd_fake)\n RAND_cleanup();\n# endif\n# ifndef OPENSSL_NO_ECDH\n if (RAND_status() != 1) {\n RAND_seed(rnd_seed, sizeof rnd_seed);\n rnd_fake = 1;\n }\n for (j = 0; j < EC_NUM; j++) {\n if (!ecdh_doit[j])\n continue;\n ecdh_a[j] = EC_KEY_new_by_curve_name(test_curves[j]);\n ecdh_b[j] = EC_KEY_new_by_curve_name(test_curves[j]);\n if ((ecdh_a[j] == NULL) || (ecdh_b[j] == NULL)) {\n BIO_printf(bio_err, "ECDH failure.\\n");\n ERR_print_errors(bio_err);\n rsa_count = 1;\n } else {\n if (!EC_KEY_generate_key(ecdh_a[j]) ||\n !EC_KEY_generate_key(ecdh_b[j])) {\n BIO_printf(bio_err, "ECDH key generation failure.\\n");\n ERR_print_errors(bio_err);\n rsa_count = 1;\n } else {\n int field_size, outlen;\n void *(*kdf) (const void *in, size_t inlen, void *out,\n size_t *xoutlen);\n field_size =\n EC_GROUP_get_degree(EC_KEY_get0_group(ecdh_a[j]));\n if (field_size <= 24 * 8) {\n outlen = KDF1_SHA1_len;\n kdf = KDF1_SHA1;\n } else {\n outlen = (field_size + 7) / 8;\n kdf = NULL;\n }\n secret_size_a =\n ECDH_compute_key(secret_a, outlen,\n EC_KEY_get0_public_key(ecdh_b[j]),\n ecdh_a[j], kdf);\n secret_size_b =\n ECDH_compute_key(secret_b, outlen,\n EC_KEY_get0_public_key(ecdh_a[j]),\n ecdh_b[j], kdf);\n if (secret_size_a != secret_size_b)\n ecdh_checks = 0;\n else\n ecdh_checks = 1;\n for (secret_idx = 0; (secret_idx < secret_size_a)\n && (ecdh_checks == 1); secret_idx++) {\n if (secret_a[secret_idx] != secret_b[secret_idx])\n ecdh_checks = 0;\n }\n if (ecdh_checks == 0) {\n BIO_printf(bio_err, "ECDH computations don\'t match.\\n");\n ERR_print_errors(bio_err);\n rsa_count = 1;\n }\n pkey_print_message("", "ecdh",\n ecdh_c[j][0],\n test_curves_bits[j], ECDH_SECONDS);\n Time_F(START);\n for (count = 0, run = 1; COND(ecdh_c[j][0]); count++) {\n ECDH_compute_key(secret_a, outlen,\n EC_KEY_get0_public_key(ecdh_b[j]),\n ecdh_a[j], kdf);\n }\n d = Time_F(STOP);\n BIO_printf(bio_err,\n mr ? "+R7:%ld:%d:%.2f\\n" :\n "%ld %d-bit ECDH ops in %.2fs\\n", count,\n test_curves_bits[j], d);\n ecdh_results[j][0] = d / (double)count;\n rsa_count = count;\n }\n }\n if (rsa_count <= 1) {\n for (j++; j < EC_NUM; j++)\n ecdh_doit[j] = 0;\n }\n }\n if (rnd_fake)\n RAND_cleanup();\n# endif\n# ifndef NO_FORK\n show_res:\n# endif\n if (!mr) {\n fprintf(stdout, "%s\\n", SSLeay_version(SSLEAY_VERSION));\n fprintf(stdout, "%s\\n", SSLeay_version(SSLEAY_BUILT_ON));\n printf("options:");\n printf("%s ", BN_options());\n# ifndef OPENSSL_NO_MD2\n printf("%s ", MD2_options());\n# endif\n# ifndef OPENSSL_NO_RC4\n printf("%s ", RC4_options());\n# endif\n# ifndef OPENSSL_NO_DES\n printf("%s ", DES_options());\n# endif\n# ifndef OPENSSL_NO_AES\n printf("%s ", AES_options());\n# endif\n# ifndef OPENSSL_NO_IDEA\n printf("%s ", idea_options());\n# endif\n# ifndef OPENSSL_NO_BF\n printf("%s ", BF_options());\n# endif\n fprintf(stdout, "\\n%s\\n", SSLeay_version(SSLEAY_CFLAGS));\n }\n if (pr_header) {\n if (mr)\n fprintf(stdout, "+H");\n else {\n fprintf(stdout,\n "The \'numbers\' are in 1000s of bytes per second processed.\\n");\n fprintf(stdout, "type ");\n }\n for (j = 0; j < SIZE_NUM; j++)\n fprintf(stdout, mr ? ":%d" : "%7d bytes", lengths[j]);\n fprintf(stdout, "\\n");\n }\n for (k = 0; k < ALGOR_NUM; k++) {\n if (!doit[k])\n continue;\n if (mr)\n fprintf(stdout, "+F:%d:%s", k, names[k]);\n else\n fprintf(stdout, "%-13s", names[k]);\n for (j = 0; j < SIZE_NUM; j++) {\n if (results[k][j] > 10000 && !mr)\n fprintf(stdout, " %11.2fk", results[k][j] / 1e3);\n else\n fprintf(stdout, mr ? ":%.2f" : " %11.2f ", results[k][j]);\n }\n fprintf(stdout, "\\n");\n }\n# ifndef OPENSSL_NO_RSA\n j = 1;\n for (k = 0; k < RSA_NUM; k++) {\n if (!rsa_doit[k])\n continue;\n if (j && !mr) {\n printf("%18ssign verify sign/s verify/s\\n", " ");\n j = 0;\n }\n if (mr)\n fprintf(stdout, "+F2:%u:%u:%f:%f\\n",\n k, rsa_bits[k], rsa_results[k][0], rsa_results[k][1]);\n else\n fprintf(stdout, "rsa %4u bits %8.6fs %8.6fs %8.1f %8.1f\\n",\n rsa_bits[k], rsa_results[k][0], rsa_results[k][1],\n 1.0 / rsa_results[k][0], 1.0 / rsa_results[k][1]);\n }\n# endif\n# ifndef OPENSSL_NO_DSA\n j = 1;\n for (k = 0; k < DSA_NUM; k++) {\n if (!dsa_doit[k])\n continue;\n if (j && !mr) {\n printf("%18ssign verify sign/s verify/s\\n", " ");\n j = 0;\n }\n if (mr)\n fprintf(stdout, "+F3:%u:%u:%f:%f\\n",\n k, dsa_bits[k], dsa_results[k][0], dsa_results[k][1]);\n else\n fprintf(stdout, "dsa %4u bits %8.6fs %8.6fs %8.1f %8.1f\\n",\n dsa_bits[k], dsa_results[k][0], dsa_results[k][1],\n 1.0 / dsa_results[k][0], 1.0 / dsa_results[k][1]);\n }\n# endif\n# ifndef OPENSSL_NO_ECDSA\n j = 1;\n for (k = 0; k < EC_NUM; k++) {\n if (!ecdsa_doit[k])\n continue;\n if (j && !mr) {\n printf("%30ssign verify sign/s verify/s\\n", " ");\n j = 0;\n }\n if (mr)\n fprintf(stdout, "+F4:%u:%u:%f:%f\\n",\n k, test_curves_bits[k],\n ecdsa_results[k][0], ecdsa_results[k][1]);\n else\n fprintf(stdout,\n "%4u bit ecdsa (%s) %8.4fs %8.4fs %8.1f %8.1f\\n",\n test_curves_bits[k],\n test_curves_names[k],\n ecdsa_results[k][0], ecdsa_results[k][1],\n 1.0 / ecdsa_results[k][0], 1.0 / ecdsa_results[k][1]);\n }\n# endif\n# ifndef OPENSSL_NO_ECDH\n j = 1;\n for (k = 0; k < EC_NUM; k++) {\n if (!ecdh_doit[k])\n continue;\n if (j && !mr) {\n printf("%30sop op/s\\n", " ");\n j = 0;\n }\n if (mr)\n fprintf(stdout, "+F5:%u:%u:%f:%f\\n",\n k, test_curves_bits[k],\n ecdh_results[k][0], 1.0 / ecdh_results[k][0]);\n else\n fprintf(stdout, "%4u bit ecdh (%s) %8.4fs %8.1f\\n",\n test_curves_bits[k],\n test_curves_names[k],\n ecdh_results[k][0], 1.0 / ecdh_results[k][0]);\n }\n# endif\n mret = 0;\n end:\n ERR_print_errors(bio_err);\n if (buf_malloc != NULL)\n OPENSSL_free(buf_malloc);\n if (buf2_malloc != NULL)\n OPENSSL_free(buf2_malloc);\n# ifndef OPENSSL_NO_RSA\n for (i = 0; i < RSA_NUM; i++)\n if (rsa_key[i] != NULL)\n RSA_free(rsa_key[i]);\n# endif\n# ifndef OPENSSL_NO_DSA\n for (i = 0; i < DSA_NUM; i++)\n if (dsa_key[i] != NULL)\n DSA_free(dsa_key[i]);\n# endif\n# ifndef OPENSSL_NO_ECDSA\n for (i = 0; i < EC_NUM; i++)\n if (ecdsa[i] != NULL)\n EC_KEY_free(ecdsa[i]);\n# endif\n# ifndef OPENSSL_NO_ECDH\n for (i = 0; i < EC_NUM; i++) {\n if (ecdh_a[i] != NULL)\n EC_KEY_free(ecdh_a[i]);\n if (ecdh_b[i] != NULL)\n EC_KEY_free(ecdh_b[i]);\n }\n# endif\n apps_shutdown();\n OPENSSL_EXIT(mret);\n}']
894
0
https://github.com/openssl/openssl/blob/305b68f1a2b6d4d0aa07a6ab47ac372f067a40bb/crypto/bn/bn_lib.c/#L290
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; }
['static EC_KEY *eckey_type2param(int ptype, const void *pval)\n{\n EC_KEY *eckey = NULL;\n if (ptype == V_ASN1_SEQUENCE) {\n const ASN1_STRING *pstr = pval;\n const unsigned char *pm = NULL;\n int pmlen;\n pm = pstr->data;\n pmlen = pstr->length;\n if ((eckey = d2i_ECParameters(NULL, &pm, pmlen)) == NULL) {\n ECerr(EC_F_ECKEY_TYPE2PARAM, EC_R_DECODE_ERROR);\n goto ecerr;\n }\n } else if (ptype == V_ASN1_OBJECT) {\n const ASN1_OBJECT *poid = pval;\n EC_GROUP *group;\n if ((eckey = EC_KEY_new()) == NULL) {\n ECerr(EC_F_ECKEY_TYPE2PARAM, ERR_R_MALLOC_FAILURE);\n goto ecerr;\n }\n group = EC_GROUP_new_by_curve_name(OBJ_obj2nid(poid));\n if (group == NULL)\n goto ecerr;\n EC_GROUP_set_asn1_flag(group, OPENSSL_EC_NAMED_CURVE);\n if (EC_KEY_set_group(eckey, group) == 0)\n goto ecerr;\n EC_GROUP_free(group);\n } else {\n ECerr(EC_F_ECKEY_TYPE2PARAM, EC_R_DECODE_ERROR);\n goto ecerr;\n }\n return eckey;\n ecerr:\n EC_KEY_free(eckey);\n return NULL;\n}', 'int EC_KEY_set_group(EC_KEY *key, const EC_GROUP *group)\n{\n if (key->meth->set_group != NULL && key->meth->set_group(key, group) == 0)\n return 0;\n EC_GROUP_free(key->group);\n key->group = EC_GROUP_dup(group);\n return (key->group == NULL) ? 0 : 1;\n}', 'EC_GROUP *EC_GROUP_dup(const EC_GROUP *a)\n{\n EC_GROUP *t = NULL;\n int ok = 0;\n if (a == NULL)\n return NULL;\n if ((t = EC_GROUP_new(a->meth)) == NULL)\n return NULL;\n if (!EC_GROUP_copy(t, a))\n goto err;\n ok = 1;\n err:\n if (!ok) {\n EC_GROUP_free(t);\n return NULL;\n }\n return t;\n}', 'int EC_GROUP_copy(EC_GROUP *dest, const EC_GROUP *src)\n{\n if (dest->meth->group_copy == 0) {\n ECerr(EC_F_EC_GROUP_COPY, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);\n return 0;\n }\n if (dest->meth != src->meth) {\n ECerr(EC_F_EC_GROUP_COPY, EC_R_INCOMPATIBLE_OBJECTS);\n return 0;\n }\n if (dest == src)\n return 1;\n dest->curve_name = src->curve_name;\n dest->pre_comp_type = src->pre_comp_type;\n switch (src->pre_comp_type) {\n case PCT_none:\n dest->pre_comp.ec = NULL;\n break;\n case PCT_nistz256:\n#ifdef ECP_NISTZ256_ASM\n dest->pre_comp.nistz256 = EC_nistz256_pre_comp_dup(src->pre_comp.nistz256);\n#endif\n break;\n#ifndef OPENSSL_NO_EC_NISTP_64_GCC_128\n case PCT_nistp224:\n dest->pre_comp.nistp224 = EC_nistp224_pre_comp_dup(src->pre_comp.nistp224);\n break;\n case PCT_nistp256:\n dest->pre_comp.nistp256 = EC_nistp256_pre_comp_dup(src->pre_comp.nistp256);\n break;\n case PCT_nistp521:\n dest->pre_comp.nistp521 = EC_nistp521_pre_comp_dup(src->pre_comp.nistp521);\n break;\n#else\n case PCT_nistp224:\n case PCT_nistp256:\n case PCT_nistp521:\n break;\n#endif\n case PCT_ec:\n dest->pre_comp.ec = EC_ec_pre_comp_dup(src->pre_comp.ec);\n break;\n }\n if (src->mont_data != NULL) {\n if (dest->mont_data == NULL) {\n dest->mont_data = BN_MONT_CTX_new();\n if (dest->mont_data == NULL)\n return 0;\n }\n if (!BN_MONT_CTX_copy(dest->mont_data, src->mont_data))\n return 0;\n } else {\n BN_MONT_CTX_free(dest->mont_data);\n dest->mont_data = NULL;\n }\n if (src->generator != NULL) {\n if (dest->generator == NULL) {\n dest->generator = EC_POINT_new(dest);\n if (dest->generator == NULL)\n return 0;\n }\n if (!EC_POINT_copy(dest->generator, src->generator))\n return 0;\n } else {\n EC_POINT_clear_free(dest->generator);\n dest->generator = NULL;\n }\n if ((src->meth->flags & EC_FLAGS_CUSTOM_CURVE) == 0) {\n if (!BN_copy(dest->order, src->order))\n return 0;\n if (!BN_copy(dest->cofactor, src->cofactor))\n return 0;\n }\n dest->asn1_flag = src->asn1_flag;\n dest->asn1_form = src->asn1_form;\n if (src->seed) {\n OPENSSL_free(dest->seed);\n if ((dest->seed = OPENSSL_malloc(src->seed_len)) == NULL) {\n ECerr(EC_F_EC_GROUP_COPY, ERR_R_MALLOC_FAILURE);\n return 0;\n }\n if (!memcpy(dest->seed, src->seed, src->seed_len))\n return 0;\n dest->seed_len = src->seed_len;\n } else {\n OPENSSL_free(dest->seed);\n dest->seed = NULL;\n dest->seed_len = 0;\n }\n return dest->meth->group_copy(dest, src);\n}', 'BN_MONT_CTX *BN_MONT_CTX_copy(BN_MONT_CTX *to, BN_MONT_CTX *from)\n{\n if (to == from)\n return to;\n if (!BN_copy(&(to->RR), &(from->RR)))\n return NULL;\n if (!BN_copy(&(to->N), &(from->N)))\n return NULL;\n if (!BN_copy(&(to->Ni), &(from->Ni)))\n return NULL;\n to->ri = from->ri;\n to->n0[0] = from->n0[0];\n to->n0[1] = from->n0[1];\n return to;\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}']
895
0
https://github.com/openssl/openssl/blob/9c46f4b9cd4912b61cb546c48b678488d7f26ed6/crypto/bn/bn_ctx.c/#L353
static unsigned int BN_STACK_pop(BN_STACK *st) { return st->indexes[--(st->depth)]; }
['int RSA_check_key_ex(const RSA *key, BN_GENCB *cb)\n{\n BIGNUM *i, *j, *k, *l, *m;\n BN_CTX *ctx;\n int r;\n int ret = 1;\n if (!key->p || !key->q || !key->n || !key->e || !key->d) {\n RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_VALUE_MISSING);\n return 0;\n }\n i = BN_new();\n j = BN_new();\n k = BN_new();\n l = BN_new();\n m = BN_new();\n ctx = BN_CTX_new();\n if (i == NULL || j == NULL || k == NULL || l == NULL ||\n m == NULL || ctx == NULL) {\n ret = -1;\n RSAerr(RSA_F_RSA_CHECK_KEY_EX, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n r = BN_is_prime_ex(key->p, BN_prime_checks, NULL, cb);\n if (r != 1) {\n ret = r;\n if (r != 0)\n goto err;\n RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_P_NOT_PRIME);\n }\n r = BN_is_prime_ex(key->q, BN_prime_checks, NULL, cb);\n if (r != 1) {\n ret = r;\n if (r != 0)\n goto err;\n RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_Q_NOT_PRIME);\n }\n r = BN_mul(i, key->p, key->q, ctx);\n if (!r) {\n ret = -1;\n goto err;\n }\n if (BN_cmp(i, key->n) != 0) {\n ret = 0;\n RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_N_DOES_NOT_EQUAL_P_Q);\n }\n r = BN_sub(i, key->p, BN_value_one());\n if (!r) {\n ret = -1;\n goto err;\n }\n r = BN_sub(j, key->q, BN_value_one());\n if (!r) {\n ret = -1;\n goto err;\n }\n r = BN_mul(l, i, j, ctx);\n if (!r) {\n ret = -1;\n goto err;\n }\n r = BN_gcd(m, i, j, ctx);\n if (!r) {\n ret = -1;\n goto err;\n }\n r = BN_div(k, NULL, l, m, ctx);\n if (!r) {\n ret = -1;\n goto err;\n }\n r = BN_mod_mul(i, key->d, key->e, k, ctx);\n if (!r) {\n ret = -1;\n goto err;\n }\n if (!BN_is_one(i)) {\n ret = 0;\n RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_D_E_NOT_CONGRUENT_TO_1);\n }\n if (key->dmp1 != NULL && key->dmq1 != NULL && key->iqmp != NULL) {\n r = BN_sub(i, key->p, BN_value_one());\n if (!r) {\n ret = -1;\n goto err;\n }\n r = BN_mod(j, key->d, i, ctx);\n if (!r) {\n ret = -1;\n goto err;\n }\n if (BN_cmp(j, key->dmp1) != 0) {\n ret = 0;\n RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_DMP1_NOT_CONGRUENT_TO_D);\n }\n r = BN_sub(i, key->q, BN_value_one());\n if (!r) {\n ret = -1;\n goto err;\n }\n r = BN_mod(j, key->d, i, ctx);\n if (!r) {\n ret = -1;\n goto err;\n }\n if (BN_cmp(j, key->dmq1) != 0) {\n ret = 0;\n RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_DMQ1_NOT_CONGRUENT_TO_D);\n }\n if (!BN_mod_inverse(i, key->q, key->p, ctx)) {\n ret = -1;\n goto err;\n }\n if (BN_cmp(i, key->iqmp) != 0) {\n ret = 0;\n RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_IQMP_NOT_INVERSE_OF_Q);\n }\n }\n err:\n if (i != NULL)\n BN_free(i);\n if (j != NULL)\n BN_free(j);\n if (k != NULL)\n BN_free(k);\n if (l != NULL)\n BN_free(l);\n if (m != NULL)\n BN_free(m);\n if (ctx != NULL)\n BN_CTX_free(ctx);\n return (ret);\n}', 'int BN_is_prime_ex(const BIGNUM *a, int checks, BN_CTX *ctx_passed,\n BN_GENCB *cb)\n{\n return BN_is_prime_fasttest_ex(a, checks, ctx_passed, 0, cb);\n}', 'int BN_is_prime_fasttest_ex(const BIGNUM *a, int checks, BN_CTX *ctx_passed,\n int do_trial_division, BN_GENCB *cb)\n{\n int i, j, ret = -1;\n int k;\n BN_CTX *ctx = NULL;\n BIGNUM *A1, *A1_odd, *check;\n BN_MONT_CTX *mont = NULL;\n const BIGNUM *A = NULL;\n if (BN_cmp(a, BN_value_one()) <= 0)\n return 0;\n if (checks == BN_prime_checks)\n checks = BN_prime_checks_for_size(BN_num_bits(a));\n if (!BN_is_odd(a))\n return BN_is_word(a, 2);\n if (do_trial_division) {\n for (i = 1; i < NUMPRIMES; i++)\n if (BN_mod_word(a, primes[i]) == 0)\n return 0;\n if (!BN_GENCB_call(cb, 1, -1))\n goto err;\n }\n if (ctx_passed != NULL)\n ctx = ctx_passed;\n else if ((ctx = BN_CTX_new()) == NULL)\n goto err;\n BN_CTX_start(ctx);\n if (a->neg) {\n BIGNUM *t;\n if ((t = BN_CTX_get(ctx)) == NULL)\n goto err;\n BN_copy(t, a);\n t->neg = 0;\n A = t;\n } else\n A = a;\n A1 = BN_CTX_get(ctx);\n A1_odd = BN_CTX_get(ctx);\n check = BN_CTX_get(ctx);\n if (check == NULL)\n goto err;\n if (!BN_copy(A1, A))\n goto err;\n if (!BN_sub_word(A1, 1))\n goto err;\n if (BN_is_zero(A1)) {\n ret = 0;\n goto err;\n }\n k = 1;\n while (!BN_is_bit_set(A1, k))\n k++;\n if (!BN_rshift(A1_odd, A1, k))\n goto err;\n mont = BN_MONT_CTX_new();\n if (mont == NULL)\n goto err;\n if (!BN_MONT_CTX_set(mont, A, ctx))\n goto err;\n for (i = 0; i < checks; i++) {\n if (!BN_pseudo_rand_range(check, A1))\n goto err;\n if (!BN_add_word(check, 1))\n goto err;\n j = witness(check, A, A1, A1_odd, k, ctx, mont);\n if (j == -1)\n goto err;\n if (j) {\n ret = 0;\n goto err;\n }\n if (!BN_GENCB_call(cb, 1, i))\n goto err;\n }\n ret = 1;\n err:\n if (ctx != NULL) {\n BN_CTX_end(ctx);\n if (ctx_passed == NULL)\n BN_CTX_free(ctx);\n }\n if (mont != NULL)\n BN_MONT_CTX_free(mont);\n return (ret);\n}', 'int BN_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)\n{\n int ret = 0;\n int top, al, bl;\n BIGNUM *rr;\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n int i;\n#endif\n#ifdef BN_RECURSION\n BIGNUM *t = NULL;\n int j = 0, k;\n#endif\n bn_check_top(a);\n bn_check_top(b);\n bn_check_top(r);\n al = a->top;\n bl = b->top;\n if ((al == 0) || (bl == 0)) {\n BN_zero(r);\n return (1);\n }\n top = al + bl;\n BN_CTX_start(ctx);\n if ((r == a) || (r == b)) {\n if ((rr = BN_CTX_get(ctx)) == NULL)\n goto err;\n } else\n rr = r;\n rr->neg = a->neg ^ b->neg;\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n i = al - bl;\n#endif\n#ifdef BN_MUL_COMBA\n if (i == 0) {\n# if 0\n if (al == 4) {\n if (bn_wexpand(rr, 8) == NULL)\n goto err;\n rr->top = 8;\n bn_mul_comba4(rr->d, a->d, b->d);\n goto end;\n }\n# endif\n if (al == 8) {\n if (bn_wexpand(rr, 16) == NULL)\n goto err;\n rr->top = 16;\n bn_mul_comba8(rr->d, a->d, b->d);\n goto end;\n }\n }\n#endif\n#ifdef BN_RECURSION\n if ((al >= BN_MULL_SIZE_NORMAL) && (bl >= BN_MULL_SIZE_NORMAL)) {\n if (i >= -1 && i <= 1) {\n if (i >= 0) {\n j = BN_num_bits_word((BN_ULONG)al);\n }\n if (i == -1) {\n j = BN_num_bits_word((BN_ULONG)bl);\n }\n j = 1 << (j - 1);\n assert(j <= al || j <= bl);\n k = j + j;\n t = BN_CTX_get(ctx);\n if (t == NULL)\n goto err;\n if (al > j || bl > j) {\n if (bn_wexpand(t, k * 4) == NULL)\n goto err;\n if (bn_wexpand(rr, k * 4) == NULL)\n goto err;\n bn_mul_part_recursive(rr->d, a->d, b->d,\n j, al - j, bl - j, t->d);\n } else {\n if (bn_wexpand(t, k * 2) == NULL)\n goto err;\n if (bn_wexpand(rr, k * 2) == NULL)\n goto err;\n bn_mul_recursive(rr->d, a->d, b->d, j, al - j, bl - j, t->d);\n }\n rr->top = top;\n goto end;\n }\n# if 0\n if (i == 1 && !BN_get_flags(b, BN_FLG_STATIC_DATA)) {\n BIGNUM *tmp_bn = (BIGNUM *)b;\n if (bn_wexpand(tmp_bn, al) == NULL)\n goto err;\n tmp_bn->d[bl] = 0;\n bl++;\n i--;\n } else if (i == -1 && !BN_get_flags(a, BN_FLG_STATIC_DATA)) {\n BIGNUM *tmp_bn = (BIGNUM *)a;\n if (bn_wexpand(tmp_bn, bl) == NULL)\n goto err;\n tmp_bn->d[al] = 0;\n al++;\n i++;\n }\n if (i == 0) {\n j = BN_num_bits_word((BN_ULONG)al);\n j = 1 << (j - 1);\n k = j + j;\n t = BN_CTX_get(ctx);\n if (al == j) {\n if (bn_wexpand(t, k * 2) == NULL)\n goto err;\n if (bn_wexpand(rr, k * 2) == NULL)\n goto err;\n bn_mul_recursive(rr->d, a->d, b->d, al, t->d);\n } else {\n if (bn_wexpand(t, k * 4) == NULL)\n goto err;\n if (bn_wexpand(rr, k * 4) == NULL)\n goto err;\n bn_mul_part_recursive(rr->d, a->d, b->d, al - j, j, t->d);\n }\n rr->top = top;\n goto end;\n }\n# endif\n }\n#endif\n if (bn_wexpand(rr, top) == NULL)\n goto err;\n rr->top = top;\n bn_mul_normal(rr->d, a->d, al, b->d, bl);\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n end:\n#endif\n bn_correct_top(rr);\n if (r != rr)\n BN_copy(r, rr);\n ret = 1;\n err:\n bn_check_top(r);\n BN_CTX_end(ctx);\n return (ret);\n}', 'int BN_gcd(BIGNUM *r, const BIGNUM *in_a, const BIGNUM *in_b, BN_CTX *ctx)\n{\n BIGNUM *a, *b, *t;\n int ret = 0;\n bn_check_top(in_a);\n bn_check_top(in_b);\n BN_CTX_start(ctx);\n a = BN_CTX_get(ctx);\n b = BN_CTX_get(ctx);\n if (a == NULL || b == NULL)\n goto err;\n if (BN_copy(a, in_a) == NULL)\n goto err;\n if (BN_copy(b, in_b) == NULL)\n goto err;\n a->neg = 0;\n b->neg = 0;\n if (BN_cmp(a, b) < 0) {\n t = a;\n a = b;\n b = t;\n }\n t = euclid(a, b);\n if (t == NULL)\n goto err;\n if (BN_copy(r, t) == NULL)\n goto err;\n ret = 1;\n err:\n BN_CTX_end(ctx);\n bn_check_top(r);\n return (ret);\n}', 'void BN_CTX_start(BN_CTX *ctx)\n{\n CTXDBG_ENTRY("BN_CTX_start", ctx);\n if (ctx->err_stack || ctx->too_many)\n ctx->err_stack++;\n else if (!BN_STACK_push(&ctx->stack, ctx->used)) {\n BNerr(BN_F_BN_CTX_START, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n ctx->err_stack++;\n }\n CTXDBG_EXIT(ctx);\n}', 'void BN_CTX_end(BN_CTX *ctx)\n{\n CTXDBG_ENTRY("BN_CTX_end", ctx);\n if (ctx->err_stack)\n ctx->err_stack--;\n else {\n unsigned int fp = BN_STACK_pop(&ctx->stack);\n if (fp < ctx->used)\n BN_POOL_release(&ctx->pool, ctx->used - fp);\n ctx->used = fp;\n ctx->too_many = 0;\n }\n CTXDBG_EXIT(ctx);\n}', 'int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,\n BN_CTX *ctx)\n{\n int norm_shift, i, loop;\n BIGNUM *tmp, wnum, *snum, *sdiv, *res;\n BN_ULONG *resp, *wnump;\n BN_ULONG d0, d1;\n int num_n, div_n;\n int no_branch = 0;\n if ((num->top > 0 && num->d[num->top - 1] == 0) ||\n (divisor->top > 0 && divisor->d[divisor->top - 1] == 0)) {\n BNerr(BN_F_BN_DIV, BN_R_NOT_INITIALIZED);\n return 0;\n }\n bn_check_top(num);\n bn_check_top(divisor);\n if ((BN_get_flags(num, BN_FLG_CONSTTIME) != 0)\n || (BN_get_flags(divisor, BN_FLG_CONSTTIME) != 0)) {\n no_branch = 1;\n }\n bn_check_top(dv);\n bn_check_top(rm);\n if (BN_is_zero(divisor)) {\n BNerr(BN_F_BN_DIV, BN_R_DIV_BY_ZERO);\n return (0);\n }\n if (!no_branch && BN_ucmp(num, divisor) < 0) {\n if (rm != NULL) {\n if (BN_copy(rm, num) == NULL)\n return (0);\n }\n if (dv != NULL)\n BN_zero(dv);\n return (1);\n }\n BN_CTX_start(ctx);\n tmp = BN_CTX_get(ctx);\n snum = BN_CTX_get(ctx);\n sdiv = BN_CTX_get(ctx);\n if (dv == NULL)\n res = BN_CTX_get(ctx);\n else\n res = dv;\n if (sdiv == NULL || res == NULL || tmp == NULL || snum == NULL)\n goto err;\n norm_shift = BN_BITS2 - ((BN_num_bits(divisor)) % BN_BITS2);\n if (!(BN_lshift(sdiv, divisor, norm_shift)))\n goto err;\n sdiv->neg = 0;\n norm_shift += BN_BITS2;\n if (!(BN_lshift(snum, num, norm_shift)))\n goto err;\n snum->neg = 0;\n if (no_branch) {\n if (snum->top <= sdiv->top + 1) {\n if (bn_wexpand(snum, sdiv->top + 2) == NULL)\n goto err;\n for (i = snum->top; i < sdiv->top + 2; i++)\n snum->d[i] = 0;\n snum->top = sdiv->top + 2;\n } else {\n if (bn_wexpand(snum, snum->top + 1) == NULL)\n goto err;\n snum->d[snum->top] = 0;\n snum->top++;\n }\n }\n div_n = sdiv->top;\n num_n = snum->top;\n loop = num_n - div_n;\n wnum.neg = 0;\n wnum.d = &(snum->d[loop]);\n wnum.top = div_n;\n wnum.dmax = snum->dmax - loop;\n d0 = sdiv->d[div_n - 1];\n d1 = (div_n == 1) ? 0 : sdiv->d[div_n - 2];\n wnump = &(snum->d[num_n - 1]);\n res->neg = (num->neg ^ divisor->neg);\n if (!bn_wexpand(res, (loop + 1)))\n goto err;\n res->top = loop - no_branch;\n resp = &(res->d[loop - 1]);\n if (!bn_wexpand(tmp, (div_n + 1)))\n goto err;\n if (!no_branch) {\n if (BN_ucmp(&wnum, sdiv) >= 0) {\n bn_clear_top2max(&wnum);\n bn_sub_words(wnum.d, wnum.d, sdiv->d, div_n);\n *resp = 1;\n } else\n res->top--;\n }\n if (res->top == 0)\n res->neg = 0;\n else\n resp--;\n for (i = 0; i < loop - 1; i++, wnump--, resp--) {\n BN_ULONG q, l0;\n# if defined(BN_DIV3W) && !defined(OPENSSL_NO_ASM)\n BN_ULONG bn_div_3_words(BN_ULONG *, BN_ULONG, BN_ULONG);\n q = bn_div_3_words(wnump, d1, d0);\n# else\n BN_ULONG n0, n1, rem = 0;\n n0 = wnump[0];\n n1 = wnump[-1];\n if (n0 == d0)\n q = BN_MASK2;\n else {\n# ifdef BN_LLONG\n BN_ULLONG t2;\n# if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)\n q = (BN_ULONG)(((((BN_ULLONG) n0) << BN_BITS2) | n1) / d0);\n# else\n q = bn_div_words(n0, n1, d0);\n# ifdef BN_DEBUG_LEVITTE\n fprintf(stderr, "DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\\\nX) -> 0x%08X\\n", n0, n1, d0, q);\n# endif\n# endif\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n t2 = (BN_ULLONG) d1 *q;\n for (;;) {\n if (t2 <= ((((BN_ULLONG) rem) << BN_BITS2) | wnump[-2]))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n t2 -= d1;\n }\n# else\n BN_ULONG t2l, t2h;\n q = bn_div_words(n0, n1, d0);\n# ifdef BN_DEBUG_LEVITTE\n fprintf(stderr, "DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\\\nX) -> 0x%08X\\n", n0, n1, d0, q);\n# endif\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n# if defined(BN_UMULT_LOHI)\n BN_UMULT_LOHI(t2l, t2h, d1, q);\n# elif defined(BN_UMULT_HIGH)\n t2l = d1 * q;\n t2h = BN_UMULT_HIGH(d1, q);\n# else\n {\n BN_ULONG ql, qh;\n t2l = LBITS(d1);\n t2h = HBITS(d1);\n ql = LBITS(q);\n qh = HBITS(q);\n mul64(t2l, t2h, ql, qh);\n }\n# endif\n for (;;) {\n if ((t2h < rem) || ((t2h == rem) && (t2l <= wnump[-2])))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n if (t2l < d1)\n t2h--;\n t2l -= d1;\n }\n# endif\n }\n# endif\n l0 = bn_mul_words(tmp->d, sdiv->d, div_n, q);\n tmp->d[div_n] = l0;\n wnum.d--;\n if (bn_sub_words(wnum.d, wnum.d, tmp->d, div_n + 1)) {\n q--;\n if (bn_add_words(wnum.d, wnum.d, sdiv->d, div_n))\n (*wnump)++;\n }\n *resp = q;\n }\n bn_correct_top(snum);\n if (rm != NULL) {\n int neg = num->neg;\n BN_rshift(rm, snum, norm_shift);\n if (!BN_is_zero(rm))\n rm->neg = neg;\n bn_check_top(rm);\n }\n if (no_branch)\n bn_correct_top(res);\n BN_CTX_end(ctx);\n return (1);\n err:\n bn_check_top(rm);\n BN_CTX_end(ctx);\n return (0);\n}', 'int BN_mod_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m,\n BN_CTX *ctx)\n{\n BIGNUM *t;\n int ret = 0;\n bn_check_top(a);\n bn_check_top(b);\n bn_check_top(m);\n BN_CTX_start(ctx);\n if ((t = BN_CTX_get(ctx)) == NULL)\n goto err;\n if (a == b) {\n if (!BN_sqr(t, a, ctx))\n goto err;\n } else {\n if (!BN_mul(t, a, b, ctx))\n goto err;\n }\n if (!BN_nnmod(r, t, m, ctx))\n goto err;\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}', 'static unsigned int BN_STACK_pop(BN_STACK *st)\n{\n return st->indexes[--(st->depth)];\n}']
896
0
https://github.com/openssl/openssl/blob/7671342e550ed2de676b23c79d0e7f45a381c76e/crypto/bn/bn_lib.c/#L636
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->d[i] |= (((BN_ULONG)1) << j); bn_check_top(a); return (1); }
['static int parameter_test(void)\n{\n EC_GROUP *group = NULL, *group2 = NULL;\n ECPARAMETERS *ecparameters = NULL;\n int r;\n r = TEST_ptr(group = EC_GROUP_new_by_curve_name(NID_secp112r1))\n && TEST_ptr(ecparameters = EC_GROUP_get_ecparameters(group, NULL))\n && TEST_ptr(group2 = EC_GROUP_new_from_ecparameters(ecparameters))\n && TEST_int_eq(EC_GROUP_cmp(group, group2, NULL), 0);\n EC_GROUP_free(group);\n EC_GROUP_free(group2);\n ECPARAMETERS_free(ecparameters);\n return r;\n}', 'EC_GROUP *EC_GROUP_new_from_ecparameters(const ECPARAMETERS *params)\n{\n int ok = 0, tmp;\n EC_GROUP *ret = NULL;\n BIGNUM *p = NULL, *a = NULL, *b = NULL;\n EC_POINT *point = NULL;\n long field_bits;\n if (!params->fieldID || !params->fieldID->fieldType ||\n !params->fieldID->p.ptr) {\n ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_ASN1_ERROR);\n goto err;\n }\n if (!params->curve || !params->curve->a ||\n !params->curve->a->data || !params->curve->b ||\n !params->curve->b->data) {\n ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_ASN1_ERROR);\n goto err;\n }\n a = BN_bin2bn(params->curve->a->data, params->curve->a->length, NULL);\n if (a == NULL) {\n ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_BN_LIB);\n goto err;\n }\n b = BN_bin2bn(params->curve->b->data, params->curve->b->length, NULL);\n if (b == NULL) {\n ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_BN_LIB);\n goto err;\n }\n tmp = OBJ_obj2nid(params->fieldID->fieldType);\n if (tmp == NID_X9_62_characteristic_two_field)\n#ifdef OPENSSL_NO_EC2M\n {\n ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_GF2M_NOT_SUPPORTED);\n goto err;\n }\n#else\n {\n X9_62_CHARACTERISTIC_TWO *char_two;\n char_two = params->fieldID->p.char_two;\n field_bits = char_two->m;\n if (field_bits > OPENSSL_ECC_MAX_FIELD_BITS) {\n ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_FIELD_TOO_LARGE);\n goto err;\n }\n if ((p = BN_new()) == NULL) {\n ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n tmp = OBJ_obj2nid(char_two->type);\n if (tmp == NID_X9_62_tpBasis) {\n long tmp_long;\n if (!char_two->p.tpBasis) {\n ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_ASN1_ERROR);\n goto err;\n }\n tmp_long = ASN1_INTEGER_get(char_two->p.tpBasis);\n if (!(char_two->m > tmp_long && tmp_long > 0)) {\n ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS,\n EC_R_INVALID_TRINOMIAL_BASIS);\n goto err;\n }\n if (!BN_set_bit(p, (int)char_two->m))\n goto err;\n if (!BN_set_bit(p, (int)tmp_long))\n goto err;\n if (!BN_set_bit(p, 0))\n goto err;\n } else if (tmp == NID_X9_62_ppBasis) {\n X9_62_PENTANOMIAL *penta;\n penta = char_two->p.ppBasis;\n if (!penta) {\n ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_ASN1_ERROR);\n goto err;\n }\n if (!\n (char_two->m > penta->k3 && penta->k3 > penta->k2\n && penta->k2 > penta->k1 && penta->k1 > 0)) {\n ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS,\n EC_R_INVALID_PENTANOMIAL_BASIS);\n goto err;\n }\n if (!BN_set_bit(p, (int)char_two->m))\n goto err;\n if (!BN_set_bit(p, (int)penta->k1))\n goto err;\n if (!BN_set_bit(p, (int)penta->k2))\n goto err;\n if (!BN_set_bit(p, (int)penta->k3))\n goto err;\n if (!BN_set_bit(p, 0))\n goto err;\n } else if (tmp == NID_X9_62_onBasis) {\n ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_NOT_IMPLEMENTED);\n goto err;\n } else {\n ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_ASN1_ERROR);\n goto err;\n }\n ret = EC_GROUP_new_curve_GF2m(p, a, b, NULL);\n }\n#endif\n else if (tmp == NID_X9_62_prime_field) {\n if (!params->fieldID->p.prime) {\n ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_ASN1_ERROR);\n goto err;\n }\n p = ASN1_INTEGER_to_BN(params->fieldID->p.prime, NULL);\n if (p == NULL) {\n ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_ASN1_LIB);\n goto err;\n }\n if (BN_is_negative(p) || BN_is_zero(p)) {\n ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_INVALID_FIELD);\n goto err;\n }\n field_bits = BN_num_bits(p);\n if (field_bits > OPENSSL_ECC_MAX_FIELD_BITS) {\n ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_FIELD_TOO_LARGE);\n goto err;\n }\n ret = EC_GROUP_new_curve_GFp(p, a, b, NULL);\n } else {\n ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_INVALID_FIELD);\n goto err;\n }\n if (ret == NULL) {\n ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_EC_LIB);\n goto err;\n }\n if (params->curve->seed != NULL) {\n OPENSSL_free(ret->seed);\n if ((ret->seed = OPENSSL_malloc(params->curve->seed->length)) == NULL) {\n ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n memcpy(ret->seed, params->curve->seed->data,\n params->curve->seed->length);\n ret->seed_len = params->curve->seed->length;\n }\n if (!params->order || !params->base || !params->base->data) {\n ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_ASN1_ERROR);\n goto err;\n }\n if ((point = EC_POINT_new(ret)) == NULL)\n goto err;\n EC_GROUP_set_point_conversion_form(ret, (point_conversion_form_t)\n (params->base->data[0] & ~0x01));\n if (!EC_POINT_oct2point(ret, point, params->base->data,\n params->base->length, NULL)) {\n ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_EC_LIB);\n goto err;\n }\n if ((a = ASN1_INTEGER_to_BN(params->order, a)) == NULL) {\n ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_ASN1_LIB);\n goto err;\n }\n if (BN_is_negative(a) || BN_is_zero(a)) {\n ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_INVALID_GROUP_ORDER);\n goto err;\n }\n if (BN_num_bits(a) > (int)field_bits + 1) {\n ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_INVALID_GROUP_ORDER);\n goto err;\n }\n if (params->cofactor == NULL) {\n BN_free(b);\n b = NULL;\n } else if ((b = ASN1_INTEGER_to_BN(params->cofactor, b)) == NULL) {\n ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_ASN1_LIB);\n goto err;\n }\n if (!EC_GROUP_set_generator(ret, point, a, b)) {\n ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_EC_LIB);\n goto err;\n }\n ok = 1;\n err:\n if (!ok) {\n EC_GROUP_clear_free(ret);\n ret = NULL;\n }\n BN_free(p);\n BN_free(a);\n BN_free(b);\n EC_POINT_free(point);\n return (ret);\n}', 'int BN_set_bit(BIGNUM *a, int n)\n{\n int i, j, k;\n if (n < 0)\n return 0;\n i = n / BN_BITS2;\n j = n % BN_BITS2;\n if (a->top <= i) {\n if (bn_wexpand(a, i + 1) == NULL)\n return (0);\n for (k = a->top; k < i + 1; k++)\n a->d[k] = 0;\n a->top = i + 1;\n }\n a->d[i] |= (((BN_ULONG)1) << j);\n bn_check_top(a);\n return (1);\n}']
897
0
https://github.com/openssl/openssl/blob/b0c863865054bdf454f3b381a347a4d105158694/apps/ca.c/#L2857
int make_revoked(X509_REVOKED *rev, char *str) { char *tmp = NULL; char *rtime_str, *reason_str = NULL, *arg_str = NULL, *p; int reason_code = -1; int i, ret = 0; ASN1_OBJECT *hold = NULL; ASN1_GENERALIZEDTIME *comp_time = NULL; ASN1_ENUMERATED *rtmp = NULL; tmp = BUF_strdup(str); p = strchr(tmp, ','); rtime_str = tmp; if (p) { *p = '\0'; p++; reason_str = p; p = strchr(p, ','); if (p) { *p = '\0'; arg_str = p + 1; } } if (rev && !ASN1_UTCTIME_set_string(rev->revocationDate, rtime_str)) { BIO_printf(bio_err, "invalid revocation date %s\n", rtime_str); goto err; } if (reason_str) { for (i = 0; i < NUM_REASONS; i++) { if(!strcasecmp(reason_str, crl_reasons[i])) { reason_code = i; break; } } if (reason_code == OCSP_REVOKED_STATUS_NOSTATUS) { BIO_printf(bio_err, "invalid reason code %s\n", reason_str); goto err; } if (reason_code == 7) reason_code = OCSP_REVOKED_STATUS_REMOVEFROMCRL; else if (reason_code == 8) { if (!arg_str) { BIO_printf(bio_err, "missing hold instruction\n"); goto err; } reason_code = OCSP_REVOKED_STATUS_CERTIFICATEHOLD; hold = OBJ_txt2obj(arg_str, 0); if (!hold) { BIO_printf(bio_err, "invalid object identifier %s\n", arg_str); goto err; } } else if ((reason_code == 9) || (reason_code == 10)) { if (!arg_str) { BIO_printf(bio_err, "missing compromised time\n"); goto err; } comp_time = ASN1_GENERALIZEDTIME_new(); if (!ASN1_GENERALIZEDTIME_set_string(comp_time, arg_str)) { BIO_printf(bio_err, "invalid compromised time %s\n", arg_str); goto err; } if (reason_code == 9) reason_code = OCSP_REVOKED_STATUS_KEYCOMPROMISE; else reason_code = OCSP_REVOKED_STATUS_CACOMPROMISE; } } if (rev && (reason_code != OCSP_REVOKED_STATUS_NOSTATUS)) { rtmp = ASN1_ENUMERATED_new(); if (!rtmp || !ASN1_ENUMERATED_set(rtmp, reason_code)) goto err; if (!X509_REVOKED_add1_ext_i2d(rev, NID_crl_reason, rtmp, 0, 0)) goto err; } if (rev && comp_time) { if (!X509_REVOKED_add1_ext_i2d(rev, NID_invalidity_date, comp_time, 0, 0)) goto err; } if (rev && hold) { if (!X509_REVOKED_add1_ext_i2d(rev, NID_hold_instruction_code, hold, 0, 0)) goto err; } if (reason_code != OCSP_REVOKED_STATUS_NOSTATUS) ret = 2; else ret = 1; err: if (tmp) OPENSSL_free(tmp); ASN1_OBJECT_free(hold); ASN1_GENERALIZEDTIME_free(comp_time); ASN1_ENUMERATED_free(rtmp); return ret; }
['int make_revoked(X509_REVOKED *rev, char *str)\n\t{\n\tchar *tmp = NULL;\n\tchar *rtime_str, *reason_str = NULL, *arg_str = NULL, *p;\n\tint reason_code = -1;\n\tint i, ret = 0;\n\tASN1_OBJECT *hold = NULL;\n\tASN1_GENERALIZEDTIME *comp_time = NULL;\n\tASN1_ENUMERATED *rtmp = NULL;\n\ttmp = BUF_strdup(str);\n\tp = strchr(tmp, \',\');\n\trtime_str = tmp;\n\tif (p)\n\t\t{\n\t\t*p = \'\\0\';\n\t\tp++;\n\t\treason_str = p;\n\t\tp = strchr(p, \',\');\n\t\tif (p)\n\t\t\t{\n\t\t\t*p = \'\\0\';\n\t\t\targ_str = p + 1;\n\t\t\t}\n\t\t}\n\tif (rev && !ASN1_UTCTIME_set_string(rev->revocationDate, rtime_str))\n\t\t{\n\t\tBIO_printf(bio_err, "invalid revocation date %s\\n", rtime_str);\n\t\tgoto err;\n\t\t}\n\tif (reason_str)\n\t\t{\n\t\tfor (i = 0; i < NUM_REASONS; i++)\n\t\t\t{\n\t\t\tif(!strcasecmp(reason_str, crl_reasons[i]))\n\t\t\t\t{\n\t\t\t\treason_code = i;\n\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\tif (reason_code == OCSP_REVOKED_STATUS_NOSTATUS)\n\t\t\t{\n\t\t\tBIO_printf(bio_err, "invalid reason code %s\\n", reason_str);\n\t\t\tgoto err;\n\t\t\t}\n\t\tif (reason_code == 7)\n\t\t\treason_code = OCSP_REVOKED_STATUS_REMOVEFROMCRL;\n\t\telse if (reason_code == 8)\n\t\t\t{\n\t\t\tif (!arg_str)\n\t\t\t\t{\n\t\t\t\tBIO_printf(bio_err, "missing hold instruction\\n");\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\treason_code = OCSP_REVOKED_STATUS_CERTIFICATEHOLD;\n\t\t\thold = OBJ_txt2obj(arg_str, 0);\n\t\t\tif (!hold)\n\t\t\t\t{\n\t\t\t\tBIO_printf(bio_err, "invalid object identifier %s\\n", arg_str);\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\t}\n\t\telse if ((reason_code == 9) || (reason_code == 10))\n\t\t\t{\n\t\t\tif (!arg_str)\n\t\t\t\t{\n\t\t\t\tBIO_printf(bio_err, "missing compromised time\\n");\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\tcomp_time = ASN1_GENERALIZEDTIME_new();\n\t\t\tif (!ASN1_GENERALIZEDTIME_set_string(comp_time, arg_str))\n\t\t\t\t{\n\t\t\t\tBIO_printf(bio_err, "invalid compromised time %s\\n", arg_str);\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\tif (reason_code == 9)\n\t\t\t\treason_code = OCSP_REVOKED_STATUS_KEYCOMPROMISE;\n\t\t\telse\n\t\t\t\treason_code = OCSP_REVOKED_STATUS_CACOMPROMISE;\n\t\t\t}\n\t\t}\n\tif (rev && (reason_code != OCSP_REVOKED_STATUS_NOSTATUS))\n\t\t{\n\t\trtmp = ASN1_ENUMERATED_new();\n\t\tif (!rtmp || !ASN1_ENUMERATED_set(rtmp, reason_code))\n\t\t\tgoto err;\n\t\tif (!X509_REVOKED_add1_ext_i2d(rev, NID_crl_reason, rtmp, 0, 0))\n\t\t\tgoto err;\n\t\t}\n\tif (rev && comp_time)\n\t\t{\n\t\tif (!X509_REVOKED_add1_ext_i2d(rev, NID_invalidity_date, comp_time, 0, 0))\n\t\t\tgoto err;\n\t\t}\n\tif (rev && hold)\n\t\t{\n\t\tif (!X509_REVOKED_add1_ext_i2d(rev, NID_hold_instruction_code, hold, 0, 0))\n\t\t\tgoto err;\n\t\t}\n\tif (reason_code != OCSP_REVOKED_STATUS_NOSTATUS)\n\t\tret = 2;\n\telse ret = 1;\n\terr:\n\tif (tmp) OPENSSL_free(tmp);\n\tASN1_OBJECT_free(hold);\n\tASN1_GENERALIZEDTIME_free(comp_time);\n\tASN1_ENUMERATED_free(rtmp);\n\treturn ret;\n\t}', 'char *BUF_strdup(const char *str)\n\t{\n\tchar *ret;\n\tint n;\n\tif (str == NULL) return(NULL);\n\tn=strlen(str);\n\tret=OPENSSL_malloc(n+1);\n\tif (ret == NULL)\n\t\t{\n\t\tBUFerr(BUF_F_BUF_STRDUP,ERR_R_MALLOC_FAILURE);\n\t\treturn(NULL);\n\t\t}\n\tmemcpy(ret,str,n+1);\n\treturn(ret);\n\t}', 'void *CRYPTO_malloc(int num, const char *file, int line)\n\t{\n\tvoid *ret = NULL;\n\tallow_customize = 0;\n\tif (malloc_debug_func != NULL)\n\t\t{\n\t\tallow_customize_debug = 0;\n\t\tmalloc_debug_func(NULL, num, file, line, 0);\n\t\t}\n\tret = malloc_ex_func(num,file,line);\n#ifdef LEVITTE_DEBUG_MEM\n\tfprintf(stderr, "LEVITTE_DEBUG_MEM: > 0x%p (%d)\\n", ret, num);\n#endif\n\tif (malloc_debug_func != NULL)\n\t\tmalloc_debug_func(ret, num, file, line, 1);\n\treturn ret;\n\t}', 'void ERR_put_error(int lib, int func, int reason, const char *file,\n\t int line)\n\t{\n\tERR_STATE *es;\n#ifdef _OSD_POSIX\n\tif (strncmp(file,"*POSIX(", sizeof("*POSIX(")-1) == 0) {\n\t\tchar *end;\n\t\tfile += sizeof("*POSIX(")-1;\n\t\tend = &file[strlen(file)-1];\n\t\tif (*end == \')\')\n\t\t\t*end = \'\\0\';\n\t\tif ((end = strrchr(file, \'/\')) != NULL)\n\t\t\tfile = &end[1];\n\t}\n#endif\n\tes=ERR_get_state();\n\tes->top=(es->top+1)%ERR_NUM_ERRORS;\n\tif (es->top == es->bottom)\n\t\tes->bottom=(es->bottom+1)%ERR_NUM_ERRORS;\n\tes->err_buffer[es->top]=ERR_PACK(lib,func,reason);\n\tes->err_file[es->top]=file;\n\tes->err_line[es->top]=line;\n\terr_clear_data(es,es->top);\n\t}', 'void CRYPTO_free(void *str)\n\t{\n\tif (free_debug_func != NULL)\n\t\tfree_debug_func(str, 0);\n#ifdef LEVITTE_DEBUG_MEM\n\tfprintf(stderr, "LEVITTE_DEBUG_MEM: < 0x%p\\n", str);\n#endif\n\tfree_func(str);\n\tif (free_debug_func != NULL)\n\t\tfree_debug_func(NULL, 1);\n\t}']
898
0
https://github.com/libav/libav/blob/cb2c4de3a16c083973921587b6e8c79af59c9626/libavcodec/vc1dec.c/#L4941
static void vc1_sprite_parse_transform(GetBitContext* gb, int c[7]) { c[1] = c[3] = 0; switch (get_bits(gb, 2)) { case 0: c[0] = 1 << 16; c[2] = get_fp_val(gb); c[4] = 1 << 16; break; case 1: c[0] = c[4] = get_fp_val(gb); c[2] = get_fp_val(gb); break; case 2: c[0] = get_fp_val(gb); c[2] = get_fp_val(gb); c[4] = get_fp_val(gb); break; case 3: c[0] = get_fp_val(gb); c[1] = get_fp_val(gb); c[2] = get_fp_val(gb); c[3] = get_fp_val(gb); c[4] = get_fp_val(gb); break; } c[5] = get_fp_val(gb); if (get_bits1(gb)) c[6] = get_fp_val(gb); else c[6] = 1 << 16; }
['static int vc1_decode_frame(AVCodecContext *avctx, void *data,\n int *data_size, AVPacket *avpkt)\n{\n const uint8_t *buf = avpkt->data;\n int buf_size = avpkt->size, n_slices = 0, i;\n VC1Context *v = avctx->priv_data;\n MpegEncContext *s = &v->s;\n AVFrame *pict = data;\n uint8_t *buf2 = NULL;\n uint8_t *buf_field2 = NULL;\n const uint8_t *buf_start = buf;\n int mb_height, n_slices1;\n struct {\n uint8_t *buf;\n GetBitContext gb;\n int mby_start;\n } *slices = NULL;\n if (buf_size == 0 || (buf_size == 4 && AV_RB32(buf) == VC1_CODE_ENDOFSEQ)) {\n if (s->low_delay == 0 && s->next_picture_ptr) {\n *pict = *(AVFrame*)s->next_picture_ptr;\n s->next_picture_ptr = NULL;\n *data_size = sizeof(AVFrame);\n }\n return 0;\n }\n if (s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU) {\n if (v->profile < PROFILE_ADVANCED)\n avctx->pix_fmt = PIX_FMT_VDPAU_WMV3;\n else\n avctx->pix_fmt = PIX_FMT_VDPAU_VC1;\n }\n if (avctx->codec_id == CODEC_ID_VC1 || avctx->codec_id == CODEC_ID_VC1IMAGE) {\n int buf_size2 = 0;\n buf2 = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);\n if (IS_MARKER(AV_RB32(buf))) {\n const uint8_t *start, *end, *next;\n int size;\n next = buf;\n for (start = buf, end = buf + buf_size; next < end; start = next) {\n next = find_next_marker(start + 4, end);\n size = next - start - 4;\n if (size <= 0) continue;\n switch (AV_RB32(start)) {\n case VC1_CODE_FRAME:\n if (avctx->hwaccel ||\n s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)\n buf_start = start;\n buf_size2 = vc1_unescape_buffer(start + 4, size, buf2);\n break;\n case VC1_CODE_FIELD: {\n int buf_size3;\n slices = av_realloc(slices, sizeof(*slices) * (n_slices+1));\n if (!slices)\n goto err;\n slices[n_slices].buf = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);\n if (!slices[n_slices].buf)\n goto err;\n buf_size3 = vc1_unescape_buffer(start + 4, size,\n slices[n_slices].buf);\n init_get_bits(&slices[n_slices].gb, slices[n_slices].buf,\n buf_size3 << 3);\n slices[n_slices].mby_start = s->mb_height >> 1;\n n_slices1 = n_slices - 1;\n n_slices++;\n buf_field2 = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);\n vc1_unescape_buffer(start + 4, size, buf_field2);\n break;\n }\n case VC1_CODE_ENTRYPOINT:\n buf_size2 = vc1_unescape_buffer(start + 4, size, buf2);\n init_get_bits(&s->gb, buf2, buf_size2 * 8);\n vc1_decode_entry_point(avctx, v, &s->gb);\n break;\n case VC1_CODE_SLICE: {\n int buf_size3;\n slices = av_realloc(slices, sizeof(*slices) * (n_slices+1));\n if (!slices)\n goto err;\n slices[n_slices].buf = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);\n if (!slices[n_slices].buf)\n goto err;\n buf_size3 = vc1_unescape_buffer(start + 4, size,\n slices[n_slices].buf);\n init_get_bits(&slices[n_slices].gb, slices[n_slices].buf,\n buf_size3 << 3);\n slices[n_slices].mby_start = get_bits(&slices[n_slices].gb, 9);\n n_slices++;\n break;\n }\n }\n }\n } else if (v->interlace && ((buf[0] & 0xC0) == 0xC0)) {\n const uint8_t *divider;\n divider = find_next_marker(buf, buf + buf_size);\n if ((divider == (buf + buf_size)) || AV_RB32(divider) != VC1_CODE_FIELD) {\n av_log(avctx, AV_LOG_ERROR, "Error in WVC1 interlaced frame\\n");\n goto err;\n } else {\n buf_field2 = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);\n vc1_unescape_buffer(divider + 4, buf + buf_size - divider - 4, buf_field2);\n }\n buf_size2 = vc1_unescape_buffer(buf, divider - buf, buf2);\n } else {\n buf_size2 = vc1_unescape_buffer(buf, buf_size, buf2);\n }\n init_get_bits(&s->gb, buf2, buf_size2*8);\n } else\n init_get_bits(&s->gb, buf, buf_size*8);\n if (v->res_sprite) {\n v->new_sprite = !get_bits1(&s->gb);\n v->two_sprites = get_bits1(&s->gb);\n if (avctx->codec_id == CODEC_ID_WMV3IMAGE || avctx->codec_id == CODEC_ID_VC1IMAGE) {\n if (v->new_sprite) {\n avctx->width = avctx->coded_width = v->sprite_width;\n avctx->height = avctx->coded_height = v->sprite_height;\n } else {\n goto image;\n }\n }\n }\n if (s->context_initialized &&\n (s->width != avctx->coded_width ||\n s->height != avctx->coded_height)) {\n vc1_decode_end(avctx);\n }\n if (!s->context_initialized) {\n if (ff_msmpeg4_decode_init(avctx) < 0 || vc1_decode_init_alloc_tables(v) < 0)\n return -1;\n s->low_delay = !avctx->has_b_frames || v->res_sprite;\n if (v->profile == PROFILE_ADVANCED) {\n s->h_edge_pos = avctx->coded_width;\n s->v_edge_pos = avctx->coded_height;\n }\n }\n if (s->current_picture_ptr == NULL || s->current_picture_ptr->f.data[0]) {\n int i = ff_find_unused_picture(s, 0);\n s->current_picture_ptr = &s->picture[i];\n }\n v->pic_header_flag = 0;\n if (v->profile < PROFILE_ADVANCED) {\n if (vc1_parse_frame_header(v, &s->gb) == -1) {\n goto err;\n }\n } else {\n if (vc1_parse_frame_header_adv(v, &s->gb) == -1) {\n goto err;\n }\n }\n if ((avctx->codec_id == CODEC_ID_WMV3IMAGE || avctx->codec_id == CODEC_ID_VC1IMAGE)\n && s->pict_type != AV_PICTURE_TYPE_I) {\n av_log(v->s.avctx, AV_LOG_ERROR, "Sprite decoder: expected I-frame\\n");\n goto err;\n }\n s->current_picture_ptr->f.repeat_pict = 0;\n if (v->rff) {\n s->current_picture_ptr->f.repeat_pict = 1;\n } else if (v->rptfrm) {\n s->current_picture_ptr->f.repeat_pict = v->rptfrm * 2;\n }\n s->current_picture.f.pict_type = s->pict_type;\n s->current_picture.f.key_frame = s->pict_type == AV_PICTURE_TYPE_I;\n if (s->last_picture_ptr == NULL && (s->pict_type == AV_PICTURE_TYPE_B || s->dropable)) {\n goto err;\n }\n if ((avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type == AV_PICTURE_TYPE_B) ||\n (avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type != AV_PICTURE_TYPE_I) ||\n avctx->skip_frame >= AVDISCARD_ALL) {\n goto end;\n }\n if (s->next_p_frame_damaged) {\n if (s->pict_type == AV_PICTURE_TYPE_B)\n goto end;\n else\n s->next_p_frame_damaged = 0;\n }\n if (MPV_frame_start(s, avctx) < 0) {\n goto err;\n }\n s->me.qpel_put = s->dsp.put_qpel_pixels_tab;\n s->me.qpel_avg = s->dsp.avg_qpel_pixels_tab;\n if ((CONFIG_VC1_VDPAU_DECODER)\n &&s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)\n ff_vdpau_vc1_decode_picture(s, buf_start, (buf + buf_size) - buf_start);\n else if (avctx->hwaccel) {\n if (avctx->hwaccel->start_frame(avctx, buf, buf_size) < 0)\n goto err;\n if (avctx->hwaccel->decode_slice(avctx, buf_start, (buf + buf_size) - buf_start) < 0)\n goto err;\n if (avctx->hwaccel->end_frame(avctx) < 0)\n goto err;\n } else {\n ff_er_frame_start(s);\n v->bits = buf_size * 8;\n if (v->field_mode) {\n uint8_t *tmp[2];\n s->current_picture.f.linesize[0] <<= 1;\n s->current_picture.f.linesize[1] <<= 1;\n s->current_picture.f.linesize[2] <<= 1;\n s->linesize <<= 1;\n s->uvlinesize <<= 1;\n tmp[0] = v->mv_f_last[0];\n tmp[1] = v->mv_f_last[1];\n v->mv_f_last[0] = v->mv_f_next[0];\n v->mv_f_last[1] = v->mv_f_next[1];\n v->mv_f_next[0] = v->mv_f[0];\n v->mv_f_next[1] = v->mv_f[1];\n v->mv_f[0] = tmp[0];\n v->mv_f[1] = tmp[1];\n }\n mb_height = s->mb_height >> v->field_mode;\n for (i = 0; i <= n_slices; i++) {\n if (i > 0 && slices[i - 1].mby_start >= mb_height) {\n v->second_field = 1;\n v->blocks_off = s->mb_width * s->mb_height << 1;\n v->mb_off = s->mb_stride * s->mb_height >> 1;\n } else {\n v->second_field = 0;\n v->blocks_off = 0;\n v->mb_off = 0;\n }\n if (i) {\n v->pic_header_flag = 0;\n if (v->field_mode && i == n_slices1 + 2)\n vc1_parse_frame_header_adv(v, &s->gb);\n else if (get_bits1(&s->gb)) {\n v->pic_header_flag = 1;\n vc1_parse_frame_header_adv(v, &s->gb);\n }\n }\n s->start_mb_y = (i == 0) ? 0 : FFMAX(0, slices[i-1].mby_start % mb_height);\n if (!v->field_mode || v->second_field)\n s->end_mb_y = (i == n_slices ) ? mb_height : FFMIN(mb_height, slices[i].mby_start % mb_height);\n else\n s->end_mb_y = (i == n_slices1 + 1) ? mb_height : FFMIN(mb_height, slices[i].mby_start % mb_height);\n vc1_decode_blocks(v);\n if (i != n_slices)\n s->gb = slices[i].gb;\n }\n if (v->field_mode) {\n av_free(buf_field2);\n v->second_field = 0;\n }\n if (v->field_mode) {\n if (s->pict_type == AV_PICTURE_TYPE_B) {\n memcpy(v->mv_f_base, v->mv_f_next_base,\n 2 * (s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2));\n }\n s->current_picture.f.linesize[0] >>= 1;\n s->current_picture.f.linesize[1] >>= 1;\n s->current_picture.f.linesize[2] >>= 1;\n s->linesize >>= 1;\n s->uvlinesize >>= 1;\n }\n ff_er_frame_end(s);\n }\n MPV_frame_end(s);\n if (avctx->codec_id == CODEC_ID_WMV3IMAGE || avctx->codec_id == CODEC_ID_VC1IMAGE) {\nimage:\n avctx->width = avctx->coded_width = v->output_width;\n avctx->height = avctx->coded_height = v->output_height;\n if (avctx->skip_frame >= AVDISCARD_NONREF)\n goto end;\n#if CONFIG_WMV3IMAGE_DECODER || CONFIG_VC1IMAGE_DECODER\n if (vc1_decode_sprites(v, &s->gb))\n goto err;\n#endif\n *pict = v->sprite_output_frame;\n *data_size = sizeof(AVFrame);\n } else {\n if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) {\n *pict = *(AVFrame*)s->current_picture_ptr;\n } else if (s->last_picture_ptr != NULL) {\n *pict = *(AVFrame*)s->last_picture_ptr;\n }\n if (s->last_picture_ptr || s->low_delay) {\n *data_size = sizeof(AVFrame);\n ff_print_debug_info(s, pict);\n }\n }\nend:\n av_free(buf2);\n for (i = 0; i < n_slices; i++)\n av_free(slices[i].buf);\n av_free(slices);\n return buf_size;\nerr:\n av_free(buf2);\n for (i = 0; i < n_slices; i++)\n av_free(slices[i].buf);\n av_free(slices);\n av_free(buf_field2);\n return -1;\n}', 'static int vc1_decode_sprites(VC1Context *v, GetBitContext* gb)\n{\n MpegEncContext *s = &v->s;\n AVCodecContext *avctx = s->avctx;\n SpriteData sd;\n vc1_parse_sprites(v, gb, &sd);\n if (!s->current_picture.f.data[0]) {\n av_log(avctx, AV_LOG_ERROR, "Got no sprites\\n");\n return -1;\n }\n if (v->two_sprites && (!s->last_picture_ptr || !s->last_picture.f.data[0])) {\n av_log(avctx, AV_LOG_WARNING, "Need two sprites, only got one\\n");\n v->two_sprites = 0;\n }\n if (v->sprite_output_frame.data[0])\n avctx->release_buffer(avctx, &v->sprite_output_frame);\n v->sprite_output_frame.buffer_hints = FF_BUFFER_HINTS_VALID;\n v->sprite_output_frame.reference = 0;\n if (avctx->get_buffer(avctx, &v->sprite_output_frame) < 0) {\n av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\\n");\n return -1;\n }\n vc1_draw_sprites(v, &sd);\n return 0;\n}', 'static void vc1_parse_sprites(VC1Context *v, GetBitContext* gb, SpriteData* sd)\n{\n AVCodecContext *avctx = v->s.avctx;\n int sprite, i;\n for (sprite = 0; sprite <= v->two_sprites; sprite++) {\n vc1_sprite_parse_transform(gb, sd->coefs[sprite]);\n if (sd->coefs[sprite][1] || sd->coefs[sprite][3])\n av_log_ask_for_sample(avctx, "Rotation coefficients are not zero");\n av_log(avctx, AV_LOG_DEBUG, sprite ? "S2:" : "S1:");\n for (i = 0; i < 7; i++)\n av_log(avctx, AV_LOG_DEBUG, " %d.%.3d",\n sd->coefs[sprite][i] / (1<<16),\n (abs(sd->coefs[sprite][i]) & 0xFFFF) * 1000 / (1 << 16));\n av_log(avctx, AV_LOG_DEBUG, "\\n");\n }\n skip_bits(gb, 2);\n if (sd->effect_type = get_bits_long(gb, 30)) {\n switch (sd->effect_pcount1 = get_bits(gb, 4)) {\n case 7:\n vc1_sprite_parse_transform(gb, sd->effect_params1);\n break;\n case 14:\n vc1_sprite_parse_transform(gb, sd->effect_params1);\n vc1_sprite_parse_transform(gb, sd->effect_params1 + 7);\n break;\n default:\n for (i = 0; i < sd->effect_pcount1; i++)\n sd->effect_params1[i] = get_fp_val(gb);\n }\n if (sd->effect_type != 13 || sd->effect_params1[0] != sd->coefs[0][6]) {\n av_log(avctx, AV_LOG_DEBUG, "Effect: %d; params: ", sd->effect_type);\n for (i = 0; i < sd->effect_pcount1; i++)\n av_log(avctx, AV_LOG_DEBUG, " %d.%.2d",\n sd->effect_params1[i] / (1 << 16),\n (abs(sd->effect_params1[i]) & 0xFFFF) * 1000 / (1 << 16));\n av_log(avctx, AV_LOG_DEBUG, "\\n");\n }\n sd->effect_pcount2 = get_bits(gb, 16);\n if (sd->effect_pcount2 > 10) {\n av_log(avctx, AV_LOG_ERROR, "Too many effect parameters\\n");\n return;\n } else if (sd->effect_pcount2) {\n i = -1;\n av_log(avctx, AV_LOG_DEBUG, "Effect params 2: ");\n while (++i < sd->effect_pcount2) {\n sd->effect_params2[i] = get_fp_val(gb);\n av_log(avctx, AV_LOG_DEBUG, " %d.%.2d",\n sd->effect_params2[i] / (1 << 16),\n (abs(sd->effect_params2[i]) & 0xFFFF) * 1000 / (1 << 16));\n }\n av_log(avctx, AV_LOG_DEBUG, "\\n");\n }\n }\n if (sd->effect_flag = get_bits1(gb))\n av_log(avctx, AV_LOG_DEBUG, "Effect flag set\\n");\n if (get_bits_count(gb) >= gb->size_in_bits +\n (avctx->codec_id == CODEC_ID_WMV3IMAGE ? 64 : 0))\n av_log(avctx, AV_LOG_ERROR, "Buffer overrun\\n");\n if (get_bits_count(gb) < gb->size_in_bits - 8)\n av_log(avctx, AV_LOG_WARNING, "Buffer not fully read\\n");\n}', 'static void vc1_sprite_parse_transform(GetBitContext* gb, int c[7])\n{\n c[1] = c[3] = 0;\n switch (get_bits(gb, 2)) {\n case 0:\n c[0] = 1 << 16;\n c[2] = get_fp_val(gb);\n c[4] = 1 << 16;\n break;\n case 1:\n c[0] = c[4] = get_fp_val(gb);\n c[2] = get_fp_val(gb);\n break;\n case 2:\n c[0] = get_fp_val(gb);\n c[2] = get_fp_val(gb);\n c[4] = get_fp_val(gb);\n break;\n case 3:\n c[0] = get_fp_val(gb);\n c[1] = get_fp_val(gb);\n c[2] = get_fp_val(gb);\n c[3] = get_fp_val(gb);\n c[4] = get_fp_val(gb);\n break;\n }\n c[5] = get_fp_val(gb);\n if (get_bits1(gb))\n c[6] = get_fp_val(gb);\n else\n c[6] = 1 << 16;\n}']
899
0
https://github.com/openssl/openssl/blob/9b02dc97e4963969da69675a871dbe80e6d31cda/crypto/bn/bn_lib.c/#L260
static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words) { BN_ULONG *a = NULL; bn_check_top(b); if (words > (INT_MAX / (4 * BN_BITS2))) { BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG); return NULL; } if (BN_get_flags(b, BN_FLG_STATIC_DATA)) { BNerr(BN_F_BN_EXPAND_INTERNAL, 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) { BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE); return NULL; } assert(b->top <= words); if (b->top > 0) memcpy(a, b->d, sizeof(*a) * b->top); return a; }
['int ec_GFp_simple_set_compressed_coordinates(const EC_GROUP *group,\n EC_POINT *point,\n const BIGNUM *x_, int y_bit,\n BN_CTX *ctx)\n{\n BN_CTX *new_ctx = NULL;\n BIGNUM *tmp1, *tmp2, *x, *y;\n int ret = 0;\n ERR_clear_error();\n if (ctx == NULL) {\n ctx = new_ctx = BN_CTX_new();\n if (ctx == NULL)\n return 0;\n }\n y_bit = (y_bit != 0);\n BN_CTX_start(ctx);\n tmp1 = BN_CTX_get(ctx);\n tmp2 = BN_CTX_get(ctx);\n x = BN_CTX_get(ctx);\n y = BN_CTX_get(ctx);\n if (y == NULL)\n goto err;\n if (!BN_nnmod(x, x_, group->field, ctx))\n goto err;\n if (group->meth->field_decode == 0) {\n if (!group->meth->field_sqr(group, tmp2, x_, ctx))\n goto err;\n if (!group->meth->field_mul(group, tmp1, tmp2, x_, ctx))\n goto err;\n } else {\n if (!BN_mod_sqr(tmp2, x_, group->field, ctx))\n goto err;\n if (!BN_mod_mul(tmp1, tmp2, x_, group->field, ctx))\n goto err;\n }\n if (group->a_is_minus3) {\n if (!BN_mod_lshift1_quick(tmp2, x, group->field))\n goto err;\n if (!BN_mod_add_quick(tmp2, tmp2, x, group->field))\n goto err;\n if (!BN_mod_sub_quick(tmp1, tmp1, tmp2, group->field))\n goto err;\n } else {\n if (group->meth->field_decode) {\n if (!group->meth->field_decode(group, tmp2, group->a, ctx))\n goto err;\n if (!BN_mod_mul(tmp2, tmp2, x, group->field, ctx))\n goto err;\n } else {\n if (!group->meth->field_mul(group, tmp2, group->a, x, ctx))\n goto err;\n }\n if (!BN_mod_add_quick(tmp1, tmp1, tmp2, group->field))\n goto err;\n }\n if (group->meth->field_decode) {\n if (!group->meth->field_decode(group, tmp2, group->b, ctx))\n goto err;\n if (!BN_mod_add_quick(tmp1, tmp1, tmp2, group->field))\n goto err;\n } else {\n if (!BN_mod_add_quick(tmp1, tmp1, group->b, group->field))\n goto err;\n }\n if (!BN_mod_sqrt(y, tmp1, group->field, ctx)) {\n unsigned long err = ERR_peek_last_error();\n if (ERR_GET_LIB(err) == ERR_LIB_BN\n && ERR_GET_REASON(err) == BN_R_NOT_A_SQUARE) {\n ERR_clear_error();\n ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,\n EC_R_INVALID_COMPRESSED_POINT);\n } else\n ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,\n ERR_R_BN_LIB);\n goto err;\n }\n if (y_bit != BN_is_odd(y)) {\n if (BN_is_zero(y)) {\n int kron;\n kron = BN_kronecker(x, group->field, ctx);\n if (kron == -2)\n goto err;\n if (kron == 1)\n ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,\n EC_R_INVALID_COMPRESSION_BIT);\n else\n ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,\n EC_R_INVALID_COMPRESSED_POINT);\n goto err;\n }\n if (!BN_usub(y, group->field, y))\n goto err;\n }\n if (y_bit != BN_is_odd(y)) {\n ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,\n ERR_R_INTERNAL_ERROR);\n goto err;\n }\n if (!EC_POINT_set_affine_coordinates_GFp(group, point, x, y, ctx))\n goto err;\n ret = 1;\n err:\n BN_CTX_end(ctx);\n BN_CTX_free(new_ctx);\n return ret;\n}', 'BIGNUM *BN_CTX_get(BN_CTX *ctx)\n{\n BIGNUM *ret;\n CTXDBG_ENTRY("BN_CTX_get", ctx);\n if (ctx->err_stack || ctx->too_many)\n return NULL;\n if ((ret = BN_POOL_get(&ctx->pool, ctx->flags)) == NULL) {\n ctx->too_many = 1;\n BNerr(BN_F_BN_CTX_GET, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n return NULL;\n }\n BN_zero(ret);\n ctx->used++;\n CTXDBG_RET(ctx, ret);\n return ret;\n}', 'int BN_set_word(BIGNUM *a, BN_ULONG w)\n{\n bn_check_top(a);\n if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)\n return 0;\n a->neg = 0;\n a->d[0] = w;\n a->top = (w ? 1 : 0);\n bn_check_top(a);\n return 1;\n}', 'int BN_nnmod(BIGNUM *r, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx)\n{\n if (!(BN_mod(r, m, d, ctx)))\n return 0;\n if (!r->neg)\n return 1;\n return (d->neg ? BN_sub : BN_add) (r, r, d);\n}', 'int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,\n BN_CTX *ctx)\n{\n int norm_shift, i, loop;\n BIGNUM *tmp, wnum, *snum, *sdiv, *res;\n BN_ULONG *resp, *wnump;\n BN_ULONG d0, d1;\n int num_n, div_n;\n int no_branch = 0;\n if ((num->top > 0 && num->d[num->top - 1] == 0) ||\n (divisor->top > 0 && divisor->d[divisor->top - 1] == 0)) {\n BNerr(BN_F_BN_DIV, BN_R_NOT_INITIALIZED);\n return 0;\n }\n bn_check_top(num);\n bn_check_top(divisor);\n if ((BN_get_flags(num, BN_FLG_CONSTTIME) != 0)\n || (BN_get_flags(divisor, BN_FLG_CONSTTIME) != 0)) {\n no_branch = 1;\n }\n bn_check_top(dv);\n bn_check_top(rm);\n if (BN_is_zero(divisor)) {\n BNerr(BN_F_BN_DIV, BN_R_DIV_BY_ZERO);\n return 0;\n }\n if (!no_branch && BN_ucmp(num, divisor) < 0) {\n if (rm != NULL) {\n if (BN_copy(rm, num) == NULL)\n return 0;\n }\n if (dv != NULL)\n BN_zero(dv);\n return 1;\n }\n BN_CTX_start(ctx);\n res = (dv == NULL) ? BN_CTX_get(ctx) : dv;\n tmp = BN_CTX_get(ctx);\n snum = BN_CTX_get(ctx);\n sdiv = BN_CTX_get(ctx);\n if (sdiv == NULL)\n goto err;\n norm_shift = BN_BITS2 - ((BN_num_bits(divisor)) % BN_BITS2);\n if (!(BN_lshift(sdiv, divisor, norm_shift)))\n goto err;\n sdiv->neg = 0;\n norm_shift += BN_BITS2;\n if (!(BN_lshift(snum, num, norm_shift)))\n goto err;\n snum->neg = 0;\n if (no_branch) {\n if (snum->top <= sdiv->top + 1) {\n if (bn_wexpand(snum, sdiv->top + 2) == NULL)\n goto err;\n for (i = snum->top; i < sdiv->top + 2; i++)\n snum->d[i] = 0;\n snum->top = sdiv->top + 2;\n } else {\n if (bn_wexpand(snum, snum->top + 1) == NULL)\n goto err;\n snum->d[snum->top] = 0;\n snum->top++;\n }\n }\n div_n = sdiv->top;\n num_n = snum->top;\n loop = num_n - div_n;\n wnum.neg = 0;\n wnum.d = &(snum->d[loop]);\n wnum.top = div_n;\n wnum.dmax = snum->dmax - loop;\n d0 = sdiv->d[div_n - 1];\n d1 = (div_n == 1) ? 0 : sdiv->d[div_n - 2];\n wnump = &(snum->d[num_n - 1]);\n if (!bn_wexpand(res, (loop + 1)))\n goto err;\n res->neg = (num->neg ^ divisor->neg);\n res->top = loop - no_branch;\n resp = &(res->d[loop - 1]);\n if (!bn_wexpand(tmp, (div_n + 1)))\n goto err;\n if (!no_branch) {\n if (BN_ucmp(&wnum, sdiv) >= 0) {\n bn_clear_top2max(&wnum);\n bn_sub_words(wnum.d, wnum.d, sdiv->d, div_n);\n *resp = 1;\n } else\n res->top--;\n }\n resp++;\n if (res->top == 0)\n res->neg = 0;\n else\n resp--;\n for (i = 0; i < loop - 1; i++, wnump--) {\n BN_ULONG q, l0;\n# if defined(BN_DIV3W) && !defined(OPENSSL_NO_ASM)\n BN_ULONG bn_div_3_words(BN_ULONG *, BN_ULONG, BN_ULONG);\n q = bn_div_3_words(wnump, d1, d0);\n# else\n BN_ULONG n0, n1, rem = 0;\n n0 = wnump[0];\n n1 = wnump[-1];\n if (n0 == d0)\n q = BN_MASK2;\n else {\n# ifdef BN_LLONG\n BN_ULLONG t2;\n# if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)\n q = (BN_ULONG)(((((BN_ULLONG) n0) << BN_BITS2) | n1) / d0);\n# else\n q = bn_div_words(n0, n1, d0);\n# endif\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n t2 = (BN_ULLONG) d1 *q;\n for (;;) {\n if (t2 <= ((((BN_ULLONG) rem) << BN_BITS2) | wnump[-2]))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n t2 -= d1;\n }\n# else\n BN_ULONG t2l, t2h;\n q = bn_div_words(n0, n1, d0);\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n# if defined(BN_UMULT_LOHI)\n BN_UMULT_LOHI(t2l, t2h, d1, q);\n# elif defined(BN_UMULT_HIGH)\n t2l = d1 * q;\n t2h = BN_UMULT_HIGH(d1, q);\n# else\n {\n BN_ULONG ql, qh;\n t2l = LBITS(d1);\n t2h = HBITS(d1);\n ql = LBITS(q);\n qh = HBITS(q);\n mul64(t2l, t2h, ql, qh);\n }\n# endif\n for (;;) {\n if ((t2h < rem) || ((t2h == rem) && (t2l <= wnump[-2])))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n if (t2l < d1)\n t2h--;\n t2l -= d1;\n }\n# endif\n }\n# endif\n l0 = bn_mul_words(tmp->d, sdiv->d, div_n, q);\n tmp->d[div_n] = l0;\n wnum.d--;\n if (bn_sub_words(wnum.d, wnum.d, tmp->d, div_n + 1)) {\n q--;\n if (bn_add_words(wnum.d, wnum.d, sdiv->d, div_n))\n (*wnump)++;\n }\n resp--;\n *resp = q;\n }\n bn_correct_top(snum);\n if (rm != NULL) {\n int neg = num->neg;\n BN_rshift(rm, snum, norm_shift);\n if (!BN_is_zero(rm))\n rm->neg = neg;\n bn_check_top(rm);\n }\n if (no_branch)\n bn_correct_top(res);\n BN_CTX_end(ctx);\n return 1;\n err:\n bn_check_top(rm);\n BN_CTX_end(ctx);\n return 0;\n}', 'int BN_rshift(BIGNUM *r, const BIGNUM *a, int n)\n{\n int i, j, nw, lb, rb;\n BN_ULONG *t, *f;\n BN_ULONG l, tmp;\n bn_check_top(r);\n bn_check_top(a);\n if (n < 0) {\n BNerr(BN_F_BN_RSHIFT, BN_R_INVALID_SHIFT);\n return 0;\n }\n nw = n / BN_BITS2;\n rb = n % BN_BITS2;\n lb = BN_BITS2 - rb;\n if (nw >= a->top || a->top == 0) {\n BN_zero(r);\n return 1;\n }\n i = (BN_num_bits(a) - n + (BN_BITS2 - 1)) / BN_BITS2;\n if (r != a) {\n if (bn_wexpand(r, i) == NULL)\n return 0;\n r->neg = a->neg;\n } else {\n if (n == 0)\n return 1;\n }\n f = &(a->d[nw]);\n t = r->d;\n j = a->top - nw;\n r->top = i;\n if (rb == 0) {\n for (i = j; i != 0; i--)\n *(t++) = *(f++);\n } else {\n l = *(f++);\n for (i = j - 1; i != 0; i--) {\n tmp = (l >> rb) & BN_MASK2;\n l = *(f++);\n *(t++) = (tmp | (l << lb)) & BN_MASK2;\n }\n if ((l = (l >> rb) & BN_MASK2))\n *(t) = l;\n }\n if (!r->top)\n r->neg = 0;\n bn_check_top(r);\n return 1;\n}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n return (words <= a->dmax) ? a : bn_expand2(a, words);\n}', 'BIGNUM *bn_expand2(BIGNUM *b, int words)\n{\n bn_check_top(b);\n if (words > b->dmax) {\n BN_ULONG *a = bn_expand_internal(b, words);\n if (!a)\n return NULL;\n if (b->d) {\n OPENSSL_cleanse(b->d, b->dmax * sizeof(b->d[0]));\n bn_free_d(b);\n }\n b->d = a;\n b->dmax = words;\n }\n bn_check_top(b);\n return b;\n}', 'static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)\n{\n BN_ULONG *a = NULL;\n bn_check_top(b);\n if (words > (INT_MAX / (4 * BN_BITS2))) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);\n return NULL;\n }\n if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);\n return NULL;\n }\n if (BN_get_flags(b, BN_FLG_SECURE))\n a = OPENSSL_secure_zalloc(words * sizeof(*a));\n else\n a = OPENSSL_zalloc(words * sizeof(*a));\n if (a == NULL) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);\n return NULL;\n }\n assert(b->top <= words);\n if (b->top > 0)\n memcpy(a, b->d, sizeof(*a) * b->top);\n return a;\n}']
900
0
https://github.com/openssl/openssl/blob/ee3a6c646ff8ea6b9ada5a58f4a0e7c9b7be944b/crypto/bn/bn_sqr.c/#L161
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); }
['char *SRP_create_verifier(const char *user, const char *pass, char **salt,\n char **verifier, const char *N, const char *g)\n{\n int len;\n char *result = NULL, *vf = NULL;\n BIGNUM *N_bn = NULL, *g_bn = NULL, *s = NULL, *v = NULL;\n unsigned char tmp[MAX_LEN];\n unsigned char tmp2[MAX_LEN];\n char *defgNid = NULL;\n int vfsize = 0;\n if ((user == NULL) ||\n (pass == NULL) || (salt == NULL) || (verifier == NULL))\n goto err;\n if (N) {\n if ((len = t_fromb64(tmp, N)) == 0)\n goto err;\n N_bn = BN_bin2bn(tmp, len, NULL);\n if ((len = t_fromb64(tmp, g)) == 0)\n goto err;\n g_bn = BN_bin2bn(tmp, len, NULL);\n defgNid = "*";\n } else {\n SRP_gN *gN = SRP_get_gN_by_id(g, NULL);\n if (gN == NULL)\n goto err;\n N_bn = gN->N;\n g_bn = gN->g;\n defgNid = gN->id;\n }\n if (*salt == NULL) {\n if (RAND_bytes(tmp2, SRP_RANDOM_SALT_LEN) <= 0)\n goto err;\n s = BN_bin2bn(tmp2, SRP_RANDOM_SALT_LEN, NULL);\n } else {\n if ((len = t_fromb64(tmp2, *salt)) == 0)\n goto err;\n s = BN_bin2bn(tmp2, len, NULL);\n }\n if (!SRP_create_verifier_BN(user, pass, &s, &v, N_bn, g_bn))\n goto err;\n BN_bn2bin(v, tmp);\n vfsize = BN_num_bytes(v) * 2;\n if (((vf = OPENSSL_malloc(vfsize)) == NULL))\n goto err;\n t_tob64(vf, tmp, BN_num_bytes(v));\n if (*salt == NULL) {\n char *tmp_salt;\n if ((tmp_salt = OPENSSL_malloc(SRP_RANDOM_SALT_LEN * 2)) == NULL) {\n goto err;\n }\n t_tob64(tmp_salt, tmp2, SRP_RANDOM_SALT_LEN);\n *salt = tmp_salt;\n }\n *verifier = vf;\n vf = NULL;\n result = defgNid;\n err:\n if (N) {\n BN_free(N_bn);\n BN_free(g_bn);\n }\n OPENSSL_clear_free(vf, vfsize);\n BN_clear_free(s);\n BN_clear_free(v);\n return result;\n}', 'BIGNUM *BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret)\n{\n unsigned int i, m;\n unsigned int n;\n BN_ULONG l;\n BIGNUM *bn = NULL;\n if (ret == NULL)\n ret = bn = BN_new();\n if (ret == NULL)\n return (NULL);\n bn_check_top(ret);\n for ( ; len > 0 && *s == 0; s++, len--)\n continue;\n n = len;\n if (n == 0) {\n ret->top = 0;\n return (ret);\n }\n i = ((n - 1) / BN_BYTES) + 1;\n m = ((n - 1) % (BN_BYTES));\n if (bn_wexpand(ret, (int)i) == NULL) {\n BN_free(bn);\n return NULL;\n }\n ret->top = i;\n ret->neg = 0;\n l = 0;\n while (n--) {\n l = (l << 8L) | *(s++);\n if (m-- == 0) {\n ret->d[--i] = l;\n l = 0;\n m = BN_BYTES - 1;\n }\n }\n bn_correct_top(ret);\n return (ret);\n}', 'int SRP_create_verifier_BN(const char *user, const char *pass, BIGNUM **salt,\n BIGNUM **verifier, const BIGNUM *N,\n const BIGNUM *g)\n{\n int result = 0;\n BIGNUM *x = NULL;\n BN_CTX *bn_ctx = BN_CTX_new();\n unsigned char tmp2[MAX_LEN];\n BIGNUM *salttmp = NULL;\n if ((user == NULL) ||\n (pass == NULL) ||\n (salt == NULL) ||\n (verifier == NULL) || (N == NULL) || (g == NULL) || (bn_ctx == NULL))\n goto err;\n if (*salt == NULL) {\n if (RAND_bytes(tmp2, SRP_RANDOM_SALT_LEN) <= 0)\n goto err;\n salttmp = BN_bin2bn(tmp2, SRP_RANDOM_SALT_LEN, NULL);\n } else {\n salttmp = *salt;\n }\n x = SRP_Calc_x(salttmp, user, pass);\n *verifier = BN_new();\n if (*verifier == NULL)\n goto err;\n if (!BN_mod_exp(*verifier, g, x, N, bn_ctx)) {\n BN_clear_free(*verifier);\n goto err;\n }\n result = 1;\n *salt = salttmp;\n err:\n if (salt != NULL && *salt != salttmp)\n BN_clear_free(salttmp);\n BN_clear_free(x);\n BN_CTX_free(bn_ctx);\n return result;\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 powerbufLen += top * sizeof(mont->N.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 *n0 = mont->n0, *np;\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 for (np = am.d + top, i = 0; i < top; i++)\n np[i] = mont->N.d[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, np, 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, np, 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, np, 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, np, 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, np, n0, top, wvalue);\n }\n }\n ret = bn_from_montgomery(tmp.d, tmp.d, NULL, np, 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, window))\n goto err;\n if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&am, top, powerbuf, 1, window))\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(&tmp, top, powerbuf, 2,\n window))\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(&tmp, top, powerbuf, i,\n window))\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(&tmp, top, powerbuf, wvalue,\n window))\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(&am, top, powerbuf, wvalue,\n window))\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}']