enb: Set cipher and integrity via scenario file

This patch enables setting cipher and integrity algorithms
in Amarisoft eNB and srsENB via scenario files. If no
settings are defined following defaults are applied:
- Cipher algorithm: EEA0, EEA2, EEA1
- Integrity algorithm: EIA2, EIA1, EIA0

Example of setting cipher algorithms:
- 4g:srsue-rftype@uhd+srsenb-rftype@uhd+mod-enb-cipher@eea1+mod-enb-cipher@eea0+mod-enb-nprb@6

Change-Id: I595206b7d49016fb6d0aec175c828d9537c53886
diff --git a/src/osmo_gsm_tester/core/schema.py b/src/osmo_gsm_tester/core/schema.py
index d4a460d..4064c20 100644
--- a/src/osmo_gsm_tester/core/schema.py
+++ b/src/osmo_gsm_tester/core/schema.py
@@ -105,11 +105,22 @@
         raise ValueError('Positive value >0 expected instead of %d' % n)
     return True
 
-def cipher(val):
+def cipher_2g(val):
     if val in ('a5_0', 'a5_1', 'a5_2', 'a5_3', 'a5_4', 'a5_5', 'a5_6', 'a5_7'):
         return True
     raise ValueError('Unknown Cipher value: %r' % val)
 
+def cipher_4g(val):
+    if val.lower() in ('eea0', 'eea1', 'eea2'):
+        return True
+    raise ValueError('Unknown 4G cipher value: %r' % val)
+
+def integrity_4g(val):
+    if val.lower() in ('eia0', 'eia1', 'eia2'):
+        # srsENB
+        return True
+    raise ValueError('Unknown 4G integrity value %r' % val)
+
 def modem_feature(val):
     if val in ('sms', 'gprs', 'voice', 'ussd', 'sim', '2g', '3g', '4g', 'dl_qam256', 'ul_qam64', 'qc_diag'):
         return True
@@ -165,7 +176,9 @@
 MSISDN = 'msisdn'
 AUTH_ALGO = 'auth_algo'
 TIMES='times'
-CIPHER = 'cipher'
+CIPHER_2G = 'cipher_2g'
+CIPHER_4G = 'cipher_4g'
+INTEGRITY_4G = 'integrity_4g'
 MODEM_FEATURE = 'modem_feature'
 PHY_CHAN = 'chan'
 CHAN_ALLOCATOR = 'chan_allocator'
@@ -189,7 +202,8 @@
         MSISDN: msisdn,
         AUTH_ALGO: auth_algo,
         TIMES: times,
-        CIPHER: cipher,
+        CIPHER_2G: cipher_2g,
+        CIPHER_4G: cipher_4g,
         MODEM_FEATURE: modem_feature,
         PHY_CHAN: phy_channel_config,
         CHAN_ALLOCATOR: channel_allocator,
@@ -198,6 +212,7 @@
         OSMO_TRX_CLOCK_REF: osmo_trx_clock_ref,
         LTE_TRANSMISSION_MODE: lte_transmission_mode,
         DURATION: duration,
+        INTEGRITY_4G: integrity_4g,
     }
 
 def add(dest, src):
diff --git a/src/osmo_gsm_tester/obj/bts.py b/src/osmo_gsm_tester/obj/bts.py
index 76a6875..fd9fa3a 100644
--- a/src/osmo_gsm_tester/obj/bts.py
+++ b/src/osmo_gsm_tester/obj/bts.py
@@ -31,7 +31,7 @@
         'addr': schema.IPV4,
         'band': schema.BAND,
         'direct_pcu': schema.BOOL_STR,
-        'ciphers[]': schema.CIPHER,
+        'ciphers[]': schema.CIPHER_2G,
         'channel_allocator': schema.CHAN_ALLOCATOR,
         'gprs_mode': schema.GPRS_MODE,
         'emergency_calls_allowed': schema.BOOL_STR,
diff --git a/src/osmo_gsm_tester/obj/enb.py b/src/osmo_gsm_tester/obj/enb.py
index 50a8832..32ead69 100644
--- a/src/osmo_gsm_tester/obj/enb.py
+++ b/src/osmo_gsm_tester/obj/enb.py
@@ -44,6 +44,8 @@
         'enable_measurements': schema.BOOL_STR,
         'enable_dl_awgn': schema.BOOL_STR,
         'dl_awgn_snr': schema.INT,
+        'cipher_list[]': schema.CIPHER_4G,
+        'integrity_list[]': schema.INTEGRITY_4G,
         'a1_report_type': schema.STR,
         'a1_report_value': schema.INT,
         'a1_hysteresis': schema.INT,
diff --git a/src/osmo_gsm_tester/obj/enb_amarisoft.py b/src/osmo_gsm_tester/obj/enb_amarisoft.py
index b7ede3b..e97bb90 100644
--- a/src/osmo_gsm_tester/obj/enb_amarisoft.py
+++ b/src/osmo_gsm_tester/obj/enb_amarisoft.py
@@ -176,6 +176,10 @@
 
         config.overlay(values, dict(enb={'enable_dl_awgn': util.str2bool(values['enb'].get('enable_dl_awgn', 'false'))}))
 
+        # Remove EEA0 from cipher list, if specified, as it's always assumed as default
+        cipher_list = values['enb'].get('cipher_list', None)
+        if "eea0" in cipher_list: cipher_list.remove("eea0")
+
         # We need to set some specific variables programatically here to match IP addresses:
         if self._conf.get('rf_dev_type') == 'zmq':
             base_srate = self.num_prb2base_srate(self.num_prb())
diff --git a/src/osmo_gsm_tester/obj/ms.py b/src/osmo_gsm_tester/obj/ms.py
index 552de71..cd4a8a1 100644
--- a/src/osmo_gsm_tester/obj/ms.py
+++ b/src/osmo_gsm_tester/obj/ms.py
@@ -31,7 +31,7 @@
         'opc': schema.OPC,
         'auth_algo': schema.AUTH_ALGO,
         'apn_ipaddr': schema.IPV4,
-        'ciphers[]': schema.CIPHER,
+        'ciphers[]': schema.CIPHER_2G,
         'features[]': schema.MODEM_FEATURE
         }
     schema.register_resource_schema('modem', resource_schema)
diff --git a/src/osmo_gsm_tester/templates/amarisoft_enb.cfg.tmpl b/src/osmo_gsm_tester/templates/amarisoft_enb.cfg.tmpl
index 4c7aa33..2c6fcc0 100644
--- a/src/osmo_gsm_tester/templates/amarisoft_enb.cfg.tmpl
+++ b/src/osmo_gsm_tester/templates/amarisoft_enb.cfg.tmpl
@@ -224,9 +224,9 @@
     dpc_pucch_snr_target: 10,
 
     /* RRC/UP ciphering algorithm preference. EEA0 is always the last. */
-    cipher_algo_pref: [],
+    cipher_algo_pref: [${', '.join(list(dict.fromkeys(enb.cipher_list))).split("eea")[1] if len(list(dict.fromkeys(enb.cipher_list))) > 0 else ''}],
     /* RRC integrity algorithm preference. EIA0 is always the last. */
-    integ_algo_pref: [2, 1],
+    integ_algo_pref: [${', '.join(list(dict.fromkeys(enb.integrity_list))).split("eia")[1]}],
 
     /* (in ms) send RRC connection release after this time of network
        inactivity */
diff --git a/src/osmo_gsm_tester/templates/srsenb.conf.tmpl b/src/osmo_gsm_tester/templates/srsenb.conf.tmpl
index 7ac75bb..794edea 100644
--- a/src/osmo_gsm_tester/templates/srsenb.conf.tmpl
+++ b/src/osmo_gsm_tester/templates/srsenb.conf.tmpl
@@ -285,5 +285,5 @@
 #link_failure_nof_err = 50
 rrc_inactivity_timer = ${enb.inactivity_timer}
 #max_prach_offset_us  = 30
-#eea_pref_list = EEA0, EEA2, EEA1
-#eia_pref_list = EIA2, EIA1, EIA0
+eea_pref_list = ${', '.join(list(dict.fromkeys(enb.cipher_list))).upper()}
+eia_pref_list = ${', '.join(list(dict.fromkeys(enb.integrity_list))).upper()}
diff --git a/sysmocom/defaults.conf b/sysmocom/defaults.conf
index 7f1368b..d66e030 100644
--- a/sysmocom/defaults.conf
+++ b/sysmocom/defaults.conf
@@ -144,6 +144,13 @@
      root_seq_idx: 205
      scell_list: []
      ncell_list: []
+  cipher_4g:
+   - eea0
+   - eea2
+   - eea1
+  integrity_4g:
+   - eia2
+   - eia1
 
 srsenb:
   num_prb: 100
diff --git a/sysmocom/scenarios/mod-enb-cipher@.conf b/sysmocom/scenarios/mod-enb-cipher@.conf
new file mode 100644
index 0000000..7cd453f
--- /dev/null
+++ b/sysmocom/scenarios/mod-enb-cipher@.conf
@@ -0,0 +1,4 @@
+modifiers:
+  enb:
+  - cipher_list:
+    - ${param1}
diff --git a/sysmocom/scenarios/mod-enb-integrity@.conf b/sysmocom/scenarios/mod-enb-integrity@.conf
new file mode 100644
index 0000000..fa708ad
--- /dev/null
+++ b/sysmocom/scenarios/mod-enb-integrity@.conf
@@ -0,0 +1,4 @@
+modifiers:
+  enb:
+  - integrity_list:
+    - ${param1}