-/* Decrypts rest of the packet (after decrypting just the SILC header).
- After calling this function the packet is ready to be parsed by calling
- silc_packet_parse. If everything goes without errors this returns TRUE,
- if packet is malformed this returns FALSE. */
-
-static int silc_packet_decrypt_rest(SilcCipher cipher, SilcHmac hmac,
- SilcBuffer buffer)
-{
- if (cipher) {
-
- /* Pull MAC from packet before decryption */
- if (hmac) {
- if ((buffer->len - silc_hmac_len(hmac)) > SILC_PACKET_MIN_LEN) {
- silc_buffer_push_tail(buffer, silc_hmac_len(hmac));
- } else {
- SILC_LOG_ERROR(("Bad MAC length in packet, packet dropped"));
- return FALSE;
- }
- }
-
- SILC_LOG_DEBUG(("Decrypting rest of the packet"));
-
- /* Decrypt rest of the packet */
- silc_buffer_pull(buffer, SILC_PACKET_MIN_HEADER_LEN);
- silc_cipher_decrypt(cipher, buffer->data, buffer->data, buffer->len,
- silc_cipher_get_iv(cipher));
- silc_buffer_push(buffer, SILC_PACKET_MIN_HEADER_LEN);
-
- SILC_LOG_HEXDUMP(("Fully decrypted packet, len %d", buffer->len),
- buffer->data, buffer->len);
- }
-
- return TRUE;
-}
-
-/* Decrypts rest of the SILC Packet header that has been decrypted partly
- already. This decrypts the padding of the packet also. After calling
- this function the packet is ready to be parsed by calling function
- silc_packet_parse. This is used in special packet reception (protocol
- defines the way of decrypting special packets). */
-
-static int silc_packet_decrypt_rest_special(SilcCipher cipher,
- SilcHmac hmac,
- SilcBuffer buffer)
-{
- /* Decrypt rest of the header plus padding */
- if (cipher) {
- SilcUInt16 len;
-
- /* Pull MAC from packet before decryption */
- if (hmac) {
- if ((buffer->len - silc_hmac_len(hmac)) > SILC_PACKET_MIN_LEN) {
- silc_buffer_push_tail(buffer, silc_hmac_len(hmac));
- } else {
- SILC_LOG_ERROR(("Bad MAC length in packet, packet dropped"));
- return FALSE;
- }
- }
-
- SILC_LOG_DEBUG(("Decrypting rest of the header"));
-
- /* padding length + src id len + dst id len + header length - 16
- bytes already decrypted, gives the rest of the encrypted packet */
- len = (((SilcUInt8)buffer->data[4] + (SilcUInt8)buffer->data[6] +
- (SilcUInt8)buffer->data[7] + SILC_PACKET_HEADER_LEN) -
- SILC_PACKET_MIN_HEADER_LEN);
-
- silc_buffer_pull(buffer, SILC_PACKET_MIN_HEADER_LEN);
- if (len > buffer->len) {
- SILC_LOG_ERROR(("Garbage in header of packet, bad packet length, "
- "packet dropped"));
- return FALSE;
- }
- silc_cipher_decrypt(cipher, buffer->data, buffer->data, len,
- silc_cipher_get_iv(cipher));
- silc_buffer_push(buffer, SILC_PACKET_MIN_HEADER_LEN);
- SILC_LOG_HEXDUMP(("packet, len %d", buffer->len),
- buffer->data, buffer->len);
- }
-
- 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. */