d03ab969 |
1 | /* -*-c-*- |
2 | * |
89ae755d |
3 | * $Id: des3.c,v 1.2 2000/06/17 10:52:32 mdw Exp $ |
d03ab969 |
4 | * |
5 | * Implementation of double- and triple-DES |
6 | * |
7 | * (c) 1999 Straylight/Edgeware |
8 | */ |
9 | |
10 | /*----- Licensing notice --------------------------------------------------* |
11 | * |
12 | * This file is part of Catacomb. |
13 | * |
14 | * Catacomb is free software; you can redistribute it and/or modify |
15 | * it under the terms of the GNU Library General Public License as |
16 | * published by the Free Software Foundation; either version 2 of the |
17 | * License, or (at your option) any later version. |
18 | * |
19 | * Catacomb is distributed in the hope that it will be useful, |
20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
22 | * GNU Library General Public License for more details. |
23 | * |
24 | * You should have received a copy of the GNU Library General Public |
25 | * License along with Catacomb; if not, write to the Free |
26 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
27 | * MA 02111-1307, USA. |
28 | */ |
29 | |
30 | /*----- Revision history --------------------------------------------------* |
31 | * |
32 | * $Log: des3.c,v $ |
89ae755d |
33 | * Revision 1.2 2000/06/17 10:52:32 mdw |
34 | * Support new key size interface. |
35 | * |
d03ab969 |
36 | * Revision 1.1 1999/09/03 08:41:11 mdw |
37 | * Initial import. |
38 | * |
39 | */ |
40 | |
41 | /*----- Header files ------------------------------------------------------*/ |
42 | |
43 | #include <assert.h> |
44 | #include <stdio.h> |
45 | #include <stdlib.h> |
46 | #include <string.h> |
47 | |
48 | #include <mLib/bits.h> |
49 | |
50 | #include "blkc.h" |
51 | #include "des-base.h" |
52 | #include "des.h" |
53 | #include "des3.h" |
89ae755d |
54 | #include "gcipher.h" |
55 | |
56 | /*----- Global variables --------------------------------------------------*/ |
57 | |
58 | const octet des3_keysz[] = { KSZ_SET, 21, 7, 8, 14, 16, 24, 0 }; |
d03ab969 |
59 | |
60 | /*----- Main code ---------------------------------------------------------*/ |
61 | |
62 | /* --- @des3_init@ --- * |
63 | * |
64 | * Arguments: @des3_ctx *k@ = pointer to key block |
65 | * @const void *buf@ = pointer to key buffer |
66 | * @size_t sz@ = size of key material |
67 | * |
68 | * Returns: --- |
69 | * |
70 | * Use: Initializes a DES key buffer. The key buffer may have length |
71 | * 7, 8, 14, 16, 21, or 24. These correspond to one, two or |
72 | * three DES keys, either packed or unpacked (i.e., still |
73 | * containing parity bits). |
74 | */ |
75 | |
76 | void des3_init(des3_ctx *k, const void *buf, size_t sz) |
77 | { |
78 | size_t step; |
79 | const octet *p = buf; |
80 | |
89ae755d |
81 | KSZ_ASSERT(des3, sz); |
d03ab969 |
82 | |
83 | if (sz % 7 == 0) |
84 | step = 7; |
85 | else |
86 | step = 8; |
87 | |
88 | des_init(&k->a, p, step); |
89 | if (sz > 8) p += step; |
90 | des_init(&k->b, p, step); |
91 | if (sz > 16) p += step; else p = buf; |
92 | des_init(&k->c, p, step); |
93 | } |
94 | |
95 | /* --- @des3_eblk@, @des3_dblk@ --- * |
96 | * |
97 | * Arguments: @const des3_ctx *k@ = pointer to key block |
98 | * @const uint32 s[2]@ = pointer to source block |
99 | * @uint32 d[2]@ = pointer to destination block |
100 | * |
101 | * Returns: --- |
102 | * |
103 | * Use: Low-level block encryption and decryption. |
104 | */ |
105 | |
106 | void des3_eblk(const des3_ctx *k, const uint32 *s, uint32 *d) |
107 | { |
108 | uint32 x = s[0], y = s[1]; |
109 | DES_IP(x, y); |
110 | DES_EBLK(k->a.k, x, y, x, y); |
111 | DES_DBLK(k->b.k, x, y, x, y); |
112 | DES_EBLK(k->c.k, x, y, x, y); |
113 | DES_IPINV(x, y); |
114 | d[0] = x, d[1] = y; |
115 | } |
116 | |
117 | void des3_dblk(const des3_ctx *k, const uint32 *s, uint32 *d) |
118 | { |
119 | uint32 x = s[0], y = s[1]; |
120 | DES_IP(x, y); |
121 | DES_DBLK(k->c.k, x, y, x, y); |
122 | DES_EBLK(k->b.k, x, y, x, y); |
123 | DES_DBLK(k->a.k, x, y, x, y); |
124 | DES_IPINV(x, y); |
125 | d[0] = x, d[1] = y; |
126 | } |
127 | |
128 | BLKC_TEST(DES3, des3) |
129 | |
130 | /*----- That's all, folks -------------------------------------------------*/ |