mme: Add code to derive NAS token from NAS ul_count

NAS Token is derived from kasme and NAS ul_count as specified in 3GPP TS
33.401 A.9, and its LSB 16 bits passed to the network when mapping the GUTI to
RAI+PTMSI+PTIMSI_SIG.

Take the chance to move guti2rai_ptmsi() up to before the place it is
used.
Change-Id: I5e6003a2fe3e74cc93cfe4a288e6c114aa288d0b
diff --git a/mme/key_derivation.c b/mme/key_derivation.c
index 36e4c91..8d5d8a4 100644
--- a/mme/key_derivation.c
+++ b/mme/key_derivation.c
@@ -80,3 +80,19 @@
 
 	gnutls_hmac_fast(GNUTLS_MAC_SHA256, k, 32, s, 14, kasme);
 }
+
+/* TS33.401 Annex A.9: NAS token derivation for inter-RAT mobility */
+void mme_kdf_nas_token(const uint8_t *kasme, uint32_t ul_count, uint8_t *nas_token)
+{
+	uint8_t s[7];
+
+	s[0] = 0x17; /* FC Value */
+
+	ul_count = htonl(ul_count);
+	memcpy(s+1, &ul_count, 4);
+
+	s[5] = 0x00;
+	s[6] = 0x04;
+
+	gnutls_hmac_fast(GNUTLS_MAC_SHA256, kasme, 32, s, 7, nas_token);
+}