gmssl 1.0.8 → 1.1.0

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: 91be02f2afeab55a32d5baa453111c80def28f5d1abfbf6be95b137f216103dd
4
+ data.tar.gz: 8b6c27ee63aac9a32249f6050fe146211f14ba0cb50c021791e4b6886a011a79
5
5
  SHA512:
6
- metadata.gz: 7d57c9230214a77d0cc7b2b7bee316363c5f810104342fa3c5cf30548bbb3b6a7b6650f150003c2cc37b126390c1d1d20369b08ceae9cd34f99beeed518200a8
7
- data.tar.gz: 4c88b05a8647b705117d880f37881d9f6e8588100043b3992de74550125774dd52390ddb5dbc9a0676bcb37809c47bd9a5ba576f8360041b5b6a133bb8060e87
6
+ metadata.gz: 00c9c994e7a478215afa4705a4ac03d8b08e5e651d4cc8b1ddd1a7a9b6224fe62e2c3804ae4dca950c6b4ddc44a7b68d75fcce8f7098a99782ff9cf73ae22095
7
+ data.tar.gz: 42770e67af88dcde97433e3a70064bcef5dececf5700ff232752385f41cca461adc3b781ee7548aa0346aaf1d88a6edc84296fa19857b0c1223d3ce381aa8056
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,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'ffi'
4
+ require 'helper'
4
5
 
5
6
  module GmSSL
6
7
  module SM3
@@ -56,5 +57,34 @@ module GmSSL
56
57
  attach_function :sm3_digest_init, [SM3_DIGEST_CTX.by_ref, :pointer, :size_t], :int
57
58
  attach_function :sm3_digest_update, [SM3_DIGEST_CTX.by_ref, :pointer, :size_t], :int
58
59
  attach_function :sm3_digest_finish, [SM3_DIGEST_CTX.by_ref, :pointer], :int
60
+
61
+ def self.digest(data)
62
+ # Initialize SM3
63
+ sm3_ctx = SM3_CTX.new
64
+ sm3_init(sm3_ctx)
65
+ # Update SM3 context with data
66
+ sm3_update(sm3_ctx, data, data.bytesize)
67
+ # Finalize the hash
68
+ digest = FFI::MemoryPointer.new(:uint8, SM3_DIGEST_SIZE)
69
+ sm3_finish(sm3_ctx, digest)
70
+ digest.read_bytes(SM3_DIGEST_SIZE).unpack1('H*')
71
+ end
72
+
73
+ def self.hmac(hex_key, data)
74
+ key = hex_string_to_packed_bytes(hex_key)
75
+ ctx = SM3_HMAC_CTX.new
76
+ sm3_hmac_init(ctx, key, key.bytesize)
77
+ sm3_hmac_update(ctx, data, data.bytesize)
78
+ mac = FFI::MemoryPointer.new(:uint8, SM3_HMAC_SIZE)
79
+ sm3_hmac_finish(ctx, mac)
80
+ mac.read_string(SM3_HMAC_SIZE).unpack1('H*')
81
+ end
82
+
83
+ def self.pbkdf2(psswd, hex_salt, iterations, outlen)
84
+ salt = hex_string_to_packed_bytes(hex_salt)
85
+ out = FFI::MemoryPointer.new(:uint8, outlen)
86
+ sm3_pbkdf2(psswd, psswd.bytesize, salt, salt.bytesize, iterations, outlen, out)
87
+ out.read_string(outlen).unpack1('H*')
88
+ end
59
89
  end
60
90
  end
data/lib/gmssl/sm4.rb CHANGED
@@ -82,5 +82,91 @@ module GmSSL
82
82
  attach_function :sm4_gcm_decrypt_update, [SM4_GCM_CTX.by_ref, :pointer, :size_t, :pointer, :pointer], :int
83
83
  attach_function :sm4_gcm_decrypt_finish, [SM4_GCM_CTX.by_ref, :pointer, :pointer], :int
84
84
 
85
+ def self.cbc_encrypt(key, iv, plaintext)
86
+ ctx = SM4::SM4_CBC_CTX.new
87
+ SM4.sm4_cbc_encrypt_init(ctx, key, iv)
88
+ ciphertext = FFI::MemoryPointer.new(:uint8, plaintext.bytesize + SM4::SM4_BLOCK_SIZE)
89
+ outlen = FFI::MemoryPointer.new(:size_t)
90
+ SM4.sm4_cbc_encrypt_update(ctx, plaintext, plaintext.bytesize, ciphertext, outlen)
91
+ ciphertext_len = outlen.read(:size_t)
92
+ SM4.sm4_cbc_encrypt_finish(ctx, ciphertext + ciphertext_len, outlen)
93
+ ciphertext_len += outlen.read(:size_t)
94
+ bytes_to_hex_string ciphertext.read_bytes(ciphertext_len)
95
+ end
96
+
97
+ def self.cbc_decrypt(key, iv, ciphertext)
98
+ ciphertext = hex_string_to_packed_bytes ciphertext
99
+ ctx = SM4::SM4_CBC_CTX.new
100
+ SM4.sm4_cbc_decrypt_init(ctx, key, iv)
101
+ decrypted = FFI::MemoryPointer.new(:uint8, ciphertext.bytesize + SM4::SM4_BLOCK_SIZE)
102
+ outlen = FFI::MemoryPointer.new(:size_t)
103
+ SM4.sm4_cbc_decrypt_update(ctx, ciphertext, ciphertext.bytesize, decrypted, outlen)
104
+ decrypted_len = outlen.read(:size_t)
105
+ SM4.sm4_cbc_decrypt_finish(ctx, decrypted + decrypted_len, outlen)
106
+ decrypted_len += outlen.read(:size_t)
107
+ decrypted.read_bytes(decrypted_len)
108
+ end
109
+
110
+ def self.ctr_encrypt(input_string, key_hex, ctr_hex)
111
+ key = hex_string_to_packed_bytes(key_hex)
112
+ ctr = hex_string_to_packed_bytes(ctr_hex)
113
+ input_data = input_string.bytes.pack("C*")
114
+ output_data = FFI::MemoryPointer.new(:uint8, input_data.bytesize)
115
+ output_length = FFI::MemoryPointer.new(:size_t)
116
+ key_ptr = FFI::MemoryPointer.new(:uint8, SM4::SM4_KEY_SIZE)
117
+ ctr_ptr = FFI::MemoryPointer.new(:uint8, SM4::SM4_BLOCK_SIZE)
118
+ key_ptr.put_array_of_uint8(0, key.bytes)
119
+ ctr_ptr.put_array_of_uint8(0, ctr.bytes)
120
+ ctx = SM4::SM4_CTR_CTX.new
121
+ SM4.sm4_ctr_encrypt_init(ctx, key_ptr, ctr_ptr)
122
+ SM4.sm4_ctr_encrypt_update(ctx, input_data, input_data.bytesize, output_data, output_length)
123
+ SM4.sm4_ctr_encrypt_finish(ctx, output_data, output_length)
124
+ encrypted_data = output_data.read_string(output_length.read(:size_t))
125
+ encrypted_data.unpack("H*")[0]
126
+ end
127
+
128
+ def self.gcm_encrypt(key, iv, aad, input)
129
+ key = hex_string_to_packed_bytes key
130
+ iv = hex_string_to_packed_bytes iv
131
+ key_struct = SM4::SM4_KEY.new
132
+ key_ptr = FFI::MemoryPointer.new(:uint8, SM4::SM4_KEY_SIZE)
133
+ key_ptr.put_array_of_uint8(0, key.bytes)
134
+ SM4::sm4_set_encrypt_key(key_struct, key_ptr)
135
+ iv_ptr = FFI::MemoryPointer.new(:uint8, SM4::SM4_BLOCK_SIZE)
136
+ iv_ptr.put_array_of_uint8(0, iv.bytes)
137
+ aad_ptr = FFI::MemoryPointer.new(:uint8, aad.bytesize)
138
+ aad_ptr.put_array_of_uint8(0, aad.bytes)
139
+ input_ptr = FFI::MemoryPointer.new(:uint8, input.bytesize)
140
+ input_ptr.put_array_of_uint8(0, input.bytes)
141
+ output_ptr = FFI::MemoryPointer.new(:uint8, input.bytesize)
142
+ tag_ptr = FFI::MemoryPointer.new(:uint8, SM4::SM4_GCM_MAX_TAG_SIZE)
143
+ 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)
144
+ encrypted_output = encrypted_output = output_ptr.read_string(input.bytesize).unpack1("H*")
145
+ tag = tag_ptr.read_string(SM4::SM4_GCM_MAX_TAG_SIZE).unpack1("H*")
146
+ return encrypted_output, tag
147
+ end
148
+
149
+ def self.gcm_decrypt(key, iv, aad, encrypted_output, tag)
150
+ encrypted_output = hex_string_to_packed_bytes encrypted_output
151
+ tag = hex_string_to_packed_bytes tag
152
+ key = hex_string_to_packed_bytes key
153
+ iv = hex_string_to_packed_bytes iv
154
+ key_struct = SM4::SM4_KEY.new
155
+ key_ptr = FFI::MemoryPointer.new(:uint8, SM4::SM4_KEY_SIZE)
156
+ key_ptr.put_array_of_uint8(0, key.bytes)
157
+ SM4::sm4_set_encrypt_key(key_struct, key_ptr)
158
+ iv_ptr = FFI::MemoryPointer.new(:uint8, SM4::SM4_BLOCK_SIZE)
159
+ iv_ptr.put_array_of_uint8(0, iv.bytes)
160
+ aad_ptr = FFI::MemoryPointer.new(:uint8, aad.bytesize)
161
+ aad_ptr.put_array_of_uint8(0, aad.bytes)
162
+ encrypted_ptr = FFI::MemoryPointer.new(:uint8, encrypted_output.bytesize)
163
+ encrypted_ptr.put_array_of_uint8(0, encrypted_output.bytes)
164
+ tag_ptr = FFI::MemoryPointer.new(:uint8, SM4::SM4_GCM_MAX_TAG_SIZE)
165
+ tag_ptr.put_array_of_uint8(0, tag.bytes)
166
+ decrypted_output_ptr = FFI::MemoryPointer.new(:uint8, encrypted_output.bytesize)
167
+ 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)
168
+ decrypted_output = decrypted_output_ptr.read_string(encrypted_output.bytesize)
169
+ return decrypted_output
170
+ end
85
171
  end
86
172
  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,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'ffi'
4
+ require 'helper'
4
5
 
5
6
  module GmSSL
6
7
  module ZUC
@@ -28,5 +29,44 @@ module GmSSL
28
29
  attach_function :zuc_encrypt_init, [ZUC_CTX.by_ref, :pointer, :pointer], :int
29
30
  attach_function :zuc_encrypt_update, [ZUC_CTX.by_ref, :pointer, :size_t, :pointer, :pointer], :int
30
31
  attach_function :zuc_encrypt_finish, [ZUC_CTX.by_ref, :pointer, :pointer], :int
32
+
33
+ def self.encrypt(key, iv, input)
34
+ key = hex_string_to_packed_bytes key
35
+ iv = hex_string_to_packed_bytes iv
36
+ key_ptr = FFI::MemoryPointer.new(:uint8, ZUC::ZUC_KEY_SIZE)
37
+ key_ptr.put_array_of_uint8(0, key.bytes)
38
+ iv_ptr = FFI::MemoryPointer.new(:uint8, ZUC::ZUC_IV_SIZE)
39
+ iv_ptr.put_array_of_uint8(0, iv.bytes)
40
+ ctx = ZUC::ZUC_CTX.new
41
+ ZUC::zuc_encrypt_init(ctx, key_ptr, iv_ptr)
42
+ input_ptr = FFI::MemoryPointer.new(:uint8, input.bytesize)
43
+ input_ptr.put_array_of_uint8(0, input.bytes)
44
+ output_ptr = FFI::MemoryPointer.new(:uint8, input.bytesize)
45
+ outlen_ptr = FFI::MemoryPointer.new(:size_t)
46
+ ZUC::zuc_encrypt_update(ctx, input_ptr, input.bytesize, output_ptr, outlen_ptr)
47
+ ZUC::zuc_encrypt_finish(ctx, output_ptr, outlen_ptr)
48
+ encrypted_output = output_ptr.get_array_of_uint8(0, input.bytesize)
49
+ bytes_to_hex_string encrypted_output.pack('C*')
50
+ end
51
+
52
+ def self.decrypt(key, iv, encrypted_output)
53
+ encrypted_output = hex_string_to_packed_bytes encrypted_output
54
+ key = hex_string_to_packed_bytes key
55
+ iv = hex_string_to_packed_bytes iv
56
+ key_ptr = FFI::MemoryPointer.new(:uint8, ZUC::ZUC_KEY_SIZE)
57
+ key_ptr.put_array_of_uint8(0, key.bytes)
58
+ iv_ptr = FFI::MemoryPointer.new(:uint8, ZUC::ZUC_IV_SIZE)
59
+ iv_ptr.put_array_of_uint8(0, iv.bytes)
60
+ ctx = ZUC::ZUC_CTX.new
61
+ ZUC::zuc_encrypt_init(ctx, key_ptr, iv_ptr)
62
+ encrypted_input_ptr = FFI::MemoryPointer.new(:uint8, encrypted_output.bytesize)
63
+ encrypted_input_ptr.put_array_of_uint8(0, encrypted_output.bytes)
64
+ decrypted_output_ptr = FFI::MemoryPointer.new(:uint8, encrypted_output.bytesize)
65
+ outlen_ptr = FFI::MemoryPointer.new(:size_t)
66
+ ZUC::zuc_encrypt_update(ctx, encrypted_input_ptr, encrypted_output.bytesize, decrypted_output_ptr, outlen_ptr)
67
+ ZUC::zuc_encrypt_finish(ctx, decrypted_output_ptr, outlen_ptr)
68
+ decrypted_output = decrypted_output_ptr.get_array_of_uint8(0, encrypted_output.bytesize)
69
+ decrypted_output.pack('C*')
70
+ end
31
71
  end
32
72
  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.0
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-22 00:00:00.000000000 Z
11
11
  dependencies: []
12
12
  description: GmSSL c to ruby FFI
13
13
  email: memorycancel@gmail.com