/* This file is part of libbrandt. * Copyright (C) 2016 GNUnet e.V. * * libbrandt 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 3 of the License, or (at your option) any later * version. * * libbrandt 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 * libbrandt. If not, see . */ /** * @file crypto.h * @brief Interface of the crypto primitives. * @author Markus Teich */ #ifndef _BRANDT_CRYPTO_H #define _BRANDT_CRYPTO_H #include #include #include "platform.h" #include #include "internals.h" void brandt_crypto_init (struct GNUNET_CRYPTO_EccDlogContext *dlogctx); /* --- EC --- */ /* used for serialized mpis and serialized curve points (they are the same size * when compressed) */ struct ec_mpi { unsigned char data[256 / 8]; }; void ec_point_copy (gcry_mpi_point_t dst, const gcry_mpi_point_t src); int ec_point_cmp (const gcry_mpi_point_t a, const gcry_mpi_point_t b); void ec_skey_create (gcry_mpi_t skey); void ec_keypair_create (gcry_mpi_point_t pkey, gcry_mpi_t skey); void ec_keypair_create_base (gcry_mpi_point_t pkey, gcry_mpi_t skey, const gcry_mpi_point_t base); /* --- Zero knowledge proofs --- */ struct proof_dl { struct ec_mpi r; struct ec_mpi a; }; struct proof_2dle { struct ec_mpi r; struct ec_mpi a; struct ec_mpi b; }; struct proof_0og { struct ec_mpi a1; struct ec_mpi a2; struct ec_mpi b1; struct ec_mpi b2; struct ec_mpi d1; struct ec_mpi d2; struct ec_mpi r1; struct ec_mpi r2; }; void smc_zkp_dl (gcry_mpi_point_t v, const gcry_mpi_t x, struct proof_dl *proof); int smc_zkp_dl_check (const gcry_mpi_point_t v, const struct proof_dl *proof); void smc_zkp_2dle (const gcry_mpi_point_t v, const gcry_mpi_point_t w, const gcry_mpi_point_t g1, const gcry_mpi_point_t g2, const gcry_mpi_t x, struct proof_2dle *proof); int smc_zkp_2dle_check (const gcry_mpi_point_t v, const gcry_mpi_point_t w, const gcry_mpi_point_t g1, const gcry_mpi_point_t g2, const struct proof_2dle *proof); void smc_zkp_0og (int m_is_gen, const gcry_mpi_point_t y, gcry_mpi_t r, gcry_mpi_point_t alpha, gcry_mpi_point_t beta, struct proof_0og *proof); int smc_zkp_0og_check (const gcry_mpi_point_t y, const gcry_mpi_point_t alpha, const gcry_mpi_point_t beta, const struct proof_0og *proof); /* --- Protocol implementation --- */ unsigned char *smc_gen_keyshare (struct BRANDT_Auction *ad, size_t *buflen); int smc_recv_keyshare (struct BRANDT_Auction *ad, const unsigned char *buf, size_t buflen, uint16_t sender_index); unsigned char *smc_encrypt_bid (struct BRANDT_Auction *ad, size_t *buflen); int smc_recv_encrypted_bid (struct BRANDT_Auction *ad, const unsigned char *buf, size_t buflen, uint16_t sender_index); unsigned char *fp_priv_compute_outcome (struct BRANDT_Auction *ad, size_t *buflen); int fp_priv_recv_outcome (struct BRANDT_Auction *ad, const unsigned char *buf, size_t buflen, uint16_t sender); unsigned char *fp_priv_decrypt_outcome (struct BRANDT_Auction *ad, size_t *buflen); int fp_priv_recv_decryption (struct BRANDT_Auction *ad, const unsigned char *buf, size_t buflen, uint16_t sender); unsigned char *fp_pub_compute_outcome (struct BRANDT_Auction *ad, size_t *buflen); int fp_pub_recv_outcome (struct BRANDT_Auction *ad, const unsigned char *buf, size_t buflen, uint16_t sender); unsigned char *fp_pub_decrypt_outcome (struct BRANDT_Auction *ad, size_t *buflen); int fp_pub_recv_decryption (struct BRANDT_Auction *ad, const unsigned char *buf, size_t buflen, uint16_t sender); int32_t fp_priv_determine_outcome (struct BRANDT_Auction *ad); /* --- Round dictionaries --- */ typedef int (*msg_in)(struct BRANDT_Auction *ad, const unsigned char *buf, size_t buflen, uint16_t sender); typedef unsigned char * (*msg_out)(struct BRANDT_Auction *ad, size_t *buflen); /** * stores the function pointers to receive functions for each state. * * The first index denotes if a first price auction or a M+1st price auction is * used. If it is 0, it is a first price auction, if it is 1, it is a M+1st * price auction. * * The second index denotes if the outcome should be public or private. A value * of 0 means a private outcome, while a value of 1 means public outcome. */ static const msg_in handler_in[auction_last][outcome_last][msg_last] = { [auction_firstPrice] = { [outcome_private] = { [msg_init] = &smc_recv_keyshare, [msg_bid] = &smc_recv_encrypted_bid, [msg_outcome] = &fp_priv_recv_outcome, [msg_decrypt] = &fp_priv_recv_decryption, }, [outcome_public] = { [msg_init] = &smc_recv_keyshare, [msg_bid] = &smc_recv_encrypted_bid, [msg_outcome] = &fp_pub_recv_outcome, [msg_decrypt] = &fp_pub_recv_decryption, }, }, [auction_mPlusFirstPrice] = { [outcome_private] = { [msg_init] = &smc_recv_keyshare, [msg_bid] = &smc_recv_encrypted_bid, }, [outcome_public] = { [msg_init] = &smc_recv_keyshare, [msg_bid] = &smc_recv_encrypted_bid, }, }, }; /** * stores the function pointers to message buffer creating functions for each * state. * * The first index denotes if a first price auction or a M+1st price auction is * used. If it is 0, it is a first price auction, if it is 1, it is a M+1st * price auction. * * The second index denotes if the outcome should be public or private. A value * of 0 means a private outcome, while a value of 1 means public outcome. */ static const msg_out handler_out[auction_last][outcome_last][msg_last] = { [auction_firstPrice] = { [outcome_private] = { [msg_init] = &smc_gen_keyshare, [msg_bid] = &smc_encrypt_bid, [msg_outcome] = &fp_priv_compute_outcome, [msg_decrypt] = &fp_priv_decrypt_outcome, }, [outcome_public] = { [msg_init] = &smc_gen_keyshare, [msg_bid] = &smc_encrypt_bid, [msg_outcome] = &fp_pub_compute_outcome, [msg_decrypt] = &fp_pub_decrypt_outcome, }, }, [auction_mPlusFirstPrice] = { [outcome_private] = { [msg_init] = &smc_gen_keyshare, [msg_bid] = &smc_encrypt_bid, }, [outcome_public] = { [msg_init] = &smc_gen_keyshare, [msg_bid] = &smc_encrypt_bid, }, }, }; #endif /* ifndef _BRANDT_CRYPTO_H */