9
9
#include "accelerator/core/mam_core.h"
10
10
#include "common/model/transfer.h"
11
11
#include "utils/containers/hash/hash_array.h"
12
+ #include "utils/fill_nines.h"
13
+ #include "utils/tryte_byte_conv.h"
12
14
13
15
#define MAM_LOGGER "mam_core"
14
16
@@ -161,26 +163,53 @@ static mam_endpoint_t *mam_api_endpoint_get(mam_api_t const *const api, tryte_t
161
163
return NULL ;
162
164
}
163
165
166
+ /**
167
+ * @brief Free 'mam_encrypt_key_t' object
168
+ *
169
+ * @param mam_key[in] 'mam_encrypt_key_t' object to be freed
170
+ *
171
+ * @return status code
172
+ */
173
+ static inline void mam_encrypt_key_free (mam_encrypt_key_t * mam_key ) {
174
+ mam_psk_t_set_free (& mam_key -> psks );
175
+ mam_ntru_pk_t_set_free (& mam_key -> ntru_pks );
176
+ mam_ntru_sk_t_set_free (& mam_key -> ntru_sks );
177
+ }
178
+
164
179
/**
165
180
* @brief Add all the keys in the list of Pre-Shared Key and NTRU public key into corresponding key set.
166
181
*
167
- * @param psks[in] Pre-Shared Key set
168
- * @param ntru_pks[in] NTRU public key set
182
+ * The PSK keys are converting the index into trytes as PSK ID.
183
+ *
184
+ * @param psks[out] Pre-Shared Key set
185
+ * @param ntru_pks[out] NTRU public key set
169
186
* @param psk[in] List of Pre-Shared Key
170
187
* @param ntru_pk[in] List of NTRU public key
171
188
*
172
189
* @return status code
173
190
*/
174
- static status_t ta_set_mam_key (mam_psk_t_set_t * const psks , mam_ntru_pk_t_set_t * const ntru_pks ,
175
- UT_array const * const psk , UT_array const * const ntru_pk ) {
191
+ static status_t ta_set_mam_key (mam_encrypt_key_t * const mam_keys , UT_array const * const psk ,
192
+ UT_array const * const ntru_pk , UT_array const * const ntru_sk ) {
193
+ status_t ret = SC_OK ;
176
194
char * * p = NULL ;
177
195
if (psk ) {
178
196
mam_psk_t psk_obj ;
197
+ uint16_t psk_id_cnt = 0 ;
179
198
while ((p = (char * * )utarray_next (psk , p ))) {
199
+ tryte_t raw_psk_id [NUM_TRYTES_MAM_PSK_ID_SIZE + 1 ] = {}, psk_id [NUM_TRYTES_MAM_PSK_ID_SIZE + 1 ] = {};
200
+ bytes_to_trytes ((unsigned char * )& psk_id_cnt , sizeof (psk_id_cnt ) / sizeof (char ), (char * )raw_psk_id );
201
+ ret = fill_nines ((char * )psk_id , (char * )raw_psk_id , NUM_TRYTES_MAM_PSK_ID_SIZE );
202
+ if (ret ) {
203
+ ta_log_error ("%s\n" , ta_error_to_string (ret ));
204
+ return ret ;
205
+ }
206
+
207
+ trytes_to_trits ((tryte_t * )psk_id , psk_obj .id , NUM_TRYTES_MAM_PSK_ID_SIZE );
180
208
trytes_to_trits ((tryte_t * )* p , psk_obj .key , NUM_TRYTES_MAM_PSK_KEY_SIZE );
181
- if (mam_psk_t_set_add (psks , & psk_obj ) != RC_OK ) {
182
- ta_log_error ("%s\n" , "SC_MAM_FAILED_INIT" );
183
- return SC_MAM_FAILED_INIT ;
209
+ if (mam_psk_t_set_add (& mam_keys -> psks , & psk_obj ) != RC_OK ) {
210
+ ret = SC_MAM_FAILED_INIT ;
211
+ ta_log_error ("%s\n" , ta_error_to_string (ret ));
212
+ return ret ;
184
213
}
185
214
}
186
215
}
@@ -189,9 +218,22 @@ static status_t ta_set_mam_key(mam_psk_t_set_t *const psks, mam_ntru_pk_t_set_t
189
218
mam_ntru_pk_t ntru_pk_obj ;
190
219
while ((p = (char * * )utarray_next (ntru_pk , p ))) {
191
220
trytes_to_trits ((tryte_t * )* p , ntru_pk_obj .key , NUM_TRYTES_MAM_NTRU_PK_SIZE );
192
- if (mam_ntru_pk_t_set_add (ntru_pks , & ntru_pk_obj ) != RC_OK ) {
193
- ta_log_error ("%s\n" , "SC_MAM_FAILED_INIT" );
194
- return SC_MAM_FAILED_INIT ;
221
+ if (mam_ntru_pk_t_set_add (& mam_keys -> ntru_pks , & ntru_pk_obj ) != RC_OK ) {
222
+ ret = SC_MAM_FAILED_INIT ;
223
+ ta_log_error ("%s\n" , ta_error_to_string (ret ));
224
+ return ret ;
225
+ }
226
+ }
227
+ }
228
+
229
+ if (ntru_sk ) {
230
+ mam_ntru_sk_t ntru_sk_obj ;
231
+ while ((p = (char * * )utarray_next (ntru_sk , p ))) {
232
+ trytes_to_trits ((tryte_t * )* p , ntru_sk_obj .secret_key , NUM_TRYTES_MAM_NTRU_SK_SIZE );
233
+ if (mam_ntru_sk_t_set_add (& mam_keys -> ntru_sks , & ntru_sk_obj ) != RC_OK ) {
234
+ ret = SC_MAM_FAILED_INIT ;
235
+ ta_log_error ("%s\n" , ta_error_to_string (ret ));
236
+ return ret ;
195
237
}
196
238
}
197
239
}
@@ -528,7 +570,8 @@ status_t ta_send_mam_message(const ta_config_t *const info, const iota_config_t
528
570
tryte_t chid [MAM_CHANNEL_ID_TRYTE_SIZE ] = {}, msg_id [NUM_TRYTES_MAM_MSG_ID ] = {};
529
571
bundle_transactions_t * bundle = NULL ;
530
572
send_mam_data_mam_v1_t * data = (send_mam_data_mam_v1_t * )req -> data ;
531
- mam_encrypt_key_t mam_key = {.psks = NULL , .ntru_pks = NULL };
573
+ send_mam_key_mam_v1_t * key = (send_mam_key_mam_v1_t * )req -> key ;
574
+ mam_encrypt_key_t mam_key = {.psks = NULL , .ntru_pks = NULL , .ntru_sks = NULL };
532
575
bool msg_sent = false;
533
576
534
577
// Creating MAM API
@@ -538,6 +581,12 @@ status_t ta_send_mam_message(const ta_config_t *const info, const iota_config_t
538
581
goto done ;
539
582
}
540
583
584
+ ret = ta_set_mam_key (& mam_key , key -> psk_array , key -> ntru_array , NULL );
585
+ if (ret ) {
586
+ ta_log_error ("%s\n" , ta_error_to_string (ret ));
587
+ goto done ;
588
+ }
589
+
541
590
mam_send_operation_t mam_operation ;
542
591
while (!msg_sent ) {
543
592
bundle_transactions_renew (& bundle );
@@ -608,7 +657,7 @@ status_t ta_send_mam_message(const ta_config_t *const info, const iota_config_t
608
657
}
609
658
}
610
659
bundle_transactions_free (& bundle );
611
-
660
+ mam_encrypt_key_free ( & mam_key );
612
661
return ret ;
613
662
}
614
663
@@ -619,6 +668,7 @@ status_t ta_recv_mam_message(const iota_config_t *const iconf, const iota_client
619
668
bundle_array_t * bundle_array = NULL ;
620
669
bundle_array_new (& bundle_array );
621
670
recv_mam_data_id_mam_v1_t * data_id = (recv_mam_data_id_mam_v1_t * )req -> data_id ;
671
+ recv_mam_key_mam_v1_t * key = (recv_mam_key_mam_v1_t * )req -> key ;
622
672
if (mam_api_init (& mam , (tryte_t * )iconf -> seed ) != RC_OK ) {
623
673
ret = SC_MAM_FAILED_INIT ;
624
674
ta_log_error ("%s\n" , ta_error_to_string (ret ));
@@ -652,6 +702,23 @@ status_t ta_recv_mam_message(const iota_config_t *const iconf, const iota_client
652
702
}
653
703
}
654
704
705
+ // Add decryption keys
706
+ mam_encrypt_key_t mam_key = {.psks = NULL , .ntru_pks = NULL , .ntru_sks = NULL };
707
+ ret = ta_set_mam_key (& mam_key , key -> psk_array , NULL , key -> ntru_array );
708
+ if (ret != SC_OK ) {
709
+ ta_log_error ("%s\n" , ta_error_to_string (ret ));
710
+ goto done ;
711
+ }
712
+
713
+ mam_psk_t_set_entry_t * curr_psk_p = NULL ;
714
+ mam_psk_t_set_entry_t * tmp_psk_p = NULL ;
715
+ HASH_ITER (hh , mam_key .psks , curr_psk_p , tmp_psk_p ) {
716
+ if (mam_api_add_psk (& mam , & curr_psk_p -> value )) {
717
+ ta_log_error ("%s\n" , "Failed to add PSK keys" );
718
+ goto done ;
719
+ }
720
+ }
721
+
655
722
// Copy the trusted_channel_pks, before fetching the information from MAM.
656
723
mam_pk_t_set_t init_trusted_ch = NULL ;
657
724
mam_pk_t_set_entry_t * curr_entry = NULL ;
@@ -694,5 +761,6 @@ status_t ta_recv_mam_message(const iota_config_t *const iconf, const iota_client
694
761
}
695
762
bundle_array_free (& bundle_array );
696
763
mam_pk_t_set_free (& init_trusted_ch );
764
+ mam_encrypt_key_free (& mam_key );
697
765
return ret ;
698
766
}
0 commit comments