Merge branch 'topic/mm-fixes' of git://208.110.73.182/silc into silc.1.1.branch
[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 - 2006 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 typedef SilcUInt32 uint_32t;
28 typedef SilcUInt8 uint_8t;
29
30 #define rotr(x, nr) (((x) >> ((int)(nr))) | ((x) << (32 - (int)(nr))))
31 #define rotl(x, nr) (((x) << ((int)(nr))) | ((x) >> (32 - (int)(nr))))
32 #define byte(x, nr) ((x) >> (nr * 8) & 255)
33
34 /* Byte key to words */
35 #define SILC_GET_WORD_KEY(s, d, len)            \
36 do {                                            \
37   int _i;                                       \
38   for (_i = 0; _i < (len / 8) / 4; _i++)        \
39     SILC_GET32_LSB(d[_i], s + (_i * 4));        \
40 } while(0);
41
42 /* CBC mode macros. */
43
44 #define SILC_CBC_GET_IV(d, s)                   \
45 do {                                            \
46   SILC_GET32_LSB(d[0], &s[0]);                  \
47   SILC_GET32_LSB(d[1], &s[4]);                  \
48   SILC_GET32_LSB(d[2], &s[8]);                  \
49   SILC_GET32_LSB(d[3], &s[12]);                 \
50 } while(0);
51
52 #define SILC_CBC_PUT_IV(s, d)                   \
53 do {                                            \
54   SILC_PUT32_LSB(s[0], &d[0]);                  \
55   SILC_PUT32_LSB(s[1], &d[4]);                  \
56   SILC_PUT32_LSB(s[2], &d[8]);                  \
57   SILC_PUT32_LSB(s[3], &d[12]);                 \
58 } while(0);
59
60 #define SILC_CBC_ENC_PRE(d, s)                  \
61 do {                                            \
62   SILC_GET32_X_LSB(d[0], &s[0]);                \
63   SILC_GET32_X_LSB(d[1], &s[4]);                \
64   SILC_GET32_X_LSB(d[2], &s[8]);                \
65   SILC_GET32_X_LSB(d[3], &s[12]);               \
66 } while(0);
67
68 #define SILC_CBC_ENC_POST(s, d, t)              \
69 do {                                            \
70   SILC_PUT32_LSB(s[0], &d[0]);                  \
71   SILC_PUT32_LSB(s[1], &d[4]);                  \
72   SILC_PUT32_LSB(s[2], &d[8]);                  \
73   SILC_PUT32_LSB(s[3], &d[12]);                 \
74                                                 \
75   d += 16;                                      \
76   t += 16;                                      \
77 } while(0);
78
79 #define SILC_CBC_DEC_PRE(d, s)                  \
80 do {                                            \
81   SILC_GET32_LSB(d[0], &s[0]);                  \
82   SILC_GET32_LSB(d[1], &s[4]);                  \
83   SILC_GET32_LSB(d[2], &s[8]);                  \
84   SILC_GET32_LSB(d[3], &s[12]);                 \
85 } while(0);
86
87 #define SILC_CBC_DEC_POST(s, d, p, t, siv)      \
88 do {                                            \
89   s[0] ^= siv[0];                               \
90   s[1] ^= siv[1];                               \
91   s[2] ^= siv[2];                               \
92   s[3] ^= siv[3];                               \
93                                                 \
94   SILC_PUT32_LSB(s[0], &d[0]);                  \
95   SILC_PUT32_LSB(s[1], &d[4]);                  \
96   SILC_PUT32_LSB(s[2], &d[8]);                  \
97   SILC_PUT32_LSB(s[3], &d[12]);                 \
98                                                 \
99   siv[0] = t[0];                                \
100   siv[1] = t[1];                                \
101   siv[2] = t[2];                                \
102   siv[3] = t[3];                                \
103                                                 \
104   d += 16;                                      \
105   p += 16;                                      \
106 } while(0);
107
108 #endif