Add AES_CBC_CTS ciphers to providers
[openssl.git] / providers / implementations / ciphers / cipher_chacha20_poly1305_hw.c
index 51e5275b7eeb781e41ad3dd144cf66201ce30e14..bd99a9fb4e203f68f0c039e09fcc712fdc4408d1 100644 (file)
@@ -9,6 +9,7 @@
 
 /* chacha20_poly1305 cipher implementation */
 
+#include "internal/endian.h"
 #include "cipher_chacha20_poly1305.h"
 
 static int chacha_poly1305_tls_init(PROV_CIPHER_CTX *bctx,
@@ -117,10 +118,7 @@ static int chacha20_poly1305_tls_cipher(PROV_CIPHER_CTX *bctx,
     size_t tail, tohash_len, buf_len, plen = ctx->tls_payload_length;
     unsigned char *buf, *tohash, *ctr, storage[sizeof(zero) + 32];
 
-    const union {
-        long one;
-        char little;
-    } is_endian = { 1 };
+    DECLARE_IS_ENDIAN;
 
     if (len != plen + POLY1305_BLOCK_SIZE)
         return 0;
@@ -142,7 +140,7 @@ static int chacha20_poly1305_tls_cipher(PROV_CIPHER_CTX *bctx,
         ctx->len.text = plen;
 
         if (plen) {
-            if (ctx->enc)
+            if (bctx->enc)
                 ctr = xor128_encrypt_n_pad(out, in, ctr, plen);
             else
                 ctr = xor128_decrypt_n_pad(out, in, ctr, plen);
@@ -214,7 +212,7 @@ static int chacha20_poly1305_tls_cipher(PROV_CIPHER_CTX *bctx,
         Poly1305_Update(poly, zero, tail);
     }
 
-    if (is_endian.little) {
+    if (IS_LITTLE_ENDIAN) {
         memcpy(ctr, (unsigned char *)&ctx->len, POLY1305_BLOCK_SIZE);
     } else {
         ctr[0]  = (unsigned char)(ctx->len.aad);
@@ -252,6 +250,8 @@ static int chacha20_poly1305_tls_cipher(PROV_CIPHER_CTX *bctx,
                        len - POLY1305_BLOCK_SIZE);
             return 0;
         }
+        /* Strip the tag */
+        len -= POLY1305_BLOCK_SIZE;
     }
 
     *out_padlen = len;
@@ -271,10 +271,7 @@ static int chacha20_poly1305_aead_cipher(PROV_CIPHER_CTX *bctx,
     size_t olen = 0;
     int rv = 0;
 
-    const union {
-        long one;
-        char little;
-    } is_endian = { 1 };
+    DECLARE_IS_ENDIAN;
 
     if (!ctx->mac_inited) {
 #if !defined(OPENSSL_SMALL_FOOTPRINT)
@@ -345,7 +342,7 @@ static int chacha20_poly1305_aead_cipher(PROV_CIPHER_CTX *bctx,
         if ((rem = (size_t)ctx->len.text % POLY1305_BLOCK_SIZE))
             Poly1305_Update(poly, zero, POLY1305_BLOCK_SIZE - rem);
 
-        if (is_endian.little) {
+        if (IS_LITTLE_ENDIAN) {
             Poly1305_Update(poly, (unsigned char *)&ctx->len,
                             POLY1305_BLOCK_SIZE);
         } else {