From 98d7e0705812bf1432a999ce60d176c976dad8a1 Mon Sep 17 00:00:00 2001 From: gardners Date: Tue, 14 May 2013 11:55:32 +0930 Subject: [PATCH] refactor out code that calculates public key from a crypto sign public key, e.g., to calculate a BID from a private key. --- crypto.c | 43 +++++++++++++++++++++++++++++++++++++++++++ rhizome_crypto.c | 18 +++--------------- serval.h | 5 +++++ 3 files changed, 51 insertions(+), 15 deletions(-) diff --git a/crypto.c b/crypto.c index 0b7fb526..016912fc 100644 --- a/crypto.c +++ b/crypto.c @@ -1,3 +1,25 @@ +/* +Serval Distributed Numbering Architecture (DNA) +Copyright (C) 2010-2013 Paul Gardner-Stephen + +This program 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 2 +of the License, or (at your option) any later version. + +This program 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 +along with this program; if not, write to the Free Software +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + +#include "crypto_sign_edwards25519sha512batch.h" +#include "nacl/src/crypto_sign_edwards25519sha512batch_ref/ge.h" + #include "serval.h" #include "overlay_address.h" #include "crypto.h" @@ -95,3 +117,24 @@ int crypto_sign_message(struct subscriber *source, unsigned char *content, int b *content_len+=sig_length; return ret; } + +int _crypto_sign_compute_public_key(struct __sourceloc __whence, + const unsigned char *skin, + const unsigned char *pk) +{ + IN(); + + unsigned char h[64]; + ge_p3 A; + + crypto_hash_sha512(h,skin,32); + h[0] &= 248; + h[31] &= 63; + h[31] |= 64; + + ge_scalarmult_base(&A,h); + ge_p3_tobytes((unsigned char *)pk,&A); + + RETURN(0); + OUT(); +} diff --git a/rhizome_crypto.c b/rhizome_crypto.c index 98942314..b57297d2 100644 --- a/rhizome_crypto.c +++ b/rhizome_crypto.c @@ -17,9 +17,6 @@ along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -#include "crypto_sign_edwards25519sha512batch.h" -#include "nacl/src/crypto_sign_edwards25519sha512batch_ref/ge.h" - #include "serval.h" #include "conf.h" #include "str.h" @@ -358,19 +355,10 @@ int rhizome_verify_bundle_privatekey(rhizome_manifest *m, { IN(); - unsigned char h[64]; - unsigned char pk[32]; - ge_p3 A; + unsigned char pk[crypto_sign_edwards25519sha512batch_PUBLICKEYBYTES]; + if (crypto_sign_compute_public_key(sk,pk)) RETURN(-1); + int i; - - crypto_hash_sha512(h,sk,32); - h[0] &= 248; - h[31] &= 63; - h[31] |= 64; - - ge_scalarmult_base(&A,h); - ge_p3_tobytes(pk,&A); - for (i = 0;i < 32;++i) if (pkin[i] != pk[i]) { if (m&&sk==m->cryptoSignSecret&&pkin==m->cryptoSignPublic) diff --git a/serval.h b/serval.h index 6e229cc9..b987917d 100644 --- a/serval.h +++ b/serval.h @@ -842,4 +842,9 @@ int link_state_announce_links(); int generate_nonce(unsigned char *nonce,int bytes); +int _crypto_sign_compute_public_key(struct __sourceloc __whence, + const unsigned char *skin, + const unsigned char *pk); +#define crypto_sign_compute_public_key(skin,pk) _crypto_sign_compute_public_key(__WHENCE__,skin,pk) + #endif // __SERVALD_SERVALD_H