Arduino Cryptography Library
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 
28 class Ed25519;
29 
31 {
32 public:
33  static bool eval(uint8_t result[32], const uint8_t s[32], const uint8_t x[32]);
34 
35  static void dh1(uint8_t k[32], uint8_t f[32]);
36  static bool dh2(uint8_t k[32], uint8_t f[32]);
37 
38 #if defined(TEST_CURVE25519_FIELD_OPS)
39 public:
40 #else
41 private:
42 #endif
43  static uint8_t isWeakPoint(const uint8_t k[32]);
44 
45  static void reduce(limb_t *result, limb_t *x, uint8_t size);
46  static limb_t reduceQuick(limb_t *x);
47 
48  static void mulNoReduce(limb_t *result, const limb_t *x, const limb_t *y);
49 
50  static void mul(limb_t *result, const limb_t *x, const limb_t *y);
51  static void square(limb_t *result, const limb_t *x)
52  {
53  mul(result, x, x);
54  }
55 
56  static void mulA24(limb_t *result, const limb_t *x);
57 
58  static void mul_P(limb_t *result, const limb_t *x, const limb_t *y);
59 
60  static void add(limb_t *result, const limb_t *x, const limb_t *y);
61  static void sub(limb_t *result, const limb_t *x, const limb_t *y);
62 
63  static void cswap(limb_t select, limb_t *x, limb_t *y);
64  static void cmove(limb_t select, limb_t *x, const limb_t *y);
65 
66  static void pow250(limb_t *result, const limb_t *x);
67  static void recip(limb_t *result, const limb_t *x);
68  static bool sqrt(limb_t *result, const limb_t *x);
69 
70  // Constructor and destructor are private - cannot instantiate this class.
71  Curve25519() {}
72  ~Curve25519() {}
73 
74  friend class Ed25519;
75 };
76 
77 #endif
Diffie-Hellman key agreement based on the elliptic curve modulo 2^255 - 19.
Definition: Curve25519.h:31
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:283
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:245
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:80
Digital signatures based on the elliptic curve modulo 2^255 - 19.
Definition: Ed25519.h:30