Skip to content

Instantly share code, notes, and snippets.

@MewX
Last active September 1, 2017 10:45
Show Gist options
  • Save MewX/f306ff72797d1a62db3b7453e6926cc4 to your computer and use it in GitHub Desktop.
Save MewX/f306ff72797d1a62db3b7453e6926cc4 to your computer and use it in GitHub Desktop.
Measures on some algorithms for MSP430. (based on: https://github.com/kmarquet/bloc)
#include <msp430.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include "tools.h"
typedef uint16_t u16;
typedef uint8_t u8;
#define NBROUND 32
#define KSIZE 80
static const u8 S0[16] = {14, 9, 15, 0, 13, 4, 10, 11, 1, 2, 8, 3, 7, 6, 12, 5};
static const u8 S1[16] = {4, 11, 14, 9, 15, 13, 0, 10, 7, 12, 5, 6, 2, 8, 1, 3};
static const u8 S2[16] = {1, 14, 7, 12, 15, 13, 0, 6, 11, 5, 9, 3, 2, 4, 8, 10};
static const u8 S3[16] = {7, 6, 8, 11, 0, 15, 3, 14, 9, 10, 12, 13, 5, 2, 4, 1};
static const u8 S4[16] = {14, 5, 15, 0, 7, 2, 12, 13, 1, 8, 4, 9, 11, 10, 6, 3};
static const u8 S5[16] = {2, 13, 11, 12, 15, 14, 0, 9, 7, 10, 6, 3, 1, 8, 4, 5};
static const u8 S6[16] = {11, 9, 4, 14, 0, 15, 10, 13, 6, 12, 5, 7, 3, 8, 1, 2};
static const u8 S7[16] = {13, 10, 15, 0, 14, 4, 9, 11, 2, 1, 8, 3, 7, 5, 12, 6};
static const u8 S8[16] = {8, 7, 14, 5, 15, 13, 0, 6, 11, 12, 9, 10, 2, 4, 1, 3};
static const u8 S9[16] = {11, 5, 15, 0, 7, 2, 9, 13, 4, 8, 1, 12, 14, 10, 3, 6};
void Swap(u8 block[8])
{
u8 tmp[4];
tmp[0] = block[0];
tmp[1] = block[1];
tmp[2] = block[2];
tmp[3] = block[3];
block[0] = block[4];
block[1] = block[5];
block[2] = block[6];
block[3] = block[7];
block[4] = tmp[0];
block[5] = tmp[1];
block[6] = tmp[2];
block[7] = tmp[3];
}
void OneRound_Inv(u8 y[8], u8 k[4])
{
u8 t[4],tmp[4];
// FAIRE PASSER Y_0, Y_1, Y_2, Y_3 dans F
// AJOUT CLE
tmp[0]=y[4]^k[0];
tmp[1]=y[5]^k[1];
tmp[2]=y[6]^k[2];
tmp[3]=y[7]^k[3];
// PASSAGE DANS LES BOITES S
tmp[0] = ((S1[((tmp[0])>>4) & 0x0F])<<4)^S0[(tmp[0] & 0x0F)];
tmp[1] = ((S3[((tmp[1])>>4) & 0x0F])<<4)^S2[(tmp[1] & 0x0F)];
tmp[2] = ((S5[((tmp[2])>>4) & 0x0F])<<4)^S4[(tmp[2] & 0x0F)];
tmp[3] = ((S7[((tmp[3])>>4) & 0x0F])<<4)^S6[(tmp[3] & 0x0F)];
// PASSAGE DE LA PERMUTATION P
t[0] =((tmp[0]>>4) & 0x0F)^(tmp[1] & 0xF0);
t[1] = (tmp[0] & 0x0F) ^ ((tmp[1]& 0x0F)<<4);
t[2] = ((tmp[2]>>4) & 0x0F)^(tmp[3] & 0xF0);
t[3] = (tmp[2] & 0x0F) ^ ((tmp[3]& 0x0F)<<4);
// FIN DE LA FONCTION F
// PARTIE DROITE AVEC DECALAGE DE 8 SUR LA DROITE
tmp[0]= y[0]^t[0];
tmp[1]= y[1]^t[1];
tmp[2]= y[2]^t[2];
tmp[3]= y[3]^t[3];
// PARTIE GAUCHE
y[0]=tmp[1];
y[1]=tmp[2];
y[2]=tmp[3];
y[3]=tmp[0];
}
void __attribute__ ((noinline)) Decrypt(u8 x[8], u8 subkey[NBROUND][4])
{
int i;
OneRound_Inv(x, subkey[31]);
for(i=30; i>=0; i--)
{
Swap(x);
OneRound_Inv(x, subkey[i]);
}
}
int main()
{
// Stop WatchDog during initialization
WDTCTL = WDTPW + WDTHOLD;
u8 mkey[10];
u8 rkey[NBROUND][4];
mkey[0] = 0xdc;
mkey[1] = 0xfe;
mkey[2] = 0xef;
mkey[3] = 0xcd;
mkey[4] = 0xab;
mkey[5] = 0x89;
mkey[6] = 0x67;
mkey[7] = 0x45;
mkey[8] = 0x23;
mkey[9] = 0x01;
u8 state[8]={0xef, 0xcd, 0xab, 0x89, 0x67, 0x45, 0x23, 0x01};
START_DECRYPT();
Decrypt(state,rkey);
END_EXPE();
return 0;
}
#include <msp430.h>
#include <stdint.h>
typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;
typedef uint64_t u64;
#include "tools.h"
static const unsigned short sBox4[] = {
0x4, 0xF, 0x3, 0x8, 0xD, 0xA, 0xC, 0x0, 0xB, 0x5, 0x7, 0xE, 0x2, 0x6, 0x1, 0x9
};
static const unsigned short sBox4Inv[] = {
0x7, 0xE, 0xC, 0x2, 0x0, 0x9, 0xD, 0xA, 0x3, 0xF, 0x5, 0x8, 0x6, 0x4, 0xB, 0x1
};
void
AddRoundKey(u8* state, u8* key)
{
// ****************** addRoundkey *************************
state[3] ^= key[7];
state[2] ^= key[6];
state[1] ^= key[5];
state[0] ^= key[4];
// ****************** addRoundkey End *********************
}
void
Substitute(u8* state)
{
u8 temp_sBox;
// ******************* sBox *******************************
temp_sBox = 0;
temp_sBox |= sBox4[(state[0] & 0xF)];
temp_sBox |= (sBox4[(state[0]>>4)]) << 4;
state[0]=temp_sBox;
temp_sBox = 0;
temp_sBox |= sBox4[(state[1] & 0xF)];
temp_sBox |= (sBox4[(state[1]>>4)]) << 4;
state[1]=temp_sBox;
temp_sBox = 0;
temp_sBox |= sBox4[(state[2] & 0xF)];
temp_sBox |= (sBox4[(state[2]>>4)]) << 4;
state[2]=temp_sBox;
temp_sBox = 0;
temp_sBox |= sBox4[(state[3] & 0xF)];
temp_sBox |= (sBox4[(state[3]>>4)]) << 4;
state[3]=temp_sBox;
// ******************* sBox End ***************************
}
void
Mix(u8* state)
{
u8 n7 = state[0] & 0xF;
u8 n6 = state[0] >> 4;
u8 n5 = state[1] & 0xF;
u8 n4 = state[1] >> 4;
u8 n3 = state[2] & 0xF;
u8 n2 = state[2] >> 4;
u8 n1 = state[3] & 0xF;
u8 n0 = state[3] >> 4;
state[3] = ((n1 ^ n2 ^ n3 ^ n4 ^ n5 ^ n6) << 4) | (n0 ^ n2 ^ n3 ^ n5 ^ n6 ^ n7);
state[2] = ((n0 ^ n1 ^ n3 ^ n4 ^ n6 ^ n7) << 4) | (n0 ^ n1 ^ n2 ^ n4 ^ n5 ^ n7);
state[1] = ((n0 ^ n1 ^ n3 ^ n4 ^ n5) << 4) | (n0 ^ n1 ^ n2 ^ n5 ^ n6);
state[0] = ((n1 ^ n2 ^ n3 ^ n6 ^ n7) << 4) | (n0 ^ n2 ^ n3 ^ n4 ^ n7);
}
void
Permute(u8* state)
{
u8 n7 = state[0] & 0xF;
u8 n6 = state[0] >> 4;
u8 n5 = state[1] & 0xF;
u8 n4 = state[1] >> 4;
u8 n3 = state[2] & 0xF;
u8 n2 = state[2] >> 4;
u8 n1 = state[3] & 0xF;
u8 n0 = state[3] >> 4;
state[3] = (n2 << 4) | n0;
state[2] = (n3 << 4) | n6;
state[1] = (n7 << 4) | n4;
state[0] = (n5 << 4) | n1;
}
void
InvKeySchedule(u8* key, u8 round)
{
key[1] = ((((key[1] >> 3) & 0x1F) ^ round ) << 3) | (key[1] & 0x7);
key[7] = (sBox4Inv[(key[7] >> 4) & 0xF] << 4) | (key[7] & 0xF);
u64 key_64 = *((u64*) key);
u64 key_64r = ROTATE_LEFT_64(key_64, 15);
*((u64*) key) = key_64r;
}
void __attribute__ ((noinline)) Decrypt(u8 *state, u8 *key)
{
int round;
for(round=32; round >= 1; round--)
{
u32 state_L = *((u32*) (state + 4));
u32 state_R = *((u32*) state);
AddRoundKey(state, key);
Substitute(state);
Mix(state);
Permute(state);
*((u32*) state) = (*((u32*) state)) ^ state_L;
*((u32*) (state + 4)) = state_R;
InvKeySchedule(key, round);
}
return;
}
int main(void)
{
// Stop WatchDog during initialization
WDTCTL = WDTPW + WDTHOLD;
// Input values
//u8 state[8]={0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
u8 state[8]={0x00};
u8 key[8]={0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
//u8 key[8]={0};
START_DECRYPT();
Decrypt(state, key);
END_EXPE();
return 0;
}
#include <msp430.h>
#include <stdint.h>
typedef uint16_t u16;
typedef int16_t s16;
typedef uint8_t u8;
#include "tools.h"
static const u16 sBox[] = {0xe,0x4,0xb,0x2,0x3,0x8,0x0,0x9,0x1,0xa,0x7,0xf,0x6,0xc,0x5,0xd};
static const u16 mul2[] = {0x0,0x2,0x4,0x6,0x8,0xa,0xc,0xe,0x3,0x1,0x7,0x5,0xb,0x9,0xf,0xd};
static const u16 mul3[] = {0x0,0x3,0x6,0x5,0xc,0xf,0xa,0x9,0xb,0x8,0xd,0xe,0x7,0x4,0x1,0x2};
#define NBROUND 31
void Gr(u16 *state, const u16 *wk, const u16 *rk);
void Gr_1(u16 *state, const u16 *wk, const u16 *rk);
void RoundPermutation(u16 *chaine);
void wKS_128(const u16 *k, u16 *wkDest);
void rKS_128(const u16 *k, u16 *rkDest);
u16 FonctionF(u16 subState);
int main(int argc, char* argv[])
{
// Stop WatchDog during initialization
WDTCTL = WDTPW + WDTHOLD;
// ************ D閏larations ************
u16 text[4]={0xcdef,0x89ab,0x4567,0x0123};
u16 k128[8] = {0x0011, 0x2233, 0x4455, 0x6677, 0x8899, 0xaabb, 0xccdd, 0xeeff};
u16 wk[4];
u16 rk[2*NBROUND];
START_DECRYPT();
dec(text, k128, wk, rk);
END_EXPE();
return 0;
}
void __attribute__ ((noinline)) dec(u16 *text, u16 *k128, u16 *wk, u16 *rk) {
wKS_128(k128,wk);
rKS_128(k128,rk);
Gr_1(text, wk, rk);
}
// Fonction de chiffrement
void Gr(u16 *state, const u16 *wk, const u16 *rk)
{
u16 round;
// Premier Wk
state[3] ^= wk[0];
state[1] ^= wk[1];
for(round = 0; round <= (NBROUND - 2); round++)
{
state[2] ^= (FonctionF(state[3]) ^ rk[2 * round]);
state[0] ^= (FonctionF(state[1]) ^ rk[2 * round + 1]);
RoundPermutation(state);
}
state[2] ^= (FonctionF(state[3]) ^ rk[2 * NBROUND - 2]);
state[0] ^= (FonctionF(state[1]) ^ rk[2 * NBROUND - 1]);
// Deuxi鑝e Wk
state[3] ^= wk[2];
state[1] ^= wk[3];
return;
}
// Fonction de d閏hiffrement
void Gr_1(u16 *state, const u16 *wk, const u16 *rk)
{
u16 wk_1[4];
u16 rk_1[2*NBROUND];
u16 i;
wk_1[0] = wk[2];
wk_1[1] = wk[3];
wk_1[2] = wk[0];
wk_1[3] = wk[1];
for(i=0; i<NBROUND; i++)
{
rk_1[2*i] = rk[2*NBROUND - 2*i - 2 + (i%2)];
rk_1[2*i+1] = rk[2*NBROUND - 2*i -1 - (i%2)];
}
Gr(state,wk_1,rk_1);
return;
}
void RoundPermutation(u16 *chaine)
{
u16 temp_0 = chaine[0];
u16 temp_2 = chaine[2];
chaine[0]= (chaine[3] & 0xFF00) | (chaine[1] & 0x00FF);
chaine[2]= (chaine[1] & 0xFF00) | (chaine[3] & 0x00FF);
chaine[1]= (temp_0 & 0xFF00) | (temp_2 & 0x00FF);
chaine[3]= (temp_2 & 0xFF00) | (temp_0 & 0x00FF);
return;
}
void wKS_128(const u16 *k, u16 *wkDest)
{
wkDest[0] = (k[1] & 0x00FF) | (k[0] & 0xFF00);
wkDest[1] = (k[0] & 0x00FF) | (k[1] & 0xFF00);
wkDest[2] = (k[7] & 0x00FF) | (k[4] & 0xFF00);
wkDest[3] = (k[4] & 0x00FF) | (k[7] & 0xFF00);
return;
}
void rKS_128(const u16 *k, u16 *rkDest)
{
u16 i;
u16 tmp;
u16 con[2];
u16 kTemp[8]={
kTemp[0] = k[0],
kTemp[1] = k[1],
kTemp[2] = k[2],
kTemp[3] = k[3],
kTemp[4] = k[4],
kTemp[5] = k[5],
kTemp[6] = k[6],
kTemp[7] = k[7]};
for (i=0; i<2*NBROUND; i++)
{
if ( (i&0x7) == 0x6)
{
tmp=kTemp[3];
kTemp[3] = kTemp[7];
kTemp[7] = kTemp[5];
kTemp[5] = tmp;
tmp=kTemp[0];
kTemp[0] = kTemp[2];
kTemp[2] = kTemp[6];
kTemp[6] = kTemp[4];
kTemp[4] = tmp;
}
tmp = (((i/2) + 1) << 10) | ((i/2) + 1);
con[0]= tmp ^ 0xA98B;
con[1]= (tmp<<1) ^ 0x6547;
rkDest[i] = kTemp[(i+2)&0x7]^con[(~i)&1];
}
return;
}
u16 FonctionF(u16 subState)
{
u16 subState0=sBox[subState&0xF];
u16 subState1=sBox[(subState>>=4)&0xF];
u16 subState2=sBox[(subState>>=4)&0xF];
u16 subState3=sBox[(subState>>=4)&0xF];
u16 temp_0=mul3[subState3]^subState2^subState1^mul2[subState0];
u16 temp_1=subState3^subState2^mul2[subState1]^mul3[subState0];
u16 temp_2=subState3^mul2[subState2]^mul3[subState1]^subState0;
u16 temp_3=mul2[subState3]^mul3[subState2]^subState1^subState0;
subState=sBox[temp_3];
subState<<=4;
subState^=sBox[temp_2];
subState<<=4;
subState^=sBox[temp_1];
subState<<=4;
subState^=sBox[temp_0];
return subState;
}
#include <msp430.h>
#include "tools.h"
#include <stdint.h>
typedef uint16_t u16;
#define SIZE 96//n : plaintext size, key size. k*6B
#define B 16 //processor (or word) size.
#define NB (SIZE/(2*B)) //nb = n/2b : number of words per Feistel branch.
#define NBROUND 51 // odd number
#define MASK 0xFFFF
/**********************************************************************/
void XOR(u16 x[NB],u16 y[NB])
{
u16 i;
for(i=0;i<NB;i++)
{
x[i]^=y[i];
}
return;
}
void Sub(u16 x[NB])
{
u16 i;
for(i=0;i<(SIZE/(6*B));i++)
{
x[3*i] ^= x[3*i+1] & x[3*i+2];
x[3*i+1] ^= x[3*i] & x[3*i+2];
x[3*i+2] ^= x[3*i+1] | x[3*i];
}
return;
}
void WordRot(u16 x[NB])
{
u16 i;
u16 temp=x[NB-1];
for(i=NB-1;i>0;i--)
{
x[i]=x[i-1];
}
x[0]=temp;
return;
}
void InvWordRot(u16 x[NB])
{
u16 i;
u16 temp=x[0];
for(i=0;i<NB-1;i++)
{
x[i]=x[i+1];
}
x[NB-1]=temp;
return;
}
void BitRot(u16 x[NB])
{
u16 i;
for(i=0;i<NB/3;i++)
{
x[3*i]=(x[3*i]>>1)^(x[3*i]<<(B-1));
x[3*i+2]=(x[3*i+2]<<1)^(x[3*i+2]>>(B-1));
}
return;
}
void Add(u16 x[NB],u16 y[NB])
{
u16 i;
for(i=0;i<NB;i++)
{
x[i]=(x[i]+y[i])&MASK;
}
return;
}
/**********************************************************************/
void fk(u16 kr[NB],u16 kl[NB],u16 krDest[NB],u16 klDest[NB],u16 c[NB])
{
u16 i;
for(i=0;i<NB;i++) krDest[i]=kr[i];
for(i=0;i<NB;i++) klDest[i]=kr[i];
Add(krDest,c);
Sub(krDest);
BitRot(krDest);
WordRot(krDest);
XOR(krDest,kl);
return;
}
void fd(u16 r[NB],u16 l[NB],u16 k[NB])
{
u16 temp[NB],i;
for(i=0;i<NB;i++) temp[i]=r[i];
Add(r,k);
Sub(r);
BitRot(r);
XOR(r,l);
InvWordRot(r);
for(i=0;i<NB;i++) l[i]=temp[i];
return;
}
void KeySchedul(u16 mkey[2*NB],u16 rkey[NBROUND][2*NB])
{
u16 i,j,temp,c[NB];
for(i=1;i<NB;i++) c[i]=0;
for(i=0;i<2*NB;i++) rkey[0][i]=mkey[i];
for(i=1;i<=(NBROUND>>2);i++)
{
c[0]=i;
//[KLi , KRi ] = FK (KLi−1 , KRi−1 , C(i));
fk(rkey[i-1],rkey[i-1]+3,rkey[i],rkey[i]+3,c);
}
for(j=0;j<NB;j++)
{
temp=rkey[NBROUND>>2][j];
rkey[NBROUND>>2][j]=rkey[NBROUND>>2][j+3];
rkey[NBROUND>>2][j+3]=temp;
}
for(;i<NBROUND;i++)
{
c[0]=NBROUND-i;
//[KLi , KRi ] = FK (KLi−1 , KRi−1 , C(r − i));
fk(rkey[i-1],rkey[i-1]+3,rkey[i],rkey[i]+3,c);
}
return;
}
void Decrypt(u16 state[2*NB],u16 rkey[NBROUND][2*NB])
{
u16 i,temp;
for(i=NBROUND;i>((NBROUND+1)>>2);i--)
{
fd(state,state+3,rkey[i-1]+3);
}
for(;i>=1;i--)
{
fd(state,state+3,rkey[i-1]);
}
for(i=0;i<NB;i++)
{
temp=state[i];
state[i]=state[i+3];
state[i+3]=temp;
}
return;
}
void __attribute__ ((noinline)) dec(u16 *mkey, u16 *rkey, u16 *state) {
KeySchedul(mkey,rkey);
Decrypt(state,rkey);
}
int main()
{
// Stop WatchDog during initialization
WDTCTL = WDTPW + WDTHOLD;
u16 i;
u16 state[2*NB];
u16 mkey[2*NB];
u16 rkey[NBROUND][2*NB];
for(i=0;i<2*NB;i++) state[i]=i;
for(i=0;i<2*NB;i++) mkey[i]=2*i;
START_DECRYPT();
dec(mkey, rkey, state);
END_EXPE();
return 0;
}
/*
Copyright (c) 2016, Moritz Bitsch
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <stdint.h>
#include <msp430.h>
#define MASK24 0xFFFFFF
#define MASK48 0xFFFFFFFFFFFF
#define SPECK_64_128
#ifdef SPECK_64_128
#define SPECK_TYPE uint32_t
#define SPECK_ROUNDS 27
#define SPECK_KEY_LEN 4
#endif
#define ROR(x, r) ((x >> r) | (x << ((sizeof(SPECK_TYPE) * 8) - r)))
#define ROL(x, r) ((x << r) | (x >> ((sizeof(SPECK_TYPE) * 8) - r)))
#define R(x, y, k) (x = ROR(x, 8), x += y, x ^= k, y = ROL(y, 3), y ^= x)
#define RR(x, y, k) (y ^= x, y = ROR(y, 3), x ^= k, x -= y, x = ROL(x, 8))
void speck_expand(SPECK_TYPE const K[SPECK_KEY_LEN], SPECK_TYPE S[SPECK_ROUNDS])
{
SPECK_TYPE i, b = K[0];
SPECK_TYPE a[SPECK_KEY_LEN - 1];
for (i = 0; i < (SPECK_KEY_LEN - 1); i++)
{
a[i] = K[i + 1];
}
S[0] = b;
for (i = 0; i < SPECK_ROUNDS - 1; i++) {
R(a[i % (SPECK_KEY_LEN - 1)], b, i);
S[i + 1] = b;
}
}
void speck_decrypt(SPECK_TYPE const ct[2], SPECK_TYPE pt[2], SPECK_TYPE const K[SPECK_ROUNDS])
{
SPECK_TYPE i;
pt[0]=ct[0]; pt[1]=ct[1];
for(i = 0; i < SPECK_ROUNDS; i++){
RR(pt[1], pt[0], K[(SPECK_ROUNDS - 1) - i]);
}
}
void speck_decrypt_combined(SPECK_TYPE const ct[2], SPECK_TYPE pt[2], SPECK_TYPE const K[SPECK_KEY_LEN])
{
int i;
SPECK_TYPE b = K[0];
SPECK_TYPE a[SPECK_KEY_LEN - 1];
pt[0]=ct[0]; pt[1]=ct[1];
for (i = 0; i < (SPECK_KEY_LEN - 1); i++)
{
a[i] = K[i + 1];
}
for (i = 0; i < SPECK_ROUNDS - 1; i++)
{
R(a[i % (SPECK_KEY_LEN - 1)], b, i);
}
for(i = 0; i < SPECK_ROUNDS; i++){
RR(pt[1], pt[0], b);
RR(a[((SPECK_ROUNDS - 2) - i) % (SPECK_KEY_LEN - 1)], b, ((SPECK_ROUNDS - 2) - i));
}
}
int main(int argc, char** argv)
{
// Stop WatchDog during initialization
WDTCTL = WDTPW + WDTHOLD;
#ifdef SPECK_64_128
uint32_t key[4] = {0x03020100, 0x0b0a0908, 0x13121110, 0x1b1a1918};
uint32_t plain[2] = {0x7475432d, 0x3b726574};
uint32_t enc[2] = {0x454e028b, 0x8c6fa548};
#endif
SPECK_TYPE buffer[2] = {0};
SPECK_TYPE exp[SPECK_ROUNDS];
speck_expand(key, exp);
speck_decrypt(enc, buffer, exp);
asm(" NOP");
return 0;
}
/** @file main.c
* @brief XTEA encode
*
* @author Yang Su, Auto-ID Lab, The University of Adelaide
*/
/**
* as a unique ID.
*/
#include<stdint.h>
#include <msp430.h>
//void XTEAdec(uint32_t * v,uint32_t *k,int N){
// uint32_t y=v[0],z=v[1],DELTA=0x9e3779b9;
// if(N>0){
// uint32_t sum = DELTA*N;
// int i = 0;
// for(i = 0;i < N;i++){
// z -= ((y<<4 ^ y>>5) + y) ^ (sum + k[sum>>11 &3]);
// sum -= DELTA;
// y -= ((z<<4 ^ z>>5) + z) ^ (sum + k[sum&3]);
// }
// }
// v[0] = y, v[1] = z;
//}
/* XTEA is a version of slightly improved tea.
The plain or cypher text is in v[0], v[1].
The key is in k[n], where n = 0 - 3,
The number of coding cycles is given by N and
the number of decoding cycles given by -N */
void
tean(uint32_t *v, uint32_t *k,uint32_t ncycles) /* replaces TEA's code and decode */
{
register uint32_t
y = v[0],
z = v[1],
DELTA = 0x9e3779b9,
sum, A,B;
sum = DELTA * (ncycles);
sum = 0x9e3779b9 * 64;
while (sum)
{ z -= (y << 4 ^ y >> 5) + y ^ sum + k[sum >> 11 & 3];
sum -= DELTA;
y -= (z << 4 ^ z >> 5) + z ^ sum + k[sum & 3];
}
v[0] = y;
v[1] = z;
return;
}
void main(void)
{
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
PM5CTL0 &= ~LOCKLPM5; // Lock LPM5.
uint32_t text[2];
text[0] = 0x4853224D;
text[1] = 0x5F50DB40;
uint32_t key[4];
key[0] = 0x17485418;
key[1] = 0x42465615;
key[2] = 0x90872153;
key[3] = 0x99887765;
tean(text,key,64);
asm(" NOP");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment