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