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