[S390] Get rid of a lot of sparse warnings.
[pandora-kernel.git] / arch / s390 / crypto / des_check_key.c
1 /*
2  * Cryptographic API.
3  *
4  * Function for checking keys for the DES and Tripple DES Encryption
5  * algorithms.
6  *
7  * Originally released as descore by Dana L. How <how@isl.stanford.edu>.
8  * Modified by Raimar Falke <rf13@inf.tu-dresden.de> for the Linux-Kernel.
9  * Derived from Cryptoapi and Nettle implementations, adapted for in-place
10  * scatterlist interface.  Changed LGPL to GPL per section 3 of the LGPL.
11  *
12  * s390 Version:
13  *   Copyright (C) 2003 IBM Deutschland GmbH, IBM Corporation
14  *   Author(s): Thomas Spatzier (tspat@de.ibm.com)
15  *
16  * Derived from "crypto/des.c"
17  *   Copyright (c) 1992 Dana L. How.
18  *   Copyright (c) Raimar Falke <rf13@inf.tu-dresden.de>
19  *   Copyright (c) Gisle Sflensminde <gisle@ii.uib.no>
20  *   Copyright (C) 2001 Niels Mvller.
21  *   Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
22  *
23  * This program is free software; you can redistribute it and/or modify
24  * it under the terms of the GNU General Public License as published by
25  * the Free Software Foundation; either version 2 of the License, or
26  * (at your option) any later version.
27  *
28  */
29 #include <linux/init.h>
30 #include <linux/module.h>
31 #include <linux/errno.h>
32 #include <linux/crypto.h>
33 #include "crypto_des.h"
34
35 #define ROR(d,c,o)      ((d) = (d) >> (c) | (d) << (o))
36
37 static const u8 parity[] = {
38         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,
39         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,
40         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,
41         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,
42         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,
43         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,
44         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,
45         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,
46 };
47
48 /*
49  * RFC2451: Weak key checks SHOULD be performed.
50  */
51 int
52 crypto_des_check_key(const u8 *key, unsigned int keylen, u32 *flags)
53 {
54         u32 n, w;
55
56         n  = parity[key[0]]; n <<= 4;
57         n |= parity[key[1]]; n <<= 4;
58         n |= parity[key[2]]; n <<= 4;
59         n |= parity[key[3]]; n <<= 4;
60         n |= parity[key[4]]; n <<= 4;
61         n |= parity[key[5]]; n <<= 4;
62         n |= parity[key[6]]; n <<= 4;
63         n |= parity[key[7]];
64         w = 0x88888888L;
65
66         if ((*flags & CRYPTO_TFM_REQ_WEAK_KEY)
67             && !((n - (w >> 3)) & w)) {  /* 1 in 10^10 keys passes this test */
68                 if (n < 0x41415151) {
69                         if (n < 0x31312121) {
70                                 if (n < 0x14141515) {
71                                         /* 01 01 01 01 01 01 01 01 */
72                                         if (n == 0x11111111) goto weak;
73                                         /* 01 1F 01 1F 01 0E 01 0E */
74                                         if (n == 0x13131212) goto weak;
75                                 } else {
76                                         /* 01 E0 01 E0 01 F1 01 F1 */
77                                         if (n == 0x14141515) goto weak;
78                                         /* 01 FE 01 FE 01 FE 01 FE */
79                                         if (n == 0x16161616) goto weak;
80                                 }
81                         } else {
82                                 if (n < 0x34342525) {
83                                         /* 1F 01 1F 01 0E 01 0E 01 */
84                                         if (n == 0x31312121) goto weak;
85                                         /* 1F 1F 1F 1F 0E 0E 0E 0E (?) */
86                                         if (n == 0x33332222) goto weak;
87                                 } else {
88                                         /* 1F E0 1F E0 0E F1 0E F1 */
89                                         if (n == 0x34342525) goto weak;
90                                         /* 1F FE 1F FE 0E FE 0E FE */
91                                         if (n == 0x36362626) goto weak;
92                                 }
93                         }
94                 } else {
95                         if (n < 0x61616161) {
96                                 if (n < 0x44445555) {
97                                         /* E0 01 E0 01 F1 01 F1 01 */
98                                         if (n == 0x41415151) goto weak;
99                                         /* E0 1F E0 1F F1 0E F1 0E */
100                                         if (n == 0x43435252) goto weak;
101                                 } else {
102                                         /* E0 E0 E0 E0 F1 F1 F1 F1 (?) */
103                                         if (n == 0x44445555) goto weak;
104                                         /* E0 FE E0 FE F1 FE F1 FE */
105                                         if (n == 0x46465656) goto weak;
106                                 }
107                         } else {
108                                 if (n < 0x64646565) {
109                                         /* FE 01 FE 01 FE 01 FE 01 */
110                                         if (n == 0x61616161) goto weak;
111                                         /* FE 1F FE 1F FE 0E FE 0E */
112                                         if (n == 0x63636262) goto weak;
113                                 } else {
114                                         /* FE E0 FE E0 FE F1 FE F1 */
115                                         if (n == 0x64646565) goto weak;
116                                         /* FE FE FE FE FE FE FE FE */
117                                         if (n == 0x66666666) goto weak;
118                                 }
119                         }
120                 }
121         }
122         return 0;
123 weak:
124         *flags |= CRYPTO_TFM_RES_WEAK_KEY;
125         return -EINVAL;
126 }
127
128 EXPORT_SYMBOL(crypto_des_check_key);
129
130 MODULE_LICENSE("GPL");
131 MODULE_DESCRIPTION("Key Check function for DES &  DES3 Cipher Algorithms");