Module: SelfCrypto::Util

Defined in:
lib/self_crypto/utility.rb,
ext/self_crypto/utility.c

Class Method Summary collapse

Class Method Details

.aead_xchacha20poly1305_ietf_decrypt(key, nonce, ciphertext) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'ext/self_crypto/utility.c', line 95

static VALUE aead_xchacha20poly1305_ietf_decrypt(VALUE self, VALUE key, VALUE nonce, VALUE ciphertext) {
    void * plaintext;
    unsigned long long plaintext_len;

    if (key == Qnil) {
        rb_raise(rb_eStandardError, "must specify a key");
    }

    if (nonce == Qnil) {
        rb_raise(rb_eStandardError, "must specify a nonce");
    }

    if (ciphertext == Qnil) {
        rb_raise(rb_eStandardError, "must specify ciphertext");
    }

    if ((plaintext = malloc(RSTRING_LEN(ciphertext))) == NULL) {
        rb_raise(rb_eNoMemError, "%s()", __FUNCTION__);
    }

    int status = self_crypto_aead_xchacha20poly1305_ietf_decrypt(
        plaintext,
        (uint64_t *) &plaintext_len,
        NULL,
        RSTRING_PTR(ciphertext),
        RSTRING_LEN(ciphertext),
        NULL,
        0,
        RSTRING_PTR(nonce),
        RSTRING_PTR(key)
    );

    if (status != 0) {
        rb_raise(rb_eStandardError, "could not authenticate encrypted message");
    }

    VALUE pt = rb_str_new(plaintext, plaintext_len);

    free(plaintext);

    return pt;
}

.aead_xchacha20poly1305_ietf_encrypt(key, nonce, plaintext) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'ext/self_crypto/utility.c', line 56

static VALUE aead_xchacha20poly1305_ietf_encrypt(VALUE self, VALUE key, VALUE nonce, VALUE plaintext) {
    void * ciphertext;
    unsigned long long ciphertext_len;

    if (key == Qnil) {
        rb_raise(rb_eStandardError, "must specify a key");
    }

    if (nonce == Qnil) {
        rb_raise(rb_eStandardError, "must specify a nonce");
    }

    if (plaintext == Qnil) {
        rb_raise(rb_eStandardError, "must specify plaintext");
    }

    if ((ciphertext = malloc(RSTRING_LEN(plaintext) + self_crypto_aead_xchacha20poly1305_ietf_ABYTES)) == NULL) {
        rb_raise(rb_eNoMemError, "%s()", __FUNCTION__);
    }

    self_crypto_aead_xchacha20poly1305_ietf_encrypt(
        ciphertext, 
        (uint64_t *) &ciphertext_len,
        RSTRING_PTR(plaintext),
        RSTRING_LEN(plaintext),
        NULL,
        0,
        NULL,
        RSTRING_PTR(nonce),
        RSTRING_PTR(key)
    );

    VALUE ct = rb_str_new(ciphertext, ciphertext_len);

    free(ciphertext);

    return ct;
}

.aead_xchacha20poly1305_ietf_keygenObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'ext/self_crypto/utility.c', line 40

static VALUE aead_xchacha20poly1305_ietf_keygen(VALUE self) {
    void * key;

    if ((key = malloc(self_crypto_aead_xchacha20poly1305_ietf_KEYBYTES)) == NULL) {
        rb_raise(rb_eNoMemError, "%s()", __FUNCTION__);
    }

    self_crypto_aead_xchacha20poly1305_ietf_keygen(key);

    VALUE k = rb_str_new(key, self_crypto_aead_xchacha20poly1305_ietf_KEYBYTES);

    free(key);

    return k;
}

.aead_xchacha20poly1305_ietf_nonceObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'ext/self_crypto/utility.c', line 24

static VALUE aead_xchacha20poly1305_ietf_nonce(VALUE self) {
    void * nonce;

    if ((nonce = malloc(self_crypto_aead_xchacha20poly1305_ietf_NPUBBYTES)) == NULL) {
        rb_raise(rb_eNoMemError, "%s()", __FUNCTION__);
    }

    self_randombytes_buf(nonce, self_crypto_aead_xchacha20poly1305_ietf_NPUBBYTES);

    VALUE n = rb_str_new(nonce, self_crypto_aead_xchacha20poly1305_ietf_NPUBBYTES);

    free(nonce);

    return n;
}

.ed25519_pk_to_curve25519(ed25519_pk) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'ext/self_crypto/utility.c', line 138

static VALUE ed25519_pk_to_curve25519(VALUE self, VALUE ed25519_pk) {
    VALUE curve25519_sk;
    void * pk_ptr, * dec_ptr, * enc_ptr;
    size_t pk_sz, dec_sz, enc_sz, success;

    if (rb_obj_is_kind_of(ed25519_pk, rb_eval_string("String")) != Qtrue) {
        rb_raise(rb_eTypeError, "ed25519_pk must be kind of String");
    }

    pk_sz = self_crypto_sign_publickeybytes();

    if ((dec_ptr = malloc(pk_sz)) == NULL) {
        rb_raise(rb_eNoMemError, "%s()", __FUNCTION__);
    }

    success = self_base642bin(
        dec_ptr,
        pk_sz,
        RSTRING_PTR(ed25519_pk),
        RSTRING_LEN(ed25519_pk),
        NULL, &
        dec_sz,
        NULL,
        self_base64_VARIANT_URLSAFE_NO_PADDING
    );

    if (success != 0) {
        free(dec_ptr);
        rb_raise(rb_eTypeError, "could not decode ed25519 public key");
    }

    if ((pk_ptr = malloc(pk_sz)) == NULL) {
        rb_raise(rb_eNoMemError, "%s()", __FUNCTION__);
    }

    success = self_crypto_sign_ed25519_pk_to_curve25519(
        pk_ptr,
        dec_ptr
    );

    free(dec_ptr);

    if (success != 0) {
        free(pk_ptr);
        rb_raise(rb_eTypeError, "could not convert ed25519 public key");
    }

    enc_sz = self_base64_ENCODED_LEN(pk_sz, self_base64_VARIANT_ORIGINAL_NO_PADDING);

    if ((enc_ptr = malloc(enc_sz)) == NULL) {
        rb_raise(rb_eNoMemError, "%s()", __FUNCTION__);
    }

    self_bin2base64(
        enc_ptr,
        enc_sz,
        pk_ptr,
        pk_sz,
        self_base64_VARIANT_ORIGINAL_NO_PADDING
    );

    free(pk_ptr);

    curve25519_sk = rb_str_new_cstr(enc_ptr);

    free(enc_ptr);

    return curve25519_sk;
}

.random_bytes(size) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'ext/self_crypto/utility.c', line 4

static VALUE random_bytes(VALUE self, VALUE size) {
    void * nonce;

    if (size == Qnil) {
        rb_raise(rb_eStandardError, "must specify a size");
    }

    if ((nonce = malloc(NUM2SIZET(size))) == NULL) {
        rb_raise(rb_eNoMemError, "%s()", __FUNCTION__);
    }

    self_randombytes_buf(nonce, NUM2SIZET(size));

    VALUE n = rb_str_new(nonce, NUM2SIZET(size));

    free(nonce);

    return n;
}