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 | |
Kees Cook | d5c3b17 | 2018-03-30 09:55:44 -0700 | [diff] [blame^] | 29 | #define ECC_CURVE_NIST_P192_DIGITS 3 |
| 30 | #define ECC_CURVE_NIST_P256_DIGITS 4 |
| 31 | #define ECC_MAX_DIGITS ECC_CURVE_NIST_P256_DIGITS |
Salvatore Benedetto | 3c4b239 | 2016-06-22 17:49:15 +0100 | [diff] [blame] | 32 | |
| 33 | #define ECC_DIGITS_TO_BYTES_SHIFT 3 |
| 34 | |
| 35 | /** |
| 36 | * ecc_is_key_valid() - Validate a given ECDH private key |
| 37 | * |
| 38 | * @curve_id: id representing the curve to use |
Tudor-Dan Ambarus | c0ca121 | 2017-05-25 10:18:03 +0300 | [diff] [blame] | 39 | * @ndigits: curve's number of digits |
Salvatore Benedetto | 3c4b239 | 2016-06-22 17:49:15 +0100 | [diff] [blame] | 40 | * @private_key: private key to be used for the given curve |
Tudor-Dan Ambarus | c0ca121 | 2017-05-25 10:18:03 +0300 | [diff] [blame] | 41 | * @private_key_len: private key length |
Salvatore Benedetto | 3c4b239 | 2016-06-22 17:49:15 +0100 | [diff] [blame] | 42 | * |
| 43 | * Returns 0 if the key is acceptable, a negative value otherwise |
| 44 | */ |
| 45 | int ecc_is_key_valid(unsigned int curve_id, unsigned int ndigits, |
Tudor-Dan Ambarus | ad26959 | 2017-05-25 10:18:05 +0300 | [diff] [blame] | 46 | const u64 *private_key, unsigned int private_key_len); |
Salvatore Benedetto | 3c4b239 | 2016-06-22 17:49:15 +0100 | [diff] [blame] | 47 | |
| 48 | /** |
Tudor-Dan Ambarus | 6755fd2 | 2017-05-30 17:52:48 +0300 | [diff] [blame] | 49 | * ecc_gen_privkey() - Generates an ECC private key. |
| 50 | * The private key is a random integer in the range 0 < random < n, where n is a |
| 51 | * prime that is the order of the cyclic subgroup generated by the distinguished |
| 52 | * point G. |
| 53 | * @curve_id: id representing the curve to use |
| 54 | * @ndigits: curve number of digits |
| 55 | * @private_key: buffer for storing the generated private key |
| 56 | * |
| 57 | * Returns 0 if the private key was generated successfully, a negative value |
| 58 | * if an error occurred. |
| 59 | */ |
| 60 | int ecc_gen_privkey(unsigned int curve_id, unsigned int ndigits, u64 *privkey); |
| 61 | |
| 62 | /** |
Tudor-Dan Ambarus | 7380c56 | 2017-05-30 15:37:56 +0300 | [diff] [blame] | 63 | * ecc_make_pub_key() - Compute an ECC public key |
Salvatore Benedetto | 3c4b239 | 2016-06-22 17:49:15 +0100 | [diff] [blame] | 64 | * |
| 65 | * @curve_id: id representing the curve to use |
Tudor-Dan Ambarus | c0ca121 | 2017-05-25 10:18:03 +0300 | [diff] [blame] | 66 | * @ndigits: curve's number of digits |
Salvatore Benedetto | 3c4b239 | 2016-06-22 17:49:15 +0100 | [diff] [blame] | 67 | * @private_key: pregenerated private key for the given curve |
Tudor-Dan Ambarus | c0ca121 | 2017-05-25 10:18:03 +0300 | [diff] [blame] | 68 | * @public_key: buffer for storing the generated public key |
Salvatore Benedetto | 3c4b239 | 2016-06-22 17:49:15 +0100 | [diff] [blame] | 69 | * |
| 70 | * Returns 0 if the public key was generated successfully, a negative value |
| 71 | * if an error occurred. |
| 72 | */ |
Tudor-Dan Ambarus | 7380c56 | 2017-05-30 15:37:56 +0300 | [diff] [blame] | 73 | int ecc_make_pub_key(const unsigned int curve_id, unsigned int ndigits, |
| 74 | const u64 *private_key, u64 *public_key); |
Salvatore Benedetto | 3c4b239 | 2016-06-22 17:49:15 +0100 | [diff] [blame] | 75 | |
| 76 | /** |
Stephen Rothwell | 8f44df1 | 2016-06-24 16:20:22 +1000 | [diff] [blame] | 77 | * crypto_ecdh_shared_secret() - Compute a shared secret |
Salvatore Benedetto | 3c4b239 | 2016-06-22 17:49:15 +0100 | [diff] [blame] | 78 | * |
| 79 | * @curve_id: id representing the curve to use |
Tudor-Dan Ambarus | c0ca121 | 2017-05-25 10:18:03 +0300 | [diff] [blame] | 80 | * @ndigits: curve's number of digits |
Salvatore Benedetto | 3c4b239 | 2016-06-22 17:49:15 +0100 | [diff] [blame] | 81 | * @private_key: private key of part A |
Salvatore Benedetto | 3c4b239 | 2016-06-22 17:49:15 +0100 | [diff] [blame] | 82 | * @public_key: public key of counterpart B |
Salvatore Benedetto | 3c4b239 | 2016-06-22 17:49:15 +0100 | [diff] [blame] | 83 | * @secret: buffer for storing the calculated shared secret |
Salvatore Benedetto | 3c4b239 | 2016-06-22 17:49:15 +0100 | [diff] [blame] | 84 | * |
Stephen Rothwell | 8f44df1 | 2016-06-24 16:20:22 +1000 | [diff] [blame] | 85 | * 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] | 86 | * before using it for symmetric encryption or HMAC. |
| 87 | * |
| 88 | * Returns 0 if the shared secret was generated successfully, a negative value |
| 89 | * if an error occurred. |
| 90 | */ |
Stephen Rothwell | 8f44df1 | 2016-06-24 16:20:22 +1000 | [diff] [blame] | 91 | int crypto_ecdh_shared_secret(unsigned int curve_id, unsigned int ndigits, |
Tudor-Dan Ambarus | ad26959 | 2017-05-25 10:18:05 +0300 | [diff] [blame] | 92 | const u64 *private_key, const u64 *public_key, |
| 93 | u64 *secret); |
Salvatore Benedetto | 3c4b239 | 2016-06-22 17:49:15 +0100 | [diff] [blame] | 94 | #endif |