2013-07-04 20:56:19 +00:00
|
|
|
/*
|
|
|
|
* Based on public domain code available at: http://cr.yp.to/snuffle.html
|
|
|
|
*
|
|
|
|
* This therefore is public domain.
|
|
|
|
*/
|
|
|
|
|
2013-12-07 00:49:20 +00:00
|
|
|
#ifndef ZT_SALSA20_HPP
|
|
|
|
#define ZT_SALSA20_HPP
|
2013-07-04 20:56:19 +00:00
|
|
|
|
2015-06-02 02:43:06 +00:00
|
|
|
#include <stdio.h>
|
2013-07-04 20:56:19 +00:00
|
|
|
#include <stdint.h>
|
2015-06-02 02:43:06 +00:00
|
|
|
#include <stdlib.h>
|
2017-04-17 23:43:03 +00:00
|
|
|
#include <string.h>
|
2013-09-17 19:53:59 +00:00
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
#include "Constants.hpp"
|
2015-10-14 17:14:07 +00:00
|
|
|
#include "Utils.hpp"
|
2013-07-04 20:56:19 +00:00
|
|
|
|
2019-09-13 18:16:21 +00:00
|
|
|
#ifndef ZT_SALSA20_SSE
|
|
|
|
#if (defined(__amd64) || defined(__amd64__) || defined(__x86_64) || defined(__x86_64__) || defined(__AMD64) || defined(__AMD64__) || defined(_M_X64))
|
|
|
|
#include <emmintrin.h>
|
2015-06-02 02:43:06 +00:00
|
|
|
#define ZT_SALSA20_SSE 1
|
|
|
|
#endif
|
2019-09-13 18:16:21 +00:00
|
|
|
#endif
|
2014-07-16 00:56:09 +00:00
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
namespace ZeroTier {
|
|
|
|
|
|
|
|
/**
|
2013-10-18 21:39:48 +00:00
|
|
|
* Salsa20 stream cipher
|
2013-07-04 20:56:19 +00:00
|
|
|
*/
|
|
|
|
class Salsa20
|
|
|
|
{
|
|
|
|
public:
|
2019-09-13 18:16:21 +00:00
|
|
|
ZT_ALWAYS_INLINE Salsa20() {}
|
|
|
|
ZT_ALWAYS_INLINE ~Salsa20() { Utils::burn(&_state,sizeof(_state)); }
|
2015-10-14 17:14:07 +00:00
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
/**
|
2017-04-17 23:43:03 +00:00
|
|
|
* @param key 256-bit (32 byte) key
|
2013-07-04 20:56:19 +00:00
|
|
|
* @param iv 64-bit initialization vector
|
|
|
|
*/
|
2019-09-13 18:16:21 +00:00
|
|
|
ZT_ALWAYS_INLINE Salsa20(const void *key,const void *iv) { init(key,iv); }
|
2013-07-04 20:56:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize cipher
|
|
|
|
*
|
|
|
|
* @param key Key bits
|
|
|
|
* @param iv 64-bit initialization vector
|
|
|
|
*/
|
2017-04-17 23:43:03 +00:00
|
|
|
void init(const void *key,const void *iv);
|
2013-07-04 20:56:19 +00:00
|
|
|
|
|
|
|
/**
|
2017-02-06 00:19:03 +00:00
|
|
|
* Encrypt/decrypt data using Salsa20/12
|
2013-07-04 20:56:19 +00:00
|
|
|
*
|
|
|
|
* @param in Input data
|
|
|
|
* @param out Output buffer
|
|
|
|
* @param bytes Length of data
|
|
|
|
*/
|
2017-04-17 23:43:03 +00:00
|
|
|
void crypt12(const void *in,void *out,unsigned int bytes);
|
2013-07-04 20:56:19 +00:00
|
|
|
|
2015-10-09 16:39:27 +00:00
|
|
|
/**
|
2017-02-06 00:19:03 +00:00
|
|
|
* Encrypt/decrypt data using Salsa20/20
|
2015-10-09 16:39:27 +00:00
|
|
|
*
|
|
|
|
* @param in Input data
|
|
|
|
* @param out Output buffer
|
|
|
|
* @param bytes Length of data
|
|
|
|
*/
|
2017-04-17 23:43:03 +00:00
|
|
|
void crypt20(const void *in,void *out,unsigned int bytes);
|
2015-10-09 16:39:27 +00:00
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
private:
|
2015-07-02 16:00:00 +00:00
|
|
|
union {
|
2014-07-16 00:56:09 +00:00
|
|
|
#ifdef ZT_SALSA20_SSE
|
|
|
|
__m128i v[4];
|
2014-09-16 15:53:18 +00:00
|
|
|
#endif // ZT_SALSA20_SSE
|
2014-07-16 00:56:09 +00:00
|
|
|
uint32_t i[16];
|
|
|
|
} _state;
|
2013-07-04 20:56:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace ZeroTier
|
|
|
|
|
|
|
|
#endif
|