GostKuznyechik
GOST R 34.12/13-2015 (Kuznyechik) block cipher algorithms for ECB, CBC, CTR, OFB, CFB, OMAC, CTR-ACPKM, OMAC-ACPKM modes and key export/import algorithms.
Installation
Add this line to your application's Gemfile:
gem 'gost_kuznyechik'
And then execute:
$ bundle
Or install it yourself as:
$ gem install gost_kuznyechik
Usage
require 'gost_kuznyechik'
include GostKuznyechik
BlockSize = Kuznyechik::BlockLengthInBytes
# GOST R 34.13-2015 Kuznyechik test data
SelfTestGostKMasterKeyData = [
0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef
].pack('C*').freeze
SelfTestGostKPlainText = [
0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x00,
0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa, 0x99, 0x88,
0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xee, 0xff, 0x0a,
0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88,
0x99, 0xaa, 0xbb, 0xcc, 0xee, 0xff, 0x0a, 0x00,
0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99,
0xaa, 0xbb, 0xcc, 0xee, 0xff, 0x0a, 0x00, 0x11
].pack('C*').freeze
key = SelfTestGostKMasterKeyData
plain_text = SelfTestGostKPlainText
# ECB mode
SelfTestGostKEcbEncText = [
0x7f, 0x67, 0x9d, 0x90, 0xbe, 0xbc, 0x24, 0x30,
0x5a, 0x46, 0x8d, 0x42, 0xb9, 0xd4, 0xed, 0xcd,
0xb4, 0x29, 0x91, 0x2c, 0x6e, 0x00, 0x32, 0xf9,
0x28, 0x54, 0x52, 0xd7, 0x67, 0x18, 0xd0, 0x8b,
0xf0, 0xca, 0x33, 0x54, 0x9d, 0x24, 0x7c, 0xee,
0xf3, 0xf5, 0xa5, 0x31, 0x3b, 0xd4, 0xb1, 0x57,
0xd0, 0xb0, 0x9c, 0xcd, 0xe8, 0x30, 0xb9, 0xeb,
0x3a, 0x02, 0xc4, 0xc5, 0xaa, 0x8a, 0xda, 0x98
].pack('C*').freeze
encrypted_test = SelfTestGostKEcbEncText
encrypted_text = KuznyechikEcb.new(key).encrypt(plain_text)
puts "ECB encrypted_text == encrypted_test: #{encrypted_text == encrypted_test}"
decrypted_text = KuznyechikEcb.new(key).decrypt(encrypted_test)
puts "ECB decrypted_text == plain_text: #{decrypted_text == plain_text}"
# OMAC mode
SelfTestGostKMacValue = [
0x33, 0x6f, 0x4d, 0x29, 0x60, 0x59, 0xfb, 0xe3
].pack('C*').freeze
mac_test = SelfTestGostKMacValue
mac = KuznyechikOmac.new(key, mac_test.length).update(plain_text).final
puts "OMAC mac == mac_test: #{mac == mac_test}"
# CTR mode
SelfTestGostKCtrSV = [
0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xce, 0xf0,
0xa1, 0xb2, 0xc3, 0xd4, 0xe5, 0xf0, 0x01, 0x12
].pack('C*').freeze
iv = SelfTestGostKCtrSV
SelfTestGostKCtrEncText = [
0xf1, 0x95, 0xd8, 0xbe, 0xc1, 0x0e, 0xd1, 0xdb,
0xd5, 0x7b, 0x5f, 0xa2, 0x40, 0xbd, 0xa1, 0xb8,
0x85, 0xee, 0xe7, 0x33, 0xf6, 0xa1, 0x3e, 0x5d,
0xf3, 0x3c, 0xe4, 0xb3, 0x3c, 0x45, 0xde, 0xe4,
0xa5, 0xea, 0xe8, 0x8b, 0xe6, 0x35, 0x6e, 0xd3,
0xd5, 0xe8, 0x77, 0xf1, 0x35, 0x64, 0xa3, 0xa5,
0xcb, 0x91, 0xfa, 0xb1, 0xf2, 0x0c, 0xba, 0xb6,
0xd1, 0xc6, 0xd1, 0x58, 0x20, 0xbd, 0xba, 0x73
].pack('C*').freeze
encrypted_test = SelfTestGostKCtrEncText
encrypted_text = KuznyechikCtr.new(key, iv, BlockSize).encrypt(plain_text)
puts "CTR encrypted_text == encrypted_test: #{encrypted_text == encrypted_test}"
# CTR multi-part usage
text_len = plain_text.length
ctx = KuznyechikCtr.new(key, iv, BlockSize)
decrypted_text = ctx.decrypt(encrypted_test[0...text_len/3]) +
ctx.decrypt(encrypted_test[text_len/3..-1])
puts "CTR decrypted_text == plain_text: #{decrypted_text == plain_text}"
# Key export/import (TC 26 KExp15/KImp15)
TC26_K = [
0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF,
0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF
].pack('C*').freeze
TC26_Kmac = [
0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F
].pack('C*').freeze
TC26_Kenc = [
0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F,
0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F,
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
].pack('C*').freeze
TC26_IV_K = [
0x09, 0x09, 0x47, 0x2D, 0xD9, 0xF2, 0x6B, 0xE8
].pack('C*').freeze
TC26_Kexp_K = [
0xE3, 0x61, 0x84, 0xE8, 0x4E, 0x8D, 0x73, 0x6F,
0xF3, 0x6C, 0xC2, 0xE5, 0xAE, 0x06, 0x5D, 0xC6,
0x56, 0xB2, 0x3C, 0x20, 0xF5, 0x49, 0xB0, 0x2F,
0xDF, 0xF8, 0x8E, 0x1F, 0x3F, 0x30, 0xD8, 0xC2,
0x9A, 0x53, 0xF3, 0xCA, 0x55, 0x4D, 0xBA, 0xD8,
0x0D, 0xE1, 0x52, 0xB9, 0xA4, 0x62, 0x5B, 0x32
].pack('C*').freeze
key = TC26_K
key_mac = TC26_Kmac
key_enc = TC26_Kenc
iv = TC26_IV_K
key_exp_test = TC26_Kexp_K
key_exp = KuznyechikKeyExpImp::export(key, key_mac, key_enc, iv)
puts "Key Export key_exp == key_exp_test: #{key_exp == key_exp_test}"
imp_key = KuznyechikKeyExpImp::import(key_exp_test, key_mac, key_enc, iv)
puts "Key Import imp_key == key: #{imp_key == key}"
For other cipher modes see test samples in /test/gost_kuznyechik_test.rb please.
Development
After checking out the repo, run bin/setup
to install dependencies. Then, run rake test
to run the tests. You can also run bin/console
for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install
. To release a new version, update the version number in version.rb
, and then run bundle exec rake release
, which will create a git tag for the version, push git commits and tags, and push the .gem
file to rubygems.org.
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/vblazhnovgit/gost_kuznyechik.
License
The gem is available as open source under the terms of the MIT License.