Repository navigation
Curve448 primitives - #5105
Curve448 primitives#5105mattcaswell wants to merge 75 commits into
Conversation
There was a problem hiding this comment.
Minor nit, declare i outside of the for loop for pre C99 compatibility. Same in other places.
There was a problem hiding this comment.
Right - likely there are quite a few issues in the non 32-bit reference code (basically anything in one of the arch* directories that isn't arch_32). As I said in my intro to this, none of that code is actually used right now, and I simply imported it from Mike Hamburg's original contribution so we have the ability to hook it up later.
There was a problem hiding this comment.
Yep, sorry missed the 'arm' bit when reviewing this header, should have spotted it was code outside the review of just arch_32.
|
Added a commit to fix an AppVeyor failure. Ping @openssl/committers - still looking for a reviewer for this. |
There was a problem hiding this comment.
I'd like to see macro parameters in parentheses
There was a problem hiding this comment.
I replaced this macro with your version.
There was a problem hiding this comment.
I'm not sure I like this new for loop syntax...
There was a problem hiding this comment.
It is as written in the original implementation by Mike Hamburg. We could choose to rewrite it anyway we like. Do you have any ideas about how else to do it? One of the problems of putting code blocks in a macro like this is that it makes it difficult to run through a debugger.
There was a problem hiding this comment.
Yes, I just saw the ugliness so far, but debugging must be near impossible this way.
I think this "FOR_LIMB(i, 0, 8, {" does not add much value over "for (i=0; i<8; i++) {"
There was a problem hiding this comment.
Well I think the justification for it is performance, i.e. we normally use this version of the macro:
# define REPEAT8(_x) _x _x _x _x _x _x _x _x
# define FOR_LIMB(_i,_start,_end,_x) do { _i=_start; REPEAT8( if (_i<_end) { _x; } _i++;) } while (0)
Which is written specifically to avoid a loop. The original code has been significantly optimised by Mike. During importing the code I was trying to avoid doing anything that was going to break that optimisation.
There was a problem hiding this comment.
Hmm....
This looks more like a duff's device
# define FOR_LIMB(_i, _start, _end, _x) do { (_i) = (_start); switch((_end)-(_start)) { case 8: _x; (_i)++; case 7: _x; (_i)++; .... case 1: _x; (_i)++; } } while(0)
There was a problem hiding this comment.
I used your version of the macro.
There was a problem hiding this comment.
Actually I just replaced this with NLIMBS which seems more appropriate.
There was a problem hiding this comment.
this looks like a strict-aliasing violation
There was a problem hiding this comment.
The comments in the architecture specific files except anything inside arch_32 have barely been touched by my at all. They are not used or compiled. I only wanted to import them because I thought we would probably integrate them at a later stage. If that is considered a blocker for review I could remove these files completely (but then we would probably add them back in again at some later time).
There was a problem hiding this comment.
yes, that might be better...
There was a problem hiding this comment.
I removed this file. It still gets imported in the original commit, and then gets deleted again. This means we can just resurrect the code easily without having to worry about the code's provenance/CLAs etc.
There was a problem hiding this comment.
the keywords always_inline, unused etc, can also be written as __always_inline__, __unused__
There was a problem hiding this comment.
Don't we have a constant_time-function for that?
There was a problem hiding this comment.
Yes, and I've already pointed that out in the previous pull request
There was a problem hiding this comment.
I deleted this file, although this function appears elsewhere so I guess the same comment applies. As @kroeckx mentioned he previously raised this. My response at that time was:
The types aren't quite the same, so this is probably a non-trivial exercise.
i.e. it is a non-trivial exercise to merge them (really they are different functions because of the different types)
There was a problem hiding this comment.
at least this function might collide with one of our constant time functions
There was a problem hiding this comment.
instead of "I_HATE.." we should use "OPENSSL_SMALL_FOOTPRINT"
There was a problem hiding this comment.
Actually I deleted these and just had the unrolled loop version. I'm not sure why we would ever not want to do that.
|
Updated commits pushed addressing comments so far. |
There was a problem hiding this comment.
Thanks, looks good, I would however like to use OPENSSL_SMALL_FOOTPRINT
to Switch to the non-inlined variant.
There was a problem hiding this comment.
Ok. Done. I moved this define to curve448utils.h so that it could be picked up by f_generic.c too.
There was a problem hiding this comment.
Hmm....
big_register_t is likely not the effective type of the object pointed to with a, and b.
Thus this seems to be in violation of the strict-aliasing rules.
There was a problem hiding this comment.
Hmmm. Nasty - could be tricky to fix. I decided to bite the bullet and rip out all the curve448 specific constant time stuff and use our standard calls. It may result in some performance degradation, but I guess we worry about that later.
There was a problem hiding this comment.
and word_t is yet another type.
There was a problem hiding this comment.
any idea why we are shifting twice?
why 60 bits, and not 63?
There was a problem hiding this comment.
I'm guessing to avoid a warning if it's 32 bit?
There was a problem hiding this comment.
Don't know. Maybe @bitwiseshiftleft can enlighten us.
There was a problem hiding this comment.
This is not working for non-GNU (or non-GNU-alikes) or elderly GNU compilers. Well, not this #if specifically, but what it enables, __int128 few lines below. I'm not really clear on which type is officially supported as builtin in long term, [and there were some changes,] but for poly1305 I've chosen __int128 alone, i.e. unsigned __int128 and not __uint128_t. Well, it doesn't mean it's the one, but it served well so far. As for pre-processor condition poly1305 uses
# if !defined(PEDANTIC) && \
(defined(__SIZEOF_INT128__) && __SIZEOF_INT128__==16) && \
(defined(__SIZEOF_LONG__) && __SIZEOF_LONG__==8)
with rationale that it filters out non-GNU (and non-alikes) as well as elderly GNU.
Just in case, "not working" means "actually tried and confirmed".
There was a problem hiding this comment.
Either use @dot-asm's suggestion for the preprocessor condition or
just take this part out for the moment, C448_WORD_BITS = 32 should always work.
There was a problem hiding this comment.
I think we change this condition to be simply:
# if defined(__SIZEOF_INT128__) && __SIZEOF_INT128__==16
what do you think?
There was a problem hiding this comment.
why use extern "C" binding?
There was a problem hiding this comment.
Since this is an internal only header, I see little point. We do seem to be doing it for some of the other internal headers, but clearly not all.
There was a problem hiding this comment.
Remnants from the imported code. I deleted all occurrences of this.
There was a problem hiding this comment.
we want extra indenting here.
There was a problem hiding this comment.
yet another differnet way of loop unrolling.
this one is limited to clang, so I have to object :)
There was a problem hiding this comment.
how about this:
#define FOR_LIMB(_i, _start, _end, _x) \
do { \
(_i) = (_start); \
do { \
switch((_end) - (_i)) { \
default: \
case 8: _x; (_i)++; /* fall through */ \
case 7: _x; (_i)++; /* fall through */ \
case 6: _x; (_i)++; /* fall through */ \
case 5: _x; (_i)++; /* fall through */ \
case 4: _x; (_i)++; /* fall through */ \
case 3: _x; (_i)++; /* fall through */ \
case 2: _x; (_i)++; /* fall through */ \
case 1: _x; (_i)++; continue; \
case 0: break; \
} \
} while(0); \
} while(0)
could translate
UNROLL while (fill < LIMB_PLACE_VALUE(LIMBPERM(i)) && j < nbytes) {
to
FOR_LIMB(j, j, nbytes, { if (fill < LIMB_PLACE_VALUE(LIMBPERM(i))) break; ...
There was a problem hiding this comment.
Aehm, correction:
case 1: _x; (_i)++; continue; \
case 0: break; \
} \
break; \
} while(1); \
} while(0)
There was a problem hiding this comment.
Loop terminates with unsigned underflow, maybe make i signed?
There was a problem hiding this comment.
it is not undefined behavior, but may be confusing (to humans).
There was a problem hiding this comment.
I don't see an underflow? Loop terminates at i == 0?
There was a problem hiding this comment.
I can't find the previous comment about this anymore, probably because it was on a file that's now deleted.
It's true that we currently don't have a version that takes a uint32_t (and uint64_t), but we've already added an uint64_t of the msb function, it shouldn't be that hard to add a few more functions.
There was a problem hiding this comment.
Now using our own standard constant time code for everything constant time related.
|
New commits pushed addressing all comments. |
There was a problem hiding this comment.
maybe add some braces around _x here?
There was a problem hiding this comment.
Done. I also did the same to the unrolled version
Reviewed-by: Bernd Edlinger <[email protected]> (Merged from #5105)
Reviewed-by: Bernd Edlinger <[email protected]> (Merged from #5105)
Reviewed-by: Bernd Edlinger <[email protected]> (Merged from #5105)
Reviewed-by: Bernd Edlinger <[email protected]> (Merged from #5105)
Reviewed-by: Bernd Edlinger <[email protected]> (Merged from #5105)
Reviewed-by: Bernd Edlinger <[email protected]> (Merged from #5105)
This ensures that this test is run as part of the test suite Reviewed-by: Bernd Edlinger <[email protected]> (Merged from #5105)
Reviewed-by: Bernd Edlinger <[email protected]> (Merged from #5105)
Reviewed-by: Bernd Edlinger <[email protected]> (Merged from #5105)
Reviewed-by: Bernd Edlinger <[email protected]> (Merged from #5105)
Reviewed-by: Bernd Edlinger <[email protected]> (Merged from #5105)
Reviewed-by: Bernd Edlinger <[email protected]> (Merged from #5105)
Reviewed-by: Bernd Edlinger <[email protected]> (Merged from #5105)
Reviewed-by: Bernd Edlinger <[email protected]> (Merged from #5105)
Remove all architecture specific files except for the reference arch_32 version. These files provide archicture specific performance optimisation. However they have not been integrated yet. In order to avoid review issues they are removed for now. They may be reintroduced at a later time. Reviewed-by: Bernd Edlinger <[email protected]> (Merged from #5105)
We already have a constant_time_select() function so, to avoid confusion/clashing we shouldn't have a second one. Reviewed-by: Bernd Edlinger <[email protected]> (Merged from #5105)
Reviewed-by: Bernd Edlinger <[email protected]> (Merged from #5105)
Reviewed-by: Bernd Edlinger <[email protected]> (Merged from #5105)
Instead we should use the standard OpenSSL constant time routines. Reviewed-by: Bernd Edlinger <[email protected]> (Merged from #5105)
Reviewed-by: Bernd Edlinger <[email protected]> (Merged from #5105)
Reviewed-by: Bernd Edlinger <[email protected]> (Merged from #5105)
Reviewed-by: Bernd Edlinger <[email protected]> (Merged from #5105)
Reviewed-by: Bernd Edlinger <[email protected]> (Merged from #5105)
Reviewed-by: Bernd Edlinger <[email protected]> (Merged from #5105)
Reviewed-by: Bernd Edlinger <[email protected]> (Merged from #5105)
Reviewed-by: Bernd Edlinger <[email protected]> (Merged from #5105)
We removed various platform specific optimisation files in an earlier commit. The vector code was related to that and therefore is no longer required. It may be resurrected at a later point if we reintroduce the opimtisations. Reviewed-by: Bernd Edlinger <[email protected]> (Merged from #5105)
Reviewed-by: Bernd Edlinger <[email protected]> (Merged from #5105)
Reviewed-by: Bernd Edlinger <[email protected]> (Merged from #5105)
The original curve448 code was templated to allow for a 25519 implementation. We've just imported the 448 stuff - but a remnant of the original templated approach remained. This just simplifies that. Reviewed-by: Bernd Edlinger <[email protected]> (Merged from #5105)
This adds all the primitives required for Curve448 support (X448 and Ed448).
This is based heavily on original work by Mike Hamburg (thanks @bitwiseshiftleft) and contributed under CLA.
This is code that originally appeared in #4829, but I've pulled out just the primitives (i.e. none of the EVP changes). The EVP layer will come in a different PR once this is in. I've also stripped out all commits that were updating the temporary Makefile I had in #4829 , so that doesn't appear here.
At the moment only the 32-bit reference code is used. There are a number of files for other architectures that I have imported but done very little else with at this stage. Again later PRs can look at integrating the platform specific code in.
Checklist