Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Cryptographic API. |
| 3 | * |
Jan Glauber | c1e26e1 | 2006-01-06 00:19:17 -0800 | [diff] [blame] | 4 | * s390 implementation of the DES Cipher Algorithm. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * |
| 6 | * Copyright (c) 2003 IBM Deutschland Entwicklung GmbH, IBM Corporation |
| 7 | * Author(s): Thomas Spatzier (tspat@de.ibm.com) |
| 8 | * |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation; either version 2 of the License, or |
| 13 | * (at your option) any later version. |
| 14 | * |
| 15 | */ |
| 16 | #include <linux/init.h> |
| 17 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <linux/crypto.h> |
Jan Glauber | c135783 | 2006-01-14 13:20:53 -0800 | [diff] [blame^] | 19 | |
Jan Glauber | c1e26e1 | 2006-01-06 00:19:17 -0800 | [diff] [blame] | 20 | #include "crypt_s390.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include "crypto_des.h" |
| 22 | |
| 23 | #define DES_BLOCK_SIZE 8 |
| 24 | #define DES_KEY_SIZE 8 |
| 25 | |
| 26 | #define DES3_128_KEY_SIZE (2 * DES_KEY_SIZE) |
| 27 | #define DES3_128_BLOCK_SIZE DES_BLOCK_SIZE |
| 28 | |
| 29 | #define DES3_192_KEY_SIZE (3 * DES_KEY_SIZE) |
| 30 | #define DES3_192_BLOCK_SIZE DES_BLOCK_SIZE |
| 31 | |
Jan Glauber | c1e26e1 | 2006-01-06 00:19:17 -0800 | [diff] [blame] | 32 | struct crypt_s390_des_ctx { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | u8 iv[DES_BLOCK_SIZE]; |
| 34 | u8 key[DES_KEY_SIZE]; |
| 35 | }; |
| 36 | |
Jan Glauber | c1e26e1 | 2006-01-06 00:19:17 -0800 | [diff] [blame] | 37 | struct crypt_s390_des3_128_ctx { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | u8 iv[DES_BLOCK_SIZE]; |
| 39 | u8 key[DES3_128_KEY_SIZE]; |
| 40 | }; |
| 41 | |
Jan Glauber | c1e26e1 | 2006-01-06 00:19:17 -0800 | [diff] [blame] | 42 | struct crypt_s390_des3_192_ctx { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | u8 iv[DES_BLOCK_SIZE]; |
| 44 | u8 key[DES3_192_KEY_SIZE]; |
| 45 | }; |
| 46 | |
Jan Glauber | c135783 | 2006-01-14 13:20:53 -0800 | [diff] [blame^] | 47 | static int des_setkey(void *ctx, const u8 *key, unsigned int keylen, |
| 48 | u32 *flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | { |
Jan Glauber | c135783 | 2006-01-14 13:20:53 -0800 | [diff] [blame^] | 50 | struct crypt_s390_des_ctx *dctx = ctx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | int ret; |
| 52 | |
Jan Glauber | c135783 | 2006-01-14 13:20:53 -0800 | [diff] [blame^] | 53 | /* test if key is valid (not a weak key) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | ret = crypto_des_check_key(key, keylen, flags); |
Jan Glauber | c135783 | 2006-01-14 13:20:53 -0800 | [diff] [blame^] | 55 | if (ret == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | memcpy(dctx->key, key, keylen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | return ret; |
| 58 | } |
| 59 | |
Jan Glauber | c135783 | 2006-01-14 13:20:53 -0800 | [diff] [blame^] | 60 | static void des_encrypt(void *ctx, u8 *dst, const u8 *src) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | { |
Jan Glauber | c135783 | 2006-01-14 13:20:53 -0800 | [diff] [blame^] | 62 | struct crypt_s390_des_ctx *dctx = ctx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | |
Jan Glauber | c1e26e1 | 2006-01-06 00:19:17 -0800 | [diff] [blame] | 64 | crypt_s390_km(KM_DEA_ENCRYPT, dctx->key, dst, src, DES_BLOCK_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | } |
| 66 | |
Jan Glauber | c135783 | 2006-01-14 13:20:53 -0800 | [diff] [blame^] | 67 | static void des_decrypt(void *ctx, u8 *dst, const u8 *src) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | { |
Jan Glauber | c135783 | 2006-01-14 13:20:53 -0800 | [diff] [blame^] | 69 | struct crypt_s390_des_ctx *dctx = ctx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | |
Jan Glauber | c1e26e1 | 2006-01-06 00:19:17 -0800 | [diff] [blame] | 71 | crypt_s390_km(KM_DEA_DECRYPT, dctx->key, dst, src, DES_BLOCK_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | static struct crypto_alg des_alg = { |
| 75 | .cra_name = "des", |
| 76 | .cra_flags = CRYPTO_ALG_TYPE_CIPHER, |
| 77 | .cra_blocksize = DES_BLOCK_SIZE, |
Jan Glauber | c1e26e1 | 2006-01-06 00:19:17 -0800 | [diff] [blame] | 78 | .cra_ctxsize = sizeof(struct crypt_s390_des_ctx), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | .cra_module = THIS_MODULE, |
| 80 | .cra_list = LIST_HEAD_INIT(des_alg.cra_list), |
Jan Glauber | c135783 | 2006-01-14 13:20:53 -0800 | [diff] [blame^] | 81 | .cra_u = { |
| 82 | .cipher = { |
| 83 | .cia_min_keysize = DES_KEY_SIZE, |
| 84 | .cia_max_keysize = DES_KEY_SIZE, |
| 85 | .cia_setkey = des_setkey, |
| 86 | .cia_encrypt = des_encrypt, |
| 87 | .cia_decrypt = des_decrypt |
| 88 | } |
| 89 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | }; |
| 91 | |
| 92 | /* |
| 93 | * RFC2451: |
| 94 | * |
| 95 | * For DES-EDE3, there is no known need to reject weak or |
| 96 | * complementation keys. Any weakness is obviated by the use of |
| 97 | * multiple keys. |
| 98 | * |
| 99 | * However, if the two independent 64-bit keys are equal, |
| 100 | * then the DES3 operation is simply the same as DES. |
| 101 | * Implementers MUST reject keys that exhibit this property. |
| 102 | * |
| 103 | */ |
Jan Glauber | c135783 | 2006-01-14 13:20:53 -0800 | [diff] [blame^] | 104 | static int des3_128_setkey(void *ctx, const u8 *key, unsigned int keylen, |
| 105 | u32 *flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | { |
| 107 | int i, ret; |
Jan Glauber | c135783 | 2006-01-14 13:20:53 -0800 | [diff] [blame^] | 108 | struct crypt_s390_des3_128_ctx *dctx = ctx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | const u8* temp_key = key; |
| 110 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | if (!(memcmp(key, &key[DES_KEY_SIZE], DES_KEY_SIZE))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | *flags |= CRYPTO_TFM_RES_BAD_KEY_SCHED; |
| 113 | return -EINVAL; |
| 114 | } |
Jan Glauber | c135783 | 2006-01-14 13:20:53 -0800 | [diff] [blame^] | 115 | for (i = 0; i < 2; i++, temp_key += DES_KEY_SIZE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | ret = crypto_des_check_key(temp_key, DES_KEY_SIZE, flags); |
| 117 | if (ret < 0) |
| 118 | return ret; |
| 119 | } |
| 120 | memcpy(dctx->key, key, keylen); |
| 121 | return 0; |
| 122 | } |
| 123 | |
Jan Glauber | c135783 | 2006-01-14 13:20:53 -0800 | [diff] [blame^] | 124 | static void des3_128_encrypt(void *ctx, u8 *dst, const u8 *src) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | { |
Jan Glauber | c135783 | 2006-01-14 13:20:53 -0800 | [diff] [blame^] | 126 | struct crypt_s390_des3_128_ctx *dctx = ctx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | |
Jan Glauber | c1e26e1 | 2006-01-06 00:19:17 -0800 | [diff] [blame] | 128 | crypt_s390_km(KM_TDEA_128_ENCRYPT, dctx->key, dst, (void*)src, |
Jan Glauber | c135783 | 2006-01-14 13:20:53 -0800 | [diff] [blame^] | 129 | DES3_128_BLOCK_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Jan Glauber | c135783 | 2006-01-14 13:20:53 -0800 | [diff] [blame^] | 132 | static void des3_128_decrypt(void *ctx, u8 *dst, const u8 *src) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | { |
Jan Glauber | c135783 | 2006-01-14 13:20:53 -0800 | [diff] [blame^] | 134 | struct crypt_s390_des3_128_ctx *dctx = ctx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | |
Jan Glauber | c1e26e1 | 2006-01-06 00:19:17 -0800 | [diff] [blame] | 136 | crypt_s390_km(KM_TDEA_128_DECRYPT, dctx->key, dst, (void*)src, |
Jan Glauber | c135783 | 2006-01-14 13:20:53 -0800 | [diff] [blame^] | 137 | DES3_128_BLOCK_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | static struct crypto_alg des3_128_alg = { |
| 141 | .cra_name = "des3_ede128", |
| 142 | .cra_flags = CRYPTO_ALG_TYPE_CIPHER, |
| 143 | .cra_blocksize = DES3_128_BLOCK_SIZE, |
Jan Glauber | c1e26e1 | 2006-01-06 00:19:17 -0800 | [diff] [blame] | 144 | .cra_ctxsize = sizeof(struct crypt_s390_des3_128_ctx), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | .cra_module = THIS_MODULE, |
| 146 | .cra_list = LIST_HEAD_INIT(des3_128_alg.cra_list), |
Jan Glauber | c135783 | 2006-01-14 13:20:53 -0800 | [diff] [blame^] | 147 | .cra_u = { |
| 148 | .cipher = { |
| 149 | .cia_min_keysize = DES3_128_KEY_SIZE, |
| 150 | .cia_max_keysize = DES3_128_KEY_SIZE, |
| 151 | .cia_setkey = des3_128_setkey, |
| 152 | .cia_encrypt = des3_128_encrypt, |
| 153 | .cia_decrypt = des3_128_decrypt |
| 154 | } |
| 155 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | }; |
| 157 | |
| 158 | /* |
| 159 | * RFC2451: |
| 160 | * |
| 161 | * For DES-EDE3, there is no known need to reject weak or |
| 162 | * complementation keys. Any weakness is obviated by the use of |
| 163 | * multiple keys. |
| 164 | * |
| 165 | * However, if the first two or last two independent 64-bit keys are |
| 166 | * equal (k1 == k2 or k2 == k3), then the DES3 operation is simply the |
| 167 | * same as DES. Implementers MUST reject keys that exhibit this |
| 168 | * property. |
| 169 | * |
| 170 | */ |
Jan Glauber | c135783 | 2006-01-14 13:20:53 -0800 | [diff] [blame^] | 171 | static int des3_192_setkey(void *ctx, const u8 *key, unsigned int keylen, |
| 172 | u32 *flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | { |
| 174 | int i, ret; |
Jan Glauber | c135783 | 2006-01-14 13:20:53 -0800 | [diff] [blame^] | 175 | struct crypt_s390_des3_192_ctx *dctx = ctx; |
| 176 | const u8* temp_key = key; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | if (!(memcmp(key, &key[DES_KEY_SIZE], DES_KEY_SIZE) && |
| 179 | memcmp(&key[DES_KEY_SIZE], &key[DES_KEY_SIZE * 2], |
Jan Glauber | c135783 | 2006-01-14 13:20:53 -0800 | [diff] [blame^] | 180 | DES_KEY_SIZE))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | |
| 182 | *flags |= CRYPTO_TFM_RES_BAD_KEY_SCHED; |
| 183 | return -EINVAL; |
| 184 | } |
| 185 | for (i = 0; i < 3; i++, temp_key += DES_KEY_SIZE) { |
| 186 | ret = crypto_des_check_key(temp_key, DES_KEY_SIZE, flags); |
Jan Glauber | c135783 | 2006-01-14 13:20:53 -0800 | [diff] [blame^] | 187 | if (ret < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | } |
| 190 | memcpy(dctx->key, key, keylen); |
| 191 | return 0; |
| 192 | } |
| 193 | |
Jan Glauber | c135783 | 2006-01-14 13:20:53 -0800 | [diff] [blame^] | 194 | static void des3_192_encrypt(void *ctx, u8 *dst, const u8 *src) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | { |
Jan Glauber | c135783 | 2006-01-14 13:20:53 -0800 | [diff] [blame^] | 196 | struct crypt_s390_des3_192_ctx *dctx = ctx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | |
Jan Glauber | c1e26e1 | 2006-01-06 00:19:17 -0800 | [diff] [blame] | 198 | crypt_s390_km(KM_TDEA_192_ENCRYPT, dctx->key, dst, (void*)src, |
Jan Glauber | c135783 | 2006-01-14 13:20:53 -0800 | [diff] [blame^] | 199 | DES3_192_BLOCK_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | } |
| 201 | |
Jan Glauber | c135783 | 2006-01-14 13:20:53 -0800 | [diff] [blame^] | 202 | static void des3_192_decrypt(void *ctx, u8 *dst, const u8 *src) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | { |
Jan Glauber | c135783 | 2006-01-14 13:20:53 -0800 | [diff] [blame^] | 204 | struct crypt_s390_des3_192_ctx *dctx = ctx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | |
Jan Glauber | c1e26e1 | 2006-01-06 00:19:17 -0800 | [diff] [blame] | 206 | crypt_s390_km(KM_TDEA_192_DECRYPT, dctx->key, dst, (void*)src, |
Jan Glauber | c135783 | 2006-01-14 13:20:53 -0800 | [diff] [blame^] | 207 | DES3_192_BLOCK_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | static struct crypto_alg des3_192_alg = { |
| 211 | .cra_name = "des3_ede", |
| 212 | .cra_flags = CRYPTO_ALG_TYPE_CIPHER, |
| 213 | .cra_blocksize = DES3_192_BLOCK_SIZE, |
Jan Glauber | c1e26e1 | 2006-01-06 00:19:17 -0800 | [diff] [blame] | 214 | .cra_ctxsize = sizeof(struct crypt_s390_des3_192_ctx), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | .cra_module = THIS_MODULE, |
| 216 | .cra_list = LIST_HEAD_INIT(des3_192_alg.cra_list), |
Jan Glauber | c135783 | 2006-01-14 13:20:53 -0800 | [diff] [blame^] | 217 | .cra_u = { |
| 218 | .cipher = { |
| 219 | .cia_min_keysize = DES3_192_KEY_SIZE, |
| 220 | .cia_max_keysize = DES3_192_KEY_SIZE, |
| 221 | .cia_setkey = des3_192_setkey, |
| 222 | .cia_encrypt = des3_192_encrypt, |
| 223 | .cia_decrypt = des3_192_decrypt |
| 224 | } |
| 225 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | }; |
| 227 | |
Jan Glauber | c135783 | 2006-01-14 13:20:53 -0800 | [diff] [blame^] | 228 | static int init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | { |
Jan Glauber | c135783 | 2006-01-14 13:20:53 -0800 | [diff] [blame^] | 230 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | |
Jan Glauber | c1e26e1 | 2006-01-06 00:19:17 -0800 | [diff] [blame] | 232 | if (!crypt_s390_func_available(KM_DEA_ENCRYPT) || |
| 233 | !crypt_s390_func_available(KM_TDEA_128_ENCRYPT) || |
Jan Glauber | c135783 | 2006-01-14 13:20:53 -0800 | [diff] [blame^] | 234 | !crypt_s390_func_available(KM_TDEA_192_ENCRYPT)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | return -ENOSYS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | |
Jan Glauber | c135783 | 2006-01-14 13:20:53 -0800 | [diff] [blame^] | 237 | ret |= (crypto_register_alg(&des_alg) == 0) ? 0:1; |
| 238 | ret |= (crypto_register_alg(&des3_128_alg) == 0) ? 0:2; |
| 239 | ret |= (crypto_register_alg(&des3_192_alg) == 0) ? 0:4; |
| 240 | if (ret) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | crypto_unregister_alg(&des3_192_alg); |
| 242 | crypto_unregister_alg(&des3_128_alg); |
| 243 | crypto_unregister_alg(&des_alg); |
| 244 | return -EEXIST; |
| 245 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | return 0; |
| 247 | } |
| 248 | |
Jan Glauber | c135783 | 2006-01-14 13:20:53 -0800 | [diff] [blame^] | 249 | static void __exit fini(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | { |
| 251 | crypto_unregister_alg(&des3_192_alg); |
| 252 | crypto_unregister_alg(&des3_128_alg); |
| 253 | crypto_unregister_alg(&des_alg); |
| 254 | } |
| 255 | |
| 256 | module_init(init); |
| 257 | module_exit(fini); |
| 258 | |
| 259 | MODULE_ALIAS("des"); |
| 260 | MODULE_ALIAS("des3_ede"); |
| 261 | |
| 262 | MODULE_LICENSE("GPL"); |
| 263 | MODULE_DESCRIPTION("DES & Triple DES EDE Cipher Algorithms"); |