blake2_impl.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * Copyright 2016-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (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 "internal/endian.h"
  17. static ossl_inline uint32_t load32(const uint8_t *src)
  18. {
  19. DECLARE_IS_ENDIAN;
  20. if (IS_LITTLE_ENDIAN) {
  21. uint32_t w;
  22. memcpy(&w, src, sizeof(w));
  23. return w;
  24. } else {
  25. uint32_t w = ((uint32_t)src[0])
  26. | ((uint32_t)src[1] << 8)
  27. | ((uint32_t)src[2] << 16)
  28. | ((uint32_t)src[3] << 24);
  29. return w;
  30. }
  31. }
  32. static ossl_inline uint64_t load64(const uint8_t *src)
  33. {
  34. DECLARE_IS_ENDIAN;
  35. if (IS_LITTLE_ENDIAN) {
  36. uint64_t w;
  37. memcpy(&w, src, sizeof(w));
  38. return w;
  39. } else {
  40. uint64_t w = ((uint64_t)src[0])
  41. | ((uint64_t)src[1] << 8)
  42. | ((uint64_t)src[2] << 16)
  43. | ((uint64_t)src[3] << 24)
  44. | ((uint64_t)src[4] << 32)
  45. | ((uint64_t)src[5] << 40)
  46. | ((uint64_t)src[6] << 48)
  47. | ((uint64_t)src[7] << 56);
  48. return w;
  49. }
  50. }
  51. static ossl_inline void store32(uint8_t *dst, uint32_t w)
  52. {
  53. DECLARE_IS_ENDIAN;
  54. if (IS_LITTLE_ENDIAN) {
  55. memcpy(dst, &w, sizeof(w));
  56. } else {
  57. uint8_t *p = (uint8_t *)dst;
  58. int i;
  59. for (i = 0; i < 4; i++)
  60. p[i] = (uint8_t)(w >> (8 * i));
  61. }
  62. }
  63. static ossl_inline void store64(uint8_t *dst, uint64_t w)
  64. {
  65. DECLARE_IS_ENDIAN;
  66. if (IS_LITTLE_ENDIAN) {
  67. memcpy(dst, &w, sizeof(w));
  68. } else {
  69. uint8_t *p = (uint8_t *)dst;
  70. int i;
  71. for (i = 0; i < 8; i++)
  72. p[i] = (uint8_t)(w >> (8 * i));
  73. }
  74. }
  75. static ossl_inline uint64_t load48(const uint8_t *src)
  76. {
  77. uint64_t w = ((uint64_t)src[0])
  78. | ((uint64_t)src[1] << 8)
  79. | ((uint64_t)src[2] << 16)
  80. | ((uint64_t)src[3] << 24)
  81. | ((uint64_t)src[4] << 32)
  82. | ((uint64_t)src[5] << 40);
  83. return w;
  84. }
  85. static ossl_inline void store48(uint8_t *dst, uint64_t w)
  86. {
  87. uint8_t *p = (uint8_t *)dst;
  88. p[0] = (uint8_t)w;
  89. p[1] = (uint8_t)(w>>8);
  90. p[2] = (uint8_t)(w>>16);
  91. p[3] = (uint8_t)(w>>24);
  92. p[4] = (uint8_t)(w>>32);
  93. p[5] = (uint8_t)(w>>40);
  94. }
  95. static ossl_inline uint32_t rotr32(const uint32_t w, const unsigned int c)
  96. {
  97. return (w >> c) | (w << (32 - c));
  98. }
  99. static ossl_inline uint64_t rotr64(const uint64_t w, const unsigned int c)
  100. {
  101. return (w >> c) | (w << (64 - c));
  102. }