Class: AesAlg
- Inherits:
-
Object
- Object
- AesAlg
- Defined in:
- ext/ruby-aes/aes_alg.c
Instance Method Summary collapse
- #c_extension ⇒ Object
- #decrypt_block(block) ⇒ Object
- #decrypt_blocks(buffer) ⇒ Object
- #decrypt_buffer(buffer) ⇒ Object
- #encrypt_block(block) ⇒ Object
- #encrypt_blocks(buffer) ⇒ Object
- #encrypt_buffer(buffer) ⇒ Object
- #init(kl, mode, key, iv) ⇒ Object
- #initialize(kl, mode, key, iv) ⇒ Object constructor
Constructor Details
#initialize(kl, mode, key, iv) ⇒ Object
791 792 793 794 795 796 797 |
# File 'ext/ruby-aes/aes_alg.c', line 791
static VALUE
rb_aes_initialize(VALUE self, VALUE kl, VALUE mode, VALUE key, VALUE iv)
{
rb_aes_init(self, kl, mode, key, iv);
return Qnil;
}
|
Instance Method Details
#c_extension ⇒ Object
695 696 697 698 699 |
# File 'ext/ruby-aes/aes_alg.c', line 695
static VALUE
rb_aes_c_extension()
{
return Qtrue;
}
|
#decrypt_block(block) ⇒ Object
464 465 466 467 468 |
# File 'ext/ruby-aes/aes_alg.c', line 464
static VALUE
rb_aes_decrypt_block(VALUE self, VALUE block)
{
return rb_str_new(aes_decrypt_block(DATA_PTR(self), RSTRING(block)->ptr), BLOCK_SIZE);
}
|
#decrypt_blocks(buffer) ⇒ Object
497 498 499 500 501 502 503 504 505 506 |
# File 'ext/ruby-aes/aes_alg.c', line 497
static VALUE
rb_aes_decrypt_blocks(VALUE self, VALUE buffer)
{
AES *data = DATA_PTR(self);
VALUE pt = rb_str_new("", 0);
aes_decrypt_blocks(data, RSTRING(buffer)->len, RSTRING(buffer)->ptr, pt);
return pt;
}
|
#decrypt_buffer(buffer) ⇒ Object
533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 |
# File 'ext/ruby-aes/aes_alg.c', line 533
static VALUE
rb_aes_decrypt_buffer(VALUE self, VALUE buffer)
{
AES *data = DATA_PTR(self);
const char *ptr = RSTRING(buffer)->ptr;
int i, len = RSTRING(buffer)->len;
uchar pad;
VALUE pt = rb_str_new("", 0);
for (; len >= BLOCK_SIZE; len -= BLOCK_SIZE, ptr += BLOCK_SIZE)
rb_str_cat(pt, aes_decrypt_block(data, ptr), BLOCK_SIZE);
if (len == 0) {
i = RSTRING(pt)->len;
ptr = RSTRING(pt)->ptr + i - 1;
pad = *ptr;
if (pad < BLOCK_SIZE) {
for (; i > 0 && pad == *ptr; ptr--, i--, len++);
/*if (pad != len)
rb_raise(rb_eRuntimeError, "Bad Block Padding");*/
if (pad == len)
rb_str_resize(pt, i);
}
} else/* if (len != 1)*/
rb_raise(rb_eRuntimeError, "Bad Block Padding");
return pt;
}
|
#encrypt_block(block) ⇒ Object
458 459 460 461 462 |
# File 'ext/ruby-aes/aes_alg.c', line 458
static VALUE
rb_aes_encrypt_block(VALUE self, VALUE block)
{
return rb_str_new(aes_encrypt_block(DATA_PTR(self), RSTRING(block)->ptr), BLOCK_SIZE);
}
|
#encrypt_blocks(buffer) ⇒ Object
486 487 488 489 490 491 492 493 494 495 |
# File 'ext/ruby-aes/aes_alg.c', line 486
static VALUE
rb_aes_encrypt_blocks(VALUE self, VALUE buffer)
{
AES *data = DATA_PTR(self);
VALUE ct = rb_str_new("", 0);
aes_encrypt_blocks(data, RSTRING(buffer)->len, RSTRING(buffer)->ptr, ct);
return ct;
}
|
#encrypt_buffer(buffer) ⇒ Object
508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 |
# File 'ext/ruby-aes/aes_alg.c', line 508
static VALUE
rb_aes_encrypt_buffer(VALUE self, VALUE buffer)
{
AES *data = DATA_PTR(self);
const char *ptr = RSTRING(buffer)->ptr;
int i, len = RSTRING(buffer)->len;
uchar pad, *buf;
VALUE ct = rb_str_new("", 0);
for (; len >= BLOCK_SIZE; len -= BLOCK_SIZE, ptr += BLOCK_SIZE)
rb_str_cat(ct, aes_encrypt_block(data, ptr), BLOCK_SIZE);
if (len > 0 && len < BLOCK_SIZE) {
pad = BLOCK_SIZE - len % BLOCK_SIZE;
buf = ALLOC_N(uchar, BLOCK_SIZE);
MEMCPY(buf, ptr, uchar, len);
for (i = len; i < BLOCK_SIZE; i++)
buf[i] = pad;
rb_str_cat(ct, aes_encrypt_block(data, buf), BLOCK_SIZE);
free(buf);
}/* else
rb_str_cat(ct, "\x0", 1);*/
return ct;
}
|
#init(kl, mode, key, iv) ⇒ Object
701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 |
# File 'ext/ruby-aes/aes_alg.c', line 701
static VALUE
rb_aes_init(VALUE self, VALUE kl, VALUE mode, VALUE key, VALUE iv)
{
AES *data = DATA_PTR(self);
data->kl = FIX2INT(kl);
if (data->kl == 128) {
data->nk = 4;
data->nr = 10;
} else if (data->kl == 192) {
data->nk = 6;
data->nr = 12;
} else if (data->kl == 256) {
data->nk = 8;
data->nr = 14;
} else {
rb_raise(rb_eArgError, "Bad Key Length");
}
data->nb = 4;
char *mode_str = RSTRING(mode)->ptr;
if (!strncmp(mode_str, "ECB", 3)) data->mode = MODE_ECB;
else if (!strncmp(mode_str, "CBC", 3)) data->mode = MODE_CBC;
else if (!strncmp(mode_str, "CFB", 3)) data->mode = MODE_CFB;
else if (!strncmp(mode_str, "OFB", 3)) data->mode = MODE_OFB;
else rb_raise(rb_eArgError, "Bad AES mode");
if (data->key) FREE(data->key);
data->key = ALLOC_N(uchar, 32);
MEMCPY(data->key, RSTRING(key)->ptr, uchar, RSTRING(key)->len > 32 ? 32 : RSTRING(key)->len);
if (data->iv) FREE(data->iv);
if (NIL_P(iv)) {
data->iv = NULL;
} else {
data->iv = ALLOC_N(uchar, BLOCK_SIZE);
MEMCPY(data->iv, RSTRING(iv)->ptr, uchar, BLOCK_SIZE);
}
decryption_key_schedule(data, data->key);
return self;
}
|