Explorar o código

modadd: declare a function not a macto

ambrop7 %!s(int64=14) %!d(string=hai) anos
pai
achega
e8e49d95e9
Modificáronse 2 ficheiros con 23 adicións e 19 borrados
  1. 19 15
      misc/modadd.h
  2. 4 4
      security/OTPChecker.c

+ 19 - 15
misc/modadd.h

@@ -22,6 +22,9 @@
  * @section DESCRIPTION
  * 
  * Modular addition macro.
+ * 
+ * Calculates (x + y) mod m, assuming
+ * 0 <= x < m and 0 <= y < m.
  */
 
 #ifndef BADVPN_MISC_MODADD_H
@@ -29,20 +32,21 @@
 
 #include <misc/debug.h>
 
-/**
- * Calculates (x + y) mod m, assuming
- * 0 <= x < m and 0 <= y < m.
- */
-#define BMODADD(x, y, m) \
-    ({ \
-        typeof (x) _modadd_x = (x); \
-        typeof (y) _modadd_y = (y); \
-        typeof (m) _modadd_m = (m); \
-        ASSERT(_modadd_x >= 0) \
-        ASSERT(_modadd_x < _modadd_m) \
-        ASSERT(_modadd_y >= 0) \
-        ASSERT(_modadd_y < _modadd_m) \
-        (_modadd_y >= _modadd_m - _modadd_x ? _modadd_y - (_modadd_m - _modadd_x) : _modadd_x + _modadd_y); \
-    })
+#define DECLARE_BMODADD(type, name) \
+static type bmodadd_##name (type x, type y, type m) \
+{ \
+    ASSERT(x >= 0) \
+    ASSERT(x < m) \
+    ASSERT(y >= 0) \
+    ASSERT(y < m) \
+     \
+    if (y >= m - x) { \
+        return (y - (m - x)); \
+    } else { \
+        return (x + y); \
+    } \
+} \
+
+DECLARE_BMODADD(int, int)
 
 #endif

+ 4 - 4
security/OTPChecker.c

@@ -45,7 +45,7 @@ void OTPChecker_Table_AddOTP (OTPChecker *mc, struct OTPChecker_table *t, otp_t
     
     // try indexes starting with the base position
     for (int i = 0; i < mc->num_entries; i++) {
-        int index = BMODADD(start_index, i, mc->num_entries);
+        int index = bmodadd_int(start_index, i, mc->num_entries);
         struct OTPChecker_entry *entry = &t->entries[index];
         
         // if we find a free index, use it
@@ -88,7 +88,7 @@ int OTPChecker_Table_CheckOTP (OTPChecker *mc, struct OTPChecker_table *t, otp_t
     
     // try indexes starting with the base position
     for (int i = 0; i < mc->num_entries; i++) {
-        int index = BMODADD(start_index, i, mc->num_entries);
+        int index = bmodadd_int(start_index, i, mc->num_entries);
         struct OTPChecker_entry *entry = &t->entries[index];
         
         // if we find an empty entry, there is no such mac
@@ -127,7 +127,7 @@ static void work_done_handler (OTPChecker *mc)
     mc->tw_have = 0;
     
     // update next table number
-    mc->next_table = BMODADD(mc->next_table, 1, mc->num_tables);
+    mc->next_table = bmodadd_int(mc->next_table, 1, mc->num_tables);
     
     // update number of used tables if not all are used yet
     if (mc->tables_used < mc->num_tables) {
@@ -266,7 +266,7 @@ int OTPChecker_CheckOTP (OTPChecker *mc, uint16_t seed_id, otp_t otp)
     
     // try tables in reverse order
     for (int i = 1; i <= mc->tables_used; i++) {
-        int table_index = BMODADD(mc->next_table, mc->num_tables - i, mc->num_tables);
+        int table_index = bmodadd_int(mc->next_table, mc->num_tables - i, mc->num_tables);
         if (table_index == mc->next_table && mc->tw_have) {
             // ignore table that is being generated
             continue;