Back to home page

LXR

 
 

    


0001 /*
0002  * Copyright (c) 2013, Kenneth MacKay
0003  * All rights reserved.
0004  *
0005  * Redistribution and use in source and binary forms, with or without
0006  * modification, are permitted provided that the following conditions are
0007  * met:
0008  *  * Redistributions of source code must retain the above copyright
0009  *   notice, this list of conditions and the following disclaimer.
0010  *  * Redistributions in binary form must reproduce the above copyright
0011  *    notice, this list of conditions and the following disclaimer in the
0012  *    documentation and/or other materials provided with the distribution.
0013  *
0014  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
0015  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
0016  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
0017  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
0018  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
0019  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
0020  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
0021  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
0022  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
0023  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
0024  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0025  */
0026 #ifndef _CRYPTO_ECC_H
0027 #define _CRYPTO_ECC_H
0028 
0029 #define ECC_MAX_DIGITS  4 /* 256 */
0030 
0031 #define ECC_DIGITS_TO_BYTES_SHIFT 3
0032 
0033 /**
0034  * ecc_is_key_valid() - Validate a given ECDH private key
0035  *
0036  * @curve_id:       id representing the curve to use
0037  * @ndigits:        curve number of digits
0038  * @private_key:    private key to be used for the given curve
0039  * @private_key_len:    private key len
0040  *
0041  * Returns 0 if the key is acceptable, a negative value otherwise
0042  */
0043 int ecc_is_key_valid(unsigned int curve_id, unsigned int ndigits,
0044              const u8 *private_key, unsigned int private_key_len);
0045 
0046 /**
0047  * ecdh_make_pub_key() - Compute an ECC public key
0048  *
0049  * @curve_id:       id representing the curve to use
0050  * @private_key:    pregenerated private key for the given curve
0051  * @private_key_len:    length of private_key
0052  * @public_key:     buffer for storing the public key generated
0053  * @public_key_len: length of the public_key buffer
0054  *
0055  * Returns 0 if the public key was generated successfully, a negative value
0056  * if an error occurred.
0057  */
0058 int ecdh_make_pub_key(const unsigned int curve_id, unsigned int ndigits,
0059               const u8 *private_key, unsigned int private_key_len,
0060               u8 *public_key, unsigned int public_key_len);
0061 
0062 /**
0063  * crypto_ecdh_shared_secret() - Compute a shared secret
0064  *
0065  * @curve_id:       id representing the curve to use
0066  * @private_key:    private key of part A
0067  * @private_key_len:    length of private_key
0068  * @public_key:     public key of counterpart B
0069  * @public_key_len: length of public_key
0070  * @secret:     buffer for storing the calculated shared secret
0071  * @secret_len:     length of the secret buffer
0072  *
0073  * Note: It is recommended that you hash the result of crypto_ecdh_shared_secret
0074  * before using it for symmetric encryption or HMAC.
0075  *
0076  * Returns 0 if the shared secret was generated successfully, a negative value
0077  * if an error occurred.
0078  */
0079 int crypto_ecdh_shared_secret(unsigned int curve_id, unsigned int ndigits,
0080                const u8 *private_key, unsigned int private_key_len,
0081                const u8 *public_key, unsigned int public_key_len,
0082                u8 *secret, unsigned int secret_len);
0083 #endif