tdes 1.0.2

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 51053dd0f4ff7a4df36529d4e50d94b7ae8794295779e8b08ed4ccf085c68885
4
+ data.tar.gz: 890f438c8a12b8e4bf39fc09b2b9ced3a629a47d283942dfbb6a8346769288c4
5
+ SHA512:
6
+ metadata.gz: fdf4e69baf621c783bf6d96f99c5740891a49284b8adace88369229b01b2337b4db46da37b7f00cca4917375c5766c6f3faf145fea3ffc5f85cd2909ed1275d3
7
+ data.tar.gz: bca0eba1feb9a310443e1607217ae51a5597cc64d454b4e00f0b0c21fb06543eb1eac1daac6ce074c0ae7a54805020a58bb2edc5d5993505568d2cd693700fd2
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ # MIT license. See http://www.opensource.org/licenses/mit-license.php
2
+
3
+ Copyright (c) 2022-2022 Cloudwalk
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'mkmf'
4
+
5
+ cflags = ENV['CFLAGS'] || '-std=c99'
6
+ ldflags = ENV['LDFLAGS'] || '-l:libcrypto.a'
7
+ with_cflags(cflags) do
8
+ with_ldflags(ldflags) do
9
+ abort 'missing openssl' unless have_library('ssl')
10
+ abort 'missing openssl/des.h' unless have_header('openssl/des.h')
11
+ abort 'missing type DES_key_schedule' unless have_type('DES_key_schedule', 'openssl/des.h')
12
+ abort 'missing type DES_cblock' unless have_type('DES_cblock', 'openssl/des.h')
13
+ abort 'missing DES_set_key_checked' unless have_func('DES_set_key_checked', 'openssl/des.h')
14
+ abort 'missing DES_ecb3_encrypt' unless have_func('DES_ecb3_encrypt', 'openssl/des.h')
15
+
16
+ create_makefile 'tdes/tdes'
17
+ end
18
+ end
data/ext/tdes/tdes.c ADDED
@@ -0,0 +1,8 @@
1
+ #include "ruby.h"
2
+
3
+ #include "tdes_class.h"
4
+
5
+ void Init_tdes(){
6
+ VALUE root_mod = rb_define_module("TDES");
7
+ setup_tdes_class(root_mod);
8
+ }
@@ -0,0 +1,94 @@
1
+ #include <assert.h>
2
+ #include <openssl/des.h>
3
+ #include "tdes_class.h"
4
+
5
+ // Key structure containing pre-processed TDES key schedules.
6
+ struct Key {
7
+ DES_key_schedule ks1, ks2, ks3;
8
+ };
9
+
10
+ // Instantiate a TDES object.
11
+ // This will embed the key schedules as C data in the object.
12
+ VALUE tdes_new(VALUE class, VALUE key) {
13
+ char* raw_key = StringValuePtr(key);
14
+ long key_length = RSTRING_LEN(key);
15
+
16
+ struct Key* key_data = NULL;
17
+ VALUE self = Data_Make_Struct(class, struct Key, NULL, free, key_data);
18
+ if (key_data == NULL) {
19
+ rb_raise(rb_eRuntimeError, "Failed to setup internal data");
20
+ }
21
+
22
+ int err;
23
+ switch (key_length) {
24
+ case 24:
25
+ err = DES_set_key_checked((void*) raw_key + 16, &key_data->ks3);
26
+ if (err) {
27
+ rb_raise(rb_eArgError, "Invalid DES key");
28
+ }
29
+ case 16:
30
+ err = DES_set_key_checked((void*) raw_key + 8, &key_data->ks2);
31
+ if (err) {
32
+ rb_raise(rb_eArgError, "Invalid DES key");
33
+ }
34
+ err = DES_set_key_checked((void*) raw_key + 0, &key_data->ks1);
35
+ if (err) {
36
+ rb_raise(rb_eArgError, "Invalid DES key");
37
+ }
38
+ break;
39
+
40
+ default:
41
+ rb_raise(rb_eArgError, "Invalid DES key length");
42
+ }
43
+
44
+ rb_obj_call_init(self, 0, NULL);
45
+
46
+ return self;
47
+ }
48
+
49
+ // Encrypt/decrypt data using TDES EDE using ECB.
50
+ // Data length must be a multiple of 8.
51
+ static VALUE tdes_encryption(VALUE self, VALUE data, int operation) {
52
+ char* raw_data = StringValuePtr(data);
53
+ long data_length = RSTRING_LEN(data);
54
+
55
+ if (data_length % 8 != 0) {
56
+ rb_raise(rb_eArgError, "Invalid DES data length");
57
+ }
58
+
59
+ struct Key* key;
60
+ Data_Get_Struct(self, struct Key, key);
61
+
62
+ VALUE encrypted = rb_str_buf_new(data_length);
63
+
64
+ assert(data_length % 8 == 0);
65
+ for (size_t i = 0; i < data_length; i += 8) {
66
+ DES_cblock out;
67
+ DES_ecb3_encrypt(
68
+ (void*) raw_data + i,
69
+ &out,
70
+ &key->ks1,
71
+ &key->ks2,
72
+ &key->ks3,
73
+ operation
74
+ );
75
+ rb_str_buf_cat(encrypted, (char *) out, 8);
76
+ }
77
+
78
+ return encrypted;
79
+ }
80
+
81
+ static VALUE tdes_encrypt(VALUE self, VALUE data) {
82
+ return tdes_encryption(self, data, DES_ENCRYPT);
83
+ }
84
+
85
+ static VALUE tdes_decrypt(VALUE self, VALUE data) {
86
+ return tdes_encryption(self, data, DES_DECRYPT);
87
+ }
88
+
89
+ void setup_tdes_class(VALUE module) {
90
+ VALUE tdes_class = rb_define_class_under(module, "TDES", rb_cObject);
91
+ rb_define_singleton_method(tdes_class, "new", tdes_new, 1);
92
+ rb_define_method(tdes_class, "encrypt", tdes_encrypt, 1);
93
+ rb_define_method(tdes_class, "decrypt", tdes_decrypt, 1);
94
+ }
@@ -0,0 +1,16 @@
1
+ #ifndef _TDES_H
2
+ #define _TDES_H
3
+
4
+ #ifdef __cplusplus
5
+ extern "C" {
6
+ #endif
7
+
8
+ #include "ruby.h"
9
+
10
+ void setup_tdes_class(VALUE module);
11
+
12
+ #ifdef __cplusplus
13
+ }
14
+ #endif
15
+
16
+ #endif /* _TDES_H */
data/lib/tdes.rb ADDED
@@ -0,0 +1 @@
1
+ require 'tdes/tdes'
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tdes
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Gabriel Bastos
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2022-07-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake-compiler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: jeweler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: triple DES encryption implemented in C using openssl
42
+ email: gabriel.bastos@cloudwalk.io
43
+ executables: []
44
+ extensions:
45
+ - ext/tdes/extconf.rb
46
+ extra_rdoc_files:
47
+ - LICENSE
48
+ files:
49
+ - LICENSE
50
+ - ext/tdes/extconf.rb
51
+ - ext/tdes/tdes.c
52
+ - ext/tdes/tdes_class.c
53
+ - ext/tdes/tdes_class.h
54
+ - lib/tdes.rb
55
+ homepage: https://github.com/cloudwalk/ruby-3des
56
+ licenses:
57
+ - MIT
58
+ metadata: {}
59
+ post_install_message:
60
+ rdoc_options: []
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ requirements: []
74
+ rubygems_version: 3.2.3
75
+ signing_key:
76
+ specification_version: 4
77
+ summary: triple DES encryption implemented in C
78
+ test_files: []