Back to home page

LXR

 
 

    


0001 /*
0002  * Copyright (c) 2013, Kenneth MacKay
0003  * All rights reserved.
0004  *
0005  * Redistribution and use in source and binary forms, with or without
0006  * modification, are permitted provided that the following conditions are
0007  * met:
0008  *  * Redistributions of source code must retain the above copyright
0009  *   notice, this list of conditions and the following disclaimer.
0010  *  * Redistributions in binary form must reproduce the above copyright
0011  *    notice, this list of conditions and the following disclaimer in the
0012  *    documentation and/or other materials provided with the distribution.
0013  *
0014  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
0015  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
0016  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
0017  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
0018  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
0019  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
0020  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
0021  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
0022  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
0023  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
0024  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0025  */
0026 
0027 #include <linux/random.h>
0028 #include <linux/slab.h>
0029 #include <linux/swab.h>
0030 #include <linux/fips.h>
0031 #include <crypto/ecdh.h>
0032 
0033 #include "ecc.h"
0034 #include "ecc_curve_defs.h"
0035 
0036 typedef struct {
0037     u64 m_low;
0038     u64 m_high;
0039 } uint128_t;
0040 
0041 static inline const struct ecc_curve *ecc_get_curve(unsigned int curve_id)
0042 {
0043     switch (curve_id) {
0044     /* In FIPS mode only allow P256 and higher */
0045     case ECC_CURVE_NIST_P192:
0046         return fips_enabled ? NULL : &nist_p192;
0047     case ECC_CURVE_NIST_P256:
0048         return &nist_p256;
0049     default:
0050         return NULL;
0051     }
0052 }
0053 
0054 static u64 *ecc_alloc_digits_space(unsigned int ndigits)
0055 {
0056     size_t len = ndigits * sizeof(u64);
0057 
0058     if (!len)
0059         return NULL;
0060 
0061     return kmalloc(len, GFP_KERNEL);
0062 }
0063 
0064 static void ecc_free_digits_space(u64 *space)
0065 {
0066     kzfree(space);
0067 }
0068 
0069 static struct ecc_point *ecc_alloc_point(unsigned int ndigits)
0070 {
0071     struct ecc_point *p = kmalloc(sizeof(*p), GFP_KERNEL);
0072 
0073     if (!p)
0074         return NULL;
0075 
0076     p->x = ecc_alloc_digits_space(ndigits);
0077     if (!p->x)
0078         goto err_alloc_x;
0079 
0080     p->y = ecc_alloc_digits_space(ndigits);
0081     if (!p->y)
0082         goto err_alloc_y;
0083 
0084     p->ndigits = ndigits;
0085 
0086     return p;
0087 
0088 err_alloc_y:
0089     ecc_free_digits_space(p->x);
0090 err_alloc_x:
0091     kfree(p);
0092     return NULL;
0093 }
0094 
0095 static void ecc_free_point(struct ecc_point *p)
0096 {
0097     if (!p)
0098         return;
0099 
0100     kzfree(p->x);
0101     kzfree(p->y);
0102     kzfree(p);
0103 }
0104 
0105 static void vli_clear(u64 *vli, unsigned int ndigits)
0106 {
0107     int i;
0108 
0109     for (i = 0; i < ndigits; i++)
0110         vli[i] = 0;
0111 }
0112 
0113 /* Returns true if vli == 0, false otherwise. */
0114 static bool vli_is_zero(const u64 *vli, unsigned int ndigits)
0115 {
0116     int i;
0117 
0118     for (i = 0; i < ndigits; i++) {
0119         if (vli[i])
0120             return false;
0121     }
0122 
0123     return true;
0124 }
0125 
0126 /* Returns nonzero if bit bit of vli is set. */
0127 static u64 vli_test_bit(const u64 *vli, unsigned int bit)
0128 {
0129     return (vli[bit / 64] & ((u64)1 << (bit % 64)));
0130 }
0131 
0132 /* Counts the number of 64-bit "digits" in vli. */
0133 static unsigned int vli_num_digits(const u64 *vli, unsigned int ndigits)
0134 {
0135     int i;
0136 
0137     /* Search from the end until we find a non-zero digit.
0138      * We do it in reverse because we expect that most digits will
0139      * be nonzero.
0140      */
0141     for (i = ndigits - 1; i >= 0 && vli[i] == 0; i--);
0142 
0143     return (i + 1);
0144 }
0145 
0146 /* Counts the number of bits required for vli. */
0147 static unsigned int vli_num_bits(const u64 *vli, unsigned int ndigits)
0148 {
0149     unsigned int i, num_digits;
0150     u64 digit;
0151 
0152     num_digits = vli_num_digits(vli, ndigits);
0153     if (num_digits == 0)
0154         return 0;
0155 
0156     digit = vli[num_digits - 1];
0157     for (i = 0; digit; i++)
0158         digit >>= 1;
0159 
0160     return ((num_digits - 1) * 64 + i);
0161 }
0162 
0163 /* Sets dest = src. */
0164 static void vli_set(u64 *dest, const u64 *src, unsigned int ndigits)
0165 {
0166     int i;
0167 
0168     for (i = 0; i < ndigits; i++)
0169         dest[i] = src[i];
0170 }
0171 
0172 /* Returns sign of left - right. */
0173 static int vli_cmp(const u64 *left, const u64 *right, unsigned int ndigits)
0174 {
0175     int i;
0176 
0177     for (i = ndigits - 1; i >= 0; i--) {
0178         if (left[i] > right[i])
0179             return 1;
0180         else if (left[i] < right[i])
0181             return -1;
0182     }
0183 
0184     return 0;
0185 }
0186 
0187 /* Computes result = in << c, returning carry. Can modify in place
0188  * (if result == in). 0 < shift < 64.
0189  */
0190 static u64 vli_lshift(u64 *result, const u64 *in, unsigned int shift,
0191               unsigned int ndigits)
0192 {
0193     u64 carry = 0;
0194     int i;
0195 
0196     for (i = 0; i < ndigits; i++) {
0197         u64 temp = in[i];
0198 
0199         result[i] = (temp << shift) | carry;
0200         carry = temp >> (64 - shift);
0201     }
0202 
0203     return carry;
0204 }
0205 
0206 /* Computes vli = vli >> 1. */
0207 static void vli_rshift1(u64 *vli, unsigned int ndigits)
0208 {
0209     u64 *end = vli;
0210     u64 carry = 0;
0211 
0212     vli += ndigits;
0213 
0214     while (vli-- > end) {
0215         u64 temp = *vli;
0216         *vli = (temp >> 1) | carry;
0217         carry = temp << 63;
0218     }
0219 }
0220 
0221 /* Computes result = left + right, returning carry. Can modify in place. */
0222 static u64 vli_add(u64 *result, const u64 *left, const u64 *right,
0223            unsigned int ndigits)
0224 {
0225     u64 carry = 0;
0226     int i;
0227 
0228     for (i = 0; i < ndigits; i++) {
0229         u64 sum;
0230 
0231         sum = left[i] + right[i] + carry;
0232         if (sum != left[i])
0233             carry = (sum < left[i]);
0234 
0235         result[i] = sum;
0236     }
0237 
0238     return carry;
0239 }
0240 
0241 /* Computes result = left - right, returning borrow. Can modify in place. */
0242 static u64 vli_sub(u64 *result, const u64 *left, const u64 *right,
0243            unsigned int ndigits)
0244 {
0245     u64 borrow = 0;
0246     int i;
0247 
0248     for (i = 0; i < ndigits; i++) {
0249         u64 diff;
0250 
0251         diff = left[i] - right[i] - borrow;
0252         if (diff != left[i])
0253             borrow = (diff > left[i]);
0254 
0255         result[i] = diff;
0256     }
0257 
0258     return borrow;
0259 }
0260 
0261 static uint128_t mul_64_64(u64 left, u64 right)
0262 {
0263     u64 a0 = left & 0xffffffffull;
0264     u64 a1 = left >> 32;
0265     u64 b0 = right & 0xffffffffull;
0266     u64 b1 = right >> 32;
0267     u64 m0 = a0 * b0;
0268     u64 m1 = a0 * b1;
0269     u64 m2 = a1 * b0;
0270     u64 m3 = a1 * b1;
0271     uint128_t result;
0272 
0273     m2 += (m0 >> 32);
0274     m2 += m1;
0275 
0276     /* Overflow */
0277     if (m2 < m1)
0278         m3 += 0x100000000ull;
0279 
0280     result.m_low = (m0 & 0xffffffffull) | (m2 << 32);
0281     result.m_high = m3 + (m2 >> 32);
0282 
0283     return result;
0284 }
0285 
0286 static uint128_t add_128_128(uint128_t a, uint128_t b)
0287 {
0288     uint128_t result;
0289 
0290     result.m_low = a.m_low + b.m_low;
0291     result.m_high = a.m_high + b.m_high + (result.m_low < a.m_low);
0292 
0293     return result;
0294 }
0295 
0296 static void vli_mult(u64 *result, const u64 *left, const u64 *right,
0297              unsigned int ndigits)
0298 {
0299     uint128_t r01 = { 0, 0 };
0300     u64 r2 = 0;
0301     unsigned int i, k;
0302 
0303     /* Compute each digit of result in sequence, maintaining the
0304      * carries.
0305      */
0306     for (k = 0; k < ndigits * 2 - 1; k++) {
0307         unsigned int min;
0308 
0309         if (k < ndigits)
0310             min = 0;
0311         else
0312             min = (k + 1) - ndigits;
0313 
0314         for (i = min; i <= k && i < ndigits; i++) {
0315             uint128_t product;
0316 
0317             product = mul_64_64(left[i], right[k - i]);
0318 
0319             r01 = add_128_128(r01, product);
0320             r2 += (r01.m_high < product.m_high);
0321         }
0322 
0323         result[k] = r01.m_low;
0324         r01.m_low = r01.m_high;
0325         r01.m_high = r2;
0326         r2 = 0;
0327     }
0328 
0329     result[ndigits * 2 - 1] = r01.m_low;
0330 }
0331 
0332 static void vli_square(u64 *result, const u64 *left, unsigned int ndigits)
0333 {
0334     uint128_t r01 = { 0, 0 };
0335     u64 r2 = 0;
0336     int i, k;
0337 
0338     for (k = 0; k < ndigits * 2 - 1; k++) {
0339         unsigned int min;
0340 
0341         if (k < ndigits)
0342             min = 0;
0343         else
0344             min = (k + 1) - ndigits;
0345 
0346         for (i = min; i <= k && i <= k - i; i++) {
0347             uint128_t product;
0348 
0349             product = mul_64_64(left[i], left[k - i]);
0350 
0351             if (i < k - i) {
0352                 r2 += product.m_high >> 63;
0353                 product.m_high = (product.m_high << 1) |
0354                          (product.m_low >> 63);
0355                 product.m_low <<= 1;
0356             }
0357 
0358             r01 = add_128_128(r01, product);
0359             r2 += (r01.m_high < product.m_high);
0360         }
0361 
0362         result[k] = r01.m_low;
0363         r01.m_low = r01.m_high;
0364         r01.m_high = r2;
0365         r2 = 0;
0366     }
0367 
0368     result[ndigits * 2 - 1] = r01.m_low;
0369 }
0370 
0371 /* Computes result = (left + right) % mod.
0372  * Assumes that left < mod and right < mod, result != mod.
0373  */
0374 static void vli_mod_add(u64 *result, const u64 *left, const u64 *right,
0375             const u64 *mod, unsigned int ndigits)
0376 {
0377     u64 carry;
0378 
0379     carry = vli_add(result, left, right, ndigits);
0380 
0381     /* result > mod (result = mod + remainder), so subtract mod to
0382      * get remainder.
0383      */
0384     if (carry || vli_cmp(result, mod, ndigits) >= 0)
0385         vli_sub(result, result, mod, ndigits);
0386 }
0387 
0388 /* Computes result = (left - right) % mod.
0389  * Assumes that left < mod and right < mod, result != mod.
0390  */
0391 static void vli_mod_sub(u64 *result, const u64 *left, const u64 *right,
0392             const u64 *mod, unsigned int ndigits)
0393 {
0394     u64 borrow = vli_sub(result, left, right, ndigits);
0395 
0396     /* In this case, p_result == -diff == (max int) - diff.
0397      * Since -x % d == d - x, we can get the correct result from
0398      * result + mod (with overflow).
0399      */
0400     if (borrow)
0401         vli_add(result, result, mod, ndigits);
0402 }
0403 
0404 /* Computes p_result = p_product % curve_p.
0405  * See algorithm 5 and 6 from
0406  * http://www.isys.uni-klu.ac.at/PDF/2001-0126-MT.pdf
0407  */
0408 static void vli_mmod_fast_192(u64 *result, const u64 *product,
0409                   const u64 *curve_prime, u64 *tmp)
0410 {
0411     const unsigned int ndigits = 3;
0412     int carry;
0413 
0414     vli_set(result, product, ndigits);
0415 
0416     vli_set(tmp, &product[3], ndigits);
0417     carry = vli_add(result, result, tmp, ndigits);
0418 
0419     tmp[0] = 0;
0420     tmp[1] = product[3];
0421     tmp[2] = product[4];
0422     carry += vli_add(result, result, tmp, ndigits);
0423 
0424     tmp[0] = tmp[1] = product[5];
0425     tmp[2] = 0;
0426     carry += vli_add(result, result, tmp, ndigits);
0427 
0428     while (carry || vli_cmp(curve_prime, result, ndigits) != 1)
0429         carry -= vli_sub(result, result, curve_prime, ndigits);
0430 }
0431 
0432 /* Computes result = product % curve_prime
0433  * from http://www.nsa.gov/ia/_files/nist-routines.pdf
0434  */
0435 static void vli_mmod_fast_256(u64 *result, const u64 *product,
0436                   const u64 *curve_prime, u64 *tmp)
0437 {
0438     int carry;
0439     const unsigned int ndigits = 4;
0440 
0441     /* t */
0442     vli_set(result, product, ndigits);
0443 
0444     /* s1 */
0445     tmp[0] = 0;
0446     tmp[1] = product[5] & 0xffffffff00000000ull;
0447     tmp[2] = product[6];
0448     tmp[3] = product[7];
0449     carry = vli_lshift(tmp, tmp, 1, ndigits);
0450     carry += vli_add(result, result, tmp, ndigits);
0451 
0452     /* s2 */
0453     tmp[1] = product[6] << 32;
0454     tmp[2] = (product[6] >> 32) | (product[7] << 32);
0455     tmp[3] = product[7] >> 32;
0456     carry += vli_lshift(tmp, tmp, 1, ndigits);
0457     carry += vli_add(result, result, tmp, ndigits);
0458 
0459     /* s3 */
0460     tmp[0] = product[4];
0461     tmp[1] = product[5] & 0xffffffff;
0462     tmp[2] = 0;
0463     tmp[3] = product[7];
0464     carry += vli_add(result, result, tmp, ndigits);
0465 
0466     /* s4 */
0467     tmp[0] = (product[4] >> 32) | (product[5] << 32);
0468     tmp[1] = (product[5] >> 32) | (product[6] & 0xffffffff00000000ull);
0469     tmp[2] = product[7];
0470     tmp[3] = (product[6] >> 32) | (product[4] << 32);
0471     carry += vli_add(result, result, tmp, ndigits);
0472 
0473     /* d1 */
0474     tmp[0] = (product[5] >> 32) | (product[6] << 32);
0475     tmp[1] = (product[6] >> 32);
0476     tmp[2] = 0;
0477     tmp[3] = (product[4] & 0xffffffff) | (product[5] << 32);
0478     carry -= vli_sub(result, result, tmp, ndigits);
0479 
0480     /* d2 */
0481     tmp[0] = product[6];
0482     tmp[1] = product[7];
0483     tmp[2] = 0;
0484     tmp[3] = (product[4] >> 32) | (product[5] & 0xffffffff00000000ull);
0485     carry -= vli_sub(result, result, tmp, ndigits);
0486 
0487     /* d3 */
0488     tmp[0] = (product[6] >> 32) | (product[7] << 32);
0489     tmp[1] = (product[7] >> 32) | (product[4] << 32);
0490     tmp[2] = (product[4] >> 32) | (product[5] << 32);
0491     tmp[3] = (product[6] << 32);
0492     carry -= vli_sub(result, result, tmp, ndigits);
0493 
0494     /* d4 */
0495     tmp[0] = product[7];
0496     tmp[1] = product[4] & 0xffffffff00000000ull;
0497     tmp[2] = product[5];
0498     tmp[3] = product[6] & 0xffffffff00000000ull;
0499     carry -= vli_sub(result, result, tmp, ndigits);
0500 
0501     if (carry < 0) {
0502         do {
0503             carry += vli_add(result, result, curve_prime, ndigits);
0504         } while (carry < 0);
0505     } else {
0506         while (carry || vli_cmp(curve_prime, result, ndigits) != 1)
0507             carry -= vli_sub(result, result, curve_prime, ndigits);
0508     }
0509 }
0510 
0511 /* Computes result = product % curve_prime
0512  *  from http://www.nsa.gov/ia/_files/nist-routines.pdf
0513 */
0514 static bool vli_mmod_fast(u64 *result, u64 *product,
0515               const u64 *curve_prime, unsigned int ndigits)
0516 {
0517     u64 tmp[2 * ndigits];
0518 
0519     switch (ndigits) {
0520     case 3:
0521         vli_mmod_fast_192(result, product, curve_prime, tmp);
0522         break;
0523     case 4:
0524         vli_mmod_fast_256(result, product, curve_prime, tmp);
0525         break;
0526     default:
0527         pr_err("unsupports digits size!\n");
0528         return false;
0529     }
0530 
0531     return true;
0532 }
0533 
0534 /* Computes result = (left * right) % curve_prime. */
0535 static void vli_mod_mult_fast(u64 *result, const u64 *left, const u64 *right,
0536                   const u64 *curve_prime, unsigned int ndigits)
0537 {
0538     u64 product[2 * ndigits];
0539 
0540     vli_mult(product, left, right, ndigits);
0541     vli_mmod_fast(result, product, curve_prime, ndigits);
0542 }
0543 
0544 /* Computes result = left^2 % curve_prime. */
0545 static void vli_mod_square_fast(u64 *result, const u64 *left,
0546                 const u64 *curve_prime, unsigned int ndigits)
0547 {
0548     u64 product[2 * ndigits];
0549 
0550     vli_square(product, left, ndigits);
0551     vli_mmod_fast(result, product, curve_prime, ndigits);
0552 }
0553 
0554 #define EVEN(vli) (!(vli[0] & 1))
0555 /* Computes result = (1 / p_input) % mod. All VLIs are the same size.
0556  * See "From Euclid's GCD to Montgomery Multiplication to the Great Divide"
0557  * https://labs.oracle.com/techrep/2001/smli_tr-2001-95.pdf
0558  */
0559 static void vli_mod_inv(u64 *result, const u64 *input, const u64 *mod,
0560             unsigned int ndigits)
0561 {
0562     u64 a[ndigits], b[ndigits];
0563     u64 u[ndigits], v[ndigits];
0564     u64 carry;
0565     int cmp_result;
0566 
0567     if (vli_is_zero(input, ndigits)) {
0568         vli_clear(result, ndigits);
0569         return;
0570     }
0571 
0572     vli_set(a, input, ndigits);
0573     vli_set(b, mod, ndigits);
0574     vli_clear(u, ndigits);
0575     u[0] = 1;
0576     vli_clear(v, ndigits);
0577 
0578     while ((cmp_result = vli_cmp(a, b, ndigits)) != 0) {
0579         carry = 0;
0580 
0581         if (EVEN(a)) {
0582             vli_rshift1(a, ndigits);
0583 
0584             if (!EVEN(u))
0585                 carry = vli_add(u, u, mod, ndigits);
0586 
0587             vli_rshift1(u, ndigits);
0588             if (carry)
0589                 u[ndigits - 1] |= 0x8000000000000000ull;
0590         } else if (EVEN(b)) {
0591             vli_rshift1(b, ndigits);
0592 
0593             if (!EVEN(v))
0594                 carry = vli_add(v, v, mod, ndigits);
0595 
0596             vli_rshift1(v, ndigits);
0597             if (carry)
0598                 v[ndigits - 1] |= 0x8000000000000000ull;
0599         } else if (cmp_result > 0) {
0600             vli_sub(a, a, b, ndigits);
0601             vli_rshift1(a, ndigits);
0602 
0603             if (vli_cmp(u, v, ndigits) < 0)
0604                 vli_add(u, u, mod, ndigits);
0605 
0606             vli_sub(u, u, v, ndigits);
0607             if (!EVEN(u))
0608                 carry = vli_add(u, u, mod, ndigits);
0609 
0610             vli_rshift1(u, ndigits);
0611             if (carry)
0612                 u[ndigits - 1] |= 0x8000000000000000ull;
0613         } else {
0614             vli_sub(b, b, a, ndigits);
0615             vli_rshift1(b, ndigits);
0616 
0617             if (vli_cmp(v, u, ndigits) < 0)
0618                 vli_add(v, v, mod, ndigits);
0619 
0620             vli_sub(v, v, u, ndigits);
0621             if (!EVEN(v))
0622                 carry = vli_add(v, v, mod, ndigits);
0623 
0624             vli_rshift1(v, ndigits);
0625             if (carry)
0626                 v[ndigits - 1] |= 0x8000000000000000ull;
0627         }
0628     }
0629 
0630     vli_set(result, u, ndigits);
0631 }
0632 
0633 /* ------ Point operations ------ */
0634 
0635 /* Returns true if p_point is the point at infinity, false otherwise. */
0636 static bool ecc_point_is_zero(const struct ecc_point *point)
0637 {
0638     return (vli_is_zero(point->x, point->ndigits) &&
0639         vli_is_zero(point->y, point->ndigits));
0640 }
0641 
0642 /* Point multiplication algorithm using Montgomery's ladder with co-Z
0643  * coordinates. From http://eprint.iacr.org/2011/338.pdf
0644  */
0645 
0646 /* Double in place */
0647 static void ecc_point_double_jacobian(u64 *x1, u64 *y1, u64 *z1,
0648                       u64 *curve_prime, unsigned int ndigits)
0649 {
0650     /* t1 = x, t2 = y, t3 = z */
0651     u64 t4[ndigits];
0652     u64 t5[ndigits];
0653 
0654     if (vli_is_zero(z1, ndigits))
0655         return;
0656 
0657     /* t4 = y1^2 */
0658     vli_mod_square_fast(t4, y1, curve_prime, ndigits);
0659     /* t5 = x1*y1^2 = A */
0660     vli_mod_mult_fast(t5, x1, t4, curve_prime, ndigits);
0661     /* t4 = y1^4 */
0662     vli_mod_square_fast(t4, t4, curve_prime, ndigits);
0663     /* t2 = y1*z1 = z3 */
0664     vli_mod_mult_fast(y1, y1, z1, curve_prime, ndigits);
0665     /* t3 = z1^2 */
0666     vli_mod_square_fast(z1, z1, curve_prime, ndigits);
0667 
0668     /* t1 = x1 + z1^2 */
0669     vli_mod_add(x1, x1, z1, curve_prime, ndigits);
0670     /* t3 = 2*z1^2 */
0671     vli_mod_add(z1, z1, z1, curve_prime, ndigits);
0672     /* t3 = x1 - z1^2 */
0673     vli_mod_sub(z1, x1, z1, curve_prime, ndigits);
0674     /* t1 = x1^2 - z1^4 */
0675     vli_mod_mult_fast(x1, x1, z1, curve_prime, ndigits);
0676 
0677     /* t3 = 2*(x1^2 - z1^4) */
0678     vli_mod_add(z1, x1, x1, curve_prime, ndigits);
0679     /* t1 = 3*(x1^2 - z1^4) */
0680     vli_mod_add(x1, x1, z1, curve_prime, ndigits);
0681     if (vli_test_bit(x1, 0)) {
0682         u64 carry = vli_add(x1, x1, curve_prime, ndigits);
0683 
0684         vli_rshift1(x1, ndigits);
0685         x1[ndigits - 1] |= carry << 63;
0686     } else {
0687         vli_rshift1(x1, ndigits);
0688     }
0689     /* t1 = 3/2*(x1^2 - z1^4) = B */
0690 
0691     /* t3 = B^2 */
0692     vli_mod_square_fast(z1, x1, curve_prime, ndigits);
0693     /* t3 = B^2 - A */
0694     vli_mod_sub(z1, z1, t5, curve_prime, ndigits);
0695     /* t3 = B^2 - 2A = x3 */
0696     vli_mod_sub(z1, z1, t5, curve_prime, ndigits);
0697     /* t5 = A - x3 */
0698     vli_mod_sub(t5, t5, z1, curve_prime, ndigits);
0699     /* t1 = B * (A - x3) */
0700     vli_mod_mult_fast(x1, x1, t5, curve_prime, ndigits);
0701     /* t4 = B * (A - x3) - y1^4 = y3 */
0702     vli_mod_sub(t4, x1, t4, curve_prime, ndigits);
0703 
0704     vli_set(x1, z1, ndigits);
0705     vli_set(z1, y1, ndigits);
0706     vli_set(y1, t4, ndigits);
0707 }
0708 
0709 /* Modify (x1, y1) => (x1 * z^2, y1 * z^3) */
0710 static void apply_z(u64 *x1, u64 *y1, u64 *z, u64 *curve_prime,
0711             unsigned int ndigits)
0712 {
0713     u64 t1[ndigits];
0714 
0715     vli_mod_square_fast(t1, z, curve_prime, ndigits);    /* z^2 */
0716     vli_mod_mult_fast(x1, x1, t1, curve_prime, ndigits); /* x1 * z^2 */
0717     vli_mod_mult_fast(t1, t1, z, curve_prime, ndigits);  /* z^3 */
0718     vli_mod_mult_fast(y1, y1, t1, curve_prime, ndigits); /* y1 * z^3 */
0719 }
0720 
0721 /* P = (x1, y1) => 2P, (x2, y2) => P' */
0722 static void xycz_initial_double(u64 *x1, u64 *y1, u64 *x2, u64 *y2,
0723                 u64 *p_initial_z, u64 *curve_prime,
0724                 unsigned int ndigits)
0725 {
0726     u64 z[ndigits];
0727 
0728     vli_set(x2, x1, ndigits);
0729     vli_set(y2, y1, ndigits);
0730 
0731     vli_clear(z, ndigits);
0732     z[0] = 1;
0733 
0734     if (p_initial_z)
0735         vli_set(z, p_initial_z, ndigits);
0736 
0737     apply_z(x1, y1, z, curve_prime, ndigits);
0738 
0739     ecc_point_double_jacobian(x1, y1, z, curve_prime, ndigits);
0740 
0741     apply_z(x2, y2, z, curve_prime, ndigits);
0742 }
0743 
0744 /* Input P = (x1, y1, Z), Q = (x2, y2, Z)
0745  * Output P' = (x1', y1', Z3), P + Q = (x3, y3, Z3)
0746  * or P => P', Q => P + Q
0747  */
0748 static void xycz_add(u64 *x1, u64 *y1, u64 *x2, u64 *y2, u64 *curve_prime,
0749              unsigned int ndigits)
0750 {
0751     /* t1 = X1, t2 = Y1, t3 = X2, t4 = Y2 */
0752     u64 t5[ndigits];
0753 
0754     /* t5 = x2 - x1 */
0755     vli_mod_sub(t5, x2, x1, curve_prime, ndigits);
0756     /* t5 = (x2 - x1)^2 = A */
0757     vli_mod_square_fast(t5, t5, curve_prime, ndigits);
0758     /* t1 = x1*A = B */
0759     vli_mod_mult_fast(x1, x1, t5, curve_prime, ndigits);
0760     /* t3 = x2*A = C */
0761     vli_mod_mult_fast(x2, x2, t5, curve_prime, ndigits);
0762     /* t4 = y2 - y1 */
0763     vli_mod_sub(y2, y2, y1, curve_prime, ndigits);
0764     /* t5 = (y2 - y1)^2 = D */
0765     vli_mod_square_fast(t5, y2, curve_prime, ndigits);
0766 
0767     /* t5 = D - B */
0768     vli_mod_sub(t5, t5, x1, curve_prime, ndigits);
0769     /* t5 = D - B - C = x3 */
0770     vli_mod_sub(t5, t5, x2, curve_prime, ndigits);
0771     /* t3 = C - B */
0772     vli_mod_sub(x2, x2, x1, curve_prime, ndigits);
0773     /* t2 = y1*(C - B) */
0774     vli_mod_mult_fast(y1, y1, x2, curve_prime, ndigits);
0775     /* t3 = B - x3 */
0776     vli_mod_sub(x2, x1, t5, curve_prime, ndigits);
0777     /* t4 = (y2 - y1)*(B - x3) */
0778     vli_mod_mult_fast(y2, y2, x2, curve_prime, ndigits);
0779     /* t4 = y3 */
0780     vli_mod_sub(y2, y2, y1, curve_prime, ndigits);
0781 
0782     vli_set(x2, t5, ndigits);
0783 }
0784 
0785 /* Input P = (x1, y1, Z), Q = (x2, y2, Z)
0786  * Output P + Q = (x3, y3, Z3), P - Q = (x3', y3', Z3)
0787  * or P => P - Q, Q => P + Q
0788  */
0789 static void xycz_add_c(u64 *x1, u64 *y1, u64 *x2, u64 *y2, u64 *curve_prime,
0790                unsigned int ndigits)
0791 {
0792     /* t1 = X1, t2 = Y1, t3 = X2, t4 = Y2 */
0793     u64 t5[ndigits];
0794     u64 t6[ndigits];
0795     u64 t7[ndigits];
0796 
0797     /* t5 = x2 - x1 */
0798     vli_mod_sub(t5, x2, x1, curve_prime, ndigits);
0799     /* t5 = (x2 - x1)^2 = A */
0800     vli_mod_square_fast(t5, t5, curve_prime, ndigits);
0801     /* t1 = x1*A = B */
0802     vli_mod_mult_fast(x1, x1, t5, curve_prime, ndigits);
0803     /* t3 = x2*A = C */
0804     vli_mod_mult_fast(x2, x2, t5, curve_prime, ndigits);
0805     /* t4 = y2 + y1 */
0806     vli_mod_add(t5, y2, y1, curve_prime, ndigits);
0807     /* t4 = y2 - y1 */
0808     vli_mod_sub(y2, y2, y1, curve_prime, ndigits);
0809 
0810     /* t6 = C - B */
0811     vli_mod_sub(t6, x2, x1, curve_prime, ndigits);
0812     /* t2 = y1 * (C - B) */
0813     vli_mod_mult_fast(y1, y1, t6, curve_prime, ndigits);
0814     /* t6 = B + C */
0815     vli_mod_add(t6, x1, x2, curve_prime, ndigits);
0816     /* t3 = (y2 - y1)^2 */
0817     vli_mod_square_fast(x2, y2, curve_prime, ndigits);
0818     /* t3 = x3 */
0819     vli_mod_sub(x2, x2, t6, curve_prime, ndigits);
0820 
0821     /* t7 = B - x3 */
0822     vli_mod_sub(t7, x1, x2, curve_prime, ndigits);
0823     /* t4 = (y2 - y1)*(B - x3) */
0824     vli_mod_mult_fast(y2, y2, t7, curve_prime, ndigits);
0825     /* t4 = y3 */
0826     vli_mod_sub(y2, y2, y1, curve_prime, ndigits);
0827 
0828     /* t7 = (y2 + y1)^2 = F */
0829     vli_mod_square_fast(t7, t5, curve_prime, ndigits);
0830     /* t7 = x3' */
0831     vli_mod_sub(t7, t7, t6, curve_prime, ndigits);
0832     /* t6 = x3' - B */
0833     vli_mod_sub(t6, t7, x1, curve_prime, ndigits);
0834     /* t6 = (y2 + y1)*(x3' - B) */
0835     vli_mod_mult_fast(t6, t6, t5, curve_prime, ndigits);
0836     /* t2 = y3' */
0837     vli_mod_sub(y1, t6, y1, curve_prime, ndigits);
0838 
0839     vli_set(x1, t7, ndigits);
0840 }
0841 
0842 static void ecc_point_mult(struct ecc_point *result,
0843                const struct ecc_point *point, const u64 *scalar,
0844                u64 *initial_z, u64 *curve_prime,
0845                unsigned int ndigits)
0846 {
0847     /* R0 and R1 */
0848     u64 rx[2][ndigits];
0849     u64 ry[2][ndigits];
0850     u64 z[ndigits];
0851     int i, nb;
0852     int num_bits = vli_num_bits(scalar, ndigits);
0853 
0854     vli_set(rx[1], point->x, ndigits);
0855     vli_set(ry[1], point->y, ndigits);
0856 
0857     xycz_initial_double(rx[1], ry[1], rx[0], ry[0], initial_z, curve_prime,
0858                 ndigits);
0859 
0860     for (i = num_bits - 2; i > 0; i--) {
0861         nb = !vli_test_bit(scalar, i);
0862         xycz_add_c(rx[1 - nb], ry[1 - nb], rx[nb], ry[nb], curve_prime,
0863                ndigits);
0864         xycz_add(rx[nb], ry[nb], rx[1 - nb], ry[1 - nb], curve_prime,
0865              ndigits);
0866     }
0867 
0868     nb = !vli_test_bit(scalar, 0);
0869     xycz_add_c(rx[1 - nb], ry[1 - nb], rx[nb], ry[nb], curve_prime,
0870            ndigits);
0871 
0872     /* Find final 1/Z value. */
0873     /* X1 - X0 */
0874     vli_mod_sub(z, rx[1], rx[0], curve_prime, ndigits);
0875     /* Yb * (X1 - X0) */
0876     vli_mod_mult_fast(z, z, ry[1 - nb], curve_prime, ndigits);
0877     /* xP * Yb * (X1 - X0) */
0878     vli_mod_mult_fast(z, z, point->x, curve_prime, ndigits);
0879 
0880     /* 1 / (xP * Yb * (X1 - X0)) */
0881     vli_mod_inv(z, z, curve_prime, point->ndigits);
0882 
0883     /* yP / (xP * Yb * (X1 - X0)) */
0884     vli_mod_mult_fast(z, z, point->y, curve_prime, ndigits);
0885     /* Xb * yP / (xP * Yb * (X1 - X0)) */
0886     vli_mod_mult_fast(z, z, rx[1 - nb], curve_prime, ndigits);
0887     /* End 1/Z calculation */
0888 
0889     xycz_add(rx[nb], ry[nb], rx[1 - nb], ry[1 - nb], curve_prime, ndigits);
0890 
0891     apply_z(rx[0], ry[0], z, curve_prime, ndigits);
0892 
0893     vli_set(result->x, rx[0], ndigits);
0894     vli_set(result->y, ry[0], ndigits);
0895 }
0896 
0897 static inline void ecc_swap_digits(const u64 *in, u64 *out,
0898                    unsigned int ndigits)
0899 {
0900     int i;
0901 
0902     for (i = 0; i < ndigits; i++)
0903         out[i] = __swab64(in[ndigits - 1 - i]);
0904 }
0905 
0906 int ecc_is_key_valid(unsigned int curve_id, unsigned int ndigits,
0907              const u8 *private_key, unsigned int private_key_len)
0908 {
0909     int nbytes;
0910     const struct ecc_curve *curve = ecc_get_curve(curve_id);
0911 
0912     if (!private_key)
0913         return -EINVAL;
0914 
0915     nbytes = ndigits << ECC_DIGITS_TO_BYTES_SHIFT;
0916 
0917     if (private_key_len != nbytes)
0918         return -EINVAL;
0919 
0920     if (vli_is_zero((const u64 *)&private_key[0], ndigits))
0921         return -EINVAL;
0922 
0923     /* Make sure the private key is in the range [1, n-1]. */
0924     if (vli_cmp(curve->n, (const u64 *)&private_key[0], ndigits) != 1)
0925         return -EINVAL;
0926 
0927     return 0;
0928 }
0929 
0930 int ecdh_make_pub_key(unsigned int curve_id, unsigned int ndigits,
0931               const u8 *private_key, unsigned int private_key_len,
0932               u8 *public_key, unsigned int public_key_len)
0933 {
0934     int ret = 0;
0935     struct ecc_point *pk;
0936     u64 priv[ndigits];
0937     unsigned int nbytes;
0938     const struct ecc_curve *curve = ecc_get_curve(curve_id);
0939 
0940     if (!private_key || !curve) {
0941         ret = -EINVAL;
0942         goto out;
0943     }
0944 
0945     ecc_swap_digits((const u64 *)private_key, priv, ndigits);
0946 
0947     pk = ecc_alloc_point(ndigits);
0948     if (!pk) {
0949         ret = -ENOMEM;
0950         goto out;
0951     }
0952 
0953     ecc_point_mult(pk, &curve->g, priv, NULL, curve->p, ndigits);
0954     if (ecc_point_is_zero(pk)) {
0955         ret = -EAGAIN;
0956         goto err_free_point;
0957     }
0958 
0959     nbytes = ndigits << ECC_DIGITS_TO_BYTES_SHIFT;
0960     ecc_swap_digits(pk->x, (u64 *)public_key, ndigits);
0961     ecc_swap_digits(pk->y, (u64 *)&public_key[nbytes], ndigits);
0962 
0963 err_free_point:
0964     ecc_free_point(pk);
0965 out:
0966     return ret;
0967 }
0968 
0969 int crypto_ecdh_shared_secret(unsigned int curve_id, unsigned int ndigits,
0970                const u8 *private_key, unsigned int private_key_len,
0971                const u8 *public_key, unsigned int public_key_len,
0972                u8 *secret, unsigned int secret_len)
0973 {
0974     int ret = 0;
0975     struct ecc_point *product, *pk;
0976     u64 priv[ndigits];
0977     u64 rand_z[ndigits];
0978     unsigned int nbytes;
0979     const struct ecc_curve *curve = ecc_get_curve(curve_id);
0980 
0981     if (!private_key || !public_key || !curve) {
0982         ret = -EINVAL;
0983         goto out;
0984     }
0985 
0986     nbytes = ndigits << ECC_DIGITS_TO_BYTES_SHIFT;
0987 
0988     get_random_bytes(rand_z, nbytes);
0989 
0990     pk = ecc_alloc_point(ndigits);
0991     if (!pk) {
0992         ret = -ENOMEM;
0993         goto out;
0994     }
0995 
0996     product = ecc_alloc_point(ndigits);
0997     if (!product) {
0998         ret = -ENOMEM;
0999         goto err_alloc_product;
1000     }
1001 
1002     ecc_swap_digits((const u64 *)public_key, pk->x, ndigits);
1003     ecc_swap_digits((const u64 *)&public_key[nbytes], pk->y, ndigits);
1004     ecc_swap_digits((const u64 *)private_key, priv, ndigits);
1005 
1006     ecc_point_mult(product, pk, priv, rand_z, curve->p, ndigits);
1007 
1008     ecc_swap_digits(product->x, (u64 *)secret, ndigits);
1009 
1010     if (ecc_point_is_zero(product))
1011         ret = -EFAULT;
1012 
1013     ecc_free_point(product);
1014 err_alloc_product:
1015     ecc_free_point(pk);
1016 out:
1017     return ret;
1018 }