diff --git a/Makefile b/Makefile index cc9a5e0..ef69d8a 100644 --- a/Makefile +++ b/Makefile @@ -277,17 +277,17 @@ doc: Doxyfile $(BUILD_OBJ)/timestamp $(HEADERS) TODO_TYPES ?= HACK TODO FIXME BUG XXX PERF FUTURE REMOVE MAGIC UNIFY TODO_LOCATIONS ?= src test Makefile Doxyfile todo:: - @(find $(TODO_LOCATIONS) -name '*.h' -or -name '*.c' -or -name '*.cxx' -or -name '*.hxx') | xargs egrep --color=auto -w \ + @(find $(TODO_LOCATIONS) -name '*.h' -or -name '*.c' -or -name '*.cxx' -or -name '*.hxx' -or -name '*.py') | xargs egrep --color=auto -w \ `echo $(TODO_TYPES) | tr ' ' '|'` @echo '=============================' @(for i in $(TODO_TYPES); do \ - (find $(TODO_LOCATIONS) -name '*.h' -or -name '*.c' -or -name '*.cxx' -or -name '*.hxx') | xargs egrep -w $$i > /dev/null || continue; \ + (find $(TODO_LOCATIONS) -name '*.h' -or -name '*.c' -or -name '*.cxx' -or -name '*.hxx' -or -name '*.py') | xargs egrep -w $$i > /dev/null || continue; \ /bin/echo -n $$i' ' | head -c 10; \ - (find $(TODO_LOCATIONS) -name '*.h' -or -name '*.c' -or -name '*.cxx' -or -name '*.hxx') | xargs egrep -w $$i| wc -l; \ + (find $(TODO_LOCATIONS) -name '*.h' -or -name '*.c' -or -name '*.cxx' -or -name '*.hxx' -or -name '*.py') | xargs egrep -w $$i| wc -l; \ done) @echo '=============================' @echo -n 'Total ' - @(find $(TODO_LOCATIONS) -name '*.h' -or -name '*.c' -or -name '*.cxx' -or -name '*.hxx') | xargs egrep -w \ + @(find $(TODO_LOCATIONS) -name '*.h' -or -name '*.c' -or -name '*.cxx' -or -name '*.hxx' -or -name '*.py') | xargs egrep -w \ `echo $(TODO_TYPES) | tr ' ' '|'` | wc -l bench: $(BUILD_IBIN)/bench diff --git a/src/gen_headers/crypto_hxx.py b/src/gen_headers/crypto_hxx.py index ba02c56..18fe75c 100644 --- a/src/gen_headers/crypto_hxx.py +++ b/src/gen_headers/crypto_hxx.py @@ -63,7 +63,7 @@ public: inline explicit PublicKey(const NOINIT&) NOEXCEPT { } /** Serialize into a buffer. */ - inline void serializeInto(unsigned char *x) const NOEXCEPT { + inline void serialize_into(unsigned char *x) const NOEXCEPT { memcpy(x,wrapped,sizeof(wrapped)); } @@ -133,7 +133,7 @@ public: inline size_t serSize() const NOEXCEPT { return SER_BYTES; } /** Serialize into a buffer. */ - inline void serializeInto(unsigned char *x) const NOEXCEPT { + inline void serialize_into(unsigned char *x) const NOEXCEPT { memcpy(x,wrapped,sizeof(wrapped)); } diff --git a/src/gen_headers/decaf_hxx.py b/src/gen_headers/decaf_hxx.py index 09cad05..22704c1 100644 --- a/src/gen_headers/decaf_hxx.py +++ b/src/gen_headers/decaf_hxx.py @@ -105,7 +105,7 @@ public: inline size_t serSize() const NOEXCEPT { return SER_BYTES; } /** Serializable instance */ - inline void serializeInto(unsigned char *buffer) const NOEXCEPT { + inline void serialize_into(unsigned char *buffer) const NOEXCEPT { %(c_ns)s_scalar_encode(buffer, s); } @@ -163,7 +163,8 @@ public: /** Negate */ inline Scalar operator- () const NOEXCEPT { Scalar r((NOINIT())); %(c_ns)s_scalar_sub(r.s,%(c_ns)s_scalar_zero,s); return r; } - /** Invert with Fermat's Little Theorem (slow!). If *this == 0, return 0. */ + /** Invert with Fermat's Little Theorem (slow!). If *this == 0, + * throw CryptoException. */ inline Scalar inverse() const throw(CryptoException) { Scalar r; if (DECAF_SUCCESS != %(c_ns)s_scalar_invert(r.s,s)) { @@ -172,6 +173,13 @@ public: return r; } + /** Invert with Fermat's Little Theorem (slow!). If *this == 0, set r=0 + * and return DECAF_FAILURE. */ + inline decaf_error_t __attribute__((warn_unused_result)) + inverse_noexcept(Scalar &r) const NOEXCEPT { + return %(c_ns)s_scalar_invert(r.s,s); + } + /** Divide by inverting q. If q == 0, return 0. */ inline Scalar operator/ (const Scalar &q) const throw(CryptoException) { return *this * q.inverse(); } @@ -319,7 +327,7 @@ public: inline size_t serSize() const NOEXCEPT { return SER_BYTES; } /** Serializable instance */ - inline void serializeInto(unsigned char *buffer) const NOEXCEPT { + inline void serialize_into(unsigned char *buffer) const NOEXCEPT { %(c_ns)s_point_encode(buffer, p); } diff --git a/src/public_include/decaf/secure_buffer.hxx b/src/public_include/decaf/secure_buffer.hxx index ed1c2d1..fef282a 100644 --- a/src/public_include/decaf/secure_buffer.hxx +++ b/src/public_include/decaf/secure_buffer.hxx @@ -88,14 +88,14 @@ public: inline size_t serSize() const NOEXCEPT { return static_cast(this)->serSize(); } /** @brief Serialize this object into a buffer */ - inline void serializeInto(unsigned char *buf) const NOEXCEPT { - static_cast(this)->serializeInto(buf); + inline void serialize_into(unsigned char *buf) const NOEXCEPT { + static_cast(this)->serialize_into(buf); } /** @brief Serialize this object into a SecureBuffer and return it */ inline SecureBuffer serialize() const throw(std::bad_alloc) { SecureBuffer out(serSize()); - serializeInto(out.data()); + serialize_into(out.data()); return out; } diff --git a/test/test_ct.cxx b/test/test_ct.cxx index 54a14f1..a2a0904 100644 --- a/test/test_ct.cxx +++ b/test/test_ct.cxx @@ -18,7 +18,7 @@ using namespace decaf; -static const long NTESTS = 1; +static const long NTESTS = 10; const char *undef_str = "Valgrind thinks this string is undefined."; const Block undef_block(undef_str); @@ -44,10 +44,10 @@ static void test_arithmetic() { (void)(x+y); (void)(x-y); (void)(x*y); - //(void)(x/y); // TODO: Fails due to zero check, but needs to be tested anyway. + ignore(x.inverse_noexcept(y)); (void)(x==y); (void)(z=y); - x.serializeInto(ser); + x.serialize_into(ser); x = y; } } @@ -73,7 +73,7 @@ static void test_ec() { Scalar y(rng),z(rng); Point p(rng),q(rng),r; - p.serializeInto(ser); + p.serialize_into(ser); ignore(Group::Point::decode(p,FixedBlock(ser))); (void)(p*y); (void)(p+q); @@ -91,10 +91,20 @@ static void test_ec() { } static void test_crypto() { - /* TODO */ + SpongeRng rng(Block("test_crypto")); + rng.stir(undef_block); + + for (int i=0; i sk1(rng); + PrivateKey sk2(rng); + SecureBuffer sig = sk1.sign(undef_block); + //sk.pub().verify(undef_block,sig); would fail. FUTURE: ct version of this? + + /* TODO: shared_secret nothrow? have to test shared_secret... */ + } } -}; // template +}; /* template */ int main(int argc, char **argv) { (void) argc; (void) argv; diff --git a/test/test_decaf.cxx b/test/test_decaf.cxx index 841498c..9ecc8a1 100644 --- a/test/test_decaf.cxx +++ b/test/test_decaf.cxx @@ -51,7 +51,7 @@ typedef typename Group::Precomputed Precomputed; static void print(const char *name, const Scalar &x) { unsigned char buffer[Scalar::SER_BYTES]; - x.serializeInto(buffer); + x.serialize_into(buffer); printf(" %s = 0x", name); for (int i=sizeof(buffer)-1; i>=0; i--) { printf("%02x", buffer[i]); @@ -69,7 +69,7 @@ static void hexprint(const char *name, const SecureBuffer &buffer) { static void print(const char *name, const Point &x) { unsigned char buffer[Point::SER_BYTES]; - x.serializeInto(buffer); + x.serialize_into(buffer); printf(" %s = 0x", name); for (int i=Point::SER_BYTES-1; i>=0; i--) { printf("%02x", buffer[i]); @@ -347,7 +347,7 @@ static void test_crypto() { } } -}; // template +}; /* template */ int main(int argc, char **argv) { (void) argc; (void) argv;