Initial revision
[silc.git] / lib / silccrypt / rc5.h
1 /*
2
3   rc5.h
4
5   Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
6
7   Copyright (C) 1997 - 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  * $Id$
22  * $Log$
23  * Revision 1.1  2000/06/27 11:36:54  priikone
24  * Initial revision
25  *
26  *
27  */
28
29 #ifndef RC5_H
30 #define RC5_H
31
32 #include "rc5_internal.h"
33
34 /* 
35  * SILC Crypto API for RC5
36  */
37
38 /* Sets the key for the cipher. */
39
40 SILC_CIPHER_API_SET_KEY(rc5)
41 {
42   rc5_set_key((RC5Context *)context, (unsigned char *)key, keylen);
43   return 1;
44 }
45
46 /* Sets the string as a new key for the cipher. The string is first
47    hashed and then used as a new key. */
48
49 SILC_CIPHER_API_SET_KEY_WITH_STRING(rc5)
50 {
51   /*  unsigned char key[md5_hash_len];
52   SilcMarsContext *ctx = (SilcMarsContext *)context;
53
54   make_md5_hash(string, &key);
55   memcpy(&ctx->key, mars_set_key(&key, keylen), keylen);
56   memset(&key, 'F', sizeoof(key));
57   */
58
59   return 1;
60 }
61
62 /* Returns the size of the cipher context. */
63
64 SILC_CIPHER_API_CONTEXT_LEN(rc5)
65 {
66   return sizeof(RC5Context);
67 }
68
69 /* Encrypts with the cipher in CBC mode. */
70
71 SILC_CIPHER_API_ENCRYPT_CBC(rc5)
72 {
73   unsigned int *in, *out, *tiv;
74   unsigned int tmp[2];
75   int i;
76
77   in = (unsigned int *)src;
78   out = (unsigned int *)dst;
79   tiv = (unsigned int *)iv;
80
81   tmp[0] = in[0] ^ tiv[0];
82   tmp[1] = in[1] ^ tiv[1];
83   rc5_encrypt((RC5Context *)context, tmp, out);
84   in += 2;
85   out += 2;
86
87   for (i = 8; i < len; i += 8) {
88     tmp[0] = in[0] ^ out[0 - 2];
89     tmp[1] = in[1] ^ out[1 - 2];
90     rc5_encrypt((RC5Context *)context, tmp, out);
91     in += 2;
92     out += 2;
93   }
94
95   return TRUE;
96 }
97
98 /* Decrypts with the cipher in CBC mode. */
99
100 SILC_CIPHER_API_DECRYPT_CBC(rc5)
101 {
102   unsigned int *in, *out, *tiv;
103   unsigned int tmp[2], tmp2[2];
104   int i;
105
106   in = (unsigned int *)src;
107   out = (unsigned int *)dst;
108   tiv = (unsigned int *)iv;
109
110   tmp[0] = in[0];
111   tmp[1] = in[1];
112   tmp[3] = in[3];
113   rc5_decrypt((RC5Context *)context, in, out);
114   out[0] ^= tiv[0];
115   out[1] ^= tiv[1];
116   in += 2;
117   out += 2;
118
119   for (i = 8; i < len; i += 8) {
120     tmp2[0] = tmp[0];
121     tmp2[1] = tmp[1];
122     tmp[0] = in[0];
123     tmp[1] = in[1];
124     rc5_decrypt((RC5Context *)context, in, out);
125     out[0] ^= tmp2[0];
126     out[1] ^= tmp2[1];
127     in += 2;
128     out += 2;
129   }
130
131   return TRUE;
132 }
133
134 #endif