Skip to content

Curve448 primitives - #5105

Closed
mattcaswell wants to merge 75 commits into
openssl:masterfrom
mattcaswell:curve448-primitives
Closed

mattcaswell wants to merge 75 commits into
openssl:masterfrom
mattcaswell:curve448-primitives

Conversation

@mattcaswell

Copy link
Copy Markdown
Member

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
  • documentation is added or updated
  • tests are added or updated

This was referenced Jan 18, 2018
Comment thread crypto/ec/curve448/arch_arm_32/f_impl.h Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor nit, declare i outside of the for loop for pre C99 compatibility. Same in other places.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, sorry missed the 'arm' bit when reviewing this header, should have spotted it was code outside the review of just arch_32.

@mattcaswell mattcaswell added this to the Post 1.1.1 milestone Jan 24, 2018
@mattcaswell

Copy link
Copy Markdown
Member Author

Added a commit to fix an AppVeyor failure. Ping @openssl/committers - still looking for a reviewer for this.

Comment thread crypto/ec/curve448/arch_32/f_impl.c Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to see macro parameters in parentheses

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I replaced this macro with your version.

Comment thread crypto/ec/curve448/arch_32/f_impl.c Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indentation looks wrong

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

Comment thread crypto/ec/curve448/arch_32/f_impl.c Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I like this new for loop syntax...

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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++) {"

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used your version of the macro.

Comment thread crypto/ec/curve448/arch_32/f_impl.h Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use OSSL_NELEM here?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually I just replaced this with NLIMBS which seems more appropriate.

Comment thread crypto/ec/curve448/arch_neon/f_impl.h Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this looks like a strict-aliasing violation

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, that might be better...

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@bernd-edlinger bernd-edlinger Jan 30, 2018 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the keywords always_inline, unused etc, can also be written as __always_inline__, __unused__

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I deleted this file

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't we have a constant_time-function for that?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, and I've already pointed that out in the previous pull request

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Comment thread crypto/ec/curve448/constant_time.h Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

at least this function might collide with one of our constant time functions

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I renamed it

Comment thread crypto/ec/curve448/arch_32/f_impl.c Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of "I_HATE.." we should use "OPENSSL_SMALL_FOOTPRINT"

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually I deleted these and just had the unrolled loop version. I'm not sure why we would ever not want to do that.

@mattcaswell

Copy link
Copy Markdown
Member Author

Updated commits pushed addressing comments so far.

Comment thread crypto/ec/curve448/arch_32/f_impl.c Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, looks good, I would however like to use OPENSSL_SMALL_FOOTPRINT
to Switch to the non-inlined variant.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. Done. I moved this define to curve448utils.h so that it could be picked up by f_generic.c too.

Comment thread crypto/ec/curve448/constant_time.h Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread crypto/ec/curve448/constant_time.h Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and word_t is yet another type.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As above.

Comment thread crypto/ec/curve448/curve448utils.h Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any idea why we are shifting twice?
why 60 bits, and not 63?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm guessing to avoid a warning if it's 32 bit?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't know. Maybe @bitwiseshiftleft can enlighten us.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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".

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we change this condition to be simply:

# if defined(__SIZEOF_INT128__) && __SIZEOF_INT128__==16

what do you think?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works for me. Done.

Comment thread crypto/ec/curve448/curve448utils.h Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why use extern "C" binding?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remnants from the imported code. I deleted all occurrences of this.

Comment thread crypto/ec/curve448/eddsa.c Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we want extra indenting here.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

Comment thread crypto/ec/curve448/f_generic.c Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yet another differnet way of loop unrolling.
this one is limited to clang, so I have to object :)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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; ...

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aehm, correction:

        case 1: _x; (_i)++; continue; \
        case 0: break; \
        } \
     break; \
     } while(1); \
   } while(0)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Comment thread crypto/ec/curve448/arch_32/f_impl.h Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use NLIMBS

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Comment thread crypto/ec/curve448/arch_32/f_impl.h Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Comment thread crypto/ec/curve448/arch_32/f_impl.h Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Loop terminates with unsigned underflow, maybe make i signed?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is not undefined behavior, but may be confusing (to humans).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see an underflow? Loop terminates at i == 0?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, forget it.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now using our own standard constant time code for everything constant time related.

@mattcaswell

Copy link
Copy Markdown
Member Author

New commits pushed addressing all comments.

Comment thread crypto/ec/curve448/curve448utils.h Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe add some braces around _x here?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. I also did the same to the unrolled version

Comment thread crypto/ec/curve448/eddsa.c Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indentation.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

Comment thread crypto/ec/curve448/eddsa.c Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indentation.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

levitte pushed a commit that referenced this pull request Feb 20, 2018
Reviewed-by: Bernd Edlinger <[email protected]>
(Merged from #5105)
levitte pushed a commit that referenced this pull request Feb 20, 2018
Reviewed-by: Bernd Edlinger <[email protected]>
(Merged from #5105)
levitte pushed a commit that referenced this pull request Feb 20, 2018
Reviewed-by: Bernd Edlinger <[email protected]>
(Merged from #5105)
levitte pushed a commit that referenced this pull request Feb 20, 2018
levitte pushed a commit that referenced this pull request Feb 20, 2018
levitte pushed a commit that referenced this pull request Feb 20, 2018
levitte pushed a commit that referenced this pull request Feb 20, 2018
This ensures that this test is run as part of the test suite

Reviewed-by: Bernd Edlinger <[email protected]>
(Merged from #5105)
levitte pushed a commit that referenced this pull request Feb 20, 2018
levitte pushed a commit that referenced this pull request Feb 20, 2018
Reviewed-by: Bernd Edlinger <[email protected]>
(Merged from #5105)
levitte pushed a commit that referenced this pull request Feb 20, 2018
Reviewed-by: Bernd Edlinger <[email protected]>
(Merged from #5105)
levitte pushed a commit that referenced this pull request Feb 20, 2018
Reviewed-by: Bernd Edlinger <[email protected]>
(Merged from #5105)
levitte pushed a commit that referenced this pull request Feb 20, 2018
Reviewed-by: Bernd Edlinger <[email protected]>
(Merged from #5105)
levitte pushed a commit that referenced this pull request Feb 20, 2018
Reviewed-by: Bernd Edlinger <[email protected]>
(Merged from #5105)
levitte pushed a commit that referenced this pull request Feb 20, 2018
Reviewed-by: Bernd Edlinger <[email protected]>
(Merged from #5105)
levitte pushed a commit that referenced this pull request Feb 20, 2018
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)
levitte pushed a commit that referenced this pull request Feb 20, 2018
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)
levitte pushed a commit that referenced this pull request Feb 20, 2018
levitte pushed a commit that referenced this pull request Feb 20, 2018
levitte pushed a commit that referenced this pull request Feb 20, 2018
Instead we should use the standard OpenSSL constant time routines.

Reviewed-by: Bernd Edlinger <[email protected]>
(Merged from #5105)
levitte pushed a commit that referenced this pull request Feb 20, 2018
Reviewed-by: Bernd Edlinger <[email protected]>
(Merged from #5105)
levitte pushed a commit that referenced this pull request Feb 20, 2018
levitte pushed a commit that referenced this pull request Feb 20, 2018
Reviewed-by: Bernd Edlinger <[email protected]>
(Merged from #5105)
levitte pushed a commit that referenced this pull request Feb 20, 2018
Reviewed-by: Bernd Edlinger <[email protected]>
(Merged from #5105)
levitte pushed a commit that referenced this pull request Feb 20, 2018
levitte pushed a commit that referenced this pull request Feb 20, 2018
levitte pushed a commit that referenced this pull request Feb 20, 2018
Reviewed-by: Bernd Edlinger <[email protected]>
(Merged from #5105)
levitte pushed a commit that referenced this pull request Feb 20, 2018
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)
levitte pushed a commit that referenced this pull request Feb 20, 2018
Reviewed-by: Bernd Edlinger <[email protected]>
(Merged from #5105)
levitte pushed a commit that referenced this pull request Feb 20, 2018
levitte pushed a commit that referenced this pull request Feb 20, 2018
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants