sha1.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * Cryptographic API.
  3. *
  4. * powerpc implementation of the SHA1 Secure Hash Algorithm.
  5. *
  6. * Derived from cryptoapi implementation, adapted for in-place
  7. * scatterlist interface.
  8. *
  9. * Derived from "crypto/sha1.c"
  10. * Copyright (c) Alan Smithee.
  11. * Copyright (c) Andrew McDonald <andrew@mcdonald.org.uk>
  12. * Copyright (c) Jean-Francois Dive <jef@linuxbe.org>
  13. *
  14. * This program is free software; you can redistribute it and/or modify it
  15. * under the terms of the GNU General Public License as published by the Free
  16. * Software Foundation; either version 2 of the License, or (at your option)
  17. * any later version.
  18. *
  19. */
  20. #include <crypto/internal/hash.h>
  21. #include <linux/init.h>
  22. #include <linux/module.h>
  23. #include <linux/mm.h>
  24. #include <linux/cryptohash.h>
  25. #include <linux/types.h>
  26. #include <crypto/sha.h>
  27. #include <asm/byteorder.h>
  28. extern void powerpc_sha_transform(u32 *state, const u8 *src, u32 *temp);
  29. static int sha1_init(struct shash_desc *desc)
  30. {
  31. struct sha1_state *sctx = shash_desc_ctx(desc);
  32. *sctx = (struct sha1_state){
  33. .state = { SHA1_H0, SHA1_H1, SHA1_H2, SHA1_H3, SHA1_H4 },
  34. };
  35. return 0;
  36. }
  37. static int sha1_update(struct shash_desc *desc, const u8 *data,
  38. unsigned int len)
  39. {
  40. struct sha1_state *sctx = shash_desc_ctx(desc);
  41. unsigned int partial, done;
  42. const u8 *src;
  43. partial = sctx->count & 0x3f;
  44. sctx->count += len;
  45. done = 0;
  46. src = data;
  47. if ((partial + len) > 63) {
  48. u32 temp[SHA_WORKSPACE_WORDS];
  49. if (partial) {
  50. done = -partial;
  51. memcpy(sctx->buffer + partial, data, done + 64);
  52. src = sctx->buffer;
  53. }
  54. do {
  55. powerpc_sha_transform(sctx->state, src, temp);
  56. done += 64;
  57. src = data + done;
  58. } while (done + 63 < len);
  59. memzero_explicit(temp, sizeof(temp));
  60. partial = 0;
  61. }
  62. memcpy(sctx->buffer + partial, src, len - done);
  63. return 0;
  64. }
  65. /* Add padding and return the message digest. */
  66. static int sha1_final(struct shash_desc *desc, u8 *out)
  67. {
  68. struct sha1_state *sctx = shash_desc_ctx(desc);
  69. __be32 *dst = (__be32 *)out;
  70. u32 i, index, padlen;
  71. __be64 bits;
  72. static const u8 padding[64] = { 0x80, };
  73. bits = cpu_to_be64(sctx->count << 3);
  74. /* Pad out to 56 mod 64 */
  75. index = sctx->count & 0x3f;
  76. padlen = (index < 56) ? (56 - index) : ((64+56) - index);
  77. sha1_update(desc, padding, padlen);
  78. /* Append length */
  79. sha1_update(desc, (const u8 *)&bits, sizeof(bits));
  80. /* Store state in digest */
  81. for (i = 0; i < 5; i++)
  82. dst[i] = cpu_to_be32(sctx->state[i]);
  83. /* Wipe context */
  84. memset(sctx, 0, sizeof *sctx);
  85. return 0;
  86. }
  87. static int sha1_export(struct shash_desc *desc, void *out)
  88. {
  89. struct sha1_state *sctx = shash_desc_ctx(desc);
  90. memcpy(out, sctx, sizeof(*sctx));
  91. return 0;
  92. }
  93. static int sha1_import(struct shash_desc *desc, const void *in)
  94. {
  95. struct sha1_state *sctx = shash_desc_ctx(desc);
  96. memcpy(sctx, in, sizeof(*sctx));
  97. return 0;
  98. }
  99. static struct shash_alg alg = {
  100. .digestsize = SHA1_DIGEST_SIZE,
  101. .init = sha1_init,
  102. .update = sha1_update,
  103. .final = sha1_final,
  104. .export = sha1_export,
  105. .import = sha1_import,
  106. .descsize = sizeof(struct sha1_state),
  107. .statesize = sizeof(struct sha1_state),
  108. .base = {
  109. .cra_name = "sha1",
  110. .cra_driver_name= "sha1-powerpc",
  111. .cra_flags = CRYPTO_ALG_TYPE_SHASH,
  112. .cra_blocksize = SHA1_BLOCK_SIZE,
  113. .cra_module = THIS_MODULE,
  114. }
  115. };
  116. static int __init sha1_powerpc_mod_init(void)
  117. {
  118. return crypto_register_shash(&alg);
  119. }
  120. static void __exit sha1_powerpc_mod_fini(void)
  121. {
  122. crypto_unregister_shash(&alg);
  123. }
  124. module_init(sha1_powerpc_mod_init);
  125. module_exit(sha1_powerpc_mod_fini);
  126. MODULE_LICENSE("GPL");
  127. MODULE_DESCRIPTION("SHA1 Secure Hash Algorithm");
  128. MODULE_ALIAS_CRYPTO("sha1");
  129. MODULE_ALIAS_CRYPTO("sha1-powerpc");