cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

HANA Custom Scalar Function for Column Encryption - Error Handling Invalid CESU-8

samson_moses
Explorer
0 Likes
702

Hello Experts ,

Though my query is basically to know if exception "Cesu8ConversionException Invalid CESU-8 sequence" can be handled in custom HANA scalar functions, I wanted to provide a context to my problem first (Apologies for the long explanation).

We have a requirement for Encryption / Decryption at Table Column level. Since HANA does not support Table-Column Level encryption, neither any built-in functions , I planned to work on a prototype by creating custom logic in my own scalar function at HANA DB level.

I referenced the blog for getting some directions : https://blogs.sap.com/2017/10/07/column-encryption-decryption-on-hana/#

Now I was able to build my simple encryption function by using the below high level logic.

Encryption: (Input = string data to be encrypted, Passkey = secure key for encryption/decryption ) - Example SAM_DO_ENCRYPT('Hello','MyKey')

  1. Convert the Input data which is unicode compliant to Binary using STRBIN . The converted value = 48656C6C6F (Lets call this as A).
  2. Get the irreversible Hash-Key for the passkey using HASH_MD5 (here I have added the logic to ensure the hash-key is big enough to handle the total length of the input Data). The Hash-key for 'MyKey' = 3D7D052A03(Lets call this as B)
  3. Do BitXOR of A with B. In this case, the result = 751869466C(Lets call this as C).
  4. The Value of C is what I am persisting in my DB-Table. Note, I am fine if this is not converted to string

Now, when I do decryption I simply pass the passkey and generate the Hash-key(B), which I do the X-OR with my saved encrypted value of (C). This would be giving me the binary data(A) of my original input and I can convert to string (No issue in conversion here as this binary data was for the unicode compliant input ). And this works fine when I pass the right pass key.

But if pass a wrong key(eg: 'My123' instead of 'MyKey'), the chances are the BitXOR(of C and B) would result binary representation of some non-unicode compliant characters. So when I try to convert this to string, it gives me the error "Cesu8ConversionException Invalid CESU-8 sequence". I want to avoid this error, by say, handling via Exit Handler in my scalar function.

If I add the below code in my scalar function, it says "feature not supported: The error code is not allowed in handler"

	DECLARE EXIT HANDLER FOR SQL_ERROR_CODE 609 --Error code CESU-Invalid 8
		BEGIN
			--ex_decrypted_data = 'Test';
		END;

Your inputs are very much needed here.

Regards,

Samson

Accepted Solutions (0)

Answers (0)