New multiprecision integer arithmetic suite.
[u/mdw/catacomb] / des3.c
CommitLineData
d03ab969 1/* -*-c-*-
2 *
3 * $Id: des3.c,v 1.1 1999/09/03 08:41:11 mdw Exp $
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 $
33 * Revision 1.1 1999/09/03 08:41:11 mdw
34 * Initial import.
35 *
36 */
37
38/*----- Header files ------------------------------------------------------*/
39
40#include <assert.h>
41#include <stdio.h>
42#include <stdlib.h>
43#include <string.h>
44
45#include <mLib/bits.h>
46
47#include "blkc.h"
48#include "des-base.h"
49#include "des.h"
50#include "des3.h"
51
52/*----- Main code ---------------------------------------------------------*/
53
54/* --- @des3_init@ --- *
55 *
56 * Arguments: @des3_ctx *k@ = pointer to key block
57 * @const void *buf@ = pointer to key buffer
58 * @size_t sz@ = size of key material
59 *
60 * Returns: ---
61 *
62 * Use: Initializes a DES key buffer. The key buffer may have length
63 * 7, 8, 14, 16, 21, or 24. These correspond to one, two or
64 * three DES keys, either packed or unpacked (i.e., still
65 * containing parity bits).
66 */
67
68void des3_init(des3_ctx *k, const void *buf, size_t sz)
69{
70 size_t step;
71 const octet *p = buf;
72
73 assert(((void)("des3 key length must be one of 7, 8, 14, 16, 21 or 24"),
74 (sz % 7 == 0 || sz % 8 == 0) && sz < 25));
75
76 if (sz % 7 == 0)
77 step = 7;
78 else
79 step = 8;
80
81 des_init(&k->a, p, step);
82 if (sz > 8) p += step;
83 des_init(&k->b, p, step);
84 if (sz > 16) p += step; else p = buf;
85 des_init(&k->c, p, step);
86}
87
88/* --- @des3_eblk@, @des3_dblk@ --- *
89 *
90 * Arguments: @const des3_ctx *k@ = pointer to key block
91 * @const uint32 s[2]@ = pointer to source block
92 * @uint32 d[2]@ = pointer to destination block
93 *
94 * Returns: ---
95 *
96 * Use: Low-level block encryption and decryption.
97 */
98
99void des3_eblk(const des3_ctx *k, const uint32 *s, uint32 *d)
100{
101 uint32 x = s[0], y = s[1];
102 DES_IP(x, y);
103 DES_EBLK(k->a.k, x, y, x, y);
104 DES_DBLK(k->b.k, x, y, x, y);
105 DES_EBLK(k->c.k, x, y, x, y);
106 DES_IPINV(x, y);
107 d[0] = x, d[1] = y;
108}
109
110void des3_dblk(const des3_ctx *k, const uint32 *s, uint32 *d)
111{
112 uint32 x = s[0], y = s[1];
113 DES_IP(x, y);
114 DES_DBLK(k->c.k, x, y, x, y);
115 DES_EBLK(k->b.k, x, y, x, y);
116 DES_DBLK(k->a.k, x, y, x, y);
117 DES_IPINV(x, y);
118 d[0] = x, d[1] = y;
119}
120
121BLKC_TEST(DES3, des3)
122
123/*----- That's all, folks -------------------------------------------------*/