Cleanup SQN code, use generic functions

Submitted by Max on July 9, 2014, 3:46 p.m.

Details

Message ID 1404920800-21107-1-git-send-email-Max.Suraev@fairwaves.co
State New
Headers show

Commit Message

Max July 9, 2014, 3:46 p.m.
Signed-off-by: Max Suraev <Max.Suraev@fairwaves.co>
---
 src/gsm/auth_milenage.c | 35 +++--------------------------------
 1 file changed, 3 insertions(+), 32 deletions(-)

Comments

Max July 9, 2014, 3:51 p.m.
Just a small code simplification due to use of generic functions, covered by test suite.

Patch hide | download patch | download mbox

diff --git a/src/gsm/auth_milenage.c b/src/gsm/auth_milenage.c
index 5b2787d..1635ac6 100644
--- a/src/gsm/auth_milenage.c
+++ b/src/gsm/auth_milenage.c
@@ -21,39 +21,10 @@ 
  */
 
 #include <osmocom/crypt/auth.h>
+#include <osmocom/core/bits.h>
 #include "milenage/common.h"
 #include "milenage/milenage.h"
 
-static void sqn_u64_to_48bit(uint8_t *sqn, const uint64_t sqn64)
-{
-	sqn[5] = (sqn64 >>  0) & 0xff;
-	sqn[4] = (sqn64 >>  8) & 0xff;
-	sqn[3] = (sqn64 >> 16) & 0xff;
-	sqn[2] = (sqn64 >> 24) & 0xff;
-	sqn[1] = (sqn64 >> 32) & 0xff;
-	sqn[0] = (sqn64 >> 40) & 0xff;
-}
-
-static uint64_t sqn_48bit_to_u64(const uint8_t *sqn)
-{
-	uint64_t sqn64;
-
-	sqn64 = sqn[0];
-	sqn64 <<= 8;
-	sqn64 |= sqn[1];
-	sqn64 <<= 8;
-	sqn64 |= sqn[2];
-	sqn64 <<= 8;
-	sqn64 |= sqn[3];
-	sqn64 <<= 8;
-	sqn64 |= sqn[4];
-	sqn64 <<= 8;
-	sqn64 |= sqn[5];
-
-	return sqn64;
-}
-
-
 static int milenage_gen_vec(struct osmo_auth_vector *vec,
 			    struct osmo_sub_auth_data *aud,
 			    const uint8_t *_rand)
@@ -62,7 +33,7 @@  static int milenage_gen_vec(struct osmo_auth_vector *vec,
 	uint8_t sqn[6];
 	int rc;
 
-	sqn_u64_to_48bit(sqn, aud->u.umts.sqn);
+	osmo_store64be_ext(aud->u.umts.sqn, sqn, 6);
 	milenage_generate(aud->u.umts.opc, aud->u.umts.amf, aud->u.umts.k,
 			  sqn, _rand,
 			  vec->autn, vec->ik, vec->ck, vec->res, &res_len);
@@ -101,7 +72,7 @@  static int milenage_gen_vec_auts(struct osmo_auth_vector *vec,
 	if (rc < 0)
 		return rc;
 
-	aud->u.umts.sqn = sqn_48bit_to_u64(sqn_out) + 1;
+	aud->u.umts.sqn = 1 + (osmo_load64be_ext(sqn_out, 6) >> 16);
 
 	return milenage_gen_vec(vec, aud, _rand);
 }