2
0

blake2_impl.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * Copyright 2016-2017 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. static ossl_inline uint32_t load32(const uint8_t *src)
  17. {
  18. const union {
  19. long one;
  20. char little;
  21. } is_endian = { 1 };
  22. if (is_endian.little) {
  23. uint32_t w;
  24. memcpy(&w, src, sizeof(w));
  25. return w;
  26. } else {
  27. uint32_t w = ((uint32_t)src[0])
  28. | ((uint32_t)src[1] << 8)
  29. | ((uint32_t)src[2] << 16)
  30. | ((uint32_t)src[3] << 24);
  31. return w;
  32. }
  33. }
  34. static ossl_inline uint64_t load64(const uint8_t *src)
  35. {
  36. const union {
  37. long one;
  38. char little;
  39. } is_endian = { 1 };
  40. if (is_endian.little) {
  41. uint64_t w;
  42. memcpy(&w, src, sizeof(w));
  43. return w;
  44. } else {
  45. uint64_t w = ((uint64_t)src[0])
  46. | ((uint64_t)src[1] << 8)
  47. | ((uint64_t)src[2] << 16)
  48. | ((uint64_t)src[3] << 24)
  49. | ((uint64_t)src[4] << 32)
  50. | ((uint64_t)src[5] << 40)
  51. | ((uint64_t)src[6] << 48)
  52. | ((uint64_t)src[7] << 56);
  53. return w;
  54. }
  55. }
  56. static ossl_inline void store32(uint8_t *dst, uint32_t w)
  57. {
  58. const union {
  59. long one;
  60. char little;
  61. } is_endian = { 1 };
  62. if (is_endian.little) {
  63. memcpy(dst, &w, sizeof(w));
  64. } else {
  65. uint8_t *p = (uint8_t *)dst;
  66. int i;
  67. for (i = 0; i < 4; i++)
  68. p[i] = (uint8_t)(w >> (8 * i));
  69. }
  70. }
  71. static ossl_inline void store64(uint8_t *dst, uint64_t w)
  72. {
  73. const union {
  74. long one;
  75. char little;
  76. } is_endian = { 1 };
  77. if (is_endian.little) {
  78. memcpy(dst, &w, sizeof(w));
  79. } else {
  80. uint8_t *p = (uint8_t *)dst;
  81. int i;
  82. for (i = 0; i < 8; i++)
  83. p[i] = (uint8_t)(w >> (8 * i));
  84. }
  85. }
  86. static ossl_inline uint64_t load48(const uint8_t *src)
  87. {
  88. uint64_t w = ((uint64_t)src[0])
  89. | ((uint64_t)src[1] << 8)
  90. | ((uint64_t)src[2] << 16)
  91. | ((uint64_t)src[3] << 24)
  92. | ((uint64_t)src[4] << 32)
  93. | ((uint64_t)src[5] << 40);
  94. return w;
  95. }
  96. static ossl_inline void store48(uint8_t *dst, uint64_t w)
  97. {
  98. uint8_t *p = (uint8_t *)dst;
  99. p[0] = (uint8_t)w;
  100. p[1] = (uint8_t)(w>>8);
  101. p[2] = (uint8_t)(w>>16);
  102. p[3] = (uint8_t)(w>>24);
  103. p[4] = (uint8_t)(w>>32);
  104. p[5] = (uint8_t)(w>>40);
  105. }
  106. static ossl_inline uint32_t rotr32(const uint32_t w, const unsigned int c)
  107. {
  108. return (w >> c) | (w << (32 - c));
  109. }
  110. static ossl_inline uint64_t rotr64(const uint64_t w, const unsigned int c)
  111. {
  112. return (w >> c) | (w << (64 - c));
  113. }