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