コード例 #1
0
ファイル: OTPCalculator.c プロジェクト: 0wsqqsw/lantern
int OTPCalculator_Init (OTPCalculator *calc, int num_otps, int cipher)
{
    ASSERT(num_otps >= 0)
    ASSERT(BEncryption_cipher_valid(cipher))
    
    // init arguments
    calc->num_otps = num_otps;
    calc->cipher = cipher;
    
    // remember block size
    calc->block_size = BEncryption_cipher_block_size(calc->cipher);
    
    // calculate number of blocks
    if (calc->num_otps > SIZE_MAX / sizeof(otp_t)) {
        goto fail0;
    }
    calc->num_blocks = bdivide_up(calc->num_otps * sizeof(otp_t), calc->block_size);
    
    // allocate buffer
    if (!(calc->data = (otp_t *)BAllocArray(calc->num_blocks, calc->block_size))) {
        goto fail0;
    }
    
    // init debug object
    DebugObject_Init(&calc->d_obj);
    
    return 1;
    
fail0:
    return 0;
}
コード例 #2
0
ファイル: OTPChecker.c プロジェクト: carriercomm/NCD
int OTPChecker_Init (OTPChecker *mc, int num_otps, int cipher, int num_tables, BThreadWorkDispatcher *twd)
{
    ASSERT(num_otps > 0)
    ASSERT(BEncryption_cipher_valid(cipher))
    ASSERT(num_tables > 0)
    
    // init arguments
    mc->num_otps = num_otps;
    mc->cipher = cipher;
    mc->num_tables = num_tables;
    mc->twd = twd;
    
    // set no handlers
    mc->handler = NULL;
    
    // set number of entries
    if (mc->num_otps > INT_MAX / 2) {
        goto fail0;
    }
    mc->num_entries = 2 * mc->num_otps;
    
    // set no tables used
    mc->tables_used = 0;
    mc->next_table = 0;
    
    // initialize calculator
    if (!OTPCalculator_Init(&mc->calc, mc->num_otps, cipher)) {
        goto fail0;
    }
    
    // allocate tables
    if (!(mc->tables = BAllocArray(mc->num_tables, sizeof(mc->tables[0])))) {
        goto fail1;
    }
    
    // allocate entries
    if (!(mc->entries = BAllocArray2(mc->num_tables, mc->num_entries, sizeof(mc->entries[0])))) {
        goto fail2;
    }
    
    // initialize tables
    for (int i = 0; i < mc->num_tables; i++) {
        struct OTPChecker_table *table = &mc->tables[i];
        table->entries = mc->entries + (size_t)i * mc->num_entries;
        OTPChecker_Table_Empty(mc, table);
    }
    
    // have no work
    mc->tw_have = 0;
    
    DebugObject_Init(&mc->d_obj);
    return 1;
    
fail2:
    BFree(mc->tables);
fail1:
    OTPCalculator_Free(&mc->calc);
fail0:
    return 0;
}
コード例 #3
0
ファイル: OTPGenerator.c プロジェクト: 2722/lantern
int OTPGenerator_Init (OTPGenerator *g, int num_otps, int cipher, BThreadWorkDispatcher *twd, OTPGenerator_handler handler, void *user)
{
    ASSERT(num_otps >= 0)
    ASSERT(BEncryption_cipher_valid(cipher))

    // init arguments
    g->num_otps = num_otps;
    g->cipher = cipher;
    g->twd = twd;
    g->handler = handler;
    g->user = user;

    // init position
    g->position = g->num_otps;

    // init calculator
    if (!OTPCalculator_Init(&g->calc[0], g->num_otps, g->cipher)) {
        goto fail0;
    }

    // init calculator
    if (!OTPCalculator_Init(&g->calc[1], g->num_otps, g->cipher)) {
        goto fail1;
    }

    // set current calculator
    g->cur_calc = 0;

    // have no work
    g->tw_have = 0;

    DebugObject_Init(&g->d_obj);
    return 1;

fail1:
    OTPCalculator_Free(&g->calc[0]);
fail0:
    return 0;
}