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 asn1write.h
HannesTschofenig 0:796d0f61a05b 3 *
HannesTschofenig 0:796d0f61a05b 4 * \brief ASN.1 buffer writing functionality
HannesTschofenig 0:796d0f61a05b 5 *
HannesTschofenig 0:796d0f61a05b 6 * Copyright (C) 2006-2014, 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_ASN1_WRITE_H
HannesTschofenig 0:796d0f61a05b 28 #define POLARSSL_ASN1_WRITE_H
HannesTschofenig 0:796d0f61a05b 29
HannesTschofenig 0:796d0f61a05b 30 #include "asn1.h"
HannesTschofenig 0:796d0f61a05b 31
HannesTschofenig 0:796d0f61a05b 32 #define ASN1_CHK_ADD(g, f) do { if( ( ret = f ) < 0 ) return( ret ); else \
HannesTschofenig 0:796d0f61a05b 33 g += ret; } while( 0 )
HannesTschofenig 0:796d0f61a05b 34
HannesTschofenig 0:796d0f61a05b 35 #ifdef __cplusplus
HannesTschofenig 0:796d0f61a05b 36 extern "C" {
HannesTschofenig 0:796d0f61a05b 37 #endif
HannesTschofenig 0:796d0f61a05b 38
HannesTschofenig 0:796d0f61a05b 39 /**
HannesTschofenig 0:796d0f61a05b 40 * \brief Write a length field in ASN.1 format
HannesTschofenig 0:796d0f61a05b 41 * Note: function works backwards in data buffer
HannesTschofenig 0:796d0f61a05b 42 *
HannesTschofenig 0:796d0f61a05b 43 * \param p reference to current position pointer
HannesTschofenig 0:796d0f61a05b 44 * \param start start of the buffer (for bounds-checking)
HannesTschofenig 0:796d0f61a05b 45 * \param len the length to write
HannesTschofenig 0:796d0f61a05b 46 *
HannesTschofenig 0:796d0f61a05b 47 * \return the length written or a negative error code
HannesTschofenig 0:796d0f61a05b 48 */
HannesTschofenig 0:796d0f61a05b 49 int asn1_write_len( unsigned char **p, unsigned char *start, size_t len );
HannesTschofenig 0:796d0f61a05b 50
HannesTschofenig 0:796d0f61a05b 51 /**
HannesTschofenig 0:796d0f61a05b 52 * \brief Write a ASN.1 tag in ASN.1 format
HannesTschofenig 0:796d0f61a05b 53 * Note: function works backwards in data buffer
HannesTschofenig 0:796d0f61a05b 54 *
HannesTschofenig 0:796d0f61a05b 55 * \param p reference to current position pointer
HannesTschofenig 0:796d0f61a05b 56 * \param start start of the buffer (for bounds-checking)
HannesTschofenig 0:796d0f61a05b 57 * \param tag the tag to write
HannesTschofenig 0:796d0f61a05b 58 *
HannesTschofenig 0:796d0f61a05b 59 * \return the length written or a negative error code
HannesTschofenig 0:796d0f61a05b 60 */
HannesTschofenig 0:796d0f61a05b 61 int asn1_write_tag( unsigned char **p, unsigned char *start,
HannesTschofenig 0:796d0f61a05b 62 unsigned char tag );
HannesTschofenig 0:796d0f61a05b 63
HannesTschofenig 0:796d0f61a05b 64 /**
HannesTschofenig 0:796d0f61a05b 65 * \brief Write raw buffer data
HannesTschofenig 0:796d0f61a05b 66 * Note: function works backwards in data buffer
HannesTschofenig 0:796d0f61a05b 67 *
HannesTschofenig 0:796d0f61a05b 68 * \param p reference to current position pointer
HannesTschofenig 0:796d0f61a05b 69 * \param start start of the buffer (for bounds-checking)
HannesTschofenig 0:796d0f61a05b 70 * \param buf data buffer to write
HannesTschofenig 0:796d0f61a05b 71 * \param size length of the data buffer
HannesTschofenig 0:796d0f61a05b 72 *
HannesTschofenig 0:796d0f61a05b 73 * \return the length written or a negative error code
HannesTschofenig 0:796d0f61a05b 74 */
HannesTschofenig 0:796d0f61a05b 75 int asn1_write_raw_buffer( unsigned char **p, unsigned char *start,
HannesTschofenig 0:796d0f61a05b 76 const unsigned char *buf, size_t size );
HannesTschofenig 0:796d0f61a05b 77
HannesTschofenig 0:796d0f61a05b 78 #if defined(POLARSSL_BIGNUM_C)
HannesTschofenig 0:796d0f61a05b 79 /**
HannesTschofenig 0:796d0f61a05b 80 * \brief Write a big number (ASN1_INTEGER) in ASN.1 format
HannesTschofenig 0:796d0f61a05b 81 * Note: function works backwards in data buffer
HannesTschofenig 0:796d0f61a05b 82 *
HannesTschofenig 0:796d0f61a05b 83 * \param p reference to current position pointer
HannesTschofenig 0:796d0f61a05b 84 * \param start start of the buffer (for bounds-checking)
HannesTschofenig 0:796d0f61a05b 85 * \param X the MPI to write
HannesTschofenig 0:796d0f61a05b 86 *
HannesTschofenig 0:796d0f61a05b 87 * \return the length written or a negative error code
HannesTschofenig 0:796d0f61a05b 88 */
HannesTschofenig 0:796d0f61a05b 89 int asn1_write_mpi( unsigned char **p, unsigned char *start, mpi *X );
HannesTschofenig 0:796d0f61a05b 90 #endif /* POLARSSL_BIGNUM_C */
HannesTschofenig 0:796d0f61a05b 91
HannesTschofenig 0:796d0f61a05b 92 /**
HannesTschofenig 0:796d0f61a05b 93 * \brief Write a NULL tag (ASN1_NULL) with zero data in ASN.1 format
HannesTschofenig 0:796d0f61a05b 94 * Note: function works backwards in data buffer
HannesTschofenig 0:796d0f61a05b 95 *
HannesTschofenig 0:796d0f61a05b 96 * \param p reference to current position pointer
HannesTschofenig 0:796d0f61a05b 97 * \param start start of the buffer (for bounds-checking)
HannesTschofenig 0:796d0f61a05b 98 *
HannesTschofenig 0:796d0f61a05b 99 * \return the length written or a negative error code
HannesTschofenig 0:796d0f61a05b 100 */
HannesTschofenig 0:796d0f61a05b 101 int asn1_write_null( unsigned char **p, unsigned char *start );
HannesTschofenig 0:796d0f61a05b 102
HannesTschofenig 0:796d0f61a05b 103 /**
HannesTschofenig 0:796d0f61a05b 104 * \brief Write an OID tag (ASN1_OID) and data in ASN.1 format
HannesTschofenig 0:796d0f61a05b 105 * Note: function works backwards in data buffer
HannesTschofenig 0:796d0f61a05b 106 *
HannesTschofenig 0:796d0f61a05b 107 * \param p reference to current position pointer
HannesTschofenig 0:796d0f61a05b 108 * \param start start of the buffer (for bounds-checking)
HannesTschofenig 0:796d0f61a05b 109 * \param oid the OID to write
HannesTschofenig 0:796d0f61a05b 110 * \param oid_len length of the OID
HannesTschofenig 0:796d0f61a05b 111 *
HannesTschofenig 0:796d0f61a05b 112 * \return the length written or a negative error code
HannesTschofenig 0:796d0f61a05b 113 */
HannesTschofenig 0:796d0f61a05b 114 int asn1_write_oid( unsigned char **p, unsigned char *start,
HannesTschofenig 0:796d0f61a05b 115 const char *oid, size_t oid_len );
HannesTschofenig 0:796d0f61a05b 116
HannesTschofenig 0:796d0f61a05b 117 /**
HannesTschofenig 0:796d0f61a05b 118 * \brief Write an AlgorithmIdentifier sequence in ASN.1 format
HannesTschofenig 0:796d0f61a05b 119 * Note: function works backwards in data buffer
HannesTschofenig 0:796d0f61a05b 120 *
HannesTschofenig 0:796d0f61a05b 121 * \param p reference to current position pointer
HannesTschofenig 0:796d0f61a05b 122 * \param start start of the buffer (for bounds-checking)
HannesTschofenig 0:796d0f61a05b 123 * \param oid the OID of the algorithm
HannesTschofenig 0:796d0f61a05b 124 * \param oid_len length of the OID
HannesTschofenig 0:796d0f61a05b 125 * \param par_len length of parameters, which must be already written.
HannesTschofenig 0:796d0f61a05b 126 * If 0, NULL parameters are added
HannesTschofenig 0:796d0f61a05b 127 *
HannesTschofenig 0:796d0f61a05b 128 * \return the length written or a negative error code
HannesTschofenig 0:796d0f61a05b 129 */
HannesTschofenig 0:796d0f61a05b 130 int asn1_write_algorithm_identifier( unsigned char **p, unsigned char *start,
HannesTschofenig 0:796d0f61a05b 131 const char *oid, size_t oid_len,
HannesTschofenig 0:796d0f61a05b 132 size_t par_len );
HannesTschofenig 0:796d0f61a05b 133
HannesTschofenig 0:796d0f61a05b 134 /**
HannesTschofenig 0:796d0f61a05b 135 * \brief Write a boolean tag (ASN1_BOOLEAN) and value in ASN.1 format
HannesTschofenig 0:796d0f61a05b 136 * Note: function works backwards in data buffer
HannesTschofenig 0:796d0f61a05b 137 *
HannesTschofenig 0:796d0f61a05b 138 * \param p reference to current position pointer
HannesTschofenig 0:796d0f61a05b 139 * \param start start of the buffer (for bounds-checking)
HannesTschofenig 0:796d0f61a05b 140 * \param boolean 0 or 1
HannesTschofenig 0:796d0f61a05b 141 *
HannesTschofenig 0:796d0f61a05b 142 * \return the length written or a negative error code
HannesTschofenig 0:796d0f61a05b 143 */
HannesTschofenig 0:796d0f61a05b 144 int asn1_write_bool( unsigned char **p, unsigned char *start, int boolean );
HannesTschofenig 0:796d0f61a05b 145
HannesTschofenig 0:796d0f61a05b 146 /**
HannesTschofenig 0:796d0f61a05b 147 * \brief Write an int tag (ASN1_INTEGER) and value in ASN.1 format
HannesTschofenig 0:796d0f61a05b 148 * Note: function works backwards in data buffer
HannesTschofenig 0:796d0f61a05b 149 *
HannesTschofenig 0:796d0f61a05b 150 * \param p reference to current position pointer
HannesTschofenig 0:796d0f61a05b 151 * \param start start of the buffer (for bounds-checking)
HannesTschofenig 0:796d0f61a05b 152 * \param val the integer value
HannesTschofenig 0:796d0f61a05b 153 *
HannesTschofenig 0:796d0f61a05b 154 * \return the length written or a negative error code
HannesTschofenig 0:796d0f61a05b 155 */
HannesTschofenig 0:796d0f61a05b 156 int asn1_write_int( unsigned char **p, unsigned char *start, int val );
HannesTschofenig 0:796d0f61a05b 157
HannesTschofenig 0:796d0f61a05b 158 /**
HannesTschofenig 0:796d0f61a05b 159 * \brief Write a printable string tag (ASN1_PRINTABLE_STRING) and
HannesTschofenig 0:796d0f61a05b 160 * value in ASN.1 format
HannesTschofenig 0:796d0f61a05b 161 * Note: function works backwards in data buffer
HannesTschofenig 0:796d0f61a05b 162 *
HannesTschofenig 0:796d0f61a05b 163 * \param p reference to current position pointer
HannesTschofenig 0:796d0f61a05b 164 * \param start start of the buffer (for bounds-checking)
HannesTschofenig 0:796d0f61a05b 165 * \param text the text to write
HannesTschofenig 0:796d0f61a05b 166 * \param text_len length of the text
HannesTschofenig 0:796d0f61a05b 167 *
HannesTschofenig 0:796d0f61a05b 168 * \return the length written or a negative error code
HannesTschofenig 0:796d0f61a05b 169 */
HannesTschofenig 0:796d0f61a05b 170 int asn1_write_printable_string( unsigned char **p, unsigned char *start,
HannesTschofenig 0:796d0f61a05b 171 const char *text, size_t text_len );
HannesTschofenig 0:796d0f61a05b 172
HannesTschofenig 0:796d0f61a05b 173 /**
HannesTschofenig 0:796d0f61a05b 174 * \brief Write an IA5 string tag (ASN1_IA5_STRING) and
HannesTschofenig 0:796d0f61a05b 175 * value in ASN.1 format
HannesTschofenig 0:796d0f61a05b 176 * Note: function works backwards in data buffer
HannesTschofenig 0:796d0f61a05b 177 *
HannesTschofenig 0:796d0f61a05b 178 * \param p reference to current position pointer
HannesTschofenig 0:796d0f61a05b 179 * \param start start of the buffer (for bounds-checking)
HannesTschofenig 0:796d0f61a05b 180 * \param text the text to write
HannesTschofenig 0:796d0f61a05b 181 * \param text_len length of the text
HannesTschofenig 0:796d0f61a05b 182 *
HannesTschofenig 0:796d0f61a05b 183 * \return the length written or a negative error code
HannesTschofenig 0:796d0f61a05b 184 */
HannesTschofenig 0:796d0f61a05b 185 int asn1_write_ia5_string( unsigned char **p, unsigned char *start,
HannesTschofenig 0:796d0f61a05b 186 const char *text, size_t text_len );
HannesTschofenig 0:796d0f61a05b 187
HannesTschofenig 0:796d0f61a05b 188 /**
HannesTschofenig 0:796d0f61a05b 189 * \brief Write a bitstring tag (ASN1_BIT_STRING) and
HannesTschofenig 0:796d0f61a05b 190 * value in ASN.1 format
HannesTschofenig 0:796d0f61a05b 191 * Note: function works backwards in data buffer
HannesTschofenig 0:796d0f61a05b 192 *
HannesTschofenig 0:796d0f61a05b 193 * \param p reference to current position pointer
HannesTschofenig 0:796d0f61a05b 194 * \param start start of the buffer (for bounds-checking)
HannesTschofenig 0:796d0f61a05b 195 * \param buf the bitstring
HannesTschofenig 0:796d0f61a05b 196 * \param bits the total number of bits in the bitstring
HannesTschofenig 0:796d0f61a05b 197 *
HannesTschofenig 0:796d0f61a05b 198 * \return the length written or a negative error code
HannesTschofenig 0:796d0f61a05b 199 */
HannesTschofenig 0:796d0f61a05b 200 int asn1_write_bitstring( unsigned char **p, unsigned char *start,
HannesTschofenig 0:796d0f61a05b 201 const unsigned char *buf, size_t bits );
HannesTschofenig 0:796d0f61a05b 202
HannesTschofenig 0:796d0f61a05b 203 /**
HannesTschofenig 0:796d0f61a05b 204 * \brief Write an octet string tag (ASN1_OCTET_STRING) and
HannesTschofenig 0:796d0f61a05b 205 * value in ASN.1 format
HannesTschofenig 0:796d0f61a05b 206 * Note: function works backwards in data buffer
HannesTschofenig 0:796d0f61a05b 207 *
HannesTschofenig 0:796d0f61a05b 208 * \param p reference to current position pointer
HannesTschofenig 0:796d0f61a05b 209 * \param start start of the buffer (for bounds-checking)
HannesTschofenig 0:796d0f61a05b 210 * \param buf data buffer to write
HannesTschofenig 0:796d0f61a05b 211 * \param size length of the data buffer
HannesTschofenig 0:796d0f61a05b 212 *
HannesTschofenig 0:796d0f61a05b 213 * \return the length written or a negative error code
HannesTschofenig 0:796d0f61a05b 214 */
HannesTschofenig 0:796d0f61a05b 215 int asn1_write_octet_string( unsigned char **p, unsigned char *start,
HannesTschofenig 0:796d0f61a05b 216 const unsigned char *buf, size_t size );
HannesTschofenig 0:796d0f61a05b 217
HannesTschofenig 0:796d0f61a05b 218 /**
HannesTschofenig 0:796d0f61a05b 219 * \brief Create or find a specific named_data entry for writing in a
HannesTschofenig 0:796d0f61a05b 220 * sequence or list based on the OID. If not already in there,
HannesTschofenig 0:796d0f61a05b 221 * a new entry is added to the head of the list.
HannesTschofenig 0:796d0f61a05b 222 * Warning: Destructive behaviour for the val data!
HannesTschofenig 0:796d0f61a05b 223 *
HannesTschofenig 0:796d0f61a05b 224 * \param list Pointer to the location of the head of the list to seek
HannesTschofenig 0:796d0f61a05b 225 * through (will be updated in case of a new entry)
HannesTschofenig 0:796d0f61a05b 226 * \param oid The OID to look for
HannesTschofenig 0:796d0f61a05b 227 * \param oid_len Size of the OID
HannesTschofenig 0:796d0f61a05b 228 * \param val Data to store (can be NULL if you want to fill it by hand)
HannesTschofenig 0:796d0f61a05b 229 * \param val_len Minimum length of the data buffer needed
HannesTschofenig 0:796d0f61a05b 230 *
HannesTschofenig 0:796d0f61a05b 231 * \return NULL if if there was a memory allocation error, or a pointer
HannesTschofenig 0:796d0f61a05b 232 * to the new / existing entry.
HannesTschofenig 0:796d0f61a05b 233 */
HannesTschofenig 0:796d0f61a05b 234 asn1_named_data *asn1_store_named_data( asn1_named_data **list,
HannesTschofenig 0:796d0f61a05b 235 const char *oid, size_t oid_len,
HannesTschofenig 0:796d0f61a05b 236 const unsigned char *val,
HannesTschofenig 0:796d0f61a05b 237 size_t val_len );
HannesTschofenig 0:796d0f61a05b 238
HannesTschofenig 0:796d0f61a05b 239 #ifdef __cplusplus
HannesTschofenig 0:796d0f61a05b 240 }
HannesTschofenig 0:796d0f61a05b 241 #endif
HannesTschofenig 0:796d0f61a05b 242
HannesTschofenig 0:796d0f61a05b 243 #endif /* POLARSSL_ASN1_WRITE_H */
HannesTschofenig 0:796d0f61a05b 244
HannesTschofenig 0:796d0f61a05b 245