2021-10-25 02:54:32 -07:00
|
|
|
// Copyright (c) Facebook, Inc. and its affiliates.
|
|
|
|
|
//
|
2021-12-03 14:38:11 -08:00
|
|
|
// This source code is licensed under both the MIT license found in the
|
|
|
|
|
// LICENSE-MIT file in the root directory of this source tree and the Apache
|
|
|
|
|
// License, Version 2.0 found in the LICENSE-APACHE file in the root directory
|
|
|
|
|
// of this source tree.
|
2021-10-25 02:54:32 -07:00
|
|
|
|
2022-12-19 18:03:11 +01:00
|
|
|
//! Key Exchange group implementation for Curve25519
|
2021-10-25 02:54:32 -07:00
|
|
|
|
2022-04-02 01:10:00 +02:00
|
|
|
use curve25519_dalek::constants::ED25519_BASEPOINT_TABLE;
|
|
|
|
|
use curve25519_dalek::montgomery::MontgomeryPoint;
|
|
|
|
|
use curve25519_dalek::scalar::Scalar;
|
|
|
|
|
use curve25519_dalek::traits::Identity;
|
2022-02-25 07:13:22 +01:00
|
|
|
use digest::core_api::BlockSizeUser;
|
2023-03-05 05:31:07 +01:00
|
|
|
use digest::{FixedOutput, HashMarker};
|
2022-02-25 07:13:22 +01:00
|
|
|
use elliptic_curve::hash2curve::{ExpandMsg, ExpandMsgXmd, Expander};
|
|
|
|
|
use generic_array::typenum::{IsLess, IsLessOrEqual, U256, U32, U64};
|
2022-01-06 06:19:02 +01:00
|
|
|
use generic_array::GenericArray;
|
2021-10-25 02:54:32 -07:00
|
|
|
use rand::{CryptoRng, RngCore};
|
2022-04-17 16:23:31 -07:00
|
|
|
use subtle::ConstantTimeEq;
|
2021-10-25 02:54:32 -07:00
|
|
|
|
2022-01-06 06:19:02 +01:00
|
|
|
use super::KeGroup;
|
|
|
|
|
use crate::errors::InternalError;
|
|
|
|
|
|
2022-12-19 18:03:11 +01:00
|
|
|
/// Implementation for Curve25519.
|
|
|
|
|
pub struct Curve25519;
|
2022-02-25 07:13:22 +01:00
|
|
|
|
2022-12-19 18:03:11 +01:00
|
|
|
/// The implementation of such a subgroup for Curve25519
|
|
|
|
|
impl KeGroup for Curve25519 {
|
2022-04-02 01:10:00 +02:00
|
|
|
type Pk = MontgomeryPoint;
|
2021-10-25 02:54:32 -07:00
|
|
|
type PkLen = U32;
|
2022-04-02 01:10:00 +02:00
|
|
|
type Sk = Scalar;
|
2021-10-25 02:54:32 -07:00
|
|
|
type SkLen = U32;
|
|
|
|
|
|
2022-04-02 01:10:00 +02:00
|
|
|
fn serialize_pk(pk: Self::Pk) -> GenericArray<u8, Self::PkLen> {
|
2022-02-25 07:13:22 +01:00
|
|
|
pk.to_bytes().into()
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-02 01:10:00 +02:00
|
|
|
fn deserialize_pk(bytes: &[u8]) -> Result<Self::Pk, InternalError> {
|
|
|
|
|
bytes
|
|
|
|
|
.try_into()
|
|
|
|
|
.ok()
|
|
|
|
|
.map(MontgomeryPoint)
|
|
|
|
|
.filter(|pk| pk != &MontgomeryPoint::identity())
|
|
|
|
|
.ok_or(InternalError::PointError)
|
2021-10-25 02:54:32 -07:00
|
|
|
}
|
|
|
|
|
|
2022-02-25 07:13:22 +01:00
|
|
|
fn random_sk<R: RngCore + CryptoRng>(rng: &mut R) -> Self::Sk {
|
2022-01-04 00:50:40 +01:00
|
|
|
loop {
|
2022-04-02 01:10:00 +02:00
|
|
|
let scalar = Scalar::random(rng);
|
2021-10-25 02:54:32 -07:00
|
|
|
|
2022-12-19 23:04:52 +01:00
|
|
|
if scalar != Scalar::ZERO {
|
2022-04-02 01:10:00 +02:00
|
|
|
break scalar;
|
2021-10-25 02:54:32 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-25 07:13:22 +01:00
|
|
|
// Implements the `HashToScalar()` function from
|
2023-03-05 05:31:07 +01:00
|
|
|
// <https://www.ietf.org/archive/id/draft-irtf-cfrg-voprf-19.html#section-4>
|
|
|
|
|
fn hash_to_scalar<'a, H>(input: &[&[u8]], dst: &[&[u8]]) -> Result<Self::Sk, InternalError>
|
2022-02-25 07:13:22 +01:00
|
|
|
where
|
2023-03-05 05:31:07 +01:00
|
|
|
H: BlockSizeUser + Default + FixedOutput + HashMarker,
|
2022-02-25 07:13:22 +01:00
|
|
|
H::OutputSize: IsLess<U256> + IsLessOrEqual<H::BlockSize>,
|
|
|
|
|
{
|
|
|
|
|
let mut uniform_bytes = GenericArray::<_, U64>::default();
|
|
|
|
|
ExpandMsgXmd::<H>::expand_message(input, dst, 64)
|
|
|
|
|
.map_err(|_| InternalError::HashToScalar)?
|
|
|
|
|
.fill_bytes(&mut uniform_bytes);
|
|
|
|
|
|
2022-04-02 01:10:00 +02:00
|
|
|
let scalar = Scalar::from_bytes_mod_order_wide(&uniform_bytes.into());
|
2022-02-25 07:13:22 +01:00
|
|
|
|
2022-12-19 23:04:52 +01:00
|
|
|
if scalar == Scalar::ZERO {
|
2022-04-02 01:10:00 +02:00
|
|
|
Err(InternalError::HashToScalar)
|
|
|
|
|
} else {
|
|
|
|
|
Ok(scalar)
|
|
|
|
|
}
|
2021-10-25 02:54:32 -07:00
|
|
|
}
|
|
|
|
|
|
2022-04-17 16:23:31 -07:00
|
|
|
fn is_zero_scalar(scalar: Self::Sk) -> subtle::Choice {
|
2022-12-19 23:04:52 +01:00
|
|
|
scalar.ct_eq(&Scalar::ZERO)
|
2022-04-17 16:23:31 -07:00
|
|
|
}
|
|
|
|
|
|
2022-04-02 01:10:00 +02:00
|
|
|
fn public_key(sk: Self::Sk) -> Self::Pk {
|
2023-02-04 01:45:52 +01:00
|
|
|
(ED25519_BASEPOINT_TABLE * &sk).to_montgomery()
|
2021-10-25 02:54:32 -07:00
|
|
|
}
|
|
|
|
|
|
2022-04-02 01:10:00 +02:00
|
|
|
fn diffie_hellman(pk: Self::Pk, sk: Self::Sk) -> GenericArray<u8, Self::PkLen> {
|
|
|
|
|
Self::serialize_pk(sk * pk)
|
2022-02-25 07:13:22 +01:00
|
|
|
}
|
|
|
|
|
|
2022-04-02 01:10:00 +02:00
|
|
|
fn serialize_sk(sk: Self::Sk) -> GenericArray<u8, Self::SkLen> {
|
2022-02-25 07:13:22 +01:00
|
|
|
sk.to_bytes().into()
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-02 01:10:00 +02:00
|
|
|
fn deserialize_sk(bytes: &[u8]) -> Result<Self::Sk, InternalError> {
|
|
|
|
|
bytes
|
|
|
|
|
.try_into()
|
|
|
|
|
.ok()
|
2022-12-19 23:04:52 +01:00
|
|
|
.and_then(|bytes| Scalar::from_canonical_bytes(bytes).into())
|
|
|
|
|
.filter(|scalar| scalar != &Scalar::ZERO)
|
2022-04-02 01:10:00 +02:00
|
|
|
.ok_or(InternalError::PointError)
|
2021-10-25 02:54:32 -07:00
|
|
|
}
|
|
|
|
|
}
|