Virgil Security Crypto library  2.6.3
Public Types | List of all members
virgil::crypto::VirgilTinyCipher Class Reference

This class aim is to minimize encryption output. More...

#include <VirgilTinyCipher.h>

Public Types

enum  PackageSize { PackageSize_Min = 113, PackageSize_Short_SMS = 120, PackageSize_Long_SMS = 1200 }
 Constants that represents maximum number of bytes in one package. More...
 

Public Member Functions

Configuration
 VirgilTinyCipher (size_t packageSize=PackageSize_Short_SMS)
 Init cipher with given maximum package size. More...
 
void reset ()
 Prepare cipher for the next encryption. More...
 
Encryption
void encrypt (const VirgilByteArray &data, const VirgilByteArray &recipientPublicKey)
 Encrypt data with given public key. More...
 
void encryptAndSign (const VirgilByteArray &data, const VirgilByteArray &recipientPublicKey, const VirgilByteArray &senderPrivateKey, const VirgilByteArray &senderPrivateKeyPassword=VirgilByteArray())
 Encrypt data with given public key and sign package. More...
 
size_t getPackageCount () const
 Return total package count. More...
 
VirgilByteArray getPackage (size_t index) const
 Return specific package. More...
 
Decryption
void addPackage (const VirgilByteArray &package)
 Add package. More...
 
bool isPackagesAccumulated () const
 Define whether all packages was accumulated or not. More...
 
VirgilByteArray decrypt (const VirgilByteArray &recipientPrivateKey, const VirgilByteArray &recipientPrivateKeyPassword=VirgilByteArray())
 Decrypt accumulated packages. More...
 
VirgilByteArray verifyAndDecrypt (const VirgilByteArray &senderPublicKey, const VirgilByteArray &recipientPrivateKey, const VirgilByteArray &recipientPrivateKeyPassword=VirgilByteArray())
 Verify accumulated packages and then decrypt it. More...
 

Detailed Description

This class aim is to minimize encryption output.

Motivation
If encrypted data is transmitted over GSM module, it very important that encryption output was as small as possible.
Solution
Throw out crypto agility and transfer minimum public information required for decryption.
Pros
  • Tiny messages.
Cons
  • Crypto agility is not included in the message, so encrypted messages should not be stored for long term.
Details
During encryption ciper packs encrypted message and service information to the set of packages, which length is limited by maximim package length.
Restrictions
Currently supported public/private keys:
  • Curve25519
Minimum package length:
  • 113 bytes
See also
VirgilTinyCipher(size_t packageSize)
VirgilTinyCipher::PackageSize
Example - Encrypt
VirgilTinyCipher tinyCipher;
VirgilByteArray data = VirgilByteArrayUtils::stringToBytes("String to be encrypted");
tinyCipher.encrypt(data, recipientPublicKey);
for (size_t i = 0; i < tinyCipher.getPackageCount(); ++i) {
VirgilByteArray package = tinyCipher.getPackage(i);
// Send package to the recipient
}
Example - Encrypt and Sign
VirgilTinyCipher tinyCipher;
VirgilByteArray data = VirgilByteArrayUtils::stringToBytes("String to be encrypted");
tinyCipher.encryptAndSign(data, recipientPublicKey, senderPrivateKey, senderPrivateKeyPassword);
for (size_t i = 0; i < tinyCipher.getPackageCount(); ++i) {
VirgilByteArray package = tinyCipher.getPackage(i);
// Send package to the recipient
}
Example - Decrypt
tinyCipher.addPackage(receivedPackage);
if (tinyCipher.isPackagesAccumulated()) {
decryptedData = tinyCipher.decrypt(recipientPrivateKey, recipientPrivateKeyPassword);
}
Example - Verify and Decrypt
tinyCipher.addPackage(receivedPackage);
if (tinyCipher.isPackagesAccumulated()) {
decryptedData = tinyCipher.verifyAndDecrypt(senderPublicKey, recipientPrivateKey, recipientPrivateKeyPassword);
}

Member Enumeration Documentation

Constants that represents maximum number of bytes in one package.

Note
Text representation is 4/3 bigger, i.e 120 * 4/3 = 160 - for short sms.
Enumerator
PackageSize_Min 

Min.

PackageSize_Short_SMS 

Short SMS.

PackageSize_Long_SMS 

Long SMS.

Constructor & Destructor Documentation

virgil::crypto::VirgilTinyCipher::VirgilTinyCipher ( size_t  packageSize = PackageSize_Short_SMS)
explicit

Init cipher with given maximum package size.

Parameters
packageSize- maximum number of bytes in one package
Exceptions
std::logic_error- if given packageSize less then minimum value
See also
VirgilTinyCipher::PackageSize

Member Function Documentation

void virgil::crypto::VirgilTinyCipher::addPackage ( const VirgilByteArray package)

Add package.

Accumulate packages before decryption.

Parameters
package- package to be accumulated
See also
To determine that all packages are received call function isPackagesAccumulated().
Exceptions
VirgilCryptoException- if given package is malformed
VirgilByteArray virgil::crypto::VirgilTinyCipher::decrypt ( const VirgilByteArray recipientPrivateKey,
const VirgilByteArray recipientPrivateKeyPassword = VirgilByteArray() 
)

Decrypt accumulated packages.

Parameters
recipientPrivateKey- recipient private key
recipientPrivateKeyPassword- recipient private key password (optional)
Returns
Decrypted data.
Exceptions
VirgilCryptoException- if public key or private key are not supported
VirgilCryptoException- if data is malformed
void virgil::crypto::VirgilTinyCipher::encrypt ( const VirgilByteArray data,
const VirgilByteArray recipientPublicKey 
)

Encrypt data with given public key.

Parameters
data- data to be encrypted
recipientPublicKey- recipient public key
See also
getPackage()
Exceptions
VirgilCryptoException- if public key is not supported
void virgil::crypto::VirgilTinyCipher::encryptAndSign ( const VirgilByteArray data,
const VirgilByteArray recipientPublicKey,
const VirgilByteArray senderPrivateKey,
const VirgilByteArray senderPrivateKeyPassword = VirgilByteArray() 
)

Encrypt data with given public key and sign package.

Parameters
data- data to be encrypted
recipientPublicKey- recipient public key
senderPrivateKey- sender private key
senderPrivateKeyPassword- sender private key password (optional)
See also
Related links can be found in the encrypt() function description.
Exceptions
VirgilCryptoException- if public key is not supported
VirgilCryptoException- if private key is not supported
VirgilCryptoException- if private key password is wrong
VirgilByteArray virgil::crypto::VirgilTinyCipher::getPackage ( size_t  index) const

Return specific package.

Return package with specific index.

Parameters
index- package index
Note
Packages are available when encryption process is completed.
Exceptions
std::out_of_range- if package with given index is not exists
size_t virgil::crypto::VirgilTinyCipher::getPackageCount ( ) const

Return total package count.

Note
Package count is known when encryption process is completed.
See also
getPackage()
bool virgil::crypto::VirgilTinyCipher::isPackagesAccumulated ( ) const

Define whether all packages was accumulated or not.

Returns
true - if all packages was successfully accumulated, false otherwise
See also
addPackage()
void virgil::crypto::VirgilTinyCipher::reset ( )

Prepare cipher for the next encryption.

Note
SHOULD be used before the next encryption.
VirgilByteArray virgil::crypto::VirgilTinyCipher::verifyAndDecrypt ( const VirgilByteArray senderPublicKey,
const VirgilByteArray recipientPrivateKey,
const VirgilByteArray recipientPrivateKeyPassword = VirgilByteArray() 
)

Verify accumulated packages and then decrypt it.

Parameters
senderPublicKey- sender public key
recipientPrivateKey- recipient private key
recipientPrivateKeyPassword- recipient private key password (optional)
Exceptions
VirgilCryptoException- if public key or private key are not supported
VirgilCryptoException- if data is malformed

The documentation for this class was generated from the following file: