boringssl/crypto/fipsmodule/ec
David Benjamin 23e1a1f2d3 Test and fix an ABI issue with small parameters.
Calling conventions must specify how to handle arguments smaller than a
machine word. Should the caller pad them up to a machine word size with
predictable values (zero/sign-extended), or should the callee tolerate
an arbitrary bit pattern?

Annoyingly, I found no text in either SysV or Win64 ABI documentation
describing any of this and resorted to experiment. The short answer is
that callees must tolerate an arbitrary bit pattern on x86_64, which
means we must test this. See the comment in abi_test::internal::ToWord
for the long answer.

CHECK_ABI now, if the type of the parameter is smaller than
crypto_word_t, fills the remaining bytes with 0xaa. This is so the
number is out of bounds for code expecting either zero or sign
extension. (Not that crypto assembly has any business seeing negative
numbers.)

Doing so reveals a bug in ecp_nistz256_ord_sqr_mont. The rep parameter
is typed int, but the code expected uint64_t. In practice, the compiler
will always compile this correctly because:

- On both Win64 and SysV, rep is a register parameter.

- The rep parameter is always a constant, so the compiler has no reason
  to leave garbage in the upper half.

However, I was indeed able to get a bug out of GCC via:

  uint64_t foo = (1ull << 63) | 2;  // Some global the compiler can't
                                    // prove constant.
  ecp_nistz256_ord_sqr_mont(res, a, foo >> 1);

Were ecp_nistz256_ord_sqr_mont a true int-taking function, this would
act like ecp_nistz256_ord_sqr_mont(res, a, 1). Instead, it hung. Fix
this by having it take a full-width word.

This mess has several consequences:

- ABI testing now ideally needs a functional testing component to fully cover
  this case. A bad input might merely produce the wrong answer. Still,
  this is fairly effective as it will cause most code to either segfault
  or loop forever. (Not the enc parameter to AES however...)

- We cannot freely change the type of assembly function prototypes. If the
  prototype says int or unsigned, it must be ignoring the upper half and
  thus "fixing" it to size_t cannot have handled the full range. (Unless
  it was simply wrong of the parameter is already bounded.) If the
  prototype says size_t, switching to int or unsigned will hit this type
  of bug. The former is a safer failure mode though.

- The simplest path out of this mess: new assembly code should *only*
  ever take word-sized parameters. This is not a tall order as the bad
  parameters are usually ints that should have been size_t.

Calling conventions are hard.

Change-Id: If8254aff8953844679fbce4bd3e345e5e2fa5213
Reviewed-on: https://boringssl-review.googlesource.com/c/34627
Commit-Queue: David Benjamin <davidben@google.com>
Reviewed-by: Adam Langley <agl@google.com>
2019-01-28 21:09:40 +00:00
..
asm Test and fix an ABI issue with small parameters. 2019-01-28 21:09:40 +00:00
ec_key.c Add EC_KEY_key2buf for OpenSSL compatibility 2019-01-03 16:32:21 +00:00
ec_montgomery.c
ec_scalar_base_mult_tests.txt
ec_test.cc
ec.c Add some Node compatibility functions. 2019-01-25 16:50:30 +00:00
felem.c
internal.h
make_ec_scalar_base_mult_tests.go
make_p256-x86_64-table.go Remove pointer cast in P-256 table. 2019-01-15 00:16:17 +00:00
make_p256-x86_64-tests.go Refresh p256-x86_64_tests.txt. 2019-01-02 23:29:31 +00:00
oct.c
p224-64.c Merge P-224 contract into serialisation. 2018-11-14 23:47:13 +00:00
p256-x86_64_test.cc Be less clever with CHECK_ABI. 2019-01-03 21:02:24 +00:00
p256-x86_64_tests.txt Refresh p256-x86_64_tests.txt. 2019-01-02 23:29:31 +00:00
p256-x86_64-table.h Remove pointer cast in P-256 table. 2019-01-15 00:16:17 +00:00
p256-x86_64.c Remove pointer cast in P-256 table. 2019-01-15 00:16:17 +00:00
p256-x86_64.h Test and fix an ABI issue with small parameters. 2019-01-28 21:09:40 +00:00
scalar.c
simple_mul.c
simple.c
util.c
wnaf.c