blake2-int.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. BLAKE2 reference source code package - reference C implementations
  3. Written in 2012 by Samuel Neves <sneves@dei.uc.pt>
  4. To the extent possible under law, the author(s) have dedicated all copyright
  5. and related and neighboring rights to this software to the public domain
  6. worldwide. This software is distributed without any warranty.
  7. You should have received a copy of the CC0 Public Domain Dedication along with
  8. this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
  9. */
  10. /* blake2-int.h
  11. *
  12. * Copyright (C) 2006-2022 wolfSSL Inc.
  13. *
  14. * This file is part of wolfSSL.
  15. *
  16. * wolfSSL is free software; you can redistribute it and/or modify
  17. * it under the terms of the GNU General Public License as published by
  18. * the Free Software Foundation; either version 2 of the License, or
  19. * (at your option) any later version.
  20. *
  21. * wolfSSL is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU General Public License
  27. * along with this program; if not, write to the Free Software
  28. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  29. */
  30. #ifndef WOLFCRYPT_BLAKE2_INT_H
  31. #define WOLFCRYPT_BLAKE2_INT_H
  32. #include <wolfssl/wolfcrypt/types.h>
  33. #if defined(__cplusplus)
  34. extern "C" {
  35. #endif
  36. enum blake2s_constant
  37. {
  38. BLAKE2S_BLOCKBYTES = 64,
  39. BLAKE2S_OUTBYTES = 32,
  40. BLAKE2S_KEYBYTES = 32,
  41. BLAKE2S_SALTBYTES = 8,
  42. BLAKE2S_PERSONALBYTES = 8
  43. };
  44. enum blake2b_constant
  45. {
  46. BLAKE2B_BLOCKBYTES = 128,
  47. BLAKE2B_OUTBYTES = 64,
  48. BLAKE2B_KEYBYTES = 64,
  49. BLAKE2B_SALTBYTES = 16,
  50. BLAKE2B_PERSONALBYTES = 16
  51. };
  52. #pragma pack(push, 1)
  53. typedef struct __blake2s_param
  54. {
  55. byte digest_length; /* 1 */
  56. byte key_length; /* 2 */
  57. byte fanout; /* 3 */
  58. byte depth; /* 4 */
  59. word32 leaf_length; /* 8 */
  60. byte node_offset[6];/* 14 */
  61. byte node_depth; /* 15 */
  62. byte inner_length; /* 16 */
  63. /* byte reserved[0]; */
  64. byte salt[BLAKE2S_SALTBYTES]; /* 24 */
  65. byte personal[BLAKE2S_PERSONALBYTES]; /* 32 */
  66. } blake2s_param;
  67. typedef struct __blake2s_state
  68. {
  69. word32 h[8];
  70. word32 t[2];
  71. word32 f[2];
  72. byte buf[2 * BLAKE2S_BLOCKBYTES];
  73. word32 buflen;
  74. byte last_node;
  75. } blake2s_state ;
  76. typedef struct __blake2b_param
  77. {
  78. byte digest_length; /* 1 */
  79. byte key_length; /* 2 */
  80. byte fanout; /* 3 */
  81. byte depth; /* 4 */
  82. word32 leaf_length; /* 8 */
  83. word64 node_offset; /* 16 */
  84. byte node_depth; /* 17 */
  85. byte inner_length; /* 18 */
  86. byte reserved[14]; /* 32 */
  87. byte salt[BLAKE2B_SALTBYTES]; /* 48 */
  88. byte personal[BLAKE2B_PERSONALBYTES]; /* 64 */
  89. } blake2b_param;
  90. typedef struct __blake2b_state
  91. {
  92. word64 h[8];
  93. word64 t[2];
  94. word64 f[2];
  95. byte buf[2 * BLAKE2B_BLOCKBYTES];
  96. word64 buflen;
  97. byte last_node;
  98. } blake2b_state;
  99. typedef struct __blake2sp_state
  100. {
  101. blake2s_state S[8][1];
  102. blake2s_state R[1];
  103. byte buf[8 * BLAKE2S_BLOCKBYTES];
  104. word32 buflen;
  105. } blake2sp_state;
  106. typedef struct __blake2bp_state
  107. {
  108. blake2b_state S[4][1];
  109. blake2b_state R[1];
  110. byte buf[4 * BLAKE2B_BLOCKBYTES];
  111. word64 buflen;
  112. } blake2bp_state;
  113. #pragma pack(pop)
  114. /* Streaming API */
  115. int blake2s_init( blake2s_state *S, byte outlen );
  116. int blake2s_init_key( blake2s_state *S, byte outlen, const void *key, byte keylen );
  117. int blake2s_init_param( blake2s_state *S, const blake2s_param *P );
  118. int blake2s_update( blake2s_state *S, const byte *in, word32 inlen );
  119. int blake2s_final( blake2s_state *S, byte *out, byte outlen );
  120. int blake2b_init( blake2b_state *S, byte outlen );
  121. int blake2b_init_key( blake2b_state *S, byte outlen, const void *key, byte keylen );
  122. int blake2b_init_param( blake2b_state *S, const blake2b_param *P );
  123. int blake2b_update( blake2b_state *S, const byte *in, word64 inlen );
  124. int blake2b_final( blake2b_state *S, byte *out, byte outlen );
  125. int blake2sp_init( blake2sp_state *S, byte outlen );
  126. int blake2sp_init_key( blake2sp_state *S, byte outlen, const void *key, byte keylen );
  127. int blake2sp_update( blake2sp_state *S, const byte *in, word32 inlen );
  128. int blake2sp_final( blake2sp_state *S, byte *out, byte outlen );
  129. int blake2bp_init( blake2bp_state *S, byte outlen );
  130. int blake2bp_init_key( blake2bp_state *S, byte outlen, const void *key, byte keylen );
  131. int blake2bp_update( blake2bp_state *S, const byte *in, word64 inlen );
  132. int blake2bp_final( blake2bp_state *S, byte *out, byte outlen );
  133. /* Simple API */
  134. int blake2s( byte *out, const void *in, const void *key, byte outlen, word32 inlen, byte keylen );
  135. int blake2b( byte *out, const void *in, const void *key, byte outlen, word64 inlen, byte keylen );
  136. int blake2sp( byte *out, const void *in, const void *key, byte outlen, word32 inlen, byte keylen );
  137. int blake2bp( byte *out, const void *in, const void *key, byte outlen, word64 inlen, byte keylen );
  138. static WC_INLINE int blake2( byte *out, const void *in, const void *key, byte outlen, word64 inlen, byte keylen )
  139. {
  140. return blake2b( out, in, key, outlen, inlen, keylen );
  141. }
  142. #if defined(__cplusplus)
  143. }
  144. #endif
  145. #endif /* WOLFCRYPT_BLAKE2_INT_H */