README.gost 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. GOST ENGINE
  2. This engine provides implementation of Russian cryptography standard.
  3. This is also an example of adding new cryptoalgorithms into OpenSSL
  4. without changing its core. If OpenSSL is compiled with dynamic engine
  5. support, new algorithms can be added even without recompilation of
  6. OpenSSL and applications which use it.
  7. ALGORITHMS SUPPORTED
  8. GOST R 34.10-94 and GOST R 34.10-2001 - digital signature algorithms.
  9. Also support key exchange based on public keys. See RFC 4357 for
  10. details of VKO key exchange algorithm. These algorithms use
  11. 256 bit private keys. Public keys are 1024 bit for 94 and 512 bit for
  12. 2001 (which is elliptic-curve based). Key exchange algorithms
  13. (VKO R 34.10) are supported on these keys too.
  14. GOST R 34.11-94 Message digest algorithm. 256-bit hash value
  15. GOST 28147-89 - Symmetric cipher with 256-bit key. Various modes are
  16. defined in the standard, but only CFB and CNT modes are implemented
  17. in the engine. To make statistical analysis more difficult, key
  18. meshing is supported (see RFC 4357).
  19. GOST 28147-89 MAC mode. Message authentication code. While most MAC
  20. algorithms out there are based on hash functions using HMAC
  21. algorithm, this algoritm is based on symmetric cipher.
  22. It has 256-bit symmetric key and only 32 bits of MAC value
  23. (while HMAC has same key size and value size).
  24. It is implemented as combination of EVP_PKEY type and EVP_MD type.
  25. USAGE OF THESE ALGORITHMS
  26. This engine is designed to allow usage of this algorithms in the
  27. high-level openssl functions, such as PKI, S/MIME and TLS.
  28. See RFC 4490 for S/MIME with GOST algorithms and RFC 4491 for PKI.
  29. TLS support is implemented according IETF
  30. draft-chudov-cryptopro-cptls-03.txt and is compatible with
  31. CryptoPro CSP 3.0 and 3.6 as well as with MagPro CSP.
  32. GOST ciphersuites implemented in CryptoPro CSP 2.0 are not supported
  33. because they use ciphersuite numbers used now by AES ciphersuites.
  34. To use the engine you have to load it via openssl configuration
  35. file. Applications should read openssl configuration file or provide
  36. their own means to load engines. Also, applications which operate with
  37. private keys, should use generic EVP_PKEY API instead of using RSA or
  38. other algorithm-specific API.
  39. CONFIGURATION FILE
  40. Configuration file should include following statement in the global
  41. section, i.e. before first bracketed section header (see config(5) for details)
  42. openssl_conf = openssl_def
  43. where openssl_def is name of the section in configuration file which
  44. describes global defaults.
  45. This section should contain following statement:
  46. [openssl_def]
  47. engines = engine_section
  48. which points to the section which describes list of the engines to be
  49. loaded. This section should contain:
  50. [engine_section]
  51. gost = gost_section
  52. And section which describes configuration of the engine should contain
  53. [gost_section]
  54. engine_id = gost
  55. dynamic_path = /usr/lib/ssl/engines/libgost.so
  56. default_algorithms = ALL
  57. CRYPT_PARAMS = id-Gost28147-89-CryptoPro-A-ParamSet
  58. Where engine_id parameter specifies name of engine (should be "gost").
  59. dynamic_path is a location of the loadable shared library implementing the
  60. engine. If the engine is compiled statically or is located in the OpenSSL
  61. engines directory, this line can be omitted.
  62. default_algorithms parameter specifies that all algorithms, provided by
  63. engine, should be used.
  64. The CRYPT_PARAMS parameter is engine-specific. It allows the user to choose
  65. between different parameter sets of symmetric cipher algorithm. RFC 4357
  66. specifies several parameters for the GOST 28147-89 algorithm, but OpenSSL
  67. doesn't provide user interface to choose one when encrypting. So use engine
  68. configuration parameter instead.
  69. Value of this parameter can be either short name, defined in OpenSSL
  70. obj_dat.h header file or numeric representation of OID, defined in RFC
  71. 4357.
  72. USAGE WITH COMMAND LINE openssl UTILITY
  73. 1. Generation of private key
  74. openssl genpkey -algorithm gost2001 -pkeyopt paramset:A -out seckey.pem
  75. Use -algorithm option to specify algorithm.
  76. Use -pkeyopt option to pass paramset to algorithm. The following paramsets
  77. are supported by
  78. gost94: 0,A,B,C,D,XA,XB,XC
  79. gost2001: 0,A,B,C,XA,XB
  80. You can also use numeric representation of OID as to destinate
  81. paramset.
  82. Paramsets starting with X are intended to use for key exchange keys.
  83. Paramsets without X are for digital signature keys.
  84. Paramset for both algorithms 0 is the test paramset which should be used
  85. only for test purposes.
  86. There are no algorithm-specific things with generation of certificate
  87. request once you have a private key.
  88. 2. Generation of certificate request along with private/public keypar
  89. openssl req -newkey gost2001 -pkeyopt paramset:A
  90. Syntax of -pkeyopt parameter is identical with genpkey command.
  91. You can also use oldstyle syntax -newkey gost2001:paramfile, but in
  92. this case you should create parameter file first.
  93. It can be created with
  94. openssl genpkey -genparam -algorithm gost2001 -pkeyopt paramset:A\
  95. -out paramfile.
  96. 3. S/MIME operations
  97. If you want to send encrypted mail using GOST algorithms, don't forget
  98. to specify -gost89 as encryption algorithm for OpenSSL smime command.
  99. While OpenSSL is clever enough to find out that GOST R 34.11-94 digest
  100. must be used for digital signing with GOST private key, it have no way
  101. to derive symmetric encryption algorithm from key exchange keys.
  102. 4. TLS operations
  103. OpenSSL supports all four ciphersuites defined in the IETF draft.
  104. Once you've loaded GOST key and certificate into your TLS server,
  105. ciphersuites which use GOST 28147-89 encryption are enabled.
  106. Ciphersuites with NULL encryption should be enabled explicitely if
  107. needed.
  108. GOST2001-GOST89-GOST89 Uses GOST R 34.10-2001 for auth and key exchange
  109. GOST 28147-89 for encryption and GOST 28147-89 MAC
  110. GOST94-GOST89-GOST89 Uses GOST R 34.10-94 for auth and key exchange
  111. GOST 28147-89 for encryption and GOST 28147-89 MAC
  112. GOST2001-NULL-GOST94 Uses GOST R 34.10-2001 for auth and key exchange,
  113. no encryption and HMAC, based on GOST R 34.11-94
  114. GOST94-NULL-GOST94 Uses GOST R 34.10-94 for auth and key exchange,
  115. no encryption and HMAC, based on GOST R 34.11-94
  116. Gost 94 and gost 2001 keys can be used simultaneously in the TLS server.
  117. RSA, DSA and EC keys can be used simultaneously with GOST keys, if
  118. server implementation supports loading more than two private
  119. key/certificate pairs. In this case ciphersuites which use any of loaded
  120. keys would be supported and clients can negotiate ones they wish.
  121. This allows creation of TLS servers which use GOST ciphersuites for
  122. Russian clients and RSA/DSA ciphersuites for foreign clients.
  123. 5. Calculation of digests and symmetric encryption
  124. OpenSSL provides specific commands (like sha1, aes etc) for calculation
  125. of digests and symmetric encryption. Since such commands cannot be
  126. added dynamically, no such commands are provided for GOST algorithms.
  127. Use generic commands 'dgst' and 'enc'.
  128. Calculation of GOST R 34.11-94 message digest
  129. openssl dgst -md_gost94 datafile
  130. Note that GOST R 34.11-94 specifies that digest value should be
  131. interpreted as little-endian number, but OpenSSL outputs just hex dump
  132. of digest value.
  133. So, to obtain correct digest value, such as produced by gostsum utility
  134. included in the engine distribution, bytes of output should be
  135. reversed.
  136. Calculation of HMAC based on GOST R 34.11-94
  137. openssl dgst -md_gost94 -mac hmac -macopt key:<32 bytes of key> datafile
  138. (or use hexkey if key contain NUL bytes)
  139. Calculation of GOST 28147 MAC
  140. openssl dgst -mac gost-mac -macopt key:<32 bytes of key> datafile
  141. Note absense of an option that specifies digest algorithm. gost-mac
  142. algorithm supports only one digest (which is actually part of
  143. implementation of this mac) and OpenSSL is clever enough to find out
  144. this.
  145. Encryption with GOST 28147 CFB mode
  146. openssl enc -gost89 -out encrypted-file -in plain-text-file -k <passphrase>
  147. Encryption with GOST 28147 CNT mode
  148. openssl enc -gost89-cnt -out encrypted-file -in plain-text-file -k <passphrase>
  149. 6. Encrypting private keys and PKCS12
  150. To produce PKCS12 files compatible with MagPro CSP, you need to use
  151. GOST algorithm for encryption of PKCS12 file and also GOST R 34.11-94
  152. hash to derive key from password.
  153. openssl pksc12 -export -inkey gost.pem -in gost_cert.pem -keypbe gost89\
  154. -certpbe gost89 -macalg md_gost94
  155. 7. Testing speed of symmetric ciphers.
  156. To test performance of GOST symmetric ciphers you should use -evp switch
  157. of the openssl speed command. Engine-provided ciphers couldn't be
  158. accessed by cipher-specific functions, only via generic evp interface
  159. openssl speed -evp gost89
  160. openssl speed -evp gost89-cnt
  161. PROGRAMMING INTERFACES DETAILS
  162. Applications never should access engine directly. They only use provided
  163. EVP_PKEY API. But there are some details, which should be taken into
  164. account.
  165. EVP provides two kinds of API for key exchange:
  166. 1. EVP_PKEY_encrypt/EVP_PKEY_decrypt functions, intended to use with
  167. RSA-like public key encryption algorithms
  168. 2. EVP_PKEY_derive, intended to use with Diffie-Hellman-like shared key
  169. computing algorithms.
  170. Although VKO R 34.10 algorithms, described in the RFC 4357 are
  171. definitely second case, engine provides BOTH API for GOST R 34.10 keys.
  172. EVP_PKEY_derive just invokes appropriate VKO algorithm and computes
  173. 256 bit shared key. VKO R 34.10-2001 requires 64 bits of random user key
  174. material (UKM). This UKM should be transmitted to other party, so it is
  175. not generated inside derive function.
  176. It should be set by EVP_PKEY_CTX_ctrl function using
  177. EVP_PKEY_CTRL_SET_IV command after call of EVP_PKEY_derive_init, but
  178. before EVP_PKEY_derive.
  179. unsigned char ukm[8];
  180. RAND_bytes(ukm,8);
  181. EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_DERIVE, 8, ukm)
  182. EVP_PKEY_encrypt encrypts provided session key with VKO shared key and
  183. packs it into GOST key transport structure, described in the RFC 4490.
  184. It typically uses ephemeral key pair to compute shared key and packs its
  185. public part along with encrypted key. So, for most cases use of
  186. EVP_PKEY_encrypt/EVP_PKEY_decrypt with GOST keys is almost same as with
  187. RSA.
  188. However, if peerkey field in the EVP_PKEY_CTX structure is set (using
  189. EVP_PKEY_derive_set_peerkey function) to EVP_PKEY structure which has private
  190. key and uses same parameters as the public key from which this EVP_PKEY_CTX is
  191. created, EVP_PKEY_encrypt will use this private key to compute shared key and
  192. set ephemeral key in the GOST_key_transport structure to NULL. In this case
  193. pkey and peerkey fields in the EVP_PKEY_CTX are used upside-down.
  194. If EVP_PKEY_decrypt encounters GOST_key_transport structure with NULL
  195. public key field, it tries to use peerkey field from the context to
  196. compute shared key. In this case peerkey field should really contain
  197. peer public key.
  198. Encrypt operation supports EVP_PKEY_CTRL_SET_IV operation as well.
  199. It can be used when some specific restriction on UKM are imposed by
  200. higher level protocol. For instance, description of GOST ciphersuites
  201. requires UKM to be derived from shared secret.
  202. If UKM is not set by this control command, encrypt operation would
  203. generate random UKM.
  204. This sources include implementation of GOST 28147-89 and GOST R 34.11-94
  205. which are completely indepentent from OpenSSL and can be used separately
  206. (files gost89.c, gost89.h, gosthash.c, gosthash.h) Utility gostsum (file
  207. gostsum.c) is provided as example of such separate usage. This is
  208. program, simular to md5sum and sha1sum utilities, but calculates GOST R
  209. 34.11-94 hash.
  210. Makefile doesn't include rule for compiling gostsum.
  211. Use command
  212. $(CC) -o gostsum gostsum.c gost89.c gosthash.c
  213. where $(CC) is name of your C compiler.
  214. Implementations of GOST R 34.10-xx, including VKO algorithms heavily
  215. depends on OpenSSL BIGNUM and Elliptic Curve libraries.