Example program to test AES-GCM functionality. Used for a workshop

Dependencies:   mbed

Committer:
HannesTschofenig
Date:
Thu Sep 27 06:34:22 2018 +0000
Revision:
0:796d0f61a05b
Example AES-GCM test program

Who changed what in which revision?

UserRevisionLine numberNew contents of line
HannesTschofenig 0:796d0f61a05b 1 /**
HannesTschofenig 0:796d0f61a05b 2 * \file pem.h
HannesTschofenig 0:796d0f61a05b 3 *
HannesTschofenig 0:796d0f61a05b 4 * \brief Privacy Enhanced Mail (PEM) decoding
HannesTschofenig 0:796d0f61a05b 5 *
HannesTschofenig 0:796d0f61a05b 6 * Copyright (C) 2006-2013, Brainspark B.V.
HannesTschofenig 0:796d0f61a05b 7 *
HannesTschofenig 0:796d0f61a05b 8 * This file is part of PolarSSL (http://www.polarssl.org)
HannesTschofenig 0:796d0f61a05b 9 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
HannesTschofenig 0:796d0f61a05b 10 *
HannesTschofenig 0:796d0f61a05b 11 * All rights reserved.
HannesTschofenig 0:796d0f61a05b 12 *
HannesTschofenig 0:796d0f61a05b 13 * This program is free software; you can redistribute it and/or modify
HannesTschofenig 0:796d0f61a05b 14 * it under the terms of the GNU General Public License as published by
HannesTschofenig 0:796d0f61a05b 15 * the Free Software Foundation; either version 2 of the License, or
HannesTschofenig 0:796d0f61a05b 16 * (at your option) any later version.
HannesTschofenig 0:796d0f61a05b 17 *
HannesTschofenig 0:796d0f61a05b 18 * This program is distributed in the hope that it will be useful,
HannesTschofenig 0:796d0f61a05b 19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
HannesTschofenig 0:796d0f61a05b 20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
HannesTschofenig 0:796d0f61a05b 21 * GNU General Public License for more details.
HannesTschofenig 0:796d0f61a05b 22 *
HannesTschofenig 0:796d0f61a05b 23 * You should have received a copy of the GNU General Public License along
HannesTschofenig 0:796d0f61a05b 24 * with this program; if not, write to the Free Software Foundation, Inc.,
HannesTschofenig 0:796d0f61a05b 25 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
HannesTschofenig 0:796d0f61a05b 26 */
HannesTschofenig 0:796d0f61a05b 27 #ifndef POLARSSL_PEM_H
HannesTschofenig 0:796d0f61a05b 28 #define POLARSSL_PEM_H
HannesTschofenig 0:796d0f61a05b 29
HannesTschofenig 0:796d0f61a05b 30 #include <string.h>
HannesTschofenig 0:796d0f61a05b 31
HannesTschofenig 0:796d0f61a05b 32 /**
HannesTschofenig 0:796d0f61a05b 33 * \name PEM Error codes
HannesTschofenig 0:796d0f61a05b 34 * These error codes are returned in case of errors reading the
HannesTschofenig 0:796d0f61a05b 35 * PEM data.
HannesTschofenig 0:796d0f61a05b 36 * \{
HannesTschofenig 0:796d0f61a05b 37 */
HannesTschofenig 0:796d0f61a05b 38 #define POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT -0x1080 /**< No PEM header or footer found. */
HannesTschofenig 0:796d0f61a05b 39 #define POLARSSL_ERR_PEM_INVALID_DATA -0x1100 /**< PEM string is not as expected. */
HannesTschofenig 0:796d0f61a05b 40 #define POLARSSL_ERR_PEM_MALLOC_FAILED -0x1180 /**< Failed to allocate memory. */
HannesTschofenig 0:796d0f61a05b 41 #define POLARSSL_ERR_PEM_INVALID_ENC_IV -0x1200 /**< RSA IV is not in hex-format. */
HannesTschofenig 0:796d0f61a05b 42 #define POLARSSL_ERR_PEM_UNKNOWN_ENC_ALG -0x1280 /**< Unsupported key encryption algorithm. */
HannesTschofenig 0:796d0f61a05b 43 #define POLARSSL_ERR_PEM_PASSWORD_REQUIRED -0x1300 /**< Private key password can't be empty. */
HannesTschofenig 0:796d0f61a05b 44 #define POLARSSL_ERR_PEM_PASSWORD_MISMATCH -0x1380 /**< Given private key password does not allow for correct decryption. */
HannesTschofenig 0:796d0f61a05b 45 #define POLARSSL_ERR_PEM_FEATURE_UNAVAILABLE -0x1400 /**< Unavailable feature, e.g. hashing/encryption combination. */
HannesTschofenig 0:796d0f61a05b 46 #define POLARSSL_ERR_PEM_BAD_INPUT_DATA -0x1480 /**< Bad input parameters to function. */
HannesTschofenig 0:796d0f61a05b 47 /* \} name */
HannesTschofenig 0:796d0f61a05b 48
HannesTschofenig 0:796d0f61a05b 49 #ifdef __cplusplus
HannesTschofenig 0:796d0f61a05b 50 extern "C" {
HannesTschofenig 0:796d0f61a05b 51 #endif
HannesTschofenig 0:796d0f61a05b 52
HannesTschofenig 0:796d0f61a05b 53 #if defined(POLARSSL_PEM_PARSE_C)
HannesTschofenig 0:796d0f61a05b 54 /**
HannesTschofenig 0:796d0f61a05b 55 * \brief PEM context structure
HannesTschofenig 0:796d0f61a05b 56 */
HannesTschofenig 0:796d0f61a05b 57 typedef struct
HannesTschofenig 0:796d0f61a05b 58 {
HannesTschofenig 0:796d0f61a05b 59 unsigned char *buf; /*!< buffer for decoded data */
HannesTschofenig 0:796d0f61a05b 60 size_t buflen; /*!< length of the buffer */
HannesTschofenig 0:796d0f61a05b 61 unsigned char *info; /*!< buffer for extra header information */
HannesTschofenig 0:796d0f61a05b 62 }
HannesTschofenig 0:796d0f61a05b 63 pem_context;
HannesTschofenig 0:796d0f61a05b 64
HannesTschofenig 0:796d0f61a05b 65 /**
HannesTschofenig 0:796d0f61a05b 66 * \brief PEM context setup
HannesTschofenig 0:796d0f61a05b 67 *
HannesTschofenig 0:796d0f61a05b 68 * \param ctx context to be initialized
HannesTschofenig 0:796d0f61a05b 69 */
HannesTschofenig 0:796d0f61a05b 70 void pem_init( pem_context *ctx );
HannesTschofenig 0:796d0f61a05b 71
HannesTschofenig 0:796d0f61a05b 72 /**
HannesTschofenig 0:796d0f61a05b 73 * \brief Read a buffer for PEM information and store the resulting
HannesTschofenig 0:796d0f61a05b 74 * data into the specified context buffers.
HannesTschofenig 0:796d0f61a05b 75 *
HannesTschofenig 0:796d0f61a05b 76 * \param ctx context to use
HannesTschofenig 0:796d0f61a05b 77 * \param header header string to seek and expect
HannesTschofenig 0:796d0f61a05b 78 * \param footer footer string to seek and expect
HannesTschofenig 0:796d0f61a05b 79 * \param data source data to look in
HannesTschofenig 0:796d0f61a05b 80 * \param pwd password for decryption (can be NULL)
HannesTschofenig 0:796d0f61a05b 81 * \param pwdlen length of password
HannesTschofenig 0:796d0f61a05b 82 * \param use_len destination for total length used (set after header is
HannesTschofenig 0:796d0f61a05b 83 * correctly read, so unless you get
HannesTschofenig 0:796d0f61a05b 84 * POLARSSL_ERR_PEM_BAD_INPUT_DATA or
HannesTschofenig 0:796d0f61a05b 85 * POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT, use_len is
HannesTschofenig 0:796d0f61a05b 86 * the length to skip)
HannesTschofenig 0:796d0f61a05b 87 *
HannesTschofenig 0:796d0f61a05b 88 * \note Attempts to check password correctness by verifying if
HannesTschofenig 0:796d0f61a05b 89 * the decrypted text starts with an ASN.1 sequence of
HannesTschofenig 0:796d0f61a05b 90 * appropriate length
HannesTschofenig 0:796d0f61a05b 91 *
HannesTschofenig 0:796d0f61a05b 92 * \return 0 on success, or a specific PEM error code
HannesTschofenig 0:796d0f61a05b 93 */
HannesTschofenig 0:796d0f61a05b 94 int pem_read_buffer( pem_context *ctx, const char *header, const char *footer,
HannesTschofenig 0:796d0f61a05b 95 const unsigned char *data,
HannesTschofenig 0:796d0f61a05b 96 const unsigned char *pwd,
HannesTschofenig 0:796d0f61a05b 97 size_t pwdlen, size_t *use_len );
HannesTschofenig 0:796d0f61a05b 98
HannesTschofenig 0:796d0f61a05b 99 /**
HannesTschofenig 0:796d0f61a05b 100 * \brief PEM context memory freeing
HannesTschofenig 0:796d0f61a05b 101 *
HannesTschofenig 0:796d0f61a05b 102 * \param ctx context to be freed
HannesTschofenig 0:796d0f61a05b 103 */
HannesTschofenig 0:796d0f61a05b 104 void pem_free( pem_context *ctx );
HannesTschofenig 0:796d0f61a05b 105 #endif /* POLARSSL_PEM_PARSE_C */
HannesTschofenig 0:796d0f61a05b 106
HannesTschofenig 0:796d0f61a05b 107 #if defined(POLARSSL_PEM_WRITE_C)
HannesTschofenig 0:796d0f61a05b 108 /**
HannesTschofenig 0:796d0f61a05b 109 * \brief Write a buffer of PEM information from a DER encoded
HannesTschofenig 0:796d0f61a05b 110 * buffer.
HannesTschofenig 0:796d0f61a05b 111 *
HannesTschofenig 0:796d0f61a05b 112 * \param header header string to write
HannesTschofenig 0:796d0f61a05b 113 * \param footer footer string to write
HannesTschofenig 0:796d0f61a05b 114 * \param der_data DER data to write
HannesTschofenig 0:796d0f61a05b 115 * \param der_len length of the DER data
HannesTschofenig 0:796d0f61a05b 116 * \param buf buffer to write to
HannesTschofenig 0:796d0f61a05b 117 * \param buf_len length of output buffer
HannesTschofenig 0:796d0f61a05b 118 * \param olen total length written / required (if buf_len is not enough)
HannesTschofenig 0:796d0f61a05b 119 *
HannesTschofenig 0:796d0f61a05b 120 * \return 0 on success, or a specific PEM or BASE64 error code. On
HannesTschofenig 0:796d0f61a05b 121 * POLARSSL_ERR_BASE64_BUFFER_TOO_SMALL olen is the required
HannesTschofenig 0:796d0f61a05b 122 * size.
HannesTschofenig 0:796d0f61a05b 123 */
HannesTschofenig 0:796d0f61a05b 124 int pem_write_buffer( const char *header, const char *footer,
HannesTschofenig 0:796d0f61a05b 125 const unsigned char *der_data, size_t der_len,
HannesTschofenig 0:796d0f61a05b 126 unsigned char *buf, size_t buf_len, size_t *olen );
HannesTschofenig 0:796d0f61a05b 127 #endif /* POLARSSL_PEM_WRITE_C */
HannesTschofenig 0:796d0f61a05b 128
HannesTschofenig 0:796d0f61a05b 129 #ifdef __cplusplus
HannesTschofenig 0:796d0f61a05b 130 }
HannesTschofenig 0:796d0f61a05b 131 #endif
HannesTschofenig 0:796d0f61a05b 132
HannesTschofenig 0:796d0f61a05b 133 #endif /* pem.h */
HannesTschofenig 0:796d0f61a05b 134
HannesTschofenig 0:796d0f61a05b 135