/* Add the message authentication code */
 
        if(digest_active(n->outdigest)) {
-               digest_create(n->outdigest, &inpkt->seqno, inpkt->len, (char *)&inpkt->seqno + inpkt->len);
+               if(!digest_create(n->outdigest, &inpkt->seqno, inpkt->len, (char *)&inpkt->seqno + inpkt->len)) {
+                       logger(DEBUG_TRAFFIC, LOG_ERR, "Error while encrypting packet to %s (%s)", n->name, n->hostname);
+                       goto end;
+               }
+
                inpkt->len += digest_length(n->outdigest);
        }
 
 
 
        while(outlen > 0) {
                /* Inner HMAC */
-               digest_create(digest, data, len + seedlen, data);
+               if(!digest_create(digest, data, len + seedlen, data)) {
+                       digest_close(digest);
+                       return false;
+               }
 
                /* Outer HMAC */
-               digest_create(digest, data, len + seedlen, hash);
+               if(!digest_create(digest, data, len + seedlen, hash)) {
+                       digest_close(digest);
+                       return false;
+               }
 
                /* XOR the results of the outer HMAC into the out buffer */
                for(int i = 0; i < len && i < outlen; i++)
 
 
        key[0] &= 0x7F;
 
-       cipher_set_key_from_rsa(c->outcipher, key, len, true);
+       if(!cipher_set_key_from_rsa(c->outcipher, key, len, true))
+               return false;
 
        if(debug_level >= DEBUG_SCARY_THINGS) {
                bin2hex(key, hexkey, len);
                return false;
        }
 
-       c->allow_request = CHAL_REPLY;
-
        /* Calculate the hash from the challenge we received */
 
-       digest_create(c->indigest, buffer, len, digest);
+       if(!digest_create(c->indigest, buffer, len, digest))
+               return false;
 
        /* Convert the hash to a hexadecimal formatted string */
 
 
        /* Send the reply */
 
+       c->allow_request = CHAL_REPLY;
+
        return send_request(c, "%d %s", CHAL_REPLY, buffer);
 }
 
 
                abort();
 
        randomize(key, keylen);
-       cipher_set_key(to->incipher, key, false);
-       digest_set_key(to->indigest, key, keylen);
+       if(!cipher_set_key(to->incipher, key, false))
+               abort();
+       if(!digest_set_key(to->indigest, key, keylen))
+               abort();
 
        bin2hex(key, key, keylen);
 
 
        /* Update our copy of the origin's packet key */
 
-       cipher_set_key(from->outcipher, key, true);
-       digest_set_key(from->outdigest, key, keylen);
+       if(!cipher_set_key(from->outcipher, key, true))
+               return false;
+       if(!digest_set_key(from->outdigest, key, keylen))
+               return false;
 
        from->status.validkey = true;
        from->sent_seqno = 0;
 
 
        if(s->outstate) {
                // If first handshake has finished, encrypt and HMAC
-               cipher_set_counter(s->outcipher, &seqno, sizeof seqno);
+               if(!cipher_set_counter(s->outcipher, &seqno, sizeof seqno))
+                       return false;
+
                if(!cipher_counter_xor(s->outcipher, buffer + 6, len + 1UL, buffer + 6))
                        return false;
 
 
        // Decrypt.
        memcpy(&seqno, buffer + 2, 4);
-       cipher_set_counter(s->incipher, &seqno, sizeof seqno);
+       if(!cipher_set_counter(s->incipher, &seqno, sizeof seqno))
+               return false;
        if(!cipher_counter_xor(s->incipher, buffer + 6, len - 4, buffer + 6))
                return false;