math/mpreduce.h: Missing include files.
[u/mdw/catacomb] / symm / des3.c
CommitLineData
d03ab969 1/* -*-c-*-
2 *
d03ab969 3 * Implementation of double- and triple-DES
4 *
5 * (c) 1999 Straylight/Edgeware
6 */
7
45c0fd36 8/*----- Licensing notice --------------------------------------------------*
d03ab969 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.
45c0fd36 16 *
d03ab969 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.
45c0fd36 21 *
d03ab969 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
d03ab969 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"
89ae755d 41#include "gcipher.h"
42
43/*----- Global variables --------------------------------------------------*/
44
45const octet des3_keysz[] = { KSZ_SET, 21, 7, 8, 14, 16, 24, 0 };
d03ab969 46
47/*----- Main code ---------------------------------------------------------*/
48
49/* --- @des3_init@ --- *
50 *
51 * Arguments: @des3_ctx *k@ = pointer to key block
52 * @const void *buf@ = pointer to key buffer
53 * @size_t sz@ = size of key material
54 *
55 * Returns: ---
56 *
57 * Use: Initializes a DES key buffer. The key buffer may have length
45c0fd36
MW
58 * 7, 8, 14, 16, 21, or 24. These correspond to one, two or
59 * three DES keys, either packed or unpacked (i.e., still
60 * containing parity bits).
d03ab969 61 */
62
63void des3_init(des3_ctx *k, const void *buf, size_t sz)
64{
65 size_t step;
66 const octet *p = buf;
67
89ae755d 68 KSZ_ASSERT(des3, sz);
d03ab969 69
70 if (sz % 7 == 0)
71 step = 7;
72 else
73 step = 8;
74
75 des_init(&k->a, p, step);
76 if (sz > 8) p += step;
77 des_init(&k->b, p, step);
78 if (sz > 16) p += step; else p = buf;
79 des_init(&k->c, p, step);
80}
81
82/* --- @des3_eblk@, @des3_dblk@ --- *
83 *
84 * Arguments: @const des3_ctx *k@ = pointer to key block
85 * @const uint32 s[2]@ = pointer to source block
86 * @uint32 d[2]@ = pointer to destination block
87 *
88 * Returns: ---
89 *
90 * Use: Low-level block encryption and decryption.
91 */
92
93void des3_eblk(const des3_ctx *k, const uint32 *s, uint32 *d)
94{
95 uint32 x = s[0], y = s[1];
96 DES_IP(x, y);
97 DES_EBLK(k->a.k, x, y, x, y);
98 DES_DBLK(k->b.k, x, y, x, y);
99 DES_EBLK(k->c.k, x, y, x, y);
100 DES_IPINV(x, y);
101 d[0] = x, d[1] = y;
102}
103
104void des3_dblk(const des3_ctx *k, const uint32 *s, uint32 *d)
105{
106 uint32 x = s[0], y = s[1];
107 DES_IP(x, y);
108 DES_DBLK(k->c.k, x, y, x, y);
109 DES_EBLK(k->b.k, x, y, x, y);
110 DES_DBLK(k->a.k, x, y, x, y);
111 DES_IPINV(x, y);
112 d[0] = x, d[1] = y;
113}
114
115BLKC_TEST(DES3, des3)
116
117/*----- That's all, folks -------------------------------------------------*/