History

crypto: camellia-x86_64 - remove duplicated glue code and use shared glue code from glue_helper

Now that shared glue code is available, convert camellia-x86_64 to use it.

Jussi Kivilinna committed 14 years ago

Commit 964263a1 parent 1d0debb2 files · +87 −269Browse files

Unified · Split

2 files
  1. arch/x86/crypto/camellia_glue.c +86 −269
  2. crypto/Kconfig +1 −0
arch/x86/crypto/camellia_glue.c+86−269
@@ -5,10 +5,6 @@ · 4 unmodified lines
55 *
66 * Camellia parts based on code by:
77 * Copyright (C) 2006 NTT (Nippon Telegraph and Telephone Corporation)
8− * CBC & ECB parts based on code (crypto/cbc.c,ecb.c) by:
9− * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
10− * CTR part based on code (crypto/ctr.c) by:
11− * (C) Copyright IBM Corp. 2007 - Joy Latten <latten@us.ibm.com>
128 *
139 * This program is free software; you can redistribute it and/or modify
1410 * it under the terms of the GNU General Public License as published by
@@ -34,9 +30,9 @@ · 19 unmodified lines
3430 #include <linux/module.h>
3531 #include <linux/types.h>
3632 #include <crypto/algapi.h>
37−#include <crypto/b128ops.h>
3833 #include <crypto/lrw.h>
3934 #include <crypto/xts.h>
35+#include <asm/crypto/glue_helper.h>
4036
4137 #define CAMELLIA_MIN_KEY_SIZE 16
4238 #define CAMELLIA_MAX_KEY_SIZE 32
@@ -1312,307 +1308,128 @@ static int camellia_setkey(struct crypto_tfm *tfm, const u8 *in_key, · 1269 unmodified lines
13121308 &tfm->crt_flags);
13131309 }
13141310
1315−static int ecb_crypt(struct blkcipher_desc *desc, struct blkcipher_walk *walk,
1316− void (*fn)(struct camellia_ctx *, u8 *, const u8 *),
1317− void (*fn_2way)(struct camellia_ctx *, u8 *, const u8 *))
1311+static void camellia_decrypt_cbc_2way(void *ctx, u128 *dst, const u128 *src)
13181312 {
1319− struct camellia_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
1320− unsigned int bsize = CAMELLIA_BLOCK_SIZE;
1321− unsigned int nbytes;
1322− int err;
1323−
1324− err = blkcipher_walk_virt(desc, walk);
1325−
1326− while ((nbytes = walk->nbytes)) {
1327− u8 *wsrc = walk->src.virt.addr;
1328− u8 *wdst = walk->dst.virt.addr;
1329−
1330− /* Process two block batch */
1331− if (nbytes >= bsize * 2) {
1332− do {
1333− fn_2way(ctx, wdst, wsrc);
1334−
1335− wsrc += bsize * 2;
1336− wdst += bsize * 2;
1337− nbytes -= bsize * 2;
1338− } while (nbytes >= bsize * 2);
1339−
1340− if (nbytes < bsize)
1341− goto done;
1342− }
1343−
1344− /* Handle leftovers */
1345− do {
1346− fn(ctx, wdst, wsrc);
1347−
1348− wsrc += bsize;
1349− wdst += bsize;
1350− nbytes -= bsize;
1351− } while (nbytes >= bsize);
1352−
1353−done:
1354− err = blkcipher_walk_done(desc, walk, nbytes);
1355− }
1356−
1357− return err;
1358−}
1359−
1360−static int ecb_encrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
1361− struct scatterlist *src, unsigned int nbytes)
1362−{
1363− struct blkcipher_walk walk;
1364−
1365− blkcipher_walk_init(&walk, dst, src, nbytes);
1366− return ecb_crypt(desc, &walk, camellia_enc_blk, camellia_enc_blk_2way);
1367−}
1313+ u128 iv = *src;
13681314
1369−static int ecb_decrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
1370− struct scatterlist *src, unsigned int nbytes)
1371−{
1372− struct blkcipher_walk walk;
1373−
1374− blkcipher_walk_init(&walk, dst, src, nbytes);
1375− return ecb_crypt(desc, &walk, camellia_dec_blk, camellia_dec_blk_2way);
1376−}
1315+ camellia_dec_blk_2way(ctx, (u8 *)dst, (u8 *)src);
13771316
1378−static unsigned int __cbc_encrypt(struct blkcipher_desc *desc,
1379− struct blkcipher_walk *walk)
1380−{
1381− struct camellia_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
1382− unsigned int bsize = CAMELLIA_BLOCK_SIZE;
1383− unsigned int nbytes = walk->nbytes;
1384− u128 *src = (u128 *)walk->src.virt.addr;
1385− u128 *dst = (u128 *)walk->dst.virt.addr;
1386− u128 *iv = (u128 *)walk->iv;
1387−
1388− do {
1389− u128_xor(dst, src, iv);
1390− camellia_enc_blk(ctx, (u8 *)dst, (u8 *)dst);
1391− iv = dst;
1392−
1393− src += 1;
1394− dst += 1;
1395− nbytes -= bsize;
1396− } while (nbytes >= bsize);
1397−
1398− u128_xor((u128 *)walk->iv, (u128 *)walk->iv, iv);
1399− return nbytes;
1317+ u128_xor(&dst[1], &dst[1], &iv);
14001318 }
14011319
1402−static int cbc_encrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
1403− struct scatterlist *src, unsigned int nbytes)
1320+static void camellia_crypt_ctr(void *ctx, u128 *dst, const u128 *src, u128 *iv)
14041321 {
1405− struct blkcipher_walk walk;
1406− int err;
1322+ be128 ctrblk;
14071323
1408− blkcipher_walk_init(&walk, dst, src, nbytes);
1409− err = blkcipher_walk_virt(desc, &walk);
1324+ if (dst != src)
1325+ *dst = *src;
14101326
1411− while ((nbytes = walk.nbytes)) {
1412− nbytes = __cbc_encrypt(desc, &walk);
1413− err = blkcipher_walk_done(desc, &walk, nbytes);
1414− }
1327+ u128_to_be128(&ctrblk, iv);
1328+ u128_inc(iv);
14151329
1416− return err;
1330+ camellia_enc_blk_xor(ctx, (u8 *)dst, (u8 *)&ctrblk);
14171331 }
14181332
1419−static unsigned int __cbc_decrypt(struct blkcipher_desc *desc,
1420− struct blkcipher_walk *walk)
1333+static void camellia_crypt_ctr_2way(void *ctx, u128 *dst, const u128 *src,
1334+ u128 *iv)
14211335 {
1422− struct camellia_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
1423− unsigned int bsize = CAMELLIA_BLOCK_SIZE;
1424− unsigned int nbytes = walk->nbytes;
1425− u128 *src = (u128 *)walk->src.virt.addr;
1426− u128 *dst = (u128 *)walk->dst.virt.addr;
1427− u128 ivs[2 - 1];
1428− u128 last_iv;
1336+ be128 ctrblks[2];
14291337
1430− /* Start of the last block. */
1431− src += nbytes / bsize - 1;
1432− dst += nbytes / bsize - 1;
1433−
1434− last_iv = *src;
1435−
1436− /* Process two block batch */
1437− if (nbytes >= bsize * 2) {
1438− do {
1439− nbytes -= bsize * (2 - 1);
1440− src -= 2 - 1;
1441− dst -= 2 - 1;
1442−
1443− ivs[0] = src[0];
1444−
1445− camellia_dec_blk_2way(ctx, (u8 *)dst, (u8 *)src);
1446−
1447− u128_xor(dst + 1, dst + 1, ivs + 0);
1448−
1449− nbytes -= bsize;
1450− if (nbytes < bsize)
1451− goto done;
1452−
1453− u128_xor(dst, dst, src - 1);
1454− src -= 1;
1455− dst -= 1;
1456− } while (nbytes >= bsize * 2);
1457−
1458− if (nbytes < bsize)
1459− goto done;
1338+ if (dst != src) {
1339+ dst[0] = src[0];
1340+ dst[1] = src[1];
14601341 }
14611342
1462− /* Handle leftovers */
1463− for (;;) {
1464− camellia_dec_blk(ctx, (u8 *)dst, (u8 *)src);
1465−
1466− nbytes -= bsize;
1467− if (nbytes < bsize)
1468− break;
1343+ u128_to_be128(&ctrblks[0], iv);
1344+ u128_inc(iv);
1345+ u128_to_be128(&ctrblks[1], iv);
1346+ u128_inc(iv);
14691347
1470− u128_xor(dst, dst, src - 1);
1471− src -= 1;
1472− dst -= 1;
1473− }
1474−
1475−done:
1476− u128_xor(dst, dst, (u128 *)walk->iv);
1477− *(u128 *)walk->iv = last_iv;
1478−
1479− return nbytes;
1348+ camellia_enc_blk_xor_2way(ctx, (u8 *)dst, (u8 *)ctrblks);
14801349 }
14811350
1482−static int cbc_decrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
1483− struct scatterlist *src, unsigned int nbytes)
1484−{
1485− struct blkcipher_walk walk;
1486− int err;
1487−
1488− blkcipher_walk_init(&walk, dst, src, nbytes);
1489− err = blkcipher_walk_virt(desc, &walk);
1351+static const struct common_glue_ctx camellia_enc = {
1352+ .num_funcs = 2,
1353+ .fpu_blocks_limit = -1,
1354+
1355+ .funcs = { {
1356+ .num_blocks = 2,
1357+ .fn_u = { .ecb = GLUE_FUNC_CAST(camellia_enc_blk_2way) }
1358+ }, {
1359+ .num_blocks = 1,
1360+ .fn_u = { .ecb = GLUE_FUNC_CAST(camellia_enc_blk) }
1361+ } }
1362+};
14901363
1491− while ((nbytes = walk.nbytes)) {
1492− nbytes = __cbc_decrypt(desc, &walk);
1493− err = blkcipher_walk_done(desc, &walk, nbytes);
1494− }
1364+static const struct common_glue_ctx camellia_ctr = {
1365+ .num_funcs = 2,
1366+ .fpu_blocks_limit = -1,
1367+
1368+ .funcs = { {
1369+ .num_blocks = 2,
1370+ .fn_u = { .ctr = GLUE_CTR_FUNC_CAST(camellia_crypt_ctr_2way) }
1371+ }, {
1372+ .num_blocks = 1,
1373+ .fn_u = { .ctr = GLUE_CTR_FUNC_CAST(camellia_crypt_ctr) }
1374+ } }
1375+};
14951376
1496− return err;
1497−}
1377+static const struct common_glue_ctx camellia_dec = {
1378+ .num_funcs = 2,
1379+ .fpu_blocks_limit = -1,
1380+
1381+ .funcs = { {
1382+ .num_blocks = 2,
1383+ .fn_u = { .ecb = GLUE_FUNC_CAST(camellia_dec_blk_2way) }
1384+ }, {
1385+ .num_blocks = 1,
1386+ .fn_u = { .ecb = GLUE_FUNC_CAST(camellia_dec_blk) }
1387+ } }
1388+};
14981389
1499−static inline void u128_to_be128(be128 *dst, const u128 *src)
1500−{
1501− dst->a = cpu_to_be64(src->a);
1502− dst->b = cpu_to_be64(src->b);
1503−}
1390+static const struct common_glue_ctx camellia_dec_cbc = {
1391+ .num_funcs = 2,
1392+ .fpu_blocks_limit = -1,
1393+
1394+ .funcs = { {
1395+ .num_blocks = 2,
1396+ .fn_u = { .cbc = GLUE_CBC_FUNC_CAST(camellia_decrypt_cbc_2way) }
1397+ }, {
1398+ .num_blocks = 1,
1399+ .fn_u = { .cbc = GLUE_CBC_FUNC_CAST(camellia_dec_blk) }
1400+ } }
1401+};
15041402
1505−static inline void be128_to_u128(u128 *dst, const be128 *src)
1403+static int ecb_encrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
1404+ struct scatterlist *src, unsigned int nbytes)
15061405 {
1507− dst->a = be64_to_cpu(src->a);
1508− dst->b = be64_to_cpu(src->b);
1406+ return glue_ecb_crypt_128bit(&camellia_enc, desc, dst, src, nbytes);
15091407 }
15101408
1511−static inline void u128_inc(u128 *i)
1409+static int ecb_decrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
1410+ struct scatterlist *src, unsigned int nbytes)
15121411 {
1513− i->b++;
1514− if (!i->b)
1515− i->a++;
1412+ return glue_ecb_crypt_128bit(&camellia_dec, desc, dst, src, nbytes);
15161413 }
15171414
1518−static void ctr_crypt_final(struct blkcipher_desc *desc,
1519− struct blkcipher_walk *walk)
1415+static int cbc_encrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
1416+ struct scatterlist *src, unsigned int nbytes)
15201417 {
1521− struct camellia_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
1522− u8 keystream[CAMELLIA_BLOCK_SIZE];
1523− u8 *src = walk->src.virt.addr;
1524− u8 *dst = walk->dst.virt.addr;
1525− unsigned int nbytes = walk->nbytes;
1526− u128 ctrblk;
1527−
1528− memcpy(keystream, src, nbytes);
1529− camellia_enc_blk_xor(ctx, keystream, walk->iv);
1530− memcpy(dst, keystream, nbytes);
1531−
1532− be128_to_u128(&ctrblk, (be128 *)walk->iv);
1533− u128_inc(&ctrblk);
1534− u128_to_be128((be128 *)walk->iv, &ctrblk);
1418+ return glue_cbc_encrypt_128bit(GLUE_FUNC_CAST(camellia_enc_blk), desc,
1419+ dst, src, nbytes);
15351420 }
15361421
1537−static unsigned int __ctr_crypt(struct blkcipher_desc *desc,
1538− struct blkcipher_walk *walk)
1422+static int cbc_decrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
1423+ struct scatterlist *src, unsigned int nbytes)
15391424 {
1540− struct camellia_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
1541− unsigned int bsize = CAMELLIA_BLOCK_SIZE;
1542− unsigned int nbytes = walk->nbytes;
1543− u128 *src = (u128 *)walk->src.virt.addr;
1544− u128 *dst = (u128 *)walk->dst.virt.addr;
1545− u128 ctrblk;
1546− be128 ctrblocks[2];
1547−
1548− be128_to_u128(&ctrblk, (be128 *)walk->iv);
1549−
1550− /* Process two block batch */
1551− if (nbytes >= bsize * 2) {
1552− do {
1553− if (dst != src) {
1554− dst[0] = src[0];
1555− dst[1] = src[1];
1556− }
1557−
1558− /* create ctrblks for parallel encrypt */
1559− u128_to_be128(&ctrblocks[0], &ctrblk);
1560− u128_inc(&ctrblk);
1561− u128_to_be128(&ctrblocks[1], &ctrblk);
1562− u128_inc(&ctrblk);
1563−
1564− camellia_enc_blk_xor_2way(ctx, (u8 *)dst,
1565− (u8 *)ctrblocks);
1566−
1567− src += 2;
1568− dst += 2;
1569− nbytes -= bsize * 2;
1570− } while (nbytes >= bsize * 2);
1571−
1572− if (nbytes < bsize)
1573− goto done;
1574− }
1575−
1576− /* Handle leftovers */
1577− do {
1578− if (dst != src)
1579− *dst = *src;
1580−
1581− u128_to_be128(&ctrblocks[0], &ctrblk);
1582− u128_inc(&ctrblk);
1583−
1584− camellia_enc_blk_xor(ctx, (u8 *)dst, (u8 *)ctrblocks);
1585−
1586− src += 1;
1587− dst += 1;
1588− nbytes -= bsize;
1589− } while (nbytes >= bsize);
1590−
1591−done:
1592− u128_to_be128((be128 *)walk->iv, &ctrblk);
1593− return nbytes;
1425+ return glue_cbc_decrypt_128bit(&camellia_dec_cbc, desc, dst, src,
1426+ nbytes);
15941427 }
15951428
15961429 static int ctr_crypt(struct blkcipher_desc *desc, struct scatterlist *dst,
15971430 struct scatterlist *src, unsigned int nbytes)
15981431 {
1599− struct blkcipher_walk walk;
1600− int err;
1601−
1602− blkcipher_walk_init(&walk, dst, src, nbytes);
1603− err = blkcipher_walk_virt_block(desc, &walk, CAMELLIA_BLOCK_SIZE);
1604−
1605− while ((nbytes = walk.nbytes) >= CAMELLIA_BLOCK_SIZE) {
1606− nbytes = __ctr_crypt(desc, &walk);
1607− err = blkcipher_walk_done(desc, &walk, nbytes);
1608− }
1609−
1610− if (walk.nbytes) {
1611− ctr_crypt_final(desc, &walk);
1612− err = blkcipher_walk_done(desc, &walk, 0);
1613− }
1614−
1615− return err;
1432+ return glue_ctr_crypt_128bit(&camellia_ctr, desc, dst, src, nbytes);
16161433 }
16171434
16181435 static void encrypt_callback(void *priv, u8 *srcdst, unsigned int nbytes)
crypto/Kconfig+1−0
@@ -671,6 +671,7 @@ config CRYPTO_CAMELLIA_X86_64 · 670 unmodified lines
671671 depends on X86 && 64BIT
672672 depends on CRYPTO
673673 select CRYPTO_ALGAPI
674+ select CRYPTO_GLUE_HELPER_X86
674675 select CRYPTO_LRW
675676 select CRYPTO_XTS
676677 help