diff options
author | David S. Miller <davem@davemloft.net> | 2010-05-22 05:45:56 -0400 |
---|---|---|
committer | David S. Miller <davem@davemloft.net> | 2010-05-25 20:37:17 -0400 |
commit | 3a2c034697558602a72e51897c6d3665bc515927 (patch) | |
tree | 709351d059760824a705c82a72e1f2d95a46bc40 /drivers | |
parent | 38511108a37e5551b2bfe143a957132d46f9e6e7 (diff) |
n2_crypto: Make ahash parameterization explicit.
All of the ahash ->digest() ops do essentially the same thing, just
using different parameters.
So instead, have a single n2_hash_async_digest() and use an
n2_ahash_alg container that provides the parameters.
Signed-off-by: David S. Miller <davem@davemloft.net>
Diffstat (limited to 'drivers')
-rw-r--r-- | drivers/crypto/n2_core.c | 216 |
1 files changed, 99 insertions, 117 deletions
diff --git a/drivers/crypto/n2_core.c b/drivers/crypto/n2_core.c index 77939e40e26f..d01a2afda6e9 100644 --- a/drivers/crypto/n2_core.c +++ b/drivers/crypto/n2_core.c | |||
@@ -239,6 +239,26 @@ static inline bool n2_should_run_async(struct spu_queue *qp, int this_len) | |||
239 | } | 239 | } |
240 | #endif | 240 | #endif |
241 | 241 | ||
242 | struct n2_ahash_alg { | ||
243 | struct list_head entry; | ||
244 | const char *hash_zero; | ||
245 | const u32 *hash_init; | ||
246 | u8 hw_op_hashsz; | ||
247 | u8 digest_size; | ||
248 | u8 auth_type; | ||
249 | struct ahash_alg alg; | ||
250 | }; | ||
251 | |||
252 | static inline struct n2_ahash_alg *n2_ahash_alg(struct crypto_tfm *tfm) | ||
253 | { | ||
254 | struct crypto_alg *alg = tfm->__crt_alg; | ||
255 | struct ahash_alg *ahash_alg; | ||
256 | |||
257 | ahash_alg = container_of(alg, struct ahash_alg, halg.base); | ||
258 | |||
259 | return container_of(ahash_alg, struct n2_ahash_alg, alg); | ||
260 | } | ||
261 | |||
242 | struct n2_hash_ctx { | 262 | struct n2_hash_ctx { |
243 | struct crypto_ahash *fallback_tfm; | 263 | struct crypto_ahash *fallback_tfm; |
244 | }; | 264 | }; |
@@ -250,9 +270,6 @@ struct n2_hash_req_ctx { | |||
250 | struct sha256_state sha256; | 270 | struct sha256_state sha256; |
251 | } u; | 271 | } u; |
252 | 272 | ||
253 | unsigned char hash_key[64]; | ||
254 | unsigned char keyed_zero_hash[32]; | ||
255 | |||
256 | struct ahash_request fallback_req; | 273 | struct ahash_request fallback_req; |
257 | }; | 274 | }; |
258 | 275 | ||
@@ -374,9 +391,9 @@ static unsigned long submit_and_wait_for_tail(struct spu_queue *qp, | |||
374 | return hv_ret; | 391 | return hv_ret; |
375 | } | 392 | } |
376 | 393 | ||
377 | static int n2_hash_async_digest(struct ahash_request *req, | 394 | static int n2_do_async_digest(struct ahash_request *req, |
378 | unsigned int auth_type, unsigned int digest_size, | 395 | unsigned int auth_type, unsigned int digest_size, |
379 | unsigned int result_size, void *hash_loc) | 396 | unsigned int result_size, void *hash_loc) |
380 | { | 397 | { |
381 | struct crypto_ahash *tfm = crypto_ahash_reqtfm(req); | 398 | struct crypto_ahash *tfm = crypto_ahash_reqtfm(req); |
382 | struct cwq_initial_entry *ent; | 399 | struct cwq_initial_entry *ent; |
@@ -462,114 +479,22 @@ out: | |||
462 | return err; | 479 | return err; |
463 | } | 480 | } |
464 | 481 | ||
465 | static int n2_md5_async_digest(struct ahash_request *req) | 482 | static int n2_hash_async_digest(struct ahash_request *req) |
466 | { | ||
467 | struct n2_hash_req_ctx *rctx = ahash_request_ctx(req); | ||
468 | struct md5_state *m = &rctx->u.md5; | ||
469 | |||
470 | if (unlikely(req->nbytes == 0)) { | ||
471 | static const char md5_zero[MD5_DIGEST_SIZE] = { | ||
472 | 0xd4, 0x1d, 0x8c, 0xd9, 0x8f, 0x00, 0xb2, 0x04, | ||
473 | 0xe9, 0x80, 0x09, 0x98, 0xec, 0xf8, 0x42, 0x7e, | ||
474 | }; | ||
475 | |||
476 | memcpy(req->result, md5_zero, MD5_DIGEST_SIZE); | ||
477 | return 0; | ||
478 | } | ||
479 | m->hash[0] = cpu_to_le32(0x67452301); | ||
480 | m->hash[1] = cpu_to_le32(0xefcdab89); | ||
481 | m->hash[2] = cpu_to_le32(0x98badcfe); | ||
482 | m->hash[3] = cpu_to_le32(0x10325476); | ||
483 | |||
484 | return n2_hash_async_digest(req, AUTH_TYPE_MD5, | ||
485 | MD5_DIGEST_SIZE, MD5_DIGEST_SIZE, | ||
486 | m->hash); | ||
487 | } | ||
488 | |||
489 | static int n2_sha1_async_digest(struct ahash_request *req) | ||
490 | { | 483 | { |
484 | struct n2_ahash_alg *n2alg = n2_ahash_alg(req->base.tfm); | ||
491 | struct n2_hash_req_ctx *rctx = ahash_request_ctx(req); | 485 | struct n2_hash_req_ctx *rctx = ahash_request_ctx(req); |
492 | struct sha1_state *s = &rctx->u.sha1; | 486 | int ds; |
493 | 487 | ||
488 | ds = n2alg->digest_size; | ||
494 | if (unlikely(req->nbytes == 0)) { | 489 | if (unlikely(req->nbytes == 0)) { |
495 | static const char sha1_zero[SHA1_DIGEST_SIZE] = { | 490 | memcpy(req->result, n2alg->hash_zero, ds); |
496 | 0xda, 0x39, 0xa3, 0xee, 0x5e, 0x6b, 0x4b, 0x0d, 0x32, | ||
497 | 0x55, 0xbf, 0xef, 0x95, 0x60, 0x18, 0x90, 0xaf, 0xd8, | ||
498 | 0x07, 0x09 | ||
499 | }; | ||
500 | |||
501 | memcpy(req->result, sha1_zero, SHA1_DIGEST_SIZE); | ||
502 | return 0; | ||
503 | } | ||
504 | s->state[0] = SHA1_H0; | ||
505 | s->state[1] = SHA1_H1; | ||
506 | s->state[2] = SHA1_H2; | ||
507 | s->state[3] = SHA1_H3; | ||
508 | s->state[4] = SHA1_H4; | ||
509 | |||
510 | return n2_hash_async_digest(req, AUTH_TYPE_SHA1, | ||
511 | SHA1_DIGEST_SIZE, SHA1_DIGEST_SIZE, | ||
512 | s->state); | ||
513 | } | ||
514 | |||
515 | static int n2_sha256_async_digest(struct ahash_request *req) | ||
516 | { | ||
517 | struct n2_hash_req_ctx *rctx = ahash_request_ctx(req); | ||
518 | struct sha256_state *s = &rctx->u.sha256; | ||
519 | |||
520 | if (req->nbytes == 0) { | ||
521 | static const char sha256_zero[SHA256_DIGEST_SIZE] = { | ||
522 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, | ||
523 | 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, | ||
524 | 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, 0xa4, 0x95, 0x99, | ||
525 | 0x1b, 0x78, 0x52, 0xb8, 0x55 | ||
526 | }; | ||
527 | |||
528 | memcpy(req->result, sha256_zero, SHA256_DIGEST_SIZE); | ||
529 | return 0; | ||
530 | } | ||
531 | s->state[0] = SHA256_H0; | ||
532 | s->state[1] = SHA256_H1; | ||
533 | s->state[2] = SHA256_H2; | ||
534 | s->state[3] = SHA256_H3; | ||
535 | s->state[4] = SHA256_H4; | ||
536 | s->state[5] = SHA256_H5; | ||
537 | s->state[6] = SHA256_H6; | ||
538 | s->state[7] = SHA256_H7; | ||
539 | |||
540 | return n2_hash_async_digest(req, AUTH_TYPE_SHA256, | ||
541 | SHA256_DIGEST_SIZE, SHA256_DIGEST_SIZE, | ||
542 | s->state); | ||
543 | } | ||
544 | |||
545 | static int n2_sha224_async_digest(struct ahash_request *req) | ||
546 | { | ||
547 | struct n2_hash_req_ctx *rctx = ahash_request_ctx(req); | ||
548 | struct sha256_state *s = &rctx->u.sha256; | ||
549 | |||
550 | if (req->nbytes == 0) { | ||
551 | static const char sha224_zero[SHA224_DIGEST_SIZE] = { | ||
552 | 0xd1, 0x4a, 0x02, 0x8c, 0x2a, 0x3a, 0x2b, 0xc9, 0x47, | ||
553 | 0x61, 0x02, 0xbb, 0x28, 0x82, 0x34, 0xc4, 0x15, 0xa2, | ||
554 | 0xb0, 0x1f, 0x82, 0x8e, 0xa6, 0x2a, 0xc5, 0xb3, 0xe4, | ||
555 | 0x2f | ||
556 | }; | ||
557 | |||
558 | memcpy(req->result, sha224_zero, SHA224_DIGEST_SIZE); | ||
559 | return 0; | 491 | return 0; |
560 | } | 492 | } |
561 | s->state[0] = SHA224_H0; | 493 | memcpy(&rctx->u, n2alg->hash_init, n2alg->hw_op_hashsz); |
562 | s->state[1] = SHA224_H1; | ||
563 | s->state[2] = SHA224_H2; | ||
564 | s->state[3] = SHA224_H3; | ||
565 | s->state[4] = SHA224_H4; | ||
566 | s->state[5] = SHA224_H5; | ||
567 | s->state[6] = SHA224_H6; | ||
568 | s->state[7] = SHA224_H7; | ||
569 | 494 | ||
570 | return n2_hash_async_digest(req, AUTH_TYPE_SHA256, | 495 | return n2_do_async_digest(req, n2alg->auth_type, |
571 | SHA256_DIGEST_SIZE, SHA224_DIGEST_SIZE, | 496 | n2alg->hw_op_hashsz, ds, |
572 | s->state); | 497 | &rctx->u); |
573 | } | 498 | } |
574 | 499 | ||
575 | struct n2_cipher_context { | 500 | struct n2_cipher_context { |
@@ -1196,34 +1121,85 @@ static LIST_HEAD(cipher_algs); | |||
1196 | 1121 | ||
1197 | struct n2_hash_tmpl { | 1122 | struct n2_hash_tmpl { |
1198 | const char *name; | 1123 | const char *name; |
1199 | int (*digest)(struct ahash_request *req); | 1124 | const char *hash_zero; |
1125 | const u32 *hash_init; | ||
1126 | u8 hw_op_hashsz; | ||
1200 | u8 digest_size; | 1127 | u8 digest_size; |
1201 | u8 block_size; | 1128 | u8 block_size; |
1129 | u8 auth_type; | ||
1130 | }; | ||
1131 | |||
1132 | static const char md5_zero[MD5_DIGEST_SIZE] = { | ||
1133 | 0xd4, 0x1d, 0x8c, 0xd9, 0x8f, 0x00, 0xb2, 0x04, | ||
1134 | 0xe9, 0x80, 0x09, 0x98, 0xec, 0xf8, 0x42, 0x7e, | ||
1135 | }; | ||
1136 | static const u32 md5_init[MD5_HASH_WORDS] = { | ||
1137 | cpu_to_le32(0x67452301), | ||
1138 | cpu_to_le32(0xefcdab89), | ||
1139 | cpu_to_le32(0x98badcfe), | ||
1140 | cpu_to_le32(0x10325476), | ||
1141 | }; | ||
1142 | static const char sha1_zero[SHA1_DIGEST_SIZE] = { | ||
1143 | 0xda, 0x39, 0xa3, 0xee, 0x5e, 0x6b, 0x4b, 0x0d, 0x32, | ||
1144 | 0x55, 0xbf, 0xef, 0x95, 0x60, 0x18, 0x90, 0xaf, 0xd8, | ||
1145 | 0x07, 0x09 | ||
1146 | }; | ||
1147 | static const u32 sha1_init[SHA1_DIGEST_SIZE / 4] = { | ||
1148 | SHA1_H0, SHA1_H1, SHA1_H2, SHA1_H3, SHA1_H4, | ||
1149 | }; | ||
1150 | static const char sha256_zero[SHA256_DIGEST_SIZE] = { | ||
1151 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, | ||
1152 | 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, | ||
1153 | 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, 0xa4, 0x95, 0x99, | ||
1154 | 0x1b, 0x78, 0x52, 0xb8, 0x55 | ||
1155 | }; | ||
1156 | static const u32 sha256_init[SHA256_DIGEST_SIZE / 4] = { | ||
1157 | SHA256_H0, SHA256_H1, SHA256_H2, SHA256_H3, | ||
1158 | SHA256_H4, SHA256_H5, SHA256_H6, SHA256_H7, | ||
1159 | }; | ||
1160 | static const char sha224_zero[SHA224_DIGEST_SIZE] = { | ||
1161 | 0xd1, 0x4a, 0x02, 0x8c, 0x2a, 0x3a, 0x2b, 0xc9, 0x47, | ||
1162 | 0x61, 0x02, 0xbb, 0x28, 0x82, 0x34, 0xc4, 0x15, 0xa2, | ||
1163 | 0xb0, 0x1f, 0x82, 0x8e, 0xa6, 0x2a, 0xc5, 0xb3, 0xe4, | ||
1164 | 0x2f | ||
1202 | }; | 1165 | }; |
1166 | static const u32 sha224_init[SHA256_DIGEST_SIZE / 4] = { | ||
1167 | SHA224_H0, SHA224_H1, SHA224_H2, SHA224_H3, | ||
1168 | SHA224_H4, SHA224_H5, SHA224_H6, SHA224_H7, | ||
1169 | }; | ||
1170 | |||
1203 | static const struct n2_hash_tmpl hash_tmpls[] = { | 1171 | static const struct n2_hash_tmpl hash_tmpls[] = { |
1204 | { .name = "md5", | 1172 | { .name = "md5", |
1205 | .digest = n2_md5_async_digest, | 1173 | .hash_zero = md5_zero, |
1174 | .hash_init = md5_init, | ||
1175 | .auth_type = AUTH_TYPE_MD5, | ||
1176 | .hw_op_hashsz = MD5_DIGEST_SIZE, | ||
1206 | .digest_size = MD5_DIGEST_SIZE, | 1177 | .digest_size = MD5_DIGEST_SIZE, |
1207 | .block_size = MD5_HMAC_BLOCK_SIZE }, | 1178 | .block_size = MD5_HMAC_BLOCK_SIZE }, |
1208 | { .name = "sha1", | 1179 | { .name = "sha1", |
1209 | .digest = n2_sha1_async_digest, | 1180 | .hash_zero = sha1_zero, |
1181 | .hash_init = sha1_init, | ||
1182 | .auth_type = AUTH_TYPE_SHA1, | ||
1183 | .hw_op_hashsz = SHA1_DIGEST_SIZE, | ||
1210 | .digest_size = SHA1_DIGEST_SIZE, | 1184 | .digest_size = SHA1_DIGEST_SIZE, |
1211 | .block_size = SHA1_BLOCK_SIZE }, | 1185 | .block_size = SHA1_BLOCK_SIZE }, |
1212 | { .name = "sha256", | 1186 | { .name = "sha256", |
1213 | .digest = n2_sha256_async_digest, | 1187 | .hash_zero = sha256_zero, |
1188 | .hash_init = sha256_init, | ||
1189 | .auth_type = AUTH_TYPE_SHA256, | ||
1190 | .hw_op_hashsz = SHA256_DIGEST_SIZE, | ||
1214 | .digest_size = SHA256_DIGEST_SIZE, | 1191 | .digest_size = SHA256_DIGEST_SIZE, |
1215 | .block_size = SHA256_BLOCK_SIZE }, | 1192 | .block_size = SHA256_BLOCK_SIZE }, |
1216 | { .name = "sha224", | 1193 | { .name = "sha224", |
1217 | .digest = n2_sha224_async_digest, | 1194 | .hash_zero = sha224_zero, |
1195 | .hash_init = sha224_init, | ||
1196 | .auth_type = AUTH_TYPE_SHA256, | ||
1197 | .hw_op_hashsz = SHA256_DIGEST_SIZE, | ||
1218 | .digest_size = SHA224_DIGEST_SIZE, | 1198 | .digest_size = SHA224_DIGEST_SIZE, |
1219 | .block_size = SHA224_BLOCK_SIZE }, | 1199 | .block_size = SHA224_BLOCK_SIZE }, |
1220 | }; | 1200 | }; |
1221 | #define NUM_HASH_TMPLS ARRAY_SIZE(hash_tmpls) | 1201 | #define NUM_HASH_TMPLS ARRAY_SIZE(hash_tmpls) |
1222 | 1202 | ||
1223 | struct n2_ahash_alg { | ||
1224 | struct list_head entry; | ||
1225 | struct ahash_alg alg; | ||
1226 | }; | ||
1227 | static LIST_HEAD(ahash_algs); | 1203 | static LIST_HEAD(ahash_algs); |
1228 | 1204 | ||
1229 | static int algs_registered; | 1205 | static int algs_registered; |
@@ -1297,12 +1273,18 @@ static int __devinit __n2_register_one_ahash(const struct n2_hash_tmpl *tmpl) | |||
1297 | if (!p) | 1273 | if (!p) |
1298 | return -ENOMEM; | 1274 | return -ENOMEM; |
1299 | 1275 | ||
1276 | p->hash_zero = tmpl->hash_zero; | ||
1277 | p->hash_init = tmpl->hash_init; | ||
1278 | p->auth_type = tmpl->auth_type; | ||
1279 | p->hw_op_hashsz = tmpl->hw_op_hashsz; | ||
1280 | p->digest_size = tmpl->digest_size; | ||
1281 | |||
1300 | ahash = &p->alg; | 1282 | ahash = &p->alg; |
1301 | ahash->init = n2_hash_async_init; | 1283 | ahash->init = n2_hash_async_init; |
1302 | ahash->update = n2_hash_async_update; | 1284 | ahash->update = n2_hash_async_update; |
1303 | ahash->final = n2_hash_async_final; | 1285 | ahash->final = n2_hash_async_final; |
1304 | ahash->finup = n2_hash_async_finup; | 1286 | ahash->finup = n2_hash_async_finup; |
1305 | ahash->digest = tmpl->digest; | 1287 | ahash->digest = n2_hash_async_digest; |
1306 | 1288 | ||
1307 | halg = &ahash->halg; | 1289 | halg = &ahash->halg; |
1308 | halg->digestsize = tmpl->digest_size; | 1290 | halg->digestsize = tmpl->digest_size; |