Module: NaCl

Defined in:
ext/nacl/nacl.c

Defined Under Namespace

Classes: OpenError

Constant Summary collapse

BOX_NONCE_LENGTH =
INT2FIX(crypto_box_NONCEBYTES)
SECRETBOX_NONCE_LENGTH =
INT2FIX(crypto_secretbox_NONCEBYTES)
SECRETBOX_KEY_LENGTH =
INT2FIX(crypto_secretbox_KEYBYTES)

Class Method Summary collapse

Class Method Details

.crypto_box(message, nonce, pk, sk) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'ext/nacl/nacl.c', line 44

VALUE method_crypto_box(VALUE self, VALUE message, VALUE nonce, VALUE pk, VALUE sk) {
    char *padded_message, *result;
    VALUE return_value;
    unsigned long long mlen;
    int n;

    Check_Type(message, T_STRING);
    CHECK_STRING_LENGTH(nonce, crypto_box_NONCEBYTES);
    CHECK_STRING_LENGTH(pk, crypto_box_PUBLICKEYBYTES);
    CHECK_STRING_LENGTH(sk, crypto_box_SECRETKEYBYTES);

    mlen = allocate_and_prepend_zeros(message, crypto_box_ZEROBYTES, &padded_message, &result);
    n = crypto_box(result, padded_message, mlen, RSTRING_PTR(nonce), RSTRING_PTR(pk), RSTRING_PTR(sk));

    if (n == 0) return_value = rb_str_new(result + crypto_box_BOXZEROBYTES, mlen - crypto_box_BOXZEROBYTES);
    memset(padded_message, 0, mlen);
    free(result);
    free(padded_message);
    if (n != 0) rb_raise(rb_eRuntimeError, "crypto_box failed");
    return return_value;
}

.crypto_box_keypairObject

*******************************************************************************



33
34
35
36
37
38
39
40
41
42
# File 'ext/nacl/nacl.c', line 33

VALUE method_crypto_box_keypair(VALUE self) {
    unsigned char pk[crypto_box_PUBLICKEYBYTES];
    unsigned char sk[crypto_box_SECRETKEYBYTES];
    VALUE keys[2];

    crypto_box_keypair(pk, sk);
    keys[0] = rb_str_new(pk, crypto_box_PUBLICKEYBYTES);
    keys[1] = rb_str_new(sk, crypto_box_SECRETKEYBYTES);
    return rb_ary_new4(2, keys);
}

.crypto_box_open(ciphertext, nonce, pk, sk) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'ext/nacl/nacl.c', line 66

VALUE method_crypto_box_open(VALUE self, VALUE ciphertext, VALUE nonce, VALUE pk, VALUE sk) {
    char *p, *padded_ciphertext, *result;
    VALUE return_value;
    unsigned long long mlen;
    int n;

    Check_Type(ciphertext, T_STRING);
    if (RSTRING_LEN(ciphertext) < crypto_box_ZEROBYTES - crypto_box_BOXZEROBYTES) rb_raise(rb_eArgError, "ciphertext must be at least %d bytes long", crypto_box_ZEROBYTES - crypto_box_BOXZEROBYTES);
    CHECK_STRING_LENGTH(nonce, crypto_box_NONCEBYTES);
    CHECK_STRING_LENGTH(pk, crypto_box_PUBLICKEYBYTES);
    CHECK_STRING_LENGTH(sk, crypto_box_SECRETKEYBYTES);

    mlen = allocate_and_prepend_zeros(ciphertext, crypto_box_BOXZEROBYTES, &padded_ciphertext, &result);
    n = crypto_box_open(result, padded_ciphertext, mlen, RSTRING_PTR(nonce), RSTRING_PTR(pk), RSTRING_PTR(sk));

    if (n == 0) return_value = rb_str_new(result + crypto_box_ZEROBYTES, mlen - crypto_box_ZEROBYTES);
    memset(result, 0, mlen);
    free(padded_ciphertext);
    free(result);
    if (n != 0) rb_raise(OpenError, "crypto_box_open failed");
    return return_value;
}

.crypto_hash(data) ⇒ Object

*******************************************************************************



189
190
191
192
193
194
# File 'ext/nacl/nacl.c', line 189

VALUE method_crypto_hash(VALUE self, VALUE data) {
    unsigned char h[crypto_hash_BYTES];
    Check_Type(data, T_STRING);
    crypto_hash(h, RSTRING_PTR(data), RSTRING_LEN(data));
    return rb_str_new(h, crypto_hash_BYTES);
}

.crypto_hash_sha256(data) ⇒ Object



196
197
198
199
200
201
# File 'ext/nacl/nacl.c', line 196

VALUE method_crypto_hash_sha256(VALUE self, VALUE data) {
    unsigned char h[crypto_hash_sha256_BYTES];
    Check_Type(data, T_STRING);
    crypto_hash_sha256(h, RSTRING_PTR(data), RSTRING_LEN(data));
    return rb_str_new(h, crypto_hash_sha256_BYTES);
}

.crypto_hash_sha512(data) ⇒ Object



203
204
205
206
207
208
# File 'ext/nacl/nacl.c', line 203

VALUE method_crypto_hash_sha512(VALUE self, VALUE data) {
    unsigned char h[crypto_hash_sha512_BYTES];
    Check_Type(data, T_STRING);
    crypto_hash_sha512(h, RSTRING_PTR(data), RSTRING_LEN(data));
    return rb_str_new(h, crypto_hash_sha512_BYTES);
}

.crypto_secretbox(message, nonce, key) ⇒ Object

*******************************************************************************



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'ext/nacl/nacl.c', line 143

VALUE method_crypto_secretbox(VALUE self, VALUE message, VALUE nonce, VALUE key) {
    char *padded_message, *result;
    VALUE return_value;
    unsigned long long mlen;
    int n;

    Check_Type(message, T_STRING);
    CHECK_STRING_LENGTH(nonce, crypto_secretbox_NONCEBYTES);
    CHECK_STRING_LENGTH(key, crypto_secretbox_KEYBYTES);

    mlen = allocate_and_prepend_zeros(message, crypto_secretbox_ZEROBYTES, &padded_message, &result);
    n = crypto_secretbox(result, padded_message, mlen, RSTRING_PTR(nonce), RSTRING_PTR(key));

    if (n == 0) return_value = rb_str_new(result + crypto_secretbox_BOXZEROBYTES, mlen - crypto_secretbox_BOXZEROBYTES);
    memset(padded_message, 0, mlen);
    free(result);
    free(padded_message);
    if (n != 0) rb_raise(rb_eRuntimeError, "crypto_secretbox failed");
    return return_value;
}

.crypto_secretbox_open(ciphertext, nonce, key) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'ext/nacl/nacl.c', line 164

VALUE method_crypto_secretbox_open(VALUE self, VALUE ciphertext, VALUE nonce, VALUE key) {
    char *p, *padded_ciphertext, *result;
    VALUE return_value;
    unsigned long long mlen;
    int n;

    Check_Type(ciphertext, T_STRING);
    if (RSTRING_LEN(ciphertext) < crypto_secretbox_ZEROBYTES - crypto_secretbox_BOXZEROBYTES) rb_raise(rb_eArgError, "ciphertext must be at least %d bytes long", crypto_secretbox_ZEROBYTES - crypto_secretbox_BOXZEROBYTES);
    CHECK_STRING_LENGTH(nonce, crypto_secretbox_NONCEBYTES);
    CHECK_STRING_LENGTH(key, crypto_secretbox_KEYBYTES);

    mlen = allocate_and_prepend_zeros(ciphertext, crypto_secretbox_BOXZEROBYTES, &padded_ciphertext, &result);
    n = crypto_secretbox_open(result, padded_ciphertext, mlen, RSTRING_PTR(nonce), RSTRING_PTR(key));

    if (n == 0) return_value = rb_str_new(result + crypto_secretbox_ZEROBYTES, mlen - crypto_secretbox_ZEROBYTES);
    memset(result, 0, mlen);
    free(padded_ciphertext);
    free(result);
    if (n != 0) rb_raise(OpenError, "crypto_secretbox_open failed");
    return return_value;
}

.crypto_sign(message, sk) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'ext/nacl/nacl.c', line 102

VALUE method_crypto_sign(VALUE self, VALUE message, VALUE sk) {
    char *result;
    VALUE return_value;
    unsigned long long smlen;

    Check_Type(message, T_STRING);
    CHECK_STRING_LENGTH(sk, crypto_sign_SECRETKEYBYTES);

    result = (char *)malloc(RSTRING_LEN(message) + crypto_sign_BYTES);
    if (result == NULL) rb_raise(rb_eNoMemError, "out of memory");

    crypto_sign(result, &smlen, RSTRING_PTR(message), RSTRING_LEN(message), RSTRING_PTR(sk));

    return_value = rb_str_new(result, smlen);
    free(result);
    return return_value;
}

.crypto_sign_keypairObject

*******************************************************************************



91
92
93
94
95
96
97
98
99
100
# File 'ext/nacl/nacl.c', line 91

VALUE method_crypto_sign_keypair(VALUE self) {
    unsigned char pk[crypto_sign_PUBLICKEYBYTES];
    unsigned char sk[crypto_sign_SECRETKEYBYTES];
    VALUE keys[2];

    crypto_sign_keypair(pk, sk);
    keys[0] = rb_str_new(pk, crypto_sign_PUBLICKEYBYTES);
    keys[1] = rb_str_new(sk, crypto_sign_SECRETKEYBYTES);
    return rb_ary_new4(2, keys);
}

.crypto_sign_open(signed_message, pk) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'ext/nacl/nacl.c', line 120

VALUE method_crypto_sign_open(VALUE self, VALUE signed_message, VALUE pk) {
    char *result;
    VALUE return_value;
    unsigned long long mlen;
    int n;

    Check_Type(signed_message, T_STRING);
    if (RSTRING_LEN(signed_message) == 0) rb_raise(OpenError, "crypto_sign_open failed");
    CHECK_STRING_LENGTH(pk, crypto_sign_PUBLICKEYBYTES);

    result = (char *)malloc(RSTRING_LEN(signed_message));
    if (result == NULL) rb_raise(rb_eNoMemError, "out of memory");

    n = crypto_sign_open(result, &mlen, RSTRING_PTR(signed_message), RSTRING_LEN(signed_message), RSTRING_PTR(pk));

    if (n == 0) return_value = rb_str_new(result, mlen);
    free(result);
    if (n != 0) rb_raise(OpenError, "crypto_sign_open failed");
    return return_value;
}