示例#1
0
/* Take a chunk of data, encrypt it in the same way OpenSSL would
 * (with a default of AES in CBC mode).
*/
size_t
rij_encrypt(unsigned char *in, size_t in_len,
    const char *key, const int key_len,
    unsigned char *out, int encryption_mode)
{
    RIJNDAEL_context    ctx;
    int                 i, pad_val;
    unsigned char      *ondx = out;

    rijndael_init(&ctx, key, key_len, NULL, encryption_mode);

    /* Prepend the salt to the ciphertext...
    */
    memcpy(ondx, "Salted__", SALT_LEN);
    ondx+=SALT_LEN;
    memcpy(ondx, ctx.salt, SALT_LEN);
    ondx+=SALT_LEN;

    /* Add padding to the original plaintext to ensure that it is a
     * multiple of the Rijndael block size
    */
    pad_val = RIJNDAEL_BLOCKSIZE - (in_len % RIJNDAEL_BLOCKSIZE);
    for (i = (int)in_len; i < ((int)in_len+pad_val); i++)
        in[i] = pad_val;

    block_encrypt(&ctx, in, in_len+pad_val, ondx, ctx.iv);

    ondx += in_len+pad_val;

    zero_buf((char *)ctx.key, RIJNDAEL_MAX_KEYSIZE);
    zero_buf((char *)ctx.iv, RIJNDAEL_BLOCKSIZE);
    zero_buf((char *)ctx.salt, SALT_LEN);

    return(ondx - out);
}
示例#2
0
/* Decrypt the given data.
*/
size_t
rij_decrypt(unsigned char *in, size_t in_len,
    const char *key, const int key_len,
    unsigned char *out, int encryption_mode)
{
    RIJNDAEL_context    ctx;
    int                 i, pad_val, pad_err = 0;
    unsigned char      *pad_s;
    unsigned char      *ondx = out;

    if(in == NULL || key == NULL || out == NULL)
        return 0;

    rijndael_init(&ctx, key, key_len, in, encryption_mode);

    /* Remove the first block since it contains the salt (it was consumed
     * by the rijndael_init() function above).
    */
    in_len -= RIJNDAEL_BLOCKSIZE;
    memmove(in, in+RIJNDAEL_BLOCKSIZE, in_len);

    block_decrypt(&ctx, in, in_len, out, ctx.iv);

    ondx += in_len;

    /* Find and remove padding.
    */
    pad_val = *(ondx-1);

    if(pad_val >= 0 && pad_val <= RIJNDAEL_BLOCKSIZE)
    {
        pad_s = ondx - pad_val;

        for(i=0; i < (ondx-pad_s); i++)
        {
            if(*(pad_s+i) != pad_val)
                pad_err++;
        }

        if(pad_err == 0)
            ondx -= pad_val;
    }

    *ondx = '\0';

    zero_buf((char *)ctx.key, RIJNDAEL_MAX_KEYSIZE);
    zero_buf((char *)ctx.iv, RIJNDAEL_BLOCKSIZE);
    zero_buf((char *)ctx.salt, SALT_LEN);

    return(ondx - out);
}
示例#3
0
文件: fwknop.c 项目: rafavg77/fwknop
static void
zero_buf_wrapper(char *buf, int len)
{

    if(buf == NULL || len == 0)
        return;

    if(zero_buf(buf, len) == FKO_ERROR_ZERO_OUT_DATA)
        log_msg(LOG_VERBOSITY_ERROR,
                "[*] Could not zero out sensitive data buffer.");

    return;
}
示例#4
0
文件: fko_util.c 项目: ksyeung/fwknop
/* zero out a buffer before free()
*/
int zero_free(char *buf, int len)
{
    int res = FKO_SUCCESS;

    if(buf == NULL)
        return res;

    if(len == 0)
    {
        free(buf);  /* always free() if buf != NULL */
        return res;
    }

    res = zero_buf(buf, len);

    free(buf);

    return res;
}
示例#5
0
文件: fko_util.c 项目: vaygr/fwknop
/* zero out a buffer before free()
*/
int zero_free(char *buf, int len)
{
    int res = FKO_SUCCESS;

    if(buf == NULL)
        return res;

    if(len == 0)
    {
        free(buf);  /* always free() if buf != NULL */
        return res;
    }

    res = zero_buf(buf, len);

    free(buf);

#if HAVE_LIBFIU
    fiu_return_on("zero_free_err", FKO_ERROR_ZERO_OUT_DATA);
#endif

    return res;
}
示例#6
0
int main(int argc, char **argv)
{
    char *trace_file_name;
    int trace_view_on = 0;
    int branch_prediction_method = 0;
    unsigned int cycle_number = 0;

    // Parse Inputs
    if (argc == 2) {
        trace_file_name = argv[1];
        trace_view_on = 0;
        branch_prediction_method = 0;       
    } else if (argc == 4) {
        trace_file_name = argv[1];
        trace_view_on = atoi(argv[2]);
        branch_prediction_method = (atoi(argv[3]) == 1) ? 1 : 0;
    } else {
        fprintf(stdout, "\nUSAGE: tv <trace_file> <switch - any character> <branch_prediction - 0|1>\n");
        fprintf(stdout, "\n(switch) to turn on or off individual item view.\n");
        fprintf(stdout, "(branch_prediction) sets the branch prediction method as \'assume not taken\' (0), or a 1-bit branch predictor (1)\n\n");
        exit(0);
    }

    // debug - print parsed options
    char dbg_msg[200];
    sprintf(dbg_msg,
            "\n-debug- parsed inputs. file=%s, view_trace=%d, branch_pred=%d\n",
            trace_file_name,
            (trace_view_on == 0) ? 0 : 1,
            (branch_prediction_method == 0) ? 0 : 1
           );
    debug_print(dbg_msg);

    // Open the trace file.
    fprintf(stdout, "\n ** opening file %s\n", trace_file_name);
    trace_fd = fopen(trace_file_name, "rb");
    if (!trace_fd) {
        fprintf(stdout, "\ntrace file %s not opened.\n\n", trace_file_name);
        exit(0);
    }

    trace_init();

    // store what instruction is in each stage of the pipeline (can be no-ops)
    struct trace_item new_instruction;
    struct trace_item if_stage;
    struct trace_item id_stage;
    struct trace_item ex_stage;
    struct trace_item mem_stage;
    struct trace_item wb_stage;
    zero_buf(&new_instruction);
    zero_buf(&if_stage);
    zero_buf(&id_stage);
    zero_buf(&ex_stage);
    zero_buf(&mem_stage);
    zero_buf(&wb_stage);
    memset(&btb_table, 0, sizeof(short) * BTB_ENTRIES);

    int instructions_left = 5;  
    while(instructions_left) {
        cycle_number++;

        if(trace_view_on) { 
            print_finished_instruction(&wb_stage, cycle_number);
        }

        int hazard = 0;

        //detection of non-happy
        if(branch_prediction_method == 0 && ex_stage.type == ti_BRANCH){
            if(ex_stage.PC + 4 != id_stage.PC){
                hazard = 2; //incorrect no prediction
                debug_print("[HAZARD] Incorrect default (not taken) prediciton\n");
            }
        }
        else if(ex_stage.type == ti_JTYPE|| ex_stage.type == ti_JRTYPE){
            hazard = 2;  //jump
            debug_print("[HAZARD] jump\n");
        }
        else if(branch_prediction_method == 1 && ex_stage.type == ti_BRANCH){
            if(ex_stage.PC +4 == id_stage.PC){ // not taken
                if(get_btb_value(ex_stage.PC) == 1){ //predict taken
                    hazard = 2; //branch
                    debug_print("[HAZARD] predicted taken when not taken\n");
                    set_btb_value(ex_stage.PC, 0);
                }
            }
            else{ //taken
                if(get_btb_value(ex_stage.PC) == 0){ //predict not taken
                    hazard = 2; //branch
                    debug_print("[HAZARD] predicted not taken when taken\n");
                    set_btb_value(ex_stage.PC, 1);
                }
            }
        }
        else if(ex_stage.type == ti_LOAD){
            if(ex_stage.dReg == id_stage.sReg_a || ex_stage.dReg == id_stage.sReg_b){
                hazard = 1;  //forward
                debug_print("[HAZARD] forward\n");
            }
        }


        wb_stage  = mem_stage;
        mem_stage = ex_stage; 

        switch(hazard) {
            case 0: //happy path
                ex_stage = id_stage;
                id_stage = if_stage;
                if(!read_instruction(&if_stage)) {
                    instructions_left--;
                    zero_buf(&if_stage);
                }
                break;

            case 1: //forward
                zero_buf(&ex_stage); 
                break;

            case 2: //branch resolve/jump resolve
                add_queued_instruction(&id_stage);
                add_queued_instruction(&if_stage);
                zero_buf(&id_stage);
                zero_buf(&if_stage);  
                ex_stage = id_stage;
                id_stage = if_stage;
                if(!read_instruction(&if_stage)) {
                    instructions_left--;
                    zero_buf(&if_stage);
                }
                break;

            default:
                printf("f**k.\n");
                exit(1);
        }
    }

    printf("+ Simulation terminates at cycle : %u\n", cycle_number); 

    trace_uninit();

    exit(0);
}