des-sse2.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /**
  2. * Author......: Jens Steube <jens.steube@gmail.com>
  3. * License.....: MIT
  4. */
  5. #define PERM_OP(a,b,tt,n,m) \
  6. { \
  7. tt = a >> n; \
  8. tt = tt ^ b; \
  9. tt = tt & m; \
  10. b = b ^ tt; \
  11. tt = tt << n; \
  12. a = a ^ tt; \
  13. }
  14. #define HPERM_OP(a,tt,n,m) \
  15. { \
  16. tt = a << (16 + n); \
  17. tt = tt ^ a; \
  18. tt = tt & m; \
  19. a = a ^ tt; \
  20. tt = tt >> (16 + n); \
  21. a = a ^ tt; \
  22. }
  23. #define IP(l,r,tt) \
  24. { \
  25. PERM_OP (r, l, tt, 4, 0x0f0f0f0f); \
  26. PERM_OP (l, r, tt, 16, 0x0000ffff); \
  27. PERM_OP (r, l, tt, 2, 0x33333333); \
  28. PERM_OP (l, r, tt, 8, 0x00ff00ff); \
  29. PERM_OP (r, l, tt, 1, 0x55555555); \
  30. }
  31. #define FP(l,r,tt) \
  32. { \
  33. PERM_OP (l, r, tt, 1, 0x55555555); \
  34. PERM_OP (r, l, tt, 8, 0x00ff00ff); \
  35. PERM_OP (l, r, tt, 2, 0x33333333); \
  36. PERM_OP (r, l, tt, 16, 0x0000ffff); \
  37. PERM_OP (l, r, tt, 4, 0x0f0f0f0f); \
  38. }
  39. #define BOX(v,i,S) (S)[(i)][(v)]
  40. void _des_keysetup (uint32_t data[2], uint32_t Kc[16], uint32_t Kd[16], const uint s_skb[8][64])
  41. {
  42. uint32_t c = data[0];
  43. uint32_t d = data[1];
  44. uint32_t tt;
  45. PERM_OP (d, c, tt, 4, 0x0f0f0f0f);
  46. HPERM_OP (c, tt, 2, 0xcccc0000);
  47. HPERM_OP (d, tt, 2, 0xcccc0000);
  48. PERM_OP (d, c, tt, 1, 0x55555555);
  49. PERM_OP (c, d, tt, 8, 0x00ff00ff);
  50. PERM_OP (d, c, tt, 1, 0x55555555);
  51. d = ((d & 0x000000ff) << 16)
  52. | ((d & 0x0000ff00) << 0)
  53. | ((d & 0x00ff0000) >> 16)
  54. | ((c & 0xf0000000) >> 4);
  55. c = c & 0x0fffffff;
  56. int i;
  57. for (i = 0; i < 16; i++)
  58. {
  59. const uint shifts3s0[16] = { 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1 };
  60. const uint shifts3s1[16] = { 27, 27, 26, 26, 26, 26, 26, 26, 27, 26, 26, 26, 26, 26, 26, 27 };
  61. c = c >> shifts3s0[i] | c << shifts3s1[i];
  62. d = d >> shifts3s0[i] | d << shifts3s1[i];
  63. c = c & 0x0fffffff;
  64. d = d & 0x0fffffff;
  65. uint32_t s = BOX ((( c >> 0) & 0x3f), 0, s_skb)
  66. | BOX ((((c >> 6) & 0x03)
  67. | ((c >> 7) & 0x3c)), 1, s_skb)
  68. | BOX ((((c >> 13) & 0x0f)
  69. | ((c >> 14) & 0x30)), 2, s_skb)
  70. | BOX ((((c >> 20) & 0x01)
  71. | ((c >> 21) & 0x06)
  72. | ((c >> 22) & 0x38)), 3, s_skb);
  73. uint32_t t = BOX ((( d >> 0) & 0x3f), 4, s_skb)
  74. | BOX ((((d >> 7) & 0x03)
  75. | ((d >> 8) & 0x3c)), 5, s_skb)
  76. | BOX ((((d >> 15) & 0x3f)), 6, s_skb)
  77. | BOX ((((d >> 21) & 0x0f)
  78. | ((d >> 22) & 0x30)), 7, s_skb);
  79. Kc[i] = ((t << 16) | (s & 0x0000ffff));
  80. Kd[i] = ((s >> 16) | (t & 0xffff0000));
  81. Kc[i] = ROTL32 (Kc[i], 2u);
  82. Kd[i] = ROTL32 (Kd[i], 2u);
  83. }
  84. }
  85. void _des_encrypt (uint32_t data[2], uint32_t Kc[16], uint32_t Kd[16], const uint s_SPtrans[8][64])
  86. {
  87. uint32_t r = data[0];
  88. uint32_t l = data[1];
  89. uint32_t tt;
  90. IP (r, l, tt);
  91. r = ROTL32 (r, 3u);
  92. l = ROTL32 (l, 3u);
  93. int i;
  94. for (i = 0; i < 16; i++)
  95. {
  96. uint32_t u = Kc[i] ^ r;
  97. uint32_t t = Kd[i] ^ ROTL32 (r, 28u);
  98. l ^= BOX (((u >> 2) & 0x3f), 0, s_SPtrans)
  99. | BOX (((u >> 10) & 0x3f), 2, s_SPtrans)
  100. | BOX (((u >> 18) & 0x3f), 4, s_SPtrans)
  101. | BOX (((u >> 26) & 0x3f), 6, s_SPtrans)
  102. | BOX (((t >> 2) & 0x3f), 1, s_SPtrans)
  103. | BOX (((t >> 10) & 0x3f), 3, s_SPtrans)
  104. | BOX (((t >> 18) & 0x3f), 5, s_SPtrans)
  105. | BOX (((t >> 26) & 0x3f), 7, s_SPtrans);
  106. tt = l;
  107. l = r;
  108. r = tt;
  109. }
  110. l = ROTL32 (l, 29u);
  111. r = ROTL32 (r, 29u);
  112. FP (r, l, tt);
  113. data[0] = l;
  114. data[1] = r;
  115. }