static void do_f1_f1star ( uint8_t * key, uint8_t * rand, uint8_t * sqn, uint8_t * amf, uint8_t * op, uint8_t * f1_exp, uint8_t * f1star_exp) { uint8_t res[8]; SetOPc (op); f1 (key, rand, sqn, amf, res); if (compare_buffer (res, 8, f1_exp, 8) != 0) { fail ("Fail: f1"); } f1star (key, rand, sqn, amf, res); if (compare_buffer (res, 8, f1star_exp, 8) != 0) { fail ("Fail: f1*"); } }
static void do_f2f3f5(uint8_t *key, uint8_t *rand, uint8_t *op, uint8_t *f2_exp, uint8_t *f5_exp, uint8_t *f3_exp) { uint8_t res_f2[8]; uint8_t res_f5[6]; uint8_t res_f3[16]; uint8_t res_f4[16]; SetOPc(op); f2345(key, rand, res_f2, res_f3, res_f4, res_f5); if (compare_buffer(res_f2, 8, f2_exp, 8) != 0) { fail("Fail: f2"); } if (compare_buffer(res_f5, 6, f5_exp, 6) != 0) { fail("Fail: f5"); } if (compare_buffer(res_f3, 16, f3_exp, 16) != 0) { fail("Fail: f3"); } }
static void eea2_encrypt(uint8_t direction, uint32_t count, uint8_t bearer, uint8_t *key, uint32_t key_length, uint8_t *message, uint32_t length, uint8_t *expected) { nas_stream_cipher_t *nas_cipher; uint8_t *result; uint32_t zero_bits = length & 7; uint32_t byte_length = length >> 3; if (zero_bits > 0) byte_length += 1; nas_cipher = calloc(1, sizeof(nas_stream_cipher_t)); nas_cipher->direction = direction; nas_cipher->count = count; nas_cipher->key = key; nas_cipher->key_length = key_length; nas_cipher->bearer = bearer; nas_cipher->blength = length; nas_cipher->message = message; if (nas_stream_cipher_eea2(nas_cipher, &result) != 0) fail("Fail: nas_stream_cipher_eea2\n"); if (compare_buffer(result, byte_length, expected, byte_length) != 0) { fail("Fail: eea2_encrypt\n"); } free(nas_cipher); free(result); }
static void test_uncipher_ctr ( const struct nettle_cipher *cipher, const uint8_t * key, unsigned key_length, const uint8_t * cipheredtext, unsigned length, const uint8_t * cleartext, const uint8_t * ictr) { void *ctx = malloc (cipher->context_size); uint8_t *data = malloc (length); uint8_t *ctr = malloc (cipher->block_size); cipher->set_encrypt_key (ctx, key_length, key); memcpy (ctr, ictr, cipher->block_size); ctr_crypt (ctx, cipher->encrypt, cipher->block_size, ctr, length, data, cipheredtext); if (compare_buffer (data, length, cleartext, length) != 0) { fail ("Fail: test_uncipher_ctr\n"); } free (ctx); free (data); free (ctr); }
static int test_item2(void) { int rc = TC_PASS; TYPE_VALUE* shmem_addr = NULL; TYPE_VALUE my_value = {0, 0}; TYPE_VALUE peer_value = {0, 0}; TYPE_VALUE expect_value = {0, 0}; int num_proc = 0; int my_proc = 0; int peer_proc = 0; num_proc = _num_pes(); my_proc = _my_pe(); shmem_addr = shmalloc(sizeof(*shmem_addr)); if (shmem_addr) { TYPE_VALUE value = {-1, 0}; /* Set my value */ my_value.field1 = my_proc; memcpy(shmem_addr, &my_value, sizeof(my_value)); /* Define peer and it value */ peer_proc = (my_proc + 1) % num_proc; peer_value.field1 = peer_proc; /* Define expected value */ memcpy(&expect_value, &peer_value, sizeof(peer_value)); /* Wait is set instead of barrier to give some time to all PE for setting their values */ shmem_barrier_all(); /* Get value from peer */ FUNC_VALUE(&value, shmem_addr, 1, peer_proc); rc = (compare_buffer((unsigned char*)&expect_value, (unsigned char*)&value, sizeof(value), NULL) ? TC_PASS : TC_FAIL); log_debug(OSH_TC, "my(#%d:%lld.%lld) peer(#%d:%lld.%lld) expected = %lld.%lld actual = %lld.%lld\n", my_proc, (INT64_TYPE)my_value.field1, (INT64_TYPE)my_value.field2, peer_proc, (INT64_TYPE)peer_value.field1, (INT64_TYPE)peer_value.field2, (INT64_TYPE)expect_value.field1, (INT64_TYPE)expect_value.field2, (INT64_TYPE)value.field1, (INT64_TYPE)value.field2); } else { rc = TC_SETUP_FAIL; } if (shmem_addr) { shfree(shmem_addr); } return rc; }
void doit (void) { int i; break_on_error = 1; // asn1_xer_print = 1; // asn_debug = 1; for (i = 0; i < sizeof(s1ap_test) / sizeof(s1ap_test_t); i++) { struct s1ap_message_s message; uint8_t *buffer; uint32_t length; memset(&message, 0, sizeof(struct s1ap_message_s)); if (s1ap_test[i].originating == ENB) { /* eNB originated procedure -> USE MME decoder */ if (s1ap_mme_decode_pdu(&message, s1ap_test[i].buffer, s1ap_test[i].buf_len) < 0) { fail("Failed to decode %s\n", s1ap_test[i].procedure_name); } else { success("Decoded %s correctly\n", s1ap_test[i].procedure_name); } } else { /* MME originated procedure -> USE eNB decoder */ if (s1ap_eNB_decode_pdu(&message, s1ap_test[i].buffer, s1ap_test[i].buf_len) < 0) { fail("Failed to decode %s\n", s1ap_test[i].procedure_name); } else { success("Decoded %s correctly\n", s1ap_test[i].procedure_name); } } if (s1ap_test[i].originating == ENB) { /* eNB originated procedure -> USE eNB encoder */ if (s1ap_eNB_encode_pdu(&message, &buffer, &length) < 0) { fail("Failed to decode %s\n", s1ap_test[i].procedure_name); } else { success("Encoded %s correctly\n", s1ap_test[i].procedure_name); } } else { /* MME originated procedure -> USE mme encoder */ if (s1ap_mme_encode_pdu(&message, &buffer, &length) < 0) { fail("Failed to encode %s\n", s1ap_test[i].procedure_name); } else { success("Encoded %s correctly\n", s1ap_test[i].procedure_name); } } if (compare_buffer(buffer, length, s1ap_test[i].buffer, s1ap_test[i].buf_len) < 0) { fail("Mismatch in buffers\n"); } } }
static void do_kdf(uint8_t *key, unsigned key_length, uint8_t *data, unsigned data_length, uint8_t *exp, unsigned exp_length) { uint8_t result[32]; kdf(key, key_length, data, data_length, result, 32); if (compare_buffer(result, exp_length, exp, exp_length) != 0) { fail("Fail: kdf\n"); } }
static void do_f4f5star(uint8_t *key, uint8_t *rand, uint8_t *op, uint8_t *f4_exp, uint8_t *f5star_exp) { uint8_t res_f2[8]; uint8_t res_f5[6]; uint8_t res_f3[16]; uint8_t res_f4[16]; uint8_t res_f5star[6]; SetOPc(op); f2345(key, rand, res_f2, res_f3, res_f4, res_f5); if (compare_buffer(res_f4, 16, f4_exp, 16) != 0) { fail("Fail: f4"); } f5star(key, rand, res_f5star); if (compare_buffer(res_f5star, 6, f5star_exp, 6) != 0) { fail("Fail: f5star"); } }
static void derive_knas_keys(algorithm_type_dist_t atd, uint8_t *kasme, unsigned length, uint8_t *knas_enc_exp, uint8_t *knas_int_exp) { uint8_t *knas_enc; uint8_t *knas_int; derive_key_nas_enc(atd, kasme, &knas_enc); derive_key_nas_int(atd, kasme, &knas_int); /* Compare both keys with expected */ if (compare_buffer(knas_enc, 32, knas_enc_exp, 32) != 0) { fail("Fail: knas_enc derivation\n"); } if (compare_buffer(knas_int, 32, knas_int_exp, 32) != 0) { fail("Fail: knas_int derivation\n"); } free(knas_enc); free(knas_int); }
static void do_derive_kenb(uint32_t nas_count, const uint8_t *kasme, const unsigned length, const uint8_t *kenb_exp) { uint8_t kenb[32]; memset(kenb, 0, sizeof(kenb)); derive_keNB(kasme, nas_count, kenb); if (compare_buffer(kenb_exp, length, kenb, length) != 0) { fail("Fail: kenb derivation\n"); } }
static void do_derive_kasme ( uint8_t * sn_id, uint8_t * sqn, uint8_t * ak, uint8_t * ck, uint8_t * ik, uint8_t * kasme_exp) { uint8_t kasme[32]; derive_kasme (ck, ik, sn_id, sqn, ak, kasme); if (compare_buffer (kasme, 32, kasme_exp, 32) != 0) { fail ("Fail: derive_kasme\n"); } }
int main(int argc, char *argv[]) { int i; asn1_xer_print = 2; if (argc > 1) { asn_debug = 1; } for (i = 0; i < sizeof(s1ap_test) / sizeof(s1ap_test_t); i++) { struct s1ap_message_s message; uint8_t *buffer; uint32_t length; memset(&message, 0, sizeof(struct s1ap_message_s)); printf("Trying to decode %s procedure with asn1c decoder\n", s1ap_test[i].procedure_name); if (s1ap_mme_decode_pdu(&message, s1ap_test[i].buffer, s1ap_test[i].buf_len) < 0) { if (s1ap_eNB_decode_pdu(&message, s1ap_test[i].buffer, s1ap_test[i].buf_len) < 0) { printf("Failed to decode this message\n"); } else { printf("Succesfully decoded %s with eNB decoder\n", s1ap_test[i].procedure_name); } } else { printf("Succesfully decoded %s with MME decoder\n", s1ap_test[i].procedure_name); } printf("Trying to encode %s procedure with asn1c encoder\n", s1ap_test[i].procedure_name); if (s1ap_eNB_encode_pdu(&message, &buffer, &length) < 0) { printf("Failed to encode this message on MME side, trying eNB side\n"); } else { compare_buffer(buffer, length, s1ap_test[i].buffer, s1ap_test[i].buf_len); free(buffer); } } return 0; }
static void test_cmac ( const uint8_t * key, unsigned key_length, const uint8_t * message, unsigned length, const uint8_t * expect, unsigned expected_length) { size_t size; uint8_t result[16]; ctx = CMAC_CTX_new (); CMAC_Init (ctx, key, key_length, EVP_aes_128_cbc (), NULL); CMAC_Update (ctx, message, length); CMAC_Final (ctx, result, &size); CMAC_CTX_free (ctx); if (compare_buffer (result, size, expect, expected_length) != 0) { fail ("test_cmac"); } }
static void eia2_encrypt(uint8_t direction, uint32_t count, uint8_t bearer, uint8_t *key, uint32_t key_length, uint8_t *message, uint32_t length, uint8_t *expected, uint32_t length_expected) { nas_stream_cipher_t nas_cipher; uint8_t result[4]; nas_cipher.direction = direction; nas_cipher.count = count; nas_cipher.key = key; nas_cipher.key_length = key_length; nas_cipher.bearer = bearer; nas_cipher.blength = length; nas_cipher.message = message; if (nas_stream_encrypt_eia2(&nas_cipher, result) != 0) { fail("Fail: nas_stream_encrypt_eia2\n"); } if (compare_buffer(result, 4, expected, length_expected) != 0) { fail("Fail: eia2_encrypt\n"); } }
/**************************************************************************** * Place for Test Item functions ***************************************************************************/ static int test_item1(void) { int rc = TC_PASS; TYPE_VALUE* shmem_addr = NULL; TYPE_VALUE* local_addr = NULL; TYPE_VALUE my_value = 0; TYPE_VALUE peer_value = 0; TYPE_VALUE* expect_value = NULL; int num_proc = 0; int my_proc = 0; int peer_proc = 0; int tst, sst; int max_stride = MAX_ARRAY_SIZE/2-1; num_proc = _num_pes(); my_proc = _my_pe(); shmem_addr = shmalloc(sizeof(*shmem_addr)*MAX_ARRAY_SIZE); local_addr = malloc(sizeof(*local_addr)*MAX_ARRAY_SIZE); expect_value = malloc(sizeof(*expect_value)*MAX_ARRAY_SIZE); if (shmem_addr) { INT64_TYPE i = 0; INT64_TYPE j = 0; int num_to_get; my_value = 0; size_t odd_pos; for (i = 0; (i < COUNT_VALUE) && (rc == TC_PASS); i++) { tst = (i < max_stride) ? i+1 : max_stride; sst = tst; num_to_get = MAX_ARRAY_SIZE/tst; /* Set my value */ my_value = (TYPE_VALUE)(my_proc + 1); memset(local_addr,0,MAX_ARRAY_SIZE*SIZE_VALUE); memset(expect_value,0,MAX_ARRAY_SIZE*SIZE_VALUE); for (j = 0; j < MAX_ARRAY_SIZE; j++) shmem_addr[j] = my_value; /* Define peer and it value */ peer_proc = (my_proc + 1) % num_proc; peer_value = (TYPE_VALUE)(peer_proc + 1); /* Define expected value */ for (j=0; j<num_to_get; j++) expect_value[j*tst] = peer_value; /* Wait is set instead of barrier to give some time to all PE for setting their values */ shmem_barrier_all(); /* Get value from peer */ FUNC_VALUE(local_addr, shmem_addr,tst,sst,num_to_get,peer_proc); if (rc == TC_PASS) { rc = (compare_buffer((unsigned char*)local_addr, (unsigned char*)expect_value, MAX_ARRAY_SIZE, &odd_pos) ? TC_PASS : TC_FAIL); } log_debug(OSH_TC, "my(#%d:%lld) peer(#%d:%lld) expected = %lld vs got = %lld\n", my_proc, (INT64_TYPE)my_value, peer_proc, (INT64_TYPE)peer_value, (INT64_TYPE)expect_value[0], (INT64_TYPE)local_addr[0]); /* Wait is set instead of barrier to give some time to all PE for setting their values */ shmem_barrier_all(); } } else { rc = TC_SETUP_FAIL; } if (local_addr) { free(local_addr); } if (expect_value) { free(expect_value); } if (shmem_addr) { shfree(shmem_addr); } return rc; }