speck_asm.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. #include <stdint.h>
  2. #include "speck.h"
  3. #ifndef ARM
  4. #include <stdio.h>
  5. #endif
  6. void FuncER16(u16 *x, u16 *y, u16 k)
  7. {
  8. *x = (((*x)>>(7)) | ((*x)<<(16-(7))));
  9. *x += *y;
  10. *x = *x ^ k;
  11. *y = (((*y)<<(2)) | (*y>>(16-(2))));
  12. *y = *y ^ *x;
  13. }
  14. #ifdef ARM
  15. void FuncER16_ASM(u16 *x, u16 *y, u16 k)
  16. {
  17. asm volatile (
  18. "ldr r3, [r11, #-8]\n\t"
  19. "ldrh r3, [r3]\n\t"
  20. "lsr r3, r3, #7\n\t"
  21. "lsl r3, r3, #16\n\t"
  22. "lsr r3, r3, #16\n\t"
  23. "lsl r3, r3, #16\n\t"
  24. "asr r2, r3, #16\n\t"
  25. "ldr r3, [r11, #-8]\n\t"
  26. "ldrh r3, [r3]\n\t"
  27. "lsl r3, r3, #9\n\t"
  28. "lsl r3, r3, #16\n\t"
  29. "asr r3, r3, #16\n\t"
  30. "orr r3, r2, r3\n\t"
  31. "lsl r3, r3, #16\n\t"
  32. "asr r3, r3, #16\n\t"
  33. "lsl r3, r3, #16\n\t"
  34. "lsr r2, r3, #16\n\t"
  35. "ldr r3, [r11, #-8]\n\t"
  36. "strh r2, [r3]\n\t"
  37. "ldr r3, [r11, #-8]\n\t"
  38. "ldrh r2, [r3]\n\t"
  39. "ldr r3, [r11, #-12]\n\t"
  40. "ldrh r3, [r3]\n\t"
  41. "add r3, r2, r3\n\t"
  42. "lsl r3, r3, #16\n\t"
  43. "lsr r2, r3, #16\n\t"
  44. "ldr r3, [r11, #-8]\n\t"
  45. "strh r2, [r3]\n\t"
  46. "ldr r3, [r11, #-8]\n\t"
  47. "ldrh r2, [r3]\n\t"
  48. "ldrh r3, [r11, #-14]\n\t"
  49. "eor r3, r3, r2\n\t"
  50. "lsl r3, r3, #16\n\t"
  51. "lsr r2, r3, #16\n\t"
  52. "ldr r3, [r11, #-8]\n\t"
  53. "strh r2, [r3]\n\t"
  54. "ldr r3, [r11, #-12]\n\t"
  55. "ldrh r3, [r3]\n\t"
  56. "lsl r3, r3, #2\n\t"
  57. "lsl r3, r3, #16\n\t"
  58. "asr r2, r3, #16\n\t"
  59. "ldr r3, [r11, #-12]\n\t"
  60. "ldrh r3, [r3]\n\t"
  61. "lsr r3, r3, #14\n\t"
  62. "lsl r3, r3, #16\n\t"
  63. "lsr r3, r3, #16\n\t"
  64. "lsl r3, r3, #16\n\t"
  65. "asr r3, r3, #16\n\t"
  66. "orr r3, r2, r3\n\t"
  67. "lsl r3, r3, #16\n\t"
  68. "asr r3, r3, #16\n\t"
  69. "lsl r3, r3, #16\n\t"
  70. "lsr r2, r3, #16\n\t"
  71. "ldr r3, [r11, #-12]\n\t"
  72. "strh r2, [r3]\n\t"
  73. "ldr r3, [r11, #-12]\n\t"
  74. "ldrh r2, [r3]\n\t"
  75. "ldr r3, [r11, #-8]\n\t"
  76. "ldrh r3, [r3]\n\t"
  77. "eor r3, r3, r2\n\t"
  78. "lsl r3, r3, #16\n\t"
  79. "lsr r2, r3, #16\n\t"
  80. "ldr r3, [r11, #-12]\n\t"
  81. "strh r2, [r3]\n\t"
  82. );
  83. }
  84. #endif
  85. void Words16ToBytes(u16 words[],u8 bytes[],int numwords)
  86. {
  87. int i,j=0;
  88. for(i=0;i<numwords;i++){
  89. bytes[j]=(u8)words[i];
  90. bytes[j+1]=(u8)(words[i]>>8);
  91. j+=2;
  92. }
  93. }
  94. void BytesToWords16(u8 bytes[],u16 words[],int numbytes)
  95. {
  96. int i,j=0; for(i=0;i<numbytes/2;i++){
  97. words[i]=(u16)bytes[j] | ((u16)bytes[j+1]<<8);
  98. j+=2;
  99. }
  100. }
  101. void Speck3264KeySchedule(u16 K[],u16 rk[])
  102. {
  103. u16 i,D=K[3],C=K[2],B=K[1],A=K[0];
  104. for(i=0;i<22;){
  105. rk[i]=A;
  106. ER16(B,A,i++);
  107. rk[i]=A;
  108. ER16(C,A,i++);
  109. rk[i]=A;
  110. ER16(D,A,i++);
  111. }
  112. }
  113. void Speck3264Encrypt(u16 Pt[],u16 Ct[],u16 rk[])
  114. {
  115. u16 i;
  116. Ct[0]=Pt[0]; Ct[1]=Pt[1];
  117. // full 22 rounds
  118. for(i=0;i<22;) {
  119. //printf("ER16( c1=0x%x, c0=0x%x, k=0x%x ) = ", Ct[1], Ct[0], rk[i]);
  120. //
  121. #ifdef ARM
  122. FuncER16_ASM(&Ct[1], &Ct[0], rk[i++]);
  123. #else
  124. //ER16(Ct[1], Ct[0], rk[i++]);
  125. FuncER16(&Ct[1], &Ct[0], rk[i++]);
  126. printf("( c1=0x%x, c0=0x%x, k=0x%x )\n", Ct[1], Ct[0], rk[i]);
  127. #endif
  128. }
  129. }
  130. void Speck3264Decrypt(u16 Pt[],u16 Ct[],u16 rk[])
  131. {
  132. int i;
  133. Pt[0]=Ct[0]; Pt[1]=Ct[1];
  134. for(i=21;i>=0;) DR16(Pt[1],Pt[0],rk[i--]);
  135. }
  136. void Speck3264_EncryptBlock(u8 pt[], u8 k[], u8 ct[]) {
  137. uint16_t Pt[2] = {0};
  138. uint16_t K[4] = {0};
  139. uint16_t rk[34] = {0};
  140. uint16_t Ct[2] = {0};
  141. BytesToWords16(pt,Pt, 8);
  142. BytesToWords16(k,K, 16);
  143. Speck3264KeySchedule(K,rk);
  144. #ifndef ARM
  145. for (int i=0; i < 16; i++)
  146. {
  147. printf("Key: 0x%x\n", rk[i]);
  148. }
  149. #endif
  150. Speck3264Encrypt(Pt,Ct,rk);
  151. Words16ToBytes(Ct, ct, 2);
  152. }
  153. int main() {
  154. u8 pt[4] = {0x4c, 0x69, 0x74, 0x65};
  155. u8 key[8] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88};
  156. u8 ct[4] = {0x0};
  157. Speck3264_EncryptBlock(pt, key, ct);
  158. #ifndef ARM
  159. printf("[[ Speck 32/64 ]]\n");
  160. printf("The output: \n");
  161. for (int i = 0; i < 4; i++) {
  162. printf("- %08x\n", ct[i]);
  163. }
  164. #endif
  165. return 0;
  166. }