self_crypto 0.0.8 → 0.0.10

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.
@@ -2,66 +2,36 @@
2
2
  #include <assert.h>
3
3
  #include <stdbool.h>
4
4
  #include <stdlib.h>
5
-
6
- #include "sodium.h"
7
- #include "self_olm/olm.h"
8
5
  #include "self_omemo.h"
9
6
 
10
7
  void account_init(void);
11
8
  void session_init(void);
12
9
  void utility_init(void);
13
10
  void group_session_init(void);
14
- void pk_init(void);
15
- void sas_init(void);
16
-
17
- static VALUE get_olm_version(VALUE self)
18
- {
19
- char buffer[20U];
20
- uint8_t major = 0U;
21
- uint8_t minor = 0U;
22
- uint8_t patch = 0U;
23
-
24
- olm_get_library_version(&major, &minor, &patch);
25
-
26
- snprintf(buffer, sizeof(buffer), "%u.%u.%u", major, minor, patch);
27
-
28
- return rb_str_new2(buffer);
29
- }
30
11
 
31
- void Init_self_crypto(void)
32
- {
12
+ void Init_self_crypto(void) {
33
13
  rb_require("json");
34
14
  rb_require("self_crypto/olm_error");
35
15
 
36
- rb_define_singleton_method(rb_eval_string("SelfCrypto"), "olm_version", get_olm_version, 0);
37
-
38
16
  account_init();
39
17
  session_init();
40
18
  group_session_init();
41
19
  utility_init();
42
- pk_init();
43
- sas_init();
44
20
  }
45
21
 
46
- void raise_olm_error(const char *error)
47
- {
22
+ void raise_olm_error(const char * error) {
48
23
  rb_funcall(rb_eval_string("SelfCrypto::OlmError"), rb_intern("raise_from_string"), 1, rb_str_new2(error));
49
24
  }
50
25
 
51
- VALUE get_random(size_t size)
52
- {
26
+ VALUE get_random(size_t size) {
53
27
  VALUE rand;
54
- void *ptr;
28
+ void * ptr;
55
29
 
56
- if (sodium_init() == -1) {
30
+ if ((ptr = malloc(size)) == NULL) {
57
31
  rb_raise(rb_eNoMemError, "%s()", __FUNCTION__);
58
32
  }
59
33
 
60
- if ((ptr = malloc(size)) == NULL){
61
- rb_raise(rb_eNoMemError, "%s()", __FUNCTION__);
62
- }
63
-
64
- randombytes_buf(ptr, size);
34
+ self_randombytes_buf(ptr, size);
65
35
 
66
36
  rand = rb_str_new(ptr, size);
67
37
 
@@ -70,15 +40,14 @@ VALUE get_random(size_t size)
70
40
  return rand;
71
41
  }
72
42
 
73
- VALUE dup_string(VALUE str)
74
- {
43
+ VALUE dup_string(VALUE str) {
75
44
  return rb_str_new(RSTRING_PTR(str), RSTRING_LEN(str));
76
45
  }
77
46
 
78
- void* malloc_or_raise(size_t len) {
47
+ void * malloc_or_raise(size_t len) {
79
48
  void * ptr = malloc(len);
80
49
  if (ptr == NULL) {
81
50
  rb_raise(rb_eNoMemError, "%s()", __FUNCTION__);
82
51
  }
83
52
  return ptr;
84
- }
53
+ }
@@ -1,316 +1,182 @@
1
- #include "self_olm/olm.h"
1
+ #include "self_omemo.h"
2
2
  #include "self_crypto.h"
3
3
 
4
- static VALUE last_error(VALUE self)
5
- {
6
- OlmSession *this;
4
+ static VALUE last_error(VALUE self) {
5
+ OlmSession * this;
7
6
  Data_Get_Struct(self, OlmSession, this);
8
7
 
9
- return rb_str_new2(olm_session_last_error(this));
8
+ return rb_str_new2(self_olm_session_last_error(this));
10
9
  }
11
10
 
12
- static VALUE initialize(int argc, VALUE *argv, VALUE self)
13
- {
11
+ static VALUE initialize(int argc, VALUE * argv, VALUE self) {
14
12
  VALUE pickle, password;
15
- OlmSession *this;
13
+ OlmSession * this;
16
14
  Data_Get_Struct(self, OlmSession, this);
17
15
 
18
- (void)rb_scan_args(argc, argv, "11", &pickle, &password);
19
-
20
- if(rb_obj_is_kind_of(pickle, rb_cString) != Qtrue){
16
+ (void) rb_scan_args(argc, argv, "11", & pickle, & password);
21
17
 
18
+ if (rb_obj_is_kind_of(pickle, rb_cString) != Qtrue) {
22
19
  rb_raise(rb_eTypeError, "pickle must be kind of String");
23
20
  }
24
21
 
25
- if(password != Qnil){
26
-
27
- if(rb_obj_is_kind_of(password, rb_cString) != Qtrue){
28
-
22
+ if (password != Qnil) {
23
+ if (rb_obj_is_kind_of(password, rb_cString) != Qtrue) {
29
24
  rb_raise(rb_eTypeError, "password must be kind of String");
30
25
  }
31
- }
32
- else{
33
-
26
+ } else {
34
27
  password = rb_str_new2("");
35
28
  }
36
29
 
37
- if(olm_unpickle_session(this, RSTRING_PTR(password), RSTRING_LEN(password), RSTRING_PTR(dup_string(pickle)), RSTRING_LEN(pickle)) == olm_error()){
38
-
39
- raise_olm_error(olm_session_last_error(this));
30
+ if (self_olm_unpickle_session(this, RSTRING_PTR(password), RSTRING_LEN(password), RSTRING_PTR(dup_string(pickle)), RSTRING_LEN(pickle)) == -1) {
31
+ raise_olm_error(self_olm_session_last_error(this));
40
32
  }
41
33
 
42
34
  return self;
43
35
  }
44
36
 
45
- static VALUE initialize_outbound(VALUE self, VALUE account, VALUE identity, VALUE one_time_key)
46
- {
37
+ static VALUE initialize_outbound(VALUE self, VALUE account, VALUE identity, VALUE one_time_key) {
47
38
  size_t size;
48
- OlmSession *this;
49
- OlmAccount *a;
39
+ OlmSession * this;
40
+ OlmAccount * a;
50
41
 
51
42
  Data_Get_Struct(self, OlmSession, this);
52
43
  Data_Get_Struct(account, OlmAccount, a);
53
44
 
54
- size = olm_create_outbound_session_random_length(this);
55
-
56
- if(rb_obj_is_instance_of(account, rb_eval_string("SelfCrypto::Account")) != Qtrue){
45
+ size = self_olm_create_outbound_session_random_length(this);
57
46
 
47
+ if (rb_obj_is_instance_of(account, rb_eval_string("SelfCrypto::Account")) != Qtrue) {
58
48
  rb_raise(rb_eTypeError, "account must be an instance of SelfCrypto::Account");
59
49
  }
60
- if(rb_obj_is_kind_of(identity, rb_eval_string("String")) != Qtrue){
61
-
50
+ if (rb_obj_is_kind_of(identity, rb_eval_string("String")) != Qtrue) {
62
51
  rb_raise(rb_eTypeError, "identity must be kind of String");
63
52
  }
64
- if(rb_obj_is_kind_of(one_time_key, rb_eval_string("String")) != Qtrue){
65
-
53
+ if (rb_obj_is_kind_of(one_time_key, rb_eval_string("String")) != Qtrue) {
66
54
  rb_raise(rb_eTypeError, "one_time_key must be kind of String");
67
55
  }
68
56
 
69
- if(olm_create_outbound_session(this, a,
57
+ if (self_olm_create_outbound_session(
58
+ this,
59
+ a,
70
60
  RSTRING_PTR(identity), RSTRING_LEN(identity),
71
61
  RSTRING_PTR(one_time_key), RSTRING_LEN(one_time_key),
72
62
  RSTRING_PTR(get_random(size)), size
73
- ) == olm_error()){
74
- raise_olm_error(olm_session_last_error(this));
63
+ ) == -1) {
64
+ raise_olm_error(self_olm_session_last_error(this));
75
65
  }
76
66
 
77
67
  return self;
78
68
  }
79
69
 
80
- static VALUE initialize_inbound(int argc, VALUE *argv, VALUE self)
81
- {
70
+ static VALUE initialize_inbound(int argc, VALUE * argv, VALUE self) {
82
71
  VALUE account, one_time_message, identity;
83
72
 
84
73
  identity = Qnil;
85
74
 
86
- (void)rb_scan_args(argc, argv, "21", &account, &one_time_message, &identity);
75
+ (void) rb_scan_args(argc, argv, "21", & account, & one_time_message, & identity);
87
76
 
88
- OlmSession *this;
77
+ OlmSession * this;
89
78
  Data_Get_Struct(self, OlmSession, this);
90
79
 
91
- OlmAccount *a;
80
+ OlmAccount * a;
92
81
  Data_Get_Struct(account, OlmAccount, a);
93
82
 
94
- if(rb_obj_is_kind_of(one_time_message, rb_eval_string("SelfCrypto::PreKeyMessage")) != Qtrue){
95
-
83
+ if (rb_obj_is_kind_of(one_time_message, rb_eval_string("SelfCrypto::PreKeyMessage")) != Qtrue) {
96
84
  rb_raise(rb_eTypeError, "one_time_message must be kind of PreKeyMessage");
97
85
  }
98
86
 
99
87
  one_time_message = rb_funcall(one_time_message, rb_intern("to_s"), 0);
100
88
 
101
- if(identity == Qnil){
102
-
103
- if(olm_create_inbound_session(this, a,
89
+ if (identity == Qnil) {
90
+ if (self_olm_create_inbound_session(this, a,
104
91
  RSTRING_PTR(dup_string(one_time_message)), RSTRING_LEN(one_time_message)
105
- ) == olm_error()){
106
- raise_olm_error(olm_session_last_error(this));
92
+ ) == -1) {
93
+ raise_olm_error(self_olm_session_last_error(this));
107
94
  }
108
- }
109
- else{
110
-
111
- if(olm_create_inbound_session_from(this, a,
95
+ } else {
96
+ if (self_olm_create_inbound_session_from(this, a,
112
97
  RSTRING_PTR(identity), RSTRING_LEN(identity),
113
98
  RSTRING_PTR(dup_string(one_time_message)), RSTRING_LEN(one_time_message)
114
- ) == olm_error()){
115
- raise_olm_error(olm_session_last_error(this));
99
+ ) == -1) {
100
+ raise_olm_error(self_olm_session_last_error(this));
116
101
  }
117
102
  }
118
103
 
119
104
  return self;
120
105
  }
121
106
 
122
- static VALUE has_received_message(VALUE self)
123
- {
124
- OlmSession *this;
125
- Data_Get_Struct(self, OlmSession, this);
126
-
127
- return (olm_session_has_received_message(this) == 0) ? Qfalse : Qtrue;
128
- }
129
-
130
- static VALUE get_session_id(VALUE self)
131
- {
132
- OlmSession *this;
133
- Data_Get_Struct(self, OlmSession, this);
134
-
135
- size_t size = olm_session_id_length(this);
136
- uint8_t buf[size];
137
-
138
- if(olm_session_id(this, buf, size) != size){
139
-
140
- raise_olm_error(olm_session_last_error(this));
141
- }
142
-
143
- return rb_str_new((char *)buf, size);
144
- }
145
-
146
- static VALUE will_receive(int argc, VALUE *argv, VALUE self)
147
- {
107
+ static VALUE will_receive(int argc, VALUE * argv, VALUE self) {
148
108
  VALUE one_time_message, identity;
149
109
  size_t result;
150
- OlmSession *this;
110
+ OlmSession * this;
151
111
  Data_Get_Struct(self, OlmSession, this);
152
112
 
153
113
  identity = Qnil;
154
114
 
155
- (void)rb_scan_args(argc, argv, "11", &one_time_message, &identity);
156
-
157
- if(rb_obj_is_kind_of(one_time_message, rb_eval_string("SelfCrypto::PreKeyMessage")) != Qtrue){
115
+ (void) rb_scan_args(argc, argv, "11", & one_time_message, & identity);
158
116
 
117
+ if (rb_obj_is_kind_of(one_time_message, rb_eval_string("SelfCrypto::PreKeyMessage")) != Qtrue) {
159
118
  rb_raise(rb_eTypeError, "one_time_message must be kind of PreKeyMessage");
160
119
  }
161
120
 
162
121
  one_time_message = rb_funcall(one_time_message, rb_intern("to_s"), 0);
163
122
 
164
- if(identity == Qnil){
165
-
166
- result = olm_matches_inbound_session(this,
167
- RSTRING_PTR(dup_string(one_time_message)), RSTRING_LEN(one_time_message)
123
+ if (identity == Qnil) {
124
+ result = self_olm_matches_inbound_session(this,
125
+ RSTRING_PTR(dup_string(one_time_message)), RSTRING_LEN(one_time_message)
168
126
  );
169
- }
170
- else{
171
-
172
- result = olm_matches_inbound_session_from(this,
173
- RSTRING_PTR(identity), RSTRING_LEN(identity),
174
- RSTRING_PTR(dup_string(one_time_message)), RSTRING_LEN(one_time_message)
127
+ } else {
128
+ result = self_olm_matches_inbound_session_from(this,
129
+ RSTRING_PTR(identity), RSTRING_LEN(identity),
130
+ RSTRING_PTR(dup_string(one_time_message)), RSTRING_LEN(one_time_message)
175
131
  );
176
132
  }
177
133
 
178
- if(result == olm_error()){
134
+ if (result == -1) {
179
135
 
180
- raise_olm_error(olm_session_last_error(this));
136
+ raise_olm_error(self_olm_session_last_error(this));
181
137
  }
182
138
 
183
139
  return (result == 1) ? Qtrue : Qfalse;
184
140
  }
185
141
 
186
- static VALUE message_type(VALUE self)
187
- {
188
- OlmSession *this;
142
+ static VALUE message_type(VALUE self) {
143
+ OlmSession * this;
189
144
  VALUE retval;
190
145
  Data_Get_Struct(self, OlmSession, this);
191
146
 
192
- if(olm_encrypt_message_type(this) == OLM_MESSAGE_TYPE_PRE_KEY){
193
-
147
+ if (self_olm_encrypt_message_type(this) == 0) {
194
148
  retval = rb_eval_string("SelfCrypto::PreKeyMessage");
195
- }
196
- else if(olm_encrypt_message_type(this) == OLM_MESSAGE_TYPE_MESSAGE){
197
-
149
+ } else if (self_olm_encrypt_message_type(this) == 1) {
198
150
  retval = rb_eval_string("SelfCrypto::Message");
199
- }
200
- else{
201
-
151
+ } else {
202
152
  rb_bug("olm_encrypt_message_type()");
203
153
  }
204
154
 
205
155
  return retval;
206
156
  }
207
157
 
208
- static VALUE session_encrypt(VALUE self, VALUE plain)
209
- {
210
- size_t cipher_size, random_size;
211
- void *ptr;
212
- OlmSession *this;
213
- VALUE retval, type;
214
- Data_Get_Struct(self, OlmSession, this);
215
-
216
- cipher_size = olm_encrypt_message_length(this, RSTRING_LEN(plain));
217
- random_size = olm_encrypt_random_length(this);
218
-
219
- if((ptr = malloc(cipher_size)) == NULL){
220
-
221
- rb_raise(rb_eNoMemError, "%s()", __FUNCTION__);
222
- }
223
-
224
- type = message_type(self);
225
-
226
- if(olm_encrypt(this, RSTRING_PTR(plain), RSTRING_LEN(plain),
227
- RSTRING_PTR(get_random(random_size)), random_size,
228
- ptr, cipher_size
229
- ) == olm_error()){
230
- free(ptr);
231
- raise_olm_error(olm_session_last_error(this));
232
- }
233
-
234
- retval = rb_funcall(type, rb_intern("new"), 1, rb_str_new(ptr, cipher_size));
235
-
236
- free(ptr);
237
-
238
- return retval;
239
- }
240
-
241
- static VALUE session_decrypt(VALUE self, VALUE cipher)
242
- {
243
- size_t plain_size, plain_max, type;
244
- void *ptr;
245
- OlmSession *this;
246
- VALUE retval, data;
247
- Data_Get_Struct(self, OlmSession, this);
248
-
249
- if(rb_obj_is_kind_of(cipher, rb_eval_string("SelfCrypto::Message")) == Qtrue){
250
-
251
- type = OLM_MESSAGE_TYPE_MESSAGE;
252
- }
253
- else if(rb_obj_is_kind_of(cipher, rb_eval_string("SelfCrypto::PreKeyMessage")) == Qtrue){
254
-
255
- type = OLM_MESSAGE_TYPE_PRE_KEY;
256
- }
257
- else{
258
-
259
- rb_raise(rb_eTypeError, "cipher must be kind of Message or PreKeyMessage");
260
- }
261
-
262
- data = rb_funcall(cipher, rb_intern("to_s"), 0);
263
-
264
- if((plain_max = olm_decrypt_max_plaintext_length(this, type, RSTRING_PTR(dup_string(data)), RSTRING_LEN(data))) == olm_error()){
265
-
266
- raise_olm_error(olm_session_last_error(this));
267
- }
268
-
269
- /* size of output will be less than size of input */
270
- if((ptr = malloc(plain_max)) == NULL){
271
-
272
- rb_raise(rb_eNoMemError, "%s()", __FUNCTION__);
273
- }
274
-
275
- if((plain_size = olm_decrypt(this,
276
- type,
277
- RSTRING_PTR(dup_string(data)), RSTRING_LEN(data),
278
- ptr, plain_max)) == olm_error())
279
- {
280
- free(ptr);
281
- raise_olm_error(olm_session_last_error(this));
282
- }
283
-
284
- retval = rb_str_new(ptr, plain_size);
285
-
286
- free(ptr);
287
-
288
- return retval;
289
- }
290
-
291
- static VALUE to_pickle(int argc, VALUE *argv, VALUE self)
292
- {
158
+ static VALUE to_pickle(int argc, VALUE * argv, VALUE self) {
293
159
  VALUE password, retval;
294
- OlmSession *this;
295
- void *ptr;
160
+ OlmSession * this;
161
+ void * ptr;
296
162
  size_t size;
297
163
  Data_Get_Struct(self, OlmSession, this);
298
164
 
299
- (void)rb_scan_args(argc, argv, "01", &password);
165
+ (void) rb_scan_args(argc, argv, "01", & password);
300
166
 
301
167
  password = (password == Qnil) ? rb_str_new2("") : password;
302
168
 
303
- size = olm_pickle_session_length(this);
169
+ size = self_olm_pickle_session_length(this);
304
170
 
305
- if((ptr = malloc(size)) == NULL){
171
+ if ((ptr = malloc(size)) == NULL) {
306
172
 
307
173
  rb_raise(rb_eNoMemError, "%s()", __FUNCTION__);
308
174
  }
309
175
 
310
- if(olm_pickle_session(this, RSTRING_PTR(password), RSTRING_LEN(password), ptr, size) != size){
176
+ if (self_olm_pickle_session(this, RSTRING_PTR(password), RSTRING_LEN(password), ptr, size) != size) {
311
177
 
312
178
  free(ptr);
313
- raise_olm_error(olm_session_last_error(this));
179
+ raise_olm_error(self_olm_session_last_error(this));
314
180
  }
315
181
 
316
182
  retval = rb_str_new(ptr, size);
@@ -320,28 +186,18 @@ static VALUE to_pickle(int argc, VALUE *argv, VALUE self)
320
186
  return retval;
321
187
  }
322
188
 
323
- static void _free(void *ptr)
324
- {
325
- olm_clear_session(ptr);
189
+ static void _free(void * ptr) {
326
190
  free(ptr);
327
191
  }
328
192
 
329
- static VALUE _alloc(VALUE klass)
330
- {
331
- OlmSession *this;
332
- VALUE self;
333
-
334
- self = Data_Wrap_Struct(klass, 0, _free, calloc(1, olm_session_size()));
193
+ static VALUE _alloc(VALUE klass) {
194
+ void *session_buf = calloc(1, self_olm_session_size());
195
+ OlmSession *session = self_olm_session(session_buf);
335
196
 
336
- Data_Get_Struct(self, OlmSession, this);
337
-
338
- (void)olm_session((void *)this);
339
-
340
- return self;
197
+ return Data_Wrap_Struct(klass, 0, _free, session);
341
198
  }
342
199
 
343
- void session_init(void)
344
- {
200
+ void session_init(void) {
345
201
  VALUE cRubyOLM = rb_define_module("SelfCrypto");
346
202
  VALUE cSession = rb_define_class_under(cRubyOLM, "Session", rb_cObject);
347
203
  VALUE cSessionOut = rb_define_class_under(cRubyOLM, "OutboundSession", cSession);
@@ -353,11 +209,7 @@ void session_init(void)
353
209
  rb_define_method(cSessionIn, "initialize", initialize_inbound, -1);
354
210
 
355
211
  rb_define_method(cSession, "initialize", initialize, -1);
356
- rb_define_method(cSession, "id", get_session_id, 0);
357
212
  rb_define_method(cSession, "last_error", last_error, 0);
358
- rb_define_method(cSession, "has_received_message", has_received_message, 0);
359
- rb_define_method(cSession, "encrypt", session_encrypt, 1);
360
- rb_define_method(cSession, "decrypt", session_decrypt, 1);
361
213
  rb_define_method(cSession, "to_pickle", to_pickle, -1);
362
214
  rb_define_method(cSession, "will_receive?", will_receive, -1);
363
- }
215
+ }