speck3264.c 3.6 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. );
  41. }
  42. #endif
  43. void Words16ToBytes(u16 words[],u8 bytes[],int numwords)
  44. {
  45. int i,j=0;
  46. for(i=0;i<numwords;i++){
  47. bytes[j]=(u8)words[i];
  48. bytes[j+1]=(u8)(words[i]>>8);
  49. j+=2;
  50. }
  51. }
  52. void BytesToWords16(u8 bytes[],u16 words[],int numbytes)
  53. {
  54. int i,j=0; for(i=0;i<numbytes/2;i++){
  55. words[i]=(u16)bytes[j] | ((u16)bytes[j+1]<<8);
  56. j+=2;
  57. }
  58. }
  59. void Speck3264KeySchedule(u16 K[],u16 rk[])
  60. {
  61. u16 i,D=K[3],C=K[2],B=K[1],A=K[0];
  62. #ifdef ARM
  63. for(i=0;i<22;){
  64. rk[i]=A;
  65. ER16(B,A,i++);
  66. rk[i]=A;
  67. ER16(C,A,i++);
  68. rk[i]=A;
  69. ER16(D,A,i++);
  70. }
  71. #endif
  72. #ifndef ARM
  73. for(i=0;i<22;){
  74. printf("A = 0x%x ; B = 0x%x ; C = 0x%x ; D = 0x%x\n", A, B, C, D);
  75. rk[i]=A;
  76. ER16(B,A,i++);
  77. printf("rk[%d] = 0x%x\n", i-1, A);
  78. printf("A = 0x%x ; B = 0x%x ; C = 0x%x ; D = 0x%x\n", A, B, C, D);
  79. rk[i]=A;
  80. ER16(C,A,i++);
  81. printf("rk[%d] = 0x%x\n", i-1, A);
  82. printf("A = 0x%x ; B = 0x%x ; C = 0x%x ; D = 0x%x\n", A, B, C, D);
  83. rk[i]=A;
  84. ER16(D,A,i++);
  85. printf("rk[%d] = 0x%x\n <- D = 0x%x", i-1, A, D);
  86. printf("----------------------\n");
  87. }
  88. #endif
  89. }
  90. void Speck3264Encrypt(u16 Pt[],u16 Ct[],u16 rk[])
  91. {
  92. u16 i;
  93. Ct[0]=Pt[0]; Ct[1]=Pt[1];
  94. // full 22 rounds
  95. for(i=0;i<22;) {
  96. //ER16(Ct[1],Ct[0],rk[i++]);
  97. #ifdef ARM
  98. FuncER16_ASM(&Ct[1], &Ct[0],rk[i++]);
  99. //FuncER16(&Ct[1], &Ct[0], rk[i++]);
  100. #else
  101. ER16(Ct[1],Ct[0],rk[i++]);
  102. #endif
  103. }
  104. }
  105. void Speck3264Decrypt(u16 Pt[],u16 Ct[],u16 rk[])
  106. {
  107. int i;
  108. Pt[0]=Ct[0]; Pt[1]=Ct[1];
  109. for(i=21;i>=0;) DR16(Pt[1],Pt[0],rk[i--]);
  110. }
  111. void Speck3264_EncryptBlock(u8 pt[], u8 k[], u8 ct[]) {
  112. u16 Pt[2] = {0};
  113. u16 K[4] = {0};
  114. u16 rk[34] = {0};
  115. u16 Ct[2] = {0};
  116. BytesToWords16(pt,Pt,8);
  117. BytesToWords16(k,K,16);
  118. Speck3264KeySchedule(K,rk);
  119. #ifndef ARM
  120. // DEBUG Purposes
  121. for (int i=0; i < 16; i++)
  122. {
  123. printf("Key: 0x%x\n", rk[i]);
  124. }
  125. #endif
  126. Speck3264Encrypt(Pt,Ct,rk);
  127. Words16ToBytes(Ct,ct,2);
  128. }
  129. #ifndef ARM
  130. int main() {
  131. // test are from https://github.com/inmcm/Simon_Speck_Ciphers
  132. //u8 key[8] = {0x00, 0x01, 0x08, 0x09, 0x10, 0x11, 0x18, 0x19};
  133. u8 pt[4] = {0x4c, 0x69, 0x74, 0x65};
  134. u8 key[8] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88};
  135. u8 ct[4] = {0x0};
  136. Speck3264_EncryptBlock(pt, key, ct);
  137. printf("[[ Speck 32/64 ]]\n");
  138. printf("The output: \n");
  139. for (int i = 0; i < 4; i++) {
  140. printf("- %08x\n", ct[i]);
  141. }
  142. return 0;
  143. }
  144. #endif