Skip to content

Commit 9547737

Browse files
herbertxdavem330
authored andcommitted
[CRYPTO] Add alignmask for low-level cipher implementations
The VIA Padlock device requires the input and output buffers to be aligned on 16-byte boundaries. This patch adds the alignmask attribute for low-level cipher implementations to indicate their alignment requirements. The mid-level crypt() function will copy the input/output buffers if they are not aligned correctly before they are passed to the low-level implementation. Strictly speaking, some of the software implementations require the buffers to be aligned on 4-byte boundaries as they do 32-bit loads. However, it is not clear whether it is better to copy the buffers or pay the penalty for unaligned loads/stores. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 4072518 commit 9547737

4 files changed

Lines changed: 49 additions & 7 deletions

File tree

‎crypto/api.c‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,12 @@ int crypto_register_alg(struct crypto_alg *alg)
168168
{
169169
int ret = 0;
170170
struct crypto_alg *q;
171+
172+
if (alg->cra_alignmask & (alg->cra_alignmask + 1))
173+
return -EINVAL;
174+
175+
if (alg->cra_alignmask > PAGE_SIZE)
176+
return -EINVAL;
171177

172178
down_write(&crypto_alg_sem);
173179

‎crypto/cipher.c‎

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,10 @@ static unsigned int crypt_slow(const struct cipher_desc *desc,
4141
struct scatter_walk *in,
4242
struct scatter_walk *out, unsigned int bsize)
4343
{
44-
u8 src[bsize];
45-
u8 dst[bsize];
44+
unsigned int alignmask = desc->tfm->__crt_alg->cra_alignmask;
45+
u8 buffer[bsize * 2 + alignmask];
46+
u8 *src = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
47+
u8 *dst = src + bsize;
4648
unsigned int n;
4749

4850
n = scatterwalk_copychunks(src, in, bsize, 0);
@@ -59,15 +61,24 @@ static unsigned int crypt_slow(const struct cipher_desc *desc,
5961
static inline unsigned int crypt_fast(const struct cipher_desc *desc,
6062
struct scatter_walk *in,
6163
struct scatter_walk *out,
62-
unsigned int nbytes)
64+
unsigned int nbytes, u8 *tmp)
6365
{
6466
u8 *src, *dst;
6567

6668
src = in->data;
6769
dst = scatterwalk_samebuf(in, out) ? src : out->data;
6870

71+
if (tmp) {
72+
memcpy(tmp, in->data, nbytes);
73+
src = tmp;
74+
dst = tmp;
75+
}
76+
6977
nbytes = desc->prfn(desc, dst, src, nbytes);
7078

79+
if (tmp)
80+
memcpy(out->data, tmp, nbytes);
81+
7182
scatterwalk_advance(in, nbytes);
7283
scatterwalk_advance(out, nbytes);
7384

@@ -87,6 +98,8 @@ static int crypt(const struct cipher_desc *desc,
8798
struct scatter_walk walk_in, walk_out;
8899
struct crypto_tfm *tfm = desc->tfm;
89100
const unsigned int bsize = crypto_tfm_alg_blocksize(tfm);
101+
unsigned int alignmask = tfm->__crt_alg->cra_alignmask;
102+
unsigned long buffer = 0;
90103

91104
if (!nbytes)
92105
return 0;
@@ -100,16 +113,27 @@ static int crypt(const struct cipher_desc *desc,
100113
scatterwalk_start(&walk_out, dst);
101114

102115
for(;;) {
103-
unsigned int n;
116+
unsigned int n = nbytes;
117+
u8 *tmp = NULL;
118+
119+
if (!scatterwalk_aligned(&walk_in, alignmask) ||
120+
!scatterwalk_aligned(&walk_out, alignmask)) {
121+
if (!buffer) {
122+
buffer = __get_free_page(GFP_ATOMIC);
123+
if (!buffer)
124+
n = 0;
125+
}
126+
tmp = (u8 *)buffer;
127+
}
104128

105129
scatterwalk_map(&walk_in, 0);
106130
scatterwalk_map(&walk_out, 1);
107131

108-
n = scatterwalk_clamp(&walk_in, nbytes);
132+
n = scatterwalk_clamp(&walk_in, n);
109133
n = scatterwalk_clamp(&walk_out, n);
110134

111135
if (likely(n >= bsize))
112-
n = crypt_fast(desc, &walk_in, &walk_out, n);
136+
n = crypt_fast(desc, &walk_in, &walk_out, n, tmp);
113137
else
114138
n = crypt_slow(desc, &walk_in, &walk_out, bsize);
115139

@@ -119,10 +143,15 @@ static int crypt(const struct cipher_desc *desc,
119143
scatterwalk_done(&walk_out, 1, nbytes);
120144

121145
if (!nbytes)
122-
return 0;
146+
break;
123147

124148
crypto_yield(tfm);
125149
}
150+
151+
if (buffer)
152+
free_page(buffer);
153+
154+
return 0;
126155
}
127156

128157
static unsigned int cbc_process_encrypt(const struct cipher_desc *desc,

‎crypto/scatterwalk.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ static inline void scatterwalk_advance(struct scatter_walk *walk,
5555
walk->len_this_segment -= nbytes;
5656
}
5757

58+
static inline unsigned int scatterwalk_aligned(struct scatter_walk *walk,
59+
unsigned int alignmask)
60+
{
61+
return !(walk->offset & alignmask);
62+
}
63+
5864
void scatterwalk_start(struct scatter_walk *walk, struct scatterlist *sg);
5965
int scatterwalk_copychunks(void *buf, struct scatter_walk *walk, size_t nbytes, int out);
6066
void scatterwalk_map(struct scatter_walk *walk, int out);

‎include/linux/crypto.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ struct crypto_alg {
124124
u32 cra_flags;
125125
unsigned int cra_blocksize;
126126
unsigned int cra_ctxsize;
127+
unsigned int cra_alignmask;
127128
const char cra_name[CRYPTO_MAX_ALG_NAME];
128129

129130
union {

0 commit comments

Comments
 (0)