Fixed a spurious warning on some compilers.
[BearSSL] / inc / bearssl_ssl.h
index 2a4fada..f2ecc37 100644 (file)
@@ -700,6 +700,110 @@ extern const br_sslrec_out_chapol_class br_sslrec_out_chapol_vtable;
 
 /* ===================================================================== */
 
+/**
+ * \brief Record decryption engine class, for CCM mode.
+ *
+ * This class type extends the decryption engine class with an
+ * initialisation method that receives the parameters needed
+ * for CCM processing: block cipher implementation, block cipher key,
+ * and 4-byte IV.
+ */
+typedef struct br_sslrec_in_ccm_class_ br_sslrec_in_ccm_class;
+struct br_sslrec_in_ccm_class_ {
+       /**
+        * \brief Superclass, as first vtable field.
+        */
+       br_sslrec_in_class inner;
+
+       /**
+        * \brief Engine initialisation method.
+        *
+        * This method sets the vtable field in the context.
+        *
+        * \param ctx           context to initialise.
+        * \param bc_impl       block cipher implementation (CTR+CBC).
+        * \param key           block cipher key.
+        * \param key_len       block cipher key length (in bytes).
+        * \param iv            static IV (4 bytes).
+        * \param tag_len       tag length (in bytes)
+        */
+       void (*init)(const br_sslrec_in_ccm_class **ctx,
+               const br_block_ctrcbc_class *bc_impl,
+               const void *key, size_t key_len,
+               const void *iv, size_t tag_len);
+};
+
+/**
+ * \brief Record encryption engine class, for CCM mode.
+ *
+ * This class type extends the encryption engine class with an
+ * initialisation method that receives the parameters needed
+ * for CCM processing: block cipher implementation, block cipher key,
+ * and 4-byte IV.
+ */
+typedef struct br_sslrec_out_ccm_class_ br_sslrec_out_ccm_class;
+struct br_sslrec_out_ccm_class_ {
+       /**
+        * \brief Superclass, as first vtable field.
+        */
+       br_sslrec_out_class inner;
+
+       /**
+        * \brief Engine initialisation method.
+        *
+        * This method sets the vtable field in the context.
+        *
+        * \param ctx           context to initialise.
+        * \param bc_impl       block cipher implementation (CTR+CBC).
+        * \param key           block cipher key.
+        * \param key_len       block cipher key length (in bytes).
+        * \param iv            static IV (4 bytes).
+        * \param tag_len       tag length (in bytes)
+        */
+       void (*init)(const br_sslrec_out_ccm_class **ctx,
+               const br_block_ctrcbc_class *bc_impl,
+               const void *key, size_t key_len,
+               const void *iv, size_t tag_len);
+};
+
+/**
+ * \brief Context structure for processing records with CCM.
+ *
+ * The same context structure is used for encrypting and decrypting.
+ *
+ * The first field points to the vtable. The other fields are opaque
+ * and shall not be accessed directly.
+ */
+typedef struct {
+       /** \brief Pointer to vtable. */
+       union {
+               const void *gen;
+               const br_sslrec_in_ccm_class *in;
+               const br_sslrec_out_ccm_class *out;
+       } vtable;
+#ifndef BR_DOXYGEN_IGNORE
+       uint64_t seq;
+       union {
+               const br_block_ctrcbc_class *vtable;
+               br_aes_gen_ctrcbc_keys aes;
+       } bc;
+       unsigned char iv[4];
+       size_t tag_len;
+#endif
+} br_sslrec_ccm_context;
+
+/**
+ * \brief Static, constant vtable for record decryption with CCM.
+ */
+extern const br_sslrec_in_ccm_class br_sslrec_in_ccm_vtable;
+
+/**
+ * \brief Static, constant vtable for record encryption with CCM.
+ */
+extern const br_sslrec_out_ccm_class br_sslrec_out_ccm_vtable;
+
+/* ===================================================================== */
+
 /**
  * \brief Type for session parameters, to be saved for session resumption.
  */
@@ -718,9 +822,9 @@ typedef struct {
 
 #ifndef BR_DOXYGEN_IGNORE
 /*
- * Maximum numnber of cipher suites supported by a client or server.
+ * Maximum number of cipher suites supported by a client or server.
  */
-#define BR_MAX_CIPHER_SUITES   40
+#define BR_MAX_CIPHER_SUITES   48
 #endif
 
 /**
@@ -813,6 +917,7 @@ typedef struct {
                br_sslrec_in_cbc_context cbc;
                br_sslrec_gcm_context gcm;
                br_sslrec_chapol_context chapol;
+               br_sslrec_ccm_context ccm;
        } in;
        union {
                const br_sslrec_out_class *vtable;
@@ -820,6 +925,7 @@ typedef struct {
                br_sslrec_out_cbc_context cbc;
                br_sslrec_gcm_context gcm;
                br_sslrec_chapol_context chapol;
+               br_sslrec_ccm_context ccm;
        } out;
 
        /*
@@ -992,6 +1098,7 @@ typedef struct {
        const br_block_cbcenc_class *iaes_cbcenc;
        const br_block_cbcdec_class *iaes_cbcdec;
        const br_block_ctr_class *iaes_ctr;
+       const br_block_ctrcbc_class *iaes_ctrcbc;
        const br_block_cbcenc_class *ides_cbcenc;
        const br_block_cbcdec_class *ides_cbcdec;
        br_ghash ighash;
@@ -1003,6 +1110,8 @@ typedef struct {
        const br_sslrec_out_gcm_class *igcm_out;
        const br_sslrec_in_chapol_class *ichapol_in;
        const br_sslrec_out_chapol_class *ichapol_out;
+       const br_sslrec_in_ccm_class *iccm_in;
+       const br_sslrec_out_ccm_class *iccm_out;
        const br_ec_impl *iec;
        br_rsa_pkcs1_vrfy irsavrfy;
        br_ecdsa_vrfy iecdsa;
@@ -1141,8 +1250,8 @@ static inline void
 br_ssl_engine_set_versions(br_ssl_engine_context *cc,
        unsigned version_min, unsigned version_max)
 {
-       cc->version_min = version_min;
-       cc->version_max = version_max;
+       cc->version_min = (uint16_t)version_min;
+       cc->version_max = (uint16_t)version_max;
 }
 
 /**
@@ -1215,7 +1324,7 @@ br_ssl_engine_set_protocol_names(br_ssl_engine_context *ctx,
        const char **names, size_t num)
 {
        ctx->protocol_names = names;
-       ctx->protocol_names_num = num;
+       ctx->protocol_names_num = (uint16_t)num;
 }
 
 /**
@@ -1451,6 +1560,31 @@ br_ssl_engine_set_poly1305(br_ssl_engine_context *cc,
  */
 void br_ssl_engine_set_default_chapol(br_ssl_engine_context *cc);
 
+/**
+ * \brief Set the AES/CTR+CBC implementation.
+ *
+ * \param cc     SSL engine context.
+ * \param impl   AES/CTR+CBC encryption/decryption implementation (or `NULL`).
+ */
+static inline void
+br_ssl_engine_set_aes_ctrcbc(br_ssl_engine_context *cc,
+       const br_block_ctrcbc_class *impl)
+{
+       cc->iaes_ctrcbc = impl;
+}
+
+/**
+ * \brief Set the "default" implementations for AES/CCM.
+ *
+ * This function configures in the engine the AES/CTR+CBC
+ * implementation that should provide best runtime performance on the local
+ * system, while still being safe (in particular, constant-time). It also
+ * sets the handlers for CCM records.
+ *
+ * \param cc   SSL engine context.
+ */
+void br_ssl_engine_set_default_aes_ccm(br_ssl_engine_context *cc);
+
 /**
  * \brief Set the record encryption and decryption engines for CBC + HMAC.
  *
@@ -1483,6 +1617,22 @@ br_ssl_engine_set_gcm(br_ssl_engine_context *cc,
        cc->igcm_out = impl_out;
 }
 
+/**
+ * \brief Set the record encryption and decryption engines for CCM.
+ *
+ * \param cc         SSL engine context.
+ * \param impl_in    record CCM decryption implementation (or `NULL`).
+ * \param impl_out   record CCM encryption implementation (or `NULL`).
+ */
+static inline void
+br_ssl_engine_set_ccm(br_ssl_engine_context *cc,
+       const br_sslrec_in_ccm_class *impl_in,
+       const br_sslrec_out_ccm_class *impl_out)
+{
+       cc->iccm_in = impl_in;
+       cc->iccm_out = impl_out;
+}
+
 /**
  * \brief Set the record encryption and decryption engines for
  * ChaCha20+Poly1305.
@@ -4090,6 +4240,16 @@ int br_sslio_close(br_sslio_context *cc);
 #define BR_TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256      0xC031
 #define BR_TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384      0xC032
 
+/* From RFC 6655 and 7251 */
+#define BR_TLS_RSA_WITH_AES_128_CCM                  0xC09C
+#define BR_TLS_RSA_WITH_AES_256_CCM                  0xC09D
+#define BR_TLS_RSA_WITH_AES_128_CCM_8                0xC0A0
+#define BR_TLS_RSA_WITH_AES_256_CCM_8                0xC0A1
+#define BR_TLS_ECDHE_ECDSA_WITH_AES_128_CCM          0xC0AC
+#define BR_TLS_ECDHE_ECDSA_WITH_AES_256_CCM          0xC0AD
+#define BR_TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8        0xC0AE
+#define BR_TLS_ECDHE_ECDSA_WITH_AES_256_CCM_8        0xC0AF
+
 /* From RFC 7905 */
 #define BR_TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256     0xCCA8
 #define BR_TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256   0xCCA9