Merged silc_1_0_branch to trunk.
[silc.git] / lib / silccrypt / ciphers_def.h
1 /*
2
3   ciphers_def.h
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 1999 - 2000 Pekka Riikonen
8
9   This program is free software; you can redistribute it and/or modify
10   it under the terms of the GNU General Public License as published by
11   the Free Software Foundation; version 2 of the License.
12   
13   This program is distributed in the hope that it will be useful,
14   but WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   GNU General Public License for more details.
17
18 */
19
20 #ifndef CIPHERS_DEF_H
21 #define CIPHERS_DEF_H
22
23 /* General definitions for algorithms */
24 typedef unsigned char u1byte;
25 typedef SilcUInt32 u4byte;
26 typedef SilcUInt32 u32;
27
28 #define rotr(x, nr) (((x) >> ((int)(nr))) | ((x) << (32 - (int)(nr))))
29 #define rotl(x, nr) (((x) << ((int)(nr))) | ((x) >> (32 - (int)(nr))))
30 #define byte(x, nr) ((x) >> (nr * 8) & 255)
31
32 /* Byte key to words */
33 #define SILC_GET_WORD_KEY(s, d, len)            \
34 do {                                            \
35   int _i;                                       \
36   for (_i = 0; _i < (len / 8) / 4; _i++)        \
37     SILC_GET32_LSB(d[_i], s + (_i * 4));        \
38 } while(0);
39
40 /* CBC mode macros. */
41
42 #define SILC_CBC_GET_IV(d, s)                   \
43 do {                                            \
44   SILC_GET32_LSB(d[0], &s[0]);                  \
45   SILC_GET32_LSB(d[1], &s[4]);                  \
46   SILC_GET32_LSB(d[2], &s[8]);                  \
47   SILC_GET32_LSB(d[3], &s[12]);                 \
48 } while(0);
49
50 #define SILC_CBC_PUT_IV(s, d)                   \
51 do {                                            \
52   SILC_PUT32_LSB(s[0], &d[0]);                  \
53   SILC_PUT32_LSB(s[1], &d[4]);                  \
54   SILC_PUT32_LSB(s[2], &d[8]);                  \
55   SILC_PUT32_LSB(s[3], &d[12]);                 \
56 } while(0);
57
58 #define SILC_CBC_ENC_PRE(d, s)                  \
59 do {                                            \
60   SILC_GET32_X_LSB(d[0], &s[0]);                \
61   SILC_GET32_X_LSB(d[1], &s[4]);                \
62   SILC_GET32_X_LSB(d[2], &s[8]);                \
63   SILC_GET32_X_LSB(d[3], &s[12]);               \
64 } while(0);
65
66 #define SILC_CBC_ENC_POST(s, d, t)              \
67 do {                                            \
68   SILC_PUT32_LSB(s[0], &d[0]);                  \
69   SILC_PUT32_LSB(s[1], &d[4]);                  \
70   SILC_PUT32_LSB(s[2], &d[8]);                  \
71   SILC_PUT32_LSB(s[3], &d[12]);                 \
72                                                 \
73   d += 16;                                      \
74   t += 16;                                      \
75 } while(0);
76
77 #define SILC_CBC_DEC_PRE(d, s)                  \
78 do {                                            \
79   SILC_GET32_LSB(d[0], &s[0]);                  \
80   SILC_GET32_LSB(d[1], &s[4]);                  \
81   SILC_GET32_LSB(d[2], &s[8]);                  \
82   SILC_GET32_LSB(d[3], &s[12]);                 \
83 } while(0);
84
85 #define SILC_CBC_DEC_POST(s, d, p, t, siv)      \
86 do {                                            \
87   s[0] ^= siv[0];                               \
88   s[1] ^= siv[1];                               \
89   s[2] ^= siv[2];                               \
90   s[3] ^= siv[3];                               \
91                                                 \
92   SILC_PUT32_LSB(s[0], &d[0]);                  \
93   SILC_PUT32_LSB(s[1], &d[4]);                  \
94   SILC_PUT32_LSB(s[2], &d[8]);                  \
95   SILC_PUT32_LSB(s[3], &d[12]);                 \
96                                                 \
97   siv[0] = t[0];                                \
98   siv[1] = t[1];                                \
99   siv[2] = t[2];                                \
100   siv[3] = t[3];                                \
101                                                 \
102   d += 16;                                      \
103   p += 16;                                      \
104 } while(0);
105
106 #endif