speck3264.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. #include <stdio.h>
  2. #include <stdint.h>
  3. #include "speck.h"
  4. #include "helper.h"
  5. // This function is only used for the "x86" Speck compilation and as reference
  6. void FuncER16(u16 *x, u16 *y, u16 k)
  7. {
  8. u16 tmp_x = *x;
  9. u16 tmp_y = *y;
  10. *x = (((tmp_x)>>(7)) | ((tmp_x)<<(16-(7))));
  11. *x += *y;
  12. //*x = *x ^ k;
  13. *x = XOR(*x, k, *x);
  14. *y = (((tmp_y)<<(2)) | (tmp_y>>(16-(2))));
  15. *y = XOR(*y, *x, *y);
  16. //*y = *y ^ *x;
  17. }
  18. #ifdef ARM
  19. // This function is used when running on the CW
  20. void FuncER16_ASM(u16 *x, u16 *y, u16 k)
  21. {
  22. asm volatile (
  23. "nop\n\t"
  24. "push {r4, r5, lr}\n\t"
  25. "ldrh r5, [r0, #0]\n\t"
  26. "ldrh r4, [r1, #0]\n\t"
  27. "lsls r3, r5, #9\n\t"
  28. "orr.w r3, r3, r5, lsr #7\n\t"
  29. "uxth r3, r3\n\t"
  30. "strh r3, [r0, #0]\n\t"
  31. "ldrh r5, [r1, #0]\n\t"
  32. "add r3, r5\n\t"
  33. "eors r2, r3\n\t"
  34. "lsls r3, r4, #2\n\t"
  35. "orr.w r3, r3, r4, lsr #14\n\t"
  36. "uxth r3, r3\n\t"
  37. "strh r2, [r0, #0]\n\t"
  38. "strh r3, [r1, #0]\n\t"
  39. "ldrh r2, [r0, #0]\n\t"
  40. "eors r3, r2\n\t"
  41. "strh r3, [r1, #0]\n\t"
  42. "pop {r4, r5, pc}\n\t"
  43. );
  44. }
  45. #endif
  46. void Words16ToBytes(u16 words[],u8 bytes[],int numwords)
  47. {
  48. int i,j=0;
  49. for(i=0;i<numwords;i++){
  50. bytes[j]=(u8)words[i];
  51. bytes[j+1]=(u8)(words[i]>>8);
  52. j+=2;
  53. }
  54. }
  55. void BytesToWords16(u8 bytes[],u16 words[],int numbytes)
  56. {
  57. int i,j=0; for(i=0;i<numbytes/2;i++){
  58. words[i]=(u16)bytes[j] | ((u16)bytes[j+1]<<8);
  59. j+=2;
  60. }
  61. }
  62. void Speck3264KeySchedule(u16 K[],u16 rk[])
  63. {
  64. u16 i,D=K[3],C=K[2],B=K[1],A=K[0];
  65. #ifdef ARM
  66. for(i=0;i<22;){
  67. rk[i]=A;
  68. //ER16(B,A,i++);
  69. FuncER16(&B,&A,i++);
  70. rk[i]=A;
  71. //ER16(C,A,i++);
  72. FuncER16(&C,&A,i++);
  73. rk[i]=A;
  74. //ER16(D,A,i++);
  75. FuncER16(&D,&A,i++);
  76. }
  77. #endif
  78. #ifndef ARM
  79. for(i=0;i<22;){
  80. printf("A = 0x%x ; B = 0x%x ; C = 0x%x ; D = 0x%x\n", A, B, C, D);
  81. rk[i]=A;
  82. //ER16(B,A,i++);
  83. FuncER16(&B, &A, i++);
  84. printf("rk[%d] = 0x%x\n", i-1, A);
  85. printf("A = 0x%x ; B = 0x%x ; C = 0x%x ; D = 0x%x\n", A, B, C, D);
  86. rk[i]=A;
  87. //ER16(C,A,i++);
  88. FuncER16(&C, &A, i++);
  89. printf("rk[%d] = 0x%x\n", i-1, A);
  90. printf("A = 0x%x ; B = 0x%x ; C = 0x%x ; D = 0x%x\n", A, B, C, D);
  91. rk[i]=A;
  92. //ER16(D,A,i++);
  93. FuncER16(&D, &A, i++);
  94. printf("rk[%d] = 0x%x\n <- D = 0x%x", i-1, A, D);
  95. printf("----------------------\n");
  96. }
  97. #endif
  98. }
  99. void Speck3264Encrypt(u16 Pt[],u16 Ct[],u16 rk[])
  100. {
  101. u16 i;
  102. Ct[0]=Pt[0]; Ct[1]=Pt[1];
  103. // full 22 rounds
  104. for(i=0;i<22;) {
  105. //ER16(Ct[1],Ct[0],rk[i++]);
  106. #ifdef ARM
  107. //FuncER16_ASM(&Ct[1], &Ct[0],rk[i++]);
  108. FuncER16(&Ct[1], &Ct[0], rk[i++]);
  109. #else
  110. FuncER16(&Ct[1], &Ct[0], rk[i++]);
  111. //ER16(Ct[1],Ct[0],rk[i++]);
  112. #endif
  113. }
  114. }
  115. void Speck3264Decrypt(u16 Pt[],u16 Ct[],u16 rk[])
  116. {
  117. int i;
  118. Pt[0]=Ct[0]; Pt[1]=Ct[1];
  119. for(i=21;i>=0;) DR16(Pt[1],Pt[0],rk[i--]);
  120. }
  121. void Speck3264_EncryptBlock(u8 pt[], u8 k[], u8 ct[]) {
  122. u16 Pt[2] = {0};
  123. u16 K[4] = {0};
  124. u16 rk[34] = {0};
  125. u16 Ct[2] = {0};
  126. BytesToWords16(pt,Pt,8);
  127. BytesToWords16(k,K,16);
  128. Speck3264KeySchedule(K,rk);
  129. #ifndef ARM
  130. // DEBUG Purposes
  131. for (int i=0; i < 16; i++)
  132. {
  133. printf("Key: 0x%x\n", rk[i]);
  134. }
  135. #endif
  136. Speck3264Encrypt(Pt,Ct,rk);
  137. Words16ToBytes(Ct,ct,2);
  138. }
  139. #ifndef ARM
  140. int main() {
  141. // test are from https://github.com/inmcm/Simon_Speck_Ciphers
  142. //u8 key[8] = {0x00, 0x01, 0x08, 0x09, 0x10, 0x11, 0x18, 0x19};
  143. u8 pt[4] = {0x4c, 0x69, 0x74, 0x65};
  144. u8 key[8] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88};
  145. u8 ct[4] = {0x0};
  146. Speck3264_EncryptBlock(pt, key, ct);
  147. printf("[[ Speck 32/64 ]]\n");
  148. printf("The output: \n");
  149. for (int i = 0; i < 4; i++) {
  150. printf("- %08x\n", ct[i]);
  151. }
  152. return 0;
  153. }
  154. #endif