Debian Codegraph @

src/DES.c

raw symbols SCIP
1 /*
2 * DES.c: DES/3DES support for PyCrypto using LibTomCrypt
3 *
4 * Written in 2009 by Dwayne C. Litzenberger <[email protected]>
5 *
6 * ===================================================================
7 * The contents of this file are dedicated to the public domain. To
8 * the extent that dedication to the public domain is not available,
9 * everyone is granted a worldwide, perpetual, royalty-free,
10 * non-exclusive license to exercise all rights associated with the
11 * contents of this file for any purpose whatsoever.
12 * No rights are reserved.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
18 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
19 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 * ===================================================================
23 *
24 * Country of origin: Canada
25 */
26
27 #include "common.h"
28 #include "block_base.h"
29
30 #ifndef PCT_DES3_MODULE
31 FAKE_INIT(raw_des)
32 #else
33 FAKE_INIT(raw_des3)
34 #endif
35
36 /* Setting this will cause LibTomCrypt to return CRYPT_INVALID_ARG when its
37 * assert-like LTC_ARGCHK macro fails. */
38 #define ARGTYPE 4
39
40 /* Include the actial DES implementation */
41 #define LTC_NO_PROTOTYPES
42 #include "libtom/tomcrypt_des.c"
43
44 struct block_state {
45 symmetric_key sk;
46 };
47
48 static int block_init(struct block_state *self, const uint8_t *key, size_t keylen)
49 {
50 int rc;
51 #ifdef PCT_DES3_MODULE
52 rc = des3_setup(key, keylen, 0, &self->sk);
53 #else
54 rc = des_setup(key, keylen, 0, &self->sk);
55 #endif
56 switch (rc) {
57 case CRYPT_OK:
58 return 0;
59 case CRYPT_INVALID_KEYSIZE:
60 return ERR_KEY_SIZE;
61 case CRYPT_INVALID_ROUNDS:
62 return ERR_NR_ROUNDS;
63 case CRYPT_INVALID_ARG:
64 return ERR_UNKNOWN;
65 }
66 return ERR_UNKNOWN;
67 }
68
69 static void block_finalize(struct block_state *self)
70 {
71 }
72
73 static void block_encrypt(struct block_state *self, const uint8_t *in, uint8_t *out)
74 {
75 #ifdef PCT_DES3_MODULE
76 des3_ecb_encrypt(in, out, &self->sk);
77 #else
78 des_ecb_encrypt(in, out, &self->sk);
79 #endif
80 }
81
82 static void block_decrypt(struct block_state *self, const uint8_t *in, uint8_t *out)
83 {
84 #ifdef PCT_DES3_MODULE
85 des3_ecb_decrypt(in, out, &self->sk);
86 #else
87 des_ecb_decrypt(in, out, &self->sk);
88 #endif
89 }
90
91 #ifdef PCT_DES3_MODULE
92 # define MODULE_NAME DES3 /* triple DES */
93 # define BLOCK_SIZE 8 /* 64-bit block size */
94 # define KEY_SIZE 0 /* variable key size (can be 128 or 192 bits (including parity) */
95 #else
96 # define MODULE_NAME DES /* single DES */
97 # define BLOCK_SIZE 8 /* 64-bit block size */
98 # define KEY_SIZE 8 /* 64-bit keys (including parity) */
99 #endif
100 #include "block_common.c"