speck3264.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #include <stdio.h>
  2. #include <stdint.h>
  3. #include "speck.h"
  4. void Words16ToBytes(u16 words[],u8 bytes[],int numwords)
  5. {
  6. int i,j=0;
  7. for(i=0;i<numwords;i++){
  8. bytes[j]=(u8)words[i];
  9. bytes[j+1]=(u8)(words[i]>>8);
  10. j+=2;
  11. }
  12. }
  13. void BytesToWords16(u8 bytes[],u16 words[],int numbytes)
  14. {
  15. int i,j=0; for(i=0;i<numbytes/2;i++){
  16. words[i]=(u16)bytes[j] | ((u16)bytes[j+1]<<8);
  17. j+=2;
  18. }
  19. }
  20. void Speck3264KeySchedule(u16 K[],u16 rk[])
  21. {
  22. u16 i,D=K[3],C=K[2],B=K[1],A=K[0];
  23. for(i=0;i<22;){
  24. rk[i]=A;
  25. ER16(B,A,i++);
  26. rk[i]=A;
  27. ER16(C,A,i++);
  28. rk[i]=A;
  29. ER16(D,A,i++);
  30. }
  31. /* DEBUG
  32. for(i=0;i<22;){
  33. printf("A = 0x%x ; B = 0x%x ; C = 0x%x ; D = 0x%x\n", A, B, C, D);
  34. rk[i]=A;
  35. ER16(B,A,i++);
  36. printf("rk[%d] = 0x%x\n", i-1, A);
  37. printf("A = 0x%x ; B = 0x%x ; C = 0x%x ; D = 0x%x\n", A, B, C, D);
  38. rk[i]=A;
  39. ER16(C,A,i++);
  40. printf("rk[%d] = 0x%x\n", i-1, A);
  41. printf("A = 0x%x ; B = 0x%x ; C = 0x%x ; D = 0x%x\n", A, B, C, D);
  42. rk[i]=A;
  43. ER16(D,A,i++);
  44. printf("rk[%d] = 0x%x\n <- D = 0x%x", i-1, A, D);
  45. printf("----------------------\n");
  46. }
  47. */
  48. }
  49. void Speck3264Encrypt(u16 Pt[],u16 Ct[],u16 rk[])
  50. {
  51. u16 i;
  52. Ct[0]=Pt[0]; Ct[1]=Pt[1];
  53. // full 22 rounds
  54. // for(i=0;i<22;) ;
  55. //er16(ct[1],ct[0],rk[0]);
  56. for(i=0;i<22;) {
  57. //er16(ct[1],ct[0],0xdead);
  58. ER16(Ct[1],Ct[0],rk[i++]);
  59. #ifndef ARM
  60. printf("( c1=0x%x, c0=0x%x, k=0x%x )\n", Ct[1], Ct[0], rk[i]);
  61. #endif
  62. }
  63. }
  64. void Speck3264Decrypt(u16 Pt[],u16 Ct[],u16 rk[])
  65. {
  66. int i;
  67. Pt[0]=Ct[0]; Pt[1]=Ct[1];
  68. for(i=21;i>=0;) DR16(Pt[1],Pt[0],rk[i--]);
  69. }
  70. void Speck3264_EncryptBlock(u8 pt[], u8 k[], u8 ct[]) {
  71. u16 Pt[2] = {0};
  72. u16 K[4] = {0};
  73. u16 rk[34] = {0};
  74. u16 Ct[2] = {0};
  75. BytesToWords16(pt,Pt,8);
  76. BytesToWords16(k,K,16);
  77. Speck3264KeySchedule(K,rk);
  78. // DEBUG Purposes
  79. #ifndef ARM
  80. for (int i=0; i < 16; i++)
  81. {
  82. printf("Key: 0x%x\n", rk[i]);
  83. }
  84. #endif
  85. Speck3264Encrypt(Pt,Ct,rk);
  86. Words16ToBytes(Ct,ct,2);
  87. }
  88. #ifndef ARM
  89. int main() {
  90. // test are from https://github.com/inmcm/Simon_Speck_Ciphers
  91. //u8 key[8] = {0x00, 0x01, 0x08, 0x09, 0x10, 0x11, 0x18, 0x19};
  92. u8 pt[4] = {0x4c, 0x69, 0x74, 0x65};
  93. u8 key[8] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88};
  94. u8 ct[4] = {0x0};
  95. Speck3264_EncryptBlock(pt, key, ct);
  96. printf("[[ Speck 32/64 ]]\n");
  97. printf("The output: \n");
  98. for (int i = 0; i < 4; i++) {
  99. printf("- %08x\n", ct[i]);
  100. }
  101. return 0;
  102. }
  103. #endif