Module: GmSSL::ZUC
- Extended by:
- FFI::Library, Helper
- Defined in:
- lib/gmssl/zuc.rb
Defined Under Namespace
Constant Summary collapse
- ZUC_KEY_SIZE =
16- ZUC_IV_SIZE =
16
Class Method Summary collapse
Methods included from Helper
bytes_to_hex_string, hex_string_to_packed_bytes
Class Method Details
.decrypt(key, iv, encrypted_output) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/gmssl/zuc.rb', line 53 def self.decrypt(key, iv, encrypted_output) encrypted_output = hex_string_to_packed_bytes encrypted_output key = hex_string_to_packed_bytes key iv = hex_string_to_packed_bytes iv key_ptr = FFI::MemoryPointer.new(:uint8, ZUC::ZUC_KEY_SIZE) key_ptr.put_array_of_uint8(0, key.bytes) iv_ptr = FFI::MemoryPointer.new(:uint8, ZUC::ZUC_IV_SIZE) iv_ptr.put_array_of_uint8(0, iv.bytes) ctx = ZUC::ZUC_CTX.new ZUC::zuc_encrypt_init(ctx, key_ptr, iv_ptr) encrypted_input_ptr = FFI::MemoryPointer.new(:uint8, encrypted_output.bytesize) encrypted_input_ptr.put_array_of_uint8(0, encrypted_output.bytes) decrypted_output_ptr = FFI::MemoryPointer.new(:uint8, encrypted_output.bytesize) outlen_ptr = FFI::MemoryPointer.new(:size_t) ZUC::zuc_encrypt_update(ctx, encrypted_input_ptr, encrypted_output.bytesize, decrypted_output_ptr, outlen_ptr) ZUC::zuc_encrypt_finish(ctx, decrypted_output_ptr, outlen_ptr) decrypted_output = decrypted_output_ptr.get_array_of_uint8(0, encrypted_output.bytesize) decrypted_output.pack('C*') end |
.encrypt(key, iv, input) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/gmssl/zuc.rb', line 34 def self.encrypt(key, iv, input) key = hex_string_to_packed_bytes key iv = hex_string_to_packed_bytes iv key_ptr = FFI::MemoryPointer.new(:uint8, ZUC::ZUC_KEY_SIZE) key_ptr.put_array_of_uint8(0, key.bytes) iv_ptr = FFI::MemoryPointer.new(:uint8, ZUC::ZUC_IV_SIZE) iv_ptr.put_array_of_uint8(0, iv.bytes) ctx = ZUC::ZUC_CTX.new ZUC::zuc_encrypt_init(ctx, key_ptr, iv_ptr) input_ptr = FFI::MemoryPointer.new(:uint8, input.bytesize) input_ptr.put_array_of_uint8(0, input.bytes) output_ptr = FFI::MemoryPointer.new(:uint8, input.bytesize) outlen_ptr = FFI::MemoryPointer.new(:size_t) ZUC::zuc_encrypt_update(ctx, input_ptr, input.bytesize, output_ptr, outlen_ptr) ZUC::zuc_encrypt_finish(ctx, output_ptr, outlen_ptr) encrypted_output = output_ptr.get_array_of_uint8(0, input.bytesize) bytes_to_hex_string encrypted_output.pack('C*') end |