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 ecdsa.h
HannesTschofenig 0:796d0f61a05b 3 *
HannesTschofenig 0:796d0f61a05b 4 * \brief Elliptic curve DSA
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_ECDSA_H
HannesTschofenig 0:796d0f61a05b 28 #define POLARSSL_ECDSA_H
HannesTschofenig 0:796d0f61a05b 29
HannesTschofenig 0:796d0f61a05b 30 #include "ecp.h"
HannesTschofenig 0:796d0f61a05b 31
HannesTschofenig 0:796d0f61a05b 32 #if defined(POLARSSL_ECDSA_DETERMINISTIC)
HannesTschofenig 0:796d0f61a05b 33 #include "md.h"
HannesTschofenig 0:796d0f61a05b 34 #endif
HannesTschofenig 0:796d0f61a05b 35
HannesTschofenig 0:796d0f61a05b 36 /**
HannesTschofenig 0:796d0f61a05b 37 * \brief ECDSA context structure
HannesTschofenig 0:796d0f61a05b 38 *
HannesTschofenig 0:796d0f61a05b 39 * \note Purposefully begins with the same members as struct ecp_keypair.
HannesTschofenig 0:796d0f61a05b 40 */
HannesTschofenig 0:796d0f61a05b 41 typedef struct
HannesTschofenig 0:796d0f61a05b 42 {
HannesTschofenig 0:796d0f61a05b 43 ecp_group grp; /*!< ellipitic curve used */
HannesTschofenig 0:796d0f61a05b 44 mpi d; /*!< secret signature key */
HannesTschofenig 0:796d0f61a05b 45 ecp_point Q; /*!< public signature key */
HannesTschofenig 0:796d0f61a05b 46 mpi r; /*!< first integer from signature */
HannesTschofenig 0:796d0f61a05b 47 mpi s; /*!< second integer from signature */
HannesTschofenig 0:796d0f61a05b 48 }
HannesTschofenig 0:796d0f61a05b 49 ecdsa_context;
HannesTschofenig 0:796d0f61a05b 50
HannesTschofenig 0:796d0f61a05b 51 #ifdef __cplusplus
HannesTschofenig 0:796d0f61a05b 52 extern "C" {
HannesTschofenig 0:796d0f61a05b 53 #endif
HannesTschofenig 0:796d0f61a05b 54
HannesTschofenig 0:796d0f61a05b 55 /**
HannesTschofenig 0:796d0f61a05b 56 * \brief Compute ECDSA signature of a previously hashed message
HannesTschofenig 0:796d0f61a05b 57 *
HannesTschofenig 0:796d0f61a05b 58 * \param grp ECP group
HannesTschofenig 0:796d0f61a05b 59 * \param r First output integer
HannesTschofenig 0:796d0f61a05b 60 * \param s Second output integer
HannesTschofenig 0:796d0f61a05b 61 * \param d Private signing key
HannesTschofenig 0:796d0f61a05b 62 * \param buf Message hash
HannesTschofenig 0:796d0f61a05b 63 * \param blen Length of buf
HannesTschofenig 0:796d0f61a05b 64 * \param f_rng RNG function
HannesTschofenig 0:796d0f61a05b 65 * \param p_rng RNG parameter
HannesTschofenig 0:796d0f61a05b 66 *
HannesTschofenig 0:796d0f61a05b 67 * \return 0 if successful,
HannesTschofenig 0:796d0f61a05b 68 * or a POLARSSL_ERR_ECP_XXX or POLARSSL_MPI_XXX error code
HannesTschofenig 0:796d0f61a05b 69 */
HannesTschofenig 0:796d0f61a05b 70 int ecdsa_sign( ecp_group *grp, mpi *r, mpi *s,
HannesTschofenig 0:796d0f61a05b 71 const mpi *d, const unsigned char *buf, size_t blen,
HannesTschofenig 0:796d0f61a05b 72 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
HannesTschofenig 0:796d0f61a05b 73
HannesTschofenig 0:796d0f61a05b 74 #if defined(POLARSSL_ECDSA_DETERMINISTIC)
HannesTschofenig 0:796d0f61a05b 75 /**
HannesTschofenig 0:796d0f61a05b 76 * \brief Compute ECDSA signature of a previously hashed message
HannesTschofenig 0:796d0f61a05b 77 * (deterministic version)
HannesTschofenig 0:796d0f61a05b 78 *
HannesTschofenig 0:796d0f61a05b 79 * \param grp ECP group
HannesTschofenig 0:796d0f61a05b 80 * \param r First output integer
HannesTschofenig 0:796d0f61a05b 81 * \param s Second output integer
HannesTschofenig 0:796d0f61a05b 82 * \param d Private signing key
HannesTschofenig 0:796d0f61a05b 83 * \param buf Message hash
HannesTschofenig 0:796d0f61a05b 84 * \param blen Length of buf
HannesTschofenig 0:796d0f61a05b 85 * \param md_alg MD algorithm used to hash the message
HannesTschofenig 0:796d0f61a05b 86 *
HannesTschofenig 0:796d0f61a05b 87 * \return 0 if successful,
HannesTschofenig 0:796d0f61a05b 88 * or a POLARSSL_ERR_ECP_XXX or POLARSSL_MPI_XXX error code
HannesTschofenig 0:796d0f61a05b 89 */
HannesTschofenig 0:796d0f61a05b 90 int ecdsa_sign_det( ecp_group *grp, mpi *r, mpi *s,
HannesTschofenig 0:796d0f61a05b 91 const mpi *d, const unsigned char *buf, size_t blen,
HannesTschofenig 0:796d0f61a05b 92 md_type_t md_alg );
HannesTschofenig 0:796d0f61a05b 93 #endif /* POLARSSL_ECDSA_DETERMINISTIC */
HannesTschofenig 0:796d0f61a05b 94
HannesTschofenig 0:796d0f61a05b 95 /**
HannesTschofenig 0:796d0f61a05b 96 * \brief Verify ECDSA signature of a previously hashed message
HannesTschofenig 0:796d0f61a05b 97 *
HannesTschofenig 0:796d0f61a05b 98 * \param grp ECP group
HannesTschofenig 0:796d0f61a05b 99 * \param buf Message hash
HannesTschofenig 0:796d0f61a05b 100 * \param blen Length of buf
HannesTschofenig 0:796d0f61a05b 101 * \param Q Public key to use for verification
HannesTschofenig 0:796d0f61a05b 102 * \param r First integer of the signature
HannesTschofenig 0:796d0f61a05b 103 * \param s Second integer of the signature
HannesTschofenig 0:796d0f61a05b 104 *
HannesTschofenig 0:796d0f61a05b 105 * \return 0 if successful,
HannesTschofenig 0:796d0f61a05b 106 * POLARSSL_ERR_ECP_BAD_INPUT_DATA if signature is invalid
HannesTschofenig 0:796d0f61a05b 107 * or a POLARSSL_ERR_ECP_XXX or POLARSSL_MPI_XXX error code
HannesTschofenig 0:796d0f61a05b 108 */
HannesTschofenig 0:796d0f61a05b 109 int ecdsa_verify( ecp_group *grp,
HannesTschofenig 0:796d0f61a05b 110 const unsigned char *buf, size_t blen,
HannesTschofenig 0:796d0f61a05b 111 const ecp_point *Q, const mpi *r, const mpi *s);
HannesTschofenig 0:796d0f61a05b 112
HannesTschofenig 0:796d0f61a05b 113 /**
HannesTschofenig 0:796d0f61a05b 114 * \brief Compute ECDSA signature and write it to buffer,
HannesTschofenig 0:796d0f61a05b 115 * serialized as defined in RFC 4492 page 20.
HannesTschofenig 0:796d0f61a05b 116 * (Not thread-safe to use same context in multiple threads)
HannesTschofenig 0:796d0f61a05b 117 *
HannesTschofenig 0:796d0f61a05b 118 * \param ctx ECDSA context
HannesTschofenig 0:796d0f61a05b 119 * \param hash Message hash
HannesTschofenig 0:796d0f61a05b 120 * \param hlen Length of hash
HannesTschofenig 0:796d0f61a05b 121 * \param sig Buffer that will hold the signature
HannesTschofenig 0:796d0f61a05b 122 * \param slen Length of the signature written
HannesTschofenig 0:796d0f61a05b 123 * \param f_rng RNG function
HannesTschofenig 0:796d0f61a05b 124 * \param p_rng RNG parameter
HannesTschofenig 0:796d0f61a05b 125 *
HannesTschofenig 0:796d0f61a05b 126 * \note The "sig" buffer must be at least as large as twice the
HannesTschofenig 0:796d0f61a05b 127 * size of the curve used, plus 7 (eg. 71 bytes if a 256-bit
HannesTschofenig 0:796d0f61a05b 128 * curve is used).
HannesTschofenig 0:796d0f61a05b 129 *
HannesTschofenig 0:796d0f61a05b 130 * \return 0 if successful,
HannesTschofenig 0:796d0f61a05b 131 * or a POLARSSL_ERR_ECP, POLARSSL_ERR_MPI or
HannesTschofenig 0:796d0f61a05b 132 * POLARSSL_ERR_ASN1 error code
HannesTschofenig 0:796d0f61a05b 133 */
HannesTschofenig 0:796d0f61a05b 134 int ecdsa_write_signature( ecdsa_context *ctx,
HannesTschofenig 0:796d0f61a05b 135 const unsigned char *hash, size_t hlen,
HannesTschofenig 0:796d0f61a05b 136 unsigned char *sig, size_t *slen,
HannesTschofenig 0:796d0f61a05b 137 int (*f_rng)(void *, unsigned char *, size_t),
HannesTschofenig 0:796d0f61a05b 138 void *p_rng );
HannesTschofenig 0:796d0f61a05b 139
HannesTschofenig 0:796d0f61a05b 140 #if defined(POLARSSL_ECDSA_DETERMINISTIC)
HannesTschofenig 0:796d0f61a05b 141 /**
HannesTschofenig 0:796d0f61a05b 142 * \brief Compute ECDSA signature and write it to buffer,
HannesTschofenig 0:796d0f61a05b 143 * serialized as defined in RFC 4492 page 20.
HannesTschofenig 0:796d0f61a05b 144 * Deterministic version, RFC 6979.
HannesTschofenig 0:796d0f61a05b 145 * (Not thread-safe to use same context in multiple threads)
HannesTschofenig 0:796d0f61a05b 146 *
HannesTschofenig 0:796d0f61a05b 147 * \param ctx ECDSA context
HannesTschofenig 0:796d0f61a05b 148 * \param hash Message hash
HannesTschofenig 0:796d0f61a05b 149 * \param hlen Length of hash
HannesTschofenig 0:796d0f61a05b 150 * \param sig Buffer that will hold the signature
HannesTschofenig 0:796d0f61a05b 151 * \param slen Length of the signature written
HannesTschofenig 0:796d0f61a05b 152 * \param md_alg MD algorithm used to hash the message
HannesTschofenig 0:796d0f61a05b 153 *
HannesTschofenig 0:796d0f61a05b 154 * \note The "sig" buffer must be at least as large as twice the
HannesTschofenig 0:796d0f61a05b 155 * size of the curve used, plus 7 (eg. 71 bytes if a 256-bit
HannesTschofenig 0:796d0f61a05b 156 * curve is used).
HannesTschofenig 0:796d0f61a05b 157 *
HannesTschofenig 0:796d0f61a05b 158 * \return 0 if successful,
HannesTschofenig 0:796d0f61a05b 159 * or a POLARSSL_ERR_ECP, POLARSSL_ERR_MPI or
HannesTschofenig 0:796d0f61a05b 160 * POLARSSL_ERR_ASN1 error code
HannesTschofenig 0:796d0f61a05b 161 */
HannesTschofenig 0:796d0f61a05b 162 int ecdsa_write_signature_det( ecdsa_context *ctx,
HannesTschofenig 0:796d0f61a05b 163 const unsigned char *hash, size_t hlen,
HannesTschofenig 0:796d0f61a05b 164 unsigned char *sig, size_t *slen,
HannesTschofenig 0:796d0f61a05b 165 md_type_t md_alg );
HannesTschofenig 0:796d0f61a05b 166 #endif /* POLARSSL_ECDSA_DETERMINISTIC */
HannesTschofenig 0:796d0f61a05b 167
HannesTschofenig 0:796d0f61a05b 168 /**
HannesTschofenig 0:796d0f61a05b 169 * \brief Read and verify an ECDSA signature
HannesTschofenig 0:796d0f61a05b 170 *
HannesTschofenig 0:796d0f61a05b 171 * \param ctx ECDSA context
HannesTschofenig 0:796d0f61a05b 172 * \param hash Message hash
HannesTschofenig 0:796d0f61a05b 173 * \param hlen Size of hash
HannesTschofenig 0:796d0f61a05b 174 * \param sig Signature to read and verify
HannesTschofenig 0:796d0f61a05b 175 * \param slen Size of sig
HannesTschofenig 0:796d0f61a05b 176 *
HannesTschofenig 0:796d0f61a05b 177 * \return 0 if successful,
HannesTschofenig 0:796d0f61a05b 178 * POLARSSL_ERR_ECP_BAD_INPUT_DATA if signature is invalid,
HannesTschofenig 0:796d0f61a05b 179 * POLARSSL_ERR_ECP_SIG_LEN_MISTMATCH if the signature is
HannesTschofenig 0:796d0f61a05b 180 * valid but its actual length is less than siglen,
HannesTschofenig 0:796d0f61a05b 181 * or a POLARSSL_ERR_ECP or POLARSSL_ERR_MPI error code
HannesTschofenig 0:796d0f61a05b 182 */
HannesTschofenig 0:796d0f61a05b 183 int ecdsa_read_signature( ecdsa_context *ctx,
HannesTschofenig 0:796d0f61a05b 184 const unsigned char *hash, size_t hlen,
HannesTschofenig 0:796d0f61a05b 185 const unsigned char *sig, size_t slen );
HannesTschofenig 0:796d0f61a05b 186
HannesTschofenig 0:796d0f61a05b 187 /**
HannesTschofenig 0:796d0f61a05b 188 * \brief Generate an ECDSA keypair on the given curve
HannesTschofenig 0:796d0f61a05b 189 *
HannesTschofenig 0:796d0f61a05b 190 * \param ctx ECDSA context in which the keypair should be stored
HannesTschofenig 0:796d0f61a05b 191 * \param gid Group (elliptic curve) to use. One of the various
HannesTschofenig 0:796d0f61a05b 192 * POLARSSL_ECP_DP_XXX macros depending on configuration.
HannesTschofenig 0:796d0f61a05b 193 * \param f_rng RNG function
HannesTschofenig 0:796d0f61a05b 194 * \param p_rng RNG parameter
HannesTschofenig 0:796d0f61a05b 195 *
HannesTschofenig 0:796d0f61a05b 196 * \return 0 on success, or a POLARSSL_ERR_ECP code.
HannesTschofenig 0:796d0f61a05b 197 */
HannesTschofenig 0:796d0f61a05b 198 int ecdsa_genkey( ecdsa_context *ctx, ecp_group_id gid,
HannesTschofenig 0:796d0f61a05b 199 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
HannesTschofenig 0:796d0f61a05b 200
HannesTschofenig 0:796d0f61a05b 201 /**
HannesTschofenig 0:796d0f61a05b 202 * \brief Set an ECDSA context from an EC key pair
HannesTschofenig 0:796d0f61a05b 203 *
HannesTschofenig 0:796d0f61a05b 204 * \param ctx ECDSA context to set
HannesTschofenig 0:796d0f61a05b 205 * \param key EC key to use
HannesTschofenig 0:796d0f61a05b 206 *
HannesTschofenig 0:796d0f61a05b 207 * \return 0 on success, or a POLARSSL_ERR_ECP code.
HannesTschofenig 0:796d0f61a05b 208 */
HannesTschofenig 0:796d0f61a05b 209 int ecdsa_from_keypair( ecdsa_context *ctx, const ecp_keypair *key );
HannesTschofenig 0:796d0f61a05b 210
HannesTschofenig 0:796d0f61a05b 211 /**
HannesTschofenig 0:796d0f61a05b 212 * \brief Initialize context
HannesTschofenig 0:796d0f61a05b 213 *
HannesTschofenig 0:796d0f61a05b 214 * \param ctx Context to initialize
HannesTschofenig 0:796d0f61a05b 215 */
HannesTschofenig 0:796d0f61a05b 216 void ecdsa_init( ecdsa_context *ctx );
HannesTschofenig 0:796d0f61a05b 217
HannesTschofenig 0:796d0f61a05b 218 /**
HannesTschofenig 0:796d0f61a05b 219 * \brief Free context
HannesTschofenig 0:796d0f61a05b 220 *
HannesTschofenig 0:796d0f61a05b 221 * \param ctx Context to free
HannesTschofenig 0:796d0f61a05b 222 */
HannesTschofenig 0:796d0f61a05b 223 void ecdsa_free( ecdsa_context *ctx );
HannesTschofenig 0:796d0f61a05b 224
HannesTschofenig 0:796d0f61a05b 225 /**
HannesTschofenig 0:796d0f61a05b 226 * \brief Checkup routine
HannesTschofenig 0:796d0f61a05b 227 *
HannesTschofenig 0:796d0f61a05b 228 * \return 0 if successful, or 1 if the test failed
HannesTschofenig 0:796d0f61a05b 229 */
HannesTschofenig 0:796d0f61a05b 230 int ecdsa_self_test( int verbose );
HannesTschofenig 0:796d0f61a05b 231
HannesTschofenig 0:796d0f61a05b 232 #ifdef __cplusplus
HannesTschofenig 0:796d0f61a05b 233 }
HannesTschofenig 0:796d0f61a05b 234 #endif
HannesTschofenig 0:796d0f61a05b 235
HannesTschofenig 0:796d0f61a05b 236 #endif /* ecdsa.h */
HannesTschofenig 0:796d0f61a05b 237
HannesTschofenig 0:796d0f61a05b 238