-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Addition of new API functions, new encryption modes (AES-CTR, GCM), unit tests and CI tests with refactoring of code. #7
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
hokeun
reviewed
Jan 22, 2024
…actor symmetric encrypt authenticate to be more memory efficient
This reverts commit 640f7a1.
hokeun
approved these changes
Jan 6, 2025
hokeun
approved these changes
Jan 8, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Please merge with iotauth/iotauth#38.
0. Introducing new APIs and examples.
This PR adds new API functions and an example of encryption and decryption of files in block chunks. Also adds unit tests, CI tests. Finally, major refactoring done on the entire code base.
Here is a summary of this PR.
malloc()
.examples/
1. New APIs
SECURE_COMM_MSG
, and fills the buffer with the received message. Input is the connected socket, pointer of the buffer, and the given buffer's length (not the received message length.)The four functions below are for encrypting and decrypting buffers with the session key.
4, 5. These functions encrypt/decrypt the given plaintext/ciphertext with the given session key. It mallocs a buffer for the encrypted/decrypted result, and returns the double pointer of the encrypted/decrypted buffer.
6, 7. Unlike the function above, they do not allocate memory, the user should provide the buffer with enough length.
8, 9. Save/Load the session_key_list to the file_path.
10,11. Save/Load the session_key_list with a salted password.
The two below are some useful helper functions.
12. Reads the session key id buffer and saves it in an unsigned integer. This is needed when the user wants to save the session_key id as an int value.
2. New example of file block encryption.
This example tests encrypting multiple buffers using different session keys, and test getting the session key by the session key ID, and decrypting them.
Encrypting part.
block_writer.c
encrypted'i'.txt
. Each file uses different session keys.plaintext'i'.txt
, which is the blocks not encrypted.encrypted_file_metadata.dat
. Same withplaintext_file_metadata.dat
. It saves the used session key id for the file.Decrypt and Comparing part.
block_reader.c
encrypted'i'.txt
and decrypt it with the requested session key.plaintext'i'.txt
, and check if it's decrypted properly.For compile details, please check the README.md
3. Refactoring encryption and decryption functions.
Change return values to enable error checking for functions.
The original code just returns the pointer of the encrypted or decrypted buffers. However, it does not do any error checking.
Now, the return values are the success and failure of the encryption and decryption.
0 stands for success, 1 for failure.
Removing
memcpy()
insidesymmetric_encrypt_authenticate()
andsymmetric_decrypt_authenticate()
The original code does many unnecessary
memcpy()
. All of them are removed.Add functions to encrypt/decrypt buffers with/without
malloc()
We now provide API functions that encrypt/decrypt buffers. Also, The users can select to use functions with or without
malloc().
These are examples using the two functions.
4. Detailed changes on each file.
c_common.c
read_from_socket()
write_from_socket()
with better error handlings.check_SECURE_COMM_MSG_type()
to not #define theSECURE_COMM_MSG
inc_api.c
.connect_as_client()
to retryconnect()
.parse_received_message()
: AddAUTH_ALERT
handling.c_common.h
read_from_socket()``write_from_socket()
check_SECURE_COMM_MSG_type()
load_config.c
ENCRYPTION_MODE
andHMAC_MODE
load_config()
:free_config_t
: Addfree()
for the config itself.load_config.h
#define
values to anenum config_type_t
.c_crypto.c
free()
and error handlings.digest_message_SHA_256()
: Does not malloc memory anymore. Buffer should be assigned before called.AES_CBC_128_encrypt()
toencrypt_AES()
, andAES_CBC_128_decrypt()
todecrypt_AES()
CTR
,GCM
).get_EVP_CIPHER()
: Set theEVP_CIPHER
based on the encryption mode.get_expected_encrypted_total_length()
,get_expected_decrypted_maximum_length()
:malloc()
memory.create_salted_password_to_32bytes()
: Create a 32 byte digested password using the salt.symmetric_encrypt_authenticate()
/symmetric_decrypt_authenticate()
get_symmetric_encrypt_authenticate_buffer()
andget_symmetric_decrypt_authenticate_buffer()
symmetric_encrypt_authenticate_without_malloc()
/symmetric_decrypt_authenticate_without_malloc()
symmetric_encrypt_authenticate()
but does not malloc memory.get_symmetric_encrypt_authenticate_buffer()
andget_symmetric_decrypt_authenticate_buffer()
c_crypto.h
#define
macro.distribution_key_t
andsession_key_t
toc_api.h
.digest_message_SHA_256()
's description.c_secure_comm.c
send_state
to check the state during the session key exchange steps.c_secure_comm.c
to top, and make itstatic
functions.encrypt_and_sign()
,serialize_session_key_req_with_distribution_key()
,check_validity()
,parse_distribution_key()
,parse_session_key_response()
update_enc_mode_and_hmac_mode_to_session_key()
is newly added.save_distribution_key()
,parse_session_key()
: Remove unused inputs, better type casting.write()
functions towrite_to_socket()
symmetric_encrypt_authenticate()
symmetric_decrypt_authenticate()
.parse_handshake_1()
,check_handshake_2_send_handshake_3()
,decrypt_received_message()
,check_handshake1_send_handshake2()
send_SECURE_COMM_message()
: Moved fromc_api.c
toc_secure_comm.c
. This is not a new function.malloc()
memory anymore, using theget_expected_encrypted_total_length()
function.print_received_message()
:free()
memory.send_session_key_req_via_TCP()
:free()
for memory deallocation.AUTH_ALERT
handling, by case.send_session_key_req_via_UDP()
, due to compiler warnings.update_enc_mode_and_hmac_mode_to_session_key()
: Update the encryption mode and hmac_mode from theSST_ctx_t
to the session key itself.encrypt_or_decrypt_buf_with_session_key()
: Encrypts or decrypts the buffer. This malloc()s memory.encrypt_or_decrypt_buf_with_session_key_without_malloc()
: This does not domalloc()
internally. The caller must allocate the buffer before using this function.c_secure_comm.h
auth_alert_code
asenum
.SST_session_ctx_t
session_key_list_t
SST_ctx_t
struct typedefs toc_api.h
. This is done because when installingc_api.h
as a header file, it should not#include
other header files such asc_secure_comm.h
. If it does, other header files should also be installed, when doingsudo make install
.INIT_SESSION_KEY_LIST
macro function, replaced withinit_empty_session_key_list()
inc_api.c
.c_secure_comm.h
, and move the descriptions toc_secure_comm.c
.encrypt_or_decrypt_buf_with_session_key()
,encrypt_or_decrypt_buf_with_session_key_without_malloc()
.send_SECURE_COMM_message()
: Moved fromc_api.h
toc_secure_comm.h
ipfs.c
ipfs.h
stdio.h
.entity_server.c
error_exit()
is not an API function. Define same function and use it.entity_client.c
c_api.c
Newly added functions:
init_empty_session_key_list()
: Replaces the macroINIT_SESSION_KEY_LIST
encrypt_buf_with_session_key()
/decrypt_buf_with_session_key()
/encrypt_buf_with_session_key_without_malloc()
/decrypt_buf_with_session_key_without_malloc()
save_session_key_list()
/load_session_key_list()
save_session_key_list_with_password()
/load_session_key_list_with_password()
convert_skid_buf_to_int()
generate_random_nonce()
init_SST()
:OPENSSL_init_crypto(OPENSSL_INIT_NO_ATEXIT, NULL);
. This should be removed in the future, and should be added by the API user.get_session_key()
: Better error checking.secure_connect_to_server()
/secure_connect_to_server_with_socket()
:get_session_key_by_ID()
: Better error checking and memory handling.server_secure_comm_setup()
:read_from_socket()
instead ofread()
.receive_thread()
,receive_thread_read_one_each()
,receive_message()
:free_SST_ctx_t
: Update free function.c_api.h
c_api.h
. This is done to make SST as a library, only includingc_api.h
.c_api.h
should not include other header files, or else other header files must be installed when doingsudo make install
.All
config
files.All ipfs related files