-
- SILC_LOG_DEBUG(("Decrypting rest of the header"));
-
- SILC_GET16_MSB(len1, &buffer->data[4]);
- SILC_GET16_MSB(len2, &buffer->data[6]);
-
- truelen = SILC_PACKET_HEADER_LEN + len1 + len2;
- padlen = SILC_PACKET_PADLEN(truelen);
- len1 = (truelen + padlen) - (SILC_PACKET_MIN_HEADER_LEN - 2);
-
- silc_buffer_pull(buffer, SILC_PACKET_MIN_HEADER_LEN - 2);
- if (len1 - 2 > buffer->len) {
- SILC_LOG_DEBUG(("Garbage in header of packet, bad packet length, "
- "packet dropped"));
- return FALSE;
- }
-
- cipher->cipher->decrypt(cipher->context, buffer->data + 2,
- buffer->data + 2, len1 - 2,
- cipher->iv);
- silc_buffer_push(buffer, SILC_PACKET_MIN_HEADER_LEN - 2);
- }
-
- return TRUE;
-}
-
-/* Decrypts a packet. This assumes that typical SILC packet is the
- packet to be decrypted and thus checks for normal and special SILC
- packets and can handle both of them. This also computes and checks
- the HMAC of the packet. If any other special or customized decryption
- processing is required this function cannot be used. This returns
- -1 on error, 0 when packet is normal packet and 1 when the packet
- is special and requires special processing.
-
- The `check_packet' is a callback funtion that this function will
- call. The callback relates to the checking whether the packet is
- normal packet or special packet and how it should be processed. If
- the callback return TRUE the packet is normal and FALSE if the packet
- is special and requires special procesing. */
-
-int silc_packet_decrypt(SilcCipher cipher, SilcHmac hmac,
- SilcBuffer buffer, SilcPacketContext *packet,
- SilcPacketCheckDecrypt check_packet,
- void *context)
-{
- int check;
-
- /* Decrypt start of the packet header */
- if (cipher)
- silc_cipher_decrypt(cipher, buffer->data + 2, buffer->data + 2,
- SILC_PACKET_MIN_HEADER_LEN - 2, cipher->iv);
-
- /* Do packet checking, whether the packet is normal or special */
- check = check_packet((SilcPacketType)buffer->data[3], buffer,
- packet, context);
-
- /* If the packet type is not any special type lets decrypt rest
- of the packet here. */
- if (check == TRUE) {
- /* Normal packet, decrypt rest of the packet */
- if (!silc_packet_decrypt_rest(cipher, hmac, buffer))
- return -1;
-
- /* Check MAC */
- if (!silc_packet_check_mac(hmac, buffer))
- return -1;
-
- return 0;
- } else {
- /* Packet requires special handling, decrypt rest of the header.
- This only decrypts. */
- if (!silc_packet_decrypt_rest_special(cipher, hmac, buffer))
- return -1;
-
- /* Check MAC */
- if (!silc_packet_check_mac(hmac, buffer))
- return -1;