Class: SelfCrypto::PK::Encryption

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

Instance Method Summary collapse

Constructor Details

#initialize(recipientKey) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
# File 'ext/self_crypto/pk_encryption.c', line 33

static VALUE initialize(VALUE self, VALUE recipientKey) {
    OlmPkEncryption *this;
    TypedData_Get_Struct(self, OlmPkEncryption, &olm_pk_encryption_type, this);
    Check_Type(recipientKey, T_STRING);

    if (olm_pk_encryption_set_recipient_key(this,
            RSTRING_PTR(recipientKey), RSTRING_LEN(recipientKey)) == olm_error()) {
        raise_olm_error(olm_pk_encryption_last_error(this));
    }

    return self;
}

Instance Method Details

#encrypt(plaintext) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'ext/self_crypto/pk_encryption.c', line 46

static VALUE pk_encrypt(VALUE self, VALUE plaintext) {
    OlmPkEncryption *this;
    size_t ciphertextLen, macLen, ephemeralLen, randomLen;
    char *ciphertextPtr, *macPtr, *ephemeralPtr;
    VALUE retval;
    TypedData_Get_Struct(self, OlmPkEncryption, &olm_pk_encryption_type, this);
    Check_Type(plaintext, T_STRING);

    ciphertextLen = olm_pk_ciphertext_length(this, RSTRING_LEN(plaintext));
    ciphertextPtr = malloc_or_raise(ciphertextLen);
    macLen = olm_pk_mac_length(this);
    macPtr = malloc_or_raise(macLen);
    ephemeralLen = olm_pk_key_length();
    ephemeralPtr = malloc_or_raise(ephemeralLen);
    randomLen = olm_pk_encrypt_random_length(this);

    if (olm_pk_encrypt(this,
            RSTRING_PTR(plaintext), RSTRING_LEN(plaintext),
            ciphertextPtr, ciphertextLen,
            macPtr, macLen,
            ephemeralPtr, ephemeralLen,
            RSTRING_PTR(get_random(randomLen)), randomLen) == olm_error()) {
        free(ephemeralPtr);
        free(macPtr);
        free(ciphertextPtr);
        raise_olm_error(olm_pk_encryption_last_error(this));
    }

    retval = rb_funcall(rb_eval_string("SelfCrypto::PK::Message"), rb_intern("new"), 3,
            rb_str_new(ciphertextPtr, ciphertextLen),
            rb_str_new(macPtr, macLen),
            rb_str_new(ephemeralPtr, ephemeralLen));

    free(ephemeralPtr);
    free(macPtr);
    free(ciphertextPtr);

    return retval;
}