mbed TLS Build

Dependents:   Slave-prot-prod

Committer:
williequesada
Date:
Tue Jun 04 16:03:38 2019 +0000
Revision:
1:1a219dea6cb5
Parent:
0:cdf462088d13
compartir a Pablo

Who changed what in which revision?

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