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
|
|
|
|
2022-01-04 23:11:08 +00:00
|
|
|
#if (!defined(ZT_SALSA20_SSE)) && (defined(__SSE2__) || (defined(__WINDOWS__) && !defined(__MINGW32__)))
|
2015-06-02 02:43:06 +00:00
|
|
|
#define ZT_SALSA20_SSE 1
|
|
|
|
#endif
|
|
|
|
|
2014-07-16 00:56:09 +00:00
|
|
|
#ifdef ZT_SALSA20_SSE
|
|
|
|
#include <emmintrin.h>
|
2014-09-16 15:53:18 +00:00
|
|
|
#endif // ZT_SALSA20_SSE
|
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:
|
2017-04-17 23:43:03 +00:00
|
|
|
Salsa20() {}
|
2015-10-14 17:14:07 +00:00
|
|
|
~Salsa20() { Utils::burn(&_state,sizeof(_state)); }
|
|
|
|
|
2017-04-20 16:33:35 +00:00
|
|
|
/**
|
|
|
|
* XOR d with s
|
|
|
|
*
|
|
|
|
* This is done efficiently using e.g. SSE if available. It's used when
|
|
|
|
* alternative Salsa20 implementations are used in Packet and is here
|
|
|
|
* since this is where all the SSE stuff is already included.
|
|
|
|
*
|
|
|
|
* @param d Destination to XOR
|
|
|
|
* @param s Source bytes to XOR with destination
|
|
|
|
* @param len Length of s and d
|
|
|
|
*/
|
|
|
|
static inline void memxor(uint8_t *d,const uint8_t *s,unsigned int len)
|
|
|
|
{
|
|
|
|
#ifdef ZT_SALSA20_SSE
|
2017-05-03 14:43:23 +00:00
|
|
|
while (len >= 128) {
|
|
|
|
__m128i s0 = _mm_loadu_si128(reinterpret_cast<const __m128i *>(s));
|
|
|
|
__m128i s1 = _mm_loadu_si128(reinterpret_cast<const __m128i *>(s + 16));
|
|
|
|
__m128i s2 = _mm_loadu_si128(reinterpret_cast<const __m128i *>(s + 32));
|
|
|
|
__m128i s3 = _mm_loadu_si128(reinterpret_cast<const __m128i *>(s + 48));
|
|
|
|
__m128i s4 = _mm_loadu_si128(reinterpret_cast<const __m128i *>(s + 64));
|
|
|
|
__m128i s5 = _mm_loadu_si128(reinterpret_cast<const __m128i *>(s + 80));
|
|
|
|
__m128i s6 = _mm_loadu_si128(reinterpret_cast<const __m128i *>(s + 96));
|
|
|
|
__m128i s7 = _mm_loadu_si128(reinterpret_cast<const __m128i *>(s + 112));
|
|
|
|
__m128i d0 = _mm_loadu_si128(reinterpret_cast<__m128i *>(d));
|
|
|
|
__m128i d1 = _mm_loadu_si128(reinterpret_cast<__m128i *>(d + 16));
|
|
|
|
__m128i d2 = _mm_loadu_si128(reinterpret_cast<__m128i *>(d + 32));
|
|
|
|
__m128i d3 = _mm_loadu_si128(reinterpret_cast<__m128i *>(d + 48));
|
|
|
|
__m128i d4 = _mm_loadu_si128(reinterpret_cast<__m128i *>(d + 64));
|
|
|
|
__m128i d5 = _mm_loadu_si128(reinterpret_cast<__m128i *>(d + 80));
|
|
|
|
__m128i d6 = _mm_loadu_si128(reinterpret_cast<__m128i *>(d + 96));
|
|
|
|
__m128i d7 = _mm_loadu_si128(reinterpret_cast<__m128i *>(d + 112));
|
|
|
|
d0 = _mm_xor_si128(d0,s0);
|
|
|
|
d1 = _mm_xor_si128(d1,s1);
|
|
|
|
d2 = _mm_xor_si128(d2,s2);
|
|
|
|
d3 = _mm_xor_si128(d3,s3);
|
|
|
|
d4 = _mm_xor_si128(d4,s4);
|
|
|
|
d5 = _mm_xor_si128(d5,s5);
|
|
|
|
d6 = _mm_xor_si128(d6,s6);
|
|
|
|
d7 = _mm_xor_si128(d7,s7);
|
|
|
|
_mm_storeu_si128(reinterpret_cast<__m128i *>(d),d0);
|
|
|
|
_mm_storeu_si128(reinterpret_cast<__m128i *>(d + 16),d1);
|
|
|
|
_mm_storeu_si128(reinterpret_cast<__m128i *>(d + 32),d2);
|
|
|
|
_mm_storeu_si128(reinterpret_cast<__m128i *>(d + 48),d3);
|
|
|
|
_mm_storeu_si128(reinterpret_cast<__m128i *>(d + 64),d4);
|
|
|
|
_mm_storeu_si128(reinterpret_cast<__m128i *>(d + 80),d5);
|
|
|
|
_mm_storeu_si128(reinterpret_cast<__m128i *>(d + 96),d6);
|
|
|
|
_mm_storeu_si128(reinterpret_cast<__m128i *>(d + 112),d7);
|
|
|
|
s += 128;
|
|
|
|
d += 128;
|
|
|
|
len -= 128;
|
|
|
|
}
|
2017-04-20 16:33:35 +00:00
|
|
|
while (len >= 16) {
|
|
|
|
_mm_storeu_si128(reinterpret_cast<__m128i *>(d),_mm_xor_si128(_mm_loadu_si128(reinterpret_cast<__m128i *>(d)),_mm_loadu_si128(reinterpret_cast<const __m128i *>(s))));
|
|
|
|
s += 16;
|
|
|
|
d += 16;
|
|
|
|
len -= 16;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#ifndef ZT_NO_TYPE_PUNNING
|
|
|
|
while (len >= 16) {
|
|
|
|
(*reinterpret_cast<uint64_t *>(d)) ^= (*reinterpret_cast<const uint64_t *>(s));
|
|
|
|
s += 8;
|
|
|
|
d += 8;
|
|
|
|
(*reinterpret_cast<uint64_t *>(d)) ^= (*reinterpret_cast<const uint64_t *>(s));
|
|
|
|
s += 8;
|
|
|
|
d += 8;
|
|
|
|
len -= 16;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif
|
2017-05-03 14:43:23 +00:00
|
|
|
while (len) {
|
|
|
|
--len;
|
2017-04-20 16:33:35 +00:00
|
|
|
*(d++) ^= *(s++);
|
2017-05-03 14:43:23 +00:00
|
|
|
}
|
2017-04-20 16:33:35 +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
|
|
|
|
*/
|
2017-04-17 23:43:03 +00:00
|
|
|
Salsa20(const void *key,const void *iv)
|
2013-07-04 20:56:19 +00:00
|
|
|
{
|
2017-04-17 23:43:03 +00:00
|
|
|
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
|