prng_bytes (iv_buf, iv_size);
/* Put packet ID in plaintext buffer or IV, depending on cipher mode */
- if (opt->packet_id)
+ if (opt->packet_id
+ && !packet_id_write (&opt->packet_id->send, buf, BOOL_CAST (opt->flags & CO_PACKET_ID_LONG_FORM), true))
{
- ASSERT (packet_id_write (&opt->packet_id->send, buf, BOOL_CAST (opt->flags & CO_PACKET_ID_LONG_FORM), true));
+ msg (D_CRYPT_ERRORS, "ENCRYPT ERROR: packet ID roll over");
+ goto err;
}
}
else if (mode == EVP_CIPH_CFB_MODE || mode == EVP_CIPH_OFB_MODE)
memset (iv_buf, 0, iv_size);
buf_set_write (&b, iv_buf, iv_size);
- ASSERT (packet_id_write (&opt->packet_id->send, &b, true, false));
+ if (!packet_id_write (&opt->packet_id->send, &b, true, false))
+ {
+ msg (D_CRYPT_ERRORS, "ENCRYPT ERROR: packet ID roll over");
+ goto err;
+ }
}
else /* We only support CBC, CFB, or OFB modes right now */
{
}
else /* No Encryption */
{
- if (opt->packet_id)
+ if (opt->packet_id
+ && !packet_id_write (&opt->packet_id->send, buf, BOOL_CAST (opt->flags & CO_PACKET_ID_LONG_FORM), true))
{
- ASSERT (packet_id_write (&opt->packet_id->send, buf, BOOL_CAST (opt->flags & CO_PACKET_ID_LONG_FORM), true));
+ msg (D_CRYPT_ERRORS, "ENCRYPT ERROR: packet ID roll over");
+ goto err;
}
work = *buf;
}
return true;
}
-static void
+static bool
packet_id_send_update(struct packet_id_send *p, bool long_form)
{
if (!p->time)
{
p->time = now;
}
- p->id++;
- if (!p->id)
+ if (p->id == PACKET_ID_MAX)
{
- ASSERT(long_form);
+ /* Packet ID only allowed to roll over if using long form and time has
+ * moved forward since last roll over.
+ */
+ if (!long_form || now <= p->time)
+ {
+ return false;
+ }
p->time = now;
- p->id = 1;
+ p->id = 0;
}
+ p->id++;
+ return true;
}
bool
packet_id_write (struct packet_id_send *p, struct buffer *buf, bool long_form,
bool prepend)
{
- packet_id_send_update(p, long_form);
+ if (!packet_id_send_update(p, long_form))
+ {
+ return false;
+ }
const packet_id_type net_id = htonpid(p->id);
const net_time_t net_time = htontime(p->time);