nacl 0.1 → 0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/ext/nacl/nacl.c +53 -1
- metadata +2 -2
data/ext/nacl/nacl.c
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
#include <ruby.h>
|
2
2
|
#include <crypto_box.h>
|
3
3
|
#include <crypto_sign.h>
|
4
|
+
#include <crypto_secretbox.h>
|
4
5
|
#include <crypto_hash.h>
|
5
6
|
#include <crypto_hash_sha256.h>
|
6
7
|
#include <crypto_hash_sha512.h>
|
@@ -47,7 +48,6 @@ VALUE method_crypto_box(VALUE self, VALUE message, VALUE nonce, VALUE pk, VALUE
|
|
47
48
|
int n;
|
48
49
|
|
49
50
|
Check_Type(message, T_STRING);
|
50
|
-
Check_Type(nonce, T_STRING);
|
51
51
|
CHECK_STRING_LENGTH(nonce, crypto_box_NONCEBYTES);
|
52
52
|
CHECK_STRING_LENGTH(pk, crypto_box_PUBLICKEYBYTES);
|
53
53
|
CHECK_STRING_LENGTH(sk, crypto_box_SECRETKEYBYTES);
|
@@ -138,6 +138,52 @@ VALUE method_crypto_sign_open(VALUE self, VALUE signed_message, VALUE pk) {
|
|
138
138
|
return return_value;
|
139
139
|
}
|
140
140
|
|
141
|
+
/**********************************************************************************/
|
142
|
+
|
143
|
+
VALUE method_crypto_secretbox(VALUE self, VALUE message, VALUE nonce, VALUE key) {
|
144
|
+
char *padded_message, *result;
|
145
|
+
VALUE return_value;
|
146
|
+
unsigned long long mlen;
|
147
|
+
int n;
|
148
|
+
|
149
|
+
Check_Type(message, T_STRING);
|
150
|
+
CHECK_STRING_LENGTH(nonce, crypto_secretbox_NONCEBYTES);
|
151
|
+
CHECK_STRING_LENGTH(key, crypto_secretbox_KEYBYTES);
|
152
|
+
|
153
|
+
mlen = allocate_and_prepend_zeros(message, crypto_secretbox_ZEROBYTES, &padded_message, &result);
|
154
|
+
n = crypto_secretbox(result, padded_message, mlen, RSTRING_PTR(nonce), RSTRING_PTR(key));
|
155
|
+
|
156
|
+
if (n == 0) return_value = rb_str_new(result + crypto_secretbox_BOXZEROBYTES, mlen - crypto_secretbox_BOXZEROBYTES);
|
157
|
+
memset(padded_message, 0, mlen);
|
158
|
+
free(result);
|
159
|
+
free(padded_message);
|
160
|
+
if (n != 0) rb_raise(rb_eRuntimeError, "crypto_secretbox failed");
|
161
|
+
return return_value;
|
162
|
+
}
|
163
|
+
|
164
|
+
VALUE method_crypto_secretbox_open(VALUE self, VALUE ciphertext, VALUE nonce, VALUE key) {
|
165
|
+
char *p, *padded_ciphertext, *result;
|
166
|
+
VALUE return_value;
|
167
|
+
unsigned long long mlen;
|
168
|
+
int n;
|
169
|
+
|
170
|
+
Check_Type(ciphertext, T_STRING);
|
171
|
+
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);
|
172
|
+
CHECK_STRING_LENGTH(nonce, crypto_secretbox_NONCEBYTES);
|
173
|
+
CHECK_STRING_LENGTH(key, crypto_secretbox_KEYBYTES);
|
174
|
+
|
175
|
+
mlen = allocate_and_prepend_zeros(ciphertext, crypto_secretbox_BOXZEROBYTES, &padded_ciphertext, &result);
|
176
|
+
n = crypto_secretbox_open(result, padded_ciphertext, mlen, RSTRING_PTR(nonce), RSTRING_PTR(key));
|
177
|
+
|
178
|
+
if (n == 0) return_value = rb_str_new(result + crypto_secretbox_ZEROBYTES, mlen - crypto_secretbox_ZEROBYTES);
|
179
|
+
memset(result, 0, mlen);
|
180
|
+
free(padded_ciphertext);
|
181
|
+
free(result);
|
182
|
+
if (n != 0) rb_raise(OpenError, "crypto_secretbox_open failed");
|
183
|
+
return return_value;
|
184
|
+
}
|
185
|
+
|
186
|
+
|
141
187
|
/**********************************************************************************/
|
142
188
|
|
143
189
|
VALUE method_crypto_hash(VALUE self, VALUE data) {
|
@@ -167,11 +213,17 @@ void Init_nacl() {
|
|
167
213
|
rb_define_module_function(NaCl, "crypto_box_keypair", method_crypto_box_keypair, 0);
|
168
214
|
rb_define_module_function(NaCl, "crypto_box", method_crypto_box, 4);
|
169
215
|
rb_define_module_function(NaCl, "crypto_box_open", method_crypto_box_open, 4);
|
216
|
+
rb_define_const(NaCl, "BOX_NONCE_LENGTH", INT2FIX(crypto_box_NONCEBYTES));
|
170
217
|
|
171
218
|
rb_define_module_function(NaCl, "crypto_sign_keypair", method_crypto_sign_keypair, 0);
|
172
219
|
rb_define_module_function(NaCl, "crypto_sign", method_crypto_sign, 2);
|
173
220
|
rb_define_module_function(NaCl, "crypto_sign_open", method_crypto_sign_open, 2);
|
174
221
|
|
222
|
+
rb_define_module_function(NaCl, "crypto_secretbox", method_crypto_secretbox, 3);
|
223
|
+
rb_define_module_function(NaCl, "crypto_secretbox_open", method_crypto_secretbox_open, 3);
|
224
|
+
rb_define_const(NaCl, "SECRETBOX_NONCE_LENGTH", INT2FIX(crypto_secretbox_NONCEBYTES));
|
225
|
+
rb_define_const(NaCl, "SECRETBOX_KEY_LENGTH", INT2FIX(crypto_secretbox_KEYBYTES));
|
226
|
+
|
175
227
|
rb_define_module_function(NaCl, "crypto_hash", method_crypto_hash, 1);
|
176
228
|
rb_define_module_function(NaCl, "crypto_hash_sha256", method_crypto_hash_sha256, 1);
|
177
229
|
rb_define_module_function(NaCl, "crypto_hash_sha512", method_crypto_hash_sha512, 1);
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: nacl
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: '0.
|
4
|
+
version: '0.2'
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-06-
|
12
|
+
date: 2012-06-10 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description:
|
15
15
|
email: roger@seriousorange.com
|