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 <inttypes.h>
27 #include <stddef.h>
28 
29 // Define exactly one of these to 1 to set the size of the basic limb type.
30 // 16-bit limbs seems to give the best performance on 8-bit AVR micros.
31 #define CURVE25519_LIMB_8BIT 0
32 #define CURVE25519_LIMB_16BIT 1
33 #define CURVE25519_LIMB_32BIT 0
34 
36 {
37 public:
38  static bool eval(uint8_t result[32], const uint8_t s[32], const uint8_t x[32]);
39 
40  static void dh1(uint8_t k[32], uint8_t f[32]);
41  static bool dh2(uint8_t k[32], uint8_t f[32]);
42 
43 #if defined(TEST_CURVE25519_FIELD_OPS)
44 public:
45 #else
46 private:
47 #endif
48  // Define the limb types to use on this platform.
49  #if CURVE25519_LIMB_8BIT
50  typedef uint8_t limb_t;
51  typedef int8_t slimb_t;
52  typedef uint16_t dlimb_t;
53  #elif CURVE25519_LIMB_16BIT
54  typedef uint16_t limb_t;
55  typedef int16_t slimb_t;
56  typedef uint32_t dlimb_t;
57  #elif CURVE25519_LIMB_32BIT
58  typedef uint32_t limb_t;
59  typedef int32_t slimb_t;
60  typedef uint64_t dlimb_t;
61  #else
62  #error "limb_t must be 8, 16, or 32 bits in size"
63  #endif
64 
65  static uint8_t isWeakPoint(const uint8_t k[32]);
66 
67  static void reduce(limb_t *result, limb_t *x, uint8_t size);
68  static limb_t reduceQuick(limb_t *x);
69 
70  static void mul(limb_t *result, const limb_t *x, const limb_t *y);
71  static void square(limb_t *result, const limb_t *x)
72  {
73  mul(result, x, x);
74  }
75 
76  static void mulA24(limb_t *result, const limb_t *x);
77 
78  static void add(limb_t *result, const limb_t *x, const limb_t *y);
79  static void sub(limb_t *result, const limb_t *x, const limb_t *y);
80 
81  static void cswap(uint8_t select, limb_t *x, limb_t *y);
82 
83  static void recip(limb_t *result, const limb_t *x);
84 
85  static void unpack(limb_t *result, const uint8_t *x);
86  static void pack(uint8_t *result, const limb_t *x);
87 
88  // Constructor and destructor are private - cannot instantiate this class.
89  Curve25519() {}
90  ~Curve25519() {}
91 };
92 
93 #endif
Diffie-Hellman key agreement based on the elliptic curve modulo 2^255 - 19.
Definition: Curve25519.h:35
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