bhash.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /**
  2. * @file bhash.h
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * This file is part of BadVPN.
  8. *
  9. * BadVPN is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * BadVPN is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. *
  22. * @section DESCRIPTION
  23. *
  24. * Cryptographic hash funtions abstraction.
  25. */
  26. #ifndef BADVPN_SECURITY_BHASH_H
  27. #define BADVPN_SECURITY_BHASH_H
  28. #include <stdint.h>
  29. #include <openssl/md5.h>
  30. #include <openssl/sha.h>
  31. #include <misc/debug.h>
  32. #define BHASH_TYPE_MD5 1
  33. #define BHASH_TYPE_MD5_SIZE 16
  34. #define BHASH_TYPE_SHA1 2
  35. #define BHASH_TYPE_SHA1_SIZE 20
  36. /**
  37. * Checks if the given hash type number is valid.
  38. *
  39. * @param type hash type number
  40. * @return 1 if valid, 0 if not
  41. */
  42. static int BHash_type_valid (int type);
  43. /**
  44. * Returns the size of a hash.
  45. *
  46. * @param cipher hash type number. Must be valid.
  47. * @return hash size in bytes
  48. */
  49. static int BHash_size (int type);
  50. /**
  51. * Calculates a hash.
  52. *
  53. * @param type hash type number. Must be valid.
  54. * @param data data to calculate the hash of
  55. * @param data_len length of data
  56. * @param out the hash will be written here. Must not overlap with data.
  57. */
  58. static void BHash_calculate (int type, uint8_t *data, int data_len, uint8_t *out);
  59. int BHash_type_valid (int type)
  60. {
  61. switch (type) {
  62. case BHASH_TYPE_MD5:
  63. case BHASH_TYPE_SHA1:
  64. return 1;
  65. default:
  66. return 0;
  67. }
  68. }
  69. int BHash_size (int type)
  70. {
  71. switch (type) {
  72. case BHASH_TYPE_MD5:
  73. return BHASH_TYPE_MD5_SIZE;
  74. case BHASH_TYPE_SHA1:
  75. return BHASH_TYPE_SHA1_SIZE;
  76. default:
  77. ASSERT(0)
  78. return 0;
  79. }
  80. }
  81. void BHash_calculate (int type, uint8_t *data, int data_len, uint8_t *out)
  82. {
  83. switch (type) {
  84. case BHASH_TYPE_MD5:
  85. MD5(data, data_len, out);
  86. break;
  87. case BHASH_TYPE_SHA1:
  88. SHA1(data, data_len, out);
  89. break;
  90. default:
  91. ASSERT(0)
  92. ;
  93. }
  94. }
  95. #endif