Skip to content

Commit c8c3641

Browse files
ebiggersherbertx
authored andcommitted
crypto: speck - export common helpers
Export the Speck constants and transform context and the ->setkey(), ->encrypt(), and ->decrypt() functions so that they can be reused by the ARM NEON implementation of Speck-XTS. The generic key expansion code will be reused because it is not performance-critical and is not vectorizable, while the generic encryption and decryption functions are needed as fallbacks and for the XTS tweak encryption. Signed-off-by: Eric Biggers <[email protected]> Signed-off-by: Herbert Xu <[email protected]>
1 parent da7a0ab commit c8c3641

File tree

2 files changed

+111
-41
lines changed

2 files changed

+111
-41
lines changed

crypto/speck.c

Lines changed: 49 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -24,29 +24,14 @@
2424
*/
2525

2626
#include <asm/unaligned.h>
27+
#include <crypto/speck.h>
2728
#include <linux/bitops.h>
2829
#include <linux/crypto.h>
2930
#include <linux/init.h>
3031
#include <linux/module.h>
3132

3233
/* Speck128 */
3334

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-
5035
static __always_inline void speck128_round(u64 *x, u64 *y, u64 k)
5136
{
5237
*x = ror64(*x, 8);
@@ -65,9 +50,9 @@ static __always_inline void speck128_unround(u64 *x, u64 *y, u64 k)
6550
*x = rol64(*x, 8);
6651
}
6752

68-
static void speck128_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
53+
void crypto_speck128_encrypt(const struct speck128_tfm_ctx *ctx,
54+
u8 *out, const u8 *in)
6955
{
70-
const struct speck128_tfm_ctx *ctx = crypto_tfm_ctx(tfm);
7156
u64 y = get_unaligned_le64(in);
7257
u64 x = get_unaligned_le64(in + 8);
7358
int i;
@@ -78,10 +63,16 @@ static void speck128_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
7863
put_unaligned_le64(y, out);
7964
put_unaligned_le64(x, out + 8);
8065
}
66+
EXPORT_SYMBOL_GPL(crypto_speck128_encrypt);
8167

82-
static void speck128_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
68+
static void speck128_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
69+
{
70+
crypto_speck128_encrypt(crypto_tfm_ctx(tfm), out, in);
71+
}
72+
73+
void crypto_speck128_decrypt(const struct speck128_tfm_ctx *ctx,
74+
u8 *out, const u8 *in)
8375
{
84-
const struct speck128_tfm_ctx *ctx = crypto_tfm_ctx(tfm);
8576
u64 y = get_unaligned_le64(in);
8677
u64 x = get_unaligned_le64(in + 8);
8778
int i;
@@ -92,11 +83,16 @@ static void speck128_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
9283
put_unaligned_le64(y, out);
9384
put_unaligned_le64(x, out + 8);
9485
}
86+
EXPORT_SYMBOL_GPL(crypto_speck128_decrypt);
9587

96-
static int speck128_setkey(struct crypto_tfm *tfm, const u8 *key,
88+
static void speck128_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
89+
{
90+
crypto_speck128_decrypt(crypto_tfm_ctx(tfm), out, in);
91+
}
92+
93+
int crypto_speck128_setkey(struct speck128_tfm_ctx *ctx, const u8 *key,
9794
unsigned int keylen)
9895
{
99-
struct speck128_tfm_ctx *ctx = crypto_tfm_ctx(tfm);
10096
u64 l[3];
10197
u64 k;
10298
int i;
@@ -138,21 +134,15 @@ static int speck128_setkey(struct crypto_tfm *tfm, const u8 *key,
138134

139135
return 0;
140136
}
137+
EXPORT_SYMBOL_GPL(crypto_speck128_setkey);
141138

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
139+
static int speck128_setkey(struct crypto_tfm *tfm, const u8 *key,
140+
unsigned int keylen)
141+
{
142+
return crypto_speck128_setkey(crypto_tfm_ctx(tfm), key, keylen);
143+
}
151144

152-
struct speck64_tfm_ctx {
153-
u32 round_keys[SPECK64_128_NROUNDS];
154-
int nrounds;
155-
};
145+
/* Speck64 */
156146

157147
static __always_inline void speck64_round(u32 *x, u32 *y, u32 k)
158148
{
@@ -172,9 +162,9 @@ static __always_inline void speck64_unround(u32 *x, u32 *y, u32 k)
172162
*x = rol32(*x, 8);
173163
}
174164

175-
static void speck64_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
165+
void crypto_speck64_encrypt(const struct speck64_tfm_ctx *ctx,
166+
u8 *out, const u8 *in)
176167
{
177-
const struct speck64_tfm_ctx *ctx = crypto_tfm_ctx(tfm);
178168
u32 y = get_unaligned_le32(in);
179169
u32 x = get_unaligned_le32(in + 4);
180170
int i;
@@ -185,10 +175,16 @@ static void speck64_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
185175
put_unaligned_le32(y, out);
186176
put_unaligned_le32(x, out + 4);
187177
}
178+
EXPORT_SYMBOL_GPL(crypto_speck64_encrypt);
188179

189-
static void speck64_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
180+
static void speck64_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
181+
{
182+
crypto_speck64_encrypt(crypto_tfm_ctx(tfm), out, in);
183+
}
184+
185+
void crypto_speck64_decrypt(const struct speck64_tfm_ctx *ctx,
186+
u8 *out, const u8 *in)
190187
{
191-
const struct speck64_tfm_ctx *ctx = crypto_tfm_ctx(tfm);
192188
u32 y = get_unaligned_le32(in);
193189
u32 x = get_unaligned_le32(in + 4);
194190
int i;
@@ -199,11 +195,16 @@ static void speck64_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
199195
put_unaligned_le32(y, out);
200196
put_unaligned_le32(x, out + 4);
201197
}
198+
EXPORT_SYMBOL_GPL(crypto_speck64_decrypt);
202199

203-
static int speck64_setkey(struct crypto_tfm *tfm, const u8 *key,
200+
static void speck64_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
201+
{
202+
crypto_speck64_decrypt(crypto_tfm_ctx(tfm), out, in);
203+
}
204+
205+
int crypto_speck64_setkey(struct speck64_tfm_ctx *ctx, const u8 *key,
204206
unsigned int keylen)
205207
{
206-
struct speck64_tfm_ctx *ctx = crypto_tfm_ctx(tfm);
207208
u32 l[3];
208209
u32 k;
209210
int i;
@@ -236,6 +237,13 @@ static int speck64_setkey(struct crypto_tfm *tfm, const u8 *key,
236237

237238
return 0;
238239
}
240+
EXPORT_SYMBOL_GPL(crypto_speck64_setkey);
241+
242+
static int speck64_setkey(struct crypto_tfm *tfm, const u8 *key,
243+
unsigned int keylen)
244+
{
245+
return crypto_speck64_setkey(crypto_tfm_ctx(tfm), key, keylen);
246+
}
239247

240248
/* Algorithm definitions */
241249

include/crypto/speck.h

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/*
3+
* Common values for the Speck algorithm
4+
*/
5+
6+
#ifndef _CRYPTO_SPECK_H
7+
#define _CRYPTO_SPECK_H
8+
9+
#include <linux/types.h>
10+
11+
/* Speck128 */
12+
13+
#define SPECK128_BLOCK_SIZE 16
14+
15+
#define SPECK128_128_KEY_SIZE 16
16+
#define SPECK128_128_NROUNDS 32
17+
18+
#define SPECK128_192_KEY_SIZE 24
19+
#define SPECK128_192_NROUNDS 33
20+
21+
#define SPECK128_256_KEY_SIZE 32
22+
#define SPECK128_256_NROUNDS 34
23+
24+
struct speck128_tfm_ctx {
25+
u64 round_keys[SPECK128_256_NROUNDS];
26+
int nrounds;
27+
};
28+
29+
void crypto_speck128_encrypt(const struct speck128_tfm_ctx *ctx,
30+
u8 *out, const u8 *in);
31+
32+
void crypto_speck128_decrypt(const struct speck128_tfm_ctx *ctx,
33+
u8 *out, const u8 *in);
34+
35+
int crypto_speck128_setkey(struct speck128_tfm_ctx *ctx, const u8 *key,
36+
unsigned int keysize);
37+
38+
/* Speck64 */
39+
40+
#define SPECK64_BLOCK_SIZE 8
41+
42+
#define SPECK64_96_KEY_SIZE 12
43+
#define SPECK64_96_NROUNDS 26
44+
45+
#define SPECK64_128_KEY_SIZE 16
46+
#define SPECK64_128_NROUNDS 27
47+
48+
struct speck64_tfm_ctx {
49+
u32 round_keys[SPECK64_128_NROUNDS];
50+
int nrounds;
51+
};
52+
53+
void crypto_speck64_encrypt(const struct speck64_tfm_ctx *ctx,
54+
u8 *out, const u8 *in);
55+
56+
void crypto_speck64_decrypt(const struct speck64_tfm_ctx *ctx,
57+
u8 *out, const u8 *in);
58+
59+
int crypto_speck64_setkey(struct speck64_tfm_ctx *ctx, const u8 *key,
60+
unsigned int keysize);
61+
62+
#endif /* _CRYPTO_SPECK_H */

0 commit comments

Comments
 (0)