2017-12-01 18:12:25 +00:00
|
|
|
/*
|
2018-01-18 13:12:46 +00:00
|
|
|
* Copyright 2017-2018 The OpenSSL Project Authors. All Rights Reserved.
|
2017-12-01 18:12:25 +00:00
|
|
|
* Copyright 2015-2016 Cryptography Research, Inc.
|
2017-11-15 15:27:21 +00:00
|
|
|
*
|
2017-12-01 18:12:25 +00:00
|
|
|
* Licensed under the OpenSSL license (the "License"). You may not use
|
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
2017-11-15 15:27:21 +00:00
|
|
|
*
|
2017-12-01 18:12:25 +00:00
|
|
|
* Originally written by Mike Hamburg
|
2017-11-15 15:27:21 +00:00
|
|
|
*/
|
|
|
|
|
2017-12-05 16:37:57 +00:00
|
|
|
#ifndef __C448_ED448_H__
|
|
|
|
# define __C448_ED448_H__ 1
|
2017-11-15 15:27:21 +00:00
|
|
|
|
2017-12-04 11:38:58 +00:00
|
|
|
# include "point_448.h"
|
2017-11-15 15:27:21 +00:00
|
|
|
|
2017-12-04 13:30:53 +00:00
|
|
|
/* Number of bytes in an EdDSA public key. */
|
2017-12-06 13:06:13 +00:00
|
|
|
# define EDDSA_448_PUBLIC_BYTES 57
|
2017-11-15 15:27:21 +00:00
|
|
|
|
2017-12-04 13:30:53 +00:00
|
|
|
/* Number of bytes in an EdDSA private key. */
|
2017-12-06 13:06:13 +00:00
|
|
|
# define EDDSA_448_PRIVATE_BYTES EDDSA_448_PUBLIC_BYTES
|
2017-11-15 15:27:21 +00:00
|
|
|
|
2017-12-04 13:30:53 +00:00
|
|
|
/* Number of bytes in an EdDSA private key. */
|
2017-12-06 13:06:13 +00:00
|
|
|
# define EDDSA_448_SIGNATURE_BYTES (EDDSA_448_PUBLIC_BYTES + \
|
|
|
|
EDDSA_448_PRIVATE_BYTES)
|
2017-11-15 15:27:21 +00:00
|
|
|
|
2017-12-04 13:30:53 +00:00
|
|
|
/* EdDSA encoding ratio. */
|
2017-12-06 13:06:13 +00:00
|
|
|
# define C448_EDDSA_ENCODE_RATIO 4
|
2017-11-15 15:27:21 +00:00
|
|
|
|
2017-12-04 13:30:53 +00:00
|
|
|
/* EdDSA decoding ratio. */
|
2017-12-06 13:06:13 +00:00
|
|
|
# define C448_EDDSA_DECODE_RATIO (4 / 4)
|
2017-11-15 15:27:21 +00:00
|
|
|
|
2017-12-04 13:30:53 +00:00
|
|
|
/*
|
|
|
|
* EdDSA key generation. This function uses a different (non-Decaf) encoding.
|
2017-11-15 15:27:21 +00:00
|
|
|
*
|
2017-12-04 13:30:53 +00:00
|
|
|
* pubkey (out): The public key.
|
|
|
|
* privkey (in): The private key.
|
2017-12-04 11:38:58 +00:00
|
|
|
*/
|
2017-12-05 16:37:57 +00:00
|
|
|
c448_error_t c448_ed448_derive_public_key(
|
2017-12-06 13:06:13 +00:00
|
|
|
uint8_t pubkey [EDDSA_448_PUBLIC_BYTES],
|
|
|
|
const uint8_t privkey [EDDSA_448_PRIVATE_BYTES]);
|
2017-12-04 13:30:53 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* EdDSA signing.
|
|
|
|
*
|
|
|
|
* signature (out): The signature.
|
|
|
|
* privkey (in): The private key.
|
|
|
|
* pubkey (in): The public key.
|
|
|
|
* message (in): The message to sign.
|
|
|
|
* message_len (in): The length of the message.
|
|
|
|
* prehashed (in): Nonzero if the message is actually the hash of something
|
|
|
|
* you want to sign.
|
|
|
|
* context (in): A "context" for this signature of up to 255 bytes.
|
|
|
|
* context_len (in): Length of the context.
|
|
|
|
*
|
|
|
|
* For Ed25519, it is unsafe to use the same key for both prehashed and
|
|
|
|
* non-prehashed messages, at least without some very careful protocol-level
|
|
|
|
* disambiguation. For Ed448 it is safe. The C++ wrapper is designed to make
|
|
|
|
* it harder to screw this up, but this C code gives you no seat belt.
|
2017-12-04 11:38:58 +00:00
|
|
|
*/
|
2017-12-05 16:37:57 +00:00
|
|
|
c448_error_t c448_ed448_sign(
|
2017-12-06 13:06:13 +00:00
|
|
|
uint8_t signature[EDDSA_448_SIGNATURE_BYTES],
|
|
|
|
const uint8_t privkey[EDDSA_448_PRIVATE_BYTES],
|
|
|
|
const uint8_t pubkey[EDDSA_448_PUBLIC_BYTES],
|
2017-12-04 13:30:53 +00:00
|
|
|
const uint8_t *message, size_t message_len,
|
|
|
|
uint8_t prehashed, const uint8_t *context,
|
2017-12-11 17:00:07 +00:00
|
|
|
size_t context_len);
|
2017-12-04 13:30:53 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* EdDSA signing with prehash.
|
|
|
|
*
|
|
|
|
* signature (out): The signature.
|
|
|
|
* privkey (in): The private key.
|
|
|
|
* pubkey (in): The public key.
|
|
|
|
* hash (in): The hash of the message. This object will not be modified by the
|
|
|
|
* call.
|
|
|
|
* context (in): A "context" for this signature of up to 255 bytes. Must be the
|
|
|
|
* same as what was used for the prehash.
|
|
|
|
* context_len (in): Length of the context.
|
|
|
|
*
|
|
|
|
* For Ed25519, it is unsafe to use the same key for both prehashed and
|
|
|
|
* non-prehashed messages, at least without some very careful protocol-level
|
|
|
|
* disambiguation. For Ed448 it is safe. The C++ wrapper is designed to make
|
|
|
|
* it harder to screw this up, but this C code gives you no seat belt.
|
2017-12-04 11:38:58 +00:00
|
|
|
*/
|
2017-12-05 16:37:57 +00:00
|
|
|
c448_error_t c448_ed448_sign_prehash(
|
2017-12-06 13:06:13 +00:00
|
|
|
uint8_t signature[EDDSA_448_SIGNATURE_BYTES],
|
|
|
|
const uint8_t privkey[EDDSA_448_PRIVATE_BYTES],
|
|
|
|
const uint8_t pubkey[EDDSA_448_PUBLIC_BYTES],
|
2017-12-04 13:30:53 +00:00
|
|
|
const uint8_t hash[64],
|
|
|
|
const uint8_t *context,
|
2017-12-11 17:00:07 +00:00
|
|
|
size_t context_len);
|
2017-12-04 13:30:53 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* EdDSA signature verification.
|
2017-11-15 15:27:21 +00:00
|
|
|
*
|
|
|
|
* Uses the standard (i.e. less-strict) verification formula.
|
|
|
|
*
|
2017-12-04 13:30:53 +00:00
|
|
|
* signature (in): The signature.
|
|
|
|
* pubkey (in): The public key.
|
|
|
|
* message (in): The message to verify.
|
|
|
|
* message_len (in): The length of the message.
|
|
|
|
* prehashed (in): Nonzero if the message is actually the hash of something you
|
|
|
|
* want to verify.
|
|
|
|
* context (in): A "context" for this signature of up to 255 bytes.
|
|
|
|
* context_len (in): Length of the context.
|
|
|
|
*
|
|
|
|
* For Ed25519, it is unsafe to use the same key for both prehashed and
|
|
|
|
* non-prehashed messages, at least without some very careful protocol-level
|
|
|
|
* disambiguation. For Ed448 it is safe. The C++ wrapper is designed to make
|
|
|
|
* it harder to screw this up, but this C code gives you no seat belt.
|
2017-11-15 15:27:21 +00:00
|
|
|
*/
|
2017-12-05 16:37:57 +00:00
|
|
|
c448_error_t c448_ed448_verify(const uint8_t
|
2017-12-06 13:06:13 +00:00
|
|
|
signature[EDDSA_448_SIGNATURE_BYTES],
|
2017-12-04 11:38:58 +00:00
|
|
|
const uint8_t
|
2017-12-06 13:06:13 +00:00
|
|
|
pubkey[EDDSA_448_PUBLIC_BYTES],
|
2017-12-04 11:38:58 +00:00
|
|
|
const uint8_t *message, size_t message_len,
|
|
|
|
uint8_t prehashed, const uint8_t *context,
|
2017-12-11 17:00:07 +00:00
|
|
|
uint8_t context_len);
|
2017-11-15 15:27:21 +00:00
|
|
|
|
2017-12-04 13:30:53 +00:00
|
|
|
/*
|
|
|
|
* EdDSA signature verification.
|
2017-11-15 15:27:21 +00:00
|
|
|
*
|
|
|
|
* Uses the standard (i.e. less-strict) verification formula.
|
|
|
|
*
|
2017-12-04 13:30:53 +00:00
|
|
|
* signature (in): The signature.
|
|
|
|
* pubkey (in): The public key.
|
|
|
|
* hash (in): The hash of the message. This object will not be modified by the
|
|
|
|
* call.
|
|
|
|
* context (in): A "context" for this signature of up to 255 bytes. Must be the
|
|
|
|
* same as what was used for the prehash.
|
|
|
|
* context_len (in): Length of the context.
|
|
|
|
*
|
|
|
|
* For Ed25519, it is unsafe to use the same key for both prehashed and
|
|
|
|
* non-prehashed messages, at least without some very careful protocol-level
|
|
|
|
* disambiguation. For Ed448 it is safe. The C++ wrapper is designed to make
|
|
|
|
* it harder to screw this up, but this C code gives you no seat belt.
|
2017-11-15 15:27:21 +00:00
|
|
|
*/
|
2017-12-05 16:37:57 +00:00
|
|
|
c448_error_t c448_ed448_verify_prehash(
|
2017-12-06 13:06:13 +00:00
|
|
|
const uint8_t signature[EDDSA_448_SIGNATURE_BYTES],
|
|
|
|
const uint8_t pubkey[EDDSA_448_PUBLIC_BYTES],
|
2017-12-04 13:30:53 +00:00
|
|
|
const uint8_t hash[64],
|
|
|
|
const uint8_t *context,
|
2017-12-11 17:00:07 +00:00
|
|
|
uint8_t context_len);
|
2017-12-04 13:30:53 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* EdDSA point encoding. Used internally, exposed externally.
|
2017-12-06 13:06:13 +00:00
|
|
|
* Multiplies by C448_EDDSA_ENCODE_RATIO first.
|
2017-11-15 15:27:21 +00:00
|
|
|
*
|
|
|
|
* The multiplication is required because the EdDSA encoding represents
|
|
|
|
* the cofactor information, but the Decaf encoding ignores it (which
|
|
|
|
* is the whole point). So if you decode from EdDSA and re-encode to
|
|
|
|
* EdDSA, the cofactor info must get cleared, because the intermediate
|
|
|
|
* representation doesn't track it.
|
|
|
|
*
|
2017-12-06 13:06:13 +00:00
|
|
|
* The way we handle this is to multiply by C448_EDDSA_DECODE_RATIO when
|
|
|
|
* decoding, and by C448_EDDSA_ENCODE_RATIO when encoding. The product of
|
2017-12-05 16:37:57 +00:00
|
|
|
* these ratios is always exactly the cofactor 4, so the cofactor ends up
|
|
|
|
* cleared one way or another. But exactly how that shakes out depends on the
|
|
|
|
* base points specified in RFC 8032.
|
2017-11-15 15:27:21 +00:00
|
|
|
*
|
|
|
|
* The upshot is that if you pass the Decaf/Ristretto base point to
|
2017-12-06 13:06:13 +00:00
|
|
|
* this function, you will get C448_EDDSA_ENCODE_RATIO times the
|
2017-11-15 15:27:21 +00:00
|
|
|
* EdDSA base point.
|
|
|
|
*
|
2017-12-04 13:30:53 +00:00
|
|
|
* enc (out): The encoded point.
|
|
|
|
* p (in): The point.
|
2017-12-04 11:38:58 +00:00
|
|
|
*/
|
2017-12-04 13:30:53 +00:00
|
|
|
void curve448_point_mul_by_ratio_and_encode_like_eddsa(
|
2017-12-06 13:06:13 +00:00
|
|
|
uint8_t enc [EDDSA_448_PUBLIC_BYTES],
|
2017-12-04 13:30:53 +00:00
|
|
|
const curve448_point_t p);
|
2017-11-15 15:27:21 +00:00
|
|
|
|
2017-12-04 13:30:53 +00:00
|
|
|
/*
|
2017-12-06 13:06:13 +00:00
|
|
|
* EdDSA point decoding. Multiplies by C448_EDDSA_DECODE_RATIO, and
|
2017-12-04 13:30:53 +00:00
|
|
|
* ignores cofactor information.
|
2017-11-15 15:27:21 +00:00
|
|
|
*
|
2017-11-16 16:27:48 +00:00
|
|
|
* See notes on curve448_point_mul_by_ratio_and_encode_like_eddsa
|
2017-11-15 15:27:21 +00:00
|
|
|
*
|
2017-12-04 13:30:53 +00:00
|
|
|
* enc (out): The encoded point.
|
|
|
|
* p (in): The point.
|
2017-12-04 11:38:58 +00:00
|
|
|
*/
|
2017-12-05 16:37:57 +00:00
|
|
|
c448_error_t curve448_point_decode_like_eddsa_and_mul_by_ratio(
|
2017-12-04 13:30:53 +00:00
|
|
|
curve448_point_t p,
|
2017-12-06 13:06:13 +00:00
|
|
|
const uint8_t enc[EDDSA_448_PUBLIC_BYTES]);
|
2017-12-04 13:30:53 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* EdDSA to ECDH private key conversion
|
2017-11-15 15:27:21 +00:00
|
|
|
* Using the appropriate hash function, hash the EdDSA private key
|
|
|
|
* and keep only the lower bytes to get the ECDH private key
|
|
|
|
*
|
2017-12-04 13:30:53 +00:00
|
|
|
* x (out): The ECDH private key as in RFC7748
|
|
|
|
* ed (in): The EdDSA private key
|
2017-11-15 15:27:21 +00:00
|
|
|
*/
|
2017-12-05 16:37:57 +00:00
|
|
|
c448_error_t c448_ed448_convert_private_key_to_x448(
|
2017-12-06 13:06:13 +00:00
|
|
|
uint8_t x[X448_PRIVATE_BYTES],
|
|
|
|
const uint8_t ed[EDDSA_448_PRIVATE_BYTES]);
|
2017-11-15 15:27:21 +00:00
|
|
|
|
2017-12-05 16:37:57 +00:00
|
|
|
#endif /* __C448_ED448_H__ */
|