X-Git-Url: https://git.distorted.org.uk/~mdw/secnet/blobdiff_plain/1047c205103e6da9fc6a317f41583147dbc11aa3..a1a6042e24c9873aa6abf668bcb68d39d0eb4190:/ed25519.h diff --git a/ed25519.h b/ed25519.h new file mode 100644 index 0000000..a782682 --- /dev/null +++ b/ed25519.h @@ -0,0 +1,170 @@ +/* -*-c-*- + * + * The Ed25519 signature scheme + * + * (c) 2017 Straylight/Edgeware + */ + +/*----- Licensing notice --------------------------------------------------* + * + * This file is part of secnet. + * See README for full list of copyright holders. + * + * secnet is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version d of the License, or + * (at your option) any later version. + * + * secnet is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * version 3 along with secnet; if not, see + * https://www.gnu.org/licenses/gpl.html. + * + * This file was originally part of Catacomb, but has been automatically + * modified for incorporation into secnet: see `import-catacomb-crypto' + * for details. + * + * Catacomb is free software; you can redistribute it and/or modify + * it under the terms of the GNU Library General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * Catacomb is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with Catacomb; if not, write to the Free + * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, + * MA 02111-1307, USA. + */ + +#ifndef CATACOMB_ED25519_H +#define CATACOMB_ED25519_H + +#ifdef __cplusplus + extern "C" { +#endif + +/*----- Notes on the Ed25519 signature scheme -----------------------------* + * + * This is Ed25519, as described in Daniel J. Bernstein, Neils Duif, Tanja + * Lange, Peter Schwabe, and Bo-Yin Yang, `High-speed high-security + * signatures', CHES 2011, https://ed25519.cr.yp.to/ed25519-20110926.pdf + * + * Specifically, this code implements `PureEdDSA', according to the + * definition in Daniel J. Bernstein, Simon Josefsson, Tanja Lange, Peter + * Schwabe, and Bo-Yin Yang, `EdDSA for more curves', + * https://ed25519.cr.yp.to/eddsa-20150704.pdf. HashEdEDSA can be + * implemented easily by presenting a hash of a message to the functions + * here, as the message to be signed or verified. + * + * It also implements `Ed25519ctx' and `Ed25519ph' as described in RFC8032, + * though in the latter case it assumes that you've already done the hashing + * and have provided the hash as the `message' input. + */ + +/*----- Header files ------------------------------------------------------*/ + +#include "fake-mLib-bits.h" + +/*----- Important constants -----------------------------------------------*/ + +#define ED25519_KEYSZ 32u +#define ED25519_PUBSZ 32u +#define ED25519_SIGSZ 64u + +#define ED25519_MAXPERSOSZ 255u + +/*----- Functions provided ------------------------------------------------*/ + +/* --- @ed25519_pubkey@ --- * + * + * Arguments: @octet K[ED25519_PUBSZ]@ = where to put the public key + * @const void *k@ = private key + * @size_t ksz@ = length of private key + * + * Returns: --- + * + * Use: Derives the public key from a private key. + */ + +extern void ed25519_pubkey(octet /*K*/[ED25519_PUBSZ], + const void */*k*/, size_t /*ksz*/); + +/* --- @ed25519_sign@, @ed25519ctx_sign@ --- * + * + * Arguments: @octet sig[ED25519_SIGSZ]@ = where to put the signature + * @const void *k@ = private key + * @size_t ksz@ = length of private key + * @const octet K[ED25519_PUBSZ]@ = public key + * @int phflag@ = whether the `message' has been hashed already + * @const void *p@ = personalization string + * @size_t psz@ = length of personalization string + * @const void *m@ = message to sign + * @size_t msz@ = length of message + * + * Returns: --- + * + * Use: Signs a message. + * + * In @ed25519ctx_sign@, if @phflag@ is @-1@ then you get plain + * old Ed25519: the personalization string pointer @p@ will be + * ignored. If @phflag > 0@ then the `message' @m@ should be a + * SHA512 hash of the actual message. + */ + +extern void ed25519ctx_sign(octet /*sig*/[ED25519_SIGSZ], + const void */*k*/, size_t /*ksz*/, + const octet /*K*/[ED25519_PUBSZ], + int /*phflag*/, + const void */*p*/, size_t /*psz*/, + const void */*m*/, size_t /*msz*/); + +extern void ed25519_sign(octet /*sig*/[ED25519_SIGSZ], + const void */*k*/, size_t /*ksz*/, + const octet /*K*/[ED25519_PUBSZ], + const void */*m*/, size_t /*msz*/); + +/* --- @ed25519_verify@, @ed25519ctx_verify@ --- * + * + * Arguments: @const octet K[ED25519_PUBSZ]@ = public key + * @int phflag@ = whether the `message' has been hashed already + * @const void *p@ = personalization string + * @size_t psz@ = length of personalization string + * @const void *m@ = message to sign + * @size_t msz@ = length of message + * @const octet sig[ED25519_SIGSZ]@ = signature + * + * Returns: Zero if OK, negative on failure. + * + * Use: Verify a signature. + * + * In @ed25519ctx_verify@, if @phflag@ is @-1@ then you get + * plain old Ed25519: the personalization string pointer @p@ + * will be ignored. If @phflag > 0@ then the `message' @m@ + * should be a SHA512 hash of the actual message. + */ + +extern int ed25519ctx_verify(const octet /*K*/[ED25519_PUBSZ], + int /*phflag*/, + const void */*p*/, size_t /*psz*/, + const void */*m*/, size_t /*msz*/, + const octet /*sig*/[ED25519_SIGSZ]); + +extern int ed25519_verify(const octet /*K*/[ED25519_PUBSZ], + const void */*m*/, size_t /*msz*/, + const octet /*sig*/[ED25519_SIGSZ]); + +/*----- That's all, folks -------------------------------------------------*/ + +#ifdef __cplusplus + } +#endif + +#endif