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
|
|---|---|---|---|---|
2,101
| 0
|
https://github.com/libav/libav/blob/3a7f7678eb3be1f9a28414c9908ed8d34b1b9846/libavcodec/mp3_header_compress_bsf.c/#L52
|
static int mp3_header_compress(AVBitStreamFilterContext *bsfc, AVCodecContext *avctx, const char *args,
uint8_t **poutbuf, int *poutbuf_size,
const uint8_t *buf, int buf_size, int keyframe){
uint32_t header, extraheader;
int mode_extension, header_size;
if(avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL){
av_log(avctx, AV_LOG_ERROR, "not standards compliant\n");
return -1;
}
header = AV_RB32(buf);
mode_extension= (header>>4)&3;
if(ff_mpa_check_header(header) < 0 || (header&0x60000) != 0x20000){
output_unchanged:
*poutbuf= (uint8_t *) buf;
*poutbuf_size= buf_size;
av_log(avctx, AV_LOG_INFO, "cannot compress %08X\n", header);
return 0;
}
if(avctx->extradata_size == 0){
avctx->extradata_size=15;
avctx->extradata= av_malloc(avctx->extradata_size);
strcpy(avctx->extradata, "FFCMP3 0.0");
memcpy(avctx->extradata+11, buf, 4);
}
if(avctx->extradata_size != 15){
av_log(avctx, AV_LOG_ERROR, "Extradata invalid\n");
return -1;
}
extraheader = AV_RB32(avctx->extradata+11);
if((extraheader&MP3_MASK) != (header&MP3_MASK))
goto output_unchanged;
header_size= (header&0x10000) ? 4 : 6;
*poutbuf_size= buf_size - header_size;
*poutbuf= av_malloc(buf_size - header_size + FF_INPUT_BUFFER_PADDING_SIZE);
memcpy(*poutbuf, buf + header_size, buf_size - header_size + FF_INPUT_BUFFER_PADDING_SIZE);
if(avctx->channels==2){
if((header & (3<<19)) != 3<<19){
(*poutbuf)[1] &= 0x3F;
(*poutbuf)[1] |= mode_extension<<6;
FFSWAP(int, (*poutbuf)[1], (*poutbuf)[2]);
}else{
(*poutbuf)[1] &= 0x8F;
(*poutbuf)[1] |= mode_extension<<4;
}
}
return 1;
}
|
['static int mp3_header_compress(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 uint32_t header, extraheader;\n int mode_extension, header_size;\n if(avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL){\n av_log(avctx, AV_LOG_ERROR, "not standards compliant\\n");\n return -1;\n }\n header = AV_RB32(buf);\n mode_extension= (header>>4)&3;\n if(ff_mpa_check_header(header) < 0 || (header&0x60000) != 0x20000){\noutput_unchanged:\n *poutbuf= (uint8_t *) buf;\n *poutbuf_size= buf_size;\n av_log(avctx, AV_LOG_INFO, "cannot compress %08X\\n", header);\n return 0;\n }\n if(avctx->extradata_size == 0){\n avctx->extradata_size=15;\n avctx->extradata= av_malloc(avctx->extradata_size);\n strcpy(avctx->extradata, "FFCMP3 0.0");\n memcpy(avctx->extradata+11, buf, 4);\n }\n if(avctx->extradata_size != 15){\n av_log(avctx, AV_LOG_ERROR, "Extradata invalid\\n");\n return -1;\n }\n extraheader = AV_RB32(avctx->extradata+11);\n if((extraheader&MP3_MASK) != (header&MP3_MASK))\n goto output_unchanged;\n header_size= (header&0x10000) ? 4 : 6;\n *poutbuf_size= buf_size - header_size;\n *poutbuf= av_malloc(buf_size - header_size + FF_INPUT_BUFFER_PADDING_SIZE);\n memcpy(*poutbuf, buf + header_size, buf_size - header_size + FF_INPUT_BUFFER_PADDING_SIZE);\n if(avctx->channels==2){\n if((header & (3<<19)) != 3<<19){\n (*poutbuf)[1] &= 0x3F;\n (*poutbuf)[1] |= mode_extension<<6;\n FFSWAP(int, (*poutbuf)[1], (*poutbuf)[2]);\n }else{\n (*poutbuf)[1] &= 0x8F;\n (*poutbuf)[1] |= mode_extension<<4;\n }\n }\n return 1;\n}', 'static av_always_inline av_const uint32_t av_bswap32(uint32_t x)\n{\n return AV_BSWAP32C(x);\n}', 'static inline int ff_mpa_check_header(uint32_t header){\n if ((header & 0xffe00000) != 0xffe00000)\n return -1;\n if ((header & (3<<17)) == 0)\n return -1;\n if ((header & (0xf<<12)) == 0xf<<12)\n return -1;\n if ((header & (3<<10)) == 3<<10)\n return -1;\n return 0;\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}']
|
2,102
| 0
|
https://github.com/openssl/openssl/blob/1fac96e4d6484a517f2ebe99b72016726391723c/crypto/bn/bn_mul.c/#L728
|
void bn_mul_normal(BN_ULONG *r, BN_ULONG *a, int na, BN_ULONG *b, int nb)
{
BN_ULONG *rr;
#ifdef BN_COUNT
printf(" bn_mul_normal %d * %d\n",na,nb);
#endif
if (na < nb)
{
int itmp;
BN_ULONG *ltmp;
itmp=na; na=nb; nb=itmp;
ltmp=a; a=b; b=ltmp;
}
rr= &(r[na]);
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;
}
}
|
['int BN_mod_exp_simple(BIGNUM *r, BIGNUM *a, BIGNUM *p, BIGNUM *m,\n\t BN_CTX *ctx)\n\t{\n\tint i,j,bits,ret=0,wstart,wend,window,wvalue,ts=0;\n\tint start=1;\n\tBIGNUM *d;\n\tBIGNUM val[TABLE_SIZE];\n\td= &(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_init(&(val[0]));\n\tts=1;\n\tif (!BN_mod(&(val[0]),a,m,ctx)) goto err;\n\tif (!BN_mod_mul(d,&(val[0]),&(val[0]),m,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(&(val[i]),&(val[i-1]),d,m,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(r,r,r,m,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(r,r,r,m,ctx))\n\t\t\t\t\tgoto err;\n\t\t\t\t}\n\t\tif (!BN_mod_mul(r,r,&(val[wvalue>>1]),m,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\treturn(ret);\n\t}', 'int BN_mod(BIGNUM *rem, BIGNUM *m, BIGNUM *d, BN_CTX *ctx)\n\t{\n#if 0\n\tint i,nm,nd;\n\tBIGNUM *dv;\n\tif (BN_ucmp(m,d) < 0)\n\t\treturn((BN_copy(rem,m) == NULL)?0:1);\n\tdv= &(ctx->bn[ctx->tos]);\n\tif (!BN_copy(rem,m)) return(0);\n\tnm=BN_num_bits(rem);\n\tnd=BN_num_bits(d);\n\tif (!BN_lshift(dv,d,nm-nd)) return(0);\n\tfor (i=nm-nd; i>=0; i--)\n\t\t{\n\t\tif (BN_cmp(rem,dv) >= 0)\n\t\t\t{\n\t\t\tif (!BN_sub(rem,rem,dv)) return(0);\n\t\t\t}\n\t\tif (!BN_rshift1(dv,dv)) return(0);\n\t\t}\n\treturn(1);\n#else\n\treturn(BN_div(NULL,rem,m,d,ctx));\n#endif\n\t}', 'int BN_div(BIGNUM *dv, BIGNUM *rm, BIGNUM *num, BIGNUM *divisor,\n\t BN_CTX *ctx)\n\t{\n\tint norm_shift,i,j,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\tbn_check_top(num);\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 (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\ttmp= &(ctx->bn[ctx->tos]);\n\ttmp->neg=0;\n\tsnum= &(ctx->bn[ctx->tos+1]);\n\tsdiv= &(ctx->bn[ctx->tos+2]);\n\tif (dv == NULL)\n\t\tres= &(ctx->bn[ctx->tos+3]);\n\telse\tres=dv;\n\tnorm_shift=BN_BITS2-((BN_num_bits(divisor))%BN_BITS2);\n\tBN_lshift(sdiv,divisor,norm_shift);\n\tsdiv->neg=0;\n\tnorm_shift+=BN_BITS2;\n\tBN_lshift(snum,num,norm_shift);\n\tsnum->neg=0;\n\tdiv_n=sdiv->top;\n\tnum_n=snum->top;\n\tloop=num_n-div_n;\n\tBN_init(&wnum);\n\twnum.d=\t &(snum->d[loop]);\n\twnum.top= div_n;\n\twnum.max= snum->max+1;\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;\n\tresp= &(res->d[loop-1]);\n\tif (!bn_wexpand(tmp,(div_n+1))) goto err;\n\tif (BN_ucmp(&wnum,sdiv) >= 0)\n\t\t{\n\t\tif (!BN_usub(&wnum,&wnum,sdiv)) goto err;\n\t\t*resp=1;\n\t\tres->d[res->top-1]=1;\n\t\t}\n\telse\n\t\tres->top--;\n\tresp--;\n\tfor (i=0; i<loop-1; i++)\n\t\t{\n\t\tBN_ULONG q,n0,n1;\n\t\tBN_ULONG l0;\n\t\twnum.d--; wnum.top++;\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\tq=bn_div_words(n0,n1,d0);\n\t\t{\n#ifdef BN_LLONG\n\t\tBN_ULLONG t1,t2,rem;\n\t\tt1=((BN_ULLONG)n0<<BN_BITS2)|n1;\n\t\tfor (;;)\n\t\t\t{\n\t\t\tt2=(BN_ULLONG)d1*q;\n\t\t\trem=t1-(BN_ULLONG)q*d0;\n\t\t\tif ((rem>>BN_BITS2) ||\n\t\t\t\t(t2 <= ((BN_ULLONG)(rem<<BN_BITS2)+wnump[-2])))\n\t\t\t\tbreak;\n\t\t\tq--;\n\t\t\t}\n#else\n\t\tBN_ULONG t1l,t1h,t2l,t2h,t3l,t3h,ql,qh,t3t;\n\t\tt1h=n0;\n\t\tt1l=n1;\n\t\tfor (;;)\n\t\t\t{\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\tt3t=LBITS(d0); t3h=HBITS(d0);\n\t\t\tmul64(t3t,t3h,ql,qh);\n\t\t\tt3l=(t1l-t3t)&BN_MASK2;\n\t\t\tif (t3l > t1l) t3h++;\n\t\t\tt3h=(t1h-t3h)&BN_MASK2;\n\t\t\tif (t3h) break;\n\t\t\tif (t2h < t3l) break;\n\t\t\tif ((t2h == t3l) && (t2l <= wnump[-2])) break;\n\t\t\tq--;\n\t\t\t}\n#endif\n\t\t}\n\t\tl0=bn_mul_words(tmp->d,sdiv->d,div_n,q);\n\t\ttmp->d[div_n]=l0;\n\t\tfor (j=div_n+1; j>0; j--)\n\t\t\tif (tmp->d[j-1]) break;\n\t\ttmp->top=j;\n\t\tj=wnum.top;\n\t\tBN_sub(&wnum,&wnum,tmp);\n\t\tsnum->top=snum->top+wnum.top-j;\n\t\tif (wnum.neg)\n\t\t\t{\n\t\t\tq--;\n\t\t\tj=wnum.top;\n\t\t\tBN_add(&wnum,&wnum,sdiv);\n\t\t\tsnum->top+=wnum.top-j;\n\t\t\t}\n\t\t*(resp--)=q;\n\t\twnump--;\n\t\t}\n\tif (rm != NULL)\n\t\t{\n\t\tBN_rshift(rm,snum,norm_shift);\n\t\trm->neg=num->neg;\n\t\t}\n\treturn(1);\nerr:\n\treturn(0);\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_normal(BN_ULONG *r, BN_ULONG *a, int na, BN_ULONG *b, int nb)\n\t{\n\tBN_ULONG *rr;\n#ifdef BN_COUNT\nprintf(" bn_mul_normal %d * %d\\n",na,nb);\n#endif\n\tif (na < nb)\n\t\t{\n\t\tint itmp;\n\t\tBN_ULONG *ltmp;\n\t\titmp=na; na=nb; nb=itmp;\n\t\tltmp=a; a=b; b=ltmp;\n\t\t}\n\trr= &(r[na]);\n\trr[0]=bn_mul_words(r,a,na,b[0]);\n\tfor (;;)\n\t\t{\n\t\tif (--nb <= 0) return;\n\t\trr[1]=bn_mul_add_words(&(r[1]),a,na,b[1]);\n\t\tif (--nb <= 0) return;\n\t\trr[2]=bn_mul_add_words(&(r[2]),a,na,b[2]);\n\t\tif (--nb <= 0) return;\n\t\trr[3]=bn_mul_add_words(&(r[3]),a,na,b[3]);\n\t\tif (--nb <= 0) return;\n\t\trr[4]=bn_mul_add_words(&(r[4]),a,na,b[4]);\n\t\trr+=4;\n\t\tr+=4;\n\t\tb+=4;\n\t\t}\n\t}']
|
2,103
| 0
|
https://github.com/openssl/openssl/blob/a08e05d1be4c5affd5a833b9014664686b79c0e3/crypto/bn/bntest.c/#L1303
|
int test_gf2m_mod_exp(BIO *bp,BN_CTX *ctx)
{
BIGNUM *a,*b[2],*c,*d,*e,*f;
int i, j, ret = 0;
unsigned int p0[] = {163,7,6,3,0};
unsigned int p1[] = {193,15,0};
a=BN_new();
b[0]=BN_new();
b[1]=BN_new();
c=BN_new();
d=BN_new();
e=BN_new();
f=BN_new();
BN_GF2m_arr2poly(p0, b[0]);
BN_GF2m_arr2poly(p1, b[1]);
for (i=0; i<num0; i++)
{
BN_bntest_rand(a, 512, 0, 0);
BN_bntest_rand(c, 512, 0, 0);
BN_bntest_rand(d, 512, 0, 0);
for (j=0; j < 2; j++)
{
BN_GF2m_mod_exp(e, a, c, b[j], ctx);
BN_GF2m_mod_exp(f, a, d, b[j], ctx);
BN_GF2m_mod_mul(e, e, f, b[j], ctx);
BN_add(f, c, d);
BN_GF2m_mod_exp(f, a, f, b[j], ctx);
#if 0
if (bp != NULL)
{
if (!results)
{
BN_print(bp,a);
BIO_puts(bp, " ^ (");
BN_print(bp,c);
BIO_puts(bp," + ");
BN_print(bp,d);
BIO_puts(bp, ") = ");
BN_print(bp,e);
BIO_puts(bp, "; - ");
BN_print(bp,f);
BIO_puts(bp, " % ");
BN_print(bp,b[j]);
BIO_puts(bp,"\n");
}
}
#endif
BN_GF2m_add(f, e, f);
if(!BN_is_zero(f))
{
fprintf(stderr,"GF(2^m) modular exponentiation test failed!\n");
goto err;
}
}
}
ret = 1;
err:
BN_free(a);
BN_free(b[0]);
BN_free(b[1]);
BN_free(c);
BN_free(d);
BN_free(e);
BN_free(f);
return ret;
}
|
['int test_gf2m_mod_exp(BIO *bp,BN_CTX *ctx)\n\t{\n\tBIGNUM *a,*b[2],*c,*d,*e,*f;\n\tint i, j, ret = 0;\n\tunsigned int p0[] = {163,7,6,3,0};\n\tunsigned int p1[] = {193,15,0};\n\ta=BN_new();\n\tb[0]=BN_new();\n\tb[1]=BN_new();\n\tc=BN_new();\n\td=BN_new();\n\te=BN_new();\n\tf=BN_new();\n\tBN_GF2m_arr2poly(p0, b[0]);\n\tBN_GF2m_arr2poly(p1, b[1]);\n\tfor (i=0; i<num0; i++)\n\t\t{\n\t\tBN_bntest_rand(a, 512, 0, 0);\n\t\tBN_bntest_rand(c, 512, 0, 0);\n\t\tBN_bntest_rand(d, 512, 0, 0);\n\t\tfor (j=0; j < 2; j++)\n\t\t\t{\n\t\t\tBN_GF2m_mod_exp(e, a, c, b[j], ctx);\n\t\t\tBN_GF2m_mod_exp(f, a, d, b[j], ctx);\n\t\t\tBN_GF2m_mod_mul(e, e, f, b[j], ctx);\n\t\t\tBN_add(f, c, d);\n\t\t\tBN_GF2m_mod_exp(f, a, f, b[j], ctx);\n#if 0\n\t\t\tif (bp != NULL)\n\t\t\t\t{\n\t\t\t\tif (!results)\n\t\t\t\t\t{\n\t\t\t\t\tBN_print(bp,a);\n\t\t\t\t\tBIO_puts(bp, " ^ (");\n\t\t\t\t\tBN_print(bp,c);\n\t\t\t\t\tBIO_puts(bp," + ");\n\t\t\t\t\tBN_print(bp,d);\n\t\t\t\t\tBIO_puts(bp, ") = ");\n\t\t\t\t\tBN_print(bp,e);\n\t\t\t\t\tBIO_puts(bp, "; - ");\n\t\t\t\t\tBN_print(bp,f);\n\t\t\t\t\tBIO_puts(bp, " % ");\n\t\t\t\t\tBN_print(bp,b[j]);\n\t\t\t\t\tBIO_puts(bp,"\\n");\n\t\t\t\t\t}\n\t\t\t\t}\n#endif\n\t\t\tBN_GF2m_add(f, e, f);\n\t\t\tif(!BN_is_zero(f))\n\t\t\t\t{\n\t\t\t\tfprintf(stderr,"GF(2^m) modular exponentiation test failed!\\n");\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\tret = 1;\n err:\n\tBN_free(a);\n\tBN_free(b[0]);\n\tBN_free(b[1]);\n\tBN_free(c);\n\tBN_free(d);\n\tBN_free(e);\n\tBN_free(f);\n\treturn ret;\n\t}', 'BIGNUM *BN_new(void)\n\t{\n\tBIGNUM *ret;\n\tif ((ret=(BIGNUM *)OPENSSL_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->dmax=0;\n\tret->d=NULL;\n\tbn_check_top(ret);\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}', 'int BN_GF2m_arr2poly(const unsigned int p[], BIGNUM *a)\n\t{\n\tint i;\n\tbn_check_top(a);\n\tBN_zero(a);\n\tfor (i = 0; p[i] != 0; i++)\n\t\t{\n\t\tBN_set_bit(a, p[i]);\n\t\t}\n\tBN_set_bit(a, 0);\n\tbn_check_top(a);\n\treturn 1;\n\t}', 'int BN_set_word(BIGNUM *a, BN_ULONG w)\n\t{\n\tbn_check_top(a);\n\tif (bn_expand(a,(int)sizeof(BN_ULONG)*8) == NULL) return(0);\n\ta->neg = 0;\n\ta->d[0] = w;\n\ta->top = (w ? 1 : 0);\n\tbn_check_top(a);\n\treturn(1);\n\t}']
|
2,104
| 0
|
https://github.com/openssl/openssl/blob/95dc05bc6d0dfe0f3f3681f5e27afbc3f7a35eea/crypto/des/des_enc.c/#L144
|
void des_encrypt(DES_LONG *data, des_key_schedule ks, int enc)
{
register DES_LONG l,r,t,u;
#ifdef DES_PTR
register const unsigned char *des_SP=(const unsigned char *)des_SPtrans;
#endif
#ifndef DES_UNROLL
register int i;
#endif
register DES_LONG *s;
r=data[0];
l=data[1];
IP(r,l);
r=ROTATE(r,29)&0xffffffffL;
l=ROTATE(l,29)&0xffffffffL;
s=(DES_LONG *)ks;
if (enc)
{
#ifdef DES_UNROLL
D_ENCRYPT(l,r, 0);
D_ENCRYPT(r,l, 2);
D_ENCRYPT(l,r, 4);
D_ENCRYPT(r,l, 6);
D_ENCRYPT(l,r, 8);
D_ENCRYPT(r,l,10);
D_ENCRYPT(l,r,12);
D_ENCRYPT(r,l,14);
D_ENCRYPT(l,r,16);
D_ENCRYPT(r,l,18);
D_ENCRYPT(l,r,20);
D_ENCRYPT(r,l,22);
D_ENCRYPT(l,r,24);
D_ENCRYPT(r,l,26);
D_ENCRYPT(l,r,28);
D_ENCRYPT(r,l,30);
#else
for (i=0; i<32; i+=8)
{
D_ENCRYPT(l,r,i+0);
D_ENCRYPT(r,l,i+2);
D_ENCRYPT(l,r,i+4);
D_ENCRYPT(r,l,i+6);
}
#endif
}
else
{
#ifdef DES_UNROLL
D_ENCRYPT(l,r,30);
D_ENCRYPT(r,l,28);
D_ENCRYPT(l,r,26);
D_ENCRYPT(r,l,24);
D_ENCRYPT(l,r,22);
D_ENCRYPT(r,l,20);
D_ENCRYPT(l,r,18);
D_ENCRYPT(r,l,16);
D_ENCRYPT(l,r,14);
D_ENCRYPT(r,l,12);
D_ENCRYPT(l,r,10);
D_ENCRYPT(r,l, 8);
D_ENCRYPT(l,r, 6);
D_ENCRYPT(r,l, 4);
D_ENCRYPT(l,r, 2);
D_ENCRYPT(r,l, 0);
#else
for (i=30; i>0; i-=8)
{
D_ENCRYPT(l,r,i-0);
D_ENCRYPT(r,l,i-2);
D_ENCRYPT(l,r,i-4);
D_ENCRYPT(r,l,i-6);
}
#endif
}
l=ROTATE(l,3)&0xffffffffL;
r=ROTATE(r,3)&0xffffffffL;
FP(r,l);
data[0]=l;
data[1]=r;
l=r=t=u=0;
}
|
['int _des_crypt(char *buf, int len, struct desparams *desp)\n\t{\n\tdes_key_schedule ks;\n\tint enc;\n\tdes_set_key(desp->des_key,ks);\n\tenc=(desp->des_dir == ENCRYPT)?DES_ENCRYPT:DES_DECRYPT;\n\tif (desp->des_mode == CBC)\n\t\tdes_ecb_encrypt(desp->UDES.UDES_buf,desp->UDES.UDES_buf,ks,\n\t\t\t\tenc);\n\telse\n\t\t{\n\t\tdes_ncbc_encrypt(desp->UDES.UDES_buf,desp->UDES.UDES_buf,\n\t\t\t\tlen,ks,desp->des_ivec,enc);\n#ifdef undef\n\t\ta=(char *)&(desp->UDES.UDES_buf[len-8]);\n\t\tb=(char *)&(desp->des_ivec[0]);\n\t\t*(a++)= *(b++); *(a++)= *(b++);\n\t\t*(a++)= *(b++); *(a++)= *(b++);\n\t\t*(a++)= *(b++); *(a++)= *(b++);\n\t\t*(a++)= *(b++); *(a++)= *(b++);\n#endif\n\t\t}\n\treturn(1);\n\t}', 'void des_ncbc_encrypt(const unsigned char *in, unsigned char *out, long length,\n\t des_key_schedule schedule, des_cblock ivec, int enc)\n\t{\n\tregister DES_LONG tin0,tin1;\n\tregister DES_LONG tout0,tout1,xor0,xor1;\n\tregister long l=length;\n\tDES_LONG tin[2];\n\tunsigned char *iv;\n\tiv=ivec;\n\tif (enc)\n\t\t{\n\t\tc2l(iv,tout0);\n\t\tc2l(iv,tout1);\n\t\tfor (l-=8; l>=0; l-=8)\n\t\t\t{\n\t\t\tc2l(in,tin0);\n\t\t\tc2l(in,tin1);\n\t\t\ttin0^=tout0; tin[0]=tin0;\n\t\t\ttin1^=tout1; tin[1]=tin1;\n\t\t\tdes_encrypt((DES_LONG *)tin,schedule,DES_ENCRYPT);\n\t\t\ttout0=tin[0]; l2c(tout0,out);\n\t\t\ttout1=tin[1]; l2c(tout1,out);\n\t\t\t}\n\t\tif (l != -8)\n\t\t\t{\n\t\t\tc2ln(in,tin0,tin1,l+8);\n\t\t\ttin0^=tout0; tin[0]=tin0;\n\t\t\ttin1^=tout1; tin[1]=tin1;\n\t\t\tdes_encrypt((DES_LONG *)tin,schedule,DES_ENCRYPT);\n\t\t\ttout0=tin[0]; l2c(tout0,out);\n\t\t\ttout1=tin[1]; l2c(tout1,out);\n\t\t\t}\n\t\tiv=ivec;\n\t\tl2c(tout0,iv);\n\t\tl2c(tout1,iv);\n\t\t}\n\telse\n\t\t{\n\t\tc2l(iv,xor0);\n\t\tc2l(iv,xor1);\n\t\tfor (l-=8; l>=0; l-=8)\n\t\t\t{\n\t\t\tc2l(in,tin0); tin[0]=tin0;\n\t\t\tc2l(in,tin1); tin[1]=tin1;\n\t\t\tdes_encrypt((DES_LONG *)tin,schedule,DES_DECRYPT);\n\t\t\ttout0=tin[0]^xor0;\n\t\t\ttout1=tin[1]^xor1;\n\t\t\tl2c(tout0,out);\n\t\t\tl2c(tout1,out);\n\t\t\txor0=tin0;\n\t\t\txor1=tin1;\n\t\t\t}\n\t\tif (l != -8)\n\t\t\t{\n\t\t\tc2l(in,tin0); tin[0]=tin0;\n\t\t\tc2l(in,tin1); tin[1]=tin1;\n\t\t\tdes_encrypt((DES_LONG *)tin,schedule,DES_DECRYPT);\n\t\t\ttout0=tin[0]^xor0;\n\t\t\ttout1=tin[1]^xor1;\n\t\t\tl2cn(tout0,tout1,out,l+8);\n\t\t\txor0=tin0;\n\t\t\txor1=tin1;\n\t\t\t}\n\t\tiv=ivec;\n\t\tl2c(xor0,iv);\n\t\tl2c(xor1,iv);\n\t\t}\n\ttin0=tin1=tout0=tout1=xor0=xor1=0;\n\ttin[0]=tin[1]=0;\n\t}', 'void des_encrypt(DES_LONG *data, des_key_schedule ks, int enc)\n\t{\n\tregister DES_LONG l,r,t,u;\n#ifdef DES_PTR\n\tregister const unsigned char *des_SP=(const unsigned char *)des_SPtrans;\n#endif\n#ifndef DES_UNROLL\n\tregister int i;\n#endif\n\tregister DES_LONG *s;\n\tr=data[0];\n\tl=data[1];\n\tIP(r,l);\n\tr=ROTATE(r,29)&0xffffffffL;\n\tl=ROTATE(l,29)&0xffffffffL;\n\ts=(DES_LONG *)ks;\n\tif (enc)\n\t\t{\n#ifdef DES_UNROLL\n\t\tD_ENCRYPT(l,r, 0);\n\t\tD_ENCRYPT(r,l, 2);\n\t\tD_ENCRYPT(l,r, 4);\n\t\tD_ENCRYPT(r,l, 6);\n\t\tD_ENCRYPT(l,r, 8);\n\t\tD_ENCRYPT(r,l,10);\n\t\tD_ENCRYPT(l,r,12);\n\t\tD_ENCRYPT(r,l,14);\n\t\tD_ENCRYPT(l,r,16);\n\t\tD_ENCRYPT(r,l,18);\n\t\tD_ENCRYPT(l,r,20);\n\t\tD_ENCRYPT(r,l,22);\n\t\tD_ENCRYPT(l,r,24);\n\t\tD_ENCRYPT(r,l,26);\n\t\tD_ENCRYPT(l,r,28);\n\t\tD_ENCRYPT(r,l,30);\n#else\n\t\tfor (i=0; i<32; i+=8)\n\t\t\t{\n\t\t\tD_ENCRYPT(l,r,i+0);\n\t\t\tD_ENCRYPT(r,l,i+2);\n\t\t\tD_ENCRYPT(l,r,i+4);\n\t\t\tD_ENCRYPT(r,l,i+6);\n\t\t\t}\n#endif\n\t\t}\n\telse\n\t\t{\n#ifdef DES_UNROLL\n\t\tD_ENCRYPT(l,r,30);\n\t\tD_ENCRYPT(r,l,28);\n\t\tD_ENCRYPT(l,r,26);\n\t\tD_ENCRYPT(r,l,24);\n\t\tD_ENCRYPT(l,r,22);\n\t\tD_ENCRYPT(r,l,20);\n\t\tD_ENCRYPT(l,r,18);\n\t\tD_ENCRYPT(r,l,16);\n\t\tD_ENCRYPT(l,r,14);\n\t\tD_ENCRYPT(r,l,12);\n\t\tD_ENCRYPT(l,r,10);\n\t\tD_ENCRYPT(r,l, 8);\n\t\tD_ENCRYPT(l,r, 6);\n\t\tD_ENCRYPT(r,l, 4);\n\t\tD_ENCRYPT(l,r, 2);\n\t\tD_ENCRYPT(r,l, 0);\n#else\n\t\tfor (i=30; i>0; i-=8)\n\t\t\t{\n\t\t\tD_ENCRYPT(l,r,i-0);\n\t\t\tD_ENCRYPT(r,l,i-2);\n\t\t\tD_ENCRYPT(l,r,i-4);\n\t\t\tD_ENCRYPT(r,l,i-6);\n\t\t\t}\n#endif\n\t\t}\n\tl=ROTATE(l,3)&0xffffffffL;\n\tr=ROTATE(r,3)&0xffffffffL;\n\tFP(r,l);\n\tdata[0]=l;\n\tdata[1]=r;\n\tl=r=t=u=0;\n\t}']
|
2,105
| 0
|
https://github.com/openssl/openssl/blob/ed371b8cbac0d0349667558c061c1ae380cf75eb/crypto/bn/bn_ctx.c/#L276
|
static unsigned int BN_STACK_pop(BN_STACK *st)
{
return st->indexes[--(st->depth)];
}
|
['static int test_kronecker(void)\n{\n BIGNUM *a = NULL, *b = NULL, *r = NULL, *t = NULL;\n int i, legendre, kronecker, st = 0;\n if (!TEST_ptr(a = BN_new())\n || !TEST_ptr(b = BN_new())\n || !TEST_ptr(r = BN_new())\n || !TEST_ptr(t = BN_new()))\n goto err;\n if (!TEST_true(BN_generate_prime_ex(b, 512, 0, NULL, NULL, NULL)))\n goto err;\n BN_set_negative(b, rand_neg());\n for (i = 0; i < NUM0; i++) {\n if (!TEST_true(BN_bntest_rand(a, 512, 0, 0)))\n goto err;\n BN_set_negative(a, rand_neg());\n if (!TEST_true(BN_copy(t, b)))\n goto err;\n BN_set_negative(t, 0);\n if (!TEST_true(BN_sub_word(t, 1)))\n goto err;\n if (!TEST_true(BN_rshift1(t, t)))\n goto err;\n BN_set_negative(b, 0);\n if (!TEST_true(BN_mod_exp_recp(r, a, t, b, ctx)))\n goto err;\n BN_set_negative(b, 1);\n if (BN_is_word(r, 1))\n legendre = 1;\n else if (BN_is_zero(r))\n legendre = 0;\n else {\n if (!TEST_true(BN_add_word(r, 1)))\n goto err;\n if (!TEST_int_eq(BN_ucmp(r, b), 0)) {\n TEST_info("Legendre symbol computation failed");\n goto err;\n }\n legendre = -1;\n }\n if (!TEST_int_ge(kronecker = BN_kronecker(a, b, ctx), -1))\n goto err;\n if (BN_is_negative(a) && BN_is_negative(b))\n kronecker = -kronecker;\n if (!TEST_int_eq(legendre, kronecker))\n goto err;\n }\n st = 1;\n err:\n BN_free(a);\n BN_free(b);\n BN_free(r);\n BN_free(t);\n return st;\n}', 'int BN_kronecker(const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)\n{\n int i;\n int ret = -2;\n int err = 0;\n BIGNUM *A, *B, *tmp;\n static const int tab[8] = { 0, 1, 0, -1, 0, -1, 0, 1 };\n bn_check_top(a);\n bn_check_top(b);\n BN_CTX_start(ctx);\n A = BN_CTX_get(ctx);\n B = BN_CTX_get(ctx);\n if (B == NULL)\n goto end;\n err = !BN_copy(A, a);\n if (err)\n goto end;\n err = !BN_copy(B, b);\n if (err)\n goto end;\n if (BN_is_zero(B)) {\n ret = BN_abs_is_word(A, 1);\n goto end;\n }\n if (!BN_is_odd(A) && !BN_is_odd(B)) {\n ret = 0;\n goto end;\n }\n i = 0;\n while (!BN_is_bit_set(B, i))\n i++;\n err = !BN_rshift(B, B, i);\n if (err)\n goto end;\n if (i & 1) {\n ret = tab[BN_lsw(A) & 7];\n } else {\n ret = 1;\n }\n if (B->neg) {\n B->neg = 0;\n if (A->neg)\n ret = -ret;\n }\n while (1) {\n if (BN_is_zero(A)) {\n ret = BN_is_one(B) ? ret : 0;\n goto end;\n }\n i = 0;\n while (!BN_is_bit_set(A, i))\n i++;\n err = !BN_rshift(A, A, i);\n if (err)\n goto end;\n if (i & 1) {\n ret = ret * tab[BN_lsw(B) & 7];\n }\n if ((A->neg ? ~BN_lsw(A) : BN_lsw(A)) & BN_lsw(B) & 2)\n ret = -ret;\n err = !BN_nnmod(B, B, A, ctx);\n if (err)\n goto end;\n tmp = A;\n A = B;\n B = tmp;\n tmp->neg = 0;\n }\n end:\n BN_CTX_end(ctx);\n if (err)\n return -2;\n else\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_mod_exp_recp(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 *aa;\n BIGNUM *val[TABLE_SIZE];\n BN_RECP_CTX recp;\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_RECP, 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 aa = BN_CTX_get(ctx);\n val[0] = BN_CTX_get(ctx);\n if (val[0] == NULL)\n goto err;\n BN_RECP_CTX_init(&recp);\n if (m->neg) {\n if (!BN_copy(aa, m))\n goto err;\n aa->neg = 0;\n if (BN_RECP_CTX_set(&recp, aa, ctx) <= 0)\n goto err;\n } else {\n if (BN_RECP_CTX_set(&recp, m, ctx) <= 0)\n goto err;\n }\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_reciprocal(aa, val[0], val[0], &recp, 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_reciprocal(val[i], val[i - 1], aa, &recp, 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_reciprocal(r, r, r, &recp, 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_reciprocal(r, r, r, &recp, ctx))\n goto err;\n }\n if (!BN_mod_mul_reciprocal(r, r, val[wvalue >> 1], &recp, 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_RECP_CTX_free(&recp);\n bn_check_top(r);\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}', 'static unsigned int BN_STACK_pop(BN_STACK *st)\n{\n return st->indexes[--(st->depth)];\n}']
|
2,106
| 0
|
https://github.com/nginx/nginx/blob/88dc647481a9280fa6cedb0bed61a34123119b3c/src/core/ngx_inet.c/#L641
|
ngx_int_t
ngx_parse_addr_port(ngx_pool_t *pool, ngx_addr_t *addr, u_char *text,
size_t len)
{
u_char *p, *last;
size_t plen;
ngx_int_t rc, port;
rc = ngx_parse_addr(pool, addr, text, len);
if (rc != NGX_DECLINED) {
return rc;
}
last = text + len;
#if (NGX_HAVE_INET6)
if (len && text[0] == '[') {
p = ngx_strlchr(text, last, ']');
if (p == NULL || p == last - 1 || *++p != ':') {
return NGX_DECLINED;
}
text++;
len -= 2;
} else
#endif
{
p = ngx_strlchr(text, last, ':');
if (p == NULL) {
return NGX_DECLINED;
}
}
p++;
plen = last - p;
port = ngx_atoi(p, plen);
if (port < 1 || port > 65535) {
return NGX_DECLINED;
}
len -= plen + 1;
rc = ngx_parse_addr(pool, addr, text, len);
if (rc != NGX_OK) {
return rc;
}
ngx_inet_set_port(addr->sockaddr, (in_port_t) port);
return NGX_OK;
}
|
["static ngx_int_t\nngx_http_get_forwarded_addr_internal(ngx_http_request_t *r, ngx_addr_t *addr,\n u_char *xff, size_t xfflen, ngx_array_t *proxies, int recursive)\n{\n u_char *p;\n ngx_int_t rc;\n ngx_addr_t paddr;\n if (ngx_cidr_match(addr->sockaddr, proxies) != NGX_OK) {\n return NGX_DECLINED;\n }\n for (p = xff + xfflen - 1; p > xff; p--, xfflen--) {\n if (*p != ' ' && *p != ',') {\n break;\n }\n }\n for ( ; p > xff; p--) {\n if (*p == ' ' || *p == ',') {\n p++;\n break;\n }\n }\n if (ngx_parse_addr_port(r->pool, &paddr, p, xfflen - (p - xff)) != NGX_OK) {\n return NGX_DECLINED;\n }\n *addr = paddr;\n if (recursive && p > xff) {\n rc = ngx_http_get_forwarded_addr_internal(r, addr, xff, p - 1 - xff,\n proxies, 1);\n if (rc == NGX_DECLINED) {\n return NGX_DONE;\n }\n return rc;\n }\n return NGX_OK;\n}", "ngx_int_t\nngx_parse_addr_port(ngx_pool_t *pool, ngx_addr_t *addr, u_char *text,\n size_t len)\n{\n u_char *p, *last;\n size_t plen;\n ngx_int_t rc, port;\n rc = ngx_parse_addr(pool, addr, text, len);\n if (rc != NGX_DECLINED) {\n return rc;\n }\n last = text + len;\n#if (NGX_HAVE_INET6)\n if (len && text[0] == '[') {\n p = ngx_strlchr(text, last, ']');\n if (p == NULL || p == last - 1 || *++p != ':') {\n return NGX_DECLINED;\n }\n text++;\n len -= 2;\n } else\n#endif\n {\n p = ngx_strlchr(text, last, ':');\n if (p == NULL) {\n return NGX_DECLINED;\n }\n }\n p++;\n plen = last - p;\n port = ngx_atoi(p, plen);\n if (port < 1 || port > 65535) {\n return NGX_DECLINED;\n }\n len -= plen + 1;\n rc = ngx_parse_addr(pool, addr, text, len);\n if (rc != NGX_OK) {\n return rc;\n }\n ngx_inet_set_port(addr->sockaddr, (in_port_t) port);\n return NGX_OK;\n}"]
|
2,107
| 0
|
https://github.com/openssl/openssl/blob/260a16f33682a819414fcba6161708a5e6bdff50/crypto/lhash/lhash.c/#L200
|
static void doall_util_fn(OPENSSL_LHASH *lh, int use_arg,
OPENSSL_LH_DOALL_FUNC func,
OPENSSL_LH_DOALL_FUNCARG func_arg, void *arg)
{
int i;
OPENSSL_LH_NODE *a, *n;
if (lh == NULL)
return;
for (i = lh->num_nodes - 1; i >= 0; i--) {
a = lh->b[i];
while (a != NULL) {
n = a->next;
if (use_arg)
func_arg(a->data, arg);
else
func(a->data);
a = n;
}
}
}
|
['static int test_tlsext_status_type(void)\n{\n SSL_CTX *cctx = NULL, *sctx = NULL;\n SSL *clientssl = NULL, *serverssl = NULL;\n int testresult = 0;\n STACK_OF(OCSP_RESPID) *ids = NULL;\n OCSP_RESPID *id = NULL;\n BIO *certbio = NULL;\n if (!create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(),\n TLS1_VERSION, 0,\n &sctx, &cctx, cert, privkey))\n return 0;\n if (SSL_CTX_get_tlsext_status_type(cctx) != -1)\n goto end;\n clientssl = SSL_new(cctx);\n if (!TEST_int_eq(SSL_get_tlsext_status_type(clientssl), -1)\n || !TEST_true(SSL_set_tlsext_status_type(clientssl,\n TLSEXT_STATUSTYPE_ocsp))\n || !TEST_int_eq(SSL_get_tlsext_status_type(clientssl),\n TLSEXT_STATUSTYPE_ocsp))\n goto end;\n SSL_free(clientssl);\n clientssl = NULL;\n if (!SSL_CTX_set_tlsext_status_type(cctx, TLSEXT_STATUSTYPE_ocsp)\n || SSL_CTX_get_tlsext_status_type(cctx) != TLSEXT_STATUSTYPE_ocsp)\n goto end;\n clientssl = SSL_new(cctx);\n if (SSL_get_tlsext_status_type(clientssl) != TLSEXT_STATUSTYPE_ocsp)\n goto end;\n SSL_free(clientssl);\n clientssl = NULL;\n SSL_CTX_set_tlsext_status_cb(cctx, ocsp_client_cb);\n SSL_CTX_set_tlsext_status_arg(cctx, &cdummyarg);\n SSL_CTX_set_tlsext_status_cb(sctx, ocsp_server_cb);\n SSL_CTX_set_tlsext_status_arg(sctx, &cdummyarg);\n if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,\n &clientssl, NULL, NULL))\n || !TEST_true(create_ssl_connection(serverssl, clientssl,\n SSL_ERROR_NONE))\n || !TEST_true(ocsp_client_called)\n || !TEST_true(ocsp_server_called))\n goto end;\n SSL_free(serverssl);\n SSL_free(clientssl);\n serverssl = NULL;\n clientssl = NULL;\n ocsp_client_called = 0;\n ocsp_server_called = 0;\n cdummyarg = 0;\n if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,\n &clientssl, NULL, NULL))\n || !TEST_false(create_ssl_connection(serverssl, clientssl,\n SSL_ERROR_NONE))\n || !TEST_false(ocsp_client_called)\n || !TEST_false(ocsp_server_called))\n goto end;\n SSL_free(serverssl);\n SSL_free(clientssl);\n serverssl = NULL;\n clientssl = NULL;\n ocsp_client_called = 0;\n ocsp_server_called = 0;\n cdummyarg = 2;\n if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,\n &clientssl, NULL, NULL)))\n goto end;\n if (!TEST_ptr(certbio = BIO_new_file(cert, "r"))\n || !TEST_ptr(id = OCSP_RESPID_new())\n || !TEST_ptr(ids = sk_OCSP_RESPID_new_null())\n || !TEST_ptr(ocspcert = PEM_read_bio_X509(certbio,\n NULL, NULL, NULL))\n || !TEST_true(OCSP_RESPID_set_by_key(id, ocspcert))\n || !TEST_true(sk_OCSP_RESPID_push(ids, id)))\n goto end;\n id = NULL;\n SSL_set_tlsext_status_ids(clientssl, ids);\n ids = NULL;\n BIO_free(certbio);\n certbio = NULL;\n if (!TEST_true(create_ssl_connection(serverssl, clientssl,\n SSL_ERROR_NONE))\n || !TEST_true(ocsp_client_called)\n || !TEST_true(ocsp_server_called))\n goto end;\n testresult = 1;\n end:\n SSL_free(serverssl);\n SSL_free(clientssl);\n SSL_CTX_free(sctx);\n SSL_CTX_free(cctx);\n sk_OCSP_RESPID_pop_free(ids, OCSP_RESPID_free);\n OCSP_RESPID_free(id);\n BIO_free(certbio);\n X509_free(ocspcert);\n ocspcert = NULL;\n return testresult;\n}', 'SSL *SSL_new(SSL_CTX *ctx)\n{\n SSL *s;\n if (ctx == NULL) {\n SSLerr(SSL_F_SSL_NEW, SSL_R_NULL_SSL_CTX);\n return NULL;\n }\n if (ctx->method == NULL) {\n SSLerr(SSL_F_SSL_NEW, SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);\n return NULL;\n }\n s = OPENSSL_zalloc(sizeof(*s));\n if (s == NULL)\n goto err;\n s->references = 1;\n s->lock = CRYPTO_THREAD_lock_new();\n if (s->lock == NULL) {\n OPENSSL_free(s);\n s = NULL;\n goto err;\n }\n RECORD_LAYER_init(&s->rlayer, s);\n s->options = ctx->options;\n s->dane.flags = ctx->dane.flags;\n s->min_proto_version = ctx->min_proto_version;\n s->max_proto_version = ctx->max_proto_version;\n s->mode = ctx->mode;\n s->max_cert_list = ctx->max_cert_list;\n s->max_early_data = ctx->max_early_data;\n s->recv_max_early_data = ctx->recv_max_early_data;\n s->num_tickets = ctx->num_tickets;\n s->pha_enabled = ctx->pha_enabled;\n s->tls13_ciphersuites = sk_SSL_CIPHER_dup(ctx->tls13_ciphersuites);\n if (s->tls13_ciphersuites == NULL)\n goto err;\n s->cert = ssl_cert_dup(ctx->cert);\n if (s->cert == NULL)\n goto err;\n RECORD_LAYER_set_read_ahead(&s->rlayer, ctx->read_ahead);\n s->msg_callback = ctx->msg_callback;\n s->msg_callback_arg = ctx->msg_callback_arg;\n s->verify_mode = ctx->verify_mode;\n s->not_resumable_session_cb = ctx->not_resumable_session_cb;\n s->record_padding_cb = ctx->record_padding_cb;\n s->record_padding_arg = ctx->record_padding_arg;\n s->block_padding = ctx->block_padding;\n s->sid_ctx_length = ctx->sid_ctx_length;\n if (!ossl_assert(s->sid_ctx_length <= sizeof(s->sid_ctx)))\n goto err;\n memcpy(&s->sid_ctx, &ctx->sid_ctx, sizeof(s->sid_ctx));\n s->verify_callback = ctx->default_verify_callback;\n s->generate_session_id = ctx->generate_session_id;\n s->param = X509_VERIFY_PARAM_new();\n if (s->param == NULL)\n goto err;\n X509_VERIFY_PARAM_inherit(s->param, ctx->param);\n s->quiet_shutdown = ctx->quiet_shutdown;\n s->ext.max_fragment_len_mode = ctx->ext.max_fragment_len_mode;\n s->max_send_fragment = ctx->max_send_fragment;\n s->split_send_fragment = ctx->split_send_fragment;\n s->max_pipelines = ctx->max_pipelines;\n if (s->max_pipelines > 1)\n RECORD_LAYER_set_read_ahead(&s->rlayer, 1);\n if (ctx->default_read_buf_len > 0)\n SSL_set_default_read_buffer_len(s, ctx->default_read_buf_len);\n SSL_CTX_up_ref(ctx);\n s->ctx = ctx;\n s->ext.debug_cb = 0;\n s->ext.debug_arg = NULL;\n s->ext.ticket_expected = 0;\n s->ext.status_type = ctx->ext.status_type;\n s->ext.status_expected = 0;\n s->ext.ocsp.ids = NULL;\n s->ext.ocsp.exts = NULL;\n s->ext.ocsp.resp = NULL;\n s->ext.ocsp.resp_len = 0;\n SSL_CTX_up_ref(ctx);\n s->session_ctx = ctx;\n#ifndef OPENSSL_NO_EC\n if (ctx->ext.ecpointformats) {\n s->ext.ecpointformats =\n OPENSSL_memdup(ctx->ext.ecpointformats,\n ctx->ext.ecpointformats_len);\n if (!s->ext.ecpointformats)\n goto err;\n s->ext.ecpointformats_len =\n ctx->ext.ecpointformats_len;\n }\n if (ctx->ext.supportedgroups) {\n s->ext.supportedgroups =\n OPENSSL_memdup(ctx->ext.supportedgroups,\n ctx->ext.supportedgroups_len\n * sizeof(*ctx->ext.supportedgroups));\n if (!s->ext.supportedgroups)\n goto err;\n s->ext.supportedgroups_len = ctx->ext.supportedgroups_len;\n }\n#endif\n#ifndef OPENSSL_NO_NEXTPROTONEG\n s->ext.npn = NULL;\n#endif\n if (s->ctx->ext.alpn) {\n s->ext.alpn = OPENSSL_malloc(s->ctx->ext.alpn_len);\n if (s->ext.alpn == NULL)\n goto err;\n memcpy(s->ext.alpn, s->ctx->ext.alpn, s->ctx->ext.alpn_len);\n s->ext.alpn_len = s->ctx->ext.alpn_len;\n }\n s->verified_chain = NULL;\n s->verify_result = X509_V_OK;\n s->default_passwd_callback = ctx->default_passwd_callback;\n s->default_passwd_callback_userdata = ctx->default_passwd_callback_userdata;\n s->method = ctx->method;\n s->key_update = SSL_KEY_UPDATE_NONE;\n s->allow_early_data_cb = ctx->allow_early_data_cb;\n s->allow_early_data_cb_data = ctx->allow_early_data_cb_data;\n if (!s->method->ssl_new(s))\n goto err;\n s->server = (ctx->method->ssl_accept == ssl_undefined_function) ? 0 : 1;\n if (!SSL_clear(s))\n goto err;\n if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data))\n goto err;\n#ifndef OPENSSL_NO_PSK\n s->psk_client_callback = ctx->psk_client_callback;\n s->psk_server_callback = ctx->psk_server_callback;\n#endif\n s->psk_find_session_cb = ctx->psk_find_session_cb;\n s->psk_use_session_cb = ctx->psk_use_session_cb;\n s->async_cb = ctx->async_cb;\n s->async_cb_arg = ctx->async_cb_arg;\n s->job = NULL;\n#ifndef OPENSSL_NO_CT\n if (!SSL_set_ct_validation_callback(s, ctx->ct_validation_callback,\n ctx->ct_validation_callback_arg))\n goto err;\n#endif\n return s;\n err:\n SSL_free(s);\n SSLerr(SSL_F_SSL_NEW, ERR_R_MALLOC_FAILURE);\n return NULL;\n}', 'void SSL_free(SSL *s)\n{\n int i;\n if (s == NULL)\n return;\n CRYPTO_DOWN_REF(&s->references, &i, s->lock);\n REF_PRINT_COUNT("SSL", s);\n if (i > 0)\n return;\n REF_ASSERT_ISNT(i < 0);\n X509_VERIFY_PARAM_free(s->param);\n dane_final(&s->dane);\n CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data);\n RECORD_LAYER_release(&s->rlayer);\n ssl_free_wbio_buffer(s);\n BIO_free_all(s->wbio);\n s->wbio = NULL;\n BIO_free_all(s->rbio);\n s->rbio = NULL;\n BUF_MEM_free(s->init_buf);\n sk_SSL_CIPHER_free(s->cipher_list);\n sk_SSL_CIPHER_free(s->cipher_list_by_id);\n sk_SSL_CIPHER_free(s->tls13_ciphersuites);\n if (s->session != NULL) {\n ssl_clear_bad_session(s);\n SSL_SESSION_free(s->session);\n }\n SSL_SESSION_free(s->psksession);\n OPENSSL_free(s->psksession_id);\n clear_ciphers(s);\n ssl_cert_free(s->cert);\n OPENSSL_free(s->ext.hostname);\n SSL_CTX_free(s->session_ctx);\n#ifndef OPENSSL_NO_EC\n OPENSSL_free(s->ext.ecpointformats);\n OPENSSL_free(s->ext.supportedgroups);\n#endif\n sk_X509_EXTENSION_pop_free(s->ext.ocsp.exts, X509_EXTENSION_free);\n#ifndef OPENSSL_NO_OCSP\n sk_OCSP_RESPID_pop_free(s->ext.ocsp.ids, OCSP_RESPID_free);\n#endif\n#ifndef OPENSSL_NO_CT\n SCT_LIST_free(s->scts);\n OPENSSL_free(s->ext.scts);\n#endif\n OPENSSL_free(s->ext.ocsp.resp);\n OPENSSL_free(s->ext.alpn);\n OPENSSL_free(s->ext.tls13_cookie);\n OPENSSL_free(s->clienthello);\n OPENSSL_free(s->pha_context);\n EVP_MD_CTX_free(s->pha_dgst);\n sk_X509_NAME_pop_free(s->ca_names, X509_NAME_free);\n sk_X509_NAME_pop_free(s->client_ca_names, X509_NAME_free);\n sk_X509_pop_free(s->verified_chain, X509_free);\n if (s->method != NULL)\n s->method->ssl_free(s);\n SSL_CTX_free(s->ctx);\n ASYNC_WAIT_CTX_free(s->waitctx);\n#if !defined(OPENSSL_NO_NEXTPROTONEG)\n OPENSSL_free(s->ext.npn);\n#endif\n#ifndef OPENSSL_NO_SRTP\n sk_SRTP_PROTECTION_PROFILE_free(s->srtp_profiles);\n#endif\n CRYPTO_THREAD_lock_free(s->lock);\n OPENSSL_free(s);\n}', 'void SSL_CTX_free(SSL_CTX *a)\n{\n int i;\n if (a == NULL)\n return;\n CRYPTO_DOWN_REF(&a->references, &i, a->lock);\n REF_PRINT_COUNT("SSL_CTX", a);\n if (i > 0)\n return;\n REF_ASSERT_ISNT(i < 0);\n X509_VERIFY_PARAM_free(a->param);\n dane_ctx_final(&a->dane);\n if (a->sessions != NULL)\n SSL_CTX_flush_sessions(a, 0);\n CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_CTX, a, &a->ex_data);\n lh_SSL_SESSION_free(a->sessions);\n X509_STORE_free(a->cert_store);\n#ifndef OPENSSL_NO_CT\n CTLOG_STORE_free(a->ctlog_store);\n#endif\n sk_SSL_CIPHER_free(a->cipher_list);\n sk_SSL_CIPHER_free(a->cipher_list_by_id);\n sk_SSL_CIPHER_free(a->tls13_ciphersuites);\n ssl_cert_free(a->cert);\n sk_X509_NAME_pop_free(a->ca_names, X509_NAME_free);\n sk_X509_NAME_pop_free(a->client_ca_names, X509_NAME_free);\n sk_X509_pop_free(a->extra_certs, X509_free);\n a->comp_methods = NULL;\n#ifndef OPENSSL_NO_SRTP\n sk_SRTP_PROTECTION_PROFILE_free(a->srtp_profiles);\n#endif\n#ifndef OPENSSL_NO_SRP\n SSL_CTX_SRP_CTX_free(a);\n#endif\n#ifndef OPENSSL_NO_ENGINE\n ENGINE_finish(a->client_cert_engine);\n#endif\n#ifndef OPENSSL_NO_EC\n OPENSSL_free(a->ext.ecpointformats);\n OPENSSL_free(a->ext.supportedgroups);\n#endif\n OPENSSL_free(a->ext.alpn);\n OPENSSL_secure_free(a->ext.secure);\n CRYPTO_THREAD_lock_free(a->lock);\n OPENSSL_free(a);\n}', 'void SSL_CTX_flush_sessions(SSL_CTX *s, long t)\n{\n unsigned long i;\n TIMEOUT_PARAM tp;\n tp.ctx = s;\n tp.cache = s->sessions;\n if (tp.cache == NULL)\n return;\n tp.time = t;\n CRYPTO_THREAD_write_lock(s->lock);\n i = lh_SSL_SESSION_get_down_load(s->sessions);\n lh_SSL_SESSION_set_down_load(s->sessions, 0);\n lh_SSL_SESSION_doall_TIMEOUT_PARAM(tp.cache, timeout_cb, &tp);\n lh_SSL_SESSION_set_down_load(s->sessions, i);\n CRYPTO_THREAD_unlock(s->lock);\n}', 'IMPLEMENT_LHASH_DOALL_ARG(SSL_SESSION, TIMEOUT_PARAM)', 'void OPENSSL_LH_doall_arg(OPENSSL_LHASH *lh, OPENSSL_LH_DOALL_FUNCARG func, void *arg)\n{\n doall_util_fn(lh, 1, (OPENSSL_LH_DOALL_FUNC)0, func, arg);\n}', 'static void doall_util_fn(OPENSSL_LHASH *lh, int use_arg,\n OPENSSL_LH_DOALL_FUNC func,\n OPENSSL_LH_DOALL_FUNCARG func_arg, void *arg)\n{\n int i;\n OPENSSL_LH_NODE *a, *n;\n if (lh == NULL)\n return;\n for (i = lh->num_nodes - 1; i >= 0; i--) {\n a = lh->b[i];\n while (a != NULL) {\n n = a->next;\n if (use_arg)\n func_arg(a->data, arg);\n else\n func(a->data);\n a = n;\n }\n }\n}']
|
2,108
| 0
|
https://github.com/openssl/openssl/blob/fe0169b09717b3c3d52c0fba96e1dcf5e8a60d94/crypto/asn1/asn1_lib.c/#L124
|
static int asn1_get_length(const unsigned char **pp, int *inf, long *rl,
long max)
{
const unsigned char *p = *pp;
unsigned long ret = 0;
unsigned long i;
if (max-- < 1)
return 0;
if (*p == 0x80) {
*inf = 1;
ret = 0;
p++;
} else {
*inf = 0;
i = *p & 0x7f;
if (*(p++) & 0x80) {
if (max < (long)i + 1)
return 0;
while (i && *p == 0) {
p++;
i--;
}
if (i > sizeof(long))
return 0;
while (i-- > 0) {
ret <<= 8L;
ret |= *(p++);
}
} else
ret = i;
}
if (ret > LONG_MAX)
return 0;
*pp = p;
*rl = (long)ret;
return 1;
}
|
['static int ctlog_new_from_conf(CTLOG **ct_log, const CONF *conf, const char *section)\n{\n const char *description = NCONF_get_string(conf, section, "description");\n char *pkey_base64;\n if (description == NULL) {\n CTerr(CT_F_CTLOG_NEW_FROM_CONF, CT_R_LOG_CONF_MISSING_DESCRIPTION);\n return 0;\n }\n pkey_base64 = NCONF_get_string(conf, section, "key");\n if (pkey_base64 == NULL) {\n CTerr(CT_F_CTLOG_NEW_FROM_CONF, CT_R_LOG_CONF_MISSING_KEY);\n return 0;\n }\n return CTLOG_new_from_base64(ct_log, pkey_base64, description);\n}', 'int CTLOG_new_from_base64(CTLOG **ct_log, const char *pkey_base64, const char *name)\n{\n unsigned char *pkey_der = NULL;\n int pkey_der_len = ct_base64_decode(pkey_base64, &pkey_der);\n const unsigned char *p;\n EVP_PKEY *pkey = NULL;\n if (ct_log == NULL) {\n CTerr(CT_F_CTLOG_NEW_FROM_BASE64, ERR_R_PASSED_INVALID_ARGUMENT);\n return 0;\n }\n if (pkey_der_len <= 0) {\n CTerr(CT_F_CTLOG_NEW_FROM_BASE64, CT_R_LOG_CONF_INVALID_KEY);\n return 0;\n }\n p = pkey_der;\n pkey = d2i_PUBKEY(NULL, &p, pkey_der_len);\n OPENSSL_free(pkey_der);\n if (pkey == NULL) {\n CTerr(CT_F_CTLOG_NEW_FROM_BASE64, CT_R_LOG_CONF_INVALID_KEY);\n return 0;\n }\n *ct_log = CTLOG_new(pkey, name);\n if (*ct_log == NULL) {\n EVP_PKEY_free(pkey);\n return -1;\n }\n return 1;\n}', 'static int ct_base64_decode(const char *in, unsigned char **out)\n{\n size_t inlen = strlen(in);\n int outlen;\n unsigned char *outbuf = NULL;\n if (inlen == 0) {\n *out = NULL;\n return 0;\n }\n outlen = (inlen / 4) * 3;\n outbuf = OPENSSL_malloc(outlen);\n if (outbuf == NULL) {\n CTerr(CT_F_CT_BASE64_DECODE, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n outlen = EVP_DecodeBlock(outbuf, (unsigned char *)in, inlen);\n if (outlen < 0) {\n CTerr(CT_F_CT_BASE64_DECODE, CT_R_BASE64_DECODE_ERROR);\n goto err;\n }\n *out = outbuf;\n return outlen;\nerr:\n OPENSSL_free(outbuf);\n return -1;\n}', 'EVP_PKEY *d2i_PUBKEY(EVP_PKEY **a, const unsigned char **pp, long length)\n{\n X509_PUBKEY *xpk;\n EVP_PKEY *pktmp;\n const unsigned char *q;\n q = *pp;\n xpk = d2i_X509_PUBKEY(NULL, &q, length);\n if (!xpk)\n return NULL;\n pktmp = X509_PUBKEY_get(xpk);\n X509_PUBKEY_free(xpk);\n if (!pktmp)\n return NULL;\n *pp = q;\n if (a) {\n EVP_PKEY_free(*a);\n *a = pktmp;\n }\n return pktmp;\n}', 'IMPLEMENT_ASN1_FUNCTIONS(X509_PUBKEY)', 'ASN1_VALUE *ASN1_item_d2i(ASN1_VALUE **pval,\n const unsigned char **in, long len,\n const ASN1_ITEM *it)\n{\n ASN1_TLC c;\n ASN1_VALUE *ptmpval = NULL;\n if (!pval)\n pval = &ptmpval;\n asn1_tlc_clear_nc(&c);\n if (ASN1_item_ex_d2i(pval, in, len, it, -1, 0, 0, &c) > 0)\n return *pval;\n return NULL;\n}', 'int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,\n const ASN1_ITEM *it,\n int tag, int aclass, char opt, ASN1_TLC *ctx)\n{\n int rv;\n rv = asn1_item_embed_d2i(pval, in, len, it, tag, aclass, opt, ctx);\n if (rv <= 0)\n ASN1_item_ex_free(pval, it);\n return rv;\n}', 'static int asn1_item_embed_d2i(ASN1_VALUE **pval, const unsigned char **in,\n long len, const ASN1_ITEM *it,\n int tag, int aclass, char opt, ASN1_TLC *ctx)\n{\n const ASN1_TEMPLATE *tt, *errtt = NULL;\n const ASN1_EXTERN_FUNCS *ef;\n const ASN1_AUX *aux = it->funcs;\n ASN1_aux_cb *asn1_cb;\n const unsigned char *p = NULL, *q;\n unsigned char oclass;\n char seq_eoc, seq_nolen, cst, isopt;\n long tmplen;\n int i;\n int otag;\n int ret = 0;\n ASN1_VALUE **pchptr;\n if (!pval)\n return 0;\n if (aux && aux->asn1_cb)\n asn1_cb = aux->asn1_cb;\n else\n asn1_cb = 0;\n switch (it->itype) {\n case ASN1_ITYPE_PRIMITIVE:\n if (it->templates) {\n if ((tag != -1) || opt) {\n ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I,\n ASN1_R_ILLEGAL_OPTIONS_ON_ITEM_TEMPLATE);\n goto err;\n }\n return asn1_template_ex_d2i(pval, in, len,\n it->templates, opt, ctx);\n }\n return asn1_d2i_ex_primitive(pval, in, len, it,\n tag, aclass, opt, ctx);\n case ASN1_ITYPE_MSTRING:\n p = *in;\n ret = asn1_check_tlen(NULL, &otag, &oclass, NULL, NULL,\n &p, len, -1, 0, 1, ctx);\n if (!ret) {\n ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ERR_R_NESTED_ASN1_ERROR);\n goto err;\n }\n if (oclass != V_ASN1_UNIVERSAL) {\n if (opt)\n return -1;\n ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_MSTRING_NOT_UNIVERSAL);\n goto err;\n }\n if (!(ASN1_tag2bit(otag) & it->utype)) {\n if (opt)\n return -1;\n ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_MSTRING_WRONG_TAG);\n goto err;\n }\n return asn1_d2i_ex_primitive(pval, in, len, it, otag, 0, 0, ctx);\n case ASN1_ITYPE_EXTERN:\n ef = it->funcs;\n return ef->asn1_ex_d2i(pval, in, len, it, tag, aclass, opt, ctx);\n case ASN1_ITYPE_CHOICE:\n if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL))\n goto auxerr;\n if (*pval) {\n i = asn1_get_choice_selector(pval, it);\n if ((i >= 0) && (i < it->tcount)) {\n tt = it->templates + i;\n pchptr = asn1_get_field_ptr(pval, tt);\n asn1_template_free(pchptr, tt);\n asn1_set_choice_selector(pval, -1, it);\n }\n } else if (!ASN1_item_ex_new(pval, it)) {\n ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ERR_R_NESTED_ASN1_ERROR);\n goto err;\n }\n p = *in;\n for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {\n pchptr = asn1_get_field_ptr(pval, tt);\n ret = asn1_template_ex_d2i(pchptr, &p, len, tt, 1, ctx);\n if (ret == -1)\n continue;\n asn1_set_choice_selector(pval, i, it);\n if (ret > 0)\n break;\n errtt = tt;\n ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ERR_R_NESTED_ASN1_ERROR);\n goto err;\n }\n if (i == it->tcount) {\n if (opt) {\n ASN1_item_ex_free(pval, it);\n return -1;\n }\n ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_NO_MATCHING_CHOICE_TYPE);\n goto err;\n }\n if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL))\n goto auxerr;\n *in = p;\n return 1;\n case ASN1_ITYPE_NDEF_SEQUENCE:\n case ASN1_ITYPE_SEQUENCE:\n p = *in;\n tmplen = len;\n if (tag == -1) {\n tag = V_ASN1_SEQUENCE;\n aclass = V_ASN1_UNIVERSAL;\n }\n ret = asn1_check_tlen(&len, NULL, NULL, &seq_eoc, &cst,\n &p, len, tag, aclass, opt, ctx);\n if (!ret) {\n ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ERR_R_NESTED_ASN1_ERROR);\n goto err;\n } else if (ret == -1)\n return -1;\n if (aux && (aux->flags & ASN1_AFLG_BROKEN)) {\n len = tmplen - (p - *in);\n seq_nolen = 1;\n }\n else\n seq_nolen = seq_eoc;\n if (!cst) {\n ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_SEQUENCE_NOT_CONSTRUCTED);\n goto err;\n }\n if (!*pval && !ASN1_item_ex_new(pval, it)) {\n ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ERR_R_NESTED_ASN1_ERROR);\n goto err;\n }\n if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL))\n goto auxerr;\n for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {\n if (tt->flags & ASN1_TFLG_ADB_MASK) {\n const ASN1_TEMPLATE *seqtt;\n ASN1_VALUE **pseqval;\n seqtt = asn1_do_adb(pval, tt, 0);\n if (seqtt == NULL)\n continue;\n pseqval = asn1_get_field_ptr(pval, seqtt);\n asn1_template_free(pseqval, seqtt);\n }\n }\n for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {\n const ASN1_TEMPLATE *seqtt;\n ASN1_VALUE **pseqval;\n seqtt = asn1_do_adb(pval, tt, 1);\n if (seqtt == NULL)\n goto err;\n pseqval = asn1_get_field_ptr(pval, seqtt);\n if (!len)\n break;\n q = p;\n if (asn1_check_eoc(&p, len)) {\n if (!seq_eoc) {\n ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_UNEXPECTED_EOC);\n goto err;\n }\n len -= p - q;\n seq_eoc = 0;\n q = p;\n break;\n }\n if (i == (it->tcount - 1))\n isopt = 0;\n else\n isopt = (char)(seqtt->flags & ASN1_TFLG_OPTIONAL);\n ret = asn1_template_ex_d2i(pseqval, &p, len, seqtt, isopt, ctx);\n if (!ret) {\n errtt = seqtt;\n goto err;\n } else if (ret == -1) {\n asn1_template_free(pseqval, seqtt);\n continue;\n }\n len -= p - q;\n }\n if (seq_eoc && !asn1_check_eoc(&p, len)) {\n ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_MISSING_EOC);\n goto err;\n }\n if (!seq_nolen && len) {\n ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_SEQUENCE_LENGTH_MISMATCH);\n goto err;\n }\n for (; i < it->tcount; tt++, i++) {\n const ASN1_TEMPLATE *seqtt;\n seqtt = asn1_do_adb(pval, tt, 1);\n if (seqtt == NULL)\n goto err;\n if (seqtt->flags & ASN1_TFLG_OPTIONAL) {\n ASN1_VALUE **pseqval;\n pseqval = asn1_get_field_ptr(pval, seqtt);\n asn1_template_free(pseqval, seqtt);\n } else {\n errtt = seqtt;\n ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_FIELD_MISSING);\n goto err;\n }\n }\n if (!asn1_enc_save(pval, *in, p - *in, it))\n goto auxerr;\n if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL))\n goto auxerr;\n *in = p;\n return 1;\n default:\n return 0;\n }\n auxerr:\n ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_AUX_ERROR);\n err:\n if (errtt)\n ERR_add_error_data(4, "Field=", errtt->field_name,\n ", Type=", it->sname);\n else\n ERR_add_error_data(2, "Type=", it->sname);\n return 0;\n}', 'static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass,\n char *inf, char *cst,\n const unsigned char **in, long len,\n int exptag, int expclass, char opt, ASN1_TLC *ctx)\n{\n int i;\n int ptag, pclass;\n long plen;\n const unsigned char *p, *q;\n p = *in;\n q = p;\n if (ctx && ctx->valid) {\n i = ctx->ret;\n plen = ctx->plen;\n pclass = ctx->pclass;\n ptag = ctx->ptag;\n p += ctx->hdrlen;\n } else {\n i = ASN1_get_object(&p, &plen, &ptag, &pclass, len);\n if (ctx) {\n ctx->ret = i;\n ctx->plen = plen;\n ctx->pclass = pclass;\n ctx->ptag = ptag;\n ctx->hdrlen = p - q;\n ctx->valid = 1;\n if (!(i & 0x81) && ((plen + ctx->hdrlen) > len)) {\n ASN1err(ASN1_F_ASN1_CHECK_TLEN, ASN1_R_TOO_LONG);\n asn1_tlc_clear(ctx);\n return 0;\n }\n }\n }\n if (i & 0x80) {\n ASN1err(ASN1_F_ASN1_CHECK_TLEN, ASN1_R_BAD_OBJECT_HEADER);\n asn1_tlc_clear(ctx);\n return 0;\n }\n if (exptag >= 0) {\n if ((exptag != ptag) || (expclass != pclass)) {\n if (opt)\n return -1;\n asn1_tlc_clear(ctx);\n ASN1err(ASN1_F_ASN1_CHECK_TLEN, ASN1_R_WRONG_TAG);\n return 0;\n }\n asn1_tlc_clear(ctx);\n }\n if (i & 1)\n plen = len - (p - q);\n if (inf)\n *inf = i & 1;\n if (cst)\n *cst = i & V_ASN1_CONSTRUCTED;\n if (olen)\n *olen = plen;\n if (oclass)\n *oclass = pclass;\n if (otag)\n *otag = ptag;\n *in = p;\n return 1;\n}', 'int ASN1_get_object(const unsigned char **pp, long *plength, int *ptag,\n int *pclass, long omax)\n{\n int i, ret;\n long l;\n const unsigned char *p = *pp;\n int tag, xclass, inf;\n long max = omax;\n if (!max)\n goto err;\n ret = (*p & V_ASN1_CONSTRUCTED);\n xclass = (*p & V_ASN1_PRIVATE);\n i = *p & V_ASN1_PRIMITIVE_TAG;\n if (i == V_ASN1_PRIMITIVE_TAG) {\n p++;\n if (--max == 0)\n goto err;\n l = 0;\n while (*p & 0x80) {\n l <<= 7L;\n l |= *(p++) & 0x7f;\n if (--max == 0)\n goto err;\n if (l > (INT_MAX >> 7L))\n goto err;\n }\n l <<= 7L;\n l |= *(p++) & 0x7f;\n tag = (int)l;\n if (--max == 0)\n goto err;\n } else {\n tag = i;\n p++;\n if (--max == 0)\n goto err;\n }\n *ptag = tag;\n *pclass = xclass;\n if (!asn1_get_length(&p, &inf, plength, max))\n goto err;\n if (inf && !(ret & V_ASN1_CONSTRUCTED))\n goto err;\n if (*plength > (omax - (p - *pp))) {\n ASN1err(ASN1_F_ASN1_GET_OBJECT, ASN1_R_TOO_LONG);\n ret |= 0x80;\n }\n *pp = p;\n return (ret | inf);\n err:\n ASN1err(ASN1_F_ASN1_GET_OBJECT, ASN1_R_HEADER_TOO_LONG);\n return (0x80);\n}', 'static int asn1_get_length(const unsigned char **pp, int *inf, long *rl,\n long max)\n{\n const unsigned char *p = *pp;\n unsigned long ret = 0;\n unsigned long i;\n if (max-- < 1)\n return 0;\n if (*p == 0x80) {\n *inf = 1;\n ret = 0;\n p++;\n } else {\n *inf = 0;\n i = *p & 0x7f;\n if (*(p++) & 0x80) {\n if (max < (long)i + 1)\n return 0;\n while (i && *p == 0) {\n p++;\n i--;\n }\n if (i > sizeof(long))\n return 0;\n while (i-- > 0) {\n ret <<= 8L;\n ret |= *(p++);\n }\n } else\n ret = i;\n }\n if (ret > LONG_MAX)\n return 0;\n *pp = p;\n *rl = (long)ret;\n return 1;\n}']
|
2,109
| 0
|
https://github.com/openssl/openssl/blob/9b340281873643d2b8a33047dc8bfa607f7e0c3c/crypto/lhash/lhash.c/#L191
|
static void doall_util_fn(OPENSSL_LHASH *lh, int use_arg,
OPENSSL_LH_DOALL_FUNC func,
OPENSSL_LH_DOALL_FUNCARG func_arg, void *arg)
{
int i;
OPENSSL_LH_NODE *a, *n;
if (lh == NULL)
return;
for (i = lh->num_nodes - 1; i >= 0; i--) {
a = lh->b[i];
while (a != NULL) {
n = a->next;
if (use_arg)
func_arg(a->data, arg);
else
func(a->data);
a = n;
}
}
}
|
['static int test_early_data_read_write(int idx)\n{\n SSL_CTX *cctx = NULL, *sctx = NULL;\n SSL *clientssl = NULL, *serverssl = NULL;\n int testresult = 0;\n SSL_SESSION *sess = NULL;\n unsigned char buf[20], data[1024];\n size_t readbytes, written, eoedlen, rawread, rawwritten;\n BIO *rbio;\n if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,\n &serverssl, &sess, idx)))\n goto end;\n if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),\n &written))\n || !TEST_size_t_eq(written, strlen(MSG1))\n || !TEST_int_eq(SSL_read_early_data(serverssl, buf,\n sizeof(buf), &readbytes),\n SSL_READ_EARLY_DATA_SUCCESS)\n || !TEST_mem_eq(MSG1, readbytes, buf, strlen(MSG1))\n || !TEST_int_eq(SSL_get_early_data_status(serverssl),\n SSL_EARLY_DATA_ACCEPTED))\n goto end;\n if (!TEST_true(SSL_write_early_data(serverssl, MSG2, strlen(MSG2),\n &written))\n || !TEST_size_t_eq(written, strlen(MSG2))\n || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes))\n || !TEST_mem_eq(buf, readbytes, MSG2, strlen(MSG2)))\n goto end;\n if (!TEST_true(SSL_write_early_data(clientssl, MSG3, strlen(MSG3),\n &written))\n || !TEST_size_t_eq(written, strlen(MSG3)))\n goto end;\n if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),\n &readbytes),\n SSL_READ_EARLY_DATA_SUCCESS)\n || !TEST_mem_eq(buf, readbytes, MSG3, strlen(MSG3)))\n goto end;\n if (!TEST_true(SSL_write_early_data(serverssl, MSG4, strlen(MSG4),\n &written))\n || !TEST_size_t_eq(written, strlen(MSG4))\n || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes))\n || !TEST_mem_eq(buf, readbytes, MSG4, strlen(MSG4)))\n goto end;\n if (!TEST_true(SSL_write_ex(clientssl, MSG5, strlen(MSG5), &written))\n || !TEST_size_t_eq(written, strlen(MSG5))\n || !TEST_int_eq(SSL_get_early_data_status(clientssl),\n SSL_EARLY_DATA_ACCEPTED))\n goto end;\n rbio = SSL_get_rbio(serverssl);\n if (!TEST_true(BIO_read_ex(rbio, data, sizeof(data), &rawread))\n || !TEST_size_t_lt(rawread, sizeof(data))\n || !TEST_size_t_gt(rawread, SSL3_RT_HEADER_LENGTH))\n goto end;\n eoedlen = SSL3_RT_HEADER_LENGTH + (data[3] << 8 | data[4]);\n if (!TEST_true(BIO_write_ex(rbio, data, eoedlen, &rawwritten))\n || !TEST_size_t_eq(rawwritten, eoedlen))\n goto end;\n if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),\n &readbytes),\n SSL_READ_EARLY_DATA_FINISH)\n || !TEST_size_t_eq(readbytes, 0))\n goto end;\n if (!TEST_true(SSL_write_early_data(serverssl, MSG6, strlen(MSG6),\n &written))\n || !TEST_size_t_eq(written, strlen(MSG6)))\n goto end;\n if (!TEST_true(BIO_write_ex(rbio, data + eoedlen, rawread - eoedlen,\n &rawwritten))\n || !TEST_size_t_eq(rawwritten, rawread - eoedlen))\n goto end;\n if (!TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))\n || !TEST_size_t_eq(readbytes, strlen(MSG5)))\n goto end;\n if (!TEST_false(SSL_write_early_data(clientssl, MSG6, strlen(MSG6),\n &written)))\n goto end;\n ERR_clear_error();\n if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),\n &readbytes),\n SSL_READ_EARLY_DATA_ERROR))\n goto end;\n ERR_clear_error();\n if (!TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes))\n || !TEST_mem_eq(buf, readbytes, MSG6, strlen(MSG6)))\n goto end;\n if (!TEST_false(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes))\n || !TEST_false(SSL_read_ex(clientssl, buf, sizeof(buf),\n &readbytes)))\n goto end;\n if (!TEST_true(SSL_write_ex(serverssl, MSG7, strlen(MSG7), &written))\n || !TEST_size_t_eq(written, strlen(MSG7))\n || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes))\n || !TEST_mem_eq(buf, readbytes, MSG7, strlen(MSG7)))\n goto end;\n SSL_SESSION_free(sess);\n sess = SSL_get1_session(clientssl);\n use_session_cb_cnt = 0;\n find_session_cb_cnt = 0;\n SSL_shutdown(clientssl);\n SSL_shutdown(serverssl);\n SSL_free(serverssl);\n SSL_free(clientssl);\n serverssl = clientssl = NULL;\n if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,\n &clientssl, NULL, NULL))\n || !TEST_true(SSL_set_session(clientssl, sess)))\n goto end;\n if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),\n &written))\n || !TEST_size_t_eq(written, strlen(MSG1))\n || !TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),\n &readbytes),\n SSL_READ_EARLY_DATA_SUCCESS)\n || !TEST_mem_eq(buf, readbytes, MSG1, strlen(MSG1)))\n goto end;\n if (!TEST_int_gt(SSL_connect(clientssl), 0)\n || !TEST_int_gt(SSL_accept(serverssl), 0))\n goto end;\n if (!TEST_false(SSL_write_early_data(clientssl, MSG6, strlen(MSG6),\n &written)))\n goto end;\n ERR_clear_error();\n if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),\n &readbytes),\n SSL_READ_EARLY_DATA_ERROR))\n goto end;\n ERR_clear_error();\n if (!TEST_true(SSL_write_ex(clientssl, MSG5, strlen(MSG5), &written))\n || !TEST_size_t_eq(written, strlen(MSG5))\n || !TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))\n || !TEST_size_t_eq(readbytes, strlen(MSG5)))\n goto end;\n testresult = 1;\n end:\n SSL_SESSION_free(sess);\n SSL_SESSION_free(clientpsk);\n SSL_SESSION_free(serverpsk);\n clientpsk = serverpsk = NULL;\n SSL_free(serverssl);\n SSL_free(clientssl);\n SSL_CTX_free(sctx);\n SSL_CTX_free(cctx);\n return testresult;\n}', 'static int setupearly_data_test(SSL_CTX **cctx, SSL_CTX **sctx, SSL **clientssl,\n SSL **serverssl, SSL_SESSION **sess, int idx)\n{\n if (*sctx == NULL\n && !TEST_true(create_ssl_ctx_pair(TLS_server_method(),\n TLS_client_method(),\n TLS1_VERSION, TLS_MAX_VERSION,\n sctx, cctx, cert, privkey)))\n return 0;\n if (!TEST_true(SSL_CTX_set_max_early_data(*sctx, SSL3_RT_MAX_PLAIN_LENGTH)))\n return 0;\n if (idx == 1) {\n SSL_CTX_set_read_ahead(*cctx, 1);\n SSL_CTX_set_read_ahead(*sctx, 1);\n } else if (idx == 2) {\n SSL_CTX_set_psk_use_session_callback(*cctx, use_session_cb);\n SSL_CTX_set_psk_find_session_callback(*sctx, find_session_cb);\n use_session_cb_cnt = 0;\n find_session_cb_cnt = 0;\n srvid = pskid;\n }\n if (!TEST_true(create_ssl_objects(*sctx, *cctx, serverssl, clientssl,\n NULL, NULL)))\n return 0;\n if (idx == 1\n && !TEST_true(SSL_set_tlsext_host_name(*clientssl, "localhost")))\n return 0;\n if (idx == 2) {\n clientpsk = create_a_psk(*clientssl);\n if (!TEST_ptr(clientpsk)\n || !TEST_true(SSL_SESSION_set_max_early_data(clientpsk,\n 0x100))\n || !TEST_true(SSL_SESSION_up_ref(clientpsk))) {\n SSL_SESSION_free(clientpsk);\n clientpsk = NULL;\n return 0;\n }\n serverpsk = clientpsk;\n if (sess != NULL) {\n if (!TEST_true(SSL_SESSION_up_ref(clientpsk))) {\n SSL_SESSION_free(clientpsk);\n SSL_SESSION_free(serverpsk);\n clientpsk = serverpsk = NULL;\n return 0;\n }\n *sess = clientpsk;\n }\n return 1;\n }\n if (sess == NULL)\n return 1;\n if (!TEST_true(create_ssl_connection(*serverssl, *clientssl,\n SSL_ERROR_NONE)))\n return 0;\n *sess = SSL_get1_session(*clientssl);\n SSL_shutdown(*clientssl);\n SSL_shutdown(*serverssl);\n SSL_free(*serverssl);\n SSL_free(*clientssl);\n *serverssl = *clientssl = NULL;\n if (!TEST_true(create_ssl_objects(*sctx, *cctx, serverssl,\n clientssl, NULL, NULL))\n || !TEST_true(SSL_set_session(*clientssl, *sess)))\n return 0;\n return 1;\n}', 'void SSL_free(SSL *s)\n{\n int i;\n if (s == NULL)\n return;\n CRYPTO_DOWN_REF(&s->references, &i, s->lock);\n REF_PRINT_COUNT("SSL", s);\n if (i > 0)\n return;\n REF_ASSERT_ISNT(i < 0);\n X509_VERIFY_PARAM_free(s->param);\n dane_final(&s->dane);\n CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data);\n RECORD_LAYER_release(&s->rlayer);\n ssl_free_wbio_buffer(s);\n BIO_free_all(s->wbio);\n s->wbio = NULL;\n BIO_free_all(s->rbio);\n s->rbio = NULL;\n BUF_MEM_free(s->init_buf);\n sk_SSL_CIPHER_free(s->cipher_list);\n sk_SSL_CIPHER_free(s->cipher_list_by_id);\n sk_SSL_CIPHER_free(s->tls13_ciphersuites);\n if (s->session != NULL) {\n ssl_clear_bad_session(s);\n SSL_SESSION_free(s->session);\n }\n SSL_SESSION_free(s->psksession);\n OPENSSL_free(s->psksession_id);\n clear_ciphers(s);\n ssl_cert_free(s->cert);\n OPENSSL_free(s->ext.hostname);\n SSL_CTX_free(s->session_ctx);\n#ifndef OPENSSL_NO_EC\n OPENSSL_free(s->ext.ecpointformats);\n OPENSSL_free(s->ext.supportedgroups);\n#endif\n sk_X509_EXTENSION_pop_free(s->ext.ocsp.exts, X509_EXTENSION_free);\n#ifndef OPENSSL_NO_OCSP\n sk_OCSP_RESPID_pop_free(s->ext.ocsp.ids, OCSP_RESPID_free);\n#endif\n#ifndef OPENSSL_NO_CT\n SCT_LIST_free(s->scts);\n OPENSSL_free(s->ext.scts);\n#endif\n OPENSSL_free(s->ext.ocsp.resp);\n OPENSSL_free(s->ext.alpn);\n OPENSSL_free(s->ext.tls13_cookie);\n OPENSSL_free(s->clienthello);\n OPENSSL_free(s->pha_context);\n EVP_MD_CTX_free(s->pha_dgst);\n sk_X509_NAME_pop_free(s->ca_names, X509_NAME_free);\n sk_X509_NAME_pop_free(s->client_ca_names, X509_NAME_free);\n sk_X509_pop_free(s->verified_chain, X509_free);\n if (s->method != NULL)\n s->method->ssl_free(s);\n SSL_CTX_free(s->ctx);\n ASYNC_WAIT_CTX_free(s->waitctx);\n#if !defined(OPENSSL_NO_NEXTPROTONEG)\n OPENSSL_free(s->ext.npn);\n#endif\n#ifndef OPENSSL_NO_SRTP\n sk_SRTP_PROTECTION_PROFILE_free(s->srtp_profiles);\n#endif\n CRYPTO_THREAD_lock_free(s->lock);\n OPENSSL_free(s);\n}', 'void SSL_CTX_free(SSL_CTX *a)\n{\n int i;\n if (a == NULL)\n return;\n CRYPTO_DOWN_REF(&a->references, &i, a->lock);\n REF_PRINT_COUNT("SSL_CTX", a);\n if (i > 0)\n return;\n REF_ASSERT_ISNT(i < 0);\n X509_VERIFY_PARAM_free(a->param);\n dane_ctx_final(&a->dane);\n if (a->sessions != NULL)\n SSL_CTX_flush_sessions(a, 0);\n CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_CTX, a, &a->ex_data);\n lh_SSL_SESSION_free(a->sessions);\n X509_STORE_free(a->cert_store);\n#ifndef OPENSSL_NO_CT\n CTLOG_STORE_free(a->ctlog_store);\n#endif\n sk_SSL_CIPHER_free(a->cipher_list);\n sk_SSL_CIPHER_free(a->cipher_list_by_id);\n sk_SSL_CIPHER_free(a->tls13_ciphersuites);\n ssl_cert_free(a->cert);\n sk_X509_NAME_pop_free(a->ca_names, X509_NAME_free);\n sk_X509_NAME_pop_free(a->client_ca_names, X509_NAME_free);\n sk_X509_pop_free(a->extra_certs, X509_free);\n a->comp_methods = NULL;\n#ifndef OPENSSL_NO_SRTP\n sk_SRTP_PROTECTION_PROFILE_free(a->srtp_profiles);\n#endif\n#ifndef OPENSSL_NO_SRP\n SSL_CTX_SRP_CTX_free(a);\n#endif\n#ifndef OPENSSL_NO_ENGINE\n ENGINE_finish(a->client_cert_engine);\n#endif\n#ifndef OPENSSL_NO_EC\n OPENSSL_free(a->ext.ecpointformats);\n OPENSSL_free(a->ext.supportedgroups);\n#endif\n OPENSSL_free(a->ext.alpn);\n OPENSSL_secure_free(a->ext.secure);\n CRYPTO_THREAD_lock_free(a->lock);\n OPENSSL_free(a);\n}', 'void SSL_CTX_flush_sessions(SSL_CTX *s, long t)\n{\n unsigned long i;\n TIMEOUT_PARAM tp;\n tp.ctx = s;\n tp.cache = s->sessions;\n if (tp.cache == NULL)\n return;\n tp.time = t;\n CRYPTO_THREAD_write_lock(s->lock);\n i = lh_SSL_SESSION_get_down_load(s->sessions);\n lh_SSL_SESSION_set_down_load(s->sessions, 0);\n lh_SSL_SESSION_doall_TIMEOUT_PARAM(tp.cache, timeout_cb, &tp);\n lh_SSL_SESSION_set_down_load(s->sessions, i);\n CRYPTO_THREAD_unlock(s->lock);\n}', 'IMPLEMENT_LHASH_DOALL_ARG(SSL_SESSION, TIMEOUT_PARAM)', 'void OPENSSL_LH_doall_arg(OPENSSL_LHASH *lh, OPENSSL_LH_DOALL_FUNCARG func, void *arg)\n{\n doall_util_fn(lh, 1, (OPENSSL_LH_DOALL_FUNC)0, func, arg);\n}', 'static void doall_util_fn(OPENSSL_LHASH *lh, int use_arg,\n OPENSSL_LH_DOALL_FUNC func,\n OPENSSL_LH_DOALL_FUNCARG func_arg, void *arg)\n{\n int i;\n OPENSSL_LH_NODE *a, *n;\n if (lh == NULL)\n return;\n for (i = lh->num_nodes - 1; i >= 0; i--) {\n a = lh->b[i];\n while (a != NULL) {\n n = a->next;\n if (use_arg)\n func_arg(a->data, arg);\n else\n func(a->data);\n a = n;\n }\n }\n}']
|
2,110
| 0
|
https://github.com/openssl/openssl/blob/ed371b8cbac0d0349667558c061c1ae380cf75eb/crypto/bn/bn_ctx.c/#L276
|
static unsigned int BN_STACK_pop(BN_STACK *st)
{
return st->indexes[--(st->depth)];
}
|
['BIGNUM *SRP_Calc_A(const BIGNUM *a, const BIGNUM *N, const BIGNUM *g)\n{\n BN_CTX *bn_ctx;\n BIGNUM *A = NULL;\n if (a == NULL || N == NULL || g == NULL || (bn_ctx = BN_CTX_new()) == NULL)\n return NULL;\n if ((A = BN_new()) != NULL && !BN_mod_exp(A, g, a, N, bn_ctx)) {\n BN_free(A);\n A = NULL;\n }\n BN_CTX_free(bn_ctx);\n return A;\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_word(BIGNUM *rr, BN_ULONG a, const BIGNUM *p,\n const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont)\n{\n BN_MONT_CTX *mont = NULL;\n int b, bits, ret = 0;\n int r_is_one;\n BN_ULONG w, next_w;\n BIGNUM *r, *t;\n BIGNUM *swap_tmp;\n#define BN_MOD_MUL_WORD(r, w, m) \\\n (BN_mul_word(r, (w)) && \\\n ( \\\n (BN_mod(t, r, m, ctx) && (swap_tmp = r, r = t, t = swap_tmp, 1))))\n#define BN_TO_MONTGOMERY_WORD(r, w, mont) \\\n (BN_set_word(r, (w)) && BN_to_montgomery(r, r, (mont), ctx))\n if (BN_get_flags(p, BN_FLG_CONSTTIME) != 0\n || BN_get_flags(m, BN_FLG_CONSTTIME) != 0) {\n BNerr(BN_F_BN_MOD_EXP_MONT_WORD, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);\n return 0;\n }\n bn_check_top(p);\n bn_check_top(m);\n if (!BN_is_odd(m)) {\n BNerr(BN_F_BN_MOD_EXP_MONT_WORD, BN_R_CALLED_WITH_EVEN_MODULUS);\n return 0;\n }\n if (m->top == 1)\n a %= m->d[0];\n bits = BN_num_bits(p);\n if (bits == 0) {\n if (BN_abs_is_word(m, 1)) {\n ret = 1;\n BN_zero(rr);\n } else {\n ret = BN_one(rr);\n }\n return ret;\n }\n if (a == 0) {\n BN_zero(rr);\n ret = 1;\n return ret;\n }\n BN_CTX_start(ctx);\n r = BN_CTX_get(ctx);\n t = BN_CTX_get(ctx);\n if (t == NULL)\n goto err;\n if (in_mont != NULL)\n mont = in_mont;\n else {\n if ((mont = BN_MONT_CTX_new()) == NULL)\n goto err;\n if (!BN_MONT_CTX_set(mont, m, ctx))\n goto err;\n }\n r_is_one = 1;\n w = a;\n for (b = bits - 2; b >= 0; b--) {\n next_w = w * w;\n if ((next_w / w) != w) {\n if (r_is_one) {\n if (!BN_TO_MONTGOMERY_WORD(r, w, mont))\n goto err;\n r_is_one = 0;\n } else {\n if (!BN_MOD_MUL_WORD(r, w, m))\n goto err;\n }\n next_w = 1;\n }\n w = next_w;\n if (!r_is_one) {\n if (!BN_mod_mul_montgomery(r, r, r, mont, ctx))\n goto err;\n }\n if (BN_is_bit_set(p, b)) {\n next_w = w * a;\n if ((next_w / a) != w) {\n if (r_is_one) {\n if (!BN_TO_MONTGOMERY_WORD(r, w, mont))\n goto err;\n r_is_one = 0;\n } else {\n if (!BN_MOD_MUL_WORD(r, w, m))\n goto err;\n }\n next_w = a;\n }\n w = next_w;\n }\n }\n if (w != 1) {\n if (r_is_one) {\n if (!BN_TO_MONTGOMERY_WORD(r, w, mont))\n goto err;\n r_is_one = 0;\n } else {\n if (!BN_MOD_MUL_WORD(r, w, m))\n goto err;\n }\n }\n if (r_is_one) {\n if (!BN_one(rr))\n goto err;\n } else {\n if (!BN_from_montgomery(rr, r, mont, ctx))\n goto err;\n }\n ret = 1;\n err:\n if (in_mont == NULL)\n BN_MONT_CTX_free(mont);\n BN_CTX_end(ctx);\n bn_check_top(rr);\n return ret;\n}', '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_MONT_CTX_set(BN_MONT_CTX *mont, const BIGNUM *mod, BN_CTX *ctx)\n{\n int i, ret = 0;\n BIGNUM *Ri, *R;\n if (BN_is_zero(mod))\n return 0;\n BN_CTX_start(ctx);\n if ((Ri = BN_CTX_get(ctx)) == NULL)\n goto err;\n R = &(mont->RR);\n if (!BN_copy(&(mont->N), mod))\n goto err;\n if (BN_get_flags(mod, BN_FLG_CONSTTIME) != 0)\n BN_set_flags(&(mont->N), BN_FLG_CONSTTIME);\n mont->N.neg = 0;\n#ifdef MONT_WORD\n {\n BIGNUM tmod;\n BN_ULONG buf[2];\n bn_init(&tmod);\n tmod.d = buf;\n tmod.dmax = 2;\n tmod.neg = 0;\n if (BN_get_flags(mod, BN_FLG_CONSTTIME) != 0)\n BN_set_flags(&tmod, BN_FLG_CONSTTIME);\n mont->ri = (BN_num_bits(mod) + (BN_BITS2 - 1)) / BN_BITS2 * BN_BITS2;\n# if defined(OPENSSL_BN_ASM_MONT) && (BN_BITS2<=32)\n BN_zero(R);\n if (!(BN_set_bit(R, 2 * BN_BITS2)))\n goto err;\n tmod.top = 0;\n if ((buf[0] = mod->d[0]))\n tmod.top = 1;\n if ((buf[1] = mod->top > 1 ? mod->d[1] : 0))\n tmod.top = 2;\n if (BN_is_one(&tmod))\n BN_zero(Ri);\n else if ((BN_mod_inverse(Ri, R, &tmod, ctx)) == NULL)\n goto err;\n if (!BN_lshift(Ri, Ri, 2 * BN_BITS2))\n goto err;\n if (!BN_is_zero(Ri)) {\n if (!BN_sub_word(Ri, 1))\n goto err;\n } else {\n if (bn_expand(Ri, (int)sizeof(BN_ULONG) * 2) == NULL)\n goto err;\n Ri->neg = 0;\n Ri->d[0] = BN_MASK2;\n Ri->d[1] = BN_MASK2;\n Ri->top = 2;\n }\n if (!BN_div(Ri, NULL, Ri, &tmod, ctx))\n goto err;\n mont->n0[0] = (Ri->top > 0) ? Ri->d[0] : 0;\n mont->n0[1] = (Ri->top > 1) ? Ri->d[1] : 0;\n# else\n BN_zero(R);\n if (!(BN_set_bit(R, BN_BITS2)))\n goto err;\n buf[0] = mod->d[0];\n buf[1] = 0;\n tmod.top = buf[0] != 0 ? 1 : 0;\n if (BN_is_one(&tmod))\n BN_zero(Ri);\n else if ((BN_mod_inverse(Ri, R, &tmod, ctx)) == NULL)\n goto err;\n if (!BN_lshift(Ri, Ri, BN_BITS2))\n goto err;\n if (!BN_is_zero(Ri)) {\n if (!BN_sub_word(Ri, 1))\n goto err;\n } else {\n if (!BN_set_word(Ri, BN_MASK2))\n goto err;\n }\n if (!BN_div(Ri, NULL, Ri, &tmod, ctx))\n goto err;\n mont->n0[0] = (Ri->top > 0) ? Ri->d[0] : 0;\n mont->n0[1] = 0;\n# endif\n }\n#else\n {\n mont->ri = BN_num_bits(&mont->N);\n BN_zero(R);\n if (!BN_set_bit(R, mont->ri))\n goto err;\n if ((BN_mod_inverse(Ri, R, &mont->N, ctx)) == NULL)\n goto err;\n if (!BN_lshift(Ri, Ri, mont->ri))\n goto err;\n if (!BN_sub_word(Ri, 1))\n goto err;\n if (!BN_div(&(mont->Ni), NULL, Ri, &mont->N, ctx))\n goto err;\n }\n#endif\n BN_zero(&(mont->RR));\n if (!BN_set_bit(&(mont->RR), mont->ri * 2))\n goto err;\n if (!BN_mod(&(mont->RR), &(mont->RR), &(mont->N), ctx))\n goto err;\n for (i = mont->RR.top, ret = mont->N.top; i < ret; i++)\n mont->RR.d[i] = 0;\n mont->RR.top = ret;\n mont->RR.flags |= BN_FLG_FIXED_TOP;\n ret = 1;\n err:\n BN_CTX_end(ctx);\n return ret;\n}', 'BIGNUM *BN_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 (BN_abs_is_word(n, 1) || BN_is_zero(n)) {\n if (pnoinv != NULL)\n *pnoinv = 1;\n return NULL;\n }\n if (pnoinv != NULL)\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}', '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_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}']
|
2,111
| 0
|
https://github.com/openssl/openssl/blob/84c15db551ce1d167b901a3bde2b21880b084384/crypto/bn/bn_asm.c/#L545
|
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_is_prime(BIGNUM *w, void (*callback)(), char *cb_arg)\n\t{\n\tint ok= -1,j,i,n;\n\tBN_CTX *ctx=NULL,*ctx2=NULL;\n\tBIGNUM *w_1,*b,*m,*z,*tmp,*mont_1;\n\tint a;\n\tBN_MONT_CTX *mont=NULL;\n\tif (!BN_is_bit_set(w,0)) return(0);\n\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\tm= &(ctx2->bn[2]);\n\tb= &(ctx2->bn[3]);\n\tz= &(ctx2->bn[4]);\n\tw_1= &(ctx2->bn[5]);\n\ttmp= &(ctx2->bn[6]);\n\tmont_1= &(ctx2->bn[7]);\n\tn=50;\n\tif (!BN_sub(w_1,w,BN_value_one())) goto err;\n\tfor (a=1; !BN_is_bit_set(w_1,a); a++)\n\t\t;\n\tif (!BN_rshift(m,w_1,a)) goto err;\n\tBN_MONT_CTX_set(mont,w,ctx);\n\tBN_to_montgomery(mont_1,BN_value_one(),mont,ctx);\n\tBN_to_montgomery(w_1,w_1,mont,ctx);\n\tfor (i=1; i < n; i++)\n\t\t{\n\t\tBN_rand(b,BN_num_bits(w)-2 ,0,0);\n\t\tj=0;\n\t\tif (!BN_mod_exp_mont(z,b,m,w,ctx,mont)) goto err;\n\t\tif (!BN_to_montgomery(z,z,mont,ctx)) goto err;\n\t\tfor (;;)\n\t\t\t{\n\t\t\tif (((j == 0) && (BN_cmp(z,mont_1) == 0)) ||\n\t\t\t\t(BN_cmp(z,w_1) == 0))\n\t\t\t\tbreak;\n\t\t\tif ((j > 0) && (BN_cmp(z,mont_1) == 0))\n\t\t\t\t{\n\t\t\t\tok=0;\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\tj++;\n\t\t\tif (j >= a)\n\t\t\t\t{\n\t\t\t\tok=0;\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\tif (!BN_mod_mul_montgomery(z,z,z,mont,ctx)) goto err;\n\t\t\tif (callback != NULL) callback(1,j,cb_arg);\n\t\t\t}\n\t\t}\n\tok=1;\nerr:\n\tif (ok == -1) DSAerr(DSA_F_DSA_IS_PRIME,ERR_R_BN_LIB);\n\tBN_CTX_free(ctx);\n\tBN_CTX_free(ctx2);\n\treturn(ok);\n\t}', 'int BN_MONT_CTX_set(BN_MONT_CTX *mont, const BIGNUM *mod, BN_CTX *ctx)\n\t{\n\tBIGNUM Ri,*R;\n\tBN_init(&Ri);\n\tR= &(mont->RR);\n\tBN_copy(&(mont->N),mod);\n#ifdef BN_RECURSION_MONT\n\tif (mont->N.top < BN_MONT_CTX_SET_SIZE_WORD)\n#endif\n\t\t{\n\t\tBIGNUM tmod;\n\t\tBN_ULONG buf[2];\n\t\tmont->use_word=1;\n\t\tmont->ri=(BN_num_bits(mod)+(BN_BITS2-1))/BN_BITS2*BN_BITS2;\n\t\tBN_zero(R);\n\t\tBN_set_bit(R,BN_BITS2);\n\t\tbuf[0]=mod->d[0];\n\t\tbuf[1]=0;\n\t\ttmod.d=buf;\n\t\ttmod.top=1;\n\t\ttmod.max=mod->max;\n\t\ttmod.neg=mod->neg;\n\t\tif ((BN_mod_inverse(&Ri,R,&tmod,ctx)) == NULL)\n\t\t\tgoto err;\n\t\tBN_lshift(&Ri,&Ri,BN_BITS2);\n\t\tif (!BN_is_zero(&Ri))\n\t\t\t{\n#if 1\n\t\t\tBN_sub_word(&Ri,1);\n#else\n\t\t\tBN_usub(&Ri,&Ri,BN_value_one());\n#endif\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tBN_set_word(&Ri,BN_MASK2);\n\t\t\t}\n\t\tBN_div(&Ri,NULL,&Ri,&tmod,ctx);\n\t\tmont->n0=Ri.d[0];\n\t\tBN_free(&Ri);\n\t\t}\n#ifdef BN_RECURSION_MONT\n\telse\n\t\t{\n\t\tmont->use_word=0;\n\t\tmont->ri=(BN_num_bits(mod)+(BN_BITS2-1))/BN_BITS2*BN_BITS2;\n#if 1\n\t\tBN_zero(R);\n\t\tBN_set_bit(R,mont->ri);\n#else\n\t\tBN_lshift(R,BN_value_one(),mont->ri);\n#endif\n\t\tif ((BN_mod_inverse(&Ri,R,mod,ctx)) == NULL)\n\t\t\tgoto err;\n\t\tBN_lshift(&Ri,&Ri,mont->ri);\n#if 1\n\t\tBN_sub_word(&Ri,1);\n#else\n\t\tBN_usub(&Ri,&Ri,BN_value_one());\n#endif\n\t\tBN_div(&(mont->Ni),NULL,&Ri,mod,ctx);\n\t\tBN_free(&Ri);\n\t\t}\n#endif\n#if 1\n\tBN_zero(&(mont->RR));\n\tBN_set_bit(&(mont->RR),mont->ri*2);\n#else\n\tBN_lshift(mont->RR,BN_value_one(),mont->ri*2);\n#endif\n\tBN_mod(&(mont->RR),&(mont->RR),&(mont->N),ctx);\n\treturn(1);\nerr:\n\treturn(0);\n\t}', 'int BN_set_word(BIGNUM *a, BN_ULONG w)\n\t{\n\tint i,n;\n\tif (bn_expand(a,sizeof(BN_ULONG)*8) == NULL) return(0);\n\tn=sizeof(BN_ULONG)/BN_BYTES;\n\ta->neg=0;\n\ta->top=0;\n\ta->d[0]=(BN_ULONG)w&BN_MASK2;\n\tif (a->d[0] != 0) a->top=1;\n\tfor (i=1; i<n; i++)\n\t\t{\n#ifndef SIXTY_FOUR_BIT\n\t\tw>>=BN_BITS4;\n\t\tw>>=BN_BITS4;\n#else\n\t\tw=0;\n#endif\n\t\ta->d[i]=(BN_ULONG)w&BN_MASK2;\n\t\tif (a->d[i] != 0) a->top=i+1;\n\t\t}\n\treturn(1);\n\t}', 'int BN_set_bit(BIGNUM *a, int n)\n\t{\n\tint i,j,k;\n\ti=n/BN_BITS2;\n\tj=n%BN_BITS2;\n\tif (a->top <= i)\n\t\t{\n\t\tif (bn_wexpand(a,i+1) == NULL) return(0);\n\t\tfor(k=a->top; k<i+1; k++)\n\t\t\ta->d[k]=0;\n\t\ta->top=i+1;\n\t\t}\n\ta->d[i]|=(((BN_ULONG)1)<<j);\n\treturn(1);\n\t}', 'int BN_mod_mul_montgomery(BIGNUM *r, BIGNUM *a, BIGNUM *b,\n\t\t\t BN_MONT_CTX *mont, BN_CTX *ctx)\n\t{\n\tBIGNUM *tmp,*tmp2;\n tmp= &(ctx->bn[ctx->tos]);\n tmp2= &(ctx->bn[ctx->tos]);\n\tctx->tos+=2;\n\tbn_check_top(tmp);\n\tbn_check_top(tmp2);\n\tif (a == b)\n\t\t{\n#if 0\n\t\tbn_wexpand(tmp,a->top*2);\n\t\tbn_wexpand(tmp2,a->top*4);\n\t\tbn_sqr_recursive(tmp->d,a->d,a->top,tmp2->d);\n\t\ttmp->top=a->top*2;\n\t\tif (tmp->d[tmp->top-1] == 0)\n\t\t\ttmp->top--;\n#else\n\t\tif (!BN_sqr(tmp,a,ctx)) goto err;\n#endif\n\t\t}\n\telse\n\t\t{\n\t\tif (!BN_mul(tmp,a,b,ctx)) goto err;\n\t\t}\n\tif (!BN_from_montgomery(r,tmp,mont,ctx)) goto err;\n\tctx->tos-=2;\n\treturn(1);\nerr:\n\treturn(0);\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}']
|
2,112
| 0
|
https://github.com/openssl/openssl/blob/924e5eda2c82d737cc5a1b9c37918aa6e34825da/apps/speed.c/#L2847
|
static int do_multi(int multi)
{
int n;
int fd[2];
int *fds;
static char sep[]=":";
fds=malloc(multi*sizeof *fds);
for(n=0 ; n < multi ; ++n)
{
if (pipe(fd) == -1)
{
fprintf(stderr, "pipe failure\n");
exit(1);
}
fflush(stdout);
fflush(stderr);
if(fork())
{
close(fd[1]);
fds[n]=fd[0];
}
else
{
close(fd[0]);
close(1);
if (dup(fd[1]) == -1)
{
fprintf(stderr, "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] != '+')
{
fprintf(stderr,"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))
{
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))
{
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;
}
else if(!strncmp(buf,"+F2:",4))
{
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))
{
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_ECDSA
else if(!strncmp(buf,"+F4:",4))
{
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_ECDH
else if(!strncmp(buf,"+F5:",4))
{
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))
{
}
else
fprintf(stderr,"Unknown type '%s' from child %d\n",buf,n);
}
fclose(f);
}
free(fds);
return 1;
}
|
['static int do_multi(int multi)\n\t{\n\tint n;\n\tint fd[2];\n\tint *fds;\n\tstatic char sep[]=":";\n\tfds=malloc(multi*sizeof *fds);\n\tfor(n=0 ; n < multi ; ++n)\n\t\t{\n\t\tif (pipe(fd) == -1)\n\t\t\t{\n\t\t\tfprintf(stderr, "pipe failure\\n");\n\t\t\texit(1);\n\t\t\t}\n\t\tfflush(stdout);\n\t\tfflush(stderr);\n\t\tif(fork())\n\t\t\t{\n\t\t\tclose(fd[1]);\n\t\t\tfds[n]=fd[0];\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tclose(fd[0]);\n\t\t\tclose(1);\n\t\t\tif (dup(fd[1]) == -1)\n\t\t\t\t{\n\t\t\t\tfprintf(stderr, "dup failed\\n");\n\t\t\t\texit(1);\n\t\t\t\t}\n\t\t\tclose(fd[1]);\n\t\t\tmr=1;\n\t\t\tusertime=0;\n\t\t\tfree(fds);\n\t\t\treturn 0;\n\t\t\t}\n\t\tprintf("Forked child %d\\n",n);\n\t\t}\n\tfor(n=0 ; n < multi ; ++n)\n\t\t{\n\t\tFILE *f;\n\t\tchar buf[1024];\n\t\tchar *p;\n\t\tf=fdopen(fds[n],"r");\n\t\twhile(fgets(buf,sizeof buf,f))\n\t\t\t{\n\t\t\tp=strchr(buf,\'\\n\');\n\t\t\tif(p)\n\t\t\t\t*p=\'\\0\';\n\t\t\tif(buf[0] != \'+\')\n\t\t\t\t{\n\t\t\t\tfprintf(stderr,"Don\'t understand line \'%s\' from child %d\\n",\n\t\t\t\t\t\tbuf,n);\n\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\tprintf("Got: %s from %d\\n",buf,n);\n\t\t\tif(!strncmp(buf,"+F:",3))\n\t\t\t\t{\n\t\t\t\tint alg;\n\t\t\t\tint j;\n\t\t\t\tp=buf+3;\n\t\t\t\talg=atoi(sstrsep(&p,sep));\n\t\t\t\tsstrsep(&p,sep);\n\t\t\t\tfor(j=0 ; j < SIZE_NUM ; ++j)\n\t\t\t\t\tresults[alg][j]+=atof(sstrsep(&p,sep));\n\t\t\t\t}\n\t\t\telse if(!strncmp(buf,"+F2:",4))\n\t\t\t\t{\n\t\t\t\tint k;\n\t\t\t\tdouble d;\n\t\t\t\tp=buf+4;\n\t\t\t\tk=atoi(sstrsep(&p,sep));\n\t\t\t\tsstrsep(&p,sep);\n\t\t\t\td=atof(sstrsep(&p,sep));\n\t\t\t\tif(n)\n\t\t\t\t\trsa_results[k][0]=1/(1/rsa_results[k][0]+1/d);\n\t\t\t\telse\n\t\t\t\t\trsa_results[k][0]=d;\n\t\t\t\td=atof(sstrsep(&p,sep));\n\t\t\t\tif(n)\n\t\t\t\t\trsa_results[k][1]=1/(1/rsa_results[k][1]+1/d);\n\t\t\t\telse\n\t\t\t\t\trsa_results[k][1]=d;\n\t\t\t\t}\n\t\t\telse if(!strncmp(buf,"+F2:",4))\n\t\t\t\t{\n\t\t\t\tint k;\n\t\t\t\tdouble d;\n\t\t\t\tp=buf+4;\n\t\t\t\tk=atoi(sstrsep(&p,sep));\n\t\t\t\tsstrsep(&p,sep);\n\t\t\t\td=atof(sstrsep(&p,sep));\n\t\t\t\tif(n)\n\t\t\t\t\trsa_results[k][0]=1/(1/rsa_results[k][0]+1/d);\n\t\t\t\telse\n\t\t\t\t\trsa_results[k][0]=d;\n\t\t\t\td=atof(sstrsep(&p,sep));\n\t\t\t\tif(n)\n\t\t\t\t\trsa_results[k][1]=1/(1/rsa_results[k][1]+1/d);\n\t\t\t\telse\n\t\t\t\t\trsa_results[k][1]=d;\n\t\t\t\t}\n#ifndef OPENSSL_NO_DSA\n\t\t\telse if(!strncmp(buf,"+F3:",4))\n\t\t\t\t{\n\t\t\t\tint k;\n\t\t\t\tdouble d;\n\t\t\t\tp=buf+4;\n\t\t\t\tk=atoi(sstrsep(&p,sep));\n\t\t\t\tsstrsep(&p,sep);\n\t\t\t\td=atof(sstrsep(&p,sep));\n\t\t\t\tif(n)\n\t\t\t\t\tdsa_results[k][0]=1/(1/dsa_results[k][0]+1/d);\n\t\t\t\telse\n\t\t\t\t\tdsa_results[k][0]=d;\n\t\t\t\td=atof(sstrsep(&p,sep));\n\t\t\t\tif(n)\n\t\t\t\t\tdsa_results[k][1]=1/(1/dsa_results[k][1]+1/d);\n\t\t\t\telse\n\t\t\t\t\tdsa_results[k][1]=d;\n\t\t\t\t}\n#endif\n#ifndef OPENSSL_NO_ECDSA\n\t\t\telse if(!strncmp(buf,"+F4:",4))\n\t\t\t\t{\n\t\t\t\tint k;\n\t\t\t\tdouble d;\n\t\t\t\tp=buf+4;\n\t\t\t\tk=atoi(sstrsep(&p,sep));\n\t\t\t\tsstrsep(&p,sep);\n\t\t\t\td=atof(sstrsep(&p,sep));\n\t\t\t\tif(n)\n\t\t\t\t\tecdsa_results[k][0]=1/(1/ecdsa_results[k][0]+1/d);\n\t\t\t\telse\n\t\t\t\t\tecdsa_results[k][0]=d;\n\t\t\t\td=atof(sstrsep(&p,sep));\n\t\t\t\tif(n)\n\t\t\t\t\tecdsa_results[k][1]=1/(1/ecdsa_results[k][1]+1/d);\n\t\t\t\telse\n\t\t\t\t\tecdsa_results[k][1]=d;\n\t\t\t\t}\n#endif\n#ifndef OPENSSL_NO_ECDH\n\t\t\telse if(!strncmp(buf,"+F5:",4))\n\t\t\t\t{\n\t\t\t\tint k;\n\t\t\t\tdouble d;\n\t\t\t\tp=buf+4;\n\t\t\t\tk=atoi(sstrsep(&p,sep));\n\t\t\t\tsstrsep(&p,sep);\n\t\t\t\td=atof(sstrsep(&p,sep));\n\t\t\t\tif(n)\n\t\t\t\t\tecdh_results[k][0]=1/(1/ecdh_results[k][0]+1/d);\n\t\t\t\telse\n\t\t\t\t\tecdh_results[k][0]=d;\n\t\t\t\t}\n#endif\n\t\t\telse if(!strncmp(buf,"+H:",3))\n\t\t\t\t{\n\t\t\t\t}\n\t\t\telse\n\t\t\t\tfprintf(stderr,"Unknown type \'%s\' from child %d\\n",buf,n);\n\t\t\t}\n\t\tfclose(f);\n\t\t}\n\tfree(fds);\n\treturn 1;\n\t}']
|
2,113
| 0
|
https://github.com/nginx/nginx/blob/e4ecddfdb0d2ffc872658e36028971ad9a873726/src/http/modules/ngx_http_fastcgi_module.c/#L804
|
static ngx_int_t
ngx_http_fastcgi_create_request(ngx_http_request_t *r)
{
off_t file_pos;
u_char ch, *pos;
size_t size, len, key_len, val_len, padding;
ngx_uint_t i, n, next;
ngx_buf_t *b;
ngx_chain_t *cl, *body;
ngx_list_part_t *part;
ngx_table_elt_t *header;
ngx_http_script_code_pt code;
ngx_http_script_engine_t e, le;
ngx_http_fastcgi_header_t *h;
ngx_http_fastcgi_loc_conf_t *flcf;
ngx_http_script_len_code_pt lcode;
len = 0;
flcf = ngx_http_get_module_loc_conf(r, ngx_http_fastcgi_module);
if (flcf->params_len) {
ngx_memzero(&le, sizeof(ngx_http_script_engine_t));
ngx_http_script_flush_no_cacheable_variables(r, flcf->flushes);
le.flushed = 1;
le.ip = flcf->params_len->elts;
le.request = r;
while (*(uintptr_t *) le.ip) {
lcode = *(ngx_http_script_len_code_pt *) le.ip;
key_len = lcode(&le);
for (val_len = 0; *(uintptr_t *) le.ip; val_len += lcode(&le)) {
lcode = *(ngx_http_script_len_code_pt *) le.ip;
}
le.ip += sizeof(uintptr_t);
len += 1 + key_len + ((val_len > 127) ? 4 : 1) + val_len;
}
}
if (flcf->upstream.pass_request_headers) {
part = &r->headers_in.headers.part;
header = part->elts;
for (i = 0; ; i++) {
if (i >= part->nelts) {
if (part->next == NULL) {
break;
}
part = part->next;
header = part->elts;
i = 0;
}
len += ((sizeof("HTTP_") - 1 + header[i].key.len > 127) ? 4 : 1)
+ ((header[i].value.len > 127) ? 4 : 1)
+ sizeof("HTTP_") - 1 + header[i].key.len + header[i].value.len;
}
}
if (len > 65535) {
ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
"fastcgi request record is too big: %uz", len);
return NGX_ERROR;
}
padding = 8 - len % 8;
padding = (padding == 8) ? 0 : padding;
size = sizeof(ngx_http_fastcgi_header_t)
+ sizeof(ngx_http_fastcgi_begin_request_t)
+ sizeof(ngx_http_fastcgi_header_t)
+ len + padding
+ sizeof(ngx_http_fastcgi_header_t)
+ sizeof(ngx_http_fastcgi_header_t);
b = ngx_create_temp_buf(r->pool, size);
if (b == NULL) {
return NGX_ERROR;
}
cl = ngx_alloc_chain_link(r->pool);
if (cl == NULL) {
return NGX_ERROR;
}
cl->buf = b;
ngx_memcpy(b->pos, &ngx_http_fastcgi_request_start,
sizeof(ngx_http_fastcgi_request_start_t));
h = (ngx_http_fastcgi_header_t *)
(b->pos + sizeof(ngx_http_fastcgi_header_t)
+ sizeof(ngx_http_fastcgi_begin_request_t));
h->content_length_hi = (u_char) ((len >> 8) & 0xff);
h->content_length_lo = (u_char) (len & 0xff);
h->padding_length = (u_char) padding;
h->reserved = 0;
b->last = b->pos + sizeof(ngx_http_fastcgi_header_t)
+ sizeof(ngx_http_fastcgi_begin_request_t)
+ sizeof(ngx_http_fastcgi_header_t);
if (flcf->params_len) {
ngx_memzero(&e, sizeof(ngx_http_script_engine_t));
e.ip = flcf->params->elts;
e.pos = b->last;
e.request = r;
e.flushed = 1;
le.ip = flcf->params_len->elts;
while (*(uintptr_t *) le.ip) {
lcode = *(ngx_http_script_len_code_pt *) le.ip;
key_len = (u_char) lcode(&le);
for (val_len = 0; *(uintptr_t *) le.ip; val_len += lcode(&le)) {
lcode = *(ngx_http_script_len_code_pt *) le.ip;
}
le.ip += sizeof(uintptr_t);
*e.pos++ = (u_char) key_len;
if (val_len > 127) {
*e.pos++ = (u_char) (((val_len >> 24) & 0x7f) | 0x80);
*e.pos++ = (u_char) ((val_len >> 16) & 0xff);
*e.pos++ = (u_char) ((val_len >> 8) & 0xff);
*e.pos++ = (u_char) (val_len & 0xff);
} else {
*e.pos++ = (u_char) val_len;
}
while (*(uintptr_t *) e.ip) {
code = *(ngx_http_script_code_pt *) e.ip;
code((ngx_http_script_engine_t *) &e);
}
e.ip += sizeof(uintptr_t);
ngx_log_debug4(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"fastcgi param: \"%*s: %*s\"",
key_len, e.pos - (key_len + val_len),
val_len, e.pos - val_len);
}
b->last = e.pos;
}
if (flcf->upstream.pass_request_headers) {
part = &r->headers_in.headers.part;
header = part->elts;
for (i = 0; ; i++) {
if (i >= part->nelts) {
if (part->next == NULL) {
break;
}
part = part->next;
header = part->elts;
i = 0;
}
len = sizeof("HTTP_") - 1 + header[i].key.len;
if (len > 127) {
*b->last++ = (u_char) (((len >> 24) & 0x7f) | 0x80);
*b->last++ = (u_char) ((len >> 16) & 0xff);
*b->last++ = (u_char) ((len >> 8) & 0xff);
*b->last++ = (u_char) (len & 0xff);
} else {
*b->last++ = (u_char) len;
}
len = header[i].value.len;
if (len > 127) {
*b->last++ = (u_char) (((len >> 24) & 0x7f) | 0x80);
*b->last++ = (u_char) ((len >> 16) & 0xff);
*b->last++ = (u_char) ((len >> 8) & 0xff);
*b->last++ = (u_char) (len & 0xff);
} else {
*b->last++ = (u_char) len;
}
b->last = ngx_cpymem(b->last, "HTTP_", sizeof("HTTP_") - 1);
for (n = 0; n < header[i].key.len; n++) {
ch = header[i].key.data[n];
if (ch >= 'a' && ch <= 'z') {
ch &= ~0x20;
} else if (ch == '-') {
ch = '_';
}
*b->last++ = ch;
}
b->last = ngx_copy(b->last, header[i].value.data,
header[i].value.len);
}
}
if (padding) {
ngx_memzero(b->last, padding);
b->last += padding;
}
h = (ngx_http_fastcgi_header_t *) b->last;
b->last += sizeof(ngx_http_fastcgi_header_t);
h->version = 1;
h->type = NGX_HTTP_FASTCGI_PARAMS;
h->request_id_hi = 0;
h->request_id_lo = 1;
h->content_length_hi = 0;
h->content_length_lo = 0;
h->padding_length = 0;
h->reserved = 0;
h = (ngx_http_fastcgi_header_t *) b->last;
b->last += sizeof(ngx_http_fastcgi_header_t);
if (flcf->upstream.pass_request_body) {
body = r->upstream->request_bufs;
r->upstream->request_bufs = cl;
#if (NGX_SUPPRESS_WARN)
file_pos = 0;
pos = NULL;
#endif
while (body) {
if (body->buf->in_file) {
file_pos = body->buf->file_pos;
} else {
pos = body->buf->pos;
}
next = 0;
do {
b = ngx_alloc_buf(r->pool);
if (b == NULL) {
return NGX_ERROR;
}
ngx_memcpy(b, body->buf, sizeof(ngx_buf_t));
if (body->buf->in_file) {
b->file_pos = file_pos;
file_pos += 32 * 1024;
if (file_pos >= body->buf->file_last) {
file_pos = body->buf->file_last;
next = 1;
}
b->file_last = file_pos;
len = (ngx_uint_t) (file_pos - b->file_pos);
} else {
b->pos = pos;
pos += 32 * 1024;
if (pos >= body->buf->last) {
pos = body->buf->last;
next = 1;
}
b->last = pos;
len = (ngx_uint_t) (pos - b->pos);
}
padding = 8 - len % 8;
padding = (padding == 8) ? 0 : padding;
h->version = 1;
h->type = NGX_HTTP_FASTCGI_STDIN;
h->request_id_hi = 0;
h->request_id_lo = 1;
h->content_length_hi = (u_char) ((len >> 8) & 0xff);
h->content_length_lo = (u_char) (len & 0xff);
h->padding_length = (u_char) padding;
h->reserved = 0;
cl->next = ngx_alloc_chain_link(r->pool);
if (cl->next == NULL) {
return NGX_ERROR;
}
cl = cl->next;
cl->buf = b;
b = ngx_create_temp_buf(r->pool,
sizeof(ngx_http_fastcgi_header_t)
+ padding);
if (b == NULL) {
return NGX_ERROR;
}
if (padding) {
ngx_memzero(b->last, padding);
b->last += padding;
}
h = (ngx_http_fastcgi_header_t *) b->last;
b->last += sizeof(ngx_http_fastcgi_header_t);
cl->next = ngx_alloc_chain_link(r->pool);
if (cl->next == NULL) {
return NGX_ERROR;
}
cl = cl->next;
cl->buf = b;
} while (!next);
body = body->next;
}
} else {
r->upstream->request_bufs = cl;
}
h->version = 1;
h->type = NGX_HTTP_FASTCGI_STDIN;
h->request_id_hi = 0;
h->request_id_lo = 1;
h->content_length_hi = 0;
h->content_length_lo = 0;
h->padding_length = 0;
h->reserved = 0;
cl->next = NULL;
return NGX_OK;
}
|
['static ngx_int_t\nngx_http_fastcgi_create_request(ngx_http_request_t *r)\n{\n off_t file_pos;\n u_char ch, *pos;\n size_t size, len, key_len, val_len, padding;\n ngx_uint_t i, n, next;\n ngx_buf_t *b;\n ngx_chain_t *cl, *body;\n ngx_list_part_t *part;\n ngx_table_elt_t *header;\n ngx_http_script_code_pt code;\n ngx_http_script_engine_t e, le;\n ngx_http_fastcgi_header_t *h;\n ngx_http_fastcgi_loc_conf_t *flcf;\n ngx_http_script_len_code_pt lcode;\n len = 0;\n flcf = ngx_http_get_module_loc_conf(r, ngx_http_fastcgi_module);\n if (flcf->params_len) {\n ngx_memzero(&le, sizeof(ngx_http_script_engine_t));\n ngx_http_script_flush_no_cacheable_variables(r, flcf->flushes);\n le.flushed = 1;\n le.ip = flcf->params_len->elts;\n le.request = r;\n while (*(uintptr_t *) le.ip) {\n lcode = *(ngx_http_script_len_code_pt *) le.ip;\n key_len = lcode(&le);\n for (val_len = 0; *(uintptr_t *) le.ip; val_len += lcode(&le)) {\n lcode = *(ngx_http_script_len_code_pt *) le.ip;\n }\n le.ip += sizeof(uintptr_t);\n len += 1 + key_len + ((val_len > 127) ? 4 : 1) + val_len;\n }\n }\n if (flcf->upstream.pass_request_headers) {\n part = &r->headers_in.headers.part;\n header = part->elts;\n for (i = 0; ; i++) {\n if (i >= part->nelts) {\n if (part->next == NULL) {\n break;\n }\n part = part->next;\n header = part->elts;\n i = 0;\n }\n len += ((sizeof("HTTP_") - 1 + header[i].key.len > 127) ? 4 : 1)\n + ((header[i].value.len > 127) ? 4 : 1)\n + sizeof("HTTP_") - 1 + header[i].key.len + header[i].value.len;\n }\n }\n if (len > 65535) {\n ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,\n "fastcgi request record is too big: %uz", len);\n return NGX_ERROR;\n }\n padding = 8 - len % 8;\n padding = (padding == 8) ? 0 : padding;\n size = sizeof(ngx_http_fastcgi_header_t)\n + sizeof(ngx_http_fastcgi_begin_request_t)\n + sizeof(ngx_http_fastcgi_header_t)\n + len + padding\n + sizeof(ngx_http_fastcgi_header_t)\n + sizeof(ngx_http_fastcgi_header_t);\n b = ngx_create_temp_buf(r->pool, size);\n if (b == NULL) {\n return NGX_ERROR;\n }\n cl = ngx_alloc_chain_link(r->pool);\n if (cl == NULL) {\n return NGX_ERROR;\n }\n cl->buf = b;\n ngx_memcpy(b->pos, &ngx_http_fastcgi_request_start,\n sizeof(ngx_http_fastcgi_request_start_t));\n h = (ngx_http_fastcgi_header_t *)\n (b->pos + sizeof(ngx_http_fastcgi_header_t)\n + sizeof(ngx_http_fastcgi_begin_request_t));\n h->content_length_hi = (u_char) ((len >> 8) & 0xff);\n h->content_length_lo = (u_char) (len & 0xff);\n h->padding_length = (u_char) padding;\n h->reserved = 0;\n b->last = b->pos + sizeof(ngx_http_fastcgi_header_t)\n + sizeof(ngx_http_fastcgi_begin_request_t)\n + sizeof(ngx_http_fastcgi_header_t);\n if (flcf->params_len) {\n ngx_memzero(&e, sizeof(ngx_http_script_engine_t));\n e.ip = flcf->params->elts;\n e.pos = b->last;\n e.request = r;\n e.flushed = 1;\n le.ip = flcf->params_len->elts;\n while (*(uintptr_t *) le.ip) {\n lcode = *(ngx_http_script_len_code_pt *) le.ip;\n key_len = (u_char) lcode(&le);\n for (val_len = 0; *(uintptr_t *) le.ip; val_len += lcode(&le)) {\n lcode = *(ngx_http_script_len_code_pt *) le.ip;\n }\n le.ip += sizeof(uintptr_t);\n *e.pos++ = (u_char) key_len;\n if (val_len > 127) {\n *e.pos++ = (u_char) (((val_len >> 24) & 0x7f) | 0x80);\n *e.pos++ = (u_char) ((val_len >> 16) & 0xff);\n *e.pos++ = (u_char) ((val_len >> 8) & 0xff);\n *e.pos++ = (u_char) (val_len & 0xff);\n } else {\n *e.pos++ = (u_char) val_len;\n }\n while (*(uintptr_t *) e.ip) {\n code = *(ngx_http_script_code_pt *) e.ip;\n code((ngx_http_script_engine_t *) &e);\n }\n e.ip += sizeof(uintptr_t);\n ngx_log_debug4(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,\n "fastcgi param: \\"%*s: %*s\\"",\n key_len, e.pos - (key_len + val_len),\n val_len, e.pos - val_len);\n }\n b->last = e.pos;\n }\n if (flcf->upstream.pass_request_headers) {\n part = &r->headers_in.headers.part;\n header = part->elts;\n for (i = 0; ; i++) {\n if (i >= part->nelts) {\n if (part->next == NULL) {\n break;\n }\n part = part->next;\n header = part->elts;\n i = 0;\n }\n len = sizeof("HTTP_") - 1 + header[i].key.len;\n if (len > 127) {\n *b->last++ = (u_char) (((len >> 24) & 0x7f) | 0x80);\n *b->last++ = (u_char) ((len >> 16) & 0xff);\n *b->last++ = (u_char) ((len >> 8) & 0xff);\n *b->last++ = (u_char) (len & 0xff);\n } else {\n *b->last++ = (u_char) len;\n }\n len = header[i].value.len;\n if (len > 127) {\n *b->last++ = (u_char) (((len >> 24) & 0x7f) | 0x80);\n *b->last++ = (u_char) ((len >> 16) & 0xff);\n *b->last++ = (u_char) ((len >> 8) & 0xff);\n *b->last++ = (u_char) (len & 0xff);\n } else {\n *b->last++ = (u_char) len;\n }\n b->last = ngx_cpymem(b->last, "HTTP_", sizeof("HTTP_") - 1);\n for (n = 0; n < header[i].key.len; n++) {\n ch = header[i].key.data[n];\n if (ch >= \'a\' && ch <= \'z\') {\n ch &= ~0x20;\n } else if (ch == \'-\') {\n ch = \'_\';\n }\n *b->last++ = ch;\n }\n b->last = ngx_copy(b->last, header[i].value.data,\n header[i].value.len);\n }\n }\n if (padding) {\n ngx_memzero(b->last, padding);\n b->last += padding;\n }\n h = (ngx_http_fastcgi_header_t *) b->last;\n b->last += sizeof(ngx_http_fastcgi_header_t);\n h->version = 1;\n h->type = NGX_HTTP_FASTCGI_PARAMS;\n h->request_id_hi = 0;\n h->request_id_lo = 1;\n h->content_length_hi = 0;\n h->content_length_lo = 0;\n h->padding_length = 0;\n h->reserved = 0;\n h = (ngx_http_fastcgi_header_t *) b->last;\n b->last += sizeof(ngx_http_fastcgi_header_t);\n if (flcf->upstream.pass_request_body) {\n body = r->upstream->request_bufs;\n r->upstream->request_bufs = cl;\n#if (NGX_SUPPRESS_WARN)\n file_pos = 0;\n pos = NULL;\n#endif\n while (body) {\n if (body->buf->in_file) {\n file_pos = body->buf->file_pos;\n } else {\n pos = body->buf->pos;\n }\n next = 0;\n do {\n b = ngx_alloc_buf(r->pool);\n if (b == NULL) {\n return NGX_ERROR;\n }\n ngx_memcpy(b, body->buf, sizeof(ngx_buf_t));\n if (body->buf->in_file) {\n b->file_pos = file_pos;\n file_pos += 32 * 1024;\n if (file_pos >= body->buf->file_last) {\n file_pos = body->buf->file_last;\n next = 1;\n }\n b->file_last = file_pos;\n len = (ngx_uint_t) (file_pos - b->file_pos);\n } else {\n b->pos = pos;\n pos += 32 * 1024;\n if (pos >= body->buf->last) {\n pos = body->buf->last;\n next = 1;\n }\n b->last = pos;\n len = (ngx_uint_t) (pos - b->pos);\n }\n padding = 8 - len % 8;\n padding = (padding == 8) ? 0 : padding;\n h->version = 1;\n h->type = NGX_HTTP_FASTCGI_STDIN;\n h->request_id_hi = 0;\n h->request_id_lo = 1;\n h->content_length_hi = (u_char) ((len >> 8) & 0xff);\n h->content_length_lo = (u_char) (len & 0xff);\n h->padding_length = (u_char) padding;\n h->reserved = 0;\n cl->next = ngx_alloc_chain_link(r->pool);\n if (cl->next == NULL) {\n return NGX_ERROR;\n }\n cl = cl->next;\n cl->buf = b;\n b = ngx_create_temp_buf(r->pool,\n sizeof(ngx_http_fastcgi_header_t)\n + padding);\n if (b == NULL) {\n return NGX_ERROR;\n }\n if (padding) {\n ngx_memzero(b->last, padding);\n b->last += padding;\n }\n h = (ngx_http_fastcgi_header_t *) b->last;\n b->last += sizeof(ngx_http_fastcgi_header_t);\n cl->next = ngx_alloc_chain_link(r->pool);\n if (cl->next == NULL) {\n return NGX_ERROR;\n }\n cl = cl->next;\n cl->buf = b;\n } while (!next);\n body = body->next;\n }\n } else {\n r->upstream->request_bufs = cl;\n }\n h->version = 1;\n h->type = NGX_HTTP_FASTCGI_STDIN;\n h->request_id_hi = 0;\n h->request_id_lo = 1;\n h->content_length_hi = 0;\n h->content_length_lo = 0;\n h->padding_length = 0;\n h->reserved = 0;\n cl->next = NULL;\n return NGX_OK;\n}', 'ngx_buf_t *\nngx_create_temp_buf(ngx_pool_t *pool, size_t size)\n{\n ngx_buf_t *b;\n b = ngx_calloc_buf(pool);\n if (b == NULL) {\n return NULL;\n }\n b->start = ngx_palloc(pool, size);\n if (b->start == NULL) {\n return NULL;\n }\n b->pos = b->start;\n b->last = b->start;\n b->end = b->last + size;\n b->temporary = 1;\n return b;\n}', 'void *\nngx_palloc(ngx_pool_t *pool, size_t size)\n{\n u_char *m;\n ngx_pool_t *p;\n if (size <= pool->max) {\n p = pool->current;\n do {\n m = ngx_align_ptr(p->d.last, NGX_ALIGNMENT);\n if ((size_t) (p->d.end - m) >= size) {\n p->d.last = m + size;\n return m;\n }\n p = p->d.next;\n } while (p);\n return ngx_palloc_block(pool, size);\n }\n return ngx_palloc_large(pool, size);\n}']
|
2,114
| 0
|
https://github.com/libav/libav/blob/2c8077621b6466da205ba26fd20a9c906bb71893/libavcodec/interplayvideo.c/#L905
|
static int ipvideo_decode_block_opcode_0xD_16(IpvideoContext *s)
{
int x, y;
uint16_t P[2];
uint16_t *pixel_ptr = (uint16_t*)s->pixel_ptr;
CHECK_STREAM_PTR(s->stream_ptr, s->stream_end, 8);
for (y = 0; y < 8; y++) {
if (!(y & 3)) {
P[0] = bytestream_get_le16(&s->stream_ptr);
P[1] = bytestream_get_le16(&s->stream_ptr);
}
for (x = 0; x < 8; x++)
pixel_ptr[x] = P[x >> 2];
pixel_ptr += s->stride;
}
return 0;
}
|
['static int ipvideo_decode_block_opcode_0xD_16(IpvideoContext *s)\n{\n int x, y;\n uint16_t P[2];\n uint16_t *pixel_ptr = (uint16_t*)s->pixel_ptr;\n CHECK_STREAM_PTR(s->stream_ptr, s->stream_end, 8);\n for (y = 0; y < 8; y++) {\n if (!(y & 3)) {\n P[0] = bytestream_get_le16(&s->stream_ptr);\n P[1] = bytestream_get_le16(&s->stream_ptr);\n }\n for (x = 0; x < 8; x++)\n pixel_ptr[x] = P[x >> 2];\n pixel_ptr += s->stride;\n }\n return 0;\n}']
|
2,115
| 0
|
https://github.com/libav/libav/blob/da5bcafe3b728f255ad068fc406622515b435082/libavcodec/aacsbr.c/#L463
|
static int sbr_make_f_master(AACContext *ac, SpectralBandReplication *sbr,
SpectrumParameters *spectrum)
{
unsigned int temp, max_qmf_subbands;
unsigned int start_min, stop_min;
int k;
const int8_t *sbr_offset_ptr;
int16_t stop_dk[13];
if (sbr->sample_rate < 32000) {
temp = 3000;
} else if (sbr->sample_rate < 64000) {
temp = 4000;
} else
temp = 5000;
start_min = ((temp << 7) + (sbr->sample_rate >> 1)) / sbr->sample_rate;
stop_min = ((temp << 8) + (sbr->sample_rate >> 1)) / sbr->sample_rate;
switch (sbr->sample_rate) {
case 16000:
sbr_offset_ptr = sbr_offset[0];
break;
case 22050:
sbr_offset_ptr = sbr_offset[1];
break;
case 24000:
sbr_offset_ptr = sbr_offset[2];
break;
case 32000:
sbr_offset_ptr = sbr_offset[3];
break;
case 44100: case 48000: case 64000:
sbr_offset_ptr = sbr_offset[4];
break;
case 88200: case 96000: case 128000: case 176400: case 192000:
sbr_offset_ptr = sbr_offset[5];
break;
default:
av_log(ac->avccontext, AV_LOG_ERROR,
"Unsupported sample rate for SBR: %d\n", sbr->sample_rate);
return -1;
}
sbr->k[0] = start_min + sbr_offset_ptr[spectrum->bs_start_freq];
if (spectrum->bs_stop_freq < 14) {
sbr->k[2] = stop_min;
make_bands(stop_dk, stop_min, 64, 13);
qsort(stop_dk, 13, sizeof(stop_dk[0]), qsort_comparison_function_int16);
for (k = 0; k < spectrum->bs_stop_freq; k++)
sbr->k[2] += stop_dk[k];
} else if (spectrum->bs_stop_freq == 14) {
sbr->k[2] = 2*sbr->k[0];
} else if (spectrum->bs_stop_freq == 15) {
sbr->k[2] = 3*sbr->k[0];
} else {
av_log(ac->avccontext, AV_LOG_ERROR,
"Invalid bs_stop_freq: %d\n", spectrum->bs_stop_freq);
return -1;
}
sbr->k[2] = FFMIN(64, sbr->k[2]);
if (sbr->sample_rate <= 32000) {
max_qmf_subbands = 48;
} else if (sbr->sample_rate == 44100) {
max_qmf_subbands = 35;
} else if (sbr->sample_rate >= 48000)
max_qmf_subbands = 32;
if (sbr->k[2] - sbr->k[0] > max_qmf_subbands) {
av_log(ac->avccontext, AV_LOG_ERROR,
"Invalid bitstream, too many QMF subbands: %d\n", sbr->k[2] - sbr->k[0]);
return -1;
}
if (!spectrum->bs_freq_scale) {
unsigned int dk;
int k2diff;
dk = spectrum->bs_alter_scale + 1;
sbr->n_master = ((sbr->k[2] - sbr->k[0] + (dk&2)) >> dk) << 1;
if (check_n_master(ac->avccontext, sbr->n_master, sbr->spectrum_params.bs_xover_band))
return -1;
for (k = 1; k <= sbr->n_master; k++)
sbr->f_master[k] = dk;
k2diff = sbr->k[2] - sbr->k[0] - sbr->n_master * dk;
if (k2diff < 0) {
sbr->f_master[1]--;
sbr->f_master[2]-= (k2diff < 1);
} else if (k2diff) {
sbr->f_master[sbr->n_master]++;
}
sbr->f_master[0] = sbr->k[0];
for (k = 1; k <= sbr->n_master; k++)
sbr->f_master[k] += sbr->f_master[k - 1];
} else {
int half_bands = 7 - spectrum->bs_freq_scale;
int two_regions, num_bands_0;
int vdk0_max, vdk1_min;
int16_t vk0[49];
if (49 * sbr->k[2] > 110 * sbr->k[0]) {
two_regions = 1;
sbr->k[1] = 2 * sbr->k[0];
} else {
two_regions = 0;
sbr->k[1] = sbr->k[2];
}
num_bands_0 = lrintf(half_bands * log2f(sbr->k[1] / (float)sbr->k[0])) * 2;
if (num_bands_0 <= 0) {
av_log(ac->avccontext, AV_LOG_ERROR, "Invalid num_bands_0: %d\n", num_bands_0);
return -1;
}
vk0[0] = 0;
make_bands(vk0+1, sbr->k[0], sbr->k[1], num_bands_0);
qsort(vk0 + 1, num_bands_0, sizeof(vk0[1]), qsort_comparison_function_int16);
vdk0_max = vk0[num_bands_0];
vk0[0] = sbr->k[0];
for (k = 1; k <= num_bands_0; k++) {
if (vk0[k] <= 0) {
av_log(ac->avccontext, AV_LOG_ERROR, "Invalid vDk0[%d]: %d\n", k, vk0[k]);
return -1;
}
vk0[k] += vk0[k-1];
}
if (two_regions) {
int16_t vk1[49];
float invwarp = spectrum->bs_alter_scale ? 0.76923076923076923077f
: 1.0f;
int num_bands_1 = lrintf(half_bands * invwarp *
log2f(sbr->k[2] / (float)sbr->k[1])) * 2;
make_bands(vk1+1, sbr->k[1], sbr->k[2], num_bands_1);
vdk1_min = array_min_int16(vk1 + 1, num_bands_1);
if (vdk1_min < vdk0_max) {
int change;
qsort(vk1 + 1, num_bands_1, sizeof(vk1[1]), qsort_comparison_function_int16);
change = FFMIN(vdk0_max - vk1[1], (vk1[num_bands_1] - vk1[1]) >> 1);
vk1[1] += change;
vk1[num_bands_1] -= change;
}
qsort(vk1 + 1, num_bands_1, sizeof(vk1[1]), qsort_comparison_function_int16);
vk1[0] = sbr->k[1];
for (k = 1; k <= num_bands_1; k++) {
if (vk1[k] <= 0) {
av_log(ac->avccontext, AV_LOG_ERROR, "Invalid vDk1[%d]: %d\n", k, vk1[k]);
return -1;
}
vk1[k] += vk1[k-1];
}
sbr->n_master = num_bands_0 + num_bands_1;
if (check_n_master(ac->avccontext, sbr->n_master, sbr->spectrum_params.bs_xover_band))
return -1;
memcpy(&sbr->f_master[0], vk0,
(num_bands_0 + 1) * sizeof(sbr->f_master[0]));
memcpy(&sbr->f_master[num_bands_0 + 1], vk1 + 1,
num_bands_1 * sizeof(sbr->f_master[0]));
} else {
sbr->n_master = num_bands_0;
if (check_n_master(ac->avccontext, sbr->n_master, sbr->spectrum_params.bs_xover_band))
return -1;
memcpy(sbr->f_master, vk0, (num_bands_0 + 1) * sizeof(sbr->f_master[0]));
}
}
return 0;
}
|
['static int sbr_make_f_master(AACContext *ac, SpectralBandReplication *sbr,\n SpectrumParameters *spectrum)\n{\n unsigned int temp, max_qmf_subbands;\n unsigned int start_min, stop_min;\n int k;\n const int8_t *sbr_offset_ptr;\n int16_t stop_dk[13];\n if (sbr->sample_rate < 32000) {\n temp = 3000;\n } else if (sbr->sample_rate < 64000) {\n temp = 4000;\n } else\n temp = 5000;\n start_min = ((temp << 7) + (sbr->sample_rate >> 1)) / sbr->sample_rate;\n stop_min = ((temp << 8) + (sbr->sample_rate >> 1)) / sbr->sample_rate;\n switch (sbr->sample_rate) {\n case 16000:\n sbr_offset_ptr = sbr_offset[0];\n break;\n case 22050:\n sbr_offset_ptr = sbr_offset[1];\n break;\n case 24000:\n sbr_offset_ptr = sbr_offset[2];\n break;\n case 32000:\n sbr_offset_ptr = sbr_offset[3];\n break;\n case 44100: case 48000: case 64000:\n sbr_offset_ptr = sbr_offset[4];\n break;\n case 88200: case 96000: case 128000: case 176400: case 192000:\n sbr_offset_ptr = sbr_offset[5];\n break;\n default:\n av_log(ac->avccontext, AV_LOG_ERROR,\n "Unsupported sample rate for SBR: %d\\n", sbr->sample_rate);\n return -1;\n }\n sbr->k[0] = start_min + sbr_offset_ptr[spectrum->bs_start_freq];\n if (spectrum->bs_stop_freq < 14) {\n sbr->k[2] = stop_min;\n make_bands(stop_dk, stop_min, 64, 13);\n qsort(stop_dk, 13, sizeof(stop_dk[0]), qsort_comparison_function_int16);\n for (k = 0; k < spectrum->bs_stop_freq; k++)\n sbr->k[2] += stop_dk[k];\n } else if (spectrum->bs_stop_freq == 14) {\n sbr->k[2] = 2*sbr->k[0];\n } else if (spectrum->bs_stop_freq == 15) {\n sbr->k[2] = 3*sbr->k[0];\n } else {\n av_log(ac->avccontext, AV_LOG_ERROR,\n "Invalid bs_stop_freq: %d\\n", spectrum->bs_stop_freq);\n return -1;\n }\n sbr->k[2] = FFMIN(64, sbr->k[2]);\n if (sbr->sample_rate <= 32000) {\n max_qmf_subbands = 48;\n } else if (sbr->sample_rate == 44100) {\n max_qmf_subbands = 35;\n } else if (sbr->sample_rate >= 48000)\n max_qmf_subbands = 32;\n if (sbr->k[2] - sbr->k[0] > max_qmf_subbands) {\n av_log(ac->avccontext, AV_LOG_ERROR,\n "Invalid bitstream, too many QMF subbands: %d\\n", sbr->k[2] - sbr->k[0]);\n return -1;\n }\n if (!spectrum->bs_freq_scale) {\n unsigned int dk;\n int k2diff;\n dk = spectrum->bs_alter_scale + 1;\n sbr->n_master = ((sbr->k[2] - sbr->k[0] + (dk&2)) >> dk) << 1;\n if (check_n_master(ac->avccontext, sbr->n_master, sbr->spectrum_params.bs_xover_band))\n return -1;\n for (k = 1; k <= sbr->n_master; k++)\n sbr->f_master[k] = dk;\n k2diff = sbr->k[2] - sbr->k[0] - sbr->n_master * dk;\n if (k2diff < 0) {\n sbr->f_master[1]--;\n sbr->f_master[2]-= (k2diff < 1);\n } else if (k2diff) {\n sbr->f_master[sbr->n_master]++;\n }\n sbr->f_master[0] = sbr->k[0];\n for (k = 1; k <= sbr->n_master; k++)\n sbr->f_master[k] += sbr->f_master[k - 1];\n } else {\n int half_bands = 7 - spectrum->bs_freq_scale;\n int two_regions, num_bands_0;\n int vdk0_max, vdk1_min;\n int16_t vk0[49];\n if (49 * sbr->k[2] > 110 * sbr->k[0]) {\n two_regions = 1;\n sbr->k[1] = 2 * sbr->k[0];\n } else {\n two_regions = 0;\n sbr->k[1] = sbr->k[2];\n }\n num_bands_0 = lrintf(half_bands * log2f(sbr->k[1] / (float)sbr->k[0])) * 2;\n if (num_bands_0 <= 0) {\n av_log(ac->avccontext, AV_LOG_ERROR, "Invalid num_bands_0: %d\\n", num_bands_0);\n return -1;\n }\n vk0[0] = 0;\n make_bands(vk0+1, sbr->k[0], sbr->k[1], num_bands_0);\n qsort(vk0 + 1, num_bands_0, sizeof(vk0[1]), qsort_comparison_function_int16);\n vdk0_max = vk0[num_bands_0];\n vk0[0] = sbr->k[0];\n for (k = 1; k <= num_bands_0; k++) {\n if (vk0[k] <= 0) {\n av_log(ac->avccontext, AV_LOG_ERROR, "Invalid vDk0[%d]: %d\\n", k, vk0[k]);\n return -1;\n }\n vk0[k] += vk0[k-1];\n }\n if (two_regions) {\n int16_t vk1[49];\n float invwarp = spectrum->bs_alter_scale ? 0.76923076923076923077f\n : 1.0f;\n int num_bands_1 = lrintf(half_bands * invwarp *\n log2f(sbr->k[2] / (float)sbr->k[1])) * 2;\n make_bands(vk1+1, sbr->k[1], sbr->k[2], num_bands_1);\n vdk1_min = array_min_int16(vk1 + 1, num_bands_1);\n if (vdk1_min < vdk0_max) {\n int change;\n qsort(vk1 + 1, num_bands_1, sizeof(vk1[1]), qsort_comparison_function_int16);\n change = FFMIN(vdk0_max - vk1[1], (vk1[num_bands_1] - vk1[1]) >> 1);\n vk1[1] += change;\n vk1[num_bands_1] -= change;\n }\n qsort(vk1 + 1, num_bands_1, sizeof(vk1[1]), qsort_comparison_function_int16);\n vk1[0] = sbr->k[1];\n for (k = 1; k <= num_bands_1; k++) {\n if (vk1[k] <= 0) {\n av_log(ac->avccontext, AV_LOG_ERROR, "Invalid vDk1[%d]: %d\\n", k, vk1[k]);\n return -1;\n }\n vk1[k] += vk1[k-1];\n }\n sbr->n_master = num_bands_0 + num_bands_1;\n if (check_n_master(ac->avccontext, sbr->n_master, sbr->spectrum_params.bs_xover_band))\n return -1;\n memcpy(&sbr->f_master[0], vk0,\n (num_bands_0 + 1) * sizeof(sbr->f_master[0]));\n memcpy(&sbr->f_master[num_bands_0 + 1], vk1 + 1,\n num_bands_1 * sizeof(sbr->f_master[0]));\n } else {\n sbr->n_master = num_bands_0;\n if (check_n_master(ac->avccontext, sbr->n_master, sbr->spectrum_params.bs_xover_band))\n return -1;\n memcpy(sbr->f_master, vk0, (num_bands_0 + 1) * sizeof(sbr->f_master[0]));\n }\n }\n return 0;\n}']
|
2,116
| 0
|
https://github.com/openssl/openssl/blob/aa24c4a736b095bfaa0698bf87e61bec7b5d8691/crypto/bn/bn_lib.c/#L598
|
BIGNUM *BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret)
{
unsigned int i,m;
unsigned int n;
BN_ULONG l;
BIGNUM *bn = NULL;
if (ret == NULL)
ret = bn = BN_new();
if (ret == NULL) return(NULL);
bn_check_top(ret);
l=0;
n=len;
if (n == 0)
{
ret->top=0;
return(ret);
}
i=((n-1)/BN_BYTES)+1;
m=((n-1)%(BN_BYTES));
if (bn_wexpand(ret, (int)i) == NULL)
{
if (bn) BN_free(bn);
return NULL;
}
ret->top=i;
ret->neg=0;
while (n--)
{
l=(l<<8L)| *(s++);
if (m-- == 0)
{
ret->d[--i]=l;
l=0;
m=BN_BYTES-1;
}
}
bn_correct_top(ret);
return(ret);
}
|
['static int SRP_user_pwd_set_sv(SRP_user_pwd *vinfo, const char *s,\n\t\t\t\t\t\t\t const char *v)\n\t{\n\tunsigned char tmp[MAX_LEN];\n\tint len;\n\tif (strlen(s) > MAX_LEN || strlen(v) > MAX_LEN)\n\t\treturn 0;\n\tlen = t_fromb64(tmp, v);\n\tif (NULL == (vinfo->v = BN_bin2bn(tmp, len, NULL)) )\n\t\treturn 0;\n\tlen = t_fromb64(tmp, s);\n\treturn ((vinfo->s = BN_bin2bn(tmp, len, NULL)) != NULL) ;\n\t}', "static int t_fromb64(unsigned char *a, const char *src)\n\t{\n\tchar *loc;\n\tint i, j;\n\tint size;\n\twhile(*src && (*src == ' ' || *src == '\\t' || *src == '\\n'))\n\t\t++src;\n\tsize = strlen((const char *)src);\n\ti = 0;\n\twhile(i < size)\n\t\t{\n\t\tloc = strchr(b64table, src[i]);\n\t\tif(loc == (char *) 0) break;\n\t\telse a[i] = loc - b64table;\n\t\t++i;\n\t\t}\n\tsize = i;\n\ti = size - 1;\n\tj = size;\n\twhile(1)\n\t\t{\n\t\ta[j] = a[i];\n\t\tif(--i < 0) break;\n\t\ta[j] |= (a[i] & 3) << 6;\n\t\t--j;\n\t\ta[j] = (unsigned char) ((a[i] & 0x3c) >> 2);\n\t\tif(--i < 0) break;\n\t\ta[j] |= (a[i] & 0xf) << 4;\n\t\t--j;\n\t\ta[j] = (unsigned char) ((a[i] & 0x30) >> 4);\n\t\tif(--i < 0) break;\n\t\ta[j] |= (a[i] << 2);\n\t\ta[--j] = 0;\n\t\tif(--i < 0) break;\n\t\t}\n\twhile(a[j] == 0 && j <= size) ++j;\n\ti = 0;\n\twhile (j <= size) a[i++] = a[j++];\n\treturn i;\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\tBIGNUM *bn = NULL;\n\tif (ret == NULL)\n\t\tret = bn = BN_new();\n\tif (ret == NULL) return(NULL);\n\tbn_check_top(ret);\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\ti=((n-1)/BN_BYTES)+1;\n\tm=((n-1)%(BN_BYTES));\n\tif (bn_wexpand(ret, (int)i) == NULL)\n\t\t{\n\t\tif (bn) BN_free(bn);\n\t\treturn NULL;\n\t\t}\n\tret->top=i;\n\tret->neg=0;\n\twhile (n--)\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_correct_top(ret);\n\treturn(ret);\n\t}']
|
2,117
| 0
|
https://github.com/libav/libav/blob/dad7a9c7c0ae8ebc56f2e3a24e6fa4da5c2cd491/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_init8(&bc, buf, len);\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 int bitstream_init8(BitstreamContext *bc, const uint8_t *buffer,\n unsigned byte_size)\n{\n if (byte_size > INT_MAX / 8)\n return AVERROR_INVALIDDATA;\n return bitstream_init(bc, buffer, byte_size * 8);\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}']
|
2,118
| 0
|
https://github.com/openssl/openssl/blob/ac33c5a477568127ad99b1260a8978477de50e36/crypto/evp/e_aes.c/#L2618
|
static int aes_ocb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
const unsigned char *in, size_t len)
{
unsigned char *buf;
int *buf_len;
int written_len = 0;
size_t trailing_len;
EVP_AES_OCB_CTX *octx = EVP_C_DATA(EVP_AES_OCB_CTX,ctx);
if (!octx->iv_set)
return -1;
if (!octx->key_set)
return -1;
if (in) {
if (out == NULL) {
buf = octx->aad_buf;
buf_len = &(octx->aad_buf_len);
} else {
buf = octx->data_buf;
buf_len = &(octx->data_buf_len);
}
if (*buf_len) {
unsigned int remaining;
remaining = 16 - (*buf_len);
if (remaining > len) {
memcpy(buf + (*buf_len), in, len);
*(buf_len) += len;
return 0;
}
memcpy(buf + (*buf_len), in, remaining);
len -= remaining;
in += remaining;
if (out == NULL) {
if (!CRYPTO_ocb128_aad(&octx->ocb, buf, 16))
return -1;
} else if (EVP_CIPHER_CTX_encrypting(ctx)) {
if (!CRYPTO_ocb128_encrypt(&octx->ocb, buf, out, 16))
return -1;
} else {
if (!CRYPTO_ocb128_decrypt(&octx->ocb, buf, out, 16))
return -1;
}
written_len = 16;
*buf_len = 0;
}
trailing_len = len % 16;
if (len != trailing_len) {
if (out == NULL) {
if (!CRYPTO_ocb128_aad(&octx->ocb, in, len - trailing_len))
return -1;
} else if (EVP_CIPHER_CTX_encrypting(ctx)) {
if (!CRYPTO_ocb128_encrypt
(&octx->ocb, in, out, len - trailing_len))
return -1;
} else {
if (!CRYPTO_ocb128_decrypt
(&octx->ocb, in, out, len - trailing_len))
return -1;
}
written_len += len - trailing_len;
in += len - trailing_len;
}
if (trailing_len) {
memcpy(buf, in, trailing_len);
*buf_len = trailing_len;
}
return written_len;
} else {
if (octx->data_buf_len) {
if (EVP_CIPHER_CTX_encrypting(ctx)) {
if (!CRYPTO_ocb128_encrypt(&octx->ocb, octx->data_buf, out,
octx->data_buf_len))
return -1;
} else {
if (!CRYPTO_ocb128_decrypt(&octx->ocb, octx->data_buf, out,
octx->data_buf_len))
return -1;
}
written_len = octx->data_buf_len;
octx->data_buf_len = 0;
}
if (octx->aad_buf_len) {
if (!CRYPTO_ocb128_aad
(&octx->ocb, octx->aad_buf, octx->aad_buf_len))
return -1;
octx->aad_buf_len = 0;
}
if (!EVP_CIPHER_CTX_encrypting(ctx)) {
if (octx->taglen < 0)
return -1;
if (CRYPTO_ocb128_finish(&octx->ocb,
octx->tag, octx->taglen) != 0)
return -1;
octx->iv_set = 0;
return written_len;
}
if (CRYPTO_ocb128_tag(&octx->ocb, octx->tag, 16) != 1)
return -1;
octx->iv_set = 0;
return written_len;
}
}
|
['static int aes_ocb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,\n const unsigned char *in, size_t len)\n{\n unsigned char *buf;\n int *buf_len;\n int written_len = 0;\n size_t trailing_len;\n EVP_AES_OCB_CTX *octx = EVP_C_DATA(EVP_AES_OCB_CTX,ctx);\n if (!octx->iv_set)\n return -1;\n if (!octx->key_set)\n return -1;\n if (in) {\n if (out == NULL) {\n buf = octx->aad_buf;\n buf_len = &(octx->aad_buf_len);\n } else {\n buf = octx->data_buf;\n buf_len = &(octx->data_buf_len);\n }\n if (*buf_len) {\n unsigned int remaining;\n remaining = 16 - (*buf_len);\n if (remaining > len) {\n memcpy(buf + (*buf_len), in, len);\n *(buf_len) += len;\n return 0;\n }\n memcpy(buf + (*buf_len), in, remaining);\n len -= remaining;\n in += remaining;\n if (out == NULL) {\n if (!CRYPTO_ocb128_aad(&octx->ocb, buf, 16))\n return -1;\n } else if (EVP_CIPHER_CTX_encrypting(ctx)) {\n if (!CRYPTO_ocb128_encrypt(&octx->ocb, buf, out, 16))\n return -1;\n } else {\n if (!CRYPTO_ocb128_decrypt(&octx->ocb, buf, out, 16))\n return -1;\n }\n written_len = 16;\n *buf_len = 0;\n }\n trailing_len = len % 16;\n if (len != trailing_len) {\n if (out == NULL) {\n if (!CRYPTO_ocb128_aad(&octx->ocb, in, len - trailing_len))\n return -1;\n } else if (EVP_CIPHER_CTX_encrypting(ctx)) {\n if (!CRYPTO_ocb128_encrypt\n (&octx->ocb, in, out, len - trailing_len))\n return -1;\n } else {\n if (!CRYPTO_ocb128_decrypt\n (&octx->ocb, in, out, len - trailing_len))\n return -1;\n }\n written_len += len - trailing_len;\n in += len - trailing_len;\n }\n if (trailing_len) {\n memcpy(buf, in, trailing_len);\n *buf_len = trailing_len;\n }\n return written_len;\n } else {\n if (octx->data_buf_len) {\n if (EVP_CIPHER_CTX_encrypting(ctx)) {\n if (!CRYPTO_ocb128_encrypt(&octx->ocb, octx->data_buf, out,\n octx->data_buf_len))\n return -1;\n } else {\n if (!CRYPTO_ocb128_decrypt(&octx->ocb, octx->data_buf, out,\n octx->data_buf_len))\n return -1;\n }\n written_len = octx->data_buf_len;\n octx->data_buf_len = 0;\n }\n if (octx->aad_buf_len) {\n if (!CRYPTO_ocb128_aad\n (&octx->ocb, octx->aad_buf, octx->aad_buf_len))\n return -1;\n octx->aad_buf_len = 0;\n }\n if (!EVP_CIPHER_CTX_encrypting(ctx)) {\n if (octx->taglen < 0)\n return -1;\n if (CRYPTO_ocb128_finish(&octx->ocb,\n octx->tag, octx->taglen) != 0)\n return -1;\n octx->iv_set = 0;\n return written_len;\n }\n if (CRYPTO_ocb128_tag(&octx->ocb, octx->tag, 16) != 1)\n return -1;\n octx->iv_set = 0;\n return written_len;\n }\n}']
|
2,119
| 0
|
https://github.com/libav/libav/blob/fd7b11d027da7cc350d867d22d4c6bbe6022d8df/libavfilter/avfilter.c/#L319
|
void avfilter_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
{
uint8_t *src[4], *dst[4];
int i, j, vsub;
void (*draw_slice)(AVFilterLink *, int, int, int);
FF_DPRINTF_START(NULL, draw_slice); ff_dprintf_link(NULL, link, 0); dprintf(NULL, " y:%d h:%d dir:%d\n", y, h, slice_dir);
if(link->src_buf) {
vsub = av_pix_fmt_descriptors[link->format].log2_chroma_h;
for(i = 0; i < 4; i ++) {
if(link->src_buf->data[i]) {
src[i] = link->src_buf-> data[i] +
(y >> (i==0 ? 0 : vsub)) * link->src_buf-> linesize[i];
dst[i] = link->cur_buf->data[i] +
(y >> (i==0 ? 0 : vsub)) * link->cur_buf->linesize[i];
} else
src[i] = dst[i] = NULL;
}
for(i = 0; i < 4; i ++) {
int planew =
ff_get_plane_bytewidth(link->format, link->cur_buf->w, i);
if(!src[i]) continue;
for(j = 0; j < h >> (i==0 ? 0 : vsub); j ++) {
memcpy(dst[i], src[i], planew);
src[i] += link->src_buf ->linesize[i];
dst[i] += link->cur_buf->linesize[i];
}
}
}
if(!(draw_slice = link_dpad(link).draw_slice))
draw_slice = avfilter_default_draw_slice;
draw_slice(link, y, h, slice_dir);
}
|
['void avfilter_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)\n{\n uint8_t *src[4], *dst[4];\n int i, j, vsub;\n void (*draw_slice)(AVFilterLink *, int, int, int);\n FF_DPRINTF_START(NULL, draw_slice); ff_dprintf_link(NULL, link, 0); dprintf(NULL, " y:%d h:%d dir:%d\\n", y, h, slice_dir);\n if(link->src_buf) {\n vsub = av_pix_fmt_descriptors[link->format].log2_chroma_h;\n for(i = 0; i < 4; i ++) {\n if(link->src_buf->data[i]) {\n src[i] = link->src_buf-> data[i] +\n (y >> (i==0 ? 0 : vsub)) * link->src_buf-> linesize[i];\n dst[i] = link->cur_buf->data[i] +\n (y >> (i==0 ? 0 : vsub)) * link->cur_buf->linesize[i];\n } else\n src[i] = dst[i] = NULL;\n }\n for(i = 0; i < 4; i ++) {\n int planew =\n ff_get_plane_bytewidth(link->format, link->cur_buf->w, i);\n if(!src[i]) continue;\n for(j = 0; j < h >> (i==0 ? 0 : vsub); j ++) {\n memcpy(dst[i], src[i], planew);\n src[i] += link->src_buf ->linesize[i];\n dst[i] += link->cur_buf->linesize[i];\n }\n }\n }\n if(!(draw_slice = link_dpad(link).draw_slice))\n draw_slice = avfilter_default_draw_slice;\n draw_slice(link, y, h, slice_dir);\n}']
|
2,120
| 0
|
https://gitlab.com/libtiff/libtiff/blob/709e93ded0000128625a23838756a408ea30745d/tools/tiffcrop.c/#L7453
|
static int
createCroppedImage(struct image_data *image, struct crop_mask *crop,
unsigned char **read_buff_ptr, unsigned char **crop_buff_ptr)
{
tsize_t cropsize;
unsigned char *read_buff = NULL;
unsigned char *crop_buff = NULL;
unsigned char *new_buff = NULL;
static tsize_t prev_cropsize = 0;
read_buff = *read_buff_ptr;
crop_buff = read_buff;
*crop_buff_ptr = read_buff;
crop->combined_width = image->width;
crop->combined_length = image->length;
cropsize = crop->bufftotal;
crop_buff = *crop_buff_ptr;
if (!crop_buff)
{
crop_buff = (unsigned char *)_TIFFmalloc(cropsize);
*crop_buff_ptr = crop_buff;
_TIFFmemset(crop_buff, 0, cropsize);
prev_cropsize = cropsize;
}
else
{
if (prev_cropsize < cropsize)
{
new_buff = _TIFFrealloc(crop_buff, cropsize);
if (!new_buff)
{
free (crop_buff);
crop_buff = (unsigned char *)_TIFFmalloc(cropsize);
}
else
crop_buff = new_buff;
_TIFFmemset(crop_buff, 0, cropsize);
}
}
if (!crop_buff)
{
TIFFError("createCroppedImage", "Unable to allocate/reallocate crop buffer");
return (-1);
}
*crop_buff_ptr = crop_buff;
if (crop->crop_mode & CROP_INVERT)
{
switch (crop->photometric)
{
case PHOTOMETRIC_MINISWHITE:
case PHOTOMETRIC_MINISBLACK:
image->photometric = crop->photometric;
break;
case INVERT_DATA_ONLY:
case INVERT_DATA_AND_TAG:
if (invertImage(image->photometric, image->spp, image->bps,
crop->combined_width, crop->combined_length, crop_buff))
{
TIFFError("createCroppedImage",
"Failed to invert colorspace for image or cropped selection");
return (-1);
}
if (crop->photometric == INVERT_DATA_AND_TAG)
{
switch (image->photometric)
{
case PHOTOMETRIC_MINISWHITE:
image->photometric = PHOTOMETRIC_MINISBLACK;
break;
case PHOTOMETRIC_MINISBLACK:
image->photometric = PHOTOMETRIC_MINISWHITE;
break;
default:
break;
}
}
break;
default: break;
}
}
if (crop->crop_mode & CROP_MIRROR)
{
if (mirrorImage(image->spp, image->bps, crop->mirror,
crop->combined_width, crop->combined_length, crop_buff))
{
TIFFError("createCroppedImage", "Failed to mirror image or cropped selection %s",
(crop->rotation == MIRROR_HORIZ) ? "horizontally" : "vertically");
return (-1);
}
}
if (crop->crop_mode & CROP_ROTATE)
{
if (rotateImage(crop->rotation, image, &crop->combined_width,
&crop->combined_length, crop_buff_ptr))
{
TIFFError("createCroppedImage",
"Failed to rotate image or cropped selection by %d degrees", crop->rotation);
return (-1);
}
}
if (crop_buff == read_buff)
*read_buff_ptr = NULL;
return (0);
}
|
['static int\ncreateCroppedImage(struct image_data *image, struct crop_mask *crop,\n unsigned char **read_buff_ptr, unsigned char **crop_buff_ptr)\n {\n tsize_t cropsize;\n unsigned char *read_buff = NULL;\n unsigned char *crop_buff = NULL;\n unsigned char *new_buff = NULL;\n static tsize_t prev_cropsize = 0;\n read_buff = *read_buff_ptr;\n crop_buff = read_buff;\n *crop_buff_ptr = read_buff;\n crop->combined_width = image->width;\n crop->combined_length = image->length;\n cropsize = crop->bufftotal;\n crop_buff = *crop_buff_ptr;\n if (!crop_buff)\n {\n crop_buff = (unsigned char *)_TIFFmalloc(cropsize);\n *crop_buff_ptr = crop_buff;\n _TIFFmemset(crop_buff, 0, cropsize);\n prev_cropsize = cropsize;\n }\n else\n {\n if (prev_cropsize < cropsize)\n {\n new_buff = _TIFFrealloc(crop_buff, cropsize);\n if (!new_buff)\n {\n\tfree (crop_buff);\n crop_buff = (unsigned char *)_TIFFmalloc(cropsize);\n }\n else\n crop_buff = new_buff;\n _TIFFmemset(crop_buff, 0, cropsize);\n }\n }\n if (!crop_buff)\n {\n TIFFError("createCroppedImage", "Unable to allocate/reallocate crop buffer");\n return (-1);\n }\n *crop_buff_ptr = crop_buff;\n if (crop->crop_mode & CROP_INVERT)\n {\n switch (crop->photometric)\n {\n case PHOTOMETRIC_MINISWHITE:\n case PHOTOMETRIC_MINISBLACK:\n\t image->photometric = crop->photometric;\n\t break;\n case INVERT_DATA_ONLY:\n case INVERT_DATA_AND_TAG:\n if (invertImage(image->photometric, image->spp, image->bps,\n crop->combined_width, crop->combined_length, crop_buff))\n {\n TIFFError("createCroppedImage",\n "Failed to invert colorspace for image or cropped selection");\n return (-1);\n }\n if (crop->photometric == INVERT_DATA_AND_TAG)\n {\n switch (image->photometric)\n {\n case PHOTOMETRIC_MINISWHITE:\n \t image->photometric = PHOTOMETRIC_MINISBLACK;\n\t break;\n case PHOTOMETRIC_MINISBLACK:\n \t image->photometric = PHOTOMETRIC_MINISWHITE;\n\t break;\n default:\n\t break;\n\t }\n\t }\n break;\n default: break;\n }\n }\n if (crop->crop_mode & CROP_MIRROR)\n {\n if (mirrorImage(image->spp, image->bps, crop->mirror,\n crop->combined_width, crop->combined_length, crop_buff))\n {\n TIFFError("createCroppedImage", "Failed to mirror image or cropped selection %s",\n\t (crop->rotation == MIRROR_HORIZ) ? "horizontally" : "vertically");\n return (-1);\n }\n }\n if (crop->crop_mode & CROP_ROTATE)\n {\n if (rotateImage(crop->rotation, image, &crop->combined_width,\n &crop->combined_length, crop_buff_ptr))\n {\n TIFFError("createCroppedImage",\n "Failed to rotate image or cropped selection by %d degrees", crop->rotation);\n return (-1);\n }\n }\n if (crop_buff == read_buff)\n *read_buff_ptr = NULL;\n return (0);\n }', 'void*\n_TIFFrealloc(void* p, tmsize_t s)\n{\n\treturn (realloc(p, (size_t) s));\n}', 'void*\n_TIFFmalloc(tmsize_t s)\n{\n\treturn (malloc((size_t) s));\n}', 'void\n_TIFFmemset(void* p, int v, tmsize_t c)\n{\n\tmemset(p, v, (size_t) c);\n}']
|
2,121
| 0
|
https://github.com/openssl/openssl/blob/0f3ffbd1581fad58095fedcc32b0da42a486b8b7/crypto/srp/srp_lib.c/#L150
|
BIGNUM *SRP_Calc_x(const BIGNUM *s, const char *user, const char *pass)
{
unsigned char dig[SHA_DIGEST_LENGTH];
EVP_MD_CTX *ctxt;
unsigned char *cs = NULL;
BIGNUM *res = NULL;
if ((s == NULL) || (user == NULL) || (pass == NULL))
return NULL;
ctxt = EVP_MD_CTX_new();
if (ctxt == NULL)
return NULL;
if ((cs = OPENSSL_malloc(BN_num_bytes(s))) == NULL)
goto err;
if (!EVP_DigestInit_ex(ctxt, EVP_sha1(), NULL)
|| !EVP_DigestUpdate(ctxt, user, strlen(user))
|| !EVP_DigestUpdate(ctxt, ":", 1)
|| !EVP_DigestUpdate(ctxt, pass, strlen(pass))
|| !EVP_DigestFinal_ex(ctxt, dig, NULL)
|| !EVP_DigestInit_ex(ctxt, EVP_sha1(), NULL))
goto err;
BN_bn2bin(s, cs);
if (!EVP_DigestUpdate(ctxt, cs, BN_num_bytes(s)))
goto err;
if (!EVP_DigestUpdate(ctxt, dig, sizeof(dig))
|| !EVP_DigestFinal_ex(ctxt, dig, NULL))
goto err;
res = BN_bin2bn(dig, sizeof(dig), NULL);
err:
OPENSSL_free(cs);
EVP_MD_CTX_free(ctxt);
return res;
}
|
['BIGNUM *SRP_Calc_x(const BIGNUM *s, const char *user, const char *pass)\n{\n unsigned char dig[SHA_DIGEST_LENGTH];\n EVP_MD_CTX *ctxt;\n unsigned char *cs = NULL;\n BIGNUM *res = NULL;\n if ((s == NULL) || (user == NULL) || (pass == NULL))\n return NULL;\n ctxt = EVP_MD_CTX_new();\n if (ctxt == NULL)\n return NULL;\n if ((cs = OPENSSL_malloc(BN_num_bytes(s))) == NULL)\n goto err;\n if (!EVP_DigestInit_ex(ctxt, EVP_sha1(), NULL)\n || !EVP_DigestUpdate(ctxt, user, strlen(user))\n || !EVP_DigestUpdate(ctxt, ":", 1)\n || !EVP_DigestUpdate(ctxt, pass, strlen(pass))\n || !EVP_DigestFinal_ex(ctxt, dig, NULL)\n || !EVP_DigestInit_ex(ctxt, EVP_sha1(), NULL))\n goto err;\n BN_bn2bin(s, cs);\n if (!EVP_DigestUpdate(ctxt, cs, BN_num_bytes(s)))\n goto err;\n if (!EVP_DigestUpdate(ctxt, dig, sizeof(dig))\n || !EVP_DigestFinal_ex(ctxt, dig, NULL))\n goto err;\n res = BN_bin2bn(dig, sizeof(dig), NULL);\n err:\n OPENSSL_free(cs);\n EVP_MD_CTX_free(ctxt);\n return res;\n}', 'EVP_MD_CTX *EVP_MD_CTX_new(void)\n{\n return OPENSSL_zalloc(sizeof(EVP_MD_CTX));\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 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}', '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}', 'const EVP_MD *EVP_sha1(void)\n{\n return (&sha1_md);\n}', 'void CRYPTO_free(void *str, const char *file, int line)\n{\n if (free_impl != NULL && free_impl != &CRYPTO_free) {\n free_impl(str, file, line);\n return;\n }\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n if (call_malloc_debug) {\n CRYPTO_mem_debug_free(str, 0, file, line);\n free(str);\n CRYPTO_mem_debug_free(str, 1, file, line);\n } else {\n free(str);\n }\n#else\n free(str);\n#endif\n}', 'void EVP_MD_CTX_free(EVP_MD_CTX *ctx)\n{\n EVP_MD_CTX_reset(ctx);\n OPENSSL_free(ctx);\n}']
|
2,122
| 0
|
https://github.com/openssl/openssl/blob/f006217bb628d05a2d5b866ff252bd94e3477e1f/crypto/asn1/asn1_lib.c/#L172
|
static int asn1_get_length(const unsigned char **pp, int *inf, long *rl,
int max)
{
const unsigned char *p = *pp;
unsigned long ret = 0;
unsigned int i;
if (max-- < 1)
return (0);
if (*p == 0x80) {
*inf = 1;
ret = 0;
p++;
} else {
*inf = 0;
i = *p & 0x7f;
if (*(p++) & 0x80) {
if (max < (int)i)
return 0;
while (i && *p == 0) {
p++;
i--;
}
if (i > sizeof(long))
return 0;
while (i-- > 0) {
ret <<= 8L;
ret |= *(p++);
}
} else
ret = i;
}
if (ret > LONG_MAX)
return 0;
*pp = p;
*rl = (long)ret;
return (1);
}
|
['void *ASN1_item_d2i_bio(const ASN1_ITEM *it, BIO *in, void *x)\n{\n BUF_MEM *b = NULL;\n const unsigned char *p;\n void *ret = NULL;\n int len;\n len = asn1_d2i_read_bio(in, &b);\n if (len < 0)\n goto err;\n p = (const unsigned char *)b->data;\n ret = ASN1_item_d2i(x, &p, len, it);\n err:\n BUF_MEM_free(b);\n return (ret);\n}', 'static int asn1_d2i_read_bio(BIO *in, BUF_MEM **pb)\n{\n BUF_MEM *b;\n unsigned char *p;\n int i;\n size_t want = HEADER_SIZE;\n int eos = 0;\n size_t off = 0;\n size_t len = 0;\n const unsigned char *q;\n long slen;\n int inf, tag, xclass;\n b = BUF_MEM_new();\n if (b == NULL) {\n ASN1err(ASN1_F_ASN1_D2I_READ_BIO, ERR_R_MALLOC_FAILURE);\n return -1;\n }\n ERR_clear_error();\n for (;;) {\n if (want >= (len - off)) {\n want -= (len - off);\n if (len + want < len || !BUF_MEM_grow_clean(b, len + want)) {\n ASN1err(ASN1_F_ASN1_D2I_READ_BIO, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n i = BIO_read(in, &(b->data[len]), want);\n if ((i < 0) && ((len - off) == 0)) {\n ASN1err(ASN1_F_ASN1_D2I_READ_BIO, ASN1_R_NOT_ENOUGH_DATA);\n goto err;\n }\n if (i > 0) {\n if (len + i < len) {\n ASN1err(ASN1_F_ASN1_D2I_READ_BIO, ASN1_R_TOO_LONG);\n goto err;\n }\n len += i;\n }\n }\n p = (unsigned char *)&(b->data[off]);\n q = p;\n inf = ASN1_get_object(&q, &slen, &tag, &xclass, len - off);\n if (inf & 0x80) {\n unsigned long e;\n e = ERR_GET_REASON(ERR_peek_error());\n if (e != ASN1_R_TOO_LONG)\n goto err;\n else\n ERR_clear_error();\n }\n i = q - p;\n off += i;\n if (inf & 1) {\n eos++;\n if (eos < 0) {\n ASN1err(ASN1_F_ASN1_D2I_READ_BIO, ASN1_R_HEADER_TOO_LONG);\n goto err;\n }\n want = HEADER_SIZE;\n } else if (eos && (slen == 0) && (tag == V_ASN1_EOC)) {\n eos--;\n if (eos <= 0)\n break;\n else\n want = HEADER_SIZE;\n } else {\n want = slen;\n if (want > (len - off)) {\n want -= (len - off);\n if (want > INT_MAX ||\n len + want < len) {\n ASN1err(ASN1_F_ASN1_D2I_READ_BIO, ASN1_R_TOO_LONG);\n goto err;\n }\n if (!BUF_MEM_grow_clean(b, len + want)) {\n ASN1err(ASN1_F_ASN1_D2I_READ_BIO, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n while (want > 0) {\n i = BIO_read(in, &(b->data[len]), want);\n if (i <= 0) {\n ASN1err(ASN1_F_ASN1_D2I_READ_BIO,\n ASN1_R_NOT_ENOUGH_DATA);\n goto err;\n }\n len += i;\n want -= i;\n }\n }\n if (off + slen < off) {\n ASN1err(ASN1_F_ASN1_D2I_READ_BIO, ASN1_R_TOO_LONG);\n goto err;\n }\n off += slen;\n if (eos <= 0) {\n break;\n } else\n want = HEADER_SIZE;\n }\n }\n if (off > INT_MAX) {\n ASN1err(ASN1_F_ASN1_D2I_READ_BIO, ASN1_R_TOO_LONG);\n goto err;\n }\n *pb = b;\n return off;\n err:\n BUF_MEM_free(b);\n return -1;\n}', 'size_t BUF_MEM_grow_clean(BUF_MEM *str, size_t len)\n{\n char *ret;\n size_t n;\n if (str->length >= len) {\n memset(&str->data[len], 0, 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_CLEAN, 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_clear_realloc(str->data, str->max, n);\n if (ret == NULL) {\n BUFerr(BUF_F_BUF_MEM_GROW_CLEAN, 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}', 'ASN1_VALUE *ASN1_item_d2i(ASN1_VALUE **pval,\n const unsigned char **in, long len,\n const ASN1_ITEM *it)\n{\n ASN1_TLC c;\n ASN1_VALUE *ptmpval = NULL;\n if (!pval)\n pval = &ptmpval;\n asn1_tlc_clear_nc(&c);\n if (ASN1_item_ex_d2i(pval, in, len, it, -1, 0, 0, &c) > 0)\n return *pval;\n return NULL;\n}', 'int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,\n const ASN1_ITEM *it,\n int tag, int aclass, char opt, ASN1_TLC *ctx)\n{\n int rv;\n rv = asn1_item_embed_d2i(pval, in, len, it, tag, aclass, opt, ctx);\n if (rv <= 0)\n ASN1_item_ex_free(pval, it);\n return rv;\n}', 'static int asn1_item_embed_d2i(ASN1_VALUE **pval, const unsigned char **in,\n long len, const ASN1_ITEM *it,\n int tag, int aclass, char opt, ASN1_TLC *ctx)\n{\n const ASN1_TEMPLATE *tt, *errtt = NULL;\n const ASN1_EXTERN_FUNCS *ef;\n const ASN1_AUX *aux = it->funcs;\n ASN1_aux_cb *asn1_cb;\n const unsigned char *p = NULL, *q;\n unsigned char oclass;\n char seq_eoc, seq_nolen, cst, isopt;\n long tmplen;\n int i;\n int otag;\n int ret = 0;\n ASN1_VALUE **pchptr;\n if (!pval)\n return 0;\n if (aux && aux->asn1_cb)\n asn1_cb = aux->asn1_cb;\n else\n asn1_cb = 0;\n switch (it->itype) {\n case ASN1_ITYPE_PRIMITIVE:\n if (it->templates) {\n if ((tag != -1) || opt) {\n ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I,\n ASN1_R_ILLEGAL_OPTIONS_ON_ITEM_TEMPLATE);\n goto err;\n }\n return asn1_template_ex_d2i(pval, in, len,\n it->templates, opt, ctx);\n }\n return asn1_d2i_ex_primitive(pval, in, len, it,\n tag, aclass, opt, ctx);\n case ASN1_ITYPE_MSTRING:\n p = *in;\n ret = asn1_check_tlen(NULL, &otag, &oclass, NULL, NULL,\n &p, len, -1, 0, 1, ctx);\n if (!ret) {\n ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ERR_R_NESTED_ASN1_ERROR);\n goto err;\n }\n if (oclass != V_ASN1_UNIVERSAL) {\n if (opt)\n return -1;\n ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_MSTRING_NOT_UNIVERSAL);\n goto err;\n }\n if (!(ASN1_tag2bit(otag) & it->utype)) {\n if (opt)\n return -1;\n ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_MSTRING_WRONG_TAG);\n goto err;\n }\n return asn1_d2i_ex_primitive(pval, in, len, it, otag, 0, 0, ctx);\n case ASN1_ITYPE_EXTERN:\n ef = it->funcs;\n return ef->asn1_ex_d2i(pval, in, len, it, tag, aclass, opt, ctx);\n case ASN1_ITYPE_CHOICE:\n if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL))\n goto auxerr;\n if (*pval) {\n i = asn1_get_choice_selector(pval, it);\n if ((i >= 0) && (i < it->tcount)) {\n tt = it->templates + i;\n pchptr = asn1_get_field_ptr(pval, tt);\n asn1_template_free(pchptr, tt);\n asn1_set_choice_selector(pval, -1, it);\n }\n } else if (!ASN1_item_ex_new(pval, it)) {\n ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ERR_R_NESTED_ASN1_ERROR);\n goto err;\n }\n p = *in;\n for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {\n pchptr = asn1_get_field_ptr(pval, tt);\n ret = asn1_template_ex_d2i(pchptr, &p, len, tt, 1, ctx);\n if (ret == -1)\n continue;\n if (ret > 0)\n break;\n errtt = tt;\n ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ERR_R_NESTED_ASN1_ERROR);\n goto err;\n }\n if (i == it->tcount) {\n if (opt) {\n ASN1_item_ex_free(pval, it);\n return -1;\n }\n ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_NO_MATCHING_CHOICE_TYPE);\n goto err;\n }\n asn1_set_choice_selector(pval, i, it);\n if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL))\n goto auxerr;\n *in = p;\n return 1;\n case ASN1_ITYPE_NDEF_SEQUENCE:\n case ASN1_ITYPE_SEQUENCE:\n p = *in;\n tmplen = len;\n if (tag == -1) {\n tag = V_ASN1_SEQUENCE;\n aclass = V_ASN1_UNIVERSAL;\n }\n ret = asn1_check_tlen(&len, NULL, NULL, &seq_eoc, &cst,\n &p, len, tag, aclass, opt, ctx);\n if (!ret) {\n ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ERR_R_NESTED_ASN1_ERROR);\n goto err;\n } else if (ret == -1)\n return -1;\n if (aux && (aux->flags & ASN1_AFLG_BROKEN)) {\n len = tmplen - (p - *in);\n seq_nolen = 1;\n }\n else\n seq_nolen = seq_eoc;\n if (!cst) {\n ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_SEQUENCE_NOT_CONSTRUCTED);\n goto err;\n }\n if (!*pval && !ASN1_item_ex_new(pval, it)) {\n ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ERR_R_NESTED_ASN1_ERROR);\n goto err;\n }\n if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL))\n goto auxerr;\n for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {\n if (tt->flags & ASN1_TFLG_ADB_MASK) {\n const ASN1_TEMPLATE *seqtt;\n ASN1_VALUE **pseqval;\n seqtt = asn1_do_adb(pval, tt, 1);\n pseqval = asn1_get_field_ptr(pval, seqtt);\n asn1_template_free(pseqval, seqtt);\n }\n }\n for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {\n const ASN1_TEMPLATE *seqtt;\n ASN1_VALUE **pseqval;\n seqtt = asn1_do_adb(pval, tt, 1);\n if (!seqtt)\n goto err;\n pseqval = asn1_get_field_ptr(pval, seqtt);\n if (!len)\n break;\n q = p;\n if (asn1_check_eoc(&p, len)) {\n if (!seq_eoc) {\n ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_UNEXPECTED_EOC);\n goto err;\n }\n len -= p - q;\n seq_eoc = 0;\n q = p;\n break;\n }\n if (i == (it->tcount - 1))\n isopt = 0;\n else\n isopt = (char)(seqtt->flags & ASN1_TFLG_OPTIONAL);\n ret = asn1_template_ex_d2i(pseqval, &p, len, seqtt, isopt, ctx);\n if (!ret) {\n errtt = seqtt;\n goto err;\n } else if (ret == -1) {\n asn1_template_free(pseqval, seqtt);\n continue;\n }\n len -= p - q;\n }\n if (seq_eoc && !asn1_check_eoc(&p, len)) {\n ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_MISSING_EOC);\n goto err;\n }\n if (!seq_nolen && len) {\n ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_SEQUENCE_LENGTH_MISMATCH);\n goto err;\n }\n for (; i < it->tcount; tt++, i++) {\n const ASN1_TEMPLATE *seqtt;\n seqtt = asn1_do_adb(pval, tt, 1);\n if (!seqtt)\n goto err;\n if (seqtt->flags & ASN1_TFLG_OPTIONAL) {\n ASN1_VALUE **pseqval;\n pseqval = asn1_get_field_ptr(pval, seqtt);\n asn1_template_free(pseqval, seqtt);\n } else {\n errtt = seqtt;\n ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_FIELD_MISSING);\n goto err;\n }\n }\n if (!asn1_enc_save(pval, *in, p - *in, it))\n goto auxerr;\n if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL))\n goto auxerr;\n *in = p;\n return 1;\n default:\n return 0;\n }\n auxerr:\n ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_AUX_ERROR);\n err:\n if (errtt)\n ERR_add_error_data(4, "Field=", errtt->field_name,\n ", Type=", it->sname);\n else\n ERR_add_error_data(2, "Type=", it->sname);\n return 0;\n}', 'static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass,\n char *inf, char *cst,\n const unsigned char **in, long len,\n int exptag, int expclass, char opt, ASN1_TLC *ctx)\n{\n int i;\n int ptag, pclass;\n long plen;\n const unsigned char *p, *q;\n p = *in;\n q = p;\n if (ctx && ctx->valid) {\n i = ctx->ret;\n plen = ctx->plen;\n pclass = ctx->pclass;\n ptag = ctx->ptag;\n p += ctx->hdrlen;\n } else {\n i = ASN1_get_object(&p, &plen, &ptag, &pclass, len);\n if (ctx) {\n ctx->ret = i;\n ctx->plen = plen;\n ctx->pclass = pclass;\n ctx->ptag = ptag;\n ctx->hdrlen = p - q;\n ctx->valid = 1;\n if (!(i & 0x81) && ((plen + ctx->hdrlen) > len)) {\n ASN1err(ASN1_F_ASN1_CHECK_TLEN, ASN1_R_TOO_LONG);\n asn1_tlc_clear(ctx);\n return 0;\n }\n }\n }\n if (i & 0x80) {\n ASN1err(ASN1_F_ASN1_CHECK_TLEN, ASN1_R_BAD_OBJECT_HEADER);\n asn1_tlc_clear(ctx);\n return 0;\n }\n if (exptag >= 0) {\n if ((exptag != ptag) || (expclass != pclass)) {\n if (opt)\n return -1;\n asn1_tlc_clear(ctx);\n ASN1err(ASN1_F_ASN1_CHECK_TLEN, ASN1_R_WRONG_TAG);\n return 0;\n }\n asn1_tlc_clear(ctx);\n }\n if (i & 1)\n plen = len - (p - q);\n if (inf)\n *inf = i & 1;\n if (cst)\n *cst = i & V_ASN1_CONSTRUCTED;\n if (olen)\n *olen = plen;\n if (oclass)\n *oclass = pclass;\n if (otag)\n *otag = ptag;\n *in = p;\n return 1;\n}', 'int ASN1_get_object(const unsigned char **pp, long *plength, int *ptag,\n int *pclass, long omax)\n{\n int i, ret;\n long l;\n const unsigned char *p = *pp;\n int tag, xclass, inf;\n long max = omax;\n if (!max)\n goto err;\n ret = (*p & V_ASN1_CONSTRUCTED);\n xclass = (*p & V_ASN1_PRIVATE);\n i = *p & V_ASN1_PRIMITIVE_TAG;\n if (i == V_ASN1_PRIMITIVE_TAG) {\n p++;\n if (--max == 0)\n goto err;\n l = 0;\n while (*p & 0x80) {\n l <<= 7L;\n l |= *(p++) & 0x7f;\n if (--max == 0)\n goto err;\n if (l > (INT_MAX >> 7L))\n goto err;\n }\n l <<= 7L;\n l |= *(p++) & 0x7f;\n tag = (int)l;\n if (--max == 0)\n goto err;\n } else {\n tag = i;\n p++;\n if (--max == 0)\n goto err;\n }\n *ptag = tag;\n *pclass = xclass;\n if (!asn1_get_length(&p, &inf, plength, (int)max))\n goto err;\n if (inf && !(ret & V_ASN1_CONSTRUCTED))\n goto err;\n if (*plength > (omax - (p - *pp))) {\n ASN1err(ASN1_F_ASN1_GET_OBJECT, ASN1_R_TOO_LONG);\n ret |= 0x80;\n }\n *pp = p;\n return (ret | inf);\n err:\n ASN1err(ASN1_F_ASN1_GET_OBJECT, ASN1_R_HEADER_TOO_LONG);\n return (0x80);\n}', 'static int asn1_get_length(const unsigned char **pp, int *inf, long *rl,\n int max)\n{\n const unsigned char *p = *pp;\n unsigned long ret = 0;\n unsigned int i;\n if (max-- < 1)\n return (0);\n if (*p == 0x80) {\n *inf = 1;\n ret = 0;\n p++;\n } else {\n *inf = 0;\n i = *p & 0x7f;\n if (*(p++) & 0x80) {\n if (max < (int)i)\n return 0;\n while (i && *p == 0) {\n p++;\n i--;\n }\n if (i > sizeof(long))\n return 0;\n while (i-- > 0) {\n ret <<= 8L;\n ret |= *(p++);\n }\n } else\n ret = i;\n }\n if (ret > LONG_MAX)\n return 0;\n *pp = p;\n *rl = (long)ret;\n return (1);\n}']
|
2,123
| 0
|
https://github.com/libav/libav/blob/1e4dd198aff2f1071b88aba6ae873745e9c18a81/libavformat/flvdec.c/#L426
|
static int flv_read_packet(AVFormatContext *s, AVPacket *pkt)
{
FLVContext *flv = s->priv_data;
int ret, i, type, size, flags, is_audio;
int64_t next, pos;
int64_t dts, pts = AV_NOPTS_VALUE;
AVStream *st = NULL;
for(;;url_fskip(s->pb, 4)){
pos = url_ftell(s->pb);
type = get_byte(s->pb);
size = get_be24(s->pb);
dts = get_be24(s->pb);
dts |= get_byte(s->pb) << 24;
if (url_feof(s->pb))
return AVERROR_EOF;
url_fskip(s->pb, 3);
flags = 0;
if(size == 0)
continue;
next= size + url_ftell(s->pb);
if (type == FLV_TAG_TYPE_AUDIO) {
is_audio=1;
flags = get_byte(s->pb);
size--;
} else if (type == FLV_TAG_TYPE_VIDEO) {
is_audio=0;
flags = get_byte(s->pb);
size--;
if ((flags & 0xf0) == 0x50)
goto skip;
} else {
if (type == FLV_TAG_TYPE_META && size > 13+1+4)
flv_read_metabody(s, next);
else
av_log(s, AV_LOG_DEBUG, "skipping flv packet: type %d, size %d, flags %d\n", type, size, flags);
skip:
url_fseek(s->pb, next, SEEK_SET);
continue;
}
if (!size)
continue;
for(i=0;i<s->nb_streams;i++) {
st = s->streams[i];
if (st->id == is_audio)
break;
}
if(i == s->nb_streams){
av_log(s, AV_LOG_ERROR, "invalid stream\n");
st= create_stream(s, is_audio);
s->ctx_flags &= ~AVFMTCTX_NOHEADER;
}
if( (st->discard >= AVDISCARD_NONKEY && !((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY || is_audio))
||(st->discard >= AVDISCARD_BIDIR && ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_DISP_INTER && !is_audio))
|| st->discard >= AVDISCARD_ALL
){
url_fseek(s->pb, next, SEEK_SET);
continue;
}
if ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY)
av_add_index_entry(st, pos, dts, size, 0, AVINDEX_KEYFRAME);
break;
}
if(!url_is_streamed(s->pb) && (!s->duration || s->duration==AV_NOPTS_VALUE)){
int size;
const int64_t pos= url_ftell(s->pb);
const int64_t fsize= url_fsize(s->pb);
url_fseek(s->pb, fsize-4, SEEK_SET);
size= get_be32(s->pb);
url_fseek(s->pb, fsize-3-size, SEEK_SET);
if(size == get_be24(s->pb) + 11){
uint32_t ts = get_be24(s->pb);
ts |= get_byte(s->pb) << 24;
s->duration = ts * (int64_t)AV_TIME_BASE / 1000;
}
url_fseek(s->pb, pos, SEEK_SET);
}
if(is_audio){
if(!st->codec->channels || !st->codec->sample_rate || !st->codec->bits_per_coded_sample) {
st->codec->channels = (flags & FLV_AUDIO_CHANNEL_MASK) == FLV_STEREO ? 2 : 1;
st->codec->sample_rate = (44100 << ((flags & FLV_AUDIO_SAMPLERATE_MASK) >> FLV_AUDIO_SAMPLERATE_OFFSET) >> 3);
st->codec->bits_per_coded_sample = (flags & FLV_AUDIO_SAMPLESIZE_MASK) ? 16 : 8;
}
if(!st->codec->codec_id){
flv_set_audio_codec(s, st, flags & FLV_AUDIO_CODECID_MASK);
}
}else{
size -= flv_set_video_codec(s, st, flags & FLV_VIDEO_CODECID_MASK);
}
if (st->codec->codec_id == CODEC_ID_AAC ||
st->codec->codec_id == CODEC_ID_H264) {
int type = get_byte(s->pb);
size--;
if (st->codec->codec_id == CODEC_ID_H264) {
int32_t cts = (get_be24(s->pb)+0xff800000)^0xff800000;
pts = dts + cts;
if (cts < 0) {
flv->wrong_dts = 1;
av_log(s, AV_LOG_WARNING, "negative cts, previous timestamps might be wrong\n");
}
if (flv->wrong_dts)
dts = AV_NOPTS_VALUE;
}
if (type == 0) {
if ((ret = flv_get_extradata(s, st, size)) < 0)
return ret;
if (st->codec->codec_id == CODEC_ID_AAC) {
MPEG4AudioConfig cfg;
ff_mpeg4audio_get_config(&cfg, st->codec->extradata,
st->codec->extradata_size);
st->codec->channels = cfg.channels;
st->codec->sample_rate = cfg.sample_rate;
dprintf(s, "mp4a config channels %d sample rate %d\n",
st->codec->channels, st->codec->sample_rate);
}
ret = AVERROR(EAGAIN);
goto leave;
}
}
if (!size) {
ret = AVERROR(EAGAIN);
goto leave;
}
ret= av_get_packet(s->pb, pkt, size);
if (ret < 0) {
return AVERROR(EIO);
}
pkt->size = ret;
pkt->dts = dts;
pkt->pts = pts == AV_NOPTS_VALUE ? dts : pts;
pkt->stream_index = st->index;
if (is_audio || ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY))
pkt->flags |= AV_PKT_FLAG_KEY;
leave:
url_fskip(s->pb, 4);
return ret;
}
|
['static int flv_read_packet(AVFormatContext *s, AVPacket *pkt)\n{\n FLVContext *flv = s->priv_data;\n int ret, i, type, size, flags, is_audio;\n int64_t next, pos;\n int64_t dts, pts = AV_NOPTS_VALUE;\n AVStream *st = NULL;\n for(;;url_fskip(s->pb, 4)){\n pos = url_ftell(s->pb);\n type = get_byte(s->pb);\n size = get_be24(s->pb);\n dts = get_be24(s->pb);\n dts |= get_byte(s->pb) << 24;\n if (url_feof(s->pb))\n return AVERROR_EOF;\n url_fskip(s->pb, 3);\n flags = 0;\n if(size == 0)\n continue;\n next= size + url_ftell(s->pb);\n if (type == FLV_TAG_TYPE_AUDIO) {\n is_audio=1;\n flags = get_byte(s->pb);\n size--;\n } else if (type == FLV_TAG_TYPE_VIDEO) {\n is_audio=0;\n flags = get_byte(s->pb);\n size--;\n if ((flags & 0xf0) == 0x50)\n goto skip;\n } else {\n if (type == FLV_TAG_TYPE_META && size > 13+1+4)\n flv_read_metabody(s, next);\n else\n av_log(s, AV_LOG_DEBUG, "skipping flv packet: type %d, size %d, flags %d\\n", type, size, flags);\n skip:\n url_fseek(s->pb, next, SEEK_SET);\n continue;\n }\n if (!size)\n continue;\n for(i=0;i<s->nb_streams;i++) {\n st = s->streams[i];\n if (st->id == is_audio)\n break;\n }\n if(i == s->nb_streams){\n av_log(s, AV_LOG_ERROR, "invalid stream\\n");\n st= create_stream(s, is_audio);\n s->ctx_flags &= ~AVFMTCTX_NOHEADER;\n }\n if( (st->discard >= AVDISCARD_NONKEY && !((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY || is_audio))\n ||(st->discard >= AVDISCARD_BIDIR && ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_DISP_INTER && !is_audio))\n || st->discard >= AVDISCARD_ALL\n ){\n url_fseek(s->pb, next, SEEK_SET);\n continue;\n }\n if ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY)\n av_add_index_entry(st, pos, dts, size, 0, AVINDEX_KEYFRAME);\n break;\n }\n if(!url_is_streamed(s->pb) && (!s->duration || s->duration==AV_NOPTS_VALUE)){\n int size;\n const int64_t pos= url_ftell(s->pb);\n const int64_t fsize= url_fsize(s->pb);\n url_fseek(s->pb, fsize-4, SEEK_SET);\n size= get_be32(s->pb);\n url_fseek(s->pb, fsize-3-size, SEEK_SET);\n if(size == get_be24(s->pb) + 11){\n uint32_t ts = get_be24(s->pb);\n ts |= get_byte(s->pb) << 24;\n s->duration = ts * (int64_t)AV_TIME_BASE / 1000;\n }\n url_fseek(s->pb, pos, SEEK_SET);\n }\n if(is_audio){\n if(!st->codec->channels || !st->codec->sample_rate || !st->codec->bits_per_coded_sample) {\n st->codec->channels = (flags & FLV_AUDIO_CHANNEL_MASK) == FLV_STEREO ? 2 : 1;\n st->codec->sample_rate = (44100 << ((flags & FLV_AUDIO_SAMPLERATE_MASK) >> FLV_AUDIO_SAMPLERATE_OFFSET) >> 3);\n st->codec->bits_per_coded_sample = (flags & FLV_AUDIO_SAMPLESIZE_MASK) ? 16 : 8;\n }\n if(!st->codec->codec_id){\n flv_set_audio_codec(s, st, flags & FLV_AUDIO_CODECID_MASK);\n }\n }else{\n size -= flv_set_video_codec(s, st, flags & FLV_VIDEO_CODECID_MASK);\n }\n if (st->codec->codec_id == CODEC_ID_AAC ||\n st->codec->codec_id == CODEC_ID_H264) {\n int type = get_byte(s->pb);\n size--;\n if (st->codec->codec_id == CODEC_ID_H264) {\n int32_t cts = (get_be24(s->pb)+0xff800000)^0xff800000;\n pts = dts + cts;\n if (cts < 0) {\n flv->wrong_dts = 1;\n av_log(s, AV_LOG_WARNING, "negative cts, previous timestamps might be wrong\\n");\n }\n if (flv->wrong_dts)\n dts = AV_NOPTS_VALUE;\n }\n if (type == 0) {\n if ((ret = flv_get_extradata(s, st, size)) < 0)\n return ret;\n if (st->codec->codec_id == CODEC_ID_AAC) {\n MPEG4AudioConfig cfg;\n ff_mpeg4audio_get_config(&cfg, st->codec->extradata,\n st->codec->extradata_size);\n st->codec->channels = cfg.channels;\n st->codec->sample_rate = cfg.sample_rate;\n dprintf(s, "mp4a config channels %d sample rate %d\\n",\n st->codec->channels, st->codec->sample_rate);\n }\n ret = AVERROR(EAGAIN);\n goto leave;\n }\n }\n if (!size) {\n ret = AVERROR(EAGAIN);\n goto leave;\n }\n ret= av_get_packet(s->pb, pkt, size);\n if (ret < 0) {\n return AVERROR(EIO);\n }\n pkt->size = ret;\n pkt->dts = dts;\n pkt->pts = pts == AV_NOPTS_VALUE ? dts : pts;\n pkt->stream_index = st->index;\n if (is_audio || ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY))\n pkt->flags |= AV_PKT_FLAG_KEY;\nleave:\n url_fskip(s->pb, 4);\n return ret;\n}']
|
2,124
| 0
|
https://github.com/nginx/nginx/blob/94992aa62ee65812fef0c9f3e1c1b9d80e0badfe/src/http/ngx_http_core_module.c/#L1254
|
ngx_int_t
ngx_http_core_try_files_phase(ngx_http_request_t *r,
ngx_http_phase_handler_t *ph)
{
size_t len, root, alias, reserve, allocated;
u_char *p, *name;
ngx_str_t path, args;
ngx_uint_t test_dir;
ngx_http_try_file_t *tf;
ngx_open_file_info_t of;
ngx_http_script_code_pt code;
ngx_http_script_engine_t e;
ngx_http_core_loc_conf_t *clcf;
ngx_http_script_len_code_pt lcode;
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"try files phase: %ui", r->phase_handler);
clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
if (clcf->try_files == NULL) {
r->phase_handler++;
return NGX_AGAIN;
}
allocated = 0;
root = 0;
name = NULL;
path.data = NULL;
tf = clcf->try_files;
alias = clcf->alias;
for ( ;; ) {
if (tf->lengths) {
ngx_memzero(&e, sizeof(ngx_http_script_engine_t));
e.ip = tf->lengths->elts;
e.request = r;
len = 1;
while (*(uintptr_t *) e.ip) {
lcode = *(ngx_http_script_len_code_pt *) e.ip;
len += lcode(&e);
}
} else {
len = tf->name.len;
}
reserve = ngx_abs((ssize_t) (len - r->uri.len)) + alias + 16;
if (reserve > allocated) {
if (ngx_http_map_uri_to_path(r, &path, &root, reserve) == NULL) {
ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
return NGX_OK;
}
name = path.data + root;
allocated = path.len - root - (r->uri.len - alias);
}
if (tf->values == NULL) {
ngx_memcpy(name, tf->name.data, tf->name.len);
path.len = (name + tf->name.len - 1) - path.data;
} else {
e.ip = tf->values->elts;
e.pos = name;
e.flushed = 1;
while (*(uintptr_t *) e.ip) {
code = *(ngx_http_script_code_pt *) e.ip;
code((ngx_http_script_engine_t *) &e);
}
path.len = e.pos - path.data;
*e.pos = '\0';
if (alias && ngx_strncmp(name, clcf->name.data, alias) == 0) {
ngx_memmove(name, name + alias, len - alias);
path.len -= alias;
}
}
test_dir = tf->test_dir;
tf++;
ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"trying to use %s: \"%s\" \"%s\"",
test_dir ? "dir" : "file", name, path.data);
if (tf->lengths == NULL && tf->name.len == 0) {
if (tf->code) {
ngx_http_finalize_request(r, tf->code);
return NGX_OK;
}
path.len -= root;
path.data += root;
if (path.data[0] == '@') {
(void) ngx_http_named_location(r, &path);
} else {
ngx_http_split_args(r, &path, &args);
(void) ngx_http_internal_redirect(r, &path, &args);
}
ngx_http_finalize_request(r, NGX_DONE);
return NGX_OK;
}
ngx_memzero(&of, sizeof(ngx_open_file_info_t));
of.read_ahead = clcf->read_ahead;
of.directio = clcf->directio;
of.valid = clcf->open_file_cache_valid;
of.min_uses = clcf->open_file_cache_min_uses;
of.test_only = 1;
of.errors = clcf->open_file_cache_errors;
of.events = clcf->open_file_cache_events;
if (ngx_open_cached_file(clcf->open_file_cache, &path, &of, r->pool)
!= NGX_OK)
{
if (of.err != NGX_ENOENT
&& of.err != NGX_ENOTDIR
&& of.err != NGX_ENAMETOOLONG)
{
ngx_log_error(NGX_LOG_CRIT, r->connection->log, of.err,
"%s \"%s\" failed", of.failed, path.data);
}
continue;
}
if (of.is_dir && !test_dir) {
continue;
}
path.len -= root;
path.data += root;
if (!alias) {
r->uri = path;
#if (NGX_PCRE)
} else if (clcf->regex) {
if (!test_dir) {
r->uri = path;
r->add_uri_to_alias = 1;
}
#endif
} else {
r->uri.len = alias + path.len;
r->uri.data = ngx_pnalloc(r->pool, r->uri.len);
if (r->uri.data == NULL) {
ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
return NGX_OK;
}
p = ngx_copy(r->uri.data, clcf->name.data, alias);
ngx_memcpy(p, name, path.len);
}
ngx_http_set_exten(r);
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"try file uri: \"%V\"", &r->uri);
r->phase_handler++;
return NGX_AGAIN;
}
}
|
['ngx_int_t\nngx_http_core_try_files_phase(ngx_http_request_t *r,\n ngx_http_phase_handler_t *ph)\n{\n size_t len, root, alias, reserve, allocated;\n u_char *p, *name;\n ngx_str_t path, args;\n ngx_uint_t test_dir;\n ngx_http_try_file_t *tf;\n ngx_open_file_info_t of;\n ngx_http_script_code_pt code;\n ngx_http_script_engine_t e;\n ngx_http_core_loc_conf_t *clcf;\n ngx_http_script_len_code_pt lcode;\n ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,\n "try files phase: %ui", r->phase_handler);\n clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);\n if (clcf->try_files == NULL) {\n r->phase_handler++;\n return NGX_AGAIN;\n }\n allocated = 0;\n root = 0;\n name = NULL;\n path.data = NULL;\n tf = clcf->try_files;\n alias = clcf->alias;\n for ( ;; ) {\n if (tf->lengths) {\n ngx_memzero(&e, sizeof(ngx_http_script_engine_t));\n e.ip = tf->lengths->elts;\n e.request = r;\n len = 1;\n while (*(uintptr_t *) e.ip) {\n lcode = *(ngx_http_script_len_code_pt *) e.ip;\n len += lcode(&e);\n }\n } else {\n len = tf->name.len;\n }\n reserve = ngx_abs((ssize_t) (len - r->uri.len)) + alias + 16;\n if (reserve > allocated) {\n if (ngx_http_map_uri_to_path(r, &path, &root, reserve) == NULL) {\n ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);\n return NGX_OK;\n }\n name = path.data + root;\n allocated = path.len - root - (r->uri.len - alias);\n }\n if (tf->values == NULL) {\n ngx_memcpy(name, tf->name.data, tf->name.len);\n path.len = (name + tf->name.len - 1) - path.data;\n } else {\n e.ip = tf->values->elts;\n e.pos = name;\n e.flushed = 1;\n while (*(uintptr_t *) e.ip) {\n code = *(ngx_http_script_code_pt *) e.ip;\n code((ngx_http_script_engine_t *) &e);\n }\n path.len = e.pos - path.data;\n *e.pos = \'\\0\';\n if (alias && ngx_strncmp(name, clcf->name.data, alias) == 0) {\n ngx_memmove(name, name + alias, len - alias);\n path.len -= alias;\n }\n }\n test_dir = tf->test_dir;\n tf++;\n ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,\n "trying to use %s: \\"%s\\" \\"%s\\"",\n test_dir ? "dir" : "file", name, path.data);\n if (tf->lengths == NULL && tf->name.len == 0) {\n if (tf->code) {\n ngx_http_finalize_request(r, tf->code);\n return NGX_OK;\n }\n path.len -= root;\n path.data += root;\n if (path.data[0] == \'@\') {\n (void) ngx_http_named_location(r, &path);\n } else {\n ngx_http_split_args(r, &path, &args);\n (void) ngx_http_internal_redirect(r, &path, &args);\n }\n ngx_http_finalize_request(r, NGX_DONE);\n return NGX_OK;\n }\n ngx_memzero(&of, sizeof(ngx_open_file_info_t));\n of.read_ahead = clcf->read_ahead;\n of.directio = clcf->directio;\n of.valid = clcf->open_file_cache_valid;\n of.min_uses = clcf->open_file_cache_min_uses;\n of.test_only = 1;\n of.errors = clcf->open_file_cache_errors;\n of.events = clcf->open_file_cache_events;\n if (ngx_open_cached_file(clcf->open_file_cache, &path, &of, r->pool)\n != NGX_OK)\n {\n if (of.err != NGX_ENOENT\n && of.err != NGX_ENOTDIR\n && of.err != NGX_ENAMETOOLONG)\n {\n ngx_log_error(NGX_LOG_CRIT, r->connection->log, of.err,\n "%s \\"%s\\" failed", of.failed, path.data);\n }\n continue;\n }\n if (of.is_dir && !test_dir) {\n continue;\n }\n path.len -= root;\n path.data += root;\n if (!alias) {\n r->uri = path;\n#if (NGX_PCRE)\n } else if (clcf->regex) {\n if (!test_dir) {\n r->uri = path;\n r->add_uri_to_alias = 1;\n }\n#endif\n } else {\n r->uri.len = alias + path.len;\n r->uri.data = ngx_pnalloc(r->pool, r->uri.len);\n if (r->uri.data == NULL) {\n ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);\n return NGX_OK;\n }\n p = ngx_copy(r->uri.data, clcf->name.data, alias);\n ngx_memcpy(p, name, path.len);\n }\n ngx_http_set_exten(r);\n ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,\n "try file uri: \\"%V\\"", &r->uri);\n r->phase_handler++;\n return NGX_AGAIN;\n }\n}']
|
2,125
| 0
|
https://github.com/openssl/openssl/blob/d00b1d62d62036dc21c78658a28da4a6279e6fe2/crypto/bn/bn_ctx.c/#L354
|
static unsigned int BN_STACK_pop(BN_STACK *st)
{
return st->indexes[--(st->depth)];
}
|
['int ec_GFp_nist_field_mul(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,\n\tconst BIGNUM *b, BN_CTX *ctx)\n\t{\n\tint\tret=0;\n\tBN_CTX\t*ctx_new=NULL;\n\tif (!group || !r || !a || !b)\n\t\t{\n\t\tECerr(EC_F_EC_GFP_NIST_FIELD_MUL, ERR_R_PASSED_NULL_PARAMETER);\n\t\tgoto err;\n\t\t}\n\tif (!ctx)\n\t\tif ((ctx_new = ctx = BN_CTX_new()) == NULL) goto err;\n\tif (!BN_mul(r, a, b, ctx)) goto err;\n\tif (!group->field_mod_func(r, r, group->field, ctx))\n\t\tgoto err;\n\tret=1;\nerr:\n\tif (ctx_new)\n\t\tBN_CTX_free(ctx_new);\n\treturn ret;\n\t}', 'int BN_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)\n\t{\n\tint ret=0;\n\tint top,al,bl;\n\tBIGNUM *rr;\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n\tint i;\n#endif\n#ifdef BN_RECURSION\n\tBIGNUM *t=NULL;\n\tint j=0,k;\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\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\tBN_CTX_start(ctx);\n\tif ((r == a) || (r == b))\n\t\t{\n\t\tif ((rr = BN_CTX_get(ctx)) == NULL) goto err;\n\t\t}\n\telse\n\t\trr = r;\n\trr->neg=a->neg^b->neg;\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n\ti = al-bl;\n#endif\n#ifdef BN_MUL_COMBA\n\tif (i == 0)\n\t\t{\n# if 0\n\t\tif (al == 4)\n\t\t\t{\n\t\t\tif (bn_wexpand(rr,8) == NULL) goto err;\n\t\t\trr->top=8;\n\t\t\tbn_mul_comba4(rr->d,a->d,b->d);\n\t\t\tgoto end;\n\t\t\t}\n# endif\n\t\tif (al == 8)\n\t\t\t{\n\t\t\tif (bn_wexpand(rr,16) == NULL) goto err;\n\t\t\trr->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\t}\n#endif\n#ifdef BN_RECURSION\n\tif ((al >= BN_MULL_SIZE_NORMAL) && (bl >= BN_MULL_SIZE_NORMAL))\n\t\t{\n\t\tif (i >= -1 && i <= 1)\n\t\t\t{\n\t\t\tif (i >= 0)\n\t\t\t\t{\n\t\t\t\tj = BN_num_bits_word((BN_ULONG)al);\n\t\t\t\t}\n\t\t\tif (i == -1)\n\t\t\t\t{\n\t\t\t\tj = BN_num_bits_word((BN_ULONG)bl);\n\t\t\t\t}\n\t\t\tj = 1<<(j-1);\n\t\t\tassert(j <= al || j <= bl);\n\t\t\tk = j+j;\n\t\t\tt = BN_CTX_get(ctx);\n\t\t\tif (t == NULL)\n\t\t\t\tgoto err;\n\t\t\tif (al > j || bl > j)\n\t\t\t\t{\n\t\t\t\tif (bn_wexpand(t,k*4) == NULL) goto err;\n\t\t\t\tif (bn_wexpand(rr,k*4) == NULL) goto err;\n\t\t\t\tbn_mul_part_recursive(rr->d,a->d,b->d,\n\t\t\t\t\tj,al-j,bl-j,t->d);\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\tif (bn_wexpand(t,k*2) == NULL) goto err;\n\t\t\t\tif (bn_wexpand(rr,k*2) == NULL) goto err;\n\t\t\t\tbn_mul_recursive(rr->d,a->d,b->d,\n\t\t\t\t\tj,al-j,bl-j,t->d);\n\t\t\t\t}\n\t\t\trr->top=top;\n\t\t\tgoto end;\n\t\t\t}\n#if 0\n\t\tif (i == 1 && !BN_get_flags(b,BN_FLG_STATIC_DATA))\n\t\t\t{\n\t\t\tBIGNUM *tmp_bn = (BIGNUM *)b;\n\t\t\tif (bn_wexpand(tmp_bn,al) == NULL) goto err;\n\t\t\ttmp_bn->d[bl]=0;\n\t\t\tbl++;\n\t\t\ti--;\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\tBIGNUM *tmp_bn = (BIGNUM *)a;\n\t\t\tif (bn_wexpand(tmp_bn,bl) == NULL) goto err;\n\t\t\ttmp_bn->d[al]=0;\n\t\t\tal++;\n\t\t\ti++;\n\t\t\t}\n\t\tif (i == 0)\n\t\t\t{\n\t\t\tj=BN_num_bits_word((BN_ULONG)al);\n\t\t\tj=1<<(j-1);\n\t\t\tk=j+j;\n\t\t\tt = BN_CTX_get(ctx);\n\t\t\tif (al == j)\n\t\t\t\t{\n\t\t\t\tif (bn_wexpand(t,k*2) == NULL) goto err;\n\t\t\t\tif (bn_wexpand(rr,k*2) == NULL) goto err;\n\t\t\t\tbn_mul_recursive(rr->d,a->d,b->d,al,t->d);\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\tif (bn_wexpand(t,k*4) == NULL) goto err;\n\t\t\t\tif (bn_wexpand(rr,k*4) == NULL) goto err;\n\t\t\t\tbn_mul_part_recursive(rr->d,a->d,b->d,al-j,j,t->d);\n\t\t\t\t}\n\t\t\trr->top=top;\n\t\t\tgoto end;\n\t\t\t}\n#endif\n\t\t}\n#endif\n\tif (bn_wexpand(rr,top) == NULL) goto err;\n\trr->top=top;\n\tbn_mul_normal(rr->d,a->d,al,b->d,bl);\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\nend:\n#endif\n\tbn_correct_top(rr);\n\tif (r != rr) BN_copy(r,rr);\n\tret=1;\nerr:\n\tbn_check_top(r);\n\tBN_CTX_end(ctx);\n\treturn(ret);\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}']
|
2,126
| 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 ssl_srp_verify_param_cb(SSL *s, void *arg)\n{\n SRP_ARG *srp_arg = (SRP_ARG *)arg;\n BIGNUM *N = NULL, *g = NULL;\n if (((N = SSL_get_srp_N(s)) == NULL) || ((g = SSL_get_srp_g(s)) == NULL))\n return 0;\n if (srp_arg->debug || srp_arg->msg || srp_arg->amp == 1) {\n BIO_printf(bio_err, "SRP parameters:\\n");\n BIO_printf(bio_err, "\\tN=");\n BN_print(bio_err, N);\n BIO_printf(bio_err, "\\n\\tg=");\n BN_print(bio_err, g);\n BIO_printf(bio_err, "\\n");\n }\n if (SRP_check_known_gN_param(g, N))\n return 1;\n if (srp_arg->amp == 1) {\n if (srp_arg->debug)\n BIO_printf(bio_err,\n "SRP param N and g are not known params, going to check deeper.\\n");\n if (BN_num_bits(g) <= BN_BITS && srp_Verify_N_and_g(N, g))\n return 1;\n }\n BIO_printf(bio_err, "SRP param N and g rejected.\\n");\n return 0;\n}', 'int BN_print(BIO *bp, const BIGNUM *a)\n{\n int i, j, v, z = 0;\n int ret = 0;\n if ((a->neg) && BIO_write(bp, "-", 1) != 1)\n goto end;\n if (BN_is_zero(a) && BIO_write(bp, "0", 1) != 1)\n goto end;\n for (i = a->top - 1; i >= 0; i--) {\n for (j = BN_BITS2 - 4; j >= 0; j -= 4) {\n v = (int)((a->d[i] >> j) & 0x0f);\n if (z || v != 0) {\n if (BIO_write(bp, &Hex[v], 1) != 1)\n goto end;\n z = 1;\n }\n }\n }\n ret = 1;\n end:\n return ret;\n}', 'char *SRP_check_known_gN_param(const BIGNUM *g, const BIGNUM *N)\n{\n size_t i;\n if ((g == NULL) || (N == NULL))\n return 0;\n for (i = 0; i < KNOWN_GN_NUMBER; i++) {\n if (BN_cmp(knowngN[i].g, g) == 0 && BN_cmp(knowngN[i].N, N) == 0)\n return knowngN[i].id;\n }\n return NULL;\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}', 'static int srp_Verify_N_and_g(const BIGNUM *N, const BIGNUM *g)\n{\n BN_CTX *bn_ctx = BN_CTX_new();\n BIGNUM *p = BN_new();\n BIGNUM *r = BN_new();\n int ret =\n g != NULL && N != NULL && bn_ctx != NULL && BN_is_odd(N) &&\n BN_is_prime_ex(N, SRP_NUMBER_ITERATIONS_FOR_PRIME, bn_ctx, NULL) == 1 &&\n p != NULL && BN_rshift1(p, N) &&\n BN_is_prime_ex(p, SRP_NUMBER_ITERATIONS_FOR_PRIME, bn_ctx, NULL) == 1 &&\n r != NULL &&\n BN_mod_exp(r, g, p, N, bn_ctx) &&\n BN_add_word(r, 1) && BN_cmp(r, N) == 0;\n BN_free(r);\n BN_free(p);\n BN_CTX_free(bn_ctx);\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_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_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}', '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}']
|
2,127
| 0
|
https://github.com/libav/libav/blob/7fa70598e83cca650717d02ac96bcf55e9f97c19/libavcodec/mpegaudiodec.c/#L893
|
void ff_mpa_synth_filter(MPA_INT *synth_buf_ptr, int *synth_buf_offset,
MPA_INT *window, int *dither_state,
OUT_INT *samples, int incr,
int32_t sb_samples[SBLIMIT])
{
int32_t tmp[32];
register MPA_INT *synth_buf;
register const MPA_INT *w, *w2, *p;
int j, offset, v;
OUT_INT *samples2;
#if FRAC_BITS <= 15
int sum, sum2;
#else
int64_t sum, sum2;
#endif
dct32(tmp, sb_samples);
offset = *synth_buf_offset;
synth_buf = synth_buf_ptr + offset;
for(j=0;j<32;j++) {
v = tmp[j];
#if FRAC_BITS <= 15
v = av_clip_int16(v);
#endif
synth_buf[j] = v;
}
memcpy(synth_buf + 512, synth_buf, 32 * sizeof(MPA_INT));
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;
offset = (offset - 32) & 511;
*synth_buf_offset = offset;
}
|
['static int decode_frame_mp3on4(AVCodecContext * avctx,\n void *data, int *data_size,\n const uint8_t * buf, int buf_size)\n{\n MP3On4DecodeContext *s = avctx->priv_data;\n MPADecodeContext *m;\n int fsize, len = buf_size, out_size = 0;\n uint32_t header;\n OUT_INT *out_samples = data;\n OUT_INT decoded_buf[MPA_FRAME_SIZE * MPA_MAX_CHANNELS];\n OUT_INT *outptr, *bp;\n int fr, j, n;\n *data_size = 0;\n if (buf_size < HEADER_SIZE)\n return -1;\n outptr = s->frames == 1 ? out_samples : decoded_buf;\n avctx->bit_rate = 0;\n for (fr = 0; fr < s->frames; fr++) {\n fsize = AV_RB16(buf) >> 4;\n fsize = FFMIN3(fsize, len, MPA_MAX_CODED_FRAME_SIZE);\n m = s->mp3decctx[fr];\n assert (m != NULL);\n header = (AV_RB32(buf) & 0x000fffff) | s->syncword;\n if (ff_mpa_check_header(header) < 0)\n break;\n ff_mpegaudio_decode_header((MPADecodeHeader *)m, header);\n out_size += mp_decode_frame(m, outptr, buf, fsize);\n buf += fsize;\n len -= fsize;\n if(s->frames > 1) {\n n = m->avctx->frame_size*m->nb_channels;\n bp = out_samples + s->coff[fr];\n if(m->nb_channels == 1) {\n for(j = 0; j < n; j++) {\n *bp = decoded_buf[j];\n bp += avctx->channels;\n }\n } else {\n for(j = 0; j < n; j++) {\n bp[0] = decoded_buf[j++];\n bp[1] = decoded_buf[j];\n bp += avctx->channels;\n }\n }\n }\n avctx->bit_rate += m->bit_rate;\n }\n avctx->sample_rate = s->mp3decctx[0]->sample_rate;\n *data_size = out_size;\n return buf_size;\n}', 'int ff_mpegaudio_decode_header(MPADecodeHeader *s, uint32_t header)\n{\n int sample_rate, frame_size, mpeg25, padding;\n int sample_rate_index, bitrate_index;\n if (header & (1<<20)) {\n s->lsf = (header & (1<<19)) ? 0 : 1;\n mpeg25 = 0;\n } else {\n s->lsf = 1;\n mpeg25 = 1;\n }\n s->layer = 4 - ((header >> 17) & 3);\n sample_rate_index = (header >> 10) & 3;\n sample_rate = ff_mpa_freq_tab[sample_rate_index] >> (s->lsf + mpeg25);\n sample_rate_index += 3 * (s->lsf + mpeg25);\n s->sample_rate_index = sample_rate_index;\n s->error_protection = ((header >> 16) & 1) ^ 1;\n s->sample_rate = sample_rate;\n bitrate_index = (header >> 12) & 0xf;\n padding = (header >> 9) & 1;\n s->mode = (header >> 6) & 3;\n s->mode_ext = (header >> 4) & 3;\n if (s->mode == MPA_MONO)\n s->nb_channels = 1;\n else\n s->nb_channels = 2;\n if (bitrate_index != 0) {\n frame_size = ff_mpa_bitrate_tab[s->lsf][s->layer - 1][bitrate_index];\n s->bit_rate = frame_size * 1000;\n switch(s->layer) {\n case 1:\n frame_size = (frame_size * 12000) / sample_rate;\n frame_size = (frame_size + padding) * 4;\n break;\n case 2:\n frame_size = (frame_size * 144000) / sample_rate;\n frame_size += padding;\n break;\n default:\n case 3:\n frame_size = (frame_size * 144000) / (sample_rate << s->lsf);\n frame_size += padding;\n break;\n }\n s->frame_size = frame_size;\n } else {\n return 1;\n }\n#if defined(DEBUG)\n dprintf(s->avctx, "layer%d, %d Hz, %d kbits/s, ",\n s->layer, s->sample_rate, s->bit_rate);\n if (s->nb_channels == 2) {\n if (s->layer == 3) {\n if (s->mode_ext & MODE_EXT_MS_STEREO)\n dprintf(s->avctx, "ms-");\n if (s->mode_ext & MODE_EXT_I_STEREO)\n dprintf(s->avctx, "i-");\n }\n dprintf(s->avctx, "stereo");\n } else {\n dprintf(s->avctx, "mono");\n }\n dprintf(s->avctx, "\\n");\n#endif\n return 0;\n}', 'static int mp_decode_frame(MPADecodeContext *s,\n OUT_INT *samples, const uint8_t *buf, int buf_size)\n{\n int i, nb_frames, ch;\n OUT_INT *samples_ptr;\n init_get_bits(&s->gb, buf + HEADER_SIZE, (buf_size - HEADER_SIZE)*8);\n if (s->error_protection)\n skip_bits(&s->gb, 16);\n dprintf(s->avctx, "frame %d:\\n", s->frame_count);\n switch(s->layer) {\n case 1:\n s->avctx->frame_size = 384;\n nb_frames = mp_decode_layer1(s);\n break;\n case 2:\n s->avctx->frame_size = 1152;\n nb_frames = mp_decode_layer2(s);\n break;\n case 3:\n s->avctx->frame_size = s->lsf ? 576 : 1152;\n default:\n nb_frames = mp_decode_layer3(s);\n s->last_buf_size=0;\n if(s->in_gb.buffer){\n align_get_bits(&s->gb);\n i= (s->gb.size_in_bits - get_bits_count(&s->gb))>>3;\n if(i >= 0 && i <= BACKSTEP_SIZE){\n memmove(s->last_buf, s->gb.buffer + (get_bits_count(&s->gb)>>3), i);\n s->last_buf_size=i;\n }else\n av_log(s->avctx, AV_LOG_ERROR, "invalid old backstep %d\\n", i);\n s->gb= s->in_gb;\n s->in_gb.buffer= NULL;\n }\n align_get_bits(&s->gb);\n assert((get_bits_count(&s->gb) & 7) == 0);\n i= (s->gb.size_in_bits - get_bits_count(&s->gb))>>3;\n if(i<0 || i > BACKSTEP_SIZE || nb_frames<0){\n if(i<0)\n av_log(s->avctx, AV_LOG_ERROR, "invalid new backstep %d\\n", i);\n i= FFMIN(BACKSTEP_SIZE, buf_size - HEADER_SIZE);\n }\n assert(i <= buf_size - HEADER_SIZE && i>= 0);\n memcpy(s->last_buf + s->last_buf_size, s->gb.buffer + buf_size - HEADER_SIZE - i, i);\n s->last_buf_size += i;\n break;\n }\n for(ch=0;ch<s->nb_channels;ch++) {\n samples_ptr = samples + ch;\n for(i=0;i<nb_frames;i++) {\n ff_mpa_synth_filter(s->synth_buf[ch], &(s->synth_buf_offset[ch]),\n window, &s->dither_state,\n samples_ptr, s->nb_channels,\n s->sb_samples[ch][i]);\n samples_ptr += 32 * s->nb_channels;\n }\n }\n return nb_frames * 32 * sizeof(OUT_INT) * s->nb_channels;\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 int32_t sb_samples[SBLIMIT])\n{\n int32_t tmp[32];\n register MPA_INT *synth_buf;\n register const MPA_INT *w, *w2, *p;\n int j, offset, v;\n OUT_INT *samples2;\n#if FRAC_BITS <= 15\n int sum, sum2;\n#else\n int64_t sum, sum2;\n#endif\n dct32(tmp, sb_samples);\n offset = *synth_buf_offset;\n synth_buf = synth_buf_ptr + offset;\n for(j=0;j<32;j++) {\n v = tmp[j];\n#if FRAC_BITS <= 15\n v = av_clip_int16(v);\n#endif\n synth_buf[j] = v;\n }\n memcpy(synth_buf + 512, synth_buf, 32 * sizeof(MPA_INT));\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 offset = (offset - 32) & 511;\n *synth_buf_offset = offset;\n}']
|
2,128
| 0
|
https://gitlab.com/libtiff/libtiff/blob/bfbc717684115d7beb96c82255dad2dd4a4b8845/tools/tiff2pdf.c/#L1850
|
void t2p_read_tiff_size(T2P* t2p, TIFF* input){
uint64* sbc=NULL;
#if defined(JPEG_SUPPORT) || defined (OJPEG_SUPPORT)
unsigned char* jpt=NULL;
tstrip_t i=0;
tstrip_t stripcount=0;
#endif
#ifdef OJPEG_SUPPORT
tsize_t k = 0;
#endif
if(t2p->pdf_transcode == T2P_TRANSCODE_RAW){
#ifdef CCITT_SUPPORT
if(t2p->pdf_compression == T2P_COMPRESS_G4 ){
TIFFGetField(input, TIFFTAG_STRIPBYTECOUNTS, &sbc);
t2p->tiff_datasize=(tmsize_t)sbc[0];
return;
}
#endif
#ifdef ZIP_SUPPORT
if(t2p->pdf_compression == T2P_COMPRESS_ZIP){
TIFFGetField(input, TIFFTAG_STRIPBYTECOUNTS, &sbc);
t2p->tiff_datasize=(tmsize_t)sbc[0];
return;
}
#endif
#ifdef OJPEG_SUPPORT
if(t2p->tiff_compression == COMPRESSION_OJPEG){
if(!TIFFGetField(input, TIFFTAG_STRIPBYTECOUNTS, &sbc)){
TIFFError(TIFF2PDF_MODULE,
"Input file %s missing field: TIFFTAG_STRIPBYTECOUNTS",
TIFFFileName(input));
t2p->t2p_error = T2P_ERR_ERROR;
return;
}
stripcount=TIFFNumberOfStrips(input);
for(i=0;i<stripcount;i++){
k += sbc[i];
}
if(TIFFGetField(input, TIFFTAG_JPEGIFOFFSET, &(t2p->tiff_dataoffset))){
if(t2p->tiff_dataoffset != 0){
if(TIFFGetField(input, TIFFTAG_JPEGIFBYTECOUNT, &(t2p->tiff_datasize))!=0){
if(t2p->tiff_datasize < k) {
t2p->pdf_ojpegiflength=t2p->tiff_datasize;
t2p->tiff_datasize+=k;
t2p->tiff_datasize+=6;
t2p->tiff_datasize+=2*stripcount;
TIFFWarning(TIFF2PDF_MODULE,
"Input file %s has short JPEG interchange file byte count",
TIFFFileName(input));
return;
}
return;
}else {
TIFFError(TIFF2PDF_MODULE,
"Input file %s missing field: TIFFTAG_JPEGIFBYTECOUNT",
TIFFFileName(input));
t2p->t2p_error = T2P_ERR_ERROR;
return;
}
}
}
t2p->tiff_datasize+=k;
t2p->tiff_datasize+=2*stripcount;
t2p->tiff_datasize+=2048;
return;
}
#endif
#ifdef JPEG_SUPPORT
if(t2p->tiff_compression == COMPRESSION_JPEG) {
uint32 count = 0;
if(TIFFGetField(input, TIFFTAG_JPEGTABLES, &count, &jpt) != 0 ){
if(count > 4){
t2p->tiff_datasize += count;
t2p->tiff_datasize -= 2;
}
} else {
t2p->tiff_datasize = 2;
}
stripcount=TIFFNumberOfStrips(input);
if(!TIFFGetField(input, TIFFTAG_STRIPBYTECOUNTS, &sbc)){
TIFFError(TIFF2PDF_MODULE,
"Input file %s missing field: TIFFTAG_STRIPBYTECOUNTS",
TIFFFileName(input));
t2p->t2p_error = T2P_ERR_ERROR;
return;
}
for(i=0;i<stripcount;i++){
t2p->tiff_datasize += sbc[i];
t2p->tiff_datasize -=4;
}
t2p->tiff_datasize +=2;
}
#endif
(void) 0;
}
t2p->tiff_datasize=TIFFScanlineSize(input) * t2p->tiff_length;
if(t2p->tiff_planar==PLANARCONFIG_SEPARATE){
t2p->tiff_datasize*= t2p->tiff_samplesperpixel;
}
return;
}
|
['void t2p_read_tiff_size(T2P* t2p, TIFF* input){\n\tuint64* sbc=NULL;\n#if defined(JPEG_SUPPORT) || defined (OJPEG_SUPPORT)\n\tunsigned char* jpt=NULL;\n\ttstrip_t i=0;\n\ttstrip_t stripcount=0;\n#endif\n#ifdef OJPEG_SUPPORT\n tsize_t k = 0;\n#endif\n\tif(t2p->pdf_transcode == T2P_TRANSCODE_RAW){\n#ifdef CCITT_SUPPORT\n\t\tif(t2p->pdf_compression == T2P_COMPRESS_G4 ){\n\t\t\tTIFFGetField(input, TIFFTAG_STRIPBYTECOUNTS, &sbc);\n\t\t\tt2p->tiff_datasize=(tmsize_t)sbc[0];\n\t\t\treturn;\n\t\t}\n#endif\n#ifdef ZIP_SUPPORT\n\t\tif(t2p->pdf_compression == T2P_COMPRESS_ZIP){\n\t\t\tTIFFGetField(input, TIFFTAG_STRIPBYTECOUNTS, &sbc);\n\t\t\tt2p->tiff_datasize=(tmsize_t)sbc[0];\n\t\t\treturn;\n\t\t}\n#endif\n#ifdef OJPEG_SUPPORT\n\t\tif(t2p->tiff_compression == COMPRESSION_OJPEG){\n\t\t\tif(!TIFFGetField(input, TIFFTAG_STRIPBYTECOUNTS, &sbc)){\n\t\t\t\tTIFFError(TIFF2PDF_MODULE,\n\t\t\t\t\t"Input file %s missing field: TIFFTAG_STRIPBYTECOUNTS",\n\t\t\t\t\tTIFFFileName(input));\n\t\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tstripcount=TIFFNumberOfStrips(input);\n\t\t\tfor(i=0;i<stripcount;i++){\n\t\t\t\tk += sbc[i];\n\t\t\t}\n\t\t\tif(TIFFGetField(input, TIFFTAG_JPEGIFOFFSET, &(t2p->tiff_dataoffset))){\n\t\t\t\tif(t2p->tiff_dataoffset != 0){\n\t\t\t\t\tif(TIFFGetField(input, TIFFTAG_JPEGIFBYTECOUNT, &(t2p->tiff_datasize))!=0){\n\t\t\t\t\t\tif(t2p->tiff_datasize < k) {\n\t\t\t\t\t\t\tt2p->pdf_ojpegiflength=t2p->tiff_datasize;\n\t\t\t\t\t\t\tt2p->tiff_datasize+=k;\n\t\t\t\t\t\t\tt2p->tiff_datasize+=6;\n\t\t\t\t\t\t\tt2p->tiff_datasize+=2*stripcount;\n\t\t\t\t\t\t\tTIFFWarning(TIFF2PDF_MODULE,\n\t\t\t\t\t\t\t\t"Input file %s has short JPEG interchange file byte count",\n\t\t\t\t\t\t\t\tTIFFFileName(input));\n\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t}\n\t\t\t\t\t\treturn;\n\t\t\t\t\t}else {\n\t\t\t\t\t\tTIFFError(TIFF2PDF_MODULE,\n\t\t\t\t\t\t\t"Input file %s missing field: TIFFTAG_JPEGIFBYTECOUNT",\n\t\t\t\t\t\t\tTIFFFileName(input));\n\t\t\t\t\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\t\t\t\t\treturn;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tt2p->tiff_datasize+=k;\n\t\t\tt2p->tiff_datasize+=2*stripcount;\n\t\t\tt2p->tiff_datasize+=2048;\n\t\t\treturn;\n\t\t}\n#endif\n#ifdef JPEG_SUPPORT\n\t\tif(t2p->tiff_compression == COMPRESSION_JPEG) {\n\t\t\tuint32 count = 0;\n\t\t\tif(TIFFGetField(input, TIFFTAG_JPEGTABLES, &count, &jpt) != 0 ){\n\t\t\t\tif(count > 4){\n\t\t\t\t\tt2p->tiff_datasize += count;\n\t\t\t\t\tt2p->tiff_datasize -= 2;\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tt2p->tiff_datasize = 2;\n\t\t\t}\n\t\t\tstripcount=TIFFNumberOfStrips(input);\n\t\t\tif(!TIFFGetField(input, TIFFTAG_STRIPBYTECOUNTS, &sbc)){\n\t\t\t\tTIFFError(TIFF2PDF_MODULE,\n\t\t\t\t\t"Input file %s missing field: TIFFTAG_STRIPBYTECOUNTS",\n\t\t\t\t\tTIFFFileName(input));\n\t\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tfor(i=0;i<stripcount;i++){\n\t\t\t\tt2p->tiff_datasize += sbc[i];\n\t\t\t\tt2p->tiff_datasize -=4;\n\t\t\t}\n\t\t\tt2p->tiff_datasize +=2;\n\t\t}\n#endif\n\t\t(void) 0;\n\t}\n\tt2p->tiff_datasize=TIFFScanlineSize(input) * t2p->tiff_length;\n\tif(t2p->tiff_planar==PLANARCONFIG_SEPARATE){\n\t\tt2p->tiff_datasize*= t2p->tiff_samplesperpixel;\n\t}\n\treturn;\n}', 'int\nTIFFGetField(TIFF* tif, uint32 tag, ...)\n{\n\tint status;\n\tva_list ap;\n\tva_start(ap, tag);\n\tstatus = TIFFVGetField(tif, tag, ap);\n\tva_end(ap);\n\treturn (status);\n}']
|
2,129
| 0
|
https://github.com/nginx/nginx/blob/e4ecddfdb0d2ffc872658e36028971ad9a873726/src/core/ngx_hash.c/#L980
|
ngx_int_t
ngx_hash_add_key(ngx_hash_keys_arrays_t *ha, ngx_str_t *key, void *value,
ngx_uint_t flags)
{
size_t len;
u_char *p;
ngx_str_t *name;
ngx_uint_t i, k, n, skip, last;
ngx_array_t *keys, *hwc;
ngx_hash_key_t *hk;
last = key->len;
if (flags & NGX_HASH_WILDCARD_KEY) {
n = 0;
for (i = 0; i < key->len; i++) {
if (key->data[i] == '*') {
if (++n > 1) {
return NGX_DECLINED;
}
}
if (key->data[i] == '.' && key->data[i + 1] == '.') {
return NGX_DECLINED;
}
}
if (key->len > 1 && key->data[0] == '.') {
skip = 1;
goto wildcard;
}
if (key->len > 2) {
if (key->data[0] == '*' && key->data[1] == '.') {
skip = 2;
goto wildcard;
}
if (key->data[i - 2] == '.' && key->data[i - 1] == '*') {
skip = 0;
last -= 2;
goto wildcard;
}
}
if (n) {
return NGX_DECLINED;
}
}
k = 0;
for (i = 0; i < last; i++) {
if (!(flags & NGX_HASH_READONLY_KEY)) {
key->data[i] = ngx_tolower(key->data[i]);
}
k = ngx_hash(k, key->data[i]);
}
k %= ha->hsize;
name = ha->keys_hash[k].elts;
if (name) {
for (i = 0; i < ha->keys_hash[k].nelts; i++) {
if (last != name[i].len) {
continue;
}
if (ngx_strncmp(key->data, name[i].data, last) == 0) {
return NGX_BUSY;
}
}
} else {
if (ngx_array_init(&ha->keys_hash[k], ha->temp_pool, 4,
sizeof(ngx_str_t))
!= NGX_OK)
{
return NGX_ERROR;
}
}
name = ngx_array_push(&ha->keys_hash[k]);
if (name == NULL) {
return NGX_ERROR;
}
*name = *key;
hk = ngx_array_push(&ha->keys);
if (hk == NULL) {
return NGX_ERROR;
}
hk->key = *key;
hk->key_hash = ngx_hash_key(key->data, last);
hk->value = value;
return NGX_OK;
wildcard:
k = ngx_hash_strlow(&key->data[skip], &key->data[skip], last - skip);
k %= ha->hsize;
if (skip == 1) {
name = ha->keys_hash[k].elts;
if (name) {
len = last - skip;
for (i = 0; i < ha->keys_hash[k].nelts; i++) {
if (len != name[i].len) {
continue;
}
if (ngx_strncmp(&key->data[1], name[i].data, len) == 0) {
return NGX_BUSY;
}
}
} else {
if (ngx_array_init(&ha->keys_hash[k], ha->temp_pool, 4,
sizeof(ngx_str_t))
!= NGX_OK)
{
return NGX_ERROR;
}
}
name = ngx_array_push(&ha->keys_hash[k]);
if (name == NULL) {
return NGX_ERROR;
}
name->len = last - 1;
name->data = ngx_pnalloc(ha->temp_pool, name->len);
if (name->data == NULL) {
return NGX_ERROR;
}
ngx_memcpy(name->data, &key->data[1], name->len);
}
if (skip) {
p = ngx_pnalloc(ha->temp_pool, last);
if (p == NULL) {
return NGX_ERROR;
}
len = 0;
n = 0;
for (i = last - 1; i; i--) {
if (key->data[i] == '.') {
ngx_memcpy(&p[n], &key->data[i + 1], len);
n += len;
p[n++] = '.';
len = 0;
continue;
}
len++;
}
if (len) {
ngx_memcpy(&p[n], &key->data[1], len);
n += len;
}
p[n] = '\0';
hwc = &ha->dns_wc_head;
keys = &ha->dns_wc_head_hash[k];
} else {
last++;
p = ngx_pnalloc(ha->temp_pool, last);
if (p == NULL) {
return NGX_ERROR;
}
ngx_cpystrn(p, key->data, last);
hwc = &ha->dns_wc_tail;
keys = &ha->dns_wc_tail_hash[k];
}
hk = ngx_array_push(hwc);
if (hk == NULL) {
return NGX_ERROR;
}
hk->key.len = last - 1;
hk->key.data = p;
hk->key_hash = 0;
hk->value = value;
name = keys->elts;
if (name) {
len = last - skip;
for (i = 0; i < keys->nelts; i++) {
if (len != name[i].len) {
continue;
}
if (ngx_strncmp(key->data + skip, name[i].data, len) == 0) {
return NGX_BUSY;
}
}
} else {
if (ngx_array_init(keys, ha->temp_pool, 4, sizeof(ngx_str_t)) != NGX_OK)
{
return NGX_ERROR;
}
}
name = ngx_array_push(keys);
if (name == NULL) {
return NGX_ERROR;
}
name->len = last - skip;
name->data = ngx_pnalloc(ha->temp_pool, name->len);
if (name->data == NULL) {
return NGX_ERROR;
}
ngx_memcpy(name->data, key->data + skip, name->len);
return NGX_OK;
}
|
['static ngx_int_t\nngx_http_userid_add_variables(ngx_conf_t *cf)\n{\n ngx_http_variable_t *var;\n var = ngx_http_add_variable(cf, &ngx_http_userid_got, NGX_HTTP_VAR_NOHASH);\n if (var == NULL) {\n return NGX_ERROR;\n }\n var->get_handler = ngx_http_userid_got_variable;\n var = ngx_http_add_variable(cf, &ngx_http_userid_set, NGX_HTTP_VAR_NOHASH);\n if (var == NULL) {\n return NGX_ERROR;\n }\n var->get_handler = ngx_http_userid_set_variable;\n return NGX_OK;\n}', 'ngx_http_variable_t *\nngx_http_add_variable(ngx_conf_t *cf, ngx_str_t *name, ngx_uint_t flags)\n{\n ngx_int_t rc;\n ngx_uint_t i;\n ngx_hash_key_t *key;\n ngx_http_variable_t *v;\n ngx_http_core_main_conf_t *cmcf;\n cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);\n key = cmcf->variables_keys->keys.elts;\n for (i = 0; i < cmcf->variables_keys->keys.nelts; i++) {\n if (name->len != key[i].key.len\n || ngx_strncasecmp(name->data, key[i].key.data, name->len) != 0)\n {\n continue;\n }\n v = key[i].value;\n if (!(v->flags & NGX_HTTP_VAR_CHANGEABLE)) {\n ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,\n "the duplicate \\"%V\\" variable", name);\n return NULL;\n }\n return v;\n }\n v = ngx_palloc(cf->pool, sizeof(ngx_http_variable_t));\n if (v == NULL) {\n return NULL;\n }\n v->name.len = name->len;\n v->name.data = ngx_pnalloc(cf->pool, name->len);\n if (v->name.data == NULL) {\n return NULL;\n }\n ngx_strlow(v->name.data, name->data, name->len);\n v->set_handler = NULL;\n v->get_handler = NULL;\n v->data = 0;\n v->flags = flags;\n v->index = 0;\n rc = ngx_hash_add_key(cmcf->variables_keys, &v->name, v, 0);\n if (rc == NGX_ERROR) {\n return NULL;\n }\n if (rc == NGX_BUSY) {\n ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,\n "conflicting variable name \\"%V\\"", name);\n return NULL;\n }\n return v;\n}', "ngx_int_t\nngx_hash_add_key(ngx_hash_keys_arrays_t *ha, ngx_str_t *key, void *value,\n ngx_uint_t flags)\n{\n size_t len;\n u_char *p;\n ngx_str_t *name;\n ngx_uint_t i, k, n, skip, last;\n ngx_array_t *keys, *hwc;\n ngx_hash_key_t *hk;\n last = key->len;\n if (flags & NGX_HASH_WILDCARD_KEY) {\n n = 0;\n for (i = 0; i < key->len; i++) {\n if (key->data[i] == '*') {\n if (++n > 1) {\n return NGX_DECLINED;\n }\n }\n if (key->data[i] == '.' && key->data[i + 1] == '.') {\n return NGX_DECLINED;\n }\n }\n if (key->len > 1 && key->data[0] == '.') {\n skip = 1;\n goto wildcard;\n }\n if (key->len > 2) {\n if (key->data[0] == '*' && key->data[1] == '.') {\n skip = 2;\n goto wildcard;\n }\n if (key->data[i - 2] == '.' && key->data[i - 1] == '*') {\n skip = 0;\n last -= 2;\n goto wildcard;\n }\n }\n if (n) {\n return NGX_DECLINED;\n }\n }\n k = 0;\n for (i = 0; i < last; i++) {\n if (!(flags & NGX_HASH_READONLY_KEY)) {\n key->data[i] = ngx_tolower(key->data[i]);\n }\n k = ngx_hash(k, key->data[i]);\n }\n k %= ha->hsize;\n name = ha->keys_hash[k].elts;\n if (name) {\n for (i = 0; i < ha->keys_hash[k].nelts; i++) {\n if (last != name[i].len) {\n continue;\n }\n if (ngx_strncmp(key->data, name[i].data, last) == 0) {\n return NGX_BUSY;\n }\n }\n } else {\n if (ngx_array_init(&ha->keys_hash[k], ha->temp_pool, 4,\n sizeof(ngx_str_t))\n != NGX_OK)\n {\n return NGX_ERROR;\n }\n }\n name = ngx_array_push(&ha->keys_hash[k]);\n if (name == NULL) {\n return NGX_ERROR;\n }\n *name = *key;\n hk = ngx_array_push(&ha->keys);\n if (hk == NULL) {\n return NGX_ERROR;\n }\n hk->key = *key;\n hk->key_hash = ngx_hash_key(key->data, last);\n hk->value = value;\n return NGX_OK;\nwildcard:\n k = ngx_hash_strlow(&key->data[skip], &key->data[skip], last - skip);\n k %= ha->hsize;\n if (skip == 1) {\n name = ha->keys_hash[k].elts;\n if (name) {\n len = last - skip;\n for (i = 0; i < ha->keys_hash[k].nelts; i++) {\n if (len != name[i].len) {\n continue;\n }\n if (ngx_strncmp(&key->data[1], name[i].data, len) == 0) {\n return NGX_BUSY;\n }\n }\n } else {\n if (ngx_array_init(&ha->keys_hash[k], ha->temp_pool, 4,\n sizeof(ngx_str_t))\n != NGX_OK)\n {\n return NGX_ERROR;\n }\n }\n name = ngx_array_push(&ha->keys_hash[k]);\n if (name == NULL) {\n return NGX_ERROR;\n }\n name->len = last - 1;\n name->data = ngx_pnalloc(ha->temp_pool, name->len);\n if (name->data == NULL) {\n return NGX_ERROR;\n }\n ngx_memcpy(name->data, &key->data[1], name->len);\n }\n if (skip) {\n p = ngx_pnalloc(ha->temp_pool, last);\n if (p == NULL) {\n return NGX_ERROR;\n }\n len = 0;\n n = 0;\n for (i = last - 1; i; i--) {\n if (key->data[i] == '.') {\n ngx_memcpy(&p[n], &key->data[i + 1], len);\n n += len;\n p[n++] = '.';\n len = 0;\n continue;\n }\n len++;\n }\n if (len) {\n ngx_memcpy(&p[n], &key->data[1], len);\n n += len;\n }\n p[n] = '\\0';\n hwc = &ha->dns_wc_head;\n keys = &ha->dns_wc_head_hash[k];\n } else {\n last++;\n p = ngx_pnalloc(ha->temp_pool, last);\n if (p == NULL) {\n return NGX_ERROR;\n }\n ngx_cpystrn(p, key->data, last);\n hwc = &ha->dns_wc_tail;\n keys = &ha->dns_wc_tail_hash[k];\n }\n hk = ngx_array_push(hwc);\n if (hk == NULL) {\n return NGX_ERROR;\n }\n hk->key.len = last - 1;\n hk->key.data = p;\n hk->key_hash = 0;\n hk->value = value;\n name = keys->elts;\n if (name) {\n len = last - skip;\n for (i = 0; i < keys->nelts; i++) {\n if (len != name[i].len) {\n continue;\n }\n if (ngx_strncmp(key->data + skip, name[i].data, len) == 0) {\n return NGX_BUSY;\n }\n }\n } else {\n if (ngx_array_init(keys, ha->temp_pool, 4, sizeof(ngx_str_t)) != NGX_OK)\n {\n return NGX_ERROR;\n }\n }\n name = ngx_array_push(keys);\n if (name == NULL) {\n return NGX_ERROR;\n }\n name->len = last - skip;\n name->data = ngx_pnalloc(ha->temp_pool, name->len);\n if (name->data == NULL) {\n return NGX_ERROR;\n }\n ngx_memcpy(name->data, key->data + skip, name->len);\n return NGX_OK;\n}"]
|
2,130
| 0
|
https://github.com/nginx/nginx/blob/e5b2d3c6b2a132bbbbac0249566f0da7ff12bc39/src/http/modules/ngx_http_log_module.c/#L940
|
static char *
ngx_http_log_set_log(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
ngx_http_log_loc_conf_t *llcf = conf;
ssize_t buf;
ngx_uint_t i, n;
ngx_str_t *value, name;
ngx_http_log_t *log;
ngx_http_log_fmt_t *fmt;
ngx_http_log_main_conf_t *lmcf;
ngx_http_script_compile_t sc;
value = cf->args->elts;
if (ngx_strcmp(value[1].data, "off") == 0) {
llcf->off = 1;
if (cf->args->nelts == 2) {
return NGX_CONF_OK;
}
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"invalid parameter \"%V\"", &value[2]);
return NGX_CONF_ERROR;
}
if (llcf->logs == NULL) {
llcf->logs = ngx_array_create(cf->pool, 2, sizeof(ngx_http_log_t));
if (llcf->logs == NULL) {
return NGX_CONF_ERROR;
}
}
lmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_log_module);
log = ngx_array_push(llcf->logs);
if (log == NULL) {
return NGX_CONF_ERROR;
}
ngx_memzero(log, sizeof(ngx_http_log_t));
n = ngx_http_script_variables_count(&value[1]);
if (n == 0) {
log->file = ngx_conf_open_file(cf->cycle, &value[1]);
if (log->file == NULL) {
return NGX_CONF_ERROR;
}
} else {
if (ngx_conf_full_name(cf->cycle, &value[1], 0) != NGX_OK) {
return NGX_CONF_ERROR;
}
log->script = ngx_pcalloc(cf->pool, sizeof(ngx_http_log_script_t));
if (log->script == NULL) {
return NGX_CONF_ERROR;
}
ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
sc.cf = cf;
sc.source = &value[1];
sc.lengths = &log->script->lengths;
sc.values = &log->script->values;
sc.variables = n;
sc.complete_lengths = 1;
sc.complete_values = 1;
if (ngx_http_script_compile(&sc) != NGX_OK) {
return NGX_CONF_ERROR;
}
}
if (cf->args->nelts >= 3) {
name = value[2];
if (ngx_strcmp(name.data, "combined") == 0) {
lmcf->combined_used = 1;
}
} else {
name.len = sizeof("combined") - 1;
name.data = (u_char *) "combined";
lmcf->combined_used = 1;
}
fmt = lmcf->formats.elts;
for (i = 0; i < lmcf->formats.nelts; i++) {
if (fmt[i].name.len == name.len
&& ngx_strcasecmp(fmt[i].name.data, name.data) == 0)
{
log->format = &fmt[i];
goto buffer;
}
}
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"unknown log format \"%V\"", &name);
return NGX_CONF_ERROR;
buffer:
if (cf->args->nelts == 4) {
if (ngx_strncmp(value[3].data, "buffer=", 7) != 0) {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"invalid parameter \"%V\"", &value[3]);
return NGX_CONF_ERROR;
}
if (log->script) {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"buffered logs can not have variables in name");
return NGX_CONF_ERROR;
}
name.len = value[3].len - 7;
name.data = value[3].data + 7;
buf = ngx_parse_size(&name);
if (buf == NGX_ERROR) {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"invalid parameter \"%V\"", &value[3]);
return NGX_CONF_ERROR;
}
if (log->file->buffer && log->file->last - log->file->pos != buf) {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"access_log \"%V\" already defined "
"with different buffer size", &value[1]);
return NGX_CONF_ERROR;
}
log->file->buffer = ngx_palloc(cf->pool, buf);
if (log->file->buffer == NULL) {
return NGX_CONF_ERROR;
}
log->file->pos = log->file->buffer;
log->file->last = log->file->buffer + buf;
}
return NGX_CONF_OK;
}
|
['static char *\nngx_http_log_set_log(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)\n{\n ngx_http_log_loc_conf_t *llcf = conf;\n ssize_t buf;\n ngx_uint_t i, n;\n ngx_str_t *value, name;\n ngx_http_log_t *log;\n ngx_http_log_fmt_t *fmt;\n ngx_http_log_main_conf_t *lmcf;\n ngx_http_script_compile_t sc;\n value = cf->args->elts;\n if (ngx_strcmp(value[1].data, "off") == 0) {\n llcf->off = 1;\n if (cf->args->nelts == 2) {\n return NGX_CONF_OK;\n }\n ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,\n "invalid parameter \\"%V\\"", &value[2]);\n return NGX_CONF_ERROR;\n }\n if (llcf->logs == NULL) {\n llcf->logs = ngx_array_create(cf->pool, 2, sizeof(ngx_http_log_t));\n if (llcf->logs == NULL) {\n return NGX_CONF_ERROR;\n }\n }\n lmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_log_module);\n log = ngx_array_push(llcf->logs);\n if (log == NULL) {\n return NGX_CONF_ERROR;\n }\n ngx_memzero(log, sizeof(ngx_http_log_t));\n n = ngx_http_script_variables_count(&value[1]);\n if (n == 0) {\n log->file = ngx_conf_open_file(cf->cycle, &value[1]);\n if (log->file == NULL) {\n return NGX_CONF_ERROR;\n }\n } else {\n if (ngx_conf_full_name(cf->cycle, &value[1], 0) != NGX_OK) {\n return NGX_CONF_ERROR;\n }\n log->script = ngx_pcalloc(cf->pool, sizeof(ngx_http_log_script_t));\n if (log->script == NULL) {\n return NGX_CONF_ERROR;\n }\n ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));\n sc.cf = cf;\n sc.source = &value[1];\n sc.lengths = &log->script->lengths;\n sc.values = &log->script->values;\n sc.variables = n;\n sc.complete_lengths = 1;\n sc.complete_values = 1;\n if (ngx_http_script_compile(&sc) != NGX_OK) {\n return NGX_CONF_ERROR;\n }\n }\n if (cf->args->nelts >= 3) {\n name = value[2];\n if (ngx_strcmp(name.data, "combined") == 0) {\n lmcf->combined_used = 1;\n }\n } else {\n name.len = sizeof("combined") - 1;\n name.data = (u_char *) "combined";\n lmcf->combined_used = 1;\n }\n fmt = lmcf->formats.elts;\n for (i = 0; i < lmcf->formats.nelts; i++) {\n if (fmt[i].name.len == name.len\n && ngx_strcasecmp(fmt[i].name.data, name.data) == 0)\n {\n log->format = &fmt[i];\n goto buffer;\n }\n }\n ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,\n "unknown log format \\"%V\\"", &name);\n return NGX_CONF_ERROR;\nbuffer:\n if (cf->args->nelts == 4) {\n if (ngx_strncmp(value[3].data, "buffer=", 7) != 0) {\n ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,\n "invalid parameter \\"%V\\"", &value[3]);\n return NGX_CONF_ERROR;\n }\n if (log->script) {\n ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,\n "buffered logs can not have variables in name");\n return NGX_CONF_ERROR;\n }\n name.len = value[3].len - 7;\n name.data = value[3].data + 7;\n buf = ngx_parse_size(&name);\n if (buf == NGX_ERROR) {\n ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,\n "invalid parameter \\"%V\\"", &value[3]);\n return NGX_CONF_ERROR;\n }\n if (log->file->buffer && log->file->last - log->file->pos != buf) {\n ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,\n "access_log \\"%V\\" already defined "\n "with different buffer size", &value[1]);\n return NGX_CONF_ERROR;\n }\n log->file->buffer = ngx_palloc(cf->pool, buf);\n if (log->file->buffer == NULL) {\n return NGX_CONF_ERROR;\n }\n log->file->pos = log->file->buffer;\n log->file->last = log->file->buffer + buf;\n }\n return NGX_CONF_OK;\n}', "ngx_uint_t\nngx_http_script_variables_count(ngx_str_t *value)\n{\n ngx_uint_t i, n;\n for (n = 0, i = 0; i < value->len; i++) {\n if (value->data[i] == '$') {\n n++;\n }\n }\n return n;\n}", 'ngx_open_file_t *\nngx_conf_open_file(ngx_cycle_t *cycle, ngx_str_t *name)\n{\n ngx_str_t full;\n ngx_uint_t i;\n ngx_list_part_t *part;\n ngx_open_file_t *file;\n#if (NGX_SUPPRESS_WARN)\n full.len = 0;\n full.data = NULL;\n#endif\n if (name->len) {\n full = *name;\n if (ngx_conf_full_name(cycle, &full, 0) != NGX_OK) {\n return NULL;\n }\n part = &cycle->open_files.part;\n file = part->elts;\n for (i = 0; ; i++) {\n if (i >= part->nelts) {\n if (part->next == NULL) {\n break;\n }\n part = part->next;\n file = part->elts;\n i = 0;\n }\n if (full.len != file[i].name.len) {\n continue;\n }\n if (ngx_strcmp(full.data, file[i].name.data) == 0) {\n return &file[i];\n }\n }\n }\n file = ngx_list_push(&cycle->open_files);\n if (file == NULL) {\n return NULL;\n }\n if (name->len) {\n file->fd = NGX_INVALID_FILE;\n file->name = full;\n } else {\n file->fd = ngx_stderr;\n file->name = *name;\n }\n file->buffer = NULL;\n return file;\n}']
|
2,131
| 0
|
https://github.com/openssl/openssl/blob/ed371b8cbac0d0349667558c061c1ae380cf75eb/crypto/bn/bn_ctx.c/#L276
|
static unsigned int BN_STACK_pop(BN_STACK *st)
{
return st->indexes[--(st->depth)];
}
|
['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}', '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 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_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}']
|
2,132
| 0
|
https://github.com/openssl/openssl/blob/54d00677f305375eee65a0c9edb5f0980c5f020f/crypto/bn/bn_lib.c/#L291
|
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;
}
|
['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}', '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 ret->flags &= (~BN_FLG_CONSTTIME);\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 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}', '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}']
|
2,133
| 0
|
https://github.com/openssl/openssl/blob/84c15db551ce1d167b901a3bde2b21880b084384/crypto/bn/bn_mul.c/#L728
|
void bn_mul_normal(BN_ULONG *r, BN_ULONG *a, int na, BN_ULONG *b, int nb)
{
BN_ULONG *rr;
#ifdef BN_COUNT
printf(" bn_mul_normal %d * %d\n",na,nb);
#endif
if (na < nb)
{
int itmp;
BN_ULONG *ltmp;
itmp=na; na=nb; nb=itmp;
ltmp=a; a=b; b=ltmp;
}
rr= &(r[na]);
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 RSA_eay_private_encrypt(int flen, unsigned char *from,\n\t unsigned char *to, RSA *rsa, int padding)\n\t{\n\tBIGNUM f,ret;\n\tint i,j,k,num=0,r= -1;\n\tunsigned char *buf=NULL;\n\tBN_CTX *ctx=NULL;\n\tBN_init(&f);\n\tBN_init(&ret);\n\tif ((ctx=BN_CTX_new()) == NULL) goto err;\n\tnum=BN_num_bytes(rsa->n);\n\tif ((buf=(unsigned char *)Malloc(num)) == NULL)\n\t\t{\n\t\tRSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,ERR_R_MALLOC_FAILURE);\n\t\tgoto err;\n\t\t}\n\tswitch (padding)\n\t\t{\n\tcase RSA_PKCS1_PADDING:\n\t\ti=RSA_padding_add_PKCS1_type_1(buf,num,from,flen);\n\t\tbreak;\n\tcase RSA_NO_PADDING:\n\t\ti=RSA_padding_add_none(buf,num,from,flen);\n\t\tbreak;\n\tcase RSA_SSLV23_PADDING:\n\tdefault:\n\t\tRSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,RSA_R_UNKNOWN_PADDING_TYPE);\n\t\tgoto err;\n\t\t}\n\tif (i <= 0) goto err;\n\tif (BN_bin2bn(buf,num,&f) == NULL) goto err;\n\tif ((rsa->flags & RSA_FLAG_BLINDING) && (rsa->blinding == NULL))\n\t\tRSA_blinding_on(rsa,ctx);\n\tif (rsa->flags & RSA_FLAG_BLINDING)\n\t\tif (!BN_BLINDING_convert(&f,rsa->blinding,ctx)) goto err;\n\tif (\t(rsa->p != NULL) &&\n\t\t(rsa->q != NULL) &&\n\t\t(rsa->dmp1 != NULL) &&\n\t\t(rsa->dmq1 != NULL) &&\n\t\t(rsa->iqmp != NULL))\n\t\t{ if (!rsa->meth->rsa_mod_exp(&ret,&f,rsa)) goto err; }\n\telse\n\t\t{\n\t\tif (!rsa->meth->bn_mod_exp(&ret,&f,rsa->d,rsa->n,ctx,NULL)) goto err;\n\t\t}\n\tif (rsa->flags & RSA_FLAG_BLINDING)\n\t\tif (!BN_BLINDING_invert(&ret,rsa->blinding,ctx)) goto err;\n\tj=BN_num_bytes(&ret);\n\ti=BN_bn2bin(&ret,&(to[num-j]));\n\tfor (k=0; k<(num-i); k++)\n\t\tto[k]=0;\n\tr=num;\nerr:\n\tif (ctx != NULL) BN_CTX_free(ctx);\n\tBN_clear_free(&ret);\n\tBN_clear_free(&f);\n\tif (buf != NULL)\n\t\t{\n\t\tmemset(buf,0,num);\n\t\tFree(buf);\n\t\t}\n\treturn(r);\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_BLINDING_convert(BIGNUM *n, BN_BLINDING *b, BN_CTX *ctx)\n\t{\n\tbn_check_top(n);\n\tif ((b->A == NULL) || (b->Ai == NULL))\n\t\t{\n\t\tBNerr(BN_F_BN_BLINDING_CONVERT,BN_R_NOT_INITIALIZED);\n\t\treturn(0);\n\t\t}\n\treturn(BN_mod_mul(n,n,b->A,b->mod,ctx));\n\t}', 'int BN_mod_mul(BIGNUM *ret, BIGNUM *a, BIGNUM *b, const 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_normal(BN_ULONG *r, BN_ULONG *a, int na, BN_ULONG *b, int nb)\n\t{\n\tBN_ULONG *rr;\n#ifdef BN_COUNT\nprintf(" bn_mul_normal %d * %d\\n",na,nb);\n#endif\n\tif (na < nb)\n\t\t{\n\t\tint itmp;\n\t\tBN_ULONG *ltmp;\n\t\titmp=na; na=nb; nb=itmp;\n\t\tltmp=a; a=b; b=ltmp;\n\t\t}\n\trr= &(r[na]);\n\trr[0]=bn_mul_words(r,a,na,b[0]);\n\tfor (;;)\n\t\t{\n\t\tif (--nb <= 0) return;\n\t\trr[1]=bn_mul_add_words(&(r[1]),a,na,b[1]);\n\t\tif (--nb <= 0) return;\n\t\trr[2]=bn_mul_add_words(&(r[2]),a,na,b[2]);\n\t\tif (--nb <= 0) return;\n\t\trr[3]=bn_mul_add_words(&(r[3]),a,na,b[3]);\n\t\tif (--nb <= 0) return;\n\t\trr[4]=bn_mul_add_words(&(r[4]),a,na,b[4]);\n\t\trr+=4;\n\t\tr+=4;\n\t\tb+=4;\n\t\t}\n\t}']
|
2,134
| 0
|
https://github.com/openssl/openssl/blob/ea32151f7b9353f8906188d007c6893704ac17bb/crypto/bn/bn_shift.c/#L110
|
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;
}
r->neg = a->neg;
nw = n / BN_BITS2;
if (bn_wexpand(r, a->top + nw + 1) == NULL)
return (0);
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);
}
|
['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}', 'BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)\n{\n int i;\n BN_ULONG *A;\n const BN_ULONG *B;\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 1\n A = a->d;\n B = b->d;\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#else\n memcpy(a->d, b->d, sizeof(b->d[0]) * b->top);\n#endif\n a->top = b->top;\n a->neg = b->neg;\n bn_check_top(a);\n return (a);\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 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 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_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 r->neg = a->neg;\n nw = n / BN_BITS2;\n if (bn_wexpand(r, a->top + nw + 1) == NULL)\n return (0);\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}']
|
2,135
| 0
|
https://github.com/libav/libav/blob/aa807425395caa17a85ed2833133278e8bd44a76/libavutil/avstring.c/#L105
|
size_t av_strlcatf(char *dst, size_t size, const char *fmt, ...)
{
int len = strlen(dst);
va_list vl;
va_start(vl, fmt);
len += vsnprintf(dst + len, size > len ? size - len : 0, fmt, vl);
va_end(vl);
return len;
}
|
['static int http_connect(URLContext *h, const char *path, const char *local_path,\n const char *hoststr, const char *auth,\n const char *proxyauth, int *new_location)\n{\n HTTPContext *s = h->priv_data;\n int post, err;\n char headers[1024] = "";\n char *authstr = NULL, *proxyauthstr = NULL;\n int64_t off = s->off;\n int len = 0;\n const char *method;\n int send_expect_100 = 0;\n post = h->flags & AVIO_FLAG_WRITE;\n if (s->post_data) {\n post = 1;\n s->chunked_post = 0;\n }\n method = post ? "POST" : "GET";\n authstr = ff_http_auth_create_response(&s->auth_state, auth, local_path,\n method);\n proxyauthstr = ff_http_auth_create_response(&s->proxy_auth_state, proxyauth,\n local_path, method);\n if (post && !s->post_data) {\n send_expect_100 = s->send_expect_100;\n if (auth && *auth &&\n s->auth_state.auth_type == HTTP_AUTH_NONE &&\n s->http_code != 401)\n send_expect_100 = 1;\n }\n if (!has_header(s->headers, "\\r\\nUser-Agent: "))\n len += av_strlcatf(headers + len, sizeof(headers) - len,\n "User-Agent: %s\\r\\n", s->user_agent);\n if (!has_header(s->headers, "\\r\\nAccept: "))\n len += av_strlcpy(headers + len, "Accept: */*\\r\\n",\n sizeof(headers) - len);\n if (!has_header(s->headers, "\\r\\nRange: ") && !post) {\n len += av_strlcatf(headers + len, sizeof(headers) - len,\n "Range: bytes=%"PRId64"-", s->off);\n if (s->end_off)\n len += av_strlcatf(headers + len, sizeof(headers) - len,\n "%"PRId64, s->end_off - 1);\n len += av_strlcpy(headers + len, "\\r\\n",\n sizeof(headers) - len);\n }\n if (send_expect_100 && !has_header(s->headers, "\\r\\nExpect: "))\n len += av_strlcatf(headers + len, sizeof(headers) - len,\n "Expect: 100-continue\\r\\n");\n if (!has_header(s->headers, "\\r\\nConnection: ")) {\n if (s->multiple_requests) {\n len += av_strlcpy(headers + len, "Connection: keep-alive\\r\\n",\n sizeof(headers) - len);\n } else {\n len += av_strlcpy(headers + len, "Connection: close\\r\\n",\n sizeof(headers) - len);\n }\n }\n if (!has_header(s->headers, "\\r\\nHost: "))\n len += av_strlcatf(headers + len, sizeof(headers) - len,\n "Host: %s\\r\\n", hoststr);\n if (!has_header(s->headers, "\\r\\nContent-Length: ") && s->post_data)\n len += av_strlcatf(headers + len, sizeof(headers) - len,\n "Content-Length: %d\\r\\n", s->post_datalen);\n if (!has_header(s->headers, "\\r\\nContent-Type: ") && s->content_type)\n len += av_strlcatf(headers + len, sizeof(headers) - len,\n "Content-Type: %s\\r\\n", s->content_type);\n if (!has_header(s->headers, "\\r\\nIcy-MetaData: ") && s->icy) {\n len += av_strlcatf(headers + len, sizeof(headers) - len,\n "Icy-MetaData: %d\\r\\n", 1);\n }\n if (s->headers)\n av_strlcpy(headers + len, s->headers, sizeof(headers) - len);\n snprintf(s->buffer, sizeof(s->buffer),\n "%s %s HTTP/1.1\\r\\n"\n "%s"\n "%s"\n "%s"\n "%s%s"\n "\\r\\n",\n method,\n path,\n post && s->chunked_post ? "Transfer-Encoding: chunked\\r\\n" : "",\n headers,\n authstr ? authstr : "",\n proxyauthstr ? "Proxy-" : "", proxyauthstr ? proxyauthstr : "");\n av_freep(&authstr);\n av_freep(&proxyauthstr);\n if ((err = ffurl_write(s->hd, s->buffer, strlen(s->buffer))) < 0)\n return err;\n if (s->post_data)\n if ((err = ffurl_write(s->hd, s->post_data, s->post_datalen)) < 0)\n return err;\n s->buf_ptr = s->buffer;\n s->buf_end = s->buffer;\n s->line_count = 0;\n s->off = 0;\n s->icy_data_read = 0;\n s->filesize = -1;\n s->willclose = 0;\n s->end_chunked_post = 0;\n s->end_header = 0;\n if (post && !s->post_data && !send_expect_100) {\n s->http_code = 200;\n return 0;\n }\n err = http_read_header(h, new_location);\n if (err < 0)\n return err;\n return (off == s->off) ? 0 : -1;\n}', 'size_t av_strlcatf(char *dst, size_t size, const char *fmt, ...)\n{\n int len = strlen(dst);\n va_list vl;\n va_start(vl, fmt);\n len += vsnprintf(dst + len, size > len ? size - len : 0, fmt, vl);\n va_end(vl);\n return len;\n}']
|
2,136
| 0
|
https://github.com/openssl/openssl/blob/02cba628daa7fea959c561531a8a984756bdf41c/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);
}
|
['int ossl_ecdsa_verify_sig(const unsigned char *dgst, int dgst_len,\n const ECDSA_SIG *sig, EC_KEY *eckey)\n{\n int ret = -1, i;\n BN_CTX *ctx;\n const BIGNUM *order;\n BIGNUM *u1, *u2, *m, *X;\n EC_POINT *point = NULL;\n const EC_GROUP *group;\n const EC_POINT *pub_key;\n if (eckey == NULL || (group = EC_KEY_get0_group(eckey)) == NULL ||\n (pub_key = EC_KEY_get0_public_key(eckey)) == NULL || sig == NULL) {\n ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, EC_R_MISSING_PARAMETERS);\n return -1;\n }\n if (!EC_KEY_can_sign(eckey)) {\n ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, EC_R_CURVE_DOES_NOT_SUPPORT_SIGNING);\n return -1;\n }\n ctx = BN_CTX_new();\n if (ctx == NULL) {\n ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_MALLOC_FAILURE);\n return -1;\n }\n BN_CTX_start(ctx);\n u1 = BN_CTX_get(ctx);\n u2 = BN_CTX_get(ctx);\n m = BN_CTX_get(ctx);\n X = BN_CTX_get(ctx);\n if (X == NULL) {\n ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);\n goto err;\n }\n order = EC_GROUP_get0_order(group);\n if (order == NULL) {\n ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_EC_LIB);\n goto err;\n }\n if (BN_is_zero(sig->r) || BN_is_negative(sig->r) ||\n BN_ucmp(sig->r, order) >= 0 || BN_is_zero(sig->s) ||\n BN_is_negative(sig->s) || BN_ucmp(sig->s, order) >= 0) {\n ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, EC_R_BAD_SIGNATURE);\n ret = 0;\n goto err;\n }\n if (!BN_mod_inverse(u2, sig->s, order, ctx)) {\n ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);\n goto err;\n }\n i = BN_num_bits(order);\n if (8 * dgst_len > i)\n dgst_len = (i + 7) / 8;\n if (!BN_bin2bn(dgst, dgst_len, m)) {\n ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);\n goto err;\n }\n if ((8 * dgst_len > i) && !BN_rshift(m, m, 8 - (i & 0x7))) {\n ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);\n goto err;\n }\n if (!BN_mod_mul(u1, m, u2, order, ctx)) {\n ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);\n goto err;\n }\n if (!BN_mod_mul(u2, sig->r, u2, order, ctx)) {\n ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);\n goto err;\n }\n if ((point = EC_POINT_new(group)) == NULL) {\n ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n if (!EC_POINT_mul(group, point, u1, pub_key, u2, ctx)) {\n ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, 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(group, point, X, NULL, ctx)) {\n ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, 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, point, X, NULL, ctx)) {\n ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_EC_LIB);\n goto err;\n }\n }\n#endif\n if (!BN_nnmod(u1, X, order, ctx)) {\n ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);\n goto err;\n }\n ret = (BN_ucmp(u1, sig->r) == 0);\n err:\n BN_CTX_end(ctx);\n BN_CTX_free(ctx);\n EC_POINT_free(point);\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}', '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_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_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 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 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_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}']
|
2,137
| 0
|
https://github.com/openssl/openssl/blob/3f97052392cb10fca5309212bf720685262ad4a6/crypto/lhash/lhash.c/#L123
|
void *OPENSSL_LH_delete(OPENSSL_LHASH *lh, const void *data)
{
unsigned long hash;
OPENSSL_LH_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);
}
|
['static int test_custom_exts(int tst)\n{\n SSL_CTX *cctx = NULL, *sctx = NULL, *sctx2 = NULL;\n SSL *clientssl = NULL, *serverssl = NULL;\n int testresult = 0;\n static int server = 1;\n static int client = 0;\n SSL_SESSION *sess = NULL;\n unsigned int context;\n clntaddoldcb = clntparseoldcb = srvaddoldcb = srvparseoldcb = 0;\n clntaddnewcb = clntparsenewcb = srvaddnewcb = srvparsenewcb = 0;\n snicb = 0;\n if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(),\n TLS_client_method(), &sctx,\n &cctx, cert, privkey)))\n goto end;\n if (tst == 2\n && !TEST_true(create_ssl_ctx_pair(TLS_server_method(), NULL, &sctx2,\n NULL, cert, privkey)))\n goto end;\n if (tst < 3) {\n SSL_CTX_set_options(cctx, SSL_OP_NO_TLSv1_3);\n SSL_CTX_set_options(sctx, SSL_OP_NO_TLSv1_3);\n if (sctx2 != NULL)\n SSL_CTX_set_options(sctx2, SSL_OP_NO_TLSv1_3);\n }\n if (tst == 4) {\n context = SSL_EXT_CLIENT_HELLO\n | SSL_EXT_TLS1_2_SERVER_HELLO\n | SSL_EXT_TLS1_3_SERVER_HELLO\n | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS\n | SSL_EXT_TLS1_3_CERTIFICATE\n | SSL_EXT_TLS1_3_NEW_SESSION_TICKET;\n } else {\n context = SSL_EXT_CLIENT_HELLO\n | SSL_EXT_TLS1_2_SERVER_HELLO\n | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS;\n }\n if (tst == 0) {\n if (!TEST_true(SSL_CTX_add_client_custom_ext(cctx, TEST_EXT_TYPE1,\n old_add_cb, old_free_cb,\n &client, old_parse_cb,\n &client)))\n goto end;\n } else {\n if (!TEST_true(SSL_CTX_add_custom_ext(cctx, TEST_EXT_TYPE1, context,\n new_add_cb, new_free_cb,\n &client, new_parse_cb, &client)))\n goto end;\n }\n if (!TEST_false(SSL_CTX_add_client_custom_ext(cctx, TEST_EXT_TYPE1,\n old_add_cb, old_free_cb,\n &client, old_parse_cb,\n &client))\n || !TEST_false(SSL_CTX_add_custom_ext(cctx, TEST_EXT_TYPE1,\n context, new_add_cb,\n new_free_cb, &client,\n new_parse_cb, &client)))\n goto end;\n if (tst == 0) {\n if (!TEST_true(SSL_CTX_add_server_custom_ext(sctx, TEST_EXT_TYPE1,\n old_add_cb, old_free_cb,\n &server, old_parse_cb,\n &server)))\n goto end;\n } else {\n if (!TEST_true(SSL_CTX_add_custom_ext(sctx, TEST_EXT_TYPE1, context,\n new_add_cb, new_free_cb,\n &server, new_parse_cb, &server)))\n goto end;\n if (sctx2 != NULL\n && !TEST_true(SSL_CTX_add_custom_ext(sctx2, TEST_EXT_TYPE1,\n context, new_add_cb,\n new_free_cb, &server,\n new_parse_cb, &server)))\n goto end;\n }\n if (!TEST_false(SSL_CTX_add_server_custom_ext(sctx, TEST_EXT_TYPE1,\n old_add_cb, old_free_cb,\n &server, old_parse_cb,\n &server))\n || !TEST_false(SSL_CTX_add_custom_ext(sctx, TEST_EXT_TYPE1,\n context, new_add_cb,\n new_free_cb, &server,\n new_parse_cb, &server)))\n goto end;\n if (tst == 2) {\n if (!TEST_true(SSL_CTX_set_tlsext_servername_callback(sctx, sni_cb))\n || !TEST_true(SSL_CTX_set_tlsext_servername_arg(sctx, sctx2)))\n goto end;\n }\n if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,\n &clientssl, NULL, NULL))\n || !TEST_true(create_ssl_connection(serverssl, clientssl,\n SSL_ERROR_NONE)))\n goto end;\n if (tst == 0) {\n if (clntaddoldcb != 1\n || clntparseoldcb != 1\n || srvaddoldcb != 1\n || srvparseoldcb != 1)\n goto end;\n } else if (tst == 1 || tst == 2 || tst == 3) {\n if (clntaddnewcb != 1\n || clntparsenewcb != 1\n || srvaddnewcb != 1\n || srvparsenewcb != 1\n || (tst != 2 && snicb != 0)\n || (tst == 2 && snicb != 1))\n goto end;\n } else {\n if (clntaddnewcb != 1\n || clntparsenewcb != 4\n || srvaddnewcb != 4\n || srvparsenewcb != 1)\n goto end;\n }\n sess = SSL_get1_session(clientssl);\n SSL_shutdown(clientssl);\n SSL_shutdown(serverssl);\n SSL_free(serverssl);\n SSL_free(clientssl);\n serverssl = clientssl = NULL;\n if (tst == 3) {\n testresult = 1;\n goto end;\n }\n if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,\n NULL, NULL))\n || !TEST_true(SSL_set_session(clientssl, sess))\n || !TEST_true(create_ssl_connection(serverssl, clientssl,\n SSL_ERROR_NONE)))\n goto end;\n if (tst == 0) {\n if (clntaddoldcb != 2\n || clntparseoldcb != 1\n || srvaddoldcb != 1\n || srvparseoldcb != 1)\n goto end;\n } else if (tst == 1 || tst == 2 || tst == 3) {\n if (clntaddnewcb != 2\n || clntparsenewcb != 2\n || srvaddnewcb != 2\n || srvparsenewcb != 2)\n goto end;\n } else {\n if (clntaddnewcb != 2\n || clntparsenewcb != 7\n || srvaddnewcb != 7\n || srvparsenewcb != 2)\n goto end;\n }\n testresult = 1;\nend:\n SSL_SESSION_free(sess);\n SSL_free(serverssl);\n SSL_free(clientssl);\n SSL_CTX_free(sctx2);\n SSL_CTX_free(sctx);\n SSL_CTX_free(cctx);\n return testresult;\n}', 'int create_ssl_objects(SSL_CTX *serverctx, SSL_CTX *clientctx, SSL **sssl,\n SSL **cssl, BIO *s_to_c_fbio, BIO *c_to_s_fbio)\n{\n SSL *serverssl = NULL, *clientssl = NULL;\n BIO *s_to_c_bio = NULL, *c_to_s_bio = NULL;\n if (*sssl != NULL)\n serverssl = *sssl;\n else if (!TEST_ptr(serverssl = SSL_new(serverctx)))\n goto error;\n if (*cssl != NULL)\n clientssl = *cssl;\n else if (!TEST_ptr(clientssl = SSL_new(clientctx)))\n goto error;\n if (SSL_is_dtls(clientssl)) {\n if (!TEST_ptr(s_to_c_bio = BIO_new(bio_s_mempacket_test()))\n || !TEST_ptr(c_to_s_bio = BIO_new(bio_s_mempacket_test())))\n goto error;\n } else {\n if (!TEST_ptr(s_to_c_bio = BIO_new(BIO_s_mem()))\n || !TEST_ptr(c_to_s_bio = BIO_new(BIO_s_mem())))\n goto error;\n }\n if (s_to_c_fbio != NULL\n && !TEST_ptr(s_to_c_bio = BIO_push(s_to_c_fbio, s_to_c_bio)))\n goto error;\n if (c_to_s_fbio != NULL\n && !TEST_ptr(c_to_s_bio = BIO_push(c_to_s_fbio, c_to_s_bio)))\n goto error;\n BIO_set_mem_eof_return(s_to_c_bio, -1);\n BIO_set_mem_eof_return(c_to_s_bio, -1);\n SSL_set_bio(serverssl, c_to_s_bio, s_to_c_bio);\n BIO_up_ref(s_to_c_bio);\n BIO_up_ref(c_to_s_bio);\n SSL_set_bio(clientssl, s_to_c_bio, c_to_s_bio);\n *sssl = serverssl;\n *cssl = clientssl;\n return 1;\n error:\n SSL_free(serverssl);\n SSL_free(clientssl);\n BIO_free(s_to_c_bio);\n BIO_free(c_to_s_bio);\n BIO_free(s_to_c_fbio);\n BIO_free(c_to_s_fbio);\n return 0;\n}', 'SSL *SSL_new(SSL_CTX *ctx)\n{\n SSL *s;\n if (ctx == NULL) {\n SSLerr(SSL_F_SSL_NEW, SSL_R_NULL_SSL_CTX);\n return (NULL);\n }\n if (ctx->method == NULL) {\n SSLerr(SSL_F_SSL_NEW, SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);\n return (NULL);\n }\n s = OPENSSL_zalloc(sizeof(*s));\n if (s == NULL)\n goto err;\n s->lock = CRYPTO_THREAD_lock_new();\n if (s->lock == NULL) {\n SSLerr(SSL_F_SSL_NEW, ERR_R_MALLOC_FAILURE);\n OPENSSL_free(s);\n return NULL;\n }\n RECORD_LAYER_init(&s->rlayer, s);\n s->options = ctx->options;\n s->dane.flags = ctx->dane.flags;\n s->min_proto_version = ctx->min_proto_version;\n s->max_proto_version = ctx->max_proto_version;\n s->mode = ctx->mode;\n s->max_cert_list = ctx->max_cert_list;\n s->references = 1;\n s->max_early_data = ctx->max_early_data;\n s->cert = ssl_cert_dup(ctx->cert);\n if (s->cert == NULL)\n goto err;\n RECORD_LAYER_set_read_ahead(&s->rlayer, ctx->read_ahead);\n s->msg_callback = ctx->msg_callback;\n s->msg_callback_arg = ctx->msg_callback_arg;\n s->verify_mode = ctx->verify_mode;\n s->not_resumable_session_cb = ctx->not_resumable_session_cb;\n s->record_padding_cb = ctx->record_padding_cb;\n s->record_padding_arg = ctx->record_padding_arg;\n s->block_padding = ctx->block_padding;\n s->sid_ctx_length = ctx->sid_ctx_length;\n OPENSSL_assert(s->sid_ctx_length <= sizeof s->sid_ctx);\n memcpy(&s->sid_ctx, &ctx->sid_ctx, sizeof(s->sid_ctx));\n s->verify_callback = ctx->default_verify_callback;\n s->generate_session_id = ctx->generate_session_id;\n s->param = X509_VERIFY_PARAM_new();\n if (s->param == NULL)\n goto err;\n X509_VERIFY_PARAM_inherit(s->param, ctx->param);\n s->quiet_shutdown = ctx->quiet_shutdown;\n s->max_send_fragment = ctx->max_send_fragment;\n s->split_send_fragment = ctx->split_send_fragment;\n s->max_pipelines = ctx->max_pipelines;\n if (s->max_pipelines > 1)\n RECORD_LAYER_set_read_ahead(&s->rlayer, 1);\n if (ctx->default_read_buf_len > 0)\n SSL_set_default_read_buffer_len(s, ctx->default_read_buf_len);\n SSL_CTX_up_ref(ctx);\n s->ctx = ctx;\n s->ext.debug_cb = 0;\n s->ext.debug_arg = NULL;\n s->ext.ticket_expected = 0;\n s->ext.status_type = ctx->ext.status_type;\n s->ext.status_expected = 0;\n s->ext.ocsp.ids = NULL;\n s->ext.ocsp.exts = NULL;\n s->ext.ocsp.resp = NULL;\n s->ext.ocsp.resp_len = 0;\n SSL_CTX_up_ref(ctx);\n s->session_ctx = ctx;\n#ifndef OPENSSL_NO_EC\n if (ctx->ext.ecpointformats) {\n s->ext.ecpointformats =\n OPENSSL_memdup(ctx->ext.ecpointformats,\n ctx->ext.ecpointformats_len);\n if (!s->ext.ecpointformats)\n goto err;\n s->ext.ecpointformats_len =\n ctx->ext.ecpointformats_len;\n }\n if (ctx->ext.supportedgroups) {\n s->ext.supportedgroups =\n OPENSSL_memdup(ctx->ext.supportedgroups,\n ctx->ext.supportedgroups_len);\n if (!s->ext.supportedgroups)\n goto err;\n s->ext.supportedgroups_len = ctx->ext.supportedgroups_len;\n }\n#endif\n#ifndef OPENSSL_NO_NEXTPROTONEG\n s->ext.npn = NULL;\n#endif\n if (s->ctx->ext.alpn) {\n s->ext.alpn = OPENSSL_malloc(s->ctx->ext.alpn_len);\n if (s->ext.alpn == NULL)\n goto err;\n memcpy(s->ext.alpn, s->ctx->ext.alpn, s->ctx->ext.alpn_len);\n s->ext.alpn_len = s->ctx->ext.alpn_len;\n }\n s->verified_chain = NULL;\n s->verify_result = X509_V_OK;\n s->default_passwd_callback = ctx->default_passwd_callback;\n s->default_passwd_callback_userdata = ctx->default_passwd_callback_userdata;\n s->method = ctx->method;\n s->key_update = SSL_KEY_UPDATE_NONE;\n if (!s->method->ssl_new(s))\n goto err;\n s->server = (ctx->method->ssl_accept == ssl_undefined_function) ? 0 : 1;\n if (!SSL_clear(s))\n goto err;\n if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data))\n goto err;\n#ifndef OPENSSL_NO_PSK\n s->psk_client_callback = ctx->psk_client_callback;\n s->psk_server_callback = ctx->psk_server_callback;\n#endif\n s->job = NULL;\n#ifndef OPENSSL_NO_CT\n if (!SSL_set_ct_validation_callback(s, ctx->ct_validation_callback,\n ctx->ct_validation_callback_arg))\n goto err;\n#endif\n return s;\n err:\n SSL_free(s);\n SSLerr(SSL_F_SSL_NEW, ERR_R_MALLOC_FAILURE);\n return NULL;\n}', 'void SSL_free(SSL *s)\n{\n int i;\n if (s == NULL)\n return;\n CRYPTO_DOWN_REF(&s->references, &i, s->lock);\n REF_PRINT_COUNT("SSL", s);\n if (i > 0)\n return;\n REF_ASSERT_ISNT(i < 0);\n X509_VERIFY_PARAM_free(s->param);\n dane_final(&s->dane);\n CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data);\n ssl_free_wbio_buffer(s);\n BIO_free_all(s->wbio);\n BIO_free_all(s->rbio);\n BUF_MEM_free(s->init_buf);\n sk_SSL_CIPHER_free(s->cipher_list);\n sk_SSL_CIPHER_free(s->cipher_list_by_id);\n if (s->session != NULL) {\n ssl_clear_bad_session(s);\n SSL_SESSION_free(s->session);\n }\n clear_ciphers(s);\n ssl_cert_free(s->cert);\n OPENSSL_free(s->ext.hostname);\n SSL_CTX_free(s->session_ctx);\n#ifndef OPENSSL_NO_EC\n OPENSSL_free(s->ext.ecpointformats);\n OPENSSL_free(s->ext.supportedgroups);\n#endif\n sk_X509_EXTENSION_pop_free(s->ext.ocsp.exts, X509_EXTENSION_free);\n#ifndef OPENSSL_NO_OCSP\n sk_OCSP_RESPID_pop_free(s->ext.ocsp.ids, OCSP_RESPID_free);\n#endif\n#ifndef OPENSSL_NO_CT\n SCT_LIST_free(s->scts);\n OPENSSL_free(s->ext.scts);\n#endif\n OPENSSL_free(s->ext.ocsp.resp);\n OPENSSL_free(s->ext.alpn);\n OPENSSL_free(s->ext.tls13_cookie);\n OPENSSL_free(s->clienthello);\n sk_X509_NAME_pop_free(s->ca_names, X509_NAME_free);\n sk_X509_pop_free(s->verified_chain, X509_free);\n if (s->method != NULL)\n s->method->ssl_free(s);\n RECORD_LAYER_release(&s->rlayer);\n SSL_CTX_free(s->ctx);\n ASYNC_WAIT_CTX_free(s->waitctx);\n#if !defined(OPENSSL_NO_NEXTPROTONEG)\n OPENSSL_free(s->ext.npn);\n#endif\n#ifndef OPENSSL_NO_SRTP\n sk_SRTP_PROTECTION_PROFILE_free(s->srtp_profiles);\n#endif\n CRYPTO_THREAD_lock_free(s->lock);\n OPENSSL_free(s);\n}', 'int ssl_clear_bad_session(SSL *s)\n{\n if ((s->session != NULL) &&\n !(s->shutdown & SSL_SENT_SHUTDOWN) &&\n !(SSL_in_init(s) || SSL_in_before(s))) {\n SSL_CTX_remove_session(s->session_ctx, s->session);\n return (1);\n } else\n return (0);\n}', 'int SSL_CTX_remove_session(SSL_CTX *ctx, SSL_SESSION *c)\n{\n return remove_session_lock(ctx, c, 1);\n}', 'static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lck)\n{\n SSL_SESSION *r;\n int ret = 0;\n if ((c != NULL) && (c->session_id_length != 0)) {\n if (lck)\n CRYPTO_THREAD_write_lock(ctx->lock);\n if ((r = lh_SSL_SESSION_retrieve(ctx->sessions, c)) == c) {\n ret = 1;\n r = lh_SSL_SESSION_delete(ctx->sessions, c);\n SSL_SESSION_list_remove(ctx, c);\n }\n c->not_resumable = 1;\n if (lck)\n CRYPTO_THREAD_unlock(ctx->lock);\n if (ret)\n SSL_SESSION_free(r);\n if (ctx->remove_session_cb != NULL)\n ctx->remove_session_cb(ctx, c);\n } else\n ret = 0;\n return (ret);\n}', 'DEFINE_LHASH_OF(SSL_SESSION)', 'void *OPENSSL_LH_delete(OPENSSL_LHASH *lh, const void *data)\n{\n unsigned long hash;\n OPENSSL_LH_NODE *nn, **rn;\n void *ret;\n lh->error = 0;\n rn = getrn(lh, data, &hash);\n if (*rn == NULL) {\n lh->num_no_delete++;\n return (NULL);\n } else {\n nn = *rn;\n *rn = nn->next;\n ret = nn->data;\n OPENSSL_free(nn);\n lh->num_delete++;\n }\n lh->num_items--;\n if ((lh->num_nodes > MIN_NODES) &&\n (lh->down_load >= (lh->num_items * LH_LOAD_MULT / lh->num_nodes)))\n contract(lh);\n return (ret);\n}']
|
2,138
| 1
|
https://github.com/libav/libav/blob/08e3ea60ff4059341b74be04a428a38f7c3630b0/libavcodec/dcadec.c/#L1200
|
static int dca_subsubframe(DCAContext *s, int base_channel, int block_index)
{
int k, l;
int subsubframe = s->current_subsubframe;
const float *quant_step_table;
float (*subband_samples)[DCA_SUBBANDS][8] = s->subband_samples[block_index];
LOCAL_ALIGNED_16(int32_t, block, [8 * DCA_SUBBANDS]);
if (s->bit_rate_index == 0x1f)
quant_step_table = lossless_quant_d;
else
quant_step_table = lossy_quant_d;
for (k = base_channel; k < s->prim_channels; k++) {
float rscale[DCA_SUBBANDS];
if (get_bits_left(&s->gb) < 0)
return AVERROR_INVALIDDATA;
for (l = 0; l < s->vq_start_subband[k]; l++) {
int m;
int abits = s->bitalloc[k][l];
float quant_step_size = quant_step_table[abits];
int sel = s->quant_index_huffman[k][abits];
if (!abits) {
rscale[l] = 0;
memset(block + 8 * l, 0, 8 * sizeof(block[0]));
} else {
int sfi = s->transition_mode[k][l] && subsubframe >= s->transition_mode[k][l];
rscale[l] = quant_step_size * s->scale_factor[k][l][sfi] *
s->scalefactor_adj[k][sel];
if (abits >= 11 || !dca_smpl_bitalloc[abits].vlc[sel].table) {
if (abits <= 7) {
int block_code1, block_code2, size, levels, err;
size = abits_sizes[abits - 1];
levels = abits_levels[abits - 1];
block_code1 = get_bits(&s->gb, size);
block_code2 = get_bits(&s->gb, size);
err = decode_blockcodes(block_code1, block_code2,
levels, block + 8 * l);
if (err) {
av_log(s->avctx, AV_LOG_ERROR,
"ERROR: block code look-up failed\n");
return AVERROR_INVALIDDATA;
}
} else {
for (m = 0; m < 8; m++)
block[8 * l + m] = get_sbits(&s->gb, abits - 3);
}
} else {
for (m = 0; m < 8; m++)
block[8 * l + m] = get_bitalloc(&s->gb,
&dca_smpl_bitalloc[abits], sel);
}
}
}
s->fmt_conv.int32_to_float_fmul_array8(&s->fmt_conv, subband_samples[k][0],
block, rscale, 8 * s->vq_start_subband[k]);
for (l = 0; l < s->vq_start_subband[k]; l++) {
int m;
if (s->prediction_mode[k][l]) {
int n;
for (m = 0; m < 8; m++) {
for (n = 1; n <= 4; n++)
if (m >= n)
subband_samples[k][l][m] +=
(adpcm_vb[s->prediction_vq[k][l]][n - 1] *
subband_samples[k][l][m - n] / 8192);
else if (s->predictor_history)
subband_samples[k][l][m] +=
(adpcm_vb[s->prediction_vq[k][l]][n - 1] *
s->subband_samples_hist[k][l][m - n + 4] / 8192);
}
}
}
for (l = s->vq_start_subband[k]; l < s->subband_activity[k]; l++) {
int hfvq = s->high_freq_vq[k][l];
if (!s->debug_flag & 0x01) {
av_log(s->avctx, AV_LOG_DEBUG,
"Stream with high frequencies VQ coding\n");
s->debug_flag |= 0x01;
}
int8x8_fmul_int32(&s->dcadsp, subband_samples[k][l],
&high_freq_vq[hfvq][subsubframe * 8],
s->scale_factor[k][l][0]);
}
}
if (s->aspf || subsubframe == s->subsubframes[s->current_subframe] - 1) {
if (0xFFFF == get_bits(&s->gb, 16)) {
#ifdef TRACE
av_log(s->avctx, AV_LOG_DEBUG, "Got subframe DSYNC\n");
#endif
} else {
av_log(s->avctx, AV_LOG_ERROR, "Didn't get subframe DSYNC\n");
return AVERROR_INVALIDDATA;
}
}
for (k = base_channel; k < s->prim_channels; k++)
for (l = 0; l < s->vq_start_subband[k]; l++)
AV_COPY128(s->subband_samples_hist[k][l], &subband_samples[k][l][4]);
return 0;
}
|
['static int dca_subsubframe(DCAContext *s, int base_channel, int block_index)\n{\n int k, l;\n int subsubframe = s->current_subsubframe;\n const float *quant_step_table;\n float (*subband_samples)[DCA_SUBBANDS][8] = s->subband_samples[block_index];\n LOCAL_ALIGNED_16(int32_t, block, [8 * DCA_SUBBANDS]);\n if (s->bit_rate_index == 0x1f)\n quant_step_table = lossless_quant_d;\n else\n quant_step_table = lossy_quant_d;\n for (k = base_channel; k < s->prim_channels; k++) {\n float rscale[DCA_SUBBANDS];\n if (get_bits_left(&s->gb) < 0)\n return AVERROR_INVALIDDATA;\n for (l = 0; l < s->vq_start_subband[k]; l++) {\n int m;\n int abits = s->bitalloc[k][l];\n float quant_step_size = quant_step_table[abits];\n int sel = s->quant_index_huffman[k][abits];\n if (!abits) {\n rscale[l] = 0;\n memset(block + 8 * l, 0, 8 * sizeof(block[0]));\n } else {\n int sfi = s->transition_mode[k][l] && subsubframe >= s->transition_mode[k][l];\n rscale[l] = quant_step_size * s->scale_factor[k][l][sfi] *\n s->scalefactor_adj[k][sel];\n if (abits >= 11 || !dca_smpl_bitalloc[abits].vlc[sel].table) {\n if (abits <= 7) {\n int block_code1, block_code2, size, levels, err;\n size = abits_sizes[abits - 1];\n levels = abits_levels[abits - 1];\n block_code1 = get_bits(&s->gb, size);\n block_code2 = get_bits(&s->gb, size);\n err = decode_blockcodes(block_code1, block_code2,\n levels, block + 8 * l);\n if (err) {\n av_log(s->avctx, AV_LOG_ERROR,\n "ERROR: block code look-up failed\\n");\n return AVERROR_INVALIDDATA;\n }\n } else {\n for (m = 0; m < 8; m++)\n block[8 * l + m] = get_sbits(&s->gb, abits - 3);\n }\n } else {\n for (m = 0; m < 8; m++)\n block[8 * l + m] = get_bitalloc(&s->gb,\n &dca_smpl_bitalloc[abits], sel);\n }\n }\n }\n s->fmt_conv.int32_to_float_fmul_array8(&s->fmt_conv, subband_samples[k][0],\n block, rscale, 8 * s->vq_start_subband[k]);\n for (l = 0; l < s->vq_start_subband[k]; l++) {\n int m;\n if (s->prediction_mode[k][l]) {\n int n;\n for (m = 0; m < 8; m++) {\n for (n = 1; n <= 4; n++)\n if (m >= n)\n subband_samples[k][l][m] +=\n (adpcm_vb[s->prediction_vq[k][l]][n - 1] *\n subband_samples[k][l][m - n] / 8192);\n else if (s->predictor_history)\n subband_samples[k][l][m] +=\n (adpcm_vb[s->prediction_vq[k][l]][n - 1] *\n s->subband_samples_hist[k][l][m - n + 4] / 8192);\n }\n }\n }\n for (l = s->vq_start_subband[k]; l < s->subband_activity[k]; l++) {\n int hfvq = s->high_freq_vq[k][l];\n if (!s->debug_flag & 0x01) {\n av_log(s->avctx, AV_LOG_DEBUG,\n "Stream with high frequencies VQ coding\\n");\n s->debug_flag |= 0x01;\n }\n int8x8_fmul_int32(&s->dcadsp, subband_samples[k][l],\n &high_freq_vq[hfvq][subsubframe * 8],\n s->scale_factor[k][l][0]);\n }\n }\n if (s->aspf || subsubframe == s->subsubframes[s->current_subframe] - 1) {\n if (0xFFFF == get_bits(&s->gb, 16)) {\n#ifdef TRACE\n av_log(s->avctx, AV_LOG_DEBUG, "Got subframe DSYNC\\n");\n#endif\n } else {\n av_log(s->avctx, AV_LOG_ERROR, "Didn\'t get subframe DSYNC\\n");\n return AVERROR_INVALIDDATA;\n }\n }\n for (k = base_channel; k < s->prim_channels; k++)\n for (l = 0; l < s->vq_start_subband[k]; l++)\n AV_COPY128(s->subband_samples_hist[k][l], &subband_samples[k][l][4]);\n return 0;\n}']
|
2,139
| 0
|
https://github.com/libav/libav/blob/75366a504dfc30deadeac71c35e3c444275986f9/libavcodec/huffyuv.c/#L343
|
static int read_old_huffman_tables(HYuvContext *s)
{
GetBitContext gb;
int i;
init_get_bits(&gb, classic_shift_luma,
classic_shift_luma_table_size * 8);
if (read_len_table(s->len[0], &gb) < 0)
return -1;
init_get_bits(&gb, classic_shift_chroma,
classic_shift_chroma_table_size * 8);
if (read_len_table(s->len[1], &gb) < 0)
return -1;
for(i=0; i<256; i++) s->bits[0][i] = classic_add_luma [i];
for(i=0; i<256; i++) s->bits[1][i] = classic_add_chroma[i];
if (s->bitstream_bpp >= 24) {
memcpy(s->bits[1], s->bits[0], 256 * sizeof(uint32_t));
memcpy(s->len[1] , s->len [0], 256 * sizeof(uint8_t));
}
memcpy(s->bits[2], s->bits[1], 256 * sizeof(uint32_t));
memcpy(s->len[2] , s->len [1], 256 * sizeof(uint8_t));
for (i = 0; i < 3; i++) {
ff_free_vlc(&s->vlc[i]);
init_vlc(&s->vlc[i], VLC_BITS, 256, s->len[i], 1, 1,
s->bits[i], 4, 4, 0);
}
generate_joint_tables(s);
return 0;
}
|
['static int read_old_huffman_tables(HYuvContext *s)\n{\n GetBitContext gb;\n int i;\n init_get_bits(&gb, classic_shift_luma,\n classic_shift_luma_table_size * 8);\n if (read_len_table(s->len[0], &gb) < 0)\n return -1;\n init_get_bits(&gb, classic_shift_chroma,\n classic_shift_chroma_table_size * 8);\n if (read_len_table(s->len[1], &gb) < 0)\n return -1;\n for(i=0; i<256; i++) s->bits[0][i] = classic_add_luma [i];\n for(i=0; i<256; i++) s->bits[1][i] = classic_add_chroma[i];\n if (s->bitstream_bpp >= 24) {\n memcpy(s->bits[1], s->bits[0], 256 * sizeof(uint32_t));\n memcpy(s->len[1] , s->len [0], 256 * sizeof(uint8_t));\n }\n memcpy(s->bits[2], s->bits[1], 256 * sizeof(uint32_t));\n memcpy(s->len[2] , s->len [1], 256 * sizeof(uint8_t));\n for (i = 0; i < 3; i++) {\n ff_free_vlc(&s->vlc[i]);\n init_vlc(&s->vlc[i], VLC_BITS, 256, s->len[i], 1, 1,\n s->bits[i], 4, 4, 0);\n }\n generate_joint_tables(s);\n return 0;\n}']
|
2,140
| 0
|
https://github.com/openssl/openssl/blob/f3f52d7f45967af4f70045921dfa12e6faedcc92/engines/e_ncipher.c/#L801
|
static EVP_PKEY *hwcrhk_load_privkey(ENGINE *eng, const char *key_id,
UI_METHOD *ui_method, void *callback_data)
{
#ifndef OPENSSL_NO_RSA
RSA *rtmp = NULL;
#endif
EVP_PKEY *res = NULL;
#ifndef OPENSSL_NO_RSA
HWCryptoHook_MPI e, n;
HWCryptoHook_RSAKeyHandle *hptr;
#endif
#if !defined(OPENSSL_NO_RSA)
char tempbuf[1024];
HWCryptoHook_ErrMsgBuf rmsg;
#endif
HWCryptoHook_PassphraseContext ppctx;
#if !defined(OPENSSL_NO_RSA)
rmsg.buf = tempbuf;
rmsg.size = sizeof(tempbuf);
#endif
if(!hwcrhk_context)
{
HWCRHKerr(HWCRHK_F_HWCRHK_LOAD_PRIVKEY,
HWCRHK_R_NOT_INITIALISED);
goto err;
}
#ifndef OPENSSL_NO_RSA
hptr = OPENSSL_malloc(sizeof(HWCryptoHook_RSAKeyHandle));
if (!hptr)
{
HWCRHKerr(HWCRHK_F_HWCRHK_LOAD_PRIVKEY,
ERR_R_MALLOC_FAILURE);
goto err;
}
ppctx.ui_method = ui_method;
ppctx.callback_data = callback_data;
if (p_hwcrhk_RSALoadKey(hwcrhk_context, key_id, hptr,
&rmsg, &ppctx))
{
HWCRHKerr(HWCRHK_F_HWCRHK_LOAD_PRIVKEY,
HWCRHK_R_CHIL_ERROR);
ERR_add_error_data(1,rmsg.buf);
goto err;
}
if (!*hptr)
{
HWCRHKerr(HWCRHK_F_HWCRHK_LOAD_PRIVKEY,
HWCRHK_R_NO_KEY);
goto err;
}
#endif
#ifndef OPENSSL_NO_RSA
rtmp = RSA_new_method(eng);
RSA_set_ex_data(rtmp, hndidx_rsa, (char *)hptr);
rtmp->e = BN_new();
rtmp->n = BN_new();
rtmp->flags |= RSA_FLAG_EXT_PKEY;
MPI2BN(rtmp->e, e);
MPI2BN(rtmp->n, n);
if (p_hwcrhk_RSAGetPublicKey(*hptr, &n, &e, &rmsg)
!= HWCRYPTOHOOK_ERROR_MPISIZE)
{
HWCRHKerr(HWCRHK_F_HWCRHK_LOAD_PUBKEY,HWCRHK_R_CHIL_ERROR);
ERR_add_error_data(1,rmsg.buf);
goto err;
}
bn_expand2(rtmp->e, e.size/sizeof(BN_ULONG));
bn_expand2(rtmp->n, n.size/sizeof(BN_ULONG));
MPI2BN(rtmp->e, e);
MPI2BN(rtmp->n, n);
if (p_hwcrhk_RSAGetPublicKey(*hptr, &n, &e, &rmsg))
{
HWCRHKerr(HWCRHK_F_HWCRHK_LOAD_PUBKEY,
HWCRHK_R_CHIL_ERROR);
ERR_add_error_data(1,rmsg.buf);
goto err;
}
rtmp->e->top = e.size / sizeof(BN_ULONG);
bn_fix_top(rtmp->e);
rtmp->n->top = n.size / sizeof(BN_ULONG);
bn_fix_top(rtmp->n);
res = EVP_PKEY_new();
EVP_PKEY_assign_RSA(res, rtmp);
#endif
if (!res)
HWCRHKerr(HWCRHK_F_HWCRHK_LOAD_PUBKEY,
HWCRHK_R_PRIVATE_KEY_ALGORITHMS_DISABLED);
return res;
err:
if (res)
EVP_PKEY_free(res);
#ifndef OPENSSL_NO_RSA
if (rtmp)
RSA_free(rtmp);
#endif
return NULL;
}
|
['static EVP_PKEY *hwcrhk_load_privkey(ENGINE *eng, const char *key_id,\n\tUI_METHOD *ui_method, void *callback_data)\n\t{\n#ifndef OPENSSL_NO_RSA\n\tRSA *rtmp = NULL;\n#endif\n\tEVP_PKEY *res = NULL;\n#ifndef OPENSSL_NO_RSA\n\tHWCryptoHook_MPI e, n;\n\tHWCryptoHook_RSAKeyHandle *hptr;\n#endif\n#if !defined(OPENSSL_NO_RSA)\n\tchar tempbuf[1024];\n\tHWCryptoHook_ErrMsgBuf rmsg;\n#endif\n\tHWCryptoHook_PassphraseContext ppctx;\n#if !defined(OPENSSL_NO_RSA)\n\trmsg.buf = tempbuf;\n\trmsg.size = sizeof(tempbuf);\n#endif\n\tif(!hwcrhk_context)\n\t\t{\n\t\tHWCRHKerr(HWCRHK_F_HWCRHK_LOAD_PRIVKEY,\n\t\t\tHWCRHK_R_NOT_INITIALISED);\n\t\tgoto err;\n\t\t}\n#ifndef OPENSSL_NO_RSA\n\thptr = OPENSSL_malloc(sizeof(HWCryptoHook_RSAKeyHandle));\n\tif (!hptr)\n\t\t{\n\t\tHWCRHKerr(HWCRHK_F_HWCRHK_LOAD_PRIVKEY,\n\t\t\tERR_R_MALLOC_FAILURE);\n\t\tgoto err;\n\t\t}\n ppctx.ui_method = ui_method;\n\tppctx.callback_data = callback_data;\n\tif (p_hwcrhk_RSALoadKey(hwcrhk_context, key_id, hptr,\n\t\t&rmsg, &ppctx))\n\t\t{\n\t\tHWCRHKerr(HWCRHK_F_HWCRHK_LOAD_PRIVKEY,\n\t\t\tHWCRHK_R_CHIL_ERROR);\n\t\tERR_add_error_data(1,rmsg.buf);\n\t\tgoto err;\n\t\t}\n\tif (!*hptr)\n\t\t{\n\t\tHWCRHKerr(HWCRHK_F_HWCRHK_LOAD_PRIVKEY,\n\t\t\tHWCRHK_R_NO_KEY);\n\t\tgoto err;\n\t\t}\n#endif\n#ifndef OPENSSL_NO_RSA\n\trtmp = RSA_new_method(eng);\n\tRSA_set_ex_data(rtmp, hndidx_rsa, (char *)hptr);\n\trtmp->e = BN_new();\n\trtmp->n = BN_new();\n\trtmp->flags |= RSA_FLAG_EXT_PKEY;\n\tMPI2BN(rtmp->e, e);\n\tMPI2BN(rtmp->n, n);\n\tif (p_hwcrhk_RSAGetPublicKey(*hptr, &n, &e, &rmsg)\n\t\t!= HWCRYPTOHOOK_ERROR_MPISIZE)\n\t\t{\n\t\tHWCRHKerr(HWCRHK_F_HWCRHK_LOAD_PUBKEY,HWCRHK_R_CHIL_ERROR);\n\t\tERR_add_error_data(1,rmsg.buf);\n\t\tgoto err;\n\t\t}\n\tbn_expand2(rtmp->e, e.size/sizeof(BN_ULONG));\n\tbn_expand2(rtmp->n, n.size/sizeof(BN_ULONG));\n\tMPI2BN(rtmp->e, e);\n\tMPI2BN(rtmp->n, n);\n\tif (p_hwcrhk_RSAGetPublicKey(*hptr, &n, &e, &rmsg))\n\t\t{\n\t\tHWCRHKerr(HWCRHK_F_HWCRHK_LOAD_PUBKEY,\n\t\t\tHWCRHK_R_CHIL_ERROR);\n\t\tERR_add_error_data(1,rmsg.buf);\n\t\tgoto err;\n\t\t}\n\trtmp->e->top = e.size / sizeof(BN_ULONG);\n\tbn_fix_top(rtmp->e);\n\trtmp->n->top = n.size / sizeof(BN_ULONG);\n\tbn_fix_top(rtmp->n);\n\tres = EVP_PKEY_new();\n\tEVP_PKEY_assign_RSA(res, rtmp);\n#endif\n if (!res)\n HWCRHKerr(HWCRHK_F_HWCRHK_LOAD_PUBKEY,\n HWCRHK_R_PRIVATE_KEY_ALGORITHMS_DISABLED);\n\treturn res;\n err:\n\tif (res)\n\t\tEVP_PKEY_free(res);\n#ifndef OPENSSL_NO_RSA\n\tif (rtmp)\n\t\tRSA_free(rtmp);\n#endif\n\treturn NULL;\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}', 'RSA *RSA_new_method(ENGINE *engine)\n\t{\n\tRSA *ret;\n\tret=(RSA *)OPENSSL_malloc(sizeof(RSA));\n\tif (ret == NULL)\n\t\t{\n\t\tRSAerr(RSA_F_RSA_NEW_METHOD,ERR_R_MALLOC_FAILURE);\n\t\treturn NULL;\n\t\t}\n\tret->meth = RSA_get_default_method();\n#ifndef OPENSSL_NO_ENGINE\n\tif (engine)\n\t\t{\n\t\tif (!ENGINE_init(engine))\n\t\t\t{\n\t\t\tRSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_ENGINE_LIB);\n\t\t\tOPENSSL_free(ret);\n\t\t\treturn NULL;\n\t\t\t}\n\t\tret->engine = engine;\n\t\t}\n\telse\n\t\tret->engine = ENGINE_get_default_RSA();\n\tif(ret->engine)\n\t\t{\n\t\tret->meth = ENGINE_get_RSA(ret->engine);\n\t\tif(!ret->meth)\n\t\t\t{\n\t\t\tRSAerr(RSA_F_RSA_NEW_METHOD,\n\t\t\t\tERR_R_ENGINE_LIB);\n\t\t\tENGINE_finish(ret->engine);\n\t\t\tOPENSSL_free(ret);\n\t\t\treturn NULL;\n\t\t\t}\n\t\t}\n#endif\n\tret->pad=0;\n\tret->version=0;\n\tret->n=NULL;\n\tret->e=NULL;\n\tret->d=NULL;\n\tret->p=NULL;\n\tret->q=NULL;\n\tret->dmp1=NULL;\n\tret->dmq1=NULL;\n\tret->iqmp=NULL;\n\tret->references=1;\n\tret->_method_mod_n=NULL;\n\tret->_method_mod_p=NULL;\n\tret->_method_mod_q=NULL;\n\tret->blinding=NULL;\n\tret->bignum_data=NULL;\n\tret->flags=ret->meth->flags;\n\tCRYPTO_new_ex_data(CRYPTO_EX_INDEX_RSA, ret, &ret->ex_data);\n\tif ((ret->meth->init != NULL) && !ret->meth->init(ret))\n\t\t{\n#ifndef OPENSSL_NO_ENGINE\n\t\tif (ret->engine)\n\t\t\tENGINE_finish(ret->engine);\n#endif\n\t\tCRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, ret, &ret->ex_data);\n\t\tOPENSSL_free(ret);\n\t\tret=NULL;\n\t\t}\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}']
|
2,141
| 0
|
https://github.com/openssl/openssl/blob/9c46f4b9cd4912b61cb546c48b678488d7f26ed6/crypto/bn/bn_ctx.c/#L437
|
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) {
offset = BN_CTX_POOL_SIZE - 1;
p->current = p->current->prev;
} else
offset--;
}
}
|
['int ec_GF2m_simple_is_on_curve(const EC_GROUP *group, const EC_POINT *point,\n BN_CTX *ctx)\n{\n int ret = -1;\n BN_CTX *new_ctx = NULL;\n BIGNUM *lh, *y2;\n int (*field_mul) (const EC_GROUP *, BIGNUM *, const BIGNUM *,\n const BIGNUM *, BN_CTX *);\n int (*field_sqr) (const EC_GROUP *, BIGNUM *, const BIGNUM *, BN_CTX *);\n if (EC_POINT_is_at_infinity(group, point))\n return 1;\n field_mul = group->meth->field_mul;\n field_sqr = group->meth->field_sqr;\n if (!point->Z_is_one)\n return -1;\n if (ctx == NULL) {\n ctx = new_ctx = BN_CTX_new();\n if (ctx == NULL)\n return -1;\n }\n BN_CTX_start(ctx);\n y2 = BN_CTX_get(ctx);\n lh = BN_CTX_get(ctx);\n if (lh == NULL)\n goto err;\n if (!BN_GF2m_add(lh, point->X, group->a))\n goto err;\n if (!field_mul(group, lh, lh, point->X, ctx))\n goto err;\n if (!BN_GF2m_add(lh, lh, point->Y))\n goto err;\n if (!field_mul(group, lh, lh, point->X, ctx))\n goto err;\n if (!BN_GF2m_add(lh, lh, group->b))\n goto err;\n if (!field_sqr(group, y2, point->Y, ctx))\n goto err;\n if (!BN_GF2m_add(lh, lh, y2))\n goto err;\n ret = BN_is_zero(lh);\n err:\n if (ctx)\n BN_CTX_end(ctx);\n if (new_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)) == 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}', '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) {\n offset = BN_CTX_POOL_SIZE - 1;\n p->current = p->current->prev;\n } else\n offset--;\n }\n}']
|
2,142
| 0
|
https://github.com/openssl/openssl/blob/47bbaa5b607f592009ed40f5678fde21c10a873c/crypto/lhash/lhash.c/#L249
|
static void doall_util_fn(_LHASH *lh, int use_arg, LHASH_DOALL_FN_TYPE func,
LHASH_DOALL_ARG_FN_TYPE func_arg, void *arg)
{
int i;
LHASH_NODE *a, *n;
if (lh == NULL)
return;
for (i = lh->num_nodes - 1; i >= 0; i--) {
a = lh->b[i];
while (a != NULL) {
n = a->next;
if (use_arg)
func_arg(a->data, arg);
else
func(a->data);
a = n;
}
}
}
|
['int s_client_main(int argc, char **argv)\n{\n BIO *sbio;\n EVP_PKEY *key = NULL;\n SSL *con = NULL;\n SSL_CTX *ctx = NULL;\n STACK_OF(X509) *chain = NULL;\n X509 *cert = NULL;\n X509_VERIFY_PARAM *vpm = NULL;\n SSL_EXCERT *exc = NULL;\n SSL_CONF_CTX *cctx = NULL;\n STACK_OF(OPENSSL_STRING) *ssl_args = NULL;\n STACK_OF(X509_CRL) *crls = NULL;\n const SSL_METHOD *meth = TLS_client_method();\n char *CApath = NULL, *CAfile = NULL, *cbuf = NULL, *sbuf = NULL;\n char *mbuf = NULL, *proxystr = NULL, *connectstr = NULL;\n char *cert_file = NULL, *key_file = NULL, *chain_file = NULL, *prog;\n char *chCApath = NULL, *chCAfile = NULL, *host = SSL_HOST_NAME;\n char *inrand = NULL;\n char *passarg = NULL, *pass = NULL, *vfyCApath = NULL, *vfyCAfile = NULL;\n char *sess_in = NULL, *sess_out = NULL, *crl_file = NULL, *p;\n char *jpake_secret = NULL, *xmpphost = NULL;\n const char *unix_path = NULL;\n const char *ehlo = "mail.example.com";\n struct sockaddr peer;\n struct timeval timeout, *timeoutp;\n fd_set readfds, writefds;\n int build_chain = 0, cbuf_len, cbuf_off, cert_format = FORMAT_PEM;\n int key_format = FORMAT_PEM, crlf = 0, full_log = 1, mbuf_len = 0;\n int prexit = 0;\n int enable_timeouts = 0, sdebug = 0, peerlen = sizeof peer;\n int reconnect = 0, verify = SSL_VERIFY_NONE, vpmtouched = 0;\n int ret = 1, in_init = 1, i, nbio_test = 0, s = -1, k, width, state = 0;\n int sbuf_len, sbuf_off, socket_type = SOCK_STREAM, cmdletters = 1;\n int starttls_proto = PROTO_OFF, crl_format = FORMAT_PEM, crl_download = 0;\n int write_tty, read_tty, write_ssl, read_ssl, tty_on, ssl_pending;\n int fallback_scsv = 0;\n long socket_mtu = 0, randamt = 0;\n unsigned short port = PORT;\n OPTION_CHOICE o;\n#ifndef OPENSSL_NO_ENGINE\n ENGINE *ssl_client_engine = NULL;\n#endif\n ENGINE *e = NULL;\n#if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_NETWARE)\n struct timeval tv;\n#endif\n char *servername = NULL;\n const char *alpn_in = NULL;\n tlsextctx tlsextcbp = { NULL, 0 };\n#define MAX_SI_TYPES 100\n unsigned short serverinfo_types[MAX_SI_TYPES];\n int serverinfo_count = 0, start = 0, len;\n#ifndef OPENSSL_NO_NEXTPROTONEG\n const char *next_proto_neg_in = NULL;\n#endif\n#ifndef OPENSSL_NO_SRP\n char *srppass = NULL;\n int srp_lateuser = 0;\n SRP_ARG srp_arg = { NULL, NULL, 0, 0, 0, 1024 };\n#endif\n prog = opt_progname(argv[0]);\n c_Pause = 0;\n c_quiet = 0;\n c_ign_eof = 0;\n c_debug = 0;\n c_msg = 0;\n c_showcerts = 0;\n c_nbio = 0;\n verify_depth = 0;\n verify_error = X509_V_OK;\n vpm = X509_VERIFY_PARAM_new();\n cbuf = app_malloc(BUFSIZZ, "cbuf");\n sbuf = app_malloc(BUFSIZZ, "sbuf");\n mbuf = app_malloc(BUFSIZZ, "mbuf");\n cctx = SSL_CONF_CTX_new();\n if (vpm == NULL || cctx == NULL) {\n BIO_printf(bio_err, "%s: out of memory\\n", prog);\n goto end;\n }\n SSL_CONF_CTX_set_flags(cctx, SSL_CONF_FLAG_CLIENT | SSL_CONF_FLAG_CMDLINE);\n prog = opt_init(argc, argv, s_client_options);\n while ((o = opt_next()) != OPT_EOF) {\n switch (o) {\n case OPT_EOF:\n case OPT_ERR:\n opthelp:\n BIO_printf(bio_err, "%s: Use -help for summary.\\n", prog);\n goto end;\n case OPT_HELP:\n opt_help(s_client_options);\n ret = 0;\n goto end;\n case OPT_HOST:\n host = opt_arg();\n break;\n case OPT_PORT:\n port = atoi(opt_arg());\n break;\n case OPT_CONNECT:\n connectstr = opt_arg();\n break;\n case OPT_PROXY:\n proxystr = opt_arg();\n starttls_proto = PROTO_CONNECT;\n break;\n case OPT_UNIX:\n unix_path = opt_arg();\n break;\n case OPT_XMPPHOST:\n xmpphost = opt_arg();\n break;\n case OPT_SMTPHOST:\n ehlo = opt_arg();\n break;\n case OPT_VERIFY:\n verify = SSL_VERIFY_PEER;\n verify_depth = atoi(opt_arg());\n if (!c_quiet)\n BIO_printf(bio_err, "verify depth is %d\\n", verify_depth);\n break;\n case OPT_CERT:\n cert_file = opt_arg();\n break;\n case OPT_CRL:\n crl_file = opt_arg();\n break;\n case OPT_CRL_DOWNLOAD:\n crl_download = 1;\n break;\n case OPT_SESS_OUT:\n sess_out = opt_arg();\n break;\n case OPT_SESS_IN:\n sess_in = opt_arg();\n break;\n case OPT_CERTFORM:\n if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &cert_format))\n goto opthelp;\n break;\n case OPT_CRLFORM:\n if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &crl_format))\n goto opthelp;\n break;\n case OPT_VERIFY_RET_ERROR:\n verify_return_error = 1;\n break;\n case OPT_VERIFY_QUIET:\n verify_quiet = 1;\n break;\n case OPT_BRIEF:\n c_brief = verify_quiet = c_quiet = 1;\n break;\n case OPT_S_CASES:\n if (ssl_args == NULL)\n ssl_args = sk_OPENSSL_STRING_new_null();\n if (ssl_args == NULL\n || !sk_OPENSSL_STRING_push(ssl_args, opt_flag())\n || !sk_OPENSSL_STRING_push(ssl_args, opt_arg())) {\n BIO_printf(bio_err, "%s: Memory allocation failure\\n", prog);\n goto end;\n }\n break;\n case OPT_V_CASES:\n if (!opt_verify(o, vpm))\n goto end;\n vpmtouched++;\n break;\n case OPT_X_CASES:\n if (!args_excert(o, &exc))\n goto end;\n break;\n case OPT_PREXIT:\n prexit = 1;\n break;\n case OPT_CRLF:\n crlf = 1;\n break;\n case OPT_QUIET:\n c_quiet = c_ign_eof = 1;\n break;\n case OPT_NBIO:\n c_nbio = 1;\n break;\n case OPT_NOCMDS:\n cmdletters = 0;\n break;\n case OPT_ENGINE:\n e = setup_engine(opt_arg(), 1);\n break;\n case OPT_SSL_CLIENT_ENGINE:\n#ifndef OPENSSL_NO_ENGINE\n ssl_client_engine = ENGINE_by_id(opt_arg());\n if (ssl_client_engine == NULL) {\n BIO_printf(bio_err, "Error getting client auth engine\\n");\n goto opthelp;\n }\n break;\n#endif\n break;\n case OPT_RAND:\n inrand = opt_arg();\n break;\n case OPT_IGN_EOF:\n c_ign_eof = 1;\n break;\n case OPT_NO_IGN_EOF:\n c_ign_eof = 0;\n break;\n case OPT_PAUSE:\n c_Pause = 1;\n break;\n case OPT_DEBUG:\n c_debug = 1;\n break;\n case OPT_TLSEXTDEBUG:\n c_tlsextdebug = 1;\n break;\n case OPT_STATUS:\n c_status_req = 1;\n break;\n case OPT_WDEBUG:\n#ifdef WATT32\n dbug_init();\n#endif\n break;\n case OPT_MSG:\n c_msg = 1;\n break;\n case OPT_MSGFILE:\n bio_c_msg = BIO_new_file(opt_arg(), "w");\n break;\n case OPT_TRACE:\n#ifndef OPENSSL_NO_SSL_TRACE\n c_msg = 2;\n#endif\n break;\n case OPT_SECURITY_DEBUG:\n sdebug = 1;\n break;\n case OPT_SECURITY_DEBUG_VERBOSE:\n sdebug = 2;\n break;\n case OPT_SHOWCERTS:\n c_showcerts = 1;\n break;\n case OPT_NBIO_TEST:\n nbio_test = 1;\n break;\n case OPT_STATE:\n state = 1;\n break;\n#ifndef OPENSSL_NO_PSK\n case OPT_PSK_IDENTITY:\n psk_identity = opt_arg();\n break;\n case OPT_PSK:\n for (p = psk_key = opt_arg(); *p; p++) {\n if (isxdigit(*p))\n continue;\n BIO_printf(bio_err, "Not a hex number \'%s\'\\n", psk_key);\n goto end;\n }\n break;\n#else\n case OPT_PSK_IDENTITY:\n case OPT_PSK:\n break;\n#endif\n#ifndef OPENSSL_NO_SRP\n case OPT_SRPUSER:\n srp_arg.srplogin = opt_arg();\n meth = TLSv1_client_method();\n break;\n case OPT_SRPPASS:\n srppass = opt_arg();\n meth = TLSv1_client_method();\n break;\n case OPT_SRP_STRENGTH:\n srp_arg.strength = atoi(opt_arg());\n BIO_printf(bio_err, "SRP minimal length for N is %d\\n",\n srp_arg.strength);\n meth = TLSv1_client_method();\n break;\n case OPT_SRP_LATEUSER:\n srp_lateuser = 1;\n meth = TLSv1_client_method();\n break;\n case OPT_SRP_MOREGROUPS:\n srp_arg.amp = 1;\n meth = TLSv1_client_method();\n break;\n#else\n case OPT_SRPUSER:\n case OPT_SRPPASS:\n case OPT_SRP_STRENGTH:\n case OPT_SRP_LATEUSER:\n case OPT_SRP_MOREGROUPS:\n break;\n#endif\n case OPT_SSL3:\n#ifndef OPENSSL_NO_SSL3\n meth = SSLv3_client_method();\n#endif\n break;\n case OPT_TLS1_2:\n meth = TLSv1_2_client_method();\n break;\n case OPT_TLS1_1:\n meth = TLSv1_1_client_method();\n break;\n case OPT_TLS1:\n meth = TLSv1_client_method();\n break;\n#ifndef OPENSSL_NO_DTLS1\n case OPT_DTLS:\n meth = DTLS_client_method();\n socket_type = SOCK_DGRAM;\n break;\n case OPT_DTLS1:\n meth = DTLSv1_client_method();\n socket_type = SOCK_DGRAM;\n break;\n case OPT_DTLS1_2:\n meth = DTLSv1_2_client_method();\n socket_type = SOCK_DGRAM;\n break;\n case OPT_TIMEOUT:\n enable_timeouts = 1;\n break;\n case OPT_MTU:\n socket_mtu = atol(opt_arg());\n break;\n#else\n case OPT_DTLS:\n case OPT_DTLS1:\n case OPT_DTLS1_2:\n case OPT_TIMEOUT:\n case OPT_MTU:\n break;\n#endif\n case OPT_FALLBACKSCSV:\n fallback_scsv = 1;\n break;\n case OPT_KEYFORM:\n if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &key_format))\n goto opthelp;\n break;\n case OPT_PASS:\n passarg = opt_arg();\n break;\n case OPT_CERT_CHAIN:\n chain_file = opt_arg();\n break;\n case OPT_KEY:\n key_file = opt_arg();\n break;\n case OPT_RECONNECT:\n reconnect = 5;\n break;\n case OPT_CAPATH:\n CApath = opt_arg();\n break;\n case OPT_CHAINCAPATH:\n chCApath = opt_arg();\n break;\n case OPT_VERIFYCAPATH:\n vfyCApath = opt_arg();\n break;\n case OPT_BUILD_CHAIN:\n build_chain = 1;\n break;\n case OPT_CAFILE:\n CAfile = opt_arg();\n break;\n case OPT_CHAINCAFILE:\n chCAfile = opt_arg();\n break;\n case OPT_VERIFYCAFILE:\n vfyCAfile = opt_arg();\n break;\n case OPT_NEXTPROTONEG:\n next_proto_neg_in = opt_arg();\n break;\n case OPT_ALPN:\n alpn_in = opt_arg();\n break;\n case OPT_SERVERINFO:\n p = opt_arg();\n len = strlen(p);\n for (start = 0, i = 0; i <= len; ++i) {\n if (i == len || p[i] == \',\') {\n serverinfo_types[serverinfo_count] = atoi(p + start);\n if (++serverinfo_count == MAX_SI_TYPES)\n break;\n start = i + 1;\n }\n }\n break;\n case OPT_STARTTLS:\n if (!opt_pair(opt_arg(), services, &starttls_proto))\n goto end;\n case OPT_SERVERNAME:\n servername = opt_arg();\n break;\n case OPT_JPAKE:\n#ifndef OPENSSL_NO_JPAKE\n jpake_secret = opt_arg();\n#endif\n break;\n case OPT_USE_SRTP:\n srtp_profiles = opt_arg();\n break;\n case OPT_KEYMATEXPORT:\n keymatexportlabel = opt_arg();\n break;\n case OPT_KEYMATEXPORTLEN:\n keymatexportlen = atoi(opt_arg());\n break;\n }\n }\n argc = opt_num_rest();\n argv = opt_rest();\n if (!app_load_modules(NULL))\n goto end;\n if (proxystr) {\n if (connectstr == NULL) {\n BIO_printf(bio_err, "%s: -proxy requires use of -connect\\n", prog);\n goto opthelp;\n }\n if (!extract_host_port(proxystr, &host, NULL, &port))\n goto end;\n }\n else if (connectstr != NULL\n && !extract_host_port(connectstr, &host, NULL, &port))\n goto end;\n if (unix_path && (socket_type != SOCK_STREAM)) {\n BIO_printf(bio_err,\n "Can\'t use unix sockets and datagrams together\\n");\n goto end;\n }\n#if !defined(OPENSSL_NO_JPAKE) && !defined(OPENSSL_NO_PSK)\n if (jpake_secret) {\n if (psk_key) {\n BIO_printf(bio_err, "Can\'t use JPAKE and PSK together\\n");\n goto end;\n }\n psk_identity = "JPAKE";\n }\n#endif\n#if !defined(OPENSSL_NO_NEXTPROTONEG)\n next_proto.status = -1;\n if (next_proto_neg_in) {\n next_proto.data =\n next_protos_parse(&next_proto.len, next_proto_neg_in);\n if (next_proto.data == NULL) {\n BIO_printf(bio_err, "Error parsing -nextprotoneg argument\\n");\n goto end;\n }\n } else\n next_proto.data = NULL;\n#endif\n if (!app_passwd(passarg, NULL, &pass, NULL)) {\n BIO_printf(bio_err, "Error getting password\\n");\n goto end;\n }\n if (key_file == NULL)\n key_file = cert_file;\n if (key_file) {\n key = load_key(key_file, key_format, 0, pass, e,\n "client certificate private key file");\n if (key == NULL) {\n ERR_print_errors(bio_err);\n goto end;\n }\n }\n if (cert_file) {\n cert = load_cert(cert_file, cert_format,\n NULL, e, "client certificate file");\n if (cert == NULL) {\n ERR_print_errors(bio_err);\n goto end;\n }\n }\n if (chain_file) {\n chain = load_certs(chain_file, FORMAT_PEM,\n NULL, e, "client certificate chain");\n if (!chain)\n goto end;\n }\n if (crl_file) {\n X509_CRL *crl;\n crl = load_crl(crl_file, crl_format);\n if (crl == NULL) {\n BIO_puts(bio_err, "Error loading CRL\\n");\n ERR_print_errors(bio_err);\n goto end;\n }\n crls = sk_X509_CRL_new_null();\n if (crls == NULL || !sk_X509_CRL_push(crls, crl)) {\n BIO_puts(bio_err, "Error adding CRL\\n");\n ERR_print_errors(bio_err);\n X509_CRL_free(crl);\n goto end;\n }\n }\n if (!load_excert(&exc))\n goto end;\n if (!app_RAND_load_file(NULL, 1) && inrand == NULL\n && !RAND_status()) {\n BIO_printf(bio_err,\n "warning, not much extra random data, consider using the -rand option\\n");\n }\n if (inrand != NULL) {\n randamt = app_RAND_load_files(inrand);\n BIO_printf(bio_err, "%ld semi-random bytes loaded\\n", randamt);\n }\n if (bio_c_out == NULL) {\n if (c_quiet && !c_debug) {\n bio_c_out = BIO_new(BIO_s_null());\n if (c_msg && !bio_c_msg)\n bio_c_msg = dup_bio_out();\n } else if (bio_c_out == NULL)\n bio_c_out = dup_bio_out();\n }\n#ifndef OPENSSL_NO_SRP\n if (!app_passwd(srppass, NULL, &srp_arg.srppassin, NULL)) {\n BIO_printf(bio_err, "Error getting password\\n");\n goto end;\n }\n#endif\n ctx = SSL_CTX_new(meth);\n if (ctx == NULL) {\n ERR_print_errors(bio_err);\n goto end;\n }\n if (sdebug)\n ssl_ctx_security_debug(ctx, sdebug);\n if (vpmtouched && !SSL_CTX_set1_param(ctx, vpm)) {\n BIO_printf(bio_err, "Error setting verify params\\n");\n ERR_print_errors(bio_err);\n goto end;\n }\n if (!config_ctx(cctx, ssl_args, ctx, 1, jpake_secret == NULL))\n goto end;\n if (!ssl_load_stores(ctx, vfyCApath, vfyCAfile, chCApath, chCAfile,\n crls, crl_download)) {\n BIO_printf(bio_err, "Error loading store locations\\n");\n ERR_print_errors(bio_err);\n goto end;\n }\n#ifndef OPENSSL_NO_ENGINE\n if (ssl_client_engine) {\n if (!SSL_CTX_set_client_cert_engine(ctx, ssl_client_engine)) {\n BIO_puts(bio_err, "Error setting client auth engine\\n");\n ERR_print_errors(bio_err);\n ENGINE_free(ssl_client_engine);\n goto end;\n }\n ENGINE_free(ssl_client_engine);\n }\n#endif\n#ifndef OPENSSL_NO_PSK\n if (psk_key != NULL || jpake_secret) {\n if (c_debug)\n BIO_printf(bio_c_out,\n "PSK key given or JPAKE in use, setting client callback\\n");\n SSL_CTX_set_psk_client_callback(ctx, psk_client_cb);\n }\n#endif\n#ifndef OPENSSL_NO_SRTP\n if (srtp_profiles != NULL) {\n if (SSL_CTX_set_tlsext_use_srtp(ctx, srtp_profiles) != 0) {\n BIO_printf(bio_err, "Error setting SRTP profile\\n");\n ERR_print_errors(bio_err);\n goto end;\n }\n }\n#endif\n if (exc)\n ssl_ctx_set_excert(ctx, exc);\n#if !defined(OPENSSL_NO_NEXTPROTONEG)\n if (next_proto.data)\n SSL_CTX_set_next_proto_select_cb(ctx, next_proto_cb, &next_proto);\n#endif\n if (alpn_in) {\n unsigned short alpn_len;\n unsigned char *alpn = next_protos_parse(&alpn_len, alpn_in);\n if (alpn == NULL) {\n BIO_printf(bio_err, "Error parsing -alpn argument\\n");\n goto end;\n }\n if (SSL_CTX_set_alpn_protos(ctx, alpn, alpn_len) != 0) {\n BIO_printf(bio_err, "Error setting ALPN\\n");\n goto end;\n }\n OPENSSL_free(alpn);\n }\n for (i = 0; i < serverinfo_count; i++) {\n if (!SSL_CTX_add_client_custom_ext(ctx,\n serverinfo_types[i],\n NULL, NULL, NULL,\n serverinfo_cli_parse_cb, NULL)) {\n BIO_printf(bio_err,\n "Warning: Unable to add custom extension %u, skipping\\n",\n serverinfo_types[i]);\n }\n }\n if (state)\n SSL_CTX_set_info_callback(ctx, apps_ssl_info_callback);\n SSL_CTX_set_verify(ctx, verify, verify_callback);\n if (!ctx_set_verify_locations(ctx, CAfile, CApath)) {\n ERR_print_errors(bio_err);\n goto end;\n }\n ssl_ctx_add_crls(ctx, crls, crl_download);\n if (!set_cert_key_stuff(ctx, cert, key, chain, build_chain))\n goto end;\n if (servername != NULL) {\n tlsextcbp.biodebug = bio_err;\n SSL_CTX_set_tlsext_servername_callback(ctx, ssl_servername_cb);\n SSL_CTX_set_tlsext_servername_arg(ctx, &tlsextcbp);\n }\n# ifndef OPENSSL_NO_SRP\n if (srp_arg.srplogin) {\n if (!srp_lateuser && !SSL_CTX_set_srp_username(ctx, srp_arg.srplogin)) {\n BIO_printf(bio_err, "Unable to set SRP username\\n");\n goto end;\n }\n srp_arg.msg = c_msg;\n srp_arg.debug = c_debug;\n SSL_CTX_set_srp_cb_arg(ctx, &srp_arg);\n SSL_CTX_set_srp_client_pwd_callback(ctx, ssl_give_srp_client_pwd_cb);\n SSL_CTX_set_srp_strength(ctx, srp_arg.strength);\n if (c_msg || c_debug || srp_arg.amp == 0)\n SSL_CTX_set_srp_verify_param_callback(ctx,\n ssl_srp_verify_param_cb);\n }\n# endif\n con = SSL_new(ctx);\n if (sess_in) {\n SSL_SESSION *sess;\n BIO *stmp = BIO_new_file(sess_in, "r");\n if (!stmp) {\n BIO_printf(bio_err, "Can\'t open session file %s\\n", sess_in);\n ERR_print_errors(bio_err);\n goto end;\n }\n sess = PEM_read_bio_SSL_SESSION(stmp, NULL, 0, NULL);\n BIO_free(stmp);\n if (!sess) {\n BIO_printf(bio_err, "Can\'t open session file %s\\n", sess_in);\n ERR_print_errors(bio_err);\n goto end;\n }\n if (!SSL_set_session(con, sess)) {\n BIO_printf(bio_err, "Can\'t set session\\n");\n ERR_print_errors(bio_err);\n goto end;\n }\n SSL_SESSION_free(sess);\n }\n if (fallback_scsv)\n SSL_set_mode(con, SSL_MODE_SEND_FALLBACK_SCSV);\n if (servername != NULL) {\n if (!SSL_set_tlsext_host_name(con, servername)) {\n BIO_printf(bio_err, "Unable to set TLS servername extension.\\n");\n ERR_print_errors(bio_err);\n goto end;\n }\n }\n re_start:\n#ifdef NO_SYS_UN_H\n if (init_client(&s, host, port, socket_type) == 0)\n#else\n if ((!unix_path && (init_client(&s, host, port, socket_type) == 0)) ||\n (unix_path && (init_client_unix(&s, unix_path) == 0)))\n#endif\n {\n BIO_printf(bio_err, "connect:errno=%d\\n", get_last_socket_error());\n SHUTDOWN(s);\n goto end;\n }\n BIO_printf(bio_c_out, "CONNECTED(%08X)\\n", s);\n#ifdef FIONBIO\n if (c_nbio) {\n unsigned long l = 1;\n BIO_printf(bio_c_out, "turning on non blocking io\\n");\n if (BIO_socket_ioctl(s, FIONBIO, &l) < 0) {\n ERR_print_errors(bio_err);\n goto end;\n }\n }\n#endif\n if (c_Pause & 0x01)\n SSL_set_debug(con, 1);\n if (socket_type == SOCK_DGRAM) {\n sbio = BIO_new_dgram(s, BIO_NOCLOSE);\n if (getsockname(s, &peer, (void *)&peerlen) < 0) {\n BIO_printf(bio_err, "getsockname:errno=%d\\n",\n get_last_socket_error());\n SHUTDOWN(s);\n goto end;\n }\n (void)BIO_ctrl_set_connected(sbio, 1, &peer);\n if (enable_timeouts) {\n timeout.tv_sec = 0;\n timeout.tv_usec = DGRAM_RCV_TIMEOUT;\n BIO_ctrl(sbio, BIO_CTRL_DGRAM_SET_RECV_TIMEOUT, 0, &timeout);\n timeout.tv_sec = 0;\n timeout.tv_usec = DGRAM_SND_TIMEOUT;\n BIO_ctrl(sbio, BIO_CTRL_DGRAM_SET_SEND_TIMEOUT, 0, &timeout);\n }\n if (socket_mtu) {\n if (socket_mtu < DTLS_get_link_min_mtu(con)) {\n BIO_printf(bio_err, "MTU too small. Must be at least %ld\\n",\n DTLS_get_link_min_mtu(con));\n BIO_free(sbio);\n goto shut;\n }\n SSL_set_options(con, SSL_OP_NO_QUERY_MTU);\n if (!DTLS_set_link_mtu(con, socket_mtu)) {\n BIO_printf(bio_err, "Failed to set MTU\\n");\n BIO_free(sbio);\n goto shut;\n }\n } else\n BIO_ctrl(sbio, BIO_CTRL_DGRAM_MTU_DISCOVER, 0, NULL);\n } else\n sbio = BIO_new_socket(s, BIO_NOCLOSE);\n if (nbio_test) {\n BIO *test;\n test = BIO_new(BIO_f_nbio_test());\n sbio = BIO_push(test, sbio);\n }\n if (c_debug) {\n SSL_set_debug(con, 1);\n BIO_set_callback(sbio, bio_dump_callback);\n BIO_set_callback_arg(sbio, (char *)bio_c_out);\n }\n if (c_msg) {\n#ifndef OPENSSL_NO_SSL_TRACE\n if (c_msg == 2)\n SSL_set_msg_callback(con, SSL_trace);\n else\n#endif\n SSL_set_msg_callback(con, msg_cb);\n SSL_set_msg_callback_arg(con, bio_c_msg ? bio_c_msg : bio_c_out);\n }\n if (c_tlsextdebug) {\n SSL_set_tlsext_debug_callback(con, tlsext_cb);\n SSL_set_tlsext_debug_arg(con, bio_c_out);\n }\n if (c_status_req) {\n SSL_set_tlsext_status_type(con, TLSEXT_STATUSTYPE_ocsp);\n SSL_CTX_set_tlsext_status_cb(ctx, ocsp_resp_cb);\n SSL_CTX_set_tlsext_status_arg(ctx, bio_c_out);\n }\n#ifndef OPENSSL_NO_JPAKE\n if (jpake_secret)\n jpake_client_auth(bio_c_out, sbio, jpake_secret);\n#endif\n SSL_set_bio(con, sbio, sbio);\n SSL_set_connect_state(con);\n width = SSL_get_fd(con) + 1;\n read_tty = 1;\n write_tty = 0;\n tty_on = 0;\n read_ssl = 1;\n write_ssl = 1;\n cbuf_len = 0;\n cbuf_off = 0;\n sbuf_len = 0;\n sbuf_off = 0;\n switch ((PROTOCOL_CHOICE) starttls_proto) {\n case PROTO_OFF:\n break;\n case PROTO_SMTP:\n {\n int foundit = 0;\n BIO *fbio = BIO_new(BIO_f_buffer());\n BIO_push(fbio, sbio);\n do {\n mbuf_len = BIO_gets(fbio, mbuf, BUFSIZZ);\n }\n while (mbuf_len > 3 && mbuf[3] == \'-\');\n BIO_printf(fbio, "EHLO %s\\r\\n", ehlo);\n (void)BIO_flush(fbio);\n do {\n mbuf_len = BIO_gets(fbio, mbuf, BUFSIZZ);\n if (strstr(mbuf, "STARTTLS"))\n foundit = 1;\n }\n while (mbuf_len > 3 && mbuf[3] == \'-\');\n (void)BIO_flush(fbio);\n BIO_pop(fbio);\n BIO_free(fbio);\n if (!foundit)\n BIO_printf(bio_err,\n "didn\'t found starttls in server response,"\n " try anyway...\\n");\n BIO_printf(sbio, "STARTTLS\\r\\n");\n BIO_read(sbio, sbuf, BUFSIZZ);\n }\n break;\n case PROTO_POP3:\n {\n BIO_read(sbio, mbuf, BUFSIZZ);\n BIO_printf(sbio, "STLS\\r\\n");\n mbuf_len = BIO_read(sbio, sbuf, BUFSIZZ);\n if (mbuf_len < 0) {\n BIO_printf(bio_err, "BIO_read failed\\n");\n goto end;\n }\n }\n break;\n case PROTO_IMAP:\n {\n int foundit = 0;\n BIO *fbio = BIO_new(BIO_f_buffer());\n BIO_push(fbio, sbio);\n BIO_gets(fbio, mbuf, BUFSIZZ);\n BIO_printf(fbio, ". CAPABILITY\\r\\n");\n (void)BIO_flush(fbio);\n do {\n mbuf_len = BIO_gets(fbio, mbuf, BUFSIZZ);\n if (strstr(mbuf, "STARTTLS"))\n foundit = 1;\n }\n while (mbuf_len > 3 && mbuf[0] != \'.\');\n (void)BIO_flush(fbio);\n BIO_pop(fbio);\n BIO_free(fbio);\n if (!foundit)\n BIO_printf(bio_err,\n "didn\'t found STARTTLS in server response,"\n " try anyway...\\n");\n BIO_printf(sbio, ". STARTTLS\\r\\n");\n BIO_read(sbio, sbuf, BUFSIZZ);\n }\n break;\n case PROTO_FTP:\n {\n BIO *fbio = BIO_new(BIO_f_buffer());\n BIO_push(fbio, sbio);\n do {\n mbuf_len = BIO_gets(fbio, mbuf, BUFSIZZ);\n }\n while (mbuf_len > 3 && mbuf[3] == \'-\');\n (void)BIO_flush(fbio);\n BIO_pop(fbio);\n BIO_free(fbio);\n BIO_printf(sbio, "AUTH TLS\\r\\n");\n BIO_read(sbio, sbuf, BUFSIZZ);\n }\n break;\n case PROTO_XMPP:\n case PROTO_XMPP_SERVER:\n {\n int seen = 0;\n BIO_printf(sbio, "<stream:stream "\n "xmlns:stream=\'http://etherx.jabber.org/streams\' "\n "xmlns=\'jabber:%s\' to=\'%s\' version=\'1.0\'>",\n starttls_proto == PROTO_XMPP ? "client" : "server",\n xmpphost ? xmpphost : host);\n seen = BIO_read(sbio, mbuf, BUFSIZZ);\n mbuf[seen] = 0;\n while (!strstr\n (mbuf, "<starttls xmlns=\'urn:ietf:params:xml:ns:xmpp-tls\'")\n && !strstr(mbuf,\n "<starttls xmlns=\\"urn:ietf:params:xml:ns:xmpp-tls\\""))\n {\n seen = BIO_read(sbio, mbuf, BUFSIZZ);\n if (seen <= 0)\n goto shut;\n mbuf[seen] = 0;\n }\n BIO_printf(sbio,\n "<starttls xmlns=\'urn:ietf:params:xml:ns:xmpp-tls\'/>");\n seen = BIO_read(sbio, sbuf, BUFSIZZ);\n sbuf[seen] = 0;\n if (!strstr(sbuf, "<proceed"))\n goto shut;\n mbuf[0] = 0;\n }\n break;\n case PROTO_TELNET:\n {\n static const unsigned char tls_do[] = {\n 255, 253, 46\n };\n static const unsigned char tls_will[] = {\n 255, 251, 46\n };\n static const unsigned char tls_follows[] = {\n 255, 250, 46, 1, 255, 240\n };\n int bytes;\n bytes = BIO_read(sbio, mbuf, BUFSIZZ);\n if (bytes != 3 || memcmp(mbuf, tls_do, 3) != 0)\n goto shut;\n BIO_write(sbio, tls_will, 3);\n BIO_write(sbio, tls_follows, 6);\n (void)BIO_flush(sbio);\n bytes = BIO_read(sbio, mbuf, BUFSIZZ);\n if (bytes != 6 || memcmp(mbuf, tls_follows, 6) != 0)\n goto shut;\n }\n break;\n case PROTO_CONNECT:\n {\n int foundit = 0;\n BIO *fbio = BIO_new(BIO_f_buffer());\n BIO_push(fbio, sbio);\n BIO_printf(fbio, "CONNECT %s\\r\\n\\r\\n", connectstr);\n (void)BIO_flush(fbio);\n do {\n mbuf_len = BIO_gets(fbio, mbuf, BUFSIZZ);\n if (strstr(mbuf, "200") != NULL\n && strstr(mbuf, "established") != NULL)\n foundit++;\n } while (mbuf_len > 3 && foundit == 0);\n (void)BIO_flush(fbio);\n BIO_pop(fbio);\n BIO_free(fbio);\n if (!foundit) {\n BIO_printf(bio_err, "%s: HTTP CONNECT failed\\n", prog);\n goto shut;\n }\n }\n break;\n }\n for (;;) {\n FD_ZERO(&readfds);\n FD_ZERO(&writefds);\n if ((SSL_version(con) == DTLS1_VERSION) &&\n DTLSv1_get_timeout(con, &timeout))\n timeoutp = &timeout;\n else\n timeoutp = NULL;\n if (SSL_in_init(con) && !SSL_total_renegotiations(con)) {\n in_init = 1;\n tty_on = 0;\n } else {\n tty_on = 1;\n if (in_init) {\n in_init = 0;\n if (servername != NULL && !SSL_session_reused(con)) {\n BIO_printf(bio_c_out,\n "Server did %sacknowledge servername extension.\\n",\n tlsextcbp.ack ? "" : "not ");\n }\n if (sess_out) {\n BIO *stmp = BIO_new_file(sess_out, "w");\n if (stmp) {\n PEM_write_bio_SSL_SESSION(stmp, SSL_get_session(con));\n BIO_free(stmp);\n } else\n BIO_printf(bio_err, "Error writing session file %s\\n",\n sess_out);\n }\n if (c_brief) {\n BIO_puts(bio_err, "CONNECTION ESTABLISHED\\n");\n print_ssl_summary(con);\n }\n print_stuff(bio_c_out, con, full_log);\n if (full_log > 0)\n full_log--;\n if (starttls_proto) {\n BIO_write(bio_err, mbuf, mbuf_len);\n if (!reconnect)\n starttls_proto = PROTO_OFF;\n }\n if (reconnect) {\n reconnect--;\n BIO_printf(bio_c_out,\n "drop connection and then reconnect\\n");\n SSL_shutdown(con);\n SSL_set_connect_state(con);\n SHUTDOWN(SSL_get_fd(con));\n goto re_start;\n }\n }\n }\n ssl_pending = read_ssl && SSL_pending(con);\n if (!ssl_pending) {\n#if !defined(OPENSSL_SYS_WINDOWS) && !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_NETWARE)\n if (tty_on) {\n if (read_tty)\n openssl_fdset(fileno(stdin), &readfds);\n if (write_tty)\n openssl_fdset(fileno(stdout), &writefds);\n }\n if (read_ssl)\n openssl_fdset(SSL_get_fd(con), &readfds);\n if (write_ssl)\n openssl_fdset(SSL_get_fd(con), &writefds);\n#else\n if (!tty_on || !write_tty) {\n if (read_ssl)\n openssl_fdset(SSL_get_fd(con), &readfds);\n if (write_ssl)\n openssl_fdset(SSL_get_fd(con), &writefds);\n }\n#endif\n#if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS)\n i = 0;\n if (!write_tty) {\n if (read_tty) {\n tv.tv_sec = 1;\n tv.tv_usec = 0;\n i = select(width, (void *)&readfds, (void *)&writefds,\n NULL, &tv);\n# if defined(OPENSSL_SYS_WINCE) || defined(OPENSSL_SYS_MSDOS)\n if (!i && (!_kbhit() || !read_tty))\n continue;\n# else\n if (!i && (!((_kbhit())\n || (WAIT_OBJECT_0 ==\n WaitForSingleObject(GetStdHandle\n (STD_INPUT_HANDLE),\n 0)))\n || !read_tty))\n continue;\n# endif\n } else\n i = select(width, (void *)&readfds, (void *)&writefds,\n NULL, timeoutp);\n }\n#elif defined(OPENSSL_SYS_NETWARE)\n if (!write_tty) {\n if (read_tty) {\n tv.tv_sec = 1;\n tv.tv_usec = 0;\n i = select(width, (void *)&readfds, (void *)&writefds,\n NULL, &tv);\n } else\n i = select(width, (void *)&readfds, (void *)&writefds,\n NULL, timeoutp);\n }\n#else\n i = select(width, (void *)&readfds, (void *)&writefds,\n NULL, timeoutp);\n#endif\n if (i < 0) {\n BIO_printf(bio_err, "bad select %d\\n",\n get_last_socket_error());\n goto shut;\n }\n }\n if ((SSL_version(con) == DTLS1_VERSION)\n && DTLSv1_handle_timeout(con) > 0) {\n BIO_printf(bio_err, "TIMEOUT occurred\\n");\n }\n if (!ssl_pending && FD_ISSET(SSL_get_fd(con), &writefds)) {\n k = SSL_write(con, &(cbuf[cbuf_off]), (unsigned int)cbuf_len);\n switch (SSL_get_error(con, k)) {\n case SSL_ERROR_NONE:\n cbuf_off += k;\n cbuf_len -= k;\n if (k <= 0)\n goto end;\n if (cbuf_len <= 0) {\n read_tty = 1;\n write_ssl = 0;\n } else {\n read_tty = 0;\n write_ssl = 1;\n }\n break;\n case SSL_ERROR_WANT_WRITE:\n BIO_printf(bio_c_out, "write W BLOCK\\n");\n write_ssl = 1;\n read_tty = 0;\n break;\n case SSL_ERROR_WANT_READ:\n BIO_printf(bio_c_out, "write R BLOCK\\n");\n write_tty = 0;\n read_ssl = 1;\n write_ssl = 0;\n break;\n case SSL_ERROR_WANT_X509_LOOKUP:\n BIO_printf(bio_c_out, "write X BLOCK\\n");\n break;\n case SSL_ERROR_ZERO_RETURN:\n if (cbuf_len != 0) {\n BIO_printf(bio_c_out, "shutdown\\n");\n ret = 0;\n goto shut;\n } else {\n read_tty = 1;\n write_ssl = 0;\n break;\n }\n case SSL_ERROR_SYSCALL:\n if ((k != 0) || (cbuf_len != 0)) {\n BIO_printf(bio_err, "write:errno=%d\\n",\n get_last_socket_error());\n goto shut;\n } else {\n read_tty = 1;\n write_ssl = 0;\n }\n break;\n case SSL_ERROR_SSL:\n ERR_print_errors(bio_err);\n goto shut;\n }\n }\n#if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_NETWARE)\n else if (!ssl_pending && write_tty)\n#else\n else if (!ssl_pending && FD_ISSET(fileno(stdout), &writefds))\n#endif\n {\n#ifdef CHARSET_EBCDIC\n ascii2ebcdic(&(sbuf[sbuf_off]), &(sbuf[sbuf_off]), sbuf_len);\n#endif\n i = raw_write_stdout(&(sbuf[sbuf_off]), sbuf_len);\n if (i <= 0) {\n BIO_printf(bio_c_out, "DONE\\n");\n ret = 0;\n goto shut;\n }\n sbuf_len -= i;;\n sbuf_off += i;\n if (sbuf_len <= 0) {\n read_ssl = 1;\n write_tty = 0;\n }\n } else if (ssl_pending || FD_ISSET(SSL_get_fd(con), &readfds)) {\n#ifdef RENEG\n {\n static int iiii;\n if (++iiii == 52) {\n SSL_renegotiate(con);\n iiii = 0;\n }\n }\n#endif\n k = SSL_read(con, sbuf, 1024 );\n switch (SSL_get_error(con, k)) {\n case SSL_ERROR_NONE:\n if (k <= 0)\n goto end;\n sbuf_off = 0;\n sbuf_len = k;\n read_ssl = 0;\n write_tty = 1;\n break;\n case SSL_ERROR_WANT_WRITE:\n BIO_printf(bio_c_out, "read W BLOCK\\n");\n write_ssl = 1;\n read_tty = 0;\n break;\n case SSL_ERROR_WANT_READ:\n BIO_printf(bio_c_out, "read R BLOCK\\n");\n write_tty = 0;\n read_ssl = 1;\n if ((read_tty == 0) && (write_ssl == 0))\n write_ssl = 1;\n break;\n case SSL_ERROR_WANT_X509_LOOKUP:\n BIO_printf(bio_c_out, "read X BLOCK\\n");\n break;\n case SSL_ERROR_SYSCALL:\n ret = get_last_socket_error();\n if (c_brief)\n BIO_puts(bio_err, "CONNECTION CLOSED BY SERVER\\n");\n else\n BIO_printf(bio_err, "read:errno=%d\\n", ret);\n goto shut;\n case SSL_ERROR_ZERO_RETURN:\n BIO_printf(bio_c_out, "closed\\n");\n ret = 0;\n goto shut;\n case SSL_ERROR_SSL:\n ERR_print_errors(bio_err);\n goto shut;\n }\n }\n#if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS)\n# if defined(OPENSSL_SYS_WINCE) || defined(OPENSSL_SYS_MSDOS)\n else if (_kbhit())\n# else\n else if ((_kbhit())\n || (WAIT_OBJECT_0 ==\n WaitForSingleObject(GetStdHandle(STD_INPUT_HANDLE), 0)))\n# endif\n#elif defined (OPENSSL_SYS_NETWARE)\n else if (_kbhit())\n#else\n else if (FD_ISSET(fileno(stdin), &readfds))\n#endif\n {\n if (crlf) {\n int j, lf_num;\n i = raw_read_stdin(cbuf, BUFSIZZ / 2);\n lf_num = 0;\n for (j = 0; j < i; j++)\n if (cbuf[j] == \'\\n\')\n lf_num++;\n for (j = i - 1; j >= 0; j--) {\n cbuf[j + lf_num] = cbuf[j];\n if (cbuf[j] == \'\\n\') {\n lf_num--;\n i++;\n cbuf[j + lf_num] = \'\\r\';\n }\n }\n assert(lf_num == 0);\n } else\n i = raw_read_stdin(cbuf, BUFSIZZ);\n if ((!c_ign_eof) && ((i <= 0) || (cbuf[0] == \'Q\' && cmdletters))) {\n BIO_printf(bio_err, "DONE\\n");\n ret = 0;\n goto shut;\n }\n if ((!c_ign_eof) && (cbuf[0] == \'R\' && cmdletters)) {\n BIO_printf(bio_err, "RENEGOTIATING\\n");\n SSL_renegotiate(con);\n cbuf_len = 0;\n }\n#ifndef OPENSSL_NO_HEARTBEATS\n else if ((!c_ign_eof) && (cbuf[0] == \'B\' && cmdletters)) {\n BIO_printf(bio_err, "HEARTBEATING\\n");\n SSL_heartbeat(con);\n cbuf_len = 0;\n }\n#endif\n else {\n cbuf_len = i;\n cbuf_off = 0;\n#ifdef CHARSET_EBCDIC\n ebcdic2ascii(cbuf, cbuf, i);\n#endif\n }\n write_ssl = 1;\n read_tty = 0;\n }\n }\n ret = 0;\n shut:\n if (in_init)\n print_stuff(bio_c_out, con, full_log);\n SSL_shutdown(con);\n SHUTDOWN(SSL_get_fd(con));\n end:\n if (con != NULL) {\n if (prexit != 0)\n print_stuff(bio_c_out, con, 1);\n SSL_free(con);\n }\n#if !defined(OPENSSL_NO_NEXTPROTONEG)\n OPENSSL_free(next_proto.data);\n#endif\n SSL_CTX_free(ctx);\n X509_free(cert);\n sk_X509_CRL_pop_free(crls, X509_CRL_free);\n EVP_PKEY_free(key);\n sk_X509_pop_free(chain, X509_free);\n OPENSSL_free(pass);\n X509_VERIFY_PARAM_free(vpm);\n ssl_excert_free(exc);\n sk_OPENSSL_STRING_free(ssl_args);\n SSL_CONF_CTX_free(cctx);\n OPENSSL_clear_free(cbuf, BUFSIZZ);\n OPENSSL_clear_free(sbuf, BUFSIZZ);\n OPENSSL_clear_free(mbuf, BUFSIZZ);\n BIO_free(bio_c_out);\n bio_c_out = NULL;\n BIO_free(bio_c_msg);\n bio_c_msg = NULL;\n return (ret);\n}', 'SSL_CTX *SSL_CTX_new(const SSL_METHOD *meth)\n{\n SSL_CTX *ret = NULL;\n if (meth == NULL) {\n SSLerr(SSL_F_SSL_CTX_NEW, SSL_R_NULL_SSL_METHOD_PASSED);\n return (NULL);\n }\n if (FIPS_mode() && (meth->version < TLS1_VERSION)) {\n SSLerr(SSL_F_SSL_CTX_NEW, SSL_R_ONLY_TLS_ALLOWED_IN_FIPS_MODE);\n return NULL;\n }\n if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0) {\n SSLerr(SSL_F_SSL_CTX_NEW, SSL_R_X509_VERIFICATION_SETUP_PROBLEMS);\n goto err;\n }\n ret = OPENSSL_zalloc(sizeof(*ret));\n if (ret == NULL)\n goto err;\n ret->method = meth;\n ret->session_cache_mode = SSL_SESS_CACHE_SERVER;\n ret->session_cache_size = SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;\n ret->session_timeout = meth->get_timeout();\n ret->references = 1;\n ret->max_cert_list = SSL_MAX_CERT_LIST_DEFAULT;\n ret->verify_mode = SSL_VERIFY_NONE;\n if ((ret->cert = ssl_cert_new()) == NULL)\n goto err;\n ret->sessions = lh_SSL_SESSION_new();\n if (ret->sessions == NULL)\n goto err;\n ret->cert_store = X509_STORE_new();\n if (ret->cert_store == NULL)\n goto err;\n if (!ssl_create_cipher_list(ret->method,\n &ret->cipher_list, &ret->cipher_list_by_id,\n SSL_DEFAULT_CIPHER_LIST, ret->cert)\n || sk_SSL_CIPHER_num(ret->cipher_list) <= 0) {\n SSLerr(SSL_F_SSL_CTX_NEW, SSL_R_LIBRARY_HAS_NO_CIPHERS);\n goto err2;\n }\n ret->param = X509_VERIFY_PARAM_new();\n if (!ret->param)\n goto err;\n if ((ret->md5 = EVP_get_digestbyname("ssl3-md5")) == NULL) {\n SSLerr(SSL_F_SSL_CTX_NEW, SSL_R_UNABLE_TO_LOAD_SSL3_MD5_ROUTINES);\n goto err2;\n }\n if ((ret->sha1 = EVP_get_digestbyname("ssl3-sha1")) == NULL) {\n SSLerr(SSL_F_SSL_CTX_NEW, SSL_R_UNABLE_TO_LOAD_SSL3_SHA1_ROUTINES);\n goto err2;\n }\n if ((ret->client_CA = sk_X509_NAME_new_null()) == NULL)\n goto err;\n CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_CTX, ret, &ret->ex_data);\n if (!(meth->ssl3_enc->enc_flags & SSL_ENC_FLAG_DTLS))\n ret->comp_methods = SSL_COMP_get_compression_methods();\n ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;\n if ((RAND_bytes(ret->tlsext_tick_key_name, 16) <= 0)\n || (RAND_bytes(ret->tlsext_tick_hmac_key, 16) <= 0)\n || (RAND_bytes(ret->tlsext_tick_aes_key, 16) <= 0))\n ret->options |= SSL_OP_NO_TICKET;\n#ifndef OPENSSL_NO_SRP\n if (!SSL_CTX_SRP_CTX_init(ret))\n goto err;\n#endif\n#ifndef OPENSSL_NO_ENGINE\n# ifdef OPENSSL_SSL_CLIENT_ENGINE_AUTO\n# define eng_strx(x) #x\n# define eng_str(x) eng_strx(x)\n {\n ENGINE *eng;\n eng = ENGINE_by_id(eng_str(OPENSSL_SSL_CLIENT_ENGINE_AUTO));\n if (!eng) {\n ERR_clear_error();\n ENGINE_load_builtin_engines();\n eng = ENGINE_by_id(eng_str(OPENSSL_SSL_CLIENT_ENGINE_AUTO));\n }\n if (!eng || !SSL_CTX_set_client_cert_engine(ret, eng))\n ERR_clear_error();\n }\n# endif\n#endif\n ret->options |= SSL_OP_LEGACY_SERVER_CONNECT;\n return (ret);\n err:\n SSLerr(SSL_F_SSL_CTX_NEW, ERR_R_MALLOC_FAILURE);\n err2:\n SSL_CTX_free(ret);\n return (NULL);\n}', '_LHASH *lh_new(LHASH_HASH_FN_TYPE h, LHASH_COMP_FN_TYPE c)\n{\n _LHASH *ret;\n if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL)\n goto err0;\n if ((ret->b = OPENSSL_zalloc(sizeof(*ret->b) * MIN_NODES)) == NULL)\n goto err1;\n ret->comp = ((c == NULL) ? (LHASH_COMP_FN_TYPE)strcmp : c);\n ret->hash = ((h == NULL) ? (LHASH_HASH_FN_TYPE)lh_strhash : h);\n ret->num_nodes = MIN_NODES / 2;\n ret->num_alloc_nodes = MIN_NODES;\n ret->pmax = MIN_NODES / 2;\n ret->up_load = UP_LOAD;\n ret->down_load = DOWN_LOAD;\n return (ret);\n err1:\n OPENSSL_free(ret);\n err0:\n return (NULL);\n}', 'void SSL_free(SSL *s)\n{\n int i;\n if (s == NULL)\n return;\n i = CRYPTO_add(&s->references, -1, CRYPTO_LOCK_SSL);\n#ifdef REF_PRINT\n REF_PRINT("SSL", s);\n#endif\n if (i > 0)\n return;\n#ifdef REF_CHECK\n if (i < 0) {\n fprintf(stderr, "SSL_free, bad reference count\\n");\n abort();\n }\n#endif\n X509_VERIFY_PARAM_free(s->param);\n CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data);\n if (s->bbio != NULL) {\n if (s->bbio == s->wbio) {\n s->wbio = BIO_pop(s->wbio);\n }\n BIO_free(s->bbio);\n s->bbio = NULL;\n }\n BIO_free_all(s->rbio);\n if (s->wbio != s->rbio)\n BIO_free_all(s->wbio);\n BUF_MEM_free(s->init_buf);\n sk_SSL_CIPHER_free(s->cipher_list);\n sk_SSL_CIPHER_free(s->cipher_list_by_id);\n if (s->session != NULL) {\n ssl_clear_bad_session(s);\n SSL_SESSION_free(s->session);\n }\n clear_ciphers(s);\n ssl_cert_free(s->cert);\n OPENSSL_free(s->tlsext_hostname);\n SSL_CTX_free(s->initial_ctx);\n#ifndef OPENSSL_NO_EC\n OPENSSL_free(s->tlsext_ecpointformatlist);\n OPENSSL_free(s->tlsext_ellipticcurvelist);\n#endif\n sk_X509_EXTENSION_pop_free(s->tlsext_ocsp_exts, X509_EXTENSION_free);\n sk_OCSP_RESPID_pop_free(s->tlsext_ocsp_ids, OCSP_RESPID_free);\n OPENSSL_free(s->tlsext_ocsp_resp);\n OPENSSL_free(s->alpn_client_proto_list);\n sk_X509_NAME_pop_free(s->client_CA, X509_NAME_free);\n if (s->method != NULL)\n s->method->ssl_free(s);\n RECORD_LAYER_release(&s->rlayer);\n SSL_CTX_free(s->ctx);\n#if !defined(OPENSSL_NO_NEXTPROTONEG)\n OPENSSL_free(s->next_proto_negotiated);\n#endif\n#ifndef OPENSSL_NO_SRTP\n sk_SRTP_PROTECTION_PROFILE_free(s->srtp_profiles);\n#endif\n OPENSSL_free(s);\n}', 'void SSL_CTX_free(SSL_CTX *a)\n{\n int i;\n if (a == NULL)\n return;\n i = CRYPTO_add(&a->references, -1, CRYPTO_LOCK_SSL_CTX);\n#ifdef REF_PRINT\n REF_PRINT("SSL_CTX", a);\n#endif\n if (i > 0)\n return;\n#ifdef REF_CHECK\n if (i < 0) {\n fprintf(stderr, "SSL_CTX_free, bad reference count\\n");\n abort();\n }\n#endif\n X509_VERIFY_PARAM_free(a->param);\n if (a->sessions != NULL)\n SSL_CTX_flush_sessions(a, 0);\n CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_CTX, a, &a->ex_data);\n lh_SSL_SESSION_free(a->sessions);\n X509_STORE_free(a->cert_store);\n sk_SSL_CIPHER_free(a->cipher_list);\n sk_SSL_CIPHER_free(a->cipher_list_by_id);\n ssl_cert_free(a->cert);\n sk_X509_NAME_pop_free(a->client_CA, X509_NAME_free);\n sk_X509_pop_free(a->extra_certs, X509_free);\n a->comp_methods = NULL;\n#ifndef OPENSSL_NO_SRTP\n sk_SRTP_PROTECTION_PROFILE_free(a->srtp_profiles);\n#endif\n#ifndef OPENSSL_NO_PSK\n OPENSSL_free(a->psk_identity_hint);\n#endif\n#ifndef OPENSSL_NO_SRP\n SSL_CTX_SRP_CTX_free(a);\n#endif\n#ifndef OPENSSL_NO_ENGINE\n if (a->client_cert_engine)\n ENGINE_finish(a->client_cert_engine);\n#endif\n#ifndef OPENSSL_NO_EC\n OPENSSL_free(a->tlsext_ecpointformatlist);\n OPENSSL_free(a->tlsext_ellipticcurvelist);\n#endif\n OPENSSL_free(a->alpn_client_proto_list);\n OPENSSL_free(a);\n}', 'void SSL_CTX_flush_sessions(SSL_CTX *s, long t)\n{\n unsigned long i;\n TIMEOUT_PARAM tp;\n tp.ctx = s;\n tp.cache = s->sessions;\n if (tp.cache == NULL)\n return;\n tp.time = t;\n CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);\n i = CHECKED_LHASH_OF(SSL_SESSION, tp.cache)->down_load;\n CHECKED_LHASH_OF(SSL_SESSION, tp.cache)->down_load = 0;\n lh_SSL_SESSION_doall_arg(tp.cache, LHASH_DOALL_ARG_FN(timeout),\n TIMEOUT_PARAM, &tp);\n CHECKED_LHASH_OF(SSL_SESSION, tp.cache)->down_load = i;\n CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);\n}', 'void lh_doall_arg(_LHASH *lh, LHASH_DOALL_ARG_FN_TYPE func, void *arg)\n{\n doall_util_fn(lh, 1, (LHASH_DOALL_FN_TYPE)0, func, arg);\n}', 'static void doall_util_fn(_LHASH *lh, int use_arg, LHASH_DOALL_FN_TYPE func,\n LHASH_DOALL_ARG_FN_TYPE func_arg, void *arg)\n{\n int i;\n LHASH_NODE *a, *n;\n if (lh == NULL)\n return;\n for (i = lh->num_nodes - 1; i >= 0; i--) {\n a = lh->b[i];\n while (a != NULL) {\n n = a->next;\n if (use_arg)\n func_arg(a->data, arg);\n else\n func(a->data);\n a = n;\n }\n }\n}']
|
2,143
| 0
|
https://github.com/openssl/openssl/blob/8da94770f0a049497b1a52ee469cca1f4a13b1a7/crypto/objects/o_names.c/#L94
|
int OBJ_NAME_new_index(unsigned long (*hash_func) (const char *),
int (*cmp_func) (const char *, const char *),
void (*free_func) (const char *, int, const char *))
{
int ret;
int i;
NAME_FUNCS *name_funcs;
if (name_funcs_stack == NULL) {
CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_DISABLE);
name_funcs_stack = sk_NAME_FUNCS_new_null();
CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE);
}
if (name_funcs_stack == NULL) {
return (0);
}
ret = names_type_num;
names_type_num++;
for (i = sk_NAME_FUNCS_num(name_funcs_stack); i < names_type_num; i++) {
CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_DISABLE);
name_funcs = OPENSSL_zalloc(sizeof(*name_funcs));
CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE);
if (name_funcs == NULL) {
OBJerr(OBJ_F_OBJ_NAME_NEW_INDEX, ERR_R_MALLOC_FAILURE);
return (0);
}
name_funcs->hash_func = lh_strhash;
name_funcs->cmp_func = OPENSSL_strcmp;
CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_DISABLE);
sk_NAME_FUNCS_push(name_funcs_stack, name_funcs);
CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE);
}
name_funcs = sk_NAME_FUNCS_value(name_funcs_stack, ret);
if (hash_func != NULL)
name_funcs->hash_func = hash_func;
if (cmp_func != NULL)
name_funcs->cmp_func = cmp_func;
if (free_func != NULL)
name_funcs->free_func = free_func;
return (ret);
}
|
['int OBJ_NAME_new_index(unsigned long (*hash_func) (const char *),\n int (*cmp_func) (const char *, const char *),\n void (*free_func) (const char *, int, const char *))\n{\n int ret;\n int i;\n NAME_FUNCS *name_funcs;\n if (name_funcs_stack == NULL) {\n CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_DISABLE);\n name_funcs_stack = sk_NAME_FUNCS_new_null();\n CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE);\n }\n if (name_funcs_stack == NULL) {\n return (0);\n }\n ret = names_type_num;\n names_type_num++;\n for (i = sk_NAME_FUNCS_num(name_funcs_stack); i < names_type_num; i++) {\n CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_DISABLE);\n name_funcs = OPENSSL_zalloc(sizeof(*name_funcs));\n CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE);\n if (name_funcs == NULL) {\n OBJerr(OBJ_F_OBJ_NAME_NEW_INDEX, ERR_R_MALLOC_FAILURE);\n return (0);\n }\n name_funcs->hash_func = lh_strhash;\n name_funcs->cmp_func = OPENSSL_strcmp;\n CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_DISABLE);\n sk_NAME_FUNCS_push(name_funcs_stack, name_funcs);\n CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE);\n }\n name_funcs = sk_NAME_FUNCS_value(name_funcs_stack, ret);\n if (hash_func != NULL)\n name_funcs->hash_func = hash_func;\n if (cmp_func != NULL)\n name_funcs->cmp_func = cmp_func;\n if (free_func != NULL)\n name_funcs->free_func = free_func;\n return (ret);\n}', 'int CRYPTO_mem_ctrl(int mode)\n{\n#ifndef CRYPTO_MDEBUG\n return mode - mode;\n#else\n int ret = mh_mode;\n CRYPTO_w_lock(CRYPTO_LOCK_MALLOC);\n switch (mode) {\n default:\n break;\n case CRYPTO_MEM_CHECK_ON:\n mh_mode = CRYPTO_MEM_CHECK_ON | CRYPTO_MEM_CHECK_ENABLE;\n num_disable = 0;\n break;\n case CRYPTO_MEM_CHECK_OFF:\n mh_mode = 0;\n num_disable = 0;\n break;\n case CRYPTO_MEM_CHECK_DISABLE:\n if (mh_mode & CRYPTO_MEM_CHECK_ON) {\n CRYPTO_THREADID cur;\n CRYPTO_THREADID_current(&cur);\n if (!num_disable\n || CRYPTO_THREADID_cmp(&disabling_threadid, &cur)) {\n CRYPTO_w_unlock(CRYPTO_LOCK_MALLOC);\n CRYPTO_w_lock(CRYPTO_LOCK_MALLOC2);\n CRYPTO_w_lock(CRYPTO_LOCK_MALLOC);\n mh_mode &= ~CRYPTO_MEM_CHECK_ENABLE;\n CRYPTO_THREADID_cpy(&disabling_threadid, &cur);\n }\n num_disable++;\n }\n break;\n case CRYPTO_MEM_CHECK_ENABLE:\n if (mh_mode & CRYPTO_MEM_CHECK_ON) {\n if (num_disable) {\n num_disable--;\n if (num_disable == 0) {\n mh_mode |= CRYPTO_MEM_CHECK_ENABLE;\n CRYPTO_w_unlock(CRYPTO_LOCK_MALLOC2);\n }\n }\n }\n break;\n }\n CRYPTO_w_unlock(CRYPTO_LOCK_MALLOC);\n return (ret);\n#endif\n}', '_STACK *sk_new_null(void)\n{\n return sk_new((int (*)(const void *, const void *))0);\n}', '_STACK *sk_new(int (*c) (const void *, const void *))\n{\n _STACK *ret;\n if ((ret = OPENSSL_zalloc(sizeof(_STACK))) == NULL)\n goto err;\n if ((ret->data = OPENSSL_zalloc(sizeof(*ret->data) * MIN_NODES)) == NULL)\n goto err;\n ret->comp = c;\n ret->num_alloc = MIN_NODES;\n return (ret);\n err:\n OPENSSL_free(ret);\n return (NULL);\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 sk_num(const _STACK *st)\n{\n if (st == NULL)\n return -1;\n return st->num;\n}', 'int sk_push(_STACK *st, void *data)\n{\n return (sk_insert(st, data, st->num));\n}', 'int sk_insert(_STACK *st, void *data, int loc)\n{\n char **s;\n if (st == NULL)\n return 0;\n if (st->num_alloc <= st->num + 1) {\n s = OPENSSL_realloc((char *)st->data,\n (unsigned int)sizeof(char *) * st->num_alloc * 2);\n if (s == NULL)\n return (0);\n st->data = s;\n st->num_alloc *= 2;\n }\n if ((loc >= (int)st->num) || (loc < 0))\n st->data[st->num] = data;\n else {\n memmove(&(st->data[loc + 1]),\n &(st->data[loc]), sizeof(char *) * (st->num - loc));\n st->data[loc] = data;\n }\n st->num++;\n st->sorted = 0;\n return (st->num);\n}']
|
2,144
| 0
|
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 run_srp_kat(void)\n{\n int ret = 0;\n BIGNUM *s = NULL;\n BIGNUM *v = NULL;\n BIGNUM *a = NULL;\n BIGNUM *b = NULL;\n BIGNUM *u = NULL;\n BIGNUM *x = NULL;\n BIGNUM *Apub = NULL;\n BIGNUM *Bpub = NULL;\n BIGNUM *Kclient = NULL;\n BIGNUM *Kserver = NULL;\n const SRP_gN *GN;\n if (!TEST_ptr(GN = SRP_get_default_gN("1024")))\n goto err;\n BN_hex2bn(&s, "BEB25379D1A8581EB5A727673A2441EE");\n if (!TEST_true(SRP_create_verifier_BN("alice", "password123", &s, &v, GN->N,\n GN->g)))\n goto err;\n TEST_info("checking v");\n if (!TEST_true(check_bn("v", v,\n "7E273DE8696FFC4F4E337D05B4B375BEB0DDE1569E8FA00A9886D812"\n "9BADA1F1822223CA1A605B530E379BA4729FDC59F105B4787E5186F5"\n "C671085A1447B52A48CF1970B4FB6F8400BBF4CEBFBB168152E08AB5"\n "EA53D15C1AFF87B2B9DA6E04E058AD51CC72BFC9033B564E26480D78"\n "E955A5E29E7AB245DB2BE315E2099AFB")))\n goto err;\n TEST_note(" okay");\n BN_hex2bn(&b, "E487CB59D31AC550471E81F00F6928E01DDA08E974A004F49E61F5D1"\n "05284D20");\n Bpub = SRP_Calc_B(b, GN->N, GN->g, v);\n if (!TEST_true(SRP_Verify_B_mod_N(Bpub, GN->N)))\n goto err;\n TEST_info("checking B");\n if (!TEST_true(check_bn("B", Bpub,\n "BD0C61512C692C0CB6D041FA01BB152D4916A1E77AF46AE105393011"\n "BAF38964DC46A0670DD125B95A981652236F99D9B681CBF87837EC99"\n "6C6DA04453728610D0C6DDB58B318885D7D82C7F8DEB75CE7BD4FBAA"\n "37089E6F9C6059F388838E7A00030B331EB76840910440B1B27AAEAE"\n "EB4012B7D7665238A8E3FB004B117B58")))\n goto err;\n TEST_note(" okay");\n BN_hex2bn(&a, "60975527035CF2AD1989806F0407210BC81EDC04E2762A56AFD529DD"\n "DA2D4393");\n Apub = SRP_Calc_A(a, GN->N, GN->g);\n if (!TEST_true(SRP_Verify_A_mod_N(Apub, GN->N)))\n goto err;\n TEST_info("checking A");\n if (!TEST_true(check_bn("A", Apub,\n "61D5E490F6F1B79547B0704C436F523DD0E560F0C64115BB72557EC4"\n "4352E8903211C04692272D8B2D1A5358A2CF1B6E0BFCF99F921530EC"\n "8E39356179EAE45E42BA92AEACED825171E1E8B9AF6D9C03E1327F44"\n "BE087EF06530E69F66615261EEF54073CA11CF5858F0EDFDFE15EFEA"\n "B349EF5D76988A3672FAC47B0769447B")))\n goto err;\n TEST_note(" okay");\n u = SRP_Calc_u(Apub, Bpub, GN->N);\n if (!TEST_true(check_bn("u", u,\n "CE38B9593487DA98554ED47D70A7AE5F462EF019")))\n goto err;\n x = SRP_Calc_x(s, "alice", "password123");\n Kclient = SRP_Calc_client_key(GN->N, Bpub, GN->g, x, a, u);\n TEST_info("checking client\'s key");\n if (!TEST_true(check_bn("Client\'s key", Kclient,\n "B0DC82BABCF30674AE450C0287745E7990A3381F63B387AAF271A10D"\n "233861E359B48220F7C4693C9AE12B0A6F67809F0876E2D013800D6C"\n "41BB59B6D5979B5C00A172B4A2A5903A0BDCAF8A709585EB2AFAFA8F"\n "3499B200210DCC1F10EB33943CD67FC88A2F39A4BE5BEC4EC0A3212D"\n "C346D7E474B29EDE8A469FFECA686E5A")))\n goto err;\n TEST_note(" okay");\n Kserver = SRP_Calc_server_key(Apub, v, u, b, GN->N);\n TEST_info("checking server\'s key");\n if (!TEST_true(check_bn("Server\'s key", Kserver,\n "B0DC82BABCF30674AE450C0287745E7990A3381F63B387AAF271A10D"\n "233861E359B48220F7C4693C9AE12B0A6F67809F0876E2D013800D6C"\n "41BB59B6D5979B5C00A172B4A2A5903A0BDCAF8A709585EB2AFAFA8F"\n "3499B200210DCC1F10EB33943CD67FC88A2F39A4BE5BEC4EC0A3212D"\n "C346D7E474B29EDE8A469FFECA686E5A")))\n goto err;\n TEST_note(" okay");\n ret = 1;\nerr:\n BN_clear_free(Kclient);\n BN_clear_free(Kserver);\n BN_clear_free(x);\n BN_free(u);\n BN_free(Apub);\n BN_clear_free(a);\n BN_free(Bpub);\n BN_clear_free(b);\n BN_free(s);\n BN_clear_free(v);\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_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}']
|
2,145
| 0
|
https://github.com/libav/libav/blob/47399ccdfd93d337c96c76fbf591f0e3637131ef/libavcodec/bitstream.h/#L245
|
static inline void bitstream_skip(BitstreamContext *bc, unsigned n)
{
if (n < bc->bits_left)
skip_remaining(bc, n);
else {
n -= bc->bits_left;
bc->bits = 0;
bc->bits_left = 0;
if (n >= 64) {
unsigned skip = n / 8;
n -= skip * 8;
bc->ptr += skip;
}
refill_64(bc);
if (n)
skip_remaining(bc, n);
}
}
|
['static inline int extend_code(BitstreamContext *bc, int val, int range, int bits)\n{\n if (val == 0) {\n val = -range - get_ue_golomb(bc);\n } else if (val == range * 2) {\n val = range + get_ue_golomb(bc);\n } else {\n val -= range;\n }\n if (bits)\n val = (val << bits) | bitstream_read(bc, bits);\n return val;\n}', 'static inline int get_ue_golomb(BitstreamContext *bc)\n{\n unsigned int buf;\n buf = bitstream_peek(bc, 32);\n if (buf >= (1 << 27)) {\n buf >>= 32 - 9;\n bitstream_skip(bc, ff_golomb_vlc_len[buf]);\n return ff_ue_golomb_vlc_code[buf];\n } else {\n int log = 2 * av_log2(buf) - 31;\n buf >>= log;\n buf--;\n bitstream_skip(bc, 32 - log);\n return buf;\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}']
|
2,146
| 0
|
https://github.com/openssl/openssl/blob/e02c519cd32a55e6ad39a0cfbeeda775f9115f28/crypto/bn/bn_mul.c/#L46
|
BN_ULONG bn_sub_part_words(BN_ULONG *r,
const BN_ULONG *a, const BN_ULONG *b,
int cl, int dl)
{
BN_ULONG c, t;
assert(cl >= 0);
c = bn_sub_words(r, a, b, cl);
if (dl == 0)
return c;
r += cl;
a += cl;
b += cl;
if (dl < 0) {
for (;;) {
t = b[0];
r[0] = (0 - t - c) & BN_MASK2;
if (t != 0)
c = 1;
if (++dl >= 0)
break;
t = b[1];
r[1] = (0 - t - c) & BN_MASK2;
if (t != 0)
c = 1;
if (++dl >= 0)
break;
t = b[2];
r[2] = (0 - t - c) & BN_MASK2;
if (t != 0)
c = 1;
if (++dl >= 0)
break;
t = b[3];
r[3] = (0 - t - c) & BN_MASK2;
if (t != 0)
c = 1;
if (++dl >= 0)
break;
b += 4;
r += 4;
}
} else {
int save_dl = dl;
while (c) {
t = a[0];
r[0] = (t - c) & BN_MASK2;
if (t != 0)
c = 0;
if (--dl <= 0)
break;
t = a[1];
r[1] = (t - c) & BN_MASK2;
if (t != 0)
c = 0;
if (--dl <= 0)
break;
t = a[2];
r[2] = (t - c) & BN_MASK2;
if (t != 0)
c = 0;
if (--dl <= 0)
break;
t = a[3];
r[3] = (t - c) & BN_MASK2;
if (t != 0)
c = 0;
if (--dl <= 0)
break;
save_dl = dl;
a += 4;
r += 4;
}
if (dl > 0) {
if (save_dl > dl) {
switch (save_dl - dl) {
case 1:
r[1] = a[1];
if (--dl <= 0)
break;
case 2:
r[2] = a[2];
if (--dl <= 0)
break;
case 3:
r[3] = a[3];
if (--dl <= 0)
break;
}
a += 4;
r += 4;
}
}
if (dl > 0) {
for (;;) {
r[0] = a[0];
if (--dl <= 0)
break;
r[1] = a[1];
if (--dl <= 0)
break;
r[2] = a[2];
if (--dl <= 0)
break;
r[3] = a[3];
if (--dl <= 0)
break;
a += 4;
r += 4;
}
}
}
return c;
}
|
['static int test_badmod(void)\n{\n BIGNUM *a = NULL, *b = NULL, *zero = NULL;\n BN_MONT_CTX *mont = NULL;\n int st = 0;\n if (!TEST_ptr(a = BN_new())\n || !TEST_ptr(b = BN_new())\n || !TEST_ptr(zero = BN_new())\n || !TEST_ptr(mont = BN_MONT_CTX_new()))\n goto err;\n BN_zero(zero);\n if (!TEST_false(BN_div(a, b, BN_value_one(), zero, ctx)))\n goto err;\n ERR_clear_error();\n if (!TEST_false(BN_mod_mul(a, BN_value_one(), BN_value_one(), zero, ctx)))\n goto err;\n ERR_clear_error();\n if (!TEST_false(BN_mod_exp(a, BN_value_one(), BN_value_one(), zero, ctx)))\n goto err;\n ERR_clear_error();\n if (!TEST_false(BN_mod_exp_mont(a, BN_value_one(), BN_value_one(),\n zero, ctx, NULL)))\n goto err;\n ERR_clear_error();\n if (!TEST_false(BN_mod_exp_mont_consttime(a, BN_value_one(), BN_value_one(),\n zero, ctx, NULL)))\n goto err;\n ERR_clear_error();\n if (!TEST_false(BN_MONT_CTX_set(mont, zero, ctx)))\n goto err;\n ERR_clear_error();\n if (!TEST_true(BN_set_word(b, 16)))\n goto err;\n if (!TEST_false(BN_MONT_CTX_set(mont, b, ctx)))\n goto err;\n ERR_clear_error();\n if (!TEST_false(BN_mod_exp_mont(a, BN_value_one(), BN_value_one(),\n b, ctx, NULL)))\n goto err;\n ERR_clear_error();\n if (!TEST_false(BN_mod_exp_mont_consttime(a, BN_value_one(), BN_value_one(),\n b, ctx, NULL)))\n goto err;\n ERR_clear_error();\n st = 1;\nerr:\n BN_free(a);\n BN_free(b);\n BN_free(zero);\n BN_MONT_CTX_free(mont);\n return st;\n}', 'const BIGNUM *BN_value_one(void)\n{\n static const BN_ULONG data_one = 1L;\n static const BIGNUM const_one =\n { (BN_ULONG *)&data_one, 1, 1, 0, BN_FLG_STATIC_DATA };\n return &const_one;\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_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}', 'BN_ULONG bn_sub_part_words(BN_ULONG *r,\n const BN_ULONG *a, const BN_ULONG *b,\n int cl, int dl)\n{\n BN_ULONG c, t;\n assert(cl >= 0);\n c = bn_sub_words(r, a, b, cl);\n if (dl == 0)\n return c;\n r += cl;\n a += cl;\n b += cl;\n if (dl < 0) {\n for (;;) {\n t = b[0];\n r[0] = (0 - t - c) & BN_MASK2;\n if (t != 0)\n c = 1;\n if (++dl >= 0)\n break;\n t = b[1];\n r[1] = (0 - t - c) & BN_MASK2;\n if (t != 0)\n c = 1;\n if (++dl >= 0)\n break;\n t = b[2];\n r[2] = (0 - t - c) & BN_MASK2;\n if (t != 0)\n c = 1;\n if (++dl >= 0)\n break;\n t = b[3];\n r[3] = (0 - t - c) & BN_MASK2;\n if (t != 0)\n c = 1;\n if (++dl >= 0)\n break;\n b += 4;\n r += 4;\n }\n } else {\n int save_dl = dl;\n while (c) {\n t = a[0];\n r[0] = (t - c) & BN_MASK2;\n if (t != 0)\n c = 0;\n if (--dl <= 0)\n break;\n t = a[1];\n r[1] = (t - c) & BN_MASK2;\n if (t != 0)\n c = 0;\n if (--dl <= 0)\n break;\n t = a[2];\n r[2] = (t - c) & BN_MASK2;\n if (t != 0)\n c = 0;\n if (--dl <= 0)\n break;\n t = a[3];\n r[3] = (t - c) & BN_MASK2;\n if (t != 0)\n c = 0;\n if (--dl <= 0)\n break;\n save_dl = dl;\n a += 4;\n r += 4;\n }\n if (dl > 0) {\n if (save_dl > dl) {\n switch (save_dl - dl) {\n case 1:\n r[1] = a[1];\n if (--dl <= 0)\n break;\n case 2:\n r[2] = a[2];\n if (--dl <= 0)\n break;\n case 3:\n r[3] = a[3];\n if (--dl <= 0)\n break;\n }\n a += 4;\n r += 4;\n }\n }\n if (dl > 0) {\n for (;;) {\n r[0] = a[0];\n if (--dl <= 0)\n break;\n r[1] = a[1];\n if (--dl <= 0)\n break;\n r[2] = a[2];\n if (--dl <= 0)\n break;\n r[3] = a[3];\n if (--dl <= 0)\n break;\n a += 4;\n r += 4;\n }\n }\n }\n return c;\n}']
|
2,147
| 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 BN_GF2m_mod_solve_quad(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,\n BN_CTX *ctx)\n{\n int ret = 0;\n const int max = BN_num_bits(p) + 1;\n int *arr = NULL;\n bn_check_top(a);\n bn_check_top(p);\n if ((arr = OPENSSL_malloc(sizeof(*arr) * max)) == NULL)\n goto err;\n ret = BN_GF2m_poly2arr(p, arr, max);\n if (!ret || ret > max) {\n BNerr(BN_F_BN_GF2M_MOD_SOLVE_QUAD, BN_R_INVALID_LENGTH);\n goto err;\n }\n ret = BN_GF2m_mod_solve_quad_arr(r, a, arr, ctx);\n bn_check_top(r);\n err:\n OPENSSL_free(arr);\n return ret;\n}', 'int BN_GF2m_poly2arr(const BIGNUM *a, int p[], int max)\n{\n int i, j, k = 0;\n BN_ULONG mask;\n if (BN_is_zero(a))\n return 0;\n for (i = a->top - 1; i >= 0; i--) {\n if (!a->d[i])\n continue;\n mask = BN_TBIT;\n for (j = BN_BITS2 - 1; j >= 0; j--) {\n if (a->d[i] & mask) {\n if (k < max)\n p[k] = BN_BITS2 * i + j;\n k++;\n }\n mask >>= 1;\n }\n }\n if (k < max) {\n p[k] = -1;\n k++;\n }\n return k;\n}', 'int BN_GF2m_mod_solve_quad_arr(BIGNUM *r, const BIGNUM *a_, const int p[],\n BN_CTX *ctx)\n{\n int ret = 0, count = 0, j;\n BIGNUM *a, *z, *rho, *w, *w2, *tmp;\n bn_check_top(a_);\n if (!p[0]) {\n BN_zero(r);\n return 1;\n }\n BN_CTX_start(ctx);\n a = BN_CTX_get(ctx);\n z = BN_CTX_get(ctx);\n w = BN_CTX_get(ctx);\n if (w == NULL)\n goto err;\n if (!BN_GF2m_mod_arr(a, a_, p))\n goto err;\n if (BN_is_zero(a)) {\n BN_zero(r);\n ret = 1;\n goto err;\n }\n if (p[0] & 0x1) {\n if (!BN_copy(z, a))\n goto err;\n for (j = 1; j <= (p[0] - 1) / 2; j++) {\n if (!BN_GF2m_mod_sqr_arr(z, z, p, ctx))\n goto err;\n if (!BN_GF2m_mod_sqr_arr(z, z, p, ctx))\n goto err;\n if (!BN_GF2m_add(z, z, a))\n goto err;\n }\n } else {\n rho = BN_CTX_get(ctx);\n w2 = BN_CTX_get(ctx);\n tmp = BN_CTX_get(ctx);\n if (tmp == NULL)\n goto err;\n do {\n if (!BN_rand(rho, p[0], BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY))\n goto err;\n if (!BN_GF2m_mod_arr(rho, rho, p))\n goto err;\n BN_zero(z);\n if (!BN_copy(w, rho))\n goto err;\n for (j = 1; j <= p[0] - 1; j++) {\n if (!BN_GF2m_mod_sqr_arr(z, z, p, ctx))\n goto err;\n if (!BN_GF2m_mod_sqr_arr(w2, w, p, ctx))\n goto err;\n if (!BN_GF2m_mod_mul_arr(tmp, w2, a, p, ctx))\n goto err;\n if (!BN_GF2m_add(z, z, tmp))\n goto err;\n if (!BN_GF2m_add(w, w2, rho))\n goto err;\n }\n count++;\n } while (BN_is_zero(w) && (count < MAX_ITERATIONS));\n if (BN_is_zero(w)) {\n BNerr(BN_F_BN_GF2M_MOD_SOLVE_QUAD_ARR, BN_R_TOO_MANY_ITERATIONS);\n goto err;\n }\n }\n if (!BN_GF2m_mod_sqr_arr(w, z, p, ctx))\n goto err;\n if (!BN_GF2m_add(w, z, w))\n goto err;\n if (BN_GF2m_cmp(w, a)) {\n BNerr(BN_F_BN_GF2M_MOD_SOLVE_QUAD_ARR, BN_R_NO_SOLUTION);\n goto err;\n }\n if (!BN_copy(r, z))\n goto err;\n bn_check_top(r);\n ret = 1;\n err:\n BN_CTX_end(ctx);\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}']
|
2,148
| 0
|
https://github.com/openssl/openssl/blob/5ea564f154ebe8bda2a0e091a312e2058edf437f/crypto/bn/bn_shift.c/#L165
|
int BN_rshift(BIGNUM *r, const BIGNUM *a, int n)
{
int i, j, nw, lb, rb;
BN_ULONG *t, *f;
BN_ULONG l, tmp;
bn_check_top(r);
bn_check_top(a);
if (n < 0) {
BNerr(BN_F_BN_RSHIFT, BN_R_INVALID_SHIFT);
return 0;
}
nw = n / BN_BITS2;
rb = n % BN_BITS2;
lb = BN_BITS2 - rb;
if (nw >= a->top || a->top == 0) {
BN_zero(r);
return (1);
}
i = (BN_num_bits(a) - n + (BN_BITS2 - 1)) / BN_BITS2;
if (r != a) {
if (bn_wexpand(r, i) == NULL)
return (0);
r->neg = a->neg;
} else {
if (n == 0)
return 1;
}
f = &(a->d[nw]);
t = r->d;
j = a->top - nw;
r->top = i;
if (rb == 0) {
for (i = j; i != 0; i--)
*(t++) = *(f++);
} else {
l = *(f++);
for (i = j - 1; i != 0; i--) {
tmp = (l >> rb) & BN_MASK2;
l = *(f++);
*(t++) = (tmp | (l << lb)) & BN_MASK2;
}
if ((l = (l >> rb) & BN_MASK2))
*(t) = l;
}
if (!r->top)
r->neg = 0;
bn_check_top(r);
return (1);
}
|
['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 BN_ULONG mod = BN_mod_word(a, primes[i]);\n if (mod == (BN_ULONG)-1)\n goto err;\n if (mod == 0)\n return 0;\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 if (a->neg) {\n BIGNUM *t;\n if ((t = BN_CTX_get(ctx)) == NULL)\n goto err;\n if (BN_copy(t, a) == NULL)\n goto err;\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 BN_MONT_CTX_free(mont);\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_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}']
|
2,149
| 0
|
https://github.com/openssl/openssl/blob/ea32151f7b9353f8906188d007c6893704ac17bb/crypto/bn/bn_ctx.c/#L273
|
static unsigned int BN_STACK_pop(BN_STACK *st)
{
return st->indexes[--(st->depth)];
}
|
['int ec_GFp_nist_field_sqr(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,\n BN_CTX *ctx)\n{\n int ret = 0;\n BN_CTX *ctx_new = NULL;\n if (!group || !r || !a) {\n ECerr(EC_F_EC_GFP_NIST_FIELD_SQR, EC_R_PASSED_NULL_PARAMETER);\n goto err;\n }\n if (!ctx)\n if ((ctx_new = ctx = BN_CTX_new()) == NULL)\n goto err;\n if (!BN_sqr(r, a, ctx))\n goto err;\n if (!group->field_mod_func(r, r, group->field, ctx))\n goto err;\n ret = 1;\n err:\n BN_CTX_free(ctx_new);\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_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}']
|
2,150
| 0
|
https://github.com/openssl/openssl/blob/5850cc75ea0c1581a9034390f1ca77cadc596238/crypto/bn/bn_ctx.c/#L328
|
static unsigned int BN_STACK_pop(BN_STACK *st)
{
return st->indexes[--(st->depth)];
}
|
['static int compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh)\n{\n BN_CTX *ctx = NULL;\n BN_MONT_CTX *mont = NULL;\n BIGNUM *tmp;\n int ret = -1;\n int check_result;\n if (BN_num_bits(dh->p) > OPENSSL_DH_MAX_MODULUS_BITS) {\n DHerr(DH_F_COMPUTE_KEY, DH_R_MODULUS_TOO_LARGE);\n goto err;\n }\n ctx = BN_CTX_new();\n if (ctx == NULL)\n goto err;\n BN_CTX_start(ctx);\n tmp = BN_CTX_get(ctx);\n if (dh->priv_key == NULL) {\n DHerr(DH_F_COMPUTE_KEY, DH_R_NO_PRIVATE_VALUE);\n goto err;\n }\n if (dh->flags & DH_FLAG_CACHE_MONT_P) {\n mont = BN_MONT_CTX_set_locked(&dh->method_mont_p,\n CRYPTO_LOCK_DH, dh->p, ctx);\n if ((dh->flags & DH_FLAG_NO_EXP_CONSTTIME) == 0) {\n BN_set_flags(dh->priv_key, BN_FLG_CONSTTIME);\n }\n if (!mont)\n goto err;\n }\n if (!DH_check_pub_key(dh, pub_key, &check_result) || check_result) {\n DHerr(DH_F_COMPUTE_KEY, DH_R_INVALID_PUBKEY);\n goto err;\n }\n if (!dh->\n meth->bn_mod_exp(dh, tmp, pub_key, dh->priv_key, dh->p, ctx, mont)) {\n DHerr(DH_F_COMPUTE_KEY, ERR_R_BN_LIB);\n goto err;\n }\n ret = BN_bn2bin(tmp, key);\n err:\n if (ctx != NULL) {\n BN_CTX_end(ctx);\n BN_CTX_free(ctx);\n }\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}', 'BN_MONT_CTX *BN_MONT_CTX_set_locked(BN_MONT_CTX **pmont, int lock,\n const BIGNUM *mod, BN_CTX *ctx)\n{\n BN_MONT_CTX *ret;\n CRYPTO_r_lock(lock);\n ret = *pmont;\n CRYPTO_r_unlock(lock);\n if (ret)\n return ret;\n ret = BN_MONT_CTX_new();\n if (!ret)\n return NULL;\n if (!BN_MONT_CTX_set(ret, mod, ctx)) {\n BN_MONT_CTX_free(ret);\n return NULL;\n }\n CRYPTO_w_lock(lock);\n if (*pmont) {\n BN_MONT_CTX_free(ret);\n ret = *pmont;\n } else\n *pmont = ret;\n CRYPTO_w_unlock(lock);\n return ret;\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 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 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_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_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_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) <= (BN_BITS <= 32 ? 450 : 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 local_A, local_B;\n BIGNUM *pA, *pB;\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 pB = &local_B;\n local_B.flags = 0;\n BN_with_flags(pB, B, BN_FLG_CONSTTIME);\n if (!BN_nnmod(B, pB, A, ctx))\n goto err;\n }\n sign = -1;\n while (!BN_is_zero(B)) {\n BIGNUM *tmp;\n pA = &local_A;\n local_A.flags = 0;\n BN_with_flags(pA, A, BN_FLG_CONSTTIME);\n if (!BN_div(D, M, pA, B, ctx))\n goto err;\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}', '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 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 unsigned int BN_STACK_pop(BN_STACK *st)\n{\n return st->indexes[--(st->depth)];\n}']
|
2,151
| 0
|
https://github.com/openssl/openssl/blob/97d37b85d4e1a218fdc61dbe0dff3e7c8ff36121/crypto/rand/drbg_lib.c/#L189
|
static RAND_DRBG *rand_drbg_new(int secure,
int type,
unsigned int flags,
RAND_DRBG *parent)
{
RAND_DRBG *drbg = secure ?
OPENSSL_secure_zalloc(sizeof(*drbg)) : OPENSSL_zalloc(sizeof(*drbg));
if (drbg == NULL) {
RANDerr(RAND_F_RAND_DRBG_NEW, ERR_R_MALLOC_FAILURE);
goto err;
}
drbg->secure = secure && CRYPTO_secure_allocated(drbg);
drbg->fork_count = rand_fork_count;
drbg->parent = parent;
if (RAND_DRBG_set(drbg, type, flags) == 0)
goto err;
if (!RAND_DRBG_set_callbacks(drbg, rand_drbg_get_entropy,
rand_drbg_cleanup_entropy,
NULL, NULL))
goto err;
return drbg;
err:
if (drbg->secure)
OPENSSL_secure_free(drbg);
else
OPENSSL_free(drbg);
return NULL;
}
|
['static RAND_DRBG *rand_drbg_new(int secure,\n int type,\n unsigned int flags,\n RAND_DRBG *parent)\n{\n RAND_DRBG *drbg = secure ?\n OPENSSL_secure_zalloc(sizeof(*drbg)) : OPENSSL_zalloc(sizeof(*drbg));\n if (drbg == NULL) {\n RANDerr(RAND_F_RAND_DRBG_NEW, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n drbg->secure = secure && CRYPTO_secure_allocated(drbg);\n drbg->fork_count = rand_fork_count;\n drbg->parent = parent;\n if (RAND_DRBG_set(drbg, type, flags) == 0)\n goto err;\n if (!RAND_DRBG_set_callbacks(drbg, rand_drbg_get_entropy,\n rand_drbg_cleanup_entropy,\n NULL, NULL))\n goto err;\n return drbg;\nerr:\n if (drbg->secure)\n OPENSSL_secure_free(drbg);\n else\n OPENSSL_free(drbg);\n return NULL;\n}', 'void *CRYPTO_secure_zalloc(size_t num, const char *file, int line)\n{\n void *ret = CRYPTO_secure_malloc(num, file, line);\n if (ret != NULL)\n memset(ret, 0, num);\n return ret;\n}', 'void *CRYPTO_secure_malloc(size_t num, const char *file, int line)\n{\n#ifdef IMPLEMENTED\n void *ret;\n size_t actual_size;\n if (!secure_mem_initialized) {\n return CRYPTO_malloc(num, file, line);\n }\n CRYPTO_THREAD_write_lock(sec_malloc_lock);\n ret = sh_malloc(num);\n actual_size = ret ? sh_actual_size(ret) : 0;\n secure_mem_used += actual_size;\n CRYPTO_THREAD_unlock(sec_malloc_lock);\n return ret;\n#else\n return CRYPTO_malloc(num, file, line);\n#endif\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}']
|
2,152
| 0
|
https://github.com/libav/libav/blob/2e55e26b40e269816bba54da7d0e03955731b8fe/libavfilter/vf_yadif.c/#L259
|
static AVFrame *get_video_buffer(AVFilterLink *link, int w, int h)
{
AVFrame *frame;
int width = FFALIGN(w, 32);
int height = FFALIGN(h + 2, 32);
int i;
frame = ff_default_get_video_buffer(link, width, height);
frame->width = w;
frame->height = h;
for (i = 0; i < 3; i++)
frame->data[i] += frame->linesize[i];
return frame;
}
|
['static AVFrame *get_video_buffer(AVFilterLink *link, int w, int h)\n{\n AVFrame *frame;\n int width = FFALIGN(w, 32);\n int height = FFALIGN(h + 2, 32);\n int i;\n frame = ff_default_get_video_buffer(link, width, height);\n frame->width = w;\n frame->height = h;\n for (i = 0; i < 3; i++)\n frame->data[i] += frame->linesize[i];\n return frame;\n}', 'AVFrame *ff_default_get_video_buffer(AVFilterLink *link, int w, int h)\n{\n AVFrame *frame = av_frame_alloc();\n int ret;\n if (!frame)\n return NULL;\n if (link->hw_frames_ctx &&\n ((AVHWFramesContext*)link->hw_frames_ctx->data)->format == link->format) {\n ret = av_hwframe_get_buffer(link->hw_frames_ctx, frame, 0);\n } else {\n frame->width = w;\n frame->height = h;\n frame->format = link->format;\n ret = av_frame_get_buffer(frame, 32);\n }\n if (ret < 0)\n av_frame_free(&frame);\n return frame;\n}']
|
2,153
| 0
|
https://github.com/libav/libav/blob/91bfac759dfd536e439ad3e35964705012c6a5a7/libavcodec/mpegvideo_enc.c/#L1139
|
static int estimate_best_b_count(MpegEncContext *s)
{
AVCodec *codec = avcodec_find_encoder(s->avctx->codec_id);
AVCodecContext *c = avcodec_alloc_context3(NULL);
const int scale = s->avctx->brd_scale;
int i, j, out_size, p_lambda, b_lambda, lambda2;
int64_t best_rd = INT64_MAX;
int best_b_count = -1;
assert(scale >= 0 && scale <= 3);
p_lambda = s->last_lambda_for[AV_PICTURE_TYPE_P];
b_lambda = s->last_lambda_for[AV_PICTURE_TYPE_B];
if (!b_lambda)
b_lambda = p_lambda;
lambda2 = (b_lambda * b_lambda + (1 << FF_LAMBDA_SHIFT) / 2) >>
FF_LAMBDA_SHIFT;
c->width = s->width >> scale;
c->height = s->height >> scale;
c->flags = CODEC_FLAG_QSCALE | CODEC_FLAG_PSNR;
c->flags |= s->avctx->flags & CODEC_FLAG_QPEL;
c->mb_decision = s->avctx->mb_decision;
c->me_cmp = s->avctx->me_cmp;
c->mb_cmp = s->avctx->mb_cmp;
c->me_sub_cmp = s->avctx->me_sub_cmp;
c->pix_fmt = AV_PIX_FMT_YUV420P;
c->time_base = s->avctx->time_base;
c->max_b_frames = s->max_b_frames;
if (avcodec_open2(c, codec, NULL) < 0)
return -1;
for (i = 0; i < s->max_b_frames + 2; i++) {
Picture pre_input, *pre_input_ptr = i ? s->input_picture[i - 1] :
s->next_picture_ptr;
if (pre_input_ptr && (!i || s->input_picture[i - 1])) {
pre_input = *pre_input_ptr;
if (!pre_input.shared && i) {
pre_input.f->data[0] += INPLACE_OFFSET;
pre_input.f->data[1] += INPLACE_OFFSET;
pre_input.f->data[2] += INPLACE_OFFSET;
}
s->mpvencdsp.shrink[scale](s->tmp_frames[i]->data[0],
s->tmp_frames[i]->linesize[0],
pre_input.f->data[0],
pre_input.f->linesize[0],
c->width, c->height);
s->mpvencdsp.shrink[scale](s->tmp_frames[i]->data[1],
s->tmp_frames[i]->linesize[1],
pre_input.f->data[1],
pre_input.f->linesize[1],
c->width >> 1, c->height >> 1);
s->mpvencdsp.shrink[scale](s->tmp_frames[i]->data[2],
s->tmp_frames[i]->linesize[2],
pre_input.f->data[2],
pre_input.f->linesize[2],
c->width >> 1, c->height >> 1);
}
}
for (j = 0; j < s->max_b_frames + 1; j++) {
int64_t rd = 0;
if (!s->input_picture[j])
break;
c->error[0] = c->error[1] = c->error[2] = 0;
s->tmp_frames[0]->pict_type = AV_PICTURE_TYPE_I;
s->tmp_frames[0]->quality = 1 * FF_QP2LAMBDA;
out_size = encode_frame(c, s->tmp_frames[0]);
for (i = 0; i < s->max_b_frames + 1; i++) {
int is_p = i % (j + 1) == j || i == s->max_b_frames;
s->tmp_frames[i + 1]->pict_type = is_p ?
AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_B;
s->tmp_frames[i + 1]->quality = is_p ? p_lambda : b_lambda;
out_size = encode_frame(c, s->tmp_frames[i + 1]);
rd += (out_size * lambda2) >> (FF_LAMBDA_SHIFT - 3);
}
while (out_size) {
out_size = encode_frame(c, NULL);
rd += (out_size * lambda2) >> (FF_LAMBDA_SHIFT - 3);
}
rd += c->error[0] + c->error[1] + c->error[2];
if (rd < best_rd) {
best_rd = rd;
best_b_count = j;
}
}
avcodec_close(c);
av_freep(&c);
return best_b_count;
}
|
['static int estimate_best_b_count(MpegEncContext *s)\n{\n AVCodec *codec = avcodec_find_encoder(s->avctx->codec_id);\n AVCodecContext *c = avcodec_alloc_context3(NULL);\n const int scale = s->avctx->brd_scale;\n int i, j, out_size, p_lambda, b_lambda, lambda2;\n int64_t best_rd = INT64_MAX;\n int best_b_count = -1;\n assert(scale >= 0 && scale <= 3);\n p_lambda = s->last_lambda_for[AV_PICTURE_TYPE_P];\n b_lambda = s->last_lambda_for[AV_PICTURE_TYPE_B];\n if (!b_lambda)\n b_lambda = p_lambda;\n lambda2 = (b_lambda * b_lambda + (1 << FF_LAMBDA_SHIFT) / 2) >>\n FF_LAMBDA_SHIFT;\n c->width = s->width >> scale;\n c->height = s->height >> scale;\n c->flags = CODEC_FLAG_QSCALE | CODEC_FLAG_PSNR;\n c->flags |= s->avctx->flags & CODEC_FLAG_QPEL;\n c->mb_decision = s->avctx->mb_decision;\n c->me_cmp = s->avctx->me_cmp;\n c->mb_cmp = s->avctx->mb_cmp;\n c->me_sub_cmp = s->avctx->me_sub_cmp;\n c->pix_fmt = AV_PIX_FMT_YUV420P;\n c->time_base = s->avctx->time_base;\n c->max_b_frames = s->max_b_frames;\n if (avcodec_open2(c, codec, NULL) < 0)\n return -1;\n for (i = 0; i < s->max_b_frames + 2; i++) {\n Picture pre_input, *pre_input_ptr = i ? s->input_picture[i - 1] :\n s->next_picture_ptr;\n if (pre_input_ptr && (!i || s->input_picture[i - 1])) {\n pre_input = *pre_input_ptr;\n if (!pre_input.shared && i) {\n pre_input.f->data[0] += INPLACE_OFFSET;\n pre_input.f->data[1] += INPLACE_OFFSET;\n pre_input.f->data[2] += INPLACE_OFFSET;\n }\n s->mpvencdsp.shrink[scale](s->tmp_frames[i]->data[0],\n s->tmp_frames[i]->linesize[0],\n pre_input.f->data[0],\n pre_input.f->linesize[0],\n c->width, c->height);\n s->mpvencdsp.shrink[scale](s->tmp_frames[i]->data[1],\n s->tmp_frames[i]->linesize[1],\n pre_input.f->data[1],\n pre_input.f->linesize[1],\n c->width >> 1, c->height >> 1);\n s->mpvencdsp.shrink[scale](s->tmp_frames[i]->data[2],\n s->tmp_frames[i]->linesize[2],\n pre_input.f->data[2],\n pre_input.f->linesize[2],\n c->width >> 1, c->height >> 1);\n }\n }\n for (j = 0; j < s->max_b_frames + 1; j++) {\n int64_t rd = 0;\n if (!s->input_picture[j])\n break;\n c->error[0] = c->error[1] = c->error[2] = 0;\n s->tmp_frames[0]->pict_type = AV_PICTURE_TYPE_I;\n s->tmp_frames[0]->quality = 1 * FF_QP2LAMBDA;\n out_size = encode_frame(c, s->tmp_frames[0]);\n for (i = 0; i < s->max_b_frames + 1; i++) {\n int is_p = i % (j + 1) == j || i == s->max_b_frames;\n s->tmp_frames[i + 1]->pict_type = is_p ?\n AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_B;\n s->tmp_frames[i + 1]->quality = is_p ? p_lambda : b_lambda;\n out_size = encode_frame(c, s->tmp_frames[i + 1]);\n rd += (out_size * lambda2) >> (FF_LAMBDA_SHIFT - 3);\n }\n while (out_size) {\n out_size = encode_frame(c, NULL);\n rd += (out_size * lambda2) >> (FF_LAMBDA_SHIFT - 3);\n }\n rd += c->error[0] + c->error[1] + c->error[2];\n if (rd < best_rd) {\n best_rd = rd;\n best_b_count = j;\n }\n }\n avcodec_close(c);\n av_freep(&c);\n return best_b_count;\n}', 'AVCodec *avcodec_find_encoder(enum AVCodecID id)\n{\n return find_encdec(id, 1);\n}', 'AVCodecContext *avcodec_alloc_context3(const AVCodec *codec)\n{\n AVCodecContext *avctx= av_malloc(sizeof(AVCodecContext));\n if (!avctx)\n return NULL;\n if(avcodec_get_context_defaults3(avctx, codec) < 0){\n av_free(avctx);\n return NULL;\n }\n return avctx;\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}']
|
2,154
| 0
|
https://github.com/libav/libav/blob/fcc0224e4fbd44ae268903185b0cf83560b13555/libavcodec/h264_loopfilter.c/#L130
|
static void av_always_inline filter_mb_edgecv( uint8_t *pix, int stride, int16_t bS[4], unsigned int qp, H264Context *h ) {
const int qp_bd_offset = 6 * (h->sps.bit_depth_luma - 8);
const unsigned int index_a = qp - qp_bd_offset + h->slice_alpha_c0_offset;
const int alpha = alpha_table[index_a];
const int beta = beta_table[qp - qp_bd_offset + h->slice_beta_offset];
if (alpha ==0 || beta == 0) return;
if( bS[0] < 4 ) {
int8_t tc[4];
tc[0] = tc0_table[index_a][bS[0]]+1;
tc[1] = tc0_table[index_a][bS[1]]+1;
tc[2] = tc0_table[index_a][bS[2]]+1;
tc[3] = tc0_table[index_a][bS[3]]+1;
h->h264dsp.h264_h_loop_filter_chroma(pix, stride, alpha, beta, tc);
} else {
h->h264dsp.h264_h_loop_filter_chroma_intra(pix, stride, alpha, beta);
}
}
|
['void ff_h264_filter_mb_fast( 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 int mb_xy;\n int mb_type, left_type;\n int qp, qp0, qp1, qpc, qpc0, qpc1, qp_thresh;\n mb_xy = h->mb_xy;\n if(!h->top_type || !h->h264dsp.h264_loop_filter_strength || h->pps.chroma_qp_diff) {\n ff_h264_filter_mb(h, mb_x, mb_y, img_y, img_cb, img_cr, linesize, uvlinesize);\n return;\n }\n assert(!FRAME_MBAFF);\n left_type= h->left_type[0];\n mb_type = s->current_picture.mb_type[mb_xy];\n qp = s->current_picture.qscale_table[mb_xy];\n qp0 = s->current_picture.qscale_table[mb_xy-1];\n qp1 = s->current_picture.qscale_table[h->top_mb_xy];\n qpc = get_chroma_qp( h, 0, qp );\n qpc0 = get_chroma_qp( h, 0, qp0 );\n qpc1 = get_chroma_qp( h, 0, qp1 );\n qp0 = (qp + qp0 + 1) >> 1;\n qp1 = (qp + qp1 + 1) >> 1;\n qpc0 = (qpc + qpc0 + 1) >> 1;\n qpc1 = (qpc + qpc1 + 1) >> 1;\n qp_thresh = 15+52 - h->slice_alpha_c0_offset;\n if(qp <= qp_thresh && qp0 <= qp_thresh && qp1 <= qp_thresh &&\n qpc <= qp_thresh && qpc0 <= qp_thresh && qpc1 <= qp_thresh)\n return;\n if( IS_INTRA(mb_type) ) {\n int16_t bS4[4] = {4,4,4,4};\n int16_t bS3[4] = {3,3,3,3};\n int16_t *bSH = FIELD_PICTURE ? bS3 : bS4;\n if(left_type)\n filter_mb_edgev( &img_y[4*0], linesize, bS4, qp0, h);\n if( IS_8x8DCT(mb_type) ) {\n filter_mb_edgev( &img_y[4*2], linesize, bS3, qp, h);\n filter_mb_edgeh( &img_y[4*0*linesize], linesize, bSH, qp1, h);\n filter_mb_edgeh( &img_y[4*2*linesize], linesize, bS3, qp, h);\n } else {\n filter_mb_edgev( &img_y[4*1], linesize, bS3, qp, h);\n filter_mb_edgev( &img_y[4*2], linesize, bS3, qp, h);\n filter_mb_edgev( &img_y[4*3], linesize, bS3, qp, h);\n filter_mb_edgeh( &img_y[4*0*linesize], linesize, bSH, qp1, h);\n filter_mb_edgeh( &img_y[4*1*linesize], linesize, bS3, qp, h);\n filter_mb_edgeh( &img_y[4*2*linesize], linesize, bS3, qp, h);\n filter_mb_edgeh( &img_y[4*3*linesize], linesize, bS3, qp, h);\n }\n if(left_type){\n filter_mb_edgecv( &img_cb[2*0], uvlinesize, bS4, qpc0, h);\n filter_mb_edgecv( &img_cr[2*0], uvlinesize, bS4, qpc0, h);\n }\n filter_mb_edgecv( &img_cb[2*2], uvlinesize, bS3, qpc, h);\n filter_mb_edgecv( &img_cr[2*2], uvlinesize, bS3, qpc, h);\n filter_mb_edgech( &img_cb[2*0*uvlinesize], uvlinesize, bSH, qpc1, h);\n filter_mb_edgech( &img_cb[2*2*uvlinesize], uvlinesize, bS3, qpc, h);\n filter_mb_edgech( &img_cr[2*0*uvlinesize], uvlinesize, bSH, qpc1, h);\n filter_mb_edgech( &img_cr[2*2*uvlinesize], uvlinesize, bS3, qpc, h);\n return;\n } else {\n LOCAL_ALIGNED_8(int16_t, bS, [2], [4][4]);\n int edges;\n if( IS_8x8DCT(mb_type) && (h->cbp&7) == 7 ) {\n edges = 4;\n AV_WN64A(bS[0][0], 0x0002000200020002ULL);\n AV_WN64A(bS[0][2], 0x0002000200020002ULL);\n AV_WN64A(bS[1][0], 0x0002000200020002ULL);\n AV_WN64A(bS[1][2], 0x0002000200020002ULL);\n } else {\n int mask_edge1 = (3*(((5*mb_type)>>5)&1)) | (mb_type>>4);\n int mask_edge0 = 3*((mask_edge1>>1) & ((5*left_type)>>5)&1);\n int step = 1+(mb_type>>24);\n edges = 4 - 3*((mb_type>>3) & !(h->cbp & 15));\n h->h264dsp.h264_loop_filter_strength( bS, h->non_zero_count_cache, h->ref_cache, h->mv_cache,\n h->list_count==2, edges, step, mask_edge0, mask_edge1, FIELD_PICTURE);\n }\n if( IS_INTRA(left_type) )\n AV_WN64A(bS[0][0], 0x0004000400040004ULL);\n if( IS_INTRA(h->top_type) )\n AV_WN64A(bS[1][0], FIELD_PICTURE ? 0x0003000300030003ULL : 0x0004000400040004ULL);\n#define FILTER(hv,dir,edge)\\\n if(AV_RN64A(bS[dir][edge])) { \\\n filter_mb_edge##hv( &img_y[4*edge*(dir?linesize:1)], linesize, bS[dir][edge], edge ? qp : qp##dir, h );\\\n if(!(edge&1)) {\\\n filter_mb_edgec##hv( &img_cb[2*edge*(dir?uvlinesize:1)], uvlinesize, bS[dir][edge], edge ? qpc : qpc##dir, h );\\\n filter_mb_edgec##hv( &img_cr[2*edge*(dir?uvlinesize:1)], uvlinesize, bS[dir][edge], edge ? qpc : qpc##dir, h );\\\n }\\\n }\n if(left_type)\n FILTER(v,0,0);\n if( edges == 1 ) {\n FILTER(h,1,0);\n } else if( IS_8x8DCT(mb_type) ) {\n FILTER(v,0,2);\n FILTER(h,1,0);\n FILTER(h,1,2);\n } else {\n FILTER(v,0,1);\n FILTER(v,0,2);\n FILTER(v,0,3);\n FILTER(h,1,0);\n FILTER(h,1,1);\n FILTER(h,1,2);\n FILTER(h,1,3);\n }\n#undef FILTER\n }\n}', 'static void av_always_inline filter_mb_edgecv( uint8_t *pix, int stride, int16_t bS[4], unsigned int qp, H264Context *h ) {\n const int qp_bd_offset = 6 * (h->sps.bit_depth_luma - 8);\n const unsigned int index_a = qp - qp_bd_offset + h->slice_alpha_c0_offset;\n const int alpha = alpha_table[index_a];\n const int beta = beta_table[qp - qp_bd_offset + h->slice_beta_offset];\n if (alpha ==0 || beta == 0) return;\n if( bS[0] < 4 ) {\n int8_t tc[4];\n tc[0] = tc0_table[index_a][bS[0]]+1;\n tc[1] = tc0_table[index_a][bS[1]]+1;\n tc[2] = tc0_table[index_a][bS[2]]+1;\n tc[3] = tc0_table[index_a][bS[3]]+1;\n h->h264dsp.h264_h_loop_filter_chroma(pix, stride, alpha, beta, tc);\n } else {\n h->h264dsp.h264_h_loop_filter_chroma_intra(pix, stride, alpha, beta);\n }\n}']
|
2,155
| 0
|
https://github.com/openssl/openssl/blob/0818dbadf32d193973d84a0736c099166777c071/ssl/statem/statem_clnt.c/#L2230
|
static int tls_construct_cke_rsa(SSL *s, unsigned char **p, int *len, int *al)
{
#ifndef OPENSSL_NO_RSA
unsigned char *q;
EVP_PKEY *pkey = NULL;
EVP_PKEY_CTX *pctx = NULL;
size_t enclen;
unsigned char *pms = NULL;
size_t pmslen = 0;
if (s->session->peer == NULL) {
SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_INTERNAL_ERROR);
return 0;
}
pkey = X509_get0_pubkey(s->session->peer);
if (EVP_PKEY_get0_RSA(pkey) == NULL) {
SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_INTERNAL_ERROR);
return 0;
}
pmslen = SSL_MAX_MASTER_KEY_LENGTH;
pms = OPENSSL_malloc(pmslen);
if (pms == NULL) {
SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_MALLOC_FAILURE);
*al = SSL_AD_INTERNAL_ERROR;
return 0;
}
pms[0] = s->client_version >> 8;
pms[1] = s->client_version & 0xff;
if (RAND_bytes(pms + 2, pmslen - 2) <= 0) {
goto err;
}
q = *p;
if (s->version > SSL3_VERSION)
*p += 2;
pctx = EVP_PKEY_CTX_new(pkey, NULL);
if (pctx == NULL || EVP_PKEY_encrypt_init(pctx) <= 0
|| EVP_PKEY_encrypt(pctx, NULL, &enclen, pms, pmslen) <= 0) {
SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_EVP_LIB);
goto err;
}
if (EVP_PKEY_encrypt(pctx, *p, &enclen, pms, pmslen) <= 0) {
SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, SSL_R_BAD_RSA_ENCRYPT);
goto err;
}
*len = enclen;
EVP_PKEY_CTX_free(pctx);
pctx = NULL;
# ifdef PKCS1_CHECK
if (s->options & SSL_OP_PKCS1_CHECK_1)
(*p)[1]++;
if (s->options & SSL_OP_PKCS1_CHECK_2)
tmp_buf[0] = 0x70;
# endif
if (s->version > SSL3_VERSION) {
s2n(*len, q);
*len += 2;
}
s->s3->tmp.pms = pms;
s->s3->tmp.pmslen = pmslen;
return 1;
err:
OPENSSL_clear_free(pms, pmslen);
EVP_PKEY_CTX_free(pctx);
return 0;
#else
SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_INTERNAL_ERROR);
*al = SSL_AD_INTERNAL_ERROR;
return 0;
#endif
}
|
['static int tls_construct_cke_rsa(SSL *s, unsigned char **p, int *len, int *al)\n{\n#ifndef OPENSSL_NO_RSA\n unsigned char *q;\n EVP_PKEY *pkey = NULL;\n EVP_PKEY_CTX *pctx = NULL;\n size_t enclen;\n unsigned char *pms = NULL;\n size_t pmslen = 0;\n if (s->session->peer == NULL) {\n SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_INTERNAL_ERROR);\n return 0;\n }\n pkey = X509_get0_pubkey(s->session->peer);\n if (EVP_PKEY_get0_RSA(pkey) == NULL) {\n SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_INTERNAL_ERROR);\n return 0;\n }\n pmslen = SSL_MAX_MASTER_KEY_LENGTH;\n pms = OPENSSL_malloc(pmslen);\n if (pms == NULL) {\n SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_MALLOC_FAILURE);\n *al = SSL_AD_INTERNAL_ERROR;\n return 0;\n }\n pms[0] = s->client_version >> 8;\n pms[1] = s->client_version & 0xff;\n if (RAND_bytes(pms + 2, pmslen - 2) <= 0) {\n goto err;\n }\n q = *p;\n if (s->version > SSL3_VERSION)\n *p += 2;\n pctx = EVP_PKEY_CTX_new(pkey, NULL);\n if (pctx == NULL || EVP_PKEY_encrypt_init(pctx) <= 0\n || EVP_PKEY_encrypt(pctx, NULL, &enclen, pms, pmslen) <= 0) {\n SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_EVP_LIB);\n goto err;\n }\n if (EVP_PKEY_encrypt(pctx, *p, &enclen, pms, pmslen) <= 0) {\n SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, SSL_R_BAD_RSA_ENCRYPT);\n goto err;\n }\n *len = enclen;\n EVP_PKEY_CTX_free(pctx);\n pctx = NULL;\n# ifdef PKCS1_CHECK\n if (s->options & SSL_OP_PKCS1_CHECK_1)\n (*p)[1]++;\n if (s->options & SSL_OP_PKCS1_CHECK_2)\n tmp_buf[0] = 0x70;\n# endif\n if (s->version > SSL3_VERSION) {\n s2n(*len, q);\n *len += 2;\n }\n s->s3->tmp.pms = pms;\n s->s3->tmp.pmslen = pmslen;\n return 1;\n err:\n OPENSSL_clear_free(pms, pmslen);\n EVP_PKEY_CTX_free(pctx);\n return 0;\n#else\n SSLerr(SSL_F_TLS_CONSTRUCT_CKE_RSA, ERR_R_INTERNAL_ERROR);\n *al = SSL_AD_INTERNAL_ERROR;\n return 0;\n#endif\n}', 'EVP_PKEY *X509_get0_pubkey(const X509 *x)\n{\n if (x == NULL)\n return NULL;\n return X509_PUBKEY_get0(x->cert_info.key);\n}', 'EVP_PKEY *X509_PUBKEY_get0(X509_PUBKEY *key)\n{\n EVP_PKEY *ret = NULL;\n if (key == NULL || key->public_key == NULL)\n return NULL;\n if (key->pkey != NULL)\n return key->pkey;\n x509_pubkey_decode(&ret, key);\n if (ret != NULL) {\n X509err(X509_F_X509_PUBKEY_GET0, ERR_R_INTERNAL_ERROR);\n EVP_PKEY_free(ret);\n }\n return NULL;\n}', 'RSA *EVP_PKEY_get0_RSA(EVP_PKEY *pkey)\n{\n if (pkey->type != EVP_PKEY_RSA) {\n EVPerr(EVP_F_EVP_PKEY_GET0_RSA, EVP_R_EXPECTING_AN_RSA_KEY);\n return NULL;\n }\n return pkey->pkey.rsa;\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}', 'int RAND_bytes(unsigned char *buf, int num)\n{\n const RAND_METHOD *meth = RAND_get_rand_method();\n if (meth && meth->bytes)\n return meth->bytes(buf, num);\n return (-1);\n}', 'const RAND_METHOD *RAND_get_rand_method(void)\n{\n if (!default_RAND_meth) {\n#ifndef OPENSSL_NO_ENGINE\n ENGINE *e = ENGINE_get_default_RAND();\n if (e) {\n default_RAND_meth = ENGINE_get_RAND(e);\n if (default_RAND_meth == NULL) {\n ENGINE_finish(e);\n e = NULL;\n }\n }\n if (e)\n funct_ref = e;\n else\n#endif\n default_RAND_meth = RAND_OpenSSL();\n }\n return default_RAND_meth;\n}', 'void CRYPTO_clear_free(void *str, size_t num, const char *file, int line)\n{\n if (str == NULL)\n return;\n if (num)\n OPENSSL_cleanse(str, num);\n CRYPTO_free(str, file, line);\n}', 'void CRYPTO_free(void *str, const char *file, int line)\n{\n if (free_impl != NULL && free_impl != &CRYPTO_free) {\n free_impl(str, file, line);\n return;\n }\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n if (call_malloc_debug) {\n CRYPTO_mem_debug_free(str, 0, file, line);\n free(str);\n CRYPTO_mem_debug_free(str, 1, file, line);\n } else {\n free(str);\n }\n#else\n free(str);\n#endif\n}']
|
2,156
| 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 void synthfilt_build_sb_samples(QDM2Context *q, BitstreamContext *bc,\n int length, int sb_min, int sb_max)\n{\n int sb, j, k, n, ch, run, channels;\n int joined_stereo, zero_encoding;\n int type34_first;\n float type34_div = 0;\n float type34_predictor;\n float samples[10], sign_bits[16];\n if (length == 0) {\n for (sb=sb_min; sb < sb_max; sb++)\n build_sb_samples_from_noise(q, sb);\n return;\n }\n for (sb = sb_min; sb < sb_max; sb++) {\n channels = q->nb_channels;\n if (q->nb_channels <= 1 || sb < 12)\n joined_stereo = 0;\n else if (sb >= 24)\n joined_stereo = 1;\n else\n joined_stereo = (bitstream_bits_left(bc) >= 1) ? bitstream_read_bit(bc) : 0;\n if (joined_stereo) {\n if (bitstream_bits_left(bc) >= 16)\n for (j = 0; j < 16; j++)\n sign_bits[j] = bitstream_read_bit(bc);\n for (j = 0; j < 64; j++)\n if (q->coding_method[1][sb][j] > q->coding_method[0][sb][j])\n q->coding_method[0][sb][j] = q->coding_method[1][sb][j];\n if (fix_coding_method_array(sb, q->nb_channels,\n q->coding_method)) {\n build_sb_samples_from_noise(q, sb);\n continue;\n }\n channels = 1;\n }\n for (ch = 0; ch < channels; ch++) {\n FIX_NOISE_IDX(q->noise_idx);\n zero_encoding = (bitstream_bits_left(bc) >= 1) ? bitstream_read_bit(bc) : 0;\n type34_predictor = 0.0;\n type34_first = 1;\n for (j = 0; j < 128; ) {\n switch (q->coding_method[ch][sb][j / 2]) {\n case 8:\n if (bitstream_bits_left(bc) >= 10) {\n if (zero_encoding) {\n for (k = 0; k < 5; k++) {\n if ((j + 2 * k) >= 128)\n break;\n samples[2 * k] = bitstream_read_bit(bc) ? dequant_1bit[joined_stereo][2 * bitstream_read_bit(bc)] : 0;\n }\n } else {\n n = bitstream_read(bc, 8);\n for (k = 0; k < 5; k++)\n samples[2 * k] = dequant_1bit[joined_stereo][random_dequant_index[n][k]];\n }\n for (k = 0; k < 5; k++)\n samples[2 * k + 1] = SB_DITHERING_NOISE(sb,q->noise_idx);\n } else {\n for (k = 0; k < 10; k++)\n samples[k] = SB_DITHERING_NOISE(sb,q->noise_idx);\n }\n run = 10;\n break;\n case 10:\n if (bitstream_bits_left(bc) >= 1) {\n float f = 0.81;\n if (bitstream_read_bit(bc))\n f = -f;\n f -= noise_samples[((sb + 1) * (j +5 * ch + 1)) & 127] * 9.0 / 40.0;\n samples[0] = f;\n } else {\n samples[0] = SB_DITHERING_NOISE(sb,q->noise_idx);\n }\n run = 1;\n break;\n case 16:\n if (bitstream_bits_left(bc) >= 10) {\n if (zero_encoding) {\n for (k = 0; k < 5; k++) {\n if ((j + k) >= 128)\n break;\n samples[k] = (bitstream_read_bit(bc) == 0) ? 0 : dequant_1bit[joined_stereo][2 * bitstream_read_bit(bc)];\n }\n } else {\n n = bitstream_read (bc, 8);\n for (k = 0; k < 5; k++)\n samples[k] = dequant_1bit[joined_stereo][random_dequant_index[n][k]];\n }\n } else {\n for (k = 0; k < 5; k++)\n samples[k] = SB_DITHERING_NOISE(sb,q->noise_idx);\n }\n run = 5;\n break;\n case 24:\n if (bitstream_bits_left(bc) >= 7) {\n n = bitstream_read(bc, 7);\n for (k = 0; k < 3; k++)\n samples[k] = (random_dequant_type24[n][k] - 2.0) * 0.5;\n } else {\n for (k = 0; k < 3; k++)\n samples[k] = SB_DITHERING_NOISE(sb,q->noise_idx);\n }\n run = 3;\n break;\n case 30:\n if (bitstream_bits_left(bc) >= 4) {\n unsigned index = qdm2_get_vlc(bc, &vlc_tab_type30, 0, 1);\n if (index < FF_ARRAY_ELEMS(type30_dequant)) {\n samples[0] = type30_dequant[index];\n } else\n samples[0] = SB_DITHERING_NOISE(sb,q->noise_idx);\n } else\n samples[0] = SB_DITHERING_NOISE(sb,q->noise_idx);\n run = 1;\n break;\n case 34:\n if (bitstream_bits_left(bc) >= 7) {\n if (type34_first) {\n type34_div = (float)(1 << bitstream_read(bc, 2));\n samples[0] = ((float)bitstream_read(bc, 5) - 16.0) / 15.0;\n type34_predictor = samples[0];\n type34_first = 0;\n } else {\n unsigned index = qdm2_get_vlc(bc, &vlc_tab_type34, 0, 1);\n if (index < FF_ARRAY_ELEMS(type34_delta)) {\n samples[0] = type34_delta[index] / type34_div + type34_predictor;\n type34_predictor = samples[0];\n } else\n samples[0] = SB_DITHERING_NOISE(sb,q->noise_idx);\n }\n } else {\n samples[0] = SB_DITHERING_NOISE(sb,q->noise_idx);\n }\n run = 1;\n break;\n default:\n samples[0] = SB_DITHERING_NOISE(sb,q->noise_idx);\n run = 1;\n break;\n }\n if (joined_stereo) {\n for (k = 0; k < run && j + k < 128; k++) {\n q->sb_samples[0][j + k][sb] =\n q->tone_level[0][sb][(j + k) / 2] * samples[k];\n if (q->nb_channels == 2) {\n if (sign_bits[(j + k) / 8])\n q->sb_samples[1][j + k][sb] =\n q->tone_level[1][sb][(j + k) / 2] * -samples[k];\n else\n q->sb_samples[1][j + k][sb] =\n q->tone_level[1][sb][(j + k) / 2] * samples[k];\n }\n }\n } else {\n for (k = 0; k < run; k++)\n if ((j + k) < 128)\n q->sb_samples[ch][j + k][sb] = q->tone_level[ch][sb][(j + k)/2] * samples[k];\n }\n j += run;\n }\n }\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 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}']
|
2,157
| 0
|
https://github.com/libav/libav/blob/75366a504dfc30deadeac71c35e3c444275986f9/cmdutils.c/#L442
|
int opt_timelimit(void *optctx, const char *opt, const char *arg)
{
#if HAVE_SETRLIMIT
int lim = parse_number_or_die(opt, arg, OPT_INT64, 0, INT_MAX);
struct rlimit rl = { lim, lim + 1 };
if (setrlimit(RLIMIT_CPU, &rl))
perror("setrlimit");
#else
av_log(NULL, AV_LOG_WARNING, "-%s not implemented on this OS\n", opt);
#endif
return 0;
}
|
['int opt_timelimit(void *optctx, const char *opt, const char *arg)\n{\n#if HAVE_SETRLIMIT\n int lim = parse_number_or_die(opt, arg, OPT_INT64, 0, INT_MAX);\n struct rlimit rl = { lim, lim + 1 };\n if (setrlimit(RLIMIT_CPU, &rl))\n perror("setrlimit");\n#else\n av_log(NULL, AV_LOG_WARNING, "-%s not implemented on this OS\\n", opt);\n#endif\n return 0;\n}', 'double parse_number_or_die(const char *context, const char *numstr, int type,\n double min, double max)\n{\n char *tail;\n const char *error;\n double d = av_strtod(numstr, &tail);\n if (*tail)\n error = "Expected number for %s but found: %s\\n";\n else if (d < min || d > max)\n error = "The value for %s was %s which is not within %f - %f\\n";\n else if (type == OPT_INT64 && (int64_t)d != d)\n error = "Expected int64 for %s but found %s\\n";\n else if (type == OPT_INT && (int)d != d)\n error = "Expected int for %s but found %s\\n";\n else\n return d;\n av_log(NULL, AV_LOG_FATAL, error, context, numstr, min, max);\n exit(1);\n return 0;\n}']
|
2,158
| 0
|
https://github.com/openssl/openssl/blob/ea32151f7b9353f8906188d007c6893704ac17bb/ssl/record/ssl3_record.c/#L1239
|
void ssl3_cbc_copy_mac(unsigned char *out,
const SSL3_RECORD *rec, unsigned md_size)
{
#if defined(CBC_MAC_ROTATE_IN_PLACE)
unsigned char rotated_mac_buf[64 + EVP_MAX_MD_SIZE];
unsigned char *rotated_mac;
#else
unsigned char rotated_mac[EVP_MAX_MD_SIZE];
#endif
unsigned mac_end = rec->length;
unsigned mac_start = mac_end - md_size;
unsigned scan_start = 0;
unsigned i, j;
unsigned div_spoiler;
unsigned rotate_offset;
OPENSSL_assert(rec->orig_len >= md_size);
OPENSSL_assert(md_size <= EVP_MAX_MD_SIZE);
#if defined(CBC_MAC_ROTATE_IN_PLACE)
rotated_mac = rotated_mac_buf + ((0 - (size_t)rotated_mac_buf) & 63);
#endif
if (rec->orig_len > md_size + 255 + 1)
scan_start = rec->orig_len - (md_size + 255 + 1);
div_spoiler = md_size >> 1;
div_spoiler <<= (sizeof(div_spoiler) - 1) * 8;
rotate_offset = (div_spoiler + mac_start - scan_start) % md_size;
memset(rotated_mac, 0, md_size);
for (i = scan_start, j = 0; i < rec->orig_len; i++) {
unsigned char mac_started = constant_time_ge_8(i, mac_start);
unsigned char mac_ended = constant_time_ge_8(i, mac_end);
unsigned char b = rec->data[i];
rotated_mac[j++] |= b & mac_started & ~mac_ended;
j &= constant_time_lt(j, md_size);
}
#if defined(CBC_MAC_ROTATE_IN_PLACE)
j = 0;
for (i = 0; i < md_size; i++) {
((volatile unsigned char *)rotated_mac)[rotate_offset ^ 32];
out[j++] = rotated_mac[rotate_offset++];
rotate_offset &= constant_time_lt(rotate_offset, md_size);
}
#else
memset(out, 0, md_size);
rotate_offset = md_size - rotate_offset;
rotate_offset &= constant_time_lt(rotate_offset, md_size);
for (i = 0; i < md_size; i++) {
for (j = 0; j < md_size; j++)
out[j] |= rotated_mac[i] & constant_time_eq_8(j, rotate_offset);
rotate_offset++;
rotate_offset &= constant_time_lt(rotate_offset, md_size);
}
#endif
}
|
['int ssl3_get_record(SSL *s)\n{\n int ssl_major, ssl_minor, al;\n int enc_err, n, i, ret = -1;\n SSL3_RECORD *rr;\n SSL3_BUFFER *rbuf;\n SSL_SESSION *sess;\n unsigned char *p;\n unsigned char md[EVP_MAX_MD_SIZE];\n short version;\n unsigned mac_size;\n unsigned empty_record_count = 0, curr_empty = 0;\n unsigned int num_recs = 0;\n unsigned int max_recs;\n unsigned int j;\n rr = RECORD_LAYER_get_rrec(&s->rlayer);\n rbuf = RECORD_LAYER_get_rbuf(&s->rlayer);\n max_recs = s->max_pipelines;\n if (max_recs == 0)\n max_recs = 1;\n sess = s->session;\n again:\n do {\n if ((RECORD_LAYER_get_rstate(&s->rlayer) != SSL_ST_READ_BODY) ||\n (RECORD_LAYER_get_packet_length(&s->rlayer)\n < SSL3_RT_HEADER_LENGTH)) {\n n = ssl3_read_n(s, SSL3_RT_HEADER_LENGTH,\n SSL3_BUFFER_get_len(rbuf), 0, num_recs == 0 ? 1 : 0);\n if (n <= 0)\n return (n);\n RECORD_LAYER_set_rstate(&s->rlayer, SSL_ST_READ_BODY);\n p = RECORD_LAYER_get_packet(&s->rlayer);\n if (s->first_packet && s->server && !s->read_hash\n && !s->enc_read_ctx\n && (p[0] & 0x80) && (p[2] == SSL2_MT_CLIENT_HELLO)) {\n rr[num_recs].type = SSL3_RT_HANDSHAKE;\n rr[num_recs].rec_version = SSL2_VERSION;\n rr[num_recs].length = ((p[0] & 0x7f) << 8) | p[1];\n if (rr[num_recs].length > SSL3_BUFFER_get_len(rbuf)\n - SSL2_RT_HEADER_LENGTH) {\n al = SSL_AD_RECORD_OVERFLOW;\n SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_PACKET_LENGTH_TOO_LONG);\n goto f_err;\n }\n if (rr[num_recs].length < MIN_SSL2_RECORD_LEN) {\n al = SSL_AD_HANDSHAKE_FAILURE;\n SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_LENGTH_TOO_SHORT);\n goto f_err;\n }\n } else {\n if (s->msg_callback)\n s->msg_callback(0, 0, SSL3_RT_HEADER, p, 5, s,\n s->msg_callback_arg);\n rr[num_recs].type = *(p++);\n ssl_major = *(p++);\n ssl_minor = *(p++);\n version = (ssl_major << 8) | ssl_minor;\n rr[num_recs].rec_version = version;\n n2s(p, rr[num_recs].length);\n if (!s->first_packet && version != s->version) {\n SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_WRONG_VERSION_NUMBER);\n if ((s->version & 0xFF00) == (version & 0xFF00)\n && !s->enc_write_ctx && !s->write_hash) {\n if (rr->type == SSL3_RT_ALERT) {\n goto err;\n }\n s->version = (unsigned short)version;\n }\n al = SSL_AD_PROTOCOL_VERSION;\n goto f_err;\n }\n if ((version >> 8) != SSL3_VERSION_MAJOR) {\n if (s->first_packet) {\n p = RECORD_LAYER_get_packet(&s->rlayer);\n if (strncmp((char *)p, "GET ", 4) == 0 ||\n strncmp((char *)p, "POST ", 5) == 0 ||\n strncmp((char *)p, "HEAD ", 5) == 0 ||\n strncmp((char *)p, "PUT ", 4) == 0) {\n SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_HTTP_REQUEST);\n goto err;\n } else if (strncmp((char *)p, "CONNE", 5) == 0) {\n SSLerr(SSL_F_SSL3_GET_RECORD,\n SSL_R_HTTPS_PROXY_REQUEST);\n goto err;\n }\n }\n SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_WRONG_VERSION_NUMBER);\n goto err;\n }\n if (rr[num_recs].length >\n SSL3_BUFFER_get_len(rbuf) - SSL3_RT_HEADER_LENGTH) {\n al = SSL_AD_RECORD_OVERFLOW;\n SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_PACKET_LENGTH_TOO_LONG);\n goto f_err;\n }\n }\n }\n if (rr[num_recs].rec_version == SSL2_VERSION) {\n i = rr[num_recs].length + SSL2_RT_HEADER_LENGTH\n - SSL3_RT_HEADER_LENGTH;\n } else {\n i = rr[num_recs].length;\n }\n if (i > 0) {\n n = ssl3_read_n(s, i, i, 1, 0);\n if (n <= 0)\n return (n);\n }\n RECORD_LAYER_set_rstate(&s->rlayer, SSL_ST_READ_HEADER);\n if(rr[num_recs].rec_version == SSL2_VERSION) {\n rr[num_recs].input =\n &(RECORD_LAYER_get_packet(&s->rlayer)[SSL2_RT_HEADER_LENGTH]);\n } else {\n rr[num_recs].input =\n &(RECORD_LAYER_get_packet(&s->rlayer)[SSL3_RT_HEADER_LENGTH]);\n }\n if (rr[num_recs].length > SSL3_RT_MAX_ENCRYPTED_LENGTH) {\n al = SSL_AD_RECORD_OVERFLOW;\n SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_ENCRYPTED_LENGTH_TOO_LONG);\n goto f_err;\n }\n rr[num_recs].data = rr[num_recs].input;\n rr[num_recs].orig_len = rr[num_recs].length;\n num_recs++;\n RECORD_LAYER_reset_packet_length(&s->rlayer);\n } while (num_recs < max_recs\n && rr[num_recs-1].type == SSL3_RT_APPLICATION_DATA\n && SSL_USE_EXPLICIT_IV(s)\n && s->enc_read_ctx != NULL\n && (EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(s->enc_read_ctx))\n & EVP_CIPH_FLAG_PIPELINE)\n && ssl3_record_app_data_waiting(s));\n if (SSL_USE_ETM(s) && s->read_hash) {\n unsigned char *mac;\n mac_size = EVP_MD_CTX_size(s->read_hash);\n OPENSSL_assert(mac_size <= EVP_MAX_MD_SIZE);\n for (j = 0; j < num_recs; j++) {\n if (rr[j].length < mac_size) {\n al = SSL_AD_DECODE_ERROR;\n SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_LENGTH_TOO_SHORT);\n goto f_err;\n }\n rr[j].length -= mac_size;\n mac = rr[j].data + rr[j].length;\n i = s->method->ssl3_enc->mac(s, &rr[j], md, 0 );\n if (i < 0 || CRYPTO_memcmp(md, mac, (size_t)mac_size) != 0) {\n al = SSL_AD_BAD_RECORD_MAC;\n SSLerr(SSL_F_SSL3_GET_RECORD,\n SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC);\n goto f_err;\n }\n }\n }\n enc_err = s->method->ssl3_enc->enc(s, rr, num_recs, 0);\n if (enc_err == 0) {\n al = SSL_AD_DECRYPTION_FAILED;\n SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_BLOCK_CIPHER_PAD_IS_WRONG);\n goto f_err;\n }\n#ifdef SSL_DEBUG\n printf("dec %d\\n", rr->length);\n {\n unsigned int z;\n for (z = 0; z < rr->length; z++)\n printf("%02X%c", rr->data[z], ((z + 1) % 16) ? \' \' : \'\\n\');\n }\n printf("\\n");\n#endif\n if ((sess != NULL) &&\n (s->enc_read_ctx != NULL) &&\n (EVP_MD_CTX_md(s->read_hash) != NULL) && !SSL_USE_ETM(s)) {\n unsigned char *mac = NULL;\n unsigned char mac_tmp[EVP_MAX_MD_SIZE];\n mac_size = EVP_MD_CTX_size(s->read_hash);\n OPENSSL_assert(mac_size <= EVP_MAX_MD_SIZE);\n for (j=0; j < num_recs; j++) {\n if (rr[j].orig_len < mac_size ||\n (EVP_CIPHER_CTX_mode(s->enc_read_ctx) == EVP_CIPH_CBC_MODE &&\n rr[j].orig_len < mac_size + 1)) {\n al = SSL_AD_DECODE_ERROR;\n SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_LENGTH_TOO_SHORT);\n goto f_err;\n }\n if (EVP_CIPHER_CTX_mode(s->enc_read_ctx) == EVP_CIPH_CBC_MODE) {\n mac = mac_tmp;\n ssl3_cbc_copy_mac(mac_tmp, &rr[j], mac_size);\n rr[j].length -= mac_size;\n } else {\n rr[j].length -= mac_size;\n mac = &rr[j].data[rr[j].length];\n }\n i = s->method->ssl3_enc->mac(s, &rr[j], md, 0 );\n if (i < 0 || mac == NULL\n || CRYPTO_memcmp(md, mac, (size_t)mac_size) != 0)\n enc_err = -1;\n if (rr->length > SSL3_RT_MAX_COMPRESSED_LENGTH + mac_size)\n enc_err = -1;\n }\n }\n if (enc_err < 0) {\n al = SSL_AD_BAD_RECORD_MAC;\n SSLerr(SSL_F_SSL3_GET_RECORD,\n SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC);\n goto f_err;\n }\n for (j = 0; j < num_recs; j++) {\n if (s->expand != NULL) {\n if (rr[j].length > SSL3_RT_MAX_COMPRESSED_LENGTH) {\n al = SSL_AD_RECORD_OVERFLOW;\n SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_COMPRESSED_LENGTH_TOO_LONG);\n goto f_err;\n }\n if (!ssl3_do_uncompress(s, &rr[j])) {\n al = SSL_AD_DECOMPRESSION_FAILURE;\n SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_BAD_DECOMPRESSION);\n goto f_err;\n }\n }\n if (rr[j].length > SSL3_RT_MAX_PLAIN_LENGTH) {\n al = SSL_AD_RECORD_OVERFLOW;\n SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_DATA_LENGTH_TOO_LONG);\n goto f_err;\n }\n rr[j].off = 0;\n if (rr[j].length == 0) {\n curr_empty++;\n empty_record_count++;\n if (empty_record_count > MAX_EMPTY_RECORDS) {\n al = SSL_AD_UNEXPECTED_MESSAGE;\n SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_RECORD_TOO_SMALL);\n goto f_err;\n }\n }\n }\n if (curr_empty == num_recs) {\n num_recs = 0;\n curr_empty = 0;\n goto again;\n }\n RECORD_LAYER_set_numrpipes(&s->rlayer, num_recs);\n return 1;\n f_err:\n ssl3_send_alert(s, SSL3_AL_FATAL, al);\n err:\n return ret;\n}', 'void ssl3_cbc_copy_mac(unsigned char *out,\n const SSL3_RECORD *rec, unsigned md_size)\n{\n#if defined(CBC_MAC_ROTATE_IN_PLACE)\n unsigned char rotated_mac_buf[64 + EVP_MAX_MD_SIZE];\n unsigned char *rotated_mac;\n#else\n unsigned char rotated_mac[EVP_MAX_MD_SIZE];\n#endif\n unsigned mac_end = rec->length;\n unsigned mac_start = mac_end - md_size;\n unsigned scan_start = 0;\n unsigned i, j;\n unsigned div_spoiler;\n unsigned rotate_offset;\n OPENSSL_assert(rec->orig_len >= md_size);\n OPENSSL_assert(md_size <= EVP_MAX_MD_SIZE);\n#if defined(CBC_MAC_ROTATE_IN_PLACE)\n rotated_mac = rotated_mac_buf + ((0 - (size_t)rotated_mac_buf) & 63);\n#endif\n if (rec->orig_len > md_size + 255 + 1)\n scan_start = rec->orig_len - (md_size + 255 + 1);\n div_spoiler = md_size >> 1;\n div_spoiler <<= (sizeof(div_spoiler) - 1) * 8;\n rotate_offset = (div_spoiler + mac_start - scan_start) % md_size;\n memset(rotated_mac, 0, md_size);\n for (i = scan_start, j = 0; i < rec->orig_len; i++) {\n unsigned char mac_started = constant_time_ge_8(i, mac_start);\n unsigned char mac_ended = constant_time_ge_8(i, mac_end);\n unsigned char b = rec->data[i];\n rotated_mac[j++] |= b & mac_started & ~mac_ended;\n j &= constant_time_lt(j, md_size);\n }\n#if defined(CBC_MAC_ROTATE_IN_PLACE)\n j = 0;\n for (i = 0; i < md_size; i++) {\n ((volatile unsigned char *)rotated_mac)[rotate_offset ^ 32];\n out[j++] = rotated_mac[rotate_offset++];\n rotate_offset &= constant_time_lt(rotate_offset, md_size);\n }\n#else\n memset(out, 0, md_size);\n rotate_offset = md_size - rotate_offset;\n rotate_offset &= constant_time_lt(rotate_offset, md_size);\n for (i = 0; i < md_size; i++) {\n for (j = 0; j < md_size; j++)\n out[j] |= rotated_mac[i] & constant_time_eq_8(j, rotate_offset);\n rotate_offset++;\n rotate_offset &= constant_time_lt(rotate_offset, md_size);\n }\n#endif\n}']
|
2,159
| 0
|
https://github.com/libav/libav/blob/be4e8908d2ccc80a0603514b95499cd4380e8f81/libavformat/oma.c/#L252
|
static int decrypt_init(AVFormatContext *s, ID3v2ExtraMeta *em, uint8_t *header)
{
OMAContext *oc = s->priv_data;
ID3v2ExtraMetaGEOB *geob = NULL;
uint8_t *gdata;
oc->encrypted = 1;
av_log(s, AV_LOG_INFO, "File is encrypted\n");
while (em) {
if (!strcmp(em->tag, "GEOB") &&
(geob = em->data) &&
!strcmp(geob->description, "OMG_LSI") ||
!strcmp(geob->description, "OMG_BKLSI")) {
break;
}
em = em->next;
}
if (!em) {
av_log(s, AV_LOG_ERROR, "No encryption header found\n");
return -1;
}
if (geob->datasize < 64) {
av_log(s, AV_LOG_ERROR, "Invalid GEOB data size: %u\n", geob->datasize);
return -1;
}
gdata = geob->data;
if (AV_RB16(gdata) != 1)
av_log(s, AV_LOG_WARNING, "Unknown version in encryption header\n");
oc->k_size = AV_RB16(&gdata[2]);
oc->e_size = AV_RB16(&gdata[4]);
oc->i_size = AV_RB16(&gdata[6]);
oc->s_size = AV_RB16(&gdata[8]);
if (memcmp(&gdata[OMA_ENC_HEADER_SIZE], "KEYRING ", 12)) {
av_log(s, AV_LOG_ERROR, "Invalid encryption header\n");
return -1;
}
oc->rid = AV_RB32(&gdata[OMA_ENC_HEADER_SIZE + 28]);
av_log(s, AV_LOG_DEBUG, "RID: %.8x\n", oc->rid);
memcpy(oc->iv, &header[0x58], 8);
hex_log(s, AV_LOG_DEBUG, "IV", oc->iv, 8);
hex_log(s, AV_LOG_DEBUG, "CBC-MAC", &gdata[OMA_ENC_HEADER_SIZE+oc->k_size+oc->e_size+oc->i_size], 8);
if (s->keylen > 0) {
kset(s, s->key, s->key, s->keylen);
}
if (!memcmp(oc->r_val, (const uint8_t[8]){0}, 8) ||
rprobe(s, gdata, oc->r_val) < 0 &&
nprobe(s, gdata, oc->n_val) < 0) {
int i;
for (i = 0; i < sizeof(leaf_table); i += 2) {
uint8_t buf[16];
AV_WL64(buf, leaf_table[i]);
AV_WL64(&buf[8], leaf_table[i+1]);
kset(s, buf, buf, 16);
if (!rprobe(s, gdata, oc->r_val) || !nprobe(s, gdata, oc->n_val))
break;
}
if (i >= sizeof(leaf_table)) {
av_log(s, AV_LOG_ERROR, "Invalid key\n");
return -1;
}
}
av_des_init(&oc->av_des, oc->m_val, 64, 0);
av_des_crypt(&oc->av_des, oc->e_val, &gdata[OMA_ENC_HEADER_SIZE + 40], 1, NULL, 0);
hex_log(s, AV_LOG_DEBUG, "EK", oc->e_val, 8);
av_des_init(&oc->av_des, oc->e_val, 64, 1);
return 0;
}
|
['static int decrypt_init(AVFormatContext *s, ID3v2ExtraMeta *em, uint8_t *header)\n{\n OMAContext *oc = s->priv_data;\n ID3v2ExtraMetaGEOB *geob = NULL;\n uint8_t *gdata;\n oc->encrypted = 1;\n av_log(s, AV_LOG_INFO, "File is encrypted\\n");\n while (em) {\n if (!strcmp(em->tag, "GEOB") &&\n (geob = em->data) &&\n !strcmp(geob->description, "OMG_LSI") ||\n !strcmp(geob->description, "OMG_BKLSI")) {\n break;\n }\n em = em->next;\n }\n if (!em) {\n av_log(s, AV_LOG_ERROR, "No encryption header found\\n");\n return -1;\n }\n if (geob->datasize < 64) {\n av_log(s, AV_LOG_ERROR, "Invalid GEOB data size: %u\\n", geob->datasize);\n return -1;\n }\n gdata = geob->data;\n if (AV_RB16(gdata) != 1)\n av_log(s, AV_LOG_WARNING, "Unknown version in encryption header\\n");\n oc->k_size = AV_RB16(&gdata[2]);\n oc->e_size = AV_RB16(&gdata[4]);\n oc->i_size = AV_RB16(&gdata[6]);\n oc->s_size = AV_RB16(&gdata[8]);\n if (memcmp(&gdata[OMA_ENC_HEADER_SIZE], "KEYRING ", 12)) {\n av_log(s, AV_LOG_ERROR, "Invalid encryption header\\n");\n return -1;\n }\n oc->rid = AV_RB32(&gdata[OMA_ENC_HEADER_SIZE + 28]);\n av_log(s, AV_LOG_DEBUG, "RID: %.8x\\n", oc->rid);\n memcpy(oc->iv, &header[0x58], 8);\n hex_log(s, AV_LOG_DEBUG, "IV", oc->iv, 8);\n hex_log(s, AV_LOG_DEBUG, "CBC-MAC", &gdata[OMA_ENC_HEADER_SIZE+oc->k_size+oc->e_size+oc->i_size], 8);\n if (s->keylen > 0) {\n kset(s, s->key, s->key, s->keylen);\n }\n if (!memcmp(oc->r_val, (const uint8_t[8]){0}, 8) ||\n rprobe(s, gdata, oc->r_val) < 0 &&\n nprobe(s, gdata, oc->n_val) < 0) {\n int i;\n for (i = 0; i < sizeof(leaf_table); i += 2) {\n uint8_t buf[16];\n AV_WL64(buf, leaf_table[i]);\n AV_WL64(&buf[8], leaf_table[i+1]);\n kset(s, buf, buf, 16);\n if (!rprobe(s, gdata, oc->r_val) || !nprobe(s, gdata, oc->n_val))\n break;\n }\n if (i >= sizeof(leaf_table)) {\n av_log(s, AV_LOG_ERROR, "Invalid key\\n");\n return -1;\n }\n }\n av_des_init(&oc->av_des, oc->m_val, 64, 0);\n av_des_crypt(&oc->av_des, oc->e_val, &gdata[OMA_ENC_HEADER_SIZE + 40], 1, NULL, 0);\n hex_log(s, AV_LOG_DEBUG, "EK", oc->e_val, 8);\n av_des_init(&oc->av_des, oc->e_val, 64, 1);\n return 0;\n}']
|
2,160
| 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 int test_modexp_mont5(void)\n{\n BIGNUM *a = NULL, *p = NULL, *m = NULL, *d = NULL, *e = NULL;\n BIGNUM *b = NULL, *n = NULL, *c = NULL;\n BN_MONT_CTX *mont = NULL;\n int st = 0;\n if (!TEST_ptr(a = BN_new())\n || !TEST_ptr(p = BN_new())\n || !TEST_ptr(m = BN_new())\n || !TEST_ptr(d = BN_new())\n || !TEST_ptr(e = BN_new())\n || !TEST_ptr(b = BN_new())\n || !TEST_ptr(n = BN_new())\n || !TEST_ptr(c = BN_new())\n || !TEST_ptr(mont = BN_MONT_CTX_new()))\n goto err;\n if (!(TEST_true(BN_bntest_rand(m, 1024, 0, 1))\n && TEST_true(BN_bntest_rand(a, 1024, 0, 0))))\n goto err;\n BN_zero(p);\n if (!TEST_true(BN_mod_exp_mont_consttime(d, a, p, m, ctx, NULL)))\n goto err;\n if (!TEST_BN_eq_one(d))\n goto err;\n if (!(TEST_true(BN_hex2bn(&a,\n "7878787878787878787878787878787878787878787878787878787878787878"\n "7878787878787878787878787878787878787878787878787878787878787878"\n "7878787878787878787878787878787878787878787878787878787878787878"\n "7878787878787878787878787878787878787878787878787878787878787878"))\n && TEST_true(BN_hex2bn(&b,\n "095D72C08C097BA488C5E439C655A192EAFB6380073D8C2664668EDDB4060744"\n "E16E57FB4EDB9AE10A0CEFCDC28A894F689A128379DB279D48A2E20849D68593"\n "9B7803BCF46CEBF5C533FB0DD35B080593DE5472E3FE5DB951B8BFF9B4CB8F03"\n "9CC638A5EE8CDD703719F8000E6A9F63BEED5F2FCD52FF293EA05A251BB4AB81"))\n && TEST_true(BN_hex2bn(&n,\n "D78AF684E71DB0C39CFF4E64FB9DB567132CB9C50CC98009FEB820B26F2DED9B"\n "91B9B5E2B83AE0AE4EB4E0523CA726BFBE969B89FD754F674CE99118C3F2D1C5"\n "D81FDC7C54E02B60262B241D53C040E99E45826ECA37A804668E690E1AFC1CA4"\n "2C9A15D84D4954425F0B7642FC0BD9D7B24E2618D2DCC9B729D944BADACFDDAF"))))\n goto err;\n if (!(TEST_true(BN_MONT_CTX_set(mont, n, ctx))\n && TEST_true(BN_mod_mul_montgomery(c, a, b, mont, ctx))\n && TEST_true(BN_mod_mul_montgomery(d, b, a, mont, ctx))\n && TEST_BN_eq(c, d)))\n goto err;\n if (!(TEST_true(parse_bigBN(&n, bn1strings))\n && TEST_true(parse_bigBN(&a, bn2strings))))\n goto err;\n BN_free(b);\n if (!(TEST_ptr(b = BN_dup(a))\n && TEST_true(BN_MONT_CTX_set(mont, n, ctx))\n && TEST_true(BN_mod_mul_montgomery(c, a, a, mont, ctx))\n && TEST_true(BN_mod_mul_montgomery(d, a, b, mont, ctx))\n && TEST_BN_eq(c, d)))\n goto err;\n {\n static const char *ahex[] = {\n "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",\n "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",\n "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",\n "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",\n "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF8FFEADBCFC4DAE7FFF908E92820306B",\n "9544D954000000006C0000000000000000000000000000000000000000000000",\n "00000000000000000000FF030202FFFFF8FFEBDBCFC4DAE7FFF908E92820306B",\n "9544D954000000006C000000FF0302030000000000FFFFFFFFFFFFFFFFFFFFFF",\n "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF01FC00FF02FFFFFFFF",\n "00FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00FCFD",\n "FCFFFFFFFFFF000000000000000000FF0302030000000000FFFFFFFFFFFFFFFF",\n "FF00FCFDFDFF030202FF00000000FFFFFFFFFFFFFFFFFF00FCFDFCFFFFFFFFFF",\n NULL\n };\n static const char *nhex[] = {\n "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",\n "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",\n "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",\n "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",\n "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF8F8F8F8000000",\n "00000010000000006C0000000000000000000000000000000000000000000000",\n "00000000000000000000000000000000000000FFFFFFFFFFFFF8F8F8F8000000",\n "00000010000000006C000000000000000000000000FFFFFFFFFFFFFFFFFFFFFF",\n "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",\n "00FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",\n "FFFFFFFFFFFF000000000000000000000000000000000000FFFFFFFFFFFFFFFF",\n "FFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",\n NULL\n };\n if (!(TEST_true(parse_bigBN(&a, ahex))\n && TEST_true(parse_bigBN(&n, nhex))))\n goto err;\n }\n BN_free(b);\n if (!(TEST_ptr(b = BN_dup(a))\n && TEST_true(BN_MONT_CTX_set(mont, n, ctx))))\n goto err;\n if (!TEST_true(BN_mod_mul_montgomery(c, a, a, mont, ctx))\n || !TEST_true(BN_mod_mul_montgomery(d, a, b, mont, ctx))\n || !TEST_BN_eq(c, d))\n goto err;\n if (!(TEST_true(BN_hex2bn(&a,\n "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"\n "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"\n "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"))\n && TEST_true(BN_hex2bn(&n,\n "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"\n "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"))\n && TEST_true(BN_MONT_CTX_set(mont, n, ctx))\n && TEST_false(BN_mod_mul_montgomery(d, a, a, mont, ctx))))\n goto err;\n if (!(TEST_true(BN_hex2bn(&a,\n "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"\n "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"\n "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"\n "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF2020202020DF"))\n && TEST_true(BN_hex2bn(&b,\n "2020202020202020202020202020202020202020202020202020202020202020"\n "2020202020202020202020202020202020202020202020202020202020202020"\n "20202020202020FF202020202020202020202020202020202020202020202020"\n "2020202020202020202020202020202020202020202020202020202020202020"))\n && TEST_true(BN_hex2bn(&n,\n "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"\n "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"\n "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"\n "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF2020202020FF"))\n && TEST_true(BN_MONT_CTX_set(mont, n, ctx))\n && TEST_true(BN_mod_exp_mont_consttime(c, a, b, n, ctx, mont))\n && TEST_true(BN_mod_exp_mont(d, a, b, n, ctx, mont))\n && TEST_BN_eq(c, d)))\n goto err;\n if (!(TEST_true(BN_hex2bn(&a,\n "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"\n "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"\n "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"\n "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF2020202020DF"))\n && TEST_true(BN_hex2bn(&b,\n "1FA53F26F8811C58BE0357897AA5E165693230BC9DF5F01DFA6A2D59229EC69D"\n "9DE6A89C36E3B6957B22D6FAAD5A3C73AE587B710DBE92E83D3A9A3339A085CB"\n "B58F508CA4F837924BB52CC1698B7FDC2FD74362456A595A5B58E38E38E38E38"\n "E38E38E38E38E38E38E38E38E38E38E38E38E38E38E38E38E38E38E38E38E38E"))\n && TEST_true(BN_hex2bn(&n,\n "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"\n "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"\n "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"\n "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF2020202020DF"))\n && TEST_true(BN_MONT_CTX_set(mont, n, ctx))\n && TEST_true(BN_mod_exp_mont_consttime(c, a, b, n, ctx, mont))))\n goto err;\n BN_zero(d);\n if (!TEST_BN_eq(c, d))\n goto err;\n if (!TEST_true(BN_bntest_rand(p, 1024, 0, 0)))\n goto err;\n BN_zero(a);\n if (!TEST_true(BN_mod_exp_mont_consttime(d, a, p, m, ctx, NULL))\n || !TEST_BN_eq_zero(d))\n goto err;\n if (!(TEST_true(BN_one(a))\n && TEST_true(BN_MONT_CTX_set(mont, m, ctx))))\n goto err;\n if (!TEST_true(BN_from_montgomery(e, a, mont, ctx))\n || !TEST_true(BN_mod_exp_mont_consttime(d, e, p, m, ctx, NULL))\n || !TEST_true(BN_mod_exp_simple(a, e, p, m, ctx))\n || !TEST_BN_eq(a, d))\n goto err;\n if (!(TEST_true(BN_bntest_rand(e, 1024, 0, 0))\n && TEST_true(BN_mod_exp_mont_consttime(d, e, p, m, ctx, NULL))\n && TEST_true(BN_mod_exp_simple(a, e, p, m, ctx))\n && TEST_BN_eq(a, d)))\n goto err;\n st = 1;\n err:\n BN_MONT_CTX_free(mont);\n BN_free(a);\n BN_free(p);\n BN_free(m);\n BN_free(d);\n BN_free(e);\n BN_free(b);\n BN_free(n);\n BN_free(c);\n return st;\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 if (a->neg || BN_ucmp(a, m) >= 0) {\n BIGNUM *reduced = BN_CTX_get(ctx);\n if (reduced == NULL\n || !BN_nnmod(reduced, a, m, ctx)) {\n goto err;\n }\n a = reduced;\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_mont_fixed_top(&tmp, BN_value_one(), mont, ctx))\n goto err;\n if (!bn_to_mont_fixed_top(&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_mul_mont_fixed_top(&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_mul_mont_fixed_top(&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_mul_mont_fixed_top(&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_mul_mont_fixed_top(&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}', '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}', '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 (BN_abs_is_word(n, 1) || BN_is_zero(n)) {\n if (pnoinv != NULL)\n *pnoinv = 1;\n return NULL;\n }\n if (pnoinv != NULL)\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}', '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 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}']
|
2,161
| 0
|
https://github.com/openssl/openssl/blob/e3713c365c2657236439fea00822a43aa396d112/crypto/kdf/tls1_prf.c/#L264
|
static int tls1_prf_alg(const EVP_MD *md,
const unsigned char *sec, size_t slen,
const unsigned char *seed, size_t seed_len,
unsigned char *out, size_t olen)
{
if (EVP_MD_type(md) == NID_md5_sha1) {
size_t i;
unsigned char *tmp;
if (!tls1_prf_P_hash(EVP_md5(), sec, slen/2 + (slen & 1),
seed, seed_len, out, olen))
return 0;
tmp = OPENSSL_malloc(olen);
if (tmp == NULL)
return 0;
if (!tls1_prf_P_hash(EVP_sha1(), sec + slen/2, slen/2 + (slen & 1),
seed, seed_len, tmp, olen)) {
OPENSSL_clear_free(tmp, olen);
return 0;
}
for (i = 0; i < olen; i++)
out[i] ^= tmp[i];
OPENSSL_clear_free(tmp, olen);
return 1;
}
if (!tls1_prf_P_hash(md, sec, slen, seed, seed_len, out, olen))
return 0;
return 1;
}
|
['static int tls1_prf_alg(const EVP_MD *md,\n const unsigned char *sec, size_t slen,\n const unsigned char *seed, size_t seed_len,\n unsigned char *out, size_t olen)\n{\n if (EVP_MD_type(md) == NID_md5_sha1) {\n size_t i;\n unsigned char *tmp;\n if (!tls1_prf_P_hash(EVP_md5(), sec, slen/2 + (slen & 1),\n seed, seed_len, out, olen))\n return 0;\n tmp = OPENSSL_malloc(olen);\n if (tmp == NULL)\n return 0;\n if (!tls1_prf_P_hash(EVP_sha1(), sec + slen/2, slen/2 + (slen & 1),\n seed, seed_len, tmp, olen)) {\n OPENSSL_clear_free(tmp, olen);\n return 0;\n }\n for (i = 0; i < olen; i++)\n out[i] ^= tmp[i];\n OPENSSL_clear_free(tmp, olen);\n return 1;\n }\n if (!tls1_prf_P_hash(md, sec, slen, seed, seed_len, out, olen))\n return 0;\n return 1;\n}', 'int EVP_MD_type(const EVP_MD *md)\n{\n return md->type;\n}', 'const EVP_MD *EVP_md5(void)\n{\n return (&md5_md);\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}', 'const EVP_MD *EVP_sha1(void)\n{\n return (&sha1_md);\n}', 'void CRYPTO_clear_free(void *str, size_t num, const char *file, int line)\n{\n if (str == NULL)\n return;\n if (num)\n OPENSSL_cleanse(str, num);\n CRYPTO_free(str, file, line);\n}', 'void CRYPTO_free(void *str, const char *file, int line)\n{\n INCREMENT(free_count);\n if (free_impl != NULL && free_impl != &CRYPTO_free) {\n free_impl(str, file, line);\n return;\n }\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n if (call_malloc_debug) {\n CRYPTO_mem_debug_free(str, 0, file, line);\n free(str);\n CRYPTO_mem_debug_free(str, 1, file, line);\n } else {\n free(str);\n }\n#else\n free(str);\n#endif\n}']
|
2,162
| 0
|
https://github.com/libav/libav/blob/452a398fd6bdca3f301c5c8af3bc241bc16a777e/libavcodec/mlpdec.c/#L1122
|
static int read_access_unit(AVCodecContext *avctx, void* data, int *data_size,
const uint8_t *buf, int buf_size)
{
MLPDecodeContext *m = avctx->priv_data;
GetBitContext gb;
unsigned int length, substr;
unsigned int substream_start;
unsigned int header_size = 4;
unsigned int substr_header_size = 0;
uint8_t substream_parity_present[MAX_SUBSTREAMS];
uint16_t substream_data_len[MAX_SUBSTREAMS];
uint8_t parity_bits;
if (buf_size < 4)
return 0;
length = (AV_RB16(buf) & 0xfff) * 2;
if (length > buf_size)
return -1;
init_get_bits(&gb, (buf + 4), (length - 4) * 8);
if (show_bits_long(&gb, 31) == (0xf8726fba >> 1)) {
dprintf(m->avctx, "Found major sync.\n");
if (read_major_sync(m, &gb) < 0)
goto error;
header_size += 28;
}
if (!m->params_valid) {
av_log(m->avctx, AV_LOG_WARNING,
"Stream parameters not seen; skipping frame.\n");
*data_size = 0;
return length;
}
substream_start = 0;
for (substr = 0; substr < m->num_substreams; substr++) {
int extraword_present, checkdata_present, end;
extraword_present = get_bits1(&gb);
skip_bits1(&gb);
checkdata_present = get_bits1(&gb);
skip_bits1(&gb);
end = get_bits(&gb, 12) * 2;
substr_header_size += 2;
if (extraword_present) {
skip_bits(&gb, 16);
substr_header_size += 2;
}
if (end + header_size + substr_header_size > length) {
av_log(m->avctx, AV_LOG_ERROR,
"Indicated length of substream %d data goes off end of "
"packet.\n", substr);
end = length - header_size - substr_header_size;
}
if (end < substream_start) {
av_log(avctx, AV_LOG_ERROR,
"Indicated end offset of substream %d data "
"is smaller than calculated start offset.\n",
substr);
goto error;
}
if (substr > m->max_decoded_substream)
continue;
substream_parity_present[substr] = checkdata_present;
substream_data_len[substr] = end - substream_start;
substream_start = end;
}
parity_bits = calculate_parity(buf, 4);
parity_bits ^= calculate_parity(buf + header_size, substr_header_size);
if ((((parity_bits >> 4) ^ parity_bits) & 0xF) != 0xF) {
av_log(avctx, AV_LOG_ERROR, "Parity check failed.\n");
goto error;
}
buf += header_size + substr_header_size;
for (substr = 0; substr <= m->max_decoded_substream; substr++) {
SubStream *s = &m->substream[substr];
init_get_bits(&gb, buf, substream_data_len[substr] * 8);
s->blockpos = 0;
do {
if (get_bits1(&gb)) {
if (get_bits1(&gb)) {
if (read_restart_header(m, &gb, buf, substr) < 0)
goto next_substr;
s->restart_seen = 1;
}
if (!s->restart_seen) {
av_log(m->avctx, AV_LOG_ERROR,
"No restart header present in substream %d.\n",
substr);
goto next_substr;
}
if (read_decoding_params(m, &gb, substr) < 0)
goto next_substr;
}
if (!s->restart_seen) {
av_log(m->avctx, AV_LOG_ERROR,
"No restart header present in substream %d.\n",
substr);
goto next_substr;
}
if (read_block_data(m, &gb, substr) < 0)
return -1;
} while ((get_bits_count(&gb) < substream_data_len[substr] * 8)
&& get_bits1(&gb) == 0);
skip_bits(&gb, (-get_bits_count(&gb)) & 15);
if (substream_data_len[substr] * 8 - get_bits_count(&gb) >= 32 &&
(show_bits_long(&gb, 32) == 0xd234d234 ||
show_bits_long(&gb, 20) == 0xd234e)) {
skip_bits(&gb, 18);
if (substr == m->max_decoded_substream)
av_log(m->avctx, AV_LOG_INFO, "End of stream indicated.\n");
if (get_bits1(&gb)) {
int shorten_by = get_bits(&gb, 13);
shorten_by = FFMIN(shorten_by, s->blockpos);
s->blockpos -= shorten_by;
} else
skip_bits(&gb, 13);
}
if (substream_data_len[substr] * 8 - get_bits_count(&gb) >= 16 &&
substream_parity_present[substr]) {
uint8_t parity, checksum;
parity = calculate_parity(buf, substream_data_len[substr] - 2);
if ((parity ^ get_bits(&gb, 8)) != 0xa9)
av_log(m->avctx, AV_LOG_ERROR,
"Substream %d parity check failed.\n", substr);
checksum = mlp_checksum8(buf, substream_data_len[substr] - 2);
if (checksum != get_bits(&gb, 8))
av_log(m->avctx, AV_LOG_ERROR, "Substream %d checksum failed.\n",
substr);
}
if (substream_data_len[substr] * 8 != get_bits_count(&gb)) {
av_log(m->avctx, AV_LOG_ERROR, "substream %d length mismatch\n",
substr);
return -1;
}
next_substr:
buf += substream_data_len[substr];
}
rematrix_channels(m, m->max_decoded_substream);
if (output_data(m, m->max_decoded_substream, data, data_size) < 0)
return -1;
return length;
error:
m->params_valid = 0;
return -1;
}
|
['static int read_access_unit(AVCodecContext *avctx, void* data, int *data_size,\n const uint8_t *buf, int buf_size)\n{\n MLPDecodeContext *m = avctx->priv_data;\n GetBitContext gb;\n unsigned int length, substr;\n unsigned int substream_start;\n unsigned int header_size = 4;\n unsigned int substr_header_size = 0;\n uint8_t substream_parity_present[MAX_SUBSTREAMS];\n uint16_t substream_data_len[MAX_SUBSTREAMS];\n uint8_t parity_bits;\n if (buf_size < 4)\n return 0;\n length = (AV_RB16(buf) & 0xfff) * 2;\n if (length > buf_size)\n return -1;\n init_get_bits(&gb, (buf + 4), (length - 4) * 8);\n if (show_bits_long(&gb, 31) == (0xf8726fba >> 1)) {\n dprintf(m->avctx, "Found major sync.\\n");\n if (read_major_sync(m, &gb) < 0)\n goto error;\n header_size += 28;\n }\n if (!m->params_valid) {\n av_log(m->avctx, AV_LOG_WARNING,\n "Stream parameters not seen; skipping frame.\\n");\n *data_size = 0;\n return length;\n }\n substream_start = 0;\n for (substr = 0; substr < m->num_substreams; substr++) {\n int extraword_present, checkdata_present, end;\n extraword_present = get_bits1(&gb);\n skip_bits1(&gb);\n checkdata_present = get_bits1(&gb);\n skip_bits1(&gb);\n end = get_bits(&gb, 12) * 2;\n substr_header_size += 2;\n if (extraword_present) {\n skip_bits(&gb, 16);\n substr_header_size += 2;\n }\n if (end + header_size + substr_header_size > length) {\n av_log(m->avctx, AV_LOG_ERROR,\n "Indicated length of substream %d data goes off end of "\n "packet.\\n", substr);\n end = length - header_size - substr_header_size;\n }\n if (end < substream_start) {\n av_log(avctx, AV_LOG_ERROR,\n "Indicated end offset of substream %d data "\n "is smaller than calculated start offset.\\n",\n substr);\n goto error;\n }\n if (substr > m->max_decoded_substream)\n continue;\n substream_parity_present[substr] = checkdata_present;\n substream_data_len[substr] = end - substream_start;\n substream_start = end;\n }\n parity_bits = calculate_parity(buf, 4);\n parity_bits ^= calculate_parity(buf + header_size, substr_header_size);\n if ((((parity_bits >> 4) ^ parity_bits) & 0xF) != 0xF) {\n av_log(avctx, AV_LOG_ERROR, "Parity check failed.\\n");\n goto error;\n }\n buf += header_size + substr_header_size;\n for (substr = 0; substr <= m->max_decoded_substream; substr++) {\n SubStream *s = &m->substream[substr];\n init_get_bits(&gb, buf, substream_data_len[substr] * 8);\n s->blockpos = 0;\n do {\n if (get_bits1(&gb)) {\n if (get_bits1(&gb)) {\n if (read_restart_header(m, &gb, buf, substr) < 0)\n goto next_substr;\n s->restart_seen = 1;\n }\n if (!s->restart_seen) {\n av_log(m->avctx, AV_LOG_ERROR,\n "No restart header present in substream %d.\\n",\n substr);\n goto next_substr;\n }\n if (read_decoding_params(m, &gb, substr) < 0)\n goto next_substr;\n }\n if (!s->restart_seen) {\n av_log(m->avctx, AV_LOG_ERROR,\n "No restart header present in substream %d.\\n",\n substr);\n goto next_substr;\n }\n if (read_block_data(m, &gb, substr) < 0)\n return -1;\n } while ((get_bits_count(&gb) < substream_data_len[substr] * 8)\n && get_bits1(&gb) == 0);\n skip_bits(&gb, (-get_bits_count(&gb)) & 15);\n if (substream_data_len[substr] * 8 - get_bits_count(&gb) >= 32 &&\n (show_bits_long(&gb, 32) == 0xd234d234 ||\n show_bits_long(&gb, 20) == 0xd234e)) {\n skip_bits(&gb, 18);\n if (substr == m->max_decoded_substream)\n av_log(m->avctx, AV_LOG_INFO, "End of stream indicated.\\n");\n if (get_bits1(&gb)) {\n int shorten_by = get_bits(&gb, 13);\n shorten_by = FFMIN(shorten_by, s->blockpos);\n s->blockpos -= shorten_by;\n } else\n skip_bits(&gb, 13);\n }\n if (substream_data_len[substr] * 8 - get_bits_count(&gb) >= 16 &&\n substream_parity_present[substr]) {\n uint8_t parity, checksum;\n parity = calculate_parity(buf, substream_data_len[substr] - 2);\n if ((parity ^ get_bits(&gb, 8)) != 0xa9)\n av_log(m->avctx, AV_LOG_ERROR,\n "Substream %d parity check failed.\\n", substr);\n checksum = mlp_checksum8(buf, substream_data_len[substr] - 2);\n if (checksum != get_bits(&gb, 8))\n av_log(m->avctx, AV_LOG_ERROR, "Substream %d checksum failed.\\n",\n substr);\n }\n if (substream_data_len[substr] * 8 != get_bits_count(&gb)) {\n av_log(m->avctx, AV_LOG_ERROR, "substream %d length mismatch\\n",\n substr);\n return -1;\n }\nnext_substr:\n buf += substream_data_len[substr];\n }\n rematrix_channels(m, m->max_decoded_substream);\n if (output_data(m, m->max_decoded_substream, data, data_size) < 0)\n return -1;\n return length;\nerror:\n m->params_valid = 0;\n return -1;\n}']
|
2,163
| 0
|
https://github.com/openssl/openssl/blob/f9df0a7775f483c175cda5832360cccd1db6943a/crypto/bn/bn_ctx.c/#L273
|
static unsigned int BN_STACK_pop(BN_STACK *st)
{
return st->indexes[--(st->depth)];
}
|
['BIGNUM *SRP_Calc_B(const BIGNUM *b, const BIGNUM *N, const BIGNUM *g,\n const BIGNUM *v)\n{\n BIGNUM *kv = NULL, *gb = NULL;\n BIGNUM *B = NULL, *k = NULL;\n BN_CTX *bn_ctx;\n if (b == NULL || N == NULL || g == NULL || v == NULL ||\n (bn_ctx = BN_CTX_new()) == NULL)\n return NULL;\n if ((kv = BN_new()) == NULL ||\n (gb = BN_new()) == NULL || (B = BN_new()) == NULL)\n goto err;\n if (!BN_mod_exp(gb, g, b, N, bn_ctx)\n || (k = srp_Calc_k(N, g)) == NULL\n || !BN_mod_mul(kv, v, k, N, bn_ctx)\n || !BN_mod_add(B, gb, kv, N, bn_ctx)) {\n BN_free(B);\n B = NULL;\n }\n err:\n BN_CTX_free(bn_ctx);\n BN_clear_free(kv);\n BN_clear_free(gb);\n BN_free(k);\n return B;\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_recp(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 *aa;\n BIGNUM *val[TABLE_SIZE];\n BN_RECP_CTX recp;\n if (BN_get_flags(p, BN_FLG_CONSTTIME) != 0) {\n BNerr(BN_F_BN_MOD_EXP_RECP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);\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(r);\n } else {\n ret = BN_one(r);\n }\n return ret;\n }\n BN_CTX_start(ctx);\n aa = BN_CTX_get(ctx);\n val[0] = BN_CTX_get(ctx);\n if (val[0] == NULL)\n goto err;\n BN_RECP_CTX_init(&recp);\n if (m->neg) {\n if (!BN_copy(aa, m))\n goto err;\n aa->neg = 0;\n if (BN_RECP_CTX_set(&recp, aa, ctx) <= 0)\n goto err;\n } else {\n if (BN_RECP_CTX_set(&recp, m, ctx) <= 0)\n goto err;\n }\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_reciprocal(aa, val[0], val[0], &recp, 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_reciprocal(val[i], val[i - 1], aa, &recp, 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_reciprocal(r, r, r, &recp, 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_reciprocal(r, r, r, &recp, ctx))\n goto err;\n }\n if (!BN_mod_mul_reciprocal(r, r, val[wvalue >> 1], &recp, 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_RECP_CTX_free(&recp);\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.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}', '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}']
|
2,164
| 0
|
https://github.com/openssl/openssl/blob/507db4c5313288d55eeb8434b0111201ba363b28/test/evp_test.c/#L716
|
static int digest_test_run(struct evp_test *t)
{
struct digest_data *mdata = t->data;
size_t i;
const char *err = "INTERNAL_ERROR";
EVP_MD_CTX *mctx;
unsigned char md[EVP_MAX_MD_SIZE];
unsigned int md_len;
mctx = EVP_MD_CTX_new();
if (!mctx)
goto err;
err = "DIGESTINIT_ERROR";
if (!EVP_DigestInit_ex(mctx, mdata->digest, NULL))
goto err;
err = "DIGESTUPDATE_ERROR";
for (i = 0; i < mdata->nrpt; i++) {
if (!EVP_DigestUpdate(mctx, mdata->input, mdata->input_len))
goto err;
}
err = "DIGESTFINAL_ERROR";
if (!EVP_DigestFinal(mctx, md, &md_len))
goto err;
err = "DIGEST_LENGTH_MISMATCH";
if (md_len != mdata->output_len)
goto err;
err = "DIGEST_MISMATCH";
if (check_output(t, mdata->output, md, md_len))
goto err;
err = NULL;
err:
EVP_MD_CTX_free(mctx);
t->err = err;
return 1;
}
|
['static int digest_test_run(struct evp_test *t)\n{\n struct digest_data *mdata = t->data;\n size_t i;\n const char *err = "INTERNAL_ERROR";\n EVP_MD_CTX *mctx;\n unsigned char md[EVP_MAX_MD_SIZE];\n unsigned int md_len;\n mctx = EVP_MD_CTX_new();\n if (!mctx)\n goto err;\n err = "DIGESTINIT_ERROR";\n if (!EVP_DigestInit_ex(mctx, mdata->digest, NULL))\n goto err;\n err = "DIGESTUPDATE_ERROR";\n for (i = 0; i < mdata->nrpt; i++) {\n if (!EVP_DigestUpdate(mctx, mdata->input, mdata->input_len))\n goto err;\n }\n err = "DIGESTFINAL_ERROR";\n if (!EVP_DigestFinal(mctx, md, &md_len))\n goto err;\n err = "DIGEST_LENGTH_MISMATCH";\n if (md_len != mdata->output_len)\n goto err;\n err = "DIGEST_MISMATCH";\n if (check_output(t, mdata->output, md, md_len))\n goto err;\n err = NULL;\n err:\n EVP_MD_CTX_free(mctx);\n t->err = err;\n return 1;\n}', 'EVP_MD_CTX *EVP_MD_CTX_new(void)\n{\n return OPENSSL_zalloc(sizeof(EVP_MD_CTX));\n}', 'void *CRYPTO_zalloc(int 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(int num, const char *file, int line)\n{\n void *ret = NULL;\n if (num <= 0)\n return NULL;\n if (allow_customize)\n allow_customize = 0;\n if (malloc_debug_func != NULL) {\n if (allow_customize_debug)\n allow_customize_debug = 0;\n malloc_debug_func(NULL, num, file, line, 0);\n }\n ret = malloc_ex_func(num, file, line);\n#ifdef LEVITTE_DEBUG_MEM\n fprintf(stderr, "LEVITTE_DEBUG_MEM: > 0x%p (%d)\\n", ret, num);\n#endif\n if (malloc_debug_func != NULL)\n malloc_debug_func(ret, num, file, line, 1);\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}']
|
2,165
| 0
|
https://github.com/libav/libav/blob/490a022d86ef1c506a79744c5a95368af356fc69/libavformat/mpc8.c/#L149
|
static void mpc8_parse_seektable(AVFormatContext *s, int64_t off)
{
MPCContext *c = s->priv_data;
int tag;
int64_t size, pos, ppos[2];
uint8_t *buf;
int i, t, seekd;
GetBitContext gb;
avio_seek(s->pb, off, SEEK_SET);
mpc8_get_chunk_header(s->pb, &tag, &size);
if(tag != TAG_SEEKTABLE){
av_log(s, AV_LOG_ERROR, "No seek table at given position\n");
return;
}
if(!(buf = av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE)))
return;
avio_read(s->pb, buf, size);
init_get_bits(&gb, buf, size * 8);
size = gb_get_v(&gb);
if(size > UINT_MAX/4 || size > c->samples/1152){
av_log(s, AV_LOG_ERROR, "Seek table is too big\n");
return;
}
seekd = get_bits(&gb, 4);
for(i = 0; i < 2; i++){
pos = gb_get_v(&gb) + c->header_pos;
ppos[1 - i] = pos;
av_add_index_entry(s->streams[0], pos, i, 0, 0, AVINDEX_KEYFRAME);
}
for(; i < size; i++){
t = get_unary(&gb, 1, 33) << 12;
t += get_bits(&gb, 12);
if(t & 1)
t = -(t & ~1);
pos = (t >> 1) + ppos[0]*2 - ppos[1];
av_add_index_entry(s->streams[0], pos, i << seekd, 0, 0, AVINDEX_KEYFRAME);
ppos[1] = ppos[0];
ppos[0] = pos;
}
av_free(buf);
}
|
['static void mpc8_parse_seektable(AVFormatContext *s, int64_t off)\n{\n MPCContext *c = s->priv_data;\n int tag;\n int64_t size, pos, ppos[2];\n uint8_t *buf;\n int i, t, seekd;\n GetBitContext gb;\n avio_seek(s->pb, off, SEEK_SET);\n mpc8_get_chunk_header(s->pb, &tag, &size);\n if(tag != TAG_SEEKTABLE){\n av_log(s, AV_LOG_ERROR, "No seek table at given position\\n");\n return;\n }\n if(!(buf = av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE)))\n return;\n avio_read(s->pb, buf, size);\n init_get_bits(&gb, buf, size * 8);\n size = gb_get_v(&gb);\n if(size > UINT_MAX/4 || size > c->samples/1152){\n av_log(s, AV_LOG_ERROR, "Seek table is too big\\n");\n return;\n }\n seekd = get_bits(&gb, 4);\n for(i = 0; i < 2; i++){\n pos = gb_get_v(&gb) + c->header_pos;\n ppos[1 - i] = pos;\n av_add_index_entry(s->streams[0], pos, i, 0, 0, AVINDEX_KEYFRAME);\n }\n for(; i < size; i++){\n t = get_unary(&gb, 1, 33) << 12;\n t += get_bits(&gb, 12);\n if(t & 1)\n t = -(t & ~1);\n pos = (t >> 1) + ppos[0]*2 - ppos[1];\n av_add_index_entry(s->streams[0], pos, i << seekd, 0, 0, AVINDEX_KEYFRAME);\n ppos[1] = ppos[0];\n ppos[0] = pos;\n }\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-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}', '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 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 int64_t gb_get_v(GetBitContext *gb)\n{\n int64_t v = 0;\n int bits = 0;\n while(get_bits1(gb) && bits < 64-7){\n v <<= 7;\n v |= get_bits(gb, 7);\n bits += 7;\n }\n v <<= 7;\n v |= get_bits(gb, 7);\n return v;\n}', 'static inline unsigned int get_bits1(GetBitContext *s){\n#ifdef ALT_BITSTREAM_READER\n unsigned int index = s->index;\n uint8_t result = s->buffer[index>>3];\n#ifdef ALT_BITSTREAM_READER_LE\n result >>= index & 7;\n result &= 1;\n#else\n result <<= index & 7;\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}', '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 av_bswap32(uint32_t x)\n{\n x= ((x<<8)&0xFF00FF00) | ((x>>8)&0x00FF00FF);\n x= (x>>16) | (x<<16);\n return x;\n}']
|
2,166
| 0
|
https://github.com/libav/libav/blob/f8f7ad758d0e1f36915467567f4d75541d98c12f/libavcodec/vp3.c/#L2001
|
static int vp3_decode_frame(AVCodecContext *avctx,
void *data, int *got_frame,
AVPacket *avpkt)
{
AVFrame *frame = data;
const uint8_t *buf = avpkt->data;
int buf_size = avpkt->size;
Vp3DecodeContext *s = avctx->priv_data;
GetBitContext gb;
int i, ret;
init_get_bits(&gb, buf, buf_size * 8);
if (s->theora && get_bits1(&gb)) {
av_log(avctx, AV_LOG_ERROR,
"Header packet passed to frame decoder, skipping\n");
return -1;
}
s->keyframe = !get_bits1(&gb);
if (!s->theora)
skip_bits(&gb, 1);
for (i = 0; i < 3; i++)
s->last_qps[i] = s->qps[i];
s->nqps = 0;
do {
s->qps[s->nqps++] = get_bits(&gb, 6);
} while (s->theora >= 0x030200 && s->nqps < 3 && get_bits1(&gb));
for (i = s->nqps; i < 3; i++)
s->qps[i] = -1;
if (s->avctx->debug & FF_DEBUG_PICT_INFO)
av_log(s->avctx, AV_LOG_INFO, " VP3 %sframe #%d: Q index = %d\n",
s->keyframe ? "key" : "", avctx->frame_number + 1, s->qps[0]);
s->skip_loop_filter = !s->filter_limit_values[s->qps[0]] ||
avctx->skip_loop_filter >= (s->keyframe ? AVDISCARD_ALL
: AVDISCARD_NONKEY);
if (s->qps[0] != s->last_qps[0])
init_loop_filter(s);
for (i = 0; i < s->nqps; i++)
if (s->qps[i] != s->last_qps[i] || s->qps[0] != s->last_qps[0])
init_dequantizer(s, i);
if (avctx->skip_frame >= AVDISCARD_NONKEY && !s->keyframe)
return buf_size;
s->current_frame.f->pict_type = s->keyframe ? AV_PICTURE_TYPE_I
: AV_PICTURE_TYPE_P;
if (ff_thread_get_buffer(avctx, &s->current_frame, AV_GET_BUFFER_FLAG_REF) < 0) {
av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
goto error;
}
if (!s->edge_emu_buffer)
s->edge_emu_buffer = av_malloc(9 * FFABS(s->current_frame.f->linesize[0]));
if (s->keyframe) {
if (!s->theora) {
skip_bits(&gb, 4);
skip_bits(&gb, 4);
if (s->version) {
s->version = get_bits(&gb, 5);
if (avctx->frame_number == 0)
av_log(s->avctx, AV_LOG_DEBUG,
"VP version: %d\n", s->version);
}
}
if (s->version || s->theora) {
if (get_bits1(&gb))
av_log(s->avctx, AV_LOG_ERROR,
"Warning, unsupported keyframe coding type?!\n");
skip_bits(&gb, 2);
}
} else {
if (!s->golden_frame.f->data[0]) {
av_log(s->avctx, AV_LOG_WARNING,
"vp3: first frame not a keyframe\n");
s->golden_frame.f->pict_type = AV_PICTURE_TYPE_I;
if (ff_thread_get_buffer(avctx, &s->golden_frame,
AV_GET_BUFFER_FLAG_REF) < 0) {
av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
goto error;
}
ff_thread_release_buffer(avctx, &s->last_frame);
if ((ret = ff_thread_ref_frame(&s->last_frame,
&s->golden_frame)) < 0)
goto error;
ff_thread_report_progress(&s->last_frame, INT_MAX, 0);
}
}
memset(s->all_fragments, 0, s->fragment_count * sizeof(Vp3Fragment));
ff_thread_finish_setup(avctx);
if (unpack_superblocks(s, &gb)) {
av_log(s->avctx, AV_LOG_ERROR, "error in unpack_superblocks\n");
goto error;
}
if (unpack_modes(s, &gb)) {
av_log(s->avctx, AV_LOG_ERROR, "error in unpack_modes\n");
goto error;
}
if (unpack_vectors(s, &gb)) {
av_log(s->avctx, AV_LOG_ERROR, "error in unpack_vectors\n");
goto error;
}
if (unpack_block_qpis(s, &gb)) {
av_log(s->avctx, AV_LOG_ERROR, "error in unpack_block_qpis\n");
goto error;
}
if (unpack_dct_coeffs(s, &gb)) {
av_log(s->avctx, AV_LOG_ERROR, "error in unpack_dct_coeffs\n");
goto error;
}
for (i = 0; i < 3; i++) {
int height = s->height >> (i && s->chroma_y_shift);
if (s->flipped_image)
s->data_offset[i] = 0;
else
s->data_offset[i] = (height - 1) * s->current_frame.f->linesize[i];
}
s->last_slice_end = 0;
for (i = 0; i < s->c_superblock_height; i++)
render_slice(s, i);
for (i = 0; i < 3; i++) {
int row = (s->height >> (3 + (i && s->chroma_y_shift))) - 1;
apply_loop_filter(s, i, row, row + 1);
}
vp3_draw_horiz_band(s, s->height);
if ((ret = av_frame_ref(data, s->current_frame.f)) < 0)
return ret;
frame->crop_left = s->offset_x;
frame->crop_right = avctx->coded_width - avctx->width - s->offset_x;
frame->crop_top = s->offset_y;
frame->crop_bottom = avctx->coded_height - avctx->height - s->offset_y;
*got_frame = 1;
if (!HAVE_THREADS || !(s->avctx->active_thread_type & FF_THREAD_FRAME)) {
ret = update_frames(avctx);
if (ret < 0)
return ret;
}
return buf_size;
error:
ff_thread_report_progress(&s->current_frame, INT_MAX, 0);
if (!HAVE_THREADS || !(s->avctx->active_thread_type & FF_THREAD_FRAME))
av_frame_unref(s->current_frame.f);
return -1;
}
|
['static int vp3_decode_frame(AVCodecContext *avctx,\n void *data, int *got_frame,\n AVPacket *avpkt)\n{\n AVFrame *frame = data;\n const uint8_t *buf = avpkt->data;\n int buf_size = avpkt->size;\n Vp3DecodeContext *s = avctx->priv_data;\n GetBitContext gb;\n int i, ret;\n init_get_bits(&gb, buf, buf_size * 8);\n if (s->theora && get_bits1(&gb)) {\n av_log(avctx, AV_LOG_ERROR,\n "Header packet passed to frame decoder, skipping\\n");\n return -1;\n }\n s->keyframe = !get_bits1(&gb);\n if (!s->theora)\n skip_bits(&gb, 1);\n for (i = 0; i < 3; i++)\n s->last_qps[i] = s->qps[i];\n s->nqps = 0;\n do {\n s->qps[s->nqps++] = get_bits(&gb, 6);\n } while (s->theora >= 0x030200 && s->nqps < 3 && get_bits1(&gb));\n for (i = s->nqps; i < 3; i++)\n s->qps[i] = -1;\n if (s->avctx->debug & FF_DEBUG_PICT_INFO)\n av_log(s->avctx, AV_LOG_INFO, " VP3 %sframe #%d: Q index = %d\\n",\n s->keyframe ? "key" : "", avctx->frame_number + 1, s->qps[0]);\n s->skip_loop_filter = !s->filter_limit_values[s->qps[0]] ||\n avctx->skip_loop_filter >= (s->keyframe ? AVDISCARD_ALL\n : AVDISCARD_NONKEY);\n if (s->qps[0] != s->last_qps[0])\n init_loop_filter(s);\n for (i = 0; i < s->nqps; i++)\n if (s->qps[i] != s->last_qps[i] || s->qps[0] != s->last_qps[0])\n init_dequantizer(s, i);\n if (avctx->skip_frame >= AVDISCARD_NONKEY && !s->keyframe)\n return buf_size;\n s->current_frame.f->pict_type = s->keyframe ? AV_PICTURE_TYPE_I\n : AV_PICTURE_TYPE_P;\n if (ff_thread_get_buffer(avctx, &s->current_frame, AV_GET_BUFFER_FLAG_REF) < 0) {\n av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\\n");\n goto error;\n }\n if (!s->edge_emu_buffer)\n s->edge_emu_buffer = av_malloc(9 * FFABS(s->current_frame.f->linesize[0]));\n if (s->keyframe) {\n if (!s->theora) {\n skip_bits(&gb, 4);\n skip_bits(&gb, 4);\n if (s->version) {\n s->version = get_bits(&gb, 5);\n if (avctx->frame_number == 0)\n av_log(s->avctx, AV_LOG_DEBUG,\n "VP version: %d\\n", s->version);\n }\n }\n if (s->version || s->theora) {\n if (get_bits1(&gb))\n av_log(s->avctx, AV_LOG_ERROR,\n "Warning, unsupported keyframe coding type?!\\n");\n skip_bits(&gb, 2);\n }\n } else {\n if (!s->golden_frame.f->data[0]) {\n av_log(s->avctx, AV_LOG_WARNING,\n "vp3: first frame not a keyframe\\n");\n s->golden_frame.f->pict_type = AV_PICTURE_TYPE_I;\n if (ff_thread_get_buffer(avctx, &s->golden_frame,\n AV_GET_BUFFER_FLAG_REF) < 0) {\n av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\\n");\n goto error;\n }\n ff_thread_release_buffer(avctx, &s->last_frame);\n if ((ret = ff_thread_ref_frame(&s->last_frame,\n &s->golden_frame)) < 0)\n goto error;\n ff_thread_report_progress(&s->last_frame, INT_MAX, 0);\n }\n }\n memset(s->all_fragments, 0, s->fragment_count * sizeof(Vp3Fragment));\n ff_thread_finish_setup(avctx);\n if (unpack_superblocks(s, &gb)) {\n av_log(s->avctx, AV_LOG_ERROR, "error in unpack_superblocks\\n");\n goto error;\n }\n if (unpack_modes(s, &gb)) {\n av_log(s->avctx, AV_LOG_ERROR, "error in unpack_modes\\n");\n goto error;\n }\n if (unpack_vectors(s, &gb)) {\n av_log(s->avctx, AV_LOG_ERROR, "error in unpack_vectors\\n");\n goto error;\n }\n if (unpack_block_qpis(s, &gb)) {\n av_log(s->avctx, AV_LOG_ERROR, "error in unpack_block_qpis\\n");\n goto error;\n }\n if (unpack_dct_coeffs(s, &gb)) {\n av_log(s->avctx, AV_LOG_ERROR, "error in unpack_dct_coeffs\\n");\n goto error;\n }\n for (i = 0; i < 3; i++) {\n int height = s->height >> (i && s->chroma_y_shift);\n if (s->flipped_image)\n s->data_offset[i] = 0;\n else\n s->data_offset[i] = (height - 1) * s->current_frame.f->linesize[i];\n }\n s->last_slice_end = 0;\n for (i = 0; i < s->c_superblock_height; i++)\n render_slice(s, i);\n for (i = 0; i < 3; i++) {\n int row = (s->height >> (3 + (i && s->chroma_y_shift))) - 1;\n apply_loop_filter(s, i, row, row + 1);\n }\n vp3_draw_horiz_band(s, s->height);\n if ((ret = av_frame_ref(data, s->current_frame.f)) < 0)\n return ret;\n frame->crop_left = s->offset_x;\n frame->crop_right = avctx->coded_width - avctx->width - s->offset_x;\n frame->crop_top = s->offset_y;\n frame->crop_bottom = avctx->coded_height - avctx->height - s->offset_y;\n *got_frame = 1;\n if (!HAVE_THREADS || !(s->avctx->active_thread_type & FF_THREAD_FRAME)) {\n ret = update_frames(avctx);\n if (ret < 0)\n return ret;\n }\n return buf_size;\nerror:\n ff_thread_report_progress(&s->current_frame, INT_MAX, 0);\n if (!HAVE_THREADS || !(s->avctx->active_thread_type & FF_THREAD_FRAME))\n av_frame_unref(s->current_frame.f);\n return -1;\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 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}']
|
2,167
| 0
|
https://github.com/openssl/openssl/blob/2d5d70b15559f9813054ddb11b30b816daf62ebe/ssl/ssl_ciph.c/#L1282
|
static int ssl_cipher_process_rulestr(const char *rule_str,
CIPHER_ORDER **head_p,
CIPHER_ORDER **tail_p,
const SSL_CIPHER **ca_list, CERT *c)
{
unsigned long alg_mkey, alg_auth, alg_enc, alg_mac, alg_ssl,
algo_strength;
const char *l, *buf;
int j, multi, found, rule, retval, ok, buflen;
unsigned long cipher_id = 0;
char ch;
retval = 1;
l = rule_str;
for (;;) {
ch = *l;
if (ch == '\0')
break;
if (ch == '-') {
rule = CIPHER_DEL;
l++;
} else if (ch == '+') {
rule = CIPHER_ORD;
l++;
} else if (ch == '!') {
rule = CIPHER_KILL;
l++;
} else if (ch == '@') {
rule = CIPHER_SPECIAL;
l++;
} else {
rule = CIPHER_ADD;
}
if (ITEM_SEP(ch)) {
l++;
continue;
}
alg_mkey = 0;
alg_auth = 0;
alg_enc = 0;
alg_mac = 0;
alg_ssl = 0;
algo_strength = 0;
for (;;) {
ch = *l;
buf = l;
buflen = 0;
#ifndef CHARSET_EBCDIC
while (((ch >= 'A') && (ch <= 'Z')) ||
((ch >= '0') && (ch <= '9')) ||
((ch >= 'a') && (ch <= 'z')) ||
(ch == '-') || (ch == '.') || (ch == '='))
#else
while (isalnum(ch) || (ch == '-') || (ch == '.') || (ch == '='))
#endif
{
ch = *(++l);
buflen++;
}
if (buflen == 0) {
SSLerr(SSL_F_SSL_CIPHER_PROCESS_RULESTR,
SSL_R_INVALID_COMMAND);
retval = found = 0;
l++;
break;
}
if (rule == CIPHER_SPECIAL) {
found = 0;
break;
}
if (ch == '+') {
multi = 1;
l++;
} else
multi = 0;
j = found = 0;
cipher_id = 0;
while (ca_list[j]) {
if (strncmp(buf, ca_list[j]->name, buflen) == 0
&& (ca_list[j]->name[buflen] == '\0')) {
found = 1;
break;
} else
j++;
}
if (!found)
break;
if (ca_list[j]->algorithm_mkey) {
if (alg_mkey) {
alg_mkey &= ca_list[j]->algorithm_mkey;
if (!alg_mkey) {
found = 0;
break;
}
} else
alg_mkey = ca_list[j]->algorithm_mkey;
}
if (ca_list[j]->algorithm_auth) {
if (alg_auth) {
alg_auth &= ca_list[j]->algorithm_auth;
if (!alg_auth) {
found = 0;
break;
}
} else
alg_auth = ca_list[j]->algorithm_auth;
}
if (ca_list[j]->algorithm_enc) {
if (alg_enc) {
alg_enc &= ca_list[j]->algorithm_enc;
if (!alg_enc) {
found = 0;
break;
}
} else
alg_enc = ca_list[j]->algorithm_enc;
}
if (ca_list[j]->algorithm_mac) {
if (alg_mac) {
alg_mac &= ca_list[j]->algorithm_mac;
if (!alg_mac) {
found = 0;
break;
}
} else
alg_mac = ca_list[j]->algorithm_mac;
}
if (ca_list[j]->algo_strength & SSL_EXP_MASK) {
if (algo_strength & SSL_EXP_MASK) {
algo_strength &=
(ca_list[j]->algo_strength & SSL_EXP_MASK) |
~SSL_EXP_MASK;
if (!(algo_strength & SSL_EXP_MASK)) {
found = 0;
break;
}
} else
algo_strength |= ca_list[j]->algo_strength & SSL_EXP_MASK;
}
if (ca_list[j]->algo_strength & SSL_STRONG_MASK) {
if (algo_strength & SSL_STRONG_MASK) {
algo_strength &=
(ca_list[j]->algo_strength & SSL_STRONG_MASK) |
~SSL_STRONG_MASK;
if (!(algo_strength & SSL_STRONG_MASK)) {
found = 0;
break;
}
} else
algo_strength |=
ca_list[j]->algo_strength & SSL_STRONG_MASK;
}
if (ca_list[j]->valid) {
cipher_id = ca_list[j]->id;
} else {
if (ca_list[j]->algorithm_ssl) {
if (alg_ssl) {
alg_ssl &= ca_list[j]->algorithm_ssl;
if (!alg_ssl) {
found = 0;
break;
}
} else
alg_ssl = ca_list[j]->algorithm_ssl;
}
}
if (!multi)
break;
}
if (rule == CIPHER_SPECIAL) {
ok = 0;
if ((buflen == 8) && strncmp(buf, "STRENGTH", 8) == 0)
ok = ssl_cipher_strength_sort(head_p, tail_p);
else if (buflen == 10 && strncmp(buf, "SECLEVEL=", 9) == 0) {
int level = buf[9] - '0';
if (level < 0 || level > 5) {
SSLerr(SSL_F_SSL_CIPHER_PROCESS_RULESTR,
SSL_R_INVALID_COMMAND);
} else {
c->sec_level = level;
ok = 1;
}
} else
SSLerr(SSL_F_SSL_CIPHER_PROCESS_RULESTR,
SSL_R_INVALID_COMMAND);
if (ok == 0)
retval = 0;
while ((*l != '\0') && !ITEM_SEP(*l))
l++;
} else if (found) {
ssl_cipher_apply_rule(cipher_id,
alg_mkey, alg_auth, alg_enc, alg_mac,
alg_ssl, algo_strength, rule, -1, head_p,
tail_p);
} else {
while ((*l != '\0') && !ITEM_SEP(*l))
l++;
}
if (*l == '\0')
break;
}
return (retval);
}
|
['static int ssl_cipher_process_rulestr(const char *rule_str,\n CIPHER_ORDER **head_p,\n CIPHER_ORDER **tail_p,\n const SSL_CIPHER **ca_list, CERT *c)\n{\n unsigned long alg_mkey, alg_auth, alg_enc, alg_mac, alg_ssl,\n algo_strength;\n const char *l, *buf;\n int j, multi, found, rule, retval, ok, buflen;\n unsigned long cipher_id = 0;\n char ch;\n retval = 1;\n l = rule_str;\n for (;;) {\n ch = *l;\n if (ch == \'\\0\')\n break;\n if (ch == \'-\') {\n rule = CIPHER_DEL;\n l++;\n } else if (ch == \'+\') {\n rule = CIPHER_ORD;\n l++;\n } else if (ch == \'!\') {\n rule = CIPHER_KILL;\n l++;\n } else if (ch == \'@\') {\n rule = CIPHER_SPECIAL;\n l++;\n } else {\n rule = CIPHER_ADD;\n }\n if (ITEM_SEP(ch)) {\n l++;\n continue;\n }\n alg_mkey = 0;\n alg_auth = 0;\n alg_enc = 0;\n alg_mac = 0;\n alg_ssl = 0;\n algo_strength = 0;\n for (;;) {\n ch = *l;\n buf = l;\n buflen = 0;\n#ifndef CHARSET_EBCDIC\n while (((ch >= \'A\') && (ch <= \'Z\')) ||\n ((ch >= \'0\') && (ch <= \'9\')) ||\n ((ch >= \'a\') && (ch <= \'z\')) ||\n (ch == \'-\') || (ch == \'.\') || (ch == \'=\'))\n#else\n while (isalnum(ch) || (ch == \'-\') || (ch == \'.\') || (ch == \'=\'))\n#endif\n {\n ch = *(++l);\n buflen++;\n }\n if (buflen == 0) {\n SSLerr(SSL_F_SSL_CIPHER_PROCESS_RULESTR,\n SSL_R_INVALID_COMMAND);\n retval = found = 0;\n l++;\n break;\n }\n if (rule == CIPHER_SPECIAL) {\n found = 0;\n break;\n }\n if (ch == \'+\') {\n multi = 1;\n l++;\n } else\n multi = 0;\n j = found = 0;\n cipher_id = 0;\n while (ca_list[j]) {\n if (strncmp(buf, ca_list[j]->name, buflen) == 0\n && (ca_list[j]->name[buflen] == \'\\0\')) {\n found = 1;\n break;\n } else\n j++;\n }\n if (!found)\n break;\n if (ca_list[j]->algorithm_mkey) {\n if (alg_mkey) {\n alg_mkey &= ca_list[j]->algorithm_mkey;\n if (!alg_mkey) {\n found = 0;\n break;\n }\n } else\n alg_mkey = ca_list[j]->algorithm_mkey;\n }\n if (ca_list[j]->algorithm_auth) {\n if (alg_auth) {\n alg_auth &= ca_list[j]->algorithm_auth;\n if (!alg_auth) {\n found = 0;\n break;\n }\n } else\n alg_auth = ca_list[j]->algorithm_auth;\n }\n if (ca_list[j]->algorithm_enc) {\n if (alg_enc) {\n alg_enc &= ca_list[j]->algorithm_enc;\n if (!alg_enc) {\n found = 0;\n break;\n }\n } else\n alg_enc = ca_list[j]->algorithm_enc;\n }\n if (ca_list[j]->algorithm_mac) {\n if (alg_mac) {\n alg_mac &= ca_list[j]->algorithm_mac;\n if (!alg_mac) {\n found = 0;\n break;\n }\n } else\n alg_mac = ca_list[j]->algorithm_mac;\n }\n if (ca_list[j]->algo_strength & SSL_EXP_MASK) {\n if (algo_strength & SSL_EXP_MASK) {\n algo_strength &=\n (ca_list[j]->algo_strength & SSL_EXP_MASK) |\n ~SSL_EXP_MASK;\n if (!(algo_strength & SSL_EXP_MASK)) {\n found = 0;\n break;\n }\n } else\n algo_strength |= ca_list[j]->algo_strength & SSL_EXP_MASK;\n }\n if (ca_list[j]->algo_strength & SSL_STRONG_MASK) {\n if (algo_strength & SSL_STRONG_MASK) {\n algo_strength &=\n (ca_list[j]->algo_strength & SSL_STRONG_MASK) |\n ~SSL_STRONG_MASK;\n if (!(algo_strength & SSL_STRONG_MASK)) {\n found = 0;\n break;\n }\n } else\n algo_strength |=\n ca_list[j]->algo_strength & SSL_STRONG_MASK;\n }\n if (ca_list[j]->valid) {\n cipher_id = ca_list[j]->id;\n } else {\n if (ca_list[j]->algorithm_ssl) {\n if (alg_ssl) {\n alg_ssl &= ca_list[j]->algorithm_ssl;\n if (!alg_ssl) {\n found = 0;\n break;\n }\n } else\n alg_ssl = ca_list[j]->algorithm_ssl;\n }\n }\n if (!multi)\n break;\n }\n if (rule == CIPHER_SPECIAL) {\n ok = 0;\n if ((buflen == 8) && strncmp(buf, "STRENGTH", 8) == 0)\n ok = ssl_cipher_strength_sort(head_p, tail_p);\n else if (buflen == 10 && strncmp(buf, "SECLEVEL=", 9) == 0) {\n int level = buf[9] - \'0\';\n if (level < 0 || level > 5) {\n SSLerr(SSL_F_SSL_CIPHER_PROCESS_RULESTR,\n SSL_R_INVALID_COMMAND);\n } else {\n c->sec_level = level;\n ok = 1;\n }\n } else\n SSLerr(SSL_F_SSL_CIPHER_PROCESS_RULESTR,\n SSL_R_INVALID_COMMAND);\n if (ok == 0)\n retval = 0;\n while ((*l != \'\\0\') && !ITEM_SEP(*l))\n l++;\n } else if (found) {\n ssl_cipher_apply_rule(cipher_id,\n alg_mkey, alg_auth, alg_enc, alg_mac,\n alg_ssl, algo_strength, rule, -1, head_p,\n tail_p);\n } else {\n while ((*l != \'\\0\') && !ITEM_SEP(*l))\n l++;\n }\n if (*l == \'\\0\')\n break;\n }\n return (retval);\n}']
|
2,168
| 0
|
https://github.com/openssl/openssl/blob/8b0d4242404f9e5da26e7594fa0864b2df4601af/crypto/bn/bn_lib.c/#L271
|
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 rand_serial(BIGNUM *b, ASN1_INTEGER *ai)\n{\n BIGNUM *btmp;\n int ret = 0;\n if (b)\n btmp = b;\n else\n btmp = BN_new();\n if (btmp == NULL)\n return 0;\n if (!BN_pseudo_rand(btmp, SERIAL_RAND_BITS, 0, 0))\n goto error;\n if (ai && !BN_to_ASN1_INTEGER(btmp, ai))\n goto error;\n ret = 1;\n error:\n if (btmp != b)\n BN_free(btmp);\n return ret;\n}', 'int BN_pseudo_rand(BIGNUM *rnd, int bits, int top, int bottom)\n{\n return bnrand(1, 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}', '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}', '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 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}']
|
2,169
| 0
|
https://github.com/openssl/openssl/blob/e3713c365c2657236439fea00822a43aa396d112/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;
}
|
['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}', '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 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}', '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}']
|
2,170
| 1
|
https://github.com/openssl/openssl/blob/88050dd1960bfaba7ede12a3ce1afe40f5deb124/ssl/packet.c/#L48
|
int WPACKET_reserve_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes)
{
if (!ossl_assert(pkt->subs != NULL && len != 0))
return 0;
if (pkt->maxsize - pkt->written < len)
return 0;
if (pkt->staticbuf == NULL && (pkt->buf->length - pkt->written < len)) {
size_t newlen;
size_t reflen;
reflen = (len > pkt->buf->length) ? len : pkt->buf->length;
if (reflen > SIZE_MAX / 2) {
newlen = SIZE_MAX;
} else {
newlen = reflen * 2;
if (newlen < DEFAULT_BUF_SIZE)
newlen = DEFAULT_BUF_SIZE;
}
if (BUF_MEM_grow(pkt->buf, newlen) == 0)
return 0;
}
if (allocbytes != NULL)
*allocbytes = WPACKET_get_curr(pkt);
return 1;
}
|
['int tls_construct_server_hello(SSL *s, WPACKET *pkt)\n{\n int compm;\n size_t sl, len;\n int version;\n version = SSL_IS_TLS13(s) ? TLS1_2_VERSION : s->version;\n if (!WPACKET_put_bytes_u16(pkt, version)\n || !WPACKET_memcpy(pkt, s->s3->server_random, SSL3_RANDOM_SIZE)) {\n SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_SERVER_HELLO,\n ERR_R_INTERNAL_ERROR);\n return 0;\n }\n if (s->session->not_resumable ||\n (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER)\n && !s->hit))\n s->session->session_id_length = 0;\n sl = s->session->session_id_length;\n if (sl > sizeof(s->session->session_id)) {\n SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_SERVER_HELLO,\n ERR_R_INTERNAL_ERROR);\n return 0;\n }\n if (SSL_IS_TLS13(s))\n sl = 0;\n#ifdef OPENSSL_NO_COMP\n compm = 0;\n#else\n if (SSL_IS_TLS13(s) || s->s3->tmp.new_compression == NULL)\n compm = 0;\n else\n compm = s->s3->tmp.new_compression->id;\n#endif\n if (!WPACKET_sub_memcpy_u8(pkt, s->session->session_id, sl)\n || !s->method->put_cipher_by_char(s->s3->tmp.new_cipher, pkt, &len)\n || !WPACKET_put_bytes_u8(pkt, compm)\n || !tls_construct_extensions(s, pkt,\n SSL_IS_TLS13(s)\n ? SSL_EXT_TLS1_3_SERVER_HELLO\n : SSL_EXT_TLS1_2_SERVER_HELLO,\n NULL, 0)) {\n return 0;\n }\n if (!(s->verify_mode & SSL_VERIFY_PEER)\n && !ssl3_digest_cached_records(s, 0)) {\n ;\n return 0;\n }\n return 1;\n}', 'int WPACKET_memcpy(WPACKET *pkt, const void *src, size_t len)\n{\n unsigned char *dest;\n if (len == 0)\n return 1;\n if (!WPACKET_allocate_bytes(pkt, len, &dest))\n return 0;\n memcpy(dest, src, len);\n return 1;\n}', 'int WPACKET_sub_memcpy__(WPACKET *pkt, const void *src, size_t len,\n size_t lenbytes)\n{\n if (!WPACKET_start_sub_packet_len__(pkt, lenbytes)\n || !WPACKET_memcpy(pkt, src, len)\n || !WPACKET_close(pkt))\n return 0;\n return 1;\n}', 'int WPACKET_start_sub_packet_len__(WPACKET *pkt, size_t lenbytes)\n{\n WPACKET_SUB *sub;\n unsigned char *lenchars;\n if (!ossl_assert(pkt->subs != NULL))\n return 0;\n sub = OPENSSL_zalloc(sizeof(*sub));\n if (sub == NULL)\n return 0;\n sub->parent = pkt->subs;\n pkt->subs = sub;\n sub->pwritten = pkt->written + lenbytes;\n sub->lenbytes = lenbytes;\n if (lenbytes == 0) {\n sub->packet_len = 0;\n return 1;\n }\n if (!WPACKET_allocate_bytes(pkt, lenbytes, &lenchars))\n return 0;\n sub->packet_len = lenchars - GETBUF(pkt);\n return 1;\n}', 'int WPACKET_allocate_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes)\n{\n if (!WPACKET_reserve_bytes(pkt, len, allocbytes))\n return 0;\n pkt->written += len;\n pkt->curr += len;\n return 1;\n}', 'int WPACKET_reserve_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes)\n{\n if (!ossl_assert(pkt->subs != NULL && len != 0))\n return 0;\n if (pkt->maxsize - pkt->written < len)\n return 0;\n if (pkt->staticbuf == NULL && (pkt->buf->length - pkt->written < len)) {\n size_t newlen;\n size_t reflen;\n reflen = (len > pkt->buf->length) ? len : pkt->buf->length;\n if (reflen > SIZE_MAX / 2) {\n newlen = SIZE_MAX;\n } else {\n newlen = reflen * 2;\n if (newlen < DEFAULT_BUF_SIZE)\n newlen = DEFAULT_BUF_SIZE;\n }\n if (BUF_MEM_grow(pkt->buf, newlen) == 0)\n return 0;\n }\n if (allocbytes != NULL)\n *allocbytes = WPACKET_get_curr(pkt);\n return 1;\n}']
|
2,171
| 0
|
https://github.com/openssl/openssl/blob/9ef73a6fd9a43a79783d2c68339c96532c3209f9/test/ssltest_old.c/#L322
|
static int cb_server_alpn(SSL *s, const unsigned char **out,
unsigned char *outlen, const unsigned char *in,
unsigned int inlen, void *arg)
{
unsigned char *protos;
size_t protos_len;
char* alpn_str = arg;
protos = next_protos_parse(&protos_len, alpn_str);
if (protos == NULL) {
fprintf(stderr, "failed to parser ALPN server protocol string: %s\n",
alpn_str);
abort();
}
if (SSL_select_next_proto
((unsigned char **)out, outlen, protos, protos_len, in,
inlen) != OPENSSL_NPN_NEGOTIATED) {
OPENSSL_free(protos);
return SSL_TLSEXT_ERR_NOACK;
}
alpn_selected = OPENSSL_malloc(*outlen);
memcpy(alpn_selected, *out, *outlen);
*out = alpn_selected;
OPENSSL_free(protos);
return SSL_TLSEXT_ERR_OK;
}
|
['static int cb_server_alpn(SSL *s, const unsigned char **out,\n unsigned char *outlen, const unsigned char *in,\n unsigned int inlen, void *arg)\n{\n unsigned char *protos;\n size_t protos_len;\n char* alpn_str = arg;\n protos = next_protos_parse(&protos_len, alpn_str);\n if (protos == NULL) {\n fprintf(stderr, "failed to parser ALPN server protocol string: %s\\n",\n alpn_str);\n abort();\n }\n if (SSL_select_next_proto\n ((unsigned char **)out, outlen, protos, protos_len, in,\n inlen) != OPENSSL_NPN_NEGOTIATED) {\n OPENSSL_free(protos);\n return SSL_TLSEXT_ERR_NOACK;\n }\n alpn_selected = OPENSSL_malloc(*outlen);\n memcpy(alpn_selected, *out, *outlen);\n *out = alpn_selected;\n OPENSSL_free(protos);\n return SSL_TLSEXT_ERR_OK;\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 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}']
|
2,172
| 0
|
https://gitlab.com/libtiff/libtiff/blob/9c243a11a35d118d00ef8947d244ac55c4145640/tools/tiff2pdf.c/#L4221
|
void t2p_pdf_currenttime(T2P* t2p)
{
struct tm* currenttime;
time_t timenow;
if (time(&timenow) == (time_t) -1) {
TIFFError(TIFF2PDF_MODULE,
"Can't get the current time: %s", strerror(errno));
timenow = (time_t) 0;
}
currenttime = localtime(&timenow);
snprintf(t2p->pdf_datetime, sizeof(t2p->pdf_datetime),
"D:%.4d%.2d%.2d%.2d%.2d%.2d",
(currenttime->tm_year + 1900) % 65536,
(currenttime->tm_mon + 1) % 256,
(currenttime->tm_mday) % 256,
(currenttime->tm_hour) % 256,
(currenttime->tm_min) % 256,
(currenttime->tm_sec) % 256);
return;
}
|
['void t2p_pdf_currenttime(T2P* t2p)\n{\n\tstruct tm* currenttime;\n\ttime_t timenow;\n\tif (time(&timenow) == (time_t) -1) {\n\t\tTIFFError(TIFF2PDF_MODULE,\n\t\t\t "Can\'t get the current time: %s", strerror(errno));\n\t\ttimenow = (time_t) 0;\n\t}\n\tcurrenttime = localtime(&timenow);\n\tsnprintf(t2p->pdf_datetime, sizeof(t2p->pdf_datetime),\n\t\t "D:%.4d%.2d%.2d%.2d%.2d%.2d",\n\t\t (currenttime->tm_year + 1900) % 65536,\n\t\t (currenttime->tm_mon + 1) % 256,\n\t\t (currenttime->tm_mday) % 256,\n\t\t (currenttime->tm_hour) % 256,\n\t\t (currenttime->tm_min) % 256,\n\t\t (currenttime->tm_sec) % 256);\n\treturn;\n}', 'void\nTIFFError(const char* module, const char* fmt, ...)\n{\n\tva_list ap;\n\tif (_TIFFerrorHandler) {\n\t\tva_start(ap, fmt);\n\t\t(*_TIFFerrorHandler)(module, fmt, ap);\n\t\tva_end(ap);\n\t}\n\tif (_TIFFerrorHandlerExt) {\n\t\tva_start(ap, fmt);\n\t\t(*_TIFFerrorHandlerExt)(0, module, fmt, ap);\n\t\tva_end(ap);\n\t}\n}']
|
2,173
| 0
|
https://github.com/openssl/openssl/blob/95dc05bc6d0dfe0f3f3681f5e27afbc3f7a35eea/crypto/asn1/a_int.c/#L109
|
int i2d_ASN1_INTEGER(ASN1_INTEGER *a, unsigned char **pp)
{
int pad=0,ret,r,i,t;
unsigned char *p,*pt,*n,pb=0;
if ((a == NULL) || (a->data == NULL)) return(0);
t=a->type;
if (a->length == 0)
ret=1;
else
{
ret=a->length;
i=a->data[0];
if ((t == V_ASN1_INTEGER) && (i > 127))
{
pad=1;
pb=0;
}
else if ((t == V_ASN1_NEG_INTEGER) && (i>128))
{
pad=1;
pb=0xFF;
}
ret+=pad;
}
r=ASN1_object_size(0,ret,V_ASN1_INTEGER);
if (pp == NULL) return(r);
p= *pp;
ASN1_put_object(&p,0,ret,V_ASN1_INTEGER,V_ASN1_UNIVERSAL);
if (pad) *(p++)=pb;
if (a->length == 0)
*(p++)=0;
else if (t == V_ASN1_INTEGER)
{
memcpy(p,a->data,(unsigned int)a->length);
p+=a->length;
}
else
{
n=a->data;
pt=p;
for (i=a->length; i>0; i--)
*(p++)= (*(n++)^0xFF)+1;
if (!pad) *pt|=0x80;
}
*pp=p;
return(r);
}
|
['static int request_certificate(SSL *s)\n\t{\n\tunsigned char *p,*p2,*buf2;\n\tunsigned char *ccd;\n\tint i,j,ctype,ret= -1;\n\tX509 *x509=NULL;\n\tSTACK_OF(X509) *sk=NULL;\n\tccd=s->s2->tmp.ccl;\n\tif (s->state == SSL2_ST_SEND_REQUEST_CERTIFICATE_A)\n\t\t{\n\t\tp=(unsigned char *)s->init_buf->data;\n\t\t*(p++)=SSL2_MT_REQUEST_CERTIFICATE;\n\t\t*(p++)=SSL2_AT_MD5_WITH_RSA_ENCRYPTION;\n\t\tRAND_bytes(ccd,SSL2_MIN_CERT_CHALLENGE_LENGTH);\n\t\tmemcpy(p,ccd,SSL2_MIN_CERT_CHALLENGE_LENGTH);\n\t\ts->state=SSL2_ST_SEND_REQUEST_CERTIFICATE_B;\n\t\ts->init_num=SSL2_MIN_CERT_CHALLENGE_LENGTH+2;\n\t\ts->init_off=0;\n\t\t}\n\tif (s->state == SSL2_ST_SEND_REQUEST_CERTIFICATE_B)\n\t\t{\n\t\ti=ssl2_do_write(s);\n\t\tif (i <= 0)\n\t\t\t{\n\t\t\tret=i;\n\t\t\tgoto end;\n\t\t\t}\n\t\ts->init_num=0;\n\t\ts->state=SSL2_ST_SEND_REQUEST_CERTIFICATE_C;\n\t\t}\n\tif (s->state == SSL2_ST_SEND_REQUEST_CERTIFICATE_C)\n\t\t{\n\t\tp=(unsigned char *)s->init_buf->data;\n\t\ti=ssl2_read(s,(char *)&(p[s->init_num]),6-s->init_num);\n\t\tif (i < 3)\n\t\t\t{\n\t\t\tret=ssl2_part_read(s,SSL_F_REQUEST_CERTIFICATE,i);\n\t\t\tgoto end;\n\t\t\t}\n\t\tif ((*p == SSL2_MT_ERROR) && (i >= 3))\n\t\t\t{\n\t\t\tn2s(p,i);\n\t\t\tif (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)\n\t\t\t\t{\n\t\t\t\tssl2_return_error(s,SSL2_PE_BAD_CERTIFICATE);\n\t\t\t\tSSLerr(SSL_F_REQUEST_CERTIFICATE,SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);\n\t\t\t\tgoto end;\n\t\t\t\t}\n\t\t\tret=1;\n\t\t\tgoto end;\n\t\t\t}\n\t\tif ((*(p++) != SSL2_MT_CLIENT_CERTIFICATE) || (i < 6))\n\t\t\t{\n\t\t\tssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);\n\t\t\tSSLerr(SSL_F_REQUEST_CERTIFICATE,SSL_R_SHORT_READ);\n\t\t\tgoto end;\n\t\t\t}\n\t\tctype= *(p++);\n\t\tif (ctype != SSL2_AT_MD5_WITH_RSA_ENCRYPTION)\n\t\t\t{\n\t\t\tssl2_return_error(s,SSL2_PE_UNSUPPORTED_CERTIFICATE_TYPE);\n\t\t\tSSLerr(SSL_F_REQUEST_CERTIFICATE,SSL_R_BAD_RESPONSE_ARGUMENT);\n\t\t\tgoto end;\n\t\t\t}\n\t\tn2s(p,i); s->s2->tmp.clen=i;\n\t\tn2s(p,i); s->s2->tmp.rlen=i;\n\t\ts->state=SSL2_ST_SEND_REQUEST_CERTIFICATE_D;\n\t\ts->init_num=0;\n\t\t}\n\tp=(unsigned char *)s->init_buf->data;\n\tj=s->s2->tmp.clen+s->s2->tmp.rlen-s->init_num;\n\ti=ssl2_read(s,(char *)&(p[s->init_num]),j);\n\tif (i < j)\n\t\t{\n\t\tret=ssl2_part_read(s,SSL_F_REQUEST_CERTIFICATE,i);\n\t\tgoto end;\n\t\t}\n\tx509=(X509 *)d2i_X509(NULL,&p,(long)s->s2->tmp.clen);\n\tif (x509 == NULL)\n\t\t{\n\t\tSSLerr(SSL_F_REQUEST_CERTIFICATE,ERR_R_X509_LIB);\n\t\tgoto msg_end;\n\t\t}\n\tif (((sk=sk_X509_new_null()) == NULL) || (!sk_X509_push(sk,x509)))\n\t\t{\n\t\tSSLerr(SSL_F_REQUEST_CERTIFICATE,ERR_R_MALLOC_FAILURE);\n\t\tgoto msg_end;\n\t\t}\n\ti=ssl_verify_cert_chain(s,sk);\n\tif (i)\n\t\t{\n\t\tEVP_MD_CTX ctx;\n\t\tEVP_PKEY *pkey=NULL;\n\t\tEVP_VerifyInit(&ctx,s->ctx->rsa_md5);\n\t\tEVP_VerifyUpdate(&ctx,s->s2->key_material,\n\t\t\t(unsigned int)s->s2->key_material_length);\n\t\tEVP_VerifyUpdate(&ctx,ccd,SSL2_MIN_CERT_CHALLENGE_LENGTH);\n\t\ti=i2d_X509(s->session->cert->pkeys[SSL_PKEY_RSA_ENC].x509,NULL);\n\t\tbuf2=(unsigned char *)Malloc((unsigned int)i);\n\t\tif (buf2 == NULL)\n\t\t\t{\n\t\t\tSSLerr(SSL_F_REQUEST_CERTIFICATE,ERR_R_MALLOC_FAILURE);\n\t\t\tgoto msg_end;\n\t\t\t}\n\t\tp2=buf2;\n\t\ti=i2d_X509(s->session->cert->pkeys[SSL_PKEY_RSA_ENC].x509,&p2);\n\t\tEVP_VerifyUpdate(&ctx,buf2,(unsigned int)i);\n\t\tFree(buf2);\n\t\tpkey=X509_get_pubkey(x509);\n\t\tif (pkey == NULL) goto end;\n\t\ti=EVP_VerifyFinal(&ctx,p,s->s2->tmp.rlen,pkey);\n\t\tEVP_PKEY_free(pkey);\n\t\tmemset(&ctx,0,sizeof(ctx));\n\t\tif (i)\n\t\t\t{\n\t\t\tif (s->session->peer != NULL)\n\t\t\t\tX509_free(s->session->peer);\n\t\t\ts->session->peer=x509;\n\t\t\tCRYPTO_add(&x509->references,1,CRYPTO_LOCK_X509);\n\t\t\tret=1;\n\t\t\tgoto end;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tSSLerr(SSL_F_REQUEST_CERTIFICATE,SSL_R_BAD_CHECKSUM);\n\t\t\tgoto msg_end;\n\t\t\t}\n\t\t}\n\telse\n\t\t{\nmsg_end:\n\t\tssl2_return_error(s,SSL2_PE_BAD_CERTIFICATE);\n\t\t}\nend:\n\tsk_X509_free(sk);\n\tX509_free(x509);\n\treturn(ret);\n\t}', 'int i2d_X509(X509 *a, unsigned char **pp)\n\t{\n\tM_ASN1_I2D_vars(a);\n\tM_ASN1_I2D_len(a->cert_info,\ti2d_X509_CINF);\n\tM_ASN1_I2D_len(a->sig_alg,\ti2d_X509_ALGOR);\n\tM_ASN1_I2D_len(a->signature,\ti2d_ASN1_BIT_STRING);\n\tM_ASN1_I2D_seq_total();\n\tM_ASN1_I2D_put(a->cert_info,\ti2d_X509_CINF);\n\tM_ASN1_I2D_put(a->sig_alg,\ti2d_X509_ALGOR);\n\tM_ASN1_I2D_put(a->signature,\ti2d_ASN1_BIT_STRING);\n\tM_ASN1_I2D_finish();\n\t}', 'void ASN1_put_object(unsigned char **pp, int constructed, int length, int tag,\n\t int xclass)\n\t{\n\tunsigned char *p= *pp;\n\tint i;\n\ti=(constructed)?V_ASN1_CONSTRUCTED:0;\n\ti|=(xclass&V_ASN1_PRIVATE);\n\tif (tag < 31)\n\t\t*(p++)=i|(tag&V_ASN1_PRIMATIVE_TAG);\n\telse\n\t\t{\n\t\t*(p++)=i|V_ASN1_PRIMATIVE_TAG;\n\t\twhile (tag > 0x7f)\n\t\t\t{\n\t\t\t*(p++)=(tag&0x7f)|0x80;\n\t\t\ttag>>=7;\n\t\t\t}\n\t\t*(p++)=(tag&0x7f);\n\t\t}\n\tif ((constructed == 2) && (length == 0))\n\t\t*(p++)=0x80;\n\telse\n\t\tasn1_put_length(&p,length);\n\t*pp=p;\n\t}', 'int i2d_X509_CINF(X509_CINF *a, unsigned char **pp)\n\t{\n\tint v1=0,v2=0;\n\tM_ASN1_I2D_vars(a);\n\tM_ASN1_I2D_len_EXP_opt(a->version,i2d_ASN1_INTEGER,0,v1);\n\tM_ASN1_I2D_len(a->serialNumber,\t\ti2d_ASN1_INTEGER);\n\tM_ASN1_I2D_len(a->signature,\t\ti2d_X509_ALGOR);\n\tM_ASN1_I2D_len(a->issuer,\t\ti2d_X509_NAME);\n\tM_ASN1_I2D_len(a->validity,\t\ti2d_X509_VAL);\n\tM_ASN1_I2D_len(a->subject,\t\ti2d_X509_NAME);\n\tM_ASN1_I2D_len(a->key,\t\t\ti2d_X509_PUBKEY);\n\tM_ASN1_I2D_len_IMP_opt(a->issuerUID,\ti2d_ASN1_BIT_STRING);\n\tM_ASN1_I2D_len_IMP_opt(a->subjectUID,\ti2d_ASN1_BIT_STRING);\n\tM_ASN1_I2D_len_EXP_SEQUENCE_opt(a->extensions,i2d_X509_EXTENSION,3,V_ASN1_SEQUENCE,v2);\n\tM_ASN1_I2D_seq_total();\n\tM_ASN1_I2D_put_EXP_opt(a->version,i2d_ASN1_INTEGER,0,v1);\n\tM_ASN1_I2D_put(a->serialNumber,\t\ti2d_ASN1_INTEGER);\n\tM_ASN1_I2D_put(a->signature,\t\ti2d_X509_ALGOR);\n\tM_ASN1_I2D_put(a->issuer,\t\ti2d_X509_NAME);\n\tM_ASN1_I2D_put(a->validity,\t\ti2d_X509_VAL);\n\tM_ASN1_I2D_put(a->subject,\t\ti2d_X509_NAME);\n\tM_ASN1_I2D_put(a->key,\t\t\ti2d_X509_PUBKEY);\n\tM_ASN1_I2D_put_IMP_opt(a->issuerUID,\ti2d_ASN1_BIT_STRING,1);\n\tM_ASN1_I2D_put_IMP_opt(a->subjectUID,\ti2d_ASN1_BIT_STRING,2);\n\tM_ASN1_I2D_put_EXP_SEQUENCE_opt(a->extensions,i2d_X509_EXTENSION,3,V_ASN1_SEQUENCE,v2);\n\tM_ASN1_I2D_finish();\n\t}', 'int i2d_ASN1_INTEGER(ASN1_INTEGER *a, unsigned char **pp)\n\t{\n\tint pad=0,ret,r,i,t;\n\tunsigned char *p,*pt,*n,pb=0;\n\tif ((a == NULL) || (a->data == NULL)) return(0);\n\tt=a->type;\n\tif (a->length == 0)\n\t\tret=1;\n\telse\n\t\t{\n\t\tret=a->length;\n\t\ti=a->data[0];\n\t\tif ((t == V_ASN1_INTEGER) && (i > 127))\n\t\t\t{\n\t\t\tpad=1;\n\t\t\tpb=0;\n\t\t\t}\n\t\telse if ((t == V_ASN1_NEG_INTEGER) && (i>128))\n\t\t\t{\n\t\t\tpad=1;\n\t\t\tpb=0xFF;\n\t\t\t}\n\t\tret+=pad;\n\t\t}\n\tr=ASN1_object_size(0,ret,V_ASN1_INTEGER);\n\tif (pp == NULL) return(r);\n\tp= *pp;\n\tASN1_put_object(&p,0,ret,V_ASN1_INTEGER,V_ASN1_UNIVERSAL);\n\tif (pad) *(p++)=pb;\n\tif (a->length == 0)\n\t\t*(p++)=0;\n\telse if (t == V_ASN1_INTEGER)\n\t\t{\n\t\tmemcpy(p,a->data,(unsigned int)a->length);\n\t\tp+=a->length;\n\t\t}\n\telse\n\t\t{\n\t\tn=a->data;\n\t\tpt=p;\n\t\tfor (i=a->length; i>0; i--)\n\t\t\t*(p++)= (*(n++)^0xFF)+1;\n\t\tif (!pad) *pt|=0x80;\n\t\t}\n\t*pp=p;\n\treturn(r);\n\t}']
|
2,174
| 0
|
https://github.com/openssl/openssl/blob/c922ebe23247ff9ee07310fa30647623c0547cd9/ssl/packet.c/#L49
|
int WPACKET_reserve_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes)
{
assert(pkt->subs != NULL && len != 0);
if (pkt->subs == NULL || len == 0)
return 0;
if (pkt->maxsize - pkt->written < len)
return 0;
if (pkt->staticbuf == NULL && (pkt->buf->length - pkt->written < len)) {
size_t newlen;
size_t reflen;
reflen = (len > pkt->buf->length) ? len : pkt->buf->length;
if (reflen > SIZE_MAX / 2) {
newlen = SIZE_MAX;
} else {
newlen = reflen * 2;
if (newlen < DEFAULT_BUF_SIZE)
newlen = DEFAULT_BUF_SIZE;
}
if (BUF_MEM_grow(pkt->buf, newlen) == 0)
return 0;
}
if (allocbytes != NULL)
*allocbytes = WPACKET_get_curr(pkt);
return 1;
}
|
['int tls_construct_stoc_ec_pt_formats(SSL *s, WPACKET *pkt, int *al)\n{\n unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;\n unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;\n int using_ecc = ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA))\n && (s->session->tlsext_ecpointformatlist != NULL);\n const unsigned char *plist;\n size_t plistlen;\n if (!using_ecc)\n return 1;\n tls1_get_formatlist(s, &plist, &plistlen);\n if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_ec_point_formats)\n || !WPACKET_start_sub_packet_u16(pkt)\n || !WPACKET_sub_memcpy_u8(pkt, plist, plistlen)\n || !WPACKET_close(pkt)) {\n SSLerr(SSL_F_TLS_CONSTRUCT_STOC_EC_PT_FORMATS, ERR_R_INTERNAL_ERROR);\n return 0;\n }\n return 1;\n}', 'int WPACKET_start_sub_packet_len__(WPACKET *pkt, size_t lenbytes)\n{\n WPACKET_SUB *sub;\n unsigned char *lenchars;\n assert(pkt->subs != NULL);\n if (pkt->subs == NULL)\n return 0;\n sub = OPENSSL_zalloc(sizeof(*sub));\n if (sub == NULL)\n return 0;\n sub->parent = pkt->subs;\n pkt->subs = sub;\n sub->pwritten = pkt->written + lenbytes;\n sub->lenbytes = lenbytes;\n if (lenbytes == 0) {\n sub->packet_len = 0;\n return 1;\n }\n if (!WPACKET_allocate_bytes(pkt, lenbytes, &lenchars))\n return 0;\n sub->packet_len = lenchars - GETBUF(pkt);\n return 1;\n}', 'int WPACKET_sub_memcpy__(WPACKET *pkt, const void *src, size_t len,\n size_t lenbytes)\n{\n if (!WPACKET_start_sub_packet_len__(pkt, lenbytes)\n || !WPACKET_memcpy(pkt, src, len)\n || !WPACKET_close(pkt))\n return 0;\n return 1;\n}', 'int WPACKET_allocate_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes)\n{\n if (!WPACKET_reserve_bytes(pkt, len, allocbytes))\n return 0;\n pkt->written += len;\n pkt->curr += len;\n return 1;\n}', 'int WPACKET_reserve_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes)\n{\n assert(pkt->subs != NULL && len != 0);\n if (pkt->subs == NULL || len == 0)\n return 0;\n if (pkt->maxsize - pkt->written < len)\n return 0;\n if (pkt->staticbuf == NULL && (pkt->buf->length - pkt->written < len)) {\n size_t newlen;\n size_t reflen;\n reflen = (len > pkt->buf->length) ? len : pkt->buf->length;\n if (reflen > SIZE_MAX / 2) {\n newlen = SIZE_MAX;\n } else {\n newlen = reflen * 2;\n if (newlen < DEFAULT_BUF_SIZE)\n newlen = DEFAULT_BUF_SIZE;\n }\n if (BUF_MEM_grow(pkt->buf, newlen) == 0)\n return 0;\n }\n if (allocbytes != NULL)\n *allocbytes = WPACKET_get_curr(pkt);\n return 1;\n}']
|
2,175
| 0
|
https://github.com/openssl/openssl/blob/f7a3296d8c8746b9901e95674425f300a6dfd1d4/apps/x509.c/#L1129
|
static int x509_certify(X509_STORE *ctx, char *CAfile, const EVP_MD *digest,
X509 *x, X509 *xca, EVP_PKEY *pkey, char *serialfile, int create,
int days, int clrext, CONF *conf, char *section, ASN1_INTEGER *sno)
{
int ret=0;
ASN1_INTEGER *bs=NULL;
X509_STORE_CTX xsc;
EVP_PKEY *upkey;
upkey = X509_get_pubkey(xca);
EVP_PKEY_copy_parameters(upkey,pkey);
EVP_PKEY_free(upkey);
if(!X509_STORE_CTX_init(&xsc,ctx,x,NULL))
{
BIO_printf(bio_err,"Error initialising X509 store\n");
goto end;
}
if (sno) bs = sno;
else if (!(bs = x509_load_serial(CAfile, serialfile, create)))
goto end;
X509_STORE_CTX_set_cert(&xsc,x);
if (!reqfile && !X509_verify_cert(&xsc))
goto end;
if (!X509_check_private_key(xca,pkey))
{
BIO_printf(bio_err,"CA certificate and CA private key do not match\n");
goto end;
}
if (!X509_set_issuer_name(x,X509_get_subject_name(xca))) goto end;
if (!X509_set_serialNumber(x,bs)) goto end;
if (X509_gmtime_adj(X509_get_notBefore(x),0L) == NULL)
goto end;
if (X509_gmtime_adj(X509_get_notAfter(x),(long)60*60*24*days) == NULL)
goto end;
if (clrext)
{
while (X509_get_ext_count(x) > 0) X509_delete_ext(x, 0);
}
if (conf)
{
X509V3_CTX ctx2;
X509_set_version(x,2);
X509V3_set_ctx(&ctx2, xca, x, NULL, NULL, 0);
X509V3_set_nconf(&ctx2, conf);
if (!X509V3_EXT_add_nconf(conf, &ctx2, section, x)) goto end;
}
if (!X509_sign(x,pkey,digest)) goto end;
ret=1;
end:
X509_STORE_CTX_cleanup(&xsc);
if (!ret)
ERR_print_errors(bio_err);
if (!sno) ASN1_INTEGER_free(bs);
return ret;
}
|
['static int x509_certify(X509_STORE *ctx, char *CAfile, const EVP_MD *digest,\n\t X509 *x, X509 *xca, EVP_PKEY *pkey, char *serialfile, int create,\n\t int days, int clrext, CONF *conf, char *section, ASN1_INTEGER *sno)\n\t{\n\tint ret=0;\n\tASN1_INTEGER *bs=NULL;\n\tX509_STORE_CTX xsc;\n\tEVP_PKEY *upkey;\n\tupkey = X509_get_pubkey(xca);\n\tEVP_PKEY_copy_parameters(upkey,pkey);\n\tEVP_PKEY_free(upkey);\n\tif(!X509_STORE_CTX_init(&xsc,ctx,x,NULL))\n\t\t{\n\t\tBIO_printf(bio_err,"Error initialising X509 store\\n");\n\t\tgoto end;\n\t\t}\n\tif (sno) bs = sno;\n\telse if (!(bs = x509_load_serial(CAfile, serialfile, create)))\n\t\tgoto end;\n\tX509_STORE_CTX_set_cert(&xsc,x);\n\tif (!reqfile && !X509_verify_cert(&xsc))\n\t\tgoto end;\n\tif (!X509_check_private_key(xca,pkey))\n\t\t{\n\t\tBIO_printf(bio_err,"CA certificate and CA private key do not match\\n");\n\t\tgoto end;\n\t\t}\n\tif (!X509_set_issuer_name(x,X509_get_subject_name(xca))) goto end;\n\tif (!X509_set_serialNumber(x,bs)) goto end;\n\tif (X509_gmtime_adj(X509_get_notBefore(x),0L) == NULL)\n\t\tgoto end;\n\tif (X509_gmtime_adj(X509_get_notAfter(x),(long)60*60*24*days) == NULL)\n\t\tgoto end;\n\tif (clrext)\n\t\t{\n\t\twhile (X509_get_ext_count(x) > 0) X509_delete_ext(x, 0);\n\t\t}\n\tif (conf)\n\t\t{\n\t\tX509V3_CTX ctx2;\n\t\tX509_set_version(x,2);\n X509V3_set_ctx(&ctx2, xca, x, NULL, NULL, 0);\n X509V3_set_nconf(&ctx2, conf);\n if (!X509V3_EXT_add_nconf(conf, &ctx2, section, x)) goto end;\n\t\t}\n\tif (!X509_sign(x,pkey,digest)) goto end;\n\tret=1;\nend:\n\tX509_STORE_CTX_cleanup(&xsc);\n\tif (!ret)\n\t\tERR_print_errors(bio_err);\n\tif (!sno) ASN1_INTEGER_free(bs);\n\treturn ret;\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\tconst EVP_PKEY_ASN1_METHOD *meth;\n\tif (key == NULL) goto error;\n\tif (key->pkey != NULL)\n\t\t{\n\t\tCRYPTO_add(&key->pkey->references, 1, CRYPTO_LOCK_EVP_PKEY);\n\t\treturn key->pkey;\n\t\t}\n\tif (key->public_key == NULL) goto error;\n\tif ((ret = EVP_PKEY_new()) == NULL)\n\t\t{\n\t\tX509err(X509_F_X509_PUBKEY_GET, ERR_R_MALLOC_FAILURE);\n\t\tgoto error;\n\t\t}\n\tmeth = EVP_PKEY_asn1_find(OBJ_obj2nid(key->algor->algorithm));\n\tif (meth)\n\t\t{\n\t\tif (meth->pub_decode)\n\t\t\t{\n\t\t\tif (!meth->pub_decode(ret, key))\n\t\t\t\t{\n\t\t\t\tX509err(X509_F_X509_PUBKEY_GET,\n\t\t\t\t\t\tX509_R_PUBLIC_KEY_DECODE_ERROR);\n\t\t\t\tgoto error;\n\t\t\t\t}\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tX509err(X509_F_X509_PUBKEY_GET,\n\t\t\t\tX509_R_METHOD_NOT_SUPPORTED);\n\t\t\tgoto error;\n\t\t\t}\n\t\t}\n\telse\n\t\t{\n\t\tX509err(X509_F_X509_PUBKEY_GET,X509_R_UNSUPPORTED_ALGORITHM);\n\t\tgoto error;\n\t\t}\n\tkey->pkey = ret;\n\tCRYPTO_add(&ret->references, 1, CRYPTO_LOCK_EVP_PKEY);\n\treturn ret;\n\terror:\n\tif (ret != NULL)\n\t\tEVP_PKEY_free(ret);\n\treturn(NULL);\n\t}', 'EVP_PKEY *EVP_PKEY_new(void)\n\t{\n\tEVP_PKEY *ret;\n\tret=(EVP_PKEY *)OPENSSL_malloc(sizeof(EVP_PKEY));\n\tif (ret == NULL)\n\t\t{\n\t\tEVPerr(EVP_F_EVP_PKEY_NEW,ERR_R_MALLOC_FAILURE);\n\t\treturn(NULL);\n\t\t}\n\tret->type=EVP_PKEY_NONE;\n\tret->references=1;\n\tret->ameth=NULL;\n\tret->pkey.ptr=NULL;\n\tret->attributes=NULL;\n\tret->save_parameters=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}', 'int EVP_PKEY_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from)\n\t{\n\tif (to->type != from->type)\n\t\t{\n\t\tEVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS,EVP_R_DIFFERENT_KEY_TYPES);\n\t\tgoto err;\n\t\t}\n\tif (EVP_PKEY_missing_parameters(from))\n\t\t{\n\t\tEVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS,EVP_R_MISSING_PARAMETERS);\n\t\tgoto err;\n\t\t}\n\tif (from->ameth && from->ameth->param_copy)\n\t\treturn from->ameth->param_copy(to, from);\nerr:\n\treturn 0;\n\t}']
|
2,176
| 0
|
https://github.com/openssl/openssl/blob/8b0d4242404f9e5da26e7594fa0864b2df4601af/crypto/bn/bn_lib.c/#L271
|
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;
}
|
['RSA *RSA_generate_key(int bits, unsigned long e_value,\n void (*callback) (int, int, void *), void *cb_arg)\n{\n int i;\n BN_GENCB *cb = BN_GENCB_new();\n RSA *rsa = RSA_new();\n BIGNUM *e = BN_new();\n if (cb == NULL || rsa == NULL || e == NULL)\n goto err;\n for (i = 0; i < (int)sizeof(unsigned long) * 8; i++) {\n if (e_value & (1UL << i))\n if (BN_set_bit(e, i) == 0)\n goto err;\n }\n BN_GENCB_set_old(cb, callback, cb_arg);\n if (RSA_generate_key_ex(rsa, bits, e, cb)) {\n BN_free(e);\n BN_GENCB_free(cb);\n return rsa;\n }\n err:\n BN_free(e);\n RSA_free(rsa);\n BN_GENCB_free(cb);\n return 0;\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}', '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}']
|
2,177
| 1
|
https://github.com/openssl/openssl/blob/1a5a1a93f6c48b135a2b384f7e571abb7b90fc55/ssl/ssl_rsa.c/#L197
|
static int ssl_set_pkey(CERT *c, EVP_PKEY *pkey)
{
int i;
i=ssl_cert_type(NULL,pkey);
if (i < 0)
{
SSLerr(SSL_F_SSL_SET_PKEY,SSL_R_UNKNOWN_CERTIFICATE_TYPE);
return(0);
}
if (c->pkeys[i].x509 != NULL)
{
EVP_PKEY *pktmp;
pktmp = X509_get_pubkey(c->pkeys[i].x509);
EVP_PKEY_copy_parameters(pktmp,pkey);
EVP_PKEY_free(pktmp);
ERR_clear_error();
#ifndef OPENSSL_NO_RSA
if ((pkey->type == EVP_PKEY_RSA) &&
(RSA_flags(pkey->pkey.rsa) & RSA_METHOD_FLAG_NO_CHECK))
;
else
#endif
if (!X509_check_private_key(c->pkeys[i].x509,pkey))
{
X509_free(c->pkeys[i].x509);
c->pkeys[i].x509 = NULL;
return 0;
}
}
if (c->pkeys[i].privatekey != NULL)
EVP_PKEY_free(c->pkeys[i].privatekey);
CRYPTO_add(&pkey->references,1,CRYPTO_LOCK_EVP_PKEY);
c->pkeys[i].privatekey=pkey;
c->key= &(c->pkeys[i]);
c->valid=0;
return(1);
}
|
['static int ssl_set_pkey(CERT *c, EVP_PKEY *pkey)\n\t{\n\tint i;\n\ti=ssl_cert_type(NULL,pkey);\n\tif (i < 0)\n\t\t{\n\t\tSSLerr(SSL_F_SSL_SET_PKEY,SSL_R_UNKNOWN_CERTIFICATE_TYPE);\n\t\treturn(0);\n\t\t}\n\tif (c->pkeys[i].x509 != NULL)\n\t\t{\n\t\tEVP_PKEY *pktmp;\n\t\tpktmp =\tX509_get_pubkey(c->pkeys[i].x509);\n\t\tEVP_PKEY_copy_parameters(pktmp,pkey);\n\t\tEVP_PKEY_free(pktmp);\n\t\tERR_clear_error();\n#ifndef OPENSSL_NO_RSA\n\t\tif ((pkey->type == EVP_PKEY_RSA) &&\n\t\t\t(RSA_flags(pkey->pkey.rsa) & RSA_METHOD_FLAG_NO_CHECK))\n\t\t\t;\n\t\telse\n#endif\n\t\tif (!X509_check_private_key(c->pkeys[i].x509,pkey))\n\t\t\t{\n\t\t\tX509_free(c->pkeys[i].x509);\n\t\t\tc->pkeys[i].x509 = NULL;\n\t\t\treturn 0;\n\t\t\t}\n\t\t}\n\tif (c->pkeys[i].privatekey != NULL)\n\t\tEVP_PKEY_free(c->pkeys[i].privatekey);\n\tCRYPTO_add(&pkey->references,1,CRYPTO_LOCK_EVP_PKEY);\n\tc->pkeys[i].privatekey=pkey;\n\tc->key= &(c->pkeys[i]);\n\tc->valid=0;\n\treturn(1);\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\tconst unsigned char *p;\n#if !defined(OPENSSL_NO_DSA) || !defined(OPENSSL_NO_ECDSA)\n\tconst unsigned char *cp;\n\tX509_ALGOR *a;\n#endif\n\tif (key == NULL) goto err;\n\tif (key->pkey != NULL)\n\t\t{\n\t\tCRYPTO_add(&key->pkey->references, 1, CRYPTO_LOCK_EVP_PKEY);\n\t\treturn(key->pkey);\n\t\t}\n\tif (key->public_key == NULL) goto err;\n\ttype=OBJ_obj2nid(key->algor->algorithm);\n\tif ((ret = EVP_PKEY_new()) == NULL)\n\t\t{\n\t\tX509err(X509_F_X509_PUBKEY_GET, ERR_R_MALLOC_FAILURE);\n\t\tgoto err;\n\t\t}\n\tret->type = EVP_PKEY_type(type);\n#if !defined(OPENSSL_NO_DSA) || !defined(OPENSSL_NO_ECDSA)\n\ta=key->algor;\n#endif\n\tif (0)\n\t\t;\n#ifndef OPENSSL_NO_DSA\n\telse if (ret->type == EVP_PKEY_DSA)\n\t\t{\n\t\tif (a->parameter && (a->parameter->type == V_ASN1_SEQUENCE))\n\t\t\t{\n\t\t\tif ((ret->pkey.dsa = DSA_new()) == NULL)\n\t\t\t\t{\n\t\t\t\tX509err(X509_F_X509_PUBKEY_GET, ERR_R_MALLOC_FAILURE);\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\tret->pkey.dsa->write_params=0;\n\t\t\tcp=p=a->parameter->value.sequence->data;\n\t\t\tj=a->parameter->value.sequence->length;\n\t\t\tif (!d2i_DSAparams(&ret->pkey.dsa, &cp, (long)j))\n\t\t\t\tgoto err;\n\t\t\t}\n\t\tret->save_parameters=1;\n\t\t}\n#endif\n#ifndef OPENSSL_NO_EC\n\telse if (ret->type == EVP_PKEY_EC)\n\t\t{\n\t\tif (a->parameter && (a->parameter->type == V_ASN1_SEQUENCE))\n\t\t\t{\n\t\t\tif ((ret->pkey.ec= EC_KEY_new()) == NULL)\n\t\t\t\t{\n\t\t\t\tX509err(X509_F_X509_PUBKEY_GET,\n\t\t\t\t\tERR_R_MALLOC_FAILURE);\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\tcp = p = a->parameter->value.sequence->data;\n\t\t\tj = a->parameter->value.sequence->length;\n\t\t\tif (!d2i_ECParameters(&ret->pkey.ec, &cp, (long)j))\n\t\t\t\t{\n\t\t\t\tX509err(X509_F_X509_PUBKEY_GET, ERR_R_EC_LIB);\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\t}\n\t\telse if (a->parameter && (a->parameter->type == V_ASN1_OBJECT))\n\t\t\t{\n\t\t\tEC_KEY *ec_key;\n\t\t\tEC_GROUP *group;\n\t\t\tif (ret->pkey.ec == NULL)\n\t\t\t\tret->pkey.ec = EC_KEY_new();\n\t\t\tec_key = ret->pkey.ec;\n\t\t\tif (ec_key == NULL)\n\t\t\t\tgoto err;\n\t\t\tgroup = EC_GROUP_new_by_curve_name(OBJ_obj2nid(a->parameter->value.object));\n\t\t\tif (group == NULL)\n\t\t\t\tgoto err;\n\t\t\tEC_GROUP_set_asn1_flag(group, OPENSSL_EC_NAMED_CURVE);\n\t\t\tif (EC_KEY_set_group(ec_key, group) == 0)\n\t\t\t\tgoto err;\n\t\t\tEC_GROUP_free(group);\n\t\t\t}\n\t\tret->save_parameters = 1;\n\t\t}\n#endif\n\tp=key->public_key->data;\n j=key->public_key->length;\n if (!d2i_PublicKey(type, &ret, &p, (long)j))\n\t\t{\n\t\tX509err(X509_F_X509_PUBKEY_GET, X509_R_ERR_ASN1_LIB);\n\t\tgoto err;\n\t\t}\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}', 'int OBJ_obj2nid(const ASN1_OBJECT *a)\n\t{\n\tASN1_OBJECT **op;\n\tADDED_OBJ ad,*adp;\n\tif (a == NULL)\n\t\treturn(NID_undef);\n\tif (a->nid != 0)\n\t\treturn(a->nid);\n\tif (added != NULL)\n\t\t{\n\t\tad.type=ADDED_DATA;\n\t\tad.obj=(ASN1_OBJECT *)a;\n\t\tadp=(ADDED_OBJ *)lh_retrieve(added,&ad);\n\t\tif (adp != NULL) return (adp->obj->nid);\n\t\t}\n\top=(ASN1_OBJECT **)OBJ_bsearch((const char *)&a,(const char *)obj_objs,\n\t\tNUM_OBJ, sizeof(ASN1_OBJECT *),obj_cmp);\n\tif (op == NULL)\n\t\treturn(NID_undef);\n\treturn((*op)->nid);\n\t}', 'void *lh_retrieve(LHASH *lh, const void *data)\n\t{\n\tunsigned long hash;\n\tLHASH_NODE **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_retrieve_miss++;\n\t\treturn(NULL);\n\t\t}\n\telse\n\t\t{\n\t\tret= (*rn)->data;\n\t\tlh->num_retrieve++;\n\t\t}\n\treturn(ret);\n\t}', 'EVP_PKEY *EVP_PKEY_new(void)\n\t{\n\tEVP_PKEY *ret;\n\tret=(EVP_PKEY *)OPENSSL_malloc(sizeof(EVP_PKEY));\n\tif (ret == NULL)\n\t\t{\n\t\tEVPerr(EVP_F_EVP_PKEY_NEW,ERR_R_MALLOC_FAILURE);\n\t\treturn(NULL);\n\t\t}\n\tret->type=EVP_PKEY_NONE;\n\tret->references=1;\n\tret->pkey.ptr=NULL;\n\tret->attributes=NULL;\n\tret->save_parameters=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}', 'int EVP_PKEY_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from)\n\t{\n\tif (to->type != from->type)\n\t\t{\n\t\tEVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS,EVP_R_DIFFERENT_KEY_TYPES);\n\t\tgoto err;\n\t\t}\n\tif (EVP_PKEY_missing_parameters(from))\n\t\t{\n\t\tEVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS,EVP_R_MISSING_PARAMETERS);\n\t\tgoto err;\n\t\t}\n#ifndef OPENSSL_NO_DSA\n\tif (to->type == EVP_PKEY_DSA)\n\t\t{\n\t\tBIGNUM *a;\n\t\tif ((a=BN_dup(from->pkey.dsa->p)) == NULL) goto err;\n\t\tif (to->pkey.dsa->p != NULL) BN_free(to->pkey.dsa->p);\n\t\tto->pkey.dsa->p=a;\n\t\tif ((a=BN_dup(from->pkey.dsa->q)) == NULL) goto err;\n\t\tif (to->pkey.dsa->q != NULL) BN_free(to->pkey.dsa->q);\n\t\tto->pkey.dsa->q=a;\n\t\tif ((a=BN_dup(from->pkey.dsa->g)) == NULL) goto err;\n\t\tif (to->pkey.dsa->g != NULL) BN_free(to->pkey.dsa->g);\n\t\tto->pkey.dsa->g=a;\n\t\t}\n#endif\n#ifndef OPENSSL_NO_EC\n\tif (to->type == EVP_PKEY_EC)\n\t\t{\n\t\tEC_GROUP *group = EC_GROUP_dup(EC_KEY_get0_group(from->pkey.ec));\n\t\tif (group == NULL)\n\t\t\tgoto err;\n\t\tif (EC_KEY_set_group(to->pkey.ec, group) == 0)\n\t\t\tgoto err;\n\t\tEC_GROUP_free(group);\n\t\t}\n#endif\n\treturn(1);\nerr:\n\treturn(0);\n\t}']
|
2,178
| 0
|
https://github.com/openssl/openssl/blob/9b02dc97e4963969da69675a871dbe80e6d31cda/crypto/bn/bn_ctx.c/#L342
|
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--;
}
}
|
['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_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}', '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}']
|
2,179
| 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_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}', '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 a->flags &= ~BN_FLG_FIXED_TOP;\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.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_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 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}']
|
2,180
| 0
|
https://github.com/openssl/openssl/blob/ddc6a5c8f5900959bdbdfee79e1625a3f7808acd/test/drbgtest.c/#L308
|
static int error_check(DRBG_SELFTEST_DATA *td)
{
static char zero[sizeof(RAND_DRBG)];
RAND_DRBG *drbg = NULL;
TEST_CTX t;
unsigned char buff[1024];
unsigned int reseed_counter_tmp;
int ret = 0;
if (!TEST_ptr(drbg = RAND_DRBG_new(0, 0, NULL)))
goto err;
if (!init(drbg, td, &t)
|| RAND_DRBG_instantiate(drbg, td->pers, drbg->max_pers + 1) > 0)
goto err;
t.entlen = 0;
if (TEST_int_le(RAND_DRBG_instantiate(drbg, td->pers, td->perslen), 0))
goto err;
if (!TEST_false(RAND_DRBG_generate(drbg, buff, td->exlen, 0,
td->adin, td->adinlen))
|| !uninstantiate(drbg))
goto err;
t.entlen = drbg->min_entropy - 1;
if (!init(drbg, td, &t)
|| RAND_DRBG_instantiate(drbg, td->pers, td->perslen) > 0
|| !uninstantiate(drbg))
goto err;
t.entlen = drbg->max_entropy + 1;
if (!init(drbg, td, &t)
|| RAND_DRBG_instantiate(drbg, td->pers, td->perslen) > 0
|| !uninstantiate(drbg))
goto err;
if (drbg->min_nonce) {
t.noncelen = drbg->min_nonce - 1;
if (!init(drbg, td, &t)
|| RAND_DRBG_instantiate(drbg, td->pers, td->perslen) > 0
|| !uninstantiate(drbg))
goto err;
}
if (drbg->max_nonce) {
t.noncelen = drbg->max_nonce + 1;
if (!init(drbg, td, &t)
|| RAND_DRBG_instantiate(drbg, td->pers, td->perslen) > 0
|| !uninstantiate(drbg))
goto err;
}
if (!instantiate(drbg, td, &t)
|| !TEST_true(RAND_DRBG_generate(drbg, buff, td->exlen, 0,
td->adin, td->adinlen)))
goto err;
if (!TEST_false(RAND_DRBG_generate(drbg, buff, drbg->max_request + 1, 0,
td->adin, td->adinlen)))
goto err;
if (!TEST_false(RAND_DRBG_generate(drbg, buff, td->exlen, 0,
td->adin, drbg->max_adin + 1)))
goto err;
t.entlen = 0;
if (TEST_false(RAND_DRBG_generate(drbg, buff, td->exlen, 1,
td->adin, td->adinlen))
|| !uninstantiate(drbg))
goto err;
if (!instantiate(drbg, td, &t))
goto err;
reseed_counter_tmp = drbg->reseed_counter;
drbg->reseed_counter = drbg->reseed_interval;
t.entcnt = 0;
if (!TEST_true(RAND_DRBG_generate(drbg, buff, td->exlen, 0,
td->adin, td->adinlen))
|| !TEST_int_eq(t.entcnt, 1)
|| !TEST_int_eq(drbg->reseed_counter, reseed_counter_tmp + 1)
|| !uninstantiate(drbg))
goto err;
t.entlen = 0;
if (!TEST_false(RAND_DRBG_generate(drbg, buff, td->exlen, 1,
td->adin, td->adinlen))
|| !uninstantiate(drbg))
goto err;
if (!instantiate(drbg, td, &t))
goto err;
reseed_counter_tmp = drbg->reseed_counter;
drbg->reseed_counter = drbg->reseed_interval;
t.entcnt = 0;
if (!TEST_true(RAND_DRBG_generate(drbg, buff, td->exlen, 0,
td->adin, td->adinlen))
|| !TEST_int_eq(t.entcnt, 1)
|| !TEST_int_eq(drbg->reseed_counter, reseed_counter_tmp + 1)
|| !uninstantiate(drbg))
goto err;
if (!init(drbg, td, &t)
|| RAND_DRBG_reseed(drbg, td->adin, drbg->max_adin + 1) > 0)
goto err;
t.entlen = 0;
if (!TEST_int_le(RAND_DRBG_reseed(drbg, td->adin, td->adinlen), 0)
|| !uninstantiate(drbg))
goto err;
if (!init(drbg, td, &t))
goto err;
t.entlen = drbg->max_entropy + 1;
if (!TEST_int_le(RAND_DRBG_reseed(drbg, td->adin, td->adinlen), 0)
|| !uninstantiate(drbg))
goto err;
if (!init(drbg, td, &t))
goto err;
t.entlen = drbg->min_entropy - 1;
if (!TEST_int_le(RAND_DRBG_reseed(drbg, td->adin, td->adinlen), 0)
|| !uninstantiate(drbg))
goto err;
if (!TEST_mem_eq(zero, sizeof(drbg->ctr), &drbg->ctr, sizeof(drbg->ctr)))
goto err;
ret = 1;
err:
uninstantiate(drbg);
RAND_DRBG_free(drbg);
return ret;
}
|
['static int error_check(DRBG_SELFTEST_DATA *td)\n{\n static char zero[sizeof(RAND_DRBG)];\n RAND_DRBG *drbg = NULL;\n TEST_CTX t;\n unsigned char buff[1024];\n unsigned int reseed_counter_tmp;\n int ret = 0;\n if (!TEST_ptr(drbg = RAND_DRBG_new(0, 0, NULL)))\n goto err;\n if (!init(drbg, td, &t)\n || RAND_DRBG_instantiate(drbg, td->pers, drbg->max_pers + 1) > 0)\n goto err;\n t.entlen = 0;\n if (TEST_int_le(RAND_DRBG_instantiate(drbg, td->pers, td->perslen), 0))\n goto err;\n if (!TEST_false(RAND_DRBG_generate(drbg, buff, td->exlen, 0,\n td->adin, td->adinlen))\n || !uninstantiate(drbg))\n goto err;\n t.entlen = drbg->min_entropy - 1;\n if (!init(drbg, td, &t)\n || RAND_DRBG_instantiate(drbg, td->pers, td->perslen) > 0\n || !uninstantiate(drbg))\n goto err;\n t.entlen = drbg->max_entropy + 1;\n if (!init(drbg, td, &t)\n || RAND_DRBG_instantiate(drbg, td->pers, td->perslen) > 0\n || !uninstantiate(drbg))\n goto err;\n if (drbg->min_nonce) {\n t.noncelen = drbg->min_nonce - 1;\n if (!init(drbg, td, &t)\n || RAND_DRBG_instantiate(drbg, td->pers, td->perslen) > 0\n || !uninstantiate(drbg))\n goto err;\n }\n if (drbg->max_nonce) {\n t.noncelen = drbg->max_nonce + 1;\n if (!init(drbg, td, &t)\n || RAND_DRBG_instantiate(drbg, td->pers, td->perslen) > 0\n || !uninstantiate(drbg))\n goto err;\n }\n if (!instantiate(drbg, td, &t)\n || !TEST_true(RAND_DRBG_generate(drbg, buff, td->exlen, 0,\n td->adin, td->adinlen)))\n goto err;\n if (!TEST_false(RAND_DRBG_generate(drbg, buff, drbg->max_request + 1, 0,\n td->adin, td->adinlen)))\n goto err;\n if (!TEST_false(RAND_DRBG_generate(drbg, buff, td->exlen, 0,\n td->adin, drbg->max_adin + 1)))\n goto err;\n t.entlen = 0;\n if (TEST_false(RAND_DRBG_generate(drbg, buff, td->exlen, 1,\n td->adin, td->adinlen))\n || !uninstantiate(drbg))\n goto err;\n if (!instantiate(drbg, td, &t))\n goto err;\n reseed_counter_tmp = drbg->reseed_counter;\n drbg->reseed_counter = drbg->reseed_interval;\n t.entcnt = 0;\n if (!TEST_true(RAND_DRBG_generate(drbg, buff, td->exlen, 0,\n td->adin, td->adinlen))\n || !TEST_int_eq(t.entcnt, 1)\n || !TEST_int_eq(drbg->reseed_counter, reseed_counter_tmp + 1)\n || !uninstantiate(drbg))\n goto err;\n t.entlen = 0;\n if (!TEST_false(RAND_DRBG_generate(drbg, buff, td->exlen, 1,\n td->adin, td->adinlen))\n || !uninstantiate(drbg))\n goto err;\n if (!instantiate(drbg, td, &t))\n goto err;\n reseed_counter_tmp = drbg->reseed_counter;\n drbg->reseed_counter = drbg->reseed_interval;\n t.entcnt = 0;\n if (!TEST_true(RAND_DRBG_generate(drbg, buff, td->exlen, 0,\n td->adin, td->adinlen))\n || !TEST_int_eq(t.entcnt, 1)\n || !TEST_int_eq(drbg->reseed_counter, reseed_counter_tmp + 1)\n || !uninstantiate(drbg))\n goto err;\n if (!init(drbg, td, &t)\n || RAND_DRBG_reseed(drbg, td->adin, drbg->max_adin + 1) > 0)\n goto err;\n t.entlen = 0;\n if (!TEST_int_le(RAND_DRBG_reseed(drbg, td->adin, td->adinlen), 0)\n || !uninstantiate(drbg))\n goto err;\n if (!init(drbg, td, &t))\n goto err;\n t.entlen = drbg->max_entropy + 1;\n if (!TEST_int_le(RAND_DRBG_reseed(drbg, td->adin, td->adinlen), 0)\n || !uninstantiate(drbg))\n goto err;\n if (!init(drbg, td, &t))\n goto err;\n t.entlen = drbg->min_entropy - 1;\n if (!TEST_int_le(RAND_DRBG_reseed(drbg, td->adin, td->adinlen), 0)\n || !uninstantiate(drbg))\n goto err;\n if (!TEST_mem_eq(zero, sizeof(drbg->ctr), &drbg->ctr, sizeof(drbg->ctr)))\n goto err;\n ret = 1;\nerr:\n uninstantiate(drbg);\n RAND_DRBG_free(drbg);\n return ret;\n}', 'RAND_DRBG *RAND_DRBG_new(int type, unsigned int flags, RAND_DRBG *parent)\n{\n RAND_DRBG *drbg = OPENSSL_zalloc(sizeof(*drbg));\n unsigned char *ucp = OPENSSL_zalloc(RANDOMNESS_NEEDED);\n if (drbg == NULL || ucp == NULL) {\n RANDerr(RAND_F_RAND_DRBG_NEW, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n drbg->size = RANDOMNESS_NEEDED;\n drbg->randomness = ucp;\n drbg->parent = parent;\n if (RAND_DRBG_set(drbg, type, flags) < 0)\n goto err;\n if (parent != NULL) {\n if (parent->state == DRBG_UNINITIALISED\n && RAND_DRBG_instantiate(parent, NULL, 0) == 0)\n goto err;\n if (!RAND_DRBG_set_callbacks(drbg, drbg_entropy_from_parent,\n drbg_release_entropy,\n NULL, NULL)\n || !RAND_DRBG_instantiate(drbg,\n (unsigned char*)&drbg, sizeof(drbg)))\n goto err;\n }\n return drbg;\nerr:\n OPENSSL_free(ucp);\n OPENSSL_free(drbg);\n return NULL;\n}', 'int RAND_DRBG_set(RAND_DRBG *drbg, int nid, unsigned int flags)\n{\n int ret = 1;\n drbg->state = DRBG_UNINITIALISED;\n drbg->flags = flags;\n drbg->nid = nid;\n switch (nid) {\n default:\n RANDerr(RAND_F_RAND_DRBG_SET, RAND_R_UNSUPPORTED_DRBG_TYPE);\n return -2;\n case 0:\n return 1;\n case NID_aes_128_ctr:\n case NID_aes_192_ctr:\n case NID_aes_256_ctr:\n ret = ctr_init(drbg);\n break;\n }\n if (ret < 0)\n RANDerr(RAND_F_RAND_DRBG_SET, RAND_R_ERROR_INITIALISING_DRBG);\n return ret;\n}']
|
2,181
| 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_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx)\n{\n int i, bits, ret = 0;\n BIGNUM *v, *rr;\n if (BN_get_flags(p, BN_FLG_CONSTTIME) != 0\n || BN_get_flags(a, BN_FLG_CONSTTIME) != 0) {\n BNerr(BN_F_BN_EXP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);\n return 0;\n }\n BN_CTX_start(ctx);\n rr = ((r == a) || (r == p)) ? BN_CTX_get(ctx) : r;\n v = BN_CTX_get(ctx);\n if (rr == NULL || v == NULL)\n goto err;\n if (BN_copy(v, a) == NULL)\n goto err;\n bits = BN_num_bits(p);\n if (BN_is_odd(p)) {\n if (BN_copy(rr, a) == NULL)\n goto err;\n } else {\n if (!BN_one(rr))\n goto err;\n }\n for (i = 1; i < bits; i++) {\n if (!BN_sqr(v, v, ctx))\n goto err;\n if (BN_is_bit_set(p, i)) {\n if (!BN_mul(rr, rr, v, ctx))\n goto err;\n }\n }\n if (r != rr && BN_copy(r, rr) == 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("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_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}', '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}']
|
2,182
| 0
|
https://github.com/openssl/openssl/blob/09977dd095f3c655c99b9e1810a213f7eafa7364/crypto/bn/bn_lib.c/#L992
|
int BN_is_odd(const BIGNUM *a)
{
return (a->top > 0) && (a->d[0] & 1);
}
|
['int ec_GF2m_simple_oct2point(const EC_GROUP *group, EC_POINT *point,\n const unsigned char *buf, size_t len,\n BN_CTX *ctx)\n{\n point_conversion_form_t form;\n int y_bit;\n BN_CTX *new_ctx = NULL;\n BIGNUM *x, *y, *yxi;\n size_t field_len, enc_len;\n int ret = 0;\n if (len == 0) {\n ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_BUFFER_TOO_SMALL);\n return 0;\n }\n form = buf[0];\n y_bit = form & 1;\n form = form & ~1U;\n if ((form != 0) && (form != POINT_CONVERSION_COMPRESSED)\n && (form != POINT_CONVERSION_UNCOMPRESSED)\n && (form != POINT_CONVERSION_HYBRID)) {\n ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);\n return 0;\n }\n if ((form == 0 || form == POINT_CONVERSION_UNCOMPRESSED) && y_bit) {\n ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);\n return 0;\n }\n if (form == 0) {\n if (len != 1) {\n ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);\n return 0;\n }\n return EC_POINT_set_to_infinity(group, point);\n }\n field_len = (EC_GROUP_get_degree(group) + 7) / 8;\n enc_len =\n (form ==\n POINT_CONVERSION_COMPRESSED) ? 1 + field_len : 1 + 2 * field_len;\n if (len != enc_len) {\n ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);\n return 0;\n }\n if (ctx == NULL) {\n ctx = new_ctx = BN_CTX_new();\n if (ctx == NULL)\n return 0;\n }\n BN_CTX_start(ctx);\n x = BN_CTX_get(ctx);\n y = BN_CTX_get(ctx);\n yxi = BN_CTX_get(ctx);\n if (yxi == NULL)\n goto err;\n if (!BN_bin2bn(buf + 1, field_len, x))\n goto err;\n if (BN_ucmp(x, group->field) >= 0) {\n ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);\n goto err;\n }\n if (form == POINT_CONVERSION_COMPRESSED) {\n if (!EC_POINT_set_compressed_coordinates_GF2m\n (group, point, x, y_bit, ctx))\n goto err;\n } else {\n if (!BN_bin2bn(buf + 1 + field_len, field_len, y))\n goto err;\n if (BN_ucmp(y, group->field) >= 0) {\n ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);\n goto err;\n }\n if (form == POINT_CONVERSION_HYBRID) {\n if (!group->meth->field_div(group, yxi, y, x, ctx))\n goto err;\n if (y_bit != BN_is_odd(yxi)) {\n ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);\n goto err;\n }\n }\n if (!EC_POINT_set_affine_coordinates_GF2m(group, point, x, y, ctx))\n goto err;\n }\n if (EC_POINT_is_on_curve(group, point, ctx) <= 0) {\n ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_POINT_IS_NOT_ON_CURVE);\n goto err;\n }\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_is_odd(const BIGNUM *a)\n{\n return (a->top > 0) && (a->d[0] & 1);\n}']
|
2,183
| 0
|
https://github.com/openssl/openssl/blob/d43c4497ce1611373c3a3e5b433dfde4907d1a69/crypto/x509/x509_vfy.c/#L223
|
int X509_verify_cert(X509_STORE_CTX *ctx)
{
X509 *x,*xtmp,*chain_ss=NULL;
X509_NAME *xn;
int bad_chain = 0;
X509_VERIFY_PARAM *param = ctx->param;
int depth,i,ok=0;
int num;
int (*cb)(int xok,X509_STORE_CTX *xctx);
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=param->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);
(void)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);
bad_chain = 1;
ok=cb(0,ctx);
if (!ok) goto end;
}
else
{
X509_free(x);
x = xtmp;
(void)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;
bad_chain = 1;
ok=cb(0,ctx);
if (!ok) goto end;
}
ok = check_chain_extensions(ctx);
if (!ok) goto end;
ok = check_name_constraints(ctx);
if (!ok) goto end;
if (param->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(!ok) goto end;
#ifndef OPENSSL_NO_RFC3779
ok = v3_asid_validate_path(ctx);
if (!ok) goto end;
ok = v3_addr_validate_path(ctx);
if (!ok) goto end;
#endif
if (!bad_chain && (ctx->param->flags & X509_V_FLAG_POLICY_CHECK))
ok = ctx->check_policy(ctx);
if(!ok) goto end;
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 bad_chain = 0;\n\tX509_VERIFY_PARAM *param = ctx->param;\n\tint depth,i,ok=0;\n\tint num;\n\tint (*cb)(int xok,X509_STORE_CTX *xctx);\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=param->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\t(void)sk_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\tbad_chain = 1;\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\t(void)sk_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\tbad_chain = 1;\n\t\tok=cb(0,ctx);\n\t\tif (!ok) goto end;\n\t\t}\n\tok = check_chain_extensions(ctx);\n\tif (!ok) goto end;\n\tok = check_name_constraints(ctx);\n\tif (!ok) goto end;\n\tif (param->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(!ok) goto end;\n#ifndef OPENSSL_NO_RFC3779\n\tok = v3_asid_validate_path(ctx);\n\tif (!ok) goto end;\n\tok = v3_addr_validate_path(ctx);\n\tif (!ok) goto end;\n#endif\n\tif (!bad_chain && (ctx->param->flags & X509_V_FLAG_POLICY_CHECK))\n\t\tok = ctx->check_policy(ctx);\n\tif(!ok) goto end;\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}', 'int sk_num(const _STACK *st)\n{\n\tif(st == NULL) return -1;\n\treturn st->num;\n}', 'void *sk_value(const _STACK *st, int i)\n{\n\tif(!st || (i < 0) || (i >= st->num)) 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}', 'int sk_push(_STACK *st, void *data)\n\t{\n\treturn(sk_insert(st,data,st->num));\n\t}', 'int CRYPTO_add_lock(int *pointer, int amount, int type, const char *file,\n\t int line)\n\t{\n\tint ret = 0;\n\tif (add_lock_callback != NULL)\n\t\t{\n#ifdef LOCK_DEBUG\n\t\tint before= *pointer;\n#endif\n\t\tret=add_lock_callback(pointer,amount,type,file,line);\n#ifdef LOCK_DEBUG\n\t\t{\n\t\tCRYPTO_THREADID id;\n\t\tCRYPTO_THREADID_current(&id);\n\t\tfprintf(stderr,"ladd:%08lx:%2d+%2d->%2d %-18s %s:%d\\n",\n\t\t\tCRYPTO_THREADID_hash(&id), before,amount,ret,\n\t\t\tCRYPTO_get_lock_name(type),\n\t\t\tfile,line);\n\t\t}\n#endif\n\t\t}\n\telse\n\t\t{\n\t\tCRYPTO_lock(CRYPTO_LOCK|CRYPTO_WRITE,type,file,line);\n\t\tret= *pointer+amount;\n#ifdef LOCK_DEBUG\n\t\t{\n\t\tCRYPTO_THREADID id;\n\t\tCRYPTO_THREADID_current(&id);\n\t\tfprintf(stderr,"ladd:%08lx:%2d+%2d->%2d %-18s %s:%d\\n",\n\t\t\tCRYPTO_THREADID_hash(&id),\n\t\t\t*pointer,amount,ret,\n\t\t\tCRYPTO_get_lock_name(type),\n\t\t\tfile,line);\n\t\t}\n#endif\n\t\t*pointer=ret;\n\t\tCRYPTO_lock(CRYPTO_UNLOCK|CRYPTO_WRITE,type,file,line);\n\t\t}\n\treturn(ret);\n\t}', 'void CRYPTO_lock(int mode, int type, const char *file, int line)\n\t{\n#ifdef LOCK_DEBUG\n\t\t{\n\t\tCRYPTO_THREADID id;\n\t\tchar *rw_text,*operation_text;\n\t\tif (mode & CRYPTO_LOCK)\n\t\t\toperation_text="lock ";\n\t\telse if (mode & CRYPTO_UNLOCK)\n\t\t\toperation_text="unlock";\n\t\telse\n\t\t\toperation_text="ERROR ";\n\t\tif (mode & CRYPTO_READ)\n\t\t\trw_text="r";\n\t\telse if (mode & CRYPTO_WRITE)\n\t\t\trw_text="w";\n\t\telse\n\t\t\trw_text="ERROR";\n\t\tCRYPTO_THREADID_current(&id);\n\t\tfprintf(stderr,"lock:%08lx:(%s)%s %-18s %s:%d\\n",\n\t\t\tCRYPTO_THREADID_hash(&id), rw_text, operation_text,\n\t\t\tCRYPTO_get_lock_name(type), file, line);\n\t\t}\n#endif\n\tif (type < 0)\n\t\t{\n\t\tif (dynlock_lock_callback != NULL)\n\t\t\t{\n\t\t\tstruct CRYPTO_dynlock_value *pointer\n\t\t\t\t= CRYPTO_get_dynlock_value(type);\n\t\t\tOPENSSL_assert(pointer != NULL);\n\t\t\tdynlock_lock_callback(mode, pointer, file, line);\n\t\t\tCRYPTO_destroy_dynlockid(type);\n\t\t\t}\n\t\t}\n\telse\n\t\tif (locking_callback != NULL)\n\t\t\tlocking_callback(mode,type,file,line);\n\t}', 'void *sk_delete_ptr(_STACK *st, void *p)\n\t{\n\tint i;\n\tfor (i=0; i<st->num; i++)\n\t\tif (st->data[i] == p)\n\t\t\treturn(sk_delete(st,i));\n\treturn(NULL);\n\t}']
|
2,184
| 0
|
https://github.com/openssl/openssl/blob/5dfc369ffcdc4722482c818e6ba6cf6e704c2cb5/crypto/bn/bn_prime.c/#L193
|
int BN_is_prime(BIGNUM *a, int checks, void (*callback)(int,int,char *),
BN_CTX *ctx_passed, char *cb_arg)
{
int i,j,c2=0,ret= -1;
BIGNUM *check;
BN_CTX *ctx=NULL,*ctx2=NULL;
BN_MONT_CTX *mont=NULL;
if (!BN_is_odd(a))
return(0);
if (ctx_passed != NULL)
ctx=ctx_passed;
else
if ((ctx=BN_CTX_new()) == NULL) goto err;
if ((ctx2=BN_CTX_new()) == NULL) goto err;
if ((mont=BN_MONT_CTX_new()) == NULL) goto err;
check= &(ctx->bn[ctx->tos++]);
if (!BN_MONT_CTX_set(mont,a,ctx2)) goto err;
for (i=0; i<checks; i++)
{
if (!BN_rand(check,BN_num_bits(a)-1,0,0)) goto err;
j=witness(check,a,ctx,ctx2,mont);
if (j == -1) goto err;
if (j)
{
ret=0;
goto err;
}
if (callback != NULL) callback(1,c2++,cb_arg);
}
ret=1;
err:
ctx->tos--;
if ((ctx_passed == NULL) && (ctx != NULL))
BN_CTX_free(ctx);
if (ctx2 != NULL)
BN_CTX_free(ctx2);
if (mont != NULL) BN_MONT_CTX_free(mont);
return(ret);
}
|
['int BN_is_prime(BIGNUM *a, int checks, void (*callback)(int,int,char *),\n\t BN_CTX *ctx_passed, char *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_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}', '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}']
|
2,185
| 0
|
https://github.com/libav/libav/blob/fa0912fe50e59df72b7bf81f8838d2c6d9780343/libavformat/mxfenc.c/#L1211
|
static void mxf_write_partition(AVFormatContext *s, int bodysid,
int indexsid,
const uint8_t *key, int write_metadata)
{
MXFContext *mxf = s->priv_data;
ByteIOContext *pb = s->pb;
int64_t header_byte_count_offset;
unsigned index_byte_count = 0;
uint64_t partition_offset = url_ftell(pb);
if (!mxf->edit_unit_byte_count && mxf->edit_units_count)
index_byte_count = 85 + 12+(s->nb_streams+1)*6 +
12+mxf->edit_units_count*(11+mxf->slice_count*4);
else if (mxf->edit_unit_byte_count && indexsid)
index_byte_count = 80;
if (index_byte_count) {
index_byte_count += 16 + klv_ber_length(index_byte_count);
index_byte_count += klv_fill_size(index_byte_count);
}
if (!memcmp(key, body_partition_key, 16)) {
mxf->body_partition_offset =
av_realloc(mxf->body_partition_offset,
(mxf->body_partitions_count+1)*
sizeof(*mxf->body_partition_offset));
mxf->body_partition_offset[mxf->body_partitions_count++] = partition_offset;
}
put_buffer(pb, key, 16);
klv_encode_ber_length(pb, 88 + 16 * mxf->essence_container_count);
put_be16(pb, 1);
put_be16(pb, 2);
put_be32(pb, KAG_SIZE);
put_be64(pb, partition_offset);
if (!memcmp(key, body_partition_key, 16) && mxf->body_partitions_count > 1)
put_be64(pb, mxf->body_partition_offset[mxf->body_partitions_count-2]);
else if (!memcmp(key, footer_partition_key, 16) && mxf->body_partitions_count)
put_be64(pb, mxf->body_partition_offset[mxf->body_partitions_count-1]);
else
put_be64(pb, 0);
put_be64(pb, mxf->footer_partition_offset);
header_byte_count_offset = url_ftell(pb);
put_be64(pb, 0);
put_be64(pb, index_byte_count);
put_be32(pb, index_byte_count ? indexsid : 0);
if (bodysid && mxf->edit_units_count && mxf->body_partitions_count) {
put_be64(pb, mxf->body_offset);
} else
put_be64(pb, 0);
put_be32(pb, bodysid);
put_buffer(pb, op1a_ul, 16);
mxf_write_essence_container_refs(s);
if (write_metadata) {
int64_t pos, start;
unsigned header_byte_count;
mxf_write_klv_fill(s);
start = url_ftell(s->pb);
mxf_write_primer_pack(s);
mxf_write_header_metadata_sets(s);
pos = url_ftell(s->pb);
header_byte_count = pos - start + klv_fill_size(pos);
url_fseek(pb, header_byte_count_offset, SEEK_SET);
put_be64(pb, header_byte_count);
url_fseek(pb, pos, SEEK_SET);
}
put_flush_packet(pb);
}
|
['static void mxf_write_partition(AVFormatContext *s, int bodysid,\n int indexsid,\n const uint8_t *key, int write_metadata)\n{\n MXFContext *mxf = s->priv_data;\n ByteIOContext *pb = s->pb;\n int64_t header_byte_count_offset;\n unsigned index_byte_count = 0;\n uint64_t partition_offset = url_ftell(pb);\n if (!mxf->edit_unit_byte_count && mxf->edit_units_count)\n index_byte_count = 85 + 12+(s->nb_streams+1)*6 +\n 12+mxf->edit_units_count*(11+mxf->slice_count*4);\n else if (mxf->edit_unit_byte_count && indexsid)\n index_byte_count = 80;\n if (index_byte_count) {\n index_byte_count += 16 + klv_ber_length(index_byte_count);\n index_byte_count += klv_fill_size(index_byte_count);\n }\n if (!memcmp(key, body_partition_key, 16)) {\n mxf->body_partition_offset =\n av_realloc(mxf->body_partition_offset,\n (mxf->body_partitions_count+1)*\n sizeof(*mxf->body_partition_offset));\n mxf->body_partition_offset[mxf->body_partitions_count++] = partition_offset;\n }\n put_buffer(pb, key, 16);\n klv_encode_ber_length(pb, 88 + 16 * mxf->essence_container_count);\n put_be16(pb, 1);\n put_be16(pb, 2);\n put_be32(pb, KAG_SIZE);\n put_be64(pb, partition_offset);\n if (!memcmp(key, body_partition_key, 16) && mxf->body_partitions_count > 1)\n put_be64(pb, mxf->body_partition_offset[mxf->body_partitions_count-2]);\n else if (!memcmp(key, footer_partition_key, 16) && mxf->body_partitions_count)\n put_be64(pb, mxf->body_partition_offset[mxf->body_partitions_count-1]);\n else\n put_be64(pb, 0);\n put_be64(pb, mxf->footer_partition_offset);\n header_byte_count_offset = url_ftell(pb);\n put_be64(pb, 0);\n put_be64(pb, index_byte_count);\n put_be32(pb, index_byte_count ? indexsid : 0);\n if (bodysid && mxf->edit_units_count && mxf->body_partitions_count) {\n put_be64(pb, mxf->body_offset);\n } else\n put_be64(pb, 0);\n put_be32(pb, bodysid);\n put_buffer(pb, op1a_ul, 16);\n mxf_write_essence_container_refs(s);\n if (write_metadata) {\n int64_t pos, start;\n unsigned header_byte_count;\n mxf_write_klv_fill(s);\n start = url_ftell(s->pb);\n mxf_write_primer_pack(s);\n mxf_write_header_metadata_sets(s);\n pos = url_ftell(s->pb);\n header_byte_count = pos - start + klv_fill_size(pos);\n url_fseek(pb, header_byte_count_offset, SEEK_SET);\n put_be64(pb, header_byte_count);\n url_fseek(pb, pos, SEEK_SET);\n }\n put_flush_packet(pb);\n}', 'int64_t url_ftell(ByteIOContext *s)\n{\n return url_fseek(s, 0, SEEK_CUR);\n}', 'static int klv_ber_length(uint64_t len)\n{\n if (len < 128)\n return 1;\n else\n return (av_log2(len) >> 3) + 2;\n}', 'static inline av_const int av_log2(unsigned int v)\n{\n int n = 0;\n if (v & 0xffff0000) {\n v >>= 16;\n n += 16;\n }\n if (v & 0xff00) {\n v >>= 8;\n n += 8;\n }\n n += ff_log2_tab[v];\n return n;\n}', 'static unsigned klv_fill_size(uint64_t size)\n{\n unsigned pad = KAG_SIZE - (size & (KAG_SIZE-1));\n if (pad < 20)\n return pad + KAG_SIZE;\n else\n return pad & (KAG_SIZE-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}']
|
2,186
| 0
|
https://github.com/openssl/openssl/blob/d40a1b865fddc3d67f8c06ff1f1466fad331c8f7/crypto/bn/bn_add.c/#L230
|
int BN_usub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)
{
size_t max,min,dif;
register BN_ULONG t1,t2,*ap,*bp,*rp;
int i,carry;
#if defined(IRIX_CC_BUG) && !defined(LINT)
int dummy;
#endif
bn_check_top(a);
bn_check_top(b);
max = a->top;
min = b->top;
dif = max - min;
if (dif < 0)
{
BNerr(BN_F_BN_USUB,BN_R_ARG2_LT_ARG3);
return(0);
}
if (bn_wexpand(r,max) == NULL) return(0);
ap=a->d;
bp=b->d;
rp=r->d;
#if 1
carry=0;
for (i = min; i != 0; i--)
{
t1= *(ap++);
t2= *(bp++);
if (carry)
{
carry=(t1 <= t2);
t1=(t1-t2-1)&BN_MASK2;
}
else
{
carry=(t1 < t2);
t1=(t1-t2)&BN_MASK2;
}
#if defined(IRIX_CC_BUG) && !defined(LINT)
dummy=t1;
#endif
*(rp++)=t1&BN_MASK2;
}
#else
carry=bn_sub_words(rp,ap,bp,min);
ap+=min;
bp+=min;
rp+=min;
#endif
if (carry)
{
if (!dif)
return 0;
while (dif)
{
dif--;
t1 = *(ap++);
t2 = (t1-1)&BN_MASK2;
*(rp++) = t2;
if (t1)
break;
}
}
#if 0
memcpy(rp,ap,sizeof(*rp)*(max-i));
#else
if (rp != ap)
{
for (;;)
{
if (!dif--) break;
rp[0]=ap[0];
if (!dif--) break;
rp[1]=ap[1];
if (!dif--) break;
rp[2]=ap[2];
if (!dif--) break;
rp[3]=ap[3];
rp+=4;
ap+=4;
}
}
#endif
r->top=max;
r->neg=0;
bn_correct_top(r);
return(1);
}
|
['int BN_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)\n\t{\n\tconst BIGNUM *tmp;\n\tint a_neg = a->neg, ret;\n\tbn_check_top(a);\n\tbn_check_top(b);\n\tif (a_neg ^ b->neg)\n\t\t{\n\t\tif (a_neg)\n\t\t\t{ tmp=a; a=b; b=tmp; }\n\t\tif (BN_ucmp(a,b) < 0)\n\t\t\t{\n\t\t\tif (!BN_usub(r,b,a)) return(0);\n\t\t\tr->neg=1;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tif (!BN_usub(r,a,b)) return(0);\n\t\t\tr->neg=0;\n\t\t\t}\n\t\treturn(1);\n\t\t}\n\tret = BN_uadd(r,a,b);\n\tr->neg = a_neg;\n\tbn_check_top(r);\n\treturn ret;\n\t}', 'int BN_ucmp(const BIGNUM *a, const BIGNUM *b)\n\t{\n\tint i;\n\tBN_ULONG t1,t2,*ap,*bp;\n\tbn_check_top(a);\n\tbn_check_top(b);\n\ti=a->top-b->top;\n\tif (i != 0) return(i);\n\tap=a->d;\n\tbp=b->d;\n\tfor (i=a->top-1; i>=0; i--)\n\t\t{\n\t\tt1= ap[i];\n\t\tt2= bp[i];\n\t\tif (t1 != t2)\n\t\t\treturn((t1 > t2) ? 1 : -1);\n\t\t}\n\treturn(0);\n\t}', 'int BN_usub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)\n\t{\n\tsize_t max,min,dif;\n\tregister BN_ULONG t1,t2,*ap,*bp,*rp;\n\tint i,carry;\n#if defined(IRIX_CC_BUG) && !defined(LINT)\n\tint dummy;\n#endif\n\tbn_check_top(a);\n\tbn_check_top(b);\n\tmax = a->top;\n\tmin = b->top;\n\tdif = max - min;\n\tif (dif < 0)\n\t\t{\n\t\tBNerr(BN_F_BN_USUB,BN_R_ARG2_LT_ARG3);\n\t\treturn(0);\n\t\t}\n\tif (bn_wexpand(r,max) == NULL) return(0);\n\tap=a->d;\n\tbp=b->d;\n\trp=r->d;\n#if 1\n\tcarry=0;\n\tfor (i = min; i != 0; i--)\n\t\t{\n\t\tt1= *(ap++);\n\t\tt2= *(bp++);\n\t\tif (carry)\n\t\t\t{\n\t\t\tcarry=(t1 <= t2);\n\t\t\tt1=(t1-t2-1)&BN_MASK2;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tcarry=(t1 < t2);\n\t\t\tt1=(t1-t2)&BN_MASK2;\n\t\t\t}\n#if defined(IRIX_CC_BUG) && !defined(LINT)\n\t\tdummy=t1;\n#endif\n\t\t*(rp++)=t1&BN_MASK2;\n\t\t}\n#else\n\tcarry=bn_sub_words(rp,ap,bp,min);\n\tap+=min;\n\tbp+=min;\n\trp+=min;\n#endif\n\tif (carry)\n\t\t{\n\t\tif (!dif)\n\t\t\treturn 0;\n\t\twhile (dif)\n\t\t\t{\n\t\t\tdif--;\n\t\t\tt1 = *(ap++);\n\t\t\tt2 = (t1-1)&BN_MASK2;\n\t\t\t*(rp++) = t2;\n\t\t\tif (t1)\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n#if 0\n\tmemcpy(rp,ap,sizeof(*rp)*(max-i));\n#else\n\tif (rp != ap)\n\t\t{\n\t\tfor (;;)\n\t\t\t{\n\t\t\tif (!dif--) break;\n\t\t\trp[0]=ap[0];\n\t\t\tif (!dif--) break;\n\t\t\trp[1]=ap[1];\n\t\t\tif (!dif--) break;\n\t\t\trp[2]=ap[2];\n\t\t\tif (!dif--) break;\n\t\t\trp[3]=ap[3];\n\t\t\trp+=4;\n\t\t\tap+=4;\n\t\t\t}\n\t\t}\n#endif\n\tr->top=max;\n\tr->neg=0;\n\tbn_correct_top(r);\n\treturn(1);\n\t}']
|
2,187
| 1
|
https://github.com/libav/libav/blob/63e8d9760f23a4edf81e9ae58c4f6d3baa6ff4dd/libavcodec/dct32.c/#L262
|
static void dct32(INTFLOAT *out, const INTFLOAT *tab)
{
INTFLOAT tmp0, tmp1;
INTFLOAT val0 , val1 , val2 , val3 , val4 , val5 , val6 , val7 ,
val8 , val9 , val10, val11, val12, val13, val14, val15,
val16, val17, val18, val19, val20, val21, val22, val23,
val24, val25, val26, val27, val28, val29, val30, val31;
BF0( 0, 31, COS0_0 , 1);
BF0(15, 16, COS0_15, 5);
BF( 0, 15, COS1_0 , 1);
BF(16, 31,-COS1_0 , 1);
BF0( 7, 24, COS0_7 , 1);
BF0( 8, 23, COS0_8 , 1);
BF( 7, 8, COS1_7 , 4);
BF(23, 24,-COS1_7 , 4);
BF( 0, 7, COS2_0 , 1);
BF( 8, 15,-COS2_0 , 1);
BF(16, 23, COS2_0 , 1);
BF(24, 31,-COS2_0 , 1);
BF0( 3, 28, COS0_3 , 1);
BF0(12, 19, COS0_12, 2);
BF( 3, 12, COS1_3 , 1);
BF(19, 28,-COS1_3 , 1);
BF0( 4, 27, COS0_4 , 1);
BF0(11, 20, COS0_11, 2);
BF( 4, 11, COS1_4 , 1);
BF(20, 27,-COS1_4 , 1);
BF( 3, 4, COS2_3 , 3);
BF(11, 12,-COS2_3 , 3);
BF(19, 20, COS2_3 , 3);
BF(27, 28,-COS2_3 , 3);
BF( 0, 3, COS3_0 , 1);
BF( 4, 7,-COS3_0 , 1);
BF( 8, 11, COS3_0 , 1);
BF(12, 15,-COS3_0 , 1);
BF(16, 19, COS3_0 , 1);
BF(20, 23,-COS3_0 , 1);
BF(24, 27, COS3_0 , 1);
BF(28, 31,-COS3_0 , 1);
BF0( 1, 30, COS0_1 , 1);
BF0(14, 17, COS0_14, 3);
BF( 1, 14, COS1_1 , 1);
BF(17, 30,-COS1_1 , 1);
BF0( 6, 25, COS0_6 , 1);
BF0( 9, 22, COS0_9 , 1);
BF( 6, 9, COS1_6 , 2);
BF(22, 25,-COS1_6 , 2);
BF( 1, 6, COS2_1 , 1);
BF( 9, 14,-COS2_1 , 1);
BF(17, 22, COS2_1 , 1);
BF(25, 30,-COS2_1 , 1);
BF0( 2, 29, COS0_2 , 1);
BF0(13, 18, COS0_13, 3);
BF( 2, 13, COS1_2 , 1);
BF(18, 29,-COS1_2 , 1);
BF0( 5, 26, COS0_5 , 1);
BF0(10, 21, COS0_10, 1);
BF( 5, 10, COS1_5 , 2);
BF(21, 26,-COS1_5 , 2);
BF( 2, 5, COS2_2 , 1);
BF(10, 13,-COS2_2 , 1);
BF(18, 21, COS2_2 , 1);
BF(26, 29,-COS2_2 , 1);
BF( 1, 2, COS3_1 , 2);
BF( 5, 6,-COS3_1 , 2);
BF( 9, 10, COS3_1 , 2);
BF(13, 14,-COS3_1 , 2);
BF(17, 18, COS3_1 , 2);
BF(21, 22,-COS3_1 , 2);
BF(25, 26, COS3_1 , 2);
BF(29, 30,-COS3_1 , 2);
BF1( 0, 1, 2, 3);
BF2( 4, 5, 6, 7);
BF1( 8, 9, 10, 11);
BF2(12, 13, 14, 15);
BF1(16, 17, 18, 19);
BF2(20, 21, 22, 23);
BF1(24, 25, 26, 27);
BF2(28, 29, 30, 31);
ADD( 8, 12);
ADD(12, 10);
ADD(10, 14);
ADD(14, 9);
ADD( 9, 13);
ADD(13, 11);
ADD(11, 15);
out[ 0] = val0;
out[16] = val1;
out[ 8] = val2;
out[24] = val3;
out[ 4] = val4;
out[20] = val5;
out[12] = val6;
out[28] = val7;
out[ 2] = val8;
out[18] = val9;
out[10] = val10;
out[26] = val11;
out[ 6] = val12;
out[22] = val13;
out[14] = val14;
out[30] = val15;
ADD(24, 28);
ADD(28, 26);
ADD(26, 30);
ADD(30, 25);
ADD(25, 29);
ADD(29, 27);
ADD(27, 31);
out[ 1] = val16 + val24;
out[17] = val17 + val25;
out[ 9] = val18 + val26;
out[25] = val19 + val27;
out[ 5] = val20 + val28;
out[21] = val21 + val29;
out[13] = val22 + val30;
out[29] = val23 + val31;
out[ 3] = val24 + val20;
out[19] = val25 + val21;
out[11] = val26 + val22;
out[27] = val27 + val23;
out[ 7] = val28 + val18;
out[23] = val29 + val19;
out[15] = val30 + val17;
out[31] = val31;
}
|
['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 dct32(INTFLOAT *out, const INTFLOAT *tab)\n{\n INTFLOAT tmp0, tmp1;\n INTFLOAT val0 , val1 , val2 , val3 , val4 , val5 , val6 , val7 ,\n val8 , val9 , val10, val11, val12, val13, val14, val15,\n val16, val17, val18, val19, val20, val21, val22, val23,\n val24, val25, val26, val27, val28, val29, val30, val31;\n BF0( 0, 31, COS0_0 , 1);\n BF0(15, 16, COS0_15, 5);\n BF( 0, 15, COS1_0 , 1);\n BF(16, 31,-COS1_0 , 1);\n BF0( 7, 24, COS0_7 , 1);\n BF0( 8, 23, COS0_8 , 1);\n BF( 7, 8, COS1_7 , 4);\n BF(23, 24,-COS1_7 , 4);\n BF( 0, 7, COS2_0 , 1);\n BF( 8, 15,-COS2_0 , 1);\n BF(16, 23, COS2_0 , 1);\n BF(24, 31,-COS2_0 , 1);\n BF0( 3, 28, COS0_3 , 1);\n BF0(12, 19, COS0_12, 2);\n BF( 3, 12, COS1_3 , 1);\n BF(19, 28,-COS1_3 , 1);\n BF0( 4, 27, COS0_4 , 1);\n BF0(11, 20, COS0_11, 2);\n BF( 4, 11, COS1_4 , 1);\n BF(20, 27,-COS1_4 , 1);\n BF( 3, 4, COS2_3 , 3);\n BF(11, 12,-COS2_3 , 3);\n BF(19, 20, COS2_3 , 3);\n BF(27, 28,-COS2_3 , 3);\n BF( 0, 3, COS3_0 , 1);\n BF( 4, 7,-COS3_0 , 1);\n BF( 8, 11, COS3_0 , 1);\n BF(12, 15,-COS3_0 , 1);\n BF(16, 19, COS3_0 , 1);\n BF(20, 23,-COS3_0 , 1);\n BF(24, 27, COS3_0 , 1);\n BF(28, 31,-COS3_0 , 1);\n BF0( 1, 30, COS0_1 , 1);\n BF0(14, 17, COS0_14, 3);\n BF( 1, 14, COS1_1 , 1);\n BF(17, 30,-COS1_1 , 1);\n BF0( 6, 25, COS0_6 , 1);\n BF0( 9, 22, COS0_9 , 1);\n BF( 6, 9, COS1_6 , 2);\n BF(22, 25,-COS1_6 , 2);\n BF( 1, 6, COS2_1 , 1);\n BF( 9, 14,-COS2_1 , 1);\n BF(17, 22, COS2_1 , 1);\n BF(25, 30,-COS2_1 , 1);\n BF0( 2, 29, COS0_2 , 1);\n BF0(13, 18, COS0_13, 3);\n BF( 2, 13, COS1_2 , 1);\n BF(18, 29,-COS1_2 , 1);\n BF0( 5, 26, COS0_5 , 1);\n BF0(10, 21, COS0_10, 1);\n BF( 5, 10, COS1_5 , 2);\n BF(21, 26,-COS1_5 , 2);\n BF( 2, 5, COS2_2 , 1);\n BF(10, 13,-COS2_2 , 1);\n BF(18, 21, COS2_2 , 1);\n BF(26, 29,-COS2_2 , 1);\n BF( 1, 2, COS3_1 , 2);\n BF( 5, 6,-COS3_1 , 2);\n BF( 9, 10, COS3_1 , 2);\n BF(13, 14,-COS3_1 , 2);\n BF(17, 18, COS3_1 , 2);\n BF(21, 22,-COS3_1 , 2);\n BF(25, 26, COS3_1 , 2);\n BF(29, 30,-COS3_1 , 2);\n BF1( 0, 1, 2, 3);\n BF2( 4, 5, 6, 7);\n BF1( 8, 9, 10, 11);\n BF2(12, 13, 14, 15);\n BF1(16, 17, 18, 19);\n BF2(20, 21, 22, 23);\n BF1(24, 25, 26, 27);\n BF2(28, 29, 30, 31);\n ADD( 8, 12);\n ADD(12, 10);\n ADD(10, 14);\n ADD(14, 9);\n ADD( 9, 13);\n ADD(13, 11);\n ADD(11, 15);\n out[ 0] = val0;\n out[16] = val1;\n out[ 8] = val2;\n out[24] = val3;\n out[ 4] = val4;\n out[20] = val5;\n out[12] = val6;\n out[28] = val7;\n out[ 2] = val8;\n out[18] = val9;\n out[10] = val10;\n out[26] = val11;\n out[ 6] = val12;\n out[22] = val13;\n out[14] = val14;\n out[30] = val15;\n ADD(24, 28);\n ADD(28, 26);\n ADD(26, 30);\n ADD(30, 25);\n ADD(25, 29);\n ADD(29, 27);\n ADD(27, 31);\n out[ 1] = val16 + val24;\n out[17] = val17 + val25;\n out[ 9] = val18 + val26;\n out[25] = val19 + val27;\n out[ 5] = val20 + val28;\n out[21] = val21 + val29;\n out[13] = val22 + val30;\n out[29] = val23 + val31;\n out[ 3] = val24 + val20;\n out[19] = val25 + val21;\n out[11] = val26 + val22;\n out[27] = val27 + val23;\n out[ 7] = val28 + val18;\n out[23] = val29 + val19;\n out[15] = val30 + val17;\n out[31] = val31;\n}']
|
2,188
| 0
|
https://github.com/openssl/openssl/blob/5ea564f154ebe8bda2a0e091a312e2058edf437f/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 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 *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}", '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 (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}']
|
2,189
| 0
|
https://github.com/openssl/openssl/blob/f006217bb628d05a2d5b866ff252bd94e3477e1f/crypto/bn/bn_lib.c/#L351
|
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_malloc(words * sizeof(*a));
else
a = A = OPENSSL_malloc(words * sizeof(*a));
if (A == NULL) {
BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);
return (NULL);
}
#ifdef PURIFY
memset(a, 0, sizeof(*a) * words);
#endif
#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 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}', '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}', '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_malloc(words * sizeof(*a));\n else\n a = A = OPENSSL_malloc(words * sizeof(*a));\n if (A == NULL) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);\n return (NULL);\n }\n#ifdef PURIFY\n memset(a, 0, sizeof(*a) * words);\n#endif\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}']
|
2,190
| 0
|
https://github.com/libav/libav/blob/25b6837f7cacd691b19cbc12b9dad1ce84a318a1/libavcodec/vp8dsp.c/#L73
|
static void vp7_luma_dc_wht_c(int16_t block[4][4][16], int16_t dc[16])
{
int i, a1, b1, c1, d1;
int16_t tmp[16];
for (i = 0; i < 4; i++) {
a1 = (dc[i * 4 + 0] + dc[i * 4 + 2]) * 23170;
b1 = (dc[i * 4 + 0] - dc[i * 4 + 2]) * 23170;
c1 = dc[i * 4 + 1] * 12540 - dc[i * 4 + 3] * 30274;
d1 = dc[i * 4 + 1] * 30274 + dc[i * 4 + 3] * 12540;
tmp[i * 4 + 0] = (a1 + d1) >> 14;
tmp[i * 4 + 3] = (a1 - d1) >> 14;
tmp[i * 4 + 1] = (b1 + c1) >> 14;
tmp[i * 4 + 2] = (b1 - c1) >> 14;
}
for (i = 0; i < 4; i++) {
a1 = (tmp[i + 0] + tmp[i + 8]) * 23170;
b1 = (tmp[i + 0] - tmp[i + 8]) * 23170;
c1 = tmp[i + 4] * 12540 - tmp[i + 12] * 30274;
d1 = tmp[i + 4] * 30274 + tmp[i + 12] * 12540;
dc[i * 4 + 0] = 0;
dc[i * 4 + 1] = 0;
dc[i * 4 + 2] = 0;
dc[i * 4 + 3] = 0;
block[0][i][0] = (a1 + d1 + 0x20000) >> 18;
block[3][i][0] = (a1 - d1 + 0x20000) >> 18;
block[1][i][0] = (b1 + c1 + 0x20000) >> 18;
block[2][i][0] = (b1 - c1 + 0x20000) >> 18;
}
}
|
['static void vp7_luma_dc_wht_c(int16_t block[4][4][16], int16_t dc[16])\n{\n int i, a1, b1, c1, d1;\n int16_t tmp[16];\n for (i = 0; i < 4; i++) {\n a1 = (dc[i * 4 + 0] + dc[i * 4 + 2]) * 23170;\n b1 = (dc[i * 4 + 0] - dc[i * 4 + 2]) * 23170;\n c1 = dc[i * 4 + 1] * 12540 - dc[i * 4 + 3] * 30274;\n d1 = dc[i * 4 + 1] * 30274 + dc[i * 4 + 3] * 12540;\n tmp[i * 4 + 0] = (a1 + d1) >> 14;\n tmp[i * 4 + 3] = (a1 - d1) >> 14;\n tmp[i * 4 + 1] = (b1 + c1) >> 14;\n tmp[i * 4 + 2] = (b1 - c1) >> 14;\n }\n for (i = 0; i < 4; i++) {\n a1 = (tmp[i + 0] + tmp[i + 8]) * 23170;\n b1 = (tmp[i + 0] - tmp[i + 8]) * 23170;\n c1 = tmp[i + 4] * 12540 - tmp[i + 12] * 30274;\n d1 = tmp[i + 4] * 30274 + tmp[i + 12] * 12540;\n dc[i * 4 + 0] = 0;\n dc[i * 4 + 1] = 0;\n dc[i * 4 + 2] = 0;\n dc[i * 4 + 3] = 0;\n block[0][i][0] = (a1 + d1 + 0x20000) >> 18;\n block[3][i][0] = (a1 - d1 + 0x20000) >> 18;\n block[1][i][0] = (b1 + c1 + 0x20000) >> 18;\n block[2][i][0] = (b1 - c1 + 0x20000) >> 18;\n }\n}']
|
2,191
| 0
|
https://github.com/openssl/openssl/blob/55525742f4c2bf416013fc3a75ec642775d97f80/crypto/bn/bn_ctx.c/#L440
|
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)
{
offset = BN_CTX_POOL_SIZE - 1;
p->current = p->current->prev;
}
else
offset--;
}
}
|
['int BN_mod_exp_simple(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,\n\t\tconst BIGNUM *m, BN_CTX *ctx)\n\t{\n\tint i,j,bits,ret=0,wstart,wend,window,wvalue;\n\tint start=1;\n\tBIGNUM *d;\n\tBIGNUM *val[TABLE_SIZE];\n\tif (BN_get_flags(p, BN_FLG_CONSTTIME) != 0)\n\t\t{\n\t\tBNerr(BN_F_BN_MOD_EXP_SIMPLE,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);\n\t\treturn -1;\n\t\t}\n\tbits=BN_num_bits(p);\n\tif (bits == 0)\n\t\t{\n\t\tret = BN_one(r);\n\t\treturn ret;\n\t\t}\n\tBN_CTX_start(ctx);\n\td = BN_CTX_get(ctx);\n\tval[0] = BN_CTX_get(ctx);\n\tif(!d || !val[0]) goto err;\n\tif (!BN_nnmod(val[0],a,m,ctx)) goto err;\n\tif (BN_is_zero(val[0]))\n\t\t{\n\t\tBN_zero(r);\n\t\tret = 1;\n\t\tgoto err;\n\t\t}\n\twindow = BN_window_bits_for_exponent_size(bits);\n\tif (window > 1)\n\t\t{\n\t\tif (!BN_mod_mul(d,val[0],val[0],m,ctx))\n\t\t\tgoto err;\n\t\tj=1<<(window-1);\n\t\tfor (i=1; i<j; i++)\n\t\t\t{\n\t\t\tif(((val[i] = BN_CTX_get(ctx)) == NULL) ||\n\t\t\t\t\t!BN_mod_mul(val[i],val[i-1],d,m,ctx))\n\t\t\t\tgoto err;\n\t\t\t}\n\t\t}\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(r,r,r,m,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(r,r,r,m,ctx))\n\t\t\t\t\tgoto err;\n\t\t\t\t}\n\t\tif (!BN_mod_mul(r,r,val[wvalue>>1],m,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\tBN_CTX_end(ctx);\n\tbn_check_top(r);\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\tif ((BN_get_flags(num, BN_FLG_CONSTTIME) != 0) || (BN_get_flags(divisor, BN_FLG_CONSTTIME) != 0))\n\t\t{\n\t\treturn BN_div_no_branch(dv, rm, num, divisor, ctx);\n\t\t}\n\tbn_check_top(dv);\n\tbn_check_top(rm);\n\tbn_check_top(num);\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 (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) goto 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\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;\n\tresp= &(res->d[loop-1]);\n\tif (!bn_wexpand(tmp,(div_n+1))) goto err;\n\tif (BN_ucmp(&wnum,sdiv) >= 0)\n\t\t{\n\t\tbn_clear_top2max(&wnum);\n\t\tbn_sub_words(wnum.d, wnum.d, sdiv->d, div_n);\n\t\t*resp=1;\n\t\t}\n\telse\n\t\tres->top--;\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,ql,qh;\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\tt2l=LBITS(d1); t2h=HBITS(d1);\n\t\t\tql =LBITS(q); qh =HBITS(q);\n\t\t\tmul64(t2l,t2h,ql,qh);\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\tBN_CTX_end(ctx);\n\treturn(1);\nerr:\n\tbn_check_top(rm);\n\tBN_CTX_end(ctx);\n\treturn(0);\n\t}', 'int BN_mod_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m,\n\tBN_CTX *ctx)\n\t{\n\tBIGNUM *t;\n\tint ret=0;\n\tbn_check_top(a);\n\tbn_check_top(b);\n\tbn_check_top(m);\n\tBN_CTX_start(ctx);\n\tif ((t = BN_CTX_get(ctx)) == NULL) goto err;\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_nnmod(r,t,m,ctx)) goto err;\n\tbn_check_top(r);\n\tret=1;\nerr:\n\tBN_CTX_end(ctx);\n\treturn(ret);\n\t}', 'BIGNUM *BN_CTX_get(BN_CTX *ctx)\n\t{\n\tBIGNUM *ret;\n\tCTXDBG_ENTRY("BN_CTX_get", ctx);\n\tif(ctx->err_stack || ctx->too_many) return NULL;\n\tif((ret = BN_POOL_get(&ctx->pool)) == NULL)\n\t\t{\n\t\tctx->too_many = 1;\n\t\tBNerr(BN_F_BN_CTX_GET,BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n\t\treturn NULL;\n\t\t}\n\tBN_zero(ret);\n\tctx->used++;\n\tCTXDBG_RET(ctx, ret);\n\treturn ret;\n\t}', 'int BN_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)\n\t{\n\tint ret=0;\n\tint top,al,bl;\n\tBIGNUM *rr;\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n\tint i;\n#endif\n#ifdef BN_RECURSION\n\tBIGNUM *t=NULL;\n\tint j=0,k;\n#endif\n#ifdef BN_COUNT\n\tfprintf(stderr,"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\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\tBN_CTX_start(ctx);\n\tif ((r == a) || (r == b))\n\t\t{\n\t\tif ((rr = BN_CTX_get(ctx)) == NULL) goto err;\n\t\t}\n\telse\n\t\trr = r;\n\trr->neg=a->neg^b->neg;\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n\ti = al-bl;\n#endif\n#ifdef BN_MUL_COMBA\n\tif (i == 0)\n\t\t{\n# if 0\n\t\tif (al == 4)\n\t\t\t{\n\t\t\tif (bn_wexpand(rr,8) == NULL) goto err;\n\t\t\trr->top=8;\n\t\t\tbn_mul_comba4(rr->d,a->d,b->d);\n\t\t\tgoto end;\n\t\t\t}\n# endif\n\t\tif (al == 8)\n\t\t\t{\n\t\t\tif (bn_wexpand(rr,16) == NULL) goto err;\n\t\t\trr->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\t}\n#endif\n#ifdef BN_RECURSION\n\tif ((al >= BN_MULL_SIZE_NORMAL) && (bl >= BN_MULL_SIZE_NORMAL))\n\t\t{\n\t\tif (i >= -1 && i <= 1)\n\t\t\t{\n\t\t\tint sav_j =0;\n\t\t\tif (i >= 0)\n\t\t\t\t{\n\t\t\t\tj = BN_num_bits_word((BN_ULONG)al);\n\t\t\t\t}\n\t\t\tif (i == -1)\n\t\t\t\t{\n\t\t\t\tj = BN_num_bits_word((BN_ULONG)bl);\n\t\t\t\t}\n\t\t\tsav_j = j;\n\t\t\tj = 1<<(j-1);\n\t\t\tassert(j <= al || j <= bl);\n\t\t\tk = j+j;\n\t\t\tt = BN_CTX_get(ctx);\n\t\t\tif (al > j || bl > j)\n\t\t\t\t{\n\t\t\t\tbn_wexpand(t,k*4);\n\t\t\t\tbn_wexpand(rr,k*4);\n\t\t\t\tbn_mul_part_recursive(rr->d,a->d,b->d,\n\t\t\t\t\tj,al-j,bl-j,t->d);\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\tbn_wexpand(t,k*2);\n\t\t\t\tbn_wexpand(rr,k*2);\n\t\t\t\tbn_mul_recursive(rr->d,a->d,b->d,\n\t\t\t\t\tj,al-j,bl-j,t->d);\n\t\t\t\t}\n\t\t\trr->top=top;\n\t\t\tgoto end;\n\t\t\t}\n#if 0\n\t\tif (i == 1 && !BN_get_flags(b,BN_FLG_STATIC_DATA))\n\t\t\t{\n\t\t\tBIGNUM *tmp_bn = (BIGNUM *)b;\n\t\t\tif (bn_wexpand(tmp_bn,al) == NULL) goto err;\n\t\t\ttmp_bn->d[bl]=0;\n\t\t\tbl++;\n\t\t\ti--;\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\tBIGNUM *tmp_bn = (BIGNUM *)a;\n\t\t\tif (bn_wexpand(tmp_bn,bl) == NULL) goto err;\n\t\t\ttmp_bn->d[al]=0;\n\t\t\tal++;\n\t\t\ti++;\n\t\t\t}\n\t\tif (i == 0)\n\t\t\t{\n\t\t\tj=BN_num_bits_word((BN_ULONG)al);\n\t\t\tj=1<<(j-1);\n\t\t\tk=j+j;\n\t\t\tt = BN_CTX_get(ctx);\n\t\t\tif (al == j)\n\t\t\t\t{\n\t\t\t\tif (bn_wexpand(t,k*2) == NULL) goto err;\n\t\t\t\tif (bn_wexpand(rr,k*2) == NULL) goto err;\n\t\t\t\tbn_mul_recursive(rr->d,a->d,b->d,al,t->d);\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\tif (bn_wexpand(t,k*4) == NULL) goto err;\n\t\t\t\tif (bn_wexpand(rr,k*4) == NULL) goto err;\n\t\t\t\tbn_mul_part_recursive(rr->d,a->d,b->d,al-j,j,t->d);\n\t\t\t\t}\n\t\t\trr->top=top;\n\t\t\tgoto end;\n\t\t\t}\n#endif\n\t\t}\n#endif\n\tif (bn_wexpand(rr,top) == NULL) goto err;\n\trr->top=top;\n\tbn_mul_normal(rr->d,a->d,al,b->d,bl);\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\nend:\n#endif\n\tbn_correct_top(rr);\n\tif (r != rr) BN_copy(r,rr);\n\tret=1;\nerr:\n\tbn_check_top(r);\n\tBN_CTX_end(ctx);\n\treturn(ret);\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 void BN_POOL_release(BN_POOL *p, unsigned int num)\n\t{\n\tunsigned int offset = (p->used - 1) % BN_CTX_POOL_SIZE;\n\tp->used -= num;\n\twhile(num--)\n\t\t{\n\t\tbn_check_top(p->current->vals + offset);\n\t\tif(!offset)\n\t\t\t{\n\t\t\toffset = BN_CTX_POOL_SIZE - 1;\n\t\t\tp->current = p->current->prev;\n\t\t\t}\n\t\telse\n\t\t\toffset--;\n\t\t}\n\t}']
|
2,192
| 0
|
https://github.com/openssl/openssl/blob/ff64702b3d83d4c77756e0fd7b624e2165dbbdf0/crypto/packet.c/#L52
|
int WPACKET_reserve_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes)
{
if (!ossl_assert(pkt->subs != NULL && len != 0))
return 0;
if (pkt->maxsize - pkt->written < len)
return 0;
if (pkt->buf != NULL && (pkt->buf->length - pkt->written < len)) {
size_t newlen;
size_t reflen;
reflen = (len > pkt->buf->length) ? len : pkt->buf->length;
if (reflen > SIZE_MAX / 2) {
newlen = SIZE_MAX;
} else {
newlen = reflen * 2;
if (newlen < DEFAULT_BUF_SIZE)
newlen = DEFAULT_BUF_SIZE;
}
if (BUF_MEM_grow(pkt->buf, newlen) == 0)
return 0;
}
if (allocbytes != NULL)
*allocbytes = WPACKET_get_curr(pkt);
return 1;
}
|
['EXT_RETURN tls_construct_ctos_cookie(SSL *s, WPACKET *pkt, unsigned int context,\n X509 *x, size_t chainidx)\n{\n EXT_RETURN ret = EXT_RETURN_FAIL;\n if (s->ext.tls13_cookie_len == 0)\n return EXT_RETURN_NOT_SENT;\n if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_cookie)\n || !WPACKET_start_sub_packet_u16(pkt)\n || !WPACKET_sub_memcpy_u16(pkt, s->ext.tls13_cookie,\n s->ext.tls13_cookie_len)\n || !WPACKET_close(pkt)) {\n SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CTOS_COOKIE,\n ERR_R_INTERNAL_ERROR);\n goto end;\n }\n ret = EXT_RETURN_SENT;\n end:\n OPENSSL_free(s->ext.tls13_cookie);\n s->ext.tls13_cookie = NULL;\n s->ext.tls13_cookie_len = 0;\n return ret;\n}', 'int WPACKET_put_bytes__(WPACKET *pkt, unsigned int val, size_t size)\n{\n unsigned char *data;\n if (!ossl_assert(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 WPACKET_start_sub_packet_len__(WPACKET *pkt, size_t lenbytes)\n{\n WPACKET_SUB *sub;\n unsigned char *lenchars;\n if (!ossl_assert(pkt->subs != NULL))\n return 0;\n if ((sub = OPENSSL_zalloc(sizeof(*sub))) == NULL) {\n SSLerr(SSL_F_WPACKET_START_SUB_PACKET_LEN__, ERR_R_MALLOC_FAILURE);\n return 0;\n }\n sub->parent = pkt->subs;\n pkt->subs = sub;\n sub->pwritten = pkt->written + lenbytes;\n sub->lenbytes = lenbytes;\n if (lenbytes == 0) {\n sub->packet_len = 0;\n return 1;\n }\n sub->packet_len = pkt->written;\n if (!WPACKET_allocate_bytes(pkt, lenbytes, &lenchars))\n return 0;\n return 1;\n}', 'int WPACKET_sub_memcpy__(WPACKET *pkt, const void *src, size_t len,\n size_t lenbytes)\n{\n if (!WPACKET_start_sub_packet_len__(pkt, lenbytes)\n || !WPACKET_memcpy(pkt, src, len)\n || !WPACKET_close(pkt))\n return 0;\n return 1;\n}', 'int WPACKET_memcpy(WPACKET *pkt, const void *src, size_t len)\n{\n unsigned char *dest;\n if (len == 0)\n return 1;\n if (!WPACKET_allocate_bytes(pkt, len, &dest))\n return 0;\n if (dest != NULL)\n memcpy(dest, src, len);\n return 1;\n}', 'int WPACKET_allocate_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes)\n{\n if (!WPACKET_reserve_bytes(pkt, len, allocbytes))\n return 0;\n pkt->written += len;\n pkt->curr += len;\n return 1;\n}', 'int WPACKET_reserve_bytes(WPACKET *pkt, size_t len, unsigned char **allocbytes)\n{\n if (!ossl_assert(pkt->subs != NULL && len != 0))\n return 0;\n if (pkt->maxsize - pkt->written < len)\n return 0;\n if (pkt->buf != NULL && (pkt->buf->length - pkt->written < len)) {\n size_t newlen;\n size_t reflen;\n reflen = (len > pkt->buf->length) ? len : pkt->buf->length;\n if (reflen > SIZE_MAX / 2) {\n newlen = SIZE_MAX;\n } else {\n newlen = reflen * 2;\n if (newlen < DEFAULT_BUF_SIZE)\n newlen = DEFAULT_BUF_SIZE;\n }\n if (BUF_MEM_grow(pkt->buf, newlen) == 0)\n return 0;\n }\n if (allocbytes != NULL)\n *allocbytes = WPACKET_get_curr(pkt);\n return 1;\n}']
|
2,193
| 0
|
https://github.com/libav/libav/blob/0ca36b4de76e10578e23199c2932682c0f510e31/libavcodec/v210enc.c/#L95
|
static int encode_frame(AVCodecContext *avctx, unsigned char *buf,
int buf_size, void *data)
{
const AVFrame *pic = data;
int aligned_width = ((avctx->width + 47) / 48) * 48;
int stride = aligned_width * 8 / 3;
int h, w;
const uint16_t *y = (const uint16_t*)pic->data[0];
const uint16_t *u = (const uint16_t*)pic->data[1];
const uint16_t *v = (const uint16_t*)pic->data[2];
uint8_t *p = buf;
uint8_t *pdst = buf;
if (buf_size < aligned_width * avctx->height * 8 / 3) {
av_log(avctx, AV_LOG_ERROR, "output buffer too small\n");
return -1;
}
#define CLIP(v) av_clip(v, 4, 1019)
#define WRITE_PIXELS(a, b, c) \
do { \
val = CLIP(*a++); \
val |= (CLIP(*b++) << 10) | \
(CLIP(*c++) << 20); \
bytestream_put_le32(&p, val); \
} while (0)
for (h = 0; h < avctx->height; h++) {
uint32_t val;
for (w = 0; w < avctx->width - 5; w += 6) {
WRITE_PIXELS(u, y, v);
WRITE_PIXELS(y, u, y);
WRITE_PIXELS(v, y, u);
WRITE_PIXELS(y, v, y);
}
if (w < avctx->width - 1) {
WRITE_PIXELS(u, y, v);
val = CLIP(*y++);
if (w == avctx->width - 2)
bytestream_put_le32(&p, val);
}
if (w < avctx->width - 3) {
val |= (CLIP(*u++) << 10) | (CLIP(*y++) << 20);
bytestream_put_le32(&p, val);
val = CLIP(*v++) | (CLIP(*y++) << 10);
bytestream_put_le32(&p, val);
}
pdst += stride;
memset(p, 0, pdst - p);
p = pdst;
y += pic->linesize[0] / 2 - avctx->width;
u += pic->linesize[1] / 2 - avctx->width / 2;
v += pic->linesize[2] / 2 - avctx->width / 2;
}
return p - buf;
}
|
['static int encode_frame(AVCodecContext *avctx, unsigned char *buf,\n int buf_size, void *data)\n{\n const AVFrame *pic = data;\n int aligned_width = ((avctx->width + 47) / 48) * 48;\n int stride = aligned_width * 8 / 3;\n int h, w;\n const uint16_t *y = (const uint16_t*)pic->data[0];\n const uint16_t *u = (const uint16_t*)pic->data[1];\n const uint16_t *v = (const uint16_t*)pic->data[2];\n uint8_t *p = buf;\n uint8_t *pdst = buf;\n if (buf_size < aligned_width * avctx->height * 8 / 3) {\n av_log(avctx, AV_LOG_ERROR, "output buffer too small\\n");\n return -1;\n }\n#define CLIP(v) av_clip(v, 4, 1019)\n#define WRITE_PIXELS(a, b, c) \\\n do { \\\n val = CLIP(*a++); \\\n val |= (CLIP(*b++) << 10) | \\\n (CLIP(*c++) << 20); \\\n bytestream_put_le32(&p, val); \\\n } while (0)\n for (h = 0; h < avctx->height; h++) {\n uint32_t val;\n for (w = 0; w < avctx->width - 5; w += 6) {\n WRITE_PIXELS(u, y, v);\n WRITE_PIXELS(y, u, y);\n WRITE_PIXELS(v, y, u);\n WRITE_PIXELS(y, v, y);\n }\n if (w < avctx->width - 1) {\n WRITE_PIXELS(u, y, v);\n val = CLIP(*y++);\n if (w == avctx->width - 2)\n bytestream_put_le32(&p, val);\n }\n if (w < avctx->width - 3) {\n val |= (CLIP(*u++) << 10) | (CLIP(*y++) << 20);\n bytestream_put_le32(&p, val);\n val = CLIP(*v++) | (CLIP(*y++) << 10);\n bytestream_put_le32(&p, val);\n }\n pdst += stride;\n memset(p, 0, pdst - p);\n p = pdst;\n y += pic->linesize[0] / 2 - avctx->width;\n u += pic->linesize[1] / 2 - avctx->width / 2;\n v += pic->linesize[2] / 2 - avctx->width / 2;\n }\n return p - buf;\n}']
|
2,194
| 0
|
https://github.com/nginx/nginx/blob/a11050ea4e4fa0c6996f9115e04635723a642f5f/src/os/unix/ngx_readv_chain.c/#L200
|
ssize_t
ngx_readv_chain(ngx_connection_t *c, ngx_chain_t *chain)
{
u_char *prev;
ssize_t n, size;
ngx_err_t err;
ngx_array_t vec;
ngx_event_t *rev;
struct iovec *iov, iovs[NGX_IOVS];
prev = NULL;
iov = NULL;
size = 0;
vec.elts = iovs;
vec.nelts = 0;
vec.size = sizeof(struct iovec);
vec.nalloc = NGX_IOVS;
vec.pool = c->pool;
while (chain) {
if (prev == chain->buf->last) {
iov->iov_len += chain->buf->end - chain->buf->last;
} else {
if (vec.nelts >= IOV_MAX) {
break;
}
iov = ngx_array_push(&vec);
if (iov == NULL) {
return NGX_ERROR;
}
iov->iov_base = (void *) chain->buf->last;
iov->iov_len = chain->buf->end - chain->buf->last;
}
size += chain->buf->end - chain->buf->last;
prev = chain->buf->end;
chain = chain->next;
}
ngx_log_debug2(NGX_LOG_DEBUG_EVENT, c->log, 0,
"readv: %d:%d", vec.nelts, iov->iov_len);
rev = c->read;
do {
n = readv(c->fd, (struct iovec *) vec.elts, vec.nelts);
if (n == 0) {
rev->ready = 0;
rev->eof = 1;
return n;
} else if (n > 0) {
if (n < size && !(ngx_event_flags & NGX_USE_GREEDY_EVENT)) {
rev->ready = 0;
}
return n;
}
err = ngx_socket_errno;
if (err == NGX_EAGAIN || err == NGX_EINTR) {
ngx_log_debug0(NGX_LOG_DEBUG_EVENT, c->log, err,
"readv() not ready");
n = NGX_AGAIN;
} else {
n = ngx_connection_error(c, err, "readv() failed");
break;
}
} while (err == NGX_EINTR);
rev->ready = 0;
if (n == NGX_ERROR) {
c->read->error = 1;
}
return n;
}
|
['ssize_t\nngx_readv_chain(ngx_connection_t *c, ngx_chain_t *chain)\n{\n u_char *prev;\n ssize_t n, size;\n ngx_err_t err;\n ngx_array_t vec;\n ngx_event_t *rev;\n struct iovec *iov, iovs[NGX_IOVS];\n prev = NULL;\n iov = NULL;\n size = 0;\n vec.elts = iovs;\n vec.nelts = 0;\n vec.size = sizeof(struct iovec);\n vec.nalloc = NGX_IOVS;\n vec.pool = c->pool;\n while (chain) {\n if (prev == chain->buf->last) {\n iov->iov_len += chain->buf->end - chain->buf->last;\n } else {\n if (vec.nelts >= IOV_MAX) {\n break;\n }\n iov = ngx_array_push(&vec);\n if (iov == NULL) {\n return NGX_ERROR;\n }\n iov->iov_base = (void *) chain->buf->last;\n iov->iov_len = chain->buf->end - chain->buf->last;\n }\n size += chain->buf->end - chain->buf->last;\n prev = chain->buf->end;\n chain = chain->next;\n }\n ngx_log_debug2(NGX_LOG_DEBUG_EVENT, c->log, 0,\n "readv: %d:%d", vec.nelts, iov->iov_len);\n rev = c->read;\n do {\n n = readv(c->fd, (struct iovec *) vec.elts, vec.nelts);\n if (n == 0) {\n rev->ready = 0;\n rev->eof = 1;\n return n;\n } else if (n > 0) {\n if (n < size && !(ngx_event_flags & NGX_USE_GREEDY_EVENT)) {\n rev->ready = 0;\n }\n return n;\n }\n err = ngx_socket_errno;\n if (err == NGX_EAGAIN || err == NGX_EINTR) {\n ngx_log_debug0(NGX_LOG_DEBUG_EVENT, c->log, err,\n "readv() not ready");\n n = NGX_AGAIN;\n } else {\n n = ngx_connection_error(c, err, "readv() failed");\n break;\n }\n } while (err == NGX_EINTR);\n rev->ready = 0;\n if (n == NGX_ERROR) {\n c->read->error = 1;\n }\n return n;\n}']
|
2,195
| 0
|
https://github.com/openssl/openssl/blob/f9df0a7775f483c175cda5832360cccd1db6943a/crypto/bn/bn_ctx.c/#L273
|
static unsigned int BN_STACK_pop(BN_STACK *st)
{
return st->indexes[--(st->depth)];
}
|
['static int dsa_sign_setup(DSA *dsa, 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, *kinv = NULL, *r = *rp;\n int ret = 0;\n if (!dsa->p || !dsa->q || !dsa->g) {\n DSAerr(DSA_F_DSA_SIGN_SETUP, DSA_R_MISSING_PARAMETERS);\n return 0;\n }\n k = BN_new();\n if (k == NULL)\n goto err;\n if (ctx_in == NULL) {\n if ((ctx = BN_CTX_new()) == NULL)\n goto err;\n } else\n ctx = ctx_in;\n do {\n if (dgst != NULL) {\n if (!BN_generate_dsa_nonce(k, dsa->q, dsa->priv_key, dgst,\n dlen, ctx))\n goto err;\n } else if (!BN_priv_rand_range(k, dsa->q))\n goto err;\n } while (BN_is_zero(k));\n BN_set_flags(k, BN_FLG_CONSTTIME);\n if (dsa->flags & DSA_FLAG_CACHE_MONT_P) {\n if (!BN_MONT_CTX_set_locked(&dsa->method_mont_p,\n dsa->lock, dsa->p, ctx))\n goto err;\n }\n if (!BN_add(k, k, dsa->q))\n goto err;\n if (BN_num_bits(k) <= BN_num_bits(dsa->q)) {\n if (!BN_add(k, k, dsa->q))\n goto err;\n }\n if ((dsa)->meth->bn_mod_exp != NULL) {\n if (!dsa->meth->bn_mod_exp(dsa, r, dsa->g, k, dsa->p, ctx,\n dsa->method_mont_p))\n goto err;\n } else {\n if (!BN_mod_exp_mont(r, dsa->g, k, dsa->p, ctx, dsa->method_mont_p))\n goto err;\n }\n if (!BN_mod(r, r, dsa->q, ctx))\n goto err;\n if ((kinv = BN_mod_inverse(NULL, k, dsa->q, ctx)) == NULL)\n goto err;\n BN_clear_free(*kinvp);\n *kinvp = kinv;\n kinv = NULL;\n ret = 1;\n err:\n if (!ret)\n DSAerr(DSA_F_DSA_SIGN_SETUP, ERR_R_BN_LIB);\n if (ctx != ctx_in)\n BN_CTX_free(ctx);\n BN_clear_free(k);\n return ret;\n}', 'BN_MONT_CTX *BN_MONT_CTX_set_locked(BN_MONT_CTX **pmont, CRYPTO_RWLOCK *lock,\n const BIGNUM *mod, BN_CTX *ctx)\n{\n BN_MONT_CTX *ret;\n CRYPTO_THREAD_read_lock(lock);\n ret = *pmont;\n CRYPTO_THREAD_unlock(lock);\n if (ret)\n return ret;\n ret = BN_MONT_CTX_new();\n if (ret == NULL)\n return NULL;\n if (!BN_MONT_CTX_set(ret, mod, ctx)) {\n BN_MONT_CTX_free(ret);\n return NULL;\n }\n CRYPTO_THREAD_write_lock(lock);\n if (*pmont) {\n BN_MONT_CTX_free(ret);\n ret = *pmont;\n } else\n *pmont = ret;\n CRYPTO_THREAD_unlock(lock);\n return ret;\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 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_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_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}', '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}', '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}', '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}', '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}']
|
2,196
| 0
|
https://github.com/openssl/openssl/blob/4cc968df403ed9321d0df722aba33323ae575ce0/crypto/bn/bn_ctx.c/#L276
|
static unsigned int BN_STACK_pop(BN_STACK *st)
{
return st->indexes[--(st->depth)];
}
|
['int BN_BLINDING_update(BN_BLINDING *b, BN_CTX *ctx)\n{\n int ret = 0;\n if ((b->A == NULL) || (b->Ai == NULL)) {\n BNerr(BN_F_BN_BLINDING_UPDATE, BN_R_NOT_INITIALIZED);\n goto err;\n }\n if (b->counter == -1)\n b->counter = 0;\n if (++b->counter == BN_BLINDING_COUNTER && b->e != NULL &&\n !(b->flags & BN_BLINDING_NO_RECREATE)) {\n if (!BN_BLINDING_create_param(b, NULL, NULL, ctx, NULL, NULL))\n goto err;\n } else if (!(b->flags & BN_BLINDING_NO_UPDATE)) {\n if (!BN_mod_mul(b->A, b->A, b->A, b->mod, ctx))\n goto err;\n if (!BN_mod_mul(b->Ai, b->Ai, b->Ai, b->mod, ctx))\n goto err;\n }\n ret = 1;\n err:\n if (b->counter == BN_BLINDING_COUNTER)\n b->counter = 0;\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}', '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_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#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 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(r);\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}']
|
2,197
| 0
|
https://github.com/libav/libav/blob/6dfd99c93808d6504dd5cb1fad847d68cb179352/libavcodec/mvcdec.c/#L210
|
static int decode_mvc2(AVCodecContext *avctx, GetByteContext *gb,
uint8_t *dst_start, int width, int height,
int linesize, int vflip)
{
uint8_t *dst;
uint32_t color[128], v[8];
int w, h, nb_colors, i, x, y, p0, p1, mask;
if (bytestream2_get_bytes_left(gb) < 6)
return AVERROR_INVALIDDATA;
w = bytestream2_get_be16u(gb);
h = bytestream2_get_be16u(gb);
if ((w & ~3) != width || (h & ~3) != height)
av_log(avctx, AV_LOG_WARNING, "dimension mismatch\n");
if (bytestream2_get_byteu(gb)) {
avpriv_request_sample(avctx, "bitmap feature");
return AVERROR_PATCHWELCOME;
}
nb_colors = bytestream2_get_byteu(gb);
if (bytestream2_get_bytes_left(gb) < nb_colors * 3)
return AVERROR_INVALIDDATA;
for (i = 0; i < FFMIN(nb_colors, 128); i++)
color[i] = 0xFF000000 | bytestream2_get_be24u(gb);
if (nb_colors > 128)
bytestream2_skip(gb, (nb_colors - 128) * 3);
if (vflip) {
dst_start += (height - 1) * linesize;
linesize = -linesize;
}
x = y = 0;
while (bytestream2_get_bytes_left(gb) >= 1) {
p0 = bytestream2_get_byteu(gb);
if ((p0 & 0x80)) {
if ((p0 & 0x40)) {
p0 &= 0x3F;
p0 = (p0 << 2) | (p0 >> 4);
set_4x4_block(dst_start + y * linesize + x * 4, linesize,
0xFF000000 | (p0 << 16) | (p0 << 8) | p0);
} else {
int g, r;
p0 &= 0x3F;
p0 = (p0 << 2) | (p0 >> 4);
if (bytestream2_get_bytes_left(gb) < 2)
return AVERROR_INVALIDDATA;
g = bytestream2_get_byteu(gb);
r = bytestream2_get_byteu(gb);
set_4x4_block(dst_start + y * linesize + x * 4, linesize,
0xFF000000 | (r << 16) | (g << 8) | p0);
}
} else {
if (bytestream2_get_bytes_left(gb) < 1)
return AVERROR_INVALIDDATA;
p1 = bytestream2_get_byteu(gb);
if ((p1 & 0x80)) {
if ((p0 & 0x7F) == (p1 & 0x7F)) {
set_4x4_block(dst_start + y * linesize + x * 4, linesize,
color[p0 & 0x7F]);
} else {
if (bytestream2_get_bytes_left(gb) < 2)
return AVERROR_INVALIDDATA;
v[0] = v[2] = v[4] = v[6] = color[p0 & 0x7F];
v[1] = v[3] = v[5] = v[7] = color[p1 & 0x7F];
mask = bytestream2_get_le16u(gb);
MVC2_BLOCK
}
} else {
if (bytestream2_get_bytes_left(gb) < 8)
return AVERROR_INVALIDDATA;
v[0] = color[p0 & 0x7F];
v[1] = color[p1 & 0x7F];
for (i = 2; i < 8; i++)
v[i] = color[bytestream2_get_byteu(gb) & 0x7F];
mask = bytestream2_get_le16u(gb);
MVC2_BLOCK
}
}
x += 4;
if (x >= width) {
y += 4;
if (y >= height)
break;
x = 0;
}
}
return 0;
}
|
['static int decode_mvc2(AVCodecContext *avctx, GetByteContext *gb,\n uint8_t *dst_start, int width, int height,\n int linesize, int vflip)\n{\n uint8_t *dst;\n uint32_t color[128], v[8];\n int w, h, nb_colors, i, x, y, p0, p1, mask;\n if (bytestream2_get_bytes_left(gb) < 6)\n return AVERROR_INVALIDDATA;\n w = bytestream2_get_be16u(gb);\n h = bytestream2_get_be16u(gb);\n if ((w & ~3) != width || (h & ~3) != height)\n av_log(avctx, AV_LOG_WARNING, "dimension mismatch\\n");\n if (bytestream2_get_byteu(gb)) {\n avpriv_request_sample(avctx, "bitmap feature");\n return AVERROR_PATCHWELCOME;\n }\n nb_colors = bytestream2_get_byteu(gb);\n if (bytestream2_get_bytes_left(gb) < nb_colors * 3)\n return AVERROR_INVALIDDATA;\n for (i = 0; i < FFMIN(nb_colors, 128); i++)\n color[i] = 0xFF000000 | bytestream2_get_be24u(gb);\n if (nb_colors > 128)\n bytestream2_skip(gb, (nb_colors - 128) * 3);\n if (vflip) {\n dst_start += (height - 1) * linesize;\n linesize = -linesize;\n }\n x = y = 0;\n while (bytestream2_get_bytes_left(gb) >= 1) {\n p0 = bytestream2_get_byteu(gb);\n if ((p0 & 0x80)) {\n if ((p0 & 0x40)) {\n p0 &= 0x3F;\n p0 = (p0 << 2) | (p0 >> 4);\n set_4x4_block(dst_start + y * linesize + x * 4, linesize,\n 0xFF000000 | (p0 << 16) | (p0 << 8) | p0);\n } else {\n int g, r;\n p0 &= 0x3F;\n p0 = (p0 << 2) | (p0 >> 4);\n if (bytestream2_get_bytes_left(gb) < 2)\n return AVERROR_INVALIDDATA;\n g = bytestream2_get_byteu(gb);\n r = bytestream2_get_byteu(gb);\n set_4x4_block(dst_start + y * linesize + x * 4, linesize,\n 0xFF000000 | (r << 16) | (g << 8) | p0);\n }\n } else {\n if (bytestream2_get_bytes_left(gb) < 1)\n return AVERROR_INVALIDDATA;\n p1 = bytestream2_get_byteu(gb);\n if ((p1 & 0x80)) {\n if ((p0 & 0x7F) == (p1 & 0x7F)) {\n set_4x4_block(dst_start + y * linesize + x * 4, linesize,\n color[p0 & 0x7F]);\n } else {\n if (bytestream2_get_bytes_left(gb) < 2)\n return AVERROR_INVALIDDATA;\n v[0] = v[2] = v[4] = v[6] = color[p0 & 0x7F];\n v[1] = v[3] = v[5] = v[7] = color[p1 & 0x7F];\n mask = bytestream2_get_le16u(gb);\n MVC2_BLOCK\n }\n } else {\n if (bytestream2_get_bytes_left(gb) < 8)\n return AVERROR_INVALIDDATA;\n v[0] = color[p0 & 0x7F];\n v[1] = color[p1 & 0x7F];\n for (i = 2; i < 8; i++)\n v[i] = color[bytestream2_get_byteu(gb) & 0x7F];\n mask = bytestream2_get_le16u(gb);\n MVC2_BLOCK\n }\n }\n x += 4;\n if (x >= width) {\n y += 4;\n if (y >= height)\n break;\n x = 0;\n }\n }\n return 0;\n}']
|
2,198
| 0
|
https://github.com/libav/libav/blob/5788623d29c3e806a7879210986110aced758dc2/libswscale/swscale.c/#L94
|
static void hScale16To19_c(SwsContext *c, int16_t *_dst, int dstW,
const uint8_t *_src, const int16_t *filter,
const int32_t *filterPos, int filterSize)
{
const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(c->srcFormat);
int i;
int32_t *dst = (int32_t *) _dst;
const uint16_t *src = (const uint16_t *) _src;
int bits = desc->comp[0].depth - 1;
int sh = bits - 4;
for (i = 0; i < dstW; i++) {
int j;
int srcPos = filterPos[i];
int val = 0;
for (j = 0; j < filterSize; j++) {
val += src[srcPos + j] * filter[filterSize * i + j];
}
dst[i] = FFMIN(val >> sh, (1 << 19) - 1);
}
}
|
['static void hScale16To19_c(SwsContext *c, int16_t *_dst, int dstW,\n const uint8_t *_src, const int16_t *filter,\n const int32_t *filterPos, int filterSize)\n{\n const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(c->srcFormat);\n int i;\n int32_t *dst = (int32_t *) _dst;\n const uint16_t *src = (const uint16_t *) _src;\n int bits = desc->comp[0].depth - 1;\n int sh = bits - 4;\n for (i = 0; i < dstW; i++) {\n int j;\n int srcPos = filterPos[i];\n int val = 0;\n for (j = 0; j < filterSize; j++) {\n val += src[srcPos + j] * filter[filterSize * i + j];\n }\n dst[i] = FFMIN(val >> sh, (1 << 19) - 1);\n }\n}', 'const AVPixFmtDescriptor *av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)\n{\n if (pix_fmt < 0 || pix_fmt >= AV_PIX_FMT_NB)\n return NULL;\n return &av_pix_fmt_descriptors[pix_fmt];\n}']
|
2,199
| 0
|
https://github.com/libav/libav/blob/3ec394ea823c2a6e65a6abbdb2041ce1c66964f8/libavcodec/motion_est_template.c/#L725
|
static int l2s_dia_search(MpegEncContext * s, int *best, int dmin,
int src_index, int ref_index, int const penalty_factor,
int size, int h, int flags)
{
MotionEstContext * const c= &s->me;
me_cmp_func cmpf, chroma_cmpf;
LOAD_COMMON
LOAD_COMMON2
int map_generation= c->map_generation;
int x,y,i,d;
int dia_size= c->dia_size&0xFF;
const int dec= dia_size & (dia_size-1);
static const int hex[8][2]={{-2, 0}, {-1,-1}, { 0,-2}, { 1,-1},
{ 2, 0}, { 1, 1}, { 0, 2}, {-1, 1}};
cmpf= s->dsp.me_cmp[size];
chroma_cmpf= s->dsp.me_cmp[size+1];
for(; dia_size; dia_size= dec ? dia_size-1 : dia_size>>1){
do{
x= best[0];
y= best[1];
for(i=0; i<8; i++){
CHECK_CLIPPED_MV(x+hex[i][0]*dia_size, y+hex[i][1]*dia_size);
}
}while(best[0] != x || best[1] != y);
}
x= best[0];
y= best[1];
CHECK_CLIPPED_MV(x+1, y);
CHECK_CLIPPED_MV(x, y+1);
CHECK_CLIPPED_MV(x-1, y);
CHECK_CLIPPED_MV(x, y-1);
return dmin;
}
|
['static int l2s_dia_search(MpegEncContext * s, int *best, int dmin,\n int src_index, int ref_index, int const penalty_factor,\n int size, int h, int flags)\n{\n MotionEstContext * const c= &s->me;\n me_cmp_func cmpf, chroma_cmpf;\n LOAD_COMMON\n LOAD_COMMON2\n int map_generation= c->map_generation;\n int x,y,i,d;\n int dia_size= c->dia_size&0xFF;\n const int dec= dia_size & (dia_size-1);\n static const int hex[8][2]={{-2, 0}, {-1,-1}, { 0,-2}, { 1,-1},\n { 2, 0}, { 1, 1}, { 0, 2}, {-1, 1}};\n cmpf= s->dsp.me_cmp[size];\n chroma_cmpf= s->dsp.me_cmp[size+1];\n for(; dia_size; dia_size= dec ? dia_size-1 : dia_size>>1){\n do{\n x= best[0];\n y= best[1];\n for(i=0; i<8; i++){\n CHECK_CLIPPED_MV(x+hex[i][0]*dia_size, y+hex[i][1]*dia_size);\n }\n }while(best[0] != x || best[1] != y);\n }\n x= best[0];\n y= best[1];\n CHECK_CLIPPED_MV(x+1, y);\n CHECK_CLIPPED_MV(x, y+1);\n CHECK_CLIPPED_MV(x-1, y);\n CHECK_CLIPPED_MV(x, y-1);\n return dmin;\n}']
|
2,200
| 0
|
https://github.com/libav/libav/blob/e4e30256f87f177decf59b59e923d05ef64147df/libavcodec/indeo4.c/#L246
|
static int decode_pic_hdr(IVI4DecContext *ctx, AVCodecContext *avctx)
{
int pic_size_indx, i, p;
IVIPicConfig pic_conf;
if (get_bits(&ctx->gb, 18) != 0x3FFF8) {
av_log(avctx, AV_LOG_ERROR, "Invalid picture start code!\n");
return AVERROR_INVALIDDATA;
}
ctx->prev_frame_type = ctx->frame_type;
ctx->frame_type = get_bits(&ctx->gb, 3);
if (ctx->frame_type == 7) {
av_log(avctx, AV_LOG_ERROR, "Invalid frame type: %d\n", ctx->frame_type);
return AVERROR_INVALIDDATA;
}
#if IVI4_STREAM_ANALYSER
if ( ctx->frame_type == FRAMETYPE_BIDIR1
|| ctx->frame_type == FRAMETYPE_BIDIR)
ctx->has_b_frames = 1;
#endif
ctx->transp_status = get_bits1(&ctx->gb);
#if IVI4_STREAM_ANALYSER
if (ctx->transp_status) {
ctx->has_transp = 1;
}
#endif
if (get_bits1(&ctx->gb)) {
av_log(avctx, AV_LOG_ERROR, "Sync bit is set!\n");
return AVERROR_INVALIDDATA;
}
ctx->data_size = get_bits1(&ctx->gb) ? get_bits(&ctx->gb, 24) : 0;
if (ctx->frame_type >= FRAMETYPE_NULL_FIRST) {
av_dlog(avctx, "Null frame encountered!\n");
return 0;
}
if (get_bits1(&ctx->gb)) {
skip_bits_long(&ctx->gb, 32);
av_dlog(avctx, "Password-protected clip!\n");
}
pic_size_indx = get_bits(&ctx->gb, 3);
if (pic_size_indx == IVI4_PIC_SIZE_ESC) {
pic_conf.pic_height = get_bits(&ctx->gb, 16);
pic_conf.pic_width = get_bits(&ctx->gb, 16);
} else {
pic_conf.pic_height = ivi4_common_pic_sizes[pic_size_indx * 2 + 1];
pic_conf.pic_width = ivi4_common_pic_sizes[pic_size_indx * 2 ];
}
if (get_bits1(&ctx->gb)) {
pic_conf.tile_height = scale_tile_size(pic_conf.pic_height, get_bits(&ctx->gb, 4));
pic_conf.tile_width = scale_tile_size(pic_conf.pic_width, get_bits(&ctx->gb, 4));
#if IVI4_STREAM_ANALYSER
ctx->uses_tiling = 1;
#endif
} else {
pic_conf.tile_height = pic_conf.pic_height;
pic_conf.tile_width = pic_conf.pic_width;
}
if (get_bits(&ctx->gb, 2)) {
av_log(avctx, AV_LOG_ERROR, "Only YVU9 picture format is supported!\n");
return AVERROR_INVALIDDATA;
}
pic_conf.chroma_height = (pic_conf.pic_height + 3) >> 2;
pic_conf.chroma_width = (pic_conf.pic_width + 3) >> 2;
pic_conf.luma_bands = decode_plane_subdivision(&ctx->gb);
if (pic_conf.luma_bands)
pic_conf.chroma_bands = decode_plane_subdivision(&ctx->gb);
ctx->is_scalable = pic_conf.luma_bands != 1 || pic_conf.chroma_bands != 1;
if (ctx->is_scalable && (pic_conf.luma_bands != 4 || pic_conf.chroma_bands != 1)) {
av_log(avctx, AV_LOG_ERROR, "Scalability: unsupported subdivision! Luma bands: %d, chroma bands: %d\n",
pic_conf.luma_bands, pic_conf.chroma_bands);
return AVERROR_INVALIDDATA;
}
if (ivi_pic_config_cmp(&pic_conf, &ctx->pic_conf)) {
if (ff_ivi_init_planes(ctx->planes, &pic_conf)) {
av_log(avctx, AV_LOG_ERROR, "Couldn't reallocate color planes!\n");
return AVERROR(ENOMEM);
}
ctx->pic_conf = pic_conf;
for (p = 0; p <= 2; p++) {
for (i = 0; i < (!p ? pic_conf.luma_bands : pic_conf.chroma_bands); i++) {
ctx->planes[p].bands[i].mb_size = !p ? (!ctx->is_scalable ? 16 : 8) : 4;
ctx->planes[p].bands[i].blk_size = !p ? 8 : 4;
}
}
if (ff_ivi_init_tiles(ctx->planes, ctx->pic_conf.tile_width,
ctx->pic_conf.tile_height)) {
av_log(avctx, AV_LOG_ERROR,
"Couldn't reallocate internal structures!\n");
return AVERROR(ENOMEM);
}
}
ctx->frame_num = get_bits1(&ctx->gb) ? get_bits(&ctx->gb, 20) : 0;
if (get_bits1(&ctx->gb))
skip_bits(&ctx->gb, 8);
if (ff_ivi_dec_huff_desc(&ctx->gb, get_bits1(&ctx->gb), IVI_MB_HUFF, &ctx->mb_vlc, avctx) ||
ff_ivi_dec_huff_desc(&ctx->gb, get_bits1(&ctx->gb), IVI_BLK_HUFF, &ctx->blk_vlc, avctx))
return AVERROR_INVALIDDATA;
ctx->rvmap_sel = get_bits1(&ctx->gb) ? get_bits(&ctx->gb, 3) : 8;
ctx->in_imf = get_bits1(&ctx->gb);
ctx->in_q = get_bits1(&ctx->gb);
ctx->pic_glob_quant = get_bits(&ctx->gb, 5);
ctx->unknown1 = get_bits1(&ctx->gb) ? get_bits(&ctx->gb, 3) : 0;
ctx->checksum = get_bits1(&ctx->gb) ? get_bits(&ctx->gb, 16) : 0;
while (get_bits1(&ctx->gb)) {
av_dlog(avctx, "Pic hdr extension encountered!\n");
skip_bits(&ctx->gb, 8);
}
if (get_bits1(&ctx->gb)) {
av_log(avctx, AV_LOG_ERROR, "Bad blocks bits encountered!\n");
}
align_get_bits(&ctx->gb);
return 0;
}
|
['static int decode_pic_hdr(IVI4DecContext *ctx, AVCodecContext *avctx)\n{\n int pic_size_indx, i, p;\n IVIPicConfig pic_conf;\n if (get_bits(&ctx->gb, 18) != 0x3FFF8) {\n av_log(avctx, AV_LOG_ERROR, "Invalid picture start code!\\n");\n return AVERROR_INVALIDDATA;\n }\n ctx->prev_frame_type = ctx->frame_type;\n ctx->frame_type = get_bits(&ctx->gb, 3);\n if (ctx->frame_type == 7) {\n av_log(avctx, AV_LOG_ERROR, "Invalid frame type: %d\\n", ctx->frame_type);\n return AVERROR_INVALIDDATA;\n }\n#if IVI4_STREAM_ANALYSER\n if ( ctx->frame_type == FRAMETYPE_BIDIR1\n || ctx->frame_type == FRAMETYPE_BIDIR)\n ctx->has_b_frames = 1;\n#endif\n ctx->transp_status = get_bits1(&ctx->gb);\n#if IVI4_STREAM_ANALYSER\n if (ctx->transp_status) {\n ctx->has_transp = 1;\n }\n#endif\n if (get_bits1(&ctx->gb)) {\n av_log(avctx, AV_LOG_ERROR, "Sync bit is set!\\n");\n return AVERROR_INVALIDDATA;\n }\n ctx->data_size = get_bits1(&ctx->gb) ? get_bits(&ctx->gb, 24) : 0;\n if (ctx->frame_type >= FRAMETYPE_NULL_FIRST) {\n av_dlog(avctx, "Null frame encountered!\\n");\n return 0;\n }\n if (get_bits1(&ctx->gb)) {\n skip_bits_long(&ctx->gb, 32);\n av_dlog(avctx, "Password-protected clip!\\n");\n }\n pic_size_indx = get_bits(&ctx->gb, 3);\n if (pic_size_indx == IVI4_PIC_SIZE_ESC) {\n pic_conf.pic_height = get_bits(&ctx->gb, 16);\n pic_conf.pic_width = get_bits(&ctx->gb, 16);\n } else {\n pic_conf.pic_height = ivi4_common_pic_sizes[pic_size_indx * 2 + 1];\n pic_conf.pic_width = ivi4_common_pic_sizes[pic_size_indx * 2 ];\n }\n if (get_bits1(&ctx->gb)) {\n pic_conf.tile_height = scale_tile_size(pic_conf.pic_height, get_bits(&ctx->gb, 4));\n pic_conf.tile_width = scale_tile_size(pic_conf.pic_width, get_bits(&ctx->gb, 4));\n#if IVI4_STREAM_ANALYSER\n ctx->uses_tiling = 1;\n#endif\n } else {\n pic_conf.tile_height = pic_conf.pic_height;\n pic_conf.tile_width = pic_conf.pic_width;\n }\n if (get_bits(&ctx->gb, 2)) {\n av_log(avctx, AV_LOG_ERROR, "Only YVU9 picture format is supported!\\n");\n return AVERROR_INVALIDDATA;\n }\n pic_conf.chroma_height = (pic_conf.pic_height + 3) >> 2;\n pic_conf.chroma_width = (pic_conf.pic_width + 3) >> 2;\n pic_conf.luma_bands = decode_plane_subdivision(&ctx->gb);\n if (pic_conf.luma_bands)\n pic_conf.chroma_bands = decode_plane_subdivision(&ctx->gb);\n ctx->is_scalable = pic_conf.luma_bands != 1 || pic_conf.chroma_bands != 1;\n if (ctx->is_scalable && (pic_conf.luma_bands != 4 || pic_conf.chroma_bands != 1)) {\n av_log(avctx, AV_LOG_ERROR, "Scalability: unsupported subdivision! Luma bands: %d, chroma bands: %d\\n",\n pic_conf.luma_bands, pic_conf.chroma_bands);\n return AVERROR_INVALIDDATA;\n }\n if (ivi_pic_config_cmp(&pic_conf, &ctx->pic_conf)) {\n if (ff_ivi_init_planes(ctx->planes, &pic_conf)) {\n av_log(avctx, AV_LOG_ERROR, "Couldn\'t reallocate color planes!\\n");\n return AVERROR(ENOMEM);\n }\n ctx->pic_conf = pic_conf;\n for (p = 0; p <= 2; p++) {\n for (i = 0; i < (!p ? pic_conf.luma_bands : pic_conf.chroma_bands); i++) {\n ctx->planes[p].bands[i].mb_size = !p ? (!ctx->is_scalable ? 16 : 8) : 4;\n ctx->planes[p].bands[i].blk_size = !p ? 8 : 4;\n }\n }\n if (ff_ivi_init_tiles(ctx->planes, ctx->pic_conf.tile_width,\n ctx->pic_conf.tile_height)) {\n av_log(avctx, AV_LOG_ERROR,\n "Couldn\'t reallocate internal structures!\\n");\n return AVERROR(ENOMEM);\n }\n }\n ctx->frame_num = get_bits1(&ctx->gb) ? get_bits(&ctx->gb, 20) : 0;\n if (get_bits1(&ctx->gb))\n skip_bits(&ctx->gb, 8);\n if (ff_ivi_dec_huff_desc(&ctx->gb, get_bits1(&ctx->gb), IVI_MB_HUFF, &ctx->mb_vlc, avctx) ||\n ff_ivi_dec_huff_desc(&ctx->gb, get_bits1(&ctx->gb), IVI_BLK_HUFF, &ctx->blk_vlc, avctx))\n return AVERROR_INVALIDDATA;\n ctx->rvmap_sel = get_bits1(&ctx->gb) ? get_bits(&ctx->gb, 3) : 8;\n ctx->in_imf = get_bits1(&ctx->gb);\n ctx->in_q = get_bits1(&ctx->gb);\n ctx->pic_glob_quant = get_bits(&ctx->gb, 5);\n ctx->unknown1 = get_bits1(&ctx->gb) ? get_bits(&ctx->gb, 3) : 0;\n ctx->checksum = get_bits1(&ctx->gb) ? get_bits(&ctx->gb, 16) : 0;\n while (get_bits1(&ctx->gb)) {\n av_dlog(avctx, "Pic hdr extension encountered!\\n");\n skip_bits(&ctx->gb, 8);\n }\n if (get_bits1(&ctx->gb)) {\n av_log(avctx, AV_LOG_ERROR, "Bad blocks bits encountered!\\n");\n }\n align_get_bits(&ctx->gb);\n return 0;\n}']
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.