Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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: |
Jan Glauber | 86aa9fc | 2007-02-05 21:18:14 +0100 | [diff] [blame] | 13 | * Copyright IBM Corp. 2003 |
| 14 | * Author(s): Thomas Spatzier |
| 15 | * Jan Glauber (jan.glauber@de.ibm.com) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | * |
| 17 | * Derived from "crypto/des.c" |
| 18 | * Copyright (c) 1992 Dana L. How. |
| 19 | * Copyright (c) Raimar Falke <rf13@inf.tu-dresden.de> |
| 20 | * Copyright (c) Gisle Sflensminde <gisle@ii.uib.no> |
| 21 | * Copyright (C) 2001 Niels Mvller. |
| 22 | * Copyright (c) 2002 James Morris <jmorris@intercode.com.au> |
| 23 | * |
| 24 | * This program is free software; you can redistribute it and/or modify |
| 25 | * it under the terms of the GNU General Public License as published by |
| 26 | * the Free Software Foundation; either version 2 of the License, or |
| 27 | * (at your option) any later version. |
| 28 | * |
| 29 | */ |
| 30 | #include <linux/init.h> |
| 31 | #include <linux/module.h> |
| 32 | #include <linux/errno.h> |
| 33 | #include <linux/crypto.h> |
Heiko Carstens | 2b67fc4 | 2007-02-05 21:16:47 +0100 | [diff] [blame] | 34 | #include "crypto_des.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | |
| 36 | #define ROR(d,c,o) ((d) = (d) >> (c) | (d) << (o)) |
| 37 | |
| 38 | static const u8 parity[] = { |
| 39 | 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, |
| 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 | 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 | 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, |
| 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 | 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, |
| 46 | 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, |
| 47 | }; |
| 48 | |
| 49 | /* |
| 50 | * RFC2451: Weak key checks SHOULD be performed. |
| 51 | */ |
| 52 | int |
| 53 | crypto_des_check_key(const u8 *key, unsigned int keylen, u32 *flags) |
| 54 | { |
| 55 | u32 n, w; |
| 56 | |
| 57 | n = parity[key[0]]; n <<= 4; |
| 58 | n |= parity[key[1]]; n <<= 4; |
| 59 | n |= parity[key[2]]; n <<= 4; |
| 60 | n |= parity[key[3]]; n <<= 4; |
| 61 | n |= parity[key[4]]; n <<= 4; |
| 62 | n |= parity[key[5]]; n <<= 4; |
| 63 | n |= parity[key[6]]; n <<= 4; |
| 64 | n |= parity[key[7]]; |
| 65 | w = 0x88888888L; |
| 66 | |
| 67 | if ((*flags & CRYPTO_TFM_REQ_WEAK_KEY) |
| 68 | && !((n - (w >> 3)) & w)) { /* 1 in 10^10 keys passes this test */ |
| 69 | if (n < 0x41415151) { |
| 70 | if (n < 0x31312121) { |
| 71 | if (n < 0x14141515) { |
| 72 | /* 01 01 01 01 01 01 01 01 */ |
| 73 | if (n == 0x11111111) goto weak; |
| 74 | /* 01 1F 01 1F 01 0E 01 0E */ |
| 75 | if (n == 0x13131212) goto weak; |
| 76 | } else { |
| 77 | /* 01 E0 01 E0 01 F1 01 F1 */ |
| 78 | if (n == 0x14141515) goto weak; |
| 79 | /* 01 FE 01 FE 01 FE 01 FE */ |
| 80 | if (n == 0x16161616) goto weak; |
| 81 | } |
| 82 | } else { |
| 83 | if (n < 0x34342525) { |
| 84 | /* 1F 01 1F 01 0E 01 0E 01 */ |
| 85 | if (n == 0x31312121) goto weak; |
| 86 | /* 1F 1F 1F 1F 0E 0E 0E 0E (?) */ |
| 87 | if (n == 0x33332222) goto weak; |
| 88 | } else { |
| 89 | /* 1F E0 1F E0 0E F1 0E F1 */ |
| 90 | if (n == 0x34342525) goto weak; |
| 91 | /* 1F FE 1F FE 0E FE 0E FE */ |
| 92 | if (n == 0x36362626) goto weak; |
| 93 | } |
| 94 | } |
| 95 | } else { |
| 96 | if (n < 0x61616161) { |
| 97 | if (n < 0x44445555) { |
| 98 | /* E0 01 E0 01 F1 01 F1 01 */ |
| 99 | if (n == 0x41415151) goto weak; |
| 100 | /* E0 1F E0 1F F1 0E F1 0E */ |
| 101 | if (n == 0x43435252) goto weak; |
| 102 | } else { |
| 103 | /* E0 E0 E0 E0 F1 F1 F1 F1 (?) */ |
| 104 | if (n == 0x44445555) goto weak; |
| 105 | /* E0 FE E0 FE F1 FE F1 FE */ |
| 106 | if (n == 0x46465656) goto weak; |
| 107 | } |
| 108 | } else { |
| 109 | if (n < 0x64646565) { |
| 110 | /* FE 01 FE 01 FE 01 FE 01 */ |
| 111 | if (n == 0x61616161) goto weak; |
| 112 | /* FE 1F FE 1F FE 0E FE 0E */ |
| 113 | if (n == 0x63636262) goto weak; |
| 114 | } else { |
| 115 | /* FE E0 FE E0 FE F1 FE F1 */ |
| 116 | if (n == 0x64646565) goto weak; |
| 117 | /* FE FE FE FE FE FE FE FE */ |
| 118 | if (n == 0x66666666) goto weak; |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | return 0; |
| 124 | weak: |
| 125 | *flags |= CRYPTO_TFM_RES_WEAK_KEY; |
| 126 | return -EINVAL; |
| 127 | } |
| 128 | |
| 129 | EXPORT_SYMBOL(crypto_des_check_key); |
| 130 | |
| 131 | MODULE_LICENSE("GPL"); |
| 132 | MODULE_DESCRIPTION("Key Check function for DES & DES3 Cipher Algorithms"); |