naoki 1.0.1-x86-linux

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.
data/Rakefile ADDED
@@ -0,0 +1,56 @@
1
+ require 'rake/gempackagetask'
2
+ require 'rake/clean'
3
+
4
+ EXT_CONF = 'ext/extconf.rb'
5
+ MAKEFILE = 'ext/Makefile'
6
+ MODULE = 'ext/data_secure.so'
7
+ SRC = Dir.glob('ext/*.c')
8
+ SRC << MAKEFILE
9
+
10
+ CLEAN.include ['ext/*.o', 'ext/*.bundle', MODULE]
11
+ CLOBBER.include ['ext/mkmf.log', MAKEFILE]
12
+
13
+ file MAKEFILE => EXT_CONF do |t|
14
+ Dir::chdir(File::dirname(EXT_CONF)) do
15
+ unless sh "ruby #{File::basename(EXT_CONF)}"
16
+ $stderr.puts "Failed to run extconf"
17
+ break
18
+ end
19
+ end
20
+ end
21
+
22
+ file MODULE => SRC do |t|
23
+ Dir::chdir(File::dirname(EXT_CONF)) do
24
+ unless sh "make"
25
+ $stderr.puts "make failed"
26
+ break
27
+ end
28
+ end
29
+ end
30
+
31
+ desc "Build the native library"
32
+ task :build => MODULE
33
+
34
+
35
+ PKG_FILES = FileList[
36
+ "Rakefile", "ext/*.[ch]", "ext/extconf.rb", "ext/*.so"
37
+ ]
38
+
39
+ SPEC = Gem::Specification.new do |s|
40
+ s.name = "naoki"
41
+ s.version = "1.0.1"
42
+ s.platform = Gem::Platform::CURRENT
43
+ s.homepage = ""
44
+ s.summary = "C bindings for SafeNet DataSecure ICAPI"
45
+ s.files = PKG_FILES
46
+ s.required_ruby_version = '>= 1.9.2'
47
+ s.extensions = "ext/extconf.rb"
48
+ s.authors = ["Chris Apolzon", "Liron Yahdav"]
49
+ s.email = ["apolzon@gmail.com"]
50
+ s.description = "C Bindings for SafeNet DataSecure ICAPI"
51
+ end
52
+
53
+ Rake::GemPackageTask.new(SPEC) do |pkg|
54
+ pkg.need_tar = true
55
+ pkg.need_zip = true
56
+ end
data/ext/binding.c ADDED
@@ -0,0 +1,151 @@
1
+ #include "icapi.h"
2
+ #include "ruby.h"
3
+
4
+ static I_O_Session gSession;
5
+
6
+ static VALUE open_session(VALUE self, VALUE username, VALUE password) {
7
+ int return_code;
8
+ return_code = I_C_OpenSession(&gSession, I_T_Auth_Password, RSTRING_PTR(username), RSTRING_PTR(password));
9
+
10
+ if(return_code != I_E_OK) {
11
+ I_C_Fini();
12
+ rb_raise(rb_eException, "I_C_OpenSession error: %s\n", I_C_GetErrorString(return_code));
13
+ }
14
+ }
15
+
16
+ static VALUE close_session() {
17
+ I_C_CloseSession(gSession);
18
+ I_C_Fini();
19
+ }
20
+
21
+ static VALUE configure(VALUE self, VALUE properties_file) {
22
+ int return_code;
23
+ return_code = I_C_Initialize(I_T_Init_File, RSTRING_PTR(properties_file));
24
+
25
+ if(return_code != I_E_OK) {
26
+ rb_raise(rb_eException, "I_C_Initialize error: %s\n", I_C_GetErrorString(return_code));
27
+ }
28
+ }
29
+
30
+ static VALUE encrypt(VALUE self, VALUE rb_algorithm, VALUE rb_key_name, VALUE rb_initialization_vector, VALUE rb_input_data) {
31
+ char *input_data = RSTRING_PTR(rb_input_data);
32
+ char *initialization_vector = RSTRING_PTR(rb_initialization_vector);
33
+ char *key_name = RSTRING_PTR(rb_key_name);
34
+ char *algorithm = RSTRING_PTR(rb_algorithm);
35
+
36
+ I_O_CipherSpec cipherspec;
37
+ int initialization_vector_length, input_data_length;
38
+ I_T_UINT encrypted_data_length;
39
+ I_T_BYTE *encrypted_data;
40
+ int return_code;
41
+
42
+ input_data_length = strlen(input_data);
43
+ initialization_vector_length = strlen(initialization_vector);
44
+
45
+ return_code = I_C_CreateCipherSpec(algorithm, key_name, &cipherspec);
46
+
47
+ if (return_code != I_E_OK) {
48
+ close_session();
49
+ rb_raise(rb_eException, "I_C_CreateCipherSpec error: %s\n", I_C_GetErrorString(return_code));
50
+ }
51
+
52
+ return_code = I_C_CalculateEncipheredSizeForKey(gSession,
53
+ cipherspec, I_T_Operation_Encrypt, input_data_length, &encrypted_data_length);
54
+
55
+ if(return_code != I_E_OK) {
56
+ I_C_DeleteCipherSpec(cipherspec);
57
+ close_session();
58
+ rb_raise(rb_eException, "I_C_CalculateEncipheredSizeForKey error: %s\n", I_C_GetErrorString(return_code));
59
+ }
60
+
61
+ encrypted_data = (I_T_BYTE *)malloc(encrypted_data_length);
62
+
63
+ if(!encrypted_data) {
64
+ I_C_DeleteCipherSpec(cipherspec);
65
+ close_session();
66
+ rb_raise(rb_eException, "Failed to allocate %d bytes.\n", (int)encrypted_data_length);
67
+ }
68
+
69
+ return_code = I_C_Crypt(gSession, cipherspec, I_T_Operation_Encrypt,
70
+ initialization_vector, initialization_vector_length, input_data,
71
+ input_data_length, (I_T_BYTE*)encrypted_data, &encrypted_data_length);
72
+
73
+ if(return_code != I_E_OK) {
74
+ I_C_DeleteCipherSpec(cipherspec);
75
+ close_session();
76
+ rb_raise(rb_eException, "I_C_Crypt() error: %s\n", I_C_GetErrorString(return_code));
77
+ }
78
+
79
+ I_C_DeleteCipherSpec(cipherspec);
80
+
81
+ VALUE result = rb_str_new(encrypted_data, encrypted_data_length);
82
+ free(encrypted_data);
83
+ return result;
84
+ }
85
+
86
+ static VALUE decrypt(VALUE self, VALUE rb_algorithm, VALUE rb_key_name, VALUE rb_initialization_vector, VALUE rb_encrypted_data, VALUE rb_encrypted_data_length)
87
+ {
88
+ char *algorithm = RSTRING_PTR(rb_algorithm);
89
+ char *key_name = RSTRING_PTR(rb_key_name);
90
+ char *initialization_vector = RSTRING_PTR(rb_initialization_vector);
91
+ char *encrypted_data = RSTRING_PTR(rb_encrypted_data);
92
+ int initialization_vector_length = strlen(initialization_vector);
93
+ int encrypted_data_length = NUM2INT(rb_encrypted_data_length);
94
+
95
+ I_O_CipherSpec cipherspec;
96
+ I_T_UINT decrypted_data_length;
97
+ I_T_BYTE *decrypted_data;
98
+ int return_code;
99
+
100
+ return_code = I_C_CreateCipherSpec("AES/CBC/PKCS5Padding", "stg-encrypt-test1", &cipherspec);
101
+
102
+ if(return_code != I_E_OK) {
103
+ close_session();
104
+ rb_raise(rb_eException, "I_C_CreateCipherSpec error: %s\n", I_C_GetErrorString(return_code));
105
+ }
106
+
107
+ return_code = I_C_CalculateOutputSizeForKey(gSession,
108
+ cipherspec, I_T_Operation_Decrypt, encrypted_data_length, &decrypted_data_length);
109
+
110
+ if(return_code != I_E_OK) {
111
+ I_C_DeleteCipherSpec(cipherspec);
112
+ close_session();
113
+ rb_raise(rb_eException, "I_C_CalculateOutputSizeForKey error: %s\n", I_C_GetErrorString(return_code));
114
+ }
115
+
116
+ decrypted_data = (I_T_BYTE*)malloc(decrypted_data_length);
117
+
118
+ if(!decrypted_data) {
119
+ I_C_DeleteCipherSpec(cipherspec);
120
+ close_session();
121
+ rb_raise(rb_eException, "Failed to allocate %d bytes.\n", (int)decrypted_data_length);
122
+ }
123
+
124
+ return_code = I_C_Crypt(gSession, cipherspec, I_T_Operation_Decrypt,
125
+ initialization_vector, initialization_vector_length,
126
+ encrypted_data, encrypted_data_length, (I_T_BYTE*)decrypted_data, &decrypted_data_length);
127
+
128
+ if(return_code != I_E_OK) {
129
+ I_C_DeleteCipherSpec(cipherspec);
130
+ close_session();
131
+ rb_raise(rb_eException, "I_C_Crypt() error: %s\n", I_C_GetErrorString(return_code));
132
+ }
133
+
134
+ I_C_DeleteCipherSpec(cipherspec);
135
+
136
+ VALUE result = rb_str_new(decrypted_data, decrypted_data_length);
137
+ free(decrypted_data);
138
+ return result;
139
+ }
140
+
141
+ void Init_data_secure() {
142
+ int return_code;
143
+ VALUE DataSecureWrapperModule = rb_define_module("DataSecureWrapper");
144
+
145
+ rb_define_module_function(DataSecureWrapperModule, "configure", configure, 1);
146
+ rb_define_module_function(DataSecureWrapperModule, "open", open_session, 2);
147
+ rb_define_module_function(DataSecureWrapperModule, "encrypt", encrypt, 4);
148
+ rb_define_module_function(DataSecureWrapperModule, "decrypt", decrypt, 5);
149
+ rb_define_module_function(DataSecureWrapperModule, "close", close_session, 0);
150
+ }
151
+
data/ext/extconf.rb ADDED
@@ -0,0 +1,3 @@
1
+ require 'mkmf'
2
+ have_library('ICAPI','I_C_Initialize') || exit
3
+ create_makefile("data_secure")