|
40 | 40 | #include <crypto/ctr.h> |
41 | 41 | #include <crypto/gcm.h> |
42 | 42 | #include <crypto/sha.h> |
| 43 | +#include <crypto/rng.h> |
43 | 44 | #include <crypto/scatterwalk.h> |
44 | 45 | #include <crypto/skcipher.h> |
45 | 46 | #include <crypto/internal/aead.h> |
| 47 | +#include <crypto/internal/rng.h> |
46 | 48 | #include <crypto/internal/skcipher.h> |
47 | 49 | #include "crypto4xx_reg_def.h" |
48 | 50 | #include "crypto4xx_core.h" |
@@ -1035,6 +1037,10 @@ static int crypto4xx_register_alg(struct crypto4xx_device *sec_dev, |
1035 | 1037 | rc = crypto_register_ahash(&alg->alg.u.hash); |
1036 | 1038 | break; |
1037 | 1039 |
|
| 1040 | + case CRYPTO_ALG_TYPE_RNG: |
| 1041 | + rc = crypto_register_rng(&alg->alg.u.rng); |
| 1042 | + break; |
| 1043 | + |
1038 | 1044 | default: |
1039 | 1045 | rc = crypto_register_skcipher(&alg->alg.u.cipher); |
1040 | 1046 | break; |
@@ -1064,6 +1070,10 @@ static void crypto4xx_unregister_alg(struct crypto4xx_device *sec_dev) |
1064 | 1070 | crypto_unregister_aead(&alg->alg.u.aead); |
1065 | 1071 | break; |
1066 | 1072 |
|
| 1073 | + case CRYPTO_ALG_TYPE_RNG: |
| 1074 | + crypto_unregister_rng(&alg->alg.u.rng); |
| 1075 | + break; |
| 1076 | + |
1067 | 1077 | default: |
1068 | 1078 | crypto_unregister_skcipher(&alg->alg.u.cipher); |
1069 | 1079 | } |
@@ -1122,6 +1132,69 @@ static irqreturn_t crypto4xx_ce_interrupt_handler_revb(int irq, void *data) |
1122 | 1132 | PPC4XX_TMO_ERR_INT); |
1123 | 1133 | } |
1124 | 1134 |
|
| 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 | + |
1125 | 1198 | /** |
1126 | 1199 | * Supported Crypto Algorithms |
1127 | 1200 | */ |
@@ -1291,6 +1364,18 @@ static struct crypto4xx_alg_common crypto4xx_alg[] = { |
1291 | 1364 | .cra_module = THIS_MODULE, |
1292 | 1365 | }, |
1293 | 1366 | } }, |
| 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 | + } }, |
1294 | 1379 | }; |
1295 | 1380 |
|
1296 | 1381 | /** |
@@ -1360,6 +1445,7 @@ static int crypto4xx_probe(struct platform_device *ofdev) |
1360 | 1445 | core_dev->dev->core_dev = core_dev; |
1361 | 1446 | core_dev->dev->is_revb = is_revb; |
1362 | 1447 | core_dev->device = dev; |
| 1448 | + mutex_init(&core_dev->rng_lock); |
1363 | 1449 | spin_lock_init(&core_dev->lock); |
1364 | 1450 | INIT_LIST_HEAD(&core_dev->dev->alg_list); |
1365 | 1451 | ratelimit_default_init(&core_dev->dev->aead_ratelimit); |
@@ -1439,6 +1525,7 @@ static int crypto4xx_remove(struct platform_device *ofdev) |
1439 | 1525 | tasklet_kill(&core_dev->tasklet); |
1440 | 1526 | /* Un-register with Linux CryptoAPI */ |
1441 | 1527 | crypto4xx_unregister_alg(core_dev->dev); |
| 1528 | + mutex_destroy(&core_dev->rng_lock); |
1442 | 1529 | /* Free all allocated memory */ |
1443 | 1530 | crypto4xx_stop_all(core_dev); |
1444 | 1531 |
|
|
0 commit comments