| 1 |
/* |
| 2 |
Copyright (c) 2012, Nils Schneider <[email protected]> |
| 3 |
Copyright (c) 2016, Matthias Schiffer <[email protected]> |
| 4 |
All rights reserved. |
| 5 |
|
| 6 |
Redistribution and use in source and binary forms, with or without |
| 7 |
modification, are permitted provided that the following conditions are met: |
| 8 |
|
| 9 |
1. Redistributions of source code must retain the above copyright notice, |
| 10 |
this list of conditions and the following disclaimer. |
| 11 |
2. Redistributions in binary form must reproduce the above copyright notice, |
| 12 |
this list of conditions and the following disclaimer in the documentation |
| 13 |
and/or other materials provided with the distribution. |
| 14 |
|
| 15 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 16 |
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 17 |
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 18 |
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| 19 |
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 20 |
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 21 |
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 22 |
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 23 |
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 24 |
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 |
*/ |
| 26 |
|
| 27 |
#pragma once |
| 28 |
|
| 29 |
|
| 30 |
#include <libuecc/ecc.h> |
| 31 |
|
| 32 |
#include <stdbool.h> |
| 33 |
#include <stddef.h> |
| 34 |
#include <stdint.h> |
| 35 |
|
| 36 |
|
| 37 |
/** Canonical representation of a ECDSA signature */ |
| 38 |
typedef struct _ecdsa_signature { |
| 39 |
ecc_int256_t r; |
| 40 |
ecc_int256_t s; |
| 41 |
} ecdsa_signature_t; |
| 42 |
|
| 43 |
/** Representation of a hash and signature for efficient checking against multiple public keys */ |
| 44 |
typedef struct _ecdsa_verify_context { |
| 45 |
ecc_25519_work_t s1; |
| 46 |
ecc_int256_t u2; |
| 47 |
ecc_int256_t r; |
| 48 |
} ecdsa_verify_context_t; |
| 49 |
|
| 50 |
|
| 51 |
/** Checks if a unpacked public key is valid */ |
| 52 |
bool ecdsa_is_valid_pubkey(const ecc_25519_work_t *pubkey); |
| 53 |
|
| 54 |
/** |
| 55 |
* Signs a hash value using a given secret key |
| 56 |
* |
| 57 |
* The signature is deterministic, so using the same secret key to sign the same hash will always |
| 58 |
* yield the same value (using the same libecdsautil version). |
| 59 |
* |
| 60 |
* This function uses libuecc's legacy curve representation. |
| 61 |
*/ |
| 62 |
void ecdsa_sign_legacy(ecdsa_signature_t *signature, const ecc_int256_t *hash, const ecc_int256_t *secret); |
| 63 |
|
| 64 |
|
| 65 |
/** |
| 66 |
* Prepares a signature for a given hash for efficient validation |
| 67 |
* |
| 68 |
* This function uses libuecc's legacy curve representation. |
| 69 |
*/ |
| 70 |
void ecdsa_verify_prepare_legacy(ecdsa_verify_context_t *ctx, const ecc_int256_t *hash, const ecdsa_signature_t *signature); |
| 71 |
|
| 72 |
/** |
| 73 |
* Verifies a signature against a given public key |
| 74 |
* |
| 75 |
* This function uses libuecc's legacy curve representation. |
| 76 |
*/ |
| 77 |
bool ecdsa_verify_legacy(const ecdsa_verify_context_t *ctx, const ecc_25519_work_t *pubkey); |
| 78 |
|
| 79 |
/** |
| 80 |
* Verifies multiple signatures against a list of public keys and returns the number of valid signatures for distinct keys |
| 81 |
* |
| 82 |
* The caller must ensure that the given list of public keys does not contain duplicates. |
| 83 |
* |
| 84 |
* This function uses libuecc's legacy curve representation. |
| 85 |
*/ |
| 86 |
size_t ecdsa_verify_list_legacy(const ecdsa_verify_context_t *ctxs, size_t n_ctxs, const ecc_25519_work_t *pubkeys, size_t n_pubkeys); |