Blame cbits/p256/p256.c

Packit 141393
/*
Packit 141393
 * Copyright 2013 The Android Open Source Project
Packit 141393
 *
Packit 141393
 * Redistribution and use in source and binary forms, with or without
Packit 141393
 * modification, are permitted provided that the following conditions are met:
Packit 141393
 *     * Redistributions of source code must retain the above copyright
Packit 141393
 *       notice, this list of conditions and the following disclaimer.
Packit 141393
 *     * Redistributions in binary form must reproduce the above copyright
Packit 141393
 *       notice, this list of conditions and the following disclaimer in the
Packit 141393
 *       documentation and/or other materials provided with the distribution.
Packit 141393
 *     * Neither the name of Google Inc. nor the names of its contributors may
Packit 141393
 *       be used to endorse or promote products derived from this software
Packit 141393
 *       without specific prior written permission.
Packit 141393
 *
Packit 141393
 * THIS SOFTWARE IS PROVIDED BY Google Inc. ``AS IS'' AND ANY EXPRESS OR
Packit 141393
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
Packit 141393
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
Packit 141393
 * EVENT SHALL Google Inc. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
Packit 141393
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
Packit 141393
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
Packit 141393
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
Packit 141393
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
Packit 141393
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
Packit 141393
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit 141393
 */
Packit 141393
Packit 141393
// This is an implementation of the P256 elliptic curve group. It's written to
Packit 141393
// be portable 32-bit, although it's still constant-time.
Packit 141393
//
Packit 141393
// WARNING: Implementing these functions in a constant-time manner is far from
Packit 141393
//          obvious. Be careful when touching this code.
Packit 141393
//
Packit 141393
// See http://www.imperialviolet.org/2010/12/04/ecc.html ([1]) for background.
Packit 141393
Packit 141393
#include <assert.h>
Packit 141393
#include <stdint.h>
Packit 141393
#include <string.h>
Packit 141393
#include <stdio.h>
Packit 141393
Packit 141393
#include "p256/p256.h"
Packit 141393
Packit 141393
const cryptonite_p256_int cryptonite_SECP256r1_n =  // curve order
Packit 141393
  {{0xfc632551, 0xf3b9cac2, 0xa7179e84, 0xbce6faad, -1, -1, 0, -1}};
Packit 141393
Packit 141393
const cryptonite_p256_int cryptonite_SECP256r1_p =  // curve field size
Packit 141393
  {{-1, -1, -1, 0, 0, 0, 1, -1 }};
Packit 141393
Packit 141393
const cryptonite_p256_int cryptonite_SECP256r1_b =  // curve b
Packit 141393
  {{0x27d2604b, 0x3bce3c3e, 0xcc53b0f6, 0x651d06b0,
Packit 141393
    0x769886bc, 0xb3ebbd55, 0xaa3a93e7, 0x5ac635d8}};
Packit 141393
Packit 141393
void cryptonite_p256_init(cryptonite_p256_int* a) {
Packit 141393
  memset(a, 0, sizeof(*a));
Packit 141393
}
Packit 141393
Packit 141393
void cryptonite_p256_clear(cryptonite_p256_int* a) { cryptonite_p256_init(a); }
Packit 141393
Packit 141393
int cryptonite_p256_get_bit(const cryptonite_p256_int* scalar, int bit) {
Packit 141393
  return (P256_DIGIT(scalar, bit / P256_BITSPERDIGIT)
Packit 141393
              >> (bit & (P256_BITSPERDIGIT - 1))) & 1;
Packit 141393
}
Packit 141393
Packit 141393
int cryptonite_p256_is_zero(const cryptonite_p256_int* a) {
Packit 141393
  int i, result = 0;
Packit 141393
  for (i = 0; i < P256_NDIGITS; ++i) result |= P256_DIGIT(a, i);
Packit 141393
  return !result;
Packit 141393
}
Packit 141393
Packit 141393
// top, c[] += a[] * b
Packit 141393
// Returns new top
Packit 141393
static cryptonite_p256_digit mulAdd(const cryptonite_p256_int* a,
Packit 141393
                         cryptonite_p256_digit b,
Packit 141393
                         cryptonite_p256_digit top,
Packit 141393
                         cryptonite_p256_digit* c) {
Packit 141393
  int i;
Packit 141393
  cryptonite_p256_ddigit carry = 0;
Packit 141393
Packit 141393
  for (i = 0; i < P256_NDIGITS; ++i) {
Packit 141393
    carry += *c;
Packit 141393
    carry += (cryptonite_p256_ddigit)P256_DIGIT(a, i) * b;
Packit 141393
    *c++ = (cryptonite_p256_digit)carry;
Packit 141393
    carry >>= P256_BITSPERDIGIT;
Packit 141393
  }
Packit 141393
  return top + (cryptonite_p256_digit)carry;
Packit 141393
}
Packit 141393
Packit 141393
// top, c[] -= top_a, a[]
Packit 141393
static cryptonite_p256_digit subTop(cryptonite_p256_digit top_a,
Packit 141393
                         const cryptonite_p256_digit* a,
Packit 141393
                         cryptonite_p256_digit top_c,
Packit 141393
                         cryptonite_p256_digit* c) {
Packit 141393
  int i;
Packit 141393
  cryptonite_p256_sddigit borrow = 0;
Packit 141393
Packit 141393
  for (i = 0; i < P256_NDIGITS; ++i) {
Packit 141393
    borrow += *c;
Packit 141393
    borrow -= *a++;
Packit 141393
    *c++ = (cryptonite_p256_digit)borrow;
Packit 141393
    borrow >>= P256_BITSPERDIGIT;
Packit 141393
  }
Packit 141393
  borrow += top_c;
Packit 141393
  borrow -= top_a;
Packit 141393
  top_c = (cryptonite_p256_digit)borrow;
Packit 141393
  assert((borrow >> P256_BITSPERDIGIT) == 0);
Packit 141393
  return top_c;
Packit 141393
}
Packit 141393
Packit 141393
// top, c[] -= MOD[] & mask (0 or -1)
Packit 141393
// returns new top.
Packit 141393
static cryptonite_p256_digit subM(const cryptonite_p256_int* MOD,
Packit 141393
                       cryptonite_p256_digit top,
Packit 141393
                       cryptonite_p256_digit* c,
Packit 141393
                       cryptonite_p256_digit mask) {
Packit 141393
  int i;
Packit 141393
  cryptonite_p256_sddigit borrow = 0;
Packit 141393
  for (i = 0; i < P256_NDIGITS; ++i) {
Packit 141393
    borrow += *c;
Packit 141393
    borrow -= P256_DIGIT(MOD, i) & mask;
Packit 141393
    *c++ = (cryptonite_p256_digit)borrow;
Packit 141393
    borrow >>= P256_BITSPERDIGIT;
Packit 141393
  }
Packit 141393
  return top + (cryptonite_p256_digit)borrow;
Packit 141393
}
Packit 141393
Packit 141393
// top, c[] += MOD[] & mask (0 or -1)
Packit 141393
// returns new top.
Packit 141393
static cryptonite_p256_digit addM(const cryptonite_p256_int* MOD,
Packit 141393
                       cryptonite_p256_digit top,
Packit 141393
                       cryptonite_p256_digit* c,
Packit 141393
                       cryptonite_p256_digit mask) {
Packit 141393
  int i;
Packit 141393
  cryptonite_p256_ddigit carry = 0;
Packit 141393
  for (i = 0; i < P256_NDIGITS; ++i) {
Packit 141393
    carry += *c;
Packit 141393
    carry += P256_DIGIT(MOD, i) & mask;
Packit 141393
    *c++ = (cryptonite_p256_digit)carry;
Packit 141393
    carry >>= P256_BITSPERDIGIT;
Packit 141393
  }
Packit 141393
  return top + (cryptonite_p256_digit)carry;
Packit 141393
}
Packit 141393
Packit 141393
// c = a * b mod MOD. c can be a and/or b.
Packit 141393
void cryptonite_p256_modmul(const cryptonite_p256_int* MOD,
Packit 141393
                 const cryptonite_p256_int* a,
Packit 141393
                 const cryptonite_p256_digit top_b,
Packit 141393
                 const cryptonite_p256_int* b,
Packit 141393
                 cryptonite_p256_int* c) {
Packit 141393
  cryptonite_p256_digit tmp[P256_NDIGITS * 2 + 1] = { 0 };
Packit 141393
  cryptonite_p256_digit top = 0;
Packit 141393
  int i;
Packit 141393
Packit 141393
  // Multiply/add into tmp.
Packit 141393
  for (i = 0; i < P256_NDIGITS; ++i) {
Packit 141393
    if (i) tmp[i + P256_NDIGITS - 1] = top;
Packit 141393
    top = mulAdd(a, P256_DIGIT(b, i), 0, tmp + i);
Packit 141393
  }
Packit 141393
Packit 141393
  // Multiply/add top digit
Packit 141393
  tmp[i + P256_NDIGITS - 1] = top;
Packit 141393
  top = mulAdd(a, top_b, 0, tmp + i);
Packit 141393
Packit 141393
  // Reduce tmp, digit by digit.
Packit 141393
  for (; i >= 0; --i) {
Packit 141393
    cryptonite_p256_digit reducer[P256_NDIGITS] = { 0 };
Packit 141393
    cryptonite_p256_digit top_reducer;
Packit 141393
Packit 141393
    // top can be any value at this point.
Packit 141393
    // Guestimate reducer as top * MOD, since msw of MOD is -1.
Packit 141393
    top_reducer = mulAdd(MOD, top, 0, reducer);
Packit 141393
Packit 141393
    // Subtract reducer from top | tmp.
Packit 141393
    top = subTop(top_reducer, reducer, top, tmp + i);
Packit 141393
Packit 141393
    // top is now either 0 or 1. Make it 0, fixed-timing.
Packit 141393
    assert(top <= 1);
Packit 141393
Packit 141393
    top = subM(MOD, top, tmp + i, ~(top - 1));
Packit 141393
Packit 141393
    assert(top == 0);
Packit 141393
Packit 141393
    // We have now reduced the top digit off tmp. Fetch new top digit.
Packit 141393
    top = tmp[i + P256_NDIGITS - 1];
Packit 141393
  }
Packit 141393
Packit 141393
  // tmp might still be larger than MOD, yet same bit length.
Packit 141393
  // Make sure it is less, fixed-timing.
Packit 141393
  addM(MOD, 0, tmp, subM(MOD, 0, tmp, -1));
Packit 141393
Packit 141393
  memcpy(c, tmp, P256_NBYTES);
Packit 141393
}
Packit 141393
int cryptonite_p256_is_odd(const cryptonite_p256_int* a) { return P256_DIGIT(a, 0) & 1; }
Packit 141393
int cryptonite_p256_is_even(const cryptonite_p256_int* a) { return !(P256_DIGIT(a, 0) & 1); }
Packit 141393
Packit 141393
cryptonite_p256_digit cryptonite_p256_shl(const cryptonite_p256_int* a, int n, cryptonite_p256_int* b) {
Packit 141393
  int i;
Packit 141393
  cryptonite_p256_digit top = P256_DIGIT(a, P256_NDIGITS - 1);
Packit 141393
Packit 141393
  n %= P256_BITSPERDIGIT;
Packit 141393
  for (i = P256_NDIGITS - 1; i > 0; --i) {
Packit 141393
    cryptonite_p256_digit accu = (P256_DIGIT(a, i) << n);
Packit 141393
    accu |= (P256_DIGIT(a, i - 1) >> (P256_BITSPERDIGIT - n));
Packit 141393
    P256_DIGIT(b, i) = accu;
Packit 141393
  }
Packit 141393
  P256_DIGIT(b, i) = (P256_DIGIT(a, i) << n);
Packit 141393
Packit 141393
  top = (cryptonite_p256_digit)((((cryptonite_p256_ddigit)top) << n) >> P256_BITSPERDIGIT);
Packit 141393
Packit 141393
  return top;
Packit 141393
}
Packit 141393
Packit 141393
void cryptonite_p256_shr(const cryptonite_p256_int* a, int n, cryptonite_p256_int* b) {
Packit 141393
  int i;
Packit 141393
Packit 141393
  n %= P256_BITSPERDIGIT;
Packit 141393
  for (i = 0; i < P256_NDIGITS - 1; ++i) {
Packit 141393
    cryptonite_p256_digit accu = (P256_DIGIT(a, i) >> n);
Packit 141393
    accu |= (P256_DIGIT(a, i + 1) << (P256_BITSPERDIGIT - n));
Packit 141393
    P256_DIGIT(b, i) = accu;
Packit 141393
  }
Packit 141393
  P256_DIGIT(b, i) = (P256_DIGIT(a, i) >> n);
Packit 141393
}
Packit 141393
Packit 141393
static void cryptonite_p256_shr1(const cryptonite_p256_int* a, int highbit, cryptonite_p256_int* b) {
Packit 141393
  int i;
Packit 141393
Packit 141393
  for (i = 0; i < P256_NDIGITS - 1; ++i) {
Packit 141393
    cryptonite_p256_digit accu = (P256_DIGIT(a, i) >> 1);
Packit 141393
    accu |= (P256_DIGIT(a, i + 1) << (P256_BITSPERDIGIT - 1));
Packit 141393
    P256_DIGIT(b, i) = accu;
Packit 141393
  }
Packit 141393
  P256_DIGIT(b, i) = (P256_DIGIT(a, i) >> 1) |
Packit 141393
      (highbit << (P256_BITSPERDIGIT - 1));
Packit 141393
}
Packit 141393
Packit 141393
// Return -1, 0, 1 for a < b, a == b or a > b respectively.
Packit 141393
int cryptonite_p256_cmp(const cryptonite_p256_int* a, const cryptonite_p256_int* b) {
Packit 141393
  int i;
Packit 141393
  cryptonite_p256_sddigit borrow = 0;
Packit 141393
  cryptonite_p256_digit notzero = 0;
Packit 141393
Packit 141393
  for (i = 0; i < P256_NDIGITS; ++i) {
Packit 141393
    borrow += (cryptonite_p256_sddigit)P256_DIGIT(a, i) - P256_DIGIT(b, i);
Packit 141393
    // Track whether any result digit is ever not zero.
Packit 141393
    // Relies on !!(non-zero) evaluating to 1, e.g., !!(-1) evaluating to 1.
Packit 141393
    notzero |= !!((cryptonite_p256_digit)borrow);
Packit 141393
    borrow >>= P256_BITSPERDIGIT;
Packit 141393
  }
Packit 141393
  return (int)borrow | notzero;
Packit 141393
}
Packit 141393
Packit 141393
// c = a - b. Returns borrow: 0 or -1.
Packit 141393
int cryptonite_p256_sub(const cryptonite_p256_int* a, const cryptonite_p256_int* b, cryptonite_p256_int* c) {
Packit 141393
  int i;
Packit 141393
  cryptonite_p256_sddigit borrow = 0;
Packit 141393
Packit 141393
  for (i = 0; i < P256_NDIGITS; ++i) {
Packit 141393
    borrow += (cryptonite_p256_sddigit)P256_DIGIT(a, i) - P256_DIGIT(b, i);
Packit 141393
    if (c) P256_DIGIT(c, i) = (cryptonite_p256_digit)borrow;
Packit 141393
    borrow >>= P256_BITSPERDIGIT;
Packit 141393
  }
Packit 141393
  return (int)borrow;
Packit 141393
}
Packit 141393
Packit 141393
// c = a + b. Returns carry: 0 or 1.
Packit 141393
int cryptonite_p256_add(const cryptonite_p256_int* a, const cryptonite_p256_int* b, cryptonite_p256_int* c) {
Packit 141393
  int i;
Packit 141393
  cryptonite_p256_ddigit carry = 0;
Packit 141393
Packit 141393
  for (i = 0; i < P256_NDIGITS; ++i) {
Packit 141393
    carry += (cryptonite_p256_ddigit)P256_DIGIT(a, i) + P256_DIGIT(b, i);
Packit 141393
    if (c) P256_DIGIT(c, i) = (cryptonite_p256_digit)carry;
Packit 141393
    carry >>= P256_BITSPERDIGIT;
Packit 141393
  }
Packit 141393
  return (int)carry;
Packit 141393
}
Packit 141393
Packit 141393
// b = a + d. Returns carry, 0 or 1.
Packit 141393
int cryptonite_p256_add_d(const cryptonite_p256_int* a, cryptonite_p256_digit d, cryptonite_p256_int* b) {
Packit 141393
  int i;
Packit 141393
  cryptonite_p256_ddigit carry = d;
Packit 141393
Packit 141393
  for (i = 0; i < P256_NDIGITS; ++i) {
Packit 141393
    carry += (cryptonite_p256_ddigit)P256_DIGIT(a, i);
Packit 141393
    if (b) P256_DIGIT(b, i) = (cryptonite_p256_digit)carry;
Packit 141393
    carry >>= P256_BITSPERDIGIT;
Packit 141393
  }
Packit 141393
  return (int)carry;
Packit 141393
}
Packit 141393
Packit 141393
// b = 1/a mod MOD, binary euclid.
Packit 141393
void cryptonite_p256_modinv_vartime(const cryptonite_p256_int* MOD,
Packit 141393
                         const cryptonite_p256_int* a,
Packit 141393
                         cryptonite_p256_int* b) {
Packit 141393
  cryptonite_p256_int R = P256_ZERO;
Packit 141393
  cryptonite_p256_int S = P256_ONE;
Packit 141393
  cryptonite_p256_int U = *MOD;
Packit 141393
  cryptonite_p256_int V = *a;
Packit 141393
Packit 141393
  for (;;) {
Packit 141393
    if (cryptonite_p256_is_even(&U)) {
Packit 141393
      cryptonite_p256_shr1(&U, 0, &U);
Packit 141393
      if (cryptonite_p256_is_even(&R)) {
Packit 141393
        cryptonite_p256_shr1(&R, 0, &R);
Packit 141393
      } else {
Packit 141393
        // R = (R+MOD)/2
Packit 141393
        cryptonite_p256_shr1(&R, cryptonite_p256_add(&R, MOD, &R), &R);
Packit 141393
      }
Packit 141393
    } else if (cryptonite_p256_is_even(&V)) {
Packit 141393
      cryptonite_p256_shr1(&V, 0, &V);
Packit 141393
      if (cryptonite_p256_is_even(&S)) {
Packit 141393
        cryptonite_p256_shr1(&S, 0, &S);
Packit 141393
      } else {
Packit 141393
        // S = (S+MOD)/2
Packit 141393
        cryptonite_p256_shr1(&S, cryptonite_p256_add(&S, MOD, &S) , &S);
Packit 141393
      }
Packit 141393
    } else {  // U,V both odd.
Packit 141393
      if (!cryptonite_p256_sub(&V, &U, NULL)) {
Packit 141393
        cryptonite_p256_sub(&V, &U, &V);
Packit 141393
        if (cryptonite_p256_sub(&S, &R, &S)) cryptonite_p256_add(&S, MOD, &S);
Packit 141393
        if (cryptonite_p256_is_zero(&V)) break;  // done.
Packit 141393
      } else {
Packit 141393
        cryptonite_p256_sub(&U, &V, &U);
Packit 141393
        if (cryptonite_p256_sub(&R, &S, &R)) cryptonite_p256_add(&R, MOD, &R);
Packit 141393
      }
Packit 141393
    }
Packit 141393
  }
Packit 141393
Packit 141393
  cryptonite_p256_mod(MOD, &R, b);
Packit 141393
}
Packit 141393
Packit 141393
void cryptonite_p256_mod(const cryptonite_p256_int* MOD,
Packit 141393
              const cryptonite_p256_int* in,
Packit 141393
              cryptonite_p256_int* out) {
Packit 141393
  if (out != in) *out = *in;
Packit 141393
  addM(MOD, 0, P256_DIGITS(out), subM(MOD, 0, P256_DIGITS(out), -1));
Packit 141393
}
Packit 141393
Packit 141393
// Verify y^2 == x^3 - 3x + b mod p
Packit 141393
// and 0 < x < p and 0 < y < p
Packit 141393
int cryptonite_p256_is_valid_point(const cryptonite_p256_int* x, const cryptonite_p256_int* y) {
Packit 141393
  cryptonite_p256_int y2, x3;
Packit 141393
Packit 141393
  if (cryptonite_p256_cmp(&cryptonite_SECP256r1_p, x) <= 0 ||
Packit 141393
      cryptonite_p256_cmp(&cryptonite_SECP256r1_p, y) <= 0 ||
Packit 141393
      cryptonite_p256_is_zero(x) ||
Packit 141393
      cryptonite_p256_is_zero(y)) return 0;
Packit 141393
Packit 141393
  cryptonite_p256_modmul(&cryptonite_SECP256r1_p, y, 0, y, &y2;;  // y^2
Packit 141393
Packit 141393
  cryptonite_p256_modmul(&cryptonite_SECP256r1_p, x, 0, x, &x3;;  // x^2
Packit 141393
  cryptonite_p256_modmul(&cryptonite_SECP256r1_p, x, 0, &x3, &x3;;  // x^3
Packit 141393
  if (cryptonite_p256_sub(&x3, x, &x3)) cryptonite_p256_add(&x3, &cryptonite_SECP256r1_p, &x3;;  // x^3 - x
Packit 141393
  if (cryptonite_p256_sub(&x3, x, &x3)) cryptonite_p256_add(&x3, &cryptonite_SECP256r1_p, &x3;;  // x^3 - 2x
Packit 141393
  if (cryptonite_p256_sub(&x3, x, &x3)) cryptonite_p256_add(&x3, &cryptonite_SECP256r1_p, &x3;;  // x^3 - 3x
Packit 141393
  if (cryptonite_p256_add(&x3, &cryptonite_SECP256r1_b, &x3))  // x^3 - 3x + b
Packit 141393
    cryptonite_p256_sub(&x3, &cryptonite_SECP256r1_p, &x3;;
Packit 141393
Packit 141393
  return cryptonite_p256_cmp(&y2, &x3) == 0;
Packit 141393
}
Packit 141393
Packit 141393
void cryptonite_p256_from_bin(const uint8_t src[P256_NBYTES], cryptonite_p256_int* dst) {
Packit 141393
  int i;
Packit 141393
  const uint8_t* p = &src[0];
Packit 141393
Packit 141393
  for (i = P256_NDIGITS - 1; i >= 0; --i) {
Packit 141393
    P256_DIGIT(dst, i) =
Packit 141393
        (p[0] << 24) |
Packit 141393
        (p[1] << 16) |
Packit 141393
        (p[2] << 8) |
Packit 141393
        p[3];
Packit 141393
    p += 4;
Packit 141393
  }
Packit 141393
}
Packit 141393
Packit 141393
void cryptonite_p256_to_bin(const cryptonite_p256_int* src, uint8_t dst[P256_NBYTES])
Packit 141393
{
Packit 141393
	int i;
Packit 141393
	uint8_t* p = &dst[0];
Packit 141393
Packit 141393
	for (i = P256_NDIGITS -1; i >= 0; --i) {
Packit 141393
		const cryptonite_p256_digit dig = P256_DIGIT(src, i);
Packit 141393
		p[0] = dig >> 24;
Packit 141393
		p[1] = dig >> 16;
Packit 141393
		p[2] = dig >> 8;
Packit 141393
		p[3] = dig;
Packit 141393
		p += 4;
Packit 141393
	}
Packit 141393
}