title: Why AND a uint32 value with 0xFFFFFFFF?
body: <p>A friend of mine was looking through <a href="https://github.com/polarssl/polarssl/blob/master/library/sha2.c#L247">this open-source SSL code</a> at the functions for handling SHA encryption, and noticed this odd snippet:</p>
  
  <pre><code>ctx-&gt;total[0] += (uint32_t) ilen;         // ilen is of type size_t
  ctx-&gt;total[0] &amp;= 0xFFFFFFFF;              
  
  if( ctx-&gt;total[0] &lt; (uint32_t) ilen )
      ctx-&gt;total[1]++;
  </code></pre>
  
  <p>We can't figure out two things about this code.  First, it ANDs <code>ctx-&gt;total[0]</code> (of type <code>uint32_t</code>) with <code>0xFFFFFFFF</code>, which shouldn't do anything.  In binary, that's ANDing with all <code>1</code>s, which should yield the same value.  In my mind, then, these two lines are identical:</p>
  
  <pre><code>ctx-&gt;total[0] &amp;= 0xFFFFFFFF;
  ctx-&gt;total[0] =  ctx-&gt;total[0];
  </code></pre>
  
  <p>If I am right, why is this line there? Some security reason? If I am wrong, how and why?</p>
  
  <p>Second, we don't understand when that <code>if</code> would ever be true, assuming the AND doesn't do anything.  If the AND does nothing, then the <code>if</code> is essentially:</p>
  
  <pre><code>if (ctx-&gt;total[0] &lt; ctx-&gt;total[0])
  </code></pre>
  
  <p>which should never be true.  What are we missing? </p>
  
  <hr>
  
  <p>If you want to see the header file to convince yourself that <code>ctx-&gt;total[0]</code> is of type <code>uint32_t</code>, or for whatever other reason, you can find that <a href="https://github.com/polarssl/polarssl/blob/master/include/polarssl/sha2.h#L50">here</a>.</p>
  
  <p>Also, my first wild guess is that there's something sneaky happening when we cast <code>ilen</code> from <code>size_t</code> to <code>uint32_t</code>, but I'm still stuck and confused.</p>