Virgil Security Crypto library  2.1.2
Public Member Functions | List of all members
virgil::crypto::foundation::VirgilSymmetricCipher Class Reference

Provides symmetric ciphers algorithms. More...

#include <VirgilSymmetricCipher.h>

Inheritance diagram for virgil::crypto::foundation::VirgilSymmetricCipher:
virgil::crypto::foundation::asn1::VirgilAsn1Compatible

Public Types

Additional types
enum  Padding {
  Padding::PKCS7 = 0, Padding::OneAndZeros, Padding::ZerosAndLen, Padding::Zeros,
  Padding::None
}
 Padding modes for the symmetric cipher. More...
 
enum  Algorithm { Algorithm::AES_128_CBC, Algorithm::AES_128_GCM, Algorithm::AES_256_CBC, Algorithm::AES_256_GCM }
 

Public Member Functions

 VirgilSymmetricCipher ()
 Create object with undefined algorithm. More...
 
 VirgilSymmetricCipher (Algorithm algorithm)
 Create object with specific algorithm type.
 
 VirgilSymmetricCipher (const std::string &name)
 Create object with given algorithm name. More...
 
 VirgilSymmetricCipher (const char *name)
 Create object with given algorithm name. More...
 
Info
std::string name () const
 Returns the name of the given cipher, as a string.
 
size_t blockSize () const
 Returns the block size of the current cipher. More...
 
size_t ivSize () const
 Returns the size of the cipher's IV in octets.
 
size_t keySize () const
 Returns the key length of the cipher. More...
 
size_t keyLength () const
 Returns the key length of the cipher. More...
 
size_t authTagLength () const
 Returns the authentication tag length of the cipher. More...
 
bool isEncryptionMode () const
 Returns true if cipher is in the encryption mode.
 
bool isDecryptionMode () const
 Returns true if cipher is in the decryption mode.
 
bool isAuthMode () const
 Returns true if cipher is configured to support authenticated encryption and decryption.
 
bool isSupportPadding () const
 Returns true if cipher support padding.
 
VirgilByteArray iv () const
 Return cipher IV, or NONCE_COUNTER for CTR-mode ciphers.
 
Configuration
void setEncryptionKey (const virgil::crypto::VirgilByteArray &key)
 Configures encryption key. More...
 
void setDecryptionKey (const virgil::crypto::VirgilByteArray &key)
 Configures decryption key. More...
 
void setPadding (VirgilSymmetricCipher::Padding padding)
 Defines padding mode. More...
 
void setIV (const virgil::crypto::VirgilByteArray &iv)
 Configures the initialization vector.
 
void setAuthData (const virgil::crypto::VirgilByteArray &authData)
 Add additional data (for AEAD ciphers). More...
 
void reset ()
 Finish preparation before encryption / decryption.
 
void clear ()
 Clear all configuration settings. More...
 
Generic Encryption / Decryption
virgil::crypto::VirgilByteArray crypt (const virgil::crypto::VirgilByteArray &input, const virgil::crypto::VirgilByteArray &iv)
 Generic all-in-one encryption / decryption. More...
 
Sequence Encryption / Decryption
virgil::crypto::VirgilByteArray update (const virgil::crypto::VirgilByteArray &input)
 Generic cipher update function. More...
 
virgil::crypto::VirgilByteArray finish ()
 Cipher finalization method. More...
 
VirgilAsn1Compatible implementation
size_t asn1Write (virgil::crypto::foundation::asn1::VirgilAsn1Writer &asn1Writer, size_t childWrittenBytes=0) const override
 Write object state to the writer. More...
 
void asn1Read (virgil::crypto::foundation::asn1::VirgilAsn1Reader &asn1Reader) override
 Read object state from the reader. More...
 
- Public Member Functions inherited from virgil::crypto::foundation::asn1::VirgilAsn1Compatible
virgil::crypto::VirgilByteArray toAsn1 () const
 Save object state to the ASN.1 structure.
 
void fromAsn1 (const virgil::crypto::VirgilByteArray &asn1)
 Restore object state from the ASN.1 structure.
 
virtual ~VirgilAsn1Compatible () noexcept
 Polymorphic destructor.
 

Additional Inherited Members

- Protected Member Functions inherited from virgil::crypto::foundation::asn1::VirgilAsn1Compatible
virtual void checkRequiredField (const VirgilByteArray &param) const
 If given parameter is empty exception will be thrown. More...
 

Detailed Description

Provides symmetric ciphers algorithms.

Member Enumeration Documentation

Enumerates possible Symmetric Cipher algorithms.

Enumerator
AES_128_CBC 

Cipher algorithm: AES-128, mode: CBC.

AES_128_GCM 

Cipher algorithm: AES-128, mode: GCM.

AES_256_CBC 

Cipher algorithm: AES-256, mode: CBC.

AES_256_GCM 

Cipher algorithm: AES-256, mode: GCM.

Padding modes for the symmetric cipher.

Enumerator
PKCS7 

Padding mode: PKCS7 padding (default)

OneAndZeros 

Padding mode: ISO/IEC 7816-4 padding.

ZerosAndLen 

Padding mode: ANSI X.923 padding.

Zeros 

Padding mode: zero padding (not reversible!)

None 

Padding mode: never pad (full blocks only)

Constructor & Destructor Documentation

virgil::crypto::foundation::VirgilSymmetricCipher::VirgilSymmetricCipher ( )

Create object with undefined algorithm.

Warning
SHOULD be used in conjunction with VirgilAsn1Compatible interface, i.e. VirgilSymmetricCipher cipher; cipher.fromAsn1(asn1);
virgil::crypto::foundation::VirgilSymmetricCipher::VirgilSymmetricCipher ( const std::string &  name)
explicit

Create object with given algorithm name.

Note
Name format: {ALG}-{LEN}-{MODE}, i.e AES-256-GCM.
virgil::crypto::foundation::VirgilSymmetricCipher::VirgilSymmetricCipher ( const char *  name)
explicit

Create object with given algorithm name.

Note
Name format: {ALG}-{LEN}-{MODE}, i.e AES-256-GCM.

Member Function Documentation

void virgil::crypto::foundation::VirgilSymmetricCipher::asn1Read ( virgil::crypto::foundation::asn1::VirgilAsn1Reader asn1Reader)
overridevirtual

Read object state from the reader.

Parameters
asn1Readerreader payloaded with ASN.1 to be read.

Implements virgil::crypto::foundation::asn1::VirgilAsn1Compatible.

size_t virgil::crypto::foundation::VirgilSymmetricCipher::asn1Write ( virgil::crypto::foundation::asn1::VirgilAsn1Writer asn1Writer,
size_t  childWrittenBytes = 0 
) const
overridevirtual

Write object state to the writer.

Parameters
asn1Writerwriter that should be payloaded by subclasses.
childWrittenBytescount of bytes that was written by subclasses.
Returns
Writen bytes count.

Implements virgil::crypto::foundation::asn1::VirgilAsn1Compatible.

size_t virgil::crypto::foundation::VirgilSymmetricCipher::authTagLength ( ) const

Returns the authentication tag length of the cipher.

Returns
tag length, in octets.
size_t virgil::crypto::foundation::VirgilSymmetricCipher::blockSize ( ) const

Returns the block size of the current cipher.

Returns
block size, in octets.
void virgil::crypto::foundation::VirgilSymmetricCipher::clear ( )

Clear all configuration settings.

Note
This method SHOULD be used if class instance was used for encryption and then will be used for decryption, and vice versa.
virgil::crypto::VirgilByteArray virgil::crypto::foundation::VirgilSymmetricCipher::crypt ( const virgil::crypto::VirgilByteArray input,
const virgil::crypto::VirgilByteArray iv 
)

Generic all-in-one encryption / decryption.

Encrypts or decrypts given data.

Parameters
input- data to be encrypted / decrypted.
iv- initialization vector.
Returns
Encrypted or decrypted bytes (rely on the current mode).
virgil::crypto::VirgilByteArray virgil::crypto::foundation::VirgilSymmetricCipher::finish ( )

Cipher finalization method.

If data still needs to be flushed from an incomplete block, data contained within it will be padded with the size of the last block, and will be returned.

Returns
Encrypted or decrypted bytes (rely on the current mode).
size_t virgil::crypto::foundation::VirgilSymmetricCipher::keyLength ( ) const

Returns the key length of the cipher.

Returns
key length, in octets.
size_t virgil::crypto::foundation::VirgilSymmetricCipher::keySize ( ) const

Returns the key length of the cipher.

Returns
key length, in bits.
void virgil::crypto::foundation::VirgilSymmetricCipher::setAuthData ( const virgil::crypto::VirgilByteArray authData)

Add additional data (for AEAD ciphers).

Note
Currently only supported with GCM.
Must be called before reset().
See also
isAuthMode()
void virgil::crypto::foundation::VirgilSymmetricCipher::setDecryptionKey ( const virgil::crypto::VirgilByteArray key)

Configures decryption key.

Configures cipher to be used in decryption mode with given key.

Warning
Only one key CAN be set.
void virgil::crypto::foundation::VirgilSymmetricCipher::setEncryptionKey ( const virgil::crypto::VirgilByteArray key)

Configures encryption key.

Configures cipher to be used in encryption mode with given key.

Warning
Only one key CAN be set.
void virgil::crypto::foundation::VirgilSymmetricCipher::setPadding ( VirgilSymmetricCipher::Padding  padding)

Defines padding mode.

Default value is PKCS7.

Note
This parameter is used only for cipher modes that use padding.
See also
isSupportPadding()
virgil::crypto::VirgilByteArray virgil::crypto::foundation::VirgilSymmetricCipher::update ( const virgil::crypto::VirgilByteArray input)

Generic cipher update function.

Encrypts or decrypts given data. Writes as many block size'd blocks of data as possible to output. Any data that cannot be written immediately will either be added to the next block, or flushed when finish is called.

Returns
Encrypted or decrypted bytes (rely on the current mode).

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