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 |
|---|---|---|---|---|
35,101 | 0 | https://github.com/libav/libav/blob/124c21d79f2124d028890022e98ea853a834a964/libavcodec/dnxhdenc.c/#L149 | static int dnxhd_init_rc(DNXHDEncContext *ctx)
{
CHECKED_ALLOCZ(ctx->mb_rc, 8160*ctx->m.avctx->qmax*sizeof(RCEntry));
if (ctx->m.avctx->mb_decision != FF_MB_DECISION_RD)
CHECKED_ALLOCZ(ctx->mb_cmp, ctx->m.mb_num*sizeof(RCCMPEntry));
ctx->frame_bits = (ctx->cid_table->coding_unit_size - 640 - 4) * 8;
ctx->qscale = 1;
ctx->lambda = 2<<LAMBDA_FRAC_BITS;
return 0;
fail:
return -1;
} | ['static int dnxhd_encode_init(AVCodecContext *avctx)\n{\n DNXHDEncContext *ctx = avctx->priv_data;\n int i, index;\n ctx->cid = ff_dnxhd_find_cid(avctx);\n if (!ctx->cid || avctx->pix_fmt != PIX_FMT_YUV422P) {\n av_log(avctx, AV_LOG_ERROR, "video parameters incompatible with DNxHD\\n");\n return -1;\n }\n av_log(avctx, AV_LOG_DEBUG, "cid %d\\n", ctx->cid);\n index = ff_dnxhd_get_cid_table(ctx->cid);\n ctx->cid_table = &ff_dnxhd_cid_table[index];\n ctx->m.avctx = avctx;\n ctx->m.mb_intra = 1;\n ctx->m.h263_aic = 1;\n ctx->get_pixels_8x4_sym = dnxhd_get_pixels_8x4;\n dsputil_init(&ctx->m.dsp, avctx);\n ff_dct_common_init(&ctx->m);\n#ifdef HAVE_MMX\n ff_dnxhd_init_mmx(ctx);\n#endif\n if (!ctx->m.dct_quantize)\n ctx->m.dct_quantize = dct_quantize_c;\n ctx->m.mb_height = (avctx->height + 15) / 16;\n ctx->m.mb_width = (avctx->width + 15) / 16;\n if (avctx->flags & CODEC_FLAG_INTERLACED_DCT) {\n ctx->interlaced = 1;\n ctx->m.mb_height /= 2;\n }\n ctx->m.mb_num = ctx->m.mb_height * ctx->m.mb_width;\n if (avctx->intra_quant_bias != FF_DEFAULT_QUANT_BIAS)\n ctx->m.intra_quant_bias = avctx->intra_quant_bias;\n if (dnxhd_init_qmat(ctx, ctx->m.intra_quant_bias, 0) < 0)\n return -1;\n if (dnxhd_init_vlc(ctx) < 0)\n return -1;\n if (dnxhd_init_rc(ctx) < 0)\n return -1;\n CHECKED_ALLOCZ(ctx->slice_size, ctx->m.mb_height*sizeof(uint32_t));\n CHECKED_ALLOCZ(ctx->mb_bits, ctx->m.mb_num *sizeof(uint16_t));\n CHECKED_ALLOCZ(ctx->mb_qscale, ctx->m.mb_num *sizeof(uint8_t));\n ctx->frame.key_frame = 1;\n ctx->frame.pict_type = FF_I_TYPE;\n ctx->m.avctx->coded_frame = &ctx->frame;\n if (avctx->thread_count > MAX_THREADS || (avctx->thread_count > ctx->m.mb_height)) {\n av_log(avctx, AV_LOG_ERROR, "too many threads\\n");\n return -1;\n }\n ctx->thread[0] = ctx;\n for (i = 1; i < avctx->thread_count; i++) {\n ctx->thread[i] = av_malloc(sizeof(DNXHDEncContext));\n memcpy(ctx->thread[i], ctx, sizeof(DNXHDEncContext));\n }\n for (i = 0; i < avctx->thread_count; i++) {\n ctx->thread[i]->m.start_mb_y = (ctx->m.mb_height*(i ) + avctx->thread_count/2) / avctx->thread_count;\n ctx->thread[i]->m.end_mb_y = (ctx->m.mb_height*(i+1) + avctx->thread_count/2) / avctx->thread_count;\n }\n return 0;\n fail:\n return -1;\n}', 'void dsputil_init(DSPContext* c, AVCodecContext *avctx)\n{\n int i;\n ff_check_alignment();\n#ifdef CONFIG_ENCODERS\n if(avctx->dct_algo==FF_DCT_FASTINT) {\n c->fdct = fdct_ifast;\n c->fdct248 = fdct_ifast248;\n }\n else if(avctx->dct_algo==FF_DCT_FAAN) {\n c->fdct = ff_faandct;\n c->fdct248 = ff_faandct248;\n }\n else {\n c->fdct = ff_jpeg_fdct_islow;\n c->fdct248 = ff_fdct248_islow;\n }\n#endif\n if(avctx->lowres==1){\n if(avctx->idct_algo==FF_IDCT_INT || avctx->idct_algo==FF_IDCT_AUTO || !ENABLE_H264_DECODER){\n c->idct_put= ff_jref_idct4_put;\n c->idct_add= ff_jref_idct4_add;\n }else{\n c->idct_put= ff_h264_lowres_idct_put_c;\n c->idct_add= ff_h264_lowres_idct_add_c;\n }\n c->idct = j_rev_dct4;\n c->idct_permutation_type= FF_NO_IDCT_PERM;\n }else if(avctx->lowres==2){\n c->idct_put= ff_jref_idct2_put;\n c->idct_add= ff_jref_idct2_add;\n c->idct = j_rev_dct2;\n c->idct_permutation_type= FF_NO_IDCT_PERM;\n }else if(avctx->lowres==3){\n c->idct_put= ff_jref_idct1_put;\n c->idct_add= ff_jref_idct1_add;\n c->idct = j_rev_dct1;\n c->idct_permutation_type= FF_NO_IDCT_PERM;\n }else{\n if(avctx->idct_algo==FF_IDCT_INT){\n c->idct_put= ff_jref_idct_put;\n c->idct_add= ff_jref_idct_add;\n c->idct = j_rev_dct;\n c->idct_permutation_type= FF_LIBMPEG2_IDCT_PERM;\n }else if((ENABLE_VP3_DECODER || ENABLE_VP5_DECODER || ENABLE_VP6_DECODER || ENABLE_THEORA_DECODER ) &&\n avctx->idct_algo==FF_IDCT_VP3){\n c->idct_put= ff_vp3_idct_put_c;\n c->idct_add= ff_vp3_idct_add_c;\n c->idct = ff_vp3_idct_c;\n c->idct_permutation_type= FF_NO_IDCT_PERM;\n }else if(avctx->idct_algo==FF_IDCT_WMV2){\n c->idct_put= ff_wmv2_idct_put_c;\n c->idct_add= ff_wmv2_idct_add_c;\n c->idct = ff_wmv2_idct_c;\n c->idct_permutation_type= FF_NO_IDCT_PERM;\n }else if(avctx->idct_algo==FF_IDCT_FAAN){\n c->idct_put= ff_faanidct_put;\n c->idct_add= ff_faanidct_add;\n c->idct = ff_faanidct;\n c->idct_permutation_type= FF_NO_IDCT_PERM;\n }else if(ENABLE_EATGQ_DECODER && avctx->idct_algo==FF_IDCT_EA) {\n c->idct_put= ff_ea_idct_put_c;\n c->idct_permutation_type= FF_NO_IDCT_PERM;\n }else{\n c->idct_put= ff_simple_idct_put;\n c->idct_add= ff_simple_idct_add;\n c->idct = ff_simple_idct;\n c->idct_permutation_type= FF_NO_IDCT_PERM;\n }\n }\n if (ENABLE_H264_DECODER) {\n c->h264_idct_add= ff_h264_idct_add_c;\n c->h264_idct8_add= ff_h264_idct8_add_c;\n c->h264_idct_dc_add= ff_h264_idct_dc_add_c;\n c->h264_idct8_dc_add= ff_h264_idct8_dc_add_c;\n }\n c->get_pixels = get_pixels_c;\n c->diff_pixels = diff_pixels_c;\n c->put_pixels_clamped = put_pixels_clamped_c;\n c->put_signed_pixels_clamped = put_signed_pixels_clamped_c;\n c->add_pixels_clamped = add_pixels_clamped_c;\n c->add_pixels8 = add_pixels8_c;\n c->add_pixels4 = add_pixels4_c;\n c->sum_abs_dctelem = sum_abs_dctelem_c;\n c->gmc1 = gmc1_c;\n c->gmc = ff_gmc_c;\n c->clear_block = clear_block_c;\n c->clear_blocks = clear_blocks_c;\n c->pix_sum = pix_sum_c;\n c->pix_norm1 = pix_norm1_c;\n c->pix_abs[0][0] = pix_abs16_c;\n c->pix_abs[0][1] = pix_abs16_x2_c;\n c->pix_abs[0][2] = pix_abs16_y2_c;\n c->pix_abs[0][3] = pix_abs16_xy2_c;\n c->pix_abs[1][0] = pix_abs8_c;\n c->pix_abs[1][1] = pix_abs8_x2_c;\n c->pix_abs[1][2] = pix_abs8_y2_c;\n c->pix_abs[1][3] = pix_abs8_xy2_c;\n#define dspfunc(PFX, IDX, NUM) \\\n c->PFX ## _pixels_tab[IDX][0] = PFX ## _pixels ## NUM ## _c; \\\n c->PFX ## _pixels_tab[IDX][1] = PFX ## _pixels ## NUM ## _x2_c; \\\n c->PFX ## _pixels_tab[IDX][2] = PFX ## _pixels ## NUM ## _y2_c; \\\n c->PFX ## _pixels_tab[IDX][3] = PFX ## _pixels ## NUM ## _xy2_c\n dspfunc(put, 0, 16);\n dspfunc(put_no_rnd, 0, 16);\n dspfunc(put, 1, 8);\n dspfunc(put_no_rnd, 1, 8);\n dspfunc(put, 2, 4);\n dspfunc(put, 3, 2);\n dspfunc(avg, 0, 16);\n dspfunc(avg_no_rnd, 0, 16);\n dspfunc(avg, 1, 8);\n dspfunc(avg_no_rnd, 1, 8);\n dspfunc(avg, 2, 4);\n dspfunc(avg, 3, 2);\n#undef dspfunc\n c->put_no_rnd_pixels_l2[0]= put_no_rnd_pixels16_l2_c;\n c->put_no_rnd_pixels_l2[1]= put_no_rnd_pixels8_l2_c;\n c->put_tpel_pixels_tab[ 0] = put_tpel_pixels_mc00_c;\n c->put_tpel_pixels_tab[ 1] = put_tpel_pixels_mc10_c;\n c->put_tpel_pixels_tab[ 2] = put_tpel_pixels_mc20_c;\n c->put_tpel_pixels_tab[ 4] = put_tpel_pixels_mc01_c;\n c->put_tpel_pixels_tab[ 5] = put_tpel_pixels_mc11_c;\n c->put_tpel_pixels_tab[ 6] = put_tpel_pixels_mc21_c;\n c->put_tpel_pixels_tab[ 8] = put_tpel_pixels_mc02_c;\n c->put_tpel_pixels_tab[ 9] = put_tpel_pixels_mc12_c;\n c->put_tpel_pixels_tab[10] = put_tpel_pixels_mc22_c;\n c->avg_tpel_pixels_tab[ 0] = avg_tpel_pixels_mc00_c;\n c->avg_tpel_pixels_tab[ 1] = avg_tpel_pixels_mc10_c;\n c->avg_tpel_pixels_tab[ 2] = avg_tpel_pixels_mc20_c;\n c->avg_tpel_pixels_tab[ 4] = avg_tpel_pixels_mc01_c;\n c->avg_tpel_pixels_tab[ 5] = avg_tpel_pixels_mc11_c;\n c->avg_tpel_pixels_tab[ 6] = avg_tpel_pixels_mc21_c;\n c->avg_tpel_pixels_tab[ 8] = avg_tpel_pixels_mc02_c;\n c->avg_tpel_pixels_tab[ 9] = avg_tpel_pixels_mc12_c;\n c->avg_tpel_pixels_tab[10] = avg_tpel_pixels_mc22_c;\n#define dspfunc(PFX, IDX, NUM) \\\n c->PFX ## _pixels_tab[IDX][ 0] = PFX ## NUM ## _mc00_c; \\\n c->PFX ## _pixels_tab[IDX][ 1] = PFX ## NUM ## _mc10_c; \\\n c->PFX ## _pixels_tab[IDX][ 2] = PFX ## NUM ## _mc20_c; \\\n c->PFX ## _pixels_tab[IDX][ 3] = PFX ## NUM ## _mc30_c; \\\n c->PFX ## _pixels_tab[IDX][ 4] = PFX ## NUM ## _mc01_c; \\\n c->PFX ## _pixels_tab[IDX][ 5] = PFX ## NUM ## _mc11_c; \\\n c->PFX ## _pixels_tab[IDX][ 6] = PFX ## NUM ## _mc21_c; \\\n c->PFX ## _pixels_tab[IDX][ 7] = PFX ## NUM ## _mc31_c; \\\n c->PFX ## _pixels_tab[IDX][ 8] = PFX ## NUM ## _mc02_c; \\\n c->PFX ## _pixels_tab[IDX][ 9] = PFX ## NUM ## _mc12_c; \\\n c->PFX ## _pixels_tab[IDX][10] = PFX ## NUM ## _mc22_c; \\\n c->PFX ## _pixels_tab[IDX][11] = PFX ## NUM ## _mc32_c; \\\n c->PFX ## _pixels_tab[IDX][12] = PFX ## NUM ## _mc03_c; \\\n c->PFX ## _pixels_tab[IDX][13] = PFX ## NUM ## _mc13_c; \\\n c->PFX ## _pixels_tab[IDX][14] = PFX ## NUM ## _mc23_c; \\\n c->PFX ## _pixels_tab[IDX][15] = PFX ## NUM ## _mc33_c\n dspfunc(put_qpel, 0, 16);\n dspfunc(put_no_rnd_qpel, 0, 16);\n dspfunc(avg_qpel, 0, 16);\n dspfunc(put_qpel, 1, 8);\n dspfunc(put_no_rnd_qpel, 1, 8);\n dspfunc(avg_qpel, 1, 8);\n dspfunc(put_h264_qpel, 0, 16);\n dspfunc(put_h264_qpel, 1, 8);\n dspfunc(put_h264_qpel, 2, 4);\n dspfunc(put_h264_qpel, 3, 2);\n dspfunc(avg_h264_qpel, 0, 16);\n dspfunc(avg_h264_qpel, 1, 8);\n dspfunc(avg_h264_qpel, 2, 4);\n#undef dspfunc\n c->put_h264_chroma_pixels_tab[0]= put_h264_chroma_mc8_c;\n c->put_h264_chroma_pixels_tab[1]= put_h264_chroma_mc4_c;\n c->put_h264_chroma_pixels_tab[2]= put_h264_chroma_mc2_c;\n c->avg_h264_chroma_pixels_tab[0]= avg_h264_chroma_mc8_c;\n c->avg_h264_chroma_pixels_tab[1]= avg_h264_chroma_mc4_c;\n c->avg_h264_chroma_pixels_tab[2]= avg_h264_chroma_mc2_c;\n c->put_no_rnd_h264_chroma_pixels_tab[0]= put_no_rnd_h264_chroma_mc8_c;\n c->weight_h264_pixels_tab[0]= weight_h264_pixels16x16_c;\n c->weight_h264_pixels_tab[1]= weight_h264_pixels16x8_c;\n c->weight_h264_pixels_tab[2]= weight_h264_pixels8x16_c;\n c->weight_h264_pixels_tab[3]= weight_h264_pixels8x8_c;\n c->weight_h264_pixels_tab[4]= weight_h264_pixels8x4_c;\n c->weight_h264_pixels_tab[5]= weight_h264_pixels4x8_c;\n c->weight_h264_pixels_tab[6]= weight_h264_pixels4x4_c;\n c->weight_h264_pixels_tab[7]= weight_h264_pixels4x2_c;\n c->weight_h264_pixels_tab[8]= weight_h264_pixels2x4_c;\n c->weight_h264_pixels_tab[9]= weight_h264_pixels2x2_c;\n c->biweight_h264_pixels_tab[0]= biweight_h264_pixels16x16_c;\n c->biweight_h264_pixels_tab[1]= biweight_h264_pixels16x8_c;\n c->biweight_h264_pixels_tab[2]= biweight_h264_pixels8x16_c;\n c->biweight_h264_pixels_tab[3]= biweight_h264_pixels8x8_c;\n c->biweight_h264_pixels_tab[4]= biweight_h264_pixels8x4_c;\n c->biweight_h264_pixels_tab[5]= biweight_h264_pixels4x8_c;\n c->biweight_h264_pixels_tab[6]= biweight_h264_pixels4x4_c;\n c->biweight_h264_pixels_tab[7]= biweight_h264_pixels4x2_c;\n c->biweight_h264_pixels_tab[8]= biweight_h264_pixels2x4_c;\n c->biweight_h264_pixels_tab[9]= biweight_h264_pixels2x2_c;\n c->draw_edges = draw_edges_c;\n#ifdef CONFIG_CAVS_DECODER\n ff_cavsdsp_init(c,avctx);\n#endif\n#if defined(CONFIG_VC1_DECODER) || defined(CONFIG_WMV3_DECODER)\n ff_vc1dsp_init(c,avctx);\n#endif\n#if defined(CONFIG_WMV2_DECODER) || defined(CONFIG_VC1_DECODER) || defined(CONFIG_WMV3_DECODER)\n ff_intrax8dsp_init(c,avctx);\n#endif\n#if defined(CONFIG_H264_ENCODER)\n ff_h264dspenc_init(c,avctx);\n#endif\n#if defined(CONFIG_RV40_DECODER)\n ff_rv40dsp_init(c,avctx);\n c->put_rv40_qpel_pixels_tab[0][15] = put_rv40_qpel16_mc33_c;\n c->avg_rv40_qpel_pixels_tab[0][15] = avg_rv40_qpel16_mc33_c;\n c->put_rv40_qpel_pixels_tab[1][15] = put_rv40_qpel8_mc33_c;\n c->avg_rv40_qpel_pixels_tab[1][15] = avg_rv40_qpel8_mc33_c;\n#endif\n c->put_mspel_pixels_tab[0]= put_mspel8_mc00_c;\n c->put_mspel_pixels_tab[1]= put_mspel8_mc10_c;\n c->put_mspel_pixels_tab[2]= put_mspel8_mc20_c;\n c->put_mspel_pixels_tab[3]= put_mspel8_mc30_c;\n c->put_mspel_pixels_tab[4]= put_mspel8_mc02_c;\n c->put_mspel_pixels_tab[5]= put_mspel8_mc12_c;\n c->put_mspel_pixels_tab[6]= put_mspel8_mc22_c;\n c->put_mspel_pixels_tab[7]= put_mspel8_mc32_c;\n#define SET_CMP_FUNC(name) \\\n c->name[0]= name ## 16_c;\\\n c->name[1]= name ## 8x8_c;\n SET_CMP_FUNC(hadamard8_diff)\n c->hadamard8_diff[4]= hadamard8_intra16_c;\n SET_CMP_FUNC(dct_sad)\n SET_CMP_FUNC(dct_max)\n#ifdef CONFIG_GPL\n SET_CMP_FUNC(dct264_sad)\n#endif\n c->sad[0]= pix_abs16_c;\n c->sad[1]= pix_abs8_c;\n c->sse[0]= sse16_c;\n c->sse[1]= sse8_c;\n c->sse[2]= sse4_c;\n SET_CMP_FUNC(quant_psnr)\n SET_CMP_FUNC(rd)\n SET_CMP_FUNC(bit)\n c->vsad[0]= vsad16_c;\n c->vsad[4]= vsad_intra16_c;\n c->vsse[0]= vsse16_c;\n c->vsse[4]= vsse_intra16_c;\n c->nsse[0]= nsse16_c;\n c->nsse[1]= nsse8_c;\n#ifdef CONFIG_SNOW_ENCODER\n c->w53[0]= w53_16_c;\n c->w53[1]= w53_8_c;\n c->w97[0]= w97_16_c;\n c->w97[1]= w97_8_c;\n#endif\n c->ssd_int8_vs_int16 = ssd_int8_vs_int16_c;\n c->add_bytes= add_bytes_c;\n c->add_bytes_l2= add_bytes_l2_c;\n c->diff_bytes= diff_bytes_c;\n c->sub_hfyu_median_prediction= sub_hfyu_median_prediction_c;\n c->bswap_buf= bswap_buf;\n#ifdef CONFIG_PNG_DECODER\n c->add_png_paeth_prediction= ff_add_png_paeth_prediction;\n#endif\n c->h264_v_loop_filter_luma= h264_v_loop_filter_luma_c;\n c->h264_h_loop_filter_luma= h264_h_loop_filter_luma_c;\n c->h264_v_loop_filter_chroma= h264_v_loop_filter_chroma_c;\n c->h264_h_loop_filter_chroma= h264_h_loop_filter_chroma_c;\n c->h264_v_loop_filter_chroma_intra= h264_v_loop_filter_chroma_intra_c;\n c->h264_h_loop_filter_chroma_intra= h264_h_loop_filter_chroma_intra_c;\n c->h264_loop_filter_strength= NULL;\n if (ENABLE_ANY_H263) {\n c->h263_h_loop_filter= h263_h_loop_filter_c;\n c->h263_v_loop_filter= h263_v_loop_filter_c;\n }\n if (ENABLE_VP3_DECODER || ENABLE_THEORA_DECODER) {\n c->vp3_h_loop_filter= ff_vp3_h_loop_filter_c;\n c->vp3_v_loop_filter= ff_vp3_v_loop_filter_c;\n }\n c->h261_loop_filter= h261_loop_filter_c;\n c->try_8x8basis= try_8x8basis_c;\n c->add_8x8basis= add_8x8basis_c;\n#ifdef CONFIG_SNOW_DECODER\n c->vertical_compose97i = ff_snow_vertical_compose97i;\n c->horizontal_compose97i = ff_snow_horizontal_compose97i;\n c->inner_add_yblock = ff_snow_inner_add_yblock;\n#endif\n#ifdef CONFIG_VORBIS_DECODER\n c->vorbis_inverse_coupling = vorbis_inverse_coupling;\n#endif\n#ifdef CONFIG_AC3_DECODER\n c->ac3_downmix = ff_ac3_downmix_c;\n#endif\n#ifdef CONFIG_FLAC_ENCODER\n c->flac_compute_autocorr = ff_flac_compute_autocorr;\n#endif\n c->vector_fmul = vector_fmul_c;\n c->vector_fmul_reverse = vector_fmul_reverse_c;\n c->vector_fmul_add_add = ff_vector_fmul_add_add_c;\n c->vector_fmul_window = ff_vector_fmul_window_c;\n c->int32_to_float_fmul_scalar = int32_to_float_fmul_scalar_c;\n c->float_to_int16 = ff_float_to_int16_c;\n c->float_to_int16_interleave = ff_float_to_int16_interleave_c;\n c->add_int16 = add_int16_c;\n c->sub_int16 = sub_int16_c;\n c->scalarproduct_int16 = scalarproduct_int16_c;\n c->shrink[0]= ff_img_copy_plane;\n c->shrink[1]= ff_shrink22;\n c->shrink[2]= ff_shrink44;\n c->shrink[3]= ff_shrink88;\n c->prefetch= just_return;\n memset(c->put_2tap_qpel_pixels_tab, 0, sizeof(c->put_2tap_qpel_pixels_tab));\n memset(c->avg_2tap_qpel_pixels_tab, 0, sizeof(c->avg_2tap_qpel_pixels_tab));\n if (ENABLE_MMX) dsputil_init_mmx (c, avctx);\n if (ENABLE_ARM) dsputil_init_arm (c, avctx);\n if (ENABLE_MLIB) dsputil_init_mlib (c, avctx);\n if (ENABLE_VIS) dsputil_init_vis (c, avctx);\n if (ENABLE_ALPHA) dsputil_init_alpha (c, avctx);\n if (ENABLE_POWERPC) dsputil_init_ppc (c, avctx);\n if (ENABLE_MMI) dsputil_init_mmi (c, avctx);\n if (ENABLE_SH4) dsputil_init_sh4 (c, avctx);\n if (ENABLE_BFIN) dsputil_init_bfin (c, avctx);\n for(i=0; i<64; i++){\n if(!c->put_2tap_qpel_pixels_tab[0][i])\n c->put_2tap_qpel_pixels_tab[0][i]= c->put_h264_qpel_pixels_tab[0][i];\n if(!c->avg_2tap_qpel_pixels_tab[0][i])\n c->avg_2tap_qpel_pixels_tab[0][i]= c->avg_h264_qpel_pixels_tab[0][i];\n }\n switch(c->idct_permutation_type){\n case FF_NO_IDCT_PERM:\n for(i=0; i<64; i++)\n c->idct_permutation[i]= i;\n break;\n case FF_LIBMPEG2_IDCT_PERM:\n for(i=0; i<64; i++)\n c->idct_permutation[i]= (i & 0x38) | ((i & 6) >> 1) | ((i & 1) << 2);\n break;\n case FF_SIMPLE_IDCT_PERM:\n for(i=0; i<64; i++)\n c->idct_permutation[i]= simple_mmx_permutation[i];\n break;\n case FF_TRANSPOSE_IDCT_PERM:\n for(i=0; i<64; i++)\n c->idct_permutation[i]= ((i&7)<<3) | (i>>3);\n break;\n case FF_PARTTRANS_IDCT_PERM:\n for(i=0; i<64; i++)\n c->idct_permutation[i]= (i&0x24) | ((i&3)<<3) | ((i>>3)&3);\n break;\n case FF_SSE2_IDCT_PERM:\n for(i=0; i<64; i++)\n c->idct_permutation[i]= (i&0x38) | idct_sse2_row_perm[i&7];\n break;\n default:\n av_log(avctx, AV_LOG_ERROR, "Internal error, IDCT permutation not set\\n");\n }\n}', 'int ff_dct_common_init(MpegEncContext *s)\n{\n s->dct_unquantize_h263_intra = dct_unquantize_h263_intra_c;\n s->dct_unquantize_h263_inter = dct_unquantize_h263_inter_c;\n s->dct_unquantize_mpeg1_intra = dct_unquantize_mpeg1_intra_c;\n s->dct_unquantize_mpeg1_inter = dct_unquantize_mpeg1_inter_c;\n s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_c;\n if(s->flags & CODEC_FLAG_BITEXACT)\n s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_bitexact;\n s->dct_unquantize_mpeg2_inter = dct_unquantize_mpeg2_inter_c;\n#if defined(HAVE_MMX)\n MPV_common_init_mmx(s);\n#elif defined(ARCH_ALPHA)\n MPV_common_init_axp(s);\n#elif defined(CONFIG_MLIB)\n MPV_common_init_mlib(s);\n#elif defined(HAVE_MMI)\n MPV_common_init_mmi(s);\n#elif defined(ARCH_ARM)\n MPV_common_init_arm(s);\n#elif defined(HAVE_ALTIVEC)\n MPV_common_init_altivec(s);\n#elif defined(ARCH_BFIN)\n MPV_common_init_bfin(s);\n#endif\n if(s->alternate_scan){\n ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_alternate_vertical_scan);\n ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_alternate_vertical_scan);\n }else{\n ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_zigzag_direct);\n ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_zigzag_direct);\n }\n ff_init_scantable(s->dsp.idct_permutation, &s->intra_h_scantable, ff_alternate_horizontal_scan);\n ff_init_scantable(s->dsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan);\n return 0;\n}', 'void ff_init_scantable(uint8_t *permutation, ScanTable *st, const uint8_t *src_scantable){\n int i;\n int end;\n st->scantable= src_scantable;\n for(i=0; i<64; i++){\n int j;\n j = src_scantable[i];\n st->permutated[i] = permutation[j];\n#ifdef ARCH_POWERPC\n st->inverse[j] = i;\n#endif\n }\n end=-1;\n for(i=0; i<64; i++){\n int j;\n j = st->permutated[i];\n if(j>end) end=j;\n st->raster_end[i]= end;\n }\n}', 'static int dnxhd_init_qmat(DNXHDEncContext *ctx, int lbias, int cbias)\n{\n uint16_t weight_matrix[64] = {1,};\n int qscale, i;\n CHECKED_ALLOCZ(ctx->qmatrix_l, (ctx->m.avctx->qmax+1) * 64 * sizeof(int));\n CHECKED_ALLOCZ(ctx->qmatrix_c, (ctx->m.avctx->qmax+1) * 64 * sizeof(int));\n CHECKED_ALLOCZ(ctx->qmatrix_l16, (ctx->m.avctx->qmax+1) * 64 * 2 * sizeof(uint16_t));\n CHECKED_ALLOCZ(ctx->qmatrix_c16, (ctx->m.avctx->qmax+1) * 64 * 2 * sizeof(uint16_t));\n for (i = 1; i < 64; i++) {\n int j = ctx->m.dsp.idct_permutation[ff_zigzag_direct[i]];\n weight_matrix[j] = ctx->cid_table->luma_weight[i];\n }\n ff_convert_matrix(&ctx->m.dsp, ctx->qmatrix_l, ctx->qmatrix_l16, weight_matrix,\n ctx->m.intra_quant_bias, 1, ctx->m.avctx->qmax, 1);\n for (i = 1; i < 64; i++) {\n int j = ctx->m.dsp.idct_permutation[ff_zigzag_direct[i]];\n weight_matrix[j] = ctx->cid_table->chroma_weight[i];\n }\n ff_convert_matrix(&ctx->m.dsp, ctx->qmatrix_c, ctx->qmatrix_c16, weight_matrix,\n ctx->m.intra_quant_bias, 1, ctx->m.avctx->qmax, 1);\n for (qscale = 1; qscale <= ctx->m.avctx->qmax; qscale++) {\n for (i = 0; i < 64; i++) {\n ctx->qmatrix_l [qscale] [i] <<= 2; ctx->qmatrix_c [qscale] [i] <<= 2;\n ctx->qmatrix_l16[qscale][0][i] <<= 2; ctx->qmatrix_l16[qscale][1][i] <<= 2;\n ctx->qmatrix_c16[qscale][0][i] <<= 2; ctx->qmatrix_c16[qscale][1][i] <<= 2;\n }\n }\n return 0;\n fail:\n return -1;\n}', 'static int dnxhd_init_vlc(DNXHDEncContext *ctx)\n{\n int i, j, level, run;\n int max_level = 1<<(ctx->cid_table->bit_depth+2);\n CHECKED_ALLOCZ(ctx->vlc_codes, max_level*4*sizeof(*ctx->vlc_codes));\n CHECKED_ALLOCZ(ctx->vlc_bits, max_level*4*sizeof(*ctx->vlc_bits));\n CHECKED_ALLOCZ(ctx->run_codes, 63*2);\n CHECKED_ALLOCZ(ctx->run_bits, 63);\n ctx->vlc_codes += max_level*2;\n ctx->vlc_bits += max_level*2;\n for (level = -max_level; level < max_level; level++) {\n for (run = 0; run < 2; run++) {\n int index = (level<<1)|run;\n int sign, offset = 0, alevel = level;\n MASK_ABS(sign, alevel);\n if (alevel > 64) {\n offset = (alevel-1)>>6;\n alevel -= offset<<6;\n }\n for (j = 0; j < 257; j++) {\n if (ctx->cid_table->ac_level[j] == alevel &&\n (!offset || (ctx->cid_table->ac_index_flag[j] && offset)) &&\n (!run || (ctx->cid_table->ac_run_flag [j] && run))) {\n assert(!ctx->vlc_codes[index]);\n if (alevel) {\n ctx->vlc_codes[index] = (ctx->cid_table->ac_codes[j]<<1)|(sign&1);\n ctx->vlc_bits [index] = ctx->cid_table->ac_bits[j]+1;\n } else {\n ctx->vlc_codes[index] = ctx->cid_table->ac_codes[j];\n ctx->vlc_bits [index] = ctx->cid_table->ac_bits [j];\n }\n break;\n }\n }\n assert(!alevel || j < 257);\n if (offset) {\n ctx->vlc_codes[index] = (ctx->vlc_codes[index]<<ctx->cid_table->index_bits)|offset;\n ctx->vlc_bits [index]+= ctx->cid_table->index_bits;\n }\n }\n }\n for (i = 0; i < 62; i++) {\n int run = ctx->cid_table->run[i];\n assert(run < 63);\n ctx->run_codes[run] = ctx->cid_table->run_codes[i];\n ctx->run_bits [run] = ctx->cid_table->run_bits[i];\n }\n return 0;\n fail:\n return -1;\n}', 'static int dnxhd_init_rc(DNXHDEncContext *ctx)\n{\n CHECKED_ALLOCZ(ctx->mb_rc, 8160*ctx->m.avctx->qmax*sizeof(RCEntry));\n if (ctx->m.avctx->mb_decision != FF_MB_DECISION_RD)\n CHECKED_ALLOCZ(ctx->mb_cmp, ctx->m.mb_num*sizeof(RCCMPEntry));\n ctx->frame_bits = (ctx->cid_table->coding_unit_size - 640 - 4) * 8;\n ctx->qscale = 1;\n ctx->lambda = 2<<LAMBDA_FRAC_BITS;\n return 0;\n fail:\n return -1;\n}'] |
35,102 | 0 | https://github.com/libav/libav/blob/cf6bae6883607f83f3b042b7b9d711197f736e2a/libavcodec/ffv1.c/#L238 | static inline int predict(int_fast16_t *src, int_fast16_t *last){
const int LT= last[-1];
const int T= last[ 0];
const int L = src[-1];
return mid_pred(L, L + T - LT, T);
} | ['static void decode_plane(FFV1Context *s, uint8_t *src, int w, int h, int stride, int plane_index){\n int x, y;\n int_fast16_t sample_buffer[2][w+6];\n int_fast16_t *sample[2];\n sample[0]=sample_buffer[0]+3;\n sample[1]=sample_buffer[1]+3;\n s->run_index=0;\n memset(sample_buffer, 0, sizeof(sample_buffer));\n for(y=0; y<h; y++){\n int_fast16_t *temp= sample[0];\n sample[0]= sample[1];\n sample[1]= temp;\n sample[1][-1]= sample[0][0 ];\n sample[0][ w]= sample[0][w-1];\n if(s->avctx->bits_per_raw_sample <= 8){\n decode_line(s, w, sample, plane_index, 8);\n for(x=0; x<w; x++){\n src[x + stride*y]= sample[1][x];\n }\n }else{\n decode_line(s, w, sample, plane_index, s->avctx->bits_per_raw_sample);\n for(x=0; x<w; x++){\n ((uint16_t*)(src + stride*y))[x]= sample[1][x] << (16 - s->avctx->bits_per_raw_sample);\n }\n }\n }\n}', 'static av_always_inline void decode_line(FFV1Context *s, int w, int_fast16_t *sample[2], int plane_index, int bits){\n PlaneContext * const p= &s->plane[plane_index];\n RangeCoder * const c= &s->c;\n int x;\n int run_count=0;\n int run_mode=0;\n int run_index= s->run_index;\n for(x=0; x<w; x++){\n int diff, context, sign;\n context= get_context(s, sample[1] + x, sample[0] + x, sample[1] + x);\n if(context < 0){\n context= -context;\n sign=1;\n }else\n sign=0;\n if(s->ac){\n diff= get_symbol_inline(c, p->state[context], 1);\n }else{\n if(context == 0 && run_mode==0) run_mode=1;\n if(run_mode){\n if(run_count==0 && run_mode==1){\n if(get_bits1(&s->gb)){\n run_count = 1<<ff_log2_run[run_index];\n if(x + run_count <= w) run_index++;\n }else{\n if(ff_log2_run[run_index]) run_count = get_bits(&s->gb, ff_log2_run[run_index]);\n else run_count=0;\n if(run_index) run_index--;\n run_mode=2;\n }\n }\n run_count--;\n if(run_count < 0){\n run_mode=0;\n run_count=0;\n diff= get_vlc_symbol(&s->gb, &p->vlc_state[context], bits);\n if(diff>=0) diff++;\n }else\n diff=0;\n }else\n diff= get_vlc_symbol(&s->gb, &p->vlc_state[context], bits);\n }\n if(sign) diff= -diff;\n sample[1][x]= (predict(sample[1] + x, sample[0] + x) + diff) & ((1<<bits)-1);\n }\n s->run_index= run_index;\n}', 'static inline int predict(int_fast16_t *src, int_fast16_t *last){\n const int LT= last[-1];\n const int T= last[ 0];\n const int L = src[-1];\n return mid_pred(L, L + T - LT, T);\n}'] |
35,103 | 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;
} | ['int ec_GFp_mont_group_set_curve(EC_GROUP *group, const BIGNUM *p,\n const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)\n{\n BN_CTX *new_ctx = NULL;\n BN_MONT_CTX *mont = NULL;\n BIGNUM *one = NULL;\n int ret = 0;\n BN_MONT_CTX_free(group->field_data1);\n group->field_data1 = NULL;\n BN_free(group->field_data2);\n group->field_data2 = NULL;\n if (ctx == NULL) {\n ctx = new_ctx = BN_CTX_new();\n if (ctx == NULL)\n return 0;\n }\n mont = BN_MONT_CTX_new();\n if (mont == NULL)\n goto err;\n if (!BN_MONT_CTX_set(mont, p, ctx)) {\n ECerr(EC_F_EC_GFP_MONT_GROUP_SET_CURVE, ERR_R_BN_LIB);\n goto err;\n }\n one = BN_new();\n if (one == NULL)\n goto err;\n if (!BN_to_montgomery(one, BN_value_one(), mont, ctx))\n goto err;\n group->field_data1 = mont;\n mont = NULL;\n group->field_data2 = one;\n one = NULL;\n ret = ec_GFp_simple_group_set_curve(group, p, a, b, ctx);\n if (!ret) {\n BN_MONT_CTX_free(group->field_data1);\n group->field_data1 = NULL;\n BN_free(group->field_data2);\n group->field_data2 = NULL;\n }\n err:\n BN_free(one);\n BN_CTX_free(new_ctx);\n BN_MONT_CTX_free(mont);\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 if (BN_get_flags(mod, BN_FLG_CONSTTIME) != 0)\n BN_set_flags(&(mont->N), BN_FLG_CONSTTIME);\n mont->N.neg = 0;\n#ifdef MONT_WORD\n {\n BIGNUM tmod;\n BN_ULONG buf[2];\n bn_init(&tmod);\n tmod.d = buf;\n tmod.dmax = 2;\n tmod.neg = 0;\n if (BN_get_flags(mod, BN_FLG_CONSTTIME) != 0)\n BN_set_flags(&tmod, BN_FLG_CONSTTIME);\n mont->ri = (BN_num_bits(mod) + (BN_BITS2 - 1)) / BN_BITS2 * BN_BITS2;\n# if defined(OPENSSL_BN_ASM_MONT) && (BN_BITS2<=32)\n BN_zero(R);\n if (!(BN_set_bit(R, 2 * BN_BITS2)))\n goto err;\n tmod.top = 0;\n if ((buf[0] = mod->d[0]))\n tmod.top = 1;\n if ((buf[1] = mod->top > 1 ? mod->d[1] : 0))\n tmod.top = 2;\n if (BN_is_one(&tmod))\n BN_zero(Ri);\n else if ((BN_mod_inverse(Ri, R, &tmod, ctx)) == NULL)\n goto err;\n if (!BN_lshift(Ri, Ri, 2 * BN_BITS2))\n goto err;\n if (!BN_is_zero(Ri)) {\n if (!BN_sub_word(Ri, 1))\n goto err;\n } else {\n if (bn_expand(Ri, (int)sizeof(BN_ULONG) * 2) == NULL)\n goto err;\n Ri->neg = 0;\n Ri->d[0] = BN_MASK2;\n Ri->d[1] = BN_MASK2;\n Ri->top = 2;\n }\n if (!BN_div(Ri, NULL, Ri, &tmod, ctx))\n goto err;\n mont->n0[0] = (Ri->top > 0) ? Ri->d[0] : 0;\n mont->n0[1] = (Ri->top > 1) ? Ri->d[1] : 0;\n# else\n BN_zero(R);\n if (!(BN_set_bit(R, BN_BITS2)))\n goto err;\n buf[0] = mod->d[0];\n buf[1] = 0;\n tmod.top = buf[0] != 0 ? 1 : 0;\n if (BN_is_one(&tmod))\n BN_zero(Ri);\n else if ((BN_mod_inverse(Ri, R, &tmod, ctx)) == NULL)\n goto err;\n if (!BN_lshift(Ri, Ri, BN_BITS2))\n goto err;\n if (!BN_is_zero(Ri)) {\n if (!BN_sub_word(Ri, 1))\n goto err;\n } else {\n if (!BN_set_word(Ri, BN_MASK2))\n goto err;\n }\n if (!BN_div(Ri, NULL, Ri, &tmod, ctx))\n goto err;\n mont->n0[0] = (Ri->top > 0) ? Ri->d[0] : 0;\n mont->n0[1] = 0;\n# endif\n }\n#else\n {\n mont->ri = BN_num_bits(&mont->N);\n BN_zero(R);\n if (!BN_set_bit(R, mont->ri))\n goto err;\n if ((BN_mod_inverse(Ri, R, &mont->N, ctx)) == NULL)\n goto err;\n if (!BN_lshift(Ri, Ri, mont->ri))\n goto err;\n if (!BN_sub_word(Ri, 1))\n goto err;\n if (!BN_div(&(mont->Ni), NULL, Ri, &mont->N, ctx))\n goto err;\n }\n#endif\n BN_zero(&(mont->RR));\n if (!BN_set_bit(&(mont->RR), mont->ri * 2))\n goto err;\n if (!BN_mod(&(mont->RR), &(mont->RR), &(mont->N), ctx))\n goto err;\n ret = 1;\n err:\n BN_CTX_end(ctx);\n return ret;\n}', 'BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)\n{\n bn_check_top(b);\n if (a == b)\n return a;\n if (bn_wexpand(a, b->top) == NULL)\n return NULL;\n if (b->top > 0)\n memcpy(a->d, b->d, sizeof(b->d[0]) * b->top);\n a->neg = b->neg;\n a->top = b->top;\n a->flags |= b->flags & BN_FLG_FIXED_TOP;\n bn_check_top(a);\n return a;\n}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n return (words <= a->dmax) ? a : bn_expand2(a, words);\n}', 'BIGNUM *bn_expand2(BIGNUM *b, int words)\n{\n if (words > b->dmax) {\n BN_ULONG *a = bn_expand_internal(b, words);\n if (!a)\n return NULL;\n if (b->d) {\n OPENSSL_cleanse(b->d, b->dmax * sizeof(b->d[0]));\n bn_free_d(b);\n }\n b->d = a;\n b->dmax = words;\n }\n return b;\n}', 'static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)\n{\n BN_ULONG *a = NULL;\n if (words > (INT_MAX / (4 * BN_BITS2))) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);\n return NULL;\n }\n if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);\n return NULL;\n }\n if (BN_get_flags(b, BN_FLG_SECURE))\n a = OPENSSL_secure_zalloc(words * sizeof(*a));\n else\n a = OPENSSL_zalloc(words * sizeof(*a));\n if (a == NULL) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);\n return NULL;\n }\n assert(b->top <= words);\n if (b->top > 0)\n memcpy(a, b->d, sizeof(*a) * b->top);\n return a;\n}', 'void *CRYPTO_zalloc(size_t num, const char *file, int line)\n{\n void *ret = CRYPTO_malloc(num, file, line);\n FAILTEST();\n if (ret != NULL)\n memset(ret, 0, num);\n return ret;\n}', 'void *CRYPTO_malloc(size_t num, const char *file, int line)\n{\n void *ret = NULL;\n INCREMENT(malloc_count);\n if (malloc_impl != NULL && malloc_impl != CRYPTO_malloc)\n return malloc_impl(num, file, line);\n if (num == 0)\n return NULL;\n FAILTEST();\n if (allow_customize) {\n allow_customize = 0;\n }\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n if (call_malloc_debug) {\n CRYPTO_mem_debug_malloc(NULL, num, 0, file, line);\n ret = malloc(num);\n CRYPTO_mem_debug_malloc(ret, num, 1, file, line);\n } else {\n ret = malloc(num);\n }\n#else\n (void)(file); (void)(line);\n ret = malloc(num);\n#endif\n return ret;\n}'] |
35,104 | 0 | https://github.com/openssl/openssl/blob/85bcf27cccd8f5f569886479ad96a0c33444404c/crypto/evp/e_aes.c/#L2553 | 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 = ctx->cipher_data;
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 (ctx->encrypt)
{
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 (ctx->encrypt)
{
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 (ctx->encrypt)
{
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 (!ctx->encrypt)
{
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\t\tconst unsigned char *in, size_t len)\n\t{\n\tunsigned char *buf;\n\tint *buf_len;\n\tint written_len = 0;\n\tsize_t trailing_len;\n\tEVP_AES_OCB_CTX *octx = ctx->cipher_data;\n\tif (!octx->iv_set)\n\t\treturn -1;\n\tif (!octx->key_set)\n\t\treturn -1;\n\tif (in)\n\t\t{\n\t\tif (out == NULL)\n\t\t\t{\n\t\t\tbuf = octx->aad_buf;\n\t\t\tbuf_len = &(octx->aad_buf_len);\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tbuf = octx->data_buf;\n\t\t\tbuf_len = &(octx->data_buf_len);\n\t\t\t}\n\t\tif(*buf_len)\n\t\t\t{\n\t\t\tunsigned int remaining;\n\t\t\tremaining = 16 - (*buf_len);\n\t\t\tif(remaining > len)\n\t\t\t\t{\n\t\t\t\tmemcpy(buf+(*buf_len), in, len);\n\t\t\t\t*(buf_len)+=len;\n\t\t\t\treturn 0;\n\t\t\t\t}\n\t\t\tmemcpy(buf+(*buf_len), in, remaining);\n\t\t\tlen -= remaining;\n\t\t\tin += remaining;\n\t\t\tif (out == NULL)\n\t\t\t\t{\n\t\t\t\tif(!CRYPTO_ocb128_aad(&octx->ocb, buf, 16))\n\t\t\t\t\treturn -1;\n\t\t\t\t}\n\t\t\telse if (ctx->encrypt)\n\t\t\t\t{\n\t\t\t\tif(!CRYPTO_ocb128_encrypt(&octx->ocb, buf, out, 16))\n\t\t\t\t\treturn -1;\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\tif(!CRYPTO_ocb128_decrypt(&octx->ocb, buf, out, 16))\n\t\t\t\t\treturn -1;\n\t\t\t\t}\n\t\t\twritten_len = 16;\n\t\t\t*buf_len = 0;\n\t\t\t}\n\t\ttrailing_len = len % 16;\n\t\tif(len != trailing_len)\n\t\t\t{\n\t\t\tif (out == NULL)\n\t\t\t\t{\n\t\t\t\tif(!CRYPTO_ocb128_aad(&octx->ocb, in, len-trailing_len))\n\t\t\t\t\treturn -1;\n\t\t\t\t}\n\t\t\telse if (ctx->encrypt)\n\t\t\t\t{\n\t\t\t\tif(!CRYPTO_ocb128_encrypt(&octx->ocb, in, out, len-trailing_len))\n\t\t\t\t\treturn -1;\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\tif(!CRYPTO_ocb128_decrypt(&octx->ocb, in, out, len-trailing_len))\n\t\t\t\t\treturn -1;\n\t\t\t\t}\n\t\t\twritten_len += len-trailing_len;\n\t\t\tin += len-trailing_len;\n\t\t\t}\n\t\tif(trailing_len)\n\t\t\t{\n\t\t\tmemcpy(buf, in, trailing_len);\n\t\t\t*buf_len = trailing_len;\n\t\t\t}\n\t\treturn written_len;\n\t\t}\n\telse\n\t\t{\n\t\tif(octx->data_buf_len)\n\t\t\t{\n\t\t\tif (ctx->encrypt)\n\t\t\t\t{\n\t\t\t\tif(!CRYPTO_ocb128_encrypt(&octx->ocb, octx->data_buf, out,\n\t\t\t\t\t\toctx->data_buf_len))\n\t\t\t\t\treturn -1;\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\tif(!CRYPTO_ocb128_decrypt(&octx->ocb, octx->data_buf, out,\n\t\t\t\t\t\toctx->data_buf_len))\n\t\t\t\t\treturn -1;\n\t\t\t\t}\n\t\t\twritten_len = octx->data_buf_len;\n\t\t\toctx->data_buf_len = 0;\n\t\t\t}\n\t\tif(octx->aad_buf_len)\n\t\t\t{\n\t\t\tif(!CRYPTO_ocb128_aad(&octx->ocb, octx->aad_buf, octx->aad_buf_len))\n\t\t\t\treturn -1;\n\t\t\toctx->aad_buf_len = 0;\n\t\t\t}\n\t\tif (!ctx->encrypt)\n\t\t\t{\n\t\t\tif (octx->taglen < 0)\n\t\t\t\treturn -1;\n\t\t\tif (CRYPTO_ocb128_finish(&octx->ocb,\n\t\t\t\t\toctx->tag, octx->taglen) != 0)\n\t\t\t\treturn -1;\n\t\t\toctx->iv_set = 0;\n\t\t\treturn written_len;\n\t\t\t}\n\t\tif(CRYPTO_ocb128_tag(&octx->ocb, octx->tag, 16) != 1)\n\t\t\treturn -1;\n\t\toctx->iv_set = 0;\n\t\treturn written_len;\n\t\t}\n\t}'] |
35,105 | 0 | https://github.com/openssl/openssl/blob/84c15db551ce1d167b901a3bde2b21880b084384/crypto/bf/bf_skey.c/#L78 | void BF_set_key(BF_KEY *key, int len, unsigned char *data)
{
int i;
BF_LONG *p,ri,in[2];
unsigned char *d,*end;
memcpy((char *)key,(char *)&bf_init,sizeof(BF_KEY));
p=key->P;
if (len > ((BF_ROUNDS+2)*4)) len=(BF_ROUNDS+2)*4;
d=data;
end= &(data[len]);
for (i=0; i<(BF_ROUNDS+2); i++)
{
ri= *(d++);
if (d >= end) d=data;
ri<<=8;
ri|= *(d++);
if (d >= end) d=data;
ri<<=8;
ri|= *(d++);
if (d >= end) d=data;
ri<<=8;
ri|= *(d++);
if (d >= end) d=data;
p[i]^=ri;
}
in[0]=0L;
in[1]=0L;
for (i=0; i<(BF_ROUNDS+2); i+=2)
{
BF_encrypt(in,key);
p[i ]=in[0];
p[i+1]=in[1];
}
p=key->S;
for (i=0; i<4*256; i+=2)
{
BF_encrypt(in,key);
p[i ]=in[0];
p[i+1]=in[1];
}
} | ['static int test(void)\n\t{\n\tunsigned char cbc_in[40],cbc_out[40],iv[8];\n\tint i,n,err=0;\n\tBF_KEY key;\n\tBF_LONG data[2];\n\tunsigned char out[8];\n\tBF_LONG len;\n#ifdef CHARSET_EBCDIC\n\tebcdic2ascii(cbc_data, cbc_data, strlen(cbc_data));\n#endif\n\tprintf("testing blowfish in raw ecb mode\\n");\n\tfor (n=0; n<2; n++)\n\t\t{\n#ifdef CHARSET_EBCDIC\n\t\tebcdic2ascii(bf_key[n], bf_key[n], strlen(bf_key[n]));\n#endif\n\t\tBF_set_key(&key,strlen(bf_key[n]),(unsigned char *)bf_key[n]);\n\t\tdata[0]=bf_plain[n][0];\n\t\tdata[1]=bf_plain[n][1];\n\t\tBF_encrypt(data,&key);\n\t\tif (memcmp(&(bf_cipher[n][0]),&(data[0]),8) != 0)\n\t\t\t{\n\t\t\tprintf("BF_encrypt error encrypting\\n");\n\t\t\tprintf("got :");\n\t\t\tfor (i=0; i<2; i++)\n\t\t\t\tprintf("%08lX ",(unsigned long)data[i]);\n\t\t\tprintf("\\n");\n\t\t\tprintf("expected:");\n\t\t\tfor (i=0; i<2; i++)\n\t\t\t\tprintf("%08lX ",(unsigned long)bf_cipher[n][i]);\n\t\t\terr=1;\n\t\t\tprintf("\\n");\n\t\t\t}\n\t\tBF_decrypt(&(data[0]),&key);\n\t\tif (memcmp(&(bf_plain[n][0]),&(data[0]),8) != 0)\n\t\t\t{\n\t\t\tprintf("BF_encrypt error decrypting\\n");\n\t\t\tprintf("got :");\n\t\t\tfor (i=0; i<2; i++)\n\t\t\t\tprintf("%08lX ",(unsigned long)data[i]);\n\t\t\tprintf("\\n");\n\t\t\tprintf("expected:");\n\t\t\tfor (i=0; i<2; i++)\n\t\t\t\tprintf("%08lX ",(unsigned long)bf_plain[n][i]);\n\t\t\tprintf("\\n");\n\t\t\terr=1;\n\t\t\t}\n\t\t}\n\tprintf("testing blowfish in ecb mode\\n");\n\tfor (n=0; n<NUM_TESTS; n++)\n\t\t{\n\t\tBF_set_key(&key,8,ecb_data[n]);\n\t\tBF_ecb_encrypt(&(plain_data[n][0]),out,&key,BF_ENCRYPT);\n\t\tif (memcmp(&(cipher_data[n][0]),out,8) != 0)\n\t\t\t{\n\t\t\tprintf("BF_ecb_encrypt blowfish error encrypting\\n");\n\t\t\tprintf("got :");\n\t\t\tfor (i=0; i<8; i++)\n\t\t\t\tprintf("%02X ",out[i]);\n\t\t\tprintf("\\n");\n\t\t\tprintf("expected:");\n\t\t\tfor (i=0; i<8; i++)\n\t\t\t\tprintf("%02X ",cipher_data[n][i]);\n\t\t\terr=1;\n\t\t\tprintf("\\n");\n\t\t\t}\n\t\tBF_ecb_encrypt(out,out,&key,BF_DECRYPT);\n\t\tif (memcmp(&(plain_data[n][0]),out,8) != 0)\n\t\t\t{\n\t\t\tprintf("BF_ecb_encrypt error decrypting\\n");\n\t\t\tprintf("got :");\n\t\t\tfor (i=0; i<8; i++)\n\t\t\t\tprintf("%02X ",out[i]);\n\t\t\tprintf("\\n");\n\t\t\tprintf("expected:");\n\t\t\tfor (i=0; i<8; i++)\n\t\t\t\tprintf("%02X ",plain_data[n][i]);\n\t\t\tprintf("\\n");\n\t\t\terr=1;\n\t\t\t}\n\t\t}\n\tprintf("testing blowfish set_key\\n");\n\tfor (n=1; n<KEY_TEST_NUM; n++)\n\t\t{\n\t\tBF_set_key(&key,n,key_test);\n\t\tBF_ecb_encrypt(key_data,out,&key,BF_ENCRYPT);\n\t\tif (memcmp(out,&(key_out[n-1][0]),8) != 0)\n\t\t\t{\n\t\t\tprintf("blowfish setkey error\\n");\n\t\t\terr=1;\n\t\t\t}\n\t\t}\n\tprintf("testing blowfish in cbc mode\\n");\n\tlen=strlen(cbc_data)+1;\n\tBF_set_key(&key,16,cbc_key);\n\tmemset(cbc_in,0,40);\n\tmemset(cbc_out,0,40);\n\tmemcpy(iv,cbc_iv,8);\n\tBF_cbc_encrypt((unsigned char *)cbc_data,cbc_out,len,\n\t\t&key,iv,BF_ENCRYPT);\n\tif (memcmp(cbc_out,cbc_ok,32) != 0)\n\t\t{\n\t\terr=1;\n\t\tprintf("BF_cbc_encrypt encrypt error\\n");\n\t\tfor (i=0; i<32; i++) printf("0x%02X,",cbc_out[i]);\n\t\t}\n\tmemcpy(iv,cbc_iv,8);\n\tBF_cbc_encrypt(cbc_out,cbc_in,len,\n\t\t&key,iv,BF_DECRYPT);\n\tif (memcmp(cbc_in,cbc_data,strlen(cbc_data)+1) != 0)\n\t\t{\n\t\tprintf("BF_cbc_encrypt decrypt error\\n");\n\t\terr=1;\n\t\t}\n\tprintf("testing blowfish in cfb64 mode\\n");\n\tBF_set_key(&key,16,cbc_key);\n\tmemset(cbc_in,0,40);\n\tmemset(cbc_out,0,40);\n\tmemcpy(iv,cbc_iv,8);\n\tn=0;\n\tBF_cfb64_encrypt((unsigned char *)cbc_data,cbc_out,(long)13,\n\t\t&key,iv,&n,BF_ENCRYPT);\n\tBF_cfb64_encrypt((unsigned char *)&(cbc_data[13]),&(cbc_out[13]),len-13,\n\t\t&key,iv,&n,BF_ENCRYPT);\n\tif (memcmp(cbc_out,cfb64_ok,(int)len) != 0)\n\t\t{\n\t\terr=1;\n\t\tprintf("BF_cfb64_encrypt encrypt error\\n");\n\t\tfor (i=0; i<(int)len; i++) printf("0x%02X,",cbc_out[i]);\n\t\t}\n\tn=0;\n\tmemcpy(iv,cbc_iv,8);\n\tBF_cfb64_encrypt(cbc_out,cbc_in,17,\n\t\t&key,iv,&n,BF_DECRYPT);\n\tBF_cfb64_encrypt(&(cbc_out[17]),&(cbc_in[17]),len-17,\n\t\t&key,iv,&n,BF_DECRYPT);\n\tif (memcmp(cbc_in,cbc_data,(int)len) != 0)\n\t\t{\n\t\tprintf("BF_cfb64_encrypt decrypt error\\n");\n\t\terr=1;\n\t\t}\n\tprintf("testing blowfish in ofb64\\n");\n\tBF_set_key(&key,16,cbc_key);\n\tmemset(cbc_in,0,40);\n\tmemset(cbc_out,0,40);\n\tmemcpy(iv,cbc_iv,8);\n\tn=0;\n\tBF_ofb64_encrypt((unsigned char *)cbc_data,cbc_out,(long)13,&key,iv,&n);\n\tBF_ofb64_encrypt((unsigned char *)&(cbc_data[13]),\n\t\t&(cbc_out[13]),len-13,&key,iv,&n);\n\tif (memcmp(cbc_out,ofb64_ok,(int)len) != 0)\n\t\t{\n\t\terr=1;\n\t\tprintf("BF_ofb64_encrypt encrypt error\\n");\n\t\tfor (i=0; i<(int)len; i++) printf("0x%02X,",cbc_out[i]);\n\t\t}\n\tn=0;\n\tmemcpy(iv,cbc_iv,8);\n\tBF_ofb64_encrypt(cbc_out,cbc_in,17,&key,iv,&n);\n\tBF_ofb64_encrypt(&(cbc_out[17]),&(cbc_in[17]),len-17,&key,iv,&n);\n\tif (memcmp(cbc_in,cbc_data,(int)len) != 0)\n\t\t{\n\t\tprintf("BF_ofb64_encrypt decrypt error\\n");\n\t\terr=1;\n\t\t}\n\treturn(err);\n\t}', 'void BF_set_key(BF_KEY *key, int len, unsigned char *data)\n\t{\n\tint i;\n\tBF_LONG *p,ri,in[2];\n\tunsigned char *d,*end;\n\tmemcpy((char *)key,(char *)&bf_init,sizeof(BF_KEY));\n\tp=key->P;\n\tif (len > ((BF_ROUNDS+2)*4)) len=(BF_ROUNDS+2)*4;\n\td=data;\n\tend= &(data[len]);\n\tfor (i=0; i<(BF_ROUNDS+2); i++)\n\t\t{\n\t\tri= *(d++);\n\t\tif (d >= end) d=data;\n\t\tri<<=8;\n\t\tri|= *(d++);\n\t\tif (d >= end) d=data;\n\t\tri<<=8;\n\t\tri|= *(d++);\n\t\tif (d >= end) d=data;\n\t\tri<<=8;\n\t\tri|= *(d++);\n\t\tif (d >= end) d=data;\n\t\tp[i]^=ri;\n\t\t}\n\tin[0]=0L;\n\tin[1]=0L;\n\tfor (i=0; i<(BF_ROUNDS+2); i+=2)\n\t\t{\n\t\tBF_encrypt(in,key);\n\t\tp[i ]=in[0];\n\t\tp[i+1]=in[1];\n\t\t}\n\tp=key->S;\n\tfor (i=0; i<4*256; i+=2)\n\t\t{\n\t\tBF_encrypt(in,key);\n\t\tp[i ]=in[0];\n\t\tp[i+1]=in[1];\n\t\t}\n\t}'] |
35,106 | 0 | https://github.com/openssl/openssl/blob/f9df0a7775f483c175cda5832360cccd1db6943a/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 BN_mod_exp_simple(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,\n const BIGNUM *m, BN_CTX *ctx)\n{\n int i, j, bits, ret = 0, wstart, wend, window, wvalue;\n int start = 1;\n BIGNUM *d;\n BIGNUM *val[TABLE_SIZE];\n if (BN_get_flags(p, BN_FLG_CONSTTIME) != 0) {\n BNerr(BN_F_BN_MOD_EXP_SIMPLE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);\n return 0;\n }\n bits = BN_num_bits(p);\n if (bits == 0) {\n if (BN_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 d = BN_CTX_get(ctx);\n val[0] = BN_CTX_get(ctx);\n if (val[0] == NULL)\n goto err;\n if (!BN_nnmod(val[0], a, m, ctx))\n goto err;\n if (BN_is_zero(val[0])) {\n BN_zero(r);\n ret = 1;\n goto err;\n }\n window = BN_window_bits_for_exponent_size(bits);\n if (window > 1) {\n if (!BN_mod_mul(d, val[0], val[0], m, ctx))\n goto err;\n j = 1 << (window - 1);\n for (i = 1; i < j; i++) {\n if (((val[i] = BN_CTX_get(ctx)) == NULL) ||\n !BN_mod_mul(val[i], val[i - 1], d, m, ctx))\n goto err;\n }\n }\n start = 1;\n wvalue = 0;\n wstart = bits - 1;\n wend = 0;\n if (!BN_one(r))\n goto err;\n for (;;) {\n if (BN_is_bit_set(p, wstart) == 0) {\n if (!start)\n if (!BN_mod_mul(r, r, r, m, ctx))\n goto err;\n if (wstart == 0)\n break;\n wstart--;\n continue;\n }\n j = wstart;\n wvalue = 1;\n wend = 0;\n for (i = 1; i < window; i++) {\n if (wstart - i < 0)\n break;\n if (BN_is_bit_set(p, wstart - i)) {\n wvalue <<= (i - wend);\n wvalue |= 1;\n wend = i;\n }\n }\n j = wend + 1;\n if (!start)\n for (i = 0; i < j; i++) {\n if (!BN_mod_mul(r, r, r, m, ctx))\n goto err;\n }\n if (!BN_mod_mul(r, r, val[wvalue >> 1], m, ctx))\n goto err;\n wstart -= wend + 1;\n wvalue = 0;\n start = 0;\n if (wstart < 0)\n break;\n }\n ret = 1;\n err:\n BN_CTX_end(ctx);\n bn_check_top(r);\n return (ret);\n}', 'BIGNUM *BN_CTX_get(BN_CTX *ctx)\n{\n BIGNUM *ret;\n CTXDBG_ENTRY("BN_CTX_get", ctx);\n if (ctx->err_stack || ctx->too_many)\n return NULL;\n if ((ret = BN_POOL_get(&ctx->pool, ctx->flags)) == NULL) {\n ctx->too_many = 1;\n BNerr(BN_F_BN_CTX_GET, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n return NULL;\n }\n BN_zero(ret);\n ctx->used++;\n CTXDBG_RET(ctx, ret);\n return ret;\n}', 'int BN_set_word(BIGNUM *a, BN_ULONG w)\n{\n bn_check_top(a);\n if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)\n return (0);\n a->neg = 0;\n a->d[0] = w;\n a->top = (w ? 1 : 0);\n bn_check_top(a);\n return 1;\n}', 'int BN_nnmod(BIGNUM *r, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx)\n{\n if (!(BN_mod(r, m, d, ctx)))\n return 0;\n if (!r->neg)\n return 1;\n return (d->neg ? BN_sub : BN_add) (r, r, d);\n}', 'int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,\n BN_CTX *ctx)\n{\n int norm_shift, i, loop;\n BIGNUM *tmp, wnum, *snum, *sdiv, *res;\n BN_ULONG *resp, *wnump;\n BN_ULONG d0, d1;\n int num_n, div_n;\n int no_branch = 0;\n if ((num->top > 0 && num->d[num->top - 1] == 0) ||\n (divisor->top > 0 && divisor->d[divisor->top - 1] == 0)) {\n BNerr(BN_F_BN_DIV, BN_R_NOT_INITIALIZED);\n return 0;\n }\n bn_check_top(num);\n bn_check_top(divisor);\n if ((BN_get_flags(num, BN_FLG_CONSTTIME) != 0)\n || (BN_get_flags(divisor, BN_FLG_CONSTTIME) != 0)) {\n no_branch = 1;\n }\n bn_check_top(dv);\n bn_check_top(rm);\n if (BN_is_zero(divisor)) {\n BNerr(BN_F_BN_DIV, BN_R_DIV_BY_ZERO);\n return (0);\n }\n if (!no_branch && BN_ucmp(num, divisor) < 0) {\n if (rm != NULL) {\n if (BN_copy(rm, num) == NULL)\n return (0);\n }\n if (dv != NULL)\n BN_zero(dv);\n return 1;\n }\n BN_CTX_start(ctx);\n res = (dv == NULL) ? BN_CTX_get(ctx) : dv;\n tmp = BN_CTX_get(ctx);\n snum = BN_CTX_get(ctx);\n sdiv = BN_CTX_get(ctx);\n if (sdiv == NULL)\n goto err;\n norm_shift = BN_BITS2 - ((BN_num_bits(divisor)) % BN_BITS2);\n if (!(BN_lshift(sdiv, divisor, norm_shift)))\n goto err;\n sdiv->neg = 0;\n norm_shift += BN_BITS2;\n if (!(BN_lshift(snum, num, norm_shift)))\n goto err;\n snum->neg = 0;\n if (no_branch) {\n if (snum->top <= sdiv->top + 1) {\n if (bn_wexpand(snum, sdiv->top + 2) == NULL)\n goto err;\n for (i = snum->top; i < sdiv->top + 2; i++)\n snum->d[i] = 0;\n snum->top = sdiv->top + 2;\n } else {\n if (bn_wexpand(snum, snum->top + 1) == NULL)\n goto err;\n snum->d[snum->top] = 0;\n snum->top++;\n }\n }\n div_n = sdiv->top;\n num_n = snum->top;\n loop = num_n - div_n;\n wnum.neg = 0;\n wnum.d = &(snum->d[loop]);\n wnum.top = div_n;\n wnum.dmax = snum->dmax - loop;\n d0 = sdiv->d[div_n - 1];\n d1 = (div_n == 1) ? 0 : sdiv->d[div_n - 2];\n wnump = &(snum->d[num_n - 1]);\n if (!bn_wexpand(res, (loop + 1)))\n goto err;\n res->neg = (num->neg ^ divisor->neg);\n res->top = loop - no_branch;\n resp = &(res->d[loop - 1]);\n if (!bn_wexpand(tmp, (div_n + 1)))\n goto err;\n if (!no_branch) {\n if (BN_ucmp(&wnum, sdiv) >= 0) {\n bn_clear_top2max(&wnum);\n bn_sub_words(wnum.d, wnum.d, sdiv->d, div_n);\n *resp = 1;\n } else\n res->top--;\n }\n resp++;\n if (res->top == 0)\n res->neg = 0;\n else\n resp--;\n for (i = 0; i < loop - 1; i++, wnump--) {\n BN_ULONG q, l0;\n# if defined(BN_DIV3W) && !defined(OPENSSL_NO_ASM)\n BN_ULONG bn_div_3_words(BN_ULONG *, BN_ULONG, BN_ULONG);\n q = bn_div_3_words(wnump, d1, d0);\n# else\n BN_ULONG n0, n1, rem = 0;\n n0 = wnump[0];\n n1 = wnump[-1];\n if (n0 == d0)\n q = BN_MASK2;\n else {\n# ifdef BN_LLONG\n BN_ULLONG t2;\n# if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)\n q = (BN_ULONG)(((((BN_ULLONG) n0) << BN_BITS2) | n1) / d0);\n# else\n q = bn_div_words(n0, n1, d0);\n# endif\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n t2 = (BN_ULLONG) d1 *q;\n for (;;) {\n if (t2 <= ((((BN_ULLONG) rem) << BN_BITS2) | wnump[-2]))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n t2 -= d1;\n }\n# else\n BN_ULONG t2l, t2h;\n q = bn_div_words(n0, n1, d0);\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n# if defined(BN_UMULT_LOHI)\n BN_UMULT_LOHI(t2l, t2h, d1, q);\n# elif defined(BN_UMULT_HIGH)\n t2l = d1 * q;\n t2h = BN_UMULT_HIGH(d1, q);\n# else\n {\n BN_ULONG ql, qh;\n t2l = LBITS(d1);\n t2h = HBITS(d1);\n ql = LBITS(q);\n qh = HBITS(q);\n mul64(t2l, t2h, ql, qh);\n }\n# endif\n for (;;) {\n if ((t2h < rem) || ((t2h == rem) && (t2l <= wnump[-2])))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n if (t2l < d1)\n t2h--;\n t2l -= d1;\n }\n# endif\n }\n# endif\n l0 = bn_mul_words(tmp->d, sdiv->d, div_n, q);\n tmp->d[div_n] = l0;\n wnum.d--;\n if (bn_sub_words(wnum.d, wnum.d, tmp->d, div_n + 1)) {\n q--;\n if (bn_add_words(wnum.d, wnum.d, sdiv->d, div_n))\n (*wnump)++;\n }\n resp--;\n *resp = q;\n }\n bn_correct_top(snum);\n if (rm != NULL) {\n int neg = num->neg;\n BN_rshift(rm, snum, norm_shift);\n if (!BN_is_zero(rm))\n rm->neg = neg;\n bn_check_top(rm);\n }\n if (no_branch)\n bn_correct_top(res);\n BN_CTX_end(ctx);\n return 1;\n err:\n bn_check_top(rm);\n BN_CTX_end(ctx);\n return (0);\n}', '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 if (BN_get_flags(b, BN_FLG_CONSTTIME) != 0)\n BN_set_flags(a, BN_FLG_CONSTTIME);\n a->top = b->top;\n a->neg = b->neg;\n bn_check_top(a);\n return a;\n}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n return (words <= a->dmax) ? a : bn_expand2(a, words);\n}', '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}'] |
35,107 | 0 | https://github.com/openssl/openssl/blob/02cba628daa7fea959c561531a8a984756bdf41c/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);
} | ['SSL *SSL_dup(SSL *s)\n{\n STACK_OF(X509_NAME) *sk;\n X509_NAME *xn;\n SSL *ret;\n int i;\n if (!SSL_in_init(s) || !SSL_in_before(s)) {\n CRYPTO_UP_REF(&s->references, &i, s->lock);\n return s;\n }\n if ((ret = SSL_new(SSL_get_SSL_CTX(s))) == NULL)\n return (NULL);\n if (s->session != NULL) {\n if (!SSL_copy_session_id(ret, s))\n goto err;\n } else {\n if (!SSL_set_ssl_method(ret, s->method))\n goto err;\n if (s->cert != NULL) {\n ssl_cert_free(ret->cert);\n ret->cert = ssl_cert_dup(s->cert);\n if (ret->cert == NULL)\n goto err;\n }\n if (!SSL_set_session_id_context(ret, s->sid_ctx,\n (int)s->sid_ctx_length))\n goto err;\n }\n if (!ssl_dane_dup(ret, s))\n goto err;\n ret->version = s->version;\n ret->options = s->options;\n ret->mode = s->mode;\n SSL_set_max_cert_list(ret, SSL_get_max_cert_list(s));\n SSL_set_read_ahead(ret, SSL_get_read_ahead(s));\n ret->msg_callback = s->msg_callback;\n ret->msg_callback_arg = s->msg_callback_arg;\n SSL_set_verify(ret, SSL_get_verify_mode(s), SSL_get_verify_callback(s));\n SSL_set_verify_depth(ret, SSL_get_verify_depth(s));\n ret->generate_session_id = s->generate_session_id;\n SSL_set_info_callback(ret, SSL_get_info_callback(s));\n if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_SSL, &ret->ex_data, &s->ex_data))\n goto err;\n if (s->rbio != NULL) {\n if (!BIO_dup_state(s->rbio, (char *)&ret->rbio))\n goto err;\n }\n if (s->wbio != NULL) {\n if (s->wbio != s->rbio) {\n if (!BIO_dup_state(s->wbio, (char *)&ret->wbio))\n goto err;\n } else {\n BIO_up_ref(ret->rbio);\n ret->wbio = ret->rbio;\n }\n }\n ret->server = s->server;\n if (s->handshake_func) {\n if (s->server)\n SSL_set_accept_state(ret);\n else\n SSL_set_connect_state(ret);\n }\n ret->shutdown = s->shutdown;\n ret->hit = s->hit;\n ret->default_passwd_callback = s->default_passwd_callback;\n ret->default_passwd_callback_userdata = s->default_passwd_callback_userdata;\n X509_VERIFY_PARAM_inherit(ret->param, s->param);\n if (s->cipher_list != NULL) {\n if ((ret->cipher_list = sk_SSL_CIPHER_dup(s->cipher_list)) == NULL)\n goto err;\n }\n if (s->cipher_list_by_id != NULL)\n if ((ret->cipher_list_by_id = sk_SSL_CIPHER_dup(s->cipher_list_by_id))\n == NULL)\n goto err;\n if (s->client_CA != NULL) {\n if ((sk = sk_X509_NAME_dup(s->client_CA)) == NULL)\n goto err;\n ret->client_CA = sk;\n for (i = 0; i < sk_X509_NAME_num(sk); i++) {\n xn = sk_X509_NAME_value(sk, i);\n if (sk_X509_NAME_set(sk, i, X509_NAME_dup(xn)) == NULL) {\n X509_NAME_free(xn);\n goto err;\n }\n }\n }\n return ret;\n err:\n SSL_free(ret);\n return NULL;\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->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->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 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 sk_X509_NAME_pop_free(s->client_CA, 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}'] |
35,108 | 0 | https://github.com/openssl/openssl/blob/8da94770f0a049497b1a52ee469cca1f4a13b1a7/test/bntest.c/#L445 | int test_sub(BIO *bp)
{
BIGNUM *a, *b, *c;
int i;
a = BN_new();
b = BN_new();
c = BN_new();
for (i = 0; i < num0 + num1; i++) {
if (i < num1) {
BN_bntest_rand(a, 512, 0, 0);
BN_copy(b, a);
if (BN_set_bit(a, i) == 0)
return (0);
BN_add_word(b, i);
} else {
BN_bntest_rand(b, 400 + i - num1, 0, 0);
a->neg = rand_neg();
b->neg = rand_neg();
}
BN_sub(c, a, b);
if (bp != NULL) {
if (!results) {
BN_print(bp, a);
BIO_puts(bp, " - ");
BN_print(bp, b);
BIO_puts(bp, " - ");
}
BN_print(bp, c);
BIO_puts(bp, "\n");
}
BN_add(c, c, b);
BN_sub(c, c, a);
if (!BN_is_zero(c)) {
fprintf(stderr, "Subtract test failed!\n");
return 0;
}
}
BN_free(a);
BN_free(b);
BN_free(c);
return (1);
} | ['int test_sub(BIO *bp)\n{\n BIGNUM *a, *b, *c;\n int i;\n a = BN_new();\n b = BN_new();\n c = BN_new();\n for (i = 0; i < num0 + num1; i++) {\n if (i < num1) {\n BN_bntest_rand(a, 512, 0, 0);\n BN_copy(b, a);\n if (BN_set_bit(a, i) == 0)\n return (0);\n BN_add_word(b, i);\n } else {\n BN_bntest_rand(b, 400 + i - num1, 0, 0);\n a->neg = rand_neg();\n b->neg = rand_neg();\n }\n BN_sub(c, a, b);\n if (bp != NULL) {\n if (!results) {\n BN_print(bp, a);\n BIO_puts(bp, " - ");\n BN_print(bp, b);\n BIO_puts(bp, " - ");\n }\n BN_print(bp, c);\n BIO_puts(bp, "\\n");\n }\n BN_add(c, c, b);\n BN_sub(c, c, a);\n if (!BN_is_zero(c)) {\n fprintf(stderr, "Subtract test failed!\\n");\n return 0;\n }\n }\n BN_free(a);\n BN_free(b);\n BN_free(c);\n return (1);\n}', 'BIGNUM *BN_new(void)\n{\n BIGNUM *ret;\n if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL) {\n BNerr(BN_F_BN_NEW, ERR_R_MALLOC_FAILURE);\n return (NULL);\n }\n ret->flags = BN_FLG_MALLOCED;\n bn_check_top(ret);\n return (ret);\n}', 'void *CRYPTO_zalloc(size_t num, const char *file, int line)\n{\n void *ret = CRYPTO_malloc(num, file, line);\n if (ret != NULL)\n memset(ret, 0, num);\n return ret;\n}', 'void *CRYPTO_malloc(size_t num, const char *file, int line)\n{\n void *ret = NULL;\n if (num <= 0)\n return NULL;\n allow_customize = 0;\n#ifdef CRYPTO_MDEBUG\n if (call_malloc_debug) {\n CRYPTO_mem_debug_malloc(NULL, num, 0, file, line);\n ret = malloc(num);\n CRYPTO_mem_debug_malloc(ret, num, 1, file, line);\n } else {\n ret = malloc(num);\n }\n#else\n (void)file;\n (void)line;\n ret = malloc(num);\n#endif\n#ifndef OPENSSL_CPUID_OBJ\n if (ret && (num > 2048)) {\n extern unsigned char cleanse_ctr;\n ((unsigned char *)ret)[0] = cleanse_ctr;\n }\n#endif\n return ret;\n}', 'void BN_free(BIGNUM *a)\n{\n if (a == NULL)\n return;\n bn_check_top(a);\n if (!BN_get_flags(a, BN_FLG_STATIC_DATA))\n bn_free_d(a);\n if (a->flags & BN_FLG_MALLOCED)\n OPENSSL_free(a);\n else {\n#if OPENSSL_API_COMPAT < 0x00908000L\n a->flags |= BN_FLG_FREE;\n#endif\n a->d = NULL;\n }\n}', 'int BN_get_flags(const BIGNUM *b, int n)\n{\n return b->flags & n;\n}'] |
35,109 | 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;
} | ['int RSA_X931_generate_key_ex(RSA *rsa, int bits, const BIGNUM *e,\n BN_GENCB *cb)\n{\n int ok = 0;\n BIGNUM *Xp = NULL, *Xq = NULL;\n BN_CTX *ctx = NULL;\n ctx = BN_CTX_new();\n if (ctx == NULL)\n goto error;\n BN_CTX_start(ctx);\n Xp = BN_CTX_get(ctx);\n Xq = BN_CTX_get(ctx);\n if (Xq == NULL)\n goto error;\n if (!BN_X931_generate_Xpq(Xp, Xq, bits, ctx))\n goto error;\n rsa->p = BN_new();\n rsa->q = BN_new();\n if (rsa->p == NULL || rsa->q == NULL)\n goto error;\n if (!BN_X931_generate_prime_ex(rsa->p, NULL, NULL, NULL, NULL, Xp,\n e, ctx, cb))\n goto error;\n if (!BN_X931_generate_prime_ex(rsa->q, NULL, NULL, NULL, NULL, Xq,\n e, ctx, cb))\n goto error;\n if (!RSA_X931_derive_ex(rsa, NULL, NULL, NULL, NULL,\n NULL, NULL, NULL, NULL, NULL, NULL, e, cb))\n goto error;\n ok = 1;\n error:\n if (ctx)\n BN_CTX_end(ctx);\n BN_CTX_free(ctx);\n if (ok)\n return 1;\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}', '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_X931_generate_Xpq(BIGNUM *Xp, BIGNUM *Xq, int nbits, BN_CTX *ctx)\n{\n BIGNUM *t;\n int i;\n if ((nbits < 1024) || (nbits & 0xff))\n return 0;\n nbits >>= 1;\n if (!BN_priv_rand(Xp, nbits, BN_RAND_TOP_TWO, BN_RAND_BOTTOM_ANY))\n goto err;\n BN_CTX_start(ctx);\n t = BN_CTX_get(ctx);\n if (t == NULL)\n goto err;\n for (i = 0; i < 1000; i++) {\n if (!BN_priv_rand(Xq, nbits, BN_RAND_TOP_TWO, BN_RAND_BOTTOM_ANY))\n goto err;\n BN_sub(t, Xp, Xq);\n if (BN_num_bits(t) > (nbits - 100))\n break;\n }\n BN_CTX_end(ctx);\n if (i < 1000)\n return 1;\n return 0;\n err:\n BN_CTX_end(ctx);\n return 0;\n}', 'int BN_priv_rand(BIGNUM *rnd, int bits, int top, int bottom)\n{\n return bnrand(PRIVATE, rnd, bits, top, bottom);\n}', 'static int bnrand(BNRAND_FLAG flag, BIGNUM *rnd, int bits, int top, int bottom)\n{\n unsigned char *buf = NULL;\n int b, ret = 0, bit, bytes, mask;\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 b = flag == NORMAL ? RAND_bytes(buf, bytes) : RAND_priv_bytes(buf, bytes);\n if (b <= 0)\n goto err;\n if (flag == TESTING) {\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}', 'BIGNUM *BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret)\n{\n unsigned int i, m;\n unsigned int n;\n BN_ULONG l;\n BIGNUM *bn = NULL;\n if (ret == NULL)\n ret = bn = BN_new();\n if (ret == NULL)\n return (NULL);\n bn_check_top(ret);\n for ( ; len > 0 && *s == 0; s++, len--)\n continue;\n n = len;\n if (n == 0) {\n ret->top = 0;\n return (ret);\n }\n i = ((n - 1) / BN_BYTES) + 1;\n m = ((n - 1) % (BN_BYTES));\n if (bn_wexpand(ret, (int)i) == NULL) {\n BN_free(bn);\n return NULL;\n }\n ret->top = i;\n ret->neg = 0;\n l = 0;\n while (n--) {\n l = (l << 8L) | *(s++);\n if (m-- == 0) {\n ret->d[--i] = l;\n l = 0;\n m = BN_BYTES - 1;\n }\n }\n bn_correct_top(ret);\n return (ret);\n}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n return (words <= a->dmax) ? a : bn_expand2(a, words);\n}', 'BIGNUM *bn_expand2(BIGNUM *b, int words)\n{\n 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}'] |
35,110 | 0 | https://github.com/libav/libav/blob/556f8a066cb33241bf29e85d7e24c9acf7ea9043/libavformat/matroskaenc.c/#L369 | static int mkv_add_cuepoint(mkv_cues *cues, int stream, int64_t ts, int64_t cluster_pos)
{
mkv_cuepoint *entries = cues->entries;
entries = av_realloc(entries, (cues->num_entries + 1) * sizeof(mkv_cuepoint));
if (entries == NULL)
return AVERROR(ENOMEM);
if (ts < 0)
return 0;
entries[cues->num_entries ].pts = ts;
entries[cues->num_entries ].tracknum = stream + 1;
entries[cues->num_entries++].cluster_pos = cluster_pos - cues->segment_offset;
cues->entries = entries;
return 0;
} | ['static int mkv_add_cuepoint(mkv_cues *cues, int stream, int64_t ts, int64_t cluster_pos)\n{\n mkv_cuepoint *entries = cues->entries;\n entries = av_realloc(entries, (cues->num_entries + 1) * sizeof(mkv_cuepoint));\n if (entries == NULL)\n return AVERROR(ENOMEM);\n if (ts < 0)\n return 0;\n entries[cues->num_entries ].pts = ts;\n entries[cues->num_entries ].tracknum = stream + 1;\n entries[cues->num_entries++].cluster_pos = cluster_pos - cues->segment_offset;\n cues->entries = entries;\n return 0;\n}', 'void *av_realloc(void *ptr, size_t 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}'] |
35,111 | 0 | https://github.com/libav/libav/blob/e4e30256f87f177decf59b59e923d05ef64147df/avconv.c/#L1460 | static void do_video_stats(AVFormatContext *os, OutputStream *ost,
int frame_size)
{
AVCodecContext *enc;
int frame_number;
double ti1, bitrate, avg_bitrate;
if (!vstats_file) {
vstats_file = fopen(vstats_filename, "w");
if (!vstats_file) {
perror("fopen");
exit_program(1);
}
}
enc = ost->st->codec;
if (enc->codec_type == AVMEDIA_TYPE_VIDEO) {
frame_number = ost->frame_number;
fprintf(vstats_file, "frame= %5d q= %2.1f ", frame_number, enc->coded_frame->quality / (float)FF_QP2LAMBDA);
if (enc->flags&CODEC_FLAG_PSNR)
fprintf(vstats_file, "PSNR= %6.2f ", psnr(enc->coded_frame->error[0] / (enc->width * enc->height * 255.0 * 255.0)));
fprintf(vstats_file,"f_size= %6d ", frame_size);
ti1 = ost->sync_opts * av_q2d(enc->time_base);
if (ti1 < 0.01)
ti1 = 0.01;
bitrate = (frame_size * 8) / av_q2d(enc->time_base) / 1000.0;
avg_bitrate = (double)(video_size * 8) / ti1 / 1000.0;
fprintf(vstats_file, "s_size= %8.0fkB time= %0.3f br= %7.1fkbits/s avg_br= %7.1fkbits/s ",
(double)video_size / 1024, ti1, bitrate, avg_bitrate);
fprintf(vstats_file, "type= %c\n", av_get_picture_type_char(enc->coded_frame->pict_type));
}
} | ['static void do_video_stats(AVFormatContext *os, OutputStream *ost,\n int frame_size)\n{\n AVCodecContext *enc;\n int frame_number;\n double ti1, bitrate, avg_bitrate;\n if (!vstats_file) {\n vstats_file = fopen(vstats_filename, "w");\n if (!vstats_file) {\n perror("fopen");\n exit_program(1);\n }\n }\n enc = ost->st->codec;\n if (enc->codec_type == AVMEDIA_TYPE_VIDEO) {\n frame_number = ost->frame_number;\n fprintf(vstats_file, "frame= %5d q= %2.1f ", frame_number, enc->coded_frame->quality / (float)FF_QP2LAMBDA);\n if (enc->flags&CODEC_FLAG_PSNR)\n fprintf(vstats_file, "PSNR= %6.2f ", psnr(enc->coded_frame->error[0] / (enc->width * enc->height * 255.0 * 255.0)));\n fprintf(vstats_file,"f_size= %6d ", frame_size);\n ti1 = ost->sync_opts * av_q2d(enc->time_base);\n if (ti1 < 0.01)\n ti1 = 0.01;\n bitrate = (frame_size * 8) / av_q2d(enc->time_base) / 1000.0;\n avg_bitrate = (double)(video_size * 8) / ti1 / 1000.0;\n fprintf(vstats_file, "s_size= %8.0fkB time= %0.3f br= %7.1fkbits/s avg_br= %7.1fkbits/s ",\n (double)video_size / 1024, ti1, bitrate, avg_bitrate);\n fprintf(vstats_file, "type= %c\\n", av_get_picture_type_char(enc->coded_frame->pict_type));\n }\n}'] |
35,112 | 0 | https://github.com/openssl/openssl/blob/ceb12d3074849e3e01e1b64b6512eb2ade325807/ssl/s3_clnt.c/#L956 | static int ssl3_get_server_certificate(SSL *s)
{
int al,i,ok,ret= -1;
unsigned long n,nc,llen,l;
X509 *x=NULL;
unsigned char *p,*d,*q;
STACK_OF(X509) *sk=NULL;
SESS_CERT *sc;
EVP_PKEY *pkey=NULL;
int need_cert = 1;
n=ssl3_get_message(s,
SSL3_ST_CR_CERT_A,
SSL3_ST_CR_CERT_B,
-1,
s->max_cert_list,
&ok);
if (!ok) return((int)n);
if (s->s3->tmp.message_type == SSL3_MT_SERVER_KEY_EXCHANGE)
{
s->s3->tmp.reuse_message=1;
return(1);
}
if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE)
{
al=SSL_AD_UNEXPECTED_MESSAGE;
SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_BAD_MESSAGE_TYPE);
goto f_err;
}
d=p=(unsigned char *)s->init_msg;
if ((sk=sk_X509_new_null()) == NULL)
{
SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,ERR_R_MALLOC_FAILURE);
goto err;
}
n2l3(p,llen);
if (llen+3 != n)
{
al=SSL_AD_DECODE_ERROR;
SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_LENGTH_MISMATCH);
goto f_err;
}
for (nc=0; nc<llen; )
{
n2l3(p,l);
if ((l+nc+3) > llen)
{
al=SSL_AD_DECODE_ERROR;
SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH);
goto f_err;
}
q=p;
x=d2i_X509(NULL,&q,l);
if (x == NULL)
{
al=SSL_AD_BAD_CERTIFICATE;
SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,ERR_R_ASN1_LIB);
goto f_err;
}
if (q != (p+l))
{
al=SSL_AD_DECODE_ERROR;
SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH);
goto f_err;
}
if (!sk_X509_push(sk,x))
{
SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,ERR_R_MALLOC_FAILURE);
goto err;
}
x=NULL;
nc+=l+3;
p=q;
}
i=ssl_verify_cert_chain(s,sk);
if ((s->verify_mode != SSL_VERIFY_NONE) && (!i)
#ifndef OPENSSL_NO_KRB5
&& (s->s3->tmp.new_cipher->algorithms & (SSL_MKEY_MASK|SSL_AUTH_MASK))
!= (SSL_aKRB5|SSL_kKRB5)
#endif
)
{
al=ssl_verify_alarm_type(s->verify_result);
SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_CERTIFICATE_VERIFY_FAILED);
goto f_err;
}
ERR_clear_error();
sc=ssl_sess_cert_new();
if (sc == NULL) goto err;
if (s->session->sess_cert) ssl_sess_cert_free(s->session->sess_cert);
s->session->sess_cert=sc;
sc->cert_chain=sk;
x=sk_X509_value(sk,0);
sk=NULL;
pkey=X509_get_pubkey(x);
need_cert = ((s->s3->tmp.new_cipher->algorithms
& (SSL_MKEY_MASK|SSL_AUTH_MASK))
== (SSL_aKRB5|SSL_kKRB5))? 0: 1;
#ifdef KSSL_DEBUG
printf("pkey,x = %p, %p\n", pkey,x);
printf("ssl_cert_type(x,pkey) = %d\n", ssl_cert_type(x,pkey));
printf("cipher, alg, nc = %s, %lx, %d\n", s->s3->tmp.new_cipher->name,
s->s3->tmp.new_cipher->algorithms, need_cert);
#endif
if (need_cert && ((pkey == NULL) || EVP_PKEY_missing_parameters(pkey)))
{
x=NULL;
al=SSL3_AL_FATAL;
SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS);
goto f_err;
}
i=ssl_cert_type(x,pkey);
if (need_cert && i < 0)
{
x=NULL;
al=SSL3_AL_FATAL;
SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
SSL_R_UNKNOWN_CERTIFICATE_TYPE);
goto f_err;
}
if (need_cert)
{
sc->peer_cert_type=i;
CRYPTO_add(&x->references,1,CRYPTO_LOCK_X509);
if (sc->peer_pkeys[i].x509 != NULL)
X509_free(sc->peer_pkeys[i].x509);
sc->peer_pkeys[i].x509=x;
sc->peer_key= &(sc->peer_pkeys[i]);
if (s->session->peer != NULL)
X509_free(s->session->peer);
CRYPTO_add(&x->references,1,CRYPTO_LOCK_X509);
s->session->peer=x;
}
else
{
sc->peer_cert_type=i;
sc->peer_key= NULL;
if (s->session->peer != NULL)
X509_free(s->session->peer);
s->session->peer=NULL;
}
s->session->verify_result = s->verify_result;
x=NULL;
ret=1;
if (0)
{
f_err:
ssl3_send_alert(s,SSL3_AL_FATAL,al);
}
err:
EVP_PKEY_free(pkey);
X509_free(x);
sk_X509_pop_free(sk,X509_free);
return(ret);
} | ['static int ssl3_get_server_certificate(SSL *s)\n\t{\n\tint al,i,ok,ret= -1;\n\tunsigned long n,nc,llen,l;\n\tX509 *x=NULL;\n\tunsigned char *p,*d,*q;\n\tSTACK_OF(X509) *sk=NULL;\n\tSESS_CERT *sc;\n\tEVP_PKEY *pkey=NULL;\n int need_cert = 1;\n\tn=ssl3_get_message(s,\n\t\tSSL3_ST_CR_CERT_A,\n\t\tSSL3_ST_CR_CERT_B,\n\t\t-1,\n\t\ts->max_cert_list,\n\t\t&ok);\n\tif (!ok) return((int)n);\n\tif (s->s3->tmp.message_type == SSL3_MT_SERVER_KEY_EXCHANGE)\n\t\t{\n\t\ts->s3->tmp.reuse_message=1;\n\t\treturn(1);\n\t\t}\n\tif (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE)\n\t\t{\n\t\tal=SSL_AD_UNEXPECTED_MESSAGE;\n\t\tSSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_BAD_MESSAGE_TYPE);\n\t\tgoto f_err;\n\t\t}\n\td=p=(unsigned char *)s->init_msg;\n\tif ((sk=sk_X509_new_null()) == NULL)\n\t\t{\n\t\tSSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,ERR_R_MALLOC_FAILURE);\n\t\tgoto err;\n\t\t}\n\tn2l3(p,llen);\n\tif (llen+3 != n)\n\t\t{\n\t\tal=SSL_AD_DECODE_ERROR;\n\t\tSSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_LENGTH_MISMATCH);\n\t\tgoto f_err;\n\t\t}\n\tfor (nc=0; nc<llen; )\n\t\t{\n\t\tn2l3(p,l);\n\t\tif ((l+nc+3) > llen)\n\t\t\t{\n\t\t\tal=SSL_AD_DECODE_ERROR;\n\t\t\tSSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH);\n\t\t\tgoto f_err;\n\t\t\t}\n\t\tq=p;\n\t\tx=d2i_X509(NULL,&q,l);\n\t\tif (x == NULL)\n\t\t\t{\n\t\t\tal=SSL_AD_BAD_CERTIFICATE;\n\t\t\tSSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,ERR_R_ASN1_LIB);\n\t\t\tgoto f_err;\n\t\t\t}\n\t\tif (q != (p+l))\n\t\t\t{\n\t\t\tal=SSL_AD_DECODE_ERROR;\n\t\t\tSSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH);\n\t\t\tgoto f_err;\n\t\t\t}\n\t\tif (!sk_X509_push(sk,x))\n\t\t\t{\n\t\t\tSSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,ERR_R_MALLOC_FAILURE);\n\t\t\tgoto err;\n\t\t\t}\n\t\tx=NULL;\n\t\tnc+=l+3;\n\t\tp=q;\n\t\t}\n\ti=ssl_verify_cert_chain(s,sk);\n\tif ((s->verify_mode != SSL_VERIFY_NONE) && (!i)\n#ifndef OPENSSL_NO_KRB5\n && (s->s3->tmp.new_cipher->algorithms & (SSL_MKEY_MASK|SSL_AUTH_MASK))\n != (SSL_aKRB5|SSL_kKRB5)\n#endif\n )\n\t\t{\n\t\tal=ssl_verify_alarm_type(s->verify_result);\n\t\tSSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_CERTIFICATE_VERIFY_FAILED);\n\t\tgoto f_err;\n\t\t}\n\tERR_clear_error();\n\tsc=ssl_sess_cert_new();\n\tif (sc == NULL) goto err;\n\tif (s->session->sess_cert) ssl_sess_cert_free(s->session->sess_cert);\n\ts->session->sess_cert=sc;\n\tsc->cert_chain=sk;\n\tx=sk_X509_value(sk,0);\n\tsk=NULL;\n\tpkey=X509_get_pubkey(x);\n need_cert =\t((s->s3->tmp.new_cipher->algorithms\n\t\t\t& (SSL_MKEY_MASK|SSL_AUTH_MASK))\n == (SSL_aKRB5|SSL_kKRB5))? 0: 1;\n#ifdef KSSL_DEBUG\n\tprintf("pkey,x = %p, %p\\n", pkey,x);\n\tprintf("ssl_cert_type(x,pkey) = %d\\n", ssl_cert_type(x,pkey));\n\tprintf("cipher, alg, nc = %s, %lx, %d\\n", s->s3->tmp.new_cipher->name,\n s->s3->tmp.new_cipher->algorithms, need_cert);\n#endif\n\tif (need_cert && ((pkey == NULL) || EVP_PKEY_missing_parameters(pkey)))\n\t\t{\n\t\tx=NULL;\n\t\tal=SSL3_AL_FATAL;\n\t\tSSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,\n\t\t\tSSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS);\n\t\tgoto f_err;\n\t\t}\n\ti=ssl_cert_type(x,pkey);\n\tif (need_cert && i < 0)\n\t\t{\n\t\tx=NULL;\n\t\tal=SSL3_AL_FATAL;\n\t\tSSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,\n\t\t\tSSL_R_UNKNOWN_CERTIFICATE_TYPE);\n\t\tgoto f_err;\n\t\t}\n if (need_cert)\n {\n sc->peer_cert_type=i;\n CRYPTO_add(&x->references,1,CRYPTO_LOCK_X509);\n if (sc->peer_pkeys[i].x509 != NULL)\n X509_free(sc->peer_pkeys[i].x509);\n sc->peer_pkeys[i].x509=x;\n sc->peer_key= &(sc->peer_pkeys[i]);\n if (s->session->peer != NULL)\n X509_free(s->session->peer);\n CRYPTO_add(&x->references,1,CRYPTO_LOCK_X509);\n s->session->peer=x;\n }\n else\n {\n sc->peer_cert_type=i;\n sc->peer_key= NULL;\n if (s->session->peer != NULL)\n X509_free(s->session->peer);\n s->session->peer=NULL;\n }\n\ts->session->verify_result = s->verify_result;\n\tx=NULL;\n\tret=1;\n\tif (0)\n\t\t{\nf_err:\n\t\tssl3_send_alert(s,SSL3_AL_FATAL,al);\n\t\t}\nerr:\n\tEVP_PKEY_free(pkey);\n\tX509_free(x);\n\tsk_X509_pop_free(sk,X509_free);\n\treturn(ret);\n\t}', 'STACK *sk_new_null(void)\n\t{\n\treturn sk_new((int (*)(const char * const *, const char * const *))0);\n\t}', 'STACK *sk_new(int (*c)(const char * const *, const char * const *))\n\t{\n\tSTACK *ret;\n\tint i;\n\tif ((ret=(STACK *)OPENSSL_malloc(sizeof(STACK))) == NULL)\n\t\tgoto err;\n\tif ((ret->data=(char **)OPENSSL_malloc(sizeof(char *)*MIN_NODES)) == NULL)\n\t\tgoto err;\n\tfor (i=0; i<MIN_NODES; i++)\n\t\tret->data[i]=NULL;\n\tret->comp=c;\n\tret->num_alloc=MIN_NODES;\n\tret->num=0;\n\tret->sorted=0;\n\treturn(ret);\nerr:\n\tif(ret)\n\t\tOPENSSL_free(ret);\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\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_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}'] |
35,113 | 0 | https://github.com/openssl/openssl/blob/9c46f4b9cd4912b61cb546c48b678488d7f26ed6/crypto/bn/bn_ctx.c/#L353 | static unsigned int BN_STACK_pop(BN_STACK *st)
{
return st->indexes[--(st->depth)];
} | ['static int dsa_do_verify(const unsigned char *dgst, int dgst_len,\n DSA_SIG *sig, DSA *dsa)\n{\n BN_CTX *ctx;\n BIGNUM *u1, *u2, *t1;\n BN_MONT_CTX *mont = NULL;\n int ret = -1, i;\n if (!dsa->p || !dsa->q || !dsa->g) {\n DSAerr(DSA_F_DSA_DO_VERIFY, DSA_R_MISSING_PARAMETERS);\n return -1;\n }\n i = BN_num_bits(dsa->q);\n if (i != 160 && i != 224 && i != 256) {\n DSAerr(DSA_F_DSA_DO_VERIFY, DSA_R_BAD_Q_VALUE);\n return -1;\n }\n if (BN_num_bits(dsa->p) > OPENSSL_DSA_MAX_MODULUS_BITS) {\n DSAerr(DSA_F_DSA_DO_VERIFY, DSA_R_MODULUS_TOO_LARGE);\n return -1;\n }\n u1 = BN_new();\n u2 = BN_new();\n t1 = BN_new();\n ctx = BN_CTX_new();\n if (!u1 || !u2 || !t1 || !ctx)\n goto err;\n if (BN_is_zero(sig->r) || BN_is_negative(sig->r) ||\n BN_ucmp(sig->r, dsa->q) >= 0) {\n ret = 0;\n goto err;\n }\n if (BN_is_zero(sig->s) || BN_is_negative(sig->s) ||\n BN_ucmp(sig->s, dsa->q) >= 0) {\n ret = 0;\n goto err;\n }\n if ((BN_mod_inverse(u2, sig->s, dsa->q, ctx)) == NULL)\n goto err;\n if (dgst_len > (i >> 3))\n dgst_len = (i >> 3);\n if (BN_bin2bn(dgst, dgst_len, u1) == NULL)\n goto err;\n if (!BN_mod_mul(u1, u1, u2, dsa->q, ctx))\n goto err;\n if (!BN_mod_mul(u2, sig->r, u2, dsa->q, ctx))\n goto err;\n if (dsa->flags & DSA_FLAG_CACHE_MONT_P) {\n mont = BN_MONT_CTX_set_locked(&dsa->method_mont_p,\n CRYPTO_LOCK_DSA, dsa->p, ctx);\n if (!mont)\n goto err;\n }\n DSA_MOD_EXP(goto err, dsa, t1, dsa->g, u1, dsa->pub_key, u2, dsa->p, ctx,\n mont);\n if (!BN_mod(u1, t1, dsa->q, ctx))\n goto err;\n ret = (BN_ucmp(u1, sig->r) == 0);\n err:\n if (ret < 0)\n DSAerr(DSA_F_DSA_DO_VERIFY, ERR_R_BN_LIB);\n if (ctx != NULL)\n BN_CTX_free(ctx);\n if (u1)\n BN_free(u1);\n if (u2)\n BN_free(u2);\n if (t1)\n BN_free(t1);\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 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 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}', '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 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}'] |
35,114 | 0 | https://github.com/openssl/openssl/blob/8da94770f0a049497b1a52ee469cca1f4a13b1a7/crypto/bn/bn_lib.c/#L352 | 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_mod_exp2_mont(BIGNUM *rr, const BIGNUM *a1, const BIGNUM *p1,\n const BIGNUM *a2, const BIGNUM *p2, const BIGNUM *m,\n BN_CTX *ctx, BN_MONT_CTX *in_mont)\n{\n int i, j, bits, b, bits1, bits2, ret =\n 0, wpos1, wpos2, window1, window2, wvalue1, wvalue2;\n int r_is_one = 1;\n BIGNUM *d, *r;\n const BIGNUM *a_mod_m;\n BIGNUM *val1[TABLE_SIZE], *val2[TABLE_SIZE];\n BN_MONT_CTX *mont = NULL;\n bn_check_top(a1);\n bn_check_top(p1);\n bn_check_top(a2);\n bn_check_top(p2);\n bn_check_top(m);\n if (!(m->d[0] & 1)) {\n BNerr(BN_F_BN_MOD_EXP2_MONT, BN_R_CALLED_WITH_EVEN_MODULUS);\n return (0);\n }\n bits1 = BN_num_bits(p1);\n bits2 = BN_num_bits(p2);\n if ((bits1 == 0) && (bits2 == 0)) {\n ret = BN_one(rr);\n return ret;\n }\n bits = (bits1 > bits2) ? bits1 : bits2;\n BN_CTX_start(ctx);\n d = BN_CTX_get(ctx);\n r = BN_CTX_get(ctx);\n val1[0] = BN_CTX_get(ctx);\n val2[0] = BN_CTX_get(ctx);\n if (!d || !r || !val1[0] || !val2[0])\n goto err;\n if (in_mont != NULL)\n mont = in_mont;\n else {\n if ((mont = BN_MONT_CTX_new()) == NULL)\n goto err;\n if (!BN_MONT_CTX_set(mont, m, ctx))\n goto err;\n }\n window1 = BN_window_bits_for_exponent_size(bits1);\n window2 = BN_window_bits_for_exponent_size(bits2);\n if (a1->neg || BN_ucmp(a1, m) >= 0) {\n if (!BN_mod(val1[0], a1, m, ctx))\n goto err;\n a_mod_m = val1[0];\n } else\n a_mod_m = a1;\n if (BN_is_zero(a_mod_m)) {\n BN_zero(rr);\n ret = 1;\n goto err;\n }\n if (!BN_to_montgomery(val1[0], a_mod_m, mont, ctx))\n goto err;\n if (window1 > 1) {\n if (!BN_mod_mul_montgomery(d, val1[0], val1[0], mont, ctx))\n goto err;\n j = 1 << (window1 - 1);\n for (i = 1; i < j; i++) {\n if (((val1[i] = BN_CTX_get(ctx)) == NULL) ||\n !BN_mod_mul_montgomery(val1[i], val1[i - 1], d, mont, ctx))\n goto err;\n }\n }\n if (a2->neg || BN_ucmp(a2, m) >= 0) {\n if (!BN_mod(val2[0], a2, m, ctx))\n goto err;\n a_mod_m = val2[0];\n } else\n a_mod_m = a2;\n if (BN_is_zero(a_mod_m)) {\n BN_zero(rr);\n ret = 1;\n goto err;\n }\n if (!BN_to_montgomery(val2[0], a_mod_m, mont, ctx))\n goto err;\n if (window2 > 1) {\n if (!BN_mod_mul_montgomery(d, val2[0], val2[0], mont, ctx))\n goto err;\n j = 1 << (window2 - 1);\n for (i = 1; i < j; i++) {\n if (((val2[i] = BN_CTX_get(ctx)) == NULL) ||\n !BN_mod_mul_montgomery(val2[i], val2[i - 1], d, mont, ctx))\n goto err;\n }\n }\n r_is_one = 1;\n wvalue1 = 0;\n wvalue2 = 0;\n wpos1 = 0;\n wpos2 = 0;\n if (!BN_to_montgomery(r, BN_value_one(), mont, ctx))\n goto err;\n for (b = bits - 1; b >= 0; b--) {\n if (!r_is_one) {\n if (!BN_mod_mul_montgomery(r, r, r, mont, ctx))\n goto err;\n }\n if (!wvalue1)\n if (BN_is_bit_set(p1, b)) {\n i = b - window1 + 1;\n while (!BN_is_bit_set(p1, i))\n i++;\n wpos1 = i;\n wvalue1 = 1;\n for (i = b - 1; i >= wpos1; i--) {\n wvalue1 <<= 1;\n if (BN_is_bit_set(p1, i))\n wvalue1++;\n }\n }\n if (!wvalue2)\n if (BN_is_bit_set(p2, b)) {\n i = b - window2 + 1;\n while (!BN_is_bit_set(p2, i))\n i++;\n wpos2 = i;\n wvalue2 = 1;\n for (i = b - 1; i >= wpos2; i--) {\n wvalue2 <<= 1;\n if (BN_is_bit_set(p2, i))\n wvalue2++;\n }\n }\n if (wvalue1 && b == wpos1) {\n if (!BN_mod_mul_montgomery(r, r, val1[wvalue1 >> 1], mont, ctx))\n goto err;\n wvalue1 = 0;\n r_is_one = 0;\n }\n if (wvalue2 && b == wpos2) {\n if (!BN_mod_mul_montgomery(r, r, val2[wvalue2 >> 1], mont, ctx))\n goto err;\n wvalue2 = 0;\n r_is_one = 0;\n }\n }\n if (!BN_from_montgomery(rr, r, mont, ctx))\n goto err;\n ret = 1;\n err:\n if (in_mont == NULL)\n BN_MONT_CTX_free(mont);\n BN_CTX_end(ctx);\n bn_check_top(rr);\n return (ret);\n}', 'BIGNUM *BN_CTX_get(BN_CTX *ctx)\n{\n BIGNUM *ret;\n CTXDBG_ENTRY("BN_CTX_get", ctx);\n if (ctx->err_stack || ctx->too_many)\n return NULL;\n if ((ret = BN_POOL_get(&ctx->pool, ctx->flags)) == NULL) {\n ctx->too_many = 1;\n BNerr(BN_F_BN_CTX_GET, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n return NULL;\n }\n BN_zero(ret);\n ctx->used++;\n CTXDBG_RET(ctx, ret);\n return ret;\n}', 'int BN_set_word(BIGNUM *a, BN_ULONG w)\n{\n bn_check_top(a);\n if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)\n return (0);\n a->neg = 0;\n a->d[0] = w;\n a->top = (w ? 1 : 0);\n bn_check_top(a);\n return (1);\n}', 'int BN_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_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}', '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}'] |
35,115 | 0 | https://github.com/openssl/openssl/blob/f9df0a7775f483c175cda5832360cccd1db6943a/crypto/cms/cms_dd.c/#L95 | int cms_DigestedData_do_final(CMS_ContentInfo *cms, BIO *chain, int verify)
{
EVP_MD_CTX *mctx = EVP_MD_CTX_new();
unsigned char md[EVP_MAX_MD_SIZE];
unsigned int mdlen;
int r = 0;
CMS_DigestedData *dd;
if (mctx == NULL) {
CMSerr(CMS_F_CMS_DIGESTEDDATA_DO_FINAL, ERR_R_MALLOC_FAILURE);
goto err;
}
dd = cms->d.digestedData;
if (!cms_DigestAlgorithm_find_ctx(mctx, chain, dd->digestAlgorithm))
goto err;
if (EVP_DigestFinal_ex(mctx, md, &mdlen) <= 0)
goto err;
if (verify) {
if (mdlen != (unsigned int)dd->digest->length) {
CMSerr(CMS_F_CMS_DIGESTEDDATA_DO_FINAL,
CMS_R_MESSAGEDIGEST_WRONG_LENGTH);
goto err;
}
if (memcmp(md, dd->digest->data, mdlen))
CMSerr(CMS_F_CMS_DIGESTEDDATA_DO_FINAL,
CMS_R_VERIFICATION_FAILURE);
else
r = 1;
} else {
if (!ASN1_STRING_set(dd->digest, md, mdlen))
goto err;
r = 1;
}
err:
EVP_MD_CTX_free(mctx);
return r;
} | ['int cms_DigestedData_do_final(CMS_ContentInfo *cms, BIO *chain, int verify)\n{\n EVP_MD_CTX *mctx = EVP_MD_CTX_new();\n unsigned char md[EVP_MAX_MD_SIZE];\n unsigned int mdlen;\n int r = 0;\n CMS_DigestedData *dd;\n if (mctx == NULL) {\n CMSerr(CMS_F_CMS_DIGESTEDDATA_DO_FINAL, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n dd = cms->d.digestedData;\n if (!cms_DigestAlgorithm_find_ctx(mctx, chain, dd->digestAlgorithm))\n goto err;\n if (EVP_DigestFinal_ex(mctx, md, &mdlen) <= 0)\n goto err;\n if (verify) {\n if (mdlen != (unsigned int)dd->digest->length) {\n CMSerr(CMS_F_CMS_DIGESTEDDATA_DO_FINAL,\n CMS_R_MESSAGEDIGEST_WRONG_LENGTH);\n goto err;\n }\n if (memcmp(md, dd->digest->data, mdlen))\n CMSerr(CMS_F_CMS_DIGESTEDDATA_DO_FINAL,\n CMS_R_VERIFICATION_FAILURE);\n else\n r = 1;\n } else {\n if (!ASN1_STRING_set(dd->digest, md, mdlen))\n goto err;\n r = 1;\n }\n err:\n EVP_MD_CTX_free(mctx);\n return r;\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 cms_DigestAlgorithm_find_ctx(EVP_MD_CTX *mctx, BIO *chain,\n X509_ALGOR *mdalg)\n{\n int nid;\n const ASN1_OBJECT *mdoid;\n X509_ALGOR_get0(&mdoid, NULL, NULL, mdalg);\n nid = OBJ_obj2nid(mdoid);\n for (;;) {\n EVP_MD_CTX *mtmp;\n chain = BIO_find_type(chain, BIO_TYPE_MD);\n if (chain == NULL) {\n CMSerr(CMS_F_CMS_DIGESTALGORITHM_FIND_CTX,\n CMS_R_NO_MATCHING_DIGEST);\n return 0;\n }\n BIO_get_md_ctx(chain, &mtmp);\n if (EVP_MD_CTX_type(mtmp) == nid\n || EVP_MD_pkey_type(EVP_MD_CTX_md(mtmp)) == nid)\n return EVP_MD_CTX_copy_ex(mctx, mtmp);\n chain = BIO_next(chain);\n }\n}', 'void X509_ALGOR_get0(const ASN1_OBJECT **paobj, int *pptype,\n const void **ppval, const X509_ALGOR *algor)\n{\n if (paobj)\n *paobj = algor->algorithm;\n if (pptype) {\n if (algor->parameter == NULL) {\n *pptype = V_ASN1_UNDEF;\n return;\n } else\n *pptype = algor->parameter->type;\n if (ppval)\n *ppval = algor->parameter->value.ptr;\n }\n}', 'int OBJ_obj2nid(const ASN1_OBJECT *a)\n{\n const unsigned int *op;\n ADDED_OBJ ad, *adp;\n if (a == NULL)\n return NID_undef;\n if (a->nid != 0)\n return a->nid;\n if (a->length == 0)\n return NID_undef;\n if (added != NULL) {\n ad.type = ADDED_DATA;\n ad.obj = (ASN1_OBJECT *)a;\n adp = lh_ADDED_OBJ_retrieve(added, &ad);\n if (adp != NULL)\n return adp->obj->nid;\n }\n op = OBJ_bsearch_obj(&a, obj_objs, NUM_OBJ);\n if (op == NULL)\n return NID_undef;\n return nid_objs[*op].nid;\n}', 'DEFINE_LHASH_OF(ADDED_OBJ)', 'void *OPENSSL_LH_retrieve(OPENSSL_LHASH *lh, const void *data)\n{\n unsigned long hash;\n OPENSSL_LH_NODE **rn;\n void *ret;\n lh->error = 0;\n rn = getrn(lh, data, &hash);\n if (*rn == NULL) {\n lh->num_retrieve_miss++;\n return NULL;\n } else {\n ret = (*rn)->data;\n lh->num_retrieve++;\n }\n return ret;\n}', 'void EVP_MD_CTX_free(EVP_MD_CTX *ctx)\n{\n EVP_MD_CTX_reset(ctx);\n OPENSSL_free(ctx);\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}'] |
35,116 | 0 | https://github.com/openssl/openssl/blob/e3713c365c2657236439fea00822a43aa396d112/ssl/ssl_lib.c/#L4161 | EVP_MD_CTX *ssl_replace_hash(EVP_MD_CTX **hash, const EVP_MD *md)
{
ssl_clear_hash_ctx(hash);
*hash = EVP_MD_CTX_new();
if (*hash == NULL || (md && EVP_DigestInit_ex(*hash, md, NULL) <= 0)) {
EVP_MD_CTX_free(*hash);
*hash = NULL;
return NULL;
}
return *hash;
} | ['EVP_MD_CTX *ssl_replace_hash(EVP_MD_CTX **hash, const EVP_MD *md)\n{\n ssl_clear_hash_ctx(hash);\n *hash = EVP_MD_CTX_new();\n if (*hash == NULL || (md && EVP_DigestInit_ex(*hash, md, NULL) <= 0)) {\n EVP_MD_CTX_free(*hash);\n *hash = NULL;\n return NULL;\n }\n return *hash;\n}', 'void ssl_clear_hash_ctx(EVP_MD_CTX **hash)\n{\n EVP_MD_CTX_free(*hash);\n *hash = NULL;\n}', 'void EVP_MD_CTX_free(EVP_MD_CTX *ctx)\n{\n EVP_MD_CTX_reset(ctx);\n OPENSSL_free(ctx);\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}', '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 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}'] |
35,117 | 0 | https://github.com/apache/httpd/blob/8b2ec33ac5d314be345814db08e194ffeda6beb0/server/util_expr_eval.c/#L920 | AP_DECLARE(const char *) ap_expr_str_exec_re(request_rec *r,
const ap_expr_info_t *info,
apr_size_t nmatch,
ap_regmatch_t *pmatch,
const char **source,
const char **err)
{
ap_expr_eval_ctx_t ctx;
int dont_vary, rc;
const char *tmp_source = NULL, *vary_this = NULL;
ap_regmatch_t tmp_pmatch[AP_MAX_REG_MATCH];
const char *result;
AP_DEBUG_ASSERT(info->flags & AP_EXPR_FLAG_STRING_RESULT);
if (info->root_node->node_op == op_String) {
*err = NULL;
return (const char *)info->root_node->node_arg1;
}
dont_vary = (info->flags & AP_EXPR_FLAG_DONT_VARY);
ctx.r = r;
ctx.c = r->connection;
ctx.s = r->server;
ctx.p = r->pool;
ctx.err = err;
ctx.info = info;
ctx.re_nmatch = nmatch;
ctx.re_pmatch = pmatch;
ctx.re_source = source;
ctx.vary_this = dont_vary ? NULL : &vary_this;
ctx.data = NULL;
ctx.result_string = &result;
if (!pmatch) {
ctx.re_nmatch = AP_MAX_REG_MATCH;
ctx.re_pmatch = tmp_pmatch;
ctx.re_source = &tmp_source;
}
rc = ap_expr_exec_ctx(&ctx);
if (rc > 0)
return result;
else if (rc < 0)
return NULL;
else
ap_assert(0);
return NULL;
} | ['AP_DECLARE(const char *) ap_expr_str_exec_re(request_rec *r,\n const ap_expr_info_t *info,\n apr_size_t nmatch,\n ap_regmatch_t *pmatch,\n const char **source,\n const char **err)\n{\n ap_expr_eval_ctx_t ctx;\n int dont_vary, rc;\n const char *tmp_source = NULL, *vary_this = NULL;\n ap_regmatch_t tmp_pmatch[AP_MAX_REG_MATCH];\n const char *result;\n AP_DEBUG_ASSERT(info->flags & AP_EXPR_FLAG_STRING_RESULT);\n if (info->root_node->node_op == op_String) {\n *err = NULL;\n return (const char *)info->root_node->node_arg1;\n }\n dont_vary = (info->flags & AP_EXPR_FLAG_DONT_VARY);\n ctx.r = r;\n ctx.c = r->connection;\n ctx.s = r->server;\n ctx.p = r->pool;\n ctx.err = err;\n ctx.info = info;\n ctx.re_nmatch = nmatch;\n ctx.re_pmatch = pmatch;\n ctx.re_source = source;\n ctx.vary_this = dont_vary ? NULL : &vary_this;\n ctx.data = NULL;\n ctx.result_string = &result;\n if (!pmatch) {\n ctx.re_nmatch = AP_MAX_REG_MATCH;\n ctx.re_pmatch = tmp_pmatch;\n ctx.re_source = &tmp_source;\n }\n rc = ap_expr_exec_ctx(&ctx);\n if (rc > 0)\n return result;\n else if (rc < 0)\n return NULL;\n else\n ap_assert(0);\n return NULL;\n}'] |
35,118 | 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;
} | ['int BN_X931_derive_prime_ex(BIGNUM *p, BIGNUM *p1, BIGNUM *p2,\n const BIGNUM *Xp, const BIGNUM *Xp1,\n const BIGNUM *Xp2, const BIGNUM *e, BN_CTX *ctx,\n BN_GENCB *cb)\n{\n int ret = 0;\n BIGNUM *t, *p1p2, *pm1;\n if (!BN_is_odd(e))\n return 0;\n BN_CTX_start(ctx);\n if (p1 == NULL)\n p1 = BN_CTX_get(ctx);\n if (p2 == NULL)\n p2 = BN_CTX_get(ctx);\n t = BN_CTX_get(ctx);\n p1p2 = BN_CTX_get(ctx);\n pm1 = BN_CTX_get(ctx);\n if (pm1 == NULL)\n goto err;\n if (!bn_x931_derive_pi(p1, Xp1, ctx, cb))\n goto err;\n if (!bn_x931_derive_pi(p2, Xp2, ctx, cb))\n goto err;\n if (!BN_mul(p1p2, p1, p2, ctx))\n goto err;\n if (!BN_mod_inverse(p, p2, p1, ctx))\n goto err;\n if (!BN_mul(p, p, p2, ctx))\n goto err;\n if (!BN_mod_inverse(t, p1, p2, ctx))\n goto err;\n if (!BN_mul(t, t, p1, ctx))\n goto err;\n if (!BN_sub(p, p, t))\n goto err;\n if (p->neg && !BN_add(p, p, p1p2))\n goto err;\n if (!BN_mod_sub(p, p, Xp, p1p2, ctx))\n goto err;\n if (!BN_add(p, p, Xp))\n goto err;\n for (;;) {\n int i = 1;\n BN_GENCB_call(cb, 0, i++);\n if (!BN_copy(pm1, p))\n goto err;\n if (!BN_sub_word(pm1, 1))\n goto err;\n if (!BN_gcd(t, pm1, e, ctx))\n goto err;\n if (BN_is_one(t)) {\n int r = BN_is_prime_fasttest_ex(p, 50, ctx, 1, cb);\n if (r < 0)\n goto err;\n if (r)\n break;\n }\n if (!BN_add(p, p, p1p2))\n goto err;\n }\n BN_GENCB_call(cb, 3, 0);\n ret = 1;\n err:\n BN_CTX_end(ctx);\n return ret;\n}', 'int BN_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)\n{\n int ret = 0;\n int top, al, bl;\n BIGNUM *rr;\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n int i;\n#endif\n#ifdef BN_RECURSION\n BIGNUM *t = NULL;\n int j = 0, k;\n#endif\n bn_check_top(a);\n bn_check_top(b);\n bn_check_top(r);\n al = a->top;\n bl = b->top;\n if ((al == 0) || (bl == 0)) {\n BN_zero(r);\n return 1;\n }\n top = al + bl;\n BN_CTX_start(ctx);\n if ((r == a) || (r == b)) {\n if ((rr = BN_CTX_get(ctx)) == NULL)\n goto err;\n } else\n rr = r;\n#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}', 'int BN_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)\n{\n int ret, r_neg, cmp_res;\n bn_check_top(a);\n bn_check_top(b);\n if (a->neg != b->neg) {\n r_neg = a->neg;\n ret = BN_uadd(r, a, b);\n } else {\n cmp_res = BN_ucmp(a, b);\n if (cmp_res > 0) {\n r_neg = a->neg;\n ret = BN_usub(r, a, b);\n } else if (cmp_res < 0) {\n r_neg = !b->neg;\n ret = BN_usub(r, b, a);\n } else {\n r_neg = 0;\n BN_zero(r);\n ret = 1;\n }\n }\n r->neg = r_neg;\n bn_check_top(r);\n return ret;\n}', 'int BN_mod_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m,\n BN_CTX *ctx)\n{\n if (!BN_sub(r, a, b))\n return 0;\n return BN_nnmod(r, r, m, ctx);\n}', 'int BN_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)\n{\n int ret, r_neg, cmp_res;\n bn_check_top(a);\n bn_check_top(b);\n if (a->neg == b->neg) {\n r_neg = a->neg;\n ret = BN_uadd(r, a, b);\n } else {\n cmp_res = BN_ucmp(a, b);\n if (cmp_res > 0) {\n r_neg = a->neg;\n ret = BN_usub(r, a, b);\n } else if (cmp_res < 0) {\n r_neg = b->neg;\n ret = BN_usub(r, b, a);\n } else {\n r_neg = 0;\n BN_zero(r);\n ret = 1;\n }\n }\n r->neg = r_neg;\n bn_check_top(r);\n return ret;\n}', 'int BN_uadd(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)\n{\n int max, min, dif;\n const BN_ULONG *ap, *bp;\n BN_ULONG *rp, carry, t1, t2;\n bn_check_top(a);\n bn_check_top(b);\n if (a->top < b->top) {\n const BIGNUM *tmp;\n tmp = a;\n a = b;\n b = tmp;\n }\n max = a->top;\n min = b->top;\n dif = max - min;\n if (bn_wexpand(r, max + 1) == NULL)\n return 0;\n r->top = max;\n ap = a->d;\n bp = b->d;\n rp = r->d;\n carry = bn_add_words(rp, ap, bp, min);\n rp += min;\n ap += min;\n while (dif) {\n dif--;\n t1 = *(ap++);\n t2 = (t1 + carry) & BN_MASK2;\n *(rp++) = t2;\n carry &= (t2 == 0);\n }\n *rp = carry;\n r->top += carry;\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}', 'void *CRYPTO_zalloc(size_t num, const char *file, int line)\n{\n void *ret = CRYPTO_malloc(num, file, line);\n FAILTEST();\n if (ret != NULL)\n memset(ret, 0, num);\n return ret;\n}', 'void *CRYPTO_malloc(size_t num, const char *file, int line)\n{\n void *ret = NULL;\n INCREMENT(malloc_count);\n if (malloc_impl != NULL && malloc_impl != CRYPTO_malloc)\n return malloc_impl(num, file, line);\n if (num == 0)\n return NULL;\n FAILTEST();\n if (allow_customize) {\n allow_customize = 0;\n }\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n if (call_malloc_debug) {\n CRYPTO_mem_debug_malloc(NULL, num, 0, file, line);\n ret = malloc(num);\n CRYPTO_mem_debug_malloc(ret, num, 1, file, line);\n } else {\n ret = malloc(num);\n }\n#else\n (void)(file); (void)(line);\n ret = malloc(num);\n#endif\n return ret;\n}'] |
35,119 | 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;
} | ['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 (!ec_group_do_inverse_ord(group, u2, sig->s, 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}', 'int ec_group_do_inverse_ord(const EC_GROUP *group, BIGNUM *res,\n const BIGNUM *x, BN_CTX *ctx)\n{\n if (group->meth->field_inverse_mod_ord != NULL)\n return group->meth->field_inverse_mod_ord(group, res, x, ctx);\n else\n return ec_field_inverse_mod_ord(group, res, x, ctx);\n}', 'int EC_POINT_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *g_scalar,\n const EC_POINT *point, const BIGNUM *p_scalar, BN_CTX *ctx)\n{\n const EC_POINT *points[1];\n const BIGNUM *scalars[1];\n points[0] = point;\n scalars[0] = p_scalar;\n return EC_POINTs_mul(group, r, g_scalar,\n (point != NULL\n && p_scalar != NULL), points, scalars, ctx);\n}', 'int EC_POINTs_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,\n size_t num, const EC_POINT *points[],\n const BIGNUM *scalars[], BN_CTX *ctx)\n{\n if (group->meth->mul == 0)\n return ec_wNAF_mul(group, r, scalar, num, points, scalars, ctx);\n return group->meth->mul(group, r, scalar, num, points, scalars, ctx);\n}', 'int ec_wNAF_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,\n size_t num, const EC_POINT *points[], const BIGNUM *scalars[],\n BN_CTX *ctx)\n{\n BN_CTX *new_ctx = NULL;\n const EC_POINT *generator = NULL;\n EC_POINT *tmp = NULL;\n size_t totalnum;\n size_t blocksize = 0, numblocks = 0;\n size_t pre_points_per_block = 0;\n size_t i, j;\n int k;\n int r_is_inverted = 0;\n int r_is_at_infinity = 1;\n size_t *wsize = NULL;\n signed char **wNAF = NULL;\n size_t *wNAF_len = NULL;\n size_t max_len = 0;\n size_t num_val;\n EC_POINT **val = NULL;\n EC_POINT **v;\n EC_POINT ***val_sub = NULL;\n const EC_PRE_COMP *pre_comp = NULL;\n int num_scalar = 0;\n int ret = 0;\n if (!ec_point_is_compat(r, group)) {\n ECerr(EC_F_EC_WNAF_MUL, EC_R_INCOMPATIBLE_OBJECTS);\n return 0;\n }\n if ((scalar == NULL) && (num == 0)) {\n return EC_POINT_set_to_infinity(group, r);\n }\n if (!BN_is_zero(group->order) && !BN_is_zero(group->cofactor)) {\n if ((scalar != NULL) && (num == 0)) {\n return ec_mul_consttime(group, r, scalar, NULL, ctx);\n }\n if ((scalar == NULL) && (num == 1)) {\n return ec_mul_consttime(group, r, scalars[0], points[0], ctx);\n }\n }\n for (i = 0; i < num; i++) {\n if (!ec_point_is_compat(points[i], group)) {\n ECerr(EC_F_EC_WNAF_MUL, EC_R_INCOMPATIBLE_OBJECTS);\n return 0;\n }\n }\n if (ctx == NULL) {\n ctx = new_ctx = BN_CTX_new();\n if (ctx == NULL)\n goto err;\n }\n if (scalar != NULL) {\n generator = EC_GROUP_get0_generator(group);\n if (generator == NULL) {\n ECerr(EC_F_EC_WNAF_MUL, EC_R_UNDEFINED_GENERATOR);\n goto err;\n }\n pre_comp = group->pre_comp.ec;\n if (pre_comp && pre_comp->numblocks\n && (EC_POINT_cmp(group, generator, pre_comp->points[0], ctx) ==\n 0)) {\n blocksize = pre_comp->blocksize;\n numblocks = (BN_num_bits(scalar) / blocksize) + 1;\n if (numblocks > pre_comp->numblocks)\n numblocks = pre_comp->numblocks;\n pre_points_per_block = (size_t)1 << (pre_comp->w - 1);\n if (pre_comp->num != (pre_comp->numblocks * pre_points_per_block)) {\n ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);\n goto err;\n }\n } else {\n pre_comp = NULL;\n numblocks = 1;\n num_scalar = 1;\n }\n }\n totalnum = num + numblocks;\n wsize = OPENSSL_malloc(totalnum * sizeof(wsize[0]));\n wNAF_len = OPENSSL_malloc(totalnum * sizeof(wNAF_len[0]));\n wNAF = OPENSSL_malloc((totalnum + 1) * sizeof(wNAF[0]));\n val_sub = OPENSSL_malloc(totalnum * sizeof(val_sub[0]));\n if (wNAF != NULL)\n wNAF[0] = NULL;\n if (wsize == NULL || wNAF_len == NULL || wNAF == NULL || val_sub == NULL) {\n ECerr(EC_F_EC_WNAF_MUL, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n num_val = 0;\n for (i = 0; i < num + num_scalar; i++) {\n size_t bits;\n bits = i < num ? BN_num_bits(scalars[i]) : BN_num_bits(scalar);\n wsize[i] = EC_window_bits_for_scalar_size(bits);\n num_val += (size_t)1 << (wsize[i] - 1);\n wNAF[i + 1] = NULL;\n wNAF[i] =\n bn_compute_wNAF((i < num ? scalars[i] : scalar), wsize[i],\n &wNAF_len[i]);\n if (wNAF[i] == NULL)\n goto err;\n if (wNAF_len[i] > max_len)\n max_len = wNAF_len[i];\n }\n if (numblocks) {\n if (pre_comp == NULL) {\n if (num_scalar != 1) {\n ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);\n goto err;\n }\n } else {\n signed char *tmp_wNAF = NULL;\n size_t tmp_len = 0;\n if (num_scalar != 0) {\n ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);\n goto err;\n }\n wsize[num] = pre_comp->w;\n tmp_wNAF = bn_compute_wNAF(scalar, wsize[num], &tmp_len);\n if (!tmp_wNAF)\n goto err;\n if (tmp_len <= max_len) {\n numblocks = 1;\n totalnum = num + 1;\n wNAF[num] = tmp_wNAF;\n wNAF[num + 1] = NULL;\n wNAF_len[num] = tmp_len;\n val_sub[num] = pre_comp->points;\n } else {\n signed char *pp;\n EC_POINT **tmp_points;\n if (tmp_len < numblocks * blocksize) {\n numblocks = (tmp_len + blocksize - 1) / blocksize;\n if (numblocks > pre_comp->numblocks) {\n ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);\n OPENSSL_free(tmp_wNAF);\n goto err;\n }\n totalnum = num + numblocks;\n }\n pp = tmp_wNAF;\n tmp_points = pre_comp->points;\n for (i = num; i < totalnum; i++) {\n if (i < totalnum - 1) {\n wNAF_len[i] = blocksize;\n if (tmp_len < blocksize) {\n ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);\n OPENSSL_free(tmp_wNAF);\n goto err;\n }\n tmp_len -= blocksize;\n } else\n wNAF_len[i] = tmp_len;\n wNAF[i + 1] = NULL;\n wNAF[i] = OPENSSL_malloc(wNAF_len[i]);\n if (wNAF[i] == NULL) {\n ECerr(EC_F_EC_WNAF_MUL, ERR_R_MALLOC_FAILURE);\n OPENSSL_free(tmp_wNAF);\n goto err;\n }\n memcpy(wNAF[i], pp, wNAF_len[i]);\n if (wNAF_len[i] > max_len)\n max_len = wNAF_len[i];\n if (*tmp_points == NULL) {\n ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);\n OPENSSL_free(tmp_wNAF);\n goto err;\n }\n val_sub[i] = tmp_points;\n tmp_points += pre_points_per_block;\n pp += blocksize;\n }\n OPENSSL_free(tmp_wNAF);\n }\n }\n }\n val = OPENSSL_malloc((num_val + 1) * sizeof(val[0]));\n if (val == NULL) {\n ECerr(EC_F_EC_WNAF_MUL, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n val[num_val] = NULL;\n v = val;\n for (i = 0; i < num + num_scalar; i++) {\n val_sub[i] = v;\n for (j = 0; j < ((size_t)1 << (wsize[i] - 1)); j++) {\n *v = EC_POINT_new(group);\n if (*v == NULL)\n goto err;\n v++;\n }\n }\n if (!(v == val + num_val)) {\n ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);\n goto err;\n }\n if ((tmp = EC_POINT_new(group)) == NULL)\n goto err;\n for (i = 0; i < num + num_scalar; i++) {\n if (i < num) {\n if (!EC_POINT_copy(val_sub[i][0], points[i]))\n goto err;\n } else {\n if (!EC_POINT_copy(val_sub[i][0], generator))\n goto err;\n }\n if (wsize[i] > 1) {\n if (!EC_POINT_dbl(group, tmp, val_sub[i][0], ctx))\n goto err;\n for (j = 1; j < ((size_t)1 << (wsize[i] - 1)); j++) {\n if (!EC_POINT_add\n (group, val_sub[i][j], val_sub[i][j - 1], tmp, ctx))\n goto err;\n }\n }\n }\n if (!EC_POINTs_make_affine(group, num_val, val, ctx))\n goto err;\n r_is_at_infinity = 1;\n for (k = max_len - 1; k >= 0; k--) {\n if (!r_is_at_infinity) {\n if (!EC_POINT_dbl(group, r, r, ctx))\n goto err;\n }\n for (i = 0; i < totalnum; i++) {\n if (wNAF_len[i] > (size_t)k) {\n int digit = wNAF[i][k];\n int is_neg;\n if (digit) {\n is_neg = digit < 0;\n if (is_neg)\n digit = -digit;\n if (is_neg != r_is_inverted) {\n if (!r_is_at_infinity) {\n if (!EC_POINT_invert(group, r, ctx))\n goto err;\n }\n r_is_inverted = !r_is_inverted;\n }\n if (r_is_at_infinity) {\n if (!EC_POINT_copy(r, val_sub[i][digit >> 1]))\n goto err;\n r_is_at_infinity = 0;\n } else {\n if (!EC_POINT_add\n (group, r, r, val_sub[i][digit >> 1], ctx))\n goto err;\n }\n }\n }\n }\n }\n if (r_is_at_infinity) {\n if (!EC_POINT_set_to_infinity(group, r))\n goto err;\n } else {\n if (r_is_inverted)\n if (!EC_POINT_invert(group, r, ctx))\n goto err;\n }\n ret = 1;\n err:\n BN_CTX_free(new_ctx);\n EC_POINT_free(tmp);\n OPENSSL_free(wsize);\n OPENSSL_free(wNAF_len);\n if (wNAF != NULL) {\n signed char **w;\n for (w = wNAF; *w != NULL; w++)\n OPENSSL_free(*w);\n OPENSSL_free(wNAF);\n }\n if (val != NULL) {\n for (v = val; *v != NULL; v++)\n EC_POINT_clear_free(*v);\n OPENSSL_free(val);\n }\n OPENSSL_free(val_sub);\n return ret;\n}', 'static int ec_mul_consttime(const EC_GROUP *group, EC_POINT *r,\n const BIGNUM *scalar, const EC_POINT *point,\n BN_CTX *ctx)\n{\n int i, cardinality_bits, group_top, kbit, pbit, Z_is_one;\n EC_POINT *s = NULL;\n BIGNUM *k = NULL;\n BIGNUM *lambda = NULL;\n BIGNUM *cardinality = NULL;\n BN_CTX *new_ctx = NULL;\n int ret = 0;\n if (ctx == NULL && (ctx = new_ctx = BN_CTX_secure_new()) == NULL)\n return 0;\n BN_CTX_start(ctx);\n s = EC_POINT_new(group);\n if (s == NULL)\n goto err;\n if (point == NULL) {\n if (!EC_POINT_copy(s, group->generator))\n goto err;\n } else {\n if (!EC_POINT_copy(s, point))\n goto err;\n }\n EC_POINT_BN_set_flags(s, BN_FLG_CONSTTIME);\n cardinality = BN_CTX_get(ctx);\n lambda = BN_CTX_get(ctx);\n k = BN_CTX_get(ctx);\n if (k == NULL || !BN_mul(cardinality, group->order, group->cofactor, ctx))\n goto err;\n cardinality_bits = BN_num_bits(cardinality);\n group_top = bn_get_top(cardinality);\n if ((bn_wexpand(k, group_top + 1) == NULL)\n || (bn_wexpand(lambda, group_top + 1) == NULL))\n goto err;\n if (!BN_copy(k, scalar))\n goto err;\n BN_set_flags(k, BN_FLG_CONSTTIME);\n if ((BN_num_bits(k) > cardinality_bits) || (BN_is_negative(k))) {\n if (!BN_nnmod(k, k, cardinality, ctx))\n goto err;\n }\n if (!BN_add(lambda, k, cardinality))\n goto err;\n BN_set_flags(lambda, BN_FLG_CONSTTIME);\n if (!BN_add(k, lambda, cardinality))\n goto err;\n kbit = BN_is_bit_set(lambda, cardinality_bits);\n BN_consttime_swap(kbit, k, lambda, group_top + 1);\n group_top = bn_get_top(group->field);\n if ((bn_wexpand(s->X, group_top) == NULL)\n || (bn_wexpand(s->Y, group_top) == NULL)\n || (bn_wexpand(s->Z, group_top) == NULL)\n || (bn_wexpand(r->X, group_top) == NULL)\n || (bn_wexpand(r->Y, group_top) == NULL)\n || (bn_wexpand(r->Z, group_top) == NULL))\n goto err;\n if (!ec_point_blind_coordinates(group, s, ctx))\n goto err;\n if (!EC_POINT_copy(r, s))\n goto err;\n EC_POINT_BN_set_flags(r, BN_FLG_CONSTTIME);\n if (!EC_POINT_dbl(group, s, s, ctx))\n goto err;\n pbit = 0;\n#define EC_POINT_CSWAP(c, a, b, w, t) do { \\\n BN_consttime_swap(c, (a)->X, (b)->X, w); \\\n BN_consttime_swap(c, (a)->Y, (b)->Y, w); \\\n BN_consttime_swap(c, (a)->Z, (b)->Z, w); \\\n t = ((a)->Z_is_one ^ (b)->Z_is_one) & (c); \\\n (a)->Z_is_one ^= (t); \\\n (b)->Z_is_one ^= (t); \\\n} while(0)\n for (i = cardinality_bits - 1; i >= 0; i--) {\n kbit = BN_is_bit_set(k, i) ^ pbit;\n EC_POINT_CSWAP(kbit, r, s, group_top, Z_is_one);\n if (!EC_POINT_add(group, s, r, s, ctx))\n goto err;\n if (!EC_POINT_dbl(group, r, r, ctx))\n goto err;\n pbit ^= kbit;\n }\n EC_POINT_CSWAP(pbit, r, s, group_top, Z_is_one);\n#undef EC_POINT_CSWAP\n ret = 1;\n err:\n EC_POINT_free(s);\n BN_CTX_end(ctx);\n BN_CTX_free(new_ctx);\n return ret;\n}', 'BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)\n{\n bn_check_top(b);\n if (a == b)\n return a;\n if (bn_wexpand(a, b->top) == NULL)\n return NULL;\n if (b->top > 0)\n memcpy(a->d, b->d, sizeof(b->d[0]) * b->top);\n a->neg = b->neg;\n a->top = b->top;\n a->flags |= b->flags & BN_FLG_FIXED_TOP;\n bn_check_top(a);\n return a;\n}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n return (words <= a->dmax) ? a : bn_expand2(a, words);\n}'] |
35,120 | 0 | https://github.com/openssl/openssl/blob/9f9442918aeaed5dc2442d81ab8d29fe3e1fb906/crypto/bn/bn_lib.c/#L333 | 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);
if (BN_get_flags(b, BN_FLG_CONSTTIME) != 0)
BN_set_flags(a, BN_FLG_CONSTTIME);
a->top = b->top;
a->neg = b->neg;
bn_check_top(a);
return a;
} | ['int srp_generate_server_master_secret(SSL *s)\n{\n BIGNUM *K = NULL, *u = NULL;\n int ret = -1, tmp_len = 0;\n unsigned char *tmp = NULL;\n if (!SRP_Verify_A_mod_N(s->srp_ctx.A, s->srp_ctx.N))\n goto err;\n if ((u = SRP_Calc_u(s->srp_ctx.A, s->srp_ctx.B, s->srp_ctx.N)) == NULL)\n goto err;\n if ((K = SRP_Calc_server_key(s->srp_ctx.A, s->srp_ctx.v, u, s->srp_ctx.b,\n s->srp_ctx.N)) == NULL)\n goto err;\n tmp_len = BN_num_bytes(K);\n if ((tmp = OPENSSL_malloc(tmp_len)) == NULL)\n goto err;\n BN_bn2bin(K, tmp);\n ret = ssl_generate_master_secret(s, tmp, tmp_len, 1);\n err:\n BN_clear_free(K);\n BN_clear_free(u);\n return ret;\n}', 'int SRP_Verify_A_mod_N(const BIGNUM *A, const BIGNUM *N)\n{\n return SRP_Verify_B_mod_N(A, N);\n}', 'int SRP_Verify_B_mod_N(const BIGNUM *B, const BIGNUM *N)\n{\n BIGNUM *r;\n BN_CTX *bn_ctx;\n int ret = 0;\n if (B == NULL || N == NULL || (bn_ctx = BN_CTX_new()) == NULL)\n return 0;\n if ((r = BN_new()) == NULL)\n goto err;\n if (!BN_nnmod(r, B, N, bn_ctx))\n goto err;\n ret = !BN_is_zero(r);\n err:\n BN_CTX_free(bn_ctx);\n BN_free(r);\n return ret;\n}', 'BIGNUM *SRP_Calc_u(const BIGNUM *A, const BIGNUM *B, const BIGNUM *N)\n{\n return srp_Calc_xy(A, B, N);\n}', 'static BIGNUM *srp_Calc_xy(const BIGNUM *x, const BIGNUM *y, const BIGNUM *N)\n{\n unsigned char digest[SHA_DIGEST_LENGTH];\n unsigned char *tmp = NULL;\n int numN = BN_num_bytes(N);\n BIGNUM *res = NULL;\n if (x != N && BN_ucmp(x, N) >= 0)\n return NULL;\n if (y != N && BN_ucmp(y, N) >= 0)\n return NULL;\n if ((tmp = OPENSSL_malloc(numN * 2)) == NULL)\n goto err;\n if (BN_bn2binpad(x, tmp, numN) < 0\n || BN_bn2binpad(y, tmp + numN, numN) < 0\n || !EVP_Digest(tmp, numN * 2, digest, NULL, EVP_sha1(), NULL))\n goto err;\n res = BN_bin2bn(digest, sizeof(digest), NULL);\n err:\n OPENSSL_free(tmp);\n return res;\n}', 'int BN_num_bits(const BIGNUM *a)\n{\n int i = a->top - 1;\n bn_check_top(a);\n if (BN_is_zero(a))\n return 0;\n return ((i * BN_BITS2) + BN_num_bits_word(a->d[i]));\n}', 'int BN_is_zero(const BIGNUM *a)\n{\n return a->top == 0;\n}', 'BIGNUM *SRP_Calc_server_key(const BIGNUM *A, const BIGNUM *v, const BIGNUM *u,\n const BIGNUM *b, const BIGNUM *N)\n{\n BIGNUM *tmp = NULL, *S = NULL;\n BN_CTX *bn_ctx;\n if (u == NULL || A == NULL || v == NULL || b == NULL || N == NULL)\n return NULL;\n if ((bn_ctx = BN_CTX_new()) == NULL || (tmp = BN_new()) == NULL)\n goto err;\n if (!BN_mod_exp(tmp, v, u, N, bn_ctx))\n goto err;\n if (!BN_mod_mul(tmp, A, tmp, N, bn_ctx))\n goto err;\n S = BN_new();\n if (S != NULL && !BN_mod_exp(S, tmp, b, N, bn_ctx)) {\n BN_free(S);\n S = NULL;\n }\n err:\n BN_CTX_free(bn_ctx);\n BN_clear_free(tmp);\n return S;\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_is_odd(const BIGNUM *a)\n{\n return (a->top > 0) && (a->d[0] & 1);\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}', '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 if (BN_get_flags(b, BN_FLG_CONSTTIME) != 0)\n BN_set_flags(a, BN_FLG_CONSTTIME);\n a->top = b->top;\n a->neg = b->neg;\n bn_check_top(a);\n return a;\n}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n return (words <= a->dmax) ? a : bn_expand2(a, words);\n}'] |
35,121 | 0 | https://github.com/libav/libav/blob/5228bcd8705523cee43e351e1a113e12aefcf837/libavcodec/aacps.c/#L406 | static void hybrid_synthesis(float out[2][38][64], float in[91][32][2], int is34, int len)
{
int i, n;
if (is34) {
for (n = 0; n < len; n++) {
memset(out[0][n], 0, 5*sizeof(out[0][n][0]));
memset(out[1][n], 0, 5*sizeof(out[1][n][0]));
for (i = 0; i < 12; i++) {
out[0][n][0] += in[ i][n][0];
out[1][n][0] += in[ i][n][1];
}
for (i = 0; i < 8; i++) {
out[0][n][1] += in[12+i][n][0];
out[1][n][1] += in[12+i][n][1];
}
for (i = 0; i < 4; i++) {
out[0][n][2] += in[20+i][n][0];
out[1][n][2] += in[20+i][n][1];
out[0][n][3] += in[24+i][n][0];
out[1][n][3] += in[24+i][n][1];
out[0][n][4] += in[28+i][n][0];
out[1][n][4] += in[28+i][n][1];
}
}
for (i = 0; i < 59; i++) {
for (n = 0; n < len; n++) {
out[0][n][i+5] = in[i+32][n][0];
out[1][n][i+5] = in[i+32][n][1];
}
}
} else {
for (n = 0; n < len; n++) {
out[0][n][0] = in[0][n][0] + in[1][n][0] + in[2][n][0] +
in[3][n][0] + in[4][n][0] + in[5][n][0];
out[1][n][0] = in[0][n][1] + in[1][n][1] + in[2][n][1] +
in[3][n][1] + in[4][n][1] + in[5][n][1];
out[0][n][1] = in[6][n][0] + in[7][n][0];
out[1][n][1] = in[6][n][1] + in[7][n][1];
out[0][n][2] = in[8][n][0] + in[9][n][0];
out[1][n][2] = in[8][n][1] + in[9][n][1];
}
for (i = 0; i < 61; i++) {
for (n = 0; n < len; n++) {
out[0][n][i+3] = in[i+10][n][0];
out[1][n][i+3] = in[i+10][n][1];
}
}
}
} | ['void ff_sbr_apply(AACContext *ac, SpectralBandReplication *sbr, int id_aac,\n float* L, float* R)\n{\n int downsampled = ac->m4ac.ext_sample_rate < sbr->sample_rate;\n int ch;\n int nch = (id_aac == TYPE_CPE) ? 2 : 1;\n if (sbr->start) {\n sbr_dequant(sbr, id_aac);\n }\n for (ch = 0; ch < nch; ch++) {\n sbr_qmf_analysis(&ac->dsp, &sbr->mdct_ana, ch ? R : L, sbr->data[ch].analysis_filterbank_samples,\n (float*)sbr->qmf_filter_scratch,\n sbr->data[ch].W, 1/(-1024 * ac->sf_scale));\n sbr_lf_gen(ac, sbr, sbr->X_low, sbr->data[ch].W);\n if (sbr->start) {\n sbr_hf_inverse_filter(sbr->alpha0, sbr->alpha1, sbr->X_low, sbr->k[0]);\n sbr_chirp(sbr, &sbr->data[ch]);\n sbr_hf_gen(ac, sbr, sbr->X_high, sbr->X_low, sbr->alpha0, sbr->alpha1,\n sbr->data[ch].bw_array, sbr->data[ch].t_env,\n sbr->data[ch].bs_num_env);\n sbr_mapping(ac, sbr, &sbr->data[ch], sbr->data[ch].e_a);\n sbr_env_estimate(sbr->e_curr, sbr->X_high, sbr, &sbr->data[ch]);\n sbr_gain_calc(ac, sbr, &sbr->data[ch], sbr->data[ch].e_a);\n sbr_hf_assemble(sbr->data[ch].Y, sbr->X_high, sbr, &sbr->data[ch],\n sbr->data[ch].e_a);\n }\n sbr_x_gen(sbr, sbr->X[ch], sbr->X_low, sbr->data[ch].Y, ch);\n }\n if (ac->m4ac.ps == 1) {\n if (sbr->ps.start) {\n ff_ps_apply(ac->avctx, &sbr->ps, sbr->X[0], sbr->X[1], sbr->kx[1] + sbr->m[1]);\n } else {\n memcpy(sbr->X[1], sbr->X[0], sizeof(sbr->X[0]));\n }\n nch = 2;\n }\n sbr_qmf_synthesis(&ac->dsp, &sbr->mdct, L, sbr->X[0], sbr->qmf_filter_scratch,\n sbr->data[0].synthesis_filterbank_samples,\n &sbr->data[0].synthesis_filterbank_samples_offset,\n downsampled,\n ac->add_bias, -1024 * ac->sf_scale);\n if (nch == 2)\n sbr_qmf_synthesis(&ac->dsp, &sbr->mdct, R, sbr->X[1], sbr->qmf_filter_scratch,\n sbr->data[1].synthesis_filterbank_samples,\n &sbr->data[1].synthesis_filterbank_samples_offset,\n downsampled,\n ac->add_bias, -1024 * ac->sf_scale);\n}', 'int ff_ps_apply(AVCodecContext *avctx, PSContext *ps, float L[2][38][64], float R[2][38][64], int top)\n{\n float Lbuf[91][32][2];\n float Rbuf[91][32][2];\n const int len = 32;\n int is34 = ps->is34bands;\n top += NR_BANDS[is34] - 64;\n memset(ps->delay+top, 0, (NR_BANDS[is34] - top)*sizeof(ps->delay[0]));\n if (top < NR_ALLPASS_BANDS[is34])\n memset(ps->ap_delay + top, 0, (NR_ALLPASS_BANDS[is34] - top)*sizeof(ps->ap_delay[0]));\n hybrid_analysis(Lbuf, ps->in_buf, L, is34, len);\n decorrelation(ps, Rbuf, Lbuf, is34);\n stereo_processing(ps, Lbuf, Rbuf, is34);\n hybrid_synthesis(L, Lbuf, is34, len);\n hybrid_synthesis(R, Rbuf, is34, len);\n return 0;\n}', 'static void hybrid_synthesis(float out[2][38][64], float in[91][32][2], int is34, int len)\n{\n int i, n;\n if (is34) {\n for (n = 0; n < len; n++) {\n memset(out[0][n], 0, 5*sizeof(out[0][n][0]));\n memset(out[1][n], 0, 5*sizeof(out[1][n][0]));\n for (i = 0; i < 12; i++) {\n out[0][n][0] += in[ i][n][0];\n out[1][n][0] += in[ i][n][1];\n }\n for (i = 0; i < 8; i++) {\n out[0][n][1] += in[12+i][n][0];\n out[1][n][1] += in[12+i][n][1];\n }\n for (i = 0; i < 4; i++) {\n out[0][n][2] += in[20+i][n][0];\n out[1][n][2] += in[20+i][n][1];\n out[0][n][3] += in[24+i][n][0];\n out[1][n][3] += in[24+i][n][1];\n out[0][n][4] += in[28+i][n][0];\n out[1][n][4] += in[28+i][n][1];\n }\n }\n for (i = 0; i < 59; i++) {\n for (n = 0; n < len; n++) {\n out[0][n][i+5] = in[i+32][n][0];\n out[1][n][i+5] = in[i+32][n][1];\n }\n }\n } else {\n for (n = 0; n < len; n++) {\n out[0][n][0] = in[0][n][0] + in[1][n][0] + in[2][n][0] +\n in[3][n][0] + in[4][n][0] + in[5][n][0];\n out[1][n][0] = in[0][n][1] + in[1][n][1] + in[2][n][1] +\n in[3][n][1] + in[4][n][1] + in[5][n][1];\n out[0][n][1] = in[6][n][0] + in[7][n][0];\n out[1][n][1] = in[6][n][1] + in[7][n][1];\n out[0][n][2] = in[8][n][0] + in[9][n][0];\n out[1][n][2] = in[8][n][1] + in[9][n][1];\n }\n for (i = 0; i < 61; i++) {\n for (n = 0; n < len; n++) {\n out[0][n][i+3] = in[i+10][n][0];\n out[1][n][i+3] = in[i+10][n][1];\n }\n }\n }\n}'] |
35,122 | 0 | https://github.com/libav/libav/blob/f2589642172d284a67e5bbd6c11c477a2aacda88/libavformat/movenc.c/#L897 | static int mov_write_ctts_tag(ByteIOContext *pb, MOVTrack *track)
{
MOVStts *ctts_entries;
uint32_t entries = 0;
uint32_t atom_size;
int i;
ctts_entries = av_malloc((track->entry + 1) * sizeof(*ctts_entries));
ctts_entries[0].count = 1;
ctts_entries[0].duration = track->cluster[0].cts;
for (i=1; i<track->entry; i++) {
if (track->cluster[i].cts == ctts_entries[entries].duration) {
ctts_entries[entries].count++;
} else {
entries++;
ctts_entries[entries].duration = track->cluster[i].cts;
ctts_entries[entries].count = 1;
}
}
entries++;
atom_size = 16 + (entries * 8);
put_be32(pb, atom_size);
put_tag(pb, "ctts");
put_be32(pb, 0);
put_be32(pb, entries);
for (i=0; i<entries; i++) {
put_be32(pb, ctts_entries[i].count);
put_be32(pb, ctts_entries[i].duration);
}
av_free(ctts_entries);
return atom_size;
} | ['static int mov_write_ctts_tag(ByteIOContext *pb, MOVTrack *track)\n{\n MOVStts *ctts_entries;\n uint32_t entries = 0;\n uint32_t atom_size;\n int i;\n ctts_entries = av_malloc((track->entry + 1) * sizeof(*ctts_entries));\n ctts_entries[0].count = 1;\n ctts_entries[0].duration = track->cluster[0].cts;\n for (i=1; i<track->entry; i++) {\n if (track->cluster[i].cts == ctts_entries[entries].duration) {\n ctts_entries[entries].count++;\n } else {\n entries++;\n ctts_entries[entries].duration = track->cluster[i].cts;\n ctts_entries[entries].count = 1;\n }\n }\n entries++;\n atom_size = 16 + (entries * 8);\n put_be32(pb, atom_size);\n put_tag(pb, "ctts");\n put_be32(pb, 0);\n put_be32(pb, entries);\n for (i=0; i<entries; i++) {\n put_be32(pb, ctts_entries[i].count);\n put_be32(pb, ctts_entries[i].duration);\n }\n av_free(ctts_entries);\n return atom_size;\n}', 'void *av_malloc(FF_INTERNAL_MEM_TYPE 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}'] |
35,123 | 0 | https://github.com/openssl/openssl/blob/24a5f17b6a221c327d292d7236b24717d5e413a9/ssl/s3_cbc.c/#L551 | void ssl3_cbc_digest_record(
const EVP_MD_CTX *ctx,
unsigned char* md_out,
size_t* md_out_size,
const unsigned char header[13],
const unsigned char *data,
size_t data_plus_mac_size,
size_t data_plus_mac_plus_padding_size,
const unsigned char *mac_secret,
unsigned mac_secret_length,
char is_sslv3)
{
union { double align;
unsigned char c[sizeof(LARGEST_DIGEST_CTX)]; } md_state;
void (*md_final_raw)(void *ctx, unsigned char *md_out);
void (*md_transform)(void *ctx, const unsigned char *block);
unsigned md_size, md_block_size = 64;
unsigned sslv3_pad_length = 40, header_length, variance_blocks,
len, max_mac_bytes, num_blocks,
num_starting_blocks, k, mac_end_offset, c, index_a, index_b;
unsigned int bits;
unsigned char length_bytes[MAX_HASH_BIT_COUNT_BYTES];
unsigned char hmac_pad[MAX_HASH_BLOCK_SIZE];
unsigned char first_block[MAX_HASH_BLOCK_SIZE];
unsigned char mac_out[EVP_MAX_MD_SIZE];
unsigned i, j, md_out_size_u;
EVP_MD_CTX md_ctx;
unsigned md_length_size = 8;
char length_is_big_endian = 1;
int ret;
OPENSSL_assert(data_plus_mac_plus_padding_size < 1024*1024);
switch (EVP_MD_CTX_type(ctx))
{
case NID_md5:
MD5_Init((MD5_CTX*)md_state.c);
md_final_raw = tls1_md5_final_raw;
md_transform = (void(*)(void *ctx, const unsigned char *block)) MD5_Transform;
md_size = 16;
sslv3_pad_length = 48;
length_is_big_endian = 0;
break;
case NID_sha1:
SHA1_Init((SHA_CTX*)md_state.c);
md_final_raw = tls1_sha1_final_raw;
md_transform = (void(*)(void *ctx, const unsigned char *block)) SHA1_Transform;
md_size = 20;
break;
#ifndef OPENSSL_NO_SHA256
case NID_sha224:
SHA224_Init((SHA256_CTX*)md_state.c);
md_final_raw = tls1_sha256_final_raw;
md_transform = (void(*)(void *ctx, const unsigned char *block)) SHA256_Transform;
md_size = 224/8;
break;
case NID_sha256:
SHA256_Init((SHA256_CTX*)md_state.c);
md_final_raw = tls1_sha256_final_raw;
md_transform = (void(*)(void *ctx, const unsigned char *block)) SHA256_Transform;
md_size = 32;
break;
#endif
#ifndef OPENSSL_NO_SHA512
case NID_sha384:
SHA384_Init((SHA512_CTX*)md_state.c);
md_final_raw = tls1_sha512_final_raw;
md_transform = (void(*)(void *ctx, const unsigned char *block)) SHA512_Transform;
md_size = 384/8;
md_block_size = 128;
md_length_size = 16;
break;
case NID_sha512:
SHA512_Init((SHA512_CTX*)md_state.c);
md_final_raw = tls1_sha512_final_raw;
md_transform = (void(*)(void *ctx, const unsigned char *block)) SHA512_Transform;
md_size = 64;
md_block_size = 128;
md_length_size = 16;
break;
#endif
default:
OPENSSL_assert(0);
if (md_out_size)
*md_out_size = -1;
return;
}
OPENSSL_assert(md_length_size <= MAX_HASH_BIT_COUNT_BYTES);
OPENSSL_assert(md_block_size <= MAX_HASH_BLOCK_SIZE);
OPENSSL_assert(md_size <= EVP_MAX_MD_SIZE);
header_length = 13;
if (is_sslv3)
{
header_length =
mac_secret_length +
sslv3_pad_length +
8 +
1 +
2 ;
}
variance_blocks = is_sslv3 ? 2 : 6;
len = data_plus_mac_plus_padding_size + header_length;
max_mac_bytes = len - md_size - 1;
num_blocks = (max_mac_bytes + 1 + md_length_size + md_block_size - 1) / md_block_size;
num_starting_blocks = 0;
k = 0;
mac_end_offset = data_plus_mac_size + header_length - md_size;
c = mac_end_offset % md_block_size;
index_a = mac_end_offset / md_block_size;
index_b = (mac_end_offset + md_length_size) / md_block_size;
if (num_blocks > variance_blocks + (is_sslv3 ? 1 : 0))
{
num_starting_blocks = num_blocks - variance_blocks;
k = md_block_size*num_starting_blocks;
}
bits = 8*mac_end_offset;
if (!is_sslv3)
{
bits += 8*md_block_size;
memset(hmac_pad, 0, md_block_size);
OPENSSL_assert(mac_secret_length <= sizeof(hmac_pad));
memcpy(hmac_pad, mac_secret, mac_secret_length);
for (i = 0; i < md_block_size; i++)
hmac_pad[i] ^= 0x36;
md_transform(md_state.c, hmac_pad);
}
if (length_is_big_endian)
{
memset(length_bytes,0,md_length_size-4);
length_bytes[md_length_size-4] = (unsigned char)(bits>>24);
length_bytes[md_length_size-3] = (unsigned char)(bits>>16);
length_bytes[md_length_size-2] = (unsigned char)(bits>>8);
length_bytes[md_length_size-1] = (unsigned char)bits;
}
else
{
memset(length_bytes,0,md_length_size);
length_bytes[md_length_size-5] = (unsigned char)(bits>>24);
length_bytes[md_length_size-6] = (unsigned char)(bits>>16);
length_bytes[md_length_size-7] = (unsigned char)(bits>>8);
length_bytes[md_length_size-8] = (unsigned char)bits;
}
if (k > 0)
{
if (is_sslv3)
{
unsigned overhang = header_length-md_block_size;
md_transform(md_state.c, header);
memcpy(first_block, header + md_block_size, overhang);
memcpy(first_block + overhang, data, md_block_size-overhang);
md_transform(md_state.c, first_block);
for (i = 1; i < k/md_block_size - 1; i++)
md_transform(md_state.c, data + md_block_size*i - overhang);
}
else
{
memcpy(first_block, header, 13);
memcpy(first_block+13, data, md_block_size-13);
md_transform(md_state.c, first_block);
for (i = 1; i < k/md_block_size; i++)
md_transform(md_state.c, data + md_block_size*i - 13);
}
}
memset(mac_out, 0, sizeof(mac_out));
for (i = num_starting_blocks; i <= num_starting_blocks+variance_blocks; i++)
{
unsigned char block[MAX_HASH_BLOCK_SIZE];
unsigned char is_block_a = constant_time_eq_8(i, index_a);
unsigned char is_block_b = constant_time_eq_8(i, index_b);
for (j = 0; j < md_block_size; j++)
{
unsigned char b = 0, is_past_c, is_past_cp1;
if (k < header_length)
b = header[k];
else if (k < data_plus_mac_plus_padding_size + header_length)
b = data[k-header_length];
k++;
is_past_c = is_block_a & constant_time_ge_8(j, c);
is_past_cp1 = is_block_a & constant_time_ge_8(j, c+1);
b = constant_time_select_8(is_past_c, 0x80, b);
b = b&~is_past_cp1;
b &= ~is_block_b | is_block_a;
if (j >= md_block_size - md_length_size)
{
b = constant_time_select_8(
is_block_b, length_bytes[j-(md_block_size-md_length_size)], b);
}
block[j] = b;
}
md_transform(md_state.c, block);
md_final_raw(md_state.c, block);
for (j = 0; j < md_size; j++)
mac_out[j] |= block[j]&is_block_b;
}
EVP_MD_CTX_init(&md_ctx);
EVP_DigestInit_ex(&md_ctx, ctx->digest, NULL );
if (is_sslv3)
{
memset(hmac_pad, 0x5c, sslv3_pad_length);
EVP_DigestUpdate(&md_ctx, mac_secret, mac_secret_length);
EVP_DigestUpdate(&md_ctx, hmac_pad, sslv3_pad_length);
EVP_DigestUpdate(&md_ctx, mac_out, md_size);
}
else
{
for (i = 0; i < md_block_size; i++)
hmac_pad[i] ^= 0x6a;
EVP_DigestUpdate(&md_ctx, hmac_pad, md_block_size);
EVP_DigestUpdate(&md_ctx, mac_out, md_size);
}
ret = EVP_DigestFinal(&md_ctx, md_out, &md_out_size_u);
if (ret && md_out_size)
*md_out_size = md_out_size_u;
EVP_MD_CTX_cleanup(&md_ctx);
} | ['int tls1_mac(SSL *ssl, unsigned char *md, int send)\n\t{\n\tSSL3_RECORD *rec;\n\tunsigned char *seq;\n\tEVP_MD_CTX *hash;\n\tsize_t md_size;\n\tint i;\n\tEVP_MD_CTX hmac, *mac_ctx;\n\tunsigned char header[13];\n\tint stream_mac = (send?(ssl->mac_flags & SSL_MAC_FLAG_WRITE_MAC_STREAM):(ssl->mac_flags&SSL_MAC_FLAG_READ_MAC_STREAM));\n\tint t;\n\tif (send)\n\t\t{\n\t\trec= &(ssl->s3->wrec);\n\t\tseq= &(ssl->s3->write_sequence[0]);\n\t\thash=ssl->write_hash;\n\t\t}\n\telse\n\t\t{\n\t\trec= &(ssl->s3->rrec);\n\t\tseq= &(ssl->s3->read_sequence[0]);\n\t\thash=ssl->read_hash;\n\t\t}\n\tt=EVP_MD_CTX_size(hash);\n\tOPENSSL_assert(t >= 0);\n\tmd_size=t;\n\tif (stream_mac)\n\t\t{\n\t\t\tmac_ctx = hash;\n\t\t}\n\t\telse\n\t\t{\n\t\t\tif (!EVP_MD_CTX_copy(&hmac,hash))\n\t\t\t\treturn -1;\n\t\t\tmac_ctx = &hmac;\n\t\t}\n\tif (SSL_IS_DTLS(ssl))\n\t\t{\n\t\tunsigned char dtlsseq[8],*p=dtlsseq;\n\t\ts2n(send?ssl->d1->w_epoch:ssl->d1->r_epoch, p);\n\t\tmemcpy (p,&seq[2],6);\n\t\tmemcpy(header, dtlsseq, 8);\n\t\t}\n\telse\n\t\tmemcpy(header, seq, 8);\n\theader[8]=rec->type;\n\theader[9]=(unsigned char)(ssl->version>>8);\n\theader[10]=(unsigned char)(ssl->version);\n\theader[11]=(rec->length)>>8;\n\theader[12]=(rec->length)&0xff;\n\tif (!send && !SSL_USE_ETM(ssl) &&\n\t EVP_CIPHER_CTX_mode(ssl->enc_read_ctx) == EVP_CIPH_CBC_MODE &&\n\t ssl3_cbc_record_digest_supported(mac_ctx))\n\t\t{\n\t\tssl3_cbc_digest_record(\n\t\t\tmac_ctx,\n\t\t\tmd, &md_size,\n\t\t\theader, rec->input,\n\t\t\trec->length + md_size, rec->orig_len,\n\t\t\tssl->s3->read_mac_secret,\n\t\t\tssl->s3->read_mac_secret_size,\n\t\t\t0);\n\t\t}\n\telse\n\t\t{\n\t\tEVP_DigestSignUpdate(mac_ctx,header,sizeof(header));\n\t\tEVP_DigestSignUpdate(mac_ctx,rec->input,rec->length);\n\t\tt=EVP_DigestSignFinal(mac_ctx,md,&md_size);\n\t\tOPENSSL_assert(t > 0);\n\t\tif (!send && !SSL_USE_ETM(ssl) && FIPS_mode())\n\t\t\ttls_fips_digest_extra(\n\t \t\t\t\tssl->enc_read_ctx,\n\t\t\t\t\tmac_ctx, rec->input,\n\t\t\t\t\trec->length, rec->orig_len);\n\t\t}\n\tif (!stream_mac)\n\t\tEVP_MD_CTX_cleanup(&hmac);\n#ifdef TLS_DEBUG\nfprintf(stderr,"seq=");\n{int z; for (z=0; z<8; z++) fprintf(stderr,"%02X ",seq[z]); fprintf(stderr,"\\n"); }\nfprintf(stderr,"rec=");\n{unsigned int z; for (z=0; z<rec->length; z++) fprintf(stderr,"%02X ",rec->data[z]); fprintf(stderr,"\\n"); }\n#endif\n\tif (!SSL_IS_DTLS(ssl))\n\t\t{\n\t\tfor (i=7; i>=0; i--)\n\t\t\t{\n\t\t\t++seq[i];\n\t\t\tif (seq[i] != 0) break;\n\t\t\t}\n\t\t}\n#ifdef TLS_DEBUG\n{unsigned int z; for (z=0; z<md_size; z++) fprintf(stderr,"%02X ",md[z]); fprintf(stderr,"\\n"); }\n#endif\n\treturn(md_size);\n\t}', 'void ssl3_cbc_digest_record(\n\tconst EVP_MD_CTX *ctx,\n\tunsigned char* md_out,\n\tsize_t* md_out_size,\n\tconst unsigned char header[13],\n\tconst unsigned char *data,\n\tsize_t data_plus_mac_size,\n\tsize_t data_plus_mac_plus_padding_size,\n\tconst unsigned char *mac_secret,\n\tunsigned mac_secret_length,\n\tchar is_sslv3)\n\t{\n\tunion {\tdouble align;\n\t\tunsigned char c[sizeof(LARGEST_DIGEST_CTX)]; } md_state;\n\tvoid (*md_final_raw)(void *ctx, unsigned char *md_out);\n\tvoid (*md_transform)(void *ctx, const unsigned char *block);\n\tunsigned md_size, md_block_size = 64;\n\tunsigned sslv3_pad_length = 40, header_length, variance_blocks,\n\t\t len, max_mac_bytes, num_blocks,\n\t\t num_starting_blocks, k, mac_end_offset, c, index_a, index_b;\n\tunsigned int bits;\n\tunsigned char length_bytes[MAX_HASH_BIT_COUNT_BYTES];\n\tunsigned char hmac_pad[MAX_HASH_BLOCK_SIZE];\n\tunsigned char first_block[MAX_HASH_BLOCK_SIZE];\n\tunsigned char mac_out[EVP_MAX_MD_SIZE];\n\tunsigned i, j, md_out_size_u;\n\tEVP_MD_CTX md_ctx;\n\tunsigned md_length_size = 8;\n\tchar length_is_big_endian = 1;\n\tint ret;\n\tOPENSSL_assert(data_plus_mac_plus_padding_size < 1024*1024);\n\tswitch (EVP_MD_CTX_type(ctx))\n\t\t{\n\t\tcase NID_md5:\n\t\t\tMD5_Init((MD5_CTX*)md_state.c);\n\t\t\tmd_final_raw = tls1_md5_final_raw;\n\t\t\tmd_transform = (void(*)(void *ctx, const unsigned char *block)) MD5_Transform;\n\t\t\tmd_size = 16;\n\t\t\tsslv3_pad_length = 48;\n\t\t\tlength_is_big_endian = 0;\n\t\t\tbreak;\n\t\tcase NID_sha1:\n\t\t\tSHA1_Init((SHA_CTX*)md_state.c);\n\t\t\tmd_final_raw = tls1_sha1_final_raw;\n\t\t\tmd_transform = (void(*)(void *ctx, const unsigned char *block)) SHA1_Transform;\n\t\t\tmd_size = 20;\n\t\t\tbreak;\n#ifndef OPENSSL_NO_SHA256\n\t\tcase NID_sha224:\n\t\t\tSHA224_Init((SHA256_CTX*)md_state.c);\n\t\t\tmd_final_raw = tls1_sha256_final_raw;\n\t\t\tmd_transform = (void(*)(void *ctx, const unsigned char *block)) SHA256_Transform;\n\t\t\tmd_size = 224/8;\n\t\t\tbreak;\n\t\tcase NID_sha256:\n\t\t\tSHA256_Init((SHA256_CTX*)md_state.c);\n\t\t\tmd_final_raw = tls1_sha256_final_raw;\n\t\t\tmd_transform = (void(*)(void *ctx, const unsigned char *block)) SHA256_Transform;\n\t\t\tmd_size = 32;\n\t\t\tbreak;\n#endif\n#ifndef OPENSSL_NO_SHA512\n\t\tcase NID_sha384:\n\t\t\tSHA384_Init((SHA512_CTX*)md_state.c);\n\t\t\tmd_final_raw = tls1_sha512_final_raw;\n\t\t\tmd_transform = (void(*)(void *ctx, const unsigned char *block)) SHA512_Transform;\n\t\t\tmd_size = 384/8;\n\t\t\tmd_block_size = 128;\n\t\t\tmd_length_size = 16;\n\t\t\tbreak;\n\t\tcase NID_sha512:\n\t\t\tSHA512_Init((SHA512_CTX*)md_state.c);\n\t\t\tmd_final_raw = tls1_sha512_final_raw;\n\t\t\tmd_transform = (void(*)(void *ctx, const unsigned char *block)) SHA512_Transform;\n\t\t\tmd_size = 64;\n\t\t\tmd_block_size = 128;\n\t\t\tmd_length_size = 16;\n\t\t\tbreak;\n#endif\n\t\tdefault:\n\t\t\tOPENSSL_assert(0);\n\t\t\tif (md_out_size)\n\t\t\t\t*md_out_size = -1;\n\t\t\treturn;\n\t\t}\n\tOPENSSL_assert(md_length_size <= MAX_HASH_BIT_COUNT_BYTES);\n\tOPENSSL_assert(md_block_size <= MAX_HASH_BLOCK_SIZE);\n\tOPENSSL_assert(md_size <= EVP_MAX_MD_SIZE);\n\theader_length = 13;\n\tif (is_sslv3)\n\t\t{\n\t\theader_length =\n\t\t\tmac_secret_length +\n\t\t\tsslv3_pad_length +\n\t\t\t8 +\n\t\t\t1 +\n\t\t\t2 ;\n\t\t}\n\tvariance_blocks = is_sslv3 ? 2 : 6;\n\tlen = data_plus_mac_plus_padding_size + header_length;\n\tmax_mac_bytes = len - md_size - 1;\n\tnum_blocks = (max_mac_bytes + 1 + md_length_size + md_block_size - 1) / md_block_size;\n\tnum_starting_blocks = 0;\n\tk = 0;\n\tmac_end_offset = data_plus_mac_size + header_length - md_size;\n\tc = mac_end_offset % md_block_size;\n\tindex_a = mac_end_offset / md_block_size;\n\tindex_b = (mac_end_offset + md_length_size) / md_block_size;\n\tif (num_blocks > variance_blocks + (is_sslv3 ? 1 : 0))\n\t\t{\n\t\tnum_starting_blocks = num_blocks - variance_blocks;\n\t\tk = md_block_size*num_starting_blocks;\n\t\t}\n\tbits = 8*mac_end_offset;\n\tif (!is_sslv3)\n\t\t{\n\t\tbits += 8*md_block_size;\n\t\tmemset(hmac_pad, 0, md_block_size);\n\t\tOPENSSL_assert(mac_secret_length <= sizeof(hmac_pad));\n\t\tmemcpy(hmac_pad, mac_secret, mac_secret_length);\n\t\tfor (i = 0; i < md_block_size; i++)\n\t\t\thmac_pad[i] ^= 0x36;\n\t\tmd_transform(md_state.c, hmac_pad);\n\t\t}\n\tif (length_is_big_endian)\n\t\t{\n\t\tmemset(length_bytes,0,md_length_size-4);\n\t\tlength_bytes[md_length_size-4] = (unsigned char)(bits>>24);\n\t\tlength_bytes[md_length_size-3] = (unsigned char)(bits>>16);\n\t\tlength_bytes[md_length_size-2] = (unsigned char)(bits>>8);\n\t\tlength_bytes[md_length_size-1] = (unsigned char)bits;\n\t\t}\n\telse\n\t\t{\n\t\tmemset(length_bytes,0,md_length_size);\n\t\tlength_bytes[md_length_size-5] = (unsigned char)(bits>>24);\n\t\tlength_bytes[md_length_size-6] = (unsigned char)(bits>>16);\n\t\tlength_bytes[md_length_size-7] = (unsigned char)(bits>>8);\n\t\tlength_bytes[md_length_size-8] = (unsigned char)bits;\n\t\t}\n\tif (k > 0)\n\t\t{\n\t\tif (is_sslv3)\n\t\t\t{\n\t\t\tunsigned overhang = header_length-md_block_size;\n\t\t\tmd_transform(md_state.c, header);\n\t\t\tmemcpy(first_block, header + md_block_size, overhang);\n\t\t\tmemcpy(first_block + overhang, data, md_block_size-overhang);\n\t\t\tmd_transform(md_state.c, first_block);\n\t\t\tfor (i = 1; i < k/md_block_size - 1; i++)\n\t\t\t\tmd_transform(md_state.c, data + md_block_size*i - overhang);\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tmemcpy(first_block, header, 13);\n\t\t\tmemcpy(first_block+13, data, md_block_size-13);\n\t\t\tmd_transform(md_state.c, first_block);\n\t\t\tfor (i = 1; i < k/md_block_size; i++)\n\t\t\t\tmd_transform(md_state.c, data + md_block_size*i - 13);\n\t\t\t}\n\t\t}\n\tmemset(mac_out, 0, sizeof(mac_out));\n\tfor (i = num_starting_blocks; i <= num_starting_blocks+variance_blocks; i++)\n\t\t{\n\t\tunsigned char block[MAX_HASH_BLOCK_SIZE];\n\t\tunsigned char is_block_a = constant_time_eq_8(i, index_a);\n\t\tunsigned char is_block_b = constant_time_eq_8(i, index_b);\n\t\tfor (j = 0; j < md_block_size; j++)\n\t\t\t{\n\t\t\tunsigned char b = 0, is_past_c, is_past_cp1;\n\t\t\tif (k < header_length)\n\t\t\t\tb = header[k];\n\t\t\telse if (k < data_plus_mac_plus_padding_size + header_length)\n\t\t\t\tb = data[k-header_length];\n\t\t\tk++;\n\t\t\tis_past_c = is_block_a & constant_time_ge_8(j, c);\n\t\t\tis_past_cp1 = is_block_a & constant_time_ge_8(j, c+1);\n b = constant_time_select_8(is_past_c, 0x80, b);\n\t\t\tb = b&~is_past_cp1;\n\t\t\tb &= ~is_block_b | is_block_a;\n\t\t\tif (j >= md_block_size - md_length_size)\n\t\t\t\t{\n\t\t\t\tb = constant_time_select_8(\n\t\t\t\t\tis_block_b, length_bytes[j-(md_block_size-md_length_size)], b);\n\t\t\t\t}\n\t\t\tblock[j] = b;\n\t\t\t}\n\t\tmd_transform(md_state.c, block);\n\t\tmd_final_raw(md_state.c, block);\n\t\tfor (j = 0; j < md_size; j++)\n\t\t\tmac_out[j] |= block[j]&is_block_b;\n\t\t}\n\tEVP_MD_CTX_init(&md_ctx);\n\tEVP_DigestInit_ex(&md_ctx, ctx->digest, NULL );\n\tif (is_sslv3)\n\t\t{\n\t\tmemset(hmac_pad, 0x5c, sslv3_pad_length);\n\t\tEVP_DigestUpdate(&md_ctx, mac_secret, mac_secret_length);\n\t\tEVP_DigestUpdate(&md_ctx, hmac_pad, sslv3_pad_length);\n\t\tEVP_DigestUpdate(&md_ctx, mac_out, md_size);\n\t\t}\n\telse\n\t\t{\n\t\tfor (i = 0; i < md_block_size; i++)\n\t\t\thmac_pad[i] ^= 0x6a;\n\t\tEVP_DigestUpdate(&md_ctx, hmac_pad, md_block_size);\n\t\tEVP_DigestUpdate(&md_ctx, mac_out, md_size);\n\t\t}\n\tret = EVP_DigestFinal(&md_ctx, md_out, &md_out_size_u);\n\tif (ret && md_out_size)\n\t\t*md_out_size = md_out_size_u;\n\tEVP_MD_CTX_cleanup(&md_ctx);\n\t}'] |
35,124 | 0 | https://github.com/openssl/openssl/blob/f61c5ca6ca183bf0a51651857e3efb02a98889ad/ssl/t1_lib.c/#L2149 | DH *ssl_get_auto_dh(SSL *s)
{
int dh_secbits = 80;
if (s->cert->dh_tmp_auto == 2)
return DH_get_1024_160();
if (s->s3->tmp.new_cipher->algorithm_auth & (SSL_aNULL | SSL_aPSK)) {
if (s->s3->tmp.new_cipher->strength_bits == 256)
dh_secbits = 128;
else
dh_secbits = 80;
} else {
CERT_PKEY *cpk = ssl_get_server_send_pkey(s);
dh_secbits = EVP_PKEY_security_bits(cpk->privatekey);
}
if (dh_secbits >= 128) {
DH *dhp = DH_new();
BIGNUM *p, *g;
if (dhp == NULL)
return NULL;
g = BN_new();
if (g != NULL)
BN_set_word(g, 2);
if (dh_secbits >= 192)
p = BN_get_rfc3526_prime_8192(NULL);
else
p = BN_get_rfc3526_prime_3072(NULL);
if (p == NULL || g == NULL || !DH_set0_pqg(dhp, p, NULL, g)) {
DH_free(dhp);
BN_free(p);
BN_free(g);
return NULL;
}
return dhp;
}
if (dh_secbits >= 112)
return DH_get_2048_224();
return DH_get_1024_160();
} | ['DH *ssl_get_auto_dh(SSL *s)\n{\n int dh_secbits = 80;\n if (s->cert->dh_tmp_auto == 2)\n return DH_get_1024_160();\n if (s->s3->tmp.new_cipher->algorithm_auth & (SSL_aNULL | SSL_aPSK)) {\n if (s->s3->tmp.new_cipher->strength_bits == 256)\n dh_secbits = 128;\n else\n dh_secbits = 80;\n } else {\n CERT_PKEY *cpk = ssl_get_server_send_pkey(s);\n dh_secbits = EVP_PKEY_security_bits(cpk->privatekey);\n }\n if (dh_secbits >= 128) {\n DH *dhp = DH_new();\n BIGNUM *p, *g;\n if (dhp == NULL)\n return NULL;\n g = BN_new();\n if (g != NULL)\n BN_set_word(g, 2);\n if (dh_secbits >= 192)\n p = BN_get_rfc3526_prime_8192(NULL);\n else\n p = BN_get_rfc3526_prime_3072(NULL);\n if (p == NULL || g == NULL || !DH_set0_pqg(dhp, p, NULL, g)) {\n DH_free(dhp);\n BN_free(p);\n BN_free(g);\n return NULL;\n }\n return dhp;\n }\n if (dh_secbits >= 112)\n return DH_get_2048_224();\n return DH_get_1024_160();\n}', 'CERT_PKEY *ssl_get_server_send_pkey(SSL *s)\n{\n CERT *c;\n int i;\n c = s->cert;\n if (!s->s3 || !s->s3->tmp.new_cipher)\n return NULL;\n ssl_set_masks(s);\n i = ssl_get_server_cert_index(s);\n if (i < 0)\n return NULL;\n return &c->pkeys[i];\n}', 'static int ssl_get_server_cert_index(const SSL *s)\n{\n int idx;\n idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);\n if (idx == SSL_PKEY_RSA_ENC && !s->cert->pkeys[SSL_PKEY_RSA_ENC].x509)\n idx = SSL_PKEY_RSA_SIGN;\n if (idx == SSL_PKEY_GOST_EC) {\n if (s->cert->pkeys[SSL_PKEY_GOST12_512].x509)\n idx = SSL_PKEY_GOST12_512;\n else if (s->cert->pkeys[SSL_PKEY_GOST12_256].x509)\n idx = SSL_PKEY_GOST12_256;\n else if (s->cert->pkeys[SSL_PKEY_GOST01].x509)\n idx = SSL_PKEY_GOST01;\n else\n idx = -1;\n }\n if (idx == -1)\n SSLerr(SSL_F_SSL_GET_SERVER_CERT_INDEX, ERR_R_INTERNAL_ERROR);\n return idx;\n}', 'int ssl_cipher_get_cert_index(const SSL_CIPHER *c)\n{\n uint32_t alg_a;\n alg_a = c->algorithm_auth;\n if (alg_a & SSL_aECDSA)\n return SSL_PKEY_ECC;\n else if (alg_a & SSL_aDSS)\n return SSL_PKEY_DSA_SIGN;\n else if (alg_a & SSL_aRSA)\n return SSL_PKEY_RSA_ENC;\n else if (alg_a & SSL_aGOST12)\n return SSL_PKEY_GOST_EC;\n else if (alg_a & SSL_aGOST01)\n return SSL_PKEY_GOST01;\n return -1;\n}'] |
35,125 | 0 | https://github.com/libav/libav/blob/5c8696555abd30a200d0d882e2913f66619fba68/libavcodec/qtrle.c/#L174 | static void qtrle_decode_8bpp(QtrleContext *s, int row_ptr, int lines_to_change)
{
int rle_code;
int pixel_ptr;
int row_inc = s->frame.linesize[0];
unsigned char pi1, pi2, pi3, pi4;
unsigned char *rgb = s->frame.data[0];
int pixel_limit = s->frame.linesize[0] * s->avctx->height;
while (lines_to_change--) {
pixel_ptr = row_ptr + (4 * (bytestream2_get_byte(&s->g) - 1));
CHECK_PIXEL_PTR(0);
while ((rle_code = (signed char)bytestream2_get_byte(&s->g)) != -1) {
if (rle_code == 0) {
pixel_ptr += (4 * (bytestream2_get_byte(&s->g) - 1));
CHECK_PIXEL_PTR(0);
} else if (rle_code < 0) {
rle_code = -rle_code;
pi1 = bytestream2_get_byte(&s->g);
pi2 = bytestream2_get_byte(&s->g);
pi3 = bytestream2_get_byte(&s->g);
pi4 = bytestream2_get_byte(&s->g);
CHECK_PIXEL_PTR(rle_code * 4);
while (rle_code--) {
rgb[pixel_ptr++] = pi1;
rgb[pixel_ptr++] = pi2;
rgb[pixel_ptr++] = pi3;
rgb[pixel_ptr++] = pi4;
}
} else {
rle_code *= 4;
CHECK_PIXEL_PTR(rle_code);
while (rle_code--) {
rgb[pixel_ptr++] = bytestream2_get_byte(&s->g);
}
}
}
row_ptr += row_inc;
}
} | ['static void qtrle_decode_8bpp(QtrleContext *s, int row_ptr, int lines_to_change)\n{\n int rle_code;\n int pixel_ptr;\n int row_inc = s->frame.linesize[0];\n unsigned char pi1, pi2, pi3, pi4;\n unsigned char *rgb = s->frame.data[0];\n int pixel_limit = s->frame.linesize[0] * s->avctx->height;\n while (lines_to_change--) {\n pixel_ptr = row_ptr + (4 * (bytestream2_get_byte(&s->g) - 1));\n CHECK_PIXEL_PTR(0);\n while ((rle_code = (signed char)bytestream2_get_byte(&s->g)) != -1) {\n if (rle_code == 0) {\n pixel_ptr += (4 * (bytestream2_get_byte(&s->g) - 1));\n CHECK_PIXEL_PTR(0);\n } else if (rle_code < 0) {\n rle_code = -rle_code;\n pi1 = bytestream2_get_byte(&s->g);\n pi2 = bytestream2_get_byte(&s->g);\n pi3 = bytestream2_get_byte(&s->g);\n pi4 = bytestream2_get_byte(&s->g);\n CHECK_PIXEL_PTR(rle_code * 4);\n while (rle_code--) {\n rgb[pixel_ptr++] = pi1;\n rgb[pixel_ptr++] = pi2;\n rgb[pixel_ptr++] = pi3;\n rgb[pixel_ptr++] = pi4;\n }\n } else {\n rle_code *= 4;\n CHECK_PIXEL_PTR(rle_code);\n while (rle_code--) {\n rgb[pixel_ptr++] = bytestream2_get_byte(&s->g);\n }\n }\n }\n row_ptr += row_inc;\n }\n}', 'DEF(unsigned int, byte, 1, AV_RB8 , AV_WB8)'] |
35,126 | 0 | https://github.com/openssl/openssl/blob/92eb4c551d7433ba1e74e77001dab2e256f8a870/crypto/bn/bn_ctx.c/#L355 | static unsigned int BN_STACK_pop(BN_STACK *st)
{
return st->indexes[--(st->depth)];
} | ['static int RSA_eay_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)\n\t{\n\tBIGNUM *r1,*m1,*vrfy;\n\tBIGNUM local_dmp1,local_dmq1,local_c,local_r1;\n\tBIGNUM *dmp1,*dmq1,*c,*pr1;\n\tint ret=0;\n\tBN_CTX_start(ctx);\n\tr1 = BN_CTX_get(ctx);\n\tm1 = BN_CTX_get(ctx);\n\tvrfy = BN_CTX_get(ctx);\n\t{\n\t\tBIGNUM local_p, local_q;\n\t\tBIGNUM *p = NULL, *q = NULL;\n\t\tif (!(rsa->flags & RSA_FLAG_NO_CONSTTIME))\n\t\t\t{\n\t\t\tBN_init(&local_p);\n\t\t\tp = &local_p;\n\t\t\tBN_with_flags(p, rsa->p, BN_FLG_CONSTTIME);\n\t\t\tBN_init(&local_q);\n\t\t\tq = &local_q;\n\t\t\tBN_with_flags(q, rsa->q, BN_FLG_CONSTTIME);\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tp = rsa->p;\n\t\t\tq = rsa->q;\n\t\t\t}\n\t\tif (rsa->flags & RSA_FLAG_CACHE_PRIVATE)\n\t\t\t{\n\t\t\tif (!BN_MONT_CTX_set_locked(&rsa->_method_mod_p, CRYPTO_LOCK_RSA, p, ctx))\n\t\t\t\tgoto err;\n\t\t\tif (!BN_MONT_CTX_set_locked(&rsa->_method_mod_q, CRYPTO_LOCK_RSA, q, ctx))\n\t\t\t\tgoto err;\n\t\t\t}\n\t}\n\tif (rsa->flags & RSA_FLAG_CACHE_PUBLIC)\n\t\tif (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, CRYPTO_LOCK_RSA, rsa->n, ctx))\n\t\t\tgoto err;\n\tif (!(rsa->flags & RSA_FLAG_NO_CONSTTIME))\n\t\t{\n\t\tc = &local_c;\n\t\tBN_with_flags(c, I, BN_FLG_CONSTTIME);\n\t\tif (!BN_mod(r1,c,rsa->q,ctx)) goto err;\n\t\t}\n\telse\n\t\t{\n\t\tif (!BN_mod(r1,I,rsa->q,ctx)) goto err;\n\t\t}\n\tif (!(rsa->flags & RSA_FLAG_NO_CONSTTIME))\n\t\t{\n\t\tdmq1 = &local_dmq1;\n\t\tBN_with_flags(dmq1, rsa->dmq1, BN_FLG_CONSTTIME);\n\t\t}\n\telse\n\t\tdmq1 = rsa->dmq1;\n\tif (!rsa->meth->bn_mod_exp(m1,r1,dmq1,rsa->q,ctx,\n\t\trsa->_method_mod_q)) goto err;\n\tif (!(rsa->flags & RSA_FLAG_NO_CONSTTIME))\n\t\t{\n\t\tc = &local_c;\n\t\tBN_with_flags(c, I, BN_FLG_CONSTTIME);\n\t\tif (!BN_mod(r1,c,rsa->p,ctx)) goto err;\n\t\t}\n\telse\n\t\t{\n\t\tif (!BN_mod(r1,I,rsa->p,ctx)) goto err;\n\t\t}\n\tif (!(rsa->flags & RSA_FLAG_NO_CONSTTIME))\n\t\t{\n\t\tdmp1 = &local_dmp1;\n\t\tBN_with_flags(dmp1, rsa->dmp1, BN_FLG_CONSTTIME);\n\t\t}\n\telse\n\t\tdmp1 = rsa->dmp1;\n\tif (!rsa->meth->bn_mod_exp(r0,r1,dmp1,rsa->p,ctx,\n\t\trsa->_method_mod_p)) goto err;\n\tif (!BN_sub(r0,r0,m1)) goto err;\n\tif (BN_is_negative(r0))\n\t\tif (!BN_add(r0,r0,rsa->p)) goto err;\n\tif (!BN_mul(r1,r0,rsa->iqmp,ctx)) goto err;\n\tif (!(rsa->flags & RSA_FLAG_NO_CONSTTIME))\n\t\t{\n\t\tpr1 = &local_r1;\n\t\tBN_with_flags(pr1, r1, BN_FLG_CONSTTIME);\n\t\t}\n\telse\n\t\tpr1 = r1;\n\tif (!BN_mod(r0,pr1,rsa->p,ctx)) goto err;\n\tif (BN_is_negative(r0))\n\t\tif (!BN_add(r0,r0,rsa->p)) goto err;\n\tif (!BN_mul(r1,r0,rsa->q,ctx)) goto err;\n\tif (!BN_add(r0,r1,m1)) goto err;\n\tif (rsa->e && rsa->n)\n\t\t{\n\t\tif (!rsa->meth->bn_mod_exp(vrfy,r0,rsa->e,rsa->n,ctx,rsa->_method_mod_n)) goto err;\n\t\tif (!BN_sub(vrfy, vrfy, I)) goto err;\n\t\tif (!BN_mod(vrfy, vrfy, rsa->n, ctx)) goto err;\n\t\tif (BN_is_negative(vrfy))\n\t\t\tif (!BN_add(vrfy, vrfy, rsa->n)) goto err;\n\t\tif (!BN_is_zero(vrfy))\n\t\t\t{\n\t\t\tBIGNUM local_d;\n\t\t\tBIGNUM *d = NULL;\n\t\t\tif (!(rsa->flags & RSA_FLAG_NO_CONSTTIME))\n\t\t\t\t{\n\t\t\t\td = &local_d;\n\t\t\t\tBN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);\n\t\t\t\t}\n\t\t\telse\n\t\t\t\td = rsa->d;\n\t\t\tif (!rsa->meth->bn_mod_exp(r0,I,d,rsa->n,ctx,\n\t\t\t\t\t\t rsa->_method_mod_n)) goto err;\n\t\t\t}\n\t\t}\n\tret=1;\nerr:\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}', 'BN_MONT_CTX *BN_MONT_CTX_set_locked(BN_MONT_CTX **pmont, int lock,\n\t\t\t\t\tconst BIGNUM *mod, BN_CTX *ctx)\n\t{\n\tint got_write_lock = 0;\n\tBN_MONT_CTX *ret;\n\tCRYPTO_r_lock(lock);\n\tif (!*pmont)\n\t\t{\n\t\tCRYPTO_r_unlock(lock);\n\t\tCRYPTO_w_lock(lock);\n\t\tgot_write_lock = 1;\n\t\tif (!*pmont)\n\t\t\t{\n\t\t\tret = BN_MONT_CTX_new();\n\t\t\tif (ret && !BN_MONT_CTX_set(ret, mod, ctx))\n\t\t\t\tBN_MONT_CTX_free(ret);\n\t\t\telse\n\t\t\t\t*pmont = ret;\n\t\t\t}\n\t\t}\n\tret = *pmont;\n\tif (got_write_lock)\n\t\tCRYPTO_w_unlock(lock);\n\telse\n\t\tCRYPTO_r_unlock(lock);\n\treturn ret;\n\t}', 'int BN_MONT_CTX_set(BN_MONT_CTX *mont, const BIGNUM *mod, BN_CTX *ctx)\n\t{\n\tint ret = 0;\n\tBIGNUM *Ri,*R;\n\tBN_CTX_start(ctx);\n\tif((Ri = BN_CTX_get(ctx)) == NULL) goto err;\n\tR= &(mont->RR);\n\tif (!BN_copy(&(mont->N),mod)) goto err;\n\tmont->N.neg = 0;\n#ifdef MONT_WORD\n\t\t{\n\t\tBIGNUM tmod;\n\t\tBN_ULONG buf[2];\n\t\tBN_init(&tmod);\n\t\ttmod.d=buf;\n\t\ttmod.dmax=2;\n\t\ttmod.neg=0;\n\t\tmont->ri=(BN_num_bits(mod)+(BN_BITS2-1))/BN_BITS2*BN_BITS2;\n#if defined(OPENSSL_BN_ASM_MONT) && (BN_BITS2<=32)\n\t\tBN_zero(R);\n\t\tif (!(BN_set_bit(R,2*BN_BITS2))) goto err;\n\t\t\t\t\t\t\t\ttmod.top=0;\n\t\tif ((buf[0] = mod->d[0]))\t\t\ttmod.top=1;\n\t\tif ((buf[1] = mod->top>1 ? mod->d[1] : 0))\ttmod.top=2;\n\t\tif ((BN_mod_inverse(Ri,R,&tmod,ctx)) == NULL)\n\t\t\tgoto err;\n\t\tif (!BN_lshift(Ri,Ri,2*BN_BITS2)) goto err;\n\t\tif (!BN_is_zero(Ri))\n\t\t\t{\n\t\t\tif (!BN_sub_word(Ri,1)) goto err;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tif (bn_expand(Ri,(int)sizeof(BN_ULONG)*2) == NULL)\n\t\t\t\tgoto err;\n\t\t\tRi->neg=0;\n\t\t\tRi->d[0]=BN_MASK2;\n\t\t\tRi->d[1]=BN_MASK2;\n\t\t\tRi->top=2;\n\t\t\t}\n\t\tif (!BN_div(Ri,NULL,Ri,&tmod,ctx)) goto err;\n\t\tmont->n0[0] = (Ri->top > 0) ? Ri->d[0] : 0;\n\t\tmont->n0[1] = (Ri->top > 1) ? Ri->d[1] : 0;\n#else\n\t\tBN_zero(R);\n\t\tif (!(BN_set_bit(R,BN_BITS2))) goto err;\n\t\tbuf[0]=mod->d[0];\n\t\tbuf[1]=0;\n\t\ttmod.top = buf[0] != 0 ? 1 : 0;\n\t\tif ((BN_mod_inverse(Ri,R,&tmod,ctx)) == NULL)\n\t\t\tgoto err;\n\t\tif (!BN_lshift(Ri,Ri,BN_BITS2)) goto err;\n\t\tif (!BN_is_zero(Ri))\n\t\t\t{\n\t\t\tif (!BN_sub_word(Ri,1)) goto err;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tif (!BN_set_word(Ri,BN_MASK2)) goto err;\n\t\t\t}\n\t\tif (!BN_div(Ri,NULL,Ri,&tmod,ctx)) goto err;\n\t\tmont->n0[0] = (Ri->top > 0) ? Ri->d[0] : 0;\n\t\tmont->n0[1] = 0;\n#endif\n\t\t}\n#else\n\t\t{\n\t\tmont->ri=BN_num_bits(&mont->N);\n\t\tBN_zero(R);\n\t\tif (!BN_set_bit(R,mont->ri)) goto err;\n\t\tif ((BN_mod_inverse(Ri,R,&mont->N,ctx)) == NULL)\n\t\t\tgoto err;\n\t\tif (!BN_lshift(Ri,Ri,mont->ri)) goto err;\n\t\tif (!BN_sub_word(Ri,1)) goto err;\n\t\tif (!BN_div(&(mont->Ni),NULL,Ri,&mont->N,ctx)) goto err;\n\t\t}\n#endif\n\tBN_zero(&(mont->RR));\n\tif (!BN_set_bit(&(mont->RR),mont->ri*2)) goto err;\n\tif (!BN_mod(&(mont->RR),&(mont->RR),&(mont->N),ctx)) goto err;\n\tret = 1;\nerr:\n\tBN_CTX_end(ctx);\n\treturn ret;\n\t}', 'BIGNUM *BN_mod_inverse(BIGNUM *in,\n\tconst BIGNUM *a, const BIGNUM *n, BN_CTX *ctx)\n\t{\n\tBIGNUM *rv;\n\tint noinv;\n\trv = int_bn_mod_inverse(in, a, n, ctx, &noinv);\n\tif (noinv)\n\t\tBNerr(BN_F_BN_MOD_INVERSE,BN_R_NO_INVERSE);\n\treturn rv;\n\t}', 'BIGNUM *int_bn_mod_inverse(BIGNUM *in,\n\tconst BIGNUM *a, const BIGNUM *n, BN_CTX *ctx, int *pnoinv)\n\t{\n\tBIGNUM *A,*B,*X,*Y,*M,*D,*T,*R=NULL;\n\tBIGNUM *ret=NULL;\n\tint sign;\n\tif (pnoinv)\n\t\t*pnoinv = 0;\n\tif ((BN_get_flags(a, BN_FLG_CONSTTIME) != 0) || (BN_get_flags(n, BN_FLG_CONSTTIME) != 0))\n\t\t{\n\t\treturn BN_mod_inverse_no_branch(in, a, n, ctx);\n\t\t}\n\tbn_check_top(a);\n\tbn_check_top(n);\n\tBN_CTX_start(ctx);\n\tA = BN_CTX_get(ctx);\n\tB = BN_CTX_get(ctx);\n\tX = BN_CTX_get(ctx);\n\tD = BN_CTX_get(ctx);\n\tM = BN_CTX_get(ctx);\n\tY = BN_CTX_get(ctx);\n\tT = BN_CTX_get(ctx);\n\tif (T == NULL) goto err;\n\tif (in == NULL)\n\t\tR=BN_new();\n\telse\n\t\tR=in;\n\tif (R == NULL) goto err;\n\tBN_one(X);\n\tBN_zero(Y);\n\tif (BN_copy(B,a) == NULL) goto err;\n\tif (BN_copy(A,n) == NULL) goto err;\n\tA->neg = 0;\n\tif (B->neg || (BN_ucmp(B, A) >= 0))\n\t\t{\n\t\tif (!BN_nnmod(B, B, A, ctx)) goto err;\n\t\t}\n\tsign = -1;\n\tif (BN_is_odd(n) && (BN_num_bits(n) <= (BN_BITS <= 32 ? 450 : 2048)))\n\t\t{\n\t\tint shift;\n\t\twhile (!BN_is_zero(B))\n\t\t\t{\n\t\t\tshift = 0;\n\t\t\twhile (!BN_is_bit_set(B, shift))\n\t\t\t\t{\n\t\t\t\tshift++;\n\t\t\t\tif (BN_is_odd(X))\n\t\t\t\t\t{\n\t\t\t\t\tif (!BN_uadd(X, X, n)) goto err;\n\t\t\t\t\t}\n\t\t\t\tif (!BN_rshift1(X, X)) goto err;\n\t\t\t\t}\n\t\t\tif (shift > 0)\n\t\t\t\t{\n\t\t\t\tif (!BN_rshift(B, B, shift)) goto err;\n\t\t\t\t}\n\t\t\tshift = 0;\n\t\t\twhile (!BN_is_bit_set(A, shift))\n\t\t\t\t{\n\t\t\t\tshift++;\n\t\t\t\tif (BN_is_odd(Y))\n\t\t\t\t\t{\n\t\t\t\t\tif (!BN_uadd(Y, Y, n)) goto err;\n\t\t\t\t\t}\n\t\t\t\tif (!BN_rshift1(Y, Y)) goto err;\n\t\t\t\t}\n\t\t\tif (shift > 0)\n\t\t\t\t{\n\t\t\t\tif (!BN_rshift(A, A, shift)) goto err;\n\t\t\t\t}\n\t\t\tif (BN_ucmp(B, A) >= 0)\n\t\t\t\t{\n\t\t\t\tif (!BN_uadd(X, X, Y)) goto err;\n\t\t\t\tif (!BN_usub(B, B, A)) goto err;\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\tif (!BN_uadd(Y, Y, X)) goto err;\n\t\t\t\tif (!BN_usub(A, A, B)) goto err;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\telse\n\t\t{\n\t\twhile (!BN_is_zero(B))\n\t\t\t{\n\t\t\tBIGNUM *tmp;\n\t\t\tif (BN_num_bits(A) == BN_num_bits(B))\n\t\t\t\t{\n\t\t\t\tif (!BN_one(D)) goto err;\n\t\t\t\tif (!BN_sub(M,A,B)) goto err;\n\t\t\t\t}\n\t\t\telse if (BN_num_bits(A) == BN_num_bits(B) + 1)\n\t\t\t\t{\n\t\t\t\tif (!BN_lshift1(T,B)) goto err;\n\t\t\t\tif (BN_ucmp(A,T) < 0)\n\t\t\t\t\t{\n\t\t\t\t\tif (!BN_one(D)) goto err;\n\t\t\t\t\tif (!BN_sub(M,A,B)) goto err;\n\t\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t\t{\n\t\t\t\t\tif (!BN_sub(M,A,T)) goto err;\n\t\t\t\t\tif (!BN_add(D,T,B)) goto err;\n\t\t\t\t\tif (BN_ucmp(A,D) < 0)\n\t\t\t\t\t\t{\n\t\t\t\t\t\tif (!BN_set_word(D,2)) goto err;\n\t\t\t\t\t\t}\n\t\t\t\t\telse\n\t\t\t\t\t\t{\n\t\t\t\t\t\tif (!BN_set_word(D,3)) goto err;\n\t\t\t\t\t\tif (!BN_sub(M,M,B)) goto err;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\tif (!BN_div(D,M,A,B,ctx)) goto err;\n\t\t\t\t}\n\t\t\ttmp=A;\n\t\t\tA=B;\n\t\t\tB=M;\n\t\t\tif (BN_is_one(D))\n\t\t\t\t{\n\t\t\t\tif (!BN_add(tmp,X,Y)) goto err;\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\tif (BN_is_word(D,2))\n\t\t\t\t\t{\n\t\t\t\t\tif (!BN_lshift1(tmp,X)) goto err;\n\t\t\t\t\t}\n\t\t\t\telse if (BN_is_word(D,4))\n\t\t\t\t\t{\n\t\t\t\t\tif (!BN_lshift(tmp,X,2)) goto err;\n\t\t\t\t\t}\n\t\t\t\telse if (D->top == 1)\n\t\t\t\t\t{\n\t\t\t\t\tif (!BN_copy(tmp,X)) goto err;\n\t\t\t\t\tif (!BN_mul_word(tmp,D->d[0])) goto err;\n\t\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t\t{\n\t\t\t\t\tif (!BN_mul(tmp,D,X,ctx)) goto err;\n\t\t\t\t\t}\n\t\t\t\tif (!BN_add(tmp,tmp,Y)) goto err;\n\t\t\t\t}\n\t\t\tM=Y;\n\t\t\tY=X;\n\t\t\tX=tmp;\n\t\t\tsign = -sign;\n\t\t\t}\n\t\t}\n\tif (sign < 0)\n\t\t{\n\t\tif (!BN_sub(Y,n,Y)) goto err;\n\t\t}\n\tif (BN_is_one(A))\n\t\t{\n\t\tif (!Y->neg && BN_ucmp(Y,n) < 0)\n\t\t\t{\n\t\t\tif (!BN_copy(R,Y)) goto err;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tif (!BN_nnmod(R,Y,n,ctx)) goto err;\n\t\t\t}\n\t\t}\n\telse\n\t\t{\n\t\tif (pnoinv)\n\t\t\t*pnoinv = 1;\n\t\tgoto err;\n\t\t}\n\tret=R;\nerr:\n\tif ((ret == NULL) && (in == NULL)) BN_free(R);\n\tBN_CTX_end(ctx);\n\tbn_check_top(ret);\n\treturn(ret);\n\t}', 'static BIGNUM *BN_mod_inverse_no_branch(BIGNUM *in,\n\tconst BIGNUM *a, const BIGNUM *n, BN_CTX *ctx)\n\t{\n\tBIGNUM *A,*B,*X,*Y,*M,*D,*T,*R=NULL;\n\tBIGNUM local_A, local_B;\n\tBIGNUM *pA, *pB;\n\tBIGNUM *ret=NULL;\n\tint sign;\n\tbn_check_top(a);\n\tbn_check_top(n);\n\tBN_CTX_start(ctx);\n\tA = BN_CTX_get(ctx);\n\tB = BN_CTX_get(ctx);\n\tX = BN_CTX_get(ctx);\n\tD = BN_CTX_get(ctx);\n\tM = BN_CTX_get(ctx);\n\tY = BN_CTX_get(ctx);\n\tT = BN_CTX_get(ctx);\n\tif (T == NULL) goto err;\n\tif (in == NULL)\n\t\tR=BN_new();\n\telse\n\t\tR=in;\n\tif (R == NULL) goto err;\n\tBN_one(X);\n\tBN_zero(Y);\n\tif (BN_copy(B,a) == NULL) goto err;\n\tif (BN_copy(A,n) == NULL) goto err;\n\tA->neg = 0;\n\tif (B->neg || (BN_ucmp(B, A) >= 0))\n\t\t{\n\t\tpB = &local_B;\n\t\tBN_with_flags(pB, B, BN_FLG_CONSTTIME);\n\t\tif (!BN_nnmod(B, pB, A, ctx)) goto err;\n\t\t}\n\tsign = -1;\n\twhile (!BN_is_zero(B))\n\t\t{\n\t\tBIGNUM *tmp;\n\t\tpA = &local_A;\n\t\tBN_with_flags(pA, A, BN_FLG_CONSTTIME);\n\t\tif (!BN_div(D,M,pA,B,ctx)) goto err;\n\t\ttmp=A;\n\t\tA=B;\n\t\tB=M;\n\t\tif (!BN_mul(tmp,D,X,ctx)) goto err;\n\t\tif (!BN_add(tmp,tmp,Y)) goto err;\n\t\tM=Y;\n\t\tY=X;\n\t\tX=tmp;\n\t\tsign = -sign;\n\t\t}\n\tif (sign < 0)\n\t\t{\n\t\tif (!BN_sub(Y,n,Y)) goto err;\n\t\t}\n\tif (BN_is_one(A))\n\t\t{\n\t\tif (!Y->neg && BN_ucmp(Y,n) < 0)\n\t\t\t{\n\t\t\tif (!BN_copy(R,Y)) goto err;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tif (!BN_nnmod(R,Y,n,ctx)) goto err;\n\t\t\t}\n\t\t}\n\telse\n\t\t{\n\t\tBNerr(BN_F_BN_MOD_INVERSE_NO_BRANCH,BN_R_NO_INVERSE);\n\t\tgoto err;\n\t\t}\n\tret=R;\nerr:\n\tif ((ret == NULL) && (in == NULL)) BN_free(R);\n\tBN_CTX_end(ctx);\n\tbn_check_top(ret);\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 (num->top > 0 && num->d[num->top - 1] == 0)\n\t\t{\n\t\tBNerr(BN_F_BN_DIV,BN_R_NOT_INITIALIZED);\n\t\treturn 0;\n\t\t}\n\tbn_check_top(num);\n\tif ((BN_get_flags(num, BN_FLG_CONSTTIME) != 0) || (BN_get_flags(divisor, BN_FLG_CONSTTIME) != 0))\n\t\t{\n\t\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(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 || tmp == NULL || snum == NULL)\n\t\tgoto err;\n\tnorm_shift=BN_BITS2-((BN_num_bits(divisor))%BN_BITS2);\n\tif (!(BN_lshift(sdiv,divisor,norm_shift))) goto err;\n\tsdiv->neg=0;\n\tnorm_shift+=BN_BITS2;\n\tif (!(BN_lshift(snum,num,norm_shift))) goto err;\n\tsnum->neg=0;\n\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;\n\t\t\tq=bn_div_words(n0,n1,d0);\n#ifdef BN_DEBUG_LEVITTE\n\t\t\tfprintf(stderr,"DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\\\nX) -> 0x%08X\\n",\n\t\t\t\tn0, n1, d0, q);\n#endif\n#ifndef REMAINDER_IS_ALREADY_CALCULATED\n\t\t\trem=(n1-q*d0)&BN_MASK2;\n#endif\n#if defined(BN_UMULT_LOHI)\n\t\t\tBN_UMULT_LOHI(t2l,t2h,d1,q);\n#elif defined(BN_UMULT_HIGH)\n\t\t\tt2l = d1 * q;\n\t\t\tt2h = BN_UMULT_HIGH(d1,q);\n#else\n\t\t\t{\n\t\t\tBN_ULONG ql, qh;\n\t\t\tt2l=LBITS(d1); t2h=HBITS(d1);\n\t\t\tql =LBITS(q); qh =HBITS(q);\n\t\t\tmul64(t2l,t2h,ql,qh);\n\t\t\t}\n#endif\n\t\t\tfor (;;)\n\t\t\t\t{\n\t\t\t\tif ((t2h < rem) ||\n\t\t\t\t\t((t2h == rem) && (t2l <= wnump[-2])))\n\t\t\t\t\tbreak;\n\t\t\t\tq--;\n\t\t\t\trem += d0;\n\t\t\t\tif (rem < d0) break;\n\t\t\t\tif (t2l < d1) t2h--; t2l -= d1;\n\t\t\t\t}\n#endif\n\t\t\t}\n#endif\n\t\tl0=bn_mul_words(tmp->d,sdiv->d,div_n,q);\n\t\ttmp->d[div_n]=l0;\n\t\twnum.d--;\n\t\tif (bn_sub_words(wnum.d, wnum.d, tmp->d, div_n+1))\n\t\t\t{\n\t\t\tq--;\n\t\t\tif (bn_add_words(wnum.d, wnum.d, sdiv->d, div_n))\n\t\t\t\t(*wnump)++;\n\t\t\t}\n\t\t*resp = q;\n\t\t}\n\tbn_correct_top(snum);\n\tif (rm != NULL)\n\t\t{\n\t\tint neg = num->neg;\n\t\tBN_rshift(rm,snum,norm_shift);\n\t\tif (!BN_is_zero(rm))\n\t\t\trm->neg = neg;\n\t\tbn_check_top(rm);\n\t\t}\n\tBN_CTX_end(ctx);\n\treturn(1);\nerr:\n\tbn_check_top(rm);\n\tBN_CTX_end(ctx);\n\treturn(0);\n\t}', 'static int BN_div_no_branch(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num,\n\tconst BIGNUM *divisor, 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\tbn_check_top(dv);\n\tbn_check_top(rm);\n\tbn_check_top(divisor);\n\tif (BN_is_zero(divisor))\n\t\t{\n\t\tBNerr(BN_F_BN_DIV_NO_BRANCH,BN_R_DIV_BY_ZERO);\n\t\treturn(0);\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\tif (snum->top <= sdiv->top+1)\n\t\t{\n\t\tif (bn_wexpand(snum, sdiv->top + 2) == NULL) goto err;\n\t\tfor (i = snum->top; i < sdiv->top + 2; i++) snum->d[i] = 0;\n\t\tsnum->top = sdiv->top + 2;\n\t\t}\n\telse\n\t\t{\n\t\tif (bn_wexpand(snum, snum->top + 1) == NULL) goto err;\n\t\tsnum->d[snum->top] = 0;\n\t\tsnum->top ++;\n\t\t}\n\tdiv_n=sdiv->top;\n\tnum_n=snum->top;\n\tloop=num_n-div_n;\n\twnum.neg = 0;\n\twnum.d = &(snum->d[loop]);\n\twnum.top = div_n;\n\twnum.dmax = snum->dmax - loop;\n\td0=sdiv->d[div_n-1];\n\td1=(div_n == 1)?0:sdiv->d[div_n-2];\n\twnump= &(snum->d[num_n-1]);\n\tres->neg= (num->neg^divisor->neg);\n\tif (!bn_wexpand(res,(loop+1))) goto err;\n\tres->top=loop-1;\n\tresp= &(res->d[loop-1]);\n\tif (!bn_wexpand(tmp,(div_n+1))) goto err;\n\tif (res->top == 0)\n\t\tres->neg = 0;\n\telse\n\t\tresp--;\n\tfor (i=0; i<loop-1; i++, wnump--, resp--)\n\t\t{\n\t\tBN_ULONG q,l0;\n#if defined(BN_DIV3W) && !defined(OPENSSL_NO_ASM)\n\t\tBN_ULONG bn_div_3_words(BN_ULONG*,BN_ULONG,BN_ULONG);\n\t\tq=bn_div_3_words(wnump,d1,d0);\n#else\n\t\tBN_ULONG n0,n1,rem=0;\n\t\tn0=wnump[0];\n\t\tn1=wnump[-1];\n\t\tif (n0 == d0)\n\t\t\tq=BN_MASK2;\n\t\telse\n\t\t\t{\n#ifdef BN_LLONG\n\t\t\tBN_ULLONG t2;\n#if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)\n\t\t\tq=(BN_ULONG)(((((BN_ULLONG)n0)<<BN_BITS2)|n1)/d0);\n#else\n\t\t\tq=bn_div_words(n0,n1,d0);\n#ifdef BN_DEBUG_LEVITTE\n\t\t\tfprintf(stderr,"DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\\\nX) -> 0x%08X\\n",\n\t\t\t\tn0, n1, d0, q);\n#endif\n#endif\n#ifndef REMAINDER_IS_ALREADY_CALCULATED\n\t\t\trem=(n1-q*d0)&BN_MASK2;\n#endif\n\t\t\tt2=(BN_ULLONG)d1*q;\n\t\t\tfor (;;)\n\t\t\t\t{\n\t\t\t\tif (t2 <= ((((BN_ULLONG)rem)<<BN_BITS2)|wnump[-2]))\n\t\t\t\t\tbreak;\n\t\t\t\tq--;\n\t\t\t\trem += d0;\n\t\t\t\tif (rem < d0) break;\n\t\t\t\tt2 -= d1;\n\t\t\t\t}\n#else\n\t\t\tBN_ULONG t2l,t2h;\n\t\t\tq=bn_div_words(n0,n1,d0);\n#ifdef BN_DEBUG_LEVITTE\n\t\t\tfprintf(stderr,"DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\\\nX) -> 0x%08X\\n",\n\t\t\t\tn0, n1, d0, q);\n#endif\n#ifndef REMAINDER_IS_ALREADY_CALCULATED\n\t\t\trem=(n1-q*d0)&BN_MASK2;\n#endif\n#if defined(BN_UMULT_LOHI)\n\t\t\tBN_UMULT_LOHI(t2l,t2h,d1,q);\n#elif defined(BN_UMULT_HIGH)\n\t\t\tt2l = d1 * q;\n\t\t\tt2h = BN_UMULT_HIGH(d1,q);\n#else\n\t\t\t{\n\t\t\tBN_ULONG ql, qh;\n\t\t\tt2l=LBITS(d1); t2h=HBITS(d1);\n\t\t\tql =LBITS(q); qh =HBITS(q);\n\t\t\tmul64(t2l,t2h,ql,qh);\n\t\t\t}\n#endif\n\t\t\tfor (;;)\n\t\t\t\t{\n\t\t\t\tif ((t2h < rem) ||\n\t\t\t\t\t((t2h == rem) && (t2l <= wnump[-2])))\n\t\t\t\t\tbreak;\n\t\t\t\tq--;\n\t\t\t\trem += d0;\n\t\t\t\tif (rem < d0) break;\n\t\t\t\tif (t2l < d1) t2h--; t2l -= d1;\n\t\t\t\t}\n#endif\n\t\t\t}\n#endif\n\t\tl0=bn_mul_words(tmp->d,sdiv->d,div_n,q);\n\t\ttmp->d[div_n]=l0;\n\t\twnum.d--;\n\t\tif (bn_sub_words(wnum.d, wnum.d, tmp->d, div_n+1))\n\t\t\t{\n\t\t\tq--;\n\t\t\tif (bn_add_words(wnum.d, wnum.d, sdiv->d, div_n))\n\t\t\t\t(*wnump)++;\n\t\t\t}\n\t\t*resp = q;\n\t\t}\n\tbn_correct_top(snum);\n\tif (rm != NULL)\n\t\t{\n\t\tint neg = num->neg;\n\t\tBN_rshift(rm,snum,norm_shift);\n\t\tif (!BN_is_zero(rm))\n\t\t\trm->neg = neg;\n\t\tbn_check_top(rm);\n\t\t}\n\tbn_correct_top(res);\n\tBN_CTX_end(ctx);\n\treturn(1);\nerr:\n\tbn_check_top(rm);\n\tBN_CTX_end(ctx);\n\treturn(0);\n\t}', 'void BN_CTX_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}'] |
35,127 | 0 | https://github.com/libav/libav/blob/3ec394ea823c2a6e65a6abbdb2041ce1c66964f8/libavcodec/motion_est_template.c/#L724 | 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}'] |
35,128 | 0 | https://github.com/libav/libav/blob/fc322d6a70189da24dbd445c710bb214eb031ce7/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;
} | ['int ff_mlp_read_major_sync(void *log, MLPHeaderInfo *mh, BitstreamContext *bc)\n{\n int ratebits, channel_arrangement, header_size;\n uint16_t checksum;\n assert(bitstream_tell(bc) == 0);\n header_size = ff_mlp_get_major_sync_size(bc->buffer, bc->size_in_bits >> 3);\n if (header_size < 0 || bc->size_in_bits < header_size << 3) {\n av_log(log, AV_LOG_ERROR, "packet too short, unable to read major sync\\n");\n return -1;\n }\n checksum = ff_mlp_checksum16(bc->buffer, header_size - 2);\n if (checksum != AV_RL16(bc->buffer + header_size - 2)) {\n av_log(log, AV_LOG_ERROR, "major sync info header checksum error\\n");\n return AVERROR_INVALIDDATA;\n }\n if (bitstream_read(bc, 24) != 0xf8726f)\n return AVERROR_INVALIDDATA;\n mh->stream_type = bitstream_read(bc, 8);\n mh->header_size = header_size;\n if (mh->stream_type == 0xbb) {\n mh->group1_bits = mlp_quants[bitstream_read(bc, 4)];\n mh->group2_bits = mlp_quants[bitstream_read(bc, 4)];\n ratebits = bitstream_read(bc, 4);\n mh->group1_samplerate = mlp_samplerate(ratebits);\n mh->group2_samplerate = mlp_samplerate(bitstream_read(bc, 4));\n bitstream_skip(bc, 11);\n channel_arrangement = bitstream_read(bc, 5);\n mh->channels_mlp = mlp_channels[channel_arrangement];\n mh->channel_layout_mlp = mlp_layout[channel_arrangement];\n } else if (mh->stream_type == 0xba) {\n mh->group1_bits = 24;\n mh->group2_bits = 0;\n ratebits = bitstream_read(bc, 4);\n mh->group1_samplerate = mlp_samplerate(ratebits);\n mh->group2_samplerate = 0;\n bitstream_skip(bc, 4);\n mh->channel_modifier_thd_stream0 = bitstream_read(bc, 2);\n mh->channel_modifier_thd_stream1 = bitstream_read(bc, 2);\n channel_arrangement = bitstream_read(bc, 5);\n mh->channels_thd_stream1 = truehd_channels(channel_arrangement);\n mh->channel_layout_thd_stream1 = truehd_layout(channel_arrangement);\n mh->channel_modifier_thd_stream2 = bitstream_read(bc, 2);\n channel_arrangement = bitstream_read(bc, 13);\n mh->channels_thd_stream2 = truehd_channels(channel_arrangement);\n mh->channel_layout_thd_stream2 = truehd_layout(channel_arrangement);\n } else\n return AVERROR_INVALIDDATA;\n mh->access_unit_size = 40 << (ratebits & 7);\n mh->access_unit_size_pow2 = 64 << (ratebits & 7);\n bitstream_skip(bc, 48);\n mh->is_vbr = bitstream_read_bit(bc);\n mh->peak_bitrate = (bitstream_read(bc, 15) * mh->group1_samplerate + 8) >> 4;\n mh->num_substreams = bitstream_read(bc, 4);\n bitstream_skip(bc, 4 + (header_size - 17) * 8);\n return 0;\n}', 'static inline uint32_t bitstream_read(BitstreamContext *bc, unsigned n)\n{\n if (!n)\n return 0;\n if (n > bc->bits_left) {\n refill_32(bc);\n if (bc->bits_left < 32)\n bc->bits_left = n;\n }\n return get_val(bc, n);\n}', 'static inline uint64_t get_val(BitstreamContext *bc, unsigned n)\n{\n#ifdef BITSTREAM_READER_LE\n uint64_t ret = bc->bits & ((UINT64_C(1) << n) - 1);\n bc->bits >>= n;\n#else\n uint64_t ret = bc->bits >> (64 - n);\n bc->bits <<= n;\n#endif\n bc->bits_left -= n;\n return ret;\n}'] |
35,129 | 0 | https://github.com/nginx/nginx/blob/70f7141074896fb1ff3e5fc08407ea0f64f2076b/src/core/ngx_sha1.c/#L224 | static const u_char *
ngx_sha1_body(ngx_sha1_t *ctx, const u_char *data, size_t size)
{
uint32_t a, b, c, d, e, temp;
uint32_t saved_a, saved_b, saved_c, saved_d, saved_e;
uint32_t words[80];
ngx_uint_t i;
const u_char *p;
p = data;
a = ctx->a;
b = ctx->b;
c = ctx->c;
d = ctx->d;
e = ctx->e;
do {
saved_a = a;
saved_b = b;
saved_c = c;
saved_d = d;
saved_e = e;
for (i = 0; i < 16; i++) {
words[i] = GET(i);
}
for (i = 16; i < 80; i++) {
words[i] = ROTATE(1, words[i - 3] ^ words[i - 8] ^ words[i - 14]
^ words[i - 16]);
}
STEP(F1, a, b, c, d, e, words[0], 0x5a827999);
STEP(F1, a, b, c, d, e, words[1], 0x5a827999);
STEP(F1, a, b, c, d, e, words[2], 0x5a827999);
STEP(F1, a, b, c, d, e, words[3], 0x5a827999);
STEP(F1, a, b, c, d, e, words[4], 0x5a827999);
STEP(F1, a, b, c, d, e, words[5], 0x5a827999);
STEP(F1, a, b, c, d, e, words[6], 0x5a827999);
STEP(F1, a, b, c, d, e, words[7], 0x5a827999);
STEP(F1, a, b, c, d, e, words[8], 0x5a827999);
STEP(F1, a, b, c, d, e, words[9], 0x5a827999);
STEP(F1, a, b, c, d, e, words[10], 0x5a827999);
STEP(F1, a, b, c, d, e, words[11], 0x5a827999);
STEP(F1, a, b, c, d, e, words[12], 0x5a827999);
STEP(F1, a, b, c, d, e, words[13], 0x5a827999);
STEP(F1, a, b, c, d, e, words[14], 0x5a827999);
STEP(F1, a, b, c, d, e, words[15], 0x5a827999);
STEP(F1, a, b, c, d, e, words[16], 0x5a827999);
STEP(F1, a, b, c, d, e, words[17], 0x5a827999);
STEP(F1, a, b, c, d, e, words[18], 0x5a827999);
STEP(F1, a, b, c, d, e, words[19], 0x5a827999);
STEP(F2, a, b, c, d, e, words[20], 0x6ed9eba1);
STEP(F2, a, b, c, d, e, words[21], 0x6ed9eba1);
STEP(F2, a, b, c, d, e, words[22], 0x6ed9eba1);
STEP(F2, a, b, c, d, e, words[23], 0x6ed9eba1);
STEP(F2, a, b, c, d, e, words[24], 0x6ed9eba1);
STEP(F2, a, b, c, d, e, words[25], 0x6ed9eba1);
STEP(F2, a, b, c, d, e, words[26], 0x6ed9eba1);
STEP(F2, a, b, c, d, e, words[27], 0x6ed9eba1);
STEP(F2, a, b, c, d, e, words[28], 0x6ed9eba1);
STEP(F2, a, b, c, d, e, words[29], 0x6ed9eba1);
STEP(F2, a, b, c, d, e, words[30], 0x6ed9eba1);
STEP(F2, a, b, c, d, e, words[31], 0x6ed9eba1);
STEP(F2, a, b, c, d, e, words[32], 0x6ed9eba1);
STEP(F2, a, b, c, d, e, words[33], 0x6ed9eba1);
STEP(F2, a, b, c, d, e, words[34], 0x6ed9eba1);
STEP(F2, a, b, c, d, e, words[35], 0x6ed9eba1);
STEP(F2, a, b, c, d, e, words[36], 0x6ed9eba1);
STEP(F2, a, b, c, d, e, words[37], 0x6ed9eba1);
STEP(F2, a, b, c, d, e, words[38], 0x6ed9eba1);
STEP(F2, a, b, c, d, e, words[39], 0x6ed9eba1);
STEP(F3, a, b, c, d, e, words[40], 0x8f1bbcdc);
STEP(F3, a, b, c, d, e, words[41], 0x8f1bbcdc);
STEP(F3, a, b, c, d, e, words[42], 0x8f1bbcdc);
STEP(F3, a, b, c, d, e, words[43], 0x8f1bbcdc);
STEP(F3, a, b, c, d, e, words[44], 0x8f1bbcdc);
STEP(F3, a, b, c, d, e, words[45], 0x8f1bbcdc);
STEP(F3, a, b, c, d, e, words[46], 0x8f1bbcdc);
STEP(F3, a, b, c, d, e, words[47], 0x8f1bbcdc);
STEP(F3, a, b, c, d, e, words[48], 0x8f1bbcdc);
STEP(F3, a, b, c, d, e, words[49], 0x8f1bbcdc);
STEP(F3, a, b, c, d, e, words[50], 0x8f1bbcdc);
STEP(F3, a, b, c, d, e, words[51], 0x8f1bbcdc);
STEP(F3, a, b, c, d, e, words[52], 0x8f1bbcdc);
STEP(F3, a, b, c, d, e, words[53], 0x8f1bbcdc);
STEP(F3, a, b, c, d, e, words[54], 0x8f1bbcdc);
STEP(F3, a, b, c, d, e, words[55], 0x8f1bbcdc);
STEP(F3, a, b, c, d, e, words[56], 0x8f1bbcdc);
STEP(F3, a, b, c, d, e, words[57], 0x8f1bbcdc);
STEP(F3, a, b, c, d, e, words[58], 0x8f1bbcdc);
STEP(F3, a, b, c, d, e, words[59], 0x8f1bbcdc);
STEP(F2, a, b, c, d, e, words[60], 0xca62c1d6);
STEP(F2, a, b, c, d, e, words[61], 0xca62c1d6);
STEP(F2, a, b, c, d, e, words[62], 0xca62c1d6);
STEP(F2, a, b, c, d, e, words[63], 0xca62c1d6);
STEP(F2, a, b, c, d, e, words[64], 0xca62c1d6);
STEP(F2, a, b, c, d, e, words[65], 0xca62c1d6);
STEP(F2, a, b, c, d, e, words[66], 0xca62c1d6);
STEP(F2, a, b, c, d, e, words[67], 0xca62c1d6);
STEP(F2, a, b, c, d, e, words[68], 0xca62c1d6);
STEP(F2, a, b, c, d, e, words[69], 0xca62c1d6);
STEP(F2, a, b, c, d, e, words[70], 0xca62c1d6);
STEP(F2, a, b, c, d, e, words[71], 0xca62c1d6);
STEP(F2, a, b, c, d, e, words[72], 0xca62c1d6);
STEP(F2, a, b, c, d, e, words[73], 0xca62c1d6);
STEP(F2, a, b, c, d, e, words[74], 0xca62c1d6);
STEP(F2, a, b, c, d, e, words[75], 0xca62c1d6);
STEP(F2, a, b, c, d, e, words[76], 0xca62c1d6);
STEP(F2, a, b, c, d, e, words[77], 0xca62c1d6);
STEP(F2, a, b, c, d, e, words[78], 0xca62c1d6);
STEP(F2, a, b, c, d, e, words[79], 0xca62c1d6);
a += saved_a;
b += saved_b;
c += saved_c;
d += saved_d;
e += saved_e;
p += 64;
} while (size -= 64);
ctx->a = a;
ctx->b = b;
ctx->c = c;
ctx->d = d;
ctx->e = e;
return p;
} | ['static const u_char *\nngx_sha1_body(ngx_sha1_t *ctx, const u_char *data, size_t size)\n{\n uint32_t a, b, c, d, e, temp;\n uint32_t saved_a, saved_b, saved_c, saved_d, saved_e;\n uint32_t words[80];\n ngx_uint_t i;\n const u_char *p;\n p = data;\n a = ctx->a;\n b = ctx->b;\n c = ctx->c;\n d = ctx->d;\n e = ctx->e;\n do {\n saved_a = a;\n saved_b = b;\n saved_c = c;\n saved_d = d;\n saved_e = e;\n for (i = 0; i < 16; i++) {\n words[i] = GET(i);\n }\n for (i = 16; i < 80; i++) {\n words[i] = ROTATE(1, words[i - 3] ^ words[i - 8] ^ words[i - 14]\n ^ words[i - 16]);\n }\n STEP(F1, a, b, c, d, e, words[0], 0x5a827999);\n STEP(F1, a, b, c, d, e, words[1], 0x5a827999);\n STEP(F1, a, b, c, d, e, words[2], 0x5a827999);\n STEP(F1, a, b, c, d, e, words[3], 0x5a827999);\n STEP(F1, a, b, c, d, e, words[4], 0x5a827999);\n STEP(F1, a, b, c, d, e, words[5], 0x5a827999);\n STEP(F1, a, b, c, d, e, words[6], 0x5a827999);\n STEP(F1, a, b, c, d, e, words[7], 0x5a827999);\n STEP(F1, a, b, c, d, e, words[8], 0x5a827999);\n STEP(F1, a, b, c, d, e, words[9], 0x5a827999);\n STEP(F1, a, b, c, d, e, words[10], 0x5a827999);\n STEP(F1, a, b, c, d, e, words[11], 0x5a827999);\n STEP(F1, a, b, c, d, e, words[12], 0x5a827999);\n STEP(F1, a, b, c, d, e, words[13], 0x5a827999);\n STEP(F1, a, b, c, d, e, words[14], 0x5a827999);\n STEP(F1, a, b, c, d, e, words[15], 0x5a827999);\n STEP(F1, a, b, c, d, e, words[16], 0x5a827999);\n STEP(F1, a, b, c, d, e, words[17], 0x5a827999);\n STEP(F1, a, b, c, d, e, words[18], 0x5a827999);\n STEP(F1, a, b, c, d, e, words[19], 0x5a827999);\n STEP(F2, a, b, c, d, e, words[20], 0x6ed9eba1);\n STEP(F2, a, b, c, d, e, words[21], 0x6ed9eba1);\n STEP(F2, a, b, c, d, e, words[22], 0x6ed9eba1);\n STEP(F2, a, b, c, d, e, words[23], 0x6ed9eba1);\n STEP(F2, a, b, c, d, e, words[24], 0x6ed9eba1);\n STEP(F2, a, b, c, d, e, words[25], 0x6ed9eba1);\n STEP(F2, a, b, c, d, e, words[26], 0x6ed9eba1);\n STEP(F2, a, b, c, d, e, words[27], 0x6ed9eba1);\n STEP(F2, a, b, c, d, e, words[28], 0x6ed9eba1);\n STEP(F2, a, b, c, d, e, words[29], 0x6ed9eba1);\n STEP(F2, a, b, c, d, e, words[30], 0x6ed9eba1);\n STEP(F2, a, b, c, d, e, words[31], 0x6ed9eba1);\n STEP(F2, a, b, c, d, e, words[32], 0x6ed9eba1);\n STEP(F2, a, b, c, d, e, words[33], 0x6ed9eba1);\n STEP(F2, a, b, c, d, e, words[34], 0x6ed9eba1);\n STEP(F2, a, b, c, d, e, words[35], 0x6ed9eba1);\n STEP(F2, a, b, c, d, e, words[36], 0x6ed9eba1);\n STEP(F2, a, b, c, d, e, words[37], 0x6ed9eba1);\n STEP(F2, a, b, c, d, e, words[38], 0x6ed9eba1);\n STEP(F2, a, b, c, d, e, words[39], 0x6ed9eba1);\n STEP(F3, a, b, c, d, e, words[40], 0x8f1bbcdc);\n STEP(F3, a, b, c, d, e, words[41], 0x8f1bbcdc);\n STEP(F3, a, b, c, d, e, words[42], 0x8f1bbcdc);\n STEP(F3, a, b, c, d, e, words[43], 0x8f1bbcdc);\n STEP(F3, a, b, c, d, e, words[44], 0x8f1bbcdc);\n STEP(F3, a, b, c, d, e, words[45], 0x8f1bbcdc);\n STEP(F3, a, b, c, d, e, words[46], 0x8f1bbcdc);\n STEP(F3, a, b, c, d, e, words[47], 0x8f1bbcdc);\n STEP(F3, a, b, c, d, e, words[48], 0x8f1bbcdc);\n STEP(F3, a, b, c, d, e, words[49], 0x8f1bbcdc);\n STEP(F3, a, b, c, d, e, words[50], 0x8f1bbcdc);\n STEP(F3, a, b, c, d, e, words[51], 0x8f1bbcdc);\n STEP(F3, a, b, c, d, e, words[52], 0x8f1bbcdc);\n STEP(F3, a, b, c, d, e, words[53], 0x8f1bbcdc);\n STEP(F3, a, b, c, d, e, words[54], 0x8f1bbcdc);\n STEP(F3, a, b, c, d, e, words[55], 0x8f1bbcdc);\n STEP(F3, a, b, c, d, e, words[56], 0x8f1bbcdc);\n STEP(F3, a, b, c, d, e, words[57], 0x8f1bbcdc);\n STEP(F3, a, b, c, d, e, words[58], 0x8f1bbcdc);\n STEP(F3, a, b, c, d, e, words[59], 0x8f1bbcdc);\n STEP(F2, a, b, c, d, e, words[60], 0xca62c1d6);\n STEP(F2, a, b, c, d, e, words[61], 0xca62c1d6);\n STEP(F2, a, b, c, d, e, words[62], 0xca62c1d6);\n STEP(F2, a, b, c, d, e, words[63], 0xca62c1d6);\n STEP(F2, a, b, c, d, e, words[64], 0xca62c1d6);\n STEP(F2, a, b, c, d, e, words[65], 0xca62c1d6);\n STEP(F2, a, b, c, d, e, words[66], 0xca62c1d6);\n STEP(F2, a, b, c, d, e, words[67], 0xca62c1d6);\n STEP(F2, a, b, c, d, e, words[68], 0xca62c1d6);\n STEP(F2, a, b, c, d, e, words[69], 0xca62c1d6);\n STEP(F2, a, b, c, d, e, words[70], 0xca62c1d6);\n STEP(F2, a, b, c, d, e, words[71], 0xca62c1d6);\n STEP(F2, a, b, c, d, e, words[72], 0xca62c1d6);\n STEP(F2, a, b, c, d, e, words[73], 0xca62c1d6);\n STEP(F2, a, b, c, d, e, words[74], 0xca62c1d6);\n STEP(F2, a, b, c, d, e, words[75], 0xca62c1d6);\n STEP(F2, a, b, c, d, e, words[76], 0xca62c1d6);\n STEP(F2, a, b, c, d, e, words[77], 0xca62c1d6);\n STEP(F2, a, b, c, d, e, words[78], 0xca62c1d6);\n STEP(F2, a, b, c, d, e, words[79], 0xca62c1d6);\n a += saved_a;\n b += saved_b;\n c += saved_c;\n d += saved_d;\n e += saved_e;\n p += 64;\n } while (size -= 64);\n ctx->a = a;\n ctx->b = b;\n ctx->c = c;\n ctx->d = d;\n ctx->e = e;\n return p;\n}'] |
35,130 | 0 | https://github.com/openssl/openssl/blob/9c46f4b9cd4912b61cb546c48b678488d7f26ed6/crypto/sha/sha512.c/#L203 | int SHA512_Update(SHA512_CTX *c, const void *_data, size_t len)
{
SHA_LONG64 l;
unsigned char *p = c->u.p;
const unsigned char *data = (const unsigned char *)_data;
if (len == 0)
return 1;
l = (c->Nl + (((SHA_LONG64) len) << 3)) & U64(0xffffffffffffffff);
if (l < c->Nl)
c->Nh++;
if (sizeof(len) >= 8)
c->Nh += (((SHA_LONG64) len) >> 61);
c->Nl = l;
if (c->num != 0) {
size_t n = sizeof(c->u) - c->num;
if (len < n) {
memcpy(p + c->num, data, len), c->num += (unsigned int)len;
return 1;
} else {
memcpy(p + c->num, data, n), c->num = 0;
len -= n, data += n;
sha512_block_data_order(c, p, 1);
}
}
if (len >= sizeof(c->u)) {
# ifndef SHA512_BLOCK_CAN_MANAGE_UNALIGNED_DATA
if ((size_t)data % sizeof(c->u.d[0]) != 0)
while (len >= sizeof(c->u))
memcpy(p, data, sizeof(c->u)),
sha512_block_data_order(c, p, 1),
len -= sizeof(c->u), data += sizeof(c->u);
else
# endif
sha512_block_data_order(c, data, len / sizeof(c->u)),
data += len, len %= sizeof(c->u), data -= len;
}
if (len != 0)
memcpy(p, data, len), c->num = (int)len;
return 1;
} | ['int BN_generate_dsa_nonce(BIGNUM *out, const BIGNUM *range,\n const BIGNUM *priv, const unsigned char *message,\n size_t message_len, BN_CTX *ctx)\n{\n SHA512_CTX sha;\n unsigned char random_bytes[64];\n unsigned char digest[SHA512_DIGEST_LENGTH];\n unsigned done, todo;\n const unsigned num_k_bytes = BN_num_bytes(range) + 8;\n unsigned char private_bytes[96];\n unsigned char *k_bytes;\n int ret = 0;\n k_bytes = OPENSSL_malloc(num_k_bytes);\n if (!k_bytes)\n goto err;\n todo = sizeof(priv->d[0]) * priv->top;\n if (todo > sizeof(private_bytes)) {\n BNerr(BN_F_BN_GENERATE_DSA_NONCE, BN_R_PRIVATE_KEY_TOO_LARGE);\n goto err;\n }\n memcpy(private_bytes, priv->d, todo);\n memset(private_bytes + todo, 0, sizeof(private_bytes) - todo);\n for (done = 0; done < num_k_bytes;) {\n if (RAND_bytes(random_bytes, sizeof(random_bytes)) != 1)\n goto err;\n SHA512_Init(&sha);\n SHA512_Update(&sha, &done, sizeof(done));\n SHA512_Update(&sha, private_bytes, sizeof(private_bytes));\n SHA512_Update(&sha, message, message_len);\n SHA512_Update(&sha, random_bytes, sizeof(random_bytes));\n SHA512_Final(digest, &sha);\n todo = num_k_bytes - done;\n if (todo > SHA512_DIGEST_LENGTH)\n todo = SHA512_DIGEST_LENGTH;\n memcpy(k_bytes + done, digest, todo);\n done += todo;\n }\n if (!BN_bin2bn(k_bytes, num_k_bytes, out))\n goto err;\n if (BN_mod(out, out, range, ctx) != 1)\n goto err;\n ret = 1;\n err:\n if (k_bytes)\n OPENSSL_free(k_bytes);\n return ret;\n}', 'int SHA512_Init(SHA512_CTX *c)\n{\n c->h[0] = U64(0x6a09e667f3bcc908);\n c->h[1] = U64(0xbb67ae8584caa73b);\n c->h[2] = U64(0x3c6ef372fe94f82b);\n c->h[3] = U64(0xa54ff53a5f1d36f1);\n c->h[4] = U64(0x510e527fade682d1);\n c->h[5] = U64(0x9b05688c2b3e6c1f);\n c->h[6] = U64(0x1f83d9abfb41bd6b);\n c->h[7] = U64(0x5be0cd19137e2179);\n c->Nl = 0;\n c->Nh = 0;\n c->num = 0;\n c->md_len = SHA512_DIGEST_LENGTH;\n return 1;\n}', 'int SHA512_Update(SHA512_CTX *c, const void *_data, size_t len)\n{\n SHA_LONG64 l;\n unsigned char *p = c->u.p;\n const unsigned char *data = (const unsigned char *)_data;\n if (len == 0)\n return 1;\n l = (c->Nl + (((SHA_LONG64) len) << 3)) & U64(0xffffffffffffffff);\n if (l < c->Nl)\n c->Nh++;\n if (sizeof(len) >= 8)\n c->Nh += (((SHA_LONG64) len) >> 61);\n c->Nl = l;\n if (c->num != 0) {\n size_t n = sizeof(c->u) - c->num;\n if (len < n) {\n memcpy(p + c->num, data, len), c->num += (unsigned int)len;\n return 1;\n } else {\n memcpy(p + c->num, data, n), c->num = 0;\n len -= n, data += n;\n sha512_block_data_order(c, p, 1);\n }\n }\n if (len >= sizeof(c->u)) {\n# ifndef SHA512_BLOCK_CAN_MANAGE_UNALIGNED_DATA\n if ((size_t)data % sizeof(c->u.d[0]) != 0)\n while (len >= sizeof(c->u))\n memcpy(p, data, sizeof(c->u)),\n sha512_block_data_order(c, p, 1),\n len -= sizeof(c->u), data += sizeof(c->u);\n else\n# endif\n sha512_block_data_order(c, data, len / sizeof(c->u)),\n data += len, len %= sizeof(c->u), data -= len;\n }\n if (len != 0)\n memcpy(p, data, len), c->num = (int)len;\n return 1;\n}'] |
35,131 | 0 | https://github.com/libav/libav/blob/124c21d79f2124d028890022e98ea853a834a964/libavcodec/vp56.c/#L178 | static void vp56_decode_4mv(VP56Context *s, int row, int col)
{
VP56mv mv = {0,0};
int type[4];
int b;
for (b=0; b<4; b++) {
type[b] = vp56_rac_gets(&s->c, 2);
if (type[b])
type[b]++;
}
for (b=0; b<4; b++) {
switch (type[b]) {
case VP56_MB_INTER_NOVEC_PF:
s->mv[b] = (VP56mv) {0,0};
break;
case VP56_MB_INTER_DELTA_PF:
s->parse_vector_adjustment(s, &s->mv[b]);
break;
case VP56_MB_INTER_V1_PF:
s->mv[b] = s->vector_candidate[0];
break;
case VP56_MB_INTER_V2_PF:
s->mv[b] = s->vector_candidate[1];
break;
}
mv.x += s->mv[b].x;
mv.y += s->mv[b].y;
}
s->macroblocks[row * s->mb_width + col].mv = s->mv[3];
if (s->avctx->codec->id == CODEC_ID_VP5) {
s->mv[4].x = s->mv[5].x = RSHIFT(mv.x,2);
s->mv[4].y = s->mv[5].y = RSHIFT(mv.y,2);
} else {
s->mv[4] = s->mv[5] = (VP56mv) {mv.x/4, mv.y/4};
}
} | ['static void vp56_decode_4mv(VP56Context *s, int row, int col)\n{\n VP56mv mv = {0,0};\n int type[4];\n int b;\n for (b=0; b<4; b++) {\n type[b] = vp56_rac_gets(&s->c, 2);\n if (type[b])\n type[b]++;\n }\n for (b=0; b<4; b++) {\n switch (type[b]) {\n case VP56_MB_INTER_NOVEC_PF:\n s->mv[b] = (VP56mv) {0,0};\n break;\n case VP56_MB_INTER_DELTA_PF:\n s->parse_vector_adjustment(s, &s->mv[b]);\n break;\n case VP56_MB_INTER_V1_PF:\n s->mv[b] = s->vector_candidate[0];\n break;\n case VP56_MB_INTER_V2_PF:\n s->mv[b] = s->vector_candidate[1];\n break;\n }\n mv.x += s->mv[b].x;\n mv.y += s->mv[b].y;\n }\n s->macroblocks[row * s->mb_width + col].mv = s->mv[3];\n if (s->avctx->codec->id == CODEC_ID_VP5) {\n s->mv[4].x = s->mv[5].x = RSHIFT(mv.x,2);\n s->mv[4].y = s->mv[5].y = RSHIFT(mv.y,2);\n } else {\n s->mv[4] = s->mv[5] = (VP56mv) {mv.x/4, mv.y/4};\n }\n}'] |
35,132 | 0 | https://github.com/openssl/openssl/blob/d40a1b865fddc3d67f8c06ff1f1466fad331c8f7/crypto/bn/bn_shift.c/#L150 | 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);
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,nw*sizeof(t[0]));
r->top=a->top+nw+1;
bn_correct_top(r);
bn_check_top(r);
return(1);
} | ['int test_mod(BIO *bp, BN_CTX *ctx)\n\t{\n\tBIGNUM *a,*b,*c,*d,*e;\n\tint i;\n\ta=BN_new();\n\tb=BN_new();\n\tc=BN_new();\n\td=BN_new();\n\te=BN_new();\n\tBN_bntest_rand(a,1024,0,0);\n\tfor (i=0; i<num0; i++)\n\t\t{\n\t\tBN_bntest_rand(b,450+i*10,0,0);\n\t\ta->neg=rand_neg();\n\t\tb->neg=rand_neg();\n\t\tBN_mod(c,a,b,ctx);\n\t\tif (bp != NULL)\n\t\t\t{\n\t\t\tif (!results)\n\t\t\t\t{\n\t\t\t\tBN_print(bp,a);\n\t\t\t\tBIO_puts(bp," % ");\n\t\t\t\tBN_print(bp,b);\n\t\t\t\tBIO_puts(bp," - ");\n\t\t\t\t}\n\t\t\tBN_print(bp,c);\n\t\t\tBIO_puts(bp,"\\n");\n\t\t\t}\n\t\tBN_div(d,e,a,b,ctx);\n\t\tBN_sub(e,e,c);\n\t\tif(!BN_is_zero(e))\n\t\t {\n\t\t fprintf(stderr,"Modulo test failed!\\n");\n\t\t return 0;\n\t\t }\n\t\t}\n\tBN_free(a);\n\tBN_free(b);\n\tBN_free(c);\n\tBN_free(d);\n\tBN_free(e);\n\treturn(1);\n\t}', 'int BN_bntest_rand(BIGNUM *rnd, size_t bits, int top, int bottom)\n\t{\n\treturn bnrand(2, rnd, bits, top, bottom);\n\t}', 'static int bnrand(int pseudorand, BIGNUM *rnd, int bits, int top, int bottom)\n\t{\n\tunsigned char *buf=NULL;\n\tint ret=0,bit,bytes,mask;\n\ttime_t tim;\n\tif (bits == 0)\n\t\t{\n\t\tBN_zero(rnd);\n\t\treturn 1;\n\t\t}\n\tbytes=(bits+7)/8;\n\tbit=(bits-1)%8;\n\tmask=0xff<<(bit+1);\n\tbuf=(unsigned char *)OPENSSL_malloc(bytes);\n\tif (buf == NULL)\n\t\t{\n\t\tBNerr(BN_F_BNRAND,ERR_R_MALLOC_FAILURE);\n\t\tgoto err;\n\t\t}\n\ttime(&tim);\n\tRAND_add(&tim,sizeof(tim),0.0);\n\tif (pseudorand)\n\t\t{\n\t\tif (RAND_pseudo_bytes(buf, bytes) == -1)\n\t\t\tgoto err;\n\t\t}\n\telse\n\t\t{\n\t\tif (RAND_bytes(buf, bytes) <= 0)\n\t\t\tgoto err;\n\t\t}\n#if 1\n\tif (pseudorand == 2)\n\t\t{\n\t\tint i;\n\t\tunsigned char c;\n\t\tfor (i = 0; i < bytes; i++)\n\t\t\t{\n\t\t\tRAND_pseudo_bytes(&c, 1);\n\t\t\tif (c >= 128 && i > 0)\n\t\t\t\tbuf[i] = buf[i-1];\n\t\t\telse if (c < 42)\n\t\t\t\tbuf[i] = 0;\n\t\t\telse if (c < 84)\n\t\t\t\tbuf[i] = 255;\n\t\t\t}\n\t\t}\n#endif\n\tif (top != -1)\n\t\t{\n\t\tif (top)\n\t\t\t{\n\t\t\tif (bit == 0)\n\t\t\t\t{\n\t\t\t\tbuf[0]=1;\n\t\t\t\tbuf[1]|=0x80;\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\tbuf[0]|=(3<<(bit-1));\n\t\t\t\t}\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tbuf[0]|=(1<<bit);\n\t\t\t}\n\t\t}\n\tbuf[0] &= ~mask;\n\tif (bottom)\n\t\tbuf[bytes-1]|=1;\n\tif (!BN_bin2bn(buf,bytes,rnd)) goto err;\n\tret=1;\nerr:\n\tif (buf != NULL)\n\t\t{\n\t\tOPENSSL_cleanse(buf,bytes);\n\t\tOPENSSL_free(buf);\n\t\t}\n\tbn_check_top(rnd);\n\treturn(ret);\n\t}', '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;\n\tsize_t loop;\n\tBIGNUM *tmp,wnum,*snum,*sdiv,*res;\n\tBN_ULONG *resp,*wnump;\n\tBN_ULONG d0,d1;\n\tsize_t num_n,div_n;\n\tif (num->top > 0 && num->d[num->top - 1] == 0)\n\t\t{\n\t\tBNerr(BN_F_BN_DIV,BN_R_NOT_INITIALIZED);\n\t\treturn 0;\n\t\t}\n\tbn_check_top(num);\n\tif ((BN_get_flags(num, BN_FLG_CONSTTIME) != 0) || (BN_get_flags(divisor, BN_FLG_CONSTTIME) != 0))\n\t\t{\n\t\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(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;\n\t\t\tq=bn_div_words(n0,n1,d0);\n#ifdef BN_DEBUG_LEVITTE\n\t\t\tfprintf(stderr,"DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\\\nX) -> 0x%08X\\n",\n\t\t\t\tn0, n1, d0, q);\n#endif\n#ifndef REMAINDER_IS_ALREADY_CALCULATED\n\t\t\trem=(n1-q*d0)&BN_MASK2;\n#endif\n#if defined(BN_UMULT_LOHI)\n\t\t\tBN_UMULT_LOHI(t2l,t2h,d1,q);\n#elif defined(BN_UMULT_HIGH)\n\t\t\tt2l = d1 * q;\n\t\t\tt2h = BN_UMULT_HIGH(d1,q);\n#else\n\t\t\t{\n\t\t\tBN_ULONG ql, qh;\n\t\t\tt2l=LBITS(d1); t2h=HBITS(d1);\n\t\t\tql =LBITS(q); qh =HBITS(q);\n\t\t\tmul64(t2l,t2h,ql,qh);\n\t\t\t}\n#endif\n\t\t\tfor (;;)\n\t\t\t\t{\n\t\t\t\tif ((t2h < rem) ||\n\t\t\t\t\t((t2h == rem) && (t2l <= wnump[-2])))\n\t\t\t\t\tbreak;\n\t\t\t\tq--;\n\t\t\t\trem += d0;\n\t\t\t\tif (rem < d0) break;\n\t\t\t\tif (t2l < d1) t2h--; t2l -= d1;\n\t\t\t\t}\n#endif\n\t\t\t}\n#endif\n\t\tl0=bn_mul_words(tmp->d,sdiv->d,div_n,q);\n\t\ttmp->d[div_n]=l0;\n\t\twnum.d--;\n\t\tif (bn_sub_words(wnum.d, wnum.d, tmp->d, div_n+1))\n\t\t\t{\n\t\t\tq--;\n\t\t\tif (bn_add_words(wnum.d, wnum.d, sdiv->d, div_n))\n\t\t\t\t(*wnump)++;\n\t\t\t}\n\t\t*resp = q;\n\t\t}\n\tbn_correct_top(snum);\n\tif (rm != NULL)\n\t\t{\n\t\tint neg = num->neg;\n\t\tBN_rshift(rm,snum,norm_shift);\n\t\tif (!BN_is_zero(rm))\n\t\t\trm->neg = neg;\n\t\tbn_check_top(rm);\n\t\t}\n\tBN_CTX_end(ctx);\n\treturn(1);\nerr:\n\tbn_check_top(rm);\n\tBN_CTX_end(ctx);\n\treturn(0);\n\t}', 'static int BN_div_no_branch(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num,\n\tconst BIGNUM *divisor, 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\tsize_t num_n,div_n;\n\tbn_check_top(dv);\n\tbn_check_top(rm);\n\tbn_check_top(divisor);\n\tif (BN_is_zero(divisor))\n\t\t{\n\t\tBNerr(BN_F_BN_DIV_NO_BRANCH,BN_R_DIV_BY_ZERO);\n\t\treturn(0);\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\tif (snum->top <= sdiv->top+1)\n\t\t{\n\t\tif (bn_wexpand(snum, sdiv->top + 2) == NULL) goto err;\n\t\tfor (i = snum->top; i < sdiv->top + 2; i++) snum->d[i] = 0;\n\t\tsnum->top = sdiv->top + 2;\n\t\t}\n\telse\n\t\t{\n\t\tif (bn_wexpand(snum, snum->top + 1) == NULL) goto err;\n\t\tsnum->d[snum->top] = 0;\n\t\tsnum->top ++;\n\t\t}\n\tdiv_n=sdiv->top;\n\tnum_n=snum->top;\n\tloop=num_n-div_n;\n\twnum.neg = 0;\n\twnum.d = &(snum->d[loop]);\n\twnum.top = div_n;\n\twnum.dmax = snum->dmax - loop;\n\td0=sdiv->d[div_n-1];\n\td1=(div_n == 1)?0:sdiv->d[div_n-2];\n\twnump= &(snum->d[num_n-1]);\n\tres->neg= (num->neg^divisor->neg);\n\tif (!bn_wexpand(res,loop+1U)) goto err;\n\tres->top=loop-1;\n\tresp= &(res->d[loop-1]);\n\tif (!bn_wexpand(tmp,div_n+1U)) goto err;\n\tif (res->top == 0)\n\t\tres->neg = 0;\n\telse\n\t\tresp--;\n\tfor (i=0; i<loop-1; i++, wnump--, resp--)\n\t\t{\n\t\tBN_ULONG q,l0;\n#if defined(BN_DIV3W) && !defined(OPENSSL_NO_ASM)\n\t\tBN_ULONG bn_div_3_words(BN_ULONG*,BN_ULONG,BN_ULONG);\n\t\tq=bn_div_3_words(wnump,d1,d0);\n#else\n\t\tBN_ULONG n0,n1,rem=0;\n\t\tn0=wnump[0];\n\t\tn1=wnump[-1];\n\t\tif (n0 == d0)\n\t\t\tq=BN_MASK2;\n\t\telse\n\t\t\t{\n#ifdef BN_LLONG\n\t\t\tBN_ULLONG t2;\n#if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)\n\t\t\tq=(BN_ULONG)(((((BN_ULLONG)n0)<<BN_BITS2)|n1)/d0);\n#else\n\t\t\tq=bn_div_words(n0,n1,d0);\n#ifdef BN_DEBUG_LEVITTE\n\t\t\tfprintf(stderr,"DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\\\nX) -> 0x%08X\\n",\n\t\t\t\tn0, n1, d0, q);\n#endif\n#endif\n#ifndef REMAINDER_IS_ALREADY_CALCULATED\n\t\t\trem=(n1-q*d0)&BN_MASK2;\n#endif\n\t\t\tt2=(BN_ULLONG)d1*q;\n\t\t\tfor (;;)\n\t\t\t\t{\n\t\t\t\tif (t2 <= ((((BN_ULLONG)rem)<<BN_BITS2)|wnump[-2]))\n\t\t\t\t\tbreak;\n\t\t\t\tq--;\n\t\t\t\trem += d0;\n\t\t\t\tif (rem < d0) break;\n\t\t\t\tt2 -= d1;\n\t\t\t\t}\n#else\n\t\t\tBN_ULONG t2l,t2h;\n\t\t\tq=bn_div_words(n0,n1,d0);\n#ifdef BN_DEBUG_LEVITTE\n\t\t\tfprintf(stderr,"DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\\\nX) -> 0x%08X\\n",\n\t\t\t\tn0, n1, d0, q);\n#endif\n#ifndef REMAINDER_IS_ALREADY_CALCULATED\n\t\t\trem=(n1-q*d0)&BN_MASK2;\n#endif\n#if defined(BN_UMULT_LOHI)\n\t\t\tBN_UMULT_LOHI(t2l,t2h,d1,q);\n#elif defined(BN_UMULT_HIGH)\n\t\t\tt2l = d1 * q;\n\t\t\tt2h = BN_UMULT_HIGH(d1,q);\n#else\n\t\t\t{\n\t\t\tBN_ULONG ql, qh;\n\t\t\tt2l=LBITS(d1); t2h=HBITS(d1);\n\t\t\tql =LBITS(q); qh =HBITS(q);\n\t\t\tmul64(t2l,t2h,ql,qh);\n\t\t\t}\n#endif\n\t\t\tfor (;;)\n\t\t\t\t{\n\t\t\t\tif ((t2h < rem) ||\n\t\t\t\t\t((t2h == rem) && (t2l <= wnump[-2])))\n\t\t\t\t\tbreak;\n\t\t\t\tq--;\n\t\t\t\trem += d0;\n\t\t\t\tif (rem < d0) break;\n\t\t\t\tif (t2l < d1) t2h--; t2l -= d1;\n\t\t\t\t}\n#endif\n\t\t\t}\n#endif\n\t\tl0=bn_mul_words(tmp->d,sdiv->d,div_n,q);\n\t\ttmp->d[div_n]=l0;\n\t\twnum.d--;\n\t\tif (bn_sub_words(wnum.d, wnum.d, tmp->d, div_n+1))\n\t\t\t{\n\t\t\tq--;\n\t\t\tif (bn_add_words(wnum.d, wnum.d, sdiv->d, div_n))\n\t\t\t\t(*wnump)++;\n\t\t\t}\n\t\t*resp = q;\n\t\t}\n\tbn_correct_top(snum);\n\tif (rm != NULL)\n\t\t{\n\t\tint neg = num->neg;\n\t\tBN_rshift(rm,snum,norm_shift);\n\t\tif (!BN_is_zero(rm))\n\t\t\trm->neg = neg;\n\t\tbn_check_top(rm);\n\t\t}\n\tbn_correct_top(res);\n\tBN_CTX_end(ctx);\n\treturn(1);\nerr:\n\tbn_check_top(rm);\n\tBN_CTX_end(ctx);\n\treturn(0);\n\t}', 'int BN_lshift(BIGNUM *r, const BIGNUM *a, int n)\n\t{\n\tint i,nw,lb,rb;\n\tBN_ULONG *t,*f;\n\tBN_ULONG l;\n\tbn_check_top(r);\n\tbn_check_top(a);\n\tr->neg=a->neg;\n\tnw=n/BN_BITS2;\n\tif (bn_wexpand(r,a->top+nw+1) == NULL) return(0);\n\tlb=n%BN_BITS2;\n\trb=BN_BITS2-lb;\n\tf=a->d;\n\tt=r->d;\n\tt[a->top+nw]=0;\n\tif (lb == 0)\n\t\tfor (i=a->top-1; i>=0; i--)\n\t\t\tt[nw+i]=f[i];\n\telse\n\t\tfor (i=a->top-1; i>=0; i--)\n\t\t\t{\n\t\t\tl=f[i];\n\t\t\tt[nw+i+1]|=(l>>rb)&BN_MASK2;\n\t\t\tt[nw+i]=(l<<lb)&BN_MASK2;\n\t\t\t}\n\tmemset(t,0,nw*sizeof(t[0]));\n\tr->top=a->top+nw+1;\n\tbn_correct_top(r);\n\tbn_check_top(r);\n\treturn(1);\n\t}'] |
35,133 | 0 | https://github.com/openssl/openssl/blob/d8028b202bfe337200a0cc89b80983ea1838cb30/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);
} | ['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}'] |
35,134 | 0 | https://github.com/libav/libav/blob/6cecd63005b29a1dc3a5104e6ac85fd112705122/libswscale/swscale.c/#L3063 | SwsFilter *sws_getDefaultFilter(float lumaGBlur, float chromaGBlur,
float lumaSharpen, float chromaSharpen,
float chromaHShift, float chromaVShift,
int verbose)
{
SwsFilter *filter= av_malloc(sizeof(SwsFilter));
if (lumaGBlur!=0.0){
filter->lumH= sws_getGaussianVec(lumaGBlur, 3.0);
filter->lumV= sws_getGaussianVec(lumaGBlur, 3.0);
}else{
filter->lumH= sws_getIdentityVec();
filter->lumV= sws_getIdentityVec();
}
if (chromaGBlur!=0.0){
filter->chrH= sws_getGaussianVec(chromaGBlur, 3.0);
filter->chrV= sws_getGaussianVec(chromaGBlur, 3.0);
}else{
filter->chrH= sws_getIdentityVec();
filter->chrV= sws_getIdentityVec();
}
if (chromaSharpen!=0.0){
SwsVector *id= sws_getIdentityVec();
sws_scaleVec(filter->chrH, -chromaSharpen);
sws_scaleVec(filter->chrV, -chromaSharpen);
sws_addVec(filter->chrH, id);
sws_addVec(filter->chrV, id);
sws_freeVec(id);
}
if (lumaSharpen!=0.0){
SwsVector *id= sws_getIdentityVec();
sws_scaleVec(filter->lumH, -lumaSharpen);
sws_scaleVec(filter->lumV, -lumaSharpen);
sws_addVec(filter->lumH, id);
sws_addVec(filter->lumV, id);
sws_freeVec(id);
}
if (chromaHShift != 0.0)
sws_shiftVec(filter->chrH, (int)(chromaHShift+0.5));
if (chromaVShift != 0.0)
sws_shiftVec(filter->chrV, (int)(chromaVShift+0.5));
sws_normalizeVec(filter->chrH, 1.0);
sws_normalizeVec(filter->chrV, 1.0);
sws_normalizeVec(filter->lumH, 1.0);
sws_normalizeVec(filter->lumV, 1.0);
if (verbose) sws_printVec2(filter->chrH, NULL, AV_LOG_DEBUG);
if (verbose) sws_printVec2(filter->lumH, NULL, AV_LOG_DEBUG);
return filter;
} | ['SwsFilter *sws_getDefaultFilter(float lumaGBlur, float chromaGBlur,\n float lumaSharpen, float chromaSharpen,\n float chromaHShift, float chromaVShift,\n int verbose)\n{\n SwsFilter *filter= av_malloc(sizeof(SwsFilter));\n if (lumaGBlur!=0.0){\n filter->lumH= sws_getGaussianVec(lumaGBlur, 3.0);\n filter->lumV= sws_getGaussianVec(lumaGBlur, 3.0);\n }else{\n filter->lumH= sws_getIdentityVec();\n filter->lumV= sws_getIdentityVec();\n }\n if (chromaGBlur!=0.0){\n filter->chrH= sws_getGaussianVec(chromaGBlur, 3.0);\n filter->chrV= sws_getGaussianVec(chromaGBlur, 3.0);\n }else{\n filter->chrH= sws_getIdentityVec();\n filter->chrV= sws_getIdentityVec();\n }\n if (chromaSharpen!=0.0){\n SwsVector *id= sws_getIdentityVec();\n sws_scaleVec(filter->chrH, -chromaSharpen);\n sws_scaleVec(filter->chrV, -chromaSharpen);\n sws_addVec(filter->chrH, id);\n sws_addVec(filter->chrV, id);\n sws_freeVec(id);\n }\n if (lumaSharpen!=0.0){\n SwsVector *id= sws_getIdentityVec();\n sws_scaleVec(filter->lumH, -lumaSharpen);\n sws_scaleVec(filter->lumV, -lumaSharpen);\n sws_addVec(filter->lumH, id);\n sws_addVec(filter->lumV, id);\n sws_freeVec(id);\n }\n if (chromaHShift != 0.0)\n sws_shiftVec(filter->chrH, (int)(chromaHShift+0.5));\n if (chromaVShift != 0.0)\n sws_shiftVec(filter->chrV, (int)(chromaVShift+0.5));\n sws_normalizeVec(filter->chrH, 1.0);\n sws_normalizeVec(filter->chrV, 1.0);\n sws_normalizeVec(filter->lumH, 1.0);\n sws_normalizeVec(filter->lumV, 1.0);\n if (verbose) sws_printVec2(filter->chrH, NULL, AV_LOG_DEBUG);\n if (verbose) sws_printVec2(filter->lumH, NULL, AV_LOG_DEBUG);\n return filter;\n}', 'void *av_malloc(unsigned int size)\n{\n void *ptr = NULL;\n#if CONFIG_MEMALIGN_HACK\n long diff;\n#endif\n if(size > (INT_MAX-16) )\n return NULL;\n#if CONFIG_MEMALIGN_HACK\n ptr = malloc(size+16);\n if(!ptr)\n return ptr;\n diff= ((-(long)ptr - 1)&15) + 1;\n ptr = (char*)ptr + diff;\n ((char*)ptr)[-1]= diff;\n#elif HAVE_POSIX_MEMALIGN\n if (posix_memalign(&ptr,16,size))\n ptr = NULL;\n#elif HAVE_MEMALIGN\n ptr = memalign(16,size);\n#else\n ptr = malloc(size);\n#endif\n return ptr;\n}', 'SwsVector *sws_getIdentityVec(void){\n return sws_getConstVec(1.0, 1);\n}'] |
35,135 | 0 | https://github.com/libav/libav/blob/688417399c69aadd4c287bdb0dec82ef8799011c/libavcodec/hevcdsp_template.c/#L901 | PUT_HEVC_QPEL_HV(1, 1) | ['QPEL(4)', 'PUT_HEVC_QPEL_HV(1, 1)'] |
35,136 | 0 | https://github.com/openssl/openssl/blob/848113a30b431c2fe21ae8de2a366b9b6146fb92/crypto/bn/bn_lib.c/#L96 | int BN_num_bits_word(BN_ULONG l)
{
BN_ULONG x, mask;
int bits = (l != 0);
#if BN_BITS2 > 32
x = l >> 32;
mask = (0 - x) & BN_MASK2;
mask = (0 - (mask >> (BN_BITS2 - 1)));
bits += 32 & mask;
l ^= (x ^ l) & mask;
#endif
x = l >> 16;
mask = (0 - x) & BN_MASK2;
mask = (0 - (mask >> (BN_BITS2 - 1)));
bits += 16 & mask;
l ^= (x ^ l) & mask;
x = l >> 8;
mask = (0 - x) & BN_MASK2;
mask = (0 - (mask >> (BN_BITS2 - 1)));
bits += 8 & mask;
l ^= (x ^ l) & mask;
x = l >> 4;
mask = (0 - x) & BN_MASK2;
mask = (0 - (mask >> (BN_BITS2 - 1)));
bits += 4 & mask;
l ^= (x ^ l) & mask;
x = l >> 2;
mask = (0 - x) & BN_MASK2;
mask = (0 - (mask >> (BN_BITS2 - 1)));
bits += 2 & mask;
l ^= (x ^ l) & mask;
x = l >> 1;
mask = (0 - x) & BN_MASK2;
mask = (0 - (mask >> (BN_BITS2 - 1)));
bits += 1 & mask;
return bits;
} | ['BIGNUM *SRP_Calc_client_key(const BIGNUM *N, const BIGNUM *B, const BIGNUM *g,\n const BIGNUM *x, const BIGNUM *a, const BIGNUM *u)\n{\n BIGNUM *tmp = NULL, *tmp2 = NULL, *tmp3 = NULL, *k = NULL, *K = NULL;\n BN_CTX *bn_ctx;\n if (u == NULL || B == NULL || N == NULL || g == NULL || x == NULL\n || a == NULL || (bn_ctx = BN_CTX_new()) == NULL)\n return NULL;\n if ((tmp = BN_new()) == NULL ||\n (tmp2 = BN_new()) == NULL ||\n (tmp3 = BN_new()) == NULL)\n goto err;\n if (!BN_mod_exp(tmp, g, x, N, bn_ctx))\n goto err;\n if ((k = srp_Calc_k(N, g)) == NULL)\n goto err;\n if (!BN_mod_mul(tmp2, tmp, k, N, bn_ctx))\n goto err;\n if (!BN_mod_sub(tmp, B, tmp2, N, bn_ctx))\n goto err;\n if (!BN_mul(tmp3, u, x, bn_ctx))\n goto err;\n if (!BN_add(tmp2, a, tmp3))\n goto err;\n K = BN_new();\n if (K != NULL && !BN_mod_exp(K, tmp, tmp2, N, bn_ctx)) {\n BN_free(K);\n K = NULL;\n }\n err:\n BN_CTX_free(bn_ctx);\n BN_clear_free(tmp);\n BN_clear_free(tmp2);\n BN_clear_free(tmp3);\n BN_free(k);\n return K;\n}', 'int BN_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m,\n BN_CTX *ctx)\n{\n int ret;\n bn_check_top(a);\n bn_check_top(p);\n bn_check_top(m);\n#define MONT_MUL_MOD\n#define MONT_EXP_WORD\n#define RECP_MUL_MOD\n#ifdef MONT_MUL_MOD\n if (BN_is_odd(m)) {\n# ifdef MONT_EXP_WORD\n if (a->top == 1 && !a->neg\n && (BN_get_flags(p, BN_FLG_CONSTTIME) == 0)\n && (BN_get_flags(a, BN_FLG_CONSTTIME) == 0)\n && (BN_get_flags(m, BN_FLG_CONSTTIME) == 0)) {\n BN_ULONG A = a->d[0];\n ret = BN_mod_exp_mont_word(r, A, p, m, ctx, NULL);\n } else\n# endif\n ret = BN_mod_exp_mont(r, a, p, m, ctx, NULL);\n } else\n#endif\n#ifdef RECP_MUL_MOD\n {\n ret = BN_mod_exp_recp(r, a, p, m, ctx);\n }\n#else\n {\n ret = BN_mod_exp_simple(r, a, p, m, ctx);\n }\n#endif\n bn_check_top(r);\n return ret;\n}', 'int BN_is_odd(const BIGNUM *a)\n{\n return (a->top > 0) && (a->d[0] & 1);\n}', 'int BN_mod_exp_mont_word(BIGNUM *rr, BN_ULONG a, const BIGNUM *p,\n const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont)\n{\n BN_MONT_CTX *mont = NULL;\n int b, bits, ret = 0;\n int r_is_one;\n BN_ULONG w, next_w;\n BIGNUM *r, *t;\n BIGNUM *swap_tmp;\n#define BN_MOD_MUL_WORD(r, w, m) \\\n (BN_mul_word(r, (w)) && \\\n ( \\\n (BN_mod(t, r, m, ctx) && (swap_tmp = r, r = t, t = swap_tmp, 1))))\n#define BN_TO_MONTGOMERY_WORD(r, w, mont) \\\n (BN_set_word(r, (w)) && BN_to_montgomery(r, r, (mont), ctx))\n if (BN_get_flags(p, BN_FLG_CONSTTIME) != 0\n || BN_get_flags(m, BN_FLG_CONSTTIME) != 0) {\n BNerr(BN_F_BN_MOD_EXP_MONT_WORD, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);\n return 0;\n }\n bn_check_top(p);\n bn_check_top(m);\n if (!BN_is_odd(m)) {\n BNerr(BN_F_BN_MOD_EXP_MONT_WORD, BN_R_CALLED_WITH_EVEN_MODULUS);\n return 0;\n }\n if (m->top == 1)\n a %= m->d[0];\n bits = BN_num_bits(p);\n if (bits == 0) {\n if (BN_abs_is_word(m, 1)) {\n ret = 1;\n BN_zero(rr);\n } else {\n ret = BN_one(rr);\n }\n return ret;\n }\n if (a == 0) {\n BN_zero(rr);\n ret = 1;\n return ret;\n }\n BN_CTX_start(ctx);\n r = BN_CTX_get(ctx);\n t = BN_CTX_get(ctx);\n if (t == NULL)\n goto err;\n if (in_mont != NULL)\n mont = in_mont;\n else {\n if ((mont = BN_MONT_CTX_new()) == NULL)\n goto err;\n if (!BN_MONT_CTX_set(mont, m, ctx))\n goto err;\n }\n r_is_one = 1;\n w = a;\n for (b = bits - 2; b >= 0; b--) {\n next_w = w * w;\n if ((next_w / w) != w) {\n if (r_is_one) {\n if (!BN_TO_MONTGOMERY_WORD(r, w, mont))\n goto err;\n r_is_one = 0;\n } else {\n if (!BN_MOD_MUL_WORD(r, w, m))\n goto err;\n }\n next_w = 1;\n }\n w = next_w;\n if (!r_is_one) {\n if (!BN_mod_mul_montgomery(r, r, r, mont, ctx))\n goto err;\n }\n if (BN_is_bit_set(p, b)) {\n next_w = w * a;\n if ((next_w / a) != w) {\n if (r_is_one) {\n if (!BN_TO_MONTGOMERY_WORD(r, w, mont))\n goto err;\n r_is_one = 0;\n } else {\n if (!BN_MOD_MUL_WORD(r, w, m))\n goto err;\n }\n next_w = a;\n }\n w = next_w;\n }\n }\n if (w != 1) {\n if (r_is_one) {\n if (!BN_TO_MONTGOMERY_WORD(r, w, mont))\n goto err;\n r_is_one = 0;\n } else {\n if (!BN_MOD_MUL_WORD(r, w, m))\n goto err;\n }\n }\n if (r_is_one) {\n if (!BN_one(rr))\n goto err;\n } else {\n if (!BN_from_montgomery(rr, r, mont, ctx))\n goto err;\n }\n ret = 1;\n err:\n if (in_mont == NULL)\n BN_MONT_CTX_free(mont);\n BN_CTX_end(ctx);\n bn_check_top(rr);\n return ret;\n}', 'static BIGNUM *srp_Calc_k(const BIGNUM *N, const BIGNUM *g)\n{\n return srp_Calc_xy(N, g, N);\n}', 'static BIGNUM *srp_Calc_xy(const BIGNUM *x, const BIGNUM *y, const BIGNUM *N)\n{\n unsigned char digest[SHA_DIGEST_LENGTH];\n unsigned char *tmp = NULL;\n int numN = BN_num_bytes(N);\n BIGNUM *res = NULL;\n if (x != N && BN_ucmp(x, N) >= 0)\n return NULL;\n if (y != N && BN_ucmp(y, N) >= 0)\n return NULL;\n if ((tmp = OPENSSL_malloc(numN * 2)) == NULL)\n goto err;\n if (BN_bn2binpad(x, tmp, numN) < 0\n || BN_bn2binpad(y, tmp + numN, numN) < 0\n || !EVP_Digest(tmp, numN * 2, digest, NULL, EVP_sha1(), NULL))\n goto err;\n res = BN_bin2bn(digest, sizeof(digest), NULL);\n err:\n OPENSSL_free(tmp);\n return res;\n}', 'int BN_num_bits(const BIGNUM *a)\n{\n int i = a->top - 1;\n bn_check_top(a);\n if (BN_is_zero(a))\n return 0;\n return ((i * BN_BITS2) + BN_num_bits_word(a->d[i]));\n}', 'int BN_is_zero(const BIGNUM *a)\n{\n return a->top == 0;\n}', '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_mod_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m,\n BN_CTX *ctx)\n{\n if (!BN_sub(r, a, b))\n return 0;\n return BN_nnmod(r, r, m, ctx);\n}', 'int BN_mod_exp_mont(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p,\n const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont)\n{\n int i, j, bits, ret = 0, wstart, wend, window, wvalue;\n int start = 1;\n BIGNUM *d, *r;\n const BIGNUM *aa;\n BIGNUM *val[TABLE_SIZE];\n BN_MONT_CTX *mont = NULL;\n if (BN_get_flags(p, BN_FLG_CONSTTIME) != 0\n || BN_get_flags(a, BN_FLG_CONSTTIME) != 0\n || BN_get_flags(m, BN_FLG_CONSTTIME) != 0) {\n return BN_mod_exp_mont_consttime(rr, a, p, m, ctx, in_mont);\n }\n bn_check_top(a);\n bn_check_top(p);\n bn_check_top(m);\n if (!BN_is_odd(m)) {\n BNerr(BN_F_BN_MOD_EXP_MONT, BN_R_CALLED_WITH_EVEN_MODULUS);\n return 0;\n }\n bits = BN_num_bits(p);\n if (bits == 0) {\n if (BN_abs_is_word(m, 1)) {\n ret = 1;\n BN_zero(rr);\n } else {\n ret = BN_one(rr);\n }\n return ret;\n }\n BN_CTX_start(ctx);\n d = BN_CTX_get(ctx);\n r = BN_CTX_get(ctx);\n val[0] = BN_CTX_get(ctx);\n if (val[0] == NULL)\n goto err;\n if (in_mont != NULL)\n mont = in_mont;\n else {\n if ((mont = BN_MONT_CTX_new()) == NULL)\n goto err;\n if (!BN_MONT_CTX_set(mont, m, ctx))\n goto err;\n }\n if (a->neg || BN_ucmp(a, m) >= 0) {\n if (!BN_nnmod(val[0], a, m, ctx))\n goto err;\n aa = val[0];\n } else\n aa = a;\n if (BN_is_zero(aa)) {\n BN_zero(rr);\n ret = 1;\n goto err;\n }\n if (!BN_to_montgomery(val[0], aa, mont, ctx))\n goto err;\n window = BN_window_bits_for_exponent_size(bits);\n if (window > 1) {\n if (!BN_mod_mul_montgomery(d, val[0], val[0], mont, ctx))\n goto err;\n j = 1 << (window - 1);\n for (i = 1; i < j; i++) {\n if (((val[i] = BN_CTX_get(ctx)) == NULL) ||\n !BN_mod_mul_montgomery(val[i], val[i - 1], d, mont, ctx))\n goto err;\n }\n }\n start = 1;\n wvalue = 0;\n wstart = bits - 1;\n wend = 0;\n#if 1\n j = m->top;\n if (m->d[j - 1] & (((BN_ULONG)1) << (BN_BITS2 - 1))) {\n if (bn_wexpand(r, j) == NULL)\n goto err;\n r->d[0] = (0 - m->d[0]) & BN_MASK2;\n for (i = 1; i < j; i++)\n r->d[i] = (~m->d[i]) & BN_MASK2;\n r->top = j;\n bn_correct_top(r);\n } else\n#endif\n if (!BN_to_montgomery(r, BN_value_one(), mont, ctx))\n goto err;\n for (;;) {\n if (BN_is_bit_set(p, wstart) == 0) {\n if (!start) {\n if (!BN_mod_mul_montgomery(r, r, r, mont, ctx))\n goto err;\n }\n if (wstart == 0)\n break;\n wstart--;\n continue;\n }\n j = wstart;\n wvalue = 1;\n wend = 0;\n for (i = 1; i < window; i++) {\n if (wstart - i < 0)\n break;\n if (BN_is_bit_set(p, wstart - i)) {\n wvalue <<= (i - wend);\n wvalue |= 1;\n wend = i;\n }\n }\n j = wend + 1;\n if (!start)\n for (i = 0; i < j; i++) {\n if (!BN_mod_mul_montgomery(r, r, r, mont, ctx))\n goto err;\n }\n if (!BN_mod_mul_montgomery(r, r, val[wvalue >> 1], mont, ctx))\n goto err;\n wstart -= wend + 1;\n wvalue = 0;\n start = 0;\n if (wstart < 0)\n break;\n }\n#if defined(SPARC_T4_MONT)\n if (OPENSSL_sparcv9cap_P[0] & (SPARCV9_VIS3 | SPARCV9_PREFER_FPU)) {\n j = mont->N.top;\n val[0]->d[0] = 1;\n for (i = 1; i < j; i++)\n val[0]->d[i] = 0;\n val[0]->top = j;\n if (!BN_mod_mul_montgomery(rr, r, val[0], mont, ctx))\n goto err;\n } else\n#endif\n if (!BN_from_montgomery(rr, r, mont, ctx))\n goto err;\n ret = 1;\n err:\n if (in_mont == NULL)\n BN_MONT_CTX_free(mont);\n BN_CTX_end(ctx);\n bn_check_top(rr);\n return ret;\n}', 'int BN_mod_exp_mont_consttime(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p,\n const BIGNUM *m, BN_CTX *ctx,\n BN_MONT_CTX *in_mont)\n{\n int i, bits, ret = 0, window, wvalue, wmask, window0;\n int top;\n BN_MONT_CTX *mont = NULL;\n int numPowers;\n unsigned char *powerbufFree = NULL;\n int powerbufLen = 0;\n unsigned char *powerbuf = NULL;\n BIGNUM tmp, am;\n#if defined(SPARC_T4_MONT)\n unsigned int t4 = 0;\n#endif\n bn_check_top(a);\n bn_check_top(p);\n bn_check_top(m);\n if (!BN_is_odd(m)) {\n BNerr(BN_F_BN_MOD_EXP_MONT_CONSTTIME, BN_R_CALLED_WITH_EVEN_MODULUS);\n return 0;\n }\n top = m->top;\n bits = p->top * BN_BITS2;\n if (bits == 0) {\n if (BN_abs_is_word(m, 1)) {\n ret = 1;\n BN_zero(rr);\n } else {\n ret = BN_one(rr);\n }\n return ret;\n }\n BN_CTX_start(ctx);\n if (in_mont != NULL)\n mont = in_mont;\n else {\n if ((mont = BN_MONT_CTX_new()) == NULL)\n goto err;\n if (!BN_MONT_CTX_set(mont, m, ctx))\n goto err;\n }\n#ifdef RSAZ_ENABLED\n if (!a->neg) {\n if ((16 == a->top) && (16 == p->top) && (BN_num_bits(m) == 1024)\n && rsaz_avx2_eligible()) {\n if (NULL == bn_wexpand(rr, 16))\n goto err;\n RSAZ_1024_mod_exp_avx2(rr->d, a->d, p->d, m->d, mont->RR.d,\n mont->n0[0]);\n rr->top = 16;\n rr->neg = 0;\n bn_correct_top(rr);\n ret = 1;\n goto err;\n } else if ((8 == a->top) && (8 == p->top) && (BN_num_bits(m) == 512)) {\n if (NULL == bn_wexpand(rr, 8))\n goto err;\n RSAZ_512_mod_exp(rr->d, a->d, p->d, m->d, mont->n0[0], mont->RR.d);\n rr->top = 8;\n rr->neg = 0;\n bn_correct_top(rr);\n ret = 1;\n goto err;\n }\n }\n#endif\n window = BN_window_bits_for_ctime_exponent_size(bits);\n#if defined(SPARC_T4_MONT)\n if (window >= 5 && (top & 15) == 0 && top <= 64 &&\n (OPENSSL_sparcv9cap_P[1] & (CFR_MONTMUL | CFR_MONTSQR)) ==\n (CFR_MONTMUL | CFR_MONTSQR) && (t4 = OPENSSL_sparcv9cap_P[0]))\n window = 5;\n else\n#endif\n#if defined(OPENSSL_BN_ASM_MONT5)\n if (window >= 5) {\n window = 5;\n powerbufLen += top * sizeof(mont->N.d[0]);\n }\n#endif\n (void)0;\n numPowers = 1 << window;\n powerbufLen += sizeof(m->d[0]) * (top * numPowers +\n ((2 * top) >\n numPowers ? (2 * top) : numPowers));\n#ifdef alloca\n if (powerbufLen < 3072)\n powerbufFree =\n alloca(powerbufLen + MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH);\n else\n#endif\n if ((powerbufFree =\n OPENSSL_malloc(powerbufLen + MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH))\n == NULL)\n goto err;\n powerbuf = MOD_EXP_CTIME_ALIGN(powerbufFree);\n memset(powerbuf, 0, powerbufLen);\n#ifdef alloca\n if (powerbufLen < 3072)\n powerbufFree = NULL;\n#endif\n tmp.d = (BN_ULONG *)(powerbuf + sizeof(m->d[0]) * top * numPowers);\n am.d = tmp.d + top;\n tmp.top = am.top = 0;\n tmp.dmax = am.dmax = top;\n tmp.neg = am.neg = 0;\n tmp.flags = am.flags = BN_FLG_STATIC_DATA;\n#if 1\n if (m->d[top - 1] & (((BN_ULONG)1) << (BN_BITS2 - 1))) {\n tmp.d[0] = (0 - m->d[0]) & BN_MASK2;\n for (i = 1; i < top; i++)\n tmp.d[i] = (~m->d[i]) & BN_MASK2;\n tmp.top = top;\n } else\n#endif\n if (!BN_to_montgomery(&tmp, BN_value_one(), mont, ctx))\n goto err;\n if (a->neg || BN_ucmp(a, m) >= 0) {\n if (!BN_nnmod(&am, a, m, ctx))\n goto err;\n if (!BN_to_montgomery(&am, &am, mont, ctx))\n goto err;\n } else if (!BN_to_montgomery(&am, a, mont, ctx))\n goto err;\n#if defined(SPARC_T4_MONT)\n if (t4) {\n typedef int (*bn_pwr5_mont_f) (BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n int bn_pwr5_mont_t4_8(BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n int bn_pwr5_mont_t4_16(BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n int bn_pwr5_mont_t4_24(BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n int bn_pwr5_mont_t4_32(BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n static const bn_pwr5_mont_f pwr5_funcs[4] = {\n bn_pwr5_mont_t4_8, bn_pwr5_mont_t4_16,\n bn_pwr5_mont_t4_24, bn_pwr5_mont_t4_32\n };\n bn_pwr5_mont_f pwr5_worker = pwr5_funcs[top / 16 - 1];\n typedef int (*bn_mul_mont_f) (BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0);\n int bn_mul_mont_t4_8(BN_ULONG *rp, const BN_ULONG *ap, const void *bp,\n const BN_ULONG *np, const BN_ULONG *n0);\n int bn_mul_mont_t4_16(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0);\n int bn_mul_mont_t4_24(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0);\n int bn_mul_mont_t4_32(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0);\n static const bn_mul_mont_f mul_funcs[4] = {\n bn_mul_mont_t4_8, bn_mul_mont_t4_16,\n bn_mul_mont_t4_24, bn_mul_mont_t4_32\n };\n bn_mul_mont_f mul_worker = mul_funcs[top / 16 - 1];\n void bn_mul_mont_vis3(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0, int num);\n void bn_mul_mont_t4(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0, int num);\n void bn_mul_mont_gather5_t4(BN_ULONG *rp, const BN_ULONG *ap,\n const void *table, const BN_ULONG *np,\n const BN_ULONG *n0, int num, int power);\n void bn_flip_n_scatter5_t4(const BN_ULONG *inp, size_t num,\n void *table, size_t power);\n void bn_gather5_t4(BN_ULONG *out, size_t num,\n void *table, size_t power);\n void bn_flip_t4(BN_ULONG *dst, BN_ULONG *src, size_t num);\n BN_ULONG *np = mont->N.d, *n0 = mont->n0;\n int stride = 5 * (6 - (top / 16 - 1));\n for (i = am.top; i < top; i++)\n am.d[i] = 0;\n for (i = tmp.top; i < top; i++)\n tmp.d[i] = 0;\n bn_flip_n_scatter5_t4(tmp.d, top, powerbuf, 0);\n bn_flip_n_scatter5_t4(am.d, top, powerbuf, 1);\n if (!(*mul_worker) (tmp.d, am.d, am.d, np, n0) &&\n !(*mul_worker) (tmp.d, am.d, am.d, np, n0))\n bn_mul_mont_vis3(tmp.d, am.d, am.d, np, n0, top);\n bn_flip_n_scatter5_t4(tmp.d, top, powerbuf, 2);\n for (i = 3; i < 32; i++) {\n if (!(*mul_worker) (tmp.d, tmp.d, am.d, np, n0) &&\n !(*mul_worker) (tmp.d, tmp.d, am.d, np, n0))\n bn_mul_mont_vis3(tmp.d, tmp.d, am.d, np, n0, top);\n bn_flip_n_scatter5_t4(tmp.d, top, powerbuf, i);\n }\n np = alloca(top * sizeof(BN_ULONG));\n top /= 2;\n bn_flip_t4(np, mont->N.d, top);\n bits--;\n for (wvalue = 0, i = bits % 5; i >= 0; i--, bits--)\n wvalue = (wvalue << 1) + BN_is_bit_set(p, bits);\n bn_gather5_t4(tmp.d, top, powerbuf, wvalue);\n while (bits >= 0) {\n if (bits < stride)\n stride = bits + 1;\n bits -= stride;\n wvalue = bn_get_bits(p, bits + 1);\n if ((*pwr5_worker) (tmp.d, np, n0, powerbuf, wvalue, stride))\n continue;\n if ((*pwr5_worker) (tmp.d, np, n0, powerbuf, wvalue, stride))\n continue;\n bits += stride - 5;\n wvalue >>= stride - 5;\n wvalue &= 31;\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_gather5_t4(tmp.d, tmp.d, powerbuf, np, n0, top,\n wvalue);\n }\n bn_flip_t4(tmp.d, tmp.d, top);\n top *= 2;\n tmp.top = top;\n bn_correct_top(&tmp);\n OPENSSL_cleanse(np, top * sizeof(BN_ULONG));\n } else\n#endif\n#if defined(OPENSSL_BN_ASM_MONT5)\n if (window == 5 && top > 1) {\n void bn_mul_mont_gather5(BN_ULONG *rp, const BN_ULONG *ap,\n const void *table, const BN_ULONG *np,\n const BN_ULONG *n0, int num, int power);\n void bn_scatter5(const BN_ULONG *inp, size_t num,\n void *table, size_t power);\n void bn_gather5(BN_ULONG *out, size_t num, void *table, size_t power);\n void bn_power5(BN_ULONG *rp, const BN_ULONG *ap,\n const void *table, const BN_ULONG *np,\n const BN_ULONG *n0, int num, int power);\n int bn_get_bits5(const BN_ULONG *ap, int off);\n int bn_from_montgomery(BN_ULONG *rp, const BN_ULONG *ap,\n const BN_ULONG *not_used, const BN_ULONG *np,\n const BN_ULONG *n0, int num);\n BN_ULONG *n0 = mont->n0, *np;\n for (i = am.top; i < top; i++)\n am.d[i] = 0;\n for (i = tmp.top; i < top; i++)\n tmp.d[i] = 0;\n for (np = am.d + top, i = 0; i < top; i++)\n np[i] = mont->N.d[i];\n bn_scatter5(tmp.d, top, powerbuf, 0);\n bn_scatter5(am.d, am.top, powerbuf, 1);\n bn_mul_mont(tmp.d, am.d, am.d, np, n0, top);\n bn_scatter5(tmp.d, top, powerbuf, 2);\n# if 0\n for (i = 3; i < 32; i++) {\n bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1);\n bn_scatter5(tmp.d, top, powerbuf, i);\n }\n# else\n for (i = 4; i < 32; i *= 2) {\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_scatter5(tmp.d, top, powerbuf, i);\n }\n for (i = 3; i < 8; i += 2) {\n int j;\n bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1);\n bn_scatter5(tmp.d, top, powerbuf, i);\n for (j = 2 * i; j < 32; j *= 2) {\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_scatter5(tmp.d, top, powerbuf, j);\n }\n }\n for (; i < 16; i += 2) {\n bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1);\n bn_scatter5(tmp.d, top, powerbuf, i);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_scatter5(tmp.d, top, powerbuf, 2 * i);\n }\n for (; i < 32; i += 2) {\n bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1);\n bn_scatter5(tmp.d, top, powerbuf, i);\n }\n# endif\n bits--;\n for (wvalue = 0, i = bits % 5; i >= 0; i--, bits--)\n wvalue = (wvalue << 1) + BN_is_bit_set(p, bits);\n bn_gather5(tmp.d, top, powerbuf, wvalue);\n if (top & 7)\n while (bits >= 0) {\n for (wvalue = 0, i = 0; i < 5; i++, bits--)\n wvalue = (wvalue << 1) + BN_is_bit_set(p, bits);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_gather5(tmp.d, tmp.d, powerbuf, np, n0, top,\n wvalue);\n } else {\n while (bits >= 0) {\n wvalue = bn_get_bits5(p->d, bits - 4);\n bits -= 5;\n bn_power5(tmp.d, tmp.d, powerbuf, np, n0, top, wvalue);\n }\n }\n ret = bn_from_montgomery(tmp.d, tmp.d, NULL, np, n0, top);\n tmp.top = top;\n bn_correct_top(&tmp);\n if (ret) {\n if (!BN_copy(rr, &tmp))\n ret = 0;\n goto err;\n }\n } else\n#endif\n {\n if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&tmp, top, powerbuf, 0, window))\n goto err;\n if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&am, top, powerbuf, 1, window))\n goto err;\n if (window > 1) {\n if (!BN_mod_mul_montgomery(&tmp, &am, &am, mont, ctx))\n goto err;\n if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&tmp, top, powerbuf, 2,\n window))\n goto err;\n for (i = 3; i < numPowers; i++) {\n if (!BN_mod_mul_montgomery(&tmp, &am, &tmp, mont, ctx))\n goto err;\n if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&tmp, top, powerbuf, i,\n window))\n goto err;\n }\n }\n window0 = (bits - 1) % window + 1;\n wmask = (1 << window0) - 1;\n bits -= window0;\n wvalue = bn_get_bits(p, bits) & wmask;\n if (!MOD_EXP_CTIME_COPY_FROM_PREBUF(&tmp, top, powerbuf, wvalue,\n window))\n goto err;\n wmask = (1 << window) - 1;\n while (bits > 0) {\n for (i = 0; i < window; i++)\n if (!BN_mod_mul_montgomery(&tmp, &tmp, &tmp, mont, ctx))\n goto err;\n bits -= window;\n wvalue = bn_get_bits(p, bits) & wmask;\n if (!MOD_EXP_CTIME_COPY_FROM_PREBUF(&am, top, powerbuf, wvalue,\n window))\n goto err;\n if (!BN_mod_mul_montgomery(&tmp, &tmp, &am, mont, ctx))\n goto err;\n }\n }\n#if defined(SPARC_T4_MONT)\n if (OPENSSL_sparcv9cap_P[0] & (SPARCV9_VIS3 | SPARCV9_PREFER_FPU)) {\n am.d[0] = 1;\n for (i = 1; i < top; i++)\n am.d[i] = 0;\n if (!BN_mod_mul_montgomery(rr, &tmp, &am, mont, ctx))\n goto err;\n } else\n#endif\n if (!BN_from_montgomery(rr, &tmp, mont, ctx))\n goto err;\n ret = 1;\n err:\n if (in_mont == NULL)\n BN_MONT_CTX_free(mont);\n if (powerbuf != NULL) {\n OPENSSL_cleanse(powerbuf, powerbufLen);\n OPENSSL_free(powerbufFree);\n }\n BN_CTX_end(ctx);\n return ret;\n}', 'static int MOD_EXP_CTIME_COPY_FROM_PREBUF(BIGNUM *b, int top,\n unsigned char *buf, int idx,\n int window)\n{\n int i, j;\n int width = 1 << window;\n volatile BN_ULONG *table = (volatile BN_ULONG *)buf;\n if (bn_wexpand(b, top) == NULL)\n return 0;\n if (window <= 3) {\n for (i = 0; i < top; i++, table += width) {\n BN_ULONG acc = 0;\n for (j = 0; j < width; j++) {\n acc |= table[j] &\n ((BN_ULONG)0 - (constant_time_eq_int(j,idx)&1));\n }\n b->d[i] = acc;\n }\n } else {\n int xstride = 1 << (window - 2);\n BN_ULONG y0, y1, y2, y3;\n i = idx >> (window - 2);\n idx &= xstride - 1;\n y0 = (BN_ULONG)0 - (constant_time_eq_int(i,0)&1);\n y1 = (BN_ULONG)0 - (constant_time_eq_int(i,1)&1);\n y2 = (BN_ULONG)0 - (constant_time_eq_int(i,2)&1);\n y3 = (BN_ULONG)0 - (constant_time_eq_int(i,3)&1);\n for (i = 0; i < top; i++, table += width) {\n BN_ULONG acc = 0;\n for (j = 0; j < xstride; j++) {\n acc |= ( (table[j + 0 * xstride] & y0) |\n (table[j + 1 * xstride] & y1) |\n (table[j + 2 * xstride] & y2) |\n (table[j + 3 * xstride] & y3) )\n & ((BN_ULONG)0 - (constant_time_eq_int(j,idx)&1));\n }\n b->d[i] = acc;\n }\n }\n b->top = top;\n bn_correct_top(b);\n return 1;\n}', 'void bn_correct_top(BIGNUM *a)\n{\n BN_ULONG *ftl;\n int tmp_top = a->top;\n if (tmp_top > 0) {\n for (ftl = &(a->d[tmp_top]); tmp_top > 0; tmp_top--) {\n ftl--;\n if (*ftl != 0)\n break;\n }\n a->top = tmp_top;\n }\n if (a->top == 0)\n a->neg = 0;\n bn_pollute(a);\n}', 'int BN_mod_mul_montgomery(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,\n BN_MONT_CTX *mont, BN_CTX *ctx)\n{\n BIGNUM *tmp;\n int ret = 0;\n#if defined(OPENSSL_BN_ASM_MONT) && defined(MONT_WORD)\n int num = mont->N.top;\n if (num > 1 && a->top == num && b->top == num) {\n if (bn_wexpand(r, num) == NULL)\n return 0;\n if (bn_mul_mont(r->d, a->d, b->d, mont->N.d, mont->n0, num)) {\n r->neg = a->neg ^ b->neg;\n r->top = num;\n bn_correct_top(r);\n return 1;\n }\n }\n#endif\n BN_CTX_start(ctx);\n tmp = BN_CTX_get(ctx);\n if (tmp == NULL)\n goto err;\n bn_check_top(tmp);\n if (a == b) {\n if (!BN_sqr(tmp, a, ctx))\n goto err;\n } else {\n if (!BN_mul(tmp, a, b, ctx))\n goto err;\n }\n#ifdef MONT_WORD\n if (!BN_from_montgomery_word(r, tmp, mont))\n goto err;\n#else\n if (!BN_from_montgomery(r, tmp, mont, ctx))\n goto err;\n#endif\n bn_check_top(r);\n ret = 1;\n err:\n BN_CTX_end(ctx);\n return ret;\n}', 'int BN_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}', 'int BN_num_bits_word(BN_ULONG l)\n{\n BN_ULONG x, mask;\n int bits = (l != 0);\n#if BN_BITS2 > 32\n x = l >> 32;\n mask = (0 - x) & BN_MASK2;\n mask = (0 - (mask >> (BN_BITS2 - 1)));\n bits += 32 & mask;\n l ^= (x ^ l) & mask;\n#endif\n x = l >> 16;\n mask = (0 - x) & BN_MASK2;\n mask = (0 - (mask >> (BN_BITS2 - 1)));\n bits += 16 & mask;\n l ^= (x ^ l) & mask;\n x = l >> 8;\n mask = (0 - x) & BN_MASK2;\n mask = (0 - (mask >> (BN_BITS2 - 1)));\n bits += 8 & mask;\n l ^= (x ^ l) & mask;\n x = l >> 4;\n mask = (0 - x) & BN_MASK2;\n mask = (0 - (mask >> (BN_BITS2 - 1)));\n bits += 4 & mask;\n l ^= (x ^ l) & mask;\n x = l >> 2;\n mask = (0 - x) & BN_MASK2;\n mask = (0 - (mask >> (BN_BITS2 - 1)));\n bits += 2 & mask;\n l ^= (x ^ l) & mask;\n x = l >> 1;\n mask = (0 - x) & BN_MASK2;\n mask = (0 - (mask >> (BN_BITS2 - 1)));\n bits += 1 & mask;\n return bits;\n}'] |
35,137 | 0 | https://gitlab.com/libtiff/libtiff/blob/b6346f6672955481abb7d177925dddfc832b5380/tools/tiff2pdf.c/#L1901 | 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
uint64 k = 0;
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 = checkAdd64(k, sbc[i], t2p);
}
if(TIFFGetField(input, TIFFTAG_JPEGIFOFFSET, &(t2p->tiff_dataoffset))){
if(t2p->tiff_dataoffset != 0){
if(TIFFGetField(input, TIFFTAG_JPEGIFBYTECOUNT, &(t2p->tiff_datasize))!=0){
if((uint64)t2p->tiff_datasize < k) {
TIFFWarning(TIFF2PDF_MODULE,
"Input file %s has short JPEG interchange file byte count",
TIFFFileName(input));
t2p->pdf_ojpegiflength=t2p->tiff_datasize;
k = checkAdd64(k, t2p->tiff_datasize, t2p);
k = checkAdd64(k, 6, t2p);
k = checkAdd64(k, stripcount, t2p);
k = checkAdd64(k, stripcount, t2p);
t2p->tiff_datasize = (tsize_t) k;
if ((uint64) t2p->tiff_datasize != k) {
TIFFError(TIFF2PDF_MODULE, "Integer overflow");
t2p->t2p_error = T2P_ERR_ERROR;
}
return;
}
return;
}else {
TIFFError(TIFF2PDF_MODULE,
"Input file %s missing field: TIFFTAG_JPEGIFBYTECOUNT",
TIFFFileName(input));
t2p->t2p_error = T2P_ERR_ERROR;
return;
}
}
}
k = checkAdd64(k, stripcount, t2p);
k = checkAdd64(k, stripcount, t2p);
k = checkAdd64(k, 2048, t2p);
t2p->tiff_datasize = (tsize_t) k;
if ((uint64) t2p->tiff_datasize != k) {
TIFFError(TIFF2PDF_MODULE, "Integer overflow");
t2p->t2p_error = T2P_ERR_ERROR;
}
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){
k += count;
k -= 2;
}
} else {
k = 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++){
k = checkAdd64(k, sbc[i], t2p);
k -=2;
k +=2;
}
k = checkAdd64(k, 2, t2p);
k = checkAdd64(k, 6, t2p);
t2p->tiff_datasize = (tsize_t) k;
if ((uint64) t2p->tiff_datasize != k) {
TIFFError(TIFF2PDF_MODULE, "Integer overflow");
t2p->t2p_error = T2P_ERR_ERROR;
}
return;
}
#endif
(void) 0;
}
k = checkMultiply64(TIFFScanlineSize(input), t2p->tiff_length, t2p);
if(t2p->tiff_planar==PLANARCONFIG_SEPARATE){
k = checkMultiply64(k, t2p->tiff_samplesperpixel, t2p);
}
if (k == 0) {
t2p->t2p_error = T2P_ERR_ERROR;
}
t2p->tiff_datasize = (tsize_t) k;
if ((uint64) t2p->tiff_datasize != k) {
TIFFError(TIFF2PDF_MODULE, "Integer overflow");
t2p->t2p_error = T2P_ERR_ERROR;
}
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 uint64 k = 0;\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 = checkAdd64(k, sbc[i], t2p);\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((uint64)t2p->tiff_datasize < k) {\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\tt2p->pdf_ojpegiflength=t2p->tiff_datasize;\n\t\t\t\t\t\t\tk = checkAdd64(k, t2p->tiff_datasize, t2p);\n\t\t\t\t\t\t\tk = checkAdd64(k, 6, t2p);\n\t\t\t\t\t\t\tk = checkAdd64(k, stripcount, t2p);\n\t\t\t\t\t\t\tk = checkAdd64(k, stripcount, t2p);\n\t\t\t\t\t\t\tt2p->tiff_datasize = (tsize_t) k;\n\t\t\t\t\t\t\tif ((uint64) t2p->tiff_datasize != k) {\n\t\t\t\t\t\t\t\tTIFFError(TIFF2PDF_MODULE, "Integer overflow");\n\t\t\t\t\t\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\t\t\t\t\t}\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\tk = checkAdd64(k, stripcount, t2p);\n\t\t\tk = checkAdd64(k, stripcount, t2p);\n\t\t\tk = checkAdd64(k, 2048, t2p);\n\t\t\tt2p->tiff_datasize = (tsize_t) k;\n\t\t\tif ((uint64) t2p->tiff_datasize != k) {\n\t\t\t\tTIFFError(TIFF2PDF_MODULE, "Integer overflow");\n\t\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\t}\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\tk += count;\n\t\t\t\t\tk -= 2;\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tk = 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\tk = checkAdd64(k, sbc[i], t2p);\n\t\t\t\tk -=2;\n\t\t\t\tk +=2;\n\t\t\t}\n\t\t\tk = checkAdd64(k, 2, t2p);\n\t\t\tk = checkAdd64(k, 6, t2p);\n\t\t\tt2p->tiff_datasize = (tsize_t) k;\n\t\t\tif ((uint64) t2p->tiff_datasize != k) {\n\t\t\t\tTIFFError(TIFF2PDF_MODULE, "Integer overflow");\n\t\t\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t\t\t}\n\t\t\treturn;\n\t\t}\n#endif\n\t\t(void) 0;\n\t}\n\tk = checkMultiply64(TIFFScanlineSize(input), t2p->tiff_length, t2p);\n\tif(t2p->tiff_planar==PLANARCONFIG_SEPARATE){\n\t\tk = checkMultiply64(k, t2p->tiff_samplesperpixel, t2p);\n\t}\n\tif (k == 0) {\n\t\tt2p->t2p_error = T2P_ERR_ERROR;\n\t}\n\tt2p->tiff_datasize = (tsize_t) k;\n\tif ((uint64) t2p->tiff_datasize != k) {\n\t\tTIFFError(TIFF2PDF_MODULE, "Integer overflow");\n\t\tt2p->t2p_error = T2P_ERR_ERROR;\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}'] |
35,138 | 0 | https://github.com/openssl/openssl/blob/4b8515baa6edef1a771f9e4e3fbc0395b4a629e8/crypto/bn/bn_ctx.c/#L273 | static unsigned int BN_STACK_pop(BN_STACK *st)
{
return st->indexes[--(st->depth)];
} | ['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}', '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}'] |
35,139 | 0 | https://github.com/openssl/openssl/blob/ba4341316ce762f917f973bb4ac604062fb11724/crypto/mem.c/#L312 | void CRYPTO_free(void *str, const char *file, int line)
{
INCREMENT(free_count);
if (free_impl != NULL && free_impl != &CRYPTO_free) {
free_impl(str, file, line);
return;
}
#if !defined(OPENSSL_NO_CRYPTO_MDEBUG) && !defined(FIPS_MODE)
if (call_malloc_debug) {
CRYPTO_mem_debug_free(str, 0, file, line);
free(str);
CRYPTO_mem_debug_free(str, 1, file, line);
} else {
free(str);
}
#else
free(str);
#endif
} | ['int cms_main(int argc, char **argv)\n{\n ASN1_OBJECT *econtent_type = NULL;\n BIO *in = NULL, *out = NULL, *indata = NULL, *rctin = NULL;\n CMS_ContentInfo *cms = NULL, *rcms = NULL;\n CMS_ReceiptRequest *rr = NULL;\n ENGINE *e = NULL;\n EVP_PKEY *key = NULL;\n const EVP_CIPHER *cipher = NULL, *wrap_cipher = NULL;\n const EVP_MD *sign_md = NULL;\n STACK_OF(OPENSSL_STRING) *rr_to = NULL, *rr_from = NULL;\n STACK_OF(OPENSSL_STRING) *sksigners = NULL, *skkeys = NULL;\n STACK_OF(X509) *encerts = NULL, *other = NULL;\n X509 *cert = NULL, *recip = NULL, *signer = NULL;\n X509_STORE *store = NULL;\n X509_VERIFY_PARAM *vpm = NULL;\n char *certfile = NULL, *keyfile = NULL, *contfile = NULL;\n const char *CAfile = NULL, *CApath = NULL;\n char *certsoutfile = NULL;\n int noCAfile = 0, noCApath = 0;\n char *infile = NULL, *outfile = NULL, *rctfile = NULL;\n char *passinarg = NULL, *passin = NULL, *signerfile = NULL, *recipfile = NULL;\n char *to = NULL, *from = NULL, *subject = NULL, *prog;\n cms_key_param *key_first = NULL, *key_param = NULL;\n int flags = CMS_DETACHED, noout = 0, print = 0, keyidx = -1, vpmtouched = 0;\n int informat = FORMAT_SMIME, outformat = FORMAT_SMIME;\n int operation = 0, ret = 1, rr_print = 0, rr_allorfirst = -1;\n int verify_retcode = 0, rctformat = FORMAT_SMIME, keyform = FORMAT_PEM;\n size_t secret_keylen = 0, secret_keyidlen = 0;\n unsigned char *pwri_pass = NULL, *pwri_tmp = NULL;\n unsigned char *secret_key = NULL, *secret_keyid = NULL;\n long ltmp;\n const char *mime_eol = "\\n";\n OPTION_CHOICE o;\n if ((vpm = X509_VERIFY_PARAM_new()) == NULL)\n return 1;\n prog = opt_init(argc, argv, cms_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(cms_options);\n ret = 0;\n goto end;\n case OPT_INFORM:\n if (!opt_format(opt_arg(), OPT_FMT_PDS, &informat))\n goto opthelp;\n break;\n case OPT_OUTFORM:\n if (!opt_format(opt_arg(), OPT_FMT_PDS, &outformat))\n goto opthelp;\n break;\n case OPT_OUT:\n outfile = opt_arg();\n break;\n case OPT_ENCRYPT:\n operation = SMIME_ENCRYPT;\n break;\n case OPT_DECRYPT:\n operation = SMIME_DECRYPT;\n break;\n case OPT_SIGN:\n operation = SMIME_SIGN;\n break;\n case OPT_SIGN_RECEIPT:\n operation = SMIME_SIGN_RECEIPT;\n break;\n case OPT_RESIGN:\n operation = SMIME_RESIGN;\n break;\n case OPT_VERIFY:\n operation = SMIME_VERIFY;\n break;\n case OPT_VERIFY_RETCODE:\n verify_retcode = 1;\n break;\n case OPT_VERIFY_RECEIPT:\n operation = SMIME_VERIFY_RECEIPT;\n rctfile = opt_arg();\n break;\n case OPT_CMSOUT:\n operation = SMIME_CMSOUT;\n break;\n case OPT_DATA_OUT:\n operation = SMIME_DATAOUT;\n break;\n case OPT_DATA_CREATE:\n operation = SMIME_DATA_CREATE;\n break;\n case OPT_DIGEST_VERIFY:\n operation = SMIME_DIGEST_VERIFY;\n break;\n case OPT_DIGEST_CREATE:\n operation = SMIME_DIGEST_CREATE;\n break;\n case OPT_COMPRESS:\n operation = SMIME_COMPRESS;\n break;\n case OPT_UNCOMPRESS:\n operation = SMIME_UNCOMPRESS;\n break;\n case OPT_ED_DECRYPT:\n operation = SMIME_ENCRYPTED_DECRYPT;\n break;\n case OPT_ED_ENCRYPT:\n operation = SMIME_ENCRYPTED_ENCRYPT;\n break;\n case OPT_DEBUG_DECRYPT:\n flags |= CMS_DEBUG_DECRYPT;\n break;\n case OPT_TEXT:\n flags |= CMS_TEXT;\n break;\n case OPT_ASCIICRLF:\n flags |= CMS_ASCIICRLF;\n break;\n case OPT_NOINTERN:\n flags |= CMS_NOINTERN;\n break;\n case OPT_NOVERIFY:\n flags |= CMS_NO_SIGNER_CERT_VERIFY;\n break;\n case OPT_NOCERTS:\n flags |= CMS_NOCERTS;\n break;\n case OPT_NOATTR:\n flags |= CMS_NOATTR;\n break;\n case OPT_NODETACH:\n flags &= ~CMS_DETACHED;\n break;\n case OPT_NOSMIMECAP:\n flags |= CMS_NOSMIMECAP;\n break;\n case OPT_BINARY:\n flags |= CMS_BINARY;\n break;\n case OPT_CADES:\n flags |= CMS_CADES;\n break;\n case OPT_KEYID:\n flags |= CMS_USE_KEYID;\n break;\n case OPT_NOSIGS:\n flags |= CMS_NOSIGS;\n break;\n case OPT_NO_CONTENT_VERIFY:\n flags |= CMS_NO_CONTENT_VERIFY;\n break;\n case OPT_NO_ATTR_VERIFY:\n flags |= CMS_NO_ATTR_VERIFY;\n break;\n case OPT_INDEF:\n flags |= CMS_STREAM;\n break;\n case OPT_NOINDEF:\n flags &= ~CMS_STREAM;\n break;\n case OPT_CRLFEOL:\n mime_eol = "\\r\\n";\n flags |= CMS_CRLFEOL;\n break;\n case OPT_NOOUT:\n noout = 1;\n break;\n case OPT_RR_PRINT:\n rr_print = 1;\n break;\n case OPT_RR_ALL:\n rr_allorfirst = 0;\n break;\n case OPT_RR_FIRST:\n rr_allorfirst = 1;\n break;\n case OPT_RCTFORM:\n if (rctformat == FORMAT_SMIME)\n rcms = SMIME_read_CMS(rctin, NULL);\n else if (rctformat == FORMAT_PEM)\n rcms = PEM_read_bio_CMS(rctin, NULL, NULL, NULL);\n else if (rctformat == FORMAT_ASN1)\n if (!opt_format(opt_arg(),\n OPT_FMT_PEMDER | OPT_FMT_SMIME, &rctformat))\n goto opthelp;\n break;\n case OPT_CERTFILE:\n certfile = opt_arg();\n break;\n case OPT_CAFILE:\n CAfile = opt_arg();\n break;\n case OPT_CAPATH:\n CApath = opt_arg();\n break;\n case OPT_NOCAFILE:\n noCAfile = 1;\n break;\n case OPT_NOCAPATH:\n noCApath = 1;\n break;\n case OPT_IN:\n infile = opt_arg();\n break;\n case OPT_CONTENT:\n contfile = opt_arg();\n break;\n case OPT_RR_FROM:\n if (rr_from == NULL\n && (rr_from = sk_OPENSSL_STRING_new_null()) == NULL)\n goto end;\n sk_OPENSSL_STRING_push(rr_from, opt_arg());\n break;\n case OPT_RR_TO:\n if (rr_to == NULL\n && (rr_to = sk_OPENSSL_STRING_new_null()) == NULL)\n goto end;\n sk_OPENSSL_STRING_push(rr_to, opt_arg());\n break;\n case OPT_PRINT:\n noout = print = 1;\n break;\n case OPT_SECRETKEY:\n if (secret_key != NULL) {\n BIO_printf(bio_err, "Invalid key (supplied twice) %s\\n",\n opt_arg());\n goto opthelp;\n }\n secret_key = OPENSSL_hexstr2buf(opt_arg(), <mp);\n if (secret_key == NULL) {\n BIO_printf(bio_err, "Invalid key %s\\n", opt_arg());\n goto end;\n }\n secret_keylen = (size_t)ltmp;\n break;\n case OPT_SECRETKEYID:\n if (secret_keyid != NULL) {\n BIO_printf(bio_err, "Invalid id (supplied twice) %s\\n",\n opt_arg());\n goto opthelp;\n }\n secret_keyid = OPENSSL_hexstr2buf(opt_arg(), <mp);\n if (secret_keyid == NULL) {\n BIO_printf(bio_err, "Invalid id %s\\n", opt_arg());\n goto opthelp;\n }\n secret_keyidlen = (size_t)ltmp;\n break;\n case OPT_PWRI_PASSWORD:\n pwri_pass = (unsigned char *)opt_arg();\n break;\n case OPT_ECONTENT_TYPE:\n if (econtent_type != NULL) {\n BIO_printf(bio_err, "Invalid OID (supplied twice) %s\\n",\n opt_arg());\n goto opthelp;\n }\n econtent_type = OBJ_txt2obj(opt_arg(), 0);\n if (econtent_type == NULL) {\n BIO_printf(bio_err, "Invalid OID %s\\n", opt_arg());\n goto opthelp;\n }\n break;\n case OPT_ENGINE:\n e = setup_engine(opt_arg(), 0);\n break;\n case OPT_PASSIN:\n passinarg = opt_arg();\n break;\n case OPT_TO:\n to = opt_arg();\n break;\n case OPT_FROM:\n from = opt_arg();\n break;\n case OPT_SUBJECT:\n subject = opt_arg();\n break;\n case OPT_CERTSOUT:\n certsoutfile = opt_arg();\n break;\n case OPT_MD:\n if (!opt_md(opt_arg(), &sign_md))\n goto end;\n break;\n case OPT_SIGNER:\n if (signerfile != NULL) {\n if (sksigners == NULL\n && (sksigners = sk_OPENSSL_STRING_new_null()) == NULL)\n goto end;\n sk_OPENSSL_STRING_push(sksigners, signerfile);\n if (keyfile == NULL)\n keyfile = signerfile;\n if (skkeys == NULL\n && (skkeys = sk_OPENSSL_STRING_new_null()) == NULL)\n goto end;\n sk_OPENSSL_STRING_push(skkeys, keyfile);\n keyfile = NULL;\n }\n signerfile = opt_arg();\n break;\n case OPT_INKEY:\n if (keyfile != NULL) {\n if (signerfile == NULL) {\n BIO_puts(bio_err, "Illegal -inkey without -signer\\n");\n goto end;\n }\n if (sksigners == NULL\n && (sksigners = sk_OPENSSL_STRING_new_null()) == NULL)\n goto end;\n sk_OPENSSL_STRING_push(sksigners, signerfile);\n signerfile = NULL;\n if (skkeys == NULL\n && (skkeys = sk_OPENSSL_STRING_new_null()) == NULL)\n goto end;\n sk_OPENSSL_STRING_push(skkeys, keyfile);\n }\n keyfile = opt_arg();\n break;\n case OPT_KEYFORM:\n if (!opt_format(opt_arg(), OPT_FMT_ANY, &keyform))\n goto opthelp;\n break;\n case OPT_RECIP:\n if (operation == SMIME_ENCRYPT) {\n if (encerts == NULL && (encerts = sk_X509_new_null()) == NULL)\n goto end;\n cert = load_cert(opt_arg(), FORMAT_PEM,\n "recipient certificate file");\n if (cert == NULL)\n goto end;\n sk_X509_push(encerts, cert);\n cert = NULL;\n } else {\n recipfile = opt_arg();\n }\n break;\n case OPT_CIPHER:\n if (!opt_cipher(opt_unknown(), &cipher))\n goto end;\n break;\n case OPT_KEYOPT:\n keyidx = -1;\n if (operation == SMIME_ENCRYPT) {\n if (encerts != NULL)\n keyidx += sk_X509_num(encerts);\n } else {\n if (keyfile != NULL || signerfile != NULL)\n keyidx++;\n if (skkeys != NULL)\n keyidx += sk_OPENSSL_STRING_num(skkeys);\n }\n if (keyidx < 0) {\n BIO_printf(bio_err, "No key specified\\n");\n goto opthelp;\n }\n if (key_param == NULL || key_param->idx != keyidx) {\n cms_key_param *nparam;\n nparam = app_malloc(sizeof(*nparam), "key param buffer");\n nparam->idx = keyidx;\n if ((nparam->param = sk_OPENSSL_STRING_new_null()) == NULL)\n goto end;\n nparam->next = NULL;\n if (key_first == NULL)\n key_first = nparam;\n else\n key_param->next = nparam;\n key_param = nparam;\n }\n sk_OPENSSL_STRING_push(key_param->param, opt_arg());\n break;\n case OPT_V_CASES:\n if (!opt_verify(o, vpm))\n goto end;\n vpmtouched++;\n break;\n case OPT_R_CASES:\n if (!opt_rand(o))\n goto end;\n break;\n case OPT_3DES_WRAP:\n# ifndef OPENSSL_NO_DES\n wrap_cipher = EVP_des_ede3_wrap();\n# endif\n break;\n case OPT_AES128_WRAP:\n wrap_cipher = EVP_aes_128_wrap();\n break;\n case OPT_AES192_WRAP:\n wrap_cipher = EVP_aes_192_wrap();\n break;\n case OPT_AES256_WRAP:\n wrap_cipher = EVP_aes_256_wrap();\n break;\n }\n }\n argc = opt_num_rest();\n argv = opt_rest();\n if ((rr_allorfirst != -1 || rr_from != NULL) && rr_to == NULL) {\n BIO_puts(bio_err, "No Signed Receipts Recipients\\n");\n goto opthelp;\n }\n if (!(operation & SMIME_SIGNERS) && (rr_to != NULL || rr_from != NULL)) {\n BIO_puts(bio_err, "Signed receipts only allowed with -sign\\n");\n goto opthelp;\n }\n if (!(operation & SMIME_SIGNERS) && (skkeys != NULL || sksigners != NULL)) {\n BIO_puts(bio_err, "Multiple signers or keys not allowed\\n");\n goto opthelp;\n }\n if (flags & CMS_CADES) {\n if (flags & CMS_NOATTR) {\n BIO_puts(bio_err, "Incompatible options: "\n "CAdES required signed attributes\\n");\n goto opthelp;\n }\n }\n if (operation & SMIME_SIGNERS) {\n if (keyfile != NULL && signerfile == NULL) {\n BIO_puts(bio_err, "Illegal -inkey without -signer\\n");\n goto opthelp;\n }\n if (signerfile != NULL) {\n if (sksigners == NULL\n && (sksigners = sk_OPENSSL_STRING_new_null()) == NULL)\n goto end;\n sk_OPENSSL_STRING_push(sksigners, signerfile);\n if (skkeys == NULL && (skkeys = sk_OPENSSL_STRING_new_null()) == NULL)\n goto end;\n if (keyfile == NULL)\n keyfile = signerfile;\n sk_OPENSSL_STRING_push(skkeys, keyfile);\n }\n if (sksigners == NULL) {\n BIO_printf(bio_err, "No signer certificate specified\\n");\n goto opthelp;\n }\n signerfile = NULL;\n keyfile = NULL;\n } else if (operation == SMIME_DECRYPT) {\n if (recipfile == NULL && keyfile == NULL\n && secret_key == NULL && pwri_pass == NULL) {\n BIO_printf(bio_err,\n "No recipient certificate or key specified\\n");\n goto opthelp;\n }\n } else if (operation == SMIME_ENCRYPT) {\n if (*argv == NULL && secret_key == NULL\n && pwri_pass == NULL && encerts == NULL) {\n BIO_printf(bio_err, "No recipient(s) certificate(s) specified\\n");\n goto opthelp;\n }\n } else if (!operation) {\n BIO_printf(bio_err, "No operation option (-encrypt|-decrypt|-sign|-verify|...) specified.\\n");\n goto opthelp;\n }\n if (!app_passwd(passinarg, NULL, &passin, NULL)) {\n BIO_printf(bio_err, "Error getting password\\n");\n goto end;\n }\n ret = 2;\n if (!(operation & SMIME_SIGNERS))\n flags &= ~CMS_DETACHED;\n if (!(operation & SMIME_OP))\n if (flags & CMS_BINARY)\n outformat = FORMAT_BINARY;\n if (!(operation & SMIME_IP))\n if (flags & CMS_BINARY)\n informat = FORMAT_BINARY;\n if (operation == SMIME_ENCRYPT) {\n if (!cipher) {\n# ifndef OPENSSL_NO_DES\n cipher = EVP_des_ede3_cbc();\n# else\n BIO_printf(bio_err, "No cipher selected\\n");\n goto end;\n# endif\n }\n if (secret_key && !secret_keyid) {\n BIO_printf(bio_err, "No secret key id\\n");\n goto end;\n }\n if (*argv && encerts == NULL)\n if ((encerts = sk_X509_new_null()) == NULL)\n goto end;\n while (*argv) {\n if ((cert = load_cert(*argv, FORMAT_PEM,\n "recipient certificate file")) == NULL)\n goto end;\n sk_X509_push(encerts, cert);\n cert = NULL;\n argv++;\n }\n }\n if (certfile != NULL) {\n if (!load_certs(certfile, &other, FORMAT_PEM, NULL,\n "certificate file")) {\n ERR_print_errors(bio_err);\n goto end;\n }\n }\n if (recipfile != NULL && (operation == SMIME_DECRYPT)) {\n if ((recip = load_cert(recipfile, FORMAT_PEM,\n "recipient certificate file")) == NULL) {\n ERR_print_errors(bio_err);\n goto end;\n }\n }\n if (operation == SMIME_SIGN_RECEIPT) {\n if ((signer = load_cert(signerfile, FORMAT_PEM,\n "receipt signer certificate file")) == NULL) {\n ERR_print_errors(bio_err);\n goto end;\n }\n }\n if (operation == SMIME_DECRYPT) {\n if (keyfile == NULL)\n keyfile = recipfile;\n } else if ((operation == SMIME_SIGN) || (operation == SMIME_SIGN_RECEIPT)) {\n if (keyfile == NULL)\n keyfile = signerfile;\n } else {\n keyfile = NULL;\n }\n if (keyfile != NULL) {\n key = load_key(keyfile, keyform, 0, passin, e, "signing key file");\n if (key == NULL)\n goto end;\n }\n in = bio_open_default(infile, \'r\', informat);\n if (in == NULL)\n goto end;\n if (operation & SMIME_IP) {\n if (informat == FORMAT_SMIME) {\n cms = SMIME_read_CMS(in, &indata);\n } else if (informat == FORMAT_PEM) {\n cms = PEM_read_bio_CMS(in, NULL, NULL, NULL);\n } else if (informat == FORMAT_ASN1) {\n cms = d2i_CMS_bio(in, NULL);\n } else {\n BIO_printf(bio_err, "Bad input format for CMS file\\n");\n goto end;\n }\n if (cms == NULL) {\n BIO_printf(bio_err, "Error reading S/MIME message\\n");\n goto end;\n }\n if (contfile != NULL) {\n BIO_free(indata);\n if ((indata = BIO_new_file(contfile, "rb")) == NULL) {\n BIO_printf(bio_err, "Can\'t read content file %s\\n", contfile);\n goto end;\n }\n }\n if (certsoutfile != NULL) {\n STACK_OF(X509) *allcerts;\n allcerts = CMS_get1_certs(cms);\n if (!save_certs(certsoutfile, allcerts)) {\n BIO_printf(bio_err,\n "Error writing certs to %s\\n", certsoutfile);\n ret = 5;\n goto end;\n }\n sk_X509_pop_free(allcerts, X509_free);\n }\n }\n if (rctfile != NULL) {\n char *rctmode = (rctformat == FORMAT_ASN1) ? "rb" : "r";\n if ((rctin = BIO_new_file(rctfile, rctmode)) == NULL) {\n BIO_printf(bio_err, "Can\'t open receipt file %s\\n", rctfile);\n goto end;\n }\n if (rctformat == FORMAT_SMIME) {\n rcms = SMIME_read_CMS(rctin, NULL);\n } else if (rctformat == FORMAT_PEM) {\n rcms = PEM_read_bio_CMS(rctin, NULL, NULL, NULL);\n } else if (rctformat == FORMAT_ASN1) {\n rcms = d2i_CMS_bio(rctin, NULL);\n } else {\n BIO_printf(bio_err, "Bad input format for receipt\\n");\n goto end;\n }\n if (rcms == NULL) {\n BIO_printf(bio_err, "Error reading receipt\\n");\n goto end;\n }\n }\n out = bio_open_default(outfile, \'w\', outformat);\n if (out == NULL)\n goto end;\n if ((operation == SMIME_VERIFY) || (operation == SMIME_VERIFY_RECEIPT)) {\n if ((store = setup_verify(CAfile, CApath, noCAfile, noCApath)) == NULL)\n goto end;\n X509_STORE_set_verify_cb(store, cms_cb);\n if (vpmtouched)\n X509_STORE_set1_param(store, vpm);\n }\n ret = 3;\n if (operation == SMIME_DATA_CREATE) {\n cms = CMS_data_create(in, flags);\n } else if (operation == SMIME_DIGEST_CREATE) {\n cms = CMS_digest_create(in, sign_md, flags);\n } else if (operation == SMIME_COMPRESS) {\n cms = CMS_compress(in, -1, flags);\n } else if (operation == SMIME_ENCRYPT) {\n int i;\n flags |= CMS_PARTIAL;\n cms = CMS_encrypt(NULL, in, cipher, flags);\n if (cms == NULL)\n goto end;\n for (i = 0; i < sk_X509_num(encerts); i++) {\n CMS_RecipientInfo *ri;\n cms_key_param *kparam;\n int tflags = flags;\n X509 *x = sk_X509_value(encerts, i);\n for (kparam = key_first; kparam; kparam = kparam->next) {\n if (kparam->idx == i) {\n tflags |= CMS_KEY_PARAM;\n break;\n }\n }\n ri = CMS_add1_recipient_cert(cms, x, tflags);\n if (ri == NULL)\n goto end;\n if (kparam != NULL) {\n EVP_PKEY_CTX *pctx;\n pctx = CMS_RecipientInfo_get0_pkey_ctx(ri);\n if (!cms_set_pkey_param(pctx, kparam->param))\n goto end;\n }\n if (CMS_RecipientInfo_type(ri) == CMS_RECIPINFO_AGREE\n && wrap_cipher) {\n EVP_CIPHER_CTX *wctx;\n wctx = CMS_RecipientInfo_kari_get0_ctx(ri);\n EVP_EncryptInit_ex(wctx, wrap_cipher, NULL, NULL, NULL);\n }\n }\n if (secret_key != NULL) {\n if (!CMS_add0_recipient_key(cms, NID_undef,\n secret_key, secret_keylen,\n secret_keyid, secret_keyidlen,\n NULL, NULL, NULL))\n goto end;\n secret_key = NULL;\n secret_keyid = NULL;\n }\n if (pwri_pass != NULL) {\n pwri_tmp = (unsigned char *)OPENSSL_strdup((char *)pwri_pass);\n if (pwri_tmp == NULL)\n goto end;\n if (CMS_add0_recipient_password(cms,\n -1, NID_undef, NID_undef,\n pwri_tmp, -1, NULL) == NULL)\n goto end;\n pwri_tmp = NULL;\n }\n if (!(flags & CMS_STREAM)) {\n if (!CMS_final(cms, in, NULL, flags))\n goto end;\n }\n } else if (operation == SMIME_ENCRYPTED_ENCRYPT) {\n cms = CMS_EncryptedData_encrypt(in, cipher,\n secret_key, secret_keylen, flags);\n } else if (operation == SMIME_SIGN_RECEIPT) {\n CMS_ContentInfo *srcms = NULL;\n STACK_OF(CMS_SignerInfo) *sis;\n CMS_SignerInfo *si;\n sis = CMS_get0_SignerInfos(cms);\n if (sis == NULL)\n goto end;\n si = sk_CMS_SignerInfo_value(sis, 0);\n srcms = CMS_sign_receipt(si, signer, key, other, flags);\n if (srcms == NULL)\n goto end;\n CMS_ContentInfo_free(cms);\n cms = srcms;\n } else if (operation & SMIME_SIGNERS) {\n int i;\n if (operation == SMIME_SIGN) {\n if (flags & CMS_DETACHED) {\n if (outformat == FORMAT_SMIME)\n flags |= CMS_STREAM;\n }\n flags |= CMS_PARTIAL;\n cms = CMS_sign(NULL, NULL, other, in, flags);\n if (cms == NULL)\n goto end;\n if (econtent_type != NULL)\n CMS_set1_eContentType(cms, econtent_type);\n if (rr_to != NULL) {\n rr = make_receipt_request(rr_to, rr_allorfirst, rr_from);\n if (rr == NULL) {\n BIO_puts(bio_err,\n "Signed Receipt Request Creation Error\\n");\n goto end;\n }\n }\n } else {\n flags |= CMS_REUSE_DIGEST;\n }\n for (i = 0; i < sk_OPENSSL_STRING_num(sksigners); i++) {\n CMS_SignerInfo *si;\n cms_key_param *kparam;\n int tflags = flags;\n signerfile = sk_OPENSSL_STRING_value(sksigners, i);\n keyfile = sk_OPENSSL_STRING_value(skkeys, i);\n signer = load_cert(signerfile, FORMAT_PEM, "signer certificate");\n if (signer == NULL) {\n ret = 2;\n goto end;\n }\n key = load_key(keyfile, keyform, 0, passin, e, "signing key file");\n if (key == NULL) {\n ret = 2;\n goto end;\n }\n for (kparam = key_first; kparam; kparam = kparam->next) {\n if (kparam->idx == i) {\n tflags |= CMS_KEY_PARAM;\n break;\n }\n }\n si = CMS_add1_signer(cms, signer, key, sign_md, tflags);\n if (si == NULL)\n goto end;\n if (kparam != NULL) {\n EVP_PKEY_CTX *pctx;\n pctx = CMS_SignerInfo_get0_pkey_ctx(si);\n if (!cms_set_pkey_param(pctx, kparam->param))\n goto end;\n }\n if (rr != NULL && !CMS_add1_ReceiptRequest(si, rr))\n goto end;\n X509_free(signer);\n signer = NULL;\n EVP_PKEY_free(key);\n key = NULL;\n }\n if ((operation == SMIME_SIGN) && !(flags & CMS_STREAM)) {\n if (!CMS_final(cms, in, NULL, flags))\n goto end;\n }\n }\n if (cms == NULL) {\n BIO_printf(bio_err, "Error creating CMS structure\\n");\n goto end;\n }\n ret = 4;\n if (operation == SMIME_DECRYPT) {\n if (flags & CMS_DEBUG_DECRYPT)\n CMS_decrypt(cms, NULL, NULL, NULL, NULL, flags);\n if (secret_key != NULL) {\n if (!CMS_decrypt_set1_key(cms,\n secret_key, secret_keylen,\n secret_keyid, secret_keyidlen)) {\n BIO_puts(bio_err, "Error decrypting CMS using secret key\\n");\n goto end;\n }\n }\n if (key != NULL) {\n if (!CMS_decrypt_set1_pkey(cms, key, recip)) {\n BIO_puts(bio_err, "Error decrypting CMS using private key\\n");\n goto end;\n }\n }\n if (pwri_pass != NULL) {\n if (!CMS_decrypt_set1_password(cms, pwri_pass, -1)) {\n BIO_puts(bio_err, "Error decrypting CMS using password\\n");\n goto end;\n }\n }\n if (!CMS_decrypt(cms, NULL, NULL, indata, out, flags)) {\n BIO_printf(bio_err, "Error decrypting CMS structure\\n");\n goto end;\n }\n } else if (operation == SMIME_DATAOUT) {\n if (!CMS_data(cms, out, flags))\n goto end;\n } else if (operation == SMIME_UNCOMPRESS) {\n if (!CMS_uncompress(cms, indata, out, flags))\n goto end;\n } else if (operation == SMIME_DIGEST_VERIFY) {\n if (CMS_digest_verify(cms, indata, out, flags) > 0) {\n BIO_printf(bio_err, "Verification successful\\n");\n } else {\n BIO_printf(bio_err, "Verification failure\\n");\n goto end;\n }\n } else if (operation == SMIME_ENCRYPTED_DECRYPT) {\n if (!CMS_EncryptedData_decrypt(cms, secret_key, secret_keylen,\n indata, out, flags))\n goto end;\n } else if (operation == SMIME_VERIFY) {\n if (CMS_verify(cms, other, store, indata, out, flags) > 0) {\n BIO_printf(bio_err, "Verification successful\\n");\n } else {\n BIO_printf(bio_err, "Verification failure\\n");\n if (verify_retcode)\n ret = verify_err + 32;\n goto end;\n }\n if (signerfile != NULL) {\n STACK_OF(X509) *signers;\n signers = CMS_get0_signers(cms);\n if (!save_certs(signerfile, signers)) {\n BIO_printf(bio_err,\n "Error writing signers to %s\\n", signerfile);\n ret = 5;\n goto end;\n }\n sk_X509_free(signers);\n }\n if (rr_print)\n receipt_request_print(cms);\n } else if (operation == SMIME_VERIFY_RECEIPT) {\n if (CMS_verify_receipt(rcms, cms, other, store, flags) > 0) {\n BIO_printf(bio_err, "Verification successful\\n");\n } else {\n BIO_printf(bio_err, "Verification failure\\n");\n goto end;\n }\n } else {\n if (noout) {\n if (print)\n CMS_ContentInfo_print_ctx(out, cms, 0, NULL);\n } else if (outformat == FORMAT_SMIME) {\n if (to)\n BIO_printf(out, "To: %s%s", to, mime_eol);\n if (from)\n BIO_printf(out, "From: %s%s", from, mime_eol);\n if (subject)\n BIO_printf(out, "Subject: %s%s", subject, mime_eol);\n if (operation == SMIME_RESIGN)\n ret = SMIME_write_CMS(out, cms, indata, flags);\n else\n ret = SMIME_write_CMS(out, cms, in, flags);\n } else if (outformat == FORMAT_PEM) {\n ret = PEM_write_bio_CMS_stream(out, cms, in, flags);\n } else if (outformat == FORMAT_ASN1) {\n ret = i2d_CMS_bio_stream(out, cms, in, flags);\n } else {\n BIO_printf(bio_err, "Bad output format for CMS file\\n");\n goto end;\n }\n if (ret <= 0) {\n ret = 6;\n goto end;\n }\n }\n ret = 0;\n end:\n if (ret)\n ERR_print_errors(bio_err);\n sk_X509_pop_free(encerts, X509_free);\n sk_X509_pop_free(other, X509_free);\n X509_VERIFY_PARAM_free(vpm);\n sk_OPENSSL_STRING_free(sksigners);\n sk_OPENSSL_STRING_free(skkeys);\n OPENSSL_free(secret_key);\n OPENSSL_free(secret_keyid);\n OPENSSL_free(pwri_tmp);\n ASN1_OBJECT_free(econtent_type);\n CMS_ReceiptRequest_free(rr);\n sk_OPENSSL_STRING_free(rr_to);\n sk_OPENSSL_STRING_free(rr_from);\n for (key_param = key_first; key_param;) {\n cms_key_param *tparam;\n sk_OPENSSL_STRING_free(key_param->param);\n tparam = key_param->next;\n OPENSSL_free(key_param);\n key_param = tparam;\n }\n X509_STORE_free(store);\n X509_free(cert);\n X509_free(recip);\n X509_free(signer);\n EVP_PKEY_free(key);\n CMS_ContentInfo_free(cms);\n CMS_ContentInfo_free(rcms);\n release_engine(e);\n BIO_free(rctin);\n BIO_free(in);\n BIO_free(indata);\n BIO_free_all(out);\n OPENSSL_free(passin);\n return ret;\n}', 'DEFINE_STACK_OF(X509)', 'int OPENSSL_sk_push(OPENSSL_STACK *st, const void *data)\n{\n if (st == NULL)\n return -1;\n return OPENSSL_sk_insert(st, data, st->num);\n}', 'int OPENSSL_sk_insert(OPENSSL_STACK *st, const void *data, int loc)\n{\n if (st == NULL || st->num == max_nodes)\n return 0;\n if (!sk_reserve(st, 1, 0))\n return 0;\n if ((loc >= st->num) || (loc < 0)) {\n st->data[st->num] = data;\n } else {\n memmove(&st->data[loc + 1], &st->data[loc],\n sizeof(st->data[0]) * (st->num - loc));\n st->data[loc] = data;\n }\n st->num++;\n st->sorted = 0;\n return st->num;\n}', 'static int sk_reserve(OPENSSL_STACK *st, int n, int exact)\n{\n const void **tmpdata;\n int num_alloc;\n if (n > max_nodes - st->num)\n return 0;\n num_alloc = st->num + n;\n if (num_alloc < min_nodes)\n num_alloc = min_nodes;\n if (st->data == NULL) {\n if ((st->data = OPENSSL_zalloc(sizeof(void *) * num_alloc)) == NULL) {\n CRYPTOerr(CRYPTO_F_SK_RESERVE, ERR_R_MALLOC_FAILURE);\n return 0;\n }\n st->num_alloc = num_alloc;\n return 1;\n }\n if (!exact) {\n if (num_alloc <= st->num_alloc)\n return 1;\n num_alloc = compute_growth(num_alloc, st->num_alloc);\n if (num_alloc == 0)\n return 0;\n } else if (num_alloc == st->num_alloc) {\n return 1;\n }\n tmpdata = OPENSSL_realloc((void *)st->data, sizeof(void *) * num_alloc);\n if (tmpdata == NULL)\n return 0;\n st->data = tmpdata;\n st->num_alloc = num_alloc;\n return 1;\n}', 'void *CRYPTO_realloc(void *str, size_t num, const char *file, int line)\n{\n INCREMENT(realloc_count);\n if (realloc_impl != NULL && realloc_impl != &CRYPTO_realloc)\n return realloc_impl(str, num, file, line);\n FAILTEST();\n if (str == NULL)\n return CRYPTO_malloc(num, file, line);\n if (num == 0) {\n CRYPTO_free(str, file, line);\n return NULL;\n }\n#if !defined(OPENSSL_NO_CRYPTO_MDEBUG) && !defined(FIPS_MODE)\n if (call_malloc_debug) {\n void *ret;\n CRYPTO_mem_debug_realloc(str, NULL, num, 0, file, line);\n ret = realloc(str, num);\n CRYPTO_mem_debug_realloc(str, ret, num, 1, file, line);\n return ret;\n }\n#else\n (void)(file); (void)(line);\n#endif\n return realloc(str, num);\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#if !defined(OPENSSL_NO_CRYPTO_MDEBUG) && !defined(FIPS_MODE)\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}'] |
35,140 | 0 | https://github.com/openssl/openssl/blob/507db4c5313288d55eeb8434b0111201ba363b28/include/internal/constant_time_locl.h/#L140 | static inline unsigned int constant_time_lt(unsigned int a, unsigned int b)
{
return constant_time_msb(a ^ ((a ^ b) | ((a - b) ^ b)));
} | ['int dtls1_get_record(SSL *s)\n{\n int ssl_major, ssl_minor;\n int i, n;\n SSL3_RECORD *rr;\n unsigned char *p = NULL;\n unsigned short version;\n DTLS1_BITMAP *bitmap;\n unsigned int is_next_epoch;\n rr = RECORD_LAYER_get_rrec(&s->rlayer);\n if (dtls1_process_buffered_records(s) < 0)\n return -1;\n if (dtls1_get_processed_record(s))\n return 1;\n again:\n if ((RECORD_LAYER_get_rstate(&s->rlayer) != SSL_ST_READ_BODY) ||\n (RECORD_LAYER_get_packet_length(&s->rlayer) < DTLS1_RT_HEADER_LENGTH)) {\n n = ssl3_read_n(s, DTLS1_RT_HEADER_LENGTH,\n SSL3_BUFFER_get_len(&s->rlayer.rbuf), 0);\n if (n <= 0)\n return (n);\n if (RECORD_LAYER_get_packet_length(&s->rlayer) != DTLS1_RT_HEADER_LENGTH) {\n RECORD_LAYER_reset_packet_length(&s->rlayer);\n goto again;\n }\n RECORD_LAYER_set_rstate(&s->rlayer, SSL_ST_READ_BODY);\n p = RECORD_LAYER_get_packet(&s->rlayer);\n if (s->msg_callback)\n s->msg_callback(0, 0, SSL3_RT_HEADER, p, DTLS1_RT_HEADER_LENGTH,\n s, s->msg_callback_arg);\n rr->type = *(p++);\n ssl_major = *(p++);\n ssl_minor = *(p++);\n version = (ssl_major << 8) | ssl_minor;\n n2s(p, rr->epoch);\n memcpy(&(RECORD_LAYER_get_read_sequence(&s->rlayer)[2]), p, 6);\n p += 6;\n n2s(p, rr->length);\n if (!s->first_packet) {\n if (version != s->version) {\n rr->length = 0;\n RECORD_LAYER_reset_packet_length(&s->rlayer);\n goto again;\n }\n }\n if ((version & 0xff00) != (s->version & 0xff00)) {\n rr->length = 0;\n RECORD_LAYER_reset_packet_length(&s->rlayer);\n goto again;\n }\n if (rr->length > SSL3_RT_MAX_ENCRYPTED_LENGTH) {\n rr->length = 0;\n RECORD_LAYER_reset_packet_length(&s->rlayer);\n goto again;\n }\n }\n if (rr->length >\n RECORD_LAYER_get_packet_length(&s->rlayer) - DTLS1_RT_HEADER_LENGTH) {\n i = rr->length;\n n = ssl3_read_n(s, i, i, 1);\n if (n != i) {\n rr->length = 0;\n RECORD_LAYER_reset_packet_length(&s->rlayer);\n goto again;\n }\n }\n RECORD_LAYER_set_rstate(&s->rlayer, SSL_ST_READ_HEADER);\n bitmap = dtls1_get_bitmap(s, rr, &is_next_epoch);\n if (bitmap == NULL) {\n rr->length = 0;\n RECORD_LAYER_reset_packet_length(&s->rlayer);\n goto again;\n }\n#ifndef OPENSSL_NO_SCTP\n if (!BIO_dgram_is_sctp(SSL_get_rbio(s))) {\n#endif\n if (!dtls1_record_replay_check(s, bitmap)) {\n rr->length = 0;\n RECORD_LAYER_reset_packet_length(&s->rlayer);\n goto again;\n }\n#ifndef OPENSSL_NO_SCTP\n }\n#endif\n if (rr->length == 0)\n goto again;\n if (is_next_epoch) {\n if ((SSL_in_init(s) || ossl_statem_get_in_handshake(s))) {\n if (dtls1_buffer_record\n (s, &(DTLS_RECORD_LAYER_get_unprocessed_rcds(&s->rlayer)),\n rr->seq_num) < 0)\n return -1;\n dtls1_record_bitmap_update(s, bitmap);\n }\n rr->length = 0;\n RECORD_LAYER_reset_packet_length(&s->rlayer);\n goto again;\n }\n if (!dtls1_process_record(s)) {\n rr->length = 0;\n RECORD_LAYER_reset_packet_length(&s->rlayer);\n goto again;\n }\n dtls1_record_bitmap_update(s, bitmap);\n return (1);\n}', 'int dtls1_process_record(SSL *s)\n{\n int i, al;\n int enc_err;\n SSL_SESSION *sess;\n SSL3_RECORD *rr;\n unsigned int mac_size;\n unsigned char md[EVP_MAX_MD_SIZE];\n rr = RECORD_LAYER_get_rrec(&s->rlayer);\n sess = s->session;\n rr->input = &(RECORD_LAYER_get_packet(&s->rlayer)[DTLS1_RT_HEADER_LENGTH]);\n if (rr->length > SSL3_RT_MAX_ENCRYPTED_LENGTH) {\n al = SSL_AD_RECORD_OVERFLOW;\n SSLerr(SSL_F_DTLS1_PROCESS_RECORD, SSL_R_ENCRYPTED_LENGTH_TOO_LONG);\n goto f_err;\n }\n rr->data = rr->input;\n rr->orig_len = rr->length;\n enc_err = s->method->ssl3_enc->enc(s, 0);\n if (enc_err == 0) {\n rr->length = 0;\n RECORD_LAYER_reset_packet_length(&s->rlayer);\n goto err;\n }\n#ifdef TLS_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) && (EVP_MD_CTX_md(s->read_hash) != NULL)) {\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 if (rr->orig_len < mac_size ||\n (EVP_CIPHER_CTX_mode(s->enc_read_ctx) == EVP_CIPH_CBC_MODE &&\n rr->orig_len < mac_size + 1)) {\n al = SSL_AD_DECODE_ERROR;\n SSLerr(SSL_F_DTLS1_PROCESS_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, mac_size);\n rr->length -= mac_size;\n } else {\n rr->length -= mac_size;\n mac = &rr->data[rr->length];\n }\n i = s->method->ssl3_enc->mac(s, 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 if (enc_err < 0) {\n rr->length = 0;\n RECORD_LAYER_reset_packet_length(&s->rlayer);\n goto err;\n }\n if (s->expand != NULL) {\n if (rr->length > SSL3_RT_MAX_COMPRESSED_LENGTH) {\n al = SSL_AD_RECORD_OVERFLOW;\n SSLerr(SSL_F_DTLS1_PROCESS_RECORD,\n SSL_R_COMPRESSED_LENGTH_TOO_LONG);\n goto f_err;\n }\n if (!ssl3_do_uncompress(s)) {\n al = SSL_AD_DECOMPRESSION_FAILURE;\n SSLerr(SSL_F_DTLS1_PROCESS_RECORD, SSL_R_BAD_DECOMPRESSION);\n goto f_err;\n }\n }\n if (rr->length > SSL3_RT_MAX_PLAIN_LENGTH) {\n al = SSL_AD_RECORD_OVERFLOW;\n SSLerr(SSL_F_DTLS1_PROCESS_RECORD, SSL_R_DATA_LENGTH_TOO_LONG);\n goto f_err;\n }\n rr->off = 0;\n RECORD_LAYER_reset_packet_length(&s->rlayer);\n return (1);\n f_err:\n ssl3_send_alert(s, SSL3_AL_FATAL, al);\n err:\n return (0);\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}', 'static inline unsigned int constant_time_lt(unsigned int a, unsigned int b)\n{\n return constant_time_msb(a ^ ((a ^ b) | ((a - b) ^ b)));\n}'] |
35,141 | 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 bn_probable_prime_dh(BIGNUM *rnd, int bits,\n const BIGNUM *add, const BIGNUM *rem, BN_CTX *ctx)\n{\n int i, ret = 0;\n BIGNUM *t1;\n BN_CTX_start(ctx);\n if ((t1 = BN_CTX_get(ctx)) == NULL)\n goto err;\n if (!BN_rand(rnd, bits, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ODD))\n goto err;\n if (!BN_mod(t1, rnd, add, ctx))\n goto err;\n if (!BN_sub(rnd, rnd, t1))\n goto err;\n if (rem == NULL) {\n if (!BN_add_word(rnd, 1))\n goto err;\n } else {\n if (!BN_add(rnd, rnd, rem))\n goto err;\n }\n loop:\n for (i = 1; i < NUMPRIMES; i++) {\n BN_ULONG mod = BN_mod_word(rnd, (BN_ULONG)primes[i]);\n if (mod == (BN_ULONG)-1)\n goto err;\n if (mod <= 1) {\n if (!BN_add(rnd, rnd, add))\n goto err;\n goto loop;\n }\n }\n ret = 1;\n err:\n BN_CTX_end(ctx);\n bn_check_top(rnd);\n return (ret);\n}', 'int BN_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}', '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}'] |
35,142 | 0 | https://github.com/openssl/openssl/blob/db26ec80873e8faa8d40795120bc15c37d36ac98/crypto/bn/bn_lcl.h/#L662 | static ossl_inline BIGNUM *bn_expand(BIGNUM *a, int bits)
{
if (bits > (INT_MAX - BN_BITS2 + 1))
return NULL;
if (((bits+BN_BITS2-1)/BN_BITS2) <= (a)->dmax)
return a;
return bn_expand2((a),(bits+BN_BITS2-1)/BN_BITS2);
} | ["int BN_hex2bn(BIGNUM **bn, const char *a)\n{\n BIGNUM *ret = NULL;\n BN_ULONG l = 0;\n int neg = 0, h, m, i, j, k, c;\n int num;\n if (a == NULL || *a == '\\0')\n return 0;\n if (*a == '-') {\n neg = 1;\n a++;\n }\n for (i = 0; i <= INT_MAX / 4 && ossl_isxdigit(a[i]); i++)\n continue;\n if (i == 0 || i > INT_MAX / 4)\n goto err;\n num = i + neg;\n if (bn == NULL)\n return num;\n if (*bn == NULL) {\n if ((ret = BN_new()) == NULL)\n return 0;\n } else {\n ret = *bn;\n BN_zero(ret);\n }\n if (bn_expand(ret, i * 4) == NULL)\n goto err;\n j = i;\n m = 0;\n h = 0;\n while (j > 0) {\n m = (BN_BYTES * 2 <= j) ? BN_BYTES * 2 : j;\n l = 0;\n for (;;) {\n c = a[j - m];\n k = OPENSSL_hexchar2int(c);\n if (k < 0)\n k = 0;\n l = (l << 4) | k;\n if (--m <= 0) {\n ret->d[h++] = l;\n break;\n }\n }\n j -= BN_BYTES * 2;\n }\n ret->top = h;\n bn_correct_top(ret);\n *bn = ret;\n bn_check_top(ret);\n if (ret->top != 0)\n ret->neg = neg;\n return num;\n err:\n if (*bn == NULL)\n BN_free(ret);\n return 0;\n}", '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}'] |
35,143 | 0 | https://github.com/openssl/openssl/blob/ee3a6c646ff8ea6b9ada5a58f4a0e7c9b7be944b/crypto/bn/bn_ctx.c/#L319 | static unsigned int BN_STACK_pop(BN_STACK *st)
{
return st->indexes[--(st->depth)];
} | ['int BN_mod_exp_mont_consttime(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p,\n const BIGNUM *m, BN_CTX *ctx,\n BN_MONT_CTX *in_mont)\n{\n int i, bits, ret = 0, window, wvalue;\n int top;\n BN_MONT_CTX *mont = NULL;\n int numPowers;\n unsigned char *powerbufFree = NULL;\n int powerbufLen = 0;\n unsigned char *powerbuf = NULL;\n BIGNUM tmp, am;\n#if defined(SPARC_T4_MONT)\n unsigned int t4 = 0;\n#endif\n bn_check_top(a);\n bn_check_top(p);\n bn_check_top(m);\n if (!BN_is_odd(m)) {\n BNerr(BN_F_BN_MOD_EXP_MONT_CONSTTIME, BN_R_CALLED_WITH_EVEN_MODULUS);\n return (0);\n }\n top = m->top;\n bits = BN_num_bits(p);\n if (bits == 0) {\n if (BN_is_one(m)) {\n ret = 1;\n BN_zero(rr);\n } else {\n ret = BN_one(rr);\n }\n return ret;\n }\n BN_CTX_start(ctx);\n if (in_mont != NULL)\n mont = in_mont;\n else {\n if ((mont = BN_MONT_CTX_new()) == NULL)\n goto err;\n if (!BN_MONT_CTX_set(mont, m, ctx))\n goto err;\n }\n#ifdef RSAZ_ENABLED\n if ((16 == a->top) && (16 == p->top) && (BN_num_bits(m) == 1024)\n && rsaz_avx2_eligible()) {\n if (NULL == bn_wexpand(rr, 16))\n goto err;\n RSAZ_1024_mod_exp_avx2(rr->d, a->d, p->d, m->d, mont->RR.d,\n mont->n0[0]);\n rr->top = 16;\n rr->neg = 0;\n bn_correct_top(rr);\n ret = 1;\n goto err;\n } else if ((8 == a->top) && (8 == p->top) && (BN_num_bits(m) == 512)) {\n if (NULL == bn_wexpand(rr, 8))\n goto err;\n RSAZ_512_mod_exp(rr->d, a->d, p->d, m->d, mont->n0[0], mont->RR.d);\n rr->top = 8;\n rr->neg = 0;\n bn_correct_top(rr);\n ret = 1;\n goto err;\n }\n#endif\n window = BN_window_bits_for_ctime_exponent_size(bits);\n#if defined(SPARC_T4_MONT)\n if (window >= 5 && (top & 15) == 0 && top <= 64 &&\n (OPENSSL_sparcv9cap_P[1] & (CFR_MONTMUL | CFR_MONTSQR)) ==\n (CFR_MONTMUL | CFR_MONTSQR) && (t4 = OPENSSL_sparcv9cap_P[0]))\n window = 5;\n else\n#endif\n#if defined(OPENSSL_BN_ASM_MONT5)\n if (window >= 5) {\n window = 5;\n powerbufLen += top * sizeof(mont->N.d[0]);\n }\n#endif\n (void)0;\n numPowers = 1 << window;\n powerbufLen += sizeof(m->d[0]) * (top * numPowers +\n ((2 * top) >\n numPowers ? (2 * top) : numPowers));\n#ifdef alloca\n if (powerbufLen < 3072)\n powerbufFree =\n alloca(powerbufLen + MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH);\n else\n#endif\n if ((powerbufFree =\n OPENSSL_malloc(powerbufLen + MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH))\n == NULL)\n goto err;\n powerbuf = MOD_EXP_CTIME_ALIGN(powerbufFree);\n memset(powerbuf, 0, powerbufLen);\n#ifdef alloca\n if (powerbufLen < 3072)\n powerbufFree = NULL;\n#endif\n tmp.d = (BN_ULONG *)(powerbuf + sizeof(m->d[0]) * top * numPowers);\n am.d = tmp.d + top;\n tmp.top = am.top = 0;\n tmp.dmax = am.dmax = top;\n tmp.neg = am.neg = 0;\n tmp.flags = am.flags = BN_FLG_STATIC_DATA;\n#if 1\n if (m->d[top - 1] & (((BN_ULONG)1) << (BN_BITS2 - 1))) {\n tmp.d[0] = (0 - m->d[0]) & BN_MASK2;\n for (i = 1; i < top; i++)\n tmp.d[i] = (~m->d[i]) & BN_MASK2;\n tmp.top = top;\n } else\n#endif\n if (!BN_to_montgomery(&tmp, BN_value_one(), mont, ctx))\n goto err;\n if (a->neg || BN_ucmp(a, m) >= 0) {\n if (!BN_mod(&am, a, m, ctx))\n goto err;\n if (!BN_to_montgomery(&am, &am, mont, ctx))\n goto err;\n } else if (!BN_to_montgomery(&am, a, mont, ctx))\n goto err;\n#if defined(SPARC_T4_MONT)\n if (t4) {\n typedef int (*bn_pwr5_mont_f) (BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n int bn_pwr5_mont_t4_8(BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n int bn_pwr5_mont_t4_16(BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n int bn_pwr5_mont_t4_24(BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n int bn_pwr5_mont_t4_32(BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n static const bn_pwr5_mont_f pwr5_funcs[4] = {\n bn_pwr5_mont_t4_8, bn_pwr5_mont_t4_16,\n bn_pwr5_mont_t4_24, bn_pwr5_mont_t4_32\n };\n bn_pwr5_mont_f pwr5_worker = pwr5_funcs[top / 16 - 1];\n typedef int (*bn_mul_mont_f) (BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0);\n int bn_mul_mont_t4_8(BN_ULONG *rp, const BN_ULONG *ap, const void *bp,\n const BN_ULONG *np, const BN_ULONG *n0);\n int bn_mul_mont_t4_16(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0);\n int bn_mul_mont_t4_24(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0);\n int bn_mul_mont_t4_32(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0);\n static const bn_mul_mont_f mul_funcs[4] = {\n bn_mul_mont_t4_8, bn_mul_mont_t4_16,\n bn_mul_mont_t4_24, bn_mul_mont_t4_32\n };\n bn_mul_mont_f mul_worker = mul_funcs[top / 16 - 1];\n void bn_mul_mont_vis3(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0, int num);\n void bn_mul_mont_t4(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0, int num);\n void bn_mul_mont_gather5_t4(BN_ULONG *rp, const BN_ULONG *ap,\n const void *table, const BN_ULONG *np,\n const BN_ULONG *n0, int num, int power);\n void bn_flip_n_scatter5_t4(const BN_ULONG *inp, size_t num,\n void *table, size_t power);\n void bn_gather5_t4(BN_ULONG *out, size_t num,\n void *table, size_t power);\n void bn_flip_t4(BN_ULONG *dst, BN_ULONG *src, size_t num);\n BN_ULONG *np = mont->N.d, *n0 = mont->n0;\n int stride = 5 * (6 - (top / 16 - 1));\n for (i = am.top; i < top; i++)\n am.d[i] = 0;\n for (i = tmp.top; i < top; i++)\n tmp.d[i] = 0;\n bn_flip_n_scatter5_t4(tmp.d, top, powerbuf, 0);\n bn_flip_n_scatter5_t4(am.d, top, powerbuf, 1);\n if (!(*mul_worker) (tmp.d, am.d, am.d, np, n0) &&\n !(*mul_worker) (tmp.d, am.d, am.d, np, n0))\n bn_mul_mont_vis3(tmp.d, am.d, am.d, np, n0, top);\n bn_flip_n_scatter5_t4(tmp.d, top, powerbuf, 2);\n for (i = 3; i < 32; i++) {\n if (!(*mul_worker) (tmp.d, tmp.d, am.d, np, n0) &&\n !(*mul_worker) (tmp.d, tmp.d, am.d, np, n0))\n bn_mul_mont_vis3(tmp.d, tmp.d, am.d, np, n0, top);\n bn_flip_n_scatter5_t4(tmp.d, top, powerbuf, i);\n }\n np = alloca(top * sizeof(BN_ULONG));\n top /= 2;\n bn_flip_t4(np, mont->N.d, top);\n bits--;\n for (wvalue = 0, i = bits % 5; i >= 0; i--, bits--)\n wvalue = (wvalue << 1) + BN_is_bit_set(p, bits);\n bn_gather5_t4(tmp.d, top, powerbuf, wvalue);\n while (bits >= 0) {\n if (bits < stride)\n stride = bits + 1;\n bits -= stride;\n wvalue = bn_get_bits(p, bits + 1);\n if ((*pwr5_worker) (tmp.d, np, n0, powerbuf, wvalue, stride))\n continue;\n if ((*pwr5_worker) (tmp.d, np, n0, powerbuf, wvalue, stride))\n continue;\n bits += stride - 5;\n wvalue >>= stride - 5;\n wvalue &= 31;\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_gather5_t4(tmp.d, tmp.d, powerbuf, np, n0, top,\n wvalue);\n }\n bn_flip_t4(tmp.d, tmp.d, top);\n top *= 2;\n tmp.top = top;\n bn_correct_top(&tmp);\n OPENSSL_cleanse(np, top * sizeof(BN_ULONG));\n } else\n#endif\n#if defined(OPENSSL_BN_ASM_MONT5)\n if (window == 5 && top > 1) {\n void bn_mul_mont_gather5(BN_ULONG *rp, const BN_ULONG *ap,\n const void *table, const BN_ULONG *np,\n const BN_ULONG *n0, int num, int power);\n void bn_scatter5(const BN_ULONG *inp, size_t num,\n void *table, size_t power);\n void bn_gather5(BN_ULONG *out, size_t num, void *table, size_t power);\n void bn_power5(BN_ULONG *rp, const BN_ULONG *ap,\n const void *table, const BN_ULONG *np,\n const BN_ULONG *n0, int num, int power);\n int bn_get_bits5(const BN_ULONG *ap, int off);\n int bn_from_montgomery(BN_ULONG *rp, const BN_ULONG *ap,\n const BN_ULONG *not_used, const BN_ULONG *np,\n const BN_ULONG *n0, int num);\n BN_ULONG *n0 = mont->n0, *np;\n for (i = am.top; i < top; i++)\n am.d[i] = 0;\n for (i = tmp.top; i < top; i++)\n tmp.d[i] = 0;\n for (np = am.d + top, i = 0; i < top; i++)\n np[i] = mont->N.d[i];\n bn_scatter5(tmp.d, top, powerbuf, 0);\n bn_scatter5(am.d, am.top, powerbuf, 1);\n bn_mul_mont(tmp.d, am.d, am.d, np, n0, top);\n bn_scatter5(tmp.d, top, powerbuf, 2);\n# if 0\n for (i = 3; i < 32; i++) {\n bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1);\n bn_scatter5(tmp.d, top, powerbuf, i);\n }\n# else\n for (i = 4; i < 32; i *= 2) {\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_scatter5(tmp.d, top, powerbuf, i);\n }\n for (i = 3; i < 8; i += 2) {\n int j;\n bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1);\n bn_scatter5(tmp.d, top, powerbuf, i);\n for (j = 2 * i; j < 32; j *= 2) {\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_scatter5(tmp.d, top, powerbuf, j);\n }\n }\n for (; i < 16; i += 2) {\n bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1);\n bn_scatter5(tmp.d, top, powerbuf, i);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_scatter5(tmp.d, top, powerbuf, 2 * i);\n }\n for (; i < 32; i += 2) {\n bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1);\n bn_scatter5(tmp.d, top, powerbuf, i);\n }\n# endif\n bits--;\n for (wvalue = 0, i = bits % 5; i >= 0; i--, bits--)\n wvalue = (wvalue << 1) + BN_is_bit_set(p, bits);\n bn_gather5(tmp.d, top, powerbuf, wvalue);\n if (top & 7)\n while (bits >= 0) {\n for (wvalue = 0, i = 0; i < 5; i++, bits--)\n wvalue = (wvalue << 1) + BN_is_bit_set(p, bits);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_gather5(tmp.d, tmp.d, powerbuf, np, n0, top,\n wvalue);\n } else {\n while (bits >= 0) {\n wvalue = bn_get_bits5(p->d, bits - 4);\n bits -= 5;\n bn_power5(tmp.d, tmp.d, powerbuf, np, n0, top, wvalue);\n }\n }\n ret = bn_from_montgomery(tmp.d, tmp.d, NULL, np, n0, top);\n tmp.top = top;\n bn_correct_top(&tmp);\n if (ret) {\n if (!BN_copy(rr, &tmp))\n ret = 0;\n goto err;\n }\n } else\n#endif\n {\n if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&tmp, top, powerbuf, 0, window))\n goto err;\n if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&am, top, powerbuf, 1, window))\n goto err;\n if (window > 1) {\n if (!BN_mod_mul_montgomery(&tmp, &am, &am, mont, ctx))\n goto err;\n if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&tmp, top, powerbuf, 2,\n window))\n goto err;\n for (i = 3; i < numPowers; i++) {\n if (!BN_mod_mul_montgomery(&tmp, &am, &tmp, mont, ctx))\n goto err;\n if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&tmp, top, powerbuf, i,\n window))\n goto err;\n }\n }\n bits--;\n for (wvalue = 0, i = bits % window; i >= 0; i--, bits--)\n wvalue = (wvalue << 1) + BN_is_bit_set(p, bits);\n if (!MOD_EXP_CTIME_COPY_FROM_PREBUF(&tmp, top, powerbuf, wvalue,\n window))\n goto err;\n while (bits >= 0) {\n wvalue = 0;\n for (i = 0; i < window; i++, bits--) {\n if (!BN_mod_mul_montgomery(&tmp, &tmp, &tmp, mont, ctx))\n goto err;\n wvalue = (wvalue << 1) + BN_is_bit_set(p, bits);\n }\n if (!MOD_EXP_CTIME_COPY_FROM_PREBUF(&am, top, powerbuf, wvalue,\n window))\n goto err;\n if (!BN_mod_mul_montgomery(&tmp, &tmp, &am, mont, ctx))\n goto err;\n }\n }\n#if defined(SPARC_T4_MONT)\n if (OPENSSL_sparcv9cap_P[0] & (SPARCV9_VIS3 | SPARCV9_PREFER_FPU)) {\n am.d[0] = 1;\n for (i = 1; i < top; i++)\n am.d[i] = 0;\n if (!BN_mod_mul_montgomery(rr, &tmp, &am, mont, ctx))\n goto err;\n } else\n#endif\n if (!BN_from_montgomery(rr, &tmp, mont, ctx))\n goto err;\n ret = 1;\n err:\n if (in_mont == NULL)\n BN_MONT_CTX_free(mont);\n if (powerbuf != NULL) {\n OPENSSL_cleanse(powerbuf, powerbufLen);\n OPENSSL_free(powerbufFree);\n }\n BN_CTX_end(ctx);\n return (ret);\n}', '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_to_montgomery(BIGNUM *r, const BIGNUM *a, BN_MONT_CTX *mont,\n BN_CTX *ctx)\n{\n return BN_mod_mul_montgomery(r, a, &(mont->RR), mont, ctx);\n}', 'int BN_mod_mul_montgomery(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,\n BN_MONT_CTX *mont, BN_CTX *ctx)\n{\n BIGNUM *tmp;\n int ret = 0;\n#if defined(OPENSSL_BN_ASM_MONT) && defined(MONT_WORD)\n int num = mont->N.top;\n if (num > 1 && a->top == num && b->top == num) {\n if (bn_wexpand(r, num) == NULL)\n return (0);\n if (bn_mul_mont(r->d, a->d, b->d, mont->N.d, mont->n0, num)) {\n r->neg = a->neg ^ b->neg;\n r->top = num;\n bn_correct_top(r);\n return (1);\n }\n }\n#endif\n BN_CTX_start(ctx);\n tmp = BN_CTX_get(ctx);\n if (tmp == NULL)\n goto err;\n bn_check_top(tmp);\n if (a == b) {\n if (!BN_sqr(tmp, a, ctx))\n goto err;\n } else {\n if (!BN_mul(tmp, a, b, ctx))\n goto err;\n }\n#ifdef MONT_WORD\n if (!BN_from_montgomery_word(r, tmp, mont))\n goto err;\n#else\n if (!BN_from_montgomery(r, tmp, mont, ctx))\n goto err;\n#endif\n bn_check_top(r);\n ret = 1;\n err:\n BN_CTX_end(ctx);\n return (ret);\n}', 'int BN_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)\n{\n int ret = 0;\n int top, al, bl;\n BIGNUM *rr;\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n int i;\n#endif\n#ifdef BN_RECURSION\n BIGNUM *t = NULL;\n int j = 0, k;\n#endif\n bn_check_top(a);\n bn_check_top(b);\n bn_check_top(r);\n al = a->top;\n bl = b->top;\n if ((al == 0) || (bl == 0)) {\n BN_zero(r);\n return (1);\n }\n top = al + bl;\n BN_CTX_start(ctx);\n if ((r == a) || (r == b)) {\n if ((rr = BN_CTX_get(ctx)) == NULL)\n goto err;\n } else\n rr = r;\n rr->neg = a->neg ^ b->neg;\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n i = al - bl;\n#endif\n#ifdef BN_MUL_COMBA\n if (i == 0) {\n# if 0\n if (al == 4) {\n if (bn_wexpand(rr, 8) == NULL)\n goto err;\n rr->top = 8;\n bn_mul_comba4(rr->d, a->d, b->d);\n goto end;\n }\n# endif\n if (al == 8) {\n if (bn_wexpand(rr, 16) == NULL)\n goto err;\n rr->top = 16;\n bn_mul_comba8(rr->d, a->d, b->d);\n goto end;\n }\n }\n#endif\n#ifdef BN_RECURSION\n if ((al >= BN_MULL_SIZE_NORMAL) && (bl >= BN_MULL_SIZE_NORMAL)) {\n if (i >= -1 && i <= 1) {\n if (i >= 0) {\n j = BN_num_bits_word((BN_ULONG)al);\n }\n if (i == -1) {\n j = BN_num_bits_word((BN_ULONG)bl);\n }\n j = 1 << (j - 1);\n assert(j <= al || j <= bl);\n k = j + j;\n t = BN_CTX_get(ctx);\n if (t == NULL)\n goto err;\n if (al > j || bl > j) {\n if (bn_wexpand(t, k * 4) == NULL)\n goto err;\n if (bn_wexpand(rr, k * 4) == NULL)\n goto err;\n bn_mul_part_recursive(rr->d, a->d, b->d,\n j, al - j, bl - j, t->d);\n } else {\n if (bn_wexpand(t, k * 2) == NULL)\n goto err;\n if (bn_wexpand(rr, k * 2) == NULL)\n goto err;\n bn_mul_recursive(rr->d, a->d, b->d, j, al - j, bl - j, t->d);\n }\n rr->top = top;\n goto end;\n }\n# if 0\n if (i == 1 && !BN_get_flags(b, BN_FLG_STATIC_DATA)) {\n BIGNUM *tmp_bn = (BIGNUM *)b;\n if (bn_wexpand(tmp_bn, al) == NULL)\n goto err;\n tmp_bn->d[bl] = 0;\n bl++;\n i--;\n } else if (i == -1 && !BN_get_flags(a, BN_FLG_STATIC_DATA)) {\n BIGNUM *tmp_bn = (BIGNUM *)a;\n if (bn_wexpand(tmp_bn, bl) == NULL)\n goto err;\n tmp_bn->d[al] = 0;\n al++;\n i++;\n }\n if (i == 0) {\n j = BN_num_bits_word((BN_ULONG)al);\n j = 1 << (j - 1);\n k = j + j;\n t = BN_CTX_get(ctx);\n if (al == j) {\n if (bn_wexpand(t, k * 2) == NULL)\n goto err;\n if (bn_wexpand(rr, k * 2) == NULL)\n goto err;\n bn_mul_recursive(rr->d, a->d, b->d, al, t->d);\n } else {\n if (bn_wexpand(t, k * 4) == NULL)\n goto err;\n if (bn_wexpand(rr, k * 4) == NULL)\n goto err;\n bn_mul_part_recursive(rr->d, a->d, b->d, al - j, j, t->d);\n }\n rr->top = top;\n goto end;\n }\n# endif\n }\n#endif\n if (bn_wexpand(rr, top) == NULL)\n goto err;\n rr->top = top;\n bn_mul_normal(rr->d, a->d, al, b->d, bl);\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n end:\n#endif\n bn_correct_top(rr);\n if (r != rr)\n BN_copy(r, rr);\n ret = 1;\n err:\n bn_check_top(r);\n BN_CTX_end(ctx);\n return (ret);\n}', 'void BN_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}'] |
35,144 | 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 svq3_decode_slice_header(AVCodecContext *avctx)\n{\n SVQ3Context *s = avctx->priv_data;\n const int mb_xy = s->mb_xy;\n int i, header;\n unsigned slice_id;\n header = bitstream_read(&s->bc, 8);\n if (((header & 0x9F) != 1 && (header & 0x9F) != 2) || (header & 0x60) == 0) {\n av_log(avctx, AV_LOG_ERROR, "unsupported slice header (%02X)\\n", header);\n return -1;\n } else {\n int slice_bits, slice_bytes, slice_length;\n int length = header >> 5 & 3;\n slice_length = bitstream_peek(&s->bc, 8 * length);\n slice_bits = slice_length * 8;\n slice_bytes = slice_length + length - 1;\n if (slice_bytes > bitstream_bits_left(&s->bc)) {\n av_log(avctx, AV_LOG_ERROR, "slice after bitstream end\\n");\n return -1;\n }\n bitstream_skip(&s->bc, 8);\n av_fast_malloc(&s->slice_buf, &s->slice_size, slice_bytes + AV_INPUT_BUFFER_PADDING_SIZE);\n if (!s->slice_buf)\n return AVERROR(ENOMEM);\n memcpy(s->slice_buf, s->bc.buffer + bitstream_tell(&s->bc) / 8, slice_bytes);\n if (s->watermark_key) {\n uint32_t header = AV_RL32(&s->bc_slice.buffer[1]);\n AV_WL32(&s->bc_slice.buffer[1], header ^ s->watermark_key);\n }\n if (length > 0) {\n memcpy(s->slice_buf, &s->slice_buf[slice_length], length - 1);\n }\n bitstream_skip(&s->bc, slice_bytes * 8);\n bitstream_init(&s->bc_slice, s->slice_buf, slice_bits);\n }\n if ((slice_id = get_interleaved_ue_golomb(&s->bc_slice)) >= 3) {\n av_log(s->avctx, AV_LOG_ERROR, "illegal slice type %u \\n", slice_id);\n return -1;\n }\n s->pict_type = ff_h264_golomb_to_pict_type[slice_id];\n if ((header & 0x9F) == 2) {\n i = (s->mb_num < 64) ? 6 : (1 + av_log2(s->mb_num - 1));\n bitstream_read(&s->bc_slice, i);\n } else {\n bitstream_skip(&s->bc_slice, 1);\n }\n s->slice_num = bitstream_read(&s->bc_slice, 8);\n s->qscale = bitstream_read(&s->bc_slice, 5);\n s->adaptive_quant = bitstream_read_bit(&s->bc_slice);\n bitstream_skip(&s->bc_slice, 1);\n if (s->unknown_flag)\n bitstream_skip(&s->bc_slice, 1);\n bitstream_skip(&s->bc_slice, 1);\n bitstream_skip(&s->bc_slice, 2);\n while (bitstream_read_bit(&s->bc_slice))\n bitstream_skip(&s->bc_slice, 8);\n if (s->mb_x > 0) {\n memset(s->intra4x4_pred_mode + s->mb2br_xy[mb_xy - 1] + 3,\n -1, 4 * sizeof(int8_t));\n memset(s->intra4x4_pred_mode + s->mb2br_xy[mb_xy - s->mb_x],\n -1, 8 * sizeof(int8_t) * s->mb_x);\n }\n if (s->mb_y > 0) {\n memset(s->intra4x4_pred_mode + s->mb2br_xy[mb_xy - s->mb_stride],\n -1, 8 * sizeof(int8_t) * (s->mb_width - s->mb_x));\n if (s->mb_x > 0)\n s->intra4x4_pred_mode[s->mb2br_xy[mb_xy - s->mb_stride - 1] + 3] = -1;\n }\n return 0;\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}'] |
35,145 | 0 | https://github.com/openssl/openssl/blob/9b02dc97e4963969da69675a871dbe80e6d31cda/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);
} | ['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}', '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_sqr(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx)\n{\n int max, al;\n int ret = 0;\n BIGNUM *tmp, *rr;\n bn_check_top(a);\n al = a->top;\n if (al <= 0) {\n r->top = 0;\n r->neg = 0;\n return 1;\n }\n BN_CTX_start(ctx);\n rr = (a != r) ? r : BN_CTX_get(ctx);\n tmp = BN_CTX_get(ctx);\n if (rr == NULL || tmp == NULL)\n goto err;\n max = 2 * al;\n if (bn_wexpand(rr, max) == NULL)\n goto err;\n if (al == 4) {\n#ifndef BN_SQR_COMBA\n BN_ULONG t[8];\n bn_sqr_normal(rr->d, a->d, 4, t);\n#else\n bn_sqr_comba4(rr->d, a->d);\n#endif\n } else if (al == 8) {\n#ifndef BN_SQR_COMBA\n BN_ULONG t[16];\n bn_sqr_normal(rr->d, a->d, 8, t);\n#else\n bn_sqr_comba8(rr->d, a->d);\n#endif\n } else {\n#if defined(BN_RECURSION)\n if (al < BN_SQR_RECURSIVE_SIZE_NORMAL) {\n BN_ULONG t[BN_SQR_RECURSIVE_SIZE_NORMAL * 2];\n bn_sqr_normal(rr->d, a->d, al, t);\n } else {\n int j, k;\n j = BN_num_bits_word((BN_ULONG)al);\n j = 1 << (j - 1);\n k = j + j;\n if (al == j) {\n if (bn_wexpand(tmp, k * 2) == NULL)\n goto err;\n bn_sqr_recursive(rr->d, a->d, al, tmp->d);\n } else {\n if (bn_wexpand(tmp, max) == NULL)\n goto err;\n bn_sqr_normal(rr->d, a->d, al, tmp->d);\n }\n }\n#else\n if (bn_wexpand(tmp, max) == NULL)\n goto err;\n bn_sqr_normal(rr->d, a->d, al, tmp->d);\n#endif\n }\n rr->neg = 0;\n if (a->d[al - 1] == (a->d[al - 1] & BN_MASK2l))\n rr->top = max - 1;\n else\n rr->top = max;\n if (r != rr && BN_copy(r, rr) == NULL)\n goto err;\n ret = 1;\n err:\n bn_check_top(rr);\n bn_check_top(tmp);\n BN_CTX_end(ctx);\n return ret;\n}', 'void bn_sqr_normal(BN_ULONG *r, const BN_ULONG *a, int n, BN_ULONG *tmp)\n{\n int i, j, max;\n const BN_ULONG *ap;\n BN_ULONG *rp;\n max = n * 2;\n ap = a;\n rp = r;\n rp[0] = rp[max - 1] = 0;\n rp++;\n j = n;\n if (--j > 0) {\n ap++;\n rp[j] = bn_mul_words(rp, ap, j, ap[-1]);\n rp += 2;\n }\n for (i = n - 2; i > 0; i--) {\n j--;\n ap++;\n rp[j] = bn_mul_add_words(rp, ap, j, ap[-1]);\n rp += 2;\n }\n bn_add_words(r, r, r, max);\n bn_sqr_words(tmp, a, n);\n bn_add_words(r, r, tmp, max);\n}'] |
35,146 | 0 | https://github.com/nginx/nginx/blob/e4ecddfdb0d2ffc872658e36028971ad9a873726/src/core/ngx_string.c/#L244 | u_char *
ngx_vsnprintf(u_char *buf, size_t max, const char *fmt, va_list args)
{
u_char *p, zero, *last;
int d;
float f, scale;
size_t len, slen;
int64_t i64;
uint64_t ui64;
ngx_msec_t ms;
ngx_uint_t width, sign, hex, max_width, frac_width, i;
ngx_str_t *v;
ngx_variable_value_t *vv;
if (max == 0) {
return buf;
}
last = buf + max;
while (*fmt && buf < last) {
if (*fmt == '%') {
i64 = 0;
ui64 = 0;
zero = (u_char) ((*++fmt == '0') ? '0' : ' ');
width = 0;
sign = 1;
hex = 0;
max_width = 0;
frac_width = 0;
slen = (size_t) -1;
while (*fmt >= '0' && *fmt <= '9') {
width = width * 10 + *fmt++ - '0';
}
for ( ;; ) {
switch (*fmt) {
case 'u':
sign = 0;
fmt++;
continue;
case 'm':
max_width = 1;
fmt++;
continue;
case 'X':
hex = 2;
sign = 0;
fmt++;
continue;
case 'x':
hex = 1;
sign = 0;
fmt++;
continue;
case '.':
fmt++;
while (*fmt >= '0' && *fmt <= '9') {
frac_width = frac_width * 10 + *fmt++ - '0';
}
break;
case '*':
slen = va_arg(args, size_t);
fmt++;
continue;
default:
break;
}
break;
}
switch (*fmt) {
case 'V':
v = va_arg(args, ngx_str_t *);
len = v->len;
len = (buf + len < last) ? len : (size_t) (last - buf);
buf = ngx_cpymem(buf, v->data, len);
fmt++;
continue;
case 'v':
vv = va_arg(args, ngx_variable_value_t *);
len = vv->len;
len = (buf + len < last) ? len : (size_t) (last - buf);
buf = ngx_cpymem(buf, vv->data, len);
fmt++;
continue;
case 's':
p = va_arg(args, u_char *);
if (slen == (size_t) -1) {
while (*p && buf < last) {
*buf++ = *p++;
}
} else {
len = (buf + slen < last) ? slen : (size_t) (last - buf);
buf = ngx_cpymem(buf, p, len);
}
fmt++;
continue;
case 'O':
i64 = (int64_t) va_arg(args, off_t);
sign = 1;
break;
case 'P':
i64 = (int64_t) va_arg(args, ngx_pid_t);
sign = 1;
break;
case 'T':
i64 = (int64_t) va_arg(args, time_t);
sign = 1;
break;
case 'M':
ms = (ngx_msec_t) va_arg(args, ngx_msec_t);
if ((ngx_msec_int_t) ms == -1) {
sign = 1;
i64 = -1;
} else {
sign = 0;
ui64 = (uint64_t) ms;
}
break;
case 'z':
if (sign) {
i64 = (int64_t) va_arg(args, ssize_t);
} else {
ui64 = (uint64_t) va_arg(args, size_t);
}
break;
case 'i':
if (sign) {
i64 = (int64_t) va_arg(args, ngx_int_t);
} else {
ui64 = (uint64_t) va_arg(args, ngx_uint_t);
}
if (max_width) {
width = NGX_INT_T_LEN;
}
break;
case 'd':
if (sign) {
i64 = (int64_t) va_arg(args, int);
} else {
ui64 = (uint64_t) va_arg(args, u_int);
}
break;
case 'l':
if (sign) {
i64 = (int64_t) va_arg(args, long);
} else {
ui64 = (uint64_t) va_arg(args, u_long);
}
break;
case 'D':
if (sign) {
i64 = (int64_t) va_arg(args, int32_t);
} else {
ui64 = (uint64_t) va_arg(args, uint32_t);
}
break;
case 'L':
if (sign) {
i64 = va_arg(args, int64_t);
} else {
ui64 = va_arg(args, uint64_t);
}
break;
case 'A':
if (sign) {
i64 = (int64_t) va_arg(args, ngx_atomic_int_t);
} else {
ui64 = (uint64_t) va_arg(args, ngx_atomic_uint_t);
}
if (max_width) {
width = NGX_ATOMIC_T_LEN;
}
break;
case 'f':
f = (float) va_arg(args, double);
if (f < 0) {
*buf++ = '-';
f = -f;
}
ui64 = (int64_t) f;
buf = ngx_sprintf_num(buf, last, ui64, zero, 0, width);
if (frac_width) {
if (buf < last) {
*buf++ = '.';
}
scale = 1.0;
for (i = 0; i < frac_width; i++) {
scale *= 10.0;
}
ui64 = (uint64_t) ((f - (int64_t) ui64) * scale);
buf = ngx_sprintf_num(buf, last, ui64, '0', 0, frac_width);
}
fmt++;
continue;
#if !(NGX_WIN32)
case 'r':
i64 = (int64_t) va_arg(args, rlim_t);
sign = 1;
break;
#endif
case 'p':
ui64 = (uintptr_t) va_arg(args, void *);
hex = 2;
sign = 0;
zero = '0';
width = NGX_PTR_SIZE * 2;
break;
case 'c':
d = va_arg(args, int);
*buf++ = (u_char) (d & 0xff);
fmt++;
continue;
case 'Z':
*buf++ = '\0';
fmt++;
continue;
case 'N':
#if (NGX_WIN32)
*buf++ = CR;
#endif
*buf++ = LF;
fmt++;
continue;
case '%':
*buf++ = '%';
fmt++;
continue;
default:
*buf++ = *fmt++;
continue;
}
if (sign) {
if (i64 < 0) {
*buf++ = '-';
ui64 = (uint64_t) -i64;
} else {
ui64 = (uint64_t) i64;
}
}
buf = ngx_sprintf_num(buf, last, ui64, zero, hex, width);
fmt++;
} else {
*buf++ = *fmt++;
}
}
return buf;
} | ['static ngx_int_t\nngx_getopt(ngx_cycle_t *cycle, int argc, char *const *argv)\n{\n ngx_int_t i;\n for (i = 1; i < argc; i++) {\n if (argv[i][0] != \'-\') {\n ngx_log_error(NGX_LOG_EMERG, cycle->log, 0,\n "invalid option: \\"%s\\"", argv[i]);\n return NGX_ERROR;\n }\n switch (argv[i][1]) {\n case \'v\':\n ngx_show_version = 1;\n break;\n case \'V\':\n ngx_show_version = 1;\n ngx_show_configure = 1;\n break;\n case \'t\':\n ngx_test_config = 1;\n break;\n case \'c\':\n if (argv[i + 1] == NULL) {\n ngx_log_error(NGX_LOG_EMERG, cycle->log, 0,\n "the option \\"-c\\" requires file name");\n return NGX_ERROR;\n }\n cycle->conf_file.data = (u_char *) argv[++i];\n cycle->conf_file.len = ngx_strlen(cycle->conf_file.data);\n break;\n case \'g\':\n if (argv[i + 1] == NULL) {\n ngx_log_error(NGX_LOG_EMERG, cycle->log, 0,\n "the option \\"-g\\" requires parameter");\n return NGX_ERROR;\n }\n cycle->conf_param.data = (u_char *) argv[++i];\n cycle->conf_param.len = ngx_strlen(cycle->conf_param.data);\n break;\n default:\n ngx_log_error(NGX_LOG_EMERG, cycle->log, 0,\n "invalid option: \\"%s\\"", argv[i]);\n return NGX_ERROR;\n }\n }\n if (cycle->conf_file.data == NULL) {\n cycle->conf_file.len = sizeof(NGX_CONF_PATH) - 1;\n cycle->conf_file.data = (u_char *) NGX_CONF_PATH;\n }\n return NGX_OK;\n}', 'void\nngx_log_error_core(ngx_uint_t level, ngx_log_t *log, ngx_err_t err,\n const char *fmt, ...)\n#else\nvoid\nngx_log_error_core(ngx_uint_t level, ngx_log_t *log, ngx_err_t err,\n const char *fmt, va_list args)\n#endif\n{\n#if (NGX_HAVE_VARIADIC_MACROS)\n va_list args;\n#endif\n u_char errstr[NGX_MAX_ERROR_STR], *p, *last;\n if (log->file->fd == NGX_INVALID_FILE) {\n return;\n }\n last = errstr + NGX_MAX_ERROR_STR;\n ngx_memcpy(errstr, ngx_cached_err_log_time.data,\n ngx_cached_err_log_time.len);\n p = errstr + ngx_cached_err_log_time.len;\n p = ngx_snprintf(p, last - p, " [%s] ", err_levels[level]);\n p = ngx_snprintf(p, last - p, "%P#" NGX_TID_T_FMT ": ",\n ngx_log_pid, ngx_log_tid);\n if (log->connection) {\n p = ngx_snprintf(p, last - p, "*%uA ", log->connection);\n }\n#if (NGX_HAVE_VARIADIC_MACROS)\n va_start(args, fmt);\n p = ngx_vsnprintf(p, last - p, fmt, args);\n va_end(args);\n#else\n p = ngx_vsnprintf(p, last - p, fmt, args);\n#endif\n if (err) {\n if (p > last - 50) {\n p = last - 50;\n *p++ = \'.\';\n *p++ = \'.\';\n *p++ = \'.\';\n }\n#if (NGX_WIN32)\n p = ngx_snprintf(p, last - p, ((unsigned) err < 0x80000000)\n ? " (%d: " : " (%Xd: ", err);\n#else\n p = ngx_snprintf(p, last - p, " (%d: ", err);\n#endif\n p = ngx_strerror_r(err, p, last - p);\n if (p < last) {\n *p++ = \')\';\n }\n }\n if (level != NGX_LOG_DEBUG && log->handler) {\n p = log->handler(log, p, last - p);\n }\n if (p > last - NGX_LINEFEED_SIZE) {\n p = last - NGX_LINEFEED_SIZE;\n }\n ngx_linefeed(p);\n (void) ngx_write_fd(log->file->fd, errstr, p - errstr);\n}', 'u_char * ngx_cdecl\nngx_snprintf(u_char *buf, size_t max, const char *fmt, ...)\n{\n u_char *p;\n va_list args;\n va_start(args, fmt);\n p = ngx_vsnprintf(buf, max, fmt, args);\n va_end(args);\n return p;\n}', "u_char *\nngx_vsnprintf(u_char *buf, size_t max, const char *fmt, va_list args)\n{\n u_char *p, zero, *last;\n int d;\n float f, scale;\n size_t len, slen;\n int64_t i64;\n uint64_t ui64;\n ngx_msec_t ms;\n ngx_uint_t width, sign, hex, max_width, frac_width, i;\n ngx_str_t *v;\n ngx_variable_value_t *vv;\n if (max == 0) {\n return buf;\n }\n last = buf + max;\n while (*fmt && buf < last) {\n if (*fmt == '%') {\n i64 = 0;\n ui64 = 0;\n zero = (u_char) ((*++fmt == '0') ? '0' : ' ');\n width = 0;\n sign = 1;\n hex = 0;\n max_width = 0;\n frac_width = 0;\n slen = (size_t) -1;\n while (*fmt >= '0' && *fmt <= '9') {\n width = width * 10 + *fmt++ - '0';\n }\n for ( ;; ) {\n switch (*fmt) {\n case 'u':\n sign = 0;\n fmt++;\n continue;\n case 'm':\n max_width = 1;\n fmt++;\n continue;\n case 'X':\n hex = 2;\n sign = 0;\n fmt++;\n continue;\n case 'x':\n hex = 1;\n sign = 0;\n fmt++;\n continue;\n case '.':\n fmt++;\n while (*fmt >= '0' && *fmt <= '9') {\n frac_width = frac_width * 10 + *fmt++ - '0';\n }\n break;\n case '*':\n slen = va_arg(args, size_t);\n fmt++;\n continue;\n default:\n break;\n }\n break;\n }\n switch (*fmt) {\n case 'V':\n v = va_arg(args, ngx_str_t *);\n len = v->len;\n len = (buf + len < last) ? len : (size_t) (last - buf);\n buf = ngx_cpymem(buf, v->data, len);\n fmt++;\n continue;\n case 'v':\n vv = va_arg(args, ngx_variable_value_t *);\n len = vv->len;\n len = (buf + len < last) ? len : (size_t) (last - buf);\n buf = ngx_cpymem(buf, vv->data, len);\n fmt++;\n continue;\n case 's':\n p = va_arg(args, u_char *);\n if (slen == (size_t) -1) {\n while (*p && buf < last) {\n *buf++ = *p++;\n }\n } else {\n len = (buf + slen < last) ? slen : (size_t) (last - buf);\n buf = ngx_cpymem(buf, p, len);\n }\n fmt++;\n continue;\n case 'O':\n i64 = (int64_t) va_arg(args, off_t);\n sign = 1;\n break;\n case 'P':\n i64 = (int64_t) va_arg(args, ngx_pid_t);\n sign = 1;\n break;\n case 'T':\n i64 = (int64_t) va_arg(args, time_t);\n sign = 1;\n break;\n case 'M':\n ms = (ngx_msec_t) va_arg(args, ngx_msec_t);\n if ((ngx_msec_int_t) ms == -1) {\n sign = 1;\n i64 = -1;\n } else {\n sign = 0;\n ui64 = (uint64_t) ms;\n }\n break;\n case 'z':\n if (sign) {\n i64 = (int64_t) va_arg(args, ssize_t);\n } else {\n ui64 = (uint64_t) va_arg(args, size_t);\n }\n break;\n case 'i':\n if (sign) {\n i64 = (int64_t) va_arg(args, ngx_int_t);\n } else {\n ui64 = (uint64_t) va_arg(args, ngx_uint_t);\n }\n if (max_width) {\n width = NGX_INT_T_LEN;\n }\n break;\n case 'd':\n if (sign) {\n i64 = (int64_t) va_arg(args, int);\n } else {\n ui64 = (uint64_t) va_arg(args, u_int);\n }\n break;\n case 'l':\n if (sign) {\n i64 = (int64_t) va_arg(args, long);\n } else {\n ui64 = (uint64_t) va_arg(args, u_long);\n }\n break;\n case 'D':\n if (sign) {\n i64 = (int64_t) va_arg(args, int32_t);\n } else {\n ui64 = (uint64_t) va_arg(args, uint32_t);\n }\n break;\n case 'L':\n if (sign) {\n i64 = va_arg(args, int64_t);\n } else {\n ui64 = va_arg(args, uint64_t);\n }\n break;\n case 'A':\n if (sign) {\n i64 = (int64_t) va_arg(args, ngx_atomic_int_t);\n } else {\n ui64 = (uint64_t) va_arg(args, ngx_atomic_uint_t);\n }\n if (max_width) {\n width = NGX_ATOMIC_T_LEN;\n }\n break;\n case 'f':\n f = (float) va_arg(args, double);\n if (f < 0) {\n *buf++ = '-';\n f = -f;\n }\n ui64 = (int64_t) f;\n buf = ngx_sprintf_num(buf, last, ui64, zero, 0, width);\n if (frac_width) {\n if (buf < last) {\n *buf++ = '.';\n }\n scale = 1.0;\n for (i = 0; i < frac_width; i++) {\n scale *= 10.0;\n }\n ui64 = (uint64_t) ((f - (int64_t) ui64) * scale);\n buf = ngx_sprintf_num(buf, last, ui64, '0', 0, frac_width);\n }\n fmt++;\n continue;\n#if !(NGX_WIN32)\n case 'r':\n i64 = (int64_t) va_arg(args, rlim_t);\n sign = 1;\n break;\n#endif\n case 'p':\n ui64 = (uintptr_t) va_arg(args, void *);\n hex = 2;\n sign = 0;\n zero = '0';\n width = NGX_PTR_SIZE * 2;\n break;\n case 'c':\n d = va_arg(args, int);\n *buf++ = (u_char) (d & 0xff);\n fmt++;\n continue;\n case 'Z':\n *buf++ = '\\0';\n fmt++;\n continue;\n case 'N':\n#if (NGX_WIN32)\n *buf++ = CR;\n#endif\n *buf++ = LF;\n fmt++;\n continue;\n case '%':\n *buf++ = '%';\n fmt++;\n continue;\n default:\n *buf++ = *fmt++;\n continue;\n }\n if (sign) {\n if (i64 < 0) {\n *buf++ = '-';\n ui64 = (uint64_t) -i64;\n } else {\n ui64 = (uint64_t) i64;\n }\n }\n buf = ngx_sprintf_num(buf, last, ui64, zero, hex, width);\n fmt++;\n } else {\n *buf++ = *fmt++;\n }\n }\n return buf;\n}"] |
35,147 | 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)];
} | ['int ec_wNAF_precompute_mult(EC_GROUP *group, BN_CTX *ctx)\n{\n const EC_POINT *generator;\n EC_POINT *tmp_point = NULL, *base = NULL, **var;\n BN_CTX *new_ctx = NULL;\n const BIGNUM *order;\n size_t i, bits, w, pre_points_per_block, blocksize, numblocks, num;\n EC_POINT **points = NULL;\n EC_PRE_COMP *pre_comp;\n int ret = 0;\n EC_pre_comp_free(group);\n if ((pre_comp = ec_pre_comp_new(group)) == NULL)\n return 0;\n generator = EC_GROUP_get0_generator(group);\n if (generator == NULL) {\n ECerr(EC_F_EC_WNAF_PRECOMPUTE_MULT, EC_R_UNDEFINED_GENERATOR);\n goto err;\n }\n if (ctx == NULL) {\n ctx = new_ctx = BN_CTX_new();\n if (ctx == NULL)\n goto err;\n }\n BN_CTX_start(ctx);\n order = EC_GROUP_get0_order(group);\n if (order == NULL)\n goto err;\n if (BN_is_zero(order)) {\n ECerr(EC_F_EC_WNAF_PRECOMPUTE_MULT, EC_R_UNKNOWN_ORDER);\n goto err;\n }\n bits = BN_num_bits(order);\n blocksize = 8;\n w = 4;\n if (EC_window_bits_for_scalar_size(bits) > w) {\n w = EC_window_bits_for_scalar_size(bits);\n }\n numblocks = (bits + blocksize - 1) / blocksize;\n pre_points_per_block = (size_t)1 << (w - 1);\n num = pre_points_per_block * numblocks;\n points = OPENSSL_malloc(sizeof(*points) * (num + 1));\n if (points == NULL) {\n ECerr(EC_F_EC_WNAF_PRECOMPUTE_MULT, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n var = points;\n var[num] = NULL;\n for (i = 0; i < num; i++) {\n if ((var[i] = EC_POINT_new(group)) == NULL) {\n ECerr(EC_F_EC_WNAF_PRECOMPUTE_MULT, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n }\n if ((tmp_point = EC_POINT_new(group)) == NULL\n || (base = EC_POINT_new(group)) == NULL) {\n ECerr(EC_F_EC_WNAF_PRECOMPUTE_MULT, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n if (!EC_POINT_copy(base, generator))\n goto err;\n for (i = 0; i < numblocks; i++) {\n size_t j;\n if (!EC_POINT_dbl(group, tmp_point, base, ctx))\n goto err;\n if (!EC_POINT_copy(*var++, base))\n goto err;\n for (j = 1; j < pre_points_per_block; j++, var++) {\n if (!EC_POINT_add(group, *var, tmp_point, *(var - 1), ctx))\n goto err;\n }\n if (i < numblocks - 1) {\n size_t k;\n if (blocksize <= 2) {\n ECerr(EC_F_EC_WNAF_PRECOMPUTE_MULT, ERR_R_INTERNAL_ERROR);\n goto err;\n }\n if (!EC_POINT_dbl(group, base, tmp_point, ctx))\n goto err;\n for (k = 2; k < blocksize; k++) {\n if (!EC_POINT_dbl(group, base, base, ctx))\n goto err;\n }\n }\n }\n if (!EC_POINTs_make_affine(group, num, points, ctx))\n goto err;\n pre_comp->group = group;\n pre_comp->blocksize = blocksize;\n pre_comp->numblocks = numblocks;\n pre_comp->w = w;\n pre_comp->points = points;\n points = NULL;\n pre_comp->num = num;\n SETPRECOMP(group, ec, pre_comp);\n pre_comp = NULL;\n ret = 1;\n err:\n BN_CTX_end(ctx);\n BN_CTX_free(new_ctx);\n EC_ec_pre_comp_free(pre_comp);\n if (points) {\n EC_POINT **p;\n for (p = points; *p != NULL; p++)\n EC_POINT_free(*p);\n OPENSSL_free(points);\n }\n EC_POINT_free(tmp_point);\n EC_POINT_free(base);\n return ret;\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}'] |
35,148 | 0 | https://github.com/openssl/openssl/blob/c1e744b9125a883450c2239ec55ea606c618a5c0/crypto/bn/bn_mul.c/#L648 | int BN_mul(BIGNUM *r, BIGNUM *a, BIGNUM *b, BN_CTX *ctx)
{
int top,al,bl;
BIGNUM *rr;
#ifdef BN_RECURSION
BIGNUM *t;
int i,j,k;
#endif
#ifdef BN_COUNT
printf("BN_mul %d * %d\n",a->top,b->top);
#endif
bn_check_top(a);
bn_check_top(b);
bn_check_top(r);
al=a->top;
bl=b->top;
r->neg=a->neg^b->neg;
if ((al == 0) || (bl == 0))
{
BN_zero(r);
return(1);
}
top=al+bl;
if ((r == a) || (r == b))
rr= &(ctx->bn[ctx->tos+1]);
else
rr=r;
#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)
if (al == bl)
{
# ifdef BN_MUL_COMBA
if (al == 8)
{
if (bn_wexpand(rr,16) == NULL) return(0);
rr->top=16;
bn_mul_comba8(rr->d,a->d,b->d);
goto end;
}
else
# endif
#ifdef BN_RECURSION
if (al < BN_MULL_SIZE_NORMAL)
#endif
{
if (bn_wexpand(rr,top) == NULL) return(0);
rr->top=top;
bn_mul_normal(rr->d,a->d,al,b->d,bl);
goto end;
}
# ifdef BN_RECURSION
goto symetric;
# endif
}
#endif
#ifdef BN_RECURSION
else if ((al < BN_MULL_SIZE_NORMAL) || (bl < BN_MULL_SIZE_NORMAL))
{
if (bn_wexpand(rr,top) == NULL) return(0);
rr->top=top;
bn_mul_normal(rr->d,a->d,al,b->d,bl);
goto end;
}
else
{
i=(al-bl);
if ((i == 1) && !BN_get_flags(b,BN_FLG_STATIC_DATA))
{
bn_wexpand(b,al);
b->d[bl]=0;
bl++;
goto symetric;
}
else if ((i == -1) && !BN_get_flags(a,BN_FLG_STATIC_DATA))
{
bn_wexpand(a,bl);
a->d[al]=0;
al++;
goto symetric;
}
}
#endif
if (bn_wexpand(rr,top) == NULL) return(0);
rr->top=top;
bn_mul_normal(rr->d,a->d,al,b->d,bl);
#ifdef BN_RECURSION
if (0)
{
symetric:
j=BN_num_bits_word((BN_ULONG)al);
j=1<<(j-1);
k=j+j;
t= &(ctx->bn[ctx->tos]);
if (al == j)
{
bn_wexpand(t,k*2);
bn_wexpand(rr,k*2);
bn_mul_recursive(rr->d,a->d,b->d,al,t->d);
}
else
{
bn_wexpand(a,k);
bn_wexpand(b,k);
bn_wexpand(t,k*4);
bn_wexpand(rr,k*4);
for (i=a->top; i<k; i++)
a->d[i]=0;
for (i=b->top; i<k; i++)
b->d[i]=0;
bn_mul_part_recursive(rr->d,a->d,b->d,al-j,j,t->d);
}
rr->top=top;
}
#endif
#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)
end:
#endif
bn_fix_top(rr);
if (r != rr) BN_copy(r,rr);
return(1);
} | ['int MAIN(int argc, char **argv)\n\t{\n\tDSA *dsa=NULL;\n\tint i,badops=0,text=0;\n\tBIO *in=NULL,*out=NULL;\n\tint informat,outformat,noout=0,C=0,ret=1;\n\tchar *infile,*outfile,*prog,*inrand=NULL;\n\tint numbits= -1,num,genkey=0;\n\tint need_rand=0;\n\tapps_startup();\n\tif (bio_err == NULL)\n\t\tif ((bio_err=BIO_new(BIO_s_file())) != NULL)\n\t\t\tBIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT);\n\tinfile=NULL;\n\toutfile=NULL;\n\tinformat=FORMAT_PEM;\n\toutformat=FORMAT_PEM;\n\tprog=argv[0];\n\targc--;\n\targv++;\n\twhile (argc >= 1)\n\t\t{\n\t\tif \t(strcmp(*argv,"-inform") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\tinformat=str2fmt(*(++argv));\n\t\t\t}\n\t\telse if (strcmp(*argv,"-outform") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\toutformat=str2fmt(*(++argv));\n\t\t\t}\n\t\telse if (strcmp(*argv,"-in") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\tinfile= *(++argv);\n\t\t\t}\n\t\telse if (strcmp(*argv,"-out") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\toutfile= *(++argv);\n\t\t\t}\n\t\telse if (strcmp(*argv,"-text") == 0)\n\t\t\ttext=1;\n\t\telse if (strcmp(*argv,"-C") == 0)\n\t\t\tC=1;\n\t\telse if (strcmp(*argv,"-genkey") == 0)\n\t\t\t{\n\t\t\tgenkey=1;\n\t\t\tneed_rand=1;\n\t\t\t}\n\t\telse if (strcmp(*argv,"-rand") == 0)\n\t\t\t{\n\t\t\tif (--argc < 1) goto bad;\n\t\t\tinrand= *(++argv);\n\t\t\tneed_rand=1;\n\t\t\t}\n\t\telse if (strcmp(*argv,"-noout") == 0)\n\t\t\tnoout=1;\n\t\telse if (sscanf(*argv,"%d",&num) == 1)\n\t\t\t{\n\t\t\tnumbits=num;\n\t\t\tneed_rand=1;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tBIO_printf(bio_err,"unknown option %s\\n",*argv);\n\t\t\tbadops=1;\n\t\t\tbreak;\n\t\t\t}\n\t\targc--;\n\t\targv++;\n\t\t}\n\tif (badops)\n\t\t{\nbad:\n\t\tBIO_printf(bio_err,"%s [options] [bits] <infile >outfile\\n",prog);\n\t\tBIO_printf(bio_err,"where options are\\n");\n\t\tBIO_printf(bio_err," -inform arg input format - DER or PEM\\n");\n\t\tBIO_printf(bio_err," -outform arg output format - DER or PEM\\n");\n\t\tBIO_printf(bio_err," -in arg input file\\n");\n\t\tBIO_printf(bio_err," -out arg output file\\n");\n\t\tBIO_printf(bio_err," -text print the key in text\\n");\n\t\tBIO_printf(bio_err," -C Output C code\\n");\n\t\tBIO_printf(bio_err," -noout no output\\n");\n\t\tBIO_printf(bio_err," -rand files to use for random number input\\n");\n\t\tBIO_printf(bio_err," number number of bits to use for generating private key\\n");\n\t\tgoto end;\n\t\t}\n\tERR_load_crypto_strings();\n\tin=BIO_new(BIO_s_file());\n\tout=BIO_new(BIO_s_file());\n\tif ((in == NULL) || (out == NULL))\n\t\t{\n\t\tERR_print_errors(bio_err);\n\t\tgoto end;\n\t\t}\n\tif (infile == NULL)\n\t\tBIO_set_fp(in,stdin,BIO_NOCLOSE);\n\telse\n\t\t{\n\t\tif (BIO_read_filename(in,infile) <= 0)\n\t\t\t{\n\t\t\tperror(infile);\n\t\t\tgoto end;\n\t\t\t}\n\t\t}\n\tif (outfile == NULL)\n\t\tBIO_set_fp(out,stdout,BIO_NOCLOSE);\n\telse\n\t\t{\n\t\tif (BIO_write_filename(out,outfile) <= 0)\n\t\t\t{\n\t\t\tperror(outfile);\n\t\t\tgoto end;\n\t\t\t}\n\t\t}\n\tif (need_rand)\n\t\t{\n\t\tapp_RAND_load_file(NULL, bio_err, (inrand != NULL));\n\t\tif (inrand != NULL)\n\t\t\tBIO_printf(bio_err,"%ld semi-random bytes loaded\\n",\n\t\t\t\tapp_RAND_load_files(inrand));\n\t\t}\n\tif (numbits > 0)\n\t\t{\n\t\tassert(need_rand);\n\t\tBIO_printf(bio_err,"Generating DSA parameters, %d bit long prime\\n",num);\n\t BIO_printf(bio_err,"This could take some time\\n");\n\t dsa=DSA_generate_parameters(num,NULL,0,NULL,NULL,\n\t\t\tdsa_cb,(char *)bio_err);\n\t\t}\n\telse if\t(informat == FORMAT_ASN1)\n\t\tdsa=d2i_DSAparams_bio(in,NULL);\n\telse if (informat == FORMAT_PEM)\n\t\tdsa=PEM_read_bio_DSAparams(in,NULL,NULL,NULL);\n\telse\n\t\t{\n\t\tBIO_printf(bio_err,"bad input format specified\\n");\n\t\tgoto end;\n\t\t}\n\tif (dsa == NULL)\n\t\t{\n\t\tBIO_printf(bio_err,"unable to load DSA parameters\\n");\n\t\tERR_print_errors(bio_err);\n\t\tgoto end;\n\t\t}\n\tif (text)\n\t\t{\n\t\tDSAparams_print(out,dsa);\n\t\t}\n\tif (C)\n\t\t{\n\t\tunsigned char *data;\n\t\tint l,len,bits_p,bits_q,bits_g;\n\t\tlen=BN_num_bytes(dsa->p);\n\t\tbits_p=BN_num_bits(dsa->p);\n\t\tbits_q=BN_num_bits(dsa->q);\n\t\tbits_g=BN_num_bits(dsa->g);\n\t\tdata=(unsigned char *)Malloc(len+20);\n\t\tif (data == NULL)\n\t\t\t{\n\t\t\tperror("Malloc");\n\t\t\tgoto end;\n\t\t\t}\n\t\tl=BN_bn2bin(dsa->p,data);\n\t\tprintf("static unsigned char dsa%d_p[]={",bits_p);\n\t\tfor (i=0; i<l; i++)\n\t\t\t{\n\t\t\tif ((i%12) == 0) printf("\\n\\t");\n\t\t\tprintf("0x%02X,",data[i]);\n\t\t\t}\n\t\tprintf("\\n\\t};\\n");\n\t\tl=BN_bn2bin(dsa->q,data);\n\t\tprintf("static unsigned char dsa%d_q[]={",bits_p);\n\t\tfor (i=0; i<l; i++)\n\t\t\t{\n\t\t\tif ((i%12) == 0) printf("\\n\\t");\n\t\t\tprintf("0x%02X,",data[i]);\n\t\t\t}\n\t\tprintf("\\n\\t};\\n");\n\t\tl=BN_bn2bin(dsa->g,data);\n\t\tprintf("static unsigned char dsa%d_g[]={",bits_p);\n\t\tfor (i=0; i<l; i++)\n\t\t\t{\n\t\t\tif ((i%12) == 0) printf("\\n\\t");\n\t\t\tprintf("0x%02X,",data[i]);\n\t\t\t}\n\t\tprintf("\\n\\t};\\n\\n");\n\t\tprintf("DSA *get_dsa%d()\\n\\t{\\n",bits_p);\n\t\tprintf("\\tDSA *dsa;\\n\\n");\n\t\tprintf("\\tif ((dsa=DSA_new()) == NULL) return(NULL);\\n");\n\t\tprintf("\\tdsa->p=BN_bin2bn(dsa%d_p,sizeof(dsa%d_p),NULL);\\n",\n\t\t\tbits_p,bits_p);\n\t\tprintf("\\tdsa->q=BN_bin2bn(dsa%d_q,sizeof(dsa%d_q),NULL);\\n",\n\t\t\tbits_p,bits_p);\n\t\tprintf("\\tdsa->g=BN_bin2bn(dsa%d_g,sizeof(dsa%d_g),NULL);\\n",\n\t\t\tbits_p,bits_p);\n\t\tprintf("\\tif ((dsa->p == NULL) || (dsa->q == NULL) || (dsa->g == NULL))\\n");\n\t\tprintf("\\t\\treturn(NULL);\\n");\n\t\tprintf("\\treturn(dsa);\\n\\t}\\n");\n\t\t}\n\tif (!noout)\n\t\t{\n\t\tif \t(outformat == FORMAT_ASN1)\n\t\t\ti=i2d_DSAparams_bio(out,dsa);\n\t\telse if (outformat == FORMAT_PEM)\n\t\t\ti=PEM_write_bio_DSAparams(out,dsa);\n\t\telse\t{\n\t\t\tBIO_printf(bio_err,"bad output format specified for outfile\\n");\n\t\t\tgoto end;\n\t\t\t}\n\t\tif (!i)\n\t\t\t{\n\t\t\tBIO_printf(bio_err,"unable to write DSA paramaters\\n");\n\t\t\tERR_print_errors(bio_err);\n\t\t\tgoto end;\n\t\t\t}\n\t\t}\n\tif (genkey)\n\t\t{\n\t\tDSA *dsakey;\n\t\tassert(need_rand);\n\t\tif ((dsakey=DSAparams_dup(dsa)) == NULL) goto end;\n\t\tif (!DSA_generate_key(dsakey)) goto end;\n\t\tif \t(outformat == FORMAT_ASN1)\n\t\t\ti=i2d_DSAPrivateKey_bio(out,dsakey);\n\t\telse if (outformat == FORMAT_PEM)\n\t\t\ti=PEM_write_bio_DSAPrivateKey(out,dsakey,NULL,NULL,0,NULL,NULL);\n\t\telse\t{\n\t\t\tBIO_printf(bio_err,"bad output format specified for outfile\\n");\n\t\t\tgoto end;\n\t\t\t}\n\t\tDSA_free(dsakey);\n\t\t}\n\tif (need_rand)\n\t\tapp_RAND_write_file(NULL, bio_err);\n\tret=0;\nend:\n\tif (in != NULL) BIO_free(in);\n\tif (out != NULL) BIO_free(out);\n\tif (dsa != NULL) DSA_free(dsa);\n\tEXIT(ret);\n\t}', 'DSA *DSA_generate_parameters(int bits, unsigned char *seed_in, int seed_len,\n\t int *counter_ret, unsigned long *h_ret, void (*callback)(),\n\t char *cb_arg)\n\t{\n\tint ok=0;\n\tunsigned char seed[SHA_DIGEST_LENGTH];\n\tunsigned char md[SHA_DIGEST_LENGTH];\n\tunsigned char buf[SHA_DIGEST_LENGTH],buf2[SHA_DIGEST_LENGTH];\n\tBIGNUM *r0,*W,*X,*c,*test;\n\tBIGNUM *g=NULL,*q=NULL,*p=NULL;\n\tBN_MONT_CTX *mont=NULL;\n\tint k,n=0,i,b,m=0;\n\tint counter=0;\n\tBN_CTX *ctx=NULL,*ctx2=NULL;\n\tunsigned int h=2;\n\tDSA *ret=NULL;\n\tif (bits < 512) bits=512;\n\tbits=(bits+63)/64*64;\n\tif ((seed_in != NULL) && (seed_len == 20))\n\t\tmemcpy(seed,seed_in,seed_len);\n\tif ((ctx=BN_CTX_new()) == NULL) goto err;\n\tif ((ctx2=BN_CTX_new()) == NULL) goto err;\n\tif ((ret=DSA_new()) == NULL) goto err;\n\tif ((mont=BN_MONT_CTX_new()) == NULL) goto err;\n\tr0= &(ctx2->bn[0]);\n\tg= &(ctx2->bn[1]);\n\tW= &(ctx2->bn[2]);\n\tq= &(ctx2->bn[3]);\n\tX= &(ctx2->bn[4]);\n\tc= &(ctx2->bn[5]);\n\tp= &(ctx2->bn[6]);\n\ttest= &(ctx2->bn[7]);\n\tBN_lshift(test,BN_value_one(),bits-1);\n\tfor (;;)\n\t\t{\n\t\tfor (;;)\n\t\t\t{\n\t\t\tif (callback != NULL) callback(0,m++,cb_arg);\n\t\t\tif (!seed_len)\n\t\t\t\tRAND_bytes(seed,SHA_DIGEST_LENGTH);\n\t\t\telse\n\t\t\t\tseed_len=0;\n\t\t\tmemcpy(buf,seed,SHA_DIGEST_LENGTH);\n\t\t\tmemcpy(buf2,seed,SHA_DIGEST_LENGTH);\n\t\t\tfor (i=SHA_DIGEST_LENGTH-1; i >= 0; i--)\n\t\t\t\t{\n\t\t\t\tbuf[i]++;\n\t\t\t\tif (buf[i] != 0) break;\n\t\t\t\t}\n\t\t\tHASH(seed,SHA_DIGEST_LENGTH,md);\n\t\t\tHASH(buf,SHA_DIGEST_LENGTH,buf2);\n\t\t\tfor (i=0; i<SHA_DIGEST_LENGTH; i++)\n\t\t\t\tmd[i]^=buf2[i];\n\t\t\tmd[0]|=0x80;\n\t\t\tmd[SHA_DIGEST_LENGTH-1]|=0x01;\n\t\t\tif (!BN_bin2bn(md,SHA_DIGEST_LENGTH,q)) abort();\n\t\t\tif (DSA_is_prime(q,callback,cb_arg) > 0) break;\n\t\t\t}\n\t\tif (callback != NULL) callback(2,0,cb_arg);\n\t\tif (callback != NULL) callback(3,0,cb_arg);\n\t\tcounter=0;\n\t\tn=(bits-1)/160;\n\t\tb=(bits-1)-n*160;\n\t\tfor (;;)\n\t\t\t{\n\t\t\tBN_zero(W);\n\t\t\tfor (k=0; k<=n; k++)\n\t\t\t\t{\n\t\t\t\tfor (i=SHA_DIGEST_LENGTH-1; i >= 0; i--)\n\t\t\t\t\t{\n\t\t\t\t\tbuf[i]++;\n\t\t\t\t\tif (buf[i] != 0) break;\n\t\t\t\t\t}\n\t\t\t\tHASH(buf,SHA_DIGEST_LENGTH,md);\n\t\t\t\tif (!BN_bin2bn(md,SHA_DIGEST_LENGTH,r0)) abort();\n\t\t\t\tBN_lshift(r0,r0,160*k);\n\t\t\t\tBN_add(W,W,r0);\n\t\t\t\t}\n\t\t\tBN_mask_bits(W,bits-1);\n\t\t\tBN_copy(X,W);\n\t\t\tBN_add(X,X,test);\n\t\t\tBN_lshift1(r0,q);\n\t\t\tBN_mod(c,X,r0,ctx);\n\t\t\tBN_sub(r0,c,BN_value_one());\n\t\t\tBN_sub(p,X,r0);\n\t\t\tif (BN_cmp(p,test) >= 0)\n\t\t\t\t{\n\t\t\t\tif (DSA_is_prime(p,callback,cb_arg) > 0)\n\t\t\t\t\tgoto end;\n\t\t\t\t}\n\t\t\tcounter++;\n\t\t\tif (counter >= 4096) break;\n\t\t\tif (callback != NULL) callback(0,counter,cb_arg);\n\t\t\t}\n\t\t}\nend:\n\tif (callback != NULL) callback(2,1,cb_arg);\n\tBN_sub(test,p,BN_value_one());\n\tBN_div(r0,NULL,test,q,ctx);\n\tBN_set_word(test,h);\n\tBN_MONT_CTX_set(mont,p,ctx);\n\tfor (;;)\n\t\t{\n\t\tBN_mod_exp_mont(g,test,r0,p,ctx,mont);\n\t\tif (!BN_is_one(g)) break;\n\t\tBN_add(test,test,BN_value_one());\n\t\th++;\n\t\t}\n\tif (callback != NULL) callback(3,1,cb_arg);\n\tok=1;\nerr:\n\tif (!ok)\n\t\t{\n\t\tif (ret != NULL) DSA_free(ret);\n\t\t}\n\telse\n\t\t{\n\t\tret->p=BN_dup(p);\n\t\tret->q=BN_dup(q);\n\t\tret->g=BN_dup(g);\n\t\tif ((m > 1) && (seed_in != NULL)) memcpy(seed_in,seed,20);\n\t\tif (counter_ret != NULL) *counter_ret=counter;\n\t\tif (h_ret != NULL) *h_ret=h;\n\t\t}\n\tif (ctx != NULL) BN_CTX_free(ctx);\n\tif (ctx != NULL) BN_CTX_free(ctx2);\n\tif (mont != NULL) BN_MONT_CTX_free(mont);\n\treturn(ok?ret:NULL);\n\t}', 'int BN_mod_exp_mont(BIGNUM *rr, BIGNUM *a, const BIGNUM *p,\n\t\t const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont)\n\t{\n\tint i,j,bits,ret=0,wstart,wend,window,wvalue;\n\tint start=1,ts=0;\n\tBIGNUM *d,*r;\n\tBIGNUM *aa;\n\tBIGNUM val[TABLE_SIZE];\n\tBN_MONT_CTX *mont=NULL;\n\tbn_check_top(a);\n\tbn_check_top(p);\n\tbn_check_top(m);\n\tif (!(m->d[0] & 1))\n\t\t{\n\t\tBNerr(BN_F_BN_MOD_EXP_MONT,BN_R_CALLED_WITH_EVEN_MODULUS);\n\t\treturn(0);\n\t\t}\n\td= &(ctx->bn[ctx->tos++]);\n\tr= &(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#if 1\n\tif (in_mont != NULL)\n\t\tmont=in_mont;\n\telse\n#endif\n\t\t{\n\t\tif ((mont=BN_MONT_CTX_new()) == NULL) goto err;\n\t\tif (!BN_MONT_CTX_set(mont,m,ctx)) goto err;\n\t\t}\n\tBN_init(&val[0]);\n\tts=1;\n\tif (BN_ucmp(a,m) >= 0)\n\t\t{\n\t\tBN_mod(&(val[0]),a,m,ctx);\n\t\taa= &(val[0]);\n\t\t}\n\telse\n\t\taa=a;\n\tif (!BN_to_montgomery(&(val[0]),aa,mont,ctx)) goto err;\n\tif (!BN_mod_mul_montgomery(d,&(val[0]),&(val[0]),mont,ctx)) goto err;\n\tif (bits <= 20)\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_montgomery(&(val[i]),&(val[i-1]),d,mont,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 if (!BN_to_montgomery(r,BN_value_one(),mont,ctx)) 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\t{\n\t\t\t\tif (!BN_mod_mul_montgomery(r,r,r,mont,ctx))\n\t\t\t\tgoto err;\n\t\t\t\t}\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_montgomery(r,r,r,mont,ctx))\n\t\t\t\t\tgoto err;\n\t\t\t\t}\n\t\tif (!BN_mod_mul_montgomery(r,r,&(val[wvalue>>1]),mont,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\tBN_from_montgomery(rr,r,mont,ctx);\n\tret=1;\nerr:\n\tif ((in_mont == NULL) && (mont != NULL)) BN_MONT_CTX_free(mont);\n\tctx->tos-=2;\n\tfor (i=0; i<ts; i++)\n\t\tBN_clear_free(&(val[i]));\n\treturn(ret);\n\t}', 'int BN_mod(BIGNUM *rem, const BIGNUM *m, const 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, const BIGNUM *num, const 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,l0;\n#ifdef BN_DIV3W\n\t\tq=bn_div_3_words(wnump,d1,d0);\n#else\n#if !defined(NO_ASM) && !defined(PEDANTIC)\n# if defined(__GNUC__) && __GNUC__>=2\n# if defined(__i386)\n# define bn_div_words(n0,n1,d0)\t\t\\\n\t({ asm volatile (\t\t\t\\\n\t\t"divl\t%4"\t\t\t\\\n\t\t: "=a"(q), "=d"(rem)\t\t\\\n\t\t: "a"(n1), "d"(n0), "g"(d0)\t\\\n\t\t: "cc");\t\t\t\\\n\t q;\t\t\t\t\t\\\n\t})\n# define REMINDER_IS_ALREADY_CALCULATED\n# endif\n# endif\n#endif\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#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#endif\n\t\t{\n#ifdef BN_LLONG\n\t\tBN_ULLONG t2;\n#ifndef REMINDER_IS_ALREADY_CALCULATED\n\t\trem=(n1-q*d0)&BN_MASK2;\n#endif\n\t\tt2=(BN_ULLONG)d1*q;\n\t\tfor (;;)\n\t\t\t{\n if (t2 <= ((((BN_ULLONG)rem)<<BN_BITS2)|wnump[-2]))\n\t\t\t\tbreak;\n\t\t\tq--;\n\t\t\trem += d0;\n\t\t\tif (rem < d0) break;\n\t\t\tt2 -= d1;\n\t\t\t}\n#else\n\t\tBN_ULONG t2l,t2h,ql,qh;\n#ifndef REMINDER_IS_ALREADY_CALCULATED\n\t\trem=(n1-q*d0)&BN_MASK2;\n#endif\n\t\tt2l=LBITS(d1); t2h=HBITS(d1);\n\t\tql =LBITS(q); qh =HBITS(q);\n\t\tmul64(t2l,t2h,ql,qh);\n\t\tfor (;;)\n\t\t\t{\n\t\t\tif ((t2h < rem) ||\n\t\t\t\t((t2h == rem) && (t2l <= wnump[-2])))\n\t\t\t\tbreak;\n\t\t\tq--;\n\t\t\trem += d0;\n\t\t\tif (rem < d0) break;\n\t\t\tif (t2l < d1) t2h--; t2l -= d1;\n\t\t\t}\n#endif\n\t\t}\n#endif\n\t\tl0=bn_mul_words(tmp->d,sdiv->d,div_n,q);\n\t\twnum.d--; wnum.top++;\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 DSA_generate_key(DSA *dsa)\n\t{\n\tint ok=0;\n\tunsigned int i;\n\tBN_CTX *ctx=NULL;\n\tBIGNUM *pub_key=NULL,*priv_key=NULL;\n\tif ((ctx=BN_CTX_new()) == NULL) goto err;\n\tif (dsa->priv_key == NULL)\n\t\t{\n\t\tif ((priv_key=BN_new()) == NULL) goto err;\n\t\t}\n\telse\n\t\tpriv_key=dsa->priv_key;\n\ti=BN_num_bits(dsa->q);\n\tfor (;;)\n\t\t{\n\t\tBN_rand(priv_key,i,1,0);\n\t\tif (BN_cmp(priv_key,dsa->q) >= 0)\n\t\t\tBN_sub(priv_key,priv_key,dsa->q);\n\t\tif (!BN_is_zero(priv_key)) break;\n\t\t}\n\tif (dsa->pub_key == NULL)\n\t\t{\n\t\tif ((pub_key=BN_new()) == NULL) goto err;\n\t\t}\n\telse\n\t\tpub_key=dsa->pub_key;\n\tif (!BN_mod_exp(pub_key,dsa->g,priv_key,dsa->p,ctx)) goto err;\n\tdsa->priv_key=priv_key;\n\tdsa->pub_key=pub_key;\n\tok=1;\nerr:\n\tif ((pub_key != NULL) && (dsa->pub_key == NULL)) BN_free(pub_key);\n\tif ((priv_key != NULL) && (dsa->priv_key == NULL)) BN_free(priv_key);\n\tif (ctx != NULL) BN_CTX_free(ctx);\n\treturn(ok);\n\t}', 'int BN_mod_exp(BIGNUM *r, BIGNUM *a, const BIGNUM *p, const BIGNUM *m,\n\t BN_CTX *ctx)\n\t{\n\tint ret;\n\tbn_check_top(a);\n\tbn_check_top(p);\n\tbn_check_top(m);\n#ifdef MONT_MUL_MOD\n\tif (BN_is_odd(m))\n\t\t{ ret=BN_mod_exp_mont(r,a,p,m,ctx,NULL); }\n\telse\n#endif\n#ifdef RECP_MUL_MOD\n\t\t{ ret=BN_mod_exp_recp(r,a,p,m,ctx); }\n#else\n\t\t{ ret=BN_mod_exp_simple(r,a,p,m,ctx); }\n#endif\n\treturn(ret);\n\t}', 'int BN_mod_exp_recp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,\n\t\t const BIGNUM *m, BN_CTX *ctx)\n\t{\n\tint i,j,bits,ret=0,wstart,wend,window,wvalue;\n\tint start=1,ts=0;\n\tBIGNUM *aa;\n\tBIGNUM val[TABLE_SIZE];\n\tBN_RECP_CTX recp;\n\taa= &(ctx->bn[ctx->tos++]);\n\tbits=BN_num_bits(p);\n\tif (bits == 0)\n\t\t{\n\t\tBN_one(r);\n\t\treturn(1);\n\t\t}\n\tBN_RECP_CTX_init(&recp);\n\tif (BN_RECP_CTX_set(&recp,m,ctx) <= 0) goto err;\n\tBN_init(&(val[0]));\n\tts=1;\n\tif (!BN_mod(&(val[0]),a,m,ctx)) goto err;\n\tif (!BN_mod_mul_reciprocal(aa,&(val[0]),&(val[0]),&recp,ctx))\n\t\tgoto err;\n\tif (bits <= 17)\n\t\twindow=1;\n\telse if (bits >= 256)\n\t\twindow=5;\n\telse if (bits >= 128)\n\t\twindow=4;\n\telse\n\t\twindow=3;\n\tj=1<<(window-1);\n\tfor (i=1; i<j; i++)\n\t\t{\n\t\tBN_init(&val[i]);\n\t\tif (!BN_mod_mul_reciprocal(&(val[i]),&(val[i-1]),aa,&recp,ctx))\n\t\t\tgoto err;\n\t\t}\n\tts=i;\n\tstart=1;\n\twvalue=0;\n\twstart=bits-1;\n\twend=0;\n\tif (!BN_one(r)) goto err;\n\tfor (;;)\n\t\t{\n\t\tif (BN_is_bit_set(p,wstart) == 0)\n\t\t\t{\n\t\t\tif (!start)\n\t\t\t\tif (!BN_mod_mul_reciprocal(r,r,r,&recp,ctx))\n\t\t\t\tgoto err;\n\t\t\tif (wstart == 0) break;\n\t\t\twstart--;\n\t\t\tcontinue;\n\t\t\t}\n\t\tj=wstart;\n\t\twvalue=1;\n\t\twend=0;\n\t\tfor (i=1; i<window; i++)\n\t\t\t{\n\t\t\tif (wstart-i < 0) break;\n\t\t\tif (BN_is_bit_set(p,wstart-i))\n\t\t\t\t{\n\t\t\t\twvalue<<=(i-wend);\n\t\t\t\twvalue|=1;\n\t\t\t\twend=i;\n\t\t\t\t}\n\t\t\t}\n\t\tj=wend+1;\n\t\tif (!start)\n\t\t\tfor (i=0; i<j; i++)\n\t\t\t\t{\n\t\t\t\tif (!BN_mod_mul_reciprocal(r,r,r,&recp,ctx))\n\t\t\t\t\tgoto err;\n\t\t\t\t}\n\t\tif (!BN_mod_mul_reciprocal(r,r,&(val[wvalue>>1]),&recp,ctx))\n\t\t\tgoto err;\n\t\twstart-=wend+1;\n\t\twvalue=0;\n\t\tstart=0;\n\t\tif (wstart < 0) break;\n\t\t}\n\tret=1;\nerr:\n\tctx->tos--;\n\tfor (i=0; i<ts; i++)\n\t\tBN_clear_free(&(val[i]));\n\tBN_RECP_CTX_free(&recp);\n\treturn(ret);\n\t}', 'int BN_RECP_CTX_set(BN_RECP_CTX *recp, const BIGNUM *d, BN_CTX *ctx)\n\t{\n\tBN_copy(&(recp->N),d);\n\tBN_zero(&(recp->Nr));\n\trecp->num_bits=BN_num_bits(d);\n\trecp->shift=0;\n\treturn(1);\n\t}', 'BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)\n\t{\n\tint i;\n\tBN_ULONG *A;\n\tconst BN_ULONG *B;\n\tbn_check_top(b);\n\tif (a == b) return(a);\n\tif (bn_wexpand(a,b->top) == NULL) return(NULL);\n#if 1\n\tA=a->d;\n\tB=b->d;\n\tfor (i=b->top>>2; i>0; i--,A+=4,B+=4)\n\t\t{\n\t\tBN_ULONG a0,a1,a2,a3;\n\t\ta0=B[0]; a1=B[1]; a2=B[2]; a3=B[3];\n\t\tA[0]=a0; A[1]=a1; A[2]=a2; A[3]=a3;\n\t\t}\n\tswitch (b->top&3)\n\t\t{\n\t\tcase 3: A[2]=B[2];\n\t\tcase 2: A[1]=B[1];\n\t\tcase 1: A[0]=B[0];\n\t\tcase 0: ;\n\t\t}\n#else\n\tmemcpy(a->d,b->d,sizeof(b->d[0])*b->top);\n#endif\n\ta->top=b->top;\n\tif ((a->top == 0) && (a->d != NULL))\n\t\ta->d[0]=0;\n\ta->neg=b->neg;\n\treturn(a);\n\t}', 'int BN_mod_mul_reciprocal(BIGNUM *r, BIGNUM *x, BIGNUM *y, BN_RECP_CTX *recp,\n\t BN_CTX *ctx)\n\t{\n\tint ret=0;\n\tBIGNUM *a;\n\ta= &(ctx->bn[ctx->tos++]);\n\tif (y != NULL)\n\t\t{\n\t\tif (x == y)\n\t\t\t{ if (!BN_sqr(a,x,ctx)) goto err; }\n\t\telse\n\t\t\t{ if (!BN_mul(a,x,y,ctx)) goto err; }\n\t\t}\n\telse\n\t\ta=x;\n\tBN_div_recp(NULL,r,a,recp,ctx);\n\tret=1;\nerr:\n\tctx->tos--;\n\treturn(ret);\n\t}', 'int BN_div_recp(BIGNUM *dv, BIGNUM *rem, BIGNUM *m, BN_RECP_CTX *recp,\n\t BN_CTX *ctx)\n\t{\n\tint i,j,tos,ret=0,ex;\n\tBIGNUM *a,*b,*d,*r;\n\ttos=ctx->tos;\n\ta= &(ctx->bn[ctx->tos++]);\n\tb= &(ctx->bn[ctx->tos++]);\n\tif (dv != NULL)\n\t\td=dv;\n\telse\n\t\td= &(ctx->bn[ctx->tos++]);\n\tif (rem != NULL)\n\t\tr=rem;\n\telse\n\t\tr= &(ctx->bn[ctx->tos++]);\n\tif (BN_ucmp(m,&(recp->N)) < 0)\n\t\t{\n\t\tBN_zero(d);\n\t\tBN_copy(r,m);\n\t\tctx->tos=tos;\n\t\treturn(1);\n\t\t}\n\ti=BN_num_bits(m);\n\tj=recp->num_bits*2;\n\tif (j > i)\n\t\t{\n\t\ti=j;\n\t\tex=0;\n\t\t}\n\telse\n\t\t{\n\t\tex=(i-j)/2;\n\t\t}\n\tj=i/2;\n\tif (i != recp->shift)\n\t\trecp->shift=BN_reciprocal(&(recp->Nr),&(recp->N),\n\t\t\ti,ctx);\n\tif (!BN_rshift(a,m,j-ex)) goto err;\n\tif (!BN_mul(b,a,&(recp->Nr),ctx)) goto err;\n\tif (!BN_rshift(d,b,j+ex)) goto err;\n\td->neg=0;\n\tif (!BN_mul(b,&(recp->N),d,ctx)) goto err;\n\tif (!BN_usub(r,m,b)) goto err;\n\tr->neg=0;\n\tj=0;\n#if 1\n\twhile (BN_ucmp(r,&(recp->N)) >= 0)\n\t\t{\n\t\tif (j++ > 2)\n\t\t\t{\n\t\t\tBNerr(BN_F_BN_MOD_MUL_RECIPROCAL,BN_R_BAD_RECIPROCAL);\n\t\t\tgoto err;\n\t\t\t}\n\t\tif (!BN_usub(r,r,&(recp->N))) goto err;\n\t\tif (!BN_add_word(d,1)) goto err;\n\t\t}\n#endif\n\tr->neg=BN_is_zero(r)?0:m->neg;\n\td->neg=m->neg^recp->N.neg;\n\tret=1;\nerr:\n\tctx->tos=tos;\n\treturn(ret);\n\t}', 'int BN_mul(BIGNUM *r, BIGNUM *a, BIGNUM *b, BN_CTX *ctx)\n\t{\n\tint top,al,bl;\n\tBIGNUM *rr;\n#ifdef BN_RECURSION\n\tBIGNUM *t;\n\tint i,j,k;\n#endif\n#ifdef BN_COUNT\nprintf("BN_mul %d * %d\\n",a->top,b->top);\n#endif\n\tbn_check_top(a);\n\tbn_check_top(b);\n\tbn_check_top(r);\n\tal=a->top;\n\tbl=b->top;\n\tr->neg=a->neg^b->neg;\n\tif ((al == 0) || (bl == 0))\n\t\t{\n\t\tBN_zero(r);\n\t\treturn(1);\n\t\t}\n\ttop=al+bl;\n\tif ((r == a) || (r == b))\n\t\trr= &(ctx->bn[ctx->tos+1]);\n\telse\n\t\trr=r;\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n\tif (al == bl)\n\t\t{\n# ifdef BN_MUL_COMBA\n if (al == 8)\n\t\t\t{\n\t\t\tif (bn_wexpand(rr,16) == NULL) return(0);\n\t\t\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\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}'] |
35,149 | 0 | https://github.com/libav/libav/blob/110d2af28e186d5ed120942f8b3d51acda59e341/avconv.c/#L3521 | static int read_avserver_streams(OptionsContext *o, AVFormatContext *s, const char *filename)
{
int i, err;
AVFormatContext *ic = avformat_alloc_context();
ic->interrupt_callback = int_cb;
err = avformat_open_input(&ic, filename, NULL, NULL);
if (err < 0)
return err;
for(i=0;i<ic->nb_streams;i++) {
AVStream *st;
OutputStream *ost;
AVCodec *codec;
codec = avcodec_find_encoder(ic->streams[i]->codec->codec_id);
ost = new_output_stream(o, s, codec->type);
st = ost->st;
memcpy(st, ic->streams[i], sizeof(AVStream));
st->info = NULL;
avcodec_copy_context(st->codec, ic->streams[i]->codec);
if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO && !ost->stream_copy)
choose_sample_fmt(st, codec);
else if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO && !ost->stream_copy)
choose_pixel_fmt(st, codec);
}
av_close_input_file(ic);
return 0;
} | ['static int read_avserver_streams(OptionsContext *o, AVFormatContext *s, const char *filename)\n{\n int i, err;\n AVFormatContext *ic = avformat_alloc_context();\n ic->interrupt_callback = int_cb;\n err = avformat_open_input(&ic, filename, NULL, NULL);\n if (err < 0)\n return err;\n for(i=0;i<ic->nb_streams;i++) {\n AVStream *st;\n OutputStream *ost;\n AVCodec *codec;\n codec = avcodec_find_encoder(ic->streams[i]->codec->codec_id);\n ost = new_output_stream(o, s, codec->type);\n st = ost->st;\n memcpy(st, ic->streams[i], sizeof(AVStream));\n st->info = NULL;\n avcodec_copy_context(st->codec, ic->streams[i]->codec);\n if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO && !ost->stream_copy)\n choose_sample_fmt(st, codec);\n else if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO && !ost->stream_copy)\n choose_pixel_fmt(st, codec);\n }\n av_close_input_file(ic);\n return 0;\n}', 'AVFormatContext *avformat_alloc_context(void)\n{\n AVFormatContext *ic;\n ic = av_malloc(sizeof(AVFormatContext));\n if (!ic) return ic;\n avformat_get_context_defaults(ic);\n return ic;\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}'] |
35,150 | 0 | https://github.com/openssl/openssl/blob/d40a1b865fddc3d67f8c06ff1f1466fad331c8f7/crypto/bn/bn_lib.c/#L250 | int BN_num_bits(const BIGNUM *a)
{
int i = a->top - 1;
bn_check_top(a);
if (BN_is_zero(a)) return 0;
return ((i*BN_BITS2) + BN_num_bits_word(a->d[i]));
} | ['static int atalla_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,\n\t\t\tconst BIGNUM *m, BN_CTX *ctx)\n\t{\n\tBIGNUM *modulus;\n\tBIGNUM *exponent;\n\tBIGNUM *argument;\n\tBIGNUM *result;\n\tRSAPrivateKey keydata;\n\tint to_return, numbytes;\n\tmodulus = exponent = argument = result = NULL;\n\tto_return = 0;\n\tif(!atalla_dso)\n\t\t{\n\t\tATALLAerr(ATALLA_F_ATALLA_MOD_EXP,ATALLA_R_NOT_LOADED);\n\t\tgoto err;\n\t\t}\n\tBN_CTX_start(ctx);\n\tmodulus = BN_CTX_get(ctx);\n\texponent = BN_CTX_get(ctx);\n\targument = BN_CTX_get(ctx);\n\tresult = BN_CTX_get(ctx);\n\tif (!result)\n\t\t{\n\t\tATALLAerr(ATALLA_F_ATALLA_MOD_EXP,ATALLA_R_BN_CTX_FULL);\n\t\tgoto err;\n\t\t}\n\tif(!bn_wexpand(modulus, m->top) || !bn_wexpand(exponent, m->top) ||\n\t !bn_wexpand(argument, m->top) || !bn_wexpand(result, m->top))\n\t\t{\n\t\tATALLAerr(ATALLA_F_ATALLA_MOD_EXP,ATALLA_R_BN_EXPAND_FAIL);\n\t\tgoto err;\n\t\t}\n\tmemset(&keydata, 0,sizeof keydata);\n\tnumbytes = BN_num_bytes(m);\n\tmemset(exponent->d, 0, numbytes);\n\tmemset(modulus->d, 0, numbytes);\n\tBN_bn2bin(p, (unsigned char *)exponent->d + numbytes - BN_num_bytes(p));\n\tBN_bn2bin(m, (unsigned char *)modulus->d + numbytes - BN_num_bytes(m));\n\tkeydata.privateExponent.data = (unsigned char *)exponent->d;\n\tkeydata.privateExponent.len = numbytes;\n\tkeydata.modulus.data = (unsigned char *)modulus->d;\n\tkeydata.modulus.len = numbytes;\n\tmemset(argument->d, 0, numbytes);\n\tmemset(result->d, 0, numbytes);\n\tBN_bn2bin(a, (unsigned char *)argument->d + numbytes - BN_num_bytes(a));\n\tif(p_Atalla_RSAPrivateKeyOpFn(&keydata, (unsigned char *)result->d,\n\t\t\t(unsigned char *)argument->d,\n\t\t\tkeydata.modulus.len) != 0)\n\t\t{\n\t\tATALLAerr(ATALLA_F_ATALLA_MOD_EXP,ATALLA_R_REQUEST_FAILED);\n\t\tgoto err;\n\t\t}\n\tBN_bin2bn((unsigned char *)result->d, numbytes, r);\n\tto_return = 1;\nerr:\n\tBN_CTX_end(ctx);\n\treturn to_return;\n\t}', 'int BN_num_bits(const BIGNUM *a)\n\t{\n\tint i = a->top - 1;\n\tbn_check_top(a);\n\tif (BN_is_zero(a)) return 0;\n\treturn ((i*BN_BITS2) + BN_num_bits_word(a->d[i]));\n\t}', 'int BN_bn2bin(const BIGNUM *a, unsigned char *to)\n\t{\n\tint n,i;\n\tBN_ULONG l;\n\tbn_check_top(a);\n\tn=i=BN_num_bytes(a);\n\twhile (i--)\n\t\t{\n\t\tl=a->d[i/BN_BYTES];\n\t\t*(to++)=(unsigned char)(l>>(8*(i%BN_BYTES)))&0xff;\n\t\t}\n\treturn(n);\n\t}'] |
35,151 | 0 | https://github.com/openssl/openssl/blob/e7d961e994620dd5dee6d80794a07fb9de1bab66/ssl/packet_locl.h/#L36 | static ossl_inline void packet_forward(PACKET *pkt, size_t len)
{
pkt->curr += len;
pkt->remaining -= len;
} | ['MSG_PROCESS_RETURN tls_process_client_hello(SSL *s, PACKET *pkt)\n{\n PACKET session_id, compression, extensions, cookie;\n static const unsigned char null_compression = 0;\n CLIENTHELLO_MSG *clienthello;\n clienthello = OPENSSL_zalloc(sizeof(*clienthello));\n if (clienthello == NULL) {\n SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CLIENT_HELLO,\n ERR_R_INTERNAL_ERROR);\n goto err;\n }\n if (s->renegotiate == 0 && !SSL_IS_FIRST_HANDSHAKE(s)) {\n if ((s->options & SSL_OP_NO_RENEGOTIATION)) {\n ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_RENEGOTIATION);\n goto err;\n }\n s->renegotiate = 1;\n s->new_session = 1;\n }\n clienthello->isv2 = RECORD_LAYER_is_sslv2_record(&s->rlayer);\n PACKET_null_init(&cookie);\n if (clienthello->isv2) {\n unsigned int mt;\n if (!SSL_IS_FIRST_HANDSHAKE(s) || s->hello_retry_request) {\n SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE,\n SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_UNEXPECTED_MESSAGE);\n goto err;\n }\n if (!PACKET_get_1(pkt, &mt)\n || mt != SSL2_MT_CLIENT_HELLO) {\n SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CLIENT_HELLO,\n ERR_R_INTERNAL_ERROR);\n goto err;\n }\n }\n if (!PACKET_get_net_2(pkt, &clienthello->legacy_version)) {\n SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CLIENT_HELLO,\n SSL_R_LENGTH_TOO_SHORT);\n goto err;\n }\n if (clienthello->isv2) {\n unsigned int ciphersuite_len, session_id_len, challenge_len;\n PACKET challenge;\n if (!PACKET_get_net_2(pkt, &ciphersuite_len)\n || !PACKET_get_net_2(pkt, &session_id_len)\n || !PACKET_get_net_2(pkt, &challenge_len)) {\n SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CLIENT_HELLO,\n SSL_R_RECORD_LENGTH_MISMATCH);\n goto err;\n }\n if (session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {\n SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,\n SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);\n goto err;\n }\n if (!PACKET_get_sub_packet(pkt, &clienthello->ciphersuites,\n ciphersuite_len)\n || !PACKET_copy_bytes(pkt, clienthello->session_id, session_id_len)\n || !PACKET_get_sub_packet(pkt, &challenge, challenge_len)\n || PACKET_remaining(pkt) != 0) {\n SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CLIENT_HELLO,\n SSL_R_RECORD_LENGTH_MISMATCH);\n goto err;\n }\n clienthello->session_id_len = session_id_len;\n challenge_len = challenge_len > SSL3_RANDOM_SIZE\n ? SSL3_RANDOM_SIZE : challenge_len;\n memset(clienthello->random, 0, SSL3_RANDOM_SIZE);\n if (!PACKET_copy_bytes(&challenge,\n clienthello->random + SSL3_RANDOM_SIZE -\n challenge_len, challenge_len)\n || !PACKET_buf_init(&compression, &null_compression, 1)) {\n SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CLIENT_HELLO,\n ERR_R_INTERNAL_ERROR);\n goto err;\n }\n PACKET_null_init(&clienthello->extensions);\n } else {\n if (!PACKET_copy_bytes(pkt, clienthello->random, SSL3_RANDOM_SIZE)\n || !PACKET_get_length_prefixed_1(pkt, &session_id)\n || !PACKET_copy_all(&session_id, clienthello->session_id,\n SSL_MAX_SSL_SESSION_ID_LENGTH,\n &clienthello->session_id_len)) {\n SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CLIENT_HELLO,\n SSL_R_LENGTH_MISMATCH);\n goto err;\n }\n if (SSL_IS_DTLS(s)) {\n if (!PACKET_get_length_prefixed_1(pkt, &cookie)) {\n SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CLIENT_HELLO,\n SSL_R_LENGTH_MISMATCH);\n goto err;\n }\n if (!PACKET_copy_all(&cookie, clienthello->dtls_cookie,\n DTLS1_COOKIE_LENGTH,\n &clienthello->dtls_cookie_len)) {\n SSLfatal(s, SSL_AD_INTERNAL_ERROR,\n SSL_F_TLS_PROCESS_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);\n goto err;\n }\n if (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) {\n if (clienthello->dtls_cookie_len == 0)\n return MSG_PROCESS_FINISHED_READING;\n }\n }\n if (!PACKET_get_length_prefixed_2(pkt, &clienthello->ciphersuites)) {\n SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CLIENT_HELLO,\n SSL_R_LENGTH_MISMATCH);\n goto err;\n }\n if (!PACKET_get_length_prefixed_1(pkt, &compression)) {\n SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CLIENT_HELLO,\n SSL_R_LENGTH_MISMATCH);\n goto err;\n }\n if (PACKET_remaining(pkt) == 0) {\n PACKET_null_init(&clienthello->extensions);\n } else {\n if (!PACKET_get_length_prefixed_2(pkt, &clienthello->extensions)\n || PACKET_remaining(pkt) != 0) {\n SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CLIENT_HELLO,\n SSL_R_LENGTH_MISMATCH);\n goto err;\n }\n }\n }\n if (!PACKET_copy_all(&compression, clienthello->compressions,\n MAX_COMPRESSIONS_SIZE,\n &clienthello->compressions_len)) {\n SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CLIENT_HELLO,\n ERR_R_INTERNAL_ERROR);\n goto err;\n }\n extensions = clienthello->extensions;\n if (!tls_collect_extensions(s, &extensions, SSL_EXT_CLIENT_HELLO,\n &clienthello->pre_proc_exts,\n &clienthello->pre_proc_exts_len, 1)) {\n goto err;\n }\n s->clienthello = clienthello;\n return MSG_PROCESS_CONTINUE_PROCESSING;\n err:\n if (clienthello != NULL)\n OPENSSL_free(clienthello->pre_proc_exts);\n OPENSSL_free(clienthello);\n return MSG_PROCESS_ERROR;\n}', 'static ossl_inline int PACKET_get_net_2(PACKET *pkt, unsigned int *data)\n{\n if (!PACKET_peek_net_2(pkt, data))\n return 0;\n packet_forward(pkt, 2);\n return 1;\n}', 'static ossl_inline int PACKET_peek_net_2(const PACKET *pkt,\n unsigned int *data)\n{\n if (PACKET_remaining(pkt) < 2)\n return 0;\n *data = ((unsigned int)(*pkt->curr)) << 8;\n *data |= *(pkt->curr + 1);\n return 1;\n}', 'static ossl_inline int PACKET_copy_bytes(PACKET *pkt,\n unsigned char *data, size_t len)\n{\n if (!PACKET_peek_copy_bytes(pkt, data, len))\n return 0;\n packet_forward(pkt, len);\n return 1;\n}', 'static ossl_inline void packet_forward(PACKET *pkt, size_t len)\n{\n pkt->curr += len;\n pkt->remaining -= len;\n}'] |
35,152 | 0 | https://github.com/openssl/openssl/blob/8da94770f0a049497b1a52ee469cca1f4a13b1a7/crypto/bn/bn_mul.c/#L1058 | void bn_mul_normal(BN_ULONG *r, BN_ULONG *a, int na, BN_ULONG *b, int nb)
{
BN_ULONG *rr;
if (na < nb) {
int itmp;
BN_ULONG *ltmp;
itmp = na;
na = nb;
nb = itmp;
ltmp = a;
a = b;
b = ltmp;
}
rr = &(r[na]);
if (nb <= 0) {
(void)bn_mul_words(r, a, na, 0);
return;
} else
rr[0] = bn_mul_words(r, a, na, b[0]);
for (;;) {
if (--nb <= 0)
return;
rr[1] = bn_mul_add_words(&(r[1]), a, na, b[1]);
if (--nb <= 0)
return;
rr[2] = bn_mul_add_words(&(r[2]), a, na, b[2]);
if (--nb <= 0)
return;
rr[3] = bn_mul_add_words(&(r[3]), a, na, b[3]);
if (--nb <= 0)
return;
rr[4] = bn_mul_add_words(&(r[4]), a, na, b[4]);
rr += 4;
r += 4;
b += 4;
}
} | ['int ec_GFp_mont_group_set_curve(EC_GROUP *group, const BIGNUM *p,\n const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)\n{\n BN_CTX *new_ctx = NULL;\n BN_MONT_CTX *mont = NULL;\n BIGNUM *one = NULL;\n int ret = 0;\n BN_MONT_CTX_free(group->field_data1);\n group->field_data1 = NULL;\n BN_free(group->field_data2);\n group->field_data2 = NULL;\n if (ctx == NULL) {\n ctx = new_ctx = BN_CTX_new();\n if (ctx == NULL)\n return 0;\n }\n mont = BN_MONT_CTX_new();\n if (mont == NULL)\n goto err;\n if (!BN_MONT_CTX_set(mont, p, ctx)) {\n ECerr(EC_F_EC_GFP_MONT_GROUP_SET_CURVE, ERR_R_BN_LIB);\n goto err;\n }\n one = BN_new();\n if (one == NULL)\n goto err;\n if (!BN_to_montgomery(one, BN_value_one(), mont, ctx))\n goto err;\n group->field_data1 = mont;\n mont = NULL;\n group->field_data2 = one;\n one = NULL;\n ret = ec_GFp_simple_group_set_curve(group, p, a, b, ctx);\n if (!ret) {\n BN_MONT_CTX_free(group->field_data1);\n group->field_data1 = NULL;\n BN_free(group->field_data2);\n group->field_data2 = NULL;\n }\n err:\n BN_CTX_free(new_ctx);\n BN_MONT_CTX_free(mont);\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}', 'int BN_to_montgomery(BIGNUM *r, const BIGNUM *a, BN_MONT_CTX *mont,\n BN_CTX *ctx)\n{\n return BN_mod_mul_montgomery(r, a, &(mont->RR), mont, ctx);\n}', 'int BN_mod_mul_montgomery(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,\n BN_MONT_CTX *mont, BN_CTX *ctx)\n{\n BIGNUM *tmp;\n int ret = 0;\n#if defined(OPENSSL_BN_ASM_MONT) && defined(MONT_WORD)\n int num = mont->N.top;\n if (num > 1 && a->top == num && b->top == num) {\n if (bn_wexpand(r, num) == NULL)\n return (0);\n if (bn_mul_mont(r->d, a->d, b->d, mont->N.d, mont->n0, num)) {\n r->neg = a->neg ^ b->neg;\n r->top = num;\n bn_correct_top(r);\n return (1);\n }\n }\n#endif\n BN_CTX_start(ctx);\n tmp = BN_CTX_get(ctx);\n if (tmp == NULL)\n goto err;\n bn_check_top(tmp);\n if (a == b) {\n if (!BN_sqr(tmp, a, ctx))\n goto err;\n } else {\n if (!BN_mul(tmp, a, b, ctx))\n goto err;\n }\n#ifdef MONT_WORD\n if (!BN_from_montgomery_word(r, tmp, mont))\n goto err;\n#else\n if (!BN_from_montgomery(r, tmp, mont, ctx))\n goto err;\n#endif\n bn_check_top(r);\n ret = 1;\n err:\n BN_CTX_end(ctx);\n return (ret);\n}', 'int BN_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)\n{\n int ret = 0;\n int top, al, bl;\n BIGNUM *rr;\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n int i;\n#endif\n#ifdef BN_RECURSION\n BIGNUM *t = NULL;\n int j = 0, k;\n#endif\n bn_check_top(a);\n bn_check_top(b);\n bn_check_top(r);\n al = a->top;\n bl = b->top;\n if ((al == 0) || (bl == 0)) {\n BN_zero(r);\n return (1);\n }\n top = al + bl;\n BN_CTX_start(ctx);\n if ((r == a) || (r == b)) {\n if ((rr = BN_CTX_get(ctx)) == NULL)\n goto err;\n } else\n rr = r;\n rr->neg = a->neg ^ b->neg;\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n i = al - bl;\n#endif\n#ifdef BN_MUL_COMBA\n if (i == 0) {\n# if 0\n if (al == 4) {\n if (bn_wexpand(rr, 8) == NULL)\n goto err;\n rr->top = 8;\n bn_mul_comba4(rr->d, a->d, b->d);\n goto end;\n }\n# endif\n if (al == 8) {\n if (bn_wexpand(rr, 16) == NULL)\n goto err;\n rr->top = 16;\n bn_mul_comba8(rr->d, a->d, b->d);\n goto end;\n }\n }\n#endif\n#ifdef BN_RECURSION\n if ((al >= BN_MULL_SIZE_NORMAL) && (bl >= BN_MULL_SIZE_NORMAL)) {\n if (i >= -1 && i <= 1) {\n if (i >= 0) {\n j = BN_num_bits_word((BN_ULONG)al);\n }\n if (i == -1) {\n j = BN_num_bits_word((BN_ULONG)bl);\n }\n j = 1 << (j - 1);\n assert(j <= al || j <= bl);\n k = j + j;\n t = BN_CTX_get(ctx);\n if (t == NULL)\n goto err;\n if (al > j || bl > j) {\n if (bn_wexpand(t, k * 4) == NULL)\n goto err;\n if (bn_wexpand(rr, k * 4) == NULL)\n goto err;\n bn_mul_part_recursive(rr->d, a->d, b->d,\n j, al - j, bl - j, t->d);\n } else {\n if (bn_wexpand(t, k * 2) == NULL)\n goto err;\n if (bn_wexpand(rr, k * 2) == NULL)\n goto err;\n bn_mul_recursive(rr->d, a->d, b->d, j, al - j, bl - j, t->d);\n }\n rr->top = top;\n goto end;\n }\n# if 0\n if (i == 1 && !BN_get_flags(b, BN_FLG_STATIC_DATA)) {\n BIGNUM *tmp_bn = (BIGNUM *)b;\n if (bn_wexpand(tmp_bn, al) == NULL)\n goto err;\n tmp_bn->d[bl] = 0;\n bl++;\n i--;\n } else if (i == -1 && !BN_get_flags(a, BN_FLG_STATIC_DATA)) {\n BIGNUM *tmp_bn = (BIGNUM *)a;\n if (bn_wexpand(tmp_bn, bl) == NULL)\n goto err;\n tmp_bn->d[al] = 0;\n al++;\n i++;\n }\n if (i == 0) {\n j = BN_num_bits_word((BN_ULONG)al);\n j = 1 << (j - 1);\n k = j + j;\n t = BN_CTX_get(ctx);\n if (al == j) {\n if (bn_wexpand(t, k * 2) == NULL)\n goto err;\n if (bn_wexpand(rr, k * 2) == NULL)\n goto err;\n bn_mul_recursive(rr->d, a->d, b->d, al, t->d);\n } else {\n if (bn_wexpand(t, k * 4) == NULL)\n goto err;\n if (bn_wexpand(rr, k * 4) == NULL)\n goto err;\n bn_mul_part_recursive(rr->d, a->d, b->d, al - j, j, t->d);\n }\n rr->top = top;\n goto end;\n }\n# endif\n }\n#endif\n if (bn_wexpand(rr, top) == NULL)\n goto err;\n rr->top = top;\n bn_mul_normal(rr->d, a->d, al, b->d, bl);\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n end:\n#endif\n bn_correct_top(rr);\n if (r != rr)\n BN_copy(r, rr);\n ret = 1;\n err:\n bn_check_top(r);\n BN_CTX_end(ctx);\n return (ret);\n}', 'void bn_mul_part_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n,\n int tna, int tnb, BN_ULONG *t)\n{\n int i, j, n2 = n * 2;\n int c1, c2, neg;\n BN_ULONG ln, lo, *p;\n if (n < 8) {\n bn_mul_normal(r, a, n + tna, b, n + tnb);\n return;\n }\n c1 = bn_cmp_part_words(a, &(a[n]), tna, n - tna);\n c2 = bn_cmp_part_words(&(b[n]), b, tnb, tnb - n);\n neg = 0;\n switch (c1 * 3 + c2) {\n case -4:\n bn_sub_part_words(t, &(a[n]), a, tna, tna - n);\n bn_sub_part_words(&(t[n]), b, &(b[n]), tnb, n - tnb);\n break;\n case -3:\n case -2:\n bn_sub_part_words(t, &(a[n]), a, tna, tna - n);\n bn_sub_part_words(&(t[n]), &(b[n]), b, tnb, tnb - n);\n neg = 1;\n break;\n case -1:\n case 0:\n case 1:\n case 2:\n bn_sub_part_words(t, a, &(a[n]), tna, n - tna);\n bn_sub_part_words(&(t[n]), b, &(b[n]), tnb, n - tnb);\n neg = 1;\n break;\n case 3:\n case 4:\n bn_sub_part_words(t, a, &(a[n]), tna, n - tna);\n bn_sub_part_words(&(t[n]), &(b[n]), b, tnb, tnb - n);\n break;\n }\n# if 0\n if (n == 4) {\n bn_mul_comba4(&(t[n2]), t, &(t[n]));\n bn_mul_comba4(r, a, b);\n bn_mul_normal(&(r[n2]), &(a[n]), tn, &(b[n]), tn);\n memset(&r[n2 + tn * 2], 0, sizeof(*r) * (n2 - tn * 2));\n } else\n# endif\n if (n == 8) {\n bn_mul_comba8(&(t[n2]), t, &(t[n]));\n bn_mul_comba8(r, a, b);\n bn_mul_normal(&(r[n2]), &(a[n]), tna, &(b[n]), tnb);\n memset(&r[n2 + tna + tnb], 0, sizeof(*r) * (n2 - tna - tnb));\n } else {\n p = &(t[n2 * 2]);\n bn_mul_recursive(&(t[n2]), t, &(t[n]), n, 0, 0, p);\n bn_mul_recursive(r, a, b, n, 0, 0, p);\n i = n / 2;\n if (tna > tnb)\n j = tna - i;\n else\n j = tnb - i;\n if (j == 0) {\n bn_mul_recursive(&(r[n2]), &(a[n]), &(b[n]),\n i, tna - i, tnb - i, p);\n memset(&r[n2 + i * 2], 0, sizeof(*r) * (n2 - i * 2));\n } else if (j > 0) {\n bn_mul_part_recursive(&(r[n2]), &(a[n]), &(b[n]),\n i, tna - i, tnb - i, p);\n memset(&(r[n2 + tna + tnb]), 0,\n sizeof(BN_ULONG) * (n2 - tna - tnb));\n } else {\n memset(&r[n2], 0, sizeof(*r) * n2);\n if (tna < BN_MUL_RECURSIVE_SIZE_NORMAL\n && tnb < BN_MUL_RECURSIVE_SIZE_NORMAL) {\n bn_mul_normal(&(r[n2]), &(a[n]), tna, &(b[n]), tnb);\n } else {\n for (;;) {\n i /= 2;\n if (i < tna || i < tnb) {\n bn_mul_part_recursive(&(r[n2]),\n &(a[n]), &(b[n]),\n i, tna - i, tnb - i, p);\n break;\n } else if (i == tna || i == tnb) {\n bn_mul_recursive(&(r[n2]),\n &(a[n]), &(b[n]),\n i, tna - i, tnb - i, p);\n break;\n }\n }\n }\n }\n }\n c1 = (int)(bn_add_words(t, r, &(r[n2]), n2));\n if (neg) {\n c1 -= (int)(bn_sub_words(&(t[n2]), t, &(t[n2]), n2));\n } else {\n c1 += (int)(bn_add_words(&(t[n2]), &(t[n2]), t, n2));\n }\n c1 += (int)(bn_add_words(&(r[n]), &(r[n]), &(t[n2]), n2));\n if (c1) {\n p = &(r[n + n2]);\n lo = *p;\n ln = (lo + c1) & BN_MASK2;\n *p = ln;\n if (ln < (BN_ULONG)c1) {\n do {\n p++;\n lo = *p;\n ln = (lo + 1) & BN_MASK2;\n *p = ln;\n } while (ln == 0);\n }\n }\n}', 'void bn_mul_normal(BN_ULONG *r, BN_ULONG *a, int na, BN_ULONG *b, int nb)\n{\n BN_ULONG *rr;\n if (na < nb) {\n int itmp;\n BN_ULONG *ltmp;\n itmp = na;\n na = nb;\n nb = itmp;\n ltmp = a;\n a = b;\n b = ltmp;\n }\n rr = &(r[na]);\n if (nb <= 0) {\n (void)bn_mul_words(r, a, na, 0);\n return;\n } else\n rr[0] = bn_mul_words(r, a, na, b[0]);\n for (;;) {\n if (--nb <= 0)\n return;\n rr[1] = bn_mul_add_words(&(r[1]), a, na, b[1]);\n if (--nb <= 0)\n return;\n rr[2] = bn_mul_add_words(&(r[2]), a, na, b[2]);\n if (--nb <= 0)\n return;\n rr[3] = bn_mul_add_words(&(r[3]), a, na, b[3]);\n if (--nb <= 0)\n return;\n rr[4] = bn_mul_add_words(&(r[4]), a, na, b[4]);\n rr += 4;\n r += 4;\n b += 4;\n }\n}'] |
35,153 | 0 | https://gitlab.com/libtiff/libtiff/blob/3adc33842b7533066daea2516741832edc44d5fd/libtiff/tif_tile.c/#L157 | uint32
TIFFNumberOfTiles(TIFF* tif)
{
TIFFDirectory *td = &tif->tif_dir;
uint32 dx = td->td_tilewidth;
uint32 dy = td->td_tilelength;
uint32 dz = td->td_tiledepth;
uint32 ntiles;
if (dx == (uint32) -1)
dx = td->td_imagewidth;
if (dy == (uint32) -1)
dy = td->td_imagelength;
if (dz == (uint32) -1)
dz = td->td_imagedepth;
ntiles = (dx == 0 || dy == 0 || dz == 0) ? 0 :
multiply_32(tif, multiply_32(tif, TIFFhowmany_32(td->td_imagewidth, dx),
TIFFhowmany_32(td->td_imagelength, dy),
"TIFFNumberOfTiles"),
TIFFhowmany_32(td->td_imagedepth, dz), "TIFFNumberOfTiles");
if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
ntiles = multiply_32(tif, ntiles, td->td_samplesperpixel,
"TIFFNumberOfTiles");
return (ntiles);
} | ['static int\ncpTiles(TIFF* in, TIFF* out)\n{\n tsize_t bufsize = TIFFTileSize(in);\n unsigned char *buf = (unsigned char *)_TIFFmalloc(bufsize);\n if (buf) {\n\tttile_t t, nt = TIFFNumberOfTiles(in);\n\tuint64 *bytecounts;\n\tTIFFGetField(in, TIFFTAG_TILEBYTECOUNTS, &bytecounts);\n\tfor (t = 0; t < nt; t++) {\n\t if (bytecounts[t] > (uint64) bufsize) {\n\t\tbuf = (unsigned char *)_TIFFrealloc(buf, (tmsize_t)bytecounts[t]);\n\t\tif (!buf)\n\t\t goto bad;\n\t\tbufsize = (tmsize_t)bytecounts[t];\n\t }\n\t if (TIFFReadRawTile(in, t, buf, (tmsize_t)bytecounts[t]) < 0 ||\n\t\tTIFFWriteRawTile(out, t, buf, (tmsize_t)bytecounts[t]) < 0) {\n\t\t_TIFFfree(buf);\n\t\treturn 0;\n\t }\n\t}\n\t_TIFFfree(buf);\n\treturn 1;\n }\nbad:\n TIFFError(TIFFFileName(in),\n\t\t "Can\'t allocate space for tile buffer.");\n\treturn (0);\n}', 'tmsize_t\nTIFFWriteRawTile(TIFF* tif, uint32 tile, void* data, tmsize_t cc)\n{\n\tstatic const char module[] = "TIFFWriteRawTile";\n\tif (!WRITECHECKTILES(tif, module))\n\t\treturn ((tmsize_t)(-1));\n\tif (tile >= tif->tif_dir.td_nstrips) {\n\t\tTIFFErrorExt(tif->tif_clientdata, module, "Tile %lu out of range, max %lu",\n\t\t (unsigned long) tile,\n\t\t (unsigned long) tif->tif_dir.td_nstrips);\n\t\treturn ((tmsize_t)(-1));\n\t}\n\treturn (TIFFAppendToStrip(tif, tile, (uint8*) data, cc) ?\n\t cc : (tmsize_t)(-1));\n}', 'int\nTIFFWriteCheck(TIFF* tif, int tiles, const char* module)\n{\n\tif (tif->tif_mode == O_RDONLY) {\n\t\tTIFFErrorExt(tif->tif_clientdata, module, "File not open for writing");\n\t\treturn (0);\n\t}\n\tif (tiles ^ isTiled(tif)) {\n\t\tTIFFErrorExt(tif->tif_clientdata, module, tiles ?\n\t\t "Can not write tiles to a stripped image" :\n\t\t "Can not write scanlines to a tiled image");\n\t\treturn (0);\n\t}\n\tif (!TIFFFieldSet(tif, FIELD_IMAGEDIMENSIONS)) {\n\t\tTIFFErrorExt(tif->tif_clientdata, module,\n\t\t "Must set \\"ImageWidth\\" before writing data");\n\t\treturn (0);\n\t}\n\tif (tif->tif_dir.td_samplesperpixel == 1) {\n\t\tif (!TIFFFieldSet(tif, FIELD_PLANARCONFIG))\n tif->tif_dir.td_planarconfig = PLANARCONFIG_CONTIG;\n\t} else {\n\t\tif (!TIFFFieldSet(tif, FIELD_PLANARCONFIG)) {\n\t\t\tTIFFErrorExt(tif->tif_clientdata, module,\n\t\t\t "Must set \\"PlanarConfiguration\\" before writing data");\n\t\t\treturn (0);\n\t\t}\n\t}\n\tif (tif->tif_dir.td_stripoffset == NULL && !TIFFSetupStrips(tif)) {\n\t\ttif->tif_dir.td_nstrips = 0;\n\t\tTIFFErrorExt(tif->tif_clientdata, module, "No space for %s arrays",\n\t\t isTiled(tif) ? "tile" : "strip");\n\t\treturn (0);\n\t}\n\tif (isTiled(tif))\n\t{\n\t\ttif->tif_tilesize = TIFFTileSize(tif);\n\t\tif (tif->tif_tilesize == 0)\n\t\t\treturn (0);\n\t}\n\telse\n\t\ttif->tif_tilesize = (tmsize_t)(-1);\n\ttif->tif_scanlinesize = TIFFScanlineSize(tif);\n\tif (tif->tif_scanlinesize == 0)\n\t\treturn (0);\n\ttif->tif_flags |= TIFF_BEENWRITING;\n\treturn (1);\n}', 'int\nTIFFSetupStrips(TIFF* tif)\n{\n\tTIFFDirectory* td = &tif->tif_dir;\n\tif (isTiled(tif))\n\t\ttd->td_stripsperimage =\n\t\t isUnspecified(tif, FIELD_TILEDIMENSIONS) ?\n\t\t\ttd->td_samplesperpixel : TIFFNumberOfTiles(tif);\n\telse\n\t\ttd->td_stripsperimage =\n\t\t isUnspecified(tif, FIELD_ROWSPERSTRIP) ?\n\t\t\ttd->td_samplesperpixel : TIFFNumberOfStrips(tif);\n\ttd->td_nstrips = td->td_stripsperimage;\n\tif (td->td_planarconfig == PLANARCONFIG_SEPARATE)\n\t\ttd->td_stripsperimage /= td->td_samplesperpixel;\n\ttd->td_stripoffset = (uint64 *)\n\t _TIFFmalloc(td->td_nstrips * sizeof (uint64));\n\ttd->td_stripbytecount = (uint64 *)\n\t _TIFFmalloc(td->td_nstrips * sizeof (uint64));\n\tif (td->td_stripoffset == NULL || td->td_stripbytecount == NULL)\n\t\treturn (0);\n\t_TIFFmemset(td->td_stripoffset, 0, td->td_nstrips*sizeof (uint64));\n\t_TIFFmemset(td->td_stripbytecount, 0, td->td_nstrips*sizeof (uint64));\n\tTIFFSetFieldBit(tif, FIELD_STRIPOFFSETS);\n\tTIFFSetFieldBit(tif, FIELD_STRIPBYTECOUNTS);\n\treturn (1);\n}', 'uint32\nTIFFNumberOfTiles(TIFF* tif)\n{\n\tTIFFDirectory *td = &tif->tif_dir;\n\tuint32 dx = td->td_tilewidth;\n\tuint32 dy = td->td_tilelength;\n\tuint32 dz = td->td_tiledepth;\n\tuint32 ntiles;\n\tif (dx == (uint32) -1)\n\t\tdx = td->td_imagewidth;\n\tif (dy == (uint32) -1)\n\t\tdy = td->td_imagelength;\n\tif (dz == (uint32) -1)\n\t\tdz = td->td_imagedepth;\n\tntiles = (dx == 0 || dy == 0 || dz == 0) ? 0 :\n\t multiply_32(tif, multiply_32(tif, TIFFhowmany_32(td->td_imagewidth, dx),\n\t TIFFhowmany_32(td->td_imagelength, dy),\n\t "TIFFNumberOfTiles"),\n\t TIFFhowmany_32(td->td_imagedepth, dz), "TIFFNumberOfTiles");\n\tif (td->td_planarconfig == PLANARCONFIG_SEPARATE)\n\t\tntiles = multiply_32(tif, ntiles, td->td_samplesperpixel,\n\t\t "TIFFNumberOfTiles");\n\treturn (ntiles);\n}'] |
35,154 | 0 | https://github.com/openssl/openssl/blob/55442b8a5b719f54578083fae0fcc814b599cd84/crypto/bn/bn_lib.c/#L233 | static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)
{
BN_ULONG *a = NULL;
bn_check_top(b);
if (words > (INT_MAX / (4 * BN_BITS2))) {
BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);
return NULL;
}
if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {
BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);
return NULL;
}
if (BN_get_flags(b, BN_FLG_SECURE))
a = OPENSSL_secure_zalloc(words * sizeof(*a));
else
a = OPENSSL_zalloc(words * sizeof(*a));
if (a == NULL) {
BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);
return NULL;
}
assert(b->top <= words);
if (b->top > 0)
memcpy(a, b->d, sizeof(*a) * b->top);
return a;
} | ['int BN_mod_sub_quick(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,\n const BIGNUM *m)\n{\n if (!BN_sub(r, a, b))\n return 0;\n if (r->neg)\n return BN_add(r, r, m);\n return 1;\n}', 'int BN_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)\n{\n int ret, r_neg, cmp_res;\n bn_check_top(a);\n bn_check_top(b);\n if (a->neg != b->neg) {\n r_neg = a->neg;\n ret = BN_uadd(r, a, b);\n } else {\n cmp_res = BN_ucmp(a, b);\n if (cmp_res > 0) {\n r_neg = a->neg;\n ret = BN_usub(r, a, b);\n } else if (cmp_res < 0) {\n r_neg = !b->neg;\n ret = BN_usub(r, b, a);\n } else {\n r_neg = 0;\n BN_zero(r);\n ret = 1;\n }\n }\n r->neg = r_neg;\n bn_check_top(r);\n return ret;\n}', 'int BN_usub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)\n{\n int max, min, dif;\n BN_ULONG t1, t2, borrow, *rp;\n const BN_ULONG *ap, *bp;\n bn_check_top(a);\n bn_check_top(b);\n max = a->top;\n min = b->top;\n dif = max - min;\n if (dif < 0) {\n BNerr(BN_F_BN_USUB, BN_R_ARG2_LT_ARG3);\n return 0;\n }\n if (bn_wexpand(r, max) == NULL)\n return 0;\n ap = a->d;\n bp = b->d;\n rp = r->d;\n borrow = bn_sub_words(rp, ap, bp, min);\n ap += min;\n rp += min;\n while (dif) {\n dif--;\n t1 = *(ap++);\n t2 = (t1 - borrow) & BN_MASK2;\n *(rp++) = t2;\n borrow &= (t1 == 0);\n }\n while (max && *--rp == 0)\n max--;\n r->top = max;\n r->neg = 0;\n bn_pollute(r);\n return 1;\n}', 'int BN_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)\n{\n int ret, r_neg, cmp_res;\n bn_check_top(a);\n bn_check_top(b);\n if (a->neg == b->neg) {\n r_neg = a->neg;\n ret = BN_uadd(r, a, b);\n } else {\n cmp_res = BN_ucmp(a, b);\n if (cmp_res > 0) {\n r_neg = a->neg;\n ret = BN_usub(r, a, b);\n } else if (cmp_res < 0) {\n r_neg = b->neg;\n ret = BN_usub(r, b, a);\n } else {\n r_neg = 0;\n BN_zero(r);\n ret = 1;\n }\n }\n r->neg = r_neg;\n bn_check_top(r);\n return ret;\n}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n return (words <= a->dmax) ? a : bn_expand2(a, words);\n}', 'BIGNUM *bn_expand2(BIGNUM *b, int words)\n{\n bn_check_top(b);\n if (words > b->dmax) {\n BN_ULONG *a = bn_expand_internal(b, words);\n if (!a)\n return NULL;\n if (b->d) {\n OPENSSL_cleanse(b->d, b->dmax * sizeof(b->d[0]));\n bn_free_d(b);\n }\n b->d = a;\n b->dmax = words;\n }\n bn_check_top(b);\n return b;\n}', 'static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)\n{\n BN_ULONG *a = NULL;\n bn_check_top(b);\n if (words > (INT_MAX / (4 * BN_BITS2))) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);\n return NULL;\n }\n if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);\n return NULL;\n }\n if (BN_get_flags(b, BN_FLG_SECURE))\n a = OPENSSL_secure_zalloc(words * sizeof(*a));\n else\n a = OPENSSL_zalloc(words * sizeof(*a));\n if (a == NULL) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);\n return NULL;\n }\n assert(b->top <= words);\n if (b->top > 0)\n memcpy(a, b->d, sizeof(*a) * b->top);\n return a;\n}', 'void *CRYPTO_zalloc(size_t num, const char *file, int line)\n{\n void *ret = CRYPTO_malloc(num, file, line);\n FAILTEST();\n if (ret != NULL)\n memset(ret, 0, num);\n return ret;\n}', 'void *CRYPTO_malloc(size_t num, const char *file, int line)\n{\n void *ret = NULL;\n INCREMENT(malloc_count);\n if (malloc_impl != NULL && malloc_impl != CRYPTO_malloc)\n return malloc_impl(num, file, line);\n if (num == 0)\n return NULL;\n FAILTEST();\n if (allow_customize) {\n allow_customize = 0;\n }\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n if (call_malloc_debug) {\n CRYPTO_mem_debug_malloc(NULL, num, 0, file, line);\n ret = malloc(num);\n CRYPTO_mem_debug_malloc(ret, num, 1, file, line);\n } else {\n ret = malloc(num);\n }\n#else\n (void)(file); (void)(line);\n ret = malloc(num);\n#endif\n return ret;\n}'] |
35,155 | 0 | https://github.com/openssl/openssl/blob/95dc05bc6d0dfe0f3f3681f5e27afbc3f7a35eea/crypto/bn/bn_lib.c/#L423 | BIGNUM *bn_expand2(BIGNUM *b, int words)
{
BN_ULONG *A,*B,*a;
int i,j;
bn_check_top(b);
if (words > b->max)
{
bn_check_top(b);
if (BN_get_flags(b,BN_FLG_STATIC_DATA))
{
BNerr(BN_F_BN_EXPAND2,BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);
return(NULL);
}
a=A=(BN_ULONG *)Malloc(sizeof(BN_ULONG)*(words+1));
if (A == NULL)
{
BNerr(BN_F_BN_EXPAND2,ERR_R_MALLOC_FAILURE);
return(NULL);
}
memset(A,0x5c,sizeof(BN_ULONG)*(words+1));
#if 1
B=b->d;
if (B != NULL)
{
for (i=b->top&(~7); i>0; i-=8)
{
A[0]=B[0]; A[1]=B[1]; A[2]=B[2]; A[3]=B[3];
A[4]=B[4]; A[5]=B[5]; A[6]=B[6]; A[7]=B[7];
A+=8;
B+=8;
}
switch (b->top&7)
{
case 7:
A[6]=B[6];
case 6:
A[5]=B[5];
case 5:
A[4]=B[4];
case 4:
A[3]=B[3];
case 3:
A[2]=B[2];
case 2:
A[1]=B[1];
case 1:
A[0]=B[0];
case 0:
;
}
Free(b->d);
}
b->d=a;
b->max=words;
B= &(b->d[b->top]);
j=(b->max - b->top) & ~7;
for (i=0; i<j; i+=8)
{
B[0]=0; B[1]=0; B[2]=0; B[3]=0;
B[4]=0; B[5]=0; B[6]=0; B[7]=0;
B+=8;
}
j=(b->max - b->top) & 7;
for (i=0; i<j; i++)
{
B[0]=0;
B++;
}
#else
memcpy(a->d,b->d,sizeof(b->d[0])*b->top);
#endif
}
return(b);
} | ['int DH_check(DH *dh, int *ret)\n\t{\n\tint ok=0;\n\tBN_CTX *ctx=NULL;\n\tBN_ULONG l;\n\tBIGNUM *q=NULL;\n\t*ret=0;\n\tctx=BN_CTX_new();\n\tif (ctx == NULL) goto err;\n\tq=BN_new();\n\tif (q == NULL) goto err;\n\tif (BN_is_word(dh->g,DH_GENERATOR_2))\n\t\t{\n\t\tl=BN_mod_word(dh->p,24);\n\t\tif (l != 11) *ret|=DH_NOT_SUITABLE_GENERATOR;\n\t\t}\n\telse if (BN_is_word(dh->g,DH_GENERATOR_5))\n\t\t{\n\t\tl=BN_mod_word(dh->p,10);\n\t\tif ((l != 3) && (l != 7))\n\t\t\t*ret|=DH_NOT_SUITABLE_GENERATOR;\n\t\t}\n\telse\n\t\t*ret|=DH_UNABLE_TO_CHECK_GENERATOR;\n\tif (!BN_is_prime(dh->p,BN_prime_checks,NULL,ctx,NULL))\n\t\t*ret|=DH_CHECK_P_NOT_PRIME;\n\telse\n\t\t{\n\t\tif (!BN_rshift1(q,dh->p)) goto err;\n\t\tif (!BN_is_prime(q,BN_prime_checks,NULL,ctx,NULL))\n\t\t\t*ret|=DH_CHECK_P_NOT_STRONG_PRIME;\n\t\t}\n\tok=1;\nerr:\n\tif (ctx != NULL) BN_CTX_free(ctx);\n\tif (q != NULL) BN_free(q);\n\treturn(ok);\n\t}', 'BIGNUM *BN_new(void)\n\t{\n\tBIGNUM *ret;\n\tif ((ret=(BIGNUM *)Malloc(sizeof(BIGNUM))) == NULL)\n\t\t{\n\t\tBNerr(BN_F_BN_NEW,ERR_R_MALLOC_FAILURE);\n\t\treturn(NULL);\n\t\t}\n\tret->flags=BN_FLG_MALLOCED;\n\tret->top=0;\n\tret->neg=0;\n\tret->max=0;\n\tret->d=NULL;\n\treturn(ret);\n\t}', 'int BN_rshift1(BIGNUM *r, BIGNUM *a)\n\t{\n\tBN_ULONG *ap,*rp,t,c;\n\tint i;\n\tif (BN_is_zero(a))\n\t\t{\n\t\tBN_zero(r);\n\t\treturn(1);\n\t\t}\n\tif (a != r)\n\t\t{\n\t\tif (bn_wexpand(r,a->top) == NULL) return(0);\n\t\tr->top=a->top;\n\t\tr->neg=a->neg;\n\t\t}\n\tap=a->d;\n\trp=r->d;\n\tc=0;\n\tfor (i=a->top-1; i>=0; i--)\n\t\t{\n\t\tt=ap[i];\n\t\trp[i]=((t>>1)&BN_MASK2)|c;\n\t\tc=(t&1)?BN_TBIT:0;\n\t\t}\n\tbn_fix_top(r);\n\treturn(1);\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#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}', 'BIGNUM *bn_expand2(BIGNUM *b, int words)\n\t{\n\tBN_ULONG *A,*B,*a;\n\tint i,j;\n\tbn_check_top(b);\n\tif (words > b->max)\n\t\t{\n\t\tbn_check_top(b);\n\t\tif (BN_get_flags(b,BN_FLG_STATIC_DATA))\n\t\t\t{\n\t\t\tBNerr(BN_F_BN_EXPAND2,BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);\n\t\t\treturn(NULL);\n\t\t\t}\n\t\ta=A=(BN_ULONG *)Malloc(sizeof(BN_ULONG)*(words+1));\n\t\tif (A == NULL)\n\t\t\t{\n\t\t\tBNerr(BN_F_BN_EXPAND2,ERR_R_MALLOC_FAILURE);\n\t\t\treturn(NULL);\n\t\t\t}\nmemset(A,0x5c,sizeof(BN_ULONG)*(words+1));\n#if 1\n\t\tB=b->d;\n\t\tif (B != NULL)\n\t\t\t{\n\t\t\tfor (i=b->top&(~7); i>0; i-=8)\n\t\t\t\t{\n\t\t\t\tA[0]=B[0]; A[1]=B[1]; A[2]=B[2]; A[3]=B[3];\n\t\t\t\tA[4]=B[4]; A[5]=B[5]; A[6]=B[6]; A[7]=B[7];\n\t\t\t\tA+=8;\n\t\t\t\tB+=8;\n\t\t\t\t}\n\t\t\tswitch (b->top&7)\n\t\t\t\t{\n\t\t\tcase 7:\n\t\t\t\tA[6]=B[6];\n\t\t\tcase 6:\n\t\t\t\tA[5]=B[5];\n\t\t\tcase 5:\n\t\t\t\tA[4]=B[4];\n\t\t\tcase 4:\n\t\t\t\tA[3]=B[3];\n\t\t\tcase 3:\n\t\t\t\tA[2]=B[2];\n\t\t\tcase 2:\n\t\t\t\tA[1]=B[1];\n\t\t\tcase 1:\n\t\t\t\tA[0]=B[0];\n\t\t\tcase 0:\n\t\t\t\t;\n\t\t\t\t}\n\t\t\tFree(b->d);\n\t\t\t}\n\t\tb->d=a;\n\t\tb->max=words;\n\t\tB= &(b->d[b->top]);\n\t\tj=(b->max - b->top) & ~7;\n\t\tfor (i=0; i<j; i+=8)\n\t\t\t{\n\t\t\tB[0]=0; B[1]=0; B[2]=0; B[3]=0;\n\t\t\tB[4]=0; B[5]=0; B[6]=0; B[7]=0;\n\t\t\tB+=8;\n\t\t\t}\n\t\tj=(b->max - b->top) & 7;\n\t\tfor (i=0; i<j; i++)\n\t\t\t{\n\t\t\tB[0]=0;\n\t\t\tB++;\n\t\t\t}\n#else\n\t\t\tmemcpy(a->d,b->d,sizeof(b->d[0])*b->top);\n#endif\n\t\t}\n\treturn(b);\n\t}'] |
35,156 | 0 | https://github.com/openssl/openssl/blob/f9df0a7775f483c175cda5832360cccd1db6943a/crypto/lhash/lhash.c/#L122 | 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_handshake_secrets(void)\n{\n SSL_CTX *ctx = NULL;\n SSL *s = NULL;\n int ret = 0;\n size_t hashsize;\n unsigned char out_master_secret[EVP_MAX_MD_SIZE];\n size_t master_secret_length;\n ctx = SSL_CTX_new(TLS_method());\n if (!TEST_ptr(ctx))\n goto err;\n s = SSL_new(ctx);\n if (!TEST_ptr(s ))\n goto err;\n s->session = SSL_SESSION_new();\n if (!TEST_ptr(s->session))\n goto err;\n if (!TEST_true(tls13_generate_secret(s, ssl_handshake_md(s), NULL, NULL, 0,\n (unsigned char *)&s->early_secret))) {\n TEST_info("Early secret generation failed");\n goto err;\n }\n if (!TEST_mem_eq(s->early_secret, sizeof(early_secret),\n early_secret, sizeof(early_secret))) {\n TEST_info("Early secret does not match");\n goto err;\n }\n if (!TEST_true(tls13_generate_handshake_secret(s, ecdhe_secret,\n sizeof(ecdhe_secret)))) {\n TEST_info("Handshake secret generation failed");\n goto err;\n }\n if (!TEST_mem_eq(s->handshake_secret, sizeof(handshake_secret),\n handshake_secret, sizeof(handshake_secret)))\n goto err;\n hashsize = EVP_MD_size(ssl_handshake_md(s));\n if (!TEST_size_t_eq(sizeof(client_hts), hashsize))\n goto err;\n if (!TEST_size_t_eq(sizeof(client_hts_key), KEYLEN))\n goto err;\n if (!TEST_size_t_eq(sizeof(client_hts_iv), IVLEN))\n goto err;\n if (!TEST_true(test_secret(s, s->handshake_secret,\n (unsigned char *)client_hts_label,\n strlen(client_hts_label), client_hts,\n client_hts_key, client_hts_iv))) {\n TEST_info("Client handshake secret test failed");\n goto err;\n }\n if (!TEST_size_t_eq(sizeof(server_hts), hashsize))\n goto err;\n if (!TEST_size_t_eq(sizeof(server_hts_key), KEYLEN))\n goto err;\n if (!TEST_size_t_eq(sizeof(server_hts_iv), IVLEN))\n goto err;\n if (!TEST_true(test_secret(s, s->handshake_secret,\n (unsigned char *)server_hts_label,\n strlen(server_hts_label), server_hts,\n server_hts_key, server_hts_iv))) {\n TEST_info("Server handshake secret test failed");\n goto err;\n }\n full_hash = 1;\n if (!TEST_true(tls13_generate_master_secret(s, out_master_secret,\n s->handshake_secret, hashsize,\n &master_secret_length))) {\n TEST_info("Master secret generation failed");\n goto err;\n }\n if (!TEST_mem_eq(out_master_secret, master_secret_length,\n master_secret, sizeof(master_secret))) {\n TEST_info("Master secret does not match");\n goto err;\n }\n if (!TEST_size_t_eq(sizeof(client_ats), hashsize))\n goto err;\n if (!TEST_size_t_eq(sizeof(client_ats_key), KEYLEN))\n goto err;\n if (!TEST_size_t_eq(sizeof(client_ats_iv), IVLEN))\n goto err;\n if (!TEST_true(test_secret(s, out_master_secret,\n (unsigned char *)client_ats_label,\n strlen(client_ats_label), client_ats,\n client_ats_key, client_ats_iv))) {\n TEST_info("Client application data secret test failed");\n goto err;\n }\n if (!TEST_size_t_eq(sizeof(server_ats), hashsize))\n goto err;\n if (!TEST_size_t_eq(sizeof(server_ats_key), KEYLEN))\n goto err;\n if (!TEST_size_t_eq(sizeof(server_ats_iv), IVLEN))\n goto err;\n if (!TEST_true(test_secret(s, out_master_secret,\n (unsigned char *)server_ats_label,\n strlen(server_ats_label), server_ats,\n server_ats_key, server_ats_iv))) {\n TEST_info("Server application data secret test failed");\n goto err;\n }\n ret = 1;\n err:\n SSL_free(s);\n SSL_CTX_free(ctx);\n return ret;\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 goto err;\n if (RAND_get_rand_method() == RAND_OpenSSL()) {\n s->drbg = RAND_DRBG_new(NID_aes_128_ctr, RAND_DRBG_FLAG_CTR_USE_DF,\n RAND_DRBG_get0_global());\n if (s->drbg == NULL\n || RAND_DRBG_instantiate(s->drbg, NULL, 0) == 0) {\n CRYPTO_THREAD_lock_free(s->lock);\n goto err;\n }\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 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->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 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->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 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 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 RAND_DRBG_free(s->drbg);\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}'] |
35,157 | 0 | https://github.com/openssl/openssl/blob/c10d1bc81cb047cbd53f8cc430632b6a4a70252d/test/evp_test.c/#L1640 | static int encode_test_init(struct evp_test *t, const char *encoding)
{
struct encode_data *edata = OPENSSL_zalloc(sizeof(*edata));
if (strcmp(encoding, "canonical") == 0) {
edata->encoding = BASE64_CANONICAL_ENCODING;
} else if (strcmp(encoding, "valid") == 0) {
edata->encoding = BASE64_VALID_ENCODING;
} else if (strcmp(encoding, "invalid") == 0) {
edata->encoding = BASE64_INVALID_ENCODING;
t->expected_err = OPENSSL_strdup("DECODE_ERROR");
if (t->expected_err == NULL)
return 0;
} else {
fprintf(stderr, "Bad encoding: %s. Should be one of "
"{canonical, valid, invalid}\n", encoding);
return 0;
}
t->data = edata;
return 1;
} | ['static int encode_test_init(struct evp_test *t, const char *encoding)\n{\n struct encode_data *edata = OPENSSL_zalloc(sizeof(*edata));\n if (strcmp(encoding, "canonical") == 0) {\n edata->encoding = BASE64_CANONICAL_ENCODING;\n } else if (strcmp(encoding, "valid") == 0) {\n edata->encoding = BASE64_VALID_ENCODING;\n } else if (strcmp(encoding, "invalid") == 0) {\n edata->encoding = BASE64_INVALID_ENCODING;\n t->expected_err = OPENSSL_strdup("DECODE_ERROR");\n if (t->expected_err == NULL)\n return 0;\n } else {\n fprintf(stderr, "Bad encoding: %s. Should be one of "\n "{canonical, valid, invalid}\\n", encoding);\n return 0;\n }\n t->data = edata;\n return 1;\n}', 'void *CRYPTO_zalloc(size_t num, const char *file, int line)\n{\n void *ret = CRYPTO_malloc(num, file, line);\n if (ret != NULL)\n memset(ret, 0, num);\n return ret;\n}', 'void *CRYPTO_malloc(size_t num, const char *file, int line)\n{\n void *ret = NULL;\n if (num <= 0)\n return NULL;\n allow_customize = 0;\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n if (call_malloc_debug) {\n CRYPTO_mem_debug_malloc(NULL, num, 0, file, line);\n ret = malloc(num);\n CRYPTO_mem_debug_malloc(ret, num, 1, file, line);\n } else {\n ret = malloc(num);\n }\n#else\n (void)file;\n (void)line;\n ret = malloc(num);\n#endif\n#ifndef OPENSSL_CPUID_OBJ\n if (ret && (num > 2048)) {\n extern unsigned char cleanse_ctr;\n ((unsigned char *)ret)[0] = cleanse_ctr;\n }\n#endif\n return ret;\n}', 'char *CRYPTO_strdup(const char *str, const char* file, int line)\n{\n char *ret;\n if (str == NULL)\n return NULL;\n ret = CRYPTO_malloc(strlen(str) + 1, file, line);\n if (ret != NULL)\n strcpy(ret, str);\n return ret;\n}'] |
35,158 | 0 | https://github.com/libav/libav/blob/1f09ab5e6665d0cae34fe4b378f16268e712e748/libavformat/oggparsevorbis.c/#L187 | static int
vorbis_header (AVFormatContext * s, int idx)
{
struct ogg *ogg = s->priv_data;
struct ogg_stream *os = ogg->streams + idx;
AVStream *st = s->streams[idx];
struct oggvorbis_private *priv;
if (os->seq > 2)
return 0;
if (os->seq == 0) {
os->private = av_mallocz(sizeof(struct oggvorbis_private));
if (!os->private)
return 0;
}
if (os->psize < 1)
return -1;
priv = os->private;
priv->len[os->seq] = os->psize;
priv->packet[os->seq] = av_mallocz(os->psize);
memcpy(priv->packet[os->seq], os->buf + os->pstart, os->psize);
if (os->buf[os->pstart] == 1) {
const uint8_t *p = os->buf + os->pstart + 7;
unsigned blocksize, bs0, bs1;
if (os->psize != 30)
return -1;
if (bytestream_get_le32(&p) != 0)
return -1;
st->codec->channels = bytestream_get_byte(&p);
st->codec->sample_rate = bytestream_get_le32(&p);
p += 4;
st->codec->bit_rate = bytestream_get_le32(&p);
p += 4;
blocksize = bytestream_get_byte(&p);
bs0 = blocksize & 15;
bs1 = blocksize >> 4;
if (bs0 > bs1)
return -1;
if (bs0 < 6 || bs1 > 13)
return -1;
if (bytestream_get_byte(&p) != 1)
return -1;
st->codec->codec_type = CODEC_TYPE_AUDIO;
st->codec->codec_id = CODEC_ID_VORBIS;
st->time_base.num = 1;
st->time_base.den = st->codec->sample_rate;
} else if (os->buf[os->pstart] == 3) {
if (os->psize > 8)
vorbis_comment (s, os->buf + os->pstart + 7, os->psize - 8);
} else {
st->codec->extradata_size =
fixup_vorbis_headers(s, priv, &st->codec->extradata);
}
return os->seq < 3;
} | ['static int\nvorbis_header (AVFormatContext * s, int idx)\n{\n struct ogg *ogg = s->priv_data;\n struct ogg_stream *os = ogg->streams + idx;\n AVStream *st = s->streams[idx];\n struct oggvorbis_private *priv;\n if (os->seq > 2)\n return 0;\n if (os->seq == 0) {\n os->private = av_mallocz(sizeof(struct oggvorbis_private));\n if (!os->private)\n return 0;\n }\n if (os->psize < 1)\n return -1;\n priv = os->private;\n priv->len[os->seq] = os->psize;\n priv->packet[os->seq] = av_mallocz(os->psize);\n memcpy(priv->packet[os->seq], os->buf + os->pstart, os->psize);\n if (os->buf[os->pstart] == 1) {\n const uint8_t *p = os->buf + os->pstart + 7;\n unsigned blocksize, bs0, bs1;\n if (os->psize != 30)\n return -1;\n if (bytestream_get_le32(&p) != 0)\n return -1;\n st->codec->channels = bytestream_get_byte(&p);\n st->codec->sample_rate = bytestream_get_le32(&p);\n p += 4;\n st->codec->bit_rate = bytestream_get_le32(&p);\n p += 4;\n blocksize = bytestream_get_byte(&p);\n bs0 = blocksize & 15;\n bs1 = blocksize >> 4;\n if (bs0 > bs1)\n return -1;\n if (bs0 < 6 || bs1 > 13)\n return -1;\n if (bytestream_get_byte(&p) != 1)\n return -1;\n st->codec->codec_type = CODEC_TYPE_AUDIO;\n st->codec->codec_id = CODEC_ID_VORBIS;\n st->time_base.num = 1;\n st->time_base.den = st->codec->sample_rate;\n } else if (os->buf[os->pstart] == 3) {\n if (os->psize > 8)\n vorbis_comment (s, os->buf + os->pstart + 7, os->psize - 8);\n } else {\n st->codec->extradata_size =\n fixup_vorbis_headers(s, priv, &st->codec->extradata);\n }\n return os->seq < 3;\n}', 'void *av_mallocz(unsigned int size)\n{\n void *ptr = av_malloc(size);\n if (ptr)\n memset(ptr, 0, size);\n return ptr;\n}', 'void *av_malloc(unsigned int size)\n{\n void *ptr = 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}'] |
35,159 | 0 | https://github.com/libav/libav/blob/31c54711cc3f1484af101d629bbb805820d37ad1/libavcodec/ffv1.c/#L1389 | static av_always_inline void decode_line(FFV1Context *s, int w,
int16_t *sample[2],
int plane_index, int bits)
{
PlaneContext *const p = &s->plane[plane_index];
RangeCoder *const c = &s->c;
int x;
int run_count = 0;
int run_mode = 0;
int run_index = s->run_index;
for (x = 0; x < w; x++) {
int diff, context, sign;
context = get_context(p, sample[1] + x, sample[0] + x, sample[1] + x);
if (context < 0) {
context = -context;
sign = 1;
} else
sign = 0;
av_assert2(context < p->context_count);
if (s->ac) {
diff = get_symbol_inline(c, p->state[context], 1);
} else {
if (context == 0 && run_mode == 0)
run_mode = 1;
if (run_mode) {
if (run_count == 0 && run_mode == 1) {
if (get_bits1(&s->gb)) {
run_count = 1 << ff_log2_run[run_index];
if (x + run_count <= w)
run_index++;
} else {
if (ff_log2_run[run_index])
run_count = get_bits(&s->gb, ff_log2_run[run_index]);
else
run_count = 0;
if (run_index)
run_index--;
run_mode = 2;
}
}
run_count--;
if (run_count < 0) {
run_mode = 0;
run_count = 0;
diff = get_vlc_symbol(&s->gb, &p->vlc_state[context],
bits);
if (diff >= 0)
diff++;
} else
diff = 0;
} else
diff = get_vlc_symbol(&s->gb, &p->vlc_state[context], bits);
av_dlog(s->avctx, "count:%d index:%d, mode:%d, x:%d pos:%d\n",
run_count, run_index, run_mode, x, get_bits_count(&s->gb));
}
if (sign)
diff = -diff;
sample[1][x] = (predict(sample[1] + x, sample[0] + x) + diff) &
((1 << bits) - 1);
}
s->run_index = run_index;
} | ['static void decode_rgb_frame(FFV1Context *s, uint32_t *src,\n int w, int h, int stride)\n{\n int x, y, p;\n int16_t *sample[3][2];\n for (x = 0; x < 3; x++) {\n sample[x][0] = s->sample_buffer + x * 2 * (w + 6) + 3;\n sample[x][1] = s->sample_buffer + (x * 2 + 1) * (w + 6) + 3;\n }\n s->run_index = 0;\n memset(s->sample_buffer, 0, 6 * (w + 6) * sizeof(*s->sample_buffer));\n for (y = 0; y < h; y++) {\n for (p = 0; p < 3; p++) {\n int16_t *temp = sample[p][0];\n sample[p][0] = sample[p][1];\n sample[p][1] = temp;\n sample[p][1][-1] = sample[p][0][0];\n sample[p][0][w] = sample[p][0][w - 1];\n decode_line(s, w, sample[p], FFMIN(p, 1), 9);\n }\n for (x = 0; x < w; x++) {\n int g = sample[0][1][x];\n int b = sample[1][1][x];\n int r = sample[2][1][x];\n b -= 0x100;\n r -= 0x100;\n g -= (b + r) >> 2;\n b += g;\n r += g;\n src[x + stride * y] = b + (g << 8) + (r << 16) + (0xFF << 24);\n }\n }\n}', 'static av_always_inline void decode_line(FFV1Context *s, int w,\n int16_t *sample[2],\n int plane_index, int bits)\n{\n PlaneContext *const p = &s->plane[plane_index];\n RangeCoder *const c = &s->c;\n int x;\n int run_count = 0;\n int run_mode = 0;\n int run_index = s->run_index;\n for (x = 0; x < w; x++) {\n int diff, context, sign;\n context = get_context(p, sample[1] + x, sample[0] + x, sample[1] + x);\n if (context < 0) {\n context = -context;\n sign = 1;\n } else\n sign = 0;\n av_assert2(context < p->context_count);\n if (s->ac) {\n diff = get_symbol_inline(c, p->state[context], 1);\n } else {\n if (context == 0 && run_mode == 0)\n run_mode = 1;\n if (run_mode) {\n if (run_count == 0 && run_mode == 1) {\n if (get_bits1(&s->gb)) {\n run_count = 1 << ff_log2_run[run_index];\n if (x + run_count <= w)\n run_index++;\n } else {\n if (ff_log2_run[run_index])\n run_count = get_bits(&s->gb, ff_log2_run[run_index]);\n else\n run_count = 0;\n if (run_index)\n run_index--;\n run_mode = 2;\n }\n }\n run_count--;\n if (run_count < 0) {\n run_mode = 0;\n run_count = 0;\n diff = get_vlc_symbol(&s->gb, &p->vlc_state[context],\n bits);\n if (diff >= 0)\n diff++;\n } else\n diff = 0;\n } else\n diff = get_vlc_symbol(&s->gb, &p->vlc_state[context], bits);\n av_dlog(s->avctx, "count:%d index:%d, mode:%d, x:%d pos:%d\\n",\n run_count, run_index, run_mode, x, get_bits_count(&s->gb));\n }\n if (sign)\n diff = -diff;\n sample[1][x] = (predict(sample[1] + x, sample[0] + x) + diff) &\n ((1 << bits) - 1);\n }\n s->run_index = run_index;\n}'] |
35,160 | 0 | https://github.com/libav/libav/blob/cc20fbcd39c7b60602edae4f7deb092ecfd3c975/libavcodec/vp9dsp.c/#L1601 | static av_always_inline void loop_filter(uint8_t *dst, ptrdiff_t stride,
int E, int I, int H,
ptrdiff_t stridea, ptrdiff_t strideb,
int wd)
{
int i;
for (i = 0; i < 8; i++, dst += stridea) {
int p7, p6, p5, p4;
int p3 = dst[strideb * -4], p2 = dst[strideb * -3];
int p1 = dst[strideb * -2], p0 = dst[strideb * -1];
int q0 = dst[strideb * +0], q1 = dst[strideb * +1];
int q2 = dst[strideb * +2], q3 = dst[strideb * +3];
int q4, q5, q6, q7;
int fm = FFABS(p3 - p2) <= I && FFABS(p2 - p1) <= I &&
FFABS(p1 - p0) <= I && FFABS(q1 - q0) <= I &&
FFABS(q2 - q1) <= I && FFABS(q3 - q2) <= I &&
FFABS(p0 - q0) * 2 + (FFABS(p1 - q1) >> 1) <= E;
int flat8out, flat8in;
if (!fm)
continue;
if (wd >= 16) {
p7 = dst[strideb * -8];
p6 = dst[strideb * -7];
p5 = dst[strideb * -6];
p4 = dst[strideb * -5];
q4 = dst[strideb * +4];
q5 = dst[strideb * +5];
q6 = dst[strideb * +6];
q7 = dst[strideb * +7];
flat8out = FFABS(p7 - p0) <= 1 && FFABS(p6 - p0) <= 1 &&
FFABS(p5 - p0) <= 1 && FFABS(p4 - p0) <= 1 &&
FFABS(q4 - q0) <= 1 && FFABS(q5 - q0) <= 1 &&
FFABS(q6 - q0) <= 1 && FFABS(q7 - q0) <= 1;
}
if (wd >= 8)
flat8in = FFABS(p3 - p0) <= 1 && FFABS(p2 - p0) <= 1 &&
FFABS(p1 - p0) <= 1 && FFABS(q1 - q0) <= 1 &&
FFABS(q2 - q0) <= 1 && FFABS(q3 - q0) <= 1;
if (wd >= 16 && flat8out && flat8in) {
dst[strideb * -7] = (p7 + p7 + p7 + p7 + p7 + p7 + p7 + p6 * 2 +
p5 + p4 + p3 + p2 + p1 + p0 + q0 + 8) >> 4;
dst[strideb * -6] = (p7 + p7 + p7 + p7 + p7 + p7 + p6 + p5 * 2 +
p4 + p3 + p2 + p1 + p0 + q0 + q1 + 8) >> 4;
dst[strideb * -5] = (p7 + p7 + p7 + p7 + p7 + p6 + p5 + p4 * 2 +
p3 + p2 + p1 + p0 + q0 + q1 + q2 + 8) >> 4;
dst[strideb * -4] = (p7 + p7 + p7 + p7 + p6 + p5 + p4 + p3 * 2 +
p2 + p1 + p0 + q0 + q1 + q2 + q3 + 8) >> 4;
dst[strideb * -3] = (p7 + p7 + p7 + p6 + p5 + p4 + p3 + p2 * 2 +
p1 + p0 + q0 + q1 + q2 + q3 + q4 + 8) >> 4;
dst[strideb * -2] = (p7 + p7 + p6 + p5 + p4 + p3 + p2 + p1 * 2 +
p0 + q0 + q1 + q2 + q3 + q4 + q5 + 8) >> 4;
dst[strideb * -1] = (p7 + p6 + p5 + p4 + p3 + p2 + p1 + p0 * 2 +
q0 + q1 + q2 + q3 + q4 + q5 + q6 + 8) >> 4;
dst[strideb * +0] = (p6 + p5 + p4 + p3 + p2 + p1 + p0 + q0 * 2 +
q1 + q2 + q3 + q4 + q5 + q6 + q7 + 8) >> 4;
dst[strideb * +1] = (p5 + p4 + p3 + p2 + p1 + p0 + q0 + q1 * 2 +
q2 + q3 + q4 + q5 + q6 + q7 + q7 + 8) >> 4;
dst[strideb * +2] = (p4 + p3 + p2 + p1 + p0 + q0 + q1 + q2 * 2 +
q3 + q4 + q5 + q6 + q7 + q7 + q7 + 8) >> 4;
dst[strideb * +3] = (p3 + p2 + p1 + p0 + q0 + q1 + q2 + q3 * 2 +
q4 + q5 + q6 + q7 + q7 + q7 + q7 + 8) >> 4;
dst[strideb * +4] = (p2 + p1 + p0 + q0 + q1 + q2 + q3 + q4 * 2 +
q5 + q6 + q7 + q7 + q7 + q7 + q7 + 8) >> 4;
dst[strideb * +5] = (p1 + p0 + q0 + q1 + q2 + q3 + q4 + q5 * 2 +
q6 + q7 + q7 + q7 + q7 + q7 + q7 + 8) >> 4;
dst[strideb * +6] = (p0 + q0 + q1 + q2 + q3 + q4 + q5 + q6 * 2 +
q7 + q7 + q7 + q7 + q7 + q7 + q7 + 8) >> 4;
} else if (wd >= 8 && flat8in) {
dst[strideb * -3] = (p3 + p3 + p3 + 2 * p2 + p1 + p0 + q0 + 4) >> 3;
dst[strideb * -2] = (p3 + p3 + p2 + 2 * p1 + p0 + q0 + q1 + 4) >> 3;
dst[strideb * -1] = (p3 + p2 + p1 + 2 * p0 + q0 + q1 + q2 + 4) >> 3;
dst[strideb * +0] = (p2 + p1 + p0 + 2 * q0 + q1 + q2 + q3 + 4) >> 3;
dst[strideb * +1] = (p1 + p0 + q0 + 2 * q1 + q2 + q3 + q3 + 4) >> 3;
dst[strideb * +2] = (p0 + q0 + q1 + 2 * q2 + q3 + q3 + q3 + 4) >> 3;
} else {
int hev = FFABS(p1 - p0) > H || FFABS(q1 - q0) > H;
if (hev) {
int f = av_clip_int8(3 * (q0 - p0) + av_clip_int8(p1 - q1));
int f1 = FFMIN(f + 4, 127) >> 3;
int f2 = FFMIN(f + 3, 127) >> 3;
dst[strideb * -1] = av_clip_uint8(p0 + f2);
dst[strideb * +0] = av_clip_uint8(q0 - f1);
} else {
int f = av_clip_int8(3 * (q0 - p0));
int f1 = FFMIN(f + 4, 127) >> 3;
int f2 = FFMIN(f + 3, 127) >> 3;
dst[strideb * -1] = av_clip_uint8(p0 + f2);
dst[strideb * +0] = av_clip_uint8(q0 - f1);
f = (f1 + 1) >> 1;
dst[strideb * -2] = av_clip_uint8(p1 + f);
dst[strideb * +1] = av_clip_uint8(q1 - f);
}
}
}
} | ['static av_always_inline void loop_filter(uint8_t *dst, ptrdiff_t stride,\n int E, int I, int H,\n ptrdiff_t stridea, ptrdiff_t strideb,\n int wd)\n{\n int i;\n for (i = 0; i < 8; i++, dst += stridea) {\n int p7, p6, p5, p4;\n int p3 = dst[strideb * -4], p2 = dst[strideb * -3];\n int p1 = dst[strideb * -2], p0 = dst[strideb * -1];\n int q0 = dst[strideb * +0], q1 = dst[strideb * +1];\n int q2 = dst[strideb * +2], q3 = dst[strideb * +3];\n int q4, q5, q6, q7;\n int fm = FFABS(p3 - p2) <= I && FFABS(p2 - p1) <= I &&\n FFABS(p1 - p0) <= I && FFABS(q1 - q0) <= I &&\n FFABS(q2 - q1) <= I && FFABS(q3 - q2) <= I &&\n FFABS(p0 - q0) * 2 + (FFABS(p1 - q1) >> 1) <= E;\n int flat8out, flat8in;\n if (!fm)\n continue;\n if (wd >= 16) {\n p7 = dst[strideb * -8];\n p6 = dst[strideb * -7];\n p5 = dst[strideb * -6];\n p4 = dst[strideb * -5];\n q4 = dst[strideb * +4];\n q5 = dst[strideb * +5];\n q6 = dst[strideb * +6];\n q7 = dst[strideb * +7];\n flat8out = FFABS(p7 - p0) <= 1 && FFABS(p6 - p0) <= 1 &&\n FFABS(p5 - p0) <= 1 && FFABS(p4 - p0) <= 1 &&\n FFABS(q4 - q0) <= 1 && FFABS(q5 - q0) <= 1 &&\n FFABS(q6 - q0) <= 1 && FFABS(q7 - q0) <= 1;\n }\n if (wd >= 8)\n flat8in = FFABS(p3 - p0) <= 1 && FFABS(p2 - p0) <= 1 &&\n FFABS(p1 - p0) <= 1 && FFABS(q1 - q0) <= 1 &&\n FFABS(q2 - q0) <= 1 && FFABS(q3 - q0) <= 1;\n if (wd >= 16 && flat8out && flat8in) {\n dst[strideb * -7] = (p7 + p7 + p7 + p7 + p7 + p7 + p7 + p6 * 2 +\n p5 + p4 + p3 + p2 + p1 + p0 + q0 + 8) >> 4;\n dst[strideb * -6] = (p7 + p7 + p7 + p7 + p7 + p7 + p6 + p5 * 2 +\n p4 + p3 + p2 + p1 + p0 + q0 + q1 + 8) >> 4;\n dst[strideb * -5] = (p7 + p7 + p7 + p7 + p7 + p6 + p5 + p4 * 2 +\n p3 + p2 + p1 + p0 + q0 + q1 + q2 + 8) >> 4;\n dst[strideb * -4] = (p7 + p7 + p7 + p7 + p6 + p5 + p4 + p3 * 2 +\n p2 + p1 + p0 + q0 + q1 + q2 + q3 + 8) >> 4;\n dst[strideb * -3] = (p7 + p7 + p7 + p6 + p5 + p4 + p3 + p2 * 2 +\n p1 + p0 + q0 + q1 + q2 + q3 + q4 + 8) >> 4;\n dst[strideb * -2] = (p7 + p7 + p6 + p5 + p4 + p3 + p2 + p1 * 2 +\n p0 + q0 + q1 + q2 + q3 + q4 + q5 + 8) >> 4;\n dst[strideb * -1] = (p7 + p6 + p5 + p4 + p3 + p2 + p1 + p0 * 2 +\n q0 + q1 + q2 + q3 + q4 + q5 + q6 + 8) >> 4;\n dst[strideb * +0] = (p6 + p5 + p4 + p3 + p2 + p1 + p0 + q0 * 2 +\n q1 + q2 + q3 + q4 + q5 + q6 + q7 + 8) >> 4;\n dst[strideb * +1] = (p5 + p4 + p3 + p2 + p1 + p0 + q0 + q1 * 2 +\n q2 + q3 + q4 + q5 + q6 + q7 + q7 + 8) >> 4;\n dst[strideb * +2] = (p4 + p3 + p2 + p1 + p0 + q0 + q1 + q2 * 2 +\n q3 + q4 + q5 + q6 + q7 + q7 + q7 + 8) >> 4;\n dst[strideb * +3] = (p3 + p2 + p1 + p0 + q0 + q1 + q2 + q3 * 2 +\n q4 + q5 + q6 + q7 + q7 + q7 + q7 + 8) >> 4;\n dst[strideb * +4] = (p2 + p1 + p0 + q0 + q1 + q2 + q3 + q4 * 2 +\n q5 + q6 + q7 + q7 + q7 + q7 + q7 + 8) >> 4;\n dst[strideb * +5] = (p1 + p0 + q0 + q1 + q2 + q3 + q4 + q5 * 2 +\n q6 + q7 + q7 + q7 + q7 + q7 + q7 + 8) >> 4;\n dst[strideb * +6] = (p0 + q0 + q1 + q2 + q3 + q4 + q5 + q6 * 2 +\n q7 + q7 + q7 + q7 + q7 + q7 + q7 + 8) >> 4;\n } else if (wd >= 8 && flat8in) {\n dst[strideb * -3] = (p3 + p3 + p3 + 2 * p2 + p1 + p0 + q0 + 4) >> 3;\n dst[strideb * -2] = (p3 + p3 + p2 + 2 * p1 + p0 + q0 + q1 + 4) >> 3;\n dst[strideb * -1] = (p3 + p2 + p1 + 2 * p0 + q0 + q1 + q2 + 4) >> 3;\n dst[strideb * +0] = (p2 + p1 + p0 + 2 * q0 + q1 + q2 + q3 + 4) >> 3;\n dst[strideb * +1] = (p1 + p0 + q0 + 2 * q1 + q2 + q3 + q3 + 4) >> 3;\n dst[strideb * +2] = (p0 + q0 + q1 + 2 * q2 + q3 + q3 + q3 + 4) >> 3;\n } else {\n int hev = FFABS(p1 - p0) > H || FFABS(q1 - q0) > H;\n if (hev) {\n int f = av_clip_int8(3 * (q0 - p0) + av_clip_int8(p1 - q1));\n int f1 = FFMIN(f + 4, 127) >> 3;\n int f2 = FFMIN(f + 3, 127) >> 3;\n dst[strideb * -1] = av_clip_uint8(p0 + f2);\n dst[strideb * +0] = av_clip_uint8(q0 - f1);\n } else {\n int f = av_clip_int8(3 * (q0 - p0));\n int f1 = FFMIN(f + 4, 127) >> 3;\n int f2 = FFMIN(f + 3, 127) >> 3;\n dst[strideb * -1] = av_clip_uint8(p0 + f2);\n dst[strideb * +0] = av_clip_uint8(q0 - f1);\n f = (f1 + 1) >> 1;\n dst[strideb * -2] = av_clip_uint8(p1 + f);\n dst[strideb * +1] = av_clip_uint8(q1 - f);\n }\n }\n }\n}'] |
35,161 | 0 | https://github.com/openssl/openssl/blob/95dc05bc6d0dfe0f3f3681f5e27afbc3f7a35eea/crypto/des/des_enc.c/#L142 | 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 MAIN(int argc, char **argv)\n\t{\n\tunsigned char *buf=NULL,*buf2=NULL;\n\tint ret=1;\n#define ALGOR_NUM\t14\n#define SIZE_NUM\t5\n#define RSA_NUM\t\t4\n#define DSA_NUM\t\t3\n\tlong count,rsa_count;\n\tint i,j,k,rsa_num,rsa_num2;\n#ifndef NO_MD2\n\tunsigned char md2[MD2_DIGEST_LENGTH];\n#endif\n#ifndef NO_MDC2\n\tunsigned char mdc2[MDC2_DIGEST_LENGTH];\n#endif\n#ifndef NO_MD5\n\tunsigned char md5[MD5_DIGEST_LENGTH];\n\tunsigned char hmac[MD5_DIGEST_LENGTH];\n#endif\n#ifndef NO_SHA1\n\tunsigned char sha[SHA_DIGEST_LENGTH];\n#endif\n#ifndef NO_RMD160\n\tunsigned char rmd160[RIPEMD160_DIGEST_LENGTH];\n#endif\n#ifndef NO_RC4\n\tRC4_KEY rc4_ks;\n#endif\n#ifndef NO_RC5\n\tRC5_32_KEY rc5_ks;\n#endif\n#ifndef NO_RC2\n\tRC2_KEY rc2_ks;\n#endif\n#ifndef NO_IDEA\n\tIDEA_KEY_SCHEDULE idea_ks;\n#endif\n#ifndef NO_BLOWFISH\n\tBF_KEY bf_ks;\n#endif\n#ifndef NO_CAST\n\tCAST_KEY cast_ks;\n#endif\n\tstatic unsigned char key16[16]=\n\t\t{0x12,0x34,0x56,0x78,0x9a,0xbc,0xde,0xf0,\n\t\t 0x34,0x56,0x78,0x9a,0xbc,0xde,0xf0,0x12};\n\tunsigned char iv[8];\n#ifndef NO_DES\n\tstatic des_cblock key ={0x12,0x34,0x56,0x78,0x9a,0xbc,0xde,0xf0};\n\tstatic des_cblock key2={0x34,0x56,0x78,0x9a,0xbc,0xde,0xf0,0x12};\n\tstatic des_cblock key3={0x56,0x78,0x9a,0xbc,0xde,0xf0,0x12,0x34};\n\tdes_key_schedule sch,sch2,sch3;\n#endif\n#define\tD_MD2\t\t0\n#define\tD_MDC2\t\t1\n#define\tD_MD5\t\t2\n#define\tD_HMAC\t\t3\n#define\tD_SHA1\t\t4\n#define D_RMD160\t5\n#define\tD_RC4\t\t6\n#define\tD_CBC_DES\t7\n#define\tD_EDE3_DES\t8\n#define\tD_CBC_IDEA\t9\n#define\tD_CBC_RC2\t10\n#define\tD_CBC_RC5\t11\n#define\tD_CBC_BF\t12\n#define\tD_CBC_CAST\t13\n\tdouble d,results[ALGOR_NUM][SIZE_NUM];\n\tstatic int lengths[SIZE_NUM]={8,64,256,1024,8*1024};\n\tlong c[ALGOR_NUM][SIZE_NUM];\n\tstatic char *names[ALGOR_NUM]={\n\t\t"md2","mdc2","md5","hmac(md5)","sha1","rmd160","rc4",\n\t\t"des cbc","des ede3","idea cbc",\n\t\t"rc2 cbc","rc5-32/12 cbc","blowfish cbc","cast cbc"};\n#define\tR_DSA_512\t0\n#define\tR_DSA_1024\t1\n#define\tR_DSA_2048\t2\n#define\tR_RSA_512\t0\n#define\tR_RSA_1024\t1\n#define\tR_RSA_2048\t2\n#define\tR_RSA_4096\t3\n\tRSA *rsa_key[RSA_NUM];\n\tlong rsa_c[RSA_NUM][2];\n#ifndef NO_RSA\n\tdouble rsa_results[RSA_NUM][2];\n\tstatic unsigned int rsa_bits[RSA_NUM]={512,1024,2048,4096};\n\tstatic unsigned char *rsa_data[RSA_NUM]=\n\t\t{test512,test1024,test2048,test4096};\n\tstatic int rsa_data_length[RSA_NUM]={\n\t\tsizeof(test512),sizeof(test1024),\n\t\tsizeof(test2048),sizeof(test4096)};\n#endif\n#ifndef NO_DSA\n\tDSA *dsa_key[DSA_NUM];\n\tlong dsa_c[DSA_NUM][2];\n\tdouble dsa_results[DSA_NUM][2];\n\tstatic unsigned int dsa_bits[DSA_NUM]={512,1024,2048};\n#endif\n\tint rsa_doit[RSA_NUM];\n\tint dsa_doit[DSA_NUM];\n\tint doit[ALGOR_NUM];\n\tint pr_header=0;\n\tapps_startup();\n#ifdef NO_DSA\n\tmemset(dsa_key,0,sizeof(dsa_key));\n#endif\n\tif (bio_err == NULL)\n\t\tif ((bio_err=BIO_new(BIO_s_file())) != NULL)\n\t\t\tBIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT);\n#ifndef NO_RSA\n\tmemset(rsa_key,0,sizeof(rsa_key));\n\tfor (i=0; i<RSA_NUM; i++)\n\t\trsa_key[i]=NULL;\n#endif\n\tif ((buf=(unsigned char *)Malloc((int)BUFSIZE)) == NULL)\n\t\t{\n\t\tBIO_printf(bio_err,"out of memory\\n");\n\t\tgoto end;\n\t\t}\n\tif ((buf2=(unsigned char *)Malloc((int)BUFSIZE)) == NULL)\n\t\t{\n\t\tBIO_printf(bio_err,"out of memory\\n");\n\t\tgoto end;\n\t\t}\n\tmemset(c,0,sizeof(c));\n\tmemset(iv,0,sizeof(iv));\n\tfor (i=0; i<ALGOR_NUM; i++)\n\t\tdoit[i]=0;\n\tfor (i=0; i<RSA_NUM; i++)\n\t\trsa_doit[i]=0;\n\tfor (i=0; i<DSA_NUM; i++)\n\t\tdsa_doit[i]=0;\n\tj=0;\n\targc--;\n\targv++;\n\twhile (argc)\n\t\t{\n#ifndef NO_MD2\n\t\tif\t(strcmp(*argv,"md2") == 0) doit[D_MD2]=1;\n\t\telse\n#endif\n#ifndef NO_MDC2\n\t\t\tif (strcmp(*argv,"mdc2") == 0) doit[D_MDC2]=1;\n\t\telse\n#endif\n#ifndef NO_MD5\n\t\t\tif (strcmp(*argv,"md5") == 0) doit[D_MD5]=1;\n\t\telse\n#endif\n#ifndef NO_MD5\n\t\t\tif (strcmp(*argv,"hmac") == 0) doit[D_HMAC]=1;\n\t\telse\n#endif\n#ifndef NO_SHA1\n\t\t\tif (strcmp(*argv,"sha1") == 0) doit[D_SHA1]=1;\n\t\telse\n\t\t\tif (strcmp(*argv,"sha") == 0) doit[D_SHA1]=1;\n\t\telse\n#endif\n#ifndef NO_RMD160\n\t\t\tif (strcmp(*argv,"ripemd") == 0) doit[D_RMD160]=1;\n\t\telse\n\t\t\tif (strcmp(*argv,"rmd160") == 0) doit[D_RMD160]=1;\n\t\telse\n\t\t\tif (strcmp(*argv,"ripemd160") == 0) doit[D_RMD160]=1;\n\t\telse\n#endif\n#ifndef NO_RC4\n\t\t\tif (strcmp(*argv,"rc4") == 0) doit[D_RC4]=1;\n\t\telse\n#endif\n#ifndef NO_DEF\n\t\t\tif (strcmp(*argv,"des-cbc") == 0) doit[D_CBC_DES]=1;\n\t\telse\tif (strcmp(*argv,"des-ede3") == 0) doit[D_EDE3_DES]=1;\n\t\telse\n#endif\n#ifndef NO_RSA\n#ifdef RSAref\n\t\t\tif (strcmp(*argv,"rsaref") == 0)\n\t\t\t{\n\t\t\tRSA_set_default_method(RSA_PKCS1_RSAref());\n\t\t\tj--;\n\t\t\t}\n\t\telse\n#endif\n\t\t\tif (strcmp(*argv,"openssl") == 0)\n\t\t\t{\n\t\t\tRSA_set_default_method(RSA_PKCS1_SSLeay());\n\t\t\tj--;\n\t\t\t}\n\t\telse\n#endif\n\t\t if (strcmp(*argv,"dsa512") == 0) dsa_doit[R_DSA_512]=2;\n\t\telse if (strcmp(*argv,"dsa1024") == 0) dsa_doit[R_DSA_1024]=2;\n\t\telse if (strcmp(*argv,"dsa2048") == 0) dsa_doit[R_DSA_2048]=2;\n\t\telse if (strcmp(*argv,"rsa512") == 0) rsa_doit[R_RSA_512]=2;\n\t\telse if (strcmp(*argv,"rsa1024") == 0) rsa_doit[R_RSA_1024]=2;\n\t\telse if (strcmp(*argv,"rsa2048") == 0) rsa_doit[R_RSA_2048]=2;\n\t\telse if (strcmp(*argv,"rsa4096") == 0) rsa_doit[R_RSA_4096]=2;\n\t\telse\n#ifndef NO_RC2\n\t\t if (strcmp(*argv,"rc2-cbc") == 0) doit[D_CBC_RC2]=1;\n\t\telse if (strcmp(*argv,"rc2") == 0) doit[D_CBC_RC2]=1;\n\t\telse\n#endif\n#ifndef NO_RC5\n\t\t if (strcmp(*argv,"rc5-cbc") == 0) doit[D_CBC_RC5]=1;\n\t\telse if (strcmp(*argv,"rc5") == 0) doit[D_CBC_RC5]=1;\n\t\telse\n#endif\n#ifndef NO_IDEA\n\t\t if (strcmp(*argv,"idea-cbc") == 0) doit[D_CBC_IDEA]=1;\n\t\telse if (strcmp(*argv,"idea") == 0) doit[D_CBC_IDEA]=1;\n\t\telse\n#endif\n#ifndef NO_BLOWFISH\n\t\t if (strcmp(*argv,"bf-cbc") == 0) doit[D_CBC_BF]=1;\n\t\telse if (strcmp(*argv,"blowfish") == 0) doit[D_CBC_BF]=1;\n\t\telse if (strcmp(*argv,"bf") == 0) doit[D_CBC_BF]=1;\n\t\telse\n#endif\n#ifndef NO_CAST\n\t\t if (strcmp(*argv,"cast-cbc") == 0) doit[D_CBC_CAST]=1;\n\t\telse if (strcmp(*argv,"cast") == 0) doit[D_CBC_CAST]=1;\n\t\telse if (strcmp(*argv,"cast5") == 0) doit[D_CBC_CAST]=1;\n\t\telse\n#endif\n#ifndef NO_DES\n\t\t\tif (strcmp(*argv,"des") == 0)\n\t\t\t{\n\t\t\tdoit[D_CBC_DES]=1;\n\t\t\tdoit[D_EDE3_DES]=1;\n\t\t\t}\n\t\telse\n#endif\n#ifndef NO_RSA\n\t\t\tif (strcmp(*argv,"rsa") == 0)\n\t\t\t{\n\t\t\trsa_doit[R_RSA_512]=1;\n\t\t\trsa_doit[R_RSA_1024]=1;\n\t\t\trsa_doit[R_RSA_2048]=1;\n\t\t\trsa_doit[R_RSA_4096]=1;\n\t\t\t}\n\t\telse\n#endif\n#ifndef NO_DSA\n\t\t\tif (strcmp(*argv,"dsa") == 0)\n\t\t\t{\n\t\t\tdsa_doit[R_DSA_512]=1;\n\t\t\tdsa_doit[R_DSA_1024]=1;\n\t\t\t}\n\t\telse\n#endif\n\t\t\t{\n\t\t\tBIO_printf(bio_err,"bad value, pick one of\\n");\n\t\t\tBIO_printf(bio_err,"md2 mdc2\tmd5 hmac sha1 rmd160\\n");\n#ifndef NO_IDEA\n\t\t\tBIO_printf(bio_err,"idea-cbc ");\n#endif\n#ifndef NO_RC2\n\t\t\tBIO_printf(bio_err,"rc2-cbc ");\n#endif\n#ifndef NO_RC5\n\t\t\tBIO_printf(bio_err,"rc5-cbc ");\n#endif\n#ifndef NO_BLOWFISH\n\t\t\tBIO_printf(bio_err,"bf-cbc");\n#endif\n#if !defined(NO_IDEA) && !defined(NO_RC2) && !defined(NO_BLOWFISH) && !defined(NO_RC5)\n\t\t\tBIO_printf(bio_err,"\\n");\n#endif\n\t\t\tBIO_printf(bio_err,"des-cbc des-ede3 ");\n#ifndef NO_RC4\n\t\t\tBIO_printf(bio_err,"rc4");\n#endif\n#ifndef NO_RSA\n\t\t\tBIO_printf(bio_err,"\\nrsa512 rsa1024 rsa2048 rsa4096\\n");\n#endif\n#ifndef NO_DSA\n\t\t\tBIO_printf(bio_err,"\\ndsa512 dsa1024 dsa2048\\n");\n#endif\n\t\t\tBIO_printf(bio_err,"idea rc2 des rsa blowfish\\n");\n\t\t\tgoto end;\n\t\t\t}\n\t\targc--;\n\t\targv++;\n\t\tj++;\n\t\t}\n\tif (j == 0)\n\t\t{\n\t\tfor (i=0; i<ALGOR_NUM; i++)\n\t\t\tdoit[i]=1;\n\t\tfor (i=0; i<RSA_NUM; i++)\n\t\t\trsa_doit[i]=1;\n\t\tfor (i=0; i<DSA_NUM; i++)\n\t\t\tdsa_doit[i]=1;\n\t\t}\n\tfor (i=0; i<ALGOR_NUM; i++)\n\t\tif (doit[i]) pr_header++;\n#ifndef TIMES\n\tBIO_printf(bio_err,"To get the most accurate results, try to run this\\n");\n\tBIO_printf(bio_err,"program when this computer is idle.\\n");\n#endif\n#ifndef NO_RSA\n\tfor (i=0; i<RSA_NUM; i++)\n\t\t{\n\t\tunsigned char *p;\n\t\tp=rsa_data[i];\n\t\trsa_key[i]=d2i_RSAPrivateKey(NULL,&p,rsa_data_length[i]);\n\t\tif (rsa_key[i] == NULL)\n\t\t\t{\n\t\t\tBIO_printf(bio_err,"internal error loading RSA key number %d\\n",i);\n\t\t\tgoto end;\n\t\t\t}\n#if 0\n\t\telse\n\t\t\t{\n\t\t\tBIO_printf(bio_err,"Loaded RSA key, %d bit modulus and e= 0x",BN_num_bits(rsa_key[i]->n));\n\t\t\tBN_print(bio_err,rsa_key[i]->e);\n\t\t\tBIO_printf(bio_err,"\\n");\n\t\t\t}\n#endif\n\t\t}\n#endif\n#ifndef NO_DSA\n\tdsa_key[0]=get_dsa512();\n\tdsa_key[1]=get_dsa1024();\n\tdsa_key[2]=get_dsa2048();\n#endif\n#ifndef NO_DES\n\tdes_set_key(key,sch);\n\tdes_set_key(key2,sch2);\n\tdes_set_key(key3,sch3);\n#endif\n#ifndef NO_IDEA\n\tidea_set_encrypt_key(key16,&idea_ks);\n#endif\n#ifndef NO_RC4\n\tRC4_set_key(&rc4_ks,16,key16);\n#endif\n#ifndef NO_RC2\n\tRC2_set_key(&rc2_ks,16,key16,128);\n#endif\n#ifndef NO_RC5\n\tRC5_32_set_key(&rc5_ks,16,key16,12);\n#endif\n#ifndef NO_BLOWFISH\n\tBF_set_key(&bf_ks,16,key16);\n#endif\n#ifndef NO_CAST\n\tCAST_set_key(&cast_ks,16,key16);\n#endif\n\tmemset(rsa_c,0,sizeof(rsa_c));\n#ifndef SIGALRM\n\tBIO_printf(bio_err,"First we calculate the approximate speed ...\\n");\n\tcount=10;\n\tdo\t{\n\t\tlong i;\n\t\tcount*=2;\n\t\tTime_F(START);\n\t\tfor (i=count; i; i--)\n\t\t\tdes_ecb_encrypt(buf,buf, &(sch[0]),DES_ENCRYPT);\n\t\td=Time_F(STOP);\n\t\t} while (d <3);\n\tc[D_MD2][0]=count/10;\n\tc[D_MDC2][0]=count/10;\n\tc[D_MD5][0]=count;\n\tc[D_HMAC][0]=count;\n\tc[D_SHA1][0]=count;\n\tc[D_RMD160][0]=count;\n\tc[D_RC4][0]=count*5;\n\tc[D_CBC_DES][0]=count;\n\tc[D_EDE3_DES][0]=count/3;\n\tc[D_CBC_IDEA][0]=count;\n\tc[D_CBC_RC2][0]=count;\n\tc[D_CBC_RC5][0]=count;\n\tc[D_CBC_BF][0]=count;\n\tc[D_CBC_CAST][0]=count;\n\tfor (i=1; i<SIZE_NUM; i++)\n\t\t{\n\t\tc[D_MD2][i]=c[D_MD2][0]*4*lengths[0]/lengths[i];\n\t\tc[D_MDC2][i]=c[D_MDC2][0]*4*lengths[0]/lengths[i];\n\t\tc[D_MD5][i]=c[D_MD5][0]*4*lengths[0]/lengths[i];\n\t\tc[D_HMAC][i]=c[D_HMAC][0]*4*lengths[0]/lengths[i];\n\t\tc[D_SHA1][i]=c[D_SHA1][0]*4*lengths[0]/lengths[i];\n\t\tc[D_RMD160][i]=c[D_RMD160][0]*4*lengths[0]/lengths[i];\n\t\t}\n\tfor (i=1; i<SIZE_NUM; i++)\n\t\t{\n\t\tlong l0,l1;\n\t\tl0=(long)lengths[i-1];\n\t\tl1=(long)lengths[i];\n\t\tc[D_RC4][i]=c[D_RC4][i-1]*l0/l1;\n\t\tc[D_CBC_DES][i]=c[D_CBC_DES][i-1]*l0/l1;\n\t\tc[D_EDE3_DES][i]=c[D_EDE3_DES][i-1]*l0/l1;\n\t\tc[D_CBC_IDEA][i]=c[D_CBC_IDEA][i-1]*l0/l1;\n\t\tc[D_CBC_RC2][i]=c[D_CBC_RC2][i-1]*l0/l1;\n\t\tc[D_CBC_RC5][i]=c[D_CBC_RC5][i-1]*l0/l1;\n\t\tc[D_CBC_BF][i]=c[D_CBC_BF][i-1]*l0/l1;\n\t\tc[D_CBC_CAST][i]=c[D_CBC_CAST][i-1]*l0/l1;\n\t\t}\n\trsa_c[R_RSA_512][0]=count/2000;\n\trsa_c[R_RSA_512][1]=count/400;\n\tfor (i=1; i<RSA_NUM; i++)\n\t\t{\n\t\trsa_c[i][0]=rsa_c[i-1][0]/8;\n\t\trsa_c[i][1]=rsa_c[i-1][1]/4;\n\t\tif ((rsa_doit[i] <= 1) && (rsa_c[i][0] == 0))\n\t\t\trsa_doit[i]=0;\n\t\telse\n\t\t\t{\n\t\t\tif (rsa_c[i][0] == 0)\n\t\t\t\t{\n\t\t\t\trsa_c[i][0]=1;\n\t\t\t\trsa_c[i][1]=20;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\tdsa_c[R_DSA_512][0]=count/1000;\n\tdsa_c[R_DSA_512][1]=count/1000/2;\n\tfor (i=1; i<DSA_NUM; i++)\n\t\t{\n\t\tdsa_c[i][0]=dsa_c[i-1][0]/4;\n\t\tdsa_c[i][1]=dsa_c[i-1][1]/4;\n\t\tif ((dsa_doit[i] <= 1) && (dsa_c[i][0] == 0))\n\t\t\tdsa_doit[i]=0;\n\t\telse\n\t\t\t{\n\t\t\tif (dsa_c[i] == 0)\n\t\t\t\t{\n\t\t\t\tdsa_c[i][0]=1;\n\t\t\t\tdsa_c[i][1]=1;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n#define COND(d)\t(count < (d))\n#define COUNT(d) (d)\n#else\n#define COND(c)\t(run)\n#define COUNT(d) (count)\n\tsignal(SIGALRM,sig_done);\n#endif\n#ifndef NO_MD2\n\tif (doit[D_MD2])\n\t\t{\n\t\tfor (j=0; j<SIZE_NUM; j++)\n\t\t\t{\n\t\t\tprint_message(names[D_MD2],c[D_MD2][j],lengths[j]);\n\t\t\tTime_F(START);\n\t\t\tfor (count=0,run=1; COND(c[D_MD2][j]); count++)\n\t\t\t\tMD2(buf,(unsigned long)lengths[j],&(md2[0]));\n\t\t\td=Time_F(STOP);\n\t\t\tBIO_printf(bio_err,"%ld %s\'s in %.2fs\\n",\n\t\t\t\tcount,names[D_MD2],d);\n\t\t\tresults[D_MD2][j]=((double)count)/d*lengths[j];\n\t\t\t}\n\t\t}\n#endif\n#ifndef NO_MDC2\n\tif (doit[D_MDC2])\n\t\t{\n\t\tfor (j=0; j<SIZE_NUM; j++)\n\t\t\t{\n\t\t\tprint_message(names[D_MDC2],c[D_MDC2][j],lengths[j]);\n\t\t\tTime_F(START);\n\t\t\tfor (count=0,run=1; COND(c[D_MDC2][j]); count++)\n\t\t\t\tMDC2(buf,(unsigned long)lengths[j],&(mdc2[0]));\n\t\t\td=Time_F(STOP);\n\t\t\tBIO_printf(bio_err,"%ld %s\'s in %.2fs\\n",\n\t\t\t\tcount,names[D_MDC2],d);\n\t\t\tresults[D_MDC2][j]=((double)count)/d*lengths[j];\n\t\t\t}\n\t\t}\n#endif\n#ifndef NO_MD5\n\tif (doit[D_MD5])\n\t\t{\n\t\tfor (j=0; j<SIZE_NUM; j++)\n\t\t\t{\n\t\t\tprint_message(names[D_MD5],c[D_MD5][j],lengths[j]);\n\t\t\tTime_F(START);\n\t\t\tfor (count=0,run=1; COND(c[D_MD5][j]); count++)\n\t\t\t\tMD5(&(buf[0]),(unsigned long)lengths[j],&(md5[0]));\n\t\t\td=Time_F(STOP);\n\t\t\tBIO_printf(bio_err,"%ld %s\'s in %.2fs\\n",\n\t\t\t\tcount,names[D_MD5],d);\n\t\t\tresults[D_MD5][j]=((double)count)/d*lengths[j];\n\t\t\t}\n\t\t}\n#endif\n#ifndef NO_MD5\n\tif (doit[D_HMAC])\n\t\t{\n\t\tHMAC_CTX hctx;\n\t\tHMAC_Init(&hctx,(unsigned char *)"This is a key...",\n\t\t\t16,EVP_md5());\n\t\tfor (j=0; j<SIZE_NUM; j++)\n\t\t\t{\n\t\t\tprint_message(names[D_HMAC],c[D_HMAC][j],lengths[j]);\n\t\t\tTime_F(START);\n\t\t\tfor (count=0,run=1; COND(c[D_HMAC][j]); count++)\n\t\t\t\t{\n\t\t\t\tHMAC_Init(&hctx,NULL,0,NULL);\n HMAC_Update(&hctx,buf,lengths[j]);\n HMAC_Final(&hctx,&(hmac[0]),NULL);\n\t\t\t\t}\n\t\t\td=Time_F(STOP);\n\t\t\tBIO_printf(bio_err,"%ld %s\'s in %.2fs\\n",\n\t\t\t\tcount,names[D_HMAC],d);\n\t\t\tresults[D_HMAC][j]=((double)count)/d*lengths[j];\n\t\t\t}\n\t\t}\n#endif\n#ifndef NO_SHA1\n\tif (doit[D_SHA1])\n\t\t{\n\t\tfor (j=0; j<SIZE_NUM; j++)\n\t\t\t{\n\t\t\tprint_message(names[D_SHA1],c[D_SHA1][j],lengths[j]);\n\t\t\tTime_F(START);\n\t\t\tfor (count=0,run=1; COND(c[D_SHA1][j]); count++)\n\t\t\t\tSHA1(buf,(unsigned long)lengths[j],&(sha[0]));\n\t\t\td=Time_F(STOP);\n\t\t\tBIO_printf(bio_err,"%ld %s\'s in %.2fs\\n",\n\t\t\t\tcount,names[D_SHA1],d);\n\t\t\tresults[D_SHA1][j]=((double)count)/d*lengths[j];\n\t\t\t}\n\t\t}\n#endif\n#ifndef NO_RMD160\n\tif (doit[D_RMD160])\n\t\t{\n\t\tfor (j=0; j<SIZE_NUM; j++)\n\t\t\t{\n\t\t\tprint_message(names[D_RMD160],c[D_RMD160][j],lengths[j]);\n\t\t\tTime_F(START);\n\t\t\tfor (count=0,run=1; COND(c[D_RMD160][j]); count++)\n\t\t\t\tRIPEMD160(buf,(unsigned long)lengths[j],&(rmd160[0]));\n\t\t\td=Time_F(STOP);\n\t\t\tBIO_printf(bio_err,"%ld %s\'s in %.2fs\\n",\n\t\t\t\tcount,names[D_RMD160],d);\n\t\t\tresults[D_RMD160][j]=((double)count)/d*lengths[j];\n\t\t\t}\n\t\t}\n#endif\n#ifndef NO_RC4\n\tif (doit[D_RC4])\n\t\t{\n\t\tfor (j=0; j<SIZE_NUM; j++)\n\t\t\t{\n\t\t\tprint_message(names[D_RC4],c[D_RC4][j],lengths[j]);\n\t\t\tTime_F(START);\n\t\t\tfor (count=0,run=1; COND(c[D_RC4][j]); count++)\n\t\t\t\tRC4(&rc4_ks,(unsigned int)lengths[j],\n\t\t\t\t\tbuf,buf);\n\t\t\td=Time_F(STOP);\n\t\t\tBIO_printf(bio_err,"%ld %s\'s in %.2fs\\n",\n\t\t\t\tcount,names[D_RC4],d);\n\t\t\tresults[D_RC4][j]=((double)count)/d*lengths[j];\n\t\t\t}\n\t\t}\n#endif\n#ifndef NO_DES\n\tif (doit[D_CBC_DES])\n\t\t{\n\t\tfor (j=0; j<SIZE_NUM; j++)\n\t\t\t{\n\t\t\tprint_message(names[D_CBC_DES],c[D_CBC_DES][j],lengths[j]);\n\t\t\tTime_F(START);\n\t\t\tfor (count=0,run=1; COND(c[D_CBC_DES][j]); count++)\n\t\t\t\tdes_ncbc_encrypt(buf,buf,lengths[j],sch,\n\t\t\t\t\t\t &(iv[0]),DES_ENCRYPT);\n\t\t\td=Time_F(STOP);\n\t\t\tBIO_printf(bio_err,"%ld %s\'s in %.2fs\\n",\n\t\t\t\tcount,names[D_CBC_DES],d);\n\t\t\tresults[D_CBC_DES][j]=((double)count)/d*lengths[j];\n\t\t\t}\n\t\t}\n\tif (doit[D_EDE3_DES])\n\t\t{\n\t\tfor (j=0; j<SIZE_NUM; j++)\n\t\t\t{\n\t\t\tprint_message(names[D_EDE3_DES],c[D_EDE3_DES][j],lengths[j]);\n\t\t\tTime_F(START);\n\t\t\tfor (count=0,run=1; COND(c[D_EDE3_DES][j]); count++)\n\t\t\t\tdes_ede3_cbc_encrypt(buf,buf,lengths[j],\n\t\t\t\t\t\t sch,sch2,sch3,\n\t\t\t\t\t\t &(iv[0]),DES_ENCRYPT);\n\t\t\td=Time_F(STOP);\n\t\t\tBIO_printf(bio_err,"%ld %s\'s in %.2fs\\n",\n\t\t\t\tcount,names[D_EDE3_DES],d);\n\t\t\tresults[D_EDE3_DES][j]=((double)count)/d*lengths[j];\n\t\t\t}\n\t\t}\n#endif\n#ifndef NO_IDEA\n\tif (doit[D_CBC_IDEA])\n\t\t{\n\t\tfor (j=0; j<SIZE_NUM; j++)\n\t\t\t{\n\t\t\tprint_message(names[D_CBC_IDEA],c[D_CBC_IDEA][j],lengths[j]);\n\t\t\tTime_F(START);\n\t\t\tfor (count=0,run=1; COND(c[D_CBC_IDEA][j]); count++)\n\t\t\t\tidea_cbc_encrypt(buf,buf,\n\t\t\t\t\t(unsigned long)lengths[j],&idea_ks,\n\t\t\t\t\t(unsigned char *)&(iv[0]),IDEA_ENCRYPT);\n\t\t\td=Time_F(STOP);\n\t\t\tBIO_printf(bio_err,"%ld %s\'s in %.2fs\\n",\n\t\t\t\tcount,names[D_CBC_IDEA],d);\n\t\t\tresults[D_CBC_IDEA][j]=((double)count)/d*lengths[j];\n\t\t\t}\n\t\t}\n#endif\n#ifndef NO_RC2\n\tif (doit[D_CBC_RC2])\n\t\t{\n\t\tfor (j=0; j<SIZE_NUM; j++)\n\t\t\t{\n\t\t\tprint_message(names[D_CBC_RC2],c[D_CBC_RC2][j],lengths[j]);\n\t\t\tTime_F(START);\n\t\t\tfor (count=0,run=1; COND(c[D_CBC_RC2][j]); count++)\n\t\t\t\tRC2_cbc_encrypt(buf,buf,\n\t\t\t\t\t(unsigned long)lengths[j],&rc2_ks,\n\t\t\t\t\t(unsigned char *)&(iv[0]),RC2_ENCRYPT);\n\t\t\td=Time_F(STOP);\n\t\t\tBIO_printf(bio_err,"%ld %s\'s in %.2fs\\n",\n\t\t\t\tcount,names[D_CBC_RC2],d);\n\t\t\tresults[D_CBC_RC2][j]=((double)count)/d*lengths[j];\n\t\t\t}\n\t\t}\n#endif\n#ifndef NO_RC5\n\tif (doit[D_CBC_RC5])\n\t\t{\n\t\tfor (j=0; j<SIZE_NUM; j++)\n\t\t\t{\n\t\t\tprint_message(names[D_CBC_RC5],c[D_CBC_RC5][j],lengths[j]);\n\t\t\tTime_F(START);\n\t\t\tfor (count=0,run=1; COND(c[D_CBC_RC5][j]); count++)\n\t\t\t\tRC5_32_cbc_encrypt(buf,buf,\n\t\t\t\t\t(unsigned long)lengths[j],&rc5_ks,\n\t\t\t\t\t(unsigned char *)&(iv[0]),RC5_ENCRYPT);\n\t\t\td=Time_F(STOP);\n\t\t\tBIO_printf(bio_err,"%ld %s\'s in %.2fs\\n",\n\t\t\t\tcount,names[D_CBC_RC5],d);\n\t\t\tresults[D_CBC_RC5][j]=((double)count)/d*lengths[j];\n\t\t\t}\n\t\t}\n#endif\n#ifndef NO_BLOWFISH\n\tif (doit[D_CBC_BF])\n\t\t{\n\t\tfor (j=0; j<SIZE_NUM; j++)\n\t\t\t{\n\t\t\tprint_message(names[D_CBC_BF],c[D_CBC_BF][j],lengths[j]);\n\t\t\tTime_F(START);\n\t\t\tfor (count=0,run=1; COND(c[D_CBC_BF][j]); count++)\n\t\t\t\tBF_cbc_encrypt(buf,buf,\n\t\t\t\t\t(unsigned long)lengths[j],&bf_ks,\n\t\t\t\t\t(unsigned char *)&(iv[0]),BF_ENCRYPT);\n\t\t\td=Time_F(STOP);\n\t\t\tBIO_printf(bio_err,"%ld %s\'s in %.2fs\\n",\n\t\t\t\tcount,names[D_CBC_BF],d);\n\t\t\tresults[D_CBC_BF][j]=((double)count)/d*lengths[j];\n\t\t\t}\n\t\t}\n#endif\n#ifndef NO_CAST\n\tif (doit[D_CBC_CAST])\n\t\t{\n\t\tfor (j=0; j<SIZE_NUM; j++)\n\t\t\t{\n\t\t\tprint_message(names[D_CBC_CAST],c[D_CBC_CAST][j],lengths[j]);\n\t\t\tTime_F(START);\n\t\t\tfor (count=0,run=1; COND(c[D_CBC_CAST][j]); count++)\n\t\t\t\tCAST_cbc_encrypt(buf,buf,\n\t\t\t\t\t(unsigned long)lengths[j],&cast_ks,\n\t\t\t\t\t(unsigned char *)&(iv[0]),CAST_ENCRYPT);\n\t\t\td=Time_F(STOP);\n\t\t\tBIO_printf(bio_err,"%ld %s\'s in %.2fs\\n",\n\t\t\t\tcount,names[D_CBC_CAST],d);\n\t\t\tresults[D_CBC_CAST][j]=((double)count)/d*lengths[j];\n\t\t\t}\n\t\t}\n#endif\n\tRAND_bytes(buf,30);\n#ifndef NO_RSA\n\tfor (j=0; j<RSA_NUM; j++)\n\t\t{\n\t\tif (!rsa_doit[j]) continue;\n\t\trsa_num=RSA_private_encrypt(30,buf,buf2,rsa_key[j],\n\t\t\tRSA_PKCS1_PADDING);\n\t\tpkey_print_message("private","rsa",rsa_c[j][0],rsa_bits[j],\n\t\t\tRSA_SECONDS);\n\t\tTime_F(START);\n\t\tfor (count=0,run=1; COND(rsa_c[j][0]); count++)\n\t\t\t{\n\t\t\trsa_num=RSA_private_encrypt(30,buf,buf2,rsa_key[j],\n\t\t\t\tRSA_PKCS1_PADDING);\n\t\t\tif (rsa_num <= 0)\n\t\t\t\t{\n\t\t\t\tBIO_printf(bio_err,"RSA private encrypt failure\\n");\n\t\t\t\tERR_print_errors(bio_err);\n\t\t\t\tcount=1;\n\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\td=Time_F(STOP);\n\t\tBIO_printf(bio_err,"%ld %d bit private RSA\'s in %.2fs\\n",\n\t\t\tcount,rsa_bits[j],d);\n\t\trsa_results[j][0]=d/(double)count;\n\t\trsa_count=count;\n#if 1\n\t\trsa_num2=RSA_public_decrypt(rsa_num,buf2,buf,rsa_key[j],\n\t\t\tRSA_PKCS1_PADDING);\n\t\tpkey_print_message("public","rsa",rsa_c[j][1],rsa_bits[j],\n\t\t\tRSA_SECONDS);\n\t\tTime_F(START);\n\t\tfor (count=0,run=1; COND(rsa_c[j][1]); count++)\n\t\t\t{\n\t\t\trsa_num2=RSA_public_decrypt(rsa_num,buf2,buf,rsa_key[j],\n\t\t\t\tRSA_PKCS1_PADDING);\n\t\t\tif (rsa_num2 <= 0)\n\t\t\t\t{\n\t\t\t\tBIO_printf(bio_err,"RSA public encrypt failure\\n");\n\t\t\t\tERR_print_errors(bio_err);\n\t\t\t\tcount=1;\n\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\td=Time_F(STOP);\n\t\tBIO_printf(bio_err,"%ld %d bit public RSA\'s in %.2fs\\n",\n\t\t\tcount,rsa_bits[j],d);\n\t\trsa_results[j][1]=d/(double)count;\n#endif\n\t\tif (rsa_count <= 1)\n\t\t\t{\n\t\t\tfor (j++; j<RSA_NUM; j++)\n\t\t\t\trsa_doit[j]=0;\n\t\t\t}\n\t\t}\n#endif\n\tRAND_bytes(buf,20);\n#ifndef NO_DSA\n\tfor (j=0; j<DSA_NUM; j++)\n\t\t{\n\t\tunsigned int kk;\n\t\tif (!dsa_doit[j]) continue;\n\t\tDSA_generate_key(dsa_key[j]);\n\t\trsa_num=DSA_sign(EVP_PKEY_DSA,buf,20,buf2,\n\t\t\t&kk,dsa_key[j]);\n\t\tpkey_print_message("sign","dsa",dsa_c[j][0],dsa_bits[j],\n\t\t\tDSA_SECONDS);\n\t\tTime_F(START);\n\t\tfor (count=0,run=1; COND(dsa_c[j][0]); count++)\n\t\t\t{\n\t\t\trsa_num=DSA_sign(EVP_PKEY_DSA,buf,20,buf2,\n\t\t\t\t&kk,dsa_key[j]);\n\t\t\tif (rsa_num <= 0)\n\t\t\t\t{\n\t\t\t\tBIO_printf(bio_err,"DSA sign failure\\n");\n\t\t\t\tERR_print_errors(bio_err);\n\t\t\t\tcount=1;\n\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\td=Time_F(STOP);\n\t\tBIO_printf(bio_err,"%ld %d bit DSA signs in %.2fs\\n",\n\t\t\tcount,dsa_bits[j],d);\n\t\tdsa_results[j][0]=d/(double)count;\n\t\trsa_count=count;\n\t\trsa_num2=DSA_verify(EVP_PKEY_DSA,buf,20,buf2,\n\t\t\tkk,dsa_key[j]);\n\t\tpkey_print_message("verify","dsa",dsa_c[j][1],dsa_bits[j],\n\t\t\tDSA_SECONDS);\n\t\tTime_F(START);\n\t\tfor (count=0,run=1; COND(dsa_c[j][1]); count++)\n\t\t\t{\n\t\t\trsa_num2=DSA_verify(EVP_PKEY_DSA,buf,20,buf2,\n\t\t\t\tkk,dsa_key[j]);\n\t\t\tif (rsa_num2 <= 0)\n\t\t\t\t{\n\t\t\t\tBIO_printf(bio_err,"DSA verify failure\\n");\n\t\t\t\tERR_print_errors(bio_err);\n\t\t\t\tcount=1;\n\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\td=Time_F(STOP);\n\t\tBIO_printf(bio_err,"%ld %d bit DSA verify in %.2fs\\n",\n\t\t\tcount,dsa_bits[j],d);\n\t\tdsa_results[j][1]=d/(double)count;\n\t\tif (rsa_count <= 1)\n\t\t\t{\n\t\t\tfor (j++; j<DSA_NUM; j++)\n\t\t\t\tdsa_doit[j]=0;\n\t\t\t}\n\t\t}\n#endif\n\tfprintf(stdout,"%s\\n",SSLeay_version(SSLEAY_VERSION));\n fprintf(stdout,"%s\\n",SSLeay_version(SSLEAY_BUILT_ON));\n\tprintf("options:");\n\tprintf("%s ",BN_options());\n#ifndef NO_MD2\n\tprintf("%s ",MD2_options());\n#endif\n#ifndef NO_RC4\n\tprintf("%s ",RC4_options());\n#endif\n#ifndef NO_DES\n\tprintf("%s ",des_options());\n#endif\n#ifndef NO_IDEA\n\tprintf("%s ",idea_options());\n#endif\n#ifndef NO_BLOWFISH\n\tprintf("%s ",BF_options());\n#endif\n\tfprintf(stdout,"\\n%s\\n",SSLeay_version(SSLEAY_CFLAGS));\n\tif (pr_header)\n\t\t{\n\t\tfprintf(stdout,"The \'numbers\' are in 1000s of bytes per second processed.\\n");\n\t\tfprintf(stdout,"type ");\n\t\tfor (j=0; j<SIZE_NUM; j++)\n\t\t\tfprintf(stdout,"%7d bytes",lengths[j]);\n\t\tfprintf(stdout,"\\n");\n\t\t}\n\tfor (k=0; k<ALGOR_NUM; k++)\n\t\t{\n\t\tif (!doit[k]) continue;\n\t\tfprintf(stdout,"%-13s",names[k]);\n\t\tfor (j=0; j<SIZE_NUM; j++)\n\t\t\t{\n\t\t\tif (results[k][j] > 10000)\n\t\t\t\tfprintf(stdout," %11.2fk",results[k][j]/1e3);\n\t\t\telse\n\t\t\t\tfprintf(stdout," %11.2f ",results[k][j]);\n\t\t\t}\n\t\tfprintf(stdout,"\\n");\n\t\t}\n#ifndef NO_RSA\n\tj=1;\n\tfor (k=0; k<RSA_NUM; k++)\n\t\t{\n\t\tif (!rsa_doit[k]) continue;\n\t\tif (j)\n\t\t\t{\n\t\t\tprintf("%18ssign verify sign/s verify/s\\n"," ");\n\t\t\tj=0;\n\t\t\t}\n\t\tfprintf(stdout,"rsa %4d bits %8.4fs %8.4fs %8.1f %8.1f",\n\t\t\trsa_bits[k],rsa_results[k][0],rsa_results[k][1],\n\t\t\t1.0/rsa_results[k][0],1.0/rsa_results[k][1]);\n\t\tfprintf(stdout,"\\n");\n\t\t}\n#endif\n#ifndef NO_DSA\n\tj=1;\n\tfor (k=0; k<DSA_NUM; k++)\n\t\t{\n\t\tif (!dsa_doit[k]) continue;\n\t\tif (j)\t{\n\t\t\tprintf("%18ssign verify sign/s verify/s\\n"," ");\n\t\t\tj=0;\n\t\t\t}\n\t\tfprintf(stdout,"dsa %4d bits %8.4fs %8.4fs %8.1f %8.1f",\n\t\t\tdsa_bits[k],dsa_results[k][0],dsa_results[k][1],\n\t\t\t1.0/dsa_results[k][0],1.0/dsa_results[k][1]);\n\t\tfprintf(stdout,"\\n");\n\t\t}\n#endif\n\tret=0;\nend:\n\tif (buf != NULL) Free(buf);\n\tif (buf2 != NULL) Free(buf2);\n#ifndef NO_RSA\n\tfor (i=0; i<RSA_NUM; i++)\n\t\tif (rsa_key[i] != NULL)\n\t\t\tRSA_free(rsa_key[i]);\n#endif\n#ifndef NO_DSA\n\tfor (i=0; i<DSA_NUM; i++)\n\t\tif (dsa_key[i] != NULL)\n\t\t\tDSA_free(dsa_key[i]);\n#endif\n\tEXIT(ret);\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}'] |
35,162 | 0 | https://github.com/openssl/openssl/blob/9f519addc09b2005fa8c6cde36e3267de02577bb/apps/speed.c/#L1924 | int speed_main(int argc, char **argv)
{
loopargs_t *loopargs = NULL;
int loopargs_len = 0;
char *prog;
const EVP_CIPHER *evp_cipher = NULL;
double d = 0.0;
OPTION_CHOICE o;
int multiblock = 0, doit[ALGOR_NUM], pr_header = 0;
int dsa_doit[DSA_NUM], rsa_doit[RSA_NUM];
int ret = 1, i, k, misalign = 0;
long c[ALGOR_NUM][SIZE_NUM], count = 0, save_count = 0;
#ifndef NO_FORK
int multi = 0;
#endif
int async_jobs = 0;
#if !defined(OPENSSL_NO_RSA) || !defined(OPENSSL_NO_DSA)
long rsa_count = 1;
#endif
#ifndef OPENSSL_NO_RC5
RC5_32_KEY rc5_ks;
#endif
#ifndef OPENSSL_NO_RC2
RC2_KEY rc2_ks;
#endif
#ifndef OPENSSL_NO_IDEA
IDEA_KEY_SCHEDULE idea_ks;
#endif
#ifndef OPENSSL_NO_SEED
SEED_KEY_SCHEDULE seed_ks;
#endif
#ifndef OPENSSL_NO_BF
BF_KEY bf_ks;
#endif
#ifndef OPENSSL_NO_CAST
CAST_KEY cast_ks;
#endif
static const unsigned char key16[16] = {
0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,
0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12
};
#ifndef OPENSSL_NO_AES
static const unsigned char key24[24] = {
0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,
0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12,
0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34
};
static const unsigned char key32[32] = {
0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,
0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12,
0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34,
0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56
};
#endif
#ifndef OPENSSL_NO_CAMELLIA
static const unsigned char ckey24[24] = {
0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,
0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12,
0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34
};
static const unsigned char ckey32[32] = {
0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,
0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12,
0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34,
0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56
};
CAMELLIA_KEY camellia_ks1, camellia_ks2, camellia_ks3;
#endif
#ifndef OPENSSL_NO_DES
static DES_cblock key = {
0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0
};
static DES_cblock key2 = {
0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12
};
static DES_cblock key3 = {
0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34
};
#endif
#ifndef OPENSSL_NO_RSA
static unsigned int rsa_bits[RSA_NUM] = {
512, 1024, 2048, 3072, 4096, 7680, 15360
};
static unsigned char *rsa_data[RSA_NUM] = {
test512, test1024, test2048, test3072, test4096, test7680, test15360
};
static int rsa_data_length[RSA_NUM] = {
sizeof(test512), sizeof(test1024),
sizeof(test2048), sizeof(test3072),
sizeof(test4096), sizeof(test7680),
sizeof(test15360)
};
#endif
#ifndef OPENSSL_NO_DSA
static unsigned int dsa_bits[DSA_NUM] = { 512, 1024, 2048 };
#endif
#ifndef OPENSSL_NO_EC
static unsigned int test_curves[EC_NUM] = {
NID_secp160r1, NID_X9_62_prime192v1, NID_secp224r1,
NID_X9_62_prime256v1, NID_secp384r1, NID_secp521r1,
NID_sect163k1, NID_sect233k1, NID_sect283k1,
NID_sect409k1, NID_sect571k1, NID_sect163r2,
NID_sect233r1, NID_sect283r1, NID_sect409r1,
NID_sect571r1,
NID_X25519
};
static const char *test_curves_names[EC_NUM] = {
"secp160r1", "nistp192", "nistp224",
"nistp256", "nistp384", "nistp521",
"nistk163", "nistk233", "nistk283",
"nistk409", "nistk571", "nistb163",
"nistb233", "nistb283", "nistb409",
"nistb571",
"X25519"
};
static int test_curves_bits[EC_NUM] = {
160, 192, 224,
256, 384, 521,
163, 233, 283,
409, 571, 163,
233, 283, 409,
571, 253
};
#endif
#ifndef OPENSSL_NO_EC
int ecdsa_doit[EC_NUM];
int secret_size_a, secret_size_b;
int ecdh_checks = 1;
int secret_idx = 0;
long ecdh_c[EC_NUM][2];
int ecdh_doit[EC_NUM];
#endif
memset(results, 0, sizeof(results));
memset(c, 0, sizeof(c));
memset(DES_iv, 0, sizeof(DES_iv));
memset(iv, 0, sizeof(iv));
for (i = 0; i < ALGOR_NUM; i++)
doit[i] = 0;
for (i = 0; i < RSA_NUM; i++)
rsa_doit[i] = 0;
for (i = 0; i < DSA_NUM; i++)
dsa_doit[i] = 0;
#ifndef OPENSSL_NO_EC
for (i = 0; i < EC_NUM; i++)
ecdsa_doit[i] = 0;
for (i = 0; i < EC_NUM; i++)
ecdh_doit[i] = 0;
#endif
misalign = 0;
prog = opt_init(argc, argv, speed_options);
while ((o = opt_next()) != OPT_EOF) {
switch (o) {
case OPT_EOF:
case OPT_ERR:
opterr:
BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
goto end;
case OPT_HELP:
opt_help(speed_options);
ret = 0;
goto end;
case OPT_ELAPSED:
usertime = 0;
break;
case OPT_EVP:
evp_cipher = EVP_get_cipherbyname(opt_arg());
if (evp_cipher == NULL)
evp_md = EVP_get_digestbyname(opt_arg());
if (evp_cipher == NULL && evp_md == NULL) {
BIO_printf(bio_err,
"%s: %s an unknown cipher or digest\n",
prog, opt_arg());
goto end;
}
doit[D_EVP] = 1;
break;
case OPT_DECRYPT:
decrypt = 1;
break;
case OPT_ENGINE:
engine_id = opt_arg();
break;
case OPT_MULTI:
#ifndef NO_FORK
multi = atoi(opt_arg());
#endif
break;
case OPT_ASYNCJOBS:
#ifndef OPENSSL_NO_ASYNC
async_jobs = atoi(opt_arg());
if (!ASYNC_is_capable()) {
BIO_printf(bio_err,
"%s: async_jobs specified but async not supported\n",
prog);
goto opterr;
}
#endif
break;
case OPT_MISALIGN:
if (!opt_int(opt_arg(), &misalign))
goto end;
if (misalign > MISALIGN) {
BIO_printf(bio_err,
"%s: Maximum offset is %d\n", prog, MISALIGN);
goto opterr;
}
break;
case OPT_MR:
mr = 1;
break;
case OPT_MB:
multiblock = 1;
break;
}
}
argc = opt_num_rest();
argv = opt_rest();
for ( ; *argv; argv++) {
if (found(*argv, doit_choices, &i)) {
doit[i] = 1;
continue;
}
#ifndef OPENSSL_NO_DES
if (strcmp(*argv, "des") == 0) {
doit[D_CBC_DES] = doit[D_EDE3_DES] = 1;
continue;
}
#endif
if (strcmp(*argv, "sha") == 0) {
doit[D_SHA1] = doit[D_SHA256] = doit[D_SHA512] = 1;
continue;
}
#ifndef OPENSSL_NO_RSA
# ifndef RSA_NULL
if (strcmp(*argv, "openssl") == 0) {
RSA_set_default_method(RSA_PKCS1_OpenSSL());
continue;
}
# endif
if (strcmp(*argv, "rsa") == 0) {
rsa_doit[R_RSA_512] = rsa_doit[R_RSA_1024] =
rsa_doit[R_RSA_2048] = rsa_doit[R_RSA_3072] =
rsa_doit[R_RSA_4096] = rsa_doit[R_RSA_7680] =
rsa_doit[R_RSA_15360] = 1;
continue;
}
if (found(*argv, rsa_choices, &i)) {
rsa_doit[i] = 1;
continue;
}
#endif
#ifndef OPENSSL_NO_DSA
if (strcmp(*argv, "dsa") == 0) {
dsa_doit[R_DSA_512] = dsa_doit[R_DSA_1024] =
dsa_doit[R_DSA_2048] = 1;
continue;
}
if (found(*argv, dsa_choices, &i)) {
dsa_doit[i] = 2;
continue;
}
#endif
#ifndef OPENSSL_NO_AES
if (strcmp(*argv, "aes") == 0) {
doit[D_CBC_128_AES] = doit[D_CBC_192_AES] =
doit[D_CBC_256_AES] = 1;
continue;
}
#endif
#ifndef OPENSSL_NO_CAMELLIA
if (strcmp(*argv, "camellia") == 0) {
doit[D_CBC_128_CML] = doit[D_CBC_192_CML] =
doit[D_CBC_256_CML] = 1;
continue;
}
#endif
#ifndef OPENSSL_NO_EC
if (strcmp(*argv, "ecdsa") == 0) {
for (i = 0; i < EC_NUM; i++)
ecdsa_doit[i] = 1;
continue;
}
if (found(*argv, ecdsa_choices, &i)) {
ecdsa_doit[i] = 2;
continue;
}
if (strcmp(*argv, "ecdh") == 0) {
for (i = 0; i < EC_NUM; i++)
ecdh_doit[i] = 1;
continue;
}
if (found(*argv, ecdh_choices, &i)) {
ecdh_doit[i] = 2;
continue;
}
#endif
BIO_printf(bio_err, "%s: Unknown algorithm %s\n", prog, *argv);
goto end;
}
if (async_jobs > 0) {
if (!ASYNC_init_thread(async_jobs, async_jobs)) {
BIO_printf(bio_err, "Error creating the ASYNC job pool\n");
goto end;
}
}
loopargs_len = (async_jobs == 0 ? 1 : async_jobs);
loopargs = app_malloc(loopargs_len * sizeof(loopargs_t), "array of loopargs");
memset(loopargs, 0, loopargs_len * sizeof(loopargs_t));
for (i = 0; i < loopargs_len; i++) {
if (async_jobs > 0) {
loopargs[i].wait_ctx = ASYNC_WAIT_CTX_new();
if (loopargs[i].wait_ctx == NULL) {
BIO_printf(bio_err, "Error creating the ASYNC_WAIT_CTX\n");
goto end;
}
}
loopargs[i].buf_malloc = app_malloc((int)BUFSIZE + MAX_MISALIGNMENT + 1, "input buffer");
loopargs[i].buf2_malloc = app_malloc((int)BUFSIZE + MAX_MISALIGNMENT + 1, "input buffer");
loopargs[i].buf = loopargs[i].buf_malloc + misalign;
loopargs[i].buf2 = loopargs[i].buf2_malloc + misalign;
loopargs[i].siglen = app_malloc(sizeof(unsigned int), "signature length");
#ifndef OPENSSL_NO_EC
loopargs[i].secret_a = app_malloc(MAX_ECDH_SIZE, "ECDH secret a");
loopargs[i].secret_b = app_malloc(MAX_ECDH_SIZE, "ECDH secret b");
#endif
}
#ifndef NO_FORK
if (multi && do_multi(multi))
goto show_res;
#endif
(void)setup_engine(engine_id, 0);
if ((argc == 0) && !doit[D_EVP]) {
for (i = 0; i < ALGOR_NUM; i++)
if (i != D_EVP)
doit[i] = 1;
for (i = 0; i < RSA_NUM; i++)
rsa_doit[i] = 1;
for (i = 0; i < DSA_NUM; i++)
dsa_doit[i] = 1;
#ifndef OPENSSL_NO_EC
for (i = 0; i < EC_NUM; i++)
ecdsa_doit[i] = 1;
for (i = 0; i < EC_NUM; i++)
ecdh_doit[i] = 1;
#endif
}
for (i = 0; i < ALGOR_NUM; i++)
if (doit[i])
pr_header++;
if (usertime == 0 && !mr)
BIO_printf(bio_err,
"You have chosen to measure elapsed time "
"instead of user CPU time.\n");
#ifndef OPENSSL_NO_RSA
for (i = 0; i < loopargs_len; i++) {
for (k = 0; k < RSA_NUM; k++) {
const unsigned char *p;
p = rsa_data[k];
loopargs[i].rsa_key[k] = d2i_RSAPrivateKey(NULL, &p, rsa_data_length[k]);
if (loopargs[i].rsa_key[k] == NULL) {
BIO_printf(bio_err, "internal error loading RSA key number %d\n",
k);
goto end;
}
}
}
#endif
#ifndef OPENSSL_NO_DSA
for (i = 0; i < loopargs_len; i++) {
loopargs[i].dsa_key[0] = get_dsa512();
loopargs[i].dsa_key[1] = get_dsa1024();
loopargs[i].dsa_key[2] = get_dsa2048();
}
#endif
#ifndef OPENSSL_NO_DES
DES_set_key_unchecked(&key, &sch);
DES_set_key_unchecked(&key2, &sch2);
DES_set_key_unchecked(&key3, &sch3);
#endif
#ifndef OPENSSL_NO_AES
AES_set_encrypt_key(key16, 128, &aes_ks1);
AES_set_encrypt_key(key24, 192, &aes_ks2);
AES_set_encrypt_key(key32, 256, &aes_ks3);
#endif
#ifndef OPENSSL_NO_CAMELLIA
Camellia_set_key(key16, 128, &camellia_ks1);
Camellia_set_key(ckey24, 192, &camellia_ks2);
Camellia_set_key(ckey32, 256, &camellia_ks3);
#endif
#ifndef OPENSSL_NO_IDEA
idea_set_encrypt_key(key16, &idea_ks);
#endif
#ifndef OPENSSL_NO_SEED
SEED_set_key(key16, &seed_ks);
#endif
#ifndef OPENSSL_NO_RC4
RC4_set_key(&rc4_ks, 16, key16);
#endif
#ifndef OPENSSL_NO_RC2
RC2_set_key(&rc2_ks, 16, key16, 128);
#endif
#ifndef OPENSSL_NO_RC5
RC5_32_set_key(&rc5_ks, 16, key16, 12);
#endif
#ifndef OPENSSL_NO_BF
BF_set_key(&bf_ks, 16, key16);
#endif
#ifndef OPENSSL_NO_CAST
CAST_set_key(&cast_ks, 16, key16);
#endif
#ifndef OPENSSL_NO_RSA
memset(rsa_c, 0, sizeof(rsa_c));
#endif
#ifndef SIGALRM
# ifndef OPENSSL_NO_DES
BIO_printf(bio_err, "First we calculate the approximate speed ...\n");
count = 10;
do {
long it;
count *= 2;
Time_F(START);
for (it = count; it; it--)
DES_ecb_encrypt((DES_cblock *)loopargs[0].buf,
(DES_cblock *)loopargs[0].buf, &sch, DES_ENCRYPT);
d = Time_F(STOP);
} while (d < 3);
save_count = count;
c[D_MD2][0] = count / 10;
c[D_MDC2][0] = count / 10;
c[D_MD4][0] = count;
c[D_MD5][0] = count;
c[D_HMAC][0] = count;
c[D_SHA1][0] = count;
c[D_RMD160][0] = count;
c[D_RC4][0] = count * 5;
c[D_CBC_DES][0] = count;
c[D_EDE3_DES][0] = count / 3;
c[D_CBC_IDEA][0] = count;
c[D_CBC_SEED][0] = count;
c[D_CBC_RC2][0] = count;
c[D_CBC_RC5][0] = count;
c[D_CBC_BF][0] = count;
c[D_CBC_CAST][0] = count;
c[D_CBC_128_AES][0] = count;
c[D_CBC_192_AES][0] = count;
c[D_CBC_256_AES][0] = count;
c[D_CBC_128_CML][0] = count;
c[D_CBC_192_CML][0] = count;
c[D_CBC_256_CML][0] = count;
c[D_SHA256][0] = count;
c[D_SHA512][0] = count;
c[D_WHIRLPOOL][0] = count;
c[D_IGE_128_AES][0] = count;
c[D_IGE_192_AES][0] = count;
c[D_IGE_256_AES][0] = count;
c[D_GHASH][0] = count;
for (i = 1; i < SIZE_NUM; i++) {
long l0, l1;
l0 = (long)lengths[0];
l1 = (long)lengths[i];
c[D_MD2][i] = c[D_MD2][0] * 4 * l0 / l1;
c[D_MDC2][i] = c[D_MDC2][0] * 4 * l0 / l1;
c[D_MD4][i] = c[D_MD4][0] * 4 * l0 / l1;
c[D_MD5][i] = c[D_MD5][0] * 4 * l0 / l1;
c[D_HMAC][i] = c[D_HMAC][0] * 4 * l0 / l1;
c[D_SHA1][i] = c[D_SHA1][0] * 4 * l0 / l1;
c[D_RMD160][i] = c[D_RMD160][0] * 4 * l0 / l1;
c[D_SHA256][i] = c[D_SHA256][0] * 4 * l0 / l1;
c[D_SHA512][i] = c[D_SHA512][0] * 4 * l0 / l1;
c[D_WHIRLPOOL][i] = c[D_WHIRLPOOL][0] * 4 * l0 / l1;
c[D_GHASH][i] = c[D_GHASH][0] * 4 * l0 / l1;
l0 = (long)lengths[i - 1];
c[D_RC4][i] = c[D_RC4][i - 1] * l0 / l1;
c[D_CBC_DES][i] = c[D_CBC_DES][i - 1] * l0 / l1;
c[D_EDE3_DES][i] = c[D_EDE3_DES][i - 1] * l0 / l1;
c[D_CBC_IDEA][i] = c[D_CBC_IDEA][i - 1] * l0 / l1;
c[D_CBC_SEED][i] = c[D_CBC_SEED][i - 1] * l0 / l1;
c[D_CBC_RC2][i] = c[D_CBC_RC2][i - 1] * l0 / l1;
c[D_CBC_RC5][i] = c[D_CBC_RC5][i - 1] * l0 / l1;
c[D_CBC_BF][i] = c[D_CBC_BF][i - 1] * l0 / l1;
c[D_CBC_CAST][i] = c[D_CBC_CAST][i - 1] * l0 / l1;
c[D_CBC_128_AES][i] = c[D_CBC_128_AES][i - 1] * l0 / l1;
c[D_CBC_192_AES][i] = c[D_CBC_192_AES][i - 1] * l0 / l1;
c[D_CBC_256_AES][i] = c[D_CBC_256_AES][i - 1] * l0 / l1;
c[D_CBC_128_CML][i] = c[D_CBC_128_CML][i - 1] * l0 / l1;
c[D_CBC_192_CML][i] = c[D_CBC_192_CML][i - 1] * l0 / l1;
c[D_CBC_256_CML][i] = c[D_CBC_256_CML][i - 1] * l0 / l1;
c[D_IGE_128_AES][i] = c[D_IGE_128_AES][i - 1] * l0 / l1;
c[D_IGE_192_AES][i] = c[D_IGE_192_AES][i - 1] * l0 / l1;
c[D_IGE_256_AES][i] = c[D_IGE_256_AES][i - 1] * l0 / l1;
}
# ifndef OPENSSL_NO_RSA
rsa_c[R_RSA_512][0] = count / 2000;
rsa_c[R_RSA_512][1] = count / 400;
for (i = 1; i < RSA_NUM; i++) {
rsa_c[i][0] = rsa_c[i - 1][0] / 8;
rsa_c[i][1] = rsa_c[i - 1][1] / 4;
if ((rsa_doit[i] <= 1) && (rsa_c[i][0] == 0))
rsa_doit[i] = 0;
else {
if (rsa_c[i][0] == 0) {
rsa_c[i][0] = 1;
rsa_c[i][1] = 20;
}
}
}
# endif
# ifndef OPENSSL_NO_DSA
dsa_c[R_DSA_512][0] = count / 1000;
dsa_c[R_DSA_512][1] = count / 1000 / 2;
for (i = 1; i < DSA_NUM; i++) {
dsa_c[i][0] = dsa_c[i - 1][0] / 4;
dsa_c[i][1] = dsa_c[i - 1][1] / 4;
if ((dsa_doit[i] <= 1) && (dsa_c[i][0] == 0))
dsa_doit[i] = 0;
else {
if (dsa_c[i] == 0) {
dsa_c[i][0] = 1;
dsa_c[i][1] = 1;
}
}
}
# endif
# ifndef OPENSSL_NO_EC
ecdsa_c[R_EC_P160][0] = count / 1000;
ecdsa_c[R_EC_P160][1] = count / 1000 / 2;
for (i = R_EC_P192; i <= R_EC_P521; i++) {
ecdsa_c[i][0] = ecdsa_c[i - 1][0] / 2;
ecdsa_c[i][1] = ecdsa_c[i - 1][1] / 2;
if ((ecdsa_doit[i] <= 1) && (ecdsa_c[i][0] == 0))
ecdsa_doit[i] = 0;
else {
if (ecdsa_c[i] == 0) {
ecdsa_c[i][0] = 1;
ecdsa_c[i][1] = 1;
}
}
}
ecdsa_c[R_EC_K163][0] = count / 1000;
ecdsa_c[R_EC_K163][1] = count / 1000 / 2;
for (i = R_EC_K233; i <= R_EC_K571; i++) {
ecdsa_c[i][0] = ecdsa_c[i - 1][0] / 2;
ecdsa_c[i][1] = ecdsa_c[i - 1][1] / 2;
if ((ecdsa_doit[i] <= 1) && (ecdsa_c[i][0] == 0))
ecdsa_doit[i] = 0;
else {
if (ecdsa_c[i] == 0) {
ecdsa_c[i][0] = 1;
ecdsa_c[i][1] = 1;
}
}
}
ecdsa_c[R_EC_B163][0] = count / 1000;
ecdsa_c[R_EC_B163][1] = count / 1000 / 2;
for (i = R_EC_B233; i <= R_EC_B571; i++) {
ecdsa_c[i][0] = ecdsa_c[i - 1][0] / 2;
ecdsa_c[i][1] = ecdsa_c[i - 1][1] / 2;
if ((ecdsa_doit[i] <= 1) && (ecdsa_c[i][0] == 0))
ecdsa_doit[i] = 0;
else {
if (ecdsa_c[i] == 0) {
ecdsa_c[i][0] = 1;
ecdsa_c[i][1] = 1;
}
}
}
ecdh_c[R_EC_P160][0] = count / 1000;
ecdh_c[R_EC_P160][1] = count / 1000;
for (i = R_EC_P192; i <= R_EC_P521; i++) {
ecdh_c[i][0] = ecdh_c[i - 1][0] / 2;
ecdh_c[i][1] = ecdh_c[i - 1][1] / 2;
if ((ecdh_doit[i] <= 1) && (ecdh_c[i][0] == 0))
ecdh_doit[i] = 0;
else {
if (ecdh_c[i] == 0) {
ecdh_c[i][0] = 1;
ecdh_c[i][1] = 1;
}
}
}
ecdh_c[R_EC_K163][0] = count / 1000;
ecdh_c[R_EC_K163][1] = count / 1000;
for (i = R_EC_K233; i <= R_EC_K571; i++) {
ecdh_c[i][0] = ecdh_c[i - 1][0] / 2;
ecdh_c[i][1] = ecdh_c[i - 1][1] / 2;
if ((ecdh_doit[i] <= 1) && (ecdh_c[i][0] == 0))
ecdh_doit[i] = 0;
else {
if (ecdh_c[i] == 0) {
ecdh_c[i][0] = 1;
ecdh_c[i][1] = 1;
}
}
}
ecdh_c[R_EC_B163][0] = count / 1000;
ecdh_c[R_EC_B163][1] = count / 1000;
for (i = R_EC_B233; i <= R_EC_B571; i++) {
ecdh_c[i][0] = ecdh_c[i - 1][0] / 2;
ecdh_c[i][1] = ecdh_c[i - 1][1] / 2;
if ((ecdh_doit[i] <= 1) && (ecdh_c[i][0] == 0))
ecdh_doit[i] = 0;
else {
if (ecdh_c[i] == 0) {
ecdh_c[i][0] = 1;
ecdh_c[i][1] = 1;
}
}
}
# endif
# else
# error "You cannot disable DES on systems without SIGALRM."
# endif
#else
# ifndef _WIN32
signal(SIGALRM, sig_done);
# endif
#endif
#ifndef OPENSSL_NO_MD2
if (doit[D_MD2]) {
for (testnum = 0; testnum < SIZE_NUM; testnum++) {
print_message(names[D_MD2], c[D_MD2][testnum], lengths[testnum]);
Time_F(START);
count = run_benchmark(async_jobs, EVP_Digest_MD2_loop, loopargs);
d = Time_F(STOP);
print_result(D_MD2, testnum, count, d);
}
}
#endif
#ifndef OPENSSL_NO_MDC2
if (doit[D_MDC2]) {
for (testnum = 0; testnum < SIZE_NUM; testnum++) {
print_message(names[D_MDC2], c[D_MDC2][testnum], lengths[testnum]);
Time_F(START);
count = run_benchmark(async_jobs, EVP_Digest_MDC2_loop, loopargs);
d = Time_F(STOP);
print_result(D_MDC2, testnum, count, d);
}
}
#endif
#ifndef OPENSSL_NO_MD4
if (doit[D_MD4]) {
for (testnum = 0; testnum < SIZE_NUM; testnum++) {
print_message(names[D_MD4], c[D_MD4][testnum], lengths[testnum]);
Time_F(START);
count = run_benchmark(async_jobs, EVP_Digest_MD4_loop, loopargs);
d = Time_F(STOP);
print_result(D_MD4, testnum, count, d);
}
}
#endif
#ifndef OPENSSL_NO_MD5
if (doit[D_MD5]) {
for (testnum = 0; testnum < SIZE_NUM; testnum++) {
print_message(names[D_MD5], c[D_MD5][testnum], lengths[testnum]);
Time_F(START);
count = run_benchmark(async_jobs, MD5_loop, loopargs);
d = Time_F(STOP);
print_result(D_MD5, testnum, count, d);
}
}
#endif
#ifndef OPENSSL_NO_MD5
if (doit[D_HMAC]) {
for (i = 0; i < loopargs_len; i++) {
loopargs[i].hctx = HMAC_CTX_new();
if (loopargs[i].hctx == NULL) {
BIO_printf(bio_err, "HMAC malloc failure, exiting...");
exit(1);
}
HMAC_Init_ex(loopargs[i].hctx, (unsigned char *)"This is a key...",
16, EVP_md5(), NULL);
}
for (testnum = 0; testnum < SIZE_NUM; testnum++) {
print_message(names[D_HMAC], c[D_HMAC][testnum], lengths[testnum]);
Time_F(START);
count = run_benchmark(async_jobs, HMAC_loop, loopargs);
d = Time_F(STOP);
print_result(D_HMAC, testnum, count, d);
}
for (i = 0; i < loopargs_len; i++) {
HMAC_CTX_free(loopargs[i].hctx);
}
}
#endif
if (doit[D_SHA1]) {
for (testnum = 0; testnum < SIZE_NUM; testnum++) {
print_message(names[D_SHA1], c[D_SHA1][testnum], lengths[testnum]);
Time_F(START);
count = run_benchmark(async_jobs, SHA1_loop, loopargs);
d = Time_F(STOP);
print_result(D_SHA1, testnum, count, d);
}
}
if (doit[D_SHA256]) {
for (testnum = 0; testnum < SIZE_NUM; testnum++) {
print_message(names[D_SHA256], c[D_SHA256][testnum], lengths[testnum]);
Time_F(START);
count = run_benchmark(async_jobs, SHA256_loop, loopargs);
d = Time_F(STOP);
print_result(D_SHA256, testnum, count, d);
}
}
if (doit[D_SHA512]) {
for (testnum = 0; testnum < SIZE_NUM; testnum++) {
print_message(names[D_SHA512], c[D_SHA512][testnum], lengths[testnum]);
Time_F(START);
count = run_benchmark(async_jobs, SHA512_loop, loopargs);
d = Time_F(STOP);
print_result(D_SHA512, testnum, count, d);
}
}
#ifndef OPENSSL_NO_WHIRLPOOL
if (doit[D_WHIRLPOOL]) {
for (testnum = 0; testnum < SIZE_NUM; testnum++) {
print_message(names[D_WHIRLPOOL], c[D_WHIRLPOOL][testnum], lengths[testnum]);
Time_F(START);
count = run_benchmark(async_jobs, WHIRLPOOL_loop, loopargs);
d = Time_F(STOP);
print_result(D_WHIRLPOOL, testnum, count, d);
}
}
#endif
#ifndef OPENSSL_NO_RMD160
if (doit[D_RMD160]) {
for (testnum = 0; testnum < SIZE_NUM; testnum++) {
print_message(names[D_RMD160], c[D_RMD160][testnum], lengths[testnum]);
Time_F(START);
count = run_benchmark(async_jobs, EVP_Digest_RMD160_loop, loopargs);
d = Time_F(STOP);
print_result(D_RMD160, testnum, count, d);
}
}
#endif
#ifndef OPENSSL_NO_RC4
if (doit[D_RC4]) {
for (testnum = 0; testnum < SIZE_NUM; testnum++) {
print_message(names[D_RC4], c[D_RC4][testnum], lengths[testnum]);
Time_F(START);
count = run_benchmark(async_jobs, RC4_loop, loopargs);
d = Time_F(STOP);
print_result(D_RC4, testnum, count, d);
}
}
#endif
#ifndef OPENSSL_NO_DES
if (doit[D_CBC_DES]) {
for (testnum = 0; testnum < SIZE_NUM; testnum++) {
print_message(names[D_CBC_DES], c[D_CBC_DES][testnum], lengths[testnum]);
Time_F(START);
count = run_benchmark(async_jobs, DES_ncbc_encrypt_loop, loopargs);
d = Time_F(STOP);
print_result(D_CBC_DES, testnum, count, d);
}
}
if (doit[D_EDE3_DES]) {
for (testnum = 0; testnum < SIZE_NUM; testnum++) {
print_message(names[D_EDE3_DES], c[D_EDE3_DES][testnum], lengths[testnum]);
Time_F(START);
count = run_benchmark(async_jobs, DES_ede3_cbc_encrypt_loop, loopargs);
d = Time_F(STOP);
print_result(D_EDE3_DES, testnum, count, d);
}
}
#endif
#ifndef OPENSSL_NO_AES
if (doit[D_CBC_128_AES]) {
for (testnum = 0; testnum < SIZE_NUM; testnum++) {
print_message(names[D_CBC_128_AES], c[D_CBC_128_AES][testnum],
lengths[testnum]);
Time_F(START);
count = run_benchmark(async_jobs, AES_cbc_128_encrypt_loop, loopargs);
d = Time_F(STOP);
print_result(D_CBC_128_AES, testnum, count, d);
}
}
if (doit[D_CBC_192_AES]) {
for (testnum = 0; testnum < SIZE_NUM; testnum++) {
print_message(names[D_CBC_192_AES], c[D_CBC_192_AES][testnum],
lengths[testnum]);
Time_F(START);
count = run_benchmark(async_jobs, AES_cbc_192_encrypt_loop, loopargs);
d = Time_F(STOP);
print_result(D_CBC_192_AES, testnum, count, d);
}
}
if (doit[D_CBC_256_AES]) {
for (testnum = 0; testnum < SIZE_NUM; testnum++) {
print_message(names[D_CBC_256_AES], c[D_CBC_256_AES][testnum],
lengths[testnum]);
Time_F(START);
count = run_benchmark(async_jobs, AES_cbc_256_encrypt_loop, loopargs);
d = Time_F(STOP);
print_result(D_CBC_256_AES, testnum, count, d);
}
}
if (doit[D_IGE_128_AES]) {
for (testnum = 0; testnum < SIZE_NUM; testnum++) {
print_message(names[D_IGE_128_AES], c[D_IGE_128_AES][testnum],
lengths[testnum]);
Time_F(START);
count = run_benchmark(async_jobs, AES_ige_128_encrypt_loop, loopargs);
d = Time_F(STOP);
print_result(D_IGE_128_AES, testnum, count, d);
}
}
if (doit[D_IGE_192_AES]) {
for (testnum = 0; testnum < SIZE_NUM; testnum++) {
print_message(names[D_IGE_192_AES], c[D_IGE_192_AES][testnum],
lengths[testnum]);
Time_F(START);
count = run_benchmark(async_jobs, AES_ige_192_encrypt_loop, loopargs);
d = Time_F(STOP);
print_result(D_IGE_192_AES, testnum, count, d);
}
}
if (doit[D_IGE_256_AES]) {
for (testnum = 0; testnum < SIZE_NUM; testnum++) {
print_message(names[D_IGE_256_AES], c[D_IGE_256_AES][testnum],
lengths[testnum]);
Time_F(START);
count = run_benchmark(async_jobs, AES_ige_256_encrypt_loop, loopargs);
d = Time_F(STOP);
print_result(D_IGE_256_AES, testnum, count, d);
}
}
if (doit[D_GHASH]) {
for (i = 0; i < loopargs_len; i++) {
loopargs[i].gcm_ctx = CRYPTO_gcm128_new(&aes_ks1, (block128_f) AES_encrypt);
CRYPTO_gcm128_setiv(loopargs[i].gcm_ctx, (unsigned char *)"0123456789ab", 12);
}
for (testnum = 0; testnum < SIZE_NUM; testnum++) {
print_message(names[D_GHASH], c[D_GHASH][testnum], lengths[testnum]);
Time_F(START);
count = run_benchmark(async_jobs, CRYPTO_gcm128_aad_loop, loopargs);
d = Time_F(STOP);
print_result(D_GHASH, testnum, count, d);
}
for (i = 0; i < loopargs_len; i++)
CRYPTO_gcm128_release(loopargs[i].gcm_ctx);
}
#endif
#ifndef OPENSSL_NO_CAMELLIA
if (doit[D_CBC_128_CML]) {
for (testnum = 0; testnum < SIZE_NUM; testnum++) {
print_message(names[D_CBC_128_CML], c[D_CBC_128_CML][testnum],
lengths[testnum]);
if (async_jobs > 0) {
BIO_printf(bio_err, "Async mode is not supported, exiting...");
exit(1);
}
Time_F(START);
for (count = 0, run = 1; COND(c[D_CBC_128_CML][testnum]); count++)
Camellia_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
(unsigned long)lengths[testnum], &camellia_ks1,
iv, CAMELLIA_ENCRYPT);
d = Time_F(STOP);
print_result(D_CBC_128_CML, testnum, count, d);
}
}
if (doit[D_CBC_192_CML]) {
for (testnum = 0; testnum < SIZE_NUM; testnum++) {
print_message(names[D_CBC_192_CML], c[D_CBC_192_CML][testnum],
lengths[testnum]);
if (async_jobs > 0) {
BIO_printf(bio_err, "Async mode is not supported, exiting...");
exit(1);
}
Time_F(START);
for (count = 0, run = 1; COND(c[D_CBC_192_CML][testnum]); count++)
Camellia_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
(unsigned long)lengths[testnum], &camellia_ks2,
iv, CAMELLIA_ENCRYPT);
d = Time_F(STOP);
print_result(D_CBC_192_CML, testnum, count, d);
}
}
if (doit[D_CBC_256_CML]) {
for (testnum = 0; testnum < SIZE_NUM; testnum++) {
print_message(names[D_CBC_256_CML], c[D_CBC_256_CML][testnum],
lengths[testnum]);
if (async_jobs > 0) {
BIO_printf(bio_err, "Async mode is not supported, exiting...");
exit(1);
}
Time_F(START);
for (count = 0, run = 1; COND(c[D_CBC_256_CML][testnum]); count++)
Camellia_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
(unsigned long)lengths[testnum], &camellia_ks3,
iv, CAMELLIA_ENCRYPT);
d = Time_F(STOP);
print_result(D_CBC_256_CML, testnum, count, d);
}
}
#endif
#ifndef OPENSSL_NO_IDEA
if (doit[D_CBC_IDEA]) {
for (testnum = 0; testnum < SIZE_NUM; testnum++) {
print_message(names[D_CBC_IDEA], c[D_CBC_IDEA][testnum], lengths[testnum]);
if (async_jobs > 0) {
BIO_printf(bio_err, "Async mode is not supported, exiting...");
exit(1);
}
Time_F(START);
for (count = 0, run = 1; COND(c[D_CBC_IDEA][testnum]); count++)
idea_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
(unsigned long)lengths[testnum], &idea_ks,
iv, IDEA_ENCRYPT);
d = Time_F(STOP);
print_result(D_CBC_IDEA, testnum, count, d);
}
}
#endif
#ifndef OPENSSL_NO_SEED
if (doit[D_CBC_SEED]) {
for (testnum = 0; testnum < SIZE_NUM; testnum++) {
print_message(names[D_CBC_SEED], c[D_CBC_SEED][testnum], lengths[testnum]);
if (async_jobs > 0) {
BIO_printf(bio_err, "Async mode is not supported, exiting...");
exit(1);
}
Time_F(START);
for (count = 0, run = 1; COND(c[D_CBC_SEED][testnum]); count++)
SEED_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
(unsigned long)lengths[testnum], &seed_ks, iv, 1);
d = Time_F(STOP);
print_result(D_CBC_SEED, testnum, count, d);
}
}
#endif
#ifndef OPENSSL_NO_RC2
if (doit[D_CBC_RC2]) {
for (testnum = 0; testnum < SIZE_NUM; testnum++) {
print_message(names[D_CBC_RC2], c[D_CBC_RC2][testnum], lengths[testnum]);
if (async_jobs > 0) {
BIO_printf(bio_err, "Async mode is not supported, exiting...");
exit(1);
}
Time_F(START);
for (count = 0, run = 1; COND(c[D_CBC_RC2][testnum]); count++)
RC2_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
(unsigned long)lengths[testnum], &rc2_ks,
iv, RC2_ENCRYPT);
d = Time_F(STOP);
print_result(D_CBC_RC2, testnum, count, d);
}
}
#endif
#ifndef OPENSSL_NO_RC5
if (doit[D_CBC_RC5]) {
for (testnum = 0; testnum < SIZE_NUM; testnum++) {
print_message(names[D_CBC_RC5], c[D_CBC_RC5][testnum], lengths[testnum]);
if (async_jobs > 0) {
BIO_printf(bio_err, "Async mode is not supported, exiting...");
exit(1);
}
Time_F(START);
for (count = 0, run = 1; COND(c[D_CBC_RC5][testnum]); count++)
RC5_32_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
(unsigned long)lengths[testnum], &rc5_ks,
iv, RC5_ENCRYPT);
d = Time_F(STOP);
print_result(D_CBC_RC5, testnum, count, d);
}
}
#endif
#ifndef OPENSSL_NO_BF
if (doit[D_CBC_BF]) {
for (testnum = 0; testnum < SIZE_NUM; testnum++) {
print_message(names[D_CBC_BF], c[D_CBC_BF][testnum], lengths[testnum]);
if (async_jobs > 0) {
BIO_printf(bio_err, "Async mode is not supported, exiting...");
exit(1);
}
Time_F(START);
for (count = 0, run = 1; COND(c[D_CBC_BF][testnum]); count++)
BF_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
(unsigned long)lengths[testnum], &bf_ks,
iv, BF_ENCRYPT);
d = Time_F(STOP);
print_result(D_CBC_BF, testnum, count, d);
}
}
#endif
#ifndef OPENSSL_NO_CAST
if (doit[D_CBC_CAST]) {
for (testnum = 0; testnum < SIZE_NUM; testnum++) {
print_message(names[D_CBC_CAST], c[D_CBC_CAST][testnum], lengths[testnum]);
if (async_jobs > 0) {
BIO_printf(bio_err, "Async mode is not supported, exiting...");
exit(1);
}
Time_F(START);
for (count = 0, run = 1; COND(c[D_CBC_CAST][testnum]); count++)
CAST_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
(unsigned long)lengths[testnum], &cast_ks,
iv, CAST_ENCRYPT);
d = Time_F(STOP);
print_result(D_CBC_CAST, testnum, count, d);
}
}
#endif
if (doit[D_EVP]) {
#ifdef EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK
if (multiblock && evp_cipher) {
if (!
(EVP_CIPHER_flags(evp_cipher) &
EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK)) {
BIO_printf(bio_err, "%s is not multi-block capable\n",
OBJ_nid2ln(EVP_CIPHER_nid(evp_cipher)));
goto end;
}
if (async_jobs > 0) {
BIO_printf(bio_err, "Async mode is not supported, exiting...");
exit(1);
}
multiblock_speed(evp_cipher);
ret = 0;
goto end;
}
#endif
for (testnum = 0; testnum < SIZE_NUM; testnum++) {
if (evp_cipher) {
names[D_EVP] = OBJ_nid2ln(EVP_CIPHER_nid(evp_cipher));
print_message(names[D_EVP], save_count, lengths[testnum]);
for (k = 0; k < loopargs_len; k++) {
loopargs[k].ctx = EVP_CIPHER_CTX_new();
if (decrypt)
EVP_DecryptInit_ex(loopargs[k].ctx, evp_cipher, NULL, key16, iv);
else
EVP_EncryptInit_ex(loopargs[k].ctx, evp_cipher, NULL, key16, iv);
EVP_CIPHER_CTX_set_padding(loopargs[k].ctx, 0);
}
Time_F(START);
count = run_benchmark(async_jobs, EVP_Update_loop, loopargs);
d = Time_F(STOP);
for (k = 0; k < loopargs_len; k++) {
EVP_CIPHER_CTX_free(loopargs[k].ctx);
}
}
if (evp_md) {
names[D_EVP] = OBJ_nid2ln(EVP_MD_type(evp_md));
print_message(names[D_EVP], save_count, lengths[testnum]);
Time_F(START);
count = run_benchmark(async_jobs, EVP_Digest_loop, loopargs);
d = Time_F(STOP);
}
print_result(D_EVP, testnum, count, d);
}
}
for (i = 0; i < loopargs_len; i++)
RAND_bytes(loopargs[i].buf, 36);
#ifndef OPENSSL_NO_RSA
for (testnum = 0; testnum < RSA_NUM; testnum++) {
int st = 0;
if (!rsa_doit[testnum])
continue;
for (i = 0; i < loopargs_len; i++) {
st = RSA_sign(NID_md5_sha1, loopargs[i].buf, 36, loopargs[i].buf2,
loopargs[i].siglen, loopargs[i].rsa_key[testnum]);
if (st == 0)
break;
}
if (st == 0) {
BIO_printf(bio_err,
"RSA sign failure. No RSA sign will be done.\n");
ERR_print_errors(bio_err);
rsa_count = 1;
} else {
pkey_print_message("private", "rsa",
rsa_c[testnum][0], rsa_bits[testnum], RSA_SECONDS);
Time_F(START);
count = run_benchmark(async_jobs, RSA_sign_loop, loopargs);
d = Time_F(STOP);
BIO_printf(bio_err,
mr ? "+R1:%ld:%d:%.2f\n"
: "%ld %d bit private RSA's in %.2fs\n",
count, rsa_bits[testnum], d);
rsa_results[testnum][0] = d / (double)count;
rsa_count = count;
}
for (i = 0; i < loopargs_len; i++) {
st = RSA_verify(NID_md5_sha1, loopargs[i].buf, 36, loopargs[i].buf2,
*(loopargs[i].siglen), loopargs[i].rsa_key[testnum]);
if (st <= 0)
break;
}
if (st <= 0) {
BIO_printf(bio_err,
"RSA verify failure. No RSA verify will be done.\n");
ERR_print_errors(bio_err);
rsa_doit[testnum] = 0;
} else {
pkey_print_message("public", "rsa",
rsa_c[testnum][1], rsa_bits[testnum], RSA_SECONDS);
Time_F(START);
count = run_benchmark(async_jobs, RSA_verify_loop, loopargs);
d = Time_F(STOP);
BIO_printf(bio_err,
mr ? "+R2:%ld:%d:%.2f\n"
: "%ld %d bit public RSA's in %.2fs\n",
count, rsa_bits[testnum], d);
rsa_results[testnum][1] = d / (double)count;
}
if (rsa_count <= 1) {
for (testnum++; testnum < RSA_NUM; testnum++)
rsa_doit[testnum] = 0;
}
}
#endif
for (i = 0; i < loopargs_len; i++)
RAND_bytes(loopargs[i].buf, 36);
#ifndef OPENSSL_NO_DSA
if (RAND_status() != 1) {
RAND_seed(rnd_seed, sizeof rnd_seed);
rnd_fake = 1;
}
for (testnum = 0; testnum < DSA_NUM; testnum++) {
int st = 0;
if (!dsa_doit[testnum])
continue;
for (i = 0; i < loopargs_len; i++) {
st = DSA_sign(0, loopargs[i].buf, 20, loopargs[i].buf2,
loopargs[i].siglen, loopargs[i].dsa_key[testnum]);
if (st == 0)
break;
}
if (st == 0) {
BIO_printf(bio_err,
"DSA sign failure. No DSA sign will be done.\n");
ERR_print_errors(bio_err);
rsa_count = 1;
} else {
pkey_print_message("sign", "dsa",
dsa_c[testnum][0], dsa_bits[testnum], DSA_SECONDS);
Time_F(START);
count = run_benchmark(async_jobs, DSA_sign_loop, loopargs);
d = Time_F(STOP);
BIO_printf(bio_err,
mr ? "+R3:%ld:%d:%.2f\n"
: "%ld %d bit DSA signs in %.2fs\n",
count, dsa_bits[testnum], d);
dsa_results[testnum][0] = d / (double)count;
rsa_count = count;
}
for (i = 0; i < loopargs_len; i++) {
st = DSA_verify(0, loopargs[i].buf, 20, loopargs[i].buf2,
*(loopargs[i].siglen), loopargs[i].dsa_key[testnum]);
if (st <= 0)
break;
}
if (st <= 0) {
BIO_printf(bio_err,
"DSA verify failure. No DSA verify will be done.\n");
ERR_print_errors(bio_err);
dsa_doit[testnum] = 0;
} else {
pkey_print_message("verify", "dsa",
dsa_c[testnum][1], dsa_bits[testnum], DSA_SECONDS);
Time_F(START);
count = run_benchmark(async_jobs, DSA_verify_loop, loopargs);
d = Time_F(STOP);
BIO_printf(bio_err,
mr ? "+R4:%ld:%d:%.2f\n"
: "%ld %d bit DSA verify in %.2fs\n",
count, dsa_bits[testnum], d);
dsa_results[testnum][1] = d / (double)count;
}
if (rsa_count <= 1) {
for (testnum++; testnum < DSA_NUM; testnum++)
dsa_doit[testnum] = 0;
}
}
if (rnd_fake)
RAND_cleanup();
#endif
#ifndef OPENSSL_NO_EC
if (RAND_status() != 1) {
RAND_seed(rnd_seed, sizeof rnd_seed);
rnd_fake = 1;
}
for (testnum = 0; testnum < EC_NUM; testnum++) {
int st = 1;
if (!ecdsa_doit[testnum])
continue;
for (i = 0; i < loopargs_len; i++) {
loopargs[i].ecdsa[testnum] = EC_KEY_new_by_curve_name(test_curves[testnum]);
if (loopargs[i].ecdsa[testnum] == NULL) {
st = 0;
break;
}
}
if (st == 0) {
BIO_printf(bio_err, "ECDSA failure.\n");
ERR_print_errors(bio_err);
rsa_count = 1;
} else {
for (i = 0; i < loopargs_len; i++) {
EC_KEY_precompute_mult(loopargs[i].ecdsa[testnum], NULL);
EC_KEY_generate_key(loopargs[i].ecdsa[testnum]);
st = ECDSA_sign(0, loopargs[i].buf, 20, loopargs[i].buf2,
loopargs[i].siglen, loopargs[i].ecdsa[testnum]);
if (st == 0)
break;
}
if (st == 0) {
BIO_printf(bio_err,
"ECDSA sign failure. No ECDSA sign will be done.\n");
ERR_print_errors(bio_err);
rsa_count = 1;
} else {
pkey_print_message("sign", "ecdsa",
ecdsa_c[testnum][0],
test_curves_bits[testnum], ECDSA_SECONDS);
Time_F(START);
count = run_benchmark(async_jobs, ECDSA_sign_loop, loopargs);
d = Time_F(STOP);
BIO_printf(bio_err,
mr ? "+R5:%ld:%d:%.2f\n" :
"%ld %d bit ECDSA signs in %.2fs \n",
count, test_curves_bits[testnum], d);
ecdsa_results[testnum][0] = d / (double)count;
rsa_count = count;
}
for (i = 0; i < loopargs_len; i++) {
st = ECDSA_verify(0, loopargs[i].buf, 20, loopargs[i].buf2,
*(loopargs[i].siglen), loopargs[i].ecdsa[testnum]);
if (st != 1)
break;
}
if (st != 1) {
BIO_printf(bio_err,
"ECDSA verify failure. No ECDSA verify will be done.\n");
ERR_print_errors(bio_err);
ecdsa_doit[testnum] = 0;
} else {
pkey_print_message("verify", "ecdsa",
ecdsa_c[testnum][1],
test_curves_bits[testnum], ECDSA_SECONDS);
Time_F(START);
count = run_benchmark(async_jobs, ECDSA_verify_loop, loopargs);
d = Time_F(STOP);
BIO_printf(bio_err,
mr ? "+R6:%ld:%d:%.2f\n"
: "%ld %d bit ECDSA verify in %.2fs\n",
count, test_curves_bits[testnum], d);
ecdsa_results[testnum][1] = d / (double)count;
}
if (rsa_count <= 1) {
for (testnum++; testnum < EC_NUM; testnum++)
ecdsa_doit[testnum] = 0;
}
}
}
if (rnd_fake)
RAND_cleanup();
#endif
#ifndef OPENSSL_NO_EC
if (RAND_status() != 1) {
RAND_seed(rnd_seed, sizeof rnd_seed);
rnd_fake = 1;
}
for (testnum = 0; testnum < EC_NUM; testnum++) {
if (!ecdh_doit[testnum])
continue;
for (i = 0; i < loopargs_len; i++) {
loopargs[i].ecdh_a[testnum] = EC_KEY_new_by_curve_name(test_curves[testnum]);
loopargs[i].ecdh_b[testnum] = EC_KEY_new_by_curve_name(test_curves[testnum]);
if (loopargs[i].ecdh_a[testnum] == NULL ||
loopargs[i].ecdh_b[testnum] == NULL) {
ecdh_checks = 0;
break;
}
}
if (ecdh_checks == 0) {
BIO_printf(bio_err, "ECDH failure.\n");
ERR_print_errors(bio_err);
rsa_count = 1;
} else {
for (i = 0; i < loopargs_len; i++) {
if (!EC_KEY_generate_key(loopargs[i].ecdh_a[testnum]) ||
!EC_KEY_generate_key(loopargs[i].ecdh_b[testnum])) {
BIO_printf(bio_err, "ECDH key generation failure.\n");
ERR_print_errors(bio_err);
ecdh_checks = 0;
rsa_count = 1;
} else {
int field_size;
field_size =
EC_GROUP_get_degree(EC_KEY_get0_group(loopargs[i].ecdh_a[testnum]));
if (field_size <= 24 * 8) {
outlen = KDF1_SHA1_len;
kdf = KDF1_SHA1;
} else {
outlen = (field_size + 7) / 8;
kdf = NULL;
}
secret_size_a =
ECDH_compute_key(loopargs[i].secret_a, outlen,
EC_KEY_get0_public_key(loopargs[i].ecdh_b[testnum]),
loopargs[i].ecdh_a[testnum], kdf);
secret_size_b =
ECDH_compute_key(loopargs[i].secret_b, outlen,
EC_KEY_get0_public_key(loopargs[i].ecdh_a[testnum]),
loopargs[i].ecdh_b[testnum], kdf);
if (secret_size_a != secret_size_b)
ecdh_checks = 0;
else
ecdh_checks = 1;
for (secret_idx = 0; (secret_idx < secret_size_a)
&& (ecdh_checks == 1); secret_idx++) {
if (loopargs[i].secret_a[secret_idx] != loopargs[i].secret_b[secret_idx])
ecdh_checks = 0;
}
if (ecdh_checks == 0) {
BIO_printf(bio_err, "ECDH computations don't match.\n");
ERR_print_errors(bio_err);
rsa_count = 1;
break;
}
}
if (ecdh_checks != 0) {
pkey_print_message("", "ecdh",
ecdh_c[testnum][0],
test_curves_bits[testnum], ECDH_SECONDS);
Time_F(START);
count = run_benchmark(async_jobs, ECDH_compute_key_loop, loopargs);
d = Time_F(STOP);
BIO_printf(bio_err,
mr ? "+R7:%ld:%d:%.2f\n" :
"%ld %d-bit ECDH ops in %.2fs\n", count,
test_curves_bits[testnum], d);
ecdh_results[testnum][0] = d / (double)count;
rsa_count = count;
}
}
}
if (rsa_count <= 1) {
for (testnum++; testnum < EC_NUM; testnum++)
ecdh_doit[testnum] = 0;
}
}
if (rnd_fake)
RAND_cleanup();
#endif
#ifndef NO_FORK
show_res:
#endif
if (!mr) {
printf("%s\n", OpenSSL_version(OPENSSL_VERSION));
printf("%s\n", OpenSSL_version(OPENSSL_BUILT_ON));
printf("options:");
printf("%s ", BN_options());
#ifndef OPENSSL_NO_MD2
printf("%s ", MD2_options());
#endif
#ifndef OPENSSL_NO_RC4
printf("%s ", RC4_options());
#endif
#ifndef OPENSSL_NO_DES
printf("%s ", DES_options());
#endif
#ifndef OPENSSL_NO_AES
printf("%s ", AES_options());
#endif
#ifndef OPENSSL_NO_IDEA
printf("%s ", idea_options());
#endif
#ifndef OPENSSL_NO_BF
printf("%s ", BF_options());
#endif
printf("\n%s\n", OpenSSL_version(OPENSSL_CFLAGS));
}
if (pr_header) {
if (mr)
printf("+H");
else {
printf
("The 'numbers' are in 1000s of bytes per second processed.\n");
printf("type ");
}
for (testnum = 0; testnum < SIZE_NUM; testnum++)
printf(mr ? ":%d" : "%7d bytes", lengths[testnum]);
printf("\n");
}
for (k = 0; k < ALGOR_NUM; k++) {
if (!doit[k])
continue;
if (mr)
printf("+F:%d:%s", k, names[k]);
else
printf("%-13s", names[k]);
for (testnum = 0; testnum < SIZE_NUM; testnum++) {
if (results[k][testnum] > 10000 && !mr)
printf(" %11.2fk", results[k][testnum] / 1e3);
else
printf(mr ? ":%.2f" : " %11.2f ", results[k][testnum]);
}
printf("\n");
}
#ifndef OPENSSL_NO_RSA
testnum = 1;
for (k = 0; k < RSA_NUM; k++) {
if (!rsa_doit[k])
continue;
if (testnum && !mr) {
printf("%18ssign verify sign/s verify/s\n", " ");
testnum = 0;
}
if (mr)
printf("+F2:%u:%u:%f:%f\n",
k, rsa_bits[k], rsa_results[k][0], rsa_results[k][1]);
else
printf("rsa %4u bits %8.6fs %8.6fs %8.1f %8.1f\n",
rsa_bits[k], rsa_results[k][0], rsa_results[k][1],
1.0 / rsa_results[k][0], 1.0 / rsa_results[k][1]);
}
#endif
#ifndef OPENSSL_NO_DSA
testnum = 1;
for (k = 0; k < DSA_NUM; k++) {
if (!dsa_doit[k])
continue;
if (testnum && !mr) {
printf("%18ssign verify sign/s verify/s\n", " ");
testnum = 0;
}
if (mr)
printf("+F3:%u:%u:%f:%f\n",
k, dsa_bits[k], dsa_results[k][0], dsa_results[k][1]);
else
printf("dsa %4u bits %8.6fs %8.6fs %8.1f %8.1f\n",
dsa_bits[k], dsa_results[k][0], dsa_results[k][1],
1.0 / dsa_results[k][0], 1.0 / dsa_results[k][1]);
}
#endif
#ifndef OPENSSL_NO_EC
testnum = 1;
for (k = 0; k < EC_NUM; k++) {
if (!ecdsa_doit[k])
continue;
if (testnum && !mr) {
printf("%30ssign verify sign/s verify/s\n", " ");
testnum = 0;
}
if (mr)
printf("+F4:%u:%u:%f:%f\n",
k, test_curves_bits[k],
ecdsa_results[k][0], ecdsa_results[k][1]);
else
printf("%4u bit ecdsa (%s) %8.4fs %8.4fs %8.1f %8.1f\n",
test_curves_bits[k],
test_curves_names[k],
ecdsa_results[k][0], ecdsa_results[k][1],
1.0 / ecdsa_results[k][0], 1.0 / ecdsa_results[k][1]);
}
#endif
#ifndef OPENSSL_NO_EC
testnum = 1;
for (k = 0; k < EC_NUM; k++) {
if (!ecdh_doit[k])
continue;
if (testnum && !mr) {
printf("%30sop op/s\n", " ");
testnum = 0;
}
if (mr)
printf("+F5:%u:%u:%f:%f\n",
k, test_curves_bits[k],
ecdh_results[k][0], 1.0 / ecdh_results[k][0]);
else
printf("%4u bit ecdh (%s) %8.4fs %8.1f\n",
test_curves_bits[k],
test_curves_names[k],
ecdh_results[k][0], 1.0 / ecdh_results[k][0]);
}
#endif
ret = 0;
end:
ERR_print_errors(bio_err);
for (i = 0; i < loopargs_len; i++) {
OPENSSL_free(loopargs[i].buf_malloc);
OPENSSL_free(loopargs[i].buf2_malloc);
OPENSSL_free(loopargs[i].siglen);
}
#ifndef OPENSSL_NO_RSA
for (i = 0; i < loopargs_len; i++) {
for (k = 0; k < RSA_NUM; k++)
RSA_free(loopargs[i].rsa_key[k]);
}
#endif
#ifndef OPENSSL_NO_DSA
for (i = 0; i < loopargs_len; i++) {
for (k = 0; k < DSA_NUM; k++)
DSA_free(loopargs[i].dsa_key[k]);
}
#endif
#ifndef OPENSSL_NO_EC
for (i = 0; i < loopargs_len; i++) {
for (k = 0; k < EC_NUM; k++) {
EC_KEY_free(loopargs[i].ecdsa[k]);
EC_KEY_free(loopargs[i].ecdh_a[k]);
EC_KEY_free(loopargs[i].ecdh_b[k]);
}
OPENSSL_free(loopargs[i].secret_a);
OPENSSL_free(loopargs[i].secret_b);
}
#endif
if (async_jobs > 0) {
for (i = 0; i < loopargs_len; i++)
ASYNC_WAIT_CTX_free(loopargs[i].wait_ctx);
ASYNC_cleanup_thread();
}
OPENSSL_free(loopargs);
return (ret);
} | ['int speed_main(int argc, char **argv)\n{\n loopargs_t *loopargs = NULL;\n int loopargs_len = 0;\n char *prog;\n const EVP_CIPHER *evp_cipher = NULL;\n double d = 0.0;\n OPTION_CHOICE o;\n int multiblock = 0, doit[ALGOR_NUM], pr_header = 0;\n int dsa_doit[DSA_NUM], rsa_doit[RSA_NUM];\n int ret = 1, i, k, misalign = 0;\n long c[ALGOR_NUM][SIZE_NUM], count = 0, save_count = 0;\n#ifndef NO_FORK\n int multi = 0;\n#endif\n int async_jobs = 0;\n#if !defined(OPENSSL_NO_RSA) || !defined(OPENSSL_NO_DSA)\n long rsa_count = 1;\n#endif\n#ifndef OPENSSL_NO_RC5\n RC5_32_KEY rc5_ks;\n#endif\n#ifndef OPENSSL_NO_RC2\n RC2_KEY rc2_ks;\n#endif\n#ifndef OPENSSL_NO_IDEA\n IDEA_KEY_SCHEDULE idea_ks;\n#endif\n#ifndef OPENSSL_NO_SEED\n SEED_KEY_SCHEDULE seed_ks;\n#endif\n#ifndef OPENSSL_NO_BF\n BF_KEY bf_ks;\n#endif\n#ifndef OPENSSL_NO_CAST\n CAST_KEY cast_ks;\n#endif\n static const unsigned char key16[16] = {\n 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,\n 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12\n };\n#ifndef OPENSSL_NO_AES\n static const unsigned char key24[24] = {\n 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,\n 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12,\n 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34\n };\n static const unsigned char key32[32] = {\n 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,\n 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12,\n 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34,\n 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56\n };\n#endif\n#ifndef OPENSSL_NO_CAMELLIA\n static const unsigned char ckey24[24] = {\n 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,\n 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12,\n 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34\n };\n static const unsigned char ckey32[32] = {\n 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,\n 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12,\n 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34,\n 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56\n };\n CAMELLIA_KEY camellia_ks1, camellia_ks2, camellia_ks3;\n#endif\n#ifndef OPENSSL_NO_DES\n static DES_cblock key = {\n 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0\n };\n static DES_cblock key2 = {\n 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12\n };\n static DES_cblock key3 = {\n 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34\n };\n#endif\n#ifndef OPENSSL_NO_RSA\n static unsigned int rsa_bits[RSA_NUM] = {\n 512, 1024, 2048, 3072, 4096, 7680, 15360\n };\n static unsigned char *rsa_data[RSA_NUM] = {\n test512, test1024, test2048, test3072, test4096, test7680, test15360\n };\n static int rsa_data_length[RSA_NUM] = {\n sizeof(test512), sizeof(test1024),\n sizeof(test2048), sizeof(test3072),\n sizeof(test4096), sizeof(test7680),\n sizeof(test15360)\n };\n#endif\n#ifndef OPENSSL_NO_DSA\n static unsigned int dsa_bits[DSA_NUM] = { 512, 1024, 2048 };\n#endif\n#ifndef OPENSSL_NO_EC\n static unsigned int test_curves[EC_NUM] = {\n NID_secp160r1, NID_X9_62_prime192v1, NID_secp224r1,\n NID_X9_62_prime256v1, NID_secp384r1, NID_secp521r1,\n NID_sect163k1, NID_sect233k1, NID_sect283k1,\n NID_sect409k1, NID_sect571k1, NID_sect163r2,\n NID_sect233r1, NID_sect283r1, NID_sect409r1,\n NID_sect571r1,\n NID_X25519\n };\n static const char *test_curves_names[EC_NUM] = {\n "secp160r1", "nistp192", "nistp224",\n "nistp256", "nistp384", "nistp521",\n "nistk163", "nistk233", "nistk283",\n "nistk409", "nistk571", "nistb163",\n "nistb233", "nistb283", "nistb409",\n "nistb571",\n "X25519"\n };\n static int test_curves_bits[EC_NUM] = {\n 160, 192, 224,\n 256, 384, 521,\n 163, 233, 283,\n 409, 571, 163,\n 233, 283, 409,\n 571, 253\n };\n#endif\n#ifndef OPENSSL_NO_EC\n int ecdsa_doit[EC_NUM];\n int secret_size_a, secret_size_b;\n int ecdh_checks = 1;\n int secret_idx = 0;\n long ecdh_c[EC_NUM][2];\n int ecdh_doit[EC_NUM];\n#endif\n memset(results, 0, sizeof(results));\n memset(c, 0, sizeof(c));\n memset(DES_iv, 0, sizeof(DES_iv));\n memset(iv, 0, sizeof(iv));\n for (i = 0; i < ALGOR_NUM; i++)\n doit[i] = 0;\n for (i = 0; i < RSA_NUM; i++)\n rsa_doit[i] = 0;\n for (i = 0; i < DSA_NUM; i++)\n dsa_doit[i] = 0;\n#ifndef OPENSSL_NO_EC\n for (i = 0; i < EC_NUM; i++)\n ecdsa_doit[i] = 0;\n for (i = 0; i < EC_NUM; i++)\n ecdh_doit[i] = 0;\n#endif\n misalign = 0;\n prog = opt_init(argc, argv, speed_options);\n while ((o = opt_next()) != OPT_EOF) {\n switch (o) {\n case OPT_EOF:\n case OPT_ERR:\n opterr:\n BIO_printf(bio_err, "%s: Use -help for summary.\\n", prog);\n goto end;\n case OPT_HELP:\n opt_help(speed_options);\n ret = 0;\n goto end;\n case OPT_ELAPSED:\n usertime = 0;\n break;\n case OPT_EVP:\n evp_cipher = EVP_get_cipherbyname(opt_arg());\n if (evp_cipher == NULL)\n evp_md = EVP_get_digestbyname(opt_arg());\n if (evp_cipher == NULL && evp_md == NULL) {\n BIO_printf(bio_err,\n "%s: %s an unknown cipher or digest\\n",\n prog, opt_arg());\n goto end;\n }\n doit[D_EVP] = 1;\n break;\n case OPT_DECRYPT:\n decrypt = 1;\n break;\n case OPT_ENGINE:\n engine_id = opt_arg();\n break;\n case OPT_MULTI:\n#ifndef NO_FORK\n multi = atoi(opt_arg());\n#endif\n break;\n case OPT_ASYNCJOBS:\n#ifndef OPENSSL_NO_ASYNC\n async_jobs = atoi(opt_arg());\n if (!ASYNC_is_capable()) {\n BIO_printf(bio_err,\n "%s: async_jobs specified but async not supported\\n",\n prog);\n goto opterr;\n }\n#endif\n break;\n case OPT_MISALIGN:\n if (!opt_int(opt_arg(), &misalign))\n goto end;\n if (misalign > MISALIGN) {\n BIO_printf(bio_err,\n "%s: Maximum offset is %d\\n", prog, MISALIGN);\n goto opterr;\n }\n break;\n case OPT_MR:\n mr = 1;\n break;\n case OPT_MB:\n multiblock = 1;\n break;\n }\n }\n argc = opt_num_rest();\n argv = opt_rest();\n for ( ; *argv; argv++) {\n if (found(*argv, doit_choices, &i)) {\n doit[i] = 1;\n continue;\n }\n#ifndef OPENSSL_NO_DES\n if (strcmp(*argv, "des") == 0) {\n doit[D_CBC_DES] = doit[D_EDE3_DES] = 1;\n continue;\n }\n#endif\n if (strcmp(*argv, "sha") == 0) {\n doit[D_SHA1] = doit[D_SHA256] = doit[D_SHA512] = 1;\n continue;\n }\n#ifndef OPENSSL_NO_RSA\n# ifndef RSA_NULL\n if (strcmp(*argv, "openssl") == 0) {\n RSA_set_default_method(RSA_PKCS1_OpenSSL());\n continue;\n }\n# endif\n if (strcmp(*argv, "rsa") == 0) {\n rsa_doit[R_RSA_512] = rsa_doit[R_RSA_1024] =\n rsa_doit[R_RSA_2048] = rsa_doit[R_RSA_3072] =\n rsa_doit[R_RSA_4096] = rsa_doit[R_RSA_7680] =\n rsa_doit[R_RSA_15360] = 1;\n continue;\n }\n if (found(*argv, rsa_choices, &i)) {\n rsa_doit[i] = 1;\n continue;\n }\n#endif\n#ifndef OPENSSL_NO_DSA\n if (strcmp(*argv, "dsa") == 0) {\n dsa_doit[R_DSA_512] = dsa_doit[R_DSA_1024] =\n dsa_doit[R_DSA_2048] = 1;\n continue;\n }\n if (found(*argv, dsa_choices, &i)) {\n dsa_doit[i] = 2;\n continue;\n }\n#endif\n#ifndef OPENSSL_NO_AES\n if (strcmp(*argv, "aes") == 0) {\n doit[D_CBC_128_AES] = doit[D_CBC_192_AES] =\n doit[D_CBC_256_AES] = 1;\n continue;\n }\n#endif\n#ifndef OPENSSL_NO_CAMELLIA\n if (strcmp(*argv, "camellia") == 0) {\n doit[D_CBC_128_CML] = doit[D_CBC_192_CML] =\n doit[D_CBC_256_CML] = 1;\n continue;\n }\n#endif\n#ifndef OPENSSL_NO_EC\n if (strcmp(*argv, "ecdsa") == 0) {\n for (i = 0; i < EC_NUM; i++)\n ecdsa_doit[i] = 1;\n continue;\n }\n if (found(*argv, ecdsa_choices, &i)) {\n ecdsa_doit[i] = 2;\n continue;\n }\n if (strcmp(*argv, "ecdh") == 0) {\n for (i = 0; i < EC_NUM; i++)\n ecdh_doit[i] = 1;\n continue;\n }\n if (found(*argv, ecdh_choices, &i)) {\n ecdh_doit[i] = 2;\n continue;\n }\n#endif\n BIO_printf(bio_err, "%s: Unknown algorithm %s\\n", prog, *argv);\n goto end;\n }\n if (async_jobs > 0) {\n if (!ASYNC_init_thread(async_jobs, async_jobs)) {\n BIO_printf(bio_err, "Error creating the ASYNC job pool\\n");\n goto end;\n }\n }\n loopargs_len = (async_jobs == 0 ? 1 : async_jobs);\n loopargs = app_malloc(loopargs_len * sizeof(loopargs_t), "array of loopargs");\n memset(loopargs, 0, loopargs_len * sizeof(loopargs_t));\n for (i = 0; i < loopargs_len; i++) {\n if (async_jobs > 0) {\n loopargs[i].wait_ctx = ASYNC_WAIT_CTX_new();\n if (loopargs[i].wait_ctx == NULL) {\n BIO_printf(bio_err, "Error creating the ASYNC_WAIT_CTX\\n");\n goto end;\n }\n }\n loopargs[i].buf_malloc = app_malloc((int)BUFSIZE + MAX_MISALIGNMENT + 1, "input buffer");\n loopargs[i].buf2_malloc = app_malloc((int)BUFSIZE + MAX_MISALIGNMENT + 1, "input buffer");\n loopargs[i].buf = loopargs[i].buf_malloc + misalign;\n loopargs[i].buf2 = loopargs[i].buf2_malloc + misalign;\n loopargs[i].siglen = app_malloc(sizeof(unsigned int), "signature length");\n#ifndef OPENSSL_NO_EC\n loopargs[i].secret_a = app_malloc(MAX_ECDH_SIZE, "ECDH secret a");\n loopargs[i].secret_b = app_malloc(MAX_ECDH_SIZE, "ECDH secret b");\n#endif\n }\n#ifndef NO_FORK\n if (multi && do_multi(multi))\n goto show_res;\n#endif\n (void)setup_engine(engine_id, 0);\n if ((argc == 0) && !doit[D_EVP]) {\n for (i = 0; i < ALGOR_NUM; i++)\n if (i != D_EVP)\n doit[i] = 1;\n for (i = 0; i < RSA_NUM; i++)\n rsa_doit[i] = 1;\n for (i = 0; i < DSA_NUM; i++)\n dsa_doit[i] = 1;\n#ifndef OPENSSL_NO_EC\n for (i = 0; i < EC_NUM; i++)\n ecdsa_doit[i] = 1;\n for (i = 0; i < EC_NUM; i++)\n ecdh_doit[i] = 1;\n#endif\n }\n for (i = 0; i < ALGOR_NUM; i++)\n if (doit[i])\n pr_header++;\n if (usertime == 0 && !mr)\n BIO_printf(bio_err,\n "You have chosen to measure elapsed time "\n "instead of user CPU time.\\n");\n#ifndef OPENSSL_NO_RSA\n for (i = 0; i < loopargs_len; i++) {\n for (k = 0; k < RSA_NUM; k++) {\n const unsigned char *p;\n p = rsa_data[k];\n loopargs[i].rsa_key[k] = d2i_RSAPrivateKey(NULL, &p, rsa_data_length[k]);\n if (loopargs[i].rsa_key[k] == NULL) {\n BIO_printf(bio_err, "internal error loading RSA key number %d\\n",\n k);\n goto end;\n }\n }\n }\n#endif\n#ifndef OPENSSL_NO_DSA\n for (i = 0; i < loopargs_len; i++) {\n loopargs[i].dsa_key[0] = get_dsa512();\n loopargs[i].dsa_key[1] = get_dsa1024();\n loopargs[i].dsa_key[2] = get_dsa2048();\n }\n#endif\n#ifndef OPENSSL_NO_DES\n DES_set_key_unchecked(&key, &sch);\n DES_set_key_unchecked(&key2, &sch2);\n DES_set_key_unchecked(&key3, &sch3);\n#endif\n#ifndef OPENSSL_NO_AES\n AES_set_encrypt_key(key16, 128, &aes_ks1);\n AES_set_encrypt_key(key24, 192, &aes_ks2);\n AES_set_encrypt_key(key32, 256, &aes_ks3);\n#endif\n#ifndef OPENSSL_NO_CAMELLIA\n Camellia_set_key(key16, 128, &camellia_ks1);\n Camellia_set_key(ckey24, 192, &camellia_ks2);\n Camellia_set_key(ckey32, 256, &camellia_ks3);\n#endif\n#ifndef OPENSSL_NO_IDEA\n idea_set_encrypt_key(key16, &idea_ks);\n#endif\n#ifndef OPENSSL_NO_SEED\n SEED_set_key(key16, &seed_ks);\n#endif\n#ifndef OPENSSL_NO_RC4\n RC4_set_key(&rc4_ks, 16, key16);\n#endif\n#ifndef OPENSSL_NO_RC2\n RC2_set_key(&rc2_ks, 16, key16, 128);\n#endif\n#ifndef OPENSSL_NO_RC5\n RC5_32_set_key(&rc5_ks, 16, key16, 12);\n#endif\n#ifndef OPENSSL_NO_BF\n BF_set_key(&bf_ks, 16, key16);\n#endif\n#ifndef OPENSSL_NO_CAST\n CAST_set_key(&cast_ks, 16, key16);\n#endif\n#ifndef OPENSSL_NO_RSA\n memset(rsa_c, 0, sizeof(rsa_c));\n#endif\n#ifndef SIGALRM\n# ifndef OPENSSL_NO_DES\n BIO_printf(bio_err, "First we calculate the approximate speed ...\\n");\n count = 10;\n do {\n long it;\n count *= 2;\n Time_F(START);\n for (it = count; it; it--)\n DES_ecb_encrypt((DES_cblock *)loopargs[0].buf,\n (DES_cblock *)loopargs[0].buf, &sch, DES_ENCRYPT);\n d = Time_F(STOP);\n } while (d < 3);\n save_count = count;\n c[D_MD2][0] = count / 10;\n c[D_MDC2][0] = count / 10;\n c[D_MD4][0] = count;\n c[D_MD5][0] = count;\n c[D_HMAC][0] = count;\n c[D_SHA1][0] = count;\n c[D_RMD160][0] = count;\n c[D_RC4][0] = count * 5;\n c[D_CBC_DES][0] = count;\n c[D_EDE3_DES][0] = count / 3;\n c[D_CBC_IDEA][0] = count;\n c[D_CBC_SEED][0] = count;\n c[D_CBC_RC2][0] = count;\n c[D_CBC_RC5][0] = count;\n c[D_CBC_BF][0] = count;\n c[D_CBC_CAST][0] = count;\n c[D_CBC_128_AES][0] = count;\n c[D_CBC_192_AES][0] = count;\n c[D_CBC_256_AES][0] = count;\n c[D_CBC_128_CML][0] = count;\n c[D_CBC_192_CML][0] = count;\n c[D_CBC_256_CML][0] = count;\n c[D_SHA256][0] = count;\n c[D_SHA512][0] = count;\n c[D_WHIRLPOOL][0] = count;\n c[D_IGE_128_AES][0] = count;\n c[D_IGE_192_AES][0] = count;\n c[D_IGE_256_AES][0] = count;\n c[D_GHASH][0] = count;\n for (i = 1; i < SIZE_NUM; i++) {\n long l0, l1;\n l0 = (long)lengths[0];\n l1 = (long)lengths[i];\n c[D_MD2][i] = c[D_MD2][0] * 4 * l0 / l1;\n c[D_MDC2][i] = c[D_MDC2][0] * 4 * l0 / l1;\n c[D_MD4][i] = c[D_MD4][0] * 4 * l0 / l1;\n c[D_MD5][i] = c[D_MD5][0] * 4 * l0 / l1;\n c[D_HMAC][i] = c[D_HMAC][0] * 4 * l0 / l1;\n c[D_SHA1][i] = c[D_SHA1][0] * 4 * l0 / l1;\n c[D_RMD160][i] = c[D_RMD160][0] * 4 * l0 / l1;\n c[D_SHA256][i] = c[D_SHA256][0] * 4 * l0 / l1;\n c[D_SHA512][i] = c[D_SHA512][0] * 4 * l0 / l1;\n c[D_WHIRLPOOL][i] = c[D_WHIRLPOOL][0] * 4 * l0 / l1;\n c[D_GHASH][i] = c[D_GHASH][0] * 4 * l0 / l1;\n l0 = (long)lengths[i - 1];\n c[D_RC4][i] = c[D_RC4][i - 1] * l0 / l1;\n c[D_CBC_DES][i] = c[D_CBC_DES][i - 1] * l0 / l1;\n c[D_EDE3_DES][i] = c[D_EDE3_DES][i - 1] * l0 / l1;\n c[D_CBC_IDEA][i] = c[D_CBC_IDEA][i - 1] * l0 / l1;\n c[D_CBC_SEED][i] = c[D_CBC_SEED][i - 1] * l0 / l1;\n c[D_CBC_RC2][i] = c[D_CBC_RC2][i - 1] * l0 / l1;\n c[D_CBC_RC5][i] = c[D_CBC_RC5][i - 1] * l0 / l1;\n c[D_CBC_BF][i] = c[D_CBC_BF][i - 1] * l0 / l1;\n c[D_CBC_CAST][i] = c[D_CBC_CAST][i - 1] * l0 / l1;\n c[D_CBC_128_AES][i] = c[D_CBC_128_AES][i - 1] * l0 / l1;\n c[D_CBC_192_AES][i] = c[D_CBC_192_AES][i - 1] * l0 / l1;\n c[D_CBC_256_AES][i] = c[D_CBC_256_AES][i - 1] * l0 / l1;\n c[D_CBC_128_CML][i] = c[D_CBC_128_CML][i - 1] * l0 / l1;\n c[D_CBC_192_CML][i] = c[D_CBC_192_CML][i - 1] * l0 / l1;\n c[D_CBC_256_CML][i] = c[D_CBC_256_CML][i - 1] * l0 / l1;\n c[D_IGE_128_AES][i] = c[D_IGE_128_AES][i - 1] * l0 / l1;\n c[D_IGE_192_AES][i] = c[D_IGE_192_AES][i - 1] * l0 / l1;\n c[D_IGE_256_AES][i] = c[D_IGE_256_AES][i - 1] * l0 / l1;\n }\n# ifndef OPENSSL_NO_RSA\n rsa_c[R_RSA_512][0] = count / 2000;\n rsa_c[R_RSA_512][1] = count / 400;\n for (i = 1; i < RSA_NUM; i++) {\n rsa_c[i][0] = rsa_c[i - 1][0] / 8;\n rsa_c[i][1] = rsa_c[i - 1][1] / 4;\n if ((rsa_doit[i] <= 1) && (rsa_c[i][0] == 0))\n rsa_doit[i] = 0;\n else {\n if (rsa_c[i][0] == 0) {\n rsa_c[i][0] = 1;\n rsa_c[i][1] = 20;\n }\n }\n }\n# endif\n# ifndef OPENSSL_NO_DSA\n dsa_c[R_DSA_512][0] = count / 1000;\n dsa_c[R_DSA_512][1] = count / 1000 / 2;\n for (i = 1; i < DSA_NUM; i++) {\n dsa_c[i][0] = dsa_c[i - 1][0] / 4;\n dsa_c[i][1] = dsa_c[i - 1][1] / 4;\n if ((dsa_doit[i] <= 1) && (dsa_c[i][0] == 0))\n dsa_doit[i] = 0;\n else {\n if (dsa_c[i] == 0) {\n dsa_c[i][0] = 1;\n dsa_c[i][1] = 1;\n }\n }\n }\n# endif\n# ifndef OPENSSL_NO_EC\n ecdsa_c[R_EC_P160][0] = count / 1000;\n ecdsa_c[R_EC_P160][1] = count / 1000 / 2;\n for (i = R_EC_P192; i <= R_EC_P521; i++) {\n ecdsa_c[i][0] = ecdsa_c[i - 1][0] / 2;\n ecdsa_c[i][1] = ecdsa_c[i - 1][1] / 2;\n if ((ecdsa_doit[i] <= 1) && (ecdsa_c[i][0] == 0))\n ecdsa_doit[i] = 0;\n else {\n if (ecdsa_c[i] == 0) {\n ecdsa_c[i][0] = 1;\n ecdsa_c[i][1] = 1;\n }\n }\n }\n ecdsa_c[R_EC_K163][0] = count / 1000;\n ecdsa_c[R_EC_K163][1] = count / 1000 / 2;\n for (i = R_EC_K233; i <= R_EC_K571; i++) {\n ecdsa_c[i][0] = ecdsa_c[i - 1][0] / 2;\n ecdsa_c[i][1] = ecdsa_c[i - 1][1] / 2;\n if ((ecdsa_doit[i] <= 1) && (ecdsa_c[i][0] == 0))\n ecdsa_doit[i] = 0;\n else {\n if (ecdsa_c[i] == 0) {\n ecdsa_c[i][0] = 1;\n ecdsa_c[i][1] = 1;\n }\n }\n }\n ecdsa_c[R_EC_B163][0] = count / 1000;\n ecdsa_c[R_EC_B163][1] = count / 1000 / 2;\n for (i = R_EC_B233; i <= R_EC_B571; i++) {\n ecdsa_c[i][0] = ecdsa_c[i - 1][0] / 2;\n ecdsa_c[i][1] = ecdsa_c[i - 1][1] / 2;\n if ((ecdsa_doit[i] <= 1) && (ecdsa_c[i][0] == 0))\n ecdsa_doit[i] = 0;\n else {\n if (ecdsa_c[i] == 0) {\n ecdsa_c[i][0] = 1;\n ecdsa_c[i][1] = 1;\n }\n }\n }\n ecdh_c[R_EC_P160][0] = count / 1000;\n ecdh_c[R_EC_P160][1] = count / 1000;\n for (i = R_EC_P192; i <= R_EC_P521; i++) {\n ecdh_c[i][0] = ecdh_c[i - 1][0] / 2;\n ecdh_c[i][1] = ecdh_c[i - 1][1] / 2;\n if ((ecdh_doit[i] <= 1) && (ecdh_c[i][0] == 0))\n ecdh_doit[i] = 0;\n else {\n if (ecdh_c[i] == 0) {\n ecdh_c[i][0] = 1;\n ecdh_c[i][1] = 1;\n }\n }\n }\n ecdh_c[R_EC_K163][0] = count / 1000;\n ecdh_c[R_EC_K163][1] = count / 1000;\n for (i = R_EC_K233; i <= R_EC_K571; i++) {\n ecdh_c[i][0] = ecdh_c[i - 1][0] / 2;\n ecdh_c[i][1] = ecdh_c[i - 1][1] / 2;\n if ((ecdh_doit[i] <= 1) && (ecdh_c[i][0] == 0))\n ecdh_doit[i] = 0;\n else {\n if (ecdh_c[i] == 0) {\n ecdh_c[i][0] = 1;\n ecdh_c[i][1] = 1;\n }\n }\n }\n ecdh_c[R_EC_B163][0] = count / 1000;\n ecdh_c[R_EC_B163][1] = count / 1000;\n for (i = R_EC_B233; i <= R_EC_B571; i++) {\n ecdh_c[i][0] = ecdh_c[i - 1][0] / 2;\n ecdh_c[i][1] = ecdh_c[i - 1][1] / 2;\n if ((ecdh_doit[i] <= 1) && (ecdh_c[i][0] == 0))\n ecdh_doit[i] = 0;\n else {\n if (ecdh_c[i] == 0) {\n ecdh_c[i][0] = 1;\n ecdh_c[i][1] = 1;\n }\n }\n }\n# endif\n# else\n# error "You cannot disable DES on systems without SIGALRM."\n# endif\n#else\n# ifndef _WIN32\n signal(SIGALRM, sig_done);\n# endif\n#endif\n#ifndef OPENSSL_NO_MD2\n if (doit[D_MD2]) {\n for (testnum = 0; testnum < SIZE_NUM; testnum++) {\n print_message(names[D_MD2], c[D_MD2][testnum], lengths[testnum]);\n Time_F(START);\n count = run_benchmark(async_jobs, EVP_Digest_MD2_loop, loopargs);\n d = Time_F(STOP);\n print_result(D_MD2, testnum, count, d);\n }\n }\n#endif\n#ifndef OPENSSL_NO_MDC2\n if (doit[D_MDC2]) {\n for (testnum = 0; testnum < SIZE_NUM; testnum++) {\n print_message(names[D_MDC2], c[D_MDC2][testnum], lengths[testnum]);\n Time_F(START);\n count = run_benchmark(async_jobs, EVP_Digest_MDC2_loop, loopargs);\n d = Time_F(STOP);\n print_result(D_MDC2, testnum, count, d);\n }\n }\n#endif\n#ifndef OPENSSL_NO_MD4\n if (doit[D_MD4]) {\n for (testnum = 0; testnum < SIZE_NUM; testnum++) {\n print_message(names[D_MD4], c[D_MD4][testnum], lengths[testnum]);\n Time_F(START);\n count = run_benchmark(async_jobs, EVP_Digest_MD4_loop, loopargs);\n d = Time_F(STOP);\n print_result(D_MD4, testnum, count, d);\n }\n }\n#endif\n#ifndef OPENSSL_NO_MD5\n if (doit[D_MD5]) {\n for (testnum = 0; testnum < SIZE_NUM; testnum++) {\n print_message(names[D_MD5], c[D_MD5][testnum], lengths[testnum]);\n Time_F(START);\n count = run_benchmark(async_jobs, MD5_loop, loopargs);\n d = Time_F(STOP);\n print_result(D_MD5, testnum, count, d);\n }\n }\n#endif\n#ifndef OPENSSL_NO_MD5\n if (doit[D_HMAC]) {\n for (i = 0; i < loopargs_len; i++) {\n loopargs[i].hctx = HMAC_CTX_new();\n if (loopargs[i].hctx == NULL) {\n BIO_printf(bio_err, "HMAC malloc failure, exiting...");\n exit(1);\n }\n HMAC_Init_ex(loopargs[i].hctx, (unsigned char *)"This is a key...",\n 16, EVP_md5(), NULL);\n }\n for (testnum = 0; testnum < SIZE_NUM; testnum++) {\n print_message(names[D_HMAC], c[D_HMAC][testnum], lengths[testnum]);\n Time_F(START);\n count = run_benchmark(async_jobs, HMAC_loop, loopargs);\n d = Time_F(STOP);\n print_result(D_HMAC, testnum, count, d);\n }\n for (i = 0; i < loopargs_len; i++) {\n HMAC_CTX_free(loopargs[i].hctx);\n }\n }\n#endif\n if (doit[D_SHA1]) {\n for (testnum = 0; testnum < SIZE_NUM; testnum++) {\n print_message(names[D_SHA1], c[D_SHA1][testnum], lengths[testnum]);\n Time_F(START);\n count = run_benchmark(async_jobs, SHA1_loop, loopargs);\n d = Time_F(STOP);\n print_result(D_SHA1, testnum, count, d);\n }\n }\n if (doit[D_SHA256]) {\n for (testnum = 0; testnum < SIZE_NUM; testnum++) {\n print_message(names[D_SHA256], c[D_SHA256][testnum], lengths[testnum]);\n Time_F(START);\n count = run_benchmark(async_jobs, SHA256_loop, loopargs);\n d = Time_F(STOP);\n print_result(D_SHA256, testnum, count, d);\n }\n }\n if (doit[D_SHA512]) {\n for (testnum = 0; testnum < SIZE_NUM; testnum++) {\n print_message(names[D_SHA512], c[D_SHA512][testnum], lengths[testnum]);\n Time_F(START);\n count = run_benchmark(async_jobs, SHA512_loop, loopargs);\n d = Time_F(STOP);\n print_result(D_SHA512, testnum, count, d);\n }\n }\n#ifndef OPENSSL_NO_WHIRLPOOL\n if (doit[D_WHIRLPOOL]) {\n for (testnum = 0; testnum < SIZE_NUM; testnum++) {\n print_message(names[D_WHIRLPOOL], c[D_WHIRLPOOL][testnum], lengths[testnum]);\n Time_F(START);\n count = run_benchmark(async_jobs, WHIRLPOOL_loop, loopargs);\n d = Time_F(STOP);\n print_result(D_WHIRLPOOL, testnum, count, d);\n }\n }\n#endif\n#ifndef OPENSSL_NO_RMD160\n if (doit[D_RMD160]) {\n for (testnum = 0; testnum < SIZE_NUM; testnum++) {\n print_message(names[D_RMD160], c[D_RMD160][testnum], lengths[testnum]);\n Time_F(START);\n count = run_benchmark(async_jobs, EVP_Digest_RMD160_loop, loopargs);\n d = Time_F(STOP);\n print_result(D_RMD160, testnum, count, d);\n }\n }\n#endif\n#ifndef OPENSSL_NO_RC4\n if (doit[D_RC4]) {\n for (testnum = 0; testnum < SIZE_NUM; testnum++) {\n print_message(names[D_RC4], c[D_RC4][testnum], lengths[testnum]);\n Time_F(START);\n count = run_benchmark(async_jobs, RC4_loop, loopargs);\n d = Time_F(STOP);\n print_result(D_RC4, testnum, count, d);\n }\n }\n#endif\n#ifndef OPENSSL_NO_DES\n if (doit[D_CBC_DES]) {\n for (testnum = 0; testnum < SIZE_NUM; testnum++) {\n print_message(names[D_CBC_DES], c[D_CBC_DES][testnum], lengths[testnum]);\n Time_F(START);\n count = run_benchmark(async_jobs, DES_ncbc_encrypt_loop, loopargs);\n d = Time_F(STOP);\n print_result(D_CBC_DES, testnum, count, d);\n }\n }\n if (doit[D_EDE3_DES]) {\n for (testnum = 0; testnum < SIZE_NUM; testnum++) {\n print_message(names[D_EDE3_DES], c[D_EDE3_DES][testnum], lengths[testnum]);\n Time_F(START);\n count = run_benchmark(async_jobs, DES_ede3_cbc_encrypt_loop, loopargs);\n d = Time_F(STOP);\n print_result(D_EDE3_DES, testnum, count, d);\n }\n }\n#endif\n#ifndef OPENSSL_NO_AES\n if (doit[D_CBC_128_AES]) {\n for (testnum = 0; testnum < SIZE_NUM; testnum++) {\n print_message(names[D_CBC_128_AES], c[D_CBC_128_AES][testnum],\n lengths[testnum]);\n Time_F(START);\n count = run_benchmark(async_jobs, AES_cbc_128_encrypt_loop, loopargs);\n d = Time_F(STOP);\n print_result(D_CBC_128_AES, testnum, count, d);\n }\n }\n if (doit[D_CBC_192_AES]) {\n for (testnum = 0; testnum < SIZE_NUM; testnum++) {\n print_message(names[D_CBC_192_AES], c[D_CBC_192_AES][testnum],\n lengths[testnum]);\n Time_F(START);\n count = run_benchmark(async_jobs, AES_cbc_192_encrypt_loop, loopargs);\n d = Time_F(STOP);\n print_result(D_CBC_192_AES, testnum, count, d);\n }\n }\n if (doit[D_CBC_256_AES]) {\n for (testnum = 0; testnum < SIZE_NUM; testnum++) {\n print_message(names[D_CBC_256_AES], c[D_CBC_256_AES][testnum],\n lengths[testnum]);\n Time_F(START);\n count = run_benchmark(async_jobs, AES_cbc_256_encrypt_loop, loopargs);\n d = Time_F(STOP);\n print_result(D_CBC_256_AES, testnum, count, d);\n }\n }\n if (doit[D_IGE_128_AES]) {\n for (testnum = 0; testnum < SIZE_NUM; testnum++) {\n print_message(names[D_IGE_128_AES], c[D_IGE_128_AES][testnum],\n lengths[testnum]);\n Time_F(START);\n count = run_benchmark(async_jobs, AES_ige_128_encrypt_loop, loopargs);\n d = Time_F(STOP);\n print_result(D_IGE_128_AES, testnum, count, d);\n }\n }\n if (doit[D_IGE_192_AES]) {\n for (testnum = 0; testnum < SIZE_NUM; testnum++) {\n print_message(names[D_IGE_192_AES], c[D_IGE_192_AES][testnum],\n lengths[testnum]);\n Time_F(START);\n count = run_benchmark(async_jobs, AES_ige_192_encrypt_loop, loopargs);\n d = Time_F(STOP);\n print_result(D_IGE_192_AES, testnum, count, d);\n }\n }\n if (doit[D_IGE_256_AES]) {\n for (testnum = 0; testnum < SIZE_NUM; testnum++) {\n print_message(names[D_IGE_256_AES], c[D_IGE_256_AES][testnum],\n lengths[testnum]);\n Time_F(START);\n count = run_benchmark(async_jobs, AES_ige_256_encrypt_loop, loopargs);\n d = Time_F(STOP);\n print_result(D_IGE_256_AES, testnum, count, d);\n }\n }\n if (doit[D_GHASH]) {\n for (i = 0; i < loopargs_len; i++) {\n loopargs[i].gcm_ctx = CRYPTO_gcm128_new(&aes_ks1, (block128_f) AES_encrypt);\n CRYPTO_gcm128_setiv(loopargs[i].gcm_ctx, (unsigned char *)"0123456789ab", 12);\n }\n for (testnum = 0; testnum < SIZE_NUM; testnum++) {\n print_message(names[D_GHASH], c[D_GHASH][testnum], lengths[testnum]);\n Time_F(START);\n count = run_benchmark(async_jobs, CRYPTO_gcm128_aad_loop, loopargs);\n d = Time_F(STOP);\n print_result(D_GHASH, testnum, count, d);\n }\n for (i = 0; i < loopargs_len; i++)\n CRYPTO_gcm128_release(loopargs[i].gcm_ctx);\n }\n#endif\n#ifndef OPENSSL_NO_CAMELLIA\n if (doit[D_CBC_128_CML]) {\n for (testnum = 0; testnum < SIZE_NUM; testnum++) {\n print_message(names[D_CBC_128_CML], c[D_CBC_128_CML][testnum],\n lengths[testnum]);\n if (async_jobs > 0) {\n BIO_printf(bio_err, "Async mode is not supported, exiting...");\n exit(1);\n }\n Time_F(START);\n for (count = 0, run = 1; COND(c[D_CBC_128_CML][testnum]); count++)\n Camellia_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,\n (unsigned long)lengths[testnum], &camellia_ks1,\n iv, CAMELLIA_ENCRYPT);\n d = Time_F(STOP);\n print_result(D_CBC_128_CML, testnum, count, d);\n }\n }\n if (doit[D_CBC_192_CML]) {\n for (testnum = 0; testnum < SIZE_NUM; testnum++) {\n print_message(names[D_CBC_192_CML], c[D_CBC_192_CML][testnum],\n lengths[testnum]);\n if (async_jobs > 0) {\n BIO_printf(bio_err, "Async mode is not supported, exiting...");\n exit(1);\n }\n Time_F(START);\n for (count = 0, run = 1; COND(c[D_CBC_192_CML][testnum]); count++)\n Camellia_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,\n (unsigned long)lengths[testnum], &camellia_ks2,\n iv, CAMELLIA_ENCRYPT);\n d = Time_F(STOP);\n print_result(D_CBC_192_CML, testnum, count, d);\n }\n }\n if (doit[D_CBC_256_CML]) {\n for (testnum = 0; testnum < SIZE_NUM; testnum++) {\n print_message(names[D_CBC_256_CML], c[D_CBC_256_CML][testnum],\n lengths[testnum]);\n if (async_jobs > 0) {\n BIO_printf(bio_err, "Async mode is not supported, exiting...");\n exit(1);\n }\n Time_F(START);\n for (count = 0, run = 1; COND(c[D_CBC_256_CML][testnum]); count++)\n Camellia_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,\n (unsigned long)lengths[testnum], &camellia_ks3,\n iv, CAMELLIA_ENCRYPT);\n d = Time_F(STOP);\n print_result(D_CBC_256_CML, testnum, count, d);\n }\n }\n#endif\n#ifndef OPENSSL_NO_IDEA\n if (doit[D_CBC_IDEA]) {\n for (testnum = 0; testnum < SIZE_NUM; testnum++) {\n print_message(names[D_CBC_IDEA], c[D_CBC_IDEA][testnum], lengths[testnum]);\n if (async_jobs > 0) {\n BIO_printf(bio_err, "Async mode is not supported, exiting...");\n exit(1);\n }\n Time_F(START);\n for (count = 0, run = 1; COND(c[D_CBC_IDEA][testnum]); count++)\n idea_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,\n (unsigned long)lengths[testnum], &idea_ks,\n iv, IDEA_ENCRYPT);\n d = Time_F(STOP);\n print_result(D_CBC_IDEA, testnum, count, d);\n }\n }\n#endif\n#ifndef OPENSSL_NO_SEED\n if (doit[D_CBC_SEED]) {\n for (testnum = 0; testnum < SIZE_NUM; testnum++) {\n print_message(names[D_CBC_SEED], c[D_CBC_SEED][testnum], lengths[testnum]);\n if (async_jobs > 0) {\n BIO_printf(bio_err, "Async mode is not supported, exiting...");\n exit(1);\n }\n Time_F(START);\n for (count = 0, run = 1; COND(c[D_CBC_SEED][testnum]); count++)\n SEED_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,\n (unsigned long)lengths[testnum], &seed_ks, iv, 1);\n d = Time_F(STOP);\n print_result(D_CBC_SEED, testnum, count, d);\n }\n }\n#endif\n#ifndef OPENSSL_NO_RC2\n if (doit[D_CBC_RC2]) {\n for (testnum = 0; testnum < SIZE_NUM; testnum++) {\n print_message(names[D_CBC_RC2], c[D_CBC_RC2][testnum], lengths[testnum]);\n if (async_jobs > 0) {\n BIO_printf(bio_err, "Async mode is not supported, exiting...");\n exit(1);\n }\n Time_F(START);\n for (count = 0, run = 1; COND(c[D_CBC_RC2][testnum]); count++)\n RC2_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,\n (unsigned long)lengths[testnum], &rc2_ks,\n iv, RC2_ENCRYPT);\n d = Time_F(STOP);\n print_result(D_CBC_RC2, testnum, count, d);\n }\n }\n#endif\n#ifndef OPENSSL_NO_RC5\n if (doit[D_CBC_RC5]) {\n for (testnum = 0; testnum < SIZE_NUM; testnum++) {\n print_message(names[D_CBC_RC5], c[D_CBC_RC5][testnum], lengths[testnum]);\n if (async_jobs > 0) {\n BIO_printf(bio_err, "Async mode is not supported, exiting...");\n exit(1);\n }\n Time_F(START);\n for (count = 0, run = 1; COND(c[D_CBC_RC5][testnum]); count++)\n RC5_32_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,\n (unsigned long)lengths[testnum], &rc5_ks,\n iv, RC5_ENCRYPT);\n d = Time_F(STOP);\n print_result(D_CBC_RC5, testnum, count, d);\n }\n }\n#endif\n#ifndef OPENSSL_NO_BF\n if (doit[D_CBC_BF]) {\n for (testnum = 0; testnum < SIZE_NUM; testnum++) {\n print_message(names[D_CBC_BF], c[D_CBC_BF][testnum], lengths[testnum]);\n if (async_jobs > 0) {\n BIO_printf(bio_err, "Async mode is not supported, exiting...");\n exit(1);\n }\n Time_F(START);\n for (count = 0, run = 1; COND(c[D_CBC_BF][testnum]); count++)\n BF_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,\n (unsigned long)lengths[testnum], &bf_ks,\n iv, BF_ENCRYPT);\n d = Time_F(STOP);\n print_result(D_CBC_BF, testnum, count, d);\n }\n }\n#endif\n#ifndef OPENSSL_NO_CAST\n if (doit[D_CBC_CAST]) {\n for (testnum = 0; testnum < SIZE_NUM; testnum++) {\n print_message(names[D_CBC_CAST], c[D_CBC_CAST][testnum], lengths[testnum]);\n if (async_jobs > 0) {\n BIO_printf(bio_err, "Async mode is not supported, exiting...");\n exit(1);\n }\n Time_F(START);\n for (count = 0, run = 1; COND(c[D_CBC_CAST][testnum]); count++)\n CAST_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,\n (unsigned long)lengths[testnum], &cast_ks,\n iv, CAST_ENCRYPT);\n d = Time_F(STOP);\n print_result(D_CBC_CAST, testnum, count, d);\n }\n }\n#endif\n if (doit[D_EVP]) {\n#ifdef EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK\n if (multiblock && evp_cipher) {\n if (!\n (EVP_CIPHER_flags(evp_cipher) &\n EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK)) {\n BIO_printf(bio_err, "%s is not multi-block capable\\n",\n OBJ_nid2ln(EVP_CIPHER_nid(evp_cipher)));\n goto end;\n }\n if (async_jobs > 0) {\n BIO_printf(bio_err, "Async mode is not supported, exiting...");\n exit(1);\n }\n multiblock_speed(evp_cipher);\n ret = 0;\n goto end;\n }\n#endif\n for (testnum = 0; testnum < SIZE_NUM; testnum++) {\n if (evp_cipher) {\n names[D_EVP] = OBJ_nid2ln(EVP_CIPHER_nid(evp_cipher));\n print_message(names[D_EVP], save_count, lengths[testnum]);\n for (k = 0; k < loopargs_len; k++) {\n loopargs[k].ctx = EVP_CIPHER_CTX_new();\n if (decrypt)\n EVP_DecryptInit_ex(loopargs[k].ctx, evp_cipher, NULL, key16, iv);\n else\n EVP_EncryptInit_ex(loopargs[k].ctx, evp_cipher, NULL, key16, iv);\n EVP_CIPHER_CTX_set_padding(loopargs[k].ctx, 0);\n }\n Time_F(START);\n count = run_benchmark(async_jobs, EVP_Update_loop, loopargs);\n d = Time_F(STOP);\n for (k = 0; k < loopargs_len; k++) {\n EVP_CIPHER_CTX_free(loopargs[k].ctx);\n }\n }\n if (evp_md) {\n names[D_EVP] = OBJ_nid2ln(EVP_MD_type(evp_md));\n print_message(names[D_EVP], save_count, lengths[testnum]);\n Time_F(START);\n count = run_benchmark(async_jobs, EVP_Digest_loop, loopargs);\n d = Time_F(STOP);\n }\n print_result(D_EVP, testnum, count, d);\n }\n }\n for (i = 0; i < loopargs_len; i++)\n RAND_bytes(loopargs[i].buf, 36);\n#ifndef OPENSSL_NO_RSA\n for (testnum = 0; testnum < RSA_NUM; testnum++) {\n int st = 0;\n if (!rsa_doit[testnum])\n continue;\n for (i = 0; i < loopargs_len; i++) {\n st = RSA_sign(NID_md5_sha1, loopargs[i].buf, 36, loopargs[i].buf2,\n loopargs[i].siglen, loopargs[i].rsa_key[testnum]);\n if (st == 0)\n break;\n }\n if (st == 0) {\n BIO_printf(bio_err,\n "RSA sign failure. No RSA sign will be done.\\n");\n ERR_print_errors(bio_err);\n rsa_count = 1;\n } else {\n pkey_print_message("private", "rsa",\n rsa_c[testnum][0], rsa_bits[testnum], RSA_SECONDS);\n Time_F(START);\n count = run_benchmark(async_jobs, RSA_sign_loop, loopargs);\n d = Time_F(STOP);\n BIO_printf(bio_err,\n mr ? "+R1:%ld:%d:%.2f\\n"\n : "%ld %d bit private RSA\'s in %.2fs\\n",\n count, rsa_bits[testnum], d);\n rsa_results[testnum][0] = d / (double)count;\n rsa_count = count;\n }\n for (i = 0; i < loopargs_len; i++) {\n st = RSA_verify(NID_md5_sha1, loopargs[i].buf, 36, loopargs[i].buf2,\n *(loopargs[i].siglen), loopargs[i].rsa_key[testnum]);\n if (st <= 0)\n break;\n }\n if (st <= 0) {\n BIO_printf(bio_err,\n "RSA verify failure. No RSA verify will be done.\\n");\n ERR_print_errors(bio_err);\n rsa_doit[testnum] = 0;\n } else {\n pkey_print_message("public", "rsa",\n rsa_c[testnum][1], rsa_bits[testnum], RSA_SECONDS);\n Time_F(START);\n count = run_benchmark(async_jobs, RSA_verify_loop, loopargs);\n d = Time_F(STOP);\n BIO_printf(bio_err,\n mr ? "+R2:%ld:%d:%.2f\\n"\n : "%ld %d bit public RSA\'s in %.2fs\\n",\n count, rsa_bits[testnum], d);\n rsa_results[testnum][1] = d / (double)count;\n }\n if (rsa_count <= 1) {\n for (testnum++; testnum < RSA_NUM; testnum++)\n rsa_doit[testnum] = 0;\n }\n }\n#endif\n for (i = 0; i < loopargs_len; i++)\n RAND_bytes(loopargs[i].buf, 36);\n#ifndef OPENSSL_NO_DSA\n if (RAND_status() != 1) {\n RAND_seed(rnd_seed, sizeof rnd_seed);\n rnd_fake = 1;\n }\n for (testnum = 0; testnum < DSA_NUM; testnum++) {\n int st = 0;\n if (!dsa_doit[testnum])\n continue;\n for (i = 0; i < loopargs_len; i++) {\n st = DSA_sign(0, loopargs[i].buf, 20, loopargs[i].buf2,\n loopargs[i].siglen, loopargs[i].dsa_key[testnum]);\n if (st == 0)\n break;\n }\n if (st == 0) {\n BIO_printf(bio_err,\n "DSA sign failure. No DSA sign will be done.\\n");\n ERR_print_errors(bio_err);\n rsa_count = 1;\n } else {\n pkey_print_message("sign", "dsa",\n dsa_c[testnum][0], dsa_bits[testnum], DSA_SECONDS);\n Time_F(START);\n count = run_benchmark(async_jobs, DSA_sign_loop, loopargs);\n d = Time_F(STOP);\n BIO_printf(bio_err,\n mr ? "+R3:%ld:%d:%.2f\\n"\n : "%ld %d bit DSA signs in %.2fs\\n",\n count, dsa_bits[testnum], d);\n dsa_results[testnum][0] = d / (double)count;\n rsa_count = count;\n }\n for (i = 0; i < loopargs_len; i++) {\n st = DSA_verify(0, loopargs[i].buf, 20, loopargs[i].buf2,\n *(loopargs[i].siglen), loopargs[i].dsa_key[testnum]);\n if (st <= 0)\n break;\n }\n if (st <= 0) {\n BIO_printf(bio_err,\n "DSA verify failure. No DSA verify will be done.\\n");\n ERR_print_errors(bio_err);\n dsa_doit[testnum] = 0;\n } else {\n pkey_print_message("verify", "dsa",\n dsa_c[testnum][1], dsa_bits[testnum], DSA_SECONDS);\n Time_F(START);\n count = run_benchmark(async_jobs, DSA_verify_loop, loopargs);\n d = Time_F(STOP);\n BIO_printf(bio_err,\n mr ? "+R4:%ld:%d:%.2f\\n"\n : "%ld %d bit DSA verify in %.2fs\\n",\n count, dsa_bits[testnum], d);\n dsa_results[testnum][1] = d / (double)count;\n }\n if (rsa_count <= 1) {\n for (testnum++; testnum < DSA_NUM; testnum++)\n dsa_doit[testnum] = 0;\n }\n }\n if (rnd_fake)\n RAND_cleanup();\n#endif\n#ifndef OPENSSL_NO_EC\n if (RAND_status() != 1) {\n RAND_seed(rnd_seed, sizeof rnd_seed);\n rnd_fake = 1;\n }\n for (testnum = 0; testnum < EC_NUM; testnum++) {\n int st = 1;\n if (!ecdsa_doit[testnum])\n continue;\n for (i = 0; i < loopargs_len; i++) {\n loopargs[i].ecdsa[testnum] = EC_KEY_new_by_curve_name(test_curves[testnum]);\n if (loopargs[i].ecdsa[testnum] == NULL) {\n st = 0;\n break;\n }\n }\n if (st == 0) {\n BIO_printf(bio_err, "ECDSA failure.\\n");\n ERR_print_errors(bio_err);\n rsa_count = 1;\n } else {\n for (i = 0; i < loopargs_len; i++) {\n EC_KEY_precompute_mult(loopargs[i].ecdsa[testnum], NULL);\n EC_KEY_generate_key(loopargs[i].ecdsa[testnum]);\n st = ECDSA_sign(0, loopargs[i].buf, 20, loopargs[i].buf2,\n loopargs[i].siglen, loopargs[i].ecdsa[testnum]);\n if (st == 0)\n break;\n }\n if (st == 0) {\n BIO_printf(bio_err,\n "ECDSA sign failure. No ECDSA sign will be done.\\n");\n ERR_print_errors(bio_err);\n rsa_count = 1;\n } else {\n pkey_print_message("sign", "ecdsa",\n ecdsa_c[testnum][0],\n test_curves_bits[testnum], ECDSA_SECONDS);\n Time_F(START);\n count = run_benchmark(async_jobs, ECDSA_sign_loop, loopargs);\n d = Time_F(STOP);\n BIO_printf(bio_err,\n mr ? "+R5:%ld:%d:%.2f\\n" :\n "%ld %d bit ECDSA signs in %.2fs \\n",\n count, test_curves_bits[testnum], d);\n ecdsa_results[testnum][0] = d / (double)count;\n rsa_count = count;\n }\n for (i = 0; i < loopargs_len; i++) {\n st = ECDSA_verify(0, loopargs[i].buf, 20, loopargs[i].buf2,\n *(loopargs[i].siglen), loopargs[i].ecdsa[testnum]);\n if (st != 1)\n break;\n }\n if (st != 1) {\n BIO_printf(bio_err,\n "ECDSA verify failure. No ECDSA verify will be done.\\n");\n ERR_print_errors(bio_err);\n ecdsa_doit[testnum] = 0;\n } else {\n pkey_print_message("verify", "ecdsa",\n ecdsa_c[testnum][1],\n test_curves_bits[testnum], ECDSA_SECONDS);\n Time_F(START);\n count = run_benchmark(async_jobs, ECDSA_verify_loop, loopargs);\n d = Time_F(STOP);\n BIO_printf(bio_err,\n mr ? "+R6:%ld:%d:%.2f\\n"\n : "%ld %d bit ECDSA verify in %.2fs\\n",\n count, test_curves_bits[testnum], d);\n ecdsa_results[testnum][1] = d / (double)count;\n }\n if (rsa_count <= 1) {\n for (testnum++; testnum < EC_NUM; testnum++)\n ecdsa_doit[testnum] = 0;\n }\n }\n }\n if (rnd_fake)\n RAND_cleanup();\n#endif\n#ifndef OPENSSL_NO_EC\n if (RAND_status() != 1) {\n RAND_seed(rnd_seed, sizeof rnd_seed);\n rnd_fake = 1;\n }\n for (testnum = 0; testnum < EC_NUM; testnum++) {\n if (!ecdh_doit[testnum])\n continue;\n for (i = 0; i < loopargs_len; i++) {\n loopargs[i].ecdh_a[testnum] = EC_KEY_new_by_curve_name(test_curves[testnum]);\n loopargs[i].ecdh_b[testnum] = EC_KEY_new_by_curve_name(test_curves[testnum]);\n if (loopargs[i].ecdh_a[testnum] == NULL ||\n loopargs[i].ecdh_b[testnum] == NULL) {\n ecdh_checks = 0;\n break;\n }\n }\n if (ecdh_checks == 0) {\n BIO_printf(bio_err, "ECDH failure.\\n");\n ERR_print_errors(bio_err);\n rsa_count = 1;\n } else {\n for (i = 0; i < loopargs_len; i++) {\n if (!EC_KEY_generate_key(loopargs[i].ecdh_a[testnum]) ||\n !EC_KEY_generate_key(loopargs[i].ecdh_b[testnum])) {\n BIO_printf(bio_err, "ECDH key generation failure.\\n");\n ERR_print_errors(bio_err);\n ecdh_checks = 0;\n rsa_count = 1;\n } else {\n int field_size;\n field_size =\n EC_GROUP_get_degree(EC_KEY_get0_group(loopargs[i].ecdh_a[testnum]));\n if (field_size <= 24 * 8) {\n outlen = KDF1_SHA1_len;\n kdf = KDF1_SHA1;\n } else {\n outlen = (field_size + 7) / 8;\n kdf = NULL;\n }\n secret_size_a =\n ECDH_compute_key(loopargs[i].secret_a, outlen,\n EC_KEY_get0_public_key(loopargs[i].ecdh_b[testnum]),\n loopargs[i].ecdh_a[testnum], kdf);\n secret_size_b =\n ECDH_compute_key(loopargs[i].secret_b, outlen,\n EC_KEY_get0_public_key(loopargs[i].ecdh_a[testnum]),\n loopargs[i].ecdh_b[testnum], kdf);\n if (secret_size_a != secret_size_b)\n ecdh_checks = 0;\n else\n ecdh_checks = 1;\n for (secret_idx = 0; (secret_idx < secret_size_a)\n && (ecdh_checks == 1); secret_idx++) {\n if (loopargs[i].secret_a[secret_idx] != loopargs[i].secret_b[secret_idx])\n ecdh_checks = 0;\n }\n if (ecdh_checks == 0) {\n BIO_printf(bio_err, "ECDH computations don\'t match.\\n");\n ERR_print_errors(bio_err);\n rsa_count = 1;\n break;\n }\n }\n if (ecdh_checks != 0) {\n pkey_print_message("", "ecdh",\n ecdh_c[testnum][0],\n test_curves_bits[testnum], ECDH_SECONDS);\n Time_F(START);\n count = run_benchmark(async_jobs, ECDH_compute_key_loop, loopargs);\n d = Time_F(STOP);\n BIO_printf(bio_err,\n mr ? "+R7:%ld:%d:%.2f\\n" :\n "%ld %d-bit ECDH ops in %.2fs\\n", count,\n test_curves_bits[testnum], d);\n ecdh_results[testnum][0] = d / (double)count;\n rsa_count = count;\n }\n }\n }\n if (rsa_count <= 1) {\n for (testnum++; testnum < EC_NUM; testnum++)\n ecdh_doit[testnum] = 0;\n }\n }\n if (rnd_fake)\n RAND_cleanup();\n#endif\n#ifndef NO_FORK\n show_res:\n#endif\n if (!mr) {\n printf("%s\\n", OpenSSL_version(OPENSSL_VERSION));\n printf("%s\\n", OpenSSL_version(OPENSSL_BUILT_ON));\n printf("options:");\n printf("%s ", BN_options());\n#ifndef OPENSSL_NO_MD2\n printf("%s ", MD2_options());\n#endif\n#ifndef OPENSSL_NO_RC4\n printf("%s ", RC4_options());\n#endif\n#ifndef OPENSSL_NO_DES\n printf("%s ", DES_options());\n#endif\n#ifndef OPENSSL_NO_AES\n printf("%s ", AES_options());\n#endif\n#ifndef OPENSSL_NO_IDEA\n printf("%s ", idea_options());\n#endif\n#ifndef OPENSSL_NO_BF\n printf("%s ", BF_options());\n#endif\n printf("\\n%s\\n", OpenSSL_version(OPENSSL_CFLAGS));\n }\n if (pr_header) {\n if (mr)\n printf("+H");\n else {\n printf\n ("The \'numbers\' are in 1000s of bytes per second processed.\\n");\n printf("type ");\n }\n for (testnum = 0; testnum < SIZE_NUM; testnum++)\n printf(mr ? ":%d" : "%7d bytes", lengths[testnum]);\n printf("\\n");\n }\n for (k = 0; k < ALGOR_NUM; k++) {\n if (!doit[k])\n continue;\n if (mr)\n printf("+F:%d:%s", k, names[k]);\n else\n printf("%-13s", names[k]);\n for (testnum = 0; testnum < SIZE_NUM; testnum++) {\n if (results[k][testnum] > 10000 && !mr)\n printf(" %11.2fk", results[k][testnum] / 1e3);\n else\n printf(mr ? ":%.2f" : " %11.2f ", results[k][testnum]);\n }\n printf("\\n");\n }\n#ifndef OPENSSL_NO_RSA\n testnum = 1;\n for (k = 0; k < RSA_NUM; k++) {\n if (!rsa_doit[k])\n continue;\n if (testnum && !mr) {\n printf("%18ssign verify sign/s verify/s\\n", " ");\n testnum = 0;\n }\n if (mr)\n printf("+F2:%u:%u:%f:%f\\n",\n k, rsa_bits[k], rsa_results[k][0], rsa_results[k][1]);\n else\n printf("rsa %4u bits %8.6fs %8.6fs %8.1f %8.1f\\n",\n rsa_bits[k], rsa_results[k][0], rsa_results[k][1],\n 1.0 / rsa_results[k][0], 1.0 / rsa_results[k][1]);\n }\n#endif\n#ifndef OPENSSL_NO_DSA\n testnum = 1;\n for (k = 0; k < DSA_NUM; k++) {\n if (!dsa_doit[k])\n continue;\n if (testnum && !mr) {\n printf("%18ssign verify sign/s verify/s\\n", " ");\n testnum = 0;\n }\n if (mr)\n printf("+F3:%u:%u:%f:%f\\n",\n k, dsa_bits[k], dsa_results[k][0], dsa_results[k][1]);\n else\n printf("dsa %4u bits %8.6fs %8.6fs %8.1f %8.1f\\n",\n dsa_bits[k], dsa_results[k][0], dsa_results[k][1],\n 1.0 / dsa_results[k][0], 1.0 / dsa_results[k][1]);\n }\n#endif\n#ifndef OPENSSL_NO_EC\n testnum = 1;\n for (k = 0; k < EC_NUM; k++) {\n if (!ecdsa_doit[k])\n continue;\n if (testnum && !mr) {\n printf("%30ssign verify sign/s verify/s\\n", " ");\n testnum = 0;\n }\n if (mr)\n printf("+F4:%u:%u:%f:%f\\n",\n k, test_curves_bits[k],\n ecdsa_results[k][0], ecdsa_results[k][1]);\n else\n printf("%4u bit ecdsa (%s) %8.4fs %8.4fs %8.1f %8.1f\\n",\n test_curves_bits[k],\n test_curves_names[k],\n ecdsa_results[k][0], ecdsa_results[k][1],\n 1.0 / ecdsa_results[k][0], 1.0 / ecdsa_results[k][1]);\n }\n#endif\n#ifndef OPENSSL_NO_EC\n testnum = 1;\n for (k = 0; k < EC_NUM; k++) {\n if (!ecdh_doit[k])\n continue;\n if (testnum && !mr) {\n printf("%30sop op/s\\n", " ");\n testnum = 0;\n }\n if (mr)\n printf("+F5:%u:%u:%f:%f\\n",\n k, test_curves_bits[k],\n ecdh_results[k][0], 1.0 / ecdh_results[k][0]);\n else\n printf("%4u bit ecdh (%s) %8.4fs %8.1f\\n",\n test_curves_bits[k],\n test_curves_names[k],\n ecdh_results[k][0], 1.0 / ecdh_results[k][0]);\n }\n#endif\n ret = 0;\n end:\n ERR_print_errors(bio_err);\n for (i = 0; i < loopargs_len; i++) {\n OPENSSL_free(loopargs[i].buf_malloc);\n OPENSSL_free(loopargs[i].buf2_malloc);\n OPENSSL_free(loopargs[i].siglen);\n }\n#ifndef OPENSSL_NO_RSA\n for (i = 0; i < loopargs_len; i++) {\n for (k = 0; k < RSA_NUM; k++)\n RSA_free(loopargs[i].rsa_key[k]);\n }\n#endif\n#ifndef OPENSSL_NO_DSA\n for (i = 0; i < loopargs_len; i++) {\n for (k = 0; k < DSA_NUM; k++)\n DSA_free(loopargs[i].dsa_key[k]);\n }\n#endif\n#ifndef OPENSSL_NO_EC\n for (i = 0; i < loopargs_len; i++) {\n for (k = 0; k < EC_NUM; k++) {\n EC_KEY_free(loopargs[i].ecdsa[k]);\n EC_KEY_free(loopargs[i].ecdh_a[k]);\n EC_KEY_free(loopargs[i].ecdh_b[k]);\n }\n OPENSSL_free(loopargs[i].secret_a);\n OPENSSL_free(loopargs[i].secret_b);\n }\n#endif\n if (async_jobs > 0) {\n for (i = 0; i < loopargs_len; i++)\n ASYNC_WAIT_CTX_free(loopargs[i].wait_ctx);\n ASYNC_cleanup_thread();\n }\n OPENSSL_free(loopargs);\n return (ret);\n}'] |
35,163 | 0 | https://github.com/openssl/openssl/blob/acc00492130d53d2d6a25bbe5409240aeba98420/apps/s_server.c/#L536 | static int cert_status_cb(SSL *s, void *arg)
{
tlsextstatusctx *srctx = arg;
char *host = NULL, *port = NULL, *path = NULL;
int use_ssl;
unsigned char *rspder = NULL;
int rspderlen;
STACK_OF(OPENSSL_STRING) *aia = NULL;
X509 *x = NULL;
X509_STORE_CTX *inctx = NULL;
X509_OBJECT *obj;
OCSP_REQUEST *req = NULL;
OCSP_RESPONSE *resp = NULL;
OCSP_CERTID *id = NULL;
STACK_OF(X509_EXTENSION) *exts;
int ret = SSL_TLSEXT_ERR_NOACK;
int i;
if (srctx->verbose)
BIO_puts(bio_err, "cert_status: callback called\n");
x = SSL_get_certificate(s);
aia = X509_get1_ocsp(x);
if (aia) {
if (!OCSP_parse_url(sk_OPENSSL_STRING_value(aia, 0),
&host, &port, &path, &use_ssl)) {
BIO_puts(bio_err, "cert_status: can't parse AIA URL\n");
goto err;
}
if (srctx->verbose)
BIO_printf(bio_err, "cert_status: AIA URL: %s\n",
sk_OPENSSL_STRING_value(aia, 0));
} else {
if (!srctx->host) {
BIO_puts(bio_err,
"cert_status: no AIA and no default responder URL\n");
goto done;
}
host = srctx->host;
path = srctx->path;
port = srctx->port;
use_ssl = srctx->use_ssl;
}
inctx = X509_STORE_CTX_new();
if (inctx == NULL)
goto err;
if (!X509_STORE_CTX_init(inctx,
SSL_CTX_get_cert_store(SSL_get_SSL_CTX(s)),
NULL, NULL))
goto err;
obj = X509_STORE_CTX_get_obj_by_subject(inctx, X509_LU_X509,
X509_get_issuer_name(x));
if (obj == NULL) {
BIO_puts(bio_err, "cert_status: Can't retrieve issuer certificate.\n");
goto done;
}
id = OCSP_cert_to_id(NULL, x, X509_OBJECT_get0_X509(obj));
X509_OBJECT_free(obj);
if (!id)
goto err;
req = OCSP_REQUEST_new();
if (req == NULL)
goto err;
if (!OCSP_request_add0_id(req, id))
goto err;
id = NULL;
SSL_get_tlsext_status_exts(s, &exts);
for (i = 0; i < sk_X509_EXTENSION_num(exts); i++) {
X509_EXTENSION *ext = sk_X509_EXTENSION_value(exts, i);
if (!OCSP_REQUEST_add_ext(req, ext, -1))
goto err;
}
resp = process_responder(req, host, path, port, use_ssl, NULL,
srctx->timeout);
if (!resp) {
BIO_puts(bio_err, "cert_status: error querying responder\n");
goto done;
}
rspderlen = i2d_OCSP_RESPONSE(resp, &rspder);
if (rspderlen <= 0)
goto err;
SSL_set_tlsext_status_ocsp_resp(s, rspder, rspderlen);
if (srctx->verbose) {
BIO_puts(bio_err, "cert_status: ocsp response sent:\n");
OCSP_RESPONSE_print(bio_err, resp, 2);
}
ret = SSL_TLSEXT_ERR_OK;
goto done;
err:
ret = SSL_TLSEXT_ERR_ALERT_FATAL;
done:
if (ret != SSL_TLSEXT_ERR_OK)
ERR_print_errors(bio_err);
if (aia) {
OPENSSL_free(host);
OPENSSL_free(path);
OPENSSL_free(port);
X509_email_free(aia);
}
OCSP_CERTID_free(id);
OCSP_REQUEST_free(req);
OCSP_RESPONSE_free(resp);
X509_STORE_CTX_free(inctx);
return ret;
} | ['static int cert_status_cb(SSL *s, void *arg)\n{\n tlsextstatusctx *srctx = arg;\n char *host = NULL, *port = NULL, *path = NULL;\n int use_ssl;\n unsigned char *rspder = NULL;\n int rspderlen;\n STACK_OF(OPENSSL_STRING) *aia = NULL;\n X509 *x = NULL;\n X509_STORE_CTX *inctx = NULL;\n X509_OBJECT *obj;\n OCSP_REQUEST *req = NULL;\n OCSP_RESPONSE *resp = NULL;\n OCSP_CERTID *id = NULL;\n STACK_OF(X509_EXTENSION) *exts;\n int ret = SSL_TLSEXT_ERR_NOACK;\n int i;\n if (srctx->verbose)\n BIO_puts(bio_err, "cert_status: callback called\\n");\n x = SSL_get_certificate(s);\n aia = X509_get1_ocsp(x);\n if (aia) {\n if (!OCSP_parse_url(sk_OPENSSL_STRING_value(aia, 0),\n &host, &port, &path, &use_ssl)) {\n BIO_puts(bio_err, "cert_status: can\'t parse AIA URL\\n");\n goto err;\n }\n if (srctx->verbose)\n BIO_printf(bio_err, "cert_status: AIA URL: %s\\n",\n sk_OPENSSL_STRING_value(aia, 0));\n } else {\n if (!srctx->host) {\n BIO_puts(bio_err,\n "cert_status: no AIA and no default responder URL\\n");\n goto done;\n }\n host = srctx->host;\n path = srctx->path;\n port = srctx->port;\n use_ssl = srctx->use_ssl;\n }\n inctx = X509_STORE_CTX_new();\n if (inctx == NULL)\n goto err;\n if (!X509_STORE_CTX_init(inctx,\n SSL_CTX_get_cert_store(SSL_get_SSL_CTX(s)),\n NULL, NULL))\n goto err;\n obj = X509_STORE_CTX_get_obj_by_subject(inctx, X509_LU_X509,\n X509_get_issuer_name(x));\n if (obj == NULL) {\n BIO_puts(bio_err, "cert_status: Can\'t retrieve issuer certificate.\\n");\n goto done;\n }\n id = OCSP_cert_to_id(NULL, x, X509_OBJECT_get0_X509(obj));\n X509_OBJECT_free(obj);\n if (!id)\n goto err;\n req = OCSP_REQUEST_new();\n if (req == NULL)\n goto err;\n if (!OCSP_request_add0_id(req, id))\n goto err;\n id = NULL;\n SSL_get_tlsext_status_exts(s, &exts);\n for (i = 0; i < sk_X509_EXTENSION_num(exts); i++) {\n X509_EXTENSION *ext = sk_X509_EXTENSION_value(exts, i);\n if (!OCSP_REQUEST_add_ext(req, ext, -1))\n goto err;\n }\n resp = process_responder(req, host, path, port, use_ssl, NULL,\n srctx->timeout);\n if (!resp) {\n BIO_puts(bio_err, "cert_status: error querying responder\\n");\n goto done;\n }\n rspderlen = i2d_OCSP_RESPONSE(resp, &rspder);\n if (rspderlen <= 0)\n goto err;\n SSL_set_tlsext_status_ocsp_resp(s, rspder, rspderlen);\n if (srctx->verbose) {\n BIO_puts(bio_err, "cert_status: ocsp response sent:\\n");\n OCSP_RESPONSE_print(bio_err, resp, 2);\n }\n ret = SSL_TLSEXT_ERR_OK;\n goto done;\n err:\n ret = SSL_TLSEXT_ERR_ALERT_FATAL;\n done:\n if (ret != SSL_TLSEXT_ERR_OK)\n ERR_print_errors(bio_err);\n if (aia) {\n OPENSSL_free(host);\n OPENSSL_free(path);\n OPENSSL_free(port);\n X509_email_free(aia);\n }\n OCSP_CERTID_free(id);\n OCSP_REQUEST_free(req);\n OCSP_RESPONSE_free(resp);\n X509_STORE_CTX_free(inctx);\n return ret;\n}', 'int BIO_puts(BIO *b, const char *in)\n{\n int i;\n long (*cb) (BIO *, int, const char *, int, long, long);\n if ((b == NULL) || (b->method == NULL) || (b->method->bputs == NULL)) {\n BIOerr(BIO_F_BIO_PUTS, BIO_R_UNSUPPORTED_METHOD);\n return (-2);\n }\n cb = b->callback;\n if ((cb != NULL) && ((i = (int)cb(b, BIO_CB_PUTS, in, 0, 0L, 1L)) <= 0))\n return (i);\n if (!b->init) {\n BIOerr(BIO_F_BIO_PUTS, BIO_R_UNINITIALIZED);\n return (-2);\n }\n i = b->method->bputs(b, in);\n if (i > 0)\n b->num_write += (uint64_t)i;\n if (cb != NULL)\n i = (int)cb(b, BIO_CB_PUTS | BIO_CB_RETURN, in, 0, 0L, (long)i);\n return (i);\n}', 'X509 *SSL_get_certificate(const SSL *s)\n{\n if (s->cert != NULL)\n return (s->cert->key->x509);\n else\n return (NULL);\n}'] |
35,164 | 0 | https://github.com/libav/libav/blob/60a42ef44cc02a8b6a0f0b92ab92c6f9a4ddc838/libavformat/mpegts.c/#L678 | static int read_sl_header(PESContext *pes, SLConfigDescr *sl, const uint8_t *buf, int buf_size)
{
GetBitContext gb;
int au_start_flag = 0, au_end_flag = 0, ocr_flag = 0, idle_flag = 0;
int padding_flag = 0, padding_bits = 0, inst_bitrate_flag = 0;
int dts_flag = -1, cts_flag = -1;
int64_t dts = AV_NOPTS_VALUE, cts = AV_NOPTS_VALUE;
init_get_bits(&gb, buf, buf_size*8);
if (sl->use_au_start)
au_start_flag = get_bits1(&gb);
if (sl->use_au_end)
au_end_flag = get_bits1(&gb);
if (!sl->use_au_start && !sl->use_au_end)
au_start_flag = au_end_flag = 1;
if (sl->ocr_len > 0)
ocr_flag = get_bits1(&gb);
if (sl->use_idle)
idle_flag = get_bits1(&gb);
if (sl->use_padding)
padding_flag = get_bits1(&gb);
if (padding_flag)
padding_bits = get_bits(&gb, 3);
if (!idle_flag && (!padding_flag || padding_bits != 0)) {
if (sl->packet_seq_num_len)
skip_bits_long(&gb, sl->packet_seq_num_len);
if (sl->degr_prior_len)
if (get_bits1(&gb))
skip_bits(&gb, sl->degr_prior_len);
if (ocr_flag)
skip_bits_long(&gb, sl->ocr_len);
if (au_start_flag) {
if (sl->use_rand_acc_pt)
get_bits1(&gb);
if (sl->au_seq_num_len > 0)
skip_bits_long(&gb, sl->au_seq_num_len);
if (sl->use_timestamps) {
dts_flag = get_bits1(&gb);
cts_flag = get_bits1(&gb);
}
}
if (sl->inst_bitrate_len)
inst_bitrate_flag = get_bits1(&gb);
if (dts_flag == 1)
dts = get_bits64(&gb, sl->timestamp_len);
if (cts_flag == 1)
cts = get_bits64(&gb, sl->timestamp_len);
if (sl->au_len > 0)
skip_bits_long(&gb, sl->au_len);
if (inst_bitrate_flag)
skip_bits_long(&gb, sl->inst_bitrate_len);
}
if (dts != AV_NOPTS_VALUE)
pes->dts = dts;
if (cts != AV_NOPTS_VALUE)
pes->pts = cts;
if (sl->timestamp_len && sl->timestamp_res)
avpriv_set_pts_info(pes->st, sl->timestamp_len, 1, sl->timestamp_res);
return (get_bits_count(&gb) + 7) >> 3;
} | ['static int read_sl_header(PESContext *pes, SLConfigDescr *sl, const uint8_t *buf, int buf_size)\n{\n GetBitContext gb;\n int au_start_flag = 0, au_end_flag = 0, ocr_flag = 0, idle_flag = 0;\n int padding_flag = 0, padding_bits = 0, inst_bitrate_flag = 0;\n int dts_flag = -1, cts_flag = -1;\n int64_t dts = AV_NOPTS_VALUE, cts = AV_NOPTS_VALUE;\n init_get_bits(&gb, buf, buf_size*8);\n if (sl->use_au_start)\n au_start_flag = get_bits1(&gb);\n if (sl->use_au_end)\n au_end_flag = get_bits1(&gb);\n if (!sl->use_au_start && !sl->use_au_end)\n au_start_flag = au_end_flag = 1;\n if (sl->ocr_len > 0)\n ocr_flag = get_bits1(&gb);\n if (sl->use_idle)\n idle_flag = get_bits1(&gb);\n if (sl->use_padding)\n padding_flag = get_bits1(&gb);\n if (padding_flag)\n padding_bits = get_bits(&gb, 3);\n if (!idle_flag && (!padding_flag || padding_bits != 0)) {\n if (sl->packet_seq_num_len)\n skip_bits_long(&gb, sl->packet_seq_num_len);\n if (sl->degr_prior_len)\n if (get_bits1(&gb))\n skip_bits(&gb, sl->degr_prior_len);\n if (ocr_flag)\n skip_bits_long(&gb, sl->ocr_len);\n if (au_start_flag) {\n if (sl->use_rand_acc_pt)\n get_bits1(&gb);\n if (sl->au_seq_num_len > 0)\n skip_bits_long(&gb, sl->au_seq_num_len);\n if (sl->use_timestamps) {\n dts_flag = get_bits1(&gb);\n cts_flag = get_bits1(&gb);\n }\n }\n if (sl->inst_bitrate_len)\n inst_bitrate_flag = get_bits1(&gb);\n if (dts_flag == 1)\n dts = get_bits64(&gb, sl->timestamp_len);\n if (cts_flag == 1)\n cts = get_bits64(&gb, sl->timestamp_len);\n if (sl->au_len > 0)\n skip_bits_long(&gb, sl->au_len);\n if (inst_bitrate_flag)\n skip_bits_long(&gb, sl->inst_bitrate_len);\n }\n if (dts != AV_NOPTS_VALUE)\n pes->dts = dts;\n if (cts != AV_NOPTS_VALUE)\n pes->pts = cts;\n if (sl->timestamp_len && sl->timestamp_res)\n avpriv_set_pts_info(pes->st, sl->timestamp_len, 1, sl->timestamp_res);\n return (get_bits_count(&gb) + 7) >> 3;\n}', 'static inline int init_get_bits(GetBitContext *s, const uint8_t *buffer,\n int bit_size)\n{\n int buffer_size;\n int ret = 0;\n if (bit_size > INT_MAX - 7 || bit_size <= 0) {\n buffer_size = bit_size = 0;\n buffer = NULL;\n ret = AVERROR_INVALIDDATA;\n }\n buffer_size = (bit_size + 7) >> 3;\n s->buffer = buffer;\n s->size_in_bits = bit_size;\n#if !UNCHECKED_BITSTREAM_READER\n s->size_in_bits_plus8 = bit_size + 8;\n#endif\n s->buffer_end = buffer + buffer_size;\n s->index = 0;\n return ret;\n}', 'static inline unsigned int get_bits1(GetBitContext *s)\n{\n unsigned int index = s->index;\n uint8_t result = s->buffer[index>>3];\n#ifdef BITSTREAM_READER_LE\n result >>= index & 7;\n result &= 1;\n#else\n result <<= index & 7;\n result >>= 8 - 1;\n#endif\n#if !UNCHECKED_BITSTREAM_READER\n if (s->index < s->size_in_bits_plus8)\n#endif\n index++;\n s->index = index;\n return result;\n}'] |
35,165 | 0 | https://github.com/libav/libav/blob/366ba2dee1f2b17825b42e2164d3b9879f0271b1/libavcodec/h264_ps.c/#L645 | static void init_dequant_tables(PPS *pps, const SPS *sps)
{
int i, x;
init_dequant4_coeff_table(pps, sps);
if (pps->transform_8x8_mode)
init_dequant8_coeff_table(pps, sps);
if (sps->transform_bypass) {
for (i = 0; i < 6; i++)
for (x = 0; x < 16; x++)
pps->dequant4_coeff[i][0][x] = 1 << 6;
if (pps->transform_8x8_mode)
for (i = 0; i < 6; i++)
for (x = 0; x < 64; x++)
pps->dequant8_coeff[i][0][x] = 1 << 6;
}
} | ['static void init_dequant_tables(PPS *pps, const SPS *sps)\n{\n int i, x;\n init_dequant4_coeff_table(pps, sps);\n if (pps->transform_8x8_mode)\n init_dequant8_coeff_table(pps, sps);\n if (sps->transform_bypass) {\n for (i = 0; i < 6; i++)\n for (x = 0; x < 16; x++)\n pps->dequant4_coeff[i][0][x] = 1 << 6;\n if (pps->transform_8x8_mode)\n for (i = 0; i < 6; i++)\n for (x = 0; x < 64; x++)\n pps->dequant8_coeff[i][0][x] = 1 << 6;\n }\n}', 'static void init_dequant4_coeff_table(PPS *pps, const SPS *sps)\n{\n int i, j, q, x;\n const int max_qp = 51 + 6 * (sps->bit_depth_luma - 8);\n for (i = 0; i < 6; i++) {\n pps->dequant4_coeff[i] = pps->dequant4_buffer[i];\n for (j = 0; j < i; j++)\n if (!memcmp(pps->scaling_matrix4[j], pps->scaling_matrix4[i],\n 16 * sizeof(uint8_t))) {\n pps->dequant4_coeff[i] = pps->dequant4_buffer[j];\n break;\n }\n if (j < i)\n continue;\n for (q = 0; q < max_qp + 1; q++) {\n int shift = ff_h264_quant_div6[q] + 2;\n int idx = ff_h264_quant_rem6[q];\n for (x = 0; x < 16; x++)\n pps->dequant4_coeff[i][q][(x >> 2) | ((x << 2) & 0xF)] =\n ((uint32_t)ff_h264_dequant4_coeff_init[idx][(x & 1) + ((x >> 2) & 1)] *\n pps->scaling_matrix4[i][x]) << shift;\n }\n }\n}'] |
35,166 | 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)];
} | ['static BIGNUM *dsa_mod_inverse_fermat(const BIGNUM *k, const BIGNUM *q,\n BN_CTX *ctx)\n{\n BIGNUM *res = NULL;\n BIGNUM *r, *e;\n if ((r = BN_new()) == NULL)\n return NULL;\n BN_CTX_start(ctx);\n if ((e = BN_CTX_get(ctx)) != NULL\n && BN_set_word(r, 2)\n && BN_sub(e, q, r)\n && BN_mod_exp_mont(r, k, e, q, ctx, NULL))\n res = r;\n else\n BN_free(r);\n BN_CTX_end(ctx);\n return res;\n}', 'void BN_CTX_start(BN_CTX *ctx)\n{\n CTXDBG("ENTER BN_CTX_start()", ctx);\n if (ctx->err_stack || ctx->too_many)\n ctx->err_stack++;\n else if (!BN_STACK_push(&ctx->stack, ctx->used)) {\n BNerr(BN_F_BN_CTX_START, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n ctx->err_stack++;\n }\n CTXDBG("LEAVE BN_CTX_start()", ctx);\n}', 'void BN_CTX_end(BN_CTX *ctx)\n{\n 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}'] |
35,167 | 0 | https://github.com/openssl/openssl/blob/0dd0cbf554f0211e49daed346a6a77587c681c22/ssl/s23_srvr.c/#L508 | int ssl23_get_client_hello(SSL *s)
{
char buf_space[11];
char *buf= &(buf_space[0]);
unsigned char *p,*d,*dd;
unsigned int i;
unsigned int csl,sil,cl;
int n=0,j;
int type=0;
int v[2];
#ifndef NO_RSA
int use_sslv2_strong=0;
#endif
if (s->state == SSL23_ST_SR_CLNT_HELLO_A)
{
v[0]=v[1]=0;
if (!ssl3_setup_buffers(s)) goto err;
n=ssl23_read_bytes(s, sizeof buf_space);
if (n != sizeof buf_space) return(n);
p=s->packet;
memcpy(buf,p,n);
if ((p[0] & 0x80) && (p[2] == SSL2_MT_CLIENT_HELLO))
{
if ((p[3] == 0x00) && (p[4] == 0x02))
{
v[0]=p[3]; v[1]=p[4];
if (!(s->options & SSL_OP_NO_SSLv2))
type=1;
}
else if (p[3] == SSL3_VERSION_MAJOR)
{
v[0]=p[3]; v[1]=p[4];
if (p[4] >= TLS1_VERSION_MINOR)
{
if (!(s->options & SSL_OP_NO_TLSv1))
{
s->version=TLS1_VERSION;
s->state=SSL23_ST_SR_CLNT_HELLO_B;
}
else if (!(s->options & SSL_OP_NO_SSLv3))
{
s->version=SSL3_VERSION;
s->state=SSL23_ST_SR_CLNT_HELLO_B;
}
else if (!(s->options & SSL_OP_NO_SSLv2))
{
type=1;
}
}
else if (!(s->options & SSL_OP_NO_SSLv3))
{
s->version=SSL3_VERSION;
s->state=SSL23_ST_SR_CLNT_HELLO_B;
}
else if (!(s->options & SSL_OP_NO_SSLv2))
type=1;
if (s->options & SSL_OP_NON_EXPORT_FIRST)
{
#if 0
STACK_OF(SSL_CIPHER) *sk;
SSL_CIPHER *c;
int ne2,ne3;
j=((p[0]&0x7f)<<8)|p[1];
if (j > (1024*4))
{
SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO,SSL_R_RECORD_TOO_LARGE);
goto err;
}
n=ssl23_read_bytes(s,j+2);
if (n <= 0) return(n);
p=s->packet;
if ((buf=Malloc(n)) == NULL)
{
SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO,ERR_R_MALLOC_FAILURE);
goto err;
}
memcpy(buf,p,n);
p+=5;
n2s(p,csl);
p+=4;
sk=ssl_bytes_to_cipher_list(
s,p,csl,NULL);
if (sk != NULL)
{
ne2=ne3=0;
for (j=0; j<sk_SSL_CIPHER_num(sk); j++)
{
c=sk_SSL_CIPHER_value(sk,j);
if (!SSL_C_IS_EXPORT(c))
{
if ((c->id>>24L) == 2L)
ne2=1;
else
ne3=1;
}
}
if (ne2 && !ne3)
{
type=1;
use_sslv2_strong=1;
goto next_bit;
}
}
#else
SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO,SSL_R_UNSUPPORTED_OPTION);
goto err;
#endif
}
}
}
else if ((p[0] == SSL3_RT_HANDSHAKE) &&
(p[1] == SSL3_VERSION_MAJOR) &&
(p[5] == SSL3_MT_CLIENT_HELLO) &&
((p[3] == 0 && p[4] < 5 )
|| (p[9] == p[1])))
{
v[0]=p[1];
v[1]=p[10];
if (p[3] == 0 && p[4] < 6)
v[1]=p[2];
if (v[1] >= TLS1_VERSION_MINOR)
{
if (!(s->options & SSL_OP_NO_TLSv1))
{
s->version=TLS1_VERSION;
type=3;
}
else if (!(s->options & SSL_OP_NO_SSLv3))
{
s->version=SSL3_VERSION;
type=3;
}
}
else if (!(s->options & SSL_OP_NO_SSLv3))
{
s->version=SSL3_VERSION;
type=3;
}
}
else if ((strncmp("GET ", (char *)p,4) == 0) ||
(strncmp("POST ",(char *)p,5) == 0) ||
(strncmp("HEAD ",(char *)p,5) == 0) ||
(strncmp("PUT ", (char *)p,4) == 0))
{
SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO,SSL_R_HTTP_REQUEST);
goto err;
}
else if (strncmp("CONNECT",(char *)p,7) == 0)
{
SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO,SSL_R_HTTPS_PROXY_REQUEST);
goto err;
}
}
if (s->state == SSL23_ST_SR_CLNT_HELLO_B)
{
type=2;
p=s->packet;
v[0] = p[3];
v[1] = p[4];
n=((p[0]&0x7f)<<8)|p[1];
if (n > (1024*4))
{
SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO,SSL_R_RECORD_TOO_LARGE);
goto err;
}
j=ssl23_read_bytes(s,n+2);
if (j <= 0) return(j);
ssl3_finish_mac(s,&(s->packet[2]),s->packet_length-2);
p=s->packet;
p+=5;
n2s(p,csl);
n2s(p,sil);
n2s(p,cl);
d=(unsigned char *)s->init_buf->data;
if ((csl+sil+cl+11) != s->packet_length)
{
SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO,SSL_R_RECORD_LENGTH_MISMATCH);
goto err;
}
*(d++) = SSL3_VERSION_MAJOR;
*(d++) = v[1];
i=(cl > SSL3_RANDOM_SIZE)?SSL3_RANDOM_SIZE:cl;
memset(d,0,SSL3_RANDOM_SIZE);
memcpy(&(d[SSL3_RANDOM_SIZE-i]),&(p[csl+sil]),i);
d+=SSL3_RANDOM_SIZE;
*(d++)=0;
j=0;
dd=d;
d+=2;
for (i=0; i<csl; i+=3)
{
if (p[i] != 0) continue;
*(d++)=p[i+1];
*(d++)=p[i+2];
j+=2;
}
s2n(j,dd);
*(d++)=1;
*(d++)=0;
i=(d-(unsigned char *)s->init_buf->data);
s->s3->tmp.reuse_message=1;
s->s3->tmp.message_type=SSL3_MT_CLIENT_HELLO;
s->s3->tmp.message_size=i;
}
if (type == 1)
{
#ifdef NO_SSL2
SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO,SSL_R_UNSUPPORTED_PROTOCOL);
goto err;
#else
if (s->s2 == NULL)
{
if (!ssl2_new(s))
goto err;
}
else
ssl2_clear(s);
if (s->s3 != NULL) ssl3_free(s);
if (!BUF_MEM_grow(s->init_buf,
SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER))
{
goto err;
}
s->state=SSL2_ST_GET_CLIENT_HELLO_A;
if ((s->options & SSL_OP_MSIE_SSLV2_RSA_PADDING) ||
use_sslv2_strong)
s->s2->ssl2_rollback=0;
else
s->s2->ssl2_rollback=1;
s->rstate=SSL_ST_READ_HEADER;
s->packet_length=n;
s->packet= &(s->s2->rbuf[0]);
memcpy(s->packet,buf,n);
s->s2->rbuf_left=n;
s->s2->rbuf_offs=0;
s->method=SSLv2_server_method();
s->handshake_func=s->method->ssl_accept;
#endif
}
if ((type == 2) || (type == 3))
{
if (!ssl_init_wbio_buffer(s,1)) goto err;
s->state=SSL3_ST_SR_CLNT_HELLO_A;
if (type == 3)
{
s->rstate=SSL_ST_READ_HEADER;
s->packet_length=n;
s->packet= &(s->s3->rbuf.buf[0]);
memcpy(s->packet,buf,n);
s->s3->rbuf.left=n;
s->s3->rbuf.offset=0;
}
else
{
s->packet_length=0;
s->s3->rbuf.left=0;
s->s3->rbuf.offset=0;
}
if (s->version == TLS1_VERSION)
s->method = TLSv1_server_method();
else
s->method = SSLv3_server_method();
#if 0
s->client_version=(v[0]<<8)|v[1];
#endif
s->handshake_func=s->method->ssl_accept;
}
if ((type < 1) || (type > 3))
{
SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO,SSL_R_UNKNOWN_PROTOCOL);
goto err;
}
s->init_num=0;
if (buf != buf_space) Free(buf);
s->first_packet=1;
return(SSL_accept(s));
err:
if (buf != buf_space) Free(buf);
return(-1);
} | ['int ssl23_get_client_hello(SSL *s)\n\t{\n\tchar buf_space[11];\n\tchar *buf= &(buf_space[0]);\n\tunsigned char *p,*d,*dd;\n\tunsigned int i;\n\tunsigned int csl,sil,cl;\n\tint n=0,j;\n\tint type=0;\n\tint v[2];\n#ifndef NO_RSA\n\tint use_sslv2_strong=0;\n#endif\n\tif (s->state ==\tSSL23_ST_SR_CLNT_HELLO_A)\n\t\t{\n\t\tv[0]=v[1]=0;\n\t\tif (!ssl3_setup_buffers(s)) goto err;\n\t\tn=ssl23_read_bytes(s, sizeof buf_space);\n\t\tif (n != sizeof buf_space) return(n);\n\t\tp=s->packet;\n\t\tmemcpy(buf,p,n);\n\t\tif ((p[0] & 0x80) && (p[2] == SSL2_MT_CLIENT_HELLO))\n\t\t\t{\n\t\t\tif ((p[3] == 0x00) && (p[4] == 0x02))\n\t\t\t\t{\n\t\t\t\tv[0]=p[3]; v[1]=p[4];\n\t\t\t\tif (!(s->options & SSL_OP_NO_SSLv2))\n\t\t\t\t\ttype=1;\n\t\t\t\t}\n\t\t\telse if (p[3] == SSL3_VERSION_MAJOR)\n\t\t\t\t{\n\t\t\t\tv[0]=p[3]; v[1]=p[4];\n\t\t\t\tif (p[4] >= TLS1_VERSION_MINOR)\n\t\t\t\t\t{\n\t\t\t\t\tif (!(s->options & SSL_OP_NO_TLSv1))\n\t\t\t\t\t\t{\n\t\t\t\t\t\ts->version=TLS1_VERSION;\n\t\t\t\t\t\ts->state=SSL23_ST_SR_CLNT_HELLO_B;\n\t\t\t\t\t\t}\n\t\t\t\t\telse if (!(s->options & SSL_OP_NO_SSLv3))\n\t\t\t\t\t\t{\n\t\t\t\t\t\ts->version=SSL3_VERSION;\n\t\t\t\t\t\ts->state=SSL23_ST_SR_CLNT_HELLO_B;\n\t\t\t\t\t\t}\n\t\t\t\t\telse if (!(s->options & SSL_OP_NO_SSLv2))\n\t\t\t\t\t\t{\n\t\t\t\t\t\ttype=1;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\telse if (!(s->options & SSL_OP_NO_SSLv3))\n\t\t\t\t\t{\n\t\t\t\t\ts->version=SSL3_VERSION;\n\t\t\t\t\ts->state=SSL23_ST_SR_CLNT_HELLO_B;\n\t\t\t\t\t}\n\t\t\t\telse if (!(s->options & SSL_OP_NO_SSLv2))\n\t\t\t\t\ttype=1;\n\t\t\t\tif (s->options & SSL_OP_NON_EXPORT_FIRST)\n\t\t\t\t\t{\n#if 0\n\t\t\t\t\tSTACK_OF(SSL_CIPHER) *sk;\n\t\t\t\t\tSSL_CIPHER *c;\n\t\t\t\t\tint ne2,ne3;\n\t\t\t\t\tj=((p[0]&0x7f)<<8)|p[1];\n\t\t\t\t\tif (j > (1024*4))\n\t\t\t\t\t\t{\n\t\t\t\t\t\tSSLerr(SSL_F_SSL23_GET_CLIENT_HELLO,SSL_R_RECORD_TOO_LARGE);\n\t\t\t\t\t\tgoto err;\n\t\t\t\t\t\t}\n\t\t\t\t\tn=ssl23_read_bytes(s,j+2);\n\t\t\t\t\tif (n <= 0) return(n);\n\t\t\t\t\tp=s->packet;\n\t\t\t\t\tif ((buf=Malloc(n)) == NULL)\n\t\t\t\t\t\t{\n\t\t\t\t\t\tSSLerr(SSL_F_SSL23_GET_CLIENT_HELLO,ERR_R_MALLOC_FAILURE);\n\t\t\t\t\t\tgoto err;\n\t\t\t\t\t\t}\n\t\t\t\t\tmemcpy(buf,p,n);\n\t\t\t\t\tp+=5;\n\t\t\t\t\tn2s(p,csl);\n\t\t\t\t\tp+=4;\n\t\t\t\t\tsk=ssl_bytes_to_cipher_list(\n\t\t\t\t\t\ts,p,csl,NULL);\n\t\t\t\t\tif (sk != NULL)\n\t\t\t\t\t\t{\n\t\t\t\t\t\tne2=ne3=0;\n\t\t\t\t\t\tfor (j=0; j<sk_SSL_CIPHER_num(sk); j++)\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\tc=sk_SSL_CIPHER_value(sk,j);\n\t\t\t\t\t\t\tif (!SSL_C_IS_EXPORT(c))\n\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\tif ((c->id>>24L) == 2L)\n\t\t\t\t\t\t\t\t\tne2=1;\n\t\t\t\t\t\t\t\telse\n\t\t\t\t\t\t\t\t\tne3=1;\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\tif (ne2 && !ne3)\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\ttype=1;\n\t\t\t\t\t\t\tuse_sslv2_strong=1;\n\t\t\t\t\t\t\tgoto next_bit;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n#else\n\t\t\t\t\tSSLerr(SSL_F_SSL23_GET_CLIENT_HELLO,SSL_R_UNSUPPORTED_OPTION);\n\t\t\t\t\tgoto err;\n#endif\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\telse if ((p[0] == SSL3_RT_HANDSHAKE) &&\n\t\t\t (p[1] == SSL3_VERSION_MAJOR) &&\n\t\t\t (p[5] == SSL3_MT_CLIENT_HELLO) &&\n\t\t\t ((p[3] == 0 && p[4] < 5 )\n\t\t\t\t|| (p[9] == p[1])))\n\t\t\t{\n\t\t\tv[0]=p[1];\n\t\t\tv[1]=p[10];\n\t\t\tif (p[3] == 0 && p[4] < 6)\n\t\t\t\tv[1]=p[2];\n\t\t\tif (v[1] >= TLS1_VERSION_MINOR)\n\t\t\t\t{\n\t\t\t\tif (!(s->options & SSL_OP_NO_TLSv1))\n\t\t\t\t\t{\n\t\t\t\t\ts->version=TLS1_VERSION;\n\t\t\t\t\ttype=3;\n\t\t\t\t\t}\n\t\t\t\telse if (!(s->options & SSL_OP_NO_SSLv3))\n\t\t\t\t\t{\n\t\t\t\t\ts->version=SSL3_VERSION;\n\t\t\t\t\ttype=3;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\telse if (!(s->options & SSL_OP_NO_SSLv3))\n\t\t\t\t{\n\t\t\t\ts->version=SSL3_VERSION;\n\t\t\t\ttype=3;\n\t\t\t\t}\n\t\t\t}\n\t\telse if ((strncmp("GET ", (char *)p,4) == 0) ||\n\t\t\t (strncmp("POST ",(char *)p,5) == 0) ||\n\t\t\t (strncmp("HEAD ",(char *)p,5) == 0) ||\n\t\t\t (strncmp("PUT ", (char *)p,4) == 0))\n\t\t\t{\n\t\t\tSSLerr(SSL_F_SSL23_GET_CLIENT_HELLO,SSL_R_HTTP_REQUEST);\n\t\t\tgoto err;\n\t\t\t}\n\t\telse if (strncmp("CONNECT",(char *)p,7) == 0)\n\t\t\t{\n\t\t\tSSLerr(SSL_F_SSL23_GET_CLIENT_HELLO,SSL_R_HTTPS_PROXY_REQUEST);\n\t\t\tgoto err;\n\t\t\t}\n\t\t}\n\tif (s->state == SSL23_ST_SR_CLNT_HELLO_B)\n\t\t{\n\t\ttype=2;\n\t\tp=s->packet;\n\t\tv[0] = p[3];\n\t\tv[1] = p[4];\n\t\tn=((p[0]&0x7f)<<8)|p[1];\n\t\tif (n > (1024*4))\n\t\t\t{\n\t\t\tSSLerr(SSL_F_SSL23_GET_CLIENT_HELLO,SSL_R_RECORD_TOO_LARGE);\n\t\t\tgoto err;\n\t\t\t}\n\t\tj=ssl23_read_bytes(s,n+2);\n\t\tif (j <= 0) return(j);\n\t\tssl3_finish_mac(s,&(s->packet[2]),s->packet_length-2);\n\t\tp=s->packet;\n\t\tp+=5;\n\t\tn2s(p,csl);\n\t\tn2s(p,sil);\n\t\tn2s(p,cl);\n\t\td=(unsigned char *)s->init_buf->data;\n\t\tif ((csl+sil+cl+11) != s->packet_length)\n\t\t\t{\n\t\t\tSSLerr(SSL_F_SSL23_GET_CLIENT_HELLO,SSL_R_RECORD_LENGTH_MISMATCH);\n\t\t\tgoto err;\n\t\t\t}\n\t\t*(d++) = SSL3_VERSION_MAJOR;\n\t\t*(d++) = v[1];\n\t\ti=(cl > SSL3_RANDOM_SIZE)?SSL3_RANDOM_SIZE:cl;\n\t\tmemset(d,0,SSL3_RANDOM_SIZE);\n\t\tmemcpy(&(d[SSL3_RANDOM_SIZE-i]),&(p[csl+sil]),i);\n\t\td+=SSL3_RANDOM_SIZE;\n\t\t*(d++)=0;\n\t\tj=0;\n\t\tdd=d;\n\t\td+=2;\n\t\tfor (i=0; i<csl; i+=3)\n\t\t\t{\n\t\t\tif (p[i] != 0) continue;\n\t\t\t*(d++)=p[i+1];\n\t\t\t*(d++)=p[i+2];\n\t\t\tj+=2;\n\t\t\t}\n\t\ts2n(j,dd);\n\t\t*(d++)=1;\n\t\t*(d++)=0;\n\t\ti=(d-(unsigned char *)s->init_buf->data);\n\t\ts->s3->tmp.reuse_message=1;\n\t\ts->s3->tmp.message_type=SSL3_MT_CLIENT_HELLO;\n\t\ts->s3->tmp.message_size=i;\n\t\t}\n\tif (type == 1)\n\t\t{\n#ifdef NO_SSL2\n\t\tSSLerr(SSL_F_SSL23_GET_CLIENT_HELLO,SSL_R_UNSUPPORTED_PROTOCOL);\n\t\tgoto err;\n#else\n\t\tif (s->s2 == NULL)\n\t\t\t{\n\t\t\tif (!ssl2_new(s))\n\t\t\t\tgoto err;\n\t\t\t}\n\t\telse\n\t\t\tssl2_clear(s);\n\t\tif (s->s3 != NULL) ssl3_free(s);\n\t\tif (!BUF_MEM_grow(s->init_buf,\n\t\t\tSSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER))\n\t\t\t{\n\t\t\tgoto err;\n\t\t\t}\n\t\ts->state=SSL2_ST_GET_CLIENT_HELLO_A;\n\t\tif ((s->options & SSL_OP_MSIE_SSLV2_RSA_PADDING) ||\n\t\t\tuse_sslv2_strong)\n\t\t\ts->s2->ssl2_rollback=0;\n\t\telse\n\t\t\ts->s2->ssl2_rollback=1;\n\t\ts->rstate=SSL_ST_READ_HEADER;\n\t\ts->packet_length=n;\n\t\ts->packet= &(s->s2->rbuf[0]);\n\t\tmemcpy(s->packet,buf,n);\n\t\ts->s2->rbuf_left=n;\n\t\ts->s2->rbuf_offs=0;\n\t\ts->method=SSLv2_server_method();\n\t\ts->handshake_func=s->method->ssl_accept;\n#endif\n\t\t}\n\tif ((type == 2) || (type == 3))\n\t\t{\n\t\tif (!ssl_init_wbio_buffer(s,1)) goto err;\n\t\ts->state=SSL3_ST_SR_CLNT_HELLO_A;\n\t\tif (type == 3)\n\t\t\t{\n\t\t\ts->rstate=SSL_ST_READ_HEADER;\n\t\t\ts->packet_length=n;\n\t\t\ts->packet= &(s->s3->rbuf.buf[0]);\n\t\t\tmemcpy(s->packet,buf,n);\n\t\t\ts->s3->rbuf.left=n;\n\t\t\ts->s3->rbuf.offset=0;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\ts->packet_length=0;\n\t\t\ts->s3->rbuf.left=0;\n\t\t\ts->s3->rbuf.offset=0;\n\t\t\t}\n\t\tif (s->version == TLS1_VERSION)\n\t\t\ts->method = TLSv1_server_method();\n\t\telse\n\t\t\ts->method = SSLv3_server_method();\n#if 0\n\t\ts->client_version=(v[0]<<8)|v[1];\n#endif\n\t\ts->handshake_func=s->method->ssl_accept;\n\t\t}\n\tif ((type < 1) || (type > 3))\n\t\t{\n\t\tSSLerr(SSL_F_SSL23_GET_CLIENT_HELLO,SSL_R_UNKNOWN_PROTOCOL);\n\t\tgoto err;\n\t\t}\n\ts->init_num=0;\n\tif (buf != buf_space) Free(buf);\n\ts->first_packet=1;\n\treturn(SSL_accept(s));\nerr:\n\tif (buf != buf_space) Free(buf);\n\treturn(-1);\n\t}'] |
35,168 | 0 | https://github.com/openssl/openssl/blob/1fac96e4d6484a517f2ebe99b72016726391723c/crypto/bn/bn_lib.c/#L469 | BIGNUM *bn_expand2(BIGNUM *b, int words)
{
BN_ULONG *A,*a;
const BN_ULONG *B;
int i;
bn_check_top(b);
if (words > b->max)
{
bn_check_top(b);
if (BN_get_flags(b,BN_FLG_STATIC_DATA))
{
BNerr(BN_F_BN_EXPAND2,BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);
return(NULL);
}
a=A=(BN_ULONG *)Malloc(sizeof(BN_ULONG)*(words+1));
if (A == NULL)
{
BNerr(BN_F_BN_EXPAND2,ERR_R_MALLOC_FAILURE);
return(NULL);
}
#if 1
B=b->d;
if (B != NULL)
{
#if 0
for (i=b->top&(~7); i>0; i-=8)
{
A[0]=B[0]; A[1]=B[1]; A[2]=B[2]; A[3]=B[3];
A[4]=B[4]; A[5]=B[5]; A[6]=B[6]; A[7]=B[7];
A+=8;
B+=8;
}
switch (b->top&7)
{
case 7:
A[6]=B[6];
case 6:
A[5]=B[5];
case 5:
A[4]=B[4];
case 4:
A[3]=B[3];
case 3:
A[2]=B[2];
case 2:
A[1]=B[1];
case 1:
A[0]=B[0];
case 0:
;
}
#else
for (i=b->top>>2; i>0; i--,A+=4,B+=4)
{
BN_ULONG a0,a1,a2,a3;
a0=B[0]; a1=B[1]; a2=B[2]; a3=B[3];
A[0]=a0; A[1]=a1; A[2]=a2; A[3]=a3;
}
switch (b->top&3)
{
case 3: A[2]=B[2];
case 2: A[1]=B[1];
case 1: A[0]=B[0];
case 0: ;
}
#endif
Free(b->d);
}
b->d=a;
b->max=words;
A= &(b->d[b->top]);
for (i=(b->max - b->top)>>3; i>0; i--,A+=8)
{
A[0]=0; A[1]=0; A[2]=0; A[3]=0;
A[4]=0; A[5]=0; A[6]=0; A[7]=0;
}
for (i=(b->max - b->top)&7; i>0; i--,A++)
A[0]=0;
#else
memset(A,0,sizeof(BN_ULONG)*(words+1));
memcpy(A,b->d,sizeof(b->d[0])*b->top);
b->d=a;
b->max=words;
#endif
}
return(b);
} | ['int BN_MONT_CTX_set(BN_MONT_CTX *mont, 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}', 'BIGNUM *BN_mod_inverse(BIGNUM *in, BIGNUM *a, BIGNUM *n, BN_CTX *ctx)\n\t{\n\tBIGNUM *A,*B,*X,*Y,*M,*D,*R;\n\tBIGNUM *T,*ret=NULL;\n\tint sign;\n\tbn_check_top(a);\n\tbn_check_top(n);\n\tA= &(ctx->bn[ctx->tos]);\n\tB= &(ctx->bn[ctx->tos+1]);\n\tX= &(ctx->bn[ctx->tos+2]);\n\tD= &(ctx->bn[ctx->tos+3]);\n\tM= &(ctx->bn[ctx->tos+4]);\n\tY= &(ctx->bn[ctx->tos+5]);\n\tctx->tos+=6;\n\tif (in == NULL)\n\t\tR=BN_new();\n\telse\n\t\tR=in;\n\tif (R == NULL) goto err;\n\tBN_zero(X);\n\tBN_one(Y);\n\tif (BN_copy(A,a) == NULL) goto err;\n\tif (BN_copy(B,n) == NULL) goto err;\n\tsign=1;\n\twhile (!BN_is_zero(B))\n\t\t{\n\t\tif (!BN_div(D,M,A,B,ctx)) goto err;\n\t\tT=A;\n\t\tA=B;\n\t\tB=M;\n\t\tif (!BN_mul(T,D,X,ctx)) goto err;\n\t\tif (!BN_add(T,T,Y)) goto err;\n\t\tM=Y;\n\t\tY=X;\n\t\tX=T;\n\t\tsign= -sign;\n\t\t}\n\tif (sign < 0)\n\t\t{\n\t\tif (!BN_sub(Y,n,Y)) goto err;\n\t\t}\n\tif (BN_is_one(A))\n\t\t{ if (!BN_mod(R,Y,n,ctx)) goto err; }\n\telse\n\t\t{\n\t\tBNerr(BN_F_BN_MOD_INVERSE,BN_R_NO_INVERSE);\n\t\tgoto err;\n\t\t}\n\tret=R;\nerr:\n\tif ((ret == NULL) && (in == NULL)) BN_free(R);\n\tctx->tos-=6;\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}', 'BIGNUM *BN_copy(BIGNUM *a, BIGNUM *b)\n\t{\n\tint i;\n\tBN_ULONG *A;\n\tconst BN_ULONG *B;\n\tbn_check_top(b);\n\tif (a == b) return(a);\n\tif (bn_wexpand(a,b->top) == NULL) return(NULL);\n#if 1\n\tA=a->d;\n\tB=b->d;\n\tfor (i=b->top>>2; i>0; i--,A+=4,B+=4)\n\t\t{\n\t\tBN_ULONG a0,a1,a2,a3;\n\t\ta0=B[0]; a1=B[1]; a2=B[2]; a3=B[3];\n\t\tA[0]=a0; A[1]=a1; A[2]=a2; A[3]=a3;\n\t\t}\n\tswitch (b->top&3)\n\t\t{\n\t\tcase 3: A[2]=B[2];\n\t\tcase 2: A[1]=B[1];\n\t\tcase 1: A[0]=B[0];\n\t\tcase 0: ;\n\t\t}\n#else\n\tmemcpy(a->d,b->d,sizeof(b->d[0])*b->top);\n#endif\n\ta->top=b->top;\n\tif ((a->top == 0) && (a->d != NULL))\n\t\ta->d[0]=0;\n\ta->neg=b->neg;\n\treturn(a);\n\t}', 'BIGNUM *bn_expand2(BIGNUM *b, int words)\n\t{\n\tBN_ULONG *A,*a;\n\tconst BN_ULONG *B;\n\tint i;\n\tbn_check_top(b);\n\tif (words > b->max)\n\t\t{\n\t\tbn_check_top(b);\n\t\tif (BN_get_flags(b,BN_FLG_STATIC_DATA))\n\t\t\t{\n\t\t\tBNerr(BN_F_BN_EXPAND2,BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);\n\t\t\treturn(NULL);\n\t\t\t}\n\t\ta=A=(BN_ULONG *)Malloc(sizeof(BN_ULONG)*(words+1));\n\t\tif (A == NULL)\n\t\t\t{\n\t\t\tBNerr(BN_F_BN_EXPAND2,ERR_R_MALLOC_FAILURE);\n\t\t\treturn(NULL);\n\t\t\t}\n#if 1\n\t\tB=b->d;\n\t\tif (B != NULL)\n\t\t\t{\n#if 0\n\t\t\tfor (i=b->top&(~7); i>0; i-=8)\n\t\t\t\t{\n\t\t\t\tA[0]=B[0]; A[1]=B[1]; A[2]=B[2]; A[3]=B[3];\n\t\t\t\tA[4]=B[4]; A[5]=B[5]; A[6]=B[6]; A[7]=B[7];\n\t\t\t\tA+=8;\n\t\t\t\tB+=8;\n\t\t\t\t}\n\t\t\tswitch (b->top&7)\n\t\t\t\t{\n\t\t\tcase 7:\n\t\t\t\tA[6]=B[6];\n\t\t\tcase 6:\n\t\t\t\tA[5]=B[5];\n\t\t\tcase 5:\n\t\t\t\tA[4]=B[4];\n\t\t\tcase 4:\n\t\t\t\tA[3]=B[3];\n\t\t\tcase 3:\n\t\t\t\tA[2]=B[2];\n\t\t\tcase 2:\n\t\t\t\tA[1]=B[1];\n\t\t\tcase 1:\n\t\t\t\tA[0]=B[0];\n\t\t\tcase 0:\n\t\t\t\t;\n\t\t\t\t}\n#else\n\t\t\tfor (i=b->top>>2; i>0; i--,A+=4,B+=4)\n\t\t\t\t{\n\t\t\t\tBN_ULONG a0,a1,a2,a3;\n\t\t\t\ta0=B[0]; a1=B[1]; a2=B[2]; a3=B[3];\n\t\t\t\tA[0]=a0; A[1]=a1; A[2]=a2; A[3]=a3;\n\t\t\t\t}\n\t\t\tswitch (b->top&3)\n\t\t\t\t{\n\t\t\t\tcase 3:\tA[2]=B[2];\n\t\t\t\tcase 2:\tA[1]=B[1];\n\t\t\t\tcase 1:\tA[0]=B[0];\n\t\t\t\tcase 0:\t;\n\t\t\t\t}\n#endif\n\t\t\tFree(b->d);\n\t\t\t}\n\t\tb->d=a;\n\t\tb->max=words;\n\t\tA= &(b->d[b->top]);\n\t\tfor (i=(b->max - b->top)>>3; i>0; i--,A+=8)\n\t\t\t{\n\t\t\tA[0]=0; A[1]=0; A[2]=0; A[3]=0;\n\t\t\tA[4]=0; A[5]=0; A[6]=0; A[7]=0;\n\t\t\t}\n\t\tfor (i=(b->max - b->top)&7; i>0; i--,A++)\n\t\t\tA[0]=0;\n#else\n\t\t\tmemset(A,0,sizeof(BN_ULONG)*(words+1));\n\t\t\tmemcpy(A,b->d,sizeof(b->d[0])*b->top);\n\t\t\tb->d=a;\n\t\t\tb->max=words;\n#endif\n\t\t}\n\treturn(b);\n\t}'] |
35,169 | 0 | https://github.com/openssl/openssl/blob/0f3ffbd1581fad58095fedcc32b0da42a486b8b7/crypto/rand/rand_lib.c/#L348 | void RAND_seed(const void *buf, int num)
{
const RAND_METHOD *meth = RAND_get_rand_method();
if (meth->seed != NULL)
meth->seed(buf, num);
} | ['void RAND_seed(const void *buf, int num)\n{\n const RAND_METHOD *meth = RAND_get_rand_method();\n if (meth->seed != NULL)\n meth->seed(buf, num);\n}', 'const RAND_METHOD *RAND_get_rand_method(void)\n{\n const RAND_METHOD *tmp_meth = NULL;\n if (!RUN_ONCE(&rand_init, do_rand_init))\n return NULL;\n CRYPTO_THREAD_write_lock(rand_meth_lock);\n if (default_RAND_meth == NULL) {\n#ifndef OPENSSL_NO_ENGINE\n ENGINE *e;\n if ((e = ENGINE_get_default_RAND()) != NULL\n && (tmp_meth = ENGINE_get_RAND(e)) != NULL) {\n funct_ref = e;\n default_RAND_meth = tmp_meth;\n } else {\n ENGINE_finish(e);\n default_RAND_meth = &rand_meth;\n }\n#else\n default_RAND_meth = &rand_meth;\n#endif\n }\n tmp_meth = default_RAND_meth;\n CRYPTO_THREAD_unlock(rand_meth_lock);\n return tmp_meth;\n}', 'int CRYPTO_THREAD_run_once(CRYPTO_ONCE *once, void (*init)(void))\n{\n if (pthread_once(once, init) != 0)\n return 0;\n return 1;\n}'] |
35,170 | 1 | https://github.com/libav/libav/blob/63e8d9760f23a4edf81e9ae58c4f6d3baa6ff4dd/libavcodec/mpegaudiodec.c/#L671 | static void apply_window_mp3_c(MPA_INT *synth_buf, MPA_INT *window,
int *dither_state, OUT_INT *samples, int incr)
{
register const MPA_INT *w, *w2, *p;
int j;
OUT_INT *samples2;
#if CONFIG_FLOAT
float sum, sum2;
#elif FRAC_BITS <= 15
int sum, sum2;
#else
int64_t sum, sum2;
#endif
memcpy(synth_buf + 512, synth_buf, 32 * sizeof(*synth_buf));
samples2 = samples + 31 * incr;
w = window;
w2 = window + 31;
sum = *dither_state;
p = synth_buf + 16;
SUM8(MACS, sum, w, p);
p = synth_buf + 48;
SUM8(MLSS, sum, w + 32, p);
*samples = round_sample(&sum);
samples += incr;
w++;
for(j=1;j<16;j++) {
sum2 = 0;
p = synth_buf + 16 + j;
SUM8P2(sum, MACS, sum2, MLSS, w, w2, p);
p = synth_buf + 48 - j;
SUM8P2(sum, MLSS, sum2, MLSS, w + 32, w2 + 32, p);
*samples = round_sample(&sum);
samples += incr;
sum += sum2;
*samples2 = round_sample(&sum);
samples2 -= incr;
w++;
w2--;
}
p = synth_buf + 32;
SUM8(MLSS, sum, w + 32, p);
*samples = round_sample(&sum);
*dither_state= sum;
} | ['static void mpc_synth(MPCContext *c, int16_t *out)\n{\n int dither_state = 0;\n int i, ch;\n OUT_INT samples[MPA_MAX_CHANNELS * MPA_FRAME_SIZE], *samples_ptr;\n for(ch = 0; ch < 2; ch++){\n samples_ptr = samples + ch;\n for(i = 0; i < SAMPLES_PER_BAND; i++) {\n ff_mpa_synth_filter(c->synth_buf[ch], &(c->synth_buf_offset[ch]),\n ff_mpa_synth_window, &dither_state,\n samples_ptr, 2,\n c->sb_samples[ch][i]);\n samples_ptr += 64;\n }\n }\n for(i = 0; i < MPC_FRAME_SIZE*2; i++)\n *out++=samples[i];\n}', 'void ff_mpa_synth_filter(MPA_INT *synth_buf_ptr, int *synth_buf_offset,\n MPA_INT *window, int *dither_state,\n OUT_INT *samples, int incr,\n INTFLOAT sb_samples[SBLIMIT])\n{\n register MPA_INT *synth_buf;\n int offset;\n#if FRAC_BITS <= 15\n int32_t tmp[32];\n int j;\n#endif\n offset = *synth_buf_offset;\n synth_buf = synth_buf_ptr + offset;\n#if FRAC_BITS <= 15\n dct32(tmp, sb_samples);\n for(j=0;j<32;j++) {\n synth_buf[j] = av_clip_int16(tmp[j]);\n }\n#else\n dct32(synth_buf, sb_samples);\n#endif\n apply_window_mp3_c(synth_buf, window, dither_state, samples, incr);\n offset = (offset - 32) & 511;\n *synth_buf_offset = offset;\n}', 'static void apply_window_mp3_c(MPA_INT *synth_buf, MPA_INT *window,\n int *dither_state, OUT_INT *samples, int incr)\n{\n register const MPA_INT *w, *w2, *p;\n int j;\n OUT_INT *samples2;\n#if CONFIG_FLOAT\n float sum, sum2;\n#elif FRAC_BITS <= 15\n int sum, sum2;\n#else\n int64_t sum, sum2;\n#endif\n memcpy(synth_buf + 512, synth_buf, 32 * sizeof(*synth_buf));\n samples2 = samples + 31 * incr;\n w = window;\n w2 = window + 31;\n sum = *dither_state;\n p = synth_buf + 16;\n SUM8(MACS, sum, w, p);\n p = synth_buf + 48;\n SUM8(MLSS, sum, w + 32, p);\n *samples = round_sample(&sum);\n samples += incr;\n w++;\n for(j=1;j<16;j++) {\n sum2 = 0;\n p = synth_buf + 16 + j;\n SUM8P2(sum, MACS, sum2, MLSS, w, w2, p);\n p = synth_buf + 48 - j;\n SUM8P2(sum, MLSS, sum2, MLSS, w + 32, w2 + 32, p);\n *samples = round_sample(&sum);\n samples += incr;\n sum += sum2;\n *samples2 = round_sample(&sum);\n samples2 -= incr;\n w++;\n w2--;\n }\n p = synth_buf + 32;\n SUM8(MLSS, sum, w + 32, p);\n *samples = round_sample(&sum);\n *dither_state= sum;\n}'] |
35,171 | 0 | https://github.com/libav/libav/blob/12f0388f9cb32016ac0dacaeca631b088b29bb96/libavformat/mpegts.c/#L741 | static int read_sl_header(PESContext *pes, SLConfigDescr *sl,
const uint8_t *buf, int buf_size)
{
GetBitContext gb;
int au_start_flag = 0, au_end_flag = 0, ocr_flag = 0, idle_flag = 0;
int padding_flag = 0, padding_bits = 0, inst_bitrate_flag = 0;
int dts_flag = -1, cts_flag = -1;
int64_t dts = AV_NOPTS_VALUE, cts = AV_NOPTS_VALUE;
init_get_bits(&gb, buf, buf_size * 8);
if (sl->use_au_start)
au_start_flag = get_bits1(&gb);
if (sl->use_au_end)
au_end_flag = get_bits1(&gb);
if (!sl->use_au_start && !sl->use_au_end)
au_start_flag = au_end_flag = 1;
if (sl->ocr_len > 0)
ocr_flag = get_bits1(&gb);
if (sl->use_idle)
idle_flag = get_bits1(&gb);
if (sl->use_padding)
padding_flag = get_bits1(&gb);
if (padding_flag)
padding_bits = get_bits(&gb, 3);
if (!idle_flag && (!padding_flag || padding_bits != 0)) {
if (sl->packet_seq_num_len)
skip_bits_long(&gb, sl->packet_seq_num_len);
if (sl->degr_prior_len)
if (get_bits1(&gb))
skip_bits(&gb, sl->degr_prior_len);
if (ocr_flag)
skip_bits_long(&gb, sl->ocr_len);
if (au_start_flag) {
if (sl->use_rand_acc_pt)
get_bits1(&gb);
if (sl->au_seq_num_len > 0)
skip_bits_long(&gb, sl->au_seq_num_len);
if (sl->use_timestamps) {
dts_flag = get_bits1(&gb);
cts_flag = get_bits1(&gb);
}
}
if (sl->inst_bitrate_len)
inst_bitrate_flag = get_bits1(&gb);
if (dts_flag == 1)
dts = get_bits64(&gb, sl->timestamp_len);
if (cts_flag == 1)
cts = get_bits64(&gb, sl->timestamp_len);
if (sl->au_len > 0)
skip_bits_long(&gb, sl->au_len);
if (inst_bitrate_flag)
skip_bits_long(&gb, sl->inst_bitrate_len);
}
if (dts != AV_NOPTS_VALUE)
pes->dts = dts;
if (cts != AV_NOPTS_VALUE)
pes->pts = cts;
if (sl->timestamp_len && sl->timestamp_res)
avpriv_set_pts_info(pes->st, sl->timestamp_len, 1, sl->timestamp_res);
return (get_bits_count(&gb) + 7) >> 3;
} | ['static int read_sl_header(PESContext *pes, SLConfigDescr *sl,\n const uint8_t *buf, int buf_size)\n{\n GetBitContext gb;\n int au_start_flag = 0, au_end_flag = 0, ocr_flag = 0, idle_flag = 0;\n int padding_flag = 0, padding_bits = 0, inst_bitrate_flag = 0;\n int dts_flag = -1, cts_flag = -1;\n int64_t dts = AV_NOPTS_VALUE, cts = AV_NOPTS_VALUE;\n init_get_bits(&gb, buf, buf_size * 8);\n if (sl->use_au_start)\n au_start_flag = get_bits1(&gb);\n if (sl->use_au_end)\n au_end_flag = get_bits1(&gb);\n if (!sl->use_au_start && !sl->use_au_end)\n au_start_flag = au_end_flag = 1;\n if (sl->ocr_len > 0)\n ocr_flag = get_bits1(&gb);\n if (sl->use_idle)\n idle_flag = get_bits1(&gb);\n if (sl->use_padding)\n padding_flag = get_bits1(&gb);\n if (padding_flag)\n padding_bits = get_bits(&gb, 3);\n if (!idle_flag && (!padding_flag || padding_bits != 0)) {\n if (sl->packet_seq_num_len)\n skip_bits_long(&gb, sl->packet_seq_num_len);\n if (sl->degr_prior_len)\n if (get_bits1(&gb))\n skip_bits(&gb, sl->degr_prior_len);\n if (ocr_flag)\n skip_bits_long(&gb, sl->ocr_len);\n if (au_start_flag) {\n if (sl->use_rand_acc_pt)\n get_bits1(&gb);\n if (sl->au_seq_num_len > 0)\n skip_bits_long(&gb, sl->au_seq_num_len);\n if (sl->use_timestamps) {\n dts_flag = get_bits1(&gb);\n cts_flag = get_bits1(&gb);\n }\n }\n if (sl->inst_bitrate_len)\n inst_bitrate_flag = get_bits1(&gb);\n if (dts_flag == 1)\n dts = get_bits64(&gb, sl->timestamp_len);\n if (cts_flag == 1)\n cts = get_bits64(&gb, sl->timestamp_len);\n if (sl->au_len > 0)\n skip_bits_long(&gb, sl->au_len);\n if (inst_bitrate_flag)\n skip_bits_long(&gb, sl->inst_bitrate_len);\n }\n if (dts != AV_NOPTS_VALUE)\n pes->dts = dts;\n if (cts != AV_NOPTS_VALUE)\n pes->pts = cts;\n if (sl->timestamp_len && sl->timestamp_res)\n avpriv_set_pts_info(pes->st, sl->timestamp_len, 1, sl->timestamp_res);\n return (get_bits_count(&gb) + 7) >> 3;\n}', 'static inline int init_get_bits(GetBitContext *s, const uint8_t *buffer,\n int bit_size)\n{\n int buffer_size;\n int ret = 0;\n if (bit_size > INT_MAX - 7 || bit_size < 0 || !buffer) {\n buffer_size = bit_size = 0;\n buffer = NULL;\n ret = AVERROR_INVALIDDATA;\n }\n buffer_size = (bit_size + 7) >> 3;\n s->buffer = buffer;\n s->size_in_bits = bit_size;\n#if !UNCHECKED_BITSTREAM_READER\n s->size_in_bits_plus8 = bit_size + 8;\n#endif\n s->buffer_end = buffer + buffer_size;\n s->index = 0;\n return ret;\n}', 'static inline unsigned int get_bits1(GetBitContext *s)\n{\n unsigned int index = s->index;\n uint8_t result = s->buffer[index >> 3];\n#ifdef BITSTREAM_READER_LE\n result >>= index & 7;\n result &= 1;\n#else\n result <<= index & 7;\n result >>= 8 - 1;\n#endif\n#if !UNCHECKED_BITSTREAM_READER\n if (s->index < s->size_in_bits_plus8)\n#endif\n index++;\n s->index = index;\n return result;\n}'] |
35,172 | 0 | https://github.com/openssl/openssl/blob/d4b009d5f88875ac0e3ac0b2b9689ed16a4c88dc/crypto/lhash/lhash.c/#L209 | void *lh_delete(_LHASH *lh, const void *data)
{
unsigned long hash;
LHASH_NODE *nn, **rn;
void *ret;
lh->error = 0;
rn = getrn(lh, data, &hash);
if (*rn == NULL) {
lh->num_no_delete++;
return (NULL);
} else {
nn = *rn;
*rn = nn->next;
ret = nn->data;
OPENSSL_free(nn);
lh->num_delete++;
}
lh->num_items--;
if ((lh->num_nodes > MIN_NODES) &&
(lh->down_load >= (lh->num_items * LH_LOAD_MULT / lh->num_nodes)))
contract(lh);
return (ret);
} | ['MSG_PROCESS_RETURN tls_process_server_hello(SSL *s, PACKET *pkt)\n{\n STACK_OF(SSL_CIPHER) *sk;\n const SSL_CIPHER *c;\n PACKET session_id;\n size_t session_id_len;\n unsigned char *cipherchars;\n int i, al = SSL_AD_INTERNAL_ERROR;\n unsigned int compression;\n unsigned int sversion;\n int protverr;\n#ifndef OPENSSL_NO_COMP\n SSL_COMP *comp;\n#endif\n if (!PACKET_get_net_2(pkt, &sversion)) {\n al = SSL_AD_DECODE_ERROR;\n SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);\n goto f_err;\n }\n protverr = ssl_choose_client_version(s, sversion);\n if (protverr != 0) {\n al = SSL_AD_PROTOCOL_VERSION;\n SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, protverr);\n goto f_err;\n }\n if (!PACKET_copy_bytes(pkt, s->s3->server_random, SSL3_RANDOM_SIZE)) {\n al = SSL_AD_DECODE_ERROR;\n SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);\n goto f_err;\n }\n s->hit = 0;\n if (!PACKET_get_length_prefixed_1(pkt, &session_id)) {\n al = SSL_AD_DECODE_ERROR;\n SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);\n goto f_err;\n }\n session_id_len = PACKET_remaining(&session_id);\n if (session_id_len > sizeof s->session->session_id\n || session_id_len > SSL3_SESSION_ID_SIZE) {\n al = SSL_AD_ILLEGAL_PARAMETER;\n SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_SSL3_SESSION_ID_TOO_LONG);\n goto f_err;\n }\n if (!PACKET_get_bytes(pkt, &cipherchars, TLS_CIPHER_LEN)) {\n SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);\n al = SSL_AD_DECODE_ERROR;\n goto f_err;\n }\n if (s->version >= TLS1_VERSION && s->tls_session_secret_cb &&\n s->session->tlsext_tick) {\n const SSL_CIPHER *pref_cipher = NULL;\n s->session->master_key_length = sizeof(s->session->master_key);\n if (s->tls_session_secret_cb(s, s->session->master_key,\n &s->session->master_key_length,\n NULL, &pref_cipher,\n s->tls_session_secret_cb_arg)) {\n s->session->cipher = pref_cipher ?\n pref_cipher : ssl_get_cipher_by_char(s, cipherchars);\n } else {\n SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, ERR_R_INTERNAL_ERROR);\n al = SSL_AD_INTERNAL_ERROR;\n goto f_err;\n }\n }\n if (session_id_len != 0 && session_id_len == s->session->session_id_length\n && memcmp(PACKET_data(&session_id), s->session->session_id,\n session_id_len) == 0) {\n if (s->sid_ctx_length != s->session->sid_ctx_length\n || memcmp(s->session->sid_ctx, s->sid_ctx, s->sid_ctx_length)) {\n al = SSL_AD_ILLEGAL_PARAMETER;\n SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,\n SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);\n goto f_err;\n }\n s->hit = 1;\n } else {\n if (s->session->session_id_length > 0) {\n if (!ssl_get_new_session(s, 0)) {\n goto f_err;\n }\n }\n s->session->session_id_length = session_id_len;\n memcpy(s->session->session_id, PACKET_data(&session_id),\n session_id_len);\n }\n c = ssl_get_cipher_by_char(s, cipherchars);\n if (c == NULL) {\n al = SSL_AD_ILLEGAL_PARAMETER;\n SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_UNKNOWN_CIPHER_RETURNED);\n goto f_err;\n }\n if (!SSL_USE_TLS1_2_CIPHERS(s))\n s->s3->tmp.mask_ssl = SSL_TLSV1_2;\n else\n s->s3->tmp.mask_ssl = 0;\n if ((c->algorithm_ssl & SSL_TLSV1) && s->version == SSL3_VERSION)\n s->s3->tmp.mask_ssl |= SSL_TLSV1;\n if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_CHECK)) {\n al = SSL_AD_ILLEGAL_PARAMETER;\n SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_WRONG_CIPHER_RETURNED);\n goto f_err;\n }\n sk = ssl_get_ciphers_by_id(s);\n i = sk_SSL_CIPHER_find(sk, c);\n if (i < 0) {\n al = SSL_AD_ILLEGAL_PARAMETER;\n SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_WRONG_CIPHER_RETURNED);\n goto f_err;\n }\n if (s->session->cipher)\n s->session->cipher_id = s->session->cipher->id;\n if (s->hit && (s->session->cipher_id != c->id)) {\n al = SSL_AD_ILLEGAL_PARAMETER;\n SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,\n SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);\n goto f_err;\n }\n s->s3->tmp.new_cipher = c;\n if (!PACKET_get_1(pkt, &compression)) {\n SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);\n al = SSL_AD_DECODE_ERROR;\n goto f_err;\n }\n#ifdef OPENSSL_NO_COMP\n if (compression != 0) {\n al = SSL_AD_ILLEGAL_PARAMETER;\n SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,\n SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);\n goto f_err;\n }\n if (s->session->compress_meth != 0) {\n SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_INCONSISTENT_COMPRESSION);\n goto f_err;\n }\n#else\n if (s->hit && compression != s->session->compress_meth) {\n al = SSL_AD_ILLEGAL_PARAMETER;\n SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,\n SSL_R_OLD_SESSION_COMPRESSION_ALGORITHM_NOT_RETURNED);\n goto f_err;\n }\n if (compression == 0)\n comp = NULL;\n else if (!ssl_allow_compression(s)) {\n al = SSL_AD_ILLEGAL_PARAMETER;\n SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_COMPRESSION_DISABLED);\n goto f_err;\n } else {\n comp = ssl3_comp_find(s->ctx->comp_methods, compression);\n }\n if (compression != 0 && comp == NULL) {\n al = SSL_AD_ILLEGAL_PARAMETER;\n SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,\n SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);\n goto f_err;\n } else {\n s->s3->tmp.new_compression = comp;\n }\n#endif\n if (!ssl_parse_serverhello_tlsext(s, pkt)) {\n SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_PARSE_TLSEXT);\n goto err;\n }\n if (PACKET_remaining(pkt) != 0) {\n al = SSL_AD_DECODE_ERROR;\n SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_BAD_PACKET_LENGTH);\n goto f_err;\n }\n#ifndef OPENSSL_NO_SCTP\n if (SSL_IS_DTLS(s) && s->hit) {\n unsigned char sctpauthkey[64];\n char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)];\n memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL,\n sizeof(DTLS1_SCTP_AUTH_LABEL));\n if (SSL_export_keying_material(s, sctpauthkey,\n sizeof(sctpauthkey),\n labelbuffer,\n sizeof(labelbuffer), NULL, 0,\n 0) <= 0)\n goto err;\n BIO_ctrl(SSL_get_wbio(s),\n BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,\n sizeof(sctpauthkey), sctpauthkey);\n }\n#endif\n return MSG_PROCESS_CONTINUE_READING;\n f_err:\n ssl3_send_alert(s, SSL3_AL_FATAL, al);\n err:\n ossl_statem_set_error(s);\n return MSG_PROCESS_ERROR;\n}', 'int ssl3_send_alert(SSL *s, int level, int desc)\n{\n desc = s->method->ssl3_enc->alert_value(desc);\n if (s->version == SSL3_VERSION && desc == SSL_AD_PROTOCOL_VERSION)\n desc = SSL_AD_HANDSHAKE_FAILURE;\n if (desc < 0)\n return -1;\n if ((level == SSL3_AL_FATAL) && (s->session != NULL))\n SSL_CTX_remove_session(s->ctx, s->session);\n s->s3->alert_dispatch = 1;\n s->s3->send_alert[0] = level;\n s->s3->send_alert[1] = desc;\n if (!RECORD_LAYER_write_pending(&s->rlayer)) {\n return s->method->ssl_dispatch_alert(s);\n }\n return -1;\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_w_lock(CRYPTO_LOCK_SSL_CTX);\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 if (lck)\n CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);\n if (ret) {\n r->not_resumable = 1;\n if (ctx->remove_session_cb != NULL)\n ctx->remove_session_cb(ctx, r);\n SSL_SESSION_free(r);\n }\n } else\n ret = 0;\n return (ret);\n}', 'DEFINE_LHASH_OF(SSL_SESSION)', 'void *lh_delete(_LHASH *lh, const void *data)\n{\n unsigned long hash;\n LHASH_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}'] |
35,173 | 0 | https://github.com/openssl/openssl/blob/61f5b6f33807306d09bccbc2dcad474d1d04ca40/crypto/bn/bn_lib.c/#L377 | BIGNUM *bn_expand2(BIGNUM *b, int words)
{
BN_ULONG *A,*B,*a;
int i,j;
bn_check_top(b);
if (words > b->max)
{
bn_check_top(b);
if (BN_get_flags(b,BN_FLG_STATIC_DATA))
{
BNerr(BN_F_BN_EXPAND2,BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);
return(NULL);
}
a=A=(BN_ULONG *)Malloc(sizeof(BN_ULONG)*(words+1));
if (A == NULL)
{
BNerr(BN_F_BN_EXPAND2,ERR_R_MALLOC_FAILURE);
return(NULL);
}
memset(A,0x5c,sizeof(BN_ULONG)*(words+1));
#if 1
B=b->d;
if (B != NULL)
{
for (i=b->top&(~7); i>0; i-=8)
{
A[0]=B[0]; A[1]=B[1]; A[2]=B[2]; A[3]=B[3];
A[4]=B[4]; A[5]=B[5]; A[6]=B[6]; A[7]=B[7];
A+=8;
B+=8;
}
switch (b->top&7)
{
case 7:
A[6]=B[6];
case 6:
A[5]=B[5];
case 5:
A[4]=B[4];
case 4:
A[3]=B[3];
case 3:
A[2]=B[2];
case 2:
A[1]=B[1];
case 1:
A[0]=B[0];
case 0:
;
}
Free(b->d);
}
b->d=a;
b->max=words;
B= &(b->d[b->top]);
j=(b->max - b->top) & ~7;
for (i=0; i<j; i+=8)
{
B[0]=0; B[1]=0; B[2]=0; B[3]=0;
B[4]=0; B[5]=0; B[6]=0; B[7]=0;
B+=8;
}
j=(b->max - b->top) & 7;
for (i=0; i<j; i++)
{
B[0]=0;
B++;
}
#else
memcpy(a->d,b->d,sizeof(b->d[0])*b->top);
#endif
}
return(b);
} | ['int PKCS12_key_gen_uni (unsigned char *pass, int passlen, unsigned char *salt,\n\t int saltlen, int id, int iter, int n, unsigned char *out,\n\t const EVP_MD *md_type)\n{\n\tunsigned char *B, *D, *I, *p, *Ai;\n\tint Slen, Plen, Ilen;\n\tint i, j, u, v;\n\tBIGNUM *Ij, *Bpl1;\n\tEVP_MD_CTX ctx;\n#ifdef DEBUG_KEYGEN\n\tunsigned char *tmpout = out;\n\tint tmpn = n;\n\tBIO_printf (bio_err, "KEYGEN DEBUG\\n");\n\tBIO_printf (bio_err, "ID %d, ITER %d\\n", id, iter);\n\tBIO_printf (bio_err, "Password (length %d):\\n", passlen);\n\th__dump (pass, passlen);\n\tBIO_printf (bio_err, "Salt (length %d):\\n", saltlen);\n\th__dump (salt, saltlen);\n\tBIO_printf (bio_err, "ID %d, ITER %d\\n\\n", id, iter);\n#endif\n\tv = EVP_MD_block_size (md_type);\n\tu = EVP_MD_size (md_type);\n\tD = Malloc (v);\n\tAi = Malloc (u);\n\tB = Malloc (v + 1);\n\tSlen = v * ((saltlen+v-1)/v);\n\tPlen = v * ((passlen+v-1)/v);\n\tIlen = Slen + Plen;\n\tI = Malloc (Ilen);\n\tIj = BN_new();\n\tBpl1 = BN_new();\n\tif (!D || !Ai || !B || !I || !Ij || !Bpl1) {\n\t\tPKCS12err(PKCS12_F_PKCS12_KEY_GEN_UNI,ERR_R_MALLOC_FAILURE);\n\t\treturn 0;\n\t}\n\tfor (i = 0; i < v; i++) D[i] = id;\n\tp = I;\n\tfor (i = 0; i < Slen; i++) *p++ = salt[i % saltlen];\n\tfor (i = 0; i < Plen; i++) *p++ = pass[i % passlen];\n\tfor (;;) {\n\t\tEVP_DigestInit (&ctx, md_type);\n\t\tEVP_DigestUpdate (&ctx, D, v);\n\t\tEVP_DigestUpdate (&ctx, I, Ilen);\n\t\tEVP_DigestFinal (&ctx, Ai, NULL);\n\t\tfor (j = 1; j < iter; j++) {\n\t\t\tEVP_DigestInit (&ctx, md_type);\n\t\t\tEVP_DigestUpdate (&ctx, Ai, u);\n\t\t\tEVP_DigestFinal (&ctx, Ai, NULL);\n\t\t}\n\t\tmemcpy (out, Ai, min (n, u));\n\t\tif (u >= n) {\n\t\t\tFree (Ai);\n\t\t\tFree (B);\n\t\t\tFree (D);\n\t\t\tFree (I);\n\t\t\tBN_free (Ij);\n\t\t\tBN_free (Bpl1);\n#ifdef DEBUG_KEYGEN\n\t\t\tBIO_printf (bio_err, "Output KEY (length %d)\\n", tmpn);\n\t\t\th__dump (tmpout, tmpn);\n#endif\n\t\t\treturn 1;\n\t\t}\n\t\tn -= u;\n\t\tout += u;\n\t\tfor (j = 0; j < v; j++) B[j] = Ai[j % u];\n\t\tBN_bin2bn (B, v, Bpl1);\n\t\tBN_add_word (Bpl1, 1);\n\t\tfor (j = 0; j < Ilen ; j+=v) {\n\t\t\tBN_bin2bn (I + j, v, Ij);\n\t\t\tBN_add (Ij, Ij, Bpl1);\n\t\t\tBN_bn2bin (Ij, B);\n\t\t\tif (BN_num_bytes (Ij) > v) {\n\t\t\t\tBN_bn2bin (Ij, B);\n\t\t\t\tmemcpy (I + j, B + 1, v);\n\t\t\t} else BN_bn2bin (Ij, I + j);\n\t\t}\n\t}\n\treturn 0;\n}', 'BIGNUM *BN_new(void)\n\t{\n\tBIGNUM *ret;\n\tif ((ret=(BIGNUM *)Malloc(sizeof(BIGNUM))) == NULL)\n\t\t{\n\t\tBNerr(BN_F_BN_NEW,ERR_R_MALLOC_FAILURE);\n\t\treturn(NULL);\n\t\t}\n\tret->flags=BN_FLG_MALLOCED;\n\tret->top=0;\n\tret->neg=0;\n\tret->max=0;\n\tret->d=NULL;\n\treturn(ret);\n\t}', '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}', 'BIGNUM *bn_expand2(BIGNUM *b, int words)\n\t{\n\tBN_ULONG *A,*B,*a;\n\tint i,j;\n\tbn_check_top(b);\n\tif (words > b->max)\n\t\t{\n\t\tbn_check_top(b);\n\t\tif (BN_get_flags(b,BN_FLG_STATIC_DATA))\n\t\t\t{\n\t\t\tBNerr(BN_F_BN_EXPAND2,BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);\n\t\t\treturn(NULL);\n\t\t\t}\n\t\ta=A=(BN_ULONG *)Malloc(sizeof(BN_ULONG)*(words+1));\n\t\tif (A == NULL)\n\t\t\t{\n\t\t\tBNerr(BN_F_BN_EXPAND2,ERR_R_MALLOC_FAILURE);\n\t\t\treturn(NULL);\n\t\t\t}\nmemset(A,0x5c,sizeof(BN_ULONG)*(words+1));\n#if 1\n\t\tB=b->d;\n\t\tif (B != NULL)\n\t\t\t{\n\t\t\tfor (i=b->top&(~7); i>0; i-=8)\n\t\t\t\t{\n\t\t\t\tA[0]=B[0]; A[1]=B[1]; A[2]=B[2]; A[3]=B[3];\n\t\t\t\tA[4]=B[4]; A[5]=B[5]; A[6]=B[6]; A[7]=B[7];\n\t\t\t\tA+=8;\n\t\t\t\tB+=8;\n\t\t\t\t}\n\t\t\tswitch (b->top&7)\n\t\t\t\t{\n\t\t\tcase 7:\n\t\t\t\tA[6]=B[6];\n\t\t\tcase 6:\n\t\t\t\tA[5]=B[5];\n\t\t\tcase 5:\n\t\t\t\tA[4]=B[4];\n\t\t\tcase 4:\n\t\t\t\tA[3]=B[3];\n\t\t\tcase 3:\n\t\t\t\tA[2]=B[2];\n\t\t\tcase 2:\n\t\t\t\tA[1]=B[1];\n\t\t\tcase 1:\n\t\t\t\tA[0]=B[0];\n\t\t\tcase 0:\n\t\t\t\t;\n\t\t\t\t}\n\t\t\tFree(b->d);\n\t\t\t}\n\t\tb->d=a;\n\t\tb->max=words;\n\t\tB= &(b->d[b->top]);\n\t\tj=(b->max - b->top) & ~7;\n\t\tfor (i=0; i<j; i+=8)\n\t\t\t{\n\t\t\tB[0]=0; B[1]=0; B[2]=0; B[3]=0;\n\t\t\tB[4]=0; B[5]=0; B[6]=0; B[7]=0;\n\t\t\tB+=8;\n\t\t\t}\n\t\tj=(b->max - b->top) & 7;\n\t\tfor (i=0; i<j; i++)\n\t\t\t{\n\t\t\tB[0]=0;\n\t\t\tB++;\n\t\t\t}\n#else\n\t\t\tmemcpy(a->d,b->d,sizeof(b->d[0])*b->top);\n#endif\n\t\t}\n\treturn(b);\n\t}'] |
35,174 | 0 | https://github.com/libav/libav/blob/72ccfb3cb7a85d35cfe2c99ab53e981974e599cd/libavcodec/dpcm.c/#L255 | static int dpcm_decode_frame(AVCodecContext *avctx, void *data,
int *got_frame_ptr, AVPacket *avpkt)
{
int buf_size = avpkt->size;
DPCMContext *s = avctx->priv_data;
int out = 0, ret;
int predictor[2];
int ch = 0;
int stereo = s->channels - 1;
int16_t *output_samples, *samples_end;
GetByteContext gb;
if (stereo && (buf_size & 1))
buf_size--;
bytestream2_init(&gb, avpkt->data, buf_size);
switch(avctx->codec->id) {
case CODEC_ID_ROQ_DPCM:
out = buf_size - 8;
break;
case CODEC_ID_INTERPLAY_DPCM:
out = buf_size - 6 - s->channels;
break;
case CODEC_ID_XAN_DPCM:
out = buf_size - 2 * s->channels;
break;
case CODEC_ID_SOL_DPCM:
if (avctx->codec_tag != 3)
out = buf_size * 2;
else
out = buf_size;
break;
}
if (out <= 0) {
av_log(avctx, AV_LOG_ERROR, "packet is too small\n");
return AVERROR(EINVAL);
}
s->frame.nb_samples = out / s->channels;
if ((ret = avctx->get_buffer(avctx, &s->frame)) < 0) {
av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
return ret;
}
output_samples = (int16_t *)s->frame.data[0];
samples_end = output_samples + out;
switch(avctx->codec->id) {
case CODEC_ID_ROQ_DPCM:
bytestream2_skipu(&gb, 6);
if (stereo) {
predictor[1] = sign_extend(bytestream2_get_byteu(&gb) << 8, 16);
predictor[0] = sign_extend(bytestream2_get_byteu(&gb) << 8, 16);
} else {
predictor[0] = sign_extend(bytestream2_get_le16u(&gb), 16);
}
while (output_samples < samples_end) {
predictor[ch] += s->roq_square_array[bytestream2_get_byteu(&gb)];
predictor[ch] = av_clip_int16(predictor[ch]);
*output_samples++ = predictor[ch];
ch ^= stereo;
}
break;
case CODEC_ID_INTERPLAY_DPCM:
bytestream2_skipu(&gb, 6);
for (ch = 0; ch < s->channels; ch++) {
predictor[ch] = sign_extend(bytestream2_get_le16u(&gb), 16);
*output_samples++ = predictor[ch];
}
ch = 0;
while (output_samples < samples_end) {
predictor[ch] += interplay_delta_table[bytestream2_get_byteu(&gb)];
predictor[ch] = av_clip_int16(predictor[ch]);
*output_samples++ = predictor[ch];
ch ^= stereo;
}
break;
case CODEC_ID_XAN_DPCM:
{
int shift[2] = { 4, 4 };
for (ch = 0; ch < s->channels; ch++)
predictor[ch] = sign_extend(bytestream2_get_le16u(&gb), 16);
ch = 0;
while (output_samples < samples_end) {
int diff = bytestream2_get_byteu(&gb);
int n = diff & 3;
if (n == 3)
shift[ch]++;
else
shift[ch] -= (2 * n);
diff = sign_extend((diff &~ 3) << 8, 16);
if (shift[ch] < 0)
shift[ch] = 0;
diff >>= shift[ch];
predictor[ch] += diff;
predictor[ch] = av_clip_int16(predictor[ch]);
*output_samples++ = predictor[ch];
ch ^= stereo;
}
break;
}
case CODEC_ID_SOL_DPCM:
if (avctx->codec_tag != 3) {
uint8_t *output_samples_u8 = s->frame.data[0],
*samples_end_u8 = output_samples_u8 + out;
while (output_samples_u8 < samples_end_u8) {
int n = bytestream2_get_byteu(&gb);
s->sample[0] += s->sol_table[n >> 4];
s->sample[0] = av_clip_uint8(s->sample[0]);
*output_samples_u8++ = s->sample[0];
s->sample[stereo] += s->sol_table[n & 0x0F];
s->sample[stereo] = av_clip_uint8(s->sample[stereo]);
*output_samples_u8++ = s->sample[stereo];
}
} else {
while (output_samples < samples_end) {
int n = bytestream2_get_byteu(&gb);
if (n & 0x80) s->sample[ch] -= sol_table_16[n & 0x7F];
else s->sample[ch] += sol_table_16[n & 0x7F];
s->sample[ch] = av_clip_int16(s->sample[ch]);
*output_samples++ = s->sample[ch];
ch ^= stereo;
}
}
break;
}
*got_frame_ptr = 1;
*(AVFrame *)data = s->frame;
return avpkt->size;
} | ['static int dpcm_decode_frame(AVCodecContext *avctx, void *data,\n int *got_frame_ptr, AVPacket *avpkt)\n{\n int buf_size = avpkt->size;\n DPCMContext *s = avctx->priv_data;\n int out = 0, ret;\n int predictor[2];\n int ch = 0;\n int stereo = s->channels - 1;\n int16_t *output_samples, *samples_end;\n GetByteContext gb;\n if (stereo && (buf_size & 1))\n buf_size--;\n bytestream2_init(&gb, avpkt->data, buf_size);\n switch(avctx->codec->id) {\n case CODEC_ID_ROQ_DPCM:\n out = buf_size - 8;\n break;\n case CODEC_ID_INTERPLAY_DPCM:\n out = buf_size - 6 - s->channels;\n break;\n case CODEC_ID_XAN_DPCM:\n out = buf_size - 2 * s->channels;\n break;\n case CODEC_ID_SOL_DPCM:\n if (avctx->codec_tag != 3)\n out = buf_size * 2;\n else\n out = buf_size;\n break;\n }\n if (out <= 0) {\n av_log(avctx, AV_LOG_ERROR, "packet is too small\\n");\n return AVERROR(EINVAL);\n }\n s->frame.nb_samples = out / s->channels;\n if ((ret = avctx->get_buffer(avctx, &s->frame)) < 0) {\n av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\\n");\n return ret;\n }\n output_samples = (int16_t *)s->frame.data[0];\n samples_end = output_samples + out;\n switch(avctx->codec->id) {\n case CODEC_ID_ROQ_DPCM:\n bytestream2_skipu(&gb, 6);\n if (stereo) {\n predictor[1] = sign_extend(bytestream2_get_byteu(&gb) << 8, 16);\n predictor[0] = sign_extend(bytestream2_get_byteu(&gb) << 8, 16);\n } else {\n predictor[0] = sign_extend(bytestream2_get_le16u(&gb), 16);\n }\n while (output_samples < samples_end) {\n predictor[ch] += s->roq_square_array[bytestream2_get_byteu(&gb)];\n predictor[ch] = av_clip_int16(predictor[ch]);\n *output_samples++ = predictor[ch];\n ch ^= stereo;\n }\n break;\n case CODEC_ID_INTERPLAY_DPCM:\n bytestream2_skipu(&gb, 6);\n for (ch = 0; ch < s->channels; ch++) {\n predictor[ch] = sign_extend(bytestream2_get_le16u(&gb), 16);\n *output_samples++ = predictor[ch];\n }\n ch = 0;\n while (output_samples < samples_end) {\n predictor[ch] += interplay_delta_table[bytestream2_get_byteu(&gb)];\n predictor[ch] = av_clip_int16(predictor[ch]);\n *output_samples++ = predictor[ch];\n ch ^= stereo;\n }\n break;\n case CODEC_ID_XAN_DPCM:\n {\n int shift[2] = { 4, 4 };\n for (ch = 0; ch < s->channels; ch++)\n predictor[ch] = sign_extend(bytestream2_get_le16u(&gb), 16);\n ch = 0;\n while (output_samples < samples_end) {\n int diff = bytestream2_get_byteu(&gb);\n int n = diff & 3;\n if (n == 3)\n shift[ch]++;\n else\n shift[ch] -= (2 * n);\n diff = sign_extend((diff &~ 3) << 8, 16);\n if (shift[ch] < 0)\n shift[ch] = 0;\n diff >>= shift[ch];\n predictor[ch] += diff;\n predictor[ch] = av_clip_int16(predictor[ch]);\n *output_samples++ = predictor[ch];\n ch ^= stereo;\n }\n break;\n }\n case CODEC_ID_SOL_DPCM:\n if (avctx->codec_tag != 3) {\n uint8_t *output_samples_u8 = s->frame.data[0],\n *samples_end_u8 = output_samples_u8 + out;\n while (output_samples_u8 < samples_end_u8) {\n int n = bytestream2_get_byteu(&gb);\n s->sample[0] += s->sol_table[n >> 4];\n s->sample[0] = av_clip_uint8(s->sample[0]);\n *output_samples_u8++ = s->sample[0];\n s->sample[stereo] += s->sol_table[n & 0x0F];\n s->sample[stereo] = av_clip_uint8(s->sample[stereo]);\n *output_samples_u8++ = s->sample[stereo];\n }\n } else {\n while (output_samples < samples_end) {\n int n = bytestream2_get_byteu(&gb);\n if (n & 0x80) s->sample[ch] -= sol_table_16[n & 0x7F];\n else s->sample[ch] += sol_table_16[n & 0x7F];\n s->sample[ch] = av_clip_int16(s->sample[ch]);\n *output_samples++ = s->sample[ch];\n ch ^= stereo;\n }\n }\n break;\n }\n *got_frame_ptr = 1;\n *(AVFrame *)data = s->frame;\n return avpkt->size;\n}'] |
35,175 | 0 | https://github.com/libav/libav/blob/8eeacf31c5ea37baf6b222dc38d20cf4fd33c455/avconv.c/#L1395 | static enum AVPixelFormat get_format(AVCodecContext *s, const enum AVPixelFormat *pix_fmts)
{
InputStream *ist = s->opaque;
const enum AVPixelFormat *p;
int ret;
for (p = pix_fmts; *p != -1; p++) {
const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(*p);
const HWAccel *hwaccel;
if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL))
break;
hwaccel = get_hwaccel(*p);
if (!hwaccel ||
(ist->active_hwaccel_id && ist->active_hwaccel_id != hwaccel->id) ||
(ist->hwaccel_id != HWACCEL_AUTO && ist->hwaccel_id != hwaccel->id))
continue;
ret = hwaccel->init(s);
if (ret < 0) {
if (ist->hwaccel_id == hwaccel->id) {
av_log(NULL, AV_LOG_FATAL,
"%s hwaccel requested for input stream #%d:%d, "
"but cannot be initialized.\n", hwaccel->name,
ist->file_index, ist->st->index);
exit_program(1);
}
continue;
}
ist->active_hwaccel_id = hwaccel->id;
ist->hwaccel_pix_fmt = *p;
break;
}
return *p;
} | ['static enum AVPixelFormat get_format(AVCodecContext *s, const enum AVPixelFormat *pix_fmts)\n{\n InputStream *ist = s->opaque;\n const enum AVPixelFormat *p;\n int ret;\n for (p = pix_fmts; *p != -1; p++) {\n const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(*p);\n const HWAccel *hwaccel;\n if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL))\n break;\n hwaccel = get_hwaccel(*p);\n if (!hwaccel ||\n (ist->active_hwaccel_id && ist->active_hwaccel_id != hwaccel->id) ||\n (ist->hwaccel_id != HWACCEL_AUTO && ist->hwaccel_id != hwaccel->id))\n continue;\n ret = hwaccel->init(s);\n if (ret < 0) {\n if (ist->hwaccel_id == hwaccel->id) {\n av_log(NULL, AV_LOG_FATAL,\n "%s hwaccel requested for input stream #%d:%d, "\n "but cannot be initialized.\\n", hwaccel->name,\n ist->file_index, ist->st->index);\n exit_program(1);\n }\n continue;\n }\n ist->active_hwaccel_id = hwaccel->id;\n ist->hwaccel_pix_fmt = *p;\n break;\n }\n return *p;\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}'] |
35,176 | 0 | https://github.com/libav/libav/blob/03f8fc0897c128028111182e6276139fa00b891b/libavcodec/smacker.c/#L313 | static int decode_header_trees(SmackVContext *smk) {
GetBitContext gb;
int mmap_size, mclr_size, full_size, type_size;
mmap_size = AV_RL32(smk->avctx->extradata);
mclr_size = AV_RL32(smk->avctx->extradata + 4);
full_size = AV_RL32(smk->avctx->extradata + 8);
type_size = AV_RL32(smk->avctx->extradata + 12);
init_get_bits(&gb, smk->avctx->extradata + 16, (smk->avctx->extradata_size - 16) * 8);
if(!get_bits1(&gb)) {
av_log(smk->avctx, AV_LOG_INFO, "Skipping MMAP tree\n");
smk->mmap_tbl = av_malloc(sizeof(int) * 2);
smk->mmap_tbl[0] = 0;
smk->mmap_last[0] = smk->mmap_last[1] = smk->mmap_last[2] = 1;
} else {
smacker_decode_header_tree(smk, &gb, &smk->mmap_tbl, smk->mmap_last, mmap_size);
}
if(!get_bits1(&gb)) {
av_log(smk->avctx, AV_LOG_INFO, "Skipping MCLR tree\n");
smk->mclr_tbl = av_malloc(sizeof(int) * 2);
smk->mclr_tbl[0] = 0;
smk->mclr_last[0] = smk->mclr_last[1] = smk->mclr_last[2] = 1;
} else {
smacker_decode_header_tree(smk, &gb, &smk->mclr_tbl, smk->mclr_last, mclr_size);
}
if(!get_bits1(&gb)) {
av_log(smk->avctx, AV_LOG_INFO, "Skipping FULL tree\n");
smk->full_tbl = av_malloc(sizeof(int) * 2);
smk->full_tbl[0] = 0;
smk->full_last[0] = smk->full_last[1] = smk->full_last[2] = 1;
} else {
smacker_decode_header_tree(smk, &gb, &smk->full_tbl, smk->full_last, full_size);
}
if(!get_bits1(&gb)) {
av_log(smk->avctx, AV_LOG_INFO, "Skipping TYPE tree\n");
smk->type_tbl = av_malloc(sizeof(int) * 2);
smk->type_tbl[0] = 0;
smk->type_last[0] = smk->type_last[1] = smk->type_last[2] = 1;
} else {
smacker_decode_header_tree(smk, &gb, &smk->type_tbl, smk->type_last, type_size);
}
return 0;
} | ['static int decode_header_trees(SmackVContext *smk) {\n GetBitContext gb;\n int mmap_size, mclr_size, full_size, type_size;\n mmap_size = AV_RL32(smk->avctx->extradata);\n mclr_size = AV_RL32(smk->avctx->extradata + 4);\n full_size = AV_RL32(smk->avctx->extradata + 8);\n type_size = AV_RL32(smk->avctx->extradata + 12);\n init_get_bits(&gb, smk->avctx->extradata + 16, (smk->avctx->extradata_size - 16) * 8);\n if(!get_bits1(&gb)) {\n av_log(smk->avctx, AV_LOG_INFO, "Skipping MMAP tree\\n");\n smk->mmap_tbl = av_malloc(sizeof(int) * 2);\n smk->mmap_tbl[0] = 0;\n smk->mmap_last[0] = smk->mmap_last[1] = smk->mmap_last[2] = 1;\n } else {\n smacker_decode_header_tree(smk, &gb, &smk->mmap_tbl, smk->mmap_last, mmap_size);\n }\n if(!get_bits1(&gb)) {\n av_log(smk->avctx, AV_LOG_INFO, "Skipping MCLR tree\\n");\n smk->mclr_tbl = av_malloc(sizeof(int) * 2);\n smk->mclr_tbl[0] = 0;\n smk->mclr_last[0] = smk->mclr_last[1] = smk->mclr_last[2] = 1;\n } else {\n smacker_decode_header_tree(smk, &gb, &smk->mclr_tbl, smk->mclr_last, mclr_size);\n }\n if(!get_bits1(&gb)) {\n av_log(smk->avctx, AV_LOG_INFO, "Skipping FULL tree\\n");\n smk->full_tbl = av_malloc(sizeof(int) * 2);\n smk->full_tbl[0] = 0;\n smk->full_last[0] = smk->full_last[1] = smk->full_last[2] = 1;\n } else {\n smacker_decode_header_tree(smk, &gb, &smk->full_tbl, smk->full_last, full_size);\n }\n if(!get_bits1(&gb)) {\n av_log(smk->avctx, AV_LOG_INFO, "Skipping TYPE tree\\n");\n smk->type_tbl = av_malloc(sizeof(int) * 2);\n smk->type_tbl[0] = 0;\n smk->type_last[0] = smk->type_last[1] = smk->type_last[2] = 1;\n } else {\n smacker_decode_header_tree(smk, &gb, &smk->type_tbl, smk->type_last, type_size);\n }\n return 0;\n}', 'static inline void init_get_bits(GetBitContext *s,\n const uint8_t *buffer, int bit_size)\n{\n int buffer_size= (bit_size+7)>>3;\n if(buffer_size < 0 || bit_size < 0) {\n buffer_size = bit_size = 0;\n buffer = NULL;\n }\n s->buffer= buffer;\n s->size_in_bits= bit_size;\n s->buffer_end= buffer + buffer_size;\n#ifdef ALT_BITSTREAM_READER\n s->index=0;\n#elif defined LIBMPEG2_BITSTREAM_READER\n s->buffer_ptr = (uint8_t*)((intptr_t)buffer&(~1));\n s->bit_count = 16 + 8*((intptr_t)buffer&1);\n skip_bits_long(s, 0);\n#elif defined A32_BITSTREAM_READER\n s->buffer_ptr = (uint32_t*)((intptr_t)buffer&(~3));\n s->bit_count = 32 + 8*((intptr_t)buffer&3);\n skip_bits_long(s, 0);\n#endif\n}', '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&0x07);\n result&= 1;\n#else\n result<<= (index&0x07);\n result>>= 8 - 1;\n#endif\n index++;\n s->index= index;\n return result;\n#else\n return get_bits(s, 1);\n#endif\n}', 'void *av_malloc(unsigned int size)\n{\n void *ptr = NULL;\n#if CONFIG_MEMALIGN_HACK\n long diff;\n#endif\n if(size > (INT_MAX-16) )\n return NULL;\n#if CONFIG_MEMALIGN_HACK\n ptr = malloc(size+16);\n if(!ptr)\n return ptr;\n diff= ((-(long)ptr - 1)&15) + 1;\n ptr = (char*)ptr + diff;\n ((char*)ptr)[-1]= diff;\n#elif HAVE_POSIX_MEMALIGN\n if (posix_memalign(&ptr,16,size))\n ptr = NULL;\n#elif HAVE_MEMALIGN\n ptr = memalign(16,size);\n#else\n ptr = malloc(size);\n#endif\n return ptr;\n}'] |
35,177 | 0 | https://github.com/libav/libav/blob/f5a2c9816e0b58edf2a87297be8d648631fc3432/libavcodec/dv.c/#L510 | static inline void dv_decode_video_segment(DVVideoContext *s,
const uint8_t *buf_ptr1,
const uint16_t *mb_pos_ptr)
{
int quant, dc, dct_mode, class1, j;
int mb_index, mb_x, mb_y, v, last_index;
int y_stride, linesize;
DCTELEM *block, *block1;
int c_offset;
uint8_t *y_ptr;
const uint8_t *buf_ptr;
PutBitContext pb, vs_pb;
GetBitContext gb;
BlockInfo mb_data[5 * DV_MAX_BPM], *mb, *mb1;
DECLARE_ALIGNED_16(DCTELEM, sblock[5*DV_MAX_BPM][64]);
DECLARE_ALIGNED_8(uint8_t, mb_bit_buffer[80 + 4]);
DECLARE_ALIGNED_8(uint8_t, vs_bit_buffer[5 * 80 + 4]);
const int log2_blocksize= 3-s->avctx->lowres;
int is_field_mode[5];
assert((((int)mb_bit_buffer)&7)==0);
assert((((int)vs_bit_buffer)&7)==0);
memset(sblock, 0, sizeof(sblock));
buf_ptr = buf_ptr1;
block1 = &sblock[0][0];
mb1 = mb_data;
init_put_bits(&vs_pb, vs_bit_buffer, 5 * 80);
for(mb_index = 0; mb_index < 5; mb_index++, mb1 += s->sys->bpm, block1 += s->sys->bpm * 64) {
quant = buf_ptr[3] & 0x0f;
buf_ptr += 4;
init_put_bits(&pb, mb_bit_buffer, 80);
mb = mb1;
block = block1;
is_field_mode[mb_index] = 0;
for(j = 0;j < s->sys->bpm; j++) {
last_index = s->sys->block_sizes[j];
init_get_bits(&gb, buf_ptr, last_index);
dc = get_sbits(&gb, 9);
dct_mode = get_bits1(&gb);
class1 = get_bits(&gb, 2);
if (DV_PROFILE_IS_HD(s->sys)) {
mb->idct_put = s->idct_put[0];
mb->scan_table = s->dv_zigzag[0];
mb->factor_table = s->dv100_idct_factor[((s->sys->height == 720)<<1)|(j >= 4)][class1][quant];
is_field_mode[mb_index] |= !j && dct_mode;
} else {
mb->idct_put = s->idct_put[dct_mode && log2_blocksize==3];
mb->scan_table = s->dv_zigzag[dct_mode];
mb->factor_table = s->dv_idct_factor[class1 == 3][dct_mode]
[quant + dv_quant_offset[class1]];
}
dc = dc << 2;
dc += 1024;
block[0] = dc;
buf_ptr += last_index >> 3;
mb->pos = 0;
mb->partial_bit_count = 0;
#ifdef VLC_DEBUG
printf("MB block: %d, %d ", mb_index, j);
#endif
dv_decode_ac(&gb, mb, block);
if (mb->pos >= 64)
bit_copy(&pb, &gb);
block += 64;
mb++;
}
#ifdef VLC_DEBUG
printf("***pass 2 size=%d MB#=%d\n", put_bits_count(&pb), mb_index);
#endif
block = block1;
mb = mb1;
init_get_bits(&gb, mb_bit_buffer, put_bits_count(&pb));
flush_put_bits(&pb);
for(j = 0;j < s->sys->bpm; j++, block += 64, mb++) {
if (mb->pos < 64 && get_bits_left(&gb) > 0) {
dv_decode_ac(&gb, mb, block);
if (mb->pos < 64)
break;
}
}
if (j >= s->sys->bpm)
bit_copy(&vs_pb, &gb);
}
#ifdef VLC_DEBUG
printf("***pass 3 size=%d\n", put_bits_count(&vs_pb));
#endif
block = &sblock[0][0];
mb = mb_data;
init_get_bits(&gb, vs_bit_buffer, put_bits_count(&vs_pb));
flush_put_bits(&vs_pb);
for(mb_index = 0; mb_index < 5; mb_index++) {
for(j = 0;j < s->sys->bpm; j++) {
if (mb->pos < 64) {
#ifdef VLC_DEBUG
printf("start %d:%d\n", mb_index, j);
#endif
dv_decode_ac(&gb, mb, block);
}
if (mb->pos >= 64 && mb->pos < 127)
av_log(NULL, AV_LOG_ERROR, "AC EOB marker is absent pos=%d\n", mb->pos);
block += 64;
mb++;
}
}
block = &sblock[0][0];
mb = mb_data;
for(mb_index = 0; mb_index < 5; mb_index++) {
v = *mb_pos_ptr++;
mb_x = v & 0xff;
mb_y = v >> 8;
if (s->sys->height == 720 && !(s->buf[1]&0x0C)) {
mb_y -= (mb_y>17)?18:-72;
}
if ((s->sys->pix_fmt == PIX_FMT_YUV420P) ||
(s->sys->pix_fmt == PIX_FMT_YUV411P && mb_x >= (704 / 8)) ||
(s->sys->height >= 720 && mb_y != 134)) {
y_stride = (s->picture.linesize[0]<<((!is_field_mode[mb_index])*log2_blocksize));
} else {
y_stride = (2<<log2_blocksize);
}
y_ptr = s->picture.data[0] + ((mb_y * s->picture.linesize[0] + mb_x)<<log2_blocksize);
linesize = s->picture.linesize[0]<<is_field_mode[mb_index];
mb[0] .idct_put(y_ptr , linesize, block + 0*64);
if (s->sys->video_stype == 4) {
mb[2].idct_put(y_ptr + (1<<log2_blocksize) , linesize, block + 2*64);
} else {
mb[1].idct_put(y_ptr + (1<<log2_blocksize) , linesize, block + 1*64);
mb[2].idct_put(y_ptr + y_stride, linesize, block + 2*64);
mb[3].idct_put(y_ptr + (1<<log2_blocksize) + y_stride, linesize, block + 3*64);
}
mb += 4;
block += 4*64;
c_offset = (((mb_y>>(s->sys->pix_fmt == PIX_FMT_YUV420P)) * s->picture.linesize[1] +
(mb_x>>((s->sys->pix_fmt == PIX_FMT_YUV411P)?2:1)))<<log2_blocksize);
for(j=2; j; j--) {
uint8_t *c_ptr = s->picture.data[j] + c_offset;
if (s->sys->pix_fmt == PIX_FMT_YUV411P && mb_x >= (704 / 8)) {
uint64_t aligned_pixels[64/8];
uint8_t *pixels = (uint8_t*)aligned_pixels;
uint8_t *c_ptr1, *ptr1;
int x, y;
mb->idct_put(pixels, 8, block);
for(y = 0; y < (1<<log2_blocksize); y++, c_ptr += s->picture.linesize[j], pixels += 8) {
ptr1= pixels + (1<<(log2_blocksize-1));
c_ptr1 = c_ptr + (s->picture.linesize[j]<<log2_blocksize);
for(x=0; x < (1<<(log2_blocksize-1)); x++) {
c_ptr[x]= pixels[x];
c_ptr1[x]= ptr1[x];
}
}
block += 64; mb++;
} else {
y_stride = (mb_y == 134) ? (1<<log2_blocksize) :
s->picture.linesize[j]<<((!is_field_mode[mb_index])*log2_blocksize);
linesize = s->picture.linesize[j]<<is_field_mode[mb_index];
(mb++)-> idct_put(c_ptr , linesize, block); block+=64;
if (s->sys->bpm == 8) {
(mb++)->idct_put(c_ptr + y_stride, linesize, block); block+=64;
}
}
}
}
} | ['static inline void dv_decode_video_segment(DVVideoContext *s,\n const uint8_t *buf_ptr1,\n const uint16_t *mb_pos_ptr)\n{\n int quant, dc, dct_mode, class1, j;\n int mb_index, mb_x, mb_y, v, last_index;\n int y_stride, linesize;\n DCTELEM *block, *block1;\n int c_offset;\n uint8_t *y_ptr;\n const uint8_t *buf_ptr;\n PutBitContext pb, vs_pb;\n GetBitContext gb;\n BlockInfo mb_data[5 * DV_MAX_BPM], *mb, *mb1;\n DECLARE_ALIGNED_16(DCTELEM, sblock[5*DV_MAX_BPM][64]);\n DECLARE_ALIGNED_8(uint8_t, mb_bit_buffer[80 + 4]);\n DECLARE_ALIGNED_8(uint8_t, vs_bit_buffer[5 * 80 + 4]);\n const int log2_blocksize= 3-s->avctx->lowres;\n int is_field_mode[5];\n assert((((int)mb_bit_buffer)&7)==0);\n assert((((int)vs_bit_buffer)&7)==0);\n memset(sblock, 0, sizeof(sblock));\n buf_ptr = buf_ptr1;\n block1 = &sblock[0][0];\n mb1 = mb_data;\n init_put_bits(&vs_pb, vs_bit_buffer, 5 * 80);\n for(mb_index = 0; mb_index < 5; mb_index++, mb1 += s->sys->bpm, block1 += s->sys->bpm * 64) {\n quant = buf_ptr[3] & 0x0f;\n buf_ptr += 4;\n init_put_bits(&pb, mb_bit_buffer, 80);\n mb = mb1;\n block = block1;\n is_field_mode[mb_index] = 0;\n for(j = 0;j < s->sys->bpm; j++) {\n last_index = s->sys->block_sizes[j];\n init_get_bits(&gb, buf_ptr, last_index);\n dc = get_sbits(&gb, 9);\n dct_mode = get_bits1(&gb);\n class1 = get_bits(&gb, 2);\n if (DV_PROFILE_IS_HD(s->sys)) {\n mb->idct_put = s->idct_put[0];\n mb->scan_table = s->dv_zigzag[0];\n mb->factor_table = s->dv100_idct_factor[((s->sys->height == 720)<<1)|(j >= 4)][class1][quant];\n is_field_mode[mb_index] |= !j && dct_mode;\n } else {\n mb->idct_put = s->idct_put[dct_mode && log2_blocksize==3];\n mb->scan_table = s->dv_zigzag[dct_mode];\n mb->factor_table = s->dv_idct_factor[class1 == 3][dct_mode]\n [quant + dv_quant_offset[class1]];\n }\n dc = dc << 2;\n dc += 1024;\n block[0] = dc;\n buf_ptr += last_index >> 3;\n mb->pos = 0;\n mb->partial_bit_count = 0;\n#ifdef VLC_DEBUG\n printf("MB block: %d, %d ", mb_index, j);\n#endif\n dv_decode_ac(&gb, mb, block);\n if (mb->pos >= 64)\n bit_copy(&pb, &gb);\n block += 64;\n mb++;\n }\n#ifdef VLC_DEBUG\n printf("***pass 2 size=%d MB#=%d\\n", put_bits_count(&pb), mb_index);\n#endif\n block = block1;\n mb = mb1;\n init_get_bits(&gb, mb_bit_buffer, put_bits_count(&pb));\n flush_put_bits(&pb);\n for(j = 0;j < s->sys->bpm; j++, block += 64, mb++) {\n if (mb->pos < 64 && get_bits_left(&gb) > 0) {\n dv_decode_ac(&gb, mb, block);\n if (mb->pos < 64)\n break;\n }\n }\n if (j >= s->sys->bpm)\n bit_copy(&vs_pb, &gb);\n }\n#ifdef VLC_DEBUG\n printf("***pass 3 size=%d\\n", put_bits_count(&vs_pb));\n#endif\n block = &sblock[0][0];\n mb = mb_data;\n init_get_bits(&gb, vs_bit_buffer, put_bits_count(&vs_pb));\n flush_put_bits(&vs_pb);\n for(mb_index = 0; mb_index < 5; mb_index++) {\n for(j = 0;j < s->sys->bpm; j++) {\n if (mb->pos < 64) {\n#ifdef VLC_DEBUG\n printf("start %d:%d\\n", mb_index, j);\n#endif\n dv_decode_ac(&gb, mb, block);\n }\n if (mb->pos >= 64 && mb->pos < 127)\n av_log(NULL, AV_LOG_ERROR, "AC EOB marker is absent pos=%d\\n", mb->pos);\n block += 64;\n mb++;\n }\n }\n block = &sblock[0][0];\n mb = mb_data;\n for(mb_index = 0; mb_index < 5; mb_index++) {\n v = *mb_pos_ptr++;\n mb_x = v & 0xff;\n mb_y = v >> 8;\n if (s->sys->height == 720 && !(s->buf[1]&0x0C)) {\n mb_y -= (mb_y>17)?18:-72;\n }\n if ((s->sys->pix_fmt == PIX_FMT_YUV420P) ||\n (s->sys->pix_fmt == PIX_FMT_YUV411P && mb_x >= (704 / 8)) ||\n (s->sys->height >= 720 && mb_y != 134)) {\n y_stride = (s->picture.linesize[0]<<((!is_field_mode[mb_index])*log2_blocksize));\n } else {\n y_stride = (2<<log2_blocksize);\n }\n y_ptr = s->picture.data[0] + ((mb_y * s->picture.linesize[0] + mb_x)<<log2_blocksize);\n linesize = s->picture.linesize[0]<<is_field_mode[mb_index];\n mb[0] .idct_put(y_ptr , linesize, block + 0*64);\n if (s->sys->video_stype == 4) {\n mb[2].idct_put(y_ptr + (1<<log2_blocksize) , linesize, block + 2*64);\n } else {\n mb[1].idct_put(y_ptr + (1<<log2_blocksize) , linesize, block + 1*64);\n mb[2].idct_put(y_ptr + y_stride, linesize, block + 2*64);\n mb[3].idct_put(y_ptr + (1<<log2_blocksize) + y_stride, linesize, block + 3*64);\n }\n mb += 4;\n block += 4*64;\n c_offset = (((mb_y>>(s->sys->pix_fmt == PIX_FMT_YUV420P)) * s->picture.linesize[1] +\n (mb_x>>((s->sys->pix_fmt == PIX_FMT_YUV411P)?2:1)))<<log2_blocksize);\n for(j=2; j; j--) {\n uint8_t *c_ptr = s->picture.data[j] + c_offset;\n if (s->sys->pix_fmt == PIX_FMT_YUV411P && mb_x >= (704 / 8)) {\n uint64_t aligned_pixels[64/8];\n uint8_t *pixels = (uint8_t*)aligned_pixels;\n uint8_t *c_ptr1, *ptr1;\n int x, y;\n mb->idct_put(pixels, 8, block);\n for(y = 0; y < (1<<log2_blocksize); y++, c_ptr += s->picture.linesize[j], pixels += 8) {\n ptr1= pixels + (1<<(log2_blocksize-1));\n c_ptr1 = c_ptr + (s->picture.linesize[j]<<log2_blocksize);\n for(x=0; x < (1<<(log2_blocksize-1)); x++) {\n c_ptr[x]= pixels[x];\n c_ptr1[x]= ptr1[x];\n }\n }\n block += 64; mb++;\n } else {\n y_stride = (mb_y == 134) ? (1<<log2_blocksize) :\n s->picture.linesize[j]<<((!is_field_mode[mb_index])*log2_blocksize);\n linesize = s->picture.linesize[j]<<is_field_mode[mb_index];\n (mb++)-> idct_put(c_ptr , linesize, block); block+=64;\n if (s->sys->bpm == 8) {\n (mb++)->idct_put(c_ptr + y_stride, linesize, block); block+=64;\n }\n }\n }\n }\n}'] |
35,178 | 0 | https://github.com/openssl/openssl/blob/e02c519cd32a55e6ad39a0cfbeeda775f9115f28/crypto/bn/bn_sqr.c/#L124 | void bn_sqr_normal(BN_ULONG *r, const BN_ULONG *a, int n, BN_ULONG *tmp)
{
int i, j, max;
const BN_ULONG *ap;
BN_ULONG *rp;
max = n * 2;
ap = a;
rp = r;
rp[0] = rp[max - 1] = 0;
rp++;
j = n;
if (--j > 0) {
ap++;
rp[j] = bn_mul_words(rp, ap, j, ap[-1]);
rp += 2;
}
for (i = n - 2; i > 0; i--) {
j--;
ap++;
rp[j] = bn_mul_add_words(rp, ap, j, ap[-1]);
rp += 2;
}
bn_add_words(r, r, r, max);
bn_sqr_words(tmp, a, n);
bn_add_words(r, r, tmp, max);
} | ['int BN_mod_exp_mont(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p,\n const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont)\n{\n int i, j, bits, ret = 0, wstart, wend, window, wvalue;\n int start = 1;\n BIGNUM *d, *r;\n const BIGNUM *aa;\n BIGNUM *val[TABLE_SIZE];\n BN_MONT_CTX *mont = NULL;\n if (BN_get_flags(p, BN_FLG_CONSTTIME) != 0\n || BN_get_flags(a, BN_FLG_CONSTTIME) != 0\n || BN_get_flags(m, BN_FLG_CONSTTIME) != 0) {\n return BN_mod_exp_mont_consttime(rr, a, p, m, ctx, in_mont);\n }\n bn_check_top(a);\n bn_check_top(p);\n bn_check_top(m);\n if (!BN_is_odd(m)) {\n BNerr(BN_F_BN_MOD_EXP_MONT, BN_R_CALLED_WITH_EVEN_MODULUS);\n return 0;\n }\n bits = BN_num_bits(p);\n if (bits == 0) {\n if (BN_abs_is_word(m, 1)) {\n ret = 1;\n BN_zero(rr);\n } else {\n ret = BN_one(rr);\n }\n return ret;\n }\n BN_CTX_start(ctx);\n d = BN_CTX_get(ctx);\n r = BN_CTX_get(ctx);\n val[0] = BN_CTX_get(ctx);\n if (val[0] == NULL)\n goto err;\n if (in_mont != NULL)\n mont = in_mont;\n else {\n if ((mont = BN_MONT_CTX_new()) == NULL)\n goto err;\n if (!BN_MONT_CTX_set(mont, m, ctx))\n goto err;\n }\n if (a->neg || BN_ucmp(a, m) >= 0) {\n if (!BN_nnmod(val[0], a, m, ctx))\n goto err;\n aa = val[0];\n } else\n aa = a;\n if (!bn_to_mont_fixed_top(val[0], aa, mont, ctx))\n goto err;\n window = BN_window_bits_for_exponent_size(bits);\n if (window > 1) {\n if (!bn_mul_mont_fixed_top(d, val[0], val[0], mont, ctx))\n goto err;\n j = 1 << (window - 1);\n for (i = 1; i < j; i++) {\n if (((val[i] = BN_CTX_get(ctx)) == NULL) ||\n !bn_mul_mont_fixed_top(val[i], val[i - 1], d, mont, ctx))\n goto err;\n }\n }\n start = 1;\n wvalue = 0;\n wstart = bits - 1;\n wend = 0;\n#if 1\n j = m->top;\n if (m->d[j - 1] & (((BN_ULONG)1) << (BN_BITS2 - 1))) {\n if (bn_wexpand(r, j) == NULL)\n goto err;\n r->d[0] = (0 - m->d[0]) & BN_MASK2;\n for (i = 1; i < j; i++)\n r->d[i] = (~m->d[i]) & BN_MASK2;\n r->top = j;\n r->flags |= BN_FLG_FIXED_TOP;\n } else\n#endif\n if (!bn_to_mont_fixed_top(r, BN_value_one(), mont, ctx))\n goto err;\n for (;;) {\n if (BN_is_bit_set(p, wstart) == 0) {\n if (!start) {\n if (!bn_mul_mont_fixed_top(r, r, r, mont, ctx))\n goto err;\n }\n if (wstart == 0)\n break;\n wstart--;\n continue;\n }\n j = wstart;\n wvalue = 1;\n wend = 0;\n for (i = 1; i < window; i++) {\n if (wstart - i < 0)\n break;\n if (BN_is_bit_set(p, wstart - i)) {\n wvalue <<= (i - wend);\n wvalue |= 1;\n wend = i;\n }\n }\n j = wend + 1;\n if (!start)\n for (i = 0; i < j; i++) {\n if (!bn_mul_mont_fixed_top(r, r, r, mont, ctx))\n goto err;\n }\n if (!bn_mul_mont_fixed_top(r, r, val[wvalue >> 1], mont, ctx))\n goto err;\n wstart -= wend + 1;\n wvalue = 0;\n start = 0;\n if (wstart < 0)\n break;\n }\n#if defined(SPARC_T4_MONT)\n if (OPENSSL_sparcv9cap_P[0] & (SPARCV9_VIS3 | SPARCV9_PREFER_FPU)) {\n j = mont->N.top;\n val[0]->d[0] = 1;\n for (i = 1; i < j; i++)\n val[0]->d[i] = 0;\n val[0]->top = j;\n if (!BN_mod_mul_montgomery(rr, r, val[0], mont, ctx))\n goto err;\n } else\n#endif\n if (!BN_from_montgomery(rr, r, mont, ctx))\n goto err;\n ret = 1;\n err:\n if (in_mont == NULL)\n BN_MONT_CTX_free(mont);\n BN_CTX_end(ctx);\n bn_check_top(rr);\n return ret;\n}', 'int bn_to_mont_fixed_top(BIGNUM *r, const BIGNUM *a, BN_MONT_CTX *mont,\n BN_CTX *ctx)\n{\n return bn_mul_mont_fixed_top(r, a, &(mont->RR), mont, ctx);\n}', 'int bn_mul_mont_fixed_top(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,\n BN_MONT_CTX *mont, BN_CTX *ctx)\n{\n BIGNUM *tmp;\n int ret = 0;\n int num = mont->N.top;\n#if defined(OPENSSL_BN_ASM_MONT) && defined(MONT_WORD)\n if (num > 1 && a->top == num && b->top == num) {\n if (bn_wexpand(r, num) == NULL)\n return 0;\n if (bn_mul_mont(r->d, a->d, b->d, mont->N.d, mont->n0, num)) {\n r->neg = a->neg ^ b->neg;\n r->top = num;\n r->flags |= BN_FLG_FIXED_TOP;\n return 1;\n }\n }\n#endif\n if ((a->top + b->top) > 2 * num)\n return 0;\n BN_CTX_start(ctx);\n tmp = BN_CTX_get(ctx);\n if (tmp == NULL)\n goto err;\n bn_check_top(tmp);\n if (a == b) {\n if (!bn_sqr_fixed_top(tmp, a, ctx))\n goto err;\n } else {\n if (!bn_mul_fixed_top(tmp, a, b, ctx))\n goto err;\n }\n#ifdef MONT_WORD\n if (!bn_from_montgomery_word(r, tmp, mont))\n goto err;\n#else\n if (!BN_from_montgomery(r, tmp, mont, ctx))\n goto err;\n#endif\n ret = 1;\n err:\n BN_CTX_end(ctx);\n return ret;\n}', 'int bn_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_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}'] |
35,179 | 0 | https://github.com/libav/libav/blob/53c20f17c78d1d8a0fc2505868f201e69ff59cc5/libavcodec/vp8dsp.c/#L197 | static av_always_inline int simple_limit(uint8_t *p, ptrdiff_t stride, int flim)
{
LOAD_PIXELS
return 2 * FFABS(p0 - q0) + (FFABS(p1 - q1) >> 1) <= flim;
} | ['static av_always_inline int simple_limit(uint8_t *p, ptrdiff_t stride, int flim)\n{\n LOAD_PIXELS\n return 2 * FFABS(p0 - q0) + (FFABS(p1 - q1) >> 1) <= flim;\n}'] |
35,180 | 0 | https://github.com/openssl/openssl/blob/a974e64aaaa8a6f99f55a68d28c07c04ecea2f50/crypto/bn/bn_ctx.c/#L328 | static unsigned int BN_STACK_pop(BN_STACK *st)
{
return st->indexes[--(st->depth)];
} | ['int EC_GROUP_cmp(const EC_GROUP *a, const EC_GROUP *b, BN_CTX *ctx)\n{\n int r = 0;\n BIGNUM *a1, *a2, *a3, *b1, *b2, *b3;\n BN_CTX *ctx_new = NULL;\n if (EC_METHOD_get_field_type(EC_GROUP_method_of(a)) !=\n EC_METHOD_get_field_type(EC_GROUP_method_of(b)))\n return 1;\n if (EC_GROUP_get_curve_name(a) && EC_GROUP_get_curve_name(b) &&\n EC_GROUP_get_curve_name(a) != EC_GROUP_get_curve_name(b))\n return 1;\n if (ctx == NULL)\n ctx_new = ctx = BN_CTX_new();\n if (ctx == NULL)\n return -1;\n BN_CTX_start(ctx);\n a1 = BN_CTX_get(ctx);\n a2 = BN_CTX_get(ctx);\n a3 = BN_CTX_get(ctx);\n b1 = BN_CTX_get(ctx);\n b2 = BN_CTX_get(ctx);\n b3 = BN_CTX_get(ctx);\n if (b3 == NULL) {\n BN_CTX_end(ctx);\n BN_CTX_free(ctx_new);\n return -1;\n }\n if (!a->meth->group_get_curve(a, a1, a2, a3, ctx) ||\n !b->meth->group_get_curve(b, b1, b2, b3, ctx))\n r = 1;\n if (r || BN_cmp(a1, b1) || BN_cmp(a2, b2) || BN_cmp(a3, b3))\n r = 1;\n if (r || EC_POINT_cmp(a, EC_GROUP_get0_generator(a),\n EC_GROUP_get0_generator(b), ctx))\n r = 1;\n if (!r) {\n if (!EC_GROUP_get_order(a, a1, ctx) ||\n !EC_GROUP_get_order(b, b1, ctx) ||\n !EC_GROUP_get_cofactor(a, a2, ctx) ||\n !EC_GROUP_get_cofactor(b, b2, ctx)) {\n BN_CTX_end(ctx);\n BN_CTX_free(ctx_new);\n return -1;\n }\n if (BN_cmp(a1, b1) || BN_cmp(a2, b2))\n r = 1;\n }\n BN_CTX_end(ctx);\n BN_CTX_free(ctx_new);\n return r;\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}'] |
35,181 | 0 | https://github.com/libav/libav/blob/a893655bdaa726a82424367b6456d195be12ebbc/libavformat/rtpdec_mpeg4.c/#L178 | static int aac_parse_packet(AVFormatContext *ctx,
PayloadContext *data,
AVStream *st,
AVPacket *pkt,
uint32_t *timestamp,
const uint8_t *buf, int len, int flags)
{
if (rtp_parse_mp4_au(data, buf))
return -1;
buf += data->au_headers_length_bytes + 2;
len -= data->au_headers_length_bytes + 2;
av_new_packet(pkt, data->au_headers[0].size);
memcpy(pkt->data, buf, data->au_headers[0].size);
pkt->stream_index = st->index;
return 0;
} | ['static int aac_parse_packet(AVFormatContext *ctx,\n PayloadContext *data,\n AVStream *st,\n AVPacket *pkt,\n uint32_t *timestamp,\n const uint8_t *buf, int len, int flags)\n{\n if (rtp_parse_mp4_au(data, buf))\n return -1;\n buf += data->au_headers_length_bytes + 2;\n len -= data->au_headers_length_bytes + 2;\n av_new_packet(pkt, data->au_headers[0].size);\n memcpy(pkt->data, buf, data->au_headers[0].size);\n pkt->stream_index = st->index;\n return 0;\n}', 'int av_new_packet(AVPacket *pkt, int size)\n{\n uint8_t *data = NULL;\n if ((unsigned)size < (unsigned)size + FF_INPUT_BUFFER_PADDING_SIZE)\n data = av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);\n if (data) {\n memset(data + size, 0, FF_INPUT_BUFFER_PADDING_SIZE);\n } else\n size = 0;\n av_init_packet(pkt);\n pkt->data = data;\n pkt->size = size;\n pkt->destruct = av_destruct_packet;\n if (!data)\n return AVERROR(ENOMEM);\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) || !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}', 'void av_init_packet(AVPacket *pkt)\n{\n pkt->pts = AV_NOPTS_VALUE;\n pkt->dts = AV_NOPTS_VALUE;\n pkt->pos = -1;\n pkt->duration = 0;\n pkt->convergence_duration = 0;\n pkt->flags = 0;\n pkt->stream_index = 0;\n pkt->destruct = NULL;\n pkt->side_data = NULL;\n pkt->side_data_elems = 0;\n}'] |
35,182 | 0 | https://github.com/openssl/openssl/blob/877e8e970c3c94c43ce1db50fdbb8e9b0342b90e/crypto/bn/bn_asm.c/#L382 | BN_ULONG bn_add_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b, int n)
{
BN_ULONG c,l,t;
assert(n >= 0);
if (n <= 0) return((BN_ULONG)0);
c=0;
#ifndef OPENSSL_SMALL_FOOTPRINT
while (n&~3)
{
t=a[0];
t=(t+c)&BN_MASK2;
c=(t < c);
l=(t+b[0])&BN_MASK2;
c+=(l < t);
r[0]=l;
t=a[1];
t=(t+c)&BN_MASK2;
c=(t < c);
l=(t+b[1])&BN_MASK2;
c+=(l < t);
r[1]=l;
t=a[2];
t=(t+c)&BN_MASK2;
c=(t < c);
l=(t+b[2])&BN_MASK2;
c+=(l < t);
r[2]=l;
t=a[3];
t=(t+c)&BN_MASK2;
c=(t < c);
l=(t+b[3])&BN_MASK2;
c+=(l < t);
r[3]=l;
a+=4; b+=4; r+=4; n-=4;
}
#endif
while(n)
{
t=a[0];
t=(t+c)&BN_MASK2;
c=(t < c);
l=(t+b[0])&BN_MASK2;
c+=(l < t);
r[0]=l;
a++; b++; r++; n--;
}
return((BN_ULONG)c);
} | ['void bn_mul_part_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n,\n\t int tna, int tnb, BN_ULONG *t)\n\t{\n\tint i,j,n2=n*2;\n\tint c1,c2,neg,zero;\n\tBN_ULONG ln,lo,*p;\n# ifdef BN_COUNT\n\tfprintf(stderr," bn_mul_part_recursive (%d+%d) * (%d+%d)\\n",\n\t\ttna, n, tnb, n);\n# endif\n\tif (n < 8)\n\t\t{\n\t\tbn_mul_normal(r,a,n+tna,b,n+tnb);\n\t\treturn;\n\t\t}\n\tc1=bn_cmp_part_words(a,&(a[n]),tna,n-tna);\n\tc2=bn_cmp_part_words(&(b[n]),b,tnb,tnb-n);\n\tzero=neg=0;\n\tswitch (c1*3+c2)\n\t\t{\n\tcase -4:\n\t\tbn_sub_part_words(t, &(a[n]),a, tna,tna-n);\n\t\tbn_sub_part_words(&(t[n]),b, &(b[n]),tnb,n-tnb);\n\t\tbreak;\n\tcase -3:\n\t\tzero=1;\n\tcase -2:\n\t\tbn_sub_part_words(t, &(a[n]),a, tna,tna-n);\n\t\tbn_sub_part_words(&(t[n]),&(b[n]),b, tnb,tnb-n);\n\t\tneg=1;\n\t\tbreak;\n\tcase -1:\n\tcase 0:\n\tcase 1:\n\t\tzero=1;\n\tcase 2:\n\t\tbn_sub_part_words(t, a, &(a[n]),tna,n-tna);\n\t\tbn_sub_part_words(&(t[n]),b, &(b[n]),tnb,n-tnb);\n\t\tneg=1;\n\t\tbreak;\n\tcase 3:\n\t\tzero=1;\n\tcase 4:\n\t\tbn_sub_part_words(t, a, &(a[n]),tna,n-tna);\n\t\tbn_sub_part_words(&(t[n]),&(b[n]),b, tnb,tnb-n);\n\t\tbreak;\n\t\t}\n# if 0\n\tif (n == 4)\n\t\t{\n\t\tbn_mul_comba4(&(t[n2]),t,&(t[n]));\n\t\tbn_mul_comba4(r,a,b);\n\t\tbn_mul_normal(&(r[n2]),&(a[n]),tn,&(b[n]),tn);\n\t\tmemset(&(r[n2+tn*2]),0,sizeof(BN_ULONG)*(n2-tn*2));\n\t\t}\n\telse\n# endif\n\tif (n == 8)\n\t\t{\n\t\tbn_mul_comba8(&(t[n2]),t,&(t[n]));\n\t\tbn_mul_comba8(r,a,b);\n\t\tbn_mul_normal(&(r[n2]),&(a[n]),tna,&(b[n]),tnb);\n\t\tmemset(&(r[n2+tna+tnb]),0,sizeof(BN_ULONG)*(n2-tna-tnb));\n\t\t}\n\telse\n\t\t{\n\t\tp= &(t[n2*2]);\n\t\tbn_mul_recursive(&(t[n2]),t,&(t[n]),n,0,0,p);\n\t\tbn_mul_recursive(r,a,b,n,0,0,p);\n\t\ti=n/2;\n\t\tif (tna > tnb)\n\t\t\tj = tna - i;\n\t\telse\n\t\t\tj = tnb - i;\n\t\tif (j == 0)\n\t\t\t{\n\t\t\tbn_mul_recursive(&(r[n2]),&(a[n]),&(b[n]),\n\t\t\t\ti,tna-i,tnb-i,p);\n\t\t\tmemset(&(r[n2+i*2]),0,sizeof(BN_ULONG)*(n2-i*2));\n\t\t\t}\n\t\telse if (j > 0)\n\t\t\t\t{\n\t\t\t\tbn_mul_part_recursive(&(r[n2]),&(a[n]),&(b[n]),\n\t\t\t\t\ti,tna-i,tnb-i,p);\n\t\t\t\tmemset(&(r[n2+tna+tnb]),0,\n\t\t\t\t\tsizeof(BN_ULONG)*(n2-tna-tnb));\n\t\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tmemset(&(r[n2]),0,sizeof(BN_ULONG)*n2);\n\t\t\tif (tna < BN_MUL_RECURSIVE_SIZE_NORMAL\n\t\t\t\t&& tnb < BN_MUL_RECURSIVE_SIZE_NORMAL)\n\t\t\t\t{\n\t\t\t\tbn_mul_normal(&(r[n2]),&(a[n]),tna,&(b[n]),tnb);\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\tfor (;;)\n\t\t\t\t\t{\n\t\t\t\t\ti/=2;\n\t\t\t\t\tif (i < tna && i < tnb)\n\t\t\t\t\t\t{\n\t\t\t\t\t\tbn_mul_part_recursive(&(r[n2]),\n\t\t\t\t\t\t\t&(a[n]),&(b[n]),\n\t\t\t\t\t\t\ti,tna-i,tnb-i,p);\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t}\n\t\t\t\t\telse if (i <= tna && i <= tnb)\n\t\t\t\t\t\t{\n\t\t\t\t\t\tbn_mul_recursive(&(r[n2]),\n\t\t\t\t\t\t\t&(a[n]),&(b[n]),\n\t\t\t\t\t\t\ti,tna-i,tnb-i,p);\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\tc1=(int)(bn_add_words(t,r,&(r[n2]),n2));\n\tif (neg)\n\t\t{\n\t\tc1-=(int)(bn_sub_words(&(t[n2]),t,&(t[n2]),n2));\n\t\t}\n\telse\n\t\t{\n\t\tc1+=(int)(bn_add_words(&(t[n2]),&(t[n2]),t,n2));\n\t\t}\n\tc1+=(int)(bn_add_words(&(r[n]),&(r[n]),&(t[n2]),n2));\n\tif (c1)\n\t\t{\n\t\tp= &(r[n+n2]);\n\t\tlo= *p;\n\t\tln=(lo+c1)&BN_MASK2;\n\t\t*p=ln;\n\t\tif (ln < (BN_ULONG)c1)\n\t\t\t{\n\t\t\tdo\t{\n\t\t\t\tp++;\n\t\t\t\tlo= *p;\n\t\t\t\tln=(lo+1)&BN_MASK2;\n\t\t\t\t*p=ln;\n\t\t\t\t} while (ln == 0);\n\t\t\t}\n\t\t}\n\t}', 'void bn_mul_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n2,\n\tint dna, int dnb, BN_ULONG *t)\n\t{\n\tint n=n2/2,c1,c2;\n\tint tna=n+dna, tnb=n+dnb;\n\tunsigned int neg,zero;\n\tBN_ULONG ln,lo,*p;\n# ifdef BN_COUNT\n\tfprintf(stderr," bn_mul_recursive %d * %d\\n",n2,n2);\n# endif\n# ifdef BN_MUL_COMBA\n# if 0\n\tif (n2 == 4)\n\t\t{\n\t\tbn_mul_comba4(r,a,b);\n\t\treturn;\n\t\t}\n# endif\n\tif (n2 == 8 && dna == 0 && dnb == 0)\n\t\t{\n\t\tbn_mul_comba8(r,a,b);\n\t\treturn;\n\t\t}\n# endif\n\tif (n2 < BN_MUL_RECURSIVE_SIZE_NORMAL)\n\t\t{\n\t\tbn_mul_normal(r,a,n2+dna,b,n2+dnb);\n\t\tif ((dna + dnb) < 0)\n\t\t\tmemset(&r[2*n2 + dna + dnb], 0,\n\t\t\t\tsizeof(BN_ULONG) * -(dna + dnb));\n\t\treturn;\n\t\t}\n\tc1=bn_cmp_part_words(a,&(a[n]),tna,n-tna);\n\tc2=bn_cmp_part_words(&(b[n]),b,tnb,tnb-n);\n\tzero=neg=0;\n\tswitch (c1*3+c2)\n\t\t{\n\tcase -4:\n\t\tbn_sub_part_words(t, &(a[n]),a, tna,tna-n);\n\t\tbn_sub_part_words(&(t[n]),b, &(b[n]),tnb,n-tnb);\n\t\tbreak;\n\tcase -3:\n\t\tzero=1;\n\t\tbreak;\n\tcase -2:\n\t\tbn_sub_part_words(t, &(a[n]),a, tna,tna-n);\n\t\tbn_sub_part_words(&(t[n]),&(b[n]),b, tnb,tnb-n);\n\t\tneg=1;\n\t\tbreak;\n\tcase -1:\n\tcase 0:\n\tcase 1:\n\t\tzero=1;\n\t\tbreak;\n\tcase 2:\n\t\tbn_sub_part_words(t, a, &(a[n]),tna,n-tna);\n\t\tbn_sub_part_words(&(t[n]),b, &(b[n]),tnb,n-tnb);\n\t\tneg=1;\n\t\tbreak;\n\tcase 3:\n\t\tzero=1;\n\t\tbreak;\n\tcase 4:\n\t\tbn_sub_part_words(t, a, &(a[n]),tna,n-tna);\n\t\tbn_sub_part_words(&(t[n]),&(b[n]),b, tnb,tnb-n);\n\t\tbreak;\n\t\t}\n# ifdef BN_MUL_COMBA\n\tif (n == 4 && dna == 0 && dnb == 0)\n\t\t{\n\t\tif (!zero)\n\t\t\tbn_mul_comba4(&(t[n2]),t,&(t[n]));\n\t\telse\n\t\t\tmemset(&(t[n2]),0,8*sizeof(BN_ULONG));\n\t\tbn_mul_comba4(r,a,b);\n\t\tbn_mul_comba4(&(r[n2]),&(a[n]),&(b[n]));\n\t\t}\n\telse if (n == 8 && dna == 0 && dnb == 0)\n\t\t{\n\t\tif (!zero)\n\t\t\tbn_mul_comba8(&(t[n2]),t,&(t[n]));\n\t\telse\n\t\t\tmemset(&(t[n2]),0,16*sizeof(BN_ULONG));\n\t\tbn_mul_comba8(r,a,b);\n\t\tbn_mul_comba8(&(r[n2]),&(a[n]),&(b[n]));\n\t\t}\n\telse\n# endif\n\t\t{\n\t\tp= &(t[n2*2]);\n\t\tif (!zero)\n\t\t\tbn_mul_recursive(&(t[n2]),t,&(t[n]),n,0,0,p);\n\t\telse\n\t\t\tmemset(&(t[n2]),0,n2*sizeof(BN_ULONG));\n\t\tbn_mul_recursive(r,a,b,n,0,0,p);\n\t\tbn_mul_recursive(&(r[n2]),&(a[n]),&(b[n]),n,dna,dnb,p);\n\t\t}\n\tc1=(int)(bn_add_words(t,r,&(r[n2]),n2));\n\tif (neg)\n\t\t{\n\t\tc1-=(int)(bn_sub_words(&(t[n2]),t,&(t[n2]),n2));\n\t\t}\n\telse\n\t\t{\n\t\tc1+=(int)(bn_add_words(&(t[n2]),&(t[n2]),t,n2));\n\t\t}\n\tc1+=(int)(bn_add_words(&(r[n]),&(r[n]),&(t[n2]),n2));\n\tif (c1)\n\t\t{\n\t\tp= &(r[n+n2]);\n\t\tlo= *p;\n\t\tln=(lo+c1)&BN_MASK2;\n\t\t*p=ln;\n\t\tif (ln < (BN_ULONG)c1)\n\t\t\t{\n\t\t\tdo\t{\n\t\t\t\tp++;\n\t\t\t\tlo= *p;\n\t\t\t\tln=(lo+1)&BN_MASK2;\n\t\t\t\t*p=ln;\n\t\t\t\t} while (ln == 0);\n\t\t\t}\n\t\t}\n\t}', 'BN_ULONG bn_add_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b, int n)\n {\n\tBN_ULONG c,l,t;\n\tassert(n >= 0);\n\tif (n <= 0) return((BN_ULONG)0);\n\tc=0;\n#ifndef OPENSSL_SMALL_FOOTPRINT\n\twhile (n&~3)\n\t\t{\n\t\tt=a[0];\n\t\tt=(t+c)&BN_MASK2;\n\t\tc=(t < c);\n\t\tl=(t+b[0])&BN_MASK2;\n\t\tc+=(l < t);\n\t\tr[0]=l;\n\t\tt=a[1];\n\t\tt=(t+c)&BN_MASK2;\n\t\tc=(t < c);\n\t\tl=(t+b[1])&BN_MASK2;\n\t\tc+=(l < t);\n\t\tr[1]=l;\n\t\tt=a[2];\n\t\tt=(t+c)&BN_MASK2;\n\t\tc=(t < c);\n\t\tl=(t+b[2])&BN_MASK2;\n\t\tc+=(l < t);\n\t\tr[2]=l;\n\t\tt=a[3];\n\t\tt=(t+c)&BN_MASK2;\n\t\tc=(t < c);\n\t\tl=(t+b[3])&BN_MASK2;\n\t\tc+=(l < t);\n\t\tr[3]=l;\n\t\ta+=4; b+=4; r+=4; n-=4;\n\t\t}\n#endif\n\twhile(n)\n\t\t{\n\t\tt=a[0];\n\t\tt=(t+c)&BN_MASK2;\n\t\tc=(t < c);\n\t\tl=(t+b[0])&BN_MASK2;\n\t\tc+=(l < t);\n\t\tr[0]=l;\n\t\ta++; b++; r++; n--;\n\t\t}\n\treturn((BN_ULONG)c);\n\t}'] |
35,183 | 0 | https://gitlab.com/libtiff/libtiff/blob/18ca4b4276e24a90031fd1641cf8ed2fa0c8d402/libtiff/tif_fax3.c/#L545 | static int
Fax3SetupState(TIFF* tif)
{
static const char module[] = "Fax3SetupState";
TIFFDirectory* td = &tif->tif_dir;
Fax3BaseState* sp = Fax3State(tif);
int needsRefLine;
Fax3CodecState* dsp = (Fax3CodecState*) Fax3State(tif);
tmsize_t rowbytes;
uint32 rowpixels;
if (td->td_bitspersample != 1) {
TIFFErrorExt(tif->tif_clientdata, module,
"Bits/sample must be 1 for Group 3/4 encoding/decoding");
return (0);
}
if (isTiled(tif)) {
rowbytes = TIFFTileRowSize(tif);
rowpixels = td->td_tilewidth;
} else {
rowbytes = TIFFScanlineSize(tif);
rowpixels = td->td_imagewidth;
}
sp->rowbytes = rowbytes;
sp->rowpixels = rowpixels;
needsRefLine = (
(sp->groupoptions & GROUP3OPT_2DENCODING) ||
td->td_compression == COMPRESSION_CCITTFAX4
);
dsp->runs=(uint32*) NULL;
dsp->nruns = TIFFroundup_32(rowpixels,32);
if (needsRefLine) {
dsp->nruns = TIFFSafeMultiply(uint32,dsp->nruns,2);
}
if ((dsp->nruns == 0) || (TIFFSafeMultiply(uint32,dsp->nruns,2) == 0)) {
TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
"Row pixels integer overflow (rowpixels %u)",
rowpixels);
return (0);
}
dsp->runs = (uint32*) _TIFFCheckMalloc(tif,
TIFFSafeMultiply(uint32,dsp->nruns,2),
sizeof (uint32),
"for Group 3/4 run arrays");
if (dsp->runs == NULL)
return (0);
memset( dsp->runs, 0, TIFFSafeMultiply(uint32,dsp->nruns,2)*sizeof(uint32));
dsp->curruns = dsp->runs;
if (needsRefLine)
dsp->refruns = dsp->runs + dsp->nruns;
else
dsp->refruns = NULL;
if (td->td_compression == COMPRESSION_CCITTFAX3
&& is2DEncoding(dsp)) {
tif->tif_decoderow = Fax3Decode2D;
tif->tif_decodestrip = Fax3Decode2D;
tif->tif_decodetile = Fax3Decode2D;
}
if (needsRefLine) {
Fax3CodecState* esp = EncoderState(tif);
esp->refline = (unsigned char*) _TIFFmalloc(rowbytes);
if (esp->refline == NULL) {
TIFFErrorExt(tif->tif_clientdata, module,
"No space for Group 3/4 reference line");
return (0);
}
} else
EncoderState(tif)->refline = NULL;
return (1);
} | ['static int\nFax3SetupState(TIFF* tif)\n{\n\tstatic const char module[] = "Fax3SetupState";\n\tTIFFDirectory* td = &tif->tif_dir;\n\tFax3BaseState* sp = Fax3State(tif);\n\tint needsRefLine;\n\tFax3CodecState* dsp = (Fax3CodecState*) Fax3State(tif);\n\ttmsize_t rowbytes;\n\tuint32 rowpixels;\n\tif (td->td_bitspersample != 1) {\n\t\tTIFFErrorExt(tif->tif_clientdata, module,\n\t\t "Bits/sample must be 1 for Group 3/4 encoding/decoding");\n\t\treturn (0);\n\t}\n\tif (isTiled(tif)) {\n\t\trowbytes = TIFFTileRowSize(tif);\n\t\trowpixels = td->td_tilewidth;\n\t} else {\n\t\trowbytes = TIFFScanlineSize(tif);\n\t\trowpixels = td->td_imagewidth;\n\t}\n\tsp->rowbytes = rowbytes;\n\tsp->rowpixels = rowpixels;\n\tneedsRefLine = (\n\t (sp->groupoptions & GROUP3OPT_2DENCODING) ||\n\t td->td_compression == COMPRESSION_CCITTFAX4\n\t);\n\tdsp->runs=(uint32*) NULL;\n\tdsp->nruns = TIFFroundup_32(rowpixels,32);\n\tif (needsRefLine) {\n\t\tdsp->nruns = TIFFSafeMultiply(uint32,dsp->nruns,2);\n\t}\n\tif ((dsp->nruns == 0) || (TIFFSafeMultiply(uint32,dsp->nruns,2) == 0)) {\n\t\tTIFFErrorExt(tif->tif_clientdata, tif->tif_name,\n\t\t\t "Row pixels integer overflow (rowpixels %u)",\n\t\t\t rowpixels);\n\t\treturn (0);\n\t}\n\tdsp->runs = (uint32*) _TIFFCheckMalloc(tif,\n\t\t\t\t\t TIFFSafeMultiply(uint32,dsp->nruns,2),\n\t\t\t\t\t sizeof (uint32),\n\t\t\t\t\t "for Group 3/4 run arrays");\n\tif (dsp->runs == NULL)\n\t\treturn (0);\n\tmemset( dsp->runs, 0, TIFFSafeMultiply(uint32,dsp->nruns,2)*sizeof(uint32));\n\tdsp->curruns = dsp->runs;\n\tif (needsRefLine)\n\t\tdsp->refruns = dsp->runs + dsp->nruns;\n\telse\n\t\tdsp->refruns = NULL;\n\tif (td->td_compression == COMPRESSION_CCITTFAX3\n\t && is2DEncoding(dsp)) {\n\t\ttif->tif_decoderow = Fax3Decode2D;\n\t\ttif->tif_decodestrip = Fax3Decode2D;\n\t\ttif->tif_decodetile = Fax3Decode2D;\n\t}\n\tif (needsRefLine) {\n\t\tFax3CodecState* esp = EncoderState(tif);\n\t\tesp->refline = (unsigned char*) _TIFFmalloc(rowbytes);\n\t\tif (esp->refline == NULL) {\n\t\t\tTIFFErrorExt(tif->tif_clientdata, module,\n\t\t\t "No space for Group 3/4 reference line");\n\t\t\treturn (0);\n\t\t}\n\t} else\n\t\tEncoderState(tif)->refline = NULL;\n\treturn (1);\n}'] |
35,184 | 0 | https://github.com/libav/libav/blob/03f8fc0897c128028111182e6276139fa00b891b/libavcodec/smacker.c/#L289 | static int decode_header_trees(SmackVContext *smk) {
GetBitContext gb;
int mmap_size, mclr_size, full_size, type_size;
mmap_size = AV_RL32(smk->avctx->extradata);
mclr_size = AV_RL32(smk->avctx->extradata + 4);
full_size = AV_RL32(smk->avctx->extradata + 8);
type_size = AV_RL32(smk->avctx->extradata + 12);
init_get_bits(&gb, smk->avctx->extradata + 16, (smk->avctx->extradata_size - 16) * 8);
if(!get_bits1(&gb)) {
av_log(smk->avctx, AV_LOG_INFO, "Skipping MMAP tree\n");
smk->mmap_tbl = av_malloc(sizeof(int) * 2);
smk->mmap_tbl[0] = 0;
smk->mmap_last[0] = smk->mmap_last[1] = smk->mmap_last[2] = 1;
} else {
smacker_decode_header_tree(smk, &gb, &smk->mmap_tbl, smk->mmap_last, mmap_size);
}
if(!get_bits1(&gb)) {
av_log(smk->avctx, AV_LOG_INFO, "Skipping MCLR tree\n");
smk->mclr_tbl = av_malloc(sizeof(int) * 2);
smk->mclr_tbl[0] = 0;
smk->mclr_last[0] = smk->mclr_last[1] = smk->mclr_last[2] = 1;
} else {
smacker_decode_header_tree(smk, &gb, &smk->mclr_tbl, smk->mclr_last, mclr_size);
}
if(!get_bits1(&gb)) {
av_log(smk->avctx, AV_LOG_INFO, "Skipping FULL tree\n");
smk->full_tbl = av_malloc(sizeof(int) * 2);
smk->full_tbl[0] = 0;
smk->full_last[0] = smk->full_last[1] = smk->full_last[2] = 1;
} else {
smacker_decode_header_tree(smk, &gb, &smk->full_tbl, smk->full_last, full_size);
}
if(!get_bits1(&gb)) {
av_log(smk->avctx, AV_LOG_INFO, "Skipping TYPE tree\n");
smk->type_tbl = av_malloc(sizeof(int) * 2);
smk->type_tbl[0] = 0;
smk->type_last[0] = smk->type_last[1] = smk->type_last[2] = 1;
} else {
smacker_decode_header_tree(smk, &gb, &smk->type_tbl, smk->type_last, type_size);
}
return 0;
} | ['static int decode_header_trees(SmackVContext *smk) {\n GetBitContext gb;\n int mmap_size, mclr_size, full_size, type_size;\n mmap_size = AV_RL32(smk->avctx->extradata);\n mclr_size = AV_RL32(smk->avctx->extradata + 4);\n full_size = AV_RL32(smk->avctx->extradata + 8);\n type_size = AV_RL32(smk->avctx->extradata + 12);\n init_get_bits(&gb, smk->avctx->extradata + 16, (smk->avctx->extradata_size - 16) * 8);\n if(!get_bits1(&gb)) {\n av_log(smk->avctx, AV_LOG_INFO, "Skipping MMAP tree\\n");\n smk->mmap_tbl = av_malloc(sizeof(int) * 2);\n smk->mmap_tbl[0] = 0;\n smk->mmap_last[0] = smk->mmap_last[1] = smk->mmap_last[2] = 1;\n } else {\n smacker_decode_header_tree(smk, &gb, &smk->mmap_tbl, smk->mmap_last, mmap_size);\n }\n if(!get_bits1(&gb)) {\n av_log(smk->avctx, AV_LOG_INFO, "Skipping MCLR tree\\n");\n smk->mclr_tbl = av_malloc(sizeof(int) * 2);\n smk->mclr_tbl[0] = 0;\n smk->mclr_last[0] = smk->mclr_last[1] = smk->mclr_last[2] = 1;\n } else {\n smacker_decode_header_tree(smk, &gb, &smk->mclr_tbl, smk->mclr_last, mclr_size);\n }\n if(!get_bits1(&gb)) {\n av_log(smk->avctx, AV_LOG_INFO, "Skipping FULL tree\\n");\n smk->full_tbl = av_malloc(sizeof(int) * 2);\n smk->full_tbl[0] = 0;\n smk->full_last[0] = smk->full_last[1] = smk->full_last[2] = 1;\n } else {\n smacker_decode_header_tree(smk, &gb, &smk->full_tbl, smk->full_last, full_size);\n }\n if(!get_bits1(&gb)) {\n av_log(smk->avctx, AV_LOG_INFO, "Skipping TYPE tree\\n");\n smk->type_tbl = av_malloc(sizeof(int) * 2);\n smk->type_tbl[0] = 0;\n smk->type_last[0] = smk->type_last[1] = smk->type_last[2] = 1;\n } else {\n smacker_decode_header_tree(smk, &gb, &smk->type_tbl, smk->type_last, type_size);\n }\n return 0;\n}', 'static inline void init_get_bits(GetBitContext *s,\n const uint8_t *buffer, int bit_size)\n{\n int buffer_size= (bit_size+7)>>3;\n if(buffer_size < 0 || bit_size < 0) {\n buffer_size = bit_size = 0;\n buffer = NULL;\n }\n s->buffer= buffer;\n s->size_in_bits= bit_size;\n s->buffer_end= buffer + buffer_size;\n#ifdef ALT_BITSTREAM_READER\n s->index=0;\n#elif defined LIBMPEG2_BITSTREAM_READER\n s->buffer_ptr = (uint8_t*)((intptr_t)buffer&(~1));\n s->bit_count = 16 + 8*((intptr_t)buffer&1);\n skip_bits_long(s, 0);\n#elif defined A32_BITSTREAM_READER\n s->buffer_ptr = (uint32_t*)((intptr_t)buffer&(~3));\n s->bit_count = 32 + 8*((intptr_t)buffer&3);\n skip_bits_long(s, 0);\n#endif\n}', '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&0x07);\n result&= 1;\n#else\n result<<= (index&0x07);\n result>>= 8 - 1;\n#endif\n index++;\n s->index= index;\n return result;\n#else\n return get_bits(s, 1);\n#endif\n}', 'void *av_malloc(unsigned int size)\n{\n void *ptr = NULL;\n#if CONFIG_MEMALIGN_HACK\n long diff;\n#endif\n if(size > (INT_MAX-16) )\n return NULL;\n#if CONFIG_MEMALIGN_HACK\n ptr = malloc(size+16);\n if(!ptr)\n return ptr;\n diff= ((-(long)ptr - 1)&15) + 1;\n ptr = (char*)ptr + diff;\n ((char*)ptr)[-1]= diff;\n#elif HAVE_POSIX_MEMALIGN\n if (posix_memalign(&ptr,16,size))\n ptr = NULL;\n#elif HAVE_MEMALIGN\n ptr = memalign(16,size);\n#else\n ptr = malloc(size);\n#endif\n return ptr;\n}'] |
35,185 | 0 | https://github.com/openssl/openssl/blob/8b0d4242404f9e5da26e7594fa0864b2df4601af/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 execute_test_session(SSL_SESSION_TEST_FIXTURE fix)\n{\n SSL_CTX *sctx = NULL, *cctx = NULL;\n SSL *serverssl1 = NULL, *clientssl1 = NULL;\n SSL *serverssl2 = NULL, *clientssl2 = NULL;\n#ifndef OPENSSL_NO_TLS1_1\n SSL *serverssl3 = NULL, *clientssl3 = NULL;\n#endif\n SSL_SESSION *sess1 = NULL, *sess2 = NULL;\n int testresult = 0;\n if (!create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(), &sctx,\n &cctx, cert, privkey)) {\n printf("Unable to create SSL_CTX pair\\n");\n return 0;\n }\n#ifndef OPENSSL_NO_TLS1_2\n SSL_CTX_set_min_proto_version(cctx, TLS1_2_VERSION);\n#endif\n if (fix.use_ext_cache) {\n SSL_CTX_sess_set_new_cb(cctx, new_session_cb);\n SSL_CTX_sess_set_remove_cb(cctx, remove_session_cb);\n }\n if (fix.use_int_cache) {\n SSL_CTX_set_session_cache_mode(cctx, SSL_SESS_CACHE_CLIENT);\n } else {\n SSL_CTX_set_session_cache_mode(cctx,\n SSL_SESS_CACHE_CLIENT\n | SSL_SESS_CACHE_NO_INTERNAL_STORE);\n }\n if (!create_ssl_objects(sctx, cctx, &serverssl1, &clientssl1, NULL,\n NULL)) {\n printf("Unable to create SSL objects\\n");\n goto end;\n }\n if (!create_ssl_connection(serverssl1, clientssl1, SSL_ERROR_NONE)) {\n printf("Unable to create SSL connection\\n");\n goto end;\n }\n sess1 = SSL_get1_session(clientssl1);\n if (sess1 == NULL) {\n printf("Unexpected NULL session\\n");\n goto end;\n }\n if (fix.use_int_cache && SSL_CTX_add_session(cctx, sess1)) {\n printf("Unexpected success adding session to cache\\n");\n goto end;\n }\n if (fix.use_ext_cache && (new_called != 1 || remove_called != 0)) {\n printf("Session not added to cache\\n");\n goto end;\n }\n if (!create_ssl_objects(sctx, cctx, &serverssl2, &clientssl2, NULL, NULL)) {\n printf("Unable to create second SSL objects\\n");\n goto end;\n }\n if (!create_ssl_connection(serverssl2, clientssl2, SSL_ERROR_NONE)) {\n printf("Unable to create second SSL connection\\n");\n goto end;\n }\n sess2 = SSL_get1_session(clientssl2);\n if (sess2 == NULL) {\n printf("Unexpected NULL session from clientssl2\\n");\n goto end;\n }\n if (fix.use_ext_cache && (new_called != 2 || remove_called != 0)) {\n printf("Remove session callback unexpectedly called\\n");\n goto end;\n }\n if (!SSL_set_session(clientssl2, sess1)) {\n printf("Unexpected failure setting session\\n");\n goto end;\n }\n if (fix.use_ext_cache && (new_called != 2 || remove_called != 1)) {\n printf("Failed to call callback to remove session\\n");\n goto end;\n }\n if (SSL_get_session(clientssl2) != sess1) {\n printf("Unexpected session found\\n");\n goto end;\n }\n if (fix.use_int_cache) {\n if (!SSL_CTX_add_session(cctx, sess2)) {\n printf("Unexpected failure adding session to cache\\n");\n goto end;\n }\n if (!SSL_CTX_remove_session(cctx, sess2)) {\n printf("Unexpected failure removing session from cache\\n");\n goto end;\n }\n if (fix.use_ext_cache)\n remove_called--;\n }\n if (SSL_CTX_remove_session(cctx, sess2)) {\n printf("Unexpected success removing session from cache\\n");\n goto end;\n }\n if (fix.use_ext_cache && (new_called != 2 || remove_called != 2)) {\n printf("Failed to call callback to remove session #2\\n");\n goto end;\n }\n#if !defined(OPENSSL_NO_TLS1_1) && !defined(OPENSSL_NO_TLS1_2)\n SSL_CTX_set_max_proto_version(sctx, TLS1_1_VERSION);\n if (!create_ssl_objects(sctx, cctx, &serverssl3, &clientssl3, NULL, NULL)) {\n printf("Unable to create third SSL objects\\n");\n goto end;\n }\n if (!SSL_set_session(clientssl3, sess1)) {\n printf("Unable to set session for third connection\\n");\n goto end;\n }\n if (create_ssl_connection(serverssl3, clientssl3, SSL_ERROR_NONE)) {\n printf("Unable to create third SSL connection\\n");\n goto end;\n }\n if (fix.use_ext_cache && (new_called != 2 || remove_called != 3)) {\n printf("Failed to call callback to remove session #2\\n");\n goto end;\n }\n if (fix.use_int_cache && !SSL_CTX_add_session(cctx, sess2)) {\n printf("Unexpected failure adding session to cache #2\\n");\n goto end;\n }\n#endif\n testresult = 1;\n end:\n SSL_free(serverssl1);\n SSL_free(clientssl1);\n SSL_free(serverssl2);\n SSL_free(clientssl2);\n#ifndef OPENSSL_NO_TLS1_1\n SSL_free(serverssl3);\n SSL_free(clientssl3);\n#endif\n SSL_SESSION_free(sess1);\n SSL_SESSION_free(sess2);\n if (new_called >= 1)\n SSL_SESSION_free(sess1);\n if (new_called >= 2)\n SSL_SESSION_free(sess2);\n SSL_CTX_free(sctx);\n SSL_CTX_free(cctx);\n return testresult;\n}', 'int create_ssl_connection(SSL *serverssl, SSL *clientssl, int want)\n{\n int retc = -1, rets = -1, err, abortctr = 0;\n int clienterr = 0, servererr = 0;\n unsigned char buf;\n size_t readbytes;\n do {\n err = SSL_ERROR_WANT_WRITE;\n while (!clienterr && retc <= 0 && err == SSL_ERROR_WANT_WRITE) {\n retc = SSL_connect(clientssl);\n if (retc <= 0)\n err = SSL_get_error(clientssl, retc);\n }\n if (!clienterr && retc <= 0 && err != SSL_ERROR_WANT_READ) {\n printf("SSL_connect() failed %d, %d\\n", retc, err);\n clienterr = 1;\n }\n if (want != SSL_ERROR_NONE && err == want)\n return 0;\n err = SSL_ERROR_WANT_WRITE;\n while (!servererr && rets <= 0 && err == SSL_ERROR_WANT_WRITE) {\n rets = SSL_accept(serverssl);\n if (rets <= 0)\n err = SSL_get_error(serverssl, rets);\n }\n if (!servererr && rets <= 0 && err != SSL_ERROR_WANT_READ) {\n printf("SSL_accept() failed %d, %d\\n", rets, err);\n servererr = 1;\n }\n if (want != SSL_ERROR_NONE && err == want)\n return 0;\n if (clienterr && servererr)\n return 0;\n if (++abortctr == MAXLOOPS) {\n printf("No progress made\\n");\n return 0;\n }\n } while (retc <=0 || rets <= 0);\n if (SSL_read_ex(clientssl, &buf, sizeof(buf), &readbytes) > 0) {\n if (readbytes != 0) {\n printf("Unexpected success reading data %"OSSLzu"\\n", readbytes);\n return 0;\n }\n } else if (SSL_get_error(clientssl, 0) != SSL_ERROR_WANT_READ) {\n printf("SSL_read_ex() failed\\n");\n return 0;\n }\n return 1;\n}', 'int SSL_connect(SSL *s)\n{\n if (s->handshake_func == NULL) {\n SSL_set_connect_state(s);\n }\n return SSL_do_handshake(s);\n}', 'int SSL_do_handshake(SSL *s)\n{\n int ret = 1;\n if (s->handshake_func == NULL) {\n SSLerr(SSL_F_SSL_DO_HANDSHAKE, SSL_R_CONNECTION_TYPE_NOT_SET);\n return -1;\n }\n if (s->early_data_state == SSL_EARLY_DATA_WRITE_RETRY) {\n int edfin;\n edfin = ssl_write_early_finish(s);\n if (edfin <= 0)\n return edfin;\n }\n ossl_statem_check_finish_init(s, -1);\n s->method->ssl_renegotiate_check(s, 0);\n if (SSL_in_init(s) || SSL_in_before(s)) {\n if ((s->mode & SSL_MODE_ASYNC) && ASYNC_get_current_job() == NULL) {\n struct ssl_async_args args;\n args.s = s;\n ret = ssl_start_async_job(s, &args, ssl_do_handshake_intern);\n } else {\n ret = s->handshake_func(s);\n }\n }\n return ret;\n}', 'int SSL_set_session(SSL *s, SSL_SESSION *session)\n{\n ssl_clear_bad_session(s);\n if (s->ctx->method != s->method) {\n if (!SSL_set_ssl_method(s, s->ctx->method))\n return 0;\n }\n if (session != NULL) {\n SSL_SESSION_up_ref(session);\n s->verify_result = session->verify_result;\n }\n SSL_SESSION_free(s->session);\n s->session = session;\n return 1;\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}'] |
35,186 | 0 | https://github.com/libav/libav/blob/a893655bdaa726a82424367b6456d195be12ebbc/libavcodec/binkaudio.c/#L326 | static int decode_frame(AVCodecContext *avctx, void *data,
int *got_frame_ptr, AVPacket *avpkt)
{
BinkAudioContext *s = avctx->priv_data;
GetBitContext *gb = &s->gb;
int ret, consumed = 0;
if (!get_bits_left(gb)) {
uint8_t *buf;
if (!avpkt->size) {
*got_frame_ptr = 0;
return 0;
}
if (avpkt->size < 4) {
av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
return AVERROR_INVALIDDATA;
}
buf = av_realloc(s->packet_buffer, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
if (!buf)
return AVERROR(ENOMEM);
s->packet_buffer = buf;
memcpy(s->packet_buffer, avpkt->data, avpkt->size);
init_get_bits(gb, s->packet_buffer, avpkt->size * 8);
consumed = avpkt->size;
skip_bits_long(gb, 32);
}
s->frame.nb_samples = s->frame_len;
if ((ret = avctx->get_buffer(avctx, &s->frame)) < 0) {
av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
return ret;
}
if (decode_block(s, (float **)s->frame.extended_data,
avctx->codec->id == AV_CODEC_ID_BINKAUDIO_DCT)) {
av_log(avctx, AV_LOG_ERROR, "Incomplete packet\n");
return AVERROR_INVALIDDATA;
}
get_bits_align32(gb);
s->frame.nb_samples = s->block_size / avctx->channels;
*got_frame_ptr = 1;
*(AVFrame *)data = s->frame;
return consumed;
} | ['static int decode_frame(AVCodecContext *avctx, void *data,\n int *got_frame_ptr, AVPacket *avpkt)\n{\n BinkAudioContext *s = avctx->priv_data;\n GetBitContext *gb = &s->gb;\n int ret, consumed = 0;\n if (!get_bits_left(gb)) {\n uint8_t *buf;\n if (!avpkt->size) {\n *got_frame_ptr = 0;\n return 0;\n }\n if (avpkt->size < 4) {\n av_log(avctx, AV_LOG_ERROR, "Packet is too small\\n");\n return AVERROR_INVALIDDATA;\n }\n buf = av_realloc(s->packet_buffer, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);\n if (!buf)\n return AVERROR(ENOMEM);\n s->packet_buffer = buf;\n memcpy(s->packet_buffer, avpkt->data, avpkt->size);\n init_get_bits(gb, s->packet_buffer, avpkt->size * 8);\n consumed = avpkt->size;\n skip_bits_long(gb, 32);\n }\n s->frame.nb_samples = s->frame_len;\n if ((ret = avctx->get_buffer(avctx, &s->frame)) < 0) {\n av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\\n");\n return ret;\n }\n if (decode_block(s, (float **)s->frame.extended_data,\n avctx->codec->id == AV_CODEC_ID_BINKAUDIO_DCT)) {\n av_log(avctx, AV_LOG_ERROR, "Incomplete packet\\n");\n return AVERROR_INVALIDDATA;\n }\n get_bits_align32(gb);\n s->frame.nb_samples = s->block_size / avctx->channels;\n *got_frame_ptr = 1;\n *(AVFrame *)data = s->frame;\n return consumed;\n}', 'static inline int get_bits_left(GetBitContext *gb)\n{\n return gb->size_in_bits - get_bits_count(gb);\n}', 'static inline int get_bits_count(const GetBitContext *s)\n{\n return s->index;\n}', 'void *av_realloc(void *ptr, size_t 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)\n return av_malloc(size);\n diff = ((char *)ptr)[-1];\n return (char *)realloc((char *)ptr - diff, size + diff) + diff;\n#elif HAVE_ALIGNED_MALLOC\n return _aligned_realloc(ptr, size, 32);\n#else\n return realloc(ptr, size);\n#endif\n}', 'static inline void init_get_bits(GetBitContext *s, const uint8_t *buffer,\n 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#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}', 'static inline void skip_bits_long(GetBitContext *s, int n){\n#if UNCHECKED_BITSTREAM_READER\n s->index += n;\n#else\n s->index += av_clip(n, -s->index, s->size_in_bits_plus8 - s->index);\n#endif\n}'] |
35,187 | 0 | https://github.com/openssl/openssl/blob/67dc995eaf538ea309c6292a1a5073465201f55b/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;
} | ['EXT_RETURN tls_construct_stoc_renegotiate(SSL *s, WPACKET *pkt,\n unsigned int context, X509 *x,\n size_t chainidx, int *al)\n{\n if (!s->s3->send_connection_binding)\n return EXT_RETURN_NOT_SENT;\n if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_renegotiate)\n || !WPACKET_start_sub_packet_u16(pkt)\n || !WPACKET_start_sub_packet_u8(pkt)\n || !WPACKET_memcpy(pkt, s->s3->previous_client_finished,\n s->s3->previous_client_finished_len)\n || !WPACKET_memcpy(pkt, s->s3->previous_server_finished,\n s->s3->previous_server_finished_len)\n || !WPACKET_close(pkt)\n || !WPACKET_close(pkt)) {\n SSLerr(SSL_F_TLS_CONSTRUCT_STOC_RENEGOTIATE, ERR_R_INTERNAL_ERROR);\n return EXT_RETURN_FAIL;\n }\n return EXT_RETURN_SENT;\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}'] |
35,188 | 0 | https://github.com/openssl/openssl/blob/877e8e970c3c94c43ce1db50fdbb8e9b0342b90e/crypto/bn/bn_asm.c/#L812 | void bn_sqr_comba4(BN_ULONG *r, const BN_ULONG *a)
{
#ifdef BN_LLONG
BN_ULLONG t,tt;
#else
BN_ULONG bl,bh;
#endif
BN_ULONG t1,t2;
BN_ULONG c1,c2,c3;
c1=0;
c2=0;
c3=0;
sqr_add_c(a,0,c1,c2,c3);
r[0]=c1;
c1=0;
sqr_add_c2(a,1,0,c2,c3,c1);
r[1]=c2;
c2=0;
sqr_add_c(a,1,c3,c1,c2);
sqr_add_c2(a,2,0,c3,c1,c2);
r[2]=c3;
c3=0;
sqr_add_c2(a,3,0,c1,c2,c3);
sqr_add_c2(a,2,1,c1,c2,c3);
r[3]=c1;
c1=0;
sqr_add_c(a,2,c2,c3,c1);
sqr_add_c2(a,3,1,c2,c3,c1);
r[4]=c2;
c2=0;
sqr_add_c2(a,3,2,c3,c1,c2);
r[5]=c3;
c3=0;
sqr_add_c(a,3,c1,c2,c3);
r[6]=c1;
r[7]=c2;
} | ['void bn_sqr_comba4(BN_ULONG *r, const BN_ULONG *a)\n\t{\n#ifdef BN_LLONG\n\tBN_ULLONG t,tt;\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\tsqr_add_c(a,0,c1,c2,c3);\n\tr[0]=c1;\n\tc1=0;\n\tsqr_add_c2(a,1,0,c2,c3,c1);\n\tr[1]=c2;\n\tc2=0;\n\tsqr_add_c(a,1,c3,c1,c2);\n\tsqr_add_c2(a,2,0,c3,c1,c2);\n\tr[2]=c3;\n\tc3=0;\n\tsqr_add_c2(a,3,0,c1,c2,c3);\n\tsqr_add_c2(a,2,1,c1,c2,c3);\n\tr[3]=c1;\n\tc1=0;\n\tsqr_add_c(a,2,c2,c3,c1);\n\tsqr_add_c2(a,3,1,c2,c3,c1);\n\tr[4]=c2;\n\tc2=0;\n\tsqr_add_c2(a,3,2,c3,c1,c2);\n\tr[5]=c3;\n\tc3=0;\n\tsqr_add_c(a,3,c1,c2,c3);\n\tr[6]=c1;\n\tr[7]=c2;\n\t}'] |
35,189 | 0 | https://github.com/openssl/openssl/blob/8da94770f0a049497b1a52ee469cca1f4a13b1a7/crypto/bn/bn_lib.c/#L450 | BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)
{
int i;
BN_ULONG *A;
const BN_ULONG *B;
bn_check_top(b);
if (a == b)
return (a);
if (bn_wexpand(a, b->top) == NULL)
return (NULL);
#if 1
A = a->d;
B = b->d;
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
memcpy(a->d, b->d, sizeof(b->d[0]) * b->top);
#endif
a->top = b->top;
a->neg = b->neg;
bn_check_top(a);
return (a);
} | ['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}', 'BIGNUM *BN_CTX_get(BN_CTX *ctx)\n{\n BIGNUM *ret;\n CTXDBG_ENTRY("BN_CTX_get", ctx);\n if (ctx->err_stack || ctx->too_many)\n return NULL;\n if ((ret = BN_POOL_get(&ctx->pool, ctx->flags)) == NULL) {\n ctx->too_many = 1;\n BNerr(BN_F_BN_CTX_GET, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n return NULL;\n }\n BN_zero(ret);\n ctx->used++;\n CTXDBG_RET(ctx, ret);\n return ret;\n}', 'int BN_set_word(BIGNUM *a, BN_ULONG w)\n{\n bn_check_top(a);\n if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)\n return (0);\n a->neg = 0;\n a->d[0] = w;\n a->top = (w ? 1 : 0);\n bn_check_top(a);\n return (1);\n}', 'int BN_nnmod(BIGNUM *r, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx)\n{\n if (!(BN_mod(r, m, d, ctx)))\n return 0;\n if (!r->neg)\n return 1;\n return (d->neg ? BN_sub : BN_add) (r, r, d);\n}', 'int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,\n BN_CTX *ctx)\n{\n int norm_shift, i, loop;\n BIGNUM *tmp, wnum, *snum, *sdiv, *res;\n BN_ULONG *resp, *wnump;\n BN_ULONG d0, d1;\n int num_n, div_n;\n int no_branch = 0;\n if ((num->top > 0 && num->d[num->top - 1] == 0) ||\n (divisor->top > 0 && divisor->d[divisor->top - 1] == 0)) {\n BNerr(BN_F_BN_DIV, BN_R_NOT_INITIALIZED);\n return 0;\n }\n bn_check_top(num);\n bn_check_top(divisor);\n if ((BN_get_flags(num, BN_FLG_CONSTTIME) != 0)\n || (BN_get_flags(divisor, BN_FLG_CONSTTIME) != 0)) {\n no_branch = 1;\n }\n bn_check_top(dv);\n bn_check_top(rm);\n if (BN_is_zero(divisor)) {\n BNerr(BN_F_BN_DIV, BN_R_DIV_BY_ZERO);\n return (0);\n }\n if (!no_branch && BN_ucmp(num, divisor) < 0) {\n if (rm != NULL) {\n if (BN_copy(rm, num) == NULL)\n return (0);\n }\n if (dv != NULL)\n BN_zero(dv);\n return (1);\n }\n BN_CTX_start(ctx);\n 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_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}'] |
35,190 | 0 | https://github.com/nginx/nginx/blob/bcd78e22e97d4c870b5104d0c540caaa972176ed/src/http/modules/ngx_http_index_module.c/#L175 | static ngx_int_t
ngx_http_index_handler(ngx_http_request_t *r)
{
u_char *p, *name;
size_t len, root, reserve, allocated;
ngx_int_t rc;
ngx_str_t path, uri;
ngx_uint_t i, dir_tested;
ngx_http_index_t *index;
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_index_loc_conf_t *ilcf;
ngx_http_script_len_code_pt lcode;
if (r->uri.data[r->uri.len - 1] != '/') {
return NGX_DECLINED;
}
if (!(r->method & (NGX_HTTP_GET|NGX_HTTP_HEAD|NGX_HTTP_POST))) {
return NGX_DECLINED;
}
ilcf = ngx_http_get_module_loc_conf(r, ngx_http_index_module);
clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
allocated = 0;
root = 0;
dir_tested = 0;
name = NULL;
path.data = NULL;
index = ilcf->indices->elts;
for (i = 0; i < ilcf->indices->nelts; i++) {
if (index[i].lengths == NULL) {
if (index[i].name.data[0] == '/') {
return ngx_http_internal_redirect(r, &index[i].name, &r->args);
}
reserve = ilcf->max_index_len;
len = index[i].name.len;
} else {
ngx_memzero(&e, sizeof(ngx_http_script_engine_t));
e.ip = index[i].lengths->elts;
e.request = r;
e.flushed = 1;
len = 1;
while (*(uintptr_t *) e.ip) {
lcode = *(ngx_http_script_len_code_pt *) e.ip;
len += lcode(&e);
}
reserve = len + 16;
}
if (reserve > allocated) {
name = ngx_http_map_uri_to_path(r, &path, &root, reserve);
if (name == NULL) {
return NGX_ERROR;
}
allocated = path.data + path.len - name;
}
if (index[i].values == NULL) {
ngx_memcpy(name, index[i].name.data, index[i].name.len);
path.len = (name + index[i].name.len - 1) - path.data;
} else {
e.ip = index[i].values->elts;
e.pos = name;
while (*(uintptr_t *) e.ip) {
code = *(ngx_http_script_code_pt *) e.ip;
code((ngx_http_script_engine_t *) &e);
}
if (*name == '/') {
uri.len = len - 1;
uri.data = name;
return ngx_http_internal_redirect(r, &uri, &r->args);
}
path.len = e.pos - path.data;
*e.pos = '\0';
}
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"open index \"%V\"", &path);
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)
{
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, of.err,
"%s \"%s\" failed", of.failed, path.data);
if (of.err == 0) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
if (of.err == NGX_ENOTDIR
|| of.err == NGX_ENAMETOOLONG
|| of.err == NGX_EACCES)
{
return ngx_http_index_error(r, clcf, path.data, of.err);
}
if (!dir_tested) {
rc = ngx_http_index_test_dir(r, clcf, path.data, name - 1);
if (rc != NGX_OK) {
return rc;
}
dir_tested = 1;
}
if (of.err == NGX_ENOENT) {
continue;
}
ngx_log_error(NGX_LOG_CRIT, r->connection->log, of.err,
"%s \"%s\" failed", of.failed, path.data);
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
uri.len = r->uri.len + len - 1;
if (!clcf->alias) {
uri.data = path.data + root;
} else {
uri.data = ngx_pnalloc(r->pool, uri.len);
if (uri.data == NULL) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
p = ngx_copy(uri.data, r->uri.data, r->uri.len);
ngx_memcpy(p, name, len - 1);
}
return ngx_http_internal_redirect(r, &uri, &r->args);
}
return NGX_DECLINED;
} | ['static ngx_int_t\nngx_http_index_handler(ngx_http_request_t *r)\n{\n u_char *p, *name;\n size_t len, root, reserve, allocated;\n ngx_int_t rc;\n ngx_str_t path, uri;\n ngx_uint_t i, dir_tested;\n ngx_http_index_t *index;\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_index_loc_conf_t *ilcf;\n ngx_http_script_len_code_pt lcode;\n if (r->uri.data[r->uri.len - 1] != \'/\') {\n return NGX_DECLINED;\n }\n if (!(r->method & (NGX_HTTP_GET|NGX_HTTP_HEAD|NGX_HTTP_POST))) {\n return NGX_DECLINED;\n }\n ilcf = ngx_http_get_module_loc_conf(r, ngx_http_index_module);\n clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);\n allocated = 0;\n root = 0;\n dir_tested = 0;\n name = NULL;\n path.data = NULL;\n index = ilcf->indices->elts;\n for (i = 0; i < ilcf->indices->nelts; i++) {\n if (index[i].lengths == NULL) {\n if (index[i].name.data[0] == \'/\') {\n return ngx_http_internal_redirect(r, &index[i].name, &r->args);\n }\n reserve = ilcf->max_index_len;\n len = index[i].name.len;\n } else {\n ngx_memzero(&e, sizeof(ngx_http_script_engine_t));\n e.ip = index[i].lengths->elts;\n e.request = r;\n e.flushed = 1;\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 reserve = len + 16;\n }\n if (reserve > allocated) {\n name = ngx_http_map_uri_to_path(r, &path, &root, reserve);\n if (name == NULL) {\n return NGX_ERROR;\n }\n allocated = path.data + path.len - name;\n }\n if (index[i].values == NULL) {\n ngx_memcpy(name, index[i].name.data, index[i].name.len);\n path.len = (name + index[i].name.len - 1) - path.data;\n } else {\n e.ip = index[i].values->elts;\n e.pos = name;\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 if (*name == \'/\') {\n uri.len = len - 1;\n uri.data = name;\n return ngx_http_internal_redirect(r, &uri, &r->args);\n }\n path.len = e.pos - path.data;\n *e.pos = \'\\0\';\n }\n ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,\n "open index \\"%V\\"", &path);\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 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, of.err,\n "%s \\"%s\\" failed", of.failed, path.data);\n if (of.err == 0) {\n return NGX_HTTP_INTERNAL_SERVER_ERROR;\n }\n if (of.err == NGX_ENOTDIR\n || of.err == NGX_ENAMETOOLONG\n || of.err == NGX_EACCES)\n {\n return ngx_http_index_error(r, clcf, path.data, of.err);\n }\n if (!dir_tested) {\n rc = ngx_http_index_test_dir(r, clcf, path.data, name - 1);\n if (rc != NGX_OK) {\n return rc;\n }\n dir_tested = 1;\n }\n if (of.err == NGX_ENOENT) {\n continue;\n }\n ngx_log_error(NGX_LOG_CRIT, r->connection->log, of.err,\n "%s \\"%s\\" failed", of.failed, path.data);\n return NGX_HTTP_INTERNAL_SERVER_ERROR;\n }\n uri.len = r->uri.len + len - 1;\n if (!clcf->alias) {\n uri.data = path.data + root;\n } else {\n uri.data = ngx_pnalloc(r->pool, uri.len);\n if (uri.data == NULL) {\n return NGX_HTTP_INTERNAL_SERVER_ERROR;\n }\n p = ngx_copy(uri.data, r->uri.data, r->uri.len);\n ngx_memcpy(p, name, len - 1);\n }\n return ngx_http_internal_redirect(r, &uri, &r->args);\n }\n return NGX_DECLINED;\n}'] |
35,191 | 0 | https://github.com/openssl/openssl/blob/9c46f4b9cd4912b61cb546c48b678488d7f26ed6/crypto/bn/bn_ctx.c/#L353 | static unsigned int BN_STACK_pop(BN_STACK *st)
{
return st->indexes[--(st->depth)];
} | ['int 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 if (new_ctx != NULL)\n BN_CTX_free(new_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}', '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}', 'int BN_mod_sqr(BIGNUM *r, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx)\n{\n if (!BN_sqr(r, a, ctx))\n return 0;\n return BN_mod(r, r, m, ctx);\n}', 'int BN_sqr(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx)\n{\n int max, al;\n int ret = 0;\n BIGNUM *tmp, *rr;\n bn_check_top(a);\n al = a->top;\n if (al <= 0) {\n r->top = 0;\n r->neg = 0;\n return 1;\n }\n BN_CTX_start(ctx);\n rr = (a != r) ? r : BN_CTX_get(ctx);\n tmp = BN_CTX_get(ctx);\n if (!rr || !tmp)\n goto err;\n max = 2 * al;\n if (bn_wexpand(rr, max) == NULL)\n goto err;\n if (al == 4) {\n#ifndef BN_SQR_COMBA\n BN_ULONG t[8];\n bn_sqr_normal(rr->d, a->d, 4, t);\n#else\n bn_sqr_comba4(rr->d, a->d);\n#endif\n } else if (al == 8) {\n#ifndef BN_SQR_COMBA\n BN_ULONG t[16];\n bn_sqr_normal(rr->d, a->d, 8, t);\n#else\n bn_sqr_comba8(rr->d, a->d);\n#endif\n } else {\n#if defined(BN_RECURSION)\n if (al < BN_SQR_RECURSIVE_SIZE_NORMAL) {\n BN_ULONG t[BN_SQR_RECURSIVE_SIZE_NORMAL * 2];\n bn_sqr_normal(rr->d, a->d, al, t);\n } else {\n int j, k;\n j = BN_num_bits_word((BN_ULONG)al);\n j = 1 << (j - 1);\n k = j + j;\n if (al == j) {\n if (bn_wexpand(tmp, k * 2) == NULL)\n goto err;\n bn_sqr_recursive(rr->d, a->d, al, tmp->d);\n } else {\n if (bn_wexpand(tmp, max) == NULL)\n goto err;\n bn_sqr_normal(rr->d, a->d, al, tmp->d);\n }\n }\n#else\n if (bn_wexpand(tmp, max) == NULL)\n goto err;\n bn_sqr_normal(rr->d, a->d, al, tmp->d);\n#endif\n }\n rr->neg = 0;\n if (a->d[al - 1] == (a->d[al - 1] & BN_MASK2l))\n rr->top = max - 1;\n else\n rr->top = max;\n if (rr != r)\n BN_copy(r, rr);\n ret = 1;\n err:\n bn_check_top(rr);\n bn_check_top(tmp);\n BN_CTX_end(ctx);\n return (ret);\n}', '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_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}'] |
35,192 | 0 | https://github.com/openssl/openssl/blob/8de2910b5cbfa6169b54d1267abf654da773f00a/crypto/bn/bn_ctx.c/#L143 | void BN_CTX_end(BN_CTX *ctx)
{
if (ctx == NULL) return;
assert(ctx->depth > 0);
if (ctx->depth == 0)
BN_CTX_start(ctx);
ctx->too_many = 0;
ctx->depth--;
if (ctx->depth < BN_CTX_NUM_POS)
ctx->tos = ctx->pos[ctx->depth];
} | ['static int RSA_eay_public_encrypt(int flen, const unsigned char *from,\n\t unsigned char *to, RSA *rsa, int padding)\n\t{\n\tconst RSA_METHOD *meth;\n\tBIGNUM f,ret;\n\tint i,j,k,num=0,r= -1;\n\tunsigned char *buf=NULL;\n\tBN_CTX *ctx=NULL;\n\tmeth = ENGINE_get_RSA(rsa->engine);\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 *)OPENSSL_malloc(num)) == NULL)\n\t\t{\n\t\tRSAerr(RSA_F_RSA_EAY_PUBLIC_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_2(buf,num,from,flen);\n\t\tbreak;\n#ifndef NO_SHA\n\tcase RSA_PKCS1_OAEP_PADDING:\n\t i=RSA_padding_add_PKCS1_OAEP(buf,num,from,flen,NULL,0);\n\t\tbreak;\n#endif\n\tcase RSA_SSLV23_PADDING:\n\t\ti=RSA_padding_add_SSLv23(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\tdefault:\n\t\tRSAerr(RSA_F_RSA_EAY_PUBLIC_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->_method_mod_n == NULL) && (rsa->flags & RSA_FLAG_CACHE_PUBLIC))\n\t\t{\n\t\tif ((rsa->_method_mod_n=BN_MONT_CTX_new()) != NULL)\n\t\t\tif (!BN_MONT_CTX_set(rsa->_method_mod_n,rsa->n,ctx))\n\t\t\t goto err;\n\t\t}\n\tif (!meth->bn_mod_exp(&ret,&f,rsa->e,rsa->n,ctx,\n\t\trsa->_method_mod_n)) 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(&f);\n\tBN_clear_free(&ret);\n\tif (buf != NULL)\n\t\t{\n\t\tmemset(buf,0,num);\n\t\tOPENSSL_free(buf);\n\t\t}\n\treturn(r);\n\t}', 'BN_CTX *BN_CTX_new(void)\n\t{\n\tBN_CTX *ret;\n\tret=(BN_CTX *)OPENSSL_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}', 'void BN_CTX_init(BN_CTX *ctx)\n\t{\n\tint i;\n\tctx->tos = 0;\n\tctx->flags = 0;\n\tctx->depth = 0;\n\tctx->too_many = 0;\n\tfor (i = 0; i < BN_CTX_NUM; i++)\n\t\tBN_init(&(ctx->bn[i]));\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 MONT_WORD\n\t\t{\n\t\tBIGNUM tmod;\n\t\tBN_ULONG buf[2];\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.dmax=2;\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\tBN_sub_word(&Ri,1);\n\t\telse\n\t\t\tBN_set_word(&Ri,BN_MASK2);\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#else\n\t\t{\n\t\tmont->ri=BN_num_bits(mod);\n\t\tBN_zero(R);\n\t\tBN_set_bit(R,mont->ri);\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\t\tBN_sub_word(&Ri,1);\n\t\tBN_div(&(mont->Ni),NULL,&Ri,mod,ctx);\n\t\tBN_free(&Ri);\n\t\t}\n#endif\n\tBN_zero(&(mont->RR));\n\tBN_set_bit(&(mont->RR),mont->ri*2);\n\tBN_mod(&(mont->RR),&(mont->RR),&(mont->N),ctx);\n\treturn(1);\nerr:\n\treturn(0);\n\t}', 'BIGNUM *BN_mod_inverse(BIGNUM *in,\n\tconst BIGNUM *a, const BIGNUM *n, BN_CTX *ctx)\n\t{\n\tBIGNUM *A,*B,*X,*Y,*M,*D,*R=NULL;\n\tBIGNUM *T,*ret=NULL;\n\tint sign;\n\tbn_check_top(a);\n\tbn_check_top(n);\n\tBN_CTX_start(ctx);\n\tA = BN_CTX_get(ctx);\n\tB = BN_CTX_get(ctx);\n\tX = BN_CTX_get(ctx);\n\tD = BN_CTX_get(ctx);\n\tM = BN_CTX_get(ctx);\n\tY = BN_CTX_get(ctx);\n\tif (Y == NULL) goto err;\n\tif (in == NULL)\n\t\tR=BN_new();\n\telse\n\t\tR=in;\n\tif (R == NULL) goto err;\n\tBN_zero(X);\n\tBN_one(Y);\n\tif (BN_copy(A,a) == NULL) goto err;\n\tif (BN_copy(B,n) == NULL) goto err;\n\tsign=1;\n\twhile (!BN_is_zero(B))\n\t\t{\n\t\tif (!BN_div(D,M,A,B,ctx)) goto err;\n\t\tT=A;\n\t\tA=B;\n\t\tB=M;\n\t\tif (!BN_mul(T,D,X,ctx)) goto err;\n\t\tif (!BN_add(T,T,Y)) goto err;\n\t\tM=Y;\n\t\tY=X;\n\t\tX=T;\n\t\tsign= -sign;\n\t\t}\n\tif (sign < 0)\n\t\t{\n\t\tif (!BN_sub(Y,n,Y)) goto err;\n\t\t}\n\tif (BN_is_one(A))\n\t\t{ if (!BN_mod(R,Y,n,ctx)) goto err; }\n\telse\n\t\t{\n\t\tBNerr(BN_F_BN_MOD_INVERSE,BN_R_NO_INVERSE);\n\t\tgoto err;\n\t\t}\n\tret=R;\nerr:\n\tif ((ret == NULL) && (in == NULL)) BN_free(R);\n\tBN_CTX_end(ctx);\n\treturn(ret);\n\t}', 'void BN_CTX_start(BN_CTX *ctx)\n\t{\n\tif (ctx->depth < BN_CTX_NUM_POS)\n\t\tctx->pos[ctx->depth] = ctx->tos;\n\tctx->depth++;\n\t}', 'BIGNUM *BN_CTX_get(BN_CTX *ctx)\n\t{\n\tif (ctx->depth > BN_CTX_NUM_POS || ctx->tos >= BN_CTX_NUM)\n\t\t{\n\t\tif (!ctx->too_many)\n\t\t\t{\n\t\t\tBNerr(BN_F_BN_CTX_GET,BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n\t\t\tctx->too_many = 1;\n\t\t\t}\n\t\treturn NULL;\n\t\t}\n\treturn (&(ctx->bn[ctx->tos++]));\n\t}', 'void BN_CTX_end(BN_CTX *ctx)\n\t{\n\tif (ctx == NULL) return;\n\tassert(ctx->depth > 0);\n\tif (ctx->depth == 0)\n\t\tBN_CTX_start(ctx);\n\tctx->too_many = 0;\n\tctx->depth--;\n\tif (ctx->depth < BN_CTX_NUM_POS)\n\t\tctx->tos = ctx->pos[ctx->depth];\n\t}'] |
35,193 | 0 | https://github.com/libav/libav/blob/42f9132218ca11a8e9a3c82a175b46bca092113e/libavfilter/vf_yadif.c/#L466 | static int config_props(AVFilterLink *link)
{
YADIFContext *s = link->src->priv;
link->time_base.num = link->src->inputs[0]->time_base.num;
link->time_base.den = link->src->inputs[0]->time_base.den * 2;
link->w = link->src->inputs[0]->w;
link->h = link->src->inputs[0]->h;
s->csp = av_pix_fmt_desc_get(link->format);
if (s->csp->comp[0].depth_minus1 / 8 == 1) {
s->filter_line = filter_line_c_16bit;
s->filter_edges = filter_edges_16bit;
} else {
s->filter_line = filter_line_c;
s->filter_edges = filter_edges;
if (ARCH_X86)
ff_yadif_init_x86(s);
}
return 0;
} | ['static int config_props(AVFilterLink *link)\n{\n YADIFContext *s = link->src->priv;\n link->time_base.num = link->src->inputs[0]->time_base.num;\n link->time_base.den = link->src->inputs[0]->time_base.den * 2;\n link->w = link->src->inputs[0]->w;\n link->h = link->src->inputs[0]->h;\n s->csp = av_pix_fmt_desc_get(link->format);\n if (s->csp->comp[0].depth_minus1 / 8 == 1) {\n s->filter_line = filter_line_c_16bit;\n s->filter_edges = filter_edges_16bit;\n } else {\n s->filter_line = filter_line_c;\n s->filter_edges = filter_edges;\n if (ARCH_X86)\n ff_yadif_init_x86(s);\n }\n return 0;\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}'] |
35,194 | 0 | https://github.com/libav/libav/blob/a2fb4bcb0189f6421608e0dec1a38c65910763f6/libavcodec/cook.c/#L475 | static void categorize(COOKContext *q, COOKSubpacket *p, int* quant_index_table,
int* category, int* category_index){
int exp_idx, bias, tmpbias1, tmpbias2, bits_left, num_bits, index, v, i, j;
int exp_index2[102];
int exp_index1[102];
int tmp_categorize_array[128*2];
int tmp_categorize_array1_idx=p->numvector_size;
int tmp_categorize_array2_idx=p->numvector_size;
bits_left = p->bits_per_subpacket - get_bits_count(&q->gb);
if(bits_left > q->samples_per_channel) {
bits_left = q->samples_per_channel +
((bits_left - q->samples_per_channel)*5)/8;
}
memset(&exp_index1, 0, sizeof(exp_index1));
memset(&exp_index2, 0, sizeof(exp_index2));
memset(&tmp_categorize_array, 0, sizeof(tmp_categorize_array));
bias=-32;
for (i=32 ; i>0 ; i=i/2){
num_bits = 0;
index = 0;
for (j=p->total_subbands ; j>0 ; j--){
exp_idx = av_clip((i - quant_index_table[index] + bias) / 2, 0, 7);
index++;
num_bits+=expbits_tab[exp_idx];
}
if(num_bits >= bits_left - 32){
bias+=i;
}
}
num_bits=0;
for (i=0 ; i<p->total_subbands ; i++) {
exp_idx = av_clip((bias - quant_index_table[i]) / 2, 0, 7);
num_bits += expbits_tab[exp_idx];
exp_index1[i] = exp_idx;
exp_index2[i] = exp_idx;
}
tmpbias1 = tmpbias2 = num_bits;
for (j = 1 ; j < p->numvector_size ; j++) {
if (tmpbias1 + tmpbias2 > 2*bits_left) {
int max = -999999;
index=-1;
for (i=0 ; i<p->total_subbands ; i++){
if (exp_index1[i] < 7) {
v = (-2*exp_index1[i]) - quant_index_table[i] + bias;
if ( v >= max) {
max = v;
index = i;
}
}
}
if(index==-1)break;
tmp_categorize_array[tmp_categorize_array1_idx++] = index;
tmpbias1 -= expbits_tab[exp_index1[index]] -
expbits_tab[exp_index1[index]+1];
++exp_index1[index];
} else {
int min = 999999;
index=-1;
for (i=0 ; i<p->total_subbands ; i++){
if(exp_index2[i] > 0){
v = (-2*exp_index2[i])-quant_index_table[i]+bias;
if ( v < min) {
min = v;
index = i;
}
}
}
if(index == -1)break;
tmp_categorize_array[--tmp_categorize_array2_idx] = index;
tmpbias2 -= expbits_tab[exp_index2[index]] -
expbits_tab[exp_index2[index]-1];
--exp_index2[index];
}
}
for(i=0 ; i<p->total_subbands ; i++)
category[i] = exp_index2[i];
for(i=0 ; i<p->numvector_size-1 ; i++)
category_index[i] = tmp_categorize_array[tmp_categorize_array2_idx++];
} | ['static int cook_decode_frame(AVCodecContext *avctx, void *data,\n int *got_frame_ptr, AVPacket *avpkt)\n{\n const uint8_t *buf = avpkt->data;\n int buf_size = avpkt->size;\n COOKContext *q = avctx->priv_data;\n float *samples = NULL;\n int i, ret;\n int offset = 0;\n int chidx = 0;\n if (buf_size < avctx->block_align)\n return buf_size;\n if (q->discarded_packets >= 2) {\n q->frame.nb_samples = q->samples_per_channel;\n if ((ret = avctx->get_buffer(avctx, &q->frame)) < 0) {\n av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\\n");\n return ret;\n }\n samples = (float *)q->frame.data[0];\n }\n q->subpacket[0].size = avctx->block_align;\n for(i=1;i<q->num_subpackets;i++){\n q->subpacket[i].size = 2 * buf[avctx->block_align - q->num_subpackets + i];\n q->subpacket[0].size -= q->subpacket[i].size + 1;\n if (q->subpacket[0].size < 0) {\n av_log(avctx,AV_LOG_DEBUG,"frame subpacket size total > avctx->block_align!\\n");\n return AVERROR_INVALIDDATA;\n }\n }\n for(i=0;i<q->num_subpackets;i++){\n q->subpacket[i].bits_per_subpacket = (q->subpacket[i].size*8)>>q->subpacket[i].bits_per_subpdiv;\n q->subpacket[i].ch_idx = chidx;\n av_log(avctx,AV_LOG_DEBUG,"subpacket[%i] size %i js %i %i block_align %i\\n",i,q->subpacket[i].size,q->subpacket[i].joint_stereo,offset,avctx->block_align);\n decode_subpacket(q, &q->subpacket[i], buf + offset, samples);\n offset += q->subpacket[i].size;\n chidx += q->subpacket[i].num_channels;\n av_log(avctx,AV_LOG_DEBUG,"subpacket[%i] %i %i\\n",i,q->subpacket[i].size * 8,get_bits_count(&q->gb));\n }\n if (q->discarded_packets < 2) {\n q->discarded_packets++;\n *got_frame_ptr = 0;\n return avctx->block_align;\n }\n *got_frame_ptr = 1;\n *(AVFrame *)data = q->frame;\n return avctx->block_align;\n}', 'static void decode_subpacket(COOKContext *q, COOKSubpacket *p,\n const uint8_t *inbuffer, float *outbuffer)\n{\n int sub_packet_size = p->size;\n memset(q->decode_buffer_1,0,sizeof(q->decode_buffer_1));\n decode_bytes_and_gain(q, p, inbuffer, &p->gains1);\n if (p->joint_stereo) {\n joint_decode(q, p, q->decode_buffer_1, q->decode_buffer_2);\n } else {\n mono_decode(q, p, q->decode_buffer_1);\n if (p->num_channels == 2) {\n decode_bytes_and_gain(q, p, inbuffer + sub_packet_size/2, &p->gains2);\n mono_decode(q, p, q->decode_buffer_2);\n }\n }\n mlt_compensate_output(q, q->decode_buffer_1, &p->gains1,\n p->mono_previous_buffer1, outbuffer, p->ch_idx);\n if (p->num_channels == 2) {\n if (p->joint_stereo) {\n mlt_compensate_output(q, q->decode_buffer_2, &p->gains1,\n p->mono_previous_buffer2, outbuffer, p->ch_idx + 1);\n } else {\n mlt_compensate_output(q, q->decode_buffer_2, &p->gains2,\n p->mono_previous_buffer2, outbuffer, p->ch_idx + 1);\n }\n }\n}', 'static void mono_decode(COOKContext *q, COOKSubpacket *p, float* mlt_buffer) {\n int category_index[128];\n int quant_index_table[102];\n int category[128];\n memset(&category, 0, sizeof(category));\n memset(&category_index, 0, sizeof(category_index));\n decode_envelope(q, p, quant_index_table);\n q->num_vectors = get_bits(&q->gb,p->log2_numvector_size);\n categorize(q, p, quant_index_table, category, category_index);\n expand_category(q, category, category_index);\n decode_vectors(q, p, category, quant_index_table, mlt_buffer);\n}', 'static void categorize(COOKContext *q, COOKSubpacket *p, int* quant_index_table,\n int* category, int* category_index){\n int exp_idx, bias, tmpbias1, tmpbias2, bits_left, num_bits, index, v, i, j;\n int exp_index2[102];\n int exp_index1[102];\n int tmp_categorize_array[128*2];\n int tmp_categorize_array1_idx=p->numvector_size;\n int tmp_categorize_array2_idx=p->numvector_size;\n bits_left = p->bits_per_subpacket - get_bits_count(&q->gb);\n if(bits_left > q->samples_per_channel) {\n bits_left = q->samples_per_channel +\n ((bits_left - q->samples_per_channel)*5)/8;\n }\n memset(&exp_index1, 0, sizeof(exp_index1));\n memset(&exp_index2, 0, sizeof(exp_index2));\n memset(&tmp_categorize_array, 0, sizeof(tmp_categorize_array));\n bias=-32;\n for (i=32 ; i>0 ; i=i/2){\n num_bits = 0;\n index = 0;\n for (j=p->total_subbands ; j>0 ; j--){\n exp_idx = av_clip((i - quant_index_table[index] + bias) / 2, 0, 7);\n index++;\n num_bits+=expbits_tab[exp_idx];\n }\n if(num_bits >= bits_left - 32){\n bias+=i;\n }\n }\n num_bits=0;\n for (i=0 ; i<p->total_subbands ; i++) {\n exp_idx = av_clip((bias - quant_index_table[i]) / 2, 0, 7);\n num_bits += expbits_tab[exp_idx];\n exp_index1[i] = exp_idx;\n exp_index2[i] = exp_idx;\n }\n tmpbias1 = tmpbias2 = num_bits;\n for (j = 1 ; j < p->numvector_size ; j++) {\n if (tmpbias1 + tmpbias2 > 2*bits_left) {\n int max = -999999;\n index=-1;\n for (i=0 ; i<p->total_subbands ; i++){\n if (exp_index1[i] < 7) {\n v = (-2*exp_index1[i]) - quant_index_table[i] + bias;\n if ( v >= max) {\n max = v;\n index = i;\n }\n }\n }\n if(index==-1)break;\n tmp_categorize_array[tmp_categorize_array1_idx++] = index;\n tmpbias1 -= expbits_tab[exp_index1[index]] -\n expbits_tab[exp_index1[index]+1];\n ++exp_index1[index];\n } else {\n int min = 999999;\n index=-1;\n for (i=0 ; i<p->total_subbands ; i++){\n if(exp_index2[i] > 0){\n v = (-2*exp_index2[i])-quant_index_table[i]+bias;\n if ( v < min) {\n min = v;\n index = i;\n }\n }\n }\n if(index == -1)break;\n tmp_categorize_array[--tmp_categorize_array2_idx] = index;\n tmpbias2 -= expbits_tab[exp_index2[index]] -\n expbits_tab[exp_index2[index]-1];\n --exp_index2[index];\n }\n }\n for(i=0 ; i<p->total_subbands ; i++)\n category[i] = exp_index2[i];\n for(i=0 ; i<p->numvector_size-1 ; i++)\n category_index[i] = tmp_categorize_array[tmp_categorize_array2_idx++];\n}'] |
35,195 | 0 | https://gitlab.com/libtiff/libtiff/blob/6c63f17749ad8fc2e1e94e056b18cbc557ab59f1/libtiff/tif_tile.c/#L128 | uint32
TIFFNumberOfTiles(TIFF* tif)
{
TIFFDirectory *td = &tif->tif_dir;
uint32 dx = td->td_tilewidth;
uint32 dy = td->td_tilelength;
uint32 dz = td->td_tiledepth;
uint32 ntiles;
if (dx == (uint32) -1)
dx = td->td_imagewidth;
if (dy == (uint32) -1)
dy = td->td_imagelength;
if (dz == (uint32) -1)
dz = td->td_imagedepth;
ntiles = (dx == 0 || dy == 0 || dz == 0) ? 0 :
_TIFFMultiply32(tif, _TIFFMultiply32(tif, TIFFhowmany_32(td->td_imagewidth, dx),
TIFFhowmany_32(td->td_imagelength, dy),
"TIFFNumberOfTiles"),
TIFFhowmany_32(td->td_imagedepth, dz), "TIFFNumberOfTiles");
if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
ntiles = _TIFFMultiply32(tif, ntiles, td->td_samplesperpixel,
"TIFFNumberOfTiles");
return (ntiles);
} | ['static TIFF* openSrcImage (char **imageSpec)\n{\n\tTIFF *tif;\n\tchar *fn = *imageSpec;\n\t*imageSpec = strchr (fn, comma);\n\tif (*imageSpec) {\n\t\t**imageSpec = \'\\0\';\n\t\ttif = TIFFOpen (fn, "r");\n\t\tif (!(*imageSpec)[1]) {*imageSpec = NULL; return tif;}\n\t\tif (tif) {\n\t\t\t**imageSpec = comma;\n\t\t\tif (!nextSrcImage(tif, imageSpec)) {\n\t\t\t\tTIFFClose (tif);\n\t\t\t\ttif = NULL;\n\t\t\t}\n\t\t}\n\t}else\n\t\ttif = TIFFOpen (fn, "r");\n\treturn tif;\n}', 'static int nextSrcImage (TIFF *tif, char **imageSpec)\n{\n\tif (**imageSpec == comma) {\n\t\tchar *start = *imageSpec + 1;\n\t\ttdir_t nextImage = (tdir_t)strtol(start, imageSpec, 0);\n\t\tif (start == *imageSpec) nextImage = TIFFCurrentDirectory (tif);\n\t\tif (**imageSpec)\n\t\t{\n\t\t\tif (**imageSpec == comma) {\n\t\t\t\tif ((*imageSpec)[1] == \'\\0\') *imageSpec = NULL;\n\t\t\t}else{\n\t\t\t\tfprintf (stderr,\n\t\t\t\t "Expected a %c separated image # list after %s\\n",\n\t\t\t\t comma, TIFFFileName (tif));\n\t\t\t\texit (-4);\n\t\t\t}\n\t\t}\n\t\tif (TIFFSetDirectory (tif, nextImage)) return 1;\n\t\tfprintf (stderr, "%s%c%d not found!\\n",\n\t\t TIFFFileName(tif), comma, (int) nextImage);\n\t}\n\treturn 0;\n}', 'void\nTIFFClose(TIFF* tif)\n{\n\tTIFFCloseProc closeproc = tif->tif_closeproc;\n\tthandle_t fd = tif->tif_clientdata;\n\tTIFFCleanup(tif);\n\t(void) (*closeproc)(fd);\n}', 'void\nTIFFCleanup(TIFF* tif)\n{\n\tif (tif->tif_mode != O_RDONLY)\n\t\tTIFFFlush(tif);\n\t(*tif->tif_cleanup)(tif);\n\tTIFFFreeDirectory(tif);\n\tif (tif->tif_dirlist)\n\t\t_TIFFfree(tif->tif_dirlist);\n\twhile( tif->tif_clientinfo )\n\t{\n\t\tTIFFClientInfoLink *psLink = tif->tif_clientinfo;\n\t\ttif->tif_clientinfo = psLink->next;\n\t\t_TIFFfree( psLink->name );\n\t\t_TIFFfree( psLink );\n\t}\n\tif (tif->tif_rawdata && (tif->tif_flags&TIFF_MYBUFFER))\n\t\t_TIFFfree(tif->tif_rawdata);\n\tif (isMapped(tif))\n\t\tTIFFUnmapFileContents(tif, tif->tif_base, (toff_t)tif->tif_size);\n\tif (tif->tif_fields && tif->tif_nfields > 0) {\n\t\tuint32 i;\n\t\tfor (i = 0; i < tif->tif_nfields; i++) {\n\t\t\tTIFFField *fld = tif->tif_fields[i];\n\t\t\tif (fld->field_bit == FIELD_CUSTOM &&\n\t\t\t strncmp("Tag ", fld->field_name, 4) == 0) {\n\t\t\t\t_TIFFfree(fld->field_name);\n\t\t\t\t_TIFFfree(fld);\n\t\t\t}\n\t\t}\n\t\t_TIFFfree(tif->tif_fields);\n\t}\n if (tif->tif_nfieldscompat > 0) {\n uint32 i;\n for (i = 0; i < tif->tif_nfieldscompat; i++) {\n if (tif->tif_fieldscompat[i].allocated_size)\n _TIFFfree(tif->tif_fieldscompat[i].fields);\n }\n _TIFFfree(tif->tif_fieldscompat);\n }\n\t_TIFFfree(tif);\n}', 'int\nTIFFFlush(TIFF* tif)\n{\n if( tif->tif_mode == O_RDONLY )\n return 1;\n if (!TIFFFlushData(tif))\n return (0);\n if( (tif->tif_flags & TIFF_DIRTYSTRIP)\n && !(tif->tif_flags & TIFF_DIRTYDIRECT)\n && tif->tif_mode == O_RDWR )\n {\n if( TIFFForceStrileArrayWriting(tif) )\n return 1;\n }\n if ((tif->tif_flags & (TIFF_DIRTYDIRECT|TIFF_DIRTYSTRIP))\n && !TIFFRewriteDirectory(tif))\n return (0);\n return (1);\n}', 'int TIFFForceStrileArrayWriting(TIFF* tif)\n{\n static const char module[] = "TIFFForceStrileArrayWriting";\n const int isTiled = TIFFIsTiled(tif);\n if (tif->tif_mode == O_RDONLY)\n {\n TIFFErrorExt(tif->tif_clientdata, tif->tif_name,\n "File opened in read-only mode");\n return 0;\n }\n if( tif->tif_diroff == 0 )\n {\n TIFFErrorExt(tif->tif_clientdata, module,\n "Directory has not yet been written");\n return 0;\n }\n if( (tif->tif_flags & TIFF_DIRTYDIRECT) != 0 )\n {\n TIFFErrorExt(tif->tif_clientdata, module,\n "Directory has changes other than the strile arrays. "\n "TIFFRewriteDirectory() should be called instead");\n return 0;\n }\n if( !(tif->tif_flags & TIFF_DIRTYSTRIP) )\n {\n if( !(tif->tif_dir.td_stripoffset_entry.tdir_tag != 0 &&\n tif->tif_dir.td_stripoffset_entry.tdir_count == 0 &&\n tif->tif_dir.td_stripoffset_entry.tdir_type == 0 &&\n tif->tif_dir.td_stripoffset_entry.tdir_offset.toff_long8 == 0 &&\n tif->tif_dir.td_stripbytecount_entry.tdir_tag != 0 &&\n tif->tif_dir.td_stripbytecount_entry.tdir_count == 0 &&\n tif->tif_dir.td_stripbytecount_entry.tdir_type == 0 &&\n tif->tif_dir.td_stripbytecount_entry.tdir_offset.toff_long8 == 0) )\n {\n TIFFErrorExt(tif->tif_clientdata, module,\n "Function not called together with "\n "TIFFDeferStrileArrayWriting()");\n return 0;\n }\n if (tif->tif_dir.td_stripoffset_p == NULL && !TIFFSetupStrips(tif))\n return 0;\n }\n if( _TIFFRewriteField( tif,\n isTiled ? TIFFTAG_TILEOFFSETS :\n TIFFTAG_STRIPOFFSETS,\n TIFF_LONG8,\n tif->tif_dir.td_nstrips,\n tif->tif_dir.td_stripoffset_p )\n && _TIFFRewriteField( tif,\n isTiled ? TIFFTAG_TILEBYTECOUNTS :\n TIFFTAG_STRIPBYTECOUNTS,\n TIFF_LONG8,\n tif->tif_dir.td_nstrips,\n tif->tif_dir.td_stripbytecount_p ) )\n {\n tif->tif_flags &= ~TIFF_DIRTYSTRIP;\n tif->tif_flags &= ~TIFF_BEENWRITING;\n return 1;\n }\n return 0;\n}', 'int\nTIFFSetupStrips(TIFF* tif)\n{\n\tTIFFDirectory* td = &tif->tif_dir;\n\tif (isTiled(tif))\n\t\ttd->td_stripsperimage =\n\t\t isUnspecified(tif, FIELD_TILEDIMENSIONS) ?\n\t\t\ttd->td_samplesperpixel : TIFFNumberOfTiles(tif);\n\telse\n\t\ttd->td_stripsperimage =\n\t\t isUnspecified(tif, FIELD_ROWSPERSTRIP) ?\n\t\t\ttd->td_samplesperpixel : TIFFNumberOfStrips(tif);\n\ttd->td_nstrips = td->td_stripsperimage;\n if( td->td_nstrips >= 0x80000000U / ((tif->tif_flags&TIFF_BIGTIFF)?0x8U:0x4U) )\n {\n TIFFErrorExt(tif->tif_clientdata, "TIFFSetupStrips",\n "Too large Strip/Tile Offsets/ByteCounts arrays");\n return 0;\n }\n\tif (td->td_planarconfig == PLANARCONFIG_SEPARATE)\n\t\ttd->td_stripsperimage /= td->td_samplesperpixel;\n\ttd->td_stripoffset_p = (uint64 *)\n _TIFFCheckMalloc(tif, td->td_nstrips, sizeof (uint64),\n "for \\"StripOffsets\\" array");\n\ttd->td_stripbytecount_p = (uint64 *)\n _TIFFCheckMalloc(tif, td->td_nstrips, sizeof (uint64),\n "for \\"StripByteCounts\\" array");\n\tif (td->td_stripoffset_p == NULL || td->td_stripbytecount_p == NULL)\n\t\treturn (0);\n\t_TIFFmemset(td->td_stripoffset_p, 0, td->td_nstrips*sizeof (uint64));\n\t_TIFFmemset(td->td_stripbytecount_p, 0, td->td_nstrips*sizeof (uint64));\n\tTIFFSetFieldBit(tif, FIELD_STRIPOFFSETS);\n\tTIFFSetFieldBit(tif, FIELD_STRIPBYTECOUNTS);\n\treturn (1);\n}', 'uint32\nTIFFNumberOfTiles(TIFF* tif)\n{\n\tTIFFDirectory *td = &tif->tif_dir;\n\tuint32 dx = td->td_tilewidth;\n\tuint32 dy = td->td_tilelength;\n\tuint32 dz = td->td_tiledepth;\n\tuint32 ntiles;\n\tif (dx == (uint32) -1)\n\t\tdx = td->td_imagewidth;\n\tif (dy == (uint32) -1)\n\t\tdy = td->td_imagelength;\n\tif (dz == (uint32) -1)\n\t\tdz = td->td_imagedepth;\n\tntiles = (dx == 0 || dy == 0 || dz == 0) ? 0 :\n\t _TIFFMultiply32(tif, _TIFFMultiply32(tif, TIFFhowmany_32(td->td_imagewidth, dx),\n\t TIFFhowmany_32(td->td_imagelength, dy),\n\t "TIFFNumberOfTiles"),\n\t TIFFhowmany_32(td->td_imagedepth, dz), "TIFFNumberOfTiles");\n\tif (td->td_planarconfig == PLANARCONFIG_SEPARATE)\n\t\tntiles = _TIFFMultiply32(tif, ntiles, td->td_samplesperpixel,\n\t\t "TIFFNumberOfTiles");\n\treturn (ntiles);\n}'] |
35,196 | 0 | https://github.com/libav/libav/blob/13c7df3dc0625d7a559a7f02644c27755e0569bd/libavformat/flvdec.c/#L386 | static int flv_read_packet(AVFormatContext *s, AVPacket *pkt)
{
int ret, i, type, size, flags, is_audio, next, pos;
unsigned dts;
AVStream *st = NULL;
retry:
for(;;){
pos = url_ftell(s->pb);
url_fskip(s->pb, 4);
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(EIO);
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_ERROR, "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(NULL, 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==AV_NOPTS_VALUE){
int size;
const int pos= url_ftell(s->pb);
const int 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){
s->duration= get_be24(s->pb) * (int64_t)AV_TIME_BASE / 1000;
}
url_fseek(s->pb, pos, SEEK_SET);
}
if(is_audio){
if(!st->codec->sample_rate || !st->codec->bits_per_coded_sample || (!st->codec->codec_id && !st->codec->codec_tag)) {
st->codec->channels = (flags & FLV_AUDIO_CHANNEL_MASK) == FLV_STEREO ? 2 : 1;
if((flags & FLV_AUDIO_CODECID_MASK) == FLV_CODECID_NELLYMOSER_8KHZ_MONO)
st->codec->sample_rate= 8000;
else
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;
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) {
get_be24(s->pb);
}
if (type == 0) {
if ((ret = flv_get_extradata(s, st, size)) < 0)
return ret;
goto retry;
}
}
ret= av_get_packet(s->pb, pkt, size);
if (ret <= 0) {
return AVERROR(EIO);
}
pkt->size = ret;
pkt->dts = dts;
pkt->stream_index = st->index;
if (is_audio || ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY))
pkt->flags |= PKT_FLAG_KEY;
return ret;
} | ['static int flv_read_packet(AVFormatContext *s, AVPacket *pkt)\n{\n int ret, i, type, size, flags, is_audio, next, pos;\n unsigned dts;\n AVStream *st = NULL;\n retry:\n for(;;){\n pos = url_ftell(s->pb);\n url_fskip(s->pb, 4);\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(EIO);\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_ERROR, "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(NULL, 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==AV_NOPTS_VALUE){\n int size;\n const int pos= url_ftell(s->pb);\n const int 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 s->duration= get_be24(s->pb) * (int64_t)AV_TIME_BASE / 1000;\n }\n url_fseek(s->pb, pos, SEEK_SET);\n }\n if(is_audio){\n if(!st->codec->sample_rate || !st->codec->bits_per_coded_sample || (!st->codec->codec_id && !st->codec->codec_tag)) {\n st->codec->channels = (flags & FLV_AUDIO_CHANNEL_MASK) == FLV_STEREO ? 2 : 1;\n if((flags & FLV_AUDIO_CODECID_MASK) == FLV_CODECID_NELLYMOSER_8KHZ_MONO)\n st->codec->sample_rate= 8000;\n else\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 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 get_be24(s->pb);\n }\n if (type == 0) {\n if ((ret = flv_get_extradata(s, st, size)) < 0)\n return ret;\n goto retry;\n }\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->stream_index = st->index;\n if (is_audio || ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY))\n pkt->flags |= PKT_FLAG_KEY;\n return ret;\n}'] |
35,197 | 0 | https://github.com/openssl/openssl/blob/0f3e6045898e9aa5d0249e61c874b1f153ae54fa/crypto/lhash/lhash.c/#L243 | char *lh_delete(LHASH *lh, char *data)
{
unsigned long hash;
LHASH_NODE *nn,**rn;
char *ret;
lh->error=0;
rn=getrn(lh,data,&hash);
if (*rn == NULL)
{
lh->num_no_delete++;
return(NULL);
}
else
{
nn= *rn;
*rn=nn->next;
ret=nn->data;
Free((char *)nn);
lh->num_delete++;
}
lh->num_items--;
if ((lh->num_nodes > MIN_NODES) &&
(lh->down_load >= (lh->num_items*LH_LOAD_MULT/lh->num_nodes)))
contract(lh);
return(ret);
} | ['long ssl3_get_message(SSL *s, int st1, int stn, int mt, long max, int *ok)\n\t{\n\tunsigned char *p;\n\tunsigned long l;\n\tlong n;\n\tint i,al;\n\tif (s->s3->tmp.reuse_message)\n\t\t{\n\t\ts->s3->tmp.reuse_message=0;\n\t\tif ((mt >= 0) && (s->s3->tmp.message_type != mt))\n\t\t\t{\n\t\t\tal=SSL_AD_UNEXPECTED_MESSAGE;\n\t\t\tSSLerr(SSL_F_SSL3_GET_MESSAGE,SSL_R_UNEXPECTED_MESSAGE);\n\t\t\tgoto f_err;\n\t\t\t}\n\t\t*ok=1;\n\t\treturn((int)s->s3->tmp.message_size);\n\t\t}\n\tp=(unsigned char *)s->init_buf->data;\n\tif (s->state == st1)\n\t\t{\n\t\ti=ssl3_read_bytes(s,SSL3_RT_HANDSHAKE,&p[s->init_num],\n\t\t\t\t 4-s->init_num);\n\t\tif (i < (4-s->init_num))\n\t\t\t{\n\t\t\t*ok=0;\n\t\t\treturn(ssl3_part_read(s,i));\n\t\t\t}\n\t\tif ((mt >= 0) && (*p != mt))\n\t\t\t{\n\t\t\tal=SSL_AD_UNEXPECTED_MESSAGE;\n\t\t\tSSLerr(SSL_F_SSL3_GET_MESSAGE,SSL_R_UNEXPECTED_MESSAGE);\n\t\t\tgoto f_err;\n\t\t\t}\n\t\ts->s3->tmp.message_type= *(p++);\n\t\tn2l3(p,l);\n\t\tif (l > (unsigned long)max)\n\t\t\t{\n\t\t\tal=SSL_AD_ILLEGAL_PARAMETER;\n\t\t\tSSLerr(SSL_F_SSL3_GET_MESSAGE,SSL_R_EXCESSIVE_MESSAGE_SIZE);\n\t\t\tgoto f_err;\n\t\t\t}\n\t\tif (l && !BUF_MEM_grow(s->init_buf,(int)l))\n\t\t\t{\n\t\t\tSSLerr(SSL_F_SSL3_GET_MESSAGE,ERR_R_BUF_LIB);\n\t\t\tgoto err;\n\t\t\t}\n\t\ts->s3->tmp.message_size=l;\n\t\ts->state=stn;\n\t\ts->init_num=0;\n\t\t}\n\tp=(unsigned char *)s->init_buf->data;\n\tn=s->s3->tmp.message_size;\n\tif (n > 0)\n\t\t{\n\t\ti=ssl3_read_bytes(s,SSL3_RT_HANDSHAKE,&p[s->init_num],n);\n\t\tif (i != (int)n)\n\t\t\t{\n\t\t\t*ok=0;\n\t\t\treturn(ssl3_part_read(s,i));\n\t\t\t}\n\t\t}\n\t*ok=1;\n\treturn(n);\nf_err:\n\tssl3_send_alert(s,SSL3_AL_FATAL,al);\nerr:\n\t*ok=0;\n\treturn(-1);\n\t}', 'void ssl3_send_alert(SSL *s, int level, int desc)\n\t{\n\tdesc=s->method->ssl3_enc->alert_value(desc);\n\tif (desc < 0) return;\n\tif ((level == 2) && (s->session != NULL))\n\t\tSSL_CTX_remove_session(s->ctx,s->session);\n\ts->s3->alert_dispatch=1;\n\ts->s3->send_alert[0]=level;\n\ts->s3->send_alert[1]=desc;\n\tif (s->s3->wbuf.left == 0)\n\t\tssl3_dispatch_alert(s);\n\t}', 'int SSL_CTX_remove_session(SSL_CTX *ctx, SSL_SESSION *c)\n{\n\treturn remove_session_lock(ctx, c, 1);\n}', 'static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lck)\n\t{\n\tSSL_SESSION *r;\n\tint ret=0;\n\tif ((c != NULL) && (c->session_id_length != 0))\n\t\t{\n\t\tif(lck) CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);\n\t\tr=(SSL_SESSION *)lh_delete(ctx->sessions,(char *)c);\n\t\tif (r != NULL)\n\t\t\t{\n\t\t\tret=1;\n\t\t\tSSL_SESSION_list_remove(ctx,c);\n\t\t\t}\n\t\tif(lck) CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);\n\t\tif (ret)\n\t\t\t{\n\t\t\tr->not_resumable=1;\n\t\t\tif (ctx->remove_session_cb != NULL)\n\t\t\t\tctx->remove_session_cb(ctx,r);\n\t\t\tSSL_SESSION_free(r);\n\t\t\t}\n\t\t}\n\telse\n\t\tret=0;\n\treturn(ret);\n\t}', 'char *lh_delete(LHASH *lh, char *data)\n\t{\n\tunsigned long hash;\n\tLHASH_NODE *nn,**rn;\n\tchar *ret;\n\tlh->error=0;\n\trn=getrn(lh,data,&hash);\n\tif (*rn == NULL)\n\t\t{\n\t\tlh->num_no_delete++;\n\t\treturn(NULL);\n\t\t}\n\telse\n\t\t{\n\t\tnn= *rn;\n\t\t*rn=nn->next;\n\t\tret=nn->data;\n\t\tFree((char *)nn);\n\t\tlh->num_delete++;\n\t\t}\n\tlh->num_items--;\n\tif ((lh->num_nodes > MIN_NODES) &&\n\t\t(lh->down_load >= (lh->num_items*LH_LOAD_MULT/lh->num_nodes)))\n\t\tcontract(lh);\n\treturn(ret);\n\t}'] |
35,198 | 0 | https://github.com/openssl/openssl/blob/ddc6a5c8f5900959bdbdfee79e1625a3f7808acd/apps/speed.c/#L2969 | static int do_multi(int multi)
{
int n;
int fd[2];
int *fds;
static char sep[] = ":";
fds = malloc(sizeof(*fds) * multi);
for (n = 0; n < multi; ++n) {
if (pipe(fd) == -1) {
BIO_printf(bio_err, "pipe failure\n");
exit(1);
}
fflush(stdout);
(void)BIO_flush(bio_err);
if (fork()) {
close(fd[1]);
fds[n] = fd[0];
} else {
close(fd[0]);
close(1);
if (dup(fd[1]) == -1) {
BIO_printf(bio_err, "dup failed\n");
exit(1);
}
close(fd[1]);
mr = 1;
usertime = 0;
free(fds);
return 0;
}
printf("Forked child %d\n", n);
}
for (n = 0; n < multi; ++n) {
FILE *f;
char buf[1024];
char *p;
f = fdopen(fds[n], "r");
while (fgets(buf, sizeof buf, f)) {
p = strchr(buf, '\n');
if (p)
*p = '\0';
if (buf[0] != '+') {
BIO_printf(bio_err,
"Don't understand line '%s' from child %d\n", buf,
n);
continue;
}
printf("Got: %s from %d\n", buf, n);
if (strncmp(buf, "+F:", 3) == 0) {
int alg;
int j;
p = buf + 3;
alg = atoi(sstrsep(&p, sep));
sstrsep(&p, sep);
for (j = 0; j < SIZE_NUM; ++j)
results[alg][j] += atof(sstrsep(&p, sep));
} else if (strncmp(buf, "+F2:", 4) == 0) {
int k;
double d;
p = buf + 4;
k = atoi(sstrsep(&p, sep));
sstrsep(&p, sep);
d = atof(sstrsep(&p, sep));
rsa_results[k][0] += d;
d = atof(sstrsep(&p, sep));
rsa_results[k][1] += d;
}
# ifndef OPENSSL_NO_DSA
else if (strncmp(buf, "+F3:", 4) == 0) {
int k;
double d;
p = buf + 4;
k = atoi(sstrsep(&p, sep));
sstrsep(&p, sep);
d = atof(sstrsep(&p, sep));
dsa_results[k][0] += d;
d = atof(sstrsep(&p, sep));
dsa_results[k][1] += d;
}
# endif
# ifndef OPENSSL_NO_EC
else if (strncmp(buf, "+F4:", 4) == 0) {
int k;
double d;
p = buf + 4;
k = atoi(sstrsep(&p, sep));
sstrsep(&p, sep);
d = atof(sstrsep(&p, sep));
ecdsa_results[k][0] += d;
d = atof(sstrsep(&p, sep));
ecdsa_results[k][1] += d;
} else if (strncmp(buf, "+F5:", 4) == 0) {
int k;
double d;
p = buf + 4;
k = atoi(sstrsep(&p, sep));
sstrsep(&p, sep);
d = atof(sstrsep(&p, sep));
ecdh_results[k][0] += d;
}
# endif
else if (strncmp(buf, "+H:", 3) == 0) {
;
} else
BIO_printf(bio_err, "Unknown type '%s' from child %d\n", buf,
n);
}
fclose(f);
}
free(fds);
return 1;
} | ['static int do_multi(int multi)\n{\n int n;\n int fd[2];\n int *fds;\n static char sep[] = ":";\n fds = malloc(sizeof(*fds) * multi);\n for (n = 0; n < multi; ++n) {\n if (pipe(fd) == -1) {\n BIO_printf(bio_err, "pipe failure\\n");\n exit(1);\n }\n fflush(stdout);\n (void)BIO_flush(bio_err);\n if (fork()) {\n close(fd[1]);\n fds[n] = fd[0];\n } else {\n close(fd[0]);\n close(1);\n if (dup(fd[1]) == -1) {\n BIO_printf(bio_err, "dup failed\\n");\n exit(1);\n }\n close(fd[1]);\n mr = 1;\n usertime = 0;\n free(fds);\n return 0;\n }\n printf("Forked child %d\\n", n);\n }\n for (n = 0; n < multi; ++n) {\n FILE *f;\n char buf[1024];\n char *p;\n f = fdopen(fds[n], "r");\n while (fgets(buf, sizeof buf, f)) {\n p = strchr(buf, \'\\n\');\n if (p)\n *p = \'\\0\';\n if (buf[0] != \'+\') {\n BIO_printf(bio_err,\n "Don\'t understand line \'%s\' from child %d\\n", buf,\n n);\n continue;\n }\n printf("Got: %s from %d\\n", buf, n);\n if (strncmp(buf, "+F:", 3) == 0) {\n int alg;\n int j;\n p = buf + 3;\n alg = atoi(sstrsep(&p, sep));\n sstrsep(&p, sep);\n for (j = 0; j < SIZE_NUM; ++j)\n results[alg][j] += atof(sstrsep(&p, sep));\n } else if (strncmp(buf, "+F2:", 4) == 0) {\n int k;\n double d;\n p = buf + 4;\n k = atoi(sstrsep(&p, sep));\n sstrsep(&p, sep);\n d = atof(sstrsep(&p, sep));\n rsa_results[k][0] += d;\n d = atof(sstrsep(&p, sep));\n rsa_results[k][1] += d;\n }\n# ifndef OPENSSL_NO_DSA\n else if (strncmp(buf, "+F3:", 4) == 0) {\n int k;\n double d;\n p = buf + 4;\n k = atoi(sstrsep(&p, sep));\n sstrsep(&p, sep);\n d = atof(sstrsep(&p, sep));\n dsa_results[k][0] += d;\n d = atof(sstrsep(&p, sep));\n dsa_results[k][1] += d;\n }\n# endif\n# ifndef OPENSSL_NO_EC\n else if (strncmp(buf, "+F4:", 4) == 0) {\n int k;\n double d;\n p = buf + 4;\n k = atoi(sstrsep(&p, sep));\n sstrsep(&p, sep);\n d = atof(sstrsep(&p, sep));\n ecdsa_results[k][0] += d;\n d = atof(sstrsep(&p, sep));\n ecdsa_results[k][1] += d;\n } else if (strncmp(buf, "+F5:", 4) == 0) {\n int k;\n double d;\n p = buf + 4;\n k = atoi(sstrsep(&p, sep));\n sstrsep(&p, sep);\n d = atof(sstrsep(&p, sep));\n ecdh_results[k][0] += d;\n }\n# endif\n else if (strncmp(buf, "+H:", 3) == 0) {\n ;\n } else\n BIO_printf(bio_err, "Unknown type \'%s\' from child %d\\n", buf,\n n);\n }\n fclose(f);\n }\n free(fds);\n return 1;\n}', 'long BIO_ctrl(BIO *b, int cmd, long larg, void *parg)\n{\n long ret;\n if (b == NULL)\n return 0;\n if ((b->method == NULL) || (b->method->ctrl == NULL)) {\n BIOerr(BIO_F_BIO_CTRL, BIO_R_UNSUPPORTED_METHOD);\n return -2;\n }\n if (b->callback != NULL || b->callback_ex != NULL) {\n ret = bio_call_callback(b, BIO_CB_CTRL, parg, 0, cmd, larg, 1L, NULL);\n if (ret <= 0)\n return ret;\n }\n ret = b->method->ctrl(b, cmd, larg, parg);\n if (b->callback != NULL || b->callback_ex != NULL)\n ret = bio_call_callback(b, BIO_CB_CTRL | BIO_CB_RETURN, parg, 0, cmd,\n larg, ret, NULL);\n return ret;\n}'] |
35,199 | 0 | https://github.com/openssl/openssl/blob/d40a1b865fddc3d67f8c06ff1f1466fad331c8f7/crypto/bn/bn_lib.c/#L250 | int BN_num_bits(const BIGNUM *a)
{
int i = a->top - 1;
bn_check_top(a);
if (BN_is_zero(a)) return 0;
return ((i*BN_BITS2) + BN_num_bits_word(a->d[i]));
} | ['static unsigned int psk_server_cb(SSL *ssl, const char *identity,\n\tunsigned char *psk, unsigned int max_psk_len)\n\t{\n\tunsigned int psk_len = 0;\n\tint ret;\n\tBIGNUM *bn = NULL;\n\tif (s_debug)\n\t\tBIO_printf(bio_s_out,"psk_server_cb\\n");\n\tif (!identity)\n\t\t{\n\t\tBIO_printf(bio_err,"Error: client did not send PSK identity\\n");\n\t\tgoto out_err;\n\t\t}\n\tif (s_debug)\n\t\tBIO_printf(bio_s_out,"identity_len=%d identity=%s\\n",\n\t\t\tidentity ? (int)strlen(identity) : 0, identity);\n \tif (strcmp(identity, psk_identity) != 0)\n\t\t{\n BIO_printf(bio_s_out, "PSK error: client identity not found\\n");\n\t\tgoto out_err;\n }\n\tif (s_debug)\n\t\tBIO_printf(bio_s_out, "PSK client identity found\\n");\n\tret = BN_hex2bn(&bn, psk_key);\n\tif (!ret)\n\t\t{\n\t\tBIO_printf(bio_err,"Could not convert PSK key \'%s\' to BIGNUM\\n", psk_key);\n\t\tif (bn)\n\t\t\tBN_free(bn);\n\t\treturn 0;\n\t\t}\n\tif (BN_num_bytes(bn) > (int)max_psk_len)\n\t\t{\n\t\tBIO_printf(bio_err,"psk buffer of callback is too small (%d) for key (%d)\\n",\n\t\t\tmax_psk_len, BN_num_bytes(bn));\n\t\tBN_free(bn);\n\t\treturn 0;\n\t\t}\n\tret = BN_bn2bin(bn, psk);\n\tBN_free(bn);\n\tif (ret < 0)\n\t\tgoto out_err;\n\tpsk_len = (unsigned int)ret;\n\tif (s_debug)\n\t\tBIO_printf(bio_s_out, "fetched PSK len=%d\\n", psk_len);\n return psk_len;\n out_err:\n\tif (s_debug)\n\t\tBIO_printf(bio_err, "Error in PSK server callback\\n");\n\treturn 0;\n }', "int BN_hex2bn(BIGNUM **bn, const char *a)\n\t{\n\tBIGNUM *ret=NULL;\n\tBN_ULONG l=0;\n\tint neg=0,h,m,i,j,k,c;\n\tint num;\n\tif ((a == NULL) || (*a == '\\0')) return(0);\n\tif (*a == '-') { neg=1; a++; }\n\tfor (i=0; isxdigit((unsigned char) a[i]); i++)\n\t\t;\n\tnum=i+neg;\n\tif (bn == NULL) return(num);\n\tif (*bn == NULL)\n\t\t{\n\t\tif ((ret=BN_new()) == NULL) return(0);\n\t\t}\n\telse\n\t\t{\n\t\tret= *bn;\n\t\tBN_zero(ret);\n\t\t}\n\tif (bn_expand(ret,i*4) == NULL) goto err;\n\tj=i;\n\tm=0;\n\th=0;\n\twhile (j > 0)\n\t\t{\n\t\tm=((BN_BYTES*2) <= j)?(BN_BYTES*2):j;\n\t\tl=0;\n\t\tfor (;;)\n\t\t\t{\n\t\t\tc=a[j-m];\n\t\t\tif ((c >= '0') && (c <= '9')) k=c-'0';\n\t\t\telse if ((c >= 'a') && (c <= 'f')) k=c-'a'+10;\n\t\t\telse if ((c >= 'A') && (c <= 'F')) k=c-'A'+10;\n\t\t\telse k=0;\n\t\t\tl=(l<<4)|k;\n\t\t\tif (--m <= 0)\n\t\t\t\t{\n\t\t\t\tret->d[h++]=l;\n\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\tj-=(BN_BYTES*2);\n\t\t}\n\tret->top=h;\n\tbn_correct_top(ret);\n\tret->neg=neg;\n\t*bn=ret;\n\tbn_check_top(ret);\n\treturn(num);\nerr:\n\tif (*bn == NULL) BN_free(ret);\n\treturn(0);\n\t}", 'int BN_num_bits(const BIGNUM *a)\n\t{\n\tint i = a->top - 1;\n\tbn_check_top(a);\n\tif (BN_is_zero(a)) return 0;\n\treturn ((i*BN_BITS2) + BN_num_bits_word(a->d[i]));\n\t}'] |
35,200 | 0 | https://github.com/libav/libav/blob/2ee054c2153c32790851cc1095c50d3a89cf2dcc/libavcodec/dv.c/#L510 | static inline void dv_decode_video_segment(DVVideoContext *s,
const uint8_t *buf_ptr1,
const uint16_t *mb_pos_ptr)
{
int quant, dc, dct_mode, class1, j;
int mb_index, mb_x, mb_y, v, last_index;
int y_stride, linesize;
DCTELEM *block, *block1;
int c_offset;
uint8_t *y_ptr;
const uint8_t *buf_ptr;
PutBitContext pb, vs_pb;
GetBitContext gb;
BlockInfo mb_data[5 * DV_MAX_BPM], *mb, *mb1;
DECLARE_ALIGNED_16(DCTELEM, sblock[5*DV_MAX_BPM][64]);
DECLARE_ALIGNED_8(uint8_t, mb_bit_buffer[80 + 4]);
DECLARE_ALIGNED_8(uint8_t, vs_bit_buffer[5 * 80 + 4]);
const int log2_blocksize = 3-s->avctx->lowres;
int is_field_mode[5];
assert((((int)mb_bit_buffer) & 7) == 0);
assert((((int)vs_bit_buffer) & 7) == 0);
memset(sblock, 0, sizeof(sblock));
buf_ptr = buf_ptr1;
block1 = &sblock[0][0];
mb1 = mb_data;
init_put_bits(&vs_pb, vs_bit_buffer, 5 * 80);
for (mb_index = 0; mb_index < 5; mb_index++, mb1 += s->sys->bpm, block1 += s->sys->bpm * 64) {
quant = buf_ptr[3] & 0x0f;
buf_ptr += 4;
init_put_bits(&pb, mb_bit_buffer, 80);
mb = mb1;
block = block1;
is_field_mode[mb_index] = 0;
for (j = 0; j < s->sys->bpm; j++) {
last_index = s->sys->block_sizes[j];
init_get_bits(&gb, buf_ptr, last_index);
dc = get_sbits(&gb, 9);
dct_mode = get_bits1(&gb);
class1 = get_bits(&gb, 2);
if (DV_PROFILE_IS_HD(s->sys)) {
mb->idct_put = s->idct_put[0];
mb->scan_table = s->dv_zigzag[0];
mb->factor_table = s->dv100_idct_factor[((s->sys->height == 720) << 1) | (j >= 4)][class1][quant];
is_field_mode[mb_index] |= !j && dct_mode;
} else {
mb->idct_put = s->idct_put[dct_mode && log2_blocksize == 3];
mb->scan_table = s->dv_zigzag[dct_mode];
mb->factor_table = s->dv_idct_factor[class1 == 3][dct_mode]
[quant + dv_quant_offset[class1]];
}
dc = dc << 2;
dc += 1024;
block[0] = dc;
buf_ptr += last_index >> 3;
mb->pos = 0;
mb->partial_bit_count = 0;
#ifdef VLC_DEBUG
printf("MB block: %d, %d ", mb_index, j);
#endif
dv_decode_ac(&gb, mb, block);
if (mb->pos >= 64)
bit_copy(&pb, &gb);
block += 64;
mb++;
}
#ifdef VLC_DEBUG
printf("***pass 2 size=%d MB#=%d\n", put_bits_count(&pb), mb_index);
#endif
block = block1;
mb = mb1;
init_get_bits(&gb, mb_bit_buffer, put_bits_count(&pb));
flush_put_bits(&pb);
for (j = 0; j < s->sys->bpm; j++, block += 64, mb++) {
if (mb->pos < 64 && get_bits_left(&gb) > 0) {
dv_decode_ac(&gb, mb, block);
if (mb->pos < 64)
break;
}
}
if (j >= s->sys->bpm)
bit_copy(&vs_pb, &gb);
}
#ifdef VLC_DEBUG
printf("***pass 3 size=%d\n", put_bits_count(&vs_pb));
#endif
block = &sblock[0][0];
mb = mb_data;
init_get_bits(&gb, vs_bit_buffer, put_bits_count(&vs_pb));
flush_put_bits(&vs_pb);
for (mb_index = 0; mb_index < 5; mb_index++) {
for (j = 0; j < s->sys->bpm; j++) {
if (mb->pos < 64) {
#ifdef VLC_DEBUG
printf("start %d:%d\n", mb_index, j);
#endif
dv_decode_ac(&gb, mb, block);
}
if (mb->pos >= 64 && mb->pos < 127)
av_log(NULL, AV_LOG_ERROR, "AC EOB marker is absent pos=%d\n", mb->pos);
block += 64;
mb++;
}
}
block = &sblock[0][0];
mb = mb_data;
for (mb_index = 0; mb_index < 5; mb_index++) {
v = *mb_pos_ptr++;
mb_x = v & 0xff;
mb_y = v >> 8;
if (s->sys->height == 720 && !(s->buf[1] & 0x0C)) {
mb_y -= (mb_y > 17) ? 18 : -72;
}
if ((s->sys->pix_fmt == PIX_FMT_YUV420P) ||
(s->sys->pix_fmt == PIX_FMT_YUV411P && mb_x >= (704 / 8)) ||
(s->sys->height >= 720 && mb_y != 134)) {
y_stride = (s->picture.linesize[0] << ((!is_field_mode[mb_index]) * log2_blocksize));
} else {
y_stride = (2 << log2_blocksize);
}
y_ptr = s->picture.data[0] + ((mb_y * s->picture.linesize[0] + mb_x) << log2_blocksize);
linesize = s->picture.linesize[0] << is_field_mode[mb_index];
mb[0] .idct_put(y_ptr , linesize, block + 0*64);
if (s->sys->video_stype == 4) {
mb[2].idct_put(y_ptr + (1 << log2_blocksize) , linesize, block + 2*64);
} else {
mb[1].idct_put(y_ptr + (1 << log2_blocksize) , linesize, block + 1*64);
mb[2].idct_put(y_ptr + y_stride, linesize, block + 2*64);
mb[3].idct_put(y_ptr + (1 << log2_blocksize) + y_stride, linesize, block + 3*64);
}
mb += 4;
block += 4*64;
c_offset = (((mb_y >> (s->sys->pix_fmt == PIX_FMT_YUV420P)) * s->picture.linesize[1] +
(mb_x >> ((s->sys->pix_fmt == PIX_FMT_YUV411P) ? 2 : 1))) << log2_blocksize);
for (j = 2; j; j--) {
uint8_t *c_ptr = s->picture.data[j] + c_offset;
if (s->sys->pix_fmt == PIX_FMT_YUV411P && mb_x >= (704 / 8)) {
uint64_t aligned_pixels[64/8];
uint8_t *pixels = (uint8_t*)aligned_pixels;
uint8_t *c_ptr1, *ptr1;
int x, y;
mb->idct_put(pixels, 8, block);
for (y = 0; y < (1 << log2_blocksize); y++, c_ptr += s->picture.linesize[j], pixels += 8) {
ptr1 = pixels + (1 << (log2_blocksize - 1));
c_ptr1 = c_ptr + (s->picture.linesize[j] << log2_blocksize);
for (x = 0; x < (1 << (log2_blocksize - 1)); x++) {
c_ptr[x] = pixels[x];
c_ptr1[x] = ptr1[x];
}
}
block += 64; mb++;
} else {
y_stride = (mb_y == 134) ? (1 << log2_blocksize) :
s->picture.linesize[j] << ((!is_field_mode[mb_index]) * log2_blocksize);
linesize = s->picture.linesize[j] << is_field_mode[mb_index];
(mb++)-> idct_put(c_ptr , linesize, block); block += 64;
if (s->sys->bpm == 8) {
(mb++)->idct_put(c_ptr + y_stride, linesize, block); block += 64;
}
}
}
}
} | ['static inline void dv_decode_video_segment(DVVideoContext *s,\n const uint8_t *buf_ptr1,\n const uint16_t *mb_pos_ptr)\n{\n int quant, dc, dct_mode, class1, j;\n int mb_index, mb_x, mb_y, v, last_index;\n int y_stride, linesize;\n DCTELEM *block, *block1;\n int c_offset;\n uint8_t *y_ptr;\n const uint8_t *buf_ptr;\n PutBitContext pb, vs_pb;\n GetBitContext gb;\n BlockInfo mb_data[5 * DV_MAX_BPM], *mb, *mb1;\n DECLARE_ALIGNED_16(DCTELEM, sblock[5*DV_MAX_BPM][64]);\n DECLARE_ALIGNED_8(uint8_t, mb_bit_buffer[80 + 4]);\n DECLARE_ALIGNED_8(uint8_t, vs_bit_buffer[5 * 80 + 4]);\n const int log2_blocksize = 3-s->avctx->lowres;\n int is_field_mode[5];\n assert((((int)mb_bit_buffer) & 7) == 0);\n assert((((int)vs_bit_buffer) & 7) == 0);\n memset(sblock, 0, sizeof(sblock));\n buf_ptr = buf_ptr1;\n block1 = &sblock[0][0];\n mb1 = mb_data;\n init_put_bits(&vs_pb, vs_bit_buffer, 5 * 80);\n for (mb_index = 0; mb_index < 5; mb_index++, mb1 += s->sys->bpm, block1 += s->sys->bpm * 64) {\n quant = buf_ptr[3] & 0x0f;\n buf_ptr += 4;\n init_put_bits(&pb, mb_bit_buffer, 80);\n mb = mb1;\n block = block1;\n is_field_mode[mb_index] = 0;\n for (j = 0; j < s->sys->bpm; j++) {\n last_index = s->sys->block_sizes[j];\n init_get_bits(&gb, buf_ptr, last_index);\n dc = get_sbits(&gb, 9);\n dct_mode = get_bits1(&gb);\n class1 = get_bits(&gb, 2);\n if (DV_PROFILE_IS_HD(s->sys)) {\n mb->idct_put = s->idct_put[0];\n mb->scan_table = s->dv_zigzag[0];\n mb->factor_table = s->dv100_idct_factor[((s->sys->height == 720) << 1) | (j >= 4)][class1][quant];\n is_field_mode[mb_index] |= !j && dct_mode;\n } else {\n mb->idct_put = s->idct_put[dct_mode && log2_blocksize == 3];\n mb->scan_table = s->dv_zigzag[dct_mode];\n mb->factor_table = s->dv_idct_factor[class1 == 3][dct_mode]\n [quant + dv_quant_offset[class1]];\n }\n dc = dc << 2;\n dc += 1024;\n block[0] = dc;\n buf_ptr += last_index >> 3;\n mb->pos = 0;\n mb->partial_bit_count = 0;\n#ifdef VLC_DEBUG\n printf("MB block: %d, %d ", mb_index, j);\n#endif\n dv_decode_ac(&gb, mb, block);\n if (mb->pos >= 64)\n bit_copy(&pb, &gb);\n block += 64;\n mb++;\n }\n#ifdef VLC_DEBUG\n printf("***pass 2 size=%d MB#=%d\\n", put_bits_count(&pb), mb_index);\n#endif\n block = block1;\n mb = mb1;\n init_get_bits(&gb, mb_bit_buffer, put_bits_count(&pb));\n flush_put_bits(&pb);\n for (j = 0; j < s->sys->bpm; j++, block += 64, mb++) {\n if (mb->pos < 64 && get_bits_left(&gb) > 0) {\n dv_decode_ac(&gb, mb, block);\n if (mb->pos < 64)\n break;\n }\n }\n if (j >= s->sys->bpm)\n bit_copy(&vs_pb, &gb);\n }\n#ifdef VLC_DEBUG\n printf("***pass 3 size=%d\\n", put_bits_count(&vs_pb));\n#endif\n block = &sblock[0][0];\n mb = mb_data;\n init_get_bits(&gb, vs_bit_buffer, put_bits_count(&vs_pb));\n flush_put_bits(&vs_pb);\n for (mb_index = 0; mb_index < 5; mb_index++) {\n for (j = 0; j < s->sys->bpm; j++) {\n if (mb->pos < 64) {\n#ifdef VLC_DEBUG\n printf("start %d:%d\\n", mb_index, j);\n#endif\n dv_decode_ac(&gb, mb, block);\n }\n if (mb->pos >= 64 && mb->pos < 127)\n av_log(NULL, AV_LOG_ERROR, "AC EOB marker is absent pos=%d\\n", mb->pos);\n block += 64;\n mb++;\n }\n }\n block = &sblock[0][0];\n mb = mb_data;\n for (mb_index = 0; mb_index < 5; mb_index++) {\n v = *mb_pos_ptr++;\n mb_x = v & 0xff;\n mb_y = v >> 8;\n if (s->sys->height == 720 && !(s->buf[1] & 0x0C)) {\n mb_y -= (mb_y > 17) ? 18 : -72;\n }\n if ((s->sys->pix_fmt == PIX_FMT_YUV420P) ||\n (s->sys->pix_fmt == PIX_FMT_YUV411P && mb_x >= (704 / 8)) ||\n (s->sys->height >= 720 && mb_y != 134)) {\n y_stride = (s->picture.linesize[0] << ((!is_field_mode[mb_index]) * log2_blocksize));\n } else {\n y_stride = (2 << log2_blocksize);\n }\n y_ptr = s->picture.data[0] + ((mb_y * s->picture.linesize[0] + mb_x) << log2_blocksize);\n linesize = s->picture.linesize[0] << is_field_mode[mb_index];\n mb[0] .idct_put(y_ptr , linesize, block + 0*64);\n if (s->sys->video_stype == 4) {\n mb[2].idct_put(y_ptr + (1 << log2_blocksize) , linesize, block + 2*64);\n } else {\n mb[1].idct_put(y_ptr + (1 << log2_blocksize) , linesize, block + 1*64);\n mb[2].idct_put(y_ptr + y_stride, linesize, block + 2*64);\n mb[3].idct_put(y_ptr + (1 << log2_blocksize) + y_stride, linesize, block + 3*64);\n }\n mb += 4;\n block += 4*64;\n c_offset = (((mb_y >> (s->sys->pix_fmt == PIX_FMT_YUV420P)) * s->picture.linesize[1] +\n (mb_x >> ((s->sys->pix_fmt == PIX_FMT_YUV411P) ? 2 : 1))) << log2_blocksize);\n for (j = 2; j; j--) {\n uint8_t *c_ptr = s->picture.data[j] + c_offset;\n if (s->sys->pix_fmt == PIX_FMT_YUV411P && mb_x >= (704 / 8)) {\n uint64_t aligned_pixels[64/8];\n uint8_t *pixels = (uint8_t*)aligned_pixels;\n uint8_t *c_ptr1, *ptr1;\n int x, y;\n mb->idct_put(pixels, 8, block);\n for (y = 0; y < (1 << log2_blocksize); y++, c_ptr += s->picture.linesize[j], pixels += 8) {\n ptr1 = pixels + (1 << (log2_blocksize - 1));\n c_ptr1 = c_ptr + (s->picture.linesize[j] << log2_blocksize);\n for (x = 0; x < (1 << (log2_blocksize - 1)); x++) {\n c_ptr[x] = pixels[x];\n c_ptr1[x] = ptr1[x];\n }\n }\n block += 64; mb++;\n } else {\n y_stride = (mb_y == 134) ? (1 << log2_blocksize) :\n s->picture.linesize[j] << ((!is_field_mode[mb_index]) * log2_blocksize);\n linesize = s->picture.linesize[j] << is_field_mode[mb_index];\n (mb++)-> idct_put(c_ptr , linesize, block); block += 64;\n if (s->sys->bpm == 8) {\n (mb++)->idct_put(c_ptr + y_stride, linesize, block); block += 64;\n }\n }\n }\n }\n}'] |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.