Class: SelfCrypto::PK::Decryption

Inherits:
Data
  • Object
show all
Defined in:
ext/self_crypto/pk_decryption.c

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'ext/self_crypto/pk_decryption.c', line 33

static VALUE initialize(int argc, VALUE *argv, VALUE self) {
    OlmPkDecryption *this;
    size_t publicKeyLen;
    char *publicKeyPtr;
    VALUE privateKey;
    TypedData_Get_Struct(self, OlmPkDecryption, &olm_pk_decryption_type, this);

    rb_scan_args(argc, argv, "01", &privateKey);

    if (NIL_P(privateKey)) {
        privateKey = get_random(olm_pk_private_key_length());
    } else {
        Check_Type(privateKey, T_STRING);
        if (RSTRING_LEN(privateKey) != olm_pk_private_key_length()) {
            rb_raise(rb_eval_string("ArgumentError"), "private_key has wrong size (must be %lu)", olm_pk_private_key_length());
        }
    }

    publicKeyLen = olm_pk_key_length();
    publicKeyPtr = malloc_or_raise(publicKeyLen);

    if (olm_pk_key_from_private(this,
            publicKeyPtr, publicKeyLen,
            RSTRING_PTR(privateKey), RSTRING_LEN(privateKey)) == olm_error()) {
        free(publicKeyPtr);
        raise_olm_error(olm_pk_decryption_last_error(this));
    }

    rb_iv_set(self, "@public_key", rb_str_new(publicKeyPtr, publicKeyLen));
    free(publicKeyPtr);

    return self;
}

Instance Attribute Details

#public_keyObject (readonly)

Instance Method Details

#decrypt(pkMessage) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'ext/self_crypto/pk_decryption.c', line 67

static VALUE pk_decrypt(VALUE self, VALUE pkMessage) {
    OlmPkDecryption *this;
    size_t plaintextLen;
    char *plaintextPtr;
    VALUE ephemeral, mac, ciphertext, retval;
    TypedData_Get_Struct(self, OlmPkDecryption, &olm_pk_decryption_type, this);

    ephemeral = rb_funcall(pkMessage, rb_intern("ephemeral_key"), 0);
    Check_Type(ephemeral, T_STRING);
    mac = rb_funcall(pkMessage, rb_intern("mac"), 0);
    Check_Type(mac, T_STRING);
    ciphertext = rb_funcall(pkMessage, rb_intern("cipher_text"), 0);
    Check_Type(ciphertext, T_STRING);

    plaintextLen = olm_pk_max_plaintext_length(this, RSTRING_LEN(ciphertext));
    plaintextPtr = malloc_or_raise(plaintextLen);

    plaintextLen = olm_pk_decrypt(this,
                       RSTRING_PTR(ephemeral), RSTRING_LEN(ephemeral),
                       RSTRING_PTR(mac), RSTRING_LEN(mac),
                       RSTRING_PTR(ciphertext), RSTRING_LEN(ciphertext),
                       plaintextPtr, plaintextLen);
    if (plaintextLen == olm_error()) {
        free(plaintextPtr);
        raise_olm_error(olm_pk_decryption_last_error(this));
    }

    retval = rb_str_new(plaintextPtr, plaintextLen);
    free(plaintextPtr);

    return retval;
}

#private_keyObject



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

static VALUE private_key(VALUE self) {
    OlmPkDecryption *this;
    size_t privkeyLen;
    char *privkeyPtr;
    VALUE retval;
    TypedData_Get_Struct(self, OlmPkDecryption, &olm_pk_decryption_type, this);

    privkeyLen = olm_pk_private_key_length();
    privkeyPtr = malloc_or_raise(privkeyLen);

    if (olm_pk_get_private_key(this, privkeyPtr, privkeyLen) == olm_error()) {
        free(privkeyPtr);
        raise_olm_error(olm_pk_decryption_last_error(this));
    }

    retval = rb_str_new(privkeyPtr, privkeyLen);
    free(privkeyPtr);
    return retval;
}