| 41 | } |
| 42 | |
| 43 | bool ChaCha20Poly1305AEAD::Crypt(uint64_t seqnr_payload, uint64_t seqnr_aad, int aad_pos, unsigned char* dest, size_t dest_len /* length of the output buffer for sanity checks */, const unsigned char* src, size_t src_len, bool is_encrypt) |
| 44 | { |
| 45 | // check buffer boundaries |
| 46 | if ( |
| 47 | // if we encrypt, make sure the source contains at least the expected AAD and the destination has at least space for the source + MAC |
| 48 | (is_encrypt && (src_len < CHACHA20_POLY1305_AEAD_AAD_LEN || dest_len < src_len + POLY1305_TAGLEN)) || |
| 49 | // if we decrypt, make sure the source contains at least the expected AAD+MAC and the destination has at least space for the source - MAC |
| 50 | (!is_encrypt && (src_len < CHACHA20_POLY1305_AEAD_AAD_LEN + POLY1305_TAGLEN || dest_len < src_len - POLY1305_TAGLEN))) { |
| 51 | return false; |
| 52 | } |
| 53 | |
| 54 | unsigned char expected_tag[POLY1305_TAGLEN], poly_key[POLY1305_KEYLEN]; |
| 55 | memset(poly_key, 0, sizeof(poly_key)); |
| 56 | m_chacha_main.SetIV(seqnr_payload); |
| 57 | |
| 58 | // block counter 0 for the poly1305 key |
| 59 | // use lower 32bytes for the poly1305 key |
| 60 | // (throws away 32 unused bytes (upper 32) from this ChaCha20 round) |
| 61 | m_chacha_main.Seek(0); |
| 62 | m_chacha_main.Crypt(poly_key, poly_key, sizeof(poly_key)); |
| 63 | |
| 64 | // if decrypting, verify the tag prior to decryption |
| 65 | if (!is_encrypt) { |
| 66 | const unsigned char* tag = src + src_len - POLY1305_TAGLEN; |
| 67 | poly1305_auth(expected_tag, src, src_len - POLY1305_TAGLEN, poly_key); |
| 68 | |
| 69 | // constant time compare the calculated MAC with the provided MAC |
| 70 | if (timingsafe_bcmp(expected_tag, tag, POLY1305_TAGLEN) != 0) { |
| 71 | memory_cleanse(expected_tag, sizeof(expected_tag)); |
| 72 | memory_cleanse(poly_key, sizeof(poly_key)); |
| 73 | return false; |
| 74 | } |
| 75 | memory_cleanse(expected_tag, sizeof(expected_tag)); |
| 76 | // MAC has been successfully verified, make sure we don't convert it in decryption |
| 77 | src_len -= POLY1305_TAGLEN; |
| 78 | } |
| 79 | |
| 80 | // calculate and cache the next 64byte keystream block if requested sequence number is not yet the cache |
| 81 | if (m_cached_aad_seqnr != seqnr_aad) { |
| 82 | m_cached_aad_seqnr = seqnr_aad; |
| 83 | m_chacha_header.SetIV(seqnr_aad); |
| 84 | m_chacha_header.Seek(0); |
| 85 | m_chacha_header.Keystream(m_aad_keystream_buffer, CHACHA20_ROUND_OUTPUT); |
| 86 | } |
| 87 | // crypt the AAD (3 bytes message length) with given position in AAD cipher instance keystream |
| 88 | dest[0] = src[0] ^ m_aad_keystream_buffer[aad_pos]; |
| 89 | dest[1] = src[1] ^ m_aad_keystream_buffer[aad_pos + 1]; |
| 90 | dest[2] = src[2] ^ m_aad_keystream_buffer[aad_pos + 2]; |
| 91 | |
| 92 | // Set the playload ChaCha instance block counter to 1 and crypt the payload |
| 93 | m_chacha_main.Seek(1); |
| 94 | m_chacha_main.Crypt(src + CHACHA20_POLY1305_AEAD_AAD_LEN, dest + CHACHA20_POLY1305_AEAD_AAD_LEN, src_len - CHACHA20_POLY1305_AEAD_AAD_LEN); |
| 95 | |
| 96 | // If encrypting, calculate and append tag |
| 97 | if (is_encrypt) { |
| 98 | // the poly1305 tag expands over the AAD (3 bytes length) & encrypted payload |
| 99 | poly1305_auth(dest + src_len, dest, src_len, poly_key); |
| 100 | } |
nothing calls this directly
no test coverage detected