2 * Copyright (C) 2001-2003 by NBMK Encryption Technologies.
5 * NBMK Encryption Technologies provides no support of any kind for
6 * this software. Questions or concerns about it may be addressed to
7 * the members of the relevant open-source community at
8 * <tech-crypto@netbsd.org>.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions are
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above
18 * copyright notice, this list of conditions and the following
19 * disclaimer in the documentation and/or other materials provided
20 * with the distribution.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 static char const n8_id
[] = "$Id: n8_key_works.c,v 1.1 2008/10/30 12:02:14 darran Exp $";
36 /*****************************************************************************/
37 /** @file n8_key_works.c
38 * @brief Contains key operations
40 *****************************************************************************/
42 /*****************************************************************************
44 * 12/11/02 brr Include n8_OS_intf to pick up needed types.
45 * 09/20/01 bac Changed Key_cblock to key_cblock_t to follow coding stds.
46 * 07/02/01 mel Fixed comments.
47 * 06/24/01 bac Changes to bring up to coding standards.
48 * 05/29/01 mel Original version.
49 ****************************************************************************/
50 /** @defgroup keyworks Key Management Routines
53 #include "n8_key_works.h" /* definitions for functions */
54 #include "n8_OS_intf.h"
57 /* buffer with current parameters for RNG settings
59 We have to design the locking mechanism!
64 /* this table is taken from des set_key.c
68 static const unsigned char odd_parity
[256]={
69 1, 1, 2, 2, 4, 4, 7, 7, 8, 8, 11, 11, 13, 13, 14, 14,
70 16, 16, 19, 19, 21, 21, 22, 22, 25, 25, 26, 26, 28, 28, 31, 31,
71 32, 32, 35, 35, 37, 37, 38, 38, 41, 41, 42, 42, 44, 44, 47, 47,
72 49, 49, 50, 50, 52, 52, 55, 55, 56, 56, 59, 59, 61, 61, 62, 62,
73 64, 64, 67, 67, 69, 69, 70, 70, 73, 73, 74, 74, 76, 76, 79, 79,
74 81, 81, 82, 82, 84, 84, 87, 87, 88, 88, 91, 91, 93, 93, 94, 94,
75 97, 97, 98, 98,100,100,103,103,104,104,107,107,109,109,110,110,
76 112,112,115,115,117,117,118,118,121,121,122,122,124,124,127,127,
77 128,128,131,131,133,133,134,134,137,137,138,138,140,140,143,143,
78 145,145,146,146,148,148,151,151,152,152,155,155,157,157,158,158,
79 161,161,162,162,164,164,167,167,168,168,171,171,173,173,174,174,
80 176,176,179,179,181,181,182,182,185,185,186,186,188,188,191,191,
81 193,193,194,194,196,196,199,199,200,200,203,203,205,205,206,206,
82 208,208,211,211,213,213,214,214,217,217,218,218,220,220,223,223,
83 224,224,227,227,229,229,230,230,233,233,234,234,236,236,239,239,
84 241,241,242,242,244,244,247,247,248,248,251,251,253,253,254,254};
86 /* Weak and semi weak keys as taken from
89 * %T Security for Computer Networks
90 * %I John Wiley & Sons
92 * Many thanks to smb@ulysses.att.com (Steven Bellovin) for the reference
93 * (and actual cblock values).
95 static key_cblock_t weak_keys
[NUM_WEAK_KEY
]={
97 {0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01},
98 {0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE},
99 {0x1F,0x1F,0x1F,0x1F,0x0E,0x0E,0x0E,0x0E},
100 {0xE0,0xE0,0xE0,0xE0,0xF1,0xF1,0xF1,0xF1},
102 {0x01,0xFE,0x01,0xFE,0x01,0xFE,0x01,0xFE},
103 {0xFE,0x01,0xFE,0x01,0xFE,0x01,0xFE,0x01},
104 {0x1F,0xE0,0x1F,0xE0,0x0E,0xF1,0x0E,0xF1},
105 {0xE0,0x1F,0xE0,0x1F,0xF1,0x0E,0xF1,0x0E},
106 {0x01,0xE0,0x01,0xE0,0x01,0xF1,0x01,0xF1},
107 {0xE0,0x01,0xE0,0x01,0xF1,0x01,0xF1,0x01},
108 {0x1F,0xFE,0x1F,0xFE,0x0E,0xFE,0x0E,0xFE},
109 {0xFE,0x1F,0xFE,0x1F,0xFE,0x0E,0xFE,0x0E},
110 {0x01,0x1F,0x01,0x1F,0x01,0x0E,0x01,0x0E},
111 {0x1F,0x01,0x1F,0x01,0x0E,0x01,0x0E,0x01},
112 {0xE0,0xFE,0xE0,0xFE,0xF1,0xFE,0xF1,0xFE},
113 {0xFE,0xE0,0xFE,0xE0,0xFE,0xF1,0xFE,0xF1}};
115 /*****************************************************************************
116 * checkKeyForWeakness
117 *****************************************************************************/
118 /** @ingroup keyworks
119 * @brief Checks key for weakness.
121 * Compare key with values from the Weak Key Table
123 * @param key_p RO: A key for X9.17 algorithm
127 * N8_FALSE - key is not weak<BR>
128 * N8_TRUE - key is weak<BR>
132 *****************************************************************************/
133 N8_Boolean_t
checkKeyForWeakness (key_cblock_t
*key_p
)
136 N8_Boolean_t ret
= N8_FALSE
;
138 DBG(("checkKeyForWeakness\n"));
140 for (i
=0; i
< NUM_WEAK_KEY
; i
++)
142 if (memcmp(weak_keys
[i
], key_p
, sizeof(key_p
)) == 0)
149 } /* checkKeyForWeakness */
152 /*****************************************************************************
154 *****************************************************************************/
155 /** @ingroup keyworks
156 * @brief checks key for parity.
158 * Compare key with values from the Parity Table
160 * @param key_p RO: A key_p for X9.17 algorithm
164 * FALSE - key is not OK <BR>
165 * TRUE - key is OK<BR>
169 *****************************************************************************/
171 N8_Boolean_t
checkKeyParity(key_cblock_t
*key_p
)
174 N8_Boolean_t ret
= N8_TRUE
;
175 DBG(("checkKeyParity\n"));
177 for (i
=0; i
< DES_KEY_SIZE
; i
++)
179 if ((*key_p
)[i
] != odd_parity
[(*key_p
)[i
]])
186 } /* checkKeyParity */
189 /*****************************************************************************
191 *****************************************************************************/
192 /** @ingroup keyworks
193 * @brief Forces key parity.
195 * @param key_p RO: A key for X9.17 algorithm
200 *****************************************************************************/
201 void forceParity(key_cblock_t
*key_p
)
205 for (i
=0; i
<DES_KEY_SIZE
; i
++)
206 (*key_p
)[i
]=odd_parity
[(*key_p
)[i
]];