diff --git a/configs/open5gs/ausf.yaml.in b/configs/open5gs/ausf.yaml.in index 54e314a33..6241b4293 100644 --- a/configs/open5gs/ausf.yaml.in +++ b/configs/open5gs/ausf.yaml.in @@ -59,6 +59,61 @@ tls: key: @sysconfdir@/open5gs/tls/ausf.key cert: @sysconfdir@/open5gs/tls/ausf.crt +# +# o Generate the private key as below. +# $ openssl genpkey -algorithm X25519 -out /etc/open5gs/hnet/curve25519-1.key +# $ openssl ecparam -name prime256v1 -genkey -conv_form compressed -out /etc/open5gs/hnet/secp256r1-2.key +# +# o The private and public keys can be viewed with the command. +# The public key is used when creating the SIM. +# $ openssl pkey -in /etc/open5gs/hnet/curve25519-1.key -text +# $ openssl ec -in /etc/open5gs/hnet/secp256r1-2.key -conv_form compressed -text +# +# hnet: +# o Home network public key identifier(PKI) value : 1 +# Protection scheme identifier : ECIES scheme profile A +# - id: 1 +# scheme: 1 +# key: /etc/open5gs/hnet/curve25519-1.key +# +# o Home network public key identifier(PKI) value : 2 +# Protection scheme identifier : ECIES scheme profile B +# - id: 2 +# scheme: 2 +# key: /etc/open5gs/hnet/secp256r1-2.key +# +# o Home network public key identifier(PKI) value : 3 +# Protection scheme identifier : ECIES scheme profile A +# - id: 3 +# scheme: 1 +# key: /etc/open5gs/hnet/curve25519-1.key +# +# o Home network public key identifier(PKI) value : 4 +# Protection scheme identifier : ECIES scheme profile B +# - id: 4 +# scheme: 2 +# key: /etc/open5gs/hnet/secp256r1-2.key +# +hnet: + - id: 1 + scheme: 1 + key: @sysconfdir@/open5gs/hnet/curve25519-1.key + - id: 2 + scheme: 2 + key: @sysconfdir@/open5gs/hnet/secp256r1-2.key + - id: 3 + scheme: 1 + key: @sysconfdir@/open5gs/hnet/curve25519-3.key + - id: 4 + scheme: 2 + key: @sysconfdir@/open5gs/hnet/secp256r1-4.key + - id: 5 + scheme: 1 + key: @sysconfdir@/open5gs/hnet/curve25519-5.key + - id: 6 + scheme: 2 + key: @sysconfdir@/open5gs/hnet/secp256r1-6.key + # # ausf: # diff --git a/configs/open5gs/hnet/curve25519-1.key b/configs/open5gs/hnet/curve25519-1.key new file mode 100644 index 000000000..419bfe93c --- /dev/null +++ b/configs/open5gs/hnet/curve25519-1.key @@ -0,0 +1,3 @@ +-----BEGIN PRIVATE KEY----- +MC4CAQAwBQYDK2VuBCIEIBDJxn6GGlYloduPaEEjiW2bNQYZnT3xlo4HtshEi7FH +-----END PRIVATE KEY----- diff --git a/configs/open5gs/hnet/curve25519-3.key b/configs/open5gs/hnet/curve25519-3.key new file mode 100644 index 000000000..b3d0b6948 --- /dev/null +++ b/configs/open5gs/hnet/curve25519-3.key @@ -0,0 +1,3 @@ +-----BEGIN PRIVATE KEY----- +MC4CAQAwBQYDK2VuBCIEIFAK2WjCQjB8TU7COXwdIKVhKGjPa+SJuyOVObjfW9hM +-----END PRIVATE KEY----- diff --git a/configs/open5gs/hnet/curve25519-5.key b/configs/open5gs/hnet/curve25519-5.key new file mode 100644 index 000000000..020fef93b --- /dev/null +++ b/configs/open5gs/hnet/curve25519-5.key @@ -0,0 +1,3 @@ +-----BEGIN PRIVATE KEY----- +MC4CAQAwBQYDK2VuBCIEIHh8rsYF8otbnyb8bcrhD1AAV5C9iBtjTlYJY3k5k0dt +-----END PRIVATE KEY----- diff --git a/configs/open5gs/hnet/meson.build b/configs/open5gs/hnet/meson.build new file mode 100644 index 000000000..6c97fc4b2 --- /dev/null +++ b/configs/open5gs/hnet/meson.build @@ -0,0 +1,38 @@ +# Copyright (C) 2022 by Sukchan Lee + +# This file is part of Open5GS. + +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +hnet_sysconfdir = join_paths(open5gs_sysconfdir, 'hnet') +meson.add_install_script(python3_exe, '-c', + mkdir_p.format(hnet_sysconfdir)) + +hnet_security = ''' + curve25519-1.key + secp256r1-2.key + curve25519-3.key + secp256r1-4.key + curve25519-5.key + secp256r1-6.key +'''.split() + +foreach file : hnet_security + gen = configure_file( + input : file, + output : file, + configuration : conf_data) + meson.add_install_script(python3_exe, '-c', + install_conf.format(gen, hnet_sysconfdir)) +endforeach diff --git a/configs/open5gs/hnet/secp256r1-2.key b/configs/open5gs/hnet/secp256r1-2.key new file mode 100644 index 000000000..f4391a132 --- /dev/null +++ b/configs/open5gs/hnet/secp256r1-2.key @@ -0,0 +1,8 @@ +-----BEGIN EC PARAMETERS----- +BggqhkjOPQMBBw== +-----END EC PARAMETERS----- +-----BEGIN EC PRIVATE KEY----- +MHcCAQEEIHSp+RhHH1bzvv2lxR1zij+U9aUtS8nbl5n1Il+8zd5BoAoGCCqGSM49 +AwEHoUQDQgAEre/NExfRzoVi7CW5G0gAEg4SNtbiZh6kI1qE48hdokS8QqWUz1YS +9J6PvihX2OSZ+RMixzf8zxu9tuTUJKgKlQ== +-----END EC PRIVATE KEY----- diff --git a/configs/open5gs/hnet/secp256r1-4.key b/configs/open5gs/hnet/secp256r1-4.key new file mode 100644 index 000000000..35235ec68 --- /dev/null +++ b/configs/open5gs/hnet/secp256r1-4.key @@ -0,0 +1,8 @@ +-----BEGIN EC PARAMETERS----- +BggqhkjOPQMBBw== +-----END EC PARAMETERS----- +-----BEGIN EC PRIVATE KEY----- +MHcCAQEEIAGZvsOAU0YMHhBK33hRkGMPkA3Xefq5b5hPQD6qnf5goAoGCCqGSM49 +AwEHoUQDQgAEdXfTAGY+0ibQoO9bfmk7+M/l//BiMzO6lNIUEMSj1k3k9SQPygGY +jAuUHpVM4Uo6cWxuyurEn8pWn1vF3tVhbg== +-----END EC PRIVATE KEY----- diff --git a/configs/open5gs/hnet/secp256r1-6.key b/configs/open5gs/hnet/secp256r1-6.key new file mode 100644 index 000000000..cd598bdca --- /dev/null +++ b/configs/open5gs/hnet/secp256r1-6.key @@ -0,0 +1,8 @@ +-----BEGIN EC PARAMETERS----- +BggqhkjOPQMBBw== +-----END EC PARAMETERS----- +-----BEGIN EC PRIVATE KEY----- +MHcCAQEEIG3VKSXh/3WK0HzCkN1DgeUjF7TSLgAUyMn/WGHsxrZ3oAoGCCqGSM49 +AwEHoUQDQgAENU1ibHe7oWu4m6M8P0XoA78ZNKtdIsJgVU0nCk/c5sC3V+/4GuxU +owtbASEXQZg4SGvts+1Yqz0p4WwCAAcwpQ== +-----END EC PRIVATE KEY----- diff --git a/configs/open5gs/meson.build b/configs/open5gs/meson.build index 0888065a2..7d6419d4e 100644 --- a/configs/open5gs/meson.build +++ b/configs/open5gs/meson.build @@ -48,3 +48,4 @@ foreach file : open5gs_conf endforeach subdir('tls') +subdir('hnet') diff --git a/configs/open5gs/udm.yaml.in b/configs/open5gs/udm.yaml.in index 2069dfd92..dc3401b2a 100644 --- a/configs/open5gs/udm.yaml.in +++ b/configs/open5gs/udm.yaml.in @@ -59,6 +59,61 @@ tls: key: @sysconfdir@/open5gs/tls/udm.key cert: @sysconfdir@/open5gs/tls/udm.crt +# +# o Generate the private key as below. +# $ openssl genpkey -algorithm X25519 -out /etc/open5gs/hnet/curve25519-1.key +# $ openssl ecparam -name prime256v1 -genkey -conv_form compressed -out /etc/open5gs/hnet/secp256r1-2.key +# +# o The private and public keys can be viewed with the command. +# The public key is used when creating the SIM. +# $ openssl pkey -in /etc/open5gs/hnet/curve25519-1.key -text +# $ openssl ec -in /etc/open5gs/hnet/secp256r1-2.key -conv_form compressed -text +# +# hnet: +# o Home network public key identifier(PKI) value : 1 +# Protection scheme identifier : ECIES scheme profile A +# - id: 1 +# scheme: 1 +# key: /etc/open5gs/hnet/curve25519-1.key +# +# o Home network public key identifier(PKI) value : 2 +# Protection scheme identifier : ECIES scheme profile B +# - id: 2 +# scheme: 2 +# key: /etc/open5gs/hnet/secp256r1-2.key +# +# o Home network public key identifier(PKI) value : 3 +# Protection scheme identifier : ECIES scheme profile A +# - id: 3 +# scheme: 1 +# key: /etc/open5gs/hnet/curve25519-1.key +# +# o Home network public key identifier(PKI) value : 4 +# Protection scheme identifier : ECIES scheme profile B +# - id: 4 +# scheme: 2 +# key: /etc/open5gs/hnet/secp256r1-2.key +# +hnet: + - id: 1 + scheme: 1 + key: @sysconfdir@/open5gs/hnet/curve25519-1.key + - id: 2 + scheme: 2 + key: @sysconfdir@/open5gs/hnet/secp256r1-2.key + - id: 3 + scheme: 1 + key: @sysconfdir@/open5gs/hnet/curve25519-3.key + - id: 4 + scheme: 2 + key: @sysconfdir@/open5gs/hnet/secp256r1-4.key + - id: 5 + scheme: 1 + key: @sysconfdir@/open5gs/hnet/curve25519-5.key + - id: 6 + scheme: 2 + key: @sysconfdir@/open5gs/hnet/secp256r1-6.key + # # udm: # diff --git a/configs/sample.yaml.in b/configs/sample.yaml.in index 5eb6304fc..2b9862ddc 100644 --- a/configs/sample.yaml.in +++ b/configs/sample.yaml.in @@ -13,6 +13,14 @@ tls: key: @build_configs_dir@/open5gs/tls/testclient.key cert: @build_configs_dir@/open5gs/tls/testclient.crt +hnet: + - id: 1 + scheme: 1 + key: @build_configs_dir@/open5gs/hnet/curve25519-1.key + - id: 2 + scheme: 2 + key: @build_configs_dir@/open5gs/hnet/secp256r1-2.key + parameter: # no_nrf: true # no_scp: true diff --git a/debian/open5gs-common.install b/debian/open5gs-common.install index b2464d717..5881490f1 100644 --- a/debian/open5gs-common.install +++ b/debian/open5gs-common.install @@ -2,4 +2,6 @@ usr/lib/*/libogs*.so* usr/lib/*/libfd*.so* usr/lib/*/freeDiameter/*.fdx configs/open5gs/tls/ca.crt /etc/open5gs/tls +configs/open5gs/hnet/curve25519-*.key /etc/open5gs/hnet +configs/open5gs/hnet/secp256r1-*.key /etc/open5gs/hnet configs/logrotate/open5gs /etc/logrotate.d diff --git a/lib/app/ogs-app.h b/lib/app/ogs-app.h index 167625a37..53361c6d9 100644 --- a/lib/app/ogs-app.h +++ b/lib/app/ogs-app.h @@ -20,7 +20,7 @@ #ifndef OGS_APP_H #define OGS_APP_H -#include "proto/ogs-proto.h" +#include "crypt/ogs-crypt.h" #define OGS_APP_INSIDE diff --git a/lib/app/ogs-context.c b/lib/app/ogs-context.c index a4338a51d..f315df558 100644 --- a/lib/app/ogs-context.c +++ b/lib/app/ogs-context.c @@ -62,44 +62,6 @@ ogs_app_context_t *ogs_app() return &self; } -bool ogs_app_tls_server_enabled(void) -{ - if (self.tls.enabled == OGS_APP_TLS_ENABLED_AUTO) { - if (self.tls.server.key && self.tls.server.cert) - return true; - else - return false; - } else if (self.tls.enabled == OGS_APP_TLS_ENABLED_YES) { - ogs_assert(self.tls.server.key); - ogs_assert(self.tls.server.cert); - return true; - } else if (self.tls.enabled == OGS_APP_TLS_ENABLED_NO) { - return false; - } else { - ogs_error("Unknown TLS enabled mode [%d]", self.tls.enabled); - return false; - } -} - -bool ogs_app_tls_client_enabled(void) -{ - if (self.tls.enabled == OGS_APP_TLS_ENABLED_AUTO) { - if (self.tls.client.key && self.tls.client.cert) - return true; - else - return false; - } else if (self.tls.enabled == OGS_APP_TLS_ENABLED_YES) { - ogs_assert(self.tls.client.key); - ogs_assert(self.tls.client.cert); - return true; - } else if (self.tls.enabled == OGS_APP_TLS_ENABLED_NO) { - return false; - } else { - ogs_error("Unknown TLS enabled mode [%d]", self.tls.enabled); - return false; - } -} - static void recalculate_pool_size(void) { self.pool.packet = self.max.ue * OGS_MAX_NUM_OF_PACKET_BUFFER; @@ -205,8 +167,6 @@ static void regenerate_all_timer_duration(void) static void app_context_prepare(void) { - self.tls.enabled = OGS_APP_TLS_ENABLED_AUTO; - #define USRSCTP_LOCAL_UDP_PORT 9899 self.usrsctp.udp_port = USRSCTP_LOCAL_UDP_PORT; @@ -274,27 +234,6 @@ static int app_context_validation(void) return OGS_ERROR; } - if (self.tls.enabled == OGS_APP_TLS_ENABLED_YES) { - - if (!self.tls.server.key) { - ogs_error("No Server Key"); - return OGS_ERROR; - } - if (!self.tls.server.cert) { - ogs_error("No Server Certificate"); - return OGS_ERROR; - } - - if (!self.tls.client.key) { - ogs_error("No Client Key"); - return OGS_ERROR; - } - if (!self.tls.client.cert) { - ogs_error("No Client Certificate"); - return OGS_ERROR; - } - } - return OGS_OK; } @@ -329,65 +268,6 @@ int ogs_app_context_parse_config(void) ogs_yaml_iter_value(&logger_iter); } } - } else if (!strcmp(root_key, "tls")) { - ogs_yaml_iter_t tls_iter; - ogs_yaml_iter_recurse(&root_iter, &tls_iter); - while (ogs_yaml_iter_next(&tls_iter)) { - const char *tls_key = ogs_yaml_iter_key(&tls_iter); - ogs_assert(tls_key); - if (!strcmp(tls_key, "enabled")) { - const char *v = ogs_yaml_iter_value(&tls_iter); - if (!strcmp(v, "auto")) - self.tls.enabled = OGS_APP_TLS_ENABLED_AUTO; - else if (!strcmp(v, "yes")) - self.tls.enabled = OGS_APP_TLS_ENABLED_YES; - else if (!strcmp(v, "no")) - self.tls.enabled = OGS_APP_TLS_ENABLED_NO; - else - ogs_warn("unknown 'tls.enabled' value `%s`", v); - } else if (!strcmp(tls_key, "server")) { - ogs_yaml_iter_t server_iter; - ogs_yaml_iter_recurse(&tls_iter, &server_iter); - - while (ogs_yaml_iter_next(&server_iter)) { - const char *server_key = - ogs_yaml_iter_key(&server_iter); - ogs_assert(server_key); - if (!strcmp(server_key, "cacert")) { - self.tls.server.cacert = - ogs_yaml_iter_value(&server_iter); - } else if (!strcmp(server_key, "cert")) { - self.tls.server.cert = - ogs_yaml_iter_value(&server_iter); - } else if (!strcmp(server_key, "key")) { - self.tls.server.key = - ogs_yaml_iter_value(&server_iter); - } else - ogs_warn("unknown key `%s`", server_key); - } - } else if (!strcmp(tls_key, "client")) { - ogs_yaml_iter_t client_iter; - ogs_yaml_iter_recurse(&tls_iter, &client_iter); - - while (ogs_yaml_iter_next(&client_iter)) { - const char *client_key = - ogs_yaml_iter_key(&client_iter); - ogs_assert(client_key); - if (!strcmp(client_key, "cacert")) { - self.tls.client.cacert = - ogs_yaml_iter_value(&client_iter); - } else if (!strcmp(client_key, "cert")) { - self.tls.client.cert = - ogs_yaml_iter_value(&client_iter); - } else if (!strcmp(client_key, "key")) { - self.tls.client.key = - ogs_yaml_iter_value(&client_iter); - } else - ogs_warn("unknown key `%s`", client_key); - } - } else - ogs_warn("unknown key `%s`", tls_key); - } } else if (!strcmp(root_key, "parameter")) { ogs_yaml_iter_t parameter_iter; ogs_yaml_iter_recurse(&root_iter, ¶meter_iter); diff --git a/lib/app/ogs-context.h b/lib/app/ogs-context.h index f445b40a9..92b23ee04 100644 --- a/lib/app/ogs-context.h +++ b/lib/app/ogs-context.h @@ -28,12 +28,6 @@ extern "C" { #endif -typedef enum { - OGS_APP_TLS_ENABLED_AUTO = 0, - OGS_APP_TLS_ENABLED_YES, - OGS_APP_TLS_ENABLED_NO, -} ogs_app_tls_enabled_mode_e; - typedef struct ogs_app_context_s { const char *version; @@ -49,15 +43,6 @@ typedef struct ogs_app_context_s { const char *domain; } logger; - struct { - ogs_app_tls_enabled_mode_e enabled; - struct { - const char *cacert; - const char *cert; - const char *key; - } server, client; - } tls; - ogs_queue_t *queue; ogs_timer_mgr_t *timer_mgr; ogs_pollset_t *pollset; diff --git a/lib/core/meson.build b/lib/core/meson.build index af64a7fee..ac82bcd2c 100644 --- a/lib/core/meson.build +++ b/lib/core/meson.build @@ -261,6 +261,7 @@ libcore_sources = files(''' ogs-hash.h ogs-misc.h ogs-getopt.h + ogs-file.h abts.h ogs-abort.c @@ -296,6 +297,7 @@ libcore_sources = files(''' ogs-hash.c ogs-misc.c ogs-getopt.c + ogs-file.c ogs-core.c abts.c '''.split()) diff --git a/lib/core/ogs-conv.c b/lib/core/ogs-conv.c index e5db3b16d..e7db948b5 100644 --- a/lib/core/ogs-conv.c +++ b/lib/core/ogs-conv.c @@ -29,7 +29,7 @@ #include "ogs-core.h" -void *ogs_ascii_to_hex(char *in, int in_len, void *out, int out_len) +int ogs_ascii_to_hex(char *in, int in_len, void *out, int out_len) { int i = 0, j = 0, k = 0, hex; uint8_t *out_p = out; @@ -49,7 +49,7 @@ void *ogs_ascii_to_hex(char *in, int in_len, void *out, int out_len) i++; } - return out; + return j; } void *ogs_hex_to_ascii(void *in, int in_len, void *out, int out_len) diff --git a/lib/core/ogs-conv.h b/lib/core/ogs-conv.h index 68b96d6b7..0c3f2833e 100644 --- a/lib/core/ogs-conv.h +++ b/lib/core/ogs-conv.h @@ -28,8 +28,14 @@ extern "C" { #endif -#define OGS_HEX(I, I_LEN, O) ogs_ascii_to_hex((char*)I, I_LEN, O, sizeof(O)) -void *ogs_ascii_to_hex(char *in, int in_len, void *out, int out_len); +int ogs_ascii_to_hex(char *in, int in_len, void *out, int out_len); +static ogs_inline void *ogs_hex_from_string( + const char *str, void *out, int out_len) +{ + ogs_ascii_to_hex((char*)str, strlen(str), out, out_len); + return out; +} + void *ogs_hex_to_ascii(void *in, int in_len, void *out, int out_len); void *ogs_uint64_to_buffer(uint64_t num, int size, void *buffer); uint64_t ogs_buffer_to_uint64(void *buffer, int size); diff --git a/lib/core/ogs-core.h b/lib/core/ogs-core.h index c91cfa716..f72cc364b 100644 --- a/lib/core/ogs-core.h +++ b/lib/core/ogs-core.h @@ -62,6 +62,7 @@ #include "core/ogs-hash.h" #include "core/ogs-misc.h" #include "core/ogs-getopt.h" +#include "core/ogs-file.h" #undef OGS_CORE_INSIDE diff --git a/lib/core/ogs-file.c b/lib/core/ogs-file.c new file mode 100644 index 000000000..928409511 --- /dev/null +++ b/lib/core/ogs-file.c @@ -0,0 +1,51 @@ +/* + * Copyright (C) 2022 by Sukchan Lee + * + * This file is part of Open5GS. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "ogs-core.h" + +int ogs_file_read_full( + const char *filename, void *buf, size_t nbytes, size_t *bytes_read) +{ + FILE *fp = NULL; + size_t total_read = 0, amt; + + ogs_assert(filename); + ogs_assert(buf); + ogs_assert(nbytes); + + fp = fopen(filename, "rb"); + if (!fp) { + ogs_error("Cannot find file [%s]", filename); + return OGS_ERROR; + } + + amt = fread(buf, 1, nbytes, fp); + + if (bytes_read != NULL) + *bytes_read = amt; + + fclose(fp); + + if (amt > 0) + return OGS_OK; + + ogs_error("Cannot read file [name:%s,amt:%d,total_read:%d]", + filename, (int)amt, (int)total_read); + return OGS_ERROR; +} diff --git a/lib/core/ogs-file.h b/lib/core/ogs-file.h new file mode 100644 index 000000000..b7e760c61 --- /dev/null +++ b/lib/core/ogs-file.h @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2022 by Sukchan Lee + * + * This file is part of Open5GS. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#if !defined(OGS_CORE_INSIDE) && !defined(OGS_CORE_COMPILATION) +#error "This header cannot be included directly." +#endif + +#ifndef OGS_FILE_H +#define OGS_FILE_H + +#ifdef __cplusplus +extern "C" { +#endif + +int ogs_file_read_full( + const char *filename, void *buf, size_t nbytes, size_t *bytes_read); + +#ifdef __cplusplus +} +#endif + +#endif /* OGS_FILE_H */ diff --git a/lib/crypt/curve25519-donna.c b/lib/crypt/curve25519-donna.c new file mode 100644 index 000000000..c29fa60b5 --- /dev/null +++ b/lib/crypt/curve25519-donna.c @@ -0,0 +1,864 @@ +/* Copyright 2008, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * curve25519-donna: Curve25519 elliptic curve, public key function + * + * http://code.google.com/p/curve25519-donna/ + * + * Adam Langley + * + * Derived from public domain C code by Daniel J. Bernstein + * + * More information about curve25519 can be found here + * http://cr.yp.to/ecdh.html + * + * djb's sample implementation of curve25519 is written in a special assembly + * language called qhasm and uses the floating point registers. + * + * This is, almost, a clean room reimplementation from the curve25519 paper. It + * uses many of the tricks described therein. Only the crecip function is taken + * from the sample implementation. */ + +#if 0 /* modified by acetcom */ +#include +#include + +#ifdef _MSC_VER +#define inline __inline +#endif +#else +#include "ogs-crypt.h" +#endif + +typedef uint8_t u8; +typedef int32_t s32; +typedef int64_t limb; + +/* Field element representation: + * + * Field elements are written as an array of signed, 64-bit limbs, least + * significant first. The value of the field element is: + * x[0] + 2^26·x[1] + x^51·x[2] + 2^102·x[3] + ... + * + * i.e. the limbs are 26, 25, 26, 25, ... bits wide. */ + +/* Sum two numbers: output += in */ +static void fsum(limb *output, const limb *in) { + unsigned i; + for (i = 0; i < 10; i += 2) { + output[0+i] = output[0+i] + in[0+i]; + output[1+i] = output[1+i] + in[1+i]; + } +} + +/* Find the difference of two numbers: output = in - output + * (note the order of the arguments!). */ +static void fdifference(limb *output, const limb *in) { + unsigned i; + for (i = 0; i < 10; ++i) { + output[i] = in[i] - output[i]; + } +} + +/* Multiply a number by a scalar: output = in * scalar */ +static void fscalar_product(limb *output, const limb *in, const limb scalar) { + unsigned i; + for (i = 0; i < 10; ++i) { + output[i] = in[i] * scalar; + } +} + +/* Multiply two numbers: output = in2 * in + * + * output must be distinct to both inputs. The inputs are reduced coefficient + * form, the output is not. + * + * output[x] <= 14 * the largest product of the input limbs. */ +static void fproduct(limb *output, const limb *in2, const limb *in) { + output[0] = ((limb) ((s32) in2[0])) * ((s32) in[0]); + output[1] = ((limb) ((s32) in2[0])) * ((s32) in[1]) + + ((limb) ((s32) in2[1])) * ((s32) in[0]); + output[2] = 2 * ((limb) ((s32) in2[1])) * ((s32) in[1]) + + ((limb) ((s32) in2[0])) * ((s32) in[2]) + + ((limb) ((s32) in2[2])) * ((s32) in[0]); + output[3] = ((limb) ((s32) in2[1])) * ((s32) in[2]) + + ((limb) ((s32) in2[2])) * ((s32) in[1]) + + ((limb) ((s32) in2[0])) * ((s32) in[3]) + + ((limb) ((s32) in2[3])) * ((s32) in[0]); + output[4] = ((limb) ((s32) in2[2])) * ((s32) in[2]) + + 2 * (((limb) ((s32) in2[1])) * ((s32) in[3]) + + ((limb) ((s32) in2[3])) * ((s32) in[1])) + + ((limb) ((s32) in2[0])) * ((s32) in[4]) + + ((limb) ((s32) in2[4])) * ((s32) in[0]); + output[5] = ((limb) ((s32) in2[2])) * ((s32) in[3]) + + ((limb) ((s32) in2[3])) * ((s32) in[2]) + + ((limb) ((s32) in2[1])) * ((s32) in[4]) + + ((limb) ((s32) in2[4])) * ((s32) in[1]) + + ((limb) ((s32) in2[0])) * ((s32) in[5]) + + ((limb) ((s32) in2[5])) * ((s32) in[0]); + output[6] = 2 * (((limb) ((s32) in2[3])) * ((s32) in[3]) + + ((limb) ((s32) in2[1])) * ((s32) in[5]) + + ((limb) ((s32) in2[5])) * ((s32) in[1])) + + ((limb) ((s32) in2[2])) * ((s32) in[4]) + + ((limb) ((s32) in2[4])) * ((s32) in[2]) + + ((limb) ((s32) in2[0])) * ((s32) in[6]) + + ((limb) ((s32) in2[6])) * ((s32) in[0]); + output[7] = ((limb) ((s32) in2[3])) * ((s32) in[4]) + + ((limb) ((s32) in2[4])) * ((s32) in[3]) + + ((limb) ((s32) in2[2])) * ((s32) in[5]) + + ((limb) ((s32) in2[5])) * ((s32) in[2]) + + ((limb) ((s32) in2[1])) * ((s32) in[6]) + + ((limb) ((s32) in2[6])) * ((s32) in[1]) + + ((limb) ((s32) in2[0])) * ((s32) in[7]) + + ((limb) ((s32) in2[7])) * ((s32) in[0]); + output[8] = ((limb) ((s32) in2[4])) * ((s32) in[4]) + + 2 * (((limb) ((s32) in2[3])) * ((s32) in[5]) + + ((limb) ((s32) in2[5])) * ((s32) in[3]) + + ((limb) ((s32) in2[1])) * ((s32) in[7]) + + ((limb) ((s32) in2[7])) * ((s32) in[1])) + + ((limb) ((s32) in2[2])) * ((s32) in[6]) + + ((limb) ((s32) in2[6])) * ((s32) in[2]) + + ((limb) ((s32) in2[0])) * ((s32) in[8]) + + ((limb) ((s32) in2[8])) * ((s32) in[0]); + output[9] = ((limb) ((s32) in2[4])) * ((s32) in[5]) + + ((limb) ((s32) in2[5])) * ((s32) in[4]) + + ((limb) ((s32) in2[3])) * ((s32) in[6]) + + ((limb) ((s32) in2[6])) * ((s32) in[3]) + + ((limb) ((s32) in2[2])) * ((s32) in[7]) + + ((limb) ((s32) in2[7])) * ((s32) in[2]) + + ((limb) ((s32) in2[1])) * ((s32) in[8]) + + ((limb) ((s32) in2[8])) * ((s32) in[1]) + + ((limb) ((s32) in2[0])) * ((s32) in[9]) + + ((limb) ((s32) in2[9])) * ((s32) in[0]); + output[10] = 2 * (((limb) ((s32) in2[5])) * ((s32) in[5]) + + ((limb) ((s32) in2[3])) * ((s32) in[7]) + + ((limb) ((s32) in2[7])) * ((s32) in[3]) + + ((limb) ((s32) in2[1])) * ((s32) in[9]) + + ((limb) ((s32) in2[9])) * ((s32) in[1])) + + ((limb) ((s32) in2[4])) * ((s32) in[6]) + + ((limb) ((s32) in2[6])) * ((s32) in[4]) + + ((limb) ((s32) in2[2])) * ((s32) in[8]) + + ((limb) ((s32) in2[8])) * ((s32) in[2]); + output[11] = ((limb) ((s32) in2[5])) * ((s32) in[6]) + + ((limb) ((s32) in2[6])) * ((s32) in[5]) + + ((limb) ((s32) in2[4])) * ((s32) in[7]) + + ((limb) ((s32) in2[7])) * ((s32) in[4]) + + ((limb) ((s32) in2[3])) * ((s32) in[8]) + + ((limb) ((s32) in2[8])) * ((s32) in[3]) + + ((limb) ((s32) in2[2])) * ((s32) in[9]) + + ((limb) ((s32) in2[9])) * ((s32) in[2]); + output[12] = ((limb) ((s32) in2[6])) * ((s32) in[6]) + + 2 * (((limb) ((s32) in2[5])) * ((s32) in[7]) + + ((limb) ((s32) in2[7])) * ((s32) in[5]) + + ((limb) ((s32) in2[3])) * ((s32) in[9]) + + ((limb) ((s32) in2[9])) * ((s32) in[3])) + + ((limb) ((s32) in2[4])) * ((s32) in[8]) + + ((limb) ((s32) in2[8])) * ((s32) in[4]); + output[13] = ((limb) ((s32) in2[6])) * ((s32) in[7]) + + ((limb) ((s32) in2[7])) * ((s32) in[6]) + + ((limb) ((s32) in2[5])) * ((s32) in[8]) + + ((limb) ((s32) in2[8])) * ((s32) in[5]) + + ((limb) ((s32) in2[4])) * ((s32) in[9]) + + ((limb) ((s32) in2[9])) * ((s32) in[4]); + output[14] = 2 * (((limb) ((s32) in2[7])) * ((s32) in[7]) + + ((limb) ((s32) in2[5])) * ((s32) in[9]) + + ((limb) ((s32) in2[9])) * ((s32) in[5])) + + ((limb) ((s32) in2[6])) * ((s32) in[8]) + + ((limb) ((s32) in2[8])) * ((s32) in[6]); + output[15] = ((limb) ((s32) in2[7])) * ((s32) in[8]) + + ((limb) ((s32) in2[8])) * ((s32) in[7]) + + ((limb) ((s32) in2[6])) * ((s32) in[9]) + + ((limb) ((s32) in2[9])) * ((s32) in[6]); + output[16] = ((limb) ((s32) in2[8])) * ((s32) in[8]) + + 2 * (((limb) ((s32) in2[7])) * ((s32) in[9]) + + ((limb) ((s32) in2[9])) * ((s32) in[7])); + output[17] = ((limb) ((s32) in2[8])) * ((s32) in[9]) + + ((limb) ((s32) in2[9])) * ((s32) in[8]); + output[18] = 2 * ((limb) ((s32) in2[9])) * ((s32) in[9]); +} + +/* Reduce a long form to a short form by taking the input mod 2^255 - 19. + * + * On entry: |output[i]| < 14*2^54 + * On exit: |output[0..8]| < 280*2^54 */ +static void freduce_degree(limb *output) { + /* Each of these shifts and adds ends up multiplying the value by 19. + * + * For output[0..8], the absolute entry value is < 14*2^54 and we add, at + * most, 19*14*2^54 thus, on exit, |output[0..8]| < 280*2^54. */ + output[8] += output[18] << 4; + output[8] += output[18] << 1; + output[8] += output[18]; + output[7] += output[17] << 4; + output[7] += output[17] << 1; + output[7] += output[17]; + output[6] += output[16] << 4; + output[6] += output[16] << 1; + output[6] += output[16]; + output[5] += output[15] << 4; + output[5] += output[15] << 1; + output[5] += output[15]; + output[4] += output[14] << 4; + output[4] += output[14] << 1; + output[4] += output[14]; + output[3] += output[13] << 4; + output[3] += output[13] << 1; + output[3] += output[13]; + output[2] += output[12] << 4; + output[2] += output[12] << 1; + output[2] += output[12]; + output[1] += output[11] << 4; + output[1] += output[11] << 1; + output[1] += output[11]; + output[0] += output[10] << 4; + output[0] += output[10] << 1; + output[0] += output[10]; +} + +#if (-1 & 3) != 3 +#error "This code only works on a two's complement system" +#endif + +/* return v / 2^26, using only shifts and adds. + * + * On entry: v can take any value. */ +static inline limb +div_by_2_26(const limb v) +{ + /* High word of v; no shift needed. */ + const uint32_t highword = (uint32_t) (((uint64_t) v) >> 32); + /* Set to all 1s if v was negative; else set to 0s. */ + const int32_t sign = ((int32_t) highword) >> 31; + /* Set to 0x3ffffff if v was negative; else set to 0. */ + const int32_t roundoff = ((uint32_t) sign) >> 6; + /* Should return v / (1<<26) */ + return (v + roundoff) >> 26; +} + +/* return v / (2^25), using only shifts and adds. + * + * On entry: v can take any value. */ +static inline limb +div_by_2_25(const limb v) +{ + /* High word of v; no shift needed*/ + const uint32_t highword = (uint32_t) (((uint64_t) v) >> 32); + /* Set to all 1s if v was negative; else set to 0s. */ + const int32_t sign = ((int32_t) highword) >> 31; + /* Set to 0x1ffffff if v was negative; else set to 0. */ + const int32_t roundoff = ((uint32_t) sign) >> 7; + /* Should return v / (1<<25) */ + return (v + roundoff) >> 25; +} + +/* Reduce all coefficients of the short form input so that |x| < 2^26. + * + * On entry: |output[i]| < 280*2^54 */ +static void freduce_coefficients(limb *output) { + unsigned i; + + output[10] = 0; + + for (i = 0; i < 10; i += 2) { + limb over = div_by_2_26(output[i]); + /* The entry condition (that |output[i]| < 280*2^54) means that over is, at + * most, 280*2^28 in the first iteration of this loop. This is added to the + * next limb and we can approximate the resulting bound of that limb by + * 281*2^54. */ + output[i] -= over << 26; + output[i+1] += over; + + /* For the first iteration, |output[i+1]| < 281*2^54, thus |over| < + * 281*2^29. When this is added to the next limb, the resulting bound can + * be approximated as 281*2^54. + * + * For subsequent iterations of the loop, 281*2^54 remains a conservative + * bound and no overflow occurs. */ + over = div_by_2_25(output[i+1]); + output[i+1] -= over << 25; + output[i+2] += over; + } + /* Now |output[10]| < 281*2^29 and all other coefficients are reduced. */ + output[0] += output[10] << 4; + output[0] += output[10] << 1; + output[0] += output[10]; + + output[10] = 0; + + /* Now output[1..9] are reduced, and |output[0]| < 2^26 + 19*281*2^29 + * So |over| will be no more than 2^16. */ + { + limb over = div_by_2_26(output[0]); + output[0] -= over << 26; + output[1] += over; + } + + /* Now output[0,2..9] are reduced, and |output[1]| < 2^25 + 2^16 < 2^26. The + * bound on |output[1]| is sufficient to meet our needs. */ +} + +/* A helpful wrapper around fproduct: output = in * in2. + * + * On entry: |in[i]| < 2^27 and |in2[i]| < 2^27. + * + * output must be distinct to both inputs. The output is reduced degree + * (indeed, one need only provide storage for 10 limbs) and |output[i]| < 2^26. */ +static void +fmul(limb *output, const limb *in, const limb *in2) { + limb t[19]; + fproduct(t, in, in2); + /* |t[i]| < 14*2^54 */ + freduce_degree(t); + freduce_coefficients(t); + /* |t[i]| < 2^26 */ + memcpy(output, t, sizeof(limb) * 10); +} + +/* Square a number: output = in**2 + * + * output must be distinct from the input. The inputs are reduced coefficient + * form, the output is not. + * + * output[x] <= 14 * the largest product of the input limbs. */ +static void fsquare_inner(limb *output, const limb *in) { + output[0] = ((limb) ((s32) in[0])) * ((s32) in[0]); + output[1] = 2 * ((limb) ((s32) in[0])) * ((s32) in[1]); + output[2] = 2 * (((limb) ((s32) in[1])) * ((s32) in[1]) + + ((limb) ((s32) in[0])) * ((s32) in[2])); + output[3] = 2 * (((limb) ((s32) in[1])) * ((s32) in[2]) + + ((limb) ((s32) in[0])) * ((s32) in[3])); + output[4] = ((limb) ((s32) in[2])) * ((s32) in[2]) + + 4 * ((limb) ((s32) in[1])) * ((s32) in[3]) + + 2 * ((limb) ((s32) in[0])) * ((s32) in[4]); + output[5] = 2 * (((limb) ((s32) in[2])) * ((s32) in[3]) + + ((limb) ((s32) in[1])) * ((s32) in[4]) + + ((limb) ((s32) in[0])) * ((s32) in[5])); + output[6] = 2 * (((limb) ((s32) in[3])) * ((s32) in[3]) + + ((limb) ((s32) in[2])) * ((s32) in[4]) + + ((limb) ((s32) in[0])) * ((s32) in[6]) + + 2 * ((limb) ((s32) in[1])) * ((s32) in[5])); + output[7] = 2 * (((limb) ((s32) in[3])) * ((s32) in[4]) + + ((limb) ((s32) in[2])) * ((s32) in[5]) + + ((limb) ((s32) in[1])) * ((s32) in[6]) + + ((limb) ((s32) in[0])) * ((s32) in[7])); + output[8] = ((limb) ((s32) in[4])) * ((s32) in[4]) + + 2 * (((limb) ((s32) in[2])) * ((s32) in[6]) + + ((limb) ((s32) in[0])) * ((s32) in[8]) + + 2 * (((limb) ((s32) in[1])) * ((s32) in[7]) + + ((limb) ((s32) in[3])) * ((s32) in[5]))); + output[9] = 2 * (((limb) ((s32) in[4])) * ((s32) in[5]) + + ((limb) ((s32) in[3])) * ((s32) in[6]) + + ((limb) ((s32) in[2])) * ((s32) in[7]) + + ((limb) ((s32) in[1])) * ((s32) in[8]) + + ((limb) ((s32) in[0])) * ((s32) in[9])); + output[10] = 2 * (((limb) ((s32) in[5])) * ((s32) in[5]) + + ((limb) ((s32) in[4])) * ((s32) in[6]) + + ((limb) ((s32) in[2])) * ((s32) in[8]) + + 2 * (((limb) ((s32) in[3])) * ((s32) in[7]) + + ((limb) ((s32) in[1])) * ((s32) in[9]))); + output[11] = 2 * (((limb) ((s32) in[5])) * ((s32) in[6]) + + ((limb) ((s32) in[4])) * ((s32) in[7]) + + ((limb) ((s32) in[3])) * ((s32) in[8]) + + ((limb) ((s32) in[2])) * ((s32) in[9])); + output[12] = ((limb) ((s32) in[6])) * ((s32) in[6]) + + 2 * (((limb) ((s32) in[4])) * ((s32) in[8]) + + 2 * (((limb) ((s32) in[5])) * ((s32) in[7]) + + ((limb) ((s32) in[3])) * ((s32) in[9]))); + output[13] = 2 * (((limb) ((s32) in[6])) * ((s32) in[7]) + + ((limb) ((s32) in[5])) * ((s32) in[8]) + + ((limb) ((s32) in[4])) * ((s32) in[9])); + output[14] = 2 * (((limb) ((s32) in[7])) * ((s32) in[7]) + + ((limb) ((s32) in[6])) * ((s32) in[8]) + + 2 * ((limb) ((s32) in[5])) * ((s32) in[9])); + output[15] = 2 * (((limb) ((s32) in[7])) * ((s32) in[8]) + + ((limb) ((s32) in[6])) * ((s32) in[9])); + output[16] = ((limb) ((s32) in[8])) * ((s32) in[8]) + + 4 * ((limb) ((s32) in[7])) * ((s32) in[9]); + output[17] = 2 * ((limb) ((s32) in[8])) * ((s32) in[9]); + output[18] = 2 * ((limb) ((s32) in[9])) * ((s32) in[9]); +} + +/* fsquare sets output = in^2. + * + * On entry: The |in| argument is in reduced coefficients form and |in[i]| < + * 2^27. + * + * On exit: The |output| argument is in reduced coefficients form (indeed, one + * need only provide storage for 10 limbs) and |out[i]| < 2^26. */ +static void +fsquare(limb *output, const limb *in) { + limb t[19]; + fsquare_inner(t, in); + /* |t[i]| < 14*2^54 because the largest product of two limbs will be < + * 2^(27+27) and fsquare_inner adds together, at most, 14 of those + * products. */ + freduce_degree(t); + freduce_coefficients(t); + /* |t[i]| < 2^26 */ + memcpy(output, t, sizeof(limb) * 10); +} + +/* Take a little-endian, 32-byte number and expand it into polynomial form */ +static void +fexpand(limb *output, const u8 *input) { +#define F(n,start,shift,mask) \ + output[n] = ((((limb) input[start + 0]) | \ + ((limb) input[start + 1]) << 8 | \ + ((limb) input[start + 2]) << 16 | \ + ((limb) input[start + 3]) << 24) >> shift) & mask; + F(0, 0, 0, 0x3ffffff); + F(1, 3, 2, 0x1ffffff); + F(2, 6, 3, 0x3ffffff); + F(3, 9, 5, 0x1ffffff); + F(4, 12, 6, 0x3ffffff); + F(5, 16, 0, 0x1ffffff); + F(6, 19, 1, 0x3ffffff); + F(7, 22, 3, 0x1ffffff); + F(8, 25, 4, 0x3ffffff); + F(9, 28, 6, 0x1ffffff); +#undef F +} + +#if (-32 >> 1) != -16 +#error "This code only works when >> does sign-extension on negative numbers" +#endif + +/* s32_eq returns 0xffffffff iff a == b and zero otherwise. */ +static s32 s32_eq(s32 a, s32 b) { + a = ~(a ^ b); + a &= a << 16; + a &= a << 8; + a &= a << 4; + a &= a << 2; + a &= a << 1; + return a >> 31; +} + +/* s32_gte returns 0xffffffff if a >= b and zero otherwise, where a and b are + * both non-negative. */ +static s32 s32_gte(s32 a, s32 b) { + a -= b; + /* a >= 0 iff a >= b. */ + return ~(a >> 31); +} + +/* Take a fully reduced polynomial form number and contract it into a + * little-endian, 32-byte array. + * + * On entry: |input_limbs[i]| < 2^26 */ +static void +fcontract(u8 *output, limb *input_limbs) { + int i; + int j; + s32 input[10]; + s32 mask; + + /* |input_limbs[i]| < 2^26, so it's valid to convert to an s32. */ + for (i = 0; i < 10; i++) { + input[i] = input_limbs[i]; + } + + for (j = 0; j < 2; ++j) { + for (i = 0; i < 9; ++i) { + if ((i & 1) == 1) { + /* This calculation is a time-invariant way to make input[i] + * non-negative by borrowing from the next-larger limb. */ + const s32 mask = input[i] >> 31; + const s32 carry = -((input[i] & mask) >> 25); + input[i] = input[i] + (carry << 25); + input[i+1] = input[i+1] - carry; + } else { + const s32 mask = input[i] >> 31; + const s32 carry = -((input[i] & mask) >> 26); + input[i] = input[i] + (carry << 26); + input[i+1] = input[i+1] - carry; + } + } + + /* There's no greater limb for input[9] to borrow from, but we can multiply + * by 19 and borrow from input[0], which is valid mod 2^255-19. */ + { + const s32 mask = input[9] >> 31; + const s32 carry = -((input[9] & mask) >> 25); + input[9] = input[9] + (carry << 25); + input[0] = input[0] - (carry * 19); + } + + /* After the first iteration, input[1..9] are non-negative and fit within + * 25 or 26 bits, depending on position. However, input[0] may be + * negative. */ + } + + /* The first borrow-propagation pass above ended with every limb + except (possibly) input[0] non-negative. + + If input[0] was negative after the first pass, then it was because of a + carry from input[9]. On entry, input[9] < 2^26 so the carry was, at most, + one, since (2**26-1) >> 25 = 1. Thus input[0] >= -19. + + In the second pass, each limb is decreased by at most one. Thus the second + borrow-propagation pass could only have wrapped around to decrease + input[0] again if the first pass left input[0] negative *and* input[1] + through input[9] were all zero. In that case, input[1] is now 2^25 - 1, + and this last borrow-propagation step will leave input[1] non-negative. */ + { + const s32 mask = input[0] >> 31; + const s32 carry = -((input[0] & mask) >> 26); + input[0] = input[0] + (carry << 26); + input[1] = input[1] - carry; + } + + /* All input[i] are now non-negative. However, there might be values between + * 2^25 and 2^26 in a limb which is, nominally, 25 bits wide. */ + for (j = 0; j < 2; j++) { + for (i = 0; i < 9; i++) { + if ((i & 1) == 1) { + const s32 carry = input[i] >> 25; + input[i] &= 0x1ffffff; + input[i+1] += carry; + } else { + const s32 carry = input[i] >> 26; + input[i] &= 0x3ffffff; + input[i+1] += carry; + } + } + + { + const s32 carry = input[9] >> 25; + input[9] &= 0x1ffffff; + input[0] += 19*carry; + } + } + + /* If the first carry-chain pass, just above, ended up with a carry from + * input[9], and that caused input[0] to be out-of-bounds, then input[0] was + * < 2^26 + 2*19, because the carry was, at most, two. + * + * If the second pass carried from input[9] again then input[0] is < 2*19 and + * the input[9] -> input[0] carry didn't push input[0] out of bounds. */ + + /* It still remains the case that input might be between 2^255-19 and 2^255. + * In this case, input[1..9] must take their maximum value and input[0] must + * be >= (2^255-19) & 0x3ffffff, which is 0x3ffffed. */ + mask = s32_gte(input[0], 0x3ffffed); + for (i = 1; i < 10; i++) { + if ((i & 1) == 1) { + mask &= s32_eq(input[i], 0x1ffffff); + } else { + mask &= s32_eq(input[i], 0x3ffffff); + } + } + + /* mask is either 0xffffffff (if input >= 2^255-19) and zero otherwise. Thus + * this conditionally subtracts 2^255-19. */ + input[0] -= mask & 0x3ffffed; + + for (i = 1; i < 10; i++) { + if ((i & 1) == 1) { + input[i] -= mask & 0x1ffffff; + } else { + input[i] -= mask & 0x3ffffff; + } + } + + input[1] <<= 2; + input[2] <<= 3; + input[3] <<= 5; + input[4] <<= 6; + input[6] <<= 1; + input[7] <<= 3; + input[8] <<= 4; + input[9] <<= 6; +#define F(i, s) \ + output[s+0] |= input[i] & 0xff; \ + output[s+1] = (input[i] >> 8) & 0xff; \ + output[s+2] = (input[i] >> 16) & 0xff; \ + output[s+3] = (input[i] >> 24) & 0xff; + output[0] = 0; + output[16] = 0; + F(0,0); + F(1,3); + F(2,6); + F(3,9); + F(4,12); + F(5,16); + F(6,19); + F(7,22); + F(8,25); + F(9,28); +#undef F +} + +/* Input: Q, Q', Q-Q' + * Output: 2Q, Q+Q' + * + * x2 z3: long form + * x3 z3: long form + * x z: short form, destroyed + * xprime zprime: short form, destroyed + * qmqp: short form, preserved + * + * On entry and exit, the absolute value of the limbs of all inputs and outputs + * are < 2^26. */ +static void fmonty(limb *x2, limb *z2, /* output 2Q */ + limb *x3, limb *z3, /* output Q + Q' */ + limb *x, limb *z, /* input Q */ + limb *xprime, limb *zprime, /* input Q' */ + const limb *qmqp /* input Q - Q' */) { + limb origx[10], origxprime[10], zzz[19], xx[19], zz[19], xxprime[19], + zzprime[19], zzzprime[19], xxxprime[19]; + + memcpy(origx, x, 10 * sizeof(limb)); + fsum(x, z); + /* |x[i]| < 2^27 */ + fdifference(z, origx); /* does x - z */ + /* |z[i]| < 2^27 */ + + memcpy(origxprime, xprime, sizeof(limb) * 10); + fsum(xprime, zprime); + /* |xprime[i]| < 2^27 */ + fdifference(zprime, origxprime); + /* |zprime[i]| < 2^27 */ + fproduct(xxprime, xprime, z); + /* |xxprime[i]| < 14*2^54: the largest product of two limbs will be < + * 2^(27+27) and fproduct adds together, at most, 14 of those products. + * (Approximating that to 2^58 doesn't work out.) */ + fproduct(zzprime, x, zprime); + /* |zzprime[i]| < 14*2^54 */ + freduce_degree(xxprime); + freduce_coefficients(xxprime); + /* |xxprime[i]| < 2^26 */ + freduce_degree(zzprime); + freduce_coefficients(zzprime); + /* |zzprime[i]| < 2^26 */ + memcpy(origxprime, xxprime, sizeof(limb) * 10); + fsum(xxprime, zzprime); + /* |xxprime[i]| < 2^27 */ + fdifference(zzprime, origxprime); + /* |zzprime[i]| < 2^27 */ + fsquare(xxxprime, xxprime); + /* |xxxprime[i]| < 2^26 */ + fsquare(zzzprime, zzprime); + /* |zzzprime[i]| < 2^26 */ + fproduct(zzprime, zzzprime, qmqp); + /* |zzprime[i]| < 14*2^52 */ + freduce_degree(zzprime); + freduce_coefficients(zzprime); + /* |zzprime[i]| < 2^26 */ + memcpy(x3, xxxprime, sizeof(limb) * 10); + memcpy(z3, zzprime, sizeof(limb) * 10); + + fsquare(xx, x); + /* |xx[i]| < 2^26 */ + fsquare(zz, z); + /* |zz[i]| < 2^26 */ + fproduct(x2, xx, zz); + /* |x2[i]| < 14*2^52 */ + freduce_degree(x2); + freduce_coefficients(x2); + /* |x2[i]| < 2^26 */ + fdifference(zz, xx); // does zz = xx - zz + /* |zz[i]| < 2^27 */ + memset(zzz + 10, 0, sizeof(limb) * 9); + fscalar_product(zzz, zz, 121665); + /* |zzz[i]| < 2^(27+17) */ + /* No need to call freduce_degree here: + fscalar_product doesn't increase the degree of its input. */ + freduce_coefficients(zzz); + /* |zzz[i]| < 2^26 */ + fsum(zzz, xx); + /* |zzz[i]| < 2^27 */ + fproduct(z2, zz, zzz); + /* |z2[i]| < 14*2^(26+27) */ + freduce_degree(z2); + freduce_coefficients(z2); + /* |z2|i| < 2^26 */ +} + +/* Conditionally swap two reduced-form limb arrays if 'iswap' is 1, but leave + * them unchanged if 'iswap' is 0. Runs in data-invariant time to avoid + * side-channel attacks. + * + * NOTE that this function requires that 'iswap' be 1 or 0; other values give + * wrong results. Also, the two limb arrays must be in reduced-coefficient, + * reduced-degree form: the values in a[10..19] or b[10..19] aren't swapped, + * and all all values in a[0..9],b[0..9] must have magnitude less than + * INT32_MAX. */ +static void +swap_conditional(limb a[19], limb b[19], limb iswap) { + unsigned i; + const s32 swap = (s32) -iswap; + + for (i = 0; i < 10; ++i) { + const s32 x = swap & ( ((s32)a[i]) ^ ((s32)b[i]) ); + a[i] = ((s32)a[i]) ^ x; + b[i] = ((s32)b[i]) ^ x; + } +} + +/* Calculates nQ where Q is the x-coordinate of a point on the curve + * + * resultx/resultz: the x coordinate of the resulting curve point (short form) + * n: a little endian, 32-byte number + * q: a point of the curve (short form) */ +static void +cmult(limb *resultx, limb *resultz, const u8 *n, const limb *q) { + limb a[19] = {0}, b[19] = {1}, c[19] = {1}, d[19] = {0}; + limb *nqpqx = a, *nqpqz = b, *nqx = c, *nqz = d, *t; + limb e[19] = {0}, f[19] = {1}, g[19] = {0}, h[19] = {1}; + limb *nqpqx2 = e, *nqpqz2 = f, *nqx2 = g, *nqz2 = h; + + unsigned i, j; + + memcpy(nqpqx, q, sizeof(limb) * 10); + + for (i = 0; i < 32; ++i) { + u8 byte = n[31 - i]; + for (j = 0; j < 8; ++j) { + const limb bit = byte >> 7; + + swap_conditional(nqx, nqpqx, bit); + swap_conditional(nqz, nqpqz, bit); + fmonty(nqx2, nqz2, + nqpqx2, nqpqz2, + nqx, nqz, + nqpqx, nqpqz, + q); + swap_conditional(nqx2, nqpqx2, bit); + swap_conditional(nqz2, nqpqz2, bit); + + t = nqx; + nqx = nqx2; + nqx2 = t; + t = nqz; + nqz = nqz2; + nqz2 = t; + t = nqpqx; + nqpqx = nqpqx2; + nqpqx2 = t; + t = nqpqz; + nqpqz = nqpqz2; + nqpqz2 = t; + + byte <<= 1; + } + } + + memcpy(resultx, nqx, sizeof(limb) * 10); + memcpy(resultz, nqz, sizeof(limb) * 10); +} + +// ----------------------------------------------------------------------------- +// Shamelessly copied from djb's code +// ----------------------------------------------------------------------------- +static void +crecip(limb *out, const limb *z) { + limb z2[10]; + limb z9[10]; + limb z11[10]; + limb z2_5_0[10]; + limb z2_10_0[10]; + limb z2_20_0[10]; + limb z2_50_0[10]; + limb z2_100_0[10]; + limb t0[10]; + limb t1[10]; + int i; + + /* 2 */ fsquare(z2,z); + /* 4 */ fsquare(t1,z2); + /* 8 */ fsquare(t0,t1); + /* 9 */ fmul(z9,t0,z); + /* 11 */ fmul(z11,z9,z2); + /* 22 */ fsquare(t0,z11); + /* 2^5 - 2^0 = 31 */ fmul(z2_5_0,t0,z9); + + /* 2^6 - 2^1 */ fsquare(t0,z2_5_0); + /* 2^7 - 2^2 */ fsquare(t1,t0); + /* 2^8 - 2^3 */ fsquare(t0,t1); + /* 2^9 - 2^4 */ fsquare(t1,t0); + /* 2^10 - 2^5 */ fsquare(t0,t1); + /* 2^10 - 2^0 */ fmul(z2_10_0,t0,z2_5_0); + + /* 2^11 - 2^1 */ fsquare(t0,z2_10_0); + /* 2^12 - 2^2 */ fsquare(t1,t0); + /* 2^20 - 2^10 */ for (i = 2;i < 10;i += 2) { fsquare(t0,t1); fsquare(t1,t0); } + /* 2^20 - 2^0 */ fmul(z2_20_0,t1,z2_10_0); + + /* 2^21 - 2^1 */ fsquare(t0,z2_20_0); + /* 2^22 - 2^2 */ fsquare(t1,t0); + /* 2^40 - 2^20 */ for (i = 2;i < 20;i += 2) { fsquare(t0,t1); fsquare(t1,t0); } + /* 2^40 - 2^0 */ fmul(t0,t1,z2_20_0); + + /* 2^41 - 2^1 */ fsquare(t1,t0); + /* 2^42 - 2^2 */ fsquare(t0,t1); + /* 2^50 - 2^10 */ for (i = 2;i < 10;i += 2) { fsquare(t1,t0); fsquare(t0,t1); } + /* 2^50 - 2^0 */ fmul(z2_50_0,t0,z2_10_0); + + /* 2^51 - 2^1 */ fsquare(t0,z2_50_0); + /* 2^52 - 2^2 */ fsquare(t1,t0); + /* 2^100 - 2^50 */ for (i = 2;i < 50;i += 2) { fsquare(t0,t1); fsquare(t1,t0); } + /* 2^100 - 2^0 */ fmul(z2_100_0,t1,z2_50_0); + + /* 2^101 - 2^1 */ fsquare(t1,z2_100_0); + /* 2^102 - 2^2 */ fsquare(t0,t1); + /* 2^200 - 2^100 */ for (i = 2;i < 100;i += 2) { fsquare(t1,t0); fsquare(t0,t1); } + /* 2^200 - 2^0 */ fmul(t1,t0,z2_100_0); + + /* 2^201 - 2^1 */ fsquare(t0,t1); + /* 2^202 - 2^2 */ fsquare(t1,t0); + /* 2^250 - 2^50 */ for (i = 2;i < 50;i += 2) { fsquare(t0,t1); fsquare(t1,t0); } + /* 2^250 - 2^0 */ fmul(t0,t1,z2_50_0); + + /* 2^251 - 2^1 */ fsquare(t1,t0); + /* 2^252 - 2^2 */ fsquare(t0,t1); + /* 2^253 - 2^3 */ fsquare(t1,t0); + /* 2^254 - 2^4 */ fsquare(t0,t1); + /* 2^255 - 2^5 */ fsquare(t1,t0); + /* 2^255 - 21 */ fmul(out,t1,z11); +} + +int +curve25519_donna(u8 *mypublic, const u8 *secret, const u8 *basepoint) { + limb bp[10], x[10], z[11], zmone[10]; + uint8_t e[32]; + int i; + + for (i = 0; i < 32; ++i) e[i] = secret[i]; + e[0] &= 248; + e[31] &= 127; + e[31] |= 64; + + fexpand(bp, basepoint); + cmult(x, z, e, bp); + crecip(zmone, z); + fmul(z, x, zmone); + fcontract(mypublic, z); + return 0; +} diff --git a/lib/crypt/ecc.c b/lib/crypt/ecc.c new file mode 100644 index 000000000..d22789d6a --- /dev/null +++ b/lib/crypt/ecc.c @@ -0,0 +1,1322 @@ +/* From https://github.com/jestan/easy-ecc */ + +#include "ecc.h" + +#include + +#define NUM_ECC_DIGITS (ECC_BYTES/8) +#define MAX_TRIES 16 + +typedef unsigned int uint; + +#if defined(__SIZEOF_INT128__) || ((__clang_major__ * 100 + __clang_minor__) >= 302) + #define SUPPORTS_INT128 1 +#else + #define SUPPORTS_INT128 0 +#endif + +#if SUPPORTS_INT128 +typedef unsigned __int128 uint128_t; +#else +typedef struct +{ + uint64_t m_low; + uint64_t m_high; +} uint128_t; +#endif + +typedef struct EccPoint +{ + uint64_t x[NUM_ECC_DIGITS]; + uint64_t y[NUM_ECC_DIGITS]; +} EccPoint; + +#define CONCAT1(a, b) a##b +#define CONCAT(a, b) CONCAT1(a, b) + +#define Curve_P_16 {0xFFFFFFFFFFFFFFFF, 0xFFFFFFFDFFFFFFFF} +#define Curve_P_24 {0xFFFFFFFFFFFFFFFFull, 0xFFFFFFFFFFFFFFFEull, 0xFFFFFFFFFFFFFFFFull} +#define Curve_P_32 {0xFFFFFFFFFFFFFFFFull, 0x00000000FFFFFFFFull, 0x0000000000000000ull, 0xFFFFFFFF00000001ull} +#define Curve_P_48 {0x00000000FFFFFFFF, 0xFFFFFFFF00000000, 0xFFFFFFFFFFFFFFFE, 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF} + +#define Curve_B_16 {0xD824993C2CEE5ED3, 0xE87579C11079F43D} +#define Curve_B_24 {0xFEB8DEECC146B9B1ull, 0x0FA7E9AB72243049ull, 0x64210519E59C80E7ull} +#define Curve_B_32 {0x3BCE3C3E27D2604Bull, 0x651D06B0CC53B0F6ull, 0xB3EBBD55769886BCull, 0x5AC635D8AA3A93E7ull} +#define Curve_B_48 {0x2A85C8EDD3EC2AEF, 0xC656398D8A2ED19D, 0x0314088F5013875A, 0x181D9C6EFE814112, 0x988E056BE3F82D19, 0xB3312FA7E23EE7E4} + +#define Curve_G_16 { \ + {0x0C28607CA52C5B86, 0x161FF7528B899B2D}, \ + {0xC02DA292DDED7A83, 0xCF5AC8395BAFEB13}} + +#define Curve_G_24 { \ + {0xF4FF0AFD82FF1012ull, 0x7CBF20EB43A18800ull, 0x188DA80EB03090F6ull}, \ + {0x73F977A11E794811ull, 0x631011ED6B24CDD5ull, 0x07192B95FFC8DA78ull}} + +#define Curve_G_32 { \ + {0xF4A13945D898C296ull, 0x77037D812DEB33A0ull, 0xF8BCE6E563A440F2ull, 0x6B17D1F2E12C4247ull}, \ + {0xCBB6406837BF51F5ull, 0x2BCE33576B315ECEull, 0x8EE7EB4A7C0F9E16ull, 0x4FE342E2FE1A7F9Bull}} + +#define Curve_G_48 { \ + {0x3A545E3872760AB7, 0x5502F25DBF55296C, 0x59F741E082542A38, 0x6E1D3B628BA79B98, 0x8EB1C71EF320AD74, 0xAA87CA22BE8B0537}, \ + {0x7A431D7C90EA0E5F, 0x0A60B1CE1D7E819D, 0xE9DA3113B5F0B8C0, 0xF8F41DBD289A147C, 0x5D9E98BF9292DC29, 0x3617DE4A96262C6F}} + +#define Curve_N_16 {0x75A30D1B9038A115, 0xFFFFFFFE00000000} +#define Curve_N_24 {0x146BC9B1B4D22831ull, 0xFFFFFFFF99DEF836ull, 0xFFFFFFFFFFFFFFFFull} +#define Curve_N_32 {0xF3B9CAC2FC632551ull, 0xBCE6FAADA7179E84ull, 0xFFFFFFFFFFFFFFFFull, 0xFFFFFFFF00000000ull} +#define Curve_N_48 {0xECEC196ACCC52973, 0x581A0DB248B0A77A, 0xC7634D81F4372DDF, 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF} + +static uint64_t curve_p[NUM_ECC_DIGITS] = CONCAT(Curve_P_, ECC_CURVE); +static uint64_t curve_b[NUM_ECC_DIGITS] = CONCAT(Curve_B_, ECC_CURVE); +static EccPoint curve_G = CONCAT(Curve_G_, ECC_CURVE); +static uint64_t curve_n[NUM_ECC_DIGITS] = CONCAT(Curve_N_, ECC_CURVE); + +#if (defined(_WIN32) || defined(_WIN64)) +/* Windows */ + +#define WIN32_LEAN_AND_MEAN +#include +#include + +static int getRandomNumber(uint64_t *p_vli) +{ + HCRYPTPROV l_prov; + if(!CryptAcquireContext(&l_prov, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) + { + return 0; + } + + CryptGenRandom(l_prov, ECC_BYTES, (BYTE *)p_vli); + CryptReleaseContext(l_prov, 0); + + return 1; +} + +#else /* _WIN32 */ + +/* Assume that we are using a POSIX-like system with /dev/urandom or /dev/random. */ +#include +#include +#include + +#ifndef O_CLOEXEC + #define O_CLOEXEC 0 +#endif + +static int getRandomNumber(uint64_t *p_vli) +{ + int l_fd = open("/dev/urandom", O_RDONLY | O_CLOEXEC); + if(l_fd == -1) + { + l_fd = open("/dev/random", O_RDONLY | O_CLOEXEC); + if(l_fd == -1) + { + return 0; + } + } + + char *l_ptr = (char *)p_vli; + size_t l_left = ECC_BYTES; + while(l_left > 0) + { + int l_read = read(l_fd, l_ptr, l_left); + if(l_read <= 0) + { // read failed + close(l_fd); + return 0; + } + l_left -= l_read; + l_ptr += l_read; + } + + close(l_fd); + return 1; +} + +#endif /* _WIN32 */ + +static void vli_clear(uint64_t *p_vli) +{ + uint i; + for(i=0; i= 0 && p_vli[i] == 0; --i) + { + } + + return (i + 1); +} + +/* Counts the number of bits required for p_vli. */ +static uint vli_numBits(uint64_t *p_vli) +{ + uint i; + uint64_t l_digit; + + uint l_numDigits = vli_numDigits(p_vli); + if(l_numDigits == 0) + { + return 0; + } + + l_digit = p_vli[l_numDigits - 1]; + for(i=0; l_digit; ++i) + { + l_digit >>= 1; + } + + return ((l_numDigits - 1) * 64 + i); +} + +/* Sets p_dest = p_src. */ +static void vli_set(uint64_t *p_dest, uint64_t *p_src) +{ + uint i; + for(i=0; i= 0; --i) + { + if(p_left[i] > p_right[i]) + { + return 1; + } + else if(p_left[i] < p_right[i]) + { + return -1; + } + } + return 0; +} + +/* Computes p_result = p_in << c, returning carry. Can modify in place (if p_result == p_in). 0 < p_shift < 64. */ +static uint64_t vli_lshift(uint64_t *p_result, uint64_t *p_in, uint p_shift) +{ + uint64_t l_carry = 0; + uint i; + for(i = 0; i < NUM_ECC_DIGITS; ++i) + { + uint64_t l_temp = p_in[i]; + p_result[i] = (l_temp << p_shift) | l_carry; + l_carry = l_temp >> (64 - p_shift); + } + + return l_carry; +} + +/* Computes p_vli = p_vli >> 1. */ +static void vli_rshift1(uint64_t *p_vli) +{ + uint64_t *l_end = p_vli; + uint64_t l_carry = 0; + + p_vli += NUM_ECC_DIGITS; + while(p_vli-- > l_end) + { + uint64_t l_temp = *p_vli; + *p_vli = (l_temp >> 1) | l_carry; + l_carry = l_temp << 63; + } +} + +/* Computes p_result = p_left + p_right, returning carry. Can modify in place. */ +static uint64_t vli_add(uint64_t *p_result, uint64_t *p_left, uint64_t *p_right) +{ + uint64_t l_carry = 0; + uint i; + for(i=0; i p_left[i]); + } + p_result[i] = l_diff; + } + return l_borrow; +} + +#if SUPPORTS_INT128 + +/* Computes p_result = p_left * p_right. */ +static void vli_mult(uint64_t *p_result, uint64_t *p_left, uint64_t *p_right) +{ + uint128_t r01 = 0; + uint64_t r2 = 0; + + uint i, k; + + /* Compute each digit of p_result in sequence, maintaining the carries. */ + for(k=0; k < NUM_ECC_DIGITS*2 - 1; ++k) + { + uint l_min = (k < NUM_ECC_DIGITS ? 0 : (k + 1) - NUM_ECC_DIGITS); + for(i=l_min; i<=k && i> 64) | (((uint128_t)r2) << 64); + r2 = 0; + } + + p_result[NUM_ECC_DIGITS*2 - 1] = (uint64_t)r01; +} + +/* Computes p_result = p_left^2. */ +static void vli_square(uint64_t *p_result, uint64_t *p_left) +{ + uint128_t r01 = 0; + uint64_t r2 = 0; + + uint i, k; + for(k=0; k < NUM_ECC_DIGITS*2 - 1; ++k) + { + uint l_min = (k < NUM_ECC_DIGITS ? 0 : (k + 1) - NUM_ECC_DIGITS); + for(i=l_min; i<=k && i<=k-i; ++i) + { + uint128_t l_product = (uint128_t)p_left[i] * p_left[k-i]; + if(i < k-i) + { + r2 += l_product >> 127; + l_product *= 2; + } + r01 += l_product; + r2 += (r01 < l_product); + } + p_result[k] = (uint64_t)r01; + r01 = (r01 >> 64) | (((uint128_t)r2) << 64); + r2 = 0; + } + + p_result[NUM_ECC_DIGITS*2 - 1] = (uint64_t)r01; +} + +#else /* #if SUPPORTS_INT128 */ + +static uint128_t mul_64_64(uint64_t p_left, uint64_t p_right) +{ + uint128_t l_result; + + uint64_t a0 = p_left & 0xffffffffull; + uint64_t a1 = p_left >> 32; + uint64_t b0 = p_right & 0xffffffffull; + uint64_t b1 = p_right >> 32; + + uint64_t m0 = a0 * b0; + uint64_t m1 = a0 * b1; + uint64_t m2 = a1 * b0; + uint64_t m3 = a1 * b1; + + m2 += (m0 >> 32); + m2 += m1; + if(m2 < m1) + { // overflow + m3 += 0x100000000ull; + } + + l_result.m_low = (m0 & 0xffffffffull) | (m2 << 32); + l_result.m_high = m3 + (m2 >> 32); + + return l_result; +} + +static uint128_t add_128_128(uint128_t a, uint128_t b) +{ + uint128_t l_result; + l_result.m_low = a.m_low + b.m_low; + l_result.m_high = a.m_high + b.m_high + (l_result.m_low < a.m_low); + return l_result; +} + +static void vli_mult(uint64_t *p_result, uint64_t *p_left, uint64_t *p_right) +{ + uint128_t r01 = {0, 0}; + uint64_t r2 = 0; + + uint i, k; + + /* Compute each digit of p_result in sequence, maintaining the carries. */ + for(k=0; k < NUM_ECC_DIGITS*2 - 1; ++k) + { + uint l_min = (k < NUM_ECC_DIGITS ? 0 : (k + 1) - NUM_ECC_DIGITS); + for(i=l_min; i<=k && i> 63; + l_product.m_high = (l_product.m_high << 1) | (l_product.m_low >> 63); + l_product.m_low <<= 1; + } + r01 = add_128_128(r01, l_product); + r2 += (r01.m_high < l_product.m_high); + } + p_result[k] = r01.m_low; + r01.m_low = r01.m_high; + r01.m_high = r2; + r2 = 0; + } + + p_result[NUM_ECC_DIGITS*2 - 1] = r01.m_low; +} + +#endif /* SUPPORTS_INT128 */ + + +/* Computes p_result = (p_left + p_right) % p_mod. + Assumes that p_left < p_mod and p_right < p_mod, p_result != p_mod. */ +static void vli_modAdd(uint64_t *p_result, uint64_t *p_left, uint64_t *p_right, uint64_t *p_mod) +{ + uint64_t l_carry = vli_add(p_result, p_left, p_right); + if(l_carry || vli_cmp(p_result, p_mod) >= 0) + { /* p_result > p_mod (p_result = p_mod + remainder), so subtract p_mod to get remainder. */ + vli_sub(p_result, p_result, p_mod); + } +} + +/* Computes p_result = (p_left - p_right) % p_mod. + Assumes that p_left < p_mod and p_right < p_mod, p_result != p_mod. */ +static void vli_modSub(uint64_t *p_result, uint64_t *p_left, uint64_t *p_right, uint64_t *p_mod) +{ + uint64_t l_borrow = vli_sub(p_result, p_left, p_right); + if(l_borrow) + { /* In this case, p_result == -diff == (max int) - diff. + Since -x % d == d - x, we can get the correct result from p_result + p_mod (with overflow). */ + vli_add(p_result, p_result, p_mod); + } +} + +#if ECC_CURVE == secp128r1 + +/* Computes p_result = p_product % curve_p. + See algorithm 5 and 6 from http://www.isys.uni-klu.ac.at/PDF/2001-0126-MT.pdf */ +static void vli_mmod_fast(uint64_t *p_result, uint64_t *p_product) +{ + uint64_t l_tmp[NUM_ECC_DIGITS]; + int l_carry; + + vli_set(p_result, p_product); + + l_tmp[0] = p_product[2]; + l_tmp[1] = (p_product[3] & 0x1FFFFFFFFull) | (p_product[2] << 33); + l_carry = vli_add(p_result, p_result, l_tmp); + + l_tmp[0] = (p_product[2] >> 31) | (p_product[3] << 33); + l_tmp[1] = (p_product[3] >> 31) | ((p_product[2] & 0xFFFFFFFF80000000ull) << 2); + l_carry += vli_add(p_result, p_result, l_tmp); + + l_tmp[0] = (p_product[2] >> 62) | (p_product[3] << 2); + l_tmp[1] = (p_product[3] >> 62) | ((p_product[2] & 0xC000000000000000ull) >> 29) | (p_product[3] << 35); + l_carry += vli_add(p_result, p_result, l_tmp); + + l_tmp[0] = (p_product[3] >> 29); + l_tmp[1] = ((p_product[3] & 0xFFFFFFFFE0000000ull) << 4); + l_carry += vli_add(p_result, p_result, l_tmp); + + l_tmp[0] = (p_product[3] >> 60); + l_tmp[1] = (p_product[3] & 0xFFFFFFFE00000000ull); + l_carry += vli_add(p_result, p_result, l_tmp); + + l_tmp[0] = 0; + l_tmp[1] = ((p_product[3] & 0xF000000000000000ull) >> 27); + l_carry += vli_add(p_result, p_result, l_tmp); + + while(l_carry || vli_cmp(curve_p, p_result) != 1) + { + l_carry -= vli_sub(p_result, p_result, curve_p); + } +} + +#elif ECC_CURVE == secp192r1 + +/* Computes p_result = p_product % curve_p. + See algorithm 5 and 6 from http://www.isys.uni-klu.ac.at/PDF/2001-0126-MT.pdf */ +static void vli_mmod_fast(uint64_t *p_result, uint64_t *p_product) +{ + uint64_t l_tmp[NUM_ECC_DIGITS]; + int l_carry; + + vli_set(p_result, p_product); + + vli_set(l_tmp, &p_product[3]); + l_carry = vli_add(p_result, p_result, l_tmp); + + l_tmp[0] = 0; + l_tmp[1] = p_product[3]; + l_tmp[2] = p_product[4]; + l_carry += vli_add(p_result, p_result, l_tmp); + + l_tmp[0] = l_tmp[1] = p_product[5]; + l_tmp[2] = 0; + l_carry += vli_add(p_result, p_result, l_tmp); + + while(l_carry || vli_cmp(curve_p, p_result) != 1) + { + l_carry -= vli_sub(p_result, p_result, curve_p); + } +} + +#elif ECC_CURVE == secp256r1 + +/* Computes p_result = p_product % curve_p + from http://www.nsa.gov/ia/_files/nist-routines.pdf */ +static void vli_mmod_fast(uint64_t *p_result, uint64_t *p_product) +{ + uint64_t l_tmp[NUM_ECC_DIGITS]; + int l_carry; + + /* t */ + vli_set(p_result, p_product); + + /* s1 */ + l_tmp[0] = 0; + l_tmp[1] = p_product[5] & 0xffffffff00000000ull; + l_tmp[2] = p_product[6]; + l_tmp[3] = p_product[7]; + l_carry = vli_lshift(l_tmp, l_tmp, 1); + l_carry += vli_add(p_result, p_result, l_tmp); + + /* s2 */ + l_tmp[1] = p_product[6] << 32; + l_tmp[2] = (p_product[6] >> 32) | (p_product[7] << 32); + l_tmp[3] = p_product[7] >> 32; + l_carry += vli_lshift(l_tmp, l_tmp, 1); + l_carry += vli_add(p_result, p_result, l_tmp); + + /* s3 */ + l_tmp[0] = p_product[4]; + l_tmp[1] = p_product[5] & 0xffffffff; + l_tmp[2] = 0; + l_tmp[3] = p_product[7]; + l_carry += vli_add(p_result, p_result, l_tmp); + + /* s4 */ + l_tmp[0] = (p_product[4] >> 32) | (p_product[5] << 32); + l_tmp[1] = (p_product[5] >> 32) | (p_product[6] & 0xffffffff00000000ull); + l_tmp[2] = p_product[7]; + l_tmp[3] = (p_product[6] >> 32) | (p_product[4] << 32); + l_carry += vli_add(p_result, p_result, l_tmp); + + /* d1 */ + l_tmp[0] = (p_product[5] >> 32) | (p_product[6] << 32); + l_tmp[1] = (p_product[6] >> 32); + l_tmp[2] = 0; + l_tmp[3] = (p_product[4] & 0xffffffff) | (p_product[5] << 32); + l_carry -= vli_sub(p_result, p_result, l_tmp); + + /* d2 */ + l_tmp[0] = p_product[6]; + l_tmp[1] = p_product[7]; + l_tmp[2] = 0; + l_tmp[3] = (p_product[4] >> 32) | (p_product[5] & 0xffffffff00000000ull); + l_carry -= vli_sub(p_result, p_result, l_tmp); + + /* d3 */ + l_tmp[0] = (p_product[6] >> 32) | (p_product[7] << 32); + l_tmp[1] = (p_product[7] >> 32) | (p_product[4] << 32); + l_tmp[2] = (p_product[4] >> 32) | (p_product[5] << 32); + l_tmp[3] = (p_product[6] << 32); + l_carry -= vli_sub(p_result, p_result, l_tmp); + + /* d4 */ + l_tmp[0] = p_product[7]; + l_tmp[1] = p_product[4] & 0xffffffff00000000ull; + l_tmp[2] = p_product[5]; + l_tmp[3] = p_product[6] & 0xffffffff00000000ull; + l_carry -= vli_sub(p_result, p_result, l_tmp); + + if(l_carry < 0) + { + do + { + l_carry += vli_add(p_result, p_result, curve_p); + } while(l_carry < 0); + } + else + { + while(l_carry || vli_cmp(curve_p, p_result) != 1) + { + l_carry -= vli_sub(p_result, p_result, curve_p); + } + } +} + +#elif ECC_CURVE == secp384r1 + +static void omega_mult(uint64_t *p_result, uint64_t *p_right) +{ + uint64_t l_tmp[NUM_ECC_DIGITS]; + uint64_t l_carry, l_diff; + + /* Multiply by (2^128 + 2^96 - 2^32 + 1). */ + vli_set(p_result, p_right); /* 1 */ + l_carry = vli_lshift(l_tmp, p_right, 32); + p_result[1 + NUM_ECC_DIGITS] = l_carry + vli_add(p_result + 1, p_result + 1, l_tmp); /* 2^96 + 1 */ + p_result[2 + NUM_ECC_DIGITS] = vli_add(p_result + 2, p_result + 2, p_right); /* 2^128 + 2^96 + 1 */ + l_carry += vli_sub(p_result, p_result, l_tmp); /* 2^128 + 2^96 - 2^32 + 1 */ + l_diff = p_result[NUM_ECC_DIGITS] - l_carry; + if(l_diff > p_result[NUM_ECC_DIGITS]) + { /* Propagate borrow if necessary. */ + uint i; + for(i = 1 + NUM_ECC_DIGITS; ; ++i) + { + --p_result[i]; + if(p_result[i] != (uint64_t)-1) + { + break; + } + } + } + p_result[NUM_ECC_DIGITS] = l_diff; +} + +/* Computes p_result = p_product % curve_p + see PDF "Comparing Elliptic Curve Cryptography and RSA on 8-bit CPUs" + section "Curve-Specific Optimizations" */ +static void vli_mmod_fast(uint64_t *p_result, uint64_t *p_product) +{ + uint64_t l_tmp[2*NUM_ECC_DIGITS]; + + while(!vli_isZero(p_product + NUM_ECC_DIGITS)) /* While c1 != 0 */ + { + uint64_t l_carry = 0; + uint i; + + vli_clear(l_tmp); + vli_clear(l_tmp + NUM_ECC_DIGITS); + omega_mult(l_tmp, p_product + NUM_ECC_DIGITS); /* tmp = w * c1 */ + vli_clear(p_product + NUM_ECC_DIGITS); /* p = c0 */ + + /* (c1, c0) = c0 + w * c1 */ + for(i=0; i 0) + { + vli_sub(p_product, p_product, curve_p); + } + vli_set(p_result, p_product); +} + +#endif + +/* Computes p_result = (p_left * p_right) % curve_p. */ +static void vli_modMult_fast(uint64_t *p_result, uint64_t *p_left, uint64_t *p_right) +{ + uint64_t l_product[2 * NUM_ECC_DIGITS]; + vli_mult(l_product, p_left, p_right); + vli_mmod_fast(p_result, l_product); +} + +/* Computes p_result = p_left^2 % curve_p. */ +static void vli_modSquare_fast(uint64_t *p_result, uint64_t *p_left) +{ + uint64_t l_product[2 * NUM_ECC_DIGITS]; + vli_square(l_product, p_left); + vli_mmod_fast(p_result, l_product); +} + +#define EVEN(vli) (!(vli[0] & 1)) +/* Computes p_result = (1 / p_input) % p_mod. All VLIs are the same size. + See "From Euclid's GCD to Montgomery Multiplication to the Great Divide" + https://labs.oracle.com/techrep/2001/smli_tr-2001-95.pdf */ +static void vli_modInv(uint64_t *p_result, uint64_t *p_input, uint64_t *p_mod) +{ + uint64_t a[NUM_ECC_DIGITS], b[NUM_ECC_DIGITS], u[NUM_ECC_DIGITS], v[NUM_ECC_DIGITS]; + uint64_t l_carry; + int l_cmpResult; + + if(vli_isZero(p_input)) + { + vli_clear(p_result); + return; + } + + vli_set(a, p_input); + vli_set(b, p_mod); + vli_clear(u); + u[0] = 1; + vli_clear(v); + + while((l_cmpResult = vli_cmp(a, b)) != 0) + { + l_carry = 0; + if(EVEN(a)) + { + vli_rshift1(a); + if(!EVEN(u)) + { + l_carry = vli_add(u, u, p_mod); + } + vli_rshift1(u); + if(l_carry) + { + u[NUM_ECC_DIGITS-1] |= 0x8000000000000000ull; + } + } + else if(EVEN(b)) + { + vli_rshift1(b); + if(!EVEN(v)) + { + l_carry = vli_add(v, v, p_mod); + } + vli_rshift1(v); + if(l_carry) + { + v[NUM_ECC_DIGITS-1] |= 0x8000000000000000ull; + } + } + else if(l_cmpResult > 0) + { + vli_sub(a, a, b); + vli_rshift1(a); + if(vli_cmp(u, v) < 0) + { + vli_add(u, u, p_mod); + } + vli_sub(u, u, v); + if(!EVEN(u)) + { + l_carry = vli_add(u, u, p_mod); + } + vli_rshift1(u); + if(l_carry) + { + u[NUM_ECC_DIGITS-1] |= 0x8000000000000000ull; + } + } + else + { + vli_sub(b, b, a); + vli_rshift1(b); + if(vli_cmp(v, u) < 0) + { + vli_add(v, v, p_mod); + } + vli_sub(v, v, u); + if(!EVEN(v)) + { + l_carry = vli_add(v, v, p_mod); + } + vli_rshift1(v); + if(l_carry) + { + v[NUM_ECC_DIGITS-1] |= 0x8000000000000000ull; + } + } + } + + vli_set(p_result, u); +} + +/* ------ Point operations ------ */ + +/* Returns 1 if p_point is the point at infinity, 0 otherwise. */ +static int EccPoint_isZero(EccPoint *p_point) +{ + return (vli_isZero(p_point->x) && vli_isZero(p_point->y)); +} + +/* Point multiplication algorithm using Montgomery's ladder with co-Z coordinates. +From http://eprint.iacr.org/2011/338.pdf +*/ + +/* Double in place */ +static void EccPoint_double_jacobian(uint64_t *X1, uint64_t *Y1, uint64_t *Z1) +{ + /* t1 = X, t2 = Y, t3 = Z */ + uint64_t t4[NUM_ECC_DIGITS]; + uint64_t t5[NUM_ECC_DIGITS]; + + if(vli_isZero(Z1)) + { + return; + } + + vli_modSquare_fast(t4, Y1); /* t4 = y1^2 */ + vli_modMult_fast(t5, X1, t4); /* t5 = x1*y1^2 = A */ + vli_modSquare_fast(t4, t4); /* t4 = y1^4 */ + vli_modMult_fast(Y1, Y1, Z1); /* t2 = y1*z1 = z3 */ + vli_modSquare_fast(Z1, Z1); /* t3 = z1^2 */ + + vli_modAdd(X1, X1, Z1, curve_p); /* t1 = x1 + z1^2 */ + vli_modAdd(Z1, Z1, Z1, curve_p); /* t3 = 2*z1^2 */ + vli_modSub(Z1, X1, Z1, curve_p); /* t3 = x1 - z1^2 */ + vli_modMult_fast(X1, X1, Z1); /* t1 = x1^2 - z1^4 */ + + vli_modAdd(Z1, X1, X1, curve_p); /* t3 = 2*(x1^2 - z1^4) */ + vli_modAdd(X1, X1, Z1, curve_p); /* t1 = 3*(x1^2 - z1^4) */ + if(vli_testBit(X1, 0)) + { + uint64_t l_carry = vli_add(X1, X1, curve_p); + vli_rshift1(X1); + X1[NUM_ECC_DIGITS-1] |= l_carry << 63; + } + else + { + vli_rshift1(X1); + } + /* t1 = 3/2*(x1^2 - z1^4) = B */ + + vli_modSquare_fast(Z1, X1); /* t3 = B^2 */ + vli_modSub(Z1, Z1, t5, curve_p); /* t3 = B^2 - A */ + vli_modSub(Z1, Z1, t5, curve_p); /* t3 = B^2 - 2A = x3 */ + vli_modSub(t5, t5, Z1, curve_p); /* t5 = A - x3 */ + vli_modMult_fast(X1, X1, t5); /* t1 = B * (A - x3) */ + vli_modSub(t4, X1, t4, curve_p); /* t4 = B * (A - x3) - y1^4 = y3 */ + + vli_set(X1, Z1); + vli_set(Z1, Y1); + vli_set(Y1, t4); +} + +/* Modify (x1, y1) => (x1 * z^2, y1 * z^3) */ +static void apply_z(uint64_t *X1, uint64_t *Y1, uint64_t *Z) +{ + uint64_t t1[NUM_ECC_DIGITS]; + + vli_modSquare_fast(t1, Z); /* z^2 */ + vli_modMult_fast(X1, X1, t1); /* x1 * z^2 */ + vli_modMult_fast(t1, t1, Z); /* z^3 */ + vli_modMult_fast(Y1, Y1, t1); /* y1 * z^3 */ +} + +/* P = (x1, y1) => 2P, (x2, y2) => P' */ +static void XYcZ_initial_double(uint64_t *X1, uint64_t *Y1, uint64_t *X2, uint64_t *Y2, uint64_t *p_initialZ) +{ + uint64_t z[NUM_ECC_DIGITS]; + + vli_set(X2, X1); + vli_set(Y2, Y1); + + vli_clear(z); + z[0] = 1; + if(p_initialZ) + { + vli_set(z, p_initialZ); + } + + apply_z(X1, Y1, z); + + EccPoint_double_jacobian(X1, Y1, z); + + apply_z(X2, Y2, z); +} + +/* Input P = (x1, y1, Z), Q = (x2, y2, Z) + Output P' = (x1', y1', Z3), P + Q = (x3, y3, Z3) + or P => P', Q => P + Q +*/ +static void XYcZ_add(uint64_t *X1, uint64_t *Y1, uint64_t *X2, uint64_t *Y2) +{ + /* t1 = X1, t2 = Y1, t3 = X2, t4 = Y2 */ + uint64_t t5[NUM_ECC_DIGITS]; + + vli_modSub(t5, X2, X1, curve_p); /* t5 = x2 - x1 */ + vli_modSquare_fast(t5, t5); /* t5 = (x2 - x1)^2 = A */ + vli_modMult_fast(X1, X1, t5); /* t1 = x1*A = B */ + vli_modMult_fast(X2, X2, t5); /* t3 = x2*A = C */ + vli_modSub(Y2, Y2, Y1, curve_p); /* t4 = y2 - y1 */ + vli_modSquare_fast(t5, Y2); /* t5 = (y2 - y1)^2 = D */ + + vli_modSub(t5, t5, X1, curve_p); /* t5 = D - B */ + vli_modSub(t5, t5, X2, curve_p); /* t5 = D - B - C = x3 */ + vli_modSub(X2, X2, X1, curve_p); /* t3 = C - B */ + vli_modMult_fast(Y1, Y1, X2); /* t2 = y1*(C - B) */ + vli_modSub(X2, X1, t5, curve_p); /* t3 = B - x3 */ + vli_modMult_fast(Y2, Y2, X2); /* t4 = (y2 - y1)*(B - x3) */ + vli_modSub(Y2, Y2, Y1, curve_p); /* t4 = y3 */ + + vli_set(X2, t5); +} + +/* Input P = (x1, y1, Z), Q = (x2, y2, Z) + Output P + Q = (x3, y3, Z3), P - Q = (x3', y3', Z3) + or P => P - Q, Q => P + Q +*/ +static void XYcZ_addC(uint64_t *X1, uint64_t *Y1, uint64_t *X2, uint64_t *Y2) +{ + /* t1 = X1, t2 = Y1, t3 = X2, t4 = Y2 */ + uint64_t t5[NUM_ECC_DIGITS]; + uint64_t t6[NUM_ECC_DIGITS]; + uint64_t t7[NUM_ECC_DIGITS]; + + vli_modSub(t5, X2, X1, curve_p); /* t5 = x2 - x1 */ + vli_modSquare_fast(t5, t5); /* t5 = (x2 - x1)^2 = A */ + vli_modMult_fast(X1, X1, t5); /* t1 = x1*A = B */ + vli_modMult_fast(X2, X2, t5); /* t3 = x2*A = C */ + vli_modAdd(t5, Y2, Y1, curve_p); /* t4 = y2 + y1 */ + vli_modSub(Y2, Y2, Y1, curve_p); /* t4 = y2 - y1 */ + + vli_modSub(t6, X2, X1, curve_p); /* t6 = C - B */ + vli_modMult_fast(Y1, Y1, t6); /* t2 = y1 * (C - B) */ + vli_modAdd(t6, X1, X2, curve_p); /* t6 = B + C */ + vli_modSquare_fast(X2, Y2); /* t3 = (y2 - y1)^2 */ + vli_modSub(X2, X2, t6, curve_p); /* t3 = x3 */ + + vli_modSub(t7, X1, X2, curve_p); /* t7 = B - x3 */ + vli_modMult_fast(Y2, Y2, t7); /* t4 = (y2 - y1)*(B - x3) */ + vli_modSub(Y2, Y2, Y1, curve_p); /* t4 = y3 */ + + vli_modSquare_fast(t7, t5); /* t7 = (y2 + y1)^2 = F */ + vli_modSub(t7, t7, t6, curve_p); /* t7 = x3' */ + vli_modSub(t6, t7, X1, curve_p); /* t6 = x3' - B */ + vli_modMult_fast(t6, t6, t5); /* t6 = (y2 + y1)*(x3' - B) */ + vli_modSub(Y1, t6, Y1, curve_p); /* t2 = y3' */ + + vli_set(X1, t7); +} + +static void EccPoint_mult(EccPoint *p_result, EccPoint *p_point, uint64_t *p_scalar, uint64_t *p_initialZ) +{ + /* R0 and R1 */ + uint64_t Rx[2][NUM_ECC_DIGITS]; + uint64_t Ry[2][NUM_ECC_DIGITS]; + uint64_t z[NUM_ECC_DIGITS]; + + int i, nb; + + vli_set(Rx[1], p_point->x); + vli_set(Ry[1], p_point->y); + + XYcZ_initial_double(Rx[1], Ry[1], Rx[0], Ry[0], p_initialZ); + + for(i = vli_numBits(p_scalar) - 2; i > 0; --i) + { + nb = !vli_testBit(p_scalar, i); + XYcZ_addC(Rx[1-nb], Ry[1-nb], Rx[nb], Ry[nb]); + XYcZ_add(Rx[nb], Ry[nb], Rx[1-nb], Ry[1-nb]); + } + + nb = !vli_testBit(p_scalar, 0); + XYcZ_addC(Rx[1-nb], Ry[1-nb], Rx[nb], Ry[nb]); + + /* Find final 1/Z value. */ + vli_modSub(z, Rx[1], Rx[0], curve_p); /* X1 - X0 */ + vli_modMult_fast(z, z, Ry[1-nb]); /* Yb * (X1 - X0) */ + vli_modMult_fast(z, z, p_point->x); /* xP * Yb * (X1 - X0) */ + vli_modInv(z, z, curve_p); /* 1 / (xP * Yb * (X1 - X0)) */ + vli_modMult_fast(z, z, p_point->y); /* yP / (xP * Yb * (X1 - X0)) */ + vli_modMult_fast(z, z, Rx[1-nb]); /* Xb * yP / (xP * Yb * (X1 - X0)) */ + /* End 1/Z calculation */ + + XYcZ_add(Rx[nb], Ry[nb], Rx[1-nb], Ry[1-nb]); + + apply_z(Rx[0], Ry[0], z); + + vli_set(p_result->x, Rx[0]); + vli_set(p_result->y, Ry[0]); +} + +static void ecc_bytes2native(uint64_t p_native[NUM_ECC_DIGITS], const uint8_t p_bytes[ECC_BYTES]) +{ + unsigned i; + for(i=0; i> 56; + p_digit[1] = p_native[i] >> 48; + p_digit[2] = p_native[i] >> 40; + p_digit[3] = p_native[i] >> 32; + p_digit[4] = p_native[i] >> 24; + p_digit[5] = p_native[i] >> 16; + p_digit[6] = p_native[i] >> 8; + p_digit[7] = p_native[i]; + } +} + +/* Compute a = sqrt(a) (mod curve_p). */ +static void mod_sqrt(uint64_t a[NUM_ECC_DIGITS]) +{ + unsigned i; + uint64_t p1[NUM_ECC_DIGITS] = {1}; + uint64_t l_result[NUM_ECC_DIGITS] = {1}; + + /* Since curve_p == 3 (mod 4) for all supported curves, we can + compute sqrt(a) = a^((curve_p + 1) / 4) (mod curve_p). */ + vli_add(p1, curve_p, p1); /* p1 = curve_p + 1 */ + for(i = vli_numBits(p1) - 1; i > 1; --i) + { + vli_modSquare_fast(l_result, l_result); + if(vli_testBit(p1, i)) + { + vli_modMult_fast(l_result, l_result, a); + } + } + vli_set(a, l_result); +} + +static void ecc_point_decompress(EccPoint *p_point, const uint8_t p_compressed[ECC_BYTES+1]) +{ + uint64_t _3[NUM_ECC_DIGITS] = {3}; /* -a = 3 */ + ecc_bytes2native(p_point->x, p_compressed+1); + + vli_modSquare_fast(p_point->y, p_point->x); /* y = x^2 */ + vli_modSub(p_point->y, p_point->y, _3, curve_p); /* y = x^2 - 3 */ + vli_modMult_fast(p_point->y, p_point->y, p_point->x); /* y = x^3 - 3x */ + vli_modAdd(p_point->y, p_point->y, curve_b, curve_p); /* y = x^3 - 3x + b */ + + mod_sqrt(p_point->y); + + if((p_point->y[0] & 0x01) != (p_compressed[0] & 0x01)) + { + vli_sub(p_point->y, curve_p, p_point->y); + } +} + +int ecc_make_key(uint8_t p_publicKey[ECC_BYTES+1], uint8_t p_privateKey[ECC_BYTES]) +{ + uint64_t l_private[NUM_ECC_DIGITS]; + EccPoint l_public; + unsigned l_tries = 0; + + do + { + if(!getRandomNumber(l_private) || (l_tries++ >= MAX_TRIES)) + { + return 0; + } + if(vli_isZero(l_private)) + { + continue; + } + + /* Make sure the private key is in the range [1, n-1]. + For the supported curves, n is always large enough that we only need to subtract once at most. */ + if(vli_cmp(curve_n, l_private) != 1) + { + vli_sub(l_private, l_private, curve_n); + } + + EccPoint_mult(&l_public, &curve_G, l_private, NULL); + } while(EccPoint_isZero(&l_public)); + + ecc_native2bytes(p_privateKey, l_private); + ecc_native2bytes(p_publicKey + 1, l_public.x); + p_publicKey[0] = 2 + (l_public.y[0] & 0x01); + return 1; +} + +int ecdh_shared_secret(const uint8_t p_publicKey[ECC_BYTES+1], const uint8_t p_privateKey[ECC_BYTES], uint8_t p_secret[ECC_BYTES]) +{ + EccPoint l_public; + uint64_t l_private[NUM_ECC_DIGITS]; + uint64_t l_random[NUM_ECC_DIGITS]; + + if(!getRandomNumber(l_random)) + { + return 0; + } + + ecc_point_decompress(&l_public, p_publicKey); + ecc_bytes2native(l_private, p_privateKey); + + EccPoint l_product; + EccPoint_mult(&l_product, &l_public, l_private, l_random); + + ecc_native2bytes(p_secret, l_product.x); + + return !EccPoint_isZero(&l_product); +} + +/* -------- ECDSA code -------- */ + +/* Computes p_result = (p_left * p_right) % p_mod. */ +static void vli_modMult(uint64_t *p_result, uint64_t *p_left, uint64_t *p_right, uint64_t *p_mod) +{ + uint64_t l_product[2 * NUM_ECC_DIGITS]; + uint64_t l_modMultiple[2 * NUM_ECC_DIGITS]; + uint l_digitShift, l_bitShift; + uint l_productBits; + uint l_modBits = vli_numBits(p_mod); + + vli_mult(l_product, p_left, p_right); + l_productBits = vli_numBits(l_product + NUM_ECC_DIGITS); + if(l_productBits) + { + l_productBits += NUM_ECC_DIGITS * 64; + } + else + { + l_productBits = vli_numBits(l_product); + } + + if(l_productBits < l_modBits) + { /* l_product < p_mod. */ + vli_set(p_result, l_product); + return; + } + + /* Shift p_mod by (l_leftBits - l_modBits). This multiplies p_mod by the largest + power of two possible while still resulting in a number less than p_left. */ + vli_clear(l_modMultiple); + vli_clear(l_modMultiple + NUM_ECC_DIGITS); + l_digitShift = (l_productBits - l_modBits) / 64; + l_bitShift = (l_productBits - l_modBits) % 64; + if(l_bitShift) + { + l_modMultiple[l_digitShift + NUM_ECC_DIGITS] = vli_lshift(l_modMultiple + l_digitShift, p_mod, l_bitShift); + } + else + { + vli_set(l_modMultiple + l_digitShift, p_mod); + } + + /* Subtract all multiples of p_mod to get the remainder. */ + vli_clear(p_result); + p_result[0] = 1; /* Use p_result as a temp var to store 1 (for subtraction) */ + while(l_productBits > NUM_ECC_DIGITS * 64 || vli_cmp(l_modMultiple, p_mod) >= 0) + { + int l_cmp = vli_cmp(l_modMultiple + NUM_ECC_DIGITS, l_product + NUM_ECC_DIGITS); + if(l_cmp < 0 || (l_cmp == 0 && vli_cmp(l_modMultiple, l_product) <= 0)) + { + if(vli_sub(l_product, l_product, l_modMultiple)) + { /* borrow */ + vli_sub(l_product + NUM_ECC_DIGITS, l_product + NUM_ECC_DIGITS, p_result); + } + vli_sub(l_product + NUM_ECC_DIGITS, l_product + NUM_ECC_DIGITS, l_modMultiple + NUM_ECC_DIGITS); + } + uint64_t l_carry = (l_modMultiple[NUM_ECC_DIGITS] & 0x01) << 63; + vli_rshift1(l_modMultiple + NUM_ECC_DIGITS); + vli_rshift1(l_modMultiple); + l_modMultiple[NUM_ECC_DIGITS-1] |= l_carry; + + --l_productBits; + } + vli_set(p_result, l_product); +} + +static uint umax(uint a, uint b) +{ + return (a > b ? a : b); +} + +int ecdsa_sign(const uint8_t p_privateKey[ECC_BYTES], const uint8_t p_hash[ECC_BYTES], uint8_t p_signature[ECC_BYTES*2]) +{ + uint64_t k[NUM_ECC_DIGITS]; + uint64_t l_tmp[NUM_ECC_DIGITS]; + uint64_t l_s[NUM_ECC_DIGITS]; + EccPoint p; + unsigned l_tries = 0; + + do + { + if(!getRandomNumber(k) || (l_tries++ >= MAX_TRIES)) + { + return 0; + } + if(vli_isZero(k)) + { + continue; + } + + if(vli_cmp(curve_n, k) != 1) + { + vli_sub(k, k, curve_n); + } + + /* tmp = k * G */ + EccPoint_mult(&p, &curve_G, k, NULL); + + /* r = x1 (mod n) */ + if(vli_cmp(curve_n, p.x) != 1) + { + vli_sub(p.x, p.x, curve_n); + } + } while(vli_isZero(p.x)); + + ecc_native2bytes(p_signature, p.x); + + ecc_bytes2native(l_tmp, p_privateKey); + vli_modMult(l_s, p.x, l_tmp, curve_n); /* s = r*d */ + ecc_bytes2native(l_tmp, p_hash); + vli_modAdd(l_s, l_tmp, l_s, curve_n); /* s = e + r*d */ + vli_modInv(k, k, curve_n); /* k = 1 / k */ + vli_modMult(l_s, l_s, k, curve_n); /* s = (e + r*d) / k */ + ecc_native2bytes(p_signature + ECC_BYTES, l_s); + + return 1; +} + +int ecdsa_verify(const uint8_t p_publicKey[ECC_BYTES+1], const uint8_t p_hash[ECC_BYTES], const uint8_t p_signature[ECC_BYTES*2]) +{ + uint64_t u1[NUM_ECC_DIGITS], u2[NUM_ECC_DIGITS]; + uint64_t z[NUM_ECC_DIGITS]; + EccPoint l_public, l_sum; + uint64_t rx[NUM_ECC_DIGITS]; + uint64_t ry[NUM_ECC_DIGITS]; + uint64_t tx[NUM_ECC_DIGITS]; + uint64_t ty[NUM_ECC_DIGITS]; + uint64_t tz[NUM_ECC_DIGITS]; + + uint64_t l_r[NUM_ECC_DIGITS], l_s[NUM_ECC_DIGITS]; + + ecc_point_decompress(&l_public, p_publicKey); + ecc_bytes2native(l_r, p_signature); + ecc_bytes2native(l_s, p_signature + ECC_BYTES); + + if(vli_isZero(l_r) || vli_isZero(l_s)) + { /* r, s must not be 0. */ + return 0; + } + + if(vli_cmp(curve_n, l_r) != 1 || vli_cmp(curve_n, l_s) != 1) + { /* r, s must be < n. */ + return 0; + } + + /* Calculate u1 and u2. */ + vli_modInv(z, l_s, curve_n); /* Z = s^-1 */ + ecc_bytes2native(u1, p_hash); + vli_modMult(u1, u1, z, curve_n); /* u1 = e/s */ + vli_modMult(u2, l_r, z, curve_n); /* u2 = r/s */ + + /* Calculate l_sum = G + Q. */ + vli_set(l_sum.x, l_public.x); + vli_set(l_sum.y, l_public.y); + vli_set(tx, curve_G.x); + vli_set(ty, curve_G.y); + vli_modSub(z, l_sum.x, tx, curve_p); /* Z = x2 - x1 */ + XYcZ_add(tx, ty, l_sum.x, l_sum.y); + vli_modInv(z, z, curve_p); /* Z = 1/Z */ + apply_z(l_sum.x, l_sum.y, z); + + /* Use Shamir's trick to calculate u1*G + u2*Q */ + EccPoint *l_points[4] = {NULL, &curve_G, &l_public, &l_sum}; + uint l_numBits = umax(vli_numBits(u1), vli_numBits(u2)); + + EccPoint *l_point = l_points[(!!vli_testBit(u1, l_numBits-1)) | ((!!vli_testBit(u2, l_numBits-1)) << 1)]; + vli_set(rx, l_point->x); + vli_set(ry, l_point->y); + vli_clear(z); + z[0] = 1; + + int i; + for(i = l_numBits - 2; i >= 0; --i) + { + EccPoint_double_jacobian(rx, ry, z); + + int l_index = (!!vli_testBit(u1, i)) | ((!!vli_testBit(u2, i)) << 1); + EccPoint *l_point = l_points[l_index]; + if(l_point) + { + vli_set(tx, l_point->x); + vli_set(ty, l_point->y); + apply_z(tx, ty, z); + vli_modSub(tz, rx, tx, curve_p); /* Z = x2 - x1 */ + XYcZ_add(tx, ty, rx, ry); + vli_modMult_fast(z, z, tz); + } + } + + vli_modInv(z, z, curve_p); /* Z = 1/Z */ + apply_z(rx, ry, z); + + /* v = x1 (mod n) */ + if(vli_cmp(curve_n, rx) != 1) + { + vli_sub(rx, rx, curve_n); + } + + /* Accept only if v == r. */ + return (vli_cmp(rx, l_r) == 0); +} diff --git a/lib/crypt/ecc.h b/lib/crypt/ecc.h new file mode 100644 index 000000000..00fa3539f --- /dev/null +++ b/lib/crypt/ecc.h @@ -0,0 +1,90 @@ +/* From https://github.com/jestan/easy-ecc */ + +#ifndef _EASY_ECC_H_ +#define _EASY_ECC_H_ + +#include + +/* Curve selection options. */ +#define secp128r1 16 +#define secp192r1 24 +#define secp256r1 32 +#define secp384r1 48 +#ifndef ECC_CURVE + #define ECC_CURVE secp256r1 +#endif + +#if (ECC_CURVE != secp128r1 && ECC_CURVE != secp192r1 && ECC_CURVE != secp256r1 && ECC_CURVE != secp384r1) + #error "Must define ECC_CURVE to one of the available curves" +#endif + +#define ECC_BYTES ECC_CURVE + +#ifdef __cplusplus +extern "C" +{ +#endif + +/* ecc_make_key() function. +Create a public/private key pair. + +Outputs: + p_publicKey - Will be filled in with the public key. + p_privateKey - Will be filled in with the private key. + +Returns 1 if the key pair was generated successfully, 0 if an error occurred. +*/ +int ecc_make_key(uint8_t p_publicKey[ECC_BYTES+1], uint8_t p_privateKey[ECC_BYTES]); + +/* ecdh_shared_secret() function. +Compute a shared secret given your secret key and someone else's public key. +Note: It is recommended that you hash the result of ecdh_shared_secret before using it for symmetric encryption or HMAC. + +Inputs: + p_publicKey - The public key of the remote party. + p_privateKey - Your private key. + +Outputs: + p_secret - Will be filled in with the shared secret value. + +Returns 1 if the shared secret was generated successfully, 0 if an error occurred. +*/ +int ecdh_shared_secret(const uint8_t p_publicKey[ECC_BYTES+1], const uint8_t p_privateKey[ECC_BYTES], uint8_t p_secret[ECC_BYTES]); + +/* ecdsa_sign() function. +Generate an ECDSA signature for a given hash value. + +Usage: Compute a hash of the data you wish to sign (SHA-2 is recommended) and pass it in to +this function along with your private key. + +Inputs: + p_privateKey - Your private key. + p_hash - The message hash to sign. + +Outputs: + p_signature - Will be filled in with the signature value. + +Returns 1 if the signature generated successfully, 0 if an error occurred. +*/ +int ecdsa_sign(const uint8_t p_privateKey[ECC_BYTES], const uint8_t p_hash[ECC_BYTES], uint8_t p_signature[ECC_BYTES*2]); + +/* ecdsa_verify() function. +Verify an ECDSA signature. + +Usage: Compute the hash of the signed data using the same hash as the signer and +pass it to this function along with the signer's public key and the signature values (r and s). + +Inputs: + p_publicKey - The signer's public key + p_hash - The hash of the signed data. + p_signature - The signature value. + +Returns 1 if the signature is valid, 0 if it is invalid. +*/ +int ecdsa_verify(const uint8_t p_publicKey[ECC_BYTES+1], const uint8_t p_hash[ECC_BYTES], const uint8_t p_signature[ECC_BYTES*2]); + +#ifdef __cplusplus +} /* end of extern "C" */ +#endif + +#endif /* _EASY_ECC_H_ */ diff --git a/lib/crypt/meson.build b/lib/crypt/meson.build index e22601e50..b501cf5c2 100644 --- a/lib/crypt/meson.build +++ b/lib/crypt/meson.build @@ -29,6 +29,7 @@ libcrypt_sources = files(''' zuc.h kasumi.h ogs-kdf.h + ecc.h ogs-aes.c ogs-aes-cmac.c @@ -43,6 +44,9 @@ libcrypt_sources = files(''' ogs-kdf.c ogs-base64.c + + curve25519-donna.c + ecc.c '''.split()) libcrypt_inc = include_directories('.') diff --git a/lib/crypt/ogs-base64.c b/lib/crypt/ogs-base64.c index de92b4622..b8b7a7df1 100644 --- a/lib/crypt/ogs-base64.c +++ b/lib/crypt/ogs-base64.c @@ -15,7 +15,30 @@ */ /* - * Copyright (C) 2019-2020 by Sukchan Lee + * Copyright (C) 2000-2012 Free Software Foundation, Inc. + * Copyright (C) 2017 Red Hat, Inc. + * + * Author: Nikos Mavrogiannopoulos + * + * This file is part of GnuTLS. + * + * The GnuTLS is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see + * + */ + +/* + * Copyright (C) 2019-2022 by Sukchan Lee * * This file is part of Open5GS. * @@ -175,3 +198,254 @@ int ogs_base64_encode_binary( *p++ = '\0'; return (int)(p - encoded); } + +/* copies data to result but removes newlines and + * returns the size of the data copied. + * + * It will fail with GNUTLS_E_BASE64_DECODING_ERROR if the + * end-result is the empty string. + */ +inline static int +cpydata(const uint8_t *data, int data_size, uint8_t **result, int *result_size) +{ + int i, j; + + *result = ogs_malloc(data_size + 1); + if (*result == NULL) { + ogs_error("ogs_malloc failed[%d]", data_size+1); + return OGS_ERROR; + } + + for (j = i = 0; i < data_size; i++) { + if (data[i] == '\n' || data[i] == '\r' || data[i] == ' ' + || data[i] == '\t') + continue; + else if (data[i] == '-') + break; + (*result)[j] = data[i]; + j++; + } + + *result_size = j; + (*result)[j] = 0; + + if (j==0) { + ogs_free(*result); + ogs_error("No Data"); + return OGS_ERROR; + } + + return j; +} + +#define MEMSUB(x,y) ((ssize_t)((ptrdiff_t)x-(ptrdiff_t)y)) +#define ENDSTR "-----" +int ogs_fbase64_decode(const char *header, + const uint8_t *data, size_t data_size, ogs_datum_t *result) +{ + static const char top[] = "-----BEGIN "; + static const char bottom[] = "-----END "; + uint8_t *rdata, *kdata; + int rdata_size; +#define MAX_PEM_HEADER_LEN 128 + char pem_header[MAX_PEM_HEADER_LEN]; + + uint8_t *bufcoded; + int bufcoded_len; + char *p, *last; + + ogs_assert(header); + ogs_assert(data); + ogs_assert(data_size); + ogs_assert(result); + + memset(result, 0, sizeof(*result)); + + p = pem_header; + last = pem_header + MAX_PEM_HEADER_LEN; + + p = ogs_slprintf(p, last, "%s", top); + if (header != NULL) + p = ogs_slprintf(p, last, "%s", header); + + rdata = memmem(data, data_size, pem_header, strlen(pem_header)); + if (rdata == NULL) { + ogs_error("Cound not find [%s]", pem_header); + ogs_log_hexdump(OGS_LOG_ERROR, data, data_size); + return OGS_ERROR; + } + + data_size -= MEMSUB(rdata, data); + + if (data_size < 4 + strlen(bottom)) { + ogs_error("Not enough data (%d < 4 + %d)", + (int)data_size, (int)strlen(bottom)); + ogs_log_hexdump(OGS_LOG_ERROR, data, data_size); + return OGS_ERROR; + } + + kdata = + memmem(rdata + 1, data_size - 1, ENDSTR, sizeof(ENDSTR) - 1); + /* allow CR as well. + */ + if (kdata == NULL) { + ogs_error("Cound not find [%s]", ENDSTR); + ogs_log_hexdump(OGS_LOG_ERROR, data, data_size); + return OGS_ERROR; + } + data_size -= strlen(ENDSTR); + data_size -= MEMSUB(kdata, rdata); + + rdata = kdata + strlen(ENDSTR); + + /* position is now after the ---BEGIN--- headers */ + + kdata = memmem(rdata, data_size, bottom, strlen(bottom)); + if (kdata == NULL) { + ogs_error("Cound not find [%s]", bottom); + ogs_log_hexdump(OGS_LOG_ERROR, data, data_size); + return OGS_ERROR; + } + + /* position of kdata is before the ----END--- footer + */ + rdata_size = MEMSUB(kdata, rdata); + + if (rdata_size < 4) { + ogs_error("Not enough data [%d]", rdata_size); + ogs_log_hexdump(OGS_LOG_ERROR, data, data_size); + return OGS_ERROR; + } + + if (cpydata(rdata, rdata_size, &bufcoded, &bufcoded_len) == OGS_ERROR) { + ogs_error("cpydata() failed"); + ogs_log_hexdump(OGS_LOG_ERROR, rdata, rdata_size); + return OGS_ERROR; + } + + result->data = ogs_calloc(1, bufcoded_len); + if (result->data == NULL) { + ogs_error("ogs_calloc() failed [%d]", bufcoded_len); + ogs_free(bufcoded); + return OGS_ERROR; + } + + result->size = ogs_base64_decode_binary( + result->data, (const char *)bufcoded); + if (result->size == 0) { + ogs_error("ogs_base64_decode_binary() failed"); + ogs_log_hexdump(OGS_LOG_ERROR, bufcoded, bufcoded_len); + ogs_free(bufcoded); + return OGS_ERROR; + } + + ogs_free(bufcoded); + + return OGS_OK; +} + +int ogs_pem_decode_curve25519_key(const char *filename, uint8_t *key) +{ + int rv; + + uint8_t buf[OGS_HUGE_LEN]; + size_t bytes_read; + ogs_datum_t result; + +#define CURVE25519_HEADER_LEN 16 + const char *_header = "302e0201 00300506 032b656e 04220420"; + uint8_t header[CURVE25519_HEADER_LEN]; + + ogs_assert(filename); + ogs_assert(key); + + rv = ogs_file_read_full(filename, buf, OGS_HUGE_LEN, &bytes_read); + if (rv != OGS_OK) { + ogs_error("ogs_file_read_full[%s] failed", filename); + return OGS_ERROR; + } + + rv = ogs_fbase64_decode("PRIVATE KEY", buf, bytes_read, &result); + if (rv != OGS_OK) { + ogs_error("ogs_fbase64_decode[%s] failed", filename); + ogs_log_hexdump(OGS_LOG_ERROR, result.data, result.size); + return OGS_ERROR; + } + + rv = OGS_OK; + + if (result.size != 48) { + ogs_error("Invalid size [%d]", (int)result.size); + rv = OGS_ERROR; + goto cleanup; + } + + if (memcmp(ogs_hex_from_string(_header, header, sizeof(header)), + result.data, CURVE25519_HEADER_LEN) != 0) { + ogs_error("Invalid header [%d]", (int)result.size); + ogs_log_hexdump(OGS_LOG_FATAL, result.data, result.size); + rv = OGS_ERROR; + goto cleanup; + } + + memcpy(key, result.data+CURVE25519_HEADER_LEN, OGS_ECCKEY_LEN); + +cleanup: + if (result.data) + ogs_free(result.data); + + return rv; +} + +int ogs_pem_decode_secp256r1_key(const char *filename, uint8_t *key) +{ + int rv; + + uint8_t buf[OGS_HUGE_LEN]; + size_t bytes_read; + ogs_datum_t result; + +#define SECP256R1_HEADER_LEN 7 + const char *_header = "30770201 010420"; + uint8_t header[SECP256R1_HEADER_LEN]; + + ogs_assert(filename); + ogs_assert(key); + + rv = ogs_file_read_full(filename, buf, OGS_HUGE_LEN, &bytes_read); + if (rv != OGS_OK) { + ogs_error("ogs_file_read_full[%s] failed", filename); + return OGS_ERROR; + } + + rv = ogs_fbase64_decode("EC PRIVATE KEY", buf, bytes_read, &result); + if (rv != OGS_OK) { + ogs_error("ogs_fbase64_decode[%s] failed", filename); + ogs_log_hexdump(OGS_LOG_ERROR, result.data, result.size); + return OGS_ERROR; + } + + rv = OGS_OK; + + if (result.size != 121) { + ogs_error("Invalid size [%d]", (int)result.size); + rv = OGS_ERROR; + goto cleanup; + } + + if (memcmp(ogs_hex_from_string(_header, header, sizeof(header)), + result.data, SECP256R1_HEADER_LEN) != 0) { + ogs_error("Invalid header [%d]", (int)result.size); + ogs_log_hexdump(OGS_LOG_FATAL, result.data, result.size); + rv = OGS_ERROR; + goto cleanup; + } + + memcpy(key, result.data+SECP256R1_HEADER_LEN, OGS_ECCKEY_LEN); + +cleanup: + if (result.data) + ogs_free(result.data); + + return rv; +} diff --git a/lib/crypt/ogs-base64.h b/lib/crypt/ogs-base64.h index 59fe61233..186c6cf71 100644 --- a/lib/crypt/ogs-base64.h +++ b/lib/crypt/ogs-base64.h @@ -15,7 +15,29 @@ */ /* - * Copyright (C) 2019-2020 by Sukchan Lee + * Copyright (C) 2000-2012 Free Software Foundation, Inc. + * + * Author: Nikos Mavrogiannopoulos + * + * This file is part of GnuTLS. + * + * The GnuTLS is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see + * + */ + +/* + * Copyright (C) 2019-2022 by Sukchan Lee * * This file is part of Open5GS. * @@ -54,6 +76,12 @@ int ogs_base64_encode( int ogs_base64_encode_binary( char *encoded, const unsigned char *string, int len); +int ogs_fbase64_decode(const char *header, + const uint8_t *data, size_t data_size, ogs_datum_t *result); + +int ogs_pem_decode_curve25519_key(const char *filename, uint8_t *key); +int ogs_pem_decode_secp256r1_key(const char *filename, uint8_t *key); + #ifdef __cplusplus } #endif diff --git a/lib/crypt/ogs-crypt.h b/lib/crypt/ogs-crypt.h index 69f294547..659d460ad 100644 --- a/lib/crypt/ogs-crypt.h +++ b/lib/crypt/ogs-crypt.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2019 by Sukchan Lee + * Copyright (C) 2019-2022 by Sukchan Lee * * This file is part of Open5GS. * @@ -35,6 +35,7 @@ #include "crypt/snow-3g.h" #include "crypt/zuc.h" #include "crypt/kasumi.h" +#include "crypt/ecc.h" #include "crypt/ogs-kdf.h" #include "crypt/ogs-base64.h" @@ -46,6 +47,7 @@ extern "C" { #endif #define OGS_KEY_LEN 16 +#define OGS_IVEC_LEN 16 #define OGS_AMF_LEN 2 #define OGS_RAND_LEN 16 #define OGS_AUTN_LEN 16 @@ -60,8 +62,14 @@ extern "C" { #define OGS_HASH_MME_LEN 8 +#define OGS_ECCKEY_LEN 32 +#define OGS_MACTAG_LEN 8 + #define OGS_KEYSTRLEN(x) ((x*2)+1) +int curve25519_donna( + uint8_t *mypublic, const uint8_t *secret, const uint8_t *basepoint); + #ifdef __cplusplus } #endif diff --git a/lib/crypt/ogs-kdf.c b/lib/crypt/ogs-kdf.c index b741908cc..8d3d84607 100644 --- a/lib/crypt/ogs-kdf.c +++ b/lib/crypt/ogs-kdf.c @@ -262,6 +262,43 @@ void ogs_kdf_nh_gnb(uint8_t *kamf, uint8_t *sync_input, uint8_t *kgnb) FC_FOR_NH_GNB_DERIVATION, param, kgnb); } +/* + * TS33.501 Annex C.3.4.1 Profile A + * TS33.501 Annex C.3.4.2 Profile B + * ANSI-X9.63-KDF + */ +void ogs_kdf_ansi_x963( + uint8_t *z, size_t z_len, uint8_t *info, size_t info_len, + uint8_t *ek, uint8_t *icb, uint8_t *mk) +{ + uint8_t input[ECC_BYTES+4+ECC_BYTES+1]; + uint8_t output[OGS_KEY_LEN+OGS_IVEC_LEN+OGS_SHA256_DIGEST_SIZE]; + uint32_t counter = 0; + size_t counter_len = sizeof(counter); + + ogs_assert(z); + ogs_assert(info); + ogs_assert(ek); + ogs_assert(icb); + ogs_assert(mk); + + ogs_assert((z_len+counter_len+info_len) <= (ECC_BYTES+4+ECC_BYTES+1)); + + memcpy(input, z, z_len); + counter = htobe32(1); + memcpy(input+z_len, &counter, counter_len); + memcpy(input+z_len+counter_len, info, info_len); + + ogs_sha256(input, z_len+counter_len+info_len, output); + memcpy(ek, output, OGS_KEY_LEN); + memcpy(icb, output+OGS_KEY_LEN, OGS_IVEC_LEN); + + counter = htobe32(2); + memcpy(input+z_len, &counter, counter_len); + + ogs_sha256(input, z_len+counter_len+info_len, mk); +} + /* TS33.401 Annex A.2 KASME derivation function */ void ogs_auc_kasme(const uint8_t *ck, const uint8_t *ik, const uint8_t *plmn_id, const uint8_t *sqn, const uint8_t *ak, diff --git a/lib/crypt/ogs-kdf.h b/lib/crypt/ogs-kdf.h index c6ed7b5c9..36286b07a 100644 --- a/lib/crypt/ogs-kdf.h +++ b/lib/crypt/ogs-kdf.h @@ -77,6 +77,15 @@ void ogs_kdf_kgnb_and_kn3iwf(uint8_t *kamf, uint32_t ul_count, /* TS33.501 Annex A.10 NH derivation function */ void ogs_kdf_nh_gnb(uint8_t *kamf, uint8_t *sync_input, uint8_t *kgnb); +/* + * TS33.501 Annex C.3.4.1 Profile A + * TS33.501 Annex C.3.4.2 Profile B + * ANSI-X9.63-KDF + */ +void ogs_kdf_ansi_x963( + uint8_t *z, size_t z_len, uint8_t *info, size_t info_len, + uint8_t *ek, uint8_t *icb, uint8_t *mk); + /* TS33.401 Annex A.2 KASME derivation function */ void ogs_auc_kasme(const uint8_t *ck, const uint8_t *ik, const uint8_t *plmn_id, const uint8_t *sqn, const uint8_t *ak, diff --git a/lib/dbi/subscription.c b/lib/dbi/subscription.c index 4b265037d..be72cda9c 100644 --- a/lib/dbi/subscription.c +++ b/lib/dbi/subscription.c @@ -80,20 +80,25 @@ int ogs_dbi_auth_info(char *supi, ogs_dbi_auth_info_t *auth_info) if (!strcmp(key, "k") && BSON_ITER_HOLDS_UTF8(&inner_iter)) { utf8 = (char *)bson_iter_utf8(&inner_iter, &length); - memcpy(auth_info->k, OGS_HEX(utf8, length, buf), OGS_KEY_LEN); + ogs_ascii_to_hex(utf8, length, buf, sizeof(buf)); + memcpy(auth_info->k, buf, OGS_KEY_LEN); } else if (!strcmp(key, "opc") && BSON_ITER_HOLDS_UTF8(&inner_iter)) { utf8 = (char *)bson_iter_utf8(&inner_iter, &length); auth_info->use_opc = 1; - memcpy(auth_info->opc, OGS_HEX(utf8, length, buf), OGS_KEY_LEN); + ogs_ascii_to_hex(utf8, length, buf, sizeof(buf)); + memcpy(auth_info->opc, buf, OGS_KEY_LEN); } else if (!strcmp(key, "op") && BSON_ITER_HOLDS_UTF8(&inner_iter)) { utf8 = (char *)bson_iter_utf8(&inner_iter, &length); - memcpy(auth_info->op, OGS_HEX(utf8, length, buf), OGS_KEY_LEN); + ogs_ascii_to_hex(utf8, length, buf, sizeof(buf)); + memcpy(auth_info->op, buf, OGS_KEY_LEN); } else if (!strcmp(key, "amf") && BSON_ITER_HOLDS_UTF8(&inner_iter)) { utf8 = (char *)bson_iter_utf8(&inner_iter, &length); - memcpy(auth_info->amf, OGS_HEX(utf8, length, buf), OGS_AMF_LEN); + ogs_ascii_to_hex(utf8, length, buf, sizeof(buf)); + memcpy(auth_info->amf, buf, OGS_AMF_LEN); } else if (!strcmp(key, "rand") && BSON_ITER_HOLDS_UTF8(&inner_iter)) { utf8 = (char *)bson_iter_utf8(&inner_iter, &length); - memcpy(auth_info->rand, OGS_HEX(utf8, length, buf), OGS_RAND_LEN); + ogs_ascii_to_hex(utf8, length, buf, sizeof(buf)); + memcpy(auth_info->rand, buf, OGS_RAND_LEN); } else if (!strcmp(key, "sqn") && BSON_ITER_HOLDS_INT64(&inner_iter)) { auth_info->sqn = bson_iter_int64(&inner_iter); } diff --git a/lib/nas/5gs/conv.c b/lib/nas/5gs/conv.c index 4cc26290d..14e9334f1 100644 --- a/lib/nas/5gs/conv.c +++ b/lib/nas/5gs/conv.c @@ -24,9 +24,12 @@ void ogs_nas_5gs_imsi_to_bcd( { ogs_nas_5gs_mobile_identity_suci_t *mobile_identity_suci = NULL; ogs_plmn_id_t plmn_id; - char tmp[OGS_MAX_IMSI_BCD_LEN+1]; char *p, *last; + uint8_t *scheme_output = NULL; + int scheme_output_size = 0; + char *scheme_output_bcd = NULL; + ogs_assert(mobile_identity); ogs_assert(imsi_bcd); @@ -45,10 +48,19 @@ void ogs_nas_5gs_imsi_to_bcd( p = ogs_slprintf(p, last, "%03d%03d", ogs_plmn_id_mcc(&plmn_id), ogs_plmn_id_mnc(&plmn_id)); - ogs_assert(mobile_identity->length > 8); - ogs_buffer_to_bcd(mobile_identity_suci->scheme_output, - mobile_identity->length - 8, tmp); - p = ogs_slprintf(p, last, "%s", tmp); + scheme_output = + (uint8_t *)mobile_identity->buffer + + OGS_NAS_5GS_MOBILE_IDENTITY_SUCI_MIN_SIZE; + scheme_output_size = + mobile_identity->length - OGS_NAS_5GS_MOBILE_IDENTITY_SUCI_MIN_SIZE; + ogs_assert(scheme_output_size); + scheme_output_bcd = ogs_calloc(1, scheme_output_size*2+1); + ogs_assert(scheme_output_bcd); + + ogs_buffer_to_bcd(scheme_output, scheme_output_size, scheme_output_bcd); + p = ogs_slprintf(p, last, "%s", scheme_output_bcd); + + ogs_free(scheme_output_bcd); } char *ogs_nas_5gs_suci_from_mobile_identity( @@ -56,10 +68,12 @@ char *ogs_nas_5gs_suci_from_mobile_identity( { ogs_nas_5gs_mobile_identity_suci_t *mobile_identity_suci = NULL; ogs_plmn_id_t plmn_id; - char tmp[OGS_NAS_MAX_SCHEME_OUTPUT_LEN*2+1]; char routing_indicator[5]; char *suci = NULL; - int scheme_output_len = 0; + + uint8_t *scheme_output = NULL; + int scheme_output_size = 0; + char *scheme_output_string_or_bcd = NULL; ogs_assert(mobile_identity); @@ -70,9 +84,9 @@ char *ogs_nas_5gs_suci_from_mobile_identity( ogs_expect_or_return_val(mobile_identity_suci->h.supi_format == OGS_NAS_5GS_SUPI_FORMAT_IMSI, NULL); ogs_expect_or_return_val(mobile_identity_suci->protection_scheme_id == - OGS_NAS_5GS_NULL_SCHEME || mobile_identity_suci->protection_scheme_id == - OGS_NAS_5GS_ECIES_SCHEME_PROFILE_A || mobile_identity_suci->protection_scheme_id == - OGS_NAS_5GS_ECIES_SCHEME_PROFILE_B, NULL); + OGS_PROTECTION_SCHEME_NULL || mobile_identity_suci->protection_scheme_id == + OGS_PROTECTION_SCHEME_PROFILE_A || mobile_identity_suci->protection_scheme_id == + OGS_PROTECTION_SCHEME_PROFILE_B, NULL); suci = ogs_msprintf("suci-%d-", mobile_identity_suci->h.supi_format); ogs_expect_or_return_val(suci, NULL); @@ -116,26 +130,33 @@ char *ogs_nas_5gs_suci_from_mobile_identity( routing_indicator[0] = '0'; } - scheme_output_len = mobile_identity->length - 8; - ogs_expect_or_return_val(scheme_output_len > 0, NULL); - ogs_expect_or_return_val( - scheme_output_len <= OGS_NAS_MAX_SCHEME_OUTPUT_LEN, NULL); + scheme_output = + (uint8_t *)mobile_identity->buffer + + OGS_NAS_5GS_MOBILE_IDENTITY_SUCI_MIN_SIZE; + scheme_output_size = + mobile_identity->length - OGS_NAS_5GS_MOBILE_IDENTITY_SUCI_MIN_SIZE; + ogs_assert(scheme_output_size); + scheme_output_string_or_bcd = ogs_calloc(1, scheme_output_size*2+1); + ogs_assert(scheme_output_string_or_bcd); - if (mobile_identity_suci->protection_scheme_id != OGS_NAS_5GS_NULL_SCHEME) { - ogs_hex_to_ascii(mobile_identity_suci->scheme_output, - scheme_output_len, tmp, sizeof(tmp)); + if (mobile_identity_suci->protection_scheme_id != + OGS_PROTECTION_SCHEME_NULL) { + ogs_hex_to_ascii(scheme_output, scheme_output_size, + scheme_output_string_or_bcd, scheme_output_size*2+1); } else { - ogs_buffer_to_bcd(mobile_identity_suci->scheme_output, - scheme_output_len, tmp); + ogs_buffer_to_bcd(scheme_output, scheme_output_size, + scheme_output_string_or_bcd); } suci = ogs_mstrcatf(suci, "%s-%d-%d-%s", routing_indicator, mobile_identity_suci->protection_scheme_id, mobile_identity_suci->home_network_pki_value, - tmp); + scheme_output_string_or_bcd); ogs_expect(suci); + ogs_free(scheme_output_string_or_bcd); + return suci; } diff --git a/lib/nas/5gs/types.h b/lib/nas/5gs/types.h index a3fce972d..2d493dec5 100644 --- a/lib/nas/5gs/types.h +++ b/lib/nas/5gs/types.h @@ -191,7 +191,6 @@ ED3(uint8_t type:4;, /* 9.11.3.4 5GS mobile identity * M LV-E 6-n */ -#define OGS_NAS_MAX_SCHEME_OUTPUT_LEN 64 typedef struct ogs_nas_5gs_mobile_identity_header_s { #define OGS_NAS_5GS_SUPI_FORMAT_IMSI 0 #define OGS_NAS_5GS_SUPI_FORMAT_NETWORK_SPECIFIC_IDENTIFIER 1 @@ -199,6 +198,7 @@ ED3(uint8_t supi_format:4;, uint8_t odd_even:1;, uint8_t type:3;) } __attribute__ ((packed)) ogs_nas_5gs_mobile_identity_header_t; +#define OGS_NAS_5GS_MOBILE_IDENTITY_SUCI_MIN_SIZE 8 typedef struct ogs_nas_5gs_mobile_identity_suci_s { ogs_nas_5gs_mobile_identity_header_t h; ogs_nas_plmn_id_t nas_plmn_id; @@ -207,12 +207,8 @@ typedef struct ogs_nas_5gs_mobile_identity_suci_s { ED2(uint8_t routing_indicator4:4;, uint8_t routing_indicator3:4;) ED2(uint8_t spare3:4;, -#define OGS_NAS_5GS_NULL_SCHEME 0 -#define OGS_NAS_5GS_ECIES_SCHEME_PROFILE_A 1 -#define OGS_NAS_5GS_ECIES_SCHEME_PROFILE_B 2 uint8_t protection_scheme_id:4;) uint8_t home_network_pki_value; - uint8_t scheme_output[OGS_NAS_MAX_SCHEME_OUTPUT_LEN]; } __attribute__ ((packed)) ogs_nas_5gs_mobile_identity_suci_t; typedef struct ogs_nas_5gs_mobile_identity_guti_s { ogs_nas_5gs_mobile_identity_header_t h; diff --git a/lib/proto/types.c b/lib/proto/types.c index 97f46d2a2..c20a938cb 100644 --- a/lib/proto/types.c +++ b/lib/proto/types.c @@ -143,7 +143,7 @@ ogs_amf_id_t *ogs_amf_id_from_string(ogs_amf_id_t *amf_id, const char *hex) ogs_assert(amf_id); ogs_assert(hex); - OGS_HEX(hex, strlen(hex), hexbuf); + ogs_hex_from_string(hex, hexbuf, sizeof(hexbuf)); amf_id->region = hexbuf[0]; amf_id->set1 = hexbuf[1]; @@ -194,78 +194,6 @@ ogs_amf_id_t *ogs_amf_id_build(ogs_amf_id_t *amf_id, return amf_id; } -char *ogs_supi_from_suci(char *suci) -{ -#define MAX_SUCI_TOKEN 16 - char *array[MAX_SUCI_TOKEN]; - char *p, *tmp; - int i; - char *supi = NULL; - - ogs_assert(suci); - tmp = ogs_strdup(suci); - ogs_expect_or_return_val(tmp, NULL); - - p = tmp; - i = 0; - while((array[i++] = strsep(&p, "-"))) { - /* Empty Body */ - } - - SWITCH(array[0]) - CASE("suci") - SWITCH(array[1]) - CASE("0") /* SUPI format : IMSI */ - if (array[2] && array[3] && array[7]) - supi = ogs_msprintf("imsi-%s%s%s", - array[2], array[3], array[7]); - - break; - DEFAULT - ogs_error("Not implemented [%s]", array[1]); - break; - END - break; - DEFAULT - ogs_error("Not implemented [%s]", array[0]); - break; - END - - ogs_free(tmp); - return supi; -} - -char *ogs_supi_from_supi_or_suci(char *supi_or_suci) -{ - char *type = NULL; - char *supi = NULL; - - ogs_assert(supi_or_suci); - type = ogs_id_get_type(supi_or_suci); - if (!type) { - ogs_error("ogs_id_get_type[%s] failed", supi_or_suci); - goto cleanup; - } - SWITCH(type) - CASE("imsi") - supi = ogs_strdup(supi_or_suci); - ogs_expect(supi); - break; - CASE("suci") - supi = ogs_supi_from_suci(supi_or_suci); - ogs_expect(supi); - break; - DEFAULT - ogs_error("Not implemented [%s]", type); - break; - END - -cleanup: - if (type) - ogs_free(type); - return supi; -} - char *ogs_id_get_type(char *str) { char *token, *p, *tmp; diff --git a/lib/proto/types.h b/lib/proto/types.h index cd925426f..c300d7d26 100644 --- a/lib/proto/types.h +++ b/lib/proto/types.h @@ -58,6 +58,8 @@ extern "C" { #define OGS_CHRGCHARS_LEN 2 +#define OGS_MSIN_LEN 5 + #define OGS_BCD_TO_BUFFER_LEN(x) (((x)+1)/2) #define OGS_MAX_IMSI_BCD_LEN 15 #define OGS_MAX_IMSI_LEN \ @@ -176,9 +178,11 @@ ogs_amf_id_t *ogs_amf_id_build(ogs_amf_id_t *amf_id, uint8_t region, uint16_t set, uint8_t pointer); /************************************ - * SUPI/SUCI */ -char *ogs_supi_from_suci(char *suci); -char *ogs_supi_from_supi_or_suci(char *supi_or_suci); + * 9.11.3.4 5GS mobile identity + * - Protection Scheme */ +#define OGS_PROTECTION_SCHEME_NULL 0 +#define OGS_PROTECTION_SCHEME_PROFILE_A 1 +#define OGS_PROTECTION_SCHEME_PROFILE_B 2 /************************************ * SUPI/GPSI */ @@ -781,6 +785,11 @@ int ogs_pcc_rule_install_flow_from_media( int ogs_pcc_rule_update_qos_from_media( ogs_pcc_rule_t *pcc_rule, ogs_media_component_t *media_component); +typedef struct ogs_datum_s { + unsigned char *data; + unsigned int size; +} ogs_datum_t; + #ifdef __cplusplus } #endif diff --git a/lib/sbi/context.c b/lib/sbi/context.c index 79d9dc67b..1bac6c684 100644 --- a/lib/sbi/context.c +++ b/lib/sbi/context.c @@ -115,6 +115,44 @@ ogs_sbi_context_t *ogs_sbi_self(void) return &self; } +bool ogs_app_tls_server_enabled(void) +{ + if (self.tls.enabled == OGS_SBI_TLS_ENABLED_AUTO) { + if (self.tls.server.key && self.tls.server.cert) + return true; + else + return false; + } else if (self.tls.enabled == OGS_SBI_TLS_ENABLED_YES) { + ogs_assert(self.tls.server.key); + ogs_assert(self.tls.server.cert); + return true; + } else if (self.tls.enabled == OGS_SBI_TLS_ENABLED_NO) { + return false; + } else { + ogs_error("Unknown TLS enabled mode [%d]", self.tls.enabled); + return false; + } +} + +bool ogs_app_tls_client_enabled(void) +{ + if (self.tls.enabled == OGS_SBI_TLS_ENABLED_AUTO) { + if (self.tls.client.key && self.tls.client.cert) + return true; + else + return false; + } else if (self.tls.enabled == OGS_SBI_TLS_ENABLED_YES) { + ogs_assert(self.tls.client.key); + ogs_assert(self.tls.client.cert); + return true; + } else if (self.tls.enabled == OGS_SBI_TLS_ENABLED_NO) { + return false; + } else { + ogs_error("Unknown TLS enabled mode [%d]", self.tls.enabled); + return false; + } +} + static int ogs_sbi_context_prepare(void) { self.sbi_port = OGS_SBI_HTTP_PORT; @@ -123,6 +161,8 @@ static int ogs_sbi_context_prepare(void) self.content_encoding = "gzip"; #endif + self.tls.enabled = OGS_SBI_TLS_ENABLED_AUTO; + return OGS_OK; } @@ -130,7 +170,7 @@ static int ogs_sbi_context_validation( const char *local, const char *nrf, const char *scp) { /* If SMF is only used in 4G EPC, no SBI interface is required. */ - if (strcmp(local, "smf") != 0 && + if (local && strcmp(local, "smf") != 0 && ogs_list_first(&self.server_list) == NULL) { ogs_error("No %s.sbi: in '%s'", local, ogs_app()->file); return OGS_ERROR; @@ -139,18 +179,19 @@ static int ogs_sbi_context_validation( ogs_assert(context_initialized == 1); switch (self.discovery_config.delegated) { case OGS_SBI_DISCOVERY_DELEGATED_AUTO: - if (strcmp(local, "nrf") == 0) { + if (local && strcmp(local, "nrf") == 0) { /* Skip NRF */ - } else if (strcmp(local, "scp") == 0) { + } else if (local && strcmp(local, "scp") == 0) { /* Skip SCP */ - } else if (strcmp(local, "smf") == 0) { + } else if (local && strcmp(local, "smf") == 0) { /* Skip SMF since SMF can run 4G */ } else { if (NF_INSTANCE_CLIENT(self.nrf_instance) || NF_INSTANCE_CLIENT(self.scp_instance)) { } else { ogs_error("DELEGATED_AUTO - Both NRF and %s are unavailable", - strcmp(scp, "next_scp") == 0 ? "Next-hop SCP" : "SCP"); + scp && strcmp(scp, "next_scp") == 0 ? + "Next-hop SCP" : "SCP"); return OGS_ERROR; } } @@ -158,7 +199,8 @@ static int ogs_sbi_context_validation( case OGS_SBI_DISCOVERY_DELEGATED_YES: if (NF_INSTANCE_CLIENT(self.scp_instance) == NULL) { ogs_error("DELEGATED_YES - no %s available", - strcmp(scp, "next_scp") == 0 ? "Next-hop SCP" : "SCP"); + scp && strcmp(scp, "next_scp") == 0 ? + "Next-hop SCP" : "SCP"); return OGS_ERROR; } break; @@ -174,6 +216,27 @@ static int ogs_sbi_context_validation( ogs_assert_if_reached(); } + if (self.tls.enabled == OGS_SBI_TLS_ENABLED_YES) { + + if (!self.tls.server.key) { + ogs_error("No Server Key"); + return OGS_ERROR; + } + if (!self.tls.server.cert) { + ogs_error("No Server Certificate"); + return OGS_ERROR; + } + + if (!self.tls.client.key) { + ogs_error("No Client Key"); + return OGS_ERROR; + } + if (!self.tls.client.cert) { + ogs_error("No Client Certificate"); + return OGS_ERROR; + } + } + return OGS_OK; } @@ -673,6 +736,141 @@ int ogs_sbi_context_parse_config( YAML_SEQUENCE_NODE); } } + } else if (!strcmp(root_key, "tls")) { + ogs_yaml_iter_t tls_iter; + ogs_yaml_iter_recurse(&root_iter, &tls_iter); + while (ogs_yaml_iter_next(&tls_iter)) { + const char *tls_key = ogs_yaml_iter_key(&tls_iter); + ogs_assert(tls_key); + if (!strcmp(tls_key, "enabled")) { + const char *v = ogs_yaml_iter_value(&tls_iter); + if (!strcmp(v, "auto")) + self.tls.enabled = OGS_SBI_TLS_ENABLED_AUTO; + else if (!strcmp(v, "yes")) + self.tls.enabled = OGS_SBI_TLS_ENABLED_YES; + else if (!strcmp(v, "no")) + self.tls.enabled = OGS_SBI_TLS_ENABLED_NO; + else + ogs_warn("unknown 'tls.enabled' value `%s`", v); + } else if (!strcmp(tls_key, "server")) { + ogs_yaml_iter_t server_iter; + ogs_yaml_iter_recurse(&tls_iter, &server_iter); + + while (ogs_yaml_iter_next(&server_iter)) { + const char *server_key = + ogs_yaml_iter_key(&server_iter); + ogs_assert(server_key); + if (!strcmp(server_key, "cacert")) { + self.tls.server.cacert = + ogs_yaml_iter_value(&server_iter); + } else if (!strcmp(server_key, "cert")) { + self.tls.server.cert = + ogs_yaml_iter_value(&server_iter); + } else if (!strcmp(server_key, "key")) { + self.tls.server.key = + ogs_yaml_iter_value(&server_iter); + } else + ogs_warn("unknown key `%s`", server_key); + } + } else if (!strcmp(tls_key, "client")) { + ogs_yaml_iter_t client_iter; + ogs_yaml_iter_recurse(&tls_iter, &client_iter); + + while (ogs_yaml_iter_next(&client_iter)) { + const char *client_key = + ogs_yaml_iter_key(&client_iter); + ogs_assert(client_key); + if (!strcmp(client_key, "cacert")) { + self.tls.client.cacert = + ogs_yaml_iter_value(&client_iter); + } else if (!strcmp(client_key, "cert")) { + self.tls.client.cert = + ogs_yaml_iter_value(&client_iter); + } else if (!strcmp(client_key, "key")) { + self.tls.client.key = + ogs_yaml_iter_value(&client_iter); + } else + ogs_warn("unknown key `%s`", client_key); + } + } else + ogs_warn("unknown key `%s`", tls_key); + } + } else if (!strcmp(root_key, "hnet")) { + ogs_yaml_iter_t hnet_array, hnet_iter; + ogs_yaml_iter_recurse(&root_iter, &hnet_array); + do { + uint8_t id = 0, scheme = 0; + const char *filename = NULL; + + if (ogs_yaml_iter_type(&hnet_array) == + YAML_MAPPING_NODE) { + memcpy(&hnet_iter, &hnet_array, + sizeof(ogs_yaml_iter_t)); + } else if (ogs_yaml_iter_type(&hnet_array) == + YAML_SEQUENCE_NODE) { + if (!ogs_yaml_iter_next(&hnet_array)) + break; + ogs_yaml_iter_recurse(&hnet_array, + &hnet_iter); + } else if (ogs_yaml_iter_type(&hnet_array) == + YAML_SCALAR_NODE) { + break; + } else + ogs_assert_if_reached(); + + while (ogs_yaml_iter_next(&hnet_iter)) { + const char *hnet_key = + ogs_yaml_iter_key(&hnet_iter); + ogs_assert(hnet_key); + if (!strcmp(hnet_key, "id")) { + const char *v = ogs_yaml_iter_value(&hnet_iter); + if (v) { + if (atoi(v) >= 1 && atoi(v) <= 254) id = atoi(v); + } + } else if (!strcmp(hnet_key, "scheme")) { + const char *v = ogs_yaml_iter_value(&hnet_iter); + if (v) { + if (atoi(v) == 1 || atoi(v) == 2) + scheme = atoi(v); + } + } else if (!strcmp(hnet_key, "key")) { + filename = ogs_yaml_iter_value(&hnet_iter); + } else + ogs_warn("unknown key `%s`", hnet_key); + } + + if (id >= OGS_HOME_NETWORK_PKI_VALUE_MIN && + id <= OGS_HOME_NETWORK_PKI_VALUE_MAX && + filename) { + if (scheme == OGS_PROTECTION_SCHEME_PROFILE_A) { + rv = ogs_pem_decode_curve25519_key( + filename, self.hnet[id].key); + if (rv == OGS_OK) { + self.hnet[id].avail = true; + self.hnet[id].scheme = scheme; + } else { + ogs_error( + "ogs_pem_decode_curve25519_key[%s] failed", + filename); + } + } else if (scheme == OGS_PROTECTION_SCHEME_PROFILE_B) { + rv = ogs_pem_decode_secp256r1_key( + filename, self.hnet[id].key); + if (rv == OGS_OK) { + self.hnet[id].avail = true; + self.hnet[id].scheme = scheme; + } else { + ogs_error( + "ogs_pem_decode_secp256r1_key[%s] failed", + filename); + } + } else + ogs_error("Invalid scheme [%d]", scheme); + } else + ogs_error("Invalid home network configuration " + "[id:%d, filename:%s]", id, filename); + } while (ogs_yaml_iter_type(&hnet_array) == + YAML_SEQUENCE_NODE); } } diff --git a/lib/sbi/context.h b/lib/sbi/context.h index 6aba80529..5ef71f648 100644 --- a/lib/sbi/context.h +++ b/lib/sbi/context.h @@ -46,9 +46,33 @@ typedef struct ogs_sbi_discovery_config_s { bool prefer_requester_nf_instance_id; } ogs_sbi_discovery_config_t; +typedef enum { + OGS_SBI_TLS_ENABLED_AUTO = 0, + OGS_SBI_TLS_ENABLED_YES, + OGS_SBI_TLS_ENABLED_NO, +} ogs_sbi_tls_enabled_mode_e; + typedef struct ogs_sbi_context_s { ogs_sbi_discovery_config_t discovery_config; /* SCP Discovery Delegated */ + struct { + ogs_sbi_tls_enabled_mode_e enabled; + struct { + const char *cacert; + const char *cert; + const char *key; + } server, client; + } tls; + +#define OGS_HOME_NETWORK_PKI_VALUE_MIN 1 +#define OGS_HOME_NETWORK_PKI_VALUE_MAX 254 + + struct { + uint8_t avail; + uint8_t scheme; + uint8_t key[OGS_ECCKEY_LEN]; /* 32 bytes Private Key */ + } hnet[OGS_HOME_NETWORK_PKI_VALUE_MAX+1]; /* PKI Value : 1 ~ 254 */ + uint16_t sbi_port; /* SBI local port */ ogs_list_t server_list; diff --git a/lib/sbi/conv.c b/lib/sbi/conv.c index db1fa4939..74de6ed52 100644 --- a/lib/sbi/conv.c +++ b/lib/sbi/conv.c @@ -20,6 +20,255 @@ #include "ogs-sbi.h" #include "yuarel.h" +static int parse_scheme_output( + char *_protection_scheme_id, char *_scheme_output, + ogs_datum_t *ecckey, ogs_datum_t *cipher_text, uint8_t *mactag) +{ + uint8_t protection_scheme_id; + uint8_t scheme_output_size; + uint8_t *scheme_output = NULL; + uint8_t *p = NULL; + + ogs_assert(_protection_scheme_id); + ogs_assert(_scheme_output); + ogs_assert(ecckey); + ogs_assert(mactag); + ogs_assert(cipher_text); + + scheme_output_size = strlen(_scheme_output)/2; + if (scheme_output_size <= ((OGS_ECCKEY_LEN+1) + OGS_MACTAG_LEN)) { + ogs_error("Not enought length [%d]", (int)strlen(_scheme_output)); + return OGS_ERROR; + } + + scheme_output = ogs_calloc(1, scheme_output_size); + ogs_assert(scheme_output); + + ogs_ascii_to_hex(_scheme_output, strlen(_scheme_output), + scheme_output, scheme_output_size); + + protection_scheme_id = atoi(_protection_scheme_id); + if (protection_scheme_id == OGS_PROTECTION_SCHEME_PROFILE_A) { + ecckey->size = OGS_ECCKEY_LEN; + } else if (protection_scheme_id == OGS_PROTECTION_SCHEME_PROFILE_B) { + ecckey->size = OGS_ECCKEY_LEN+1; + } else { + ogs_free(scheme_output); + + ogs_fatal("Invalid protection scheme id [%s]", _protection_scheme_id); + ogs_assert_if_reached(); + + return OGS_ERROR; + } + + cipher_text->size = OGS_MSIN_LEN; + + p = scheme_output; + ecckey->data = ogs_memdup(p, ecckey->size); + ogs_assert(ecckey->data); + + p += ecckey->size; + cipher_text->data = ogs_memdup(p, cipher_text->size); + ogs_assert(cipher_text->data); + + p += cipher_text->size; + memcpy(mactag, p, OGS_MACTAG_LEN); + + ogs_free(scheme_output); + + return OGS_OK; +} + +char *ogs_supi_from_suci(char *suci) +{ +#define MAX_SUCI_TOKEN 16 + char *array[MAX_SUCI_TOKEN]; + char *p, *tmp; + int i; + char *supi = NULL; + + ogs_assert(suci); + tmp = ogs_strdup(suci); + ogs_expect_or_return_val(tmp, NULL); + + p = tmp; + i = 0; + while((array[i++] = strsep(&p, "-"))) { + /* Empty Body */ + } + + SWITCH(array[0]) + CASE("suci") + SWITCH(array[1]) + CASE("0") /* SUPI format : IMSI */ + if (array[2] && array[3] && array[5] && array[6] && array[7]) { + uint8_t protection_scheme_id = atoi(array[5]); + uint8_t home_network_pki_value = atoi(array[6]); + + if (protection_scheme_id == OGS_PROTECTION_SCHEME_NULL) { + supi = ogs_msprintf("imsi-%s%s%s", + array[2], array[3], array[7]); + } else if (protection_scheme_id == + OGS_PROTECTION_SCHEME_PROFILE_A || + protection_scheme_id == + OGS_PROTECTION_SCHEME_PROFILE_B) { + + ogs_datum_t pubkey; + ogs_datum_t cipher_text; + ogs_datum_t plain_text; + char *plain_bcd; + uint8_t mactag1[OGS_MACTAG_LEN], mactag2[OGS_MACTAG_LEN]; + + uint8_t z[OGS_ECCKEY_LEN]; + + uint8_t ek[OGS_KEY_LEN]; + uint8_t icb[OGS_IVEC_LEN]; + uint8_t mk[OGS_SHA256_DIGEST_SIZE]; + + if (home_network_pki_value < + OGS_HOME_NETWORK_PKI_VALUE_MIN || + home_network_pki_value > + OGS_HOME_NETWORK_PKI_VALUE_MAX) { + ogs_error("Invalid HNET PKI Value [%s]", array[6]); + break; + } + + if (!ogs_sbi_self()->hnet[home_network_pki_value].avail) { + ogs_error("HNET PKI Value Not Avaiable [%s]", array[6]); + break; + } + + if (ogs_sbi_self()->hnet[home_network_pki_value].scheme + != protection_scheme_id) { + ogs_error("Scheme Not Matched [%d != %s]", + ogs_sbi_self()->hnet[protection_scheme_id].scheme, + array[5]); + break; + } + + if (parse_scheme_output( + array[5], array[7], + &pubkey, &cipher_text, mactag1) != OGS_OK) { + ogs_error("parse_scheme_output[%s] failed", array[7]); + break; + } + + if (protection_scheme_id == + OGS_PROTECTION_SCHEME_PROFILE_A) { + curve25519_donna(z, + ogs_sbi_self()->hnet[home_network_pki_value].key, + pubkey.data); + } else if (protection_scheme_id == + OGS_PROTECTION_SCHEME_PROFILE_B) { + if (ecdh_shared_secret( + pubkey.data, + ogs_sbi_self()-> + hnet[home_network_pki_value].key, + z) != 1) { + ogs_error("ecdh_shared_secret() failed"); + ogs_log_hexdump(OGS_LOG_ERROR, + pubkey.data, OGS_ECCKEY_LEN); + ogs_log_hexdump(OGS_LOG_ERROR, + ogs_sbi_self()-> + hnet[home_network_pki_value].key, + OGS_ECCKEY_LEN); + goto cleanup; + } + } else + ogs_assert_if_reached(); + + ogs_kdf_ansi_x963( + z, OGS_ECCKEY_LEN, pubkey.data, pubkey.size, + ek, icb, mk); + + ogs_hmac_sha256( + mk, OGS_SHA256_DIGEST_SIZE, + cipher_text.data, cipher_text.size, + mactag2, OGS_MACTAG_LEN); + + if (memcmp(mactag1, mactag2, OGS_MACTAG_LEN) != 0) { + ogs_error("MAC-tag not matched"); + ogs_log_hexdump(OGS_LOG_ERROR, mactag1, OGS_MACTAG_LEN); + ogs_log_hexdump(OGS_LOG_ERROR, mactag2, OGS_MACTAG_LEN); + goto cleanup; + } + + plain_text.size = cipher_text.size; + plain_text.data = ogs_calloc(1, plain_text.size); + ogs_assert(plain_text.data); + + ogs_aes_ctr128_encrypt( + ek, icb, cipher_text.data, cipher_text.size, + plain_text.data); + + plain_bcd = ogs_calloc(1, plain_text.size*2+1); + ogs_assert(plain_bcd); + + ogs_buffer_to_bcd( + plain_text.data, plain_text.size, plain_bcd); + + supi = ogs_msprintf("imsi-%s%s%s", + array[2], array[3], plain_bcd); + ogs_assert(supi); + + if (plain_text.data) + ogs_free(plain_text.data); + ogs_free(plain_bcd); +cleanup: + if (pubkey.data) + ogs_free(pubkey.data); + if (cipher_text.data) + ogs_free(cipher_text.data); + } else { + ogs_error("Invalid Protection Scheme [%s]", array[5]); + } + } + break; + DEFAULT + ogs_error("Not implemented [%s]", array[1]); + break; + END + break; + DEFAULT + ogs_error("Not implemented [%s]", array[0]); + break; + END + + ogs_free(tmp); + return supi; +} + +char *ogs_supi_from_supi_or_suci(char *supi_or_suci) +{ + char *type = NULL; + char *supi = NULL; + + ogs_assert(supi_or_suci); + type = ogs_id_get_type(supi_or_suci); + if (!type) { + ogs_error("ogs_id_get_type[%s] failed", supi_or_suci); + goto cleanup; + } + SWITCH(type) + CASE("imsi") + supi = ogs_strdup(supi_or_suci); + ogs_expect(supi); + break; + CASE("suci") + supi = ogs_supi_from_suci(supi_or_suci); + ogs_expect(supi); + break; + DEFAULT + ogs_error("Not implemented [%s]", type); + break; + END + +cleanup: + if (type) + ogs_free(type); + return supi; +} + char *ogs_uridup(bool https, ogs_sockaddr_t *addr, ogs_sbi_header_t *h) { char buf[OGS_ADDRSTRLEN]; diff --git a/lib/sbi/conv.h b/lib/sbi/conv.h index d1ba5f80a..71f144168 100644 --- a/lib/sbi/conv.h +++ b/lib/sbi/conv.h @@ -32,6 +32,9 @@ typedef struct ogs_sbi_server_s ogs_sbi_server_t; typedef struct ogs_sbi_client_s ogs_sbi_client_t; typedef struct ogs_sbi_header_s ogs_sbi_header_t; +char *ogs_supi_from_suci(char *suci); +char *ogs_supi_from_supi_or_suci(char *supi_or_suci); + char *ogs_uridup(bool https, ogs_sockaddr_t *addr, ogs_sbi_header_t *h); char *ogs_sbi_server_uri(ogs_sbi_server_t *server, ogs_sbi_header_t *h); char *ogs_sbi_client_apiroot(ogs_sbi_client_t *client); diff --git a/lib/sbi/nghttp2-server.c b/lib/sbi/nghttp2-server.c index 75c151de3..36b91c0bf 100644 --- a/lib/sbi/nghttp2-server.c +++ b/lib/sbi/nghttp2-server.c @@ -202,10 +202,11 @@ static int server_start(ogs_sbi_server_t *server, /* Create SSL CTX */ if (ogs_app_tls_server_enabled() == true) { - ogs_assert(ogs_app()->tls.server.key); - ogs_assert(ogs_app()->tls.server.cert); + ogs_assert(ogs_sbi_self()->tls.server.key); + ogs_assert(ogs_sbi_self()->tls.server.cert); server->ssl_ctx = create_ssl_ctx( - ogs_app()->tls.server.key, ogs_app()->tls.server.cert); + ogs_sbi_self()->tls.server.key, + ogs_sbi_self()->tls.server.cert); if (!server->ssl_ctx) { ogs_error("Cannot create SSL CTX"); return OGS_ERROR; diff --git a/meson.build b/meson.build index 1b59f4525..9de40c454 100644 --- a/meson.build +++ b/meson.build @@ -148,7 +148,7 @@ message('\n'.join([ if cppcheck.found() run_target('analyze-cppcheck', - command : [ 'misc/static_code_analyze.sh', + command : [ 'misc/static-code-analyze.sh', meson.version().version_compare('>=0.55.0') ? cppcheck.full_path() : cppcheck.path(), meson.current_build_dir(), @@ -159,7 +159,7 @@ endif if clangtidy.found() run_target('analyze-clang-tidy', - command : [ 'misc/static_code_analyze.sh', + command : [ 'misc/static-code-analyze.sh', meson.version().version_compare('>=0.55.0') ? clangtidy.full_path() : clangtidy.path(), meson.current_build_dir(), diff --git a/misc/gen-hnkey.sh b/misc/gen-hnkey.sh new file mode 100755 index 000000000..2e9bfe0cb --- /dev/null +++ b/misc/gen-hnkey.sh @@ -0,0 +1,23 @@ +#!/bin/sh + +if [ $# -lt 1 -o $# -gt 2 ] +then + echo You must specify output directory : ./gen-hnkey.sh ../config/open5gs/hnet [numberOfKey] + exit; +fi + +limit=3 + +if [ $# -eq 2 ] +then + limit=$2 +fi + +limit=$((limit * 2)) + +i=1; while [ $i -le $limit ]; do + openssl genpkey -algorithm X25519 -out $1/curve25519-$i.key + openssl ecparam -name prime256v1 -genkey -conv_form compressed -out $1/secp256r1-$(($i+1)).key + + i=$(($i + 2)) +done diff --git a/misc/make-certs.sh b/misc/make-certs.sh index 5ec44b5df..125f2192a 100755 --- a/misc/make-certs.sh +++ b/misc/make-certs.sh @@ -2,7 +2,7 @@ if [ 1 -ne $# ] then - echo You must specify output directory : ./make-certs.sh ../config/tls + echo You must specify output directory : ./make-certs.sh ../config/open5gs/tls exit; fi diff --git a/misc/static_code_analyze.sh b/misc/static-code-analyze.sh similarity index 100% rename from misc/static_code_analyze.sh rename to misc/static-code-analyze.sh diff --git a/src/amf/context.c b/src/amf/context.c index 6aaa4520a..ce1a9eccc 100644 --- a/src/amf/context.c +++ b/src/amf/context.c @@ -1593,11 +1593,11 @@ amf_ue_t *amf_ue_find_by_message(ogs_nas_5gs_message_t *message) (ogs_nas_5gs_mobile_identity_suci_t *)mobile_identity->buffer; if (mobile_identity_suci->protection_scheme_id != - OGS_NAS_5GS_NULL_SCHEME && + OGS_PROTECTION_SCHEME_NULL && mobile_identity_suci->protection_scheme_id != - OGS_NAS_5GS_ECIES_SCHEME_PROFILE_A && + OGS_PROTECTION_SCHEME_PROFILE_A && mobile_identity_suci->protection_scheme_id != - OGS_NAS_5GS_ECIES_SCHEME_PROFILE_B) { + OGS_PROTECTION_SCHEME_PROFILE_B) { ogs_error("Invalid ProtectionSchemeID(%d) in SUCI", mobile_identity_suci->protection_scheme_id); return NULL; diff --git a/src/amf/gmm-handler.c b/src/amf/gmm-handler.c index a0376696a..47f103724 100644 --- a/src/amf/gmm-handler.c +++ b/src/amf/gmm-handler.c @@ -136,11 +136,11 @@ ogs_nas_5gmm_cause_t gmm_handle_registration_request(amf_ue_t *amf_ue, (ogs_nas_5gs_mobile_identity_suci_t *)mobile_identity->buffer; if (mobile_identity_suci->protection_scheme_id != - OGS_NAS_5GS_NULL_SCHEME && + OGS_PROTECTION_SCHEME_NULL && mobile_identity_suci->protection_scheme_id != - OGS_NAS_5GS_ECIES_SCHEME_PROFILE_A && + OGS_PROTECTION_SCHEME_PROFILE_A && mobile_identity_suci->protection_scheme_id != - OGS_NAS_5GS_ECIES_SCHEME_PROFILE_B) { + OGS_PROTECTION_SCHEME_PROFILE_B) { ogs_error("Invalid ProtectionSchemeID(%d) in SUCI", mobile_identity_suci->protection_scheme_id); return OGS_5GMM_CAUSE_SEMANTICALLY_INCORRECT_MESSAGE; @@ -774,8 +774,12 @@ int gmm_handle_identity_response(amf_ue_t *amf_ue, mobile_identity_suci = (ogs_nas_5gs_mobile_identity_suci_t *)mobile_identity->buffer; if (mobile_identity_suci->protection_scheme_id != - OGS_NAS_5GS_NULL_SCHEME) { - ogs_error("Not implemented ProtectionSchemeID(%d) in SUCI", + OGS_PROTECTION_SCHEME_NULL && + mobile_identity_suci->protection_scheme_id != + OGS_PROTECTION_SCHEME_PROFILE_A && + mobile_identity_suci->protection_scheme_id != + OGS_PROTECTION_SCHEME_PROFILE_B) { + ogs_error("Invalid ProtectionSchemeID(%d) in SUCI", mobile_identity_suci->protection_scheme_id); return OGS_ERROR; } diff --git a/src/mme/mme-fd-path.c b/src/mme/mme-fd-path.c index c7eb0bf93..2e74668f4 100644 --- a/src/mme/mme-fd-path.c +++ b/src/mme/mme-fd-path.c @@ -138,9 +138,10 @@ static int mme_s6a_subscription_data_from_avp(struct avp *avp, ogs_assert(ret == 0); if (avpch1) { ret = fd_msg_avp_hdr(avpch1, &hdr); - memcpy(mme_ue->charging_characteristics, - OGS_HEX(hdr->avp_value->os.data, (int)hdr->avp_value->os.len, buf), - OGS_CHRGCHARS_LEN); + ogs_ascii_to_hex( + (char*)hdr->avp_value->os.data, (int)hdr->avp_value->os.len, + buf, sizeof(buf)); + memcpy(mme_ue->charging_characteristics, buf, OGS_CHRGCHARS_LEN); mme_ue->charging_characteristics_presence = true; *subdatamask = (*subdatamask | OGS_DIAM_S6A_SUBDATA_CC); } @@ -342,10 +343,12 @@ static int mme_s6a_subscription_data_from_avp(struct avp *avp, ogs_assert(ret == 0); if (avpch3) { ret = fd_msg_avp_hdr(avpch3, &hdr); + ogs_ascii_to_hex( + (char*)hdr->avp_value->os.data, + (int)hdr->avp_value->os.len, + buf, sizeof(buf)); memcpy(session->charging_characteristics, - OGS_HEX(hdr->avp_value->os.data, - (int)hdr->avp_value->os.len, buf), - OGS_CHRGCHARS_LEN); + buf, OGS_CHRGCHARS_LEN); session->charging_characteristics_presence = true; } else { memcpy(session->charging_characteristics, @@ -1701,12 +1704,13 @@ static int mme_ogs_diam_s6a_idr_cb( struct msg **msg, struct avp *avp, memcpy(ida_ecgi, ogs_nas_from_plmn_id(&ida_plmn_buf, &mme_ue->e_cgi.plmn_id), 3); memcpy(ida_ecgi + 3, - OGS_HEX(ida_cell_id_hex, sizeof(ida_cell_id_hex), buf), 5); + ogs_hex_from_string(ida_cell_id_hex, buf, sizeof(buf)), 5); ogs_snprintf(ida_tac_hex, sizeof(ida_tac_hex), "%04x", ida_tac); memcpy(ida_tai, ogs_nas_from_plmn_id(&ida_plmn_buf, &mme_ue->tai.plmn_id), 3); - memcpy(ida_tai + 3, OGS_HEX(ida_tac_hex, sizeof(ida_tac_hex), buf), 2); + memcpy(ida_tai + 3, + ogs_hex_from_string(ida_tac_hex, buf, sizeof(buf)), 2); ida_age = (ogs_time_now() - mme_ue->ue_location_timestamp) / 1000000 / 60; diff --git a/tests/310014/epc-test.c b/tests/310014/epc-test.c index 0a9624952..18db2c524 100644 --- a/tests/310014/epc-test.c +++ b/tests/310014/epc-test.c @@ -46,15 +46,10 @@ static void test1_func(abts_case *tc, void *data) mobile_identity_suci.routing_indicator2 = 0xf; mobile_identity_suci.routing_indicator3 = 0xf; mobile_identity_suci.routing_indicator4 = 0xf; - mobile_identity_suci.protection_scheme_id = OGS_NAS_5GS_NULL_SCHEME; + mobile_identity_suci.protection_scheme_id = OGS_PROTECTION_SCHEME_NULL; mobile_identity_suci.home_network_pki_value = 0; - mobile_identity_suci.scheme_output[0] = 0x89; - mobile_identity_suci.scheme_output[1] = 0x67; - mobile_identity_suci.scheme_output[2] = 0x45; - mobile_identity_suci.scheme_output[3] = 0; - mobile_identity_suci.scheme_output[4] = 0xf4; - test_ue = test_ue_add_by_suci(&mobile_identity_suci, 13); + test_ue = test_ue_add_by_suci(&mobile_identity_suci, "89674500f4"); ogs_assert(test_ue); test_ue->e_cgi.cell_id = 0x8d01b780; diff --git a/tests/app/5gc-init.c b/tests/app/5gc-init.c index b1ad017ac..fcd6f1201 100644 --- a/tests/app/5gc-init.c +++ b/tests/app/5gc-init.c @@ -112,6 +112,7 @@ void test_5gc_init(void) ogs_log_install_domain(&__ogs_dbi_domain, "dbi", OGS_LOG_ERROR); ogs_log_install_domain(&__ogs_nas_domain, "nas", OGS_LOG_ERROR); ogs_log_install_domain(&__ogs_gtp_domain, "gtp", OGS_LOG_ERROR); + ogs_log_install_domain(&__ogs_sbi_domain, "sbi", OGS_LOG_ERROR); ogs_sctp_init(ogs_app()->usrsctp.udp_port); ogs_assert(ogs_dbi_init(ogs_app()->db_uri) == OGS_OK); diff --git a/tests/app/app-init.c b/tests/app/app-init.c index 3926bae76..040448d53 100644 --- a/tests/app/app-init.c +++ b/tests/app/app-init.c @@ -137,6 +137,7 @@ void test_app_init(void) ogs_log_install_domain(&__ogs_dbi_domain, "dbi", OGS_LOG_ERROR); ogs_log_install_domain(&__ogs_nas_domain, "nas", OGS_LOG_ERROR); ogs_log_install_domain(&__ogs_gtp_domain, "gtp", OGS_LOG_ERROR); + ogs_log_install_domain(&__ogs_sbi_domain, "sbi", OGS_LOG_ERROR); ogs_sctp_init(ogs_app()->usrsctp.udp_port); ogs_assert(ogs_dbi_init(ogs_app()->db_uri) == OGS_OK); diff --git a/tests/attach/auth-test.c b/tests/attach/auth-test.c index dafb014ad..370e4a1b3 100644 --- a/tests/attach/auth-test.c +++ b/tests/attach/auth-test.c @@ -49,15 +49,10 @@ static void test1_func(abts_case *tc, void *data) mobile_identity_suci.routing_indicator2 = 0xf; mobile_identity_suci.routing_indicator3 = 0xf; mobile_identity_suci.routing_indicator4 = 0xf; - mobile_identity_suci.protection_scheme_id = OGS_NAS_5GS_NULL_SCHEME; + mobile_identity_suci.protection_scheme_id = OGS_PROTECTION_SCHEME_NULL; mobile_identity_suci.home_network_pki_value = 0; - mobile_identity_suci.scheme_output[0] = 0x37; - mobile_identity_suci.scheme_output[1] = 0x46; - mobile_identity_suci.scheme_output[2] = 0; - mobile_identity_suci.scheme_output[3] = 0; - mobile_identity_suci.scheme_output[4] = 0x06; - test_ue = test_ue_add_by_suci(&mobile_identity_suci, 13); + test_ue = test_ue_add_by_suci(&mobile_identity_suci, "3746000006"); ogs_assert(test_ue); test_ue->e_cgi.cell_id = 0x1079baf0; diff --git a/tests/attach/emm-status-test.c b/tests/attach/emm-status-test.c index cc9a09029..e0efc4bdc 100644 --- a/tests/attach/emm-status-test.c +++ b/tests/attach/emm-status-test.c @@ -46,15 +46,10 @@ static void test1_func(abts_case *tc, void *data) mobile_identity_suci.routing_indicator2 = 0xf; mobile_identity_suci.routing_indicator3 = 0xf; mobile_identity_suci.routing_indicator4 = 0xf; - mobile_identity_suci.protection_scheme_id = OGS_NAS_5GS_NULL_SCHEME; + mobile_identity_suci.protection_scheme_id = OGS_PROTECTION_SCHEME_NULL; mobile_identity_suci.home_network_pki_value = 0; - mobile_identity_suci.scheme_output[0] = 0x37; - mobile_identity_suci.scheme_output[1] = 0x46; - mobile_identity_suci.scheme_output[2] = 0; - mobile_identity_suci.scheme_output[3] = 0; - mobile_identity_suci.scheme_output[4] = 0x06; - test_ue = test_ue_add_by_suci(&mobile_identity_suci, 13); + test_ue = test_ue_add_by_suci(&mobile_identity_suci, "3746000006"); ogs_assert(test_ue); test_ue->e_cgi.cell_id = 0x1079baf0; diff --git a/tests/attach/guti-test.c b/tests/attach/guti-test.c index cc5dc3963..dd837144e 100644 --- a/tests/attach/guti-test.c +++ b/tests/attach/guti-test.c @@ -49,15 +49,10 @@ static void test1_func(abts_case *tc, void *data) mobile_identity_suci.routing_indicator2 = 0xf; mobile_identity_suci.routing_indicator3 = 0xf; mobile_identity_suci.routing_indicator4 = 0xf; - mobile_identity_suci.protection_scheme_id = OGS_NAS_5GS_NULL_SCHEME; + mobile_identity_suci.protection_scheme_id = OGS_PROTECTION_SCHEME_NULL; mobile_identity_suci.home_network_pki_value = 0; - mobile_identity_suci.scheme_output[0] = 0x37; - mobile_identity_suci.scheme_output[1] = 0x46; - mobile_identity_suci.scheme_output[2] = 0; - mobile_identity_suci.scheme_output[3] = 0; - mobile_identity_suci.scheme_output[4] = 0x06; - test_ue = test_ue_add_by_suci(&mobile_identity_suci, 13); + test_ue = test_ue_add_by_suci(&mobile_identity_suci, "3746000006"); ogs_assert(test_ue); test_ue->e_cgi.cell_id = 0x1079baf0; @@ -476,15 +471,10 @@ static void test2_func(abts_case *tc, void *data) mobile_identity_suci.routing_indicator2 = 0xf; mobile_identity_suci.routing_indicator3 = 0xf; mobile_identity_suci.routing_indicator4 = 0xf; - mobile_identity_suci.protection_scheme_id = OGS_NAS_5GS_NULL_SCHEME; + mobile_identity_suci.protection_scheme_id = OGS_PROTECTION_SCHEME_NULL; mobile_identity_suci.home_network_pki_value = 0; - mobile_identity_suci.scheme_output[0] = 0x37; - mobile_identity_suci.scheme_output[1] = 0x46; - mobile_identity_suci.scheme_output[2] = 0; - mobile_identity_suci.scheme_output[3] = 0; - mobile_identity_suci.scheme_output[4] = 0x06; - test_ue = test_ue_add_by_suci(&mobile_identity_suci, 13); + test_ue = test_ue_add_by_suci(&mobile_identity_suci, "3746000006"); ogs_assert(test_ue); test_ue->e_cgi.cell_id = 0x1079baf0; @@ -895,15 +885,10 @@ static void test3_func(abts_case *tc, void *data) mobile_identity_suci.routing_indicator2 = 0xf; mobile_identity_suci.routing_indicator3 = 0xf; mobile_identity_suci.routing_indicator4 = 0xf; - mobile_identity_suci.protection_scheme_id = OGS_NAS_5GS_NULL_SCHEME; + mobile_identity_suci.protection_scheme_id = OGS_PROTECTION_SCHEME_NULL; mobile_identity_suci.home_network_pki_value = 0; - mobile_identity_suci.scheme_output[0] = 0x37; - mobile_identity_suci.scheme_output[1] = 0x46; - mobile_identity_suci.scheme_output[2] = 0; - mobile_identity_suci.scheme_output[3] = 0; - mobile_identity_suci.scheme_output[4] = 0x06; - test_ue = test_ue_add_by_suci(&mobile_identity_suci, 13); + test_ue = test_ue_add_by_suci(&mobile_identity_suci, "3746000006"); ogs_assert(test_ue); test_ue->e_cgi.cell_id = 0x54f6401; @@ -1231,15 +1216,10 @@ static void test4_func(abts_case *tc, void *data) mobile_identity_suci.routing_indicator2 = 0xf; mobile_identity_suci.routing_indicator3 = 0xf; mobile_identity_suci.routing_indicator4 = 0xf; - mobile_identity_suci.protection_scheme_id = OGS_NAS_5GS_NULL_SCHEME; + mobile_identity_suci.protection_scheme_id = OGS_PROTECTION_SCHEME_NULL; mobile_identity_suci.home_network_pki_value = 0; - mobile_identity_suci.scheme_output[0] = 0x37; - mobile_identity_suci.scheme_output[1] = 0x46; - mobile_identity_suci.scheme_output[2] = 0; - mobile_identity_suci.scheme_output[3] = 0; - mobile_identity_suci.scheme_output[4] = 0x06; - test_ue = test_ue_add_by_suci(&mobile_identity_suci, 13); + test_ue = test_ue_add_by_suci(&mobile_identity_suci, "3746000006"); ogs_assert(test_ue); test_ue->e_cgi.cell_id = 0x64010; diff --git a/tests/attach/idle-test.c b/tests/attach/idle-test.c index 13fc76b45..e56566201 100644 --- a/tests/attach/idle-test.c +++ b/tests/attach/idle-test.c @@ -49,15 +49,10 @@ static void test1_func(abts_case *tc, void *data) mobile_identity_suci.routing_indicator2 = 0xf; mobile_identity_suci.routing_indicator3 = 0xf; mobile_identity_suci.routing_indicator4 = 0xf; - mobile_identity_suci.protection_scheme_id = OGS_NAS_5GS_NULL_SCHEME; + mobile_identity_suci.protection_scheme_id = OGS_PROTECTION_SCHEME_NULL; mobile_identity_suci.home_network_pki_value = 0; - mobile_identity_suci.scheme_output[0] = 0x37; - mobile_identity_suci.scheme_output[1] = 0x46; - mobile_identity_suci.scheme_output[2] = 0; - mobile_identity_suci.scheme_output[3] = 0; - mobile_identity_suci.scheme_output[4] = 0x06; - test_ue = test_ue_add_by_suci(&mobile_identity_suci, 13); + test_ue = test_ue_add_by_suci(&mobile_identity_suci, "3746000006"); ogs_assert(test_ue); test_ue->e_cgi.cell_id = 0x54f6401; @@ -383,15 +378,10 @@ static void test2_func(abts_case *tc, void *data) mobile_identity_suci.routing_indicator2 = 0xf; mobile_identity_suci.routing_indicator3 = 0xf; mobile_identity_suci.routing_indicator4 = 0xf; - mobile_identity_suci.protection_scheme_id = OGS_NAS_5GS_NULL_SCHEME; + mobile_identity_suci.protection_scheme_id = OGS_PROTECTION_SCHEME_NULL; mobile_identity_suci.home_network_pki_value = 0; - mobile_identity_suci.scheme_output[0] = 0x37; - mobile_identity_suci.scheme_output[1] = 0x46; - mobile_identity_suci.scheme_output[2] = 0; - mobile_identity_suci.scheme_output[3] = 0; - mobile_identity_suci.scheme_output[4] = 0x06; - test_ue = test_ue_add_by_suci(&mobile_identity_suci, 13); + test_ue = test_ue_add_by_suci(&mobile_identity_suci, "3746000006"); ogs_assert(test_ue); test_ue->e_cgi.cell_id = 0x54f6401; @@ -673,15 +663,10 @@ static void test3_func(abts_case *tc, void *data) mobile_identity_suci.routing_indicator2 = 0xf; mobile_identity_suci.routing_indicator3 = 0xf; mobile_identity_suci.routing_indicator4 = 0xf; - mobile_identity_suci.protection_scheme_id = OGS_NAS_5GS_NULL_SCHEME; + mobile_identity_suci.protection_scheme_id = OGS_PROTECTION_SCHEME_NULL; mobile_identity_suci.home_network_pki_value = 0; - mobile_identity_suci.scheme_output[0] = 0x37; - mobile_identity_suci.scheme_output[1] = 0x46; - mobile_identity_suci.scheme_output[2] = 0; - mobile_identity_suci.scheme_output[3] = 0; - mobile_identity_suci.scheme_output[4] = 0x06; - test_ue = test_ue_add_by_suci(&mobile_identity_suci, 13); + test_ue = test_ue_add_by_suci(&mobile_identity_suci, "3746000006"); ogs_assert(test_ue); test_ue->e_cgi.cell_id = 0x54f6401; diff --git a/tests/attach/issues-test.c b/tests/attach/issues-test.c index bd4696dea..06567e54b 100644 --- a/tests/attach/issues-test.c +++ b/tests/attach/issues-test.c @@ -51,15 +51,10 @@ static void issues_1431_func(abts_case *tc, void *data) mobile_identity_suci.routing_indicator2 = 0xf; mobile_identity_suci.routing_indicator3 = 0xf; mobile_identity_suci.routing_indicator4 = 0xf; - mobile_identity_suci.protection_scheme_id = OGS_NAS_5GS_NULL_SCHEME; + mobile_identity_suci.protection_scheme_id = OGS_PROTECTION_SCHEME_NULL; mobile_identity_suci.home_network_pki_value = 0; - mobile_identity_suci.scheme_output[0] = 0x37; - mobile_identity_suci.scheme_output[1] = 0x46; - mobile_identity_suci.scheme_output[2] = 0; - mobile_identity_suci.scheme_output[3] = 0; - mobile_identity_suci.scheme_output[4] = 0x06; - test_ue = test_ue_add_by_suci(&mobile_identity_suci, 13); + test_ue = test_ue_add_by_suci(&mobile_identity_suci, "3746000006"); ogs_assert(test_ue); test_ue->e_cgi.cell_id = 0x00003c01; diff --git a/tests/attach/reset-test.c b/tests/attach/reset-test.c index ee6886f26..044e32982 100644 --- a/tests/attach/reset-test.c +++ b/tests/attach/reset-test.c @@ -48,15 +48,10 @@ static void test1_func(abts_case *tc, void *data) mobile_identity_suci.routing_indicator2 = 0xf; mobile_identity_suci.routing_indicator3 = 0xf; mobile_identity_suci.routing_indicator4 = 0xf; - mobile_identity_suci.protection_scheme_id = OGS_NAS_5GS_NULL_SCHEME; + mobile_identity_suci.protection_scheme_id = OGS_PROTECTION_SCHEME_NULL; mobile_identity_suci.home_network_pki_value = 0; - mobile_identity_suci.scheme_output[0] = 0x37; - mobile_identity_suci.scheme_output[1] = 0x46; - mobile_identity_suci.scheme_output[2] = 0; - mobile_identity_suci.scheme_output[3] = 0; - mobile_identity_suci.scheme_output[4] = 0x06; - test_ue = test_ue_add_by_suci(&mobile_identity_suci, 13); + test_ue = test_ue_add_by_suci(&mobile_identity_suci, "3746000006"); ogs_assert(test_ue); test_ue->e_cgi.cell_id = 0x54f6401; @@ -316,7 +311,13 @@ static void test2_func(abts_case *tc, void *data) tests1ap_recv(NULL, recvbuf); for (i = 0; i < NUM_OF_TEST_UE; i++) { - uint64_t imsi_index; + const char *scheme_output[] = { + "0000000001", + "0000000002", + "0000000003", + "0000000004", + "0000000005", + }; /* Setup Test UE & Session Context */ memset(&mobile_identity_suci, 0, sizeof(mobile_identity_suci)); @@ -327,13 +328,11 @@ static void test2_func(abts_case *tc, void *data) mobile_identity_suci.routing_indicator2 = 0xf; mobile_identity_suci.routing_indicator3 = 0xf; mobile_identity_suci.routing_indicator4 = 0xf; - mobile_identity_suci.protection_scheme_id = OGS_NAS_5GS_NULL_SCHEME; + mobile_identity_suci.protection_scheme_id = OGS_PROTECTION_SCHEME_NULL; mobile_identity_suci.home_network_pki_value = 0; - imsi_index = i + 1; - ogs_uint64_to_buffer(imsi_index, 5, mobile_identity_suci.scheme_output); - - test_ue[i] = test_ue_add_by_suci(&mobile_identity_suci, 13); + test_ue[i] = test_ue_add_by_suci( + &mobile_identity_suci, scheme_output[i]); ogs_assert(test_ue[i]); test_ue[i]->e_cgi.cell_id = 0x54f6401; @@ -545,7 +544,13 @@ static void test3_func(abts_case *tc, void *data) tests1ap_recv(NULL, recvbuf); for (i = 0; i < NUM_OF_TEST_UE; i++) { - uint64_t imsi_index; + const char *scheme_output[] = { + "0000000001", + "0000000002", + "0000000003", + "0000000004", + "0000000005", + }; /* Setup Test UE & Session Context */ memset(&mobile_identity_suci, 0, sizeof(mobile_identity_suci)); @@ -556,13 +561,11 @@ static void test3_func(abts_case *tc, void *data) mobile_identity_suci.routing_indicator2 = 0xf; mobile_identity_suci.routing_indicator3 = 0xf; mobile_identity_suci.routing_indicator4 = 0xf; - mobile_identity_suci.protection_scheme_id = OGS_NAS_5GS_NULL_SCHEME; + mobile_identity_suci.protection_scheme_id = OGS_PROTECTION_SCHEME_NULL; mobile_identity_suci.home_network_pki_value = 0; - imsi_index = i + 1; - ogs_uint64_to_buffer(imsi_index, 5, mobile_identity_suci.scheme_output); - - test_ue[i] = test_ue_add_by_suci(&mobile_identity_suci, 13); + test_ue[i] = test_ue_add_by_suci( + &mobile_identity_suci, scheme_output[i]); ogs_assert(test_ue[i]); /* Multiple eNB-UE-S1AP-UD */ diff --git a/tests/attach/simple-test.c b/tests/attach/simple-test.c index d86462aff..252b08b10 100644 --- a/tests/attach/simple-test.c +++ b/tests/attach/simple-test.c @@ -49,15 +49,10 @@ static void test1_func(abts_case *tc, void *data) mobile_identity_suci.routing_indicator2 = 0xf; mobile_identity_suci.routing_indicator3 = 0xf; mobile_identity_suci.routing_indicator4 = 0xf; - mobile_identity_suci.protection_scheme_id = OGS_NAS_5GS_NULL_SCHEME; + mobile_identity_suci.protection_scheme_id = OGS_PROTECTION_SCHEME_NULL; mobile_identity_suci.home_network_pki_value = 0; - mobile_identity_suci.scheme_output[0] = 0x37; - mobile_identity_suci.scheme_output[1] = 0x46; - mobile_identity_suci.scheme_output[2] = 0; - mobile_identity_suci.scheme_output[3] = 0; - mobile_identity_suci.scheme_output[4] = 0x06; - test_ue = test_ue_add_by_suci(&mobile_identity_suci, 13); + test_ue = test_ue_add_by_suci(&mobile_identity_suci, "3746000006"); ogs_assert(test_ue); test_ue->e_cgi.cell_id = 0x1079baf0; diff --git a/tests/attach/ue-context-test.c b/tests/attach/ue-context-test.c index 126aad29e..cfe45f4d0 100644 --- a/tests/attach/ue-context-test.c +++ b/tests/attach/ue-context-test.c @@ -46,15 +46,10 @@ static void test1_func(abts_case *tc, void *data) mobile_identity_suci.routing_indicator2 = 0xf; mobile_identity_suci.routing_indicator3 = 0xf; mobile_identity_suci.routing_indicator4 = 0xf; - mobile_identity_suci.protection_scheme_id = OGS_NAS_5GS_NULL_SCHEME; + mobile_identity_suci.protection_scheme_id = OGS_PROTECTION_SCHEME_NULL; mobile_identity_suci.home_network_pki_value = 0; - mobile_identity_suci.scheme_output[0] = 0x37; - mobile_identity_suci.scheme_output[1] = 0x46; - mobile_identity_suci.scheme_output[2] = 0; - mobile_identity_suci.scheme_output[3] = 0; - mobile_identity_suci.scheme_output[4] = 0x06; - test_ue = test_ue_add_by_suci(&mobile_identity_suci, 13); + test_ue = test_ue_add_by_suci(&mobile_identity_suci, "3746000006"); ogs_assert(test_ue); test_ue->e_cgi.cell_id = 0x1079baf0; @@ -214,15 +209,10 @@ static void test2_func(abts_case *tc, void *data) mobile_identity_suci.routing_indicator2 = 0xf; mobile_identity_suci.routing_indicator3 = 0xf; mobile_identity_suci.routing_indicator4 = 0xf; - mobile_identity_suci.protection_scheme_id = OGS_NAS_5GS_NULL_SCHEME; + mobile_identity_suci.protection_scheme_id = OGS_PROTECTION_SCHEME_NULL; mobile_identity_suci.home_network_pki_value = 0; - mobile_identity_suci.scheme_output[0] = 0x37; - mobile_identity_suci.scheme_output[1] = 0x46; - mobile_identity_suci.scheme_output[2] = 0; - mobile_identity_suci.scheme_output[3] = 0; - mobile_identity_suci.scheme_output[4] = 0x06; - test_ue = test_ue_add_by_suci(&mobile_identity_suci, 13); + test_ue = test_ue_add_by_suci(&mobile_identity_suci, "3746000006"); ogs_assert(test_ue); test_ue->e_cgi.cell_id = 0x64010; @@ -499,15 +489,10 @@ static void test3_func(abts_case *tc, void *data) mobile_identity_suci.routing_indicator2 = 0xf; mobile_identity_suci.routing_indicator3 = 0xf; mobile_identity_suci.routing_indicator4 = 0xf; - mobile_identity_suci.protection_scheme_id = OGS_NAS_5GS_NULL_SCHEME; + mobile_identity_suci.protection_scheme_id = OGS_PROTECTION_SCHEME_NULL; mobile_identity_suci.home_network_pki_value = 0; - mobile_identity_suci.scheme_output[0] = 0x37; - mobile_identity_suci.scheme_output[1] = 0x46; - mobile_identity_suci.scheme_output[2] = 0; - mobile_identity_suci.scheme_output[3] = 0; - mobile_identity_suci.scheme_output[4] = 0x06; - test_ue = test_ue_add_by_suci(&mobile_identity_suci, 13); + test_ue = test_ue_add_by_suci(&mobile_identity_suci, "3746000006"); ogs_assert(test_ue); test_ue->e_cgi.cell_id = 0x64010; diff --git a/tests/common/context.c b/tests/common/context.c index 628d29406..976573662 100644 --- a/tests/common/context.c +++ b/tests/common/context.c @@ -805,26 +805,44 @@ int test_context_parse_config(void) return OGS_OK; } -void test_ue_set_mobile_identity_suci(test_ue_t *test_ue, +static void test_ue_set_mobile_identity(test_ue_t *test_ue, ogs_nas_5gs_mobile_identity_suci_t *mobile_identity_suci, - uint16_t mobile_identity_suci_length) + const char *scheme_output_string) { - ogs_nas_5gs_mobile_identity_t mobile_identity; + ogs_nas_5gs_mobile_identity_t *mobile_identity = NULL; + uint16_t scheme_output_size; + uint8_t *scheme_output = NULL; ogs_assert(test_ue); ogs_assert(mobile_identity_suci); - ogs_assert(mobile_identity_suci_length); + ogs_assert(scheme_output_string); - test_ue->mobile_identity_suci_length = mobile_identity_suci_length; - memcpy(&test_ue->mobile_identity_suci, mobile_identity_suci, - mobile_identity_suci_length); + scheme_output_size = strlen(scheme_output_string)/2; + scheme_output = ogs_calloc(1, scheme_output_size); + ogs_assert(scheme_output); - mobile_identity.length = test_ue->mobile_identity_suci_length; - mobile_identity.buffer = &test_ue->mobile_identity_suci; + scheme_output_size = ogs_ascii_to_hex( + scheme_output_string, strlen(scheme_output_string), + scheme_output, scheme_output_size); + ogs_assert(scheme_output_size); + + mobile_identity = &test_ue->mobile_identity; + + mobile_identity->length = + OGS_NAS_5GS_MOBILE_IDENTITY_SUCI_MIN_SIZE + scheme_output_size; + mobile_identity->buffer = ogs_calloc(1, mobile_identity->length); + ogs_assert(mobile_identity->buffer); + memcpy(mobile_identity->buffer, + mobile_identity_suci, OGS_NAS_5GS_MOBILE_IDENTITY_SUCI_MIN_SIZE); + memcpy((uint8_t *)mobile_identity->buffer + + OGS_NAS_5GS_MOBILE_IDENTITY_SUCI_MIN_SIZE, + scheme_output, scheme_output_size); + + ogs_free(scheme_output); if (test_ue->suci) ogs_free(test_ue->suci); - test_ue->suci = ogs_nas_5gs_suci_from_mobile_identity(&mobile_identity); + test_ue->suci = ogs_nas_5gs_suci_from_mobile_identity(mobile_identity); if (test_ue->supi) ogs_free(test_ue->supi); test_ue->supi = ogs_supi_from_suci(test_ue->suci); @@ -890,13 +908,13 @@ static void test_ue_set_mobile_identity_imsisv(test_ue_t *test_ue) test_ue_t *test_ue_add_by_suci( ogs_nas_5gs_mobile_identity_suci_t *mobile_identity_suci, - uint16_t mobile_identity_suci_length) + const char *scheme_output) { int i, j; test_ue_t *test_ue = NULL; ogs_assert(mobile_identity_suci); - ogs_assert(mobile_identity_suci_length); + ogs_assert(scheme_output); ogs_pool_alloc(&test_ue_pool, &test_ue); ogs_assert(test_ue); @@ -944,8 +962,7 @@ test_ue_t *test_ue_add_by_suci( } } - test_ue_set_mobile_identity_suci( - test_ue, mobile_identity_suci, mobile_identity_suci_length); + test_ue_set_mobile_identity(test_ue, mobile_identity_suci, scheme_output); test_ue_set_mobile_identity_imsi(test_ue); test_ue_set_mobile_identity_imsisv(test_ue); @@ -961,6 +978,9 @@ void test_ue_remove(test_ue_t *test_ue) ogs_list_remove(&self.test_ue_list, test_ue); + if (test_ue->mobile_identity.buffer) + ogs_free(test_ue->mobile_identity.buffer); + if (test_ue->suci) ogs_free(test_ue->suci); if (test_ue->supi) @@ -1216,8 +1236,10 @@ int test_db_insert_ue(test_ue_t *test_ue, bson_t *doc) ogs_assert(test_ue); ogs_assert(doc); - OGS_HEX(test_ue->k_string, strlen(test_ue->k_string), test_ue->k); - OGS_HEX(test_ue->opc_string, strlen(test_ue->opc_string), test_ue->opc); + ogs_hex_from_string( + test_ue->k_string, test_ue->k, sizeof(test_ue->k)); + ogs_hex_from_string( + test_ue->opc_string, test_ue->opc, sizeof(test_ue->opc)); collection = mongoc_client_get_collection( ogs_mongoc()->client, ogs_mongoc()->name, "subscribers"); diff --git a/tests/common/context.h b/tests/common/context.h index 7c8a2077a..1f56d2e58 100644 --- a/tests/common/context.h +++ b/tests/common/context.h @@ -297,7 +297,7 @@ typedef struct test_ue_s { uint8_t imsi_buf[OGS_MAX_IMSI_LEN]; int imsi_len; - ogs_nas_5gs_mobile_identity_suci_t mobile_identity_suci; + ogs_nas_5gs_mobile_identity_t mobile_identity; ogs_nas_mobile_identity_imeisv_t mobile_identity_imeisv; bool mobile_identity_imeisv_presence; uint16_t mobile_identity_suci_length; @@ -488,13 +488,9 @@ test_context_t *test_self(void); int test_context_parse_config(void); -void test_ue_set_mobile_identity_suci(test_ue_t *test_ue, - ogs_nas_5gs_mobile_identity_suci_t *mobile_identity_suci, - uint16_t mobile_identity_suci_length); - test_ue_t *test_ue_add_by_suci( ogs_nas_5gs_mobile_identity_suci_t *mobile_identity_suci, - uint16_t mobile_identity_suci_length); + const char *scheme_output); void test_ue_remove(test_ue_t *test_ue); void test_ue_remove_all(void); diff --git a/tests/common/emm-build.c b/tests/common/emm-build.c index a65d72893..ad3b45ff3 100644 --- a/tests/common/emm-build.c +++ b/tests/common/emm-build.c @@ -295,8 +295,10 @@ ogs_pkbuf_t *testemm_build_authentication_response(test_ue_t *test_ue) message.emm.h.protocol_discriminator = OGS_NAS_PROTOCOL_DISCRIMINATOR_EMM; message.emm.h.message_type = OGS_NAS_EPS_AUTHENTICATION_RESPONSE; - OGS_HEX(test_ue->k_string, strlen(test_ue->k_string), test_ue->k); - OGS_HEX(test_ue->opc_string, strlen(test_ue->opc_string), test_ue->opc); + ogs_hex_from_string( + test_ue->k_string, test_ue->k, sizeof(test_ue->k)); + ogs_hex_from_string( + test_ue->opc_string, test_ue->opc, sizeof(test_ue->opc)); milenage_f2345(test_ue->opc, test_ue->k, test_ue->rand, res, ck, ik, ak, NULL); @@ -346,8 +348,10 @@ ogs_pkbuf_t *testemm_build_authentication_failure( authentication_failure->presencemask |= OGS_NAS_EPS_AUTHENTICATION_FAILURE_AUTHENTICATION_FAILURE_PARAMETER_PRESENT; - OGS_HEX(test_ue->k_string, strlen(test_ue->k_string), test_ue->k); - OGS_HEX(test_ue->opc_string, strlen(test_ue->opc_string), test_ue->opc); + ogs_hex_from_string( + test_ue->k_string, test_ue->k, sizeof(test_ue->k)); + ogs_hex_from_string( + test_ue->opc_string, test_ue->opc, sizeof(test_ue->opc)); milenage_f2345(test_ue->opc, test_ue->k, test_ue->rand, NULL, NULL, NULL, NULL, ak); @@ -895,7 +899,7 @@ ogs_pkbuf_t *testemm_build_uplink_nas_transport(test_ue_t *test_ue) message.emm.h.protocol_discriminator = OGS_NAS_PROTOCOL_DISCRIMINATOR_EMM; message.emm.h.message_type = OGS_NAS_EPS_UPLINK_NAS_TRANSPORT; - OGS_HEX(payload, strlen(payload), hexbuf); + ogs_hex_from_string(payload, hexbuf, sizeof(hexbuf)); nas_message_container->length = 106; memcpy(nas_message_container->buffer, hexbuf, nas_message_container->length); diff --git a/tests/common/gmm-build.c b/tests/common/gmm-build.c index 502e30403..2b51fe425 100644 --- a/tests/common/gmm-build.c +++ b/tests/common/gmm-build.c @@ -87,9 +87,9 @@ ogs_pkbuf_t *testgmm_build_registration_request( } else { registration_request->mobile_identity.length = - test_ue->mobile_identity_suci_length; + test_ue->mobile_identity.length; registration_request->mobile_identity.buffer = - &test_ue->mobile_identity_suci; + test_ue->mobile_identity.buffer; } if (test_ue->registration_request_param.uplink_data_status) { @@ -423,9 +423,9 @@ ogs_pkbuf_t *testgmm_build_de_registration_request( &mobile_identity_guti; } else { deregistration_request_from_ue->mobile_identity.length = - test_ue->mobile_identity_suci_length; + test_ue->mobile_identity.length; deregistration_request_from_ue->mobile_identity.buffer = - &test_ue->mobile_identity_suci; + test_ue->mobile_identity.buffer; } if (integrity_protected) @@ -449,9 +449,9 @@ ogs_pkbuf_t *testgmm_build_identity_response(test_ue_t *test_ue) message.gmm.h.message_type = OGS_NAS_5GS_IDENTITY_RESPONSE; identity_response->mobile_identity.length = - test_ue->mobile_identity_suci_length; + test_ue->mobile_identity.length; identity_response->mobile_identity.buffer = - &test_ue->mobile_identity_suci; + test_ue->mobile_identity.buffer; return ogs_nas_5gs_plain_encode(&message); } diff --git a/tests/common/gtpu.c b/tests/common/gtpu.c index b692a43de..a63fb069c 100644 --- a/tests/common/gtpu.c +++ b/tests/common/gtpu.c @@ -370,7 +370,7 @@ int test_gtpu_send_slacc_rs(ogs_socknode_t *node, test_bearer_t *bearer) ogs_pkbuf_put(pkbuf, 200-OGS_GTPV1U_5GC_HEADER_LEN); memset(pkbuf->data, 0, pkbuf->len); - OGS_HEX(payload, strlen(payload), tmp); + ogs_hex_from_string(payload, tmp, sizeof(tmp)); memcpy(pkbuf->data, tmp, payload_len); ip6_h = pkbuf->data; @@ -439,7 +439,7 @@ int test_gtpu_send_slacc_rs_with_unspecified_source_address( ogs_pkbuf_put(pkbuf, 200-OGS_GTPV1U_5GC_HEADER_LEN); memset(pkbuf->data, 0, pkbuf->len); - OGS_HEX(payload, strlen(payload), tmp); + ogs_hex_from_string(payload, tmp, sizeof(tmp)); memcpy(pkbuf->data, tmp, payload_len); ogs_pkbuf_trim(pkbuf, payload_len); diff --git a/tests/common/ngap-build.c b/tests/common/ngap-build.c index cf4a6eefd..6d4312a5a 100644 --- a/tests/common/ngap-build.c +++ b/tests/common/ngap-build.c @@ -736,7 +736,7 @@ ogs_pkbuf_t *testngap_build_ue_radio_capability_info_indication( asn_uint642INTEGER(AMF_UE_NGAP_ID, test_ue->amf_ue_ngap_id); *RAN_UE_NGAP_ID = test_ue->ran_ue_ngap_id; - OGS_HEX(_capability_captured, strlen(_capability_captured), tmp), + ogs_hex_from_string(_capability_captured, tmp, sizeof(tmp)); UERadioCapability->size = 407; UERadioCapability->buf = CALLOC(UERadioCapability->size, sizeof(uint8_t)); @@ -1876,7 +1876,7 @@ ogs_pkbuf_t *testngap_build_handover_required( SourceToTarget_TransparentContainer = &ie->value.choice.SourceToTarget_TransparentContainer; - OGS_HEX(_container, strlen(_container), tmp), + ogs_hex_from_string(_container, tmp, sizeof(tmp)); SourceToTarget_TransparentContainer->size = 32; SourceToTarget_TransparentContainer->buf = @@ -1992,7 +1992,7 @@ ogs_pkbuf_t *testngap_build_handover_request_ack(test_ue_t *test_ue) TargetToSource_TransparentContainer = &ie->value.choice.TargetToSource_TransparentContainer; - OGS_HEX(_container, strlen(_container), tmp), + ogs_hex_from_string(_container, tmp, sizeof(tmp)); TargetToSource_TransparentContainer->size = 4; TargetToSource_TransparentContainer->buf = diff --git a/tests/common/s1ap-build.c b/tests/common/s1ap-build.c index 614184717..ed834ba83 100644 --- a/tests/common/s1ap-build.c +++ b/tests/common/s1ap-build.c @@ -456,7 +456,7 @@ ogs_pkbuf_t *tests1ap_build_ue_radio_capability_info_indication( UERadioCapability = &ie->value.choice.UERadioCapability; - OGS_HEX(_capability_captured, strlen(_capability_captured), tmp), + ogs_hex_from_string(_capability_captured, tmp, sizeof(tmp)); UERadioCapability->size = 40; UERadioCapability->buf = CALLOC(UERadioCapability->size, sizeof(uint8_t)); @@ -1545,7 +1545,7 @@ ogs_pkbuf_t *test_s1ap_build_handover_required( Source_ToTarget_TransparentContainer = &ie->value.choice.Source_ToTarget_TransparentContainer; - OGS_HEX(_container, strlen(_container), tmp), + ogs_hex_from_string(_container, tmp, sizeof(tmp)); Source_ToTarget_TransparentContainer->size = 206; Source_ToTarget_TransparentContainer->buf = @@ -1696,7 +1696,7 @@ ogs_pkbuf_t *test_s1ap_build_handover_request_ack(test_ue_t *test_ue) &ie->value.choice.Target_ToSource_TransparentContainer; ogs_s1ap_buffer_to_OCTET_STRING( - OGS_HEX(payload, strlen(payload), hexbuf), 132, + ogs_hex_from_string(payload, hexbuf, sizeof(hexbuf)), 132, Target_ToSource_TransparentContainer); return ogs_s1ap_encode(&pdu); @@ -2016,7 +2016,7 @@ ogs_pkbuf_t *test_s1ap_build_invalid_packet(int i) pkbuf = ogs_pkbuf_alloc(NULL, OGS_MAX_SDU_LEN); ogs_assert(pkbuf); ogs_pkbuf_put_data(pkbuf, - OGS_HEX(payload[i], strlen(payload[i]), hexbuf), len[i]); + ogs_hex_from_string(payload[i], hexbuf, sizeof(hexbuf)), len[i]); return pkbuf; } @@ -2060,7 +2060,7 @@ ogs_pkbuf_t *test_s1ap_build_enb_configuration_transfer(int i) pkbuf = ogs_pkbuf_alloc(NULL, OGS_MAX_SDU_LEN); ogs_assert(pkbuf); ogs_pkbuf_put_data(pkbuf, - OGS_HEX(payload[i], strlen(payload[i]), hexbuf), len[i]); + ogs_hex_from_string(payload[i], hexbuf, sizeof(hexbuf)), len[i]); return pkbuf; } diff --git a/tests/common/sgsap-build.c b/tests/common/sgsap-build.c index afbb5d814..5d4a6324f 100644 --- a/tests/common/sgsap-build.c +++ b/tests/common/sgsap-build.c @@ -40,7 +40,7 @@ ogs_pkbuf_t *test_sgsap_location_update_accept(int i) pkbuf = ogs_pkbuf_alloc(NULL, OGS_MAX_SDU_LEN); ogs_assert(pkbuf); ogs_pkbuf_put_data(pkbuf, - OGS_HEX(payload[i], strlen(payload[i]), hexbuf), len[i]); + ogs_hex_from_string(payload[i], hexbuf, sizeof(hexbuf)), len[i]); return pkbuf; } @@ -64,7 +64,7 @@ ogs_pkbuf_t *test_sgsap_location_update_reject(int i) pkbuf = ogs_pkbuf_alloc(NULL, OGS_MAX_SDU_LEN); ogs_assert(pkbuf); ogs_pkbuf_put_data(pkbuf, - OGS_HEX(payload[i], strlen(payload[i]), hexbuf), len[i]); + ogs_hex_from_string(payload[i], hexbuf, sizeof(hexbuf)), len[i]); return pkbuf; } @@ -88,7 +88,7 @@ ogs_pkbuf_t *test_sgsap_imsi_detach_ack(int i) pkbuf = ogs_pkbuf_alloc(NULL, OGS_MAX_SDU_LEN); ogs_assert(pkbuf); ogs_pkbuf_put_data(pkbuf, - OGS_HEX(payload[i], strlen(payload[i]), hexbuf), len[i]); + ogs_hex_from_string(payload[i], hexbuf, sizeof(hexbuf)), len[i]); return pkbuf; } @@ -124,7 +124,7 @@ ogs_pkbuf_t *test_sgsap_paging_request(int i) pkbuf = ogs_pkbuf_alloc(NULL, OGS_MAX_SDU_LEN); ogs_assert(pkbuf); ogs_pkbuf_put_data(pkbuf, - OGS_HEX(payload[i], strlen(payload[i]), hexbuf), len[i]); + ogs_hex_from_string(payload[i], hexbuf, sizeof(hexbuf)), len[i]); return pkbuf; } @@ -148,7 +148,7 @@ ogs_pkbuf_t *test_sgsap_reset_indication(int i) pkbuf = ogs_pkbuf_alloc(NULL, OGS_MAX_SDU_LEN); ogs_assert(pkbuf); ogs_pkbuf_put_data(pkbuf, - OGS_HEX(payload[i], strlen(payload[i]), hexbuf), len[i]); + ogs_hex_from_string(payload[i], hexbuf, sizeof(hexbuf)), len[i]); return pkbuf; } @@ -172,7 +172,7 @@ ogs_pkbuf_t *test_sgsap_release_request(int i) pkbuf = ogs_pkbuf_alloc(NULL, OGS_MAX_SDU_LEN); ogs_assert(pkbuf); ogs_pkbuf_put_data(pkbuf, - OGS_HEX(payload[i], strlen(payload[i]), hexbuf), len[i]); + ogs_hex_from_string(payload[i], hexbuf, sizeof(hexbuf)), len[i]); return pkbuf; } @@ -193,7 +193,7 @@ ogs_pkbuf_t *test_sgsap_downlink_unitdata(int i) pkbuf = ogs_pkbuf_alloc(NULL, OGS_MAX_SDU_LEN); ogs_assert(pkbuf); ogs_pkbuf_put_data(pkbuf, - OGS_HEX(payload[i], strlen(payload[i]), hexbuf), len[i]); + ogs_hex_from_string(payload[i], hexbuf, sizeof(hexbuf)), len[i]); return pkbuf; } @@ -214,7 +214,7 @@ ogs_pkbuf_t *test_sgsap_mm_information_request(int i) pkbuf = ogs_pkbuf_alloc(NULL, OGS_MAX_SDU_LEN); ogs_assert(pkbuf); ogs_pkbuf_put_data(pkbuf, - OGS_HEX(payload[i], strlen(payload[i]), hexbuf), len[i]); + ogs_hex_from_string(payload[i], hexbuf, sizeof(hexbuf)), len[i]); return pkbuf; } diff --git a/tests/core/conv-test.c b/tests/core/conv-test.c index b93d2f9d9..62621aee5 100644 --- a/tests/core/conv-test.c +++ b/tests/core/conv-test.c @@ -35,16 +35,20 @@ static void conv_test2(abts_case *tc, void *data) char buffer[16]; - ABTS_TRUE(tc, memcmp(k, OGS_HEX(K, strlen(K), buffer), 16) == 0); + ABTS_TRUE(tc, memcmp(k, + ogs_hex_from_string(K, buffer, sizeof(buffer)), 16) == 0); - ABTS_TRUE(tc, memcmp(op, OGS_HEX(OP, strlen(OP), buffer), 16) == 0); + ABTS_TRUE(tc, memcmp(op, + ogs_hex_from_string(OP, buffer, sizeof(buffer)), 16) == 0); - ABTS_TRUE(tc, memcmp(opc, OGS_HEX(OPc, strlen(OPc), buffer), 16) == 0); + ABTS_TRUE(tc, memcmp(opc, + ogs_hex_from_string(OPc, buffer, sizeof(buffer)), 16) == 0); - ABTS_TRUE(tc, memcmp(amf, OGS_HEX(AMF, strlen(AMF), buffer), 2) == 0); + ABTS_TRUE(tc, memcmp(amf, + ogs_hex_from_string(AMF, buffer, sizeof(buffer)), 2) == 0); ABTS_TRUE(tc, memcmp(lower, - OGS_HEX(LOWER, strlen(LOWER), buffer), 4) == 0); + ogs_hex_from_string(LOWER, buffer, sizeof(buffer)), 4) == 0); } static void conv_test3(abts_case *tc, void *data) diff --git a/tests/core/tlv-test.c b/tests/core/tlv-test.c index 16700721d..b77f110d8 100644 --- a/tests/core/tlv-test.c +++ b/tests/core/tlv-test.c @@ -817,7 +817,7 @@ static void test6_func(abts_case *tc, void *data) ABTS_INT_EQUAL(tc, 182, req->len); ABTS_TRUE(tc, memcmp(req->data, - OGS_HEX(TEST_TLV_BUILD_MSG, strlen(TEST_TLV_BUILD_MSG), testbuf), + ogs_hex_from_string(TEST_TLV_BUILD_MSG, testbuf, sizeof(testbuf)), req->len) == 0); /* Initialize message value structure */ diff --git a/tests/crypt/abts-main.c b/tests/crypt/abts-main.c index 2af191bcb..ae4a0e8b9 100644 --- a/tests/crypt/abts-main.c +++ b/tests/crypt/abts-main.c @@ -24,6 +24,7 @@ abts_suite *test_aes(abts_suite *suite); abts_suite *test_sha(abts_suite *suite); abts_suite *test_base64(abts_suite *suite); +abts_suite *test_ecies(abts_suite *suite); const struct testlist { abts_suite *(*func)(abts_suite *suite); @@ -31,6 +32,7 @@ const struct testlist { {test_aes}, {test_sha}, {test_base64}, + {test_ecies}, {NULL}, }; diff --git a/tests/crypt/base64-test.c b/tests/crypt/base64-test.c index 6ee5ee9ca..19998667b 100644 --- a/tests/crypt/base64-test.c +++ b/tests/crypt/base64-test.c @@ -63,11 +63,97 @@ static void base64_test1(abts_case *tc, void *data) } } +struct decode_tests_st { + const char *name; + uint8_t *raw; + unsigned raw_size; + const char *pem; + unsigned pem_size; + int res; +}; + +struct decode_tests_st decode_tests[] = { + { + .name = "dec-rnd1", + .pem = "-----BEGIN dec-rnd1-----\n" + "9ppGioRpeiiD2lLNYC85eA==\n" + "-----END rnd1-----\n", + .raw = "\xf6\x9a\x46\x8a\x84\x69\x7a\x28\x83\xda\x52\xcd\x60\x2f\x39\x78", + .raw_size = 16, + .res = OGS_OK + }, + { + .name = "dec-rnd2", + .pem = "-----BEGIN dec-rnd2-----\n" + "LJ/7hUZ3TtPIz2dlc5+YvELe+Q==\n" + "-----END rnd2-----\n", + .raw = "\x2c\x9f\xfb\x85\x46\x77\x4e\xd3\xc8\xcf\x67\x65\x73\x9f\x98\xbc\x42\xde\xf9", + .raw_size = 19, + .res = OGS_OK + }, + { + .name = "dec-extra-chars", + .pem = "-----BEGIN dec-extra-chars----- \n\n" + "\n\n LJ/7hUZ3TtPIz2dlc5+YvELe+Q== \n" + " -----END rnd2----- \n ", + .raw = "\x2c\x9f\xfb\x85\x46\x77\x4e\xd3\xc8\xcf\x67\x65\x73\x9f\x98\xbc\x42\xde\xf9", + .raw_size = 19, + .res = OGS_OK + }, +#if 0 + { + .name = "dec-invalid-header", + .pem = "-----BEGIN dec-xxx-----\n" + "LJ/7hUZ3TtPIz2dlc5+YvELe+Q==\n" + "-----END rnd2-----\n", + .raw = "\x2c\x9f\xfb\x85\x46\x77\x4e\xd3\xc8\xcf\x67\x65\x73\x9f\x98\xbc\x42\xde\xf9", + .raw_size = 19, + .res = OGS_ERROR + }, + { + .name = "leak1", + .pem = "-----BEGIN leak1-----E-\x00\x00-----END ", + .pem_size = 34, + .raw = "", + .raw_size = 0, + .res = OGS_ERROR + }, +#endif +}; + +static void base64_test2(abts_case *tc, void *data) +{ + int i, rv; + ogs_datum_t result; + + for (i = 0; i < sizeof(decode_tests)/sizeof(decode_tests[0]); i++) { + if (!decode_tests[i].pem_size) + decode_tests[i].pem_size = strlen(decode_tests[i].pem); + + rv = ogs_fbase64_decode( + decode_tests[i].name, + (const uint8_t *)decode_tests[i].pem, decode_tests[i].pem_size, + &result); + + ABTS_INT_EQUAL(tc, decode_tests[i].res, rv); + + if (result.size) { + ABTS_INT_EQUAL(tc, decode_tests[i].raw_size, result.size); + ABTS_TRUE(tc, + memcmp(decode_tests[i].raw, result.data, result.size) == 0); + } + + if (result.data) + ogs_free(result.data); + } +} + abts_suite *test_base64(abts_suite *suite) { suite = ADD_SUITE(suite) abts_run_test(suite, base64_test1, NULL); + abts_run_test(suite, base64_test2, NULL); return suite; } diff --git a/tests/crypt/ecies-test.c b/tests/crypt/ecies-test.c new file mode 100644 index 000000000..5e617536a --- /dev/null +++ b/tests/crypt/ecies-test.c @@ -0,0 +1,385 @@ +/* + * Copyright (C) 2022 by Sukchan Lee + * + * This file is part of Open5GS. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "ogs-crypt.h" +#include "core/abts.h" + +static void ecies_profile_a(abts_case *tc, void *data) +{ + const char *_e[] = { + "c53c22208b61860b06c62e5406a7b330c2b577aa5558981510d128247d38bd1d", + "c80949f13ebe61af4ebdbd293ea4f942696b9e815d7e8f0096bbf6ed7de62256", + "C53C22208B61860B06C62E5406A7B330C2B577AA5558981510D128247D38BD1D", + "BE9EFF3E9F22A4B42A3D236E7A6C500B3F2E7E0C7449988BA800D664BF4FCD97", + + /* Private Key in curve25519-1.key */ + "10c9c67e861a5625a1db8f684123896d9b3506199d3df1968e07b6c8448bb147", + /* Private Key in TS33.501 C4.3.1 */ + "c80949f13ebe61af4ebdbd293ea4f942696b9e815d7e8f0096bbf6ed7de62256", + + }; + const char *_k[] = { + "b2e92f836055a255837debf850b528997ce0201cb82adfe4be1f587d07d8457d", + "5a8d38864820197c3394b92613b20b91633cbd897119273bf8e4a6f4eec0a650", + "977D8B2FDAA7B64AA700D04227D5B440630EA4EC50F9082273A26BB678C92222", + "5A8D38864820197C3394B92613B20B91633CBD897119273BF8e4A6f4EEC0A650", + + /* Public Key in TS33.501 C4.3.1 */ + "b2e92f836055a255837debf850b528997ce0201cb82adfe4be1f587d07d8457d", + /* Public Key in curve25519-1.key */ + "e421686f6fb2d70e3fa28d940494095686c3179fef53514667a6ed106b8a7d3d", + + }; + const char *_ek[] = { + "028ddf890ec83cdf163947ce45f6ec1a0e3070ea5fe57e2b1f05139f3e82422a", + "028ddf890ec83cdf163947ce45f6ec1a0e3070ea5fe57e2b1f05139f3e82422a", + "511C1DF473BB88317F923501F8BA944FD3B667D25699DCB552DBCEF60BBDC56D", + "511C1DF473BB88317F923501F8BA944FD3B667D25699DCB552DBCEF60BBDC56D", + + /* Share Key from curve25519-1.key + TS33.501 C4.3.1 */ + "514bacfdc28039187eec8196339d3ef2665691cd13abcc2a5df15561d9348c60", + /* Share Key from curve25519-1.key + TS33.501 C4.3.1 */ + "514bacfdc28039187eec8196339d3ef2665691cd13abcc2a5df15561d9348c60", + }; + int i, num; + + uint8_t e[OGS_ECCKEY_LEN]; + uint8_t k[OGS_ECCKEY_LEN]; + uint8_t ek[OGS_ECCKEY_LEN]; + uint8_t tmp[OGS_ECCKEY_LEN]; + + num = 6; + for (i = 0; i < num; i++) { + curve25519_donna(ek, + ogs_hex_from_string(_e[i], e, sizeof(e)), + ogs_hex_from_string(_k[i], k, sizeof(k))); + + ABTS_TRUE(tc, memcmp(ek, + ogs_hex_from_string(_ek[i], tmp, sizeof(tmp)), OGS_ECCKEY_LEN) == 0); + } +} + +static void ecies_profile_b(abts_case *tc, void *data) +{ + const char *_e[] = { + "F1AB1074477EBCC7F554EA1C5FC368B1616730155E0041AC447D6301975FECDA", + "99798858A1DC6A2C68637149A4B1DBFD1FDFF5ADDD62A2142F06699ED7602529", + "F1AB1074477EBCC7F554EA1C5FC368B1616730155E0041AC447D6301975FECDA", + "90A5898BD29FFA3F261E00E980067C70A2B1B992A21F5B4FEF6D4DF69FE804AD", + + /* Private Key in secp256r1-2.key */ + "74a9f918471f56f3befda5c51d738a3f94f5a52d4bc9db9799f5225fbccdde41", + /* Private Key in TS33.501 C4.4.1 */ + "99798858A1DC6A2C68637149A4B1DBFD1FDFF5ADDD62A2142F06699ED7602529", + }; + const char *_k[] = { + "039AAB8376597021E855679A9778EA0B67396E68C66DF32C0F41E9ACCA2DA9B9D1", + "0272DA71976234CE833A6907425867B82E074D44EF907DFB4B3E21C1C2256EBCD1", + "03759BB22C563D9F4A6B3C1419E543FC2F39D6823F02A9D71162B39399218B244B", + "0272DA71976234CE833A6907425867B82E074D44EF907DFB4B3E21C1C2256EBCD1", + + /* Public Key in TS33.501 C4.4.1 */ + "039AAB8376597021E855679A9778EA0B67396E68C66DF32C0F41E9ACCA2DA9B9D1", + /* Public Key in secp256r1-2.key */ + "03adefcd1317d1ce8562ec25b91b4800120e1236d6e2661ea4235a84e3c85da244", + }; + const char *_ek[] = { + "6C7E6518980025B982FBB2FF746E3C2E85A196D252099A7AD23EA7B4C0959CAE", + "6C7E6518980025B982FBB2FF746E3C2E85A196D252099A7AD23EA7B4C0959CAE", + "BC3529ED79541CF8C007CE9806330F4A5FF15064D7CF4B16943EF8F007597872", + "BC3529ED79541CF8C007CE9806330F4A5FF15064D7CF4B16943EF8F007597872", + + /* Share Key from secp256r1-2.key + TS33.501 C4.4.1 */ + "4072b8e3989b8695b35b99c923069508726fa3e65551aca1ec9560c501c190ce", + /* Share Key from secp256r1-2.key + TS33.501 C4.4.1 */ + "4072b8e3989b8695b35b99c923069508726fa3e65551aca1ec9560c501c190ce", + }; + + uint8_t e[OGS_ECCKEY_LEN]; + uint8_t k[OGS_ECCKEY_LEN+1]; + uint8_t ek[OGS_ECCKEY_LEN]; + uint8_t tmp[OGS_ECCKEY_LEN]; + + int i, r, num; + + num = 6; + for (i = 0; i < num; i++) { + r = ecdh_shared_secret( + ogs_hex_from_string(_k[i], k, sizeof(k)), + ogs_hex_from_string(_e[i], e, sizeof(e)), + ek); + ABTS_INT_EQUAL(tc, 1, r); + + ABTS_TRUE(tc, memcmp(ek, + ogs_hex_from_string(_ek[i], tmp, sizeof(tmp)), OGS_ECCKEY_LEN) == 0); + } +} + +static void ansi_x963_kdf(abts_case *tc, void *data) +{ + const char *_z[] = { + "028ddf890ec83cdf163947ce45f6ec1a0e3070ea5fe57e2b1f05139f3e82422a", + "511C1DF473BB88317F923501F8BA944FD3B667D25699DCB552DBCEF60BBDC56D", + "6C7E6518980025B982FBB2FF746E3C2E85A196D252099A7AD23EA7B4C0959CAE", + "BC3529ED79541CF8C007CE9806330F4A5FF15064D7CF4B16943EF8F007597872", + + /* Share Key from curve25519-1.key + TS33.501 C4.3.1 */ + "514bacfdc28039187eec8196339d3ef2665691cd13abcc2a5df15561d9348c60", + /* Share Key from secp256r1-2.key + TS33.501 C4.4.1 */ + "4072b8e3989b8695b35b99c923069508726fa3e65551aca1ec9560c501c190ce", + }; + const char *_info[] = { + "b2e92f836055a255837debf850b528997ce0201cb82adfe4be1f587d07d8457d", + "977D8B2FDAA7B64AA700D04227D5B440630EA4EC50F9082273A26BB678C92222", + "039AAB8376597021E855679A9778EA0B67396E68C66DF32C0F41E9ACCA2DA9B9D1", + "03759BB22C563D9F4A6B3C1419E543FC2F39D6823F02A9D71162B39399218B244B", + + /* Public Key in TS33.501 C4.3.1 */ + "b2e92f836055a255837debf850b528997ce0201cb82adfe4be1f587d07d8457d", + /* Public Key in TS33.501 C4.4.1 */ + "039AAB8376597021E855679A9778EA0B67396E68C66DF32C0F41E9ACCA2DA9B9D1", + }; + int info_len[] = { + 32, + 32, + 33, + 33, + + 32, + 33, + }; + const char *_ek[] = { + "2ba342cabd2b3b1e5e4e890da11b65f6", + "FE77B87D87F40428EDD71BCA69D79059", + "8A65C3AED80295C12BD55087E965702A", + "84F9A78995D39E6968047547ECC12C4F", + + /* Enc Key from curve25519-1.key + TS33.501 C4.3.1 */ + "6a898587f494ed58a1185b01207b6710", + /* Enc Key from secp256r1-2.key + TS33.501 C4.4.1 */ + "a950bf5f8922b395f9b6bcf8ddfa48c2", + }; + const char *_icb[] = { + "e2622cb0cdd08204e721c8ea9b95a7c6", + "cafa5287de2b20e3df1bd3a858da00ac", + "EF285B4061C3BAEE858AB6EC68487DAE", + "a1a6b7f89d422a733675996ebe781eb7", + + /* ICB from curve25519-1.key + TS33.501 C4.3.1 */ + "ebfc734de36bf03d5e94b9f9db800b32", + /* ICB from secp256r1-2.key + TS33.501 C4.4.1 */ + "ff0b5323557bd5e2b1da731cd6ca1416", + }; + const char *_mk[] = { + "d9846966fb7cf5fcf11266c5957dea60b83fff2b7c940690a4bfe57b1eb52bd2", + "D87B69F4FE8CD6B211264EA5E69F682F151A82252684CDB15A047E6EF0595028", + "A5EBAC0BC48D9CF7AE5CE39CD840AC6C761AEC04078FAB954D634F923E901C64", + "39D5517E965F8E1252B61345ED45226C5F1A8C69F03D6C91437591F0B8E48FA0", + + /* MAC Key from curve25519-1.key + TS33.501 C4.3.1 */ + "19045a963a79b4e397f2b81eb0258cbc0a64c9102aaf9e936d5c9f6d44fd8434", + /* MAC Key from secp256r1-2.key + TS33.501 C4.4.1 */ + "96f5757bcdb3f4c33976afc96cf7883d3dd017feb3ac229fd813d535e23ed58f", + }; + + uint8_t z[OGS_ECCKEY_LEN]; + uint8_t info[OGS_ECCKEY_LEN+1]; + + uint8_t ek[OGS_KEY_LEN]; + uint8_t icb[OGS_IVEC_LEN]; + uint8_t mk[OGS_SHA256_DIGEST_SIZE]; + + uint8_t tmp[OGS_SHA256_DIGEST_SIZE]; + + int i, num; + + num = 6; + for (i = 0; i < num; i++) { + ogs_kdf_ansi_x963( + ogs_hex_from_string(_z[i], z, sizeof(z)), OGS_ECCKEY_LEN, + ogs_hex_from_string(_info[i], info, sizeof(info)), info_len[i], + ek, icb, mk); + ABTS_TRUE(tc, memcmp(ek, + ogs_hex_from_string(_ek[i], tmp, sizeof(tmp)), OGS_KEY_LEN) == 0); + ABTS_TRUE(tc, memcmp(icb, + ogs_hex_from_string(_icb[i], tmp, sizeof(tmp)), OGS_IVEC_LEN) == 0); + ABTS_TRUE(tc, memcmp(mk, + ogs_hex_from_string(_mk[i], tmp, sizeof(tmp)), OGS_SHA256_DIGEST_SIZE) == 0); + } +} + +static void aes_128ctr(abts_case *tc, void *data) +{ + const char *_ek[] = { + "2ba342cabd2b3b1e5e4e890da11b65f6", + "FE77B87D87F40428EDD71BCA69D79059", + "8A65C3AED80295C12BD55087E965702A", + "84F9A78995D39E6968047547ECC12C4F", + + /* Enc Key from curve25519-1.key + TS33.501 C4.3.1 */ + "6a898587f494ed58a1185b01207b6710", + /* Enc Key from secp256r1-2.key + TS33.501 C4.4.1 */ + "a950bf5f8922b395f9b6bcf8ddfa48c2", + }; + const char *_icb[] = { + "e2622cb0cdd08204e721c8ea9b95a7c6", + "cafa5287de2b20e3df1bd3a858da00ac", + "EF285B4061C3BAEE858AB6EC68487DAE", + "a1a6b7f89d422a733675996ebe781eb7", + + /* ICB from curve25519-1.key + TS33.501 C4.3.1 */ + "ebfc734de36bf03d5e94b9f9db800b32", + /* ICB from secp256r1-2.key + TS33.501 C4.4.1 */ + "ff0b5323557bd5e2b1da731cd6ca1416", + }; + const char *_plain[] = { + "00012080f6", + "766572796C6F6E67757365726E616D6531", + "00012080f6", + "766572796C6F6E67757365726E616D6531", + + /* Plain-text from curve25519-1.key + TS33.501 C4.3.1 */ + "00012080f6", + /* Plain-text from secp256r1-2.key + TS33.501 C4.4.1 */ + "00012080f6", + }; + const char *_cipher[] = { + "cb02352410", + "8E358A1582ADB15322C10E515141D2039A", + "46A33FC271", + "BE22D8B9F856A52ED381CD7EAF4CF2D525", + + /* Ciphered-text from curve25519-1.key + TS33.501 C4.3.1 */ + "7ee4435e19", + /* Ciphered-text from secp256r1-2.key + TS33.501 C4.4.1 */ + "cdd22e3496", + }; + int len[] = { + 5, + 17, + 5, + 17, + + 5, + 5, + }; + + uint8_t ek[OGS_KEY_LEN]; + uint8_t icb[OGS_IVEC_LEN]; + uint8_t plain[17]; + uint8_t cipher[17]; + uint8_t tmp[17]; + + int i, num; + + num = 6; + for (i = 0; i < num; i++) { + ogs_aes_ctr128_encrypt( + ogs_hex_from_string(_ek[i], ek, sizeof(ek)), + ogs_hex_from_string(_icb[i], icb, sizeof(icb)), + ogs_hex_from_string(_plain[i], plain, sizeof(plain)), len[i], + cipher); + ABTS_TRUE(tc, memcmp(cipher, + ogs_hex_from_string(_cipher[i], tmp, sizeof(tmp)), len[i]) == 0); + ogs_aes_ctr128_encrypt( + ogs_hex_from_string(_ek[i], ek, sizeof(ek)), + ogs_hex_from_string(_icb[i], icb, sizeof(icb)), + ogs_hex_from_string(_cipher[i], cipher, sizeof(cipher)), len[i], + plain); + ABTS_TRUE(tc, memcmp(plain, + ogs_hex_from_string(_plain[i], tmp, sizeof(tmp)), len[i]) == 0); + } +} + +static void hmac_sha_256(abts_case *tc, void *data) +{ + const char *_mk[] = { + "d9846966fb7cf5fcf11266c5957dea60b83fff2b7c940690a4bfe57b1eb52bd2", + "D87B69F4FE8CD6B211264EA5E69F682F151A82252684CDB15A047E6EF0595028", + "A5EBAC0BC48D9CF7AE5CE39CD840AC6C761AEC04078FAB954D634F923E901C64", + "39D5517E965F8E1252B61345ED45226C5F1A8C69F03D6C91437591F0B8E48FA0", + + /* MAC Key from curve25519-1.key + TS33.501 C4.3.1 */ + "19045a963a79b4e397f2b81eb0258cbc0a64c9102aaf9e936d5c9f6d44fd8434", + /* MAC Key from secp256r1-2.key + TS33.501 C4.4.1 */ + "96f5757bcdb3f4c33976afc96cf7883d3dd017feb3ac229fd813d535e23ed58f", + }; + const char *_cipher[] = { + "cb02352410", + "8E358A1582ADB15322C10E515141D2039A", + "46A33FC271", + "BE22D8B9F856A52ED381CD7EAF4CF2D525", + + /* Ciphered-text from curve25519-1.key + TS33.501 C4.3.1 */ + "7ee4435e19", + /* Ciphered-text from secp256r1-2.key + TS33.501 C4.4.1 */ + "cdd22e3496", + }; + const char *_mac_tag[] = { + "cddd9e730ef3fa87", + "12E1D7783A97F1AC", + "6AC7DAE96AA30A4D", + "3CDDC61A0A7882EB", + + /* MAG-tag value from curve25519-1.key + TS33.501 C4.3.1 */ + "78dc897bff24129c", + /* MAC-tag value secp256r1-2.key + TS33.501 C4.4.1 */ + "5500e9242e65f58c", + }; + int len[] = { + 5, + 17, + 5, + 17, + + 5, + 5, + }; + + uint8_t mk[OGS_SHA256_DIGEST_SIZE]; + uint8_t cipher[17]; + uint8_t mac_tag[OGS_MACTAG_LEN]; + uint8_t tmp[OGS_MACTAG_LEN]; + + int i, num; + + num = 6; + for (i = 0; i < num; i++) { + ogs_hmac_sha256( + ogs_hex_from_string(_mk[i], mk, sizeof(mk)), OGS_SHA256_DIGEST_SIZE, + ogs_hex_from_string(_cipher[i], cipher, sizeof(cipher)), len[i], + mac_tag, OGS_MACTAG_LEN); + ABTS_TRUE(tc, memcmp(mac_tag, + ogs_hex_from_string(_mac_tag[i], tmp, sizeof(tmp)), + OGS_MACTAG_LEN) == 0); + } +} + +abts_suite *test_ecies(abts_suite *suite) +{ + suite = ADD_SUITE(suite) + + abts_run_test(suite, ecies_profile_a, NULL); + abts_run_test(suite, ecies_profile_b, NULL); + abts_run_test(suite, ansi_x963_kdf, NULL); + abts_run_test(suite, aes_128ctr, NULL); + abts_run_test(suite, hmac_sha_256, NULL); + + return suite; +} diff --git a/tests/crypt/meson.build b/tests/crypt/meson.build index 3946954e6..053343518 100644 --- a/tests/crypt/meson.build +++ b/tests/crypt/meson.build @@ -19,6 +19,7 @@ testunit_crypt_sources = files(''' aes-test.c sha-test.c base64-test.c + ecies-test.c abts-main.c '''.split()) diff --git a/tests/csfb/crash-test.c b/tests/csfb/crash-test.c index 92b2d9b12..c7649f69e 100644 --- a/tests/csfb/crash-test.c +++ b/tests/csfb/crash-test.c @@ -48,15 +48,10 @@ static void test1_func(abts_case *tc, void *data) mobile_identity_suci.routing_indicator2 = 0xf; mobile_identity_suci.routing_indicator3 = 0xf; mobile_identity_suci.routing_indicator4 = 0xf; - mobile_identity_suci.protection_scheme_id = OGS_NAS_5GS_NULL_SCHEME; + mobile_identity_suci.protection_scheme_id = OGS_PROTECTION_SCHEME_NULL; mobile_identity_suci.home_network_pki_value = 0; - mobile_identity_suci.scheme_output[0] = 0x37; - mobile_identity_suci.scheme_output[1] = 0x46; - mobile_identity_suci.scheme_output[2] = 0; - mobile_identity_suci.scheme_output[3] = 0; - mobile_identity_suci.scheme_output[4] = 0x06; - test_ue = test_ue_add_by_suci(&mobile_identity_suci, 13); + test_ue = test_ue_add_by_suci(&mobile_identity_suci, "3746000006"); ogs_assert(test_ue); test_ue->e_cgi.cell_id = 0x19b010; diff --git a/tests/csfb/mo-active-test.c b/tests/csfb/mo-active-test.c index 01b05ff75..98d4947aa 100644 --- a/tests/csfb/mo-active-test.c +++ b/tests/csfb/mo-active-test.c @@ -48,15 +48,10 @@ static void test1_func(abts_case *tc, void *data) mobile_identity_suci.routing_indicator2 = 0xf; mobile_identity_suci.routing_indicator3 = 0xf; mobile_identity_suci.routing_indicator4 = 0xf; - mobile_identity_suci.protection_scheme_id = OGS_NAS_5GS_NULL_SCHEME; + mobile_identity_suci.protection_scheme_id = OGS_PROTECTION_SCHEME_NULL; mobile_identity_suci.home_network_pki_value = 0; - mobile_identity_suci.scheme_output[0] = 0x37; - mobile_identity_suci.scheme_output[1] = 0x46; - mobile_identity_suci.scheme_output[2] = 0; - mobile_identity_suci.scheme_output[3] = 0; - mobile_identity_suci.scheme_output[4] = 0x06; - test_ue = test_ue_add_by_suci(&mobile_identity_suci, 13); + test_ue = test_ue_add_by_suci(&mobile_identity_suci, "3746000006"); ogs_assert(test_ue); test_ue->e_cgi.cell_id = 0x19b010; diff --git a/tests/csfb/mo-idle-test.c b/tests/csfb/mo-idle-test.c index 8cfb9210a..cf2dfac31 100644 --- a/tests/csfb/mo-idle-test.c +++ b/tests/csfb/mo-idle-test.c @@ -48,15 +48,10 @@ static void test1_func(abts_case *tc, void *data) mobile_identity_suci.routing_indicator2 = 0xf; mobile_identity_suci.routing_indicator3 = 0xf; mobile_identity_suci.routing_indicator4 = 0xf; - mobile_identity_suci.protection_scheme_id = OGS_NAS_5GS_NULL_SCHEME; + mobile_identity_suci.protection_scheme_id = OGS_PROTECTION_SCHEME_NULL; mobile_identity_suci.home_network_pki_value = 0; - mobile_identity_suci.scheme_output[0] = 0x37; - mobile_identity_suci.scheme_output[1] = 0x46; - mobile_identity_suci.scheme_output[2] = 0; - mobile_identity_suci.scheme_output[3] = 0; - mobile_identity_suci.scheme_output[4] = 0x06; - test_ue = test_ue_add_by_suci(&mobile_identity_suci, 13); + test_ue = test_ue_add_by_suci(&mobile_identity_suci, "3746000006"); ogs_assert(test_ue); test_ue->e_cgi.cell_id = 0x19b010; @@ -337,15 +332,10 @@ static void test2_func(abts_case *tc, void *data) mobile_identity_suci.routing_indicator2 = 0xf; mobile_identity_suci.routing_indicator3 = 0xf; mobile_identity_suci.routing_indicator4 = 0xf; - mobile_identity_suci.protection_scheme_id = OGS_NAS_5GS_NULL_SCHEME; + mobile_identity_suci.protection_scheme_id = OGS_PROTECTION_SCHEME_NULL; mobile_identity_suci.home_network_pki_value = 0; - mobile_identity_suci.scheme_output[0] = 0x37; - mobile_identity_suci.scheme_output[1] = 0x46; - mobile_identity_suci.scheme_output[2] = 0; - mobile_identity_suci.scheme_output[3] = 0; - mobile_identity_suci.scheme_output[4] = 0x06; - test_ue = test_ue_add_by_suci(&mobile_identity_suci, 13); + test_ue = test_ue_add_by_suci(&mobile_identity_suci, "3746000006"); ogs_assert(test_ue); test_ue->e_cgi.cell_id = 0x19b010; @@ -537,15 +527,10 @@ static void test3_func(abts_case *tc, void *data) mobile_identity_suci.routing_indicator2 = 0xf; mobile_identity_suci.routing_indicator3 = 0xf; mobile_identity_suci.routing_indicator4 = 0xf; - mobile_identity_suci.protection_scheme_id = OGS_NAS_5GS_NULL_SCHEME; + mobile_identity_suci.protection_scheme_id = OGS_PROTECTION_SCHEME_NULL; mobile_identity_suci.home_network_pki_value = 0; - mobile_identity_suci.scheme_output[0] = 0x37; - mobile_identity_suci.scheme_output[1] = 0x46; - mobile_identity_suci.scheme_output[2] = 0; - mobile_identity_suci.scheme_output[3] = 0; - mobile_identity_suci.scheme_output[4] = 0x06; - test_ue = test_ue_add_by_suci(&mobile_identity_suci, 13); + test_ue = test_ue_add_by_suci(&mobile_identity_suci, "3746000006"); ogs_assert(test_ue); test_ue->e_cgi.cell_id = 0x19b010; @@ -823,15 +808,10 @@ static void test4_func(abts_case *tc, void *data) mobile_identity_suci.routing_indicator2 = 0xf; mobile_identity_suci.routing_indicator3 = 0xf; mobile_identity_suci.routing_indicator4 = 0xf; - mobile_identity_suci.protection_scheme_id = OGS_NAS_5GS_NULL_SCHEME; + mobile_identity_suci.protection_scheme_id = OGS_PROTECTION_SCHEME_NULL; mobile_identity_suci.home_network_pki_value = 0; - mobile_identity_suci.scheme_output[0] = 0x37; - mobile_identity_suci.scheme_output[1] = 0x46; - mobile_identity_suci.scheme_output[2] = 0; - mobile_identity_suci.scheme_output[3] = 0; - mobile_identity_suci.scheme_output[4] = 0x06; - test_ue = test_ue_add_by_suci(&mobile_identity_suci, 13); + test_ue = test_ue_add_by_suci(&mobile_identity_suci, "3746000006"); ogs_assert(test_ue); test_ue->e_cgi.cell_id = 0x19b010; diff --git a/tests/csfb/mo-sms-test.c b/tests/csfb/mo-sms-test.c index 8d60f04f1..8dfeb920f 100644 --- a/tests/csfb/mo-sms-test.c +++ b/tests/csfb/mo-sms-test.c @@ -48,15 +48,10 @@ static void test1_func(abts_case *tc, void *data) mobile_identity_suci.routing_indicator2 = 0xf; mobile_identity_suci.routing_indicator3 = 0xf; mobile_identity_suci.routing_indicator4 = 0xf; - mobile_identity_suci.protection_scheme_id = OGS_NAS_5GS_NULL_SCHEME; + mobile_identity_suci.protection_scheme_id = OGS_PROTECTION_SCHEME_NULL; mobile_identity_suci.home_network_pki_value = 0; - mobile_identity_suci.scheme_output[0] = 0x37; - mobile_identity_suci.scheme_output[1] = 0x46; - mobile_identity_suci.scheme_output[2] = 0; - mobile_identity_suci.scheme_output[3] = 0; - mobile_identity_suci.scheme_output[4] = 0x06; - test_ue = test_ue_add_by_suci(&mobile_identity_suci, 13); + test_ue = test_ue_add_by_suci(&mobile_identity_suci, "3746000006"); ogs_assert(test_ue); test_ue->e_cgi.cell_id = 0x19b010; diff --git a/tests/csfb/mt-active-test.c b/tests/csfb/mt-active-test.c index b6c247979..7a10a384a 100644 --- a/tests/csfb/mt-active-test.c +++ b/tests/csfb/mt-active-test.c @@ -48,15 +48,10 @@ static void test1_func(abts_case *tc, void *data) mobile_identity_suci.routing_indicator2 = 0xf; mobile_identity_suci.routing_indicator3 = 0xf; mobile_identity_suci.routing_indicator4 = 0xf; - mobile_identity_suci.protection_scheme_id = OGS_NAS_5GS_NULL_SCHEME; + mobile_identity_suci.protection_scheme_id = OGS_PROTECTION_SCHEME_NULL; mobile_identity_suci.home_network_pki_value = 0; - mobile_identity_suci.scheme_output[0] = 0x37; - mobile_identity_suci.scheme_output[1] = 0x46; - mobile_identity_suci.scheme_output[2] = 0; - mobile_identity_suci.scheme_output[3] = 0; - mobile_identity_suci.scheme_output[4] = 0x06; - test_ue = test_ue_add_by_suci(&mobile_identity_suci, 13); + test_ue = test_ue_add_by_suci(&mobile_identity_suci, "3746000006"); ogs_assert(test_ue); test_ue->e_cgi.cell_id = 0x19b010; diff --git a/tests/csfb/mt-idle-test.c b/tests/csfb/mt-idle-test.c index 945500427..326445af8 100644 --- a/tests/csfb/mt-idle-test.c +++ b/tests/csfb/mt-idle-test.c @@ -48,15 +48,10 @@ static void test1_func(abts_case *tc, void *data) mobile_identity_suci.routing_indicator2 = 0xf; mobile_identity_suci.routing_indicator3 = 0xf; mobile_identity_suci.routing_indicator4 = 0xf; - mobile_identity_suci.protection_scheme_id = OGS_NAS_5GS_NULL_SCHEME; + mobile_identity_suci.protection_scheme_id = OGS_PROTECTION_SCHEME_NULL; mobile_identity_suci.home_network_pki_value = 0; - mobile_identity_suci.scheme_output[0] = 0x37; - mobile_identity_suci.scheme_output[1] = 0x46; - mobile_identity_suci.scheme_output[2] = 0; - mobile_identity_suci.scheme_output[3] = 0; - mobile_identity_suci.scheme_output[4] = 0x06; - test_ue = test_ue_add_by_suci(&mobile_identity_suci, 13); + test_ue = test_ue_add_by_suci(&mobile_identity_suci, "3746000006"); ogs_assert(test_ue); test_ue->e_cgi.cell_id = 0x19b010; diff --git a/tests/csfb/mt-sms-test.c b/tests/csfb/mt-sms-test.c index 82da432c7..a254eb181 100644 --- a/tests/csfb/mt-sms-test.c +++ b/tests/csfb/mt-sms-test.c @@ -48,15 +48,10 @@ static void test1_func(abts_case *tc, void *data) mobile_identity_suci.routing_indicator2 = 0xf; mobile_identity_suci.routing_indicator3 = 0xf; mobile_identity_suci.routing_indicator4 = 0xf; - mobile_identity_suci.protection_scheme_id = OGS_NAS_5GS_NULL_SCHEME; + mobile_identity_suci.protection_scheme_id = OGS_PROTECTION_SCHEME_NULL; mobile_identity_suci.home_network_pki_value = 0; - mobile_identity_suci.scheme_output[0] = 0x37; - mobile_identity_suci.scheme_output[1] = 0x46; - mobile_identity_suci.scheme_output[2] = 0; - mobile_identity_suci.scheme_output[3] = 0; - mobile_identity_suci.scheme_output[4] = 0x06; - test_ue = test_ue_add_by_suci(&mobile_identity_suci, 13); + test_ue = test_ue_add_by_suci(&mobile_identity_suci, "3746000006"); ogs_assert(test_ue); test_ue->e_cgi.cell_id = 0x19b010; diff --git a/tests/handover/5gc-n2-test.c b/tests/handover/5gc-n2-test.c index b0ba850ef..40d4f9ecb 100644 --- a/tests/handover/5gc-n2-test.c +++ b/tests/handover/5gc-n2-test.c @@ -53,15 +53,10 @@ static void failure_func(abts_case *tc, void *data) mobile_identity_suci.routing_indicator2 = 0xf; mobile_identity_suci.routing_indicator3 = 0xf; mobile_identity_suci.routing_indicator4 = 0xf; - mobile_identity_suci.protection_scheme_id = OGS_NAS_5GS_NULL_SCHEME; + mobile_identity_suci.protection_scheme_id = OGS_PROTECTION_SCHEME_NULL; mobile_identity_suci.home_network_pki_value = 0; - mobile_identity_suci.scheme_output[0] = 0; - mobile_identity_suci.scheme_output[1] = 0; - mobile_identity_suci.scheme_output[2] = 0x20; - mobile_identity_suci.scheme_output[3] = 0x31; - mobile_identity_suci.scheme_output[4] = 0x90; - test_ue = test_ue_add_by_suci(&mobile_identity_suci, 13); + test_ue = test_ue_add_by_suci(&mobile_identity_suci, "0000203190"); ogs_assert(test_ue); test_ue->nr_cgi.cell_id = 0x40001; @@ -456,15 +451,10 @@ static void direct_complete_func(abts_case *tc, void *data) mobile_identity_suci.routing_indicator2 = 0xf; mobile_identity_suci.routing_indicator3 = 0xf; mobile_identity_suci.routing_indicator4 = 0xf; - mobile_identity_suci.protection_scheme_id = OGS_NAS_5GS_NULL_SCHEME; + mobile_identity_suci.protection_scheme_id = OGS_PROTECTION_SCHEME_NULL; mobile_identity_suci.home_network_pki_value = 0; - mobile_identity_suci.scheme_output[0] = 0; - mobile_identity_suci.scheme_output[1] = 0; - mobile_identity_suci.scheme_output[2] = 0x20; - mobile_identity_suci.scheme_output[3] = 0x31; - mobile_identity_suci.scheme_output[4] = 0x90; - test_ue = test_ue_add_by_suci(&mobile_identity_suci, 13); + test_ue = test_ue_add_by_suci(&mobile_identity_suci, "0000203190"); ogs_assert(test_ue); test_ue->nr_cgi.cell_id = 0x40001; @@ -1075,15 +1065,10 @@ static void direct_cancel_func(abts_case *tc, void *data) mobile_identity_suci.routing_indicator2 = 0xf; mobile_identity_suci.routing_indicator3 = 0xf; mobile_identity_suci.routing_indicator4 = 0xf; - mobile_identity_suci.protection_scheme_id = OGS_NAS_5GS_NULL_SCHEME; + mobile_identity_suci.protection_scheme_id = OGS_PROTECTION_SCHEME_NULL; mobile_identity_suci.home_network_pki_value = 0; - mobile_identity_suci.scheme_output[0] = 0; - mobile_identity_suci.scheme_output[1] = 0; - mobile_identity_suci.scheme_output[2] = 0x20; - mobile_identity_suci.scheme_output[3] = 0x31; - mobile_identity_suci.scheme_output[4] = 0x90; - test_ue = test_ue_add_by_suci(&mobile_identity_suci, 13); + test_ue = test_ue_add_by_suci(&mobile_identity_suci, "0000203190"); ogs_assert(test_ue); test_ue->nr_cgi.cell_id = 0x40001; @@ -1494,15 +1479,10 @@ static void indirect_complete_func(abts_case *tc, void *data) mobile_identity_suci.routing_indicator2 = 0xf; mobile_identity_suci.routing_indicator3 = 0xf; mobile_identity_suci.routing_indicator4 = 0xf; - mobile_identity_suci.protection_scheme_id = OGS_NAS_5GS_NULL_SCHEME; + mobile_identity_suci.protection_scheme_id = OGS_PROTECTION_SCHEME_NULL; mobile_identity_suci.home_network_pki_value = 0; - mobile_identity_suci.scheme_output[0] = 0; - mobile_identity_suci.scheme_output[1] = 0; - mobile_identity_suci.scheme_output[2] = 0x20; - mobile_identity_suci.scheme_output[3] = 0x31; - mobile_identity_suci.scheme_output[4] = 0x90; - test_ue = test_ue_add_by_suci(&mobile_identity_suci, 13); + test_ue = test_ue_add_by_suci(&mobile_identity_suci, "0000203190"); ogs_assert(test_ue); test_ue->nr_cgi.cell_id = 0x40001; @@ -2149,15 +2129,10 @@ static void indirect_cancel_func(abts_case *tc, void *data) mobile_identity_suci.routing_indicator2 = 0xf; mobile_identity_suci.routing_indicator3 = 0xf; mobile_identity_suci.routing_indicator4 = 0xf; - mobile_identity_suci.protection_scheme_id = OGS_NAS_5GS_NULL_SCHEME; + mobile_identity_suci.protection_scheme_id = OGS_PROTECTION_SCHEME_NULL; mobile_identity_suci.home_network_pki_value = 0; - mobile_identity_suci.scheme_output[0] = 0; - mobile_identity_suci.scheme_output[1] = 0; - mobile_identity_suci.scheme_output[2] = 0x20; - mobile_identity_suci.scheme_output[3] = 0x31; - mobile_identity_suci.scheme_output[4] = 0x90; - test_ue = test_ue_add_by_suci(&mobile_identity_suci, 13); + test_ue = test_ue_add_by_suci(&mobile_identity_suci, "0000203190"); ogs_assert(test_ue); test_ue->nr_cgi.cell_id = 0x40001; diff --git a/tests/handover/5gc-xn-test.c b/tests/handover/5gc-xn-test.c index 5bf4a6699..4adb8aba8 100644 --- a/tests/handover/5gc-xn-test.c +++ b/tests/handover/5gc-xn-test.c @@ -53,15 +53,10 @@ static void test1_func(abts_case *tc, void *data) mobile_identity_suci.routing_indicator2 = 0xf; mobile_identity_suci.routing_indicator3 = 0xf; mobile_identity_suci.routing_indicator4 = 0xf; - mobile_identity_suci.protection_scheme_id = OGS_NAS_5GS_NULL_SCHEME; + mobile_identity_suci.protection_scheme_id = OGS_PROTECTION_SCHEME_NULL; mobile_identity_suci.home_network_pki_value = 0; - mobile_identity_suci.scheme_output[0] = 0; - mobile_identity_suci.scheme_output[1] = 0; - mobile_identity_suci.scheme_output[2] = 0x20; - mobile_identity_suci.scheme_output[3] = 0x31; - mobile_identity_suci.scheme_output[4] = 0x90; - test_ue = test_ue_add_by_suci(&mobile_identity_suci, 13); + test_ue = test_ue_add_by_suci(&mobile_identity_suci, "0000203190"); ogs_assert(test_ue); test_ue->nr_cgi.cell_id = 0x40001; diff --git a/tests/handover/epc-s1-test.c b/tests/handover/epc-s1-test.c index b22e6ea93..13b226896 100644 --- a/tests/handover/epc-s1-test.c +++ b/tests/handover/epc-s1-test.c @@ -49,15 +49,10 @@ static void test1_func(abts_case *tc, void *data) mobile_identity_suci.routing_indicator2 = 0xf; mobile_identity_suci.routing_indicator3 = 0xf; mobile_identity_suci.routing_indicator4 = 0xf; - mobile_identity_suci.protection_scheme_id = OGS_NAS_5GS_NULL_SCHEME; + mobile_identity_suci.protection_scheme_id = OGS_PROTECTION_SCHEME_NULL; mobile_identity_suci.home_network_pki_value = 0; - mobile_identity_suci.scheme_output[0] = 0x37; - mobile_identity_suci.scheme_output[1] = 0x46; - mobile_identity_suci.scheme_output[2] = 0; - mobile_identity_suci.scheme_output[3] = 0; - mobile_identity_suci.scheme_output[4] = 0x06; - test_ue = test_ue_add_by_suci(&mobile_identity_suci, 13); + test_ue = test_ue_add_by_suci(&mobile_identity_suci, "3746000006"); ogs_assert(test_ue); test_ue->e_cgi.cell_id = 0x1234560; @@ -619,15 +614,10 @@ static void test2_func(abts_case *tc, void *data) mobile_identity_suci.routing_indicator2 = 0xf; mobile_identity_suci.routing_indicator3 = 0xf; mobile_identity_suci.routing_indicator4 = 0xf; - mobile_identity_suci.protection_scheme_id = OGS_NAS_5GS_NULL_SCHEME; + mobile_identity_suci.protection_scheme_id = OGS_PROTECTION_SCHEME_NULL; mobile_identity_suci.home_network_pki_value = 0; - mobile_identity_suci.scheme_output[0] = 0x37; - mobile_identity_suci.scheme_output[1] = 0x46; - mobile_identity_suci.scheme_output[2] = 0; - mobile_identity_suci.scheme_output[3] = 0; - mobile_identity_suci.scheme_output[4] = 0x06; - test_ue = test_ue_add_by_suci(&mobile_identity_suci, 13); + test_ue = test_ue_add_by_suci(&mobile_identity_suci, "3746000006"); ogs_assert(test_ue); test_ue->e_cgi.cell_id = 0x1234560; @@ -946,15 +936,10 @@ static void test3_func(abts_case *tc, void *data) mobile_identity_suci.routing_indicator2 = 0xf; mobile_identity_suci.routing_indicator3 = 0xf; mobile_identity_suci.routing_indicator4 = 0xf; - mobile_identity_suci.protection_scheme_id = OGS_NAS_5GS_NULL_SCHEME; + mobile_identity_suci.protection_scheme_id = OGS_PROTECTION_SCHEME_NULL; mobile_identity_suci.home_network_pki_value = 0; - mobile_identity_suci.scheme_output[0] = 0x37; - mobile_identity_suci.scheme_output[1] = 0x46; - mobile_identity_suci.scheme_output[2] = 0; - mobile_identity_suci.scheme_output[3] = 0; - mobile_identity_suci.scheme_output[4] = 0x06; - test_ue = test_ue_add_by_suci(&mobile_identity_suci, 13); + test_ue = test_ue_add_by_suci(&mobile_identity_suci, "3746000006"); ogs_assert(test_ue); test_ue->e_cgi.cell_id = 0x1234560; diff --git a/tests/handover/epc-x2-test.c b/tests/handover/epc-x2-test.c index 0a1d58976..67993e528 100644 --- a/tests/handover/epc-x2-test.c +++ b/tests/handover/epc-x2-test.c @@ -46,15 +46,10 @@ static void test1_func(abts_case *tc, void *data) mobile_identity_suci.routing_indicator2 = 0xf; mobile_identity_suci.routing_indicator3 = 0xf; mobile_identity_suci.routing_indicator4 = 0xf; - mobile_identity_suci.protection_scheme_id = OGS_NAS_5GS_NULL_SCHEME; + mobile_identity_suci.protection_scheme_id = OGS_PROTECTION_SCHEME_NULL; mobile_identity_suci.home_network_pki_value = 0; - mobile_identity_suci.scheme_output[0] = 0x37; - mobile_identity_suci.scheme_output[1] = 0x46; - mobile_identity_suci.scheme_output[2] = 0; - mobile_identity_suci.scheme_output[3] = 0; - mobile_identity_suci.scheme_output[4] = 0x06; - test_ue = test_ue_add_by_suci(&mobile_identity_suci, 13); + test_ue = test_ue_add_by_suci(&mobile_identity_suci, "3746000006"); ogs_assert(test_ue); test_ue->e_cgi.cell_id = 0x1234560; diff --git a/tests/non3gpp/diameter-swx-path.c b/tests/non3gpp/diameter-swx-path.c index 7839ca736..575cb3520 100644 --- a/tests/non3gpp/diameter-swx-path.c +++ b/tests/non3gpp/diameter-swx-path.c @@ -201,8 +201,10 @@ static void test_swx_send_mar(struct sess_state *sess_data) uint64_t sqn_ms; int i; - OGS_HEX(test_ue->k_string, strlen(test_ue->k_string), test_ue->k); - OGS_HEX(test_ue->opc_string, strlen(test_ue->opc_string), test_ue->opc); + ogs_hex_from_string( + test_ue->k_string, test_ue->k, sizeof(test_ue->k)); + ogs_hex_from_string( + test_ue->opc_string, test_ue->opc, sizeof(test_ue->opc)); milenage_f2345(test_ue->opc, test_ue->k, test_ue->rand, NULL, NULL, NULL, NULL, ak); diff --git a/tests/non3gpp/epdg-test.c b/tests/non3gpp/epdg-test.c index 262254694..6dda4d876 100644 --- a/tests/non3gpp/epdg-test.c +++ b/tests/non3gpp/epdg-test.c @@ -56,15 +56,10 @@ static void test1_func(abts_case *tc, void *data) mobile_identity_suci.routing_indicator2 = 0xf; mobile_identity_suci.routing_indicator3 = 0xf; mobile_identity_suci.routing_indicator4 = 0xf; - mobile_identity_suci.protection_scheme_id = OGS_NAS_5GS_NULL_SCHEME; + mobile_identity_suci.protection_scheme_id = OGS_PROTECTION_SCHEME_NULL; mobile_identity_suci.home_network_pki_value = 0; - mobile_identity_suci.scheme_output[0] = 0x10; - mobile_identity_suci.scheme_output[1] = 0x32; - mobile_identity_suci.scheme_output[2] = 0x54; - mobile_identity_suci.scheme_output[3] = 0x86; - mobile_identity_suci.scheme_output[4] = 0x91; - test_ue = test_ue_add_by_suci(&mobile_identity_suci, 13); + test_ue = test_ue_add_by_suci(&mobile_identity_suci, "1032548691"); ogs_assert(test_ue); test_ue->e_cgi.cell_id = 0x1079baf; @@ -202,15 +197,10 @@ static void test2_func(abts_case *tc, void *data) mobile_identity_suci.routing_indicator2 = 0xf; mobile_identity_suci.routing_indicator3 = 0xf; mobile_identity_suci.routing_indicator4 = 0xf; - mobile_identity_suci.protection_scheme_id = OGS_NAS_5GS_NULL_SCHEME; + mobile_identity_suci.protection_scheme_id = OGS_PROTECTION_SCHEME_NULL; mobile_identity_suci.home_network_pki_value = 0; - mobile_identity_suci.scheme_output[0] = 0x10; - mobile_identity_suci.scheme_output[1] = 0x32; - mobile_identity_suci.scheme_output[2] = 0x54; - mobile_identity_suci.scheme_output[3] = 0x86; - mobile_identity_suci.scheme_output[4] = 0x91; - test_ue = test_ue_add_by_suci(&mobile_identity_suci, 13); + test_ue = test_ue_add_by_suci(&mobile_identity_suci, "1032548691"); ogs_assert(test_ue); test_ue->e_cgi.cell_id = 0x1079baf; @@ -353,15 +343,10 @@ static void test3_func(abts_case *tc, void *data) mobile_identity_suci.routing_indicator2 = 0xf; mobile_identity_suci.routing_indicator3 = 0xf; mobile_identity_suci.routing_indicator4 = 0xf; - mobile_identity_suci.protection_scheme_id = OGS_NAS_5GS_NULL_SCHEME; + mobile_identity_suci.protection_scheme_id = OGS_PROTECTION_SCHEME_NULL; mobile_identity_suci.home_network_pki_value = 0; - mobile_identity_suci.scheme_output[0] = 0x10; - mobile_identity_suci.scheme_output[1] = 0x32; - mobile_identity_suci.scheme_output[2] = 0x54; - mobile_identity_suci.scheme_output[3] = 0x86; - mobile_identity_suci.scheme_output[4] = 0x91; - test_ue = test_ue_add_by_suci(&mobile_identity_suci, 13); + test_ue = test_ue_add_by_suci(&mobile_identity_suci, "1032548691"); ogs_assert(test_ue); test_ue->e_cgi.cell_id = 0x1079baf; diff --git a/tests/registration/abts-main.c b/tests/registration/abts-main.c index 5f9e77418..51c5f0dcb 100644 --- a/tests/registration/abts-main.c +++ b/tests/registration/abts-main.c @@ -22,6 +22,7 @@ abts_suite *test_simple(abts_suite *suite); abts_suite *test_guti(abts_suite *suite); abts_suite *test_auth(abts_suite *suite); +abts_suite *test_ecc(abts_suite *suite); abts_suite *test_idle(abts_suite *suite); abts_suite *test_dereg(abts_suite *suite); abts_suite *test_paging(abts_suite *suite); @@ -38,6 +39,7 @@ const struct testlist { {test_simple}, {test_guti}, {test_auth}, + {test_ecc}, {test_idle}, {test_dereg}, {test_paging}, @@ -59,7 +61,9 @@ static void terminate(void) test_child_terminate(); app_terminate(); + ogs_sbi_context_final(); test_5gc_final(); + ogs_app_terminate(); } @@ -69,8 +73,12 @@ static void initialize(const char *const argv[]) rv = ogs_app_initialize(NULL, NULL, argv); ogs_assert(rv == OGS_OK); + test_5gc_init(); + ogs_sbi_context_init(); + ogs_assert(ogs_sbi_context_parse_config(NULL, "nrf", "scp") == OGS_OK); + rv = app_initialize(argv); ogs_assert(rv == OGS_OK); } diff --git a/tests/registration/auth-test.c b/tests/registration/auth-test.c index 3120eaaab..9ae7a13e5 100644 --- a/tests/registration/auth-test.c +++ b/tests/registration/auth-test.c @@ -47,15 +47,10 @@ static void test1_func(abts_case *tc, void *data) mobile_identity_suci.routing_indicator2 = 0xf; mobile_identity_suci.routing_indicator3 = 0xf; mobile_identity_suci.routing_indicator4 = 0xf; - mobile_identity_suci.protection_scheme_id = OGS_NAS_5GS_NULL_SCHEME; + mobile_identity_suci.protection_scheme_id = OGS_PROTECTION_SCHEME_NULL; mobile_identity_suci.home_network_pki_value = 0; - mobile_identity_suci.scheme_output[0] = 0; - mobile_identity_suci.scheme_output[1] = 0; - mobile_identity_suci.scheme_output[2] = 0x20; - mobile_identity_suci.scheme_output[3] = 0x31; - mobile_identity_suci.scheme_output[4] = 0x90; - test_ue = test_ue_add_by_suci(&mobile_identity_suci, 13); + test_ue = test_ue_add_by_suci(&mobile_identity_suci, "0000203190"); ogs_assert(test_ue); test_ue->nr_cgi.cell_id = 0x40001; diff --git a/tests/registration/crash-test.c b/tests/registration/crash-test.c index 6e1643db7..62c73d75e 100644 --- a/tests/registration/crash-test.c +++ b/tests/registration/crash-test.c @@ -48,15 +48,10 @@ static void test1_func(abts_case *tc, void *data) mobile_identity_suci.routing_indicator2 = 0xf; mobile_identity_suci.routing_indicator3 = 0xf; mobile_identity_suci.routing_indicator4 = 0xf; - mobile_identity_suci.protection_scheme_id = OGS_NAS_5GS_NULL_SCHEME; + mobile_identity_suci.protection_scheme_id = OGS_PROTECTION_SCHEME_NULL; mobile_identity_suci.home_network_pki_value = 0; - mobile_identity_suci.scheme_output[0] = 0; - mobile_identity_suci.scheme_output[1] = 0; - mobile_identity_suci.scheme_output[2] = 0x20; - mobile_identity_suci.scheme_output[3] = 0x31; - mobile_identity_suci.scheme_output[4] = 0x90; - test_ue = test_ue_add_by_suci(&mobile_identity_suci, 13); + test_ue = test_ue_add_by_suci(&mobile_identity_suci, "0000203190"); ogs_assert(test_ue); test_ue->nr_cgi.cell_id = 0x40001; @@ -412,15 +407,10 @@ static void test2_func(abts_case *tc, void *data) mobile_identity_suci.routing_indicator2 = 0xf; mobile_identity_suci.routing_indicator3 = 0xf; mobile_identity_suci.routing_indicator4 = 0xf; - mobile_identity_suci.protection_scheme_id = OGS_NAS_5GS_NULL_SCHEME; + mobile_identity_suci.protection_scheme_id = OGS_PROTECTION_SCHEME_NULL; mobile_identity_suci.home_network_pki_value = 0; - mobile_identity_suci.scheme_output[0] = 0; - mobile_identity_suci.scheme_output[1] = 0; - mobile_identity_suci.scheme_output[2] = 0x20; - mobile_identity_suci.scheme_output[3] = 0x31; - mobile_identity_suci.scheme_output[4] = 0x90; - test_ue = test_ue_add_by_suci(&mobile_identity_suci, 13); + test_ue = test_ue_add_by_suci(&mobile_identity_suci, "0000203190"); ogs_assert(test_ue); test_ue->nr_cgi.cell_id = 0x40001; @@ -778,15 +768,10 @@ static void test3_func(abts_case *tc, void *data) mobile_identity_suci.routing_indicator2 = 0xf; mobile_identity_suci.routing_indicator3 = 0xf; mobile_identity_suci.routing_indicator4 = 0xf; - mobile_identity_suci.protection_scheme_id = OGS_NAS_5GS_NULL_SCHEME; + mobile_identity_suci.protection_scheme_id = OGS_PROTECTION_SCHEME_NULL; mobile_identity_suci.home_network_pki_value = 0; - mobile_identity_suci.scheme_output[0] = 0; - mobile_identity_suci.scheme_output[1] = 0; - mobile_identity_suci.scheme_output[2] = 0x20; - mobile_identity_suci.scheme_output[3] = 0x31; - mobile_identity_suci.scheme_output[4] = 0x90; - test_ue = test_ue_add_by_suci(&mobile_identity_suci, 13); + test_ue = test_ue_add_by_suci(&mobile_identity_suci, "0000203190"); ogs_assert(test_ue); test_ue->nr_cgi.cell_id = 0x40001; diff --git a/tests/registration/dereg-test.c b/tests/registration/dereg-test.c index ea90fbaf6..bddd07ec6 100644 --- a/tests/registration/dereg-test.c +++ b/tests/registration/dereg-test.c @@ -48,15 +48,10 @@ static void test1_func(abts_case *tc, void *data) mobile_identity_suci.routing_indicator2 = 0xf; mobile_identity_suci.routing_indicator3 = 0xf; mobile_identity_suci.routing_indicator4 = 0xf; - mobile_identity_suci.protection_scheme_id = OGS_NAS_5GS_NULL_SCHEME; + mobile_identity_suci.protection_scheme_id = OGS_PROTECTION_SCHEME_NULL; mobile_identity_suci.home_network_pki_value = 0; - mobile_identity_suci.scheme_output[0] = 0; - mobile_identity_suci.scheme_output[1] = 0; - mobile_identity_suci.scheme_output[2] = 0x20; - mobile_identity_suci.scheme_output[3] = 0x31; - mobile_identity_suci.scheme_output[4] = 0x90; - test_ue = test_ue_add_by_suci(&mobile_identity_suci, 13); + test_ue = test_ue_add_by_suci(&mobile_identity_suci, "0000203190"); ogs_assert(test_ue); test_ue->nr_cgi.cell_id = 0x40001; @@ -294,15 +289,10 @@ static void test2_func(abts_case *tc, void *data) mobile_identity_suci.routing_indicator2 = 0xf; mobile_identity_suci.routing_indicator3 = 0xf; mobile_identity_suci.routing_indicator4 = 0xf; - mobile_identity_suci.protection_scheme_id = OGS_NAS_5GS_NULL_SCHEME; + mobile_identity_suci.protection_scheme_id = OGS_PROTECTION_SCHEME_NULL; mobile_identity_suci.home_network_pki_value = 0; - mobile_identity_suci.scheme_output[0] = 0; - mobile_identity_suci.scheme_output[1] = 0; - mobile_identity_suci.scheme_output[2] = 0x20; - mobile_identity_suci.scheme_output[3] = 0x31; - mobile_identity_suci.scheme_output[4] = 0x90; - test_ue = test_ue_add_by_suci(&mobile_identity_suci, 13); + test_ue = test_ue_add_by_suci(&mobile_identity_suci, "0000203190"); ogs_assert(test_ue); test_ue->nr_cgi.cell_id = 0x40001; @@ -506,15 +496,10 @@ static void test3_func(abts_case *tc, void *data) mobile_identity_suci.routing_indicator2 = 0xf; mobile_identity_suci.routing_indicator3 = 0xf; mobile_identity_suci.routing_indicator4 = 0xf; - mobile_identity_suci.protection_scheme_id = OGS_NAS_5GS_NULL_SCHEME; + mobile_identity_suci.protection_scheme_id = OGS_PROTECTION_SCHEME_NULL; mobile_identity_suci.home_network_pki_value = 0; - mobile_identity_suci.scheme_output[0] = 0; - mobile_identity_suci.scheme_output[1] = 0; - mobile_identity_suci.scheme_output[2] = 0x20; - mobile_identity_suci.scheme_output[3] = 0x31; - mobile_identity_suci.scheme_output[4] = 0x90; - test_ue = test_ue_add_by_suci(&mobile_identity_suci, 13); + test_ue = test_ue_add_by_suci(&mobile_identity_suci, "0000203190"); ogs_assert(test_ue); test_ue->nr_cgi.cell_id = 0x40001; @@ -958,15 +943,10 @@ static void test4_func(abts_case *tc, void *data) mobile_identity_suci.routing_indicator2 = 0xf; mobile_identity_suci.routing_indicator3 = 0xf; mobile_identity_suci.routing_indicator4 = 0xf; - mobile_identity_suci.protection_scheme_id = OGS_NAS_5GS_NULL_SCHEME; + mobile_identity_suci.protection_scheme_id = OGS_PROTECTION_SCHEME_NULL; mobile_identity_suci.home_network_pki_value = 0; - mobile_identity_suci.scheme_output[0] = 0; - mobile_identity_suci.scheme_output[1] = 0; - mobile_identity_suci.scheme_output[2] = 0x20; - mobile_identity_suci.scheme_output[3] = 0x31; - mobile_identity_suci.scheme_output[4] = 0x90; - test_ue = test_ue_add_by_suci(&mobile_identity_suci, 13); + test_ue = test_ue_add_by_suci(&mobile_identity_suci, "0000203190"); ogs_assert(test_ue); test_ue->nr_cgi.cell_id = 0x40001; @@ -1257,15 +1237,10 @@ static void test5_func(abts_case *tc, void *data) mobile_identity_suci.routing_indicator2 = 0xf; mobile_identity_suci.routing_indicator3 = 0xf; mobile_identity_suci.routing_indicator4 = 0xf; - mobile_identity_suci.protection_scheme_id = OGS_NAS_5GS_NULL_SCHEME; + mobile_identity_suci.protection_scheme_id = OGS_PROTECTION_SCHEME_NULL; mobile_identity_suci.home_network_pki_value = 0; - mobile_identity_suci.scheme_output[0] = 0; - mobile_identity_suci.scheme_output[1] = 0; - mobile_identity_suci.scheme_output[2] = 0x20; - mobile_identity_suci.scheme_output[3] = 0x31; - mobile_identity_suci.scheme_output[4] = 0x90; - test_ue = test_ue_add_by_suci(&mobile_identity_suci, 13); + test_ue = test_ue_add_by_suci(&mobile_identity_suci, "0000203190"); ogs_assert(test_ue); test_ue->nr_cgi.cell_id = 0x40001; diff --git a/tests/registration/ecc-test.c b/tests/registration/ecc-test.c new file mode 100644 index 000000000..e28e3bf99 --- /dev/null +++ b/tests/registration/ecc-test.c @@ -0,0 +1,882 @@ +/* + * Copyright (C) 2019,2020 by Sukchan Lee + * + * This file is part of Open5GS. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "test-common.h" + +static void test1_func(abts_case *tc, void *data) +{ + int rv; + ogs_socknode_t *ngap; + ogs_socknode_t *gtpu; + ogs_pkbuf_t *gmmbuf; + ogs_pkbuf_t *gsmbuf; + ogs_pkbuf_t *nasbuf; + ogs_pkbuf_t *sendbuf; + ogs_pkbuf_t *recvbuf; + ogs_ngap_message_t message; + int i; + + ogs_nas_5gs_mobile_identity_suci_t mobile_identity_suci; + test_ue_t *test_ue = NULL; + test_sess_t *sess = NULL; + test_bearer_t *qos_flow = NULL; + + bson_t *doc = NULL; + + const char *scheme_output = + "b2e92f83 6055a255 837debf8 50b52899" + "7ce0201c b82adfe4 be1f587d 07d8457d" + "7ee4435e 1978dc89 7bff2412 9c"; + + /* Setup Test UE & Session Context */ + memset(&mobile_identity_suci, 0, sizeof(mobile_identity_suci)); + + mobile_identity_suci.h.supi_format = OGS_NAS_5GS_SUPI_FORMAT_IMSI; + mobile_identity_suci.h.type = OGS_NAS_5GS_MOBILE_IDENTITY_SUCI; + mobile_identity_suci.routing_indicator1 = 0; + mobile_identity_suci.routing_indicator2 = 0xf; + mobile_identity_suci.routing_indicator3 = 0xf; + mobile_identity_suci.routing_indicator4 = 0xf; + mobile_identity_suci.protection_scheme_id = + OGS_PROTECTION_SCHEME_PROFILE_A; + mobile_identity_suci.home_network_pki_value = 1; + + test_ue = test_ue_add_by_suci(&mobile_identity_suci, scheme_output); + ogs_assert(test_ue); + + test_ue->nr_cgi.cell_id = 0x40001; + + test_ue->nas.registration.tsc = 0; + test_ue->nas.registration.ksi = OGS_NAS_KSI_NO_KEY_IS_AVAILABLE; + test_ue->nas.registration.follow_on_request = 1; + test_ue->nas.registration.value = OGS_NAS_5GS_REGISTRATION_TYPE_INITIAL; + + test_ue->k_string = "465b5ce8b199b49faa5f0a2ee238a6bc"; + test_ue->opc_string = "e8ed289deba952e4283b54e88e6183ca"; + + /* gNB connects to AMF */ + ngap = testngap_client(AF_INET); + ABTS_PTR_NOTNULL(tc, ngap); + + /* gNB connects to UPF */ + gtpu = test_gtpu_server(1, AF_INET); + ABTS_PTR_NOTNULL(tc, gtpu); + + /* Send NG-Setup Reqeust */ + sendbuf = testngap_build_ng_setup_request(0x4000, 22); + ABTS_PTR_NOTNULL(tc, sendbuf); + rv = testgnb_ngap_send(ngap, sendbuf); + ABTS_INT_EQUAL(tc, OGS_OK, rv); + + /* Receive NG-Setup Response */ + recvbuf = testgnb_ngap_read(ngap); + ABTS_PTR_NOTNULL(tc, recvbuf); + testngap_recv(test_ue, recvbuf); + + /********** Insert Subscriber in Database */ + doc = test_db_new_simple(test_ue); + ABTS_PTR_NOTNULL(tc, doc); + ABTS_INT_EQUAL(tc, OGS_OK, test_db_insert_ue(test_ue, doc)); + + /* Send Registration request */ + gmmbuf = testgmm_build_registration_request(test_ue, NULL, false, false); + ABTS_PTR_NOTNULL(tc, gmmbuf); + + test_ue->registration_request_param.gmm_capability = 1; + test_ue->registration_request_param.s1_ue_network_capability = 1; + test_ue->registration_request_param.requested_nssai = 1; + test_ue->registration_request_param.last_visited_registered_tai = 1; + test_ue->registration_request_param.ue_usage_setting = 1; + nasbuf = testgmm_build_registration_request(test_ue, NULL, false, false); + ABTS_PTR_NOTNULL(tc, nasbuf); + + sendbuf = testngap_build_initial_ue_message(test_ue, gmmbuf, + NGAP_RRCEstablishmentCause_mo_Signalling, false, true); + ABTS_PTR_NOTNULL(tc, sendbuf); + rv = testgnb_ngap_send(ngap, sendbuf); + ABTS_INT_EQUAL(tc, OGS_OK, rv); + + /* Receive Authentication request */ + recvbuf = testgnb_ngap_read(ngap); + ABTS_PTR_NOTNULL(tc, recvbuf); + testngap_recv(test_ue, recvbuf); + + /* Send Authentication response */ + gmmbuf = testgmm_build_authentication_response(test_ue); + ABTS_PTR_NOTNULL(tc, gmmbuf); + sendbuf = testngap_build_uplink_nas_transport(test_ue, gmmbuf); + ABTS_PTR_NOTNULL(tc, sendbuf); + rv = testgnb_ngap_send(ngap, sendbuf); + ABTS_INT_EQUAL(tc, OGS_OK, rv); + + /* Receive Security mode command */ + recvbuf = testgnb_ngap_read(ngap); + ABTS_PTR_NOTNULL(tc, recvbuf); + testngap_recv(test_ue, recvbuf); + + /* Send Security mode complete */ + gmmbuf = testgmm_build_security_mode_complete(test_ue, nasbuf); + ABTS_PTR_NOTNULL(tc, gmmbuf); + sendbuf = testngap_build_uplink_nas_transport(test_ue, gmmbuf); + ABTS_PTR_NOTNULL(tc, sendbuf); + rv = testgnb_ngap_send(ngap, sendbuf); + ABTS_INT_EQUAL(tc, OGS_OK, rv); + + /* Receive InitialContextSetupRequest + + * Registration accept */ + recvbuf = testgnb_ngap_read(ngap); + ABTS_PTR_NOTNULL(tc, recvbuf); + testngap_recv(test_ue, recvbuf); + ABTS_INT_EQUAL(tc, + NGAP_ProcedureCode_id_InitialContextSetup, + test_ue->ngap_procedure_code); + + /* Send UERadioCapabilityInfoIndication */ + sendbuf = testngap_build_ue_radio_capability_info_indication(test_ue); + ABTS_PTR_NOTNULL(tc, sendbuf); + rv = testgnb_ngap_send(ngap, sendbuf); + ABTS_INT_EQUAL(tc, OGS_OK, rv); + + /* Send InitialContextSetupResponse */ + sendbuf = testngap_build_initial_context_setup_response(test_ue, false); + ABTS_PTR_NOTNULL(tc, sendbuf); + rv = testgnb_ngap_send(ngap, sendbuf); + ABTS_INT_EQUAL(tc, OGS_OK, rv); + + /* Send Registration complete */ + gmmbuf = testgmm_build_registration_complete(test_ue); + ABTS_PTR_NOTNULL(tc, gmmbuf); + sendbuf = testngap_build_uplink_nas_transport(test_ue, gmmbuf); + ABTS_PTR_NOTNULL(tc, sendbuf); + rv = testgnb_ngap_send(ngap, sendbuf); + ABTS_INT_EQUAL(tc, OGS_OK, rv); + + /* Receive Configuration update command */ + recvbuf = testgnb_ngap_read(ngap); + ABTS_PTR_NOTNULL(tc, recvbuf); + testngap_recv(test_ue, recvbuf); + + /* Send De-registration request */ + gmmbuf = testgmm_build_de_registration_request(test_ue, 1, true, false); + ABTS_PTR_NOTNULL(tc, gmmbuf); + sendbuf = testngap_build_initial_ue_message(test_ue, gmmbuf, + NGAP_RRCEstablishmentCause_mo_Signalling, false, false); + ABTS_PTR_NOTNULL(tc, sendbuf); + rv = testgnb_ngap_send(ngap, sendbuf); + ABTS_INT_EQUAL(tc, OGS_OK, rv); + + /* Receive OLD UEContextReleaseCommand */ + recvbuf = testgnb_ngap_read(ngap); + ABTS_PTR_NOTNULL(tc, recvbuf); + testngap_recv(test_ue, recvbuf); + ABTS_INT_EQUAL(tc, + NGAP_ProcedureCode_id_UEContextRelease, + test_ue->ngap_procedure_code); + + /* Send OLD UEContextReleaseComplete */ + sendbuf = testngap_build_ue_context_release_complete(test_ue); + ABTS_PTR_NOTNULL(tc, sendbuf); + rv = testgnb_ngap_send(ngap, sendbuf); + ABTS_INT_EQUAL(tc, OGS_OK, rv); + + /* Receive UEContextReleaseCommand */ + recvbuf = testgnb_ngap_read(ngap); + ABTS_PTR_NOTNULL(tc, recvbuf); + testngap_recv(test_ue, recvbuf); + ABTS_INT_EQUAL(tc, + NGAP_ProcedureCode_id_UEContextRelease, + test_ue->ngap_procedure_code); + + /* Send UEContextReleaseComplete */ + sendbuf = testngap_build_ue_context_release_complete(test_ue); + ABTS_PTR_NOTNULL(tc, sendbuf); + rv = testgnb_ngap_send(ngap, sendbuf); + ABTS_INT_EQUAL(tc, OGS_OK, rv); + + ogs_msleep(300); + + /********** Remove Subscriber in Database */ + ABTS_INT_EQUAL(tc, OGS_OK, test_db_remove_ue(test_ue)); + + /* gNB disonncect from UPF */ + testgnb_gtpu_close(gtpu); + + /* gNB disonncect from AMF */ + testgnb_ngap_close(ngap); + + /* Clear Test UE Context */ + test_ue_remove(test_ue); +} + +static void test2_func(abts_case *tc, void *data) +{ + int rv; + ogs_socknode_t *ngap; + ogs_socknode_t *gtpu; + ogs_pkbuf_t *gmmbuf; + ogs_pkbuf_t *gsmbuf; + ogs_pkbuf_t *nasbuf; + ogs_pkbuf_t *sendbuf; + ogs_pkbuf_t *recvbuf; + ogs_ngap_message_t message; + int i; + + ogs_nas_5gs_mobile_identity_suci_t mobile_identity_suci; + test_ue_t *test_ue = NULL; + test_sess_t *sess = NULL; + test_bearer_t *qos_flow = NULL; + + bson_t *doc = NULL; + + const char *scheme_output = + "b2e92f83 6055a255 837debf8 50b52899" + "7ce0201c b82adfe4 be1f587d 07d8457d" + "7ee4435e 1978dc89 7bff2412 9c"; + + /* Setup Test UE & Session Context */ + memset(&mobile_identity_suci, 0, sizeof(mobile_identity_suci)); + + mobile_identity_suci.h.supi_format = OGS_NAS_5GS_SUPI_FORMAT_IMSI; + mobile_identity_suci.h.type = OGS_NAS_5GS_MOBILE_IDENTITY_SUCI; + mobile_identity_suci.routing_indicator1 = 0; + mobile_identity_suci.routing_indicator2 = 0xf; + mobile_identity_suci.routing_indicator3 = 0xf; + mobile_identity_suci.routing_indicator4 = 0xf; + mobile_identity_suci.protection_scheme_id = + OGS_PROTECTION_SCHEME_PROFILE_A; + mobile_identity_suci.home_network_pki_value = 1; + + test_ue = test_ue_add_by_suci(&mobile_identity_suci, scheme_output); + ogs_assert(test_ue); + + test_ue->nr_cgi.cell_id = 0x40001; + + test_ue->nas.registration.tsc = 0; + test_ue->nas.registration.ksi = OGS_NAS_KSI_NO_KEY_IS_AVAILABLE; + test_ue->nas.registration.follow_on_request = 1; + test_ue->nas.registration.value = OGS_NAS_5GS_REGISTRATION_TYPE_INITIAL; + + test_ue->k_string = "465b5ce8b199b49faa5f0a2ee238a6bc"; + test_ue->opc_string = "e8ed289deba952e4283b54e88e6183ca"; + + /* gNB connects to AMF */ + ngap = testngap_client(AF_INET); + ABTS_PTR_NOTNULL(tc, ngap); + + /* gNB connects to UPF */ + gtpu = test_gtpu_server(1, AF_INET); + ABTS_PTR_NOTNULL(tc, gtpu); + + /* Send NG-Setup Reqeust */ + sendbuf = testngap_build_ng_setup_request(0x4000, 22); + ABTS_PTR_NOTNULL(tc, sendbuf); + rv = testgnb_ngap_send(ngap, sendbuf); + ABTS_INT_EQUAL(tc, OGS_OK, rv); + + /* Receive NG-Setup Response */ + recvbuf = testgnb_ngap_read(ngap); + ABTS_PTR_NOTNULL(tc, recvbuf); + testngap_recv(test_ue, recvbuf); + + /********** Insert Subscriber in Database */ + doc = test_db_new_simple(test_ue); + ABTS_PTR_NOTNULL(tc, doc); + ABTS_INT_EQUAL(tc, OGS_OK, test_db_insert_ue(test_ue, doc)); + + /* Send Registration request */ + test_ue->registration_request_param.guti = 1; + gmmbuf = testgmm_build_registration_request(test_ue, NULL, false, false); + ABTS_PTR_NOTNULL(tc, gmmbuf); + + test_ue->registration_request_param.gmm_capability = 1; + test_ue->registration_request_param.s1_ue_network_capability = 1; + test_ue->registration_request_param.requested_nssai = 1; + test_ue->registration_request_param.last_visited_registered_tai = 1; + test_ue->registration_request_param.ue_usage_setting = 1; + nasbuf = testgmm_build_registration_request(test_ue, NULL, false, false); + ABTS_PTR_NOTNULL(tc, nasbuf); + + sendbuf = testngap_build_initial_ue_message(test_ue, gmmbuf, + NGAP_RRCEstablishmentCause_mo_Signalling, false, true); + ABTS_PTR_NOTNULL(tc, sendbuf); + rv = testgnb_ngap_send(ngap, sendbuf); + ABTS_INT_EQUAL(tc, OGS_OK, rv); + + /* Receive Identity request */ + recvbuf = testgnb_ngap_read(ngap); + ABTS_PTR_NOTNULL(tc, recvbuf); + testngap_recv(test_ue, recvbuf); + + /* Send Identity response */ + gmmbuf = testgmm_build_identity_response(test_ue); + ABTS_PTR_NOTNULL(tc, gmmbuf); + sendbuf = testngap_build_uplink_nas_transport(test_ue, gmmbuf); + ABTS_PTR_NOTNULL(tc, sendbuf); + rv = testgnb_ngap_send(ngap, sendbuf); + ABTS_INT_EQUAL(tc, OGS_OK, rv); + + /* Receive Authentication request */ + recvbuf = testgnb_ngap_read(ngap); + ABTS_PTR_NOTNULL(tc, recvbuf); + testngap_recv(test_ue, recvbuf); + + /* Send Authentication response */ + gmmbuf = testgmm_build_authentication_response(test_ue); + ABTS_PTR_NOTNULL(tc, gmmbuf); + sendbuf = testngap_build_uplink_nas_transport(test_ue, gmmbuf); + ABTS_PTR_NOTNULL(tc, sendbuf); + rv = testgnb_ngap_send(ngap, sendbuf); + ABTS_INT_EQUAL(tc, OGS_OK, rv); + + /* Receive Security mode command */ + recvbuf = testgnb_ngap_read(ngap); + ABTS_PTR_NOTNULL(tc, recvbuf); + testngap_recv(test_ue, recvbuf); + + /* Send Security mode complete */ + gmmbuf = testgmm_build_security_mode_complete(test_ue, nasbuf); + ABTS_PTR_NOTNULL(tc, gmmbuf); + sendbuf = testngap_build_uplink_nas_transport(test_ue, gmmbuf); + ABTS_PTR_NOTNULL(tc, sendbuf); + rv = testgnb_ngap_send(ngap, sendbuf); + ABTS_INT_EQUAL(tc, OGS_OK, rv); + + /* Receive InitialContextSetupRequest + + * Registration accept */ + recvbuf = testgnb_ngap_read(ngap); + ABTS_PTR_NOTNULL(tc, recvbuf); + testngap_recv(test_ue, recvbuf); + ABTS_INT_EQUAL(tc, + NGAP_ProcedureCode_id_InitialContextSetup, + test_ue->ngap_procedure_code); + + /* Send UERadioCapabilityInfoIndication */ + sendbuf = testngap_build_ue_radio_capability_info_indication(test_ue); + ABTS_PTR_NOTNULL(tc, sendbuf); + rv = testgnb_ngap_send(ngap, sendbuf); + ABTS_INT_EQUAL(tc, OGS_OK, rv); + + /* Send InitialContextSetupResponse */ + sendbuf = testngap_build_initial_context_setup_response(test_ue, false); + ABTS_PTR_NOTNULL(tc, sendbuf); + rv = testgnb_ngap_send(ngap, sendbuf); + ABTS_INT_EQUAL(tc, OGS_OK, rv); + + /* Send Registration complete */ + gmmbuf = testgmm_build_registration_complete(test_ue); + ABTS_PTR_NOTNULL(tc, gmmbuf); + sendbuf = testngap_build_uplink_nas_transport(test_ue, gmmbuf); + ABTS_PTR_NOTNULL(tc, sendbuf); + rv = testgnb_ngap_send(ngap, sendbuf); + ABTS_INT_EQUAL(tc, OGS_OK, rv); + + /* Receive Configuration update command */ + recvbuf = testgnb_ngap_read(ngap); + ABTS_PTR_NOTNULL(tc, recvbuf); + testngap_recv(test_ue, recvbuf); + + /* Send De-registration request */ + gmmbuf = testgmm_build_de_registration_request(test_ue, 1, true, false); + ABTS_PTR_NOTNULL(tc, gmmbuf); + sendbuf = testngap_build_initial_ue_message(test_ue, gmmbuf, + NGAP_RRCEstablishmentCause_mo_Signalling, false, false); + ABTS_PTR_NOTNULL(tc, sendbuf); + rv = testgnb_ngap_send(ngap, sendbuf); + ABTS_INT_EQUAL(tc, OGS_OK, rv); + + /* Receive OLD UEContextReleaseCommand */ + recvbuf = testgnb_ngap_read(ngap); + ABTS_PTR_NOTNULL(tc, recvbuf); + testngap_recv(test_ue, recvbuf); + ABTS_INT_EQUAL(tc, + NGAP_ProcedureCode_id_UEContextRelease, + test_ue->ngap_procedure_code); + + /* Send OLD UEContextReleaseComplete */ + sendbuf = testngap_build_ue_context_release_complete(test_ue); + ABTS_PTR_NOTNULL(tc, sendbuf); + rv = testgnb_ngap_send(ngap, sendbuf); + ABTS_INT_EQUAL(tc, OGS_OK, rv); + + /* Receive UEContextReleaseCommand */ + recvbuf = testgnb_ngap_read(ngap); + ABTS_PTR_NOTNULL(tc, recvbuf); + testngap_recv(test_ue, recvbuf); + ABTS_INT_EQUAL(tc, + NGAP_ProcedureCode_id_UEContextRelease, + test_ue->ngap_procedure_code); + + /* Send UEContextReleaseComplete */ + sendbuf = testngap_build_ue_context_release_complete(test_ue); + ABTS_PTR_NOTNULL(tc, sendbuf); + rv = testgnb_ngap_send(ngap, sendbuf); + ABTS_INT_EQUAL(tc, OGS_OK, rv); + + ogs_msleep(300); + + /********** Remove Subscriber in Database */ + ABTS_INT_EQUAL(tc, OGS_OK, test_db_remove_ue(test_ue)); + + /* gNB disonncect from UPF */ + testgnb_gtpu_close(gtpu); + + /* gNB disonncect from AMF */ + testgnb_ngap_close(ngap); + + /* Clear Test UE Context */ + test_ue_remove(test_ue); +} + +static void test3_func(abts_case *tc, void *data) +{ + int rv; + ogs_socknode_t *ngap; + ogs_socknode_t *gtpu; + ogs_pkbuf_t *gmmbuf; + ogs_pkbuf_t *gsmbuf; + ogs_pkbuf_t *nasbuf; + ogs_pkbuf_t *sendbuf; + ogs_pkbuf_t *recvbuf; + ogs_ngap_message_t message; + int i; + + ogs_nas_5gs_mobile_identity_suci_t mobile_identity_suci; + test_ue_t *test_ue = NULL; + test_sess_t *sess = NULL; + test_bearer_t *qos_flow = NULL; + + bson_t *doc = NULL; + + const char *scheme_output = + "039AAB83 76597021 E855679A 9778EA0B" + "67396E68 C66DF32C 0F41E9AC CA2DA9B9" "D1" + "cdd22e34 96" + "5500e9242e65f58c"; + + /* Setup Test UE & Session Context */ + memset(&mobile_identity_suci, 0, sizeof(mobile_identity_suci)); + + mobile_identity_suci.h.supi_format = OGS_NAS_5GS_SUPI_FORMAT_IMSI; + mobile_identity_suci.h.type = OGS_NAS_5GS_MOBILE_IDENTITY_SUCI; + mobile_identity_suci.routing_indicator1 = 0; + mobile_identity_suci.routing_indicator2 = 0xf; + mobile_identity_suci.routing_indicator3 = 0xf; + mobile_identity_suci.routing_indicator4 = 0xf; + mobile_identity_suci.protection_scheme_id = + OGS_PROTECTION_SCHEME_PROFILE_B; + mobile_identity_suci.home_network_pki_value = 2; + + test_ue = test_ue_add_by_suci(&mobile_identity_suci, scheme_output); + ogs_assert(test_ue); + + test_ue->nr_cgi.cell_id = 0x40001; + + test_ue->nas.registration.tsc = 0; + test_ue->nas.registration.ksi = OGS_NAS_KSI_NO_KEY_IS_AVAILABLE; + test_ue->nas.registration.follow_on_request = 1; + test_ue->nas.registration.value = OGS_NAS_5GS_REGISTRATION_TYPE_INITIAL; + + test_ue->k_string = "465b5ce8b199b49faa5f0a2ee238a6bc"; + test_ue->opc_string = "e8ed289deba952e4283b54e88e6183ca"; + + /* gNB connects to AMF */ + ngap = testngap_client(AF_INET); + ABTS_PTR_NOTNULL(tc, ngap); + + /* gNB connects to UPF */ + gtpu = test_gtpu_server(1, AF_INET); + ABTS_PTR_NOTNULL(tc, gtpu); + + /* Send NG-Setup Reqeust */ + sendbuf = testngap_build_ng_setup_request(0x4000, 22); + ABTS_PTR_NOTNULL(tc, sendbuf); + rv = testgnb_ngap_send(ngap, sendbuf); + ABTS_INT_EQUAL(tc, OGS_OK, rv); + + /* Receive NG-Setup Response */ + recvbuf = testgnb_ngap_read(ngap); + ABTS_PTR_NOTNULL(tc, recvbuf); + testngap_recv(test_ue, recvbuf); + + /********** Insert Subscriber in Database */ + doc = test_db_new_simple(test_ue); + ABTS_PTR_NOTNULL(tc, doc); + ABTS_INT_EQUAL(tc, OGS_OK, test_db_insert_ue(test_ue, doc)); + + /* Send Registration request */ + gmmbuf = testgmm_build_registration_request(test_ue, NULL, false, false); + ABTS_PTR_NOTNULL(tc, gmmbuf); + + test_ue->registration_request_param.gmm_capability = 1; + test_ue->registration_request_param.s1_ue_network_capability = 1; + test_ue->registration_request_param.requested_nssai = 1; + test_ue->registration_request_param.last_visited_registered_tai = 1; + test_ue->registration_request_param.ue_usage_setting = 1; + nasbuf = testgmm_build_registration_request(test_ue, NULL, false, false); + ABTS_PTR_NOTNULL(tc, nasbuf); + + sendbuf = testngap_build_initial_ue_message(test_ue, gmmbuf, + NGAP_RRCEstablishmentCause_mo_Signalling, false, true); + ABTS_PTR_NOTNULL(tc, sendbuf); + rv = testgnb_ngap_send(ngap, sendbuf); + ABTS_INT_EQUAL(tc, OGS_OK, rv); + + /* Receive Authentication request */ + recvbuf = testgnb_ngap_read(ngap); + ABTS_PTR_NOTNULL(tc, recvbuf); + testngap_recv(test_ue, recvbuf); + + /* Send Authentication response */ + gmmbuf = testgmm_build_authentication_response(test_ue); + ABTS_PTR_NOTNULL(tc, gmmbuf); + sendbuf = testngap_build_uplink_nas_transport(test_ue, gmmbuf); + ABTS_PTR_NOTNULL(tc, sendbuf); + rv = testgnb_ngap_send(ngap, sendbuf); + ABTS_INT_EQUAL(tc, OGS_OK, rv); + + /* Receive Security mode command */ + recvbuf = testgnb_ngap_read(ngap); + ABTS_PTR_NOTNULL(tc, recvbuf); + testngap_recv(test_ue, recvbuf); + + /* Send Security mode complete */ + gmmbuf = testgmm_build_security_mode_complete(test_ue, nasbuf); + ABTS_PTR_NOTNULL(tc, gmmbuf); + sendbuf = testngap_build_uplink_nas_transport(test_ue, gmmbuf); + ABTS_PTR_NOTNULL(tc, sendbuf); + rv = testgnb_ngap_send(ngap, sendbuf); + ABTS_INT_EQUAL(tc, OGS_OK, rv); + + /* Receive InitialContextSetupRequest + + * Registration accept */ + recvbuf = testgnb_ngap_read(ngap); + ABTS_PTR_NOTNULL(tc, recvbuf); + testngap_recv(test_ue, recvbuf); + ABTS_INT_EQUAL(tc, + NGAP_ProcedureCode_id_InitialContextSetup, + test_ue->ngap_procedure_code); + + /* Send UERadioCapabilityInfoIndication */ + sendbuf = testngap_build_ue_radio_capability_info_indication(test_ue); + ABTS_PTR_NOTNULL(tc, sendbuf); + rv = testgnb_ngap_send(ngap, sendbuf); + ABTS_INT_EQUAL(tc, OGS_OK, rv); + + /* Send InitialContextSetupResponse */ + sendbuf = testngap_build_initial_context_setup_response(test_ue, false); + ABTS_PTR_NOTNULL(tc, sendbuf); + rv = testgnb_ngap_send(ngap, sendbuf); + ABTS_INT_EQUAL(tc, OGS_OK, rv); + + /* Send Registration complete */ + gmmbuf = testgmm_build_registration_complete(test_ue); + ABTS_PTR_NOTNULL(tc, gmmbuf); + sendbuf = testngap_build_uplink_nas_transport(test_ue, gmmbuf); + ABTS_PTR_NOTNULL(tc, sendbuf); + rv = testgnb_ngap_send(ngap, sendbuf); + ABTS_INT_EQUAL(tc, OGS_OK, rv); + + /* Receive Configuration update command */ + recvbuf = testgnb_ngap_read(ngap); + ABTS_PTR_NOTNULL(tc, recvbuf); + testngap_recv(test_ue, recvbuf); + + /* Send De-registration request */ + gmmbuf = testgmm_build_de_registration_request(test_ue, 1, true, false); + ABTS_PTR_NOTNULL(tc, gmmbuf); + sendbuf = testngap_build_initial_ue_message(test_ue, gmmbuf, + NGAP_RRCEstablishmentCause_mo_Signalling, false, false); + ABTS_PTR_NOTNULL(tc, sendbuf); + rv = testgnb_ngap_send(ngap, sendbuf); + ABTS_INT_EQUAL(tc, OGS_OK, rv); + + /* Receive OLD UEContextReleaseCommand */ + recvbuf = testgnb_ngap_read(ngap); + ABTS_PTR_NOTNULL(tc, recvbuf); + testngap_recv(test_ue, recvbuf); + ABTS_INT_EQUAL(tc, + NGAP_ProcedureCode_id_UEContextRelease, + test_ue->ngap_procedure_code); + + /* Send OLD UEContextReleaseComplete */ + sendbuf = testngap_build_ue_context_release_complete(test_ue); + ABTS_PTR_NOTNULL(tc, sendbuf); + rv = testgnb_ngap_send(ngap, sendbuf); + ABTS_INT_EQUAL(tc, OGS_OK, rv); + + /* Receive UEContextReleaseCommand */ + recvbuf = testgnb_ngap_read(ngap); + ABTS_PTR_NOTNULL(tc, recvbuf); + testngap_recv(test_ue, recvbuf); + ABTS_INT_EQUAL(tc, + NGAP_ProcedureCode_id_UEContextRelease, + test_ue->ngap_procedure_code); + + /* Send UEContextReleaseComplete */ + sendbuf = testngap_build_ue_context_release_complete(test_ue); + ABTS_PTR_NOTNULL(tc, sendbuf); + rv = testgnb_ngap_send(ngap, sendbuf); + ABTS_INT_EQUAL(tc, OGS_OK, rv); + + ogs_msleep(300); + + /********** Remove Subscriber in Database */ + ABTS_INT_EQUAL(tc, OGS_OK, test_db_remove_ue(test_ue)); + + /* gNB disonncect from UPF */ + testgnb_gtpu_close(gtpu); + + /* gNB disonncect from AMF */ + testgnb_ngap_close(ngap); + + /* Clear Test UE Context */ + test_ue_remove(test_ue); +} + +static void test4_func(abts_case *tc, void *data) +{ + int rv; + ogs_socknode_t *ngap; + ogs_socknode_t *gtpu; + ogs_pkbuf_t *gmmbuf; + ogs_pkbuf_t *gsmbuf; + ogs_pkbuf_t *nasbuf; + ogs_pkbuf_t *sendbuf; + ogs_pkbuf_t *recvbuf; + ogs_ngap_message_t message; + int i; + + ogs_nas_5gs_mobile_identity_suci_t mobile_identity_suci; + test_ue_t *test_ue = NULL; + test_sess_t *sess = NULL; + test_bearer_t *qos_flow = NULL; + + bson_t *doc = NULL; + + const char *scheme_output = + "039AAB83 76597021 E855679A 9778EA0B" + "67396E68 C66DF32C 0F41E9AC CA2DA9B9" "D1" + "cdd22e34 96" + "5500e9242e65f58c"; + + /* Setup Test UE & Session Context */ + memset(&mobile_identity_suci, 0, sizeof(mobile_identity_suci)); + + mobile_identity_suci.h.supi_format = OGS_NAS_5GS_SUPI_FORMAT_IMSI; + mobile_identity_suci.h.type = OGS_NAS_5GS_MOBILE_IDENTITY_SUCI; + mobile_identity_suci.routing_indicator1 = 0; + mobile_identity_suci.routing_indicator2 = 0xf; + mobile_identity_suci.routing_indicator3 = 0xf; + mobile_identity_suci.routing_indicator4 = 0xf; + mobile_identity_suci.protection_scheme_id = + OGS_PROTECTION_SCHEME_PROFILE_B; + mobile_identity_suci.home_network_pki_value = 2; + + test_ue = test_ue_add_by_suci(&mobile_identity_suci, scheme_output); + ogs_assert(test_ue); + + test_ue->nr_cgi.cell_id = 0x40001; + + test_ue->nas.registration.tsc = 0; + test_ue->nas.registration.ksi = OGS_NAS_KSI_NO_KEY_IS_AVAILABLE; + test_ue->nas.registration.follow_on_request = 1; + test_ue->nas.registration.value = OGS_NAS_5GS_REGISTRATION_TYPE_INITIAL; + + test_ue->k_string = "465b5ce8b199b49faa5f0a2ee238a6bc"; + test_ue->opc_string = "e8ed289deba952e4283b54e88e6183ca"; + + /* gNB connects to AMF */ + ngap = testngap_client(AF_INET); + ABTS_PTR_NOTNULL(tc, ngap); + + /* gNB connects to UPF */ + gtpu = test_gtpu_server(1, AF_INET); + ABTS_PTR_NOTNULL(tc, gtpu); + + /* Send NG-Setup Reqeust */ + sendbuf = testngap_build_ng_setup_request(0x4000, 22); + ABTS_PTR_NOTNULL(tc, sendbuf); + rv = testgnb_ngap_send(ngap, sendbuf); + ABTS_INT_EQUAL(tc, OGS_OK, rv); + + /* Receive NG-Setup Response */ + recvbuf = testgnb_ngap_read(ngap); + ABTS_PTR_NOTNULL(tc, recvbuf); + testngap_recv(test_ue, recvbuf); + + /********** Insert Subscriber in Database */ + doc = test_db_new_simple(test_ue); + ABTS_PTR_NOTNULL(tc, doc); + ABTS_INT_EQUAL(tc, OGS_OK, test_db_insert_ue(test_ue, doc)); + + /* Send Registration request */ + test_ue->registration_request_param.guti = 1; + gmmbuf = testgmm_build_registration_request(test_ue, NULL, false, false); + ABTS_PTR_NOTNULL(tc, gmmbuf); + + test_ue->registration_request_param.gmm_capability = 1; + test_ue->registration_request_param.s1_ue_network_capability = 1; + test_ue->registration_request_param.requested_nssai = 1; + test_ue->registration_request_param.last_visited_registered_tai = 1; + test_ue->registration_request_param.ue_usage_setting = 1; + nasbuf = testgmm_build_registration_request(test_ue, NULL, false, false); + ABTS_PTR_NOTNULL(tc, nasbuf); + + sendbuf = testngap_build_initial_ue_message(test_ue, gmmbuf, + NGAP_RRCEstablishmentCause_mo_Signalling, false, true); + ABTS_PTR_NOTNULL(tc, sendbuf); + rv = testgnb_ngap_send(ngap, sendbuf); + ABTS_INT_EQUAL(tc, OGS_OK, rv); + + /* Receive Identity request */ + recvbuf = testgnb_ngap_read(ngap); + ABTS_PTR_NOTNULL(tc, recvbuf); + testngap_recv(test_ue, recvbuf); + + /* Send Identity response */ + gmmbuf = testgmm_build_identity_response(test_ue); + ABTS_PTR_NOTNULL(tc, gmmbuf); + sendbuf = testngap_build_uplink_nas_transport(test_ue, gmmbuf); + ABTS_PTR_NOTNULL(tc, sendbuf); + rv = testgnb_ngap_send(ngap, sendbuf); + ABTS_INT_EQUAL(tc, OGS_OK, rv); + + /* Receive Authentication request */ + recvbuf = testgnb_ngap_read(ngap); + ABTS_PTR_NOTNULL(tc, recvbuf); + testngap_recv(test_ue, recvbuf); + + /* Send Authentication response */ + gmmbuf = testgmm_build_authentication_response(test_ue); + ABTS_PTR_NOTNULL(tc, gmmbuf); + sendbuf = testngap_build_uplink_nas_transport(test_ue, gmmbuf); + ABTS_PTR_NOTNULL(tc, sendbuf); + rv = testgnb_ngap_send(ngap, sendbuf); + ABTS_INT_EQUAL(tc, OGS_OK, rv); + + /* Receive Security mode command */ + recvbuf = testgnb_ngap_read(ngap); + ABTS_PTR_NOTNULL(tc, recvbuf); + testngap_recv(test_ue, recvbuf); + + /* Send Security mode complete */ + gmmbuf = testgmm_build_security_mode_complete(test_ue, nasbuf); + ABTS_PTR_NOTNULL(tc, gmmbuf); + sendbuf = testngap_build_uplink_nas_transport(test_ue, gmmbuf); + ABTS_PTR_NOTNULL(tc, sendbuf); + rv = testgnb_ngap_send(ngap, sendbuf); + ABTS_INT_EQUAL(tc, OGS_OK, rv); + + /* Receive InitialContextSetupRequest + + * Registration accept */ + recvbuf = testgnb_ngap_read(ngap); + ABTS_PTR_NOTNULL(tc, recvbuf); + testngap_recv(test_ue, recvbuf); + ABTS_INT_EQUAL(tc, + NGAP_ProcedureCode_id_InitialContextSetup, + test_ue->ngap_procedure_code); + + /* Send UERadioCapabilityInfoIndication */ + sendbuf = testngap_build_ue_radio_capability_info_indication(test_ue); + ABTS_PTR_NOTNULL(tc, sendbuf); + rv = testgnb_ngap_send(ngap, sendbuf); + ABTS_INT_EQUAL(tc, OGS_OK, rv); + + /* Send InitialContextSetupResponse */ + sendbuf = testngap_build_initial_context_setup_response(test_ue, false); + ABTS_PTR_NOTNULL(tc, sendbuf); + rv = testgnb_ngap_send(ngap, sendbuf); + ABTS_INT_EQUAL(tc, OGS_OK, rv); + + /* Send Registration complete */ + gmmbuf = testgmm_build_registration_complete(test_ue); + ABTS_PTR_NOTNULL(tc, gmmbuf); + sendbuf = testngap_build_uplink_nas_transport(test_ue, gmmbuf); + ABTS_PTR_NOTNULL(tc, sendbuf); + rv = testgnb_ngap_send(ngap, sendbuf); + ABTS_INT_EQUAL(tc, OGS_OK, rv); + + /* Receive Configuration update command */ + recvbuf = testgnb_ngap_read(ngap); + ABTS_PTR_NOTNULL(tc, recvbuf); + testngap_recv(test_ue, recvbuf); + + /* Send De-registration request */ + gmmbuf = testgmm_build_de_registration_request(test_ue, 1, true, false); + ABTS_PTR_NOTNULL(tc, gmmbuf); + sendbuf = testngap_build_initial_ue_message(test_ue, gmmbuf, + NGAP_RRCEstablishmentCause_mo_Signalling, false, false); + ABTS_PTR_NOTNULL(tc, sendbuf); + rv = testgnb_ngap_send(ngap, sendbuf); + ABTS_INT_EQUAL(tc, OGS_OK, rv); + + /* Receive OLD UEContextReleaseCommand */ + recvbuf = testgnb_ngap_read(ngap); + ABTS_PTR_NOTNULL(tc, recvbuf); + testngap_recv(test_ue, recvbuf); + ABTS_INT_EQUAL(tc, + NGAP_ProcedureCode_id_UEContextRelease, + test_ue->ngap_procedure_code); + + /* Send OLD UEContextReleaseComplete */ + sendbuf = testngap_build_ue_context_release_complete(test_ue); + ABTS_PTR_NOTNULL(tc, sendbuf); + rv = testgnb_ngap_send(ngap, sendbuf); + ABTS_INT_EQUAL(tc, OGS_OK, rv); + + /* Receive UEContextReleaseCommand */ + recvbuf = testgnb_ngap_read(ngap); + ABTS_PTR_NOTNULL(tc, recvbuf); + testngap_recv(test_ue, recvbuf); + ABTS_INT_EQUAL(tc, + NGAP_ProcedureCode_id_UEContextRelease, + test_ue->ngap_procedure_code); + + /* Send UEContextReleaseComplete */ + sendbuf = testngap_build_ue_context_release_complete(test_ue); + ABTS_PTR_NOTNULL(tc, sendbuf); + rv = testgnb_ngap_send(ngap, sendbuf); + ABTS_INT_EQUAL(tc, OGS_OK, rv); + + ogs_msleep(300); + + /********** Remove Subscriber in Database */ + ABTS_INT_EQUAL(tc, OGS_OK, test_db_remove_ue(test_ue)); + + /* gNB disonncect from UPF */ + testgnb_gtpu_close(gtpu); + + /* gNB disonncect from AMF */ + testgnb_ngap_close(ngap); + + /* Clear Test UE Context */ + test_ue_remove(test_ue); +} + +abts_suite *test_ecc(abts_suite *suite) +{ + suite = ADD_SUITE(suite) + + abts_run_test(suite, test1_func, NULL); + abts_run_test(suite, test2_func, NULL); + abts_run_test(suite, test3_func, NULL); + abts_run_test(suite, test4_func, NULL); + + return suite; +} diff --git a/tests/registration/gmm-status-test.c b/tests/registration/gmm-status-test.c index b2e2268b0..86abfbd96 100644 --- a/tests/registration/gmm-status-test.c +++ b/tests/registration/gmm-status-test.c @@ -48,15 +48,10 @@ static void test1_func(abts_case *tc, void *data) mobile_identity_suci.routing_indicator2 = 0xf; mobile_identity_suci.routing_indicator3 = 0xf; mobile_identity_suci.routing_indicator4 = 0xf; - mobile_identity_suci.protection_scheme_id = OGS_NAS_5GS_NULL_SCHEME; + mobile_identity_suci.protection_scheme_id = OGS_PROTECTION_SCHEME_NULL; mobile_identity_suci.home_network_pki_value = 0; - mobile_identity_suci.scheme_output[0] = 0; - mobile_identity_suci.scheme_output[1] = 0; - mobile_identity_suci.scheme_output[2] = 0x20; - mobile_identity_suci.scheme_output[3] = 0x31; - mobile_identity_suci.scheme_output[4] = 0x90; - test_ue = test_ue_add_by_suci(&mobile_identity_suci, 13); + test_ue = test_ue_add_by_suci(&mobile_identity_suci, "0000203190"); ogs_assert(test_ue); test_ue->nr_cgi.cell_id = 0x40001; diff --git a/tests/registration/guti-test.c b/tests/registration/guti-test.c index ed1799a48..d6be10a17 100644 --- a/tests/registration/guti-test.c +++ b/tests/registration/guti-test.c @@ -48,15 +48,10 @@ static void test1_func(abts_case *tc, void *data) mobile_identity_suci.routing_indicator2 = 0xf; mobile_identity_suci.routing_indicator3 = 0xf; mobile_identity_suci.routing_indicator4 = 0xf; - mobile_identity_suci.protection_scheme_id = OGS_NAS_5GS_NULL_SCHEME; + mobile_identity_suci.protection_scheme_id = OGS_PROTECTION_SCHEME_NULL; mobile_identity_suci.home_network_pki_value = 0; - mobile_identity_suci.scheme_output[0] = 0; - mobile_identity_suci.scheme_output[1] = 0; - mobile_identity_suci.scheme_output[2] = 0x20; - mobile_identity_suci.scheme_output[3] = 0x31; - mobile_identity_suci.scheme_output[4] = 0x90; - test_ue = test_ue_add_by_suci(&mobile_identity_suci, 13); + test_ue = test_ue_add_by_suci(&mobile_identity_suci, "0000203190"); ogs_assert(test_ue); test_ue->nr_cgi.cell_id = 0x40001; @@ -528,15 +523,10 @@ static void test2_func(abts_case *tc, void *data) mobile_identity_suci.routing_indicator2 = 0xf; mobile_identity_suci.routing_indicator3 = 0xf; mobile_identity_suci.routing_indicator4 = 0xf; - mobile_identity_suci.protection_scheme_id = OGS_NAS_5GS_NULL_SCHEME; + mobile_identity_suci.protection_scheme_id = OGS_PROTECTION_SCHEME_NULL; mobile_identity_suci.home_network_pki_value = 0; - mobile_identity_suci.scheme_output[0] = 0; - mobile_identity_suci.scheme_output[1] = 0; - mobile_identity_suci.scheme_output[2] = 0x20; - mobile_identity_suci.scheme_output[3] = 0x31; - mobile_identity_suci.scheme_output[4] = 0x90; - test_ue = test_ue_add_by_suci(&mobile_identity_suci, 13); + test_ue = test_ue_add_by_suci(&mobile_identity_suci, "0000203190"); ogs_assert(test_ue); test_ue->nr_cgi.cell_id = 0x40001; @@ -990,15 +980,10 @@ static void test3_func(abts_case *tc, void *data) mobile_identity_suci.routing_indicator2 = 0xf; mobile_identity_suci.routing_indicator3 = 0xf; mobile_identity_suci.routing_indicator4 = 0xf; - mobile_identity_suci.protection_scheme_id = OGS_NAS_5GS_NULL_SCHEME; + mobile_identity_suci.protection_scheme_id = OGS_PROTECTION_SCHEME_NULL; mobile_identity_suci.home_network_pki_value = 0; - mobile_identity_suci.scheme_output[0] = 0; - mobile_identity_suci.scheme_output[1] = 0; - mobile_identity_suci.scheme_output[2] = 0x20; - mobile_identity_suci.scheme_output[3] = 0x31; - mobile_identity_suci.scheme_output[4] = 0x90; - test_ue = test_ue_add_by_suci(&mobile_identity_suci, 13); + test_ue = test_ue_add_by_suci(&mobile_identity_suci, "0000203190"); ogs_assert(test_ue); test_ue->nr_cgi.cell_id = 0x40001; diff --git a/tests/registration/identity-test.c b/tests/registration/identity-test.c index 4d54a1075..6b8de0585 100644 --- a/tests/registration/identity-test.c +++ b/tests/registration/identity-test.c @@ -48,15 +48,10 @@ static void test1_func(abts_case *tc, void *data) mobile_identity_suci.routing_indicator2 = 0xf; mobile_identity_suci.routing_indicator3 = 0xf; mobile_identity_suci.routing_indicator4 = 0xf; - mobile_identity_suci.protection_scheme_id = OGS_NAS_5GS_NULL_SCHEME; + mobile_identity_suci.protection_scheme_id = OGS_PROTECTION_SCHEME_NULL; mobile_identity_suci.home_network_pki_value = 0; - mobile_identity_suci.scheme_output[0] = 0; - mobile_identity_suci.scheme_output[1] = 0; - mobile_identity_suci.scheme_output[2] = 0x20; - mobile_identity_suci.scheme_output[3] = 0x31; - mobile_identity_suci.scheme_output[4] = 0x90; - test_ue = test_ue_add_by_suci(&mobile_identity_suci, 13); + test_ue = test_ue_add_by_suci(&mobile_identity_suci, "0000203190"); ogs_assert(test_ue); test_ue->nr_cgi.cell_id = 0x40001; @@ -315,7 +310,7 @@ static void test1_func(abts_case *tc, void *data) #endif /* INVALID SUCI */ - test_ue->mobile_identity_suci.scheme_output[0] = 0x99; + ((uint8_t *)(test_ue->mobile_identity.buffer))[9] = 0x99; /* Send Identity response */ gmmbuf = testgmm_build_identity_response(test_ue); diff --git a/tests/registration/idle-test.c b/tests/registration/idle-test.c index 0d792185d..a72fc448d 100644 --- a/tests/registration/idle-test.c +++ b/tests/registration/idle-test.c @@ -48,15 +48,10 @@ static void test1_func(abts_case *tc, void *data) mobile_identity_suci.routing_indicator2 = 0xf; mobile_identity_suci.routing_indicator3 = 0xf; mobile_identity_suci.routing_indicator4 = 0xf; - mobile_identity_suci.protection_scheme_id = OGS_NAS_5GS_NULL_SCHEME; + mobile_identity_suci.protection_scheme_id = OGS_PROTECTION_SCHEME_NULL; mobile_identity_suci.home_network_pki_value = 0; - mobile_identity_suci.scheme_output[0] = 0; - mobile_identity_suci.scheme_output[1] = 0; - mobile_identity_suci.scheme_output[2] = 0x20; - mobile_identity_suci.scheme_output[3] = 0x31; - mobile_identity_suci.scheme_output[4] = 0x90; - test_ue = test_ue_add_by_suci(&mobile_identity_suci, 13); + test_ue = test_ue_add_by_suci(&mobile_identity_suci, "0000203190"); ogs_assert(test_ue); test_ue->nr_cgi.cell_id = 0x40001; @@ -444,15 +439,10 @@ static void test2_func(abts_case *tc, void *data) mobile_identity_suci.routing_indicator2 = 0xf; mobile_identity_suci.routing_indicator3 = 0xf; mobile_identity_suci.routing_indicator4 = 0xf; - mobile_identity_suci.protection_scheme_id = OGS_NAS_5GS_NULL_SCHEME; + mobile_identity_suci.protection_scheme_id = OGS_PROTECTION_SCHEME_NULL; mobile_identity_suci.home_network_pki_value = 0; - mobile_identity_suci.scheme_output[0] = 0; - mobile_identity_suci.scheme_output[1] = 0; - mobile_identity_suci.scheme_output[2] = 0x20; - mobile_identity_suci.scheme_output[3] = 0x31; - mobile_identity_suci.scheme_output[4] = 0x90; - test_ue = test_ue_add_by_suci(&mobile_identity_suci, 13); + test_ue = test_ue_add_by_suci(&mobile_identity_suci, "0000203190"); ogs_assert(test_ue); test_ue->nr_cgi.cell_id = 0x40001; @@ -692,15 +682,10 @@ static void test3_func(abts_case *tc, void *data) mobile_identity_suci.routing_indicator2 = 0xf; mobile_identity_suci.routing_indicator3 = 0xf; mobile_identity_suci.routing_indicator4 = 0xf; - mobile_identity_suci.protection_scheme_id = OGS_NAS_5GS_NULL_SCHEME; + mobile_identity_suci.protection_scheme_id = OGS_PROTECTION_SCHEME_NULL; mobile_identity_suci.home_network_pki_value = 0; - mobile_identity_suci.scheme_output[0] = 0; - mobile_identity_suci.scheme_output[1] = 0; - mobile_identity_suci.scheme_output[2] = 0x20; - mobile_identity_suci.scheme_output[3] = 0x31; - mobile_identity_suci.scheme_output[4] = 0x90; - test_ue = test_ue_add_by_suci(&mobile_identity_suci, 13); + test_ue = test_ue_add_by_suci(&mobile_identity_suci, "0000203190"); ogs_assert(test_ue); test_ue->nr_cgi.cell_id = 0x40001; @@ -1050,15 +1035,10 @@ static void test4_func(abts_case *tc, void *data) mobile_identity_suci.routing_indicator2 = 0xf; mobile_identity_suci.routing_indicator3 = 0xf; mobile_identity_suci.routing_indicator4 = 0xf; - mobile_identity_suci.protection_scheme_id = OGS_NAS_5GS_NULL_SCHEME; + mobile_identity_suci.protection_scheme_id = OGS_PROTECTION_SCHEME_NULL; mobile_identity_suci.home_network_pki_value = 0; - mobile_identity_suci.scheme_output[0] = 0; - mobile_identity_suci.scheme_output[1] = 0; - mobile_identity_suci.scheme_output[2] = 0x20; - mobile_identity_suci.scheme_output[3] = 0x31; - mobile_identity_suci.scheme_output[4] = 0x90; - test_ue = test_ue_add_by_suci(&mobile_identity_suci, 13); + test_ue = test_ue_add_by_suci(&mobile_identity_suci, "0000203190"); ogs_assert(test_ue); test_ue->nr_cgi.cell_id = 0x40001; @@ -1503,15 +1483,10 @@ static void test5_func(abts_case *tc, void *data) mobile_identity_suci.routing_indicator2 = 0xf; mobile_identity_suci.routing_indicator3 = 0xf; mobile_identity_suci.routing_indicator4 = 0xf; - mobile_identity_suci.protection_scheme_id = OGS_NAS_5GS_NULL_SCHEME; + mobile_identity_suci.protection_scheme_id = OGS_PROTECTION_SCHEME_NULL; mobile_identity_suci.home_network_pki_value = 0; - mobile_identity_suci.scheme_output[0] = 0; - mobile_identity_suci.scheme_output[1] = 0; - mobile_identity_suci.scheme_output[2] = 0x20; - mobile_identity_suci.scheme_output[3] = 0x31; - mobile_identity_suci.scheme_output[4] = 0x90; - test_ue = test_ue_add_by_suci(&mobile_identity_suci, 13); + test_ue = test_ue_add_by_suci(&mobile_identity_suci, "0000203190"); ogs_assert(test_ue); test_ue->nr_cgi.cell_id = 0x40001; @@ -1862,15 +1837,10 @@ static void test6_func(abts_case *tc, void *data) mobile_identity_suci.routing_indicator2 = 0xf; mobile_identity_suci.routing_indicator3 = 0xf; mobile_identity_suci.routing_indicator4 = 0xf; - mobile_identity_suci.protection_scheme_id = OGS_NAS_5GS_NULL_SCHEME; + mobile_identity_suci.protection_scheme_id = OGS_PROTECTION_SCHEME_NULL; mobile_identity_suci.home_network_pki_value = 0; - mobile_identity_suci.scheme_output[0] = 0; - mobile_identity_suci.scheme_output[1] = 0; - mobile_identity_suci.scheme_output[2] = 0x20; - mobile_identity_suci.scheme_output[3] = 0x31; - mobile_identity_suci.scheme_output[4] = 0x90; - test_ue = test_ue_add_by_suci(&mobile_identity_suci, 13); + test_ue = test_ue_add_by_suci(&mobile_identity_suci, "0000203190"); ogs_assert(test_ue); test_ue->nr_cgi.cell_id = 0x40001; diff --git a/tests/registration/meson.build b/tests/registration/meson.build index bee8611f7..76540e23a 100644 --- a/tests/registration/meson.build +++ b/tests/registration/meson.build @@ -20,6 +20,7 @@ test5gc_registration_sources = files(''' simple-test.c guti-test.c auth-test.c + ecc-test.c idle-test.c dereg-test.c paging-test.c diff --git a/tests/registration/multi-ue-test.c b/tests/registration/multi-ue-test.c index 4b3672345..5a058b7ba 100644 --- a/tests/registration/multi-ue-test.c +++ b/tests/registration/multi-ue-test.c @@ -60,7 +60,13 @@ static void test1_func(abts_case *tc, void *data) ABTS_PTR_NOTNULL(tc, recvbuf); for (i = 0; i < NUM_OF_TEST_UE; i++) { - uint64_t imsi_index; + const char *scheme_output[] = { + "0000000001", + "0000000002", + "0000000003", + "0000000004", + "0000000005", + }; /* Setup Test UE & Session Context */ memset(&mobile_identity_suci, 0, sizeof(mobile_identity_suci)); @@ -71,18 +77,11 @@ static void test1_func(abts_case *tc, void *data) mobile_identity_suci.routing_indicator2 = 0xf; mobile_identity_suci.routing_indicator3 = 0xf; mobile_identity_suci.routing_indicator4 = 0xf; - mobile_identity_suci.protection_scheme_id = OGS_NAS_5GS_NULL_SCHEME; + mobile_identity_suci.protection_scheme_id = OGS_PROTECTION_SCHEME_NULL; mobile_identity_suci.home_network_pki_value = 0; - mobile_identity_suci.scheme_output[0] = 0; - mobile_identity_suci.scheme_output[1] = 0; - mobile_identity_suci.scheme_output[2] = 0x20; - mobile_identity_suci.scheme_output[3] = 0x31; - mobile_identity_suci.scheme_output[4] = 0x90; - imsi_index = i + 1; - ogs_uint64_to_buffer(imsi_index, 5, mobile_identity_suci.scheme_output); - - test_ue[i] = test_ue_add_by_suci(&mobile_identity_suci, 13); + test_ue[i] = test_ue_add_by_suci( + &mobile_identity_suci, scheme_output[i]); ogs_assert(test_ue[i]); test_ue[i]->nr_cgi.cell_id = 0x40001; diff --git a/tests/registration/paging-test.c b/tests/registration/paging-test.c index 10b102350..48047a665 100644 --- a/tests/registration/paging-test.c +++ b/tests/registration/paging-test.c @@ -48,15 +48,10 @@ static void cm_idle_paging_func(abts_case *tc, void *data) mobile_identity_suci.routing_indicator2 = 0xf; mobile_identity_suci.routing_indicator3 = 0xf; mobile_identity_suci.routing_indicator4 = 0xf; - mobile_identity_suci.protection_scheme_id = OGS_NAS_5GS_NULL_SCHEME; + mobile_identity_suci.protection_scheme_id = OGS_PROTECTION_SCHEME_NULL; mobile_identity_suci.home_network_pki_value = 0; - mobile_identity_suci.scheme_output[0] = 0; - mobile_identity_suci.scheme_output[1] = 0; - mobile_identity_suci.scheme_output[2] = 0x20; - mobile_identity_suci.scheme_output[3] = 0x31; - mobile_identity_suci.scheme_output[4] = 0x90; - test_ue = test_ue_add_by_suci(&mobile_identity_suci, 13); + test_ue = test_ue_add_by_suci(&mobile_identity_suci, "0000203190"); ogs_assert(test_ue); test_ue->nr_cgi.cell_id = 0x40001; @@ -614,15 +609,10 @@ static void cm_connected_paging_func(abts_case *tc, void *data) mobile_identity_suci.routing_indicator2 = 0xf; mobile_identity_suci.routing_indicator3 = 0xf; mobile_identity_suci.routing_indicator4 = 0xf; - mobile_identity_suci.protection_scheme_id = OGS_NAS_5GS_NULL_SCHEME; + mobile_identity_suci.protection_scheme_id = OGS_PROTECTION_SCHEME_NULL; mobile_identity_suci.home_network_pki_value = 0; - mobile_identity_suci.scheme_output[0] = 0; - mobile_identity_suci.scheme_output[1] = 0; - mobile_identity_suci.scheme_output[2] = 0x20; - mobile_identity_suci.scheme_output[3] = 0x31; - mobile_identity_suci.scheme_output[4] = 0x90; - test_ue = test_ue_add_by_suci(&mobile_identity_suci, 13); + test_ue = test_ue_add_by_suci(&mobile_identity_suci, "0000203190"); ogs_assert(test_ue); test_ue->nr_cgi.cell_id = 0x40001; @@ -959,15 +949,10 @@ static void cm_idle_error_indication_func(abts_case *tc, void *data) mobile_identity_suci.routing_indicator2 = 0xf; mobile_identity_suci.routing_indicator3 = 0xf; mobile_identity_suci.routing_indicator4 = 0xf; - mobile_identity_suci.protection_scheme_id = OGS_NAS_5GS_NULL_SCHEME; + mobile_identity_suci.protection_scheme_id = OGS_PROTECTION_SCHEME_NULL; mobile_identity_suci.home_network_pki_value = 0; - mobile_identity_suci.scheme_output[0] = 0; - mobile_identity_suci.scheme_output[1] = 0; - mobile_identity_suci.scheme_output[2] = 0x20; - mobile_identity_suci.scheme_output[3] = 0x31; - mobile_identity_suci.scheme_output[4] = 0x90; - test_ue = test_ue_add_by_suci(&mobile_identity_suci, 13); + test_ue = test_ue_add_by_suci(&mobile_identity_suci, "0000203190"); ogs_assert(test_ue); test_ue->nr_cgi.cell_id = 0x40001; @@ -1306,15 +1291,10 @@ static void cm_connected_error_indication_func(abts_case *tc, void *data) mobile_identity_suci.routing_indicator2 = 0xf; mobile_identity_suci.routing_indicator3 = 0xf; mobile_identity_suci.routing_indicator4 = 0xf; - mobile_identity_suci.protection_scheme_id = OGS_NAS_5GS_NULL_SCHEME; + mobile_identity_suci.protection_scheme_id = OGS_PROTECTION_SCHEME_NULL; mobile_identity_suci.home_network_pki_value = 0; - mobile_identity_suci.scheme_output[0] = 0; - mobile_identity_suci.scheme_output[1] = 0; - mobile_identity_suci.scheme_output[2] = 0x20; - mobile_identity_suci.scheme_output[3] = 0x31; - mobile_identity_suci.scheme_output[4] = 0x90; - test_ue = test_ue_add_by_suci(&mobile_identity_suci, 13); + test_ue = test_ue_add_by_suci(&mobile_identity_suci, "0000203190"); ogs_assert(test_ue); test_ue->nr_cgi.cell_id = 0x40001; @@ -1589,15 +1569,10 @@ static void vonr_qos_flow_test1_func(abts_case *tc, void *data) mobile_identity_suci.routing_indicator2 = 0xf; mobile_identity_suci.routing_indicator3 = 0xf; mobile_identity_suci.routing_indicator4 = 0xf; - mobile_identity_suci.protection_scheme_id = OGS_NAS_5GS_NULL_SCHEME; + mobile_identity_suci.protection_scheme_id = OGS_PROTECTION_SCHEME_NULL; mobile_identity_suci.home_network_pki_value = 0; - mobile_identity_suci.scheme_output[0] = 0; - mobile_identity_suci.scheme_output[1] = 0; - mobile_identity_suci.scheme_output[2] = 0x20; - mobile_identity_suci.scheme_output[3] = 0x31; - mobile_identity_suci.scheme_output[4] = 0x90; - test_ue = test_ue_add_by_suci(&mobile_identity_suci, 13); + test_ue = test_ue_add_by_suci(&mobile_identity_suci, "0000203190"); ogs_assert(test_ue); test_ue->nr_cgi.cell_id = 0x40001; @@ -1989,15 +1964,10 @@ static void vonr_session_test2_func(abts_case *tc, void *data) mobile_identity_suci.routing_indicator2 = 0xf; mobile_identity_suci.routing_indicator3 = 0xf; mobile_identity_suci.routing_indicator4 = 0xf; - mobile_identity_suci.protection_scheme_id = OGS_NAS_5GS_NULL_SCHEME; + mobile_identity_suci.protection_scheme_id = OGS_PROTECTION_SCHEME_NULL; mobile_identity_suci.home_network_pki_value = 0; - mobile_identity_suci.scheme_output[0] = 0; - mobile_identity_suci.scheme_output[1] = 0; - mobile_identity_suci.scheme_output[2] = 0x20; - mobile_identity_suci.scheme_output[3] = 0x31; - mobile_identity_suci.scheme_output[4] = 0x90; - test_ue = test_ue_add_by_suci(&mobile_identity_suci, 13); + test_ue = test_ue_add_by_suci(&mobile_identity_suci, "0000203190"); ogs_assert(test_ue); test_ue->nr_cgi.cell_id = 0x40001; @@ -2483,15 +2453,10 @@ static void registration_ue_context_test4_func(abts_case *tc, void *data) mobile_identity_suci.routing_indicator2 = 0xf; mobile_identity_suci.routing_indicator3 = 0xf; mobile_identity_suci.routing_indicator4 = 0xf; - mobile_identity_suci.protection_scheme_id = OGS_NAS_5GS_NULL_SCHEME; + mobile_identity_suci.protection_scheme_id = OGS_PROTECTION_SCHEME_NULL; mobile_identity_suci.home_network_pki_value = 0; - mobile_identity_suci.scheme_output[0] = 0; - mobile_identity_suci.scheme_output[1] = 0; - mobile_identity_suci.scheme_output[2] = 0x20; - mobile_identity_suci.scheme_output[3] = 0x31; - mobile_identity_suci.scheme_output[4] = 0x90; - test_ue = test_ue_add_by_suci(&mobile_identity_suci, 13); + test_ue = test_ue_add_by_suci(&mobile_identity_suci, "0000203190"); ogs_assert(test_ue); test_ue->nr_cgi.cell_id = 0x40001; @@ -2877,15 +2842,10 @@ static void registration_idle_test1_func(abts_case *tc, void *data) mobile_identity_suci.routing_indicator2 = 0xf; mobile_identity_suci.routing_indicator3 = 0xf; mobile_identity_suci.routing_indicator4 = 0xf; - mobile_identity_suci.protection_scheme_id = OGS_NAS_5GS_NULL_SCHEME; + mobile_identity_suci.protection_scheme_id = OGS_PROTECTION_SCHEME_NULL; mobile_identity_suci.home_network_pki_value = 0; - mobile_identity_suci.scheme_output[0] = 0; - mobile_identity_suci.scheme_output[1] = 0; - mobile_identity_suci.scheme_output[2] = 0x20; - mobile_identity_suci.scheme_output[3] = 0x31; - mobile_identity_suci.scheme_output[4] = 0x90; - test_ue = test_ue_add_by_suci(&mobile_identity_suci, 13); + test_ue = test_ue_add_by_suci(&mobile_identity_suci, "0000203190"); ogs_assert(test_ue); test_ue->nr_cgi.cell_id = 0x40001; diff --git a/tests/registration/reset-test.c b/tests/registration/reset-test.c index 3112fd589..5247b56d6 100644 --- a/tests/registration/reset-test.c +++ b/tests/registration/reset-test.c @@ -48,15 +48,10 @@ static void test1_func(abts_case *tc, void *data) mobile_identity_suci.routing_indicator2 = 0xf; mobile_identity_suci.routing_indicator3 = 0xf; mobile_identity_suci.routing_indicator4 = 0xf; - mobile_identity_suci.protection_scheme_id = OGS_NAS_5GS_NULL_SCHEME; + mobile_identity_suci.protection_scheme_id = OGS_PROTECTION_SCHEME_NULL; mobile_identity_suci.home_network_pki_value = 0; - mobile_identity_suci.scheme_output[0] = 0; - mobile_identity_suci.scheme_output[1] = 0; - mobile_identity_suci.scheme_output[2] = 0x20; - mobile_identity_suci.scheme_output[3] = 0x31; - mobile_identity_suci.scheme_output[4] = 0x90; - test_ue = test_ue_add_by_suci(&mobile_identity_suci, 13); + test_ue = test_ue_add_by_suci(&mobile_identity_suci, "0000203190"); ogs_assert(test_ue); test_ue->nr_cgi.cell_id = 0x40001; @@ -360,15 +355,10 @@ static void test2_func(abts_case *tc, void *data) mobile_identity_suci.routing_indicator2 = 0xf; mobile_identity_suci.routing_indicator3 = 0xf; mobile_identity_suci.routing_indicator4 = 0xf; - mobile_identity_suci.protection_scheme_id = OGS_NAS_5GS_NULL_SCHEME; + mobile_identity_suci.protection_scheme_id = OGS_PROTECTION_SCHEME_NULL; mobile_identity_suci.home_network_pki_value = 0; - mobile_identity_suci.scheme_output[0] = 0; - mobile_identity_suci.scheme_output[1] = 0; - mobile_identity_suci.scheme_output[2] = 0x20; - mobile_identity_suci.scheme_output[3] = 0x31; - mobile_identity_suci.scheme_output[4] = 0x90; - test_ue = test_ue_add_by_suci(&mobile_identity_suci, 13); + test_ue = test_ue_add_by_suci(&mobile_identity_suci, "0000203190"); ogs_assert(test_ue); test_ue->nr_cgi.cell_id = 0x40001; @@ -625,15 +615,10 @@ static void test3_func(abts_case *tc, void *data) mobile_identity_suci.routing_indicator2 = 0xf; mobile_identity_suci.routing_indicator3 = 0xf; mobile_identity_suci.routing_indicator4 = 0xf; - mobile_identity_suci.protection_scheme_id = OGS_NAS_5GS_NULL_SCHEME; + mobile_identity_suci.protection_scheme_id = OGS_PROTECTION_SCHEME_NULL; mobile_identity_suci.home_network_pki_value = 0; - mobile_identity_suci.scheme_output[0] = 0; - mobile_identity_suci.scheme_output[1] = 0; - mobile_identity_suci.scheme_output[2] = 0x20; - mobile_identity_suci.scheme_output[3] = 0x31; - mobile_identity_suci.scheme_output[4] = 0x90; - test_ue = test_ue_add_by_suci(&mobile_identity_suci, 13); + test_ue = test_ue_add_by_suci(&mobile_identity_suci, "0000203190"); ogs_assert(test_ue); test_ue->nr_cgi.cell_id = 0x40001; diff --git a/tests/registration/simple-test.c b/tests/registration/simple-test.c index b351a1664..5acbcd554 100644 --- a/tests/registration/simple-test.c +++ b/tests/registration/simple-test.c @@ -48,15 +48,10 @@ static void test1_func(abts_case *tc, void *data) mobile_identity_suci.routing_indicator2 = 0xf; mobile_identity_suci.routing_indicator3 = 0xf; mobile_identity_suci.routing_indicator4 = 0xf; - mobile_identity_suci.protection_scheme_id = OGS_NAS_5GS_NULL_SCHEME; + mobile_identity_suci.protection_scheme_id = OGS_PROTECTION_SCHEME_NULL; mobile_identity_suci.home_network_pki_value = 0; - mobile_identity_suci.scheme_output[0] = 0; - mobile_identity_suci.scheme_output[1] = 0; - mobile_identity_suci.scheme_output[2] = 0x20; - mobile_identity_suci.scheme_output[3] = 0x31; - mobile_identity_suci.scheme_output[4] = 0x90; - test_ue = test_ue_add_by_suci(&mobile_identity_suci, 13); + test_ue = test_ue_add_by_suci(&mobile_identity_suci, "0000203190"); ogs_assert(test_ue); test_ue->nr_cgi.cell_id = 0x40001; diff --git a/tests/registration/ue-context-test.c b/tests/registration/ue-context-test.c index b6c0fbaa1..1168ada19 100644 --- a/tests/registration/ue-context-test.c +++ b/tests/registration/ue-context-test.c @@ -48,15 +48,10 @@ static void test1_func(abts_case *tc, void *data) mobile_identity_suci.routing_indicator2 = 0xf; mobile_identity_suci.routing_indicator3 = 0xf; mobile_identity_suci.routing_indicator4 = 0xf; - mobile_identity_suci.protection_scheme_id = OGS_NAS_5GS_NULL_SCHEME; + mobile_identity_suci.protection_scheme_id = OGS_PROTECTION_SCHEME_NULL; mobile_identity_suci.home_network_pki_value = 0; - mobile_identity_suci.scheme_output[0] = 0; - mobile_identity_suci.scheme_output[1] = 0; - mobile_identity_suci.scheme_output[2] = 0x20; - mobile_identity_suci.scheme_output[3] = 0x31; - mobile_identity_suci.scheme_output[4] = 0x90; - test_ue = test_ue_add_by_suci(&mobile_identity_suci, 13); + test_ue = test_ue_add_by_suci(&mobile_identity_suci, "0000203190"); ogs_assert(test_ue); test_ue->nr_cgi.cell_id = 0x40001; @@ -209,15 +204,10 @@ static void test2_func(abts_case *tc, void *data) mobile_identity_suci.routing_indicator2 = 0xf; mobile_identity_suci.routing_indicator3 = 0xf; mobile_identity_suci.routing_indicator4 = 0xf; - mobile_identity_suci.protection_scheme_id = OGS_NAS_5GS_NULL_SCHEME; + mobile_identity_suci.protection_scheme_id = OGS_PROTECTION_SCHEME_NULL; mobile_identity_suci.home_network_pki_value = 0; - mobile_identity_suci.scheme_output[0] = 0; - mobile_identity_suci.scheme_output[1] = 0; - mobile_identity_suci.scheme_output[2] = 0x20; - mobile_identity_suci.scheme_output[3] = 0x31; - mobile_identity_suci.scheme_output[4] = 0x90; - test_ue = test_ue_add_by_suci(&mobile_identity_suci, 13); + test_ue = test_ue_add_by_suci(&mobile_identity_suci, "0000203190"); ogs_assert(test_ue); test_ue->nr_cgi.cell_id = 0x40001; @@ -509,15 +499,10 @@ static void test3_func(abts_case *tc, void *data) mobile_identity_suci.routing_indicator2 = 0xf; mobile_identity_suci.routing_indicator3 = 0xf; mobile_identity_suci.routing_indicator4 = 0xf; - mobile_identity_suci.protection_scheme_id = OGS_NAS_5GS_NULL_SCHEME; + mobile_identity_suci.protection_scheme_id = OGS_PROTECTION_SCHEME_NULL; mobile_identity_suci.home_network_pki_value = 0; - mobile_identity_suci.scheme_output[0] = 0; - mobile_identity_suci.scheme_output[1] = 0; - mobile_identity_suci.scheme_output[2] = 0x20; - mobile_identity_suci.scheme_output[3] = 0x31; - mobile_identity_suci.scheme_output[4] = 0x90; - test_ue = test_ue_add_by_suci(&mobile_identity_suci, 13); + test_ue = test_ue_add_by_suci(&mobile_identity_suci, "0000203190"); ogs_assert(test_ue); test_ue->nr_cgi.cell_id = 0x40001; @@ -957,15 +942,10 @@ static void test4_func(abts_case *tc, void *data) mobile_identity_suci.routing_indicator2 = 0xf; mobile_identity_suci.routing_indicator3 = 0xf; mobile_identity_suci.routing_indicator4 = 0xf; - mobile_identity_suci.protection_scheme_id = OGS_NAS_5GS_NULL_SCHEME; + mobile_identity_suci.protection_scheme_id = OGS_PROTECTION_SCHEME_NULL; mobile_identity_suci.home_network_pki_value = 0; - mobile_identity_suci.scheme_output[0] = 0; - mobile_identity_suci.scheme_output[1] = 0; - mobile_identity_suci.scheme_output[2] = 0x20; - mobile_identity_suci.scheme_output[3] = 0x31; - mobile_identity_suci.scheme_output[4] = 0x90; - test_ue = test_ue_add_by_suci(&mobile_identity_suci, 13); + test_ue = test_ue_add_by_suci(&mobile_identity_suci, "0000203190"); ogs_assert(test_ue); test_ue->nr_cgi.cell_id = 0x40001; @@ -1332,15 +1312,10 @@ static void test5_func(abts_case *tc, void *data) mobile_identity_suci.routing_indicator2 = 0xf; mobile_identity_suci.routing_indicator3 = 0xf; mobile_identity_suci.routing_indicator4 = 0xf; - mobile_identity_suci.protection_scheme_id = OGS_NAS_5GS_NULL_SCHEME; + mobile_identity_suci.protection_scheme_id = OGS_PROTECTION_SCHEME_NULL; mobile_identity_suci.home_network_pki_value = 0; - mobile_identity_suci.scheme_output[0] = 0; - mobile_identity_suci.scheme_output[1] = 0; - mobile_identity_suci.scheme_output[2] = 0x20; - mobile_identity_suci.scheme_output[3] = 0x31; - mobile_identity_suci.scheme_output[4] = 0x90; - test_ue = test_ue_add_by_suci(&mobile_identity_suci, 13); + test_ue = test_ue_add_by_suci(&mobile_identity_suci, "0000203190"); ogs_assert(test_ue); test_ue->nr_cgi.cell_id = 0x40001; diff --git a/tests/slice/different-dnn-test.c b/tests/slice/different-dnn-test.c index 3c2e57396..b67abfb92 100644 --- a/tests/slice/different-dnn-test.c +++ b/tests/slice/different-dnn-test.c @@ -48,14 +48,10 @@ static void test1_func(abts_case *tc, void *data) mobile_identity_suci.routing_indicator2 = 0xf; mobile_identity_suci.routing_indicator3 = 0xf; mobile_identity_suci.routing_indicator4 = 0xf; - mobile_identity_suci.protection_scheme_id = OGS_NAS_5GS_NULL_SCHEME; + mobile_identity_suci.protection_scheme_id = OGS_PROTECTION_SCHEME_NULL; mobile_identity_suci.home_network_pki_value = 0; - mobile_identity_suci.scheme_output[0] = 0; - mobile_identity_suci.scheme_output[1] = 0; - mobile_identity_suci.scheme_output[2] = 0x47; - mobile_identity_suci.scheme_output[3] = 0x78; - test_ue = test_ue_add_by_suci(&mobile_identity_suci, 12); + test_ue = test_ue_add_by_suci(&mobile_identity_suci, "00004778"); ogs_assert(test_ue); test_ue->nr_cgi.cell_id = 0x40001; diff --git a/tests/slice/same-dnn-test.c b/tests/slice/same-dnn-test.c index 8ad4b79b4..07d637fda 100644 --- a/tests/slice/same-dnn-test.c +++ b/tests/slice/same-dnn-test.c @@ -48,14 +48,10 @@ static void test1_func(abts_case *tc, void *data) mobile_identity_suci.routing_indicator2 = 0xf; mobile_identity_suci.routing_indicator3 = 0xf; mobile_identity_suci.routing_indicator4 = 0xf; - mobile_identity_suci.protection_scheme_id = OGS_NAS_5GS_NULL_SCHEME; + mobile_identity_suci.protection_scheme_id = OGS_PROTECTION_SCHEME_NULL; mobile_identity_suci.home_network_pki_value = 0; - mobile_identity_suci.scheme_output[0] = 0; - mobile_identity_suci.scheme_output[1] = 0; - mobile_identity_suci.scheme_output[2] = 0x47; - mobile_identity_suci.scheme_output[3] = 0x78; - test_ue = test_ue_add_by_suci(&mobile_identity_suci, 12); + test_ue = test_ue_add_by_suci(&mobile_identity_suci, "00004778"); ogs_assert(test_ue); test_ue->nr_cgi.cell_id = 0x40001; @@ -353,14 +349,10 @@ static void test2_func(abts_case *tc, void *data) mobile_identity_suci.routing_indicator2 = 0xf; mobile_identity_suci.routing_indicator3 = 0xf; mobile_identity_suci.routing_indicator4 = 0xf; - mobile_identity_suci.protection_scheme_id = OGS_NAS_5GS_NULL_SCHEME; + mobile_identity_suci.protection_scheme_id = OGS_PROTECTION_SCHEME_NULL; mobile_identity_suci.home_network_pki_value = 0; - mobile_identity_suci.scheme_output[0] = 0; - mobile_identity_suci.scheme_output[1] = 0; - mobile_identity_suci.scheme_output[2] = 0x47; - mobile_identity_suci.scheme_output[3] = 0x78; - test_ue = test_ue_add_by_suci(&mobile_identity_suci, 12); + test_ue = test_ue_add_by_suci(&mobile_identity_suci, "00004778"); ogs_assert(test_ue); test_ue->nr_cgi.cell_id = 0x40001; diff --git a/tests/unit/crash-test.c b/tests/unit/crash-test.c index c31664b6a..2bccc9c83 100644 --- a/tests/unit/crash-test.c +++ b/tests/unit/crash-test.c @@ -348,7 +348,7 @@ static void test2_func(abts_case *tc, void *data) enb_pkbuf = ogs_pkbuf_alloc(NULL, OGS_MAX_SDU_LEN); ogs_assert(enb_pkbuf); ogs_pkbuf_put_data(enb_pkbuf, - OGS_HEX(payload, strlen(payload), hexbuf), 38); + ogs_hex_from_string(payload, hexbuf, sizeof(hexbuf)), 38); result = ogs_s1ap_decode(&message, enb_pkbuf); ABTS_INT_EQUAL(tc, 0, result); @@ -375,7 +375,7 @@ static void test3_func(abts_case *tc, void *data) enb_pkbuf = ogs_pkbuf_alloc(NULL, OGS_MAX_SDU_LEN); ogs_assert(enb_pkbuf); ogs_pkbuf_put_data(enb_pkbuf, - OGS_HEX(payload, strlen(payload), hexbuf), 44); + ogs_hex_from_string(payload, hexbuf, sizeof(hexbuf)), 44); result = ogs_s1ap_decode(&message, enb_pkbuf); ABTS_INT_EQUAL(tc, 0, result); @@ -401,7 +401,7 @@ static void test4_func(abts_case *tc, void *data) enb_pkbuf = ogs_pkbuf_alloc(NULL, OGS_MAX_SDU_LEN); ogs_assert(enb_pkbuf); ogs_pkbuf_put_data(enb_pkbuf, - OGS_HEX(payload, strlen(payload), hexbuf), 59); + ogs_hex_from_string(payload, hexbuf, sizeof(hexbuf)), 59); result = ogs_s1ap_decode(&message, enb_pkbuf); ABTS_INT_EQUAL(tc, 0, result); @@ -425,7 +425,7 @@ static void test5_func(abts_case *tc, void *data) enb_pkbuf = ogs_pkbuf_alloc(NULL, OGS_MAX_SDU_LEN); ogs_assert(enb_pkbuf); ogs_pkbuf_put_data(enb_pkbuf, - OGS_HEX(payload, strlen(payload), hexbuf), 72); + ogs_hex_from_string(payload, hexbuf, sizeof(hexbuf)), 72); result = ogs_s1ap_decode(&message, enb_pkbuf); ABTS_INT_EQUAL(tc, 0, result); @@ -592,7 +592,7 @@ static void test6_func(abts_case *tc, void *data) emmbuf = ogs_pkbuf_alloc(NULL, OGS_MAX_SDU_LEN); ogs_assert(emmbuf); ogs_pkbuf_put_data(emmbuf, - OGS_HEX(nas_payload, strlen(nas_payload), hexbuf), 13); + ogs_hex_from_string(nas_payload, hexbuf, sizeof(hexbuf)), 13); s1apbuf = test_build_uplink_nas_transport(1, 1, emmbuf); ABTS_PTR_NOTNULL(tc, s1apbuf); diff --git a/tests/unit/gtp-message-test.c b/tests/unit/gtp-message-test.c index 404fa79c2..257fceb45 100644 --- a/tests/unit/gtp-message-test.c +++ b/tests/unit/gtp-message-test.c @@ -109,7 +109,8 @@ static void gtp_message_test1(abts_case *tc, void *data) _value = (char*)"05766f6c7465036e 6732046d6e657406 6d6e63303130066d 6363353535046770 7273"; req.access_point_name.presence = 1; - req.access_point_name.data = OGS_HEX(_value, strlen(_value), apnbuf); + req.access_point_name.data = + ogs_hex_from_string(_value, apnbuf, sizeof(apnbuf)); req.access_point_name.len = sizeof(apnbuf); req.selection_mode.presence = 1; @@ -183,8 +184,8 @@ static void gtp_message_test1(abts_case *tc, void *data) &req, OGS_TLV_MODE_T1_L2_I1); ABTS_PTR_NOTNULL(tc, pkbuf); - ABTS_TRUE(tc, memcmp(pkbuf->data, - OGS_HEX(_payload, strlen(_payload), hexbuf), pkbuf->len) == 0); + ABTS_TRUE(tc, memcmp(pkbuf->data, ogs_hex_from_string( + _payload, hexbuf, sizeof(hexbuf)), pkbuf->len) == 0); memset(&req, 0, sizeof(req)); rv = ogs_tlv_parse_msg(&req, &ogs_gtp2_tlv_desc_create_session_request, @@ -194,19 +195,19 @@ static void gtp_message_test1(abts_case *tc, void *data) ABTS_INT_EQUAL(tc, 1, req.imsi.presence); ABTS_INT_EQUAL(tc, 8, req.imsi.len); _value = (char*)"55153011 340010f4"; - ABTS_TRUE(tc, memcmp(OGS_HEX(_value, strlen(_value), hexbuf), + ABTS_TRUE(tc, memcmp(ogs_hex_from_string(_value, hexbuf, sizeof(hexbuf)), req.imsi.data, req.imsi.len) == 0); ABTS_INT_EQUAL(tc, 1, req.msisdn.presence); ABTS_INT_EQUAL(tc, 6, req.msisdn.len); _value = (char*)"94715276 0041"; - ABTS_TRUE(tc, memcmp(OGS_HEX(_value, strlen(_value), hexbuf), + ABTS_TRUE(tc, memcmp(ogs_hex_from_string(_value, hexbuf, sizeof(hexbuf)), req.msisdn.data, req.msisdn.len) == 0); ABTS_INT_EQUAL(tc, 1, req.me_identity.presence); ABTS_INT_EQUAL(tc, 8, req.me_identity.len); _value = (char*)"53612000 91788400"; - ABTS_TRUE(tc, memcmp(OGS_HEX(_value, strlen(_value), hexbuf), + ABTS_TRUE(tc, memcmp(ogs_hex_from_string(_value, hexbuf, sizeof(hexbuf)), req.me_identity.data, req.me_identity.len) == 0); ABTS_INT_EQUAL(tc, 1, req.user_location_information.presence); diff --git a/tests/unit/nas-message-test.c b/tests/unit/nas-message-test.c index ac992b119..8ab798ae6 100644 --- a/tests/unit/nas-message-test.c +++ b/tests/unit/nas-message-test.c @@ -39,7 +39,7 @@ static void ogs_nas_eps_message_test1(abts_case *tc, void *data) ogs_pkbuf_put(pkbuf, OGS_MAX_SDU_LEN); ogs_pkbuf_trim(pkbuf, 53); memcpy(pkbuf->data, - OGS_HEX(payload, strlen(payload), hexbuf), pkbuf->len); + ogs_hex_from_string(payload, hexbuf, sizeof(hexbuf)), pkbuf->len); rv = ogs_nas_emm_decode(&message, pkbuf); ABTS_INT_EQUAL(tc, OGS_OK, rv); @@ -94,7 +94,7 @@ static void ogs_nas_eps_message_test2(abts_case *tc, void *data) attach_accept->esm_message_container.length = sizeof(esm_buffer); attach_accept->esm_message_container.buffer = - OGS_HEX(esm_payload, strlen(esm_payload), esm_buffer); + ogs_hex_from_string(esm_payload, esm_buffer, sizeof(esm_buffer)); attach_accept->presencemask |= OGS_NAS_EPS_ATTACH_ACCEPT_GUTI_PRESENT; attach_accept->guti.length = 11; @@ -132,7 +132,7 @@ static void ogs_nas_eps_message_test2(abts_case *tc, void *data) pkbuf = ogs_nas_eps_plain_encode(&message); ABTS_INT_EQUAL(tc, sizeof(buffer), pkbuf->len); ogs_log_hexdump(OGS_LOG_DEBUG, pkbuf->data, pkbuf->len); - ABTS_TRUE(tc, memcmp(OGS_HEX(payload, strlen(payload), buffer), + ABTS_TRUE(tc, memcmp(ogs_hex_from_string(payload, buffer, sizeof(buffer)), pkbuf->data, pkbuf->len) == 0); ogs_pkbuf_free(pkbuf); @@ -152,7 +152,7 @@ static void ogs_nas_eps_message_test3(abts_case *tc, void *data) ogs_pkbuf_put(pkbuf, OGS_MAX_SDU_LEN); ogs_pkbuf_trim(pkbuf, 7); memcpy(pkbuf->data, - OGS_HEX(payload, strlen(payload), hexbuf), pkbuf->len); + ogs_hex_from_string(payload, hexbuf, sizeof(hexbuf)), pkbuf->len); rv = ogs_nas_emm_decode(&message, pkbuf); ABTS_INT_EQUAL(tc, OGS_OK, rv); @@ -179,7 +179,7 @@ static void ogs_nas_eps_message_test4(abts_case *tc, void *data) pkbuf = ogs_nas_eps_plain_encode(&message); ABTS_INT_EQUAL(tc, sizeof(buffer), pkbuf->len); - ABTS_TRUE(tc, memcmp(OGS_HEX(payload, strlen(payload), buffer), + ABTS_TRUE(tc, memcmp(ogs_hex_from_string(payload, buffer, sizeof(buffer)), pkbuf->data, pkbuf->len) == 0); ogs_pkbuf_free(pkbuf); @@ -218,7 +218,7 @@ static void ogs_nas_eps_message_test6(abts_case *tc, void *data) ogs_pkbuf_put(pkbuf, OGS_MAX_SDU_LEN); ogs_pkbuf_trim(pkbuf, 3); memcpy(pkbuf->data, - OGS_HEX(payload, strlen(payload), hexbuf), pkbuf->len); + ogs_hex_from_string(payload, hexbuf, sizeof(hexbuf)), pkbuf->len); rv = ogs_nas_emm_decode(&message, pkbuf); ABTS_INT_EQUAL(tc, OGS_OK, rv); @@ -272,7 +272,7 @@ static void ogs_nas_eps_message_test7(abts_case *tc, void *data) pkbuf = ogs_nas_eps_plain_encode(&message); ABTS_INT_EQUAL(tc, sizeof(buffer), pkbuf->len); - ABTS_TRUE(tc, memcmp(OGS_HEX(payload, strlen(payload), buffer), + ABTS_TRUE(tc, memcmp(ogs_hex_from_string(payload, buffer, sizeof(buffer)), pkbuf->data, pkbuf->len) == 0); ogs_pkbuf_free(pkbuf); @@ -298,7 +298,7 @@ static void ogs_nas_eps_message_test8(abts_case *tc, void *data) ogs_pkbuf_put(pkbuf, OGS_MAX_SDU_LEN); ogs_pkbuf_trim(pkbuf, 4); memcpy(pkbuf->data, - OGS_HEX(payload, strlen(payload), hexbuf), pkbuf->len); + ogs_hex_from_string(payload, hexbuf, sizeof(hexbuf)), pkbuf->len); /* Decode service request */ rv = ogs_nas_emm_decode(&message, pkbuf); @@ -321,7 +321,7 @@ static void ogs_nas_eps_message_test8(abts_case *tc, void *data) pkbuf = ogs_nas_eps_plain_encode(&message); ABTS_INT_EQUAL(tc, sizeof(buffer), pkbuf->len); - ABTS_TRUE(tc, memcmp(OGS_HEX(payload, strlen(payload), buffer), + ABTS_TRUE(tc, memcmp(ogs_hex_from_string(payload, buffer, sizeof(buffer)), pkbuf->data, pkbuf->len) == 0); ogs_pkbuf_free(pkbuf); diff --git a/tests/unit/ngap-message-test.c b/tests/unit/ngap-message-test.c index 20179751d..d3f6027e7 100644 --- a/tests/unit/ngap-message-test.c +++ b/tests/unit/ngap-message-test.c @@ -99,7 +99,7 @@ static void ngap_message_test2(abts_case *tc, void *data) pkbuf = ogs_pkbuf_alloc(NULL, OGS_MAX_SDU_LEN); ogs_assert(pkbuf); ogs_pkbuf_put_data(pkbuf, - OGS_HEX(payload, strlen(payload), hexbuf), 23); + ogs_hex_from_string(payload, hexbuf, sizeof(hexbuf)), 23); struct_ptr = &message; struct_size = sizeof(ogs_ngap_message_t); @@ -134,7 +134,7 @@ static void ngap_message_test3(abts_case *tc, void *data) pkbuf = ogs_pkbuf_alloc(NULL, OGS_MAX_SDU_LEN); ogs_assert(pkbuf); ogs_pkbuf_put_data(pkbuf, - OGS_HEX(payload, strlen(payload), hexbuf), 70); + ogs_hex_from_string(payload, hexbuf, sizeof(hexbuf)), 70); struct_ptr = &message; struct_size = sizeof(ogs_ngap_message_t); @@ -169,7 +169,7 @@ static void ngap_message_test4(abts_case *tc, void *data) pkbuf = ogs_pkbuf_alloc(NULL, OGS_MAX_SDU_LEN); ogs_assert(pkbuf); ogs_pkbuf_put_data(pkbuf, - OGS_HEX(payload, strlen(payload), hexbuf), 67); + ogs_hex_from_string(payload, hexbuf, sizeof(hexbuf)), 67); struct_ptr = &message; struct_size = sizeof(ogs_ngap_message_t); diff --git a/tests/unit/s1ap-message-test.c b/tests/unit/s1ap-message-test.c index 391ca7de8..47c1e179b 100644 --- a/tests/unit/s1ap-message-test.c +++ b/tests/unit/s1ap-message-test.c @@ -37,7 +37,7 @@ static void s1ap_message_test1(abts_case *tc, void *data) pkbuf = ogs_pkbuf_alloc(NULL, OGS_MAX_SDU_LEN); ogs_assert(pkbuf); ogs_pkbuf_put_data(pkbuf, - OGS_HEX(payload, strlen(payload), hexbuf), 49); + ogs_hex_from_string(payload, hexbuf, sizeof(hexbuf)), 49); result = ogs_s1ap_decode(&message, pkbuf); ABTS_INT_EQUAL(tc, 0, result); @@ -67,7 +67,7 @@ static void s1ap_message_test2(abts_case *tc, void *data) pkbuf = ogs_pkbuf_alloc(NULL, OGS_MAX_SDU_LEN); ogs_assert(pkbuf); ogs_pkbuf_put_data(pkbuf, - OGS_HEX(payload, strlen(payload), hexbuf), 115); + ogs_hex_from_string(payload, hexbuf, sizeof(hexbuf)), 115); result = ogs_s1ap_decode(&message, pkbuf); ABTS_INT_EQUAL(tc, 0, result); @@ -92,7 +92,7 @@ static void s1ap_message_test3(abts_case *tc, void *data) pkbuf = ogs_pkbuf_alloc(NULL, OGS_MAX_SDU_LEN); ogs_assert(pkbuf); ogs_pkbuf_put_data(pkbuf, - OGS_HEX(payload, strlen(payload), hexbuf), 41); + ogs_hex_from_string(payload, hexbuf, sizeof(hexbuf)), 41); result = ogs_s1ap_decode(&message, pkbuf); ABTS_INT_EQUAL(tc, 0, result); @@ -118,7 +118,7 @@ static void s1ap_message_test4(abts_case *tc, void *data) pkbuf = ogs_pkbuf_alloc(NULL, OGS_MAX_SDU_LEN); ogs_assert(pkbuf); ogs_pkbuf_put_data(pkbuf, - OGS_HEX(payload, strlen(payload), hexbuf), 128); + ogs_hex_from_string(payload, hexbuf, sizeof(hexbuf)), 128); result = ogs_s1ap_decode(&message, pkbuf); ABTS_INT_EQUAL(tc, 0, result); @@ -143,7 +143,7 @@ static void s1ap_message_test5(abts_case *tc, void *data) pkbuf = ogs_pkbuf_alloc(NULL, OGS_MAX_SDU_LEN); ogs_assert(pkbuf); ogs_pkbuf_put_data(pkbuf, - OGS_HEX(payload, strlen(payload), hexbuf), 78); + ogs_hex_from_string(payload, hexbuf, sizeof(hexbuf)), 78); result = ogs_s1ap_decode(&message, pkbuf); ABTS_INT_EQUAL(tc, 0, result); @@ -168,7 +168,7 @@ static void s1ap_message_test6(abts_case *tc, void *data) pkbuf = ogs_pkbuf_alloc(NULL, OGS_MAX_SDU_LEN); ogs_assert(pkbuf); ogs_pkbuf_put_data(pkbuf, - OGS_HEX(payload, strlen(payload), hexbuf), 78); + ogs_hex_from_string(payload, hexbuf, sizeof(hexbuf)), 78); result = ogs_s1ap_decode(&message, pkbuf); ABTS_INT_EQUAL(tc, 0, result); @@ -193,7 +193,7 @@ static void s1ap_message_test7(abts_case *tc, void *data) pkbuf = ogs_pkbuf_alloc(NULL, OGS_MAX_SDU_LEN); ogs_assert(pkbuf); ogs_pkbuf_put_data(pkbuf, - OGS_HEX(payload, strlen(payload), hexbuf), 78); + ogs_hex_from_string(payload, hexbuf, sizeof(hexbuf)), 78); result = ogs_s1ap_decode(&message, pkbuf); ABTS_INT_EQUAL(tc, 0, result); @@ -363,7 +363,7 @@ static void s1ap_message_test8(abts_case *tc, void *data) SecurityKey->size = 32; SecurityKey->buf = CALLOC(SecurityKey->size, sizeof(uint8_t)); SecurityKey->bits_unused = 0; - OGS_HEX(kenb_string, strlen(kenb_string), kenb); + ogs_hex_from_string(kenb_string, kenb, sizeof(kenb)); memcpy(SecurityKey->buf, kenb, SecurityKey->size); } @@ -520,7 +520,7 @@ static void s1ap_message_test8(abts_case *tc, void *data) UERadioCapability = &ie->value.choice.UERadioCapability; ogs_assert(UERadioCapability); - OGS_HEX(radio_string, strlen(radio_string), radio); + ogs_hex_from_string(radio_string, radio, sizeof(radio)); ogs_s1ap_buffer_to_OCTET_STRING( radio, TEST8_RADIO_SIZE, UERadioCapability); } @@ -543,7 +543,7 @@ static void s1ap_message_test8(abts_case *tc, void *data) Masked_IMEISV->size = 8; Masked_IMEISV->buf = CALLOC(Masked_IMEISV->size, sizeof(uint8_t)); Masked_IMEISV->bits_unused = 0; - OGS_HEX(masked_string, strlen(masked_string), masked); + ogs_hex_from_string(masked_string, masked, sizeof(masked)); memcpy(Masked_IMEISV->buf, masked, Masked_IMEISV->size); } { @@ -739,7 +739,7 @@ static void s1ap_message_test9(abts_case *tc, void *data) SecurityKey->size = 32; SecurityKey->buf = CALLOC(SecurityKey->size, sizeof(uint8_t)); SecurityKey->bits_unused = 0; - OGS_HEX(kenb_string, strlen(kenb_string), kenb); + ogs_hex_from_string(kenb_string, kenb, sizeof(kenb)); memcpy(SecurityKey->buf, kenb, SecurityKey->size); } @@ -896,7 +896,7 @@ static void s1ap_message_test9(abts_case *tc, void *data) UERadioCapability = &ie->value.choice.UERadioCapability; ogs_assert(UERadioCapability); - OGS_HEX(radio_string, strlen(radio_string), radio); + ogs_hex_from_string(radio_string, radio, sizeof(radio)); ogs_s1ap_buffer_to_OCTET_STRING( radio, TEST9_RADIO_SIZE, UERadioCapability); } @@ -919,7 +919,7 @@ static void s1ap_message_test9(abts_case *tc, void *data) Masked_IMEISV->size = 8; Masked_IMEISV->buf = CALLOC(Masked_IMEISV->size, sizeof(uint8_t)); Masked_IMEISV->bits_unused = 0; - OGS_HEX(masked_string, strlen(masked_string), masked); + ogs_hex_from_string(masked_string, masked, sizeof(masked)); memcpy(Masked_IMEISV->buf, masked, Masked_IMEISV->size); } { @@ -1115,7 +1115,7 @@ static void s1ap_message_test10(abts_case *tc, void *data) SecurityKey->size = 32; SecurityKey->buf = CALLOC(SecurityKey->size, sizeof(uint8_t)); SecurityKey->bits_unused = 0; - OGS_HEX(kenb_string, strlen(kenb_string), kenb); + ogs_hex_from_string(kenb_string, kenb, sizeof(kenb)); memcpy(SecurityKey->buf, kenb, SecurityKey->size); } @@ -1272,7 +1272,7 @@ static void s1ap_message_test10(abts_case *tc, void *data) UERadioCapability = &ie->value.choice.UERadioCapability; ogs_assert(UERadioCapability); - OGS_HEX(radio_string, strlen(radio_string), radio); + ogs_hex_from_string(radio_string, radio, sizeof(radio)); ogs_s1ap_buffer_to_OCTET_STRING( radio, TEST10_RADIO_SIZE, UERadioCapability); } @@ -1295,7 +1295,7 @@ static void s1ap_message_test10(abts_case *tc, void *data) Masked_IMEISV->size = 8; Masked_IMEISV->buf = CALLOC(Masked_IMEISV->size, sizeof(uint8_t)); Masked_IMEISV->bits_unused = 0; - OGS_HEX(masked_string, strlen(masked_string), masked); + ogs_hex_from_string(masked_string, masked, sizeof(masked)); memcpy(Masked_IMEISV->buf, masked, Masked_IMEISV->size); } { diff --git a/tests/unit/security-test.c b/tests/unit/security-test.c index 937e59a47..25a38b63d 100644 --- a/tests/unit/security-test.c +++ b/tests/unit/security-test.c @@ -52,27 +52,32 @@ static void security_test1(abts_case *tc, void *data) uint8_t tmp[16]; - milenage_opc(OGS_HEX(_k, strlen(_k), k), - OGS_HEX(_op, strlen(_op), op), opc); - ABTS_TRUE(tc, memcmp(opc, OGS_HEX(_opc, strlen(_opc), tmp), 16) == 0); + milenage_opc(ogs_hex_from_string(_k, k, sizeof(k)), + ogs_hex_from_string(_op, op, sizeof(op)), opc); + ABTS_TRUE(tc, memcmp(opc, + ogs_hex_from_string(_opc, tmp, sizeof(tmp)), 16) == 0); milenage_f1(opc, k, - OGS_HEX(_rand, strlen(_rand), rand), - OGS_HEX(_sqn, strlen(_sqn), sqn), - OGS_HEX(_amf, strlen(_amf), amf), + ogs_hex_from_string(_rand, rand, sizeof(rand)), + ogs_hex_from_string(_sqn, sqn, sizeof(sqn)), + ogs_hex_from_string(_amf, amf, sizeof(amf)), mac_a, mac_s); ABTS_TRUE(tc, memcmp(mac_a, - OGS_HEX(_mac_a, strlen(_mac_a), tmp), 8) == 0); + ogs_hex_from_string(_mac_a, tmp, sizeof(tmp)), 8) == 0); ABTS_TRUE(tc, memcmp(mac_s, - OGS_HEX(_mac_s, strlen(_mac_s), tmp), 8) == 0); + ogs_hex_from_string(_mac_s, tmp, sizeof(tmp)), 8) == 0); milenage_f2345(opc, k, rand, res, ck, ik, ak, akstar); - ABTS_TRUE(tc, memcmp(res, OGS_HEX(_res, strlen(_res), tmp), 8) == 0); - ABTS_TRUE(tc, memcmp(ck, OGS_HEX(_ck, strlen(_ck), tmp), 16) == 0); - ABTS_TRUE(tc, memcmp(ik, OGS_HEX(_ik, strlen(_ik), tmp), 16) == 0); - ABTS_TRUE(tc, memcmp(ak, OGS_HEX(_ak, strlen(_ak), tmp), 6) == 0); + ABTS_TRUE(tc, memcmp(res, + ogs_hex_from_string(_res, tmp, sizeof(tmp)), 8) == 0); + ABTS_TRUE(tc, memcmp(ck, + ogs_hex_from_string(_ck, tmp, sizeof(tmp)), 16) == 0); + ABTS_TRUE(tc, memcmp(ik, + ogs_hex_from_string(_ik, tmp, sizeof(tmp)), 16) == 0); + ABTS_TRUE(tc, memcmp(ak, + ogs_hex_from_string(_ak, tmp, sizeof(tmp)), 6) == 0); ABTS_TRUE(tc, memcmp(akstar, - OGS_HEX(_akstar, strlen(_akstar), tmp), 6) == 0); + ogs_hex_from_string(_akstar, tmp, sizeof(tmp)), 6) == 0); } static void security_test2(abts_case *tc, void *data) @@ -86,11 +91,12 @@ static void security_test2(abts_case *tc, void *data) uint8_t hmac[32]; uint8_t tmp[32]; - ogs_hmac_sha256(OGS_HEX(_key, strlen(_key), key), 4, - OGS_HEX(_message, strlen(_message), message), 28, + ogs_hmac_sha256(ogs_hex_from_string(_key, key, sizeof(key)), 4, + ogs_hex_from_string(_message, message, sizeof(message)), 28, hmac, 32); - ABTS_TRUE(tc, memcmp(hmac, OGS_HEX(_hmac, strlen(_hmac), tmp), 32) == 0); + ABTS_TRUE(tc, memcmp(hmac, + ogs_hex_from_string(_hmac, tmp, sizeof(tmp)), 32) == 0); } static void security_test3(abts_case *tc, void *data) @@ -111,15 +117,15 @@ static void security_test3(abts_case *tc, void *data) uint8_t tmp[32]; ogs_auc_kasme( - OGS_HEX(_ck, strlen(_ck), ck), - OGS_HEX(_ik, strlen(_ik), ik), - OGS_HEX(_plmn_id, strlen(_plmn_id), plmn_id), - OGS_HEX(_sqn, strlen(_sqn), sqn), - OGS_HEX(_ak, strlen(_ak), ak), + ogs_hex_from_string(_ck, ck, sizeof(ck)), + ogs_hex_from_string(_ik, ik, sizeof(ik)), + ogs_hex_from_string(_plmn_id, plmn_id, sizeof(plmn_id)), + ogs_hex_from_string(_sqn, sqn, sizeof(sqn)), + ogs_hex_from_string(_ak, ak, sizeof(ak)), kasme); ABTS_TRUE(tc, - memcmp(kasme, OGS_HEX(_kasme, strlen(_kasme), tmp), 32) == 0); + memcmp(kasme, ogs_hex_from_string(_kasme, tmp, sizeof(tmp)), 32) == 0); } static void security_test4(abts_case *tc, void *data) @@ -136,11 +142,12 @@ static void security_test4(abts_case *tc, void *data) uint8_t mac[4]; ogs_pkbuf_t *pkbuf = NULL; - snow_3g_f9(OGS_HEX(_ik, strlen(_ik), ik), + snow_3g_f9(ogs_hex_from_string(_ik, ik, sizeof(ik)), 0x38a6f056, ((int64_t)0x1f << 27), 0, - OGS_HEX(_message, strlen(_message), message), + ogs_hex_from_string(_message, message, sizeof(message)), SECURITY_TEST4_BIT_LEN, mact); - ABTS_TRUE(tc, memcmp(mact, OGS_HEX(_mact, strlen(_mact), tmp), 4) == 0); + ABTS_TRUE(tc, memcmp(mact, + ogs_hex_from_string(_mact, tmp, sizeof(tmp)), 4) == 0); pkbuf = ogs_pkbuf_alloc(NULL, OGS_NAS_HEADROOM+SECURITY_TEST4_LEN); ogs_assert(pkbuf); @@ -180,12 +187,12 @@ static void security_test5(abts_case *tc, void *data) ogs_pkbuf_t *pkbuf = NULL; snow_3g_f8( - OGS_HEX(_ck, strlen(_ck), ck), + ogs_hex_from_string(_ck, ck, sizeof(ck)), 0x72a4f20f, 0x0c, 1, - OGS_HEX(_plain, strlen(_plain), plain), + ogs_hex_from_string(_plain, plain, sizeof(plain)), SECURITY_TEST5_BIT_LEN); ABTS_TRUE(tc, memcmp(plain, - OGS_HEX(_cipher, strlen(_cipher), tmp), + ogs_hex_from_string(_cipher, tmp, sizeof(tmp)), SECURITY_TEST5_LEN) == 0); pkbuf = ogs_pkbuf_alloc(NULL, OGS_NAS_HEADROOM+SECURITY_TEST5_LEN); @@ -195,7 +202,8 @@ static void security_test5(abts_case *tc, void *data) ogs_nas_encrypt(OGS_NAS_SECURITY_ALGORITHMS_128_EEA1, ck, 0x72a4f20f, 0x0c, 1, pkbuf); - ABTS_TRUE(tc, memcmp(pkbuf->data, OGS_HEX(_plain, strlen(_plain), tmp), + ABTS_TRUE(tc, memcmp(pkbuf->data, + ogs_hex_from_string(_plain, tmp, sizeof(tmp)), SECURITY_TEST5_LEN) == 0); ogs_pkbuf_free(pkbuf); } @@ -222,13 +230,16 @@ static void security_test6(abts_case *tc, void *data) ABTS_PTR_NOTNULL(tc, m); memcpy(m, &count, sizeof(uint32_t)); m[4] = ((0x1a << 3) | (1 << 2)); - memcpy(m+8, OGS_HEX(_message, strlen(_message), message), msg_len); + memcpy(m+8, + ogs_hex_from_string(_message, message, sizeof(message)), msg_len); - ogs_aes_cmac_calculate(mact, OGS_HEX(_ik, strlen(_ik), ik), m, m_len); + ogs_aes_cmac_calculate(mact, + ogs_hex_from_string(_ik, ik, sizeof(ik)), m, m_len); ogs_free(m); - ABTS_TRUE(tc, memcmp(mact, OGS_HEX(_mact, strlen(_mact), tmp), 4) == 0); + ABTS_TRUE(tc, memcmp(mact, + ogs_hex_from_string(_mact, tmp, sizeof(tmp)), 4) == 0); pkbuf = ogs_pkbuf_alloc(NULL, OGS_NAS_HEADROOM+SECURITY_TEST6_LEN); ogs_assert(pkbuf); @@ -269,18 +280,19 @@ static void security_test7(abts_case *tc, void *data) ivec[4] = (0x0c << 3) | (1 << 2); ogs_aes_ctr128_encrypt( - OGS_HEX(_ck, strlen(_ck), ck), ivec, - OGS_HEX(_plain, strlen(_plain), plain), SECURITY_TEST7_LEN, + ogs_hex_from_string(_ck, ck, sizeof(ck)), ivec, + ogs_hex_from_string(_plain, plain, sizeof(plain)), SECURITY_TEST7_LEN, cipher); - ABTS_TRUE(tc, memcmp(cipher, OGS_HEX(_cipher, strlen(_cipher), tmp), - SECURITY_TEST7_LEN) == 0); + ABTS_TRUE(tc, + memcmp(cipher, ogs_hex_from_string(_cipher, tmp, sizeof(tmp)), + SECURITY_TEST7_LEN) == 0); memset(ivec, 0, sizeof(ivec)); memcpy(ivec+0, &count, sizeof(count)); ivec[4] = (0x0c << 3) | (1 << 2); - ogs_aes_ctr128_encrypt(OGS_HEX(_ck, strlen(_ck), ck), + ogs_aes_ctr128_encrypt(ogs_hex_from_string(_ck, ck, sizeof(ck)), ivec, cipher, SECURITY_TEST7_LEN, cipher); ABTS_TRUE(tc, memcmp(cipher, plain, SECURITY_TEST7_LEN) == 0); @@ -292,8 +304,9 @@ static void security_test7(abts_case *tc, void *data) ogs_nas_encrypt(OGS_NAS_SECURITY_ALGORITHMS_128_EEA2, ck, 0xc675a64b, 0x0c, 1, pkbuf); - ABTS_TRUE(tc, memcmp(pkbuf->data, - OGS_HEX(_cipher, strlen(_cipher), tmp), SECURITY_TEST7_LEN) == 0); + ABTS_TRUE(tc, + memcmp(pkbuf->data, ogs_hex_from_string(_cipher, tmp, sizeof(tmp)), + SECURITY_TEST7_LEN) == 0); ogs_pkbuf_free(pkbuf); } @@ -315,17 +328,17 @@ static void security_test8(abts_case *tc, void *data) uint8_t mac[4]; zuc_eia3( - OGS_HEX(_ik, strlen(_ik), ik), + ogs_hex_from_string(_ik, ik, sizeof(ik)), 0xa94059da, 0xa, 1, SECURITY_TEST8_BIT_LEN, - OGS_HEX(_message, strlen(_message), message), + ogs_hex_from_string(_message, message, sizeof(message)), &mac32); mac32 = ntohl(mac32); ABTS_TRUE(tc, memcmp(&mac32, - OGS_HEX(_mact, strlen(_mact), mact), 4) == 0); + ogs_hex_from_string(_mact, mact, sizeof(mact)), 4) == 0); pkbuf = ogs_pkbuf_alloc(NULL, OGS_NAS_HEADROOM+SECURITY_TEST8_LEN); ogs_assert(pkbuf); @@ -353,23 +366,23 @@ static void security_test9(abts_case *tc, void *data) uint8_t tmp[SECURITY_TEST9_LEN]; ogs_pkbuf_t *pkbuf = NULL; - OGS_HEX(_plain, strlen(_plain), plain); + ogs_hex_from_string(_plain, plain, sizeof(plain)); zuc_eea3( - OGS_HEX(_ck, strlen(_ck), ck), + ogs_hex_from_string(_ck, ck, sizeof(ck)), 0x66035492, 0xf, 0, SECURITY_TEST9_BIT_LEN, plain, plain); - ABTS_TRUE(tc, memcmp(plain, OGS_HEX(_cipher, strlen(_cipher), tmp), + ABTS_TRUE(tc, memcmp(plain, ogs_hex_from_string(_cipher, tmp, sizeof(tmp)), SECURITY_TEST9_LEN) == 0); zuc_eea3( - OGS_HEX(_ck, strlen(_ck), ck), + ogs_hex_from_string(_ck, ck, sizeof(ck)), 0x66035492, 0xf, 0, SECURITY_TEST9_BIT_LEN, - OGS_HEX(_plain, strlen(_plain), plain), + ogs_hex_from_string(_plain, plain, sizeof(plain)), cipher); - ABTS_TRUE(tc, memcmp(cipher, OGS_HEX(_cipher, strlen(_cipher), tmp), + ABTS_TRUE(tc, memcmp(cipher, ogs_hex_from_string(_cipher, tmp, sizeof(tmp)), SECURITY_TEST9_LEN) == 0); pkbuf = ogs_pkbuf_alloc(NULL, OGS_NAS_HEADROOM+SECURITY_TEST9_LEN); @@ -379,8 +392,9 @@ static void security_test9(abts_case *tc, void *data) ogs_nas_encrypt(OGS_NAS_SECURITY_ALGORITHMS_128_EEA3, ck, 0x66035492, 0xf, 0, pkbuf); - ABTS_TRUE(tc, memcmp(pkbuf->data, - OGS_HEX(_cipher, strlen(_cipher), tmp), SECURITY_TEST9_LEN) == 0); + ABTS_TRUE(tc, + memcmp(pkbuf->data, ogs_hex_from_string(_cipher, tmp, sizeof(tmp)), + SECURITY_TEST9_LEN) == 0); ogs_pkbuf_free(pkbuf); } diff --git a/tests/volte/bearer-test.c b/tests/volte/bearer-test.c index 8951dd113..63c5dfff7 100644 --- a/tests/volte/bearer-test.c +++ b/tests/volte/bearer-test.c @@ -46,15 +46,10 @@ static void test1_func(abts_case *tc, void *data) mobile_identity_suci.routing_indicator2 = 0xf; mobile_identity_suci.routing_indicator3 = 0xf; mobile_identity_suci.routing_indicator4 = 0xf; - mobile_identity_suci.protection_scheme_id = OGS_NAS_5GS_NULL_SCHEME; + mobile_identity_suci.protection_scheme_id = OGS_PROTECTION_SCHEME_NULL; mobile_identity_suci.home_network_pki_value = 0; - mobile_identity_suci.scheme_output[0] = 0x10; - mobile_identity_suci.scheme_output[1] = 0x32; - mobile_identity_suci.scheme_output[2] = 0x54; - mobile_identity_suci.scheme_output[3] = 0x86; - mobile_identity_suci.scheme_output[4] = 0x91; - test_ue = test_ue_add_by_suci(&mobile_identity_suci, 13); + test_ue = test_ue_add_by_suci(&mobile_identity_suci, "1032548691"); ogs_assert(test_ue); test_ue->e_cgi.cell_id = 0x1079baf; diff --git a/tests/volte/cx-test.c b/tests/volte/cx-test.c index 04dbedbae..1f5d72933 100644 --- a/tests/volte/cx-test.c +++ b/tests/volte/cx-test.c @@ -52,15 +52,10 @@ static void test1_func(abts_case *tc, void *data) mobile_identity_suci.routing_indicator2 = 0xf; mobile_identity_suci.routing_indicator3 = 0xf; mobile_identity_suci.routing_indicator4 = 0xf; - mobile_identity_suci.protection_scheme_id = OGS_NAS_5GS_NULL_SCHEME; + mobile_identity_suci.protection_scheme_id = OGS_PROTECTION_SCHEME_NULL; mobile_identity_suci.home_network_pki_value = 0; - mobile_identity_suci.scheme_output[0] = 0x10; - mobile_identity_suci.scheme_output[1] = 0x32; - mobile_identity_suci.scheme_output[2] = 0x54; - mobile_identity_suci.scheme_output[3] = 0x86; - mobile_identity_suci.scheme_output[4] = 0x91; - test_ue = test_ue_add_by_suci(&mobile_identity_suci, 13); + test_ue = test_ue_add_by_suci(&mobile_identity_suci, "1032548691"); ogs_assert(test_ue); test_ue->e_cgi.cell_id = 0x1079baf; diff --git a/tests/volte/diameter-cx-path.c b/tests/volte/diameter-cx-path.c index 964813f63..60a82350f 100644 --- a/tests/volte/diameter-cx-path.c +++ b/tests/volte/diameter-cx-path.c @@ -434,8 +434,10 @@ static void test_cx_send_mar(struct sess_state *sess_data) uint64_t sqn_ms; int i; - OGS_HEX(test_ue->k_string, strlen(test_ue->k_string), test_ue->k); - OGS_HEX(test_ue->opc_string, strlen(test_ue->opc_string), test_ue->opc); + ogs_hex_from_string( + test_ue->k_string, test_ue->k, sizeof(test_ue->k)); + ogs_hex_from_string( + test_ue->opc_string, test_ue->opc, sizeof(test_ue->opc)); milenage_f2345(test_ue->opc, test_ue->k, test_ue->rand, NULL, NULL, NULL, NULL, ak); diff --git a/tests/volte/rx-test.c b/tests/volte/rx-test.c index 4fb12971f..2bfe1a138 100644 --- a/tests/volte/rx-test.c +++ b/tests/volte/rx-test.c @@ -52,15 +52,10 @@ static void test1_func(abts_case *tc, void *data) mobile_identity_suci.routing_indicator2 = 0xf; mobile_identity_suci.routing_indicator3 = 0xf; mobile_identity_suci.routing_indicator4 = 0xf; - mobile_identity_suci.protection_scheme_id = OGS_NAS_5GS_NULL_SCHEME; + mobile_identity_suci.protection_scheme_id = OGS_PROTECTION_SCHEME_NULL; mobile_identity_suci.home_network_pki_value = 0; - mobile_identity_suci.scheme_output[0] = 0x10; - mobile_identity_suci.scheme_output[1] = 0x32; - mobile_identity_suci.scheme_output[2] = 0x54; - mobile_identity_suci.scheme_output[3] = 0x86; - mobile_identity_suci.scheme_output[4] = 0x91; - test_ue = test_ue_add_by_suci(&mobile_identity_suci, 13); + test_ue = test_ue_add_by_suci(&mobile_identity_suci, "1032548691"); ogs_assert(test_ue); test_ue->e_cgi.cell_id = 0x1079baf; @@ -567,15 +562,10 @@ static void test2_func(abts_case *tc, void *data) mobile_identity_suci.routing_indicator2 = 0xf; mobile_identity_suci.routing_indicator3 = 0xf; mobile_identity_suci.routing_indicator4 = 0xf; - mobile_identity_suci.protection_scheme_id = OGS_NAS_5GS_NULL_SCHEME; + mobile_identity_suci.protection_scheme_id = OGS_PROTECTION_SCHEME_NULL; mobile_identity_suci.home_network_pki_value = 0; - mobile_identity_suci.scheme_output[0] = 0x10; - mobile_identity_suci.scheme_output[1] = 0x32; - mobile_identity_suci.scheme_output[2] = 0x54; - mobile_identity_suci.scheme_output[3] = 0x86; - mobile_identity_suci.scheme_output[4] = 0x91; - test_ue = test_ue_add_by_suci(&mobile_identity_suci, 13); + test_ue = test_ue_add_by_suci(&mobile_identity_suci, "1032548691"); ogs_assert(test_ue); test_ue->e_cgi.cell_id = 0x1079baf; @@ -921,15 +911,10 @@ static void test3_func(abts_case *tc, void *data) mobile_identity_suci.routing_indicator2 = 0xf; mobile_identity_suci.routing_indicator3 = 0xf; mobile_identity_suci.routing_indicator4 = 0xf; - mobile_identity_suci.protection_scheme_id = OGS_NAS_5GS_NULL_SCHEME; + mobile_identity_suci.protection_scheme_id = OGS_PROTECTION_SCHEME_NULL; mobile_identity_suci.home_network_pki_value = 0; - mobile_identity_suci.scheme_output[0] = 0x10; - mobile_identity_suci.scheme_output[1] = 0x32; - mobile_identity_suci.scheme_output[2] = 0x54; - mobile_identity_suci.scheme_output[3] = 0x86; - mobile_identity_suci.scheme_output[4] = 0x91; - test_ue = test_ue_add_by_suci(&mobile_identity_suci, 13); + test_ue = test_ue_add_by_suci(&mobile_identity_suci, "1032548691"); ogs_assert(test_ue); test_ue->e_cgi.cell_id = 0x1079baf; @@ -1271,15 +1256,10 @@ static void test4_func(abts_case *tc, void *data) mobile_identity_suci.routing_indicator2 = 0xf; mobile_identity_suci.routing_indicator3 = 0xf; mobile_identity_suci.routing_indicator4 = 0xf; - mobile_identity_suci.protection_scheme_id = OGS_NAS_5GS_NULL_SCHEME; + mobile_identity_suci.protection_scheme_id = OGS_PROTECTION_SCHEME_NULL; mobile_identity_suci.home_network_pki_value = 0; - mobile_identity_suci.scheme_output[0] = 0x10; - mobile_identity_suci.scheme_output[1] = 0x32; - mobile_identity_suci.scheme_output[2] = 0x54; - mobile_identity_suci.scheme_output[3] = 0x86; - mobile_identity_suci.scheme_output[4] = 0x91; - test_ue = test_ue_add_by_suci(&mobile_identity_suci, 13); + test_ue = test_ue_add_by_suci(&mobile_identity_suci, "1032548691"); ogs_assert(test_ue); test_ue->e_cgi.cell_id = 0x1079baf; @@ -1800,15 +1780,10 @@ static void test5_func(abts_case *tc, void *data) mobile_identity_suci.routing_indicator2 = 0xf; mobile_identity_suci.routing_indicator3 = 0xf; mobile_identity_suci.routing_indicator4 = 0xf; - mobile_identity_suci.protection_scheme_id = OGS_NAS_5GS_NULL_SCHEME; + mobile_identity_suci.protection_scheme_id = OGS_PROTECTION_SCHEME_NULL; mobile_identity_suci.home_network_pki_value = 0; - mobile_identity_suci.scheme_output[0] = 0x10; - mobile_identity_suci.scheme_output[1] = 0x32; - mobile_identity_suci.scheme_output[2] = 0x54; - mobile_identity_suci.scheme_output[3] = 0x86; - mobile_identity_suci.scheme_output[4] = 0x91; - test_ue = test_ue_add_by_suci(&mobile_identity_suci, 13); + test_ue = test_ue_add_by_suci(&mobile_identity_suci, "1032548691"); ogs_assert(test_ue); test_ue->e_cgi.cell_id = 0x1079baf; @@ -2306,15 +2281,10 @@ static void test6_func(abts_case *tc, void *data) mobile_identity_suci.routing_indicator2 = 0xf; mobile_identity_suci.routing_indicator3 = 0xf; mobile_identity_suci.routing_indicator4 = 0xf; - mobile_identity_suci.protection_scheme_id = OGS_NAS_5GS_NULL_SCHEME; + mobile_identity_suci.protection_scheme_id = OGS_PROTECTION_SCHEME_NULL; mobile_identity_suci.home_network_pki_value = 0; - mobile_identity_suci.scheme_output[0] = 0x10; - mobile_identity_suci.scheme_output[1] = 0x32; - mobile_identity_suci.scheme_output[2] = 0x54; - mobile_identity_suci.scheme_output[3] = 0x86; - mobile_identity_suci.scheme_output[4] = 0x91; - test_ue = test_ue_add_by_suci(&mobile_identity_suci, 13); + test_ue = test_ue_add_by_suci(&mobile_identity_suci, "1032548691"); ogs_assert(test_ue); test_ue->e_cgi.cell_id = 0x1079baf; @@ -2731,15 +2701,10 @@ static void test7_func(abts_case *tc, void *data) mobile_identity_suci.routing_indicator2 = 0xf; mobile_identity_suci.routing_indicator3 = 0xf; mobile_identity_suci.routing_indicator4 = 0xf; - mobile_identity_suci.protection_scheme_id = OGS_NAS_5GS_NULL_SCHEME; + mobile_identity_suci.protection_scheme_id = OGS_PROTECTION_SCHEME_NULL; mobile_identity_suci.home_network_pki_value = 0; - mobile_identity_suci.scheme_output[0] = 0x10; - mobile_identity_suci.scheme_output[1] = 0x32; - mobile_identity_suci.scheme_output[2] = 0x54; - mobile_identity_suci.scheme_output[3] = 0x86; - mobile_identity_suci.scheme_output[4] = 0x91; - test_ue = test_ue_add_by_suci(&mobile_identity_suci, 13); + test_ue = test_ue_add_by_suci(&mobile_identity_suci, "1032548691"); ogs_assert(test_ue); test_ue->e_cgi.cell_id = 0x1079baf; diff --git a/tests/volte/session-test.c b/tests/volte/session-test.c index 8ac526811..c7e089798 100644 --- a/tests/volte/session-test.c +++ b/tests/volte/session-test.c @@ -46,15 +46,10 @@ static void test1_func(abts_case *tc, void *data) mobile_identity_suci.routing_indicator2 = 0xf; mobile_identity_suci.routing_indicator3 = 0xf; mobile_identity_suci.routing_indicator4 = 0xf; - mobile_identity_suci.protection_scheme_id = OGS_NAS_5GS_NULL_SCHEME; + mobile_identity_suci.protection_scheme_id = OGS_PROTECTION_SCHEME_NULL; mobile_identity_suci.home_network_pki_value = 0; - mobile_identity_suci.scheme_output[0] = 0x10; - mobile_identity_suci.scheme_output[1] = 0x32; - mobile_identity_suci.scheme_output[2] = 0x54; - mobile_identity_suci.scheme_output[3] = 0x86; - mobile_identity_suci.scheme_output[4] = 0x91; - test_ue = test_ue_add_by_suci(&mobile_identity_suci, 13); + test_ue = test_ue_add_by_suci(&mobile_identity_suci, "1032548691"); ogs_assert(test_ue); test_ue->e_cgi.cell_id = 0x1079baf; diff --git a/tests/volte/simple-test.c b/tests/volte/simple-test.c index e51298c74..9e7fd9ce7 100644 --- a/tests/volte/simple-test.c +++ b/tests/volte/simple-test.c @@ -52,15 +52,10 @@ static void test1_func(abts_case *tc, void *data) mobile_identity_suci.routing_indicator2 = 0xf; mobile_identity_suci.routing_indicator3 = 0xf; mobile_identity_suci.routing_indicator4 = 0xf; - mobile_identity_suci.protection_scheme_id = OGS_NAS_5GS_NULL_SCHEME; + mobile_identity_suci.protection_scheme_id = OGS_PROTECTION_SCHEME_NULL; mobile_identity_suci.home_network_pki_value = 0; - mobile_identity_suci.scheme_output[0] = 0x10; - mobile_identity_suci.scheme_output[1] = 0x32; - mobile_identity_suci.scheme_output[2] = 0x54; - mobile_identity_suci.scheme_output[3] = 0x86; - mobile_identity_suci.scheme_output[4] = 0x91; - test_ue = test_ue_add_by_suci(&mobile_identity_suci, 13); + test_ue = test_ue_add_by_suci(&mobile_identity_suci, "1032548691"); ogs_assert(test_ue); test_ue->e_cgi.cell_id = 0x1079baf; diff --git a/tests/volte/video-test.c b/tests/volte/video-test.c index 333a9bfb5..4ae7be6ee 100644 --- a/tests/volte/video-test.c +++ b/tests/volte/video-test.c @@ -50,15 +50,10 @@ static void test1_func(abts_case *tc, void *data) mobile_identity_suci.routing_indicator2 = 0xf; mobile_identity_suci.routing_indicator3 = 0xf; mobile_identity_suci.routing_indicator4 = 0xf; - mobile_identity_suci.protection_scheme_id = OGS_NAS_5GS_NULL_SCHEME; + mobile_identity_suci.protection_scheme_id = OGS_PROTECTION_SCHEME_NULL; mobile_identity_suci.home_network_pki_value = 0; - mobile_identity_suci.scheme_output[0] = 0x10; - mobile_identity_suci.scheme_output[1] = 0x32; - mobile_identity_suci.scheme_output[2] = 0x54; - mobile_identity_suci.scheme_output[3] = 0x86; - mobile_identity_suci.scheme_output[4] = 0x91; - test_ue = test_ue_add_by_suci(&mobile_identity_suci, 13); + test_ue = test_ue_add_by_suci(&mobile_identity_suci, "1032548691"); ogs_assert(test_ue); test_ue->e_cgi.cell_id = 0x1079baf; diff --git a/tests/vonr/af-test.c b/tests/vonr/af-test.c index 93fca9d59..305253515 100644 --- a/tests/vonr/af-test.c +++ b/tests/vonr/af-test.c @@ -57,15 +57,10 @@ static void test1_func(abts_case *tc, void *data) mobile_identity_suci.routing_indicator2 = 0xf; mobile_identity_suci.routing_indicator3 = 0xf; mobile_identity_suci.routing_indicator4 = 0xf; - mobile_identity_suci.protection_scheme_id = OGS_NAS_5GS_NULL_SCHEME; + mobile_identity_suci.protection_scheme_id = OGS_PROTECTION_SCHEME_NULL; mobile_identity_suci.home_network_pki_value = 0; - mobile_identity_suci.scheme_output[0] = 0; - mobile_identity_suci.scheme_output[1] = 0; - mobile_identity_suci.scheme_output[2] = 0x20; - mobile_identity_suci.scheme_output[3] = 0x31; - mobile_identity_suci.scheme_output[4] = 0x90; - test_ue = test_ue_add_by_suci(&mobile_identity_suci, 13); + test_ue = test_ue_add_by_suci(&mobile_identity_suci, "0000203190"); ogs_assert(test_ue); test_ue->nr_cgi.cell_id = 0x40001; @@ -742,15 +737,10 @@ static void test2_func(abts_case *tc, void *data) mobile_identity_suci.routing_indicator2 = 0xf; mobile_identity_suci.routing_indicator3 = 0xf; mobile_identity_suci.routing_indicator4 = 0xf; - mobile_identity_suci.protection_scheme_id = OGS_NAS_5GS_NULL_SCHEME; + mobile_identity_suci.protection_scheme_id = OGS_PROTECTION_SCHEME_NULL; mobile_identity_suci.home_network_pki_value = 0; - mobile_identity_suci.scheme_output[0] = 0; - mobile_identity_suci.scheme_output[1] = 0; - mobile_identity_suci.scheme_output[2] = 0x20; - mobile_identity_suci.scheme_output[3] = 0x31; - mobile_identity_suci.scheme_output[4] = 0x90; - test_ue = test_ue_add_by_suci(&mobile_identity_suci, 13); + test_ue = test_ue_add_by_suci(&mobile_identity_suci, "0000203190"); ogs_assert(test_ue); test_ue->nr_cgi.cell_id = 0x40001; @@ -1222,15 +1212,10 @@ static void test3_func(abts_case *tc, void *data) mobile_identity_suci.routing_indicator2 = 0xf; mobile_identity_suci.routing_indicator3 = 0xf; mobile_identity_suci.routing_indicator4 = 0xf; - mobile_identity_suci.protection_scheme_id = OGS_NAS_5GS_NULL_SCHEME; + mobile_identity_suci.protection_scheme_id = OGS_PROTECTION_SCHEME_NULL; mobile_identity_suci.home_network_pki_value = 0; - mobile_identity_suci.scheme_output[0] = 0; - mobile_identity_suci.scheme_output[1] = 0; - mobile_identity_suci.scheme_output[2] = 0x20; - mobile_identity_suci.scheme_output[3] = 0x31; - mobile_identity_suci.scheme_output[4] = 0x90; - test_ue = test_ue_add_by_suci(&mobile_identity_suci, 13); + test_ue = test_ue_add_by_suci(&mobile_identity_suci, "0000203190"); ogs_assert(test_ue); test_ue->nr_cgi.cell_id = 0x40001; @@ -1682,15 +1667,10 @@ static void test4_func(abts_case *tc, void *data) mobile_identity_suci.routing_indicator2 = 0xf; mobile_identity_suci.routing_indicator3 = 0xf; mobile_identity_suci.routing_indicator4 = 0xf; - mobile_identity_suci.protection_scheme_id = OGS_NAS_5GS_NULL_SCHEME; + mobile_identity_suci.protection_scheme_id = OGS_PROTECTION_SCHEME_NULL; mobile_identity_suci.home_network_pki_value = 0; - mobile_identity_suci.scheme_output[0] = 0; - mobile_identity_suci.scheme_output[1] = 0; - mobile_identity_suci.scheme_output[2] = 0x20; - mobile_identity_suci.scheme_output[3] = 0x31; - mobile_identity_suci.scheme_output[4] = 0x90; - test_ue = test_ue_add_by_suci(&mobile_identity_suci, 13); + test_ue = test_ue_add_by_suci(&mobile_identity_suci, "0000203190"); ogs_assert(test_ue); test_ue->nr_cgi.cell_id = 0x40001; @@ -2314,15 +2294,10 @@ static void test5_func(abts_case *tc, void *data) mobile_identity_suci.routing_indicator2 = 0xf; mobile_identity_suci.routing_indicator3 = 0xf; mobile_identity_suci.routing_indicator4 = 0xf; - mobile_identity_suci.protection_scheme_id = OGS_NAS_5GS_NULL_SCHEME; + mobile_identity_suci.protection_scheme_id = OGS_PROTECTION_SCHEME_NULL; mobile_identity_suci.home_network_pki_value = 0; - mobile_identity_suci.scheme_output[0] = 0; - mobile_identity_suci.scheme_output[1] = 0; - mobile_identity_suci.scheme_output[2] = 0x20; - mobile_identity_suci.scheme_output[3] = 0x31; - mobile_identity_suci.scheme_output[4] = 0x90; - test_ue = test_ue_add_by_suci(&mobile_identity_suci, 13); + test_ue = test_ue_add_by_suci(&mobile_identity_suci, "0000203190"); ogs_assert(test_ue); test_ue->nr_cgi.cell_id = 0x40001; @@ -3036,15 +3011,10 @@ static void test6_func(abts_case *tc, void *data) mobile_identity_suci.routing_indicator2 = 0xf; mobile_identity_suci.routing_indicator3 = 0xf; mobile_identity_suci.routing_indicator4 = 0xf; - mobile_identity_suci.protection_scheme_id = OGS_NAS_5GS_NULL_SCHEME; + mobile_identity_suci.protection_scheme_id = OGS_PROTECTION_SCHEME_NULL; mobile_identity_suci.home_network_pki_value = 0; - mobile_identity_suci.scheme_output[0] = 0; - mobile_identity_suci.scheme_output[1] = 0; - mobile_identity_suci.scheme_output[2] = 0x20; - mobile_identity_suci.scheme_output[3] = 0x31; - mobile_identity_suci.scheme_output[4] = 0x90; - test_ue = test_ue_add_by_suci(&mobile_identity_suci, 13); + test_ue = test_ue_add_by_suci(&mobile_identity_suci, "0000203190"); ogs_assert(test_ue); test_ue->nr_cgi.cell_id = 0x40001; @@ -3578,15 +3548,10 @@ static void test7_func(abts_case *tc, void *data) mobile_identity_suci.routing_indicator2 = 0xf; mobile_identity_suci.routing_indicator3 = 0xf; mobile_identity_suci.routing_indicator4 = 0xf; - mobile_identity_suci.protection_scheme_id = OGS_NAS_5GS_NULL_SCHEME; + mobile_identity_suci.protection_scheme_id = OGS_PROTECTION_SCHEME_NULL; mobile_identity_suci.home_network_pki_value = 0; - mobile_identity_suci.scheme_output[0] = 0; - mobile_identity_suci.scheme_output[1] = 0; - mobile_identity_suci.scheme_output[2] = 0x20; - mobile_identity_suci.scheme_output[3] = 0x31; - mobile_identity_suci.scheme_output[4] = 0x90; - test_ue = test_ue_add_by_suci(&mobile_identity_suci, 13); + test_ue = test_ue_add_by_suci(&mobile_identity_suci, "0000203190"); ogs_assert(test_ue); test_ue->nr_cgi.cell_id = 0x40001; diff --git a/tests/vonr/qos-flow-test.c b/tests/vonr/qos-flow-test.c index 7de74a988..13d4fac98 100644 --- a/tests/vonr/qos-flow-test.c +++ b/tests/vonr/qos-flow-test.c @@ -53,15 +53,10 @@ static void test1_func(abts_case *tc, void *data) mobile_identity_suci.routing_indicator2 = 0xf; mobile_identity_suci.routing_indicator3 = 0xf; mobile_identity_suci.routing_indicator4 = 0xf; - mobile_identity_suci.protection_scheme_id = OGS_NAS_5GS_NULL_SCHEME; + mobile_identity_suci.protection_scheme_id = OGS_PROTECTION_SCHEME_NULL; mobile_identity_suci.home_network_pki_value = 0; - mobile_identity_suci.scheme_output[0] = 0; - mobile_identity_suci.scheme_output[1] = 0; - mobile_identity_suci.scheme_output[2] = 0x20; - mobile_identity_suci.scheme_output[3] = 0x31; - mobile_identity_suci.scheme_output[4] = 0x90; - test_ue = test_ue_add_by_suci(&mobile_identity_suci, 13); + test_ue = test_ue_add_by_suci(&mobile_identity_suci, "0000203190"); ogs_assert(test_ue); test_ue->nr_cgi.cell_id = 0x40001; @@ -297,7 +292,7 @@ static void test1_func(abts_case *tc, void *data) ABTS_PTR_NOTNULL(tc, recvbuf); /* For checking qos_flow_identifier == 2 */ ABTS_TRUE(tc, memcmp(recvbuf->data, - OGS_HEX(_gtp_payload, strlen(_gtp_payload), tmp), 20) == 0); + ogs_hex_from_string(_gtp_payload, tmp, sizeof(tmp)), 20) == 0); ogs_pkbuf_free(recvbuf); /* Send UEContextReleaseRequest */ @@ -376,7 +371,7 @@ static void test1_func(abts_case *tc, void *data) ABTS_PTR_NOTNULL(tc, recvbuf); /* For checking qos_flow_identifier == 2 */ ABTS_TRUE(tc, memcmp(recvbuf->data, - OGS_HEX(_gtp_payload, strlen(_gtp_payload), tmp), 20) == 0); + ogs_hex_from_string(_gtp_payload, tmp, sizeof(tmp)), 20) == 0); ogs_pkbuf_free(recvbuf); /* Send De-registration request */ diff --git a/tests/vonr/session-test.c b/tests/vonr/session-test.c index 8ace2b316..81aa4b242 100644 --- a/tests/vonr/session-test.c +++ b/tests/vonr/session-test.c @@ -53,15 +53,10 @@ static void test1_func(abts_case *tc, void *data) mobile_identity_suci.routing_indicator2 = 0xf; mobile_identity_suci.routing_indicator3 = 0xf; mobile_identity_suci.routing_indicator4 = 0xf; - mobile_identity_suci.protection_scheme_id = OGS_NAS_5GS_NULL_SCHEME; + mobile_identity_suci.protection_scheme_id = OGS_PROTECTION_SCHEME_NULL; mobile_identity_suci.home_network_pki_value = 0; - mobile_identity_suci.scheme_output[0] = 0; - mobile_identity_suci.scheme_output[1] = 0; - mobile_identity_suci.scheme_output[2] = 0x20; - mobile_identity_suci.scheme_output[3] = 0x31; - mobile_identity_suci.scheme_output[4] = 0x90; - test_ue = test_ue_add_by_suci(&mobile_identity_suci, 13); + test_ue = test_ue_add_by_suci(&mobile_identity_suci, "0000203190"); ogs_assert(test_ue); test_ue->nr_cgi.cell_id = 0x40001; @@ -537,15 +532,10 @@ static void test2_func(abts_case *tc, void *data) mobile_identity_suci.routing_indicator2 = 0xf; mobile_identity_suci.routing_indicator3 = 0xf; mobile_identity_suci.routing_indicator4 = 0xf; - mobile_identity_suci.protection_scheme_id = OGS_NAS_5GS_NULL_SCHEME; + mobile_identity_suci.protection_scheme_id = OGS_PROTECTION_SCHEME_NULL; mobile_identity_suci.home_network_pki_value = 0; - mobile_identity_suci.scheme_output[0] = 0; - mobile_identity_suci.scheme_output[1] = 0; - mobile_identity_suci.scheme_output[2] = 0x20; - mobile_identity_suci.scheme_output[3] = 0x31; - mobile_identity_suci.scheme_output[4] = 0x90; - test_ue = test_ue_add_by_suci(&mobile_identity_suci, 13); + test_ue = test_ue_add_by_suci(&mobile_identity_suci, "0000203190"); ogs_assert(test_ue); test_ue->nr_cgi.cell_id = 0x40001; @@ -971,15 +961,10 @@ static void test3_func(abts_case *tc, void *data) mobile_identity_suci.routing_indicator2 = 0xf; mobile_identity_suci.routing_indicator3 = 0xf; mobile_identity_suci.routing_indicator4 = 0xf; - mobile_identity_suci.protection_scheme_id = OGS_NAS_5GS_NULL_SCHEME; + mobile_identity_suci.protection_scheme_id = OGS_PROTECTION_SCHEME_NULL; mobile_identity_suci.home_network_pki_value = 0; - mobile_identity_suci.scheme_output[0] = 0; - mobile_identity_suci.scheme_output[1] = 0; - mobile_identity_suci.scheme_output[2] = 0x20; - mobile_identity_suci.scheme_output[3] = 0x31; - mobile_identity_suci.scheme_output[4] = 0x90; - test_ue = test_ue_add_by_suci(&mobile_identity_suci, 13); + test_ue = test_ue_add_by_suci(&mobile_identity_suci, "0000203190"); ogs_assert(test_ue); test_ue->nr_cgi.cell_id = 0x40001; @@ -1391,15 +1376,10 @@ static void test4_func(abts_case *tc, void *data) mobile_identity_suci.routing_indicator2 = 0xf; mobile_identity_suci.routing_indicator3 = 0xf; mobile_identity_suci.routing_indicator4 = 0xf; - mobile_identity_suci.protection_scheme_id = OGS_NAS_5GS_NULL_SCHEME; + mobile_identity_suci.protection_scheme_id = OGS_PROTECTION_SCHEME_NULL; mobile_identity_suci.home_network_pki_value = 0; - mobile_identity_suci.scheme_output[0] = 0; - mobile_identity_suci.scheme_output[1] = 0; - mobile_identity_suci.scheme_output[2] = 0x20; - mobile_identity_suci.scheme_output[3] = 0x31; - mobile_identity_suci.scheme_output[4] = 0x90; - test_ue = test_ue_add_by_suci(&mobile_identity_suci, 13); + test_ue = test_ue_add_by_suci(&mobile_identity_suci, "0000203190"); ogs_assert(test_ue); test_ue->nr_cgi.cell_id = 0x40001; @@ -1739,15 +1719,10 @@ static void test5_func(abts_case *tc, void *data) mobile_identity_suci.routing_indicator2 = 0xf; mobile_identity_suci.routing_indicator3 = 0xf; mobile_identity_suci.routing_indicator4 = 0xf; - mobile_identity_suci.protection_scheme_id = OGS_NAS_5GS_NULL_SCHEME; + mobile_identity_suci.protection_scheme_id = OGS_PROTECTION_SCHEME_NULL; mobile_identity_suci.home_network_pki_value = 0; - mobile_identity_suci.scheme_output[0] = 0; - mobile_identity_suci.scheme_output[1] = 0; - mobile_identity_suci.scheme_output[2] = 0x20; - mobile_identity_suci.scheme_output[3] = 0x31; - mobile_identity_suci.scheme_output[4] = 0x90; - test_ue = test_ue_add_by_suci(&mobile_identity_suci, 13); + test_ue = test_ue_add_by_suci(&mobile_identity_suci, "0000203190"); ogs_assert(test_ue); test_ue->nr_cgi.cell_id = 0x40001; @@ -2022,15 +1997,10 @@ static void test6_func(abts_case *tc, void *data) mobile_identity_suci.routing_indicator2 = 0xf; mobile_identity_suci.routing_indicator3 = 0xf; mobile_identity_suci.routing_indicator4 = 0xf; - mobile_identity_suci.protection_scheme_id = OGS_NAS_5GS_NULL_SCHEME; + mobile_identity_suci.protection_scheme_id = OGS_PROTECTION_SCHEME_NULL; mobile_identity_suci.home_network_pki_value = 0; - mobile_identity_suci.scheme_output[0] = 0; - mobile_identity_suci.scheme_output[1] = 0; - mobile_identity_suci.scheme_output[2] = 0x20; - mobile_identity_suci.scheme_output[3] = 0x31; - mobile_identity_suci.scheme_output[4] = 0x90; - test_ue = test_ue_add_by_suci(&mobile_identity_suci, 13); + test_ue = test_ue_add_by_suci(&mobile_identity_suci, "0000203190"); ogs_assert(test_ue); test_ue->nr_cgi.cell_id = 0x40001; diff --git a/tests/vonr/simple-test.c b/tests/vonr/simple-test.c index 66b55dc2e..2329c5e28 100644 --- a/tests/vonr/simple-test.c +++ b/tests/vonr/simple-test.c @@ -57,15 +57,10 @@ static void test1_func(abts_case *tc, void *data) mobile_identity_suci.routing_indicator2 = 0xf; mobile_identity_suci.routing_indicator3 = 0xf; mobile_identity_suci.routing_indicator4 = 0xf; - mobile_identity_suci.protection_scheme_id = OGS_NAS_5GS_NULL_SCHEME; + mobile_identity_suci.protection_scheme_id = OGS_PROTECTION_SCHEME_NULL; mobile_identity_suci.home_network_pki_value = 0; - mobile_identity_suci.scheme_output[0] = 0; - mobile_identity_suci.scheme_output[1] = 0; - mobile_identity_suci.scheme_output[2] = 0x20; - mobile_identity_suci.scheme_output[3] = 0x31; - mobile_identity_suci.scheme_output[4] = 0x90; - test_ue = test_ue_add_by_suci(&mobile_identity_suci, 13); + test_ue = test_ue_add_by_suci(&mobile_identity_suci, "0000203190"); ogs_assert(test_ue); test_ue->nr_cgi.cell_id = 0x40001; diff --git a/tests/vonr/video-test.c b/tests/vonr/video-test.c index 17cb309e8..8427b1c7b 100644 --- a/tests/vonr/video-test.c +++ b/tests/vonr/video-test.c @@ -57,15 +57,10 @@ static void test1_func(abts_case *tc, void *data) mobile_identity_suci.routing_indicator2 = 0xf; mobile_identity_suci.routing_indicator3 = 0xf; mobile_identity_suci.routing_indicator4 = 0xf; - mobile_identity_suci.protection_scheme_id = OGS_NAS_5GS_NULL_SCHEME; + mobile_identity_suci.protection_scheme_id = OGS_PROTECTION_SCHEME_NULL; mobile_identity_suci.home_network_pki_value = 0; - mobile_identity_suci.scheme_output[0] = 0; - mobile_identity_suci.scheme_output[1] = 0; - mobile_identity_suci.scheme_output[2] = 0x20; - mobile_identity_suci.scheme_output[3] = 0x31; - mobile_identity_suci.scheme_output[4] = 0x90; - test_ue = test_ue_add_by_suci(&mobile_identity_suci, 13); + test_ue = test_ue_add_by_suci(&mobile_identity_suci, "0000203190"); ogs_assert(test_ue); test_ue->nr_cgi.cell_id = 0x40001;