You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

140 lines
5.1 KiB

  1. /*
  2. * FILE: sha2.h
  3. * AUTHOR: Aaron D. Gifford
  4. * http://www.aarongifford.com/computers/sha.html
  5. *
  6. * Copyright (c) 2000-2003, Aaron D. Gifford
  7. * All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. * 3. Neither the name of the copyright holder nor the names of contributors
  18. * may be used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTOR(S) ``AS IS'' AND
  22. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTOR(S) BE LIABLE
  25. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. * SUCH DAMAGE.
  32. *
  33. * $Id: sha2.h,v 1.4 2004/01/07 19:06:18 adg Exp $
  34. */
  35. #ifndef __SHA2_H__
  36. #define __SHA2_H__
  37. #include "cm_sha2_mangle.h"
  38. /* CMake modification: use integer types from KWIML. */
  39. #include <cm_kwiml.h>
  40. typedef KWIML_INT_uint8_t cm_sha2_uint8_t;
  41. typedef KWIML_INT_uint32_t cm_sha2_uint32_t;
  42. typedef KWIML_INT_uint64_t cm_sha2_uint64_t;
  43. #ifdef __cplusplus
  44. extern "C" {
  45. #endif
  46. /*
  47. * Import u_intXX_t size_t type definitions from system headers. You
  48. * may need to change this, or define these things yourself in this
  49. * file.
  50. */
  51. #include <sys/types.h>
  52. /*** SHA-224/256/384/512 Various Length Definitions *******************/
  53. /* Digest lengths for SHA-1/224/256/384/512 */
  54. #define SHA1_DIGEST_LENGTH 20
  55. #define SHA1_DIGEST_STRING_LENGTH (SHA1_DIGEST_LENGTH * 2 + 1)
  56. #define SHA224_DIGEST_LENGTH 28
  57. #define SHA224_DIGEST_STRING_LENGTH (SHA224_DIGEST_LENGTH * 2 + 1)
  58. #define SHA256_DIGEST_LENGTH 32
  59. #define SHA256_DIGEST_STRING_LENGTH (SHA256_DIGEST_LENGTH * 2 + 1)
  60. #define SHA384_DIGEST_LENGTH 48
  61. #define SHA384_DIGEST_STRING_LENGTH (SHA384_DIGEST_LENGTH * 2 + 1)
  62. #define SHA512_DIGEST_LENGTH 64
  63. #define SHA512_DIGEST_STRING_LENGTH (SHA512_DIGEST_LENGTH * 2 + 1)
  64. /*** SHA-224/256/384/512 Context Structures ***************************/
  65. typedef union _SHA_CTX {
  66. /* SHA-1 uses this part of the union: */
  67. struct {
  68. cm_sha2_uint32_t state[5];
  69. cm_sha2_uint64_t bitcount;
  70. cm_sha2_uint8_t buffer[64];
  71. } s1;
  72. /* SHA-224 and SHA-256 use this part of the union: */
  73. struct {
  74. cm_sha2_uint32_t state[8];
  75. cm_sha2_uint64_t bitcount;
  76. cm_sha2_uint8_t buffer[64];
  77. } s256;
  78. /* SHA-384 and SHA-512 use this part of the union: */
  79. struct {
  80. cm_sha2_uint64_t state[8];
  81. cm_sha2_uint64_t bitcount[2];
  82. cm_sha2_uint8_t buffer[128];
  83. } s512;
  84. } SHA_CTX;
  85. /*** SHA-256/384/512 Function Prototypes ******************************/
  86. void SHA1_Init(SHA_CTX*);
  87. void SHA1_Update(SHA_CTX*, const cm_sha2_uint8_t*, size_t);
  88. void SHA1_Final(cm_sha2_uint8_t[SHA1_DIGEST_LENGTH], SHA_CTX*);
  89. char* SHA1_End(SHA_CTX*, char[SHA1_DIGEST_STRING_LENGTH]);
  90. char* SHA1_Data(const cm_sha2_uint8_t*, size_t,
  91. char[SHA1_DIGEST_STRING_LENGTH]);
  92. void SHA224_Init(SHA_CTX*);
  93. void SHA224_Update(SHA_CTX*, const cm_sha2_uint8_t*, size_t);
  94. void SHA224_Final(cm_sha2_uint8_t[SHA224_DIGEST_LENGTH], SHA_CTX*);
  95. char* SHA224_End(SHA_CTX*, char[SHA224_DIGEST_STRING_LENGTH]);
  96. char* SHA224_Data(const cm_sha2_uint8_t*, size_t,
  97. char[SHA224_DIGEST_STRING_LENGTH]);
  98. void SHA256_Init(SHA_CTX*);
  99. void SHA256_Update(SHA_CTX*, const cm_sha2_uint8_t*, size_t);
  100. void SHA256_Final(cm_sha2_uint8_t[SHA256_DIGEST_LENGTH], SHA_CTX*);
  101. char* SHA256_End(SHA_CTX*, char[SHA256_DIGEST_STRING_LENGTH]);
  102. char* SHA256_Data(const cm_sha2_uint8_t*, size_t,
  103. char[SHA256_DIGEST_STRING_LENGTH]);
  104. void SHA384_Init(SHA_CTX*);
  105. void SHA384_Update(SHA_CTX*, const cm_sha2_uint8_t*, size_t);
  106. void SHA384_Final(cm_sha2_uint8_t[SHA384_DIGEST_LENGTH], SHA_CTX*);
  107. char* SHA384_End(SHA_CTX*, char[SHA384_DIGEST_STRING_LENGTH]);
  108. char* SHA384_Data(const cm_sha2_uint8_t*, size_t,
  109. char[SHA384_DIGEST_STRING_LENGTH]);
  110. void SHA512_Init(SHA_CTX*);
  111. void SHA512_Update(SHA_CTX*, const cm_sha2_uint8_t*, size_t);
  112. void SHA512_Final(cm_sha2_uint8_t[SHA512_DIGEST_LENGTH], SHA_CTX*);
  113. char* SHA512_End(SHA_CTX*, char[SHA512_DIGEST_STRING_LENGTH]);
  114. char* SHA512_Data(const cm_sha2_uint8_t*, size_t,
  115. char[SHA512_DIGEST_STRING_LENGTH]);
  116. #ifdef __cplusplus
  117. }
  118. #endif /* __cplusplus */
  119. #endif /* __SHA2_H__ */