mtproto 0.0.4 → 0.0.6

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.
Files changed (41) hide show
  1. checksums.yaml +4 -4
  2. data/.env.example +5 -0
  3. data/Rakefile +26 -1
  4. data/ext/aes_ige/Makefile +273 -0
  5. data/ext/aes_ige/aes_ige.c +103 -0
  6. data/ext/aes_ige/extconf.rb +25 -0
  7. data/ext/factorization/Makefile +273 -0
  8. data/ext/factorization/extconf.rb +3 -0
  9. data/ext/factorization/factorization.c +62 -0
  10. data/lib/mtproto/auth_key_generator.rb +241 -0
  11. data/lib/mtproto/client.rb +217 -20
  12. data/lib/mtproto/connection.rb +103 -0
  13. data/lib/mtproto/crypto/aes_ige.rb +23 -0
  14. data/lib/mtproto/crypto/auth_key_helper.rb +25 -0
  15. data/lib/mtproto/crypto/dh_key_exchange.rb +44 -0
  16. data/lib/mtproto/crypto/dh_validator.rb +80 -0
  17. data/lib/mtproto/crypto/factorization.rb +39 -0
  18. data/lib/mtproto/crypto/message_key.rb +32 -0
  19. data/lib/mtproto/crypto/rsa_key.rb +9 -15
  20. data/lib/mtproto/crypto/rsa_pad.rb +59 -0
  21. data/lib/mtproto/encrypted_message.rb +86 -0
  22. data/lib/mtproto/errors.rb +33 -0
  23. data/lib/mtproto/session.rb +20 -0
  24. data/lib/mtproto/tl/bad_msg_notification.rb +46 -0
  25. data/lib/mtproto/tl/client_dh_inner_data.rb +29 -0
  26. data/lib/mtproto/tl/code_settings.rb +25 -0
  27. data/lib/mtproto/tl/config.rb +124 -0
  28. data/lib/mtproto/tl/gzip_packed.rb +41 -0
  29. data/lib/mtproto/tl/message.rb +148 -2
  30. data/lib/mtproto/tl/msg_container.rb +40 -0
  31. data/lib/mtproto/tl/new_session_created.rb +30 -0
  32. data/lib/mtproto/tl/p_q_inner_data.rb +41 -0
  33. data/lib/mtproto/tl/rpc_error.rb +34 -0
  34. data/lib/mtproto/tl/sent_code.rb +128 -0
  35. data/lib/mtproto/tl/serializer.rb +55 -0
  36. data/lib/mtproto/tl/server_dh_inner_data.rb +85 -0
  37. data/lib/mtproto/transport/tcp_connection.rb +1 -1
  38. data/lib/mtproto/version.rb +1 -1
  39. data/lib/mtproto.rb +24 -0
  40. data/tmp/.keep +0 -0
  41. metadata +33 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 414d6f7ed0a5c1c20c5108844469daafff2af7d2c350c8370b2eb6cec73068fe
4
- data.tar.gz: d7c8e3397861bdc4f9d3c695ba884a672698f39c051c06271e5e39dcdf0aa7bd
3
+ metadata.gz: e7f507f0d6cf42ac479bf224933efca12e7519cd67d7e9d8796ceba14f517960
4
+ data.tar.gz: 79c5137adfc60752fecb5367c6b90a08e7648f8fc523a95382fe7e755888c439
5
5
  SHA512:
6
- metadata.gz: 2ccbe16eaf853697c6267ebe6b84e8593c071a66d95de9b1a1be1cafb0e33317400fb52fe85ef7aec43da2aa69bbc3f4cbc55af25928116711518925f4ed7897
7
- data.tar.gz: 9658e4c8bd82fed68fd43f40e35efb1a57742170d27aa5f6a641528628e6da0013dfa9ddb632f42cb6e5418cd82c3006b8a4624294ebc40567c08d636aeae96b
6
+ metadata.gz: 48de4193ba146630f831f690527cefcc0577c1bf56ab32452092e68dd75fa5bf86b294b8578e88e4daa26766c39ef78aa60c1a132e38975d6b01f0c21c508f29
7
+ data.tar.gz: 46d2ad65c9c1f59ea5f75789fb27008620117ab63877dad711add23cb6d477ad9cea5e5dffb724cc4d1d128a67dd9b75f8db9eddaf8e30a16005c377cb1a7542
data/.env.example ADDED
@@ -0,0 +1,5 @@
1
+ TG_API_ID=your_api_id
2
+ TG_API_HASH=your_api_hash
3
+ TG_TEST_DC='111.111.111.111:443'
4
+ TG_TEST_DC_N='1'
5
+ TG_TEST_DC_KEY='DC public key'
data/Rakefile CHANGED
@@ -2,7 +2,32 @@
2
2
 
3
3
  require 'bundler/gem_tasks'
4
4
  require 'rspec/core/rake_task'
5
+ require 'rake/extensiontask'
5
6
 
6
- RSpec::Core::RakeTask.new(:spec)
7
+ Rake::ExtensionTask.new('aes_ige') do |ext|
8
+ ext.lib_dir = 'ext/aes_ige'
9
+ end
7
10
 
11
+ Rake::ExtensionTask.new('factorization') do |ext|
12
+ ext.lib_dir = 'ext/factorization'
13
+ end
14
+
15
+ RSpec::Core::RakeTask.new(:spec) do |t|
16
+ t.rspec_opts = '--require spec_helper'
17
+ t.pattern = 'spec/**/*_spec.rb'
18
+ end
19
+
20
+ RSpec::Core::RakeTask.new('spec:unit') do |t|
21
+ t.rspec_opts = '--require spec_helper'
22
+ t.pattern = 'spec/lib/**/*_spec.rb'
23
+ end
24
+
25
+ RSpec::Core::RakeTask.new('spec:integration') do |t|
26
+ t.rspec_opts = '--require spec_helper'
27
+ t.pattern = 'spec/integration/**/*_spec.rb'
28
+ end
29
+
30
+ task spec: :compile
31
+ task 'spec:unit': :compile
32
+ task 'spec:integration': :compile
8
33
  task default: :spec
@@ -0,0 +1,273 @@
1
+
2
+ SHELL = /bin/sh
3
+
4
+ # V=0 quiet, V=1 verbose. other values don't work.
5
+ V = 0
6
+ V0 = $(V:0=)
7
+ Q1 = $(V:1=)
8
+ Q = $(Q1:0=@)
9
+ ECHO1 = $(V:1=@ :)
10
+ ECHO = $(ECHO1:0=@ echo)
11
+ NULLCMD = :
12
+
13
+ #### Start of system configuration section. ####
14
+
15
+ srcdir = .
16
+ topdir = /Users/alev/.rvm/rubies/ruby-3.4.4/include/ruby-3.4.0
17
+ hdrdir = $(topdir)
18
+ arch_hdrdir = /Users/alev/.rvm/rubies/ruby-3.4.4/include/ruby-3.4.0/arm64-darwin24
19
+ PATH_SEPARATOR = :
20
+ VPATH = $(srcdir):$(arch_hdrdir)/ruby:$(hdrdir)/ruby
21
+ prefix = $(DESTDIR)/Users/alev/.rvm/rubies/ruby-3.4.4
22
+ rubysitearchprefix = $(rubylibprefix)/$(sitearch)
23
+ rubyarchprefix = $(rubylibprefix)/$(arch)
24
+ rubylibprefix = $(libdir)/$(RUBY_BASE_NAME)
25
+ exec_prefix = $(prefix)
26
+ vendorarchhdrdir = $(vendorhdrdir)/$(sitearch)
27
+ sitearchhdrdir = $(sitehdrdir)/$(sitearch)
28
+ rubyarchhdrdir = $(rubyhdrdir)/$(arch)
29
+ vendorhdrdir = $(rubyhdrdir)/vendor_ruby
30
+ sitehdrdir = $(rubyhdrdir)/site_ruby
31
+ rubyhdrdir = $(includedir)/$(RUBY_VERSION_NAME)
32
+ vendorarchdir = $(vendorlibdir)/$(sitearch)
33
+ vendorlibdir = $(vendordir)/$(ruby_version)
34
+ vendordir = $(rubylibprefix)/vendor_ruby
35
+ sitearchdir = $(sitelibdir)/$(sitearch)
36
+ sitelibdir = $(sitedir)/$(ruby_version)
37
+ sitedir = $(rubylibprefix)/site_ruby
38
+ rubyarchdir = $(rubylibdir)/$(arch)
39
+ rubylibdir = $(rubylibprefix)/$(ruby_version)
40
+ sitearchincludedir = $(includedir)/$(sitearch)
41
+ archincludedir = $(includedir)/$(arch)
42
+ sitearchlibdir = $(libdir)/$(sitearch)
43
+ archlibdir = $(libdir)/$(arch)
44
+ ridir = $(datarootdir)/$(RI_BASE_NAME)
45
+ modular_gc_dir = $(DESTDIR)
46
+ mandir = $(datarootdir)/man
47
+ localedir = $(datarootdir)/locale
48
+ libdir = $(exec_prefix)/lib
49
+ psdir = $(docdir)
50
+ pdfdir = $(docdir)
51
+ dvidir = $(docdir)
52
+ htmldir = $(docdir)
53
+ infodir = $(datarootdir)/info
54
+ docdir = $(datarootdir)/doc/$(PACKAGE)
55
+ oldincludedir = $(DESTDIR)/usr/include
56
+ includedir = $(SDKROOT)$(prefix)/include
57
+ runstatedir = $(localstatedir)/run
58
+ localstatedir = $(prefix)/var
59
+ sharedstatedir = $(prefix)/com
60
+ sysconfdir = $(prefix)/etc
61
+ datadir = $(datarootdir)
62
+ datarootdir = $(prefix)/share
63
+ libexecdir = $(exec_prefix)/libexec
64
+ sbindir = $(exec_prefix)/sbin
65
+ bindir = $(exec_prefix)/bin
66
+ archdir = $(rubyarchdir)
67
+
68
+
69
+ CC_WRAPPER =
70
+ CC = gcc
71
+ CXX = g++
72
+ LIBRUBY = $(LIBRUBY_SO)
73
+ LIBRUBY_A = lib$(RUBY_SO_NAME)-static.a
74
+ LIBRUBYARG_SHARED = -l$(RUBY_SO_NAME)
75
+ LIBRUBYARG_STATIC = -l$(RUBY_SO_NAME)-static -framework CoreFoundation $(MAINLIBS)
76
+ empty =
77
+ OUTFLAG = -o $(empty)
78
+ COUTFLAG = -o $(empty)
79
+ CSRCFLAG = $(empty)
80
+
81
+ RUBY_EXTCONF_H =
82
+ cflags = $(hardenflags) -fdeclspec $(optflags) $(debugflags) $(warnflags)
83
+ cxxflags =
84
+ optflags = -O3 -fno-fast-math
85
+ debugflags = -ggdb3
86
+ warnflags = -Wall -Wextra -Wextra-tokens -Wdeprecated-declarations -Wdivision-by-zero -Wdiv-by-zero -Wimplicit-function-declaration -Wimplicit-int -Wpointer-arith -Wshorten-64-to-32 -Wwrite-strings -Wold-style-definition -Wmissing-noreturn -Wno-cast-function-type -Wno-constant-logical-operand -Wno-long-long -Wno-missing-field-initializers -Wno-overlength-strings -Wno-parentheses-equality -Wno-self-assign -Wno-tautological-compare -Wno-unused-parameter -Wno-unused-value -Wunused-variable -Wmisleading-indentation -Wundef
87
+ cppflags =
88
+ CCDLFLAGS = -fno-common
89
+ CFLAGS = $(CCDLFLAGS) -O3 -I/opt/homebrew/opt/libyaml/include -I/opt/homebrew/opt/libksba/include -I/opt/homebrew/opt/readline/include -I/opt/homebrew/opt/zlib/include -I/opt/homebrew/opt/openssl@1.1/include $(cflags) -fno-common -pipe -Wno-deprecated-declarations $(ARCH_FLAG)
90
+ INCFLAGS = -I. -I$(arch_hdrdir) -I$(hdrdir)/ruby/backward -I$(hdrdir) -I$(srcdir)
91
+ DEFS =
92
+ CPPFLAGS = -DHAVE_OPENSSL_AES_H -DHAVE_AES_IGE_ENCRYPT -I/opt/homebrew/opt/openssl@3/include -D_XOPEN_SOURCE -D_DARWIN_C_SOURCE -D_DARWIN_UNLIMITED_SELECT -D_REENTRANT $(DEFS) $(cppflags)
93
+ CXXFLAGS = $(CCDLFLAGS) -fdeclspec $(ARCH_FLAG)
94
+ ldflags = -L. -L/opt/homebrew/opt/libyaml/lib -L/opt/homebrew/opt/libksba/lib -L/opt/homebrew/opt/readline/lib -L/opt/homebrew/opt/zlib/lib -L/opt/homebrew/opt/openssl@1.1/lib -fstack-protector-strong
95
+ dldflags = -L/opt/homebrew/opt/libyaml/lib -L/opt/homebrew/opt/libksba/lib -L/opt/homebrew/opt/readline/lib -L/opt/homebrew/opt/zlib/lib -L/opt/homebrew/opt/openssl@1.1/lib -Wl,-undefined,dynamic_lookup
96
+ ARCH_FLAG = -arch arm64
97
+ DLDFLAGS = $(ldflags) $(dldflags) $(ARCH_FLAG)
98
+ LDSHARED = $(CC) -dynamic -bundle
99
+ LDSHAREDXX = $(CXX) -dynamic -bundle
100
+ POSTLINK = dsymutil $@ 2>/dev/null; { test -z '$(RUBY_CODESIGN)' || codesign -s '$(RUBY_CODESIGN)' $@; }
101
+ AR = ar
102
+ LD = ld
103
+ EXEEXT =
104
+
105
+ RUBY_INSTALL_NAME = $(RUBY_BASE_NAME)
106
+ RUBY_SO_NAME = ruby.3.4
107
+ RUBYW_INSTALL_NAME =
108
+ RUBY_VERSION_NAME = $(RUBY_BASE_NAME)-$(ruby_version)
109
+ RUBYW_BASE_NAME = rubyw
110
+ RUBY_BASE_NAME = ruby
111
+
112
+ arch = arm64-darwin24
113
+ sitearch = $(arch)
114
+ ruby_version = 3.4.0
115
+ ruby = $(bindir)/$(RUBY_BASE_NAME)
116
+ RUBY = $(ruby)
117
+ BUILTRUBY = $(bindir)/$(RUBY_BASE_NAME)
118
+ ruby_headers = $(hdrdir)/ruby.h $(hdrdir)/ruby/backward.h $(hdrdir)/ruby/ruby.h $(hdrdir)/ruby/defines.h $(hdrdir)/ruby/missing.h $(hdrdir)/ruby/intern.h $(hdrdir)/ruby/st.h $(hdrdir)/ruby/subst.h $(arch_hdrdir)/ruby/config.h
119
+
120
+ RM = rm -f
121
+ RM_RF = rm -fr
122
+ RMDIRS = rmdir -p
123
+ MAKEDIRS = /opt/homebrew/opt/coreutils/bin/gmkdir -p
124
+ INSTALL = /opt/homebrew/opt/coreutils/bin/ginstall -c
125
+ INSTALL_PROG = $(INSTALL) -m 0755
126
+ INSTALL_DATA = $(INSTALL) -m 644
127
+ COPY = cp
128
+ TOUCH = exit >
129
+
130
+ #### End of system configuration section. ####
131
+
132
+ preload =
133
+ libpath = . $(libdir) /opt/homebrew/opt/openssl@3/lib
134
+ LIBPATH = -L. -L$(libdir) -L/opt/homebrew/opt/openssl@3/lib
135
+ DEFFILE =
136
+
137
+ CLEANFILES = mkmf.log
138
+ DISTCLEANFILES =
139
+ DISTCLEANDIRS =
140
+
141
+ extout =
142
+ extout_prefix =
143
+ target_prefix = /aes_ige
144
+ LOCAL_LIBS =
145
+ LIBS = $(LIBRUBYARG_SHARED) -lcrypto -lssl -lpthread
146
+ ORIG_SRCS = aes_ige.c
147
+ SRCS = $(ORIG_SRCS)
148
+ OBJS = aes_ige.o
149
+ HDRS =
150
+ LOCAL_HDRS =
151
+ TARGET = aes_ige
152
+ TARGET_NAME = aes_ige
153
+ TARGET_ENTRY = Init_$(TARGET_NAME)
154
+ DLLIB = $(TARGET).bundle
155
+ EXTSTATIC =
156
+ STATIC_LIB =
157
+
158
+ TIMESTAMP_DIR = .
159
+ BINDIR = $(bindir)
160
+ RUBYCOMMONDIR = $(sitedir)$(target_prefix)
161
+ RUBYLIBDIR = $(sitelibdir)$(target_prefix)
162
+ RUBYARCHDIR = $(sitearchdir)$(target_prefix)
163
+ HDRDIR = $(sitehdrdir)$(target_prefix)
164
+ ARCHHDRDIR = $(sitearchhdrdir)$(target_prefix)
165
+ TARGET_SO_DIR =
166
+ TARGET_SO = $(TARGET_SO_DIR)$(DLLIB)
167
+ CLEANLIBS = $(TARGET_SO) $(TARGET_SO:=.dSYM)
168
+ CLEANOBJS = $(OBJS) *.bak
169
+ TARGET_SO_DIR_TIMESTAMP = $(TIMESTAMP_DIR)/.sitearchdir.-.aes_ige.time
170
+
171
+ all: $(DLLIB)
172
+ static: $(STATIC_LIB)
173
+ .PHONY: all install static install-so install-rb
174
+ .PHONY: clean clean-so clean-static clean-rb
175
+
176
+ clean-static::
177
+ clean-rb-default::
178
+ clean-rb::
179
+ clean-so::
180
+ clean: clean-so clean-static clean-rb-default clean-rb
181
+ -$(Q)$(RM_RF) $(CLEANLIBS) $(CLEANOBJS) $(CLEANFILES) .*.time
182
+
183
+ distclean-rb-default::
184
+ distclean-rb::
185
+ distclean-so::
186
+ distclean-static::
187
+ distclean: clean distclean-so distclean-static distclean-rb-default distclean-rb
188
+ -$(Q)$(RM) Makefile $(RUBY_EXTCONF_H) conftest.* mkmf.log
189
+ -$(Q)$(RM) core ruby$(EXEEXT) *~ $(DISTCLEANFILES)
190
+ -$(Q)$(RMDIRS) $(DISTCLEANDIRS) 2> /dev/null || true
191
+
192
+ realclean: distclean
193
+ install: install-so install-rb
194
+
195
+ install-so: $(DLLIB) $(TARGET_SO_DIR_TIMESTAMP)
196
+ $(INSTALL_PROG) $(DLLIB) $(RUBYARCHDIR)
197
+ clean-static::
198
+ -$(Q)$(RM) $(STATIC_LIB)
199
+ install-rb: pre-install-rb do-install-rb install-rb-default
200
+ install-rb-default: pre-install-rb-default do-install-rb-default
201
+ pre-install-rb: Makefile
202
+ pre-install-rb-default: Makefile
203
+ do-install-rb:
204
+ do-install-rb-default:
205
+ pre-install-rb-default:
206
+ @$(NULLCMD)
207
+ $(TARGET_SO_DIR_TIMESTAMP):
208
+ $(Q) $(MAKEDIRS) $(@D) $(RUBYARCHDIR)
209
+ $(Q) $(TOUCH) $@
210
+
211
+ site-install: site-install-so site-install-rb
212
+ site-install-so: install-so
213
+ site-install-rb: install-rb
214
+
215
+ .SUFFIXES: .c .m .cc .mm .cxx .cpp .o .S
216
+
217
+ .cc.o:
218
+ $(ECHO) compiling $(<)
219
+ $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -c $(CSRCFLAG)$<
220
+
221
+ .cc.S:
222
+ $(ECHO) translating $(<)
223
+ $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -S $(CSRCFLAG)$<
224
+
225
+ .mm.o:
226
+ $(ECHO) compiling $(<)
227
+ $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -c $(CSRCFLAG)$<
228
+
229
+ .mm.S:
230
+ $(ECHO) translating $(<)
231
+ $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -S $(CSRCFLAG)$<
232
+
233
+ .cxx.o:
234
+ $(ECHO) compiling $(<)
235
+ $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -c $(CSRCFLAG)$<
236
+
237
+ .cxx.S:
238
+ $(ECHO) translating $(<)
239
+ $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -S $(CSRCFLAG)$<
240
+
241
+ .cpp.o:
242
+ $(ECHO) compiling $(<)
243
+ $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -c $(CSRCFLAG)$<
244
+
245
+ .cpp.S:
246
+ $(ECHO) translating $(<)
247
+ $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -S $(CSRCFLAG)$<
248
+
249
+ .c.o:
250
+ $(ECHO) compiling $(<)
251
+ $(Q) $(CC) $(INCFLAGS) $(CPPFLAGS) $(CFLAGS) $(COUTFLAG)$@ -c $(CSRCFLAG)$<
252
+
253
+ .c.S:
254
+ $(ECHO) translating $(<)
255
+ $(Q) $(CC) $(INCFLAGS) $(CPPFLAGS) $(CFLAGS) $(COUTFLAG)$@ -S $(CSRCFLAG)$<
256
+
257
+ .m.o:
258
+ $(ECHO) compiling $(<)
259
+ $(Q) $(CC) $(INCFLAGS) $(CPPFLAGS) $(CFLAGS) $(COUTFLAG)$@ -c $(CSRCFLAG)$<
260
+
261
+ .m.S:
262
+ $(ECHO) translating $(<)
263
+ $(Q) $(CC) $(INCFLAGS) $(CPPFLAGS) $(CFLAGS) $(COUTFLAG)$@ -S $(CSRCFLAG)$<
264
+
265
+ $(TARGET_SO): $(OBJS) Makefile
266
+ $(ECHO) linking shared-object aes_ige/$(DLLIB)
267
+ -$(Q)$(RM) $(@)
268
+ $(Q) $(LDSHARED) -o $@ $(OBJS) $(LIBPATH) $(DLDFLAGS) $(LOCAL_LIBS) $(LIBS)
269
+ $(Q) $(POSTLINK)
270
+
271
+
272
+
273
+ $(OBJS): $(HDRS) $(ruby_headers)
@@ -0,0 +1,103 @@
1
+ #include <ruby.h>
2
+ #include <openssl/aes.h>
3
+ #include <string.h>
4
+
5
+ static VALUE mMTProto;
6
+ static VALUE mCrypto;
7
+ static VALUE mAES_IGE;
8
+
9
+ static VALUE
10
+ aes_ige_encrypt(VALUE self, VALUE plaintext, VALUE key, VALUE iv)
11
+ {
12
+ Check_Type(plaintext, T_STRING);
13
+ Check_Type(key, T_STRING);
14
+ Check_Type(iv, T_STRING);
15
+
16
+ long plaintext_len = RSTRING_LEN(plaintext);
17
+ long key_len = RSTRING_LEN(key);
18
+ long iv_len = RSTRING_LEN(iv);
19
+
20
+ if (key_len != 32) {
21
+ rb_raise(rb_eArgError, "Key must be 32 bytes");
22
+ }
23
+ if (iv_len != 32) {
24
+ rb_raise(rb_eArgError, "IV must be 32 bytes");
25
+ }
26
+ if (plaintext_len % 16 != 0) {
27
+ rb_raise(rb_eArgError, "Plaintext length must be multiple of 16");
28
+ }
29
+
30
+ AES_KEY aes_key;
31
+ int ret = AES_set_encrypt_key((unsigned char *)RSTRING_PTR(key), 256, &aes_key);
32
+ if (ret != 0) {
33
+ rb_raise(rb_eRuntimeError, "AES_set_encrypt_key failed");
34
+ }
35
+
36
+ VALUE ciphertext = rb_str_new(NULL, plaintext_len);
37
+ unsigned char iv_copy[32];
38
+ memcpy(iv_copy, RSTRING_PTR(iv), 32);
39
+
40
+ AES_ige_encrypt(
41
+ (unsigned char *)RSTRING_PTR(plaintext),
42
+ (unsigned char *)RSTRING_PTR(ciphertext),
43
+ plaintext_len,
44
+ &aes_key,
45
+ iv_copy,
46
+ AES_ENCRYPT
47
+ );
48
+
49
+ return ciphertext;
50
+ }
51
+
52
+ static VALUE
53
+ aes_ige_decrypt(VALUE self, VALUE ciphertext, VALUE key, VALUE iv)
54
+ {
55
+ Check_Type(ciphertext, T_STRING);
56
+ Check_Type(key, T_STRING);
57
+ Check_Type(iv, T_STRING);
58
+
59
+ long ciphertext_len = RSTRING_LEN(ciphertext);
60
+ long key_len = RSTRING_LEN(key);
61
+ long iv_len = RSTRING_LEN(iv);
62
+
63
+ if (key_len != 32) {
64
+ rb_raise(rb_eArgError, "Key must be 32 bytes");
65
+ }
66
+ if (iv_len != 32) {
67
+ rb_raise(rb_eArgError, "IV must be 32 bytes");
68
+ }
69
+ if (ciphertext_len % 16 != 0) {
70
+ rb_raise(rb_eArgError, "Ciphertext length must be multiple of 16");
71
+ }
72
+
73
+ AES_KEY aes_key;
74
+ int ret = AES_set_decrypt_key((unsigned char *)RSTRING_PTR(key), 256, &aes_key);
75
+ if (ret != 0) {
76
+ rb_raise(rb_eRuntimeError, "AES_set_decrypt_key failed");
77
+ }
78
+
79
+ VALUE plaintext = rb_str_new(NULL, ciphertext_len);
80
+ unsigned char iv_copy[32];
81
+ memcpy(iv_copy, RSTRING_PTR(iv), 32);
82
+
83
+ AES_ige_encrypt(
84
+ (unsigned char *)RSTRING_PTR(ciphertext),
85
+ (unsigned char *)RSTRING_PTR(plaintext),
86
+ ciphertext_len,
87
+ &aes_key,
88
+ iv_copy,
89
+ AES_DECRYPT
90
+ );
91
+
92
+ return plaintext;
93
+ }
94
+
95
+ void Init_aes_ige(void)
96
+ {
97
+ mMTProto = rb_define_module("MTProto");
98
+ mCrypto = rb_define_module_under(mMTProto, "Crypto");
99
+ mAES_IGE = rb_define_module_under(mCrypto, "AES_IGE");
100
+
101
+ rb_define_singleton_method(mAES_IGE, "encrypt_ige", aes_ige_encrypt, 3);
102
+ rb_define_singleton_method(mAES_IGE, "decrypt_ige", aes_ige_decrypt, 3);
103
+ }
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'mkmf'
4
+
5
+ # Find OpenSSL
6
+ dir_config('openssl')
7
+
8
+ # Check for OpenSSL headers and libraries
9
+ unless have_header('openssl/aes.h')
10
+ abort 'OpenSSL headers not found. Please install OpenSSL development files.'
11
+ end
12
+
13
+ unless have_library('ssl') && have_library('crypto')
14
+ abort 'OpenSSL libraries not found. Please install OpenSSL.'
15
+ end
16
+
17
+ # Check for AES_ige_encrypt function
18
+ unless have_func('AES_ige_encrypt', 'openssl/aes.h')
19
+ abort 'AES_ige_encrypt function not found in OpenSSL library.'
20
+ end
21
+
22
+ # Suppress deprecation warnings for OpenSSL 3.0
23
+ $CFLAGS += ' -Wno-deprecated-declarations'
24
+
25
+ create_makefile('aes_ige/aes_ige')