1
0
mirror of https://github.com/taigrr/arduinolibs synced 2025-01-18 04:33:12 -08:00

Redesign how Noise protocol descriptors are defined

This commit is contained in:
Rhys Weatherley 2018-06-22 04:49:33 +10:00
parent 4db3d6c8f0
commit ff04a61efa
21 changed files with 128 additions and 245 deletions

View File

@ -98,7 +98,6 @@ SOURCES += \
Noise_NNpsk0_25519_ChaChaPoly_SHA256.cpp \
Noise_NNpsk0.cpp \
NoiseProtobufs.cpp \
NoiseProtocolDescriptor.cpp \
NoiseSymmetricState_AESGCM_SHA256.cpp \
NoiseSymmetricState_ChaChaPoly_BLAKE2s.cpp \
NoiseSymmetricState_ChaChaPoly_SHA256.cpp \

View File

@ -1,77 +0,0 @@
/*
* Copyright (C) 2018 Southern Storm Software, Pty Ltd.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#include "NoiseProtocolDescriptor.h"
/**
* \class NoiseProtocolDescriptor NoiseProtocolDescriptor.h <NoiseProtocolDescriptor.h>
* \brief Description of a Noise protocol and a method to create a handshake
* object that implements the protocol.
*
* This class is abstract. The caller should instantiate a subclass like
* Noise_XX_25519_ChaChaPoly_BLAKE2s or Noise_XX_25519_AESGCM_SHA256 to
* get an actual descriptor.
*/
/**
* \fn NoiseProtocolDescriptor::NoiseProtocolDescriptor(const char *name, const char *alias)
* \brief Creates a new Noise protocol descriptor.
*
* \param name Name of the Noise protocol, e.g. "Noise_XX_25519_AESGCM_SHA256".
* \param alias NoiseTinyLink alias for the Noise protocol; e.g. "1". Set this
* to NULL if the protocol does not have a NoiseTinyLink alias defined.
*/
/**
* \brief Destroys this Noise protocol descriptor.
*/
NoiseProtocolDescriptor::~NoiseProtocolDescriptor()
{
}
/**
* \fn const char *NoiseProtocolDescriptor::protocolName() const
* \brief Gets the name of the Noise protocol represented by this descriptor.
*
* \return The name of the Noise protocol.
*
* \sa protocolAlias()
*/
/**
* \fn const char *NoiseProtocolDescriptor::protocolAlias() const
* \brief Gets the NoiseTinyLink alias for the Noise protocol represented
* by this descriptor.
*
* \return The alias or NULL if the protocol does not have a NoiseTinyLink
* alias defined.
*
* \sa protocolName()
*/
/**
* \fn NoiseHandshakeState *NoiseProtocolDescriptor::createHandshake() const
* \brief Creates a handshake object for the Noise protocol represented
* by this descriptor.
*
* \return A new handshake object for the protocol.
*/

View File

@ -25,23 +25,29 @@
class NoiseHandshakeState;
class NoiseProtocolDescriptor
/** Noise Protocol needs a local static key pair to connect */
#define NOISE_PROTOCOL_NEEDS_LOCAL_STATIC 0x0001
/** Noise protocol needs a remote static public key to connect */
#define NOISE_PROTOCOL_NEEDS_REMOTE_STATIC 0x0002
/** Noise protocol needs a pre-shared symmetric key to connect */
#define NOISE_PROTOCOL_NEEDS_PSK 0x0004
/**
* \brief Structure that provides metadata for Noise protocols.
*/
struct NoiseProtocolDescriptor
{
public:
virtual ~NoiseProtocolDescriptor();
/** Flags that define the properties and required keys for the protocol */
unsigned flags;
const char *protocolName() const { return protoName; }
const char *protocolAlias() const { return protoAlias; }
/** Full Noise protocol name; e.g. "Noise_XX_25519_ChaChaPoly_BLAKE2s" */
const char *protocolName;
virtual NoiseHandshakeState *createHandshake() const = 0;
/** NoiseTinyLink alias for the protocol, or NULL if no alias */
const char *protocolAlias;
protected:
explicit NoiseProtocolDescriptor(const char *name, const char *alias = 0)
: protoName(name), protoAlias(alias) {}
private:
const char *protoName;
const char *protoAlias;
/** Function that creates a handshake instance for the protocol */
NoiseHandshakeState *(*createHandshake)();
};
#endif

View File

@ -46,16 +46,17 @@ NoiseHandshakeState_IK_25519_AESGCM_SHA256::~NoiseHandshakeState_IK_25519_AESGCM
{
}
Noise_IK_25519_AESGCM_SHA256::Noise_IK_25519_AESGCM_SHA256()
: NoiseProtocolDescriptor(Noise_IK_25519_AESGCM_SHA256_Name)
{
}
Noise_IK_25519_AESGCM_SHA256::~Noise_IK_25519_AESGCM_SHA256()
{
}
NoiseHandshakeState *Noise_IK_25519_AESGCM_SHA256::createHandshake() const
static NoiseHandshakeState *Noise_IK_25519_AESGCM_SHA256_createHandshake()
{
return new NoiseHandshakeState_IK_25519_AESGCM_SHA256();
}
/**
* \brief Protocol descriptor for "Noise_IK_25519_AESGCM_SHA256".
*/
const NoiseProtocolDescriptor Noise_IK_25519_AESGCM_SHA256 = {
NOISE_PROTOCOL_NEEDS_LOCAL_STATIC | NOISE_PROTOCOL_NEEDS_REMOTE_STATIC,
Noise_IK_25519_AESGCM_SHA256_Name,
0,
Noise_IK_25519_AESGCM_SHA256_createHandshake
};

View File

@ -39,13 +39,6 @@ private:
NoiseDHState_Curve25519 dh;
};
class Noise_IK_25519_AESGCM_SHA256 : public NoiseProtocolDescriptor
{
public:
Noise_IK_25519_AESGCM_SHA256();
virtual ~Noise_IK_25519_AESGCM_SHA256();
NoiseHandshakeState *createHandshake() const;
};
extern const NoiseProtocolDescriptor Noise_IK_25519_AESGCM_SHA256;
#endif

View File

@ -46,16 +46,17 @@ NoiseHandshakeState_IK_25519_ChaChaPoly_BLAKE2s::~NoiseHandshakeState_IK_25519_C
{
}
Noise_IK_25519_ChaChaPoly_BLAKE2s::Noise_IK_25519_ChaChaPoly_BLAKE2s()
: NoiseProtocolDescriptor(Noise_IK_25519_ChaChaPoly_BLAKE2s_Name)
{
}
Noise_IK_25519_ChaChaPoly_BLAKE2s::~Noise_IK_25519_ChaChaPoly_BLAKE2s()
{
}
NoiseHandshakeState *Noise_IK_25519_ChaChaPoly_BLAKE2s::createHandshake() const
static NoiseHandshakeState *Noise_IK_25519_ChaChaPoly_BLAKE2s_createHandshake()
{
return new NoiseHandshakeState_IK_25519_ChaChaPoly_BLAKE2s();
}
/**
* \brief Protocol descriptor for "Noise_IK_25519_ChaChaPoly_BLAKE2s".
*/
const NoiseProtocolDescriptor Noise_IK_25519_ChaChaPoly_BLAKE2s = {
NOISE_PROTOCOL_NEEDS_LOCAL_STATIC | NOISE_PROTOCOL_NEEDS_REMOTE_STATIC,
Noise_IK_25519_ChaChaPoly_BLAKE2s_Name,
0,
Noise_IK_25519_ChaChaPoly_BLAKE2s_createHandshake
};

View File

@ -40,13 +40,6 @@ private:
NoiseDHState_Curve25519 dh;
};
class Noise_IK_25519_ChaChaPoly_BLAKE2s : public NoiseProtocolDescriptor
{
public:
Noise_IK_25519_ChaChaPoly_BLAKE2s();
virtual ~Noise_IK_25519_ChaChaPoly_BLAKE2s();
NoiseHandshakeState *createHandshake() const;
};
extern const NoiseProtocolDescriptor Noise_IK_25519_ChaChaPoly_BLAKE2s;
#endif

View File

@ -46,16 +46,17 @@ NoiseHandshakeState_IK_25519_ChaChaPoly_SHA256::~NoiseHandshakeState_IK_25519_Ch
{
}
Noise_IK_25519_ChaChaPoly_SHA256::Noise_IK_25519_ChaChaPoly_SHA256()
: NoiseProtocolDescriptor(Noise_IK_25519_ChaChaPoly_SHA256_Name)
{
}
Noise_IK_25519_ChaChaPoly_SHA256::~Noise_IK_25519_ChaChaPoly_SHA256()
{
}
NoiseHandshakeState *Noise_IK_25519_ChaChaPoly_SHA256::createHandshake() const
static NoiseHandshakeState *Noise_IK_25519_ChaChaPoly_SHA256_createHandshake()
{
return new NoiseHandshakeState_IK_25519_ChaChaPoly_SHA256();
}
/**
* \brief Protocol descriptor for "Noise_IK_25519_ChaChaPoly_SHA256".
*/
const NoiseProtocolDescriptor Noise_IK_25519_ChaChaPoly_SHA256 = {
NOISE_PROTOCOL_NEEDS_LOCAL_STATIC | NOISE_PROTOCOL_NEEDS_REMOTE_STATIC,
Noise_IK_25519_ChaChaPoly_SHA256_Name,
0,
Noise_IK_25519_ChaChaPoly_SHA256_createHandshake
};

View File

@ -40,13 +40,6 @@ private:
NoiseDHState_Curve25519 dh;
};
class Noise_IK_25519_ChaChaPoly_SHA256 : public NoiseProtocolDescriptor
{
public:
Noise_IK_25519_ChaChaPoly_SHA256();
virtual ~Noise_IK_25519_ChaChaPoly_SHA256();
NoiseHandshakeState *createHandshake() const;
};
extern const NoiseProtocolDescriptor Noise_IK_25519_ChaChaPoly_SHA256;
#endif

View File

@ -46,16 +46,17 @@ NoiseHandshakeState_NNpsk0_25519_AESGCM_SHA256::~NoiseHandshakeState_NNpsk0_2551
{
}
Noise_NNpsk0_25519_AESGCM_SHA256::Noise_NNpsk0_25519_AESGCM_SHA256()
: NoiseProtocolDescriptor(Noise_NNpsk0_25519_AESGCM_SHA256_Name)
{
}
Noise_NNpsk0_25519_AESGCM_SHA256::~Noise_NNpsk0_25519_AESGCM_SHA256()
{
}
NoiseHandshakeState *Noise_NNpsk0_25519_AESGCM_SHA256::createHandshake() const
static NoiseHandshakeState *Noise_NNpsk0_25519_AESGCM_SHA256_createHandshake()
{
return new NoiseHandshakeState_NNpsk0_25519_AESGCM_SHA256();
}
/**
* \brief Protocol descriptor for "Noise_NNps0_25519_AESGCM_SHA256".
*/
const NoiseProtocolDescriptor Noise_NNpsk0_25519_AESGCM_SHA256 = {
NOISE_PROTOCOL_NEEDS_PSK,
Noise_NNpsk0_25519_AESGCM_SHA256_Name,
0,
Noise_NNpsk0_25519_AESGCM_SHA256_createHandshake
};

View File

@ -40,13 +40,6 @@ private:
NoiseDHState_Curve25519_EphemOnly dh;
};
class Noise_NNpsk0_25519_AESGCM_SHA256 : public NoiseProtocolDescriptor
{
public:
Noise_NNpsk0_25519_AESGCM_SHA256();
virtual ~Noise_NNpsk0_25519_AESGCM_SHA256();
NoiseHandshakeState *createHandshake() const;
};
extern const NoiseProtocolDescriptor Noise_NNpsk0_25519_AESGCM_SHA256;
#endif

View File

@ -46,16 +46,17 @@ NoiseHandshakeState_NNpsk0_25519_ChaChaPoly_BLAKE2s::~NoiseHandshakeState_NNpsk0
{
}
Noise_NNpsk0_25519_ChaChaPoly_BLAKE2s::Noise_NNpsk0_25519_ChaChaPoly_BLAKE2s()
: NoiseProtocolDescriptor(Noise_NNpsk0_25519_ChaChaPoly_BLAKE2s_Name)
{
}
Noise_NNpsk0_25519_ChaChaPoly_BLAKE2s::~Noise_NNpsk0_25519_ChaChaPoly_BLAKE2s()
{
}
NoiseHandshakeState *Noise_NNpsk0_25519_ChaChaPoly_BLAKE2s::createHandshake() const
static NoiseHandshakeState *Noise_NNpsk0_25519_ChaChaPoly_BLAKE2s_createHandshake()
{
return new NoiseHandshakeState_NNpsk0_25519_ChaChaPoly_BLAKE2s();
}
/**
* \brief Protocol descriptor for "Noise_NNps0_25519_ChaChaPoly_BLAKE2s".
*/
const NoiseProtocolDescriptor Noise_NNpsk0_25519_ChaChaPoly_BLAKE2s = {
NOISE_PROTOCOL_NEEDS_PSK,
Noise_NNpsk0_25519_ChaChaPoly_BLAKE2s_Name,
0,
Noise_NNpsk0_25519_ChaChaPoly_BLAKE2s_createHandshake
};

View File

@ -40,13 +40,6 @@ private:
NoiseDHState_Curve25519_EphemOnly dh;
};
class Noise_NNpsk0_25519_ChaChaPoly_BLAKE2s : public NoiseProtocolDescriptor
{
public:
Noise_NNpsk0_25519_ChaChaPoly_BLAKE2s();
virtual ~Noise_NNpsk0_25519_ChaChaPoly_BLAKE2s();
NoiseHandshakeState *createHandshake() const;
};
extern const NoiseProtocolDescriptor Noise_NNpsk0_25519_ChaChaPoly_BLAKE2s;
#endif

View File

@ -46,16 +46,17 @@ NoiseHandshakeState_NNpsk0_25519_ChaChaPoly_SHA256::~NoiseHandshakeState_NNpsk0_
{
}
Noise_NNpsk0_25519_ChaChaPoly_SHA256::Noise_NNpsk0_25519_ChaChaPoly_SHA256()
: NoiseProtocolDescriptor(Noise_NNpsk0_25519_ChaChaPoly_SHA256_Name)
{
}
Noise_NNpsk0_25519_ChaChaPoly_SHA256::~Noise_NNpsk0_25519_ChaChaPoly_SHA256()
{
}
NoiseHandshakeState *Noise_NNpsk0_25519_ChaChaPoly_SHA256::createHandshake() const
static NoiseHandshakeState *Noise_NNpsk0_25519_ChaChaPoly_SHA256_createHandshake()
{
return new NoiseHandshakeState_NNpsk0_25519_ChaChaPoly_SHA256();
}
/**
* \brief Protocol descriptor for "Noise_NNps0_25519_ChaChaPoly_SHA256".
*/
const NoiseProtocolDescriptor Noise_NNpsk0_25519_ChaChaPoly_SHA256 = {
NOISE_PROTOCOL_NEEDS_PSK,
Noise_NNpsk0_25519_ChaChaPoly_SHA256_Name,
0,
Noise_NNpsk0_25519_ChaChaPoly_SHA256_createHandshake
};

View File

@ -49,4 +49,6 @@ public:
NoiseHandshakeState *createHandshake() const;
};
extern const NoiseProtocolDescriptor Noise_NNpsk0_25519_ChaChaPoly_SHA256;
#endif

View File

@ -46,16 +46,17 @@ NoiseHandshakeState_XX_25519_AESGCM_SHA256::~NoiseHandshakeState_XX_25519_AESGCM
{
}
Noise_XX_25519_AESGCM_SHA256::Noise_XX_25519_AESGCM_SHA256()
: NoiseProtocolDescriptor(Noise_XX_25519_AESGCM_SHA256_Name, "1")
{
}
Noise_XX_25519_AESGCM_SHA256::~Noise_XX_25519_AESGCM_SHA256()
{
}
NoiseHandshakeState *Noise_XX_25519_AESGCM_SHA256::createHandshake() const
static NoiseHandshakeState *Noise_XX_25519_AESGCM_SHA256_createHandshake()
{
return new NoiseHandshakeState_XX_25519_AESGCM_SHA256();
}
/**
* \brief Protocol descriptor for "Noise_XX_25519_AESGCM_SHA256".
*/
const NoiseProtocolDescriptor Noise_XX_25519_AESGCM_SHA256 = {
NOISE_PROTOCOL_NEEDS_LOCAL_STATIC,
Noise_XX_25519_AESGCM_SHA256_Name,
"1",
Noise_XX_25519_AESGCM_SHA256_createHandshake
};

View File

@ -39,13 +39,6 @@ private:
NoiseDHState_Curve25519 dh;
};
class Noise_XX_25519_AESGCM_SHA256 : public NoiseProtocolDescriptor
{
public:
Noise_XX_25519_AESGCM_SHA256();
virtual ~Noise_XX_25519_AESGCM_SHA256();
NoiseHandshakeState *createHandshake() const;
};
extern const NoiseProtocolDescriptor Noise_XX_25519_AESGCM_SHA256;
#endif

View File

@ -46,16 +46,17 @@ NoiseHandshakeState_XX_25519_ChaChaPoly_BLAKE2s::~NoiseHandshakeState_XX_25519_C
{
}
Noise_XX_25519_ChaChaPoly_BLAKE2s::Noise_XX_25519_ChaChaPoly_BLAKE2s()
: NoiseProtocolDescriptor(Noise_XX_25519_ChaChaPoly_BLAKE2s_Name, "3")
{
}
Noise_XX_25519_ChaChaPoly_BLAKE2s::~Noise_XX_25519_ChaChaPoly_BLAKE2s()
{
}
NoiseHandshakeState *Noise_XX_25519_ChaChaPoly_BLAKE2s::createHandshake() const
static NoiseHandshakeState *Noise_XX_25519_ChaChaPoly_BLAKE2s_createHandshake()
{
return new NoiseHandshakeState_XX_25519_ChaChaPoly_BLAKE2s();
}
/**
* \brief Protocol descriptor for "Noise_XX_25519ChaChaPoly_BLAKE2s".
*/
const NoiseProtocolDescriptor Noise_XX_25519_ChaChaPoly_BLAKE2s = {
NOISE_PROTOCOL_NEEDS_LOCAL_STATIC,
Noise_XX_25519_ChaChaPoly_BLAKE2s_Name,
"3",
Noise_XX_25519_ChaChaPoly_BLAKE2s_createHandshake
};

View File

@ -40,13 +40,6 @@ private:
NoiseDHState_Curve25519 dh;
};
class Noise_XX_25519_ChaChaPoly_BLAKE2s : public NoiseProtocolDescriptor
{
public:
Noise_XX_25519_ChaChaPoly_BLAKE2s();
virtual ~Noise_XX_25519_ChaChaPoly_BLAKE2s();
NoiseHandshakeState *createHandshake() const;
};
extern const NoiseProtocolDescriptor Noise_XX_25519_ChaChaPoly_BLAKE2s;
#endif

View File

@ -46,16 +46,17 @@ NoiseHandshakeState_XX_25519_ChaChaPoly_SHA256::~NoiseHandshakeState_XX_25519_Ch
{
}
Noise_XX_25519_ChaChaPoly_SHA256::Noise_XX_25519_ChaChaPoly_SHA256()
: NoiseProtocolDescriptor(Noise_XX_25519_ChaChaPoly_SHA256_Name, "2")
{
}
Noise_XX_25519_ChaChaPoly_SHA256::~Noise_XX_25519_ChaChaPoly_SHA256()
{
}
NoiseHandshakeState *Noise_XX_25519_ChaChaPoly_SHA256::createHandshake() const
static NoiseHandshakeState *Noise_XX_25519_ChaChaPoly_SHA256_createHandshake()
{
return new NoiseHandshakeState_XX_25519_ChaChaPoly_SHA256();
}
/**
* \brief Protocol descriptor for "Noise_XX_25519ChaChaPoly_SHA256".
*/
const NoiseProtocolDescriptor Noise_XX_25519_ChaChaPoly_SHA256 = {
NOISE_PROTOCOL_NEEDS_LOCAL_STATIC,
Noise_XX_25519_ChaChaPoly_SHA256_Name,
"2",
Noise_XX_25519_ChaChaPoly_SHA256_createHandshake
};

View File

@ -40,13 +40,6 @@ private:
NoiseDHState_Curve25519 dh;
};
class Noise_XX_25519_ChaChaPoly_SHA256 : public NoiseProtocolDescriptor
{
public:
Noise_XX_25519_ChaChaPoly_SHA256();
virtual ~Noise_XX_25519_ChaChaPoly_SHA256();
NoiseHandshakeState *createHandshake() const;
};
extern const NoiseProtocolDescriptor Noise_XX_25519_ChaChaPoly_SHA256;
#endif