sha1_glue.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * Cryptographic API.
  3. * Glue code for the SHA1 Secure Hash Algorithm assembler implementation
  4. *
  5. * This file is based on sha1_generic.c and sha1_ssse3_glue.c
  6. *
  7. * Copyright (c) Alan Smithee.
  8. * Copyright (c) Andrew McDonald <andrew@mcdonald.org.uk>
  9. * Copyright (c) Jean-Francois Dive <jef@linuxbe.org>
  10. * Copyright (c) Mathias Krause <minipli@googlemail.com>
  11. *
  12. * This program is free software; you can redistribute it and/or modify it
  13. * under the terms of the GNU General Public License as published by the Free
  14. * Software Foundation; either version 2 of the License, or (at your option)
  15. * any later version.
  16. *
  17. */
  18. #include <crypto/internal/hash.h>
  19. #include <linux/init.h>
  20. #include <linux/module.h>
  21. #include <linux/cryptohash.h>
  22. #include <linux/types.h>
  23. #include <crypto/sha.h>
  24. #include <asm/byteorder.h>
  25. struct SHA1_CTX {
  26. uint32_t h0,h1,h2,h3,h4;
  27. u64 count;
  28. u8 data[SHA1_BLOCK_SIZE];
  29. };
  30. asmlinkage void sha1_block_data_order(struct SHA1_CTX *digest,
  31. const unsigned char *data, unsigned int rounds);
  32. static int sha1_init(struct shash_desc *desc)
  33. {
  34. struct SHA1_CTX *sctx = shash_desc_ctx(desc);
  35. memset(sctx, 0, sizeof(*sctx));
  36. sctx->h0 = SHA1_H0;
  37. sctx->h1 = SHA1_H1;
  38. sctx->h2 = SHA1_H2;
  39. sctx->h3 = SHA1_H3;
  40. sctx->h4 = SHA1_H4;
  41. return 0;
  42. }
  43. static int __sha1_update(struct SHA1_CTX *sctx, const u8 *data,
  44. unsigned int len, unsigned int partial)
  45. {
  46. unsigned int done = 0;
  47. sctx->count += len;
  48. if (partial) {
  49. done = SHA1_BLOCK_SIZE - partial;
  50. memcpy(sctx->data + partial, data, done);
  51. sha1_block_data_order(sctx, sctx->data, 1);
  52. }
  53. if (len - done >= SHA1_BLOCK_SIZE) {
  54. const unsigned int rounds = (len - done) / SHA1_BLOCK_SIZE;
  55. sha1_block_data_order(sctx, data + done, rounds);
  56. done += rounds * SHA1_BLOCK_SIZE;
  57. }
  58. memcpy(sctx->data, data + done, len - done);
  59. return 0;
  60. }
  61. static int sha1_update(struct shash_desc *desc, const u8 *data,
  62. unsigned int len)
  63. {
  64. struct SHA1_CTX *sctx = shash_desc_ctx(desc);
  65. unsigned int partial = sctx->count % SHA1_BLOCK_SIZE;
  66. int res;
  67. /* Handle the fast case right here */
  68. if (partial + len < SHA1_BLOCK_SIZE) {
  69. sctx->count += len;
  70. memcpy(sctx->data + partial, data, len);
  71. return 0;
  72. }
  73. res = __sha1_update(sctx, data, len, partial);
  74. return res;
  75. }
  76. /* Add padding and return the message digest. */
  77. static int sha1_final(struct shash_desc *desc, u8 *out)
  78. {
  79. struct SHA1_CTX *sctx = shash_desc_ctx(desc);
  80. unsigned int i, index, padlen;
  81. __be32 *dst = (__be32 *)out;
  82. __be64 bits;
  83. static const u8 padding[SHA1_BLOCK_SIZE] = { 0x80, };
  84. bits = cpu_to_be64(sctx->count << 3);
  85. /* Pad out to 56 mod 64 and append length */
  86. index = sctx->count % SHA1_BLOCK_SIZE;
  87. padlen = (index < 56) ? (56 - index) : ((SHA1_BLOCK_SIZE+56) - index);
  88. /* We need to fill a whole block for __sha1_update() */
  89. if (padlen <= 56) {
  90. sctx->count += padlen;
  91. memcpy(sctx->data + index, padding, padlen);
  92. } else {
  93. __sha1_update(sctx, padding, padlen, index);
  94. }
  95. __sha1_update(sctx, (const u8 *)&bits, sizeof(bits), 56);
  96. /* Store state in digest */
  97. for (i = 0; i < 5; i++)
  98. dst[i] = cpu_to_be32(((u32 *)sctx)[i]);
  99. /* Wipe context */
  100. memset(sctx, 0, sizeof(*sctx));
  101. return 0;
  102. }
  103. static int sha1_export(struct shash_desc *desc, void *out)
  104. {
  105. struct SHA1_CTX *sctx = shash_desc_ctx(desc);
  106. memcpy(out, sctx, sizeof(*sctx));
  107. return 0;
  108. }
  109. static int sha1_import(struct shash_desc *desc, const void *in)
  110. {
  111. struct SHA1_CTX *sctx = shash_desc_ctx(desc);
  112. memcpy(sctx, in, sizeof(*sctx));
  113. return 0;
  114. }
  115. static struct shash_alg alg = {
  116. .digestsize = SHA1_DIGEST_SIZE,
  117. .init = sha1_init,
  118. .update = sha1_update,
  119. .final = sha1_final,
  120. .export = sha1_export,
  121. .import = sha1_import,
  122. .descsize = sizeof(struct SHA1_CTX),
  123. .statesize = sizeof(struct SHA1_CTX),
  124. .base = {
  125. .cra_name = "sha1",
  126. .cra_driver_name= "sha1-asm",
  127. .cra_priority = 150,
  128. .cra_flags = CRYPTO_ALG_TYPE_SHASH,
  129. .cra_blocksize = SHA1_BLOCK_SIZE,
  130. .cra_module = THIS_MODULE,
  131. }
  132. };
  133. static int __init sha1_mod_init(void)
  134. {
  135. return crypto_register_shash(&alg);
  136. }
  137. static void __exit sha1_mod_fini(void)
  138. {
  139. crypto_unregister_shash(&alg);
  140. }
  141. module_init(sha1_mod_init);
  142. module_exit(sha1_mod_fini);
  143. MODULE_LICENSE("GPL");
  144. MODULE_DESCRIPTION("SHA1 Secure Hash Algorithm (ARM)");
  145. MODULE_ALIAS("sha1");
  146. MODULE_AUTHOR("David McCullough <ucdevel@gmail.com>");