Tizen 2.0 Release
[external/nettle.git] / nettle-internal.c
1 /* nettle-internal.c
2  *
3  * Things that are used only by the testsuite and benchmark, and
4  * subject to change.
5  */
6
7 /* nettle, low-level cryptographics library
8  *
9  * Copyright (C) 2002 Niels Möller
10  *  
11  * The nettle library is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License as published by
13  * the Free Software Foundation; either version 2.1 of the License, or (at your
14  * option) any later version.
15  * 
16  * The nettle library is distributed in the hope that it will be useful, but
17  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
18  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
19  * License for more details.
20  * 
21  * You should have received a copy of the GNU Lesser General Public License
22  * along with the nettle library; see the file COPYING.LIB.  If not, write to
23  * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
24  * MA 02111-1307, USA.
25  */
26
27 #if HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <assert.h>
32 #include <stdlib.h>
33
34 #include "nettle-internal.h"
35 #include "des.h"
36 #include "blowfish.h"
37
38 /* DES uses a different signature for the key set function.
39  * And we have to adjust parity. */
40 static void
41 des_set_key_hack(void *c, unsigned length, const uint8_t *key)
42 {
43   struct des_ctx *ctx = c;
44   uint8_t pkey[DES_KEY_SIZE];
45   
46   assert(length == DES_KEY_SIZE);
47   des_fix_parity(DES_KEY_SIZE, pkey, key);
48   if (!des_set_key(ctx, pkey))
49     abort();
50 }
51
52 static void
53 des3_set_key_hack(void *c, unsigned length, const uint8_t *key)
54 {
55   struct des3_ctx *ctx = c;
56   uint8_t pkey[DES3_KEY_SIZE];
57   
58   assert(length == DES3_KEY_SIZE);
59   des_fix_parity(DES3_KEY_SIZE, pkey, key);
60   if (!des3_set_key(ctx, pkey))
61     abort();
62 }
63
64 const struct nettle_cipher
65 nettle_des = {
66   "des", sizeof(struct des_ctx),
67   DES_BLOCK_SIZE, DES_KEY_SIZE,
68   des_set_key_hack, des_set_key_hack,
69   (nettle_crypt_func *) des_encrypt,
70   (nettle_crypt_func *) des_decrypt
71 };
72
73 const struct nettle_cipher
74 nettle_des3 = {
75  "des3", sizeof(struct des3_ctx),
76  DES3_BLOCK_SIZE, DES3_KEY_SIZE,
77  des3_set_key_hack, des3_set_key_hack,
78  (nettle_crypt_func *) des3_encrypt,
79  (nettle_crypt_func *) des3_decrypt
80 };
81
82 /* NOTE: This is not as nice as one might think, as it will crash if
83  * we try to encrypt something with a weak key. */
84 const struct nettle_cipher
85 nettle_blowfish128 = _NETTLE_CIPHER(blowfish, BLOWFISH, 128);