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_CURVE15519_h
24 #define CRYPTO_CURVE15519_h
25 
26 #include "BigNumberUtil.h"
27 #include <stddef.h>
28 
30 {
31 public:
32  static bool eval(uint8_t result[32], const uint8_t s[32], const uint8_t x[32]);
33 
34  static void dh1(uint8_t k[32], uint8_t f[32]);
35  static bool dh2(uint8_t k[32], uint8_t f[32]);
36 
37 #if defined(TEST_CURVE25519_FIELD_OPS)
38 public:
39 #else
40 private:
41 #endif
42  static uint8_t isWeakPoint(const uint8_t k[32]);
43 
44  static void reduce(limb_t *result, limb_t *x, uint8_t size);
45  static limb_t reduceQuick(limb_t *x);
46 
47  static void mul(limb_t *result, const limb_t *x, const limb_t *y);
48  static void square(limb_t *result, const limb_t *x)
49  {
50  mul(result, x, x);
51  }
52 
53  static void mulA24(limb_t *result, const limb_t *x);
54 
55  static void add(limb_t *result, const limb_t *x, const limb_t *y);
56  static void sub(limb_t *result, const limb_t *x, const limb_t *y);
57 
58  static void cswap(uint8_t select, limb_t *x, limb_t *y);
59 
60  static void recip(limb_t *result, const limb_t *x);
61 
62  static void unpack(limb_t *result, const uint8_t *x);
63  static void pack(uint8_t *result, const limb_t *x);
64 
65  // Constructor and destructor are private - cannot instantiate this class.
66  Curve25519() {}
67  ~Curve25519() {}
68 };
69 
70 #endif
Diffie-Hellman key agreement based on the elliptic curve modulo 2^255 - 19.
Definition: Curve25519.h:29
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:68
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:231
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:269