ice_cipher 0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/extension/ice_cipher/bindings.c +10 -0
- data/extension/ice_cipher/bindings.h +10 -0
- data/extension/ice_cipher/extconf.rb +6 -0
- data/extension/ice_cipher/ice.c +422 -0
- data/extension/ice_cipher/ice.h +30 -0
- data/extension/ice_cipher/ice_cipher.c +182 -0
- data/extension/ice_cipher/ice_cipher.h +17 -0
- data/library/ice-cipher.rb +8 -0
- metadata +80 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: e21b1fc7709e2726d8f06377019721545c0220f1
|
4
|
+
data.tar.gz: 3445b94daef4d3be508c33831ca254d2894af12d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 48502fa53a0e7da28a2ab76c441ad34560fc746b58d638afaa577ce313a0564e8c001f4a3957ad1739c1f84dff9ff0f61b764eed2095421a0f9b420f36980937
|
7
|
+
data.tar.gz: b4fccc874728b26704f418a46b33bc37e047e3f92f01c7d5cfaabc6797f2a8122652642da701b1754396a1f64feb5254e04261e4b81aceece30e9ad6e49ea97f
|
@@ -0,0 +1,422 @@
|
|
1
|
+
/*
|
2
|
+
* Implementation of the ICE encryption algorithm.
|
3
|
+
*
|
4
|
+
* Written by Matthew Kwan - July 1996
|
5
|
+
*/
|
6
|
+
|
7
|
+
#include "ice.h"
|
8
|
+
#include <stdio.h>
|
9
|
+
#include <stdlib.h>
|
10
|
+
|
11
|
+
|
12
|
+
/* Structure of a single round subkey */
|
13
|
+
typedef unsigned long ICE_SUBKEY[3];
|
14
|
+
|
15
|
+
|
16
|
+
/* Internal structure of the ICE_KEY structure */
|
17
|
+
struct ice_key_struct {
|
18
|
+
int ik_size;
|
19
|
+
int ik_rounds;
|
20
|
+
ICE_SUBKEY *ik_keysched;
|
21
|
+
};
|
22
|
+
|
23
|
+
/* The S-boxes */
|
24
|
+
static unsigned long ice_sbox[4][1024];
|
25
|
+
static int ice_sboxes_initialised = 0;
|
26
|
+
|
27
|
+
|
28
|
+
/* Modulo values for the S-boxes */
|
29
|
+
static const int ice_smod[4][4] = {
|
30
|
+
{333, 313, 505, 369},
|
31
|
+
{379, 375, 319, 391},
|
32
|
+
{361, 445, 451, 397},
|
33
|
+
{397, 425, 395, 505}};
|
34
|
+
|
35
|
+
/* XOR values for the S-boxes */
|
36
|
+
static const int ice_sxor[4][4] = {
|
37
|
+
{0x83, 0x85, 0x9b, 0xcd},
|
38
|
+
{0xcc, 0xa7, 0xad, 0x41},
|
39
|
+
{0x4b, 0x2e, 0xd4, 0x33},
|
40
|
+
{0xea, 0xcb, 0x2e, 0x04}};
|
41
|
+
|
42
|
+
/* Expanded permutation values for the P-box */
|
43
|
+
static const unsigned long ice_pbox[32] = {
|
44
|
+
0x00000001, 0x00000080, 0x00000400, 0x00002000,
|
45
|
+
0x00080000, 0x00200000, 0x01000000, 0x40000000,
|
46
|
+
0x00000008, 0x00000020, 0x00000100, 0x00004000,
|
47
|
+
0x00010000, 0x00800000, 0x04000000, 0x20000000,
|
48
|
+
0x00000004, 0x00000010, 0x00000200, 0x00008000,
|
49
|
+
0x00020000, 0x00400000, 0x08000000, 0x10000000,
|
50
|
+
0x00000002, 0x00000040, 0x00000800, 0x00001000,
|
51
|
+
0x00040000, 0x00100000, 0x02000000, 0x80000000};
|
52
|
+
|
53
|
+
/* The key rotation schedule */
|
54
|
+
static const int ice_keyrot[16] = {
|
55
|
+
0, 1, 2, 3, 2, 1, 3, 0,
|
56
|
+
1, 3, 2, 0, 3, 1, 0, 2};
|
57
|
+
|
58
|
+
|
59
|
+
/*
|
60
|
+
* Galois Field multiplication of a by b, modulo m.
|
61
|
+
* Just like arithmetic multiplication, except that additions and
|
62
|
+
* subtractions are replaced by XOR.
|
63
|
+
*/
|
64
|
+
|
65
|
+
static unsigned int
|
66
|
+
gf_mult (
|
67
|
+
register unsigned int a,
|
68
|
+
register unsigned int b,
|
69
|
+
register unsigned int m
|
70
|
+
) {
|
71
|
+
register unsigned int res = 0;
|
72
|
+
|
73
|
+
while (b) {
|
74
|
+
if (b & 1)
|
75
|
+
res ^= a;
|
76
|
+
|
77
|
+
a <<= 1;
|
78
|
+
b >>= 1;
|
79
|
+
|
80
|
+
if (a >= 256)
|
81
|
+
a ^= m;
|
82
|
+
}
|
83
|
+
|
84
|
+
return (res);
|
85
|
+
}
|
86
|
+
|
87
|
+
|
88
|
+
/*
|
89
|
+
* Galois Field exponentiation.
|
90
|
+
* Raise the base to the power of 7, modulo m.
|
91
|
+
*/
|
92
|
+
|
93
|
+
static unsigned long
|
94
|
+
gf_exp7 (
|
95
|
+
register unsigned int b,
|
96
|
+
unsigned int m
|
97
|
+
) {
|
98
|
+
register unsigned int x;
|
99
|
+
|
100
|
+
if (b == 0)
|
101
|
+
return (0);
|
102
|
+
|
103
|
+
x = gf_mult (b, b, m);
|
104
|
+
x = gf_mult (b, x, m);
|
105
|
+
x = gf_mult (x, x, m);
|
106
|
+
return (gf_mult (b, x, m));
|
107
|
+
}
|
108
|
+
|
109
|
+
|
110
|
+
/*
|
111
|
+
* Carry out the ICE 32-bit P-box permutation.
|
112
|
+
*/
|
113
|
+
|
114
|
+
static unsigned long
|
115
|
+
ice_perm32 (
|
116
|
+
register unsigned long x
|
117
|
+
) {
|
118
|
+
register unsigned long res = 0;
|
119
|
+
register const unsigned long *pbox = ice_pbox;
|
120
|
+
|
121
|
+
while (x) {
|
122
|
+
if (x & 1)
|
123
|
+
res |= *pbox;
|
124
|
+
pbox++;
|
125
|
+
x >>= 1;
|
126
|
+
}
|
127
|
+
|
128
|
+
return (res);
|
129
|
+
}
|
130
|
+
|
131
|
+
|
132
|
+
/*
|
133
|
+
* Initialise the ICE S-boxes.
|
134
|
+
* This only has to be done once.
|
135
|
+
*/
|
136
|
+
|
137
|
+
static void
|
138
|
+
ice_sboxes_init (void)
|
139
|
+
{
|
140
|
+
register int i;
|
141
|
+
|
142
|
+
for (i=0; i<1024; i++) {
|
143
|
+
int col = (i >> 1) & 0xff;
|
144
|
+
int row = (i & 0x1) | ((i & 0x200) >> 8);
|
145
|
+
unsigned long x;
|
146
|
+
|
147
|
+
x = gf_exp7 (col ^ ice_sxor[0][row], ice_smod[0][row]) << 24;
|
148
|
+
ice_sbox[0][i] = ice_perm32 (x);
|
149
|
+
|
150
|
+
x = gf_exp7 (col ^ ice_sxor[1][row], ice_smod[1][row]) << 16;
|
151
|
+
ice_sbox[1][i] = ice_perm32 (x);
|
152
|
+
|
153
|
+
x = gf_exp7 (col ^ ice_sxor[2][row], ice_smod[2][row]) << 8;
|
154
|
+
ice_sbox[2][i] = ice_perm32 (x);
|
155
|
+
|
156
|
+
x = gf_exp7 (col ^ ice_sxor[3][row], ice_smod[3][row]);
|
157
|
+
ice_sbox[3][i] = ice_perm32 (x);
|
158
|
+
}
|
159
|
+
}
|
160
|
+
|
161
|
+
|
162
|
+
/*
|
163
|
+
* Create a new ICE key.
|
164
|
+
*/
|
165
|
+
|
166
|
+
ICE_KEY *
|
167
|
+
ice_key_create (
|
168
|
+
int n
|
169
|
+
) {
|
170
|
+
ICE_KEY *ik;
|
171
|
+
|
172
|
+
if (!ice_sboxes_initialised) {
|
173
|
+
ice_sboxes_init ();
|
174
|
+
ice_sboxes_initialised = 1;
|
175
|
+
}
|
176
|
+
|
177
|
+
if ((ik = (ICE_KEY *) malloc (sizeof (ICE_KEY))) == NULL)
|
178
|
+
return (NULL);
|
179
|
+
|
180
|
+
if (n < 1) {
|
181
|
+
ik->ik_size = 1;
|
182
|
+
ik->ik_rounds = 8;
|
183
|
+
} else {
|
184
|
+
ik->ik_size = n;
|
185
|
+
ik->ik_rounds = n * 16;
|
186
|
+
}
|
187
|
+
|
188
|
+
if ((ik->ik_keysched = (ICE_SUBKEY *) malloc (ik->ik_rounds
|
189
|
+
* sizeof (ICE_SUBKEY))) == NULL) {
|
190
|
+
free (ik);
|
191
|
+
return (NULL);
|
192
|
+
}
|
193
|
+
|
194
|
+
return (ik);
|
195
|
+
}
|
196
|
+
|
197
|
+
|
198
|
+
/*
|
199
|
+
* Destroy an ICE key.
|
200
|
+
* Zero out the memory to prevent snooping.
|
201
|
+
*/
|
202
|
+
|
203
|
+
void
|
204
|
+
ice_key_destroy (
|
205
|
+
ICE_KEY *ik
|
206
|
+
) {
|
207
|
+
int i, j;
|
208
|
+
|
209
|
+
if (ik == NULL)
|
210
|
+
return;
|
211
|
+
|
212
|
+
for (i=0; i<ik->ik_rounds; i++)
|
213
|
+
for (j=0; j<3; j++)
|
214
|
+
ik->ik_keysched[i][j] = 0;
|
215
|
+
|
216
|
+
ik->ik_rounds = ik->ik_size = 0;
|
217
|
+
|
218
|
+
if (ik->ik_keysched != NULL)
|
219
|
+
free (ik->ik_keysched);
|
220
|
+
|
221
|
+
free (ik);
|
222
|
+
}
|
223
|
+
|
224
|
+
|
225
|
+
/*
|
226
|
+
* The single round ICE f function.
|
227
|
+
*/
|
228
|
+
|
229
|
+
static unsigned long
|
230
|
+
ice_f (
|
231
|
+
register unsigned long p,
|
232
|
+
const ICE_SUBKEY sk
|
233
|
+
) {
|
234
|
+
unsigned long tl, tr; /* Expanded 40-bit values */
|
235
|
+
unsigned long al, ar; /* Salted expanded 40-bit values */
|
236
|
+
|
237
|
+
/* Left half expansion */
|
238
|
+
tl = ((p >> 16) & 0x3ff) | (((p >> 14) | (p << 18)) & 0xffc00);
|
239
|
+
|
240
|
+
/* Right half expansion */
|
241
|
+
tr = (p & 0x3ff) | ((p << 2) & 0xffc00);
|
242
|
+
|
243
|
+
/* Perform the salt permutation */
|
244
|
+
/* al = (tr & sk[2]) | (tl & ~sk[2]); */
|
245
|
+
/* ar = (tl & sk[2]) | (tr & ~sk[2]); */
|
246
|
+
al = sk[2] & (tl ^ tr);
|
247
|
+
ar = al ^ tr;
|
248
|
+
al ^= tl;
|
249
|
+
|
250
|
+
al ^= sk[0]; /* XOR with the subkey */
|
251
|
+
ar ^= sk[1];
|
252
|
+
|
253
|
+
/* S-box lookup and permutation */
|
254
|
+
return (ice_sbox[0][al >> 10] | ice_sbox[1][al & 0x3ff]
|
255
|
+
| ice_sbox[2][ar >> 10] | ice_sbox[3][ar & 0x3ff]);
|
256
|
+
}
|
257
|
+
|
258
|
+
|
259
|
+
/*
|
260
|
+
* Encrypt a block of 8 bytes of data with the given ICE key.
|
261
|
+
*/
|
262
|
+
|
263
|
+
void
|
264
|
+
ice_key_encrypt (
|
265
|
+
const ICE_KEY *ik,
|
266
|
+
const unsigned char *ptext,
|
267
|
+
unsigned char *ctext
|
268
|
+
) {
|
269
|
+
register int i;
|
270
|
+
register unsigned long l, r;
|
271
|
+
|
272
|
+
l = (((unsigned long) ptext[0]) << 24)
|
273
|
+
| (((unsigned long) ptext[1]) << 16)
|
274
|
+
| (((unsigned long) ptext[2]) << 8) | ptext[3];
|
275
|
+
r = (((unsigned long) ptext[4]) << 24)
|
276
|
+
| (((unsigned long) ptext[5]) << 16)
|
277
|
+
| (((unsigned long) ptext[6]) << 8) | ptext[7];
|
278
|
+
|
279
|
+
for (i = 0; i < ik->ik_rounds; i += 2) {
|
280
|
+
l ^= ice_f (r, ik->ik_keysched[i]);
|
281
|
+
r ^= ice_f (l, ik->ik_keysched[i + 1]);
|
282
|
+
}
|
283
|
+
|
284
|
+
for (i = 0; i < 4; i++) {
|
285
|
+
ctext[3 - i] = r & 0xff;
|
286
|
+
ctext[7 - i] = l & 0xff;
|
287
|
+
|
288
|
+
r >>= 8;
|
289
|
+
l >>= 8;
|
290
|
+
}
|
291
|
+
}
|
292
|
+
|
293
|
+
|
294
|
+
/*
|
295
|
+
* Decrypt a block of 8 bytes of data with the given ICE key.
|
296
|
+
*/
|
297
|
+
|
298
|
+
void
|
299
|
+
ice_key_decrypt (
|
300
|
+
const ICE_KEY *ik,
|
301
|
+
const unsigned char *ctext,
|
302
|
+
unsigned char *ptext
|
303
|
+
) {
|
304
|
+
register int i;
|
305
|
+
register unsigned long l, r;
|
306
|
+
|
307
|
+
l = (((unsigned long) ctext[0]) << 24)
|
308
|
+
| (((unsigned long) ctext[1]) << 16)
|
309
|
+
| (((unsigned long) ctext[2]) << 8) | ctext[3];
|
310
|
+
r = (((unsigned long) ctext[4]) << 24)
|
311
|
+
| (((unsigned long) ctext[5]) << 16)
|
312
|
+
| (((unsigned long) ctext[6]) << 8) | ctext[7];
|
313
|
+
|
314
|
+
for (i = ik->ik_rounds - 1; i > 0; i -= 2) {
|
315
|
+
l ^= ice_f (r, ik->ik_keysched[i]);
|
316
|
+
r ^= ice_f (l, ik->ik_keysched[i - 1]);
|
317
|
+
}
|
318
|
+
|
319
|
+
for (i = 0; i < 4; i++) {
|
320
|
+
ptext[3 - i] = r & 0xff;
|
321
|
+
ptext[7 - i] = l & 0xff;
|
322
|
+
|
323
|
+
r >>= 8;
|
324
|
+
l >>= 8;
|
325
|
+
}
|
326
|
+
}
|
327
|
+
|
328
|
+
|
329
|
+
/*
|
330
|
+
* Set 8 rounds [n, n+7] of the key schedule of an ICE key.
|
331
|
+
*/
|
332
|
+
|
333
|
+
static void
|
334
|
+
ice_key_sched_build (
|
335
|
+
ICE_KEY *ik,
|
336
|
+
unsigned short *kb,
|
337
|
+
int n,
|
338
|
+
const int *keyrot
|
339
|
+
) {
|
340
|
+
int i;
|
341
|
+
|
342
|
+
for (i=0; i<8; i++) {
|
343
|
+
register int j;
|
344
|
+
register int kr = keyrot[i];
|
345
|
+
ICE_SUBKEY *isk = &ik->ik_keysched[n + i];
|
346
|
+
|
347
|
+
for (j=0; j<3; j++)
|
348
|
+
(*isk)[j] = 0;
|
349
|
+
|
350
|
+
for (j=0; j<15; j++) {
|
351
|
+
register int k;
|
352
|
+
unsigned long *curr_sk = &(*isk)[j % 3];
|
353
|
+
|
354
|
+
for (k=0; k<4; k++) {
|
355
|
+
unsigned short *curr_kb = &kb[(kr + k) & 3];
|
356
|
+
register int bit = *curr_kb & 1;
|
357
|
+
|
358
|
+
*curr_sk = (*curr_sk << 1) | bit;
|
359
|
+
*curr_kb = (*curr_kb >> 1) | ((bit ^ 1) << 15);
|
360
|
+
}
|
361
|
+
}
|
362
|
+
}
|
363
|
+
}
|
364
|
+
|
365
|
+
|
366
|
+
/*
|
367
|
+
* Set the key schedule of an ICE key.
|
368
|
+
*/
|
369
|
+
|
370
|
+
void
|
371
|
+
ice_key_set (
|
372
|
+
ICE_KEY *ik,
|
373
|
+
const unsigned char *key
|
374
|
+
) {
|
375
|
+
int i;
|
376
|
+
|
377
|
+
if (ik->ik_rounds == 8) {
|
378
|
+
unsigned short kb[4];
|
379
|
+
|
380
|
+
for (i=0; i<4; i++)
|
381
|
+
kb[3 - i] = (key[i*2] << 8) | key[i*2 + 1];
|
382
|
+
|
383
|
+
ice_key_sched_build (ik, kb, 0, ice_keyrot);
|
384
|
+
return;
|
385
|
+
}
|
386
|
+
|
387
|
+
for (i = 0; i < ik->ik_size; i++) {
|
388
|
+
int j;
|
389
|
+
unsigned short kb[4];
|
390
|
+
|
391
|
+
for (j=0; j<4; j++)
|
392
|
+
kb[3 - j] = (key[i*8 + j*2] << 8) | key[i*8 + j*2 + 1];
|
393
|
+
|
394
|
+
ice_key_sched_build (ik, kb, i*8, ice_keyrot);
|
395
|
+
ice_key_sched_build (ik, kb, ik->ik_rounds - 8 - i*8,
|
396
|
+
&ice_keyrot[8]);
|
397
|
+
}
|
398
|
+
}
|
399
|
+
|
400
|
+
|
401
|
+
/*
|
402
|
+
* Return the key size, in bytes.
|
403
|
+
*/
|
404
|
+
|
405
|
+
int
|
406
|
+
ice_key_key_size (
|
407
|
+
const ICE_KEY *ik
|
408
|
+
) {
|
409
|
+
return (ik->ik_size * 8);
|
410
|
+
}
|
411
|
+
|
412
|
+
|
413
|
+
/*
|
414
|
+
* Return the block size, in bytes.
|
415
|
+
*/
|
416
|
+
|
417
|
+
int
|
418
|
+
ice_key_block_size (
|
419
|
+
const ICE_KEY *ik
|
420
|
+
) {
|
421
|
+
return (8);
|
422
|
+
}
|
@@ -0,0 +1,30 @@
|
|
1
|
+
/*
|
2
|
+
* Header file for the ICE encryption library.
|
3
|
+
*
|
4
|
+
* Written by Matthew Kwan - July 1996
|
5
|
+
*/
|
6
|
+
|
7
|
+
#ifndef _ICE_H
|
8
|
+
#define _ICE_H
|
9
|
+
|
10
|
+
typedef struct ice_key_struct ICE_KEY;
|
11
|
+
|
12
|
+
#if __STDC__
|
13
|
+
#define P_(x) x
|
14
|
+
#else
|
15
|
+
#define P_(x) ()
|
16
|
+
#endif
|
17
|
+
|
18
|
+
extern ICE_KEY *ice_key_create P_((int n));
|
19
|
+
extern void ice_key_destroy P_((ICE_KEY *ik));
|
20
|
+
extern void ice_key_set P_((ICE_KEY *ik, const unsigned char *k));
|
21
|
+
extern void ice_key_encrypt P_((const ICE_KEY *ik,
|
22
|
+
const unsigned char *ptxt, unsigned char *ctxt));
|
23
|
+
extern void ice_key_decrypt P_((const ICE_KEY *ik,
|
24
|
+
const unsigned char *ctxt, unsigned char *ptxt));
|
25
|
+
extern int ice_key_key_size P_((const ICE_KEY *ik));
|
26
|
+
extern int ice_key_block_size P_((const ICE_KEY *ik));
|
27
|
+
|
28
|
+
#undef P_
|
29
|
+
|
30
|
+
#endif
|
@@ -0,0 +1,182 @@
|
|
1
|
+
#include "bindings.h"
|
2
|
+
#include "ice_cipher.h"
|
3
|
+
#include "ice.h"
|
4
|
+
|
5
|
+
void ice_cipher_deallocate(void* data)
|
6
|
+
{
|
7
|
+
ICE_KEY* key = (ICE_KEY*)data;
|
8
|
+
|
9
|
+
ice_key_destroy(key);
|
10
|
+
}
|
11
|
+
|
12
|
+
VALUE ice_cipher_allocate(VALUE klass)
|
13
|
+
{
|
14
|
+
ICE_KEY* key = ice_key_create(0);
|
15
|
+
|
16
|
+
return Data_Wrap_Struct(klass, NULL, ice_cipher_deallocate, key);
|
17
|
+
}
|
18
|
+
|
19
|
+
void Init_ice_cipher_class()
|
20
|
+
{
|
21
|
+
VALUE cIcecipher = rb_define_class_under(kIceModule, "Cipher", rb_cObject);
|
22
|
+
|
23
|
+
rb_define_alloc_func(cIcecipher, ice_cipher_allocate);
|
24
|
+
rb_define_method(cIcecipher, "initialize", ice_cipher_initialize, 1);
|
25
|
+
rb_define_method(cIcecipher, "key_size", ice_cipher_key_size, 0);
|
26
|
+
rb_define_method(cIcecipher, "block_size", ice_cipher_block_size, 0);
|
27
|
+
rb_define_method(cIcecipher, "key=", ice_cipher_set_key, 1);
|
28
|
+
rb_define_method(cIcecipher, "encrypt", ice_cipher_encrypt, 1);
|
29
|
+
rb_define_method(cIcecipher, "decrypt", ice_cipher_decrypt, 1);
|
30
|
+
}
|
31
|
+
|
32
|
+
VALUE ice_cipher_initialize(VALUE self, VALUE level)
|
33
|
+
{
|
34
|
+
Check_Type(level, T_FIXNUM);
|
35
|
+
|
36
|
+
ice_cipher_create_key(self, NUM2INT(level), NULL);
|
37
|
+
|
38
|
+
return self;
|
39
|
+
}
|
40
|
+
|
41
|
+
void ice_cipher_create_key(VALUE self, int level, unsigned char* key)
|
42
|
+
{
|
43
|
+
// Destroy the current key
|
44
|
+
ICE_KEY* ik;
|
45
|
+
|
46
|
+
Data_Get_Struct(self, ICE_KEY, ik);
|
47
|
+
|
48
|
+
if (ik != NULL)
|
49
|
+
{
|
50
|
+
ice_key_destroy(ik);
|
51
|
+
}
|
52
|
+
|
53
|
+
// Create a new key
|
54
|
+
ik = ice_key_create(level);
|
55
|
+
|
56
|
+
if (key != NULL)
|
57
|
+
{
|
58
|
+
// Set an cipher key
|
59
|
+
ice_key_set(ik, key);
|
60
|
+
}
|
61
|
+
}
|
62
|
+
|
63
|
+
VALUE ice_cipher_key_size(VALUE self)
|
64
|
+
{
|
65
|
+
ICE_KEY* key;
|
66
|
+
|
67
|
+
Data_Get_Struct(self, ICE_KEY, key);
|
68
|
+
|
69
|
+
return INT2NUM(ice_key_key_size(key));
|
70
|
+
}
|
71
|
+
|
72
|
+
VALUE ice_cipher_block_size(VALUE self)
|
73
|
+
{
|
74
|
+
ICE_KEY* key;
|
75
|
+
|
76
|
+
Data_Get_Struct(self, ICE_KEY, key);
|
77
|
+
|
78
|
+
return INT2NUM(ice_key_block_size(key));
|
79
|
+
}
|
80
|
+
|
81
|
+
VALUE ice_cipher_set_key(VALUE self, VALUE value)
|
82
|
+
{
|
83
|
+
ICE_KEY* key;
|
84
|
+
|
85
|
+
Check_Type(value, T_STRING);
|
86
|
+
Data_Get_Struct(self, ICE_KEY, key);
|
87
|
+
|
88
|
+
if (key != NULL)
|
89
|
+
{
|
90
|
+
ice_key_set(key, (unsigned char*)StringValuePtr(value));
|
91
|
+
|
92
|
+
return value;
|
93
|
+
}
|
94
|
+
|
95
|
+
return Qnil;
|
96
|
+
}
|
97
|
+
|
98
|
+
VALUE ice_cipher_encrypt(VALUE self, VALUE text)
|
99
|
+
{
|
100
|
+
ICE_KEY* key;
|
101
|
+
VALUE result;
|
102
|
+
unsigned int textLength;
|
103
|
+
unsigned char* plainText;
|
104
|
+
unsigned char* cipherText;
|
105
|
+
|
106
|
+
// Get the string length
|
107
|
+
textLength = RSTRING_LEN(text);
|
108
|
+
|
109
|
+
// Check that the string is divisible by 8
|
110
|
+
if (textLength % 8 != 0)
|
111
|
+
{
|
112
|
+
rb_raise(rb_eArgError, "expected argument to be divisible by 8, but length is %d", textLength);
|
113
|
+
|
114
|
+
return Qnil;
|
115
|
+
}
|
116
|
+
|
117
|
+
// Get the contents of the string
|
118
|
+
plainText = (unsigned char*)StringValuePtr(text);
|
119
|
+
|
120
|
+
// Allocate space for the cipher text
|
121
|
+
cipherText = (unsigned char*)malloc(textLength);
|
122
|
+
|
123
|
+
// Get the data struct
|
124
|
+
Data_Get_Struct(self, ICE_KEY, key);
|
125
|
+
|
126
|
+
// Decrypt the cipher text
|
127
|
+
ice_key_encrypt(key, (const unsigned char*)plainText, cipherText);
|
128
|
+
|
129
|
+
if (cipherText != NULL)
|
130
|
+
{
|
131
|
+
result = rb_str_new((const char*)cipherText, textLength);
|
132
|
+
}
|
133
|
+
else
|
134
|
+
{
|
135
|
+
result = Qnil;
|
136
|
+
}
|
137
|
+
|
138
|
+
return result;
|
139
|
+
}
|
140
|
+
|
141
|
+
VALUE ice_cipher_decrypt(VALUE self, VALUE text)
|
142
|
+
{
|
143
|
+
ICE_KEY* key;
|
144
|
+
VALUE result;
|
145
|
+
unsigned int textLength;
|
146
|
+
unsigned char* plainText;
|
147
|
+
unsigned char* cipherText;
|
148
|
+
|
149
|
+
// Get the string length
|
150
|
+
textLength = RSTRING_LEN(text);
|
151
|
+
|
152
|
+
// Check that the string is divisible by 8
|
153
|
+
if (textLength % 8 != 0)
|
154
|
+
{
|
155
|
+
rb_raise(rb_eArgError, "expected argument to be divisible by 8, but length is %d", textLength);
|
156
|
+
|
157
|
+
return Qnil;
|
158
|
+
}
|
159
|
+
|
160
|
+
// Get the contents of the string
|
161
|
+
cipherText = (unsigned char*)StringValuePtr(text);
|
162
|
+
|
163
|
+
// Allocate space for the decrypted text
|
164
|
+
plainText = (unsigned char*)malloc(textLength);
|
165
|
+
|
166
|
+
// Get the data struct
|
167
|
+
Data_Get_Struct(self, ICE_KEY, key);
|
168
|
+
|
169
|
+
// Decrypt the cipher text
|
170
|
+
ice_key_decrypt(key, (const unsigned char*)cipherText, plainText);
|
171
|
+
|
172
|
+
if (plainText != NULL)
|
173
|
+
{
|
174
|
+
result = rb_str_new((const char*)plainText, textLength);
|
175
|
+
}
|
176
|
+
else
|
177
|
+
{
|
178
|
+
result = Qnil;
|
179
|
+
}
|
180
|
+
|
181
|
+
return result;
|
182
|
+
}
|
@@ -0,0 +1,17 @@
|
|
1
|
+
#ifndef __RUBY_ICE_KEY_H
|
2
|
+
#define __RUBY_ICE_KEY_H
|
3
|
+
#include <ruby.h>
|
4
|
+
|
5
|
+
void Init_ice_cipher_class();
|
6
|
+
|
7
|
+
void ice_cipher_create_key(VALUE self, int level, unsigned char* key);
|
8
|
+
void ice_cipher_deallocate(void *ik);
|
9
|
+
VALUE ice_cipher_allocate(VALUE klass);
|
10
|
+
VALUE ice_cipher_initialize(VALUE self, VALUE level);
|
11
|
+
VALUE ice_cipher_set_key(VALUE self, VALUE key);
|
12
|
+
VALUE ice_cipher_key_size(VALUE self);
|
13
|
+
VALUE ice_cipher_block_size(VALUE self);
|
14
|
+
VALUE ice_cipher_encrypt(VALUE self, VALUE text);
|
15
|
+
VALUE ice_cipher_decrypt(VALUE self, VALUE ciphertext);
|
16
|
+
|
17
|
+
#endif
|
metadata
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ice_cipher
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: '0.1'
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Mikkel Kroman
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-09-14 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rspec
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake-compiler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description:
|
42
|
+
email: mk@uplink.io
|
43
|
+
executables: []
|
44
|
+
extensions:
|
45
|
+
- extension/ice_cipher/extconf.rb
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- library/ice-cipher.rb
|
49
|
+
- extension/ice_cipher/extconf.rb
|
50
|
+
- extension/ice_cipher/ice.c
|
51
|
+
- extension/ice_cipher/ice_cipher.h
|
52
|
+
- extension/ice_cipher/ice.h
|
53
|
+
- extension/ice_cipher/bindings.c
|
54
|
+
- extension/ice_cipher/ice_cipher.c
|
55
|
+
- extension/ice_cipher/bindings.h
|
56
|
+
homepage:
|
57
|
+
licenses:
|
58
|
+
- Internet Systems Consortium (ISC)
|
59
|
+
metadata: {}
|
60
|
+
post_install_message:
|
61
|
+
rdoc_options: []
|
62
|
+
require_paths:
|
63
|
+
- library
|
64
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 1.9.1
|
69
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
70
|
+
requirements:
|
71
|
+
- - '>='
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: '0'
|
74
|
+
requirements: []
|
75
|
+
rubyforge_project:
|
76
|
+
rubygems_version: 2.1.3
|
77
|
+
signing_key:
|
78
|
+
specification_version: 4
|
79
|
+
summary: ICE encryption library
|
80
|
+
test_files: []
|