|
| 1 | +/* Copyright 2016 Brian Smith. |
| 2 | + * |
| 3 | + * Permission to use, copy, modify, and/or distribute this software for any |
| 4 | + * purpose with or without fee is hereby granted, provided that the above |
| 5 | + * copyright notice and this permission notice appear in all copies. |
| 6 | + * |
| 7 | + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES |
| 8 | + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 9 | + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY |
| 10 | + * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 11 | + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION |
| 12 | + * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
| 13 | + * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ |
| 14 | + |
| 15 | +#include "../../limbs/limbs.h" |
| 16 | + |
| 17 | +#include "../bn/internal.h" |
| 18 | +#include "../../internal.h" |
| 19 | + |
| 20 | +#include "../../limbs/limbs.inl" |
| 21 | + |
| 22 | +#define P521_LIMBS ((521u + LIMB_BITS - 1u)/ LIMB_BITS) |
| 23 | +#if defined(OPENSSL_64_BIT) |
| 24 | + |
| 25 | +static const BN_ULONG Q[P521_LIMBS] = { |
| 26 | + 0xffffffffffffffff, 0xffffffffffffffff, 0xffffffffffffffff, |
| 27 | + 0xffffffffffffffff, 0xffffffffffffffff, 0xffffffffffffffff, |
| 28 | + 0xffffffffffffffff, 0xffffffffffffffff, 0x00000000000001ff |
| 29 | +}; |
| 30 | + |
| 31 | +static const BN_ULONG N[P521_LIMBS] = { |
| 32 | + 0xbb6fb71e91386409, 0x3bb5c9b8899c47ae, 0x7fcc0148f709a5d0, |
| 33 | + 0x51868783bf2f966b, 0xfffffffffffffffa, 0xffffffffffffffff, |
| 34 | + 0xffffffffffffffff, 0xffffffffffffffff, 0x00000000000001ff |
| 35 | +}; |
| 36 | + |
| 37 | +static const BN_ULONG ONE[P521_LIMBS] = { |
| 38 | + 0x0080000000000000, 0x0000000000000000, 0x0000000000000000, |
| 39 | + 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, |
| 40 | + 0x0000000000000000, 0x0000000000000000, 0x0000000000000000 |
| 41 | +}; |
| 42 | + |
| 43 | +/* This is just 2**520 */ |
| 44 | +static const BN_ULONG Q_PLUS_1_SHR_1[P521_LIMBS] = { |
| 45 | + 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, |
| 46 | + 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, |
| 47 | + 0x0000000000000000, 0x0000000000000000, 0x0000000000000100 |
| 48 | +}; |
| 49 | + |
| 50 | +#elif defined(OPENSSL_32_BIT) |
| 51 | + |
| 52 | +static const BN_ULONG Q[P521_LIMBS] = { |
| 53 | + 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, |
| 54 | + 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, |
| 55 | + 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0x000001ff |
| 56 | +}; |
| 57 | + |
| 58 | +static const BN_ULONG N[P521_LIMBS] = { |
| 59 | + 0x91386409, 0xbb6fb71e, 0x899c47ae, 0x3bb5c9b8, 0xf709a5d0, 0x7fcc0148, |
| 60 | + 0xbf2f966b, 0x51868783, 0xfffffffa, 0xffffffff, 0xffffffff, 0xffffffff, |
| 61 | + 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0x000001ff |
| 62 | +}; |
| 63 | + |
| 64 | +static const BN_ULONG ONE[P521_LIMBS] = { |
| 65 | + 0x00800000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, |
| 66 | + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, |
| 67 | + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000 |
| 68 | +}; |
| 69 | + |
| 70 | +static const BN_ULONG Q_PLUS_1_SHR_1[P521_LIMBS] = { |
| 71 | + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, |
| 72 | + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, |
| 73 | + 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000100 |
| 74 | +}; |
| 75 | + |
| 76 | +#else |
| 77 | +#error "Must define either OPENSSL_32_BIT or OPENSSL_64_BIT" |
| 78 | +#endif |
| 79 | + |
| 80 | +static const BN_ULONG Q_N0[] = { |
| 81 | + BN_MONT_CTX_N0(0x0, 0x1) |
| 82 | +}; |
| 83 | + |
| 84 | +/* XXX: MSVC for x86 warns when it fails to inline these functions it should |
| 85 | + * probably inline. */ |
| 86 | +#if defined(_MSC_VER) && !defined(__clang__) && defined(OPENSSL_X86) |
| 87 | +#define INLINE_IF_POSSIBLE __forceinline |
| 88 | +#else |
| 89 | +#define INLINE_IF_POSSIBLE inline |
| 90 | +#endif |
| 91 | + |
| 92 | +#define BITS 521 |
| 93 | +/* Window values that are Ok for p521 (look at `ecp_nistz.h`): 4 */ |
| 94 | +#define W_BITS 4 |
| 95 | +#define FE_LIMBS P521_LIMBS |
| 96 | + |
| 97 | +#include "ecp_nistz.inl" |
| 98 | + |
| 99 | +void p521_elem_sub(Elem r, const Elem a, const Elem b) { |
| 100 | + elem_sub(r, a, b); |
| 101 | +} |
| 102 | + |
| 103 | +void p521_elem_div_by_2(Elem r, const Elem a) { |
| 104 | + elem_div_by_2(r, a); |
| 105 | +} |
| 106 | + |
| 107 | +void p521_elem_mul_mont(Elem r, const Elem a, const Elem b) { |
| 108 | + elem_mul_mont(r, a, b); |
| 109 | +} |
| 110 | + |
| 111 | +void p521_elem_neg(Elem r, const Elem a) { |
| 112 | + elem_neg(r, a); |
| 113 | +} |
| 114 | + |
| 115 | +void p521_scalar_mul_mont(ScalarMont r, const ScalarMont a, |
| 116 | + const ScalarMont b) { |
| 117 | + static const BN_ULONG N_N0[] = { |
| 118 | + BN_MONT_CTX_N0(0x1d2f5ccd, 0x79a995c7) |
| 119 | + }; |
| 120 | + /* XXX: Inefficient. TODO: Add dedicated multiplication routine. */ |
| 121 | + bn_mul_mont(r, a, b, N, N_N0, FE_LIMBS); |
| 122 | +} |
0 commit comments