/* test: cryptomod.exe -v | grep 'morep verbose level not specified' */
/* test: cryptomod.exe -V | grep 'cryptomod verbose level not specified' */
-/* test: cryptomod.exe -V 3 -l script-cross_crypto_r2b-cm.log & pid=$!; sleep 1; ./simulator.exe -l script-cross_crypto_r2b-si.log -s CROSS_CRYPTO -t lo://00:00:00:00:00:00/0819 -r lo://00:00:00:00:00:00/0829 test/script-cross_crypto_r2b-si.eth; rc=$?; sleep 1; kill -TERM $pid; test $rc -eq 0 */
-/* test: cryptomod.exe -V 3 -l script-cross_crypto_b2r-cm.log & pid=$!; sleep 1; ./simulator.exe -l script-cross_crypto_b2r-si.log -s CROSS_CRYPTO -t lo://00:00:00:00:00:00/0839 -r lo://00:00:00:00:00:00/0809 test/script-cross_crypto_b2r-si.eth; rc=$?; sleep 1; kill -TERM $pid; test $rc -eq 0 */
+/* test: cryptomod.exe -V 3 -l script-cross_crypto_r2b-cm.log & pid=$!; sleep 1; ./simulator.exe -l script-cross_crypto_r2b-si.log -s CROSS_CRYPTO -t lo://00:00:00:00:00:00/0819 -r lo://00:00:00:00:00:00/0829 -s PROVISIONING -t lo://00:00:00:00:00:00/081b -r lo://00:00:00:00:00:00/080b test/script-cross_crypto_r2b-si.eth; rc=$?; sleep 1; kill -TERM $pid; test $rc -eq 0 */
+/* test: cryptomod.exe -V 3 -l script-cross_crypto_b2r-cm.log & pid=$!; sleep 1; ./simulator.exe -l script-cross_crypto_b2r-si.log -s CROSS_CRYPTO -t lo://00:00:00:00:00:00/0839 -r lo://00:00:00:00:00:00/0809 -s PROVISIONING -t lo://00:00:00:00:00:00/081b -r lo://00:00:00:00:00:00/080b test/script-cross_crypto_b2r-si.eth; rc=$?; sleep 1; kill -TERM $pid; test $rc -eq 0 */
-/* test: cryptomod.exe -V 3 -l script-local_crypto_black-cm.log & pid=$!; sleep 1; ./simulator.exe -l script-local_crypto_black-si.log -s LOCAL_CRYPTO -t lo://00:00:00:00:00:00/083a -r lo://00:00:00:00:00:00/082a test/script-local_crypto-si.eth; rc=$?; sleep 1; kill -TERM $pid; test $rc -eq 0 */
+/* test: cryptomod.exe -V 3 -l script-local_crypto_black-cm.log & pid=$!; sleep 1; ./simulator.exe -l script-local_crypto_black-si.log -s LOCAL_CRYPTO -t lo://00:00:00:00:00:00/083a -r lo://00:00:00:00:00:00/082a -s PROVISIONING -t lo://00:00:00:00:00:00/081b -r lo://00:00:00:00:00:00/080b test/script-local_crypto-si.eth; rc=$?; sleep 1; kill -TERM $pid; test $rc -eq 0 */
-/* test: cryptomod.exe -V 3 -l script-local_crypto_red-cm.log & pid=$!; sleep 1; ./simulator.exe -l script-local_crypto_red-si.log -s LOCAL_CRYPTO -t lo://00:00:00:00:00:00/081a -r lo://00:00:00:00:00:00/080a test/script-local_crypto-si.eth; rc=$?; sleep 1; kill -TERM $pid; test $rc -eq 0 */
+/* test: cryptomod.exe -V 3 -l script-local_crypto_red-cm.log & pid=$!; sleep 1; ./simulator.exe -l script-local_crypto_red-si.log -s LOCAL_CRYPTO -t lo://00:00:00:00:00:00/081a -r lo://00:00:00:00:00:00/080a -s PROVISIONING -t lo://00:00:00:00:00:00/081b -r lo://00:00:00:00:00:00/080b test/script-local_crypto-si.eth; rc=$?; sleep 1; kill -TERM $pid; test $rc -eq 0 */
/* test: cryptomod.exe -V 3 -l script-provisioning-cm.log & pid=$!; sleep 1; ./simulator.exe -l script-provisioning-si.log -s PROVISIONING -t lo://00:00:00:00:00:00/081b -r lo://00:00:00:00:00:00/080b test/script-provisioning-si.eth; rc=$?; sleep 1; kill -TERM $pid; test $rc -eq 0 */
{
VERBOSE (crypto, TRACE, PRINTF ("encrypt_func\n"));
+ int rc = 0;
+
// FIXIT: not thread safe
state = working_e;
out->channel_id = in->channel_id;
/* filter bypass */
-#ifndef PROTOCOL_EXT
if (in->bypass_len) {
+#ifndef PROTOCOL_EXT
VERBOSE (crypto, WARNING, PRINTF ("bypass not supproted by this protocol\n"));
- }
- out->bypass_len = 0;
-#else
- BYPASS_t pdu_bypass = {0};
- deserial_bypass (in->bypass, in->bypass_len, &pdu_bypass);
- if (content_filter (&pdu_bypass)) {
- VERBOSE (crypto, WARNING, PRINTF ("bypass message filtered\n"));
- out->bypass_len = 0;
- } else {
- memcpy (out->bypass, in->bypass, in->bypass_len * sizeof (uint8_t));
- out->bypass_len = in->bypass_len;
- }
+ rc = 1;
+ goto end_encrypt_func;
#endif
- /* get add */
- int aad_len = sizeof (in->aad);
-
- /* draw iv */
- int iv_len = sizeof (out->iv);
- for (int i = 0, r = 0; i < iv_len; i++) {
- if (i % 4) {
- r = rand ();
+ BYPASS_t pdu_bypass = {0};
+ deserial_bypass (in->bypass, in->bypass_len, &pdu_bypass);
+ if (content_filter (&pdu_bypass)) {
+ VERBOSE (crypto, WARNING, PRINTF ("bypass message filtered\n"));
+ rc = 1;
+ goto end_encrypt_func;
+ } else {
+ memcpy (out->bypass, in->bypass, in->bypass_len * sizeof (uint8_t));
+ out->bypass_len = in->bypass_len;
}
- out->iv[i] = r & 0xff;
- r >>= 8;
}
+ /* get add */
+ int aad_len = sizeof (in->aad);
+
/* look for key */
int cid = in->channel_id;
int kid = channels[cid] - 1;
int klen = key_lengths[kid];
if (klen != KEY_LENGTH) {
VERBOSE (crypto, WARNING, PRINTF ("incorrect key length (%d)\n", klen));
- }
- memset (key, 0, (klen > KEY_LENGTH) ? KEY_LENGTH : klen);
+ rc = 1;
+ goto end_encrypt_func;
+ }
+ memcpy (key, keys[kid], klen);
} else {
VERBOSE (crypto, WARNING, PRINTF ("no key associated to channel %d (%d)\n", cid, kid));
+ rc = 1;
+ goto end_encrypt_func;
+ }
+
+ /* draw iv */
+ int iv_len = sizeof (out->iv);
+ for (int i = 0, r = 0; i < iv_len; i++) {
+ if (i % 4) {
+ r = rand ();
+ }
+ out->iv[i] = r & 0xff;
+ r >>= 8;
}
/* encrypt message */
}
EVP_EncryptUpdate (ctx, out->data, &out_len, in->data, in->data_len);
out->data_len = out_len;
- int rc = 0;
if (EVP_EncryptFinal_ex (ctx, out->data + out->data_len, &out_len)) {
out->data_len += out_len;
EVP_CIPHER_CTX_ctrl (ctx, EVP_CTRL_AEAD_GET_TAG, TAG_LENGTH, (void *)(out->data + out->data_len));
}
EVP_CIPHER_CTX_free (ctx);
+end_encrypt_func:
+
state = ready_e;
return rc;
{
VERBOSE (crypto, TRACE, PRINTF ("decrypt_func\n"));
+ int rc = 0;
+
// FIXIT: not thread safe
state = working_e;
out->channel_id = in->channel_id;
/* filter bypass */
-#ifndef PROTOCOL_EXT
if (in->bypass_len) {
+#ifndef PROTOCOL_EXT
VERBOSE (crypto, WARNING, PRINTF ("bypass not supproted by this protocol\n"));
- }
- out->bypass_len = 0;
-#else
- BYPASS_t pdu_bypass = {0};
- deserial_bypass (in->bypass, in->bypass_len, &pdu_bypass);
- if (content_filter (&pdu_bypass)) {
- VERBOSE (crypto, WARNING, PRINTF ("bypass message filtered\n"));
- out->bypass_len = 0;
- } else {
- memcpy (out->bypass, in->bypass, in->bypass_len * sizeof (uint8_t));
- out->bypass_len = in->bypass_len;
- }
+ rc = 1;
+ goto end_decrypt_func;
#endif
+ BYPASS_t pdu_bypass = {0};
+ deserial_bypass (in->bypass, in->bypass_len, &pdu_bypass);
+ if (content_filter (&pdu_bypass)) {
+ VERBOSE (crypto, WARNING, PRINTF ("bypass message filtered\n"));
+ rc = 1;
+ goto end_decrypt_func;
+ } else {
+ memcpy (out->bypass, in->bypass, in->bypass_len * sizeof (uint8_t));
+ out->bypass_len = in->bypass_len;
+ }
+ }
+
/* get aad */
int aad_len = sizeof (in->aad);
- /* get iv */
- int iv_len = sizeof (in->iv);
-
/* look for key */
int cid = in->channel_id;
int kid = channels[cid] - 1;
int klen = key_lengths[kid];
if (klen != KEY_LENGTH) {
VERBOSE (crypto, WARNING, PRINTF ("incorrect key length (%d)\n", klen));
+ rc = 1;
+ goto end_decrypt_func;
}
- memset (key, 0, (klen > KEY_LENGTH) ? KEY_LENGTH : klen);
+ memcpy (key, keys[kid], klen);
} else {
VERBOSE (crypto, WARNING, PRINTF ("no key associated to channel %d (%d)\n", cid, kid));
+ rc = 1;
+ goto end_decrypt_func;
}
+ /* get iv */
+ int iv_len = sizeof (in->iv);
+
/* decrypt message */
EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
EVP_DecryptInit_ex (ctx, EVP_aes_256_gcm (), NULL, NULL, NULL);
if (aad_len) {
EVP_DecryptUpdate (ctx, NULL, &out_len, in->aad, aad_len);
}
- int rc = 0;
if (EVP_DecryptUpdate (ctx, out->data, &out_len, in->data, in->data_len - TAG_LENGTH)) {
out->data_len = out_len;
EVP_DecryptFinal_ex (ctx, NULL, &out_len);
}
EVP_CIPHER_CTX_free (ctx);
+end_decrypt_func:
+
state = ready_e;
return rc;
#ifndef PROTOCOL_EXT
channels[kid] = kid + 1;
+ VERBOSE(crypto, DEBUG, PRINTF ("load key (%d) stored in channels (%d)\n", kid, channels[kid]));
#endif
out->status = ok_e;