gmssl 1.0.5 → 1.0.8
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 +4 -4
- data/lib/gmssl/ghash.rb +29 -0
- data/lib/gmssl/random.rb +13 -0
- data/lib/gmssl/sm3.rb +60 -0
- data/lib/gmssl/sm4.rb +86 -0
- data/lib/gmssl/zuc.rb +32 -0
- data/lib/gmssl.rb +2 -0
- data/test/helper.rb +9 -0
- metadata +8 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 436a0544ac1c53637b71170298bc0bf93d94c6fd21372b56587db3cc46599991
|
4
|
+
data.tar.gz: 366622549c376a7a6eab32ab4bc3fe25f3849a18a1e9e581c8dbc2c9429ca162
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7d57c9230214a77d0cc7b2b7bee316363c5f810104342fa3c5cf30548bbb3b6a7b6650f150003c2cc37b126390c1d1d20369b08ceae9cd34f99beeed518200a8
|
7
|
+
data.tar.gz: 4c88b05a8647b705117d880f37881d9f6e8588100043b3992de74550125774dd52390ddb5dbc9a0676bcb37809c47bd9a5ba576f8360041b5b6a133bb8060e87
|
data/lib/gmssl/ghash.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'ffi'
|
4
|
+
|
5
|
+
module GmSSL
|
6
|
+
module Ghash
|
7
|
+
extend FFI::Library
|
8
|
+
file = File.join GmSSL.lib, LIB_FILE
|
9
|
+
ffi_lib file
|
10
|
+
|
11
|
+
class GF128 < FFI::Struct
|
12
|
+
layout :data, [:uint64, 2]
|
13
|
+
end
|
14
|
+
|
15
|
+
class GHASH_CTX < FFI::Struct
|
16
|
+
layout :H, GF128,
|
17
|
+
:X, GF128,
|
18
|
+
:aadlen, :size_t,
|
19
|
+
:clen, :size_t,
|
20
|
+
:block, [:uint8, 16],
|
21
|
+
:num, :size_t
|
22
|
+
end
|
23
|
+
|
24
|
+
attach_function :ghash, [:pointer, :pointer, :size_t, :pointer, :size_t, :pointer], :void
|
25
|
+
attach_function :ghash_init, [GHASH_CTX.by_ref, :pointer, :pointer, :size_t], :void
|
26
|
+
attach_function :ghash_update, [GHASH_CTX.by_ref, :pointer, :size_t], :void
|
27
|
+
attach_function :ghash_finish, [GHASH_CTX.by_ref, :pointer], :void
|
28
|
+
end
|
29
|
+
end
|
data/lib/gmssl/random.rb
ADDED
data/lib/gmssl/sm3.rb
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'ffi'
|
4
|
+
|
5
|
+
module GmSSL
|
6
|
+
module SM3
|
7
|
+
extend FFI::Library
|
8
|
+
file = File.join GmSSL.lib, LIB_FILE
|
9
|
+
ffi_lib file
|
10
|
+
|
11
|
+
SM3_DIGEST_SIZE = 32
|
12
|
+
SM3_BLOCK_SIZE = 64
|
13
|
+
SM3_STATE_WORDS = 8
|
14
|
+
SM3_HMAC_SIZE = SM3_DIGEST_SIZE
|
15
|
+
SM3_PBKDF2_MIN_ITER = 10000
|
16
|
+
SM3_PBKDF2_MAX_ITER = 16777215
|
17
|
+
SM3_PBKDF2_MAX_SALT_SIZE = 64
|
18
|
+
SM3_PBKDF2_DEFAULT_SALT_SIZE = 8
|
19
|
+
|
20
|
+
class SM3_CTX < FFI::Struct
|
21
|
+
layout :digest, [:uint32, SM3_STATE_WORDS],
|
22
|
+
:nblocks, :uint64,
|
23
|
+
:block, [:uint8, SM3_BLOCK_SIZE],
|
24
|
+
:num, :size_t
|
25
|
+
end
|
26
|
+
|
27
|
+
class SM3_HMAC_CTX < FFI::Struct
|
28
|
+
layout :sm3_ctx, SM3_CTX,
|
29
|
+
:key, [:uint8, SM3_BLOCK_SIZE]
|
30
|
+
end
|
31
|
+
|
32
|
+
class SM3_KDF_CTX < FFI::Struct
|
33
|
+
layout :sm3_ctx, SM3_CTX,
|
34
|
+
:outlen, :size_t
|
35
|
+
end
|
36
|
+
|
37
|
+
class SM3_DIGEST_CTX < FFI::Union
|
38
|
+
layout :sm3_ctx, SM3_CTX,
|
39
|
+
:hmac_ctx, SM3_HMAC_CTX
|
40
|
+
end
|
41
|
+
|
42
|
+
attach_function :sm3_compress_blocks, [:pointer, :pointer, :size_t], :void
|
43
|
+
attach_function :sm3_init, [SM3_CTX.by_ref], :void
|
44
|
+
attach_function :sm3_update, [SM3_CTX.by_ref, :pointer, :size_t], :void
|
45
|
+
attach_function :sm3_finish, [SM3_CTX.by_ref, :pointer], :void
|
46
|
+
|
47
|
+
attach_function :sm3_hmac_init, [SM3_HMAC_CTX.by_ref, :pointer, :size_t], :void
|
48
|
+
attach_function :sm3_hmac_update, [SM3_HMAC_CTX.by_ref, :pointer, :size_t], :void
|
49
|
+
attach_function :sm3_hmac_finish, [SM3_HMAC_CTX.by_ref, :pointer], :void
|
50
|
+
|
51
|
+
attach_function :sm3_kdf_init, [SM3_KDF_CTX.by_ref, :size_t], :void
|
52
|
+
attach_function :sm3_kdf_update, [SM3_KDF_CTX.by_ref, :pointer, :size_t], :void
|
53
|
+
attach_function :sm3_kdf_finish, [SM3_KDF_CTX.by_ref, :pointer], :void
|
54
|
+
attach_function :sm3_pbkdf2, [:string, :size_t, :pointer, :size_t, :size_t, :size_t, :pointer], :int
|
55
|
+
|
56
|
+
attach_function :sm3_digest_init, [SM3_DIGEST_CTX.by_ref, :pointer, :size_t], :int
|
57
|
+
attach_function :sm3_digest_update, [SM3_DIGEST_CTX.by_ref, :pointer, :size_t], :int
|
58
|
+
attach_function :sm3_digest_finish, [SM3_DIGEST_CTX.by_ref, :pointer], :int
|
59
|
+
end
|
60
|
+
end
|
data/lib/gmssl/sm4.rb
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'ffi'
|
4
|
+
|
5
|
+
require 'gmssl/ghash'
|
6
|
+
|
7
|
+
module GmSSL
|
8
|
+
module SM4
|
9
|
+
extend FFI::Library
|
10
|
+
file = File.join GmSSL.lib, LIB_FILE
|
11
|
+
ffi_lib file
|
12
|
+
|
13
|
+
SM4_KEY_SIZE = 16
|
14
|
+
SM4_BLOCK_SIZE = 16
|
15
|
+
SM4_NUM_ROUNDS = 32
|
16
|
+
SM4_GCM_MAX_TAG_SIZE = 16
|
17
|
+
|
18
|
+
class SM4_KEY < FFI::Struct
|
19
|
+
layout :rk, [:uint32, SM4_NUM_ROUNDS]
|
20
|
+
end
|
21
|
+
|
22
|
+
class SM4_CBC_CTX < FFI::Struct
|
23
|
+
layout :sm4_key, SM4_KEY,
|
24
|
+
:iv, [:uint8, SM4_BLOCK_SIZE],
|
25
|
+
:block, [:uint8, SM4_BLOCK_SIZE],
|
26
|
+
:block_nbytes, :size_t
|
27
|
+
end
|
28
|
+
|
29
|
+
class SM4_CTR_CTX < FFI::Struct
|
30
|
+
layout :sm4_key, SM4_KEY,
|
31
|
+
:ctr, [:uint8, SM4_BLOCK_SIZE],
|
32
|
+
:block, [:uint8, SM4_BLOCK_SIZE],
|
33
|
+
:block_nbytes, :size_t
|
34
|
+
end
|
35
|
+
|
36
|
+
class SM4_GCM_CTX < FFI::Struct
|
37
|
+
layout :enc_ctx, SM4_CTR_CTX,
|
38
|
+
:mac_ctx, GmSSL::Ghash::GHASH_CTX, # GHASH_CTX defined in ghash.rb
|
39
|
+
:Y, [:uint8, 16],
|
40
|
+
:taglen, :size_t,
|
41
|
+
:mac, [:uint8, 16],
|
42
|
+
:maclen, :size_t,
|
43
|
+
:encedlen, :uint64
|
44
|
+
end
|
45
|
+
|
46
|
+
attach_function :sm4_set_encrypt_key, [SM4_KEY.by_ref, :pointer], :void
|
47
|
+
attach_function :sm4_set_decrypt_key, [SM4_KEY.by_ref, :pointer], :void
|
48
|
+
attach_function :sm4_encrypt, [SM4_KEY.by_ref, :pointer, :pointer], :void
|
49
|
+
|
50
|
+
attach_function :sm4_encrypt_blocks, [SM4_KEY.by_ref, :pointer, :size_t, :pointer], :void
|
51
|
+
attach_function :sm4_cbc_encrypt_blocks, [SM4_KEY.by_ref, :pointer, :pointer, :size_t, :pointer], :void
|
52
|
+
attach_function :sm4_cbc_decrypt_blocks, [SM4_KEY.by_ref, :pointer, :pointer, :size_t, :pointer], :void
|
53
|
+
attach_function :sm4_ctr_encrypt_blocks, [SM4_KEY.by_ref, :pointer, :pointer, :size_t, :pointer], :void
|
54
|
+
attach_function :sm4_ctr32_encrypt_blocks, [SM4_KEY.by_ref, :pointer, :pointer, :size_t, :pointer], :void
|
55
|
+
|
56
|
+
attach_function :sm4_cbc_padding_encrypt, [SM4_KEY.by_ref, :pointer, :pointer, :size_t, :pointer, :pointer], :int
|
57
|
+
attach_function :sm4_cbc_padding_decrypt, [SM4_KEY.by_ref, :pointer, :pointer, :size_t, :pointer, :pointer], :int
|
58
|
+
attach_function :sm4_ctr_encrypt, [SM4_KEY.by_ref, :pointer, :pointer, :size_t, :pointer], :void
|
59
|
+
attach_function :sm4_ctr32_encrypt, [SM4_KEY.by_ref, :pointer, :pointer, :size_t, :pointer], :void
|
60
|
+
|
61
|
+
attach_function :sm4_cbc_encrypt_init, [SM4_CBC_CTX.by_ref, :pointer, :pointer], :int
|
62
|
+
attach_function :sm4_cbc_encrypt_update, [SM4_CBC_CTX.by_ref, :pointer, :size_t, :pointer, :pointer], :int
|
63
|
+
attach_function :sm4_cbc_encrypt_finish, [SM4_CBC_CTX.by_ref, :pointer, :pointer], :int
|
64
|
+
attach_function :sm4_cbc_decrypt_init, [SM4_CBC_CTX.by_ref, :pointer, :pointer], :int
|
65
|
+
attach_function :sm4_cbc_decrypt_update, [SM4_CBC_CTX.by_ref, :pointer, :size_t, :pointer, :pointer], :int
|
66
|
+
attach_function :sm4_cbc_decrypt_finish, [SM4_CBC_CTX.by_ref, :pointer, :pointer], :int
|
67
|
+
|
68
|
+
attach_function :sm4_ctr_encrypt_init, [SM4_CTR_CTX.by_ref, :pointer, :pointer], :int
|
69
|
+
attach_function :sm4_ctr_encrypt_update, [SM4_CTR_CTX.by_ref, :pointer, :size_t, :pointer, :pointer], :int
|
70
|
+
attach_function :sm4_ctr_encrypt_finish, [SM4_CTR_CTX.by_ref, :pointer, :pointer], :int
|
71
|
+
attach_function :sm4_ctr32_encrypt_init, [SM4_CTR_CTX.by_ref, :pointer, :pointer], :int
|
72
|
+
attach_function :sm4_ctr32_encrypt_update, [SM4_CTR_CTX.by_ref, :pointer, :size_t, :pointer, :pointer], :int
|
73
|
+
attach_function :sm4_ctr32_encrypt_finish, [SM4_CTR_CTX.by_ref, :pointer, :pointer], :int
|
74
|
+
|
75
|
+
attach_function :sm4_gcm_encrypt, [SM4_KEY.by_ref, :pointer, :size_t, :pointer, :size_t, :pointer, :size_t, :pointer, :size_t, :pointer], :int
|
76
|
+
attach_function :sm4_gcm_decrypt, [SM4_KEY.by_ref, :pointer, :size_t, :pointer, :size_t, :pointer, :size_t, :pointer, :size_t, :pointer], :int
|
77
|
+
|
78
|
+
attach_function :sm4_gcm_encrypt_init, [SM4_GCM_CTX.by_ref, :pointer, :size_t, :pointer, :size_t, :pointer, :size_t, :size_t], :int
|
79
|
+
attach_function :sm4_gcm_encrypt_update, [SM4_GCM_CTX.by_ref, :pointer, :size_t, :pointer, :pointer], :int
|
80
|
+
attach_function :sm4_gcm_encrypt_finish, [SM4_GCM_CTX.by_ref, :pointer, :pointer], :int
|
81
|
+
attach_function :sm4_gcm_decrypt_init, [SM4_GCM_CTX.by_ref, :pointer, :size_t, :pointer, :size_t, :pointer, :size_t, :size_t], :int
|
82
|
+
attach_function :sm4_gcm_decrypt_update, [SM4_GCM_CTX.by_ref, :pointer, :size_t, :pointer, :pointer], :int
|
83
|
+
attach_function :sm4_gcm_decrypt_finish, [SM4_GCM_CTX.by_ref, :pointer, :pointer], :int
|
84
|
+
|
85
|
+
end
|
86
|
+
end
|
data/lib/gmssl/zuc.rb
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'ffi'
|
4
|
+
|
5
|
+
module GmSSL
|
6
|
+
module ZUC
|
7
|
+
extend FFI::Library
|
8
|
+
file = File.join GmSSL.lib, LIB_FILE
|
9
|
+
ffi_lib file
|
10
|
+
|
11
|
+
ZUC_KEY_SIZE = 16
|
12
|
+
ZUC_IV_SIZE = 16
|
13
|
+
|
14
|
+
class ZUC_STATE < FFI::Struct
|
15
|
+
layout :LFSR, [:uint32, 16],
|
16
|
+
:R1, :uint32,
|
17
|
+
:R2, :uint32
|
18
|
+
end
|
19
|
+
|
20
|
+
class ZUC_CTX < FFI::Struct
|
21
|
+
layout :zuc_state, ZUC_STATE,
|
22
|
+
:block, [:uint8, 4],
|
23
|
+
:block_nbytes, :size_t
|
24
|
+
end
|
25
|
+
|
26
|
+
attach_function :zuc_init, [ZUC_STATE.by_ref, :pointer, :pointer], :void
|
27
|
+
attach_function :zuc_encrypt, [ZUC_STATE.by_ref, :pointer, :size_t, :pointer], :void
|
28
|
+
attach_function :zuc_encrypt_init, [ZUC_CTX.by_ref, :pointer, :pointer], :int
|
29
|
+
attach_function :zuc_encrypt_update, [ZUC_CTX.by_ref, :pointer, :size_t, :pointer, :pointer], :int
|
30
|
+
attach_function :zuc_encrypt_finish, [ZUC_CTX.by_ref, :pointer, :pointer], :int
|
31
|
+
end
|
32
|
+
end
|
data/lib/gmssl.rb
CHANGED
data/test/helper.rb
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Example usage
|
4
|
+
# hex_string = "54A38E3B599E48C4F581FEC14B62EA29"
|
5
|
+
# packed_bytes = hex_string_to_packed_bytes(hex_string)
|
6
|
+
# puts packed_bytes
|
7
|
+
def hex_string_to_packed_bytes(hex_string)
|
8
|
+
hex_string.scan(/../).map { |byte| byte.hex }.pack("C*")
|
9
|
+
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.
|
4
|
+
version: 1.0.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- memorycancel
|
8
8
|
bindir: bin
|
9
9
|
cert_chain: []
|
10
|
-
date: 2025-01-
|
10
|
+
date: 2025-01-20 00:00:00.000000000 Z
|
11
11
|
dependencies: []
|
12
12
|
description: GmSSL c to ruby FFI
|
13
13
|
email: memorycancel@gmail.com
|
@@ -67,7 +67,13 @@ files:
|
|
67
67
|
- GmSSL/build/bin/x509test
|
68
68
|
- GmSSL/build/bin/zuctest
|
69
69
|
- lib/gmssl.rb
|
70
|
+
- lib/gmssl/ghash.rb
|
71
|
+
- lib/gmssl/random.rb
|
72
|
+
- lib/gmssl/sm3.rb
|
73
|
+
- lib/gmssl/sm4.rb
|
70
74
|
- lib/gmssl/version.rb
|
75
|
+
- lib/gmssl/zuc.rb
|
76
|
+
- test/helper.rb
|
71
77
|
homepage: https://rubygems.org/gems/gmssl
|
72
78
|
licenses:
|
73
79
|
- MIT
|