1
0

gost.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /* $OpenBSD: gost.h,v 1.4 2022/07/12 14:42:49 kn Exp $ */
  2. /*
  3. * Copyright (c) 2014 Dmitry Eremin-Solenikov <[email protected]>
  4. * Copyright (c) 2005-2006 Cryptocom LTD
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. *
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in
  15. * the documentation and/or other materials provided with the
  16. * distribution.
  17. *
  18. * 3. All advertising materials mentioning features or use of this
  19. * software must display the following acknowledgment:
  20. * "This product includes software developed by the OpenSSL Project
  21. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  22. *
  23. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  24. * endorse or promote products derived from this software without
  25. * prior written permission. For written permission, please contact
  26. * [email protected].
  27. *
  28. * 5. Products derived from this software may not be called "OpenSSL"
  29. * nor may "OpenSSL" appear in their names without prior written
  30. * permission of the OpenSSL Project.
  31. *
  32. * 6. Redistributions of any form whatsoever must retain the following
  33. * acknowledgment:
  34. * "This product includes software developed by the OpenSSL Project
  35. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  36. *
  37. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  38. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  39. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  40. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  41. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  42. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  43. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  44. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  45. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  46. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  47. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  48. * OF THE POSSIBILITY OF SUCH DAMAGE.
  49. * ====================================================================
  50. */
  51. #ifndef HEADER_GOST_H
  52. #define HEADER_GOST_H
  53. #include <openssl/opensslconf.h>
  54. #ifdef OPENSSL_NO_GOST
  55. #error GOST is disabled.
  56. #endif
  57. #include <openssl/asn1t.h>
  58. #include <openssl/ec.h>
  59. #ifdef __cplusplus
  60. extern "C" {
  61. #endif
  62. typedef struct gost2814789_key_st {
  63. unsigned int key[8];
  64. unsigned int k87[256],k65[256],k43[256],k21[256];
  65. unsigned int count;
  66. unsigned key_meshing : 1;
  67. } GOST2814789_KEY;
  68. int Gost2814789_set_sbox(GOST2814789_KEY *key, int nid);
  69. int Gost2814789_set_key(GOST2814789_KEY *key,
  70. const unsigned char *userKey, const int bits);
  71. void Gost2814789_ecb_encrypt(const unsigned char *in, unsigned char *out,
  72. GOST2814789_KEY *key, const int enc);
  73. void Gost2814789_cfb64_encrypt(const unsigned char *in, unsigned char *out,
  74. size_t length, GOST2814789_KEY *key,
  75. unsigned char *ivec, int *num, const int enc);
  76. void Gost2814789_cnt_encrypt(const unsigned char *in, unsigned char *out,
  77. size_t length, GOST2814789_KEY *key,
  78. unsigned char *ivec, unsigned char *cnt_buf, int *num);
  79. typedef struct {
  80. ASN1_OCTET_STRING *iv;
  81. ASN1_OBJECT *enc_param_set;
  82. } GOST_CIPHER_PARAMS;
  83. GOST_CIPHER_PARAMS *GOST_CIPHER_PARAMS_new(void);
  84. void GOST_CIPHER_PARAMS_free(GOST_CIPHER_PARAMS *a);
  85. GOST_CIPHER_PARAMS *d2i_GOST_CIPHER_PARAMS(GOST_CIPHER_PARAMS **a, const unsigned char **in, long len);
  86. int i2d_GOST_CIPHER_PARAMS(GOST_CIPHER_PARAMS *a, unsigned char **out);
  87. extern const ASN1_ITEM GOST_CIPHER_PARAMS_it;
  88. #define GOST2814789IMIT_LENGTH 4
  89. #define GOST2814789IMIT_CBLOCK 8
  90. #define GOST2814789IMIT_LONG unsigned int
  91. typedef struct GOST2814789IMITstate_st {
  92. GOST2814789IMIT_LONG Nl, Nh;
  93. unsigned char data[GOST2814789IMIT_CBLOCK];
  94. unsigned int num;
  95. GOST2814789_KEY cipher;
  96. unsigned char mac[GOST2814789IMIT_CBLOCK];
  97. } GOST2814789IMIT_CTX;
  98. /* Note, also removed second parameter and removed dctx->cipher setting */
  99. int GOST2814789IMIT_Init(GOST2814789IMIT_CTX *c, int nid);
  100. int GOST2814789IMIT_Update(GOST2814789IMIT_CTX *c, const void *data, size_t len);
  101. int GOST2814789IMIT_Final(unsigned char *md, GOST2814789IMIT_CTX *c);
  102. void GOST2814789IMIT_Transform(GOST2814789IMIT_CTX *c, const unsigned char *data);
  103. unsigned char *GOST2814789IMIT(const unsigned char *d, size_t n,
  104. unsigned char *md, int nid,
  105. const unsigned char *key, const unsigned char *iv);
  106. #define GOSTR341194_LONG unsigned int
  107. #define GOSTR341194_LENGTH 32
  108. #define GOSTR341194_CBLOCK 32
  109. #define GOSTR341194_LBLOCK (GOSTR341194_CBLOCK/4)
  110. typedef struct GOSTR341194state_st {
  111. GOSTR341194_LONG Nl, Nh;
  112. GOSTR341194_LONG data[GOSTR341194_LBLOCK];
  113. unsigned int num;
  114. GOST2814789_KEY cipher;
  115. unsigned char H[GOSTR341194_CBLOCK];
  116. unsigned char S[GOSTR341194_CBLOCK];
  117. } GOSTR341194_CTX;
  118. /* Note, also removed second parameter and removed dctx->cipher setting */
  119. int GOSTR341194_Init(GOSTR341194_CTX *c, int nid);
  120. int GOSTR341194_Update(GOSTR341194_CTX *c, const void *data, size_t len);
  121. int GOSTR341194_Final(unsigned char *md, GOSTR341194_CTX *c);
  122. void GOSTR341194_Transform(GOSTR341194_CTX *c, const unsigned char *data);
  123. unsigned char *GOSTR341194(const unsigned char *d, size_t n,unsigned char *md, int nid);
  124. #if defined(_LP64)
  125. #define STREEBOG_LONG64 unsigned long
  126. #define U64(C) C##UL
  127. #else
  128. #define STREEBOG_LONG64 unsigned long long
  129. #define U64(C) C##ULL
  130. #endif
  131. #define STREEBOG_LBLOCK 8
  132. #define STREEBOG_CBLOCK 64
  133. #define STREEBOG256_LENGTH 32
  134. #define STREEBOG512_LENGTH 64
  135. typedef struct STREEBOGstate_st {
  136. STREEBOG_LONG64 data[STREEBOG_LBLOCK];
  137. unsigned int num;
  138. unsigned int md_len;
  139. STREEBOG_LONG64 h[STREEBOG_LBLOCK];
  140. STREEBOG_LONG64 N[STREEBOG_LBLOCK];
  141. STREEBOG_LONG64 Sigma[STREEBOG_LBLOCK];
  142. } STREEBOG_CTX;
  143. int STREEBOG256_Init(STREEBOG_CTX *c);
  144. int STREEBOG256_Update(STREEBOG_CTX *c, const void *data, size_t len);
  145. int STREEBOG256_Final(unsigned char *md, STREEBOG_CTX *c);
  146. void STREEBOG256_Transform(STREEBOG_CTX *c, const unsigned char *data);
  147. unsigned char *STREEBOG256(const unsigned char *d, size_t n,unsigned char *md);
  148. int STREEBOG512_Init(STREEBOG_CTX *c);
  149. int STREEBOG512_Update(STREEBOG_CTX *c, const void *data, size_t len);
  150. int STREEBOG512_Final(unsigned char *md, STREEBOG_CTX *c);
  151. void STREEBOG512_Transform(STREEBOG_CTX *c, const unsigned char *data);
  152. unsigned char *STREEBOG512(const unsigned char *d, size_t n,unsigned char *md);
  153. typedef struct gost_key_st GOST_KEY;
  154. GOST_KEY *GOST_KEY_new(void);
  155. void GOST_KEY_free(GOST_KEY * r);
  156. int GOST_KEY_check_key(const GOST_KEY * eckey);
  157. int GOST_KEY_set_public_key_affine_coordinates(GOST_KEY * key, BIGNUM * x, BIGNUM * y);
  158. const EC_GROUP * GOST_KEY_get0_group(const GOST_KEY * key);
  159. int GOST_KEY_set_group(GOST_KEY * key, const EC_GROUP * group);
  160. int GOST_KEY_get_digest(const GOST_KEY * key);
  161. int GOST_KEY_set_digest(GOST_KEY * key, int digest_nid);
  162. const BIGNUM * GOST_KEY_get0_private_key(const GOST_KEY * key);
  163. int GOST_KEY_set_private_key(GOST_KEY * key, const BIGNUM * priv_key);
  164. const EC_POINT * GOST_KEY_get0_public_key(const GOST_KEY * key);
  165. int GOST_KEY_set_public_key(GOST_KEY * key, const EC_POINT * pub_key);
  166. size_t GOST_KEY_get_size(const GOST_KEY * r);
  167. /* Gost-specific pmeth control-function parameters */
  168. /* For GOST R34.10 parameters */
  169. #define EVP_PKEY_CTRL_GOST_PARAMSET (EVP_PKEY_ALG_CTRL+1)
  170. #define EVP_PKEY_CTRL_GOST_SIG_FORMAT (EVP_PKEY_ALG_CTRL+2)
  171. #define EVP_PKEY_CTRL_GOST_SET_DIGEST (EVP_PKEY_ALG_CTRL+3)
  172. #define EVP_PKEY_CTRL_GOST_GET_DIGEST (EVP_PKEY_ALG_CTRL+4)
  173. #define GOST_SIG_FORMAT_SR_BE 0
  174. #define GOST_SIG_FORMAT_RS_LE 1
  175. void ERR_load_GOST_strings(void);
  176. /* Error codes for the GOST functions. */
  177. /* Function codes. */
  178. #define GOST_F_DECODE_GOST01_ALGOR_PARAMS 104
  179. #define GOST_F_ENCODE_GOST01_ALGOR_PARAMS 105
  180. #define GOST_F_GOST2001_COMPUTE_PUBLIC 106
  181. #define GOST_F_GOST2001_DO_SIGN 107
  182. #define GOST_F_GOST2001_DO_VERIFY 108
  183. #define GOST_F_GOST2001_KEYGEN 109
  184. #define GOST_F_GOST89_GET_ASN1_PARAMETERS 102
  185. #define GOST_F_GOST89_SET_ASN1_PARAMETERS 103
  186. #define GOST_F_GOST_KEY_CHECK_KEY 124
  187. #define GOST_F_GOST_KEY_NEW 125
  188. #define GOST_F_GOST_KEY_SET_PUBLIC_KEY_AFFINE_COORDINATES 126
  189. #define GOST_F_PARAM_COPY_GOST01 110
  190. #define GOST_F_PARAM_DECODE_GOST01 111
  191. #define GOST_F_PKEY_GOST01_CTRL 116
  192. #define GOST_F_PKEY_GOST01_DECRYPT 112
  193. #define GOST_F_PKEY_GOST01_DERIVE 113
  194. #define GOST_F_PKEY_GOST01_ENCRYPT 114
  195. #define GOST_F_PKEY_GOST01_PARAMGEN 115
  196. #define GOST_F_PKEY_GOST01_SIGN 123
  197. #define GOST_F_PKEY_GOST_MAC_CTRL 100
  198. #define GOST_F_PKEY_GOST_MAC_KEYGEN 101
  199. #define GOST_F_PRIV_DECODE_GOST01 117
  200. #define GOST_F_PUB_DECODE_GOST01 118
  201. #define GOST_F_PUB_ENCODE_GOST01 119
  202. #define GOST_F_PUB_PRINT_GOST01 120
  203. #define GOST_F_UNPACK_SIGNATURE_CP 121
  204. #define GOST_F_UNPACK_SIGNATURE_LE 122
  205. /* Reason codes. */
  206. #define GOST_R_BAD_KEY_PARAMETERS_FORMAT 104
  207. #define GOST_R_BAD_PKEY_PARAMETERS_FORMAT 105
  208. #define GOST_R_CANNOT_PACK_EPHEMERAL_KEY 106
  209. #define GOST_R_CTRL_CALL_FAILED 107
  210. #define GOST_R_ERROR_COMPUTING_SHARED_KEY 108
  211. #define GOST_R_ERROR_PARSING_KEY_TRANSPORT_INFO 109
  212. #define GOST_R_INCOMPATIBLE_ALGORITHMS 110
  213. #define GOST_R_INCOMPATIBLE_PEER_KEY 111
  214. #define GOST_R_INVALID_DIGEST_TYPE 100
  215. #define GOST_R_INVALID_IV_LENGTH 103
  216. #define GOST_R_INVALID_MAC_KEY_LENGTH 101
  217. #define GOST_R_KEY_IS_NOT_INITIALIZED 112
  218. #define GOST_R_KEY_PARAMETERS_MISSING 113
  219. #define GOST_R_MAC_KEY_NOT_SET 102
  220. #define GOST_R_NO_PARAMETERS_SET 115
  221. #define GOST_R_NO_PEER_KEY 116
  222. #define GOST_R_NO_PRIVATE_PART_OF_NON_EPHEMERAL_KEYPAIR 117
  223. #define GOST_R_PUBLIC_KEY_UNDEFINED 118
  224. #define GOST_R_RANDOM_NUMBER_GENERATOR_FAILED 120
  225. #define GOST_R_SIGNATURE_MISMATCH 121
  226. #define GOST_R_SIGNATURE_PARTS_GREATER_THAN_Q 122
  227. #define GOST_R_UKM_NOT_SET 123
  228. #ifdef __cplusplus
  229. }
  230. #endif
  231. #endif