Skip to content

Crypto functions

Sudheer edited this page Mar 11, 2023 · 9 revisions

Set of cryptographic utility functions, built on top of Crypto module of Poco

Loading the library

local evl_crypto = (package.loadlib('libevlcrypto.so','luaopen_libevlcrypto'))();

Hashing and MAC functions

evl_crypto.s_sha1_hash(text, [salt]);

Generates SHA1 digest for the given text data and optional salt

Parameters:
    text: string
    salt: string, optional
Return:
    digest: string

ERROR:
    Exceptions are thrown upon error, which can be caught via mechanism of pcall/xpcall

evl_crypto.s_sha256_hash(text, [salt]);

Generates SHA256 digest for the given text data and optional salt

Parameters:
    text: string
    salt: string, optional
Return:
    digest: string

ERROR:
    Exceptions are thrown upon error, which can be caught via mechanism of pcall/xpcall

evl_crypto.s_sha384_hash(text, [salt]);

Generates SHA384 digest for the given text data and optional salt

Parameters:
    text: string
    salt: string, optional
Return:
    digest: string

ERROR:
    Exceptions are thrown upon error, which can be caught via mechanism of pcall/xpcall

evl_crypto.s_sha512_hash(text, [salt]);

Generates SHA512 digest for the given text data and optional salt

Parameters:
    text: string
    salt: string, optional
Return:
    digest: string

ERROR:
    Exceptions are thrown upon error, which can be caught via mechanism of pcall/xpcall

evl_crypto.hmac_digest(digest_name, text, key, raw);

Generates cryptofraphic digest for the given text based, the digest name can be passed as a preference
further passed ke is used in the process of digest generation.

Parameters:
    digest_name: string, valid cryptographic digest name, like SHA256, SHA512
    text: strin
    key: Key used in generation
    raw: boolean, if true the raw digest is returned as a string, else HEX encoded digest is returned
Return:
    digest: string, either raw or hex

ERROR:
    Exceptions are thrown upon error, which can be caught via mechanism of pcall/xpcall

Symmetric key management

evl_crypto.generate_symmetric_key(key_name);

Generates a symmetric key for encryption/decryption based on input key_name
(e.g. AES256, AES128, 3DES, DES etc...);

Parameters:
    key_name: string
Return:
    symmetric_cipher_key: handle to key

ERROR:
    Exceptions are thrown upon error, which can be caught via mechanism of pcall/xpcall

evl_crypto.generate_aes_key(key_size);

Generates a symmetric key for encryption/decryption based on input key_size

Parameters:
    key_size: number, 128, 256 etc...
Return:
    none
    symmetric_cipher_key: handle to key

ERROR:
    Exceptions are thrown upon error, which can be caught via mechanism of pcall/xpcall

RSA Key management

evl_crypto.generate_rsa_key_pair(key_length);

Returns a (public, private) kep-pair given the key length.

Parameters:
    key_length: integer, one of 512, 1024, 2048 or 4096
Return:
    rsa_key: handle to rsa_key object

ERROR:
    Exceptions are thrown upon error, which can be caught via mechanism of pcall/xpcall

evl_crypto.get_rsa_public_key(rsa_key);

Given the rsa_key object, returns the public key in string (base64) format

Parameters:
    rsa_key: handle to RSA key
Return:
    public_key: string

ERROR:
    Exceptions are thrown upon error, which can be caught via mechanism of pcall/xpcall

evl_crypto.get_rsa_private_key(rsa_key);

Given the rsa_key object, returns the private key in string (base64) format

Parameters:
    rsa_key: handle to RSA key
Return:
    private_key: string

ERROR:

evl_crypto.load_rsa_public_key(public_key);

Given a public key in string format (typically read from a file),
returns the rsa_key object

Parameters:
    public_key: string
Return:
    rsa_key: handle to rsa_key object

ERROR:
    Exceptions are thrown upon error, which can be caught via mechanism of pcall/xpcall

evl_crypto.load_rsa_private_key(private_key);

Given a private key in string format (typically read from a file),
returns the rsa_key object

Parameters:
    private_key: string
Return:
    rsa_key: handle to rsa_key object

ERROR:
    Exceptions are thrown upon error, which can be caught via mechanism of pcall/xpcall

RSA Encryption

evl_crypto.rsa_encrypt_symm_key(symm_key, rsa_key);

Encrypts a symmetric key with using the public key in the rsa_key object

Parameters:
    symm_key: symmetric key handle
    rsa_key: RSA key handle
Return:
    length: integer, Length of the encrypted buffer
    buffer: lightuserdata, Encripted buffer

ERROR:
    Exceptions are thrown upon error, which can be caught via mechanism of pcall/xpcall

RSA Decryption

evl_crypto.rsa_decrypt_enc_symm_key(cipher_text, rsa_key);

Decrypts the encrypted symmetric key, using the private key in the rsa_key

Parameters:
    cipher_text: userdata, Encrypted symmetric key
    rsa_key: userdata, handle to rsa_key object
Return:
    symm_key: userdata, symm_key object

ERROR:
    Exceptions are thrown upon error, which can be caught via mechanism of pcall/xpcall

evl_crypto.rsa_decrypt_udata_enc_symm_key(cipher_text, length, rsa_key);

Decrypts the encrypted symmetric key, using the private key in the rsa_key

Parameters:
    cipher_text: userdata, Encrypted symmetric key
    lenght: integer, length of the ciphertext buffer
    rsa_key: userdata, handle to rsa_key object
Return:
    symm_key: userdata, symm_key object

ERROR:
    Exceptions are thrown upon error, which can be caught via mechanism of pcall/xpcall

evl_crypto.rsa_decrypt_b64_enc_symm_key(b64_cipher_text, rsa_key);

Decrypts the encrypted symmetric key, using the private key in the rsa_key

Parameters:
    b64_cipher_text: string, Encrypted symmetric key in base64 format
    rsa_key: userdata, handle to rsa_key object
Return:
    symm_key: userdata, symm_key object

ERROR:
    Exceptions are thrown upon error, which can be caught via mechanism of pcall/xpcall

Symmetric encryption

evl_crypto.encrypt_text(plain_text, key);

Encrypts the given plain text, using the key

Parameters:
    plain_text: string
    key: cipher_key handle
Return:
    length: integer, size of the buffer
    cipher_text: userdata

ERROR:
    Exceptions are thrown upon error, which can be caught via mechanism of pcall/xpcall

Symmetric Decryption

evl_crypto.decrypt_cipher_text(cipher_text, key);

Decrypts the ciphertext using the key and returns plaintext

Parameters:
    cipher_text: userdata
    key: symm_key
Return:
    plain_text: string

ERROR:
    Exceptions are thrown upon error, which can be caught via mechanism of pcall/xpcall

evl_crypto.decrypt_udata_cipher_text(cipher_text, bufferlen, key);

Decrypts the ciphertext, in the form of a memory buffer along with length using the key and returns plaintext

Parameters:
    cipher_text: userdata
    bufferlen: integer, size of userdata
    key: symm_key
Return:
    plain_text: string

ERROR:
    Exceptions are thrown upon error, which can be caught via mechanism of pcall/xpcall

evl_crypto.decrypt_b64_cipher_text(b64_cipher_text, key);

Decrypts the input ciphertext n base64 format, using the key and returns plaintext

Parameters:
    b64_cipher_text: string
    key: symm_key
Return:
    plain_text: string

ERROR:
    Exceptions are thrown upon error, which can be caught via mechanism of pcall/xpcall
Clone this wiki locally