speck3264.c 3.5 KB

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