]> git.codecow.com Git - Monocypher.git/log
Monocypher.git
7 years agoRemoved division by zero in speed benchmarks
Loup Vaillant [Sun, 17 Feb 2019 18:25:52 +0000 (19:25 +0100)]
Removed division by zero in speed benchmarks

If some library is so fast that it goes below the resolution of the
timer we're using to measure it, the measured duration may be zero, and
then trigger a division by zero when we convert it to a speed in Hz.

This could possibly happen with a very fast library (Libsodium), on a
very fast machine, with a sufficiently low resolution timer.

This patch reworks and simplifies things a bit, and adds an explicit
check. We now print "too fast to be measured" instead of dividing by
zero.

7 years agoDocumentation typo
Loup Vaillant [Fri, 15 Feb 2019 07:29:49 +0000 (08:29 +0100)]
Documentation typo

7 years agoWrong array size in function argument
Loup Vaillant [Sun, 3 Feb 2019 10:59:02 +0000 (11:59 +0100)]
Wrong array size in function argument

Fixes #114

Most proably harmless, but nevertheless ugly.

7 years agoForgot Libsodium include dependency
Loup Vaillant [Mon, 28 Jan 2019 23:20:31 +0000 (00:20 +0100)]
Forgot Libsodium include dependency

7 years agoAllow the test suite to customise its random seed
Loup Vaillant [Sat, 26 Jan 2019 14:44:01 +0000 (15:44 +0100)]
Allow the test suite to customise its random seed

This will only affect the property based tests, not the test vectors
themselves.  The idea is to let paranoid users run the test suite with
lots and lots of different streams of random numbers, just to be safe.

Test vector generation could undergo a similar transformation, though it
is less likely to be worth the trouble (we'd have to generate the test
vectors, compile the test suite all over again).

7 years agoLink SHA-512 code when using -DED25519_SHA512
Loup Vaillant [Fri, 25 Jan 2019 14:43:02 +0000 (15:43 +0100)]
Link SHA-512 code when using -DED25519_SHA512

When the $CFLAGS variable contains the -DED25519_SHA512 option (by
default it doesn't), the code from src/optional/sha512.c is
automatically linked to the final libraries (libmonocypher.a and
libmonocypher.so).

That way, users who need to install a ED25519 compliant version of
Monocypher can do so simply by altering the compilation options with the
$CFLAGS variable.

7 years agoMade L an array of *signed* integers
Loup Vaillant [Sun, 20 Jan 2019 21:42:38 +0000 (22:42 +0100)]
Made L an array of *signed* integers

Was unsigned previously, causing a bunch of implementation defined
conversions.  No machine nowadays are no 2's complement, but it's still
cleaner that way.

7 years agoTry not to waste the stack
Loup Vaillant [Fri, 14 Dec 2018 20:12:33 +0000 (21:12 +0100)]
Try not to waste the stack

7 years agoFixed MSVC W4 warnings
Loup Vaillant [Fri, 14 Dec 2018 20:03:54 +0000 (21:03 +0100)]
Fixed MSVC W4 warnings

Fixes #112

7 years agoDecoupled window widths, minimised stack usage
Loup Vaillant [Thu, 6 Dec 2018 00:04:37 +0000 (01:04 +0100)]
Decoupled window widths, minimised stack usage

The width of the pre-computed window affects the program size. It has
been set to 5 (8 elements) so we can approach maximum performance
without bloating the program too much.

The width of the cached window affects the *stack* size. It has been set
to 3 (2 elements) to avoid blowing up the stack (this matters most on
embedded environments). The performance hit is measurable, yet very
reasonable.

Footgun wielders can adjust those widths as they see fit.

7 years agoInlined window caching
Loup Vaillant [Wed, 5 Dec 2018 23:46:07 +0000 (00:46 +0100)]
Inlined window caching

7 years agoOptimised addition of base point window
Loup Vaillant [Wed, 5 Dec 2018 23:39:49 +0000 (00:39 +0100)]
Optimised addition of base point window

7 years agoFully pre-compute the base point window
Loup Vaillant [Wed, 5 Dec 2018 22:44:33 +0000 (23:44 +0100)]
Fully pre-compute the base point window

7 years agoParameterise sliding window width with a macro
Loup Vaillant [Wed, 5 Dec 2018 22:16:55 +0000 (23:16 +0100)]
Parameterise sliding window width with a macro

This is more general, perhaps even more readable this way. This also
lays the groundwork for using different window widths for the
pre-computed window and the cached one. (The cached window has to be
smaller to save stack space, while the pre-computed constant is allowed
to be bigger).

7 years agoTest -DBLAKE2_NO_UNROLLING flag
Loup Vaillant [Thu, 29 Nov 2018 22:05:23 +0000 (23:05 +0100)]
Test -DBLAKE2_NO_UNROLLING flag

7 years agoConvert int indices to size_t
Loup Vaillant [Thu, 29 Nov 2018 21:32:39 +0000 (22:32 +0100)]
Convert int indices to size_t

Fixes #111 (MSVC warnings)

All indices in Monocypher are size_t.  We might as well be consistent.

7 years agoAdded -DBLAKE2_NO_UNROLLING preprocessor option
Loup Vaillant [Sat, 10 Nov 2018 12:59:38 +0000 (13:59 +0100)]
Added -DBLAKE2_NO_UNROLLING preprocessor option

Less bloat, faster on some embedded platforms.

7 years agoConst correctness
Loup Vaillant [Fri, 2 Nov 2018 16:27:41 +0000 (17:27 +0100)]
Const correctness

7 years agoNo intermediate buffer for HChacha20
Loup Vaillant [Tue, 23 Oct 2018 19:06:47 +0000 (21:06 +0200)]
No intermediate buffer for HChacha20

That buffer wasn't really needed, so I optimised it away

7 years agoOverlap test for HChacha20
Loup Vaillant [Tue, 23 Oct 2018 19:05:12 +0000 (21:05 +0200)]
Overlap test for HChacha20

7 years agoManual: wrong nonce size in examples
Loup Vaillant [Sun, 23 Sep 2018 16:24:03 +0000 (18:24 +0200)]
Manual: wrong nonce size in examples

7 years agoRemoved version numbers from coverage script
Loup Vaillant [Sat, 15 Sep 2018 13:21:29 +0000 (15:21 +0200)]
Removed version numbers from coverage script

7 years agoMerge pull request #108 from fscoto/master+doc/windows-random
Loup Vaillant [Tue, 28 Aug 2018 14:22:02 +0000 (16:22 +0200)]
Merge pull request #108 from fscoto/master+doc/windows-random

Recommend BCryptGenRandom on Windows

7 years agoRecommend BCryptGenRandom on Windows
Fabio Scotoni [Tue, 28 Aug 2018 13:23:22 +0000 (15:23 +0200)]
Recommend BCryptGenRandom on Windows

CryptGenRandom is deprecated.

7 years agoBetter code coverage for EdDSA
Loup Vaillant [Fri, 24 Aug 2018 22:28:30 +0000 (00:28 +0200)]
Better code coverage for EdDSA

7 years agoC++ compatibility
Loup Vaillant [Thu, 23 Aug 2018 18:25:48 +0000 (20:25 +0200)]
C++ compatibility

7 years ago2.0.5 changelog
Loup Vaillant [Thu, 23 Aug 2018 18:15:36 +0000 (20:15 +0200)]
2.0.5 changelog

7 years agofe_ccopy() now works the same as fe_cswap()
Loup Vaillant [Mon, 20 Aug 2018 16:58:19 +0000 (18:58 +0200)]
fe_ccopy() now works the same as fe_cswap()

7 years agocosmetic
Loup Vaillant [Sat, 18 Aug 2018 10:23:14 +0000 (12:23 +0200)]
cosmetic

7 years agoAdded tests for HChacha20
Loup Vaillant [Thu, 16 Aug 2018 19:29:13 +0000 (21:29 +0200)]
Added tests for HChacha20

Not that it needed any (XChacha20 were enough), but it's easier to
communicate to outsiders that HChacha20 is correct when we have explicit
test vectors.

7 years agoProperly prevent S malleability
Loup Vaillant [Wed, 15 Aug 2018 18:02:03 +0000 (20:02 +0200)]
Properly prevent S malleability

S malleability was mostly prevented in a previous commit, for reasons
that had nothing to do with S malleability.  This mislead users into
thinking Monocypher was not S malleable.

To avoid confusion, I properly verify that S is strictly lower than L
(the order of the curve).  S malleability is no longer a thing.

We still have nonce malleability, but that one can't be helped.

Also added Wycheproof test vectors about malleability.

7 years agoSimplified sliding windows a bit
Loup Vaillant [Tue, 14 Aug 2018 10:59:17 +0000 (12:59 +0200)]
Simplified sliding windows a bit

7 years agoRemoved sliding windows edge cases
Loup Vaillant [Mon, 13 Aug 2018 11:05:39 +0000 (13:05 +0200)]
Removed sliding windows edge cases

By making sure the scalar's most significant bits are 0, we remove an
edge case, and can skip the epilogue of the sliding windows.

This adds 2 bytes to the sliding windows representation of the scalars,
but also makes the code smaller and simpler.

7 years agoCorrected wrong comment
Loup Vaillant [Sun, 12 Aug 2018 11:04:39 +0000 (13:04 +0200)]
Corrected wrong comment

7 years agoSigned sliding windows for EdDSA
Loup Vaillant [Sat, 11 Aug 2018 18:05:28 +0000 (20:05 +0200)]
Signed sliding windows for EdDSA

Signed sliding windows are effectively one bit wider than their unsigned
counterparts, without doubling the size of the corresponding look up
table.  Going from 4-bit unsigned to 5-bit signed allowed us to gain
almost 17 additions on average.

This gain is less impressive than it sounds: the whole operation still
costs 254 doublings and 56 additions, and going signed made window
construction and look up a bit slower.  Overall, we barely gained 2.5%.

We could gain a bit more speed still by precomputing the look up table
for the base point, but the gains would be similar, and the costs in
code size and complexity would be even bigger.

7 years agoReduced EdDSA malleability for sliding windows
Loup Vaillant [Sat, 11 Aug 2018 16:19:35 +0000 (18:19 +0200)]
Reduced EdDSA malleability for sliding windows

Signed sliding windows can overflow the initial scalar by one bit.  This
is not a problem when the scalar is reduced modulo L, which is smaller
than 2^253.  The second half of the signature however is controlled by
the attacker, and can be any value.

Legitimate signatures however always reduce modulo L.  They don't really
have to, but this helps with determinism, and enables test vectors.  So
we can safely reject any signature whose second half exceeds L.

This patch rejects anything above 2^253-1, thus guaranteeing that the
three most significant bits are cleared.  This eliminate s-malleability
in most cases, but not all.  Besides, there is still nonce malleability.

Users should still assume signatures are malleable.

7 years agoEdDSA double scalarmult can now subtract
Loup Vaillant [Sat, 11 Aug 2018 16:06:35 +0000 (18:06 +0200)]
EdDSA double scalarmult can now subtract

This allows it to handle signed sliding windows (not yet implemented).

7 years agoEdDSA sliding windows now indicate the number
Loup Vaillant [Sat, 11 Aug 2018 15:36:14 +0000 (17:36 +0200)]
EdDSA sliding windows now indicate the number

This is in preparation for signed sliding windows.  Instead of choosing
-1 for doing nothing, and an index to point to the table, we write how
much we add directly (that means 0 for nothing).  We divide the number
by 2 to get the index.

The double scalarmult routine doesn't handle negative values yet.

7 years agoThank Mike Hamburg for his advice on comb algorithms
Loup Vaillant [Fri, 10 Aug 2018 23:39:20 +0000 (01:39 +0200)]
Thank Mike Hamburg for his advice on comb algorithms

7 years agoReferenced the signed comb for EdDSA
Loup Vaillant [Fri, 10 Aug 2018 20:48:14 +0000 (22:48 +0200)]
Referenced the signed comb for EdDSA

7 years agoFactored out mul_add() algorithm
Loup Vaillant [Fri, 10 Aug 2018 20:35:49 +0000 (22:35 +0200)]
Factored out mul_add() algorithm

It is was originally only used in signature verification, but the
transformation to all bits sets for the signed comb algorithm now also
uses it.

It also makes the high level algorithms a bit more readable.

7 years agoEdDSA is back to constant time
Loup Vaillant [Fri, 10 Aug 2018 20:13:57 +0000 (22:13 +0200)]
EdDSA is back to constant time

7 years agoProper Signed comb for EdDSA (not constant time yet)
Loup Vaillant [Fri, 10 Aug 2018 19:50:21 +0000 (21:50 +0200)]
Proper Signed comb for EdDSA (not constant time yet)

7 years agoSigned comb with unsigned table
Loup Vaillant [Wed, 8 Aug 2018 21:24:25 +0000 (23:24 +0200)]
Signed comb with unsigned table

Or, bitwiseshiftleft saves the day.  The current code is hacky as hell,
but it works, and it cleared up my confusion.  Turns out a suitable
signed comb is quite different from an unsigned one: the table itself
should represent -1 and 1 bits, instead of 0 and 1 bits.

Right now the same effect is achieved with 2 additions (more precisely,
an addition and a subtraction).  With the proper table, it can be one
operation.

7 years agoDesperate attempt to sort the signed comb mess
Loup Vaillant [Tue, 7 Aug 2018 21:34:43 +0000 (23:34 +0200)]
Desperate attempt to sort the signed comb mess

7 years agoSeparated odd/even tables
Loup Vaillant [Mon, 6 Aug 2018 19:20:13 +0000 (21:20 +0200)]
Separated odd/even tables

7 years ago5-bit table for EdDSA
Loup Vaillant [Mon, 6 Aug 2018 19:09:01 +0000 (21:09 +0200)]
5-bit table for EdDSA

Just to make sure the table generation process is correct.

7 years agoMoved EdDSA scalar operations
Loup Vaillant [Mon, 6 Aug 2018 18:20:11 +0000 (20:20 +0200)]
Moved EdDSA scalar operations

Just ground work for signed combs.

7 years agoInlined ge_from_xy() (called only once)
Loup Vaillant [Sun, 5 Aug 2018 00:14:50 +0000 (02:14 +0200)]
Inlined ge_from_xy() (called only once)

7 years agoAvoid macros where we can
Loup Vaillant [Sat, 4 Aug 2018 20:48:20 +0000 (22:48 +0200)]
Avoid macros where we can

Turns out a simple test (which doesn't depend on a secret) was enough to
not need the macro at all.  And we still save that multiplication.

7 years agoAvoids the first doubling for EdDSA signatures
Loup Vaillant [Sat, 4 Aug 2018 19:47:40 +0000 (21:47 +0200)]
Avoids the first doubling for EdDSA signatures

The overhead of this first multiplication is not much, but it's
measurable.

Note the use of a macro for the constant time lookup and addition. It
could have been a function, but the function call overhead eats up all
the gains (I guess there are too many arguments to push to and pop from
the stack).

7 years agoAvoids the first few doublings in EdDSA verification
Loup Vaillant [Sat, 4 Aug 2018 19:37:14 +0000 (21:37 +0200)]
Avoids the first few doublings in EdDSA verification

Legitimate scalars with EdDSA verification are at most 253-bit long.
That's 3 bits less than the full 256 bits.  By starting the loop at the
highest bit set, we can save a couple doublings.  It's not much, but
it's measurable.

7 years agoComb for EdDSA signatures in Niels coordinates
Loup Vaillant [Sat, 4 Aug 2018 19:08:53 +0000 (21:08 +0200)]
Comb for EdDSA signatures in Niels coordinates

While it takes a bit more space to encode, this also avoids some initial
overhead, and significantly reduces stack size.

Note: we could do away with the T2 coordinate to reduce the overhead of
constant time lookup, but this would also require more work per point
addition.  Experiments suggest the bigger table is a little faster.

7 years agoAll field element constants have the proper invariants
Loup Vaillant [Sat, 4 Aug 2018 13:30:54 +0000 (15:30 +0200)]
All field element constants have the proper invariants

A number of pre-computed constant didn't follow the ideal invariants set
forth by the carry propagation logic.  This increased the risk of limb
overflow.

Now all such constants are generated with fe_frombytes(), which
guarantees they can withstand the same number of additions and
subtraction before needing carry propagation. This reduces the risks,
and simplifies the analysis of code using field arithmetic.

7 years agoRevert "Cleaner fe_frombytes() (loading field elements)"
Loup Vaillant [Sat, 4 Aug 2018 11:25:23 +0000 (13:25 +0200)]
Revert "Cleaner fe_frombytes() (loading field elements)"

This reverts commit 6ee8787e61b3918789eab0ab38ce176abc767abb.

Turns out this commit was a huge blunder.  Carry propagation works by
minimising the absolute value of each limb.  The reverted patch did not
do that, resulting in limbs that were basically twice as big as they
should be.

While it could still work, this would at least reduce the margin for
error.  Better safe than sorry, and keep the more versatile loading
routine we had before.

Likewise, constants should minimise the absolute value of their limbs.
Failing to do so caused what was described in issue #107.

7 years agoCleaner fe_frombytes() (loading field elements)
Loup Vaillant [Fri, 3 Aug 2018 21:25:55 +0000 (23:25 +0200)]
Cleaner fe_frombytes() (loading field elements)

The old version of fe_frombytes() from the ref10 implementation was not
as clean as I wanted it to be: instead of loading exactly the right
bytes, it played fast and loose, then used a carry operation to
compensate.

It works, but there's a more direct, simpler, and I suspect faster
approach: put the right bits in the right place to begin with.

7 years agoMerged the Montgomery ladder back to crypto_x25519()
Loup Vaillant [Fri, 3 Aug 2018 18:58:38 +0000 (20:58 +0200)]
Merged the Montgomery ladder back  to crypto_x25519()

EdDSA stopped using the ladder, making it a separate function no longer
makes sense.

7 years agoSpecialised adding code for EdDSA signatures
Loup Vaillant [Fri, 3 Aug 2018 17:28:31 +0000 (19:28 +0200)]
Specialised adding code for EdDSA signatures

- Saved one multiplication by assuming Z=1
- Hoisted wipes out of loops
- Removed wipes for variable time additions

This made both signatures and verification a bit faster.  (Note: current
signature verification speed is only 23% slower than key exchange.  I
didn't think it could be that fast.)

7 years agoFull pre-computed table for EdDSA signatures
Loup Vaillant [Fri, 3 Aug 2018 16:47:15 +0000 (18:47 +0200)]
Full pre-computed table for EdDSA signatures

The main gain for now comes from reducing the amount of constant time
lookup.  We could reduce the table's size even further, *or* save a few
multiplications.

I'm currently a little suspicious of the way I generated the table. If
it passes the tests, it shouldn't have any error, but it still requires
some checking.

7 years agoge_double() is now more readable
Loup Vaillant [Mon, 30 Jul 2018 15:28:49 +0000 (17:28 +0200)]
ge_double() is now more readable

It's also become easier to hoist buffers out of loops

7 years agoCleaner point addition for EdDSA
Loup Vaillant [Sun, 29 Jul 2018 22:20:27 +0000 (00:20 +0200)]
Cleaner point addition for EdDSA

Point addition used to use 8 intermediate variables.  That's 6 more than
what was needed.  Removing them made wiping faster, and shrank the stack
by 240 bytes. (Stack size may matter in embedded systems.)

7 years agoProper comb algorithm for EdDSA
Loup Vaillant [Thu, 26 Jul 2018 20:40:21 +0000 (22:40 +0200)]
Proper comb algorithm for EdDSA

This will require some tweaking yet.  We could use signed combs to add 1
bit to the table (currently 4) without making it any bigger.  We should
hoist buffer wipes out of the double and add operations.  We could
consider other means of adding and doubling, to save multiplications and
to reduce table sizes (smaller tables have faster constant time lookup).

But we're faster than key exchange already.  We're on the right track.

7 years agoDon't use the Montgomery ladder for EdDSA
Loup Vaillant [Thu, 26 Jul 2018 18:29:11 +0000 (20:29 +0200)]
Don't use the Montgomery ladder for EdDSA

Again.

There are two reasons: the first is that there are 2 special cases where
the ladder or conversion gives the wrong results (the ladder is correct,
it was just hijacked for another purpose).

The second reason is, fixed based scalar multiplication can be made even
faster in Twisted Edwards space, by using a comb algorithm.  The current
patch uses a window, so it is slower.  It will get faster again.

7 years agoForgot to static the fe_pow22523() function
Loup Vaillant [Sun, 22 Jul 2018 21:15:10 +0000 (23:15 +0200)]
Forgot to static the fe_pow22523() function

7 years agoWording nitpick
Loup Vaillant [Wed, 18 Jul 2018 20:42:12 +0000 (22:42 +0200)]
Wording nitpick

7 years agoUpdated the examples for key exchange.
Loup Vaillant [Wed, 18 Jul 2018 20:33:27 +0000 (22:33 +0200)]
Updated the examples for key exchange.

Related to #102

The crypto_key_exchange() and crypto_x25519() examples no longer use the
return value, which has been deprecated.

Also harmonised the phrasing between the two man pages (I liked one more
than the other).

7 years agoMerge pull request #106 from fscoto/master+return-doc
Loup Vaillant [Wed, 18 Jul 2018 19:44:06 +0000 (21:44 +0200)]
Merge pull request #106 from fscoto/master+return-doc

Deprecate return values in crypto_x25519

7 years agoUnsigned sliding windows for EdDSA verification
Loup Vaillant [Wed, 18 Jul 2018 19:08:40 +0000 (21:08 +0200)]
Unsigned sliding windows for EdDSA verification

Reduces the number of additions in ge_double_scalarmult_vartime().
Verification is now 80% as fast as signing (in naive implementations,
it's only 50% as fast).

It could be even faster, but it's probably not worth the trouble:

- We could precompute the lookup table for the base point instead of
  constructing a cache.  This would save about 8 point additions total,
  at the cost of 64 lines of code just to lay out the 320 precomputed
  constants.

- We could use special, cheaper additions for the precomputed base
  point, at the cost of an additional addition function.

- We could use *signed* sliding windows to further reduce the number of
  additions, at the cost of an additional point subtraction function
  (two if combined with special additions for the base point).  Besides,
  I don't understand how they work.

The low hanging fruits have been taken.  Signature verification is
faster than ever before.  This is good enough.

7 years agoDeprecate return values in crypto_x25519
Fabio Scotoni [Wed, 18 Jul 2018 14:43:23 +0000 (16:43 +0200)]
Deprecate return values in crypto_x25519

This addresses #102.

This also adds a note that the public key function returns nothing, as
is noted on crypto_key_exchange(3monocypher).

7 years agoEasier access to scalar's bits
Loup Vaillant [Wed, 18 Jul 2018 11:41:16 +0000 (13:41 +0200)]
Easier access to scalar's bits

7 years agoMerge pull request #105 from fscoto/master+return-doc
Loup Vaillant [Wed, 18 Jul 2018 11:31:39 +0000 (13:31 +0200)]
Merge pull request #105 from fscoto/master+return-doc

Deprecate return values of crypto_key_exchange()

7 years agoDeprecate return values
Fabio Scotoni [Tue, 17 Jul 2018 13:40:47 +0000 (15:40 +0200)]
Deprecate return values

This addresses #102.

7 years agoShuffled functions around
Loup Vaillant [Sun, 15 Jul 2018 23:48:43 +0000 (01:48 +0200)]
Shuffled functions around

7 years agocosmetic
Loup Vaillant [Sun, 15 Jul 2018 23:47:14 +0000 (01:47 +0200)]
cosmetic

7 years agoCached points for EdDSA point addition
Loup Vaillant [Sun, 15 Jul 2018 23:44:01 +0000 (01:44 +0200)]
Cached points for EdDSA point addition

This saves one multiplication per addition, with a small but noticeable
increase in performance.

7 years agoBetter EdDSA point doubling (from ref10)
Loup Vaillant [Sun, 15 Jul 2018 21:45:59 +0000 (23:45 +0200)]
Better EdDSA point doubling (from ref10)

7 years agoFused doublings in EdDSA double scalarmult
Loup Vaillant [Sun, 15 Jul 2018 13:27:36 +0000 (15:27 +0200)]
Fused doublings in EdDSA double scalarmult

Fusing the two scalar multiplication together halves the number of
required doublings.  The code is just as simple, speeds up signature
verification quite a bit.

7 years agoUse variable time scalarmult for EdDSA checks
Loup Vaillant [Sat, 14 Jul 2018 17:54:54 +0000 (19:54 +0200)]
Use variable time scalarmult for EdDSA checks

Related to #101

General scalar multiplication for EdDSA was only used for checking, and
thus no longer needs to be constant time.  Removing buffer wipes and
using a variable time ladder simplifies and speeds up checks quite a
bit.

7 years agoRestored EdDSA optimisation
Loup Vaillant [Sat, 14 Jul 2018 16:16:42 +0000 (18:16 +0200)]
Restored EdDSA optimisation

The one that was removed for version 2.0.4, because a bogus special
cases caused it to accept forged signature (a big fat vulnerability).

To avoid the vulnerability, this optimisation is only used for signing
and public key generation.  Those never multiply the base point by zero,
and as such should not hit any nasty special case.  More specifically:

- Public key generation works the same as X25519: the scalar is trimmed
  before it is multiplied by the base point.
- Signing multiplies the base point by a nonce, which by construction is
  a random number between 1 and L-1 (L is the order of the curve).

External scrutiny will be needed to confirm this is safe.

Note: signing is now even faster than it was, because multiplying by a
known point (the base point) lets us avoid a conversion and a division.

Related to #101

7 years agoSeparated EdDSA scalarmult internal APIs
Loup Vaillant [Sat, 14 Jul 2018 11:55:59 +0000 (13:55 +0200)]
Separated EdDSA scalarmult internal APIs

- crypto_sign() and crypto_sign() public keys only use
  ge_scalarmult_base().
- crypto_check() only use ge_double_scalarmult_vartime().

They will be optimised separately.

7 years agofree(NULL) was not undefined after all
Loup Vaillant [Sat, 14 Jul 2018 11:06:54 +0000 (13:06 +0200)]
free(NULL) was not undefined after all

Reverts 79f8285ea6cae0db2e906bf3eb65c8fcadabdaf8
This simplifies the test suite a tiny bit.
Fixes #104

7 years agoDocumented EdDSA signature malleability
Loup Vaillant [Mon, 2 Jul 2018 11:29:03 +0000 (13:29 +0200)]
Documented EdDSA signature malleability

Some users tend to rely on security properties that are not provided by
cryptographic signatures.  This has lead to serious problems int the
past, such as BitCoin transaction malleability (a replay attack where
the recipient could repeat a previously existing transaction).

Mitigations for signature malleability are possible, but they're at best
easily misunderstood, and at worst incomplete.  Better warn the users in
the manual than encouraging the reliance on non-standard security
properties.

Fixes #100

7 years agoRemoved debug printf() calls
Loup Vaillant [Sat, 30 Jun 2018 19:09:00 +0000 (21:09 +0200)]
Removed debug printf() calls

7 years agoIntegrated Wicherproof Ed25519 test vectors
Loup Vaillant [Sat, 30 Jun 2018 18:23:55 +0000 (20:23 +0200)]
Integrated Wicherproof Ed25519 test vectors

Those test vectors assume SHA-512, and thus are only activated with the
-DED25519_SHA512 compilation option.

Note the omission of malleability test vectors.  Monocypher will
happily accept signatures even when S is not in canonical form.  This
is contrary to RFC 8032, which requires implementations to check that S
is lower than L.

I believe RFC 8032 is wrong.  Non-malleability means that someone who
only knows the public key, message, and signature, cannot produce
another valid signature.  It does *not* mean there is only one valid
signature.  In fact, when we know the private key, we can produce a
virtually unlimited number of different, valid, canonical signatures.

Like ECDSA, EdDSA uses a nonce.  Unlike ECDSA, that nonce doesn't come
from a random source, it comes from a hash of the message itself.  This
determinism prevents nonce reuse, among other problems.  However,
nothing prevents someone to bypass this rule, and use a random nonce
instead.  This will naturally produce a different, yet valid, signature.

EdDSA signatures are not unique. The difference between this and
malleability is subtle enough that advertising non-malleability will
lead users to believe in uniqueness, and bake that faulty assumption in
their designs, which will then be insecure.

Fixes #99

7 years agoEasier tarball generation
Loup Vaillant [Wed, 27 Jun 2018 09:16:56 +0000 (11:16 +0200)]
Easier tarball generation

- TARBALL_VERSION now defaults to the contents of VERSION.md
- TARBALL_DIR     now defaults to the current directory
- tarball_ignore is now included in the tarball (one can use the tarball
  to generate other tarballs).

7 years agoAdded VERSION file
Loup Vaillant [Wed, 27 Jun 2018 08:51:14 +0000 (10:51 +0200)]
Added VERSION file

7 years agoTypo
Loup Vaillant [Wed, 27 Jun 2018 08:48:44 +0000 (10:48 +0200)]
Typo

7 years ago2.0.4 changelog
Loup Vaillant [Sun, 24 Jun 2018 14:03:35 +0000 (16:03 +0200)]
2.0.4 changelog

7 years agoDon't free() NULL pointers
Loup Vaillant [Sun, 24 Jun 2018 13:58:55 +0000 (15:58 +0200)]
Don't free() NULL pointers

The alloc() function in the test suite unconditionally succeeds when
trying to allocate zero bytes.  It does so by returning NULL right away,
without exiting the program.  This was for portability for platforms
that refuse to allocate zero bytes.

Unfortunately, this meant that the test suite later called free() on
those NULL pointers, which is undefined.  Wrapping free() in a dealloc()
function avoids this error.

7 years agoEdDSA no longer accepts all zero signatures
Loup Vaillant [Sat, 23 Jun 2018 18:34:48 +0000 (20:34 +0200)]
EdDSA no longer accepts all zero signatures

This fixes the critical vulnerability in commit
e4cbf84384ffdce194895078c88680be0c341d76 (compute signatures in
Montgomery space (faster)), somewhere between versions 0.8 and 1.0, and
detected by the tests in the parent commit.

The fix basically reverts the optimisation, effectively halving the
performance of EdDSA.

It appears the conversion to Montgomery space and back didn't handle
every edge case correctly.  That optimisation will be re-introduced once
the issue has been fully understood.  This will probably require expert
advice.

7 years agoAdded anti-forgery tests for EdDSA
Loup Vaillant [Sat, 23 Jun 2018 17:30:01 +0000 (19:30 +0200)]
Added anti-forgery tests for EdDSA

Note how EdDSA fails miserably to reject all-zero signatures.  This is
the first critical vulnerability since 1.0.

7 years agoAdded tests vectors for public key generation
Loup Vaillant [Sat, 23 Jun 2018 13:42:18 +0000 (15:42 +0200)]
Added tests vectors for public key generation

Public key generation was up until now only tested implicitly.  Test
vectors can make us more confident.

7 years agoFixed wrong dependency in the makefile
Loup Vaillant [Tue, 19 Jun 2018 22:58:43 +0000 (00:58 +0200)]
Fixed wrong dependency in the makefile

7 years agoDon't try to malloc() zero bytes
Loup Vaillant [Tue, 19 Jun 2018 22:53:37 +0000 (00:53 +0200)]
Don't try to malloc() zero bytes

Some platforms refuse to allocate zero bytes.  This causes the entire
test suite to fail miserably.

To avoid this, we now fake success, and return the NULL pointer.  It
won't be dereferenced anyway.

7 years agoCorrected failing test on 32-bit systems
Loup Vaillant [Tue, 19 Jun 2018 22:26:41 +0000 (00:26 +0200)]
Corrected failing test on 32-bit systems

When size_t is not uint64_t, converting "negative" size_t integers to
uint64_t yields nonsensical results.  That is, the following isn't
portable:

    size_t   x = 42;
    uint64_t y = -i;

Because y might be missing the high order bits if size_t is smaller than
uint64_t. Instead, we want to convert to a large sized integer *before*
we negate it:

    size_t   x = 42;
    uint64_t y = -(uint64_t)i;

7 years agoChangelog formatting
Loup Vaillant [Mon, 18 Jun 2018 09:21:34 +0000 (11:21 +0200)]
Changelog formatting

7 years agoMerge pull request #97 from MikeA1/patch-1
Loup Vaillant [Mon, 18 Jun 2018 09:07:28 +0000 (11:07 +0200)]
Merge pull request #97 from MikeA1/patch-1

Update makefile

7 years agoTests for crypto_verify*() catch more errors
Loup Vaillant [Sun, 17 Jun 2018 18:05:36 +0000 (20:05 +0200)]
Tests for crypto_verify*() catch more errors

The test had a symmetry that caused them to miss a possible error, where
the implementer would replace an `|` operator by an `&` operator.
Breaking that symmetry allow them to catch that error.

The other errors are caught all the same.

7 years agoFaster crypto_verify*() tests
Loup Vaillant [Sun, 17 Jun 2018 17:47:46 +0000 (19:47 +0200)]
Faster crypto_verify*() tests

There was no need to test every possible value to catch the errors the
tests caugth.  Cutting them down makes the test 64000 times faster,
which matters quite a lot when we run the TIS interpreter.

The new tests catch just as many errors as the old ones.

7 years agoProperly ignore the formal-analysis folder
Loup Vaillant [Sun, 17 Jun 2018 17:30:41 +0000 (19:30 +0200)]
Properly ignore the formal-analysis folder

The way it was done earlier was also ignoring the formal-analysis.sh
script, which was then absent from the tarball releases.