Secret Key Cryptography _AEAD_AES256GCM - Chewhern/ASodium GitHub Wiki

It's extremely important that you read the detailed documentation from libsodium.. If security is not a concern to you, you can ignore this warning I give to you.

Even though the code works but I don't recommend anyone to use the pre computation of hardware accelerated AES256 GCM.

There're lots of code that seem to be extra and I think I will be spending some free time to rework and erase.. the code

Mutual requirements for both variants (Prerequisites)

Initial Functions

public static Boolean IsAES256GCMAvailable()

Example Code

Boolean IsHAAES256GCMAvailable = SodiumSecretAeadAES256GCM.IsAES256GCMAvailable();
if(IsHAAES256GCMAvailable)
{
    //Do something..
}

Kindly ensures that your machine or current machine supports HA-AES256GCM else all the wrapper functions couldn't be called by you as a developer.

Mutual functions for both variants (Key Generation and Nonce Generation)

Initial Functions

public static Byte[] GenerateKey()
public static Byte[] GeneratePublicNonce()

Example Code

Byte[] Key = SodiumSecretAeadAES256GCM.GenerateKey();
Byte[] NoncePublic = SodiumSecretAeadAES256GCM.GeneratePublicNonce();

Hardware accelerated AES256GCM without pre-computation

This section of the documentation describes about using HA-AES256GCM without involving pre-computation.

Combined encryption and decryption

Initial Functions

public static Byte[] Encrypt(Byte[] Message, Byte[] NoncePublic, Byte[] Key, Byte[] AdditionalData = null, Byte[] NonceSecurity = null,Boolean ClearKey=false)
public static Byte[] Decrypt(Byte[] CipherText, Byte[] NoncePublic, Byte[] Key, Byte[] AdditionalData = null, Byte[] NonceSecurity = null,Boolean ClearKey=false)

Example Code

Byte[] Key = SodiumSecretAeadAES256GCM.GenerateKey();
Byte[] NoncePublic = SodiumSecretAeadAES256GCM.GeneratePublicNonce();
Byte[] Message = SodiumRNG.GetRandomBytes(32);
Byte[] CipherText = SodiumSecretAeadAES256GCM.Encrypt(Message,NoncePublic,Key,null,null,false);
Byte[] PlainText = SodiumSecretAeadAES256GCM.Decrypt(CipherText,NoncePublic,Key,null,null,false);

Detached encryption and decryption

Initial Functions

public static AES256GCMDetachedBox CreateDetachedBox(Byte[] Message, Byte[] NoncePublic, Byte[] Key, Byte[] NonceSecurity = null, Byte[] AdditionalData = null,Boolean ClearKey=false)
public static Byte[] OpenDetachedBox(AES256GCMDetachedBox MyDetachedBox, Byte[] NoncePublic, Byte[] Key, Byte[] AdditionalData = null, Byte[] NonceSecurity = null,Boolean ClearKey=false)
public static Byte[] OpenDetachedBox(Byte[] CipherText, Byte[] MAC, Byte[] NoncePublic, Byte[] Key, Byte[] AdditionalData = null, Byte[] NonceSecurity = null,Boolean ClearKey=false)

Example Code

Byte[] Key = SodiumSecretAeadAES256GCM.GenerateKey();
Byte[] NoncePublic = SodiumSecretAeadAES256GCM.GeneratePublicNonce();
Byte[] Message = SodiumRNG.GetRandomBytes(32);
Byte[] CipherText = new Byte[] {};
Byte[] PlainText = new Byte[] {};
AES256GCMDetachedBox MyDetachedBox = SodiumSecretAeadAES256GCM.CreateDetachedBox(Message,NoncePublic,Key,null,null,false);
PlainText = SodiumSecretAeadAES256GCM.OpenDetachedBox(MyDetachedBox,NoncePublic,Key,null,null,false);

Hardware accelerated AES256GCM with pre computation

Combined mode encryption and decryption

Initial functions

public static Byte[] InitializeState(Byte[] Key,Boolean ClearKey=false)
public static Byte[] Encrypt(Byte[] Message, Byte[] NoncePublic, Byte[] StateBytes, Byte[] AdditionalData = null, Byte[] NonceSecurity = null,Boolean ClearKey=false)
public static Byte[] Decrypt(Byte[] CipherText, Byte[] NoncePublic, Byte[] StateBytes, Byte[] AdditionalData = null, Byte[] NonceSecurity = null,Boolean ClearKey=false)

Example code

Byte[] Key = SodiumSecretAeadAES256GCM.GenerateKey();
Byte[] StateBytes = SodiumSecretAeadAES256GCMPC.InitializeState(Key);
Byte[] NoncePublic = SodiumSecretAeadAES256GCM.GeneratePublicNonce();
Byte[] Message = SodiumRNG.GetRandomBytes(32);
Byte[] CipherText = SodiumSecretAeadAES256GCMPC.Encrypt(Message,NoncePublic,StateBytes,null,null,false);
Byte[] PlainText = SodiumSecretAeadAES256GCMPC.Decrypt(CipherText,NoncePublic,StateBytes,null,null,false);

Detached encryption and decryption

Initial functions

public static AES256GCMDetachedBox CreateDetachedBox(Byte[] Message, Byte[] NoncePublic, Byte[] StateBytes, Byte[] NonceSecurity = null, Byte[] AdditionalData = null,Boolean ClearKey=false)
public static Byte[] OpenDetachedBox(AES256GCMDetachedBox MyDetachedBox, Byte[] NoncePublic, Byte[] StateBytes, Byte[] AdditionalData = null, Byte[] NonceSecurity = null,Boolean ClearKey=false)
public static Byte[] OpenDetachedBox(Byte[] CipherText, Byte[] MAC, Byte[] NoncePublic, Byte[] StateBytes, Byte[] AdditionalData = null, Byte[] NonceSecurity = null,Boolean ClearKey=false)

Example Code

Byte[] Key = SodiumSecretAeadAES256GCM.GenerateKey();
Byte[] StateBytes = SodiumSecretAeadAES256GCMPC.InitializeState(Key);
Byte[] NoncePublic = SodiumSecretAeadAES256GCM.GeneratePublicNonce();
Byte[] Message = SodiumRNG.GetRandomBytes(32);
AES256GCMDetachedBox MyDetachedBox = SodiumSecretAeadAES256GCMPC.CreateDetachedBox(Message,NoncePublic,StateBytes);
Byte[] PlainText = SodiumSecretAeadAES256GCMPC.OpenDetachedBox(MyDetachedBox,NoncePublic,StateBytes);