void GFp_p256_scalar_mul_mont(ScalarMont r, const ScalarMont a, const ScalarMont b) { static const BN_ULONG N[] = { TOBN(0xf3b9cac2, 0xfc632551), TOBN(0xbce6faad, 0xa7179e84), TOBN(0xffffffff, 0xffffffff), TOBN(0xffffffff, 0x00000000), }; static const BN_ULONG N_N0[] = { BN_MONT_CTX_N0(0xccd1c8aa, 0xee00bc4f) }; /* XXX: Inefficient. TODO: optimize with dedicated multiplication routine. */ GFp_bn_mul_mont(r, a, b, N, N_N0, P256_LIMBS); }
BN_free(&qm1); BN_free(&lcm); BN_free(&gcd); BN_free(&de); BN_free(&dmp1); BN_free(&dmq1); BN_free(&iqmp_times_q); BN_CTX_free(ctx); return ok; } /* This is the product of the 132 smallest odd primes, from 3 to 751. */ static const BN_ULONG kSmallFactorsLimbs[] = { TOBN(0xc4309333, 0x3ef4e3e1), TOBN(0x71161eb6, 0xcd2d655f), TOBN(0x95e2238c, 0x0bf94862), TOBN(0x3eb233d3, 0x24f7912b), TOBN(0x6b55514b, 0xbf26c483), TOBN(0x0a84d817, 0x5a144871), TOBN(0x77d12fee, 0x9b82210a), TOBN(0xdb5b93c2, 0x97f050b3), TOBN(0x4acad6b9, 0x4d6c026b), TOBN(0xeb7751f3, 0x54aec893), TOBN(0xdba53368, 0x36bc85c4), TOBN(0xd85a1b28, 0x7f5ec78e), TOBN(0x2eb072d8, 0x6b322244), TOBN(0xbba51112, 0x5e2b3aea), TOBN(0x36ed1a6c, 0x0e2486bf), TOBN(0x5f270460, 0xec0c5727), 0x000017b1 }; DEFINE_LOCAL_DATA(BIGNUM, g_small_factors) { out->d = (BN_ULONG *) kSmallFactorsLimbs; out->top = OPENSSL_ARRAY_SIZE(kSmallFactorsLimbs); out->dmax = out->top; out->neg = 0;
// kBoringSSLRSASqrtTwo is the BIGNUM representation of ⌊2¹⁵³⁵×√2⌋. This is // chosen to give enough precision for 3072-bit RSA, the largest key size FIPS // specifies. Key sizes beyond this will round up. // // To verify this number, check that n² < 2³⁰⁷¹ < (n+1)², where n is value // represented here. Note the components are listed in little-endian order. Here // is some sample Python code to check: // // >>> TOBN = lambda a, b: a << 32 | b // >>> l = [ <paste the contents of kSqrtTwo> ] // >>> n = sum(a * 2**(64*i) for i, a in enumerate(l)) // >>> n**2 < 2**3071 < (n+1)**2 // True const BN_ULONG kBoringSSLRSASqrtTwo[] = { TOBN(0xdea06241, 0xf7aa81c2), TOBN(0xf6a1be3f, 0xca221307), TOBN(0x332a5e9f, 0x7bda1ebf), TOBN(0x0104dc01, 0xfe32352f), TOBN(0xb8cf341b, 0x6f8236c7), TOBN(0x4264dabc, 0xd528b651), TOBN(0xf4d3a02c, 0xebc93e0c), TOBN(0x81394ab6, 0xd8fd0efd), TOBN(0xeaa4a089, 0x9040ca4a), TOBN(0xf52f120f, 0x836e582e), TOBN(0xcb2a6343, 0x31f3c84d), TOBN(0xc6d5a8a3, 0x8bb7e9dc), TOBN(0x460abc72, 0x2f7c4e33), TOBN(0xcab1bc91, 0x1688458a), TOBN(0x53059c60, 0x11bc337b), TOBN(0xd2202e87, 0x42af1f4e), TOBN(0x78048736, 0x3dfa2768), TOBN(0x0f74a85e, 0x439c7b4a), TOBN(0xa8b1fe6f, 0xdc83db39), TOBN(0x4afc8304, 0x3ab8a2c3), TOBN(0xed17ac85, 0x83339915), TOBN(0x1d6f60ba, 0x893ba84c), TOBN(0x597d89b3, 0x754abe9f), TOBN(0xb504f333, 0xf9de6484), }; const size_t kBoringSSLRSASqrtTwoLen = OPENSSL_ARRAY_SIZE(kBoringSSLRSASqrtTwo); // generate_prime sets |out| to a prime with length |bits| such that |out|-1 is
typedef GFp_Limb Elem[P384_LIMBS]; typedef GFp_Limb ScalarMont[P384_LIMBS]; typedef GFp_Limb Scalar[P384_LIMBS]; /* Prototypes to avoid -Wmissing-prototypes warnings. */ void GFp_p384_elem_add(Elem r, const Elem a, const Elem b); void GFp_p384_elem_mul_mont(Elem r, const Elem a, const Elem b); void GFp_p384_scalar_inv_to_mont(ScalarMont r, const Scalar a); void GFp_p384_scalar_mul_mont(ScalarMont r, const ScalarMont a, const ScalarMont b); static const BN_ULONG Q[P384_LIMBS] = { TOBN(0x00000000, 0xffffffff), TOBN(0xffffffff, 0x00000000), TOBN(0xffffffff, 0xfffffffe), TOBN(0xffffffff, 0xffffffff), TOBN(0xffffffff, 0xffffffff), TOBN(0xffffffff, 0xffffffff), }; static const BN_ULONG N[P384_LIMBS] = { TOBN(0xecec196a, 0xccc52973), TOBN(0x581a0db2, 0x48b0a77a), TOBN(0xc7634d81, 0xf4372ddf), TOBN(0xffffffff, 0xffffffff), TOBN(0xffffffff, 0xffffffff), TOBN(0xffffffff, 0xffffffff), };
#include <openssl/bn.h> #include "internal.h" #if BN_BITS2 == 32 #define TOBN(lo, hi) lo, hi #elif BN_BITS2 == 64 #define TOBN(lo, hi) ((BN_ULONG)hi << 32 | lo) #else #error "unsupported BN_BITS2" #endif static const BN_ULONG dh1024_160_p[] = { TOBN(0x2E4A4371, 0xDF1FB2BC), TOBN(0x6D4DA708, 0xE68CFDA7), TOBN(0x365C1A65, 0x45BF37DF), TOBN(0x0DC8B4BD, 0xA151AF5F), TOBN(0xF55BCCC0, 0xFAA31A4F), TOBN(0xE5644738, 0x4EFFD6FA), TOBN(0x219A7372, 0x98488E9C), TOBN(0x90C4BD70, 0xACCBDD7D), TOBN(0xD49B83BF, 0x24975C3C), TOBN(0xA9061123, 0x13ECB4AE), TOBN(0x2EE652C0, 0x9838EF1E), TOBN(0x75A23D18, 0x6073E286), TOBN(0x52D23B61, 0x9A6A9DCA), TOBN(0xFB06A3C6, 0x52C99FBC), TOBN(0xAE5D54EC, 0xDE92DE5E), TOBN(0xA080E01D, 0xB10B8F96), }; static const BN_ULONG dh1024_160_g[] = { TOBN(0x22B3B2E5, 0x855E6EEB), TOBN(0xF97C2A24, 0x858F4DCE), TOBN(0x18D08BC8, 0x2D779D59), TOBN(0x8E73AFA3, 0xD662A4D1), TOBN(0x69B6A28A, 0x1DBF0A01), TOBN(0x7A091F53, 0xA6A24C08), TOBN(0x63F80A76, 0x909D0D22), TOBN(0xB9A92EE1, 0xD7FBD7D3), TOBN(0x9E2749F4, 0x5E91547F), TOBN(0xB01B886A, 0x160217B4), TOBN(0x5504F213, 0x777E690F), TOBN(0x5C41564B, 0x266FEA1E),