self_crypto 0.0.8 → 0.0.10

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,38 +1,18 @@
1
- #include "self_olm/olm.h"
1
+ #include "self_omemo.h"
2
2
  #include "self_crypto.h"
3
- #include "sodium.h"
4
3
 
5
- static VALUE last_error(VALUE self)
6
- {
7
- OlmUtility *this;
8
- Data_Get_Struct(self, OlmUtility, this);
4
+ static VALUE random_bytes(VALUE self, VALUE size) {
5
+ void * nonce;
9
6
 
10
- return rb_str_new2(olm_utility_last_error(this));
11
- }
12
-
13
- static VALUE ed25519_verify(VALUE self, VALUE data, VALUE key, VALUE signature)
14
- {
15
- VALUE retval = Qtrue;
16
- OlmUtility *this;
17
- Data_Get_Struct(self, OlmUtility, this);
18
-
19
- if(olm_ed25519_verify(this, RSTRING_PTR(key), RSTRING_LEN(key), RSTRING_PTR(data), RSTRING_LEN(data), RSTRING_PTR(dup_string(signature)), RSTRING_LEN(signature)) == olm_error()){
20
-
21
- retval = Qfalse;
7
+ if (size == Qnil) {
8
+ rb_raise(rb_eStandardError, "must specify a size");
22
9
  }
23
10
 
24
- return retval;
25
- }
26
-
27
- static VALUE random_bytes(VALUE self, VALUE size)
28
- {
29
- void *nonce;
30
-
31
- if((nonce = malloc(NUM2SIZET(size))) == NULL){
11
+ if ((nonce = malloc(NUM2SIZET(size))) == NULL) {
32
12
  rb_raise(rb_eNoMemError, "%s()", __FUNCTION__);
33
13
  }
34
14
 
35
- randombytes_buf(nonce, NUM2SIZET(size));
15
+ self_randombytes_buf(nonce, NUM2SIZET(size));
36
16
 
37
17
  VALUE n = rb_str_new(nonce, NUM2SIZET(size));
38
18
 
@@ -41,52 +21,61 @@ static VALUE random_bytes(VALUE self, VALUE size)
41
21
  return n;
42
22
  }
43
23
 
44
- static VALUE aead_xchacha20poly1305_ietf_nonce(VALUE self)
45
- {
46
- void *nonce;
24
+ static VALUE aead_xchacha20poly1305_ietf_nonce(VALUE self) {
25
+ void * nonce;
47
26
 
48
- if((nonce = malloc(crypto_aead_xchacha20poly1305_ietf_NPUBBYTES)) == NULL){
27
+ if ((nonce = malloc(self_crypto_aead_xchacha20poly1305_ietf_NPUBBYTES)) == NULL) {
49
28
  rb_raise(rb_eNoMemError, "%s()", __FUNCTION__);
50
29
  }
51
30
 
52
- randombytes_buf(nonce, crypto_aead_xchacha20poly1305_ietf_NPUBBYTES);
31
+ self_randombytes_buf(nonce, self_crypto_aead_xchacha20poly1305_ietf_NPUBBYTES);
53
32
 
54
- VALUE n = rb_str_new(nonce, crypto_aead_xchacha20poly1305_ietf_NPUBBYTES);
33
+ VALUE n = rb_str_new(nonce, self_crypto_aead_xchacha20poly1305_ietf_NPUBBYTES);
55
34
 
56
35
  free(nonce);
57
36
 
58
37
  return n;
59
38
  }
60
39
 
61
- static VALUE aead_xchacha20poly1305_ietf_keygen(VALUE self)
62
- {
63
- void *key;
40
+ static VALUE aead_xchacha20poly1305_ietf_keygen(VALUE self) {
41
+ void * key;
64
42
 
65
- if((key = malloc(crypto_aead_xchacha20poly1305_ietf_KEYBYTES)) == NULL){
43
+ if ((key = malloc(self_crypto_aead_xchacha20poly1305_ietf_KEYBYTES)) == NULL) {
66
44
  rb_raise(rb_eNoMemError, "%s()", __FUNCTION__);
67
45
  }
68
46
 
69
- crypto_aead_xchacha20poly1305_ietf_keygen(key);
47
+ self_crypto_aead_xchacha20poly1305_ietf_keygen(key);
70
48
 
71
- VALUE k = rb_str_new(key, crypto_aead_xchacha20poly1305_ietf_KEYBYTES);
49
+ VALUE k = rb_str_new(key, self_crypto_aead_xchacha20poly1305_ietf_KEYBYTES);
72
50
 
73
51
  free(key);
74
52
 
75
53
  return k;
76
54
  }
77
55
 
78
- static VALUE aead_xchacha20poly1305_ietf_encrypt(VALUE self, VALUE key, VALUE nonce, VALUE plaintext)
79
- {
80
- void *ciphertext;
56
+ static VALUE aead_xchacha20poly1305_ietf_encrypt(VALUE self, VALUE key, VALUE nonce, VALUE plaintext) {
57
+ void * ciphertext;
81
58
  unsigned long long ciphertext_len;
82
59
 
83
- if((ciphertext = malloc(RSTRING_LEN(plaintext) + crypto_aead_xchacha20poly1305_ietf_ABYTES)) == NULL){
60
+ if (key == Qnil) {
61
+ rb_raise(rb_eStandardError, "must specify a key");
62
+ }
63
+
64
+ if (nonce == Qnil) {
65
+ rb_raise(rb_eStandardError, "must specify a nonce");
66
+ }
67
+
68
+ if (plaintext == Qnil) {
69
+ rb_raise(rb_eStandardError, "must specify plaintext");
70
+ }
71
+
72
+ if ((ciphertext = malloc(RSTRING_LEN(plaintext) + self_crypto_aead_xchacha20poly1305_ietf_ABYTES)) == NULL) {
84
73
  rb_raise(rb_eNoMemError, "%s()", __FUNCTION__);
85
74
  }
86
75
 
87
- crypto_aead_xchacha20poly1305_ietf_encrypt(
88
- ciphertext,
89
- &ciphertext_len,
76
+ self_crypto_aead_xchacha20poly1305_ietf_encrypt(
77
+ ciphertext,
78
+ (uint64_t *) &ciphertext_len,
90
79
  RSTRING_PTR(plaintext),
91
80
  RSTRING_LEN(plaintext),
92
81
  NULL,
@@ -103,18 +92,29 @@ static VALUE aead_xchacha20poly1305_ietf_encrypt(VALUE self, VALUE key, VALUE no
103
92
  return ct;
104
93
  }
105
94
 
106
- static VALUE aead_xchacha20poly1305_ietf_decrypt(VALUE self, VALUE key, VALUE nonce, VALUE ciphertext)
107
- {
108
- void *plaintext;
95
+ static VALUE aead_xchacha20poly1305_ietf_decrypt(VALUE self, VALUE key, VALUE nonce, VALUE ciphertext) {
96
+ void * plaintext;
109
97
  unsigned long long plaintext_len;
110
98
 
111
- if((plaintext = malloc(RSTRING_LEN(ciphertext))) == NULL){
99
+ if (key == Qnil) {
100
+ rb_raise(rb_eStandardError, "must specify a key");
101
+ }
102
+
103
+ if (nonce == Qnil) {
104
+ rb_raise(rb_eStandardError, "must specify a nonce");
105
+ }
106
+
107
+ if (ciphertext == Qnil) {
108
+ rb_raise(rb_eStandardError, "must specify ciphertext");
109
+ }
110
+
111
+ if ((plaintext = malloc(RSTRING_LEN(ciphertext))) == NULL) {
112
112
  rb_raise(rb_eNoMemError, "%s()", __FUNCTION__);
113
113
  }
114
114
 
115
- int status = crypto_aead_xchacha20poly1305_ietf_decrypt(
115
+ int status = self_crypto_aead_xchacha20poly1305_ietf_decrypt(
116
116
  plaintext,
117
- &plaintext_len,
117
+ (uint64_t *) &plaintext_len,
118
118
  NULL,
119
119
  RSTRING_PTR(ciphertext),
120
120
  RSTRING_LEN(ciphertext),
@@ -135,66 +135,65 @@ static VALUE aead_xchacha20poly1305_ietf_decrypt(VALUE self, VALUE key, VALUE no
135
135
  return pt;
136
136
  }
137
137
 
138
- static VALUE ed25519_pk_to_curve25519(VALUE self, VALUE ed25519_pk)
139
- {
138
+ static VALUE ed25519_pk_to_curve25519(VALUE self, VALUE ed25519_pk) {
140
139
  VALUE curve25519_sk;
141
- void *pk_ptr, *dec_ptr, *enc_ptr;
140
+ void * pk_ptr, * dec_ptr, * enc_ptr;
142
141
  size_t pk_sz, dec_sz, enc_sz, success;
143
142
 
144
- if(rb_obj_is_kind_of(ed25519_pk, rb_eval_string("String")) != Qtrue){
143
+ if (rb_obj_is_kind_of(ed25519_pk, rb_eval_string("String")) != Qtrue) {
145
144
  rb_raise(rb_eTypeError, "ed25519_pk must be kind of String");
146
145
  }
147
146
 
148
- pk_sz = crypto_sign_publickeybytes();
147
+ pk_sz = self_crypto_sign_publickeybytes();
149
148
 
150
- if((dec_ptr = malloc(pk_sz)) == NULL){
149
+ if ((dec_ptr = malloc(pk_sz)) == NULL) {
151
150
  rb_raise(rb_eNoMemError, "%s()", __FUNCTION__);
152
151
  }
153
152
 
154
- success = sodium_base642bin(
153
+ success = self_base642bin(
155
154
  dec_ptr,
156
155
  pk_sz,
157
156
  RSTRING_PTR(ed25519_pk),
158
157
  RSTRING_LEN(ed25519_pk),
158
+ NULL, &
159
+ dec_sz,
159
160
  NULL,
160
- &dec_sz,
161
- NULL,
162
- sodium_base64_VARIANT_URLSAFE_NO_PADDING
161
+ self_base64_VARIANT_URLSAFE_NO_PADDING
163
162
  );
164
163
 
165
- if(success != 0) {
164
+ if (success != 0) {
166
165
  free(dec_ptr);
167
166
  rb_raise(rb_eTypeError, "could not decode ed25519 public key");
168
167
  }
169
168
 
170
- if((pk_ptr = malloc(pk_sz)) == NULL){
169
+ if ((pk_ptr = malloc(pk_sz)) == NULL) {
171
170
  rb_raise(rb_eNoMemError, "%s()", __FUNCTION__);
172
171
  }
173
172
 
174
- success = crypto_sign_ed25519_pk_to_curve25519(
173
+ success = self_crypto_sign_ed25519_pk_to_curve25519(
175
174
  pk_ptr,
176
175
  dec_ptr
177
176
  );
178
177
 
179
178
  free(dec_ptr);
180
179
 
181
- if(success != 0) {
180
+ if (success != 0) {
182
181
  free(pk_ptr);
183
182
  rb_raise(rb_eTypeError, "could not convert ed25519 public key");
184
183
  }
185
184
 
186
- enc_sz = sodium_base64_ENCODED_LEN(pk_sz, sodium_base64_VARIANT_ORIGINAL_NO_PADDING);
185
+ enc_sz = self_base64_ENCODED_LEN(pk_sz, self_base64_VARIANT_ORIGINAL_NO_PADDING);
187
186
 
188
- if((enc_ptr = malloc(enc_sz)) == NULL){
187
+ if ((enc_ptr = malloc(enc_sz)) == NULL) {
189
188
  rb_raise(rb_eNoMemError, "%s()", __FUNCTION__);
190
189
  }
191
190
 
192
- sodium_bin2base64(
191
+ self_bin2base64(
193
192
  enc_ptr,
194
193
  enc_sz,
195
194
  pk_ptr,
196
195
  pk_sz,
197
- sodium_base64_VARIANT_ORIGINAL_NO_PADDING
196
+ self_base64_VARIANT_ORIGINAL_NO_PADDING
198
197
  );
199
198
 
200
199
  free(pk_ptr);
@@ -206,54 +205,18 @@ static VALUE ed25519_pk_to_curve25519(VALUE self, VALUE ed25519_pk)
206
205
  return curve25519_sk;
207
206
  }
208
207
 
209
- static VALUE sha256(VALUE self, VALUE data)
210
- {
211
- size_t size;
212
- OlmUtility *this;
213
- Data_Get_Struct(self, OlmUtility, this);
214
-
215
- size = olm_sha256_length(this);
216
- uint8_t buf[size];
217
-
218
- (void)olm_sha256(this, RSTRING_PTR(data), RSTRING_LEN(data), buf, size);
219
-
220
- return rb_str_new(buf, size);
221
- }
222
-
223
- static void _free(void *ptr)
224
- {
225
- olm_clear_utility(ptr);
208
+ static void _free(void * ptr) {
226
209
  free(ptr);
227
210
  }
228
211
 
229
- static VALUE _alloc(VALUE klass)
230
- {
231
- OlmUtility *this;
232
- VALUE self;
233
-
234
- self = Data_Wrap_Struct(klass, 0, _free, calloc(1, olm_utility_size()));
235
-
236
- Data_Get_Struct(self, OlmUtility, this);
237
-
238
- (void)olm_utility((void *)this);
239
-
240
- return self;
241
- }
242
-
243
- void utility_init(void)
244
- {
212
+ void utility_init(void) {
245
213
  VALUE cRubyOLM = rb_define_module("SelfCrypto");
246
214
  VALUE cUtil = rb_define_module_under(cRubyOLM, "Util");
247
- VALUE cUtility = rb_define_class_under(cRubyOLM, "Utility", rb_cObject);
248
-
249
- rb_define_alloc_func(cUtility, _alloc);
250
-
251
- rb_define_method(cUtility, "sha256", sha256, 1);
252
- rb_define_method(cUtility, "ed25519_verify", ed25519_verify, 3);
215
+
253
216
  rb_define_module_function(cUtil, "ed25519_pk_to_curve25519", ed25519_pk_to_curve25519, 1);
254
217
  rb_define_module_function(cUtil, "random_bytes", random_bytes, 1);
255
218
  rb_define_module_function(cUtil, "aead_xchacha20poly1305_ietf_keygen", aead_xchacha20poly1305_ietf_keygen, 0);
256
219
  rb_define_module_function(cUtil, "aead_xchacha20poly1305_ietf_nonce", aead_xchacha20poly1305_ietf_nonce, 0);
257
220
  rb_define_module_function(cUtil, "aead_xchacha20poly1305_ietf_encrypt", aead_xchacha20poly1305_ietf_encrypt, 3);
258
221
  rb_define_module_function(cUtil, "aead_xchacha20poly1305_ietf_decrypt", aead_xchacha20poly1305_ietf_decrypt, 3);
259
- }
222
+ }
@@ -9,8 +9,6 @@ module SelfCrypto
9
9
  Session.new(pickle, password)
10
10
  end
11
11
 
12
- alias_method :has_received?, :has_received_message
13
-
14
12
  end
15
13
 
16
14
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module SelfCrypto
4
4
 
5
- VERSION="0.0.8"
5
+ VERSION="0.0.10"
6
6
 
7
7
  end
data/lib/self_crypto.rb CHANGED
@@ -9,7 +9,6 @@ require 'self_crypto/olm_message'
9
9
  require 'self_crypto/message'
10
10
  require 'self_crypto/group_message'
11
11
  require 'self_crypto/pre_key_message'
12
- require 'self_crypto/sas'
13
12
  require 'self_crypto/utility'
14
13
 
15
14
  module SelfCrypto
@@ -77,34 +77,6 @@ describe "Account" do
77
77
 
78
78
  end
79
79
 
80
- describe "#inbound_session" do
81
-
82
- let(:remote_session){ remote.outbound_session(account.ik['curve25519'], account.otk['curve25519'].values.first) }
83
- let(:remote_message){ remote_session.encrypt("hello") }
84
-
85
- it("creates session") { _(account.inbound_session(remote_message)).must_be_kind_of SelfCrypto::Session }
86
-
87
- end
88
-
89
- describe "#inbound_session from known remote" do
90
-
91
- let(:remote_session){ remote.outbound_session(account.ik['curve25519'], account.otk['curve25519'].values.first) }
92
- let(:remote_message){ remote_session.encrypt("hello") }
93
-
94
- it("creates session") { _(account.inbound_session(remote_message, remote.ik['curve25519'])).must_be_kind_of SelfCrypto::Session }
95
-
96
- end
97
-
98
- describe "#inbound_session from pickled account" do
99
-
100
- let(:remote_session){ remote.outbound_session(account.ik['curve25519'], account.otk['curve25519'].values.first) }
101
- let(:remote_message){ remote_session.encrypt("hello") }
102
- let(:pickled_account){ account.to_pickle("test") }
103
- let(:unpickled_account){ SelfCrypto::Account.from_pickle(pickled_account, "test") }
104
- it("creates session") { _(unpickled_account.inbound_session(remote_message, remote.ik['curve25519'])).must_be_kind_of SelfCrypto::Session }
105
-
106
- end
107
-
108
80
  end
109
81
 
110
82
  end
metadata CHANGED
@@ -1,15 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: self_crypto
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.8
4
+ version: 0.0.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tom Bevan
8
8
  - Cameron Harper
9
- autorequire:
9
+ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2022-01-12 00:00:00.000000000 Z
12
+ date: 2023-06-21 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake-compiler
@@ -67,7 +67,7 @@ dependencies:
67
67
  - - ">="
68
68
  - !ruby/object:Gem::Version
69
69
  version: '0'
70
- description:
70
+ description:
71
71
  email: ops@selfid.net
72
72
  executables: []
73
73
  extensions:
@@ -78,11 +78,6 @@ files:
78
78
  - ext/self_crypto/account.c
79
79
  - ext/self_crypto/extconf.rb
80
80
  - ext/self_crypto/omemo.c
81
- - ext/self_crypto/pk.c
82
- - ext/self_crypto/pk_decryption.c
83
- - ext/self_crypto/pk_encryption.c
84
- - ext/self_crypto/pk_signing.c
85
- - ext/self_crypto/sas.c
86
81
  - ext/self_crypto/self_crypto.c
87
82
  - ext/self_crypto/self_crypto.h
88
83
  - ext/self_crypto/session.c
@@ -95,13 +90,9 @@ files:
95
90
  - lib/self_crypto/olm_error.rb
96
91
  - lib/self_crypto/olm_message.rb
97
92
  - lib/self_crypto/pre_key_message.rb
98
- - lib/self_crypto/sas.rb
99
- - lib/self_crypto/sas_data.rb
100
93
  - lib/self_crypto/session.rb
101
94
  - lib/self_crypto/utility.rb
102
95
  - lib/self_crypto/version.rb
103
- - test/examples/test_bob_no_answer.rb
104
- - test/examples/test_exchange.rb
105
96
  - test/spec/test_account.rb
106
97
  - test/spec/test_util.rb
107
98
  - test/unit/test_account_methods.rb
@@ -109,7 +100,7 @@ homepage: https://github.com/aldgate-ventures/self-crypto-ruby
109
100
  licenses:
110
101
  - Apache-2.0
111
102
  metadata: {}
112
- post_install_message:
103
+ post_install_message:
113
104
  rdoc_options: []
114
105
  require_paths:
115
106
  - lib
@@ -124,13 +115,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
124
115
  - !ruby/object:Gem::Version
125
116
  version: '0'
126
117
  requirements: []
127
- rubygems_version: 3.2.5
128
- signing_key:
118
+ rubygems_version: 3.3.15
119
+ signing_key:
129
120
  specification_version: 4
130
121
  summary: Group end to end encryption for self
131
122
  test_files:
132
- - test/unit/test_account_methods.rb
133
- - test/examples/test_bob_no_answer.rb
134
- - test/examples/test_exchange.rb
135
123
  - test/spec/test_account.rb
136
124
  - test/spec/test_util.rb
125
+ - test/unit/test_account_methods.rb
data/ext/self_crypto/pk.c DELETED
@@ -1,15 +0,0 @@
1
- #include "self_olm/pk.h"
2
- #include "self_crypto.h"
3
-
4
- void pk_encryption_init(VALUE cSelfCryptoPK);
5
- void pk_decryption_init(VALUE cSelfCryptoPK);
6
- void pk_signing_init(VALUE cSelfCryptoPK);
7
-
8
- void pk_init(void) {
9
- VALUE cSelfCrypto = rb_define_module("SelfCrypto");
10
- VALUE cSelfCryptoPK = rb_define_module_under(cSelfCrypto, "PK");
11
-
12
- pk_encryption_init(cSelfCryptoPK);
13
- pk_decryption_init(cSelfCryptoPK);
14
- pk_signing_init(cSelfCryptoPK);
15
- }
@@ -1,129 +0,0 @@
1
- #include <ruby.h>
2
- #include <stdlib.h>
3
- #include <self_olm/pk.h>
4
- #include <self_olm/olm.h>
5
- #include "self_crypto.h"
6
-
7
- static void _free(void *ptr) {
8
- olm_clear_pk_decryption(ptr);
9
- free(ptr);
10
- }
11
-
12
- static size_t _size(const void *ptr __attribute__((unused))) {
13
- return olm_pk_decryption_size();
14
- }
15
-
16
- static const rb_data_type_t olm_pk_decryption_type = {
17
- .wrap_struct_name = "olm_pk_decryption",
18
- .function = {
19
- .dmark = NULL,
20
- .dfree = _free,
21
- .dsize = _size,
22
- .reserved = {NULL}
23
- },
24
- .data = NULL,
25
- .flags = RUBY_TYPED_FREE_IMMEDIATELY
26
- };
27
-
28
- static VALUE _alloc(VALUE klass) {
29
- void *memory = malloc_or_raise(olm_pk_decryption_size());
30
- return TypedData_Wrap_Struct(klass, &olm_pk_decryption_type, olm_pk_decryption(memory));
31
- }
32
-
33
- static VALUE initialize(int argc, VALUE *argv, VALUE self) {
34
- OlmPkDecryption *this;
35
- size_t publicKeyLen;
36
- char *publicKeyPtr;
37
- VALUE privateKey;
38
- TypedData_Get_Struct(self, OlmPkDecryption, &olm_pk_decryption_type, this);
39
-
40
- rb_scan_args(argc, argv, "01", &privateKey);
41
-
42
- if (NIL_P(privateKey)) {
43
- privateKey = get_random(olm_pk_private_key_length());
44
- } else {
45
- Check_Type(privateKey, T_STRING);
46
- if (RSTRING_LEN(privateKey) != olm_pk_private_key_length()) {
47
- rb_raise(rb_eval_string("ArgumentError"), "private_key has wrong size (must be %lu)", olm_pk_private_key_length());
48
- }
49
- }
50
-
51
- publicKeyLen = olm_pk_key_length();
52
- publicKeyPtr = malloc_or_raise(publicKeyLen);
53
-
54
- if (olm_pk_key_from_private(this,
55
- publicKeyPtr, publicKeyLen,
56
- RSTRING_PTR(privateKey), RSTRING_LEN(privateKey)) == olm_error()) {
57
- free(publicKeyPtr);
58
- raise_olm_error(olm_pk_decryption_last_error(this));
59
- }
60
-
61
- rb_iv_set(self, "@public_key", rb_str_new(publicKeyPtr, publicKeyLen));
62
- free(publicKeyPtr);
63
-
64
- return self;
65
- }
66
-
67
- static VALUE pk_decrypt(VALUE self, VALUE pkMessage) {
68
- OlmPkDecryption *this;
69
- size_t plaintextLen;
70
- char *plaintextPtr;
71
- VALUE ephemeral, mac, ciphertext, retval;
72
- TypedData_Get_Struct(self, OlmPkDecryption, &olm_pk_decryption_type, this);
73
-
74
- ephemeral = rb_funcall(pkMessage, rb_intern("ephemeral_key"), 0);
75
- Check_Type(ephemeral, T_STRING);
76
- mac = rb_funcall(pkMessage, rb_intern("mac"), 0);
77
- Check_Type(mac, T_STRING);
78
- ciphertext = rb_funcall(pkMessage, rb_intern("cipher_text"), 0);
79
- Check_Type(ciphertext, T_STRING);
80
-
81
- plaintextLen = olm_pk_max_plaintext_length(this, RSTRING_LEN(ciphertext));
82
- plaintextPtr = malloc_or_raise(plaintextLen);
83
-
84
- plaintextLen = olm_pk_decrypt(this,
85
- RSTRING_PTR(ephemeral), RSTRING_LEN(ephemeral),
86
- RSTRING_PTR(mac), RSTRING_LEN(mac),
87
- RSTRING_PTR(ciphertext), RSTRING_LEN(ciphertext),
88
- plaintextPtr, plaintextLen);
89
- if (plaintextLen == olm_error()) {
90
- free(plaintextPtr);
91
- raise_olm_error(olm_pk_decryption_last_error(this));
92
- }
93
-
94
- retval = rb_str_new(plaintextPtr, plaintextLen);
95
- free(plaintextPtr);
96
-
97
- return retval;
98
- }
99
-
100
- static VALUE private_key(VALUE self) {
101
- OlmPkDecryption *this;
102
- size_t privkeyLen;
103
- char *privkeyPtr;
104
- VALUE retval;
105
- TypedData_Get_Struct(self, OlmPkDecryption, &olm_pk_decryption_type, this);
106
-
107
- privkeyLen = olm_pk_private_key_length();
108
- privkeyPtr = malloc_or_raise(privkeyLen);
109
-
110
- if (olm_pk_get_private_key(this, privkeyPtr, privkeyLen) == olm_error()) {
111
- free(privkeyPtr);
112
- raise_olm_error(olm_pk_decryption_last_error(this));
113
- }
114
-
115
- retval = rb_str_new(privkeyPtr, privkeyLen);
116
- free(privkeyPtr);
117
- return retval;
118
- }
119
-
120
- void pk_decryption_init(VALUE cSelfCryptoPK) {
121
- VALUE cDecryption = rb_define_class_under(cSelfCryptoPK, "Decryption", rb_cData);
122
-
123
- rb_define_alloc_func(cDecryption, _alloc);
124
-
125
- rb_define_attr(cDecryption, "public_key", 1, 0);
126
- rb_define_method(cDecryption, "initialize", initialize, -1);
127
- rb_define_method(cDecryption, "decrypt", pk_decrypt, 1);
128
- rb_define_method(cDecryption, "private_key", private_key, 0);
129
- }
@@ -1,93 +0,0 @@
1
- #include <ruby.h>
2
- #include <stdlib.h>
3
- #include <self_olm/pk.h>
4
- #include <self_olm/olm.h>
5
- #include "self_crypto.h"
6
-
7
- static void _free(void *ptr) {
8
- olm_clear_pk_encryption(ptr);
9
- free(ptr);
10
- }
11
-
12
- static size_t _size(const void *ptr __attribute__((unused))) {
13
- return olm_pk_encryption_size();
14
- }
15
-
16
- static const rb_data_type_t olm_pk_encryption_type = {
17
- .wrap_struct_name = "olm_pk_encryption",
18
- .function = {
19
- .dmark = NULL,
20
- .dfree = _free,
21
- .dsize = _size,
22
- .reserved = {NULL}
23
- },
24
- .data = NULL,
25
- .flags = RUBY_TYPED_FREE_IMMEDIATELY
26
- };
27
-
28
- static VALUE _alloc(VALUE klass) {
29
- void *memory = malloc_or_raise(olm_pk_encryption_size());
30
- return TypedData_Wrap_Struct(klass, &olm_pk_encryption_type, olm_pk_encryption(memory));
31
- }
32
-
33
- static VALUE initialize(VALUE self, VALUE recipientKey) {
34
- OlmPkEncryption *this;
35
- TypedData_Get_Struct(self, OlmPkEncryption, &olm_pk_encryption_type, this);
36
- Check_Type(recipientKey, T_STRING);
37
-
38
- if (olm_pk_encryption_set_recipient_key(this,
39
- RSTRING_PTR(recipientKey), RSTRING_LEN(recipientKey)) == olm_error()) {
40
- raise_olm_error(olm_pk_encryption_last_error(this));
41
- }
42
-
43
- return self;
44
- }
45
-
46
- static VALUE pk_encrypt(VALUE self, VALUE plaintext) {
47
- OlmPkEncryption *this;
48
- size_t ciphertextLen, macLen, ephemeralLen, randomLen;
49
- char *ciphertextPtr, *macPtr, *ephemeralPtr;
50
- VALUE retval;
51
- TypedData_Get_Struct(self, OlmPkEncryption, &olm_pk_encryption_type, this);
52
- Check_Type(plaintext, T_STRING);
53
-
54
- ciphertextLen = olm_pk_ciphertext_length(this, RSTRING_LEN(plaintext));
55
- ciphertextPtr = malloc_or_raise(ciphertextLen);
56
- macLen = olm_pk_mac_length(this);
57
- macPtr = malloc_or_raise(macLen);
58
- ephemeralLen = olm_pk_key_length();
59
- ephemeralPtr = malloc_or_raise(ephemeralLen);
60
- randomLen = olm_pk_encrypt_random_length(this);
61
-
62
- if (olm_pk_encrypt(this,
63
- RSTRING_PTR(plaintext), RSTRING_LEN(plaintext),
64
- ciphertextPtr, ciphertextLen,
65
- macPtr, macLen,
66
- ephemeralPtr, ephemeralLen,
67
- RSTRING_PTR(get_random(randomLen)), randomLen) == olm_error()) {
68
- free(ephemeralPtr);
69
- free(macPtr);
70
- free(ciphertextPtr);
71
- raise_olm_error(olm_pk_encryption_last_error(this));
72
- }
73
-
74
- retval = rb_funcall(rb_eval_string("SelfCrypto::PK::Message"), rb_intern("new"), 3,
75
- rb_str_new(ciphertextPtr, ciphertextLen),
76
- rb_str_new(macPtr, macLen),
77
- rb_str_new(ephemeralPtr, ephemeralLen));
78
-
79
- free(ephemeralPtr);
80
- free(macPtr);
81
- free(ciphertextPtr);
82
-
83
- return retval;
84
- }
85
-
86
- void pk_encryption_init(VALUE cSelfCryptoPK) {
87
- VALUE cEncryption = rb_define_class_under(cSelfCryptoPK, "Encryption", rb_cData);
88
-
89
- rb_define_alloc_func(cEncryption, _alloc);
90
-
91
- rb_define_method(cEncryption, "initialize", initialize, 1);
92
- rb_define_method(cEncryption, "encrypt", pk_encrypt, 1);
93
- }