Browse Source

speck3264 for cw

Hans Martin 3 years ago
parent
commit
6f50dcff9d

+ 1 - 1
cw_firmware/makefile

@@ -11,7 +11,7 @@ CRYPTO_TARGET=NONE
 # List C source files here.
 # Header files (.h) are automatically pulled in.
 SRC += simple-speck.c
-SRC += speck.c
+SRC += speck3264.c
 
 # -----------------------------------------------------------------------------
 

+ 10 - 10
cw_firmware/simple-speck.c

@@ -5,16 +5,16 @@
 #include "simpleserial.h"
 #include "speck.h"
 
-u8 gkey[32] = {0x00};
+u8 gkey[8] = {0x00};
 
 
 uint8_t get_key(uint8_t* k, uint8_t len) {
-	simpleserial_put('o', 32, gkey);
+	simpleserial_put('o', 8, gkey);
 	return 0x00;
 }
 
 uint8_t set_key(uint8_t* key, uint8_t len) {
-    memcpy(gkey, key, 32);
+    memcpy(gkey, key, 8);
     return 0x00;
 }
 
@@ -40,14 +40,14 @@ uint8_t encrypt_block(uint8_t* pt, uint8_t len) {
 
     trigger_high(); // TRIGGER START
 
-    u8 key[32] = {0x00};
-    memcpy(key, gkey, 32); // copy the globally set encryption key
-    u8 ct[16] = {0x00};
+    u8 key[8] = {0x00};
+    memcpy(key, gkey, 8); // copy the globally set encryption key
+    u8 ct[4] = {0x00};
     EncryptBlock(pt, key, ct); // the encryption happens here
 
     trigger_low(); // TRIGGER STOP
 
-    simpleserial_put('c', 16, ct);
+    simpleserial_put('c', 4, ct);
     return 0x00;
 }
 
@@ -76,9 +76,9 @@ int main(void) {
 
 	simpleserial_init();
 	simpleserial_addcmd('p', 16, get_pt);
-	simpleserial_addcmd('e', 16, encrypt_block);
-	simpleserial_addcmd('k', 16, get_key);
-	simpleserial_addcmd('s', 32, set_key);
+	simpleserial_addcmd('e', 4, encrypt_block);
+	simpleserial_addcmd('k', 4, get_key);
+	simpleserial_addcmd('s', 8, set_key);
 	simpleserial_addcmd('x', 0, reset);
 
 	while(1)

+ 86 - 0
cw_firmware/simple-speck128256.c

@@ -0,0 +1,86 @@
+#include "hal.h"
+#include <stdint.h>
+#include <stdlib.h>
+
+#include "simpleserial.h"
+#include "speck.h"
+
+u8 gkey[32] = {0x00};
+
+
+uint8_t get_key(uint8_t* k, uint8_t len) {
+	simpleserial_put('o', 32, gkey);
+	return 0x00;
+}
+
+uint8_t set_key(uint8_t* key, uint8_t len) {
+    memcpy(gkey, key, 32);
+    return 0x00;
+}
+
+uint8_t get_pt(uint8_t* pt, uint8_t len) {
+	/**********************************
+	* Start user-specific code here. */
+	trigger_high();
+
+        // Only for testing purposes
+        volatile uint8_t testing_output[] = {0x42, 0x41, 0x41, 0x41,0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 };
+
+
+	trigger_low();
+	/* End user-specific code here. *
+	********************************/
+	simpleserial_put('r', 16, testing_output);
+
+	return 0x00;
+}
+
+
+uint8_t encrypt_block(uint8_t* pt, uint8_t len) {
+
+    trigger_high(); // TRIGGER START
+
+    u8 key[32] = {0x00};
+    memcpy(key, gkey, 32); // copy the globally set encryption key
+    u8 ct[16] = {0x00};
+    EncryptBlock(pt, key, ct); // the encryption happens here
+
+    trigger_low(); // TRIGGER STOP
+
+    simpleserial_put('c', 16, ct);
+    return 0x00;
+}
+
+
+uint8_t reset(uint8_t* x, uint8_t len) {
+	simpleserial_put('r', 0, NULL);
+	// Reset key here if needed
+	return 0x00;
+}
+
+
+int main(void) {
+    platform_init();
+	init_uart();
+	trigger_setup();
+
+ 	/* Uncomment this to get a HELLO message for debug */
+        /*
+	putch('h');
+	putch('e');
+	putch('l');
+	putch('l');
+	putch('o');
+	putch('\n');
+        */
+
+	simpleserial_init();
+	simpleserial_addcmd('p', 16, get_pt);
+	simpleserial_addcmd('e', 16, encrypt_block);
+	simpleserial_addcmd('k', 16, get_key);
+	simpleserial_addcmd('s', 32, set_key);
+	simpleserial_addcmd('x', 0, reset);
+
+	while(1)
+		simpleserial_get();
+}

BIN
cw_firmware/speck


+ 30 - 0
cw_firmware/speck.h

@@ -6,7 +6,25 @@
 #define ROTL64(x,r) (((x)<<(r)) | (x>>(64-(r))))
 #define ROTR64(x,r) (((x)>>(r)) | ((x)<<(64-(r))))
 
+// The same stuff for 32 bit
+
+#define ROTL32(x,r) (((x)<<(r)) | (x>>(32-(r))))
+#define ROTR32(x,r) (((x)>>(r)) | ((x)<<(32-(r))))
+
+#define ER32(x,y,k) (x=ROTR32(x,8), x+=y, x^=k, y=ROTL32(y,3), y^=x)
+#define DR32(x,y,k) (y^=x, y=ROTR32(y,3), x^=k, x-=y, x=ROTL32(x,8))
+
+// The same stuff for 16 bit
+
+#define ROTL16(x,r) (((x)<<(r)) | (x>>(16-(r))))
+#define ROTR16(x,r) (((x)>>(r)) | ((x)<<(16-(r))))
+
+#define ER16(x,y,k) (x=ROTR16(x,7), x+=y, x^=k, y=ROTL16(y,2), y^=x)
+#define DR16(x,y,k) (y^=x, y=ROTR16(y,2), x^=k, x-=y, x=ROTL16(x,7))
+
+
 #define u8 uint8_t
+#define u16 uint16_t
 #define u32 uint32_t
 #define u64 uint64_t
 void Words64ToBytes(u64 words[],u8 bytes[],int numwords);
@@ -16,4 +34,16 @@ void Speck128256Encrypt(u64 Pt[],u64 Ct[],u64 rk[]);
 void Speck128256Decrypt(u64 Pt[],u64 Ct[],u64 rk[]);
 void EncryptBlock(u8 pt[], u8 k[], u8 ct[]);
 
+
+// 32 bit versions functions
+void Words32ToBytes(u32 words[],u8 bytes[],int numwords);
+void BytesToWords32(u8 bytes[],u32 words[],int numbytes);
+
+
+void Words16ToBytes(u16 words[],u8 bytes[],int numwords);
+void BytesToWords16(u8 bytes[],u16 words[],int numbytes);
+
+void Speck3264KeySchedule(u16 K[],u16 rk[]);
+void Speck3264Encrypt(u16 Pt[],u16 Ct[],u16 rk[]);
+void Speck3264Decrypt(u16 Pt[],u16 Ct[],u16 rk[]);
 #endif

+ 0 - 0
cw_firmware/speck.c → cw_firmware/speck128256.c


+ 93 - 0
cw_firmware/speck3264.c

@@ -0,0 +1,93 @@
+#include <stdio.h>
+#include <stdint.h>
+#include "speck.h"
+
+
+void Words16ToBytes(u16 words[],u8 bytes[],int numwords)
+{
+    int i,j=0;
+    for(i=0;i<numwords;i++){
+        bytes[j]=(u8)words[i];
+        bytes[j+1]=(u8)(words[i]>>8);
+        j+=2;
+    }
+}
+
+void BytesToWords16(u8 bytes[],u16 words[],int numbytes)
+{
+    int i,j=0;
+    for(i=0;i<numbytes/2;i++){
+        words[i]=(u16)bytes[j] | ((u16)bytes[j+1]<<8);
+        j+=2;
+    }
+}
+
+void Speck3264KeySchedule(u16 K[],u16 rk[])
+{
+    u16 i,D=K[3],C=K[2],B=K[1],A=K[0];
+
+    for(i=0;i<22;){
+        rk[i]=A; ER16(B,A,i++);
+        rk[i]=A; ER16(C,A,i++);
+        rk[i]=A; ER16(D,A,i++);
+    }
+}
+
+
+void Speck3264Encrypt(u16 Pt[],u16 Ct[],u16 rk[])
+{
+    u16 i;
+    Ct[0]=Pt[0]; Ct[1]=Pt[1];
+
+    for(i=0;i<22;) ER16(Ct[1],Ct[0],rk[i++]);
+}
+
+
+void Speck3264Decrypt(u16 Pt[],u16 Ct[],u16 rk[])
+{
+    int i;
+    Pt[0]=Ct[0]; Pt[1]=Ct[1];
+
+    for(i=21;i>=0;) DR16(Pt[1],Pt[0],rk[i--]);
+}
+
+
+
+void EncryptBlock(u8 pt[], u8 k[], u8 ct[]) {
+
+    u16 Pt[2] = {0};
+    u16 K[4] = {0};
+    u16 rk[34] = {0};
+    u16 Ct[2] = {0};
+
+    BytesToWords16(pt,Pt,8);
+    BytesToWords16(k,K,16);
+
+    Speck3264KeySchedule(K,rk);
+
+    for (int i=0; i < 16; i++)
+    {
+        printf("Key: 0x%x\n", rk[i]);
+    }
+    Speck3264Encrypt(Pt,Ct,rk);
+    Words16ToBytes(Ct,ct,2);
+}
+
+
+int main() {
+
+    u8 key[8] = {0x1, 0x2,0x3,0x4,0x5,0x6,0x7,0x8};
+    u8 pt[4] = {0xde, 0xad, 0xbe, 0xef};
+    u8 ct[4] = {0x0};
+
+    EncryptBlock(pt, key, ct);
+    printf("[[ Speck 32/64  ]]\n");
+
+    printf("The output: \n");
+    for (int i = 0; i < 4; i++) {
+        printf("- %08x\n", ct[i]);
+    }
+    return 0;
+}
+
+