summaryrefslogtreecommitdiff
path: root/lib/libcrypto/bn
AgeCommit message (Collapse)Author
2023-04-25GF2m bites the dust. It won't be missed.Theo Buehler
2023-04-25BN_RECP_CTX moves to internalTheo Buehler
2023-04-25Remove the horror show that is bn_nist and ecp_nistTheo Buehler
This code is full of problematic C and is also otherwise of questionable quality. It is far from constant time and jsing informs me it also isn't faster. Good riddance.
2023-04-25Remove the no longer used BN_MONT_CTX_init()Theo Buehler
2023-04-25Move a few now internal prototypes to bn_local.hTheo Buehler
2023-04-25Remove old BN_one/BN_zero compat stuffTheo Buehler
ok jsing
2023-04-25Remove X9.31 supportTheo Buehler
ok jsing
2023-04-25Remove the no longer used BN_CTX_init()Theo Buehler
ok jsing
2023-04-25Add endbr64 where needed by inspection. Passes regresson tests.Theo de Raadt
ok jsing, and kind of tb an earlier version
2023-04-22Improve bn_montgomery_multiply_words()Joel Sing
Pull a number of invariants into variables, which avoids repeated loading from memory on architectures where sufficient registers are available. Also keep track of the per-iteration carry in a variable, rather than unnecessarily reading from and writing to memory. This gives a reasonable performance gain on some architectures (e.g. armv7)
2023-04-19Rename Hex array to hex_digits.Joel Sing
ok tb@
2023-04-19Move the BN_bn2bin()/BN_bin2bn() family to bn_convert.cJoel Sing
2023-04-19Reorder functions.Joel Sing
No functional change.
2023-04-19Move BN_options() from bn_convert.c to bn_lib.cJoel Sing
2023-04-19unifdef BN_RECURSIONJoel Sing
This removes a bunch of incomplete and scary code, which potentially leaks secrets and is not constant time. A performance gain is achieved on arm64 for sizes that we care about, while a minimal decrease in performance is noted for larger sizes on some other platforms. While we will potentially reimplement Karatsuba (or Toom-Cook) at a later date, it will be easier and safer to do it from a clean slate. ok tb@
2023-04-17Tweak indent and use named registers.Joel Sing
No functional change.
2023-04-17Move BN_bn2mpi()/BN_mpi2bn() into bn_convert.cJoel Sing
2023-04-16Mark X9.31 BN API for removalTheo Buehler
This supports a mostly forgotten, seemingly unused and long retired standard. No need for this in our public API Dyson sphere. ok jsing
2023-04-16The BN reciprocal API will also become internal-onlyTheo Buehler
This is unused outside of the library and could do with some reworking. That's easier without having to care about outside consumers. ok jsing
2023-04-16Various BN*init() will be removed from the public APITheo Buehler
With the corresponding structs now being opaque, the only thing they are good for outside the library are memory leaks. They will be removed completely or become internal only. ok jsing
2023-04-16Mark public bn_nist and ec_nist API for removalTheo Buehler
The faster nist code is rife with problematic C. While this is generally considered to be a pleonasm nowadays, here it specifically refers to aliasing issues and other flavors of undefined behavior. With compilers and standardization committees becoming seemingly more determined about making C even more unusable than it already is, this code has resulted in miscompilations and generally is a target rich environment for fuzzers to feast on. We're better off without it. Go look while it's still there. It's some of the very worst we have to offer. ok jsing
2023-04-15Remove now unused GF2m perlasm generatorsTheo Buehler
2023-04-14Rename the largely misnamed bn_print.c to bn_convert.cJoel Sing
This file primarily contains the various BN_bn2*() and BN_*2bn() functions (along with BN_print() and BN_options()). More function shuffling will follow. Discussed with tb@
2023-04-14Provide and use bn_copy_words() in BN_copy().Joel Sing
This is simpler than the current code, while still being well optimised by compilers, across a range of architectures. In many cases we even get a performance gain for the BN sizes that we primarily care about. Joint work with tb@
2023-04-11Add a new implementation of BN_mod_sqrt()Theo Buehler
This is a reimplementation from scratch of the Tonelli-Shanks algorithm based on Henri Cohen "A Course in Computational Algebraic Number Theory", Springer GTM 138, section 1.5.1. It is API compatible with the previous implementation, so no documentation change is required. Contrary to the old implementation, this does not have any infinite loops and has various additional sanity checks to prevent misbehavior in case the input modulus is not a prime. It contains extensive comments and the individual parts of the algorithm are split into digestible chunks instead of having one huge function. One difference of note is that it BN_mod_sqrt() now always returns the smaller of the two possible answers. In other words, while its core is non-deterministic, its answer is not. ok jsing
2023-04-09Remove some doubled empty linesTheo Buehler
2023-04-07bn_mont: fix typo in comment divisable -> divisibleTheo Buehler
2023-04-03Compress euclid() a littleTheo Buehler
This function is spread out over way too many lines and has too much repetition. Once this is made a little more compact, it becomes clearer that this is a somewhat obfuscated version of binary gcd (it is not constant time therefore cryptographically unsound. It is not used internally). This will likely go away later. ok jsing
2023-04-01Pull static const data out of BN_value_one()Theo Buehler
Also use C99 initializers for readability. discussed with jsing
2023-04-01Indent labelsTheo Buehler
2023-04-01Group the non-constant time gcd functions togetherTheo Buehler
The only consumer of euclid() is BN_gcd(), which, in turn is only used by BN_gcd_nonct(). Group them together rather than having parts of the constant time implementation separate them. This moves two functions to a different place in the file.
2023-03-31Copy BN_FLG flags in BN_copy()Theo Buehler
BN_copy() forgot to copy the flags from the source to the target. Fix this by copying the flags. In fact, only copy BN_FLG_CONSTTIME since propagating BN_FLG_MALLOCED and BN_FLG_STATIC_DATA is wrong. Ignore the BN_FLG_FREE flag "used for debugging" which of course means "unused" like a lot of other debug code that somehow ended up in public headers. Also: make BN_FLG_CONSTTIME sticky on the target, i.e., don't clear the flag when copying from a non-constant time BIGNUM to a constant time one for the following reason: if a is constant time, BN_sqr(a, a, ctx) would use a BIGNUM without the flag internally, then copy the result to a in which process a would lose its constant time flag. Fixing this would be a lot of pointless work since someone had the good sense of not relying on a fragile flag for something this important. Rather, libcrypto always uses the constant time paths instead of the faster, cryptographically inadequate paths. Before this was changed, this was a pretty bad bug. The RSA code uses the horrible BN_with_flags() function to create local versions of the private moduli and set BN_FLG_CONSTTIME on them. If the RSA_FLAG_CACHE_PRIVATE for caching moduli is set on the RSA, which it is by default, it attempts to set these constant time versions on the RSA's internal Montgomery contexts. Since it is called BN_MONT_CTX_set(), the setter doesn't set a BIGNUM on the BN_MONT_CTX, rather it copies it over, losing the BN_FLG_CONSTTIME flag in the process and make all the horrible leaky RSA code leak some more. Good job. This is all harmless and is mostly a cosmetic fix. BN_FLG_CONSTTIME should be removed internally. It will be kept since various language bindings of course picked it up and expose it. ok beck jsing
2023-03-30Call bn_copy() unconditionally in BN_mul() and BN_sqr()Theo Buehler
bn_copy() does the right thing if source and target are the same, so there is no need for an additional check. Requested by jsing
2023-03-30Rework BN_exp() a bitTheo Buehler
This mostly only cleans up the mess that it was - which doesn't stand out because of the horror that lurks in the rest of this file. It avoids copying the partial calculation out on error and does away with some other weirdness. with/ok jsing
2023-03-27Replace the remaining BN_copy() with bn_copy()Theo Buehler
ok jsing
2023-03-27Convert BN_copy() with missing error checks to bn_copy()Theo Buehler
ok jsing
2023-03-27Convert BN_copy() with explicit comparison against NULL to bn_copy()Theo Buehler
ok jsing
2023-03-27Use bn_copy() rather than inlining itTheo Buehler
ok jsing
2023-03-27Drop unnecessary parentheses.Theo Buehler
ok jsing
2023-03-27Convert bn_nist.c to BN_copy()Theo Buehler
Like everything else in this file, the use of BN_copy() needs to be ... special. Simplify using the new bn_copy(). ok jsing
2023-03-27Add bn_copy(), a sane wrapper of BN_copy() for internal useTheo Buehler
ok jsing
2023-03-26Minor whitespace tidyingTheo Buehler
2023-03-26Make several calls to BN_nnmod() unconditionalTheo Buehler
This removes a potential branch in a sensitive function and makes the code a lot simpler. It is a really bad idea optimize here for what davidben aptly calls "calculator" purposes. ok jsing
2023-03-26Correctly reduce negative inpot to BN_mod_exp2_mont()Theo Buehler
Negative bases could result in a negative modulus being returned. This is not strictly speaking incorrect but slightly surprising. This is all a consequence of the shortcut of defining BN_mod() as a macro using BN_div(). Fixes ossfuzz #55997 ok jsing
2023-03-26bn_prime.pl: fix shebang and a couple more whitespace tweaksTheo Buehler
2023-03-25Use strict and warningsTheo Buehler
2023-03-25Make an attempt at reducing the eyebleed in bn_prime.plTheo Buehler
Use a style more resembling KNF and drop lots of parentheses. Simplify a few things. No change in generated output on success.
2023-03-25Use Eric Young's usual license in the proper place rather than a weirdTheo Buehler
commented-out license stub in a HERE document.
2023-03-25Add RCSIDTheo Buehler
2023-03-25Add checks to ensure the uint16_t array isn't overflowed when thisTheo Buehler
script is run. This is more of an issue with uint16_t now than it was with prime_t aka BN_ULONG before r1.6.