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
201
0
https://github.com/openssl/openssl/blob/a5a95f8d65c2c616ebee13ae4b33eacde34bb2d3/crypto/x509/x509_trs.c/#L78
int X509_check_trust(X509 *x, int id, int flags) { X509_TRUST *pt; int idx; if (id == X509_TRUST_DEFAULT) return obj_trust(NID_anyExtendedKeyUsage, x, flags | X509_TRUST_DO_SS_COMPAT); idx = X509_TRUST_get_by_id(id); if (idx == -1) return default_trust(id, x, flags); pt = X509_TRUST_get0(idx); return pt->check_trust(pt, x, flags); }
['int X509_check_trust(X509 *x, int id, int flags)\n{\n X509_TRUST *pt;\n int idx;\n if (id == X509_TRUST_DEFAULT)\n return obj_trust(NID_anyExtendedKeyUsage, x,\n flags | X509_TRUST_DO_SS_COMPAT);\n idx = X509_TRUST_get_by_id(id);\n if (idx == -1)\n return default_trust(id, x, flags);\n pt = X509_TRUST_get0(idx);\n return pt->check_trust(pt, x, flags);\n}', 'int X509_TRUST_get_by_id(int id)\n{\n X509_TRUST tmp;\n int idx;\n if ((id >= X509_TRUST_MIN) && (id <= X509_TRUST_MAX))\n return id - X509_TRUST_MIN;\n tmp.trust = id;\n if (!trtable)\n return -1;\n idx = sk_X509_TRUST_find(trtable, &tmp);\n if (idx == -1)\n return -1;\n return idx + X509_TRUST_COUNT;\n}', 'int OPENSSL_sk_find(OPENSSL_STACK *st, const void *data)\n{\n return internal_find(st, data, OBJ_BSEARCH_FIRST_VALUE_ON_MATCH);\n}', 'static int internal_find(OPENSSL_STACK *st, const void *data,\n int ret_val_options)\n{\n const void *r;\n int i;\n if (st == NULL)\n return -1;\n if (st->comp == NULL) {\n for (i = 0; i < st->num; i++)\n if (st->data[i] == data)\n return (i);\n return (-1);\n }\n OPENSSL_sk_sort(st);\n if (data == NULL)\n return (-1);\n r = OBJ_bsearch_ex_(&data, st->data, st->num, sizeof(void *), st->comp,\n ret_val_options);\n if (r == NULL)\n return (-1);\n return (int)((const char **)r - st->data);\n}', 'X509_TRUST *X509_TRUST_get0(int idx)\n{\n if (idx < 0)\n return NULL;\n if (idx < (int)X509_TRUST_COUNT)\n return trstandard + idx;\n return sk_X509_TRUST_value(trtable, idx - X509_TRUST_COUNT);\n}']
202
0
https://github.com/libav/libav/blob/25b6837f7cacd691b19cbc12b9dad1ce84a318a1/libavcodec/vp8dsp.c/#L346
NORMAL_LIMIT(8)
['NORMAL_LIMIT(8)']
203
0
https://github.com/libav/libav/blob/cf6bae6883607f83f3b042b7b9d711197f736e2a/libavcodec/imgconvert.c/#L529
void avcodec_get_chroma_sub_sample(enum PixelFormat pix_fmt, int *h_shift, int *v_shift) { *h_shift = pix_fmt_info[pix_fmt].x_chroma_shift; *v_shift = pix_fmt_info[pix_fmt].y_chroma_shift; }
['static int h261_decode_frame(AVCodecContext *avctx,\n void *data, int *data_size,\n AVPacket *avpkt)\n{\n const uint8_t *buf = avpkt->data;\n int buf_size = avpkt->size;\n H261Context *h= avctx->priv_data;\n MpegEncContext *s = &h->s;\n int ret;\n AVFrame *pict = data;\n#ifdef DEBUG\n av_log(avctx, AV_LOG_DEBUG, "*****frame %d size=%d\\n", avctx->frame_number, buf_size);\n av_log(avctx, AV_LOG_DEBUG, "bytes=%x %x %x %x\\n", buf[0], buf[1], buf[2], buf[3]);\n#endif\n s->flags= avctx->flags;\n s->flags2= avctx->flags2;\n h->gob_start_code_skipped=0;\nretry:\n init_get_bits(&s->gb, buf, buf_size*8);\n if(!s->context_initialized){\n if (MPV_common_init(s) < 0)\n return -1;\n }\n if(s->current_picture_ptr==NULL || s->current_picture_ptr->data[0]){\n int i= ff_find_unused_picture(s, 0);\n s->current_picture_ptr= &s->picture[i];\n }\n ret = h261_decode_picture_header(h);\n if (ret < 0){\n av_log(s->avctx, AV_LOG_ERROR, "header damaged\\n");\n return -1;\n }\n if (s->width != avctx->coded_width || s->height != avctx->coded_height){\n ParseContext pc= s->parse_context;\n s->parse_context.buffer=0;\n MPV_common_end(s);\n s->parse_context= pc;\n }\n if (!s->context_initialized) {\n avcodec_set_dimensions(avctx, s->width, s->height);\n goto retry;\n }\n s->current_picture.pict_type= s->pict_type;\n s->current_picture.key_frame= s->pict_type == FF_I_TYPE;\n if(avctx->hurry_up>=5) return get_consumed_bytes(s, buf_size);\n if( (avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type==FF_B_TYPE)\n ||(avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type!=FF_I_TYPE)\n || avctx->skip_frame >= AVDISCARD_ALL)\n return get_consumed_bytes(s, buf_size);\n if(MPV_frame_start(s, avctx) < 0)\n return -1;\n ff_er_frame_start(s);\n s->mb_x=0;\n s->mb_y=0;\n while(h->gob_number < (s->mb_height==18 ? 12 : 5)){\n if(ff_h261_resync(h)<0)\n break;\n h261_decode_gob(h);\n }\n MPV_frame_end(s);\nassert(s->current_picture.pict_type == s->current_picture_ptr->pict_type);\nassert(s->current_picture.pict_type == s->pict_type);\n *pict= *(AVFrame*)s->current_picture_ptr;\n ff_print_debug_info(s, pict);\n *data_size = sizeof(AVFrame);\n return get_consumed_bytes(s, buf_size);\n}', 'static int h261_decode_picture_header(H261Context *h){\n MpegEncContext * const s = &h->s;\n int format, i;\n uint32_t startcode= 0;\n for(i= s->gb.size_in_bits - get_bits_count(&s->gb); i>24; i-=1){\n startcode = ((startcode << 1) | get_bits(&s->gb, 1)) & 0x000FFFFF;\n if(startcode == 0x10)\n break;\n }\n if (startcode != 0x10){\n av_log(s->avctx, AV_LOG_ERROR, "Bad picture start code\\n");\n return -1;\n }\n i= get_bits(&s->gb, 5);\n if(i < (s->picture_number&31))\n i += 32;\n s->picture_number = (s->picture_number&~31) + i;\n s->avctx->time_base= (AVRational){1001, 30000};\n s->current_picture.pts= s->picture_number;\n skip_bits1(&s->gb);\n skip_bits1(&s->gb);\n skip_bits1(&s->gb);\n format = get_bits1(&s->gb);\n if (format == 0){\n s->width = 176;\n s->height = 144;\n s->mb_width = 11;\n s->mb_height = 9;\n }else{\n s->width = 352;\n s->height = 288;\n s->mb_width = 22;\n s->mb_height = 18;\n }\n s->mb_num = s->mb_width * s->mb_height;\n skip_bits1(&s->gb);\n skip_bits1(&s->gb);\n while (get_bits1(&s->gb) != 0){\n skip_bits(&s->gb, 8);\n }\n s->pict_type = FF_P_TYPE;\n h->gob_number = 0;\n return 0;\n}', 'int MPV_common_init(MpegEncContext *s)\n{\n int y_size, c_size, yc_size, i, mb_array_size, mv_table_size, x, y, threads;\n if(s->codec_id == CODEC_ID_MPEG2VIDEO && !s->progressive_sequence)\n s->mb_height = (s->height + 31) / 32 * 2;\n else\n s->mb_height = (s->height + 15) / 16;\n if(s->avctx->pix_fmt == PIX_FMT_NONE){\n av_log(s->avctx, AV_LOG_ERROR, "decoding to PIX_FMT_NONE is not supported.\\n");\n return -1;\n }\n if(s->avctx->thread_count > MAX_THREADS || (s->avctx->thread_count > s->mb_height && s->mb_height)){\n av_log(s->avctx, AV_LOG_ERROR, "too many threads\\n");\n return -1;\n }\n if((s->width || s->height) && avcodec_check_dimensions(s->avctx, s->width, s->height))\n return -1;\n dsputil_init(&s->dsp, s->avctx);\n ff_dct_common_init(s);\n s->flags= s->avctx->flags;\n s->flags2= s->avctx->flags2;\n s->mb_width = (s->width + 15) / 16;\n s->mb_stride = s->mb_width + 1;\n s->b8_stride = s->mb_width*2 + 1;\n s->b4_stride = s->mb_width*4 + 1;\n mb_array_size= s->mb_height * s->mb_stride;\n mv_table_size= (s->mb_height+2) * s->mb_stride + 1;\n avcodec_get_chroma_sub_sample(s->avctx->pix_fmt,&(s->chroma_x_shift),\n &(s->chroma_y_shift) );\n s->h_edge_pos= s->mb_width*16;\n s->v_edge_pos= s->mb_height*16;\n s->mb_num = s->mb_width * s->mb_height;\n s->block_wrap[0]=\n s->block_wrap[1]=\n s->block_wrap[2]=\n s->block_wrap[3]= s->b8_stride;\n s->block_wrap[4]=\n s->block_wrap[5]= s->mb_stride;\n y_size = s->b8_stride * (2 * s->mb_height + 1);\n c_size = s->mb_stride * (s->mb_height + 1);\n yc_size = y_size + 2 * c_size;\n s->codec_tag= toupper( s->avctx->codec_tag &0xFF)\n + (toupper((s->avctx->codec_tag>>8 )&0xFF)<<8 )\n + (toupper((s->avctx->codec_tag>>16)&0xFF)<<16)\n + (toupper((s->avctx->codec_tag>>24)&0xFF)<<24);\n s->stream_codec_tag= toupper( s->avctx->stream_codec_tag &0xFF)\n + (toupper((s->avctx->stream_codec_tag>>8 )&0xFF)<<8 )\n + (toupper((s->avctx->stream_codec_tag>>16)&0xFF)<<16)\n + (toupper((s->avctx->stream_codec_tag>>24)&0xFF)<<24);\n s->avctx->coded_frame= (AVFrame*)&s->current_picture;\n CHECKED_ALLOCZ(s->mb_index2xy, (s->mb_num+1)*sizeof(int))\n for(y=0; y<s->mb_height; y++){\n for(x=0; x<s->mb_width; x++){\n s->mb_index2xy[ x + y*s->mb_width ] = x + y*s->mb_stride;\n }\n }\n s->mb_index2xy[ s->mb_height*s->mb_width ] = (s->mb_height-1)*s->mb_stride + s->mb_width;\n if (s->encoding) {\n CHECKED_ALLOCZ(s->p_mv_table_base , mv_table_size * 2 * sizeof(int16_t))\n CHECKED_ALLOCZ(s->b_forw_mv_table_base , mv_table_size * 2 * sizeof(int16_t))\n CHECKED_ALLOCZ(s->b_back_mv_table_base , mv_table_size * 2 * sizeof(int16_t))\n CHECKED_ALLOCZ(s->b_bidir_forw_mv_table_base , mv_table_size * 2 * sizeof(int16_t))\n CHECKED_ALLOCZ(s->b_bidir_back_mv_table_base , mv_table_size * 2 * sizeof(int16_t))\n CHECKED_ALLOCZ(s->b_direct_mv_table_base , mv_table_size * 2 * sizeof(int16_t))\n s->p_mv_table = s->p_mv_table_base + s->mb_stride + 1;\n s->b_forw_mv_table = s->b_forw_mv_table_base + s->mb_stride + 1;\n s->b_back_mv_table = s->b_back_mv_table_base + s->mb_stride + 1;\n s->b_bidir_forw_mv_table= s->b_bidir_forw_mv_table_base + s->mb_stride + 1;\n s->b_bidir_back_mv_table= s->b_bidir_back_mv_table_base + s->mb_stride + 1;\n s->b_direct_mv_table = s->b_direct_mv_table_base + s->mb_stride + 1;\n if(s->msmpeg4_version){\n CHECKED_ALLOCZ(s->ac_stats, 2*2*(MAX_LEVEL+1)*(MAX_RUN+1)*2*sizeof(int));\n }\n CHECKED_ALLOCZ(s->avctx->stats_out, 256);\n CHECKED_ALLOCZ(s->mb_type , mb_array_size * sizeof(uint16_t))\n CHECKED_ALLOCZ(s->lambda_table, mb_array_size * sizeof(int))\n CHECKED_ALLOCZ(s->q_intra_matrix, 64*32 * sizeof(int))\n CHECKED_ALLOCZ(s->q_inter_matrix, 64*32 * sizeof(int))\n CHECKED_ALLOCZ(s->q_intra_matrix16, 64*32*2 * sizeof(uint16_t))\n CHECKED_ALLOCZ(s->q_inter_matrix16, 64*32*2 * sizeof(uint16_t))\n CHECKED_ALLOCZ(s->input_picture, MAX_PICTURE_COUNT * sizeof(Picture*))\n CHECKED_ALLOCZ(s->reordered_input_picture, MAX_PICTURE_COUNT * sizeof(Picture*))\n if(s->avctx->noise_reduction){\n CHECKED_ALLOCZ(s->dct_offset, 2 * 64 * sizeof(uint16_t))\n }\n }\n CHECKED_ALLOCZ(s->picture, MAX_PICTURE_COUNT * sizeof(Picture))\n CHECKED_ALLOCZ(s->error_status_table, mb_array_size*sizeof(uint8_t))\n if(s->codec_id==CODEC_ID_MPEG4 || (s->flags & CODEC_FLAG_INTERLACED_ME)){\n for(i=0; i<2; i++){\n int j, k;\n for(j=0; j<2; j++){\n for(k=0; k<2; k++){\n CHECKED_ALLOCZ(s->b_field_mv_table_base[i][j][k] , mv_table_size * 2 * sizeof(int16_t))\n s->b_field_mv_table[i][j][k] = s->b_field_mv_table_base[i][j][k] + s->mb_stride + 1;\n }\n CHECKED_ALLOCZ(s->b_field_select_table[i][j] , mb_array_size * 2 * sizeof(uint8_t))\n CHECKED_ALLOCZ(s->p_field_mv_table_base[i][j] , mv_table_size * 2 * sizeof(int16_t))\n s->p_field_mv_table[i][j] = s->p_field_mv_table_base[i][j] + s->mb_stride + 1;\n }\n CHECKED_ALLOCZ(s->p_field_select_table[i] , mb_array_size * 2 * sizeof(uint8_t))\n }\n }\n if (s->out_format == FMT_H263) {\n CHECKED_ALLOCZ(s->ac_val_base, yc_size * sizeof(int16_t) * 16);\n s->ac_val[0] = s->ac_val_base + s->b8_stride + 1;\n s->ac_val[1] = s->ac_val_base + y_size + s->mb_stride + 1;\n s->ac_val[2] = s->ac_val[1] + c_size;\n CHECKED_ALLOCZ(s->coded_block_base, y_size);\n s->coded_block= s->coded_block_base + s->b8_stride + 1;\n CHECKED_ALLOCZ(s->cbp_table , mb_array_size * sizeof(uint8_t))\n CHECKED_ALLOCZ(s->pred_dir_table, mb_array_size * sizeof(uint8_t))\n }\n if (s->h263_pred || s->h263_plus || !s->encoding) {\n CHECKED_ALLOCZ(s->dc_val_base, yc_size * sizeof(int16_t));\n s->dc_val[0] = s->dc_val_base + s->b8_stride + 1;\n s->dc_val[1] = s->dc_val_base + y_size + s->mb_stride + 1;\n s->dc_val[2] = s->dc_val[1] + c_size;\n for(i=0;i<yc_size;i++)\n s->dc_val_base[i] = 1024;\n }\n CHECKED_ALLOCZ(s->mbintra_table, mb_array_size);\n memset(s->mbintra_table, 1, mb_array_size);\n CHECKED_ALLOCZ(s->mbskip_table, mb_array_size+2);\n CHECKED_ALLOCZ(s->prev_pict_types, PREV_PICT_TYPES_BUFFER_SIZE);\n s->parse_context.state= -1;\n if((s->avctx->debug&(FF_DEBUG_VIS_QP|FF_DEBUG_VIS_MB_TYPE)) || (s->avctx->debug_mv)){\n s->visualization_buffer[0] = av_malloc((s->mb_width*16 + 2*EDGE_WIDTH) * s->mb_height*16 + 2*EDGE_WIDTH);\n s->visualization_buffer[1] = av_malloc((s->mb_width*16 + 2*EDGE_WIDTH) * s->mb_height*16 + 2*EDGE_WIDTH);\n s->visualization_buffer[2] = av_malloc((s->mb_width*16 + 2*EDGE_WIDTH) * s->mb_height*16 + 2*EDGE_WIDTH);\n }\n s->context_initialized = 1;\n s->thread_context[0]= s;\n threads = s->avctx->thread_count;\n for(i=1; i<threads; i++){\n s->thread_context[i]= av_malloc(sizeof(MpegEncContext));\n memcpy(s->thread_context[i], s, sizeof(MpegEncContext));\n }\n for(i=0; i<threads; i++){\n if(init_duplicate_context(s->thread_context[i], s) < 0)\n goto fail;\n s->thread_context[i]->start_mb_y= (s->mb_height*(i ) + s->avctx->thread_count/2) / s->avctx->thread_count;\n s->thread_context[i]->end_mb_y = (s->mb_height*(i+1) + s->avctx->thread_count/2) / s->avctx->thread_count;\n }\n return 0;\n fail:\n MPV_common_end(s);\n return -1;\n}', 'void avcodec_get_chroma_sub_sample(enum PixelFormat pix_fmt, int *h_shift, int *v_shift)\n{\n *h_shift = pix_fmt_info[pix_fmt].x_chroma_shift;\n *v_shift = pix_fmt_info[pix_fmt].y_chroma_shift;\n}']
204
0
https://github.com/libav/libav/blob/df84d7d9bdf6b8e6896c711880f130b72738c828/libavcodec/wmaprodec.c/#L817
static int decode_coeffs(WMAProDecodeCtx *s, int c) { static const int fval_tab[16] = { 0x00000000, 0x3f800000, 0x40000000, 0x40400000, 0x40800000, 0x40a00000, 0x40c00000, 0x40e00000, 0x41000000, 0x41100000, 0x41200000, 0x41300000, 0x41400000, 0x41500000, 0x41600000, 0x41700000, }; int vlctable; VLC* vlc; WMAProChannelCtx* ci = &s->channel[c]; int rl_mode = 0; int cur_coeff = 0; int num_zeros = 0; const uint16_t* run; const float* level; dprintf(s->avctx, "decode coefficients for channel %i\n", c); vlctable = get_bits1(&s->gb); vlc = &coef_vlc[vlctable]; if (vlctable) { run = coef1_run; level = coef1_level; } else { run = coef0_run; level = coef0_level; } while (!rl_mode && cur_coeff + 3 < s->subframe_len) { int vals[4]; int i; unsigned int idx; idx = get_vlc2(&s->gb, vec4_vlc.table, VLCBITS, VEC4MAXDEPTH); if (idx == HUFF_VEC4_SIZE - 1) { for (i = 0; i < 4; i += 2) { idx = get_vlc2(&s->gb, vec2_vlc.table, VLCBITS, VEC2MAXDEPTH); if (idx == HUFF_VEC2_SIZE - 1) { int v0, v1; v0 = get_vlc2(&s->gb, vec1_vlc.table, VLCBITS, VEC1MAXDEPTH); if (v0 == HUFF_VEC1_SIZE - 1) v0 += ff_wma_get_large_val(&s->gb); v1 = get_vlc2(&s->gb, vec1_vlc.table, VLCBITS, VEC1MAXDEPTH); if (v1 == HUFF_VEC1_SIZE - 1) v1 += ff_wma_get_large_val(&s->gb); ((float*)vals)[i ] = v0; ((float*)vals)[i+1] = v1; } else { vals[i] = fval_tab[symbol_to_vec2[idx] >> 4 ]; vals[i+1] = fval_tab[symbol_to_vec2[idx] & 0xF]; } } } else { vals[0] = fval_tab[ symbol_to_vec4[idx] >> 12 ]; vals[1] = fval_tab[(symbol_to_vec4[idx] >> 8) & 0xF]; vals[2] = fval_tab[(symbol_to_vec4[idx] >> 4) & 0xF]; vals[3] = fval_tab[ symbol_to_vec4[idx] & 0xF]; } for (i = 0; i < 4; i++) { if (vals[i]) { int sign = get_bits1(&s->gb) - 1; *(uint32_t*)&ci->coeffs[cur_coeff] = vals[i] ^ sign<<31; num_zeros = 0; } else { ci->coeffs[cur_coeff] = 0; rl_mode |= (++num_zeros > s->subframe_len >> 8); } ++cur_coeff; } } if (rl_mode) { memset(&ci->coeffs[cur_coeff], 0, sizeof(*ci->coeffs) * (s->subframe_len - cur_coeff)); if (ff_wma_run_level_decode(s->avctx, &s->gb, vlc, level, run, 1, ci->coeffs, cur_coeff, s->subframe_len, s->subframe_len, s->esc_len, 0)) return AVERROR_INVALIDDATA; } return 0; }
['static int decode_coeffs(WMAProDecodeCtx *s, int c)\n{\n static const int fval_tab[16] = {\n 0x00000000, 0x3f800000, 0x40000000, 0x40400000,\n 0x40800000, 0x40a00000, 0x40c00000, 0x40e00000,\n 0x41000000, 0x41100000, 0x41200000, 0x41300000,\n 0x41400000, 0x41500000, 0x41600000, 0x41700000,\n };\n int vlctable;\n VLC* vlc;\n WMAProChannelCtx* ci = &s->channel[c];\n int rl_mode = 0;\n int cur_coeff = 0;\n int num_zeros = 0;\n const uint16_t* run;\n const float* level;\n dprintf(s->avctx, "decode coefficients for channel %i\\n", c);\n vlctable = get_bits1(&s->gb);\n vlc = &coef_vlc[vlctable];\n if (vlctable) {\n run = coef1_run;\n level = coef1_level;\n } else {\n run = coef0_run;\n level = coef0_level;\n }\n while (!rl_mode && cur_coeff + 3 < s->subframe_len) {\n int vals[4];\n int i;\n unsigned int idx;\n idx = get_vlc2(&s->gb, vec4_vlc.table, VLCBITS, VEC4MAXDEPTH);\n if (idx == HUFF_VEC4_SIZE - 1) {\n for (i = 0; i < 4; i += 2) {\n idx = get_vlc2(&s->gb, vec2_vlc.table, VLCBITS, VEC2MAXDEPTH);\n if (idx == HUFF_VEC2_SIZE - 1) {\n int v0, v1;\n v0 = get_vlc2(&s->gb, vec1_vlc.table, VLCBITS, VEC1MAXDEPTH);\n if (v0 == HUFF_VEC1_SIZE - 1)\n v0 += ff_wma_get_large_val(&s->gb);\n v1 = get_vlc2(&s->gb, vec1_vlc.table, VLCBITS, VEC1MAXDEPTH);\n if (v1 == HUFF_VEC1_SIZE - 1)\n v1 += ff_wma_get_large_val(&s->gb);\n ((float*)vals)[i ] = v0;\n ((float*)vals)[i+1] = v1;\n } else {\n vals[i] = fval_tab[symbol_to_vec2[idx] >> 4 ];\n vals[i+1] = fval_tab[symbol_to_vec2[idx] & 0xF];\n }\n }\n } else {\n vals[0] = fval_tab[ symbol_to_vec4[idx] >> 12 ];\n vals[1] = fval_tab[(symbol_to_vec4[idx] >> 8) & 0xF];\n vals[2] = fval_tab[(symbol_to_vec4[idx] >> 4) & 0xF];\n vals[3] = fval_tab[ symbol_to_vec4[idx] & 0xF];\n }\n for (i = 0; i < 4; i++) {\n if (vals[i]) {\n int sign = get_bits1(&s->gb) - 1;\n *(uint32_t*)&ci->coeffs[cur_coeff] = vals[i] ^ sign<<31;\n num_zeros = 0;\n } else {\n ci->coeffs[cur_coeff] = 0;\n rl_mode |= (++num_zeros > s->subframe_len >> 8);\n }\n ++cur_coeff;\n }\n }\n if (rl_mode) {\n memset(&ci->coeffs[cur_coeff], 0,\n sizeof(*ci->coeffs) * (s->subframe_len - cur_coeff));\n if (ff_wma_run_level_decode(s->avctx, &s->gb, vlc,\n level, run, 1, ci->coeffs,\n cur_coeff, s->subframe_len,\n s->subframe_len, s->esc_len, 0))\n return AVERROR_INVALIDDATA;\n }\n return 0;\n}']
205
0
https://github.com/openssl/openssl/blob/aef838fc957afefc4d49a360f1ab77b14595f27c/apps/ca.c/#L1508
static int do_body(X509 **xret, EVP_PKEY *pkey, X509 *x509, const EVP_MD *dgst, STACK_OF(CONF_VALUE) *policy, TXT_DB *db, BIGNUM *serial, char *startdate, char *enddate, int days, int batch, int verbose, X509_REQ *req, char *ext_sect, LHASH *lconf) { X509_NAME *name=NULL,*CAname=NULL,*subject=NULL; ASN1_UTCTIME *tm,*tmptm; ASN1_STRING *str,*str2; ASN1_OBJECT *obj; X509 *ret=NULL; X509_CINF *ci; X509_NAME_ENTRY *ne; X509_NAME_ENTRY *tne,*push; EVP_PKEY *pktmp; int ok= -1,i,j,last,nid; char *p; CONF_VALUE *cv; char *row[DB_NUMBER],**rrow,**irow=NULL; char buf[25],*pbuf; tmptm=ASN1_UTCTIME_new(); if (tmptm == NULL) { BIO_printf(bio_err,"malloc error\n"); return(0); } for (i=0; i<DB_NUMBER; i++) row[i]=NULL; BIO_printf(bio_err,"The Subjects Distinguished Name is as follows\n"); name=X509_REQ_get_subject_name(req); for (i=0; i<X509_NAME_entry_count(name); i++) { ne=(X509_NAME_ENTRY *)X509_NAME_get_entry(name,i); obj=X509_NAME_ENTRY_get_object(ne); j=i2a_ASN1_OBJECT(bio_err,obj); str=X509_NAME_ENTRY_get_data(ne); pbuf=buf; for (j=22-j; j>0; j--) *(pbuf++)=' '; *(pbuf++)=':'; *(pbuf++)='\0'; BIO_puts(bio_err,buf); if (msie_hack) { nid=OBJ_obj2nid(ne->object); if (str->type == V_ASN1_UNIVERSALSTRING) ASN1_UNIVERSALSTRING_to_string(str); if ((str->type == V_ASN1_IA5STRING) && (nid != NID_pkcs9_emailAddress)) str->type=V_ASN1_T61STRING; if ((nid == NID_pkcs9_emailAddress) && (str->type == V_ASN1_PRINTABLESTRING)) str->type=V_ASN1_IA5STRING; } if (str->type == V_ASN1_PRINTABLESTRING) BIO_printf(bio_err,"PRINTABLE:'"); else if (str->type == V_ASN1_T61STRING) BIO_printf(bio_err,"T61STRING:'"); else if (str->type == V_ASN1_IA5STRING) BIO_printf(bio_err,"IA5STRING:'"); else if (str->type == V_ASN1_UNIVERSALSTRING) BIO_printf(bio_err,"UNIVERSALSTRING:'"); else BIO_printf(bio_err,"ASN.1 %2d:'",str->type); if ((OBJ_obj2nid(obj) == NID_pkcs9_emailAddress) && (str->type != V_ASN1_IA5STRING)) { BIO_printf(bio_err,"\nemailAddress type needs to be of type IA5STRING\n"); goto err; } j=ASN1_PRINTABLE_type(str->data,str->length); if ( ((j == V_ASN1_T61STRING) && (str->type != V_ASN1_T61STRING)) || ((j == V_ASN1_IA5STRING) && (str->type == V_ASN1_PRINTABLESTRING))) { BIO_printf(bio_err,"\nThe string contains characters that are illegal for the ASN.1 type\n"); goto err; } p=(char *)str->data; for (j=str->length; j>0; j--) { if ((*p >= ' ') && (*p <= '~')) BIO_printf(bio_err,"%c",*p); else if (*p & 0x80) BIO_printf(bio_err,"\\0x%02X",*p); else if ((unsigned char)*p == 0xf7) BIO_printf(bio_err,"^?"); else BIO_printf(bio_err,"^%c",*p+'@'); p++; } BIO_printf(bio_err,"'\n"); } if ((subject=X509_NAME_new()) == NULL) { BIO_printf(bio_err,"Malloc failure\n"); goto err; } CAname=X509_NAME_dup(x509->cert_info->subject); if (CAname == NULL) goto err; str=str2=NULL; for (i=0; i<sk_CONF_VALUE_num(policy); i++) { cv=sk_CONF_VALUE_value(policy,i); if ((j=OBJ_txt2nid(cv->name)) == NID_undef) { BIO_printf(bio_err,"%s:unknown object type in 'policy' configuration\n",cv->name); goto err; } obj=OBJ_nid2obj(j); last= -1; for (;;) { j=X509_NAME_get_index_by_OBJ(name,obj,last); if (j < 0) { if (last != -1) break; tne=NULL; } else { tne=X509_NAME_get_entry(name,j); } last=j; push=NULL; if (strcmp(cv->value,"optional") == 0) { if (tne != NULL) push=tne; } else if (strcmp(cv->value,"supplied") == 0) { if (tne == NULL) { BIO_printf(bio_err,"The %s field needed to be supplied and was missing\n",cv->name); goto err; } else push=tne; } else if (strcmp(cv->value,"match") == 0) { int last2; if (tne == NULL) { BIO_printf(bio_err,"The mandatory %s field was missing\n",cv->name); goto err; } last2= -1; again2: j=X509_NAME_get_index_by_OBJ(CAname,obj,last2); if ((j < 0) && (last2 == -1)) { BIO_printf(bio_err,"The %s field does not exist in the CA certificate,\nthe 'policy' is misconfigured\n",cv->name); goto err; } if (j >= 0) { push=X509_NAME_get_entry(CAname,j); str=X509_NAME_ENTRY_get_data(tne); str2=X509_NAME_ENTRY_get_data(push); last2=j; if (ASN1_STRING_cmp(str,str2) != 0) goto again2; } if (j < 0) { BIO_printf(bio_err,"The %s field needed to be the same in the\nCA certificate (%s) and the request (%s)\n",cv->name,((str == NULL)?"NULL":(char *)str->data),((str2 == NULL)?"NULL":(char *)str2->data)); goto err; } } else { BIO_printf(bio_err,"%s:invalid type in 'policy' configuration\n",cv->value); goto err; } if (push != NULL) { if (!X509_NAME_add_entry(subject,push, X509_NAME_entry_count(subject),0)) { if (push != NULL) X509_NAME_ENTRY_free(push); BIO_printf(bio_err,"Malloc failure\n"); goto err; } } if (j < 0) break; } } if (preserve) { X509_NAME_free(subject); subject=X509_NAME_dup(X509_REQ_get_subject_name(req)); if (subject == NULL) goto err; } if (verbose) BIO_printf(bio_err,"The subject name apears to be ok, checking data base for clashes\n"); row[DB_name]=X509_NAME_oneline(subject,NULL,0); row[DB_serial]=BN_bn2hex(serial); if ((row[DB_name] == NULL) || (row[DB_serial] == NULL)) { BIO_printf(bio_err,"Malloc failure\n"); goto err; } rrow=TXT_DB_get_by_index(db,DB_name,row); if (rrow != NULL) { BIO_printf(bio_err,"ERROR:There is already a certificate for %s\n", row[DB_name]); } else { rrow=TXT_DB_get_by_index(db,DB_serial,row); if (rrow != NULL) { BIO_printf(bio_err,"ERROR:Serial number %s has already been issued,\n", row[DB_serial]); BIO_printf(bio_err," check the database/serial_file for corruption\n"); } } if (rrow != NULL) { BIO_printf(bio_err, "The matching entry has the following details\n"); if (rrow[DB_type][0] == 'E') p="Expired"; else if (rrow[DB_type][0] == 'R') p="Revoked"; else if (rrow[DB_type][0] == 'V') p="Valid"; else p="\ninvalid type, Data base error\n"; BIO_printf(bio_err,"Type :%s\n",p);; if (rrow[DB_type][0] == 'R') { p=rrow[DB_exp_date]; if (p == NULL) p="undef"; BIO_printf(bio_err,"Was revoked on:%s\n",p); } p=rrow[DB_exp_date]; if (p == NULL) p="undef"; BIO_printf(bio_err,"Expires on :%s\n",p); p=rrow[DB_serial]; if (p == NULL) p="undef"; BIO_printf(bio_err,"Serial Number :%s\n",p); p=rrow[DB_file]; if (p == NULL) p="undef"; BIO_printf(bio_err,"File name :%s\n",p); p=rrow[DB_name]; if (p == NULL) p="undef"; BIO_printf(bio_err,"Subject Name :%s\n",p); ok= -1; goto err; } if (verbose) BIO_printf(bio_err,"Everything appears to be ok, creating and signing the certificate\n"); if ((ret=X509_new()) == NULL) goto err; ci=ret->cert_info; #ifdef X509_V3 if (!X509_set_version(x509,2)) goto err; #endif if (BN_to_ASN1_INTEGER(serial,ci->serialNumber) == NULL) goto err; if (!X509_set_issuer_name(ret,X509_get_subject_name(x509))) goto err; BIO_printf(bio_err,"Certificate is to be certified until "); if (strcmp(startdate,"today") == 0) X509_gmtime_adj(X509_get_notBefore(ret),0); else ASN1_UTCTIME_set_string(X509_get_notBefore(ret),startdate); if (enddate == NULL) X509_gmtime_adj(X509_get_notAfter(ret),(long)60*60*24*days); else ASN1_UTCTIME_set_string(X509_get_notAfter(ret),enddate); ASN1_UTCTIME_print(bio_err,X509_get_notAfter(ret)); if(days) BIO_printf(bio_err," (%d days)",days); BIO_printf(bio_err, "\n"); if (!X509_set_subject_name(ret,subject)) goto err; pktmp=X509_REQ_get_pubkey(req); i = X509_set_pubkey(ret,pktmp); EVP_PKEY_free(pktmp); if (!i) goto err; if (ext_sect) { X509V3_CTX ctx; if (ci->version == NULL) if ((ci->version=ASN1_INTEGER_new()) == NULL) goto err; ASN1_INTEGER_set(ci->version,2); if (ci->extensions != NULL) sk_X509_EXTENSION_pop_free(ci->extensions, X509_EXTENSION_free); ci->extensions = NULL; X509V3_set_ctx(&ctx, x509, ret, req, NULL, 0); X509V3_set_conf_lhash(&ctx, lconf); if(!X509V3_EXT_add_conf(lconf, &ctx, ext_sect, ret)) goto err; } if (!batch) { BIO_printf(bio_err,"Sign the certificate? [y/n]:"); (void)BIO_flush(bio_err); buf[0]='\0'; fgets(buf,sizeof(buf)-1,stdin); if (!((buf[0] == 'y') || (buf[0] == 'Y'))) { BIO_printf(bio_err,"CERTIFICATE WILL NOT BE CERTIFIED\n"); ok=0; goto err; } } #ifndef NO_DSA if (pkey->type == EVP_PKEY_DSA) dgst=EVP_dss1(); pktmp=X509_get_pubkey(ret); if (EVP_PKEY_missing_parameters(pktmp) && !EVP_PKEY_missing_parameters(pkey)) EVP_PKEY_copy_parameters(pktmp,pkey); EVP_PKEY_free(pktmp); #endif if (!X509_sign(ret,pkey,dgst)) goto err; row[DB_type]=(char *)Malloc(2); tm=X509_get_notAfter(ret); row[DB_exp_date]=(char *)Malloc(tm->length+1); memcpy(row[DB_exp_date],tm->data,tm->length); row[DB_exp_date][tm->length]='\0'; row[DB_rev_date]=NULL; row[DB_file]=(char *)Malloc(8); if ((row[DB_type] == NULL) || (row[DB_exp_date] == NULL) || (row[DB_file] == NULL)) { BIO_printf(bio_err,"Malloc failure\n"); goto err; } strcpy(row[DB_file],"unknown"); row[DB_type][0]='V'; row[DB_type][1]='\0'; if ((irow=(char **)Malloc(sizeof(char *)*(DB_NUMBER+1))) == NULL) { BIO_printf(bio_err,"Malloc failure\n"); goto err; } for (i=0; i<DB_NUMBER; i++) { irow[i]=row[i]; row[i]=NULL; } irow[DB_NUMBER]=NULL; if (!TXT_DB_insert(db,irow)) { BIO_printf(bio_err,"failed to update database\n"); BIO_printf(bio_err,"TXT_DB error number %ld\n",db->error); goto err; } ok=1; err: for (i=0; i<DB_NUMBER; i++) if (row[i] != NULL) Free(row[i]); if (CAname != NULL) X509_NAME_free(CAname); if (subject != NULL) X509_NAME_free(subject); if (ok <= 0) { if (ret != NULL) X509_free(ret); ret=NULL; } else *xret=ret; return(ok); }
['static int do_body(X509 **xret, EVP_PKEY *pkey, X509 *x509, const EVP_MD *dgst,\n\t STACK_OF(CONF_VALUE) *policy, TXT_DB *db, BIGNUM *serial,\n\t char *startdate, char *enddate, int days, int batch, int verbose,\n\t X509_REQ *req, char *ext_sect, LHASH *lconf)\n\t{\n\tX509_NAME *name=NULL,*CAname=NULL,*subject=NULL;\n\tASN1_UTCTIME *tm,*tmptm;\n\tASN1_STRING *str,*str2;\n\tASN1_OBJECT *obj;\n\tX509 *ret=NULL;\n\tX509_CINF *ci;\n\tX509_NAME_ENTRY *ne;\n\tX509_NAME_ENTRY *tne,*push;\n\tEVP_PKEY *pktmp;\n\tint ok= -1,i,j,last,nid;\n\tchar *p;\n\tCONF_VALUE *cv;\n\tchar *row[DB_NUMBER],**rrow,**irow=NULL;\n\tchar buf[25],*pbuf;\n\ttmptm=ASN1_UTCTIME_new();\n\tif (tmptm == NULL)\n\t\t{\n\t\tBIO_printf(bio_err,"malloc error\\n");\n\t\treturn(0);\n\t\t}\n\tfor (i=0; i<DB_NUMBER; i++)\n\t\trow[i]=NULL;\n\tBIO_printf(bio_err,"The Subjects Distinguished Name is as follows\\n");\n\tname=X509_REQ_get_subject_name(req);\n\tfor (i=0; i<X509_NAME_entry_count(name); i++)\n\t\t{\n\t\tne=(X509_NAME_ENTRY *)X509_NAME_get_entry(name,i);\n\t\tobj=X509_NAME_ENTRY_get_object(ne);\n\t\tj=i2a_ASN1_OBJECT(bio_err,obj);\n\t\tstr=X509_NAME_ENTRY_get_data(ne);\n\t\tpbuf=buf;\n\t\tfor (j=22-j; j>0; j--)\n\t\t\t*(pbuf++)=\' \';\n\t\t*(pbuf++)=\':\';\n\t\t*(pbuf++)=\'\\0\';\n\t\tBIO_puts(bio_err,buf);\n\t\tif (msie_hack)\n\t\t\t{\n\t\t\tnid=OBJ_obj2nid(ne->object);\n\t\t\tif (str->type == V_ASN1_UNIVERSALSTRING)\n\t\t\t\tASN1_UNIVERSALSTRING_to_string(str);\n\t\t\tif ((str->type == V_ASN1_IA5STRING) &&\n\t\t\t\t(nid != NID_pkcs9_emailAddress))\n\t\t\t\tstr->type=V_ASN1_T61STRING;\n\t\t\tif ((nid == NID_pkcs9_emailAddress) &&\n\t\t\t\t(str->type == V_ASN1_PRINTABLESTRING))\n\t\t\t\tstr->type=V_ASN1_IA5STRING;\n\t\t\t}\n\t\tif (str->type == V_ASN1_PRINTABLESTRING)\n\t\t\tBIO_printf(bio_err,"PRINTABLE:\'");\n\t\telse if (str->type == V_ASN1_T61STRING)\n\t\t\tBIO_printf(bio_err,"T61STRING:\'");\n\t\telse if (str->type == V_ASN1_IA5STRING)\n\t\t\tBIO_printf(bio_err,"IA5STRING:\'");\n\t\telse if (str->type == V_ASN1_UNIVERSALSTRING)\n\t\t\tBIO_printf(bio_err,"UNIVERSALSTRING:\'");\n\t\telse\n\t\t\tBIO_printf(bio_err,"ASN.1 %2d:\'",str->type);\n\t\tif ((OBJ_obj2nid(obj) == NID_pkcs9_emailAddress) &&\n\t\t\t(str->type != V_ASN1_IA5STRING))\n\t\t\t{\n\t\t\tBIO_printf(bio_err,"\\nemailAddress type needs to be of type IA5STRING\\n");\n\t\t\tgoto err;\n\t\t\t}\n\t\tj=ASN1_PRINTABLE_type(str->data,str->length);\n\t\tif (\t((j == V_ASN1_T61STRING) &&\n\t\t\t (str->type != V_ASN1_T61STRING)) ||\n\t\t\t((j == V_ASN1_IA5STRING) &&\n\t\t\t (str->type == V_ASN1_PRINTABLESTRING)))\n\t\t\t{\n\t\t\tBIO_printf(bio_err,"\\nThe string contains characters that are illegal for the ASN.1 type\\n");\n\t\t\tgoto err;\n\t\t\t}\n\t\tp=(char *)str->data;\n\t\tfor (j=str->length; j>0; j--)\n\t\t\t{\n\t\t\tif ((*p >= \' \') && (*p <= \'~\'))\n\t\t\t\tBIO_printf(bio_err,"%c",*p);\n\t\t\telse if (*p & 0x80)\n\t\t\t\tBIO_printf(bio_err,"\\\\0x%02X",*p);\n\t\t\telse if ((unsigned char)*p == 0xf7)\n\t\t\t\tBIO_printf(bio_err,"^?");\n\t\t\telse\tBIO_printf(bio_err,"^%c",*p+\'@\');\n\t\t\tp++;\n\t\t\t}\n\t\tBIO_printf(bio_err,"\'\\n");\n\t\t}\n\tif ((subject=X509_NAME_new()) == NULL)\n\t\t{\n\t\tBIO_printf(bio_err,"Malloc failure\\n");\n\t\tgoto err;\n\t\t}\n\tCAname=X509_NAME_dup(x509->cert_info->subject);\n\tif (CAname == NULL) goto err;\n\tstr=str2=NULL;\n\tfor (i=0; i<sk_CONF_VALUE_num(policy); i++)\n\t\t{\n\t\tcv=sk_CONF_VALUE_value(policy,i);\n\t\tif ((j=OBJ_txt2nid(cv->name)) == NID_undef)\n\t\t\t{\n\t\t\tBIO_printf(bio_err,"%s:unknown object type in \'policy\' configuration\\n",cv->name);\n\t\t\tgoto err;\n\t\t\t}\n\t\tobj=OBJ_nid2obj(j);\n\t\tlast= -1;\n\t\tfor (;;)\n\t\t\t{\n\t\t\tj=X509_NAME_get_index_by_OBJ(name,obj,last);\n\t\t\tif (j < 0)\n\t\t\t\t{\n\t\t\t\tif (last != -1) break;\n\t\t\t\ttne=NULL;\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\ttne=X509_NAME_get_entry(name,j);\n\t\t\t\t}\n\t\t\tlast=j;\n\t\t\tpush=NULL;\n\t\t\tif (strcmp(cv->value,"optional") == 0)\n\t\t\t\t{\n\t\t\t\tif (tne != NULL)\n\t\t\t\t\tpush=tne;\n\t\t\t\t}\n\t\t\telse if (strcmp(cv->value,"supplied") == 0)\n\t\t\t\t{\n\t\t\t\tif (tne == NULL)\n\t\t\t\t\t{\n\t\t\t\t\tBIO_printf(bio_err,"The %s field needed to be supplied and was missing\\n",cv->name);\n\t\t\t\t\tgoto err;\n\t\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t\tpush=tne;\n\t\t\t\t}\n\t\t\telse if (strcmp(cv->value,"match") == 0)\n\t\t\t\t{\n\t\t\t\tint last2;\n\t\t\t\tif (tne == NULL)\n\t\t\t\t\t{\n\t\t\t\t\tBIO_printf(bio_err,"The mandatory %s field was missing\\n",cv->name);\n\t\t\t\t\tgoto err;\n\t\t\t\t\t}\n\t\t\t\tlast2= -1;\nagain2:\n\t\t\t\tj=X509_NAME_get_index_by_OBJ(CAname,obj,last2);\n\t\t\t\tif ((j < 0) && (last2 == -1))\n\t\t\t\t\t{\n\t\t\t\t\tBIO_printf(bio_err,"The %s field does not exist in the CA certificate,\\nthe \'policy\' is misconfigured\\n",cv->name);\n\t\t\t\t\tgoto err;\n\t\t\t\t\t}\n\t\t\t\tif (j >= 0)\n\t\t\t\t\t{\n\t\t\t\t\tpush=X509_NAME_get_entry(CAname,j);\n\t\t\t\t\tstr=X509_NAME_ENTRY_get_data(tne);\n\t\t\t\t\tstr2=X509_NAME_ENTRY_get_data(push);\n\t\t\t\t\tlast2=j;\n\t\t\t\t\tif (ASN1_STRING_cmp(str,str2) != 0)\n\t\t\t\t\t\tgoto again2;\n\t\t\t\t\t}\n\t\t\t\tif (j < 0)\n\t\t\t\t\t{\n\t\t\t\t\tBIO_printf(bio_err,"The %s field needed to be the same in the\\nCA certificate (%s) and the request (%s)\\n",cv->name,((str == NULL)?"NULL":(char *)str->data),((str2 == NULL)?"NULL":(char *)str2->data));\n\t\t\t\t\tgoto err;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\tBIO_printf(bio_err,"%s:invalid type in \'policy\' configuration\\n",cv->value);\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\tif (push != NULL)\n\t\t\t\t{\n\t\t\t\tif (!X509_NAME_add_entry(subject,push,\n\t\t\t\t\tX509_NAME_entry_count(subject),0))\n\t\t\t\t\t{\n\t\t\t\t\tif (push != NULL)\n\t\t\t\t\t\tX509_NAME_ENTRY_free(push);\n\t\t\t\t\tBIO_printf(bio_err,"Malloc failure\\n");\n\t\t\t\t\tgoto err;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\tif (j < 0) break;\n\t\t\t}\n\t\t}\n\tif (preserve)\n\t\t{\n\t\tX509_NAME_free(subject);\n\t\tsubject=X509_NAME_dup(X509_REQ_get_subject_name(req));\n\t\tif (subject == NULL) goto err;\n\t\t}\n\tif (verbose)\n\t\tBIO_printf(bio_err,"The subject name apears to be ok, checking data base for clashes\\n");\n\trow[DB_name]=X509_NAME_oneline(subject,NULL,0);\n\trow[DB_serial]=BN_bn2hex(serial);\n\tif ((row[DB_name] == NULL) || (row[DB_serial] == NULL))\n\t\t{\n\t\tBIO_printf(bio_err,"Malloc failure\\n");\n\t\tgoto err;\n\t\t}\n\trrow=TXT_DB_get_by_index(db,DB_name,row);\n\tif (rrow != NULL)\n\t\t{\n\t\tBIO_printf(bio_err,"ERROR:There is already a certificate for %s\\n",\n\t\t\trow[DB_name]);\n\t\t}\n\telse\n\t\t{\n\t\trrow=TXT_DB_get_by_index(db,DB_serial,row);\n\t\tif (rrow != NULL)\n\t\t\t{\n\t\t\tBIO_printf(bio_err,"ERROR:Serial number %s has already been issued,\\n",\n\t\t\t\trow[DB_serial]);\n\t\t\tBIO_printf(bio_err," check the database/serial_file for corruption\\n");\n\t\t\t}\n\t\t}\n\tif (rrow != NULL)\n\t\t{\n\t\tBIO_printf(bio_err,\n\t\t\t"The matching entry has the following details\\n");\n\t\tif (rrow[DB_type][0] == \'E\')\n\t\t\tp="Expired";\n\t\telse if (rrow[DB_type][0] == \'R\')\n\t\t\tp="Revoked";\n\t\telse if (rrow[DB_type][0] == \'V\')\n\t\t\tp="Valid";\n\t\telse\n\t\t\tp="\\ninvalid type, Data base error\\n";\n\t\tBIO_printf(bio_err,"Type\t :%s\\n",p);;\n\t\tif (rrow[DB_type][0] == \'R\')\n\t\t\t{\n\t\t\tp=rrow[DB_exp_date]; if (p == NULL) p="undef";\n\t\t\tBIO_printf(bio_err,"Was revoked on:%s\\n",p);\n\t\t\t}\n\t\tp=rrow[DB_exp_date]; if (p == NULL) p="undef";\n\t\tBIO_printf(bio_err,"Expires on :%s\\n",p);\n\t\tp=rrow[DB_serial]; if (p == NULL) p="undef";\n\t\tBIO_printf(bio_err,"Serial Number :%s\\n",p);\n\t\tp=rrow[DB_file]; if (p == NULL) p="undef";\n\t\tBIO_printf(bio_err,"File name :%s\\n",p);\n\t\tp=rrow[DB_name]; if (p == NULL) p="undef";\n\t\tBIO_printf(bio_err,"Subject Name :%s\\n",p);\n\t\tok= -1;\n\t\tgoto err;\n\t\t}\n\tif (verbose)\n\t\tBIO_printf(bio_err,"Everything appears to be ok, creating and signing the certificate\\n");\n\tif ((ret=X509_new()) == NULL) goto err;\n\tci=ret->cert_info;\n#ifdef X509_V3\n\tif (!X509_set_version(x509,2)) goto err;\n#endif\n\tif (BN_to_ASN1_INTEGER(serial,ci->serialNumber) == NULL)\n\t\tgoto err;\n\tif (!X509_set_issuer_name(ret,X509_get_subject_name(x509)))\n\t\tgoto err;\n\tBIO_printf(bio_err,"Certificate is to be certified until ");\n\tif (strcmp(startdate,"today") == 0)\n\t\tX509_gmtime_adj(X509_get_notBefore(ret),0);\n\telse ASN1_UTCTIME_set_string(X509_get_notBefore(ret),startdate);\n\tif (enddate == NULL)\n\t\tX509_gmtime_adj(X509_get_notAfter(ret),(long)60*60*24*days);\n\telse ASN1_UTCTIME_set_string(X509_get_notAfter(ret),enddate);\n\tASN1_UTCTIME_print(bio_err,X509_get_notAfter(ret));\n\tif(days) BIO_printf(bio_err," (%d days)",days);\n\tBIO_printf(bio_err, "\\n");\n\tif (!X509_set_subject_name(ret,subject)) goto err;\n\tpktmp=X509_REQ_get_pubkey(req);\n\ti = X509_set_pubkey(ret,pktmp);\n\tEVP_PKEY_free(pktmp);\n\tif (!i) goto err;\n\tif (ext_sect)\n\t\t{\n\t\tX509V3_CTX ctx;\n\t\tif (ci->version == NULL)\n\t\t\tif ((ci->version=ASN1_INTEGER_new()) == NULL)\n\t\t\t\tgoto err;\n\t\tASN1_INTEGER_set(ci->version,2);\n\t\tif (ci->extensions != NULL)\n\t\t\tsk_X509_EXTENSION_pop_free(ci->extensions,\n\t\t\t\t\t\t X509_EXTENSION_free);\n\t\tci->extensions = NULL;\n\t\tX509V3_set_ctx(&ctx, x509, ret, req, NULL, 0);\n\t\tX509V3_set_conf_lhash(&ctx, lconf);\n\t\tif(!X509V3_EXT_add_conf(lconf, &ctx, ext_sect, ret)) goto err;\n\t\t}\n\tif (!batch)\n\t\t{\n\t\tBIO_printf(bio_err,"Sign the certificate? [y/n]:");\n\t\t(void)BIO_flush(bio_err);\n\t\tbuf[0]=\'\\0\';\n\t\tfgets(buf,sizeof(buf)-1,stdin);\n\t\tif (!((buf[0] == \'y\') || (buf[0] == \'Y\')))\n\t\t\t{\n\t\t\tBIO_printf(bio_err,"CERTIFICATE WILL NOT BE CERTIFIED\\n");\n\t\t\tok=0;\n\t\t\tgoto err;\n\t\t\t}\n\t\t}\n#ifndef NO_DSA\n\tif (pkey->type == EVP_PKEY_DSA) dgst=EVP_dss1();\n\tpktmp=X509_get_pubkey(ret);\n\tif (EVP_PKEY_missing_parameters(pktmp) &&\n\t\t!EVP_PKEY_missing_parameters(pkey))\n\t\tEVP_PKEY_copy_parameters(pktmp,pkey);\n\tEVP_PKEY_free(pktmp);\n#endif\n\tif (!X509_sign(ret,pkey,dgst))\n\t\tgoto err;\n\trow[DB_type]=(char *)Malloc(2);\n\ttm=X509_get_notAfter(ret);\n\trow[DB_exp_date]=(char *)Malloc(tm->length+1);\n\tmemcpy(row[DB_exp_date],tm->data,tm->length);\n\trow[DB_exp_date][tm->length]=\'\\0\';\n\trow[DB_rev_date]=NULL;\n\trow[DB_file]=(char *)Malloc(8);\n\tif ((row[DB_type] == NULL) || (row[DB_exp_date] == NULL) ||\n\t\t(row[DB_file] == NULL))\n\t\t{\n\t\tBIO_printf(bio_err,"Malloc failure\\n");\n\t\tgoto err;\n\t\t}\n\tstrcpy(row[DB_file],"unknown");\n\trow[DB_type][0]=\'V\';\n\trow[DB_type][1]=\'\\0\';\n\tif ((irow=(char **)Malloc(sizeof(char *)*(DB_NUMBER+1))) == NULL)\n\t\t{\n\t\tBIO_printf(bio_err,"Malloc failure\\n");\n\t\tgoto err;\n\t\t}\n\tfor (i=0; i<DB_NUMBER; i++)\n\t\t{\n\t\tirow[i]=row[i];\n\t\trow[i]=NULL;\n\t\t}\n\tirow[DB_NUMBER]=NULL;\n\tif (!TXT_DB_insert(db,irow))\n\t\t{\n\t\tBIO_printf(bio_err,"failed to update database\\n");\n\t\tBIO_printf(bio_err,"TXT_DB error number %ld\\n",db->error);\n\t\tgoto err;\n\t\t}\n\tok=1;\nerr:\n\tfor (i=0; i<DB_NUMBER; i++)\n\t\tif (row[i] != NULL) Free(row[i]);\n\tif (CAname != NULL)\n\t\tX509_NAME_free(CAname);\n\tif (subject != NULL)\n\t\tX509_NAME_free(subject);\n\tif (ok <= 0)\n\t\t{\n\t\tif (ret != NULL) X509_free(ret);\n\t\tret=NULL;\n\t\t}\n\telse\n\t\t*xret=ret;\n\treturn(ok);\n\t}', 'ASN1_STRING *ASN1_STRING_type_new(int type)\n\t{\n\tASN1_STRING *ret;\n\tret=(ASN1_STRING *)Malloc(sizeof(ASN1_STRING));\n\tif (ret == NULL)\n\t\t{\n\t\tASN1err(ASN1_F_ASN1_STRING_TYPE_NEW,ERR_R_MALLOC_FAILURE);\n\t\treturn(NULL);\n\t\t}\n\tret->length=0;\n\tret->type=type;\n\tret->data=NULL;\n\tret->flags=0;\n\treturn(ret);\n\t}']
206
0
https://github.com/libav/libav/blob/452a398fd6bdca3f301c5c8af3bc241bc16a777e/libavcodec/vc1.c/#L3207
static int vc1_decode_p_mb(VC1Context *v) { MpegEncContext *s = &v->s; GetBitContext *gb = &s->gb; int i, j; int mb_pos = s->mb_x + s->mb_y * s->mb_stride; int cbp; int mqdiff, mquant; int ttmb = v->ttfrm; static const int size_table[6] = { 0, 2, 3, 4, 5, 8 }, offset_table[6] = { 0, 1, 3, 7, 15, 31 }; int mb_has_coeffs = 1; int dmv_x, dmv_y; int index, index1; int val, sign; int first_block = 1; int dst_idx, off; int skipped, fourmv; int block_cbp = 0, pat; mquant = v->pq; if (v->mv_type_is_raw) fourmv = get_bits1(gb); else fourmv = v->mv_type_mb_plane[mb_pos]; if (v->skip_is_raw) skipped = get_bits1(gb); else skipped = v->s.mbskip_table[mb_pos]; s->dsp.clear_blocks(s->block[0]); if (!fourmv) { if (!skipped) { GET_MVDATA(dmv_x, dmv_y); if (s->mb_intra) { s->current_picture.motion_val[1][s->block_index[0]][0] = 0; s->current_picture.motion_val[1][s->block_index[0]][1] = 0; } s->current_picture.mb_type[mb_pos] = s->mb_intra ? MB_TYPE_INTRA : MB_TYPE_16x16; vc1_pred_mv(s, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0]); if (s->mb_intra && !mb_has_coeffs) { GET_MQUANT(); s->ac_pred = get_bits1(gb); cbp = 0; } else if (mb_has_coeffs) { if (s->mb_intra) s->ac_pred = get_bits1(gb); cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2); GET_MQUANT(); } else { mquant = v->pq; cbp = 0; } s->current_picture.qscale_table[mb_pos] = mquant; if (!v->ttmbf && !s->mb_intra && mb_has_coeffs) ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2); if(!s->mb_intra) vc1_mc_1mv(v, 0); dst_idx = 0; for (i=0; i<6; i++) { s->dc_val[0][s->block_index[i]] = 0; dst_idx += i >> 2; val = ((cbp >> (5 - i)) & 1); off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize); v->mb_type[0][s->block_index[i]] = s->mb_intra; if(s->mb_intra) { v->a_avail = v->c_avail = 0; if(i == 2 || i == 3 || !s->first_slice_line) v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]]; if(i == 1 || i == 3 || s->mb_x) v->c_avail = v->mb_type[0][s->block_index[i] - 1]; vc1_decode_intra_block(v, s->block[i], i, val, mquant, (i&4)?v->codingset2:v->codingset); if((i>3) && (s->flags & CODEC_FLAG_GRAY)) continue; s->dsp.vc1_inv_trans_8x8(s->block[i]); if(v->rangeredfrm) for(j = 0; j < 64; j++) s->block[i][j] <<= 1; s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2)); if(v->pq >= 9 && v->overlap) { if(v->c_avail) s->dsp.vc1_h_overlap(s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2)); if(v->a_avail) s->dsp.vc1_v_overlap(s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2)); } if(v->s.loop_filter && s->mb_x && s->mb_x != (s->mb_width - 1) && s->mb_y && s->mb_y != (s->mb_height - 1)){ int left_cbp, top_cbp; if(i & 4){ left_cbp = v->cbp[s->mb_x - 1] >> (i * 4); top_cbp = v->cbp[s->mb_x - s->mb_stride] >> (i * 4); }else{ left_cbp = (i & 1) ? (pat >> ((i-1)*4)) : (v->cbp[s->mb_x - 1] >> ((i+1)*4)); top_cbp = (i & 2) ? (pat >> ((i-2)*4)) : (v->cbp[s->mb_x - s->mb_stride] >> ((i+2)*4)); } if(left_cbp & 0xC) vc1_loop_filter(s->dest[dst_idx] + off, 1, i & 4 ? s->uvlinesize : s->linesize, 8, mquant); if(top_cbp & 0xA) vc1_loop_filter(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize, 1, 8, mquant); } block_cbp |= 0xF << (i << 2); } else if(val) { int left_cbp = 0, top_cbp = 0, filter = 0; if(v->s.loop_filter && s->mb_x && s->mb_x != (s->mb_width - 1) && s->mb_y && s->mb_y != (s->mb_height - 1)){ filter = 1; if(i & 4){ left_cbp = v->cbp[s->mb_x - 1] >> (i * 4); top_cbp = v->cbp[s->mb_x - s->mb_stride] >> (i * 4); }else{ left_cbp = (i & 1) ? (pat >> ((i-1)*4)) : (v->cbp[s->mb_x - 1] >> ((i+1)*4)); top_cbp = (i & 2) ? (pat >> ((i-2)*4)) : (v->cbp[s->mb_x - s->mb_stride] >> ((i+2)*4)); } } pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb, first_block, s->dest[dst_idx] + off, (i&4)?s->uvlinesize:s->linesize, (i&4) && (s->flags & CODEC_FLAG_GRAY), filter, left_cbp, top_cbp); block_cbp |= pat << (i << 2); if(!v->ttmbf && ttmb < 8) ttmb = -1; first_block = 0; } } } else { s->mb_intra = 0; for(i = 0; i < 6; i++) { v->mb_type[0][s->block_index[i]] = 0; s->dc_val[0][s->block_index[i]] = 0; } s->current_picture.mb_type[mb_pos] = MB_TYPE_SKIP; s->current_picture.qscale_table[mb_pos] = 0; vc1_pred_mv(s, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0]); vc1_mc_1mv(v, 0); return 0; } } else { if (!skipped ) { int intra_count = 0, coded_inter = 0; int is_intra[6], is_coded[6]; cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2); for (i=0; i<6; i++) { val = ((cbp >> (5 - i)) & 1); s->dc_val[0][s->block_index[i]] = 0; s->mb_intra = 0; if(i < 4) { dmv_x = dmv_y = 0; s->mb_intra = 0; mb_has_coeffs = 0; if(val) { GET_MVDATA(dmv_x, dmv_y); } vc1_pred_mv(s, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0]); if(!s->mb_intra) vc1_mc_4mv_luma(v, i); intra_count += s->mb_intra; is_intra[i] = s->mb_intra; is_coded[i] = mb_has_coeffs; } if(i&4){ is_intra[i] = (intra_count >= 3); is_coded[i] = val; } if(i == 4) vc1_mc_4mv_chroma(v); v->mb_type[0][s->block_index[i]] = is_intra[i]; if(!coded_inter) coded_inter = !is_intra[i] & is_coded[i]; } if(!intra_count && !coded_inter) return 0; dst_idx = 0; GET_MQUANT(); s->current_picture.qscale_table[mb_pos] = mquant; { int intrapred = 0; for(i=0; i<6; i++) if(is_intra[i]) { if(((!s->first_slice_line || (i==2 || i==3)) && v->mb_type[0][s->block_index[i] - s->block_wrap[i]]) || ((s->mb_x || (i==1 || i==3)) && v->mb_type[0][s->block_index[i] - 1])) { intrapred = 1; break; } } if(intrapred)s->ac_pred = get_bits1(gb); else s->ac_pred = 0; } if (!v->ttmbf && coded_inter) ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2); for (i=0; i<6; i++) { dst_idx += i >> 2; off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize); s->mb_intra = is_intra[i]; if (is_intra[i]) { v->a_avail = v->c_avail = 0; if(i == 2 || i == 3 || !s->first_slice_line) v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]]; if(i == 1 || i == 3 || s->mb_x) v->c_avail = v->mb_type[0][s->block_index[i] - 1]; vc1_decode_intra_block(v, s->block[i], i, is_coded[i], mquant, (i&4)?v->codingset2:v->codingset); if((i>3) && (s->flags & CODEC_FLAG_GRAY)) continue; s->dsp.vc1_inv_trans_8x8(s->block[i]); if(v->rangeredfrm) for(j = 0; j < 64; j++) s->block[i][j] <<= 1; s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, (i&4)?s->uvlinesize:s->linesize); if(v->pq >= 9 && v->overlap) { if(v->c_avail) s->dsp.vc1_h_overlap(s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2)); if(v->a_avail) s->dsp.vc1_v_overlap(s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2)); } if(v->s.loop_filter && s->mb_x && s->mb_x != (s->mb_width - 1) && s->mb_y && s->mb_y != (s->mb_height - 1)){ int left_cbp, top_cbp; if(i & 4){ left_cbp = v->cbp[s->mb_x - 1] >> (i * 4); top_cbp = v->cbp[s->mb_x - s->mb_stride] >> (i * 4); }else{ left_cbp = (i & 1) ? (pat >> ((i-1)*4)) : (v->cbp[s->mb_x - 1] >> ((i+1)*4)); top_cbp = (i & 2) ? (pat >> ((i-2)*4)) : (v->cbp[s->mb_x - s->mb_stride] >> ((i+2)*4)); } if(left_cbp & 0xC) vc1_loop_filter(s->dest[dst_idx] + off, 1, i & 4 ? s->uvlinesize : s->linesize, 8, mquant); if(top_cbp & 0xA) vc1_loop_filter(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize, 1, 8, mquant); } block_cbp |= 0xF << (i << 2); } else if(is_coded[i]) { int left_cbp = 0, top_cbp = 0, filter = 0; if(v->s.loop_filter && s->mb_x && s->mb_x != (s->mb_width - 1) && s->mb_y && s->mb_y != (s->mb_height - 1)){ filter = 1; if(i & 4){ left_cbp = v->cbp[s->mb_x - 1] >> (i * 4); top_cbp = v->cbp[s->mb_x - s->mb_stride] >> (i * 4); }else{ left_cbp = (i & 1) ? (pat >> ((i-1)*4)) : (v->cbp[s->mb_x - 1] >> ((i+1)*4)); top_cbp = (i & 2) ? (pat >> ((i-2)*4)) : (v->cbp[s->mb_x - s->mb_stride] >> ((i+2)*4)); } } pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb, first_block, s->dest[dst_idx] + off, (i&4)?s->uvlinesize:s->linesize, (i&4) && (s->flags & CODEC_FLAG_GRAY), filter, left_cbp, top_cbp); block_cbp |= pat << (i << 2); if(!v->ttmbf && ttmb < 8) ttmb = -1; first_block = 0; } } return 0; } else { s->mb_intra = 0; s->current_picture.qscale_table[mb_pos] = 0; for (i=0; i<6; i++) { v->mb_type[0][s->block_index[i]] = 0; s->dc_val[0][s->block_index[i]] = 0; } for (i=0; i<4; i++) { vc1_pred_mv(s, i, 0, 0, 0, v->range_x, v->range_y, v->mb_type[0]); vc1_mc_4mv_luma(v, i); } vc1_mc_4mv_chroma(v); s->current_picture.qscale_table[mb_pos] = 0; return 0; } } v->cbp[s->mb_x] = block_cbp; return -1; }
['static int vc1_decode_p_mb(VC1Context *v)\n{\n MpegEncContext *s = &v->s;\n GetBitContext *gb = &s->gb;\n int i, j;\n int mb_pos = s->mb_x + s->mb_y * s->mb_stride;\n int cbp;\n int mqdiff, mquant;\n int ttmb = v->ttfrm;\n static const int size_table[6] = { 0, 2, 3, 4, 5, 8 },\n offset_table[6] = { 0, 1, 3, 7, 15, 31 };\n int mb_has_coeffs = 1;\n int dmv_x, dmv_y;\n int index, index1;\n int val, sign;\n int first_block = 1;\n int dst_idx, off;\n int skipped, fourmv;\n int block_cbp = 0, pat;\n mquant = v->pq;\n if (v->mv_type_is_raw)\n fourmv = get_bits1(gb);\n else\n fourmv = v->mv_type_mb_plane[mb_pos];\n if (v->skip_is_raw)\n skipped = get_bits1(gb);\n else\n skipped = v->s.mbskip_table[mb_pos];\n s->dsp.clear_blocks(s->block[0]);\n if (!fourmv)\n {\n if (!skipped)\n {\n GET_MVDATA(dmv_x, dmv_y);\n if (s->mb_intra) {\n s->current_picture.motion_val[1][s->block_index[0]][0] = 0;\n s->current_picture.motion_val[1][s->block_index[0]][1] = 0;\n }\n s->current_picture.mb_type[mb_pos] = s->mb_intra ? MB_TYPE_INTRA : MB_TYPE_16x16;\n vc1_pred_mv(s, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0]);\n if (s->mb_intra && !mb_has_coeffs)\n {\n GET_MQUANT();\n s->ac_pred = get_bits1(gb);\n cbp = 0;\n }\n else if (mb_has_coeffs)\n {\n if (s->mb_intra) s->ac_pred = get_bits1(gb);\n cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);\n GET_MQUANT();\n }\n else\n {\n mquant = v->pq;\n cbp = 0;\n }\n s->current_picture.qscale_table[mb_pos] = mquant;\n if (!v->ttmbf && !s->mb_intra && mb_has_coeffs)\n ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table,\n VC1_TTMB_VLC_BITS, 2);\n if(!s->mb_intra) vc1_mc_1mv(v, 0);\n dst_idx = 0;\n for (i=0; i<6; i++)\n {\n s->dc_val[0][s->block_index[i]] = 0;\n dst_idx += i >> 2;\n val = ((cbp >> (5 - i)) & 1);\n off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);\n v->mb_type[0][s->block_index[i]] = s->mb_intra;\n if(s->mb_intra) {\n v->a_avail = v->c_avail = 0;\n if(i == 2 || i == 3 || !s->first_slice_line)\n v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];\n if(i == 1 || i == 3 || s->mb_x)\n v->c_avail = v->mb_type[0][s->block_index[i] - 1];\n vc1_decode_intra_block(v, s->block[i], i, val, mquant, (i&4)?v->codingset2:v->codingset);\n if((i>3) && (s->flags & CODEC_FLAG_GRAY)) continue;\n s->dsp.vc1_inv_trans_8x8(s->block[i]);\n if(v->rangeredfrm) for(j = 0; j < 64; j++) s->block[i][j] <<= 1;\n s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2));\n if(v->pq >= 9 && v->overlap) {\n if(v->c_avail)\n s->dsp.vc1_h_overlap(s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2));\n if(v->a_avail)\n s->dsp.vc1_v_overlap(s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2));\n }\n if(v->s.loop_filter && s->mb_x && s->mb_x != (s->mb_width - 1) && s->mb_y && s->mb_y != (s->mb_height - 1)){\n int left_cbp, top_cbp;\n if(i & 4){\n left_cbp = v->cbp[s->mb_x - 1] >> (i * 4);\n top_cbp = v->cbp[s->mb_x - s->mb_stride] >> (i * 4);\n }else{\n left_cbp = (i & 1) ? (pat >> ((i-1)*4)) : (v->cbp[s->mb_x - 1] >> ((i+1)*4));\n top_cbp = (i & 2) ? (pat >> ((i-2)*4)) : (v->cbp[s->mb_x - s->mb_stride] >> ((i+2)*4));\n }\n if(left_cbp & 0xC)\n vc1_loop_filter(s->dest[dst_idx] + off, 1, i & 4 ? s->uvlinesize : s->linesize, 8, mquant);\n if(top_cbp & 0xA)\n vc1_loop_filter(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize, 1, 8, mquant);\n }\n block_cbp |= 0xF << (i << 2);\n } else if(val) {\n int left_cbp = 0, top_cbp = 0, filter = 0;\n if(v->s.loop_filter && s->mb_x && s->mb_x != (s->mb_width - 1) && s->mb_y && s->mb_y != (s->mb_height - 1)){\n filter = 1;\n if(i & 4){\n left_cbp = v->cbp[s->mb_x - 1] >> (i * 4);\n top_cbp = v->cbp[s->mb_x - s->mb_stride] >> (i * 4);\n }else{\n left_cbp = (i & 1) ? (pat >> ((i-1)*4)) : (v->cbp[s->mb_x - 1] >> ((i+1)*4));\n top_cbp = (i & 2) ? (pat >> ((i-2)*4)) : (v->cbp[s->mb_x - s->mb_stride] >> ((i+2)*4));\n }\n }\n pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb, first_block, s->dest[dst_idx] + off, (i&4)?s->uvlinesize:s->linesize, (i&4) && (s->flags & CODEC_FLAG_GRAY), filter, left_cbp, top_cbp);\n block_cbp |= pat << (i << 2);\n if(!v->ttmbf && ttmb < 8) ttmb = -1;\n first_block = 0;\n }\n }\n }\n else\n {\n s->mb_intra = 0;\n for(i = 0; i < 6; i++) {\n v->mb_type[0][s->block_index[i]] = 0;\n s->dc_val[0][s->block_index[i]] = 0;\n }\n s->current_picture.mb_type[mb_pos] = MB_TYPE_SKIP;\n s->current_picture.qscale_table[mb_pos] = 0;\n vc1_pred_mv(s, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0]);\n vc1_mc_1mv(v, 0);\n return 0;\n }\n }\n else\n {\n if (!skipped )\n {\n int intra_count = 0, coded_inter = 0;\n int is_intra[6], is_coded[6];\n cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);\n for (i=0; i<6; i++)\n {\n val = ((cbp >> (5 - i)) & 1);\n s->dc_val[0][s->block_index[i]] = 0;\n s->mb_intra = 0;\n if(i < 4) {\n dmv_x = dmv_y = 0;\n s->mb_intra = 0;\n mb_has_coeffs = 0;\n if(val) {\n GET_MVDATA(dmv_x, dmv_y);\n }\n vc1_pred_mv(s, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0]);\n if(!s->mb_intra) vc1_mc_4mv_luma(v, i);\n intra_count += s->mb_intra;\n is_intra[i] = s->mb_intra;\n is_coded[i] = mb_has_coeffs;\n }\n if(i&4){\n is_intra[i] = (intra_count >= 3);\n is_coded[i] = val;\n }\n if(i == 4) vc1_mc_4mv_chroma(v);\n v->mb_type[0][s->block_index[i]] = is_intra[i];\n if(!coded_inter) coded_inter = !is_intra[i] & is_coded[i];\n }\n if(!intra_count && !coded_inter) return 0;\n dst_idx = 0;\n GET_MQUANT();\n s->current_picture.qscale_table[mb_pos] = mquant;\n {\n int intrapred = 0;\n for(i=0; i<6; i++)\n if(is_intra[i]) {\n if(((!s->first_slice_line || (i==2 || i==3)) && v->mb_type[0][s->block_index[i] - s->block_wrap[i]])\n || ((s->mb_x || (i==1 || i==3)) && v->mb_type[0][s->block_index[i] - 1])) {\n intrapred = 1;\n break;\n }\n }\n if(intrapred)s->ac_pred = get_bits1(gb);\n else s->ac_pred = 0;\n }\n if (!v->ttmbf && coded_inter)\n ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);\n for (i=0; i<6; i++)\n {\n dst_idx += i >> 2;\n off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);\n s->mb_intra = is_intra[i];\n if (is_intra[i]) {\n v->a_avail = v->c_avail = 0;\n if(i == 2 || i == 3 || !s->first_slice_line)\n v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];\n if(i == 1 || i == 3 || s->mb_x)\n v->c_avail = v->mb_type[0][s->block_index[i] - 1];\n vc1_decode_intra_block(v, s->block[i], i, is_coded[i], mquant, (i&4)?v->codingset2:v->codingset);\n if((i>3) && (s->flags & CODEC_FLAG_GRAY)) continue;\n s->dsp.vc1_inv_trans_8x8(s->block[i]);\n if(v->rangeredfrm) for(j = 0; j < 64; j++) s->block[i][j] <<= 1;\n s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, (i&4)?s->uvlinesize:s->linesize);\n if(v->pq >= 9 && v->overlap) {\n if(v->c_avail)\n s->dsp.vc1_h_overlap(s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2));\n if(v->a_avail)\n s->dsp.vc1_v_overlap(s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2));\n }\n if(v->s.loop_filter && s->mb_x && s->mb_x != (s->mb_width - 1) && s->mb_y && s->mb_y != (s->mb_height - 1)){\n int left_cbp, top_cbp;\n if(i & 4){\n left_cbp = v->cbp[s->mb_x - 1] >> (i * 4);\n top_cbp = v->cbp[s->mb_x - s->mb_stride] >> (i * 4);\n }else{\n left_cbp = (i & 1) ? (pat >> ((i-1)*4)) : (v->cbp[s->mb_x - 1] >> ((i+1)*4));\n top_cbp = (i & 2) ? (pat >> ((i-2)*4)) : (v->cbp[s->mb_x - s->mb_stride] >> ((i+2)*4));\n }\n if(left_cbp & 0xC)\n vc1_loop_filter(s->dest[dst_idx] + off, 1, i & 4 ? s->uvlinesize : s->linesize, 8, mquant);\n if(top_cbp & 0xA)\n vc1_loop_filter(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize, 1, 8, mquant);\n }\n block_cbp |= 0xF << (i << 2);\n } else if(is_coded[i]) {\n int left_cbp = 0, top_cbp = 0, filter = 0;\n if(v->s.loop_filter && s->mb_x && s->mb_x != (s->mb_width - 1) && s->mb_y && s->mb_y != (s->mb_height - 1)){\n filter = 1;\n if(i & 4){\n left_cbp = v->cbp[s->mb_x - 1] >> (i * 4);\n top_cbp = v->cbp[s->mb_x - s->mb_stride] >> (i * 4);\n }else{\n left_cbp = (i & 1) ? (pat >> ((i-1)*4)) : (v->cbp[s->mb_x - 1] >> ((i+1)*4));\n top_cbp = (i & 2) ? (pat >> ((i-2)*4)) : (v->cbp[s->mb_x - s->mb_stride] >> ((i+2)*4));\n }\n }\n pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb, first_block, s->dest[dst_idx] + off, (i&4)?s->uvlinesize:s->linesize, (i&4) && (s->flags & CODEC_FLAG_GRAY), filter, left_cbp, top_cbp);\n block_cbp |= pat << (i << 2);\n if(!v->ttmbf && ttmb < 8) ttmb = -1;\n first_block = 0;\n }\n }\n return 0;\n }\n else\n {\n s->mb_intra = 0;\n s->current_picture.qscale_table[mb_pos] = 0;\n for (i=0; i<6; i++) {\n v->mb_type[0][s->block_index[i]] = 0;\n s->dc_val[0][s->block_index[i]] = 0;\n }\n for (i=0; i<4; i++)\n {\n vc1_pred_mv(s, i, 0, 0, 0, v->range_x, v->range_y, v->mb_type[0]);\n vc1_mc_4mv_luma(v, i);\n }\n vc1_mc_4mv_chroma(v);\n s->current_picture.qscale_table[mb_pos] = 0;\n return 0;\n }\n }\n v->cbp[s->mb_x] = block_cbp;\n return -1;\n}']
207
0
https://github.com/libav/libav/blob/43317fd52cb967b4401e0401998bbf287489a2be/libavfilter/formats.c/#L140
void avfilter_formats_ref(AVFilterFormats *f, AVFilterFormats **ref) { *ref = f; f->refs = av_realloc(f->refs, sizeof(AVFilterFormats**) * ++f->refcount); f->refs[f->refcount-1] = ref; }
['void avfilter_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)\n{\n *ref = f;\n f->refs = av_realloc(f->refs, sizeof(AVFilterFormats**) * ++f->refcount);\n f->refs[f->refcount-1] = ref;\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}']
208
0
https://github.com/libav/libav/blob/ff866063e981ea6a51036c2ffd9bb152b8219437/ffserver.c/#L3921
static void load_module(const char *filename) { void *dll; void (*init_func)(void); dll = dlopen(filename, RTLD_NOW); if (!dll) { fprintf(stderr, "Could not load module '%s' - %s\n", filename, dlerror()); return; } init_func = dlsym(dll, "ffserver_module_init"); if (!init_func) { fprintf(stderr, "%s: init function 'ffserver_module_init()' not found\n", filename); dlclose(dll); } init_func(); }
['static void load_module(const char *filename)\n{\n void *dll;\n void (*init_func)(void);\n dll = dlopen(filename, RTLD_NOW);\n if (!dll) {\n fprintf(stderr, "Could not load module \'%s\' - %s\\n",\n filename, dlerror());\n return;\n }\n init_func = dlsym(dll, "ffserver_module_init");\n if (!init_func) {\n fprintf(stderr,\n "%s: init function \'ffserver_module_init()\' not found\\n",\n filename);\n dlclose(dll);\n }\n init_func();\n}']
209
0
https://github.com/openssl/openssl/blob/7d461736f7bd3af3c2f266f8541034ecf6f41ed9/crypto/bn/bn_lib.c/#L322
BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b) { bn_check_top(b); if (a == b) return a; if (bn_wexpand(a, b->top) == NULL) return NULL; if (b->top > 0) memcpy(a->d, b->d, sizeof(b->d[0]) * b->top); a->top = b->top; a->neg = b->neg; bn_check_top(a); return a; }
['static int ec_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from)\n{\n EC_GROUP *group = EC_GROUP_dup(EC_KEY_get0_group(from->pkey.ec));\n if (group == NULL)\n return 0;\n if (to->pkey.ec == NULL) {\n to->pkey.ec = EC_KEY_new();\n if (to->pkey.ec == NULL)\n goto err;\n }\n if (EC_KEY_set_group(to->pkey.ec, group) == 0)\n goto err;\n EC_GROUP_free(group);\n return 1;\n err:\n EC_GROUP_free(group);\n return 0;\n}', 'EC_GROUP *EC_GROUP_dup(const EC_GROUP *a)\n{\n EC_GROUP *t = NULL;\n int ok = 0;\n if (a == NULL)\n return NULL;\n if ((t = EC_GROUP_new(a->meth)) == NULL)\n return NULL;\n if (!EC_GROUP_copy(t, a))\n goto err;\n ok = 1;\n err:\n if (!ok) {\n EC_GROUP_free(t);\n return NULL;\n }\n return t;\n}', 'int EC_GROUP_copy(EC_GROUP *dest, const EC_GROUP *src)\n{\n if (dest->meth->group_copy == 0) {\n ECerr(EC_F_EC_GROUP_COPY, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);\n return 0;\n }\n if (dest->meth != src->meth) {\n ECerr(EC_F_EC_GROUP_COPY, EC_R_INCOMPATIBLE_OBJECTS);\n return 0;\n }\n if (dest == src)\n return 1;\n dest->pre_comp_type = src->pre_comp_type;\n switch (src->pre_comp_type) {\n case PCT_none:\n dest->pre_comp.ec = NULL;\n break;\n case PCT_nistz256:\n#ifdef ECP_NISTZ256_ASM\n dest->pre_comp.nistz256 = EC_nistz256_pre_comp_dup(src->pre_comp.nistz256);\n#endif\n break;\n#ifndef OPENSSL_NO_EC_NISTP_64_GCC_128\n case PCT_nistp224:\n dest->pre_comp.nistp224 = EC_nistp224_pre_comp_dup(src->pre_comp.nistp224);\n break;\n case PCT_nistp256:\n dest->pre_comp.nistp256 = EC_nistp256_pre_comp_dup(src->pre_comp.nistp256);\n break;\n case PCT_nistp521:\n dest->pre_comp.nistp521 = EC_nistp521_pre_comp_dup(src->pre_comp.nistp521);\n break;\n#else\n case PCT_nistp224:\n case PCT_nistp256:\n case PCT_nistp521:\n break;\n#endif\n case PCT_ec:\n dest->pre_comp.ec = EC_ec_pre_comp_dup(src->pre_comp.ec);\n break;\n }\n if (src->mont_data != NULL) {\n if (dest->mont_data == NULL) {\n dest->mont_data = BN_MONT_CTX_new();\n if (dest->mont_data == NULL)\n return 0;\n }\n if (!BN_MONT_CTX_copy(dest->mont_data, src->mont_data))\n return 0;\n } else {\n BN_MONT_CTX_free(dest->mont_data);\n dest->mont_data = NULL;\n }\n if (src->generator != NULL) {\n if (dest->generator == NULL) {\n dest->generator = EC_POINT_new(dest);\n if (dest->generator == NULL)\n return 0;\n }\n if (!EC_POINT_copy(dest->generator, src->generator))\n return 0;\n } else {\n EC_POINT_clear_free(dest->generator);\n dest->generator = NULL;\n }\n if ((src->meth->flags & EC_FLAGS_CUSTOM_CURVE) == 0) {\n if (!BN_copy(dest->order, src->order))\n return 0;\n if (!BN_copy(dest->cofactor, src->cofactor))\n return 0;\n }\n dest->curve_name = src->curve_name;\n dest->asn1_flag = src->asn1_flag;\n dest->asn1_form = src->asn1_form;\n if (src->seed) {\n OPENSSL_free(dest->seed);\n dest->seed = OPENSSL_malloc(src->seed_len);\n if (dest->seed == NULL)\n return 0;\n if (!memcpy(dest->seed, src->seed, src->seed_len))\n return 0;\n dest->seed_len = src->seed_len;\n } else {\n OPENSSL_free(dest->seed);\n dest->seed = NULL;\n dest->seed_len = 0;\n }\n return dest->meth->group_copy(dest, src);\n}', 'int EC_KEY_set_group(EC_KEY *key, const EC_GROUP *group)\n{\n if (key->meth->set_group != NULL && key->meth->set_group(key, group) == 0)\n return 0;\n EC_GROUP_free(key->group);\n key->group = EC_GROUP_dup(group);\n return (key->group == NULL) ? 0 : 1;\n}', 'BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)\n{\n bn_check_top(b);\n if (a == b)\n return a;\n if (bn_wexpand(a, b->top) == NULL)\n return NULL;\n if (b->top > 0)\n memcpy(a->d, b->d, sizeof(b->d[0]) * b->top);\n a->top = b->top;\n a->neg = b->neg;\n bn_check_top(a);\n return a;\n}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n return (words <= a->dmax) ? a : bn_expand2(a, words);\n}']
210
0
https://github.com/openssl/openssl/blob/9b02dc97e4963969da69675a871dbe80e6d31cda/crypto/bn/bn_lib.c/#L260
static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words) { BN_ULONG *a = NULL; bn_check_top(b); if (words > (INT_MAX / (4 * BN_BITS2))) { BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG); return NULL; } if (BN_get_flags(b, BN_FLG_STATIC_DATA)) { BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA); return NULL; } if (BN_get_flags(b, BN_FLG_SECURE)) a = OPENSSL_secure_zalloc(words * sizeof(*a)); else a = OPENSSL_zalloc(words * sizeof(*a)); if (a == NULL) { BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE); return NULL; } assert(b->top <= words); if (b->top > 0) memcpy(a, b->d, sizeof(*a) * b->top); return a; }
['int BN_is_prime_fasttest_ex(const BIGNUM *a, int checks, BN_CTX *ctx_passed,\n int do_trial_division, BN_GENCB *cb)\n{\n int i, j, ret = -1;\n int k;\n BN_CTX *ctx = NULL;\n BIGNUM *A1, *A1_odd, *check;\n BN_MONT_CTX *mont = NULL;\n if (BN_cmp(a, BN_value_one()) <= 0)\n return 0;\n if (checks == BN_prime_checks)\n checks = BN_prime_checks_for_size(BN_num_bits(a));\n if (!BN_is_odd(a))\n return BN_is_word(a, 2);\n if (do_trial_division) {\n for (i = 1; i < NUMPRIMES; i++) {\n BN_ULONG mod = BN_mod_word(a, primes[i]);\n if (mod == (BN_ULONG)-1)\n goto err;\n if (mod == 0)\n return BN_is_word(a, primes[i]);\n }\n if (!BN_GENCB_call(cb, 1, -1))\n goto err;\n }\n if (ctx_passed != NULL)\n ctx = ctx_passed;\n else if ((ctx = BN_CTX_new()) == NULL)\n goto err;\n BN_CTX_start(ctx);\n A1 = BN_CTX_get(ctx);\n A1_odd = BN_CTX_get(ctx);\n check = BN_CTX_get(ctx);\n if (check == NULL)\n goto err;\n if (!BN_copy(A1, a))\n goto err;\n if (!BN_sub_word(A1, 1))\n goto err;\n if (BN_is_zero(A1)) {\n ret = 0;\n goto err;\n }\n k = 1;\n while (!BN_is_bit_set(A1, k))\n k++;\n if (!BN_rshift(A1_odd, A1, k))\n goto err;\n mont = BN_MONT_CTX_new();\n if (mont == NULL)\n goto err;\n if (!BN_MONT_CTX_set(mont, a, ctx))\n goto err;\n for (i = 0; i < checks; i++) {\n if (!BN_priv_rand_range(check, A1))\n goto err;\n if (!BN_add_word(check, 1))\n goto err;\n j = witness(check, a, A1, A1_odd, k, ctx, mont);\n if (j == -1)\n goto err;\n if (j) {\n ret = 0;\n goto err;\n }\n if (!BN_GENCB_call(cb, 1, i))\n goto err;\n }\n ret = 1;\n err:\n if (ctx != NULL) {\n BN_CTX_end(ctx);\n if (ctx_passed == NULL)\n BN_CTX_free(ctx);\n }\n BN_MONT_CTX_free(mont);\n return ret;\n}', '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}', 'int BN_priv_rand_range(BIGNUM *r, const BIGNUM *range)\n{\n return bnrand_range(PRIVATE, r, range);\n}', 'static int bnrand_range(BNRAND_FLAG flag, BIGNUM *r, const BIGNUM *range)\n{\n int b, n;\n int count = 100;\n if (range->neg || BN_is_zero(range)) {\n BNerr(BN_F_BNRAND_RANGE, BN_R_INVALID_RANGE);\n return 0;\n }\n n = BN_num_bits(range);\n if (n == 1)\n BN_zero(r);\n else if (!BN_is_bit_set(range, n - 2) && !BN_is_bit_set(range, n - 3)) {\n do {\n b = flag == NORMAL\n ? BN_rand(r, n + 1, BN_RAND_TOP_ANY, BN_RAND_BOTTOM_ANY)\n : BN_priv_rand(r, n + 1, BN_RAND_TOP_ANY, BN_RAND_BOTTOM_ANY);\n if (!b)\n return 0;\n if (BN_cmp(r, range) >= 0) {\n if (!BN_sub(r, r, range))\n return 0;\n if (BN_cmp(r, range) >= 0)\n if (!BN_sub(r, r, range))\n return 0;\n }\n if (!--count) {\n BNerr(BN_F_BNRAND_RANGE, BN_R_TOO_MANY_ITERATIONS);\n return 0;\n }\n }\n while (BN_cmp(r, range) >= 0);\n } else {\n do {\n if (!BN_rand(r, n, BN_RAND_TOP_ANY, BN_RAND_BOTTOM_ANY))\n return 0;\n if (!--count) {\n BNerr(BN_F_BNRAND_RANGE, BN_R_TOO_MANY_ITERATIONS);\n return 0;\n }\n }\n while (BN_cmp(r, range) >= 0);\n }\n bn_check_top(r);\n return 1;\n}', 'int BN_set_word(BIGNUM *a, BN_ULONG w)\n{\n bn_check_top(a);\n if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)\n return 0;\n a->neg = 0;\n a->d[0] = w;\n a->top = (w ? 1 : 0);\n bn_check_top(a);\n return 1;\n}', 'static ossl_inline BIGNUM *bn_expand(BIGNUM *a, int bits)\n{\n if (bits > (INT_MAX - BN_BITS2 + 1))\n return NULL;\n if (((bits+BN_BITS2-1)/BN_BITS2) <= (a)->dmax)\n return a;\n return bn_expand2((a),(bits+BN_BITS2-1)/BN_BITS2);\n}', 'BIGNUM *bn_expand2(BIGNUM *b, int words)\n{\n bn_check_top(b);\n if (words > b->dmax) {\n BN_ULONG *a = bn_expand_internal(b, words);\n if (!a)\n return NULL;\n if (b->d) {\n OPENSSL_cleanse(b->d, b->dmax * sizeof(b->d[0]));\n bn_free_d(b);\n }\n b->d = a;\n b->dmax = words;\n }\n bn_check_top(b);\n return b;\n}', 'static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)\n{\n BN_ULONG *a = NULL;\n bn_check_top(b);\n if (words > (INT_MAX / (4 * BN_BITS2))) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);\n return NULL;\n }\n if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);\n return NULL;\n }\n if (BN_get_flags(b, BN_FLG_SECURE))\n a = OPENSSL_secure_zalloc(words * sizeof(*a));\n else\n a = OPENSSL_zalloc(words * sizeof(*a));\n if (a == NULL) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);\n return NULL;\n }\n assert(b->top <= words);\n if (b->top > 0)\n memcpy(a, b->d, sizeof(*a) * b->top);\n return a;\n}', 'void *CRYPTO_zalloc(size_t num, const char *file, int line)\n{\n void *ret = CRYPTO_malloc(num, file, line);\n FAILTEST();\n if (ret != NULL)\n memset(ret, 0, num);\n return ret;\n}', 'void *CRYPTO_malloc(size_t num, const char *file, int line)\n{\n void *ret = NULL;\n 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}']
211
0
https://github.com/openssl/openssl/blob/5d1c09de1f2736e1d4b1877206d08455ec75f558/crypto/bn/bn_lib.c/#L232
static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words) { BN_ULONG *a = NULL; if (words > (INT_MAX / (4 * BN_BITS2))) { BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG); return NULL; } if (BN_get_flags(b, BN_FLG_STATIC_DATA)) { BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA); return NULL; } if (BN_get_flags(b, BN_FLG_SECURE)) a = OPENSSL_secure_zalloc(words * sizeof(*a)); else a = OPENSSL_zalloc(words * sizeof(*a)); if (a == NULL) { BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE); return NULL; } assert(b->top <= words); if (b->top > 0) memcpy(a, b->d, sizeof(*a) * b->top); return a; }
['static int ec_precompute_mont_data(EC_GROUP *group)\n{\n BN_CTX *ctx = BN_CTX_new();\n int ret = 0;\n BN_MONT_CTX_free(group->mont_data);\n group->mont_data = NULL;\n if (ctx == NULL)\n goto err;\n group->mont_data = BN_MONT_CTX_new();\n if (group->mont_data == NULL)\n goto err;\n if (!BN_MONT_CTX_set(group->mont_data, group->order, ctx)) {\n BN_MONT_CTX_free(group->mont_data);\n group->mont_data = NULL;\n goto err;\n }\n ret = 1;\n err:\n BN_CTX_free(ctx);\n return ret;\n}', 'int BN_MONT_CTX_set(BN_MONT_CTX *mont, const BIGNUM *mod, BN_CTX *ctx)\n{\n int i, ret = 0;\n BIGNUM *Ri, *R;\n if (BN_is_zero(mod))\n return 0;\n BN_CTX_start(ctx);\n if ((Ri = BN_CTX_get(ctx)) == NULL)\n goto err;\n R = &(mont->RR);\n if (!BN_copy(&(mont->N), mod))\n goto err;\n if (BN_get_flags(mod, BN_FLG_CONSTTIME) != 0)\n BN_set_flags(&(mont->N), BN_FLG_CONSTTIME);\n mont->N.neg = 0;\n#ifdef MONT_WORD\n {\n BIGNUM tmod;\n BN_ULONG buf[2];\n bn_init(&tmod);\n tmod.d = buf;\n tmod.dmax = 2;\n tmod.neg = 0;\n if (BN_get_flags(mod, BN_FLG_CONSTTIME) != 0)\n BN_set_flags(&tmod, BN_FLG_CONSTTIME);\n mont->ri = (BN_num_bits(mod) + (BN_BITS2 - 1)) / BN_BITS2 * BN_BITS2;\n# if defined(OPENSSL_BN_ASM_MONT) && (BN_BITS2<=32)\n BN_zero(R);\n if (!(BN_set_bit(R, 2 * BN_BITS2)))\n goto err;\n tmod.top = 0;\n if ((buf[0] = mod->d[0]))\n tmod.top = 1;\n if ((buf[1] = mod->top > 1 ? mod->d[1] : 0))\n tmod.top = 2;\n if (BN_is_one(&tmod))\n BN_zero(Ri);\n else if ((BN_mod_inverse(Ri, R, &tmod, ctx)) == NULL)\n goto err;\n if (!BN_lshift(Ri, Ri, 2 * BN_BITS2))\n goto err;\n if (!BN_is_zero(Ri)) {\n if (!BN_sub_word(Ri, 1))\n goto err;\n } else {\n if (bn_expand(Ri, (int)sizeof(BN_ULONG) * 2) == NULL)\n goto err;\n Ri->neg = 0;\n Ri->d[0] = BN_MASK2;\n Ri->d[1] = BN_MASK2;\n Ri->top = 2;\n }\n if (!BN_div(Ri, NULL, Ri, &tmod, ctx))\n goto err;\n mont->n0[0] = (Ri->top > 0) ? Ri->d[0] : 0;\n mont->n0[1] = (Ri->top > 1) ? Ri->d[1] : 0;\n# else\n BN_zero(R);\n if (!(BN_set_bit(R, BN_BITS2)))\n goto err;\n buf[0] = mod->d[0];\n buf[1] = 0;\n tmod.top = buf[0] != 0 ? 1 : 0;\n if (BN_is_one(&tmod))\n BN_zero(Ri);\n else if ((BN_mod_inverse(Ri, R, &tmod, ctx)) == NULL)\n goto err;\n if (!BN_lshift(Ri, Ri, BN_BITS2))\n goto err;\n if (!BN_is_zero(Ri)) {\n if (!BN_sub_word(Ri, 1))\n goto err;\n } else {\n if (!BN_set_word(Ri, BN_MASK2))\n goto err;\n }\n if (!BN_div(Ri, NULL, Ri, &tmod, ctx))\n goto err;\n mont->n0[0] = (Ri->top > 0) ? Ri->d[0] : 0;\n mont->n0[1] = 0;\n# endif\n }\n#else\n {\n mont->ri = BN_num_bits(&mont->N);\n BN_zero(R);\n if (!BN_set_bit(R, mont->ri))\n goto err;\n if ((BN_mod_inverse(Ri, R, &mont->N, ctx)) == NULL)\n goto err;\n if (!BN_lshift(Ri, Ri, mont->ri))\n goto err;\n if (!BN_sub_word(Ri, 1))\n goto err;\n if (!BN_div(&(mont->Ni), NULL, Ri, &mont->N, ctx))\n goto err;\n }\n#endif\n BN_zero(&(mont->RR));\n if (!BN_set_bit(&(mont->RR), mont->ri * 2))\n goto err;\n if (!BN_mod(&(mont->RR), &(mont->RR), &(mont->N), ctx))\n goto err;\n for (i = mont->RR.top, ret = mont->N.top; i < ret; i++)\n mont->RR.d[i] = 0;\n mont->RR.top = ret;\n mont->RR.flags |= BN_FLG_FIXED_TOP;\n ret = 1;\n err:\n BN_CTX_end(ctx);\n return ret;\n}', 'BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)\n{\n bn_check_top(b);\n if (a == b)\n return a;\n if (bn_wexpand(a, b->top) == NULL)\n return NULL;\n if (b->top > 0)\n memcpy(a->d, b->d, sizeof(b->d[0]) * b->top);\n a->neg = b->neg;\n a->top = b->top;\n a->flags |= b->flags & BN_FLG_FIXED_TOP;\n bn_check_top(a);\n return a;\n}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n return (words <= a->dmax) ? a : bn_expand2(a, words);\n}', '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}']
212
0
https://github.com/openssl/openssl/blob/305b68f1a2b6d4d0aa07a6ab47ac372f067a40bb/crypto/bn/bn_sqr.c/#L114
void bn_sqr_normal(BN_ULONG *r, const BN_ULONG *a, int n, BN_ULONG *tmp) { int i, j, max; const BN_ULONG *ap; BN_ULONG *rp; max = n * 2; ap = a; rp = r; rp[0] = rp[max - 1] = 0; rp++; j = n; if (--j > 0) { ap++; rp[j] = bn_mul_words(rp, ap, j, ap[-1]); rp += 2; } for (i = n - 2; i > 0; i--) { j--; ap++; rp[j] = bn_mul_add_words(rp, ap, j, ap[-1]); rp += 2; } bn_add_words(r, r, r, max); bn_sqr_words(tmp, a, n); bn_add_words(r, r, tmp, max); }
['static int test_mod_exp_zero(void)\n{\n BIGNUM *a = NULL, *p = NULL, *m = NULL;\n BIGNUM *r = NULL;\n BN_ULONG one_word = 1;\n BN_CTX *ctx = BN_CTX_new();\n int ret = 1, failed = 0;\n if (!TEST_ptr(m = BN_new())\n || !TEST_ptr(a = BN_new())\n || !TEST_ptr(p = BN_new())\n || !TEST_ptr(r = BN_new()))\n goto err;\n BN_one(m);\n BN_one(a);\n BN_zero(p);\n if (!TEST_true(BN_rand(a, 1024, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY)))\n goto err;\n if (!TEST_true(BN_mod_exp(r, a, p, m, ctx)))\n goto err;\n if (!TEST_true(a_is_zero_mod_one("BN_mod_exp", r, a)))\n failed = 1;\n if (!TEST_true(BN_mod_exp_recp(r, a, p, m, ctx)))\n goto err;\n if (!TEST_true(a_is_zero_mod_one("BN_mod_exp_recp", r, a)))\n failed = 1;\n if (!TEST_true(BN_mod_exp_simple(r, a, p, m, ctx)))\n goto err;\n if (!TEST_true(a_is_zero_mod_one("BN_mod_exp_simple", r, a)))\n failed = 1;\n if (!TEST_true(BN_mod_exp_mont(r, a, p, m, ctx, NULL)))\n goto err;\n if (!TEST_true(a_is_zero_mod_one("BN_mod_exp_mont", r, a)))\n failed = 1;\n if (!TEST_true(BN_mod_exp_mont_consttime(r, a, p, m, ctx, NULL)))\n goto err;\n if (!TEST_true(a_is_zero_mod_one("BN_mod_exp_mont_consttime", r, a)))\n failed = 1;\n if (!TEST_true(BN_mod_exp_mont_word(r, one_word, p, m, ctx, NULL)))\n goto err;\n if (!TEST_BN_eq_zero(r)) {\n TEST_error("BN_mod_exp_mont_word failed: "\n "1 ** 0 mod 1 = r (should be 0)");\n BN_print_var(r);\n goto err;\n }\n ret = !failed;\n err:\n BN_free(r);\n BN_free(a);\n BN_free(p);\n BN_free(m);\n BN_CTX_free(ctx);\n return ret;\n}', 'int BN_set_word(BIGNUM *a, BN_ULONG w)\n{\n bn_check_top(a);\n if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)\n return 0;\n a->neg = 0;\n a->d[0] = w;\n a->top = (w ? 1 : 0);\n a->flags &= ~BN_FLG_FIXED_TOP;\n bn_check_top(a);\n return 1;\n}', 'int BN_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m,\n BN_CTX *ctx)\n{\n int ret;\n bn_check_top(a);\n bn_check_top(p);\n bn_check_top(m);\n#define MONT_MUL_MOD\n#define MONT_EXP_WORD\n#define RECP_MUL_MOD\n#ifdef MONT_MUL_MOD\n if (BN_is_odd(m)) {\n# ifdef MONT_EXP_WORD\n if (a->top == 1 && !a->neg\n && (BN_get_flags(p, BN_FLG_CONSTTIME) == 0)\n && (BN_get_flags(a, BN_FLG_CONSTTIME) == 0)\n && (BN_get_flags(m, BN_FLG_CONSTTIME) == 0)) {\n BN_ULONG A = a->d[0];\n ret = BN_mod_exp_mont_word(r, A, p, m, ctx, NULL);\n } else\n# endif\n ret = BN_mod_exp_mont(r, a, p, m, ctx, NULL);\n } else\n#endif\n#ifdef RECP_MUL_MOD\n {\n ret = BN_mod_exp_recp(r, a, p, m, ctx);\n }\n#else\n {\n ret = BN_mod_exp_simple(r, a, p, m, ctx);\n }\n#endif\n bn_check_top(r);\n return ret;\n}', 'int BN_mod_exp_mont(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p,\n const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont)\n{\n int i, j, bits, ret = 0, wstart, wend, window, wvalue;\n int start = 1;\n BIGNUM *d, *r;\n const BIGNUM *aa;\n BIGNUM *val[TABLE_SIZE];\n BN_MONT_CTX *mont = NULL;\n if (BN_get_flags(p, BN_FLG_CONSTTIME) != 0\n || BN_get_flags(a, BN_FLG_CONSTTIME) != 0\n || BN_get_flags(m, BN_FLG_CONSTTIME) != 0) {\n return BN_mod_exp_mont_consttime(rr, a, p, m, ctx, in_mont);\n }\n bn_check_top(a);\n bn_check_top(p);\n bn_check_top(m);\n if (!BN_is_odd(m)) {\n BNerr(BN_F_BN_MOD_EXP_MONT, BN_R_CALLED_WITH_EVEN_MODULUS);\n return 0;\n }\n bits = BN_num_bits(p);\n if (bits == 0) {\n if (BN_abs_is_word(m, 1)) {\n ret = 1;\n BN_zero(rr);\n } else {\n ret = BN_one(rr);\n }\n return ret;\n }\n BN_CTX_start(ctx);\n d = BN_CTX_get(ctx);\n r = BN_CTX_get(ctx);\n val[0] = BN_CTX_get(ctx);\n if (val[0] == NULL)\n goto err;\n if (in_mont != NULL)\n mont = in_mont;\n else {\n if ((mont = BN_MONT_CTX_new()) == NULL)\n goto err;\n if (!BN_MONT_CTX_set(mont, m, ctx))\n goto err;\n }\n if (a->neg || BN_ucmp(a, m) >= 0) {\n if (!BN_nnmod(val[0], a, m, ctx))\n goto err;\n aa = val[0];\n } else\n aa = a;\n if (BN_is_zero(aa)) {\n BN_zero(rr);\n ret = 1;\n goto err;\n }\n if (!BN_to_montgomery(val[0], aa, mont, ctx))\n goto err;\n window = BN_window_bits_for_exponent_size(bits);\n if (window > 1) {\n if (!BN_mod_mul_montgomery(d, val[0], val[0], mont, ctx))\n goto err;\n j = 1 << (window - 1);\n for (i = 1; i < j; i++) {\n if (((val[i] = BN_CTX_get(ctx)) == NULL) ||\n !BN_mod_mul_montgomery(val[i], val[i - 1], d, mont, ctx))\n goto err;\n }\n }\n start = 1;\n wvalue = 0;\n wstart = bits - 1;\n wend = 0;\n#if 1\n j = m->top;\n if (m->d[j - 1] & (((BN_ULONG)1) << (BN_BITS2 - 1))) {\n if (bn_wexpand(r, j) == NULL)\n goto err;\n r->d[0] = (0 - m->d[0]) & BN_MASK2;\n for (i = 1; i < j; i++)\n r->d[i] = (~m->d[i]) & BN_MASK2;\n r->top = j;\n bn_correct_top(r);\n } else\n#endif\n if (!BN_to_montgomery(r, BN_value_one(), mont, ctx))\n goto err;\n for (;;) {\n if (BN_is_bit_set(p, wstart) == 0) {\n if (!start) {\n if (!BN_mod_mul_montgomery(r, r, r, mont, ctx))\n goto err;\n }\n if (wstart == 0)\n break;\n wstart--;\n continue;\n }\n j = wstart;\n wvalue = 1;\n wend = 0;\n for (i = 1; i < window; i++) {\n if (wstart - i < 0)\n break;\n if (BN_is_bit_set(p, wstart - i)) {\n wvalue <<= (i - wend);\n wvalue |= 1;\n wend = i;\n }\n }\n j = wend + 1;\n if (!start)\n for (i = 0; i < j; i++) {\n if (!BN_mod_mul_montgomery(r, r, r, mont, ctx))\n goto err;\n }\n if (!BN_mod_mul_montgomery(r, r, val[wvalue >> 1], mont, ctx))\n goto err;\n wstart -= wend + 1;\n wvalue = 0;\n start = 0;\n if (wstart < 0)\n break;\n }\n#if defined(SPARC_T4_MONT)\n if (OPENSSL_sparcv9cap_P[0] & (SPARCV9_VIS3 | SPARCV9_PREFER_FPU)) {\n j = mont->N.top;\n val[0]->d[0] = 1;\n for (i = 1; i < j; i++)\n val[0]->d[i] = 0;\n val[0]->top = j;\n if (!BN_mod_mul_montgomery(rr, r, val[0], mont, ctx))\n goto err;\n } else\n#endif\n if (!BN_from_montgomery(rr, r, mont, ctx))\n goto err;\n ret = 1;\n err:\n if (in_mont == NULL)\n BN_MONT_CTX_free(mont);\n BN_CTX_end(ctx);\n bn_check_top(rr);\n return ret;\n}', 'int BN_mod_exp_mont_consttime(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p,\n const BIGNUM *m, BN_CTX *ctx,\n BN_MONT_CTX *in_mont)\n{\n int i, bits, ret = 0, window, wvalue, wmask, window0;\n int top;\n BN_MONT_CTX *mont = NULL;\n int numPowers;\n unsigned char *powerbufFree = NULL;\n int powerbufLen = 0;\n unsigned char *powerbuf = NULL;\n BIGNUM tmp, am;\n#if defined(SPARC_T4_MONT)\n unsigned int t4 = 0;\n#endif\n bn_check_top(a);\n bn_check_top(p);\n bn_check_top(m);\n if (!BN_is_odd(m)) {\n BNerr(BN_F_BN_MOD_EXP_MONT_CONSTTIME, BN_R_CALLED_WITH_EVEN_MODULUS);\n return 0;\n }\n top = m->top;\n bits = p->top * BN_BITS2;\n if (bits == 0) {\n if (BN_abs_is_word(m, 1)) {\n ret = 1;\n BN_zero(rr);\n } else {\n ret = BN_one(rr);\n }\n return ret;\n }\n BN_CTX_start(ctx);\n if (in_mont != NULL)\n mont = in_mont;\n else {\n if ((mont = BN_MONT_CTX_new()) == NULL)\n goto err;\n if (!BN_MONT_CTX_set(mont, m, ctx))\n goto err;\n }\n#ifdef RSAZ_ENABLED\n if (!a->neg) {\n if ((16 == a->top) && (16 == p->top) && (BN_num_bits(m) == 1024)\n && rsaz_avx2_eligible()) {\n if (NULL == bn_wexpand(rr, 16))\n goto err;\n RSAZ_1024_mod_exp_avx2(rr->d, a->d, p->d, m->d, mont->RR.d,\n mont->n0[0]);\n rr->top = 16;\n rr->neg = 0;\n bn_correct_top(rr);\n ret = 1;\n goto err;\n } else if ((8 == a->top) && (8 == p->top) && (BN_num_bits(m) == 512)) {\n if (NULL == bn_wexpand(rr, 8))\n goto err;\n RSAZ_512_mod_exp(rr->d, a->d, p->d, m->d, mont->n0[0], mont->RR.d);\n rr->top = 8;\n rr->neg = 0;\n bn_correct_top(rr);\n ret = 1;\n goto err;\n }\n }\n#endif\n window = BN_window_bits_for_ctime_exponent_size(bits);\n#if defined(SPARC_T4_MONT)\n if (window >= 5 && (top & 15) == 0 && top <= 64 &&\n (OPENSSL_sparcv9cap_P[1] & (CFR_MONTMUL | CFR_MONTSQR)) ==\n (CFR_MONTMUL | CFR_MONTSQR) && (t4 = OPENSSL_sparcv9cap_P[0]))\n window = 5;\n else\n#endif\n#if defined(OPENSSL_BN_ASM_MONT5)\n if (window >= 5) {\n window = 5;\n powerbufLen += top * sizeof(mont->N.d[0]);\n }\n#endif\n (void)0;\n numPowers = 1 << window;\n powerbufLen += sizeof(m->d[0]) * (top * numPowers +\n ((2 * top) >\n numPowers ? (2 * top) : numPowers));\n#ifdef alloca\n if (powerbufLen < 3072)\n powerbufFree =\n alloca(powerbufLen + MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH);\n else\n#endif\n if ((powerbufFree =\n OPENSSL_malloc(powerbufLen + MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH))\n == NULL)\n goto err;\n powerbuf = MOD_EXP_CTIME_ALIGN(powerbufFree);\n memset(powerbuf, 0, powerbufLen);\n#ifdef alloca\n if (powerbufLen < 3072)\n powerbufFree = NULL;\n#endif\n tmp.d = (BN_ULONG *)(powerbuf + sizeof(m->d[0]) * top * numPowers);\n am.d = tmp.d + top;\n tmp.top = am.top = 0;\n tmp.dmax = am.dmax = top;\n tmp.neg = am.neg = 0;\n tmp.flags = am.flags = BN_FLG_STATIC_DATA;\n#if 1\n if (m->d[top - 1] & (((BN_ULONG)1) << (BN_BITS2 - 1))) {\n tmp.d[0] = (0 - m->d[0]) & BN_MASK2;\n for (i = 1; i < top; i++)\n tmp.d[i] = (~m->d[i]) & BN_MASK2;\n tmp.top = top;\n } else\n#endif\n if (!BN_to_montgomery(&tmp, BN_value_one(), mont, ctx))\n goto err;\n if (a->neg || BN_ucmp(a, m) >= 0) {\n if (!BN_nnmod(&am, a, m, ctx))\n goto err;\n if (!BN_to_montgomery(&am, &am, mont, ctx))\n goto err;\n } else if (!BN_to_montgomery(&am, a, mont, ctx))\n goto err;\n#if defined(SPARC_T4_MONT)\n if (t4) {\n typedef int (*bn_pwr5_mont_f) (BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n int bn_pwr5_mont_t4_8(BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n int bn_pwr5_mont_t4_16(BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n int bn_pwr5_mont_t4_24(BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n int bn_pwr5_mont_t4_32(BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n static const bn_pwr5_mont_f pwr5_funcs[4] = {\n bn_pwr5_mont_t4_8, bn_pwr5_mont_t4_16,\n bn_pwr5_mont_t4_24, bn_pwr5_mont_t4_32\n };\n bn_pwr5_mont_f pwr5_worker = pwr5_funcs[top / 16 - 1];\n typedef int (*bn_mul_mont_f) (BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0);\n int bn_mul_mont_t4_8(BN_ULONG *rp, const BN_ULONG *ap, const void *bp,\n const BN_ULONG *np, const BN_ULONG *n0);\n int bn_mul_mont_t4_16(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0);\n int bn_mul_mont_t4_24(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0);\n int bn_mul_mont_t4_32(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0);\n static const bn_mul_mont_f mul_funcs[4] = {\n bn_mul_mont_t4_8, bn_mul_mont_t4_16,\n bn_mul_mont_t4_24, bn_mul_mont_t4_32\n };\n bn_mul_mont_f mul_worker = mul_funcs[top / 16 - 1];\n void bn_mul_mont_vis3(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0, int num);\n void bn_mul_mont_t4(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0, int num);\n void bn_mul_mont_gather5_t4(BN_ULONG *rp, const BN_ULONG *ap,\n const void *table, const BN_ULONG *np,\n const BN_ULONG *n0, int num, int power);\n void bn_flip_n_scatter5_t4(const BN_ULONG *inp, size_t num,\n void *table, size_t power);\n void bn_gather5_t4(BN_ULONG *out, size_t num,\n void *table, size_t power);\n void bn_flip_t4(BN_ULONG *dst, BN_ULONG *src, size_t num);\n BN_ULONG *np = mont->N.d, *n0 = mont->n0;\n int stride = 5 * (6 - (top / 16 - 1));\n for (i = am.top; i < top; i++)\n am.d[i] = 0;\n for (i = tmp.top; i < top; i++)\n tmp.d[i] = 0;\n bn_flip_n_scatter5_t4(tmp.d, top, powerbuf, 0);\n bn_flip_n_scatter5_t4(am.d, top, powerbuf, 1);\n if (!(*mul_worker) (tmp.d, am.d, am.d, np, n0) &&\n !(*mul_worker) (tmp.d, am.d, am.d, np, n0))\n bn_mul_mont_vis3(tmp.d, am.d, am.d, np, n0, top);\n bn_flip_n_scatter5_t4(tmp.d, top, powerbuf, 2);\n for (i = 3; i < 32; i++) {\n if (!(*mul_worker) (tmp.d, tmp.d, am.d, np, n0) &&\n !(*mul_worker) (tmp.d, tmp.d, am.d, np, n0))\n bn_mul_mont_vis3(tmp.d, tmp.d, am.d, np, n0, top);\n bn_flip_n_scatter5_t4(tmp.d, top, powerbuf, i);\n }\n np = alloca(top * sizeof(BN_ULONG));\n top /= 2;\n bn_flip_t4(np, mont->N.d, top);\n window0 = (bits - 1) % 5 + 1;\n wmask = (1 << window0) - 1;\n bits -= window0;\n wvalue = bn_get_bits(p, bits) & wmask;\n bn_gather5_t4(tmp.d, top, powerbuf, wvalue);\n while (bits > 0) {\n if (bits < stride)\n stride = bits;\n bits -= stride;\n wvalue = bn_get_bits(p, bits);\n if ((*pwr5_worker) (tmp.d, np, n0, powerbuf, wvalue, stride))\n continue;\n if ((*pwr5_worker) (tmp.d, np, n0, powerbuf, wvalue, stride))\n continue;\n bits += stride - 5;\n wvalue >>= stride - 5;\n wvalue &= 31;\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_gather5_t4(tmp.d, tmp.d, powerbuf, np, n0, top,\n wvalue);\n }\n bn_flip_t4(tmp.d, tmp.d, top);\n top *= 2;\n tmp.top = top;\n bn_correct_top(&tmp);\n OPENSSL_cleanse(np, top * sizeof(BN_ULONG));\n } else\n#endif\n#if defined(OPENSSL_BN_ASM_MONT5)\n if (window == 5 && top > 1) {\n void bn_mul_mont_gather5(BN_ULONG *rp, const BN_ULONG *ap,\n const void *table, const BN_ULONG *np,\n const BN_ULONG *n0, int num, int power);\n void bn_scatter5(const BN_ULONG *inp, size_t num,\n void *table, size_t power);\n void bn_gather5(BN_ULONG *out, size_t num, void *table, size_t power);\n void bn_power5(BN_ULONG *rp, const BN_ULONG *ap,\n const void *table, const BN_ULONG *np,\n const BN_ULONG *n0, int num, int power);\n int bn_get_bits5(const BN_ULONG *ap, int off);\n int bn_from_montgomery(BN_ULONG *rp, const BN_ULONG *ap,\n const BN_ULONG *not_used, const BN_ULONG *np,\n const BN_ULONG *n0, int num);\n BN_ULONG *n0 = mont->n0, *np;\n for (i = am.top; i < top; i++)\n am.d[i] = 0;\n for (i = tmp.top; i < top; i++)\n tmp.d[i] = 0;\n for (np = am.d + top, i = 0; i < top; i++)\n np[i] = mont->N.d[i];\n bn_scatter5(tmp.d, top, powerbuf, 0);\n bn_scatter5(am.d, am.top, powerbuf, 1);\n bn_mul_mont(tmp.d, am.d, am.d, np, n0, top);\n bn_scatter5(tmp.d, top, powerbuf, 2);\n# if 0\n for (i = 3; i < 32; i++) {\n bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1);\n bn_scatter5(tmp.d, top, powerbuf, i);\n }\n# else\n for (i = 4; i < 32; i *= 2) {\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_scatter5(tmp.d, top, powerbuf, i);\n }\n for (i = 3; i < 8; i += 2) {\n int j;\n bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1);\n bn_scatter5(tmp.d, top, powerbuf, i);\n for (j = 2 * i; j < 32; j *= 2) {\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_scatter5(tmp.d, top, powerbuf, j);\n }\n }\n for (; i < 16; i += 2) {\n bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1);\n bn_scatter5(tmp.d, top, powerbuf, i);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_scatter5(tmp.d, top, powerbuf, 2 * i);\n }\n for (; i < 32; i += 2) {\n bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1);\n bn_scatter5(tmp.d, top, powerbuf, i);\n }\n# endif\n window0 = (bits - 1) % 5 + 1;\n wmask = (1 << window0) - 1;\n bits -= window0;\n wvalue = bn_get_bits(p, bits) & wmask;\n bn_gather5(tmp.d, top, powerbuf, wvalue);\n if (top & 7) {\n while (bits > 0) {\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_gather5(tmp.d, tmp.d, powerbuf, np, n0, top,\n bn_get_bits5(p->d, bits -= 5));\n }\n } else {\n while (bits > 0) {\n bn_power5(tmp.d, tmp.d, powerbuf, np, n0, top,\n bn_get_bits5(p->d, bits -= 5));\n }\n }\n ret = bn_from_montgomery(tmp.d, tmp.d, NULL, np, n0, top);\n tmp.top = top;\n bn_correct_top(&tmp);\n if (ret) {\n if (!BN_copy(rr, &tmp))\n ret = 0;\n goto err;\n }\n } else\n#endif\n {\n if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&tmp, top, powerbuf, 0, window))\n goto err;\n if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&am, top, powerbuf, 1, window))\n goto err;\n if (window > 1) {\n if (!BN_mod_mul_montgomery(&tmp, &am, &am, mont, ctx))\n goto err;\n if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&tmp, top, powerbuf, 2,\n window))\n goto err;\n for (i = 3; i < numPowers; i++) {\n if (!BN_mod_mul_montgomery(&tmp, &am, &tmp, mont, ctx))\n goto err;\n if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&tmp, top, powerbuf, i,\n window))\n goto err;\n }\n }\n window0 = (bits - 1) % window + 1;\n wmask = (1 << window0) - 1;\n bits -= window0;\n wvalue = bn_get_bits(p, bits) & wmask;\n if (!MOD_EXP_CTIME_COPY_FROM_PREBUF(&tmp, top, powerbuf, wvalue,\n window))\n goto err;\n wmask = (1 << window) - 1;\n while (bits > 0) {\n for (i = 0; i < window; i++)\n if (!BN_mod_mul_montgomery(&tmp, &tmp, &tmp, mont, ctx))\n goto err;\n bits -= window;\n wvalue = bn_get_bits(p, bits) & wmask;\n if (!MOD_EXP_CTIME_COPY_FROM_PREBUF(&am, top, powerbuf, wvalue,\n window))\n goto err;\n if (!BN_mod_mul_montgomery(&tmp, &tmp, &am, mont, ctx))\n goto err;\n }\n }\n#if defined(SPARC_T4_MONT)\n if (OPENSSL_sparcv9cap_P[0] & (SPARCV9_VIS3 | SPARCV9_PREFER_FPU)) {\n am.d[0] = 1;\n for (i = 1; i < top; i++)\n am.d[i] = 0;\n if (!BN_mod_mul_montgomery(rr, &tmp, &am, mont, ctx))\n goto err;\n } else\n#endif\n if (!BN_from_montgomery(rr, &tmp, mont, ctx))\n goto err;\n ret = 1;\n err:\n if (in_mont == NULL)\n BN_MONT_CTX_free(mont);\n if (powerbuf != NULL) {\n OPENSSL_cleanse(powerbuf, powerbufLen);\n OPENSSL_free(powerbufFree);\n }\n BN_CTX_end(ctx);\n return ret;\n}', 'int BN_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 int num = mont->N.top;\n#if defined(OPENSSL_BN_ASM_MONT) && defined(MONT_WORD)\n if (num > 1 && a->top == num && b->top == num) {\n if (bn_wexpand(r, num) == NULL)\n return 0;\n if (bn_mul_mont(r->d, a->d, b->d, mont->N.d, mont->n0, num)) {\n r->neg = a->neg ^ b->neg;\n r->top = num;\n bn_correct_top(r);\n return 1;\n }\n }\n#endif\n if ((a->top + b->top) > 2 * num)\n return 0;\n BN_CTX_start(ctx);\n tmp = BN_CTX_get(ctx);\n if (tmp == NULL)\n goto err;\n bn_check_top(tmp);\n if (a == b) {\n if (!BN_sqr(tmp, a, ctx))\n goto err;\n } else {\n if (!BN_mul(tmp, a, b, ctx))\n goto err;\n }\n#ifdef MONT_WORD\n if (!BN_from_montgomery_word(r, tmp, mont))\n goto err;\n#else\n if (!BN_from_montgomery(r, tmp, mont, ctx))\n goto err;\n#endif\n bn_check_top(r);\n ret = 1;\n err:\n BN_CTX_end(ctx);\n return ret;\n}', 'int BN_sqr(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx)\n{\n int max, al;\n int ret = 0;\n BIGNUM *tmp, *rr;\n bn_check_top(a);\n al = a->top;\n if (al <= 0) {\n r->top = 0;\n r->neg = 0;\n return 1;\n }\n BN_CTX_start(ctx);\n rr = (a != r) ? r : BN_CTX_get(ctx);\n tmp = BN_CTX_get(ctx);\n if (rr == NULL || tmp == NULL)\n goto err;\n max = 2 * al;\n if (bn_wexpand(rr, max) == NULL)\n goto err;\n if (al == 4) {\n#ifndef BN_SQR_COMBA\n BN_ULONG t[8];\n bn_sqr_normal(rr->d, a->d, 4, t);\n#else\n bn_sqr_comba4(rr->d, a->d);\n#endif\n } else if (al == 8) {\n#ifndef BN_SQR_COMBA\n BN_ULONG t[16];\n bn_sqr_normal(rr->d, a->d, 8, t);\n#else\n bn_sqr_comba8(rr->d, a->d);\n#endif\n } else {\n#if defined(BN_RECURSION)\n if (al < BN_SQR_RECURSIVE_SIZE_NORMAL) {\n BN_ULONG t[BN_SQR_RECURSIVE_SIZE_NORMAL * 2];\n bn_sqr_normal(rr->d, a->d, al, t);\n } else {\n int j, k;\n j = BN_num_bits_word((BN_ULONG)al);\n j = 1 << (j - 1);\n k = j + j;\n if (al == j) {\n if (bn_wexpand(tmp, k * 2) == NULL)\n goto err;\n bn_sqr_recursive(rr->d, a->d, al, tmp->d);\n } else {\n if (bn_wexpand(tmp, max) == NULL)\n goto err;\n bn_sqr_normal(rr->d, a->d, al, tmp->d);\n }\n }\n#else\n if (bn_wexpand(tmp, max) == NULL)\n goto err;\n bn_sqr_normal(rr->d, a->d, al, tmp->d);\n#endif\n }\n rr->neg = 0;\n rr->top = max;\n bn_correct_top(rr);\n if (r != rr && BN_copy(r, rr) == NULL)\n goto err;\n ret = 1;\n err:\n bn_check_top(rr);\n bn_check_top(tmp);\n BN_CTX_end(ctx);\n return ret;\n}', 'void bn_sqr_normal(BN_ULONG *r, const BN_ULONG *a, int n, BN_ULONG *tmp)\n{\n int i, j, max;\n const BN_ULONG *ap;\n BN_ULONG *rp;\n max = n * 2;\n ap = a;\n rp = r;\n rp[0] = rp[max - 1] = 0;\n rp++;\n j = n;\n if (--j > 0) {\n ap++;\n rp[j] = bn_mul_words(rp, ap, j, ap[-1]);\n rp += 2;\n }\n for (i = n - 2; i > 0; i--) {\n j--;\n ap++;\n rp[j] = bn_mul_add_words(rp, ap, j, ap[-1]);\n rp += 2;\n }\n bn_add_words(r, r, r, max);\n bn_sqr_words(tmp, a, n);\n bn_add_words(r, r, tmp, max);\n}']
213
0
https://github.com/libav/libav/blob/da5bcafe3b728f255ad068fc406622515b435082/libavcodec/aacsbr.c/#L382
static int sbr_make_f_master(AACContext *ac, SpectralBandReplication *sbr, SpectrumParameters *spectrum) { unsigned int temp, max_qmf_subbands; unsigned int start_min, stop_min; int k; const int8_t *sbr_offset_ptr; int16_t stop_dk[13]; if (sbr->sample_rate < 32000) { temp = 3000; } else if (sbr->sample_rate < 64000) { temp = 4000; } else temp = 5000; start_min = ((temp << 7) + (sbr->sample_rate >> 1)) / sbr->sample_rate; stop_min = ((temp << 8) + (sbr->sample_rate >> 1)) / sbr->sample_rate; switch (sbr->sample_rate) { case 16000: sbr_offset_ptr = sbr_offset[0]; break; case 22050: sbr_offset_ptr = sbr_offset[1]; break; case 24000: sbr_offset_ptr = sbr_offset[2]; break; case 32000: sbr_offset_ptr = sbr_offset[3]; break; case 44100: case 48000: case 64000: sbr_offset_ptr = sbr_offset[4]; break; case 88200: case 96000: case 128000: case 176400: case 192000: sbr_offset_ptr = sbr_offset[5]; break; default: av_log(ac->avccontext, AV_LOG_ERROR, "Unsupported sample rate for SBR: %d\n", sbr->sample_rate); return -1; } sbr->k[0] = start_min + sbr_offset_ptr[spectrum->bs_start_freq]; if (spectrum->bs_stop_freq < 14) { sbr->k[2] = stop_min; make_bands(stop_dk, stop_min, 64, 13); qsort(stop_dk, 13, sizeof(stop_dk[0]), qsort_comparison_function_int16); for (k = 0; k < spectrum->bs_stop_freq; k++) sbr->k[2] += stop_dk[k]; } else if (spectrum->bs_stop_freq == 14) { sbr->k[2] = 2*sbr->k[0]; } else if (spectrum->bs_stop_freq == 15) { sbr->k[2] = 3*sbr->k[0]; } else { av_log(ac->avccontext, AV_LOG_ERROR, "Invalid bs_stop_freq: %d\n", spectrum->bs_stop_freq); return -1; } sbr->k[2] = FFMIN(64, sbr->k[2]); if (sbr->sample_rate <= 32000) { max_qmf_subbands = 48; } else if (sbr->sample_rate == 44100) { max_qmf_subbands = 35; } else if (sbr->sample_rate >= 48000) max_qmf_subbands = 32; if (sbr->k[2] - sbr->k[0] > max_qmf_subbands) { av_log(ac->avccontext, AV_LOG_ERROR, "Invalid bitstream, too many QMF subbands: %d\n", sbr->k[2] - sbr->k[0]); return -1; } if (!spectrum->bs_freq_scale) { unsigned int dk; int k2diff; dk = spectrum->bs_alter_scale + 1; sbr->n_master = ((sbr->k[2] - sbr->k[0] + (dk&2)) >> dk) << 1; if (check_n_master(ac->avccontext, sbr->n_master, sbr->spectrum_params.bs_xover_band)) return -1; for (k = 1; k <= sbr->n_master; k++) sbr->f_master[k] = dk; k2diff = sbr->k[2] - sbr->k[0] - sbr->n_master * dk; if (k2diff < 0) { sbr->f_master[1]--; sbr->f_master[2]-= (k2diff < 1); } else if (k2diff) { sbr->f_master[sbr->n_master]++; } sbr->f_master[0] = sbr->k[0]; for (k = 1; k <= sbr->n_master; k++) sbr->f_master[k] += sbr->f_master[k - 1]; } else { int half_bands = 7 - spectrum->bs_freq_scale; int two_regions, num_bands_0; int vdk0_max, vdk1_min; int16_t vk0[49]; if (49 * sbr->k[2] > 110 * sbr->k[0]) { two_regions = 1; sbr->k[1] = 2 * sbr->k[0]; } else { two_regions = 0; sbr->k[1] = sbr->k[2]; } num_bands_0 = lrintf(half_bands * log2f(sbr->k[1] / (float)sbr->k[0])) * 2; if (num_bands_0 <= 0) { av_log(ac->avccontext, AV_LOG_ERROR, "Invalid num_bands_0: %d\n", num_bands_0); return -1; } vk0[0] = 0; make_bands(vk0+1, sbr->k[0], sbr->k[1], num_bands_0); qsort(vk0 + 1, num_bands_0, sizeof(vk0[1]), qsort_comparison_function_int16); vdk0_max = vk0[num_bands_0]; vk0[0] = sbr->k[0]; for (k = 1; k <= num_bands_0; k++) { if (vk0[k] <= 0) { av_log(ac->avccontext, AV_LOG_ERROR, "Invalid vDk0[%d]: %d\n", k, vk0[k]); return -1; } vk0[k] += vk0[k-1]; } if (two_regions) { int16_t vk1[49]; float invwarp = spectrum->bs_alter_scale ? 0.76923076923076923077f : 1.0f; int num_bands_1 = lrintf(half_bands * invwarp * log2f(sbr->k[2] / (float)sbr->k[1])) * 2; make_bands(vk1+1, sbr->k[1], sbr->k[2], num_bands_1); vdk1_min = array_min_int16(vk1 + 1, num_bands_1); if (vdk1_min < vdk0_max) { int change; qsort(vk1 + 1, num_bands_1, sizeof(vk1[1]), qsort_comparison_function_int16); change = FFMIN(vdk0_max - vk1[1], (vk1[num_bands_1] - vk1[1]) >> 1); vk1[1] += change; vk1[num_bands_1] -= change; } qsort(vk1 + 1, num_bands_1, sizeof(vk1[1]), qsort_comparison_function_int16); vk1[0] = sbr->k[1]; for (k = 1; k <= num_bands_1; k++) { if (vk1[k] <= 0) { av_log(ac->avccontext, AV_LOG_ERROR, "Invalid vDk1[%d]: %d\n", k, vk1[k]); return -1; } vk1[k] += vk1[k-1]; } sbr->n_master = num_bands_0 + num_bands_1; if (check_n_master(ac->avccontext, sbr->n_master, sbr->spectrum_params.bs_xover_band)) return -1; memcpy(&sbr->f_master[0], vk0, (num_bands_0 + 1) * sizeof(sbr->f_master[0])); memcpy(&sbr->f_master[num_bands_0 + 1], vk1 + 1, num_bands_1 * sizeof(sbr->f_master[0])); } else { sbr->n_master = num_bands_0; if (check_n_master(ac->avccontext, sbr->n_master, sbr->spectrum_params.bs_xover_band)) return -1; memcpy(sbr->f_master, vk0, (num_bands_0 + 1) * sizeof(sbr->f_master[0])); } } return 0; }
['static int sbr_make_f_master(AACContext *ac, SpectralBandReplication *sbr,\n SpectrumParameters *spectrum)\n{\n unsigned int temp, max_qmf_subbands;\n unsigned int start_min, stop_min;\n int k;\n const int8_t *sbr_offset_ptr;\n int16_t stop_dk[13];\n if (sbr->sample_rate < 32000) {\n temp = 3000;\n } else if (sbr->sample_rate < 64000) {\n temp = 4000;\n } else\n temp = 5000;\n start_min = ((temp << 7) + (sbr->sample_rate >> 1)) / sbr->sample_rate;\n stop_min = ((temp << 8) + (sbr->sample_rate >> 1)) / sbr->sample_rate;\n switch (sbr->sample_rate) {\n case 16000:\n sbr_offset_ptr = sbr_offset[0];\n break;\n case 22050:\n sbr_offset_ptr = sbr_offset[1];\n break;\n case 24000:\n sbr_offset_ptr = sbr_offset[2];\n break;\n case 32000:\n sbr_offset_ptr = sbr_offset[3];\n break;\n case 44100: case 48000: case 64000:\n sbr_offset_ptr = sbr_offset[4];\n break;\n case 88200: case 96000: case 128000: case 176400: case 192000:\n sbr_offset_ptr = sbr_offset[5];\n break;\n default:\n av_log(ac->avccontext, AV_LOG_ERROR,\n "Unsupported sample rate for SBR: %d\\n", sbr->sample_rate);\n return -1;\n }\n sbr->k[0] = start_min + sbr_offset_ptr[spectrum->bs_start_freq];\n if (spectrum->bs_stop_freq < 14) {\n sbr->k[2] = stop_min;\n make_bands(stop_dk, stop_min, 64, 13);\n qsort(stop_dk, 13, sizeof(stop_dk[0]), qsort_comparison_function_int16);\n for (k = 0; k < spectrum->bs_stop_freq; k++)\n sbr->k[2] += stop_dk[k];\n } else if (spectrum->bs_stop_freq == 14) {\n sbr->k[2] = 2*sbr->k[0];\n } else if (spectrum->bs_stop_freq == 15) {\n sbr->k[2] = 3*sbr->k[0];\n } else {\n av_log(ac->avccontext, AV_LOG_ERROR,\n "Invalid bs_stop_freq: %d\\n", spectrum->bs_stop_freq);\n return -1;\n }\n sbr->k[2] = FFMIN(64, sbr->k[2]);\n if (sbr->sample_rate <= 32000) {\n max_qmf_subbands = 48;\n } else if (sbr->sample_rate == 44100) {\n max_qmf_subbands = 35;\n } else if (sbr->sample_rate >= 48000)\n max_qmf_subbands = 32;\n if (sbr->k[2] - sbr->k[0] > max_qmf_subbands) {\n av_log(ac->avccontext, AV_LOG_ERROR,\n "Invalid bitstream, too many QMF subbands: %d\\n", sbr->k[2] - sbr->k[0]);\n return -1;\n }\n if (!spectrum->bs_freq_scale) {\n unsigned int dk;\n int k2diff;\n dk = spectrum->bs_alter_scale + 1;\n sbr->n_master = ((sbr->k[2] - sbr->k[0] + (dk&2)) >> dk) << 1;\n if (check_n_master(ac->avccontext, sbr->n_master, sbr->spectrum_params.bs_xover_band))\n return -1;\n for (k = 1; k <= sbr->n_master; k++)\n sbr->f_master[k] = dk;\n k2diff = sbr->k[2] - sbr->k[0] - sbr->n_master * dk;\n if (k2diff < 0) {\n sbr->f_master[1]--;\n sbr->f_master[2]-= (k2diff < 1);\n } else if (k2diff) {\n sbr->f_master[sbr->n_master]++;\n }\n sbr->f_master[0] = sbr->k[0];\n for (k = 1; k <= sbr->n_master; k++)\n sbr->f_master[k] += sbr->f_master[k - 1];\n } else {\n int half_bands = 7 - spectrum->bs_freq_scale;\n int two_regions, num_bands_0;\n int vdk0_max, vdk1_min;\n int16_t vk0[49];\n if (49 * sbr->k[2] > 110 * sbr->k[0]) {\n two_regions = 1;\n sbr->k[1] = 2 * sbr->k[0];\n } else {\n two_regions = 0;\n sbr->k[1] = sbr->k[2];\n }\n num_bands_0 = lrintf(half_bands * log2f(sbr->k[1] / (float)sbr->k[0])) * 2;\n if (num_bands_0 <= 0) {\n av_log(ac->avccontext, AV_LOG_ERROR, "Invalid num_bands_0: %d\\n", num_bands_0);\n return -1;\n }\n vk0[0] = 0;\n make_bands(vk0+1, sbr->k[0], sbr->k[1], num_bands_0);\n qsort(vk0 + 1, num_bands_0, sizeof(vk0[1]), qsort_comparison_function_int16);\n vdk0_max = vk0[num_bands_0];\n vk0[0] = sbr->k[0];\n for (k = 1; k <= num_bands_0; k++) {\n if (vk0[k] <= 0) {\n av_log(ac->avccontext, AV_LOG_ERROR, "Invalid vDk0[%d]: %d\\n", k, vk0[k]);\n return -1;\n }\n vk0[k] += vk0[k-1];\n }\n if (two_regions) {\n int16_t vk1[49];\n float invwarp = spectrum->bs_alter_scale ? 0.76923076923076923077f\n : 1.0f;\n int num_bands_1 = lrintf(half_bands * invwarp *\n log2f(sbr->k[2] / (float)sbr->k[1])) * 2;\n make_bands(vk1+1, sbr->k[1], sbr->k[2], num_bands_1);\n vdk1_min = array_min_int16(vk1 + 1, num_bands_1);\n if (vdk1_min < vdk0_max) {\n int change;\n qsort(vk1 + 1, num_bands_1, sizeof(vk1[1]), qsort_comparison_function_int16);\n change = FFMIN(vdk0_max - vk1[1], (vk1[num_bands_1] - vk1[1]) >> 1);\n vk1[1] += change;\n vk1[num_bands_1] -= change;\n }\n qsort(vk1 + 1, num_bands_1, sizeof(vk1[1]), qsort_comparison_function_int16);\n vk1[0] = sbr->k[1];\n for (k = 1; k <= num_bands_1; k++) {\n if (vk1[k] <= 0) {\n av_log(ac->avccontext, AV_LOG_ERROR, "Invalid vDk1[%d]: %d\\n", k, vk1[k]);\n return -1;\n }\n vk1[k] += vk1[k-1];\n }\n sbr->n_master = num_bands_0 + num_bands_1;\n if (check_n_master(ac->avccontext, sbr->n_master, sbr->spectrum_params.bs_xover_band))\n return -1;\n memcpy(&sbr->f_master[0], vk0,\n (num_bands_0 + 1) * sizeof(sbr->f_master[0]));\n memcpy(&sbr->f_master[num_bands_0 + 1], vk1 + 1,\n num_bands_1 * sizeof(sbr->f_master[0]));\n } else {\n sbr->n_master = num_bands_0;\n if (check_n_master(ac->avccontext, sbr->n_master, sbr->spectrum_params.bs_xover_band))\n return -1;\n memcpy(sbr->f_master, vk0, (num_bands_0 + 1) * sizeof(sbr->f_master[0]));\n }\n }\n return 0;\n}']
214
0
https://github.com/libav/libav/blob/12b812d2e5dcc7c80b19b7c01713be31389120d5/libavcodec/h263.h/#L183
static inline int get_p_cbp(MpegEncContext * s, DCTELEM block[6][64], int motion_x, int motion_y){ int cbp, i; if (s->mpv_flags & FF_MPV_FLAG_CBP_RD) { int best_cbpy_score= INT_MAX; int best_cbpc_score= INT_MAX; int cbpc = (-1), cbpy= (-1); const int offset= (s->mv_type==MV_TYPE_16X16 ? 0 : 16) + (s->dquant ? 8 : 0); const int lambda= s->lambda2 >> (FF_LAMBDA_SHIFT - 6); for(i=0; i<4; i++){ int score= ff_h263_inter_MCBPC_bits[i + offset] * lambda; if(i&1) score += s->coded_score[5]; if(i&2) score += s->coded_score[4]; if(score < best_cbpc_score){ best_cbpc_score= score; cbpc= i; } } for(i=0; i<16; i++){ int score= ff_h263_cbpy_tab[i ^ 0xF][1] * lambda; if(i&1) score += s->coded_score[3]; if(i&2) score += s->coded_score[2]; if(i&4) score += s->coded_score[1]; if(i&8) score += s->coded_score[0]; if(score < best_cbpy_score){ best_cbpy_score= score; cbpy= i; } } cbp= cbpc + 4*cbpy; if ((motion_x | motion_y | s->dquant) == 0 && s->mv_type==MV_TYPE_16X16){ if(best_cbpy_score + best_cbpc_score + 2*lambda >= 0) cbp= 0; } for (i = 0; i < 6; i++) { if (s->block_last_index[i] >= 0 && ((cbp >> (5 - i))&1)==0 ){ s->block_last_index[i]= -1; s->dsp.clear_block(s->block[i]); } } }else{ cbp= 0; for (i = 0; i < 6; i++) { if (s->block_last_index[i] >= 0) cbp |= 1 << (5 - i); } } return cbp; }
['static inline int get_p_cbp(MpegEncContext * s,\n DCTELEM block[6][64],\n int motion_x, int motion_y){\n int cbp, i;\n if (s->mpv_flags & FF_MPV_FLAG_CBP_RD) {\n int best_cbpy_score= INT_MAX;\n int best_cbpc_score= INT_MAX;\n int cbpc = (-1), cbpy= (-1);\n const int offset= (s->mv_type==MV_TYPE_16X16 ? 0 : 16) + (s->dquant ? 8 : 0);\n const int lambda= s->lambda2 >> (FF_LAMBDA_SHIFT - 6);\n for(i=0; i<4; i++){\n int score= ff_h263_inter_MCBPC_bits[i + offset] * lambda;\n if(i&1) score += s->coded_score[5];\n if(i&2) score += s->coded_score[4];\n if(score < best_cbpc_score){\n best_cbpc_score= score;\n cbpc= i;\n }\n }\n for(i=0; i<16; i++){\n int score= ff_h263_cbpy_tab[i ^ 0xF][1] * lambda;\n if(i&1) score += s->coded_score[3];\n if(i&2) score += s->coded_score[2];\n if(i&4) score += s->coded_score[1];\n if(i&8) score += s->coded_score[0];\n if(score < best_cbpy_score){\n best_cbpy_score= score;\n cbpy= i;\n }\n }\n cbp= cbpc + 4*cbpy;\n if ((motion_x | motion_y | s->dquant) == 0 && s->mv_type==MV_TYPE_16X16){\n if(best_cbpy_score + best_cbpc_score + 2*lambda >= 0)\n cbp= 0;\n }\n for (i = 0; i < 6; i++) {\n if (s->block_last_index[i] >= 0 && ((cbp >> (5 - i))&1)==0 ){\n s->block_last_index[i]= -1;\n s->dsp.clear_block(s->block[i]);\n }\n }\n }else{\n cbp= 0;\n for (i = 0; i < 6; i++) {\n if (s->block_last_index[i] >= 0)\n cbp |= 1 << (5 - i);\n }\n }\n return cbp;\n}']
215
0
https://github.com/openssl/openssl/blob/10f0c85cfc26ffafd426d9797275191829efd599/crypto/bn/bn_mul.c/#L709
void bn_mul_part_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n, int tna, int tnb, BN_ULONG *t) { int i,j,n2=n*2; int c1,c2,neg,zero; BN_ULONG ln,lo,*p; # ifdef BN_COUNT fprintf(stderr," bn_mul_part_recursive (%d%+d) * (%d%+d)\n", n, tna, n, tnb); # endif if (n < 8) { bn_mul_normal(r,a,n+tna,b,n+tnb); return; } c1=bn_cmp_part_words(a,&(a[n]),tna,n-tna); c2=bn_cmp_part_words(&(b[n]),b,tnb,tnb-n); zero=neg=0; switch (c1*3+c2) { case -4: bn_sub_part_words(t, &(a[n]),a, tna,tna-n); bn_sub_part_words(&(t[n]),b, &(b[n]),tnb,n-tnb); break; case -3: zero=1; case -2: bn_sub_part_words(t, &(a[n]),a, tna,tna-n); bn_sub_part_words(&(t[n]),&(b[n]),b, tnb,tnb-n); neg=1; break; case -1: case 0: case 1: zero=1; case 2: bn_sub_part_words(t, a, &(a[n]),tna,n-tna); bn_sub_part_words(&(t[n]),b, &(b[n]),tnb,n-tnb); neg=1; break; case 3: zero=1; case 4: bn_sub_part_words(t, a, &(a[n]),tna,n-tna); bn_sub_part_words(&(t[n]),&(b[n]),b, tnb,tnb-n); break; } # if 0 if (n == 4) { bn_mul_comba4(&(t[n2]),t,&(t[n])); bn_mul_comba4(r,a,b); bn_mul_normal(&(r[n2]),&(a[n]),tn,&(b[n]),tn); memset(&(r[n2+tn*2]),0,sizeof(BN_ULONG)*(n2-tn*2)); } else # endif if (n == 8) { bn_mul_comba8(&(t[n2]),t,&(t[n])); bn_mul_comba8(r,a,b); bn_mul_normal(&(r[n2]),&(a[n]),tna,&(b[n]),tnb); memset(&(r[n2+tna+tnb]),0,sizeof(BN_ULONG)*(n2-tna-tnb)); } else { p= &(t[n2*2]); bn_mul_recursive(&(t[n2]),t,&(t[n]),n,0,0,p); bn_mul_recursive(r,a,b,n,0,0,p); i=n/2; if (tna > tnb) j = tna - i; else j = tnb - i; if (j == 0) { bn_mul_recursive(&(r[n2]),&(a[n]),&(b[n]), i,tna-i,tnb-i,p); memset(&(r[n2+i*2]),0,sizeof(BN_ULONG)*(n2-i*2)); } else if (j > 0) { bn_mul_part_recursive(&(r[n2]),&(a[n]),&(b[n]), i,tna-i,tnb-i,p); memset(&(r[n2+tna+tnb]),0, sizeof(BN_ULONG)*(n2-tna-tnb)); } else { memset(&(r[n2]),0,sizeof(BN_ULONG)*n2); if (tna < BN_MUL_RECURSIVE_SIZE_NORMAL && tnb < BN_MUL_RECURSIVE_SIZE_NORMAL) { bn_mul_normal(&(r[n2]),&(a[n]),tna,&(b[n]),tnb); } else { for (;;) { i/=2; if (i < tna || i < tnb) { bn_mul_part_recursive(&(r[n2]), &(a[n]),&(b[n]), i,tna-i,tnb-i,p); break; } else if (i == tna || i == tnb) { bn_mul_recursive(&(r[n2]), &(a[n]),&(b[n]), i,tna-i,tnb-i,p); break; } } } } } c1=(int)(bn_add_words(t,r,&(r[n2]),n2)); if (neg) { c1-=(int)(bn_sub_words(&(t[n2]),t,&(t[n2]),n2)); } else { c1+=(int)(bn_add_words(&(t[n2]),&(t[n2]),t,n2)); } c1+=(int)(bn_add_words(&(r[n]),&(r[n]),&(t[n2]),n2)); if (c1) { p= &(r[n+n2]); lo= *p; ln=(lo+c1)&BN_MASK2; *p=ln; if (ln < (BN_ULONG)c1) { do { p++; lo= *p; ln=(lo+1)&BN_MASK2; *p=ln; } while (ln == 0); } } }
['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\tn, tna, n, tnb);\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 * %d%+d\\n",n2,dna,n2,dnb);\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}']
216
0
https://github.com/nginx/nginx/blob/bcd78e22e97d4c870b5104d0c540caaa972176ed/src/core/ngx_string.c/#L859
ngx_int_t ngx_atoi(u_char *line, size_t n) { ngx_int_t value; if (n == 0) { return NGX_ERROR; } for (value = 0; n--; line++) { if (*line < '0' || *line > '9') { return NGX_ERROR; } value = value * 10 + (*line - '0'); } if (value < 0) { return NGX_ERROR; } else { return value; } }
['static ngx_int_t\nngx_http_uwsgi_eval(ngx_http_request_t *r, ngx_http_uwsgi_loc_conf_t * uwcf)\n{\n ngx_url_t url;\n ngx_http_upstream_t *u;\n ngx_memzero(&url, sizeof(ngx_url_t));\n if (ngx_http_script_run(r, &url.url, uwcf->uwsgi_lengths->elts, 0,\n uwcf->uwsgi_values->elts)\n == NULL)\n {\n return NGX_ERROR;\n }\n url.no_resolve = 1;\n if (ngx_parse_url(r->pool, &url) != NGX_OK) {\n if (url.err) {\n ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,\n "%s in upstream \\"%V\\"", url.err, &url.url);\n }\n return NGX_ERROR;\n }\n if (url.no_port) {\n ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,\n "no port in upstream \\"%V\\"", &url.url);\n return NGX_ERROR;\n }\n u = r->upstream;\n u->resolved = ngx_pcalloc(r->pool, sizeof(ngx_http_upstream_resolved_t));\n if (u->resolved == NULL) {\n return NGX_ERROR;\n }\n if (url.addrs && url.addrs[0].sockaddr) {\n u->resolved->sockaddr = url.addrs[0].sockaddr;\n u->resolved->socklen = url.addrs[0].socklen;\n u->resolved->naddrs = 1;\n u->resolved->host = url.addrs[0].name;\n } else {\n u->resolved->host = url.host;\n u->resolved->port = url.port;\n }\n return NGX_OK;\n}', 'u_char *\nngx_http_script_run(ngx_http_request_t *r, ngx_str_t *value,\n void *code_lengths, size_t len, void *code_values)\n{\n ngx_uint_t i;\n ngx_http_script_code_pt code;\n ngx_http_script_len_code_pt lcode;\n ngx_http_script_engine_t e;\n ngx_http_core_main_conf_t *cmcf;\n cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);\n for (i = 0; i < cmcf->variables.nelts; i++) {\n if (r->variables[i].no_cacheable) {\n r->variables[i].valid = 0;\n r->variables[i].not_found = 0;\n }\n }\n ngx_memzero(&e, sizeof(ngx_http_script_engine_t));\n e.ip = code_lengths;\n e.request = r;\n e.flushed = 1;\n while (*(uintptr_t *) e.ip) {\n lcode = *(ngx_http_script_len_code_pt *) e.ip;\n len += lcode(&e);\n }\n value->len = len;\n value->data = ngx_pnalloc(r->pool, len);\n if (value->data == NULL) {\n return NULL;\n }\n e.ip = code_values;\n e.pos = value->data;\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 return e.pos;\n}', 'void *\nngx_pnalloc(ngx_pool_t *pool, size_t size)\n{\n u_char *m;\n ngx_pool_t *p;\n if (size <= pool->max) {\n p = pool->current;\n do {\n m = p->d.last;\n if ((size_t) (p->d.end - m) >= size) {\n p->d.last = m + size;\n return m;\n }\n p = p->d.next;\n } while (p);\n return ngx_palloc_block(pool, size);\n }\n return ngx_palloc_large(pool, size);\n}', 'ngx_int_t\nngx_parse_url(ngx_pool_t *pool, ngx_url_t *u)\n{\n u_char *p;\n p = u->url.data;\n if (ngx_strncasecmp(p, (u_char *) "unix:", 5) == 0) {\n return ngx_parse_unix_domain_url(pool, u);\n }\n if ((p[0] == \':\' || p[0] == \'/\') && !u->listen) {\n u->err = "invalid host";\n return NGX_ERROR;\n }\n if (p[0] == \'[\') {\n return ngx_parse_inet6_url(pool, u);\n }\n return ngx_parse_inet_url(pool, u);\n}', 'static ngx_int_t\nngx_parse_inet_url(ngx_pool_t *pool, ngx_url_t *u)\n{\n u_char *p, *host, *port, *last, *uri, *args;\n size_t len;\n ngx_int_t n;\n struct hostent *h;\n struct sockaddr_in *sin;\n u->socklen = sizeof(struct sockaddr_in);\n sin = (struct sockaddr_in *) &u->sockaddr;\n sin->sin_family = AF_INET;\n u->family = AF_INET;\n host = u->url.data;\n last = host + u->url.len;\n port = ngx_strlchr(host, last, \':\');\n uri = ngx_strlchr(host, last, \'/\');\n args = ngx_strlchr(host, last, \'?\');\n if (args) {\n if (uri == NULL) {\n uri = args;\n } else if (args < uri) {\n uri = args;\n }\n }\n if (uri) {\n if (u->listen || !u->uri_part) {\n u->err = "invalid host";\n return NGX_ERROR;\n }\n u->uri.len = last - uri;\n u->uri.data = uri;\n last = uri;\n if (uri < port) {\n port = NULL;\n }\n }\n if (port) {\n port++;\n len = last - port;\n if (len == 0) {\n u->err = "invalid port";\n return NGX_ERROR;\n }\n n = ngx_atoi(port, len);\n if (n < 1 || n > 65536) {\n u->err = "invalid port";\n return NGX_ERROR;\n }\n u->port = (in_port_t) n;\n sin->sin_port = htons((in_port_t) n);\n u->port_text.len = len;\n u->port_text.data = port;\n last = port - 1;\n } else {\n if (uri == NULL) {\n if (u->listen) {\n n = ngx_atoi(host, last - host);\n if (n != NGX_ERROR) {\n if (n < 1 || n > 65536) {\n u->err = "invalid port";\n return NGX_ERROR;\n }\n u->port = (in_port_t) n;\n sin->sin_port = htons((in_port_t) n);\n u->port_text.len = last - host;\n u->port_text.data = host;\n u->wildcard = 1;\n return NGX_OK;\n }\n }\n }\n u->no_port = 1;\n }\n len = last - host;\n if (len == 0) {\n u->err = "no host";\n return NGX_ERROR;\n }\n if (len == 1 && *host == \'*\') {\n len = 0;\n }\n u->host.len = len;\n u->host.data = host;\n if (u->no_resolve) {\n return NGX_OK;\n }\n if (len) {\n sin->sin_addr.s_addr = ngx_inet_addr(host, len);\n if (sin->sin_addr.s_addr == INADDR_NONE) {\n p = ngx_alloc(++len, pool->log);\n if (p == NULL) {\n return NGX_ERROR;\n }\n (void) ngx_cpystrn(p, host, len);\n h = gethostbyname((const char *) p);\n ngx_free(p);\n if (h == NULL || h->h_addr_list[0] == NULL) {\n u->err = "host not found";\n return NGX_ERROR;\n }\n sin->sin_addr.s_addr = *(in_addr_t *) (h->h_addr_list[0]);\n }\n if (sin->sin_addr.s_addr == INADDR_ANY) {\n u->wildcard = 1;\n }\n } else {\n sin->sin_addr.s_addr = INADDR_ANY;\n u->wildcard = 1;\n }\n if (u->no_port) {\n u->port = u->default_port;\n sin->sin_port = htons(u->default_port);\n }\n if (u->listen) {\n return NGX_OK;\n }\n if (ngx_inet_resolve_host(pool, u) != NGX_OK) {\n return NGX_ERROR;\n }\n return NGX_OK;\n}', "ngx_int_t\nngx_atoi(u_char *line, size_t n)\n{\n ngx_int_t value;\n if (n == 0) {\n return NGX_ERROR;\n }\n for (value = 0; n--; line++) {\n if (*line < '0' || *line > '9') {\n return NGX_ERROR;\n }\n value = value * 10 + (*line - '0');\n }\n if (value < 0) {\n return NGX_ERROR;\n } else {\n return value;\n }\n}"]
217
0
https://github.com/openssl/openssl/blob/e7e1150706f8c8bcc807d8184bd0ebd08b6f5aff/crypto/aes/aes_cfb.c/#L188
void AES_cfbr_encrypt_block(const unsigned char *in,unsigned char *out, const int nbits,const AES_KEY *key, unsigned char *ivec,const int enc) { int n,rem,num; unsigned char ovec[AES_BLOCK_SIZE*2]; if (nbits<=0 || nbits>128) return; memcpy(ovec,ivec,AES_BLOCK_SIZE); AES_encrypt(ivec,ivec,key); num = (nbits+7)/8; if (enc) for(n=0 ; n < num ; ++n) out[n] = (ovec[AES_BLOCK_SIZE+n] = in[n] ^ ivec[n]); else for(n=0 ; n < num ; ++n) out[n] = (ovec[AES_BLOCK_SIZE+n] = in[n]) ^ ivec[n]; rem = nbits%8; num = nbits/8; if(rem==0) memcpy(ivec,ovec+num,AES_BLOCK_SIZE); else for(n=0 ; n < AES_BLOCK_SIZE ; ++n) ivec[n] = ovec[n+num]<<rem | ovec[n+num+1]>>(8-rem); }
['void AES_cfbr_encrypt_block(const unsigned char *in,unsigned char *out,\n\t\t\t const int nbits,const AES_KEY *key,\n\t\t\t unsigned char *ivec,const int enc)\n {\n int n,rem,num;\n unsigned char ovec[AES_BLOCK_SIZE*2];\n if (nbits<=0 || nbits>128) return;\n\tmemcpy(ovec,ivec,AES_BLOCK_SIZE);\n\tAES_encrypt(ivec,ivec,key);\n\tnum = (nbits+7)/8;\n\tif (enc)\n\t for(n=0 ; n < num ; ++n)\n\t\tout[n] = (ovec[AES_BLOCK_SIZE+n] = in[n] ^ ivec[n]);\n\telse\n\t for(n=0 ; n < num ; ++n)\n\t\tout[n] = (ovec[AES_BLOCK_SIZE+n] = in[n]) ^ ivec[n];\n\trem = nbits%8;\n\tnum = nbits/8;\n\tif(rem==0)\n\t memcpy(ivec,ovec+num,AES_BLOCK_SIZE);\n\telse\n\t for(n=0 ; n < AES_BLOCK_SIZE ; ++n)\n\t\tivec[n] = ovec[n+num]<<rem | ovec[n+num+1]>>(8-rem);\n }']
218
0
https://github.com/nginx/nginx/blob/7e4f193bb0e1cfa4128052f538dd60519cac02e4/src/core/ngx_string.c/#L364
u_char * ngx_vslprintf(u_char *buf, u_char *last, const char *fmt, va_list args) { u_char *p, zero; 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; 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; }
['ngx_http_variable_value_t *\nngx_http_get_variable(ngx_http_request_t *r, ngx_str_t *name, ngx_uint_t key,\n ngx_uint_t nowarn)\n{\n ngx_http_variable_t *v;\n ngx_http_variable_value_t *vv;\n ngx_http_core_main_conf_t *cmcf;\n cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);\n v = ngx_hash_find(&cmcf->variables_hash, key, name->data, name->len);\n if (v) {\n if (v->flags & NGX_HTTP_VAR_INDEXED) {\n return ngx_http_get_indexed_variable(r, v->index);\n } else {\n vv = ngx_palloc(r->pool, sizeof(ngx_http_variable_value_t));\n if (vv && v->get_handler(r, vv, v->data) == NGX_OK) {\n return vv;\n }\n return NULL;\n }\n }\n vv = ngx_palloc(r->pool, sizeof(ngx_http_variable_value_t));\n if (vv == NULL) {\n return NULL;\n }\n if (ngx_strncmp(name->data, "http_", 5) == 0) {\n if (ngx_http_variable_unknown_header_in(r, vv, (uintptr_t) name)\n == NGX_OK)\n {\n return vv;\n }\n return NULL;\n }\n if (ngx_strncmp(name->data, "sent_http_", 10) == 0) {\n if (ngx_http_variable_unknown_header_out(r, vv, (uintptr_t) name)\n == NGX_OK)\n {\n return vv;\n }\n return NULL;\n }\n if (ngx_strncmp(name->data, "upstream_http_", 10) == 0) {\n if (ngx_http_upstream_header_variable(r, vv, (uintptr_t) name)\n == NGX_OK)\n {\n return vv;\n }\n return NULL;\n }\n if (ngx_strncmp(name->data, "cookie_", 7) == 0) {\n if (ngx_http_variable_cookie(r, vv, (uintptr_t) name) == NGX_OK) {\n return vv;\n }\n return NULL;\n }\n if (ngx_strncmp(name->data, "arg_", 4) == 0) {\n if (ngx_http_variable_argument(r, vv, (uintptr_t) name) == NGX_OK) {\n return vv;\n }\n return NULL;\n }\n vv->not_found = 1;\n if (nowarn == 0) {\n ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,\n "unknown \\"%V\\" variable", name);\n }\n return vv;\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 *p, *last, *msg;\n u_char errstr[NGX_MAX_ERROR_STR];\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_slprintf(p, last, " [%V] ", &err_levels[level]);\n p = ngx_slprintf(p, last, "%P#" NGX_TID_T_FMT ": ",\n ngx_log_pid, ngx_log_tid);\n if (log->connection) {\n p = ngx_slprintf(p, last, "*%uA ", log->connection);\n }\n msg = p;\n#if (NGX_HAVE_VARIADIC_MACROS)\n va_start(args, fmt);\n p = ngx_vslprintf(p, last, fmt, args);\n va_end(args);\n#else\n p = ngx_vslprintf(p, last, fmt, args);\n#endif\n if (err) {\n p = ngx_log_errno(p, last, err);\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 if (!ngx_use_stderr\n || level > NGX_LOG_WARN\n || log->file->fd == ngx_stderr)\n {\n return;\n }\n msg -= (err_levels[level].len + 4);\n (void) ngx_sprintf(msg, "[%V]: ", &err_levels[level]);\n (void) ngx_write_console(ngx_stderr, msg, p - msg);\n}', 'u_char * ngx_cdecl\nngx_sprintf(u_char *buf, const char *fmt, ...)\n{\n u_char *p;\n va_list args;\n va_start(args, fmt);\n p = ngx_vslprintf(buf, (void *) -1, fmt, args);\n va_end(args);\n return p;\n}', "u_char *\nngx_vslprintf(u_char *buf, u_char *last, const char *fmt, va_list args)\n{\n u_char *p, zero;\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 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}"]
219
0
https://github.com/openssl/openssl/blob/95dc05bc6d0dfe0f3f3681f5e27afbc3f7a35eea/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 DH_compute_key(unsigned char *key, BIGNUM *pub_key, DH *dh)\n\t{\n\tBN_CTX ctx;\n\tBN_MONT_CTX *mont;\n\tBIGNUM *tmp;\n\tint ret= -1;\n\tBN_CTX_init(&ctx);\n\ttmp= &(ctx.bn[ctx.tos++]);\n\tif (dh->priv_key == NULL)\n\t\t{\n\t\tDHerr(DH_F_DH_COMPUTE_KEY,DH_R_NO_PRIVATE_VALUE);\n\t\tgoto err;\n\t\t}\n\tif ((dh->method_mont_p == NULL) && (dh->flags & DH_FLAG_CACHE_MONT_P))\n\t\t{\n\t\tif ((dh->method_mont_p=(char *)BN_MONT_CTX_new()) != NULL)\n\t\t\tif (!BN_MONT_CTX_set((BN_MONT_CTX *)dh->method_mont_p,\n\t\t\t\tdh->p,&ctx)) goto err;\n\t\t}\n\tmont=(BN_MONT_CTX *)dh->method_mont_p;\n\tif (!BN_mod_exp_mont(tmp,pub_key,dh->priv_key,dh->p,&ctx,mont))\n\t\t{\n\t\tDHerr(DH_F_DH_COMPUTE_KEY,ERR_R_BN_LIB);\n\t\tgoto err;\n\t\t}\n\tret=BN_bn2bin(tmp,key);\nerr:\n\tBN_CTX_free(&ctx);\n\treturn(ret);\n\t}', 'int BN_mod_exp_mont(BIGNUM *rr, BIGNUM *a, BIGNUM *p, BIGNUM *m, BN_CTX *ctx,\n\t 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,*aa,*r;\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_mul_montgomery(BIGNUM *r, BIGNUM *a, BIGNUM *b, BN_MONT_CTX *mont,\n\t BN_CTX *ctx)\n\t{\n\tBIGNUM *tmp,*tmp2;\n tmp= &(ctx->bn[ctx->tos]);\n tmp2= &(ctx->bn[ctx->tos]);\n\tctx->tos+=2;\n\tbn_check_top(tmp);\n\tbn_check_top(tmp2);\n\tif (a == b)\n\t\t{\n#if 0\n\t\tbn_wexpand(tmp,a->top*2);\n\t\tbn_wexpand(tmp2,a->top*4);\n\t\tbn_sqr_recursive(tmp->d,a->d,a->top,tmp2->d);\n\t\ttmp->top=a->top*2;\n\t\tif (tmp->d[tmp->top-1] == 0)\n\t\t\ttmp->top--;\n#else\n\t\tif (!BN_sqr(tmp,a,ctx)) goto err;\n#endif\n\t\t}\n\telse\n\t\t{\n\t\tif (!BN_mul(tmp,a,b,ctx)) goto err;\n\t\t}\n\tif (!BN_from_montgomery(r,tmp,mont,ctx)) goto err;\n\tctx->tos-=2;\n\treturn(1);\nerr:\n\treturn(0);\n\t}', 'int BN_mul(BIGNUM *r, BIGNUM *a, BIGNUM *b, BN_CTX *ctx)\n\t{\n\tint top,al,bl;\n\tBIGNUM *rr;\n#ifdef BN_RECURSION\n\tBIGNUM *t;\n\tint i,j,k;\n#endif\n#ifdef BN_COUNT\nprintf("BN_mul %d * %d\\n",a->top,b->top);\n#endif\n\tbn_check_top(a);\n\tbn_check_top(b);\n\tbn_check_top(r);\n\tal=a->top;\n\tbl=b->top;\n\tr->neg=a->neg^b->neg;\n\tif ((al == 0) || (bl == 0))\n\t\t{\n\t\tBN_zero(r);\n\t\treturn(1);\n\t\t}\n\ttop=al+bl;\n\tif ((r == a) || (r == b))\n\t\trr= &(ctx->bn[ctx->tos+1]);\n\telse\n\t\trr=r;\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n\tif (al == bl)\n\t\t{\n# ifdef BN_MUL_COMBA\n if (al == 8)\n\t\t\t{\n\t\t\tif (bn_wexpand(rr,16) == NULL) return(0);\n\t\t\tr->top=16;\n\t\t\tbn_mul_comba8(rr->d,a->d,b->d);\n\t\t\tgoto end;\n\t\t\t}\n\t\telse\n# endif\n#ifdef BN_RECURSION\n\t\tif (al < BN_MULL_SIZE_NORMAL)\n#endif\n\t\t\t{\n\t\t\tif (bn_wexpand(rr,top) == NULL) return(0);\n\t\t\trr->top=top;\n\t\t\tbn_mul_normal(rr->d,a->d,al,b->d,bl);\n\t\t\tgoto end;\n\t\t\t}\n# ifdef BN_RECURSION\n\t\tgoto symetric;\n# endif\n\t\t}\n#endif\n#ifdef BN_RECURSION\n\telse if ((al < BN_MULL_SIZE_NORMAL) || (bl < BN_MULL_SIZE_NORMAL))\n\t\t{\n\t\tif (bn_wexpand(rr,top) == NULL) return(0);\n\t\trr->top=top;\n\t\tbn_mul_normal(rr->d,a->d,al,b->d,bl);\n\t\tgoto end;\n\t\t}\n\telse\n\t\t{\n\t\ti=(al-bl);\n\t\tif ((i == 1) && !BN_get_flags(b,BN_FLG_STATIC_DATA))\n\t\t\t{\n\t\t\tbn_wexpand(b,al);\n\t\t\tb->d[bl]=0;\n\t\t\tbl++;\n\t\t\tgoto symetric;\n\t\t\t}\n\t\telse if ((i == -1) && !BN_get_flags(a,BN_FLG_STATIC_DATA))\n\t\t\t{\n\t\t\tbn_wexpand(a,bl);\n\t\t\ta->d[al]=0;\n\t\t\tal++;\n\t\t\tgoto symetric;\n\t\t\t}\n\t\t}\n#endif\n\tif (bn_wexpand(rr,top) == NULL) return(0);\n\trr->top=top;\n\tbn_mul_normal(rr->d,a->d,al,b->d,bl);\n#ifdef BN_RECURSION\n\tif (0)\n\t\t{\nsymetric:\n\t\tj=BN_num_bits_word((BN_ULONG)al);\n\t\tj=1<<(j-1);\n\t\tk=j+j;\n\t\tt= &(ctx->bn[ctx->tos]);\n\t\tif (al == j)\n\t\t\t{\n\t\t\tbn_wexpand(t,k*2);\n\t\t\tbn_wexpand(rr,k*2);\n\t\t\tbn_mul_recursive(rr->d,a->d,b->d,al,t->d);\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tbn_wexpand(a,k);\n\t\t\tbn_wexpand(b,k);\n\t\t\tbn_wexpand(t,k*4);\n\t\t\tbn_wexpand(rr,k*4);\n\t\t\tfor (i=a->top; i<k; i++)\n\t\t\t\ta->d[i]=0;\n\t\t\tfor (i=b->top; i<k; i++)\n\t\t\t\tb->d[i]=0;\n\t\t\tbn_mul_part_recursive(rr->d,a->d,b->d,al-j,j,t->d);\n\t\t\t}\n\t\trr->top=top;\n\t\t}\n#endif\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\nend:\n#endif\n\tbn_fix_top(rr);\n\tif (r != rr) BN_copy(r,rr);\n\treturn(1);\n\t}', '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}']
220
0
https://github.com/libav/libav/blob/b7847a3f2eb56591590ab065b65c335892af6e4c/ffmpeg.c/#L4229
static int opt_preset(const char *opt, const char *arg) { FILE *f=NULL; char filename[1000], tmp[1000], tmp2[1000], line[1000]; char *codec_name = *opt == 'v' ? video_codec_name : *opt == 'a' ? audio_codec_name : subtitle_codec_name; if (!(f = get_preset_file(filename, sizeof(filename), arg, *opt == 'f', codec_name))) { fprintf(stderr, "File for preset '%s' not found\n", arg); ffmpeg_exit(1); } while(!feof(f)){ int e= fscanf(f, "%999[^\n]\n", line) - 1; if(line[0] == '#' && !e) continue; e|= sscanf(line, "%999[^=]=%999[^\n]\n", tmp, tmp2) - 2; if(e){ fprintf(stderr, "%s: Invalid syntax: '%s'\n", filename, line); ffmpeg_exit(1); } if(!strcmp(tmp, "acodec")){ opt_audio_codec(tmp, tmp2); }else if(!strcmp(tmp, "vcodec")){ opt_video_codec(tmp, tmp2); }else if(!strcmp(tmp, "scodec")){ opt_subtitle_codec(tmp, tmp2); }else if(!strcmp(tmp, "dcodec")){ opt_data_codec(tmp, tmp2); }else if(opt_default(tmp, tmp2) < 0){ fprintf(stderr, "%s: Invalid option or argument: '%s', parsed as '%s' = '%s'\n", filename, line, tmp, tmp2); ffmpeg_exit(1); } } fclose(f); return 0; }
['static int opt_preset(const char *opt, const char *arg)\n{\n FILE *f=NULL;\n char filename[1000], tmp[1000], tmp2[1000], line[1000];\n char *codec_name = *opt == \'v\' ? video_codec_name :\n *opt == \'a\' ? audio_codec_name :\n subtitle_codec_name;\n if (!(f = get_preset_file(filename, sizeof(filename), arg, *opt == \'f\', codec_name))) {\n fprintf(stderr, "File for preset \'%s\' not found\\n", arg);\n ffmpeg_exit(1);\n }\n while(!feof(f)){\n int e= fscanf(f, "%999[^\\n]\\n", line) - 1;\n if(line[0] == \'#\' && !e)\n continue;\n e|= sscanf(line, "%999[^=]=%999[^\\n]\\n", tmp, tmp2) - 2;\n if(e){\n fprintf(stderr, "%s: Invalid syntax: \'%s\'\\n", filename, line);\n ffmpeg_exit(1);\n }\n if(!strcmp(tmp, "acodec")){\n opt_audio_codec(tmp, tmp2);\n }else if(!strcmp(tmp, "vcodec")){\n opt_video_codec(tmp, tmp2);\n }else if(!strcmp(tmp, "scodec")){\n opt_subtitle_codec(tmp, tmp2);\n }else if(!strcmp(tmp, "dcodec")){\n opt_data_codec(tmp, tmp2);\n }else if(opt_default(tmp, tmp2) < 0){\n fprintf(stderr, "%s: Invalid option or argument: \'%s\', parsed as \'%s\' = \'%s\'\\n", filename, line, tmp, tmp2);\n ffmpeg_exit(1);\n }\n }\n fclose(f);\n return 0;\n}']
221
0
https://github.com/openssl/openssl/blob/198c42f5af0d2cc0eb4789c7597b68b6b10cc97a/crypto/rand/rand_lib.c/#L318
void RAND_add(const void *buf, int num, double randomness) { const RAND_METHOD *meth = RAND_get_rand_method(); if (meth->add != NULL) meth->add(buf, num, randomness); }
['void RAND_add(const void *buf, int num, double randomness)\n{\n const RAND_METHOD *meth = RAND_get_rand_method();\n if (meth->add != NULL)\n meth->add(buf, num, randomness);\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}']
222
0
https://github.com/openssl/openssl/blob/9c46f4b9cd4912b61cb546c48b678488d7f26ed6/crypto/modes/gcm128.c/#L1493
int CRYPTO_gcm128_encrypt_ctr32(GCM128_CONTEXT *ctx, const unsigned char *in, unsigned char *out, size_t len, ctr128_f stream) { const union { long one; char little; } is_endian = { 1 }; unsigned int n, ctr; size_t i; u64 mlen = ctx->len.u[1]; void *key = ctx->key; #ifdef GCM_FUNCREF_4BIT void (*gcm_gmult_p) (u64 Xi[2], const u128 Htable[16]) = ctx->gmult; # ifdef GHASH void (*gcm_ghash_p) (u64 Xi[2], const u128 Htable[16], const u8 *inp, size_t len) = ctx->ghash; # endif #endif mlen += len; if (mlen > ((U64(1) << 36) - 32) || (sizeof(len) == 8 && mlen < len)) return -1; ctx->len.u[1] = mlen; if (ctx->ares) { GCM_MUL(ctx, Xi); ctx->ares = 0; } if (is_endian.little) #ifdef BSWAP4 ctr = BSWAP4(ctx->Yi.d[3]); #else ctr = GETU32(ctx->Yi.c + 12); #endif else ctr = ctx->Yi.d[3]; n = ctx->mres; if (n) { while (n && len) { ctx->Xi.c[n] ^= *(out++) = *(in++) ^ ctx->EKi.c[n]; --len; n = (n + 1) % 16; } if (n == 0) GCM_MUL(ctx, Xi); else { ctx->mres = n; return 0; } } #if defined(GHASH) && !defined(OPENSSL_SMALL_FOOTPRINT) while (len >= GHASH_CHUNK) { (*stream) (in, out, GHASH_CHUNK / 16, key, ctx->Yi.c); ctr += GHASH_CHUNK / 16; if (is_endian.little) # ifdef BSWAP4 ctx->Yi.d[3] = BSWAP4(ctr); # else PUTU32(ctx->Yi.c + 12, ctr); # endif else ctx->Yi.d[3] = ctr; GHASH(ctx, out, GHASH_CHUNK); out += GHASH_CHUNK; in += GHASH_CHUNK; len -= GHASH_CHUNK; } #endif if ((i = (len & (size_t)-16))) { size_t j = i / 16; (*stream) (in, out, j, key, ctx->Yi.c); ctr += (unsigned int)j; if (is_endian.little) #ifdef BSWAP4 ctx->Yi.d[3] = BSWAP4(ctr); #else PUTU32(ctx->Yi.c + 12, ctr); #endif else ctx->Yi.d[3] = ctr; in += i; len -= i; #if defined(GHASH) GHASH(ctx, out, i); out += i; #else while (j--) { for (i = 0; i < 16; ++i) ctx->Xi.c[i] ^= out[i]; GCM_MUL(ctx, Xi); out += 16; } #endif } if (len) { (*ctx->block) (ctx->Yi.c, ctx->EKi.c, key); ++ctr; if (is_endian.little) #ifdef BSWAP4 ctx->Yi.d[3] = BSWAP4(ctr); #else PUTU32(ctx->Yi.c + 12, ctr); #endif else ctx->Yi.d[3] = ctr; while (len--) { ctx->Xi.c[n] ^= out[n] = in[n] ^ ctx->EKi.c[n]; ++n; } } ctx->mres = n; return 0; }
['static int aes_gcm_tls_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,\n const unsigned char *in, size_t len)\n{\n EVP_AES_GCM_CTX *gctx = ctx->cipher_data;\n int rv = -1;\n if (out != in\n || len < (EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN))\n return -1;\n if (EVP_CIPHER_CTX_ctrl(ctx, ctx->encrypt ?\n EVP_CTRL_GCM_IV_GEN : EVP_CTRL_GCM_SET_IV_INV,\n EVP_GCM_TLS_EXPLICIT_IV_LEN, out) <= 0)\n goto err;\n if (CRYPTO_gcm128_aad(&gctx->gcm, ctx->buf, gctx->tls_aad_len))\n goto err;\n in += EVP_GCM_TLS_EXPLICIT_IV_LEN;\n out += EVP_GCM_TLS_EXPLICIT_IV_LEN;\n len -= EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN;\n if (ctx->encrypt) {\n if (gctx->ctr) {\n size_t bulk = 0;\n# if defined(AES_GCM_ASM)\n if (len >= 32 && AES_GCM_ASM(gctx)) {\n if (CRYPTO_gcm128_encrypt(&gctx->gcm, NULL, NULL, 0))\n return -1;\n bulk = AES_gcm_encrypt(in, out, len,\n gctx->gcm.key,\n gctx->gcm.Yi.c, gctx->gcm.Xi.u);\n gctx->gcm.len.u[1] += bulk;\n }\n# endif\n if (CRYPTO_gcm128_encrypt_ctr32(&gctx->gcm,\n in + bulk,\n out + bulk,\n len - bulk, gctx->ctr))\n goto err;\n } else {\n size_t bulk = 0;\n# if defined(AES_GCM_ASM2)\n if (len >= 32 && AES_GCM_ASM2(gctx)) {\n if (CRYPTO_gcm128_encrypt(&gctx->gcm, NULL, NULL, 0))\n return -1;\n bulk = AES_gcm_encrypt(in, out, len,\n gctx->gcm.key,\n gctx->gcm.Yi.c, gctx->gcm.Xi.u);\n gctx->gcm.len.u[1] += bulk;\n }\n# endif\n if (CRYPTO_gcm128_encrypt(&gctx->gcm,\n in + bulk, out + bulk, len - bulk))\n goto err;\n }\n out += len;\n CRYPTO_gcm128_tag(&gctx->gcm, out, EVP_GCM_TLS_TAG_LEN);\n rv = len + EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN;\n } else {\n if (gctx->ctr) {\n size_t bulk = 0;\n# if defined(AES_GCM_ASM)\n if (len >= 16 && AES_GCM_ASM(gctx)) {\n if (CRYPTO_gcm128_decrypt(&gctx->gcm, NULL, NULL, 0))\n return -1;\n bulk = AES_gcm_decrypt(in, out, len,\n gctx->gcm.key,\n gctx->gcm.Yi.c, gctx->gcm.Xi.u);\n gctx->gcm.len.u[1] += bulk;\n }\n# endif\n if (CRYPTO_gcm128_decrypt_ctr32(&gctx->gcm,\n in + bulk,\n out + bulk,\n len - bulk, gctx->ctr))\n goto err;\n } else {\n size_t bulk = 0;\n# if defined(AES_GCM_ASM2)\n if (len >= 16 && AES_GCM_ASM2(gctx)) {\n if (CRYPTO_gcm128_decrypt(&gctx->gcm, NULL, NULL, 0))\n return -1;\n bulk = AES_gcm_decrypt(in, out, len,\n gctx->gcm.key,\n gctx->gcm.Yi.c, gctx->gcm.Xi.u);\n gctx->gcm.len.u[1] += bulk;\n }\n# endif\n if (CRYPTO_gcm128_decrypt(&gctx->gcm,\n in + bulk, out + bulk, len - bulk))\n goto err;\n }\n CRYPTO_gcm128_tag(&gctx->gcm, ctx->buf, EVP_GCM_TLS_TAG_LEN);\n if (memcmp(ctx->buf, in + len, EVP_GCM_TLS_TAG_LEN)) {\n OPENSSL_cleanse(out, len);\n goto err;\n }\n rv = len;\n }\n err:\n gctx->iv_set = 0;\n gctx->tls_aad_len = -1;\n return rv;\n}', 'int CRYPTO_gcm128_encrypt_ctr32(GCM128_CONTEXT *ctx,\n const unsigned char *in, unsigned char *out,\n size_t len, ctr128_f stream)\n{\n const union {\n long one;\n char little;\n } is_endian = {\n 1\n };\n unsigned int n, ctr;\n size_t i;\n u64 mlen = ctx->len.u[1];\n void *key = ctx->key;\n#ifdef GCM_FUNCREF_4BIT\n void (*gcm_gmult_p) (u64 Xi[2], const u128 Htable[16]) = ctx->gmult;\n# ifdef GHASH\n void (*gcm_ghash_p) (u64 Xi[2], const u128 Htable[16],\n const u8 *inp, size_t len) = ctx->ghash;\n# endif\n#endif\n mlen += len;\n if (mlen > ((U64(1) << 36) - 32) || (sizeof(len) == 8 && mlen < len))\n return -1;\n ctx->len.u[1] = mlen;\n if (ctx->ares) {\n GCM_MUL(ctx, Xi);\n ctx->ares = 0;\n }\n if (is_endian.little)\n#ifdef BSWAP4\n ctr = BSWAP4(ctx->Yi.d[3]);\n#else\n ctr = GETU32(ctx->Yi.c + 12);\n#endif\n else\n ctr = ctx->Yi.d[3];\n n = ctx->mres;\n if (n) {\n while (n && len) {\n ctx->Xi.c[n] ^= *(out++) = *(in++) ^ ctx->EKi.c[n];\n --len;\n n = (n + 1) % 16;\n }\n if (n == 0)\n GCM_MUL(ctx, Xi);\n else {\n ctx->mres = n;\n return 0;\n }\n }\n#if defined(GHASH) && !defined(OPENSSL_SMALL_FOOTPRINT)\n while (len >= GHASH_CHUNK) {\n (*stream) (in, out, GHASH_CHUNK / 16, key, ctx->Yi.c);\n ctr += GHASH_CHUNK / 16;\n if (is_endian.little)\n# ifdef BSWAP4\n ctx->Yi.d[3] = BSWAP4(ctr);\n# else\n PUTU32(ctx->Yi.c + 12, ctr);\n# endif\n else\n ctx->Yi.d[3] = ctr;\n GHASH(ctx, out, GHASH_CHUNK);\n out += GHASH_CHUNK;\n in += GHASH_CHUNK;\n len -= GHASH_CHUNK;\n }\n#endif\n if ((i = (len & (size_t)-16))) {\n size_t j = i / 16;\n (*stream) (in, out, j, key, ctx->Yi.c);\n ctr += (unsigned int)j;\n if (is_endian.little)\n#ifdef BSWAP4\n ctx->Yi.d[3] = BSWAP4(ctr);\n#else\n PUTU32(ctx->Yi.c + 12, ctr);\n#endif\n else\n ctx->Yi.d[3] = ctr;\n in += i;\n len -= i;\n#if defined(GHASH)\n GHASH(ctx, out, i);\n out += i;\n#else\n while (j--) {\n for (i = 0; i < 16; ++i)\n ctx->Xi.c[i] ^= out[i];\n GCM_MUL(ctx, Xi);\n out += 16;\n }\n#endif\n }\n if (len) {\n (*ctx->block) (ctx->Yi.c, ctx->EKi.c, key);\n ++ctr;\n if (is_endian.little)\n#ifdef BSWAP4\n ctx->Yi.d[3] = BSWAP4(ctr);\n#else\n PUTU32(ctx->Yi.c + 12, ctr);\n#endif\n else\n ctx->Yi.d[3] = ctr;\n while (len--) {\n ctx->Xi.c[n] ^= out[n] = in[n] ^ ctx->EKi.c[n];\n ++n;\n }\n }\n ctx->mres = n;\n return 0;\n}']
223
0
https://github.com/openssl/openssl/blob/8da94770f0a049497b1a52ee469cca1f4a13b1a7/crypto/bn/bn_shift.c/#L212
int BN_rshift(BIGNUM *r, const BIGNUM *a, int n) { int i, j, nw, lb, rb; BN_ULONG *t, *f; BN_ULONG l, tmp; bn_check_top(r); bn_check_top(a); if (n < 0) { BNerr(BN_F_BN_RSHIFT, BN_R_INVALID_SHIFT); return 0; } nw = n / BN_BITS2; rb = n % BN_BITS2; lb = BN_BITS2 - rb; if (nw >= a->top || a->top == 0) { BN_zero(r); return (1); } i = (BN_num_bits(a) - n + (BN_BITS2 - 1)) / BN_BITS2; if (r != a) { r->neg = a->neg; if (bn_wexpand(r, i) == NULL) return (0); } else { if (n == 0) return 1; } f = &(a->d[nw]); t = r->d; j = a->top - nw; r->top = i; if (rb == 0) { for (i = j; i != 0; i--) *(t++) = *(f++); } else { l = *(f++); for (i = j - 1; i != 0; i--) { tmp = (l >> rb) & BN_MASK2; l = *(f++); *(t++) = (tmp | (l << lb)) & BN_MASK2; } if ((l = (l >> rb) & BN_MASK2)) *(t) = l; } bn_check_top(r); return (1); }
['BIGNUM *int_bn_mod_inverse(BIGNUM *in,\n const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx,\n int *pnoinv)\n{\n BIGNUM *A, *B, *X, *Y, *M, *D, *T, *R = NULL;\n BIGNUM *ret = NULL;\n int sign;\n if (pnoinv)\n *pnoinv = 0;\n if ((BN_get_flags(a, BN_FLG_CONSTTIME) != 0)\n || (BN_get_flags(n, BN_FLG_CONSTTIME) != 0)) {\n return BN_mod_inverse_no_branch(in, a, n, ctx);\n }\n bn_check_top(a);\n bn_check_top(n);\n BN_CTX_start(ctx);\n A = BN_CTX_get(ctx);\n B = BN_CTX_get(ctx);\n X = BN_CTX_get(ctx);\n D = BN_CTX_get(ctx);\n M = BN_CTX_get(ctx);\n Y = BN_CTX_get(ctx);\n T = BN_CTX_get(ctx);\n if (T == NULL)\n goto err;\n if (in == NULL)\n R = BN_new();\n else\n R = in;\n if (R == NULL)\n goto err;\n BN_one(X);\n BN_zero(Y);\n if (BN_copy(B, a) == NULL)\n goto err;\n if (BN_copy(A, n) == NULL)\n goto err;\n A->neg = 0;\n if (B->neg || (BN_ucmp(B, A) >= 0)) {\n if (!BN_nnmod(B, B, A, ctx))\n goto err;\n }\n sign = -1;\n if (BN_is_odd(n) && (BN_num_bits(n) <= (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}', '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_rshift(BIGNUM *r, const BIGNUM *a, int n)\n{\n int i, j, nw, lb, rb;\n BN_ULONG *t, *f;\n BN_ULONG l, tmp;\n bn_check_top(r);\n bn_check_top(a);\n if (n < 0) {\n BNerr(BN_F_BN_RSHIFT, BN_R_INVALID_SHIFT);\n return 0;\n }\n nw = n / BN_BITS2;\n rb = n % BN_BITS2;\n lb = BN_BITS2 - rb;\n if (nw >= a->top || a->top == 0) {\n BN_zero(r);\n return (1);\n }\n i = (BN_num_bits(a) - n + (BN_BITS2 - 1)) / BN_BITS2;\n if (r != a) {\n r->neg = a->neg;\n if (bn_wexpand(r, i) == NULL)\n return (0);\n } else {\n if (n == 0)\n return 1;\n }\n f = &(a->d[nw]);\n t = r->d;\n j = a->top - nw;\n r->top = i;\n if (rb == 0) {\n for (i = j; i != 0; i--)\n *(t++) = *(f++);\n } else {\n l = *(f++);\n for (i = j - 1; i != 0; i--) {\n tmp = (l >> rb) & BN_MASK2;\n l = *(f++);\n *(t++) = (tmp | (l << lb)) & BN_MASK2;\n }\n if ((l = (l >> rb) & BN_MASK2))\n *(t) = l;\n }\n bn_check_top(r);\n return (1);\n}']
224
0
https://github.com/libav/libav/blob/66a71d989fdca27749bf4a965d0c0b2fe6d7b473/ffmpeg.c/#L4003
static void opt_vstats (void) { char filename[40]; time_t today2 = time(NULL); struct tm *today = localtime(&today2); snprintf(filename, sizeof(filename), "vstats_%02d%02d%02d.log", today->tm_hour, today->tm_min, today->tm_sec); opt_vstats_file(filename); }
['static void opt_vstats (void)\n{\n char filename[40];\n time_t today2 = time(NULL);\n struct tm *today = localtime(&today2);\n snprintf(filename, sizeof(filename), "vstats_%02d%02d%02d.log", today->tm_hour, today->tm_min,\n today->tm_sec);\n opt_vstats_file(filename);\n}']
225
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 int test_expmodone(void)\n{\n int ret = 0, i;\n BIGNUM *r = BN_new();\n BIGNUM *a = BN_new();\n BIGNUM *p = BN_new();\n BIGNUM *m = BN_new();\n if (!TEST_ptr(r)\n || !TEST_ptr(a)\n || !TEST_ptr(p)\n || !TEST_ptr(p)\n || !TEST_ptr(m)\n || !TEST_true(BN_set_word(a, 1))\n || !TEST_true(BN_set_word(p, 0))\n || !TEST_true(BN_set_word(m, 1)))\n goto err;\n for (i = 0; i < 2; i++) {\n if (!TEST_true(BN_mod_exp(r, a, p, m, NULL))\n || !TEST_BN_eq_zero(r)\n || !TEST_true(BN_mod_exp_mont(r, a, p, m, NULL, NULL))\n || !TEST_BN_eq_zero(r)\n || !TEST_true(BN_mod_exp_mont_consttime(r, a, p, m, NULL, NULL))\n || !TEST_BN_eq_zero(r)\n || !TEST_true(BN_mod_exp_mont_word(r, 1, p, m, NULL, NULL))\n || !TEST_BN_eq_zero(r)\n || !TEST_true(BN_mod_exp_simple(r, a, p, m, NULL))\n || !TEST_BN_eq_zero(r)\n || !TEST_true(BN_mod_exp_recp(r, a, p, m, NULL))\n || !TEST_BN_eq_zero(r))\n goto err;\n if (i == 0)\n BN_set_negative(m, 1);\n }\n ret = 1;\n err:\n BN_free(r);\n BN_free(a);\n BN_free(p);\n BN_free(m);\n return ret;\n}', 'int BN_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m,\n BN_CTX *ctx)\n{\n int ret;\n bn_check_top(a);\n bn_check_top(p);\n bn_check_top(m);\n#define MONT_MUL_MOD\n#define MONT_EXP_WORD\n#define RECP_MUL_MOD\n#ifdef MONT_MUL_MOD\n if (BN_is_odd(m)) {\n# ifdef MONT_EXP_WORD\n if (a->top == 1 && !a->neg\n && (BN_get_flags(p, BN_FLG_CONSTTIME) == 0)\n && (BN_get_flags(a, BN_FLG_CONSTTIME) == 0)\n && (BN_get_flags(m, BN_FLG_CONSTTIME) == 0)) {\n BN_ULONG A = a->d[0];\n ret = BN_mod_exp_mont_word(r, A, p, m, ctx, NULL);\n } else\n# endif\n ret = BN_mod_exp_mont(r, a, p, m, ctx, NULL);\n } else\n#endif\n#ifdef RECP_MUL_MOD\n {\n ret = BN_mod_exp_recp(r, a, p, m, ctx);\n }\n#else\n {\n ret = BN_mod_exp_simple(r, a, p, m, ctx);\n }\n#endif\n bn_check_top(r);\n return ret;\n}', 'int BN_mod_exp_mont_word(BIGNUM *rr, BN_ULONG a, const BIGNUM *p,\n const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont)\n{\n BN_MONT_CTX *mont = NULL;\n int b, bits, ret = 0;\n int r_is_one;\n BN_ULONG w, next_w;\n BIGNUM *r, *t;\n BIGNUM *swap_tmp;\n#define BN_MOD_MUL_WORD(r, w, m) \\\n (BN_mul_word(r, (w)) && \\\n ( \\\n (BN_mod(t, r, m, ctx) && (swap_tmp = r, r = t, t = swap_tmp, 1))))\n#define BN_TO_MONTGOMERY_WORD(r, w, mont) \\\n (BN_set_word(r, (w)) && BN_to_montgomery(r, r, (mont), ctx))\n if (BN_get_flags(p, BN_FLG_CONSTTIME) != 0\n || BN_get_flags(m, BN_FLG_CONSTTIME) != 0) {\n BNerr(BN_F_BN_MOD_EXP_MONT_WORD, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);\n return 0;\n }\n bn_check_top(p);\n bn_check_top(m);\n if (!BN_is_odd(m)) {\n BNerr(BN_F_BN_MOD_EXP_MONT_WORD, BN_R_CALLED_WITH_EVEN_MODULUS);\n return 0;\n }\n if (m->top == 1)\n a %= m->d[0];\n bits = BN_num_bits(p);\n if (bits == 0) {\n if (BN_abs_is_word(m, 1)) {\n ret = 1;\n BN_zero(rr);\n } else {\n ret = BN_one(rr);\n }\n return ret;\n }\n if (a == 0) {\n BN_zero(rr);\n ret = 1;\n return ret;\n }\n BN_CTX_start(ctx);\n r = BN_CTX_get(ctx);\n t = BN_CTX_get(ctx);\n if (t == NULL)\n goto err;\n if (in_mont != NULL)\n mont = in_mont;\n else {\n if ((mont = BN_MONT_CTX_new()) == NULL)\n goto err;\n if (!BN_MONT_CTX_set(mont, m, ctx))\n goto err;\n }\n r_is_one = 1;\n w = a;\n for (b = bits - 2; b >= 0; b--) {\n next_w = w * w;\n if ((next_w / w) != w) {\n if (r_is_one) {\n if (!BN_TO_MONTGOMERY_WORD(r, w, mont))\n goto err;\n r_is_one = 0;\n } else {\n if (!BN_MOD_MUL_WORD(r, w, m))\n goto err;\n }\n next_w = 1;\n }\n w = next_w;\n if (!r_is_one) {\n if (!BN_mod_mul_montgomery(r, r, r, mont, ctx))\n goto err;\n }\n if (BN_is_bit_set(p, b)) {\n next_w = w * a;\n if ((next_w / a) != w) {\n if (r_is_one) {\n if (!BN_TO_MONTGOMERY_WORD(r, w, mont))\n goto err;\n r_is_one = 0;\n } else {\n if (!BN_MOD_MUL_WORD(r, w, m))\n goto err;\n }\n next_w = a;\n }\n w = next_w;\n }\n }\n if (w != 1) {\n if (r_is_one) {\n if (!BN_TO_MONTGOMERY_WORD(r, w, mont))\n goto err;\n r_is_one = 0;\n } else {\n if (!BN_MOD_MUL_WORD(r, w, m))\n goto err;\n }\n }\n if (r_is_one) {\n if (!BN_one(rr))\n goto err;\n } else {\n if (!BN_from_montgomery(rr, r, mont, ctx))\n goto err;\n }\n ret = 1;\n err:\n if (in_mont == NULL)\n BN_MONT_CTX_free(mont);\n BN_CTX_end(ctx);\n bn_check_top(rr);\n return ret;\n}', 'int BN_mod_exp_mont(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p,\n const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont)\n{\n int i, j, bits, ret = 0, wstart, wend, window, wvalue;\n int start = 1;\n BIGNUM *d, *r;\n const BIGNUM *aa;\n BIGNUM *val[TABLE_SIZE];\n BN_MONT_CTX *mont = NULL;\n if (BN_get_flags(p, BN_FLG_CONSTTIME) != 0\n || BN_get_flags(a, BN_FLG_CONSTTIME) != 0\n || BN_get_flags(m, BN_FLG_CONSTTIME) != 0) {\n return BN_mod_exp_mont_consttime(rr, a, p, m, ctx, in_mont);\n }\n bn_check_top(a);\n bn_check_top(p);\n bn_check_top(m);\n if (!BN_is_odd(m)) {\n BNerr(BN_F_BN_MOD_EXP_MONT, BN_R_CALLED_WITH_EVEN_MODULUS);\n return 0;\n }\n bits = BN_num_bits(p);\n if (bits == 0) {\n if (BN_abs_is_word(m, 1)) {\n ret = 1;\n BN_zero(rr);\n } else {\n ret = BN_one(rr);\n }\n return ret;\n }\n BN_CTX_start(ctx);\n d = BN_CTX_get(ctx);\n r = BN_CTX_get(ctx);\n val[0] = BN_CTX_get(ctx);\n if (val[0] == NULL)\n goto err;\n if (in_mont != NULL)\n mont = in_mont;\n else {\n if ((mont = BN_MONT_CTX_new()) == NULL)\n goto err;\n if (!BN_MONT_CTX_set(mont, m, ctx))\n goto err;\n }\n if (a->neg || BN_ucmp(a, m) >= 0) {\n if (!BN_nnmod(val[0], a, m, ctx))\n goto err;\n aa = val[0];\n } else\n aa = a;\n if (!bn_to_mont_fixed_top(val[0], aa, mont, ctx))\n goto err;\n window = BN_window_bits_for_exponent_size(bits);\n if (window > 1) {\n if (!bn_mul_mont_fixed_top(d, val[0], val[0], mont, ctx))\n goto err;\n j = 1 << (window - 1);\n for (i = 1; i < j; i++) {\n if (((val[i] = BN_CTX_get(ctx)) == NULL) ||\n !bn_mul_mont_fixed_top(val[i], val[i - 1], d, mont, ctx))\n goto err;\n }\n }\n start = 1;\n wvalue = 0;\n wstart = bits - 1;\n wend = 0;\n#if 1\n j = m->top;\n if (m->d[j - 1] & (((BN_ULONG)1) << (BN_BITS2 - 1))) {\n if (bn_wexpand(r, j) == NULL)\n goto err;\n r->d[0] = (0 - m->d[0]) & BN_MASK2;\n for (i = 1; i < j; i++)\n r->d[i] = (~m->d[i]) & BN_MASK2;\n r->top = j;\n r->flags |= BN_FLG_FIXED_TOP;\n } else\n#endif\n if (!bn_to_mont_fixed_top(r, BN_value_one(), mont, ctx))\n goto err;\n for (;;) {\n if (BN_is_bit_set(p, wstart) == 0) {\n if (!start) {\n if (!bn_mul_mont_fixed_top(r, r, r, mont, ctx))\n goto err;\n }\n if (wstart == 0)\n break;\n wstart--;\n continue;\n }\n j = wstart;\n wvalue = 1;\n wend = 0;\n for (i = 1; i < window; i++) {\n if (wstart - i < 0)\n break;\n if (BN_is_bit_set(p, wstart - i)) {\n wvalue <<= (i - wend);\n wvalue |= 1;\n wend = i;\n }\n }\n j = wend + 1;\n if (!start)\n for (i = 0; i < j; i++) {\n if (!bn_mul_mont_fixed_top(r, r, r, mont, ctx))\n goto err;\n }\n if (!bn_mul_mont_fixed_top(r, r, val[wvalue >> 1], mont, ctx))\n goto err;\n wstart -= wend + 1;\n wvalue = 0;\n start = 0;\n if (wstart < 0)\n break;\n }\n#if defined(SPARC_T4_MONT)\n if (OPENSSL_sparcv9cap_P[0] & (SPARCV9_VIS3 | SPARCV9_PREFER_FPU)) {\n j = mont->N.top;\n val[0]->d[0] = 1;\n for (i = 1; i < j; i++)\n val[0]->d[i] = 0;\n val[0]->top = j;\n if (!BN_mod_mul_montgomery(rr, r, val[0], mont, ctx))\n goto err;\n } else\n#endif\n if (!BN_from_montgomery(rr, r, mont, ctx))\n goto err;\n ret = 1;\n err:\n if (in_mont == NULL)\n BN_MONT_CTX_free(mont);\n BN_CTX_end(ctx);\n bn_check_top(rr);\n return ret;\n}', 'int BN_mod_exp_mont_consttime(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p,\n const BIGNUM *m, BN_CTX *ctx,\n BN_MONT_CTX *in_mont)\n{\n int i, bits, ret = 0, window, wvalue, wmask, window0;\n int top;\n BN_MONT_CTX *mont = NULL;\n int numPowers;\n unsigned char *powerbufFree = NULL;\n int powerbufLen = 0;\n unsigned char *powerbuf = NULL;\n BIGNUM tmp, am;\n#if defined(SPARC_T4_MONT)\n unsigned int t4 = 0;\n#endif\n bn_check_top(a);\n bn_check_top(p);\n bn_check_top(m);\n if (!BN_is_odd(m)) {\n BNerr(BN_F_BN_MOD_EXP_MONT_CONSTTIME, BN_R_CALLED_WITH_EVEN_MODULUS);\n return 0;\n }\n top = m->top;\n bits = p->top * BN_BITS2;\n if (bits == 0) {\n if (BN_abs_is_word(m, 1)) {\n ret = 1;\n BN_zero(rr);\n } else {\n ret = BN_one(rr);\n }\n return ret;\n }\n BN_CTX_start(ctx);\n if (in_mont != NULL)\n mont = in_mont;\n else {\n if ((mont = BN_MONT_CTX_new()) == NULL)\n goto err;\n if (!BN_MONT_CTX_set(mont, m, ctx))\n goto err;\n }\n if (a->neg || BN_ucmp(a, m) >= 0) {\n BIGNUM *reduced = BN_CTX_get(ctx);\n if (reduced == NULL\n || !BN_nnmod(reduced, a, m, ctx)) {\n goto err;\n }\n a = reduced;\n }\n#ifdef RSAZ_ENABLED\n if ((16 == a->top) && (16 == p->top) && (BN_num_bits(m) == 1024)\n && rsaz_avx2_eligible()) {\n if (NULL == bn_wexpand(rr, 16))\n goto err;\n RSAZ_1024_mod_exp_avx2(rr->d, a->d, p->d, m->d, mont->RR.d,\n mont->n0[0]);\n rr->top = 16;\n rr->neg = 0;\n bn_correct_top(rr);\n ret = 1;\n goto err;\n } else if ((8 == a->top) && (8 == p->top) && (BN_num_bits(m) == 512)) {\n if (NULL == bn_wexpand(rr, 8))\n goto err;\n RSAZ_512_mod_exp(rr->d, a->d, p->d, m->d, mont->n0[0], mont->RR.d);\n rr->top = 8;\n rr->neg = 0;\n bn_correct_top(rr);\n ret = 1;\n goto err;\n }\n#endif\n window = BN_window_bits_for_ctime_exponent_size(bits);\n#if defined(SPARC_T4_MONT)\n if (window >= 5 && (top & 15) == 0 && top <= 64 &&\n (OPENSSL_sparcv9cap_P[1] & (CFR_MONTMUL | CFR_MONTSQR)) ==\n (CFR_MONTMUL | CFR_MONTSQR) && (t4 = OPENSSL_sparcv9cap_P[0]))\n window = 5;\n else\n#endif\n#if defined(OPENSSL_BN_ASM_MONT5)\n if (window >= 5) {\n window = 5;\n powerbufLen += top * sizeof(mont->N.d[0]);\n }\n#endif\n (void)0;\n numPowers = 1 << window;\n powerbufLen += sizeof(m->d[0]) * (top * numPowers +\n ((2 * top) >\n numPowers ? (2 * top) : numPowers));\n#ifdef alloca\n if (powerbufLen < 3072)\n powerbufFree =\n alloca(powerbufLen + MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH);\n else\n#endif\n if ((powerbufFree =\n OPENSSL_malloc(powerbufLen + MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH))\n == NULL)\n goto err;\n powerbuf = MOD_EXP_CTIME_ALIGN(powerbufFree);\n memset(powerbuf, 0, powerbufLen);\n#ifdef alloca\n if (powerbufLen < 3072)\n powerbufFree = NULL;\n#endif\n tmp.d = (BN_ULONG *)(powerbuf + sizeof(m->d[0]) * top * numPowers);\n am.d = tmp.d + top;\n tmp.top = am.top = 0;\n tmp.dmax = am.dmax = top;\n tmp.neg = am.neg = 0;\n tmp.flags = am.flags = BN_FLG_STATIC_DATA;\n#if 1\n if (m->d[top - 1] & (((BN_ULONG)1) << (BN_BITS2 - 1))) {\n tmp.d[0] = (0 - m->d[0]) & BN_MASK2;\n for (i = 1; i < top; i++)\n tmp.d[i] = (~m->d[i]) & BN_MASK2;\n tmp.top = top;\n } else\n#endif\n if (!bn_to_mont_fixed_top(&tmp, BN_value_one(), mont, ctx))\n goto err;\n if (!bn_to_mont_fixed_top(&am, a, mont, ctx))\n goto err;\n#if defined(SPARC_T4_MONT)\n if (t4) {\n typedef int (*bn_pwr5_mont_f) (BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n int bn_pwr5_mont_t4_8(BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n int bn_pwr5_mont_t4_16(BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n int bn_pwr5_mont_t4_24(BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n int bn_pwr5_mont_t4_32(BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n static const bn_pwr5_mont_f pwr5_funcs[4] = {\n bn_pwr5_mont_t4_8, bn_pwr5_mont_t4_16,\n bn_pwr5_mont_t4_24, bn_pwr5_mont_t4_32\n };\n bn_pwr5_mont_f pwr5_worker = pwr5_funcs[top / 16 - 1];\n typedef int (*bn_mul_mont_f) (BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0);\n int bn_mul_mont_t4_8(BN_ULONG *rp, const BN_ULONG *ap, const void *bp,\n const BN_ULONG *np, const BN_ULONG *n0);\n int bn_mul_mont_t4_16(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0);\n int bn_mul_mont_t4_24(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0);\n int bn_mul_mont_t4_32(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0);\n static const bn_mul_mont_f mul_funcs[4] = {\n bn_mul_mont_t4_8, bn_mul_mont_t4_16,\n bn_mul_mont_t4_24, bn_mul_mont_t4_32\n };\n bn_mul_mont_f mul_worker = mul_funcs[top / 16 - 1];\n void bn_mul_mont_vis3(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0, int num);\n void bn_mul_mont_t4(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0, int num);\n void bn_mul_mont_gather5_t4(BN_ULONG *rp, const BN_ULONG *ap,\n const void *table, const BN_ULONG *np,\n const BN_ULONG *n0, int num, int power);\n void bn_flip_n_scatter5_t4(const BN_ULONG *inp, size_t num,\n void *table, size_t power);\n void bn_gather5_t4(BN_ULONG *out, size_t num,\n void *table, size_t power);\n void bn_flip_t4(BN_ULONG *dst, BN_ULONG *src, size_t num);\n BN_ULONG *np = mont->N.d, *n0 = mont->n0;\n int stride = 5 * (6 - (top / 16 - 1));\n for (i = am.top; i < top; i++)\n am.d[i] = 0;\n for (i = tmp.top; i < top; i++)\n tmp.d[i] = 0;\n bn_flip_n_scatter5_t4(tmp.d, top, powerbuf, 0);\n bn_flip_n_scatter5_t4(am.d, top, powerbuf, 1);\n if (!(*mul_worker) (tmp.d, am.d, am.d, np, n0) &&\n !(*mul_worker) (tmp.d, am.d, am.d, np, n0))\n bn_mul_mont_vis3(tmp.d, am.d, am.d, np, n0, top);\n bn_flip_n_scatter5_t4(tmp.d, top, powerbuf, 2);\n for (i = 3; i < 32; i++) {\n if (!(*mul_worker) (tmp.d, tmp.d, am.d, np, n0) &&\n !(*mul_worker) (tmp.d, tmp.d, am.d, np, n0))\n bn_mul_mont_vis3(tmp.d, tmp.d, am.d, np, n0, top);\n bn_flip_n_scatter5_t4(tmp.d, top, powerbuf, i);\n }\n np = alloca(top * sizeof(BN_ULONG));\n top /= 2;\n bn_flip_t4(np, mont->N.d, top);\n window0 = (bits - 1) % 5 + 1;\n wmask = (1 << window0) - 1;\n bits -= window0;\n wvalue = bn_get_bits(p, bits) & wmask;\n bn_gather5_t4(tmp.d, top, powerbuf, wvalue);\n while (bits > 0) {\n if (bits < stride)\n stride = bits;\n bits -= stride;\n wvalue = bn_get_bits(p, bits);\n if ((*pwr5_worker) (tmp.d, np, n0, powerbuf, wvalue, stride))\n continue;\n if ((*pwr5_worker) (tmp.d, np, n0, powerbuf, wvalue, stride))\n continue;\n bits += stride - 5;\n wvalue >>= stride - 5;\n wvalue &= 31;\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_gather5_t4(tmp.d, tmp.d, powerbuf, np, n0, top,\n wvalue);\n }\n bn_flip_t4(tmp.d, tmp.d, top);\n top *= 2;\n tmp.top = top;\n bn_correct_top(&tmp);\n OPENSSL_cleanse(np, top * sizeof(BN_ULONG));\n } else\n#endif\n#if defined(OPENSSL_BN_ASM_MONT5)\n if (window == 5 && top > 1) {\n void bn_mul_mont_gather5(BN_ULONG *rp, const BN_ULONG *ap,\n const void *table, const BN_ULONG *np,\n const BN_ULONG *n0, int num, int power);\n void bn_scatter5(const BN_ULONG *inp, size_t num,\n void *table, size_t power);\n void bn_gather5(BN_ULONG *out, size_t num, void *table, size_t power);\n void bn_power5(BN_ULONG *rp, const BN_ULONG *ap,\n const void *table, const BN_ULONG *np,\n const BN_ULONG *n0, int num, int power);\n int bn_get_bits5(const BN_ULONG *ap, int off);\n int bn_from_montgomery(BN_ULONG *rp, const BN_ULONG *ap,\n const BN_ULONG *not_used, const BN_ULONG *np,\n const BN_ULONG *n0, int num);\n BN_ULONG *n0 = mont->n0, *np;\n for (i = am.top; i < top; i++)\n am.d[i] = 0;\n for (i = tmp.top; i < top; i++)\n tmp.d[i] = 0;\n for (np = am.d + top, i = 0; i < top; i++)\n np[i] = mont->N.d[i];\n bn_scatter5(tmp.d, top, powerbuf, 0);\n bn_scatter5(am.d, am.top, powerbuf, 1);\n bn_mul_mont(tmp.d, am.d, am.d, np, n0, top);\n bn_scatter5(tmp.d, top, powerbuf, 2);\n# if 0\n for (i = 3; i < 32; i++) {\n bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1);\n bn_scatter5(tmp.d, top, powerbuf, i);\n }\n# else\n for (i = 4; i < 32; i *= 2) {\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_scatter5(tmp.d, top, powerbuf, i);\n }\n for (i = 3; i < 8; i += 2) {\n int j;\n bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1);\n bn_scatter5(tmp.d, top, powerbuf, i);\n for (j = 2 * i; j < 32; j *= 2) {\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_scatter5(tmp.d, top, powerbuf, j);\n }\n }\n for (; i < 16; i += 2) {\n bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1);\n bn_scatter5(tmp.d, top, powerbuf, i);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_scatter5(tmp.d, top, powerbuf, 2 * i);\n }\n for (; i < 32; i += 2) {\n bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1);\n bn_scatter5(tmp.d, top, powerbuf, i);\n }\n# endif\n window0 = (bits - 1) % 5 + 1;\n wmask = (1 << window0) - 1;\n bits -= window0;\n wvalue = bn_get_bits(p, bits) & wmask;\n bn_gather5(tmp.d, top, powerbuf, wvalue);\n if (top & 7) {\n while (bits > 0) {\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_gather5(tmp.d, tmp.d, powerbuf, np, n0, top,\n bn_get_bits5(p->d, bits -= 5));\n }\n } else {\n while (bits > 0) {\n bn_power5(tmp.d, tmp.d, powerbuf, np, n0, top,\n bn_get_bits5(p->d, bits -= 5));\n }\n }\n ret = bn_from_montgomery(tmp.d, tmp.d, NULL, np, n0, top);\n tmp.top = top;\n bn_correct_top(&tmp);\n if (ret) {\n if (!BN_copy(rr, &tmp))\n ret = 0;\n goto err;\n }\n } else\n#endif\n {\n if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&tmp, top, powerbuf, 0, window))\n goto err;\n if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&am, top, powerbuf, 1, window))\n goto err;\n if (window > 1) {\n if (!bn_mul_mont_fixed_top(&tmp, &am, &am, mont, ctx))\n goto err;\n if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&tmp, top, powerbuf, 2,\n window))\n goto err;\n for (i = 3; i < numPowers; i++) {\n if (!bn_mul_mont_fixed_top(&tmp, &am, &tmp, mont, ctx))\n goto err;\n if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&tmp, top, powerbuf, i,\n window))\n goto err;\n }\n }\n window0 = (bits - 1) % window + 1;\n wmask = (1 << window0) - 1;\n bits -= window0;\n wvalue = bn_get_bits(p, bits) & wmask;\n if (!MOD_EXP_CTIME_COPY_FROM_PREBUF(&tmp, top, powerbuf, wvalue,\n window))\n goto err;\n wmask = (1 << window) - 1;\n while (bits > 0) {\n for (i = 0; i < window; i++)\n if (!bn_mul_mont_fixed_top(&tmp, &tmp, &tmp, mont, ctx))\n goto err;\n bits -= window;\n wvalue = bn_get_bits(p, bits) & wmask;\n if (!MOD_EXP_CTIME_COPY_FROM_PREBUF(&am, top, powerbuf, wvalue,\n window))\n goto err;\n if (!bn_mul_mont_fixed_top(&tmp, &tmp, &am, mont, ctx))\n goto err;\n }\n }\n#if defined(SPARC_T4_MONT)\n if (OPENSSL_sparcv9cap_P[0] & (SPARCV9_VIS3 | SPARCV9_PREFER_FPU)) {\n am.d[0] = 1;\n for (i = 1; i < top; i++)\n am.d[i] = 0;\n if (!BN_mod_mul_montgomery(rr, &tmp, &am, mont, ctx))\n goto err;\n } else\n#endif\n if (!BN_from_montgomery(rr, &tmp, mont, ctx))\n goto err;\n ret = 1;\n err:\n if (in_mont == NULL)\n BN_MONT_CTX_free(mont);\n if (powerbuf != NULL) {\n OPENSSL_cleanse(powerbuf, powerbufLen);\n OPENSSL_free(powerbufFree);\n }\n BN_CTX_end(ctx);\n return ret;\n}', 'int BN_mod_exp_simple(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,\n const BIGNUM *m, BN_CTX *ctx)\n{\n int i, j, bits, ret = 0, wstart, wend, window, wvalue;\n int start = 1;\n BIGNUM *d;\n BIGNUM *val[TABLE_SIZE];\n if (BN_get_flags(p, BN_FLG_CONSTTIME) != 0\n || BN_get_flags(a, BN_FLG_CONSTTIME) != 0\n || BN_get_flags(m, BN_FLG_CONSTTIME) != 0) {\n BNerr(BN_F_BN_MOD_EXP_SIMPLE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);\n return 0;\n }\n bits = BN_num_bits(p);\n if (bits == 0) {\n if (BN_abs_is_word(m, 1)) {\n ret = 1;\n BN_zero(r);\n } else {\n ret = BN_one(r);\n }\n return ret;\n }\n BN_CTX_start(ctx);\n d = BN_CTX_get(ctx);\n val[0] = BN_CTX_get(ctx);\n if (val[0] == NULL)\n goto err;\n if (!BN_nnmod(val[0], a, m, ctx))\n goto err;\n if (BN_is_zero(val[0])) {\n BN_zero(r);\n ret = 1;\n goto err;\n }\n window = BN_window_bits_for_exponent_size(bits);\n if (window > 1) {\n if (!BN_mod_mul(d, val[0], val[0], m, ctx))\n goto err;\n j = 1 << (window - 1);\n for (i = 1; i < j; i++) {\n if (((val[i] = BN_CTX_get(ctx)) == NULL) ||\n !BN_mod_mul(val[i], val[i - 1], d, m, ctx))\n goto err;\n }\n }\n start = 1;\n wvalue = 0;\n wstart = bits - 1;\n wend = 0;\n if (!BN_one(r))\n goto err;\n for (;;) {\n if (BN_is_bit_set(p, wstart) == 0) {\n if (!start)\n if (!BN_mod_mul(r, r, r, m, ctx))\n goto err;\n if (wstart == 0)\n break;\n wstart--;\n continue;\n }\n j = wstart;\n wvalue = 1;\n wend = 0;\n for (i = 1; i < window; i++) {\n if (wstart - i < 0)\n break;\n if (BN_is_bit_set(p, wstart - i)) {\n wvalue <<= (i - wend);\n wvalue |= 1;\n wend = i;\n }\n }\n j = wend + 1;\n if (!start)\n for (i = 0; i < j; i++) {\n if (!BN_mod_mul(r, r, r, m, ctx))\n goto err;\n }\n if (!BN_mod_mul(r, r, val[wvalue >> 1], m, ctx))\n goto err;\n wstart -= wend + 1;\n wvalue = 0;\n start = 0;\n if (wstart < 0)\n break;\n }\n ret = 1;\n err:\n BN_CTX_end(ctx);\n bn_check_top(r);\n return ret;\n}', 'int BN_mod_exp_recp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,\n const BIGNUM *m, BN_CTX *ctx)\n{\n int i, j, bits, ret = 0, wstart, wend, window, wvalue;\n int start = 1;\n BIGNUM *aa;\n BIGNUM *val[TABLE_SIZE];\n BN_RECP_CTX recp;\n if (BN_get_flags(p, BN_FLG_CONSTTIME) != 0\n || BN_get_flags(a, BN_FLG_CONSTTIME) != 0\n || BN_get_flags(m, BN_FLG_CONSTTIME) != 0) {\n BNerr(BN_F_BN_MOD_EXP_RECP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);\n return 0;\n }\n bits = BN_num_bits(p);\n if (bits == 0) {\n if (BN_abs_is_word(m, 1)) {\n ret = 1;\n BN_zero(r);\n } else {\n ret = BN_one(r);\n }\n return ret;\n }\n BN_CTX_start(ctx);\n aa = BN_CTX_get(ctx);\n val[0] = BN_CTX_get(ctx);\n if (val[0] == NULL)\n goto err;\n BN_RECP_CTX_init(&recp);\n if (m->neg) {\n if (!BN_copy(aa, m))\n goto err;\n aa->neg = 0;\n if (BN_RECP_CTX_set(&recp, aa, ctx) <= 0)\n goto err;\n } else {\n if (BN_RECP_CTX_set(&recp, m, ctx) <= 0)\n goto err;\n }\n if (!BN_nnmod(val[0], a, m, ctx))\n goto err;\n if (BN_is_zero(val[0])) {\n BN_zero(r);\n ret = 1;\n goto err;\n }\n window = BN_window_bits_for_exponent_size(bits);\n if (window > 1) {\n if (!BN_mod_mul_reciprocal(aa, val[0], val[0], &recp, ctx))\n goto err;\n j = 1 << (window - 1);\n for (i = 1; i < j; i++) {\n if (((val[i] = BN_CTX_get(ctx)) == NULL) ||\n !BN_mod_mul_reciprocal(val[i], val[i - 1], aa, &recp, ctx))\n goto err;\n }\n }\n start = 1;\n wvalue = 0;\n wstart = bits - 1;\n wend = 0;\n if (!BN_one(r))\n goto err;\n for (;;) {\n if (BN_is_bit_set(p, wstart) == 0) {\n if (!start)\n if (!BN_mod_mul_reciprocal(r, r, r, &recp, ctx))\n goto err;\n if (wstart == 0)\n break;\n wstart--;\n continue;\n }\n j = wstart;\n wvalue = 1;\n wend = 0;\n for (i = 1; i < window; i++) {\n if (wstart - i < 0)\n break;\n if (BN_is_bit_set(p, wstart - i)) {\n wvalue <<= (i - wend);\n wvalue |= 1;\n wend = i;\n }\n }\n j = wend + 1;\n if (!start)\n for (i = 0; i < j; i++) {\n if (!BN_mod_mul_reciprocal(r, r, r, &recp, ctx))\n goto err;\n }\n if (!BN_mod_mul_reciprocal(r, r, val[wvalue >> 1], &recp, ctx))\n goto err;\n wstart -= wend + 1;\n wvalue = 0;\n start = 0;\n if (wstart < 0)\n break;\n }\n ret = 1;\n err:\n BN_CTX_end(ctx);\n BN_RECP_CTX_free(&recp);\n bn_check_top(r);\n return ret;\n}', 'void BN_CTX_start(BN_CTX *ctx)\n{\n CTXDBG("ENTER BN_CTX_start()", ctx);\n if (ctx->err_stack || ctx->too_many)\n ctx->err_stack++;\n else if (!BN_STACK_push(&ctx->stack, ctx->used)) {\n BNerr(BN_F_BN_CTX_START, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n ctx->err_stack++;\n }\n CTXDBG("LEAVE BN_CTX_start()", ctx);\n}', 'int BN_nnmod(BIGNUM *r, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx)\n{\n if (!(BN_mod(r, m, d, ctx)))\n return 0;\n if (!r->neg)\n return 1;\n return (d->neg ? BN_sub : BN_add) (r, r, d);\n}', 'int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,\n BN_CTX *ctx)\n{\n int ret;\n if (BN_is_zero(divisor)) {\n BNerr(BN_F_BN_DIV, BN_R_DIV_BY_ZERO);\n return 0;\n }\n if (divisor->d[divisor->top - 1] == 0) {\n BNerr(BN_F_BN_DIV, BN_R_NOT_INITIALIZED);\n return 0;\n }\n ret = bn_div_fixed_top(dv, rm, num, divisor, ctx);\n if (ret) {\n if (dv != NULL)\n bn_correct_top(dv);\n if (rm != NULL)\n bn_correct_top(rm);\n }\n return ret;\n}', 'int bn_div_fixed_top(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num,\n const BIGNUM *divisor, BN_CTX *ctx)\n{\n int norm_shift, i, j, loop;\n BIGNUM *tmp, *snum, *sdiv, *res;\n BN_ULONG *resp, *wnum, *wnumtop;\n BN_ULONG d0, d1;\n int num_n, div_n;\n assert(divisor->top > 0 && divisor->d[divisor->top - 1] != 0);\n bn_check_top(num);\n bn_check_top(divisor);\n bn_check_top(dv);\n bn_check_top(rm);\n BN_CTX_start(ctx);\n res = (dv == NULL) ? BN_CTX_get(ctx) : dv;\n tmp = BN_CTX_get(ctx);\n snum = BN_CTX_get(ctx);\n sdiv = BN_CTX_get(ctx);\n if (sdiv == NULL)\n goto err;\n if (!BN_copy(sdiv, divisor))\n goto err;\n norm_shift = bn_left_align(sdiv);\n sdiv->neg = 0;\n if (!(bn_lshift_fixed_top(snum, num, norm_shift)))\n goto err;\n div_n = sdiv->top;\n num_n = snum->top;\n if (num_n <= div_n) {\n if (bn_wexpand(snum, div_n + 1) == NULL)\n goto err;\n memset(&(snum->d[num_n]), 0, (div_n - num_n + 1) * sizeof(BN_ULONG));\n snum->top = num_n = div_n + 1;\n }\n loop = num_n - div_n;\n wnum = &(snum->d[loop]);\n wnumtop = &(snum->d[num_n - 1]);\n d0 = sdiv->d[div_n - 1];\n d1 = (div_n == 1) ? 0 : sdiv->d[div_n - 2];\n if (!bn_wexpand(res, loop))\n goto err;\n res->neg = (num->neg ^ divisor->neg);\n res->top = loop;\n res->flags |= BN_FLG_FIXED_TOP;\n resp = &(res->d[loop]);\n if (!bn_wexpand(tmp, (div_n + 1)))\n goto err;\n for (i = 0; i < loop; i++, wnumtop--) {\n BN_ULONG q, l0;\n# if defined(BN_DIV3W)\n q = bn_div_3_words(wnumtop, d1, d0);\n# else\n BN_ULONG n0, n1, rem = 0;\n n0 = wnumtop[0];\n n1 = wnumtop[-1];\n if (n0 == d0)\n q = BN_MASK2;\n else {\n BN_ULONG n2 = (wnumtop == wnum) ? 0 : wnumtop[-2];\n# ifdef BN_LLONG\n BN_ULLONG t2;\n# if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)\n q = (BN_ULONG)(((((BN_ULLONG) n0) << BN_BITS2) | n1) / d0);\n# else\n q = bn_div_words(n0, n1, d0);\n# endif\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n t2 = (BN_ULLONG) d1 *q;\n for (;;) {\n if (t2 <= ((((BN_ULLONG) rem) << BN_BITS2) | n2))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n t2 -= d1;\n }\n# else\n BN_ULONG t2l, t2h;\n q = bn_div_words(n0, n1, d0);\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n# if defined(BN_UMULT_LOHI)\n BN_UMULT_LOHI(t2l, t2h, d1, q);\n# elif defined(BN_UMULT_HIGH)\n t2l = d1 * q;\n t2h = BN_UMULT_HIGH(d1, q);\n# else\n {\n BN_ULONG ql, qh;\n t2l = LBITS(d1);\n t2h = HBITS(d1);\n ql = LBITS(q);\n qh = HBITS(q);\n mul64(t2l, t2h, ql, qh);\n }\n# endif\n for (;;) {\n if ((t2h < rem) || ((t2h == rem) && (t2l <= n2)))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n if (t2l < d1)\n t2h--;\n t2l -= d1;\n }\n# endif\n }\n# endif\n l0 = bn_mul_words(tmp->d, sdiv->d, div_n, q);\n tmp->d[div_n] = l0;\n wnum--;\n l0 = bn_sub_words(wnum, wnum, tmp->d, div_n + 1);\n q -= l0;\n for (l0 = 0 - l0, j = 0; j < div_n; j++)\n tmp->d[j] = sdiv->d[j] & l0;\n l0 = bn_add_words(wnum, wnum, tmp->d, div_n);\n (*wnumtop) += l0;\n assert((*wnumtop) == 0);\n *--resp = q;\n }\n snum->neg = num->neg;\n snum->top = div_n;\n snum->flags |= BN_FLG_FIXED_TOP;\n if (rm != NULL)\n bn_rshift_fixed_top(rm, snum, norm_shift);\n BN_CTX_end(ctx);\n return 1;\n err:\n bn_check_top(rm);\n BN_CTX_end(ctx);\n return 0;\n}', 'void BN_CTX_end(BN_CTX *ctx)\n{\n CTXDBG("ENTER BN_CTX_end()", ctx);\n if (ctx->err_stack)\n ctx->err_stack--;\n else {\n unsigned int fp = BN_STACK_pop(&ctx->stack);\n if (fp < ctx->used)\n BN_POOL_release(&ctx->pool, ctx->used - fp);\n ctx->used = fp;\n ctx->too_many = 0;\n }\n CTXDBG("LEAVE BN_CTX_end()", ctx);\n}', 'static unsigned int BN_STACK_pop(BN_STACK *st)\n{\n return st->indexes[--(st->depth)];\n}']
226
0
https://github.com/openssl/openssl/blob/e334d78b87517652cc34825aff7b6fc7be9a1e83/crypto/lhash/lhash.c/#L359
static void contract(LHASH *lh) { LHASH_NODE **n,*n1,*np; np=lh->b[lh->p+lh->pmax-1]; lh->b[lh->p+lh->pmax-1]=NULL; if (lh->p == 0) { n=(LHASH_NODE **)Realloc((char *)lh->b, (unsigned int)(sizeof(LHASH_NODE *)*lh->pmax)); if (n == NULL) { lh->error++; return; } lh->num_contract_reallocs++; lh->num_alloc_nodes/=2; lh->pmax/=2; lh->p=lh->pmax-1; lh->b=n; } else lh->p--; lh->num_nodes--; lh->num_contracts++; n1=lh->b[(int)lh->p]; if (n1 == NULL) lh->b[(int)lh->p]=np; else { while (n1->next != NULL) n1=n1->next; n1->next=np; } }
['static SSL *doConnection(SSL *scon)\n\t{\n\tBIO *conn;\n\tSSL *serverCon;\n\tint width, i;\n\tfd_set readfds;\n\tif ((conn=BIO_new(BIO_s_connect())) == NULL)\n\t\treturn(NULL);\n\tBIO_set_conn_hostname(conn,host);\n\tif (scon == NULL)\n\t\tserverCon=(SSL *)SSL_new(tm_ctx);\n\telse\n\t\t{\n\t\tserverCon=scon;\n\t\tSSL_set_connect_state(serverCon);\n\t\t}\n\tSSL_set_bio(serverCon,conn,conn);\n#if 0\n\tif( scon != NULL )\n\t\tSSL_set_session(serverCon,SSL_get_session(scon));\n#endif\n\tfor(;;) {\n\t\ti=SSL_connect(serverCon);\n\t\tif (BIO_sock_should_retry(i))\n\t\t\t{\n\t\t\tBIO_printf(bio_err,"DELAY\\n");\n\t\t\ti=SSL_get_fd(serverCon);\n\t\t\twidth=i+1;\n\t\t\tFD_ZERO(&readfds);\n\t\t\tFD_SET(i,&readfds);\n\t\t\tselect(width,(void *)&readfds,NULL,NULL,NULL);\n\t\t\tcontinue;\n\t\t\t}\n\t\tbreak;\n\t\t}\n\tif(i <= 0)\n\t\t{\n\t\tBIO_printf(bio_err,"ERROR\\n");\n\t\tif (verify_error != X509_V_OK)\n\t\t\tBIO_printf(bio_err,"verify error:%s\\n",\n\t\t\t\tX509_verify_cert_error_string(verify_error));\n\t\telse\n\t\t\tERR_print_errors(bio_err);\n\t\tif (scon == NULL)\n\t\t\tSSL_free(serverCon);\n\t\treturn NULL;\n\t\t}\n\treturn serverCon;\n\t}', 'SSL *SSL_new(SSL_CTX *ctx)\n\t{\n\tSSL *s;\n\tif (ctx == NULL)\n\t\t{\n\t\tSSLerr(SSL_F_SSL_NEW,SSL_R_NULL_SSL_CTX);\n\t\treturn(NULL);\n\t\t}\n\tif (ctx->method == NULL)\n\t\t{\n\t\tSSLerr(SSL_F_SSL_NEW,SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);\n\t\treturn(NULL);\n\t\t}\n\ts=(SSL *)Malloc(sizeof(SSL));\n\tif (s == NULL) goto err;\n\tmemset(s,0,sizeof(SSL));\n\tif (ctx->cert != NULL)\n\t\t{\n\t\ts->cert = ssl_cert_dup(ctx->cert);\n\t\tif (s->cert == NULL)\n\t\t\tgoto err;\n\t\t}\n\telse\n\t\ts->cert=NULL;\n\ts->sid_ctx_length=ctx->sid_ctx_length;\n\tmemcpy(&s->sid_ctx,&ctx->sid_ctx,sizeof(s->sid_ctx));\n\ts->verify_mode=ctx->verify_mode;\n\ts->verify_depth=ctx->verify_depth;\n\ts->verify_callback=ctx->default_verify_callback;\n\tCRYPTO_add(&ctx->references,1,CRYPTO_LOCK_SSL_CTX);\n\ts->ctx=ctx;\n\ts->verify_result=X509_V_OK;\n\ts->method=ctx->method;\n\tif (!s->method->ssl_new(s))\n\t\tgoto err;\n\ts->quiet_shutdown=ctx->quiet_shutdown;\n\ts->references=1;\n\ts->server=(ctx->method->ssl_accept == ssl_undefined_function)?0:1;\n\ts->options=ctx->options;\n\ts->mode=ctx->mode;\n\tSSL_clear(s);\n\tCRYPTO_new_ex_data(ssl_meth,(char *)s,&s->ex_data);\n\treturn(s);\nerr:\n\tif (s != NULL)\n\t\t{\n\t\tif (s->cert != NULL)\n\t\t\tssl_cert_free(s->cert);\n\t\tif (s->ctx != NULL)\n\t\t\tSSL_CTX_free(s->ctx);\n\t\tFree(s);\n\t\t}\n\tSSLerr(SSL_F_SSL_NEW,ERR_R_MALLOC_FAILURE);\n\treturn(NULL);\n\t}', 'int SSL_clear(SSL *s)\n\t{\n\tint state;\n\tif (s->method == NULL)\n\t\t{\n\t\tSSLerr(SSL_F_SSL_CLEAR,SSL_R_NO_METHOD_SPECIFIED);\n\t\treturn(0);\n\t\t}\n\ts->error=0;\n\ts->hit=0;\n\ts->shutdown=0;\n#if 0\n\tif (s->new_session) return(1);\n#endif\n\tstate=s->state;\n\ts->type=0;\n\ts->state=SSL_ST_BEFORE|((s->server)?SSL_ST_ACCEPT:SSL_ST_CONNECT);\n\ts->version=s->method->version;\n\ts->client_version=s->version;\n\ts->rwstate=SSL_NOTHING;\n\ts->rstate=SSL_ST_READ_HEADER;\n\ts->read_ahead=s->ctx->read_ahead;\n\tif (s->init_buf != NULL)\n\t\t{\n\t\tBUF_MEM_free(s->init_buf);\n\t\ts->init_buf=NULL;\n\t\t}\n\tssl_clear_cipher_ctx(s);\n\tif (ssl_clear_bad_session(s))\n\t\t{\n\t\tSSL_SESSION_free(s->session);\n\t\ts->session=NULL;\n\t\t}\n\ts->first_packet=0;\n#if 1\n\tif ((s->session == NULL) && (s->method != s->ctx->method))\n\t\t{\n\t\ts->method->ssl_free(s);\n\t\ts->method=s->ctx->method;\n\t\tif (!s->method->ssl_new(s))\n\t\t\treturn(0);\n\t\t}\n\telse\n#endif\n\t\ts->method->ssl_clear(s);\n\treturn(1);\n\t}', 'int ssl_clear_bad_session(SSL *s)\n\t{\n\tif (\t(s->session != NULL) &&\n\t\t!(s->shutdown & SSL_SENT_SHUTDOWN) &&\n\t\t!(SSL_in_init(s) || SSL_in_before(s)))\n\t\t{\n\t\tSSL_CTX_remove_session(s->ctx,s->session);\n\t\treturn(1);\n\t\t}\n\telse\n\t\treturn(0);\n\t}', 'int SSL_CTX_remove_session(SSL_CTX *ctx, SSL_SESSION *c)\n{\n\treturn remove_session_lock(ctx, c, 1);\n}', 'static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lck)\n\t{\n\tSSL_SESSION *r;\n\tint ret=0;\n\tif ((c != NULL) && (c->session_id_length != 0))\n\t\t{\n\t\tif(lck) CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);\n\t\tr=(SSL_SESSION *)lh_delete(ctx->sessions,(char *)c);\n\t\tif (r != NULL)\n\t\t\t{\n\t\t\tret=1;\n\t\t\tSSL_SESSION_list_remove(ctx,c);\n\t\t\t}\n\t\tif(lck) CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);\n\t\tif (ret)\n\t\t\t{\n\t\t\tr->not_resumable=1;\n\t\t\tif (ctx->remove_session_cb != NULL)\n\t\t\t\tctx->remove_session_cb(ctx,r);\n\t\t\tSSL_SESSION_free(r);\n\t\t\t}\n\t\t}\n\telse\n\t\tret=0;\n\treturn(ret);\n\t}', 'char *lh_delete(LHASH *lh, char *data)\n\t{\n\tunsigned long hash;\n\tLHASH_NODE *nn,**rn;\n\tchar *ret;\n\tlh->error=0;\n\trn=getrn(lh,data,&hash);\n\tif (*rn == NULL)\n\t\t{\n\t\tlh->num_no_delete++;\n\t\treturn(NULL);\n\t\t}\n\telse\n\t\t{\n\t\tnn= *rn;\n\t\t*rn=nn->next;\n\t\tret=nn->data;\n\t\tFree((char *)nn);\n\t\tlh->num_delete++;\n\t\t}\n\tlh->num_items--;\n\tif ((lh->num_nodes > MIN_NODES) &&\n\t\t(lh->down_load >= (lh->num_items*LH_LOAD_MULT/lh->num_nodes)))\n\t\tcontract(lh);\n\treturn(ret);\n\t}', 'static void contract(LHASH *lh)\n\t{\n\tLHASH_NODE **n,*n1,*np;\n\tnp=lh->b[lh->p+lh->pmax-1];\n\tlh->b[lh->p+lh->pmax-1]=NULL;\n\tif (lh->p == 0)\n\t\t{\n\t\tn=(LHASH_NODE **)Realloc((char *)lh->b,\n\t\t\t(unsigned int)(sizeof(LHASH_NODE *)*lh->pmax));\n\t\tif (n == NULL)\n\t\t\t{\n\t\t\tlh->error++;\n\t\t\treturn;\n\t\t\t}\n\t\tlh->num_contract_reallocs++;\n\t\tlh->num_alloc_nodes/=2;\n\t\tlh->pmax/=2;\n\t\tlh->p=lh->pmax-1;\n\t\tlh->b=n;\n\t\t}\n\telse\n\t\tlh->p--;\n\tlh->num_nodes--;\n\tlh->num_contracts++;\n\tn1=lh->b[(int)lh->p];\n\tif (n1 == NULL)\n\t\tlh->b[(int)lh->p]=np;\n\telse\n\t\t{\n\t\twhile (n1->next != NULL)\n\t\t\tn1=n1->next;\n\t\tn1->next=np;\n\t\t}\n\t}']
227
0
https://github.com/libav/libav/blob/4391805916a1557278351f25428d0145b1073520/libavcodec/smacker.c/#L311
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 { if (smacker_decode_header_tree(smk, &gb, &smk->mmap_tbl, smk->mmap_last, mmap_size)) return -1; } 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 { if (smacker_decode_header_tree(smk, &gb, &smk->mclr_tbl, smk->mclr_last, mclr_size)) return -1; } 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 { if (smacker_decode_header_tree(smk, &gb, &smk->full_tbl, smk->full_last, full_size)) return -1; } 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 { if (smacker_decode_header_tree(smk, &gb, &smk->type_tbl, smk->type_last, type_size)) return -1; } 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 if (smacker_decode_header_tree(smk, &gb, &smk->mmap_tbl, smk->mmap_last, mmap_size))\n return -1;\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 if (smacker_decode_header_tree(smk, &gb, &smk->mclr_tbl, smk->mclr_last, mclr_size))\n return -1;\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 if (smacker_decode_header_tree(smk, &gb, &smk->full_tbl, smk->full_last, full_size))\n return -1;\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 if (smacker_decode_header_tree(smk, &gb, &smk->type_tbl, smk->type_last, type_size))\n return -1;\n }\n return 0;\n}', 'static inline void init_get_bits(GetBitContext *s,\n const uint8_t *buffer, int bit_size)\n{\n int buffer_size = (bit_size+7)>>3;\n if (buffer_size < 0 || bit_size < 0) {\n buffer_size = bit_size = 0;\n buffer = NULL;\n }\n s->buffer = buffer;\n s->size_in_bits = bit_size;\n#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 unsigned int get_bits1(GetBitContext *s){\n unsigned int index = s->index;\n uint8_t result = s->buffer[index>>3];\n#ifdef ALT_BITSTREAM_READER_LE\n result >>= index & 7;\n result &= 1;\n#else\n result <<= index & 7;\n result >>= 8 - 1;\n#endif\n#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}', '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}']
228
0
https://github.com/openssl/openssl/blob/a68d8c7b77a3d46d591b89cfd0ecd2a2242e4613/crypto/rsa/rsa_oaep.c/#L284
int PKCS1_MGF1(unsigned char *mask, long len, const unsigned char *seed, long seedlen, const EVP_MD *dgst) { long i, outlen = 0; unsigned char cnt[4]; EVP_MD_CTX *c = EVP_MD_CTX_new(); unsigned char md[EVP_MAX_MD_SIZE]; int mdlen; int rv = -1; if (c == NULL) goto err; mdlen = EVP_MD_size(dgst); if (mdlen < 0) goto err; for (i = 0; outlen < len; i++) { cnt[0] = (unsigned char)((i >> 24) & 255); cnt[1] = (unsigned char)((i >> 16) & 255); cnt[2] = (unsigned char)((i >> 8)) & 255; cnt[3] = (unsigned char)(i & 255); if (!EVP_DigestInit_ex(c, dgst, NULL) || !EVP_DigestUpdate(c, seed, seedlen) || !EVP_DigestUpdate(c, cnt, 4)) goto err; if (outlen + mdlen <= len) { if (!EVP_DigestFinal_ex(c, mask + outlen, NULL)) goto err; outlen += mdlen; } else { if (!EVP_DigestFinal_ex(c, md, NULL)) goto err; memcpy(mask + outlen, md, len - outlen); outlen = len; } } rv = 0; err: EVP_MD_CTX_free(c); return rv; }
['int PKCS1_MGF1(unsigned char *mask, long len,\n const unsigned char *seed, long seedlen, const EVP_MD *dgst)\n{\n long i, outlen = 0;\n unsigned char cnt[4];\n EVP_MD_CTX *c = EVP_MD_CTX_new();\n unsigned char md[EVP_MAX_MD_SIZE];\n int mdlen;\n int rv = -1;\n if (c == NULL)\n goto err;\n mdlen = EVP_MD_size(dgst);\n if (mdlen < 0)\n goto err;\n for (i = 0; outlen < len; i++) {\n cnt[0] = (unsigned char)((i >> 24) & 255);\n cnt[1] = (unsigned char)((i >> 16) & 255);\n cnt[2] = (unsigned char)((i >> 8)) & 255;\n cnt[3] = (unsigned char)(i & 255);\n if (!EVP_DigestInit_ex(c, dgst, NULL)\n || !EVP_DigestUpdate(c, seed, seedlen)\n || !EVP_DigestUpdate(c, cnt, 4))\n goto err;\n if (outlen + mdlen <= len) {\n if (!EVP_DigestFinal_ex(c, mask + outlen, NULL))\n goto err;\n outlen += mdlen;\n } else {\n if (!EVP_DigestFinal_ex(c, md, NULL))\n goto err;\n memcpy(mask + outlen, md, len - outlen);\n outlen = len;\n }\n }\n rv = 0;\n err:\n EVP_MD_CTX_free(c);\n return rv;\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 osslargused(file); osslargused(line);\n ret = malloc(num);\n#endif\n return ret;\n}', 'int EVP_MD_size(const EVP_MD *md)\n{\n if (!md) {\n EVPerr(EVP_F_EVP_MD_SIZE, EVP_R_MESSAGE_DIGEST_IS_NULL);\n return -1;\n }\n return md->md_size;\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}']
229
0
https://github.com/openssl/openssl/blob/a21285b3636a8356f01027416b0cd43b016f58ca/crypto/bn/bn_lib.c/#L232
static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words) { BN_ULONG *a = NULL; if (words > (INT_MAX / (4 * BN_BITS2))) { BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG); return NULL; } if (BN_get_flags(b, BN_FLG_STATIC_DATA)) { BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA); return NULL; } if (BN_get_flags(b, BN_FLG_SECURE)) a = OPENSSL_secure_zalloc(words * sizeof(*a)); else a = OPENSSL_zalloc(words * sizeof(*a)); if (a == NULL) { BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE); return NULL; } assert(b->top <= words); if (b->top > 0) memcpy(a, b->d, sizeof(*a) * b->top); return a; }
['int BN_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_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)\n{\n int ret, r_neg, cmp_res;\n bn_check_top(a);\n bn_check_top(b);\n if (a->neg != b->neg) {\n r_neg = a->neg;\n ret = BN_uadd(r, a, b);\n } else {\n cmp_res = BN_ucmp(a, b);\n if (cmp_res > 0) {\n r_neg = a->neg;\n ret = BN_usub(r, a, b);\n } else if (cmp_res < 0) {\n r_neg = !b->neg;\n ret = BN_usub(r, b, a);\n } else {\n r_neg = 0;\n BN_zero(r);\n ret = 1;\n }\n }\n r->neg = r_neg;\n bn_check_top(r);\n return ret;\n}', 'int BN_usub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)\n{\n int max, min, dif;\n BN_ULONG t1, t2, borrow, *rp;\n const BN_ULONG *ap, *bp;\n bn_check_top(a);\n bn_check_top(b);\n max = a->top;\n min = b->top;\n dif = max - min;\n if (dif < 0) {\n BNerr(BN_F_BN_USUB, BN_R_ARG2_LT_ARG3);\n return 0;\n }\n if (bn_wexpand(r, max) == NULL)\n return 0;\n ap = a->d;\n bp = b->d;\n rp = r->d;\n borrow = bn_sub_words(rp, ap, bp, min);\n ap += min;\n rp += min;\n while (dif) {\n dif--;\n t1 = *(ap++);\n t2 = (t1 - borrow) & BN_MASK2;\n *(rp++) = t2;\n borrow &= (t1 == 0);\n }\n while (max && *--rp == 0)\n max--;\n r->top = max;\n r->neg = 0;\n bn_pollute(r);\n return 1;\n}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n return (words <= a->dmax) ? a : bn_expand2(a, words);\n}', 'BIGNUM *bn_expand2(BIGNUM *b, int words)\n{\n if (words > b->dmax) {\n BN_ULONG *a = bn_expand_internal(b, words);\n if (!a)\n return NULL;\n if (b->d) {\n OPENSSL_cleanse(b->d, b->dmax * sizeof(b->d[0]));\n bn_free_d(b);\n }\n b->d = a;\n b->dmax = words;\n }\n return b;\n}', 'static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)\n{\n BN_ULONG *a = NULL;\n if (words > (INT_MAX / (4 * BN_BITS2))) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);\n return NULL;\n }\n if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);\n return NULL;\n }\n if (BN_get_flags(b, BN_FLG_SECURE))\n a = OPENSSL_secure_zalloc(words * sizeof(*a));\n else\n a = OPENSSL_zalloc(words * sizeof(*a));\n if (a == NULL) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);\n return NULL;\n }\n assert(b->top <= words);\n if (b->top > 0)\n memcpy(a, b->d, sizeof(*a) * b->top);\n return a;\n}']
230
0
https://github.com/libav/libav/blob/fa0912fe50e59df72b7bf81f8838d2c6d9780343/libswscale/swscale.c/#L3378
static SwsVector *sws_sumVec(SwsVector *a, SwsVector *b){ int length= FFMAX(a->length, b->length); double *coeff= av_malloc(length*sizeof(double)); int i; SwsVector *vec= av_malloc(sizeof(SwsVector)); vec->coeff= coeff; vec->length= length; for (i=0; i<length; i++) coeff[i]= 0.0; for (i=0; i<a->length; i++) coeff[i + (length-1)/2 - (a->length-1)/2]+= a->coeff[i]; for (i=0; i<b->length; i++) coeff[i + (length-1)/2 - (b->length-1)/2]+= b->coeff[i]; return vec; }
['static SwsVector *sws_sumVec(SwsVector *a, SwsVector *b){\n int length= FFMAX(a->length, b->length);\n double *coeff= av_malloc(length*sizeof(double));\n int i;\n SwsVector *vec= av_malloc(sizeof(SwsVector));\n vec->coeff= coeff;\n vec->length= length;\n for (i=0; i<length; i++) coeff[i]= 0.0;\n for (i=0; i<a->length; i++) coeff[i + (length-1)/2 - (a->length-1)/2]+= a->coeff[i];\n for (i=0; i<b->length; i++) coeff[i + (length-1)/2 - (b->length-1)/2]+= b->coeff[i];\n return vec;\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}']
231
0
https://github.com/openssl/openssl/blob/6d4c65835de4ed9aa0a37e3234055d075fe0bc58/crypto/bn/bn_ctx.c/#L355
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\t\t const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont)\n\t{\n\tint i,bits,ret=0,window,wvalue;\n\tint top;\n\tBN_MONT_CTX *mont=NULL;\n\tint numPowers;\n\tunsigned char *powerbufFree=NULL;\n\tint powerbufLen = 0;\n\tunsigned char *powerbuf=NULL;\n\tBIGNUM tmp, am;\n\tbn_check_top(a);\n\tbn_check_top(p);\n\tbn_check_top(m);\n\ttop = m->top;\n\tif (!(m->d[0] & 1))\n\t\t{\n\t\tBNerr(BN_F_BN_MOD_EXP_MONT_CONSTTIME,BN_R_CALLED_WITH_EVEN_MODULUS);\n\t\treturn(0);\n\t\t}\n\tbits=BN_num_bits(p);\n\tif (bits == 0)\n\t\t{\n\t\tret = BN_one(rr);\n\t\treturn ret;\n\t\t}\n\tBN_CTX_start(ctx);\n\tif (in_mont != NULL)\n\t\tmont=in_mont;\n\telse\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\twindow = BN_window_bits_for_ctime_exponent_size(bits);\n#if defined(OPENSSL_BN_ASM_MONT5)\n\tif (window==6 && bits<=1024) window=5;\n#endif\n\tnumPowers = 1 << window;\n\tpowerbufLen = sizeof(m->d[0])*(top*numPowers +\n\t\t\t\t((2*top)>numPowers?(2*top):numPowers));\n#ifdef alloca\n\tif (powerbufLen < 3072)\n\t\tpowerbufFree = alloca(powerbufLen+MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH);\n\telse\n#endif\n\tif ((powerbufFree=(unsigned char*)OPENSSL_malloc(powerbufLen+MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH)) == NULL)\n\t\tgoto err;\n\tpowerbuf = MOD_EXP_CTIME_ALIGN(powerbufFree);\n\tmemset(powerbuf, 0, powerbufLen);\n#ifdef alloca\n\tif (powerbufLen < 3072)\n\t\tpowerbufFree = NULL;\n#endif\n\ttmp.d = (BN_ULONG *)(powerbuf + sizeof(m->d[0])*top*numPowers);\n\tam.d = tmp.d + top;\n\ttmp.top = am.top = 0;\n\ttmp.dmax = am.dmax = top;\n\ttmp.neg = am.neg = 0;\n\ttmp.flags = am.flags = BN_FLG_STATIC_DATA;\n#if 1\n \tif (!BN_to_montgomery(&tmp,BN_value_one(),mont,ctx))\tgoto err;\n#else\n\ttmp.d[0] = (0-m->d[0])&BN_MASK2;\n\tfor (i=1;i<top;i++)\n\t\ttmp.d[i] = (~m->d[i])&BN_MASK2;\n\ttmp.top = top;\n#endif\n\tif (a->neg || BN_ucmp(a,m) >= 0)\n\t\t{\n\t\tif (!BN_mod(&am,a,m,ctx))\t\t\tgoto err;\n\t\tif (!BN_to_montgomery(&am,&am,mont,ctx))\tgoto err;\n\t\t}\n\telse\tif (!BN_to_montgomery(&am,a,mont,ctx))\t\tgoto err;\n#if defined(OPENSSL_BN_ASM_MONT5)\n if (window==5)\n\t{\n\tvoid bn_mul_mont_gather5(BN_ULONG *rp,const BN_ULONG *ap,\n\t\t\tconst void *table,const BN_ULONG *np,\n\t\t\tconst BN_ULONG *n0,int num,int power);\n\tvoid bn_scatter5(const BN_ULONG *inp,size_t num,\n\t\t\tvoid *table,size_t power);\n\tvoid bn_gather5(BN_ULONG *out,size_t num,\n\t\t\tvoid *table,size_t power);\n\tBN_ULONG *np=mont->N.d, *n0=mont->n0;\n\tbn_scatter5(tmp.d,top,powerbuf,0);\n\tbn_scatter5(am.d,am.top,powerbuf,1);\n\tbn_mul_mont(tmp.d,am.d,am.d,np,n0,top);\n\tbn_scatter5(tmp.d,top,powerbuf,2);\n#if 0\n\tfor (i=3; i<32; i++)\n\t\t{\n\t\tbn_mul_mont_gather5(tmp.d,am.d,powerbuf,np,n0,top,i-1);\n\t\tbn_scatter5(tmp.d,top,powerbuf,i);\n\t\t}\n#else\n\tfor (i=4; i<32; i*=2)\n\t\t{\n\t\tbn_mul_mont(tmp.d,tmp.d,tmp.d,np,n0,top);\n\t\tbn_scatter5(tmp.d,top,powerbuf,i);\n\t\t}\n\tfor (i=3; i<8; i+=2)\n\t\t{\n\t\tint j;\n\t\tbn_mul_mont_gather5(tmp.d,am.d,powerbuf,np,n0,top,i-1);\n\t\tbn_scatter5(tmp.d,top,powerbuf,i);\n\t\tfor (j=2*i; j<32; j*=2)\n\t\t\t{\n\t\t\tbn_mul_mont(tmp.d,tmp.d,tmp.d,np,n0,top);\n\t\t\tbn_scatter5(tmp.d,top,powerbuf,j);\n\t\t\t}\n\t\t}\n\tfor (; i<16; i+=2)\n\t\t{\n\t\tbn_mul_mont_gather5(tmp.d,am.d,powerbuf,np,n0,top,i-1);\n\t\tbn_scatter5(tmp.d,top,powerbuf,i);\n\t\tbn_mul_mont(tmp.d,tmp.d,tmp.d,np,n0,top);\n\t\tbn_scatter5(tmp.d,top,powerbuf,2*i);\n\t\t}\n\tfor (; i<32; i+=2)\n\t\t{\n\t\tbn_mul_mont_gather5(tmp.d,am.d,powerbuf,np,n0,top,i-1);\n\t\tbn_scatter5(tmp.d,top,powerbuf,i);\n\t\t}\n#endif\n\tbits--;\n\tfor (wvalue=0, i=bits%5; i>=0; i--,bits--)\n\t\twvalue = (wvalue<<1)+BN_is_bit_set(p,bits);\n\tbn_gather5(tmp.d,top,powerbuf,wvalue);\n\twhile (bits >= 0)\n\t\t{\n\t\tfor (wvalue=0, i=0; i<5; i++,bits--)\n\t\t\twvalue = (wvalue<<1)+BN_is_bit_set(p,bits);\n\t\tbn_mul_mont(tmp.d,tmp.d,tmp.d,np,n0,top);\n\t\tbn_mul_mont(tmp.d,tmp.d,tmp.d,np,n0,top);\n\t\tbn_mul_mont(tmp.d,tmp.d,tmp.d,np,n0,top);\n\t\tbn_mul_mont(tmp.d,tmp.d,tmp.d,np,n0,top);\n\t\tbn_mul_mont(tmp.d,tmp.d,tmp.d,np,n0,top);\n\t\tbn_mul_mont_gather5(tmp.d,tmp.d,powerbuf,np,n0,top,wvalue);\n\t\t}\n\ttmp.top=top;\n\tbn_correct_top(&tmp);\n\t}\n else\n#endif\n\t{\n\tif (!MOD_EXP_CTIME_COPY_TO_PREBUF(&tmp, top, powerbuf, 0, numPowers)) goto err;\n\tif (!MOD_EXP_CTIME_COPY_TO_PREBUF(&am, top, powerbuf, 1, numPowers)) goto err;\n\tif (window > 1)\n\t\t{\n\t\tif (!BN_mod_mul_montgomery(&tmp,&am,&am,mont,ctx))\tgoto err;\n\t\tif (!MOD_EXP_CTIME_COPY_TO_PREBUF(&tmp, top, powerbuf, 2, numPowers)) goto err;\n\t\tfor (i=3; i<numPowers; i++)\n\t\t\t{\n\t\t\tif (!BN_mod_mul_montgomery(&tmp,&am,&tmp,mont,ctx))\n\t\t\t\tgoto err;\n\t\t\tif (!MOD_EXP_CTIME_COPY_TO_PREBUF(&tmp, top, powerbuf, i, numPowers)) goto err;\n\t\t\t}\n\t\t}\n\tbits--;\n\tfor (wvalue=0, i=bits%window; i>=0; i--,bits--)\n\t\twvalue = (wvalue<<1)+BN_is_bit_set(p,bits);\n\tif (!MOD_EXP_CTIME_COPY_FROM_PREBUF(&tmp,top,powerbuf,wvalue,numPowers)) goto err;\n \twhile (bits >= 0)\n \t\t{\n \t\twvalue=0;\n \t\tfor (i=0; i<window; i++,bits--)\n \t\t\t{\n\t\t\tif (!BN_mod_mul_montgomery(&tmp,&tmp,&tmp,mont,ctx))\tgoto err;\n\t\t\twvalue = (wvalue<<1)+BN_is_bit_set(p,bits);\n \t\t\t}\n\t\tif (!MOD_EXP_CTIME_COPY_FROM_PREBUF(&am, top, powerbuf, wvalue, numPowers)) goto err;\n \t\tif (!BN_mod_mul_montgomery(&tmp,&tmp,&am,mont,ctx)) goto err;\n \t\t}\n\t}\n\tif (!BN_from_montgomery(rr,&tmp,mont,ctx)) goto err;\n\tret=1;\nerr:\n\tif ((in_mont == NULL) && (mont != NULL)) BN_MONT_CTX_free(mont);\n\tif (powerbuf!=NULL)\n\t\t{\n\t\tOPENSSL_cleanse(powerbuf,powerbufLen);\n\t\tif (powerbufFree) OPENSSL_free(powerbufFree);\n\t\t}\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}', 'int BN_mod_mul_montgomery(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,\n\t\t\t BN_MONT_CTX *mont, BN_CTX *ctx)\n\t{\n\tBIGNUM *tmp;\n\tint ret=0;\n#if defined(OPENSSL_BN_ASM_MONT) && defined(MONT_WORD)\n\tint num = mont->N.top;\n\tif (num>1 && a->top==num && b->top==num)\n\t\t{\n\t\tif (bn_wexpand(r,num) == NULL) return(0);\n\t\tif (bn_mul_mont(r->d,a->d,b->d,mont->N.d,mont->n0,num))\n\t\t\t{\n\t\t\tr->neg = a->neg^b->neg;\n\t\t\tr->top = num;\n\t\t\tbn_correct_top(r);\n\t\t\treturn(1);\n\t\t\t}\n\t\t}\n#endif\n\tBN_CTX_start(ctx);\n\ttmp = BN_CTX_get(ctx);\n\tif (tmp == NULL) goto err;\n\tbn_check_top(tmp);\n\tif (a == b)\n\t\t{\n\t\tif (!BN_sqr(tmp,a,ctx)) goto err;\n\t\t}\n\telse\n\t\t{\n\t\tif (!BN_mul(tmp,a,b,ctx)) goto err;\n\t\t}\n#ifdef MONT_WORD\n\tif (!BN_from_montgomery_word(r,tmp,mont)) goto err;\n#else\n\tif (!BN_from_montgomery(r,tmp,mont,ctx)) goto err;\n#endif\n\tbn_check_top(r);\n\tret=1;\nerr:\n\tBN_CTX_end(ctx);\n\treturn(ret);\n\t}', 'int BN_sqr(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx)\n\t{\n\tint max,al;\n\tint ret = 0;\n\tBIGNUM *tmp,*rr;\n#ifdef BN_COUNT\n\tfprintf(stderr,"BN_sqr %d * %d\\n",a->top,a->top);\n#endif\n\tbn_check_top(a);\n\tal=a->top;\n\tif (al <= 0)\n\t\t{\n\t\tr->top=0;\n\t\treturn 1;\n\t\t}\n\tBN_CTX_start(ctx);\n\trr=(a != r) ? r : BN_CTX_get(ctx);\n\ttmp=BN_CTX_get(ctx);\n\tif (!rr || !tmp) goto err;\n\tmax = 2 * al;\n\tif (bn_wexpand(rr,max) == NULL) goto err;\n\tif (al == 4)\n\t\t{\n#ifndef BN_SQR_COMBA\n\t\tBN_ULONG t[8];\n\t\tbn_sqr_normal(rr->d,a->d,4,t);\n#else\n\t\tbn_sqr_comba4(rr->d,a->d);\n#endif\n\t\t}\n\telse if (al == 8)\n\t\t{\n#ifndef BN_SQR_COMBA\n\t\tBN_ULONG t[16];\n\t\tbn_sqr_normal(rr->d,a->d,8,t);\n#else\n\t\tbn_sqr_comba8(rr->d,a->d);\n#endif\n\t\t}\n\telse\n\t\t{\n#if defined(BN_RECURSION)\n\t\tif (al < BN_SQR_RECURSIVE_SIZE_NORMAL)\n\t\t\t{\n\t\t\tBN_ULONG t[BN_SQR_RECURSIVE_SIZE_NORMAL*2];\n\t\t\tbn_sqr_normal(rr->d,a->d,al,t);\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tint j,k;\n\t\t\tj=BN_num_bits_word((BN_ULONG)al);\n\t\t\tj=1<<(j-1);\n\t\t\tk=j+j;\n\t\t\tif (al == j)\n\t\t\t\t{\n\t\t\t\tif (bn_wexpand(tmp,k*2) == NULL) goto err;\n\t\t\t\tbn_sqr_recursive(rr->d,a->d,al,tmp->d);\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\tif (bn_wexpand(tmp,max) == NULL) goto err;\n\t\t\t\tbn_sqr_normal(rr->d,a->d,al,tmp->d);\n\t\t\t\t}\n\t\t\t}\n#else\n\t\tif (bn_wexpand(tmp,max) == NULL) goto err;\n\t\tbn_sqr_normal(rr->d,a->d,al,tmp->d);\n#endif\n\t\t}\n\trr->neg=0;\n\tif(a->d[al - 1] == (a->d[al - 1] & BN_MASK2l))\n\t\trr->top = max - 1;\n\telse\n\t\trr->top = max;\n\tif (rr != r) BN_copy(r,rr);\n\tret = 1;\n err:\n\tbn_check_top(rr);\n\tbn_check_top(tmp);\n\tBN_CTX_end(ctx);\n\treturn(ret);\n\t}', 'void BN_CTX_end(BN_CTX *ctx)\n\t{\n\tCTXDBG_ENTRY("BN_CTX_end", ctx);\n\tif(ctx->err_stack)\n\t\tctx->err_stack--;\n\telse\n\t\t{\n\t\tunsigned int fp = BN_STACK_pop(&ctx->stack);\n\t\tif(fp < ctx->used)\n\t\t\tBN_POOL_release(&ctx->pool, ctx->used - fp);\n\t\tctx->used = fp;\n\t\tctx->too_many = 0;\n\t\t}\n\tCTXDBG_EXIT(ctx);\n\t}', 'static unsigned int BN_STACK_pop(BN_STACK *st)\n\t{\n\treturn st->indexes[--(st->depth)];\n\t}']
232
0
https://github.com/openssl/openssl/blob/7f7eb90b8ac55997c5c825bb3ebcfe28611e06f5/test/evp_test.c/#L1504
static int encode_test_init(EVP_TEST *t, const char *encoding) { ENCODE_DATA *edata; if (!TEST_ptr(edata = OPENSSL_zalloc(sizeof(*edata)))) return 0; 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; if (!TEST_ptr(t->expected_err = OPENSSL_strdup("DECODE_ERROR"))) return 0; } else { TEST_error("Bad encoding: %s." " Should be one of {canonical, valid, invalid}", encoding); return 0; } t->data = edata; return 1; }
['static int encode_test_init(EVP_TEST *t, const char *encoding)\n{\n ENCODE_DATA *edata;\n if (!TEST_ptr(edata = OPENSSL_zalloc(sizeof(*edata))))\n return 0;\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 if (!TEST_ptr(t->expected_err = OPENSSL_strdup("DECODE_ERROR")))\n return 0;\n } else {\n TEST_error("Bad encoding: %s."\n " Should be one of {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 FAILTEST();\n if (ret != NULL)\n memset(ret, 0, num);\n return ret;\n}', 'void *CRYPTO_malloc(size_t num, const char *file, int line)\n{\n void *ret = NULL;\n if (malloc_impl != NULL && malloc_impl != CRYPTO_malloc)\n return malloc_impl(num, file, line);\n if (num == 0)\n return NULL;\n FAILTEST();\n allow_customize = 0;\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n if (call_malloc_debug) {\n CRYPTO_mem_debug_malloc(NULL, num, 0, file, line);\n ret = malloc(num);\n CRYPTO_mem_debug_malloc(ret, num, 1, file, line);\n } else {\n ret = malloc(num);\n }\n#else\n osslargused(file); osslargused(line);\n ret = malloc(num);\n#endif\n return ret;\n}', 'int test_ptr(const char *file, int line, const char *s, const void *p)\n{\n if (p != NULL)\n return 1;\n test_fail_message(NULL, file, line, "ptr", s, "NULL", "!=", "%p", p);\n return 0;\n}']
233
0
https://github.com/openssl/openssl/blob/47bbaa5b607f592009ed40f5678fde21c10a873c/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); }
['int s_client_main(int argc, char **argv)\n{\n BIO *sbio;\n EVP_PKEY *key = NULL;\n SSL *con = NULL;\n SSL_CTX *ctx = NULL;\n STACK_OF(X509) *chain = NULL;\n X509 *cert = NULL;\n X509_VERIFY_PARAM *vpm = NULL;\n SSL_EXCERT *exc = NULL;\n SSL_CONF_CTX *cctx = NULL;\n STACK_OF(OPENSSL_STRING) *ssl_args = NULL;\n STACK_OF(X509_CRL) *crls = NULL;\n const SSL_METHOD *meth = TLS_client_method();\n char *CApath = NULL, *CAfile = NULL, *cbuf = NULL, *sbuf = NULL;\n char *mbuf = NULL, *proxystr = NULL, *connectstr = NULL;\n char *cert_file = NULL, *key_file = NULL, *chain_file = NULL, *prog;\n char *chCApath = NULL, *chCAfile = NULL, *host = SSL_HOST_NAME;\n char *inrand = NULL;\n char *passarg = NULL, *pass = NULL, *vfyCApath = NULL, *vfyCAfile = NULL;\n char *sess_in = NULL, *sess_out = NULL, *crl_file = NULL, *p;\n char *jpake_secret = NULL, *xmpphost = NULL;\n const char *unix_path = NULL;\n const char *ehlo = "mail.example.com";\n struct sockaddr peer;\n struct timeval timeout, *timeoutp;\n fd_set readfds, writefds;\n int build_chain = 0, cbuf_len, cbuf_off, cert_format = FORMAT_PEM;\n int key_format = FORMAT_PEM, crlf = 0, full_log = 1, mbuf_len = 0;\n int prexit = 0;\n int enable_timeouts = 0, sdebug = 0, peerlen = sizeof peer;\n int reconnect = 0, verify = SSL_VERIFY_NONE, vpmtouched = 0;\n int ret = 1, in_init = 1, i, nbio_test = 0, s = -1, k, width, state = 0;\n int sbuf_len, sbuf_off, socket_type = SOCK_STREAM, cmdletters = 1;\n int starttls_proto = PROTO_OFF, crl_format = FORMAT_PEM, crl_download = 0;\n int write_tty, read_tty, write_ssl, read_ssl, tty_on, ssl_pending;\n int fallback_scsv = 0;\n long socket_mtu = 0, randamt = 0;\n unsigned short port = PORT;\n OPTION_CHOICE o;\n#ifndef OPENSSL_NO_ENGINE\n ENGINE *ssl_client_engine = NULL;\n#endif\n ENGINE *e = NULL;\n#if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_NETWARE)\n struct timeval tv;\n#endif\n char *servername = NULL;\n const char *alpn_in = NULL;\n tlsextctx tlsextcbp = { NULL, 0 };\n#define MAX_SI_TYPES 100\n unsigned short serverinfo_types[MAX_SI_TYPES];\n int serverinfo_count = 0, start = 0, len;\n#ifndef OPENSSL_NO_NEXTPROTONEG\n const char *next_proto_neg_in = NULL;\n#endif\n#ifndef OPENSSL_NO_SRP\n char *srppass = NULL;\n int srp_lateuser = 0;\n SRP_ARG srp_arg = { NULL, NULL, 0, 0, 0, 1024 };\n#endif\n prog = opt_progname(argv[0]);\n c_Pause = 0;\n c_quiet = 0;\n c_ign_eof = 0;\n c_debug = 0;\n c_msg = 0;\n c_showcerts = 0;\n c_nbio = 0;\n verify_depth = 0;\n verify_error = X509_V_OK;\n vpm = X509_VERIFY_PARAM_new();\n cbuf = app_malloc(BUFSIZZ, "cbuf");\n sbuf = app_malloc(BUFSIZZ, "sbuf");\n mbuf = app_malloc(BUFSIZZ, "mbuf");\n cctx = SSL_CONF_CTX_new();\n if (vpm == NULL || cctx == NULL) {\n BIO_printf(bio_err, "%s: out of memory\\n", prog);\n goto end;\n }\n SSL_CONF_CTX_set_flags(cctx, SSL_CONF_FLAG_CLIENT | SSL_CONF_FLAG_CMDLINE);\n prog = opt_init(argc, argv, s_client_options);\n while ((o = opt_next()) != OPT_EOF) {\n switch (o) {\n case OPT_EOF:\n case OPT_ERR:\n opthelp:\n BIO_printf(bio_err, "%s: Use -help for summary.\\n", prog);\n goto end;\n case OPT_HELP:\n opt_help(s_client_options);\n ret = 0;\n goto end;\n case OPT_HOST:\n host = opt_arg();\n break;\n case OPT_PORT:\n port = atoi(opt_arg());\n break;\n case OPT_CONNECT:\n connectstr = opt_arg();\n break;\n case OPT_PROXY:\n proxystr = opt_arg();\n starttls_proto = PROTO_CONNECT;\n break;\n case OPT_UNIX:\n unix_path = opt_arg();\n break;\n case OPT_XMPPHOST:\n xmpphost = opt_arg();\n break;\n case OPT_SMTPHOST:\n ehlo = opt_arg();\n break;\n case OPT_VERIFY:\n verify = SSL_VERIFY_PEER;\n verify_depth = atoi(opt_arg());\n if (!c_quiet)\n BIO_printf(bio_err, "verify depth is %d\\n", verify_depth);\n break;\n case OPT_CERT:\n cert_file = opt_arg();\n break;\n case OPT_CRL:\n crl_file = opt_arg();\n break;\n case OPT_CRL_DOWNLOAD:\n crl_download = 1;\n break;\n case OPT_SESS_OUT:\n sess_out = opt_arg();\n break;\n case OPT_SESS_IN:\n sess_in = opt_arg();\n break;\n case OPT_CERTFORM:\n if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &cert_format))\n goto opthelp;\n break;\n case OPT_CRLFORM:\n if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &crl_format))\n goto opthelp;\n break;\n case OPT_VERIFY_RET_ERROR:\n verify_return_error = 1;\n break;\n case OPT_VERIFY_QUIET:\n verify_quiet = 1;\n break;\n case OPT_BRIEF:\n c_brief = verify_quiet = c_quiet = 1;\n break;\n case OPT_S_CASES:\n if (ssl_args == NULL)\n ssl_args = sk_OPENSSL_STRING_new_null();\n if (ssl_args == NULL\n || !sk_OPENSSL_STRING_push(ssl_args, opt_flag())\n || !sk_OPENSSL_STRING_push(ssl_args, opt_arg())) {\n BIO_printf(bio_err, "%s: Memory allocation failure\\n", prog);\n goto end;\n }\n break;\n case OPT_V_CASES:\n if (!opt_verify(o, vpm))\n goto end;\n vpmtouched++;\n break;\n case OPT_X_CASES:\n if (!args_excert(o, &exc))\n goto end;\n break;\n case OPT_PREXIT:\n prexit = 1;\n break;\n case OPT_CRLF:\n crlf = 1;\n break;\n case OPT_QUIET:\n c_quiet = c_ign_eof = 1;\n break;\n case OPT_NBIO:\n c_nbio = 1;\n break;\n case OPT_NOCMDS:\n cmdletters = 0;\n break;\n case OPT_ENGINE:\n e = setup_engine(opt_arg(), 1);\n break;\n case OPT_SSL_CLIENT_ENGINE:\n#ifndef OPENSSL_NO_ENGINE\n ssl_client_engine = ENGINE_by_id(opt_arg());\n if (ssl_client_engine == NULL) {\n BIO_printf(bio_err, "Error getting client auth engine\\n");\n goto opthelp;\n }\n break;\n#endif\n break;\n case OPT_RAND:\n inrand = opt_arg();\n break;\n case OPT_IGN_EOF:\n c_ign_eof = 1;\n break;\n case OPT_NO_IGN_EOF:\n c_ign_eof = 0;\n break;\n case OPT_PAUSE:\n c_Pause = 1;\n break;\n case OPT_DEBUG:\n c_debug = 1;\n break;\n case OPT_TLSEXTDEBUG:\n c_tlsextdebug = 1;\n break;\n case OPT_STATUS:\n c_status_req = 1;\n break;\n case OPT_WDEBUG:\n#ifdef WATT32\n dbug_init();\n#endif\n break;\n case OPT_MSG:\n c_msg = 1;\n break;\n case OPT_MSGFILE:\n bio_c_msg = BIO_new_file(opt_arg(), "w");\n break;\n case OPT_TRACE:\n#ifndef OPENSSL_NO_SSL_TRACE\n c_msg = 2;\n#endif\n break;\n case OPT_SECURITY_DEBUG:\n sdebug = 1;\n break;\n case OPT_SECURITY_DEBUG_VERBOSE:\n sdebug = 2;\n break;\n case OPT_SHOWCERTS:\n c_showcerts = 1;\n break;\n case OPT_NBIO_TEST:\n nbio_test = 1;\n break;\n case OPT_STATE:\n state = 1;\n break;\n#ifndef OPENSSL_NO_PSK\n case OPT_PSK_IDENTITY:\n psk_identity = opt_arg();\n break;\n case OPT_PSK:\n for (p = psk_key = opt_arg(); *p; p++) {\n if (isxdigit(*p))\n continue;\n BIO_printf(bio_err, "Not a hex number \'%s\'\\n", psk_key);\n goto end;\n }\n break;\n#else\n case OPT_PSK_IDENTITY:\n case OPT_PSK:\n break;\n#endif\n#ifndef OPENSSL_NO_SRP\n case OPT_SRPUSER:\n srp_arg.srplogin = opt_arg();\n meth = TLSv1_client_method();\n break;\n case OPT_SRPPASS:\n srppass = opt_arg();\n meth = TLSv1_client_method();\n break;\n case OPT_SRP_STRENGTH:\n srp_arg.strength = atoi(opt_arg());\n BIO_printf(bio_err, "SRP minimal length for N is %d\\n",\n srp_arg.strength);\n meth = TLSv1_client_method();\n break;\n case OPT_SRP_LATEUSER:\n srp_lateuser = 1;\n meth = TLSv1_client_method();\n break;\n case OPT_SRP_MOREGROUPS:\n srp_arg.amp = 1;\n meth = TLSv1_client_method();\n break;\n#else\n case OPT_SRPUSER:\n case OPT_SRPPASS:\n case OPT_SRP_STRENGTH:\n case OPT_SRP_LATEUSER:\n case OPT_SRP_MOREGROUPS:\n break;\n#endif\n case OPT_SSL3:\n#ifndef OPENSSL_NO_SSL3\n meth = SSLv3_client_method();\n#endif\n break;\n case OPT_TLS1_2:\n meth = TLSv1_2_client_method();\n break;\n case OPT_TLS1_1:\n meth = TLSv1_1_client_method();\n break;\n case OPT_TLS1:\n meth = TLSv1_client_method();\n break;\n#ifndef OPENSSL_NO_DTLS1\n case OPT_DTLS:\n meth = DTLS_client_method();\n socket_type = SOCK_DGRAM;\n break;\n case OPT_DTLS1:\n meth = DTLSv1_client_method();\n socket_type = SOCK_DGRAM;\n break;\n case OPT_DTLS1_2:\n meth = DTLSv1_2_client_method();\n socket_type = SOCK_DGRAM;\n break;\n case OPT_TIMEOUT:\n enable_timeouts = 1;\n break;\n case OPT_MTU:\n socket_mtu = atol(opt_arg());\n break;\n#else\n case OPT_DTLS:\n case OPT_DTLS1:\n case OPT_DTLS1_2:\n case OPT_TIMEOUT:\n case OPT_MTU:\n break;\n#endif\n case OPT_FALLBACKSCSV:\n fallback_scsv = 1;\n break;\n case OPT_KEYFORM:\n if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &key_format))\n goto opthelp;\n break;\n case OPT_PASS:\n passarg = opt_arg();\n break;\n case OPT_CERT_CHAIN:\n chain_file = opt_arg();\n break;\n case OPT_KEY:\n key_file = opt_arg();\n break;\n case OPT_RECONNECT:\n reconnect = 5;\n break;\n case OPT_CAPATH:\n CApath = opt_arg();\n break;\n case OPT_CHAINCAPATH:\n chCApath = opt_arg();\n break;\n case OPT_VERIFYCAPATH:\n vfyCApath = opt_arg();\n break;\n case OPT_BUILD_CHAIN:\n build_chain = 1;\n break;\n case OPT_CAFILE:\n CAfile = opt_arg();\n break;\n case OPT_CHAINCAFILE:\n chCAfile = opt_arg();\n break;\n case OPT_VERIFYCAFILE:\n vfyCAfile = opt_arg();\n break;\n case OPT_NEXTPROTONEG:\n next_proto_neg_in = opt_arg();\n break;\n case OPT_ALPN:\n alpn_in = opt_arg();\n break;\n case OPT_SERVERINFO:\n p = opt_arg();\n len = strlen(p);\n for (start = 0, i = 0; i <= len; ++i) {\n if (i == len || p[i] == \',\') {\n serverinfo_types[serverinfo_count] = atoi(p + start);\n if (++serverinfo_count == MAX_SI_TYPES)\n break;\n start = i + 1;\n }\n }\n break;\n case OPT_STARTTLS:\n if (!opt_pair(opt_arg(), services, &starttls_proto))\n goto end;\n case OPT_SERVERNAME:\n servername = opt_arg();\n break;\n case OPT_JPAKE:\n#ifndef OPENSSL_NO_JPAKE\n jpake_secret = opt_arg();\n#endif\n break;\n case OPT_USE_SRTP:\n srtp_profiles = opt_arg();\n break;\n case OPT_KEYMATEXPORT:\n keymatexportlabel = opt_arg();\n break;\n case OPT_KEYMATEXPORTLEN:\n keymatexportlen = atoi(opt_arg());\n break;\n }\n }\n argc = opt_num_rest();\n argv = opt_rest();\n if (!app_load_modules(NULL))\n goto end;\n if (proxystr) {\n if (connectstr == NULL) {\n BIO_printf(bio_err, "%s: -proxy requires use of -connect\\n", prog);\n goto opthelp;\n }\n if (!extract_host_port(proxystr, &host, NULL, &port))\n goto end;\n }\n else if (connectstr != NULL\n && !extract_host_port(connectstr, &host, NULL, &port))\n goto end;\n if (unix_path && (socket_type != SOCK_STREAM)) {\n BIO_printf(bio_err,\n "Can\'t use unix sockets and datagrams together\\n");\n goto end;\n }\n#if !defined(OPENSSL_NO_JPAKE) && !defined(OPENSSL_NO_PSK)\n if (jpake_secret) {\n if (psk_key) {\n BIO_printf(bio_err, "Can\'t use JPAKE and PSK together\\n");\n goto end;\n }\n psk_identity = "JPAKE";\n }\n#endif\n#if !defined(OPENSSL_NO_NEXTPROTONEG)\n next_proto.status = -1;\n if (next_proto_neg_in) {\n next_proto.data =\n next_protos_parse(&next_proto.len, next_proto_neg_in);\n if (next_proto.data == NULL) {\n BIO_printf(bio_err, "Error parsing -nextprotoneg argument\\n");\n goto end;\n }\n } else\n next_proto.data = NULL;\n#endif\n if (!app_passwd(passarg, NULL, &pass, NULL)) {\n BIO_printf(bio_err, "Error getting password\\n");\n goto end;\n }\n if (key_file == NULL)\n key_file = cert_file;\n if (key_file) {\n key = load_key(key_file, key_format, 0, pass, e,\n "client certificate private key file");\n if (key == NULL) {\n ERR_print_errors(bio_err);\n goto end;\n }\n }\n if (cert_file) {\n cert = load_cert(cert_file, cert_format,\n NULL, e, "client certificate file");\n if (cert == NULL) {\n ERR_print_errors(bio_err);\n goto end;\n }\n }\n if (chain_file) {\n chain = load_certs(chain_file, FORMAT_PEM,\n NULL, e, "client certificate chain");\n if (!chain)\n goto end;\n }\n if (crl_file) {\n X509_CRL *crl;\n crl = load_crl(crl_file, crl_format);\n if (crl == NULL) {\n BIO_puts(bio_err, "Error loading CRL\\n");\n ERR_print_errors(bio_err);\n goto end;\n }\n crls = sk_X509_CRL_new_null();\n if (crls == NULL || !sk_X509_CRL_push(crls, crl)) {\n BIO_puts(bio_err, "Error adding CRL\\n");\n ERR_print_errors(bio_err);\n X509_CRL_free(crl);\n goto end;\n }\n }\n if (!load_excert(&exc))\n goto end;\n if (!app_RAND_load_file(NULL, 1) && inrand == NULL\n && !RAND_status()) {\n BIO_printf(bio_err,\n "warning, not much extra random data, consider using the -rand option\\n");\n }\n if (inrand != NULL) {\n randamt = app_RAND_load_files(inrand);\n BIO_printf(bio_err, "%ld semi-random bytes loaded\\n", randamt);\n }\n if (bio_c_out == NULL) {\n if (c_quiet && !c_debug) {\n bio_c_out = BIO_new(BIO_s_null());\n if (c_msg && !bio_c_msg)\n bio_c_msg = dup_bio_out();\n } else if (bio_c_out == NULL)\n bio_c_out = dup_bio_out();\n }\n#ifndef OPENSSL_NO_SRP\n if (!app_passwd(srppass, NULL, &srp_arg.srppassin, NULL)) {\n BIO_printf(bio_err, "Error getting password\\n");\n goto end;\n }\n#endif\n ctx = SSL_CTX_new(meth);\n if (ctx == NULL) {\n ERR_print_errors(bio_err);\n goto end;\n }\n if (sdebug)\n ssl_ctx_security_debug(ctx, sdebug);\n if (vpmtouched && !SSL_CTX_set1_param(ctx, vpm)) {\n BIO_printf(bio_err, "Error setting verify params\\n");\n ERR_print_errors(bio_err);\n goto end;\n }\n if (!config_ctx(cctx, ssl_args, ctx, 1, jpake_secret == NULL))\n goto end;\n if (!ssl_load_stores(ctx, vfyCApath, vfyCAfile, chCApath, chCAfile,\n crls, crl_download)) {\n BIO_printf(bio_err, "Error loading store locations\\n");\n ERR_print_errors(bio_err);\n goto end;\n }\n#ifndef OPENSSL_NO_ENGINE\n if (ssl_client_engine) {\n if (!SSL_CTX_set_client_cert_engine(ctx, ssl_client_engine)) {\n BIO_puts(bio_err, "Error setting client auth engine\\n");\n ERR_print_errors(bio_err);\n ENGINE_free(ssl_client_engine);\n goto end;\n }\n ENGINE_free(ssl_client_engine);\n }\n#endif\n#ifndef OPENSSL_NO_PSK\n if (psk_key != NULL || jpake_secret) {\n if (c_debug)\n BIO_printf(bio_c_out,\n "PSK key given or JPAKE in use, setting client callback\\n");\n SSL_CTX_set_psk_client_callback(ctx, psk_client_cb);\n }\n#endif\n#ifndef OPENSSL_NO_SRTP\n if (srtp_profiles != NULL) {\n if (SSL_CTX_set_tlsext_use_srtp(ctx, srtp_profiles) != 0) {\n BIO_printf(bio_err, "Error setting SRTP profile\\n");\n ERR_print_errors(bio_err);\n goto end;\n }\n }\n#endif\n if (exc)\n ssl_ctx_set_excert(ctx, exc);\n#if !defined(OPENSSL_NO_NEXTPROTONEG)\n if (next_proto.data)\n SSL_CTX_set_next_proto_select_cb(ctx, next_proto_cb, &next_proto);\n#endif\n if (alpn_in) {\n unsigned short alpn_len;\n unsigned char *alpn = next_protos_parse(&alpn_len, alpn_in);\n if (alpn == NULL) {\n BIO_printf(bio_err, "Error parsing -alpn argument\\n");\n goto end;\n }\n if (SSL_CTX_set_alpn_protos(ctx, alpn, alpn_len) != 0) {\n BIO_printf(bio_err, "Error setting ALPN\\n");\n goto end;\n }\n OPENSSL_free(alpn);\n }\n for (i = 0; i < serverinfo_count; i++) {\n if (!SSL_CTX_add_client_custom_ext(ctx,\n serverinfo_types[i],\n NULL, NULL, NULL,\n serverinfo_cli_parse_cb, NULL)) {\n BIO_printf(bio_err,\n "Warning: Unable to add custom extension %u, skipping\\n",\n serverinfo_types[i]);\n }\n }\n if (state)\n SSL_CTX_set_info_callback(ctx, apps_ssl_info_callback);\n SSL_CTX_set_verify(ctx, verify, verify_callback);\n if (!ctx_set_verify_locations(ctx, CAfile, CApath)) {\n ERR_print_errors(bio_err);\n goto end;\n }\n ssl_ctx_add_crls(ctx, crls, crl_download);\n if (!set_cert_key_stuff(ctx, cert, key, chain, build_chain))\n goto end;\n if (servername != NULL) {\n tlsextcbp.biodebug = bio_err;\n SSL_CTX_set_tlsext_servername_callback(ctx, ssl_servername_cb);\n SSL_CTX_set_tlsext_servername_arg(ctx, &tlsextcbp);\n }\n# ifndef OPENSSL_NO_SRP\n if (srp_arg.srplogin) {\n if (!srp_lateuser && !SSL_CTX_set_srp_username(ctx, srp_arg.srplogin)) {\n BIO_printf(bio_err, "Unable to set SRP username\\n");\n goto end;\n }\n srp_arg.msg = c_msg;\n srp_arg.debug = c_debug;\n SSL_CTX_set_srp_cb_arg(ctx, &srp_arg);\n SSL_CTX_set_srp_client_pwd_callback(ctx, ssl_give_srp_client_pwd_cb);\n SSL_CTX_set_srp_strength(ctx, srp_arg.strength);\n if (c_msg || c_debug || srp_arg.amp == 0)\n SSL_CTX_set_srp_verify_param_callback(ctx,\n ssl_srp_verify_param_cb);\n }\n# endif\n con = SSL_new(ctx);\n if (sess_in) {\n SSL_SESSION *sess;\n BIO *stmp = BIO_new_file(sess_in, "r");\n if (!stmp) {\n BIO_printf(bio_err, "Can\'t open session file %s\\n", sess_in);\n ERR_print_errors(bio_err);\n goto end;\n }\n sess = PEM_read_bio_SSL_SESSION(stmp, NULL, 0, NULL);\n BIO_free(stmp);\n if (!sess) {\n BIO_printf(bio_err, "Can\'t open session file %s\\n", sess_in);\n ERR_print_errors(bio_err);\n goto end;\n }\n if (!SSL_set_session(con, sess)) {\n BIO_printf(bio_err, "Can\'t set session\\n");\n ERR_print_errors(bio_err);\n goto end;\n }\n SSL_SESSION_free(sess);\n }\n if (fallback_scsv)\n SSL_set_mode(con, SSL_MODE_SEND_FALLBACK_SCSV);\n if (servername != NULL) {\n if (!SSL_set_tlsext_host_name(con, servername)) {\n BIO_printf(bio_err, "Unable to set TLS servername extension.\\n");\n ERR_print_errors(bio_err);\n goto end;\n }\n }\n re_start:\n#ifdef NO_SYS_UN_H\n if (init_client(&s, host, port, socket_type) == 0)\n#else\n if ((!unix_path && (init_client(&s, host, port, socket_type) == 0)) ||\n (unix_path && (init_client_unix(&s, unix_path) == 0)))\n#endif\n {\n BIO_printf(bio_err, "connect:errno=%d\\n", get_last_socket_error());\n SHUTDOWN(s);\n goto end;\n }\n BIO_printf(bio_c_out, "CONNECTED(%08X)\\n", s);\n#ifdef FIONBIO\n if (c_nbio) {\n unsigned long l = 1;\n BIO_printf(bio_c_out, "turning on non blocking io\\n");\n if (BIO_socket_ioctl(s, FIONBIO, &l) < 0) {\n ERR_print_errors(bio_err);\n goto end;\n }\n }\n#endif\n if (c_Pause & 0x01)\n SSL_set_debug(con, 1);\n if (socket_type == SOCK_DGRAM) {\n sbio = BIO_new_dgram(s, BIO_NOCLOSE);\n if (getsockname(s, &peer, (void *)&peerlen) < 0) {\n BIO_printf(bio_err, "getsockname:errno=%d\\n",\n get_last_socket_error());\n SHUTDOWN(s);\n goto end;\n }\n (void)BIO_ctrl_set_connected(sbio, 1, &peer);\n if (enable_timeouts) {\n timeout.tv_sec = 0;\n timeout.tv_usec = DGRAM_RCV_TIMEOUT;\n BIO_ctrl(sbio, BIO_CTRL_DGRAM_SET_RECV_TIMEOUT, 0, &timeout);\n timeout.tv_sec = 0;\n timeout.tv_usec = DGRAM_SND_TIMEOUT;\n BIO_ctrl(sbio, BIO_CTRL_DGRAM_SET_SEND_TIMEOUT, 0, &timeout);\n }\n if (socket_mtu) {\n if (socket_mtu < DTLS_get_link_min_mtu(con)) {\n BIO_printf(bio_err, "MTU too small. Must be at least %ld\\n",\n DTLS_get_link_min_mtu(con));\n BIO_free(sbio);\n goto shut;\n }\n SSL_set_options(con, SSL_OP_NO_QUERY_MTU);\n if (!DTLS_set_link_mtu(con, socket_mtu)) {\n BIO_printf(bio_err, "Failed to set MTU\\n");\n BIO_free(sbio);\n goto shut;\n }\n } else\n BIO_ctrl(sbio, BIO_CTRL_DGRAM_MTU_DISCOVER, 0, NULL);\n } else\n sbio = BIO_new_socket(s, BIO_NOCLOSE);\n if (nbio_test) {\n BIO *test;\n test = BIO_new(BIO_f_nbio_test());\n sbio = BIO_push(test, sbio);\n }\n if (c_debug) {\n SSL_set_debug(con, 1);\n BIO_set_callback(sbio, bio_dump_callback);\n BIO_set_callback_arg(sbio, (char *)bio_c_out);\n }\n if (c_msg) {\n#ifndef OPENSSL_NO_SSL_TRACE\n if (c_msg == 2)\n SSL_set_msg_callback(con, SSL_trace);\n else\n#endif\n SSL_set_msg_callback(con, msg_cb);\n SSL_set_msg_callback_arg(con, bio_c_msg ? bio_c_msg : bio_c_out);\n }\n if (c_tlsextdebug) {\n SSL_set_tlsext_debug_callback(con, tlsext_cb);\n SSL_set_tlsext_debug_arg(con, bio_c_out);\n }\n if (c_status_req) {\n SSL_set_tlsext_status_type(con, TLSEXT_STATUSTYPE_ocsp);\n SSL_CTX_set_tlsext_status_cb(ctx, ocsp_resp_cb);\n SSL_CTX_set_tlsext_status_arg(ctx, bio_c_out);\n }\n#ifndef OPENSSL_NO_JPAKE\n if (jpake_secret)\n jpake_client_auth(bio_c_out, sbio, jpake_secret);\n#endif\n SSL_set_bio(con, sbio, sbio);\n SSL_set_connect_state(con);\n width = SSL_get_fd(con) + 1;\n read_tty = 1;\n write_tty = 0;\n tty_on = 0;\n read_ssl = 1;\n write_ssl = 1;\n cbuf_len = 0;\n cbuf_off = 0;\n sbuf_len = 0;\n sbuf_off = 0;\n switch ((PROTOCOL_CHOICE) starttls_proto) {\n case PROTO_OFF:\n break;\n case PROTO_SMTP:\n {\n int foundit = 0;\n BIO *fbio = BIO_new(BIO_f_buffer());\n BIO_push(fbio, sbio);\n do {\n mbuf_len = BIO_gets(fbio, mbuf, BUFSIZZ);\n }\n while (mbuf_len > 3 && mbuf[3] == \'-\');\n BIO_printf(fbio, "EHLO %s\\r\\n", ehlo);\n (void)BIO_flush(fbio);\n do {\n mbuf_len = BIO_gets(fbio, mbuf, BUFSIZZ);\n if (strstr(mbuf, "STARTTLS"))\n foundit = 1;\n }\n while (mbuf_len > 3 && mbuf[3] == \'-\');\n (void)BIO_flush(fbio);\n BIO_pop(fbio);\n BIO_free(fbio);\n if (!foundit)\n BIO_printf(bio_err,\n "didn\'t found starttls in server response,"\n " try anyway...\\n");\n BIO_printf(sbio, "STARTTLS\\r\\n");\n BIO_read(sbio, sbuf, BUFSIZZ);\n }\n break;\n case PROTO_POP3:\n {\n BIO_read(sbio, mbuf, BUFSIZZ);\n BIO_printf(sbio, "STLS\\r\\n");\n mbuf_len = BIO_read(sbio, sbuf, BUFSIZZ);\n if (mbuf_len < 0) {\n BIO_printf(bio_err, "BIO_read failed\\n");\n goto end;\n }\n }\n break;\n case PROTO_IMAP:\n {\n int foundit = 0;\n BIO *fbio = BIO_new(BIO_f_buffer());\n BIO_push(fbio, sbio);\n BIO_gets(fbio, mbuf, BUFSIZZ);\n BIO_printf(fbio, ". CAPABILITY\\r\\n");\n (void)BIO_flush(fbio);\n do {\n mbuf_len = BIO_gets(fbio, mbuf, BUFSIZZ);\n if (strstr(mbuf, "STARTTLS"))\n foundit = 1;\n }\n while (mbuf_len > 3 && mbuf[0] != \'.\');\n (void)BIO_flush(fbio);\n BIO_pop(fbio);\n BIO_free(fbio);\n if (!foundit)\n BIO_printf(bio_err,\n "didn\'t found STARTTLS in server response,"\n " try anyway...\\n");\n BIO_printf(sbio, ". STARTTLS\\r\\n");\n BIO_read(sbio, sbuf, BUFSIZZ);\n }\n break;\n case PROTO_FTP:\n {\n BIO *fbio = BIO_new(BIO_f_buffer());\n BIO_push(fbio, sbio);\n do {\n mbuf_len = BIO_gets(fbio, mbuf, BUFSIZZ);\n }\n while (mbuf_len > 3 && mbuf[3] == \'-\');\n (void)BIO_flush(fbio);\n BIO_pop(fbio);\n BIO_free(fbio);\n BIO_printf(sbio, "AUTH TLS\\r\\n");\n BIO_read(sbio, sbuf, BUFSIZZ);\n }\n break;\n case PROTO_XMPP:\n case PROTO_XMPP_SERVER:\n {\n int seen = 0;\n BIO_printf(sbio, "<stream:stream "\n "xmlns:stream=\'http://etherx.jabber.org/streams\' "\n "xmlns=\'jabber:%s\' to=\'%s\' version=\'1.0\'>",\n starttls_proto == PROTO_XMPP ? "client" : "server",\n xmpphost ? xmpphost : host);\n seen = BIO_read(sbio, mbuf, BUFSIZZ);\n mbuf[seen] = 0;\n while (!strstr\n (mbuf, "<starttls xmlns=\'urn:ietf:params:xml:ns:xmpp-tls\'")\n && !strstr(mbuf,\n "<starttls xmlns=\\"urn:ietf:params:xml:ns:xmpp-tls\\""))\n {\n seen = BIO_read(sbio, mbuf, BUFSIZZ);\n if (seen <= 0)\n goto shut;\n mbuf[seen] = 0;\n }\n BIO_printf(sbio,\n "<starttls xmlns=\'urn:ietf:params:xml:ns:xmpp-tls\'/>");\n seen = BIO_read(sbio, sbuf, BUFSIZZ);\n sbuf[seen] = 0;\n if (!strstr(sbuf, "<proceed"))\n goto shut;\n mbuf[0] = 0;\n }\n break;\n case PROTO_TELNET:\n {\n static const unsigned char tls_do[] = {\n 255, 253, 46\n };\n static const unsigned char tls_will[] = {\n 255, 251, 46\n };\n static const unsigned char tls_follows[] = {\n 255, 250, 46, 1, 255, 240\n };\n int bytes;\n bytes = BIO_read(sbio, mbuf, BUFSIZZ);\n if (bytes != 3 || memcmp(mbuf, tls_do, 3) != 0)\n goto shut;\n BIO_write(sbio, tls_will, 3);\n BIO_write(sbio, tls_follows, 6);\n (void)BIO_flush(sbio);\n bytes = BIO_read(sbio, mbuf, BUFSIZZ);\n if (bytes != 6 || memcmp(mbuf, tls_follows, 6) != 0)\n goto shut;\n }\n break;\n case PROTO_CONNECT:\n {\n int foundit = 0;\n BIO *fbio = BIO_new(BIO_f_buffer());\n BIO_push(fbio, sbio);\n BIO_printf(fbio, "CONNECT %s\\r\\n\\r\\n", connectstr);\n (void)BIO_flush(fbio);\n do {\n mbuf_len = BIO_gets(fbio, mbuf, BUFSIZZ);\n if (strstr(mbuf, "200") != NULL\n && strstr(mbuf, "established") != NULL)\n foundit++;\n } while (mbuf_len > 3 && foundit == 0);\n (void)BIO_flush(fbio);\n BIO_pop(fbio);\n BIO_free(fbio);\n if (!foundit) {\n BIO_printf(bio_err, "%s: HTTP CONNECT failed\\n", prog);\n goto shut;\n }\n }\n break;\n }\n for (;;) {\n FD_ZERO(&readfds);\n FD_ZERO(&writefds);\n if ((SSL_version(con) == DTLS1_VERSION) &&\n DTLSv1_get_timeout(con, &timeout))\n timeoutp = &timeout;\n else\n timeoutp = NULL;\n if (SSL_in_init(con) && !SSL_total_renegotiations(con)) {\n in_init = 1;\n tty_on = 0;\n } else {\n tty_on = 1;\n if (in_init) {\n in_init = 0;\n if (servername != NULL && !SSL_session_reused(con)) {\n BIO_printf(bio_c_out,\n "Server did %sacknowledge servername extension.\\n",\n tlsextcbp.ack ? "" : "not ");\n }\n if (sess_out) {\n BIO *stmp = BIO_new_file(sess_out, "w");\n if (stmp) {\n PEM_write_bio_SSL_SESSION(stmp, SSL_get_session(con));\n BIO_free(stmp);\n } else\n BIO_printf(bio_err, "Error writing session file %s\\n",\n sess_out);\n }\n if (c_brief) {\n BIO_puts(bio_err, "CONNECTION ESTABLISHED\\n");\n print_ssl_summary(con);\n }\n print_stuff(bio_c_out, con, full_log);\n if (full_log > 0)\n full_log--;\n if (starttls_proto) {\n BIO_write(bio_err, mbuf, mbuf_len);\n if (!reconnect)\n starttls_proto = PROTO_OFF;\n }\n if (reconnect) {\n reconnect--;\n BIO_printf(bio_c_out,\n "drop connection and then reconnect\\n");\n SSL_shutdown(con);\n SSL_set_connect_state(con);\n SHUTDOWN(SSL_get_fd(con));\n goto re_start;\n }\n }\n }\n ssl_pending = read_ssl && SSL_pending(con);\n if (!ssl_pending) {\n#if !defined(OPENSSL_SYS_WINDOWS) && !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_NETWARE)\n if (tty_on) {\n if (read_tty)\n openssl_fdset(fileno(stdin), &readfds);\n if (write_tty)\n openssl_fdset(fileno(stdout), &writefds);\n }\n if (read_ssl)\n openssl_fdset(SSL_get_fd(con), &readfds);\n if (write_ssl)\n openssl_fdset(SSL_get_fd(con), &writefds);\n#else\n if (!tty_on || !write_tty) {\n if (read_ssl)\n openssl_fdset(SSL_get_fd(con), &readfds);\n if (write_ssl)\n openssl_fdset(SSL_get_fd(con), &writefds);\n }\n#endif\n#if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS)\n i = 0;\n if (!write_tty) {\n if (read_tty) {\n tv.tv_sec = 1;\n tv.tv_usec = 0;\n i = select(width, (void *)&readfds, (void *)&writefds,\n NULL, &tv);\n# if defined(OPENSSL_SYS_WINCE) || defined(OPENSSL_SYS_MSDOS)\n if (!i && (!_kbhit() || !read_tty))\n continue;\n# else\n if (!i && (!((_kbhit())\n || (WAIT_OBJECT_0 ==\n WaitForSingleObject(GetStdHandle\n (STD_INPUT_HANDLE),\n 0)))\n || !read_tty))\n continue;\n# endif\n } else\n i = select(width, (void *)&readfds, (void *)&writefds,\n NULL, timeoutp);\n }\n#elif defined(OPENSSL_SYS_NETWARE)\n if (!write_tty) {\n if (read_tty) {\n tv.tv_sec = 1;\n tv.tv_usec = 0;\n i = select(width, (void *)&readfds, (void *)&writefds,\n NULL, &tv);\n } else\n i = select(width, (void *)&readfds, (void *)&writefds,\n NULL, timeoutp);\n }\n#else\n i = select(width, (void *)&readfds, (void *)&writefds,\n NULL, timeoutp);\n#endif\n if (i < 0) {\n BIO_printf(bio_err, "bad select %d\\n",\n get_last_socket_error());\n goto shut;\n }\n }\n if ((SSL_version(con) == DTLS1_VERSION)\n && DTLSv1_handle_timeout(con) > 0) {\n BIO_printf(bio_err, "TIMEOUT occurred\\n");\n }\n if (!ssl_pending && FD_ISSET(SSL_get_fd(con), &writefds)) {\n k = SSL_write(con, &(cbuf[cbuf_off]), (unsigned int)cbuf_len);\n switch (SSL_get_error(con, k)) {\n case SSL_ERROR_NONE:\n cbuf_off += k;\n cbuf_len -= k;\n if (k <= 0)\n goto end;\n if (cbuf_len <= 0) {\n read_tty = 1;\n write_ssl = 0;\n } else {\n read_tty = 0;\n write_ssl = 1;\n }\n break;\n case SSL_ERROR_WANT_WRITE:\n BIO_printf(bio_c_out, "write W BLOCK\\n");\n write_ssl = 1;\n read_tty = 0;\n break;\n case SSL_ERROR_WANT_READ:\n BIO_printf(bio_c_out, "write R BLOCK\\n");\n write_tty = 0;\n read_ssl = 1;\n write_ssl = 0;\n break;\n case SSL_ERROR_WANT_X509_LOOKUP:\n BIO_printf(bio_c_out, "write X BLOCK\\n");\n break;\n case SSL_ERROR_ZERO_RETURN:\n if (cbuf_len != 0) {\n BIO_printf(bio_c_out, "shutdown\\n");\n ret = 0;\n goto shut;\n } else {\n read_tty = 1;\n write_ssl = 0;\n break;\n }\n case SSL_ERROR_SYSCALL:\n if ((k != 0) || (cbuf_len != 0)) {\n BIO_printf(bio_err, "write:errno=%d\\n",\n get_last_socket_error());\n goto shut;\n } else {\n read_tty = 1;\n write_ssl = 0;\n }\n break;\n case SSL_ERROR_SSL:\n ERR_print_errors(bio_err);\n goto shut;\n }\n }\n#if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_NETWARE)\n else if (!ssl_pending && write_tty)\n#else\n else if (!ssl_pending && FD_ISSET(fileno(stdout), &writefds))\n#endif\n {\n#ifdef CHARSET_EBCDIC\n ascii2ebcdic(&(sbuf[sbuf_off]), &(sbuf[sbuf_off]), sbuf_len);\n#endif\n i = raw_write_stdout(&(sbuf[sbuf_off]), sbuf_len);\n if (i <= 0) {\n BIO_printf(bio_c_out, "DONE\\n");\n ret = 0;\n goto shut;\n }\n sbuf_len -= i;;\n sbuf_off += i;\n if (sbuf_len <= 0) {\n read_ssl = 1;\n write_tty = 0;\n }\n } else if (ssl_pending || FD_ISSET(SSL_get_fd(con), &readfds)) {\n#ifdef RENEG\n {\n static int iiii;\n if (++iiii == 52) {\n SSL_renegotiate(con);\n iiii = 0;\n }\n }\n#endif\n k = SSL_read(con, sbuf, 1024 );\n switch (SSL_get_error(con, k)) {\n case SSL_ERROR_NONE:\n if (k <= 0)\n goto end;\n sbuf_off = 0;\n sbuf_len = k;\n read_ssl = 0;\n write_tty = 1;\n break;\n case SSL_ERROR_WANT_WRITE:\n BIO_printf(bio_c_out, "read W BLOCK\\n");\n write_ssl = 1;\n read_tty = 0;\n break;\n case SSL_ERROR_WANT_READ:\n BIO_printf(bio_c_out, "read R BLOCK\\n");\n write_tty = 0;\n read_ssl = 1;\n if ((read_tty == 0) && (write_ssl == 0))\n write_ssl = 1;\n break;\n case SSL_ERROR_WANT_X509_LOOKUP:\n BIO_printf(bio_c_out, "read X BLOCK\\n");\n break;\n case SSL_ERROR_SYSCALL:\n ret = get_last_socket_error();\n if (c_brief)\n BIO_puts(bio_err, "CONNECTION CLOSED BY SERVER\\n");\n else\n BIO_printf(bio_err, "read:errno=%d\\n", ret);\n goto shut;\n case SSL_ERROR_ZERO_RETURN:\n BIO_printf(bio_c_out, "closed\\n");\n ret = 0;\n goto shut;\n case SSL_ERROR_SSL:\n ERR_print_errors(bio_err);\n goto shut;\n }\n }\n#if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS)\n# if defined(OPENSSL_SYS_WINCE) || defined(OPENSSL_SYS_MSDOS)\n else if (_kbhit())\n# else\n else if ((_kbhit())\n || (WAIT_OBJECT_0 ==\n WaitForSingleObject(GetStdHandle(STD_INPUT_HANDLE), 0)))\n# endif\n#elif defined (OPENSSL_SYS_NETWARE)\n else if (_kbhit())\n#else\n else if (FD_ISSET(fileno(stdin), &readfds))\n#endif\n {\n if (crlf) {\n int j, lf_num;\n i = raw_read_stdin(cbuf, BUFSIZZ / 2);\n lf_num = 0;\n for (j = 0; j < i; j++)\n if (cbuf[j] == \'\\n\')\n lf_num++;\n for (j = i - 1; j >= 0; j--) {\n cbuf[j + lf_num] = cbuf[j];\n if (cbuf[j] == \'\\n\') {\n lf_num--;\n i++;\n cbuf[j + lf_num] = \'\\r\';\n }\n }\n assert(lf_num == 0);\n } else\n i = raw_read_stdin(cbuf, BUFSIZZ);\n if ((!c_ign_eof) && ((i <= 0) || (cbuf[0] == \'Q\' && cmdletters))) {\n BIO_printf(bio_err, "DONE\\n");\n ret = 0;\n goto shut;\n }\n if ((!c_ign_eof) && (cbuf[0] == \'R\' && cmdletters)) {\n BIO_printf(bio_err, "RENEGOTIATING\\n");\n SSL_renegotiate(con);\n cbuf_len = 0;\n }\n#ifndef OPENSSL_NO_HEARTBEATS\n else if ((!c_ign_eof) && (cbuf[0] == \'B\' && cmdletters)) {\n BIO_printf(bio_err, "HEARTBEATING\\n");\n SSL_heartbeat(con);\n cbuf_len = 0;\n }\n#endif\n else {\n cbuf_len = i;\n cbuf_off = 0;\n#ifdef CHARSET_EBCDIC\n ebcdic2ascii(cbuf, cbuf, i);\n#endif\n }\n write_ssl = 1;\n read_tty = 0;\n }\n }\n ret = 0;\n shut:\n if (in_init)\n print_stuff(bio_c_out, con, full_log);\n SSL_shutdown(con);\n SHUTDOWN(SSL_get_fd(con));\n end:\n if (con != NULL) {\n if (prexit != 0)\n print_stuff(bio_c_out, con, 1);\n SSL_free(con);\n }\n#if !defined(OPENSSL_NO_NEXTPROTONEG)\n OPENSSL_free(next_proto.data);\n#endif\n SSL_CTX_free(ctx);\n X509_free(cert);\n sk_X509_CRL_pop_free(crls, X509_CRL_free);\n EVP_PKEY_free(key);\n sk_X509_pop_free(chain, X509_free);\n OPENSSL_free(pass);\n X509_VERIFY_PARAM_free(vpm);\n ssl_excert_free(exc);\n sk_OPENSSL_STRING_free(ssl_args);\n SSL_CONF_CTX_free(cctx);\n OPENSSL_clear_free(cbuf, BUFSIZZ);\n OPENSSL_clear_free(sbuf, BUFSIZZ);\n OPENSSL_clear_free(mbuf, BUFSIZZ);\n BIO_free(bio_c_out);\n bio_c_out = NULL;\n BIO_free(bio_c_msg);\n bio_c_msg = NULL;\n return (ret);\n}', 'SSL *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 RECORD_LAYER_init(&s->rlayer, s);\n s->options = ctx->options;\n s->mode = ctx->mode;\n s->max_cert_list = ctx->max_cert_list;\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)\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 CRYPTO_add(&ctx->references, 1, CRYPTO_LOCK_SSL_CTX);\n s->ctx = ctx;\n s->tlsext_debug_cb = 0;\n s->tlsext_debug_arg = NULL;\n s->tlsext_ticket_expected = 0;\n s->tlsext_status_type = -1;\n s->tlsext_status_expected = 0;\n s->tlsext_ocsp_ids = NULL;\n s->tlsext_ocsp_exts = NULL;\n s->tlsext_ocsp_resp = NULL;\n s->tlsext_ocsp_resplen = -1;\n CRYPTO_add(&ctx->references, 1, CRYPTO_LOCK_SSL_CTX);\n s->initial_ctx = ctx;\n# ifndef OPENSSL_NO_EC\n if (ctx->tlsext_ecpointformatlist) {\n s->tlsext_ecpointformatlist =\n BUF_memdup(ctx->tlsext_ecpointformatlist,\n ctx->tlsext_ecpointformatlist_length);\n if (!s->tlsext_ecpointformatlist)\n goto err;\n s->tlsext_ecpointformatlist_length =\n ctx->tlsext_ecpointformatlist_length;\n }\n if (ctx->tlsext_ellipticcurvelist) {\n s->tlsext_ellipticcurvelist =\n BUF_memdup(ctx->tlsext_ellipticcurvelist,\n ctx->tlsext_ellipticcurvelist_length);\n if (!s->tlsext_ellipticcurvelist)\n goto err;\n s->tlsext_ellipticcurvelist_length =\n ctx->tlsext_ellipticcurvelist_length;\n }\n# endif\n# ifndef OPENSSL_NO_NEXTPROTONEG\n s->next_proto_negotiated = NULL;\n# endif\n if (s->ctx->alpn_client_proto_list) {\n s->alpn_client_proto_list =\n OPENSSL_malloc(s->ctx->alpn_client_proto_list_len);\n if (s->alpn_client_proto_list == NULL)\n goto err;\n memcpy(s->alpn_client_proto_list, s->ctx->alpn_client_proto_list,\n s->ctx->alpn_client_proto_list_len);\n s->alpn_client_proto_list_len = s->ctx->alpn_client_proto_list_len;\n }\n s->verify_result = X509_V_OK;\n s->method = ctx->method;\n if (!s->method->ssl_new(s))\n goto err;\n s->references = 1;\n s->server = (ctx->method->ssl_accept == ssl_undefined_function) ? 0 : 1;\n if (!SSL_clear(s))\n goto err;\n CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data);\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 return (s);\n err:\n SSL_free(s);\n SSLerr(SSL_F_SSL_NEW, ERR_R_MALLOC_FAILURE);\n return (NULL);\n}', 'int SSL_clear(SSL *s)\n{\n if (s->method == NULL) {\n SSLerr(SSL_F_SSL_CLEAR, SSL_R_NO_METHOD_SPECIFIED);\n return (0);\n }\n if (ssl_clear_bad_session(s)) {\n SSL_SESSION_free(s->session);\n s->session = NULL;\n }\n s->error = 0;\n s->hit = 0;\n s->shutdown = 0;\n if (s->renegotiate) {\n SSLerr(SSL_F_SSL_CLEAR, ERR_R_INTERNAL_ERROR);\n return 0;\n }\n s->type = 0;\n s->state = SSL_ST_BEFORE | ((s->server) ? SSL_ST_ACCEPT : SSL_ST_CONNECT);\n s->version = s->method->version;\n s->client_version = s->version;\n s->rwstate = SSL_NOTHING;\n BUF_MEM_free(s->init_buf);\n s->init_buf = NULL;\n clear_ciphers(s);\n s->first_packet = 0;\n if (!s->in_handshake && (s->session == NULL)\n && (s->method != s->ctx->method)) {\n s->method->ssl_free(s);\n s->method = s->ctx->method;\n if (!s->method->ssl_new(s))\n return (0);\n } else\n s->method->ssl_clear(s);\n RECORD_LAYER_clear(&s->rlayer);\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->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_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}', '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}']
234
0
https://github.com/nginx/nginx/blob/493b898ae94ae7dd390d77c42f5500974be48393/src/core/ngx_string.c/#L40
u_char * ngx_cpystrn(u_char *dst, u_char *src, size_t n) { if (n == 0) { return dst; } while (--n) { *dst = *src; if (*dst == '\0') { return dst; } dst++; src++; } *dst = '\0'; return dst; }
['static char *\nngx_http_log_set_log(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)\n{\n ngx_http_log_loc_conf_t *llcf = conf;\n ssize_t size;\n ngx_int_t gzip;\n ngx_uint_t i, n;\n ngx_msec_t flush;\n ngx_str_t *value, name, s, filter;\n ngx_http_log_t *log;\n ngx_syslog_peer_t *peer;\n ngx_http_log_buf_t *buffer;\n ngx_http_log_fmt_t *fmt;\n ngx_http_log_main_conf_t *lmcf;\n ngx_http_script_compile_t sc;\n ngx_http_compile_complex_value_t ccv;\n value = cf->args->elts;\n if (ngx_strcmp(value[1].data, "off") == 0) {\n llcf->off = 1;\n if (cf->args->nelts == 2) {\n return NGX_CONF_OK;\n }\n ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,\n "invalid parameter \\"%V\\"", &value[2]);\n return NGX_CONF_ERROR;\n }\n if (llcf->logs == NULL) {\n llcf->logs = ngx_array_create(cf->pool, 2, sizeof(ngx_http_log_t));\n if (llcf->logs == NULL) {\n return NGX_CONF_ERROR;\n }\n }\n lmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_log_module);\n log = ngx_array_push(llcf->logs);\n if (log == NULL) {\n return NGX_CONF_ERROR;\n }\n ngx_memzero(log, sizeof(ngx_http_log_t));\n if (ngx_strncmp(value[1].data, "syslog:", 7) == 0) {\n peer = ngx_pcalloc(cf->pool, sizeof(ngx_syslog_peer_t));\n if (peer == NULL) {\n return NGX_CONF_ERROR;\n }\n if (ngx_syslog_process_conf(cf, peer) != NGX_CONF_OK) {\n return NGX_CONF_ERROR;\n }\n log->syslog_peer = peer;\n goto process_formats;\n }\n n = ngx_http_script_variables_count(&value[1]);\n if (n == 0) {\n log->file = ngx_conf_open_file(cf->cycle, &value[1]);\n if (log->file == NULL) {\n return NGX_CONF_ERROR;\n }\n } else {\n if (ngx_conf_full_name(cf->cycle, &value[1], 0) != NGX_OK) {\n return NGX_CONF_ERROR;\n }\n log->script = ngx_pcalloc(cf->pool, sizeof(ngx_http_log_script_t));\n if (log->script == NULL) {\n return NGX_CONF_ERROR;\n }\n ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));\n sc.cf = cf;\n sc.source = &value[1];\n sc.lengths = &log->script->lengths;\n sc.values = &log->script->values;\n sc.variables = n;\n sc.complete_lengths = 1;\n sc.complete_values = 1;\n if (ngx_http_script_compile(&sc) != NGX_OK) {\n return NGX_CONF_ERROR;\n }\n }\nprocess_formats:\n if (cf->args->nelts >= 3) {\n name = value[2];\n if (ngx_strcmp(name.data, "combined") == 0) {\n lmcf->combined_used = 1;\n }\n } else {\n ngx_str_set(&name, "combined");\n lmcf->combined_used = 1;\n }\n fmt = lmcf->formats.elts;\n for (i = 0; i < lmcf->formats.nelts; i++) {\n if (fmt[i].name.len == name.len\n && ngx_strcasecmp(fmt[i].name.data, name.data) == 0)\n {\n log->format = &fmt[i];\n break;\n }\n }\n if (log->format == NULL) {\n ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,\n "unknown log format \\"%V\\"", &name);\n return NGX_CONF_ERROR;\n }\n if (log->syslog_peer != NULL) {\n if (cf->args->nelts > 3) {\n ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,\n "parameter \\"%V\\" is not supported by syslog",\n &value[3]);\n return NGX_CONF_ERROR;\n }\n return NGX_CONF_OK;\n }\n size = 0;\n flush = 0;\n gzip = 0;\n filter.len = 0;\n for (i = 3; i < cf->args->nelts; i++) {\n if (ngx_strncmp(value[i].data, "buffer=", 7) == 0) {\n s.len = value[i].len - 7;\n s.data = value[i].data + 7;\n size = ngx_parse_size(&s);\n if (size == NGX_ERROR || size == 0) {\n ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,\n "invalid buffer size \\"%V\\"", &s);\n return NGX_CONF_ERROR;\n }\n continue;\n }\n if (ngx_strncmp(value[i].data, "flush=", 6) == 0) {\n s.len = value[i].len - 6;\n s.data = value[i].data + 6;\n flush = ngx_parse_time(&s, 0);\n if (flush == (ngx_msec_t) NGX_ERROR || flush == 0) {\n ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,\n "invalid flush time \\"%V\\"", &s);\n return NGX_CONF_ERROR;\n }\n continue;\n }\n if (ngx_strncmp(value[i].data, "gzip", 4) == 0\n && (value[i].len == 4 || value[i].data[4] == \'=\'))\n {\n#if (NGX_ZLIB)\n if (size == 0) {\n size = 64 * 1024;\n }\n if (value[i].len == 4) {\n gzip = Z_BEST_SPEED;\n continue;\n }\n s.len = value[i].len - 5;\n s.data = value[i].data + 5;\n gzip = ngx_atoi(s.data, s.len);\n if (gzip < 1 || gzip > 9) {\n ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,\n "invalid compression level \\"%V\\"", &s);\n return NGX_CONF_ERROR;\n }\n continue;\n#else\n ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,\n "nginx was built without zlib support");\n return NGX_CONF_ERROR;\n#endif\n }\n if (ngx_strncmp(value[i].data, "if=", 3) == 0) {\n filter.len = value[i].len - 3;\n filter.data = value[i].data + 3;\n continue;\n }\n ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,\n "invalid parameter \\"%V\\"", &value[i]);\n return NGX_CONF_ERROR;\n }\n if (flush && size == 0) {\n ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,\n "no buffer is defined for access_log \\"%V\\"",\n &value[1]);\n return NGX_CONF_ERROR;\n }\n if (size) {\n if (log->script) {\n ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,\n "buffered logs cannot have variables in name");\n return NGX_CONF_ERROR;\n }\n if (log->file->data) {\n buffer = log->file->data;\n if (buffer->last - buffer->start != size\n || buffer->flush != flush\n || buffer->gzip != gzip)\n {\n ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,\n "access_log \\"%V\\" already defined "\n "with conflicting parameters",\n &value[1]);\n return NGX_CONF_ERROR;\n }\n return NGX_CONF_OK;\n }\n buffer = ngx_pcalloc(cf->pool, sizeof(ngx_http_log_buf_t));\n if (buffer == NULL) {\n return NGX_CONF_ERROR;\n }\n buffer->start = ngx_pnalloc(cf->pool, size);\n if (buffer->start == NULL) {\n return NGX_CONF_ERROR;\n }\n buffer->pos = buffer->start;\n buffer->last = buffer->start + size;\n if (flush) {\n buffer->event = ngx_pcalloc(cf->pool, sizeof(ngx_event_t));\n if (buffer->event == NULL) {\n return NGX_CONF_ERROR;\n }\n buffer->event->data = log->file;\n buffer->event->handler = ngx_http_log_flush_handler;\n buffer->event->log = &cf->cycle->new_log;\n buffer->flush = flush;\n }\n buffer->gzip = gzip;\n log->file->flush = ngx_http_log_flush;\n log->file->data = buffer;\n }\n if (filter.len) {\n log->filter = ngx_palloc(cf->pool, sizeof(ngx_http_complex_value_t));\n if (log->filter == NULL) {\n return NGX_CONF_ERROR;\n }\n ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));\n ccv.cf = cf;\n ccv.value = &filter;\n ccv.complex_value = log->filter;\n if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {\n return NGX_CONF_ERROR;\n }\n }\n return NGX_CONF_OK;\n}', "ngx_uint_t\nngx_http_script_variables_count(ngx_str_t *value)\n{\n ngx_uint_t i, n;\n for (n = 0, i = 0; i < value->len; i++) {\n if (value->data[i] == '$') {\n n++;\n }\n }\n return n;\n}", 'ngx_int_t\nngx_conf_full_name(ngx_cycle_t *cycle, ngx_str_t *name, ngx_uint_t conf_prefix)\n{\n ngx_str_t *prefix;\n prefix = conf_prefix ? &cycle->conf_prefix : &cycle->prefix;\n return ngx_get_full_name(cycle->pool, prefix, name);\n}', 'ngx_int_t\nngx_get_full_name(ngx_pool_t *pool, ngx_str_t *prefix, ngx_str_t *name)\n{\n size_t len;\n u_char *p, *n;\n ngx_int_t rc;\n rc = ngx_test_full_name(name);\n if (rc == NGX_OK) {\n return rc;\n }\n len = prefix->len;\n#if (NGX_WIN32)\n if (rc == 2) {\n len = rc;\n }\n#endif\n n = ngx_pnalloc(pool, len + name->len + 1);\n if (n == NULL) {\n return NGX_ERROR;\n }\n p = ngx_cpymem(n, prefix->data, len);\n ngx_cpystrn(p, name->data, name->len + 1);\n name->len += len;\n name->data = n;\n return NGX_OK;\n}', "ngx_int_t\nngx_http_compile_complex_value(ngx_http_compile_complex_value_t *ccv)\n{\n ngx_str_t *v;\n ngx_uint_t i, n, nv, nc;\n ngx_array_t flushes, lengths, values, *pf, *pl, *pv;\n ngx_http_script_compile_t sc;\n v = ccv->value;\n nv = 0;\n nc = 0;\n for (i = 0; i < v->len; i++) {\n if (v->data[i] == '$') {\n if (v->data[i + 1] >= '1' && v->data[i + 1] <= '9') {\n nc++;\n } else {\n nv++;\n }\n }\n }\n if ((v->len == 0 || v->data[0] != '$')\n && (ccv->conf_prefix || ccv->root_prefix))\n {\n if (ngx_conf_full_name(ccv->cf->cycle, v, ccv->conf_prefix) != NGX_OK) {\n return NGX_ERROR;\n }\n ccv->conf_prefix = 0;\n ccv->root_prefix = 0;\n }\n ccv->complex_value->value = *v;\n ccv->complex_value->flushes = NULL;\n ccv->complex_value->lengths = NULL;\n ccv->complex_value->values = NULL;\n if (nv == 0 && nc == 0) {\n return NGX_OK;\n }\n n = nv + 1;\n if (ngx_array_init(&flushes, ccv->cf->pool, n, sizeof(ngx_uint_t))\n != NGX_OK)\n {\n return NGX_ERROR;\n }\n n = nv * (2 * sizeof(ngx_http_script_copy_code_t)\n + sizeof(ngx_http_script_var_code_t))\n + sizeof(uintptr_t);\n if (ngx_array_init(&lengths, ccv->cf->pool, n, 1) != NGX_OK) {\n return NGX_ERROR;\n }\n n = (nv * (2 * sizeof(ngx_http_script_copy_code_t)\n + sizeof(ngx_http_script_var_code_t))\n + sizeof(uintptr_t)\n + v->len\n + sizeof(uintptr_t) - 1)\n & ~(sizeof(uintptr_t) - 1);\n if (ngx_array_init(&values, ccv->cf->pool, n, 1) != NGX_OK) {\n return NGX_ERROR;\n }\n pf = &flushes;\n pl = &lengths;\n pv = &values;\n ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));\n sc.cf = ccv->cf;\n sc.source = v;\n sc.flushes = &pf;\n sc.lengths = &pl;\n sc.values = &pv;\n sc.complete_lengths = 1;\n sc.complete_values = 1;\n sc.zero = ccv->zero;\n sc.conf_prefix = ccv->conf_prefix;\n sc.root_prefix = ccv->root_prefix;\n if (ngx_http_script_compile(&sc) != NGX_OK) {\n return NGX_ERROR;\n }\n if (flushes.nelts) {\n ccv->complex_value->flushes = flushes.elts;\n ccv->complex_value->flushes[flushes.nelts] = (ngx_uint_t) -1;\n }\n ccv->complex_value->lengths = lengths.elts;\n ccv->complex_value->values = values.elts;\n return NGX_OK;\n}", "u_char *\nngx_cpystrn(u_char *dst, u_char *src, size_t n)\n{\n if (n == 0) {\n return dst;\n }\n while (--n) {\n *dst = *src;\n if (*dst == '\\0') {\n return dst;\n }\n dst++;\n src++;\n }\n *dst = '\\0';\n return dst;\n}"]
235
0
https://github.com/openssl/openssl/blob/8da94770f0a049497b1a52ee469cca1f4a13b1a7/crypto/bn/bn_mul.c/#L1072
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 RSA_check_key_ex(const RSA *key, BN_GENCB *cb)\n{\n BIGNUM *i, *j, *k, *l, *m;\n BN_CTX *ctx;\n int r;\n int ret = 1;\n if (!key->p || !key->q || !key->n || !key->e || !key->d) {\n RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_VALUE_MISSING);\n return 0;\n }\n i = BN_new();\n j = BN_new();\n k = BN_new();\n l = BN_new();\n m = BN_new();\n ctx = BN_CTX_new();\n if (i == NULL || j == NULL || k == NULL || l == NULL ||\n m == NULL || ctx == NULL) {\n ret = -1;\n RSAerr(RSA_F_RSA_CHECK_KEY_EX, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n r = BN_is_prime_ex(key->p, BN_prime_checks, NULL, cb);\n if (r != 1) {\n ret = r;\n if (r != 0)\n goto err;\n RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_P_NOT_PRIME);\n }\n r = BN_is_prime_ex(key->q, BN_prime_checks, NULL, cb);\n if (r != 1) {\n ret = r;\n if (r != 0)\n goto err;\n RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_Q_NOT_PRIME);\n }\n r = BN_mul(i, key->p, key->q, ctx);\n if (!r) {\n ret = -1;\n goto err;\n }\n if (BN_cmp(i, key->n) != 0) {\n ret = 0;\n RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_N_DOES_NOT_EQUAL_P_Q);\n }\n r = BN_sub(i, key->p, BN_value_one());\n if (!r) {\n ret = -1;\n goto err;\n }\n r = BN_sub(j, key->q, BN_value_one());\n if (!r) {\n ret = -1;\n goto err;\n }\n r = BN_mul(l, i, j, ctx);\n if (!r) {\n ret = -1;\n goto err;\n }\n r = BN_gcd(m, i, j, ctx);\n if (!r) {\n ret = -1;\n goto err;\n }\n r = BN_div(k, NULL, l, m, ctx);\n if (!r) {\n ret = -1;\n goto err;\n }\n r = BN_mod_mul(i, key->d, key->e, k, ctx);\n if (!r) {\n ret = -1;\n goto err;\n }\n if (!BN_is_one(i)) {\n ret = 0;\n RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_D_E_NOT_CONGRUENT_TO_1);\n }\n if (key->dmp1 != NULL && key->dmq1 != NULL && key->iqmp != NULL) {\n r = BN_sub(i, key->p, BN_value_one());\n if (!r) {\n ret = -1;\n goto err;\n }\n r = BN_mod(j, key->d, i, ctx);\n if (!r) {\n ret = -1;\n goto err;\n }\n if (BN_cmp(j, key->dmp1) != 0) {\n ret = 0;\n RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_DMP1_NOT_CONGRUENT_TO_D);\n }\n r = BN_sub(i, key->q, BN_value_one());\n if (!r) {\n ret = -1;\n goto err;\n }\n r = BN_mod(j, key->d, i, ctx);\n if (!r) {\n ret = -1;\n goto err;\n }\n if (BN_cmp(j, key->dmq1) != 0) {\n ret = 0;\n RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_DMQ1_NOT_CONGRUENT_TO_D);\n }\n if (!BN_mod_inverse(i, key->q, key->p, ctx)) {\n ret = -1;\n goto err;\n }\n if (BN_cmp(i, key->iqmp) != 0) {\n ret = 0;\n RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_IQMP_NOT_INVERSE_OF_Q);\n }\n }\n err:\n BN_free(i);\n BN_free(j);\n BN_free(k);\n BN_free(l);\n BN_free(m);\n BN_CTX_free(ctx);\n return (ret);\n}', 'int BN_is_prime_ex(const BIGNUM *a, int checks, BN_CTX *ctx_passed,\n BN_GENCB *cb)\n{\n return BN_is_prime_fasttest_ex(a, checks, ctx_passed, 0, cb);\n}', 'int BN_is_prime_fasttest_ex(const BIGNUM *a, int checks, BN_CTX *ctx_passed,\n int do_trial_division, BN_GENCB *cb)\n{\n int i, j, ret = -1;\n int k;\n BN_CTX *ctx = NULL;\n BIGNUM *A1, *A1_odd, *check;\n BN_MONT_CTX *mont = NULL;\n const BIGNUM *A = NULL;\n if (BN_cmp(a, BN_value_one()) <= 0)\n return 0;\n if (checks == BN_prime_checks)\n checks = BN_prime_checks_for_size(BN_num_bits(a));\n if (!BN_is_odd(a))\n return BN_is_word(a, 2);\n if (do_trial_division) {\n for (i = 1; i < NUMPRIMES; i++)\n if (BN_mod_word(a, primes[i]) == 0)\n return 0;\n if (!BN_GENCB_call(cb, 1, -1))\n goto err;\n }\n if (ctx_passed != NULL)\n ctx = ctx_passed;\n else if ((ctx = BN_CTX_new()) == NULL)\n goto err;\n BN_CTX_start(ctx);\n if (a->neg) {\n BIGNUM *t;\n if ((t = BN_CTX_get(ctx)) == NULL)\n goto err;\n BN_copy(t, a);\n t->neg = 0;\n A = t;\n } else\n A = a;\n A1 = BN_CTX_get(ctx);\n A1_odd = BN_CTX_get(ctx);\n check = BN_CTX_get(ctx);\n if (check == NULL)\n goto err;\n if (!BN_copy(A1, A))\n goto err;\n if (!BN_sub_word(A1, 1))\n goto err;\n if (BN_is_zero(A1)) {\n ret = 0;\n goto err;\n }\n k = 1;\n while (!BN_is_bit_set(A1, k))\n k++;\n if (!BN_rshift(A1_odd, A1, k))\n goto err;\n mont = BN_MONT_CTX_new();\n if (mont == NULL)\n goto err;\n if (!BN_MONT_CTX_set(mont, A, ctx))\n goto err;\n for (i = 0; i < checks; i++) {\n if (!BN_pseudo_rand_range(check, A1))\n goto err;\n if (!BN_add_word(check, 1))\n goto err;\n j = witness(check, A, A1, A1_odd, k, ctx, mont);\n if (j == -1)\n goto err;\n if (j) {\n ret = 0;\n goto err;\n }\n if (!BN_GENCB_call(cb, 1, i))\n goto err;\n }\n ret = 1;\n err:\n if (ctx != NULL) {\n BN_CTX_end(ctx);\n if (ctx_passed == NULL)\n BN_CTX_free(ctx);\n }\n 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}', 'BIGNUM *BN_CTX_get(BN_CTX *ctx)\n{\n BIGNUM *ret;\n CTXDBG_ENTRY("BN_CTX_get", ctx);\n if (ctx->err_stack || ctx->too_many)\n return NULL;\n if ((ret = BN_POOL_get(&ctx->pool, ctx->flags)) == NULL) {\n ctx->too_many = 1;\n BNerr(BN_F_BN_CTX_GET, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n return NULL;\n }\n BN_zero(ret);\n ctx->used++;\n CTXDBG_RET(ctx, ret);\n return ret;\n}', 'int BN_set_word(BIGNUM *a, BN_ULONG w)\n{\n bn_check_top(a);\n if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)\n return (0);\n a->neg = 0;\n a->d[0] = w;\n a->top = (w ? 1 : 0);\n bn_check_top(a);\n return (1);\n}', 'BIGNUM *BN_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}', 'BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)\n{\n int i;\n BN_ULONG *A;\n const BN_ULONG *B;\n bn_check_top(b);\n if (a == b)\n return (a);\n if (bn_wexpand(a, b->top) == NULL)\n return (NULL);\n#if 1\n A = a->d;\n B = b->d;\n for (i = b->top >> 2; i > 0; i--, A += 4, B += 4) {\n BN_ULONG a0, a1, a2, a3;\n a0 = B[0];\n a1 = B[1];\n a2 = B[2];\n a3 = B[3];\n A[0] = a0;\n A[1] = a1;\n A[2] = a2;\n A[3] = a3;\n }\n switch (b->top & 3) {\n case 3:\n A[2] = B[2];\n case 2:\n A[1] = B[1];\n case 1:\n A[0] = B[0];\n case 0:;\n }\n#else\n memcpy(a->d, b->d, sizeof(b->d[0]) * b->top);\n#endif\n a->top = b->top;\n a->neg = b->neg;\n bn_check_top(a);\n return (a);\n}', 'int BN_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_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n2,\n int dna, int dnb, BN_ULONG *t)\n{\n int n = n2 / 2, c1, c2;\n int tna = n + dna, tnb = n + dnb;\n unsigned int neg, zero;\n BN_ULONG ln, lo, *p;\n# ifdef BN_MUL_COMBA\n# if 0\n if (n2 == 4) {\n bn_mul_comba4(r, a, b);\n return;\n }\n# endif\n if (n2 == 8 && dna == 0 && dnb == 0) {\n bn_mul_comba8(r, a, b);\n return;\n }\n# endif\n if (n2 < BN_MUL_RECURSIVE_SIZE_NORMAL) {\n bn_mul_normal(r, a, n2 + dna, b, n2 + dnb);\n if ((dna + dnb) < 0)\n memset(&r[2 * n2 + dna + dnb], 0,\n sizeof(BN_ULONG) * -(dna + dnb));\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 zero = 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 zero = 1;\n break;\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 zero = 1;\n break;\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 zero = 1;\n break;\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# ifdef BN_MUL_COMBA\n if (n == 4 && dna == 0 && dnb == 0) {\n if (!zero)\n bn_mul_comba4(&(t[n2]), t, &(t[n]));\n else\n memset(&t[n2], 0, sizeof(*t) * 8);\n bn_mul_comba4(r, a, b);\n bn_mul_comba4(&(r[n2]), &(a[n]), &(b[n]));\n } else if (n == 8 && dna == 0 && dnb == 0) {\n if (!zero)\n bn_mul_comba8(&(t[n2]), t, &(t[n]));\n else\n memset(&t[n2], 0, sizeof(*t) * 16);\n bn_mul_comba8(r, a, b);\n bn_mul_comba8(&(r[n2]), &(a[n]), &(b[n]));\n } else\n# endif\n {\n p = &(t[n2 * 2]);\n if (!zero)\n bn_mul_recursive(&(t[n2]), t, &(t[n]), n, 0, 0, p);\n else\n memset(&t[n2], 0, sizeof(*t) * n2);\n bn_mul_recursive(r, a, b, n, 0, 0, p);\n bn_mul_recursive(&(r[n2]), &(a[n]), &(b[n]), n, dna, dnb, p);\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}']
236
0
https://github.com/openssl/openssl/blob/5d1c09de1f2736e1d4b1877206d08455ec75f558/crypto/bn/bn_sqr.c/#L114
void bn_sqr_normal(BN_ULONG *r, const BN_ULONG *a, int n, BN_ULONG *tmp) { int i, j, max; const BN_ULONG *ap; BN_ULONG *rp; max = n * 2; ap = a; rp = r; rp[0] = rp[max - 1] = 0; rp++; j = n; if (--j > 0) { ap++; rp[j] = bn_mul_words(rp, ap, j, ap[-1]); rp += 2; } for (i = n - 2; i > 0; i--) { j--; ap++; rp[j] = bn_mul_add_words(rp, ap, j, ap[-1]); rp += 2; } bn_add_words(r, r, r, max); bn_sqr_words(tmp, a, n); bn_add_words(r, r, tmp, max); }
['static EVP_PKEY *b2i_dss(const unsigned char **in,\n unsigned int bitlen, int ispub)\n{\n const unsigned char *p = *in;\n EVP_PKEY *ret = NULL;\n DSA *dsa = NULL;\n BN_CTX *ctx = NULL;\n unsigned int nbyte;\n BIGNUM *pbn = NULL, *qbn = NULL, *gbn = NULL, *priv_key = NULL;\n BIGNUM *pub_key = NULL;\n nbyte = (bitlen + 7) >> 3;\n dsa = DSA_new();\n ret = EVP_PKEY_new();\n if (dsa == NULL || ret == NULL)\n goto memerr;\n if (!read_lebn(&p, nbyte, &pbn))\n goto memerr;\n if (!read_lebn(&p, 20, &qbn))\n goto memerr;\n if (!read_lebn(&p, nbyte, &gbn))\n goto memerr;\n if (ispub) {\n if (!read_lebn(&p, nbyte, &pub_key))\n goto memerr;\n } else {\n if (!read_lebn(&p, 20, &priv_key))\n goto memerr;\n pub_key = BN_new();\n if (pub_key == NULL)\n goto memerr;\n if ((ctx = BN_CTX_new()) == NULL)\n goto memerr;\n if (!BN_mod_exp(pub_key, gbn, priv_key, pbn, ctx))\n goto memerr;\n BN_CTX_free(ctx);\n ctx = NULL;\n }\n if (!DSA_set0_pqg(dsa, pbn, qbn, gbn))\n goto memerr;\n pbn = qbn = gbn = NULL;\n if (!DSA_set0_key(dsa, pub_key, priv_key))\n goto memerr;\n pub_key = priv_key = NULL;\n if (!EVP_PKEY_set1_DSA(ret, dsa))\n goto memerr;\n DSA_free(dsa);\n *in = p;\n return ret;\n memerr:\n PEMerr(PEM_F_B2I_DSS, ERR_R_MALLOC_FAILURE);\n DSA_free(dsa);\n BN_free(pbn);\n BN_free(qbn);\n BN_free(gbn);\n BN_free(pub_key);\n BN_free(priv_key);\n EVP_PKEY_free(ret);\n BN_CTX_free(ctx);\n return NULL;\n}', 'static int read_lebn(const unsigned char **in, unsigned int nbyte, BIGNUM **r)\n{\n *r = BN_lebin2bn(*in, nbyte, NULL);\n if (*r == NULL)\n return 0;\n *in += nbyte;\n return 1;\n}', 'BIGNUM *BN_lebin2bn(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 s += len;\n for ( ; len > 0 && s[-1] == 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 s--;\n l = (l << 8L) | *s;\n if (m-- == 0) {\n ret->d[--i] = l;\n l = 0;\n m = BN_BYTES - 1;\n }\n }\n bn_correct_top(ret);\n return ret;\n}', 'int BN_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m,\n BN_CTX *ctx)\n{\n int ret;\n bn_check_top(a);\n bn_check_top(p);\n bn_check_top(m);\n#define MONT_MUL_MOD\n#define MONT_EXP_WORD\n#define RECP_MUL_MOD\n#ifdef MONT_MUL_MOD\n if (BN_is_odd(m)) {\n# ifdef MONT_EXP_WORD\n if (a->top == 1 && !a->neg\n && (BN_get_flags(p, BN_FLG_CONSTTIME) == 0)\n && (BN_get_flags(a, BN_FLG_CONSTTIME) == 0)\n && (BN_get_flags(m, BN_FLG_CONSTTIME) == 0)) {\n BN_ULONG A = a->d[0];\n ret = BN_mod_exp_mont_word(r, A, p, m, ctx, NULL);\n } else\n# endif\n ret = BN_mod_exp_mont(r, a, p, m, ctx, NULL);\n } else\n#endif\n#ifdef RECP_MUL_MOD\n {\n ret = BN_mod_exp_recp(r, a, p, m, ctx);\n }\n#else\n {\n ret = BN_mod_exp_simple(r, a, p, m, ctx);\n }\n#endif\n bn_check_top(r);\n return ret;\n}', 'int BN_mod_exp_mont(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p,\n const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont)\n{\n int i, j, bits, ret = 0, wstart, wend, window, wvalue;\n int start = 1;\n BIGNUM *d, *r;\n const BIGNUM *aa;\n BIGNUM *val[TABLE_SIZE];\n BN_MONT_CTX *mont = NULL;\n if (BN_get_flags(p, BN_FLG_CONSTTIME) != 0\n || BN_get_flags(a, BN_FLG_CONSTTIME) != 0\n || BN_get_flags(m, BN_FLG_CONSTTIME) != 0) {\n return BN_mod_exp_mont_consttime(rr, a, p, m, ctx, in_mont);\n }\n bn_check_top(a);\n bn_check_top(p);\n bn_check_top(m);\n if (!BN_is_odd(m)) {\n BNerr(BN_F_BN_MOD_EXP_MONT, BN_R_CALLED_WITH_EVEN_MODULUS);\n return 0;\n }\n bits = BN_num_bits(p);\n if (bits == 0) {\n if (BN_abs_is_word(m, 1)) {\n ret = 1;\n BN_zero(rr);\n } else {\n ret = BN_one(rr);\n }\n return ret;\n }\n BN_CTX_start(ctx);\n d = BN_CTX_get(ctx);\n r = BN_CTX_get(ctx);\n val[0] = BN_CTX_get(ctx);\n if (val[0] == NULL)\n goto err;\n if (in_mont != NULL)\n mont = in_mont;\n else {\n if ((mont = BN_MONT_CTX_new()) == NULL)\n goto err;\n if (!BN_MONT_CTX_set(mont, m, ctx))\n goto err;\n }\n if (a->neg || BN_ucmp(a, m) >= 0) {\n if (!BN_nnmod(val[0], a, m, ctx))\n goto err;\n aa = val[0];\n } else\n aa = a;\n if (BN_is_zero(aa)) {\n BN_zero(rr);\n ret = 1;\n goto err;\n }\n if (!bn_to_mont_fixed_top(val[0], aa, mont, ctx))\n goto err;\n window = BN_window_bits_for_exponent_size(bits);\n if (window > 1) {\n if (!bn_mul_mont_fixed_top(d, val[0], val[0], mont, ctx))\n goto err;\n j = 1 << (window - 1);\n for (i = 1; i < j; i++) {\n if (((val[i] = BN_CTX_get(ctx)) == NULL) ||\n !bn_mul_mont_fixed_top(val[i], val[i - 1], d, mont, ctx))\n goto err;\n }\n }\n start = 1;\n wvalue = 0;\n wstart = bits - 1;\n wend = 0;\n#if 1\n j = m->top;\n if (m->d[j - 1] & (((BN_ULONG)1) << (BN_BITS2 - 1))) {\n if (bn_wexpand(r, j) == NULL)\n goto err;\n r->d[0] = (0 - m->d[0]) & BN_MASK2;\n for (i = 1; i < j; i++)\n r->d[i] = (~m->d[i]) & BN_MASK2;\n r->top = j;\n r->flags |= BN_FLG_FIXED_TOP;\n } else\n#endif\n if (!bn_to_mont_fixed_top(r, BN_value_one(), mont, ctx))\n goto err;\n for (;;) {\n if (BN_is_bit_set(p, wstart) == 0) {\n if (!start) {\n if (!bn_mul_mont_fixed_top(r, r, r, mont, ctx))\n goto err;\n }\n if (wstart == 0)\n break;\n wstart--;\n continue;\n }\n j = wstart;\n wvalue = 1;\n wend = 0;\n for (i = 1; i < window; i++) {\n if (wstart - i < 0)\n break;\n if (BN_is_bit_set(p, wstart - i)) {\n wvalue <<= (i - wend);\n wvalue |= 1;\n wend = i;\n }\n }\n j = wend + 1;\n if (!start)\n for (i = 0; i < j; i++) {\n if (!bn_mul_mont_fixed_top(r, r, r, mont, ctx))\n goto err;\n }\n if (!bn_mul_mont_fixed_top(r, r, val[wvalue >> 1], mont, ctx))\n goto err;\n wstart -= wend + 1;\n wvalue = 0;\n start = 0;\n if (wstart < 0)\n break;\n }\n#if defined(SPARC_T4_MONT)\n if (OPENSSL_sparcv9cap_P[0] & (SPARCV9_VIS3 | SPARCV9_PREFER_FPU)) {\n j = mont->N.top;\n val[0]->d[0] = 1;\n for (i = 1; i < j; i++)\n val[0]->d[i] = 0;\n val[0]->top = j;\n if (!BN_mod_mul_montgomery(rr, r, val[0], mont, ctx))\n goto err;\n } else\n#endif\n if (!BN_from_montgomery(rr, r, mont, ctx))\n goto err;\n ret = 1;\n err:\n if (in_mont == NULL)\n BN_MONT_CTX_free(mont);\n BN_CTX_end(ctx);\n bn_check_top(rr);\n return ret;\n}', 'int BN_mod_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_mont_fixed_top(&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_mont_fixed_top(&am, &am, mont, ctx))\n goto err;\n } else if (!bn_to_mont_fixed_top(&am, a, mont, ctx))\n goto err;\n#if defined(SPARC_T4_MONT)\n if (t4) {\n typedef int (*bn_pwr5_mont_f) (BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n int bn_pwr5_mont_t4_8(BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n int bn_pwr5_mont_t4_16(BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n int bn_pwr5_mont_t4_24(BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n int bn_pwr5_mont_t4_32(BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n static const bn_pwr5_mont_f pwr5_funcs[4] = {\n bn_pwr5_mont_t4_8, bn_pwr5_mont_t4_16,\n bn_pwr5_mont_t4_24, bn_pwr5_mont_t4_32\n };\n bn_pwr5_mont_f pwr5_worker = pwr5_funcs[top / 16 - 1];\n typedef int (*bn_mul_mont_f) (BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0);\n int bn_mul_mont_t4_8(BN_ULONG *rp, const BN_ULONG *ap, const void *bp,\n const BN_ULONG *np, const BN_ULONG *n0);\n int bn_mul_mont_t4_16(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0);\n int bn_mul_mont_t4_24(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0);\n int bn_mul_mont_t4_32(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0);\n static const bn_mul_mont_f mul_funcs[4] = {\n bn_mul_mont_t4_8, bn_mul_mont_t4_16,\n bn_mul_mont_t4_24, bn_mul_mont_t4_32\n };\n bn_mul_mont_f mul_worker = mul_funcs[top / 16 - 1];\n void bn_mul_mont_vis3(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0, int num);\n void bn_mul_mont_t4(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0, int num);\n void bn_mul_mont_gather5_t4(BN_ULONG *rp, const BN_ULONG *ap,\n const void *table, const BN_ULONG *np,\n const BN_ULONG *n0, int num, int power);\n void bn_flip_n_scatter5_t4(const BN_ULONG *inp, size_t num,\n void *table, size_t power);\n void bn_gather5_t4(BN_ULONG *out, size_t num,\n void *table, size_t power);\n void bn_flip_t4(BN_ULONG *dst, BN_ULONG *src, size_t num);\n BN_ULONG *np = mont->N.d, *n0 = mont->n0;\n int stride = 5 * (6 - (top / 16 - 1));\n for (i = am.top; i < top; i++)\n am.d[i] = 0;\n for (i = tmp.top; i < top; i++)\n tmp.d[i] = 0;\n bn_flip_n_scatter5_t4(tmp.d, top, powerbuf, 0);\n bn_flip_n_scatter5_t4(am.d, top, powerbuf, 1);\n if (!(*mul_worker) (tmp.d, am.d, am.d, np, n0) &&\n !(*mul_worker) (tmp.d, am.d, am.d, np, n0))\n bn_mul_mont_vis3(tmp.d, am.d, am.d, np, n0, top);\n bn_flip_n_scatter5_t4(tmp.d, top, powerbuf, 2);\n for (i = 3; i < 32; i++) {\n if (!(*mul_worker) (tmp.d, tmp.d, am.d, np, n0) &&\n !(*mul_worker) (tmp.d, tmp.d, am.d, np, n0))\n bn_mul_mont_vis3(tmp.d, tmp.d, am.d, np, n0, top);\n bn_flip_n_scatter5_t4(tmp.d, top, powerbuf, i);\n }\n np = alloca(top * sizeof(BN_ULONG));\n top /= 2;\n bn_flip_t4(np, mont->N.d, top);\n window0 = (bits - 1) % 5 + 1;\n wmask = (1 << window0) - 1;\n bits -= window0;\n wvalue = bn_get_bits(p, bits) & wmask;\n bn_gather5_t4(tmp.d, top, powerbuf, wvalue);\n while (bits > 0) {\n if (bits < stride)\n stride = bits;\n bits -= stride;\n wvalue = bn_get_bits(p, bits);\n if ((*pwr5_worker) (tmp.d, np, n0, powerbuf, wvalue, stride))\n continue;\n if ((*pwr5_worker) (tmp.d, np, n0, powerbuf, wvalue, stride))\n continue;\n bits += stride - 5;\n wvalue >>= stride - 5;\n wvalue &= 31;\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_gather5_t4(tmp.d, tmp.d, powerbuf, np, n0, top,\n wvalue);\n }\n bn_flip_t4(tmp.d, tmp.d, top);\n top *= 2;\n tmp.top = top;\n bn_correct_top(&tmp);\n OPENSSL_cleanse(np, top * sizeof(BN_ULONG));\n } else\n#endif\n#if defined(OPENSSL_BN_ASM_MONT5)\n if (window == 5 && top > 1) {\n void bn_mul_mont_gather5(BN_ULONG *rp, const BN_ULONG *ap,\n const void *table, const BN_ULONG *np,\n const BN_ULONG *n0, int num, int power);\n void bn_scatter5(const BN_ULONG *inp, size_t num,\n void *table, size_t power);\n void bn_gather5(BN_ULONG *out, size_t num, void *table, size_t power);\n void bn_power5(BN_ULONG *rp, const BN_ULONG *ap,\n const void *table, const BN_ULONG *np,\n const BN_ULONG *n0, int num, int power);\n int bn_get_bits5(const BN_ULONG *ap, int off);\n int bn_from_montgomery(BN_ULONG *rp, const BN_ULONG *ap,\n const BN_ULONG *not_used, const BN_ULONG *np,\n const BN_ULONG *n0, int num);\n BN_ULONG *n0 = mont->n0, *np;\n for (i = am.top; i < top; i++)\n am.d[i] = 0;\n for (i = tmp.top; i < top; i++)\n tmp.d[i] = 0;\n for (np = am.d + top, i = 0; i < top; i++)\n np[i] = mont->N.d[i];\n bn_scatter5(tmp.d, top, powerbuf, 0);\n bn_scatter5(am.d, am.top, powerbuf, 1);\n bn_mul_mont(tmp.d, am.d, am.d, np, n0, top);\n bn_scatter5(tmp.d, top, powerbuf, 2);\n# if 0\n for (i = 3; i < 32; i++) {\n bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1);\n bn_scatter5(tmp.d, top, powerbuf, i);\n }\n# else\n for (i = 4; i < 32; i *= 2) {\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_scatter5(tmp.d, top, powerbuf, i);\n }\n for (i = 3; i < 8; i += 2) {\n int j;\n bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1);\n bn_scatter5(tmp.d, top, powerbuf, i);\n for (j = 2 * i; j < 32; j *= 2) {\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_scatter5(tmp.d, top, powerbuf, j);\n }\n }\n for (; i < 16; i += 2) {\n bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1);\n bn_scatter5(tmp.d, top, powerbuf, i);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_scatter5(tmp.d, top, powerbuf, 2 * i);\n }\n for (; i < 32; i += 2) {\n bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1);\n bn_scatter5(tmp.d, top, powerbuf, i);\n }\n# endif\n window0 = (bits - 1) % 5 + 1;\n wmask = (1 << window0) - 1;\n bits -= window0;\n wvalue = bn_get_bits(p, bits) & wmask;\n bn_gather5(tmp.d, top, powerbuf, wvalue);\n if (top & 7) {\n while (bits > 0) {\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_gather5(tmp.d, tmp.d, powerbuf, np, n0, top,\n bn_get_bits5(p->d, bits -= 5));\n }\n } else {\n while (bits > 0) {\n bn_power5(tmp.d, tmp.d, powerbuf, np, n0, top,\n bn_get_bits5(p->d, bits -= 5));\n }\n }\n ret = bn_from_montgomery(tmp.d, tmp.d, NULL, np, n0, top);\n tmp.top = top;\n bn_correct_top(&tmp);\n if (ret) {\n if (!BN_copy(rr, &tmp))\n ret = 0;\n goto err;\n }\n } else\n#endif\n {\n if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&tmp, top, powerbuf, 0, window))\n goto err;\n if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&am, top, powerbuf, 1, window))\n goto err;\n if (window > 1) {\n if (!bn_mul_mont_fixed_top(&tmp, &am, &am, mont, ctx))\n goto err;\n if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&tmp, top, powerbuf, 2,\n window))\n goto err;\n for (i = 3; i < numPowers; i++) {\n if (!bn_mul_mont_fixed_top(&tmp, &am, &tmp, mont, ctx))\n goto err;\n if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&tmp, top, powerbuf, i,\n window))\n goto err;\n }\n }\n window0 = (bits - 1) % window + 1;\n wmask = (1 << window0) - 1;\n bits -= window0;\n wvalue = bn_get_bits(p, bits) & wmask;\n if (!MOD_EXP_CTIME_COPY_FROM_PREBUF(&tmp, top, powerbuf, wvalue,\n window))\n goto err;\n wmask = (1 << window) - 1;\n while (bits > 0) {\n for (i = 0; i < window; i++)\n if (!bn_mul_mont_fixed_top(&tmp, &tmp, &tmp, mont, ctx))\n goto err;\n bits -= window;\n wvalue = bn_get_bits(p, bits) & wmask;\n if (!MOD_EXP_CTIME_COPY_FROM_PREBUF(&am, top, powerbuf, wvalue,\n window))\n goto err;\n if (!bn_mul_mont_fixed_top(&tmp, &tmp, &am, mont, ctx))\n goto err;\n }\n }\n#if defined(SPARC_T4_MONT)\n if (OPENSSL_sparcv9cap_P[0] & (SPARCV9_VIS3 | SPARCV9_PREFER_FPU)) {\n am.d[0] = 1;\n for (i = 1; i < top; i++)\n am.d[i] = 0;\n if (!BN_mod_mul_montgomery(rr, &tmp, &am, mont, ctx))\n goto err;\n } else\n#endif\n if (!BN_from_montgomery(rr, &tmp, mont, ctx))\n goto err;\n ret = 1;\n err:\n if (in_mont == NULL)\n BN_MONT_CTX_free(mont);\n if (powerbuf != NULL) {\n OPENSSL_cleanse(powerbuf, powerbufLen);\n OPENSSL_free(powerbufFree);\n }\n BN_CTX_end(ctx);\n return ret;\n}', '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(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 ret = 1;\n err:\n BN_CTX_end(ctx);\n return ret;\n}', 'int BN_sqr(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx)\n{\n int max, al;\n int ret = 0;\n BIGNUM *tmp, *rr;\n bn_check_top(a);\n al = a->top;\n if (al <= 0) {\n r->top = 0;\n r->neg = 0;\n return 1;\n }\n BN_CTX_start(ctx);\n rr = (a != r) ? r : BN_CTX_get(ctx);\n tmp = BN_CTX_get(ctx);\n if (rr == NULL || tmp == NULL)\n goto err;\n max = 2 * al;\n if (bn_wexpand(rr, max) == NULL)\n goto err;\n if (al == 4) {\n#ifndef BN_SQR_COMBA\n BN_ULONG t[8];\n bn_sqr_normal(rr->d, a->d, 4, t);\n#else\n bn_sqr_comba4(rr->d, a->d);\n#endif\n } else if (al == 8) {\n#ifndef BN_SQR_COMBA\n BN_ULONG t[16];\n bn_sqr_normal(rr->d, a->d, 8, t);\n#else\n bn_sqr_comba8(rr->d, a->d);\n#endif\n } else {\n#if defined(BN_RECURSION)\n if (al < BN_SQR_RECURSIVE_SIZE_NORMAL) {\n BN_ULONG t[BN_SQR_RECURSIVE_SIZE_NORMAL * 2];\n bn_sqr_normal(rr->d, a->d, al, t);\n } else {\n int j, k;\n j = BN_num_bits_word((BN_ULONG)al);\n j = 1 << (j - 1);\n k = j + j;\n if (al == j) {\n if (bn_wexpand(tmp, k * 2) == NULL)\n goto err;\n bn_sqr_recursive(rr->d, a->d, al, tmp->d);\n } else {\n if (bn_wexpand(tmp, max) == NULL)\n goto err;\n bn_sqr_normal(rr->d, a->d, al, tmp->d);\n }\n }\n#else\n if (bn_wexpand(tmp, max) == NULL)\n goto err;\n bn_sqr_normal(rr->d, a->d, al, tmp->d);\n#endif\n }\n rr->neg = 0;\n rr->top = max;\n bn_correct_top(rr);\n if (r != rr && BN_copy(r, rr) == NULL)\n goto err;\n ret = 1;\n err:\n bn_check_top(rr);\n bn_check_top(tmp);\n BN_CTX_end(ctx);\n return ret;\n}', 'void bn_sqr_normal(BN_ULONG *r, const BN_ULONG *a, int n, BN_ULONG *tmp)\n{\n int i, j, max;\n const BN_ULONG *ap;\n BN_ULONG *rp;\n max = n * 2;\n ap = a;\n rp = r;\n rp[0] = rp[max - 1] = 0;\n rp++;\n j = n;\n if (--j > 0) {\n ap++;\n rp[j] = bn_mul_words(rp, ap, j, ap[-1]);\n rp += 2;\n }\n for (i = n - 2; i > 0; i--) {\n j--;\n ap++;\n rp[j] = bn_mul_add_words(rp, ap, j, ap[-1]);\n rp += 2;\n }\n bn_add_words(r, r, r, max);\n bn_sqr_words(tmp, a, n);\n bn_add_words(r, r, tmp, max);\n}']
237
0
https://github.com/openssl/openssl/blob/18e1e302452e6dea4500b6f981cee7e151294dea/crypto/bn/bn_ctx.c/#L268
static unsigned int BN_STACK_pop(BN_STACK *st) { return st->indexes[--(st->depth)]; }
['static int rsa_ossl_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)\n{\n BIGNUM *r1, *m1, *vrfy, *r2, *m[RSA_MAX_PRIME_NUM - 2];\n int ret = 0, i, ex_primes = 0, smooth = 0;\n RSA_PRIME_INFO *pinfo;\n BN_CTX_start(ctx);\n r1 = BN_CTX_get(ctx);\n r2 = BN_CTX_get(ctx);\n m1 = BN_CTX_get(ctx);\n vrfy = BN_CTX_get(ctx);\n if (vrfy == NULL)\n goto err;\n if (rsa->version == RSA_ASN1_VERSION_MULTI\n && ((ex_primes = sk_RSA_PRIME_INFO_num(rsa->prime_infos)) <= 0\n || ex_primes > RSA_MAX_PRIME_NUM - 2))\n goto err;\n if (rsa->flags & RSA_FLAG_CACHE_PRIVATE) {\n BIGNUM *factor = BN_new();\n if (factor == NULL)\n goto err;\n if (!(BN_with_flags(factor, rsa->p, BN_FLG_CONSTTIME),\n BN_MONT_CTX_set_locked(&rsa->_method_mod_p, rsa->lock,\n factor, ctx))\n || !(BN_with_flags(factor, rsa->q, BN_FLG_CONSTTIME),\n BN_MONT_CTX_set_locked(&rsa->_method_mod_q, rsa->lock,\n factor, ctx))) {\n BN_free(factor);\n goto err;\n }\n for (i = 0; i < ex_primes; i++) {\n pinfo = sk_RSA_PRIME_INFO_value(rsa->prime_infos, i);\n BN_with_flags(factor, pinfo->r, BN_FLG_CONSTTIME);\n if (!BN_MONT_CTX_set_locked(&pinfo->m, rsa->lock, factor, ctx)) {\n BN_free(factor);\n goto err;\n }\n }\n BN_free(factor);\n smooth = (ex_primes == 0)\n && (rsa->meth->bn_mod_exp == BN_mod_exp_mont)\n && (BN_num_bits(rsa->q) == BN_num_bits(rsa->p));\n }\n if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)\n if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, rsa->lock,\n rsa->n, ctx))\n goto err;\n if (smooth) {\n if (\n !bn_from_mont_fixed_top(m1, I, rsa->_method_mod_q, ctx)\n || !bn_to_mont_fixed_top(m1, m1, rsa->_method_mod_q, ctx)\n || !BN_mod_exp_mont_consttime(m1, m1, rsa->dmq1, rsa->q, ctx,\n rsa->_method_mod_q)\n || !bn_from_mont_fixed_top(r1, I, rsa->_method_mod_p, ctx)\n || !bn_to_mont_fixed_top(r1, r1, rsa->_method_mod_p, ctx)\n || !BN_mod_exp_mont_consttime(r1, r1, rsa->dmp1, rsa->p, ctx,\n rsa->_method_mod_p)\n || !bn_mod_sub_fixed_top(r1, r1, m1, rsa->p)\n || !bn_to_mont_fixed_top(r1, r1, rsa->_method_mod_p, ctx)\n || !bn_mul_mont_fixed_top(r1, r1, rsa->iqmp, rsa->_method_mod_p,\n ctx)\n || !bn_mul_fixed_top(r0, r1, rsa->q, ctx)\n || !bn_mod_add_fixed_top(r0, r0, m1, rsa->n))\n goto err;\n goto tail;\n }\n {\n BIGNUM *c = BN_new();\n if (c == NULL)\n goto err;\n BN_with_flags(c, I, BN_FLG_CONSTTIME);\n if (!BN_mod(r1, c, rsa->q, ctx)) {\n BN_free(c);\n goto err;\n }\n {\n BIGNUM *dmq1 = BN_new();\n if (dmq1 == NULL) {\n BN_free(c);\n goto err;\n }\n BN_with_flags(dmq1, rsa->dmq1, BN_FLG_CONSTTIME);\n if (!rsa->meth->bn_mod_exp(m1, r1, dmq1, rsa->q, ctx,\n rsa->_method_mod_q)) {\n BN_free(c);\n BN_free(dmq1);\n goto err;\n }\n BN_free(dmq1);\n }\n if (!BN_mod(r1, c, rsa->p, ctx)) {\n BN_free(c);\n goto err;\n }\n BN_free(c);\n }\n {\n BIGNUM *dmp1 = BN_new();\n if (dmp1 == NULL)\n goto err;\n BN_with_flags(dmp1, rsa->dmp1, BN_FLG_CONSTTIME);\n if (!rsa->meth->bn_mod_exp(r0, r1, dmp1, rsa->p, ctx,\n rsa->_method_mod_p)) {\n BN_free(dmp1);\n goto err;\n }\n BN_free(dmp1);\n }\n if (ex_primes > 0) {\n BIGNUM *di = BN_new(), *cc = BN_new();\n if (cc == NULL || di == NULL) {\n BN_free(cc);\n BN_free(di);\n goto err;\n }\n for (i = 0; i < ex_primes; i++) {\n if ((m[i] = BN_CTX_get(ctx)) == NULL) {\n BN_free(cc);\n BN_free(di);\n goto err;\n }\n pinfo = sk_RSA_PRIME_INFO_value(rsa->prime_infos, i);\n BN_with_flags(cc, I, BN_FLG_CONSTTIME);\n BN_with_flags(di, pinfo->d, BN_FLG_CONSTTIME);\n if (!BN_mod(r1, cc, pinfo->r, ctx)) {\n BN_free(cc);\n BN_free(di);\n goto err;\n }\n if (!rsa->meth->bn_mod_exp(m[i], r1, di, pinfo->r, ctx, pinfo->m)) {\n BN_free(cc);\n BN_free(di);\n goto err;\n }\n }\n BN_free(cc);\n BN_free(di);\n }\n if (!BN_sub(r0, r0, m1))\n goto err;\n if (BN_is_negative(r0))\n if (!BN_add(r0, r0, rsa->p))\n goto err;\n if (!BN_mul(r1, r0, rsa->iqmp, ctx))\n goto err;\n {\n BIGNUM *pr1 = BN_new();\n if (pr1 == NULL)\n goto err;\n BN_with_flags(pr1, r1, BN_FLG_CONSTTIME);\n if (!BN_mod(r0, pr1, rsa->p, ctx)) {\n BN_free(pr1);\n goto err;\n }\n BN_free(pr1);\n }\n if (BN_is_negative(r0))\n if (!BN_add(r0, r0, rsa->p))\n goto err;\n if (!BN_mul(r1, r0, rsa->q, ctx))\n goto err;\n if (!BN_add(r0, r1, m1))\n goto err;\n if (ex_primes > 0) {\n BIGNUM *pr2 = BN_new();\n if (pr2 == NULL)\n goto err;\n for (i = 0; i < ex_primes; i++) {\n pinfo = sk_RSA_PRIME_INFO_value(rsa->prime_infos, i);\n if (!BN_sub(r1, m[i], r0)) {\n BN_free(pr2);\n goto err;\n }\n if (!BN_mul(r2, r1, pinfo->t, ctx)) {\n BN_free(pr2);\n goto err;\n }\n BN_with_flags(pr2, r2, BN_FLG_CONSTTIME);\n if (!BN_mod(r1, pr2, pinfo->r, ctx)) {\n BN_free(pr2);\n goto err;\n }\n if (BN_is_negative(r1))\n if (!BN_add(r1, r1, pinfo->r)) {\n BN_free(pr2);\n goto err;\n }\n if (!BN_mul(r1, r1, pinfo->pp, ctx)) {\n BN_free(pr2);\n goto err;\n }\n if (!BN_add(r0, r0, r1)) {\n BN_free(pr2);\n goto err;\n }\n }\n BN_free(pr2);\n }\n tail:\n if (rsa->e && rsa->n) {\n if (rsa->meth->bn_mod_exp == BN_mod_exp_mont) {\n if (!BN_mod_exp_mont(vrfy, r0, rsa->e, rsa->n, ctx,\n rsa->_method_mod_n))\n goto err;\n } else {\n bn_correct_top(r0);\n if (!rsa->meth->bn_mod_exp(vrfy, r0, rsa->e, rsa->n, ctx,\n rsa->_method_mod_n))\n goto err;\n }\n if (!BN_sub(vrfy, vrfy, I))\n goto err;\n if (BN_is_zero(vrfy)) {\n bn_correct_top(r0);\n ret = 1;\n goto err;\n }\n if (!BN_mod(vrfy, vrfy, rsa->n, ctx))\n goto err;\n if (BN_is_negative(vrfy))\n if (!BN_add(vrfy, vrfy, rsa->n))\n goto err;\n if (!BN_is_zero(vrfy)) {\n BIGNUM *d = BN_new();\n if (d == NULL)\n goto err;\n BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);\n if (!rsa->meth->bn_mod_exp(r0, I, d, rsa->n, ctx,\n rsa->_method_mod_n)) {\n BN_free(d);\n goto err;\n }\n BN_free(d);\n }\n }\n bn_correct_top(r0);\n ret = 1;\n err:\n BN_CTX_end(ctx);\n return ret;\n}', 'void BN_CTX_start(BN_CTX *ctx)\n{\n CTXDBG("ENTER BN_CTX_start()", ctx);\n if (ctx->err_stack || ctx->too_many)\n ctx->err_stack++;\n else if (!BN_STACK_push(&ctx->stack, ctx->used)) {\n BNerr(BN_F_BN_CTX_START, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n ctx->err_stack++;\n }\n CTXDBG("LEAVE BN_CTX_start()", ctx);\n}', 'BIGNUM *BN_CTX_get(BN_CTX *ctx)\n{\n BIGNUM *ret;\n CTXDBG("ENTER BN_CTX_get()", ctx);\n if (ctx->err_stack || ctx->too_many)\n return NULL;\n if ((ret = BN_POOL_get(&ctx->pool, ctx->flags)) == NULL) {\n ctx->too_many = 1;\n BNerr(BN_F_BN_CTX_GET, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n return NULL;\n }\n BN_zero(ret);\n ret->flags &= (~BN_FLG_CONSTTIME);\n ctx->used++;\n CTXDBG("LEAVE BN_CTX_get()", ctx);\n return ret;\n}', 'BN_MONT_CTX *BN_MONT_CTX_set_locked(BN_MONT_CTX **pmont, CRYPTO_RWLOCK *lock,\n const BIGNUM *mod, BN_CTX *ctx)\n{\n BN_MONT_CTX *ret;\n CRYPTO_THREAD_read_lock(lock);\n ret = *pmont;\n CRYPTO_THREAD_unlock(lock);\n if (ret)\n return ret;\n ret = BN_MONT_CTX_new();\n if (ret == NULL)\n return NULL;\n if (!BN_MONT_CTX_set(ret, mod, ctx)) {\n BN_MONT_CTX_free(ret);\n return NULL;\n }\n CRYPTO_THREAD_write_lock(lock);\n if (*pmont) {\n BN_MONT_CTX_free(ret);\n ret = *pmont;\n } else\n *pmont = ret;\n CRYPTO_THREAD_unlock(lock);\n return ret;\n}', 'int BN_MONT_CTX_set(BN_MONT_CTX *mont, const BIGNUM *mod, BN_CTX *ctx)\n{\n int i, ret = 0;\n BIGNUM *Ri, *R;\n if (BN_is_zero(mod))\n return 0;\n BN_CTX_start(ctx);\n if ((Ri = BN_CTX_get(ctx)) == NULL)\n goto err;\n R = &(mont->RR);\n if (!BN_copy(&(mont->N), mod))\n goto err;\n if (BN_get_flags(mod, BN_FLG_CONSTTIME) != 0)\n BN_set_flags(&(mont->N), BN_FLG_CONSTTIME);\n mont->N.neg = 0;\n#ifdef MONT_WORD\n {\n BIGNUM tmod;\n BN_ULONG buf[2];\n bn_init(&tmod);\n tmod.d = buf;\n tmod.dmax = 2;\n tmod.neg = 0;\n if (BN_get_flags(mod, BN_FLG_CONSTTIME) != 0)\n BN_set_flags(&tmod, BN_FLG_CONSTTIME);\n mont->ri = (BN_num_bits(mod) + (BN_BITS2 - 1)) / BN_BITS2 * BN_BITS2;\n# if defined(OPENSSL_BN_ASM_MONT) && (BN_BITS2<=32)\n BN_zero(R);\n if (!(BN_set_bit(R, 2 * BN_BITS2)))\n goto err;\n tmod.top = 0;\n if ((buf[0] = mod->d[0]))\n tmod.top = 1;\n if ((buf[1] = mod->top > 1 ? mod->d[1] : 0))\n tmod.top = 2;\n if (BN_is_one(&tmod))\n BN_zero(Ri);\n else if ((BN_mod_inverse(Ri, R, &tmod, ctx)) == NULL)\n goto err;\n if (!BN_lshift(Ri, Ri, 2 * BN_BITS2))\n goto err;\n if (!BN_is_zero(Ri)) {\n if (!BN_sub_word(Ri, 1))\n goto err;\n } else {\n if (bn_expand(Ri, (int)sizeof(BN_ULONG) * 2) == NULL)\n goto err;\n Ri->neg = 0;\n Ri->d[0] = BN_MASK2;\n Ri->d[1] = BN_MASK2;\n Ri->top = 2;\n }\n if (!BN_div(Ri, NULL, Ri, &tmod, ctx))\n goto err;\n mont->n0[0] = (Ri->top > 0) ? Ri->d[0] : 0;\n mont->n0[1] = (Ri->top > 1) ? Ri->d[1] : 0;\n# else\n BN_zero(R);\n if (!(BN_set_bit(R, BN_BITS2)))\n goto err;\n buf[0] = mod->d[0];\n buf[1] = 0;\n tmod.top = buf[0] != 0 ? 1 : 0;\n if (BN_is_one(&tmod))\n BN_zero(Ri);\n else if ((BN_mod_inverse(Ri, R, &tmod, ctx)) == NULL)\n goto err;\n if (!BN_lshift(Ri, Ri, BN_BITS2))\n goto err;\n if (!BN_is_zero(Ri)) {\n if (!BN_sub_word(Ri, 1))\n goto err;\n } else {\n if (!BN_set_word(Ri, BN_MASK2))\n goto err;\n }\n if (!BN_div(Ri, NULL, Ri, &tmod, ctx))\n goto err;\n mont->n0[0] = (Ri->top > 0) ? Ri->d[0] : 0;\n mont->n0[1] = 0;\n# endif\n }\n#else\n {\n mont->ri = BN_num_bits(&mont->N);\n BN_zero(R);\n if (!BN_set_bit(R, mont->ri))\n goto err;\n if ((BN_mod_inverse(Ri, R, &mont->N, ctx)) == NULL)\n goto err;\n if (!BN_lshift(Ri, Ri, mont->ri))\n goto err;\n if (!BN_sub_word(Ri, 1))\n goto err;\n if (!BN_div(&(mont->Ni), NULL, Ri, &mont->N, ctx))\n goto err;\n }\n#endif\n BN_zero(&(mont->RR));\n if (!BN_set_bit(&(mont->RR), mont->ri * 2))\n goto err;\n if (!BN_mod(&(mont->RR), &(mont->RR), &(mont->N), ctx))\n goto err;\n for (i = mont->RR.top, ret = mont->N.top; i < ret; i++)\n mont->RR.d[i] = 0;\n mont->RR.top = ret;\n mont->RR.flags |= BN_FLG_FIXED_TOP;\n ret = 1;\n err:\n BN_CTX_end(ctx);\n return ret;\n}', 'BIGNUM *BN_mod_inverse(BIGNUM *in,\n const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx)\n{\n BIGNUM *rv;\n int noinv;\n rv = int_bn_mod_inverse(in, a, n, ctx, &noinv);\n if (noinv)\n BNerr(BN_F_BN_MOD_INVERSE, BN_R_NO_INVERSE);\n return rv;\n}', 'BIGNUM *int_bn_mod_inverse(BIGNUM *in,\n const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx,\n int *pnoinv)\n{\n BIGNUM *A, *B, *X, *Y, *M, *D, *T, *R = NULL;\n BIGNUM *ret = NULL;\n int sign;\n if (BN_abs_is_word(n, 1) || BN_is_zero(n)) {\n if (pnoinv != NULL)\n *pnoinv = 1;\n return NULL;\n }\n if (pnoinv != NULL)\n *pnoinv = 0;\n if ((BN_get_flags(a, BN_FLG_CONSTTIME) != 0)\n || (BN_get_flags(n, BN_FLG_CONSTTIME) != 0)) {\n return BN_mod_inverse_no_branch(in, a, n, ctx);\n }\n bn_check_top(a);\n bn_check_top(n);\n BN_CTX_start(ctx);\n A = BN_CTX_get(ctx);\n B = BN_CTX_get(ctx);\n X = BN_CTX_get(ctx);\n D = BN_CTX_get(ctx);\n M = BN_CTX_get(ctx);\n Y = BN_CTX_get(ctx);\n T = BN_CTX_get(ctx);\n if (T == NULL)\n goto err;\n if (in == NULL)\n R = BN_new();\n else\n R = in;\n if (R == NULL)\n goto err;\n BN_one(X);\n BN_zero(Y);\n if (BN_copy(B, a) == NULL)\n goto err;\n if (BN_copy(A, n) == NULL)\n goto err;\n A->neg = 0;\n if (B->neg || (BN_ucmp(B, A) >= 0)) {\n if (!BN_nnmod(B, B, A, ctx))\n goto err;\n }\n sign = -1;\n if (BN_is_odd(n) && (BN_num_bits(n) <= 2048)) {\n int shift;\n while (!BN_is_zero(B)) {\n shift = 0;\n while (!BN_is_bit_set(B, shift)) {\n shift++;\n if (BN_is_odd(X)) {\n if (!BN_uadd(X, X, n))\n goto err;\n }\n if (!BN_rshift1(X, X))\n goto err;\n }\n if (shift > 0) {\n if (!BN_rshift(B, B, shift))\n goto err;\n }\n shift = 0;\n while (!BN_is_bit_set(A, shift)) {\n shift++;\n if (BN_is_odd(Y)) {\n if (!BN_uadd(Y, Y, n))\n goto err;\n }\n if (!BN_rshift1(Y, Y))\n goto err;\n }\n if (shift > 0) {\n if (!BN_rshift(A, A, shift))\n goto err;\n }\n if (BN_ucmp(B, A) >= 0) {\n if (!BN_uadd(X, X, Y))\n goto err;\n if (!BN_usub(B, B, A))\n goto err;\n } else {\n if (!BN_uadd(Y, Y, X))\n goto err;\n if (!BN_usub(A, A, B))\n goto err;\n }\n }\n } else {\n while (!BN_is_zero(B)) {\n BIGNUM *tmp;\n if (BN_num_bits(A) == BN_num_bits(B)) {\n if (!BN_one(D))\n goto err;\n if (!BN_sub(M, A, B))\n goto err;\n } else if (BN_num_bits(A) == BN_num_bits(B) + 1) {\n if (!BN_lshift1(T, B))\n goto err;\n if (BN_ucmp(A, T) < 0) {\n if (!BN_one(D))\n goto err;\n if (!BN_sub(M, A, B))\n goto err;\n } else {\n if (!BN_sub(M, A, T))\n goto err;\n if (!BN_add(D, T, B))\n goto err;\n if (BN_ucmp(A, D) < 0) {\n if (!BN_set_word(D, 2))\n goto err;\n } else {\n if (!BN_set_word(D, 3))\n goto err;\n if (!BN_sub(M, M, B))\n goto err;\n }\n }\n } else {\n if (!BN_div(D, M, A, B, ctx))\n goto err;\n }\n tmp = A;\n A = B;\n B = M;\n if (BN_is_one(D)) {\n if (!BN_add(tmp, X, Y))\n goto err;\n } else {\n if (BN_is_word(D, 2)) {\n if (!BN_lshift1(tmp, X))\n goto err;\n } else if (BN_is_word(D, 4)) {\n if (!BN_lshift(tmp, X, 2))\n goto err;\n } else if (D->top == 1) {\n if (!BN_copy(tmp, X))\n goto err;\n if (!BN_mul_word(tmp, D->d[0]))\n goto err;\n } else {\n if (!BN_mul(tmp, D, X, ctx))\n goto err;\n }\n if (!BN_add(tmp, tmp, Y))\n goto err;\n }\n M = Y;\n Y = X;\n X = tmp;\n sign = -sign;\n }\n }\n if (sign < 0) {\n if (!BN_sub(Y, n, Y))\n goto err;\n }\n if (BN_is_one(A)) {\n if (!Y->neg && BN_ucmp(Y, n) < 0) {\n if (!BN_copy(R, Y))\n goto err;\n } else {\n if (!BN_nnmod(R, Y, n, ctx))\n goto err;\n }\n } else {\n if (pnoinv)\n *pnoinv = 1;\n goto err;\n }\n ret = R;\n err:\n if ((ret == NULL) && (in == NULL))\n BN_free(R);\n BN_CTX_end(ctx);\n bn_check_top(ret);\n return ret;\n}', 'static BIGNUM *BN_mod_inverse_no_branch(BIGNUM *in,\n const BIGNUM *a, const BIGNUM *n,\n BN_CTX *ctx)\n{\n BIGNUM *A, *B, *X, *Y, *M, *D, *T, *R = NULL;\n BIGNUM *ret = NULL;\n int sign;\n bn_check_top(a);\n bn_check_top(n);\n BN_CTX_start(ctx);\n A = BN_CTX_get(ctx);\n B = BN_CTX_get(ctx);\n X = BN_CTX_get(ctx);\n D = BN_CTX_get(ctx);\n M = BN_CTX_get(ctx);\n Y = BN_CTX_get(ctx);\n T = BN_CTX_get(ctx);\n if (T == NULL)\n goto err;\n if (in == NULL)\n R = BN_new();\n else\n R = in;\n if (R == NULL)\n goto err;\n BN_one(X);\n BN_zero(Y);\n if (BN_copy(B, a) == NULL)\n goto err;\n if (BN_copy(A, n) == NULL)\n goto err;\n A->neg = 0;\n if (B->neg || (BN_ucmp(B, A) >= 0)) {\n {\n BIGNUM local_B;\n bn_init(&local_B);\n BN_with_flags(&local_B, B, BN_FLG_CONSTTIME);\n if (!BN_nnmod(B, &local_B, A, ctx))\n goto err;\n }\n }\n sign = -1;\n while (!BN_is_zero(B)) {\n BIGNUM *tmp;\n {\n BIGNUM local_A;\n bn_init(&local_A);\n BN_with_flags(&local_A, A, BN_FLG_CONSTTIME);\n if (!BN_div(D, M, &local_A, B, ctx))\n goto err;\n }\n tmp = A;\n A = B;\n B = M;\n if (!BN_mul(tmp, D, X, ctx))\n goto err;\n if (!BN_add(tmp, tmp, Y))\n goto err;\n M = Y;\n Y = X;\n X = tmp;\n sign = -sign;\n }\n if (sign < 0) {\n if (!BN_sub(Y, n, Y))\n goto err;\n }\n if (BN_is_one(A)) {\n if (!Y->neg && BN_ucmp(Y, n) < 0) {\n if (!BN_copy(R, Y))\n goto err;\n } else {\n if (!BN_nnmod(R, Y, n, ctx))\n goto err;\n }\n } else {\n BNerr(BN_F_BN_MOD_INVERSE_NO_BRANCH, BN_R_NO_INVERSE);\n goto err;\n }\n ret = R;\n err:\n if ((ret == NULL) && (in == NULL))\n BN_free(R);\n BN_CTX_end(ctx);\n bn_check_top(ret);\n return ret;\n}', 'int BN_nnmod(BIGNUM *r, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx)\n{\n if (!(BN_mod(r, m, d, ctx)))\n return 0;\n if (!r->neg)\n return 1;\n return (d->neg ? BN_sub : BN_add) (r, r, d);\n}', 'int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,\n BN_CTX *ctx)\n{\n int ret;\n if (BN_is_zero(divisor)) {\n BNerr(BN_F_BN_DIV, BN_R_DIV_BY_ZERO);\n return 0;\n }\n if (divisor->d[divisor->top - 1] == 0) {\n BNerr(BN_F_BN_DIV, BN_R_NOT_INITIALIZED);\n return 0;\n }\n ret = bn_div_fixed_top(dv, rm, num, divisor, ctx);\n if (ret) {\n if (dv != NULL)\n bn_correct_top(dv);\n if (rm != NULL)\n bn_correct_top(rm);\n }\n return ret;\n}', 'int bn_div_fixed_top(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num,\n const BIGNUM *divisor, BN_CTX *ctx)\n{\n int norm_shift, i, j, loop;\n BIGNUM *tmp, *snum, *sdiv, *res;\n BN_ULONG *resp, *wnum, *wnumtop;\n BN_ULONG d0, d1;\n int num_n, div_n;\n assert(divisor->top > 0 && divisor->d[divisor->top - 1] != 0);\n bn_check_top(num);\n bn_check_top(divisor);\n bn_check_top(dv);\n bn_check_top(rm);\n BN_CTX_start(ctx);\n res = (dv == NULL) ? BN_CTX_get(ctx) : dv;\n tmp = BN_CTX_get(ctx);\n snum = BN_CTX_get(ctx);\n sdiv = BN_CTX_get(ctx);\n if (sdiv == NULL)\n goto err;\n if (!BN_copy(sdiv, divisor))\n goto err;\n norm_shift = bn_left_align(sdiv);\n sdiv->neg = 0;\n if (!(bn_lshift_fixed_top(snum, num, norm_shift)))\n goto err;\n div_n = sdiv->top;\n num_n = snum->top;\n if (num_n <= div_n) {\n if (bn_wexpand(snum, div_n + 1) == NULL)\n goto err;\n memset(&(snum->d[num_n]), 0, (div_n - num_n + 1) * sizeof(BN_ULONG));\n snum->top = num_n = div_n + 1;\n }\n loop = num_n - div_n;\n wnum = &(snum->d[loop]);\n wnumtop = &(snum->d[num_n - 1]);\n d0 = sdiv->d[div_n - 1];\n d1 = (div_n == 1) ? 0 : sdiv->d[div_n - 2];\n if (!bn_wexpand(res, loop))\n goto err;\n res->neg = (num->neg ^ divisor->neg);\n res->top = loop;\n res->flags |= BN_FLG_FIXED_TOP;\n resp = &(res->d[loop]);\n if (!bn_wexpand(tmp, (div_n + 1)))\n goto err;\n for (i = 0; i < loop; i++, wnumtop--) {\n BN_ULONG q, l0;\n# if defined(BN_DIV3W)\n q = bn_div_3_words(wnumtop, d1, d0);\n# else\n BN_ULONG n0, n1, rem = 0;\n n0 = wnumtop[0];\n n1 = wnumtop[-1];\n if (n0 == d0)\n q = BN_MASK2;\n else {\n BN_ULONG n2 = (wnumtop == wnum) ? 0 : wnumtop[-2];\n# ifdef BN_LLONG\n BN_ULLONG t2;\n# if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)\n q = (BN_ULONG)(((((BN_ULLONG) n0) << BN_BITS2) | n1) / d0);\n# else\n q = bn_div_words(n0, n1, d0);\n# endif\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n t2 = (BN_ULLONG) d1 *q;\n for (;;) {\n if (t2 <= ((((BN_ULLONG) rem) << BN_BITS2) | n2))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n t2 -= d1;\n }\n# else\n BN_ULONG t2l, t2h;\n q = bn_div_words(n0, n1, d0);\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n# if defined(BN_UMULT_LOHI)\n BN_UMULT_LOHI(t2l, t2h, d1, q);\n# elif defined(BN_UMULT_HIGH)\n t2l = d1 * q;\n t2h = BN_UMULT_HIGH(d1, q);\n# else\n {\n BN_ULONG ql, qh;\n t2l = LBITS(d1);\n t2h = HBITS(d1);\n ql = LBITS(q);\n qh = HBITS(q);\n mul64(t2l, t2h, ql, qh);\n }\n# endif\n for (;;) {\n if ((t2h < rem) || ((t2h == rem) && (t2l <= n2)))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n if (t2l < d1)\n t2h--;\n t2l -= d1;\n }\n# endif\n }\n# endif\n l0 = bn_mul_words(tmp->d, sdiv->d, div_n, q);\n tmp->d[div_n] = l0;\n wnum--;\n l0 = bn_sub_words(wnum, wnum, tmp->d, div_n + 1);\n q -= l0;\n for (l0 = 0 - l0, j = 0; j < div_n; j++)\n tmp->d[j] = sdiv->d[j] & l0;\n l0 = bn_add_words(wnum, wnum, tmp->d, div_n);\n (*wnumtop) += l0;\n assert((*wnumtop) == 0);\n *--resp = q;\n }\n snum->neg = num->neg;\n snum->top = div_n;\n snum->flags |= BN_FLG_FIXED_TOP;\n if (rm != NULL)\n bn_rshift_fixed_top(rm, snum, norm_shift);\n BN_CTX_end(ctx);\n return 1;\n err:\n bn_check_top(rm);\n BN_CTX_end(ctx);\n return 0;\n}', 'void BN_CTX_end(BN_CTX *ctx)\n{\n CTXDBG("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}']
238
0
https://github.com/openssl/openssl/blob/5dc312215fa54b70cc8b4baec933bcc19525bc03/crypto/ct/ct_oct.c/#L460
int i2o_SCT_LIST(STACK_OF(SCT) *a, unsigned char **pp) { int len, sct_len, i, is_pp_new = 0; size_t len2; unsigned char *p = NULL, *p2; if (pp != NULL) { if (*pp == NULL) { if ((len = i2o_SCT_LIST(a, NULL)) == -1) { CTerr(CT_F_I2O_SCT_LIST, CT_R_SCT_LIST_INVALID); return -1; } if ((*pp = OPENSSL_malloc(len)) == NULL) { CTerr(CT_F_I2O_SCT_LIST, ERR_R_MALLOC_FAILURE); return -1; } is_pp_new = 1; } p = *pp + 2; } len2 = 2; for (i = 0; i < sk_SCT_num(a); i++) { if (pp != NULL) { p2 = p; p += 2; if ((sct_len = i2o_SCT(sk_SCT_value(a, i), &p)) == -1) goto err; s2n(sct_len, p2); } else { if ((sct_len = i2o_SCT(sk_SCT_value(a, i), NULL)) == -1) goto err; } len2 += 2 + sct_len; } if (len2 > MAX_SCT_LIST_SIZE) goto err; if (pp != NULL) { p = *pp; s2n(len2 - 2, p); } if (!is_pp_new) *pp += len2; return len2; err: if (is_pp_new) { OPENSSL_free(*pp); *pp = NULL; } return -1; }
['int i2o_SCT_LIST(STACK_OF(SCT) *a, unsigned char **pp)\n{\n int len, sct_len, i, is_pp_new = 0;\n size_t len2;\n unsigned char *p = NULL, *p2;\n if (pp != NULL) {\n if (*pp == NULL) {\n if ((len = i2o_SCT_LIST(a, NULL)) == -1) {\n CTerr(CT_F_I2O_SCT_LIST, CT_R_SCT_LIST_INVALID);\n return -1;\n }\n if ((*pp = OPENSSL_malloc(len)) == NULL) {\n CTerr(CT_F_I2O_SCT_LIST, ERR_R_MALLOC_FAILURE);\n return -1;\n }\n is_pp_new = 1;\n }\n p = *pp + 2;\n }\n len2 = 2;\n for (i = 0; i < sk_SCT_num(a); i++) {\n if (pp != NULL) {\n p2 = p;\n p += 2;\n if ((sct_len = i2o_SCT(sk_SCT_value(a, i), &p)) == -1)\n goto err;\n s2n(sct_len, p2);\n } else {\n if ((sct_len = i2o_SCT(sk_SCT_value(a, i), NULL)) == -1)\n goto err;\n }\n len2 += 2 + sct_len;\n }\n if (len2 > MAX_SCT_LIST_SIZE)\n goto err;\n if (pp != NULL) {\n p = *pp;\n s2n(len2 - 2, p);\n }\n if (!is_pp_new)\n *pp += len2;\n return len2;\n err:\n if (is_pp_new) {\n OPENSSL_free(*pp);\n *pp = NULL;\n }\n return -1;\n}']
239
0
https://github.com/libav/libav/blob/50f70541d36b3ff477b63b3ec754e28ace824d8e/libavutil/aes.c/#L77
static inline void mix(uint8_t state[2][4][4], uint32_t multbl[][256], int s1, int s3){ ((uint32_t *)(state))[0] = mix_core(multbl, state[1][0][0], state[1][s1 ][1], state[1][2][2], state[1][s3 ][3]); ((uint32_t *)(state))[1] = mix_core(multbl, state[1][1][0], state[1][s3-1][1], state[1][3][2], state[1][s1-1][3]); ((uint32_t *)(state))[2] = mix_core(multbl, state[1][2][0], state[1][s3 ][1], state[1][0][2], state[1][s1 ][3]); ((uint32_t *)(state))[3] = mix_core(multbl, state[1][3][0], state[1][s1-1][1], state[1][1][2], state[1][s3-1][3]); }
['int av_aes_init(AVAES *a, const uint8_t *key, int key_bits, int decrypt) {\n int i, j, t, rconpointer = 0;\n uint8_t tk[8][4];\n int KC= key_bits>>5;\n int rounds= KC + 6;\n uint8_t log8[256];\n uint8_t alog8[512];\n if(!enc_multbl[0][sizeof(enc_multbl)/sizeof(enc_multbl[0][0])-1]){\n j=1;\n for(i=0; i<255; i++){\n alog8[i]=\n alog8[i+255]= j;\n log8[j]= i;\n j^= j+j;\n if(j>255) j^= 0x11B;\n }\n for(i=0; i<256; i++){\n j= i ? alog8[255-log8[i]] : 0;\n j ^= (j<<1) ^ (j<<2) ^ (j<<3) ^ (j<<4);\n j = (j ^ (j>>8) ^ 99) & 255;\n inv_sbox[j]= i;\n sbox [i]= j;\n }\n init_multbl2(dec_multbl[0], (const int[4]){0xe, 0x9, 0xd, 0xb}, log8, alog8, inv_sbox);\n init_multbl2(enc_multbl[0], (const int[4]){0x2, 0x1, 0x1, 0x3}, log8, alog8, sbox);\n }\n if(key_bits!=128 && key_bits!=192 && key_bits!=256)\n return -1;\n a->rounds= rounds;\n memcpy(tk, key, KC*4);\n for(t= 0; t < (rounds+1)*16;) {\n memcpy(a->round_key[0][0]+t, tk, KC*4);\n t+= KC*4;\n for(i = 0; i < 4; i++)\n tk[0][i] ^= sbox[tk[KC-1][(i+1)&3]];\n tk[0][0] ^= rcon[rconpointer++];\n for(j = 1; j < KC; j++){\n if(KC != 8 || j != KC>>1)\n for(i = 0; i < 4; i++) tk[j][i] ^= tk[j-1][i];\n else\n for(i = 0; i < 4; i++) tk[j][i] ^= sbox[tk[j-1][i]];\n }\n }\n if(decrypt){\n for(i=1; i<rounds; i++){\n uint8_t tmp[3][16];\n memcpy(tmp[2], a->round_key[i][0], 16);\n subshift(&tmp[1], 0, sbox);\n mix(tmp, dec_multbl, 1, 3);\n memcpy(a->round_key[i][0], tmp[0], 16);\n }\n }else{\n for(i=0; i<(rounds+1)>>1; i++){\n for(j=0; j<16; j++)\n FFSWAP(int, a->round_key[i][0][j], a->round_key[rounds-i][0][j]);\n }\n }\n return 0;\n}', 'static inline void mix(uint8_t state[2][4][4], uint32_t multbl[][256], int s1, int s3){\n ((uint32_t *)(state))[0] = mix_core(multbl, state[1][0][0], state[1][s1 ][1], state[1][2][2], state[1][s3 ][3]);\n ((uint32_t *)(state))[1] = mix_core(multbl, state[1][1][0], state[1][s3-1][1], state[1][3][2], state[1][s1-1][3]);\n ((uint32_t *)(state))[2] = mix_core(multbl, state[1][2][0], state[1][s3 ][1], state[1][0][2], state[1][s1 ][3]);\n ((uint32_t *)(state))[3] = mix_core(multbl, state[1][3][0], state[1][s1-1][1], state[1][1][2], state[1][s3-1][3]);\n}']
240
0
https://github.com/libav/libav/blob/f02cbc4590b31de98b97d376686be148d078143d/libavcodec/ac3enc.c/#L1824
static av_cold void set_bandwidth(AC3EncodeContext *s) { int ch, bw_code; if (s->cutoff) { int fbw_coeffs; fbw_coeffs = s->cutoff * 2 * AC3_MAX_COEFS / s->sample_rate; bw_code = av_clip((fbw_coeffs - 73) / 3, 0, 60); } else { bw_code = 50; } for (ch = 0; ch < s->fbw_channels; ch++) { s->bandwidth_code[ch] = bw_code; s->nb_coefs[ch] = bw_code * 3 + 73; } if (s->lfe_on) s->nb_coefs[s->lfe_channel] = 7; }
['static av_cold int ac3_encode_init(AVCodecContext *avctx)\n{\n AC3EncodeContext *s = avctx->priv_data;\n int ret, frame_size_58;\n avctx->frame_size = AC3_FRAME_SIZE;\n ac3_common_init();\n ret = validate_options(avctx, s);\n if (ret)\n return ret;\n s->bitstream_id = 8 + s->bit_alloc.sr_shift;\n s->bitstream_mode = 0;\n s->frame_size_min = 2 * ff_ac3_frame_size_tab[s->frame_size_code][s->bit_alloc.sr_code];\n s->bits_written = 0;\n s->samples_written = 0;\n s->frame_size = s->frame_size_min;\n frame_size_58 = (( s->frame_size >> 2) + ( s->frame_size >> 4)) << 1;\n s->crc_inv[0] = pow_poly((CRC16_POLY >> 1), (8 * frame_size_58) - 16, CRC16_POLY);\n if (s->bit_alloc.sr_code == 1) {\n frame_size_58 = (((s->frame_size+2) >> 2) + ((s->frame_size+2) >> 4)) << 1;\n s->crc_inv[1] = pow_poly((CRC16_POLY >> 1), (8 * frame_size_58) - 16, CRC16_POLY);\n }\n set_bandwidth(s);\n exponent_init(s);\n bit_alloc_init(s);\n s->mdct.avctx = avctx;\n ret = mdct_init(&s->mdct, 9);\n if (ret)\n goto init_fail;\n ret = allocate_buffers(avctx);\n if (ret)\n goto init_fail;\n avctx->coded_frame= avcodec_alloc_frame();\n dsputil_init(&s->dsp, avctx);\n return 0;\ninit_fail:\n ac3_encode_close(avctx);\n return ret;\n}', 'static av_cold int validate_options(AVCodecContext *avctx, AC3EncodeContext *s)\n{\n int i, ret;\n if (!avctx->channel_layout) {\n av_log(avctx, AV_LOG_WARNING, "No channel layout specified. The "\n "encoder will guess the layout, but it "\n "might be incorrect.\\n");\n }\n ret = set_channel_info(s, avctx->channels, &avctx->channel_layout);\n if (ret) {\n av_log(avctx, AV_LOG_ERROR, "invalid channel layout\\n");\n return ret;\n }\n for (i = 0; i < 9; i++) {\n if ((ff_ac3_sample_rate_tab[i / 3] >> (i % 3)) == avctx->sample_rate)\n break;\n }\n if (i == 9) {\n av_log(avctx, AV_LOG_ERROR, "invalid sample rate\\n");\n return AVERROR(EINVAL);\n }\n s->sample_rate = avctx->sample_rate;\n s->bit_alloc.sr_shift = i % 3;\n s->bit_alloc.sr_code = i / 3;\n for (i = 0; i < 19; i++) {\n if ((ff_ac3_bitrate_tab[i] >> s->bit_alloc.sr_shift)*1000 == avctx->bit_rate)\n break;\n }\n if (i == 19) {\n av_log(avctx, AV_LOG_ERROR, "invalid bit rate\\n");\n return AVERROR(EINVAL);\n }\n s->bit_rate = avctx->bit_rate;\n s->frame_size_code = i << 1;\n if (avctx->cutoff < 0) {\n av_log(avctx, AV_LOG_ERROR, "invalid cutoff frequency\\n");\n return AVERROR(EINVAL);\n }\n s->cutoff = avctx->cutoff;\n if (s->cutoff > (s->sample_rate >> 1))\n s->cutoff = s->sample_rate >> 1;\n return 0;\n}', 'static av_cold int set_channel_info(AC3EncodeContext *s, int channels,\n int64_t *channel_layout)\n{\n int ch_layout;\n if (channels < 1 || channels > AC3_MAX_CHANNELS)\n return AVERROR(EINVAL);\n if ((uint64_t)*channel_layout > 0x7FF)\n return AVERROR(EINVAL);\n ch_layout = *channel_layout;\n if (!ch_layout)\n ch_layout = avcodec_guess_channel_layout(channels, CODEC_ID_AC3, NULL);\n if (av_get_channel_layout_nb_channels(ch_layout) != channels)\n return AVERROR(EINVAL);\n s->lfe_on = !!(ch_layout & AV_CH_LOW_FREQUENCY);\n s->channels = channels;\n s->fbw_channels = channels - s->lfe_on;\n s->lfe_channel = s->lfe_on ? s->fbw_channels : -1;\n if (s->lfe_on)\n ch_layout -= AV_CH_LOW_FREQUENCY;\n switch (ch_layout) {\n case AV_CH_LAYOUT_MONO: s->channel_mode = AC3_CHMODE_MONO; break;\n case AV_CH_LAYOUT_STEREO: s->channel_mode = AC3_CHMODE_STEREO; break;\n case AV_CH_LAYOUT_SURROUND: s->channel_mode = AC3_CHMODE_3F; break;\n case AV_CH_LAYOUT_2_1: s->channel_mode = AC3_CHMODE_2F1R; break;\n case AV_CH_LAYOUT_4POINT0: s->channel_mode = AC3_CHMODE_3F1R; break;\n case AV_CH_LAYOUT_QUAD:\n case AV_CH_LAYOUT_2_2: s->channel_mode = AC3_CHMODE_2F2R; break;\n case AV_CH_LAYOUT_5POINT0:\n case AV_CH_LAYOUT_5POINT0_BACK: s->channel_mode = AC3_CHMODE_3F2R; break;\n default:\n return AVERROR(EINVAL);\n }\n s->channel_map = ff_ac3_enc_channel_map[s->channel_mode][s->lfe_on];\n *channel_layout = ch_layout;\n if (s->lfe_on)\n *channel_layout |= AV_CH_LOW_FREQUENCY;\n return 0;\n}', 'static av_cold void set_bandwidth(AC3EncodeContext *s)\n{\n int ch, bw_code;\n if (s->cutoff) {\n int fbw_coeffs;\n fbw_coeffs = s->cutoff * 2 * AC3_MAX_COEFS / s->sample_rate;\n bw_code = av_clip((fbw_coeffs - 73) / 3, 0, 60);\n } else {\n bw_code = 50;\n }\n for (ch = 0; ch < s->fbw_channels; ch++) {\n s->bandwidth_code[ch] = bw_code;\n s->nb_coefs[ch] = bw_code * 3 + 73;\n }\n if (s->lfe_on)\n s->nb_coefs[s->lfe_channel] = 7;\n}']
241
0
https://github.com/libav/libav/blob/67ce33162aa93bee1a5f9e8d6f00060329fa67da/libavutil/string.c/#L74
size_t av_strlcatf(char *dst, size_t size, const char *fmt, ...) { int len = strlen(dst); va_list vl; va_start(vl, fmt); len += vsnprintf(dst + len, size > len ? size - len : 0, fmt, vl); va_end(vl); return len; }
['static int rtsp_read_packet(AVFormatContext *s,\n AVPacket *pkt)\n{\n RTSPState *rt = s->priv_data;\n RTSPStream *rtsp_st;\n int ret, len;\n uint8_t buf[RTP_MAX_PACKET_LENGTH];\n if (rt->server_type == RTSP_SERVER_RDT && rt->need_subscription) {\n int i;\n RTSPHeader reply1, *reply = &reply1;\n char cmd[1024];\n snprintf(cmd, sizeof(cmd),\n "SET_PARAMETER %s RTSP/1.0\\r\\n"\n "Subscribe: ",\n s->filename);\n for (i = 0; i < rt->nb_rtsp_streams; i++) {\n if (i != 0) av_strlcat(cmd, ",", sizeof(cmd));\n ff_rdt_subscribe_rule(\n rt->rtsp_streams[i]->rtp_ctx,\n cmd, sizeof(cmd), i, 0);\n }\n av_strlcat(cmd, "\\r\\n", sizeof(cmd));\n rtsp_send_cmd(s, cmd, reply, NULL);\n if (reply->status_code != RTSP_STATUS_OK)\n return AVERROR_INVALIDDATA;\n rt->need_subscription = 0;\n if (rt->state == RTSP_STATE_PLAYING)\n rtsp_read_play (s);\n }\n if (rt->cur_rtp) {\n if (rt->server_type == RTSP_SERVER_RDT)\n ret = ff_rdt_parse_packet(rt->cur_rtp, pkt, NULL, 0);\n else\n ret = rtp_parse_packet(rt->cur_rtp, pkt, NULL, 0);\n if (ret == 0) {\n rt->cur_rtp = NULL;\n return 0;\n } else if (ret == 1) {\n return 0;\n } else {\n rt->cur_rtp = NULL;\n }\n }\n redo:\n switch(rt->protocol) {\n default:\n case RTSP_PROTOCOL_RTP_TCP:\n len = tcp_read_packet(s, &rtsp_st, buf, sizeof(buf));\n break;\n case RTSP_PROTOCOL_RTP_UDP:\n case RTSP_PROTOCOL_RTP_UDP_MULTICAST:\n len = udp_read_packet(s, &rtsp_st, buf, sizeof(buf));\n if (len >=0 && rtsp_st->rtp_ctx)\n rtp_check_and_send_back_rr(rtsp_st->rtp_ctx, len);\n break;\n }\n if (len < 0)\n return len;\n if (rt->server_type == RTSP_SERVER_RDT)\n ret = ff_rdt_parse_packet(rtsp_st->rtp_ctx, pkt, buf, len);\n else\n ret = rtp_parse_packet(rtsp_st->rtp_ctx, pkt, buf, len);\n if (ret < 0)\n goto redo;\n if (ret == 1) {\n rt->cur_rtp = rtsp_st->rtp_ctx;\n }\n return 0;\n}', 'void\nff_rdt_subscribe_rule (RTPDemuxContext *s, char *cmd, int size,\n int stream_nr, int rule_nr)\n{\n rdt_data *rdt = s->dynamic_protocol_context;\n av_strlcatf(cmd, size, "stream=%d;rule=%d,stream=%d;rule=%d",\n stream_nr, rule_nr, stream_nr, rule_nr + 1);\n rdt_load_mdpr(rdt, s->st, 0);\n}', 'size_t av_strlcatf(char *dst, size_t size, const char *fmt, ...)\n{\n int len = strlen(dst);\n va_list vl;\n va_start(vl, fmt);\n len += vsnprintf(dst + len, size > len ? size - len : 0, fmt, vl);\n va_end(vl);\n return len;\n}']
242
0
https://github.com/libav/libav/blob/63e8d9760f23a4edf81e9ae58c4f6d3baa6ff4dd/libavcodec/xan.c/#L111
static int xan_huffman_decode(unsigned char *dest, const unsigned char *src, int dest_len) { unsigned char byte = *src++; unsigned char ival = byte + 0x16; const unsigned char * ptr = src + byte*2; unsigned char val = ival; unsigned char *dest_end = dest + dest_len; GetBitContext gb; init_get_bits(&gb, ptr, 0); while ( val != 0x16 ) { val = src[val - 0x17 + get_bits1(&gb) * byte]; if ( val < 0x16 ) { if (dest >= dest_end) return 0; *dest++ = val; val = ival; } } return 0; }
['static int xan_huffman_decode(unsigned char *dest, const unsigned char *src,\n int dest_len)\n{\n unsigned char byte = *src++;\n unsigned char ival = byte + 0x16;\n const unsigned char * ptr = src + byte*2;\n unsigned char val = ival;\n unsigned char *dest_end = dest + dest_len;\n GetBitContext gb;\n init_get_bits(&gb, ptr, 0);\n while ( val != 0x16 ) {\n val = src[val - 0x17 + get_bits1(&gb) * byte];\n if ( val < 0x16 ) {\n if (dest >= dest_end)\n return 0;\n *dest++ = val;\n val = ival;\n }\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}']
243
0
https://github.com/openssl/openssl/blob/84c15db551ce1d167b901a3bde2b21880b084384/crypto/bn/bn_asm.c/#L552
void bn_mul_comba8(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b) { #ifdef BN_LLONG BN_ULLONG t; #else BN_ULONG bl,bh; #endif BN_ULONG t1,t2; BN_ULONG c1,c2,c3; c1=0; c2=0; c3=0; mul_add_c(a[0],b[0],c1,c2,c3); r[0]=c1; c1=0; mul_add_c(a[0],b[1],c2,c3,c1); mul_add_c(a[1],b[0],c2,c3,c1); r[1]=c2; c2=0; mul_add_c(a[2],b[0],c3,c1,c2); mul_add_c(a[1],b[1],c3,c1,c2); mul_add_c(a[0],b[2],c3,c1,c2); r[2]=c3; c3=0; mul_add_c(a[0],b[3],c1,c2,c3); mul_add_c(a[1],b[2],c1,c2,c3); mul_add_c(a[2],b[1],c1,c2,c3); mul_add_c(a[3],b[0],c1,c2,c3); r[3]=c1; c1=0; mul_add_c(a[4],b[0],c2,c3,c1); mul_add_c(a[3],b[1],c2,c3,c1); mul_add_c(a[2],b[2],c2,c3,c1); mul_add_c(a[1],b[3],c2,c3,c1); mul_add_c(a[0],b[4],c2,c3,c1); r[4]=c2; c2=0; mul_add_c(a[0],b[5],c3,c1,c2); mul_add_c(a[1],b[4],c3,c1,c2); mul_add_c(a[2],b[3],c3,c1,c2); mul_add_c(a[3],b[2],c3,c1,c2); mul_add_c(a[4],b[1],c3,c1,c2); mul_add_c(a[5],b[0],c3,c1,c2); r[5]=c3; c3=0; mul_add_c(a[6],b[0],c1,c2,c3); mul_add_c(a[5],b[1],c1,c2,c3); mul_add_c(a[4],b[2],c1,c2,c3); mul_add_c(a[3],b[3],c1,c2,c3); mul_add_c(a[2],b[4],c1,c2,c3); mul_add_c(a[1],b[5],c1,c2,c3); mul_add_c(a[0],b[6],c1,c2,c3); r[6]=c1; c1=0; mul_add_c(a[0],b[7],c2,c3,c1); mul_add_c(a[1],b[6],c2,c3,c1); mul_add_c(a[2],b[5],c2,c3,c1); mul_add_c(a[3],b[4],c2,c3,c1); mul_add_c(a[4],b[3],c2,c3,c1); mul_add_c(a[5],b[2],c2,c3,c1); mul_add_c(a[6],b[1],c2,c3,c1); mul_add_c(a[7],b[0],c2,c3,c1); r[7]=c2; c2=0; mul_add_c(a[7],b[1],c3,c1,c2); mul_add_c(a[6],b[2],c3,c1,c2); mul_add_c(a[5],b[3],c3,c1,c2); mul_add_c(a[4],b[4],c3,c1,c2); mul_add_c(a[3],b[5],c3,c1,c2); mul_add_c(a[2],b[6],c3,c1,c2); mul_add_c(a[1],b[7],c3,c1,c2); r[8]=c3; c3=0; mul_add_c(a[2],b[7],c1,c2,c3); mul_add_c(a[3],b[6],c1,c2,c3); mul_add_c(a[4],b[5],c1,c2,c3); mul_add_c(a[5],b[4],c1,c2,c3); mul_add_c(a[6],b[3],c1,c2,c3); mul_add_c(a[7],b[2],c1,c2,c3); r[9]=c1; c1=0; mul_add_c(a[7],b[3],c2,c3,c1); mul_add_c(a[6],b[4],c2,c3,c1); mul_add_c(a[5],b[5],c2,c3,c1); mul_add_c(a[4],b[6],c2,c3,c1); mul_add_c(a[3],b[7],c2,c3,c1); r[10]=c2; c2=0; mul_add_c(a[4],b[7],c3,c1,c2); mul_add_c(a[5],b[6],c3,c1,c2); mul_add_c(a[6],b[5],c3,c1,c2); mul_add_c(a[7],b[4],c3,c1,c2); r[11]=c3; c3=0; mul_add_c(a[7],b[5],c1,c2,c3); mul_add_c(a[6],b[6],c1,c2,c3); mul_add_c(a[5],b[7],c1,c2,c3); r[12]=c1; c1=0; mul_add_c(a[6],b[7],c2,c3,c1); mul_add_c(a[7],b[6],c2,c3,c1); r[13]=c2; c2=0; mul_add_c(a[7],b[7],c3,c1,c2); r[14]=c3; r[15]=c1; }
['int test_div_recp(BIO *bp, BN_CTX *ctx)\n\t{\n\tBIGNUM a,b,c,d,e;\n\tBN_RECP_CTX recp;\n\tint i;\n\tint j;\n\tBN_RECP_CTX_init(&recp);\n\tBN_init(&a);\n\tBN_init(&b);\n\tBN_init(&c);\n\tBN_init(&d);\n\tBN_init(&e);\n\tBN_rand(&a,400,0,0);\n\tfor (i=0; i<100; i++)\n\t\t{\n\t\tBN_rand(&b,50+i,0,0);\n\t\ta.neg=rand_neg();\n\t\tb.neg=rand_neg();\n\t\tBN_RECP_CTX_set(&recp,&b,ctx);\n\t\tif (bp == NULL)\n\t\t\tfor (j=0; j<100; j++)\n\t\t\t\tBN_div_recp(&d,&c,&a,&recp,ctx);\n\t\tBN_div_recp(&d,&c,&a,&recp,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,&d);\n\t\t\tBIO_puts(bp,"\\n");\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_mul(&e,&d,&b,ctx);\n\t\tBN_add(&d,&e,&c);\n\t\tBN_sub(&d,&d,&a);\n\t\tif(!BN_is_zero(&d))\n\t\t {\n\t\t BIO_puts(bp,"Reciprocal division test failed!\\n");\n\t\t return 0;\n\t\t }\n\t\t}\n\tBN_free(&a);\n\tBN_free(&b);\n\tBN_free(&c);\n\tBN_free(&d);\n\tBN_free(&e);\n\tBN_RECP_CTX_free(&recp);\n\treturn(1);\n\t}', 'int BN_rand(BIGNUM *rnd, int bits, int top, int bottom)\n\t{\n\tunsigned char *buf=NULL;\n\tint ret=0,bit,bytes,mask;\n\ttime_t tim;\n\tbytes=(bits+7)/8;\n\tbit=(bits-1)%8;\n\tmask=0xff<<bit;\n\tbuf=(unsigned char *)Malloc(bytes);\n\tif (buf == NULL)\n\t\t{\n\t\tBNerr(BN_F_BN_RAND,ERR_R_MALLOC_FAILURE);\n\t\tgoto err;\n\t\t}\n\ttime(&tim);\n\tRAND_seed(&tim,sizeof(tim));\n\tRAND_bytes(buf,(int)bytes);\n\tif (top)\n\t\t{\n\t\tif (bit == 0)\n\t\t\t{\n\t\t\tbuf[0]=1;\n\t\t\tbuf[1]|=0x80;\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tbuf[0]|=(3<<(bit-1));\n\t\t\tbuf[0]&= ~(mask<<1);\n\t\t\t}\n\t\t}\n\telse\n\t\t{\n\t\tbuf[0]|=(1<<bit);\n\t\tbuf[0]&= ~(mask<<1);\n\t\t}\n\tif (bottom)\n\t\tbuf[bytes-1]|=1;\n\tif (!BN_bin2bn(buf,bytes,rnd)) goto err;\n\tret=1;\nerr:\n\tif (buf != NULL)\n\t\t{\n\t\tmemset(buf,0,bytes);\n\t\tFree(buf);\n\t\t}\n\treturn(ret);\n\t}', 'int BN_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_div_recp(BIGNUM *dv, BIGNUM *rem, BIGNUM *m, BN_RECP_CTX *recp,\n\t BN_CTX *ctx)\n\t{\n\tint i,j,tos,ret=0,ex;\n\tBIGNUM *a,*b,*d,*r;\n\ttos=ctx->tos;\n\ta= &(ctx->bn[ctx->tos++]);\n\tb= &(ctx->bn[ctx->tos++]);\n\tif (dv != NULL)\n\t\td=dv;\n\telse\n\t\td= &(ctx->bn[ctx->tos++]);\n\tif (rem != NULL)\n\t\tr=rem;\n\telse\n\t\tr= &(ctx->bn[ctx->tos++]);\n\tif (BN_ucmp(m,&(recp->N)) < 0)\n\t\t{\n\t\tBN_zero(d);\n\t\tBN_copy(r,m);\n\t\tctx->tos=tos;\n\t\treturn(1);\n\t\t}\n\ti=BN_num_bits(m);\n\tj=recp->num_bits*2;\n\tif (j > i)\n\t\t{\n\t\ti=j;\n\t\tex=0;\n\t\t}\n\telse\n\t\t{\n\t\tex=(i-j)/2;\n\t\t}\n\tj=i/2;\n\tif (i != recp->shift)\n\t\trecp->shift=BN_reciprocal(&(recp->Nr),&(recp->N),\n\t\t\ti,ctx);\n\tif (!BN_rshift(a,m,j-ex)) goto err;\n\tif (!BN_mul(b,a,&(recp->Nr),ctx)) goto err;\n\tif (!BN_rshift(d,b,j+ex)) goto err;\n\td->neg=0;\n\tif (!BN_mul(b,&(recp->N),d,ctx)) goto err;\n\tif (!BN_usub(r,m,b)) goto err;\n\tr->neg=0;\n\tj=0;\n#if 1\n\twhile (BN_ucmp(r,&(recp->N)) >= 0)\n\t\t{\n\t\tif (j++ > 2)\n\t\t\t{\n\t\t\tBNerr(BN_F_BN_MOD_MUL_RECIPROCAL,BN_R_BAD_RECIPROCAL);\n\t\t\tgoto err;\n\t\t\t}\n\t\tif (!BN_usub(r,r,&(recp->N))) goto err;\n\t\tif (!BN_add_word(d,1)) goto err;\n\t\t}\n#endif\n\tr->neg=BN_is_zero(r)?0:m->neg;\n\td->neg=m->neg^recp->N.neg;\n\tret=1;\nerr:\n\tctx->tos=tos;\n\treturn(ret);\n\t}', 'int BN_mul(BIGNUM *r, BIGNUM *a, BIGNUM *b, BN_CTX *ctx)\n\t{\n\tint top,al,bl;\n\tBIGNUM *rr;\n#ifdef BN_RECURSION\n\tBIGNUM *t;\n\tint i,j,k;\n#endif\n#ifdef BN_COUNT\nprintf("BN_mul %d * %d\\n",a->top,b->top);\n#endif\n\tbn_check_top(a);\n\tbn_check_top(b);\n\tbn_check_top(r);\n\tal=a->top;\n\tbl=b->top;\n\tr->neg=a->neg^b->neg;\n\tif ((al == 0) || (bl == 0))\n\t\t{\n\t\tBN_zero(r);\n\t\treturn(1);\n\t\t}\n\ttop=al+bl;\n\tif ((r == a) || (r == b))\n\t\trr= &(ctx->bn[ctx->tos+1]);\n\telse\n\t\trr=r;\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n\tif (al == bl)\n\t\t{\n# ifdef BN_MUL_COMBA\n if (al == 8)\n\t\t\t{\n\t\t\tif (bn_wexpand(rr,16) == NULL) return(0);\n\t\t\tr->top=16;\n\t\t\tbn_mul_comba8(rr->d,a->d,b->d);\n\t\t\tgoto end;\n\t\t\t}\n\t\telse\n# endif\n#ifdef BN_RECURSION\n\t\tif (al < BN_MULL_SIZE_NORMAL)\n#endif\n\t\t\t{\n\t\t\tif (bn_wexpand(rr,top) == NULL) return(0);\n\t\t\trr->top=top;\n\t\t\tbn_mul_normal(rr->d,a->d,al,b->d,bl);\n\t\t\tgoto end;\n\t\t\t}\n# ifdef BN_RECURSION\n\t\tgoto symetric;\n# endif\n\t\t}\n#endif\n#ifdef BN_RECURSION\n\telse if ((al < BN_MULL_SIZE_NORMAL) || (bl < BN_MULL_SIZE_NORMAL))\n\t\t{\n\t\tif (bn_wexpand(rr,top) == NULL) return(0);\n\t\trr->top=top;\n\t\tbn_mul_normal(rr->d,a->d,al,b->d,bl);\n\t\tgoto end;\n\t\t}\n\telse\n\t\t{\n\t\ti=(al-bl);\n\t\tif ((i == 1) && !BN_get_flags(b,BN_FLG_STATIC_DATA))\n\t\t\t{\n\t\t\tbn_wexpand(b,al);\n\t\t\tb->d[bl]=0;\n\t\t\tbl++;\n\t\t\tgoto symetric;\n\t\t\t}\n\t\telse if ((i == -1) && !BN_get_flags(a,BN_FLG_STATIC_DATA))\n\t\t\t{\n\t\t\tbn_wexpand(a,bl);\n\t\t\ta->d[al]=0;\n\t\t\tal++;\n\t\t\tgoto symetric;\n\t\t\t}\n\t\t}\n#endif\n\tif (bn_wexpand(rr,top) == NULL) return(0);\n\trr->top=top;\n\tbn_mul_normal(rr->d,a->d,al,b->d,bl);\n#ifdef BN_RECURSION\n\tif (0)\n\t\t{\nsymetric:\n\t\tj=BN_num_bits_word((BN_ULONG)al);\n\t\tj=1<<(j-1);\n\t\tk=j+j;\n\t\tt= &(ctx->bn[ctx->tos]);\n\t\tif (al == j)\n\t\t\t{\n\t\t\tbn_wexpand(t,k*2);\n\t\t\tbn_wexpand(rr,k*2);\n\t\t\tbn_mul_recursive(rr->d,a->d,b->d,al,t->d);\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tbn_wexpand(a,k);\n\t\t\tbn_wexpand(b,k);\n\t\t\tbn_wexpand(t,k*4);\n\t\t\tbn_wexpand(rr,k*4);\n\t\t\tfor (i=a->top; i<k; i++)\n\t\t\t\ta->d[i]=0;\n\t\t\tfor (i=b->top; i<k; i++)\n\t\t\t\tb->d[i]=0;\n\t\t\tbn_mul_part_recursive(rr->d,a->d,b->d,al-j,j,t->d);\n\t\t\t}\n\t\trr->top=top;\n\t\t}\n#endif\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\nend:\n#endif\n\tbn_fix_top(rr);\n\tif (r != rr) BN_copy(r,rr);\n\treturn(1);\n\t}', 'void bn_mul_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n2,\n\t BN_ULONG *t)\n\t{\n\tint n=n2/2,c1,c2;\n\tunsigned int neg,zero;\n\tBN_ULONG ln,lo,*p;\n#ifdef BN_COUNT\nprintf(" bn_mul_recursive %d * %d\\n",n2,n2);\n#endif\n#ifdef BN_MUL_COMBA\n if (n2 == 8)\n\t\t{\n\t\tbn_mul_comba8(r,a,b);\n\t\treturn;\n\t\t}\n#endif\n\tif (n2 < BN_MUL_RECURSIVE_SIZE_NORMAL)\n\t\t{\n\t\tbn_mul_normal(r,a,n2,b,n2);\n\t\treturn;\n\t\t}\n\tc1=bn_cmp_words(a,&(a[n]),n);\n\tc2=bn_cmp_words(&(b[n]),b,n);\n\tzero=neg=0;\n\tswitch (c1*3+c2)\n\t\t{\n\tcase -4:\n\t\tbn_sub_words(t, &(a[n]),a, n);\n\t\tbn_sub_words(&(t[n]),b, &(b[n]),n);\n\t\tbreak;\n\tcase -3:\n\t\tzero=1;\n\t\tbreak;\n\tcase -2:\n\t\tbn_sub_words(t, &(a[n]),a, n);\n\t\tbn_sub_words(&(t[n]),&(b[n]),b, n);\n\t\tneg=1;\n\t\tbreak;\n\tcase -1:\n\tcase 0:\n\tcase 1:\n\t\tzero=1;\n\t\tbreak;\n\tcase 2:\n\t\tbn_sub_words(t, a, &(a[n]),n);\n\t\tbn_sub_words(&(t[n]),b, &(b[n]),n);\n\t\tneg=1;\n\t\tbreak;\n\tcase 3:\n\t\tzero=1;\n\t\tbreak;\n\tcase 4:\n\t\tbn_sub_words(t, a, &(a[n]),n);\n\t\tbn_sub_words(&(t[n]),&(b[n]),b, n);\n\t\tbreak;\n\t\t}\n#ifdef BN_MUL_COMBA\n\tif (n == 4)\n\t\t{\n\t\tif (!zero)\n\t\t\tbn_mul_comba4(&(t[n2]),t,&(t[n]));\n\t\telse\n\t\t\tmemset(&(t[n2]),0,8*sizeof(BN_ULONG));\n\t\tbn_mul_comba4(r,a,b);\n\t\tbn_mul_comba4(&(r[n2]),&(a[n]),&(b[n]));\n\t\t}\n\telse if (n == 8)\n\t\t{\n\t\tif (!zero)\n\t\t\tbn_mul_comba8(&(t[n2]),t,&(t[n]));\n\t\telse\n\t\t\tmemset(&(t[n2]),0,16*sizeof(BN_ULONG));\n\t\tbn_mul_comba8(r,a,b);\n\t\tbn_mul_comba8(&(r[n2]),&(a[n]),&(b[n]));\n\t\t}\n\telse\n#endif\n\t\t{\n\t\tp= &(t[n2*2]);\n\t\tif (!zero)\n\t\t\tbn_mul_recursive(&(t[n2]),t,&(t[n]),n,p);\n\t\telse\n\t\t\tmemset(&(t[n2]),0,n2*sizeof(BN_ULONG));\n\t\tbn_mul_recursive(r,a,b,n,p);\n\t\tbn_mul_recursive(&(r[n2]),&(a[n]),&(b[n]),n,p);\n\t\t}\n\tc1=(int)(bn_add_words(t,r,&(r[n2]),n2));\n\tif (neg)\n\t\t{\n\t\tc1-=(int)(bn_sub_words(&(t[n2]),t,&(t[n2]),n2));\n\t\t}\n\telse\n\t\t{\n\t\tc1+=(int)(bn_add_words(&(t[n2]),&(t[n2]),t,n2));\n\t\t}\n\tc1+=(int)(bn_add_words(&(r[n]),&(r[n]),&(t[n2]),n2));\n\tif (c1)\n\t\t{\n\t\tp= &(r[n+n2]);\n\t\tlo= *p;\n\t\tln=(lo+c1)&BN_MASK2;\n\t\t*p=ln;\n\t\tif (ln < (BN_ULONG)c1)\n\t\t\t{\n\t\t\tdo\t{\n\t\t\t\tp++;\n\t\t\t\tlo= *p;\n\t\t\t\tln=(lo+1)&BN_MASK2;\n\t\t\t\t*p=ln;\n\t\t\t\t} while (ln == 0);\n\t\t\t}\n\t\t}\n\t}', 'void bn_mul_comba8(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b)\n\t{\n#ifdef BN_LLONG\n\tBN_ULLONG t;\n#else\n\tBN_ULONG bl,bh;\n#endif\n\tBN_ULONG t1,t2;\n\tBN_ULONG c1,c2,c3;\n\tc1=0;\n\tc2=0;\n\tc3=0;\n\tmul_add_c(a[0],b[0],c1,c2,c3);\n\tr[0]=c1;\n\tc1=0;\n\tmul_add_c(a[0],b[1],c2,c3,c1);\n\tmul_add_c(a[1],b[0],c2,c3,c1);\n\tr[1]=c2;\n\tc2=0;\n\tmul_add_c(a[2],b[0],c3,c1,c2);\n\tmul_add_c(a[1],b[1],c3,c1,c2);\n\tmul_add_c(a[0],b[2],c3,c1,c2);\n\tr[2]=c3;\n\tc3=0;\n\tmul_add_c(a[0],b[3],c1,c2,c3);\n\tmul_add_c(a[1],b[2],c1,c2,c3);\n\tmul_add_c(a[2],b[1],c1,c2,c3);\n\tmul_add_c(a[3],b[0],c1,c2,c3);\n\tr[3]=c1;\n\tc1=0;\n\tmul_add_c(a[4],b[0],c2,c3,c1);\n\tmul_add_c(a[3],b[1],c2,c3,c1);\n\tmul_add_c(a[2],b[2],c2,c3,c1);\n\tmul_add_c(a[1],b[3],c2,c3,c1);\n\tmul_add_c(a[0],b[4],c2,c3,c1);\n\tr[4]=c2;\n\tc2=0;\n\tmul_add_c(a[0],b[5],c3,c1,c2);\n\tmul_add_c(a[1],b[4],c3,c1,c2);\n\tmul_add_c(a[2],b[3],c3,c1,c2);\n\tmul_add_c(a[3],b[2],c3,c1,c2);\n\tmul_add_c(a[4],b[1],c3,c1,c2);\n\tmul_add_c(a[5],b[0],c3,c1,c2);\n\tr[5]=c3;\n\tc3=0;\n\tmul_add_c(a[6],b[0],c1,c2,c3);\n\tmul_add_c(a[5],b[1],c1,c2,c3);\n\tmul_add_c(a[4],b[2],c1,c2,c3);\n\tmul_add_c(a[3],b[3],c1,c2,c3);\n\tmul_add_c(a[2],b[4],c1,c2,c3);\n\tmul_add_c(a[1],b[5],c1,c2,c3);\n\tmul_add_c(a[0],b[6],c1,c2,c3);\n\tr[6]=c1;\n\tc1=0;\n\tmul_add_c(a[0],b[7],c2,c3,c1);\n\tmul_add_c(a[1],b[6],c2,c3,c1);\n\tmul_add_c(a[2],b[5],c2,c3,c1);\n\tmul_add_c(a[3],b[4],c2,c3,c1);\n\tmul_add_c(a[4],b[3],c2,c3,c1);\n\tmul_add_c(a[5],b[2],c2,c3,c1);\n\tmul_add_c(a[6],b[1],c2,c3,c1);\n\tmul_add_c(a[7],b[0],c2,c3,c1);\n\tr[7]=c2;\n\tc2=0;\n\tmul_add_c(a[7],b[1],c3,c1,c2);\n\tmul_add_c(a[6],b[2],c3,c1,c2);\n\tmul_add_c(a[5],b[3],c3,c1,c2);\n\tmul_add_c(a[4],b[4],c3,c1,c2);\n\tmul_add_c(a[3],b[5],c3,c1,c2);\n\tmul_add_c(a[2],b[6],c3,c1,c2);\n\tmul_add_c(a[1],b[7],c3,c1,c2);\n\tr[8]=c3;\n\tc3=0;\n\tmul_add_c(a[2],b[7],c1,c2,c3);\n\tmul_add_c(a[3],b[6],c1,c2,c3);\n\tmul_add_c(a[4],b[5],c1,c2,c3);\n\tmul_add_c(a[5],b[4],c1,c2,c3);\n\tmul_add_c(a[6],b[3],c1,c2,c3);\n\tmul_add_c(a[7],b[2],c1,c2,c3);\n\tr[9]=c1;\n\tc1=0;\n\tmul_add_c(a[7],b[3],c2,c3,c1);\n\tmul_add_c(a[6],b[4],c2,c3,c1);\n\tmul_add_c(a[5],b[5],c2,c3,c1);\n\tmul_add_c(a[4],b[6],c2,c3,c1);\n\tmul_add_c(a[3],b[7],c2,c3,c1);\n\tr[10]=c2;\n\tc2=0;\n\tmul_add_c(a[4],b[7],c3,c1,c2);\n\tmul_add_c(a[5],b[6],c3,c1,c2);\n\tmul_add_c(a[6],b[5],c3,c1,c2);\n\tmul_add_c(a[7],b[4],c3,c1,c2);\n\tr[11]=c3;\n\tc3=0;\n\tmul_add_c(a[7],b[5],c1,c2,c3);\n\tmul_add_c(a[6],b[6],c1,c2,c3);\n\tmul_add_c(a[5],b[7],c1,c2,c3);\n\tr[12]=c1;\n\tc1=0;\n\tmul_add_c(a[6],b[7],c2,c3,c1);\n\tmul_add_c(a[7],b[6],c2,c3,c1);\n\tr[13]=c2;\n\tc2=0;\n\tmul_add_c(a[7],b[7],c3,c1,c2);\n\tr[14]=c3;\n\tr[15]=c1;\n\t}']
244
0
https://github.com/openssl/openssl/blob/f38edcab594b4934bd9625ef889934b2dfb5d1f0/crypto/ec/ec_mult.c/#L494
int ec_wNAF_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar, size_t num, const EC_POINT *points[], const BIGNUM *scalars[], BN_CTX *ctx) { const EC_POINT *generator = NULL; EC_POINT *tmp = NULL; size_t totalnum; size_t blocksize = 0, numblocks = 0; size_t pre_points_per_block = 0; size_t i, j; int k; int r_is_inverted = 0; int r_is_at_infinity = 1; size_t *wsize = NULL; signed char **wNAF = NULL; size_t *wNAF_len = NULL; size_t max_len = 0; size_t num_val; EC_POINT **val = NULL; EC_POINT **v; EC_POINT ***val_sub = NULL; const EC_PRE_COMP *pre_comp = NULL; int num_scalar = 0; int ret = 0; if (!BN_is_zero(group->order) && !BN_is_zero(group->cofactor)) { if ((scalar != NULL) && (num == 0)) { return ec_scalar_mul_ladder(group, r, scalar, NULL, ctx); } if ((scalar == NULL) && (num == 1)) { return ec_scalar_mul_ladder(group, r, scalars[0], points[0], ctx); } } if (scalar != NULL) { generator = EC_GROUP_get0_generator(group); if (generator == NULL) { ECerr(EC_F_EC_WNAF_MUL, EC_R_UNDEFINED_GENERATOR); goto err; } pre_comp = group->pre_comp.ec; if (pre_comp && pre_comp->numblocks && (EC_POINT_cmp(group, generator, pre_comp->points[0], ctx) == 0)) { blocksize = pre_comp->blocksize; numblocks = (BN_num_bits(scalar) / blocksize) + 1; if (numblocks > pre_comp->numblocks) numblocks = pre_comp->numblocks; pre_points_per_block = (size_t)1 << (pre_comp->w - 1); if (pre_comp->num != (pre_comp->numblocks * pre_points_per_block)) { ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR); goto err; } } else { pre_comp = NULL; numblocks = 1; num_scalar = 1; } } totalnum = num + numblocks; wsize = OPENSSL_malloc(totalnum * sizeof(wsize[0])); wNAF_len = OPENSSL_malloc(totalnum * sizeof(wNAF_len[0])); wNAF = OPENSSL_malloc((totalnum + 1) * sizeof(wNAF[0])); val_sub = OPENSSL_malloc(totalnum * sizeof(val_sub[0])); if (wNAF != NULL) wNAF[0] = NULL; if (wsize == NULL || wNAF_len == NULL || wNAF == NULL || val_sub == NULL) { ECerr(EC_F_EC_WNAF_MUL, ERR_R_MALLOC_FAILURE); goto err; } num_val = 0; for (i = 0; i < num + num_scalar; i++) { size_t bits; bits = i < num ? BN_num_bits(scalars[i]) : BN_num_bits(scalar); wsize[i] = EC_window_bits_for_scalar_size(bits); num_val += (size_t)1 << (wsize[i] - 1); wNAF[i + 1] = NULL; wNAF[i] = bn_compute_wNAF((i < num ? scalars[i] : scalar), wsize[i], &wNAF_len[i]); if (wNAF[i] == NULL) goto err; if (wNAF_len[i] > max_len) max_len = wNAF_len[i]; } if (numblocks) { if (pre_comp == NULL) { if (num_scalar != 1) { ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR); goto err; } } else { signed char *tmp_wNAF = NULL; size_t tmp_len = 0; if (num_scalar != 0) { ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR); goto err; } wsize[num] = pre_comp->w; tmp_wNAF = bn_compute_wNAF(scalar, wsize[num], &tmp_len); if (!tmp_wNAF) goto err; if (tmp_len <= max_len) { numblocks = 1; totalnum = num + 1; wNAF[num] = tmp_wNAF; wNAF[num + 1] = NULL; wNAF_len[num] = tmp_len; val_sub[num] = pre_comp->points; } else { signed char *pp; EC_POINT **tmp_points; if (tmp_len < numblocks * blocksize) { numblocks = (tmp_len + blocksize - 1) / blocksize; if (numblocks > pre_comp->numblocks) { ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR); OPENSSL_free(tmp_wNAF); goto err; } totalnum = num + numblocks; } pp = tmp_wNAF; tmp_points = pre_comp->points; for (i = num; i < totalnum; i++) { if (i < totalnum - 1) { wNAF_len[i] = blocksize; if (tmp_len < blocksize) { ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR); OPENSSL_free(tmp_wNAF); goto err; } tmp_len -= blocksize; } else wNAF_len[i] = tmp_len; wNAF[i + 1] = NULL; wNAF[i] = OPENSSL_malloc(wNAF_len[i]); if (wNAF[i] == NULL) { ECerr(EC_F_EC_WNAF_MUL, ERR_R_MALLOC_FAILURE); OPENSSL_free(tmp_wNAF); goto err; } memcpy(wNAF[i], pp, wNAF_len[i]); if (wNAF_len[i] > max_len) max_len = wNAF_len[i]; if (*tmp_points == NULL) { ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR); OPENSSL_free(tmp_wNAF); goto err; } val_sub[i] = tmp_points; tmp_points += pre_points_per_block; pp += blocksize; } OPENSSL_free(tmp_wNAF); } } } val = OPENSSL_malloc((num_val + 1) * sizeof(val[0])); if (val == NULL) { ECerr(EC_F_EC_WNAF_MUL, ERR_R_MALLOC_FAILURE); goto err; } val[num_val] = NULL; v = val; for (i = 0; i < num + num_scalar; i++) { val_sub[i] = v; for (j = 0; j < ((size_t)1 << (wsize[i] - 1)); j++) { *v = EC_POINT_new(group); if (*v == NULL) goto err; v++; } } if (!(v == val + num_val)) { ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR); goto err; } if ((tmp = EC_POINT_new(group)) == NULL) goto err; for (i = 0; i < num + num_scalar; i++) { if (i < num) { if (!EC_POINT_copy(val_sub[i][0], points[i])) goto err; } else { if (!EC_POINT_copy(val_sub[i][0], generator)) goto err; } if (wsize[i] > 1) { if (!EC_POINT_dbl(group, tmp, val_sub[i][0], ctx)) goto err; for (j = 1; j < ((size_t)1 << (wsize[i] - 1)); j++) { if (!EC_POINT_add (group, val_sub[i][j], val_sub[i][j - 1], tmp, ctx)) goto err; } } } if (!EC_POINTs_make_affine(group, num_val, val, ctx)) goto err; r_is_at_infinity = 1; for (k = max_len - 1; k >= 0; k--) { if (!r_is_at_infinity) { if (!EC_POINT_dbl(group, r, r, ctx)) goto err; } for (i = 0; i < totalnum; i++) { if (wNAF_len[i] > (size_t)k) { int digit = wNAF[i][k]; int is_neg; if (digit) { is_neg = digit < 0; if (is_neg) digit = -digit; if (is_neg != r_is_inverted) { if (!r_is_at_infinity) { if (!EC_POINT_invert(group, r, ctx)) goto err; } r_is_inverted = !r_is_inverted; } if (r_is_at_infinity) { if (!EC_POINT_copy(r, val_sub[i][digit >> 1])) goto err; r_is_at_infinity = 0; } else { if (!EC_POINT_add (group, r, r, val_sub[i][digit >> 1], ctx)) goto err; } } } } } if (r_is_at_infinity) { if (!EC_POINT_set_to_infinity(group, r)) goto err; } else { if (r_is_inverted) if (!EC_POINT_invert(group, r, ctx)) goto err; } ret = 1; err: EC_POINT_free(tmp); OPENSSL_free(wsize); OPENSSL_free(wNAF_len); if (wNAF != NULL) { signed char **w; for (w = wNAF; *w != NULL; w++) OPENSSL_free(*w); OPENSSL_free(wNAF); } if (val != NULL) { for (v = val; *v != NULL; v++) EC_POINT_clear_free(*v); OPENSSL_free(val); } OPENSSL_free(val_sub); return ret; }
['int sm2_decrypt(const EC_KEY *key,\n const EVP_MD *digest,\n const uint8_t *ciphertext,\n size_t ciphertext_len, uint8_t *ptext_buf, size_t *ptext_len)\n{\n int rc = 0;\n int i;\n BN_CTX *ctx = NULL;\n const EC_GROUP *group = EC_KEY_get0_group(key);\n EC_POINT *C1 = NULL;\n struct SM2_Ciphertext_st *sm2_ctext = NULL;\n BIGNUM *x2 = NULL;\n BIGNUM *y2 = NULL;\n uint8_t *x2y2 = NULL;\n uint8_t *computed_C3 = NULL;\n const size_t field_size = ec_field_size(group);\n const int hash_size = EVP_MD_size(digest);\n uint8_t *msg_mask = NULL;\n const uint8_t *C2 = NULL;\n const uint8_t *C3 = NULL;\n int msg_len = 0;\n EVP_MD_CTX *hash = NULL;\n if (field_size == 0 || hash_size <= 0)\n goto done;\n memset(ptext_buf, 0xFF, *ptext_len);\n sm2_ctext = d2i_SM2_Ciphertext(NULL, &ciphertext, ciphertext_len);\n if (sm2_ctext == NULL) {\n SM2err(SM2_F_SM2_DECRYPT, SM2_R_ASN1_ERROR);\n goto done;\n }\n if (sm2_ctext->C3->length != hash_size) {\n SM2err(SM2_F_SM2_DECRYPT, SM2_R_INVALID_ENCODING);\n goto done;\n }\n C2 = sm2_ctext->C2->data;\n C3 = sm2_ctext->C3->data;\n msg_len = sm2_ctext->C2->length;\n ctx = BN_CTX_new();\n if (ctx == NULL) {\n SM2err(SM2_F_SM2_DECRYPT, ERR_R_MALLOC_FAILURE);\n goto done;\n }\n BN_CTX_start(ctx);\n x2 = BN_CTX_get(ctx);\n y2 = BN_CTX_get(ctx);\n if (y2 == NULL) {\n SM2err(SM2_F_SM2_DECRYPT, ERR_R_BN_LIB);\n goto done;\n }\n msg_mask = OPENSSL_zalloc(msg_len);\n x2y2 = OPENSSL_zalloc(2 * field_size);\n computed_C3 = OPENSSL_zalloc(hash_size);\n if (msg_mask == NULL || x2y2 == NULL || computed_C3 == NULL) {\n SM2err(SM2_F_SM2_DECRYPT, ERR_R_MALLOC_FAILURE);\n goto done;\n }\n C1 = EC_POINT_new(group);\n if (C1 == NULL) {\n SM2err(SM2_F_SM2_DECRYPT, ERR_R_MALLOC_FAILURE);\n goto done;\n }\n if (!EC_POINT_set_affine_coordinates(group, C1, sm2_ctext->C1x,\n sm2_ctext->C1y, ctx)\n || !EC_POINT_mul(group, C1, NULL, C1, EC_KEY_get0_private_key(key),\n ctx)\n || !EC_POINT_get_affine_coordinates(group, C1, x2, y2, ctx)) {\n SM2err(SM2_F_SM2_DECRYPT, ERR_R_EC_LIB);\n goto done;\n }\n if (BN_bn2binpad(x2, x2y2, field_size) < 0\n || BN_bn2binpad(y2, x2y2 + field_size, field_size) < 0\n || !ECDH_KDF_X9_62(msg_mask, msg_len, x2y2, 2 * field_size, NULL, 0,\n digest)) {\n SM2err(SM2_F_SM2_DECRYPT, ERR_R_INTERNAL_ERROR);\n goto done;\n }\n for (i = 0; i != msg_len; ++i)\n ptext_buf[i] = C2[i] ^ msg_mask[i];\n hash = EVP_MD_CTX_new();\n if (hash == NULL) {\n SM2err(SM2_F_SM2_DECRYPT, ERR_R_MALLOC_FAILURE);\n goto done;\n }\n if (!EVP_DigestInit(hash, digest)\n || !EVP_DigestUpdate(hash, x2y2, field_size)\n || !EVP_DigestUpdate(hash, ptext_buf, msg_len)\n || !EVP_DigestUpdate(hash, x2y2 + field_size, field_size)\n || !EVP_DigestFinal(hash, computed_C3, NULL)) {\n SM2err(SM2_F_SM2_DECRYPT, ERR_R_EVP_LIB);\n goto done;\n }\n if (CRYPTO_memcmp(computed_C3, C3, hash_size) != 0) {\n SM2err(SM2_F_SM2_DECRYPT, SM2_R_INVALID_DIGEST);\n goto done;\n }\n rc = 1;\n *ptext_len = msg_len;\n done:\n if (rc == 0)\n memset(ptext_buf, 0, *ptext_len);\n OPENSSL_free(msg_mask);\n OPENSSL_free(x2y2);\n OPENSSL_free(computed_C3);\n EC_POINT_free(C1);\n BN_CTX_free(ctx);\n SM2_Ciphertext_free(sm2_ctext);\n EVP_MD_CTX_free(hash);\n return rc;\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 int ret = 0;\n size_t i = 0;\n BN_CTX *new_ctx = NULL;\n if ((scalar == NULL) && (num == 0)) {\n return EC_POINT_set_to_infinity(group, r);\n }\n if (!ec_point_is_compat(r, group)) {\n ECerr(EC_F_EC_POINTS_MUL, EC_R_INCOMPATIBLE_OBJECTS);\n return 0;\n }\n for (i = 0; i < num; i++) {\n if (!ec_point_is_compat(points[i], group)) {\n ECerr(EC_F_EC_POINTS_MUL, EC_R_INCOMPATIBLE_OBJECTS);\n return 0;\n }\n }\n if (ctx == NULL && (ctx = new_ctx = BN_CTX_secure_new()) == NULL) {\n ECerr(EC_F_EC_POINTS_MUL, ERR_R_INTERNAL_ERROR);\n return 0;\n }\n if (group->meth->mul != NULL)\n ret = group->meth->mul(group, r, scalar, num, points, scalars, ctx);\n else\n ret = ec_wNAF_mul(group, r, scalar, num, points, scalars, ctx);\n BN_CTX_free(new_ctx);\n return ret;\n}', 'int ec_wNAF_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,\n size_t num, const EC_POINT *points[], const BIGNUM *scalars[],\n BN_CTX *ctx)\n{\n const EC_POINT *generator = NULL;\n EC_POINT *tmp = NULL;\n size_t totalnum;\n size_t blocksize = 0, numblocks = 0;\n size_t pre_points_per_block = 0;\n size_t i, j;\n int k;\n int r_is_inverted = 0;\n int r_is_at_infinity = 1;\n size_t *wsize = NULL;\n signed char **wNAF = NULL;\n size_t *wNAF_len = NULL;\n size_t max_len = 0;\n size_t num_val;\n EC_POINT **val = NULL;\n EC_POINT **v;\n EC_POINT ***val_sub = NULL;\n const EC_PRE_COMP *pre_comp = NULL;\n int num_scalar = 0;\n int ret = 0;\n if (!BN_is_zero(group->order) && !BN_is_zero(group->cofactor)) {\n if ((scalar != NULL) && (num == 0)) {\n return ec_scalar_mul_ladder(group, r, scalar, NULL, ctx);\n }\n if ((scalar == NULL) && (num == 1)) {\n return ec_scalar_mul_ladder(group, r, scalars[0], points[0], ctx);\n }\n }\n if (scalar != NULL) {\n generator = EC_GROUP_get0_generator(group);\n if (generator == NULL) {\n ECerr(EC_F_EC_WNAF_MUL, EC_R_UNDEFINED_GENERATOR);\n goto err;\n }\n pre_comp = group->pre_comp.ec;\n if (pre_comp && pre_comp->numblocks\n && (EC_POINT_cmp(group, generator, pre_comp->points[0], ctx) ==\n 0)) {\n blocksize = pre_comp->blocksize;\n numblocks = (BN_num_bits(scalar) / blocksize) + 1;\n if (numblocks > pre_comp->numblocks)\n numblocks = pre_comp->numblocks;\n pre_points_per_block = (size_t)1 << (pre_comp->w - 1);\n if (pre_comp->num != (pre_comp->numblocks * pre_points_per_block)) {\n ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);\n goto err;\n }\n } else {\n pre_comp = NULL;\n numblocks = 1;\n num_scalar = 1;\n }\n }\n totalnum = num + numblocks;\n wsize = OPENSSL_malloc(totalnum * sizeof(wsize[0]));\n wNAF_len = OPENSSL_malloc(totalnum * sizeof(wNAF_len[0]));\n wNAF = OPENSSL_malloc((totalnum + 1) * sizeof(wNAF[0]));\n val_sub = OPENSSL_malloc(totalnum * sizeof(val_sub[0]));\n if (wNAF != NULL)\n wNAF[0] = NULL;\n if (wsize == NULL || wNAF_len == NULL || wNAF == NULL || val_sub == NULL) {\n ECerr(EC_F_EC_WNAF_MUL, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n num_val = 0;\n for (i = 0; i < num + num_scalar; i++) {\n size_t bits;\n bits = i < num ? BN_num_bits(scalars[i]) : BN_num_bits(scalar);\n wsize[i] = EC_window_bits_for_scalar_size(bits);\n num_val += (size_t)1 << (wsize[i] - 1);\n wNAF[i + 1] = NULL;\n wNAF[i] =\n bn_compute_wNAF((i < num ? scalars[i] : scalar), wsize[i],\n &wNAF_len[i]);\n if (wNAF[i] == NULL)\n goto err;\n if (wNAF_len[i] > max_len)\n max_len = wNAF_len[i];\n }\n if (numblocks) {\n if (pre_comp == NULL) {\n if (num_scalar != 1) {\n ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);\n goto err;\n }\n } else {\n signed char *tmp_wNAF = NULL;\n size_t tmp_len = 0;\n if (num_scalar != 0) {\n ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);\n goto err;\n }\n wsize[num] = pre_comp->w;\n tmp_wNAF = bn_compute_wNAF(scalar, wsize[num], &tmp_len);\n if (!tmp_wNAF)\n goto err;\n if (tmp_len <= max_len) {\n numblocks = 1;\n totalnum = num + 1;\n wNAF[num] = tmp_wNAF;\n wNAF[num + 1] = NULL;\n wNAF_len[num] = tmp_len;\n val_sub[num] = pre_comp->points;\n } else {\n signed char *pp;\n EC_POINT **tmp_points;\n if (tmp_len < numblocks * blocksize) {\n numblocks = (tmp_len + blocksize - 1) / blocksize;\n if (numblocks > pre_comp->numblocks) {\n ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);\n OPENSSL_free(tmp_wNAF);\n goto err;\n }\n totalnum = num + numblocks;\n }\n pp = tmp_wNAF;\n tmp_points = pre_comp->points;\n for (i = num; i < totalnum; i++) {\n if (i < totalnum - 1) {\n wNAF_len[i] = blocksize;\n if (tmp_len < blocksize) {\n ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);\n OPENSSL_free(tmp_wNAF);\n goto err;\n }\n tmp_len -= blocksize;\n } else\n wNAF_len[i] = tmp_len;\n wNAF[i + 1] = NULL;\n wNAF[i] = OPENSSL_malloc(wNAF_len[i]);\n if (wNAF[i] == NULL) {\n ECerr(EC_F_EC_WNAF_MUL, ERR_R_MALLOC_FAILURE);\n OPENSSL_free(tmp_wNAF);\n goto err;\n }\n memcpy(wNAF[i], pp, wNAF_len[i]);\n if (wNAF_len[i] > max_len)\n max_len = wNAF_len[i];\n if (*tmp_points == NULL) {\n ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);\n OPENSSL_free(tmp_wNAF);\n goto err;\n }\n val_sub[i] = tmp_points;\n tmp_points += pre_points_per_block;\n pp += blocksize;\n }\n OPENSSL_free(tmp_wNAF);\n }\n }\n }\n val = OPENSSL_malloc((num_val + 1) * sizeof(val[0]));\n if (val == NULL) {\n ECerr(EC_F_EC_WNAF_MUL, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n val[num_val] = NULL;\n v = val;\n for (i = 0; i < num + num_scalar; i++) {\n val_sub[i] = v;\n for (j = 0; j < ((size_t)1 << (wsize[i] - 1)); j++) {\n *v = EC_POINT_new(group);\n if (*v == NULL)\n goto err;\n v++;\n }\n }\n if (!(v == val + num_val)) {\n ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);\n goto err;\n }\n if ((tmp = EC_POINT_new(group)) == NULL)\n goto err;\n for (i = 0; i < num + num_scalar; i++) {\n if (i < num) {\n if (!EC_POINT_copy(val_sub[i][0], points[i]))\n goto err;\n } else {\n if (!EC_POINT_copy(val_sub[i][0], generator))\n goto err;\n }\n if (wsize[i] > 1) {\n if (!EC_POINT_dbl(group, tmp, val_sub[i][0], ctx))\n goto err;\n for (j = 1; j < ((size_t)1 << (wsize[i] - 1)); j++) {\n if (!EC_POINT_add\n (group, val_sub[i][j], val_sub[i][j - 1], tmp, ctx))\n goto err;\n }\n }\n }\n if (!EC_POINTs_make_affine(group, num_val, val, ctx))\n goto err;\n r_is_at_infinity = 1;\n for (k = max_len - 1; k >= 0; k--) {\n if (!r_is_at_infinity) {\n if (!EC_POINT_dbl(group, r, r, ctx))\n goto err;\n }\n for (i = 0; i < totalnum; i++) {\n if (wNAF_len[i] > (size_t)k) {\n int digit = wNAF[i][k];\n int is_neg;\n if (digit) {\n is_neg = digit < 0;\n if (is_neg)\n digit = -digit;\n if (is_neg != r_is_inverted) {\n if (!r_is_at_infinity) {\n if (!EC_POINT_invert(group, r, ctx))\n goto err;\n }\n r_is_inverted = !r_is_inverted;\n }\n if (r_is_at_infinity) {\n if (!EC_POINT_copy(r, val_sub[i][digit >> 1]))\n goto err;\n r_is_at_infinity = 0;\n } else {\n if (!EC_POINT_add\n (group, r, r, val_sub[i][digit >> 1], ctx))\n goto err;\n }\n }\n }\n }\n }\n if (r_is_at_infinity) {\n if (!EC_POINT_set_to_infinity(group, r))\n goto err;\n } else {\n if (r_is_inverted)\n if (!EC_POINT_invert(group, r, ctx))\n goto err;\n }\n ret = 1;\n err:\n EC_POINT_free(tmp);\n OPENSSL_free(wsize);\n OPENSSL_free(wNAF_len);\n if (wNAF != NULL) {\n signed char **w;\n for (w = wNAF; *w != NULL; w++)\n OPENSSL_free(*w);\n OPENSSL_free(wNAF);\n }\n if (val != NULL) {\n for (v = val; *v != NULL; v++)\n EC_POINT_clear_free(*v);\n OPENSSL_free(val);\n }\n OPENSSL_free(val_sub);\n return ret;\n}']
245
0
https://github.com/libav/libav/blob/688417399c69aadd4c287bdb0dec82ef8799011c/libavcodec/hevcdsp_template.c/#L901
PUT_HEVC_QPEL_HV(1, 1)
['QPEL(64)', 'PUT_HEVC_QPEL_HV(1, 1)']
246
0
https://github.com/libav/libav/blob/4391805916a1557278351f25428d0145b1073520/libavcodec/smacker.c/#L302
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 { if (smacker_decode_header_tree(smk, &gb, &smk->mmap_tbl, smk->mmap_last, mmap_size)) return -1; } 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 { if (smacker_decode_header_tree(smk, &gb, &smk->mclr_tbl, smk->mclr_last, mclr_size)) return -1; } 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 { if (smacker_decode_header_tree(smk, &gb, &smk->full_tbl, smk->full_last, full_size)) return -1; } 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 { if (smacker_decode_header_tree(smk, &gb, &smk->type_tbl, smk->type_last, type_size)) return -1; } 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 if (smacker_decode_header_tree(smk, &gb, &smk->mmap_tbl, smk->mmap_last, mmap_size))\n return -1;\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 if (smacker_decode_header_tree(smk, &gb, &smk->mclr_tbl, smk->mclr_last, mclr_size))\n return -1;\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 if (smacker_decode_header_tree(smk, &gb, &smk->full_tbl, smk->full_last, full_size))\n return -1;\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 if (smacker_decode_header_tree(smk, &gb, &smk->type_tbl, smk->type_last, type_size))\n return -1;\n }\n return 0;\n}', 'static inline void init_get_bits(GetBitContext *s,\n const uint8_t *buffer, int bit_size)\n{\n int buffer_size = (bit_size+7)>>3;\n if (buffer_size < 0 || bit_size < 0) {\n buffer_size = bit_size = 0;\n buffer = NULL;\n }\n s->buffer = buffer;\n s->size_in_bits = bit_size;\n#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 unsigned int get_bits1(GetBitContext *s){\n unsigned int index = s->index;\n uint8_t result = s->buffer[index>>3];\n#ifdef ALT_BITSTREAM_READER_LE\n result >>= index & 7;\n result &= 1;\n#else\n result <<= index & 7;\n result >>= 8 - 1;\n#endif\n#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}', '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}']
247
0
https://github.com/openssl/openssl/blob/ea32151f7b9353f8906188d007c6893704ac17bb/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_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->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->tlsext_debug_cb = 0;\n s->tlsext_debug_arg = NULL;\n s->tlsext_ticket_expected = 0;\n s->tlsext_status_type = ctx->tlsext_status_type;\n s->tlsext_status_expected = 0;\n s->tlsext_ocsp_ids = NULL;\n s->tlsext_ocsp_exts = NULL;\n s->tlsext_ocsp_resp = NULL;\n s->tlsext_ocsp_resplen = -1;\n SSL_CTX_up_ref(ctx);\n s->initial_ctx = ctx;\n# ifndef OPENSSL_NO_EC\n if (ctx->tlsext_ecpointformatlist) {\n s->tlsext_ecpointformatlist =\n OPENSSL_memdup(ctx->tlsext_ecpointformatlist,\n ctx->tlsext_ecpointformatlist_length);\n if (!s->tlsext_ecpointformatlist)\n goto err;\n s->tlsext_ecpointformatlist_length =\n ctx->tlsext_ecpointformatlist_length;\n }\n if (ctx->tlsext_ellipticcurvelist) {\n s->tlsext_ellipticcurvelist =\n OPENSSL_memdup(ctx->tlsext_ellipticcurvelist,\n ctx->tlsext_ellipticcurvelist_length);\n if (!s->tlsext_ellipticcurvelist)\n goto err;\n s->tlsext_ellipticcurvelist_length =\n ctx->tlsext_ellipticcurvelist_length;\n }\n# endif\n# ifndef OPENSSL_NO_NEXTPROTONEG\n s->next_proto_negotiated = NULL;\n# endif\n if (s->ctx->alpn_client_proto_list) {\n s->alpn_client_proto_list =\n OPENSSL_malloc(s->ctx->alpn_client_proto_list_len);\n if (s->alpn_client_proto_list == NULL)\n goto err;\n memcpy(s->alpn_client_proto_list, s->ctx->alpn_client_proto_list,\n s->ctx->alpn_client_proto_list_len);\n s->alpn_client_proto_list_len = s->ctx->alpn_client_proto_list_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_atomic_add(&s->references, -1, &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 if (s->bbio != NULL) {\n if (s->bbio == s->wbio) {\n s->wbio = BIO_pop(s->wbio);\n }\n BIO_free(s->bbio);\n s->bbio = NULL;\n }\n BIO_free_all(s->rbio);\n if (s->wbio != s->rbio)\n BIO_free_all(s->wbio);\n BUF_MEM_free(s->init_buf);\n sk_SSL_CIPHER_free(s->cipher_list);\n sk_SSL_CIPHER_free(s->cipher_list_by_id);\n if (s->session != NULL) {\n ssl_clear_bad_session(s);\n SSL_SESSION_free(s->session);\n }\n clear_ciphers(s);\n ssl_cert_free(s->cert);\n OPENSSL_free(s->tlsext_hostname);\n SSL_CTX_free(s->initial_ctx);\n#ifndef OPENSSL_NO_EC\n OPENSSL_free(s->tlsext_ecpointformatlist);\n OPENSSL_free(s->tlsext_ellipticcurvelist);\n#endif\n sk_X509_EXTENSION_pop_free(s->tlsext_ocsp_exts, X509_EXTENSION_free);\n#ifndef OPENSSL_NO_OCSP\n sk_OCSP_RESPID_pop_free(s->tlsext_ocsp_ids, OCSP_RESPID_free);\n#endif\n#ifndef OPENSSL_NO_CT\n SCT_LIST_free(s->scts);\n OPENSSL_free(s->tlsext_scts);\n#endif\n OPENSSL_free(s->tlsext_ocsp_resp);\n OPENSSL_free(s->alpn_client_proto_list);\n sk_X509_NAME_pop_free(s->client_CA, X509_NAME_free);\n 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->next_proto_negotiated);\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->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 if (lck)\n CRYPTO_THREAD_unlock(ctx->lock);\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 *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}']
248
0
https://github.com/libav/libav/blob/3ec394ea823c2a6e65a6abbdb2041ce1c66964f8/libavcodec/motion_est_template.c/#L717
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}']
249
0
https://github.com/openssl/openssl/blob/9b02dc97e4963969da69675a871dbe80e6d31cda/crypto/bn/bn_lib.c/#L260
static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words) { BN_ULONG *a = NULL; bn_check_top(b); if (words > (INT_MAX / (4 * BN_BITS2))) { BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG); return NULL; } if (BN_get_flags(b, BN_FLG_STATIC_DATA)) { BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA); return NULL; } if (BN_get_flags(b, BN_FLG_SECURE)) a = OPENSSL_secure_zalloc(words * sizeof(*a)); else a = OPENSSL_zalloc(words * sizeof(*a)); if (a == NULL) { BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE); return NULL; } assert(b->top <= words); if (b->top > 0) memcpy(a, b->d, sizeof(*a) * b->top); return a; }
['static int dh_builtin_genparams(DH *ret, int prime_len, int generator,\n BN_GENCB *cb)\n{\n BIGNUM *t1, *t2;\n int g, ok = -1;\n BN_CTX *ctx = NULL;\n ctx = BN_CTX_new();\n if (ctx == NULL)\n goto err;\n BN_CTX_start(ctx);\n t1 = BN_CTX_get(ctx);\n t2 = BN_CTX_get(ctx);\n if (t2 == NULL)\n goto err;\n if (!ret->p && ((ret->p = BN_new()) == NULL))\n goto err;\n if (!ret->g && ((ret->g = BN_new()) == NULL))\n goto err;\n if (generator <= 1) {\n DHerr(DH_F_DH_BUILTIN_GENPARAMS, DH_R_BAD_GENERATOR);\n goto err;\n }\n if (generator == DH_GENERATOR_2) {\n if (!BN_set_word(t1, 24))\n goto err;\n if (!BN_set_word(t2, 11))\n goto err;\n g = 2;\n } else if (generator == DH_GENERATOR_5) {\n if (!BN_set_word(t1, 10))\n goto err;\n if (!BN_set_word(t2, 3))\n goto err;\n g = 5;\n } else {\n if (!BN_set_word(t1, 2))\n goto err;\n if (!BN_set_word(t2, 1))\n goto err;\n g = generator;\n }\n if (!BN_generate_prime_ex(ret->p, prime_len, 1, t1, t2, cb))\n goto err;\n if (!BN_GENCB_call(cb, 3, 0))\n goto err;\n if (!BN_set_word(ret->g, g))\n goto err;\n ok = 1;\n err:\n if (ok == -1) {\n DHerr(DH_F_DH_BUILTIN_GENPARAMS, ERR_R_BN_LIB);\n ok = 0;\n }\n if (ctx != NULL) {\n BN_CTX_end(ctx);\n BN_CTX_free(ctx);\n }\n return ok;\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}', '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}']
250
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; }
['static int read_decoding_params(MLPDecodeContext *m, BitstreamContext *bc,\n unsigned int substr)\n{\n SubStream *s = &m->substream[substr];\n unsigned int ch;\n int ret;\n if (s->param_presence_flags & PARAM_PRESENCE)\n if (bitstream_read_bit(bc))\n s->param_presence_flags = bitstream_read(bc, 8);\n if (s->param_presence_flags & PARAM_BLOCKSIZE)\n if (bitstream_read_bit(bc)) {\n s->blocksize = bitstream_read(bc, 9);\n if (s->blocksize < 8 || s->blocksize > m->access_unit_size) {\n av_log(m->avctx, AV_LOG_ERROR, "Invalid blocksize.");\n s->blocksize = 0;\n return AVERROR_INVALIDDATA;\n }\n }\n if (s->param_presence_flags & PARAM_MATRIX)\n if (bitstream_read_bit(bc))\n if ((ret = read_matrix_params(m, substr, bc)) < 0)\n return ret;\n if (s->param_presence_flags & PARAM_OUTSHIFT)\n if (bitstream_read_bit(bc)) {\n for (ch = 0; ch <= s->max_matrix_channel; ch++)\n s->output_shift[ch] = bitstream_read_signed(bc, 4);\n if (substr == m->max_decoded_substream)\n m->dsp.mlp_pack_output = m->dsp.mlp_select_pack_output(s->ch_assign,\n s->output_shift,\n s->max_matrix_channel,\n m->avctx->sample_fmt == AV_SAMPLE_FMT_S32);\n }\n if (s->param_presence_flags & PARAM_QUANTSTEP)\n if (bitstream_read_bit(bc))\n for (ch = 0; ch <= s->max_channel; ch++) {\n ChannelParams *cp = &s->channel_params[ch];\n s->quant_step_size[ch] = bitstream_read(bc, 4);\n cp->sign_huff_offset = calculate_sign_huff(m, substr, ch);\n }\n for (ch = s->min_channel; ch <= s->max_channel; ch++)\n if (bitstream_read_bit(bc))\n if ((ret = read_channel_params(m, substr, bc, ch)) < 0)\n return ret;\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}']
251
0
https://github.com/openssl/openssl/blob/5ea564f154ebe8bda2a0e091a312e2058edf437f/crypto/bn/bn_gf2m.c/#L640
int BN_GF2m_mod_inv(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx) { BIGNUM *b, *c = NULL, *u = NULL, *v = NULL, *tmp; int ret = 0; bn_check_top(a); bn_check_top(p); BN_CTX_start(ctx); if ((b = BN_CTX_get(ctx)) == NULL) goto err; if ((c = BN_CTX_get(ctx)) == NULL) goto err; if ((u = BN_CTX_get(ctx)) == NULL) goto err; if ((v = BN_CTX_get(ctx)) == NULL) goto err; if (!BN_GF2m_mod(u, a, p)) goto err; if (BN_is_zero(u)) goto err; if (!BN_copy(v, p)) goto err; # if 0 if (!BN_one(b)) goto err; while (1) { while (!BN_is_odd(u)) { if (BN_is_zero(u)) goto err; if (!BN_rshift1(u, u)) goto err; if (BN_is_odd(b)) { if (!BN_GF2m_add(b, b, p)) goto err; } if (!BN_rshift1(b, b)) goto err; } if (BN_abs_is_word(u, 1)) break; if (BN_num_bits(u) < BN_num_bits(v)) { tmp = u; u = v; v = tmp; tmp = b; b = c; c = tmp; } if (!BN_GF2m_add(u, u, v)) goto err; if (!BN_GF2m_add(b, b, c)) goto err; } # else { int i; int ubits = BN_num_bits(u); int vbits = BN_num_bits(v); int top = p->top; BN_ULONG *udp, *bdp, *vdp, *cdp; if (!bn_wexpand(u, top)) goto err; udp = u->d; for (i = u->top; i < top; i++) udp[i] = 0; u->top = top; if (!bn_wexpand(b, top)) goto err; bdp = b->d; bdp[0] = 1; for (i = 1; i < top; i++) bdp[i] = 0; b->top = top; if (!bn_wexpand(c, top)) goto err; cdp = c->d; for (i = 0; i < top; i++) cdp[i] = 0; c->top = top; vdp = v->d; while (1) { while (ubits && !(udp[0] & 1)) { BN_ULONG u0, u1, b0, b1, mask; u0 = udp[0]; b0 = bdp[0]; mask = (BN_ULONG)0 - (b0 & 1); b0 ^= p->d[0] & mask; for (i = 0; i < top - 1; i++) { u1 = udp[i + 1]; udp[i] = ((u0 >> 1) | (u1 << (BN_BITS2 - 1))) & BN_MASK2; u0 = u1; b1 = bdp[i + 1] ^ (p->d[i + 1] & mask); bdp[i] = ((b0 >> 1) | (b1 << (BN_BITS2 - 1))) & BN_MASK2; b0 = b1; } udp[i] = u0 >> 1; bdp[i] = b0 >> 1; ubits--; } if (ubits <= BN_BITS2) { if (udp[0] == 0) goto err; if (udp[0] == 1) break; } if (ubits < vbits) { i = ubits; ubits = vbits; vbits = i; tmp = u; u = v; v = tmp; tmp = b; b = c; c = tmp; udp = vdp; vdp = v->d; bdp = cdp; cdp = c->d; } for (i = 0; i < top; i++) { udp[i] ^= vdp[i]; bdp[i] ^= cdp[i]; } if (ubits == vbits) { BN_ULONG ul; int utop = (ubits - 1) / BN_BITS2; while ((ul = udp[utop]) == 0 && utop) utop--; ubits = utop * BN_BITS2 + BN_num_bits_word(ul); } } bn_correct_top(b); } # endif if (!BN_copy(r, b)) goto err; bn_check_top(r); ret = 1; err: # ifdef BN_DEBUG bn_correct_top(c); bn_correct_top(u); bn_correct_top(v); # endif BN_CTX_end(ctx); return ret; }
['int BN_GF2m_mod_inv_arr(BIGNUM *r, const BIGNUM *xx, const int p[],\n BN_CTX *ctx)\n{\n BIGNUM *field;\n int ret = 0;\n bn_check_top(xx);\n BN_CTX_start(ctx);\n if ((field = BN_CTX_get(ctx)) == NULL)\n goto err;\n if (!BN_GF2m_arr2poly(p, field))\n goto err;\n ret = BN_GF2m_mod_inv(r, xx, field, ctx);\n bn_check_top(r);\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_GF2m_mod_inv(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx)\n{\n BIGNUM *b, *c = NULL, *u = NULL, *v = NULL, *tmp;\n int ret = 0;\n bn_check_top(a);\n bn_check_top(p);\n BN_CTX_start(ctx);\n if ((b = BN_CTX_get(ctx)) == NULL)\n goto err;\n if ((c = BN_CTX_get(ctx)) == NULL)\n goto err;\n if ((u = BN_CTX_get(ctx)) == NULL)\n goto err;\n if ((v = BN_CTX_get(ctx)) == NULL)\n goto err;\n if (!BN_GF2m_mod(u, a, p))\n goto err;\n if (BN_is_zero(u))\n goto err;\n if (!BN_copy(v, p))\n goto err;\n# if 0\n if (!BN_one(b))\n goto err;\n while (1) {\n while (!BN_is_odd(u)) {\n if (BN_is_zero(u))\n goto err;\n if (!BN_rshift1(u, u))\n goto err;\n if (BN_is_odd(b)) {\n if (!BN_GF2m_add(b, b, p))\n goto err;\n }\n if (!BN_rshift1(b, b))\n goto err;\n }\n if (BN_abs_is_word(u, 1))\n break;\n if (BN_num_bits(u) < BN_num_bits(v)) {\n tmp = u;\n u = v;\n v = tmp;\n tmp = b;\n b = c;\n c = tmp;\n }\n if (!BN_GF2m_add(u, u, v))\n goto err;\n if (!BN_GF2m_add(b, b, c))\n goto err;\n }\n# else\n {\n int i;\n int ubits = BN_num_bits(u);\n int vbits = BN_num_bits(v);\n int top = p->top;\n BN_ULONG *udp, *bdp, *vdp, *cdp;\n if (!bn_wexpand(u, top))\n goto err;\n udp = u->d;\n for (i = u->top; i < top; i++)\n udp[i] = 0;\n u->top = top;\n if (!bn_wexpand(b, top))\n goto err;\n bdp = b->d;\n bdp[0] = 1;\n for (i = 1; i < top; i++)\n bdp[i] = 0;\n b->top = top;\n if (!bn_wexpand(c, top))\n goto err;\n cdp = c->d;\n for (i = 0; i < top; i++)\n cdp[i] = 0;\n c->top = top;\n vdp = v->d;\n while (1) {\n while (ubits && !(udp[0] & 1)) {\n BN_ULONG u0, u1, b0, b1, mask;\n u0 = udp[0];\n b0 = bdp[0];\n mask = (BN_ULONG)0 - (b0 & 1);\n b0 ^= p->d[0] & mask;\n for (i = 0; i < top - 1; i++) {\n u1 = udp[i + 1];\n udp[i] = ((u0 >> 1) | (u1 << (BN_BITS2 - 1))) & BN_MASK2;\n u0 = u1;\n b1 = bdp[i + 1] ^ (p->d[i + 1] & mask);\n bdp[i] = ((b0 >> 1) | (b1 << (BN_BITS2 - 1))) & BN_MASK2;\n b0 = b1;\n }\n udp[i] = u0 >> 1;\n bdp[i] = b0 >> 1;\n ubits--;\n }\n if (ubits <= BN_BITS2) {\n if (udp[0] == 0)\n goto err;\n if (udp[0] == 1)\n break;\n }\n if (ubits < vbits) {\n i = ubits;\n ubits = vbits;\n vbits = i;\n tmp = u;\n u = v;\n v = tmp;\n tmp = b;\n b = c;\n c = tmp;\n udp = vdp;\n vdp = v->d;\n bdp = cdp;\n cdp = c->d;\n }\n for (i = 0; i < top; i++) {\n udp[i] ^= vdp[i];\n bdp[i] ^= cdp[i];\n }\n if (ubits == vbits) {\n BN_ULONG ul;\n int utop = (ubits - 1) / BN_BITS2;\n while ((ul = udp[utop]) == 0 && utop)\n utop--;\n ubits = utop * BN_BITS2 + BN_num_bits_word(ul);\n }\n }\n bn_correct_top(b);\n }\n# endif\n if (!BN_copy(r, b))\n goto err;\n bn_check_top(r);\n ret = 1;\n err:\n# ifdef BN_DEBUG\n bn_correct_top(c);\n bn_correct_top(u);\n bn_correct_top(v);\n# endif\n BN_CTX_end(ctx);\n return ret;\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}']
252
0
https://github.com/openssl/openssl/blob/95dc05bc6d0dfe0f3f3681f5e27afbc3f7a35eea/crypto/des/des_enc.c/#L143
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; }
['static int cfb64_test(unsigned char *cfb_cipher)\n\t{\n\tdes_key_schedule ks;\n\tint err=0,i,n;\n\tdes_key_sched(cfb_key,ks);\n\tmemcpy(cfb_tmp,cfb_iv,sizeof(cfb_iv));\n\tn=0;\n\tdes_cfb64_encrypt(plain,cfb_buf1,12,ks,cfb_tmp,&n,DES_ENCRYPT);\n\tdes_cfb64_encrypt(&(plain[12]),&(cfb_buf1[12]),sizeof(plain)-12,ks,\n\t\t\t cfb_tmp,&n,DES_ENCRYPT);\n\tif (memcmp(cfb_cipher,cfb_buf1,sizeof(plain)) != 0)\n\t\t{\n\t\terr=1;\n\t\tprintf("cfb_encrypt encrypt error\\n");\n\t\tfor (i=0; i<24; i+=8)\n\t\t\tprintf("%s\\n",pt(&(cfb_buf1[i])));\n\t\t}\n\tmemcpy(cfb_tmp,cfb_iv,sizeof(cfb_iv));\n\tn=0;\n\tdes_cfb64_encrypt(cfb_buf1,cfb_buf2,17,ks,cfb_tmp,&n,DES_DECRYPT);\n\tdes_cfb64_encrypt(&(cfb_buf1[17]),&(cfb_buf2[17]),\n\t\t\t sizeof(plain)-17,ks,cfb_tmp,&n,DES_DECRYPT);\n\tif (memcmp(plain,cfb_buf2,sizeof(plain)) != 0)\n\t\t{\n\t\terr=1;\n\t\tprintf("cfb_encrypt decrypt error\\n");\n\t\tfor (i=0; i<24; i+=8)\n\t\t\tprintf("%s\\n",pt(&(cfb_buf2[i])));\n\t\t}\n\treturn(err);\n\t}', 'void des_cfb64_encrypt(const unsigned char *in, unsigned char *out,\n\t long length, des_key_schedule schedule, des_cblock ivec, int *num,\n\t int enc)\n\t{\n\tregister DES_LONG v0,v1;\n\tregister long l=length;\n\tregister int n= *num;\n\tDES_LONG ti[2];\n\tunsigned char *iv,c,cc;\n\tiv=ivec;\n\tif (enc)\n\t\t{\n\t\twhile (l--)\n\t\t\t{\n\t\t\tif (n == 0)\n\t\t\t\t{\n\t\t\t\tc2l(iv,v0); ti[0]=v0;\n\t\t\t\tc2l(iv,v1); ti[1]=v1;\n\t\t\t\tdes_encrypt(ti,schedule,DES_ENCRYPT);\n\t\t\t\tiv=ivec;\n\t\t\t\tv0=ti[0]; l2c(v0,iv);\n\t\t\t\tv0=ti[1]; l2c(v0,iv);\n\t\t\t\tiv=ivec;\n\t\t\t\t}\n\t\t\tc= *(in++)^iv[n];\n\t\t\t*(out++)=c;\n\t\t\tiv[n]=c;\n\t\t\tn=(n+1)&0x07;\n\t\t\t}\n\t\t}\n\telse\n\t\t{\n\t\twhile (l--)\n\t\t\t{\n\t\t\tif (n == 0)\n\t\t\t\t{\n\t\t\t\tc2l(iv,v0); ti[0]=v0;\n\t\t\t\tc2l(iv,v1); ti[1]=v1;\n\t\t\t\tdes_encrypt(ti,schedule,DES_ENCRYPT);\n\t\t\t\tiv=ivec;\n\t\t\t\tv0=ti[0]; l2c(v0,iv);\n\t\t\t\tv0=ti[1]; l2c(v0,iv);\n\t\t\t\tiv=ivec;\n\t\t\t\t}\n\t\t\tcc= *(in++);\n\t\t\tc=iv[n];\n\t\t\tiv[n]=cc;\n\t\t\t*(out++)=c^cc;\n\t\t\tn=(n+1)&0x07;\n\t\t\t}\n\t\t}\n\tv0=v1=ti[0]=ti[1]=c=cc=0;\n\t*num=n;\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}']
253
0
https://github.com/libav/libav/blob/4c679750cb4cb112c19f862bd733bf6660a935bd/libavcodec/h264.c/#L409
static void await_references(H264Context *h) { MpegEncContext *const s = &h->s; const int mb_xy = h->mb_xy; const int mb_type = s->current_picture.f.mb_type[mb_xy]; int refs[2][48]; int nrefs[2] = { 0 }; int ref, list; memset(refs, -1, sizeof(refs)); if (IS_16X16(mb_type)) { get_lowest_part_y(h, refs, 0, 16, 0, IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1), nrefs); } else if (IS_16X8(mb_type)) { get_lowest_part_y(h, refs, 0, 8, 0, IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1), nrefs); get_lowest_part_y(h, refs, 8, 8, 8, IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1), nrefs); } else if (IS_8X16(mb_type)) { get_lowest_part_y(h, refs, 0, 16, 0, IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1), nrefs); get_lowest_part_y(h, refs, 4, 16, 0, IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1), nrefs); } else { int i; assert(IS_8X8(mb_type)); for (i = 0; i < 4; i++) { const int sub_mb_type = h->sub_mb_type[i]; const int n = 4 * i; int y_offset = (i & 2) << 2; if (IS_SUB_8X8(sub_mb_type)) { get_lowest_part_y(h, refs, n, 8, y_offset, IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1), nrefs); } else if (IS_SUB_8X4(sub_mb_type)) { get_lowest_part_y(h, refs, n, 4, y_offset, IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1), nrefs); get_lowest_part_y(h, refs, n + 2, 4, y_offset + 4, IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1), nrefs); } else if (IS_SUB_4X8(sub_mb_type)) { get_lowest_part_y(h, refs, n, 8, y_offset, IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1), nrefs); get_lowest_part_y(h, refs, n + 1, 8, y_offset, IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1), nrefs); } else { int j; assert(IS_SUB_4X4(sub_mb_type)); for (j = 0; j < 4; j++) { int sub_y_offset = y_offset + 2 * (j & 2); get_lowest_part_y(h, refs, n + j, 4, sub_y_offset, IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1), nrefs); } } } } for (list = h->list_count - 1; list >= 0; list--) for (ref = 0; ref < 48 && nrefs[list]; ref++) { int row = refs[list][ref]; if (row >= 0) { Picture *ref_pic = &h->ref_list[list][ref]; int ref_field = ref_pic->f.reference - 1; int ref_field_picture = ref_pic->field_picture; int pic_height = 16 * s->mb_height >> ref_field_picture; row <<= MB_MBAFF; nrefs[list]--; if (!FIELD_PICTURE && ref_field_picture) { ff_thread_await_progress(&ref_pic->f, FFMIN((row >> 1) - !(row & 1), pic_height - 1), 1); ff_thread_await_progress(&ref_pic->f, FFMIN((row >> 1), pic_height - 1), 0); } else if (FIELD_PICTURE && !ref_field_picture) { ff_thread_await_progress(&ref_pic->f, FFMIN(row * 2 + ref_field, pic_height - 1), 0); } else if (FIELD_PICTURE) { ff_thread_await_progress(&ref_pic->f, FFMIN(row, pic_height - 1), ref_field); } else { ff_thread_await_progress(&ref_pic->f, FFMIN(row, pic_height - 1), 0); } } } }
['static int slice_end(AVCodecContext *avctx, AVFrame *pict)\n{\n Mpeg1Context *s1 = avctx->priv_data;\n MpegEncContext *s = &s1->mpeg_enc_ctx;\n if (!s1->mpeg_enc_ctx_allocated || !s->current_picture_ptr)\n return 0;\n if (s->avctx->hwaccel) {\n if (s->avctx->hwaccel->end_frame(s->avctx) < 0)\n av_log(avctx, AV_LOG_ERROR, "hardware accelerator failed to decode picture\\n");\n }\n if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)\n ff_xvmc_field_end(s);\n if ( !s->first_field) {\n s->current_picture_ptr->f.qscale_type = FF_QSCALE_TYPE_MPEG2;\n ff_er_frame_end(s);\n ff_MPV_frame_end(s);\n if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) {\n *pict = s->current_picture_ptr->f;\n ff_print_debug_info(s, pict);\n } else {\n if (avctx->active_thread_type & FF_THREAD_FRAME)\n s->picture_number++;\n if (s->last_picture_ptr != NULL) {\n *pict = s->last_picture_ptr->f;\n ff_print_debug_info(s, pict);\n }\n }\n return 1;\n } else {\n return 0;\n }\n}', 'void ff_er_frame_end(MpegEncContext *s)\n{\n int i, mb_x, mb_y, error, error_type, dc_error, mv_error, ac_error;\n int distance;\n int threshold_part[4] = { 100, 100, 100 };\n int threshold = 50;\n int is_intra_likely;\n int size = s->b8_stride * 2 * s->mb_height;\n Picture *pic = s->current_picture_ptr;\n if (!s->err_recognition || s->error_count == 0 ||\n s->avctx->hwaccel ||\n s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU ||\n s->picture_structure != PICT_FRAME ||\n s->error_count == 3 * s->mb_width *\n (s->avctx->skip_top + s->avctx->skip_bottom)) {\n return;\n };\n if (s->current_picture.f.motion_val[0] == NULL) {\n av_log(s->avctx, AV_LOG_ERROR, "Warning MVs not available\\n");\n for (i = 0; i < 2; i++) {\n pic->f.ref_index[i] = av_mallocz(s->mb_stride * s->mb_height * 4 * sizeof(uint8_t));\n pic->motion_val_base[i] = av_mallocz((size + 4) * 2 * sizeof(uint16_t));\n pic->f.motion_val[i] = pic->motion_val_base[i] + 4;\n }\n pic->f.motion_subsample_log2 = 3;\n s->current_picture = *s->current_picture_ptr;\n }\n if (s->avctx->debug & FF_DEBUG_ER) {\n for (mb_y = 0; mb_y < s->mb_height; mb_y++) {\n for (mb_x = 0; mb_x < s->mb_width; mb_x++) {\n int status = s->error_status_table[mb_x + mb_y * s->mb_stride];\n av_log(s->avctx, AV_LOG_DEBUG, "%2X ", status);\n }\n av_log(s->avctx, AV_LOG_DEBUG, "\\n");\n }\n }\n for (error_type = 1; error_type <= 3; error_type++) {\n int end_ok = 0;\n for (i = s->mb_num - 1; i >= 0; i--) {\n const int mb_xy = s->mb_index2xy[i];\n int error = s->error_status_table[mb_xy];\n if (error & (1 << error_type))\n end_ok = 1;\n if (error & (8 << error_type))\n end_ok = 1;\n if (!end_ok)\n s->error_status_table[mb_xy] |= 1 << error_type;\n if (error & VP_START)\n end_ok = 0;\n }\n }\n if (s->partitioned_frame) {\n int end_ok = 0;\n for (i = s->mb_num - 1; i >= 0; i--) {\n const int mb_xy = s->mb_index2xy[i];\n int error = s->error_status_table[mb_xy];\n if (error & ER_AC_END)\n end_ok = 0;\n if ((error & ER_MV_END) ||\n (error & ER_DC_END) ||\n (error & ER_AC_ERROR))\n end_ok = 1;\n if (!end_ok)\n s->error_status_table[mb_xy]|= ER_AC_ERROR;\n if (error & VP_START)\n end_ok = 0;\n }\n }\n if (s->err_recognition & AV_EF_EXPLODE) {\n int end_ok = 1;\n for (i = s->mb_num - 2; i >= s->mb_width + 100; i--) {\n const int mb_xy = s->mb_index2xy[i];\n int error1 = s->error_status_table[mb_xy];\n int error2 = s->error_status_table[s->mb_index2xy[i + 1]];\n if (error1 & VP_START)\n end_ok = 1;\n if (error2 == (VP_START | ER_MB_ERROR | ER_MB_END) &&\n error1 != (VP_START | ER_MB_ERROR | ER_MB_END) &&\n ((error1 & ER_AC_END) || (error1 & ER_DC_END) ||\n (error1 & ER_MV_END))) {\n end_ok = 0;\n }\n if (!end_ok)\n s->error_status_table[mb_xy] |= ER_MB_ERROR;\n }\n }\n distance = 9999999;\n for (error_type = 1; error_type <= 3; error_type++) {\n for (i = s->mb_num - 1; i >= 0; i--) {\n const int mb_xy = s->mb_index2xy[i];\n int error = s->error_status_table[mb_xy];\n if (!s->mbskip_table[mb_xy])\n distance++;\n if (error & (1 << error_type))\n distance = 0;\n if (s->partitioned_frame) {\n if (distance < threshold_part[error_type - 1])\n s->error_status_table[mb_xy] |= 1 << error_type;\n } else {\n if (distance < threshold)\n s->error_status_table[mb_xy] |= 1 << error_type;\n }\n if (error & VP_START)\n distance = 9999999;\n }\n }\n error = 0;\n for (i = 0; i < s->mb_num; i++) {\n const int mb_xy = s->mb_index2xy[i];\n int old_error = s->error_status_table[mb_xy];\n if (old_error & VP_START) {\n error = old_error & ER_MB_ERROR;\n } else {\n error |= old_error & ER_MB_ERROR;\n s->error_status_table[mb_xy] |= error;\n }\n }\n if (!s->partitioned_frame) {\n for (i = 0; i < s->mb_num; i++) {\n const int mb_xy = s->mb_index2xy[i];\n error = s->error_status_table[mb_xy];\n if (error & ER_MB_ERROR)\n error |= ER_MB_ERROR;\n s->error_status_table[mb_xy] = error;\n }\n }\n dc_error = ac_error = mv_error = 0;\n for (i = 0; i < s->mb_num; i++) {\n const int mb_xy = s->mb_index2xy[i];\n error = s->error_status_table[mb_xy];\n if (error & ER_DC_ERROR)\n dc_error++;\n if (error & ER_AC_ERROR)\n ac_error++;\n if (error & ER_MV_ERROR)\n mv_error++;\n }\n av_log(s->avctx, AV_LOG_INFO, "concealing %d DC, %d AC, %d MV errors\\n",\n dc_error, ac_error, mv_error);\n is_intra_likely = is_intra_more_likely(s);\n for (i = 0; i < s->mb_num; i++) {\n const int mb_xy = s->mb_index2xy[i];\n error = s->error_status_table[mb_xy];\n if (!((error & ER_DC_ERROR) && (error & ER_MV_ERROR)))\n continue;\n if (is_intra_likely)\n s->current_picture.f.mb_type[mb_xy] = MB_TYPE_INTRA4x4;\n else\n s->current_picture.f.mb_type[mb_xy] = MB_TYPE_16x16 | MB_TYPE_L0;\n }\n if (!s->last_picture.f.data[0] && !s->next_picture.f.data[0])\n for (i = 0; i < s->mb_num; i++) {\n const int mb_xy = s->mb_index2xy[i];\n if (!IS_INTRA(s->current_picture.f.mb_type[mb_xy]))\n s->current_picture.f.mb_type[mb_xy] = MB_TYPE_INTRA4x4;\n }\n for (mb_y = 0; mb_y < s->mb_height; mb_y++) {\n s->mb_x = 0;\n s->mb_y = mb_y;\n ff_init_block_index(s);\n for (mb_x = 0; mb_x < s->mb_width; mb_x++) {\n const int mb_xy = mb_x + mb_y * s->mb_stride;\n const int mb_type = s->current_picture.f.mb_type[mb_xy];\n int dir = !s->last_picture.f.data[0];\n ff_update_block_index(s);\n error = s->error_status_table[mb_xy];\n if (IS_INTRA(mb_type))\n continue;\n if (error & ER_MV_ERROR)\n continue;\n if (!(error & ER_AC_ERROR))\n continue;\n s->mv_dir = dir ? MV_DIR_BACKWARD : MV_DIR_FORWARD;\n s->mb_intra = 0;\n s->mb_skipped = 0;\n if (IS_8X8(mb_type)) {\n int mb_index = mb_x * 2 + mb_y * 2 * s->b8_stride;\n int j;\n s->mv_type = MV_TYPE_8X8;\n for (j = 0; j < 4; j++) {\n s->mv[0][j][0] = s->current_picture.f.motion_val[dir][mb_index + (j & 1) + (j >> 1) * s->b8_stride][0];\n s->mv[0][j][1] = s->current_picture.f.motion_val[dir][mb_index + (j & 1) + (j >> 1) * s->b8_stride][1];\n }\n } else {\n s->mv_type = MV_TYPE_16X16;\n s->mv[0][0][0] = s->current_picture.f.motion_val[dir][mb_x * 2 + mb_y * 2 * s->b8_stride][0];\n s->mv[0][0][1] = s->current_picture.f.motion_val[dir][mb_x * 2 + mb_y * 2 * s->b8_stride][1];\n }\n s->dsp.clear_blocks(s->block[0]);\n s->mb_x = mb_x;\n s->mb_y = mb_y;\n decode_mb(s, 0 );\n }\n }\n if (s->pict_type == AV_PICTURE_TYPE_B) {\n for (mb_y = 0; mb_y < s->mb_height; mb_y++) {\n s->mb_x = 0;\n s->mb_y = mb_y;\n ff_init_block_index(s);\n for (mb_x = 0; mb_x < s->mb_width; mb_x++) {\n int xy = mb_x * 2 + mb_y * 2 * s->b8_stride;\n const int mb_xy = mb_x + mb_y * s->mb_stride;\n const int mb_type = s->current_picture.f.mb_type[mb_xy];\n ff_update_block_index(s);\n error = s->error_status_table[mb_xy];\n if (IS_INTRA(mb_type))\n continue;\n if (!(error & ER_MV_ERROR))\n continue;\n if (!(error & ER_AC_ERROR))\n continue;\n s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;\n if (!s->last_picture.f.data[0])\n s->mv_dir &= ~MV_DIR_FORWARD;\n if (!s->next_picture.f.data[0])\n s->mv_dir &= ~MV_DIR_BACKWARD;\n s->mb_intra = 0;\n s->mv_type = MV_TYPE_16X16;\n s->mb_skipped = 0;\n if (s->pp_time) {\n int time_pp = s->pp_time;\n int time_pb = s->pb_time;\n if (s->avctx->codec_id == CODEC_ID_H264) {\n } else {\n ff_thread_await_progress(&s->next_picture_ptr->f, mb_y, 0);\n }\n s->mv[0][0][0] = s->next_picture.f.motion_val[0][xy][0] * time_pb / time_pp;\n s->mv[0][0][1] = s->next_picture.f.motion_val[0][xy][1] * time_pb / time_pp;\n s->mv[1][0][0] = s->next_picture.f.motion_val[0][xy][0] * (time_pb - time_pp) / time_pp;\n s->mv[1][0][1] = s->next_picture.f.motion_val[0][xy][1] * (time_pb - time_pp) / time_pp;\n } else {\n s->mv[0][0][0] = 0;\n s->mv[0][0][1] = 0;\n s->mv[1][0][0] = 0;\n s->mv[1][0][1] = 0;\n }\n s->dsp.clear_blocks(s->block[0]);\n s->mb_x = mb_x;\n s->mb_y = mb_y;\n decode_mb(s, 0);\n }\n }\n } else\n guess_mv(s);\n if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)\n goto ec_clean;\n for (mb_y = 0; mb_y < s->mb_height; mb_y++) {\n for (mb_x = 0; mb_x < s->mb_width; mb_x++) {\n int dc, dcu, dcv, y, n;\n int16_t *dc_ptr;\n uint8_t *dest_y, *dest_cb, *dest_cr;\n const int mb_xy = mb_x + mb_y * s->mb_stride;\n const int mb_type = s->current_picture.f.mb_type[mb_xy];\n error = s->error_status_table[mb_xy];\n if (IS_INTRA(mb_type) && s->partitioned_frame)\n continue;\n dest_y = s->current_picture.f.data[0] + mb_x * 16 + mb_y * 16 * s->linesize;\n dest_cb = s->current_picture.f.data[1] + mb_x * 8 + mb_y * 8 * s->uvlinesize;\n dest_cr = s->current_picture.f.data[2] + mb_x * 8 + mb_y * 8 * s->uvlinesize;\n dc_ptr = &s->dc_val[0][mb_x * 2 + mb_y * 2 * s->b8_stride];\n for (n = 0; n < 4; n++) {\n dc = 0;\n for (y = 0; y < 8; y++) {\n int x;\n for (x = 0; x < 8; x++)\n dc += dest_y[x + (n & 1) * 8 +\n (y + (n >> 1) * 8) * s->linesize];\n }\n dc_ptr[(n & 1) + (n >> 1) * s->b8_stride] = (dc + 4) >> 3;\n }\n dcu = dcv = 0;\n for (y = 0; y < 8; y++) {\n int x;\n for (x = 0; x < 8; x++) {\n dcu += dest_cb[x + y * s->uvlinesize];\n dcv += dest_cr[x + y * s->uvlinesize];\n }\n }\n s->dc_val[1][mb_x + mb_y * s->mb_stride] = (dcu + 4) >> 3;\n s->dc_val[2][mb_x + mb_y * s->mb_stride] = (dcv + 4) >> 3;\n }\n }\n guess_dc(s, s->dc_val[0], s->mb_width * 2, s->mb_height * 2, s->b8_stride, 1);\n guess_dc(s, s->dc_val[1], s->mb_width, s->mb_height, s->mb_stride, 0);\n guess_dc(s, s->dc_val[2], s->mb_width, s->mb_height, s->mb_stride, 0);\n filter181(s->dc_val[0], s->mb_width * 2, s->mb_height * 2, s->b8_stride);\n for (mb_y = 0; mb_y < s->mb_height; mb_y++) {\n for (mb_x = 0; mb_x < s->mb_width; mb_x++) {\n uint8_t *dest_y, *dest_cb, *dest_cr;\n const int mb_xy = mb_x + mb_y * s->mb_stride;\n const int mb_type = s->current_picture.f.mb_type[mb_xy];\n error = s->error_status_table[mb_xy];\n if (IS_INTER(mb_type))\n continue;\n if (!(error & ER_AC_ERROR))\n continue;\n dest_y = s->current_picture.f.data[0] + mb_x * 16 + mb_y * 16 * s->linesize;\n dest_cb = s->current_picture.f.data[1] + mb_x * 8 + mb_y * 8 * s->uvlinesize;\n dest_cr = s->current_picture.f.data[2] + mb_x * 8 + mb_y * 8 * s->uvlinesize;\n put_dc(s, dest_y, dest_cb, dest_cr, mb_x, mb_y);\n }\n }\n if (s->avctx->error_concealment & FF_EC_DEBLOCK) {\n h_block_filter(s, s->current_picture.f.data[0], s->mb_width * 2,\n s->mb_height * 2, s->linesize, 1);\n h_block_filter(s, s->current_picture.f.data[1], s->mb_width,\n s->mb_height , s->uvlinesize, 0);\n h_block_filter(s, s->current_picture.f.data[2], s->mb_width,\n s->mb_height , s->uvlinesize, 0);\n v_block_filter(s, s->current_picture.f.data[0], s->mb_width * 2,\n s->mb_height * 2, s->linesize, 1);\n v_block_filter(s, s->current_picture.f.data[1], s->mb_width,\n s->mb_height , s->uvlinesize, 0);\n v_block_filter(s, s->current_picture.f.data[2], s->mb_width,\n s->mb_height , s->uvlinesize, 0);\n }\nec_clean:\n for (i = 0; i < s->mb_num; i++) {\n const int mb_xy = s->mb_index2xy[i];\n int error = s->error_status_table[mb_xy];\n if (s->pict_type != AV_PICTURE_TYPE_B &&\n (error & (ER_DC_ERROR | ER_MV_ERROR | ER_AC_ERROR))) {\n s->mbskip_table[mb_xy] = 0;\n }\n s->mbintra_table[mb_xy] = 1;\n }\n}', 'static int is_intra_more_likely(MpegEncContext *s)\n{\n int is_intra_likely, i, j, undamaged_count, skip_amount, mb_x, mb_y;\n if (!s->last_picture_ptr || !s->last_picture_ptr->f.data[0])\n return 1;\n undamaged_count = 0;\n for (i = 0; i < s->mb_num; i++) {\n const int mb_xy = s->mb_index2xy[i];\n const int error = s->error_status_table[mb_xy];\n if (!((error & ER_DC_ERROR) && (error & ER_MV_ERROR)))\n undamaged_count++;\n }\n if (s->codec_id == CODEC_ID_H264) {\n H264Context *h = (void*) s;\n if (h->list_count <= 0 || h->ref_count[0] <= 0 ||\n !h->ref_list[0][0].f.data[0])\n return 1;\n }\n if (undamaged_count < 5)\n return 0;\n if (CONFIG_MPEG_XVMC_DECODER &&\n s->avctx->xvmc_acceleration &&\n s->pict_type == AV_PICTURE_TYPE_I)\n return 1;\n skip_amount = FFMAX(undamaged_count / 50, 1);\n is_intra_likely = 0;\n j = 0;\n for (mb_y = 0; mb_y < s->mb_height - 1; mb_y++) {\n for (mb_x = 0; mb_x < s->mb_width; mb_x++) {\n int error;\n const int mb_xy = mb_x + mb_y * s->mb_stride;\n error = s->error_status_table[mb_xy];\n if ((error & ER_DC_ERROR) && (error & ER_MV_ERROR))\n continue;\n j++;\n if ((j % skip_amount) != 0)\n continue;\n if (s->pict_type == AV_PICTURE_TYPE_I) {\n uint8_t *mb_ptr = s->current_picture.f.data[0] +\n mb_x * 16 + mb_y * 16 * s->linesize;\n uint8_t *last_mb_ptr = s->last_picture.f.data[0] +\n mb_x * 16 + mb_y * 16 * s->linesize;\n if (s->avctx->codec_id == CODEC_ID_H264) {\n } else {\n ff_thread_await_progress(&s->last_picture_ptr->f,\n mb_y, 0);\n }\n is_intra_likely += s->dsp.sad[0](NULL, last_mb_ptr, mb_ptr,\n s->linesize, 16);\n is_intra_likely -= s->dsp.sad[0](NULL, last_mb_ptr,\n last_mb_ptr + s->linesize * 16,\n s->linesize, 16);\n } else {\n if (IS_INTRA(s->current_picture.f.mb_type[mb_xy]))\n is_intra_likely++;\n else\n is_intra_likely--;\n }\n }\n }\n return is_intra_likely > 0;\n}', 'static void decode_mb(MpegEncContext *s, int ref)\n{\n s->dest[0] = s->current_picture.f.data[0] + (s->mb_y * 16 * s->linesize) + s->mb_x * 16;\n s->dest[1] = s->current_picture.f.data[1] + (s->mb_y * (16 >> s->chroma_y_shift) * s->uvlinesize) + s->mb_x * (16 >> s->chroma_x_shift);\n s->dest[2] = s->current_picture.f.data[2] + (s->mb_y * (16 >> s->chroma_y_shift) * s->uvlinesize) + s->mb_x * (16 >> s->chroma_x_shift);\n if (CONFIG_H264_DECODER && s->codec_id == CODEC_ID_H264) {\n H264Context *h = (void*)s;\n h->mb_xy = s->mb_x + s->mb_y * s->mb_stride;\n memset(h->non_zero_count_cache, 0, sizeof(h->non_zero_count_cache));\n assert(ref >= 0);\n if (ref >= h->ref_count[0])\n ref = 0;\n fill_rectangle(&s->current_picture.f.ref_index[0][4 * h->mb_xy],\n 2, 2, 2, ref, 1);\n fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, ref, 1);\n fill_rectangle(h->mv_cache[0][scan8[0]], 4, 4, 8,\n pack16to32(s->mv[0][0][0], s->mv[0][0][1]), 4);\n assert(!FRAME_MBAFF);\n ff_h264_hl_decode_mb(h);\n } else {\n assert(ref == 0);\n ff_MPV_decode_mb(s, s->block);\n }\n}', 'void ff_h264_hl_decode_mb(H264Context *h)\n{\n MpegEncContext *const s = &h->s;\n const int mb_xy = h->mb_xy;\n const int mb_type = s->current_picture.f.mb_type[mb_xy];\n int is_complex = CONFIG_SMALL || h->is_complex || IS_INTRA_PCM(mb_type) || s->qscale == 0;\n if (CHROMA444) {\n if (is_complex || h->pixel_shift)\n hl_decode_mb_444_complex(h);\n else\n hl_decode_mb_444_simple_8(h);\n } else if (is_complex) {\n hl_decode_mb_complex(h);\n } else if (h->pixel_shift) {\n hl_decode_mb_simple_16(h);\n } else\n hl_decode_mb_simple_8(h);\n}', 'static av_noinline void FUNC(hl_decode_mb_444)(H264Context *h)\n{\n MpegEncContext *const s = &h->s;\n const int mb_x = s->mb_x;\n const int mb_y = s->mb_y;\n const int mb_xy = h->mb_xy;\n const int mb_type = s->current_picture.f.mb_type[mb_xy];\n uint8_t *dest[3];\n int linesize;\n int i, j, p;\n int *block_offset = &h->block_offset[0];\n const int transform_bypass = !SIMPLE && (s->qscale == 0 && h->sps.transform_bypass);\n const int plane_count = (SIMPLE || !CONFIG_GRAY || !(s->flags & CODEC_FLAG_GRAY)) ? 3 : 1;\n for (p = 0; p < plane_count; p++) {\n dest[p] = s->current_picture.f.data[p] +\n ((mb_x << PIXEL_SHIFT) + mb_y * s->linesize) * 16;\n s->dsp.prefetch(dest[p] + (s->mb_x & 3) * 4 * s->linesize + (64 << PIXEL_SHIFT),\n s->linesize, 4);\n }\n h->list_counts[mb_xy] = h->list_count;\n if (!SIMPLE && MB_FIELD) {\n linesize = h->mb_linesize = h->mb_uvlinesize = s->linesize * 2;\n block_offset = &h->block_offset[48];\n if (mb_y & 1)\n for (p = 0; p < 3; p++)\n dest[p] -= s->linesize * 15;\n if (FRAME_MBAFF) {\n int list;\n for (list = 0; list < h->list_count; list++) {\n if (!USES_LIST(mb_type, list))\n continue;\n if (IS_16X16(mb_type)) {\n int8_t *ref = &h->ref_cache[list][scan8[0]];\n fill_rectangle(ref, 4, 4, 8, (16 + *ref) ^ (s->mb_y & 1), 1);\n } else {\n for (i = 0; i < 16; i += 4) {\n int ref = h->ref_cache[list][scan8[i]];\n if (ref >= 0)\n fill_rectangle(&h->ref_cache[list][scan8[i]], 2, 2,\n 8, (16 + ref) ^ (s->mb_y & 1), 1);\n }\n }\n }\n }\n } else {\n linesize = h->mb_linesize = h->mb_uvlinesize = s->linesize;\n }\n if (!SIMPLE && IS_INTRA_PCM(mb_type)) {\n if (PIXEL_SHIFT) {\n const int bit_depth = h->sps.bit_depth_luma;\n GetBitContext gb;\n init_get_bits(&gb, (uint8_t *)h->mb, 768 * bit_depth);\n for (p = 0; p < plane_count; p++)\n for (i = 0; i < 16; i++) {\n uint16_t *tmp = (uint16_t *)(dest[p] + i * linesize);\n for (j = 0; j < 16; j++)\n tmp[j] = get_bits(&gb, bit_depth);\n }\n } else {\n for (p = 0; p < plane_count; p++)\n for (i = 0; i < 16; i++)\n memcpy(dest[p] + i * linesize,\n (uint8_t *)h->mb + p * 256 + i * 16, 16);\n }\n } else {\n if (IS_INTRA(mb_type)) {\n if (h->deblocking_filter)\n xchg_mb_border(h, dest[0], dest[1], dest[2], linesize,\n linesize, 1, 1, SIMPLE, PIXEL_SHIFT);\n for (p = 0; p < plane_count; p++)\n hl_decode_mb_predict_luma(h, mb_type, 1, SIMPLE,\n transform_bypass, PIXEL_SHIFT,\n block_offset, linesize, dest[p], p);\n if (h->deblocking_filter)\n xchg_mb_border(h, dest[0], dest[1], dest[2], linesize,\n linesize, 0, 1, SIMPLE, PIXEL_SHIFT);\n } else {\n FUNC(hl_motion_444)(h, dest[0], dest[1], dest[2],\n s->me.qpel_put, s->dsp.put_h264_chroma_pixels_tab,\n s->me.qpel_avg, s->dsp.avg_h264_chroma_pixels_tab,\n h->h264dsp.weight_h264_pixels_tab,\n h->h264dsp.biweight_h264_pixels_tab);\n }\n for (p = 0; p < plane_count; p++)\n hl_decode_mb_idct_luma(h, mb_type, 1, SIMPLE, transform_bypass,\n PIXEL_SHIFT, block_offset, linesize,\n dest[p], p);\n }\n if (h->cbp || IS_INTRA(mb_type)) {\n s->dsp.clear_blocks(h->mb);\n s->dsp.clear_blocks(h->mb + (24 * 16 << PIXEL_SHIFT));\n }\n}', 'static void MCFUNC(hl_motion)(H264Context *h, uint8_t *dest_y,\n uint8_t *dest_cb, uint8_t *dest_cr,\n qpel_mc_func(*qpix_put)[16],\n h264_chroma_mc_func(*chroma_put),\n qpel_mc_func(*qpix_avg)[16],\n h264_chroma_mc_func(*chroma_avg),\n h264_weight_func *weight_op,\n h264_biweight_func *weight_avg)\n{\n MpegEncContext *const s = &h->s;\n const int mb_xy = h->mb_xy;\n const int mb_type = s->current_picture.f.mb_type[mb_xy];\n assert(IS_INTER(mb_type));\n if (HAVE_THREADS && (s->avctx->active_thread_type & FF_THREAD_FRAME))\n await_references(h);\n prefetch_motion(h, 0, PIXEL_SHIFT, CHROMA_IDC);\n if (IS_16X16(mb_type)) {\n mc_part(h, 0, 1, 16, 0, dest_y, dest_cb, dest_cr, 0, 0,\n qpix_put[0], chroma_put[0], qpix_avg[0], chroma_avg[0],\n weight_op, weight_avg,\n IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));\n } else if (IS_16X8(mb_type)) {\n mc_part(h, 0, 0, 8, 8 << PIXEL_SHIFT, dest_y, dest_cb, dest_cr, 0, 0,\n qpix_put[1], chroma_put[0], qpix_avg[1], chroma_avg[0],\n weight_op, weight_avg,\n IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));\n mc_part(h, 8, 0, 8, 8 << PIXEL_SHIFT, dest_y, dest_cb, dest_cr, 0, 4,\n qpix_put[1], chroma_put[0], qpix_avg[1], chroma_avg[0],\n weight_op, weight_avg,\n IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1));\n } else if (IS_8X16(mb_type)) {\n mc_part(h, 0, 0, 16, 8 * h->mb_linesize, dest_y, dest_cb, dest_cr, 0, 0,\n qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],\n &weight_op[1], &weight_avg[1],\n IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));\n mc_part(h, 4, 0, 16, 8 * h->mb_linesize, dest_y, dest_cb, dest_cr, 4, 0,\n qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],\n &weight_op[1], &weight_avg[1],\n IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1));\n } else {\n int i;\n assert(IS_8X8(mb_type));\n for (i = 0; i < 4; i++) {\n const int sub_mb_type = h->sub_mb_type[i];\n const int n = 4 * i;\n int x_offset = (i & 1) << 2;\n int y_offset = (i & 2) << 1;\n if (IS_SUB_8X8(sub_mb_type)) {\n mc_part(h, n, 1, 8, 0, dest_y, dest_cb, dest_cr,\n x_offset, y_offset,\n qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],\n &weight_op[1], &weight_avg[1],\n IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));\n } else if (IS_SUB_8X4(sub_mb_type)) {\n mc_part(h, n, 0, 4, 4 << PIXEL_SHIFT, dest_y, dest_cb, dest_cr,\n x_offset, y_offset,\n qpix_put[2], chroma_put[1], qpix_avg[2], chroma_avg[1],\n &weight_op[1], &weight_avg[1],\n IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));\n mc_part(h, n + 2, 0, 4, 4 << PIXEL_SHIFT,\n dest_y, dest_cb, dest_cr, x_offset, y_offset + 2,\n qpix_put[2], chroma_put[1], qpix_avg[2], chroma_avg[1],\n &weight_op[1], &weight_avg[1],\n IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));\n } else if (IS_SUB_4X8(sub_mb_type)) {\n mc_part(h, n, 0, 8, 4 * h->mb_linesize,\n dest_y, dest_cb, dest_cr, x_offset, y_offset,\n qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],\n &weight_op[2], &weight_avg[2],\n IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));\n mc_part(h, n + 1, 0, 8, 4 * h->mb_linesize,\n dest_y, dest_cb, dest_cr, x_offset + 2, y_offset,\n qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],\n &weight_op[2], &weight_avg[2],\n IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));\n } else {\n int j;\n assert(IS_SUB_4X4(sub_mb_type));\n for (j = 0; j < 4; j++) {\n int sub_x_offset = x_offset + 2 * (j & 1);\n int sub_y_offset = y_offset + (j & 2);\n mc_part(h, n + j, 1, 4, 0,\n dest_y, dest_cb, dest_cr, sub_x_offset, sub_y_offset,\n qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],\n &weight_op[2], &weight_avg[2],\n IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));\n }\n }\n }\n }\n prefetch_motion(h, 1, PIXEL_SHIFT, CHROMA_IDC);\n}', 'static void await_references(H264Context *h)\n{\n MpegEncContext *const s = &h->s;\n const int mb_xy = h->mb_xy;\n const int mb_type = s->current_picture.f.mb_type[mb_xy];\n int refs[2][48];\n int nrefs[2] = { 0 };\n int ref, list;\n memset(refs, -1, sizeof(refs));\n if (IS_16X16(mb_type)) {\n get_lowest_part_y(h, refs, 0, 16, 0,\n IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1), nrefs);\n } else if (IS_16X8(mb_type)) {\n get_lowest_part_y(h, refs, 0, 8, 0,\n IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1), nrefs);\n get_lowest_part_y(h, refs, 8, 8, 8,\n IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1), nrefs);\n } else if (IS_8X16(mb_type)) {\n get_lowest_part_y(h, refs, 0, 16, 0,\n IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1), nrefs);\n get_lowest_part_y(h, refs, 4, 16, 0,\n IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1), nrefs);\n } else {\n int i;\n assert(IS_8X8(mb_type));\n for (i = 0; i < 4; i++) {\n const int sub_mb_type = h->sub_mb_type[i];\n const int n = 4 * i;\n int y_offset = (i & 2) << 2;\n if (IS_SUB_8X8(sub_mb_type)) {\n get_lowest_part_y(h, refs, n, 8, y_offset,\n IS_DIR(sub_mb_type, 0, 0),\n IS_DIR(sub_mb_type, 0, 1),\n nrefs);\n } else if (IS_SUB_8X4(sub_mb_type)) {\n get_lowest_part_y(h, refs, n, 4, y_offset,\n IS_DIR(sub_mb_type, 0, 0),\n IS_DIR(sub_mb_type, 0, 1),\n nrefs);\n get_lowest_part_y(h, refs, n + 2, 4, y_offset + 4,\n IS_DIR(sub_mb_type, 0, 0),\n IS_DIR(sub_mb_type, 0, 1),\n nrefs);\n } else if (IS_SUB_4X8(sub_mb_type)) {\n get_lowest_part_y(h, refs, n, 8, y_offset,\n IS_DIR(sub_mb_type, 0, 0),\n IS_DIR(sub_mb_type, 0, 1),\n nrefs);\n get_lowest_part_y(h, refs, n + 1, 8, y_offset,\n IS_DIR(sub_mb_type, 0, 0),\n IS_DIR(sub_mb_type, 0, 1),\n nrefs);\n } else {\n int j;\n assert(IS_SUB_4X4(sub_mb_type));\n for (j = 0; j < 4; j++) {\n int sub_y_offset = y_offset + 2 * (j & 2);\n get_lowest_part_y(h, refs, n + j, 4, sub_y_offset,\n IS_DIR(sub_mb_type, 0, 0),\n IS_DIR(sub_mb_type, 0, 1),\n nrefs);\n }\n }\n }\n }\n for (list = h->list_count - 1; list >= 0; list--)\n for (ref = 0; ref < 48 && nrefs[list]; ref++) {\n int row = refs[list][ref];\n if (row >= 0) {\n Picture *ref_pic = &h->ref_list[list][ref];\n int ref_field = ref_pic->f.reference - 1;\n int ref_field_picture = ref_pic->field_picture;\n int pic_height = 16 * s->mb_height >> ref_field_picture;\n row <<= MB_MBAFF;\n nrefs[list]--;\n if (!FIELD_PICTURE && ref_field_picture) {\n ff_thread_await_progress(&ref_pic->f,\n FFMIN((row >> 1) - !(row & 1),\n pic_height - 1),\n 1);\n ff_thread_await_progress(&ref_pic->f,\n FFMIN((row >> 1), pic_height - 1),\n 0);\n } else if (FIELD_PICTURE && !ref_field_picture) {\n ff_thread_await_progress(&ref_pic->f,\n FFMIN(row * 2 + ref_field,\n pic_height - 1),\n 0);\n } else if (FIELD_PICTURE) {\n ff_thread_await_progress(&ref_pic->f,\n FFMIN(row, pic_height - 1),\n ref_field);\n } else {\n ff_thread_await_progress(&ref_pic->f,\n FFMIN(row, pic_height - 1),\n 0);\n }\n }\n }\n}']
254
0
https://github.com/openssl/openssl/blob/6fc1748ec65c94c195d02b59556434e36a5f7651/ssl/statem/statem_srvr.c/#L2205
static int tls_process_cke_rsa(SSL *s, PACKET *pkt, int *al) { #ifndef OPENSSL_NO_RSA unsigned char rand_premaster_secret[SSL_MAX_MASTER_KEY_LENGTH]; int decrypt_len; unsigned char decrypt_good, version_good; size_t j, padding_len; PACKET enc_premaster; RSA *rsa = NULL; unsigned char *rsa_decrypt = NULL; int ret = 0; rsa = EVP_PKEY_get0_RSA(s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey); if (rsa == NULL) { *al = SSL_AD_HANDSHAKE_FAILURE; SSLerr(SSL_F_TLS_PROCESS_CKE_RSA, SSL_R_MISSING_RSA_CERTIFICATE); return 0; } if (s->version == SSL3_VERSION || s->version == DTLS1_BAD_VER) { enc_premaster = *pkt; } else { if (!PACKET_get_length_prefixed_2(pkt, &enc_premaster) || PACKET_remaining(pkt) != 0) { *al = SSL_AD_DECODE_ERROR; SSLerr(SSL_F_TLS_PROCESS_CKE_RSA, SSL_R_LENGTH_MISMATCH); return 0; } } if (RSA_size(rsa) < SSL_MAX_MASTER_KEY_LENGTH) { *al = SSL_AD_INTERNAL_ERROR; SSLerr(SSL_F_TLS_PROCESS_CKE_RSA, RSA_R_KEY_SIZE_TOO_SMALL); return 0; } rsa_decrypt = OPENSSL_malloc(RSA_size(rsa)); if (rsa_decrypt == NULL) { *al = SSL_AD_INTERNAL_ERROR; SSLerr(SSL_F_TLS_PROCESS_CKE_RSA, ERR_R_MALLOC_FAILURE); return 0; } if (RAND_bytes(rand_premaster_secret, sizeof(rand_premaster_secret)) <= 0) goto err; decrypt_len = RSA_private_decrypt(PACKET_remaining(&enc_premaster), PACKET_data(&enc_premaster), rsa_decrypt, rsa, RSA_NO_PADDING); if (decrypt_len < 0) goto err; if (decrypt_len < 11 + SSL_MAX_MASTER_KEY_LENGTH) { *al = SSL_AD_DECRYPT_ERROR; SSLerr(SSL_F_TLS_PROCESS_CKE_RSA, SSL_R_DECRYPTION_FAILED); goto err; } padding_len = decrypt_len - SSL_MAX_MASTER_KEY_LENGTH; decrypt_good = constant_time_eq_int_8(rsa_decrypt[0], 0) & constant_time_eq_int_8(rsa_decrypt[1], 2); for (j = 2; j < padding_len - 1; j++) { decrypt_good &= ~constant_time_is_zero_8(rsa_decrypt[j]); } decrypt_good &= constant_time_is_zero_8(rsa_decrypt[padding_len - 1]); version_good = constant_time_eq_8(rsa_decrypt[padding_len], (unsigned)(s->client_version >> 8)); version_good &= constant_time_eq_8(rsa_decrypt[padding_len + 1], (unsigned)(s->client_version & 0xff)); if (s->options & SSL_OP_TLS_ROLLBACK_BUG) { unsigned char workaround_good; workaround_good = constant_time_eq_8(rsa_decrypt[padding_len], (unsigned)(s->version >> 8)); workaround_good &= constant_time_eq_8(rsa_decrypt[padding_len + 1], (unsigned)(s->version & 0xff)); version_good |= workaround_good; } decrypt_good &= version_good; for (j = 0; j < sizeof(rand_premaster_secret); j++) { rsa_decrypt[padding_len + j] = constant_time_select_8(decrypt_good, rsa_decrypt[padding_len + j], rand_premaster_secret[j]); } if (!ssl_generate_master_secret(s, rsa_decrypt + padding_len, sizeof(rand_premaster_secret), 0)) { *al = SSL_AD_INTERNAL_ERROR; SSLerr(SSL_F_TLS_PROCESS_CKE_RSA, ERR_R_INTERNAL_ERROR); goto err; } ret = 1; err: OPENSSL_free(rsa_decrypt); return ret; #else *al = SSL_AD_INTERNAL_ERROR; SSLerr(SSL_F_TLS_PROCESS_CKE_RSA, ERR_R_INTERNAL_ERROR); return 0; #endif }
['static int tls_process_cke_rsa(SSL *s, PACKET *pkt, int *al)\n{\n#ifndef OPENSSL_NO_RSA\n unsigned char rand_premaster_secret[SSL_MAX_MASTER_KEY_LENGTH];\n int decrypt_len;\n unsigned char decrypt_good, version_good;\n size_t j, padding_len;\n PACKET enc_premaster;\n RSA *rsa = NULL;\n unsigned char *rsa_decrypt = NULL;\n int ret = 0;\n rsa = EVP_PKEY_get0_RSA(s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey);\n if (rsa == NULL) {\n *al = SSL_AD_HANDSHAKE_FAILURE;\n SSLerr(SSL_F_TLS_PROCESS_CKE_RSA, SSL_R_MISSING_RSA_CERTIFICATE);\n return 0;\n }\n if (s->version == SSL3_VERSION || s->version == DTLS1_BAD_VER) {\n enc_premaster = *pkt;\n } else {\n if (!PACKET_get_length_prefixed_2(pkt, &enc_premaster)\n || PACKET_remaining(pkt) != 0) {\n *al = SSL_AD_DECODE_ERROR;\n SSLerr(SSL_F_TLS_PROCESS_CKE_RSA, SSL_R_LENGTH_MISMATCH);\n return 0;\n }\n }\n if (RSA_size(rsa) < SSL_MAX_MASTER_KEY_LENGTH) {\n *al = SSL_AD_INTERNAL_ERROR;\n SSLerr(SSL_F_TLS_PROCESS_CKE_RSA, RSA_R_KEY_SIZE_TOO_SMALL);\n return 0;\n }\n rsa_decrypt = OPENSSL_malloc(RSA_size(rsa));\n if (rsa_decrypt == NULL) {\n *al = SSL_AD_INTERNAL_ERROR;\n SSLerr(SSL_F_TLS_PROCESS_CKE_RSA, ERR_R_MALLOC_FAILURE);\n return 0;\n }\n if (RAND_bytes(rand_premaster_secret, sizeof(rand_premaster_secret)) <= 0)\n goto err;\n decrypt_len = RSA_private_decrypt(PACKET_remaining(&enc_premaster),\n PACKET_data(&enc_premaster),\n rsa_decrypt, rsa, RSA_NO_PADDING);\n if (decrypt_len < 0)\n goto err;\n if (decrypt_len < 11 + SSL_MAX_MASTER_KEY_LENGTH) {\n *al = SSL_AD_DECRYPT_ERROR;\n SSLerr(SSL_F_TLS_PROCESS_CKE_RSA, SSL_R_DECRYPTION_FAILED);\n goto err;\n }\n padding_len = decrypt_len - SSL_MAX_MASTER_KEY_LENGTH;\n decrypt_good = constant_time_eq_int_8(rsa_decrypt[0], 0) &\n constant_time_eq_int_8(rsa_decrypt[1], 2);\n for (j = 2; j < padding_len - 1; j++) {\n decrypt_good &= ~constant_time_is_zero_8(rsa_decrypt[j]);\n }\n decrypt_good &= constant_time_is_zero_8(rsa_decrypt[padding_len - 1]);\n version_good =\n constant_time_eq_8(rsa_decrypt[padding_len],\n (unsigned)(s->client_version >> 8));\n version_good &=\n constant_time_eq_8(rsa_decrypt[padding_len + 1],\n (unsigned)(s->client_version & 0xff));\n if (s->options & SSL_OP_TLS_ROLLBACK_BUG) {\n unsigned char workaround_good;\n workaround_good = constant_time_eq_8(rsa_decrypt[padding_len],\n (unsigned)(s->version >> 8));\n workaround_good &=\n constant_time_eq_8(rsa_decrypt[padding_len + 1],\n (unsigned)(s->version & 0xff));\n version_good |= workaround_good;\n }\n decrypt_good &= version_good;\n for (j = 0; j < sizeof(rand_premaster_secret); j++) {\n rsa_decrypt[padding_len + j] =\n constant_time_select_8(decrypt_good,\n rsa_decrypt[padding_len + j],\n rand_premaster_secret[j]);\n }\n if (!ssl_generate_master_secret(s, rsa_decrypt + padding_len,\n sizeof(rand_premaster_secret), 0)) {\n *al = SSL_AD_INTERNAL_ERROR;\n SSLerr(SSL_F_TLS_PROCESS_CKE_RSA, ERR_R_INTERNAL_ERROR);\n goto err;\n }\n ret = 1;\n err:\n OPENSSL_free(rsa_decrypt);\n return ret;\n#else\n *al = SSL_AD_INTERNAL_ERROR;\n SSLerr(SSL_F_TLS_PROCESS_CKE_RSA, ERR_R_INTERNAL_ERROR);\n return 0;\n#endif\n}', 'int RSA_size(const RSA *r)\n{\n return (BN_num_bytes(r->n));\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}', 'void *CRYPTO_malloc(size_t num, const char *file, int line)\n{\n void *ret = NULL;\n if (malloc_impl != NULL && malloc_impl != CRYPTO_malloc)\n return malloc_impl(num, file, line);\n if (num <= 0)\n return NULL;\n allow_customize = 0;\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n if (call_malloc_debug) {\n CRYPTO_mem_debug_malloc(NULL, num, 0, file, line);\n ret = malloc(num);\n CRYPTO_mem_debug_malloc(ret, num, 1, file, line);\n } else {\n ret = malloc(num);\n }\n#else\n osslargused(file); osslargused(line);\n ret = malloc(num);\n#endif\n return ret;\n}']
255
0
https://github.com/openssl/openssl/blob/79a578b90244b890c8a6a8fc26c03943da71c054/apps/ca.c/#L1692
static int do_body(X509 **xret, EVP_PKEY *pkey, X509 *x509, const EVP_MD *dgst, STACK_OF(OPENSSL_STRING) *sigopts, STACK_OF(CONF_VALUE) *policy, CA_DB *db, BIGNUM *serial, char *subj, unsigned long chtype, int multirdn, int email_dn, char *startdate, char *enddate, long days, int batch, int verbose, X509_REQ *req, char *ext_sect, CONF *lconf, unsigned long certopt, unsigned long nameopt, int default_op, int ext_copy, int selfsign) { X509_NAME *name = NULL, *CAname = NULL, *subject = NULL, *dn_subject = NULL; ASN1_UTCTIME *tm, *tmptm; ASN1_STRING *str, *str2; ASN1_OBJECT *obj; X509 *ret = NULL; X509_CINF *ci; X509_NAME_ENTRY *ne; X509_NAME_ENTRY *tne, *push; EVP_PKEY *pktmp; int ok = -1, i, j, last, nid; const char *p; CONF_VALUE *cv; OPENSSL_STRING row[DB_NUMBER]; OPENSSL_STRING *irow = NULL; OPENSSL_STRING *rrow = NULL; char buf[25]; tmptm = ASN1_UTCTIME_new(); if (tmptm == NULL) { BIO_printf(bio_err, "malloc error\n"); return (0); } for (i = 0; i < DB_NUMBER; i++) row[i] = NULL; if (subj) { X509_NAME *n = parse_name(subj, chtype, multirdn); if (!n) { ERR_print_errors(bio_err); goto err; } X509_REQ_set_subject_name(req, n); req->req_info->enc.modified = 1; X509_NAME_free(n); } if (default_op) BIO_printf(bio_err, "The Subject's Distinguished Name is as follows\n"); name = X509_REQ_get_subject_name(req); for (i = 0; i < X509_NAME_entry_count(name); i++) { ne = X509_NAME_get_entry(name, i); str = X509_NAME_ENTRY_get_data(ne); obj = X509_NAME_ENTRY_get_object(ne); if (msie_hack) { nid = OBJ_obj2nid(X509_NAME_ENTRY_get_object(ne)); if (str->type == V_ASN1_UNIVERSALSTRING) ASN1_UNIVERSALSTRING_to_string(str); if ((str->type == V_ASN1_IA5STRING) && (nid != NID_pkcs9_emailAddress)) str->type = V_ASN1_T61STRING; if ((nid == NID_pkcs9_emailAddress) && (str->type == V_ASN1_PRINTABLESTRING)) str->type = V_ASN1_IA5STRING; } if ((OBJ_obj2nid(obj) == NID_pkcs9_emailAddress) && (!email_dn)) continue; if ((OBJ_obj2nid(obj) == NID_pkcs9_emailAddress) && (str->type != V_ASN1_IA5STRING)) { BIO_printf(bio_err, "\nemailAddress type needs to be of type IA5STRING\n"); goto err; } if ((str->type != V_ASN1_BMPSTRING) && (str->type != V_ASN1_UTF8STRING)) { j = ASN1_PRINTABLE_type(str->data, str->length); if (((j == V_ASN1_T61STRING) && (str->type != V_ASN1_T61STRING)) || ((j == V_ASN1_IA5STRING) && (str->type == V_ASN1_PRINTABLESTRING))) { BIO_printf(bio_err, "\nThe string contains characters that are illegal for the ASN.1 type\n"); goto err; } } if (default_op) old_entry_print(bio_err, obj, str); } if ((subject = X509_NAME_new()) == NULL) { BIO_printf(bio_err, "Memory allocation failure\n"); goto err; } if (selfsign) CAname = X509_NAME_dup(name); else CAname = X509_NAME_dup(x509->cert_info->subject); if (CAname == NULL) goto err; str = str2 = NULL; for (i = 0; i < sk_CONF_VALUE_num(policy); i++) { cv = sk_CONF_VALUE_value(policy, i); if ((j = OBJ_txt2nid(cv->name)) == NID_undef) { BIO_printf(bio_err, "%s:unknown object type in 'policy' configuration\n", cv->name); goto err; } obj = OBJ_nid2obj(j); last = -1; for (;;) { j = X509_NAME_get_index_by_OBJ(name, obj, last); if (j < 0) { if (last != -1) break; tne = NULL; } else { tne = X509_NAME_get_entry(name, j); } last = j; push = NULL; if (strcmp(cv->value, "optional") == 0) { if (tne != NULL) push = tne; } else if (strcmp(cv->value, "supplied") == 0) { if (tne == NULL) { BIO_printf(bio_err, "The %s field needed to be supplied and was missing\n", cv->name); goto err; } else push = tne; } else if (strcmp(cv->value, "match") == 0) { int last2; if (tne == NULL) { BIO_printf(bio_err, "The mandatory %s field was missing\n", cv->name); goto err; } last2 = -1; again2: j = X509_NAME_get_index_by_OBJ(CAname, obj, last2); if ((j < 0) && (last2 == -1)) { BIO_printf(bio_err, "The %s field does not exist in the CA certificate,\nthe 'policy' is misconfigured\n", cv->name); goto err; } if (j >= 0) { push = X509_NAME_get_entry(CAname, j); str = X509_NAME_ENTRY_get_data(tne); str2 = X509_NAME_ENTRY_get_data(push); last2 = j; if (ASN1_STRING_cmp(str, str2) != 0) goto again2; } if (j < 0) { BIO_printf(bio_err, "The %s field needed to be the same in the\nCA certificate (%s) and the request (%s)\n", cv->name, ((str2 == NULL) ? "NULL" : (char *)str2->data), ((str == NULL) ? "NULL" : (char *)str->data)); goto err; } } else { BIO_printf(bio_err, "%s:invalid type in 'policy' configuration\n", cv->value); goto err; } if (push != NULL) { if (!X509_NAME_add_entry(subject, push, -1, 0)) { if (push != NULL) X509_NAME_ENTRY_free(push); BIO_printf(bio_err, "Memory allocation failure\n"); goto err; } } if (j < 0) break; } } if (preserve) { X509_NAME_free(subject); subject = X509_NAME_dup(name); if (subject == NULL) goto err; } if (verbose) BIO_printf(bio_err, "The subject name appears to be ok, checking data base for clashes\n"); if (email_dn) dn_subject = subject; else { X509_NAME_ENTRY *tmpne; if (!(dn_subject = X509_NAME_dup(subject))) { BIO_printf(bio_err, "Memory allocation failure\n"); goto err; } while ((i = X509_NAME_get_index_by_NID(dn_subject, NID_pkcs9_emailAddress, -1)) >= 0) { tmpne = X509_NAME_get_entry(dn_subject, i); X509_NAME_delete_entry(dn_subject, i); X509_NAME_ENTRY_free(tmpne); } } if (BN_is_zero(serial)) row[DB_serial] = BUF_strdup("00"); else row[DB_serial] = BN_bn2hex(serial); if (row[DB_serial] == NULL) { BIO_printf(bio_err, "Memory allocation failure\n"); goto err; } if (db->attributes.unique_subject) { OPENSSL_STRING *crow = row; rrow = TXT_DB_get_by_index(db->db, DB_name, crow); if (rrow != NULL) { BIO_printf(bio_err, "ERROR:There is already a certificate for %s\n", row[DB_name]); } } if (rrow == NULL) { rrow = TXT_DB_get_by_index(db->db, DB_serial, row); if (rrow != NULL) { BIO_printf(bio_err, "ERROR:Serial number %s has already been issued,\n", row[DB_serial]); BIO_printf(bio_err, " check the database/serial_file for corruption\n"); } } if (rrow != NULL) { BIO_printf(bio_err, "The matching entry has the following details\n"); if (rrow[DB_type][0] == 'E') p = "Expired"; else if (rrow[DB_type][0] == 'R') p = "Revoked"; else if (rrow[DB_type][0] == 'V') p = "Valid"; else p = "\ninvalid type, Data base error\n"; BIO_printf(bio_err, "Type :%s\n", p);; if (rrow[DB_type][0] == 'R') { p = rrow[DB_exp_date]; if (p == NULL) p = "undef"; BIO_printf(bio_err, "Was revoked on:%s\n", p); } p = rrow[DB_exp_date]; if (p == NULL) p = "undef"; BIO_printf(bio_err, "Expires on :%s\n", p); p = rrow[DB_serial]; if (p == NULL) p = "undef"; BIO_printf(bio_err, "Serial Number :%s\n", p); p = rrow[DB_file]; if (p == NULL) p = "undef"; BIO_printf(bio_err, "File name :%s\n", p); p = rrow[DB_name]; if (p == NULL) p = "undef"; BIO_printf(bio_err, "Subject Name :%s\n", p); ok = -1; goto err; } if (verbose) BIO_printf(bio_err, "Everything appears to be ok, creating and signing the certificate\n"); if ((ret = X509_new()) == NULL) goto err; ci = ret->cert_info; #ifdef X509_V3 if (!X509_set_version(ret, 2)) goto err; #endif if (BN_to_ASN1_INTEGER(serial, ci->serialNumber) == NULL) goto err; if (selfsign) { if (!X509_set_issuer_name(ret, subject)) goto err; } else { if (!X509_set_issuer_name(ret, X509_get_subject_name(x509))) goto err; } if (strcmp(startdate, "today") == 0) X509_gmtime_adj(X509_get_notBefore(ret), 0); else ASN1_TIME_set_string(X509_get_notBefore(ret), startdate); if (enddate == NULL) X509_time_adj_ex(X509_get_notAfter(ret), days, 0, NULL); else { int tdays; ASN1_TIME_set_string(X509_get_notAfter(ret), enddate); ASN1_TIME_diff(&tdays, NULL, NULL, X509_get_notAfter(ret)); days = tdays; } if (!X509_set_subject_name(ret, subject)) goto err; pktmp = X509_REQ_get_pubkey(req); i = X509_set_pubkey(ret, pktmp); EVP_PKEY_free(pktmp); if (!i) goto err; if (ext_sect) { X509V3_CTX ctx; if (ci->version == NULL) if ((ci->version = ASN1_INTEGER_new()) == NULL) goto err; ASN1_INTEGER_set(ci->version, 2); if (ci->extensions != NULL) sk_X509_EXTENSION_pop_free(ci->extensions, X509_EXTENSION_free); ci->extensions = NULL; if (selfsign) X509V3_set_ctx(&ctx, ret, ret, req, NULL, 0); else X509V3_set_ctx(&ctx, x509, ret, req, NULL, 0); if (extconf) { if (verbose) BIO_printf(bio_err, "Extra configuration file found\n"); X509V3_set_nconf(&ctx, extconf); if (!X509V3_EXT_add_nconf(extconf, &ctx, ext_sect, ret)) { BIO_printf(bio_err, "ERROR: adding extensions in section %s\n", ext_sect); ERR_print_errors(bio_err); goto err; } if (verbose) BIO_printf(bio_err, "Successfully added extensions from file.\n"); } else if (ext_sect) { X509V3_set_nconf(&ctx, lconf); if (!X509V3_EXT_add_nconf(lconf, &ctx, ext_sect, ret)) { BIO_printf(bio_err, "ERROR: adding extensions in section %s\n", ext_sect); ERR_print_errors(bio_err); goto err; } if (verbose) BIO_printf(bio_err, "Successfully added extensions from config\n"); } } if (!copy_extensions(ret, req, ext_copy)) { BIO_printf(bio_err, "ERROR: adding extensions from request\n"); ERR_print_errors(bio_err); goto err; } if (email_dn == 0) { if (!X509_set_subject_name(ret, dn_subject)) goto err; } if (!default_op) { BIO_printf(bio_err, "Certificate Details:\n"); certopt |= X509_FLAG_NO_SIGDUMP | X509_FLAG_NO_SIGNAME; X509_print_ex(bio_err, ret, nameopt, certopt); } BIO_printf(bio_err, "Certificate is to be certified until "); ASN1_TIME_print(bio_err, X509_get_notAfter(ret)); if (days) BIO_printf(bio_err, " (%ld days)", days); BIO_printf(bio_err, "\n"); if (!batch) { BIO_printf(bio_err, "Sign the certificate? [y/n]:"); (void)BIO_flush(bio_err); buf[0] = '\0'; if (!fgets(buf, sizeof(buf) - 1, stdin)) { BIO_printf(bio_err, "CERTIFICATE WILL NOT BE CERTIFIED: I/O error\n"); ok = 0; goto err; } if (!((buf[0] == 'y') || (buf[0] == 'Y'))) { BIO_printf(bio_err, "CERTIFICATE WILL NOT BE CERTIFIED\n"); ok = 0; goto err; } } pktmp = X509_get_pubkey(ret); if (EVP_PKEY_missing_parameters(pktmp) && !EVP_PKEY_missing_parameters(pkey)) EVP_PKEY_copy_parameters(pktmp, pkey); EVP_PKEY_free(pktmp); if (!do_X509_sign(bio_err, ret, pkey, dgst, sigopts)) goto err; row[DB_type] = (char *)OPENSSL_malloc(2); tm = X509_get_notAfter(ret); row[DB_exp_date] = (char *)OPENSSL_malloc(tm->length + 1); memcpy(row[DB_exp_date], tm->data, tm->length); row[DB_exp_date][tm->length] = '\0'; row[DB_rev_date] = NULL; row[DB_file] = (char *)OPENSSL_malloc(8); row[DB_name] = X509_NAME_oneline(X509_get_subject_name(ret), NULL, 0); if ((row[DB_type] == NULL) || (row[DB_exp_date] == NULL) || (row[DB_file] == NULL) || (row[DB_name] == NULL)) { BIO_printf(bio_err, "Memory allocation failure\n"); goto err; } BUF_strlcpy(row[DB_file], "unknown", 8); row[DB_type][0] = 'V'; row[DB_type][1] = '\0'; if ((irow = (char **)OPENSSL_malloc(sizeof(char *) * (DB_NUMBER + 1))) == NULL) { BIO_printf(bio_err, "Memory allocation failure\n"); goto err; } for (i = 0; i < DB_NUMBER; i++) { irow[i] = row[i]; row[i] = NULL; } irow[DB_NUMBER] = NULL; if (!TXT_DB_insert(db->db, irow)) { BIO_printf(bio_err, "failed to update database\n"); BIO_printf(bio_err, "TXT_DB error number %ld\n", db->db->error); goto err; } ok = 1; err: for (i = 0; i < DB_NUMBER; i++) if (row[i] != NULL) OPENSSL_free(row[i]); if (CAname != NULL) X509_NAME_free(CAname); if (subject != NULL) X509_NAME_free(subject); if ((dn_subject != NULL) && !email_dn) X509_NAME_free(dn_subject); if (tmptm != NULL) ASN1_UTCTIME_free(tmptm); if (ok <= 0) { if (ret != NULL) X509_free(ret); ret = NULL; } else *xret = ret; return (ok); }
['static int do_body(X509 **xret, EVP_PKEY *pkey, X509 *x509,\n const EVP_MD *dgst, STACK_OF(OPENSSL_STRING) *sigopts,\n STACK_OF(CONF_VALUE) *policy, CA_DB *db, BIGNUM *serial,\n char *subj, unsigned long chtype, int multirdn,\n int email_dn, char *startdate, char *enddate, long days,\n int batch, int verbose, X509_REQ *req, char *ext_sect,\n CONF *lconf, unsigned long certopt, unsigned long nameopt,\n int default_op, int ext_copy, int selfsign)\n{\n X509_NAME *name = NULL, *CAname = NULL, *subject = NULL, *dn_subject =\n NULL;\n ASN1_UTCTIME *tm, *tmptm;\n ASN1_STRING *str, *str2;\n ASN1_OBJECT *obj;\n X509 *ret = NULL;\n X509_CINF *ci;\n X509_NAME_ENTRY *ne;\n X509_NAME_ENTRY *tne, *push;\n EVP_PKEY *pktmp;\n int ok = -1, i, j, last, nid;\n const char *p;\n CONF_VALUE *cv;\n OPENSSL_STRING row[DB_NUMBER];\n OPENSSL_STRING *irow = NULL;\n OPENSSL_STRING *rrow = NULL;\n char buf[25];\n tmptm = ASN1_UTCTIME_new();\n if (tmptm == NULL) {\n BIO_printf(bio_err, "malloc error\\n");\n return (0);\n }\n for (i = 0; i < DB_NUMBER; i++)\n row[i] = NULL;\n if (subj) {\n X509_NAME *n = parse_name(subj, chtype, multirdn);\n if (!n) {\n ERR_print_errors(bio_err);\n goto err;\n }\n X509_REQ_set_subject_name(req, n);\n req->req_info->enc.modified = 1;\n X509_NAME_free(n);\n }\n if (default_op)\n BIO_printf(bio_err,\n "The Subject\'s Distinguished Name is as follows\\n");\n name = X509_REQ_get_subject_name(req);\n for (i = 0; i < X509_NAME_entry_count(name); i++) {\n ne = X509_NAME_get_entry(name, i);\n str = X509_NAME_ENTRY_get_data(ne);\n obj = X509_NAME_ENTRY_get_object(ne);\n if (msie_hack) {\n nid = OBJ_obj2nid(X509_NAME_ENTRY_get_object(ne));\n if (str->type == V_ASN1_UNIVERSALSTRING)\n ASN1_UNIVERSALSTRING_to_string(str);\n if ((str->type == V_ASN1_IA5STRING) &&\n (nid != NID_pkcs9_emailAddress))\n str->type = V_ASN1_T61STRING;\n if ((nid == NID_pkcs9_emailAddress) &&\n (str->type == V_ASN1_PRINTABLESTRING))\n str->type = V_ASN1_IA5STRING;\n }\n if ((OBJ_obj2nid(obj) == NID_pkcs9_emailAddress) && (!email_dn))\n continue;\n if ((OBJ_obj2nid(obj) == NID_pkcs9_emailAddress) &&\n (str->type != V_ASN1_IA5STRING)) {\n BIO_printf(bio_err,\n "\\nemailAddress type needs to be of type IA5STRING\\n");\n goto err;\n }\n if ((str->type != V_ASN1_BMPSTRING)\n && (str->type != V_ASN1_UTF8STRING)) {\n j = ASN1_PRINTABLE_type(str->data, str->length);\n if (((j == V_ASN1_T61STRING) &&\n (str->type != V_ASN1_T61STRING)) ||\n ((j == V_ASN1_IA5STRING) &&\n (str->type == V_ASN1_PRINTABLESTRING))) {\n BIO_printf(bio_err,\n "\\nThe string contains characters that are illegal for the ASN.1 type\\n");\n goto err;\n }\n }\n if (default_op)\n old_entry_print(bio_err, obj, str);\n }\n if ((subject = X509_NAME_new()) == NULL) {\n BIO_printf(bio_err, "Memory allocation failure\\n");\n goto err;\n }\n if (selfsign)\n CAname = X509_NAME_dup(name);\n else\n CAname = X509_NAME_dup(x509->cert_info->subject);\n if (CAname == NULL)\n goto err;\n str = str2 = NULL;\n for (i = 0; i < sk_CONF_VALUE_num(policy); i++) {\n cv = sk_CONF_VALUE_value(policy, i);\n if ((j = OBJ_txt2nid(cv->name)) == NID_undef) {\n BIO_printf(bio_err,\n "%s:unknown object type in \'policy\' configuration\\n",\n cv->name);\n goto err;\n }\n obj = OBJ_nid2obj(j);\n last = -1;\n for (;;) {\n j = X509_NAME_get_index_by_OBJ(name, obj, last);\n if (j < 0) {\n if (last != -1)\n break;\n tne = NULL;\n } else {\n tne = X509_NAME_get_entry(name, j);\n }\n last = j;\n push = NULL;\n if (strcmp(cv->value, "optional") == 0) {\n if (tne != NULL)\n push = tne;\n } else if (strcmp(cv->value, "supplied") == 0) {\n if (tne == NULL) {\n BIO_printf(bio_err,\n "The %s field needed to be supplied and was missing\\n",\n cv->name);\n goto err;\n } else\n push = tne;\n } else if (strcmp(cv->value, "match") == 0) {\n int last2;\n if (tne == NULL) {\n BIO_printf(bio_err,\n "The mandatory %s field was missing\\n",\n cv->name);\n goto err;\n }\n last2 = -1;\n again2:\n j = X509_NAME_get_index_by_OBJ(CAname, obj, last2);\n if ((j < 0) && (last2 == -1)) {\n BIO_printf(bio_err,\n "The %s field does not exist in the CA certificate,\\nthe \'policy\' is misconfigured\\n",\n cv->name);\n goto err;\n }\n if (j >= 0) {\n push = X509_NAME_get_entry(CAname, j);\n str = X509_NAME_ENTRY_get_data(tne);\n str2 = X509_NAME_ENTRY_get_data(push);\n last2 = j;\n if (ASN1_STRING_cmp(str, str2) != 0)\n goto again2;\n }\n if (j < 0) {\n BIO_printf(bio_err,\n "The %s field needed to be the same in the\\nCA certificate (%s) and the request (%s)\\n",\n cv->name,\n ((str2 == NULL) ? "NULL" : (char *)str2->data),\n ((str == NULL) ? "NULL" : (char *)str->data));\n goto err;\n }\n } else {\n BIO_printf(bio_err,\n "%s:invalid type in \'policy\' configuration\\n",\n cv->value);\n goto err;\n }\n if (push != NULL) {\n if (!X509_NAME_add_entry(subject, push, -1, 0)) {\n if (push != NULL)\n X509_NAME_ENTRY_free(push);\n BIO_printf(bio_err, "Memory allocation failure\\n");\n goto err;\n }\n }\n if (j < 0)\n break;\n }\n }\n if (preserve) {\n X509_NAME_free(subject);\n subject = X509_NAME_dup(name);\n if (subject == NULL)\n goto err;\n }\n if (verbose)\n BIO_printf(bio_err,\n "The subject name appears to be ok, checking data base for clashes\\n");\n if (email_dn)\n dn_subject = subject;\n else {\n X509_NAME_ENTRY *tmpne;\n if (!(dn_subject = X509_NAME_dup(subject))) {\n BIO_printf(bio_err, "Memory allocation failure\\n");\n goto err;\n }\n while ((i = X509_NAME_get_index_by_NID(dn_subject,\n NID_pkcs9_emailAddress,\n -1)) >= 0) {\n tmpne = X509_NAME_get_entry(dn_subject, i);\n X509_NAME_delete_entry(dn_subject, i);\n X509_NAME_ENTRY_free(tmpne);\n }\n }\n if (BN_is_zero(serial))\n row[DB_serial] = BUF_strdup("00");\n else\n row[DB_serial] = BN_bn2hex(serial);\n if (row[DB_serial] == NULL) {\n BIO_printf(bio_err, "Memory allocation failure\\n");\n goto err;\n }\n if (db->attributes.unique_subject) {\n OPENSSL_STRING *crow = row;\n rrow = TXT_DB_get_by_index(db->db, DB_name, crow);\n if (rrow != NULL) {\n BIO_printf(bio_err,\n "ERROR:There is already a certificate for %s\\n",\n row[DB_name]);\n }\n }\n if (rrow == NULL) {\n rrow = TXT_DB_get_by_index(db->db, DB_serial, row);\n if (rrow != NULL) {\n BIO_printf(bio_err,\n "ERROR:Serial number %s has already been issued,\\n",\n row[DB_serial]);\n BIO_printf(bio_err,\n " check the database/serial_file for corruption\\n");\n }\n }\n if (rrow != NULL) {\n BIO_printf(bio_err, "The matching entry has the following details\\n");\n if (rrow[DB_type][0] == \'E\')\n p = "Expired";\n else if (rrow[DB_type][0] == \'R\')\n p = "Revoked";\n else if (rrow[DB_type][0] == \'V\')\n p = "Valid";\n else\n p = "\\ninvalid type, Data base error\\n";\n BIO_printf(bio_err, "Type :%s\\n", p);;\n if (rrow[DB_type][0] == \'R\') {\n p = rrow[DB_exp_date];\n if (p == NULL)\n p = "undef";\n BIO_printf(bio_err, "Was revoked on:%s\\n", p);\n }\n p = rrow[DB_exp_date];\n if (p == NULL)\n p = "undef";\n BIO_printf(bio_err, "Expires on :%s\\n", p);\n p = rrow[DB_serial];\n if (p == NULL)\n p = "undef";\n BIO_printf(bio_err, "Serial Number :%s\\n", p);\n p = rrow[DB_file];\n if (p == NULL)\n p = "undef";\n BIO_printf(bio_err, "File name :%s\\n", p);\n p = rrow[DB_name];\n if (p == NULL)\n p = "undef";\n BIO_printf(bio_err, "Subject Name :%s\\n", p);\n ok = -1;\n goto err;\n }\n if (verbose)\n BIO_printf(bio_err,\n "Everything appears to be ok, creating and signing the certificate\\n");\n if ((ret = X509_new()) == NULL)\n goto err;\n ci = ret->cert_info;\n#ifdef X509_V3\n if (!X509_set_version(ret, 2))\n goto err;\n#endif\n if (BN_to_ASN1_INTEGER(serial, ci->serialNumber) == NULL)\n goto err;\n if (selfsign) {\n if (!X509_set_issuer_name(ret, subject))\n goto err;\n } else {\n if (!X509_set_issuer_name(ret, X509_get_subject_name(x509)))\n goto err;\n }\n if (strcmp(startdate, "today") == 0)\n X509_gmtime_adj(X509_get_notBefore(ret), 0);\n else\n ASN1_TIME_set_string(X509_get_notBefore(ret), startdate);\n if (enddate == NULL)\n X509_time_adj_ex(X509_get_notAfter(ret), days, 0, NULL);\n else {\n int tdays;\n ASN1_TIME_set_string(X509_get_notAfter(ret), enddate);\n ASN1_TIME_diff(&tdays, NULL, NULL, X509_get_notAfter(ret));\n days = tdays;\n }\n if (!X509_set_subject_name(ret, subject))\n goto err;\n pktmp = X509_REQ_get_pubkey(req);\n i = X509_set_pubkey(ret, pktmp);\n EVP_PKEY_free(pktmp);\n if (!i)\n goto err;\n if (ext_sect) {\n X509V3_CTX ctx;\n if (ci->version == NULL)\n if ((ci->version = ASN1_INTEGER_new()) == NULL)\n goto err;\n ASN1_INTEGER_set(ci->version, 2);\n if (ci->extensions != NULL)\n sk_X509_EXTENSION_pop_free(ci->extensions, X509_EXTENSION_free);\n ci->extensions = NULL;\n if (selfsign)\n X509V3_set_ctx(&ctx, ret, ret, req, NULL, 0);\n else\n X509V3_set_ctx(&ctx, x509, ret, req, NULL, 0);\n if (extconf) {\n if (verbose)\n BIO_printf(bio_err, "Extra configuration file found\\n");\n X509V3_set_nconf(&ctx, extconf);\n if (!X509V3_EXT_add_nconf(extconf, &ctx, ext_sect, ret)) {\n BIO_printf(bio_err,\n "ERROR: adding extensions in section %s\\n",\n ext_sect);\n ERR_print_errors(bio_err);\n goto err;\n }\n if (verbose)\n BIO_printf(bio_err,\n "Successfully added extensions from file.\\n");\n } else if (ext_sect) {\n X509V3_set_nconf(&ctx, lconf);\n if (!X509V3_EXT_add_nconf(lconf, &ctx, ext_sect, ret)) {\n BIO_printf(bio_err,\n "ERROR: adding extensions in section %s\\n",\n ext_sect);\n ERR_print_errors(bio_err);\n goto err;\n }\n if (verbose)\n BIO_printf(bio_err,\n "Successfully added extensions from config\\n");\n }\n }\n if (!copy_extensions(ret, req, ext_copy)) {\n BIO_printf(bio_err, "ERROR: adding extensions from request\\n");\n ERR_print_errors(bio_err);\n goto err;\n }\n if (email_dn == 0) {\n if (!X509_set_subject_name(ret, dn_subject))\n goto err;\n }\n if (!default_op) {\n BIO_printf(bio_err, "Certificate Details:\\n");\n certopt |= X509_FLAG_NO_SIGDUMP | X509_FLAG_NO_SIGNAME;\n X509_print_ex(bio_err, ret, nameopt, certopt);\n }\n BIO_printf(bio_err, "Certificate is to be certified until ");\n ASN1_TIME_print(bio_err, X509_get_notAfter(ret));\n if (days)\n BIO_printf(bio_err, " (%ld days)", days);\n BIO_printf(bio_err, "\\n");\n if (!batch) {\n BIO_printf(bio_err, "Sign the certificate? [y/n]:");\n (void)BIO_flush(bio_err);\n buf[0] = \'\\0\';\n if (!fgets(buf, sizeof(buf) - 1, stdin)) {\n BIO_printf(bio_err,\n "CERTIFICATE WILL NOT BE CERTIFIED: I/O error\\n");\n ok = 0;\n goto err;\n }\n if (!((buf[0] == \'y\') || (buf[0] == \'Y\'))) {\n BIO_printf(bio_err, "CERTIFICATE WILL NOT BE CERTIFIED\\n");\n ok = 0;\n goto err;\n }\n }\n pktmp = X509_get_pubkey(ret);\n if (EVP_PKEY_missing_parameters(pktmp) &&\n !EVP_PKEY_missing_parameters(pkey))\n EVP_PKEY_copy_parameters(pktmp, pkey);\n EVP_PKEY_free(pktmp);\n if (!do_X509_sign(bio_err, ret, pkey, dgst, sigopts))\n goto err;\n row[DB_type] = (char *)OPENSSL_malloc(2);\n tm = X509_get_notAfter(ret);\n row[DB_exp_date] = (char *)OPENSSL_malloc(tm->length + 1);\n memcpy(row[DB_exp_date], tm->data, tm->length);\n row[DB_exp_date][tm->length] = \'\\0\';\n row[DB_rev_date] = NULL;\n row[DB_file] = (char *)OPENSSL_malloc(8);\n row[DB_name] = X509_NAME_oneline(X509_get_subject_name(ret), NULL, 0);\n if ((row[DB_type] == NULL) || (row[DB_exp_date] == NULL) ||\n (row[DB_file] == NULL) || (row[DB_name] == NULL)) {\n BIO_printf(bio_err, "Memory allocation failure\\n");\n goto err;\n }\n BUF_strlcpy(row[DB_file], "unknown", 8);\n row[DB_type][0] = \'V\';\n row[DB_type][1] = \'\\0\';\n if ((irow =\n (char **)OPENSSL_malloc(sizeof(char *) * (DB_NUMBER + 1))) == NULL) {\n BIO_printf(bio_err, "Memory allocation failure\\n");\n goto err;\n }\n for (i = 0; i < DB_NUMBER; i++) {\n irow[i] = row[i];\n row[i] = NULL;\n }\n irow[DB_NUMBER] = NULL;\n if (!TXT_DB_insert(db->db, irow)) {\n BIO_printf(bio_err, "failed to update database\\n");\n BIO_printf(bio_err, "TXT_DB error number %ld\\n", db->db->error);\n goto err;\n }\n ok = 1;\n err:\n for (i = 0; i < DB_NUMBER; i++)\n if (row[i] != NULL)\n OPENSSL_free(row[i]);\n if (CAname != NULL)\n X509_NAME_free(CAname);\n if (subject != NULL)\n X509_NAME_free(subject);\n if ((dn_subject != NULL) && !email_dn)\n X509_NAME_free(dn_subject);\n if (tmptm != NULL)\n ASN1_UTCTIME_free(tmptm);\n if (ok <= 0) {\n if (ret != NULL)\n X509_free(ret);\n ret = NULL;\n } else\n *xret = ret;\n return (ok);\n}', 'IMPLEMENT_ASN1_STRING_FUNCTIONS(ASN1_UTCTIME)', 'int X509_REQ_set_subject_name(X509_REQ *x, X509_NAME *name)\n{\n if ((x == NULL) || (x->req_info == NULL))\n return (0);\n return (X509_NAME_set(&x->req_info->subject, name));\n}', 'int X509_NAME_set(X509_NAME **xn, X509_NAME *name)\n{\n X509_NAME *in;\n if (!xn || !name)\n return (0);\n if (*xn != name) {\n in = X509_NAME_dup(name);\n if (in != NULL) {\n X509_NAME_free(*xn);\n *xn = in;\n }\n }\n return (*xn != NULL);\n}', 'IMPLEMENT_ASN1_FUNCTIONS(X509_NAME)', 'void ASN1_item_free(ASN1_VALUE *val, const ASN1_ITEM *it)\n{\n ASN1_item_ex_free(&val, it);\n}', 'int BIO_printf(BIO *bio, const char *format, ...)\n{\n va_list args;\n int ret;\n va_start(args, format);\n ret = BIO_vprintf(bio, format, args);\n va_end(args);\n return (ret);\n}', 'int X509_NAME_entry_count(X509_NAME *name)\n{\n if (name == NULL)\n return (0);\n return (sk_X509_NAME_ENTRY_num(name->entries));\n}', 'int sk_num(const _STACK *st)\n{\n if (st == NULL)\n return -1;\n return st->num;\n}', 'X509_NAME_ENTRY *X509_NAME_get_entry(X509_NAME *name, int loc)\n{\n if (name == NULL || sk_X509_NAME_ENTRY_num(name->entries) <= loc\n || loc < 0)\n return (NULL);\n else\n return (sk_X509_NAME_ENTRY_value(name->entries, loc));\n}', 'void *sk_value(const _STACK *st, int i)\n{\n if (!st || (i < 0) || (i >= st->num))\n return NULL;\n return st->data[i];\n}', 'ASN1_STRING *X509_NAME_ENTRY_get_data(X509_NAME_ENTRY *ne)\n{\n if (ne == NULL)\n return (NULL);\n return (ne->value);\n}', 'ASN1_OBJECT *X509_NAME_ENTRY_get_object(X509_NAME_ENTRY *ne)\n{\n if (ne == NULL)\n return (NULL);\n return (ne->object);\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 (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}']
256
0
https://github.com/openssl/openssl/blob/38d1b3cc0271008b8bd130a2c4b442775b028a08/crypto/bn/bn_shift.c/#L110
int BN_lshift(BIGNUM *r, const BIGNUM *a, int n) { int i, nw, lb, rb; BN_ULONG *t, *f; BN_ULONG l; bn_check_top(r); bn_check_top(a); if (n < 0) { BNerr(BN_F_BN_LSHIFT, BN_R_INVALID_SHIFT); return 0; } 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_mod_lshift_quick(BIGNUM *r, const BIGNUM *a, int n, const BIGNUM *m)\n{\n if (r != a) {\n if (BN_copy(r, a) == NULL)\n return 0;\n }\n while (n > 0) {\n int max_shift;\n max_shift = BN_num_bits(m) - BN_num_bits(r);\n if (max_shift < 0) {\n BNerr(BN_F_BN_MOD_LSHIFT_QUICK, BN_R_INPUT_NOT_REDUCED);\n return 0;\n }\n if (max_shift > n)\n max_shift = n;\n if (max_shift) {\n if (!BN_lshift(r, r, max_shift))\n return 0;\n n -= max_shift;\n } else {\n if (!BN_lshift1(r, r))\n return 0;\n --n;\n }\n if (BN_cmp(r, m) >= 0) {\n if (!BN_sub(r, r, m))\n return 0;\n }\n }\n bn_check_top(r);\n return 1;\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}']
257
0
https://github.com/openssl/openssl/blob/f006217bb628d05a2d5b866ff252bd94e3477e1f/crypto/dsa/dsa_gen.c/#L659
int dsa_paramgen_check_g(DSA *dsa) { BN_CTX *ctx; BIGNUM *tmp; BN_MONT_CTX *mont = NULL; int rv = -1; ctx = BN_CTX_new(); if (ctx == NULL) return -1; BN_CTX_start(ctx); if (BN_cmp(dsa->g, BN_value_one()) <= 0) return 0; if (BN_cmp(dsa->g, dsa->p) >= 0) return 0; tmp = BN_CTX_get(ctx); if (!tmp) goto err; if ((mont = BN_MONT_CTX_new()) == NULL) goto err; if (!BN_MONT_CTX_set(mont, dsa->p, ctx)) goto err; if (!BN_mod_exp_mont(tmp, dsa->g, dsa->q, dsa->p, ctx, mont)) goto err; if (!BN_cmp(tmp, BN_value_one())) rv = 1; else rv = 0; err: BN_CTX_end(ctx); BN_MONT_CTX_free(mont); BN_CTX_free(ctx); return rv; }
['int dsa_paramgen_check_g(DSA *dsa)\n{\n BN_CTX *ctx;\n BIGNUM *tmp;\n BN_MONT_CTX *mont = NULL;\n int rv = -1;\n ctx = BN_CTX_new();\n if (ctx == NULL)\n return -1;\n BN_CTX_start(ctx);\n if (BN_cmp(dsa->g, BN_value_one()) <= 0)\n return 0;\n if (BN_cmp(dsa->g, dsa->p) >= 0)\n return 0;\n tmp = BN_CTX_get(ctx);\n if (!tmp)\n goto err;\n if ((mont = BN_MONT_CTX_new()) == NULL)\n goto err;\n if (!BN_MONT_CTX_set(mont, dsa->p, ctx))\n goto err;\n if (!BN_mod_exp_mont(tmp, dsa->g, dsa->q, dsa->p, ctx, mont))\n goto err;\n if (!BN_cmp(tmp, BN_value_one()))\n rv = 1;\n else\n rv = 0;\n err:\n BN_CTX_end(ctx);\n BN_MONT_CTX_free(mont);\n BN_CTX_free(ctx);\n return rv;\n}', 'BN_CTX *BN_CTX_new(void)\n{\n BN_CTX *ret;\n if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL) {\n BNerr(BN_F_BN_CTX_NEW, ERR_R_MALLOC_FAILURE);\n return NULL;\n }\n BN_POOL_init(&ret->pool);\n BN_STACK_init(&ret->stack);\n return ret;\n}', 'void *CRYPTO_zalloc(size_t num, const char *file, int line)\n{\n void *ret = CRYPTO_malloc(num, file, line);\n if (ret != NULL)\n memset(ret, 0, num);\n return ret;\n}', 'void *CRYPTO_malloc(size_t num, const char *file, int line)\n{\n void *ret = NULL;\n if (num <= 0)\n return NULL;\n allow_customize = 0;\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n if (call_malloc_debug) {\n CRYPTO_mem_debug_malloc(NULL, num, 0, file, line);\n ret = malloc(num);\n CRYPTO_mem_debug_malloc(ret, num, 1, file, line);\n } else {\n ret = malloc(num);\n }\n#else\n (void)file;\n (void)line;\n ret = malloc(num);\n#endif\n#ifndef OPENSSL_CPUID_OBJ\n if (ret && (num > 2048)) {\n extern unsigned char cleanse_ctr;\n ((unsigned char *)ret)[0] = cleanse_ctr;\n }\n#endif\n return ret;\n}', 'static void BN_POOL_init(BN_POOL *p)\n{\n p->head = p->current = p->tail = NULL;\n p->used = p->size = 0;\n}', 'static void BN_STACK_init(BN_STACK *st)\n{\n st->indexes = NULL;\n st->depth = st->size = 0;\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}', 'static int BN_STACK_push(BN_STACK *st, unsigned int idx)\n{\n if (st->depth == st->size) {\n unsigned int newsize =\n st->size ? (st->size * 3 / 2) : BN_CTX_START_FRAMES;\n unsigned int *newitems = OPENSSL_malloc(sizeof(*newitems) * newsize);\n if (newitems == NULL)\n return 0;\n if (st->depth)\n memcpy(newitems, st->indexes, sizeof(*newitems) * st->depth);\n OPENSSL_free(st->indexes);\n st->indexes = newitems;\n st->size = newsize;\n }\n st->indexes[(st->depth)++] = idx;\n return 1;\n}', 'void CRYPTO_free(void *str)\n{\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n if (call_malloc_debug) {\n CRYPTO_mem_debug_free(str, 0);\n free(str);\n CRYPTO_mem_debug_free(str, 1);\n } else {\n free(str);\n }\n#else\n free(str);\n#endif\n}', 'const BIGNUM *BN_value_one(void)\n{\n static const BN_ULONG data_one = 1L;\n static const BIGNUM const_one =\n { (BN_ULONG *)&data_one, 1, 1, 0, BN_FLG_STATIC_DATA };\n return (&const_one);\n}']
258
0
https://github.com/openssl/openssl/blob/6c2c3e9ba9146ef8c9b1fd2b660357b657706969/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\tif((mt < 0) && (*p == SSL3_MT_CLIENT_HELLO) &&\n\t\t\t\t\t(st1 == SSL3_ST_SR_CERT_A) &&\n\t\t\t\t\t(stn == SSL3_ST_SR_CERT_B))\n\t\t\t{\n\t\t\tssl3_init_finished_mac(s);\n\t\t\tssl3_finish_mac(s, p + s->init_num, i);\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}']
259
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)]; }
['static int test_div_recip()\n{\n BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL, *e = NULL;\n BN_RECP_CTX *recp = NULL;\n int st = 0, i;\n if (!TEST_ptr(a = BN_new())\n || !TEST_ptr(b = BN_new())\n || !TEST_ptr(c = BN_new())\n || !TEST_ptr(d = BN_new())\n || !TEST_ptr(e = BN_new())\n || !TEST_ptr(recp = BN_RECP_CTX_new()))\n goto err;\n for (i = 0; i < NUM0 + NUM1; i++) {\n if (i < NUM1) {\n BN_bntest_rand(a, 400, 0, 0);\n BN_copy(b, a);\n BN_lshift(a, a, i);\n BN_add_word(a, i);\n } else\n BN_bntest_rand(b, 50 + 3 * (i - NUM1), 0, 0);\n a->neg = rand_neg();\n b->neg = rand_neg();\n BN_RECP_CTX_set(recp, b, ctx);\n BN_div_recp(d, c, a, recp, ctx);\n BN_mul(e, d, b, ctx);\n BN_add(d, e, c);\n BN_sub(d, d, a);\n if (!TEST_BN_eq_zero(d))\n goto err;\n }\n st = 1;\nerr:\n BN_free(a);\n BN_free(b);\n BN_free(c);\n BN_free(d);\n BN_free(e);\n BN_RECP_CTX_free(recp);\n return st;\n}', 'int BN_div_recp(BIGNUM *dv, BIGNUM *rem, const BIGNUM *m,\n BN_RECP_CTX *recp, BN_CTX *ctx)\n{\n int i, j, ret = 0;\n BIGNUM *a, *b, *d, *r;\n BN_CTX_start(ctx);\n d = (dv != NULL) ? dv : BN_CTX_get(ctx);\n r = (rem != NULL) ? rem : BN_CTX_get(ctx);\n a = BN_CTX_get(ctx);\n b = BN_CTX_get(ctx);\n if (b == NULL)\n goto err;\n if (BN_ucmp(m, &(recp->N)) < 0) {\n BN_zero(d);\n if (!BN_copy(r, m)) {\n BN_CTX_end(ctx);\n return 0;\n }\n BN_CTX_end(ctx);\n return (1);\n }\n i = BN_num_bits(m);\n j = recp->num_bits << 1;\n if (j > i)\n i = j;\n if (i != recp->shift)\n recp->shift = BN_reciprocal(&(recp->Nr), &(recp->N), i, ctx);\n if (recp->shift == -1)\n goto err;\n if (!BN_rshift(a, m, recp->num_bits))\n goto err;\n if (!BN_mul(b, a, &(recp->Nr), ctx))\n goto err;\n if (!BN_rshift(d, b, i - recp->num_bits))\n goto err;\n d->neg = 0;\n if (!BN_mul(b, &(recp->N), d, ctx))\n goto err;\n if (!BN_usub(r, m, b))\n goto err;\n r->neg = 0;\n j = 0;\n while (BN_ucmp(r, &(recp->N)) >= 0) {\n if (j++ > 2) {\n BNerr(BN_F_BN_DIV_RECP, BN_R_BAD_RECIPROCAL);\n goto err;\n }\n if (!BN_usub(r, r, &(recp->N)))\n goto err;\n if (!BN_add_word(d, 1))\n goto err;\n }\n r->neg = BN_is_zero(r) ? 0 : m->neg;\n d->neg = m->neg ^ recp->N.neg;\n ret = 1;\n err:\n BN_CTX_end(ctx);\n bn_check_top(dv);\n bn_check_top(rem);\n return (ret);\n}', 'void BN_CTX_start(BN_CTX *ctx)\n{\n CTXDBG_ENTRY("BN_CTX_start", ctx);\n if (ctx->err_stack || ctx->too_many)\n ctx->err_stack++;\n else if (!BN_STACK_push(&ctx->stack, ctx->used)) {\n BNerr(BN_F_BN_CTX_START, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n ctx->err_stack++;\n }\n CTXDBG_EXIT(ctx);\n}', 'void BN_CTX_end(BN_CTX *ctx)\n{\n CTXDBG_ENTRY("BN_CTX_end", ctx);\n if (ctx->err_stack)\n ctx->err_stack--;\n else {\n unsigned int fp = BN_STACK_pop(&ctx->stack);\n if (fp < ctx->used)\n BN_POOL_release(&ctx->pool, ctx->used - fp);\n ctx->used = fp;\n ctx->too_many = 0;\n }\n CTXDBG_EXIT(ctx);\n}', 'int BN_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)\n{\n int ret = 0;\n int top, al, bl;\n BIGNUM *rr;\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n int i;\n#endif\n#ifdef BN_RECURSION\n BIGNUM *t = NULL;\n int j = 0, k;\n#endif\n bn_check_top(a);\n bn_check_top(b);\n bn_check_top(r);\n al = a->top;\n bl = b->top;\n if ((al == 0) || (bl == 0)) {\n BN_zero(r);\n return (1);\n }\n top = al + bl;\n BN_CTX_start(ctx);\n if ((r == a) || (r == b)) {\n if ((rr = BN_CTX_get(ctx)) == NULL)\n goto err;\n } else\n rr = r;\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n i = al - bl;\n#endif\n#ifdef BN_MUL_COMBA\n if (i == 0) {\n# if 0\n if (al == 4) {\n if (bn_wexpand(rr, 8) == NULL)\n goto err;\n rr->top = 8;\n bn_mul_comba4(rr->d, a->d, b->d);\n goto end;\n }\n# endif\n if (al == 8) {\n if (bn_wexpand(rr, 16) == NULL)\n goto err;\n rr->top = 16;\n bn_mul_comba8(rr->d, a->d, b->d);\n goto end;\n }\n }\n#endif\n#ifdef BN_RECURSION\n if ((al >= BN_MULL_SIZE_NORMAL) && (bl >= BN_MULL_SIZE_NORMAL)) {\n if (i >= -1 && i <= 1) {\n if (i >= 0) {\n j = BN_num_bits_word((BN_ULONG)al);\n }\n if (i == -1) {\n j = BN_num_bits_word((BN_ULONG)bl);\n }\n j = 1 << (j - 1);\n assert(j <= al || j <= bl);\n k = j + j;\n t = BN_CTX_get(ctx);\n if (t == NULL)\n goto err;\n if (al > j || bl > j) {\n if (bn_wexpand(t, k * 4) == NULL)\n goto err;\n if (bn_wexpand(rr, k * 4) == NULL)\n goto err;\n bn_mul_part_recursive(rr->d, a->d, b->d,\n j, al - j, bl - j, t->d);\n } else {\n if (bn_wexpand(t, k * 2) == NULL)\n goto err;\n if (bn_wexpand(rr, k * 2) == NULL)\n goto err;\n bn_mul_recursive(rr->d, a->d, b->d, j, al - j, bl - j, t->d);\n }\n rr->top = top;\n goto end;\n }\n }\n#endif\n if (bn_wexpand(rr, top) == NULL)\n goto err;\n rr->top = top;\n bn_mul_normal(rr->d, a->d, al, b->d, bl);\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n end:\n#endif\n rr->neg = a->neg ^ b->neg;\n bn_correct_top(rr);\n if (r != rr && BN_copy(r, rr) == NULL)\n goto err;\n ret = 1;\n err:\n bn_check_top(r);\n BN_CTX_end(ctx);\n return (ret);\n}', 'static unsigned int BN_STACK_pop(BN_STACK *st)\n{\n return st->indexes[--(st->depth)];\n}']
260
0
https://github.com/libav/libav/blob/63e8d9760f23a4edf81e9ae58c4f6d3baa6ff4dd/libavcodec/avpacket.c/#L83
int av_grow_packet(AVPacket *pkt, int grow_by) { void *new_ptr; av_assert0((unsigned)pkt->size <= INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE); if (!pkt->size) return av_new_packet(pkt, grow_by); if ((unsigned)grow_by > INT_MAX - (pkt->size + FF_INPUT_BUFFER_PADDING_SIZE)) return -1; new_ptr = av_realloc(pkt->data, pkt->size + grow_by + FF_INPUT_BUFFER_PADDING_SIZE); if (!new_ptr) return AVERROR(ENOMEM); pkt->data = new_ptr; pkt->size += grow_by; memset(pkt->data + pkt->size, 0, FF_INPUT_BUFFER_PADDING_SIZE); return 0; }
['static int wc3_read_header(AVFormatContext *s,\n AVFormatParameters *ap)\n{\n Wc3DemuxContext *wc3 = s->priv_data;\n ByteIOContext *pb = s->pb;\n unsigned int fourcc_tag;\n unsigned int size;\n AVStream *st;\n int ret = 0;\n char *buffer;\n wc3->width = WC3_DEFAULT_WIDTH;\n wc3->height = WC3_DEFAULT_HEIGHT;\n wc3->pts = 0;\n wc3->video_stream_index = wc3->audio_stream_index = 0;\n av_init_packet(&wc3->vpkt);\n wc3->vpkt.data = NULL; wc3->vpkt.size = 0;\n url_fseek(pb, 12, SEEK_CUR);\n fourcc_tag = get_le32(pb);\n size = (get_be32(pb) + 1) & (~1);\n do {\n switch (fourcc_tag) {\n case SOND_TAG:\n case INDX_TAG:\n url_fseek(pb, size, SEEK_CUR);\n break;\n case PC__TAG:\n url_fseek(pb, 12, SEEK_CUR);\n break;\n case BNAM_TAG:\n buffer = av_malloc(size+1);\n if (!buffer)\n return AVERROR(ENOMEM);\n if ((ret = get_buffer(pb, buffer, size)) != size)\n return AVERROR(EIO);\n buffer[size] = 0;\n av_metadata_set2(&s->metadata, "title", buffer,\n AV_METADATA_DONT_STRDUP_VAL);\n break;\n case SIZE_TAG:\n wc3->width = get_le32(pb);\n wc3->height = get_le32(pb);\n break;\n case PALT_TAG:\n url_fseek(pb, -8, SEEK_CUR);\n av_append_packet(pb, &wc3->vpkt, 8 + PALETTE_SIZE);\n break;\n default:\n av_log(s, AV_LOG_ERROR, " unrecognized WC3 chunk: %c%c%c%c (0x%02X%02X%02X%02X)\\n",\n (uint8_t)fourcc_tag, (uint8_t)(fourcc_tag >> 8), (uint8_t)(fourcc_tag >> 16), (uint8_t)(fourcc_tag >> 24),\n (uint8_t)fourcc_tag, (uint8_t)(fourcc_tag >> 8), (uint8_t)(fourcc_tag >> 16), (uint8_t)(fourcc_tag >> 24));\n return AVERROR_INVALIDDATA;\n break;\n }\n fourcc_tag = get_le32(pb);\n size = (get_be32(pb) + 1) & (~1);\n if (url_feof(pb))\n return AVERROR(EIO);\n } while (fourcc_tag != BRCH_TAG);\n st = av_new_stream(s, 0);\n if (!st)\n return AVERROR(ENOMEM);\n av_set_pts_info(st, 33, 1, WC3_FRAME_FPS);\n wc3->video_stream_index = st->index;\n st->codec->codec_type = AVMEDIA_TYPE_VIDEO;\n st->codec->codec_id = CODEC_ID_XAN_WC3;\n st->codec->codec_tag = 0;\n st->codec->width = wc3->width;\n st->codec->height = wc3->height;\n st = av_new_stream(s, 0);\n if (!st)\n return AVERROR(ENOMEM);\n av_set_pts_info(st, 33, 1, WC3_FRAME_FPS);\n wc3->audio_stream_index = st->index;\n st->codec->codec_type = AVMEDIA_TYPE_AUDIO;\n st->codec->codec_id = CODEC_ID_PCM_S16LE;\n st->codec->codec_tag = 1;\n st->codec->channels = WC3_AUDIO_CHANNELS;\n st->codec->bits_per_coded_sample = WC3_AUDIO_BITS;\n st->codec->sample_rate = WC3_SAMPLE_RATE;\n st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *\n st->codec->bits_per_coded_sample;\n st->codec->block_align = WC3_AUDIO_BITS * WC3_AUDIO_CHANNELS;\n return 0;\n}', 'int av_append_packet(ByteIOContext *s, AVPacket *pkt, int size)\n{\n int ret;\n int old_size;\n if (!pkt->size)\n return av_get_packet(s, pkt, size);\n old_size = pkt->size;\n ret = av_grow_packet(pkt, size);\n if (ret < 0)\n return ret;\n ret = get_buffer(s, pkt->data + old_size, size);\n av_shrink_packet(pkt, old_size + FFMAX(ret, 0));\n return ret;\n}', 'int av_grow_packet(AVPacket *pkt, int grow_by)\n{\n void *new_ptr;\n av_assert0((unsigned)pkt->size <= INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE);\n if (!pkt->size)\n return av_new_packet(pkt, grow_by);\n if ((unsigned)grow_by > INT_MAX - (pkt->size + FF_INPUT_BUFFER_PADDING_SIZE))\n return -1;\n new_ptr = av_realloc(pkt->data, pkt->size + grow_by + FF_INPUT_BUFFER_PADDING_SIZE);\n if (!new_ptr)\n return AVERROR(ENOMEM);\n pkt->data = new_ptr;\n pkt->size += grow_by;\n memset(pkt->data + pkt->size, 0, FF_INPUT_BUFFER_PADDING_SIZE);\n return 0;\n}']
261
0
https://github.com/openssl/openssl/blob/8b0d4242404f9e5da26e7594fa0864b2df4601af/crypto/bn/bn_lib.c/#L271
static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words) { BN_ULONG *a = NULL; bn_check_top(b); if (words > (INT_MAX / (4 * BN_BITS2))) { BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG); return NULL; } if (BN_get_flags(b, BN_FLG_STATIC_DATA)) { BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA); return (NULL); } if (BN_get_flags(b, BN_FLG_SECURE)) a = OPENSSL_secure_zalloc(words * sizeof(*a)); else a = OPENSSL_zalloc(words * sizeof(*a)); if (a == NULL) { BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE); return (NULL); } assert(b->top <= words); if (b->top > 0) memcpy(a, b->d, sizeof(*a) * b->top); return a; }
['int 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}', 'static int MOD_EXP_CTIME_COPY_TO_PREBUF(const BIGNUM *b, int top,\n unsigned char *buf, int idx,\n int window)\n{\n int i, j;\n int width = 1 << window;\n BN_ULONG *table = (BN_ULONG *)buf;\n if (top > b->top)\n top = b->top;\n for (i = 0, j = idx; i < top; i++, j += width) {\n table[j] = b->d[i];\n }\n return 1;\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}', '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 if (malloc_impl != NULL && malloc_impl != CRYPTO_malloc)\n return malloc_impl(num, file, line);\n if (num == 0)\n return NULL;\n FAILTEST();\n allow_customize = 0;\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n if (call_malloc_debug) {\n CRYPTO_mem_debug_malloc(NULL, num, 0, file, line);\n ret = malloc(num);\n CRYPTO_mem_debug_malloc(ret, num, 1, file, line);\n } else {\n ret = malloc(num);\n }\n#else\n osslargused(file); osslargused(line);\n ret = malloc(num);\n#endif\n return ret;\n}']
262
0
https://github.com/openssl/openssl/blob/4882171df5ba7cb8735b685478a008df25eef47a/crypto/bn/bn_print.c/#L197
int BN_hex2bn(BIGNUM **bn, const char *a) { BIGNUM *ret=NULL; BN_ULONG l=0; int neg=0,h,m,i,j,k,c; int num; if ((a == NULL) || (*a == '\0')) return(0); if (*a == '-') { neg=1; a++; } for (i=0; isxdigit((unsigned char) a[i]); i++) ; num=i+neg; if (bn == NULL) return(num); if (*bn == NULL) { if ((ret=BN_new()) == NULL) return(0); } else { ret= *bn; BN_zero(ret); } if (bn_expand(ret,i*4) == NULL) goto err; j=i; m=0; h=0; while (j > 0) { m=((BN_BYTES*2) <= j)?(BN_BYTES*2):j; l=0; for (;;) { c=a[j-m]; if ((c >= '0') && (c <= '9')) k=c-'0'; else if ((c >= 'a') && (c <= 'f')) k=c-'a'+10; else if ((c >= 'A') && (c <= 'F')) k=c-'A'+10; else k=0; l=(l<<4)|k; if (--m <= 0) { ret->d[h++]=l; break; } } j-=(BN_BYTES*2); } ret->top=h; bn_fix_top(ret); ret->neg=neg; *bn=ret; return(num); err: if (*bn == NULL) BN_free(ret); return(0); }
['EC_GROUP *EC_GROUP_new_by_name(int name)\n\t{\n\tEC_GROUP *ret = NULL;\n\tswitch (name)\n\t\t{\n\tcase EC_GROUP_NO_CURVE:\n\t\treturn NULL;\n\tcase EC_GROUP_NIST_PRIME_224:\n\t\tret = ec_group_new_GFp_from_hex(_EC_GROUP_NIST_PRIME_224);\n\t\tbreak;\n\tcase EC_GROUP_NIST_PRIME_384:\n\t\tret = ec_group_new_GFp_from_hex(_EC_GROUP_NIST_PRIME_384);\n\t\tbreak;\n\tcase EC_GROUP_NIST_PRIME_521:\n\t\tret = ec_group_new_GFp_from_hex(_EC_GROUP_NIST_PRIME_521);\n\t\tbreak;\n\tcase EC_GROUP_NIST_PRIME_192:\n\tcase EC_GROUP_X9_62_PRIME_192V1:\n\t\tret = ec_group_new_GFp_from_hex(_EC_GROUP_NIST_PRIME_192);\n\t\tbreak;\n\tcase EC_GROUP_X9_62_PRIME_192V2:\n\t\tret = ec_group_new_GFp_from_hex(_EC_GROUP_X9_62_PRIME_192V2);\n\t\tbreak;\n\tcase EC_GROUP_X9_62_PRIME_192V3:\n\t\tret = ec_group_new_GFp_from_hex(_EC_GROUP_X9_62_PRIME_192V3);\n\t\tbreak;\n\tcase EC_GROUP_X9_62_PRIME_239V1:\n\t\tret = ec_group_new_GFp_from_hex(_EC_GROUP_X9_62_PRIME_239V1);\n\t\tbreak;\n\tcase EC_GROUP_X9_62_PRIME_239V2:\n\t\tret = ec_group_new_GFp_from_hex(_EC_GROUP_X9_62_PRIME_239V2);\n\t\tbreak;\n\tcase EC_GROUP_X9_62_PRIME_239V3:\n\t\tret = ec_group_new_GFp_from_hex(_EC_GROUP_X9_62_PRIME_239V3);\n\t\tbreak;\n\tcase EC_GROUP_NIST_PRIME_256:\n\tcase EC_GROUP_X9_62_PRIME_256V1:\n\t\tret = ec_group_new_GFp_from_hex(_EC_GROUP_X9_62_PRIME_256V1);\n\t\tbreak;\n\tcase EC_GROUP_SECG_PRIME_112R1:\n\t\tret = ec_group_new_GFp_from_hex(_EC_GROUP_SECG_PRIME_112R1);\n\t\tbreak;\n\tcase EC_GROUP_SECG_PRIME_112R2:\n\t\tret = ec_group_new_GFp_from_hex(_EC_GROUP_SECG_PRIME_112R2);\n\t\tbreak;\n\tcase EC_GROUP_SECG_PRIME_128R1:\n\t\tret = ec_group_new_GFp_from_hex(_EC_GROUP_SECG_PRIME_128R1);\n\t\tbreak;\n\tcase EC_GROUP_SECG_PRIME_128R2:\n\t\tret = ec_group_new_GFp_from_hex(_EC_GROUP_SECG_PRIME_128R2);\n\t\tbreak;\n\tcase EC_GROUP_SECG_PRIME_160K1:\n\t\tret = ec_group_new_GFp_from_hex(_EC_GROUP_SECG_PRIME_160K1);\n\t\tbreak;\n\tcase EC_GROUP_SECG_PRIME_160R1:\n\t\tret = ec_group_new_GFp_from_hex(_EC_GROUP_SECG_PRIME_160R1);\n\t\tbreak;\n\tcase EC_GROUP_SECG_PRIME_160R2:\n\t\tret = ec_group_new_GFp_from_hex(_EC_GROUP_SECG_PRIME_160R2);\n\t\tbreak;\n\tcase EC_GROUP_SECG_PRIME_192K1:\n\t\tret = ec_group_new_GFp_from_hex(_EC_GROUP_SECG_PRIME_192K1);\n\t\tbreak;\n\tcase EC_GROUP_SECG_PRIME_224K1:\n\t\tret = ec_group_new_GFp_from_hex(_EC_GROUP_SECG_PRIME_224K1);\n\t\tbreak;\n\tcase EC_GROUP_SECG_PRIME_256K1:\n\t\tret = ec_group_new_GFp_from_hex(_EC_GROUP_SECG_PRIME_256K1);\n\t\tbreak;\n\t\t}\n\tif (ret == NULL)\n\t\t{\n\t\tECerr(EC_F_EC_GROUP_NEW_BY_NAME, EC_R_UNKNOWN_GROUP);\n\t\treturn NULL;\n\t\t}\n\tEC_GROUP_set_nid(ret, name);\n\treturn ret;\n\t}', 'static EC_GROUP *ec_group_new_GFp_from_hex(const char *prime_in,\n\t const char *a_in, const char *b_in,\n\t const char *x_in, const int y_bit, const char *order_in, const BN_ULONG cofac_in)\n\t{\n\tEC_GROUP *group=NULL;\n\tEC_POINT *P=NULL;\n\tBN_CTX\t *ctx=NULL;\n\tBIGNUM \t *prime=NULL,*a=NULL,*b=NULL,*x=NULL,*order=NULL;\n\tint\t ok=0;\n\tif ((ctx = BN_CTX_new()) == NULL) goto bn_err;\n\tif ((prime = BN_new()) == NULL || (a = BN_new()) == NULL || (b = BN_new()) == NULL ||\n\t\t(x = BN_new()) == NULL || (order = BN_new()) == NULL) goto bn_err;\n\tif (!BN_hex2bn(&prime, prime_in)) goto bn_err;\n\tif (!BN_hex2bn(&a, a_in)) goto bn_err;\n\tif (!BN_hex2bn(&b, b_in)) goto bn_err;\n\tif ((group = EC_GROUP_new_curve_GFp(prime, a, b, ctx)) == NULL) goto err;\n\tif ((P = EC_POINT_new(group)) == NULL) goto err;\n\tif (!BN_hex2bn(&x, x_in)) goto bn_err;\n\tif (!EC_POINT_set_compressed_coordinates_GFp(group, P, x, y_bit, ctx)) goto err;\n\tif (!BN_hex2bn(&order, order_in)) goto bn_err;\n\tif (!BN_set_word(x, cofac_in)) goto bn_err;\n\tif (!EC_GROUP_set_generator(group, P, order, x)) goto err;\n\tok=1;\nbn_err:\n\tif (!ok)\n\t\tECerr(EC_F_EC_GROUP_NEW_GFP_FROM_HEX, ERR_R_BN_LIB);\nerr:\n\tif (!ok)\n\t\t{\n\t\tEC_GROUP_free(group);\n\t\tgroup = NULL;\n\t\t}\n\tif (P) \t EC_POINT_free(P);\n\tif (ctx) BN_CTX_free(ctx);\n\tif (prime) BN_free(prime);\n\tif (a) BN_free(a);\n\tif (b) BN_free(b);\n\tif (order) BN_free(order);\n\tif (x) BN_free(x);\n\treturn(group);\n\t}', "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_fix_top(ret);\n\tret->neg=neg;\n\t*bn=ret;\n\treturn(num);\nerr:\n\tif (*bn == NULL) BN_free(ret);\n\treturn(0);\n\t}"]
263
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 srp_generate_client_master_secret(SSL *s)\n{\n BIGNUM *x = NULL, *u = NULL, *K = NULL;\n int ret = -1, tmp_len = 0;\n char *passwd = NULL;\n unsigned char *tmp = NULL;\n if (SRP_Verify_B_mod_N(s->srp_ctx.B, s->srp_ctx.N) == 0\n || (u = SRP_Calc_u(s->srp_ctx.A, s->srp_ctx.B, s->srp_ctx.N))\n == NULL\n || s->srp_ctx.SRP_give_srp_client_pwd_callback == NULL) {\n SSLfatal(s, SSL_AD_INTERNAL_ERROR,\n SSL_F_SRP_GENERATE_CLIENT_MASTER_SECRET, ERR_R_INTERNAL_ERROR);\n goto err;\n }\n if ((passwd = s->srp_ctx.SRP_give_srp_client_pwd_callback(s,\n s->srp_ctx.SRP_cb_arg))\n == NULL) {\n SSLfatal(s, SSL_AD_INTERNAL_ERROR,\n SSL_F_SRP_GENERATE_CLIENT_MASTER_SECRET,\n SSL_R_CALLBACK_FAILED);\n goto err;\n }\n if ((x = SRP_Calc_x(s->srp_ctx.s, s->srp_ctx.login, passwd)) == NULL\n || (K = SRP_Calc_client_key(s->srp_ctx.N, s->srp_ctx.B,\n s->srp_ctx.g, x,\n s->srp_ctx.a, u)) == NULL) {\n SSLfatal(s, SSL_AD_INTERNAL_ERROR,\n SSL_F_SRP_GENERATE_CLIENT_MASTER_SECRET, ERR_R_INTERNAL_ERROR);\n goto err;\n }\n tmp_len = BN_num_bytes(K);\n if ((tmp = OPENSSL_malloc(tmp_len)) == NULL) {\n SSLfatal(s, SSL_AD_INTERNAL_ERROR,\n SSL_F_SRP_GENERATE_CLIENT_MASTER_SECRET, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n BN_bn2bin(K, tmp);\n ret = ssl_generate_master_secret(s, tmp, tmp_len, 1);\n err:\n BN_clear_free(K);\n BN_clear_free(x);\n if (passwd != NULL)\n OPENSSL_clear_free(passwd, strlen(passwd));\n BN_clear_free(u);\n return ret;\n}', 'int SRP_Verify_B_mod_N(const BIGNUM *B, const BIGNUM *N)\n{\n BIGNUM *r;\n BN_CTX *bn_ctx;\n int ret = 0;\n if (B == NULL || N == NULL || (bn_ctx = BN_CTX_new()) == NULL)\n return 0;\n if ((r = BN_new()) == NULL)\n goto err;\n if (!BN_nnmod(r, B, N, bn_ctx))\n goto err;\n ret = !BN_is_zero(r);\n err:\n BN_CTX_free(bn_ctx);\n BN_free(r);\n return ret;\n}', 'BIGNUM *SRP_Calc_u(const BIGNUM *A, const BIGNUM *B, const BIGNUM *N)\n{\n return srp_Calc_xy(A, B, N);\n}', 'static BIGNUM *srp_Calc_xy(const BIGNUM *x, const BIGNUM *y, const BIGNUM *N)\n{\n unsigned char digest[SHA_DIGEST_LENGTH];\n unsigned char *tmp = NULL;\n int numN = BN_num_bytes(N);\n BIGNUM *res = NULL;\n if (x != N && BN_ucmp(x, N) >= 0)\n return NULL;\n if (y != N && BN_ucmp(y, N) >= 0)\n return NULL;\n if ((tmp = OPENSSL_malloc(numN * 2)) == NULL)\n goto err;\n if (BN_bn2binpad(x, tmp, numN) < 0\n || BN_bn2binpad(y, tmp + numN, numN) < 0\n || !EVP_Digest(tmp, numN * 2, digest, NULL, EVP_sha1(), NULL))\n goto err;\n res = BN_bin2bn(digest, sizeof(digest), NULL);\n err:\n OPENSSL_free(tmp);\n return res;\n}', 'int BN_num_bits(const BIGNUM *a)\n{\n int i = a->top - 1;\n bn_check_top(a);\n if (BN_is_zero(a))\n return 0;\n return ((i * BN_BITS2) + BN_num_bits_word(a->d[i]));\n}', 'int BN_is_zero(const BIGNUM *a)\n{\n return a->top == 0;\n}', 'BIGNUM *SRP_Calc_client_key(const BIGNUM *N, const BIGNUM *B, const BIGNUM *g,\n const BIGNUM *x, const BIGNUM *a, const BIGNUM *u)\n{\n BIGNUM *tmp = NULL, *tmp2 = NULL, *tmp3 = NULL, *k = NULL, *K = NULL;\n BN_CTX *bn_ctx;\n if (u == NULL || B == NULL || N == NULL || g == NULL || x == NULL\n || a == NULL || (bn_ctx = BN_CTX_new()) == NULL)\n return NULL;\n if ((tmp = BN_new()) == NULL ||\n (tmp2 = BN_new()) == NULL ||\n (tmp3 = BN_new()) == NULL)\n goto err;\n if (!BN_mod_exp(tmp, g, x, N, bn_ctx))\n goto err;\n if ((k = srp_Calc_k(N, g)) == NULL)\n goto err;\n if (!BN_mod_mul(tmp2, tmp, k, N, bn_ctx))\n goto err;\n if (!BN_mod_sub(tmp, B, tmp2, N, bn_ctx))\n goto err;\n if (!BN_mul(tmp3, u, x, bn_ctx))\n goto err;\n if (!BN_add(tmp2, a, tmp3))\n goto err;\n K = BN_new();\n if (K != NULL && !BN_mod_exp(K, tmp, tmp2, N, bn_ctx)) {\n BN_free(K);\n K = NULL;\n }\n err:\n BN_CTX_free(bn_ctx);\n BN_clear_free(tmp);\n BN_clear_free(tmp2);\n BN_clear_free(tmp3);\n BN_free(k);\n return K;\n}', 'int BN_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m,\n BN_CTX *ctx)\n{\n int ret;\n bn_check_top(a);\n bn_check_top(p);\n bn_check_top(m);\n#define MONT_MUL_MOD\n#define MONT_EXP_WORD\n#define RECP_MUL_MOD\n#ifdef MONT_MUL_MOD\n if (BN_is_odd(m)) {\n# ifdef MONT_EXP_WORD\n if (a->top == 1 && !a->neg\n && (BN_get_flags(p, BN_FLG_CONSTTIME) == 0)\n && (BN_get_flags(a, BN_FLG_CONSTTIME) == 0)\n && (BN_get_flags(m, BN_FLG_CONSTTIME) == 0)) {\n BN_ULONG A = a->d[0];\n ret = BN_mod_exp_mont_word(r, A, p, m, ctx, NULL);\n } else\n# endif\n ret = BN_mod_exp_mont(r, a, p, m, ctx, NULL);\n } else\n#endif\n#ifdef RECP_MUL_MOD\n {\n ret = BN_mod_exp_recp(r, a, p, m, ctx);\n }\n#else\n {\n ret = BN_mod_exp_simple(r, a, p, m, ctx);\n }\n#endif\n bn_check_top(r);\n return ret;\n}', 'int BN_is_odd(const BIGNUM *a)\n{\n return (a->top > 0) && (a->d[0] & 1);\n}', 'int BN_mod_exp_recp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,\n const BIGNUM *m, BN_CTX *ctx)\n{\n int i, j, bits, ret = 0, wstart, wend, window, wvalue;\n int start = 1;\n BIGNUM *aa;\n BIGNUM *val[TABLE_SIZE];\n BN_RECP_CTX recp;\n if (BN_get_flags(p, BN_FLG_CONSTTIME) != 0\n || BN_get_flags(a, BN_FLG_CONSTTIME) != 0\n || BN_get_flags(m, BN_FLG_CONSTTIME) != 0) {\n BNerr(BN_F_BN_MOD_EXP_RECP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);\n return 0;\n }\n bits = BN_num_bits(p);\n if (bits == 0) {\n if (BN_abs_is_word(m, 1)) {\n ret = 1;\n BN_zero(r);\n } else {\n ret = BN_one(r);\n }\n return ret;\n }\n BN_CTX_start(ctx);\n aa = BN_CTX_get(ctx);\n val[0] = BN_CTX_get(ctx);\n if (val[0] == NULL)\n goto err;\n BN_RECP_CTX_init(&recp);\n if (m->neg) {\n if (!BN_copy(aa, m))\n goto err;\n aa->neg = 0;\n if (BN_RECP_CTX_set(&recp, aa, ctx) <= 0)\n goto err;\n } else {\n if (BN_RECP_CTX_set(&recp, m, ctx) <= 0)\n goto err;\n }\n if (!BN_nnmod(val[0], a, m, ctx))\n goto err;\n if (BN_is_zero(val[0])) {\n BN_zero(r);\n ret = 1;\n goto err;\n }\n window = BN_window_bits_for_exponent_size(bits);\n if (window > 1) {\n if (!BN_mod_mul_reciprocal(aa, val[0], val[0], &recp, ctx))\n goto err;\n j = 1 << (window - 1);\n for (i = 1; i < j; i++) {\n if (((val[i] = BN_CTX_get(ctx)) == NULL) ||\n !BN_mod_mul_reciprocal(val[i], val[i - 1], aa, &recp, ctx))\n goto err;\n }\n }\n start = 1;\n wvalue = 0;\n wstart = bits - 1;\n wend = 0;\n if (!BN_one(r))\n goto err;\n for (;;) {\n if (BN_is_bit_set(p, wstart) == 0) {\n if (!start)\n if (!BN_mod_mul_reciprocal(r, r, r, &recp, ctx))\n goto err;\n if (wstart == 0)\n break;\n wstart--;\n continue;\n }\n j = wstart;\n wvalue = 1;\n wend = 0;\n for (i = 1; i < window; i++) {\n if (wstart - i < 0)\n break;\n if (BN_is_bit_set(p, wstart - i)) {\n wvalue <<= (i - wend);\n wvalue |= 1;\n wend = i;\n }\n }\n j = wend + 1;\n if (!start)\n for (i = 0; i < j; i++) {\n if (!BN_mod_mul_reciprocal(r, r, r, &recp, ctx))\n goto err;\n }\n if (!BN_mod_mul_reciprocal(r, r, val[wvalue >> 1], &recp, ctx))\n goto err;\n wstart -= wend + 1;\n wvalue = 0;\n start = 0;\n if (wstart < 0)\n break;\n }\n ret = 1;\n err:\n BN_CTX_end(ctx);\n BN_RECP_CTX_free(&recp);\n bn_check_top(r);\n return ret;\n}', 'int BN_RECP_CTX_set(BN_RECP_CTX *recp, const BIGNUM *d, BN_CTX *ctx)\n{\n if (!BN_copy(&(recp->N), d))\n return 0;\n BN_zero(&(recp->Nr));\n recp->num_bits = BN_num_bits(d);\n recp->shift = 0;\n return 1;\n}', 'BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)\n{\n bn_check_top(b);\n if (a == b)\n return a;\n if (bn_wexpand(a, b->top) == NULL)\n return NULL;\n if (b->top > 0)\n memcpy(a->d, b->d, sizeof(b->d[0]) * b->top);\n a->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}']
264
0
https://github.com/openssl/openssl/blob/e3713c365c2657236439fea00822a43aa396d112/crypto/bn/bn_lib.c/#L711
int bn_cmp_words(const BN_ULONG *a, const BN_ULONG *b, int n) { int i; BN_ULONG aa, bb; aa = a[n - 1]; bb = b[n - 1]; if (aa != bb) return ((aa > bb) ? 1 : -1); for (i = n - 2; i >= 0; i--) { aa = a[i]; bb = b[i]; if (aa != bb) return ((aa > bb) ? 1 : -1); } return (0); }
['static int test_mod_exp_zero(void)\n{\n BIGNUM *a = NULL, *p = NULL, *m = NULL;\n BIGNUM *r = NULL;\n BN_ULONG one_word = 1;\n BN_CTX *ctx = BN_CTX_new();\n int ret = 1, failed = 0;\n if (!TEST_ptr(m = BN_new())\n || !TEST_ptr(a = BN_new())\n || !TEST_ptr(p = BN_new())\n || !TEST_ptr(r = BN_new()))\n goto err;\n BN_one(m);\n BN_one(a);\n BN_zero(p);\n if (!TEST_true(BN_rand(a, 1024, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY)))\n goto err;\n if (!TEST_true(BN_mod_exp(r, a, p, m, ctx)))\n goto err;\n if (!TEST_true(a_is_zero_mod_one("BN_mod_exp", r, a)))\n failed = 1;\n if (!TEST_true(BN_mod_exp_recp(r, a, p, m, ctx)))\n goto err;\n if (!TEST_true(a_is_zero_mod_one("BN_mod_exp_recp", r, a)))\n failed = 1;\n if (!TEST_true(BN_mod_exp_simple(r, a, p, m, ctx)))\n goto err;\n if (!TEST_true(a_is_zero_mod_one("BN_mod_exp_simple", r, a)))\n failed = 1;\n if (!TEST_true(BN_mod_exp_mont(r, a, p, m, ctx, NULL)))\n goto err;\n if (!TEST_true(a_is_zero_mod_one("BN_mod_exp_mont", r, a)))\n failed = 1;\n if (!TEST_true(BN_mod_exp_mont_consttime(r, a, p, m, ctx, NULL)))\n goto err;\n if (!TEST_true(a_is_zero_mod_one("BN_mod_exp_mont_consttime", r, a)))\n failed = 1;\n if (!TEST_true(BN_mod_exp_mont_word(r, one_word, p, m, ctx, NULL)))\n goto err;\n if (!TEST_BN_eq_zero(r)) {\n TEST_error("BN_mod_exp_mont_word failed: "\n "1 ** 0 mod 1 = r (should be 0)");\n BN_print_var(r);\n goto err;\n }\n ret = !failed;\n err:\n BN_free(r);\n BN_free(a);\n BN_free(p);\n BN_free(m);\n BN_CTX_free(ctx);\n return ret;\n}', 'int BN_set_word(BIGNUM *a, BN_ULONG w)\n{\n bn_check_top(a);\n if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)\n return (0);\n a->neg = 0;\n a->d[0] = w;\n a->top = (w ? 1 : 0);\n bn_check_top(a);\n return 1;\n}', 'static ossl_inline BIGNUM *bn_expand(BIGNUM *a, int bits)\n{\n if (bits > (INT_MAX - BN_BITS2 + 1))\n return NULL;\n if (((bits+BN_BITS2-1)/BN_BITS2) <= (a)->dmax)\n return a;\n return bn_expand2((a),(bits+BN_BITS2-1)/BN_BITS2);\n}', 'int BN_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m,\n BN_CTX *ctx)\n{\n int ret;\n bn_check_top(a);\n bn_check_top(p);\n bn_check_top(m);\n#define MONT_MUL_MOD\n#define MONT_EXP_WORD\n#define RECP_MUL_MOD\n#ifdef MONT_MUL_MOD\n if (BN_is_odd(m)) {\n# ifdef MONT_EXP_WORD\n if (a->top == 1 && !a->neg\n && (BN_get_flags(p, BN_FLG_CONSTTIME) == 0)\n && (BN_get_flags(a, BN_FLG_CONSTTIME) == 0)\n && (BN_get_flags(m, BN_FLG_CONSTTIME) == 0)) {\n BN_ULONG A = a->d[0];\n ret = BN_mod_exp_mont_word(r, A, p, m, ctx, NULL);\n } else\n# endif\n ret = BN_mod_exp_mont(r, a, p, m, ctx, NULL);\n } else\n#endif\n#ifdef RECP_MUL_MOD\n {\n ret = BN_mod_exp_recp(r, a, p, m, ctx);\n }\n#else\n {\n ret = BN_mod_exp_simple(r, a, p, m, ctx);\n }\n#endif\n bn_check_top(r);\n return (ret);\n}', 'int BN_mod_exp_mont(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p,\n const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont)\n{\n int i, j, bits, ret = 0, wstart, wend, window, wvalue;\n int start = 1;\n BIGNUM *d, *r;\n const BIGNUM *aa;\n BIGNUM *val[TABLE_SIZE];\n BN_MONT_CTX *mont = NULL;\n if (BN_get_flags(p, BN_FLG_CONSTTIME) != 0\n || BN_get_flags(a, BN_FLG_CONSTTIME) != 0\n || BN_get_flags(m, BN_FLG_CONSTTIME) != 0) {\n return BN_mod_exp_mont_consttime(rr, a, p, m, ctx, in_mont);\n }\n bn_check_top(a);\n bn_check_top(p);\n bn_check_top(m);\n if (!BN_is_odd(m)) {\n BNerr(BN_F_BN_MOD_EXP_MONT, BN_R_CALLED_WITH_EVEN_MODULUS);\n return (0);\n }\n bits = BN_num_bits(p);\n if (bits == 0) {\n if (BN_is_one(m)) {\n ret = 1;\n BN_zero(rr);\n } else {\n ret = BN_one(rr);\n }\n return ret;\n }\n BN_CTX_start(ctx);\n d = BN_CTX_get(ctx);\n r = BN_CTX_get(ctx);\n val[0] = BN_CTX_get(ctx);\n if (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;\n int top;\n BN_MONT_CTX *mont = NULL;\n int numPowers;\n unsigned char *powerbufFree = NULL;\n int powerbufLen = 0;\n unsigned char *powerbuf = NULL;\n BIGNUM tmp, am;\n#if defined(SPARC_T4_MONT)\n unsigned int t4 = 0;\n#endif\n bn_check_top(a);\n bn_check_top(p);\n bn_check_top(m);\n if (!BN_is_odd(m)) {\n BNerr(BN_F_BN_MOD_EXP_MONT_CONSTTIME, BN_R_CALLED_WITH_EVEN_MODULUS);\n return (0);\n }\n top = m->top;\n bits = BN_num_bits(p);\n if (bits == 0) {\n if (BN_is_one(m)) {\n ret = 1;\n BN_zero(rr);\n } else {\n ret = BN_one(rr);\n }\n return ret;\n }\n BN_CTX_start(ctx);\n if (in_mont != NULL)\n mont = in_mont;\n else {\n if ((mont = BN_MONT_CTX_new()) == NULL)\n goto err;\n if (!BN_MONT_CTX_set(mont, m, ctx))\n goto err;\n }\n#ifdef RSAZ_ENABLED\n if ((16 == a->top) && (16 == p->top) && (BN_num_bits(m) == 1024)\n && rsaz_avx2_eligible()) {\n if (NULL == bn_wexpand(rr, 16))\n goto err;\n RSAZ_1024_mod_exp_avx2(rr->d, a->d, p->d, m->d, mont->RR.d,\n mont->n0[0]);\n rr->top = 16;\n rr->neg = 0;\n bn_correct_top(rr);\n ret = 1;\n goto err;\n } else if ((8 == a->top) && (8 == p->top) && (BN_num_bits(m) == 512)) {\n if (NULL == bn_wexpand(rr, 8))\n goto err;\n RSAZ_512_mod_exp(rr->d, a->d, p->d, m->d, mont->n0[0], mont->RR.d);\n rr->top = 8;\n rr->neg = 0;\n bn_correct_top(rr);\n ret = 1;\n goto err;\n }\n#endif\n window = BN_window_bits_for_ctime_exponent_size(bits);\n#if defined(SPARC_T4_MONT)\n if (window >= 5 && (top & 15) == 0 && top <= 64 &&\n (OPENSSL_sparcv9cap_P[1] & (CFR_MONTMUL | CFR_MONTSQR)) ==\n (CFR_MONTMUL | CFR_MONTSQR) && (t4 = OPENSSL_sparcv9cap_P[0]))\n window = 5;\n else\n#endif\n#if defined(OPENSSL_BN_ASM_MONT5)\n if (window >= 5) {\n window = 5;\n powerbufLen += top * sizeof(mont->N.d[0]);\n }\n#endif\n (void)0;\n numPowers = 1 << window;\n powerbufLen += sizeof(m->d[0]) * (top * numPowers +\n ((2 * top) >\n numPowers ? (2 * top) : numPowers));\n#ifdef alloca\n if (powerbufLen < 3072)\n powerbufFree =\n alloca(powerbufLen + MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH);\n else\n#endif\n if ((powerbufFree =\n OPENSSL_malloc(powerbufLen + MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH))\n == NULL)\n goto err;\n powerbuf = MOD_EXP_CTIME_ALIGN(powerbufFree);\n memset(powerbuf, 0, powerbufLen);\n#ifdef alloca\n if (powerbufLen < 3072)\n powerbufFree = NULL;\n#endif\n tmp.d = (BN_ULONG *)(powerbuf + sizeof(m->d[0]) * top * numPowers);\n am.d = tmp.d + top;\n tmp.top = am.top = 0;\n tmp.dmax = am.dmax = top;\n tmp.neg = am.neg = 0;\n tmp.flags = am.flags = BN_FLG_STATIC_DATA;\n#if 1\n if (m->d[top - 1] & (((BN_ULONG)1) << (BN_BITS2 - 1))) {\n tmp.d[0] = (0 - m->d[0]) & BN_MASK2;\n for (i = 1; i < top; i++)\n tmp.d[i] = (~m->d[i]) & BN_MASK2;\n tmp.top = top;\n } else\n#endif\n if (!BN_to_montgomery(&tmp, BN_value_one(), mont, ctx))\n goto err;\n if (a->neg || BN_ucmp(a, m) >= 0) {\n if (!BN_mod(&am, a, m, ctx))\n goto err;\n if (!BN_to_montgomery(&am, &am, mont, ctx))\n goto err;\n } else if (!BN_to_montgomery(&am, a, mont, ctx))\n goto err;\n#if defined(SPARC_T4_MONT)\n if (t4) {\n typedef int (*bn_pwr5_mont_f) (BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n int bn_pwr5_mont_t4_8(BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n int bn_pwr5_mont_t4_16(BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n int bn_pwr5_mont_t4_24(BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n int bn_pwr5_mont_t4_32(BN_ULONG *tp, const BN_ULONG *np,\n const BN_ULONG *n0, const void *table,\n int power, int bits);\n static const bn_pwr5_mont_f pwr5_funcs[4] = {\n bn_pwr5_mont_t4_8, bn_pwr5_mont_t4_16,\n bn_pwr5_mont_t4_24, bn_pwr5_mont_t4_32\n };\n bn_pwr5_mont_f pwr5_worker = pwr5_funcs[top / 16 - 1];\n typedef int (*bn_mul_mont_f) (BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0);\n int bn_mul_mont_t4_8(BN_ULONG *rp, const BN_ULONG *ap, const void *bp,\n const BN_ULONG *np, const BN_ULONG *n0);\n int bn_mul_mont_t4_16(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0);\n int bn_mul_mont_t4_24(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0);\n int bn_mul_mont_t4_32(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0);\n static const bn_mul_mont_f mul_funcs[4] = {\n bn_mul_mont_t4_8, bn_mul_mont_t4_16,\n bn_mul_mont_t4_24, bn_mul_mont_t4_32\n };\n bn_mul_mont_f mul_worker = mul_funcs[top / 16 - 1];\n void bn_mul_mont_vis3(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0, int num);\n void bn_mul_mont_t4(BN_ULONG *rp, const BN_ULONG *ap,\n const void *bp, const BN_ULONG *np,\n const BN_ULONG *n0, int num);\n void bn_mul_mont_gather5_t4(BN_ULONG *rp, const BN_ULONG *ap,\n const void *table, const BN_ULONG *np,\n const BN_ULONG *n0, int num, int power);\n void bn_flip_n_scatter5_t4(const BN_ULONG *inp, size_t num,\n void *table, size_t power);\n void bn_gather5_t4(BN_ULONG *out, size_t num,\n void *table, size_t power);\n void bn_flip_t4(BN_ULONG *dst, BN_ULONG *src, size_t num);\n BN_ULONG *np = mont->N.d, *n0 = mont->n0;\n int stride = 5 * (6 - (top / 16 - 1));\n for (i = am.top; i < top; i++)\n am.d[i] = 0;\n for (i = tmp.top; i < top; i++)\n tmp.d[i] = 0;\n bn_flip_n_scatter5_t4(tmp.d, top, powerbuf, 0);\n bn_flip_n_scatter5_t4(am.d, top, powerbuf, 1);\n if (!(*mul_worker) (tmp.d, am.d, am.d, np, n0) &&\n !(*mul_worker) (tmp.d, am.d, am.d, np, n0))\n bn_mul_mont_vis3(tmp.d, am.d, am.d, np, n0, top);\n bn_flip_n_scatter5_t4(tmp.d, top, powerbuf, 2);\n for (i = 3; i < 32; i++) {\n if (!(*mul_worker) (tmp.d, tmp.d, am.d, np, n0) &&\n !(*mul_worker) (tmp.d, tmp.d, am.d, np, n0))\n bn_mul_mont_vis3(tmp.d, tmp.d, am.d, np, n0, top);\n bn_flip_n_scatter5_t4(tmp.d, top, powerbuf, i);\n }\n np = alloca(top * sizeof(BN_ULONG));\n top /= 2;\n bn_flip_t4(np, mont->N.d, top);\n bits--;\n for (wvalue = 0, i = bits % 5; i >= 0; i--, bits--)\n wvalue = (wvalue << 1) + BN_is_bit_set(p, bits);\n bn_gather5_t4(tmp.d, top, powerbuf, wvalue);\n while (bits >= 0) {\n if (bits < stride)\n stride = bits + 1;\n bits -= stride;\n wvalue = bn_get_bits(p, bits + 1);\n if ((*pwr5_worker) (tmp.d, np, n0, powerbuf, wvalue, stride))\n continue;\n if ((*pwr5_worker) (tmp.d, np, n0, powerbuf, wvalue, stride))\n continue;\n bits += stride - 5;\n wvalue >>= stride - 5;\n wvalue &= 31;\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_gather5_t4(tmp.d, tmp.d, powerbuf, np, n0, top,\n wvalue);\n }\n bn_flip_t4(tmp.d, tmp.d, top);\n top *= 2;\n tmp.top = top;\n bn_correct_top(&tmp);\n OPENSSL_cleanse(np, top * sizeof(BN_ULONG));\n } else\n#endif\n#if defined(OPENSSL_BN_ASM_MONT5)\n if (window == 5 && top > 1) {\n void bn_mul_mont_gather5(BN_ULONG *rp, const BN_ULONG *ap,\n const void *table, const BN_ULONG *np,\n const BN_ULONG *n0, int num, int power);\n void bn_scatter5(const BN_ULONG *inp, size_t num,\n void *table, size_t power);\n void bn_gather5(BN_ULONG *out, size_t num, void *table, size_t power);\n void bn_power5(BN_ULONG *rp, const BN_ULONG *ap,\n const void *table, const BN_ULONG *np,\n const BN_ULONG *n0, int num, int power);\n int bn_get_bits5(const BN_ULONG *ap, int off);\n int bn_from_montgomery(BN_ULONG *rp, const BN_ULONG *ap,\n const BN_ULONG *not_used, const BN_ULONG *np,\n const BN_ULONG *n0, int num);\n BN_ULONG *n0 = mont->n0, *np;\n for (i = am.top; i < top; i++)\n am.d[i] = 0;\n for (i = tmp.top; i < top; i++)\n tmp.d[i] = 0;\n for (np = am.d + top, i = 0; i < top; i++)\n np[i] = mont->N.d[i];\n bn_scatter5(tmp.d, top, powerbuf, 0);\n bn_scatter5(am.d, am.top, powerbuf, 1);\n bn_mul_mont(tmp.d, am.d, am.d, np, n0, top);\n bn_scatter5(tmp.d, top, powerbuf, 2);\n# if 0\n for (i = 3; i < 32; i++) {\n bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1);\n bn_scatter5(tmp.d, top, powerbuf, i);\n }\n# else\n for (i = 4; i < 32; i *= 2) {\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_scatter5(tmp.d, top, powerbuf, i);\n }\n for (i = 3; i < 8; i += 2) {\n int j;\n bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1);\n bn_scatter5(tmp.d, top, powerbuf, i);\n for (j = 2 * i; j < 32; j *= 2) {\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_scatter5(tmp.d, top, powerbuf, j);\n }\n }\n for (; i < 16; i += 2) {\n bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1);\n bn_scatter5(tmp.d, top, powerbuf, i);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_scatter5(tmp.d, top, powerbuf, 2 * i);\n }\n for (; i < 32; i += 2) {\n bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1);\n bn_scatter5(tmp.d, top, powerbuf, i);\n }\n# endif\n bits--;\n for (wvalue = 0, i = bits % 5; i >= 0; i--, bits--)\n wvalue = (wvalue << 1) + BN_is_bit_set(p, bits);\n bn_gather5(tmp.d, top, powerbuf, wvalue);\n if (top & 7)\n while (bits >= 0) {\n for (wvalue = 0, i = 0; i < 5; i++, bits--)\n wvalue = (wvalue << 1) + BN_is_bit_set(p, bits);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);\n bn_mul_mont_gather5(tmp.d, tmp.d, powerbuf, np, n0, top,\n wvalue);\n } else {\n while (bits >= 0) {\n wvalue = bn_get_bits5(p->d, bits - 4);\n bits -= 5;\n bn_power5(tmp.d, tmp.d, powerbuf, np, n0, top, wvalue);\n }\n }\n ret = bn_from_montgomery(tmp.d, tmp.d, NULL, np, n0, top);\n tmp.top = top;\n bn_correct_top(&tmp);\n if (ret) {\n if (!BN_copy(rr, &tmp))\n ret = 0;\n goto err;\n }\n } else\n#endif\n {\n if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&tmp, top, powerbuf, 0, window))\n goto err;\n if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&am, top, powerbuf, 1, window))\n goto err;\n if (window > 1) {\n if (!BN_mod_mul_montgomery(&tmp, &am, &am, mont, ctx))\n goto err;\n if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&tmp, top, powerbuf, 2,\n window))\n goto err;\n for (i = 3; i < numPowers; i++) {\n if (!BN_mod_mul_montgomery(&tmp, &am, &tmp, mont, ctx))\n goto err;\n if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&tmp, top, powerbuf, i,\n window))\n goto err;\n }\n }\n bits--;\n for (wvalue = 0, i = bits % window; i >= 0; i--, bits--)\n wvalue = (wvalue << 1) + BN_is_bit_set(p, bits);\n if (!MOD_EXP_CTIME_COPY_FROM_PREBUF(&tmp, top, powerbuf, wvalue,\n window))\n goto err;\n while (bits >= 0) {\n wvalue = 0;\n for (i = 0; i < window; i++, bits--) {\n if (!BN_mod_mul_montgomery(&tmp, &tmp, &tmp, mont, ctx))\n goto err;\n wvalue = (wvalue << 1) + BN_is_bit_set(p, bits);\n }\n if (!MOD_EXP_CTIME_COPY_FROM_PREBUF(&am, top, powerbuf, wvalue,\n window))\n goto err;\n if (!BN_mod_mul_montgomery(&tmp, &tmp, &am, mont, ctx))\n goto err;\n }\n }\n#if defined(SPARC_T4_MONT)\n if (OPENSSL_sparcv9cap_P[0] & (SPARCV9_VIS3 | SPARCV9_PREFER_FPU)) {\n am.d[0] = 1;\n for (i = 1; i < top; i++)\n am.d[i] = 0;\n if (!BN_mod_mul_montgomery(rr, &tmp, &am, mont, ctx))\n goto err;\n } else\n#endif\n if (!BN_from_montgomery(rr, &tmp, mont, ctx))\n goto err;\n ret = 1;\n err:\n if (in_mont == NULL)\n BN_MONT_CTX_free(mont);\n if (powerbuf != NULL) {\n OPENSSL_cleanse(powerbuf, powerbufLen);\n OPENSSL_free(powerbufFree);\n }\n BN_CTX_end(ctx);\n return (ret);\n}', 'int BN_to_montgomery(BIGNUM *r, const BIGNUM *a, BN_MONT_CTX *mont,\n BN_CTX *ctx)\n{\n return BN_mod_mul_montgomery(r, a, &(mont->RR), mont, ctx);\n}', 'int BN_mod_mul_montgomery(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,\n BN_MONT_CTX *mont, BN_CTX *ctx)\n{\n BIGNUM *tmp;\n int ret = 0;\n#if defined(OPENSSL_BN_ASM_MONT) && defined(MONT_WORD)\n int num = mont->N.top;\n if (num > 1 && a->top == num && b->top == num) {\n if (bn_wexpand(r, num) == NULL)\n return (0);\n if (bn_mul_mont(r->d, a->d, b->d, mont->N.d, mont->n0, num)) {\n r->neg = a->neg ^ b->neg;\n r->top = num;\n bn_correct_top(r);\n return 1;\n }\n }\n#endif\n BN_CTX_start(ctx);\n tmp = BN_CTX_get(ctx);\n if (tmp == NULL)\n goto err;\n bn_check_top(tmp);\n if (a == b) {\n if (!BN_sqr(tmp, a, ctx))\n goto err;\n } else {\n if (!BN_mul(tmp, a, b, ctx))\n goto err;\n }\n#ifdef MONT_WORD\n if (!BN_from_montgomery_word(r, tmp, mont))\n goto err;\n#else\n if (!BN_from_montgomery(r, tmp, mont, ctx))\n goto err;\n#endif\n bn_check_top(r);\n ret = 1;\n err:\n BN_CTX_end(ctx);\n return (ret);\n}', 'int BN_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}', 'void bn_mul_part_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n,\n int tna, int tnb, BN_ULONG *t)\n{\n int i, j, n2 = n * 2;\n int c1, c2, neg;\n BN_ULONG ln, lo, *p;\n if (n < 8) {\n bn_mul_normal(r, a, n + tna, b, n + tnb);\n return;\n }\n c1 = bn_cmp_part_words(a, &(a[n]), tna, n - tna);\n c2 = bn_cmp_part_words(&(b[n]), b, tnb, tnb - n);\n neg = 0;\n switch (c1 * 3 + c2) {\n case -4:\n bn_sub_part_words(t, &(a[n]), a, tna, tna - n);\n bn_sub_part_words(&(t[n]), b, &(b[n]), tnb, n - tnb);\n break;\n case -3:\n case -2:\n bn_sub_part_words(t, &(a[n]), a, tna, tna - n);\n bn_sub_part_words(&(t[n]), &(b[n]), b, tnb, tnb - n);\n neg = 1;\n break;\n case -1:\n case 0:\n case 1:\n case 2:\n bn_sub_part_words(t, a, &(a[n]), tna, n - tna);\n bn_sub_part_words(&(t[n]), b, &(b[n]), tnb, n - tnb);\n neg = 1;\n break;\n case 3:\n case 4:\n bn_sub_part_words(t, a, &(a[n]), tna, n - tna);\n bn_sub_part_words(&(t[n]), &(b[n]), b, tnb, tnb - n);\n break;\n }\n# if 0\n if (n == 4) {\n bn_mul_comba4(&(t[n2]), t, &(t[n]));\n bn_mul_comba4(r, a, b);\n bn_mul_normal(&(r[n2]), &(a[n]), tn, &(b[n]), tn);\n memset(&r[n2 + tn * 2], 0, sizeof(*r) * (n2 - tn * 2));\n } else\n# endif\n if (n == 8) {\n bn_mul_comba8(&(t[n2]), t, &(t[n]));\n bn_mul_comba8(r, a, b);\n bn_mul_normal(&(r[n2]), &(a[n]), tna, &(b[n]), tnb);\n memset(&r[n2 + tna + tnb], 0, sizeof(*r) * (n2 - tna - tnb));\n } else {\n p = &(t[n2 * 2]);\n bn_mul_recursive(&(t[n2]), t, &(t[n]), n, 0, 0, p);\n bn_mul_recursive(r, a, b, n, 0, 0, p);\n i = n / 2;\n if (tna > tnb)\n j = tna - i;\n else\n j = tnb - i;\n if (j == 0) {\n bn_mul_recursive(&(r[n2]), &(a[n]), &(b[n]),\n i, tna - i, tnb - i, p);\n memset(&r[n2 + i * 2], 0, sizeof(*r) * (n2 - i * 2));\n } else if (j > 0) {\n bn_mul_part_recursive(&(r[n2]), &(a[n]), &(b[n]),\n i, tna - i, tnb - i, p);\n memset(&(r[n2 + tna + tnb]), 0,\n sizeof(BN_ULONG) * (n2 - tna - tnb));\n } else {\n memset(&r[n2], 0, sizeof(*r) * n2);\n if (tna < BN_MUL_RECURSIVE_SIZE_NORMAL\n && tnb < BN_MUL_RECURSIVE_SIZE_NORMAL) {\n bn_mul_normal(&(r[n2]), &(a[n]), tna, &(b[n]), tnb);\n } else {\n for (;;) {\n i /= 2;\n if (i < tna || i < tnb) {\n bn_mul_part_recursive(&(r[n2]),\n &(a[n]), &(b[n]),\n i, tna - i, tnb - i, p);\n break;\n } else if (i == tna || i == tnb) {\n bn_mul_recursive(&(r[n2]),\n &(a[n]), &(b[n]),\n i, tna - i, tnb - i, p);\n break;\n }\n }\n }\n }\n }\n c1 = (int)(bn_add_words(t, r, &(r[n2]), n2));\n if (neg) {\n c1 -= (int)(bn_sub_words(&(t[n2]), t, &(t[n2]), n2));\n } else {\n c1 += (int)(bn_add_words(&(t[n2]), &(t[n2]), t, n2));\n }\n c1 += (int)(bn_add_words(&(r[n]), &(r[n]), &(t[n2]), n2));\n if (c1) {\n p = &(r[n + n2]);\n lo = *p;\n ln = (lo + c1) & BN_MASK2;\n *p = ln;\n if (ln < (BN_ULONG)c1) {\n do {\n p++;\n lo = *p;\n ln = (lo + 1) & BN_MASK2;\n *p = ln;\n } while (ln == 0);\n }\n }\n}', 'int bn_cmp_part_words(const BN_ULONG *a, const BN_ULONG *b, int cl, int dl)\n{\n int n, i;\n n = cl - 1;\n if (dl < 0) {\n for (i = dl; i < 0; i++) {\n if (b[n - i] != 0)\n return -1;\n }\n }\n if (dl > 0) {\n for (i = dl; i > 0; i--) {\n if (a[n + i] != 0)\n return 1;\n }\n }\n return bn_cmp_words(a, b, cl);\n}', 'int bn_cmp_words(const BN_ULONG *a, const BN_ULONG *b, int n)\n{\n int i;\n BN_ULONG aa, bb;\n aa = a[n - 1];\n bb = b[n - 1];\n if (aa != bb)\n return ((aa > bb) ? 1 : -1);\n for (i = n - 2; i >= 0; i--) {\n aa = a[i];\n bb = b[i];\n if (aa != bb)\n return ((aa > bb) ? 1 : -1);\n }\n return (0);\n}']
265
0
https://github.com/openssl/openssl/blob/4cc968df403ed9321d0df722aba33323ae575ce0/crypto/bn/bn_ctx.c/#L276
static unsigned int BN_STACK_pop(BN_STACK *st) { return st->indexes[--(st->depth)]; }
['ECDSA_SIG *ossl_ecdsa_sign_sig(const unsigned char *dgst, int dgst_len,\n const BIGNUM *in_kinv, const BIGNUM *in_r,\n EC_KEY *eckey)\n{\n int ok = 0, i;\n BIGNUM *kinv = NULL, *s, *m = NULL;\n const BIGNUM *order, *ckinv;\n BN_CTX *ctx = NULL;\n const EC_GROUP *group;\n ECDSA_SIG *ret;\n const BIGNUM *priv_key;\n group = EC_KEY_get0_group(eckey);\n priv_key = EC_KEY_get0_private_key(eckey);\n if (group == NULL || priv_key == NULL) {\n ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_PASSED_NULL_PARAMETER);\n return NULL;\n }\n if (!EC_KEY_can_sign(eckey)) {\n ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, EC_R_CURVE_DOES_NOT_SUPPORT_SIGNING);\n return NULL;\n }\n ret = ECDSA_SIG_new();\n if (ret == NULL) {\n ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_MALLOC_FAILURE);\n return NULL;\n }\n ret->r = BN_new();\n ret->s = BN_new();\n if (ret->r == NULL || ret->s == NULL) {\n ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n s = ret->s;\n if ((ctx = BN_CTX_new()) == NULL\n || (m = BN_new()) == NULL) {\n ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n order = EC_GROUP_get0_order(group);\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_SIGN_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_SIGN_SIG, ERR_R_BN_LIB);\n goto err;\n }\n do {\n if (in_kinv == NULL || in_r == NULL) {\n if (!ecdsa_sign_setup(eckey, ctx, &kinv, &ret->r, dgst, dgst_len)) {\n ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_ECDSA_LIB);\n goto err;\n }\n ckinv = kinv;\n } else {\n ckinv = in_kinv;\n if (BN_copy(ret->r, in_r) == NULL) {\n ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n }\n if (!bn_to_mont_fixed_top(s, ret->r, group->mont_data, ctx)\n || !bn_mul_mont_fixed_top(s, s, priv_key, group->mont_data, ctx)) {\n ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);\n goto err;\n }\n if (!bn_mod_add_fixed_top(s, s, m, order)) {\n ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);\n goto err;\n }\n if (!bn_to_mont_fixed_top(s, s, group->mont_data, ctx)\n || !BN_mod_mul_montgomery(s, s, ckinv, group->mont_data, ctx)) {\n ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);\n goto err;\n }\n if (BN_is_zero(s)) {\n if (in_kinv != NULL && in_r != NULL) {\n ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, EC_R_NEED_NEW_SETUP_VALUES);\n goto err;\n }\n } else {\n break;\n }\n } while (1);\n ok = 1;\n err:\n if (!ok) {\n ECDSA_SIG_free(ret);\n ret = NULL;\n }\n BN_CTX_free(ctx);\n BN_clear_free(m);\n BN_clear_free(kinv);\n return ret;\n}', 'static int ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in,\n BIGNUM **kinvp, BIGNUM **rp,\n const unsigned char *dgst, int dlen)\n{\n BN_CTX *ctx = NULL;\n BIGNUM *k = NULL, *r = NULL, *X = NULL;\n const BIGNUM *order;\n EC_POINT *tmp_point = NULL;\n const EC_GROUP *group;\n int ret = 0;\n int order_bits;\n if (eckey == NULL || (group = EC_KEY_get0_group(eckey)) == NULL) {\n ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_PASSED_NULL_PARAMETER);\n return 0;\n }\n if (!EC_KEY_can_sign(eckey)) {\n ECerr(EC_F_ECDSA_SIGN_SETUP, EC_R_CURVE_DOES_NOT_SUPPORT_SIGNING);\n return 0;\n }\n if ((ctx = ctx_in) == NULL) {\n if ((ctx = BN_CTX_new()) == NULL) {\n ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_MALLOC_FAILURE);\n return 0;\n }\n }\n k = BN_new();\n r = BN_new();\n X = BN_new();\n if (k == NULL || r == NULL || X == NULL) {\n ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n if ((tmp_point = EC_POINT_new(group)) == NULL) {\n ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);\n goto err;\n }\n order = EC_GROUP_get0_order(group);\n order_bits = BN_num_bits(order);\n if (!BN_set_bit(k, order_bits)\n || !BN_set_bit(r, order_bits)\n || !BN_set_bit(X, order_bits))\n goto err;\n do {\n do {\n if (dgst != NULL) {\n if (!BN_generate_dsa_nonce(k, order,\n EC_KEY_get0_private_key(eckey),\n dgst, dlen, ctx)) {\n ECerr(EC_F_ECDSA_SIGN_SETUP,\n EC_R_RANDOM_NUMBER_GENERATION_FAILED);\n goto err;\n }\n } else {\n if (!BN_priv_rand_range(k, order)) {\n ECerr(EC_F_ECDSA_SIGN_SETUP,\n EC_R_RANDOM_NUMBER_GENERATION_FAILED);\n goto err;\n }\n }\n } while (BN_is_zero(k));\n if (!EC_POINT_mul(group, tmp_point, k, NULL, NULL, ctx)) {\n ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);\n goto err;\n }\n if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) ==\n NID_X9_62_prime_field) {\n if (!EC_POINT_get_affine_coordinates_GFp(group, tmp_point, X,\n NULL, ctx)) {\n ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);\n goto err;\n }\n }\n#ifndef OPENSSL_NO_EC2M\n else {\n if (!EC_POINT_get_affine_coordinates_GF2m(group, tmp_point, X,\n NULL, ctx)) {\n ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);\n goto err;\n }\n }\n#endif\n if (!BN_nnmod(r, X, order, ctx)) {\n ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);\n goto err;\n }\n } while (BN_is_zero(r));\n if (!ec_group_do_inverse_ord(group, k, k, ctx)) {\n ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);\n goto err;\n }\n BN_clear_free(*rp);\n BN_clear_free(*kinvp);\n *rp = r;\n *kinvp = k;\n ret = 1;\n err:\n if (!ret) {\n BN_clear_free(k);\n BN_clear_free(r);\n }\n if (ctx != ctx_in)\n BN_CTX_free(ctx);\n EC_POINT_free(tmp_point);\n BN_clear_free(X);\n return ret;\n}', 'int BN_mod_mul_montgomery(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,\n BN_MONT_CTX *mont, BN_CTX *ctx)\n{\n int ret = bn_mul_mont_fixed_top(r, a, b, mont, ctx);\n bn_correct_top(r);\n bn_check_top(r);\n return ret;\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(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 ret = 1;\n err:\n BN_CTX_end(ctx);\n return ret;\n}', 'void BN_CTX_start(BN_CTX *ctx)\n{\n CTXDBG_ENTRY("BN_CTX_start", ctx);\n if (ctx->err_stack || ctx->too_many)\n ctx->err_stack++;\n else if (!BN_STACK_push(&ctx->stack, ctx->used)) {\n BNerr(BN_F_BN_CTX_START, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n ctx->err_stack++;\n }\n CTXDBG_EXIT(ctx);\n}', 'int BN_sqr(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx)\n{\n int max, al;\n int ret = 0;\n BIGNUM *tmp, *rr;\n bn_check_top(a);\n al = a->top;\n if (al <= 0) {\n r->top = 0;\n r->neg = 0;\n return 1;\n }\n BN_CTX_start(ctx);\n rr = (a != r) ? r : BN_CTX_get(ctx);\n tmp = BN_CTX_get(ctx);\n if (rr == NULL || tmp == NULL)\n goto err;\n max = 2 * al;\n if (bn_wexpand(rr, max) == NULL)\n goto err;\n if (al == 4) {\n#ifndef BN_SQR_COMBA\n BN_ULONG t[8];\n bn_sqr_normal(rr->d, a->d, 4, t);\n#else\n bn_sqr_comba4(rr->d, a->d);\n#endif\n } else if (al == 8) {\n#ifndef BN_SQR_COMBA\n BN_ULONG t[16];\n bn_sqr_normal(rr->d, a->d, 8, t);\n#else\n bn_sqr_comba8(rr->d, a->d);\n#endif\n } else {\n#if defined(BN_RECURSION)\n if (al < BN_SQR_RECURSIVE_SIZE_NORMAL) {\n BN_ULONG t[BN_SQR_RECURSIVE_SIZE_NORMAL * 2];\n bn_sqr_normal(rr->d, a->d, al, t);\n } else {\n int j, k;\n j = BN_num_bits_word((BN_ULONG)al);\n j = 1 << (j - 1);\n k = j + j;\n if (al == j) {\n if (bn_wexpand(tmp, k * 2) == NULL)\n goto err;\n bn_sqr_recursive(rr->d, a->d, al, tmp->d);\n } else {\n if (bn_wexpand(tmp, max) == NULL)\n goto err;\n bn_sqr_normal(rr->d, a->d, al, tmp->d);\n }\n }\n#else\n if (bn_wexpand(tmp, max) == NULL)\n goto err;\n bn_sqr_normal(rr->d, a->d, al, tmp->d);\n#endif\n }\n rr->neg = 0;\n rr->top = max;\n bn_correct_top(rr);\n if (r != rr && BN_copy(r, rr) == NULL)\n goto err;\n ret = 1;\n err:\n bn_check_top(rr);\n bn_check_top(tmp);\n BN_CTX_end(ctx);\n return ret;\n}', 'void BN_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}']
266
0
https://github.com/openssl/openssl/blob/9f519addc09b2005fa8c6cde36e3267de02577bb/apps/speed.c/#L2118
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}']
267
0
https://github.com/openssl/openssl/blob/c922ebe23247ff9ee07310fa30647623c0547cd9/ssl/t1_lib.c/#L316
int tls_curve_allowed(SSL *s, const unsigned char *curve, int op) { const tls_curve_info *cinfo; if (curve[0]) return 1; if ((curve[1] < 1) || ((size_t)curve[1] > OSSL_NELEM(nid_list))) return 0; cinfo = &nid_list[curve[1] - 1]; # ifdef OPENSSL_NO_EC2M if (cinfo->flags & TLS_CURVE_CHAR2) return 0; # endif return ssl_security(s, op, cinfo->secbits, cinfo->nid, (void *)curve); }
['int tls_construct_ctos_key_share(SSL *s, WPACKET *pkt, int *al)\n{\n size_t i, sharessent = 0, num_curves = 0;\n const unsigned char *pcurves = NULL;\n if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_key_share)\n || !WPACKET_start_sub_packet_u16(pkt)\n || !WPACKET_start_sub_packet_u16(pkt)) {\n SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_KEY_SHARE, ERR_R_INTERNAL_ERROR);\n return 0;\n }\n pcurves = s->tlsext_supportedgroupslist;\n if (!tls1_get_curvelist(s, 0, &pcurves, &num_curves)) {\n SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_KEY_SHARE, ERR_R_INTERNAL_ERROR);\n return 0;\n }\n for (i = 0; i < num_curves && sharessent < 1; i++, pcurves += 2) {\n unsigned char *encodedPoint = NULL;\n unsigned int curve_id = 0;\n EVP_PKEY *key_share_key = NULL;\n size_t encodedlen;\n if (!tls_curve_allowed(s, pcurves, SSL_SECOP_CURVE_SUPPORTED))\n continue;\n if (s->s3->tmp.pkey != NULL) {\n SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_KEY_SHARE, ERR_R_INTERNAL_ERROR);\n return 0;\n }\n curve_id = (pcurves[0] << 8) | pcurves[1];\n key_share_key = ssl_generate_pkey_curve(curve_id);\n if (key_share_key == NULL) {\n SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_KEY_SHARE, ERR_R_EVP_LIB);\n return 0;\n }\n encodedlen = EVP_PKEY_get1_tls_encodedpoint(key_share_key,\n &encodedPoint);\n if (encodedlen == 0) {\n SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_KEY_SHARE, ERR_R_EC_LIB);\n EVP_PKEY_free(key_share_key);\n return 0;\n }\n if (!WPACKET_put_bytes_u16(pkt, curve_id)\n || !WPACKET_sub_memcpy_u16(pkt, encodedPoint, encodedlen)) {\n SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_KEY_SHARE, ERR_R_INTERNAL_ERROR);\n EVP_PKEY_free(key_share_key);\n OPENSSL_free(encodedPoint);\n return 0;\n }\n s->s3->group_id = curve_id;\n s->s3->tmp.pkey = key_share_key;\n sharessent++;\n OPENSSL_free(encodedPoint);\n }\n if (!WPACKET_close(pkt) || !WPACKET_close(pkt)) {\n SSLerr(SSL_F_TLS_CONSTRUCT_CTOS_KEY_SHARE, ERR_R_INTERNAL_ERROR);\n return 0;\n }\n return 1;\n}', 'int tls_curve_allowed(SSL *s, const unsigned char *curve, int op)\n{\n const tls_curve_info *cinfo;\n if (curve[0])\n return 1;\n if ((curve[1] < 1) || ((size_t)curve[1] > OSSL_NELEM(nid_list)))\n return 0;\n cinfo = &nid_list[curve[1] - 1];\n# ifdef OPENSSL_NO_EC2M\n if (cinfo->flags & TLS_CURVE_CHAR2)\n return 0;\n# endif\n return ssl_security(s, op, cinfo->secbits, cinfo->nid, (void *)curve);\n}']
268
0
https://github.com/openssl/openssl/blob/54b5fd537f7a7ac1874359fd42a4721b6839f7a1/crypto/pem/pem_all.c/#L267
EC_KEY *PEM_read_bio_ECPrivateKey(BIO *bp, EC_KEY **key, pem_password_cb *cb, void *u) { EVP_PKEY *pktmp; pktmp = PEM_read_bio_PrivateKey(bp, NULL, cb, u); return pkey_get_eckey(pktmp, key); }
['EC_KEY *PEM_read_bio_ECPrivateKey(BIO *bp, EC_KEY **key, pem_password_cb *cb,\n\t\t\t\t\t\t\tvoid *u)\n{\n\tEVP_PKEY *pktmp;\n\tpktmp = PEM_read_bio_PrivateKey(bp, NULL, cb, u);\n\treturn pkey_get_eckey(pktmp, key);\n}', 'EVP_PKEY *PEM_read_bio_PrivateKey(BIO *bp, EVP_PKEY **x, pem_password_cb *cb, void *u)\n\t{\n\tchar *nm=NULL;\n\tconst unsigned char *p=NULL;\n\tunsigned char *data=NULL;\n\tlong len;\n\tint slen;\n\tEVP_PKEY *ret=NULL;\n\tif (!PEM_bytes_read_bio(&data, &len, &nm, PEM_STRING_EVP_PKEY, bp, cb, u))\n\t\treturn NULL;\n\tp = data;\n\tif (strcmp(nm,PEM_STRING_PKCS8INF) == 0) {\n\t\tPKCS8_PRIV_KEY_INFO *p8inf;\n\t\tp8inf=d2i_PKCS8_PRIV_KEY_INFO(NULL, &p, len);\n\t\tif(!p8inf) goto p8err;\n\t\tret = EVP_PKCS82PKEY(p8inf);\n\t\tif(x) {\n\t\t\tif(*x) EVP_PKEY_free((EVP_PKEY *)*x);\n\t\t\t*x = ret;\n\t\t}\n\t\tPKCS8_PRIV_KEY_INFO_free(p8inf);\n\t} else if (strcmp(nm,PEM_STRING_PKCS8) == 0) {\n\t\tPKCS8_PRIV_KEY_INFO *p8inf;\n\t\tX509_SIG *p8;\n\t\tint klen;\n\t\tchar psbuf[PEM_BUFSIZE];\n\t\tp8 = d2i_X509_SIG(NULL, &p, len);\n\t\tif(!p8) goto p8err;\n\t\tif (cb) klen=cb(psbuf,PEM_BUFSIZE,0,u);\n\t\telse klen=PEM_def_callback(psbuf,PEM_BUFSIZE,0,u);\n\t\tif (klen <= 0) {\n\t\t\tPEMerr(PEM_F_PEM_READ_BIO_PRIVATEKEY,\n\t\t\t\t\tPEM_R_BAD_PASSWORD_READ);\n\t\t\tX509_SIG_free(p8);\n\t\t\tgoto err;\n\t\t}\n\t\tp8inf = PKCS8_decrypt(p8, psbuf, klen);\n\t\tX509_SIG_free(p8);\n\t\tif(!p8inf) goto p8err;\n\t\tret = EVP_PKCS82PKEY(p8inf);\n\t\tif(x) {\n\t\t\tif(*x) EVP_PKEY_free((EVP_PKEY *)*x);\n\t\t\t*x = ret;\n\t\t}\n\t\tPKCS8_PRIV_KEY_INFO_free(p8inf);\n\t} else if ((slen = pem_check_suffix(nm, "PRIVATE KEY")) > 0)\n\t\t{\n\t\tconst EVP_PKEY_ASN1_METHOD *ameth;\n\t\tameth = EVP_PKEY_asn1_find_str(NULL, nm, slen);\n\t\tif (!ameth || !ameth->old_priv_decode)\n\t\t\tgoto p8err;\n\t\tret=d2i_PrivateKey(ameth->pkey_id,x,&p,len);\n\t\t}\np8err:\n\tif (ret == NULL)\n\t\tPEMerr(PEM_F_PEM_READ_BIO_PRIVATEKEY,ERR_R_ASN1_LIB);\nerr:\n\tOPENSSL_free(nm);\n\tOPENSSL_cleanse(data, len);\n\tOPENSSL_free(data);\n\treturn(ret);\n\t}', 'int PEM_bytes_read_bio(unsigned char **pdata, long *plen, char **pnm, const char *name, BIO *bp,\n\t pem_password_cb *cb, void *u)\n\t{\n\tEVP_CIPHER_INFO cipher;\n\tchar *nm=NULL,*header=NULL;\n\tunsigned char *data=NULL;\n\tlong len;\n\tint ret = 0;\n\tfor (;;)\n\t\t{\n\t\tif (!PEM_read_bio(bp,&nm,&header,&data,&len)) {\n\t\t\tif(ERR_GET_REASON(ERR_peek_error()) ==\n\t\t\t\tPEM_R_NO_START_LINE)\n\t\t\t\tERR_add_error_data(2, "Expecting: ", name);\n\t\t\treturn 0;\n\t\t}\n\t\tif(check_pem(nm, name)) break;\n\t\tOPENSSL_free(nm);\n\t\tOPENSSL_free(header);\n\t\tOPENSSL_free(data);\n\t\t}\n\tif (!PEM_get_EVP_CIPHER_INFO(header,&cipher)) goto err;\n\tif (!PEM_do_header(&cipher,data,&len,cb,u)) goto err;\n\t*pdata = data;\n\t*plen = len;\n\tif (pnm)\n\t\t*pnm = nm;\n\tret = 1;\nerr:\n\tif (!ret || !pnm) OPENSSL_free(nm);\n\tOPENSSL_free(header);\n\tif (!ret) OPENSSL_free(data);\n\treturn ret;\n\t}', 'int PEM_read_bio(BIO *bp, char **name, char **header, unsigned char **data,\n\t long *len)\n\t{\n\tEVP_ENCODE_CTX ctx;\n\tint end=0,i,k,bl=0,hl=0,nohead=0;\n\tchar buf[256];\n\tBUF_MEM *nameB;\n\tBUF_MEM *headerB;\n\tBUF_MEM *dataB,*tmpB;\n\tnameB=BUF_MEM_new();\n\theaderB=BUF_MEM_new();\n\tdataB=BUF_MEM_new();\n\tif ((nameB == NULL) || (headerB == NULL) || (dataB == NULL))\n\t\t{\n\t\tBUF_MEM_free(nameB);\n\t\tBUF_MEM_free(headerB);\n\t\tBUF_MEM_free(dataB);\n\t\tPEMerr(PEM_F_PEM_READ_BIO,ERR_R_MALLOC_FAILURE);\n\t\treturn(0);\n\t\t}\n\tbuf[254]=\'\\0\';\n\tfor (;;)\n\t\t{\n\t\ti=BIO_gets(bp,buf,254);\n\t\tif (i <= 0)\n\t\t\t{\n\t\t\tPEMerr(PEM_F_PEM_READ_BIO,PEM_R_NO_START_LINE);\n\t\t\tgoto err;\n\t\t\t}\n\t\twhile ((i >= 0) && (buf[i] <= \' \')) i--;\n\t\tbuf[++i]=\'\\n\'; buf[++i]=\'\\0\';\n\t\tif (strncmp(buf,"-----BEGIN ",11) == 0)\n\t\t\t{\n\t\t\ti=strlen(&(buf[11]));\n\t\t\tif (strncmp(&(buf[11+i-6]),"-----\\n",6) != 0)\n\t\t\t\tcontinue;\n\t\t\tif (!BUF_MEM_grow(nameB,i+9))\n\t\t\t\t{\n\t\t\t\tPEMerr(PEM_F_PEM_READ_BIO,ERR_R_MALLOC_FAILURE);\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\tmemcpy(nameB->data,&(buf[11]),i-6);\n\t\t\tnameB->data[i-6]=\'\\0\';\n\t\t\tbreak;\n\t\t\t}\n\t\t}\n\thl=0;\n\tif (!BUF_MEM_grow(headerB,256))\n\t\t{ PEMerr(PEM_F_PEM_READ_BIO,ERR_R_MALLOC_FAILURE); goto err; }\n\theaderB->data[0]=\'\\0\';\n\tfor (;;)\n\t\t{\n\t\ti=BIO_gets(bp,buf,254);\n\t\tif (i <= 0) break;\n\t\twhile ((i >= 0) && (buf[i] <= \' \')) i--;\n\t\tbuf[++i]=\'\\n\'; buf[++i]=\'\\0\';\n\t\tif (buf[0] == \'\\n\') break;\n\t\tif (!BUF_MEM_grow(headerB,hl+i+9))\n\t\t\t{ PEMerr(PEM_F_PEM_READ_BIO,ERR_R_MALLOC_FAILURE); goto err; }\n\t\tif (strncmp(buf,"-----END ",9) == 0)\n\t\t\t{\n\t\t\tnohead=1;\n\t\t\tbreak;\n\t\t\t}\n\t\tmemcpy(&(headerB->data[hl]),buf,i);\n\t\theaderB->data[hl+i]=\'\\0\';\n\t\thl+=i;\n\t\t}\n\tbl=0;\n\tif (!BUF_MEM_grow(dataB,1024))\n\t\t{ PEMerr(PEM_F_PEM_READ_BIO,ERR_R_MALLOC_FAILURE); goto err; }\n\tdataB->data[0]=\'\\0\';\n\tif (!nohead)\n\t\t{\n\t\tfor (;;)\n\t\t\t{\n\t\t\ti=BIO_gets(bp,buf,254);\n\t\t\tif (i <= 0) break;\n\t\t\twhile ((i >= 0) && (buf[i] <= \' \')) i--;\n\t\t\tbuf[++i]=\'\\n\'; buf[++i]=\'\\0\';\n\t\t\tif (i != 65) end=1;\n\t\t\tif (strncmp(buf,"-----END ",9) == 0)\n\t\t\t\tbreak;\n\t\t\tif (i > 65) break;\n\t\t\tif (!BUF_MEM_grow_clean(dataB,i+bl+9))\n\t\t\t\t{\n\t\t\t\tPEMerr(PEM_F_PEM_READ_BIO,ERR_R_MALLOC_FAILURE);\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\tmemcpy(&(dataB->data[bl]),buf,i);\n\t\t\tdataB->data[bl+i]=\'\\0\';\n\t\t\tbl+=i;\n\t\t\tif (end)\n\t\t\t\t{\n\t\t\t\tbuf[0]=\'\\0\';\n\t\t\t\ti=BIO_gets(bp,buf,254);\n\t\t\t\tif (i <= 0) break;\n\t\t\t\twhile ((i >= 0) && (buf[i] <= \' \')) i--;\n\t\t\t\tbuf[++i]=\'\\n\'; buf[++i]=\'\\0\';\n\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\telse\n\t\t{\n\t\ttmpB=headerB;\n\t\theaderB=dataB;\n\t\tdataB=tmpB;\n\t\tbl=hl;\n\t\t}\n\ti=strlen(nameB->data);\n\tif (\t(strncmp(buf,"-----END ",9) != 0) ||\n\t\t(strncmp(nameB->data,&(buf[9]),i) != 0) ||\n\t\t(strncmp(&(buf[9+i]),"-----\\n",6) != 0))\n\t\t{\n\t\tPEMerr(PEM_F_PEM_READ_BIO,PEM_R_BAD_END_LINE);\n\t\tgoto err;\n\t\t}\n\tEVP_DecodeInit(&ctx);\n\ti=EVP_DecodeUpdate(&ctx,\n\t\t(unsigned char *)dataB->data,&bl,\n\t\t(unsigned char *)dataB->data,bl);\n\tif (i < 0)\n\t\t{\n\t\tPEMerr(PEM_F_PEM_READ_BIO,PEM_R_BAD_BASE64_DECODE);\n\t\tgoto err;\n\t\t}\n\ti=EVP_DecodeFinal(&ctx,(unsigned char *)&(dataB->data[bl]),&k);\n\tif (i < 0)\n\t\t{\n\t\tPEMerr(PEM_F_PEM_READ_BIO,PEM_R_BAD_BASE64_DECODE);\n\t\tgoto err;\n\t\t}\n\tbl+=k;\n\tif (bl == 0) goto err;\n\t*name=nameB->data;\n\t*header=headerB->data;\n\t*data=(unsigned char *)dataB->data;\n\t*len=bl;\n\tOPENSSL_free(nameB);\n\tOPENSSL_free(headerB);\n\tOPENSSL_free(dataB);\n\treturn(1);\nerr:\n\tBUF_MEM_free(nameB);\n\tBUF_MEM_free(headerB);\n\tBUF_MEM_free(dataB);\n\treturn(0);\n\t}', 'BUF_MEM *BUF_MEM_new(void)\n\t{\n\tBUF_MEM *ret;\n\tret=OPENSSL_malloc(sizeof(BUF_MEM));\n\tif (ret == NULL)\n\t\t{\n\t\tBUFerr(BUF_F_BUF_MEM_NEW,ERR_R_MALLOC_FAILURE);\n\t\treturn(NULL);\n\t\t}\n\tret->length=0;\n\tret->max=0;\n\tret->data=NULL;\n\treturn(ret);\n\t}', 'void *CRYPTO_malloc(int num, const char *file, int line)\n\t{\n\tvoid *ret = NULL;\n\tif (num <= 0) return NULL;\n\tallow_customize = 0;\n\tif (malloc_debug_func != NULL)\n\t\t{\n\t\tallow_customize_debug = 0;\n\t\tmalloc_debug_func(NULL, num, file, line, 0);\n\t\t}\n\tret = malloc_ex_func(num,file,line);\n#ifdef LEVITTE_DEBUG_MEM\n\tfprintf(stderr, "LEVITTE_DEBUG_MEM: > 0x%p (%d)\\n", ret, num);\n#endif\n\tif (malloc_debug_func != NULL)\n\t\tmalloc_debug_func(ret, num, file, line, 1);\n#ifndef OPENSSL_CPUID_OBJ\n if(ret && (num > 2048))\n\t{\textern unsigned char cleanse_ctr;\n ((unsigned char *)ret)[0] = cleanse_ctr;\n\t}\n#endif\n\treturn ret;\n\t}', 'void ERR_put_error(int lib, int func, int reason, const char *file,\n\t int line)\n\t{\n\tERR_STATE *es;\n#ifdef _OSD_POSIX\n\tif (strncmp(file,"*POSIX(", sizeof("*POSIX(")-1) == 0) {\n\t\tchar *end;\n\t\tfile += sizeof("*POSIX(")-1;\n\t\tend = &file[strlen(file)-1];\n\t\tif (*end == \')\')\n\t\t\t*end = \'\\0\';\n\t\tif ((end = strrchr(file, \'/\')) != NULL)\n\t\t\tfile = &end[1];\n\t}\n#endif\n\tes=ERR_get_state();\n\tes->top=(es->top+1)%ERR_NUM_ERRORS;\n\tif (es->top == es->bottom)\n\t\tes->bottom=(es->bottom+1)%ERR_NUM_ERRORS;\n\tes->err_flags[es->top]=0;\n\tes->err_buffer[es->top]=ERR_PACK(lib,func,reason);\n\tes->err_file[es->top]=file;\n\tes->err_line[es->top]=line;\n\terr_clear_data(es,es->top);\n\t}', 'void BUF_MEM_free(BUF_MEM *a)\n\t{\n\tif(a == NULL)\n\t return;\n\tif (a->data != NULL)\n\t\t{\n\t\tmemset(a->data,0,(unsigned int)a->max);\n\t\tOPENSSL_free(a->data);\n\t\t}\n\tOPENSSL_free(a);\n\t}', 'unsigned long ERR_peek_error(void)\n\t{ return(get_error_values(0,0,NULL,NULL,NULL,NULL)); }']
269
0
https://github.com/openssl/openssl/blob/3b5873567d24bf0d8bc2a175848e716e295d6c94/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 speed_main(int argc, char **argv)\n{\n ENGINE *e = NULL;\n int (*loopfunc)(void *args);\n loopargs_t *loopargs = NULL;\n int async_init = 0;\n int loopargs_len = 0;\n char *prog;\n const char *engine_id = NULL;\n const EVP_CIPHER *evp_cipher = NULL;\n double d = 0.0;\n OPTION_CHOICE o;\n int multiblock = 0, pr_header = 0;\n int doit[ALGOR_NUM] = { 0 };\n int ret = 1, i, k, misalign = 0;\n long count = 0;\n#ifndef NO_FORK\n int multi = 0;\n#endif\n unsigned int async_jobs = 0;\n#if !defined(OPENSSL_NO_RSA) || !defined(OPENSSL_NO_DSA) \\\n || !defined(OPENSSL_NO_EC)\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 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#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 const unsigned int rsa_bits[RSA_NUM] = {\n 512, 1024, 2048, 3072, 4096, 7680, 15360\n };\n static const unsigned char *rsa_data[RSA_NUM] = {\n test512, test1024, test2048, test3072, test4096, test7680, test15360\n };\n static const 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 int rsa_doit[RSA_NUM] = { 0 };\n int primes = RSA_DEFAULT_PRIME_NUM;\n#endif\n#ifndef OPENSSL_NO_DSA\n static const unsigned int dsa_bits[DSA_NUM] = { 512, 1024, 2048 };\n int dsa_doit[DSA_NUM] = { 0 };\n#endif\n#ifndef OPENSSL_NO_EC\n static const 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 const 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 int ecdsa_doit[EC_NUM] = { 0 };\n int ecdh_doit[EC_NUM] = { 0 };\n#endif\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_md = NULL;\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 is 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 if (async_jobs > 99999) {\n BIO_printf(bio_err,\n "%s: too many async_jobs\\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#ifdef OPENSSL_NO_MULTIBLOCK\n BIO_printf(bio_err,\n "%s: -mb specified but multi-block support is disabled\\n",\n prog);\n goto end;\n#endif\n break;\n case OPT_R_CASES:\n if (!opt_rand(o))\n goto end;\n break;\n case OPT_PRIMES:\n if (!opt_int(opt_arg(), &primes))\n goto end;\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 if (strcmp(*argv, "openssl") == 0)\n continue;\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 if (strcmp(*argv, "aes") == 0) {\n doit[D_CBC_128_AES] = doit[D_CBC_192_AES] = doit[D_CBC_256_AES] = 1;\n continue;\n }\n#ifndef OPENSSL_NO_CAMELLIA\n if (strcmp(*argv, "camellia") == 0) {\n doit[D_CBC_128_CML] = doit[D_CBC_192_CML] = 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 async_init = ASYNC_init_thread(async_jobs, async_jobs);\n if (!async_init) {\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 =\n 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 =\n app_malloc((int)BUFSIZE + MAX_MISALIGNMENT + 1, "input buffer");\n loopargs[i].buf2_malloc =\n 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#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 e = 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#ifndef OPENSSL_NO_RSA\n for (i = 0; i < RSA_NUM; i++)\n rsa_doit[i] = 1;\n#endif\n#ifndef OPENSSL_NO_DSA\n for (i = 0; i < DSA_NUM; i++)\n dsa_doit[i] = 1;\n#endif\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 if (primes > RSA_DEFAULT_PRIME_NUM) {\n break;\n }\n for (k = 0; k < RSA_NUM; k++) {\n const unsigned char *p;\n p = rsa_data[k];\n loopargs[i].rsa_key[k] =\n d2i_RSAPrivateKey(NULL, &p, rsa_data_length[k]);\n if (loopargs[i].rsa_key[k] == NULL) {\n BIO_printf(bio_err,\n "internal error loading RSA key number %d\\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_dsa(512);\n loopargs[i].dsa_key[1] = get_dsa(1024);\n loopargs[i].dsa_key[2] = get_dsa(2048);\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 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#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 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 c[D_RAND][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 c[D_RAND][i] = c[D_RAND][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] == 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] == 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] == 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] == 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 for (i = R_EC_P192; i <= R_EC_P521; i++) {\n ecdh_c[i][0] = ecdh_c[i - 1][0] / 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] == 0) {\n ecdh_c[i][0] = 1;\n }\n }\n }\n ecdh_c[R_EC_K163][0] = 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 if (ecdh_doit[i] <= 1 && ecdh_c[i][0] == 0)\n ecdh_doit[i] = 0;\n else {\n if (ecdh_c[i][0] == 0) {\n ecdh_c[i][0] = 1;\n }\n }\n }\n ecdh_c[R_EC_B163][0] = 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 if (ecdh_doit[i] <= 1 && ecdh_c[i][0] == 0)\n ecdh_doit[i] = 0;\n else {\n if (ecdh_c[i][0] == 0) {\n ecdh_c[i][0] = 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 if (doit[D_HMAC]) {\n static const char hmac_key[] = "This is a key...";\n int len = strlen(hmac_key);\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, hmac_key, len, 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],\n 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],\n 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],\n 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],\n 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],\n 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],\n lengths[testnum]);\n Time_F(START);\n count =\n 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 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 =\n 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 =\n 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 =\n 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 =\n 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 =\n 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 =\n 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 =\n CRYPTO_gcm128_new(&aes_ks1, (block128_f) AES_encrypt);\n CRYPTO_gcm128_setiv(loopargs[i].gcm_ctx,\n (unsigned char *)"0123456789ab", 12);\n }\n for (testnum = 0; testnum < SIZE_NUM; testnum++) {\n print_message(names[D_GHASH], c[D_GHASH][testnum],\n 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#ifndef OPENSSL_NO_CAMELLIA\n if (doit[D_CBC_128_CML]) {\n if (async_jobs > 0) {\n BIO_printf(bio_err, "Async mode is not supported with %s\\n",\n names[D_CBC_128_CML]);\n doit[D_CBC_128_CML] = 0;\n }\n for (testnum = 0; testnum < SIZE_NUM && async_init == 0; testnum++) {\n print_message(names[D_CBC_128_CML], c[D_CBC_128_CML][testnum],\n lengths[testnum]);\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 (size_t)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 if (async_jobs > 0) {\n BIO_printf(bio_err, "Async mode is not supported with %s\\n",\n names[D_CBC_192_CML]);\n doit[D_CBC_192_CML] = 0;\n }\n for (testnum = 0; testnum < SIZE_NUM && async_init == 0; 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 (size_t)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 if (async_jobs > 0) {\n BIO_printf(bio_err, "Async mode is not supported with %s\\n",\n names[D_CBC_256_CML]);\n doit[D_CBC_256_CML] = 0;\n }\n for (testnum = 0; testnum < SIZE_NUM && async_init == 0; testnum++) {\n print_message(names[D_CBC_256_CML], c[D_CBC_256_CML][testnum],\n lengths[testnum]);\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 (size_t)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 if (async_jobs > 0) {\n BIO_printf(bio_err, "Async mode is not supported with %s\\n",\n names[D_CBC_IDEA]);\n doit[D_CBC_IDEA] = 0;\n }\n for (testnum = 0; testnum < SIZE_NUM && async_init == 0; testnum++) {\n print_message(names[D_CBC_IDEA], c[D_CBC_IDEA][testnum],\n lengths[testnum]);\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 (size_t)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 if (async_jobs > 0) {\n BIO_printf(bio_err, "Async mode is not supported with %s\\n",\n names[D_CBC_SEED]);\n doit[D_CBC_SEED] = 0;\n }\n for (testnum = 0; testnum < SIZE_NUM && async_init == 0; testnum++) {\n print_message(names[D_CBC_SEED], c[D_CBC_SEED][testnum],\n lengths[testnum]);\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 (size_t)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 if (async_jobs > 0) {\n BIO_printf(bio_err, "Async mode is not supported with %s\\n",\n names[D_CBC_RC2]);\n doit[D_CBC_RC2] = 0;\n }\n for (testnum = 0; testnum < SIZE_NUM && async_init == 0; testnum++) {\n print_message(names[D_CBC_RC2], c[D_CBC_RC2][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_RC2][testnum]); count++)\n RC2_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,\n (size_t)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 if (async_jobs > 0) {\n BIO_printf(bio_err, "Async mode is not supported with %s\\n",\n names[D_CBC_RC5]);\n doit[D_CBC_RC5] = 0;\n }\n for (testnum = 0; testnum < SIZE_NUM && async_init == 0; testnum++) {\n print_message(names[D_CBC_RC5], c[D_CBC_RC5][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_RC5][testnum]); count++)\n RC5_32_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,\n (size_t)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 if (async_jobs > 0) {\n BIO_printf(bio_err, "Async mode is not supported with %s\\n",\n names[D_CBC_BF]);\n doit[D_CBC_BF] = 0;\n }\n for (testnum = 0; testnum < SIZE_NUM && async_init == 0; testnum++) {\n print_message(names[D_CBC_BF], c[D_CBC_BF][testnum],\n lengths[testnum]);\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 (size_t)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 if (async_jobs > 0) {\n BIO_printf(bio_err, "Async mode is not supported with %s\\n",\n names[D_CBC_CAST]);\n doit[D_CBC_CAST] = 0;\n }\n for (testnum = 0; testnum < SIZE_NUM && async_init == 0; testnum++) {\n print_message(names[D_CBC_CAST], c[D_CBC_CAST][testnum],\n lengths[testnum]);\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 (size_t)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_RAND]) {\n for (testnum = 0; testnum < SIZE_NUM; testnum++) {\n print_message(names[D_RAND], c[D_RAND][testnum], lengths[testnum]);\n Time_F(START);\n count = run_benchmark(async_jobs, RAND_bytes_loop, loopargs);\n d = Time_F(STOP);\n print_result(D_RAND, testnum, count, d);\n }\n }\n if (doit[D_EVP]) {\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 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,\n key16, iv);\n else\n EVP_EncryptInit_ex(loopargs[k].ctx, evp_cipher, NULL,\n key16, iv);\n EVP_CIPHER_CTX_set_padding(loopargs[k].ctx, 0);\n }\n switch (EVP_CIPHER_mode(evp_cipher)) {\n case EVP_CIPH_CCM_MODE:\n loopfunc = EVP_Update_loop_ccm;\n break;\n default:\n loopfunc = EVP_Update_loop;\n }\n Time_F(START);\n count = run_benchmark(async_jobs, loopfunc, 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 if (primes > 2) {\n BIGNUM *bn = BN_new();\n if (bn == NULL)\n goto end;\n if (!BN_set_word(bn, RSA_F4)) {\n BN_free(bn);\n goto end;\n }\n BIO_printf(bio_err, "Generate multi-prime RSA key for %s\\n",\n rsa_choices[testnum].name);\n loopargs[i].rsa_key[testnum] = RSA_new();\n if (loopargs[i].rsa_key[testnum] == NULL) {\n BN_free(bn);\n goto end;\n }\n if (!RSA_generate_multi_prime_key(loopargs[i].rsa_key[testnum],\n rsa_bits[testnum],\n primes, bn, NULL)) {\n BN_free(bn);\n goto end;\n }\n BN_free(bn);\n }\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],\n 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] = (double)count / d;\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],\n 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] = (double)count / d;\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 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],\n 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] = (double)count / d;\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],\n 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] = (double)count / d;\n }\n if (rsa_count <= 1) {\n for (testnum++; testnum < DSA_NUM; testnum++)\n dsa_doit[testnum] = 0;\n }\n }\n#endif\n#ifndef OPENSSL_NO_EC\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] =\n 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,\n 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] = (double)count / d;\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,\n 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] = (double)count / d;\n }\n if (rsa_count <= 1) {\n for (testnum++; testnum < EC_NUM; testnum++)\n ecdsa_doit[testnum] = 0;\n }\n }\n }\n for (testnum = 0; testnum < EC_NUM; testnum++) {\n int ecdh_checks = 1;\n if (!ecdh_doit[testnum])\n continue;\n for (i = 0; i < loopargs_len; i++) {\n EVP_PKEY_CTX *kctx = NULL;\n EVP_PKEY_CTX *test_ctx = NULL;\n EVP_PKEY_CTX *ctx = NULL;\n EVP_PKEY *key_A = NULL;\n EVP_PKEY *key_B = NULL;\n size_t outlen;\n size_t test_outlen;\n if (ERR_peek_error()) {\n BIO_printf(bio_err,\n "WARNING: the error queue contains previous unhandled errors.\\n");\n ERR_print_errors(bio_err);\n }\n kctx = EVP_PKEY_CTX_new_id(test_curves[testnum], NULL);\n if (!kctx) {\n EVP_PKEY_CTX *pctx = NULL;\n EVP_PKEY *params = NULL;\n unsigned long error = ERR_peek_error();\n if (error == ERR_peek_last_error() &&\n ERR_GET_LIB(error) == ERR_LIB_EVP &&\n ERR_GET_FUNC(error) == EVP_F_INT_CTX_NEW &&\n ERR_GET_REASON(error) == EVP_R_UNSUPPORTED_ALGORITHM)\n ERR_get_error();\n if (ERR_peek_error()) {\n BIO_printf(bio_err,\n "Unhandled error in the error queue during ECDH init.\\n");\n ERR_print_errors(bio_err);\n rsa_count = 1;\n break;\n }\n if (\n !(pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL)) ||\n !EVP_PKEY_paramgen_init(pctx) ||\n !EVP_PKEY_CTX_set_ec_paramgen_curve_nid(pctx,\n test_curves\n [testnum]) ||\n !EVP_PKEY_paramgen(pctx, &params)) {\n ecdh_checks = 0;\n BIO_printf(bio_err, "ECDH EC params init failure.\\n");\n ERR_print_errors(bio_err);\n rsa_count = 1;\n break;\n }\n kctx = EVP_PKEY_CTX_new(params, NULL);\n EVP_PKEY_free(params);\n params = NULL;\n EVP_PKEY_CTX_free(pctx);\n pctx = NULL;\n }\n if (kctx == NULL ||\n !EVP_PKEY_keygen_init(kctx) ) {\n ecdh_checks = 0;\n BIO_printf(bio_err, "ECDH keygen failure.\\n");\n ERR_print_errors(bio_err);\n rsa_count = 1;\n break;\n }\n if (!EVP_PKEY_keygen(kctx, &key_A) ||\n !EVP_PKEY_keygen(kctx, &key_B) ||\n !(ctx = EVP_PKEY_CTX_new(key_A, NULL)) ||\n !EVP_PKEY_derive_init(ctx) ||\n !EVP_PKEY_derive_set_peer(ctx, key_B) ||\n !EVP_PKEY_derive(ctx, NULL, &outlen) ||\n outlen == 0 ||\n outlen > MAX_ECDH_SIZE ) {\n ecdh_checks = 0;\n BIO_printf(bio_err, "ECDH key generation failure.\\n");\n ERR_print_errors(bio_err);\n rsa_count = 1;\n break;\n }\n if (!(test_ctx = EVP_PKEY_CTX_new(key_B, NULL)) ||\n !EVP_PKEY_derive_init(test_ctx) ||\n !EVP_PKEY_derive_set_peer(test_ctx, key_A) ||\n !EVP_PKEY_derive(test_ctx, NULL, &test_outlen) ||\n !EVP_PKEY_derive(ctx, loopargs[i].secret_a, &outlen) ||\n !EVP_PKEY_derive(test_ctx, loopargs[i].secret_b, &test_outlen) ||\n test_outlen != outlen ) {\n ecdh_checks = 0;\n BIO_printf(bio_err, "ECDH computation failure.\\n");\n ERR_print_errors(bio_err);\n rsa_count = 1;\n break;\n }\n if (CRYPTO_memcmp(loopargs[i].secret_a,\n loopargs[i].secret_b, outlen)) {\n 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 loopargs[i].ecdh_ctx[testnum] = ctx;\n loopargs[i].outlen[testnum] = outlen;\n EVP_PKEY_CTX_free(kctx);\n kctx = NULL;\n EVP_PKEY_CTX_free(test_ctx);\n test_ctx = NULL;\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 =\n run_benchmark(async_jobs, ECDH_EVP_derive_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] = (double)count / d;\n rsa_count = count;\n }\n if (rsa_count <= 1) {\n for (testnum++; testnum < EC_NUM; testnum++)\n ecdh_doit[testnum] = 0;\n }\n }\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 printf("%s ", AES_options());\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], 1.0 / rsa_results[k][0], 1.0 / rsa_results[k][1],\n rsa_results[k][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], 1.0 / dsa_results[k][0], 1.0 / dsa_results[k][1],\n dsa_results[k][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 1.0 / ecdsa_results[k][0], 1.0 / ecdsa_results[k][1],\n ecdsa_results[k][0], ecdsa_results[k][1]);\n }\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 1.0 / ecdh_results[k][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#ifndef OPENSSL_NO_RSA\n for (k = 0; k < RSA_NUM; k++)\n RSA_free(loopargs[i].rsa_key[k]);\n#endif\n#ifndef OPENSSL_NO_DSA\n for (k = 0; k < DSA_NUM; k++)\n DSA_free(loopargs[i].dsa_key[k]);\n#endif\n#ifndef OPENSSL_NO_EC\n for (k = 0; k < EC_NUM; k++) {\n EC_KEY_free(loopargs[i].ecdsa[k]);\n EVP_PKEY_CTX_free(loopargs[i].ecdh_ctx[k]);\n }\n OPENSSL_free(loopargs[i].secret_a);\n OPENSSL_free(loopargs[i].secret_b);\n#endif\n }\n if (async_jobs > 0) {\n for (i = 0; i < loopargs_len; i++)\n ASYNC_WAIT_CTX_free(loopargs[i].wait_ctx);\n }\n if (async_init) {\n ASYNC_cleanup_thread();\n }\n OPENSSL_free(loopargs);\n release_engine(e);\n return ret;\n}', 'int RSA_generate_multi_prime_key(RSA *rsa, int bits, int primes,\n BIGNUM *e_value, BN_GENCB *cb)\n{\n if (rsa->meth->rsa_multi_prime_keygen != NULL)\n return rsa->meth->rsa_multi_prime_keygen(rsa, bits, primes,\n e_value, cb);\n return rsa_builtin_keygen(rsa, bits, primes, e_value, cb);\n}', 'int BN_set_word(BIGNUM *a, BN_ULONG w)\n{\n bn_check_top(a);\n if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)\n return 0;\n a->neg = 0;\n a->d[0] = w;\n a->top = (w ? 1 : 0);\n bn_check_top(a);\n return 1;\n}', 'static ossl_inline BIGNUM *bn_expand(BIGNUM *a, int bits)\n{\n if (bits > (INT_MAX - BN_BITS2 + 1))\n return NULL;\n if (((bits+BN_BITS2-1)/BN_BITS2) <= (a)->dmax)\n return a;\n return bn_expand2((a),(bits+BN_BITS2-1)/BN_BITS2);\n}', 'BIGNUM *bn_expand2(BIGNUM *b, int words)\n{\n bn_check_top(b);\n if (words > b->dmax) {\n BN_ULONG *a = bn_expand_internal(b, words);\n if (!a)\n return NULL;\n if (b->d) {\n OPENSSL_cleanse(b->d, b->dmax * sizeof(b->d[0]));\n bn_free_d(b);\n }\n b->d = a;\n b->dmax = words;\n }\n bn_check_top(b);\n return b;\n}', 'static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)\n{\n BN_ULONG *a = NULL;\n bn_check_top(b);\n if (words > (INT_MAX / (4 * BN_BITS2))) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);\n return NULL;\n }\n if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);\n return NULL;\n }\n if (BN_get_flags(b, BN_FLG_SECURE))\n a = OPENSSL_secure_zalloc(words * sizeof(*a));\n else\n a = OPENSSL_zalloc(words * sizeof(*a));\n if (a == NULL) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);\n return NULL;\n }\n assert(b->top <= words);\n if (b->top > 0)\n memcpy(a, b->d, sizeof(*a) * b->top);\n return a;\n}', 'void *CRYPTO_zalloc(size_t num, const char *file, int line)\n{\n void *ret = CRYPTO_malloc(num, file, line);\n FAILTEST();\n if (ret != NULL)\n memset(ret, 0, num);\n return ret;\n}', 'void *CRYPTO_malloc(size_t num, const char *file, int line)\n{\n void *ret = NULL;\n 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}']
270
0
https://github.com/openssl/openssl/blob/95dc05bc6d0dfe0f3f3681f5e27afbc3f7a35eea/crypto/des/des_enc.c/#L144
void des_encrypt(DES_LONG *data, des_key_schedule ks, int enc) { register DES_LONG l,r,t,u; #ifdef DES_PTR register const unsigned char *des_SP=(const unsigned char *)des_SPtrans; #endif #ifndef DES_UNROLL register int i; #endif register DES_LONG *s; r=data[0]; l=data[1]; IP(r,l); r=ROTATE(r,29)&0xffffffffL; l=ROTATE(l,29)&0xffffffffL; s=(DES_LONG *)ks; if (enc) { #ifdef DES_UNROLL D_ENCRYPT(l,r, 0); D_ENCRYPT(r,l, 2); D_ENCRYPT(l,r, 4); D_ENCRYPT(r,l, 6); D_ENCRYPT(l,r, 8); D_ENCRYPT(r,l,10); D_ENCRYPT(l,r,12); D_ENCRYPT(r,l,14); D_ENCRYPT(l,r,16); D_ENCRYPT(r,l,18); D_ENCRYPT(l,r,20); D_ENCRYPT(r,l,22); D_ENCRYPT(l,r,24); D_ENCRYPT(r,l,26); D_ENCRYPT(l,r,28); D_ENCRYPT(r,l,30); #else for (i=0; i<32; i+=8) { D_ENCRYPT(l,r,i+0); D_ENCRYPT(r,l,i+2); D_ENCRYPT(l,r,i+4); D_ENCRYPT(r,l,i+6); } #endif } else { #ifdef DES_UNROLL D_ENCRYPT(l,r,30); D_ENCRYPT(r,l,28); D_ENCRYPT(l,r,26); D_ENCRYPT(r,l,24); D_ENCRYPT(l,r,22); D_ENCRYPT(r,l,20); D_ENCRYPT(l,r,18); D_ENCRYPT(r,l,16); D_ENCRYPT(l,r,14); D_ENCRYPT(r,l,12); D_ENCRYPT(l,r,10); D_ENCRYPT(r,l, 8); D_ENCRYPT(l,r, 6); D_ENCRYPT(r,l, 4); D_ENCRYPT(l,r, 2); D_ENCRYPT(r,l, 0); #else for (i=30; i>0; i-=8) { D_ENCRYPT(l,r,i-0); D_ENCRYPT(r,l,i-2); D_ENCRYPT(l,r,i-4); D_ENCRYPT(r,l,i-6); } #endif } l=ROTATE(l,3)&0xffffffffL; r=ROTATE(r,3)&0xffffffffL; FP(r,l); data[0]=l; data[1]=r; l=r=t=u=0; }
['void des_string_to_key(const char *str, des_cblock key)\n\t{\n\tdes_key_schedule ks;\n\tint i,length;\n\tregister unsigned char j;\n\tmemset(key,0,8);\n\tlength=strlen(str);\n#ifdef OLD_STR_TO_KEY\n\tfor (i=0; i<length; i++)\n\t\tkey[i%8]^=(str[i]<<1);\n#else\n\tfor (i=0; i<length; i++)\n\t\t{\n\t\tj=str[i];\n\t\tif ((i%16) < 8)\n\t\t\tkey[i%8]^=(j<<1);\n\t\telse\n\t\t\t{\n\t\t\tj=((j<<4)&0xf0)|((j>>4)&0x0f);\n\t\t\tj=((j<<2)&0xcc)|((j>>2)&0x33);\n\t\t\tj=((j<<1)&0xaa)|((j>>1)&0x55);\n\t\t\tkey[7-(i%8)]^=j;\n\t\t\t}\n\t\t}\n#endif\n\tdes_set_odd_parity(key);\n\ti=des_check_key;\n\tdes_check_key=0;\n\tdes_set_key(key,ks);\n\tdes_check_key=i;\n\tdes_cbc_cksum((unsigned char*)str,key,length,ks,key);\n\tmemset(ks,0,sizeof(ks));\n\tdes_set_odd_parity(key);\n\t}', 'DES_LONG des_cbc_cksum(const unsigned char *in, des_cblock out, long length,\n\t des_key_schedule schedule, const des_cblock iv)\n\t{\n\tregister DES_LONG tout0,tout1,tin0,tin1;\n\tregister long l=length;\n\tDES_LONG tin[2];\n\tc2l(iv,tout0);\n\tc2l(iv,tout1);\n\tfor (; l>0; l-=8)\n\t\t{\n\t\tif (l >= 8)\n\t\t\t{\n\t\t\tc2l(in,tin0);\n\t\t\tc2l(in,tin1);\n\t\t\t}\n\t\telse\n\t\t\tc2ln(in,tin0,tin1,l);\n\t\ttin0^=tout0; tin[0]=tin0;\n\t\ttin1^=tout1; tin[1]=tin1;\n\t\tdes_encrypt((DES_LONG *)tin,schedule,DES_ENCRYPT);\n\t\ttout0=tin[0];\n\t\ttout1=tin[1];\n\t\t}\n\tif (out != NULL)\n\t\t{\n\t\tl2c(tout0,out);\n\t\tl2c(tout1,out);\n\t\t}\n\ttout0=tin0=tin1=tin[0]=tin[1]=0;\n\treturn(tout1);\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}']
271
0
https://github.com/libav/libav/blob/3ec394ea823c2a6e65a6abbdb2041ce1c66964f8/libavcodec/motion_est.c/#L596
static inline int sad_hpel_motion_search(MpegEncContext * s, int *mx_ptr, int *my_ptr, int dmin, int src_index, int ref_index, int size, int h) { MotionEstContext * const c= &s->me; const int penalty_factor= c->sub_penalty_factor; int mx, my, dminh; uint8_t *pix, *ptr; int stride= c->stride; const int flags= c->sub_flags; LOAD_COMMON assert(flags == 0); if(c->skip){ *mx_ptr = 0; *my_ptr = 0; return dmin; } pix = c->src[src_index][0]; mx = *mx_ptr; my = *my_ptr; ptr = c->ref[ref_index][0] + (my * stride) + mx; dminh = dmin; if (mx > xmin && mx < xmax && my > ymin && my < ymax) { int dx=0, dy=0; int d, pen_x, pen_y; const int index= (my<<ME_MAP_SHIFT) + mx; const int t= score_map[(index-(1<<ME_MAP_SHIFT))&(ME_MAP_SIZE-1)]; const int l= score_map[(index- 1 )&(ME_MAP_SIZE-1)]; const int r= score_map[(index+ 1 )&(ME_MAP_SIZE-1)]; const int b= score_map[(index+(1<<ME_MAP_SHIFT))&(ME_MAP_SIZE-1)]; mx<<=1; my<<=1; pen_x= pred_x + mx; pen_y= pred_y + my; ptr-= stride; if(t<=b){ CHECK_SAD_HALF_MV(y2 , 0, -1) if(l<=r){ CHECK_SAD_HALF_MV(xy2, -1, -1) if(t+r<=b+l){ CHECK_SAD_HALF_MV(xy2, +1, -1) ptr+= stride; }else{ ptr+= stride; CHECK_SAD_HALF_MV(xy2, -1, +1) } CHECK_SAD_HALF_MV(x2 , -1, 0) }else{ CHECK_SAD_HALF_MV(xy2, +1, -1) if(t+l<=b+r){ CHECK_SAD_HALF_MV(xy2, -1, -1) ptr+= stride; }else{ ptr+= stride; CHECK_SAD_HALF_MV(xy2, +1, +1) } CHECK_SAD_HALF_MV(x2 , +1, 0) } }else{ if(l<=r){ if(t+l<=b+r){ CHECK_SAD_HALF_MV(xy2, -1, -1) ptr+= stride; }else{ ptr+= stride; CHECK_SAD_HALF_MV(xy2, +1, +1) } CHECK_SAD_HALF_MV(x2 , -1, 0) CHECK_SAD_HALF_MV(xy2, -1, +1) }else{ if(t+r<=b+l){ CHECK_SAD_HALF_MV(xy2, +1, -1) ptr+= stride; }else{ ptr+= stride; CHECK_SAD_HALF_MV(xy2, -1, +1) } CHECK_SAD_HALF_MV(x2 , +1, 0) CHECK_SAD_HALF_MV(xy2, +1, +1) } CHECK_SAD_HALF_MV(y2 , 0, +1) } mx+=dx; my+=dy; }else{ mx<<=1; my<<=1; } *mx_ptr = mx; *my_ptr = my; return dminh; }
['static inline int sad_hpel_motion_search(MpegEncContext * s,\n int *mx_ptr, int *my_ptr, int dmin,\n int src_index, int ref_index,\n int size, int h)\n{\n MotionEstContext * const c= &s->me;\n const int penalty_factor= c->sub_penalty_factor;\n int mx, my, dminh;\n uint8_t *pix, *ptr;\n int stride= c->stride;\n const int flags= c->sub_flags;\n LOAD_COMMON\n assert(flags == 0);\n if(c->skip){\n *mx_ptr = 0;\n *my_ptr = 0;\n return dmin;\n }\n pix = c->src[src_index][0];\n mx = *mx_ptr;\n my = *my_ptr;\n ptr = c->ref[ref_index][0] + (my * stride) + mx;\n dminh = dmin;\n if (mx > xmin && mx < xmax &&\n my > ymin && my < ymax) {\n int dx=0, dy=0;\n int d, pen_x, pen_y;\n const int index= (my<<ME_MAP_SHIFT) + mx;\n const int t= score_map[(index-(1<<ME_MAP_SHIFT))&(ME_MAP_SIZE-1)];\n const int l= score_map[(index- 1 )&(ME_MAP_SIZE-1)];\n const int r= score_map[(index+ 1 )&(ME_MAP_SIZE-1)];\n const int b= score_map[(index+(1<<ME_MAP_SHIFT))&(ME_MAP_SIZE-1)];\n mx<<=1;\n my<<=1;\n pen_x= pred_x + mx;\n pen_y= pred_y + my;\n ptr-= stride;\n if(t<=b){\n CHECK_SAD_HALF_MV(y2 , 0, -1)\n if(l<=r){\n CHECK_SAD_HALF_MV(xy2, -1, -1)\n if(t+r<=b+l){\n CHECK_SAD_HALF_MV(xy2, +1, -1)\n ptr+= stride;\n }else{\n ptr+= stride;\n CHECK_SAD_HALF_MV(xy2, -1, +1)\n }\n CHECK_SAD_HALF_MV(x2 , -1, 0)\n }else{\n CHECK_SAD_HALF_MV(xy2, +1, -1)\n if(t+l<=b+r){\n CHECK_SAD_HALF_MV(xy2, -1, -1)\n ptr+= stride;\n }else{\n ptr+= stride;\n CHECK_SAD_HALF_MV(xy2, +1, +1)\n }\n CHECK_SAD_HALF_MV(x2 , +1, 0)\n }\n }else{\n if(l<=r){\n if(t+l<=b+r){\n CHECK_SAD_HALF_MV(xy2, -1, -1)\n ptr+= stride;\n }else{\n ptr+= stride;\n CHECK_SAD_HALF_MV(xy2, +1, +1)\n }\n CHECK_SAD_HALF_MV(x2 , -1, 0)\n CHECK_SAD_HALF_MV(xy2, -1, +1)\n }else{\n if(t+r<=b+l){\n CHECK_SAD_HALF_MV(xy2, +1, -1)\n ptr+= stride;\n }else{\n ptr+= stride;\n CHECK_SAD_HALF_MV(xy2, -1, +1)\n }\n CHECK_SAD_HALF_MV(x2 , +1, 0)\n CHECK_SAD_HALF_MV(xy2, +1, +1)\n }\n CHECK_SAD_HALF_MV(y2 , 0, +1)\n }\n mx+=dx;\n my+=dy;\n }else{\n mx<<=1;\n my<<=1;\n }\n *mx_ptr = mx;\n *my_ptr = my;\n return dminh;\n}']
272
0
https://github.com/libav/libav/blob/e5d403720ec4914169f55913a5a5555d908500b6/libavcodec/svq3.c/#L147
void ff_svq3_luma_dc_dequant_idct_c(DCTELEM *output, DCTELEM *input, int qp){ const int qmul = svq3_dequant_coeff[qp]; #define stride 16 int i; int temp[16]; static const uint8_t x_offset[4]={0, 1*stride, 4*stride, 5*stride}; for(i=0; i<4; i++){ const int z0 = 13*(input[4*i+0] + input[4*i+2]); const int z1 = 13*(input[4*i+0] - input[4*i+2]); const int z2 = 7* input[4*i+1] - 17*input[4*i+3]; const int z3 = 17* input[4*i+1] + 7*input[4*i+3]; temp[4*i+0] = z0+z3; temp[4*i+1] = z1+z2; temp[4*i+2] = z1-z2; temp[4*i+3] = z0-z3; } for(i=0; i<4; i++){ const int offset= x_offset[i]; const int z0= 13*(temp[4*0+i] + temp[4*2+i]); const int z1= 13*(temp[4*0+i] - temp[4*2+i]); const int z2= 7* temp[4*1+i] - 17*temp[4*3+i]; const int z3= 17* temp[4*1+i] + 7*temp[4*3+i]; output[stride* 0+offset] = ((z0 + z3)*qmul + 0x80000) >> 20; output[stride* 2+offset] = ((z1 + z2)*qmul + 0x80000) >> 20; output[stride* 8+offset] = ((z1 - z2)*qmul + 0x80000) >> 20; output[stride*10+offset] = ((z0 - z3)*qmul + 0x80000) >> 20; } }
['static av_always_inline void hl_decode_mb_predict_luma(H264Context *h,\n int mb_type, int is_h264,\n int simple,\n int transform_bypass,\n int pixel_shift,\n int *block_offset,\n int linesize,\n uint8_t *dest_y, int p)\n{\n MpegEncContext *const s = &h->s;\n void (*idct_add)(uint8_t *dst, DCTELEM *block, int stride);\n void (*idct_dc_add)(uint8_t *dst, DCTELEM *block, int stride);\n int i;\n int qscale = p == 0 ? s->qscale : h->chroma_qp[p - 1];\n block_offset += 16 * p;\n if (IS_INTRA4x4(mb_type)) {\n if (simple || !s->encoding) {\n if (IS_8x8DCT(mb_type)) {\n if (transform_bypass) {\n idct_dc_add =\n idct_add = s->dsp.add_pixels8;\n } else {\n idct_dc_add = h->h264dsp.h264_idct8_dc_add;\n idct_add = h->h264dsp.h264_idct8_add;\n }\n for (i = 0; i < 16; i += 4) {\n uint8_t *const ptr = dest_y + block_offset[i];\n const int dir = h->intra4x4_pred_mode_cache[scan8[i]];\n if (transform_bypass && h->sps.profile_idc == 244 && dir <= 1) {\n h->hpc.pred8x8l_add[dir](ptr, h->mb + (i * 16 + p * 256 << pixel_shift), linesize);\n } else {\n const int nnz = h->non_zero_count_cache[scan8[i + p * 16]];\n h->hpc.pred8x8l[dir](ptr, (h->topleft_samples_available << i) & 0x8000,\n (h->topright_samples_available << i) & 0x4000, linesize);\n if (nnz) {\n if (nnz == 1 && dctcoef_get(h->mb, pixel_shift, i * 16 + p * 256))\n idct_dc_add(ptr, h->mb + (i * 16 + p * 256 << pixel_shift), linesize);\n else\n idct_add(ptr, h->mb + (i * 16 + p * 256 << pixel_shift), linesize);\n }\n }\n }\n } else {\n if (transform_bypass) {\n idct_dc_add =\n idct_add = s->dsp.add_pixels4;\n } else {\n idct_dc_add = h->h264dsp.h264_idct_dc_add;\n idct_add = h->h264dsp.h264_idct_add;\n }\n for (i = 0; i < 16; i++) {\n uint8_t *const ptr = dest_y + block_offset[i];\n const int dir = h->intra4x4_pred_mode_cache[scan8[i]];\n if (transform_bypass && h->sps.profile_idc == 244 && dir <= 1) {\n h->hpc.pred4x4_add[dir](ptr, h->mb + (i * 16 + p * 256 << pixel_shift), linesize);\n } else {\n uint8_t *topright;\n int nnz, tr;\n uint64_t tr_high;\n if (dir == DIAG_DOWN_LEFT_PRED || dir == VERT_LEFT_PRED) {\n const int topright_avail = (h->topright_samples_available << i) & 0x8000;\n assert(s->mb_y || linesize <= block_offset[i]);\n if (!topright_avail) {\n if (pixel_shift) {\n tr_high = ((uint16_t *)ptr)[3 - linesize / 2] * 0x0001000100010001ULL;\n topright = (uint8_t *)&tr_high;\n } else {\n tr = ptr[3 - linesize] * 0x01010101u;\n topright = (uint8_t *)&tr;\n }\n } else\n topright = ptr + (4 << pixel_shift) - linesize;\n } else\n topright = NULL;\n h->hpc.pred4x4[dir](ptr, topright, linesize);\n nnz = h->non_zero_count_cache[scan8[i + p * 16]];\n if (nnz) {\n if (is_h264) {\n if (nnz == 1 && dctcoef_get(h->mb, pixel_shift, i * 16 + p * 256))\n idct_dc_add(ptr, h->mb + (i * 16 + p * 256 << pixel_shift), linesize);\n else\n idct_add(ptr, h->mb + (i * 16 + p * 256 << pixel_shift), linesize);\n } else if (CONFIG_SVQ3_DECODER)\n ff_svq3_add_idct_c(ptr, h->mb + i * 16 + p * 256, linesize, qscale, 0);\n }\n }\n }\n }\n }\n } else {\n h->hpc.pred16x16[h->intra16x16_pred_mode](dest_y, linesize);\n if (is_h264) {\n if (h->non_zero_count_cache[scan8[LUMA_DC_BLOCK_INDEX + p]]) {\n if (!transform_bypass)\n h->h264dsp.h264_luma_dc_dequant_idct(h->mb + (p * 256 << pixel_shift),\n h->mb_luma_dc[p],\n h->dequant4_coeff[p][qscale][0]);\n else {\n static const uint8_t dc_mapping[16] = {\n 0 * 16, 1 * 16, 4 * 16, 5 * 16,\n 2 * 16, 3 * 16, 6 * 16, 7 * 16,\n 8 * 16, 9 * 16, 12 * 16, 13 * 16,\n 10 * 16, 11 * 16, 14 * 16, 15 * 16 };\n for (i = 0; i < 16; i++)\n dctcoef_set(h->mb + (p * 256 << pixel_shift),\n pixel_shift, dc_mapping[i],\n dctcoef_get(h->mb_luma_dc[p],\n pixel_shift, i));\n }\n }\n } else if (CONFIG_SVQ3_DECODER)\n ff_svq3_luma_dc_dequant_idct_c(h->mb + p * 256,\n h->mb_luma_dc[p], qscale);\n }\n}', 'void ff_svq3_luma_dc_dequant_idct_c(DCTELEM *output, DCTELEM *input, int qp){\n const int qmul = svq3_dequant_coeff[qp];\n#define stride 16\n int i;\n int temp[16];\n static const uint8_t x_offset[4]={0, 1*stride, 4*stride, 5*stride};\n for(i=0; i<4; i++){\n const int z0 = 13*(input[4*i+0] + input[4*i+2]);\n const int z1 = 13*(input[4*i+0] - input[4*i+2]);\n const int z2 = 7* input[4*i+1] - 17*input[4*i+3];\n const int z3 = 17* input[4*i+1] + 7*input[4*i+3];\n temp[4*i+0] = z0+z3;\n temp[4*i+1] = z1+z2;\n temp[4*i+2] = z1-z2;\n temp[4*i+3] = z0-z3;\n }\n for(i=0; i<4; i++){\n const int offset= x_offset[i];\n const int z0= 13*(temp[4*0+i] + temp[4*2+i]);\n const int z1= 13*(temp[4*0+i] - temp[4*2+i]);\n const int z2= 7* temp[4*1+i] - 17*temp[4*3+i];\n const int z3= 17* temp[4*1+i] + 7*temp[4*3+i];\n output[stride* 0+offset] = ((z0 + z3)*qmul + 0x80000) >> 20;\n output[stride* 2+offset] = ((z1 + z2)*qmul + 0x80000) >> 20;\n output[stride* 8+offset] = ((z1 - z2)*qmul + 0x80000) >> 20;\n output[stride*10+offset] = ((z0 - z3)*qmul + 0x80000) >> 20;\n }\n}']
273
0
https://github.com/openssl/openssl/blob/b8a3f39b890304757058deb730c855b72c14947b/include/internal/constant_time_locl.h/#L185
static ossl_inline uint32_t constant_time_is_zero_32(uint32_t a) { return constant_time_msb_32(~a & (a - 1)); }
['static int test_is_zero_32(int i)\n{\n uint32_t a = test_values_32[i];\n if (a == 0 && !TEST_true(constant_time_is_zero_32(a) == CONSTTIME_TRUE_32))\n return 0;\n if (a != 0 && !TEST_true(constant_time_is_zero_32(a) == CONSTTIME_FALSE_32))\n return 0;\n return 1;\n}', 'static ossl_inline uint32_t constant_time_is_zero_32(uint32_t a)\n{\n return constant_time_msb_32(~a & (a - 1));\n}']
274
0
https://github.com/libav/libav/blob/a4be782cbde337ced47b04e2e6f79ba99e79ae1d/libavcodec/h264_refs.c/#L485
int ff_h264_execute_ref_pic_marking(H264Context *h, MMCO *mmco, int mmco_count){ MpegEncContext * const s = &h->s; int i, av_uninit(j); int current_ref_assigned=0; Picture *av_uninit(pic); if((s->avctx->debug&FF_DEBUG_MMCO) && mmco_count==0) av_log(h->s.avctx, AV_LOG_DEBUG, "no mmco here\n"); for(i=0; i<mmco_count; i++){ int av_uninit(structure), av_uninit(frame_num); if(s->avctx->debug&FF_DEBUG_MMCO) av_log(h->s.avctx, AV_LOG_DEBUG, "mmco:%d %d %d\n", h->mmco[i].opcode, h->mmco[i].short_pic_num, h->mmco[i].long_arg); if( mmco[i].opcode == MMCO_SHORT2UNUSED || mmco[i].opcode == MMCO_SHORT2LONG){ frame_num = pic_num_extract(h, mmco[i].short_pic_num, &structure); pic = find_short(h, frame_num, &j); if(!pic){ if(mmco[i].opcode != MMCO_SHORT2LONG || !h->long_ref[mmco[i].long_arg] || h->long_ref[mmco[i].long_arg]->frame_num != frame_num) av_log(h->s.avctx, AV_LOG_ERROR, "mmco: unref short failure\n"); continue; } } switch(mmco[i].opcode){ case MMCO_SHORT2UNUSED: if(s->avctx->debug&FF_DEBUG_MMCO) av_log(h->s.avctx, AV_LOG_DEBUG, "mmco: unref short %d count %d\n", h->mmco[i].short_pic_num, h->short_ref_count); remove_short(h, frame_num, structure ^ PICT_FRAME); break; case MMCO_SHORT2LONG: if (h->long_ref[mmco[i].long_arg] != pic) remove_long(h, mmco[i].long_arg, 0); remove_short_at_index(h, j); h->long_ref[ mmco[i].long_arg ]= pic; if (h->long_ref[ mmco[i].long_arg ]){ h->long_ref[ mmco[i].long_arg ]->long_ref=1; h->long_ref_count++; } break; case MMCO_LONG2UNUSED: j = pic_num_extract(h, mmco[i].long_arg, &structure); pic = h->long_ref[j]; if (pic) { remove_long(h, j, structure ^ PICT_FRAME); } else if(s->avctx->debug&FF_DEBUG_MMCO) av_log(h->s.avctx, AV_LOG_DEBUG, "mmco: unref long failure\n"); break; case MMCO_LONG: if (h->long_ref[mmco[i].long_arg] != s->current_picture_ptr) { remove_long(h, mmco[i].long_arg, 0); h->long_ref[ mmco[i].long_arg ]= s->current_picture_ptr; h->long_ref[ mmco[i].long_arg ]->long_ref=1; h->long_ref_count++; } s->current_picture_ptr->reference |= s->picture_structure; current_ref_assigned=1; break; case MMCO_SET_MAX_LONG: assert(mmco[i].long_arg <= 16); for(j = mmco[i].long_arg; j<16; j++){ remove_long(h, j, 0); } break; case MMCO_RESET: while(h->short_ref_count){ remove_short(h, h->short_ref[0]->frame_num, 0); } for(j = 0; j < 16; j++) { remove_long(h, j, 0); } s->current_picture_ptr->poc= s->current_picture_ptr->field_poc[0]= s->current_picture_ptr->field_poc[1]= h->poc_lsb= h->poc_msb= h->frame_num= s->current_picture_ptr->frame_num= 0; s->current_picture_ptr->mmco_reset=1; break; default: assert(0); } } if (!current_ref_assigned) { if (h->short_ref_count && h->short_ref[0] == s->current_picture_ptr) { s->current_picture_ptr->reference = PICT_FRAME; } else if (s->current_picture_ptr->long_ref) { av_log(h->s.avctx, AV_LOG_ERROR, "illegal short term reference " "assignment for second field " "in complementary field pair " "(first field is long term)\n"); } else { pic= remove_short(h, s->current_picture_ptr->frame_num, 0); if(pic){ av_log(h->s.avctx, AV_LOG_ERROR, "illegal short term buffer state detected\n"); } if(h->short_ref_count) memmove(&h->short_ref[1], &h->short_ref[0], h->short_ref_count*sizeof(Picture*)); h->short_ref[0]= s->current_picture_ptr; h->short_ref_count++; s->current_picture_ptr->reference |= s->picture_structure; } } if (h->long_ref_count + h->short_ref_count > h->sps.ref_frame_count){ av_log(h->s.avctx, AV_LOG_ERROR, "number of reference frames exceeds max (probably " "corrupt input), discarding one\n"); if (h->long_ref_count && !h->short_ref_count) { for (i = 0; i < 16; ++i) if (h->long_ref[i]) break; assert(i < 16); remove_long(h, i, 0); } else { pic = h->short_ref[h->short_ref_count - 1]; remove_short(h, pic->frame_num, 0); } } print_short_term(h); print_long_term(h); return 0; }
['int ff_h264_execute_ref_pic_marking(H264Context *h, MMCO *mmco, int mmco_count){\n MpegEncContext * const s = &h->s;\n int i, av_uninit(j);\n int current_ref_assigned=0;\n Picture *av_uninit(pic);\n if((s->avctx->debug&FF_DEBUG_MMCO) && mmco_count==0)\n av_log(h->s.avctx, AV_LOG_DEBUG, "no mmco here\\n");\n for(i=0; i<mmco_count; i++){\n int av_uninit(structure), av_uninit(frame_num);\n if(s->avctx->debug&FF_DEBUG_MMCO)\n av_log(h->s.avctx, AV_LOG_DEBUG, "mmco:%d %d %d\\n", h->mmco[i].opcode, h->mmco[i].short_pic_num, h->mmco[i].long_arg);\n if( mmco[i].opcode == MMCO_SHORT2UNUSED\n || mmco[i].opcode == MMCO_SHORT2LONG){\n frame_num = pic_num_extract(h, mmco[i].short_pic_num, &structure);\n pic = find_short(h, frame_num, &j);\n if(!pic){\n if(mmco[i].opcode != MMCO_SHORT2LONG || !h->long_ref[mmco[i].long_arg]\n || h->long_ref[mmco[i].long_arg]->frame_num != frame_num)\n av_log(h->s.avctx, AV_LOG_ERROR, "mmco: unref short failure\\n");\n continue;\n }\n }\n switch(mmco[i].opcode){\n case MMCO_SHORT2UNUSED:\n if(s->avctx->debug&FF_DEBUG_MMCO)\n av_log(h->s.avctx, AV_LOG_DEBUG, "mmco: unref short %d count %d\\n", h->mmco[i].short_pic_num, h->short_ref_count);\n remove_short(h, frame_num, structure ^ PICT_FRAME);\n break;\n case MMCO_SHORT2LONG:\n if (h->long_ref[mmco[i].long_arg] != pic)\n remove_long(h, mmco[i].long_arg, 0);\n remove_short_at_index(h, j);\n h->long_ref[ mmco[i].long_arg ]= pic;\n if (h->long_ref[ mmco[i].long_arg ]){\n h->long_ref[ mmco[i].long_arg ]->long_ref=1;\n h->long_ref_count++;\n }\n break;\n case MMCO_LONG2UNUSED:\n j = pic_num_extract(h, mmco[i].long_arg, &structure);\n pic = h->long_ref[j];\n if (pic) {\n remove_long(h, j, structure ^ PICT_FRAME);\n } else if(s->avctx->debug&FF_DEBUG_MMCO)\n av_log(h->s.avctx, AV_LOG_DEBUG, "mmco: unref long failure\\n");\n break;\n case MMCO_LONG:\n if (h->long_ref[mmco[i].long_arg] != s->current_picture_ptr) {\n remove_long(h, mmco[i].long_arg, 0);\n h->long_ref[ mmco[i].long_arg ]= s->current_picture_ptr;\n h->long_ref[ mmco[i].long_arg ]->long_ref=1;\n h->long_ref_count++;\n }\n s->current_picture_ptr->reference |= s->picture_structure;\n current_ref_assigned=1;\n break;\n case MMCO_SET_MAX_LONG:\n assert(mmco[i].long_arg <= 16);\n for(j = mmco[i].long_arg; j<16; j++){\n remove_long(h, j, 0);\n }\n break;\n case MMCO_RESET:\n while(h->short_ref_count){\n remove_short(h, h->short_ref[0]->frame_num, 0);\n }\n for(j = 0; j < 16; j++) {\n remove_long(h, j, 0);\n }\n s->current_picture_ptr->poc=\n s->current_picture_ptr->field_poc[0]=\n s->current_picture_ptr->field_poc[1]=\n h->poc_lsb=\n h->poc_msb=\n h->frame_num=\n s->current_picture_ptr->frame_num= 0;\n s->current_picture_ptr->mmco_reset=1;\n break;\n default: assert(0);\n }\n }\n if (!current_ref_assigned) {\n if (h->short_ref_count && h->short_ref[0] == s->current_picture_ptr) {\n s->current_picture_ptr->reference = PICT_FRAME;\n } else if (s->current_picture_ptr->long_ref) {\n av_log(h->s.avctx, AV_LOG_ERROR, "illegal short term reference "\n "assignment for second field "\n "in complementary field pair "\n "(first field is long term)\\n");\n } else {\n pic= remove_short(h, s->current_picture_ptr->frame_num, 0);\n if(pic){\n av_log(h->s.avctx, AV_LOG_ERROR, "illegal short term buffer state detected\\n");\n }\n if(h->short_ref_count)\n memmove(&h->short_ref[1], &h->short_ref[0], h->short_ref_count*sizeof(Picture*));\n h->short_ref[0]= s->current_picture_ptr;\n h->short_ref_count++;\n s->current_picture_ptr->reference |= s->picture_structure;\n }\n }\n if (h->long_ref_count + h->short_ref_count > h->sps.ref_frame_count){\n av_log(h->s.avctx, AV_LOG_ERROR,\n "number of reference frames exceeds max (probably "\n "corrupt input), discarding one\\n");\n if (h->long_ref_count && !h->short_ref_count) {\n for (i = 0; i < 16; ++i)\n if (h->long_ref[i])\n break;\n assert(i < 16);\n remove_long(h, i, 0);\n } else {\n pic = h->short_ref[h->short_ref_count - 1];\n remove_short(h, pic->frame_num, 0);\n }\n }\n print_short_term(h);\n print_long_term(h);\n return 0;\n}']
275
0
https://github.com/openssl/openssl/blob/6d4c65835de4ed9aa0a37e3234055d075fe0bc58/crypto/bn/bn_asm.c/#L406
BN_ULONG bn_sub_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b, int n) { BN_ULONG t1,t2; int c=0; assert(n >= 0); if (n <= 0) return((BN_ULONG)0); #ifndef OPENSSL_SMALL_FOOTPRINT while (n&~3) { t1=a[0]; t2=b[0]; r[0]=(t1-t2-c)&BN_MASK2; if (t1 != t2) c=(t1 < t2); t1=a[1]; t2=b[1]; r[1]=(t1-t2-c)&BN_MASK2; if (t1 != t2) c=(t1 < t2); t1=a[2]; t2=b[2]; r[2]=(t1-t2-c)&BN_MASK2; if (t1 != t2) c=(t1 < t2); t1=a[3]; t2=b[3]; r[3]=(t1-t2-c)&BN_MASK2; if (t1 != t2) c=(t1 < t2); a+=4; b+=4; r+=4; n-=4; } #endif while (n) { t1=a[0]; t2=b[0]; r[0]=(t1-t2-c)&BN_MASK2; if (t1 != t2) c=(t1 < t2); a++; b++; r++; n--; } return(c); }
['int BN_mod_exp_mont_consttime(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p,\n\t\t const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont)\n\t{\n\tint i,bits,ret=0,window,wvalue;\n\tint top;\n\tBN_MONT_CTX *mont=NULL;\n\tint numPowers;\n\tunsigned char *powerbufFree=NULL;\n\tint powerbufLen = 0;\n\tunsigned char *powerbuf=NULL;\n\tBIGNUM tmp, am;\n\tbn_check_top(a);\n\tbn_check_top(p);\n\tbn_check_top(m);\n\ttop = m->top;\n\tif (!(m->d[0] & 1))\n\t\t{\n\t\tBNerr(BN_F_BN_MOD_EXP_MONT_CONSTTIME,BN_R_CALLED_WITH_EVEN_MODULUS);\n\t\treturn(0);\n\t\t}\n\tbits=BN_num_bits(p);\n\tif (bits == 0)\n\t\t{\n\t\tret = BN_one(rr);\n\t\treturn ret;\n\t\t}\n\tBN_CTX_start(ctx);\n\tif (in_mont != NULL)\n\t\tmont=in_mont;\n\telse\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\twindow = BN_window_bits_for_ctime_exponent_size(bits);\n#if defined(OPENSSL_BN_ASM_MONT5)\n\tif (window==6 && bits<=1024) window=5;\n#endif\n\tnumPowers = 1 << window;\n\tpowerbufLen = sizeof(m->d[0])*(top*numPowers +\n\t\t\t\t((2*top)>numPowers?(2*top):numPowers));\n#ifdef alloca\n\tif (powerbufLen < 3072)\n\t\tpowerbufFree = alloca(powerbufLen+MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH);\n\telse\n#endif\n\tif ((powerbufFree=(unsigned char*)OPENSSL_malloc(powerbufLen+MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH)) == NULL)\n\t\tgoto err;\n\tpowerbuf = MOD_EXP_CTIME_ALIGN(powerbufFree);\n\tmemset(powerbuf, 0, powerbufLen);\n#ifdef alloca\n\tif (powerbufLen < 3072)\n\t\tpowerbufFree = NULL;\n#endif\n\ttmp.d = (BN_ULONG *)(powerbuf + sizeof(m->d[0])*top*numPowers);\n\tam.d = tmp.d + top;\n\ttmp.top = am.top = 0;\n\ttmp.dmax = am.dmax = top;\n\ttmp.neg = am.neg = 0;\n\ttmp.flags = am.flags = BN_FLG_STATIC_DATA;\n#if 1\n \tif (!BN_to_montgomery(&tmp,BN_value_one(),mont,ctx))\tgoto err;\n#else\n\ttmp.d[0] = (0-m->d[0])&BN_MASK2;\n\tfor (i=1;i<top;i++)\n\t\ttmp.d[i] = (~m->d[i])&BN_MASK2;\n\ttmp.top = top;\n#endif\n\tif (a->neg || BN_ucmp(a,m) >= 0)\n\t\t{\n\t\tif (!BN_mod(&am,a,m,ctx))\t\t\tgoto err;\n\t\tif (!BN_to_montgomery(&am,&am,mont,ctx))\tgoto err;\n\t\t}\n\telse\tif (!BN_to_montgomery(&am,a,mont,ctx))\t\tgoto err;\n#if defined(OPENSSL_BN_ASM_MONT5)\n if (window==5)\n\t{\n\tvoid bn_mul_mont_gather5(BN_ULONG *rp,const BN_ULONG *ap,\n\t\t\tconst void *table,const BN_ULONG *np,\n\t\t\tconst BN_ULONG *n0,int num,int power);\n\tvoid bn_scatter5(const BN_ULONG *inp,size_t num,\n\t\t\tvoid *table,size_t power);\n\tvoid bn_gather5(BN_ULONG *out,size_t num,\n\t\t\tvoid *table,size_t power);\n\tBN_ULONG *np=mont->N.d, *n0=mont->n0;\n\tbn_scatter5(tmp.d,top,powerbuf,0);\n\tbn_scatter5(am.d,am.top,powerbuf,1);\n\tbn_mul_mont(tmp.d,am.d,am.d,np,n0,top);\n\tbn_scatter5(tmp.d,top,powerbuf,2);\n#if 0\n\tfor (i=3; i<32; i++)\n\t\t{\n\t\tbn_mul_mont_gather5(tmp.d,am.d,powerbuf,np,n0,top,i-1);\n\t\tbn_scatter5(tmp.d,top,powerbuf,i);\n\t\t}\n#else\n\tfor (i=4; i<32; i*=2)\n\t\t{\n\t\tbn_mul_mont(tmp.d,tmp.d,tmp.d,np,n0,top);\n\t\tbn_scatter5(tmp.d,top,powerbuf,i);\n\t\t}\n\tfor (i=3; i<8; i+=2)\n\t\t{\n\t\tint j;\n\t\tbn_mul_mont_gather5(tmp.d,am.d,powerbuf,np,n0,top,i-1);\n\t\tbn_scatter5(tmp.d,top,powerbuf,i);\n\t\tfor (j=2*i; j<32; j*=2)\n\t\t\t{\n\t\t\tbn_mul_mont(tmp.d,tmp.d,tmp.d,np,n0,top);\n\t\t\tbn_scatter5(tmp.d,top,powerbuf,j);\n\t\t\t}\n\t\t}\n\tfor (; i<16; i+=2)\n\t\t{\n\t\tbn_mul_mont_gather5(tmp.d,am.d,powerbuf,np,n0,top,i-1);\n\t\tbn_scatter5(tmp.d,top,powerbuf,i);\n\t\tbn_mul_mont(tmp.d,tmp.d,tmp.d,np,n0,top);\n\t\tbn_scatter5(tmp.d,top,powerbuf,2*i);\n\t\t}\n\tfor (; i<32; i+=2)\n\t\t{\n\t\tbn_mul_mont_gather5(tmp.d,am.d,powerbuf,np,n0,top,i-1);\n\t\tbn_scatter5(tmp.d,top,powerbuf,i);\n\t\t}\n#endif\n\tbits--;\n\tfor (wvalue=0, i=bits%5; i>=0; i--,bits--)\n\t\twvalue = (wvalue<<1)+BN_is_bit_set(p,bits);\n\tbn_gather5(tmp.d,top,powerbuf,wvalue);\n\twhile (bits >= 0)\n\t\t{\n\t\tfor (wvalue=0, i=0; i<5; i++,bits--)\n\t\t\twvalue = (wvalue<<1)+BN_is_bit_set(p,bits);\n\t\tbn_mul_mont(tmp.d,tmp.d,tmp.d,np,n0,top);\n\t\tbn_mul_mont(tmp.d,tmp.d,tmp.d,np,n0,top);\n\t\tbn_mul_mont(tmp.d,tmp.d,tmp.d,np,n0,top);\n\t\tbn_mul_mont(tmp.d,tmp.d,tmp.d,np,n0,top);\n\t\tbn_mul_mont(tmp.d,tmp.d,tmp.d,np,n0,top);\n\t\tbn_mul_mont_gather5(tmp.d,tmp.d,powerbuf,np,n0,top,wvalue);\n\t\t}\n\ttmp.top=top;\n\tbn_correct_top(&tmp);\n\t}\n else\n#endif\n\t{\n\tif (!MOD_EXP_CTIME_COPY_TO_PREBUF(&tmp, top, powerbuf, 0, numPowers)) goto err;\n\tif (!MOD_EXP_CTIME_COPY_TO_PREBUF(&am, top, powerbuf, 1, numPowers)) goto err;\n\tif (window > 1)\n\t\t{\n\t\tif (!BN_mod_mul_montgomery(&tmp,&am,&am,mont,ctx))\tgoto err;\n\t\tif (!MOD_EXP_CTIME_COPY_TO_PREBUF(&tmp, top, powerbuf, 2, numPowers)) goto err;\n\t\tfor (i=3; i<numPowers; i++)\n\t\t\t{\n\t\t\tif (!BN_mod_mul_montgomery(&tmp,&am,&tmp,mont,ctx))\n\t\t\t\tgoto err;\n\t\t\tif (!MOD_EXP_CTIME_COPY_TO_PREBUF(&tmp, top, powerbuf, i, numPowers)) goto err;\n\t\t\t}\n\t\t}\n\tbits--;\n\tfor (wvalue=0, i=bits%window; i>=0; i--,bits--)\n\t\twvalue = (wvalue<<1)+BN_is_bit_set(p,bits);\n\tif (!MOD_EXP_CTIME_COPY_FROM_PREBUF(&tmp,top,powerbuf,wvalue,numPowers)) goto err;\n \twhile (bits >= 0)\n \t\t{\n \t\twvalue=0;\n \t\tfor (i=0; i<window; i++,bits--)\n \t\t\t{\n\t\t\tif (!BN_mod_mul_montgomery(&tmp,&tmp,&tmp,mont,ctx))\tgoto err;\n\t\t\twvalue = (wvalue<<1)+BN_is_bit_set(p,bits);\n \t\t\t}\n\t\tif (!MOD_EXP_CTIME_COPY_FROM_PREBUF(&am, top, powerbuf, wvalue, numPowers)) goto err;\n \t\tif (!BN_mod_mul_montgomery(&tmp,&tmp,&am,mont,ctx)) goto err;\n \t\t}\n\t}\n\tif (!BN_from_montgomery(rr,&tmp,mont,ctx)) goto err;\n\tret=1;\nerr:\n\tif ((in_mont == NULL) && (mont != NULL)) BN_MONT_CTX_free(mont);\n\tif (powerbuf!=NULL)\n\t\t{\n\t\tOPENSSL_cleanse(powerbuf,powerbufLen);\n\t\tif (powerbufFree) OPENSSL_free(powerbufFree);\n\t\t}\n\tBN_CTX_end(ctx);\n\treturn(ret);\n\t}', 'const BIGNUM *BN_value_one(void)\n\t{\n\tstatic const BN_ULONG data_one=1L;\n\tstatic const BIGNUM const_one={(BN_ULONG *)&data_one,1,1,0,BN_FLG_STATIC_DATA};\n\treturn(&const_one);\n\t}', 'int BN_mod_mul_montgomery(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,\n\t\t\t BN_MONT_CTX *mont, BN_CTX *ctx)\n\t{\n\tBIGNUM *tmp;\n\tint ret=0;\n#if defined(OPENSSL_BN_ASM_MONT) && defined(MONT_WORD)\n\tint num = mont->N.top;\n\tif (num>1 && a->top==num && b->top==num)\n\t\t{\n\t\tif (bn_wexpand(r,num) == NULL) return(0);\n\t\tif (bn_mul_mont(r->d,a->d,b->d,mont->N.d,mont->n0,num))\n\t\t\t{\n\t\t\tr->neg = a->neg^b->neg;\n\t\t\tr->top = num;\n\t\t\tbn_correct_top(r);\n\t\t\treturn(1);\n\t\t\t}\n\t\t}\n#endif\n\tBN_CTX_start(ctx);\n\ttmp = BN_CTX_get(ctx);\n\tif (tmp == NULL) goto err;\n\tbn_check_top(tmp);\n\tif (a == b)\n\t\t{\n\t\tif (!BN_sqr(tmp,a,ctx)) goto err;\n\t\t}\n\telse\n\t\t{\n\t\tif (!BN_mul(tmp,a,b,ctx)) goto err;\n\t\t}\n#ifdef MONT_WORD\n\tif (!BN_from_montgomery_word(r,tmp,mont)) goto err;\n#else\n\tif (!BN_from_montgomery(r,tmp,mont,ctx)) goto err;\n#endif\n\tbn_check_top(r);\n\tret=1;\nerr:\n\tBN_CTX_end(ctx);\n\treturn(ret);\n\t}', 'int BN_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)\n\t{\n\tint ret=0;\n\tint top,al,bl;\n\tBIGNUM *rr;\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n\tint i;\n#endif\n#ifdef BN_RECURSION\n\tBIGNUM *t=NULL;\n\tint j=0,k;\n#endif\n#ifdef BN_COUNT\n\tfprintf(stderr,"BN_mul %d * %d\\n",a->top,b->top);\n#endif\n\tbn_check_top(a);\n\tbn_check_top(b);\n\tbn_check_top(r);\n\tal=a->top;\n\tbl=b->top;\n\tif ((al == 0) || (bl == 0))\n\t\t{\n\t\tBN_zero(r);\n\t\treturn(1);\n\t\t}\n\ttop=al+bl;\n\tBN_CTX_start(ctx);\n\tif ((r == a) || (r == b))\n\t\t{\n\t\tif ((rr = BN_CTX_get(ctx)) == NULL) goto err;\n\t\t}\n\telse\n\t\trr = r;\n\trr->neg=a->neg^b->neg;\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n\ti = al-bl;\n#endif\n#ifdef BN_MUL_COMBA\n\tif (i == 0)\n\t\t{\n# if 0\n\t\tif (al == 4)\n\t\t\t{\n\t\t\tif (bn_wexpand(rr,8) == NULL) goto err;\n\t\t\trr->top=8;\n\t\t\tbn_mul_comba4(rr->d,a->d,b->d);\n\t\t\tgoto end;\n\t\t\t}\n# endif\n\t\tif (al == 8)\n\t\t\t{\n\t\t\tif (bn_wexpand(rr,16) == NULL) goto err;\n\t\t\trr->top=16;\n\t\t\tbn_mul_comba8(rr->d,a->d,b->d);\n\t\t\tgoto end;\n\t\t\t}\n\t\t}\n#endif\n#ifdef BN_RECURSION\n\tif ((al >= BN_MULL_SIZE_NORMAL) && (bl >= BN_MULL_SIZE_NORMAL))\n\t\t{\n\t\tif (i >= -1 && i <= 1)\n\t\t\t{\n\t\t\tif (i >= 0)\n\t\t\t\t{\n\t\t\t\tj = BN_num_bits_word((BN_ULONG)al);\n\t\t\t\t}\n\t\t\tif (i == -1)\n\t\t\t\t{\n\t\t\t\tj = BN_num_bits_word((BN_ULONG)bl);\n\t\t\t\t}\n\t\t\tj = 1<<(j-1);\n\t\t\tassert(j <= al || j <= bl);\n\t\t\tk = j+j;\n\t\t\tt = BN_CTX_get(ctx);\n\t\t\tif (t == NULL)\n\t\t\t\tgoto err;\n\t\t\tif (al > j || bl > j)\n\t\t\t\t{\n\t\t\t\tif (bn_wexpand(t,k*4) == NULL) goto err;\n\t\t\t\tif (bn_wexpand(rr,k*4) == NULL) goto err;\n\t\t\t\tbn_mul_part_recursive(rr->d,a->d,b->d,\n\t\t\t\t\tj,al-j,bl-j,t->d);\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\tif (bn_wexpand(t,k*2) == NULL) goto err;\n\t\t\t\tif (bn_wexpand(rr,k*2) == NULL) goto err;\n\t\t\t\tbn_mul_recursive(rr->d,a->d,b->d,\n\t\t\t\t\tj,al-j,bl-j,t->d);\n\t\t\t\t}\n\t\t\trr->top=top;\n\t\t\tgoto end;\n\t\t\t}\n#if 0\n\t\tif (i == 1 && !BN_get_flags(b,BN_FLG_STATIC_DATA))\n\t\t\t{\n\t\t\tBIGNUM *tmp_bn = (BIGNUM *)b;\n\t\t\tif (bn_wexpand(tmp_bn,al) == NULL) goto err;\n\t\t\ttmp_bn->d[bl]=0;\n\t\t\tbl++;\n\t\t\ti--;\n\t\t\t}\n\t\telse if (i == -1 && !BN_get_flags(a,BN_FLG_STATIC_DATA))\n\t\t\t{\n\t\t\tBIGNUM *tmp_bn = (BIGNUM *)a;\n\t\t\tif (bn_wexpand(tmp_bn,bl) == NULL) goto err;\n\t\t\ttmp_bn->d[al]=0;\n\t\t\tal++;\n\t\t\ti++;\n\t\t\t}\n\t\tif (i == 0)\n\t\t\t{\n\t\t\tj=BN_num_bits_word((BN_ULONG)al);\n\t\t\tj=1<<(j-1);\n\t\t\tk=j+j;\n\t\t\tt = BN_CTX_get(ctx);\n\t\t\tif (al == j)\n\t\t\t\t{\n\t\t\t\tif (bn_wexpand(t,k*2) == NULL) goto err;\n\t\t\t\tif (bn_wexpand(rr,k*2) == NULL) goto err;\n\t\t\t\tbn_mul_recursive(rr->d,a->d,b->d,al,t->d);\n\t\t\t\t}\n\t\t\telse\n\t\t\t\t{\n\t\t\t\tif (bn_wexpand(t,k*4) == NULL) goto err;\n\t\t\t\tif (bn_wexpand(rr,k*4) == NULL) goto err;\n\t\t\t\tbn_mul_part_recursive(rr->d,a->d,b->d,al-j,j,t->d);\n\t\t\t\t}\n\t\t\trr->top=top;\n\t\t\tgoto end;\n\t\t\t}\n#endif\n\t\t}\n#endif\n\tif (bn_wexpand(rr,top) == NULL) goto err;\n\trr->top=top;\n\tbn_mul_normal(rr->d,a->d,al,b->d,bl);\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\nend:\n#endif\n\tbn_correct_top(rr);\n\tif (r != rr) BN_copy(r,rr);\n\tret=1;\nerr:\n\tbn_check_top(r);\n\tBN_CTX_end(ctx);\n\treturn(ret);\n\t}', 'void bn_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;\n\tBN_ULONG ln,lo,*p;\n# ifdef BN_COUNT\n\tfprintf(stderr," bn_mul_part_recursive (%d%+d) * (%d%+d)\\n",\n\t\tn, tna, n, tnb);\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\tneg=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\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\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\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}', 'int bn_cmp_part_words(const BN_ULONG *a, const BN_ULONG *b,\n\tint cl, int dl)\n\t{\n\tint n,i;\n\tn = cl-1;\n\tif (dl < 0)\n\t\t{\n\t\tfor (i=dl; i<0; i++)\n\t\t\t{\n\t\t\tif (b[n-i] != 0)\n\t\t\t\treturn -1;\n\t\t\t}\n\t\t}\n\tif (dl > 0)\n\t\t{\n\t\tfor (i=dl; i>0; i--)\n\t\t\t{\n\t\t\tif (a[n+i] != 0)\n\t\t\t\treturn 1;\n\t\t\t}\n\t\t}\n\treturn bn_cmp_words(a,b,cl);\n\t}', 'BN_ULONG bn_sub_part_words(BN_ULONG *r,\n\tconst BN_ULONG *a, const BN_ULONG *b,\n\tint cl, int dl)\n\t{\n\tBN_ULONG c, t;\n\tassert(cl >= 0);\n\tc = bn_sub_words(r, a, b, cl);\n\tif (dl == 0)\n\t\treturn c;\n\tr += cl;\n\ta += cl;\n\tb += cl;\n\tif (dl < 0)\n\t\t{\n#ifdef BN_COUNT\n\t\tfprintf(stderr, " bn_sub_part_words %d + %d (dl < 0, c = %d)\\n", cl, dl, c);\n#endif\n\t\tfor (;;)\n\t\t\t{\n\t\t\tt = b[0];\n\t\t\tr[0] = (0-t-c)&BN_MASK2;\n\t\t\tif (t != 0) c=1;\n\t\t\tif (++dl >= 0) break;\n\t\t\tt = b[1];\n\t\t\tr[1] = (0-t-c)&BN_MASK2;\n\t\t\tif (t != 0) c=1;\n\t\t\tif (++dl >= 0) break;\n\t\t\tt = b[2];\n\t\t\tr[2] = (0-t-c)&BN_MASK2;\n\t\t\tif (t != 0) c=1;\n\t\t\tif (++dl >= 0) break;\n\t\t\tt = b[3];\n\t\t\tr[3] = (0-t-c)&BN_MASK2;\n\t\t\tif (t != 0) c=1;\n\t\t\tif (++dl >= 0) break;\n\t\t\tb += 4;\n\t\t\tr += 4;\n\t\t\t}\n\t\t}\n\telse\n\t\t{\n\t\tint save_dl = dl;\n#ifdef BN_COUNT\n\t\tfprintf(stderr, " bn_sub_part_words %d + %d (dl > 0, c = %d)\\n", cl, dl, c);\n#endif\n\t\twhile(c)\n\t\t\t{\n\t\t\tt = a[0];\n\t\t\tr[0] = (t-c)&BN_MASK2;\n\t\t\tif (t != 0) c=0;\n\t\t\tif (--dl <= 0) break;\n\t\t\tt = a[1];\n\t\t\tr[1] = (t-c)&BN_MASK2;\n\t\t\tif (t != 0) c=0;\n\t\t\tif (--dl <= 0) break;\n\t\t\tt = a[2];\n\t\t\tr[2] = (t-c)&BN_MASK2;\n\t\t\tif (t != 0) c=0;\n\t\t\tif (--dl <= 0) break;\n\t\t\tt = a[3];\n\t\t\tr[3] = (t-c)&BN_MASK2;\n\t\t\tif (t != 0) c=0;\n\t\t\tif (--dl <= 0) break;\n\t\t\tsave_dl = dl;\n\t\t\ta += 4;\n\t\t\tr += 4;\n\t\t\t}\n\t\tif (dl > 0)\n\t\t\t{\n#ifdef BN_COUNT\n\t\t\tfprintf(stderr, " bn_sub_part_words %d + %d (dl > 0, c == 0)\\n", cl, dl);\n#endif\n\t\t\tif (save_dl > dl)\n\t\t\t\t{\n\t\t\t\tswitch (save_dl - dl)\n\t\t\t\t\t{\n\t\t\t\tcase 1:\n\t\t\t\t\tr[1] = a[1];\n\t\t\t\t\tif (--dl <= 0) break;\n\t\t\t\tcase 2:\n\t\t\t\t\tr[2] = a[2];\n\t\t\t\t\tif (--dl <= 0) break;\n\t\t\t\tcase 3:\n\t\t\t\t\tr[3] = a[3];\n\t\t\t\t\tif (--dl <= 0) break;\n\t\t\t\t\t}\n\t\t\t\ta += 4;\n\t\t\t\tr += 4;\n\t\t\t\t}\n\t\t\t}\n\t\tif (dl > 0)\n\t\t\t{\n#ifdef BN_COUNT\n\t\t\tfprintf(stderr, " bn_sub_part_words %d + %d (dl > 0, copy)\\n", cl, dl);\n#endif\n\t\t\tfor(;;)\n\t\t\t\t{\n\t\t\t\tr[0] = a[0];\n\t\t\t\tif (--dl <= 0) break;\n\t\t\t\tr[1] = a[1];\n\t\t\t\tif (--dl <= 0) break;\n\t\t\t\tr[2] = a[2];\n\t\t\t\tif (--dl <= 0) break;\n\t\t\t\tr[3] = a[3];\n\t\t\t\tif (--dl <= 0) break;\n\t\t\t\ta += 4;\n\t\t\t\tr += 4;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\treturn c;\n\t}', 'BN_ULONG bn_sub_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b, int n)\n {\n\tBN_ULONG t1,t2;\n\tint c=0;\n\tassert(n >= 0);\n\tif (n <= 0) return((BN_ULONG)0);\n#ifndef OPENSSL_SMALL_FOOTPRINT\n\twhile (n&~3)\n\t\t{\n\t\tt1=a[0]; t2=b[0];\n\t\tr[0]=(t1-t2-c)&BN_MASK2;\n\t\tif (t1 != t2) c=(t1 < t2);\n\t\tt1=a[1]; t2=b[1];\n\t\tr[1]=(t1-t2-c)&BN_MASK2;\n\t\tif (t1 != t2) c=(t1 < t2);\n\t\tt1=a[2]; t2=b[2];\n\t\tr[2]=(t1-t2-c)&BN_MASK2;\n\t\tif (t1 != t2) c=(t1 < t2);\n\t\tt1=a[3]; t2=b[3];\n\t\tr[3]=(t1-t2-c)&BN_MASK2;\n\t\tif (t1 != t2) c=(t1 < t2);\n\t\ta+=4; b+=4; r+=4; n-=4;\n\t\t}\n#endif\n\twhile (n)\n\t\t{\n\t\tt1=a[0]; t2=b[0];\n\t\tr[0]=(t1-t2-c)&BN_MASK2;\n\t\tif (t1 != t2) c=(t1 < t2);\n\t\ta++; b++; r++; n--;\n\t\t}\n\treturn(c);\n\t}']
276
0
https://github.com/libav/libav/blob/0bf511d579c7b21f1244eec688abf571ca1235bd/libavcodec/mpegvideo.c/#L1531
void ff_MPV_frame_end(MpegEncContext *s) { int i; if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration) { ff_xvmc_field_end(s); } else if ((s->error_count || s->encoding) && !s->avctx->hwaccel && !(s->avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU) && s->unrestricted_mv && s->current_picture.f.reference && !s->intra_only && !(s->flags & CODEC_FLAG_EMU_EDGE)) { const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->avctx->pix_fmt); int hshift = desc->log2_chroma_w; int vshift = desc->log2_chroma_h; s->dsp.draw_edges(s->current_picture.f.data[0], s->linesize, s->h_edge_pos, s->v_edge_pos, EDGE_WIDTH, EDGE_WIDTH, EDGE_TOP | EDGE_BOTTOM); s->dsp.draw_edges(s->current_picture.f.data[1], s->uvlinesize, s->h_edge_pos >> hshift, s->v_edge_pos >> vshift, EDGE_WIDTH >> hshift, EDGE_WIDTH >> vshift, EDGE_TOP | EDGE_BOTTOM); s->dsp.draw_edges(s->current_picture.f.data[2], s->uvlinesize, s->h_edge_pos >> hshift, s->v_edge_pos >> vshift, EDGE_WIDTH >> hshift, EDGE_WIDTH >> vshift, EDGE_TOP | EDGE_BOTTOM); } emms_c(); s->last_pict_type = s->pict_type; s->last_lambda_for [s->pict_type] = s->current_picture_ptr->f.quality; if (s->pict_type!= AV_PICTURE_TYPE_B) { s->last_non_b_pict_type = s->pict_type; } #if 0 for (i = 0; i < MAX_PICTURE_COUNT; i++) { if (s->picture[i].f.data[0] == s->current_picture.f.data[0]) { s->picture[i] = s->current_picture; break; } } assert(i < MAX_PICTURE_COUNT); #endif if (s->encoding) { for (i = 0; i < s->picture_count; i++) { if (s->picture[i].f.data[0] && !s->picture[i].f.reference ) { free_frame_buffer(s, &s->picture[i]); } } } #if 0 memset(&s->last_picture, 0, sizeof(Picture)); memset(&s->next_picture, 0, sizeof(Picture)); memset(&s->current_picture, 0, sizeof(Picture)); #endif s->avctx->coded_frame = &s->current_picture_ptr->f; if (s->codec_id != AV_CODEC_ID_H264 && s->current_picture.f.reference) { ff_thread_report_progress(&s->current_picture_ptr->f, INT_MAX, 0); } }
['void ff_MPV_frame_end(MpegEncContext *s)\n{\n int i;\n if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration) {\n ff_xvmc_field_end(s);\n } else if ((s->error_count || s->encoding) &&\n !s->avctx->hwaccel &&\n !(s->avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU) &&\n s->unrestricted_mv &&\n s->current_picture.f.reference &&\n !s->intra_only &&\n !(s->flags & CODEC_FLAG_EMU_EDGE)) {\n const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->avctx->pix_fmt);\n int hshift = desc->log2_chroma_w;\n int vshift = desc->log2_chroma_h;\n s->dsp.draw_edges(s->current_picture.f.data[0], s->linesize,\n s->h_edge_pos, s->v_edge_pos,\n EDGE_WIDTH, EDGE_WIDTH,\n EDGE_TOP | EDGE_BOTTOM);\n s->dsp.draw_edges(s->current_picture.f.data[1], s->uvlinesize,\n s->h_edge_pos >> hshift, s->v_edge_pos >> vshift,\n EDGE_WIDTH >> hshift, EDGE_WIDTH >> vshift,\n EDGE_TOP | EDGE_BOTTOM);\n s->dsp.draw_edges(s->current_picture.f.data[2], s->uvlinesize,\n s->h_edge_pos >> hshift, s->v_edge_pos >> vshift,\n EDGE_WIDTH >> hshift, EDGE_WIDTH >> vshift,\n EDGE_TOP | EDGE_BOTTOM);\n }\n emms_c();\n s->last_pict_type = s->pict_type;\n s->last_lambda_for [s->pict_type] = s->current_picture_ptr->f.quality;\n if (s->pict_type!= AV_PICTURE_TYPE_B) {\n s->last_non_b_pict_type = s->pict_type;\n }\n#if 0\n for (i = 0; i < MAX_PICTURE_COUNT; i++) {\n if (s->picture[i].f.data[0] == s->current_picture.f.data[0]) {\n s->picture[i] = s->current_picture;\n break;\n }\n }\n assert(i < MAX_PICTURE_COUNT);\n#endif\n if (s->encoding) {\n for (i = 0; i < s->picture_count; i++) {\n if (s->picture[i].f.data[0] && !s->picture[i].f.reference\n ) {\n free_frame_buffer(s, &s->picture[i]);\n }\n }\n }\n#if 0\n memset(&s->last_picture, 0, sizeof(Picture));\n memset(&s->next_picture, 0, sizeof(Picture));\n memset(&s->current_picture, 0, sizeof(Picture));\n#endif\n s->avctx->coded_frame = &s->current_picture_ptr->f;\n if (s->codec_id != AV_CODEC_ID_H264 && s->current_picture.f.reference) {\n ff_thread_report_progress(&s->current_picture_ptr->f, INT_MAX, 0);\n }\n}', 'const AVPixFmtDescriptor *av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)\n{\n if (pix_fmt < 0 || pix_fmt >= AV_PIX_FMT_NB)\n return NULL;\n return &av_pix_fmt_descriptors[pix_fmt];\n}']
277
0
https://github.com/libav/libav/blob/7fa70598e83cca650717d02ac96bcf55e9f97c19/libavformat/rtsp.c/#L860
static int rtsp_open_transport_ctx(AVFormatContext *s, RTSPStream *rtsp_st) { RTSPState *rt = s->priv_data; AVStream *st = NULL; if (rtsp_st->stream_index >= 0) st = s->streams[rtsp_st->stream_index]; if (!st) s->ctx_flags |= AVFMTCTX_NOHEADER; if (rt->transport == RTSP_TRANSPORT_RDT) rtsp_st->transport_priv = ff_rdt_parse_open(s, st->index, rtsp_st->dynamic_protocol_context, rtsp_st->dynamic_handler); else rtsp_st->transport_priv = rtp_parse_open(s, st, rtsp_st->rtp_handle, rtsp_st->sdp_payload_type, &rtsp_st->rtp_payload_data); if (!rtsp_st->transport_priv) { return AVERROR(ENOMEM); } else if (rt->transport != RTSP_TRANSPORT_RDT) { if(rtsp_st->dynamic_handler) { rtp_parse_set_dynamic_protocol(rtsp_st->transport_priv, rtsp_st->dynamic_protocol_context, rtsp_st->dynamic_handler); } } return 0; }
['static int\nrtsp_open_transport_ctx(AVFormatContext *s, RTSPStream *rtsp_st)\n{\n RTSPState *rt = s->priv_data;\n AVStream *st = NULL;\n if (rtsp_st->stream_index >= 0)\n st = s->streams[rtsp_st->stream_index];\n if (!st)\n s->ctx_flags |= AVFMTCTX_NOHEADER;\n if (rt->transport == RTSP_TRANSPORT_RDT)\n rtsp_st->transport_priv = ff_rdt_parse_open(s, st->index,\n rtsp_st->dynamic_protocol_context,\n rtsp_st->dynamic_handler);\n else\n rtsp_st->transport_priv = rtp_parse_open(s, st, rtsp_st->rtp_handle,\n rtsp_st->sdp_payload_type,\n &rtsp_st->rtp_payload_data);\n if (!rtsp_st->transport_priv) {\n return AVERROR(ENOMEM);\n } else if (rt->transport != RTSP_TRANSPORT_RDT) {\n if(rtsp_st->dynamic_handler) {\n rtp_parse_set_dynamic_protocol(rtsp_st->transport_priv,\n rtsp_st->dynamic_protocol_context,\n rtsp_st->dynamic_handler);\n }\n }\n return 0;\n}']
278
0
https://github.com/openssl/openssl/blob/9998b32cb63b0bdd3d014abfa1d70e9a2c20a283/test/handshake_helper.c/#L122
static int select_server_ctx(SSL *s, void *arg, int ignore) { const char *servername = SSL_get_servername(s, TLSEXT_NAMETYPE_host_name); HANDSHAKE_EX_DATA *ex_data = (HANDSHAKE_EX_DATA*)(SSL_get_ex_data(s, ex_data_idx)); if (servername == NULL) { ex_data->servername = SSL_TEST_SERVERNAME_SERVER1; return SSL_TLSEXT_ERR_NOACK; } if (strcmp(servername, "server2") == 0) { SSL_CTX *new_ctx = (SSL_CTX*)arg; SSL_set_SSL_CTX(s, new_ctx); SSL_clear_options(s, 0xFFFFFFFFL); SSL_set_options(s, SSL_CTX_get_options(new_ctx)); ex_data->servername = SSL_TEST_SERVERNAME_SERVER2; return SSL_TLSEXT_ERR_OK; } else if (strcmp(servername, "server1") == 0) { ex_data->servername = SSL_TEST_SERVERNAME_SERVER1; return SSL_TLSEXT_ERR_OK; } else if (ignore) { ex_data->servername = SSL_TEST_SERVERNAME_SERVER1; return SSL_TLSEXT_ERR_NOACK; } else { return SSL_TLSEXT_ERR_ALERT_FATAL; } }
['static int select_server_ctx(SSL *s, void *arg, int ignore)\n{\n const char *servername = SSL_get_servername(s, TLSEXT_NAMETYPE_host_name);\n HANDSHAKE_EX_DATA *ex_data =\n (HANDSHAKE_EX_DATA*)(SSL_get_ex_data(s, ex_data_idx));\n if (servername == NULL) {\n ex_data->servername = SSL_TEST_SERVERNAME_SERVER1;\n return SSL_TLSEXT_ERR_NOACK;\n }\n if (strcmp(servername, "server2") == 0) {\n SSL_CTX *new_ctx = (SSL_CTX*)arg;\n SSL_set_SSL_CTX(s, new_ctx);\n SSL_clear_options(s, 0xFFFFFFFFL);\n SSL_set_options(s, SSL_CTX_get_options(new_ctx));\n ex_data->servername = SSL_TEST_SERVERNAME_SERVER2;\n return SSL_TLSEXT_ERR_OK;\n } else if (strcmp(servername, "server1") == 0) {\n ex_data->servername = SSL_TEST_SERVERNAME_SERVER1;\n return SSL_TLSEXT_ERR_OK;\n } else if (ignore) {\n ex_data->servername = SSL_TEST_SERVERNAME_SERVER1;\n return SSL_TLSEXT_ERR_NOACK;\n } else {\n return SSL_TLSEXT_ERR_ALERT_FATAL;\n }\n}', 'const char *SSL_get_servername(const SSL *s, const int type)\n{\n if (type != TLSEXT_NAMETYPE_host_name)\n return NULL;\n return s->session && !s->ext.hostname ?\n s->session->ext.hostname : s->ext.hostname;\n}', 'void *SSL_get_ex_data(const SSL *s, int idx)\n{\n return (CRYPTO_get_ex_data(&s->ex_data, idx));\n}', 'void *CRYPTO_get_ex_data(const CRYPTO_EX_DATA *ad, int idx)\n{\n if (ad->sk == NULL || idx >= sk_void_num(ad->sk))\n return NULL;\n return sk_void_value(ad->sk, idx);\n}', 'SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX *ctx)\n{\n CERT *new_cert;\n if (ssl->ctx == ctx)\n return ssl->ctx;\n if (ctx == NULL)\n ctx = ssl->session_ctx;\n new_cert = ssl_cert_dup(ctx->cert);\n if (new_cert == NULL) {\n return NULL;\n }\n ssl_cert_free(ssl->cert);\n ssl->cert = new_cert;\n OPENSSL_assert(ssl->sid_ctx_length <= sizeof(ssl->sid_ctx));\n if ((ssl->ctx != NULL) &&\n (ssl->sid_ctx_length == ssl->ctx->sid_ctx_length) &&\n (memcmp(ssl->sid_ctx, ssl->ctx->sid_ctx, ssl->sid_ctx_length) == 0)) {\n ssl->sid_ctx_length = ctx->sid_ctx_length;\n memcpy(&ssl->sid_ctx, &ctx->sid_ctx, sizeof(ssl->sid_ctx));\n }\n SSL_CTX_up_ref(ctx);\n SSL_CTX_free(ssl->ctx);\n ssl->ctx = ctx;\n return ssl->ctx;\n}', 'unsigned long SSL_clear_options(SSL *s, unsigned long op)\n{\n return s->options &= ~op;\n}', 'unsigned long SSL_CTX_get_options(const SSL_CTX *ctx)\n{\n return ctx->options;\n}', 'unsigned long SSL_set_options(SSL *s, unsigned long op)\n{\n return s->options |= op;\n}']
279
0
https://github.com/openssl/openssl/blob/0f3e6045898e9aa5d0249e61c874b1f153ae54fa/crypto/lhash/lhash.c/#L359
static void contract(LHASH *lh) { LHASH_NODE **n,*n1,*np; np=lh->b[lh->p+lh->pmax-1]; lh->b[lh->p+lh->pmax-1]=NULL; if (lh->p == 0) { n=(LHASH_NODE **)Realloc((char *)lh->b, (unsigned int)(sizeof(LHASH_NODE *)*lh->pmax)); if (n == NULL) { lh->error++; return; } lh->num_contract_reallocs++; lh->num_alloc_nodes/=2; lh->pmax/=2; lh->p=lh->pmax-1; lh->b=n; } else lh->p--; lh->num_nodes--; lh->num_contracts++; n1=lh->b[(int)lh->p]; if (n1 == NULL) lh->b[(int)lh->p]=np; else { while (n1->next != NULL) n1=n1->next; n1->next=np; } }
['static SSL *doConnection(SSL *scon)\n\t{\n\tBIO *conn;\n\tSSL *serverCon;\n\tint width, i;\n\tfd_set readfds;\n\tif ((conn=BIO_new(BIO_s_connect())) == NULL)\n\t\treturn(NULL);\n\tBIO_set_conn_hostname(conn,host);\n\tif (scon == NULL)\n\t\tserverCon=(SSL *)SSL_new(tm_ctx);\n\telse\n\t\t{\n\t\tserverCon=scon;\n\t\tSSL_set_connect_state(serverCon);\n\t\t}\n\tSSL_set_bio(serverCon,conn,conn);\n#if 0\n\tif( scon != NULL )\n\t\tSSL_set_session(serverCon,SSL_get_session(scon));\n#endif\n\tfor(;;) {\n\t\ti=SSL_connect(serverCon);\n\t\tif (BIO_sock_should_retry(i))\n\t\t\t{\n\t\t\tBIO_printf(bio_err,"DELAY\\n");\n\t\t\ti=SSL_get_fd(serverCon);\n\t\t\twidth=i+1;\n\t\t\tFD_ZERO(&readfds);\n\t\t\tFD_SET(i,&readfds);\n\t\t\tselect(width,&readfds,NULL,NULL,NULL);\n\t\t\tcontinue;\n\t\t\t}\n\t\tbreak;\n\t\t}\n\tif(i <= 0)\n\t\t{\n\t\tBIO_printf(bio_err,"ERROR\\n");\n\t\tif (verify_error != X509_V_OK)\n\t\t\tBIO_printf(bio_err,"verify error:%s\\n",\n\t\t\t\tX509_verify_cert_error_string(verify_error));\n\t\telse\n\t\t\tERR_print_errors(bio_err);\n\t\tif (scon == NULL)\n\t\t\tSSL_free(serverCon);\n\t\treturn NULL;\n\t\t}\n\treturn serverCon;\n\t}', 'void SSL_free(SSL *s)\n\t{\n\tint i;\n\tif(s == NULL)\n\t return;\n\ti=CRYPTO_add(&s->references,-1,CRYPTO_LOCK_SSL);\n#ifdef REF_PRINT\n\tREF_PRINT("SSL",s);\n#endif\n\tif (i > 0) return;\n#ifdef REF_CHECK\n\tif (i < 0)\n\t\t{\n\t\tfprintf(stderr,"SSL_free, bad reference count\\n");\n\t\tabort();\n\t\t}\n#endif\n\tCRYPTO_free_ex_data(ssl_meth,(char *)s,&s->ex_data);\n\tif (s->bbio != NULL)\n\t\t{\n\t\tif (s->bbio == s->wbio)\n\t\t\t{\n\t\t\ts->wbio=BIO_pop(s->wbio);\n\t\t\t}\n\t\tBIO_free(s->bbio);\n\t\ts->bbio=NULL;\n\t\t}\n\tif (s->rbio != NULL)\n\t\tBIO_free_all(s->rbio);\n\tif ((s->wbio != NULL) && (s->wbio != s->rbio))\n\t\tBIO_free_all(s->wbio);\n\tif (s->init_buf != NULL) BUF_MEM_free(s->init_buf);\n\tif (s->cipher_list != NULL) sk_SSL_CIPHER_free(s->cipher_list);\n\tif (s->cipher_list_by_id != NULL) sk_SSL_CIPHER_free(s->cipher_list_by_id);\n\tif (s->session != NULL)\n\t\t{\n\t\tssl_clear_bad_session(s);\n\t\tSSL_SESSION_free(s->session);\n\t\t}\n\tssl_clear_cipher_ctx(s);\n\tif (s->cert != NULL) ssl_cert_free(s->cert);\n\tif (s->ctx) SSL_CTX_free(s->ctx);\n\tif (s->client_CA != NULL)\n\t\tsk_X509_NAME_pop_free(s->client_CA,X509_NAME_free);\n\tif (s->method != NULL) s->method->ssl_free(s);\n\tFree((char *)s);\n\t}', 'int ssl_clear_bad_session(SSL *s)\n\t{\n\tif (\t(s->session != NULL) &&\n\t\t!(s->shutdown & SSL_SENT_SHUTDOWN) &&\n\t\t!(SSL_in_init(s) || SSL_in_before(s)))\n\t\t{\n\t\tSSL_CTX_remove_session(s->ctx,s->session);\n\t\treturn(1);\n\t\t}\n\telse\n\t\treturn(0);\n\t}', 'int SSL_CTX_remove_session(SSL_CTX *ctx, SSL_SESSION *c)\n{\n\treturn remove_session_lock(ctx, c, 1);\n}', 'static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lck)\n\t{\n\tSSL_SESSION *r;\n\tint ret=0;\n\tif ((c != NULL) && (c->session_id_length != 0))\n\t\t{\n\t\tif(lck) CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);\n\t\tr=(SSL_SESSION *)lh_delete(ctx->sessions,(char *)c);\n\t\tif (r != NULL)\n\t\t\t{\n\t\t\tret=1;\n\t\t\tSSL_SESSION_list_remove(ctx,c);\n\t\t\t}\n\t\tif(lck) CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);\n\t\tif (ret)\n\t\t\t{\n\t\t\tr->not_resumable=1;\n\t\t\tif (ctx->remove_session_cb != NULL)\n\t\t\t\tctx->remove_session_cb(ctx,r);\n\t\t\tSSL_SESSION_free(r);\n\t\t\t}\n\t\t}\n\telse\n\t\tret=0;\n\treturn(ret);\n\t}', 'char *lh_delete(LHASH *lh, char *data)\n\t{\n\tunsigned long hash;\n\tLHASH_NODE *nn,**rn;\n\tchar *ret;\n\tlh->error=0;\n\trn=getrn(lh,data,&hash);\n\tif (*rn == NULL)\n\t\t{\n\t\tlh->num_no_delete++;\n\t\treturn(NULL);\n\t\t}\n\telse\n\t\t{\n\t\tnn= *rn;\n\t\t*rn=nn->next;\n\t\tret=nn->data;\n\t\tFree((char *)nn);\n\t\tlh->num_delete++;\n\t\t}\n\tlh->num_items--;\n\tif ((lh->num_nodes > MIN_NODES) &&\n\t\t(lh->down_load >= (lh->num_items*LH_LOAD_MULT/lh->num_nodes)))\n\t\tcontract(lh);\n\treturn(ret);\n\t}', 'static void contract(LHASH *lh)\n\t{\n\tLHASH_NODE **n,*n1,*np;\n\tnp=lh->b[lh->p+lh->pmax-1];\n\tlh->b[lh->p+lh->pmax-1]=NULL;\n\tif (lh->p == 0)\n\t\t{\n\t\tn=(LHASH_NODE **)Realloc((char *)lh->b,\n\t\t\t(unsigned int)(sizeof(LHASH_NODE *)*lh->pmax));\n\t\tif (n == NULL)\n\t\t\t{\n\t\t\tlh->error++;\n\t\t\treturn;\n\t\t\t}\n\t\tlh->num_contract_reallocs++;\n\t\tlh->num_alloc_nodes/=2;\n\t\tlh->pmax/=2;\n\t\tlh->p=lh->pmax-1;\n\t\tlh->b=n;\n\t\t}\n\telse\n\t\tlh->p--;\n\tlh->num_nodes--;\n\tlh->num_contracts++;\n\tn1=lh->b[(int)lh->p];\n\tif (n1 == NULL)\n\t\tlh->b[(int)lh->p]=np;\n\telse\n\t\t{\n\t\twhile (n1->next != NULL)\n\t\t\tn1=n1->next;\n\t\tn1->next=np;\n\t\t}\n\t}']
280
0
https://github.com/openssl/openssl/blob/fa9bb6201e1d16ba8ccab938833d140ef81a7f73/crypto/bn/bn_shift.c/#L161
int BN_lshift(BIGNUM *r, const BIGNUM *a, int n) { int i, nw, lb, rb; BN_ULONG *t, *f; BN_ULONG l; bn_check_top(r); bn_check_top(a); if (n < 0) { BNerr(BN_F_BN_LSHIFT, BN_R_INVALID_SHIFT); return 0; } r->neg = a->neg; nw = n / BN_BITS2; if (bn_wexpand(r, a->top + nw + 1) == NULL) return (0); lb = n % BN_BITS2; rb = BN_BITS2 - lb; f = a->d; t = r->d; t[a->top + nw] = 0; if (lb == 0) for (i = a->top - 1; i >= 0; i--) t[nw + i] = f[i]; else for (i = a->top - 1; i >= 0; i--) { l = f[i]; t[nw + i + 1] |= (l >> rb) & BN_MASK2; t[nw + i] = (l << lb) & BN_MASK2; } memset(t, 0, sizeof(*t) * nw); r->top = a->top + nw + 1; bn_correct_top(r); bn_check_top(r); return (1); }
['int ec_GFp_simple_dbl(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,\n BN_CTX *ctx)\n{\n int (*field_mul) (const EC_GROUP *, BIGNUM *, const BIGNUM *,\n const BIGNUM *, BN_CTX *);\n int (*field_sqr) (const EC_GROUP *, BIGNUM *, const BIGNUM *, BN_CTX *);\n const BIGNUM *p;\n BN_CTX *new_ctx = NULL;\n BIGNUM *n0, *n1, *n2, *n3;\n int ret = 0;\n if (EC_POINT_is_at_infinity(group, a)) {\n BN_zero(r->Z);\n r->Z_is_one = 0;\n return 1;\n }\n field_mul = group->meth->field_mul;\n field_sqr = group->meth->field_sqr;\n p = group->field;\n if (ctx == NULL) {\n ctx = new_ctx = BN_CTX_new();\n if (ctx == NULL)\n return 0;\n }\n BN_CTX_start(ctx);\n n0 = BN_CTX_get(ctx);\n n1 = BN_CTX_get(ctx);\n n2 = BN_CTX_get(ctx);\n n3 = BN_CTX_get(ctx);\n if (n3 == NULL)\n goto err;\n if (a->Z_is_one) {\n if (!field_sqr(group, n0, a->X, ctx))\n goto err;\n if (!BN_mod_lshift1_quick(n1, n0, p))\n goto err;\n if (!BN_mod_add_quick(n0, n0, n1, p))\n goto err;\n if (!BN_mod_add_quick(n1, n0, group->a, p))\n goto err;\n } else if (group->a_is_minus3) {\n if (!field_sqr(group, n1, a->Z, ctx))\n goto err;\n if (!BN_mod_add_quick(n0, a->X, n1, p))\n goto err;\n if (!BN_mod_sub_quick(n2, a->X, n1, p))\n goto err;\n if (!field_mul(group, n1, n0, n2, ctx))\n goto err;\n if (!BN_mod_lshift1_quick(n0, n1, p))\n goto err;\n if (!BN_mod_add_quick(n1, n0, n1, p))\n goto err;\n } else {\n if (!field_sqr(group, n0, a->X, ctx))\n goto err;\n if (!BN_mod_lshift1_quick(n1, n0, p))\n goto err;\n if (!BN_mod_add_quick(n0, n0, n1, p))\n goto err;\n if (!field_sqr(group, n1, a->Z, ctx))\n goto err;\n if (!field_sqr(group, n1, n1, ctx))\n goto err;\n if (!field_mul(group, n1, n1, group->a, ctx))\n goto err;\n if (!BN_mod_add_quick(n1, n1, n0, p))\n goto err;\n }\n if (a->Z_is_one) {\n if (!BN_copy(n0, a->Y))\n goto err;\n } else {\n if (!field_mul(group, n0, a->Y, a->Z, ctx))\n goto err;\n }\n if (!BN_mod_lshift1_quick(r->Z, n0, p))\n goto err;\n r->Z_is_one = 0;\n if (!field_sqr(group, n3, a->Y, ctx))\n goto err;\n if (!field_mul(group, n2, a->X, n3, ctx))\n goto err;\n if (!BN_mod_lshift_quick(n2, n2, 2, p))\n goto err;\n if (!BN_mod_lshift1_quick(n0, n2, p))\n goto err;\n if (!field_sqr(group, r->X, n1, ctx))\n goto err;\n if (!BN_mod_sub_quick(r->X, r->X, n0, p))\n goto err;\n if (!field_sqr(group, n0, n3, ctx))\n goto err;\n if (!BN_mod_lshift_quick(n3, n0, 3, p))\n goto err;\n if (!BN_mod_sub_quick(n0, n2, r->X, p))\n goto err;\n if (!field_mul(group, n0, n1, n0, ctx))\n goto err;\n if (!BN_mod_sub_quick(r->Y, n0, n3, p))\n goto err;\n ret = 1;\n err:\n BN_CTX_end(ctx);\n BN_CTX_free(new_ctx);\n return ret;\n}', 'int BN_mod_lshift_quick(BIGNUM *r, const BIGNUM *a, int n, const BIGNUM *m)\n{\n if (r != a) {\n if (BN_copy(r, a) == NULL)\n return 0;\n }\n while (n > 0) {\n int max_shift;\n max_shift = BN_num_bits(m) - BN_num_bits(r);\n if (max_shift < 0) {\n BNerr(BN_F_BN_MOD_LSHIFT_QUICK, BN_R_INPUT_NOT_REDUCED);\n return 0;\n }\n if (max_shift > n)\n max_shift = n;\n if (max_shift) {\n if (!BN_lshift(r, r, max_shift))\n return 0;\n n -= max_shift;\n } else {\n if (!BN_lshift1(r, r))\n return 0;\n --n;\n }\n if (BN_cmp(r, m) >= 0) {\n if (!BN_sub(r, r, m))\n return 0;\n }\n }\n bn_check_top(r);\n return 1;\n}', 'int BN_lshift(BIGNUM *r, const BIGNUM *a, int n)\n{\n int i, nw, lb, rb;\n BN_ULONG *t, *f;\n BN_ULONG l;\n bn_check_top(r);\n bn_check_top(a);\n if (n < 0) {\n BNerr(BN_F_BN_LSHIFT, BN_R_INVALID_SHIFT);\n return 0;\n }\n r->neg = a->neg;\n nw = n / BN_BITS2;\n if (bn_wexpand(r, a->top + nw + 1) == NULL)\n return (0);\n lb = n % BN_BITS2;\n rb = BN_BITS2 - lb;\n f = a->d;\n t = r->d;\n t[a->top + nw] = 0;\n if (lb == 0)\n for (i = a->top - 1; i >= 0; i--)\n t[nw + i] = f[i];\n else\n for (i = a->top - 1; i >= 0; i--) {\n l = f[i];\n t[nw + i + 1] |= (l >> rb) & BN_MASK2;\n t[nw + i] = (l << lb) & BN_MASK2;\n }\n memset(t, 0, sizeof(*t) * nw);\n r->top = a->top + nw + 1;\n bn_correct_top(r);\n bn_check_top(r);\n return (1);\n}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n return (words <= a->dmax) ? a : bn_expand2(a, words);\n}']
281
0
https://github.com/openssl/openssl/blob/18e1e302452e6dea4500b6f981cee7e151294dea/crypto/bn/bn_ctx.c/#L340
static void BN_POOL_release(BN_POOL *p, unsigned int num) { unsigned int offset = (p->used - 1) % BN_CTX_POOL_SIZE; p->used -= num; while (num--) { bn_check_top(p->current->vals + offset); if (offset == 0) { offset = BN_CTX_POOL_SIZE - 1; p->current = p->current->prev; } else offset--; } }
['int BN_mod_exp_simple(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,\n const BIGNUM *m, BN_CTX *ctx)\n{\n int i, j, bits, ret = 0, wstart, wend, window, wvalue;\n int start = 1;\n BIGNUM *d;\n BIGNUM *val[TABLE_SIZE];\n if (BN_get_flags(p, BN_FLG_CONSTTIME) != 0\n || BN_get_flags(a, BN_FLG_CONSTTIME) != 0\n || BN_get_flags(m, BN_FLG_CONSTTIME) != 0) {\n BNerr(BN_F_BN_MOD_EXP_SIMPLE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);\n return 0;\n }\n bits = BN_num_bits(p);\n if (bits == 0) {\n if (BN_abs_is_word(m, 1)) {\n ret = 1;\n BN_zero(r);\n } else {\n ret = BN_one(r);\n }\n return ret;\n }\n BN_CTX_start(ctx);\n d = BN_CTX_get(ctx);\n val[0] = BN_CTX_get(ctx);\n if (val[0] == NULL)\n goto err;\n if (!BN_nnmod(val[0], a, m, ctx))\n goto err;\n if (BN_is_zero(val[0])) {\n BN_zero(r);\n ret = 1;\n goto err;\n }\n window = BN_window_bits_for_exponent_size(bits);\n if (window > 1) {\n if (!BN_mod_mul(d, val[0], val[0], m, ctx))\n goto err;\n j = 1 << (window - 1);\n for (i = 1; i < j; i++) {\n if (((val[i] = BN_CTX_get(ctx)) == NULL) ||\n !BN_mod_mul(val[i], val[i - 1], d, m, ctx))\n goto err;\n }\n }\n start = 1;\n wvalue = 0;\n wstart = bits - 1;\n wend = 0;\n if (!BN_one(r))\n goto err;\n for (;;) {\n if (BN_is_bit_set(p, wstart) == 0) {\n if (!start)\n if (!BN_mod_mul(r, r, r, m, ctx))\n goto err;\n if (wstart == 0)\n break;\n wstart--;\n continue;\n }\n j = wstart;\n wvalue = 1;\n wend = 0;\n for (i = 1; i < window; i++) {\n if (wstart - i < 0)\n break;\n if (BN_is_bit_set(p, wstart - i)) {\n wvalue <<= (i - wend);\n wvalue |= 1;\n wend = i;\n }\n }\n j = wend + 1;\n if (!start)\n for (i = 0; i < j; i++) {\n if (!BN_mod_mul(r, r, r, m, ctx))\n goto err;\n }\n if (!BN_mod_mul(r, r, val[wvalue >> 1], m, ctx))\n goto err;\n wstart -= wend + 1;\n wvalue = 0;\n start = 0;\n if (wstart < 0)\n break;\n }\n ret = 1;\n err:\n BN_CTX_end(ctx);\n bn_check_top(r);\n return ret;\n}', 'int BN_nnmod(BIGNUM *r, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx)\n{\n if (!(BN_mod(r, m, d, ctx)))\n return 0;\n if (!r->neg)\n return 1;\n return (d->neg ? BN_sub : BN_add) (r, r, d);\n}', 'int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,\n BN_CTX *ctx)\n{\n int ret;\n if (BN_is_zero(divisor)) {\n BNerr(BN_F_BN_DIV, BN_R_DIV_BY_ZERO);\n return 0;\n }\n if (divisor->d[divisor->top - 1] == 0) {\n BNerr(BN_F_BN_DIV, BN_R_NOT_INITIALIZED);\n return 0;\n }\n ret = bn_div_fixed_top(dv, rm, num, divisor, ctx);\n if (ret) {\n if (dv != NULL)\n bn_correct_top(dv);\n if (rm != NULL)\n bn_correct_top(rm);\n }\n return ret;\n}', 'int BN_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("ENTER BN_CTX_get()", ctx);\n if (ctx->err_stack || ctx->too_many)\n return NULL;\n if ((ret = BN_POOL_get(&ctx->pool, ctx->flags)) == NULL) {\n ctx->too_many = 1;\n BNerr(BN_F_BN_CTX_GET, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n return NULL;\n }\n BN_zero(ret);\n ret->flags &= (~BN_FLG_CONSTTIME);\n ctx->used++;\n CTXDBG("LEAVE BN_CTX_get()", ctx);\n return ret;\n}', 'int BN_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)\n{\n int ret = bn_mul_fixed_top(r, a, b, ctx);\n bn_correct_top(r);\n bn_check_top(r);\n return ret;\n}', 'int bn_mul_fixed_top(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)\n{\n int ret = 0;\n int top, al, bl;\n BIGNUM *rr;\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n int i;\n#endif\n#ifdef BN_RECURSION\n BIGNUM *t = NULL;\n int j = 0, k;\n#endif\n bn_check_top(a);\n bn_check_top(b);\n bn_check_top(r);\n al = a->top;\n bl = b->top;\n if ((al == 0) || (bl == 0)) {\n BN_zero(r);\n return 1;\n }\n top = al + bl;\n BN_CTX_start(ctx);\n if ((r == a) || (r == b)) {\n if ((rr = BN_CTX_get(ctx)) == NULL)\n goto err;\n } else\n rr = r;\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n i = al - bl;\n#endif\n#ifdef BN_MUL_COMBA\n if (i == 0) {\n# if 0\n if (al == 4) {\n if (bn_wexpand(rr, 8) == NULL)\n goto err;\n rr->top = 8;\n bn_mul_comba4(rr->d, a->d, b->d);\n goto end;\n }\n# endif\n if (al == 8) {\n if (bn_wexpand(rr, 16) == NULL)\n goto err;\n rr->top = 16;\n bn_mul_comba8(rr->d, a->d, b->d);\n goto end;\n }\n }\n#endif\n#ifdef BN_RECURSION\n if ((al >= BN_MULL_SIZE_NORMAL) && (bl >= BN_MULL_SIZE_NORMAL)) {\n if (i >= -1 && i <= 1) {\n if (i >= 0) {\n j = BN_num_bits_word((BN_ULONG)al);\n }\n if (i == -1) {\n j = BN_num_bits_word((BN_ULONG)bl);\n }\n j = 1 << (j - 1);\n assert(j <= al || j <= bl);\n k = j + j;\n t = BN_CTX_get(ctx);\n if (t == NULL)\n goto err;\n if (al > j || bl > j) {\n if (bn_wexpand(t, k * 4) == NULL)\n goto err;\n if (bn_wexpand(rr, k * 4) == NULL)\n goto err;\n bn_mul_part_recursive(rr->d, a->d, b->d,\n j, al - j, bl - j, t->d);\n } else {\n if (bn_wexpand(t, k * 2) == NULL)\n goto err;\n if (bn_wexpand(rr, k * 2) == NULL)\n goto err;\n bn_mul_recursive(rr->d, a->d, b->d, j, al - j, bl - j, t->d);\n }\n rr->top = top;\n goto end;\n }\n }\n#endif\n if (bn_wexpand(rr, top) == NULL)\n goto err;\n rr->top = top;\n bn_mul_normal(rr->d, a->d, al, b->d, bl);\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n end:\n#endif\n rr->neg = a->neg ^ b->neg;\n rr->flags |= BN_FLG_FIXED_TOP;\n if (r != rr && BN_copy(r, rr) == NULL)\n goto err;\n ret = 1;\n err:\n bn_check_top(r);\n BN_CTX_end(ctx);\n return ret;\n}', 'void BN_CTX_end(BN_CTX *ctx)\n{\n CTXDBG("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 void BN_POOL_release(BN_POOL *p, unsigned int num)\n{\n unsigned int offset = (p->used - 1) % BN_CTX_POOL_SIZE;\n p->used -= num;\n while (num--) {\n bn_check_top(p->current->vals + offset);\n if (offset == 0) {\n offset = BN_CTX_POOL_SIZE - 1;\n p->current = p->current->prev;\n } else\n offset--;\n }\n}']
282
0
https://github.com/openssl/openssl/blob/ea32151f7b9353f8906188d007c6893704ac17bb/crypto/lhash/lhash.c/#L222
static void expand(OPENSSL_LHASH *lh) { OPENSSL_LH_NODE **n, **n1, **n2, *np; unsigned int p, i, j; unsigned long hash, nni; lh->num_nodes++; lh->num_expands++; p = (int)lh->p++; n1 = &(lh->b[p]); n2 = &(lh->b[p + (int)lh->pmax]); *n2 = NULL; nni = lh->num_alloc_nodes; for (np = *n1; np != NULL;) { hash = np->hash; if ((hash % nni) != p) { *n1 = (*n1)->next; np->next = *n2; *n2 = np; } else n1 = &((*n1)->next); np = *n1; } if ((lh->p) >= lh->pmax) { j = (int)lh->num_alloc_nodes * 2; n = OPENSSL_realloc(lh->b, (int)(sizeof(OPENSSL_LH_NODE *) * j)); if (n == NULL) { lh->error++; lh->p = 0; return; } for (i = (int)lh->num_alloc_nodes; i < j; i++) n[i] = NULL; lh->pmax = lh->num_alloc_nodes; lh->num_alloc_nodes = j; lh->num_expand_reallocs++; lh->p = 0; lh->b = n; } }
['STACK_OF(X509_NAME) *SSL_load_client_CA_file(const char *file)\n{\n BIO *in = BIO_new(BIO_s_file());\n X509 *x = NULL;\n X509_NAME *xn = NULL;\n STACK_OF(X509_NAME) *ret = NULL;\n LHASH_OF(X509_NAME) *name_hash =\n lh_X509_NAME_new(xname_hash, xname_cmp);\n if ((name_hash == NULL) || (in == NULL)) {\n SSLerr(SSL_F_SSL_LOAD_CLIENT_CA_FILE, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n if (!BIO_read_filename(in, file))\n goto err;\n for (;;) {\n if (PEM_read_bio_X509(in, &x, NULL, NULL) == NULL)\n break;\n if (ret == NULL) {\n ret = sk_X509_NAME_new_null();\n if (ret == NULL) {\n SSLerr(SSL_F_SSL_LOAD_CLIENT_CA_FILE, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n }\n if ((xn = X509_get_subject_name(x)) == NULL)\n goto err;\n xn = X509_NAME_dup(xn);\n if (xn == NULL)\n goto err;\n if (lh_X509_NAME_retrieve(name_hash, xn) != NULL) {\n X509_NAME_free(xn);\n } else {\n lh_X509_NAME_insert(name_hash, xn);\n sk_X509_NAME_push(ret, xn);\n }\n }\n goto done;\n err:\n sk_X509_NAME_pop_free(ret, X509_NAME_free);\n ret = NULL;\n done:\n BIO_free(in);\n X509_free(x);\n lh_X509_NAME_free(name_hash);\n if (ret != NULL)\n ERR_clear_error();\n return (ret);\n}', 'DEFINE_LHASH_OF(X509_NAME)', 'OPENSSL_LHASH *OPENSSL_LH_new(OPENSSL_LH_HASHFUNC h, OPENSSL_LH_COMPFUNC c)\n{\n OPENSSL_LHASH *ret;\n if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL)\n goto err0;\n if ((ret->b = OPENSSL_zalloc(sizeof(*ret->b) * MIN_NODES)) == NULL)\n goto err1;\n ret->comp = ((c == NULL) ? (OPENSSL_LH_COMPFUNC)strcmp : c);\n ret->hash = ((h == NULL) ? (OPENSSL_LH_HASHFUNC)OPENSSL_LH_strhash : h);\n ret->num_nodes = MIN_NODES / 2;\n ret->num_alloc_nodes = MIN_NODES;\n ret->pmax = MIN_NODES / 2;\n ret->up_load = UP_LOAD;\n ret->down_load = DOWN_LOAD;\n return (ret);\n err1:\n OPENSSL_free(ret);\n err0:\n return (NULL);\n}', 'void *OPENSSL_LH_insert(OPENSSL_LHASH *lh, void *data)\n{\n unsigned long hash;\n OPENSSL_LH_NODE *nn, **rn;\n void *ret;\n lh->error = 0;\n if (lh->up_load <= (lh->num_items * LH_LOAD_MULT / lh->num_nodes))\n expand(lh);\n rn = getrn(lh, data, &hash);\n if (*rn == NULL) {\n if ((nn = OPENSSL_malloc(sizeof(*nn))) == NULL) {\n lh->error++;\n return (NULL);\n }\n nn->data = data;\n nn->next = NULL;\n nn->hash = hash;\n *rn = nn;\n ret = NULL;\n lh->num_insert++;\n lh->num_items++;\n } else {\n ret = (*rn)->data;\n (*rn)->data = data;\n lh->num_replace++;\n }\n return (ret);\n}', 'static void expand(OPENSSL_LHASH *lh)\n{\n OPENSSL_LH_NODE **n, **n1, **n2, *np;\n unsigned int p, i, j;\n unsigned long hash, nni;\n lh->num_nodes++;\n lh->num_expands++;\n p = (int)lh->p++;\n n1 = &(lh->b[p]);\n n2 = &(lh->b[p + (int)lh->pmax]);\n *n2 = NULL;\n nni = lh->num_alloc_nodes;\n for (np = *n1; np != NULL;) {\n hash = np->hash;\n if ((hash % nni) != p) {\n *n1 = (*n1)->next;\n np->next = *n2;\n *n2 = np;\n } else\n n1 = &((*n1)->next);\n np = *n1;\n }\n if ((lh->p) >= lh->pmax) {\n j = (int)lh->num_alloc_nodes * 2;\n n = OPENSSL_realloc(lh->b, (int)(sizeof(OPENSSL_LH_NODE *) * j));\n if (n == NULL) {\n lh->error++;\n lh->p = 0;\n return;\n }\n for (i = (int)lh->num_alloc_nodes; i < j; i++)\n n[i] = NULL;\n lh->pmax = lh->num_alloc_nodes;\n lh->num_alloc_nodes = j;\n lh->num_expand_reallocs++;\n lh->p = 0;\n lh->b = n;\n }\n}', 'void *CRYPTO_realloc(void *str, size_t num, const char *file, int line)\n{\n if (realloc_impl != NULL && realloc_impl != &CRYPTO_realloc)\n return realloc_impl(str, num, file, line);\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 allow_customize = 0;\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\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 osslargused(file); osslargused(line);\n#endif\n return realloc(str, num);\n}']
283
0
https://github.com/libav/libav/blob/2f99117f6ff24ce5be2abb9e014cb8b86c2aa0e0/libavcodec/bitstream.h/#L68
static inline void refill_32(BitstreamContext *bc) { if (bc->ptr >= bc->buffer_end) return; #ifdef BITSTREAM_READER_LE bc->bits = (uint64_t)AV_RL32(bc->ptr) << bc->bits_left | bc->bits; #else bc->bits = bc->bits | (uint64_t)AV_RB32(bc->ptr) << (32 - bc->bits_left); #endif bc->ptr += 4; bc->bits_left += 32; }
['static int gsm_decode_frame(AVCodecContext *avctx, void *data,\n int *got_frame_ptr, AVPacket *avpkt)\n{\n AVFrame *frame = data;\n int res;\n BitstreamContext bc;\n const uint8_t *buf = avpkt->data;\n int buf_size = avpkt->size;\n int16_t *samples;\n if (buf_size < avctx->block_align) {\n av_log(avctx, AV_LOG_ERROR, "Packet is too small\\n");\n return AVERROR_INVALIDDATA;\n }\n frame->nb_samples = avctx->frame_size;\n if ((res = ff_get_buffer(avctx, frame, 0)) < 0) {\n av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\\n");\n return res;\n }\n samples = (int16_t *)frame->data[0];\n switch (avctx->codec_id) {\n case AV_CODEC_ID_GSM:\n bitstream_init(&bc, buf, buf_size * 8);\n if (bitstream_read(&bc, 4) != 0xd)\n av_log(avctx, AV_LOG_WARNING, "Missing GSM magic!\\n");\n res = gsm_decode_block(avctx, samples, &bc, GSM_13000);\n if (res < 0)\n return res;\n break;\n case AV_CODEC_ID_GSM_MS:\n res = ff_msgsm_decode_block(avctx, samples, buf,\n (GSM_MS_BLOCK_SIZE - avctx->block_align) / 3);\n if (res < 0)\n return res;\n }\n *got_frame_ptr = 1;\n return avctx->block_align;\n}', 'static inline int bitstream_init(BitstreamContext *bc, const uint8_t *buffer,\n unsigned bit_size)\n{\n unsigned buffer_size;\n if (bit_size > INT_MAX - 7 || !buffer) {\n buffer =\n bc->buffer =\n bc->ptr = NULL;\n bc->bits_left = 0;\n return AVERROR_INVALIDDATA;\n }\n buffer_size = (bit_size + 7) >> 3;\n bc->buffer = buffer;\n bc->buffer_end = buffer + buffer_size;\n bc->ptr = bc->buffer;\n bc->size_in_bits = bit_size;\n bc->bits_left = 0;\n bc->bits = 0;\n refill_64(bc);\n return 0;\n}', 'static inline uint32_t bitstream_read(BitstreamContext *bc, unsigned n)\n{\n if (!n)\n return 0;\n if (n > bc->bits_left) {\n refill_32(bc);\n if (bc->bits_left < 32)\n bc->bits_left = n;\n }\n return get_val(bc, n);\n}', 'static inline void refill_32(BitstreamContext *bc)\n{\n if (bc->ptr >= bc->buffer_end)\n return;\n#ifdef BITSTREAM_READER_LE\n bc->bits = (uint64_t)AV_RL32(bc->ptr) << bc->bits_left | bc->bits;\n#else\n bc->bits = bc->bits | (uint64_t)AV_RB32(bc->ptr) << (32 - bc->bits_left);\n#endif\n bc->ptr += 4;\n bc->bits_left += 32;\n}']
284
0
https://github.com/openssl/openssl/blob/f4675379275c304dbfa593cc573b4e4c4eb54bd4/crypto/bn/bn_lib.c/#L295
BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b) { bn_check_top(b); if (a == b) return a; if (bn_wexpand(a, b->top) == NULL) return NULL; if (b->top > 0) memcpy(a->d, b->d, sizeof(b->d[0]) * b->top); a->top = b->top; a->neg = b->neg; bn_check_top(a); return a; }
['int ecdh_simple_compute_key(unsigned char **pout, size_t *poutlen,\n const EC_POINT *pub_key, const EC_KEY *ecdh)\n{\n BN_CTX *ctx;\n EC_POINT *tmp = NULL;\n BIGNUM *x = NULL;\n const BIGNUM *priv_key;\n const EC_GROUP *group;\n int ret = 0;\n size_t buflen, len;\n unsigned char *buf = NULL;\n if ((ctx = BN_CTX_new()) == NULL)\n goto err;\n BN_CTX_start(ctx);\n x = BN_CTX_get(ctx);\n if (x == NULL) {\n ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n priv_key = EC_KEY_get0_private_key(ecdh);\n if (priv_key == NULL) {\n ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, EC_R_NO_PRIVATE_VALUE);\n goto err;\n }\n group = EC_KEY_get0_group(ecdh);\n if (EC_KEY_get_flags(ecdh) & EC_FLAG_COFACTOR_ECDH) {\n if (!EC_GROUP_get_cofactor(group, x, NULL) ||\n !BN_mul(x, x, priv_key, ctx)) {\n ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n priv_key = x;\n }\n if ((tmp = EC_POINT_new(group)) == NULL) {\n ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n if (!EC_POINT_mul(group, tmp, NULL, pub_key, priv_key, ctx)) {\n ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, EC_R_POINT_ARITHMETIC_FAILURE);\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, tmp, x, NULL, ctx)) {\n ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, EC_R_POINT_ARITHMETIC_FAILURE);\n goto err;\n }\n }\n#ifndef OPENSSL_NO_EC2M\n else {\n if (!EC_POINT_get_affine_coordinates_GF2m(group, tmp, x, NULL, ctx)) {\n ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, EC_R_POINT_ARITHMETIC_FAILURE);\n goto err;\n }\n }\n#endif\n buflen = (EC_GROUP_get_degree(group) + 7) / 8;\n len = BN_num_bytes(x);\n if (len > buflen) {\n ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, ERR_R_INTERNAL_ERROR);\n goto err;\n }\n if ((buf = OPENSSL_malloc(buflen)) == NULL) {\n ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, ERR_R_MALLOC_FAILURE);\n goto err;\n }\n memset(buf, 0, buflen - len);\n if (len != (size_t)BN_bn2bin(x, buf + buflen - len)) {\n ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, ERR_R_BN_LIB);\n goto err;\n }\n *pout = buf;\n *poutlen = buflen;\n buf = NULL;\n ret = 1;\n err:\n EC_POINT_free(tmp);\n if (ctx)\n BN_CTX_end(ctx);\n BN_CTX_free(ctx);\n OPENSSL_free(buf);\n return ret;\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 ((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 if (group->meth != r->meth) {\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 for (i = 0; i < num; i++) {\n if (group->meth != points[i]->meth) {\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, order_bits, group_top, kbit, pbit, Z_is_one;\n EC_POINT *s = NULL;\n BIGNUM *k = NULL;\n BIGNUM *lambda = NULL;\n BN_CTX *new_ctx = NULL;\n int ret = 0;\n if (ctx == NULL && (ctx = new_ctx = BN_CTX_secure_new()) == NULL)\n goto err;\n if ((group->order == NULL) || (group->field == NULL))\n goto err;\n order_bits = BN_num_bits(group->order);\n s = EC_POINT_new(group);\n if (s == NULL)\n goto err;\n if (point == NULL) {\n if (group->generator == NULL)\n goto err;\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 BN_CTX_start(ctx);\n lambda = BN_CTX_get(ctx);\n k = BN_CTX_get(ctx);\n if (k == NULL)\n goto err;\n group_top = bn_get_top(group->order);\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) > order_bits) || (BN_is_negative(k))) {\n if (!BN_nnmod(k, k, group->order, ctx))\n goto err;\n }\n if (!BN_add(lambda, k, group->order))\n goto err;\n BN_set_flags(lambda, BN_FLG_CONSTTIME);\n if (!BN_add(k, lambda, group->order))\n goto err;\n kbit = BN_is_bit_set(lambda, order_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_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 = order_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->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}']
285
0
https://github.com/libav/libav/blob/fcda30f2dcb744d89df9d5d1ec89ba55279cb83c/cmdutils.c/#L1384
int show_help(void *optctx, const char *opt, const char *arg) { char *topic, *par; av_log_set_callback(log_callback_help); topic = av_strdup(arg ? arg : ""); if (!topic) return AVERROR(ENOMEM); par = strchr(topic, '='); if (par) *par++ = 0; if (!*topic) { show_help_default(topic, par); } else if (!strcmp(topic, "decoder")) { show_help_codec(par, 0); } else if (!strcmp(topic, "encoder")) { show_help_codec(par, 1); } else if (!strcmp(topic, "demuxer")) { show_help_demuxer(par); } else if (!strcmp(topic, "muxer")) { show_help_muxer(par); #if CONFIG_AVFILTER } else if (!strcmp(topic, "filter")) { show_help_filter(par); #endif } else { show_help_default(topic, par); } av_freep(&topic); return 0; }
['int show_help(void *optctx, const char *opt, const char *arg)\n{\n char *topic, *par;\n av_log_set_callback(log_callback_help);\n topic = av_strdup(arg ? arg : "");\n if (!topic)\n return AVERROR(ENOMEM);\n par = strchr(topic, \'=\');\n if (par)\n *par++ = 0;\n if (!*topic) {\n show_help_default(topic, par);\n } else if (!strcmp(topic, "decoder")) {\n show_help_codec(par, 0);\n } else if (!strcmp(topic, "encoder")) {\n show_help_codec(par, 1);\n } else if (!strcmp(topic, "demuxer")) {\n show_help_demuxer(par);\n } else if (!strcmp(topic, "muxer")) {\n show_help_muxer(par);\n#if CONFIG_AVFILTER\n } else if (!strcmp(topic, "filter")) {\n show_help_filter(par);\n#endif\n } else {\n show_help_default(topic, par);\n }\n av_freep(&topic);\n return 0;\n}', 'void av_log_set_callback(void (*callback)(void*, int, const char*, va_list))\n{\n av_log_callback = callback;\n}', 'char *av_strdup(const char *s)\n{\n char *ptr = NULL;\n if (s) {\n int len = strlen(s) + 1;\n ptr = av_realloc(NULL, len);\n if (ptr)\n memcpy(ptr, s, len);\n }\n return ptr;\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 void show_help_codec(const char *name, int encoder)\n{\n const AVCodecDescriptor *desc;\n const AVCodec *codec;\n if (!name) {\n av_log(NULL, AV_LOG_ERROR, "No codec name specified.\\n");\n return;\n }\n codec = encoder ? avcodec_find_encoder_by_name(name) :\n avcodec_find_decoder_by_name(name);\n if (codec)\n print_codec(codec);\n else if ((desc = avcodec_descriptor_get_by_name(name))) {\n int printed = 0;\n while ((codec = next_codec_for_id(desc->id, codec, encoder))) {\n printed = 1;\n print_codec(codec);\n }\n if (!printed) {\n av_log(NULL, AV_LOG_ERROR, "Codec \'%s\' is known to Libav, "\n "but no %s for it are available. Libav might need to be "\n "recompiled with additional external libraries.\\n",\n name, encoder ? "encoders" : "decoders");\n }\n } else {\n av_log(NULL, AV_LOG_ERROR, "Codec \'%s\' is not recognized by Libav.\\n",\n name);\n }\n}']
286
0
https://github.com/libav/libav/blob/8b9a153ef3673d5847291987fa0dcddeac4a640b/libavformat/utils.c/#L2661
void avformat_free_context(AVFormatContext *s) { int i; AVStream *st; av_opt_free(s); if (s->iformat && s->iformat->priv_class && s->priv_data) av_opt_free(s->priv_data); for(i=0;i<s->nb_streams;i++) { st = s->streams[i]; if (st->parser) { av_parser_close(st->parser); } if (st->attached_pic.data) av_free_packet(&st->attached_pic); av_dict_free(&st->metadata); av_free(st->index_entries); av_free(st->codec->extradata); av_free(st->codec->subtitle_header); av_free(st->codec); av_free(st->priv_data); av_free(st->info); av_free(st); } for(i=s->nb_programs-1; i>=0; i--) { av_dict_free(&s->programs[i]->metadata); av_freep(&s->programs[i]->stream_index); av_freep(&s->programs[i]); } av_freep(&s->programs); av_freep(&s->priv_data); while(s->nb_chapters--) { av_dict_free(&s->chapters[s->nb_chapters]->metadata); av_free(s->chapters[s->nb_chapters]); } av_freep(&s->chapters); av_dict_free(&s->metadata); av_freep(&s->streams); av_free(s); }
['static void exit_program(void)\n{\n int i, j;\n for (i = 0; i < nb_filtergraphs; i++) {\n avfilter_graph_free(&filtergraphs[i]->graph);\n for (j = 0; j < filtergraphs[i]->nb_inputs; j++) {\n av_freep(&filtergraphs[i]->inputs[j]->name);\n av_freep(&filtergraphs[i]->inputs[j]);\n }\n av_freep(&filtergraphs[i]->inputs);\n for (j = 0; j < filtergraphs[i]->nb_outputs; j++) {\n av_freep(&filtergraphs[i]->outputs[j]->name);\n av_freep(&filtergraphs[i]->outputs[j]);\n }\n av_freep(&filtergraphs[i]->outputs);\n av_freep(&filtergraphs[i]->graph_desc);\n av_freep(&filtergraphs[i]);\n }\n av_freep(&filtergraphs);\n for (i = 0; i < nb_output_files; i++) {\n AVFormatContext *s = output_files[i]->ctx;\n if (!(s->oformat->flags & AVFMT_NOFILE) && s->pb)\n avio_close(s->pb);\n avformat_free_context(s);\n av_dict_free(&output_files[i]->opts);\n av_freep(&output_files[i]);\n }\n for (i = 0; i < nb_output_streams; i++) {\n AVBitStreamFilterContext *bsfc = output_streams[i]->bitstream_filters;\n while (bsfc) {\n AVBitStreamFilterContext *next = bsfc->next;\n av_bitstream_filter_close(bsfc);\n bsfc = next;\n }\n output_streams[i]->bitstream_filters = NULL;\n avcodec_free_frame(&output_streams[i]->filtered_frame);\n av_freep(&output_streams[i]->forced_keyframes);\n av_freep(&output_streams[i]->avfilter);\n av_freep(&output_streams[i]->logfile_prefix);\n av_freep(&output_streams[i]);\n }\n for (i = 0; i < nb_input_files; i++) {\n avformat_close_input(&input_files[i]->ctx);\n av_freep(&input_files[i]);\n }\n for (i = 0; i < nb_input_streams; i++) {\n av_frame_free(&input_streams[i]->decoded_frame);\n av_frame_free(&input_streams[i]->filter_frame);\n av_dict_free(&input_streams[i]->opts);\n av_freep(&input_streams[i]->filters);\n av_freep(&input_streams[i]);\n }\n if (vstats_file)\n fclose(vstats_file);\n av_free(vstats_filename);\n av_freep(&input_streams);\n av_freep(&input_files);\n av_freep(&output_streams);\n av_freep(&output_files);\n uninit_opts();\n avfilter_uninit();\n avformat_network_deinit();\n if (received_sigterm) {\n av_log(NULL, AV_LOG_INFO, "Received signal %d: terminating.\\n",\n (int) received_sigterm);\n exit (255);\n }\n}', 'void av_freep(void *arg)\n{\n void **ptr = (void **)arg;\n av_free(*ptr);\n *ptr = NULL;\n}', 'void avformat_free_context(AVFormatContext *s)\n{\n int i;\n AVStream *st;\n av_opt_free(s);\n if (s->iformat && s->iformat->priv_class && s->priv_data)\n av_opt_free(s->priv_data);\n for(i=0;i<s->nb_streams;i++) {\n st = s->streams[i];\n if (st->parser) {\n av_parser_close(st->parser);\n }\n if (st->attached_pic.data)\n av_free_packet(&st->attached_pic);\n av_dict_free(&st->metadata);\n av_free(st->index_entries);\n av_free(st->codec->extradata);\n av_free(st->codec->subtitle_header);\n av_free(st->codec);\n av_free(st->priv_data);\n av_free(st->info);\n av_free(st);\n }\n for(i=s->nb_programs-1; i>=0; i--) {\n av_dict_free(&s->programs[i]->metadata);\n av_freep(&s->programs[i]->stream_index);\n av_freep(&s->programs[i]);\n }\n av_freep(&s->programs);\n av_freep(&s->priv_data);\n while(s->nb_chapters--) {\n av_dict_free(&s->chapters[s->nb_chapters]->metadata);\n av_free(s->chapters[s->nb_chapters]);\n }\n av_freep(&s->chapters);\n av_dict_free(&s->metadata);\n av_freep(&s->streams);\n av_free(s);\n}']
287
0
https://github.com/openssl/openssl/blob/9b02dc97e4963969da69675a871dbe80e6d31cda/crypto/bn/bn_lib.c/#L260
static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words) { BN_ULONG *a = NULL; bn_check_top(b); if (words > (INT_MAX / (4 * BN_BITS2))) { BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG); return NULL; } if (BN_get_flags(b, BN_FLG_STATIC_DATA)) { BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA); return NULL; } if (BN_get_flags(b, BN_FLG_SECURE)) a = OPENSSL_secure_zalloc(words * sizeof(*a)); else a = OPENSSL_zalloc(words * sizeof(*a)); if (a == NULL) { BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE); return NULL; } assert(b->top <= words); if (b->top > 0) memcpy(a, b->d, sizeof(*a) * b->top); return a; }
['int BN_generate_prime_ex(BIGNUM *ret, int bits, int safe,\n const BIGNUM *add, const BIGNUM *rem, BN_GENCB *cb)\n{\n BIGNUM *t;\n int found = 0;\n int i, j, c1 = 0;\n BN_CTX *ctx = NULL;\n prime_t *mods = NULL;\n int checks = BN_prime_checks_for_size(bits);\n if (bits < 2) {\n BNerr(BN_F_BN_GENERATE_PRIME_EX, BN_R_BITS_TOO_SMALL);\n return 0;\n } else if (bits == 2 && safe) {\n BNerr(BN_F_BN_GENERATE_PRIME_EX, BN_R_BITS_TOO_SMALL);\n return 0;\n }\n mods = OPENSSL_zalloc(sizeof(*mods) * NUMPRIMES);\n if (mods == NULL)\n goto err;\n ctx = BN_CTX_new();\n if (ctx == NULL)\n goto err;\n BN_CTX_start(ctx);\n t = BN_CTX_get(ctx);\n if (t == NULL)\n goto err;\n loop:\n if (add == NULL) {\n if (!probable_prime(ret, bits, mods))\n goto err;\n } else {\n if (safe) {\n if (!probable_prime_dh_safe(ret, bits, add, rem, ctx))\n goto err;\n } else {\n if (!bn_probable_prime_dh(ret, bits, add, rem, ctx))\n goto err;\n }\n }\n if (!BN_GENCB_call(cb, 0, c1++))\n goto err;\n if (!safe) {\n i = BN_is_prime_fasttest_ex(ret, checks, ctx, 0, cb);\n if (i == -1)\n goto err;\n if (i == 0)\n goto loop;\n } else {\n if (!BN_rshift1(t, ret))\n goto err;\n for (i = 0; i < checks; i++) {\n j = BN_is_prime_fasttest_ex(ret, 1, ctx, 0, cb);\n if (j == -1)\n goto err;\n if (j == 0)\n goto loop;\n j = BN_is_prime_fasttest_ex(t, 1, ctx, 0, cb);\n if (j == -1)\n goto err;\n if (j == 0)\n goto loop;\n if (!BN_GENCB_call(cb, 2, c1 - 1))\n goto err;\n }\n }\n found = 1;\n err:\n OPENSSL_free(mods);\n if (ctx != NULL)\n BN_CTX_end(ctx);\n BN_CTX_free(ctx);\n bn_check_top(ret);\n return found;\n}', '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_priv_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_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}', 'int BN_set_word(BIGNUM *a, BN_ULONG w)\n{\n bn_check_top(a);\n if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)\n return 0;\n a->neg = 0;\n a->d[0] = w;\n a->top = (w ? 1 : 0);\n bn_check_top(a);\n return 1;\n}', 'static ossl_inline BIGNUM *bn_expand(BIGNUM *a, int bits)\n{\n if (bits > (INT_MAX - BN_BITS2 + 1))\n return NULL;\n if (((bits+BN_BITS2-1)/BN_BITS2) <= (a)->dmax)\n return a;\n return bn_expand2((a),(bits+BN_BITS2-1)/BN_BITS2);\n}', 'BIGNUM *bn_expand2(BIGNUM *b, int words)\n{\n bn_check_top(b);\n if (words > b->dmax) {\n BN_ULONG *a = bn_expand_internal(b, words);\n if (!a)\n return NULL;\n if (b->d) {\n OPENSSL_cleanse(b->d, b->dmax * sizeof(b->d[0]));\n bn_free_d(b);\n }\n b->d = a;\n b->dmax = words;\n }\n bn_check_top(b);\n return b;\n}', 'static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)\n{\n BN_ULONG *a = NULL;\n bn_check_top(b);\n if (words > (INT_MAX / (4 * BN_BITS2))) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);\n return NULL;\n }\n if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);\n return NULL;\n }\n if (BN_get_flags(b, BN_FLG_SECURE))\n a = OPENSSL_secure_zalloc(words * sizeof(*a));\n else\n a = OPENSSL_zalloc(words * sizeof(*a));\n if (a == NULL) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);\n return NULL;\n }\n assert(b->top <= words);\n if (b->top > 0)\n memcpy(a, b->d, sizeof(*a) * b->top);\n return a;\n}', 'void *CRYPTO_zalloc(size_t num, const char *file, int line)\n{\n void *ret = CRYPTO_malloc(num, file, line);\n FAILTEST();\n if (ret != NULL)\n memset(ret, 0, num);\n return ret;\n}', 'void *CRYPTO_malloc(size_t num, const char *file, int line)\n{\n void *ret = NULL;\n 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}']
288
0
https://github.com/openssl/openssl/blob/3ba25ee86a3758cc659c954b59718d8397030768/crypto/lhash/lhash.c/#L363
static void contract(LHASH *lh) { LHASH_NODE **n,*n1,*np; np=lh->b[lh->p+lh->pmax-1]; lh->b[lh->p+lh->pmax-1]=NULL; if (lh->p == 0) { n=(LHASH_NODE **)OPENSSL_realloc(lh->b, (unsigned int)(sizeof(LHASH_NODE *)*lh->pmax)); if (n == NULL) { lh->error++; return; } lh->num_contract_reallocs++; lh->num_alloc_nodes/=2; lh->pmax/=2; lh->p=lh->pmax-1; lh->b=n; } else lh->p--; lh->num_nodes--; lh->num_contracts++; n1=lh->b[(int)lh->p]; if (n1 == NULL) lh->b[(int)lh->p]=np; else { while (n1->next != NULL) n1=n1->next; n1->next=np; } }
['static APP_INFO *pop_info(void)\n\t{\n\tAPP_INFO tmp;\n\tAPP_INFO *ret = NULL;\n\tif (amih != NULL)\n\t\t{\n\t\ttmp.thread=CRYPTO_thread_id();\n\t\tif ((ret=(APP_INFO *)lh_delete(amih,&tmp)) != NULL)\n\t\t\t{\n\t\t\tAPP_INFO *next=ret->next;\n\t\t\tif (next != NULL)\n\t\t\t\t{\n\t\t\t\tnext->references++;\n\t\t\t\tlh_insert(amih,(char *)next);\n\t\t\t\t}\n#ifdef LEVITTE_DEBUG_MEM\n\t\t\tif (ret->thread != tmp.thread)\n\t\t\t\t{\n\t\t\t\tfprintf(stderr, "pop_info(): deleted info has other thread ID (%lu) than the current thread (%lu)!!!!\\n",\n\t\t\t\t\tret->thread, tmp.thread);\n\t\t\t\tabort();\n\t\t\t\t}\n#endif\n\t\t\tif (--(ret->references) <= 0)\n\t\t\t\t{\n\t\t\t\tret->next = NULL;\n\t\t\t\tif (next != NULL)\n\t\t\t\t\tnext->references--;\n\t\t\t\tOPENSSL_free(ret);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\treturn(ret);\n\t}', 'void *lh_delete(LHASH *lh, const void *data)\n\t{\n\tunsigned long hash;\n\tLHASH_NODE *nn,**rn;\n\tconst void *ret;\n\tlh->error=0;\n\trn=getrn(lh,data,&hash);\n\tif (*rn == NULL)\n\t\t{\n\t\tlh->num_no_delete++;\n\t\treturn(NULL);\n\t\t}\n\telse\n\t\t{\n\t\tnn= *rn;\n\t\t*rn=nn->next;\n\t\tret=nn->data;\n\t\tOPENSSL_free(nn);\n\t\tlh->num_delete++;\n\t\t}\n\tlh->num_items--;\n\tif ((lh->num_nodes > MIN_NODES) &&\n\t\t(lh->down_load >= (lh->num_items*LH_LOAD_MULT/lh->num_nodes)))\n\t\tcontract(lh);\n\treturn((void *)ret);\n\t}', 'static void contract(LHASH *lh)\n\t{\n\tLHASH_NODE **n,*n1,*np;\n\tnp=lh->b[lh->p+lh->pmax-1];\n\tlh->b[lh->p+lh->pmax-1]=NULL;\n\tif (lh->p == 0)\n\t\t{\n\t\tn=(LHASH_NODE **)OPENSSL_realloc(lh->b,\n\t\t\t(unsigned int)(sizeof(LHASH_NODE *)*lh->pmax));\n\t\tif (n == NULL)\n\t\t\t{\n\t\t\tlh->error++;\n\t\t\treturn;\n\t\t\t}\n\t\tlh->num_contract_reallocs++;\n\t\tlh->num_alloc_nodes/=2;\n\t\tlh->pmax/=2;\n\t\tlh->p=lh->pmax-1;\n\t\tlh->b=n;\n\t\t}\n\telse\n\t\tlh->p--;\n\tlh->num_nodes--;\n\tlh->num_contracts++;\n\tn1=lh->b[(int)lh->p];\n\tif (n1 == NULL)\n\t\tlh->b[(int)lh->p]=np;\n\telse\n\t\t{\n\t\twhile (n1->next != NULL)\n\t\t\tn1=n1->next;\n\t\tn1->next=np;\n\t\t}\n\t}']
289
0
https://github.com/openssl/openssl/blob/8da94770f0a049497b1a52ee469cca1f4a13b1a7/crypto/bn/bn_mul.c/#L1072
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 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 -1;\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 (!d || !val[0])\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_mod_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m,\n BN_CTX *ctx)\n{\n BIGNUM *t;\n int ret = 0;\n bn_check_top(a);\n bn_check_top(b);\n bn_check_top(m);\n BN_CTX_start(ctx);\n if ((t = BN_CTX_get(ctx)) == NULL)\n goto err;\n if (a == b) {\n if (!BN_sqr(t, a, ctx))\n goto err;\n } else {\n if (!BN_mul(t, a, b, ctx))\n goto err;\n }\n if (!BN_nnmod(r, t, m, ctx))\n goto err;\n bn_check_top(r);\n ret = 1;\n err:\n BN_CTX_end(ctx);\n return (ret);\n}', 'int BN_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)\n{\n int ret = 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}']
290
0
https://github.com/openssl/openssl/blob/8da94770f0a049497b1a52ee469cca1f4a13b1a7/crypto/bn/bn_ctx.c/#L328
static unsigned int BN_STACK_pop(BN_STACK *st) { return st->indexes[--(st->depth)]; }
['int BN_is_prime_fasttest_ex(const BIGNUM *a, int checks, BN_CTX *ctx_passed,\n int do_trial_division, BN_GENCB *cb)\n{\n int i, j, ret = -1;\n int k;\n BN_CTX *ctx = NULL;\n BIGNUM *A1, *A1_odd, *check;\n BN_MONT_CTX *mont = NULL;\n const BIGNUM *A = NULL;\n if (BN_cmp(a, BN_value_one()) <= 0)\n return 0;\n if (checks == BN_prime_checks)\n checks = BN_prime_checks_for_size(BN_num_bits(a));\n if (!BN_is_odd(a))\n return BN_is_word(a, 2);\n if (do_trial_division) {\n for (i = 1; i < NUMPRIMES; i++)\n if (BN_mod_word(a, primes[i]) == 0)\n return 0;\n if (!BN_GENCB_call(cb, 1, -1))\n goto err;\n }\n if (ctx_passed != NULL)\n ctx = ctx_passed;\n else if ((ctx = BN_CTX_new()) == NULL)\n goto err;\n BN_CTX_start(ctx);\n if (a->neg) {\n BIGNUM *t;\n if ((t = BN_CTX_get(ctx)) == NULL)\n goto err;\n BN_copy(t, a);\n t->neg = 0;\n A = t;\n } else\n A = a;\n A1 = BN_CTX_get(ctx);\n A1_odd = BN_CTX_get(ctx);\n check = BN_CTX_get(ctx);\n if (check == NULL)\n goto err;\n if (!BN_copy(A1, A))\n goto err;\n if (!BN_sub_word(A1, 1))\n goto err;\n if (BN_is_zero(A1)) {\n ret = 0;\n goto err;\n }\n k = 1;\n while (!BN_is_bit_set(A1, k))\n k++;\n if (!BN_rshift(A1_odd, A1, k))\n goto err;\n mont = BN_MONT_CTX_new();\n if (mont == NULL)\n goto err;\n if (!BN_MONT_CTX_set(mont, A, ctx))\n goto err;\n for (i = 0; i < checks; i++) {\n if (!BN_pseudo_rand_range(check, A1))\n goto err;\n if (!BN_add_word(check, 1))\n goto err;\n j = witness(check, A, A1, A1_odd, k, ctx, mont);\n if (j == -1)\n goto err;\n if (j) {\n ret = 0;\n goto err;\n }\n if (!BN_GENCB_call(cb, 1, i))\n goto err;\n }\n ret = 1;\n err:\n if (ctx != NULL) {\n BN_CTX_end(ctx);\n if (ctx_passed == NULL)\n BN_CTX_free(ctx);\n }\n BN_MONT_CTX_free(mont);\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}']
291
0
https://github.com/openssl/openssl/blob/85bcf27cccd8f5f569886479ad96a0c33444404c/crypto/bn/bn_lib.c/#L171
int BN_num_bits_word(BN_ULONG l) { static const unsigned char bits[256]={ 0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4, 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5, 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6, 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6, 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, }; #if defined(SIXTY_FOUR_BIT_LONG) if (l & 0xffffffff00000000L) { if (l & 0xffff000000000000L) { if (l & 0xff00000000000000L) { return(bits[(int)(l>>56)]+56); } else return(bits[(int)(l>>48)]+48); } else { if (l & 0x0000ff0000000000L) { return(bits[(int)(l>>40)]+40); } else return(bits[(int)(l>>32)]+32); } } else #else #ifdef SIXTY_FOUR_BIT if (l & 0xffffffff00000000LL) { if (l & 0xffff000000000000LL) { if (l & 0xff00000000000000LL) { return(bits[(int)(l>>56)]+56); } else return(bits[(int)(l>>48)]+48); } else { if (l & 0x0000ff0000000000LL) { return(bits[(int)(l>>40)]+40); } else return(bits[(int)(l>>32)]+32); } } else #endif #endif { #if defined(THIRTY_TWO_BIT) || defined(SIXTY_FOUR_BIT) || defined(SIXTY_FOUR_BIT_LONG) if (l & 0xffff0000L) { if (l & 0xff000000L) return(bits[(int)(l>>24L)]+24); else return(bits[(int)(l>>16L)]+16); } else #endif { #if defined(THIRTY_TWO_BIT) || defined(SIXTY_FOUR_BIT) || defined(SIXTY_FOUR_BIT_LONG) if (l & 0xff00L) return(bits[(int)(l>>8)]+8); else #endif return(bits[(int)(l )] ); } } }
["char *BN_bn2dec(const BIGNUM *a)\n\t{\n\tint i=0,num, ok = 0;\n\tchar *buf=NULL;\n\tchar *p;\n\tBIGNUM *t=NULL;\n\tBN_ULONG *bn_data=NULL,*lp;\n\ti=BN_num_bits(a)*3;\n\tnum=(i/10+i/1000+1)+1;\n\tbn_data=(BN_ULONG *)OPENSSL_malloc((num/BN_DEC_NUM+1)*sizeof(BN_ULONG));\n\tbuf=(char *)OPENSSL_malloc(num+3);\n\tif ((buf == NULL) || (bn_data == NULL))\n\t\t{\n\t\tBNerr(BN_F_BN_BN2DEC,ERR_R_MALLOC_FAILURE);\n\t\tgoto err;\n\t\t}\n\tif ((t=BN_dup(a)) == NULL) goto err;\n#define BUF_REMAIN (num+3 - (size_t)(p - buf))\n\tp=buf;\n\tlp=bn_data;\n\tif (BN_is_zero(t))\n\t\t{\n\t\t*(p++)='0';\n\t\t*(p++)='\\0';\n\t\t}\n\telse\n\t\t{\n\t\tif (BN_is_negative(t))\n\t\t\t*p++ = '-';\n\t\ti=0;\n\t\twhile (!BN_is_zero(t))\n\t\t\t{\n\t\t\t*lp=BN_div_word(t,BN_DEC_CONV);\n\t\t\tlp++;\n\t\t\t}\n\t\tlp--;\n\t\tBIO_snprintf(p,BUF_REMAIN,BN_DEC_FMT1,*lp);\n\t\twhile (*p) p++;\n\t\twhile (lp != bn_data)\n\t\t\t{\n\t\t\tlp--;\n\t\t\tBIO_snprintf(p,BUF_REMAIN,BN_DEC_FMT2,*lp);\n\t\t\twhile (*p) p++;\n\t\t\t}\n\t\t}\n\tok = 1;\nerr:\n\tif (bn_data != NULL) OPENSSL_free(bn_data);\n\tif (t != NULL) BN_free(t);\n\tif (!ok && buf)\n\t\t{\n\t\tOPENSSL_free(buf);\n\t\tbuf = NULL;\n\t\t}\n\treturn(buf);\n\t}", 'BN_ULONG BN_div_word(BIGNUM *a, BN_ULONG w)\n\t{\n\tBN_ULONG ret = 0;\n\tint i, j;\n\tbn_check_top(a);\n\tw &= BN_MASK2;\n\tif (!w)\n\t\treturn (BN_ULONG)-1;\n\tif (a->top == 0)\n\t\treturn 0;\n\tj = BN_BITS2 - BN_num_bits_word(w);\n\tw <<= j;\n\tif (!BN_lshift(a, a, j))\n\t\treturn (BN_ULONG)-1;\n\tfor (i=a->top-1; i>=0; i--)\n\t\t{\n\t\tBN_ULONG l,d;\n\t\tl=a->d[i];\n\t\td=bn_div_words(ret,l,w);\n\t\tret=(l-((d*w)&BN_MASK2))&BN_MASK2;\n\t\ta->d[i]=d;\n\t\t}\n\tif ((a->top > 0) && (a->d[a->top-1] == 0))\n\t\ta->top--;\n\tret >>= j;\n\tbn_check_top(a);\n\treturn(ret);\n\t}', 'int BN_num_bits_word(BN_ULONG l)\n\t{\n\tstatic const unsigned char bits[256]={\n\t\t0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,\n\t\t5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,\n\t\t6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,\n\t\t6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,\n\t\t7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,\n\t\t7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,\n\t\t7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,\n\t\t7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,\n\t\t8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,\n\t\t8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,\n\t\t8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,\n\t\t8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,\n\t\t8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,\n\t\t8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,\n\t\t8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,\n\t\t8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,\n\t\t};\n#if defined(SIXTY_FOUR_BIT_LONG)\n\tif (l & 0xffffffff00000000L)\n\t\t{\n\t\tif (l & 0xffff000000000000L)\n\t\t\t{\n\t\t\tif (l & 0xff00000000000000L)\n\t\t\t\t{\n\t\t\t\treturn(bits[(int)(l>>56)]+56);\n\t\t\t\t}\n\t\t\telse\treturn(bits[(int)(l>>48)]+48);\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tif (l & 0x0000ff0000000000L)\n\t\t\t\t{\n\t\t\t\treturn(bits[(int)(l>>40)]+40);\n\t\t\t\t}\n\t\t\telse\treturn(bits[(int)(l>>32)]+32);\n\t\t\t}\n\t\t}\n\telse\n#else\n#ifdef SIXTY_FOUR_BIT\n\tif (l & 0xffffffff00000000LL)\n\t\t{\n\t\tif (l & 0xffff000000000000LL)\n\t\t\t{\n\t\t\tif (l & 0xff00000000000000LL)\n\t\t\t\t{\n\t\t\t\treturn(bits[(int)(l>>56)]+56);\n\t\t\t\t}\n\t\t\telse\treturn(bits[(int)(l>>48)]+48);\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\tif (l & 0x0000ff0000000000LL)\n\t\t\t\t{\n\t\t\t\treturn(bits[(int)(l>>40)]+40);\n\t\t\t\t}\n\t\t\telse\treturn(bits[(int)(l>>32)]+32);\n\t\t\t}\n\t\t}\n\telse\n#endif\n#endif\n\t\t{\n#if defined(THIRTY_TWO_BIT) || defined(SIXTY_FOUR_BIT) || defined(SIXTY_FOUR_BIT_LONG)\n\t\tif (l & 0xffff0000L)\n\t\t\t{\n\t\t\tif (l & 0xff000000L)\n\t\t\t\treturn(bits[(int)(l>>24L)]+24);\n\t\t\telse\treturn(bits[(int)(l>>16L)]+16);\n\t\t\t}\n\t\telse\n#endif\n\t\t\t{\n#if defined(THIRTY_TWO_BIT) || defined(SIXTY_FOUR_BIT) || defined(SIXTY_FOUR_BIT_LONG)\n\t\t\tif (l & 0xff00L)\n\t\t\t\treturn(bits[(int)(l>>8)]+8);\n\t\t\telse\n#endif\n\t\t\t\treturn(bits[(int)(l )] );\n\t\t\t}\n\t\t}\n\t}']
292
0
https://github.com/openssl/openssl/blob/1a5a1a93f6c48b135a2b384f7e571abb7b90fc55/crypto/bn/bntest.c/#L1318
int test_gf2m_mod_inv(BIO *bp,BN_CTX *ctx) { BIGNUM *a,*b[2],*c,*d; int i, j, ret = 0; unsigned int p0[] = {163,7,6,3,0}; unsigned int p1[] = {193,15,0}; a=BN_new(); b[0]=BN_new(); b[1]=BN_new(); c=BN_new(); d=BN_new(); BN_GF2m_arr2poly(p0, b[0]); BN_GF2m_arr2poly(p1, b[1]); for (i=0; i<num0; i++) { BN_bntest_rand(a, 512, 0, 0); for (j=0; j < 2; j++) { BN_GF2m_mod_inv(c, a, b[j], ctx); BN_GF2m_mod_mul(d, a, c, b[j], ctx); #if 0 if (bp != NULL) { if (!results) { BN_print(bp,a); BIO_puts(bp, " * "); BN_print(bp,c); BIO_puts(bp," - 1 % "); BN_print(bp,b[j]); BIO_puts(bp,"\n"); } } #endif if(!BN_is_one(d)) { fprintf(stderr,"GF(2^m) modular inversion test failed!\n"); goto err; } } } ret = 1; err: BN_free(a); BN_free(b[0]); BN_free(b[1]); BN_free(c); BN_free(d); return ret; }
['int test_gf2m_mod_inv(BIO *bp,BN_CTX *ctx)\n\t{\n\tBIGNUM *a,*b[2],*c,*d;\n\tint i, j, ret = 0;\n\tunsigned int p0[] = {163,7,6,3,0};\n\tunsigned int p1[] = {193,15,0};\n\ta=BN_new();\n\tb[0]=BN_new();\n\tb[1]=BN_new();\n\tc=BN_new();\n\td=BN_new();\n\tBN_GF2m_arr2poly(p0, b[0]);\n\tBN_GF2m_arr2poly(p1, b[1]);\n\tfor (i=0; i<num0; i++)\n\t\t{\n\t\tBN_bntest_rand(a, 512, 0, 0);\n\t\tfor (j=0; j < 2; j++)\n\t\t\t{\n\t\t\tBN_GF2m_mod_inv(c, a, b[j], ctx);\n\t\t\tBN_GF2m_mod_mul(d, a, c, b[j], ctx);\n#if 0\n\t\t\tif (bp != NULL)\n\t\t\t\t{\n\t\t\t\tif (!results)\n\t\t\t\t\t{\n\t\t\t\t\tBN_print(bp,a);\n\t\t\t\t\tBIO_puts(bp, " * ");\n\t\t\t\t\tBN_print(bp,c);\n\t\t\t\t\tBIO_puts(bp," - 1 % ");\n\t\t\t\t\tBN_print(bp,b[j]);\n\t\t\t\t\tBIO_puts(bp,"\\n");\n\t\t\t\t\t}\n\t\t\t\t}\n#endif\n\t\t\tif(!BN_is_one(d))\n\t\t\t\t{\n\t\t\t\tfprintf(stderr,"GF(2^m) modular inversion test failed!\\n");\n\t\t\t\tgoto err;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\tret = 1;\n err:\n\tBN_free(a);\n\tBN_free(b[0]);\n\tBN_free(b[1]);\n\tBN_free(c);\n\tBN_free(d);\n\treturn ret;\n\t}', 'BIGNUM *BN_new(void)\n\t{\n\tBIGNUM *ret;\n\tif ((ret=(BIGNUM *)OPENSSL_malloc(sizeof(BIGNUM))) == NULL)\n\t\t{\n\t\tBNerr(BN_F_BN_NEW,ERR_R_MALLOC_FAILURE);\n\t\treturn(NULL);\n\t\t}\n\tret->flags=BN_FLG_MALLOCED;\n\tret->top=0;\n\tret->neg=0;\n\tret->dmax=0;\n\tret->d=NULL;\n\tbn_check_top(ret);\n\treturn(ret);\n\t}', 'void *CRYPTO_malloc(int num, const char *file, int line)\n\t{\n\tvoid *ret = NULL;\n\textern unsigned char cleanse_ctr;\n\tif (num <= 0) return NULL;\n\tallow_customize = 0;\n\tif (malloc_debug_func != NULL)\n\t\t{\n\t\tallow_customize_debug = 0;\n\t\tmalloc_debug_func(NULL, num, file, line, 0);\n\t\t}\n\tret = malloc_ex_func(num,file,line);\n#ifdef LEVITTE_DEBUG_MEM\n\tfprintf(stderr, "LEVITTE_DEBUG_MEM: > 0x%p (%d)\\n", ret, num);\n#endif\n\tif (malloc_debug_func != NULL)\n\t\tmalloc_debug_func(ret, num, file, line, 1);\n if(ret && (num > 2048))\n ((unsigned char *)ret)[0] = cleanse_ctr;\n\treturn ret;\n\t}', 'void ERR_put_error(int lib, int func, int reason, const char *file,\n\t int line)\n\t{\n\tERR_STATE *es;\n#ifdef _OSD_POSIX\n\tif (strncmp(file,"*POSIX(", sizeof("*POSIX(")-1) == 0) {\n\t\tchar *end;\n\t\tfile += sizeof("*POSIX(")-1;\n\t\tend = &file[strlen(file)-1];\n\t\tif (*end == \')\')\n\t\t\t*end = \'\\0\';\n\t\tif ((end = strrchr(file, \'/\')) != NULL)\n\t\t\tfile = &end[1];\n\t}\n#endif\n\tes=ERR_get_state();\n\tes->top=(es->top+1)%ERR_NUM_ERRORS;\n\tif (es->top == es->bottom)\n\t\tes->bottom=(es->bottom+1)%ERR_NUM_ERRORS;\n\tes->err_flags[es->top]=0;\n\tes->err_buffer[es->top]=ERR_PACK(lib,func,reason);\n\tes->err_file[es->top]=file;\n\tes->err_line[es->top]=line;\n\terr_clear_data(es,es->top);\n\t}', 'int BN_GF2m_arr2poly(const unsigned int p[], BIGNUM *a)\n\t{\n\tint i;\n\tbn_check_top(a);\n\tBN_zero(a);\n\tfor (i = 0; p[i] != 0; i++)\n\t\t{\n\t\tif (BN_set_bit(a, p[i]) == 0)\n\t\t\treturn 0;\n\t\t}\n\tBN_set_bit(a, 0);\n\tbn_check_top(a);\n\treturn 1;\n\t}', 'int BN_set_word(BIGNUM *a, BN_ULONG w)\n\t{\n\tbn_check_top(a);\n\tif (bn_expand(a,(int)sizeof(BN_ULONG)*8) == NULL) return(0);\n\ta->neg = 0;\n\ta->d[0] = w;\n\ta->top = (w ? 1 : 0);\n\tbn_check_top(a);\n\treturn(1);\n\t}']
293
0
https://github.com/openssl/openssl/blob/f9df0a7775f483c175cda5832360cccd1db6943a/crypto/bn/bn_ctx.c/#L273
static unsigned int BN_STACK_pop(BN_STACK *st) { return st->indexes[--(st->depth)]; }
['static int rsa_ossl_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)\n{\n BIGNUM *r1, *m1, *vrfy;\n int ret = 0;\n BN_CTX_start(ctx);\n r1 = BN_CTX_get(ctx);\n m1 = BN_CTX_get(ctx);\n vrfy = BN_CTX_get(ctx);\n if (vrfy == NULL)\n goto err;\n {\n BIGNUM *p = BN_new(), *q = BN_new();\n if (p == NULL || q == NULL) {\n BN_free(p);\n BN_free(q);\n goto err;\n }\n BN_with_flags(p, rsa->p, BN_FLG_CONSTTIME);\n BN_with_flags(q, rsa->q, BN_FLG_CONSTTIME);\n if (rsa->flags & RSA_FLAG_CACHE_PRIVATE) {\n if (!BN_MONT_CTX_set_locked\n (&rsa->_method_mod_p, rsa->lock, p, ctx)\n || !BN_MONT_CTX_set_locked(&rsa->_method_mod_q,\n rsa->lock, q, ctx)) {\n BN_free(p);\n BN_free(q);\n goto err;\n }\n }\n BN_free(p);\n BN_free(q);\n }\n if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)\n if (!BN_MONT_CTX_set_locked\n (&rsa->_method_mod_n, rsa->lock, rsa->n, ctx))\n goto err;\n {\n BIGNUM *c = BN_new();\n if (c == NULL)\n goto err;\n BN_with_flags(c, I, BN_FLG_CONSTTIME);\n if (!BN_mod(r1, c, rsa->q, ctx)) {\n BN_free(c);\n goto err;\n }\n {\n BIGNUM *dmq1 = BN_new();\n if (dmq1 == NULL) {\n BN_free(c);\n goto err;\n }\n BN_with_flags(dmq1, rsa->dmq1, BN_FLG_CONSTTIME);\n if (!rsa->meth->bn_mod_exp(m1, r1, dmq1, rsa->q, ctx,\n rsa->_method_mod_q)) {\n BN_free(c);\n BN_free(dmq1);\n goto err;\n }\n BN_free(dmq1);\n }\n if (!BN_mod(r1, c, rsa->p, ctx)) {\n BN_free(c);\n goto err;\n }\n BN_free(c);\n }\n {\n BIGNUM *dmp1 = BN_new();\n if (dmp1 == NULL)\n goto err;\n BN_with_flags(dmp1, rsa->dmp1, BN_FLG_CONSTTIME);\n if (!rsa->meth->bn_mod_exp(r0, r1, dmp1, rsa->p, ctx,\n rsa->_method_mod_p)) {\n BN_free(dmp1);\n goto err;\n }\n BN_free(dmp1);\n }\n if (!BN_sub(r0, r0, m1))\n goto err;\n if (BN_is_negative(r0))\n if (!BN_add(r0, r0, rsa->p))\n goto err;\n if (!BN_mul(r1, r0, rsa->iqmp, ctx))\n goto err;\n {\n BIGNUM *pr1 = BN_new();\n if (pr1 == NULL)\n goto err;\n BN_with_flags(pr1, r1, BN_FLG_CONSTTIME);\n if (!BN_mod(r0, pr1, rsa->p, ctx)) {\n BN_free(pr1);\n goto err;\n }\n BN_free(pr1);\n }\n if (BN_is_negative(r0))\n if (!BN_add(r0, r0, rsa->p))\n goto err;\n if (!BN_mul(r1, r0, rsa->q, ctx))\n goto err;\n if (!BN_add(r0, r1, m1))\n goto err;\n if (rsa->e && rsa->n) {\n if (!rsa->meth->bn_mod_exp(vrfy, r0, rsa->e, rsa->n, ctx,\n rsa->_method_mod_n))\n goto err;\n if (!BN_sub(vrfy, vrfy, I))\n goto err;\n if (!BN_mod(vrfy, vrfy, rsa->n, ctx))\n goto err;\n if (BN_is_negative(vrfy))\n if (!BN_add(vrfy, vrfy, rsa->n))\n goto err;\n if (!BN_is_zero(vrfy)) {\n BIGNUM *d = BN_new();\n if (d == NULL)\n goto err;\n BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);\n if (!rsa->meth->bn_mod_exp(r0, I, d, rsa->n, ctx,\n rsa->_method_mod_n)) {\n BN_free(d);\n goto err;\n }\n BN_free(d);\n }\n }\n ret = 1;\n err:\n BN_CTX_end(ctx);\n return ret;\n}', 'void BN_CTX_start(BN_CTX *ctx)\n{\n CTXDBG_ENTRY("BN_CTX_start", ctx);\n if (ctx->err_stack || ctx->too_many)\n ctx->err_stack++;\n else if (!BN_STACK_push(&ctx->stack, ctx->used)) {\n BNerr(BN_F_BN_CTX_START, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n ctx->err_stack++;\n }\n CTXDBG_EXIT(ctx);\n}', 'BN_MONT_CTX *BN_MONT_CTX_set_locked(BN_MONT_CTX **pmont, CRYPTO_RWLOCK *lock,\n const BIGNUM *mod, BN_CTX *ctx)\n{\n BN_MONT_CTX *ret;\n CRYPTO_THREAD_read_lock(lock);\n ret = *pmont;\n CRYPTO_THREAD_unlock(lock);\n if (ret)\n return ret;\n ret = BN_MONT_CTX_new();\n if (ret == NULL)\n return NULL;\n if (!BN_MONT_CTX_set(ret, mod, ctx)) {\n BN_MONT_CTX_free(ret);\n return NULL;\n }\n CRYPTO_THREAD_write_lock(lock);\n if (*pmont) {\n BN_MONT_CTX_free(ret);\n ret = *pmont;\n } else\n *pmont = ret;\n CRYPTO_THREAD_unlock(lock);\n return ret;\n}', 'int BN_MONT_CTX_set(BN_MONT_CTX *mont, const BIGNUM *mod, BN_CTX *ctx)\n{\n int ret = 0;\n BIGNUM *Ri, *R;\n if (BN_is_zero(mod))\n return 0;\n BN_CTX_start(ctx);\n if ((Ri = BN_CTX_get(ctx)) == NULL)\n goto err;\n R = &(mont->RR);\n if (!BN_copy(&(mont->N), mod))\n goto err;\n mont->N.neg = 0;\n#ifdef MONT_WORD\n {\n BIGNUM tmod;\n BN_ULONG buf[2];\n bn_init(&tmod);\n tmod.d = buf;\n tmod.dmax = 2;\n tmod.neg = 0;\n if (BN_get_flags(mod, BN_FLG_CONSTTIME) != 0)\n BN_set_flags(&tmod, BN_FLG_CONSTTIME);\n mont->ri = (BN_num_bits(mod) + (BN_BITS2 - 1)) / BN_BITS2 * BN_BITS2;\n# if defined(OPENSSL_BN_ASM_MONT) && (BN_BITS2<=32)\n BN_zero(R);\n if (!(BN_set_bit(R, 2 * BN_BITS2)))\n goto err;\n tmod.top = 0;\n if ((buf[0] = mod->d[0]))\n tmod.top = 1;\n if ((buf[1] = mod->top > 1 ? mod->d[1] : 0))\n tmod.top = 2;\n if ((BN_mod_inverse(Ri, R, &tmod, ctx)) == NULL)\n goto err;\n if (!BN_lshift(Ri, Ri, 2 * BN_BITS2))\n goto err;\n if (!BN_is_zero(Ri)) {\n if (!BN_sub_word(Ri, 1))\n goto err;\n } else {\n if (bn_expand(Ri, (int)sizeof(BN_ULONG) * 2) == NULL)\n goto err;\n Ri->neg = 0;\n Ri->d[0] = BN_MASK2;\n Ri->d[1] = BN_MASK2;\n Ri->top = 2;\n }\n if (!BN_div(Ri, NULL, Ri, &tmod, ctx))\n goto err;\n mont->n0[0] = (Ri->top > 0) ? Ri->d[0] : 0;\n mont->n0[1] = (Ri->top > 1) ? Ri->d[1] : 0;\n# else\n BN_zero(R);\n if (!(BN_set_bit(R, BN_BITS2)))\n goto err;\n buf[0] = mod->d[0];\n buf[1] = 0;\n tmod.top = buf[0] != 0 ? 1 : 0;\n if ((BN_mod_inverse(Ri, R, &tmod, ctx)) == NULL)\n goto err;\n if (!BN_lshift(Ri, Ri, BN_BITS2))\n goto err;\n if (!BN_is_zero(Ri)) {\n if (!BN_sub_word(Ri, 1))\n goto err;\n } else {\n if (!BN_set_word(Ri, BN_MASK2))\n goto err;\n }\n if (!BN_div(Ri, NULL, Ri, &tmod, ctx))\n goto err;\n mont->n0[0] = (Ri->top > 0) ? Ri->d[0] : 0;\n mont->n0[1] = 0;\n# endif\n }\n#else\n {\n mont->ri = BN_num_bits(&mont->N);\n BN_zero(R);\n if (!BN_set_bit(R, mont->ri))\n goto err;\n if ((BN_mod_inverse(Ri, R, &mont->N, ctx)) == NULL)\n goto err;\n if (!BN_lshift(Ri, Ri, mont->ri))\n goto err;\n if (!BN_sub_word(Ri, 1))\n goto err;\n if (!BN_div(&(mont->Ni), NULL, Ri, &mont->N, ctx))\n goto err;\n }\n#endif\n BN_zero(&(mont->RR));\n if (!BN_set_bit(&(mont->RR), mont->ri * 2))\n goto err;\n if (!BN_mod(&(mont->RR), &(mont->RR), &(mont->N), ctx))\n goto err;\n ret = 1;\n err:\n BN_CTX_end(ctx);\n return ret;\n}', 'BIGNUM *BN_mod_inverse(BIGNUM *in,\n const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx)\n{\n BIGNUM *rv;\n int noinv;\n rv = int_bn_mod_inverse(in, a, n, ctx, &noinv);\n if (noinv)\n BNerr(BN_F_BN_MOD_INVERSE, BN_R_NO_INVERSE);\n return rv;\n}', 'BIGNUM *int_bn_mod_inverse(BIGNUM *in,\n const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx,\n int *pnoinv)\n{\n BIGNUM *A, *B, *X, *Y, *M, *D, *T, *R = NULL;\n BIGNUM *ret = NULL;\n int sign;\n if (pnoinv)\n *pnoinv = 0;\n if ((BN_get_flags(a, BN_FLG_CONSTTIME) != 0)\n || (BN_get_flags(n, BN_FLG_CONSTTIME) != 0)) {\n return BN_mod_inverse_no_branch(in, a, n, ctx);\n }\n bn_check_top(a);\n bn_check_top(n);\n BN_CTX_start(ctx);\n A = BN_CTX_get(ctx);\n B = BN_CTX_get(ctx);\n X = BN_CTX_get(ctx);\n D = BN_CTX_get(ctx);\n M = BN_CTX_get(ctx);\n Y = BN_CTX_get(ctx);\n T = BN_CTX_get(ctx);\n if (T == NULL)\n goto err;\n if (in == NULL)\n R = BN_new();\n else\n R = in;\n if (R == NULL)\n goto err;\n BN_one(X);\n BN_zero(Y);\n if (BN_copy(B, a) == NULL)\n goto err;\n if (BN_copy(A, n) == NULL)\n goto err;\n A->neg = 0;\n if (B->neg || (BN_ucmp(B, A) >= 0)) {\n if (!BN_nnmod(B, B, A, ctx))\n goto err;\n }\n sign = -1;\n if (BN_is_odd(n) && (BN_num_bits(n) <= 2048)) {\n int shift;\n while (!BN_is_zero(B)) {\n shift = 0;\n while (!BN_is_bit_set(B, shift)) {\n shift++;\n if (BN_is_odd(X)) {\n if (!BN_uadd(X, X, n))\n goto err;\n }\n if (!BN_rshift1(X, X))\n goto err;\n }\n if (shift > 0) {\n if (!BN_rshift(B, B, shift))\n goto err;\n }\n shift = 0;\n while (!BN_is_bit_set(A, shift)) {\n shift++;\n if (BN_is_odd(Y)) {\n if (!BN_uadd(Y, Y, n))\n goto err;\n }\n if (!BN_rshift1(Y, Y))\n goto err;\n }\n if (shift > 0) {\n if (!BN_rshift(A, A, shift))\n goto err;\n }\n if (BN_ucmp(B, A) >= 0) {\n if (!BN_uadd(X, X, Y))\n goto err;\n if (!BN_usub(B, B, A))\n goto err;\n } else {\n if (!BN_uadd(Y, Y, X))\n goto err;\n if (!BN_usub(A, A, B))\n goto err;\n }\n }\n } else {\n while (!BN_is_zero(B)) {\n BIGNUM *tmp;\n if (BN_num_bits(A) == BN_num_bits(B)) {\n if (!BN_one(D))\n goto err;\n if (!BN_sub(M, A, B))\n goto err;\n } else if (BN_num_bits(A) == BN_num_bits(B) + 1) {\n if (!BN_lshift1(T, B))\n goto err;\n if (BN_ucmp(A, T) < 0) {\n if (!BN_one(D))\n goto err;\n if (!BN_sub(M, A, B))\n goto err;\n } else {\n if (!BN_sub(M, A, T))\n goto err;\n if (!BN_add(D, T, B))\n goto err;\n if (BN_ucmp(A, D) < 0) {\n if (!BN_set_word(D, 2))\n goto err;\n } else {\n if (!BN_set_word(D, 3))\n goto err;\n if (!BN_sub(M, M, B))\n goto err;\n }\n }\n } else {\n if (!BN_div(D, M, A, B, ctx))\n goto err;\n }\n tmp = A;\n A = B;\n B = M;\n if (BN_is_one(D)) {\n if (!BN_add(tmp, X, Y))\n goto err;\n } else {\n if (BN_is_word(D, 2)) {\n if (!BN_lshift1(tmp, X))\n goto err;\n } else if (BN_is_word(D, 4)) {\n if (!BN_lshift(tmp, X, 2))\n goto err;\n } else if (D->top == 1) {\n if (!BN_copy(tmp, X))\n goto err;\n if (!BN_mul_word(tmp, D->d[0]))\n goto err;\n } else {\n if (!BN_mul(tmp, D, X, ctx))\n goto err;\n }\n if (!BN_add(tmp, tmp, Y))\n goto err;\n }\n M = Y;\n Y = X;\n X = tmp;\n sign = -sign;\n }\n }\n if (sign < 0) {\n if (!BN_sub(Y, n, Y))\n goto err;\n }\n if (BN_is_one(A)) {\n if (!Y->neg && BN_ucmp(Y, n) < 0) {\n if (!BN_copy(R, Y))\n goto err;\n } else {\n if (!BN_nnmod(R, Y, n, ctx))\n goto err;\n }\n } else {\n if (pnoinv)\n *pnoinv = 1;\n goto err;\n }\n ret = R;\n err:\n if ((ret == NULL) && (in == NULL))\n BN_free(R);\n BN_CTX_end(ctx);\n bn_check_top(ret);\n return (ret);\n}', 'static BIGNUM *BN_mod_inverse_no_branch(BIGNUM *in,\n const BIGNUM *a, const BIGNUM *n,\n BN_CTX *ctx)\n{\n BIGNUM *A, *B, *X, *Y, *M, *D, *T, *R = NULL;\n BIGNUM *ret = NULL;\n int sign;\n bn_check_top(a);\n bn_check_top(n);\n BN_CTX_start(ctx);\n A = BN_CTX_get(ctx);\n B = BN_CTX_get(ctx);\n X = BN_CTX_get(ctx);\n D = BN_CTX_get(ctx);\n M = BN_CTX_get(ctx);\n Y = BN_CTX_get(ctx);\n T = BN_CTX_get(ctx);\n if (T == NULL)\n goto err;\n if (in == NULL)\n R = BN_new();\n else\n R = in;\n if (R == NULL)\n goto err;\n BN_one(X);\n BN_zero(Y);\n if (BN_copy(B, a) == NULL)\n goto err;\n if (BN_copy(A, n) == NULL)\n goto err;\n A->neg = 0;\n if (B->neg || (BN_ucmp(B, A) >= 0)) {\n {\n BIGNUM local_B;\n bn_init(&local_B);\n BN_with_flags(&local_B, B, BN_FLG_CONSTTIME);\n if (!BN_nnmod(B, &local_B, A, ctx))\n goto err;\n }\n }\n sign = -1;\n while (!BN_is_zero(B)) {\n BIGNUM *tmp;\n {\n BIGNUM local_A;\n bn_init(&local_A);\n BN_with_flags(&local_A, A, BN_FLG_CONSTTIME);\n if (!BN_div(D, M, &local_A, B, ctx))\n goto err;\n }\n tmp = A;\n A = B;\n B = M;\n if (!BN_mul(tmp, D, X, ctx))\n goto err;\n if (!BN_add(tmp, tmp, Y))\n goto err;\n M = Y;\n Y = X;\n X = tmp;\n sign = -sign;\n }\n if (sign < 0) {\n if (!BN_sub(Y, n, Y))\n goto err;\n }\n if (BN_is_one(A)) {\n if (!Y->neg && BN_ucmp(Y, n) < 0) {\n if (!BN_copy(R, Y))\n goto err;\n } else {\n if (!BN_nnmod(R, Y, n, ctx))\n goto err;\n }\n } else {\n BNerr(BN_F_BN_MOD_INVERSE_NO_BRANCH, BN_R_NO_INVERSE);\n goto err;\n }\n ret = R;\n err:\n if ((ret == NULL) && (in == NULL))\n BN_free(R);\n BN_CTX_end(ctx);\n bn_check_top(ret);\n return (ret);\n}', 'int BN_nnmod(BIGNUM *r, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx)\n{\n if (!(BN_mod(r, m, d, ctx)))\n return 0;\n if (!r->neg)\n return 1;\n return (d->neg ? BN_sub : BN_add) (r, r, d);\n}', 'int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,\n BN_CTX *ctx)\n{\n int norm_shift, i, loop;\n BIGNUM *tmp, wnum, *snum, *sdiv, *res;\n BN_ULONG *resp, *wnump;\n BN_ULONG d0, d1;\n int num_n, div_n;\n int no_branch = 0;\n if ((num->top > 0 && num->d[num->top - 1] == 0) ||\n (divisor->top > 0 && divisor->d[divisor->top - 1] == 0)) {\n BNerr(BN_F_BN_DIV, BN_R_NOT_INITIALIZED);\n return 0;\n }\n bn_check_top(num);\n bn_check_top(divisor);\n if ((BN_get_flags(num, BN_FLG_CONSTTIME) != 0)\n || (BN_get_flags(divisor, BN_FLG_CONSTTIME) != 0)) {\n no_branch = 1;\n }\n bn_check_top(dv);\n bn_check_top(rm);\n if (BN_is_zero(divisor)) {\n BNerr(BN_F_BN_DIV, BN_R_DIV_BY_ZERO);\n return (0);\n }\n if (!no_branch && BN_ucmp(num, divisor) < 0) {\n if (rm != NULL) {\n if (BN_copy(rm, num) == NULL)\n return (0);\n }\n if (dv != NULL)\n BN_zero(dv);\n return 1;\n }\n BN_CTX_start(ctx);\n res = (dv == NULL) ? BN_CTX_get(ctx) : dv;\n tmp = BN_CTX_get(ctx);\n snum = BN_CTX_get(ctx);\n sdiv = BN_CTX_get(ctx);\n if (sdiv == NULL)\n goto err;\n norm_shift = BN_BITS2 - ((BN_num_bits(divisor)) % BN_BITS2);\n if (!(BN_lshift(sdiv, divisor, norm_shift)))\n goto err;\n sdiv->neg = 0;\n norm_shift += BN_BITS2;\n if (!(BN_lshift(snum, num, norm_shift)))\n goto err;\n snum->neg = 0;\n if (no_branch) {\n if (snum->top <= sdiv->top + 1) {\n if (bn_wexpand(snum, sdiv->top + 2) == NULL)\n goto err;\n for (i = snum->top; i < sdiv->top + 2; i++)\n snum->d[i] = 0;\n snum->top = sdiv->top + 2;\n } else {\n if (bn_wexpand(snum, snum->top + 1) == NULL)\n goto err;\n snum->d[snum->top] = 0;\n snum->top++;\n }\n }\n div_n = sdiv->top;\n num_n = snum->top;\n loop = num_n - div_n;\n wnum.neg = 0;\n wnum.d = &(snum->d[loop]);\n wnum.top = div_n;\n wnum.dmax = snum->dmax - loop;\n d0 = sdiv->d[div_n - 1];\n d1 = (div_n == 1) ? 0 : sdiv->d[div_n - 2];\n wnump = &(snum->d[num_n - 1]);\n if (!bn_wexpand(res, (loop + 1)))\n goto err;\n res->neg = (num->neg ^ divisor->neg);\n res->top = loop - no_branch;\n resp = &(res->d[loop - 1]);\n if (!bn_wexpand(tmp, (div_n + 1)))\n goto err;\n if (!no_branch) {\n if (BN_ucmp(&wnum, sdiv) >= 0) {\n bn_clear_top2max(&wnum);\n bn_sub_words(wnum.d, wnum.d, sdiv->d, div_n);\n *resp = 1;\n } else\n res->top--;\n }\n resp++;\n if (res->top == 0)\n res->neg = 0;\n else\n resp--;\n for (i = 0; i < loop - 1; i++, wnump--) {\n BN_ULONG q, l0;\n# if defined(BN_DIV3W) && !defined(OPENSSL_NO_ASM)\n BN_ULONG bn_div_3_words(BN_ULONG *, BN_ULONG, BN_ULONG);\n q = bn_div_3_words(wnump, d1, d0);\n# else\n BN_ULONG n0, n1, rem = 0;\n n0 = wnump[0];\n n1 = wnump[-1];\n if (n0 == d0)\n q = BN_MASK2;\n else {\n# ifdef BN_LLONG\n BN_ULLONG t2;\n# if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)\n q = (BN_ULONG)(((((BN_ULLONG) n0) << BN_BITS2) | n1) / d0);\n# else\n q = bn_div_words(n0, n1, d0);\n# endif\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n t2 = (BN_ULLONG) d1 *q;\n for (;;) {\n if (t2 <= ((((BN_ULLONG) rem) << BN_BITS2) | wnump[-2]))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n t2 -= d1;\n }\n# else\n BN_ULONG t2l, t2h;\n q = bn_div_words(n0, n1, d0);\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n# if defined(BN_UMULT_LOHI)\n BN_UMULT_LOHI(t2l, t2h, d1, q);\n# elif defined(BN_UMULT_HIGH)\n t2l = d1 * q;\n t2h = BN_UMULT_HIGH(d1, q);\n# else\n {\n BN_ULONG ql, qh;\n t2l = LBITS(d1);\n t2h = HBITS(d1);\n ql = LBITS(q);\n qh = HBITS(q);\n mul64(t2l, t2h, ql, qh);\n }\n# endif\n for (;;) {\n if ((t2h < rem) || ((t2h == rem) && (t2l <= wnump[-2])))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n if (t2l < d1)\n t2h--;\n t2l -= d1;\n }\n# endif\n }\n# endif\n l0 = bn_mul_words(tmp->d, sdiv->d, div_n, q);\n tmp->d[div_n] = l0;\n wnum.d--;\n if (bn_sub_words(wnum.d, wnum.d, tmp->d, div_n + 1)) {\n q--;\n if (bn_add_words(wnum.d, wnum.d, sdiv->d, div_n))\n (*wnump)++;\n }\n resp--;\n *resp = q;\n }\n bn_correct_top(snum);\n if (rm != NULL) {\n int neg = num->neg;\n BN_rshift(rm, snum, norm_shift);\n if (!BN_is_zero(rm))\n rm->neg = neg;\n bn_check_top(rm);\n }\n if (no_branch)\n bn_correct_top(res);\n BN_CTX_end(ctx);\n return 1;\n err:\n bn_check_top(rm);\n BN_CTX_end(ctx);\n return (0);\n}', 'void BN_CTX_end(BN_CTX *ctx)\n{\n CTXDBG_ENTRY("BN_CTX_end", ctx);\n if (ctx->err_stack)\n ctx->err_stack--;\n else {\n unsigned int fp = BN_STACK_pop(&ctx->stack);\n if (fp < ctx->used)\n BN_POOL_release(&ctx->pool, ctx->used - fp);\n ctx->used = fp;\n ctx->too_many = 0;\n }\n CTXDBG_EXIT(ctx);\n}', 'static unsigned int BN_STACK_pop(BN_STACK *st)\n{\n return st->indexes[--(st->depth)];\n}']
294
0
https://github.com/openssl/openssl/blob/9f519addc09b2005fa8c6cde36e3267de02577bb/apps/speed.c/#L2146
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}']
295
0
https://github.com/nginx/nginx/blob/70f7141074896fb1ff3e5fc08407ea0f64f2076b/src/core/ngx_sha1.c/#L230
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}']
296
0
https://github.com/openssl/openssl/blob/4718f449a3ecd5efac62b22d0fa9a759a7895dbc/test/handshake_helper.c/#L99
static void info_cb(const SSL *s, int where, int ret) { if (where & SSL_CB_ALERT) { HANDSHAKE_EX_DATA *ex_data = (HANDSHAKE_EX_DATA*)(SSL_get_ex_data(s, ex_data_idx)); if (where & SSL_CB_WRITE) { ex_data->alert_sent = ret; if (strcmp(SSL_alert_type_string(ret), "F") == 0 || strcmp(SSL_alert_desc_string(ret), "CN") == 0) ex_data->num_fatal_alerts_sent++; } else { ex_data->alert_received = ret; } } }
['static void info_cb(const SSL *s, int where, int ret)\n{\n if (where & SSL_CB_ALERT) {\n HANDSHAKE_EX_DATA *ex_data =\n (HANDSHAKE_EX_DATA*)(SSL_get_ex_data(s, ex_data_idx));\n if (where & SSL_CB_WRITE) {\n ex_data->alert_sent = ret;\n if (strcmp(SSL_alert_type_string(ret), "F") == 0\n || strcmp(SSL_alert_desc_string(ret), "CN") == 0)\n ex_data->num_fatal_alerts_sent++;\n } else {\n ex_data->alert_received = ret;\n }\n }\n}', 'void *SSL_get_ex_data(const SSL *s, int idx)\n{\n return CRYPTO_get_ex_data(&s->ex_data, idx);\n}', 'void *CRYPTO_get_ex_data(const CRYPTO_EX_DATA *ad, int idx)\n{\n if (ad->sk == NULL || idx >= sk_void_num(ad->sk))\n return NULL;\n return sk_void_value(ad->sk, idx);\n}']
297
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; }
['static int probable_prime_dh_safe(BIGNUM *p, int bits, const BIGNUM *padd,\n const BIGNUM *rem, BN_CTX *ctx)\n{\n int i, ret = 0;\n BIGNUM *t1, *qadd, *q;\n bits--;\n BN_CTX_start(ctx);\n t1 = BN_CTX_get(ctx);\n q = BN_CTX_get(ctx);\n qadd = BN_CTX_get(ctx);\n if (qadd == NULL)\n goto err;\n if (!BN_rshift1(qadd, padd))\n goto err;\n if (!BN_rand(q, bits, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ODD))\n goto err;\n if (!BN_mod(t1, q, qadd, ctx))\n goto err;\n if (!BN_sub(q, q, t1))\n goto err;\n if (rem == NULL) {\n if (!BN_add_word(q, 1))\n goto err;\n } else {\n if (!BN_rshift1(t1, rem))\n goto err;\n if (!BN_add(q, q, t1))\n goto err;\n }\n if (!BN_lshift1(p, q))\n goto err;\n if (!BN_add_word(p, 1))\n goto err;\n loop:\n for (i = 1; i < NUMPRIMES; i++) {\n BN_ULONG pmod = BN_mod_word(p, (BN_ULONG)primes[i]);\n BN_ULONG qmod = BN_mod_word(q, (BN_ULONG)primes[i]);\n if (pmod == (BN_ULONG)-1 || qmod == (BN_ULONG)-1)\n goto err;\n if (pmod == 0 || qmod == 0) {\n if (!BN_add(p, p, padd))\n goto err;\n if (!BN_add(q, q, qadd))\n goto err;\n goto loop;\n }\n }\n ret = 1;\n err:\n BN_CTX_end(ctx);\n bn_check_top(p);\n return ret;\n}', 'int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,\n BN_CTX *ctx)\n{\n int norm_shift, i, loop;\n BIGNUM *tmp, wnum, *snum, *sdiv, *res;\n BN_ULONG *resp, *wnump;\n BN_ULONG d0, d1;\n int num_n, div_n;\n int no_branch = 0;\n if ((num->top > 0 && num->d[num->top - 1] == 0) ||\n (divisor->top > 0 && divisor->d[divisor->top - 1] == 0)) {\n BNerr(BN_F_BN_DIV, BN_R_NOT_INITIALIZED);\n return 0;\n }\n bn_check_top(num);\n bn_check_top(divisor);\n if ((BN_get_flags(num, BN_FLG_CONSTTIME) != 0)\n || (BN_get_flags(divisor, BN_FLG_CONSTTIME) != 0)) {\n no_branch = 1;\n }\n bn_check_top(dv);\n bn_check_top(rm);\n if (BN_is_zero(divisor)) {\n BNerr(BN_F_BN_DIV, BN_R_DIV_BY_ZERO);\n return 0;\n }\n if (!no_branch && BN_ucmp(num, divisor) < 0) {\n if (rm != NULL) {\n if (BN_copy(rm, num) == NULL)\n return 0;\n }\n if (dv != NULL)\n BN_zero(dv);\n return 1;\n }\n BN_CTX_start(ctx);\n res = (dv == NULL) ? BN_CTX_get(ctx) : dv;\n tmp = BN_CTX_get(ctx);\n snum = BN_CTX_get(ctx);\n sdiv = BN_CTX_get(ctx);\n if (sdiv == NULL)\n goto err;\n norm_shift = BN_BITS2 - ((BN_num_bits(divisor)) % BN_BITS2);\n if (!(BN_lshift(sdiv, divisor, norm_shift)))\n goto err;\n sdiv->neg = 0;\n norm_shift += BN_BITS2;\n if (!(BN_lshift(snum, num, norm_shift)))\n goto err;\n snum->neg = 0;\n if (no_branch) {\n if (snum->top <= sdiv->top + 1) {\n if (bn_wexpand(snum, sdiv->top + 2) == NULL)\n goto err;\n for (i = snum->top; i < sdiv->top + 2; i++)\n snum->d[i] = 0;\n snum->top = sdiv->top + 2;\n } else {\n if (bn_wexpand(snum, snum->top + 1) == NULL)\n goto err;\n snum->d[snum->top] = 0;\n snum->top++;\n }\n }\n div_n = sdiv->top;\n num_n = snum->top;\n loop = num_n - div_n;\n wnum.neg = 0;\n wnum.d = &(snum->d[loop]);\n wnum.top = div_n;\n wnum.dmax = snum->dmax - loop;\n d0 = sdiv->d[div_n - 1];\n d1 = (div_n == 1) ? 0 : sdiv->d[div_n - 2];\n wnump = &(snum->d[num_n - 1]);\n if (!bn_wexpand(res, (loop + 1)))\n goto err;\n res->neg = (num->neg ^ divisor->neg);\n res->top = loop - no_branch;\n resp = &(res->d[loop - 1]);\n if (!bn_wexpand(tmp, (div_n + 1)))\n goto err;\n if (!no_branch) {\n if (BN_ucmp(&wnum, sdiv) >= 0) {\n bn_clear_top2max(&wnum);\n bn_sub_words(wnum.d, wnum.d, sdiv->d, div_n);\n *resp = 1;\n } else\n res->top--;\n }\n resp++;\n if (res->top == 0)\n res->neg = 0;\n else\n resp--;\n for (i = 0; i < loop - 1; i++, wnump--) {\n BN_ULONG q, l0;\n# if defined(BN_DIV3W) && !defined(OPENSSL_NO_ASM)\n BN_ULONG bn_div_3_words(BN_ULONG *, BN_ULONG, BN_ULONG);\n q = bn_div_3_words(wnump, d1, d0);\n# else\n BN_ULONG n0, n1, rem = 0;\n n0 = wnump[0];\n n1 = wnump[-1];\n if (n0 == d0)\n q = BN_MASK2;\n else {\n# ifdef BN_LLONG\n BN_ULLONG t2;\n# if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)\n q = (BN_ULONG)(((((BN_ULLONG) n0) << BN_BITS2) | n1) / d0);\n# else\n q = bn_div_words(n0, n1, d0);\n# endif\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n t2 = (BN_ULLONG) d1 *q;\n for (;;) {\n if (t2 <= ((((BN_ULLONG) rem) << BN_BITS2) | wnump[-2]))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n t2 -= d1;\n }\n# else\n BN_ULONG t2l, t2h;\n q = bn_div_words(n0, n1, d0);\n# ifndef REMAINDER_IS_ALREADY_CALCULATED\n rem = (n1 - q * d0) & BN_MASK2;\n# endif\n# if defined(BN_UMULT_LOHI)\n BN_UMULT_LOHI(t2l, t2h, d1, q);\n# elif defined(BN_UMULT_HIGH)\n t2l = d1 * q;\n t2h = BN_UMULT_HIGH(d1, q);\n# else\n {\n BN_ULONG ql, qh;\n t2l = LBITS(d1);\n t2h = HBITS(d1);\n ql = LBITS(q);\n qh = HBITS(q);\n mul64(t2l, t2h, ql, qh);\n }\n# endif\n for (;;) {\n if ((t2h < rem) || ((t2h == rem) && (t2l <= wnump[-2])))\n break;\n q--;\n rem += d0;\n if (rem < d0)\n break;\n if (t2l < d1)\n t2h--;\n t2l -= d1;\n }\n# endif\n }\n# endif\n l0 = bn_mul_words(tmp->d, sdiv->d, div_n, q);\n tmp->d[div_n] = l0;\n wnum.d--;\n if (bn_sub_words(wnum.d, wnum.d, tmp->d, div_n + 1)) {\n q--;\n if (bn_add_words(wnum.d, wnum.d, sdiv->d, div_n))\n (*wnump)++;\n }\n resp--;\n *resp = q;\n }\n bn_correct_top(snum);\n if (rm != NULL) {\n int neg = num->neg;\n BN_rshift(rm, snum, norm_shift);\n if (!BN_is_zero(rm))\n rm->neg = neg;\n bn_check_top(rm);\n }\n if (no_branch)\n bn_correct_top(res);\n BN_CTX_end(ctx);\n return 1;\n err:\n bn_check_top(rm);\n BN_CTX_end(ctx);\n return 0;\n}', 'int BN_rshift1(BIGNUM *r, const BIGNUM *a)\n{\n BN_ULONG *ap, *rp, t, c;\n int i, j;\n bn_check_top(r);\n bn_check_top(a);\n if (BN_is_zero(a)) {\n BN_zero(r);\n return 1;\n }\n i = a->top;\n ap = a->d;\n j = i - (ap[i - 1] == 1);\n if (a != r) {\n if (bn_wexpand(r, j) == NULL)\n return 0;\n r->neg = a->neg;\n }\n rp = r->d;\n t = ap[--i];\n c = (t & 1) ? BN_TBIT : 0;\n if (t >>= 1)\n rp[i] = t;\n while (i > 0) {\n t = ap[--i];\n rp[i] = ((t >> 1) & BN_MASK2) | c;\n c = (t & 1) ? BN_TBIT : 0;\n }\n r->top = j;\n if (!r->top)\n r->neg = 0;\n bn_check_top(r);\n return 1;\n}', 'int BN_set_word(BIGNUM *a, BN_ULONG w)\n{\n bn_check_top(a);\n if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)\n return 0;\n a->neg = 0;\n a->d[0] = w;\n a->top = (w ? 1 : 0);\n bn_check_top(a);\n return 1;\n}', 'static ossl_inline BIGNUM *bn_expand(BIGNUM *a, int bits)\n{\n if (bits > (INT_MAX - BN_BITS2 + 1))\n return NULL;\n if (((bits+BN_BITS2-1)/BN_BITS2) <= (a)->dmax)\n return a;\n return bn_expand2((a),(bits+BN_BITS2-1)/BN_BITS2);\n}', 'BIGNUM *bn_expand2(BIGNUM *b, int words)\n{\n bn_check_top(b);\n if (words > b->dmax) {\n BN_ULONG *a = bn_expand_internal(b, words);\n if (!a)\n return NULL;\n if (b->d) {\n OPENSSL_cleanse(b->d, b->dmax * sizeof(b->d[0]));\n bn_free_d(b);\n }\n b->d = a;\n b->dmax = words;\n }\n bn_check_top(b);\n return b;\n}', 'static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)\n{\n BN_ULONG *a = NULL;\n bn_check_top(b);\n if (words > (INT_MAX / (4 * BN_BITS2))) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);\n return NULL;\n }\n if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);\n return NULL;\n }\n if (BN_get_flags(b, BN_FLG_SECURE))\n a = OPENSSL_secure_zalloc(words * sizeof(*a));\n else\n a = OPENSSL_zalloc(words * sizeof(*a));\n if (a == NULL) {\n BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);\n return NULL;\n }\n assert(b->top <= words);\n if (b->top > 0)\n memcpy(a, b->d, sizeof(*a) * b->top);\n return a;\n}']
298
0
https://github.com/openssl/openssl/blob/e02c519cd32a55e6ad39a0cfbeeda775f9115f28/crypto/bn/bn_ctx.c/#L276
static unsigned int BN_STACK_pop(BN_STACK *st) { return st->indexes[--(st->depth)]; }
['int BN_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx)\n{\n int i, bits, ret = 0;\n BIGNUM *v, *rr;\n if (BN_get_flags(p, BN_FLG_CONSTTIME) != 0\n || BN_get_flags(a, BN_FLG_CONSTTIME) != 0) {\n BNerr(BN_F_BN_EXP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);\n return 0;\n }\n BN_CTX_start(ctx);\n rr = ((r == a) || (r == p)) ? BN_CTX_get(ctx) : r;\n v = BN_CTX_get(ctx);\n if (rr == NULL || v == NULL)\n goto err;\n if (BN_copy(v, a) == NULL)\n goto err;\n bits = BN_num_bits(p);\n if (BN_is_odd(p)) {\n if (BN_copy(rr, a) == NULL)\n goto err;\n } else {\n if (!BN_one(rr))\n goto err;\n }\n for (i = 1; i < bits; i++) {\n if (!BN_sqr(v, v, ctx))\n goto err;\n if (BN_is_bit_set(p, i)) {\n if (!BN_mul(rr, rr, v, ctx))\n goto err;\n }\n }\n if (r != rr && BN_copy(r, rr) == NULL)\n goto err;\n ret = 1;\n err:\n BN_CTX_end(ctx);\n bn_check_top(r);\n return ret;\n}', 'void BN_CTX_start(BN_CTX *ctx)\n{\n CTXDBG_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_sqr(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx)\n{\n int ret = bn_sqr_fixed_top(r, a, ctx);\n bn_correct_top(r);\n bn_check_top(r);\n return ret;\n}', 'int bn_sqr_fixed_top(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx)\n{\n int max, al;\n int ret = 0;\n BIGNUM *tmp, *rr;\n bn_check_top(a);\n al = a->top;\n if (al <= 0) {\n r->top = 0;\n r->neg = 0;\n return 1;\n }\n BN_CTX_start(ctx);\n rr = (a != r) ? r : BN_CTX_get(ctx);\n tmp = BN_CTX_get(ctx);\n if (rr == NULL || tmp == NULL)\n goto err;\n max = 2 * al;\n if (bn_wexpand(rr, max) == NULL)\n goto err;\n if (al == 4) {\n#ifndef BN_SQR_COMBA\n BN_ULONG t[8];\n bn_sqr_normal(rr->d, a->d, 4, t);\n#else\n bn_sqr_comba4(rr->d, a->d);\n#endif\n } else if (al == 8) {\n#ifndef BN_SQR_COMBA\n BN_ULONG t[16];\n bn_sqr_normal(rr->d, a->d, 8, t);\n#else\n bn_sqr_comba8(rr->d, a->d);\n#endif\n } else {\n#if defined(BN_RECURSION)\n if (al < BN_SQR_RECURSIVE_SIZE_NORMAL) {\n BN_ULONG t[BN_SQR_RECURSIVE_SIZE_NORMAL * 2];\n bn_sqr_normal(rr->d, a->d, al, t);\n } else {\n int j, k;\n j = BN_num_bits_word((BN_ULONG)al);\n j = 1 << (j - 1);\n k = j + j;\n if (al == j) {\n if (bn_wexpand(tmp, k * 2) == NULL)\n goto err;\n bn_sqr_recursive(rr->d, a->d, al, tmp->d);\n } else {\n if (bn_wexpand(tmp, max) == NULL)\n goto err;\n bn_sqr_normal(rr->d, a->d, al, tmp->d);\n }\n }\n#else\n if (bn_wexpand(tmp, max) == NULL)\n goto err;\n bn_sqr_normal(rr->d, a->d, al, tmp->d);\n#endif\n }\n rr->neg = 0;\n rr->top = max;\n rr->flags |= BN_FLG_FIXED_TOP;\n if (r != rr && BN_copy(r, rr) == NULL)\n goto err;\n ret = 1;\n err:\n bn_check_top(rr);\n bn_check_top(tmp);\n BN_CTX_end(ctx);\n return ret;\n}', 'void BN_CTX_end(BN_CTX *ctx)\n{\n CTXDBG_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}']
299
0
https://github.com/openssl/openssl/blob/9b02dc97e4963969da69675a871dbe80e6d31cda/crypto/bn/bn_lib.c/#L393
int BN_set_word(BIGNUM *a, BN_ULONG w) { bn_check_top(a); if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL) return 0; a->neg = 0; a->d[0] = w; a->top = (w ? 1 : 0); bn_check_top(a); return 1; }
['static int test_gf2m_add(void)\n{\n BIGNUM *a = NULL, *b = NULL, *c = NULL;\n int i, st = 0;\n if (!TEST_ptr(a = BN_new())\n || !TEST_ptr(b = BN_new())\n || !TEST_ptr(c = BN_new()))\n goto err;\n for (i = 0; i < NUM0; i++) {\n BN_rand(a, 512, 0, 0);\n BN_copy(b, BN_value_one());\n a->neg = rand_neg();\n b->neg = rand_neg();\n BN_GF2m_add(c, a, b);\n if (!TEST_false((BN_is_odd(a) && BN_is_odd(c))\n || (!BN_is_odd(a) && !BN_is_odd(c))))\n goto err;\n BN_GF2m_add(c, c, c);\n if (!TEST_BN_eq_zero(c))\n goto err;\n }\n st = 1;\n err:\n BN_free(a);\n BN_free(b);\n BN_free(c);\n return st;\n}', 'int BN_rand(BIGNUM *rnd, int bits, int top, int bottom)\n{\n return bnrand(NORMAL, 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}', '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}']
300
0
https://github.com/openssl/openssl/blob/ea32151f7b9353f8906188d007c6893704ac17bb/crypto/bn/bn_gf2m.c/#L449
int BN_GF2m_mod_mul_arr(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const int p[], BN_CTX *ctx) { int zlen, i, j, k, ret = 0; BIGNUM *s; BN_ULONG x1, x0, y1, y0, zz[4]; bn_check_top(a); bn_check_top(b); if (a == b) { return BN_GF2m_mod_sqr_arr(r, a, p, ctx); } BN_CTX_start(ctx); if ((s = BN_CTX_get(ctx)) == NULL) goto err; zlen = a->top + b->top + 4; if (!bn_wexpand(s, zlen)) goto err; s->top = zlen; for (i = 0; i < zlen; i++) s->d[i] = 0; for (j = 0; j < b->top; j += 2) { y0 = b->d[j]; y1 = ((j + 1) == b->top) ? 0 : b->d[j + 1]; for (i = 0; i < a->top; i += 2) { x0 = a->d[i]; x1 = ((i + 1) == a->top) ? 0 : a->d[i + 1]; bn_GF2m_mul_2x2(zz, x1, x0, y1, y0); for (k = 0; k < 4; k++) s->d[i + j + k] ^= zz[k]; } } bn_correct_top(s); if (BN_GF2m_mod_arr(r, s, p)) ret = 1; bn_check_top(r); err: BN_CTX_end(ctx); return ret; }
['int BN_GF2m_mod_exp_arr(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,\n const int p[], BN_CTX *ctx)\n{\n int ret = 0, i, n;\n BIGNUM *u;\n bn_check_top(a);\n bn_check_top(b);\n if (BN_is_zero(b))\n return (BN_one(r));\n if (BN_abs_is_word(b, 1))\n return (BN_copy(r, a) != NULL);\n BN_CTX_start(ctx);\n if ((u = BN_CTX_get(ctx)) == NULL)\n goto err;\n if (!BN_GF2m_mod_arr(u, a, p))\n goto err;\n n = BN_num_bits(b) - 1;\n for (i = n - 1; i >= 0; i--) {\n if (!BN_GF2m_mod_sqr_arr(u, u, p, ctx))\n goto err;\n if (BN_is_bit_set(b, i)) {\n if (!BN_GF2m_mod_mul_arr(u, u, a, p, ctx))\n goto err;\n }\n }\n if (!BN_copy(r, u))\n goto err;\n bn_check_top(r);\n ret = 1;\n err:\n BN_CTX_end(ctx);\n return ret;\n}', 'int BN_GF2m_mod_arr(BIGNUM *r, const BIGNUM *a, const int p[])\n{\n int j, k;\n int n, dN, d0, d1;\n BN_ULONG zz, *z;\n bn_check_top(a);\n if (!p[0]) {\n BN_zero(r);\n return 1;\n }\n if (a != r) {\n if (!bn_wexpand(r, a->top))\n return 0;\n for (j = 0; j < a->top; j++) {\n r->d[j] = a->d[j];\n }\n r->top = a->top;\n }\n z = r->d;\n dN = p[0] / BN_BITS2;\n for (j = r->top - 1; j > dN;) {\n zz = z[j];\n if (z[j] == 0) {\n j--;\n continue;\n }\n z[j] = 0;\n for (k = 1; p[k] != 0; k++) {\n n = p[0] - p[k];\n d0 = n % BN_BITS2;\n d1 = BN_BITS2 - d0;\n n /= BN_BITS2;\n z[j - n] ^= (zz >> d0);\n if (d0)\n z[j - n - 1] ^= (zz << d1);\n }\n n = dN;\n d0 = p[0] % BN_BITS2;\n d1 = BN_BITS2 - d0;\n z[j - n] ^= (zz >> d0);\n if (d0)\n z[j - n - 1] ^= (zz << d1);\n }\n while (j == dN) {\n d0 = p[0] % BN_BITS2;\n zz = z[dN] >> d0;\n if (zz == 0)\n break;\n d1 = BN_BITS2 - d0;\n if (d0)\n z[dN] = (z[dN] << d1) >> d1;\n else\n z[dN] = 0;\n z[0] ^= zz;\n for (k = 1; p[k] != 0; k++) {\n BN_ULONG tmp_ulong;\n n = p[k] / BN_BITS2;\n d0 = p[k] % BN_BITS2;\n d1 = BN_BITS2 - d0;\n z[n] ^= (zz << d0);\n if (d0 && (tmp_ulong = zz >> d1))\n z[n + 1] ^= tmp_ulong;\n }\n }\n bn_correct_top(r);\n return 1;\n}', 'int BN_GF2m_mod_mul_arr(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,\n const int p[], BN_CTX *ctx)\n{\n int zlen, i, j, k, ret = 0;\n BIGNUM *s;\n BN_ULONG x1, x0, y1, y0, zz[4];\n bn_check_top(a);\n bn_check_top(b);\n if (a == b) {\n return BN_GF2m_mod_sqr_arr(r, a, p, ctx);\n }\n BN_CTX_start(ctx);\n if ((s = BN_CTX_get(ctx)) == NULL)\n goto err;\n zlen = a->top + b->top + 4;\n if (!bn_wexpand(s, zlen))\n goto err;\n s->top = zlen;\n for (i = 0; i < zlen; i++)\n s->d[i] = 0;\n for (j = 0; j < b->top; j += 2) {\n y0 = b->d[j];\n y1 = ((j + 1) == b->top) ? 0 : b->d[j + 1];\n for (i = 0; i < a->top; i += 2) {\n x0 = a->d[i];\n x1 = ((i + 1) == a->top) ? 0 : a->d[i + 1];\n bn_GF2m_mul_2x2(zz, x1, x0, y1, y0);\n for (k = 0; k < 4; k++)\n s->d[i + j + k] ^= zz[k];\n }\n }\n bn_correct_top(s);\n if (BN_GF2m_mod_arr(r, s, p))\n ret = 1;\n bn_check_top(r);\n err:\n BN_CTX_end(ctx);\n return ret;\n}']