File: ipcrypt.c

package info (click to toggle)
pdns 5.0.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 10,824 kB
  • sloc: cpp: 101,240; sh: 5,616; makefile: 2,318; sql: 860; ansic: 675; python: 635; yacc: 245; perl: 161; lex: 131
file content (87 lines) | stat: -rw-r--r-- 1,891 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87

#include "ipcrypt.h"

#define ROTL(X, R) (X) = (unsigned char) ((X) << (R)) | ((X) >> (8 - (R)))

static void
arx_fwd(unsigned char state[4])
{
    state[0] += state[1];
    state[2] += state[3];
    ROTL(state[1], 2);
    ROTL(state[3], 5);
    state[1] ^= state[0];
    state[3] ^= state[2];
    ROTL(state[0], 4);
    state[0] += state[3];
    state[2] += state[1];
    ROTL(state[1], 3);
    ROTL(state[3], 7);
    state[1] ^= state[2];
    state[3] ^= state[0];
    ROTL(state[2], 4);
}

static void
arx_bwd(unsigned char state[4])
{
    ROTL(state[2], 4);
    state[1] ^= state[2];
    state[3] ^= state[0];
    ROTL(state[1], 5);
    ROTL(state[3], 1);
    state[0] -= state[3];
    state[2] -= state[1];
    ROTL(state[0], 4);
    state[1] ^= state[0];
    state[3] ^= state[2];
    ROTL(state[1], 6);
    ROTL(state[3], 3);
    state[0] -= state[1];
    state[2] -= state[3];
}

static inline void
xor4(unsigned char *out, const unsigned char *x, const unsigned char *y)
{
    out[0] = x[0] ^ y[0];
    out[1] = x[1] ^ y[1];
    out[2] = x[2] ^ y[2];
    out[3] = x[3] ^ y[3];
}

int
ipcrypt_encrypt(unsigned char out[IPCRYPT_BYTES],
                const unsigned char in[IPCRYPT_BYTES],
                const unsigned char key[IPCRYPT_KEYBYTES])
{
    unsigned char state[4];

    xor4(state, in, key);
    arx_fwd(state);
    xor4(state, state, key + 4);
    arx_fwd(state);
    xor4(state, state, key + 8);
    arx_fwd(state);
    xor4(out, state, key + 12);

    return 0;
}

int
ipcrypt_decrypt(unsigned char out[IPCRYPT_BYTES],
                const unsigned char in[IPCRYPT_BYTES],
                const unsigned char key[IPCRYPT_KEYBYTES])
{
    unsigned char state[4];

    xor4(state, in, key + 12);
    arx_bwd(state);
    xor4(state, state, key + 8);
    arx_bwd(state);
    xor4(state, state, key + 4);
    arx_bwd(state);
    xor4(out, state, key);

    return 0;
}