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>
|
2013-09-17 19:53:59 +00:00
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
#include "Constants.hpp"
|
|
|
|
|
2015-06-02 02:43:06 +00:00
|
|
|
#if (!defined(ZT_SALSA20_SSE)) && (defined(__SSE2__) || defined(__WINDOWS__))
|
|
|
|
#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:
|
|
|
|
Salsa20() throw() {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param key Key bits
|
|
|
|
* @param kbits Number of key bits: 128 or 256 (recommended)
|
|
|
|
* @param iv 64-bit initialization vector
|
2013-10-18 21:39:48 +00:00
|
|
|
* @param rounds Number of rounds: 8, 12, or 20
|
2013-07-04 20:56:19 +00:00
|
|
|
*/
|
2013-10-18 21:39:48 +00:00
|
|
|
Salsa20(const void *key,unsigned int kbits,const void *iv,unsigned int rounds)
|
2013-07-04 20:56:19 +00:00
|
|
|
throw()
|
|
|
|
{
|
2013-10-18 21:39:48 +00:00
|
|
|
init(key,kbits,iv,rounds);
|
2013-07-04 20:56:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize cipher
|
|
|
|
*
|
|
|
|
* @param key Key bits
|
|
|
|
* @param kbits Number of key bits: 128 or 256 (recommended)
|
|
|
|
* @param iv 64-bit initialization vector
|
2013-10-18 21:39:48 +00:00
|
|
|
* @param rounds Number of rounds: 8, 12, or 20
|
2013-07-04 20:56:19 +00:00
|
|
|
*/
|
2013-10-18 21:39:48 +00:00
|
|
|
void init(const void *key,unsigned int kbits,const void *iv,unsigned int rounds)
|
2013-07-04 20:56:19 +00:00
|
|
|
throw();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Encrypt data
|
|
|
|
*
|
|
|
|
* @param in Input data
|
|
|
|
* @param out Output buffer
|
|
|
|
* @param bytes Length of data
|
|
|
|
*/
|
|
|
|
void encrypt(const void *in,void *out,unsigned int bytes)
|
|
|
|
throw();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Decrypt data
|
|
|
|
*
|
|
|
|
* @param in Input data
|
|
|
|
* @param out Output buffer
|
|
|
|
* @param bytes Length of data
|
|
|
|
*/
|
|
|
|
inline void decrypt(const void *in,void *out,unsigned int bytes)
|
|
|
|
throw()
|
|
|
|
{
|
|
|
|
encrypt(in,out,bytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2015-09-30 21:35:05 +00:00
|
|
|
unsigned int _roundsDiv4;
|
2013-07-04 20:56:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace ZeroTier
|
|
|
|
|
|
|
|
#endif
|