-/******************************************************************************
-
- Channel Message Payload
-
-******************************************************************************/
-
-/* Channel Message Payload structure. Contents of this structure is parsed
- from SILC packets. */
-struct SilcChannelMessagePayloadStruct {
- unsigned short data_len;
- unsigned char *data;
- unsigned char *mac;
- unsigned char *iv;
-};
-
-/* Decrypts the channel message payload. */
-
-int silc_channel_message_payload_decrypt(unsigned char *data,
- size_t data_len,
- SilcCipher cipher,
- SilcHmac hmac)
-{
- unsigned int iv_len, mac_len;
- unsigned char *end, *mac, mac2[32];
-
- /* Decrypt the channel message. First push the IV out of the packet.
- The IV is used in the decryption process. Then decrypt the message.
- After decyprtion, take the MAC from the decrypted packet, compute MAC
- and compare the MACs. If they match, the decryption was successfull
- and we have the channel message ready to be displayed. */
- end = data + data_len;
-
- /* Push the IV out of the packet */
- iv_len = silc_cipher_get_block_len(cipher);
-
- /* Decrypt the channel message */
- silc_cipher_decrypt(cipher, data, data, data_len - iv_len, (end - iv_len));
-
- /* Take the MAC */
- if (hmac) {
- mac_len = silc_hmac_len(hmac);
- mac = (end - iv_len - mac_len);
-
- /* Check the MAC of the message */
- SILC_LOG_DEBUG(("Checking channel message MACs"));
- silc_hmac_make(hmac, data, (data_len - iv_len - mac_len), mac2, &mac_len);
- if (memcmp(mac, mac2, mac_len)) {
- SILC_LOG_DEBUG(("Channel message MACs does not match"));
- return FALSE;
- }
- SILC_LOG_DEBUG(("MAC is Ok"));
- }
-
- return TRUE;
-}
-
-/* Parses channel message payload returning new channel payload structure.
- This also decrypts it and checks the MAC. */
-
-SilcChannelMessagePayload
-silc_channel_message_payload_parse(SilcBuffer buffer,
- SilcCipher cipher,
- SilcHmac hmac)
-{
- SilcChannelMessagePayload new;
- int ret;
- unsigned int iv_len, mac_len;
-
- SILC_LOG_DEBUG(("Parsing channel message payload"));
-
- /* Decrypt the payload */
- ret = silc_channel_message_payload_decrypt(buffer->data, buffer->len,
- cipher, hmac);
- if (ret == FALSE)
- return NULL;
-
- iv_len = silc_cipher_get_block_len(cipher);
- mac_len = silc_hmac_len(hmac);
-
- new = silc_calloc(1, sizeof(*new));
-
- /* Parse the Channel Message Payload. Ignore the padding. */
- ret = silc_buffer_unformat(buffer,
- SILC_STR_UI16_NSTRING_ALLOC(&new->data,
- &new->data_len),
- SILC_STR_UI16_NSTRING(NULL, NULL),
- SILC_STR_UI_XNSTRING(&new->mac, mac_len),
- SILC_STR_UI_XNSTRING(&new->iv, iv_len),
- SILC_STR_END);
- if (ret == -1)
- goto err;
-
- if (new->data_len < 1 || new->data_len > buffer->len) {
- SILC_LOG_ERROR(("Incorrect channel messaeg payload in packet, "
- "packet dropped"));
- goto err;
- }
-
- return new;
-
- err:
- silc_channel_message_payload_free(new);
- return NULL;
-}
-
-/* Encodes channel message payload into a buffer and returns it. This is used
- to add channel message payload into a packet. As the channel payload is
- encrypted separately from other parts of the packet padding must
- be applied to the payload. */
-
-SilcBuffer silc_channel_message_payload_encode(unsigned short data_len,
- unsigned char *data,
- unsigned short iv_len,
- unsigned char *iv,
- SilcCipher cipher,
- SilcHmac hmac,
- SilcRng rng)
-{
- int i;
- SilcBuffer buffer;
- unsigned int len, pad_len, mac_len;
- unsigned char pad[SILC_PACKET_MAX_PADLEN];
- unsigned char mac[32];
-
- SILC_LOG_DEBUG(("Encoding channel message payload"));
-
- /* Calculate length of padding. IV is not included into the calculation
- since it is not encrypted. */
- mac_len = silc_hmac_len(hmac);
- len = 4 + data_len + mac_len;
- pad_len = SILC_PACKET_PADLEN((len + 2));
-
- /* Allocate channel payload buffer */
- len += pad_len + iv_len;
- buffer = silc_buffer_alloc(len);
-
- /* Generate padding */
- for (i = 0; i < pad_len; i++) pad[i] = silc_rng_get_byte(rng);
-
- /* Encode the Channel Message Payload */
- silc_buffer_pull_tail(buffer, 4 + data_len + pad_len);
- silc_buffer_format(buffer,
- SILC_STR_UI_SHORT(data_len),
- SILC_STR_UI_XNSTRING(data, data_len),
- SILC_STR_UI_SHORT(pad_len),
- SILC_STR_UI_XNSTRING(pad, pad_len),
- SILC_STR_END);
-
- /* Compute the MAC of the channel message data */
- silc_hmac_make(hmac, buffer->data, buffer->len, mac, &mac_len);
-
- /* Put rest of the data to the payload */
- silc_buffer_pull_tail(buffer, mac_len + iv_len);
- silc_buffer_pull(buffer, 4 + data_len + pad_len);
- silc_buffer_format(buffer,
- SILC_STR_UI_XNSTRING(mac, mac_len),
- SILC_STR_UI_XNSTRING(iv, iv_len),
- SILC_STR_END);
- silc_buffer_push(buffer, 4 + data_len + pad_len);
-
- /* Encrypt payload of the packet. This is encrypted with the channel key. */
- silc_cipher_encrypt(cipher, buffer->data, buffer->data,
- buffer->len - iv_len, iv);
-
- memset(pad, 0, sizeof(pad));
- memset(mac, 0, sizeof(mac));
-
- return buffer;
-}
-
-/* Free's Channel Message Payload */
-
-void silc_channel_message_payload_free(SilcChannelMessagePayload payload)
-{
- if (payload->data) {
- memset(payload->data, 0, payload->data_len);
- silc_free(payload->data);
- }
- silc_free(payload);
-}
-
-/* Return data */
-
-unsigned char *silc_channel_message_get_data(SilcChannelMessagePayload payload,
- unsigned int *data_len)
-{
- if (data_len)
- *data_len = payload->data_len;
-
- return payload->data;
-}
-
-/* Return MAC. The caller knows the length of the MAC */
-
-unsigned char *silc_channel_mesage_get_mac(SilcChannelMessagePayload payload)
-{
- return payload->mac;
-}
-
-/* Return IV. The caller knows the length of the IV */
-
-unsigned char *silc_channel_message_get_iv(SilcChannelMessagePayload payload)
-{
- return payload->iv;
-}