本文整理汇总了C++中SymmetricKey类的典型用法代码示例。如果您正苦于以下问题:C++ SymmetricKey类的具体用法?C++ SymmetricKey怎么用?C++ SymmetricKey使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了SymmetricKey类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: generateKey
bool OSSLDES::generateKey(SymmetricKey& key, RNG* rng /* = NULL */)
{
if (rng == NULL)
{
return false;
}
if (key.getBitLen() == 0)
{
return false;
}
ByteString keyBits;
// don't count parity bit
if (!rng->generateRandom(keyBits, key.getBitLen()/7))
{
return false;
}
// fix the odd parity
size_t i;
for (i = 0; i < keyBits.size(); i++)
{
keyBits[i] = odd_parity[keyBits[i]];
}
return key.setKeyBits(keyBits);
}
示例2: SSH_SERVER_EXCEPTION
void SshKeyExchange::sendNewKeysPacket(const SshIncomingPacket &dhReply,
const QByteArray &clientId)
{
const SshKeyExchangeReply &reply
= dhReply.extractKeyExchangeReply(m_serverHostKeyAlgo);
if (reply.f <= 0 || reply.f >= m_dhKey->group_p()) {
throw SSH_SERVER_EXCEPTION(SSH_DISCONNECT_KEY_EXCHANGE_FAILED,
"Server sent invalid f.");
}
QByteArray concatenatedData = AbstractSshPacket::encodeString(clientId);
concatenatedData += AbstractSshPacket::encodeString(m_serverId);
concatenatedData += AbstractSshPacket::encodeString(m_clientKexInitPayload);
concatenatedData += AbstractSshPacket::encodeString(m_serverKexInitPayload);
concatenatedData += reply.k_s;
concatenatedData += AbstractSshPacket::encodeMpInt(m_dhKey->get_y());
concatenatedData += AbstractSshPacket::encodeMpInt(reply.f);
SymmetricKey k = m_dhKey->derive_key(reply.f);
m_k = AbstractSshPacket::encodeMpInt(BigInt(k.begin(), k.length()));
concatenatedData += m_k;
m_hash.reset(get_hash(botanSha1Name()));
const SecureVector<byte> &hashResult
= m_hash->process(convertByteArray(concatenatedData),
concatenatedData.size());
m_h = convertByteArray(hashResult);
QScopedPointer<Public_Key> sigKey;
QScopedPointer<PK_Verifier> verifier;
if (m_serverHostKeyAlgo == SshCapabilities::PubKeyDss) {
const DL_Group group(reply.parameters.at(0), reply.parameters.at(1),
reply.parameters.at(2));
DSA_PublicKey * const dsaKey
= new DSA_PublicKey(group, reply.parameters.at(3));
sigKey.reset(dsaKey);
verifier.reset(get_pk_verifier(*dsaKey,
botanEmsaAlgoName(SshCapabilities::PubKeyDss)));
} else if (m_serverHostKeyAlgo == SshCapabilities::PubKeyRsa) {
RSA_PublicKey * const rsaKey
= new RSA_PublicKey(reply.parameters.at(1), reply.parameters.at(0));
sigKey.reset(rsaKey);
verifier.reset(get_pk_verifier(*rsaKey,
botanEmsaAlgoName(SshCapabilities::PubKeyRsa)));
} else {
Q_ASSERT(!"Impossible: Neither DSS nor RSA!");
}
const byte * const botanH = convertByteArray(m_h);
const Botan::byte * const botanSig
= convertByteArray(reply.signatureBlob);
if (!verifier->verify_message(botanH, m_h.size(), botanSig,
reply.signatureBlob.size())) {
throw SSH_SERVER_EXCEPTION(SSH_DISCONNECT_KEY_EXCHANGE_FAILED,
"Invalid signature in SSH_MSG_KEXDH_REPLY packet.");
}
m_sendFacility.sendNewKeysPacket();
}
示例3: CPPUNIT_ASSERT
void GOSTTests::testHmac()
{
// Get an RNG and HMAC GOST R34.11-94 instance
CPPUNIT_ASSERT((rng = CryptoFactory::i()->getRNG()) != NULL);
CPPUNIT_ASSERT((mac = CryptoFactory::i()->getMacAlgorithm("hmac-gost")) != NULL);
// Key
char pk[] = "a_key_for_HMAC-GOST_R-34.11-94_test";
ByteString k((unsigned char *)pk, sizeof(pk));
SymmetricKey key;
CPPUNIT_ASSERT(key.setKeyBits(k));
// Generate some random input data
ByteString b;
CPPUNIT_ASSERT(rng->generateRandom(b, 53287));
// Sign the MAC using our implementation in a single operation
ByteString mResult1;
CPPUNIT_ASSERT(mac->signInit(&key));
CPPUNIT_ASSERT(mac->signUpdate(b));
CPPUNIT_ASSERT(mac->signFinal(mResult1));
// Sign the MAC in a multiple part operation
ByteString mResult2;
CPPUNIT_ASSERT(mac->signInit(&key));
CPPUNIT_ASSERT(mac->signUpdate(b.substr(0, 567)));
CPPUNIT_ASSERT(mac->signUpdate(b.substr(567, 989)));
CPPUNIT_ASSERT(mac->signUpdate(b.substr(567 + 989)));
CPPUNIT_ASSERT(mac->signFinal(mResult2));
// Now verify the MAC using our implementation in a single operation
CPPUNIT_ASSERT(mac->verifyInit(&key));
CPPUNIT_ASSERT(mac->verifyUpdate(b));
CPPUNIT_ASSERT(mac->verifyFinal(mResult2));
// Now verify the MAC in a multiple part operation
CPPUNIT_ASSERT(mac->verifyInit(&key));
CPPUNIT_ASSERT(mac->verifyUpdate(b.substr(0, 567)));
CPPUNIT_ASSERT(mac->verifyUpdate(b.substr(567, 989)));
CPPUNIT_ASSERT(mac->verifyUpdate(b.substr(567 + 989)));
CPPUNIT_ASSERT(mac->verifyFinal(mResult1));
// Check if bad key is refused
mResult1[10] ^= 0x11;
CPPUNIT_ASSERT(mac->verifyInit(&key));
CPPUNIT_ASSERT(mac->verifyUpdate(b));
CPPUNIT_ASSERT(!mac->verifyFinal(mResult1));
CryptoFactory::i()->recycleMacAlgorithm(mac);
mac = NULL;
rng = NULL;
}
示例4: set_key
void XTS_Decryption::set_key(const SymmetricKey& key)
{
u32bit key_half = key.length() / 2;
if(key.length() % 2 == 1 || !cipher->valid_keylength(key_half))
throw Invalid_Key_Length(name(), key.length());
cipher->set_key(key.begin(), key_half);
cipher2->set_key(key.begin() + key_half, key_half);
}
示例5: rfc3394_keywrap
secure_vector<uint8_t> rfc3394_keywrap(const secure_vector<uint8_t>& key,
const SymmetricKey& kek)
{
BOTAN_ARG_CHECK(kek.size() == 16 || kek.size() == 24 || kek.size() == 32,
"Invalid KEK length for NIST key wrap");
const std::string cipher_name = "AES-" + std::to_string(8*kek.size());
std::unique_ptr<BlockCipher> aes(BlockCipher::create_or_throw(cipher_name));
aes->set_key(kek);
std::vector<uint8_t> wrapped = nist_key_wrap(key.data(), key.size(), *aes);
return secure_vector<uint8_t>(wrapped.begin(), wrapped.end());
}
示例6: key_agreement
bool ne7ssh_crypt::makeKexSecret (Botan::SecureVector<Botan::byte> &result, Botan::BigInt &f)
{
PK_Key_Agreement key_agreement(*privKexKey, "Raw");
SymmetricKey negotiated = key_agreement.derive_key(32, (byte*)f.data(), f.bytes(), "");
// SymmetricKey negotiated = privKexKey->derive_key (f);
if (!negotiated.length()) return false;
BigInt Kint (negotiated.begin(), negotiated.length());
ne7ssh_string::bn2vector(result, Kint);
K = Botan::SecureVector<Botan::byte>(result);
delete privKexKey;
privKexKey = 0;
return true;
}
示例7: rfc3394_keyunwrap
secure_vector<uint8_t> rfc3394_keyunwrap(const secure_vector<uint8_t>& key,
const SymmetricKey& kek)
{
BOTAN_ARG_CHECK(kek.size() == 16 || kek.size() == 24 || kek.size() == 32,
"Invalid KEK length for NIST key wrap");
BOTAN_ARG_CHECK(key.size() >= 16 && key.size() % 8 == 0,
"Bad input key size for NIST key unwrap");
const std::string cipher_name = "AES-" + std::to_string(8*kek.size());
std::unique_ptr<BlockCipher> aes(BlockCipher::create_or_throw(cipher_name));
aes->set_key(kek);
return nist_key_unwrap(key.data(), key.size(), *aes);
}
示例8: derive_key
SymmetricKey derive_key(const std::string& param,
const SymmetricKey& masterkey,
u32bit outputlength)
{
std::auto_ptr<KDF> kdf(get_kdf("KDF2(SHA-1)"));
return kdf->derive_key(outputlength, masterkey.bits_of(), param);
}
示例9: set_key
void Transform_Filter::set_key(const SymmetricKey& key)
{
if(Keyed_Transform* keyed = dynamic_cast<Keyed_Transform*>(m_transform.get()))
keyed->set_key(key);
else if(key.length() != 0)
throw std::runtime_error("Transform " + name() + " does not accept keys");
}
示例10: rfc3394_keyunwrap
secure_vector<uint8_t> rfc3394_keyunwrap(const secure_vector<uint8_t>& key,
const SymmetricKey& kek)
{
if(key.size() < 16 || key.size() % 8 != 0)
throw Invalid_Argument("Bad input key size for NIST key unwrap");
if(kek.size() != 16 && kek.size() != 24 && kek.size() != 32)
throw Invalid_Argument("Bad KEK length " + std::to_string(kek.size()) + " for NIST key unwrap");
const std::string cipher_name = "AES-" + std::to_string(8*kek.size());
std::unique_ptr<BlockCipher> aes(BlockCipher::create_or_throw(cipher_name));
aes->set_key(kek);
const size_t n = (key.size() - 8) / 8;
secure_vector<uint8_t> R(n * 8);
secure_vector<uint8_t> A(16);
for(size_t i = 0; i != 8; ++i)
A[i] = key[i];
copy_mem(R.data(), &key[8], key.size() - 8);
for(size_t j = 0; j <= 5; ++j)
{
for(size_t i = n; i != 0; --i)
{
const uint32_t t = static_cast<uint32_t>((5 - j) * n + i);
uint8_t t_buf[4] = { 0 };
store_be(t, t_buf);
xor_buf(&A[4], t_buf, 4);
copy_mem(&A[8], &R[8*(i-1)], 8);
aes->decrypt(A.data());
copy_mem(&R[8*(i-1)], &A[8], 8);
}
}
if(load_be<uint64_t>(A.data(), 0) != 0xA6A6A6A6A6A6A6A6)
throw Integrity_Failure("NIST key unwrap failed");
return R;
}
示例11: rfc3394_keywrap
secure_vector<uint8_t> rfc3394_keywrap(const secure_vector<uint8_t>& key,
const SymmetricKey& kek)
{
if(key.size() % 8 != 0)
throw Invalid_Argument("Bad input key size for NIST key wrap");
if(kek.size() != 16 && kek.size() != 24 && kek.size() != 32)
throw Invalid_Argument("Bad KEK length " + std::to_string(kek.size()) + " for NIST key wrap");
const std::string cipher_name = "AES-" + std::to_string(8*kek.size());
std::unique_ptr<BlockCipher> aes(BlockCipher::create_or_throw(cipher_name));
aes->set_key(kek);
const size_t n = key.size() / 8;
secure_vector<uint8_t> R((n + 1) * 8);
secure_vector<uint8_t> A(16);
for(size_t i = 0; i != 8; ++i)
A[i] = 0xA6;
copy_mem(&R[8], key.data(), key.size());
for(size_t j = 0; j <= 5; ++j)
{
for(size_t i = 1; i <= n; ++i)
{
const uint32_t t = static_cast<uint32_t>((n * j) + i);
copy_mem(&A[8], &R[8*i], 8);
aes->encrypt(A.data());
copy_mem(&R[8*i], &A[8], 8);
uint8_t t_buf[4] = { 0 };
store_be(t, t_buf);
xor_buf(&A[4], t_buf, 4);
}
}
copy_mem(R.data(), A.data(), 8);
return R;
}
示例12: CPPUNIT_ASSERT
void ECDHTests::testDeriveKnownVector()
{
ECPublicKey* pubKeya = (ECPublicKey*) ecdh->newPublicKey();
ECPublicKey* pubKeyb = (ECPublicKey*) ecdh->newPublicKey();
ECPrivateKey* privKeya = (ECPrivateKey*) ecdh->newPrivateKey();
ECPrivateKey* privKeyb = (ECPrivateKey*) ecdh->newPrivateKey();
// Reconstruct public and private key for Alice
ByteString ec = "06082a8648ce3d030107"; // X9.62 prime256v1
ByteString da = "c88f01f510d9ac3f70a292daa2316de544e9aab8afe84049c62a9c57862d1433";
// add 04 (ASN_String) <len+1> 04 (UNCOMPRESSED) in front!
ByteString qa = "044104dad0b65394221cf9b051e1feca5787d098dfe637fc90b9ef945d0c37725811805271a0461cdb8252d61f1c456fa3e59ab1f45b33accf5f58389e0577b8990bb3";
pubKeya->setEC(ec);
pubKeya->setQ(qa);
privKeya->setEC(ec);
privKeya->setD(da);
// Reconstruct public and private key for Bob
ByteString db = "c6ef9c5d78ae012a011164acb397ce2088685d8f06bf9be0b283ab46476bee53";
ByteString qb = "044104d12dfb5289c8d4f81208b70270398c342296970a0bccb74c736fc7554494bf6356fbf3ca366cc23e8157854c13c58d6aac23f046ada30f8353e74f33039872ab";
pubKeyb->setEC(ec);
pubKeyb->setQ(qb);
privKeyb->setEC(ec);
privKeyb->setD(db);
// Test
ByteString expected = "d6840f6b42f6edafd13116e0e12565202fef8e9ece7dce03812464d04b9442de";
SymmetricKey* sa;
CPPUNIT_ASSERT(ecdh->deriveKey(&sa, pubKeya, privKeyb));
CPPUNIT_ASSERT(sa->getKeyBits() == expected);
SymmetricKey* sb;
CPPUNIT_ASSERT(ecdh->deriveKey(&sb, pubKeyb, privKeya));
CPPUNIT_ASSERT(sb->getKeyBits() == expected);
ecdh->recyclePublicKey(pubKeya);
ecdh->recyclePublicKey(pubKeyb);
ecdh->recyclePrivateKey(privKeya);
ecdh->recyclePrivateKey(privKeyb);
ecdh->recycleSymmetricKey(sa);
ecdh->recycleSymmetricKey(sb);
}
示例13: CPPUNIT_ASSERT
void DHTests::testDerivation()
{
AsymmetricKeyPair* kpa;
AsymmetricKeyPair* kpb;
// Key sizes to test
std::vector<size_t> keySizes;
#ifdef WITH_FIPS
keySizes.push_back(1024);
#else
keySizes.push_back(512);
//keySizes.push_back(768);
//keySizes.push_back(1024);
#endif
for (std::vector<size_t>::iterator k = keySizes.begin(); k != keySizes.end(); k++)
{
// Generate parameters
AsymmetricParameters* p;
CPPUNIT_ASSERT(dh->generateParameters(&p, (void*) *k));
// Generate key-pairs
CPPUNIT_ASSERT(dh->generateKeyPair(&kpa, p));
CPPUNIT_ASSERT(dh->generateKeyPair(&kpb, p));
// Derive secrets
SymmetricKey* sa;
CPPUNIT_ASSERT(dh->deriveKey(&sa, kpb->getPublicKey(), kpa->getPrivateKey()));
SymmetricKey* sb;
CPPUNIT_ASSERT(dh->deriveKey(&sb, kpa->getPublicKey(), kpb->getPrivateKey()));
// Must be the same
CPPUNIT_ASSERT(sa->getKeyBits() == sb->getKeyBits());
// Clean up
dh->recycleSymmetricKey(sa);
dh->recycleSymmetricKey(sb);
dh->recycleKeyPair(kpa);
dh->recycleKeyPair(kpb);
dh->recycleParameters(p);
}
}
示例14: CPPUNIT_ASSERT
void EDDSATests::testDeriveKnownVector()
{
EDPublicKey* pubKeya = (EDPublicKey*) eddsa->newPublicKey();
EDPublicKey* pubKeyb = (EDPublicKey*) eddsa->newPublicKey();
EDPrivateKey* privKeya = (EDPrivateKey*) eddsa->newPrivateKey();
EDPrivateKey* privKeyb = (EDPrivateKey*) eddsa->newPrivateKey();
// Reconstruct public and private key for Alice
ByteString ec = "06032b656e"; // x25519
ByteString ka = "77076d0a7318a57d3c16c17251b26645df4c2f87ebc0992ab177fba51db92c2a";
ByteString aa = "04208520f0098930a754748b7ddcb43ef75a0dbf3a0d26381af4eba4a98eaa9b4e6a";
pubKeya->setEC(ec);
pubKeya->setA(aa);
privKeya->setEC(ec);
privKeya->setK(ka);
// Reconstruct public and private key for Bob
ByteString kb = "5dab087e624a8a4b79e17f8b83800ee66f3bb1292618b6fd1c2f8b27ff88e0eb";
ByteString ab = "0420de9edb7d7b7dc1b4d35b61c2ece435373f8343c85b78674dadfc7e146f882b4f";
pubKeyb->setEC(ec);
pubKeyb->setA(ab);
privKeyb->setEC(ec);
privKeyb->setK(kb);
// Test
ByteString expected = "4a5d9d5ba4ce2de1728e3bf480350f25e07e21c947d19e3376f09b3c1e161742";
SymmetricKey* sa;
CPPUNIT_ASSERT(eddsa->deriveKey(&sa, pubKeya, privKeyb));
CPPUNIT_ASSERT(sa->getKeyBits() == expected);
SymmetricKey* sb;
CPPUNIT_ASSERT(eddsa->deriveKey(&sb, pubKeyb, privKeya));
CPPUNIT_ASSERT(sb->getKeyBits() == expected);
eddsa->recyclePublicKey(pubKeya);
eddsa->recyclePublicKey(pubKeyb);
eddsa->recyclePrivateKey(privKeya);
eddsa->recyclePrivateKey(privKeyb);
eddsa->recycleSymmetricKey(sa);
eddsa->recycleSymmetricKey(sb);
}
示例15: generateKey
bool SymmetricAlgorithm::generateKey(SymmetricKey& key, RNG* rng /* = NULL */)
{
if (rng == NULL)
{
return false;
}
if (key.getBitLen() == 0)
{
return false;
}
ByteString keyBits;
if (!rng->generateRandom(keyBits, key.getBitLen()/8))
{
return false;
}
return key.setKeyBits(keyBits);
}