aboutsummaryrefslogtreecommitdiff
path: root/src/include/gnunet_crypto_lib.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/include/gnunet_crypto_lib.h')
-rw-r--r--src/include/gnunet_crypto_lib.h566
1 files changed, 566 insertions, 0 deletions
diff --git a/src/include/gnunet_crypto_lib.h b/src/include/gnunet_crypto_lib.h
index 44dfb4e44..ca51f586c 100644
--- a/src/include/gnunet_crypto_lib.h
+++ b/src/include/gnunet_crypto_lib.h
@@ -348,6 +348,105 @@ struct GNUNET_CRYPTO_Edx25519Signature
348 unsigned char s[256 / 8]; 348 unsigned char s[256 / 8];
349}; 349};
350 350
351/**
352 * Key type for the generic public key union
353 */
354enum GNUNET_CRYPTO_KeyType
355{
356 /**
357 * The identity type. The value is the same as the
358 * PKEY record type.
359 */
360 GNUNET_PUBLIC_KEY_TYPE_ECDSA = 65536,
361
362 /**
363 * EDDSA identity. The value is the same as the EDKEY
364 * record type.
365 */
366 GNUNET_PUBLIC_KEY_TYPE_EDDSA = 65556
367};
368
369/**
370 * A private key for an identity as per LSD0001.
371 * Note that these types are NOT packed and MUST NOT be used in RPC
372 * messages. Use the respective serialization functions.
373 */
374struct GNUNET_CRYPTO_PrivateKey
375{
376 /**
377 * Type of public key.
378 * Defined by the GNS zone type value.
379 * In NBO.
380 */
381 uint32_t type;
382
383 union
384 {
385 /**
386 * An ECDSA identity key.
387 */
388 struct GNUNET_CRYPTO_EcdsaPrivateKey ecdsa_key;
389
390 /**
391 * AN EdDSA identtiy key
392 */
393 struct GNUNET_CRYPTO_EddsaPrivateKey eddsa_key;
394 };
395};
396
397
398/**
399 * An identity key as per LSD0001.
400 */
401struct GNUNET_CRYPTO_PublicKey
402{
403 /**
404 * Type of public key.
405 * Defined by the GNS zone type value.
406 * In NBO.
407 */
408 uint32_t type;
409
410 union
411 {
412 /**
413 * An ECDSA identity key.
414 */
415 struct GNUNET_CRYPTO_EcdsaPublicKey ecdsa_key;
416
417 /**
418 * AN EdDSA identtiy key
419 */
420 struct GNUNET_CRYPTO_EddsaPublicKey eddsa_key;
421 };
422};
423
424
425/**
426 * An identity signature as per LSD0001.
427 */
428struct GNUNET_CRYPTO_Signature
429{
430 /**
431 * Type of signature.
432 * Defined by the GNS zone type value.
433 * In NBO.
434 */
435 uint32_t type;
436
437 union
438 {
439 /**
440 * An ECDSA signature
441 */
442 struct GNUNET_CRYPTO_EcdsaSignature ecdsa_signature;
443
444 /**
445 * AN EdDSA signature
446 */
447 struct GNUNET_CRYPTO_EddsaSignature eddsa_signature;
448 };
449};
351 450
352/** 451/**
353 * @brief type for session keys 452 * @brief type for session keys
@@ -3073,6 +3172,473 @@ GNUNET_CRYPTO_cs_verify (const struct GNUNET_CRYPTO_CsSignature *sig,
3073 size_t msg_len); 3172 size_t msg_len);
3074 3173
3075 3174
3175/**
3176 * Get the compacted length of a #GNUNET_CRYPTO_PublicKey.
3177 * Compacted means that it returns the minimum number of bytes this
3178 * key is long, as opposed to the union structure inside
3179 * #GNUNET_CRYPTO_PublicKey.
3180 * Useful for compact serializations.
3181 *
3182 * @param key the key.
3183 * @return -1 on error, else the compacted length of the key.
3184 */
3185ssize_t
3186GNUNET_CRYPTO_public_key_get_length (const struct
3187 GNUNET_CRYPTO_PublicKey *key);
3188
3189/**
3190 * Reads a #GNUNET_CRYPTO_PublicKey from a compact buffer.
3191 * The buffer has to contain at least the compacted length of
3192 * a #GNUNET_CRYPTO_PublicKey in bytes.
3193 * If the buffer is too small, the function returns -1 as error.
3194 * If the buffer does not contain a valid key, it returns -2 as error.
3195 *
3196 * @param buffer the buffer
3197 * @param len the length of buffer
3198 * @param key the key
3199 * @param the amount of bytes read from the buffer
3200 * @return #GNUNET_SYSERR on error
3201 */
3202enum GNUNET_GenericReturnValue
3203GNUNET_CRYPTO_read_public_key_from_buffer (
3204 const void *buffer,
3205 size_t len,
3206 struct GNUNET_CRYPTO_PublicKey *key,
3207 size_t *read);
3208
3209/**
3210 * Get the compacted length of a #GNUNET_CRYPTO_PrivateKey.
3211 * Compacted means that it returns the minimum number of bytes this
3212 * key is long, as opposed to the union structure inside
3213 * #GNUNET_CRYPTO_PrivateKey.
3214 * Useful for compact serializations.
3215 *
3216 * @param key the key.
3217 * @return -1 on error, else the compacted length of the key.
3218 */
3219ssize_t
3220GNUNET_CRYPTO_private_key_get_length (
3221 const struct GNUNET_CRYPTO_PrivateKey *key);
3222
3223
3224/**
3225 * Writes a #GNUNET_CRYPTO_PublicKey to a compact buffer.
3226 * The buffer requires space for at least the compacted length of
3227 * a #GNUNET_CRYPTO_PublicKey in bytes.
3228 * If the buffer is too small, the function returns -1 as error.
3229 * If the key is not valid, it returns -2 as error.
3230 *
3231 * @param key the key
3232 * @param buffer the buffer
3233 * @param len the length of buffer
3234 * @return -1 or -2 on error, else the amount of bytes written to the buffer
3235 */
3236ssize_t
3237GNUNET_CRYPTO_write_public_key_to_buffer (const struct
3238 GNUNET_CRYPTO_PublicKey *key,
3239 void*buffer,
3240 size_t len);
3241
3242
3243/**
3244 * Reads a #GNUNET_CRYPTO_PrivateKey from a compact buffer.
3245 * The buffer has to contain at least the compacted length of
3246 * a #GNUNET_CRYPTO_PrivateKey in bytes.
3247 * If the buffer is too small, the function returns GNUNET_SYSERR as error.
3248 *
3249 * @param buffer the buffer
3250 * @param len the length of buffer
3251 * @param key the key
3252 * @param the amount of bytes read from the buffer
3253 * @return #GNUNET_SYSERR on error
3254 */
3255enum GNUNET_GenericReturnValue
3256GNUNET_CRYPTO_read_private_key_from_buffer (
3257 const void*buffer,
3258 size_t len,
3259 struct GNUNET_CRYPTO_PrivateKey *key,
3260 size_t *read);
3261
3262
3263/**
3264 * Writes a #GNUNET_CRYPTO_PrivateKey to a compact buffer.
3265 * The buffer requires space for at least the compacted length of
3266 * a #GNUNET_CRYPTO_PrivateKey in bytes.
3267 * If the buffer is too small, the function returns -1 as error.
3268 * If the key is not valid, it returns -2 as error.
3269 *
3270 * @param key the key
3271 * @param buffer the buffer
3272 * @param len the length of buffer
3273 * @return -1 or -2 on error, else the amount of bytes written to the buffer
3274 */
3275ssize_t
3276GNUNET_CRYPTO_write_private_key_to_buffer (
3277 const struct GNUNET_CRYPTO_PrivateKey *key,
3278 void*buffer,
3279 size_t len);
3280
3281
3282/**
3283 * Get the compacted length of a #GNUNET_CRYPTO_Signature.
3284 * Compacted means that it returns the minimum number of bytes this
3285 * signature is long, as opposed to the union structure inside
3286 * #GNUNET_CRYPTO_Signature.
3287 * Useful for compact serializations.
3288 *
3289 * @param sig the signature.
3290 * @return -1 on error, else the compacted length of the signature.
3291 */
3292ssize_t
3293GNUNET_CRYPTO_signature_get_length (
3294 const struct GNUNET_CRYPTO_Signature *sig);
3295
3296
3297/**
3298 * Get the compacted length of a signature by type.
3299 * Compacted means that it returns the minimum number of bytes this
3300 * signature is long, as opposed to the union structure inside
3301 * #GNUNET_CRYPTO_Signature.
3302 * Useful for compact serializations.
3303 *
3304 * @param sig the signature.
3305 * @return -1 on error, else the compacted length of the signature.
3306 */
3307ssize_t
3308GNUNET_CRYPTO_signature_get_raw_length_by_type (uint32_t type);
3309
3310
3311/**
3312 * Reads a #GNUNET_CRYPTO_Signature from a compact buffer.
3313 * The buffer has to contain at least the compacted length of
3314 * a #GNUNET_CRYPTO_Signature in bytes.
3315 * If the buffer is too small, the function returns -1 as error.
3316 * If the buffer does not contain a valid key, it returns -2 as error.
3317 *
3318 * @param sig the signature
3319 * @param buffer the buffer
3320 * @param len the length of buffer
3321 * @return -1 or -2 on error, else the amount of bytes read from the buffer
3322 */
3323ssize_t
3324GNUNET_CRYPTO_read_signature_from_buffer (
3325 struct GNUNET_CRYPTO_Signature *sig,
3326 const void*buffer,
3327 size_t len);
3328
3329
3330/**
3331 * Writes a #GNUNET_CRYPTO_Signature to a compact buffer.
3332 * The buffer requires space for at least the compacted length of
3333 * a #GNUNET_CRYPTO_Signature in bytes.
3334 * If the buffer is too small, the function returns -1 as error.
3335 * If the key is not valid, it returns -2 as error.
3336 *
3337 * @param sig the signature
3338 * @param buffer the buffer
3339 * @param len the length of buffer
3340 * @return -1 or -2 on error, else the amount of bytes written to the buffer
3341 */
3342ssize_t
3343GNUNET_CRYPTO_write_signature_to_buffer (
3344 const struct GNUNET_CRYPTO_Signature *sig,
3345 void*buffer,
3346 size_t len);
3347
3348
3349/**
3350 * @brief Sign a given block.
3351 *
3352 * The @a purpose data is the beginning of the data of which the signature is
3353 * to be created. The `size` field in @a purpose must correctly indicate the
3354 * number of bytes of the data structure, including its header. If possible,
3355 * use #GNUNET_CRYPTO_sign() instead of this function.
3356 *
3357 * @param priv private key to use for the signing
3358 * @param purpose what to sign (size, purpose)
3359 * @param[out] sig where to write the signature
3360 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
3361 */
3362enum GNUNET_GenericReturnValue
3363GNUNET_CRYPTO_sign_ (
3364 const struct GNUNET_CRYPTO_PrivateKey *priv,
3365 const struct GNUNET_CRYPTO_EccSignaturePurpose *purpose,
3366 struct GNUNET_CRYPTO_Signature *sig);
3367
3368/**
3369 * @brief Sign a given block.
3370 *
3371 * The @a purpose data is the beginning of the data of which the signature is
3372 * to be created. The `size` field in @a purpose must correctly indicate the
3373 * number of bytes of the data structure, including its header.
3374 * The signature payload and length depends on the key type.
3375 *
3376 * @param priv private key to use for the signing
3377 * @param purpose what to sign (size, purpose)
3378 * @param[out] sig where to write the signature
3379 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
3380 */
3381enum GNUNET_GenericReturnValue
3382GNUNET_CRYPTO_sign_raw_ (
3383 const struct GNUNET_CRYPTO_PrivateKey *priv,
3384 const struct GNUNET_CRYPTO_EccSignaturePurpose *purpose,
3385 unsigned char *sig);
3386
3387
3388/**
3389 * @brief Sign a given block with #GNUNET_CRYPTO_PrivateKey.
3390 *
3391 * The @a ps data must be a fixed-size struct for which the signature is to be
3392 * created. The `size` field in @a ps->purpose must correctly indicate the
3393 * number of bytes of the data structure, including its header.
3394 *
3395 * @param priv private key to use for the signing
3396 * @param ps packed struct with what to sign, MUST begin with a purpose
3397 * @param[out] sig where to write the signature
3398 */
3399#define GNUNET_CRYPTO_sign(priv,ps,sig) do { \
3400 /* check size is set correctly */ \
3401 GNUNET_assert (ntohl ((ps)->purpose.size) == sizeof (*(ps))); \
3402 /* check 'ps' begins with the purpose */ \
3403 GNUNET_static_assert (((void*) (ps)) == \
3404 ((void*) &(ps)->purpose)); \
3405 GNUNET_assert (GNUNET_OK == \
3406 GNUNET_CRYPTO_sign_ (priv, \
3407 &(ps)->purpose, \
3408 sig)); \
3409} while (0)
3410
3411
3412/**
3413 * @brief Verify a given signature.
3414 *
3415 * The @a validate data is the beginning of the data of which the signature
3416 * is to be verified. The `size` field in @a validate must correctly indicate
3417 * the number of bytes of the data structure, including its header. If @a
3418 * purpose does not match the purpose given in @a validate (the latter must be
3419 * in big endian), signature verification fails. If possible,
3420 * use #GNUNET_CRYPTO_signature_verify() instead of this function (only if @a validate
3421 * is not fixed-size, you must use this function directly).
3422 *
3423 * @param purpose what is the purpose that the signature should have?
3424 * @param validate block to validate (size, purpose, data)
3425 * @param sig signature that is being validated
3426 * @param pub public key of the signer
3427 * @returns #GNUNET_OK if ok, #GNUNET_SYSERR if invalid
3428 */
3429enum GNUNET_GenericReturnValue
3430GNUNET_CRYPTO_signature_verify_ (
3431 uint32_t purpose,
3432 const struct GNUNET_CRYPTO_EccSignaturePurpose *validate,
3433 const struct GNUNET_CRYPTO_Signature *sig,
3434 const struct GNUNET_CRYPTO_PublicKey *pub);
3435
3436/**
3437 * @brief Verify a given signature.
3438 *
3439 * The @a validate data is the beginning of the data of which the signature
3440 * is to be verified. The `size` field in @a validate must correctly indicate
3441 * the number of bytes of the data structure, including its header. If @a
3442 * purpose does not match the purpose given in @a validate (the latter must be
3443 * in big endian), signature verification fails.
3444 *
3445 * @param purpose what is the purpose that the signature should have?
3446 * @param validate block to validate (size, purpose, data)
3447 * @param sig signature that is being validated
3448 * @param pub public key of the signer
3449 * @returns #GNUNET_OK if ok, #GNUNET_SYSERR if invalid
3450 */
3451enum GNUNET_GenericReturnValue
3452GNUNET_CRYPTO_signature_verify_raw_ (
3453 uint32_t purpose,
3454 const struct GNUNET_CRYPTO_EccSignaturePurpose *validate,
3455 const unsigned char *sig,
3456 const struct GNUNET_CRYPTO_PublicKey *pub);
3457
3458
3459/**
3460 * @brief Verify a given signature with #GNUNET_CRYPTO_PublicKey.
3461 *
3462 * The @a ps data must be a fixed-size struct for which the signature is to be
3463 * created. The `size` field in @a ps->purpose must correctly indicate the
3464 * number of bytes of the data structure, including its header.
3465 *
3466 * @param purp purpose of the signature, must match 'ps->purpose.purpose'
3467 * (except in host byte order)
3468 * @param ps packed struct with what to sign, MUST begin with a purpose
3469 * @param sig where to read the signature from
3470 * @param pub public key to use for the verifying
3471 */
3472#define GNUNET_CRYPTO_signature_verify(purp,ps,sig,pub) ({ \
3473 /* check size is set correctly */ \
3474 GNUNET_assert (ntohl ((ps)->purpose.size) == sizeof (*(ps))); \
3475 /* check 'ps' begins with the purpose */ \
3476 GNUNET_static_assert (((void*) (ps)) == \
3477 ((void*) &(ps)->purpose)); \
3478 GNUNET_CRYPTO_signature_verify_ (purp, \
3479 &(ps)->purpose, \
3480 sig, \
3481 pub); \
3482 })
3483
3484
3485/**
3486 * Encrypt a block with #GNUNET_CRYPTO_PublicKey and derives a
3487 * #GNUNET_CRYPTO_EcdhePublicKey which is required for decryption
3488 * using ecdh to derive a symmetric key.
3489 *
3490 * @param block the block to encrypt
3491 * @param size the size of the @a block
3492 * @param pub public key to use for ecdh
3493 * @param ecc where to write the ecc public key
3494 * @param result the output parameter in which to store the encrypted result
3495 * can be the same or overlap with @c block
3496 * @returns the size of the encrypted block, -1 for errors.
3497 * Due to the use of CFB and therefore an effective stream cipher,
3498 * this size should be the same as @c len.
3499 */
3500ssize_t
3501GNUNET_CRYPTO_encrypt_old (const void *block,
3502 size_t size,
3503 const struct GNUNET_CRYPTO_PublicKey *pub,
3504 struct GNUNET_CRYPTO_EcdhePublicKey *ecc,
3505 void *result);
3506
3507
3508/**
3509 * Decrypt a given block with #GNUNET_CRYPTO_PrivateKey and a given
3510 * #GNUNET_CRYPTO_EcdhePublicKey using ecdh to derive a symmetric key.
3511 *
3512 * @param block the data to decrypt, encoded as returned by encrypt
3513 * @param size the size of the @a block to decrypt
3514 * @param priv private key to use for ecdh
3515 * @param ecc the ecc public key
3516 * @param result address to store the result at
3517 * can be the same or overlap with @c block
3518 * @return -1 on failure, size of decrypted block on success.
3519 * Due to the use of CFB and therefore an effective stream cipher,
3520 * this size should be the same as @c size.
3521 */
3522ssize_t
3523GNUNET_CRYPTO_decrypt_old (
3524 const void *block,
3525 size_t size,
3526 const struct GNUNET_CRYPTO_PrivateKey *priv,
3527 const struct GNUNET_CRYPTO_EcdhePublicKey *ecc,
3528 void *result);
3529
3530#define GNUNET_CRYPTO_ENCRYPT_OVERHEAD_BYTES (crypto_secretbox_MACBYTES \
3531 + sizeof (struct \
3532 GNUNET_CRYPTO_FoKemC))
3533
3534/**
3535 * Encrypt a block with #GNUNET_CRYPTO_PublicKey and derives a
3536 * #GNUNET_CRYPTO_EcdhePublicKey which is required for decryption
3537 * using ecdh to derive a symmetric key.
3538 *
3539 * Note that the result buffer for the ciphertext must be the length of
3540 * the message to encrypt plus #GNUNET_CRYPTO_ENCRYPT_OVERHEAD_BYTES.
3541 *
3542 * @param block the block to encrypt
3543 * @param size the size of the @a block
3544 * @param pub public key to encrypt for
3545 * @param result the output parameter in which to store the encrypted result
3546 * can be the same or overlap with @c block
3547 * @returns GNUNET_OK on success.
3548 */
3549enum GNUNET_GenericReturnValue
3550GNUNET_CRYPTO_encrypt (const void *block,
3551 size_t size,
3552 const struct GNUNET_CRYPTO_PublicKey *pub,
3553 void *result,
3554 size_t result_size);
3555
3556
3557/**
3558 * Decrypt a given block with #GNUNET_CRYPTO_PrivateKey and a given
3559 * #GNUNET_CRYPTO_EcdhePublicKey using ecdh to derive a symmetric key.
3560 *
3561 * @param block the data to decrypt, encoded as returned by encrypt
3562 * @param size the size of the @a block to decrypt
3563 * @param priv private key to use for ecdh
3564 * @param result address to store the result at
3565 * can be the same or overlap with @c block
3566 * @returns GNUNET_OK on success.
3567 */
3568enum GNUNET_GenericReturnValue
3569GNUNET_CRYPTO_decrypt (const void *block,
3570 size_t size,
3571 const struct GNUNET_CRYPTO_PrivateKey *priv,
3572 void *result,
3573 size_t result_size);
3574
3575
3576/**
3577 * Creates a (Base32) string representation of the public key.
3578 * The resulting string encodes a compacted representation of the key.
3579 * See also #GNUNET_CRYPTO_key_get_length.
3580 *
3581 * @param key the key.
3582 * @return the string representation of the key, or NULL on error.
3583 */
3584char *
3585GNUNET_CRYPTO_public_key_to_string (
3586 const struct GNUNET_CRYPTO_PublicKey *key);
3587
3588
3589/**
3590 * Creates a (Base32) string representation of the private key.
3591 * The resulting string encodes a compacted representation of the key.
3592 * See also #GNUNET_CRYPTO_key_get_length.
3593 *
3594 * @param key the key.
3595 * @return the string representation of the key, or NULL on error.
3596 */
3597char *
3598GNUNET_CRYPTO_private_key_to_string (
3599 const struct GNUNET_CRYPTO_PrivateKey *key);
3600
3601
3602/**
3603 * Parses a (Base32) string representation of the public key.
3604 * See also #GNUNET_CRYPTO_public_key_to_string.
3605 *
3606 * @param str the encoded key.
3607 * @param key where to write the key.
3608 * @return GNUNET_SYSERR on error.
3609 */
3610enum GNUNET_GenericReturnValue
3611GNUNET_CRYPTO_public_key_from_string (const char*str,
3612 struct GNUNET_CRYPTO_PublicKey *key);
3613
3614
3615/**
3616 * Parses a (Base32) string representation of the private key.
3617 * See also #GNUNET_CRYPTO_private_key_to_string.
3618 *
3619 * @param str the encoded key.
3620 * @param key where to write the key.
3621 * @return GNUNET_SYSERR on error.
3622 */
3623enum GNUNET_GenericReturnValue
3624GNUNET_CRYPTO_private_key_from_string (const char*str,
3625 struct GNUNET_CRYPTO_PrivateKey *key);
3626
3627
3628/**
3629 * Retrieves the public key representation of a private key.
3630 *
3631 * @param privkey the private key.
3632 * @param key the public key result.
3633 * @return GNUNET_SYSERR on error.
3634 */
3635enum GNUNET_GenericReturnValue
3636GNUNET_CRYPTO_key_get_public (const struct
3637 GNUNET_CRYPTO_PrivateKey *privkey,
3638 struct GNUNET_CRYPTO_PublicKey *key);
3639
3640
3641
3076#if 0 /* keep Emacsens' auto-indent happy */ 3642#if 0 /* keep Emacsens' auto-indent happy */
3077{ 3643{
3078#endif 3644#endif