|
| 1 | +/* $Id: sph_gost.h 216 2010-06-08 09:46:57Z tp $ */ |
| 2 | +/** |
| 3 | + * GOST interface. This is the interface for GOST R 12 with the |
| 4 | + * recommended parameters for SHA-3, with output lengths 256 |
| 5 | + * and 512 bits. |
| 6 | + * |
| 7 | + * ==========================(LICENSE BEGIN)============================ |
| 8 | + * |
| 9 | + * Copyright (c) 2007-2010 Projet RNRT SAPHIR |
| 10 | + * |
| 11 | + * Permission is hereby granted, free of charge, to any person obtaining |
| 12 | + * a copy of this software and associated documentation files (the |
| 13 | + * "Software"), to deal in the Software without restriction, including |
| 14 | + * without limitation the rights to use, copy, modify, merge, publish, |
| 15 | + * distribute, sublicense, and/or sell copies of the Software, and to |
| 16 | + * permit persons to whom the Software is furnished to do so, subject to |
| 17 | + * the following conditions: |
| 18 | + * |
| 19 | + * The above copyright notice and this permission notice shall be |
| 20 | + * included in all copies or substantial portions of the Software. |
| 21 | + * |
| 22 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 23 | + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 24 | + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 25 | + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 26 | + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 27 | + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 28 | + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 29 | + * |
| 30 | + * ===========================(LICENSE END)============================= |
| 31 | + * |
| 32 | + * @file sph_gost.h |
| 33 | + * @author Mish <mish@btchouse.com> |
| 34 | + */ |
| 35 | + |
| 36 | +#ifndef BITCORE_CRYPTO_GOST_STREEBOG_H |
| 37 | +#define BITCORE_CRYPTO_GOST_STREEBOG_H |
| 38 | + |
| 39 | +#ifdef __cplusplus |
| 40 | +extern "C"{ |
| 41 | +#endif |
| 42 | + |
| 43 | +#include <stddef.h> |
| 44 | +#include <crypto/sph_types.h> |
| 45 | + |
| 46 | +/** |
| 47 | + * Output size (in bits) for GOST-256. |
| 48 | + */ |
| 49 | +#define SPH_SIZE_gost256 256 |
| 50 | + |
| 51 | +/** |
| 52 | + * Output size (in bits) for GOST-512. |
| 53 | + */ |
| 54 | +#define SPH_SIZE_gost512 512 |
| 55 | + |
| 56 | +/** |
| 57 | + * This structure is a context for Keccak computations: it contains the |
| 58 | + * intermediate values and some data from the last entered block. Once a |
| 59 | + * GOST computation has been performed, the context can be reused for |
| 60 | + * another computation. |
| 61 | + * |
| 62 | + * The contents of this structure are private. A running GOST computation |
| 63 | + * can be cloned by copying the context (e.g. with a simple |
| 64 | + * <code>memcpy()</code>). |
| 65 | + */ |
| 66 | + |
| 67 | +/** |
| 68 | + * This structure is a context for Gost-256 computations. |
| 69 | + */ |
| 70 | + |
| 71 | +typedef struct { |
| 72 | +#ifndef DOXYGEN_IGNORE |
| 73 | + unsigned char buf[32]; /* first field, for alignment */ |
| 74 | + size_t ptr; |
| 75 | + sph_u32 V[3][8]; |
| 76 | +#endif |
| 77 | +} sph_gost256_context; |
| 78 | + |
| 79 | +/** |
| 80 | + * This structure is a context for Gost-512 computations. |
| 81 | + */ |
| 82 | +typedef struct { |
| 83 | +#ifndef DOXYGEN_IGNORE |
| 84 | + unsigned char buf[64]; /* first field, for alignment */ |
| 85 | + size_t ptr; |
| 86 | + sph_u32 V[5][8]; |
| 87 | +#endif |
| 88 | +} sph_gost512_context; |
| 89 | + |
| 90 | + |
| 91 | +/** |
| 92 | + * Initialize a GOST-256 context. This process performs no memory allocation. |
| 93 | + * |
| 94 | + * @param cc the GOST-256 context (pointer to a |
| 95 | + * <code>sph_gost256_context</code>) |
| 96 | + */ |
| 97 | +void sph_gost256_init(void *cc); |
| 98 | + |
| 99 | +/** |
| 100 | + * Process some data bytes. It is acceptable that <code>len</code> is zero |
| 101 | + * (in which case this function does nothing). |
| 102 | + * |
| 103 | + * @param cc the Gost-256 context |
| 104 | + * @param data the input data |
| 105 | + * @param len the input data length (in bytes) |
| 106 | + */ |
| 107 | +void sph_gost256(void *cc, const void *data, size_t len); |
| 108 | + |
| 109 | +/** |
| 110 | + * Terminate the current GOST-256 computation and output the result into |
| 111 | + * the provided buffer. The destination buffer must be wide enough to |
| 112 | + * accomodate the result (32 bytes). The context is automatically |
| 113 | + * reinitialized. |
| 114 | + * |
| 115 | + * @param cc the GOST-256 context |
| 116 | + * @param dst the destination buffer |
| 117 | + */ |
| 118 | +void sph_gost256_close(void *cc, void *dst); |
| 119 | + |
| 120 | +/** |
| 121 | + * Add a few additional bits (0 to 7) to the current computation, then |
| 122 | + * terminate it and output the result in the provided buffer, which must |
| 123 | + * be wide enough to accomodate the result (32 bytes). If bit number i |
| 124 | + * in <code>ub</code> has value 2^i, then the extra bits are those |
| 125 | + * numbered 7 downto 8-n (this is the big-endian convention at the byte |
| 126 | + * level). The context is automatically reinitialized. |
| 127 | + * |
| 128 | + * @param cc the GOST-256 context |
| 129 | + * @param ub the extra bits |
| 130 | + * @param n the number of extra bits (0 to 7) |
| 131 | + * @param dst the destination buffer |
| 132 | + */ |
| 133 | +void sph_gost256_addbits_and_close( |
| 134 | + void *cc, unsigned ub, unsigned n, void *dst); |
| 135 | + |
| 136 | +/** |
| 137 | + * Initialize a Gost-512 context. This process performs no memory allocation. |
| 138 | + * |
| 139 | + * @param cc the GOST-512 context (pointer to a |
| 140 | + * <code>sph_gost512_context</code>) |
| 141 | + */ |
| 142 | +void sph_gost512_init(void *cc); |
| 143 | + |
| 144 | +/** |
| 145 | + * Process some data bytes. It is acceptable that <code>len</code> is zero |
| 146 | + * (in which case this function does nothing). |
| 147 | + * |
| 148 | + * @param cc the GOST-512 context |
| 149 | + * @param data the input data |
| 150 | + * @param len the input data length (in bytes) |
| 151 | + */ |
| 152 | +void sph_gost512(void *cc, const void *data, size_t len); |
| 153 | + |
| 154 | +/** |
| 155 | + * Terminate the current GOST-512 computation and output the result into |
| 156 | + * the provided buffer. The destination buffer must be wide enough to |
| 157 | + * accomodate the result (64 bytes). The context is automatically |
| 158 | + * reinitialized. |
| 159 | + * |
| 160 | + * @param cc the GOST-512 context |
| 161 | + * @param dst the destination buffer |
| 162 | + */ |
| 163 | +void sph_gost512_close(void *cc, void *dst); |
| 164 | + |
| 165 | +/** |
| 166 | + * Add a few additional bits (0 to 7) to the current computation, then |
| 167 | + * terminate it and output the result in the provided buffer, which must |
| 168 | + * be wide enough to accomodate the result (64 bytes). If bit number i |
| 169 | + * in <code>ub</code> has value 2^i, then the extra bits are those |
| 170 | + * numbered 7 downto 8-n (this is the big-endian convention at the byte |
| 171 | + * level). The context is automatically reinitialized. |
| 172 | + * |
| 173 | + * @param cc the GOST-512 context |
| 174 | + * @param ub the extra bits |
| 175 | + * @param n the number of extra bits (0 to 7) |
| 176 | + * @param dst the destination buffer |
| 177 | + */ |
| 178 | +void sph_gost512_addbits_and_close( |
| 179 | + void *cc, unsigned ub, unsigned n, void *dst); |
| 180 | + |
| 181 | +#ifdef __cplusplus |
| 182 | +} |
| 183 | +#endif |
| 184 | + |
| 185 | +#endif // BITCORE_CRYPTO_GOST_STREEBOG_H |
0 commit comments