Class: SelfCrypto::SAS

Inherits:
Data
  • Object
show all
Defined in:
lib/self_crypto/sas.rb,
lib/self_crypto/sas_data.rb,
ext/self_crypto/sas.c

Constant Summary collapse

METHODS =
i[decimal emoji]
EMOJI_TABLE =
{
  0 => '🐶',
  1 => '🐱',
  2 => '🦁',
  3 => '🐎',
  4 => '🦄',
  5 => '🐷',
  6 => '🐘',
  7 => '🐰',
  8 => '🐼',
  9 => '🐓',
  10 => '🐧',
  11 => '🐢',
  12 => '🐟',
  13 => '🐙',
  14 => '🦋',
  15 => '🌷',
  16 => '🌳',
  17 => '🌵',
  18 => '🍄',
  19 => '🌏',
  20 => '🌙',
  21 => '☁',
  22 => '🔥',
  23 => '🍌',
  24 => '🍎',
  25 => '🍓',
  26 => '🌽',
  27 => '🍕',
  28 => '🎂',
  29 => '❤',
  30 => '😀',
  31 => '🤖',
  32 => '🎩',
  33 => '👓',
  34 => '🔧',
  35 => '🎅',
  36 => '👍',
  37 => '☂',
  38 => '⌛',
  39 => '⏰',
  40 => '🎁',
  41 => '💡',
  42 => '📕',
  43 => '✏',
  44 => '📎',
  45 => '✂',
  46 => '🔒',
  47 => '🔑',
  48 => '🔨',
  49 => '☎',
  50 => '🏁',
  51 => '🚂',
  52 => '🚲',
  53 => '✈',
  54 => '🚀',
  55 => '🏆',
  56 => '⚽',
  57 => '🎸',
  58 => '🎺',
  59 => '🔔',
  60 => '⚓',
  61 => '🎧',
  62 => '📁',
  63 => '📌'
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'ext/self_crypto/sas.c', line 41

static VALUE initialize(int argc, VALUE *argv, VALUE self) {
    OlmSAS *this;
    VALUE random;
    VALUE other_pubkey;
    TypedData_Get_Struct(self, OlmSAS, &olm_sas_type, this);

    rb_scan_args(argc, argv, "01", &other_pubkey);

    random = get_random(olm_create_sas_random_length(this));
    if (olm_create_sas(this, RSTRING_PTR(random), RSTRING_LEN(random)) == olm_error()) {
        raise_olm_error(olm_sas_last_error(this));
    }

    // Make sure @other_public_key is set
    rb_iv_set(self, "@other_public_key", Qnil);

    if (!NIL_P(other_pubkey)) {
        set_other_pubkey(self, other_pubkey);
    }

    return self;
}

Instance Attribute Details

#other_public_keyObject (readonly)

Instance Method Details

#calculate_mac(message, info) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'ext/self_crypto/sas.c', line 126

static VALUE calculate_mac(VALUE self, VALUE message, VALUE info) {
    OlmSAS *this;
    size_t mac_len;
    char *mac;
    VALUE retval;
    TypedData_Get_Struct(self, OlmSAS, &olm_sas_type, this);
    Check_Type(message, T_STRING);
    Check_Type(info, T_STRING);

    mac_len = olm_sas_mac_length(this);
    mac = malloc_or_raise(mac_len);

    if (olm_sas_calculate_mac(this,
                              RSTRING_PTR(message), RSTRING_LEN(message),
                              RSTRING_PTR(info), RSTRING_LEN(info),
                              mac, mac_len) == olm_error()) {
        free(mac);
        raise_olm_error(olm_sas_last_error(this));
    }

    retval = rb_str_new(mac, mac_len);
    free(mac);
    return retval;
}

#calculate_mac_long_kdf(message, info) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'ext/self_crypto/sas.c', line 151

static VALUE calculate_mac_long_kdf(VALUE self, VALUE message, VALUE info) {
    OlmSAS *this;
    size_t mac_len;
    char *mac;
    VALUE retval;
    TypedData_Get_Struct(self, OlmSAS, &olm_sas_type, this);
    Check_Type(message, T_STRING);
    Check_Type(info, T_STRING);

    mac_len = olm_sas_mac_length(this);
    mac = malloc_or_raise(mac_len);

    if (olm_sas_calculate_mac_long_kdf(this,
                              RSTRING_PTR(message), RSTRING_LEN(message),
                              RSTRING_PTR(info), RSTRING_LEN(info),
                              mac, mac_len) == olm_error()) {
        free(mac);
        raise_olm_error(olm_sas_last_error(this));
    }

    retval = rb_str_new(mac, mac_len);
    free(mac);
    return retval;
}

#generate(method, info) ⇒ Object

Raises:

  • (ArgumentError)


6
7
8
9
10
11
# File 'lib/self_crypto/sas.rb', line 6

def generate(method, info)
  method = method.to_sym
  raise ArgumentError, "Unknown SAS method: #{method}" unless METHODS.include? method

  send method, info
end

#generate_bytes(count, info) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'ext/self_crypto/sas.c', line 103

static VALUE generate_bytes(VALUE self, VALUE count, VALUE info) {
    OlmSAS *this;
    size_t output_len;
    char *output;
    VALUE retval;
    TypedData_Get_Struct(self, OlmSAS, &olm_sas_type, this);
    Check_Type(count, T_FIXNUM);
    Check_Type(info, T_STRING);

    output_len = NUM2ULONG(count);
    output = malloc_or_raise(output_len);

    if (olm_sas_generate_bytes(this, RSTRING_PTR(info), RSTRING_LEN(info), output, output_len) == olm_error()) {
        free(output);
        raise_olm_error(olm_sas_last_error(this));
    }

    retval = rb_str_new(output, output_len);
    free(output);
    // Return raw byte string here, higher abstraction in Ruby
    return retval;
}

#other_public_key=(other_public_key) ⇒ Object (readonly)



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'ext/self_crypto/sas.c', line 64

static VALUE set_other_pubkey(VALUE self, VALUE other_public_key) {
    OlmSAS *this;
    TypedData_Get_Struct(self, OlmSAS, &olm_sas_type, this);
    Check_Type(other_public_key, T_STRING);

    if (RSTRING_LEN(other_public_key) != olm_sas_pubkey_length(this)) {
        rb_raise(rb_eval_string("ArgumentError"), "other_public_key has wrong size (must be %lu)", olm_sas_pubkey_length(this));
    }

    // olm_sas_set_their_key trashes other_public_key, and rb_str_dup only creates a shallow copy.
    VALUE other_public_key_dup = rb_str_new(RSTRING_PTR(other_public_key), RSTRING_LEN(other_public_key));
    if (olm_sas_set_their_key(this, RSTRING_PTR(other_public_key_dup), RSTRING_LEN(other_public_key_dup)) == olm_error()) {
        raise_olm_error(olm_sas_last_error(this));
    }

    rb_iv_set(self, "@other_public_key", other_public_key);
    return other_public_key;
}

#public_keyObject



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'ext/self_crypto/sas.c', line 83

static VALUE get_public_key(VALUE self) {
    OlmSAS *this;
    size_t public_key_len;
    char *public_key;
    VALUE retval;
    TypedData_Get_Struct(self, OlmSAS, &olm_sas_type, this);

    public_key_len = olm_sas_pubkey_length(this);
    public_key = malloc_or_raise(public_key_len);

    if (olm_sas_get_pubkey(this, public_key, public_key_len) == olm_error()) {
        free(public_key);
        raise_olm_error(olm_sas_last_error(this));
    }

    retval = rb_str_new(public_key, public_key_len);
    free(public_key);
    return retval;
}