Make Kprobes depend on modules
[linux-2.6.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
34 #define ROR(d,c,o)      ((d) = (d) >> (c) | (d) << (o))
35
36 static const u8 parity[] = {
37         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,
38         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,
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         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,
41         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,
42         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,
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         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,
45 };
46
47 /*
48  * RFC2451: Weak key checks SHOULD be performed.
49  */
50 int
51 crypto_des_check_key(const u8 *key, unsigned int keylen, u32 *flags)
52 {
53         u32 n, w;
54
55         n  = parity[key[0]]; n <<= 4;
56         n |= parity[key[1]]; n <<= 4;
57         n |= parity[key[2]]; n <<= 4;
58         n |= parity[key[3]]; n <<= 4;
59         n |= parity[key[4]]; n <<= 4;
60         n |= parity[key[5]]; n <<= 4;
61         n |= parity[key[6]]; n <<= 4;
62         n |= parity[key[7]];
63         w = 0x88888888L;
64
65         if ((*flags & CRYPTO_TFM_REQ_WEAK_KEY)
66             && !((n - (w >> 3)) & w)) {  /* 1 in 10^10 keys passes this test */
67                 if (n < 0x41415151) {
68                         if (n < 0x31312121) {
69                                 if (n < 0x14141515) {
70                                         /* 01 01 01 01 01 01 01 01 */
71                                         if (n == 0x11111111) goto weak;
72                                         /* 01 1F 01 1F 01 0E 01 0E */
73                                         if (n == 0x13131212) goto weak;
74                                 } else {
75                                         /* 01 E0 01 E0 01 F1 01 F1 */
76                                         if (n == 0x14141515) goto weak;
77                                         /* 01 FE 01 FE 01 FE 01 FE */
78                                         if (n == 0x16161616) goto weak;
79                                 }
80                         } else {
81                                 if (n < 0x34342525) {
82                                         /* 1F 01 1F 01 0E 01 0E 01 */
83                                         if (n == 0x31312121) goto weak;
84                                         /* 1F 1F 1F 1F 0E 0E 0E 0E (?) */
85                                         if (n == 0x33332222) goto weak;
86                                 } else {
87                                         /* 1F E0 1F E0 0E F1 0E F1 */
88                                         if (n == 0x34342525) goto weak;
89                                         /* 1F FE 1F FE 0E FE 0E FE */
90                                         if (n == 0x36362626) goto weak;
91                                 }
92                         }
93                 } else {
94                         if (n < 0x61616161) {
95                                 if (n < 0x44445555) {
96                                         /* E0 01 E0 01 F1 01 F1 01 */
97                                         if (n == 0x41415151) goto weak;
98                                         /* E0 1F E0 1F F1 0E F1 0E */
99                                         if (n == 0x43435252) goto weak;
100                                 } else {
101                                         /* E0 E0 E0 E0 F1 F1 F1 F1 (?) */
102                                         if (n == 0x44445555) goto weak;
103                                         /* E0 FE E0 FE F1 FE F1 FE */
104                                         if (n == 0x46465656) goto weak;
105                                 }
106                         } else {
107                                 if (n < 0x64646565) {
108                                         /* FE 01 FE 01 FE 01 FE 01 */
109                                         if (n == 0x61616161) goto weak;
110                                         /* FE 1F FE 1F FE 0E FE 0E */
111                                         if (n == 0x63636262) goto weak;
112                                 } else {
113                                         /* FE E0 FE E0 FE F1 FE F1 */
114                                         if (n == 0x64646565) goto weak;
115                                         /* FE FE FE FE FE FE FE FE */
116                                         if (n == 0x66666666) goto weak;
117                                 }
118                         }
119                 }
120         }
121         return 0;
122 weak:
123         *flags |= CRYPTO_TFM_RES_WEAK_KEY;
124         return -EINVAL;
125 }
126
127 EXPORT_SYMBOL(crypto_des_check_key);
128
129 MODULE_LICENSE("GPL");
130 MODULE_DESCRIPTION("Key Check function for DES &  DES3 Cipher Algorithms");