gnunet-android

GNUnet for Android
Log | Files | Refs | README

gnunet_crypto_lib.h (144917B)


      1 /*
      2      This file is part of GNUnet.
      3      Copyright (C) 2001-2023 GNUnet e.V.
      4 
      5      GNUnet is free software: you can redistribute it and/or modify it
      6      under the terms of the GNU Affero General Public License as published
      7      by the Free Software Foundation, either version 3 of the License,
      8      or (at your option) any later version.
      9 
     10      GNUnet is distributed in the hope that it will be useful, but
     11      WITHOUT ANY WARRANTY; without even the implied warranty of
     12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     13      Affero General Public License for more details.
     14 
     15      You should have received a copy of the GNU Affero General Public License
     16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
     17 
     18      SPDX-License-Identifier: AGPL3.0-or-later
     19  */
     20 
     21 /**
     22  * @addtogroup libgnunetutil
     23  * Multi-function utilities library for GNUnet programs
     24  * @{
     25  *
     26  * @file include/gnunet_crypto_lib.h
     27  * @brief cryptographic primitives for GNUnet
     28  *
     29  * @author Christian Grothoff
     30  * @author Krista Bennett
     31  * @author Gerd Knorr <kraxel@bytesex.org>
     32  * @author Ioana Patrascu
     33  * @author Tzvetan Horozov
     34  * @author Jeffrey Burdges <burdges@gnunet.org>
     35  *
     36  * @defgroup crypto  Crypto library: cryptographic operations
     37  * Provides cryptographic primitives.
     38  *
     39  * @see [Documentation](https://gnunet.org/crypto-api)
     40  *
     41  * @defgroup hash  Crypto library: hash operations
     42  * Provides hashing and operations on hashes.
     43  *
     44  * @see [Documentation](https://gnunet.org/crypto-api)
     45  */
     46 
     47 #if ! defined (__GNUNET_UTIL_LIB_H_INSIDE__)
     48 #error "Only <gnunet_util_lib.h> can be included directly."
     49 #endif
     50 
     51 #ifndef GNUNET_CRYPTO_LIB_H
     52 #define GNUNET_CRYPTO_LIB_H
     53 
     54 #ifdef __cplusplus
     55 extern "C" {
     56 #if 0 /* keep Emacsens' auto-indent happy */
     57 }
     58 #endif
     59 #endif
     60 
     61 
     62 #include "gnunet_common.h"
     63 #include <stdbool.h>
     64 #include <sodium.h>
     65 
     66 /**
     67  * The identity of the host (wraps the signing key of the peer).
     68  */
     69 struct GNUNET_PeerIdentity;
     70 
     71 #include <gcrypt.h>
     72 
     73 
     74 /**
     75  * Maximum length of an ECC signature.
     76  * Note: round up to multiple of 8 minus 2 for alignment.
     77  */
     78 #define GNUNET_CRYPTO_ECC_SIGNATURE_DATA_ENCODING_LENGTH 126
     79 
     80 
     81 /**
     82  * Desired quality level for random numbers.
     83  * @ingroup crypto
     84  */
     85 enum GNUNET_CRYPTO_Quality
     86 {
     87   /**
     88    * No good quality of the operation is needed (i.e.,
     89    * random numbers can be pseudo-random).
     90    * @ingroup crypto
     91    */
     92   GNUNET_CRYPTO_QUALITY_WEAK,
     93 
     94   /**
     95    * High-quality operations are desired.
     96    * @ingroup crypto
     97    */
     98   GNUNET_CRYPTO_QUALITY_STRONG,
     99 
    100   /**
    101    * Randomness for IVs etc. is required.
    102    * @ingroup crypto
    103    */
    104   GNUNET_CRYPTO_QUALITY_NONCE
    105 };
    106 
    107 
    108 /**
    109  * @brief length of the sessionkey in bytes (256 BIT sessionkey)
    110  */
    111 #define GNUNET_CRYPTO_AES_KEY_LENGTH (256 / 8)
    112 
    113 /**
    114  * Length of a hash value
    115  */
    116 #define GNUNET_CRYPTO_HASH_LENGTH (512 / 8)
    117 
    118 /**
    119  * How many characters (without 0-terminator) are our ASCII-encoded
    120  * public keys (ECDSA/EDDSA/ECDHE).
    121  */
    122 #define GNUNET_CRYPTO_PKEY_ASCII_LENGTH 52
    123 
    124 /**
    125  * @brief 0-terminated ASCII encoding of a struct GNUNET_HashCode.
    126  */
    127 struct GNUNET_CRYPTO_HashAsciiEncoded
    128 {
    129   unsigned char encoding[104];
    130 };
    131 
    132 
    133 GNUNET_NETWORK_STRUCT_BEGIN
    134 
    135 
    136 /**
    137  * @brief header of what an ECC signature signs
    138  *        this must be followed by "size - 8" bytes of
    139  *        the actual signed data
    140  */
    141 struct GNUNET_CRYPTO_SignaturePurpose
    142 {
    143   /**
    144    * How many bytes does this signature sign?
    145    * (including this purpose header); in network
    146    * byte order (!).
    147    */
    148   uint32_t size GNUNET_PACKED;
    149 
    150   /**
    151    * What does this signature vouch for?  This
    152    * must contain a GNUNET_SIGNATURE_PURPOSE_XXX
    153    * constant (from gnunet_signatures.h).  In
    154    * network byte order!
    155    */
    156   uint32_t purpose GNUNET_PACKED;
    157 };
    158 
    159 
    160 /**
    161  * @brief an ECC signature using EdDSA.
    162  * See cr.yp.to/papers.html#ed25519
    163  */
    164 struct GNUNET_CRYPTO_EddsaSignature
    165 {
    166   /**
    167    * R value.
    168    */
    169   unsigned char r[256 / 8];
    170 
    171   /**
    172    * S value.
    173    */
    174   unsigned char s[256 / 8];
    175 };
    176 
    177 
    178 /**
    179  * @brief an ECC signature using ECDSA
    180  */
    181 struct GNUNET_CRYPTO_EcdsaSignature
    182 {
    183   /**
    184    * R value.
    185    */
    186   unsigned char r[256 / 8];
    187 
    188   /**
    189    * S value.
    190    */
    191   unsigned char s[256 / 8];
    192 };
    193 
    194 
    195 /**
    196  * Public ECC key (always for curve Ed25519) encoded in a format
    197  * suitable for network transmission and EdDSA signatures.  Refer
    198  * to section 5.1.3 of rfc8032, for a thorough explanation of how
    199  * this value maps to the x- and y-coordinates.
    200  */
    201 struct GNUNET_CRYPTO_EddsaPublicKey
    202 {
    203   /**
    204    * Point Q consists of a y-value mod p (256 bits); the x-value is
    205    * always positive. The point is stored in Ed25519 standard
    206    * compact format.
    207    */
    208   unsigned char q_y[256 / 8];
    209 };
    210 
    211 
    212 /**
    213  * Public ECC key (always for Curve25519) encoded in a format suitable
    214  * for network transmission and ECDSA signatures.
    215  */
    216 struct GNUNET_CRYPTO_EcdsaPublicKey
    217 {
    218   /**
    219    * Q consists of an x- and a y-value, each mod p (256 bits), given
    220    * here in affine coordinates and Ed25519 standard compact format.
    221    */
    222   unsigned char q_y[256 / 8];
    223 };
    224 
    225 
    226 /**
    227  * The identity of the host (wraps the signing key of the peer).
    228  */
    229 struct GNUNET_PeerIdentity
    230 {
    231   struct GNUNET_CRYPTO_EddsaPublicKey public_key;
    232 };
    233 
    234 /**
    235  * Public ECC key (always for Curve25519) encoded in a format suitable
    236  * for network transmission and encryption (ECDH),
    237  * See http://cr.yp.to/ecdh.html
    238  */
    239 struct GNUNET_CRYPTO_EcdhePublicKey
    240 {
    241   /**
    242    * Q consists of an x- and a y-value, each mod p (256 bits), given
    243    * here in affine coordinates and Ed25519 standard compact format.
    244    */
    245   unsigned char q_y[256 / 8];
    246 };
    247 
    248 
    249 /**
    250  * Private ECC key encoded for transmission.  To be used only for ECDH
    251  * key exchange (ECDHE to be precise).
    252  */
    253 struct GNUNET_CRYPTO_EcdhePrivateKey
    254 {
    255   /**
    256    * d is a value mod n, where n has at most 256 bits.
    257    */
    258   unsigned char d[256 / 8];
    259 };
    260 
    261 /**
    262  * Private ECC key encoded for transmission.  To be used only for ECDSA
    263  * signatures.
    264  */
    265 struct GNUNET_CRYPTO_EcdsaPrivateKey
    266 {
    267   /**
    268    * d is a value mod n, where n has at most 256 bits.
    269    */
    270   unsigned char d[256 / 8];
    271 };
    272 
    273 /**
    274  * Private ECC key encoded for transmission.  To be used only for EdDSA
    275  * signatures.
    276  */
    277 struct GNUNET_CRYPTO_EddsaPrivateKey
    278 {
    279   /**
    280    * d is a value mod n, where n has at most 256 bits.
    281    */
    282   unsigned char d[256 / 8];
    283 };
    284 
    285 
    286 /**
    287  * Private ECC scalar encoded for transmission.  To be used only for EdDSA
    288  * signatures.
    289  */
    290 struct GNUNET_CRYPTO_EddsaPrivateScalar
    291 {
    292   /**
    293    * s is the expandedprivate 512-bit scalar of a private key.
    294    */
    295   unsigned char s[512 / 8];
    296 };
    297 
    298 /**
    299  * Private ECC key material encoded for transmission.  To be used only for
    300  * Edx25519 signatures.  An initial key corresponds to data from the key
    301  * expansion and clamping in the EdDSA key generation.
    302  */
    303 struct GNUNET_CRYPTO_Edx25519PrivateKey
    304 {
    305   /**
    306    * a is a value mod n, where n has at most 256 bits.  It is the first half of
    307    * the seed-expansion of EdDSA and will be clamped.
    308    */
    309   unsigned char a[256 / 8];
    310 
    311   /**
    312    * b consists of 32 bytes which where originally the lower 32bytes of the key
    313    * expansion.  Subsequent calls to derive_private will change this value, too.
    314    */
    315   unsigned char b[256 / 8];
    316 };
    317 
    318 
    319 /**
    320  * Public ECC key (always for curve Ed25519) encoded in a format suitable for
    321  * network transmission and Edx25519 (same as EdDSA) signatures.  Refer to
    322  * section 5.1.3 of rfc8032, for a thorough explanation of how this value maps
    323  * to the x- and y-coordinates.
    324  */
    325 struct GNUNET_CRYPTO_Edx25519PublicKey
    326 {
    327   /**
    328    * Point Q consists of a y-value mod p (256 bits); the x-value is
    329    * always positive. The point is stored in Ed25519 standard
    330    * compact format.
    331    */
    332   unsigned char q_y[256 / 8];
    333 };
    334 
    335 /**
    336  * @brief an ECC signature using Edx25519 (same as in EdDSA).
    337  */
    338 struct GNUNET_CRYPTO_Edx25519Signature
    339 {
    340   /**
    341    * R value.
    342    */
    343   unsigned char r[256 / 8];
    344 
    345   /**
    346    * S value.
    347    */
    348   unsigned char s[256 / 8];
    349 };
    350 
    351 /**
    352  * Special private ECC key generated by GNUNET_CRYPTO_ecdhe_elligator_key_create.
    353  * To be used only for the Elligator KEM.
    354  * Encoded for transmission.
    355  */
    356 struct GNUNET_CRYPTO_ElligatorEcdhePrivateKey
    357 {
    358   /**
    359    * d is a value mod n, where n has at most 256 bits.
    360    */
    361   unsigned char d[256 / 8];
    362 };
    363 
    364 /**
    365  * Elligator representative (always for Curve25519)
    366  */
    367 struct GNUNET_CRYPTO_ElligatorRepresentative
    368 {
    369   /**
    370    * Represents an element of Curve25519 finite field.
    371    * Always smaller than 2 ^ 254 - 10 -> Needs to be serialized into a random-looking byte stream before transmission.
    372    */
    373   uint8_t r[256 / 8];
    374 };
    375 
    376 /**
    377  * Key type for the generic public key union
    378  */
    379 enum GNUNET_CRYPTO_KeyType
    380 {
    381   /**
    382    * The identity type. The value is the same as the
    383    * PKEY record type.
    384    */
    385   GNUNET_PUBLIC_KEY_TYPE_ECDSA = 65536,
    386 
    387   /**
    388    * EDDSA identity. The value is the same as the EDKEY
    389    * record type.
    390    */
    391   GNUNET_PUBLIC_KEY_TYPE_EDDSA = 65556
    392 };
    393 
    394 /**
    395  * A private key for an identity as per LSD0001.
    396  * Note that these types are NOT packed and MUST NOT be used in RPC
    397  * messages. Use the respective serialization functions.
    398  */
    399 struct GNUNET_CRYPTO_BlindablePrivateKey
    400 {
    401   /**
    402    * Type of public key.
    403    * Defined by the GNS zone type value.
    404    * In NBO.
    405    */
    406   uint32_t type;
    407 
    408   union
    409   {
    410     /**
    411      * An ECDSA identity key.
    412      */
    413     struct GNUNET_CRYPTO_EcdsaPrivateKey ecdsa_key;
    414 
    415     /**
    416      * AN EdDSA identtiy key
    417      */
    418     struct GNUNET_CRYPTO_EddsaPrivateKey eddsa_key;
    419   };
    420 };
    421 
    422 
    423 /**
    424  * An identity key as per LSD0001.
    425  */
    426 struct GNUNET_CRYPTO_BlindablePublicKey
    427 {
    428   /**
    429    * Type of public key.
    430    * Defined by the GNS zone type value.
    431    * In NBO.
    432    */
    433   uint32_t type;
    434 
    435   union
    436   {
    437     /**
    438      * An ECDSA identity key.
    439      */
    440     struct GNUNET_CRYPTO_EcdsaPublicKey ecdsa_key;
    441 
    442     /**
    443      * AN EdDSA identtiy key
    444      */
    445     struct GNUNET_CRYPTO_EddsaPublicKey eddsa_key;
    446   };
    447 };
    448 
    449 /**
    450  * A public key used for decryption.
    451  * Right now, only X25519/ECDHE keys supported.
    452  */
    453 struct GNUNET_CRYPTO_HpkePrivateKey
    454 {
    455   /**
    456    * Type of key.
    457    * In NBO.
    458    */
    459   uint32_t type;
    460 
    461   union
    462   {
    463     /**
    464      * An ECDHE/X25519 key
    465      */
    466     struct GNUNET_CRYPTO_EcdhePrivateKey ecdhe_key;
    467 
    468   };
    469 };
    470 
    471 
    472 /**
    473  * A public key used for encryption.
    474  * Right now, only X25519/ECDHE keys supported.
    475  */
    476 struct GNUNET_CRYPTO_HpkePublicKey
    477 {
    478   /**
    479    * Type of key.
    480    * In NBO.
    481    */
    482   uint32_t type;
    483 
    484   union
    485   {
    486     /**
    487      * An ECDHE/X25519 key
    488      */
    489     struct GNUNET_CRYPTO_EcdhePublicKey ecdhe_key;
    490 
    491   };
    492 };
    493 
    494 /**
    495  * An identity signature as per LSD0001.
    496  */
    497 struct GNUNET_CRYPTO_BlindableKeySignature
    498 {
    499   /**
    500    * Type of signature.
    501    * Defined by the GNS zone type value.
    502    * In NBO.
    503    */
    504   uint32_t type;
    505 
    506   union
    507   {
    508     /**
    509      * An ECDSA signature
    510      */
    511     struct GNUNET_CRYPTO_EcdsaSignature ecdsa_signature;
    512 
    513     /**
    514      * AN EdDSA signature
    515      */
    516     struct GNUNET_CRYPTO_EddsaSignature eddsa_signature;
    517   };
    518 };
    519 
    520 /**
    521  * @brief type for session keys
    522  */
    523 struct GNUNET_CRYPTO_SymmetricSessionKey
    524 {
    525   /**
    526    * Actual key for AES.
    527    */
    528   unsigned char aes_key[GNUNET_CRYPTO_AES_KEY_LENGTH];
    529 
    530   /**
    531    * Actual key for TwoFish.
    532    */
    533   unsigned char twofish_key[GNUNET_CRYPTO_AES_KEY_LENGTH];
    534 };
    535 
    536 /**
    537  * Type of a nonce used for challenges.
    538  */
    539 struct GNUNET_CRYPTO_ChallengeNonceP
    540 {
    541   /**
    542    * The value of the nonce.  Note that this is NOT a hash.
    543    */
    544   struct GNUNET_ShortHashCode value;
    545 };
    546 
    547 GNUNET_NETWORK_STRUCT_END
    548 
    549 /**
    550  * @brief IV for sym cipher
    551  *
    552  * NOTE: must be smaller (!) in size than the
    553  * `struct GNUNET_HashCode`.
    554  */
    555 struct GNUNET_CRYPTO_SymmetricInitializationVector
    556 {
    557   unsigned char aes_iv[GNUNET_CRYPTO_AES_KEY_LENGTH / 2];
    558 
    559   unsigned char twofish_iv[GNUNET_CRYPTO_AES_KEY_LENGTH / 2];
    560 };
    561 
    562 
    563 /**
    564  * @brief type for (message) authentication keys
    565  */
    566 struct GNUNET_CRYPTO_AuthKey
    567 {
    568   unsigned char key[GNUNET_CRYPTO_HASH_LENGTH];
    569 };
    570 
    571 
    572 /**
    573  * Size of paillier plain texts and public keys.
    574  * Private keys and ciphertexts are twice this size.
    575  */
    576 #define GNUNET_CRYPTO_PAILLIER_BITS 2048
    577 
    578 
    579 /**
    580  * Paillier public key.
    581  */
    582 struct GNUNET_CRYPTO_PaillierPublicKey
    583 {
    584   /**
    585    * N value.
    586    */
    587   unsigned char n[GNUNET_CRYPTO_PAILLIER_BITS / 8];
    588 };
    589 
    590 
    591 /**
    592  * Paillier private key.
    593  */
    594 struct GNUNET_CRYPTO_PaillierPrivateKey
    595 {
    596   /**
    597    * Lambda-component of the private key.
    598    */
    599   unsigned char lambda[GNUNET_CRYPTO_PAILLIER_BITS / 8];
    600   /**
    601    * Mu-component of the private key.
    602    */
    603   unsigned char mu[GNUNET_CRYPTO_PAILLIER_BITS / 8];
    604 };
    605 
    606 
    607 /**
    608  * Paillier ciphertext.
    609  */
    610 struct GNUNET_CRYPTO_PaillierCiphertext
    611 {
    612   /**
    613    * Guaranteed minimum number of homomorphic operations with this ciphertext,
    614    * in network byte order (NBO).
    615    */
    616   int32_t remaining_ops GNUNET_PACKED;
    617 
    618   /**
    619    * The bits of the ciphertext.
    620    */
    621   unsigned char bits[GNUNET_CRYPTO_PAILLIER_BITS * 2 / 8];
    622 };
    623 
    624 
    625 /**
    626  * Curve25519 Scalar
    627  */
    628 struct GNUNET_CRYPTO_Cs25519Scalar
    629 {
    630   /**
    631    * 32 byte scalar
    632    */
    633   unsigned char d[crypto_core_ed25519_SCALARBYTES];
    634 };
    635 
    636 
    637 /**
    638  * Curve25519 point
    639  */
    640 struct GNUNET_CRYPTO_Cs25519Point
    641 {
    642   /**
    643    * This is a point on the Curve25519.
    644    * The x coordinate can be restored using the y coordinate
    645    */
    646   unsigned char y[crypto_core_ed25519_BYTES];
    647 };
    648 
    649 
    650 /**
    651  * The private information of an Schnorr key pair.
    652  */
    653 struct GNUNET_CRYPTO_CsPrivateKey
    654 {
    655   struct GNUNET_CRYPTO_Cs25519Scalar scalar;
    656 };
    657 
    658 
    659 /**
    660  * The public information of an Schnorr key pair.
    661  */
    662 struct GNUNET_CRYPTO_CsPublicKey
    663 {
    664   struct GNUNET_CRYPTO_Cs25519Point point;
    665 };
    666 
    667 
    668 /**
    669  * Secret used for blinding (alpha and beta).
    670  */
    671 struct GNUNET_CRYPTO_CsBlindingSecret
    672 {
    673   struct GNUNET_CRYPTO_Cs25519Scalar alpha;
    674   struct GNUNET_CRYPTO_Cs25519Scalar beta;
    675 };
    676 
    677 
    678 /**
    679  * the private r used in the signature
    680  */
    681 struct GNUNET_CRYPTO_CsRSecret
    682 {
    683   struct GNUNET_CRYPTO_Cs25519Scalar scalar;
    684 };
    685 
    686 
    687 /**
    688  * the public R (derived from r) used in c
    689  */
    690 struct GNUNET_CRYPTO_CsRPublic
    691 {
    692   struct GNUNET_CRYPTO_Cs25519Point point;
    693 };
    694 
    695 
    696 /**
    697  * Schnorr c to be signed
    698  */
    699 struct GNUNET_CRYPTO_CsC
    700 {
    701   struct GNUNET_CRYPTO_Cs25519Scalar scalar;
    702 };
    703 
    704 
    705 /**
    706  * s in the signature
    707  */
    708 struct GNUNET_CRYPTO_CsS
    709 {
    710   struct GNUNET_CRYPTO_Cs25519Scalar scalar;
    711 };
    712 
    713 
    714 /**
    715  * blinded s in the signature
    716  */
    717 struct GNUNET_CRYPTO_CsBlindS
    718 {
    719   struct GNUNET_CRYPTO_Cs25519Scalar scalar;
    720 };
    721 
    722 
    723 /**
    724  * CS Signtature containing scalar s and point R
    725  */
    726 struct GNUNET_CRYPTO_CsSignature
    727 {
    728   /**
    729    * Schnorr signatures are composed of a scalar s and a curve point
    730    */
    731   struct GNUNET_CRYPTO_CsS s_scalar;
    732 
    733   /**
    734    * Curve point of the Schnorr signature.
    735    */
    736   struct GNUNET_CRYPTO_CsRPublic r_point;
    737 };
    738 
    739 
    740 /**
    741  * Nonce for the session, picked by client,
    742  * shared with the signer.
    743  */
    744 struct GNUNET_CRYPTO_CsSessionNonce
    745 {
    746   /*a nonce*/
    747   unsigned char snonce[256 / 8];
    748 };
    749 
    750 
    751 /**
    752  * Nonce for computing blinding factors. Not
    753  * shared with the signer.
    754  */
    755 struct GNUNET_CRYPTO_CsBlindingNonce
    756 {
    757   /*a nonce*/
    758   unsigned char bnonce[256 / 8];
    759 };
    760 
    761 
    762 /* **************** Functions and Macros ************* */
    763 
    764 /**
    765  * @ingroup crypto
    766  * Seed a weak random generator. Only #GNUNET_CRYPTO_QUALITY_WEAK-mode generator
    767  * can be seeded.
    768  *
    769  * @param seed the seed to use
    770  */
    771 void
    772 GNUNET_CRYPTO_seed_weak_random (int32_t seed);
    773 
    774 
    775 /**
    776  * @ingroup hash
    777  * Calculate the checksum of a buffer in one step.
    778  *
    779  * @param buf buffer to calculate CRC over
    780  * @param len number of bytes in @a buf
    781  * @return crc8 value
    782  */
    783 uint8_t
    784 GNUNET_CRYPTO_crc8_n (const void *buf, size_t len);
    785 
    786 
    787 /**
    788  * Perform an incremental step in a CRC16 (for TCP/IP) calculation.
    789  *
    790  * @param sum current sum, initially 0
    791  * @param buf buffer to calculate CRC over (must be 16-bit aligned)
    792  * @param len number of bytes in @a buf, must be multiple of 2
    793  * @return updated crc sum (must be subjected to #GNUNET_CRYPTO_crc16_finish to get actual crc16)
    794  */
    795 uint32_t
    796 GNUNET_CRYPTO_crc16_step (uint32_t sum, const void *buf, size_t len);
    797 
    798 
    799 /**
    800  * Convert results from GNUNET_CRYPTO_crc16_step to final crc16.
    801  *
    802  * @param sum cumulative sum
    803  * @return crc16 value
    804  */
    805 uint16_t
    806 GNUNET_CRYPTO_crc16_finish (uint32_t sum);
    807 
    808 
    809 /**
    810  * @ingroup hash
    811  * Calculate the checksum of a buffer in one step.
    812  *
    813  * @param buf buffer to calculate CRC over (must be 16-bit aligned)
    814  * @param len number of bytes in @a buf, must be multiple of 2
    815  * @return crc16 value
    816  */
    817 uint16_t
    818 GNUNET_CRYPTO_crc16_n (const void *buf, size_t len);
    819 
    820 
    821 /**
    822  * @ingroup hash
    823  * Compute the CRC32 checksum for the first len
    824  * bytes of the buffer.
    825  *
    826  * @param buf the data over which we're taking the CRC
    827  * @param len the length of the buffer @a buf in bytes
    828  * @return the resulting CRC32 checksum
    829  */
    830 int32_t
    831 GNUNET_CRYPTO_crc32_n (const void *buf, size_t len);
    832 
    833 /**
    834  * @ingroup crypto
    835  * Zero out @a buffer, securely against compiler optimizations.
    836  * Used to delete key material.
    837  *
    838  * @param buffer the buffer to zap
    839  * @param length buffer length
    840  */
    841 void
    842 GNUNET_CRYPTO_zero_keys (void *buffer, size_t length);
    843 
    844 
    845 /**
    846  * @ingroup crypto
    847  * Fill block with a random values.
    848  *
    849  * @param mode desired quality of the random number
    850  * @param[out] buffer the buffer to fill
    851  * @param length buffer length
    852  */
    853 void
    854 GNUNET_CRYPTO_random_block (enum GNUNET_CRYPTO_Quality mode,
    855                             void *buffer,
    856                             size_t length);
    857 
    858 
    859 /**
    860  * @ingroup crypto
    861  * Fill UUID with a timeflake pseudo-random value.  Note that
    862  * timeflakes use only 80 bits of randomness and 48 bits
    863  * to encode a timestamp in milliseconds. So what we return
    864  * here is not a completely random number.
    865  *
    866  * @param mode desired quality of the random number
    867  * @param[out] uuid the value to fill
    868  */
    869 void
    870 GNUNET_CRYPTO_random_timeflake (enum GNUNET_CRYPTO_Quality mode,
    871                                 struct GNUNET_Uuid *uuid);
    872 
    873 
    874 /**
    875  * @ingroup crypto
    876  * Produce a random value.
    877  *
    878  * @param mode desired quality of the random number
    879  * @param i the upper limit (exclusive) for the random number
    880  * @return a random value in the interval [0,@a i) (exclusive).
    881  */
    882 uint32_t
    883 GNUNET_CRYPTO_random_u32 (enum GNUNET_CRYPTO_Quality mode, uint32_t i);
    884 
    885 
    886 /**
    887  * @ingroup crypto
    888  * Generate a random unsigned 64-bit value.
    889  *
    890  * @param mode desired quality of the random number
    891  * @param max value returned will be in range [0,@a max) (exclusive)
    892  * @return random 64-bit number
    893  */
    894 uint64_t
    895 GNUNET_CRYPTO_random_u64 (enum GNUNET_CRYPTO_Quality mode, uint64_t max);
    896 
    897 
    898 /**
    899  * @ingroup crypto
    900  * Get an array with a random permutation of the
    901  * numbers 0...n-1.
    902  * @param mode #GNUNET_CRYPTO_QUALITY_STRONG if the strong (but expensive) PRNG should be used,
    903  *             #GNUNET_CRYPTO_QUALITY_WEAK or #GNUNET_CRYPTO_QUALITY_NONCE otherwise
    904  * @param n the size of the array
    905  * @return the permutation array (allocated from heap)
    906  */
    907 unsigned int *
    908 GNUNET_CRYPTO_random_permute (enum GNUNET_CRYPTO_Quality mode, unsigned int n);
    909 
    910 
    911 /**
    912  * @ingroup crypto
    913  * Create a new random session key.
    914  *
    915  * @param key key to initialize
    916  */
    917 void
    918 GNUNET_CRYPTO_symmetric_create_session_key (
    919   struct GNUNET_CRYPTO_SymmetricSessionKey *key);
    920 
    921 
    922 /**
    923  * @ingroup crypto
    924  * Encrypt a block using a symmetric sessionkey.
    925  *
    926  * @param block the block to encrypt
    927  * @param size the size of the @a block
    928  * @param sessionkey the key used to encrypt
    929  * @param iv the initialization vector to use, use INITVALUE
    930  *        for streams.
    931  * @return the size of the encrypted block, -1 for errors
    932  */
    933 ssize_t
    934 GNUNET_CRYPTO_symmetric_encrypt (
    935   const void *block,
    936   size_t size,
    937   const struct GNUNET_CRYPTO_SymmetricSessionKey *sessionkey,
    938   const struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
    939   void *result);
    940 
    941 
    942 /**
    943  * @ingroup crypto
    944  * Decrypt a given block using a symmetric sessionkey.
    945  *
    946  * @param block the data to decrypt, encoded as returned by encrypt
    947  * @param size how big is the block?
    948  * @param sessionkey the key used to decrypt
    949  * @param iv the initialization vector to use
    950  * @param result address to store the result at
    951  * @return -1 on failure, size of decrypted block on success
    952  */
    953 ssize_t
    954 GNUNET_CRYPTO_symmetric_decrypt (
    955   const void *block,
    956   size_t size,
    957   const struct GNUNET_CRYPTO_SymmetricSessionKey *sessionkey,
    958   const struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
    959   void *result);
    960 
    961 
    962 /**
    963  * @ingroup crypto
    964  * @brief Derive an IV
    965  * @param iv initialization vector
    966  * @param skey session key
    967  * @param salt salt for the derivation
    968  * @param salt_len size of the @a salt
    969  * @param ... pairs of void * & size_t for context chunks, terminated by NULL
    970  */
    971 void
    972 GNUNET_CRYPTO_symmetric_derive_iv (
    973   struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
    974   const struct GNUNET_CRYPTO_SymmetricSessionKey *skey,
    975   const void *salt,
    976   size_t salt_len,
    977   ...);
    978 
    979 
    980 /**
    981  * @brief Derive an IV
    982  * @param iv initialization vector
    983  * @param skey session key
    984  * @param salt salt for the derivation
    985  * @param salt_len size of the @a salt
    986  * @param argp pairs of void * & size_t for context chunks, terminated by NULL
    987  */
    988 void
    989 GNUNET_CRYPTO_symmetric_derive_iv_v (
    990   struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
    991   const struct GNUNET_CRYPTO_SymmetricSessionKey *skey,
    992   const void *salt,
    993   size_t salt_len,
    994   va_list argp);
    995 
    996 
    997 /**
    998  * @ingroup hash
    999  * Convert hash to ASCII encoding.
   1000  * @param block the hash code
   1001  * @param result where to store the encoding (struct GNUNET_CRYPTO_HashAsciiEncoded can be
   1002  *  safely cast to char*, a '\\0' termination is set).
   1003  */
   1004 void
   1005 GNUNET_CRYPTO_hash_to_enc (const struct GNUNET_HashCode *block,
   1006                            struct GNUNET_CRYPTO_HashAsciiEncoded *result);
   1007 
   1008 
   1009 /**
   1010  * @ingroup hash
   1011  * Convert ASCII encoding back to a 'struct GNUNET_HashCode'
   1012  *
   1013  * @param enc the encoding
   1014  * @param enclen number of characters in @a enc (without 0-terminator, which can be missing)
   1015  * @param result where to store the hash code
   1016  * @return #GNUNET_OK on success, #GNUNET_SYSERR if result has the wrong encoding
   1017  */
   1018 enum GNUNET_GenericReturnValue
   1019 GNUNET_CRYPTO_hash_from_string2 (const char *enc,
   1020                                  size_t enclen,
   1021                                  struct GNUNET_HashCode *result);
   1022 
   1023 
   1024 /**
   1025  * @ingroup hash
   1026  * Convert ASCII encoding back to `struct GNUNET_HashCode`
   1027  *
   1028  * @param enc the encoding
   1029  * @param result where to store the hash code
   1030  * @return #GNUNET_OK on success, #GNUNET_SYSERR if result has the wrong encoding
   1031  */
   1032 #define GNUNET_CRYPTO_hash_from_string(enc, result) \
   1033   GNUNET_CRYPTO_hash_from_string2 (enc, strlen (enc), result)
   1034 
   1035 
   1036 /**
   1037  * @ingroup hash
   1038  *
   1039  * Compute the distance between 2 hashcodes.  The
   1040  * computation must be fast, not involve @a a[0] or @a a[4] (they're used
   1041  * elsewhere), and be somewhat consistent. And of course, the result
   1042  * should be a positive number.
   1043  *
   1044  * @param a some hash code
   1045  * @param b some hash code
   1046  * @return number between 0 and UINT32_MAX
   1047  */
   1048 uint32_t
   1049 GNUNET_CRYPTO_hash_distance_u32 (const struct GNUNET_HashCode *a,
   1050                                  const struct GNUNET_HashCode *b);
   1051 
   1052 
   1053 /**
   1054  * @ingroup hash
   1055  * Compute hash of a given block.
   1056  *
   1057  * @param block the data to hash
   1058  * @param size size of the @a block
   1059  * @param ret pointer to where to write the hashcode
   1060  */
   1061 void
   1062 GNUNET_CRYPTO_hash (const void *block,
   1063                     size_t size,
   1064                     struct GNUNET_HashCode *ret);
   1065 
   1066 
   1067 /**
   1068  * Value for a salt for #GNUNET_CRYPTO_pow_hash().
   1069  */
   1070 struct GNUNET_CRYPTO_PowSalt
   1071 {
   1072   char salt[crypto_pwhash_argon2id_SALTBYTES];
   1073 };
   1074 
   1075 
   1076 /**
   1077  * Calculate the 'proof-of-work' hash (an expensive hash).
   1078  *
   1079  * @param salt salt for the hash. Must be crypto_pwhash_argon2id_SALTBYTES long.
   1080  * @param buf data to hash
   1081  * @param buf_len number of bytes in @a buf
   1082  * @param result where to write the resulting hash
   1083  */
   1084 void
   1085 GNUNET_CRYPTO_pow_hash (const struct GNUNET_CRYPTO_PowSalt *salt,
   1086                         const void *buf,
   1087                         size_t buf_len,
   1088                         struct GNUNET_HashCode *result);
   1089 
   1090 
   1091 /**
   1092  * Context for cumulative hashing.
   1093  */
   1094 struct GNUNET_HashContext;
   1095 
   1096 
   1097 /**
   1098  * Start incremental hashing operation.
   1099  *
   1100  * @return context for incremental hash computation
   1101  */
   1102 struct GNUNET_HashContext *
   1103 GNUNET_CRYPTO_hash_context_start (void);
   1104 
   1105 
   1106 /**
   1107  * Make a copy of the hash computation.
   1108  *
   1109  * @param hc hash context to use (to continue hashing independently)
   1110  * @return copy of @a hc
   1111  */
   1112 struct GNUNET_HashContext *
   1113 GNUNET_CRYPTO_hash_context_copy (const struct GNUNET_HashContext *hc);
   1114 
   1115 
   1116 /**
   1117  * Add data to be hashed.
   1118  *
   1119  * @param hc cumulative hash context
   1120  * @param buf data to add
   1121  * @param size number of bytes in @a buf
   1122  */
   1123 void
   1124 GNUNET_CRYPTO_hash_context_read (struct GNUNET_HashContext *hc,
   1125                                  const void *buf,
   1126                                  size_t size);
   1127 
   1128 
   1129 /**
   1130  * Finish the hash computation.
   1131  *
   1132  * @param hc hash context to use, is freed in the process
   1133  * @param r_hash where to write the latest / final hash code
   1134  */
   1135 void
   1136 GNUNET_CRYPTO_hash_context_finish (struct GNUNET_HashContext *hc,
   1137                                    struct GNUNET_HashCode *r_hash);
   1138 
   1139 
   1140 /**
   1141  * Abort hashing, do not bother calculating final result.
   1142  *
   1143  * @param hc hash context to destroy
   1144  */
   1145 void
   1146 GNUNET_CRYPTO_hash_context_abort (struct GNUNET_HashContext *hc);
   1147 
   1148 
   1149 /**
   1150  * Calculate HMAC of a message (RFC 2104)
   1151  * TODO: Shouldn't this be the standard hmac function and
   1152  * the above be renamed?
   1153  *
   1154  * @param key secret key
   1155  * @param key_len secret key length
   1156  * @param plaintext input plaintext
   1157  * @param plaintext_len length of @a plaintext
   1158  * @param hmac where to store the hmac
   1159  */
   1160 void
   1161 GNUNET_CRYPTO_hmac_raw (const void *key,
   1162                         size_t key_len,
   1163                         const void *plaintext,
   1164                         size_t plaintext_len,
   1165                         struct GNUNET_HashCode *hmac);
   1166 
   1167 
   1168 /**
   1169  * @ingroup hash
   1170  * Calculate HMAC of a message (RFC 2104)
   1171  *
   1172  * @param key secret key
   1173  * @param plaintext input plaintext
   1174  * @param plaintext_len length of @a plaintext
   1175  * @param hmac where to store the hmac
   1176  */
   1177 void
   1178 GNUNET_CRYPTO_hmac (const struct GNUNET_CRYPTO_AuthKey *key,
   1179                     const void *plaintext,
   1180                     size_t plaintext_len,
   1181                     struct GNUNET_HashCode *hmac);
   1182 
   1183 
   1184 /**
   1185  * Function called once the hash computation over the
   1186  * specified file has completed.
   1187  *
   1188  * @param cls closure
   1189  * @param res resulting hash, NULL on error
   1190  */
   1191 typedef void
   1192 (*GNUNET_CRYPTO_HashCompletedCallback) (
   1193   void *cls,
   1194   const struct GNUNET_HashCode *res);
   1195 
   1196 
   1197 /**
   1198  * Handle to file hashing operation.
   1199  */
   1200 struct GNUNET_CRYPTO_FileHashContext;
   1201 
   1202 
   1203 /**
   1204  * @ingroup hash
   1205  * Compute the hash of an entire file.
   1206  *
   1207  * @param priority scheduling priority to use
   1208  * @param filename name of file to hash
   1209  * @param blocksize number of bytes to process in one task
   1210  * @param callback function to call upon completion
   1211  * @param callback_cls closure for @a callback
   1212  * @return NULL on (immediate) error
   1213  */
   1214 struct GNUNET_CRYPTO_FileHashContext *
   1215 GNUNET_CRYPTO_hash_file (enum GNUNET_SCHEDULER_Priority priority,
   1216                          const char *filename,
   1217                          size_t blocksize,
   1218                          GNUNET_CRYPTO_HashCompletedCallback callback,
   1219                          void *callback_cls);
   1220 
   1221 
   1222 /**
   1223  * Cancel a file hashing operation.
   1224  *
   1225  * @param fhc operation to cancel (callback must not yet have been invoked)
   1226  */
   1227 void
   1228 GNUNET_CRYPTO_hash_file_cancel (struct GNUNET_CRYPTO_FileHashContext *fhc);
   1229 
   1230 
   1231 /**
   1232  * @ingroup hash
   1233  * Create a random hash code.
   1234  *
   1235  * @param mode desired quality level
   1236  * @param result hash code that is randomized
   1237  */
   1238 void
   1239 GNUNET_CRYPTO_hash_create_random (enum GNUNET_CRYPTO_Quality mode,
   1240                                   struct GNUNET_HashCode *result);
   1241 
   1242 
   1243 /**
   1244  * @ingroup hash
   1245  * compute @a result = @a b - @a a
   1246  *
   1247  * @param a some hash code
   1248  * @param b some hash code
   1249  * @param result set to @a b - @a a
   1250  */
   1251 void
   1252 GNUNET_CRYPTO_hash_difference (const struct GNUNET_HashCode *a,
   1253                                const struct GNUNET_HashCode *b,
   1254                                struct GNUNET_HashCode *result);
   1255 
   1256 
   1257 /**
   1258  * @ingroup hash
   1259  * compute @a result = @a a + @a delta
   1260  *
   1261  * @param a some hash code
   1262  * @param delta some hash code
   1263  * @param result set to @a a + @a delta
   1264  */
   1265 void
   1266 GNUNET_CRYPTO_hash_sum (const struct GNUNET_HashCode *a,
   1267                         const struct GNUNET_HashCode *delta,
   1268                         struct GNUNET_HashCode *result);
   1269 
   1270 
   1271 /**
   1272  * @ingroup hash
   1273  * compute result = a ^ b
   1274  *
   1275  * @param a some hash code
   1276  * @param b some hash code
   1277  * @param result set to @a a ^ @a b
   1278  */
   1279 void
   1280 GNUNET_CRYPTO_hash_xor (const struct GNUNET_HashCode *a,
   1281                         const struct GNUNET_HashCode *b,
   1282                         struct GNUNET_HashCode *result);
   1283 
   1284 
   1285 /**
   1286  * Count the number of leading 0 bits in @a h.
   1287  *
   1288  * @param h a hash
   1289  * @return number of leading 0 bits in @a h
   1290  */
   1291 unsigned int
   1292 GNUNET_CRYPTO_hash_count_leading_zeros (const struct GNUNET_HashCode *h);
   1293 
   1294 
   1295 /**
   1296  * Count the number of tailing 0 bits in @a h.
   1297  *
   1298  * @param h a hash
   1299  * @return number of tailing 0 bits in @a h
   1300  */
   1301 unsigned int
   1302 GNUNET_CRYPTO_hash_count_tailing_zeros (const struct GNUNET_HashCode *h);
   1303 
   1304 
   1305 /**
   1306  * @ingroup hash
   1307  * Convert a hashcode into a key.
   1308  *
   1309  * @param hc hash code that serves to generate the key
   1310  * @param skey set to a valid session key
   1311  * @param iv set to a valid initialization vector
   1312  */
   1313 void
   1314 GNUNET_CRYPTO_hash_to_aes_key (
   1315   const struct GNUNET_HashCode *hc,
   1316   struct GNUNET_CRYPTO_SymmetricSessionKey *skey,
   1317   struct GNUNET_CRYPTO_SymmetricInitializationVector *iv);
   1318 
   1319 
   1320 /**
   1321  * @ingroup hash
   1322  * Compare function for HashCodes, producing a total ordering
   1323  * of all hashcodes.
   1324  *
   1325  * @param h1 some hash code
   1326  * @param h2 some hash code
   1327  * @return 1 if @a h1 > @a h2, -1 if @a h1 < @a h2 and 0 if @a h1 == @a h2.
   1328  */
   1329 int
   1330 GNUNET_CRYPTO_hash_cmp (const struct GNUNET_HashCode *h1,
   1331                         const struct GNUNET_HashCode *h2);
   1332 
   1333 
   1334 /**
   1335  * @ingroup hash
   1336  * Find out which of the two GNUNET_CRYPTO_hash codes is closer to target
   1337  * in the XOR metric (Kademlia).
   1338  *
   1339  * @param h1 some hash code
   1340  * @param h2 some hash code
   1341  * @param target some hash code
   1342  * @return -1 if @a h1 is closer, 1 if @a h2 is closer and 0 if @a h1== @a h2.
   1343  */
   1344 int
   1345 GNUNET_CRYPTO_hash_xorcmp (const struct GNUNET_HashCode *h1,
   1346                            const struct GNUNET_HashCode *h2,
   1347                            const struct GNUNET_HashCode *target);
   1348 
   1349 
   1350 /**
   1351  * @ingroup hash
   1352  * @brief Derive an authentication key
   1353  * @param key authentication key
   1354  * @param rkey root key
   1355  * @param salt salt
   1356  * @param salt_len size of the salt
   1357  * @param argp pair of void * & size_t for context chunks, terminated by NULL
   1358  */
   1359 void
   1360 GNUNET_CRYPTO_hmac_derive_key_v (
   1361   struct GNUNET_CRYPTO_AuthKey *key,
   1362   const struct GNUNET_CRYPTO_SymmetricSessionKey *rkey,
   1363   const void *salt,
   1364   size_t salt_len,
   1365   va_list argp);
   1366 
   1367 
   1368 /**
   1369  * @ingroup hash
   1370  * @brief Derive an authentication key
   1371  * @param key authentication key
   1372  * @param rkey root key
   1373  * @param salt salt
   1374  * @param salt_len size of the salt
   1375  * @param ... pair of void * & size_t for context chunks, terminated by NULL
   1376  */
   1377 void
   1378 GNUNET_CRYPTO_hmac_derive_key (
   1379   struct GNUNET_CRYPTO_AuthKey *key,
   1380   const struct GNUNET_CRYPTO_SymmetricSessionKey *rkey,
   1381   const void *salt,
   1382   size_t salt_len,
   1383   ...);
   1384 
   1385 
   1386 /**
   1387  * @ingroup hash
   1388  * @brief HKDF-Extract using SHA256. RFC 5869
   1389  * @param prk the PRK
   1390  * @param salt salt
   1391  * @param salt_len length of @a xts
   1392  * @param ikm source key material
   1393  * @param ikm_len length of @a skm
   1394  * @return #GNUNET_YES on success
   1395  */
   1396 enum GNUNET_GenericReturnValue
   1397 GNUNET_CRYPTO_hkdf_extract (struct GNUNET_ShortHashCode *prk,
   1398                             const void *salt,
   1399                             size_t salt_len,
   1400                             const void *ikm,
   1401                             size_t ikm_len);
   1402 
   1403 /**
   1404  * @ingroup hash
   1405  * @brief HKDF-Expand using SHA256. RFC 5869
   1406  * @param result buffer for the derived key, allocated by caller
   1407  * @param out_len desired length of the derived key
   1408  * @param prk pesudorandom key
   1409  * @param ... pair of void * & size_t for context chunks, terminated by NULL
   1410  * @return #GNUNET_YES on success
   1411  */
   1412 enum GNUNET_GenericReturnValue
   1413 GNUNET_CRYPTO_hkdf_expand (void *result,
   1414                            size_t out_len,
   1415                            const struct GNUNET_ShortHashCode *prk,
   1416                            ...);
   1417 
   1418 /**
   1419  * @ingroup hash
   1420  * @brief HKDF-Expand using SHA256. See #GNUNET_CRYPTO_hkdf_expand
   1421  * @param result buffer for the derived key, allocated by caller
   1422  * @param out_len desired length of the derived key
   1423  * @param argp va_list of void * & size_t pairs for context chunks
   1424  * @return #GNUNET_YES on success
   1425  */
   1426 enum GNUNET_GenericReturnValue
   1427 GNUNET_CRYPTO_hkdf_expand_v (void *result,
   1428                              size_t out_len,
   1429                              const struct GNUNET_ShortHashCode *prk,
   1430                              va_list argp);
   1431 
   1432 
   1433 /**
   1434  * @ingroup hash
   1435  * @brief A peculiar HKDF instantiation that tried to mimic Truncated NMAC.
   1436  * But, what it actually does is HKDF-Extract with SHA512 and instead of
   1437  * truncating the PRK, it uses it as a 64 byte key in the HKDF-Expand
   1438  * phase with SHA256.
   1439  * (Truncated NMAC would require us to, well, truncate it to 32 byte.)
   1440  * ONLY USE FOR COMPATIBILITY WITH OLDER KEY DERIVATIONS.
   1441  * Use the more standard #GNUNET_CRYPTO_hkdf_extract and
   1442  * #GNUNET_CRYPTO_HKDF_expand instead!
   1443  *
   1444  * @param result buffer for the derived key, allocated by caller
   1445  * @param out_len desired length of the derived key
   1446  * @param xts salt
   1447  * @param xts_len length of @a xts
   1448  * @param skm source key material
   1449  * @param skm_len length of @a skm
   1450  * @param ... pair of void * & size_t for context chunks, terminated by NULL
   1451  * @return #GNUNET_YES on success
   1452  */
   1453 enum GNUNET_GenericReturnValue
   1454 GNUNET_CRYPTO_hkdf_gnunet (void *result,
   1455                            size_t out_len,
   1456                            const void *xts,
   1457                            size_t xts_len,
   1458                            const void *skm,
   1459                            size_t skm_len,
   1460                            ...);
   1461 
   1462 
   1463 /**
   1464  * @ingroup hash
   1465  * @brief Derive key. See #GNUNET_CRYPTO_hkdf_gnunet
   1466  * @param result buffer for the derived key, allocated by caller
   1467  * @param out_len desired length of the derived key
   1468  * @param xts salt
   1469  * @param xts_len length of @a xts
   1470  * @param skm source key material
   1471  * @param skm_len length of @a skm
   1472  * @param argp va_list of void * & size_t pairs for context chunks
   1473  * @return #GNUNET_YES on success
   1474  */
   1475 enum GNUNET_GenericReturnValue
   1476 GNUNET_CRYPTO_hkdf_gnunet_v (void *result,
   1477                              size_t out_len,
   1478                              const void *xts,
   1479                              size_t xts_len,
   1480                              const void *skm,
   1481                              size_t skm_len,
   1482                              va_list argp);
   1483 
   1484 
   1485 /**
   1486  * @brief Derive key
   1487  * @param result buffer for the derived key, allocated by caller
   1488  * @param out_len desired length of the derived key
   1489  * @param xts salt
   1490  * @param xts_len length of @a xts
   1491  * @param skm source key material
   1492  * @param skm_len length of @a skm
   1493  * @param argp va_list of void * & size_t pairs for context chunks
   1494  * @return #GNUNET_YES on success
   1495  */
   1496 enum GNUNET_GenericReturnValue
   1497 GNUNET_CRYPTO_kdf_v (void *result,
   1498                      size_t out_len,
   1499                      const void *xts,
   1500                      size_t xts_len,
   1501                      const void *skm,
   1502                      size_t skm_len,
   1503                      va_list argp);
   1504 
   1505 
   1506 /**
   1507  * Deterministically generate a pseudo-random number uniformly from the
   1508  * integers modulo a libgcrypt mpi.
   1509  *
   1510  * @param[out] r MPI value set to the FDH
   1511  * @param n MPI to work modulo
   1512  * @param xts salt
   1513  * @param xts_len length of @a xts
   1514  * @param skm source key material
   1515  * @param skm_len length of @a skm
   1516  * @param ctx context string
   1517  */
   1518 void
   1519 GNUNET_CRYPTO_kdf_mod_mpi (gcry_mpi_t *r,
   1520                            gcry_mpi_t n,
   1521                            const void *xts,
   1522                            size_t xts_len,
   1523                            const void *skm,
   1524                            size_t skm_len,
   1525                            const char *ctx);
   1526 
   1527 
   1528 /**
   1529  * @ingroup hash
   1530  * @brief Derive key
   1531  * @param result buffer for the derived key, allocated by caller
   1532  * @param out_len desired length of the derived key
   1533  * @param xts salt
   1534  * @param xts_len length of @a xts
   1535  * @param skm source key material
   1536  * @param skm_len length of @a skm
   1537  * @param ... void * & size_t pairs for context chunks
   1538  * @return #GNUNET_YES on success
   1539  */
   1540 enum GNUNET_GenericReturnValue
   1541 GNUNET_CRYPTO_kdf (void *result,
   1542                    size_t out_len,
   1543                    const void *xts,
   1544                    size_t xts_len,
   1545                    const void *skm,
   1546                    size_t skm_len,
   1547                    ...);
   1548 
   1549 
   1550 /**
   1551  * @ingroup crypto
   1552  * Extract the public key for the given private key.
   1553  *
   1554  * @param priv the special elligator private key
   1555  * @param pub where to write the public key
   1556  */
   1557 void
   1558 GNUNET_CRYPTO_ecdsa_key_get_public (
   1559   const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv,
   1560   struct GNUNET_CRYPTO_EcdsaPublicKey *pub);
   1561 
   1562 /**
   1563  * @ingroup crypto
   1564  * Extract the public key for the given private key.
   1565  *
   1566  * @param priv the private key
   1567  * @param pub where to write the public key
   1568  */
   1569 void
   1570 GNUNET_CRYPTO_eddsa_key_get_public (
   1571   const struct GNUNET_CRYPTO_EddsaPrivateKey *priv,
   1572   struct GNUNET_CRYPTO_EddsaPublicKey *pub);
   1573 
   1574 /**
   1575  * @ingroup crypto
   1576  * Extract the public key for the given private key.
   1577  *
   1578  * @param priv the private key
   1579  * @param pub where to write the public key
   1580  */
   1581 void
   1582 GNUNET_CRYPTO_edx25519_key_get_public (
   1583   const struct GNUNET_CRYPTO_Edx25519PrivateKey *priv,
   1584   struct GNUNET_CRYPTO_Edx25519PublicKey *pub);
   1585 
   1586 /**
   1587  * @ingroup crypto
   1588  * Extract the public key for the given private key.
   1589  *
   1590  * @param priv the private key
   1591  * @param pub where to write the public key
   1592  */
   1593 void
   1594 GNUNET_CRYPTO_ecdhe_key_get_public (
   1595   const struct GNUNET_CRYPTO_EcdhePrivateKey *priv,
   1596   struct GNUNET_CRYPTO_EcdhePublicKey *pub);
   1597 
   1598 
   1599 /**
   1600  * Convert a public key to a string.
   1601  *
   1602  * @param pub key to convert
   1603  * @return string representing @a pub
   1604  */
   1605 char *
   1606 GNUNET_CRYPTO_ecdsa_public_key_to_string (
   1607   const struct GNUNET_CRYPTO_EcdsaPublicKey *pub);
   1608 
   1609 /**
   1610  * Convert a private key to a string.
   1611  *
   1612  * @param priv key to convert
   1613  * @return string representing @a priv
   1614  */
   1615 char *
   1616 GNUNET_CRYPTO_ecdsa_private_key_to_string (
   1617   const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv);
   1618 
   1619 
   1620 /**
   1621  * Convert a private key to a string.
   1622  *
   1623  * @param priv key to convert
   1624  * @return string representing @a pub
   1625  */
   1626 char *
   1627 GNUNET_CRYPTO_eddsa_private_key_to_string (
   1628   const struct GNUNET_CRYPTO_EddsaPrivateKey *priv);
   1629 
   1630 
   1631 /**
   1632  * Convert a public key to a string.
   1633  *
   1634  * @param pub key to convert
   1635  * @return string representing @a pub
   1636  */
   1637 char *
   1638 GNUNET_CRYPTO_eddsa_public_key_to_string (
   1639   const struct GNUNET_CRYPTO_EddsaPublicKey *pub);
   1640 
   1641 
   1642 /**
   1643  * Convert a string representing a public key to a public key.
   1644  *
   1645  * @param enc encoded public key
   1646  * @param enclen number of bytes in @a enc (without 0-terminator)
   1647  * @param pub where to store the public key
   1648  * @return #GNUNET_OK on success
   1649  */
   1650 enum GNUNET_GenericReturnValue
   1651 GNUNET_CRYPTO_ecdsa_public_key_from_string (
   1652   const char *enc,
   1653   size_t enclen,
   1654   struct GNUNET_CRYPTO_EcdsaPublicKey *pub);
   1655 
   1656 
   1657 /**
   1658  * Convert a string representing a private key to a private key.
   1659  *
   1660  * @param enc encoded public key
   1661  * @param enclen number of bytes in @a enc (without 0-terminator)
   1662  * @param priv where to store the private key
   1663  * @return #GNUNET_OK on success
   1664  */
   1665 enum GNUNET_GenericReturnValue
   1666 GNUNET_CRYPTO_eddsa_private_key_from_string (
   1667   const char *enc,
   1668   size_t enclen,
   1669   struct GNUNET_CRYPTO_EddsaPrivateKey *priv);
   1670 
   1671 
   1672 /**
   1673  * Convert a string representing a public key to a public key.
   1674  *
   1675  * @param enc encoded public key
   1676  * @param enclen number of bytes in @a enc (without 0-terminator)
   1677  * @param pub where to store the public key
   1678  * @return #GNUNET_OK on success
   1679  */
   1680 enum GNUNET_GenericReturnValue
   1681 GNUNET_CRYPTO_eddsa_public_key_from_string (
   1682   const char *enc,
   1683   size_t enclen,
   1684   struct GNUNET_CRYPTO_EddsaPublicKey *pub);
   1685 
   1686 
   1687 /**
   1688  * @ingroup crypto
   1689  * @brief Create a new private key by reading it from a file.
   1690  *
   1691  * If the files does not exist and @a do_create is set, creates a new key and
   1692  * write it to the file.
   1693  *
   1694  * If the contents of the file are invalid, an error is returned.
   1695  *
   1696  * @param filename name of file to use to store the key
   1697  * @param do_create should a file be created?
   1698  * @param[out] pkey set to the private key from @a filename on success
   1699  * @return #GNUNET_OK on success, #GNUNET_NO if @a do_create was set but
   1700  *         we found an existing file, #GNUNET_SYSERR on failure
   1701  */
   1702 enum GNUNET_GenericReturnValue
   1703 GNUNET_CRYPTO_ecdsa_key_from_file (const char *filename,
   1704                                    int do_create,
   1705                                    struct GNUNET_CRYPTO_EcdsaPrivateKey *pkey);
   1706 
   1707 
   1708 /**
   1709  * @ingroup crypto
   1710  * @brief Create a new private key by reading it from a file.
   1711  *
   1712  * If the files does not exist and @a do_create is set, creates a new key and
   1713  * write it to the file.
   1714  *
   1715  * If the contents of the file are invalid, an error is returned.
   1716  *
   1717  * @param filename name of file to use to store the key
   1718  * @param do_create should a file be created?
   1719  * @param[out] pkey set to the private key from @a filename on success
   1720  * @return #GNUNET_OK on success, #GNUNET_NO if @a do_create was set but
   1721  *         we found an existing file, #GNUNET_SYSERR on failure
   1722  */
   1723 enum GNUNET_GenericReturnValue
   1724 GNUNET_CRYPTO_eddsa_key_from_file (const char *filename,
   1725                                    int do_create,
   1726                                    struct GNUNET_CRYPTO_EddsaPrivateKey *pkey);
   1727 
   1728 
   1729 /**
   1730  * Forward declaration to simplify #include-structure.
   1731  */
   1732 struct GNUNET_CONFIGURATION_Handle;
   1733 
   1734 
   1735 /**
   1736  * @ingroup crypto
   1737  * Create a new private key by reading our peer's key from
   1738  * the file specified in the configuration.
   1739  *
   1740  * @param cfg the configuration to use
   1741  * @return new private key, NULL on error (for example,
   1742  *   permission denied); free using #GNUNET_free
   1743  */
   1744 struct GNUNET_CRYPTO_EddsaPrivateKey *
   1745 GNUNET_CRYPTO_eddsa_key_create_from_configuration (
   1746   const struct GNUNET_CONFIGURATION_Handle *cfg);
   1747 
   1748 
   1749 /**
   1750  * @ingroup crypto
   1751  * Create a new private key.
   1752  *
   1753  * @param[out] pk private key to initialize
   1754  */
   1755 void
   1756 GNUNET_CRYPTO_ecdsa_key_create (struct GNUNET_CRYPTO_EcdsaPrivateKey *pk);
   1757 
   1758 
   1759 /**
   1760  * @ingroup crypto
   1761  * Create a new private key.
   1762  *
   1763  * @param[out] pk private key to initialize
   1764  */
   1765 void
   1766 GNUNET_CRYPTO_eddsa_key_create (struct GNUNET_CRYPTO_EddsaPrivateKey *pk);
   1767 
   1768 
   1769 /**
   1770  * @ingroup crypto
   1771  * Create a new private key.
   1772  *
   1773  * @param[out] pk private key to initialize
   1774  */
   1775 void
   1776 GNUNET_CRYPTO_edx25519_key_create (struct GNUNET_CRYPTO_Edx25519PrivateKey *pk);
   1777 
   1778 /**
   1779  * @ingroup crypto
   1780  * Create a new private key for Edx25519 from a given seed.  After expanding
   1781  * the seed, the first half of the key will be clamped according to EdDSA.
   1782  *
   1783  * @param seed seed input
   1784  * @param seedsize size of the seed in bytes
   1785  * @param[out] pk private key to initialize
   1786  */
   1787 void
   1788 GNUNET_CRYPTO_edx25519_key_create_from_seed (
   1789   const void *seed,
   1790   size_t seedsize,
   1791   struct GNUNET_CRYPTO_Edx25519PrivateKey *pk);
   1792 
   1793 /**
   1794  * @ingroup crypto
   1795  * Create a new private key.  Clear with #GNUNET_CRYPTO_ecdhe_key_clear().
   1796  * This is X25519 DH (RFC 7748 Section 5) and corresponds to
   1797  * X25519(a,9).
   1798  * See #GNUNET_CRYPTO_ecc_ecdh for the DH function.
   1799  *
   1800  * @param[out] pk set to fresh private key;
   1801  */
   1802 void
   1803 GNUNET_CRYPTO_ecdhe_key_create (struct GNUNET_CRYPTO_EcdhePrivateKey *pk);
   1804 
   1805 
   1806 /**
   1807  * @ingroup crypto
   1808  * Clear memory that was used to store a private key.
   1809  *
   1810  * @param pk location of the key
   1811  */
   1812 void
   1813 GNUNET_CRYPTO_eddsa_key_clear (struct GNUNET_CRYPTO_EddsaPrivateKey *pk);
   1814 
   1815 
   1816 /**
   1817  * @ingroup crypto
   1818  * Clear memory that was used to store a private key.
   1819  *
   1820  * @param pk location of the key
   1821  */
   1822 void
   1823 GNUNET_CRYPTO_ecdsa_key_clear (struct GNUNET_CRYPTO_EcdsaPrivateKey *pk);
   1824 
   1825 /**
   1826  * @ingroup crypto
   1827  * Clear memory that was used to store a private key.
   1828  *
   1829  * @param pk location of the key
   1830  */
   1831 void
   1832 GNUNET_CRYPTO_edx25519_key_clear (struct GNUNET_CRYPTO_Edx25519PrivateKey *pk);
   1833 
   1834 /**
   1835  * @ingroup crypto
   1836  * Clear memory that was used to store a private key.
   1837  *
   1838  * @param pk location of the key
   1839  */
   1840 void
   1841 GNUNET_CRYPTO_ecdhe_key_clear (struct GNUNET_CRYPTO_EcdhePrivateKey *pk);
   1842 
   1843 /**
   1844  * @ingroup crypto
   1845  * Clear memory that was used to store a private key.
   1846  *
   1847  * @param pk location of the key
   1848  */
   1849 void
   1850 GNUNET_CRYPTO_private_key_clear (struct GNUNET_CRYPTO_BlindablePrivateKey *pk);
   1851 
   1852 
   1853 /**
   1854  * @ingroup crypto
   1855  * Get the shared private key we use for anonymous users.
   1856  *
   1857  * @return "anonymous" private key; do not free
   1858  */
   1859 const struct GNUNET_CRYPTO_EcdsaPrivateKey *
   1860 GNUNET_CRYPTO_ecdsa_key_get_anonymous (void);
   1861 
   1862 
   1863 /**
   1864  * @ingroup crypto
   1865  * Setup a hostkey file for a peer given the name of the
   1866  * configuration file (!).  This function is used so that
   1867  * at a later point code can be certain that reading a
   1868  * hostkey is fast (for example in time-dependent testcases).
   1869  *
   1870  * @param cfg_name name of the configuration file to use
   1871  */
   1872 void
   1873 GNUNET_CRYPTO_eddsa_setup_hostkey (const char *cfg_name);
   1874 
   1875 
   1876 /**
   1877  * @ingroup crypto
   1878  * Retrieve the identity of the host's peer.
   1879  *
   1880  * @param cfg configuration to use
   1881  * @param dst pointer to where to write the peer identity
   1882  * @return #GNUNET_OK on success, #GNUNET_SYSERR if the identity
   1883  *         could not be retrieved
   1884  */
   1885 enum GNUNET_GenericReturnValue
   1886 GNUNET_CRYPTO_get_peer_identity (const struct GNUNET_CONFIGURATION_Handle *cfg,
   1887                                  struct GNUNET_PeerIdentity *dst);
   1888 
   1889 
   1890 /**
   1891  * @ingroup crypto
   1892  * Sign a given block with a specific purpose using the host's peer identity.
   1893  *
   1894  * @param cfg configuration to use
   1895  * @param purpose what to sign (size, purpose)
   1896  * @param sig where to write the signature
   1897  * @return #GNUNET_OK on success, #GNUNET_SYSERR if the identity
   1898  *         could not be retrieved
   1899  */
   1900 enum GNUNET_GenericReturnValue
   1901 GNUNET_CRYPTO_blinded_key_sign_by_peer_identity (const struct
   1902                                                  GNUNET_CONFIGURATION_Handle *
   1903                                                  cfg,
   1904                                                  const struct
   1905                                                  GNUNET_CRYPTO_SignaturePurpose
   1906                                                  *purpose,
   1907                                                  struct
   1908                                                  GNUNET_CRYPTO_EddsaSignature *
   1909                                                  sig);
   1910 
   1911 
   1912 /**
   1913  * @ingroup crypto
   1914  * Verify a given signature with a peer's identity.
   1915  *
   1916  * @param purpose what is the purpose that the signature should have?
   1917  * @param validate block to validate (size, purpose, data)
   1918  * @param sig signature that is being validated
   1919  * @param identity the peer's identity to verify
   1920  * @return #GNUNET_OK if ok, #GNUNET_SYSERR if invalid
   1921  */
   1922 enum GNUNET_GenericReturnValue
   1923 GNUNET_CRYPTO_verify_peer_identity (uint32_t purpose,
   1924                                     const struct
   1925                                     GNUNET_CRYPTO_SignaturePurpose *validate,
   1926                                     const struct
   1927                                     GNUNET_CRYPTO_EddsaSignature *sig,
   1928                                     const struct GNUNET_PeerIdentity *identity);
   1929 
   1930 
   1931 /**
   1932  * Internal structure used to cache pre-calculated values for DLOG calculation.
   1933  */
   1934 struct GNUNET_CRYPTO_EccDlogContext;
   1935 
   1936 
   1937 /**
   1938  * Point on a curve (always for Curve25519) encoded in a format suitable
   1939  * for network transmission (ECDH), see http://cr.yp.to/ecdh.html.
   1940  */
   1941 struct GNUNET_CRYPTO_EccPoint
   1942 {
   1943   /**
   1944    * Q consists of an x- and a y-value, each mod p (256 bits), given
   1945    * here in affine coordinates and Ed25519 standard compact format.
   1946    */
   1947   unsigned char v[256 / 8];
   1948 };
   1949 
   1950 /**
   1951  * A ECC scalar for use in point multiplications
   1952  */
   1953 struct GNUNET_CRYPTO_EccScalar
   1954 {
   1955   unsigned char v[256 / 8];
   1956 };
   1957 
   1958 /**
   1959  * Do pre-calculation for ECC discrete logarithm for small factors.
   1960  *
   1961  * @param max maximum value the factor can be
   1962  * @param mem memory to use (should be smaller than @a max), must not be zero.
   1963  * @return NULL on error
   1964  */
   1965 struct GNUNET_CRYPTO_EccDlogContext *
   1966 GNUNET_CRYPTO_ecc_dlog_prepare (unsigned int max,
   1967                                 unsigned int mem);
   1968 
   1969 
   1970 /**
   1971  * Calculate ECC discrete logarithm for small factors.
   1972  * Opposite of #GNUNET_CRYPTO_ecc_dexp().
   1973  *
   1974  * @param edc precalculated values, determine range of factors
   1975  * @param input point on the curve to factor
   1976  * @return INT_MAX if dlog failed, otherwise the factor
   1977  */
   1978 int
   1979 GNUNET_CRYPTO_ecc_dlog (struct GNUNET_CRYPTO_EccDlogContext *edc,
   1980                         const struct GNUNET_CRYPTO_EccPoint *input);
   1981 
   1982 
   1983 /**
   1984  * Multiply the generator g of the elliptic curve by @a val
   1985  * to obtain the point on the curve representing @a val.
   1986  * Afterwards, point addition will correspond to integer
   1987  * addition.  #GNUNET_CRYPTO_ecc_dlog() can be used to
   1988  * convert a point back to an integer (as long as the
   1989  * integer is smaller than the MAX of the @a edc context).
   1990  *
   1991  * @param val value to encode into a point
   1992  * @param r where to write the point (must be allocated)
   1993  */
   1994 void
   1995 GNUNET_CRYPTO_ecc_dexp (int val,
   1996                         struct GNUNET_CRYPTO_EccPoint*r);
   1997 
   1998 
   1999 /**
   2000  * Multiply the generator g of the elliptic curve by @a val
   2001  * to obtain the point on the curve representing @a val.
   2002  *
   2003  * @param val (positive) value to encode into a point
   2004  * @param r where to write the point (must be allocated)
   2005  * @return #GNUNET_OK on success.
   2006  */
   2007 enum GNUNET_GenericReturnValue
   2008 GNUNET_CRYPTO_ecc_dexp_mpi (const struct GNUNET_CRYPTO_EccScalar *val,
   2009                             struct GNUNET_CRYPTO_EccPoint *r);
   2010 
   2011 
   2012 /**
   2013  * Multiply the point @a p on the elliptic curve by @a val.
   2014  *
   2015  * @param p point to multiply
   2016  * @param val (positive) value to encode into a point
   2017  * @param r where to write the point (must be allocated)
   2018  * @return #GNUNET_OK on success.
   2019  */
   2020 enum GNUNET_GenericReturnValue
   2021 GNUNET_CRYPTO_ecc_pmul_mpi (const struct GNUNET_CRYPTO_EccPoint *p,
   2022                             const struct GNUNET_CRYPTO_EccScalar *val,
   2023                             struct GNUNET_CRYPTO_EccPoint *r);
   2024 
   2025 
   2026 /**
   2027  * Add two points on the elliptic curve.
   2028  *
   2029  * @param a some value
   2030  * @param b some value
   2031  * @param r where to write the point (must be allocated)
   2032  * @return #GNUNET_OK on success.
   2033  */
   2034 enum GNUNET_GenericReturnValue
   2035 GNUNET_CRYPTO_ecc_add (const struct GNUNET_CRYPTO_EccPoint *a,
   2036                        const struct GNUNET_CRYPTO_EccPoint *b,
   2037                        struct GNUNET_CRYPTO_EccPoint *r);
   2038 
   2039 
   2040 /**
   2041  * Obtain a random point on the curve and its
   2042  * additive inverse.
   2043  *
   2044  * @param[out] r set to a random point on the curve
   2045  * @param[out] r_inv set to the additive inverse of @a r
   2046  * @return #GNUNET_OK on success.
   2047  */
   2048 enum GNUNET_GenericReturnValue
   2049 GNUNET_CRYPTO_ecc_rnd (struct GNUNET_CRYPTO_EccPoint *r,
   2050                        struct GNUNET_CRYPTO_EccPoint *r_inv);
   2051 
   2052 
   2053 /**
   2054  * Obtain a random scalar for point multiplication on the curve and
   2055  * its additive inverse.
   2056  *
   2057  * @param[out] r set to a random scalar on the curve
   2058  * @param[out] r_neg set to the negation of @a
   2059  */
   2060 void
   2061 GNUNET_CRYPTO_ecc_rnd_mpi (struct GNUNET_CRYPTO_EccScalar *r,
   2062                            struct GNUNET_CRYPTO_EccScalar *r_neg);
   2063 
   2064 
   2065 /**
   2066  * Generate a random value mod n.
   2067  *
   2068  * @param[out] r random value mod n.
   2069  */
   2070 void
   2071 GNUNET_CRYPTO_ecc_random_mod_n (struct GNUNET_CRYPTO_EccScalar*r);
   2072 
   2073 
   2074 /**
   2075  * Release precalculated values.
   2076  *
   2077  * @param dlc dlog context
   2078  */
   2079 void
   2080 GNUNET_CRYPTO_ecc_dlog_release (struct GNUNET_CRYPTO_EccDlogContext *dlc);
   2081 
   2082 
   2083 /**
   2084  * Create a scalar from int value.
   2085  *
   2086  * @param val the int value
   2087  * @param[out] r where to write the salar
   2088  */
   2089 void
   2090 GNUNET_CRYPTO_ecc_scalar_from_int (int64_t val,
   2091                                    struct GNUNET_CRYPTO_EccScalar *r);
   2092 
   2093 
   2094 /**
   2095  * @ingroup crypto
   2096  * Derive key material from a public and a private ECC key.
   2097  * This is X25519 DH (RFC 7748 Section 5) and corresponds to
   2098  * H(X25519(b,X25519(a,9))) where b := priv, pub := X25519(a,9),
   2099  * and a := #GNUNET_CRYPTO_ecdhe_key_create().
   2100  *
   2101  * @param priv private key to use for the ECDH (x)
   2102  * @param pub public key to use for the ECDH (yG)
   2103  * @param key_material where to write the key material (xyG)
   2104  * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
   2105  */
   2106 enum GNUNET_GenericReturnValue
   2107 GNUNET_CRYPTO_ecc_ecdh (const struct GNUNET_CRYPTO_EcdhePrivateKey *priv,
   2108                         const struct GNUNET_CRYPTO_EcdhePublicKey *pub,
   2109                         struct GNUNET_HashCode *key_material);
   2110 
   2111 
   2112 /**
   2113  * @ingroup crypto
   2114  * Derive key material from a ECDH public key and a private EdDSA key.
   2115  * Dual to #GNUNET_CRRYPTO_ecdh_eddsa.
   2116  * This uses the Ed25519 private seed as X25519 seed.
   2117  * As such, this also is a X25519 DH (see #GNUNET_CRYPTO_ecc_ecdh).
   2118  * NOTE: Whenever you can get away with it, use separate key pairs
   2119  * for signing and encryption (DH)!
   2120  *
   2121  * @param priv private key from EdDSA to use for the ECDH (x)
   2122  * @param pub public key to use for the ECDH (yG)
   2123  * @param key_material where to write the key material H(h(x)yG)
   2124  * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
   2125  */
   2126 enum GNUNET_GenericReturnValue
   2127 GNUNET_CRYPTO_eddsa_ecdh (const struct GNUNET_CRYPTO_EddsaPrivateKey *priv,
   2128                           const struct GNUNET_CRYPTO_EcdhePublicKey *pub,
   2129                           struct GNUNET_HashCode *key_material);
   2130 
   2131 
   2132 /**
   2133  * @ingroup crypto
   2134  * Derive key material from a ECDH public key and a private X25519 key.
   2135  * Dual to #GNUNET_CRRYPTO_ecdh_x25519.
   2136  * NOTE: Whenever you can get away with it, use separate key pairs
   2137  * for signing and encryption (DH)!
   2138  *
   2139  * @param sk private key from X25519 to use for the ECDH (x)
   2140  * @param pk public key to use for the ECDH (yG)
   2141  * @param additional_data this is fed into HKDF-Extract along with
   2142                           the ECDH shared secret
   2143  * @param ad_len Length of the additional data
   2144  * @param dh the DH shared secret (NOTE: Derive key from this before use!)
   2145  * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
   2146  */
   2147 enum GNUNET_GenericReturnValue
   2148 GNUNET_CRYPTO_x25519_ecdh (const struct GNUNET_CRYPTO_EcdhePrivateKey *sk,
   2149                            const struct GNUNET_CRYPTO_EcdhePublicKey *pk,
   2150                            struct GNUNET_CRYPTO_EcdhePublicKey *dh);
   2151 
   2152 
   2153 /** HPKE RFC 9180 **/
   2154 
   2155 /**
   2156  * The HPKE Mode
   2157  * "PSK" stands for "Pre-Shared Key".
   2158  * The "AUTH" variants use an authenticating KEM
   2159  * construction.
   2160  */
   2161 enum GNUNET_CRYPTO_HpkeMode
   2162 {
   2163   GNUNET_CRYPTO_HPKE_MODE_BASE = 0x00,
   2164   GNUNET_CRYPTO_HPKE_MODE_PSK = 0x01,
   2165   GNUNET_CRYPTO_HPKE_MODE_AUTH = 0x02,
   2166   GNUNET_CRYPTO_HPKE_MODE_AUTH_PSK = 0x03
   2167 };
   2168 
   2169 // Nt
   2170 #define GNUNET_CRYPTO_HPKE_AEAD_ID 0x0003
   2171 
   2172 // Nn
   2173 #define GNUNET_CRYPTO_HPKE_NONCE_LEN 12
   2174 
   2175 // Nk
   2176 #define GNUNET_CRYPTO_HPKE_KEY_LEN 32
   2177 
   2178 // Nt
   2179 #define GNUNET_CRYPTO_HPKE_TAG_LEN 16
   2180 
   2181 // Overhead required for ciphertext
   2182 #define GNUNET_CRYPTO_HPKE_SEAL_OVERHEAD_BYTES GNUNET_CRYPTO_HPKE_TAG_LEN
   2183 
   2184 // Overhead required for ciphertext
   2185 #define GNUNET_CRYPTO_HPKE_SEAL_ONESHOT_OVERHEAD_BYTES \
   2186         GNUNET_CRYPTO_HPKE_SEAL_OVERHEAD_BYTES   \
   2187         + sizeof (struct GNUNET_CRYPTO_HpkeEncapsulation)
   2188 
   2189 /**
   2190  * Role of the HPKE participant.
   2191  */
   2192 enum GNUNET_CRYPTO_HpkeRole
   2193 {
   2194   // Receiver
   2195   GNUNET_CRYPTO_HPKE_ROLE_R = 0,
   2196   // Sender
   2197   GNUNET_CRYPTO_HPKE_ROLE_S = 1
   2198 };
   2199 
   2200 
   2201 /**
   2202  * HPKE crypto context.
   2203  */
   2204 struct GNUNET_CRYPTO_HpkeContext
   2205 {
   2206   // Participant role
   2207   enum GNUNET_CRYPTO_HpkeRole role;
   2208 
   2209   // Encapsulated/Decapsulated key
   2210   uint8_t key[GNUNET_CRYPTO_HPKE_KEY_LEN];
   2211 
   2212   // Base nonce
   2213   uint8_t base_nonce[GNUNET_CRYPTO_HPKE_NONCE_LEN];
   2214 
   2215   // Sequence number
   2216   uint64_t seq;
   2217 
   2218   // Exporter secret
   2219   struct GNUNET_ShortHashCode exporter_secret;
   2220 };
   2221 
   2222 /**
   2223  * HPKE KEM identifier
   2224  * TODO: Elligator KEM was requested at IANA; Number is currently a placeholder.
   2225  */
   2226 enum GNUNET_CRYPTO_HpkeKem
   2227 {
   2228   // Non-elligator X25519 KEM using HKDF256
   2229   GNUNET_CRYPTO_HPKE_KEM_DH_X25519_HKDF256 = 0x0020,
   2230   // Elligator X25519 KEM using HKDF256
   2231   GNUNET_CRYPTO_HPKE_KEM_DH_X25519ELLIGATOR_HKDF256 = 0x0030,
   2232 };
   2233 
   2234 
   2235 /**
   2236  * HPKE DHKEM encapsulation (X25519)
   2237  * See RFC 9180
   2238  */
   2239 struct GNUNET_CRYPTO_HpkeEncapsulation
   2240 {
   2241   /**
   2242    * Q consists of an x- and a y-value, each mod p (256 bits), given
   2243    * here in affine coordinates and Ed25519 standard compact format.
   2244    */
   2245   unsigned char q_y[256 / 8];
   2246 };
   2247 
   2248 
   2249 /**
   2250  * Convert a GNUnet identity key to a key sutiable for HPKE (X25519)
   2251  *
   2252  * @param sk the private key
   2253  * @param sk_enc the new key
   2254  * @return GNUNET_OK on success
   2255  */
   2256 enum GNUNET_GenericReturnValue
   2257 GNUNET_CRYPTO_hpke_sk_to_x25519 (const struct
   2258                                  GNUNET_CRYPTO_BlindablePrivateKey *sk,
   2259                                  struct GNUNET_CRYPTO_HpkePrivateKey *sk_enc);
   2260 
   2261 
   2262 /**
   2263  * Convert a GNUnet identity key to a key sutiable for HPKE (X25519)
   2264  *
   2265  * @param pk the public key
   2266  * @param x25519 the new key
   2267  * @return GNUNET_OK on success
   2268  */
   2269 enum GNUNET_GenericReturnValue
   2270 GNUNET_CRYPTO_hpke_pk_to_x25519 (const struct GNUNET_CRYPTO_BlindablePublicKey *
   2271                                  pk,
   2272                                  struct GNUNET_CRYPTO_HpkePublicKey *pk_enc);
   2273 
   2274 /**
   2275  * @ingroup crypto
   2276  * Decapsulate a key for a private X25519 key.
   2277  * Dual to #GNUNET_CRYPTO_hpke_kem_encaps.
   2278  * Use #GNUNET_CRYPTO_hkdf_expand to derive further context-specific
   2279  * keys from the key material.
   2280  *
   2281  * @param priv private key from X25519 to use for the ECDH (x)
   2282  * @param c the encapsulated key
   2283  * @param prk where to write the key material
   2284  * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
   2285  */
   2286 enum GNUNET_GenericReturnValue
   2287 GNUNET_CRYPTO_hpke_kem_decaps (const struct
   2288                                GNUNET_CRYPTO_HpkePrivateKey *priv,
   2289                                const struct GNUNET_CRYPTO_HpkeEncapsulation *c,
   2290                                struct GNUNET_ShortHashCode *prk);
   2291 
   2292 /**
   2293  * @ingroup crypto
   2294  * Encapsulate key material for a X25519 public key.
   2295  * Dual to #GNUNET_CRYPTO_hpke_kem_decaps.
   2296  * Use #GNUNET_CRYPTO_hkdf_expand to derive further context-specific
   2297  * keys from the key material.
   2298  *
   2299  * @param pkR public key of receiver
   2300  * @param c public key from X25519 to use for the ECDH (X=h(x)G)
   2301  * @param prk where to write the key material
   2302  * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
   2303  */
   2304 enum GNUNET_GenericReturnValue
   2305 GNUNET_CRYPTO_hpke_kem_encaps (const struct GNUNET_CRYPTO_HpkePublicKey *pkR,
   2306                                struct GNUNET_CRYPTO_HpkeEncapsulation *c,
   2307                                struct GNUNET_ShortHashCode *prk);
   2308 
   2309 /**
   2310  * @ingroup crypto
   2311  * Deterministic variant of #GNUNET_CRYPTO_hpke_kem_encaps.
   2312  * Use #GNUNET_CRYPTO_hkdf_expand to derive further context-specific
   2313  * keys from the key material.
   2314  *
   2315  * @param pkR public key of receiver
   2316  * @param c public key from X25519 to use for the ECDH (X=h(x)G)
   2317  * @param skE ephemeral private key from X25519 to use
   2318  * @param prk where to write the key material
   2319  * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
   2320  */
   2321 enum GNUNET_GenericReturnValue
   2322 GNUNET_CRYPTO_hpke_kem_encaps_norand (const struct
   2323                                       GNUNET_CRYPTO_HpkePublicKey *pkR,
   2324                                       struct GNUNET_CRYPTO_HpkeEncapsulation *c,
   2325                                       const struct
   2326                                       GNUNET_CRYPTO_HpkePrivateKey *skE,
   2327                                       struct GNUNET_ShortHashCode *prk);
   2328 
   2329 /**
   2330  * @ingroup crypto
   2331  * Carries out ecdh encapsulation with given public key and the private key from a freshly created ephemeral key pair.
   2332  * Following the terminology in https://eprint.iacr.org/2021/509.pdf.
   2333  * Use #GNUNET_CRYPTO_hkdf_expand to derive further context-specific
   2334  * keys from the key material.
   2335  *
   2336  * @param random_tweak random 8-bit value used as seed
   2337  * @param pkR public key of receiver
   2338  * @param c representative of ephemeral public key A to use for the ECDH (direct_map(r)=A=aG)
   2339  * @param skE special elligator ephemeral private key from X25519 to use
   2340  * @param shared_secret where to write the key material HKDF-Extract(r||aX)=HKDF-Extract(r||x(aG))
   2341  * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
   2342  */
   2343 enum GNUNET_GenericReturnValue
   2344 GNUNET_CRYPTO_hpke_elligator_kem_encaps_norand (
   2345   uint8_t random_tweak,
   2346   const struct GNUNET_CRYPTO_HpkePublicKey *pkR,
   2347   struct GNUNET_CRYPTO_HpkeEncapsulation *c,
   2348   const struct GNUNET_CRYPTO_ElligatorEcdhePrivateKey *skE,
   2349   struct GNUNET_ShortHashCode *shared_secret);
   2350 
   2351 /**
   2352  * @ingroup crypto
   2353  * Carries out ecdh encapsulation with given public key and the private key from a freshly created ephemeral key pair.
   2354  * Following the terminology in https://eprint.iacr.org/2021/509.pdf.
   2355  * Use #GNUNET_CRYPTO_hkdf_expand to derive further context-specific
   2356  * keys from the key material.
   2357  *
   2358  * @param pkR Receiver public key (X)
   2359  * @param c representative of ephemeral public key A to use for the ECDH (direct_map(r)=A=aG)
   2360  * @param shared_secret where to write the key material HKDF-Extract(r||aX)=HKDF-Extract(r||x(aG))
   2361  * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
   2362  */
   2363 enum GNUNET_GenericReturnValue
   2364 GNUNET_CRYPTO_hpke_elligator_kem_encaps (
   2365   const struct GNUNET_CRYPTO_HpkePublicKey *pkR,
   2366   struct GNUNET_CRYPTO_HpkeEncapsulation *c,
   2367   struct GNUNET_ShortHashCode *shared_secret);
   2368 
   2369 /**
   2370  * @ingroup crypto
   2371  * Carries out ecdh decapsulation with own private key and the representative of the received public key.
   2372  * Following the terminology in https://eprint.iacr.org/2021/509.pdf.
   2373  * Use #GNUNET_CRYPTO_hkdf_expand to derive further context-specific
   2374  * keys from the key material.
   2375  *
   2376  * @param skR sender private key (x)
   2377  * @param r received representative r, from which we can obtain the public key A (direct_map(r)=A=aG)
   2378  * @param shared_secret where to write the key material HKDF-Extract(r||aX)=HKDF-Extract(r||x(aG))
   2379  * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
   2380  */
   2381 enum GNUNET_GenericReturnValue
   2382 GNUNET_CRYPTO_hpke_elligator_kem_decaps (
   2383   const struct GNUNET_CRYPTO_HpkePrivateKey *skR,
   2384   const struct GNUNET_CRYPTO_HpkeEncapsulation *c,
   2385   struct GNUNET_ShortHashCode *shared_secret);
   2386 
   2387 /**
   2388  * @ingroup crypto
   2389  * Decapsulate a key for a private EdDSA key.
   2390  * Dual to #GNUNET_CRRYPTO_eddsa_kem_encaps.
   2391  * Use #GNUNET_CRYPTO_hkdf_expand to derive further context-specific
   2392  * keys from the key material.
   2393  *
   2394  * @param priv private key from EdDSA to use for the ECDH (x)
   2395  * @param c the encapsulated key
   2396  * @param prk where to write the key material HKDF-Extract(c||aX)=HKDF-Extract(c||x(aG))
   2397  * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
   2398  */
   2399 enum GNUNET_GenericReturnValue
   2400 GNUNET_CRYPTO_eddsa_kem_decaps (const struct
   2401                                 GNUNET_CRYPTO_EddsaPrivateKey *priv,
   2402                                 const struct GNUNET_CRYPTO_HpkeEncapsulation *c,
   2403                                 struct GNUNET_ShortHashCode *prk);
   2404 
   2405 /**
   2406  * @ingroup crypto
   2407  * Encapsulate key material for a EdDSA public key.
   2408  * Dual to #GNUNET_CRRYPTO_eddsa_kem_decaps.
   2409  * Use #GNUNET_CRYPTO_hkdf_expand to derive further context-specific
   2410  * keys from the key material.
   2411  *
   2412  * @param pub public key from EdDSA to use for the ECDH (X=h(x)G)
   2413  * @param c encapsulation of prk
   2414  * @param prk where to write the key material HKDF-Extract(c||aX)=HKDF-Extract(c||x(aG))
   2415  * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
   2416  */
   2417 enum GNUNET_GenericReturnValue
   2418 GNUNET_CRYPTO_eddsa_kem_encaps (const struct GNUNET_CRYPTO_EddsaPublicKey *pub,
   2419                                 struct GNUNET_CRYPTO_HpkeEncapsulation *c,
   2420                                 struct GNUNET_ShortHashCode *prk);
   2421 
   2422 
   2423 /**
   2424  * RFC9180 HPKE encryption.
   2425  * This sets the encryption context up for a sender of
   2426  * encrypted messages.
   2427  * Algorithm: DHKEM(X25519, HKDF-SHA256), HKDF-SHA256, ChaCha20Poly1305
   2428  *
   2429  * The encapsulation "enc" must be exchanged with the receiver.
   2430  * From then on, encrypted messages can be created and sent using "ctx"
   2431  *
   2432  * @param pkR the X25519 receiver public key
   2433  * @param info the info context separator
   2434  * @param info_len length of info in bytes
   2435  * @param enc the encapsulation to exchange with the other party
   2436  * @param ctx the encryption context allocated by caller
   2437  * @return GNUNET_OK on success
   2438  */
   2439 enum GNUNET_GenericReturnValue
   2440 GNUNET_CRYPTO_hpke_sender_setup (const struct GNUNET_CRYPTO_HpkePublicKey *pkR,
   2441                                  const uint8_t *info, size_t info_len,
   2442                                  struct GNUNET_CRYPTO_HpkeEncapsulation *enc,
   2443                                  struct GNUNET_CRYPTO_HpkeContext *ctx);
   2444 
   2445 /**
   2446  * RFC9180 HPKE encryption.
   2447  * This sets the encryption context up for a sender of
   2448  * encrypted messages.
   2449  * Algorithm: DHKEM(X25519, HKDF-SHA256), HKDF-SHA256, ChaCha20Poly1305
   2450  *
   2451  * The encapsulation "enc" must be exchanged with the receiver.
   2452  * From then on, encrypted messages can be created and sent using "ctx"
   2453  *
   2454  * @param kem the HPKE KEM to use
   2455  * @param mode the HPKE mode
   2456  * @param skE the X25519 ephemeral key to use as encapsulation
   2457  * @param skR the X25519 sender private key (may be null for non-Auth modes)
   2458  * @param info the info context separator
   2459  * @param info_len length of info in bytes
   2460  * @param psk the pre-shared key (must not be set non-PSK modes)
   2461  * @param psk_len length of psk in bytes
   2462  * @param psk_id the ID of the pre-shared key (must be set of psk is set)
   2463  * @param psk_id_len length of psk_id in bytes
   2464  * @param enc the encapsulation to exchange with the other party
   2465  * @param ctx the encryption context allocated by caller
   2466  * @return GNUNET_OK on success
   2467  */
   2468 enum GNUNET_GenericReturnValue
   2469 GNUNET_CRYPTO_hpke_sender_setup2 (
   2470   enum GNUNET_CRYPTO_HpkeKem kem,
   2471   enum GNUNET_CRYPTO_HpkeMode mode,
   2472   struct GNUNET_CRYPTO_HpkePrivateKey *skE,
   2473   struct GNUNET_CRYPTO_HpkePrivateKey *skS,
   2474   const struct GNUNET_CRYPTO_HpkePublicKey *pkR,
   2475   const uint8_t *info, size_t info_len,
   2476   const uint8_t *psk, size_t psk_len,
   2477   const uint8_t *psk_id, size_t psk_id_len,
   2478   struct GNUNET_CRYPTO_HpkeEncapsulation *enc,
   2479   struct GNUNET_CRYPTO_HpkeContext *ctx);
   2480 
   2481 /**
   2482  * RFC9180 HPKE encryption.
   2483  * This sets the encryption context up for a receiver of
   2484  * encrypted messages.
   2485  * Algorithm: DHKEM(X25519, HKDF-SHA256), HKDF-SHA256, ChaCha20Poly1305
   2486  *
   2487  * The encapsulation "enc" must be exchanged with the receiver.
   2488  * From then on, encrypted messages can be decrypted using "ctx"
   2489  *
   2490  * @param kem the HPKE KEM to use
   2491  * @param mode the HPKE mode
   2492  * @param enc the encapsulation from the sender
   2493  * @param skR the X25519 receiver secret key
   2494  * @param pkS the X25519 sender public key (may be NULL for non-Auth modes)
   2495  * @param info the info context separator
   2496  * @param info_len length of info in bytes
   2497  * @param psk the pre-shared key (must not be set non-PSK modes)
   2498  * @param psk_len length of psk in bytes
   2499  * @param psk_id the ID of the pre-shared key (must be set of psk is set)
   2500  * @param psk_id_len length of psk_id in bytes
   2501  * @param ctx the encryption context allocated by caller
   2502  * @return GNUNET_OK on success
   2503  */
   2504 enum GNUNET_GenericReturnValue
   2505 GNUNET_CRYPTO_hpke_receiver_setup2 (
   2506   enum GNUNET_CRYPTO_HpkeKem kem,
   2507   enum GNUNET_CRYPTO_HpkeMode mode,
   2508   const struct GNUNET_CRYPTO_HpkeEncapsulation *enc,
   2509   const struct GNUNET_CRYPTO_HpkePrivateKey *skR,
   2510   const struct GNUNET_CRYPTO_HpkePublicKey *pkS,
   2511   const uint8_t *info, size_t info_len,
   2512   const uint8_t *psk, size_t psk_len,
   2513   const uint8_t *psk_id, size_t psk_id_len,
   2514   struct GNUNET_CRYPTO_HpkeContext *ctx);
   2515 
   2516 
   2517 /**
   2518  * RFC9180 HPKE encryption.
   2519  * This sets the encryption context up for a receiver of
   2520  * encrypted messages.
   2521  * Algorithm: DHKEM(X25519, HKDF-SHA256), HKDF-SHA256, ChaCha20Poly1305
   2522  *
   2523  * The encapsulation "enc" must be exchanged with the receiver.
   2524  * From then on, encrypted messages can be decrypted using "ctx"
   2525  *
   2526  * @param enc the encapsulation from the sender
   2527  * @param skR the X25519 receiver secret key
   2528  * @param info the info context separator
   2529  * @param info_len length of info in bytes
   2530  * @param ctx the encryption context allocated by caller
   2531  * @return GNUNET_OK on success
   2532  */
   2533 enum GNUNET_GenericReturnValue
   2534 GNUNET_CRYPTO_hpke_receiver_setup (
   2535   const struct GNUNET_CRYPTO_HpkeEncapsulation *enc,
   2536   const struct GNUNET_CRYPTO_HpkePrivateKey *skR,
   2537   const uint8_t *info,
   2538   size_t info_len,
   2539   struct GNUNET_CRYPTO_HpkeContext *ctx);
   2540 
   2541 /**
   2542  * RFC9180 HPKE encryption.
   2543  * Encrypt messages in a context.
   2544  * Algorithm: DHKEM(X25519, HKDF-SHA256), HKDF-SHA256, ChaCha20Poly1305
   2545  *
   2546  * The encapsulation "enc" must be exchanged with the receiver.
   2547  * From then on, encrypted messages can be decrypted using "ctx"
   2548  *
   2549  * @param ctx the encryption context
   2550  * @param aad addition authenticated data to send (not encrypted)
   2551  * @param aad_len length of aad in bytes
   2552  * @param pt plaintext data to encrypt
   2553  * @param pt_len length of pt in bytes
   2554  * @param ct ciphertext to send (to be allocated by caller)
   2555  * @param ct_len[out] length of written bytes in ct. may be NULL
   2556  * @return GNUNET_OK on success
   2557  */
   2558 enum GNUNET_GenericReturnValue
   2559 GNUNET_CRYPTO_hpke_seal (struct GNUNET_CRYPTO_HpkeContext *ctx,
   2560                          const uint8_t *aad,
   2561                          size_t aad_len,
   2562                          const uint8_t *pt,
   2563                          size_t pt_len,
   2564                          uint8_t *ct,
   2565                          unsigned long long *ct_len);
   2566 
   2567 
   2568 /**
   2569  * RFC9180 HPKE encryption.
   2570  * Encrypt messages in a context.
   2571  * Algorithm: DHKEM(X25519, HKDF-SHA256), HKDF-SHA256, ChaCha20Poly1305
   2572  *
   2573  * The encapsulation "enc" must be exchanged with the receiver.
   2574  * From then on, encrypted messages can be decrypted using "ctx"
   2575  *
   2576  * @param pkR the X25519 receiver secret key
   2577  * @param info the info context separator
   2578  * @param info_len length of info in bytes
   2579  * @param aad addition authenticated data to send (not encrypted)
   2580  * @param aad_len length of aad in bytes
   2581  * @param pt plaintext data to encrypt
   2582  * @param pt_len length of pt in bytes
   2583  * @param ct ciphertext to send (to be allocated by caller)
   2584  * @param ct_len[out] length of written bytes in ct. may be NULL
   2585  * @return GNUNET_OK on success
   2586  */
   2587 enum GNUNET_GenericReturnValue
   2588 GNUNET_CRYPTO_hpke_seal_oneshot (const struct GNUNET_CRYPTO_HpkePublicKey *pkR,
   2589                                  const uint8_t *info, size_t info_len,
   2590                                  const uint8_t*aad, size_t aad_len,
   2591                                  const uint8_t *pt, size_t pt_len,
   2592                                  uint8_t *ct, unsigned long long *ct_len);
   2593 
   2594 
   2595 /**
   2596  * RFC9180 HPKE encryption.
   2597  * Decrypt messages in a context.
   2598  * Algorithm: DHKEM(X25519, HKDF-SHA256), HKDF-SHA256, ChaCha20Poly1305
   2599  *
   2600  * The encapsulation "enc" must be exchanged with the receiver.
   2601  * From then on, encrypted messages can be decrypted using "ctx"
   2602  *
   2603  * @param ctx the encryption context
   2604  * @param aad addition authenticated data to send (not encrypted)
   2605  * @param aad_len length of aad in bytes
   2606  * @param ct ciphertext to decrypt
   2607  * @param ct_len length of ct in bytes
   2608  * @param pt plaintext (to be allocated by caller)
   2609  * @param pt_len length of written bytes in pt. May be NULL
   2610  * @return GNUNET_OK on success
   2611  */
   2612 enum GNUNET_GenericReturnValue
   2613 GNUNET_CRYPTO_hpke_open (struct GNUNET_CRYPTO_HpkeContext *ctx,
   2614                          const uint8_t*aad, size_t aad_len,
   2615                          const uint8_t *ct, size_t ct_len,
   2616                          uint8_t *pt, unsigned long long *pt_len_p);
   2617 
   2618 
   2619 /**
   2620  * RFC9180 HPKE encryption.
   2621  * Decrypt messages in a context.
   2622  * Algorithm: DHKEM(X25519, HKDF-SHA256), HKDF-SHA256, ChaCha20Poly1305
   2623  *
   2624  * The encapsulation "enc" must be exchanged with the receiver.
   2625  * From then on, encrypted messages can be decrypted using "ctx"
   2626  *
   2627  * @param skR the X25519 receiver secret key
   2628  * @param info the info context separator
   2629  * @param info_len length of info in bytes
   2630  * @param aad addition authenticated data to send (not encrypted)
   2631  * @param aad_len length of aad in bytes
   2632  * @param ct ciphertext to decrypt
   2633  * @param ct_len length of ct in bytes
   2634  * @param pt plaintext (to be allocated by caller)
   2635  * @param pt_len length of written bytes in pt. May be NULL
   2636  * @return GNUNET_OK on success
   2637  */
   2638 enum GNUNET_GenericReturnValue
   2639 GNUNET_CRYPTO_hpke_open_oneshot (
   2640   const struct GNUNET_CRYPTO_HpkePrivateKey *skR,
   2641   const uint8_t *info, size_t info_len,
   2642   const uint8_t*aad, size_t aad_len,
   2643   const uint8_t *ct, size_t ct_len,
   2644   uint8_t *pt, unsigned long long *pt_len);
   2645 
   2646 
   2647 /** HPKE END **/
   2648 
   2649 /**
   2650  * @ingroup crypto
   2651  * Derive key material from a ECDH public key and a private ECDSA key.
   2652  * Dual to #GNUNET_CRRYPTO_ecdh_ecdsa.
   2653  *
   2654  * @param priv private key from ECDSA to use for the ECDH (x)
   2655  * @param pub public key to use for the ECDH (yG)
   2656  * @param key_material where to write the key material H(h(x)yG)
   2657  * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
   2658  */
   2659 enum GNUNET_GenericReturnValue
   2660 GNUNET_CRYPTO_ecdsa_ecdh (const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv,
   2661                           const struct GNUNET_CRYPTO_EcdhePublicKey *pub,
   2662                           struct GNUNET_HashCode *key_material);
   2663 
   2664 
   2665 /**
   2666  * @ingroup crypto
   2667  * Derive key material from a EdDSA public key and a private ECDH key.
   2668  * Dual to #GNUNET_CRRYPTO_eddsa_ecdh.
   2669  * This converts the Edwards25519 public key @a pub to a Curve25519
   2670  * public key before computing a X25519 DH (see #GNUNET_CRYPTO_ecc_ecdh).
   2671  * The resulting X25519 secret is then derived to a key using
   2672  * SHA-512.
   2673  * NOTE: Whenever you can get away with it, use separate key pairs
   2674  * for signing and encryption (DH)!
   2675  *
   2676  * @param priv private key to use for the ECDH (y)
   2677  * @param pub public key from EdDSA to use for the ECDH (X=h(x)G)
   2678  * @param key_material where to write the key material H(yX)=H(h(x)yG)
   2679  * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
   2680  */
   2681 enum GNUNET_GenericReturnValue
   2682 GNUNET_CRYPTO_ecdh_eddsa (const struct GNUNET_CRYPTO_EcdhePrivateKey *priv,
   2683                           const struct GNUNET_CRYPTO_EddsaPublicKey *pub,
   2684                           struct GNUNET_HashCode *key_material);
   2685 
   2686 
   2687 /**
   2688  * @ingroup crypto
   2689  * Derive key material from a EdDSA public key and a private ECDH key.
   2690  * Dual to #GNUNET_CRRYPTO_x25519_ecdh.
   2691  * NOTE: Whenever you can get away with it, use separate key pairs
   2692  * for signing and encryption (DH)!
   2693  *
   2694  * @param priv private key to use for the ECDH (y)
   2695  * @param pub public key from EdDSA to use for the ECDH (X=h(x)G)
   2696  * @param dh the DH shared secret (NOTE: Derive key from this before use!)
   2697  * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
   2698  */
   2699 enum GNUNET_GenericReturnValue
   2700 GNUNET_CRYPTO_ecdh_x25519 (const struct GNUNET_CRYPTO_EcdhePrivateKey *
   2701                            priv,
   2702                            const struct GNUNET_CRYPTO_EcdhePublicKey *pub,
   2703                            struct GNUNET_CRYPTO_EcdhePublicKey *dh);
   2704 
   2705 /**
   2706  * @ingroup crypto
   2707  * Derive key material from a EcDSA public key and a private ECDH key.
   2708  * Dual to #GNUNET_CRRYPTO_ecdsa_ecdh.
   2709  *
   2710  * @param priv private key to use for the ECDH (y)
   2711  * @param pub public key from ECDSA to use for the ECDH (X=h(x)G)
   2712  * @param key_material where to write the key material H(yX)=H(h(x)yG)
   2713  * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
   2714  */
   2715 enum GNUNET_GenericReturnValue
   2716 GNUNET_CRYPTO_ecdh_ecdsa (const struct GNUNET_CRYPTO_EcdhePrivateKey *priv,
   2717                           const struct GNUNET_CRYPTO_EcdsaPublicKey *pub,
   2718                           struct GNUNET_HashCode *key_material);
   2719 
   2720 
   2721 /**
   2722  * @ingroup crypto
   2723  * @brief EdDSA sign a given block.
   2724  *
   2725  * The @a purpose data is the beginning of the data of which the signature is
   2726  * to be created. The `size` field in @a purpose must correctly indicate the
   2727  * number of bytes of the data structure, including its header.  If possible,
   2728  * use #GNUNET_CRYPTO_eddsa_sign() instead of this function (only if @a validate
   2729  * is not fixed-size, you must use this function directly).
   2730  *
   2731  * @param priv private key to use for the signing
   2732  * @param purpose what to sign (size, purpose)
   2733  * @param[out] sig where to write the signature
   2734  * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
   2735  */
   2736 enum GNUNET_GenericReturnValue
   2737 GNUNET_CRYPTO_eddsa_sign_ (
   2738   const struct GNUNET_CRYPTO_EddsaPrivateKey *priv,
   2739   const struct GNUNET_CRYPTO_SignaturePurpose *purpose,
   2740   struct GNUNET_CRYPTO_EddsaSignature *sig);
   2741 
   2742 
   2743 /**
   2744  * @ingroup crypto
   2745  * @brief EdDSA sign a given block.
   2746  *
   2747  * The @a ps data must be a fixed-size struct for which the signature is to be
   2748  * created. The `size` field in @a ps->purpose must correctly indicate the
   2749  * number of bytes of the data structure, including its header.
   2750  *
   2751  * @param priv private key to use for the signing
   2752  * @param ps packed struct with what to sign, MUST begin with a purpose
   2753  * @param[out] sig where to write the signature
   2754  */
   2755 #define GNUNET_CRYPTO_eddsa_sign(priv,ps,sig) do {                 \
   2756     /* check size is set correctly */                              \
   2757     GNUNET_assert (ntohl ((ps)->purpose.size) == sizeof (*ps));    \
   2758     /* check 'ps' begins with the purpose */                       \
   2759     GNUNET_static_assert (((void*) (ps)) ==                        \
   2760                           ((void*) &(ps)->purpose));               \
   2761     GNUNET_assert (GNUNET_OK ==                                    \
   2762                    GNUNET_CRYPTO_eddsa_sign_ (priv,                \
   2763                                               &(ps)->purpose,      \
   2764                                               sig));               \
   2765 } while (0)
   2766 
   2767 
   2768 /**
   2769  * @ingroup crypto
   2770  * @brief ECDSA Sign a given block.
   2771  *
   2772  * The @a purpose data is the beginning of the data of which the signature is
   2773  * to be created. The `size` field in @a purpose must correctly indicate the
   2774  * number of bytes of the data structure, including its header. If possible,
   2775  * use #GNUNET_CRYPTO_ecdsa_sign() instead of this function (only if @a validate
   2776  * is not fixed-size, you must use this function directly).
   2777  *
   2778  * @param priv private key to use for the signing
   2779  * @param purpose what to sign (size, purpose)
   2780  * @param[out] sig where to write the signature
   2781  * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
   2782  */
   2783 enum GNUNET_GenericReturnValue
   2784 GNUNET_CRYPTO_ecdsa_sign_ (
   2785   const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv,
   2786   const struct GNUNET_CRYPTO_SignaturePurpose *purpose,
   2787   struct GNUNET_CRYPTO_EcdsaSignature *sig);
   2788 
   2789 /**
   2790  * @brief
   2791  *
   2792  * @param priv
   2793  * @param data
   2794  * @param size
   2795  * @param sig
   2796  * @return enum GNUNET_GenericReturnValue
   2797  */
   2798 enum GNUNET_GenericReturnValue
   2799 GNUNET_CRYPTO_eddsa_sign_raw (
   2800   const struct GNUNET_CRYPTO_EddsaPrivateKey *priv,
   2801   void *data,
   2802   size_t size,
   2803   struct GNUNET_CRYPTO_EddsaSignature *sig);
   2804 
   2805 /**
   2806  * @ingroup crypto
   2807  * @brief ECDSA sign a given block.
   2808  *
   2809  * The @a ps data must be a fixed-size struct for which the signature is to be
   2810  * created. The `size` field in @a ps->purpose must correctly indicate the
   2811  * number of bytes of the data structure, including its header.
   2812  *
   2813  * @param priv private key to use for the signing
   2814  * @param ps packed struct with what to sign, MUST begin with a purpose
   2815  * @param[out] sig where to write the signature
   2816  */
   2817 #define GNUNET_CRYPTO_ecdsa_sign(priv,ps,sig) do {                 \
   2818     /* check size is set correctly */                              \
   2819     GNUNET_assert (ntohl ((ps)->purpose.size) == sizeof (*(ps)));  \
   2820     /* check 'ps' begins with the purpose */                       \
   2821     GNUNET_static_assert (((void*) (ps)) ==                        \
   2822                           ((void*) &(ps)->purpose));               \
   2823     GNUNET_assert (GNUNET_OK ==                                    \
   2824                    GNUNET_CRYPTO_ecdsa_sign_ (priv,                \
   2825                                               &(ps)->purpose,      \
   2826                                               sig));               \
   2827 } while (0)
   2828 
   2829 /**
   2830  * @ingroup crypto
   2831  * @brief Edx25519 sign a given block.
   2832  *
   2833  * The @a purpose data is the beginning of the data of which the signature is
   2834  * to be created. The `size` field in @a purpose must correctly indicate the
   2835  * number of bytes of the data structure, including its header.  If possible,
   2836  * use #GNUNET_CRYPTO_edx25519_sign() instead of this function (only if @a
   2837  * validate is not fixed-size, you must use this function directly).
   2838  *
   2839  * @param priv private key to use for the signing
   2840  * @param purpose what to sign (size, purpose)
   2841  * @param[out] sig where to write the signature
   2842  * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
   2843  */
   2844 enum GNUNET_GenericReturnValue
   2845 GNUNET_CRYPTO_edx25519_sign_ (
   2846   const struct GNUNET_CRYPTO_Edx25519PrivateKey *priv,
   2847   const struct GNUNET_CRYPTO_SignaturePurpose *purpose,
   2848   struct GNUNET_CRYPTO_Edx25519Signature *sig);
   2849 
   2850 
   2851 /**
   2852  * @ingroup crypto
   2853  * @brief Edx25519 sign a given block.  The resulting signature is compatible
   2854  * with EdDSA.
   2855  *
   2856  * The @a ps data must be a fixed-size struct for which the signature is to be
   2857  * created. The `size` field in @a ps->purpose must correctly indicate the
   2858  * number of bytes of the data structure, including its header.
   2859  *
   2860  * @param priv private key to use for the signing
   2861  * @param ps packed struct with what to sign, MUST begin with a purpose
   2862  * @param[out] sig where to write the signature
   2863  */
   2864 #define GNUNET_CRYPTO_edx25519_sign(priv,ps,sig) do {              \
   2865     /* check size is set correctly */                              \
   2866     GNUNET_assert (ntohl ((ps)->purpose.size) == sizeof (*(ps)));  \
   2867     /* check 'ps' begins with the purpose */                       \
   2868     GNUNET_static_assert (((void*) (ps)) ==                        \
   2869                           ((void*) &(ps)->purpose));               \
   2870     GNUNET_assert (GNUNET_OK ==                                    \
   2871                    GNUNET_CRYPTO_edx25519_sign_ (priv,             \
   2872                                                  &(ps)->purpose,   \
   2873                                                  sig));            \
   2874 } while (0)
   2875 
   2876 
   2877 /**
   2878  * @ingroup crypto
   2879  * @brief Verify EdDSA signature.
   2880  *
   2881  * The @a validate data is the beginning of the data of which the signature
   2882  * is to be verified. The `size` field in @a validate must correctly indicate
   2883  * the number of bytes of the data structure, including its header.  If @a
   2884  * purpose does not match the purpose given in @a validate (the latter must be
   2885  * in big endian), signature verification fails.  If possible,
   2886  * use #GNUNET_CRYPTO_eddsa_verify() instead of this function (only if @a validate
   2887  * is not fixed-size, you must use this function directly).
   2888  *
   2889  * @param purpose what is the purpose that the signature should have?
   2890  * @param validate block to validate (size, purpose, data)
   2891  * @param sig signature that is being validated
   2892  * @param pub public key of the signer
   2893  * @returns #GNUNET_OK if ok, #GNUNET_SYSERR if invalid
   2894  */
   2895 enum GNUNET_GenericReturnValue
   2896 GNUNET_CRYPTO_eddsa_verify_ (
   2897   uint32_t purpose,
   2898   const struct GNUNET_CRYPTO_SignaturePurpose *validate,
   2899   const struct GNUNET_CRYPTO_EddsaSignature *sig,
   2900   const struct GNUNET_CRYPTO_EddsaPublicKey *pub);
   2901 
   2902 
   2903 /**
   2904  * @ingroup crypto
   2905  * @brief Verify EdDSA signature.
   2906  *
   2907  * The @a ps data must be a fixed-size struct for which the signature is to be
   2908  * created. The `size` field in @a ps->purpose must correctly indicate the
   2909  * number of bytes of the data structure, including its header.
   2910  *
   2911  * @param purp purpose of the signature, must match 'ps->purpose.purpose'
   2912  *              (except in host byte order)
   2913  * @param ps packed struct with what to sign, MUST begin with a purpose
   2914  * @param sig where to write the signature
   2915  * @param pub public key key to use for the verification
   2916  */
   2917 #define GNUNET_CRYPTO_eddsa_verify(purp,ps,sig,pub) ({             \
   2918     /* check size is set correctly */                              \
   2919     GNUNET_assert (ntohl ((ps)->purpose.size) == sizeof (*(ps)));  \
   2920     /* check 'ps' begins with the purpose */                       \
   2921     GNUNET_static_assert (((void*) (ps)) ==                        \
   2922                           ((void*) &(ps)->purpose));               \
   2923     GNUNET_CRYPTO_eddsa_verify_ (purp,                             \
   2924                                  &(ps)->purpose,                   \
   2925                                  sig,                              \
   2926                                  pub);                             \
   2927   })
   2928 
   2929 /**
   2930  * @ingroup crypto
   2931  * @brief Verify ECDSA signature.
   2932  *
   2933  * The @a validate data is the beginning of the data of which the signature is
   2934  * to be verified. The `size` field in @a validate must correctly indicate the
   2935  * number of bytes of the data structure, including its header.  If @a purpose
   2936  * does not match the purpose given in @a validate (the latter must be in big
   2937  * endian), signature verification fails.  If possible, use
   2938  * #GNUNET_CRYPTO_eddsa_verify() instead of this function (only if @a validate
   2939  * is not fixed-size, you must use this function directly).
   2940  *
   2941  * @param purpose what is the purpose that the signature should have?
   2942  * @param validate block to validate (size, purpose, data)
   2943  * @param sig signature that is being validated
   2944  * @param pub public key of the signer
   2945  * @returns #GNUNET_OK if ok, #GNUNET_SYSERR if invalid
   2946  */
   2947 enum GNUNET_GenericReturnValue
   2948 GNUNET_CRYPTO_ecdsa_verify_ (
   2949   uint32_t purpose,
   2950   const struct GNUNET_CRYPTO_SignaturePurpose *validate,
   2951   const struct GNUNET_CRYPTO_EcdsaSignature *sig,
   2952   const struct GNUNET_CRYPTO_EcdsaPublicKey *pub);
   2953 
   2954 
   2955 /**
   2956  * @ingroup crypto
   2957  * @brief Verify ECDSA signature.
   2958  *
   2959  * The @a ps data must be a fixed-size struct for which the signature is to be
   2960  * created. The `size` field in @a ps->purpose must correctly indicate the
   2961  * number of bytes of the data structure, including its header.
   2962  *
   2963  * @param purp purpose of the signature, must match 'ps->purpose.purpose'
   2964  *              (except in host byte order)
   2965  * @param priv private key to use for the signing
   2966  * @param ps packed struct with what to sign, MUST begin with a purpose
   2967  * @param sig where to write the signature
   2968  */
   2969 #define GNUNET_CRYPTO_ecdsa_verify(purp,ps,sig,pub) ({             \
   2970     /* check size is set correctly */                              \
   2971     GNUNET_assert (ntohl ((ps)->purpose.size) == sizeof (*(ps)));  \
   2972     /* check 'ps' begins with the purpose */                       \
   2973     GNUNET_static_assert (((void*) (ps)) ==                        \
   2974                           ((void*) &(ps)->purpose));               \
   2975     GNUNET_CRYPTO_ecdsa_verify_ (purp,                             \
   2976                                  &(ps)->purpose,                   \
   2977                                  sig,                              \
   2978                                  pub);                             \
   2979   })
   2980 
   2981 /**
   2982  * @ingroup crypto
   2983  * @brief Verify Edx25519 signature.
   2984  *
   2985  * The @a validate data is the beginning of the data of which the signature
   2986  * is to be verified. The `size` field in @a validate must correctly indicate
   2987  * the number of bytes of the data structure, including its header.  If @a
   2988  * purpose does not match the purpose given in @a validate (the latter must be
   2989  * in big endian), signature verification fails.  If possible, use
   2990  * #GNUNET_CRYPTO_edx25519_verify() instead of this function (only if @a
   2991  * validate is not fixed-size, you must use this function directly).
   2992  *
   2993  * @param purpose what is the purpose that the signature should have?
   2994  * @param validate block to validate (size, purpose, data)
   2995  * @param sig signature that is being validated
   2996  * @param pub public key of the signer
   2997  * @returns #GNUNET_OK if ok, #GNUNET_SYSERR if invalid
   2998  */
   2999 enum GNUNET_GenericReturnValue
   3000 GNUNET_CRYPTO_edx25519_verify_ (
   3001   uint32_t purpose,
   3002   const struct GNUNET_CRYPTO_SignaturePurpose *validate,
   3003   const struct GNUNET_CRYPTO_Edx25519Signature *sig,
   3004   const struct GNUNET_CRYPTO_Edx25519PublicKey *pub);
   3005 
   3006 
   3007 /**
   3008  * @ingroup crypto
   3009  * @brief Verify Edx25519 signature.
   3010  *
   3011  * The @a ps data must be a fixed-size struct for which the signature is to be
   3012  * created. The `size` field in @a ps->purpose must correctly indicate the
   3013  * number of bytes of the data structure, including its header.
   3014  *
   3015  * @param purp purpose of the signature, must match 'ps->purpose.purpose'
   3016  *              (except in host byte order)
   3017  * @param priv private key to use for the signing
   3018  * @param ps packed struct with what to sign, MUST begin with a purpose
   3019  * @param sig where to write the signature
   3020  */
   3021 #define GNUNET_CRYPTO_edx25519_verify(purp,ps,sig,pub) ({         \
   3022     /* check size is set correctly */                             \
   3023     GNUNET_assert (ntohl ((ps)->purpose.size) == sizeof (*(ps))); \
   3024     /* check 'ps' begins with the purpose */                      \
   3025     GNUNET_static_assert (((void*) (ps)) ==                       \
   3026                           ((void*) &(ps)->purpose));              \
   3027     GNUNET_CRYPTO_edx25519_verify_ (purp,                         \
   3028                                     &(ps)->purpose,               \
   3029                                     sig,                          \
   3030                                     pub);                         \
   3031   })
   3032 
   3033 /**
   3034  * @ingroup crypto
   3035  * Derive a private key from a given private key and a label.
   3036  * Essentially calculates a private key 'h = H(l,P) * d mod n'
   3037  * where n is the size of the ECC group and P is the public
   3038  * key associated with the private key 'd'.
   3039  *
   3040  * @param priv original private key
   3041  * @param label label to use for key deriviation
   3042  * @param context additional context to use for HKDF of 'h';
   3043  *        typically the name of the subsystem/application
   3044  * @return derived private key
   3045  */
   3046 struct GNUNET_CRYPTO_EcdsaPrivateKey *
   3047 GNUNET_CRYPTO_ecdsa_private_key_derive (
   3048   const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv,
   3049   const char *label,
   3050   const char *context);
   3051 
   3052 
   3053 /**
   3054  * @ingroup crypto
   3055  * Derive a public key from a given public key and a label.
   3056  * Essentially calculates a public key 'V = H(l,P) * P'.
   3057  *
   3058  * @param pub original public key
   3059  * @param label label to use for key deriviation
   3060  * @param context additional context to use for HKDF of 'h'.
   3061  *        typically the name of the subsystem/application
   3062  * @param result where to write the derived public key
   3063  */
   3064 void
   3065 GNUNET_CRYPTO_ecdsa_public_key_derive (
   3066   const struct GNUNET_CRYPTO_EcdsaPublicKey *pub,
   3067   const char *label,
   3068   const char *context,
   3069   struct GNUNET_CRYPTO_EcdsaPublicKey *result);
   3070 
   3071 /**
   3072  * This is a signature function for ECDSA which takes a
   3073  * private key, derives/blinds it and signs the message.
   3074  *
   3075  * @param pkey original private key
   3076  * @param label label to use for key deriviation
   3077  * @param context additional context to use for HKDF of 'h';
   3078  *        typically the name of the subsystem/application
   3079  * @param purpose the signature purpose
   3080  * @param sig the resulting signature
   3081  * @return GNUNET_OK on success
   3082  */
   3083 enum GNUNET_GenericReturnValue
   3084 GNUNET_CRYPTO_ecdsa_sign_derived (
   3085   const struct GNUNET_CRYPTO_EcdsaPrivateKey *pkey,
   3086   const char *label,
   3087   const char *context,
   3088   const struct GNUNET_CRYPTO_SignaturePurpose *purpose,
   3089   struct GNUNET_CRYPTO_EcdsaSignature *sig);
   3090 
   3091 
   3092 /**
   3093  * @ingroup crypto
   3094  * Derive a private scalar from a given private key and a label.
   3095  * Essentially calculates a private key 'h = H(l,P) * d mod n'
   3096  * where n is the size of the ECC group and P is the public
   3097  * key associated with the private key 'd'.
   3098  * The result is the derived private _scalar_, not the private
   3099  * key as for EdDSA we cannot derive before we hash the
   3100  * private key.
   3101  *
   3102  * @param priv original private key
   3103  * @param label label to use for key deriviation
   3104  * @param context additional context to use for HKDF of 'h';
   3105  *        typically the name of the subsystem/application
   3106  * @param result derived private scalar
   3107  */
   3108 void
   3109 GNUNET_CRYPTO_eddsa_private_key_derive (
   3110   const struct GNUNET_CRYPTO_EddsaPrivateKey *priv,
   3111   const char *label,
   3112   const char *context,
   3113   struct GNUNET_CRYPTO_EddsaPrivateScalar *result);
   3114 
   3115 
   3116 /**
   3117  * @ingroup crypto
   3118  * Derive a public key from a given public key and a label.
   3119  * Essentially calculates a public key 'V = H(l,P) * P'.
   3120  *
   3121  * @param pub original public key
   3122  * @param label label to use for key deriviation
   3123  * @param context additional context to use for HKDF of 'h'.
   3124  *        typically the name of the subsystem/application
   3125  * @param result where to write the derived public key
   3126  */
   3127 void
   3128 GNUNET_CRYPTO_eddsa_public_key_derive (
   3129   const struct GNUNET_CRYPTO_EddsaPublicKey *pub,
   3130   const char *label,
   3131   const char *context,
   3132   struct GNUNET_CRYPTO_EddsaPublicKey *result);
   3133 
   3134 
   3135 /**
   3136  * This is a signature function for EdDSA which takes a
   3137  * private key and derives it using the label and context
   3138  * before signing.
   3139  *
   3140  * @param pkey original private key
   3141  * @param label label to use for key deriviation
   3142  * @param context additional context to use for HKDF of 'h';
   3143  *        typically the name of the subsystem/application
   3144  * @param purpose the signature purpose
   3145  * @param sig the resulting signature
   3146  * @return GNUNET_OK on success
   3147  */
   3148 enum GNUNET_GenericReturnValue
   3149 GNUNET_CRYPTO_eddsa_sign_derived (
   3150   const struct GNUNET_CRYPTO_EddsaPrivateKey *pkey,
   3151   const char *label,
   3152   const char *context,
   3153   const struct GNUNET_CRYPTO_SignaturePurpose *purpose,
   3154   struct GNUNET_CRYPTO_EddsaSignature *sig);
   3155 
   3156 
   3157 /**
   3158  * Extract the public key of the given private scalar.
   3159  *
   3160  * @param s the private scalar
   3161  * @param pkey the resulting public key
   3162  */
   3163 void
   3164 GNUNET_CRYPTO_eddsa_key_get_public_from_scalar (
   3165   const struct GNUNET_CRYPTO_EddsaPrivateScalar *s,
   3166   struct GNUNET_CRYPTO_EddsaPublicKey *pkey);
   3167 
   3168 /**
   3169  * @ingroup crypto
   3170  * Derive a private scalar from a given private key and a label.
   3171  * Essentially calculates a private key 'h = H(l,P) * d mod n'
   3172  * where n is the size of the ECC group and P is the public
   3173  * key associated with the private key 'd'.
   3174  *
   3175  * @param priv original private key
   3176  * @param seed input seed
   3177  * @param seedsize size of the seed
   3178  * @param result derived private key
   3179  */
   3180 void
   3181 GNUNET_CRYPTO_edx25519_private_key_derive (
   3182   const struct GNUNET_CRYPTO_Edx25519PrivateKey *priv,
   3183   const void *seed,
   3184   size_t seedsize,
   3185   struct GNUNET_CRYPTO_Edx25519PrivateKey *result);
   3186 
   3187 
   3188 /**
   3189  * @ingroup crypto
   3190  * Derive a public key from a given public key and a label.
   3191  * Essentially calculates a public key 'V = H(l,P) * P'.
   3192  *
   3193  * @param pub original public key
   3194  * @param seed input seed
   3195  * @param seedsize size of the seed
   3196  * @param result where to write the derived public key
   3197  */
   3198 void
   3199 GNUNET_CRYPTO_edx25519_public_key_derive (
   3200   const struct GNUNET_CRYPTO_Edx25519PublicKey *pub,
   3201   const void *seed,
   3202   size_t seedsize,
   3203   struct GNUNET_CRYPTO_Edx25519PublicKey *result);
   3204 
   3205 
   3206 /**
   3207  * @ingroup crypto
   3208  * Clears the most significant bit and second most significant bit of the serialized representaive before applying elligator direct map.
   3209  *
   3210  * @param representative serialized elligator representative of an element of Curves25519's finite field
   3211  * @param point destination for the calculated point on the curve
   3212  * @param high_y bool pointed to will be set to 'true' if corresponding y-coordinate is > 2 ^ 254 - 10, otherwise 0. Can be set to NULL if not needed.
   3213  */
   3214 void
   3215 GNUNET_CRYPTO_ecdhe_elligator_decoding (
   3216   struct GNUNET_CRYPTO_EcdhePublicKey *point,
   3217   bool *high_y,
   3218   const struct GNUNET_CRYPTO_ElligatorRepresentative *representative);
   3219 
   3220 
   3221 /**
   3222  * @ingroup crypto
   3223  * Encodes a point on Curve25519 to a an element of the underlying finite field.
   3224  * This transformation is deterministic.
   3225  *
   3226  * @param random_tweak random 8-bit value used as seed
   3227  * @param r storage for the calculated representative
   3228  * @param pub a point on the curve
   3229  * @return 'true' if the given point can be encoded into a representative. Otherwise 'false' is returned and the content of the representative storage is undefined
   3230  */
   3231 bool
   3232 GNUNET_CRYPTO_ecdhe_elligator_encoding (
   3233   uint8_t random_tweak,
   3234   struct GNUNET_CRYPTO_ElligatorRepresentative *r,
   3235   const struct GNUNET_CRYPTO_EcdhePublicKey *pub);
   3236 
   3237 
   3238 /**
   3239  * @ingroup crypto
   3240  * Generates a valid public key for elligator's inverse map by adding a lower order point to a prime order point.
   3241  * Following Method 1 in description https://elligator.org/key-exchange section Step 2: Generate a “special” public key.
   3242  *
   3243  * @param random_tweak random 8-bit value used as seed
   3244  * @param sk private key for generating valid public key
   3245  * @param pub valid public key for elligator inverse map
   3246  * @param repr storage for a calculated representative
   3247  * @return GNUNET_OK on success
   3248  */
   3249 enum GNUNET_GenericReturnValue
   3250 GNUNET_CRYPTO_ecdhe_elligator_key_get_public_norand (
   3251   uint8_t random_tweak,
   3252   const struct GNUNET_CRYPTO_ElligatorEcdhePrivateKey *sk,
   3253   struct GNUNET_CRYPTO_EcdhePublicKey *pub,
   3254   struct GNUNET_CRYPTO_ElligatorRepresentative *repr);
   3255 
   3256 /**
   3257  * @ingroup crypto
   3258  * Generates a valid public key for elligator's inverse map by adding a lower order point to a prime order point.
   3259  * Following Method 1 in description https://elligator.org/key-exchange section Step 2: Generate a “special” public key.
   3260  *
   3261  * @param sk private key for generating valid public key
   3262  * @param pub valid public key for elligator inverse map
   3263  * @param repr storage for a calculated representative
   3264  * @return GNUNET_OK on success
   3265  */
   3266 enum GNUNET_GenericReturnValue
   3267 GNUNET_CRYPTO_ecdhe_elligator_key_get_public (
   3268   const struct GNUNET_CRYPTO_ElligatorEcdhePrivateKey *sk,
   3269   struct GNUNET_CRYPTO_EcdhePublicKey *pub,
   3270   struct GNUNET_CRYPTO_ElligatorRepresentative *repr);
   3271 
   3272 
   3273 /**
   3274  * @ingroup crypto
   3275  * Generates a private key for Curve25519.
   3276  *
   3277  * @param sk Curve25519 private key
   3278  */
   3279 void
   3280 GNUNET_CRYPTO_ecdhe_elligator_key_create (
   3281   struct GNUNET_CRYPTO_ElligatorEcdhePrivateKey *sk);
   3282 
   3283 
   3284 /**
   3285  * Output the given MPI value to the given buffer in network
   3286  * byte order.  The MPI @a val may not be negative.
   3287  *
   3288  * @param buf where to output to
   3289  * @param size number of bytes in @a buf
   3290  * @param val value to write to @a buf
   3291  */
   3292 void
   3293 GNUNET_CRYPTO_mpi_print_unsigned (void *buf,
   3294                                   size_t size,
   3295                                   gcry_mpi_t val);
   3296 
   3297 
   3298 /**
   3299  * Convert data buffer into MPI value.
   3300  * The buffer is interpreted as network
   3301  * byte order, unsigned integer.
   3302  *
   3303  * @param result where to store MPI value (allocated)
   3304  * @param data raw data (GCRYMPI_FMT_USG)
   3305  * @param size number of bytes in @a data
   3306  */
   3307 void
   3308 GNUNET_CRYPTO_mpi_scan_unsigned (gcry_mpi_t *result,
   3309                                  const void *data,
   3310                                  size_t size);
   3311 
   3312 
   3313 /**
   3314  * Create a freshly generated paillier public key.
   3315  *
   3316  * @param[out] public_key Where to store the public key?
   3317  * @param[out] private_key Where to store the private key?
   3318  */
   3319 void
   3320 GNUNET_CRYPTO_paillier_create (
   3321   struct GNUNET_CRYPTO_PaillierPublicKey *public_key,
   3322   struct GNUNET_CRYPTO_PaillierPrivateKey *private_key);
   3323 
   3324 
   3325 /**
   3326  * Encrypt a plaintext with a paillier public key.
   3327  *
   3328  * @param public_key Public key to use.
   3329  * @param m Plaintext to encrypt.
   3330  * @param desired_ops How many homomorphic ops the caller intends to use
   3331  * @param[out] ciphertext Encryption of @a plaintext with @a public_key.
   3332  * @return guaranteed number of supported homomorphic operations >= 1,
   3333  *         or desired_ops, in case that is lower,
   3334  *         or -1 if less than one homomorphic operation is possible
   3335  */
   3336 int
   3337 GNUNET_CRYPTO_paillier_encrypt (
   3338   const struct GNUNET_CRYPTO_PaillierPublicKey *public_key,
   3339   const gcry_mpi_t m,
   3340   int desired_ops,
   3341   struct GNUNET_CRYPTO_PaillierCiphertext *ciphertext);
   3342 
   3343 
   3344 /**
   3345  * Decrypt a paillier ciphertext with a private key.
   3346  *
   3347  * @param private_key Private key to use for decryption.
   3348  * @param public_key Public key to use for decryption.
   3349  * @param ciphertext Ciphertext to decrypt.
   3350  * @param[out] m Decryption of @a ciphertext with @a private_key.
   3351  */
   3352 void
   3353 GNUNET_CRYPTO_paillier_decrypt (
   3354   const struct GNUNET_CRYPTO_PaillierPrivateKey *private_key,
   3355   const struct GNUNET_CRYPTO_PaillierPublicKey *public_key,
   3356   const struct GNUNET_CRYPTO_PaillierCiphertext *ciphertext,
   3357   gcry_mpi_t m);
   3358 
   3359 
   3360 /**
   3361  * Compute a ciphertext that represents the sum of the plaintext in @a c1
   3362  * and @a c2
   3363  *
   3364  * Note that this operation can only be done a finite number of times
   3365  * before an overflow occurs.
   3366  *
   3367  * @param public_key Public key to use for encryption.
   3368  * @param c1 Paillier cipher text.
   3369  * @param c2 Paillier cipher text.
   3370  * @param[out] result Result of the homomorphic operation.
   3371  * @return #GNUNET_OK if the result could be computed,
   3372  *         #GNUNET_SYSERR if no more homomorphic operations are remaining.
   3373  */
   3374 int
   3375 GNUNET_CRYPTO_paillier_hom_add (
   3376   const struct GNUNET_CRYPTO_PaillierPublicKey *public_key,
   3377   const struct GNUNET_CRYPTO_PaillierCiphertext *c1,
   3378   const struct GNUNET_CRYPTO_PaillierCiphertext *c2,
   3379   struct GNUNET_CRYPTO_PaillierCiphertext *result);
   3380 
   3381 
   3382 /**
   3383  * Get the number of remaining supported homomorphic operations.
   3384  *
   3385  * @param c Paillier cipher text.
   3386  * @return the number of remaining homomorphic operations
   3387  */
   3388 int
   3389 GNUNET_CRYPTO_paillier_hom_get_remaining (
   3390   const struct GNUNET_CRYPTO_PaillierCiphertext *c);
   3391 
   3392 
   3393 /* ********* Chaum-style RSA-based blind signatures ******************* */
   3394 
   3395 
   3396 /**
   3397  * The private information of an RSA key pair.
   3398  */
   3399 struct GNUNET_CRYPTO_RsaPrivateKey;
   3400 
   3401 /**
   3402  * The public information of an RSA key pair.
   3403  */
   3404 struct GNUNET_CRYPTO_RsaPublicKey;
   3405 
   3406 /**
   3407  * Constant-size pre-secret for blinding key generation.
   3408  */
   3409 struct GNUNET_CRYPTO_RsaBlindingKeySecret
   3410 {
   3411   /**
   3412    * Bits used to generate the blinding key.  256 bits
   3413    * of entropy is enough.
   3414    */
   3415   uint32_t pre_secret[8] GNUNET_PACKED;
   3416 };
   3417 
   3418 /**
   3419  * @brief an RSA signature
   3420  */
   3421 struct GNUNET_CRYPTO_RsaSignature;
   3422 
   3423 
   3424 /**
   3425  * Create a new private key. Caller must free return value.
   3426  *
   3427  * @param len length of the key in bits (e.g. 2048)
   3428  * @return fresh private key
   3429  */
   3430 struct GNUNET_CRYPTO_RsaPrivateKey *
   3431 GNUNET_CRYPTO_rsa_private_key_create (unsigned int len);
   3432 
   3433 
   3434 /**
   3435  * Free memory occupied by the private key.
   3436  *
   3437  * @param key pointer to the memory to free
   3438  */
   3439 void
   3440 GNUNET_CRYPTO_rsa_private_key_free (struct GNUNET_CRYPTO_RsaPrivateKey *key);
   3441 
   3442 
   3443 /**
   3444  * Encode the private key in a format suitable for
   3445  * storing it into a file.
   3446  *
   3447  * @param key the private key
   3448  * @param[out] buffer set to a buffer with the encoded key
   3449  * @return size of memory allocatedin @a buffer
   3450  */
   3451 size_t
   3452 GNUNET_CRYPTO_rsa_private_key_encode (
   3453   const struct GNUNET_CRYPTO_RsaPrivateKey *key,
   3454   void **buffer);
   3455 
   3456 
   3457 /**
   3458  * Decode the private key from the data-format back
   3459  * to the "normal", internal format.
   3460  *
   3461  * @param buf the buffer where the private key data is stored
   3462  * @param buf_size the size of the data in @a buf
   3463  * @return NULL on error
   3464  */
   3465 struct GNUNET_CRYPTO_RsaPrivateKey *
   3466 GNUNET_CRYPTO_rsa_private_key_decode (const void *buf,
   3467                                       size_t buf_size);
   3468 
   3469 
   3470 /**
   3471  * Duplicate the given private key
   3472  *
   3473  * @param key the private key to duplicate
   3474  * @return the duplicate key; NULL upon error
   3475  */
   3476 struct GNUNET_CRYPTO_RsaPrivateKey *
   3477 GNUNET_CRYPTO_rsa_private_key_dup (
   3478   const struct GNUNET_CRYPTO_RsaPrivateKey *key);
   3479 
   3480 
   3481 /**
   3482  * Extract the public key of the given private key.
   3483  *
   3484  * @param priv the private key
   3485  * @return NULL on error, otherwise the public key
   3486  */
   3487 struct GNUNET_CRYPTO_RsaPublicKey *
   3488 GNUNET_CRYPTO_rsa_private_key_get_public (
   3489   const struct GNUNET_CRYPTO_RsaPrivateKey *priv);
   3490 
   3491 
   3492 /**
   3493  * Compute hash over the public key.
   3494  *
   3495  * @param key public key to hash
   3496  * @param hc where to store the hash code
   3497  */
   3498 void
   3499 GNUNET_CRYPTO_rsa_public_key_hash (
   3500   const struct GNUNET_CRYPTO_RsaPublicKey *key,
   3501   struct GNUNET_HashCode *hc);
   3502 
   3503 
   3504 /**
   3505  * Check if @a key is well-formed.
   3506  *
   3507  * @return true if @a key is well-formed.
   3508  */
   3509 bool
   3510 GNUNET_CRYPTO_rsa_public_key_check (
   3511   const struct GNUNET_CRYPTO_RsaPublicKey *key);
   3512 
   3513 /**
   3514  * Obtain the length of the RSA key in bits.
   3515  *
   3516  * @param key the public key to introspect
   3517  * @return length of the key in bits
   3518  */
   3519 unsigned int
   3520 GNUNET_CRYPTO_rsa_public_key_len (const struct GNUNET_CRYPTO_RsaPublicKey *key);
   3521 
   3522 
   3523 /**
   3524  * Free memory occupied by the public key.
   3525  *
   3526  * @param key pointer to the memory to free
   3527  */
   3528 void
   3529 GNUNET_CRYPTO_rsa_public_key_free (struct GNUNET_CRYPTO_RsaPublicKey *key);
   3530 
   3531 
   3532 /**
   3533  * Encode the public key in a format suitable for
   3534  * storing it into a file.
   3535  *
   3536  * @param key the private key
   3537  * @param[out] buffer set to a buffer with the encoded key
   3538  * @return size of memory allocated in @a buffer
   3539  */
   3540 size_t
   3541 GNUNET_CRYPTO_rsa_public_key_encode (
   3542   const struct GNUNET_CRYPTO_RsaPublicKey *key,
   3543   void **buffer);
   3544 
   3545 
   3546 /**
   3547  * Decode the public key from the data-format back
   3548  * to the "normal", internal format.
   3549  *
   3550  * @param buf the buffer where the public key data is stored
   3551  * @param len the length of the data in @a buf
   3552  * @return NULL on error
   3553  */
   3554 struct GNUNET_CRYPTO_RsaPublicKey *
   3555 GNUNET_CRYPTO_rsa_public_key_decode (const char *buf,
   3556                                      size_t len);
   3557 
   3558 
   3559 /**
   3560  * Duplicate the given public key
   3561  *
   3562  * @param key the public key to duplicate
   3563  * @return the duplicate key; NULL upon error
   3564  */
   3565 struct GNUNET_CRYPTO_RsaPublicKey *
   3566 GNUNET_CRYPTO_rsa_public_key_dup (const struct GNUNET_CRYPTO_RsaPublicKey *key);
   3567 
   3568 
   3569 /**
   3570  * Compare the values of two signatures.
   3571  *
   3572  * @param s1 one signature
   3573  * @param s2 the other signature
   3574  * @return 0 if the two are equal
   3575  */
   3576 int
   3577 GNUNET_CRYPTO_rsa_signature_cmp (const struct GNUNET_CRYPTO_RsaSignature *s1,
   3578                                  const struct GNUNET_CRYPTO_RsaSignature *s2);
   3579 
   3580 /**
   3581  * Compare the values of two private keys.
   3582  *
   3583  * @param p1 one private key
   3584  * @param p2 the other private key
   3585  * @return 0 if the two are equal
   3586  */
   3587 int
   3588 GNUNET_CRYPTO_rsa_private_key_cmp (
   3589   const struct GNUNET_CRYPTO_RsaPrivateKey *p1,
   3590   const struct GNUNET_CRYPTO_RsaPrivateKey *p2);
   3591 
   3592 
   3593 /**
   3594  * Compare the values of two public keys.
   3595  *
   3596  * @param p1 one public key
   3597  * @param p2 the other public key
   3598  * @return 0 if the two are equal
   3599  */
   3600 int
   3601 GNUNET_CRYPTO_rsa_public_key_cmp (const struct GNUNET_CRYPTO_RsaPublicKey *p1,
   3602                                   const struct GNUNET_CRYPTO_RsaPublicKey *p2);
   3603 
   3604 
   3605 /**
   3606  * @brief RSA Parameters to create blinded signature
   3607  */
   3608 struct GNUNET_CRYPTO_RsaBlindedMessage
   3609 {
   3610   /**
   3611    * Blinded message to be signed
   3612    * Note: is malloc()'ed!
   3613    */
   3614   void *blinded_msg;
   3615 
   3616   /**
   3617    * Size of the @e blinded_msg to be signed.
   3618    */
   3619   size_t blinded_msg_size;
   3620 };
   3621 
   3622 
   3623 /**
   3624  * Blinds the given message with the given blinding key
   3625  *
   3626  * @param message the message to sign
   3627  * @param message_size number of bytes in @a message
   3628  * @param bks the blinding key
   3629  * @param pkey the public key of the signer
   3630  * @param[out] bm set to the blinded message
   3631  * @return #GNUNET_YES if successful, #GNUNET_NO if RSA key is malicious
   3632  */
   3633 enum GNUNET_GenericReturnValue
   3634 GNUNET_CRYPTO_rsa_blind (const void *message,
   3635                          size_t message_size,
   3636                          const struct GNUNET_CRYPTO_RsaBlindingKeySecret *bks,
   3637                          struct GNUNET_CRYPTO_RsaPublicKey *pkey,
   3638                          struct GNUNET_CRYPTO_RsaBlindedMessage *bm);
   3639 
   3640 
   3641 /**
   3642  * Sign a blinded value, which must be a full domain hash of a message.
   3643  *
   3644  * @param key private key to use for the signing
   3645  * @param bm the (blinded) message to sign
   3646  * @return NULL on error, signature on success
   3647  */
   3648 struct GNUNET_CRYPTO_RsaSignature *
   3649 GNUNET_CRYPTO_rsa_sign_blinded (const struct GNUNET_CRYPTO_RsaPrivateKey *key,
   3650                                 const struct
   3651                                 GNUNET_CRYPTO_RsaBlindedMessage *bm);
   3652 
   3653 
   3654 /**
   3655  * Create and sign a full domain hash of a message.
   3656  *
   3657  * @param key private key to use for the signing
   3658  * @param message the message to sign
   3659  * @param message_size number of bytes in @a message
   3660  * @return NULL on error, including a malicious RSA key, signature on success
   3661  */
   3662 struct GNUNET_CRYPTO_RsaSignature *
   3663 GNUNET_CRYPTO_rsa_sign_fdh (const struct GNUNET_CRYPTO_RsaPrivateKey *key,
   3664                             const void *message,
   3665                             size_t message_size);
   3666 
   3667 
   3668 /**
   3669  * Free memory occupied by blinded message. Only frees contents, not
   3670  * @a bm itself.
   3671  *
   3672  * @param[in] bm memory to free
   3673  */
   3674 void
   3675 GNUNET_CRYPTO_rsa_blinded_message_free (
   3676   struct GNUNET_CRYPTO_RsaBlindedMessage *bm);
   3677 
   3678 
   3679 /**
   3680  * Free memory occupied by signature.
   3681  *
   3682  * @param[in] sig memory to free
   3683  */
   3684 void
   3685 GNUNET_CRYPTO_rsa_signature_free (struct GNUNET_CRYPTO_RsaSignature *sig);
   3686 
   3687 
   3688 /**
   3689  * Encode the given signature in a format suitable for storing it into a file.
   3690  *
   3691  * @param sig the signature
   3692  * @param[out] buffer set to a buffer with the encoded key
   3693  * @return size of memory allocated in @a buffer
   3694  */
   3695 size_t
   3696 GNUNET_CRYPTO_rsa_signature_encode (
   3697   const struct GNUNET_CRYPTO_RsaSignature *sig,
   3698   void **buffer);
   3699 
   3700 
   3701 /**
   3702  * Decode the signature from the data-format back to the "normal", internal
   3703  * format.
   3704  *
   3705  * @param buf the buffer where the public key data is stored
   3706  * @param buf_size the number of bytes of the data in @a buf
   3707  * @return NULL on error
   3708  */
   3709 struct GNUNET_CRYPTO_RsaSignature *
   3710 GNUNET_CRYPTO_rsa_signature_decode (
   3711   const void *buf,
   3712   size_t buf_size);
   3713 
   3714 
   3715 /**
   3716  * Duplicate the given rsa signature
   3717  *
   3718  * @param sig the signature to duplicate
   3719  * @return the duplicate key; NULL upon error
   3720  */
   3721 struct GNUNET_CRYPTO_RsaSignature *
   3722 GNUNET_CRYPTO_rsa_signature_dup (
   3723   const struct GNUNET_CRYPTO_RsaSignature *sig);
   3724 
   3725 
   3726 /**
   3727  * Unblind a blind-signed signature.  The signature should have been generated
   3728  * with #GNUNET_CRYPTO_rsa_sign() using a hash that was blinded with
   3729  * #GNUNET_CRYPTO_rsa_blind().
   3730  *
   3731  * @param sig the signature made on the blinded signature purpose
   3732  * @param bks the blinding key secret used to blind the signature purpose
   3733  * @param pkey the public key of the signer
   3734  * @return unblinded signature on success, NULL if RSA key is bad or malicious.
   3735  */
   3736 struct GNUNET_CRYPTO_RsaSignature *
   3737 GNUNET_CRYPTO_rsa_unblind (const struct GNUNET_CRYPTO_RsaSignature *sig,
   3738                            const struct GNUNET_CRYPTO_RsaBlindingKeySecret *bks,
   3739                            struct GNUNET_CRYPTO_RsaPublicKey *pkey);
   3740 
   3741 
   3742 /**
   3743  * Verify whether the given hash corresponds to the given signature and the
   3744  * signature is valid with respect to the given public key.
   3745  *
   3746  * @param message the message to sign
   3747  * @param message_size number of bytes in @a message
   3748  * @param sig signature that is being validated
   3749  * @param public_key public key of the signer
   3750  * @returns #GNUNET_YES if ok, #GNUNET_NO if RSA key is malicious, #GNUNET_SYSERR if signature
   3751  */
   3752 enum GNUNET_GenericReturnValue
   3753 GNUNET_CRYPTO_rsa_verify (const void *message,
   3754                           size_t message_size,
   3755                           const struct GNUNET_CRYPTO_RsaSignature *sig,
   3756                           const struct GNUNET_CRYPTO_RsaPublicKey *public_key);
   3757 
   3758 
   3759 /**
   3760  * Create a new random private key.
   3761  *
   3762  * @param[out] priv where to write the fresh private key
   3763  */
   3764 void
   3765 GNUNET_CRYPTO_cs_private_key_generate (struct GNUNET_CRYPTO_CsPrivateKey *priv);
   3766 
   3767 
   3768 /**
   3769  * Extract the public key of the given private key.
   3770  *
   3771  * @param priv the private key
   3772  * @param[out] pub where to write the public key
   3773  */
   3774 void
   3775 GNUNET_CRYPTO_cs_private_key_get_public (
   3776   const struct GNUNET_CRYPTO_CsPrivateKey *priv,
   3777   struct GNUNET_CRYPTO_CsPublicKey *pub);
   3778 
   3779 
   3780 /**
   3781  * Derive a new secret r pair r0 and r1.
   3782  * In original papers r is generated randomly
   3783  * To provide abort-idempotency, r needs to be derived but still needs to be UNPREDICTABLE
   3784  * To ensure unpredictability a new nonce should be used when a new r needs to be derived.
   3785  * Uses HKDF internally.
   3786  * Comment: Can be done in one HKDF shot and split output.
   3787  *
   3788  * @param nonce is a random nonce
   3789  * @param seed seed to use in derivation
   3790  * @param lts is a long-term-secret in form of a private key
   3791  * @param[out] r array containing derived secrets r0 and r1
   3792  */
   3793 void
   3794 GNUNET_CRYPTO_cs_r_derive (
   3795   const struct GNUNET_CRYPTO_CsSessionNonce *nonce,
   3796   const char *seed,
   3797   const struct GNUNET_CRYPTO_CsPrivateKey *lts,
   3798   struct GNUNET_CRYPTO_CsRSecret r[2]);
   3799 
   3800 
   3801 /**
   3802  * Extract the public R of the given secret r.
   3803  *
   3804  * @param r_priv the private key
   3805  * @param[out] r_pub where to write the public key
   3806  */
   3807 void
   3808 GNUNET_CRYPTO_cs_r_get_public (
   3809   const struct GNUNET_CRYPTO_CsRSecret *r_priv,
   3810   struct GNUNET_CRYPTO_CsRPublic *r_pub);
   3811 
   3812 
   3813 /**
   3814  * Derives new random blinding factors.
   3815  * In original papers blinding factors are generated randomly
   3816  * To provide abort-idempotency, blinding factors need to be derived but still need to be UNPREDICTABLE.
   3817  * To ensure unpredictability a new nonce has to be used.
   3818  * Uses HKDF internally.
   3819  *
   3820  * @param blind_seed is the blinding seed to derive blinding factors
   3821  * @param[out] bs array containing the two derived blinding secrets
   3822  */
   3823 void
   3824 GNUNET_CRYPTO_cs_blinding_secrets_derive (
   3825   const struct GNUNET_CRYPTO_CsBlindingNonce *blind_seed,
   3826   struct GNUNET_CRYPTO_CsBlindingSecret bs[2]);
   3827 
   3828 
   3829 /**
   3830  * @brief CS Parameters derived from the message
   3831  * during blinding to create blinded signature
   3832  */
   3833 struct GNUNET_CRYPTO_CsBlindedMessage
   3834 {
   3835   /**
   3836    * The Clause Schnorr c_0 and c_1 containing the blinded message
   3837    */
   3838   struct GNUNET_CRYPTO_CsC c[2];
   3839 
   3840   /**
   3841    * Nonce used in initial request.
   3842    */
   3843   struct GNUNET_CRYPTO_CsSessionNonce nonce;
   3844 
   3845 };
   3846 
   3847 
   3848 /**
   3849  * Pair of Public R values for Cs denominations
   3850  */
   3851 struct GNUNET_CRYPTO_CSPublicRPairP
   3852 {
   3853   struct GNUNET_CRYPTO_CsRPublic r_pub[2];
   3854 };
   3855 
   3856 
   3857 /**
   3858  * Calculate two blinded c's.
   3859  * Comment: One would be insecure due to Wagner's algorithm solving ROS
   3860  *
   3861  * @param bs array of the two blinding factor structs each containing alpha and beta
   3862  * @param r_pub array of the two signer's nonce R
   3863  * @param pub the public key of the signer
   3864  * @param msg the message to blind in preparation for signing
   3865  * @param msg_len length of message msg
   3866  * @param[out] blinded_c array of the two blinded c's
   3867  * @param[out] r_pub_blind array of the two blinded R
   3868  */
   3869 void
   3870 GNUNET_CRYPTO_cs_calc_blinded_c (
   3871   const struct GNUNET_CRYPTO_CsBlindingSecret bs[2],
   3872   const struct GNUNET_CRYPTO_CsRPublic r_pub[2],
   3873   const struct GNUNET_CRYPTO_CsPublicKey *pub,
   3874   const void *msg,
   3875   size_t msg_len,
   3876   struct GNUNET_CRYPTO_CsC blinded_c[2],
   3877   struct GNUNET_CRYPTO_CSPublicRPairP *r_pub_blind);
   3878 
   3879 
   3880 /**
   3881  * The Sign Answer for Clause Blind Schnorr signature.
   3882  * The sign operation returns a parameter @param b and the signature
   3883  * scalar @param s_scalar.
   3884  */
   3885 struct GNUNET_CRYPTO_CsBlindSignature
   3886 {
   3887   /**
   3888    * To make ROS problem harder, the signer chooses an unpredictable b and
   3889    * only calculates signature of c_b
   3890    */
   3891   unsigned int b;
   3892 
   3893   /**
   3894    * The blinded s scalar calculated from c_b
   3895    */
   3896   struct GNUNET_CRYPTO_CsBlindS s_scalar;
   3897 };
   3898 
   3899 
   3900 /**
   3901  * Sign a blinded @a c.
   3902  * This function derives b from a nonce and a longterm secret.
   3903  * In the original papers b is generated randomly.
   3904  * To provide abort-idempotency, b needs to be derived but still need to be UNPREDICTABLE.
   3905  * To ensure unpredictability a new nonce has to be used for every signature.
   3906  * HKDF is used internally for derivation.
   3907  * r0 and r1 can be derived prior by using GNUNET_CRYPTO_cs_r_derive.
   3908  *
   3909  * @param priv private key to use for the signing and as LTS in HKDF
   3910  * @param r array of the two secret inputs from the signer
   3911  * @param bm blinded message, including array of the two blinded c to sign c_b and the random nonce
   3912  * @param[out] cs_blind_sig where to write the blind signature
   3913  */
   3914 void
   3915 GNUNET_CRYPTO_cs_sign_derive (
   3916   const struct GNUNET_CRYPTO_CsPrivateKey *priv,
   3917   const struct GNUNET_CRYPTO_CsRSecret r[2],
   3918   const struct GNUNET_CRYPTO_CsBlindedMessage *bm,
   3919   struct GNUNET_CRYPTO_CsBlindSignature *cs_blind_sig);
   3920 
   3921 
   3922 /**
   3923  * Unblind a blind-signed signature using a c that was blinded
   3924  *
   3925  * @param blinded_signature_scalar the signature made on the blinded c
   3926  * @param bs the blinding factors used in the blinding
   3927  * @param[out] signature_scalar where to write the unblinded signature
   3928  */
   3929 void
   3930 GNUNET_CRYPTO_cs_unblind (
   3931   const struct GNUNET_CRYPTO_CsBlindS *blinded_signature_scalar,
   3932   const struct GNUNET_CRYPTO_CsBlindingSecret *bs,
   3933   struct GNUNET_CRYPTO_CsS *signature_scalar);
   3934 
   3935 
   3936 /**
   3937  * Verify whether the given message corresponds to the given signature and the
   3938  * signature is valid with respect to the given public key.
   3939  *
   3940  * @param sig signature that is being validated
   3941  * @param pub public key of the signer
   3942  * @param msg is the message that should be signed by @a sig  (message is used to calculate c)
   3943  * @param msg_len is the message length
   3944  * @returns #GNUNET_YES on success, #GNUNET_SYSERR if signature invalid
   3945  */
   3946 enum GNUNET_GenericReturnValue
   3947 GNUNET_CRYPTO_cs_verify (
   3948   const struct GNUNET_CRYPTO_CsSignature *sig,
   3949   const struct GNUNET_CRYPTO_CsPublicKey *pub,
   3950   const void *msg,
   3951   size_t msg_len);
   3952 
   3953 
   3954 /**
   3955  * Types of public keys used for blind signatures.
   3956  */
   3957 enum GNUNET_CRYPTO_BlindSignatureAlgorithm
   3958 {
   3959 
   3960   /**
   3961    * Invalid type of signature.
   3962    */
   3963   GNUNET_CRYPTO_BSA_INVALID = 0,
   3964 
   3965   /**
   3966    * RSA blind signature.
   3967    */
   3968   GNUNET_CRYPTO_BSA_RSA = 1,
   3969 
   3970   /**
   3971    * Clause Blind Schnorr signature.
   3972    */
   3973   GNUNET_CRYPTO_BSA_CS = 2
   3974 };
   3975 
   3976 
   3977 /**
   3978  * @brief Type of (unblinded) signatures.
   3979  */
   3980 struct GNUNET_CRYPTO_UnblindedSignature
   3981 {
   3982 
   3983   /**
   3984    * Type of the signature.
   3985    */
   3986   enum GNUNET_CRYPTO_BlindSignatureAlgorithm cipher;
   3987 
   3988   /**
   3989    * Reference counter.
   3990    */
   3991   unsigned int rc;
   3992 
   3993   /**
   3994    * Details, depending on @e cipher.
   3995    */
   3996   union
   3997   {
   3998     /**
   3999      * If we use #GNUNET_CRYPTO_BSA_CS in @a cipher.
   4000      */
   4001     struct GNUNET_CRYPTO_CsSignature cs_signature;
   4002 
   4003     /**
   4004      * If we use #GNUNET_CRYPTO_BSA_RSA in @a cipher.
   4005      */
   4006     struct GNUNET_CRYPTO_RsaSignature *rsa_signature;
   4007 
   4008   } details;
   4009 
   4010 };
   4011 
   4012 
   4013 /**
   4014  * @brief Type for *blinded* signatures.
   4015  * Must be unblinded before it becomes valid.
   4016  */
   4017 struct GNUNET_CRYPTO_BlindedSignature
   4018 {
   4019 
   4020   /**
   4021    * Type of the signature.
   4022    */
   4023   enum GNUNET_CRYPTO_BlindSignatureAlgorithm cipher;
   4024 
   4025   /**
   4026    * Reference counter.
   4027    */
   4028   unsigned int rc;
   4029 
   4030   /**
   4031    * Details, depending on @e cipher.
   4032    */
   4033   union
   4034   {
   4035     /**
   4036      * If we use #GNUNET_CRYPTO_BSA_CS in @a cipher.
   4037      * At this point only the blinded s scalar is used.
   4038      * The final signature consisting of r,s is built after unblinding.
   4039      */
   4040     struct GNUNET_CRYPTO_CsBlindSignature blinded_cs_answer;
   4041 
   4042     /**
   4043      * If we use #GNUNET_CRYPTO_BSA_RSA in @a cipher.
   4044      */
   4045     struct GNUNET_CRYPTO_RsaSignature *blinded_rsa_signature;
   4046 
   4047   } details;
   4048 
   4049 };
   4050 
   4051 
   4052 /**
   4053  * @brief Type of public signing keys for blind signatures.
   4054  */
   4055 struct GNUNET_CRYPTO_BlindSignPublicKey
   4056 {
   4057 
   4058   /**
   4059    * Type of the public key.
   4060    */
   4061   enum GNUNET_CRYPTO_BlindSignatureAlgorithm cipher;
   4062 
   4063   /**
   4064    * Reference counter.
   4065    */
   4066   unsigned int rc;
   4067 
   4068   /**
   4069    * Hash of the public key.
   4070    */
   4071   struct GNUNET_HashCode pub_key_hash;
   4072 
   4073   /**
   4074    * Details, depending on @e cipher.
   4075    */
   4076   union
   4077   {
   4078     /**
   4079      * If we use #GNUNET_CRYPTO_BSA_CS in @a cipher.
   4080      */
   4081     struct GNUNET_CRYPTO_CsPublicKey cs_public_key;
   4082 
   4083     /**
   4084      * If we use #GNUNET_CRYPTO_BSA_RSA in @a cipher.
   4085      */
   4086     struct GNUNET_CRYPTO_RsaPublicKey *rsa_public_key;
   4087 
   4088   } details;
   4089 };
   4090 
   4091 
   4092 /**
   4093  * @brief Type of private signing keys for blind signing.
   4094  */
   4095 struct GNUNET_CRYPTO_BlindSignPrivateKey
   4096 {
   4097 
   4098   /**
   4099    * Type of the public key.
   4100    */
   4101   enum GNUNET_CRYPTO_BlindSignatureAlgorithm cipher;
   4102 
   4103   /**
   4104    * Reference counter.
   4105    */
   4106   unsigned int rc;
   4107 
   4108   /**
   4109    * Details, depending on @e cipher.
   4110    */
   4111   union
   4112   {
   4113     /**
   4114      * If we use #GNUNET_CRYPTO_BSA_CS in @a cipher.
   4115      */
   4116     struct GNUNET_CRYPTO_CsPrivateKey cs_private_key;
   4117 
   4118     /**
   4119      * If we use #GNUNET_CRYPTO_BSA_RSA in @a cipher.
   4120      */
   4121     struct GNUNET_CRYPTO_RsaPrivateKey *rsa_private_key;
   4122 
   4123   } details;
   4124 };
   4125 
   4126 
   4127 /**
   4128  * @brief Blinded message ready for blind signing.
   4129  */
   4130 struct GNUNET_CRYPTO_BlindedMessage
   4131 {
   4132   /**
   4133    * Type of the sign blinded message
   4134    */
   4135   enum GNUNET_CRYPTO_BlindSignatureAlgorithm cipher;
   4136 
   4137   /**
   4138    * Reference counter.
   4139    */
   4140   unsigned int rc;
   4141 
   4142   /**
   4143    * Details, depending on @e cipher.
   4144    */
   4145   union
   4146   {
   4147     /**
   4148      * If we use #GNUNET_CRYPTO_BSA_CS in @a cipher.
   4149      */
   4150     struct GNUNET_CRYPTO_CsBlindedMessage cs_blinded_message;
   4151 
   4152     /**
   4153      * If we use #GNUNET_CRYPTO_BSA_RSA in @a cipher.
   4154      */
   4155     struct GNUNET_CRYPTO_RsaBlindedMessage rsa_blinded_message;
   4156 
   4157   } details;
   4158 };
   4159 
   4160 
   4161 /**
   4162  * Secret r for Cs denominations
   4163  */
   4164 struct GNUNET_CRYPTO_CSPrivateRPairP
   4165 {
   4166   struct GNUNET_CRYPTO_CsRSecret r[2];
   4167 };
   4168 
   4169 
   4170 /**
   4171  * @brief Input needed for blinding a message.
   4172  */
   4173 struct GNUNET_CRYPTO_BlindingInputValues
   4174 {
   4175 
   4176   /**
   4177    * Type of the signature.
   4178    */
   4179   enum GNUNET_CRYPTO_BlindSignatureAlgorithm cipher;
   4180 
   4181   /**
   4182    * Reference counter.
   4183    */
   4184   unsigned int rc;
   4185 
   4186   /**
   4187    * Details, depending on @e cipher.
   4188    */
   4189   union
   4190   {
   4191     /**
   4192      * If we use #GNUNET_CRYPTO_BSA_CS in @a cipher.
   4193      */
   4194     struct GNUNET_CRYPTO_CSPublicRPairP cs_values;
   4195 
   4196   } details;
   4197 
   4198 };
   4199 
   4200 
   4201 /**
   4202  * Nonce used to deterministiacally derive input values
   4203  * used in multi-round blind signature protocols.
   4204  */
   4205 union GNUNET_CRYPTO_BlindSessionNonce
   4206 {
   4207   /**
   4208    * Nonce used when signing with CS.
   4209    */
   4210   struct GNUNET_CRYPTO_CsSessionNonce cs_nonce;
   4211 };
   4212 
   4213 
   4214 /**
   4215  * Compute blinding input values for a given @a nonce and
   4216  * @a salt.
   4217  *
   4218  * @param bsign_priv private key to compute input values for
   4219  * @param nonce session nonce to derive input values from
   4220  * @param salt salt to include in derivation logic
   4221  * @return blinding input values
   4222  */
   4223 struct GNUNET_CRYPTO_BlindingInputValues *
   4224 GNUNET_CRYPTO_get_blinding_input_values (
   4225   const struct GNUNET_CRYPTO_BlindSignPrivateKey *bsign_priv,
   4226   const union GNUNET_CRYPTO_BlindSessionNonce *nonce,
   4227   const char *salt);
   4228 
   4229 
   4230 /**
   4231  * Decrement reference counter of a @a bsign_pub, and free it if it reaches zero.
   4232  *
   4233  * @param[in] bsign_pub key to free
   4234  */
   4235 void
   4236 GNUNET_CRYPTO_blind_sign_pub_decref (
   4237   struct GNUNET_CRYPTO_BlindSignPublicKey *bsign_pub);
   4238 
   4239 
   4240 /**
   4241  * Decrement reference counter of a @a bsign_priv, and free it if it reaches zero.
   4242  *
   4243  * @param[in] bsign_priv key to free
   4244  */
   4245 void
   4246 GNUNET_CRYPTO_blind_sign_priv_decref (
   4247   struct GNUNET_CRYPTO_BlindSignPrivateKey *bsign_priv);
   4248 
   4249 
   4250 /**
   4251  * Decrement reference counter of a @a ub_sig, and free it if it reaches zero.
   4252  *
   4253  * @param[in] ub_sig signature to free
   4254  */
   4255 void
   4256 GNUNET_CRYPTO_unblinded_sig_decref (
   4257   struct GNUNET_CRYPTO_UnblindedSignature *ub_sig);
   4258 
   4259 
   4260 /**
   4261  * Decrement reference counter of a @a blind_sig, and free it if it reaches zero.
   4262  *
   4263  * @param[in] blind_sig signature to free
   4264  */
   4265 void
   4266 GNUNET_CRYPTO_blinded_sig_decref (
   4267   struct GNUNET_CRYPTO_BlindedSignature *blind_sig);
   4268 
   4269 
   4270 /**
   4271  * Decrement reference counter of a @a bm, and free it if it reaches zero.
   4272  *
   4273  * @param[in] bm blinded message to free
   4274  */
   4275 void
   4276 GNUNET_CRYPTO_blinded_message_decref (
   4277   struct GNUNET_CRYPTO_BlindedMessage *bm);
   4278 
   4279 
   4280 /**
   4281  * Increment reference counter of the given @a bm.
   4282  *
   4283  * @param[in,out] bm blinded message to increment reference counter for
   4284  * @return alias of @a bm with RC incremented
   4285  */
   4286 struct GNUNET_CRYPTO_BlindedMessage *
   4287 GNUNET_CRYPTO_blinded_message_incref (
   4288   struct GNUNET_CRYPTO_BlindedMessage *bm);
   4289 
   4290 
   4291 /**
   4292  * Increment reference counter of the given @a bi.
   4293  *
   4294  * @param[in,out] bi blinding input values to increment reference counter for
   4295  * @return alias of @a bi with RC incremented
   4296  */
   4297 struct GNUNET_CRYPTO_BlindingInputValues *
   4298 GNUNET_CRYPTO_blinding_input_values_incref (
   4299   struct GNUNET_CRYPTO_BlindingInputValues *bm);
   4300 
   4301 
   4302 /**
   4303  * Decrement reference counter of the given @a bi, and free it if it reaches
   4304  * zero.
   4305  *
   4306  * @param[in,out] bi blinding input values to decrement reference counter for
   4307  */
   4308 void
   4309 GNUNET_CRYPTO_blinding_input_values_decref (
   4310   struct GNUNET_CRYPTO_BlindingInputValues *bm);
   4311 
   4312 
   4313 /**
   4314  * Increment reference counter of the given @a bsign_pub.
   4315  *
   4316  * @param[in,out] bsign_pub public key to increment reference counter for
   4317  * @return alias of @a bsign_pub with RC incremented
   4318  */
   4319 struct GNUNET_CRYPTO_BlindSignPublicKey *
   4320 GNUNET_CRYPTO_bsign_pub_incref (
   4321   struct GNUNET_CRYPTO_BlindSignPublicKey *bsign_pub);
   4322 
   4323 
   4324 /**
   4325  * Increment reference counter of the given @a bsign_priv.
   4326  *
   4327  * @param[in,out] bsign_priv private key to increment reference counter for
   4328  * @return alias of @a bsign_priv with RC incremented
   4329  */
   4330 struct GNUNET_CRYPTO_BlindSignPrivateKey *
   4331 GNUNET_CRYPTO_bsign_priv_incref (
   4332   struct GNUNET_CRYPTO_BlindSignPrivateKey *bsign_priv);
   4333 
   4334 
   4335 /**
   4336  * Increment reference counter of the given @a ub_sig.
   4337  *
   4338  * @param[in,out] ub_sig signature to increment reference counter for
   4339  * @return alias of @a ub_sig with RC incremented
   4340  */
   4341 struct GNUNET_CRYPTO_UnblindedSignature *
   4342 GNUNET_CRYPTO_ub_sig_incref (struct GNUNET_CRYPTO_UnblindedSignature *ub_sig);
   4343 
   4344 
   4345 /**
   4346  * Increment reference counter of the given @a blind_sig.
   4347  *
   4348  * @param[in,out] blind_sig signature to increment reference counter for
   4349  * @return alias of @a blind_sig with RC incremented
   4350  */
   4351 struct GNUNET_CRYPTO_BlindedSignature *
   4352 GNUNET_CRYPTO_blind_sig_incref (
   4353   struct GNUNET_CRYPTO_BlindedSignature *blind_sig);
   4354 
   4355 
   4356 /**
   4357  * Compare two denomination public keys.
   4358  *
   4359  * @param bp1 first key
   4360  * @param bp2 second key
   4361  * @return 0 if the keys are equal, otherwise -1 or 1
   4362  */
   4363 int
   4364 GNUNET_CRYPTO_bsign_pub_cmp (
   4365   const struct GNUNET_CRYPTO_BlindSignPublicKey *bp1,
   4366   const struct GNUNET_CRYPTO_BlindSignPublicKey *bp2);
   4367 
   4368 
   4369 /**
   4370  * Compare two denomination signatures.
   4371  *
   4372  * @param sig1 first signature
   4373  * @param sig2 second signature
   4374  * @return 0 if the keys are equal, otherwise -1 or 1
   4375  */
   4376 int
   4377 GNUNET_CRYPTO_ub_sig_cmp (const struct GNUNET_CRYPTO_UnblindedSignature *sig1,
   4378                           const struct GNUNET_CRYPTO_UnblindedSignature *sig2);
   4379 
   4380 
   4381 /**
   4382  * Compare two blinded denomination signatures.
   4383  *
   4384  * @param sig1 first signature
   4385  * @param sig2 second signature
   4386  * @return 0 if the keys are equal, otherwise -1 or 1
   4387  */
   4388 int
   4389 GNUNET_CRYPTO_blind_sig_cmp (
   4390   const struct GNUNET_CRYPTO_BlindedSignature *sig1,
   4391   const struct GNUNET_CRYPTO_BlindedSignature *sig2);
   4392 
   4393 
   4394 /**
   4395  * Compare two blinded messages.
   4396  *
   4397  * @param bp1 first blinded message
   4398  * @param bp2 second blinded message
   4399  * @return 0 if the keys are equal, otherwise -1 or 1
   4400  */
   4401 int
   4402 GNUNET_CRYPTO_blinded_message_cmp (
   4403   const struct GNUNET_CRYPTO_BlindedMessage *bp1,
   4404   const struct GNUNET_CRYPTO_BlindedMessage *bp2);
   4405 
   4406 
   4407 /**
   4408  * Initialize public-private key pair for blind signatures.
   4409  *
   4410  * For #GNUNET_CRYPTO_BSA_RSA, an additional "unsigned int"
   4411  * argument with the number of bits for 'n' (e.g. 2048) must
   4412  * be passed.
   4413  *
   4414  * @param[out] bsign_priv where to write the private key with RC 1
   4415  * @param[out] bsign_pub where to write the public key with RC 1
   4416  * @param cipher which type of cipher to use
   4417  * @param ... RSA key size (eg. 2048/3072/4096)
   4418  * @return #GNUNET_OK on success, #GNUNET_NO if parameterst were invalid
   4419  */
   4420 enum GNUNET_GenericReturnValue
   4421 GNUNET_CRYPTO_blind_sign_keys_create (
   4422   struct GNUNET_CRYPTO_BlindSignPrivateKey **bsign_priv,
   4423   struct GNUNET_CRYPTO_BlindSignPublicKey **bsign_pub,
   4424   enum GNUNET_CRYPTO_BlindSignatureAlgorithm cipher,
   4425   ...);
   4426 
   4427 
   4428 /**
   4429  * Initialize public-private key pair for blind signatures.
   4430  *
   4431  * For #GNUNET_CRYPTO_BSA_RSA, an additional "unsigned int"
   4432  * argument with the number of bits for 'n' (e.g. 2048) must
   4433  * be passed.
   4434  *
   4435  * @param[out] bsign_priv where to write the private key with RC 1
   4436  * @param[out] bsign_pub where to write the public key with RC 1
   4437  * @param cipher which type of cipher to use
   4438  * @param ap RSA key size (eg. 2048/3072/4096)
   4439  * @return #GNUNET_OK on success, #GNUNET_NO if parameterst were invalid
   4440  */
   4441 enum GNUNET_GenericReturnValue
   4442 GNUNET_CRYPTO_blind_sign_keys_create_va (
   4443   struct GNUNET_CRYPTO_BlindSignPrivateKey **bsign_priv,
   4444   struct GNUNET_CRYPTO_BlindSignPublicKey **bsign_pub,
   4445   enum GNUNET_CRYPTO_BlindSignatureAlgorithm cipher,
   4446   va_list ap);
   4447 
   4448 
   4449 /**
   4450  * @brief Type of blinding secrets.  Must be exactly 32 bytes (DB).
   4451  */
   4452 union GNUNET_CRYPTO_BlindingSecretP
   4453 {
   4454   /**
   4455    * Clause Schnorr nonce.
   4456    */
   4457   struct GNUNET_CRYPTO_CsBlindingNonce nonce;
   4458 
   4459   /**
   4460    * Variant for RSA for blind signatures.
   4461    */
   4462   struct GNUNET_CRYPTO_RsaBlindingKeySecret rsa_bks;
   4463 };
   4464 
   4465 
   4466 /**
   4467  * Blind message for blind signing with @a dk using blinding secret @a coin_bks.
   4468  *
   4469  * @param bsign_pub public key to blind for
   4470  * @param bks blinding secret to use
   4471  * @param nonce nonce used to obtain @a alg_values
   4472  *        can be NULL if input values are not used for the cipher
   4473  * @param message message to sign
   4474  * @param message_size number of bytes in @a message
   4475  * @param alg_values algorithm specific values to blind the @a message
   4476  * @return blinded message to give to signer, NULL on error
   4477  */
   4478 struct GNUNET_CRYPTO_BlindedMessage *
   4479 GNUNET_CRYPTO_message_blind_to_sign (
   4480   const struct GNUNET_CRYPTO_BlindSignPublicKey *bsign_pub,
   4481   const union GNUNET_CRYPTO_BlindingSecretP *bks,
   4482   const union GNUNET_CRYPTO_BlindSessionNonce *nonce,
   4483   const void *message,
   4484   size_t message_size,
   4485   const struct GNUNET_CRYPTO_BlindingInputValues *alg_values);
   4486 
   4487 
   4488 /**
   4489  * Create blind signature.
   4490  *
   4491  * @param bsign_priv private key to use for signing
   4492  * @param salt salt value to use for the HKDF,
   4493  *        can be NULL if input values are not used for the cipher
   4494  * @param blinded_message the already blinded message to sign
   4495  * @return blind signature with RC=1, NULL on failure
   4496  */
   4497 struct GNUNET_CRYPTO_BlindedSignature *
   4498 GNUNET_CRYPTO_blind_sign (
   4499   const struct GNUNET_CRYPTO_BlindSignPrivateKey *bsign_priv,
   4500   const char *salt,
   4501   const struct GNUNET_CRYPTO_BlindedMessage *blinded_message);
   4502 
   4503 
   4504 /**
   4505  * Unblind blind signature.
   4506  *
   4507  * @param blinded_sig the blind signature
   4508  * @param bks blinding secret to use
   4509  * @param message message that was supposedly signed
   4510  * @param message_size number of bytes in @a message
   4511  * @param alg_values algorithm specific values
   4512  * @param bsign_pub public key used for signing
   4513  * @return unblinded signature with RC=1, NULL on error
   4514  */
   4515 struct GNUNET_CRYPTO_UnblindedSignature *
   4516 GNUNET_CRYPTO_blind_sig_unblind (
   4517   const struct GNUNET_CRYPTO_BlindedSignature *blinded_sig,
   4518   const union GNUNET_CRYPTO_BlindingSecretP *bks,
   4519   const void *message,
   4520   size_t message_size,
   4521   const struct GNUNET_CRYPTO_BlindingInputValues *alg_values,
   4522   const struct GNUNET_CRYPTO_BlindSignPublicKey *bsign_pub);
   4523 
   4524 
   4525 /**
   4526  * Verify signature made blindly.
   4527  *
   4528  * @param bsign_pub public key
   4529  * @param ub_sig signature made blindly with the private key
   4530  * @param message message that was supposedly signed
   4531  * @param message_size number of bytes in @a message
   4532  * @return #GNUNET_OK if the signature is valid
   4533  */
   4534 enum GNUNET_GenericReturnValue
   4535 GNUNET_CRYPTO_blind_sig_verify (
   4536   const struct GNUNET_CRYPTO_BlindSignPublicKey *bsign_pub,
   4537   const struct GNUNET_CRYPTO_UnblindedSignature *ub_sig,
   4538   const void *message,
   4539   size_t message_size);
   4540 
   4541 
   4542 /**
   4543  * Get the compacted length of a #GNUNET_CRYPTO_BlindablePublicKey.
   4544  * Compacted means that it returns the minimum number of bytes this
   4545  * key is long, as opposed to the union structure inside
   4546  * #GNUNET_CRYPTO_BlindablePublicKey.
   4547  * Useful for compact serializations.
   4548  *
   4549  * @param key the key.
   4550  * @return -1 on error, else the compacted length of the key.
   4551  */
   4552 ssize_t
   4553 GNUNET_CRYPTO_public_key_get_length (const struct
   4554                                      GNUNET_CRYPTO_BlindablePublicKey *key);
   4555 
   4556 /**
   4557  * Reads a #GNUNET_CRYPTO_BlindablePublicKey from a compact buffer.
   4558  * The buffer has to contain at least the compacted length of
   4559  * a #GNUNET_CRYPTO_BlindablePublicKey in bytes.
   4560  * If the buffer is too small, the function returns -1 as error.
   4561  * If the buffer does not contain a valid key, it returns -2 as error.
   4562  *
   4563  * @param buffer the buffer
   4564  * @param len the length of buffer
   4565  * @param key the key
   4566  * @param the amount of bytes read from the buffer
   4567  * @return #GNUNET_SYSERR on error
   4568  */
   4569 enum GNUNET_GenericReturnValue
   4570 GNUNET_CRYPTO_read_public_key_from_buffer (
   4571   const void *buffer,
   4572   size_t len,
   4573   struct GNUNET_CRYPTO_BlindablePublicKey *key,
   4574   size_t *read);
   4575 
   4576 /**
   4577  * Get the compacted length of a #GNUNET_CRYPTO_BlindablePrivateKey.
   4578  * Compacted means that it returns the minimum number of bytes this
   4579  * key is long, as opposed to the union structure inside
   4580  * #GNUNET_CRYPTO_BlindablePrivateKey.
   4581  * Useful for compact serializations.
   4582  *
   4583  * @param key the key.
   4584  * @return -1 on error, else the compacted length of the key.
   4585  */
   4586 ssize_t
   4587 GNUNET_CRYPTO_blindable_sk_get_length (
   4588   const struct GNUNET_CRYPTO_BlindablePrivateKey *key);
   4589 
   4590 
   4591 /**
   4592  * Writes a #GNUNET_CRYPTO_BlindablePublicKey to a compact buffer.
   4593  * The buffer requires space for at least the compacted length of
   4594  * a #GNUNET_CRYPTO_BlindablePublicKey in bytes.
   4595  * If the buffer is too small, the function returns -1 as error.
   4596  * If the key is not valid, it returns -2 as error.
   4597  *
   4598  * @param key the key
   4599  * @param buffer the buffer
   4600  * @param len the length of buffer
   4601  * @return -1 or -2 on error, else the amount of bytes written to the buffer
   4602  */
   4603 ssize_t
   4604 GNUNET_CRYPTO_write_blindable_pk_to_buffer (const struct
   4605                                             GNUNET_CRYPTO_BlindablePublicKey *
   4606                                             key,
   4607                                             void*buffer,
   4608                                             size_t len);
   4609 
   4610 
   4611 /**
   4612  * Reads a #GNUNET_CRYPTO_BlindablePrivateKey from a compact buffer.
   4613  * The buffer has to contain at least the compacted length of
   4614  * a #GNUNET_CRYPTO_BlindablePrivateKey in bytes.
   4615  * If the buffer is too small, the function returns GNUNET_SYSERR as error.
   4616  *
   4617  * @param buffer the buffer
   4618  * @param len the length of buffer
   4619  * @param key the key
   4620  * @param the amount of bytes read from the buffer
   4621  * @return #GNUNET_SYSERR on error
   4622  */
   4623 enum GNUNET_GenericReturnValue
   4624 GNUNET_CRYPTO_read_private_key_from_buffer (
   4625   const void*buffer,
   4626   size_t len,
   4627   struct GNUNET_CRYPTO_BlindablePrivateKey *key,
   4628   size_t *read);
   4629 
   4630 
   4631 /**
   4632  * Writes a #GNUNET_CRYPTO_BlindablePrivateKey to a compact buffer.
   4633  * The buffer requires space for at least the compacted length of
   4634  * a #GNUNET_CRYPTO_BlindablePrivateKey in bytes.
   4635  * If the buffer is too small, the function returns -1 as error.
   4636  * If the key is not valid, it returns -2 as error.
   4637  *
   4638  * @param key the key
   4639  * @param buffer the buffer
   4640  * @param len the length of buffer
   4641  * @return -1 or -2 on error, else the amount of bytes written to the buffer
   4642  */
   4643 ssize_t
   4644 GNUNET_CRYPTO_write_blindable_sk_to_buffer (
   4645   const struct GNUNET_CRYPTO_BlindablePrivateKey *key,
   4646   void*buffer,
   4647   size_t len);
   4648 
   4649 
   4650 /**
   4651  * Get the compacted length of a #GNUNET_CRYPTO_Signature.
   4652  * Compacted means that it returns the minimum number of bytes this
   4653  * signature is long, as opposed to the union structure inside
   4654  * #GNUNET_CRYPTO_Signature.
   4655  * Useful for compact serializations.
   4656  *
   4657  * @param sig the signature.
   4658  * @return -1 on error, else the compacted length of the signature.
   4659  */
   4660 ssize_t
   4661 GNUNET_CRYPTO_blinded_key_signature_get_length (
   4662   const struct GNUNET_CRYPTO_BlindableKeySignature *sig);
   4663 
   4664 
   4665 /**
   4666  * Get the compacted length of a signature by type.
   4667  * Compacted means that it returns the minimum number of bytes this
   4668  * signature is long, as opposed to the union structure inside
   4669  * #GNUNET_CRYPTO_Signature.
   4670  * Useful for compact serializations.
   4671  *
   4672  * @param sig the signature.
   4673  * @return -1 on error, else the compacted length of the signature.
   4674  */
   4675 ssize_t
   4676 GNUNET_CRYPTO_blinded_key_signature_get_length_by_type (uint32_t type);
   4677 
   4678 
   4679 /**
   4680  * Reads a #GNUNET_CRYPTO_BlindableKeySignature from a compact buffer.
   4681  * The buffer has to contain at least the compacted length of
   4682  * a #GNUNET_CRYPTO_BlindableKeySignature in bytes.
   4683  * If the buffer is too small, the function returns -1 as error.
   4684  * If the buffer does not contain a valid key, it returns -2 as error.
   4685  *
   4686  * @param sig the signature
   4687  * @param buffer the buffer
   4688  * @param len the length of buffer
   4689  * @return -1 or -2 on error, else the amount of bytes read from the buffer
   4690  */
   4691 ssize_t
   4692 GNUNET_CRYPTO_read_blinded_key_signature_from_buffer (
   4693   struct GNUNET_CRYPTO_BlindableKeySignature *sig,
   4694   const void*buffer,
   4695   size_t len);
   4696 
   4697 
   4698 /**
   4699  * Writes a #GNUNET_CRYPTO_BlindableKeySignature to a compact buffer.
   4700  * The buffer requires space for at least the compacted length of
   4701  * a #GNUNET_CRYPTO_BlindableKeySignature in bytes.
   4702  * If the buffer is too small, the function returns -1 as error.
   4703  * If the key is not valid, it returns -2 as error.
   4704  *
   4705  * @param sig the signature
   4706  * @param buffer the buffer
   4707  * @param len the length of buffer
   4708  * @return -1 or -2 on error, else the amount of bytes written to the buffer
   4709  */
   4710 ssize_t
   4711 GNUNET_CRYPTO_write_blinded_key_signature_to_buffer (
   4712   const struct GNUNET_CRYPTO_BlindableKeySignature *sig,
   4713   void*buffer,
   4714   size_t len);
   4715 
   4716 
   4717 /**
   4718  * @brief Sign a given block.
   4719  *
   4720  * The @a purpose data is the beginning of the data of which the signature is
   4721  * to be created. The `size` field in @a purpose must correctly indicate the
   4722  * number of bytes of the data structure, including its header. If possible,
   4723  * use #GNUNET_CRYPTO_blinded_key_sign() instead of this function.
   4724  *
   4725  * @param priv private key to use for the signing
   4726  * @param purpose what to sign (size, purpose)
   4727  * @param[out] sig where to write the signature
   4728  * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
   4729  */
   4730 enum GNUNET_GenericReturnValue
   4731 GNUNET_CRYPTO_blinded_key_sign_ (
   4732   const struct GNUNET_CRYPTO_BlindablePrivateKey *priv,
   4733   const struct GNUNET_CRYPTO_SignaturePurpose *purpose,
   4734   struct GNUNET_CRYPTO_BlindableKeySignature *sig);
   4735 
   4736 /**
   4737  * @brief Sign a given block.
   4738  *
   4739  * The @a purpose data is the beginning of the data of which the signature is
   4740  * to be created. The `size` field in @a purpose must correctly indicate the
   4741  * number of bytes of the data structure, including its header.
   4742  * The signature payload and length depends on the key type.
   4743  *
   4744  * @param priv private key to use for the signing
   4745  * @param purpose what to sign (size, purpose)
   4746  * @param[out] sig where to write the signature
   4747  * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
   4748  */
   4749 enum GNUNET_GenericReturnValue
   4750 GNUNET_CRYPTO_blinded_key_sign_raw_ (
   4751   const struct GNUNET_CRYPTO_BlindablePrivateKey *priv,
   4752   const struct GNUNET_CRYPTO_SignaturePurpose *purpose,
   4753   unsigned char *sig);
   4754 
   4755 
   4756 /**
   4757  * @brief Sign a given block with #GNUNET_CRYPTO_BlindablePrivateKey.
   4758  *
   4759  * The @a ps data must be a fixed-size struct for which the signature is to be
   4760  * created. The `size` field in @a ps->purpose must correctly indicate the
   4761  * number of bytes of the data structure, including its header.
   4762  *
   4763  * @param priv private key to use for the signing
   4764  * @param ps packed struct with what to sign, MUST begin with a purpose
   4765  * @param[out] sig where to write the signature
   4766  */
   4767 #define GNUNET_CRYPTO_blinded_key_sign(priv,ps,sig) do {                \
   4768           /* check size is set correctly */                                     \
   4769           GNUNET_assert (ntohl ((ps)->purpose.size) == sizeof (*(ps)));         \
   4770           /* check 'ps' begins with the purpose */                              \
   4771           GNUNET_static_assert (((void*) (ps)) ==                               \
   4772                                 ((void*) &(ps)->purpose));                      \
   4773           GNUNET_assert (GNUNET_OK ==                                           \
   4774                          GNUNET_CRYPTO_blinded_key_sign_ (priv,               \
   4775                                                           &(ps)->purpose,             \
   4776                                                           sig));                      \
   4777 } while (0)
   4778 
   4779 
   4780 /**
   4781  * @brief Verify a given signature.
   4782  *
   4783  * The @a validate data is the beginning of the data of which the signature
   4784  * is to be verified. The `size` field in @a validate must correctly indicate
   4785  * the number of bytes of the data structure, including its header.  If @a
   4786  * purpose does not match the purpose given in @a validate (the latter must be
   4787  * in big endian), signature verification fails.  If possible,
   4788  * use #GNUNET_CRYPTO_blinded_key_signature_verify() instead of this function (only if @a validate
   4789  * is not fixed-size, you must use this function directly).
   4790  *
   4791  * @param purpose what is the purpose that the signature should have?
   4792  * @param validate block to validate (size, purpose, data)
   4793  * @param sig signature that is being validated
   4794  * @param pub public key of the signer
   4795  * @returns #GNUNET_OK if ok, #GNUNET_SYSERR if invalid
   4796  */
   4797 enum GNUNET_GenericReturnValue
   4798 GNUNET_CRYPTO_blinded_key_signature_verify_ (
   4799   uint32_t purpose,
   4800   const struct GNUNET_CRYPTO_SignaturePurpose *validate,
   4801   const struct GNUNET_CRYPTO_BlindableKeySignature *sig,
   4802   const struct GNUNET_CRYPTO_BlindablePublicKey *pub);
   4803 
   4804 /**
   4805  * @brief Verify a given signature.
   4806  *
   4807  * The @a validate data is the beginning of the data of which the signature
   4808  * is to be verified. The `size` field in @a validate must correctly indicate
   4809  * the number of bytes of the data structure, including its header.  If @a
   4810  * purpose does not match the purpose given in @a validate (the latter must be
   4811  * in big endian), signature verification fails.
   4812  *
   4813  * @param purpose what is the purpose that the signature should have?
   4814  * @param validate block to validate (size, purpose, data)
   4815  * @param sig signature that is being validated
   4816  * @param pub public key of the signer
   4817  * @returns #GNUNET_OK if ok, #GNUNET_SYSERR if invalid
   4818  */
   4819 enum GNUNET_GenericReturnValue
   4820 GNUNET_CRYPTO_blinded_key_signature_verify_raw_ (
   4821   uint32_t purpose,
   4822   const struct GNUNET_CRYPTO_SignaturePurpose *validate,
   4823   const unsigned char *sig,
   4824   const struct GNUNET_CRYPTO_BlindablePublicKey *pub);
   4825 
   4826 
   4827 /**
   4828  * @brief Verify a given signature with #GNUNET_CRYPTO_BlindablePublicKey.
   4829  *
   4830  * The @a ps data must be a fixed-size struct for which the signature is to be
   4831  * created. The `size` field in @a ps->purpose must correctly indicate the
   4832  * number of bytes of the data structure, including its header.
   4833  *
   4834  * @param purp purpose of the signature, must match 'ps->purpose.purpose'
   4835  *              (except in host byte order)
   4836  * @param ps packed struct with what to sign, MUST begin with a purpose
   4837  * @param sig where to read the signature from
   4838  * @param pub public key to use for the verifying
   4839  */
   4840 #define GNUNET_CRYPTO_blinded_key_signature_verify(purp,ps,sig,pub) ({             \
   4841     /* check size is set correctly */                                     \
   4842     GNUNET_assert (ntohl ((ps)->purpose.size) == sizeof (*(ps)));         \
   4843     /* check 'ps' begins with the purpose */                              \
   4844     GNUNET_static_assert (((void*) (ps)) ==                               \
   4845                           ((void*) &(ps)->purpose));                      \
   4846     GNUNET_CRYPTO_blinded_key_signature_verify_ (purp,                              \
   4847                                                  &(ps)->purpose,                    \
   4848                                                  sig,                               \
   4849                                                  pub);                              \
   4850   })
   4851 
   4852 
   4853 /**
   4854  * Creates a (Base32) string representation of the public key.
   4855  * The resulting string encodes a compacted representation of the key.
   4856  * See also #GNUNET_CRYPTO_key_get_length.
   4857  *
   4858  * @param key the key.
   4859  * @return the string representation of the key, or NULL on error.
   4860  */
   4861 char *
   4862 GNUNET_CRYPTO_blindable_public_key_to_string (
   4863   const struct GNUNET_CRYPTO_BlindablePublicKey *key);
   4864 
   4865 
   4866 /**
   4867  * Creates a (Base32) string representation of the private key.
   4868  * The resulting string encodes a compacted representation of the key.
   4869  * See also #GNUNET_CRYPTO_key_get_length.
   4870  *
   4871  * @param key the key.
   4872  * @return the string representation of the key, or NULL on error.
   4873  */
   4874 char *
   4875 GNUNET_CRYPTO_blindable_private_key_to_string (
   4876   const struct GNUNET_CRYPTO_BlindablePrivateKey *key);
   4877 
   4878 
   4879 /**
   4880  * Parses a (Base32) string representation of the public key.
   4881  * See also #GNUNET_CRYPTO_blindable_public_key_to_string.
   4882  *
   4883  * @param str the encoded key.
   4884  * @param key where to write the key.
   4885  * @return GNUNET_SYSERR on error.
   4886  */
   4887 enum GNUNET_GenericReturnValue
   4888 GNUNET_CRYPTO_blindable_public_key_from_string (const char*str,
   4889                                                 struct
   4890                                                 GNUNET_CRYPTO_BlindablePublicKey
   4891                                                 *key);
   4892 
   4893 
   4894 /**
   4895  * Parses a (Base32) string representation of the private key.
   4896  * See also #GNUNET_CRYPTO_blindable_private_key_to_string.
   4897  *
   4898  * @param str the encoded key.
   4899  * @param key where to write the key.
   4900  * @return GNUNET_SYSERR on error.
   4901  */
   4902 enum GNUNET_GenericReturnValue
   4903 GNUNET_CRYPTO_blindable_private_key_from_string (const char*str,
   4904                                                  struct
   4905                                                  GNUNET_CRYPTO_BlindablePrivateKey
   4906                                                  *key);
   4907 
   4908 
   4909 /**
   4910  * Retrieves the public key representation of a private key.
   4911  *
   4912  * @param privkey the private key.
   4913  * @param key the public key result.
   4914  * @return GNUNET_SYSERR on error.
   4915  */
   4916 enum GNUNET_GenericReturnValue
   4917 GNUNET_CRYPTO_blindable_key_get_public (const struct
   4918                                         GNUNET_CRYPTO_BlindablePrivateKey *
   4919                                         privkey,
   4920                                         struct GNUNET_CRYPTO_BlindablePublicKey
   4921                                         *key);
   4922 
   4923 #if 0 /* keep Emacsens' auto-indent happy */
   4924 {
   4925 #endif
   4926 #ifdef __cplusplus
   4927 }
   4928 #endif
   4929 
   4930 
   4931 /* ifndef GNUNET_CRYPTO_LIB_H */
   4932 #endif
   4933 
   4934 /** @} */ /* end of group addition */
   4935 
   4936 /* end of gnunet_crypto_lib.h */