garage44.eu/jeroen/pinnacle

logclone: git clone https://garage44.eu/jeroen/pinnacle.git

.. /crypto/src/p256.rs

raw

//! Elliptic-curve Diffie-Hellman on P-256 (SEC 1): what Bluetooth's LE
//! Secure Connections pairing agrees its key with. Keys are 32-byte
//! big-endian scalars and points 64 bytes, x then y, big-endian.
//!
//! The multiplication runs on [`crate::bignum`], which does not take care
//! to run in constant time: a key pair is made afresh for every pairing
//! and dropped after it, but a program timing a pairing on the same core
//! could learn about that pairing's key. A constant-time field is still to
//! come.

use crate::bignum::{Modulus, Num};
use crate::ecdsa::{Field, P256, Point, num};

struct Curve {
    p: Num,
    n: Num,
    fp: Modulus,
}

fn curve() -> Curve {
    let (p, n) = (num(P256.p), num(P256.n));
    let fp = Modulus::new(p).unwrap();
    Curve { p, n, fp }
}

/// `k·(x, y)`, the point back in plain coordinates, or `None` at infinity.
fn multiply(c: &Curve, k: &Num, x: &Num, y: &Num) -> Option<[u8; 64]> {
    let field = Field { f: &c.fp };
    let point: Point = Some((c.fp.to_mont(x), c.fp.to_mont(y), c.fp.one()));
    let mut acc: Point = None;
    for i in (0..k.bits()).rev() {
        acc = field.double(&acc);
        if k.bit(i) {
            acc = field.add_points(&acc, &point);
        }
    }
    let (x, y, z) = acc?;
    let zinv = c.fp.inverse(&z);
    let zinv2 = field.mul(&zinv, &zinv);
    let x = c.fp.from_mont(&field.mul(&x, &zinv2));
    let y = c.fp.from_mont(&field.mul(&y, &field.mul(&zinv2, &zinv)));
    let mut out = [0u8; 64];
    x.to_be_bytes(&mut out[..32]);
    y.to_be_bytes(&mut out[32..]);
    Some(out)
}

/// The private key, as a number in [1, n), or `None`.
fn scalar(c: &Curve, private: &[u8; 32]) -> Option<Num> {
    let k = Num::from_be_bytes(private)?;
    (!k.is_zero() && k < c.n).then_some(k)
}

/// The public key of `private`; `None` for a scalar out of range (zero, or
/// not below the group's order), which a caller draws again.
pub fn public_key(private: &[u8; 32]) -> Option<[u8; 64]> {
    let c = curve();
    let k = scalar(&c, private)?;
    multiply(&c, &k, &num(P256.gx), &num(P256.gy))
}

/// The shared secret, the x coordinate of `private·peer`; `None` when
/// `peer` is not a point on the curve (an invalid-curve attack hands one
/// over to learn the key bit by bit) or the result is at infinity.
pub fn shared(private: &[u8; 32], peer: &[u8; 64]) -> Option<[u8; 32]> {
    let c = curve();
    let k = scalar(&c, private)?;
    let x = Num::from_be_bytes(&peer[..32])?;
    let y = Num::from_be_bytes(&peer[32..])?;
    if x >= c.p || y >= c.p {
        return None;
    }
    let field = Field { f: &c.fp };
    let (xm, ym) = (c.fp.to_mont(&x), c.fp.to_mont(&y));
    let b = c.fp.to_mont(&num(P256.b));
    let x3 = field.mul(&field.mul(&xm, &xm), &xm);
    let three_x = field.add(&field.add(&xm, &xm), &xm);
    if field.mul(&ym, &ym) != field.add(&field.sub(&x3, &three_x), &b) {
        return None;
    }
    let point = multiply(&c, &k, &x, &y)?;
    Some(point[..32].try_into().unwrap())
}