|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
| 2 | +/* |
| 3 | + * Speck: a lightweight block cipher |
| 4 | + * |
| 5 | + * Copyright (c) 2018 Google, Inc |
| 6 | + * |
| 7 | + * Speck has 10 variants, including 5 block sizes. For now we only implement |
| 8 | + * the variants Speck128/128, Speck128/192, Speck128/256, Speck64/96, and |
| 9 | + * Speck64/128. Speck${B}/${K} denotes the variant with a block size of B bits |
| 10 | + * and a key size of K bits. The Speck128 variants are believed to be the most |
| 11 | + * secure variants, and they use the same block size and key sizes as AES. The |
| 12 | + * Speck64 variants are less secure, but on 32-bit processors are usually |
| 13 | + * faster. The remaining variants (Speck32, Speck48, and Speck96) are even less |
| 14 | + * secure and/or not as well suited for implementation on either 32-bit or |
| 15 | + * 64-bit processors, so are omitted. |
| 16 | + * |
| 17 | + * Reference: "The Simon and Speck Families of Lightweight Block Ciphers" |
| 18 | + * https://eprint.iacr.org/2013/404.pdf |
| 19 | + * |
| 20 | + * In a correspondence, the Speck designers have also clarified that the words |
| 21 | + * should be interpreted in little-endian format, and the words should be |
| 22 | + * ordered such that the first word of each block is 'y' rather than 'x', and |
| 23 | + * the first key word (rather than the last) becomes the first round key. |
| 24 | + */ |
| 25 | + |
| 26 | +#include <asm/unaligned.h> |
| 27 | +#include <linux/bitops.h> |
| 28 | +#include <linux/crypto.h> |
| 29 | +#include <linux/init.h> |
| 30 | +#include <linux/module.h> |
| 31 | + |
| 32 | +/* Speck128 */ |
| 33 | + |
| 34 | +#define SPECK128_BLOCK_SIZE 16 |
| 35 | + |
| 36 | +#define SPECK128_128_KEY_SIZE 16 |
| 37 | +#define SPECK128_128_NROUNDS 32 |
| 38 | + |
| 39 | +#define SPECK128_192_KEY_SIZE 24 |
| 40 | +#define SPECK128_192_NROUNDS 33 |
| 41 | + |
| 42 | +#define SPECK128_256_KEY_SIZE 32 |
| 43 | +#define SPECK128_256_NROUNDS 34 |
| 44 | + |
| 45 | +struct speck128_tfm_ctx { |
| 46 | + u64 round_keys[SPECK128_256_NROUNDS]; |
| 47 | + int nrounds; |
| 48 | +}; |
| 49 | + |
| 50 | +static __always_inline void speck128_round(u64 *x, u64 *y, u64 k) |
| 51 | +{ |
| 52 | + *x = ror64(*x, 8); |
| 53 | + *x += *y; |
| 54 | + *x ^= k; |
| 55 | + *y = rol64(*y, 3); |
| 56 | + *y ^= *x; |
| 57 | +} |
| 58 | + |
| 59 | +static __always_inline void speck128_unround(u64 *x, u64 *y, u64 k) |
| 60 | +{ |
| 61 | + *y ^= *x; |
| 62 | + *y = ror64(*y, 3); |
| 63 | + *x ^= k; |
| 64 | + *x -= *y; |
| 65 | + *x = rol64(*x, 8); |
| 66 | +} |
| 67 | + |
| 68 | +static void speck128_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in) |
| 69 | +{ |
| 70 | + const struct speck128_tfm_ctx *ctx = crypto_tfm_ctx(tfm); |
| 71 | + u64 y = get_unaligned_le64(in); |
| 72 | + u64 x = get_unaligned_le64(in + 8); |
| 73 | + int i; |
| 74 | + |
| 75 | + for (i = 0; i < ctx->nrounds; i++) |
| 76 | + speck128_round(&x, &y, ctx->round_keys[i]); |
| 77 | + |
| 78 | + put_unaligned_le64(y, out); |
| 79 | + put_unaligned_le64(x, out + 8); |
| 80 | +} |
| 81 | + |
| 82 | +static void speck128_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in) |
| 83 | +{ |
| 84 | + const struct speck128_tfm_ctx *ctx = crypto_tfm_ctx(tfm); |
| 85 | + u64 y = get_unaligned_le64(in); |
| 86 | + u64 x = get_unaligned_le64(in + 8); |
| 87 | + int i; |
| 88 | + |
| 89 | + for (i = ctx->nrounds - 1; i >= 0; i--) |
| 90 | + speck128_unround(&x, &y, ctx->round_keys[i]); |
| 91 | + |
| 92 | + put_unaligned_le64(y, out); |
| 93 | + put_unaligned_le64(x, out + 8); |
| 94 | +} |
| 95 | + |
| 96 | +static int speck128_setkey(struct crypto_tfm *tfm, const u8 *key, |
| 97 | + unsigned int keylen) |
| 98 | +{ |
| 99 | + struct speck128_tfm_ctx *ctx = crypto_tfm_ctx(tfm); |
| 100 | + u64 l[3]; |
| 101 | + u64 k; |
| 102 | + int i; |
| 103 | + |
| 104 | + switch (keylen) { |
| 105 | + case SPECK128_128_KEY_SIZE: |
| 106 | + k = get_unaligned_le64(key); |
| 107 | + l[0] = get_unaligned_le64(key + 8); |
| 108 | + ctx->nrounds = SPECK128_128_NROUNDS; |
| 109 | + for (i = 0; i < ctx->nrounds; i++) { |
| 110 | + ctx->round_keys[i] = k; |
| 111 | + speck128_round(&l[0], &k, i); |
| 112 | + } |
| 113 | + break; |
| 114 | + case SPECK128_192_KEY_SIZE: |
| 115 | + k = get_unaligned_le64(key); |
| 116 | + l[0] = get_unaligned_le64(key + 8); |
| 117 | + l[1] = get_unaligned_le64(key + 16); |
| 118 | + ctx->nrounds = SPECK128_192_NROUNDS; |
| 119 | + for (i = 0; i < ctx->nrounds; i++) { |
| 120 | + ctx->round_keys[i] = k; |
| 121 | + speck128_round(&l[i % 2], &k, i); |
| 122 | + } |
| 123 | + break; |
| 124 | + case SPECK128_256_KEY_SIZE: |
| 125 | + k = get_unaligned_le64(key); |
| 126 | + l[0] = get_unaligned_le64(key + 8); |
| 127 | + l[1] = get_unaligned_le64(key + 16); |
| 128 | + l[2] = get_unaligned_le64(key + 24); |
| 129 | + ctx->nrounds = SPECK128_256_NROUNDS; |
| 130 | + for (i = 0; i < ctx->nrounds; i++) { |
| 131 | + ctx->round_keys[i] = k; |
| 132 | + speck128_round(&l[i % 3], &k, i); |
| 133 | + } |
| 134 | + break; |
| 135 | + default: |
| 136 | + return -EINVAL; |
| 137 | + } |
| 138 | + |
| 139 | + return 0; |
| 140 | +} |
| 141 | + |
| 142 | +/* Speck64 */ |
| 143 | + |
| 144 | +#define SPECK64_BLOCK_SIZE 8 |
| 145 | + |
| 146 | +#define SPECK64_96_KEY_SIZE 12 |
| 147 | +#define SPECK64_96_NROUNDS 26 |
| 148 | + |
| 149 | +#define SPECK64_128_KEY_SIZE 16 |
| 150 | +#define SPECK64_128_NROUNDS 27 |
| 151 | + |
| 152 | +struct speck64_tfm_ctx { |
| 153 | + u32 round_keys[SPECK64_128_NROUNDS]; |
| 154 | + int nrounds; |
| 155 | +}; |
| 156 | + |
| 157 | +static __always_inline void speck64_round(u32 *x, u32 *y, u32 k) |
| 158 | +{ |
| 159 | + *x = ror32(*x, 8); |
| 160 | + *x += *y; |
| 161 | + *x ^= k; |
| 162 | + *y = rol32(*y, 3); |
| 163 | + *y ^= *x; |
| 164 | +} |
| 165 | + |
| 166 | +static __always_inline void speck64_unround(u32 *x, u32 *y, u32 k) |
| 167 | +{ |
| 168 | + *y ^= *x; |
| 169 | + *y = ror32(*y, 3); |
| 170 | + *x ^= k; |
| 171 | + *x -= *y; |
| 172 | + *x = rol32(*x, 8); |
| 173 | +} |
| 174 | + |
| 175 | +static void speck64_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in) |
| 176 | +{ |
| 177 | + const struct speck64_tfm_ctx *ctx = crypto_tfm_ctx(tfm); |
| 178 | + u32 y = get_unaligned_le32(in); |
| 179 | + u32 x = get_unaligned_le32(in + 4); |
| 180 | + int i; |
| 181 | + |
| 182 | + for (i = 0; i < ctx->nrounds; i++) |
| 183 | + speck64_round(&x, &y, ctx->round_keys[i]); |
| 184 | + |
| 185 | + put_unaligned_le32(y, out); |
| 186 | + put_unaligned_le32(x, out + 4); |
| 187 | +} |
| 188 | + |
| 189 | +static void speck64_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in) |
| 190 | +{ |
| 191 | + const struct speck64_tfm_ctx *ctx = crypto_tfm_ctx(tfm); |
| 192 | + u32 y = get_unaligned_le32(in); |
| 193 | + u32 x = get_unaligned_le32(in + 4); |
| 194 | + int i; |
| 195 | + |
| 196 | + for (i = ctx->nrounds - 1; i >= 0; i--) |
| 197 | + speck64_unround(&x, &y, ctx->round_keys[i]); |
| 198 | + |
| 199 | + put_unaligned_le32(y, out); |
| 200 | + put_unaligned_le32(x, out + 4); |
| 201 | +} |
| 202 | + |
| 203 | +static int speck64_setkey(struct crypto_tfm *tfm, const u8 *key, |
| 204 | + unsigned int keylen) |
| 205 | +{ |
| 206 | + struct speck64_tfm_ctx *ctx = crypto_tfm_ctx(tfm); |
| 207 | + u32 l[3]; |
| 208 | + u32 k; |
| 209 | + int i; |
| 210 | + |
| 211 | + switch (keylen) { |
| 212 | + case SPECK64_96_KEY_SIZE: |
| 213 | + k = get_unaligned_le32(key); |
| 214 | + l[0] = get_unaligned_le32(key + 4); |
| 215 | + l[1] = get_unaligned_le32(key + 8); |
| 216 | + ctx->nrounds = SPECK64_96_NROUNDS; |
| 217 | + for (i = 0; i < ctx->nrounds; i++) { |
| 218 | + ctx->round_keys[i] = k; |
| 219 | + speck64_round(&l[i % 2], &k, i); |
| 220 | + } |
| 221 | + break; |
| 222 | + case SPECK64_128_KEY_SIZE: |
| 223 | + k = get_unaligned_le32(key); |
| 224 | + l[0] = get_unaligned_le32(key + 4); |
| 225 | + l[1] = get_unaligned_le32(key + 8); |
| 226 | + l[2] = get_unaligned_le32(key + 12); |
| 227 | + ctx->nrounds = SPECK64_128_NROUNDS; |
| 228 | + for (i = 0; i < ctx->nrounds; i++) { |
| 229 | + ctx->round_keys[i] = k; |
| 230 | + speck64_round(&l[i % 3], &k, i); |
| 231 | + } |
| 232 | + break; |
| 233 | + default: |
| 234 | + return -EINVAL; |
| 235 | + } |
| 236 | + |
| 237 | + return 0; |
| 238 | +} |
| 239 | + |
| 240 | +/* Algorithm definitions */ |
| 241 | + |
| 242 | +static struct crypto_alg speck_algs[] = { |
| 243 | + { |
| 244 | + .cra_name = "speck128", |
| 245 | + .cra_driver_name = "speck128-generic", |
| 246 | + .cra_priority = 100, |
| 247 | + .cra_flags = CRYPTO_ALG_TYPE_CIPHER, |
| 248 | + .cra_blocksize = SPECK128_BLOCK_SIZE, |
| 249 | + .cra_ctxsize = sizeof(struct speck128_tfm_ctx), |
| 250 | + .cra_module = THIS_MODULE, |
| 251 | + .cra_u = { |
| 252 | + .cipher = { |
| 253 | + .cia_min_keysize = SPECK128_128_KEY_SIZE, |
| 254 | + .cia_max_keysize = SPECK128_256_KEY_SIZE, |
| 255 | + .cia_setkey = speck128_setkey, |
| 256 | + .cia_encrypt = speck128_encrypt, |
| 257 | + .cia_decrypt = speck128_decrypt |
| 258 | + } |
| 259 | + } |
| 260 | + }, { |
| 261 | + .cra_name = "speck64", |
| 262 | + .cra_driver_name = "speck64-generic", |
| 263 | + .cra_priority = 100, |
| 264 | + .cra_flags = CRYPTO_ALG_TYPE_CIPHER, |
| 265 | + .cra_blocksize = SPECK64_BLOCK_SIZE, |
| 266 | + .cra_ctxsize = sizeof(struct speck64_tfm_ctx), |
| 267 | + .cra_module = THIS_MODULE, |
| 268 | + .cra_u = { |
| 269 | + .cipher = { |
| 270 | + .cia_min_keysize = SPECK64_96_KEY_SIZE, |
| 271 | + .cia_max_keysize = SPECK64_128_KEY_SIZE, |
| 272 | + .cia_setkey = speck64_setkey, |
| 273 | + .cia_encrypt = speck64_encrypt, |
| 274 | + .cia_decrypt = speck64_decrypt |
| 275 | + } |
| 276 | + } |
| 277 | + } |
| 278 | +}; |
| 279 | + |
| 280 | +static int __init speck_module_init(void) |
| 281 | +{ |
| 282 | + return crypto_register_algs(speck_algs, ARRAY_SIZE(speck_algs)); |
| 283 | +} |
| 284 | + |
| 285 | +static void __exit speck_module_exit(void) |
| 286 | +{ |
| 287 | + crypto_unregister_algs(speck_algs, ARRAY_SIZE(speck_algs)); |
| 288 | +} |
| 289 | + |
| 290 | +module_init(speck_module_init); |
| 291 | +module_exit(speck_module_exit); |
| 292 | + |
| 293 | +MODULE_DESCRIPTION("Speck block cipher (generic)"); |
| 294 | +MODULE_LICENSE("GPL"); |
| 295 | +MODULE_AUTHOR( "Eric Biggers <[email protected]>"); |
| 296 | +MODULE_ALIAS_CRYPTO("speck128"); |
| 297 | +MODULE_ALIAS_CRYPTO("speck128-generic"); |
| 298 | +MODULE_ALIAS_CRYPTO("speck64"); |
| 299 | +MODULE_ALIAS_CRYPTO("speck64-generic"); |
0 commit comments