streebog.odin 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. package streebog
  2. /*
  3. Copyright 2021 zhibog
  4. Made available under the BSD-3 license.
  5. List of contributors:
  6. zhibog, dotbmp: Initial implementation.
  7. Jeroen van Rijn: Context design to be able to change from Odin implementation to bindings.
  8. Implementation of the Streebog hashing algorithm, standardized as GOST R 34.11-2012 in RFC 6986 <https://datatracker.ietf.org/doc/html/rfc6986>
  9. */
  10. import "core:os"
  11. import "core:io"
  12. import "../util"
  13. import "../botan"
  14. import "../_ctx"
  15. /*
  16. Context initialization and switching between the Odin implementation and the bindings
  17. */
  18. USE_BOTAN_LIB :: bool(#config(USE_BOTAN_LIB, false))
  19. @(private)
  20. _init_vtable :: #force_inline proc() -> ^_ctx.Hash_Context {
  21. ctx := _ctx._init_vtable()
  22. when USE_BOTAN_LIB {
  23. use_botan()
  24. } else {
  25. _assign_hash_vtable(ctx)
  26. }
  27. return ctx
  28. }
  29. @(private)
  30. _assign_hash_vtable :: #force_inline proc(ctx: ^_ctx.Hash_Context) {
  31. ctx.hash_bytes_32 = hash_bytes_odin_32
  32. ctx.hash_file_32 = hash_file_odin_32
  33. ctx.hash_stream_32 = hash_stream_odin_32
  34. ctx.hash_bytes_64 = hash_bytes_odin_64
  35. ctx.hash_file_64 = hash_file_odin_64
  36. ctx.hash_stream_64 = hash_stream_odin_64
  37. ctx.init = _init_odin
  38. ctx.update = _update_odin
  39. ctx.final = _final_odin
  40. }
  41. _hash_impl := _init_vtable()
  42. // use_botan assigns the internal vtable of the hash context to use the Botan bindings
  43. use_botan :: #force_inline proc() {
  44. botan.assign_hash_vtable(_hash_impl, botan.HASH_STREEBOG)
  45. }
  46. // use_odin assigns the internal vtable of the hash context to use the Odin implementation
  47. use_odin :: #force_inline proc() {
  48. _assign_hash_vtable(_hash_impl)
  49. }
  50. @(private)
  51. _create_streebog_ctx :: #force_inline proc(is256: bool) {
  52. ctx: Streebog_Context
  53. ctx.is256 = is256
  54. _hash_impl.internal_ctx = ctx
  55. _hash_impl.hash_size = is256 ? ._32 : ._64
  56. }
  57. /*
  58. High level API
  59. */
  60. // hash_string_256 will hash the given input and return the
  61. // computed hash
  62. hash_string_256 :: proc(data: string) -> [32]byte {
  63. return hash_bytes_256(transmute([]byte)(data))
  64. }
  65. // hash_bytes_256 will hash the given input and return the
  66. // computed hash
  67. hash_bytes_256 :: proc(data: []byte) -> [32]byte {
  68. _create_streebog_ctx(true)
  69. return _hash_impl->hash_bytes_32(data)
  70. }
  71. // hash_stream_256 will read the stream in chunks and compute a
  72. // hash from its contents
  73. hash_stream_256 :: proc(s: io.Stream) -> ([32]byte, bool) {
  74. _create_streebog_ctx(true)
  75. return _hash_impl->hash_stream_32(s)
  76. }
  77. // hash_file_256 will read the file provided by the given handle
  78. // and compute a hash
  79. hash_file_256 :: proc(hd: os.Handle, load_at_once := false) -> ([32]byte, bool) {
  80. _create_streebog_ctx(true)
  81. return _hash_impl->hash_file_32(hd, load_at_once)
  82. }
  83. hash_256 :: proc {
  84. hash_stream_256,
  85. hash_file_256,
  86. hash_bytes_256,
  87. hash_string_256,
  88. }
  89. // hash_string_512 will hash the given input and return the
  90. // computed hash
  91. hash_string_512 :: proc(data: string) -> [64]byte {
  92. return hash_bytes_512(transmute([]byte)(data))
  93. }
  94. // hash_bytes_512 will hash the given input and return the
  95. // computed hash
  96. hash_bytes_512 :: proc(data: []byte) -> [64]byte {
  97. _create_streebog_ctx(false)
  98. return _hash_impl->hash_bytes_64(data)
  99. }
  100. // hash_stream_512 will read the stream in chunks and compute a
  101. // hash from its contents
  102. hash_stream_512 :: proc(s: io.Stream) -> ([64]byte, bool) {
  103. _create_streebog_ctx(false)
  104. return _hash_impl->hash_stream_64(s)
  105. }
  106. // hash_file_512 will read the file provided by the given handle
  107. // and compute a hash
  108. hash_file_512 :: proc(hd: os.Handle, load_at_once := false) -> ([64]byte, bool) {
  109. _create_streebog_ctx(false)
  110. return _hash_impl->hash_file_64(hd, load_at_once)
  111. }
  112. hash_512 :: proc {
  113. hash_stream_512,
  114. hash_file_512,
  115. hash_bytes_512,
  116. hash_string_512,
  117. }
  118. /*
  119. Low level API
  120. */
  121. init :: proc(ctx: ^_ctx.Hash_Context) {
  122. _hash_impl->init()
  123. }
  124. update :: proc(ctx: ^_ctx.Hash_Context, data: []byte) {
  125. _hash_impl->update(data)
  126. }
  127. final :: proc(ctx: ^_ctx.Hash_Context, hash: []byte) {
  128. _hash_impl->final(hash)
  129. }
  130. hash_bytes_odin_32 :: #force_inline proc(ctx: ^_ctx.Hash_Context, data: []byte) -> [32]byte {
  131. hash: [32]byte
  132. if c, ok := ctx.internal_ctx.(Streebog_Context); ok {
  133. init_odin(&c)
  134. update_odin(&c, data)
  135. final_odin(&c, hash[:])
  136. }
  137. return hash
  138. }
  139. hash_stream_odin_32 :: #force_inline proc(ctx: ^_ctx.Hash_Context, fs: io.Stream) -> ([32]byte, bool) {
  140. hash: [32]byte
  141. if c, ok := ctx.internal_ctx.(Streebog_Context); ok {
  142. init_odin(&c)
  143. buf := make([]byte, 512)
  144. defer delete(buf)
  145. read := 1
  146. for read > 0 {
  147. read, _ = fs->impl_read(buf)
  148. if read > 0 {
  149. update_odin(&c, buf[:read])
  150. }
  151. }
  152. final_odin(&c, hash[:])
  153. return hash, true
  154. } else {
  155. return hash, false
  156. }
  157. }
  158. hash_file_odin_32 :: #force_inline proc(ctx: ^_ctx.Hash_Context, hd: os.Handle, load_at_once := false) -> ([32]byte, bool) {
  159. if !load_at_once {
  160. return hash_stream_odin_32(ctx, os.stream_from_handle(hd))
  161. } else {
  162. if buf, ok := os.read_entire_file(hd); ok {
  163. return hash_bytes_odin_32(ctx, buf[:]), ok
  164. }
  165. }
  166. return [32]byte{}, false
  167. }
  168. hash_bytes_odin_64 :: #force_inline proc(ctx: ^_ctx.Hash_Context, data: []byte) -> [64]byte {
  169. hash: [64]byte
  170. if c, ok := ctx.internal_ctx.(Streebog_Context); ok {
  171. init_odin(&c)
  172. update_odin(&c, data)
  173. final_odin(&c, hash[:])
  174. }
  175. return hash
  176. }
  177. hash_stream_odin_64 :: #force_inline proc(ctx: ^_ctx.Hash_Context, fs: io.Stream) -> ([64]byte, bool) {
  178. hash: [64]byte
  179. if c, ok := ctx.internal_ctx.(Streebog_Context); ok {
  180. init_odin(&c)
  181. buf := make([]byte, 512)
  182. defer delete(buf)
  183. read := 1
  184. for read > 0 {
  185. read, _ = fs->impl_read(buf)
  186. if read > 0 {
  187. update_odin(&c, buf[:read])
  188. }
  189. }
  190. final_odin(&c, hash[:])
  191. return hash, true
  192. } else {
  193. return hash, false
  194. }
  195. }
  196. hash_file_odin_64 :: #force_inline proc(ctx: ^_ctx.Hash_Context, hd: os.Handle, load_at_once := false) -> ([64]byte, bool) {
  197. if !load_at_once {
  198. return hash_stream_odin_64(ctx, os.stream_from_handle(hd))
  199. } else {
  200. if buf, ok := os.read_entire_file(hd); ok {
  201. return hash_bytes_odin_64(ctx, buf[:]), ok
  202. }
  203. }
  204. return [64]byte{}, false
  205. }
  206. @(private)
  207. _init_odin :: #force_inline proc(ctx: ^_ctx.Hash_Context) {
  208. _create_streebog_ctx(ctx.hash_size == ._32)
  209. if c, ok := ctx.internal_ctx.(Streebog_Context); ok {
  210. init_odin(&c)
  211. }
  212. }
  213. @(private)
  214. _update_odin :: #force_inline proc(ctx: ^_ctx.Hash_Context, data: []byte) {
  215. if c, ok := ctx.internal_ctx.(Streebog_Context); ok {
  216. update_odin(&c, data)
  217. }
  218. }
  219. @(private)
  220. _final_odin :: #force_inline proc(ctx: ^_ctx.Hash_Context, hash: []byte) {
  221. if c, ok := ctx.internal_ctx.(Streebog_Context); ok {
  222. final_odin(&c, hash)
  223. }
  224. }
  225. /*
  226. Streebog implementation
  227. */
  228. PI := [256]byte {
  229. 252, 238, 221, 17, 207, 110, 49, 22, 251, 196, 250, 218, 35, 197, 4, 77,
  230. 233, 119, 240, 219, 147, 46, 153, 186, 23, 54, 241, 187, 20, 205, 95, 193,
  231. 249, 24, 101, 90, 226, 92, 239, 33, 129, 28, 60, 66, 139, 1, 142, 79,
  232. 5, 132, 2, 174, 227, 106, 143, 160, 6, 11, 237, 152, 127, 212, 211, 31,
  233. 235, 52, 44, 81, 234, 200, 72, 171, 242, 42, 104, 162, 253, 58, 206, 204,
  234. 181, 112, 14, 86, 8, 12, 118, 18, 191, 114, 19, 71, 156, 183, 93, 135,
  235. 21, 161, 150, 41, 16, 123, 154, 199, 243, 145, 120, 111, 157, 158, 178, 177,
  236. 50, 117, 25, 61, 255, 53, 138, 126, 109, 84, 198, 128, 195, 189, 13, 87,
  237. 223, 245, 36, 169, 62, 168, 67, 201, 215, 121, 214, 246, 124, 34, 185, 3,
  238. 224, 15, 236, 222, 122, 148, 176, 188, 220, 232, 40, 80, 78, 51, 10, 74,
  239. 167, 151, 96, 115, 30, 0, 98, 68, 26, 184, 56, 130, 100, 159, 38, 65,
  240. 173, 69, 70, 146, 39, 94, 85, 47, 140, 163, 165, 125, 105, 213, 149, 59,
  241. 7, 88, 179, 64, 134, 172, 29, 247, 48, 55, 107, 228, 136, 217, 231, 137,
  242. 225, 27, 131, 73, 76, 63, 248, 254, 141, 83, 170, 144, 202, 216, 133, 97,
  243. 32, 113, 103, 164, 45, 43, 9, 91, 203, 155, 37, 208, 190, 229, 108, 82,
  244. 89, 166, 116, 210, 230, 244, 180, 192, 209, 102, 175, 194, 57, 75, 99, 182,
  245. }
  246. TAU := [64]byte {
  247. 0, 8, 16, 24, 32, 40, 48, 56,
  248. 1, 9, 17, 25, 33, 41, 49, 57,
  249. 2, 10, 18, 26, 34, 42, 50, 58,
  250. 3, 11, 19, 27, 35, 43, 51, 59,
  251. 4, 12, 20, 28, 36, 44, 52, 60,
  252. 5, 13, 21, 29, 37, 45, 53, 61,
  253. 6, 14, 22, 30, 38, 46, 54, 62,
  254. 7, 15, 23, 31, 39, 47, 55, 63,
  255. }
  256. STREEBOG_A := [64]u64 {
  257. 0x8e20faa72ba0b470, 0x47107ddd9b505a38, 0xad08b0e0c3282d1c, 0xd8045870ef14980e,
  258. 0x6c022c38f90a4c07, 0x3601161cf205268d, 0x1b8e0b0e798c13c8, 0x83478b07b2468764,
  259. 0xa011d380818e8f40, 0x5086e740ce47c920, 0x2843fd2067adea10, 0x14aff010bdd87508,
  260. 0x0ad97808d06cb404, 0x05e23c0468365a02, 0x8c711e02341b2d01, 0x46b60f011a83988e,
  261. 0x90dab52a387ae76f, 0x486dd4151c3dfdb9, 0x24b86a840e90f0d2, 0x125c354207487869,
  262. 0x092e94218d243cba, 0x8a174a9ec8121e5d, 0x4585254f64090fa0, 0xaccc9ca9328a8950,
  263. 0x9d4df05d5f661451, 0xc0a878a0a1330aa6, 0x60543c50de970553, 0x302a1e286fc58ca7,
  264. 0x18150f14b9ec46dd, 0x0c84890ad27623e0, 0x0642ca05693b9f70, 0x0321658cba93c138,
  265. 0x86275df09ce8aaa8, 0x439da0784e745554, 0xafc0503c273aa42a, 0xd960281e9d1d5215,
  266. 0xe230140fc0802984, 0x71180a8960409a42, 0xb60c05ca30204d21, 0x5b068c651810a89e,
  267. 0x456c34887a3805b9, 0xac361a443d1c8cd2, 0x561b0d22900e4669, 0x2b838811480723ba,
  268. 0x9bcf4486248d9f5d, 0xc3e9224312c8c1a0, 0xeffa11af0964ee50, 0xf97d86d98a327728,
  269. 0xe4fa2054a80b329c, 0x727d102a548b194e, 0x39b008152acb8227, 0x9258048415eb419d,
  270. 0x492c024284fbaec0, 0xaa16012142f35760, 0x550b8e9e21f7a530, 0xa48b474f9ef5dc18,
  271. 0x70a6a56e2440598e, 0x3853dc371220a247, 0x1ca76e95091051ad, 0x0edd37c48a08a6d8,
  272. 0x07e095624504536c, 0x8d70c431ac02a736, 0xc83862965601dd1b, 0x641c314b2b8ee083,
  273. }
  274. STREEBOG_C := [12][64]byte {
  275. {
  276. 0x07, 0x45, 0xa6, 0xf2, 0x59, 0x65, 0x80, 0xdd,
  277. 0x23, 0x4d, 0x74, 0xcc, 0x36, 0x74, 0x76, 0x05,
  278. 0x15, 0xd3, 0x60, 0xa4, 0x08, 0x2a, 0x42, 0xa2,
  279. 0x01, 0x69, 0x67, 0x92, 0x91, 0xe0, 0x7c, 0x4b,
  280. 0xfc, 0xc4, 0x85, 0x75, 0x8d, 0xb8, 0x4e, 0x71,
  281. 0x16, 0xd0, 0x45, 0x2e, 0x43, 0x76, 0x6a, 0x2f,
  282. 0x1f, 0x7c, 0x65, 0xc0, 0x81, 0x2f, 0xcb, 0xeb,
  283. 0xe9, 0xda, 0xca, 0x1e, 0xda, 0x5b, 0x08, 0xb1,
  284. },
  285. {
  286. 0xb7, 0x9b, 0xb1, 0x21, 0x70, 0x04, 0x79, 0xe6,
  287. 0x56, 0xcd, 0xcb, 0xd7, 0x1b, 0xa2, 0xdd, 0x55,
  288. 0xca, 0xa7, 0x0a, 0xdb, 0xc2, 0x61, 0xb5, 0x5c,
  289. 0x58, 0x99, 0xd6, 0x12, 0x6b, 0x17, 0xb5, 0x9a,
  290. 0x31, 0x01, 0xb5, 0x16, 0x0f, 0x5e, 0xd5, 0x61,
  291. 0x98, 0x2b, 0x23, 0x0a, 0x72, 0xea, 0xfe, 0xf3,
  292. 0xd7, 0xb5, 0x70, 0x0f, 0x46, 0x9d, 0xe3, 0x4f,
  293. 0x1a, 0x2f, 0x9d, 0xa9, 0x8a, 0xb5, 0xa3, 0x6f,
  294. },
  295. {
  296. 0xb2, 0x0a, 0xba, 0x0a, 0xf5, 0x96, 0x1e, 0x99,
  297. 0x31, 0xdb, 0x7a, 0x86, 0x43, 0xf4, 0xb6, 0xc2,
  298. 0x09, 0xdb, 0x62, 0x60, 0x37, 0x3a, 0xc9, 0xc1,
  299. 0xb1, 0x9e, 0x35, 0x90, 0xe4, 0x0f, 0xe2, 0xd3,
  300. 0x7b, 0x7b, 0x29, 0xb1, 0x14, 0x75, 0xea, 0xf2,
  301. 0x8b, 0x1f, 0x9c, 0x52, 0x5f, 0x5e, 0xf1, 0x06,
  302. 0x35, 0x84, 0x3d, 0x6a, 0x28, 0xfc, 0x39, 0x0a,
  303. 0xc7, 0x2f, 0xce, 0x2b, 0xac, 0xdc, 0x74, 0xf5,
  304. },
  305. {
  306. 0x2e, 0xd1, 0xe3, 0x84, 0xbc, 0xbe, 0x0c, 0x22,
  307. 0xf1, 0x37, 0xe8, 0x93, 0xa1, 0xea, 0x53, 0x34,
  308. 0xbe, 0x03, 0x52, 0x93, 0x33, 0x13, 0xb7, 0xd8,
  309. 0x75, 0xd6, 0x03, 0xed, 0x82, 0x2c, 0xd7, 0xa9,
  310. 0x3f, 0x35, 0x5e, 0x68, 0xad, 0x1c, 0x72, 0x9d,
  311. 0x7d, 0x3c, 0x5c, 0x33, 0x7e, 0x85, 0x8e, 0x48,
  312. 0xdd, 0xe4, 0x71, 0x5d, 0xa0, 0xe1, 0x48, 0xf9,
  313. 0xd2, 0x66, 0x15, 0xe8, 0xb3, 0xdf, 0x1f, 0xef,
  314. },
  315. {
  316. 0x57, 0xfe, 0x6c, 0x7c, 0xfd, 0x58, 0x17, 0x60,
  317. 0xf5, 0x63, 0xea, 0xa9, 0x7e, 0xa2, 0x56, 0x7a,
  318. 0x16, 0x1a, 0x27, 0x23, 0xb7, 0x00, 0xff, 0xdf,
  319. 0xa3, 0xf5, 0x3a, 0x25, 0x47, 0x17, 0xcd, 0xbf,
  320. 0xbd, 0xff, 0x0f, 0x80, 0xd7, 0x35, 0x9e, 0x35,
  321. 0x4a, 0x10, 0x86, 0x16, 0x1f, 0x1c, 0x15, 0x7f,
  322. 0x63, 0x23, 0xa9, 0x6c, 0x0c, 0x41, 0x3f, 0x9a,
  323. 0x99, 0x47, 0x47, 0xad, 0xac, 0x6b, 0xea, 0x4b,
  324. },
  325. {
  326. 0x6e, 0x7d, 0x64, 0x46, 0x7a, 0x40, 0x68, 0xfa,
  327. 0x35, 0x4f, 0x90, 0x36, 0x72, 0xc5, 0x71, 0xbf,
  328. 0xb6, 0xc6, 0xbe, 0xc2, 0x66, 0x1f, 0xf2, 0x0a,
  329. 0xb4, 0xb7, 0x9a, 0x1c, 0xb7, 0xa6, 0xfa, 0xcf,
  330. 0xc6, 0x8e, 0xf0, 0x9a, 0xb4, 0x9a, 0x7f, 0x18,
  331. 0x6c, 0xa4, 0x42, 0x51, 0xf9, 0xc4, 0x66, 0x2d,
  332. 0xc0, 0x39, 0x30, 0x7a, 0x3b, 0xc3, 0xa4, 0x6f,
  333. 0xd9, 0xd3, 0x3a, 0x1d, 0xae, 0xae, 0x4f, 0xae,
  334. },
  335. {
  336. 0x93, 0xd4, 0x14, 0x3a, 0x4d, 0x56, 0x86, 0x88,
  337. 0xf3, 0x4a, 0x3c, 0xa2, 0x4c, 0x45, 0x17, 0x35,
  338. 0x04, 0x05, 0x4a, 0x28, 0x83, 0x69, 0x47, 0x06,
  339. 0x37, 0x2c, 0x82, 0x2d, 0xc5, 0xab, 0x92, 0x09,
  340. 0xc9, 0x93, 0x7a, 0x19, 0x33, 0x3e, 0x47, 0xd3,
  341. 0xc9, 0x87, 0xbf, 0xe6, 0xc7, 0xc6, 0x9e, 0x39,
  342. 0x54, 0x09, 0x24, 0xbf, 0xfe, 0x86, 0xac, 0x51,
  343. 0xec, 0xc5, 0xaa, 0xee, 0x16, 0x0e, 0xc7, 0xf4,
  344. },
  345. {
  346. 0x1e, 0xe7, 0x02, 0xbf, 0xd4, 0x0d, 0x7f, 0xa4,
  347. 0xd9, 0xa8, 0x51, 0x59, 0x35, 0xc2, 0xac, 0x36,
  348. 0x2f, 0xc4, 0xa5, 0xd1, 0x2b, 0x8d, 0xd1, 0x69,
  349. 0x90, 0x06, 0x9b, 0x92, 0xcb, 0x2b, 0x89, 0xf4,
  350. 0x9a, 0xc4, 0xdb, 0x4d, 0x3b, 0x44, 0xb4, 0x89,
  351. 0x1e, 0xde, 0x36, 0x9c, 0x71, 0xf8, 0xb7, 0x4e,
  352. 0x41, 0x41, 0x6e, 0x0c, 0x02, 0xaa, 0xe7, 0x03,
  353. 0xa7, 0xc9, 0x93, 0x4d, 0x42, 0x5b, 0x1f, 0x9b,
  354. },
  355. {
  356. 0xdb, 0x5a, 0x23, 0x83, 0x51, 0x44, 0x61, 0x72,
  357. 0x60, 0x2a, 0x1f, 0xcb, 0x92, 0xdc, 0x38, 0x0e,
  358. 0x54, 0x9c, 0x07, 0xa6, 0x9a, 0x8a, 0x2b, 0x7b,
  359. 0xb1, 0xce, 0xb2, 0xdb, 0x0b, 0x44, 0x0a, 0x80,
  360. 0x84, 0x09, 0x0d, 0xe0, 0xb7, 0x55, 0xd9, 0x3c,
  361. 0x24, 0x42, 0x89, 0x25, 0x1b, 0x3a, 0x7d, 0x3a,
  362. 0xde, 0x5f, 0x16, 0xec, 0xd8, 0x9a, 0x4c, 0x94,
  363. 0x9b, 0x22, 0x31, 0x16, 0x54, 0x5a, 0x8f, 0x37,
  364. },
  365. {
  366. 0xed, 0x9c, 0x45, 0x98, 0xfb, 0xc7, 0xb4, 0x74,
  367. 0xc3, 0xb6, 0x3b, 0x15, 0xd1, 0xfa, 0x98, 0x36,
  368. 0xf4, 0x52, 0x76, 0x3b, 0x30, 0x6c, 0x1e, 0x7a,
  369. 0x4b, 0x33, 0x69, 0xaf, 0x02, 0x67, 0xe7, 0x9f,
  370. 0x03, 0x61, 0x33, 0x1b, 0x8a, 0xe1, 0xff, 0x1f,
  371. 0xdb, 0x78, 0x8a, 0xff, 0x1c, 0xe7, 0x41, 0x89,
  372. 0xf3, 0xf3, 0xe4, 0xb2, 0x48, 0xe5, 0x2a, 0x38,
  373. 0x52, 0x6f, 0x05, 0x80, 0xa6, 0xde, 0xbe, 0xab,
  374. },
  375. {
  376. 0x1b, 0x2d, 0xf3, 0x81, 0xcd, 0xa4, 0xca, 0x6b,
  377. 0x5d, 0xd8, 0x6f, 0xc0, 0x4a, 0x59, 0xa2, 0xde,
  378. 0x98, 0x6e, 0x47, 0x7d, 0x1d, 0xcd, 0xba, 0xef,
  379. 0xca, 0xb9, 0x48, 0xea, 0xef, 0x71, 0x1d, 0x8a,
  380. 0x79, 0x66, 0x84, 0x14, 0x21, 0x80, 0x01, 0x20,
  381. 0x61, 0x07, 0xab, 0xeb, 0xbb, 0x6b, 0xfa, 0xd8,
  382. 0x94, 0xfe, 0x5a, 0x63, 0xcd, 0xc6, 0x02, 0x30,
  383. 0xfb, 0x89, 0xc8, 0xef, 0xd0, 0x9e, 0xcd, 0x7b,
  384. },
  385. {
  386. 0x20, 0xd7, 0x1b, 0xf1, 0x4a, 0x92, 0xbc, 0x48,
  387. 0x99, 0x1b, 0xb2, 0xd9, 0xd5, 0x17, 0xf4, 0xfa,
  388. 0x52, 0x28, 0xe1, 0x88, 0xaa, 0xa4, 0x1d, 0xe7,
  389. 0x86, 0xcc, 0x91, 0x18, 0x9d, 0xef, 0x80, 0x5d,
  390. 0x9b, 0x9f, 0x21, 0x30, 0xd4, 0x12, 0x20, 0xf8,
  391. 0x77, 0x1d, 0xdf, 0xbc, 0x32, 0x3c, 0xa4, 0xcd,
  392. 0x7a, 0xb1, 0x49, 0x04, 0xb0, 0x80, 0x13, 0xd2,
  393. 0xba, 0x31, 0x16, 0xf1, 0x67, 0xe7, 0x8e, 0x37,
  394. },
  395. }
  396. Streebog_Context :: struct {
  397. buffer: [64]byte,
  398. h: [64]byte,
  399. n: [64]byte,
  400. sigma: [64]byte,
  401. v_0: [64]byte,
  402. v_512: [64]byte,
  403. buf_size: u64,
  404. hash_size: int,
  405. is256: bool,
  406. }
  407. add_mod_512 :: proc(first_vector, second_vector, result_vector: []byte) {
  408. t: i32 = 0
  409. for i: i32 = 0; i < 64; i += 1 {
  410. t = i32(first_vector[i]) + i32(second_vector[i]) + (t >> 8)
  411. result_vector[i] = byte(t & 0xff)
  412. }
  413. }
  414. X :: #force_inline proc(a, k, out: []byte) {
  415. for i := 0; i < 64; i += 1 {
  416. out[i] = a[i] ~ k[i]
  417. }
  418. }
  419. S :: #force_inline proc(state: []byte) {
  420. t: [64]byte
  421. for i: i32 = 63; i >= 0; i -= 1 {
  422. t[i] = PI[state[i]]
  423. }
  424. copy(state, t[:])
  425. }
  426. P :: #force_inline proc(state: []byte) {
  427. t: [64]byte
  428. for i: i32 = 63; i >= 0; i -= 1 {
  429. t[i] = state[TAU[i]]
  430. }
  431. copy(state, t[:])
  432. }
  433. L :: #force_inline proc(state: []byte) {
  434. ins := util.cast_slice([]u64, state)
  435. out: [8]u64
  436. for i: i32 = 7; i >= 0; i -= 1 {
  437. for j: i32 = 63; j >= 0; j -= 1 {
  438. if (ins[i] >> u32(j)) & 1 != 0 {
  439. out[i] ~= STREEBOG_A[63 - j]
  440. }
  441. }
  442. }
  443. copy(state, util.cast_slice([]byte, out[:]))
  444. }
  445. E :: #force_inline proc(K, m, state: []byte) {
  446. X(m, K, state)
  447. for i: i32 = 0; i < 12; i += 1 {
  448. S(state)
  449. P(state)
  450. L(state)
  451. get_key(K, i)
  452. X(state, K, state)
  453. }
  454. }
  455. get_key :: #force_inline proc(K: []byte, i: i32) {
  456. X(K, STREEBOG_C[i][:], K)
  457. S(K)
  458. P(K)
  459. L(K)
  460. }
  461. G :: #force_inline proc(h, N, m: []byte) {
  462. t, K: [64]byte
  463. X(N, h, K[:])
  464. S(K[:])
  465. P(K[:])
  466. L(K[:])
  467. E(K[:], m, t[:])
  468. X(t[:], h, t[:])
  469. X(t[:], m, h)
  470. }
  471. stage2 :: proc(ctx: ^Streebog_Context, m: []byte) {
  472. G(ctx.h[:], ctx.n[:], m)
  473. add_mod_512(ctx.n[:], ctx.v_512[:], ctx.n[:])
  474. add_mod_512(ctx.sigma[:], m, ctx.sigma[:])
  475. }
  476. padding :: proc(ctx: ^Streebog_Context) {
  477. if ctx.buf_size < 64 {
  478. t: [64]byte
  479. copy(t[:], ctx.buffer[:int(ctx.buf_size)])
  480. t[ctx.buf_size] = 0x01
  481. copy(ctx.buffer[:], t[:])
  482. }
  483. }
  484. init_odin :: proc(ctx: ^Streebog_Context) {
  485. if ctx.is256 {
  486. ctx.hash_size = 256
  487. for _, i in ctx.h {
  488. ctx.h[i] = 0x01
  489. }
  490. } else {
  491. ctx.hash_size = 512
  492. }
  493. ctx.v_512[1] = 0x02
  494. }
  495. update_odin :: proc(ctx: ^Streebog_Context, data: []byte) {
  496. length := u64(len(data))
  497. chk_size: u64
  498. data := data
  499. for (length > 63) && (ctx.buf_size == 0) {
  500. stage2(ctx, data)
  501. data = data[64:]
  502. length -= 64
  503. }
  504. for length != 0 {
  505. chk_size = 64 - ctx.buf_size
  506. if chk_size > length {
  507. chk_size = length
  508. }
  509. copy(ctx.buffer[ctx.buf_size:], data[:chk_size])
  510. ctx.buf_size += chk_size
  511. length -= chk_size
  512. data = data[chk_size:]
  513. if ctx.buf_size == 64 {
  514. stage2(ctx, ctx.buffer[:])
  515. ctx.buf_size = 0
  516. }
  517. }
  518. }
  519. final_odin :: proc(ctx: ^Streebog_Context, hash: []byte) {
  520. t: [64]byte
  521. t[1] = byte((ctx.buf_size * 8) >> 8) & 0xff
  522. t[0] = byte((ctx.buf_size) * 8) & 0xff
  523. padding(ctx)
  524. G(ctx.h[:], ctx.n[:], ctx.buffer[:])
  525. add_mod_512(ctx.n[:], t[:], ctx.n[:])
  526. add_mod_512(ctx.sigma[:], ctx.buffer[:], ctx.sigma[:])
  527. G(ctx.h[:], ctx.v_0[:], ctx.n[:])
  528. G(ctx.h[:], ctx.v_0[:], ctx.sigma[:])
  529. if ctx.is256 {
  530. copy(hash[:], ctx.h[32:])
  531. } else {
  532. copy(hash[:], ctx.h[:])
  533. }
  534. }