blake2_impl.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. /*
  10. * Derived from the BLAKE2 reference implementation written by Samuel Neves.
  11. * Copyright 2012, Samuel Neves <sneves@dei.uc.pt>
  12. * More information about the BLAKE2 hash function and its implementations
  13. * can be found at https://blake2.net.
  14. */
  15. #include <string.h>
  16. #include "e_os.h"
  17. static ossl_inline uint32_t load32(const uint8_t *src)
  18. {
  19. const union {
  20. long one;
  21. char little;
  22. } is_endian = { 1 };
  23. if (is_endian.little) {
  24. uint32_t w;
  25. memcpy(&w, src, sizeof(w));
  26. return w;
  27. } else {
  28. uint32_t w = ((uint32_t)src[0])
  29. | ((uint32_t)src[1] << 8)
  30. | ((uint32_t)src[2] << 16)
  31. | ((uint32_t)src[3] << 24);
  32. return w;
  33. }
  34. }
  35. static ossl_inline uint64_t load64(const uint8_t *src)
  36. {
  37. const union {
  38. long one;
  39. char little;
  40. } is_endian = { 1 };
  41. if (is_endian.little) {
  42. uint64_t w;
  43. memcpy(&w, src, sizeof(w));
  44. return w;
  45. } else {
  46. uint64_t w = ((uint64_t)src[0])
  47. | ((uint64_t)src[1] << 8)
  48. | ((uint64_t)src[2] << 16)
  49. | ((uint64_t)src[3] << 24)
  50. | ((uint64_t)src[4] << 32)
  51. | ((uint64_t)src[5] << 40)
  52. | ((uint64_t)src[6] << 48)
  53. | ((uint64_t)src[7] << 56);
  54. return w;
  55. }
  56. }
  57. static ossl_inline void store32(uint8_t *dst, uint32_t w)
  58. {
  59. const union {
  60. long one;
  61. char little;
  62. } is_endian = { 1 };
  63. if (is_endian.little) {
  64. memcpy(dst, &w, sizeof(w));
  65. } else {
  66. uint8_t *p = (uint8_t *)dst;
  67. int i;
  68. for (i = 0; i < 4; i++)
  69. p[i] = (uint8_t)(w >> (8 * i));
  70. }
  71. }
  72. static ossl_inline void store64(uint8_t *dst, uint64_t w)
  73. {
  74. const union {
  75. long one;
  76. char little;
  77. } is_endian = { 1 };
  78. if (is_endian.little) {
  79. memcpy(dst, &w, sizeof(w));
  80. } else {
  81. uint8_t *p = (uint8_t *)dst;
  82. int i;
  83. for (i = 0; i < 8; i++)
  84. p[i] = (uint8_t)(w >> (8 * i));
  85. }
  86. }
  87. static ossl_inline uint64_t load48(const uint8_t *src)
  88. {
  89. uint64_t w = ((uint64_t)src[0])
  90. | ((uint64_t)src[1] << 8)
  91. | ((uint64_t)src[2] << 16)
  92. | ((uint64_t)src[3] << 24)
  93. | ((uint64_t)src[4] << 32)
  94. | ((uint64_t)src[5] << 40);
  95. return w;
  96. }
  97. static ossl_inline void store48(uint8_t *dst, uint64_t w)
  98. {
  99. uint8_t *p = (uint8_t *)dst;
  100. p[0] = (uint8_t)w;
  101. p[1] = (uint8_t)(w>>8);
  102. p[2] = (uint8_t)(w>>16);
  103. p[3] = (uint8_t)(w>>24);
  104. p[4] = (uint8_t)(w>>32);
  105. p[5] = (uint8_t)(w>>40);
  106. }
  107. static ossl_inline uint32_t rotr32(const uint32_t w, const unsigned int c)
  108. {
  109. return (w >> c) | (w << (32 - c));
  110. }
  111. static ossl_inline uint64_t rotr64(const uint64_t w, const unsigned int c)
  112. {
  113. return (w >> c) | (w << (64 - c));
  114. }