Skip to content

Commit d072bfa

Browse files
chunkeeyherbertx
authored andcommitted
crypto: crypto4xx - add prng crypto support
This patch adds support for crypto4xx's ANSI X9.17 Annex C compliant pseudo random number generator which provides a pseudo random source for the purpose of generating Initialization Vectors (IV's) for AES algorithms to the Packet Engine and other pseudo random number requirements. Signed-off-by: Christian Lamparter <chunkeey@gmail.com> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent 12b8567 commit d072bfa

3 files changed

Lines changed: 92 additions & 0 deletions

File tree

‎drivers/crypto/amcc/crypto4xx_core.c‎

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,11 @@
4040
#include <crypto/ctr.h>
4141
#include <crypto/gcm.h>
4242
#include <crypto/sha.h>
43+
#include <crypto/rng.h>
4344
#include <crypto/scatterwalk.h>
4445
#include <crypto/skcipher.h>
4546
#include <crypto/internal/aead.h>
47+
#include <crypto/internal/rng.h>
4648
#include <crypto/internal/skcipher.h>
4749
#include "crypto4xx_reg_def.h"
4850
#include "crypto4xx_core.h"
@@ -1035,6 +1037,10 @@ static int crypto4xx_register_alg(struct crypto4xx_device *sec_dev,
10351037
rc = crypto_register_ahash(&alg->alg.u.hash);
10361038
break;
10371039

1040+
case CRYPTO_ALG_TYPE_RNG:
1041+
rc = crypto_register_rng(&alg->alg.u.rng);
1042+
break;
1043+
10381044
default:
10391045
rc = crypto_register_skcipher(&alg->alg.u.cipher);
10401046
break;
@@ -1064,6 +1070,10 @@ static void crypto4xx_unregister_alg(struct crypto4xx_device *sec_dev)
10641070
crypto_unregister_aead(&alg->alg.u.aead);
10651071
break;
10661072

1073+
case CRYPTO_ALG_TYPE_RNG:
1074+
crypto_unregister_rng(&alg->alg.u.rng);
1075+
break;
1076+
10671077
default:
10681078
crypto_unregister_skcipher(&alg->alg.u.cipher);
10691079
}
@@ -1122,6 +1132,69 @@ static irqreturn_t crypto4xx_ce_interrupt_handler_revb(int irq, void *data)
11221132
PPC4XX_TMO_ERR_INT);
11231133
}
11241134

1135+
static int ppc4xx_prng_data_read(struct crypto4xx_device *dev,
1136+
u8 *data, unsigned int max)
1137+
{
1138+
unsigned int i, curr = 0;
1139+
u32 val[2];
1140+
1141+
do {
1142+
/* trigger PRN generation */
1143+
writel(PPC4XX_PRNG_CTRL_AUTO_EN,
1144+
dev->ce_base + CRYPTO4XX_PRNG_CTRL);
1145+
1146+
for (i = 0; i < 1024; i++) {
1147+
/* usually 19 iterations are enough */
1148+
if ((readl(dev->ce_base + CRYPTO4XX_PRNG_STAT) &
1149+
CRYPTO4XX_PRNG_STAT_BUSY))
1150+
continue;
1151+
1152+
val[0] = readl_be(dev->ce_base + CRYPTO4XX_PRNG_RES_0);
1153+
val[1] = readl_be(dev->ce_base + CRYPTO4XX_PRNG_RES_1);
1154+
break;
1155+
}
1156+
if (i == 1024)
1157+
return -ETIMEDOUT;
1158+
1159+
if ((max - curr) >= 8) {
1160+
memcpy(data, &val, 8);
1161+
data += 8;
1162+
curr += 8;
1163+
} else {
1164+
/* copy only remaining bytes */
1165+
memcpy(data, &val, max - curr);
1166+
break;
1167+
}
1168+
} while (curr < max);
1169+
1170+
return curr;
1171+
}
1172+
1173+
static int crypto4xx_prng_generate(struct crypto_rng *tfm,
1174+
const u8 *src, unsigned int slen,
1175+
u8 *dstn, unsigned int dlen)
1176+
{
1177+
struct rng_alg *alg = crypto_rng_alg(tfm);
1178+
struct crypto4xx_alg *amcc_alg;
1179+
struct crypto4xx_device *dev;
1180+
int ret;
1181+
1182+
amcc_alg = container_of(alg, struct crypto4xx_alg, alg.u.rng);
1183+
dev = amcc_alg->dev;
1184+
1185+
mutex_lock(&dev->core_dev->rng_lock);
1186+
ret = ppc4xx_prng_data_read(dev, dstn, dlen);
1187+
mutex_unlock(&dev->core_dev->rng_lock);
1188+
return ret;
1189+
}
1190+
1191+
1192+
static int crypto4xx_prng_seed(struct crypto_rng *tfm, const u8 *seed,
1193+
unsigned int slen)
1194+
{
1195+
return 0;
1196+
}
1197+
11251198
/**
11261199
* Supported Crypto Algorithms
11271200
*/
@@ -1291,6 +1364,18 @@ static struct crypto4xx_alg_common crypto4xx_alg[] = {
12911364
.cra_module = THIS_MODULE,
12921365
},
12931366
} },
1367+
{ .type = CRYPTO_ALG_TYPE_RNG, .u.rng = {
1368+
.base = {
1369+
.cra_name = "stdrng",
1370+
.cra_driver_name = "crypto4xx_rng",
1371+
.cra_priority = 300,
1372+
.cra_ctxsize = 0,
1373+
.cra_module = THIS_MODULE,
1374+
},
1375+
.generate = crypto4xx_prng_generate,
1376+
.seed = crypto4xx_prng_seed,
1377+
.seedsize = 0,
1378+
} },
12941379
};
12951380

12961381
/**
@@ -1360,6 +1445,7 @@ static int crypto4xx_probe(struct platform_device *ofdev)
13601445
core_dev->dev->core_dev = core_dev;
13611446
core_dev->dev->is_revb = is_revb;
13621447
core_dev->device = dev;
1448+
mutex_init(&core_dev->rng_lock);
13631449
spin_lock_init(&core_dev->lock);
13641450
INIT_LIST_HEAD(&core_dev->dev->alg_list);
13651451
ratelimit_default_init(&core_dev->dev->aead_ratelimit);
@@ -1439,6 +1525,7 @@ static int crypto4xx_remove(struct platform_device *ofdev)
14391525
tasklet_kill(&core_dev->tasklet);
14401526
/* Un-register with Linux CryptoAPI */
14411527
crypto4xx_unregister_alg(core_dev->dev);
1528+
mutex_destroy(&core_dev->rng_lock);
14421529
/* Free all allocated memory */
14431530
crypto4xx_stop_all(core_dev);
14441531

‎drivers/crypto/amcc/crypto4xx_core.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@
2323
#define __CRYPTO4XX_CORE_H__
2424

2525
#include <linux/ratelimit.h>
26+
#include <linux/mutex.h>
2627
#include <crypto/internal/hash.h>
2728
#include <crypto/internal/aead.h>
29+
#include <crypto/internal/rng.h>
2830
#include <crypto/internal/skcipher.h>
2931
#include "crypto4xx_reg_def.h"
3032
#include "crypto4xx_sa.h"
@@ -119,6 +121,7 @@ struct crypto4xx_core_device {
119121
u32 irq;
120122
struct tasklet_struct tasklet;
121123
spinlock_t lock;
124+
struct mutex rng_lock;
122125
};
123126

124127
struct crypto4xx_ctx {
@@ -143,6 +146,7 @@ struct crypto4xx_alg_common {
143146
struct skcipher_alg cipher;
144147
struct ahash_alg hash;
145148
struct aead_alg aead;
149+
struct rng_alg rng;
146150
} u;
147151
};
148152

‎drivers/crypto/amcc/crypto4xx_reg_def.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@
100100
#define CRYPTO4XX_ENDIAN_CFG 0x000600d8
101101

102102
#define CRYPTO4XX_PRNG_STAT 0x00070000
103+
#define CRYPTO4XX_PRNG_STAT_BUSY 0x1
103104
#define CRYPTO4XX_PRNG_CTRL 0x00070004
104105
#define CRYPTO4XX_PRNG_SEED_L 0x00070008
105106
#define CRYPTO4XX_PRNG_SEED_H 0x0007000c

0 commit comments

Comments
 (0)