updates. New data types.
[crypto.git] / lib / silccrypt / blowfish.h
1 /*
2
3   blowfish.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 BLOWFISH_H
33 #define BLOWFISH_H
34
35 #include "blowfish_internal.h"
36
37 /* 
38  * SILC Crypto API for Blowfish
39  */
40
41 /* Sets the key for the cipher. */
42
43 SILC_CIPHER_API_SET_KEY(blowfish)
44 {
45   blowfish_set_key((BlowfishContext *)context, 
46                    (unsigned char *)key, keylen);
47   return TRUE;
48 }
49
50 /* Sets the string as a new key for the cipher. The string is first
51    hashed and then used as a new key. */
52
53 SILC_CIPHER_API_SET_KEY_WITH_STRING(blowfish)
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   blowfish_set_key((BlowfishContext *)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 SILC_CIPHER_API_CONTEXT_LEN(blowfish)
72 {
73   return sizeof(BlowfishContext);
74 }
75
76 /* Encrypts with the cipher in CBC mode. */
77
78 SILC_CIPHER_API_ENCRYPT_CBC(blowfish)
79 {
80   uint32 *in, *out, *tiv;
81   uint32 tmp[4];
82   int i;
83
84   in = (uint32 *)src;
85   out = (uint32 *)dst;
86   tiv = (uint32 *)iv;
87
88   tmp[0] = in[0] ^ tiv[0];
89   tmp[1] = in[1] ^ tiv[1];  
90   tmp[2] = in[2] ^ tiv[2];
91   tmp[3] = in[3] ^ tiv[3];
92   blowfish_encrypt((BlowfishContext *)context, tmp, out, 16);
93   in += 4;
94   out += 4;
95
96   for (i = 16; i < len; i += 16) {
97     tmp[0] = in[0] ^ out[0 - 4];
98     tmp[1] = in[1] ^ out[1 - 4];
99     tmp[2] = in[2] ^ out[2 - 4];
100     tmp[3] = in[3] ^ out[3 - 4];
101     blowfish_encrypt((BlowfishContext *)context, tmp, out, 16);
102     in += 4;
103     out += 4;
104   }
105
106   return 1;
107 }
108
109 /* Decrypts with the cipher in CBC mode. */
110
111 SILC_CIPHER_API_DECRYPT_CBC(blowfish)
112 {
113   uint32 *in, *out, *tiv;
114   int i;
115
116   in = (uint32 *)src;
117   out = (uint32 *)dst;
118   tiv = (uint32 *)iv;
119
120   blowfish_decrypt((BlowfishContext *)context, in, out, 16);
121   out[0] ^= tiv[0];
122   out[1] ^= tiv[1];
123   out[2] ^= tiv[2];
124   out[3] ^= tiv[3];
125   in += 4;
126   out += 4;
127
128   for (i = 16; i < len; i += 16) {
129     blowfish_decrypt((BlowfishContext *)context, in, out, 16);
130     out[0] ^= in[0 - 4];
131     out[1] ^= in[1 - 4];
132     out[2] ^= in[2 - 4];
133     out[3] ^= in[3 - 4];
134     in += 4;
135     out += 4;
136   }
137
138   return 1;
139 }
140
141 #endif