Salvatore Benedetto | 3c4b239 | 2016-06-22 17:49:15 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2013, Kenneth MacKay |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions are |
| 7 | * met: |
| 8 | * * Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * * Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in the |
| 12 | * documentation and/or other materials provided with the distribution. |
| 13 | * |
| 14 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 15 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 16 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 17 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 18 | * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 19 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 20 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 21 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 22 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 23 | * (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 | #ifndef _CRYPTO_ECC_H |
| 27 | #define _CRYPTO_ECC_H |
| 28 | |
| 29 | #define ECC_MAX_DIGITS 4 /* 256 */ |
| 30 | |
| 31 | #define ECC_DIGITS_TO_BYTES_SHIFT 3 |
| 32 | |
| 33 | /** |
| 34 | * ecc_is_key_valid() - Validate a given ECDH private key |
| 35 | * |
| 36 | * @curve_id: id representing the curve to use |
| 37 | * @ndigits: curve number of digits |
| 38 | * @private_key: private key to be used for the given curve |
| 39 | * @private_key_len: private key len |
| 40 | * |
| 41 | * Returns 0 if the key is acceptable, a negative value otherwise |
| 42 | */ |
| 43 | int ecc_is_key_valid(unsigned int curve_id, unsigned int ndigits, |
| 44 | const u8 *private_key, unsigned int private_key_len); |
| 45 | |
| 46 | /** |
| 47 | * ecdh_make_pub_key() - Compute an ECC public key |
| 48 | * |
| 49 | * @curve_id: id representing the curve to use |
| 50 | * @private_key: pregenerated private key for the given curve |
| 51 | * @private_key_len: length of private_key |
| 52 | * @public_key: buffer for storing the public key generated |
| 53 | * @public_key_len: length of the public_key buffer |
| 54 | * |
| 55 | * Returns 0 if the public key was generated successfully, a negative value |
| 56 | * if an error occurred. |
| 57 | */ |
| 58 | int ecdh_make_pub_key(const unsigned int curve_id, unsigned int ndigits, |
| 59 | const u8 *private_key, unsigned int private_key_len, |
| 60 | u8 *public_key, unsigned int public_key_len); |
| 61 | |
| 62 | /** |
Stephen Rothwell | 8f44df1 | 2016-06-24 16:20:22 +1000 | [diff] [blame] | 63 | * crypto_ecdh_shared_secret() - Compute a shared secret |
Salvatore Benedetto | 3c4b239 | 2016-06-22 17:49:15 +0100 | [diff] [blame] | 64 | * |
| 65 | * @curve_id: id representing the curve to use |
| 66 | * @private_key: private key of part A |
| 67 | * @private_key_len: length of private_key |
| 68 | * @public_key: public key of counterpart B |
| 69 | * @public_key_len: length of public_key |
| 70 | * @secret: buffer for storing the calculated shared secret |
| 71 | * @secret_len: length of the secret buffer |
| 72 | * |
Stephen Rothwell | 8f44df1 | 2016-06-24 16:20:22 +1000 | [diff] [blame] | 73 | * Note: It is recommended that you hash the result of crypto_ecdh_shared_secret |
Salvatore Benedetto | 3c4b239 | 2016-06-22 17:49:15 +0100 | [diff] [blame] | 74 | * before using it for symmetric encryption or HMAC. |
| 75 | * |
| 76 | * Returns 0 if the shared secret was generated successfully, a negative value |
| 77 | * if an error occurred. |
| 78 | */ |
Stephen Rothwell | 8f44df1 | 2016-06-24 16:20:22 +1000 | [diff] [blame] | 79 | int crypto_ecdh_shared_secret(unsigned int curve_id, unsigned int ndigits, |
Salvatore Benedetto | 3c4b239 | 2016-06-22 17:49:15 +0100 | [diff] [blame] | 80 | const u8 *private_key, unsigned int private_key_len, |
| 81 | const u8 *public_key, unsigned int public_key_len, |
| 82 | u8 *secret, unsigned int secret_len); |
| 83 | #endif |