int SelfTestGost14Cbc() { const size_t svLen = sizeof(kSeltTestGost14CbcSV) / sizeof(kSeltTestGost14CbcSV[0]); unsigned char outText[textLen14]; unsigned char outTextDec[textLen14]; unsigned char ctx[kCbc14ContextLen]; if (init_cbc_14_impl(kSeltTestGost14MasterKeyData, ctx, kSeltTestGost14CbcSV, svLen)) return -1; if (encrypt_cbc(ctx, kSeltTestGost14PlainText, outText, textLen14)) return -1; free_cbc(ctx); if (init_cbc_14_impl(kSeltTestGost14MasterKeyData, ctx, kSeltTestGost14CbcSV, svLen)) return -1; if (decrypt_cbc(ctx, outText, outTextDec, textLen14)) return -1; free_cbc(ctx); if (memcmp(outTextDec, kSeltTestGost14PlainText, textLen14)) return -1; return memcmp(outText, kSeltTestGost14CbcEncText, textLen14); }
bool CCrypt::setKey(const BYTE *pKey, size_t cbKeyLen) { // full external key. decode & check password if (cbKeyLen != sizeof(ExternalKey)) return false; BYTE tmpHash[32]; slow_hash(m_password, m_password.GetLength(), tmpHash); BYTE ctx[kCbc14ContextLen]; init_cbc_14(tmpHash, ctx, iv0, _countof(iv0)); ExternalKey tmp = { 0 }; decrypt_cbc(ctx, (BYTE*)pKey, (BYTE*)&tmp, sizeof(tmp)); free_cbc(ctx); if (tmp.m_crc32 != crc32(0xAbbaDead, (LPCBYTE)m_password.GetString(), m_password.GetLength())) return false; memcpy(m_key, &tmp.m_key, KEY_LENGTH); memcpy(m_iv, &tmp.m_iv, KEY_LENGTH); init_cbc_14(m_key, m_ctx, m_iv, KEY_LENGTH); return m_valid = true; }
bool CCrypt::getKey(BYTE *pKey, size_t cbKeyLen) { if (!m_valid || cbKeyLen < sizeof(ExternalKey)) return false; ExternalKey tmp = { 0 }; memcpy(&tmp.m_key, m_key, KEY_LENGTH); memcpy(&tmp.m_iv, m_iv, KEY_LENGTH); tmp.m_crc32 = crc32(0xAbbaDead, (LPCBYTE)m_password.GetString(), m_password.GetLength()); getRandomBytes(tmp.slack, sizeof(tmp.slack)); BYTE tmpHash[32]; slow_hash(m_password, m_password.GetLength(), tmpHash); BYTE ctx[kCbc14ContextLen]; init_cbc_14(tmpHash, ctx, iv0, _countof(iv0)); bool val = !encrypt_cbc(ctx, (BYTE*)&tmp, pKey, cbKeyLen); free_cbc(ctx); return val; }
static int afalg_destroy(ENGINE *e) { ERR_unload_AFALG_strings(); free_cbc(); return 1; }