self_crypto 0.0.7 → 0.0.10
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/ext/self_crypto/account.c +126 -120
- data/ext/self_crypto/extconf.rb +2 -4
- data/ext/self_crypto/omemo.c +42 -49
- data/ext/self_crypto/self_crypto.c +9 -40
- data/ext/self_crypto/session.c +71 -219
- data/ext/self_crypto/utility.c +152 -73
- data/lib/self_crypto/group_message.rb +2 -0
- data/lib/self_crypto/group_session.rb +2 -0
- data/lib/self_crypto/session.rb +0 -2
- data/lib/self_crypto/utility.rb +2 -0
- data/lib/self_crypto/version.rb +3 -1
- data/lib/self_crypto.rb +2 -1
- data/test/spec/test_account.rb +0 -28
- data/test/spec/test_util.rb +12 -0
- metadata +9 -20
- data/ext/self_crypto/pk.c +0 -15
- data/ext/self_crypto/pk_decryption.c +0 -129
- data/ext/self_crypto/pk_encryption.c +0 -93
- data/ext/self_crypto/pk_signing.c +0 -102
- data/ext/self_crypto/sas.c +0 -190
- data/lib/self_crypto/sas.rb +0 -28
- data/lib/self_crypto/sas_data.rb +0 -71
- data/test/examples/test_bob_no_answer.rb +0 -62
- data/test/examples/test_exchange.rb +0 -60
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 31cc22c6b9dd88e85087d810252d8c27ffe3519794f3bcc9508b518c45e169d8
|
4
|
+
data.tar.gz: fce33b4e5a82e3733a271cb7f7d6550f3689e93f0f80a1b4c8976f30f5115f7d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a4ba1661748742d3b5bdb3f563b71ca88ab646e1c05bfdf7578ff2943a672b592455a8ec039457069f2b7ac88b951344415a2349a20545b7a9683d8f13a92d2b
|
7
|
+
data.tar.gz: ab7e3453c2d122c5fb68eca9d9c5606c3184626765fed83baac4166e2d76fb0cfad15c9776e42513a034a04d9526d1f3020675fdb4c85aad4d624d6b85935254
|
data/ext/self_crypto/account.c
CHANGED
@@ -1,130 +1,154 @@
|
|
1
|
-
#include "
|
2
|
-
#include "self_olm/olm.h"
|
1
|
+
#include "self_omemo.h"
|
3
2
|
#include "self_crypto.h"
|
4
3
|
|
5
|
-
static VALUE last_error(VALUE self)
|
6
|
-
|
7
|
-
OlmAccount *this;
|
4
|
+
static VALUE last_error(VALUE self) {
|
5
|
+
OlmAccount * this;
|
8
6
|
Data_Get_Struct(self, OlmAccount, this);
|
9
7
|
|
10
|
-
return rb_funcall(rb_eval_string("SelfCrypto::OlmError"), rb_intern("from_string"), 1, rb_str_new2(
|
8
|
+
return rb_funcall(rb_eval_string("SelfCrypto::OlmError"), rb_intern("from_string"), 1, rb_str_new2(self_olm_account_last_error(this)));
|
11
9
|
}
|
12
10
|
|
13
|
-
static VALUE initialize(int argc, VALUE *argv, VALUE self)
|
14
|
-
{
|
11
|
+
static VALUE initialize(int argc, VALUE * argv, VALUE self) {
|
15
12
|
VALUE pickle, password, seed, opts;
|
16
13
|
size_t size;
|
17
|
-
OlmAccount *this;
|
14
|
+
OlmAccount * this;
|
15
|
+
|
18
16
|
Data_Get_Struct(self, OlmAccount, this);
|
19
17
|
|
20
|
-
(void)rb_scan_args(argc, argv, "0:", &opts);
|
18
|
+
(void) rb_scan_args(argc, argv, "0:", & opts);
|
21
19
|
|
22
|
-
opts = (opts == Qnil) ? rb_hash_new(): opts;
|
20
|
+
opts = (opts == Qnil) ? rb_hash_new() : opts;
|
23
21
|
|
24
22
|
pickle = rb_hash_aref(opts, ID2SYM(rb_intern("pickle")));
|
25
23
|
password = rb_hash_aref(opts, ID2SYM(rb_intern("password")));
|
26
24
|
seed = rb_hash_aref(opts, ID2SYM(rb_intern("seed")));
|
27
25
|
|
28
|
-
if(pickle != Qnil){
|
29
|
-
|
30
|
-
if(rb_obj_is_kind_of(pickle, rb_cString) != Qtrue){
|
31
|
-
|
26
|
+
if (pickle != Qnil) {
|
27
|
+
if (rb_obj_is_kind_of(pickle, rb_cString) != Qtrue) {
|
32
28
|
rb_raise(rb_eTypeError, "pickle must be kind of String");
|
33
29
|
}
|
34
30
|
}
|
35
31
|
|
36
|
-
if(password != Qnil){
|
37
|
-
|
38
|
-
if(rb_obj_is_kind_of(password, rb_cString) != Qtrue){
|
39
|
-
|
32
|
+
if (password != Qnil) {
|
33
|
+
if (rb_obj_is_kind_of(password, rb_cString) != Qtrue) {
|
40
34
|
rb_raise(rb_eTypeError, "password must be kind of String");
|
41
35
|
}
|
42
|
-
}
|
43
|
-
else{
|
44
|
-
|
36
|
+
} else {
|
45
37
|
password = rb_str_new2("");
|
46
38
|
}
|
47
39
|
|
48
|
-
if(pickle != Qnil){
|
49
|
-
if(
|
50
|
-
raise_olm_error(
|
40
|
+
if (pickle != Qnil) {
|
41
|
+
if (self_olm_unpickle_account(this, RSTRING_PTR(password), RSTRING_LEN(password), RSTRING_PTR(dup_string(pickle)), RSTRING_LEN(pickle)) == -1) {
|
42
|
+
raise_olm_error(self_olm_account_last_error(this));
|
43
|
+
}
|
44
|
+
} else if (seed != Qnil) {
|
45
|
+
uint8_t *ed25519_pk = malloc_or_raise(self_crypto_sign_publickeybytes());
|
46
|
+
uint8_t *ed25519_sk = malloc_or_raise(self_crypto_sign_secretkeybytes());
|
47
|
+
uint8_t *curve25519_pk = malloc_or_raise(self_crypto_sign_publickeybytes()); // equivalent to crypto_scalarmult_curve25519_BYTES
|
48
|
+
uint8_t *curve25519_sk = malloc_or_raise(self_crypto_sign_publickeybytes()); // equivalent to crypto_scalarmult_curve25519_BYTES
|
49
|
+
size_t status = self_crypto_sign_seed_keypair(
|
50
|
+
ed25519_pk,
|
51
|
+
ed25519_sk,
|
52
|
+
RSTRING_PTR(seed)
|
53
|
+
);
|
54
|
+
|
55
|
+
if (status != 0) {
|
56
|
+
raise_olm_error("could not generate key from seed");
|
51
57
|
}
|
52
|
-
|
53
|
-
|
54
|
-
|
58
|
+
|
59
|
+
status = self_crypto_sign_ed25519_pk_to_curve25519(
|
60
|
+
curve25519_pk,
|
61
|
+
ed25519_pk
|
62
|
+
);
|
63
|
+
|
64
|
+
if (status != 0) {
|
65
|
+
raise_olm_error("could not convert ed25519 public key to curve25519");
|
66
|
+
}
|
67
|
+
|
68
|
+
status = self_crypto_sign_ed25519_sk_to_curve25519(
|
69
|
+
curve25519_sk,
|
70
|
+
ed25519_sk
|
71
|
+
);
|
72
|
+
|
73
|
+
if (status != 0) {
|
74
|
+
raise_olm_error("could not convert ed25519 secret key to curve25519");
|
75
|
+
}
|
76
|
+
|
77
|
+
status = self_olm_import_account(
|
78
|
+
this,
|
79
|
+
ed25519_sk,
|
80
|
+
ed25519_pk,
|
81
|
+
curve25519_sk,
|
82
|
+
curve25519_pk
|
83
|
+
);
|
84
|
+
|
85
|
+
if (status == -1) {
|
86
|
+
raise_olm_error(self_olm_account_last_error(this));
|
55
87
|
}
|
56
88
|
} else {
|
57
|
-
size =
|
89
|
+
size = self_olm_create_account_random_length(this);
|
58
90
|
|
59
|
-
if(
|
60
|
-
raise_olm_error(
|
91
|
+
if (self_olm_create_account(this, RSTRING_PTR(get_random(size)), size) == -1) {
|
92
|
+
raise_olm_error(self_olm_account_last_error(this));
|
61
93
|
}
|
62
94
|
}
|
63
95
|
|
64
96
|
return self;
|
65
97
|
}
|
66
98
|
|
67
|
-
static VALUE identity_keys(VALUE self)
|
68
|
-
|
69
|
-
OlmAccount *this;
|
99
|
+
static VALUE identity_keys(VALUE self) {
|
100
|
+
OlmAccount * this;
|
70
101
|
size_t size;
|
71
102
|
Data_Get_Struct(self, OlmAccount, this);
|
72
103
|
|
73
|
-
size =
|
104
|
+
size = self_olm_account_identity_keys_length(this);
|
74
105
|
uint8_t buf[size];
|
75
106
|
|
76
|
-
if(
|
77
|
-
|
78
|
-
raise_olm_error(olm_account_last_error(this));
|
107
|
+
if (self_olm_account_identity_keys(this, buf, size) != size) {
|
108
|
+
raise_olm_error(self_olm_account_last_error(this));
|
79
109
|
}
|
80
110
|
|
81
|
-
return rb_funcall(rb_eval_string("JSON"), rb_intern("parse"), 1, rb_str_new((char *)buf, size));
|
111
|
+
return rb_funcall(rb_eval_string("JSON"), rb_intern("parse"), 1, rb_str_new((char * ) buf, size));
|
82
112
|
}
|
83
113
|
|
84
|
-
static VALUE generate_one_time_keys(VALUE self, VALUE number)
|
85
|
-
{
|
114
|
+
static VALUE generate_one_time_keys(VALUE self, VALUE number) {
|
86
115
|
size_t size;
|
87
|
-
OlmAccount *this;
|
116
|
+
OlmAccount * this;
|
88
117
|
Data_Get_Struct(self, OlmAccount, this);
|
89
118
|
|
90
|
-
size =
|
119
|
+
size = self_olm_account_generate_one_time_keys_random_length(this, NUM2SIZET(number));
|
91
120
|
|
92
|
-
if(
|
93
|
-
|
94
|
-
raise_olm_error(olm_account_last_error(this));
|
121
|
+
if (self_olm_account_generate_one_time_keys(this, NUM2SIZET(number), RSTRING_PTR(get_random(size)), size) == -1) {
|
122
|
+
raise_olm_error(self_olm_account_last_error(this));
|
95
123
|
}
|
96
124
|
|
97
125
|
return self;
|
98
126
|
}
|
99
127
|
|
100
|
-
static VALUE one_time_keys_size(VALUE self)
|
101
|
-
|
102
|
-
OlmAccount *this;
|
128
|
+
static VALUE one_time_keys_size(VALUE self) {
|
129
|
+
OlmAccount * this;
|
103
130
|
Data_Get_Struct(self, OlmAccount, this);
|
104
131
|
|
105
|
-
return SIZET2NUM(
|
132
|
+
return SIZET2NUM(self_olm_account_one_time_keys_length(this));
|
106
133
|
}
|
107
134
|
|
108
|
-
static VALUE one_time_keys(VALUE self)
|
109
|
-
{
|
135
|
+
static VALUE one_time_keys(VALUE self) {
|
110
136
|
VALUE retval;
|
111
137
|
size_t size;
|
112
|
-
void *ptr;
|
113
|
-
OlmAccount *this;
|
138
|
+
void * ptr;
|
139
|
+
OlmAccount * this;
|
114
140
|
Data_Get_Struct(self, OlmAccount, this);
|
115
141
|
|
116
|
-
size =
|
142
|
+
size = self_olm_account_one_time_keys_length(this);
|
117
143
|
|
118
144
|
// add an additional byte to stop this from overflowing
|
119
|
-
if((ptr = malloc(size+1)) == NULL){
|
120
|
-
|
145
|
+
if ((ptr = malloc(size + 1)) == NULL) {
|
121
146
|
rb_raise(rb_eNoMemError, "%s()", __FUNCTION__);
|
122
147
|
}
|
123
148
|
|
124
|
-
if(
|
125
|
-
|
149
|
+
if (self_olm_account_one_time_keys(this, ptr, size) != size) {
|
126
150
|
free(ptr);
|
127
|
-
raise_olm_error(
|
151
|
+
raise_olm_error(self_olm_account_last_error(this));
|
128
152
|
}
|
129
153
|
|
130
154
|
retval = rb_funcall(rb_eval_string("JSON"), rb_intern("parse"), 1, rb_str_new(ptr, size));
|
@@ -134,25 +158,22 @@ static VALUE one_time_keys(VALUE self)
|
|
134
158
|
return retval;
|
135
159
|
}
|
136
160
|
|
137
|
-
static VALUE sign(VALUE self, VALUE message)
|
138
|
-
{
|
161
|
+
static VALUE sign(VALUE self, VALUE message) {
|
139
162
|
VALUE retval;
|
140
163
|
size_t size;
|
141
|
-
void *ptr;
|
142
|
-
OlmAccount *this;
|
164
|
+
void * ptr;
|
165
|
+
OlmAccount * this;
|
143
166
|
Data_Get_Struct(self, OlmAccount, this);
|
144
167
|
|
145
|
-
size =
|
146
|
-
|
147
|
-
if((ptr = malloc(size)) == NULL){
|
168
|
+
size = self_olm_account_signature_length(this);
|
148
169
|
|
170
|
+
if ((ptr = malloc(size)) == NULL) {
|
149
171
|
rb_raise(rb_eNoMemError, "%s()", __FUNCTION__);
|
150
172
|
}
|
151
173
|
|
152
|
-
if(
|
153
|
-
|
174
|
+
if (self_olm_account_sign(this, RSTRING_PTR(message), RSTRING_LEN(message), ptr, size) == -1) {
|
154
175
|
free(ptr);
|
155
|
-
raise_olm_error(
|
176
|
+
raise_olm_error(self_olm_account_last_error(this));
|
156
177
|
}
|
157
178
|
|
158
179
|
retval = rb_str_new(ptr, size);
|
@@ -162,77 +183,72 @@ static VALUE sign(VALUE self, VALUE message)
|
|
162
183
|
return retval;
|
163
184
|
}
|
164
185
|
|
165
|
-
static VALUE mark_keys_as_published(VALUE self)
|
166
|
-
|
167
|
-
OlmAccount *this;
|
186
|
+
static VALUE mark_keys_as_published(VALUE self) {
|
187
|
+
OlmAccount * this;
|
168
188
|
Data_Get_Struct(self, OlmAccount, this);
|
169
189
|
|
170
|
-
(void)
|
190
|
+
(void) self_olm_account_mark_keys_as_published(this);
|
171
191
|
|
172
192
|
return self;
|
173
193
|
}
|
174
194
|
|
175
|
-
static VALUE max_number_of_one_time_keys(VALUE self)
|
176
|
-
|
177
|
-
OlmAccount *this;
|
195
|
+
static VALUE max_number_of_one_time_keys(VALUE self) {
|
196
|
+
OlmAccount * this;
|
178
197
|
Data_Get_Struct(self, OlmAccount, this);
|
179
198
|
|
180
|
-
return SIZET2NUM(
|
199
|
+
return SIZET2NUM(self_olm_account_max_number_of_one_time_keys(this));
|
181
200
|
}
|
182
201
|
|
183
|
-
static VALUE remove_one_time_keys(VALUE self, VALUE session)
|
184
|
-
|
185
|
-
OlmAccount *this;
|
202
|
+
static VALUE remove_one_time_keys(VALUE self, VALUE session) {
|
203
|
+
OlmAccount * this;
|
186
204
|
Data_Get_Struct(self, OlmAccount, this);
|
187
205
|
|
188
|
-
OlmSession *s;
|
206
|
+
OlmSession * s;
|
189
207
|
Data_Get_Struct(session, OlmSession, s);
|
190
208
|
|
191
|
-
if(
|
192
|
-
|
193
|
-
raise_olm_error(olm_account_last_error(this));
|
209
|
+
if (self_olm_remove_one_time_keys(this, s) == -1) {
|
210
|
+
raise_olm_error(self_olm_account_last_error(this));
|
194
211
|
}
|
195
212
|
|
196
213
|
return self;
|
197
214
|
}
|
198
215
|
|
199
|
-
static VALUE outbound_session(VALUE self, VALUE identity_key, VALUE pre_key)
|
200
|
-
{
|
216
|
+
static VALUE outbound_session(VALUE self, VALUE identity_key, VALUE pre_key) {
|
201
217
|
return rb_funcall(rb_eval_string("SelfCrypto::OutboundSession"), rb_intern("new"), 3, self, identity_key, pre_key);
|
202
218
|
}
|
203
219
|
|
204
|
-
static VALUE inbound_session(int argc, VALUE *argv, VALUE self)
|
205
|
-
{
|
206
|
-
|
220
|
+
static VALUE inbound_session(int argc, VALUE * argv, VALUE self) {
|
221
|
+
VALUE args[] = {
|
222
|
+
self,
|
223
|
+
Qnil,
|
224
|
+
Qnil
|
225
|
+
};
|
207
226
|
|
208
|
-
(void)rb_scan_args(argc, argv, "11", &args[1], &args[2]);
|
227
|
+
(void) rb_scan_args(argc, argv, "11", & args[1], & args[2]);
|
209
228
|
|
210
|
-
return rb_class_new_instance(sizeof(args)/sizeof(*args), args, rb_eval_string("SelfCrypto::InboundSession"));
|
229
|
+
return rb_class_new_instance(sizeof(args) / sizeof( * args), args, rb_eval_string("SelfCrypto::InboundSession"));
|
211
230
|
}
|
212
231
|
|
213
|
-
static VALUE to_pickle(int argc, VALUE *argv, VALUE self)
|
214
|
-
{
|
232
|
+
static VALUE to_pickle(int argc, VALUE * argv, VALUE self) {
|
215
233
|
VALUE password, retval;
|
216
|
-
OlmAccount *this;
|
217
|
-
void *ptr;
|
234
|
+
OlmAccount * this;
|
235
|
+
void * ptr;
|
218
236
|
size_t size;
|
219
237
|
Data_Get_Struct(self, OlmAccount, this);
|
220
238
|
|
221
|
-
(void)rb_scan_args(argc, argv, "01", &password);
|
239
|
+
(void) rb_scan_args(argc, argv, "01", & password);
|
222
240
|
|
223
241
|
password = (password == Qnil) ? rb_str_new2("") : password;
|
224
242
|
|
225
|
-
size =
|
226
|
-
|
227
|
-
if((ptr = malloc(size)) == NULL){
|
243
|
+
size = self_olm_pickle_account_length(this);
|
228
244
|
|
245
|
+
if ((ptr = malloc(size)) == NULL) {
|
229
246
|
rb_raise(rb_eNoMemError, "%s()", __FUNCTION__);
|
230
247
|
}
|
231
248
|
|
232
|
-
if(
|
233
|
-
|
249
|
+
if (self_olm_pickle_account(this, RSTRING_PTR(password), RSTRING_LEN(password), ptr, size) != size) {
|
234
250
|
free(ptr);
|
235
|
-
raise_olm_error(
|
251
|
+
raise_olm_error(self_olm_account_last_error(this));
|
236
252
|
}
|
237
253
|
|
238
254
|
retval = rb_str_new(ptr, size);
|
@@ -242,28 +258,18 @@ static VALUE to_pickle(int argc, VALUE *argv, VALUE self)
|
|
242
258
|
return retval;
|
243
259
|
}
|
244
260
|
|
245
|
-
static void _free(void *ptr)
|
246
|
-
{
|
247
|
-
olm_clear_account(ptr);
|
261
|
+
static void _free(void * ptr) {
|
248
262
|
free(ptr);
|
249
263
|
}
|
250
264
|
|
251
|
-
static VALUE _alloc(VALUE klass)
|
252
|
-
|
253
|
-
OlmAccount *
|
254
|
-
VALUE self;
|
255
|
-
|
256
|
-
self = Data_Wrap_Struct(klass, 0, _free, calloc(1, olm_account_size()));
|
257
|
-
|
258
|
-
Data_Get_Struct(self, OlmAccount, this);
|
265
|
+
static VALUE _alloc(VALUE klass) {
|
266
|
+
void *account_buf = calloc(1, self_olm_account_size());
|
267
|
+
OlmAccount *account = self_olm_account(account_buf);
|
259
268
|
|
260
|
-
(
|
261
|
-
|
262
|
-
return self;
|
269
|
+
return Data_Wrap_Struct(klass, 0, _free, account);
|
263
270
|
}
|
264
271
|
|
265
|
-
void account_init(void)
|
266
|
-
{
|
272
|
+
void account_init(void) {
|
267
273
|
VALUE cRubyOLM = rb_define_module("SelfCrypto");
|
268
274
|
VALUE cAccount = rb_define_class_under(cRubyOLM, "Account", rb_cObject);
|
269
275
|
|
@@ -282,4 +288,4 @@ void account_init(void)
|
|
282
288
|
rb_define_method(cAccount, "outbound_session", outbound_session, 2);
|
283
289
|
rb_define_method(cAccount, "inbound_session", inbound_session, -1);
|
284
290
|
rb_define_method(cAccount, "to_pickle", to_pickle, -1);
|
285
|
-
}
|
291
|
+
}
|
data/ext/self_crypto/extconf.rb
CHANGED
@@ -4,12 +4,10 @@ $CFLAGS = " -std=c99"
|
|
4
4
|
|
5
5
|
RbConfig::MAKEFILE_CONFIG['CC'] = ENV['CC'] if ENV['CC']
|
6
6
|
|
7
|
-
pkg_config('
|
7
|
+
pkg_config('stdc++')
|
8
8
|
pkg_config('self_omemo')
|
9
|
-
pkg_config("sodium")
|
10
9
|
|
11
|
-
abort "Missing
|
10
|
+
abort "Missing stdc++" unless have_library("stdc++")
|
12
11
|
abort "Missing omemo" unless have_library("self_omemo")
|
13
|
-
abort "Missing olm" unless have_library("self_olm")
|
14
12
|
|
15
13
|
create_makefile('self_crypto/self_crypto')
|
data/ext/self_crypto/omemo.c
CHANGED
@@ -1,77 +1,74 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
// Copyright 2020 Self Group Ltd. All Rights Reserved.
|
2
|
+
|
3
3
|
#include "self_omemo.h"
|
4
4
|
#include "self_crypto.h"
|
5
5
|
|
6
|
-
static VALUE initialize(int argc, VALUE *argv, VALUE self)
|
7
|
-
{
|
6
|
+
static VALUE initialize(int argc, VALUE * argv, VALUE self) {
|
8
7
|
VALUE identity;
|
9
|
-
GroupSession *this;
|
8
|
+
GroupSession * this;
|
10
9
|
|
11
10
|
Data_Get_Struct(self, GroupSession, this);
|
12
11
|
|
13
|
-
(void)rb_scan_args(argc, argv, "1", &identity);
|
12
|
+
(void) rb_scan_args(argc, argv, "1", & identity);
|
14
13
|
|
15
|
-
if(identity != Qnil){
|
16
|
-
if(rb_obj_is_kind_of(identity, rb_cString) != Qtrue){
|
14
|
+
if (identity != Qnil) {
|
15
|
+
if (rb_obj_is_kind_of(identity, rb_cString) != Qtrue) {
|
17
16
|
rb_raise(rb_eTypeError, "identity must be kind of String");
|
18
17
|
}
|
19
18
|
}
|
20
19
|
|
21
|
-
|
20
|
+
self_omemo_set_identity(this, RSTRING_PTR(identity));
|
22
21
|
|
23
22
|
return self;
|
24
23
|
}
|
25
24
|
|
26
|
-
static VALUE add_participant(VALUE self, VALUE identity, VALUE session)
|
27
|
-
|
28
|
-
|
29
|
-
OlmSession *s;
|
25
|
+
static VALUE add_participant(VALUE self, VALUE identity, VALUE session) {
|
26
|
+
GroupSession * this;
|
27
|
+
OlmSession * s;
|
30
28
|
|
31
29
|
Data_Get_Struct(self, GroupSession, this);
|
32
30
|
Data_Get_Struct(session, OlmSession, s);
|
33
31
|
|
34
|
-
if(rb_obj_is_kind_of(identity, rb_eval_string("String")) != Qtrue){
|
32
|
+
if (rb_obj_is_kind_of(identity, rb_eval_string("String")) != Qtrue) {
|
35
33
|
rb_raise(rb_eTypeError, "identity must be kind of String");
|
36
34
|
}
|
37
35
|
|
38
|
-
if(
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
36
|
+
if (
|
37
|
+
rb_obj_is_instance_of(session, rb_eval_string("SelfCrypto::Session")) != Qtrue &&
|
38
|
+
rb_obj_is_instance_of(session, rb_eval_string("SelfCrypto::InboundSession")) != Qtrue &&
|
39
|
+
rb_obj_is_instance_of(session, rb_eval_string("SelfCrypto::OutboundSession")) != Qtrue
|
40
|
+
) {
|
43
41
|
rb_raise(rb_eTypeError, "session must be an instance of SelfCrypto::Session, SelfCrypto::InboundSession or SelfCrypto::OutboundSession");
|
44
42
|
}
|
45
43
|
|
46
|
-
|
44
|
+
self_omemo_add_group_participant(this, RSTRING_PTR(identity), s);
|
47
45
|
|
48
46
|
return identity;
|
49
47
|
}
|
50
48
|
|
51
|
-
static VALUE group_encrypt(VALUE self, VALUE plaintext)
|
52
|
-
|
53
|
-
GroupSession *this;
|
49
|
+
static VALUE group_encrypt(VALUE self, VALUE plaintext) {
|
50
|
+
GroupSession * this;
|
54
51
|
VALUE ciphertext;
|
55
|
-
void *ptr;
|
52
|
+
void * ptr;
|
56
53
|
size_t ciphertext_sz;
|
57
54
|
|
58
55
|
Data_Get_Struct(self, GroupSession, this);
|
59
56
|
|
60
|
-
if(rb_obj_is_kind_of(plaintext, rb_eval_string("String")) != Qtrue){
|
57
|
+
if (rb_obj_is_kind_of(plaintext, rb_eval_string("String")) != Qtrue) {
|
61
58
|
rb_raise(rb_eTypeError, "plaintext must be kind of String");
|
62
59
|
}
|
63
60
|
|
64
|
-
ciphertext_sz =
|
61
|
+
ciphertext_sz = self_omemo_encrypted_size(this, RSTRING_LEN(plaintext));
|
65
62
|
|
66
63
|
if (ciphertext_sz == 0) {
|
67
64
|
rb_raise(rb_eTypeError, "could not get size of encrypted message");
|
68
65
|
}
|
69
66
|
|
70
|
-
if((ptr = malloc(ciphertext_sz)) == NULL){
|
67
|
+
if ((ptr = malloc(ciphertext_sz)) == NULL) {
|
71
68
|
rb_raise(rb_eNoMemError, "%s()", __FUNCTION__);
|
72
69
|
}
|
73
70
|
|
74
|
-
ciphertext_sz =
|
71
|
+
ciphertext_sz = self_omemo_encrypt(
|
75
72
|
this,
|
76
73
|
RSTRING_PTR(plaintext),
|
77
74
|
RSTRING_LEN(plaintext),
|
@@ -79,7 +76,7 @@ static VALUE group_encrypt(VALUE self, VALUE plaintext)
|
|
79
76
|
ciphertext_sz
|
80
77
|
);
|
81
78
|
|
82
|
-
if(ciphertext_sz == 0) {
|
79
|
+
if (ciphertext_sz == 0) {
|
83
80
|
free(ptr);
|
84
81
|
rb_raise(rb_eTypeError, "failed to encrypt");
|
85
82
|
}
|
@@ -91,34 +88,33 @@ static VALUE group_encrypt(VALUE self, VALUE plaintext)
|
|
91
88
|
return ciphertext;
|
92
89
|
}
|
93
90
|
|
94
|
-
static VALUE group_decrypt(VALUE self, VALUE sender, VALUE ciphertext)
|
95
|
-
|
96
|
-
GroupSession *this;
|
91
|
+
static VALUE group_decrypt(VALUE self, VALUE sender, VALUE ciphertext) {
|
92
|
+
GroupSession * this;
|
97
93
|
VALUE plaintext;
|
98
|
-
void *ptr;
|
94
|
+
void * ptr;
|
99
95
|
size_t plaintext_sz;
|
100
96
|
|
101
97
|
Data_Get_Struct(self, GroupSession, this);
|
102
98
|
|
103
|
-
if(rb_obj_is_kind_of(sender, rb_eval_string("String")) != Qtrue){
|
99
|
+
if (rb_obj_is_kind_of(sender, rb_eval_string("String")) != Qtrue) {
|
104
100
|
rb_raise(rb_eTypeError, "sender must be kind of String");
|
105
101
|
}
|
106
102
|
|
107
|
-
if(rb_obj_is_kind_of(ciphertext, rb_eval_string("String")) != Qtrue){
|
103
|
+
if (rb_obj_is_kind_of(ciphertext, rb_eval_string("String")) != Qtrue) {
|
108
104
|
rb_raise(rb_eTypeError, "ciphertext must be kind of String");
|
109
105
|
}
|
110
106
|
|
111
|
-
plaintext_sz =
|
107
|
+
plaintext_sz = self_omemo_decrypted_size(this, RSTRING_PTR(ciphertext), RSTRING_LEN(ciphertext));
|
112
108
|
|
113
109
|
if (plaintext_sz == 0) {
|
114
110
|
rb_raise(rb_eTypeError, "could not get size of decrypted message");
|
115
111
|
}
|
116
112
|
|
117
|
-
if((ptr = malloc(plaintext_sz)) == NULL){
|
113
|
+
if ((ptr = malloc(plaintext_sz)) == NULL) {
|
118
114
|
rb_raise(rb_eNoMemError, "%s()", __FUNCTION__);
|
119
115
|
}
|
120
116
|
|
121
|
-
plaintext_sz =
|
117
|
+
plaintext_sz = self_omemo_decrypt(
|
122
118
|
this,
|
123
119
|
RSTRING_PTR(sender),
|
124
120
|
ptr,
|
@@ -127,7 +123,7 @@ static VALUE group_decrypt(VALUE self, VALUE sender, VALUE ciphertext)
|
|
127
123
|
RSTRING_LEN(ciphertext)
|
128
124
|
);
|
129
125
|
|
130
|
-
if(plaintext_sz == 0) {
|
126
|
+
if (plaintext_sz == 0) {
|
131
127
|
free(ptr);
|
132
128
|
rb_raise(rb_eTypeError, "failed to decrypt");
|
133
129
|
}
|
@@ -139,25 +135,22 @@ static VALUE group_decrypt(VALUE self, VALUE sender, VALUE ciphertext)
|
|
139
135
|
return plaintext;
|
140
136
|
}
|
141
137
|
|
142
|
-
static void _free(void *ptr)
|
143
|
-
|
144
|
-
omemo_destroy_group_session(ptr);
|
138
|
+
static void _free(void * ptr) {
|
139
|
+
self_omemo_destroy_group_session(ptr);
|
145
140
|
}
|
146
141
|
|
147
|
-
static VALUE _alloc(VALUE klass)
|
148
|
-
|
149
|
-
GroupSession *this;
|
142
|
+
static VALUE _alloc(VALUE klass) {
|
143
|
+
GroupSession * this;
|
150
144
|
VALUE self;
|
151
145
|
|
152
|
-
self = Data_Wrap_Struct(klass, 0, _free,
|
146
|
+
self = Data_Wrap_Struct(klass, 0, _free, self_omemo_create_group_session());
|
153
147
|
|
154
148
|
Data_Get_Struct(self, GroupSession, this);
|
155
149
|
|
156
150
|
return self;
|
157
151
|
}
|
158
152
|
|
159
|
-
void group_session_init()
|
160
|
-
{
|
153
|
+
void group_session_init() {
|
161
154
|
VALUE cRubyOLM = rb_define_module("SelfCrypto");
|
162
155
|
VALUE cGroupSession = rb_define_class_under(cRubyOLM, "GroupSession", rb_cObject);
|
163
156
|
|
@@ -167,4 +160,4 @@ void group_session_init()
|
|
167
160
|
rb_define_method(cGroupSession, "add_participant", add_participant, 2);
|
168
161
|
rb_define_method(cGroupSession, "encrypt", group_encrypt, 1);
|
169
162
|
rb_define_method(cGroupSession, "decrypt", group_decrypt, 2);
|
170
|
-
}
|
163
|
+
}
|