disable sstrip when using musl
[openwrt/openwrt.git] / target / linux / ubicom32 / files / arch / ubicom32 / crypto / des_check_key.c
1 /*
2 * arch/ubicom32/crypto/des_check_key.c
3 * Ubicom32 architecture function for checking keys for the DES and
4 * Tripple DES Encryption algorithms.
5 *
6 * (C) Copyright 2009, Ubicom, Inc.
7 *
8 * Originally released as descore by Dana L. How <how@isl.stanford.edu>.
9 * Modified by Raimar Falke <rf13@inf.tu-dresden.de> for the Linux-Kernel.
10 * Derived from Cryptoapi and Nettle implementations, adapted for in-place
11 * scatterlist interface. Changed LGPL to GPL per section 3 of the LGPL.
12 *
13 * s390 Version:
14 * Copyright IBM Corp. 2003
15 * Author(s): Thomas Spatzier
16 * Jan Glauber (jan.glauber@de.ibm.com)
17 *
18 * Derived from "crypto/des.c"
19 * Copyright (c) 1992 Dana L. How.
20 * Copyright (c) Raimar Falke <rf13@inf.tu-dresden.de>
21 * Copyright (c) Gisle Sflensminde <gisle@ii.uib.no>
22 * Copyright (C) 2001 Niels Mvller.
23 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
24 *
25 * This file is part of the Ubicom32 Linux Kernel Port.
26 *
27 * The Ubicom32 Linux Kernel Port is free software: you can redistribute
28 * it and/or modify it under the terms of the GNU General Public License
29 * as published by the Free Software Foundation, either version 2 of the
30 * License, or (at your option) any later version.
31 *
32 * The Ubicom32 Linux Kernel Port is distributed in the hope that it
33 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
34 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
35 * the GNU General Public License for more details.
36 *
37 * You should have received a copy of the GNU General Public License
38 * along with the Ubicom32 Linux Kernel Port. If not,
39 * see <http://www.gnu.org/licenses/>.
40 *
41 * Ubicom32 implementation derived from (with many thanks):
42 * arch/m68knommu
43 * arch/blackfin
44 * arch/parisc
45 */
46 #include <linux/init.h>
47 #include <linux/module.h>
48 #include <linux/errno.h>
49 #include <linux/crypto.h>
50 #include "crypto_des.h"
51
52 #define ROR(d,c,o) ((d) = (d) >> (c) | (d) << (o))
53
54 static const u8 parity[] = {
55 8,1,0,8,0,8,8,0,0,8,8,0,8,0,2,8,0,8,8,0,8,0,0,8,8,0,0,8,0,8,8,3,
56 0,8,8,0,8,0,0,8,8,0,0,8,0,8,8,0,8,0,0,8,0,8,8,0,0,8,8,0,8,0,0,8,
57 0,8,8,0,8,0,0,8,8,0,0,8,0,8,8,0,8,0,0,8,0,8,8,0,0,8,8,0,8,0,0,8,
58 8,0,0,8,0,8,8,0,0,8,8,0,8,0,0,8,0,8,8,0,8,0,0,8,8,0,0,8,0,8,8,0,
59 0,8,8,0,8,0,0,8,8,0,0,8,0,8,8,0,8,0,0,8,0,8,8,0,0,8,8,0,8,0,0,8,
60 8,0,0,8,0,8,8,0,0,8,8,0,8,0,0,8,0,8,8,0,8,0,0,8,8,0,0,8,0,8,8,0,
61 8,0,0,8,0,8,8,0,0,8,8,0,8,0,0,8,0,8,8,0,8,0,0,8,8,0,0,8,0,8,8,0,
62 4,8,8,0,8,0,0,8,8,0,0,8,0,8,8,0,8,5,0,8,0,8,8,0,0,8,8,0,8,0,6,8,
63 };
64
65 /*
66 * RFC2451: Weak key checks SHOULD be performed.
67 */
68 int
69 crypto_des_check_key(const u8 *key, unsigned int keylen, u32 *flags)
70 {
71 u32 n, w;
72
73 n = parity[key[0]]; n <<= 4;
74 n |= parity[key[1]]; n <<= 4;
75 n |= parity[key[2]]; n <<= 4;
76 n |= parity[key[3]]; n <<= 4;
77 n |= parity[key[4]]; n <<= 4;
78 n |= parity[key[5]]; n <<= 4;
79 n |= parity[key[6]]; n <<= 4;
80 n |= parity[key[7]];
81 w = 0x88888888L;
82
83 if ((*flags & CRYPTO_TFM_REQ_WEAK_KEY)
84 && !((n - (w >> 3)) & w)) { /* 1 in 10^10 keys passes this test */
85 if (n < 0x41415151) {
86 if (n < 0x31312121) {
87 if (n < 0x14141515) {
88 /* 01 01 01 01 01 01 01 01 */
89 if (n == 0x11111111) goto weak;
90 /* 01 1F 01 1F 01 0E 01 0E */
91 if (n == 0x13131212) goto weak;
92 } else {
93 /* 01 E0 01 E0 01 F1 01 F1 */
94 if (n == 0x14141515) goto weak;
95 /* 01 FE 01 FE 01 FE 01 FE */
96 if (n == 0x16161616) goto weak;
97 }
98 } else {
99 if (n < 0x34342525) {
100 /* 1F 01 1F 01 0E 01 0E 01 */
101 if (n == 0x31312121) goto weak;
102 /* 1F 1F 1F 1F 0E 0E 0E 0E (?) */
103 if (n == 0x33332222) goto weak;
104 } else {
105 /* 1F E0 1F E0 0E F1 0E F1 */
106 if (n == 0x34342525) goto weak;
107 /* 1F FE 1F FE 0E FE 0E FE */
108 if (n == 0x36362626) goto weak;
109 }
110 }
111 } else {
112 if (n < 0x61616161) {
113 if (n < 0x44445555) {
114 /* E0 01 E0 01 F1 01 F1 01 */
115 if (n == 0x41415151) goto weak;
116 /* E0 1F E0 1F F1 0E F1 0E */
117 if (n == 0x43435252) goto weak;
118 } else {
119 /* E0 E0 E0 E0 F1 F1 F1 F1 (?) */
120 if (n == 0x44445555) goto weak;
121 /* E0 FE E0 FE F1 FE F1 FE */
122 if (n == 0x46465656) goto weak;
123 }
124 } else {
125 if (n < 0x64646565) {
126 /* FE 01 FE 01 FE 01 FE 01 */
127 if (n == 0x61616161) goto weak;
128 /* FE 1F FE 1F FE 0E FE 0E */
129 if (n == 0x63636262) goto weak;
130 } else {
131 /* FE E0 FE E0 FE F1 FE F1 */
132 if (n == 0x64646565) goto weak;
133 /* FE FE FE FE FE FE FE FE */
134 if (n == 0x66666666) goto weak;
135 }
136 }
137 }
138 }
139 return 0;
140 weak:
141 *flags |= CRYPTO_TFM_RES_WEAK_KEY;
142 return -EINVAL;
143 }
144
145 EXPORT_SYMBOL(crypto_des_check_key);
146
147 MODULE_LICENSE("GPL");
148 MODULE_DESCRIPTION("Key Check function for DES & DES3 Cipher Algorithms");