Created
October 31, 2017 13:44
-
-
Save bit-hack/6d45ff104530f1c371f06ce2240ab8b8 to your computer and use it in GitHub Desktop.
xtea
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include <cstdint> | |
struct xtea_t | |
{ | |
static const uint32_t _delta = 0x9E3779B9; | |
typedef uint32_t key_t[4]; | |
template <uint32_t num_rounds> | |
void encipher(uint32_t v[2], const key_t &key) | |
{ | |
uint32_t v0 = v[0], v1 = v[1], sum = 0; | |
for (uint32_t i = 0; i < num_rounds; i++) { | |
v0 += (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + key[3 & sum]); | |
sum += _delta; | |
v1 += (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + key[3 & (sum >> 11)]); | |
} | |
v[0] = v0, v[1] = v1; | |
} | |
template <uint32_t num_rounds> | |
void decipher(uint32_t v[2], const key_t &key) | |
{ | |
uint32_t v0 = v[0], v1 = v[1], sum = delta * num_rounds; | |
for (uint32_t i = 0; i < num_rounds; i++) { | |
v1 -= (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + key[3 & (sum >> 11)]); | |
sum -= _delta; | |
v0 -= (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + key[3 & sum]); | |
} | |
v[0] = v0, v[1] = v1; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment