Initial revision
[silc.git] / lib / silccrypt / dfc.h
1 /*
2
3   dfc.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 DFC_H
30 #define DFC_H
31
32 #include "dfc_internal.h"
33
34 /* 
35  * SILC Crypto API for DFC
36  */
37
38 /* Sets the key for the cipher. */
39
40 inline int silc_dfc_init(void *context, 
41                          const unsigned char *key, 
42                          unsigned int keylen)
43 {
44   dfc_set_key((DfcContext *)context, (unsigned int *)key, keylen);
45   return 1;
46 }
47
48 /* Sets the string as a new key for the cipher. The string is first
49    hashed and then used as a new key. */
50
51 inline int silc_dfc_set_string_as_key(void *context, 
52                                       const unsigned char *string,
53                                       unsigned int keylen)
54 {
55   /*  SilcHash hash;
56   unsigned char key[16];
57
58   silc_hash_alloc("md5", &hash);
59   hash->make_hash(hash, string, stringlen, key);
60
61   dfc_set_key((DfcContext *)context, key, sizeof(key));
62
63   silc_hash_free(hash);
64   memset(&key, 'F', sizeof(key));
65   */
66   return TRUE;
67 }
68
69 /* Returns the size of the cipher context. */
70
71 inline unsigned int silc_dfc_context_len()
72 {
73   return sizeof(DfcContext);
74 }
75
76 /* Encrypts with the cipher in CBC mode. */
77
78 inline int silc_dfc_encrypt_cbc(void *context,
79                                 const unsigned char *src,
80                                 unsigned char *dst,
81                                 unsigned int len,
82                                 unsigned char *iv)
83 {
84   unsigned int *in, *out, *tiv;
85   unsigned int tmp[4];
86   int i;
87
88   in = (unsigned int *)src;
89   out = (unsigned int *)dst;
90   tiv = (unsigned int *)iv;
91
92   tmp[0] = in[0] ^ tiv[0];
93   tmp[1] = in[1] ^ tiv[1];
94   tmp[2] = in[2] ^ tiv[2];
95   tmp[3] = in[3] ^ tiv[3];
96   dfc_encrypt((DfcContext *)context, tmp, out);
97   in += 4;
98   out += 4;
99
100   for (i = 16; i < len; i += 16) {
101     tmp[0] = in[0] ^ out[0 - 4];
102     tmp[1] = in[1] ^ out[1 - 4];
103     tmp[2] = in[2] ^ out[2 - 4];
104     tmp[3] = in[3] ^ out[3 - 4];
105     dfc_encrypt((DfcContext *)context, tmp, out);
106     in += 4;
107     out += 4;
108   }
109
110   return 1;
111 }
112
113 /* Decrypts with the cipher in CBC mode. */
114
115 inline int silc_dfc_decrypt_cbc(void *context,
116                                 const unsigned char *src,
117                                 unsigned char *dst,
118                                 unsigned int len,
119                                 unsigned char *iv)
120 {
121   unsigned int *in, *out, *tiv;
122   int i;
123
124   in = (unsigned int *)src;
125   out = (unsigned int *)dst;
126   tiv = (unsigned int *)iv;
127
128   dfc_decrypt((DfcContext *)context, in, out);
129   out[0] ^= tiv[0];
130   out[1] ^= tiv[1];
131   out[2] ^= tiv[2];
132   out[3] ^= tiv[3];
133   in += 4;
134   out += 4;
135
136   for (i = 16; i < len; i += 16) {
137     dfc_decrypt((DfcContext *)context, in, out);
138     out[0] ^= in[0 - 4];
139     out[1] ^= in[1 - 4];
140     out[2] ^= in[2 - 4];
141     out[3] ^= in[3 - 4];
142     in += 4;
143     out += 4;
144   }
145
146   return 1;
147 }
148
149 #endif