1
mirror of https://github.com/hashcat/hashcat synced 2024-11-28 05:21:38 +01:00
hashcat/OpenCL/inc_cipher_des.cl
2019-03-24 00:26:10 +01:00

234 lines
6.6 KiB
Common Lisp

/**
* Author......: See docs/credits.txt
* License.....: MIT
*/
#include "inc_vendor.h"
#include "inc_types.h"
#include "inc_common.h"
#include "inc_cipher_des.h"
#define PERM_OP(a,b,n,m) \
{ \
u32x t; \
t = a >> n; \
t = t ^ b; \
t = t & m; \
b = b ^ t; \
t = t << n; \
a = a ^ t; \
}
#define HPERM_OP(a,n,m) \
{ \
u32x t; \
t = a << (16 + n); \
t = t ^ a; \
t = t & m; \
a = a ^ t; \
t = t >> (16 + n); \
a = a ^ t; \
}
#define DES_IP(l,r) \
{ \
PERM_OP (r, l, 4, 0x0f0f0f0f); \
PERM_OP (l, r, 16, 0x0000ffff); \
PERM_OP (r, l, 2, 0x33333333); \
PERM_OP (l, r, 8, 0x00ff00ff); \
PERM_OP (r, l, 1, 0x55555555); \
}
#define DES_FP(l,r) \
{ \
PERM_OP (l, r, 1, 0x55555555); \
PERM_OP (r, l, 8, 0x00ff00ff); \
PERM_OP (l, r, 2, 0x33333333); \
PERM_OP (r, l, 16, 0x0000ffff); \
PERM_OP (l, r, 4, 0x0f0f0f0f); \
}
#if VECT_SIZE == 1
#define DES_BOX(i,n,S) (S)[(n)][(i)]
#elif VECT_SIZE == 2
#define DES_BOX(i,n,S) (u32x) ((S)[(n)][(i).s0], (S)[(n)][(i).s1])
#elif VECT_SIZE == 4
#define DES_BOX(i,n,S) (u32x) ((S)[(n)][(i).s0], (S)[(n)][(i).s1], (S)[(n)][(i).s2], (S)[(n)][(i).s3])
#elif VECT_SIZE == 8
#define DES_BOX(i,n,S) (u32x) ((S)[(n)][(i).s0], (S)[(n)][(i).s1], (S)[(n)][(i).s2], (S)[(n)][(i).s3], (S)[(n)][(i).s4], (S)[(n)][(i).s5], (S)[(n)][(i).s6], (S)[(n)][(i).s7])
#elif VECT_SIZE == 16
#define DES_BOX(i,n,S) (u32x) ((S)[(n)][(i).s0], (S)[(n)][(i).s1], (S)[(n)][(i).s2], (S)[(n)][(i).s3], (S)[(n)][(i).s4], (S)[(n)][(i).s5], (S)[(n)][(i).s6], (S)[(n)][(i).s7], (S)[(n)][(i).s8], (S)[(n)][(i).s9], (S)[(n)][(i).sa], (S)[(n)][(i).sb], (S)[(n)][(i).sc], (S)[(n)][(i).sd], (S)[(n)][(i).se], (S)[(n)][(i).sf])
#endif
DECLSPEC void _des_crypt_encrypt (u32x *iv, u32x *data, u32x *Kc, u32x *Kd, SHM_TYPE u32 (*s_SPtrans)[64])
{
u32x r = data[0];
u32x l = data[1];
DES_IP (r, l);
r = hc_rotl32 (r, 3u);
l = hc_rotl32 (l, 3u);
for (u32 i = 0; i < 16; i += 2)
{
u32x u;
u32x t;
u = Kc[i + 0] ^ r;
t = Kd[i + 0] ^ hc_rotl32 (r, 28u);
l ^= DES_BOX (((u >> 2) & 0x3f), 0, s_SPtrans)
| DES_BOX (((u >> 10) & 0x3f), 2, s_SPtrans)
| DES_BOX (((u >> 18) & 0x3f), 4, s_SPtrans)
| DES_BOX (((u >> 26) & 0x3f), 6, s_SPtrans)
| DES_BOX (((t >> 2) & 0x3f), 1, s_SPtrans)
| DES_BOX (((t >> 10) & 0x3f), 3, s_SPtrans)
| DES_BOX (((t >> 18) & 0x3f), 5, s_SPtrans)
| DES_BOX (((t >> 26) & 0x3f), 7, s_SPtrans);
u = Kc[i + 1] ^ l;
t = Kd[i + 1] ^ hc_rotl32 (l, 28u);
r ^= DES_BOX (((u >> 2) & 0x3f), 0, s_SPtrans)
| DES_BOX (((u >> 10) & 0x3f), 2, s_SPtrans)
| DES_BOX (((u >> 18) & 0x3f), 4, s_SPtrans)
| DES_BOX (((u >> 26) & 0x3f), 6, s_SPtrans)
| DES_BOX (((t >> 2) & 0x3f), 1, s_SPtrans)
| DES_BOX (((t >> 10) & 0x3f), 3, s_SPtrans)
| DES_BOX (((t >> 18) & 0x3f), 5, s_SPtrans)
| DES_BOX (((t >> 26) & 0x3f), 7, s_SPtrans);
}
l = hc_rotl32 (l, 29u);
r = hc_rotl32 (r, 29u);
DES_FP (r, l);
iv[0] = l;
iv[1] = r;
}
DECLSPEC void _des_crypt_decrypt (u32x *iv, u32x *data, u32x *Kc, u32x *Kd, SHM_TYPE u32 (*s_SPtrans)[64])
{
u32x r = data[0];
u32x l = data[1];
DES_IP (r, l);
r = hc_rotl32 (r, 3u);
l = hc_rotl32 (l, 3u);
for (u32 i = 16; i > 0; i -= 2)
{
u32x u;
u32x t;
u = Kc[i - 1] ^ r;
t = Kd[i - 1] ^ hc_rotl32 (r, 28u);
l ^= DES_BOX (((u >> 2) & 0x3f), 0, s_SPtrans)
| DES_BOX (((u >> 10) & 0x3f), 2, s_SPtrans)
| DES_BOX (((u >> 18) & 0x3f), 4, s_SPtrans)
| DES_BOX (((u >> 26) & 0x3f), 6, s_SPtrans)
| DES_BOX (((t >> 2) & 0x3f), 1, s_SPtrans)
| DES_BOX (((t >> 10) & 0x3f), 3, s_SPtrans)
| DES_BOX (((t >> 18) & 0x3f), 5, s_SPtrans)
| DES_BOX (((t >> 26) & 0x3f), 7, s_SPtrans);
u = Kc[i - 2] ^ l;
t = Kd[i - 2] ^ hc_rotl32 (l, 28u);
r ^= DES_BOX (((u >> 2) & 0x3f), 0, s_SPtrans)
| DES_BOX (((u >> 10) & 0x3f), 2, s_SPtrans)
| DES_BOX (((u >> 18) & 0x3f), 4, s_SPtrans)
| DES_BOX (((u >> 26) & 0x3f), 6, s_SPtrans)
| DES_BOX (((t >> 2) & 0x3f), 1, s_SPtrans)
| DES_BOX (((t >> 10) & 0x3f), 3, s_SPtrans)
| DES_BOX (((t >> 18) & 0x3f), 5, s_SPtrans)
| DES_BOX (((t >> 26) & 0x3f), 7, s_SPtrans);
}
l = hc_rotl32 (l, 29u);
r = hc_rotl32 (r, 29u);
DES_FP (r, l);
iv[0] = l;
iv[1] = r;
}
DECLSPEC void _des_crypt_keysetup (u32x c, u32x d, u32x *Kc, u32x *Kd, SHM_TYPE u32 (*s_skb)[64])
{
PERM_OP (d, c, 4, 0x0f0f0f0f);
HPERM_OP (c, 2, 0xcccc0000);
HPERM_OP (d, 2, 0xcccc0000);
PERM_OP (d, c, 1, 0x55555555);
PERM_OP (c, d, 8, 0x00ff00ff);
PERM_OP (d, c, 1, 0x55555555);
d = ((d & 0x000000ff) << 16)
| ((d & 0x0000ff00) << 0)
| ((d & 0x00ff0000) >> 16)
| ((c & 0xf0000000) >> 4);
c = c & 0x0fffffff;
for (u32 i = 0; i < 16; i++)
{
if ((i < 2) || (i == 8) || (i == 15))
{
c = ((c >> 1) | (c << 27));
d = ((d >> 1) | (d << 27));
}
else
{
c = ((c >> 2) | (c << 26));
d = ((d >> 2) | (d << 26));
}
c = c & 0x0fffffff;
d = d & 0x0fffffff;
const u32x c00 = (c >> 0) & 0x0000003f;
const u32x c06 = (c >> 6) & 0x00383003;
const u32x c07 = (c >> 7) & 0x0000003c;
const u32x c13 = (c >> 13) & 0x0000060f;
const u32x c20 = (c >> 20) & 0x00000001;
u32x s = DES_BOX (((c00 >> 0) & 0xff), 0, s_skb)
| DES_BOX (((c06 >> 0) & 0xff)
|((c07 >> 0) & 0xff), 1, s_skb)
| DES_BOX (((c13 >> 0) & 0xff)
|((c06 >> 8) & 0xff), 2, s_skb)
| DES_BOX (((c20 >> 0) & 0xff)
|((c13 >> 8) & 0xff)
|((c06 >> 16) & 0xff), 3, s_skb);
const u32x d00 = (d >> 0) & 0x00003c3f;
const u32x d07 = (d >> 7) & 0x00003f03;
const u32x d21 = (d >> 21) & 0x0000000f;
const u32x d22 = (d >> 22) & 0x00000030;
u32x t = DES_BOX (((d00 >> 0) & 0xff), 4, s_skb)
| DES_BOX (((d07 >> 0) & 0xff)
|((d00 >> 8) & 0xff), 5, s_skb)
| DES_BOX (((d07 >> 8) & 0xff), 6, s_skb)
| DES_BOX (((d21 >> 0) & 0xff)
|((d22 >> 0) & 0xff), 7, s_skb);
Kc[i] = ((t << 16) | (s & 0x0000ffff));
Kd[i] = ((s >> 16) | (t & 0xffff0000));
Kc[i] = hc_rotl32 (Kc[i], 2u);
Kd[i] = hc_rotl32 (Kd[i], 2u);
}
}
#undef PERM_OP
#undef HPERM_OP
#undef DES_IP
#undef DES_FP
#undef DES_BOX