static void generateAesKeys(Blob& encryptionKeyBlob, Blob& decryptionKeyBlob) { AesKeyParams params; DecryptKey memberDecryptKey = AesAlgorithm::generateKey(params); decryptionKeyBlob = memberDecryptKey.getKeyBits(); EncryptKey memberEncryptKey = AesAlgorithm::deriveEncryptKey(decryptionKeyBlob); encryptionKeyBlob = memberEncryptKey.getKeyBits(); }
static void generateRsaKeys(Blob& encryptionKeyBlob, Blob& decryptionKeyBlob) { RsaKeyParams params; DecryptKey decryptKey = RsaAlgorithm::generateKey(params); decryptionKeyBlob = decryptKey.getKeyBits(); EncryptKey encryptKey = RsaAlgorithm::deriveEncryptKey(decryptionKeyBlob); encryptionKeyBlob = encryptKey.getKeyBits(); }
void GroupManager::generateKeyPair(Blob& privateKeyBlob, Blob& publicKeyBlob) { RsaKeyParams params(keySize_); DecryptKey privateKey = RsaAlgorithm::generateKey(params); privateKeyBlob = privateKey.getKeyBits(); EncryptKey publicKey = RsaAlgorithm::deriveEncryptKey(privateKeyBlob); publicKeyBlob = publicKey.getKeyBits(); }
TEST_F(TestRsaAlgorithm, EncryptionDecryption) { EncryptParams encryptParams(ndn_EncryptAlgorithmType_RsaOaep, 0); ptr_lib::shared_ptr<vector<uint8_t> > privateKeyBuffer(new vector<uint8_t>()); fromBase64(PRIVATE_KEY, *privateKeyBuffer); Blob privateKeyBlob(privateKeyBuffer, false); ptr_lib::shared_ptr<vector<uint8_t> > publicKeyBuffer(new vector<uint8_t>()); fromBase64(PUBLIC_KEY, *publicKeyBuffer); Blob publicKeyBlob(publicKeyBuffer, false); DecryptKey decryptKey(privateKeyBlob); EncryptKey encryptKey = RsaAlgorithm::deriveEncryptKey(decryptKey.getKeyBits()); Blob encodedPublic = publicKeyBlob; Blob derivedPublicKey = encryptKey.getKeyBits(); ASSERT_TRUE(encodedPublic.equals(derivedPublicKey)); Blob plainBlob(PLAINTEXT, sizeof(PLAINTEXT)); Blob encryptBlob = RsaAlgorithm::encrypt (encryptKey.getKeyBits(), plainBlob, encryptParams); Blob receivedBlob = RsaAlgorithm::decrypt (decryptKey.getKeyBits(), encryptBlob, encryptParams); ASSERT_TRUE(plainBlob.equals(receivedBlob)); Blob cipherBlob(CIPHERTEXT_OAEP, sizeof(CIPHERTEXT_OAEP)); Blob decryptedBlob = RsaAlgorithm::decrypt (decryptKey.getKeyBits(), cipherBlob, encryptParams); ASSERT_TRUE(plainBlob.equals(decryptedBlob)); // Now test RsaPkcs. encryptParams = EncryptParams(ndn_EncryptAlgorithmType_RsaPkcs, 0); encryptBlob = RsaAlgorithm::encrypt (encryptKey.getKeyBits(), plainBlob, encryptParams); receivedBlob = RsaAlgorithm::decrypt (decryptKey.getKeyBits(), encryptBlob, encryptParams); ASSERT_TRUE(plainBlob.equals(receivedBlob)); cipherBlob = Blob(CIPHERTEXT_PKCS, sizeof(CIPHERTEXT_PKCS)); decryptedBlob = RsaAlgorithm::decrypt (decryptKey.getKeyBits(), cipherBlob, encryptParams); ASSERT_TRUE(plainBlob.equals(decryptedBlob)); }
TEST_F(TestAesAlgorithm, KeyGeneration) { AesKeyParams keyParams(128); DecryptKey decryptKey = AesAlgorithm::generateKey(keyParams); EncryptKey encryptKey = AesAlgorithm::deriveEncryptKey(decryptKey.getKeyBits()); Blob plainBlob(PLAINTEXT, sizeof(PLAINTEXT)); // Encrypt/decrypt data in AES_CBC with auto-generated IV. EncryptParams encryptParams(ndn_EncryptAlgorithmType_AesCbc, 16); Blob cipherBlob = AesAlgorithm::encrypt (encryptKey.getKeyBits(), plainBlob, encryptParams); Blob receivedBlob = AesAlgorithm::decrypt (decryptKey.getKeyBits(), cipherBlob, encryptParams); ASSERT_TRUE(receivedBlob.equals(plainBlob)); }
TEST_F(TestAesAlgorithm, EncryptionDecryption) { EncryptParams encryptParams(ndn_EncryptAlgorithmType_AesEcb, 16); Blob key(KEY, sizeof(KEY)); DecryptKey decryptKey(key); EncryptKey encryptKey = AesAlgorithm::deriveEncryptKey(decryptKey.getKeyBits()); // Check key loading and key derivation. ASSERT_TRUE(encryptKey.getKeyBits().equals(key)); ASSERT_TRUE(decryptKey.getKeyBits().equals(key)); Blob plainBlob(PLAINTEXT, sizeof(PLAINTEXT)); // Encrypt data in AES_ECB. Blob cipherBlob = AesAlgorithm::encrypt (encryptKey.getKeyBits(), plainBlob, encryptParams); ASSERT_TRUE(cipherBlob.equals(Blob(CIPHERTEXT_ECB, sizeof(CIPHERTEXT_ECB)))); // Decrypt data in AES_ECB. Blob receivedBlob = AesAlgorithm::decrypt (decryptKey.getKeyBits(), cipherBlob, encryptParams); ASSERT_TRUE(receivedBlob.equals(plainBlob)); // Encrypt/decrypt data in AES_CBC with auto-generated IV. encryptParams.setAlgorithmType(ndn_EncryptAlgorithmType_AesCbc); cipherBlob = AesAlgorithm::encrypt (encryptKey.getKeyBits(), plainBlob, encryptParams); receivedBlob = AesAlgorithm::decrypt (decryptKey.getKeyBits(), cipherBlob, encryptParams); ASSERT_TRUE(receivedBlob.equals(plainBlob)); // Encrypt data in AES_CBC with specified IV. Blob initialVector(INITIAL_VECTOR, sizeof(INITIAL_VECTOR)); encryptParams.setInitialVector(initialVector); cipherBlob = AesAlgorithm::encrypt (encryptKey.getKeyBits(), plainBlob, encryptParams); ASSERT_TRUE(cipherBlob.equals(Blob(CIPHERTEXT_CBC_IV, sizeof(CIPHERTEXT_CBC_IV)))); // Decrypt data in AES_CBC with specified IV. receivedBlob = AesAlgorithm::decrypt (decryptKey.getKeyBits(), cipherBlob, encryptParams); ASSERT_TRUE(receivedBlob.equals(plainBlob)); }