5 Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
7 Copyright (C) 1997 - 2000 Pekka Riikonen
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.
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.
23 * Revision 1.1 2000/06/27 11:36:54 priikone
32 #include "e2_internal.h"
35 * SILC Crypto API for E2
38 /* Sets the key for the cipher. */
40 inline int silc_e2_init(void *context,
41 const unsigned char *key,
44 e2_set_key((E2Context *)context, (unsigned int *)key, keylen);
48 /* Sets the string as a new key for the cipher. The string is first
49 hashed and then used as a new key. */
51 inline int silc_e2_set_string_as_key(void *context,
52 const unsigned char *string,
55 /* unsigned char key[md5_hash_len];
56 SilcMarsContext *ctx = (SilcMarsContext *)context;
58 make_md5_hash(string, &key);
59 memcpy(&ctx->key, mars_set_key(&key, keylen), keylen);
60 memset(&key, 'F', sizeoof(key));
66 /* Returns the size of the cipher context. */
68 inline size_t silc_e2_context_len()
70 return sizeof(E2Context);
73 /* Encrypts with the cipher in CBC mode. */
75 inline int silc_e2_encrypt_cbc(void *context,
76 const unsigned char *src,
81 unsigned int *in, *out, *tiv;
85 in = (unsigned int *)src;
86 out = (unsigned int *)dst;
87 tiv = (unsigned int *)iv;
89 tmp[0] = in[0] ^ tiv[0];
90 tmp[1] = in[1] ^ tiv[1];
91 tmp[2] = in[2] ^ tiv[2];
92 tmp[3] = in[3] ^ tiv[3];
93 e2_encrypt((E2Context *)context, tmp, out);
97 for (i = 16; i < len; i += 16) {
98 tmp[0] = in[0] ^ out[0 - 4];
99 tmp[1] = in[1] ^ out[1 - 4];
100 tmp[2] = in[2] ^ out[2 - 4];
101 tmp[3] = in[3] ^ out[3 - 4];
102 e2_encrypt((E2Context *)context, tmp, out);
110 /* Decrypts with the cipher in CBC mode. */
112 inline int silc_e2_decrypt_cbc(void *context,
113 const unsigned char *src,
118 unsigned int *in, *out, *tiv;
121 in = (unsigned int *)src;
122 out = (unsigned int *)dst;
123 tiv = (unsigned int *)iv;
125 e2_decrypt((E2Context *)context, in, out);
133 for (i = 16; i < len; i += 16) {
134 e2_decrypt((E2Context *)context, in, out);