I'm new to AWS lambda and encryption.
I have a raw string which needs to be first encoded using SHA 512 and then further encoded using AES 256 with salt.
I am able to encrypt to SHA 512 using hashlib
enStr = 'amdkasmd' #some random string
res = hashlib.sha512(enStr.encode()).hexdigest()
I want to further encrypt this using AES 256 algorithm in CBC mode with salt (initialization vector)
I tried doing this using pycryptodomex
from base64 import b64encode, b64decode
import hashlib
from Cryptodome.Cipher import AES
import os
from Cryptodome.Random import get_random_bytes
def encrypt(plain_text, password):
# generate a random salt
salt = get_random_bytes(AES.block_size)
# use the Scrypt KDF to get a private key from the password
private_key = hashlib.scrypt(
password.encode(), salt=salt, n=2**14, r=8, p=1, dklen=32)
# create cipher config
cipher_config = AES.new(private_key, AES.MODE_GCM)
# return a dictionary with the encrypted text
cipher_text, tag = cipher_config.encrypt_and_digest(bytes(plain_text, 'utf-8'))
return {
'cipher_text': b64encode(cipher_text).decode('utf-8'),
'salt': b64encode(salt).decode('utf-8'),
'nonce': b64encode(cipher_config.nonce).decode('utf-8'),
'tag': b64encode(tag).decode('utf-8')
}
def decrypt(enc_dict, password):
# decode the dictionary entries from base64
salt = b64decode(enc_dict['salt'])
cipher_text = b64decode(enc_dict['cipher_text'])
nonce = b64decode(enc_dict['nonce'])
tag = b64decode(enc_dict['tag'])
# generate the private key from the password and salt
private_key = hashlib.scrypt(
password.encode(), salt=salt, n=2**14, r=8, p=1, dklen=32)
# create the cipher config
cipher = AES.new(private_key, AES.MODE_GCM, nonce=nonce)
# decrypt the cipher text
decrypted = cipher.decrypt_and_verify(cipher_text, tag)
return decrypted
This worked on my local machine. I installed the packages to a directory on my local machine using
pip install <packages> -t . --no-user
and zipped the packages and added them to AWS layers.
but when i try to do the same on AWS LAMBDA i get an import error
[ERROR] OSError: Cannot load native module 'Cryptodome.Cipher._raw_ecb': Not found '_raw_ecb.cpython-39-x86_64-linux-gnu.so', Not found '_raw_ecb.abi3.so', Not found '_raw_ecb.so'
Traceback (most recent call last):
File "/var/lang/lib/python3.9/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 850, in exec_module
File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
File "/var/task/lambda_function.py", line 7, in <module>
from Cryptodome.Cipher import AES
File "/opt/python/Cryptodome/Cipher/__init__.py", line 27, in <module>
from Cryptodome.Cipher._mode_ecb import _create_ecb_cipher
File "/opt/python/Cryptodome/Cipher/_mode_ecb.py", line 35, in <module>
raw_ecb_lib = load_pycryptodome_raw_lib("Cryptodome.Cipher._raw_ecb", """
File "/opt/python/Cryptodome/Util/_raw_api.py", line 309, in load_pycryptodome_raw_lib
raise OSError("Cannot load native module '%s': %s" % (name, ", ".join(attempts)))START RequestId: 17522507-0686-4f36-a104-f728c870b93e Version: $LATEST
2023-02-08T07:15:32.694Z 17522507-0686-4f36-a104-f728c870b93e Task timed out after 3.04 seconds
END RequestId: 17522507-0686-4f36-a104-f728c870b93e
REPORT RequestId: 17522507-0686-4f36-a104-f728c870b93e Duration: 3039.25 ms Billed Duration: 3000 ms Memory Size: 128 MB Max Memory Used: 19 MB
Any help appreciated