gmssl 1.0.8 → 1.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 436a0544ac1c53637b71170298bc0bf93d94c6fd21372b56587db3cc46599991
4
- data.tar.gz: 366622549c376a7a6eab32ab4bc3fe25f3849a18a1e9e581c8dbc2c9429ca162
3
+ metadata.gz: f7e663d96e76c2db23bf58d1a32af5dee12caa50388867dbb72edda107bd0a65
4
+ data.tar.gz: 83d4a5d271287c85bada897a9fd22a4ee5db8e4d96cf9dac6fe59f9c58492eb8
5
5
  SHA512:
6
- metadata.gz: 7d57c9230214a77d0cc7b2b7bee316363c5f810104342fa3c5cf30548bbb3b6a7b6650f150003c2cc37b126390c1d1d20369b08ceae9cd34f99beeed518200a8
7
- data.tar.gz: 4c88b05a8647b705117d880f37881d9f6e8588100043b3992de74550125774dd52390ddb5dbc9a0676bcb37809c47bd9a5ba576f8360041b5b6a133bb8060e87
6
+ metadata.gz: a2d9c2376e77251cd14214f75bc11770b4c3711e7cac6fccc88ac65b44d297d95424b9681b860bd9a6332f66e64605f3e4ccaf27472933d1bb4d82f34677a9f6
7
+ data.tar.gz: ea7f7305374bb138ec02ca4ebb85cb1fa948fb1aaedd12790f85d145e30ced25b981d7512709fa03d11509f5da68199fd7a9c207d966b62d0ff637c317f170c9
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Helper
4
+ # Example usage
5
+ # hex_string = "54A38E3B599E48C4F581FEC14B62EA29"
6
+ # packed_bytes = hex_string_to_packed_bytes(hex_string)
7
+ # puts packed_bytes
8
+ def hex_string_to_packed_bytes(hex_string)
9
+ hex_string.scan(/../).map { |byte| byte.hex }.pack("C*")
10
+ end
11
+
12
+ # Example usage
13
+ # bytes = [0x54, 0xA3, 0x8E, 0x3B, 0x59, 0x9E, 0x48, 0xC4]
14
+ # hex_string = bytes_to_hex_string(bytes.pack('C*'))
15
+ # puts hex_string
16
+ def bytes_to_hex_string(bytes)
17
+ bytes.unpack1('H*')
18
+ end
19
+ end
data/lib/gmssl/random.rb CHANGED
@@ -9,5 +9,11 @@ module GmSSL
9
9
  ffi_lib file
10
10
 
11
11
  attach_function :rand_bytes, [:pointer, :size_t], :int
12
+
13
+ def self.bytes(n = 256)
14
+ buf = FFI::MemoryPointer.new(:uint8, n)
15
+ Random.rand_bytes(buf, n)
16
+ buf.read_bytes(n).unpack('H*').first
17
+ end
12
18
  end
13
19
  end
data/lib/gmssl/sm3.rb CHANGED
@@ -1,9 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'ffi'
4
+ require 'gmssl/helper'
4
5
 
5
6
  module GmSSL
6
7
  module SM3
8
+ extend Helper
7
9
  extend FFI::Library
8
10
  file = File.join GmSSL.lib, LIB_FILE
9
11
  ffi_lib file
@@ -56,5 +58,34 @@ module GmSSL
56
58
  attach_function :sm3_digest_init, [SM3_DIGEST_CTX.by_ref, :pointer, :size_t], :int
57
59
  attach_function :sm3_digest_update, [SM3_DIGEST_CTX.by_ref, :pointer, :size_t], :int
58
60
  attach_function :sm3_digest_finish, [SM3_DIGEST_CTX.by_ref, :pointer], :int
61
+
62
+ def self.digest(data)
63
+ # Initialize SM3
64
+ sm3_ctx = SM3_CTX.new
65
+ sm3_init(sm3_ctx)
66
+ # Update SM3 context with data
67
+ sm3_update(sm3_ctx, data, data.bytesize)
68
+ # Finalize the hash
69
+ digest = FFI::MemoryPointer.new(:uint8, SM3_DIGEST_SIZE)
70
+ sm3_finish(sm3_ctx, digest)
71
+ digest.read_bytes(SM3_DIGEST_SIZE).unpack1('H*')
72
+ end
73
+
74
+ def self.hmac(hex_key, data)
75
+ key = hex_string_to_packed_bytes(hex_key)
76
+ ctx = SM3_HMAC_CTX.new
77
+ sm3_hmac_init(ctx, key, key.bytesize)
78
+ sm3_hmac_update(ctx, data, data.bytesize)
79
+ mac = FFI::MemoryPointer.new(:uint8, SM3_HMAC_SIZE)
80
+ sm3_hmac_finish(ctx, mac)
81
+ mac.read_string(SM3_HMAC_SIZE).unpack1('H*')
82
+ end
83
+
84
+ def self.pbkdf2(psswd, hex_salt, iterations, outlen)
85
+ salt = hex_string_to_packed_bytes(hex_salt)
86
+ out = FFI::MemoryPointer.new(:uint8, outlen)
87
+ sm3_pbkdf2(psswd, psswd.bytesize, salt, salt.bytesize, iterations, outlen, out)
88
+ out.read_string(outlen).unpack1('H*')
89
+ end
59
90
  end
60
91
  end
data/lib/gmssl/sm4.rb CHANGED
@@ -1,11 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'ffi'
4
-
5
4
  require 'gmssl/ghash'
5
+ require 'gmssl/helper'
6
6
 
7
7
  module GmSSL
8
8
  module SM4
9
+ extend Helper
9
10
  extend FFI::Library
10
11
  file = File.join GmSSL.lib, LIB_FILE
11
12
  ffi_lib file
@@ -82,5 +83,91 @@ module GmSSL
82
83
  attach_function :sm4_gcm_decrypt_update, [SM4_GCM_CTX.by_ref, :pointer, :size_t, :pointer, :pointer], :int
83
84
  attach_function :sm4_gcm_decrypt_finish, [SM4_GCM_CTX.by_ref, :pointer, :pointer], :int
84
85
 
86
+ def self.cbc_encrypt(key, iv, plaintext)
87
+ ctx = SM4::SM4_CBC_CTX.new
88
+ SM4.sm4_cbc_encrypt_init(ctx, key, iv)
89
+ ciphertext = FFI::MemoryPointer.new(:uint8, plaintext.bytesize + SM4::SM4_BLOCK_SIZE)
90
+ outlen = FFI::MemoryPointer.new(:size_t)
91
+ SM4.sm4_cbc_encrypt_update(ctx, plaintext, plaintext.bytesize, ciphertext, outlen)
92
+ ciphertext_len = outlen.read(:size_t)
93
+ SM4.sm4_cbc_encrypt_finish(ctx, ciphertext + ciphertext_len, outlen)
94
+ ciphertext_len += outlen.read(:size_t)
95
+ bytes_to_hex_string ciphertext.read_bytes(ciphertext_len)
96
+ end
97
+
98
+ def self.cbc_decrypt(key, iv, ciphertext)
99
+ ciphertext = hex_string_to_packed_bytes ciphertext
100
+ ctx = SM4::SM4_CBC_CTX.new
101
+ SM4.sm4_cbc_decrypt_init(ctx, key, iv)
102
+ decrypted = FFI::MemoryPointer.new(:uint8, ciphertext.bytesize + SM4::SM4_BLOCK_SIZE)
103
+ outlen = FFI::MemoryPointer.new(:size_t)
104
+ SM4.sm4_cbc_decrypt_update(ctx, ciphertext, ciphertext.bytesize, decrypted, outlen)
105
+ decrypted_len = outlen.read(:size_t)
106
+ SM4.sm4_cbc_decrypt_finish(ctx, decrypted + decrypted_len, outlen)
107
+ decrypted_len += outlen.read(:size_t)
108
+ decrypted.read_bytes(decrypted_len)
109
+ end
110
+
111
+ def self.ctr_encrypt(input_string, key_hex, ctr_hex)
112
+ key = hex_string_to_packed_bytes(key_hex)
113
+ ctr = hex_string_to_packed_bytes(ctr_hex)
114
+ input_data = input_string.bytes.pack("C*")
115
+ output_data = FFI::MemoryPointer.new(:uint8, input_data.bytesize)
116
+ output_length = FFI::MemoryPointer.new(:size_t)
117
+ key_ptr = FFI::MemoryPointer.new(:uint8, SM4::SM4_KEY_SIZE)
118
+ ctr_ptr = FFI::MemoryPointer.new(:uint8, SM4::SM4_BLOCK_SIZE)
119
+ key_ptr.put_array_of_uint8(0, key.bytes)
120
+ ctr_ptr.put_array_of_uint8(0, ctr.bytes)
121
+ ctx = SM4::SM4_CTR_CTX.new
122
+ SM4.sm4_ctr_encrypt_init(ctx, key_ptr, ctr_ptr)
123
+ SM4.sm4_ctr_encrypt_update(ctx, input_data, input_data.bytesize, output_data, output_length)
124
+ SM4.sm4_ctr_encrypt_finish(ctx, output_data, output_length)
125
+ encrypted_data = output_data.read_string(output_length.read(:size_t))
126
+ encrypted_data.unpack("H*")[0]
127
+ end
128
+
129
+ def self.gcm_encrypt(key, iv, aad, input)
130
+ key = hex_string_to_packed_bytes key
131
+ iv = hex_string_to_packed_bytes iv
132
+ key_struct = SM4::SM4_KEY.new
133
+ key_ptr = FFI::MemoryPointer.new(:uint8, SM4::SM4_KEY_SIZE)
134
+ key_ptr.put_array_of_uint8(0, key.bytes)
135
+ SM4::sm4_set_encrypt_key(key_struct, key_ptr)
136
+ iv_ptr = FFI::MemoryPointer.new(:uint8, SM4::SM4_BLOCK_SIZE)
137
+ iv_ptr.put_array_of_uint8(0, iv.bytes)
138
+ aad_ptr = FFI::MemoryPointer.new(:uint8, aad.bytesize)
139
+ aad_ptr.put_array_of_uint8(0, aad.bytes)
140
+ input_ptr = FFI::MemoryPointer.new(:uint8, input.bytesize)
141
+ input_ptr.put_array_of_uint8(0, input.bytes)
142
+ output_ptr = FFI::MemoryPointer.new(:uint8, input.bytesize)
143
+ tag_ptr = FFI::MemoryPointer.new(:uint8, SM4::SM4_GCM_MAX_TAG_SIZE)
144
+ SM4::sm4_gcm_encrypt(key_struct, iv_ptr, iv.bytesize, aad_ptr, aad.bytesize, input_ptr, input.bytesize, output_ptr, SM4::SM4_GCM_MAX_TAG_SIZE, tag_ptr)
145
+ encrypted_output = encrypted_output = output_ptr.read_string(input.bytesize).unpack1("H*")
146
+ tag = tag_ptr.read_string(SM4::SM4_GCM_MAX_TAG_SIZE).unpack1("H*")
147
+ return encrypted_output, tag
148
+ end
149
+
150
+ def self.gcm_decrypt(key, iv, aad, encrypted_output, tag)
151
+ encrypted_output = hex_string_to_packed_bytes encrypted_output
152
+ tag = hex_string_to_packed_bytes tag
153
+ key = hex_string_to_packed_bytes key
154
+ iv = hex_string_to_packed_bytes iv
155
+ key_struct = SM4::SM4_KEY.new
156
+ key_ptr = FFI::MemoryPointer.new(:uint8, SM4::SM4_KEY_SIZE)
157
+ key_ptr.put_array_of_uint8(0, key.bytes)
158
+ SM4::sm4_set_encrypt_key(key_struct, key_ptr)
159
+ iv_ptr = FFI::MemoryPointer.new(:uint8, SM4::SM4_BLOCK_SIZE)
160
+ iv_ptr.put_array_of_uint8(0, iv.bytes)
161
+ aad_ptr = FFI::MemoryPointer.new(:uint8, aad.bytesize)
162
+ aad_ptr.put_array_of_uint8(0, aad.bytes)
163
+ encrypted_ptr = FFI::MemoryPointer.new(:uint8, encrypted_output.bytesize)
164
+ encrypted_ptr.put_array_of_uint8(0, encrypted_output.bytes)
165
+ tag_ptr = FFI::MemoryPointer.new(:uint8, SM4::SM4_GCM_MAX_TAG_SIZE)
166
+ tag_ptr.put_array_of_uint8(0, tag.bytes)
167
+ decrypted_output_ptr = FFI::MemoryPointer.new(:uint8, encrypted_output.bytesize)
168
+ SM4::sm4_gcm_decrypt(key_struct, iv_ptr, iv.bytesize, aad_ptr, aad.bytesize, encrypted_ptr, encrypted_output.bytesize, tag_ptr, SM4::SM4_GCM_MAX_TAG_SIZE, decrypted_output_ptr)
169
+ decrypted_output = decrypted_output_ptr.read_string(encrypted_output.bytesize)
170
+ return decrypted_output
171
+ end
85
172
  end
86
173
  end
data/lib/gmssl/version.rb CHANGED
@@ -10,5 +10,9 @@ module GmSSL
10
10
 
11
11
  attach_function :gmssl_version_num, [], :int
12
12
  attach_function :gmssl_version_str, [], :string
13
+
14
+ def self.info
15
+ "VERSION: #{gmssl_version_num}, #{gmssl_version_str}"
16
+ end
13
17
  end
14
18
  end
data/lib/gmssl/zuc.rb CHANGED
@@ -1,9 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'ffi'
4
+ require 'gmssl/helper'
4
5
 
5
6
  module GmSSL
6
7
  module ZUC
8
+ extend Helper
7
9
  extend FFI::Library
8
10
  file = File.join GmSSL.lib, LIB_FILE
9
11
  ffi_lib file
@@ -28,5 +30,44 @@ module GmSSL
28
30
  attach_function :zuc_encrypt_init, [ZUC_CTX.by_ref, :pointer, :pointer], :int
29
31
  attach_function :zuc_encrypt_update, [ZUC_CTX.by_ref, :pointer, :size_t, :pointer, :pointer], :int
30
32
  attach_function :zuc_encrypt_finish, [ZUC_CTX.by_ref, :pointer, :pointer], :int
33
+
34
+ def self.encrypt(key, iv, input)
35
+ key = hex_string_to_packed_bytes key
36
+ iv = hex_string_to_packed_bytes iv
37
+ key_ptr = FFI::MemoryPointer.new(:uint8, ZUC::ZUC_KEY_SIZE)
38
+ key_ptr.put_array_of_uint8(0, key.bytes)
39
+ iv_ptr = FFI::MemoryPointer.new(:uint8, ZUC::ZUC_IV_SIZE)
40
+ iv_ptr.put_array_of_uint8(0, iv.bytes)
41
+ ctx = ZUC::ZUC_CTX.new
42
+ ZUC::zuc_encrypt_init(ctx, key_ptr, iv_ptr)
43
+ input_ptr = FFI::MemoryPointer.new(:uint8, input.bytesize)
44
+ input_ptr.put_array_of_uint8(0, input.bytes)
45
+ output_ptr = FFI::MemoryPointer.new(:uint8, input.bytesize)
46
+ outlen_ptr = FFI::MemoryPointer.new(:size_t)
47
+ ZUC::zuc_encrypt_update(ctx, input_ptr, input.bytesize, output_ptr, outlen_ptr)
48
+ ZUC::zuc_encrypt_finish(ctx, output_ptr, outlen_ptr)
49
+ encrypted_output = output_ptr.get_array_of_uint8(0, input.bytesize)
50
+ bytes_to_hex_string encrypted_output.pack('C*')
51
+ end
52
+
53
+ def self.decrypt(key, iv, encrypted_output)
54
+ encrypted_output = hex_string_to_packed_bytes encrypted_output
55
+ key = hex_string_to_packed_bytes key
56
+ iv = hex_string_to_packed_bytes iv
57
+ key_ptr = FFI::MemoryPointer.new(:uint8, ZUC::ZUC_KEY_SIZE)
58
+ key_ptr.put_array_of_uint8(0, key.bytes)
59
+ iv_ptr = FFI::MemoryPointer.new(:uint8, ZUC::ZUC_IV_SIZE)
60
+ iv_ptr.put_array_of_uint8(0, iv.bytes)
61
+ ctx = ZUC::ZUC_CTX.new
62
+ ZUC::zuc_encrypt_init(ctx, key_ptr, iv_ptr)
63
+ encrypted_input_ptr = FFI::MemoryPointer.new(:uint8, encrypted_output.bytesize)
64
+ encrypted_input_ptr.put_array_of_uint8(0, encrypted_output.bytes)
65
+ decrypted_output_ptr = FFI::MemoryPointer.new(:uint8, encrypted_output.bytesize)
66
+ outlen_ptr = FFI::MemoryPointer.new(:size_t)
67
+ ZUC::zuc_encrypt_update(ctx, encrypted_input_ptr, encrypted_output.bytesize, decrypted_output_ptr, outlen_ptr)
68
+ ZUC::zuc_encrypt_finish(ctx, decrypted_output_ptr, outlen_ptr)
69
+ decrypted_output = decrypted_output_ptr.get_array_of_uint8(0, encrypted_output.bytesize)
70
+ decrypted_output.pack('C*')
71
+ end
31
72
  end
32
73
  end
data/test/helper.rb CHANGED
@@ -7,3 +7,11 @@
7
7
  def hex_string_to_packed_bytes(hex_string)
8
8
  hex_string.scan(/../).map { |byte| byte.hex }.pack("C*")
9
9
  end
10
+
11
+ # Example usage
12
+ # bytes = [0x54, 0xA3, 0x8E, 0x3B, 0x59, 0x9E, 0x48, 0xC4]
13
+ # hex_string = bytes_to_hex_string(bytes.pack('C*'))
14
+ # puts hex_string
15
+ def bytes_to_hex_string(bytes)
16
+ bytes.unpack1('H*')
17
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gmssl
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.8
4
+ version: 1.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - memorycancel
8
8
  bindir: bin
9
9
  cert_chain: []
10
- date: 2025-01-20 00:00:00.000000000 Z
10
+ date: 2025-01-23 00:00:00.000000000 Z
11
11
  dependencies: []
12
12
  description: GmSSL c to ruby FFI
13
13
  email: memorycancel@gmail.com
@@ -68,6 +68,7 @@ files:
68
68
  - GmSSL/build/bin/zuctest
69
69
  - lib/gmssl.rb
70
70
  - lib/gmssl/ghash.rb
71
+ - lib/gmssl/helper.rb
71
72
  - lib/gmssl/random.rb
72
73
  - lib/gmssl/sm3.rb
73
74
  - lib/gmssl/sm4.rb