ArduinoLibs
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Groups Pages
Curve25519.h
1 /*
2  * Copyright (C) 2015 Southern Storm Software, Pty Ltd.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included
12  * in all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20  * DEALINGS IN THE SOFTWARE.
21  */
22 
23 #ifndef CRYPTO_CURVE25519_h
24 #define CRYPTO_CURVE25519_h
25 
26 #include "BigNumberUtil.h"
27 
29 {
30 public:
31  static bool eval(uint8_t result[32], const uint8_t s[32], const uint8_t x[32]);
32 
33  static void dh1(uint8_t k[32], uint8_t f[32]);
34  static bool dh2(uint8_t k[32], uint8_t f[32]);
35 
36 #if defined(TEST_CURVE25519_FIELD_OPS)
37 public:
38 #else
39 private:
40 #endif
41  static uint8_t isWeakPoint(const uint8_t k[32]);
42 
43  static void reduce(limb_t *result, limb_t *x, uint8_t size);
44  static limb_t reduceQuick(limb_t *x);
45 
46  static void mul(limb_t *result, const limb_t *x, const limb_t *y);
47  static void square(limb_t *result, const limb_t *x)
48  {
49  mul(result, x, x);
50  }
51 
52  static void mulA24(limb_t *result, const limb_t *x);
53 
54  static void add(limb_t *result, const limb_t *x, const limb_t *y);
55  static void sub(limb_t *result, const limb_t *x, const limb_t *y);
56 
57  static void cswap(uint8_t select, limb_t *x, limb_t *y);
58 
59  static void recip(limb_t *result, const limb_t *x);
60 
61  // Constructor and destructor are private - cannot instantiate this class.
62  Curve25519() {}
63  ~Curve25519() {}
64 };
65 
66 #endif
Diffie-Hellman key agreement based on the elliptic curve modulo 2^255 - 19.
Definition: Curve25519.h:28
static bool eval(uint8_t result[32], const uint8_t s[32], const uint8_t x[32])
Evaluates the raw Curve25519 function.
Definition: Curve25519.cpp:79
static void dh1(uint8_t k[32], uint8_t f[32])
Performs phase 1 of a Diffie-Hellman key exchange using Curve25519.
Definition: Curve25519.cpp:244
static bool dh2(uint8_t k[32], uint8_t f[32])
Performs phase 2 of a Diffie-Hellman key exchange using Curve25519.
Definition: Curve25519.cpp:282