Class: Camellia
- Inherits:
-
Object
- Object
- Camellia
- Defined in:
- ext/camellia-rb.c
Instance Method Summary collapse
-
#cbc_decrypt(args) ⇒ Object
# CBC decryption method.
-
#cbc_encrypt(args) ⇒ Object
# CBC encryption method.
-
#cbc_pchar(args) ⇒ Object
# CBC padding charactor set.
-
#cbc_salt(args) ⇒ Object
# CBC IV set.
-
#cfb_decrypt(args) ⇒ Object
# CFB decryption method.
-
#cfb_encrypt(args) ⇒ Object
# CFB encryption method.
-
#cfb_salt(args) ⇒ Object
# CFB IV set method.
-
#decrypt(args) ⇒ Object
# decryption method.
-
#encrypt(args) ⇒ Object
# encryption method.
Instance Method Details
#cbc_decrypt(args) ⇒ Object
# CBC decryption method
468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 |
# File 'ext/camellia-rb.c', line 468
static VALUE cbc_decrypt(VALUE *self, VALUE args)
{
camelliaObject *camellia;
unsigned char *src;
unsigned char *destdata;
int srclen;
int i, j, destidx;
u1byte tmp[16];
VALUE retvalue;
Check_Type(args, T_STRING);
srclen = RSTRING_LEN(args);
src = (unsigned char *)RSTRING_PTR(args);
Data_Get_Struct(self,camelliaObject, camellia);
// check if key is initialized
if (!camellia->key_gen)
{
rb_raise(eCamellia,"must set up a key before you can cbc_decrypt!");
return 0;
}
// check if IV is initialized
if (camellia->cbc128_idx != -1)
{
rb_raise(eCamellia,"must set up a salt before you can cbc_decrypt!");
return 0;
}
destdata = (unsigned char *)malloc(srclen);
// CBC decryption
destidx = 0;
camellia->cbc128_idx = 0;
for (i = 0; i < srclen; i++)
{
tmp[camellia->cbc128_idx] = camellia->cbc_blk[camellia->cbc128_idx];
camellia->cbc_blk[camellia->cbc128_idx++] = src[i];
if (camellia->cbc128_idx == 16)
{
Camellia_DecryptBlock(camellia->key_len, camellia->cbc_blk, camellia->key, camellia->cbc_crypt);
camellia->cbc128_idx=0;
for (j = 0; j < 16; j++)
{
destdata[destidx++] = camellia->cbc_crypt[j] ^ tmp[j];
}
}
}
// remove paddingchar
while (destdata[srclen-1] == camellia->cbc_pchar)
{
srclen--;
}
retvalue=rb_str_new((char *)destdata, srclen);
free(destdata);
return retvalue;
}
|
#cbc_encrypt(args) ⇒ Object
# CBC encryption method
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 |
# File 'ext/camellia-rb.c', line 391
static VALUE cbc_encrypt(VALUE self,VALUE args)
{
camelliaObject *camellia;
unsigned char *src;
unsigned char *destdata;
int srclen;
int i, j, ch, destidx;
VALUE retvalue;
Check_Type(args,T_STRING);
src = (unsigned char *)RSTRING_PTR(args);
srclen = RSTRING_LEN(args);
Data_Get_Struct(self,camelliaObject,camellia);
// check if key is initialized
if (!camellia->key_gen)
{
rb_raise(eCamellia,"must set up a key before you can cbc_encrypt!");
return self;
}
// check if IV is initialized
if (camellia->cbc128_idx != -1)
{
rb_raise(eCamellia,"must set up a salt before you can cbc_encrypt!");
return self;
}
if (srclen % 16 == 0)
destdata = (unsigned char *)malloc(srclen);
else
destdata = (unsigned char *)malloc((srclen/16+1)*16);
// CBC encryption
destidx = 0;
camellia->cbc128_idx = 0;
for (i = 0; i < srclen; i++)
{
ch = src[i] ^ camellia->cbc_crypt[camellia->cbc128_idx];
camellia->cbc_blk[camellia->cbc128_idx++] = ch;
if (camellia->cbc128_idx == 16)
{
Camellia_EncryptBlock(camellia->key_len, camellia->cbc_blk, camellia->key, camellia->cbc_crypt);
camellia->cbc128_idx=0;
for (j = 0; j < 16; j++)
{
destdata[destidx++] = camellia->cbc_crypt[j];
}
}
}
if (srclen % 16 != 0)
{
while (camellia->cbc128_idx < 16)
{
camellia->cbc_blk[camellia->cbc128_idx] = camellia->cbc_pchar ^ camellia->cbc_crypt[camellia->cbc128_idx++];
}
Camellia_EncryptBlock(camellia->key_len, camellia->cbc_blk, camellia->key, camellia->cbc_crypt);
for (j = 0; j < 16; j++)
{
destdata[destidx++] = camellia->cbc_crypt[j];
}
}
if (srclen % 16 == 0)
retvalue = rb_str_new((char *)destdata, srclen);
else
retvalue = rb_str_new((char *)destdata, (srclen/16+1)*16);
free(destdata);
return retvalue;
}
|
#cbc_pchar(args) ⇒ Object
# CBC padding charactor set
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 |
# File 'ext/camellia-rb.c', line 363
static VALUE cbc_pchar(VALUE self, VALUE args)
{
camelliaObject *camellia;
unsigned char *src;
int src_len;
Check_Type(args,T_STRING);
src = (unsigned char *)RSTRING_PTR(args);
src_len = RSTRING_LEN(args);
// check pcharlength
if (src_len != 1)
{
rb_raise(rb_eArgError, "wrong padding data length (must be 1 bytes, found %d bytes)", src_len);
return self;
}
Data_Get_Struct(self, camelliaObject, camellia);
// pchar set
camellia->cbc_pchar = *src;
return self;
}
|
#cbc_salt(args) ⇒ Object
# CBC IV set
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 |
# File 'ext/camellia-rb.c', line 326
static VALUE cbc_salt(VALUE self, VALUE args)
{
camelliaObject *camellia;
unsigned char *src;
unsigned char *dest, *dest2;
int src_len;
int i;
Check_Type(args,T_STRING);
src = (unsigned char *)RSTRING_PTR(args);
src_len = RSTRING_LEN(args);
// check IV length
if (src_len != 16)
{
rb_raise(rb_eArgError, "wrong data length (must be 16 bytes, found %d bytes)", src_len);
return self;
}
Data_Get_Struct(self, camelliaObject, camellia);
// CBC initial set
camellia->cbc128_idx = -1;
dest = camellia->cbc_blk;
dest2 = camellia->cbc_crypt;
for (i = 0; i < 16; i++)
{
*dest++ = *src;
*dest2++ = *src++;
}
return self;
}
|
#cfb_decrypt(args) ⇒ Object
# CFB decryption method
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 |
# File 'ext/camellia-rb.c', line 273
static VALUE cfb_decrypt(VALUE *self, VALUE args)
{
camelliaObject *camellia;
unsigned char *src;
unsigned char *destdata;
int srclen;
int i;
unsigned char ch;
VALUE retvalue;
Check_Type(args, T_STRING);
srclen = RSTRING_LEN(args);
src = (unsigned char *)RSTRING_PTR(args);
Data_Get_Struct(self,camelliaObject, camellia);
// check if key is initialized
if (!camellia->key_gen)
{
rb_raise(eCamellia,"must set up a key before you can cfb_decrypt!");
return 0;
}
// check if IV is initialized
if (camellia->cfb128_idx != -1)
{
rb_raise(eCamellia,"must set up a salt before you can cfb_decrypt!");
return 0;
}
destdata = (unsigned char *)malloc(srclen);
// CFB decryption
for (i = 0; i < srclen; i++)
{
if (camellia->cfb128_idx < 0 || camellia->cfb128_idx > 15)
{
Camellia_EncryptBlock(camellia->key_len, camellia->cfb_blk, camellia->key, camellia->cfb_crypt);
camellia->cfb128_idx=0;
}
ch = src[i];
destdata[i] = ch ^ camellia->cfb_crypt[camellia->cfb128_idx];
camellia->cfb_blk[camellia->cfb128_idx++] = ch;
}
retvalue=rb_str_new((char *)destdata, srclen);
free(destdata);
return retvalue;
}
|
#cfb_encrypt(args) ⇒ Object
# CFB encryption method
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 |
# File 'ext/camellia-rb.c', line 221
static VALUE cfb_encrypt(VALUE self,VALUE args)
{
camelliaObject *camellia;
unsigned char *src;
unsigned char *destdata;
int srclen;
int i,ch;
VALUE retvalue;
Check_Type(args,T_STRING);
src = (unsigned char *)RSTRING_PTR(args);
srclen = RSTRING_LEN(args);
Data_Get_Struct(self,camelliaObject,camellia);
// check if key is initialized
if (!camellia->key_gen)
{
rb_raise(eCamellia,"must set up a key before you can cfb_encrypt!");
return self;
}
// check if IV is initialized
if (camellia->cfb128_idx != -1)
{
rb_raise(eCamellia,"must set up a salt before you can cfb_encrypt!");
return self;
}
destdata = (unsigned char *)malloc(srclen);
// CFB encryption
for (i = 0; i < srclen; i++)
{
if ((camellia->cfb128_idx < 0) || (camellia->cfb128_idx > 15))
{
Camellia_EncryptBlock(camellia->key_len, camellia->cfb_blk, camellia->key, camellia->cfb_crypt);
camellia->cfb128_idx=0;
}
ch = src[i] ^ camellia->cfb_crypt[camellia->cfb128_idx];
camellia->cfb_blk[camellia->cfb128_idx++] = ch;
destdata[i] = (unsigned char)ch;
}
retvalue = rb_str_new((char *)destdata, srclen);
free(destdata);
return retvalue;
}
|
#cfb_salt(args) ⇒ Object
# CFB IV set method
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 |
# File 'ext/camellia-rb.c', line 186
static VALUE cfb_salt(VALUE self, VALUE args)
{
camelliaObject *camellia;
unsigned char *src;
unsigned char *dest;
int src_len;
int i;
Check_Type(args,T_STRING);
src = (unsigned char *)RSTRING_PTR(args);
src_len = RSTRING_LEN(args);
// check IV length
if (src_len != 16)
{
rb_raise(rb_eArgError, "wrong data length (must be 16 bytes, found %d bytes)", src_len);
return self;
}
Data_Get_Struct(self, camelliaObject, camellia);
// CFB initial set
camellia->cfb128_idx = -1;
dest = camellia->cfb_blk;
for (i = 0; i < 16; i++)
{
*dest++ = *src++;
}
return self;
}
|
#decrypt(args) ⇒ Object
# decryption method
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 |
# File 'ext/camellia-rb.c', line 150
static VALUE decrypt(VALUE self,VALUE args)
{
camelliaObject *camellia;
unsigned char *data;
int data_len;
u1byte out_blk[16];
Check_Type(args, T_STRING);
data_len = RSTRING_LEN(args);
data = (unsigned char *)RSTRING_PTR(args);
// check data length
if (data_len != 16)
{
rb_raise(rb_eArgError, "wrong data length (must be 16 bytes, found %d bytes)", data_len);
return 0;
}
Data_Get_Struct(self, camelliaObject, camellia);
// check if key is initialized
if (!camellia->key_gen)
{
rb_raise(eCamellia,"must set up a key before you can decrypt!");
return 0;
}
// decryption
Camellia_DecryptBlock(camellia->key_len, data, camellia->key, out_blk);
return rb_str_new((char *)out_blk,16);
}
|
#encrypt(args) ⇒ Object
# encryption method
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'ext/camellia-rb.c', line 114
static VALUE encrypt(VALUE self, VALUE args)
{
camelliaObject *camellia;
unsigned char *data;
int data_len;
u1byte out_blk[16];
Check_Type(args, T_STRING);
data_len = RSTRING_LEN(args);
data = (unsigned char *)RSTRING_PTR(args);
Data_Get_Struct(self, camelliaObject, camellia);
// check data length
if (data_len != 16)
{
rb_raise(rb_eArgError, "wrong data length (must be 16 bytes, found %d bytes)", data_len);
return self;
}
// check if key is initialized
if (!camellia->key_gen)
{
rb_raise(eCamellia,"must set up a key before you can encrypt!");
return self;
}
// encryption
Camellia_EncryptBlock(camellia->key_len, data, camellia->key, out_blk);
return rb_str_new((char *)out_blk, 16);
}
|