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