mbed client lightswitch demo

Dependencies:   mbed Socket lwip-eth lwip-sys lwip

Fork of mbed-client-classic-example-lwip by Austin Blackstone

Committer:
mbedAustin
Date:
Thu Jun 09 17:08:36 2016 +0000
Revision:
11:cada08fc8a70
Commit for public Consumption

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbedAustin 11:cada08fc8a70 1 /**
mbedAustin 11:cada08fc8a70 2 * \file gcm.h
mbedAustin 11:cada08fc8a70 3 *
mbedAustin 11:cada08fc8a70 4 * \brief Galois/Counter mode for 128-bit block ciphers
mbedAustin 11:cada08fc8a70 5 *
mbedAustin 11:cada08fc8a70 6 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
mbedAustin 11:cada08fc8a70 7 * SPDX-License-Identifier: Apache-2.0
mbedAustin 11:cada08fc8a70 8 *
mbedAustin 11:cada08fc8a70 9 * Licensed under the Apache License, Version 2.0 (the "License"); you may
mbedAustin 11:cada08fc8a70 10 * not use this file except in compliance with the License.
mbedAustin 11:cada08fc8a70 11 * You may obtain a copy of the License at
mbedAustin 11:cada08fc8a70 12 *
mbedAustin 11:cada08fc8a70 13 * http://www.apache.org/licenses/LICENSE-2.0
mbedAustin 11:cada08fc8a70 14 *
mbedAustin 11:cada08fc8a70 15 * Unless required by applicable law or agreed to in writing, software
mbedAustin 11:cada08fc8a70 16 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
mbedAustin 11:cada08fc8a70 17 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
mbedAustin 11:cada08fc8a70 18 * See the License for the specific language governing permissions and
mbedAustin 11:cada08fc8a70 19 * limitations under the License.
mbedAustin 11:cada08fc8a70 20 *
mbedAustin 11:cada08fc8a70 21 * This file is part of mbed TLS (https://tls.mbed.org)
mbedAustin 11:cada08fc8a70 22 */
mbedAustin 11:cada08fc8a70 23 #ifndef MBEDTLS_GCM_H
mbedAustin 11:cada08fc8a70 24 #define MBEDTLS_GCM_H
mbedAustin 11:cada08fc8a70 25
mbedAustin 11:cada08fc8a70 26 #include "cipher.h"
mbedAustin 11:cada08fc8a70 27
mbedAustin 11:cada08fc8a70 28 #include <stdint.h>
mbedAustin 11:cada08fc8a70 29
mbedAustin 11:cada08fc8a70 30 #define MBEDTLS_GCM_ENCRYPT 1
mbedAustin 11:cada08fc8a70 31 #define MBEDTLS_GCM_DECRYPT 0
mbedAustin 11:cada08fc8a70 32
mbedAustin 11:cada08fc8a70 33 #define MBEDTLS_ERR_GCM_AUTH_FAILED -0x0012 /**< Authenticated decryption failed. */
mbedAustin 11:cada08fc8a70 34 #define MBEDTLS_ERR_GCM_BAD_INPUT -0x0014 /**< Bad input parameters to function. */
mbedAustin 11:cada08fc8a70 35
mbedAustin 11:cada08fc8a70 36 #ifdef __cplusplus
mbedAustin 11:cada08fc8a70 37 extern "C" {
mbedAustin 11:cada08fc8a70 38 #endif
mbedAustin 11:cada08fc8a70 39
mbedAustin 11:cada08fc8a70 40 /**
mbedAustin 11:cada08fc8a70 41 * \brief GCM context structure
mbedAustin 11:cada08fc8a70 42 */
mbedAustin 11:cada08fc8a70 43 typedef struct {
mbedAustin 11:cada08fc8a70 44 mbedtls_cipher_context_t cipher_ctx;/*!< cipher context used */
mbedAustin 11:cada08fc8a70 45 uint64_t HL[16]; /*!< Precalculated HTable */
mbedAustin 11:cada08fc8a70 46 uint64_t HH[16]; /*!< Precalculated HTable */
mbedAustin 11:cada08fc8a70 47 uint64_t len; /*!< Total data length */
mbedAustin 11:cada08fc8a70 48 uint64_t add_len; /*!< Total add length */
mbedAustin 11:cada08fc8a70 49 unsigned char base_ectr[16];/*!< First ECTR for tag */
mbedAustin 11:cada08fc8a70 50 unsigned char y[16]; /*!< Y working value */
mbedAustin 11:cada08fc8a70 51 unsigned char buf[16]; /*!< buf working value */
mbedAustin 11:cada08fc8a70 52 int mode; /*!< Encrypt or Decrypt */
mbedAustin 11:cada08fc8a70 53 }
mbedAustin 11:cada08fc8a70 54 mbedtls_gcm_context;
mbedAustin 11:cada08fc8a70 55
mbedAustin 11:cada08fc8a70 56 /**
mbedAustin 11:cada08fc8a70 57 * \brief Initialize GCM context (just makes references valid)
mbedAustin 11:cada08fc8a70 58 * Makes the context ready for mbedtls_gcm_setkey() or
mbedAustin 11:cada08fc8a70 59 * mbedtls_gcm_free().
mbedAustin 11:cada08fc8a70 60 *
mbedAustin 11:cada08fc8a70 61 * \param ctx GCM context to initialize
mbedAustin 11:cada08fc8a70 62 */
mbedAustin 11:cada08fc8a70 63 void mbedtls_gcm_init( mbedtls_gcm_context *ctx );
mbedAustin 11:cada08fc8a70 64
mbedAustin 11:cada08fc8a70 65 /**
mbedAustin 11:cada08fc8a70 66 * \brief GCM initialization (encryption)
mbedAustin 11:cada08fc8a70 67 *
mbedAustin 11:cada08fc8a70 68 * \param ctx GCM context to be initialized
mbedAustin 11:cada08fc8a70 69 * \param cipher cipher to use (a 128-bit block cipher)
mbedAustin 11:cada08fc8a70 70 * \param key encryption key
mbedAustin 11:cada08fc8a70 71 * \param keybits must be 128, 192 or 256
mbedAustin 11:cada08fc8a70 72 *
mbedAustin 11:cada08fc8a70 73 * \return 0 if successful, or a cipher specific error code
mbedAustin 11:cada08fc8a70 74 */
mbedAustin 11:cada08fc8a70 75 int mbedtls_gcm_setkey( mbedtls_gcm_context *ctx,
mbedAustin 11:cada08fc8a70 76 mbedtls_cipher_id_t cipher,
mbedAustin 11:cada08fc8a70 77 const unsigned char *key,
mbedAustin 11:cada08fc8a70 78 unsigned int keybits );
mbedAustin 11:cada08fc8a70 79
mbedAustin 11:cada08fc8a70 80 /**
mbedAustin 11:cada08fc8a70 81 * \brief GCM buffer encryption/decryption using a block cipher
mbedAustin 11:cada08fc8a70 82 *
mbedAustin 11:cada08fc8a70 83 * \note On encryption, the output buffer can be the same as the input buffer.
mbedAustin 11:cada08fc8a70 84 * On decryption, the output buffer cannot be the same as input buffer.
mbedAustin 11:cada08fc8a70 85 * If buffers overlap, the output buffer must trail at least 8 bytes
mbedAustin 11:cada08fc8a70 86 * behind the input buffer.
mbedAustin 11:cada08fc8a70 87 *
mbedAustin 11:cada08fc8a70 88 * \param ctx GCM context
mbedAustin 11:cada08fc8a70 89 * \param mode MBEDTLS_GCM_ENCRYPT or MBEDTLS_GCM_DECRYPT
mbedAustin 11:cada08fc8a70 90 * \param length length of the input data
mbedAustin 11:cada08fc8a70 91 * \param iv initialization vector
mbedAustin 11:cada08fc8a70 92 * \param iv_len length of IV
mbedAustin 11:cada08fc8a70 93 * \param add additional data
mbedAustin 11:cada08fc8a70 94 * \param add_len length of additional data
mbedAustin 11:cada08fc8a70 95 * \param input buffer holding the input data
mbedAustin 11:cada08fc8a70 96 * \param output buffer for holding the output data
mbedAustin 11:cada08fc8a70 97 * \param tag_len length of the tag to generate
mbedAustin 11:cada08fc8a70 98 * \param tag buffer for holding the tag
mbedAustin 11:cada08fc8a70 99 *
mbedAustin 11:cada08fc8a70 100 * \return 0 if successful
mbedAustin 11:cada08fc8a70 101 */
mbedAustin 11:cada08fc8a70 102 int mbedtls_gcm_crypt_and_tag( mbedtls_gcm_context *ctx,
mbedAustin 11:cada08fc8a70 103 int mode,
mbedAustin 11:cada08fc8a70 104 size_t length,
mbedAustin 11:cada08fc8a70 105 const unsigned char *iv,
mbedAustin 11:cada08fc8a70 106 size_t iv_len,
mbedAustin 11:cada08fc8a70 107 const unsigned char *add,
mbedAustin 11:cada08fc8a70 108 size_t add_len,
mbedAustin 11:cada08fc8a70 109 const unsigned char *input,
mbedAustin 11:cada08fc8a70 110 unsigned char *output,
mbedAustin 11:cada08fc8a70 111 size_t tag_len,
mbedAustin 11:cada08fc8a70 112 unsigned char *tag );
mbedAustin 11:cada08fc8a70 113
mbedAustin 11:cada08fc8a70 114 /**
mbedAustin 11:cada08fc8a70 115 * \brief GCM buffer authenticated decryption using a block cipher
mbedAustin 11:cada08fc8a70 116 *
mbedAustin 11:cada08fc8a70 117 * \note On decryption, the output buffer cannot be the same as input buffer.
mbedAustin 11:cada08fc8a70 118 * If buffers overlap, the output buffer must trail at least 8 bytes
mbedAustin 11:cada08fc8a70 119 * behind the input buffer.
mbedAustin 11:cada08fc8a70 120 *
mbedAustin 11:cada08fc8a70 121 * \param ctx GCM context
mbedAustin 11:cada08fc8a70 122 * \param length length of the input data
mbedAustin 11:cada08fc8a70 123 * \param iv initialization vector
mbedAustin 11:cada08fc8a70 124 * \param iv_len length of IV
mbedAustin 11:cada08fc8a70 125 * \param add additional data
mbedAustin 11:cada08fc8a70 126 * \param add_len length of additional data
mbedAustin 11:cada08fc8a70 127 * \param tag buffer holding the tag
mbedAustin 11:cada08fc8a70 128 * \param tag_len length of the tag
mbedAustin 11:cada08fc8a70 129 * \param input buffer holding the input data
mbedAustin 11:cada08fc8a70 130 * \param output buffer for holding the output data
mbedAustin 11:cada08fc8a70 131 *
mbedAustin 11:cada08fc8a70 132 * \return 0 if successful and authenticated,
mbedAustin 11:cada08fc8a70 133 * MBEDTLS_ERR_GCM_AUTH_FAILED if tag does not match
mbedAustin 11:cada08fc8a70 134 */
mbedAustin 11:cada08fc8a70 135 int mbedtls_gcm_auth_decrypt( mbedtls_gcm_context *ctx,
mbedAustin 11:cada08fc8a70 136 size_t length,
mbedAustin 11:cada08fc8a70 137 const unsigned char *iv,
mbedAustin 11:cada08fc8a70 138 size_t iv_len,
mbedAustin 11:cada08fc8a70 139 const unsigned char *add,
mbedAustin 11:cada08fc8a70 140 size_t add_len,
mbedAustin 11:cada08fc8a70 141 const unsigned char *tag,
mbedAustin 11:cada08fc8a70 142 size_t tag_len,
mbedAustin 11:cada08fc8a70 143 const unsigned char *input,
mbedAustin 11:cada08fc8a70 144 unsigned char *output );
mbedAustin 11:cada08fc8a70 145
mbedAustin 11:cada08fc8a70 146 /**
mbedAustin 11:cada08fc8a70 147 * \brief Generic GCM stream start function
mbedAustin 11:cada08fc8a70 148 *
mbedAustin 11:cada08fc8a70 149 * \param ctx GCM context
mbedAustin 11:cada08fc8a70 150 * \param mode MBEDTLS_GCM_ENCRYPT or MBEDTLS_GCM_DECRYPT
mbedAustin 11:cada08fc8a70 151 * \param iv initialization vector
mbedAustin 11:cada08fc8a70 152 * \param iv_len length of IV
mbedAustin 11:cada08fc8a70 153 * \param add additional data (or NULL if length is 0)
mbedAustin 11:cada08fc8a70 154 * \param add_len length of additional data
mbedAustin 11:cada08fc8a70 155 *
mbedAustin 11:cada08fc8a70 156 * \return 0 if successful
mbedAustin 11:cada08fc8a70 157 */
mbedAustin 11:cada08fc8a70 158 int mbedtls_gcm_starts( mbedtls_gcm_context *ctx,
mbedAustin 11:cada08fc8a70 159 int mode,
mbedAustin 11:cada08fc8a70 160 const unsigned char *iv,
mbedAustin 11:cada08fc8a70 161 size_t iv_len,
mbedAustin 11:cada08fc8a70 162 const unsigned char *add,
mbedAustin 11:cada08fc8a70 163 size_t add_len );
mbedAustin 11:cada08fc8a70 164
mbedAustin 11:cada08fc8a70 165 /**
mbedAustin 11:cada08fc8a70 166 * \brief Generic GCM update function. Encrypts/decrypts using the
mbedAustin 11:cada08fc8a70 167 * given GCM context. Expects input to be a multiple of 16
mbedAustin 11:cada08fc8a70 168 * bytes! Only the last call before mbedtls_gcm_finish() can be less
mbedAustin 11:cada08fc8a70 169 * than 16 bytes!
mbedAustin 11:cada08fc8a70 170 *
mbedAustin 11:cada08fc8a70 171 * \note On decryption, the output buffer cannot be the same as input buffer.
mbedAustin 11:cada08fc8a70 172 * If buffers overlap, the output buffer must trail at least 8 bytes
mbedAustin 11:cada08fc8a70 173 * behind the input buffer.
mbedAustin 11:cada08fc8a70 174 *
mbedAustin 11:cada08fc8a70 175 * \param ctx GCM context
mbedAustin 11:cada08fc8a70 176 * \param length length of the input data
mbedAustin 11:cada08fc8a70 177 * \param input buffer holding the input data
mbedAustin 11:cada08fc8a70 178 * \param output buffer for holding the output data
mbedAustin 11:cada08fc8a70 179 *
mbedAustin 11:cada08fc8a70 180 * \return 0 if successful or MBEDTLS_ERR_GCM_BAD_INPUT
mbedAustin 11:cada08fc8a70 181 */
mbedAustin 11:cada08fc8a70 182 int mbedtls_gcm_update( mbedtls_gcm_context *ctx,
mbedAustin 11:cada08fc8a70 183 size_t length,
mbedAustin 11:cada08fc8a70 184 const unsigned char *input,
mbedAustin 11:cada08fc8a70 185 unsigned char *output );
mbedAustin 11:cada08fc8a70 186
mbedAustin 11:cada08fc8a70 187 /**
mbedAustin 11:cada08fc8a70 188 * \brief Generic GCM finalisation function. Wraps up the GCM stream
mbedAustin 11:cada08fc8a70 189 * and generates the tag. The tag can have a maximum length of
mbedAustin 11:cada08fc8a70 190 * 16 bytes.
mbedAustin 11:cada08fc8a70 191 *
mbedAustin 11:cada08fc8a70 192 * \param ctx GCM context
mbedAustin 11:cada08fc8a70 193 * \param tag buffer for holding the tag (may be NULL if tag_len is 0)
mbedAustin 11:cada08fc8a70 194 * \param tag_len length of the tag to generate
mbedAustin 11:cada08fc8a70 195 *
mbedAustin 11:cada08fc8a70 196 * \return 0 if successful or MBEDTLS_ERR_GCM_BAD_INPUT
mbedAustin 11:cada08fc8a70 197 */
mbedAustin 11:cada08fc8a70 198 int mbedtls_gcm_finish( mbedtls_gcm_context *ctx,
mbedAustin 11:cada08fc8a70 199 unsigned char *tag,
mbedAustin 11:cada08fc8a70 200 size_t tag_len );
mbedAustin 11:cada08fc8a70 201
mbedAustin 11:cada08fc8a70 202 /**
mbedAustin 11:cada08fc8a70 203 * \brief Free a GCM context and underlying cipher sub-context
mbedAustin 11:cada08fc8a70 204 *
mbedAustin 11:cada08fc8a70 205 * \param ctx GCM context to free
mbedAustin 11:cada08fc8a70 206 */
mbedAustin 11:cada08fc8a70 207 void mbedtls_gcm_free( mbedtls_gcm_context *ctx );
mbedAustin 11:cada08fc8a70 208
mbedAustin 11:cada08fc8a70 209 /**
mbedAustin 11:cada08fc8a70 210 * \brief Checkup routine
mbedAustin 11:cada08fc8a70 211 *
mbedAustin 11:cada08fc8a70 212 * \return 0 if successful, or 1 if the test failed
mbedAustin 11:cada08fc8a70 213 */
mbedAustin 11:cada08fc8a70 214 int mbedtls_gcm_self_test( int verbose );
mbedAustin 11:cada08fc8a70 215
mbedAustin 11:cada08fc8a70 216 #ifdef __cplusplus
mbedAustin 11:cada08fc8a70 217 }
mbedAustin 11:cada08fc8a70 218 #endif
mbedAustin 11:cada08fc8a70 219
mbedAustin 11:cada08fc8a70 220 #endif /* gcm.h */