blob: e4fd4492c765f0d2031b6a276c6b85d74edd9375 [file] [log] [blame]
Salvatore Benedetto3c4b2392016-06-22 17:49:15 +01001/*
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
Tudor-Dan Ambarusc0ca1212017-05-25 10:18:03 +030037 * @ndigits: curve's number of digits
Salvatore Benedetto3c4b2392016-06-22 17:49:15 +010038 * @private_key: private key to be used for the given curve
Tudor-Dan Ambarusc0ca1212017-05-25 10:18:03 +030039 * @private_key_len: private key length
Salvatore Benedetto3c4b2392016-06-22 17:49:15 +010040 *
41 * Returns 0 if the key is acceptable, a negative value otherwise
42 */
43int ecc_is_key_valid(unsigned int curve_id, unsigned int ndigits,
Tudor-Dan Ambarusad269592017-05-25 10:18:05 +030044 const u64 *private_key, unsigned int private_key_len);
Salvatore Benedetto3c4b2392016-06-22 17:49:15 +010045
46/**
Tudor-Dan Ambarus6755fd22017-05-30 17:52:48 +030047 * ecc_gen_privkey() - Generates an ECC private key.
48 * The private key is a random integer in the range 0 < random < n, where n is a
49 * prime that is the order of the cyclic subgroup generated by the distinguished
50 * point G.
51 * @curve_id: id representing the curve to use
52 * @ndigits: curve number of digits
53 * @private_key: buffer for storing the generated private key
54 *
55 * Returns 0 if the private key was generated successfully, a negative value
56 * if an error occurred.
57 */
58int ecc_gen_privkey(unsigned int curve_id, unsigned int ndigits, u64 *privkey);
59
60/**
Tudor-Dan Ambarus7380c562017-05-30 15:37:56 +030061 * ecc_make_pub_key() - Compute an ECC public key
Salvatore Benedetto3c4b2392016-06-22 17:49:15 +010062 *
63 * @curve_id: id representing the curve to use
Tudor-Dan Ambarusc0ca1212017-05-25 10:18:03 +030064 * @ndigits: curve's number of digits
Salvatore Benedetto3c4b2392016-06-22 17:49:15 +010065 * @private_key: pregenerated private key for the given curve
Tudor-Dan Ambarusc0ca1212017-05-25 10:18:03 +030066 * @public_key: buffer for storing the generated public key
Salvatore Benedetto3c4b2392016-06-22 17:49:15 +010067 *
68 * Returns 0 if the public key was generated successfully, a negative value
69 * if an error occurred.
70 */
Tudor-Dan Ambarus7380c562017-05-30 15:37:56 +030071int ecc_make_pub_key(const unsigned int curve_id, unsigned int ndigits,
72 const u64 *private_key, u64 *public_key);
Salvatore Benedetto3c4b2392016-06-22 17:49:15 +010073
74/**
Stephen Rothwell8f44df12016-06-24 16:20:22 +100075 * crypto_ecdh_shared_secret() - Compute a shared secret
Salvatore Benedetto3c4b2392016-06-22 17:49:15 +010076 *
77 * @curve_id: id representing the curve to use
Tudor-Dan Ambarusc0ca1212017-05-25 10:18:03 +030078 * @ndigits: curve's number of digits
Salvatore Benedetto3c4b2392016-06-22 17:49:15 +010079 * @private_key: private key of part A
Salvatore Benedetto3c4b2392016-06-22 17:49:15 +010080 * @public_key: public key of counterpart B
Salvatore Benedetto3c4b2392016-06-22 17:49:15 +010081 * @secret: buffer for storing the calculated shared secret
Salvatore Benedetto3c4b2392016-06-22 17:49:15 +010082 *
Stephen Rothwell8f44df12016-06-24 16:20:22 +100083 * Note: It is recommended that you hash the result of crypto_ecdh_shared_secret
Salvatore Benedetto3c4b2392016-06-22 17:49:15 +010084 * before using it for symmetric encryption or HMAC.
85 *
86 * Returns 0 if the shared secret was generated successfully, a negative value
87 * if an error occurred.
88 */
Stephen Rothwell8f44df12016-06-24 16:20:22 +100089int crypto_ecdh_shared_secret(unsigned int curve_id, unsigned int ndigits,
Tudor-Dan Ambarusad269592017-05-25 10:18:05 +030090 const u64 *private_key, const u64 *public_key,
91 u64 *secret);
Salvatore Benedetto3c4b2392016-06-22 17:49:15 +010092#endif