Sfoglia il codice sorgente

Creating qarith.c, as module for arithmetic operations with bignums.

Moving some code from wiener's attack inro qarith, in order to provide easy
access to square root operations also for other attacks.
Doing this, I also moved the unittests for this function to test_qarith.c, and
added it to Makefile.am as test target.
Michele Orrù 11 anni fa
parent
commit
32e338c01b

+ 3 - 2
src/questions/Makefile.am

@@ -1,7 +1,8 @@
 SUBDIRS = tests
 
-lib_LIBRARIES = libquestions.a
 AM_CFLAGS = -I ../include/
-libquestions_a_SOURCES = wiener.c pollard.c example.c allquestions.c
+AM_LDFLAGS = -lssl -lcrypto
 
+lib_LIBRARIES = libquestions.a
+libquestions_a_SOURCES = wiener.c pollard.c example.c allquestions.c qarith.c qstrings.c
 # da fuck liquestions_a_HEADERS = qwiener.h questions.h

+ 0 - 1
src/questions/include/qarith.h

@@ -1,7 +1,6 @@
 #ifndef _QA_ARITH_H_
 #define _QA_ARITH_H_
 
-
 int BN_sqrtmod(BIGNUM* dv, BIGNUM* rem, BIGNUM* a, BN_CTX* ctx);
 
 #endif /* _QA_ARITH_H_ */

+ 1 - 0
src/questions/pollard.c

@@ -26,6 +26,7 @@
 #include <openssl/err.h>
 
 #include "qa/questions/questions.h"
+#include "qa/questions/qarith.h"
 #include "qa/questions/qpollard.h"
 
 

+ 47 - 0
src/questions/qarith.c

@@ -0,0 +1,47 @@
+#include <openssl/bn.h>
+
+#include "qa/questions/qarith.h"
+
+/**
+ * \brief Square Root for bignums.
+ *
+ * An implementation of Dijkstra's Square Root Algorithm.
+ * A Discipline of Programming, page 61 - Fifth Exercise.
+ *
+ * \return true if rem is equal to zero, false otherwise.
+ */
+int BN_sqrtmod(BIGNUM* dv, BIGNUM* rem, BIGNUM* a, BN_CTX* ctx)
+{
+  BIGNUM *shift;
+  BIGNUM *adj;
+
+  shift = BN_new();
+  adj = BN_new();
+  BN_zero(dv);
+  BN_copy(rem, a);
+
+  /* hacking into internal sequence to skip some cycles. */
+  /* for  (BN_one(shift);     original */
+  for (bn_wexpand(shift, a->top+1), shift->top=a->top, shift->d[shift->top-1] = 1;
+       BN_ucmp(shift, rem) != 1;
+       /* BN_rshift(shift, shift, 2); */
+       BN_lshift1(shift, shift), BN_lshift1(shift, shift));
+
+
+  while (!BN_is_one(shift)) {
+    /* BN_rshift(shift, shift, 2); */
+    BN_rshift1(shift, shift);
+    BN_rshift1(shift, shift);
+
+    BN_uadd(adj, dv, shift);
+    BN_rshift1(dv, dv);
+    if (BN_ucmp(rem, adj) != -1) {
+      BN_uadd(dv, dv, shift);
+      BN_usub(rem, rem, adj);
+    }
+  }
+
+  BN_free(shift);
+  BN_free(adj);
+  return BN_is_zero(rem);
+}

+ 7 - 5
src/questions/tests/Makefile.am

@@ -1,9 +1,11 @@
 # unittesting my ass
-check_PROGRAMS = test_qstrings
-
 AM_CFLAGS = -I ../../include/
+AM_LDFLAGS = -lcrypto -lssl -L ../libquestions.a
+
+check_PROGRAMS = test_qstrings test_qarith
+
+test_qstrings_SOURCES = test_qstrings.c
 
-test_qstrings_SOURCES = test_qstrings.c ../qstrings.c
-test_qstrings_LDADD = -L ../libquestions.a
+test_qarith_SOURCES = test_qarith.c
 
-TESTS = test_qstrings
+TESTS = test_qstrings test_qarith

+ 57 - 0
src/questions/tests/test_qarith.c

@@ -0,0 +1,57 @@
+#include <assert.h>
+
+#include <openssl/bn.h>
+#include "qa/questions/qarith.h"
+
+static void test_BN_sqrtmod(void)
+{
+  BIGNUM *a, *b, *expected;
+  BIGNUM *root, *rem;
+  BIGNUM *mayzero;
+  BN_CTX *ctx;
+
+  a = b = expected = NULL;
+  root = BN_new();
+  rem = BN_new();
+  mayzero = BN_new();
+  ctx = BN_CTX_new();
+
+  BN_dec2bn(&a, "144");
+  BN_dec2bn(&expected, "12");
+  BN_sqrtmod(root, rem, a, ctx);
+  assert(!BN_cmp(expected, root));
+  assert(BN_is_zero(rem));
+
+  BN_dec2bn(&a, "15245419238964964");
+  BN_dec2bn(&expected, "123472342");
+  BN_sqrtmod(root, rem, a, ctx);
+  assert(!BN_cmp(root, expected));
+  assert(BN_is_zero(rem));
+
+  BN_dec2bn(&a, "5");
+  BN_dec2bn(&expected, "2");
+  BN_sqrtmod(root, rem, a, ctx);
+  assert(!BN_cmp(root, expected));
+  assert(BN_is_one(rem));
+
+  BN_dec2bn(&a, "106929");
+  BN_dec2bn(&expected, "327");
+  BN_sqrtmod(root, rem, a, ctx);
+  assert(BN_is_zero(rem));
+  assert(!BN_cmp(root, expected));
+
+  BN_free(root);
+  BN_free(rem);
+  BN_free(mayzero);
+  BN_CTX_free(ctx);
+  BN_free(a);
+  BN_free(expected);
+}
+
+
+int main(int argc, char **argv)
+{
+  test_BN_sqrtmod();
+  return 0;
+
+}

+ 0 - 1
src/questions/tests/test_qstrings.c

@@ -3,7 +3,6 @@
 
 #include "qa/questions/qstrings.h"
 
-
 void test_is_vzero(void)
 {
   const char *v = "\x0\x0\x0\x1\x0\x1";

+ 0 - 47
src/questions/tests/test_wiener.c

@@ -113,52 +113,6 @@ void test_cf(void)
 }
 
 
-void test_BN_sqrtmod(void)
-{
-  BIGNUM *a, *b, *expected;
-  BIGNUM *root, *rem;
-  BIGNUM *mayzero;
-  BN_CTX *ctx;
-
-  a = b = expected = NULL;
-  root = BN_new();
-  rem = BN_new();
-  mayzero = BN_new();
-  ctx = BN_CTX_new();
-
-  BN_dec2bn(&a, "144");
-  BN_dec2bn(&expected, "12");
-  BN_sqrtmod(root, rem, a, ctx);
-  assert(!BN_cmp(expected, root));
-  assert(BN_is_zero(rem));
-
-  BN_dec2bn(&a, "15245419238964964");
-  BN_dec2bn(&expected, "123472342");
-  BN_sqrtmod(root, rem, a, ctx);
-  assert(!BN_cmp(root, expected));
-  assert(BN_is_zero(rem));
-
-  BN_dec2bn(&a, "5");
-  BN_dec2bn(&expected, "2");
-  BN_sqrtmod(root, rem, a, ctx);
-  assert(!BN_cmp(root, expected));
-  assert(BN_is_one(rem));
-
-  BN_dec2bn(&a, "106929");
-  BN_dec2bn(&expected, "327");
-  BN_sqrtmod(root, rem, a, ctx);
-  assert(BN_is_zero(rem));
-  assert(!BN_cmp(root, expected));
-
-  BN_free(root);
-  BN_free(rem);
-  BN_free(mayzero);
-  BN_CTX_free(ctx);
-  BN_free(a);
-  BN_free(expected);
-}
-
-
 void test_wiener(void)
 {
   X509 *crt;
@@ -179,7 +133,6 @@ int main(int argc, char ** argv)
   WienerQuestion.setup();
 
   test_cf();
-  test_BN_sqrtmod();
   test_wiener();
 
   WienerQuestion.teardown();

+ 1 - 45
src/questions/wiener.c

@@ -10,6 +10,7 @@
 #include <openssl/bn.h>
 
 #include "qa/questions/questions.h"
+#include "qa/questions/qarith.h"
 #include "qa/questions/qwiener.h"
 
 
@@ -135,51 +136,6 @@ bigfraction_t* cf_next(cf_t *f)
 }
 
 
-/**
- * \brief Square Root for bignums.
- *
- * An implementation of Dijkstra's Square Root Algorithm.
- * A Discipline of Programming, page 61 - Fifth Exercise.
- *
- * \return true if rem is equal to zero, false otherwise.
- */
-int BN_sqrtmod(BIGNUM* dv, BIGNUM* rem, BIGNUM* a, BN_CTX* ctx)
-{
-  BIGNUM *shift;
-  BIGNUM *adj;
-
-  shift = BN_new();
-  adj = BN_new();
-  BN_zero(dv);
-  BN_copy(rem, a);
-
-  /* hacking into internal sequence to skip some cycles. */
-  /* for  (BN_one(shift);     original */
-  for (bn_wexpand(shift, a->top+1), shift->top=a->top, shift->d[shift->top-1] = 1;
-       BN_ucmp(shift, rem) != 1;
-       /* BN_rshift(shift, shift, 2); */
-       BN_lshift1(shift, shift), BN_lshift1(shift, shift));
-
-
-  while (!BN_is_one(shift)) {
-    /* BN_rshift(shift, shift, 2); */
-    BN_rshift1(shift, shift);
-    BN_rshift1(shift, shift);
-
-    BN_uadd(adj, dv, shift);
-    BN_rshift1(dv, dv);
-    if (BN_ucmp(rem, adj) != -1) {
-      BN_uadd(dv, dv, shift);
-      BN_usub(rem, rem, adj);
-    }
-  }
-
-  BN_free(shift);
-  BN_free(adj);
-  return BN_is_zero(rem);
-}
-
-
 /*
  *  Weiner Attack Implementation
  */