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; }
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; }