chiark / gitweb /
Fix daft error in the comment for @gfshare_get@.
[catacomb] / serpent-check.c
1 /* -*-c-*-
2  *
3  * $Id: serpent-check.c,v 1.1 2000/06/17 12:08:43 mdw Exp $
4  *
5  * Check the Serpent S-boxes
6  *
7  * (c) 2000 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: serpent-check.c,v $
33  * Revision 1.1  2000/06/17 12:08:43  mdw
34  * New cipher.
35  *
36  */
37
38 /*----- Header files ------------------------------------------------------*/
39
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43
44 #include <mLib/bits.h>
45
46 #include "serpent-sbox.h"
47
48 /*----- S-box tables ------------------------------------------------------*/
49
50 static const octet s[8][16] = {
51   {  3,  8, 15,  1, 10,  6,  5, 11, 14, 13,  4,  2,  7,  0,  9, 12 },
52   { 15, 12,  2,  7,  9,  0,  5, 10,  1, 11, 14,  8,  6, 13,  3,  4 },
53   {  8,  6,  7,  9,  3, 12, 10, 15, 13,  1, 14,  4,  0, 11,  5,  2 },
54   {  0, 15, 11,  8, 12,  9,  6,  3, 13,  1,  2,  4, 10,  7,  5, 14 },
55   {  1, 15,  8,  3, 12,  0, 11,  6,  2,  5,  4, 10,  9, 14,  7, 13 },
56   { 15,  5,  2, 11,  4, 10,  9, 12,  0,  3, 14,  8, 13,  6,  7,  1 },
57   {  7,  2, 12,  5,  8,  4,  6, 11, 14,  9,  1, 15, 13,  3, 10,  0 },
58   {  1, 13, 15,  0, 14,  8,  2, 11,  7,  4, 12, 10,  9,  3,  5,  6 }
59 };
60
61 /*----- Main code ---------------------------------------------------------*/
62
63 /* --- @check@ --- *
64  *
65  * Arguments:   @unsigned a, b, c, d@ = four bitslice output registers
66  *              @const octet *p@ = pointer to S-box
67  *
68  * Returns:     Zero if OK, nonzero on failure.
69  *
70  * Use:         Checks that an S-box output is correct.
71  */
72
73 static int check(unsigned a, unsigned b, unsigned c, unsigned d,
74                  const octet *p)
75 {
76   octet buf[16];
77   octet *q = buf;
78   unsigned i;
79
80   for (i = 0; i < 16; i++) {
81     *q++ = (a & 1) | ((b & 1) << 1) | ((c & 1) << 2) | ((d & 1) << 3);
82     a >>= 1; b >>= 1; c >>= 1; d >>= 1;
83   }
84   return (memcmp(buf, p, sizeof(buf)));
85 }
86
87 #define CHECK(i) do {                                                   \
88   unsigned a = 0xaaaa, b = 0xcccc, c = 0xf0f0, d = 0xff00;              \
89   S##i(a, b, c, d);                                                     \
90   if (check(a, b, c, d, s[i])) {                                        \
91     fprintf(stderr, "failure in S%i\n", i);                             \
92     rc = EXIT_FAILURE;                                                  \
93   }                                                                     \
94   IS##i(a, b, c, d);                                                    \
95   if (a != 0xaaaa || b != 0xcccc || c != 0xf0f0 || d != 0xff00) {       \
96     fprintf(stderr, "failure in IS%i\n", i);                            \
97     rc = EXIT_FAILURE;                                                  \
98   }                                                                     \
99 } while (0)
100
101 int main(void)
102 {
103   int rc = 0;
104   CHECK(0); CHECK(1); CHECK(2); CHECK(3);
105   CHECK(4); CHECK(5); CHECK(6); CHECK(7);
106   return (rc);
107 }
108
109 /*----- That's all, folks -------------------------------------------------*/