mbed-os-examples / Mbed OS mbed-os-example-tls-benchmark Featured
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002  *  Benchmark demonstration program
00003  *
00004  *  Copyright (C) 2006-2018, Arm Limited, All Rights Reserved
00005  *  SPDX-License-Identifier: Apache-2.0
00006  *
00007  *  Licensed under the Apache License, Version 2.0 (the "License"); you may
00008  *  not use this file except in compliance with the License.
00009  *  You may obtain a copy of the License at
00010  *
00011  *  http://www.apache.org/licenses/LICENSE-2.0
00012  *
00013  *  Unless required by applicable law or agreed to in writing, software
00014  *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00015  *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00016  *  See the License for the specific language governing permissions and
00017  *  limitations under the License.
00018  */
00019 
00020 #include "mbed.h"
00021 
00022 #if !defined(MBEDTLS_CONFIG_FILE)
00023 #include "mbedtls/config.h"
00024 #else
00025 #include MBEDTLS_CONFIG_FILE
00026 #endif /* MBEDTLS_CONFIG_FILE */
00027 
00028 #include "mbedtls/platform.h"
00029 #include "mbedtls/md4.h"
00030 #include "mbedtls/md5.h"
00031 #include "mbedtls/ripemd160.h"
00032 #include "mbedtls/sha1.h"
00033 #include "mbedtls/sha256.h"
00034 #include "mbedtls/sha512.h"
00035 #include "mbedtls/arc4.h"
00036 #include "mbedtls/des.h"
00037 #include "mbedtls/aes.h"
00038 #include "mbedtls/cmac.h"
00039 #include "mbedtls/blowfish.h"
00040 #include "mbedtls/camellia.h"
00041 #include "mbedtls/gcm.h"
00042 #include "mbedtls/ccm.h"
00043 #include "mbedtls/havege.h"
00044 #include "mbedtls/ctr_drbg.h"
00045 #include "mbedtls/hmac_drbg.h"
00046 #include "mbedtls/rsa.h"
00047 #include "mbedtls/pk.h"
00048 #include "mbedtls/dhm.h"
00049 #include "mbedtls/ecdsa.h"
00050 #include "mbedtls/ecdh.h"
00051 #include "mbedtls/error.h"
00052 
00053 #define RSA_PRIVATE_KEY_2048                                          \
00054 "-----BEGIN RSA PRIVATE KEY-----\n"                                   \
00055 "MIIEogIBAAKCAQEA2dwVr+IMGEtA2/MCP6fA5eb/6B18Bq6e7gw8brNPkm3E6LyR\n"  \
00056 "4DnMJVxZmw3bPDKBDoKzfntkMESi/Yw5UopLtVfjGfWeQWPClqffLZBsZ60BRAsg\n"  \
00057 "/g+ID5tgzxSuxzftypK59uexOVCAm7hCKZHGO3DbI7bLY27j7VAgEP7d/yuaz5Fx\n"  \
00058 "Kl/vu7shqrBoz6ABJVJD3KC8nUiMRUCXRINmxbyUUjA4DnicZv6+xrGKr36r6M8h\n"  \
00059 "VYLa5msKc8WzbnBWzpUsrpb4/r7ML+qp92gdSfVJ8/bLiU7h2C7faDA59uaqrFK9\n"  \
00060 "xmDdx7FaWhGQs3LWW6w1UNgkPS0FDYUslpsnsQIDAQABAoIBAC7IJNwM5V3+IuJY\n"  \
00061 "T35Nzo1PyloUosJokvY5KGz5Ejg2XBdCDu0gXCcVqqQyGIbXrYDpLhQV+RCoXHun\n"  \
00062 "tdN0oQdC5SB47s/J1Uo2qCUHo0+sBd6PqTkFKsl3KxWssk9TQjvCwC412IefMs69\n"  \
00063 "hW+ZvwCanmQP56LleApIr2oW4KLfW8Ry/QfZlua+dizctdN7+H1mWwgZQTY9T27J\n"  \
00064 "6RtGRA5NVkKVPzIHVJfdpKoO7xGg1g06aEbPB/VmGvZaaFWWnaf7uRvFjLZecBLu\n"  \
00065 "QSx2DA/GDjirlDYj99PJb7DtB4xRtKzsyw0o+xapC8w6OtIl/3xFt9moCu2jGrsx\n"  \
00066 "vpjHdfECgYEA7fSACRseIs9gAIVX8wq6gayTpA47DHYWAD6IQfIj35SJ+AgsvbFF\n"  \
00067 "4AmrwDhcJVPmDy1N4nLBfyGAMt/2CfiYkdkW6QFX/ULRMMBL/G7kWV8hYQDICB2g\n"  \
00068 "xaMRN1lPCmFq6BkSWjwIYTnYDFBDWVm1GVT8TMtJoM8Erej9qC0PeFUCgYEA6mF3\n"  \
00069 "bigO3t8f5sig+XepaftEUbkJMzo72TVRnIR2ycdR2ihelPQ+25g9dwV0ZA5XXhBS\n"  \
00070 "DKOABWjMM739Mwmy9v26Dlmu9R01zHQktMvtEAyfz7lk2NF0aMuj8285OJUBf9bz\n"  \
00071 "Cq3MjtMCD+4CZ6iaEqCdUKOuxfpx5cWVJV+qve0CgYBhD1YaYMFOGaBjFgDl1f51\n"  \
00072 "Xltqk5NqZdBbkSYrIAWZ8RDF5y+4wFJsLAWuhk6vuyUgE66tK3nZzWRpXAkT0B8L\n"  \
00073 "fq1lpXKqj1KcvBNCiEkEW1VWJ+dvyAYIF5eyJ++hoFLnETL3M32HivyhKSwPihPg\n"  \
00074 "nVW8TT9fJJIYDe1JZ/fjcQKBgHJfv7UsrR0LSvkG3K8AOtbx+8PZhOjPuRbk0v+L\n"  \
00075 "EKCkuIe5/XW4vtfQMeZb7hFJgk7vrepm+vkoy8VQKDf4urGW3W1VTHBmobM01hi4\n"  \
00076 "DuYvEul+Mf0wMRtWjJolo4m+BO5KiW2jpFfqFm6JmfjVqOIAKOSKC6am8V/MDF0h\n"  \
00077 "kyN9AoGAT9oOiEXMolbkDZw/QCaBiRoAGlGlNYUkJ+58U6OjIZLISw6aFv+Y2uE0\n"  \
00078 "mEImItjuYZtSYKblWikp6ldPoKlt9bwEFe3c6IZ8kJ3+xyEyAGrvjXjEY7PzP6dp\n"  \
00079 "Ajbjp9X9uocEBv9W/KsBLdQ7yizcL/toHwdBO4vQqmqTvAc5IIw=\n"              \
00080 "-----END RSA PRIVATE KEY-----\n"
00081 
00082 #define RSA_PRIVATE_KEY_4096                                          \
00083 "-----BEGIN RSA PRIVATE KEY-----\n"                                   \
00084 "MIIJKgIBAAKCAgEAmkdGjoIshJuOt2NO47qB3Z3yyvmLg2j351isItSNuFQU3qr+\n"  \
00085 "jXHIeANf03yw/K0Zvos8RPd+CqLjoxAQL3QDH4bZAl88bIo29i+SANbNSrKQmc0k\n"  \
00086 "pH+yzw3alDzO0GZaOPZjsbo6AwBrno5msi0vRuC2aY8vGLPsZWSyLai7tneS1j/o\n"  \
00087 "vYW6XIo8Cj61j2Ypy9HhVUW/4Wc+zAT25D/x7jTpkqJLWWT+YzibNbOY48M5eJcB\n"  \
00088 "6/sMyUIeI3/u/wXyMrooNyLiCpedkuHRA0m7u5cWPTUISTunSRlVFij/NHJjuU8e\n"  \
00089 "wA3B29yfZFsUqDEnyc+OxniIueAixTomVszxAaVn8zFEbYhFMPqziiFp99u3jfeG\n"  \
00090 "k1q9mmUi/uCfUC4e2IC5rqq1ZbKSduH7Ug/Vn2bGQahww0sZFRHDXFrnBcotcW+M\n"  \
00091 "bnC290VBDnYgzmdYrIOxuPb2aUwJo4ZlbKh5uBB1PigMuyhLKibQ1a+V5ZJGdpP6\n"  \
00092 "SE9PGIdgYWSmh2QEMuLE6v+wTO2LQ5JgqsvFfi3GIZvkn0s8jTS72Jq2uMkFkMer\n"  \
00093 "UBjPDYaSPy5kpo103KerWs+cMPOJ/3FtZzI++7MoSUTkWVr1ySQFt5i1EIZ/0Thi\n"  \
00094 "jut2jNe8a4AoA3TtC8Rkk/3AIIbg8MVNT4EnT+KHROTMu6gET1oJ3YfBRpUCAwEA\n"  \
00095 "AQKCAgEAhuNSmT7PVZH8kfLOAuYKrY1vvm+4v0iDl048Eqfs0QESziyLK3gUYnnw\n"  \
00096 "yqP2yrU+EQ8Dvvj0xq/sf6GHxTWVlXb9PcmutueRbmXhLcKg83J0Y0StiPXtjIL8\n"  \
00097 "XSddW3Bh6fPi7n14Qy+W6KZwu9AtybanRlvePabyRSRpdOpWVQ7u30w5XZsSed6S\n"  \
00098 "6BI0BBC68m2qqje1sInoqdCdXKtcB31TytUDNEHM+UuAyM8iGeGS2hCNqZlycHTS\n"  \
00099 "jQ9KEsdMH3YLu0lQgRpWtxmg+VL6ROWwmAtKF12EwbDYZ+uoVl69OkQnCpv8pxKa\n"  \
00100 "ec/4m6V+uEA1AOpaAMorHG3fH31IKWC/fTZstovgO/eG2XCtlbcCoWCQ7amFq16l\n"  \
00101 "Gh1UKeBHxMXpDj4oDmIUGUvgzSNnEeSN/v76losWvWYQDjXR/LMDa/CNYsD8BmJR\n"  \
00102 "PZidIjIXdVRlYOhA7ljtySQvp6RBujBfw3tsVMyZw2XzXFwM9O89b1xXC6+M5jf9\n"  \
00103 "DXs/U7Fw+J9qq/YpByABcPCwWdttwdQFRbOxwxaSOKarIqS87TW1JuFcNJ59Ut6G\n"  \
00104 "kMvAg6gC34U+0ktkG/AmI1hgjC+P7ErHCXBR2xARoGzcO/CMZF59S+Z2HFchpTSP\n"  \
00105 "5T2o4mGy3VfHSBidQQrcZRukg8ZP8M1NF3bXjpY6QZpeLHc4oHECggEBAMjdgzzk\n"  \
00106 "xp4mIYFxAEiXYt7tzuUXJk+0UpEJj5uboWLirUZqZmNUPyh6WDnzlREBH++Ms0LO\n"  \
00107 "+AWSfaGPDoMb0NE2j3c4FRWAhe7Vn6lj7nLVpF2RdwRo88yGerZ4uwGMY8NUQCtn\n"  \
00108 "zum3J7eCJ5DojiceRb6uMxTJ8xZmUC4W2f3J/lrR7wlYjyVnnHqH5HcemYUipWSw\n"  \
00109 "sM0/cHp3lrz2VWrbAEu8HVpklvDQpdAgl7cjXt/JHYawY+p426IF/PzQSRROnzgy\n"  \
00110 "4WI8FVYNV2tgu0TOFURbkkEvuj/duDKeooUIF0G0XHzha5oAX/j0iWiHbrOF6wHj\n"  \
00111 "0xeajL9msKBnmD8CggEBAMSgLWmv7G31x4tndJCcXnX4AyVL7KpygAx/ZwCcyTR8\n"  \
00112 "rY1rO07f/ta2noEra/xmEW/BW98qJFCHSU2nSLAQ5FpFSWyuQqrnffrMJnfWyvpr\n"  \
00113 "ceQ0yQ/MiA6/JIOvGAjabcspzZijxzGp+Qk3eTT0yOXLSVOCH9B9XVHLodcy4PQM\n"  \
00114 "KSCxy0vVHhVNl2SdPEwTXRmxk99Q/rw6IHVpQxBq1OhQt05nTKT+rZMD/grSK22e\n"  \
00115 "my2F0DodAJwLo063Zv3RXQZhDYodMmjcp9Hqrtvj9P3HD7J3z6ACiV3SCi8cZumL\n"  \
00116 "bSmnKCcd0bb45+aOWm31ieECJuIcJ9rOREEa/KDYTCsCggEBAMG5WkSVhLWsou37\n"  \
00117 "dUGNuA63nq42SH3gtS0q4nU6gUkkw+dA4ST1cMByVrr1oRQ4WHup4I4TnQOKyF3T\n"  \
00118 "4jQy1I+ipnVeAn+tZ/7zyzwMpEHeqNqRXA9FxbTBEoMAJ6QTqXgOvqDeSqIAQm7r\n"  \
00119 "OYu5rrgtqyh/S8bGCwvUe4ooAfCSKx2ekYMbBVwW9MT8YS09tuS/iHJ3Mt2RTMLg\n"  \
00120 "qeHvVmxrcXqZoFm44Ba7tN/pP0mi9HKyviZT4tmV3IYEbn3JyGGsfkUuVU9wEUfg\n"  \
00121 "MCrgrVxrwfketAzooiHMjkVL2ASjzAJTmEvdAPETYXxzJD9LN0ovY3t8JfAC37IN\n"  \
00122 "sVXS8/MCggEBALByOS59Y4Ktq1rLBQx8djwQyuneP0wZohUVAx7Gk7xZIfklQDyg\n"  \
00123 "v/R4PrcVezstcPpDnykdjScCsGJR+uWc0v667I/ttP/e6utz5hVmmBGu965dPAzE\n"  \
00124 "c1ggaSkOqFfRg/Nr2Qbf+fH0YPnHYSqHe/zSt0OMIvaaeXLcdKhEDSCUBRhE1HWB\n"  \
00125 "kxR046WzgBeYzNQwycz9xwqsctJKGpeR9ute+5ANHPd3X9XtID0fqz8ctI5eZaSw\n"  \
00126 "wApIW01ZQcAF8B+4WkkVuFXnpWW33yCOaRyPVOPHpnclr5WU1fS+3Q85QkW9rkej\n"  \
00127 "97zlkl0QY9AHJqrXnoML1ywAK7ns+MVyNK8CggEAf62xcKZhOb1djeF72Ms+i/i/\n"  \
00128 "WIAq4Q4YpsElgvJTHpNH2v9g4ngSTKe3ws3bGc502sWRlhcoTFMOW2rJNe/iqKkb\n"  \
00129 "3cdeTkseDbpqozmJWz9dJWSVtXas2bZjzBEa//gQ7nHGVeQdqZJQ9rxPsoOAkfpi\n"  \
00130 "qCFrmfUVUqC53e3XMt8+W+aSvKl+JZiB9ozkO9A6Q0vfQLKtjUMdQE3XaCFQT8DI\n"  \
00131 "smaLBlBmeRaBpc02ENeC4ADlWosm1SwgxqMhuh2Alba/GrHOoPlVl4hDs9Fb5a6R\n"  \
00132 "rmpXSt07GAxnG6j9jssA95E4rc1zO0CVKG5bvjVTxwi/sT0/VVX7VsJM4uTAQg==\n"  \
00133 "-----END RSA PRIVATE KEY-----\n"
00134 
00135 #define BUFSIZE         1024
00136 #define HEADER_FORMAT   "  %-24s :  "
00137 #define TITLE_LEN       25
00138 
00139 #if defined(MBEDTLS_ERROR_C)
00140 #define PRINT_ERROR(RET, CODE)                              \
00141     mbedtls_strerror(RET, err_buf, sizeof(err_buf));        \
00142     mbedtls_printf("%s returned -0x%04X\n", CODE, -RET);    \
00143     mbedtls_printf("  !  %s\n", err_buf);
00144 #else
00145 #define PRINT_ERROR(RET, CODE)                              \
00146     mbedtls_printf("%s returned -0x%04X\n", CODE, -RET);
00147 #endif /* MBEDTLS_ERROR_C */
00148 
00149 #define BENCHMARK_FUNC_CALL(TITLE, CODE)                                    \
00150 do {                                                                        \
00151     unsigned long i;                                                        \
00152     Timeout t;                                                              \
00153                                                                             \
00154     mbedtls_printf(HEADER_FORMAT, TITLE);                                   \
00155     fflush(stdout);                                                         \
00156                                                                             \
00157     for (i = 1, alarmed = 0, t.attach(alarm, 1.0); !alarmed; i++) {         \
00158         ret = CODE;                                                         \
00159         if (ret == MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED) {              \
00160             mbedtls_printf("Feature unsupported\n");                        \
00161             break;                                                          \
00162         } else if (ret != 0) {                                              \
00163             PRINT_ERROR(ret, #CODE);                                        \
00164             goto exit;                                                      \
00165         }                                                                   \
00166     }                                                                       \
00167                                                                             \
00168     if (ret == 0) {                                                         \
00169         mbedtls_printf("%9lu KB/s\n", i * BUFSIZE / 1024);                  \
00170     }                                                                       \
00171 } while(0)
00172 
00173 #define BENCHMARK_PUBLIC(TITLE, TYPE, CODE)               \
00174 do {                                                      \
00175     unsigned long ms;                                     \
00176     Timer t;                                              \
00177                                                           \
00178     mbedtls_printf(HEADER_FORMAT, TITLE);                 \
00179     fflush(stdout);                                       \
00180                                                           \
00181     t.start();                                            \
00182     CODE;                                                 \
00183     t.stop();                                             \
00184     ms = t.read_ms();                                     \
00185                                                           \
00186     if (ret == MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED) {\
00187         mbedtls_printf("Feature unsupported\n");          \
00188             break;                                        \
00189     } else if (ret != 0) {                                \
00190         PRINT_ERROR(ret, "Public function");              \
00191         goto exit;                                        \
00192     } else {                                              \
00193         mbedtls_printf("%6lu ms/" TYPE, ms);              \
00194         mbedtls_printf("\n");                             \
00195     }                                                     \
00196 } while(0)
00197 
00198 /* Clear some memory that was used to prepare the context */
00199 #if defined(MBEDTLS_ECP_C)
00200 void ecp_clear_precomputed(mbedtls_ecp_group *grp)
00201 {
00202     if (grp->T != NULL) {
00203         size_t i;
00204         for (i = 0; i < grp->T_size; i++) {
00205             mbedtls_ecp_point_free(&grp->T[i]);
00206         }
00207         mbedtls_free(grp->T);
00208     }
00209     grp->T = NULL;
00210     grp->T_size = 0;
00211 }
00212 #else
00213 #define ecp_clear_precomputed(g)
00214 #endif /* MBEDTLS_ECP_C */
00215 
00216 static unsigned char buf[BUFSIZE];
00217 /*
00218  * Buffer used to hold various data such as IV, signatures, keys, etc. ECDSA
00219  * seems to be the benchmark that uses the most memory from this buffer as it
00220  * is holds the output signature
00221  */
00222 static unsigned char tmp[150];
00223 /* The longest error message has 134 characters (including \0) */
00224 static char err_buf[134];
00225 static char title[TITLE_LEN];
00226 
00227 static volatile int alarmed;
00228 
00229 static void alarm()
00230 {
00231     alarmed = 1;
00232 }
00233 
00234 static int myrand(void *rng_state, unsigned char *output, size_t len)
00235 {
00236     size_t use_len;
00237     int rnd;
00238 
00239     if (rng_state != NULL) {
00240         rng_state  = NULL;
00241     }
00242 
00243     while (len > 0) {
00244         use_len = len;
00245         if (use_len > sizeof(int)) {
00246             use_len = sizeof(int);
00247         }
00248 
00249         rnd = rand();
00250         memcpy(output, &rnd, use_len);
00251         output += use_len;
00252         len -= use_len;
00253     }
00254 
00255     return 0;
00256 }
00257 
00258 #if defined(MBEDTLS_MD4_C)
00259 MBED_NOINLINE static int benchmark_md4()
00260 {
00261     int ret;
00262 
00263     BENCHMARK_FUNC_CALL("MD4", mbedtls_md4_ret(buf, BUFSIZE, tmp));
00264 
00265     ret = 0;
00266 
00267 exit:
00268 
00269     return ret;
00270 }
00271 #endif /* MBEDTLS_MD4_C */
00272 
00273 #if defined(MBEDTLS_MD5_C)
00274 MBED_NOINLINE static int benchmark_md5()
00275 {
00276     int ret;
00277 
00278     BENCHMARK_FUNC_CALL("MD5", mbedtls_md5_ret(buf, BUFSIZE, tmp));
00279 
00280     ret = 0;
00281 
00282 exit:
00283 
00284     return ret;
00285 }
00286 #endif /* MBEDTLS_MD5_C */
00287 
00288 #if defined(MBEDTLS_RIPEMD160_C)
00289 MBED_NOINLINE static int benchmark_ripemd160()
00290 {
00291     int ret;
00292 
00293     BENCHMARK_FUNC_CALL("RIPEMD160", mbedtls_ripemd160_ret(buf, BUFSIZE, tmp));
00294 
00295     ret = 0;
00296 
00297 exit:
00298 
00299     return ret;
00300 }
00301 #endif /* MBEDTLS_RIPEMD160_C */
00302 
00303 #if defined(MBEDTLS_SHA1_C)
00304 MBED_NOINLINE static int benchmark_sha1()
00305 {
00306     int ret;
00307 
00308     BENCHMARK_FUNC_CALL("SHA-1", mbedtls_sha1_ret(buf, BUFSIZE, tmp));
00309 
00310     ret = 0;
00311 
00312 exit:
00313 
00314     return ret;
00315 }
00316 #endif /* MBEDTLS_SHA1_C */
00317 
00318 #if defined(MBEDTLS_SHA256_C)
00319 MBED_NOINLINE static int benchmark_sha256()
00320 {
00321     int ret;
00322 
00323     BENCHMARK_FUNC_CALL("SHA-256", mbedtls_sha256_ret(buf, BUFSIZE, tmp, 0));
00324 
00325     ret = 0;
00326 
00327 exit:
00328 
00329     return ret;
00330 }
00331 #endif /* MBEDTLS_SHA256_C */
00332 
00333 #if defined(MBEDTLS_SHA512_C)
00334 MBED_NOINLINE static int benchmark_sha512()
00335 {
00336     int ret;
00337 
00338     BENCHMARK_FUNC_CALL("SHA-512", mbedtls_sha512_ret(buf, BUFSIZE, tmp, 0));
00339 
00340     ret = 0;
00341 
00342 exit:
00343 
00344     return ret;
00345 }
00346 #endif /* MBEDTLS_SHA512_C */
00347 
00348 #if defined(MBEDTLS_ARC4_C)
00349 MBED_NOINLINE static int benchmark_arc4()
00350 {
00351     int ret = 0;
00352     mbedtls_arc4_context arc4;
00353 
00354     mbedtls_arc4_init(&arc4);
00355 
00356     mbedtls_arc4_setup(&arc4, tmp, 32);
00357     BENCHMARK_FUNC_CALL("ARC4",
00358                         mbedtls_arc4_crypt(&arc4, BUFSIZE, buf, buf));
00359 
00360     ret = 0;
00361 
00362 exit:
00363     mbedtls_arc4_free(&arc4);
00364 
00365     return ret;
00366 }
00367 #endif /* MBEDTLS_ARC4_C */
00368 
00369 #if defined(MBEDTLS_DES_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
00370 MBED_NOINLINE static int benchmark_des3()
00371 {
00372     int ret = 0;
00373     mbedtls_des3_context des3;
00374 
00375     mbedtls_des3_init(&des3);
00376 
00377     if ((ret = mbedtls_des3_set3key_enc(&des3, tmp)) != 0) {
00378         PRINT_ERROR(ret, "mbedtls_des3_set3key_enc()");
00379         goto exit;
00380     }
00381     BENCHMARK_FUNC_CALL("3DES",
00382                         mbedtls_des3_crypt_cbc(&des3, MBEDTLS_DES_ENCRYPT,
00383                                 BUFSIZE, tmp, buf, buf));
00384 
00385     ret = 0;
00386 
00387 exit:
00388     mbedtls_des3_free(&des3);
00389 
00390     return ret;
00391 }
00392 #endif /* MBEDTLS_DES_C && MBEDTLS_CIPHER_MODE_CBC */
00393 
00394 #if defined(MBEDTLS_DES_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
00395 MBED_NOINLINE static int benchmark_des()
00396 {
00397     int ret = 0;
00398     mbedtls_des_context des;
00399 
00400     mbedtls_des_init(&des);
00401 
00402     if ((ret = mbedtls_des_setkey_enc(&des, tmp)) != 0) {
00403         PRINT_ERROR(ret, "mbedtls_des_setkey_enc()");
00404         goto exit;
00405     }
00406     BENCHMARK_FUNC_CALL("DES",
00407                         mbedtls_des_crypt_cbc(&des, MBEDTLS_DES_ENCRYPT,
00408                                 BUFSIZE, tmp, buf, buf));
00409 
00410     ret = 0;
00411 
00412 exit:
00413     mbedtls_des_free(&des);
00414 
00415     return ret;
00416 }
00417 #endif /* MBEDTLS_DES_C && MBEDTLS_CIPHER_MODE_CBC */
00418 
00419 #if defined(MBEDTLS_DES_C) && defined(MBEDTLS_CIPHER_MODE_CBC) && \
00420     defined(MBEDTLS_CMAC_C)
00421 MBED_NOINLINE static int benchmark_des3_cmac()
00422 {
00423     int ret = 0;
00424     unsigned char output[8];
00425     const mbedtls_cipher_info_t *cipher_info;
00426 
00427     memset(buf, 0, sizeof(buf));
00428     memset(tmp, 0, sizeof(tmp));
00429 
00430     cipher_info = mbedtls_cipher_info_from_type(MBEDTLS_CIPHER_DES_EDE3_ECB);
00431     if (cipher_info == NULL) {
00432         mbedtls_printf("mbedtls_cipher_info_from_type() returned NULL\n");
00433         return -1;
00434     }
00435 
00436     BENCHMARK_FUNC_CALL("3DES-CMAC",
00437                         mbedtls_cipher_cmac(cipher_info, tmp, 192, buf,
00438                                             BUFSIZE, output));
00439 
00440     ret = 0;
00441 
00442 exit:
00443     return ret;
00444 }
00445 #endif /* MBEDTLS_DES_C && MBEDTLS_CIPHER_MODE_CBC && MBEDTLS_CMAC_C */
00446 
00447 #if defined(MBEDTLS_AES_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
00448 MBED_NOINLINE static int benchmark_aes_cbc()
00449 {
00450     int ret = 0;
00451     int keysize;
00452     mbedtls_aes_context aes;
00453 
00454     mbedtls_aes_init(&aes);
00455 
00456     for (keysize = 128; keysize <= 256; keysize += 64) {
00457         ret = mbedtls_snprintf(title, sizeof(title), "AES-CBC-%d", keysize);
00458         if (ret < 0 || static_cast<size_t>(ret) >= sizeof(title)) {
00459             mbedtls_printf("Failed to compose title string using "
00460                            "mbedtls_snprintf(): %d\n", ret);
00461             goto exit;
00462         }
00463 
00464         memset(buf, 0, sizeof(buf));
00465         memset(tmp, 0, sizeof(tmp));
00466 
00467         ret = mbedtls_aes_setkey_enc(&aes, tmp, keysize);
00468         if (ret == MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED) {
00469             /* Do not consider this as a failure */
00470             mbedtls_printf(HEADER_FORMAT "Feature unsupported\n", title);
00471             continue;
00472         } else if (ret != 0) {
00473             PRINT_ERROR(ret, "mbedtls_aes_setkey_enc()");
00474             goto exit;
00475         }
00476 
00477         BENCHMARK_FUNC_CALL(title,
00478                             mbedtls_aes_crypt_cbc(&aes,
00479                                     MBEDTLS_AES_ENCRYPT, BUFSIZE,
00480                                     tmp, buf, buf));
00481     }
00482 
00483     ret = 0;
00484 
00485 exit:
00486     mbedtls_aes_free(&aes);
00487 
00488     return ret;
00489 }
00490 #endif /* MBEDTLS_AES_C && MBEDTLS_CIPHER_MODE_CBC */
00491 
00492 #if defined(MBEDTLS_AES_C) && defined(MBEDTLS_CIPHER_MODE_CTR)
00493 MBED_NOINLINE static int benchmark_aes_ctr()
00494 {
00495     int ret = 0;
00496     int keysize;
00497     size_t nc_offset = 0;
00498     unsigned char stream_block[16];
00499     mbedtls_aes_context aes;
00500 
00501     mbedtls_aes_init(&aes);
00502 
00503     for (keysize = 128; keysize <= 256; keysize += 64) {
00504         ret = mbedtls_snprintf(title, sizeof(title), "AES-CTR-%d", keysize);
00505         if (ret < 0 || static_cast<size_t>(ret) >= sizeof(title)) {
00506             mbedtls_printf("Failed to compose title string using "
00507                            "mbedtls_snprintf(): %d\n", ret);
00508             goto exit;
00509         }
00510 
00511         memset(buf, 0, sizeof(buf));
00512         memset(tmp, 0, sizeof(tmp));
00513 
00514         ret = mbedtls_aes_setkey_enc(&aes, tmp, keysize);
00515         if (ret == MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED) {
00516             /* Do not consider this as a failure */
00517             mbedtls_printf(HEADER_FORMAT "Feature unsupported\n", title);
00518             continue;
00519         } else if (ret != 0) {
00520             PRINT_ERROR(ret, "mbedtls_aes_setkey_enc()");
00521             goto exit;
00522         }
00523 
00524         BENCHMARK_FUNC_CALL(title,
00525                             mbedtls_aes_crypt_ctr(&aes, BUFSIZE, &nc_offset,
00526                                     tmp, stream_block, buf,
00527                                     buf));
00528     }
00529 
00530     ret = 0;
00531 
00532 exit:
00533     mbedtls_aes_free(&aes);
00534 
00535     return ret;
00536 }
00537 #endif /* MBEDTLS_AES_C && MBEDTLS_CIPHER_MODE_CTR */
00538 
00539 #if defined(MBEDTLS_AES_C) && defined(MBEDTLS_GCM_C)
00540 MBED_NOINLINE static int benchmark_aes_gcm()
00541 {
00542     int ret = 0;
00543     int keysize;
00544     mbedtls_gcm_context gcm;
00545 
00546     mbedtls_gcm_init(&gcm);
00547 
00548     for (keysize = 128; keysize <= 256; keysize += 64) {
00549         ret = mbedtls_snprintf(title, sizeof(title), "AES-GCM-%d", keysize);
00550         if (ret < 0 || static_cast<size_t>(ret) >= sizeof(title)) {
00551             mbedtls_printf("Failed to compose title string using "
00552                            "mbedtls_snprintf(): %d\n", ret);
00553             goto exit;
00554         }
00555 
00556         memset(buf, 0, sizeof(buf));
00557         memset(tmp, 0, sizeof(tmp));
00558 
00559         ret = mbedtls_gcm_setkey(&gcm, MBEDTLS_CIPHER_ID_AES, tmp, keysize);
00560         if (ret == MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED) {
00561             /* Do not consider this as a failure */
00562             mbedtls_printf(HEADER_FORMAT "Feature unsupported\n", title);
00563             continue;
00564         } else if (ret != 0) {
00565             PRINT_ERROR(ret, "mbedtls_gcm_setkey()");
00566             goto exit;
00567         }
00568 
00569         BENCHMARK_FUNC_CALL(title,
00570                             mbedtls_gcm_crypt_and_tag(&gcm,
00571                                     MBEDTLS_GCM_ENCRYPT,
00572                                     BUFSIZE, tmp, 12, NULL,
00573                                     0, buf, buf, 16, tmp));
00574     }
00575 
00576     ret = 0;
00577 
00578 exit:
00579     mbedtls_gcm_free(&gcm);
00580 
00581     return ret;
00582 }
00583 #endif /* MBEDTLS_AES_C && MBEDTLS_GCM_C */
00584 
00585 #if defined(MBEDTLS_AES_C) && defined(MBEDTLS_CCM_C)
00586 MBED_NOINLINE static int benchmark_aes_ccm()
00587 {
00588     int ret = 0;
00589     int keysize;
00590     mbedtls_ccm_context ccm;
00591 
00592     mbedtls_ccm_init(&ccm);
00593 
00594     for (keysize = 128; keysize <= 256; keysize += 64) {
00595         ret = mbedtls_snprintf(title, sizeof(title), "AES-CCM-%d", keysize);
00596         if (ret < 0 || static_cast<size_t>(ret) >= sizeof(title)) {
00597             mbedtls_printf("Failed to compose title string using "
00598                            "mbedtls_snprintf(): %d\n", ret);
00599             goto exit;
00600         }
00601 
00602         memset(buf, 0, sizeof(buf));
00603         memset(tmp, 0, sizeof(tmp));
00604 
00605         ret = mbedtls_ccm_setkey(&ccm, MBEDTLS_CIPHER_ID_AES, tmp, keysize);
00606         if (ret == MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED) {
00607             /* Do not consider this as a failure */
00608             mbedtls_printf(HEADER_FORMAT "Feature unsupported\n", title);
00609             continue;
00610         } else if (ret != 0) {
00611             PRINT_ERROR(ret, "mbedtls_gcm_setkey()");
00612             goto exit;
00613         }
00614 
00615         BENCHMARK_FUNC_CALL(title,
00616                             mbedtls_ccm_encrypt_and_tag(&ccm, BUFSIZE, tmp, 12,
00617                                     NULL, 0, buf, buf, tmp,
00618                                     16));
00619     }
00620 
00621     ret = 0;
00622 
00623 exit:
00624     mbedtls_ccm_free(&ccm);
00625 
00626     return ret;
00627 }
00628 #endif /* MBEDTLS_AES_C && MBEDTLS_CCM_C */
00629 
00630 #if defined(MBEDTLS_AES_C) && defined(MBEDTLS_CMAC_C)
00631 MBED_NOINLINE static int benchmark_aes_cmac()
00632 {
00633     int ret = 0;
00634     unsigned char output[16];
00635     const mbedtls_cipher_info_t *cipher_info;
00636     mbedtls_cipher_type_t cipher_type;
00637     int keysize;
00638 
00639     cipher_type = MBEDTLS_CIPHER_AES_128_ECB;
00640     for (keysize = 128; keysize <= 256; keysize += 64) {
00641         ret = mbedtls_snprintf(title, sizeof(title), "AES-CMAC-%d", keysize);
00642         if (ret < 0 || static_cast<size_t>(ret) >= sizeof(title)) {
00643             mbedtls_printf("Failed to compose title string using "
00644                            "mbedtls_snprintf(): %d\n", ret);
00645             goto exit;
00646         }
00647 
00648         memset(buf, 0, sizeof(buf));
00649         memset(tmp, 0, sizeof(tmp));
00650 
00651         cipher_info = mbedtls_cipher_info_from_type(cipher_type);
00652         if (cipher_info == NULL) {
00653             mbedtls_printf("mbedtls_cipher_info_from_type() returned NULL\n");
00654             goto exit;
00655         }
00656 
00657         BENCHMARK_FUNC_CALL(title,
00658                             mbedtls_cipher_cmac(cipher_info, tmp, keysize,
00659                                                 buf, BUFSIZE, output));
00660         cipher_type = (mbedtls_cipher_type_t)(cipher_type + 1);
00661     }
00662 
00663     memset(buf, 0, sizeof(buf));
00664     memset(tmp, 0, sizeof(tmp));
00665 
00666     BENCHMARK_FUNC_CALL("AES-CMAC-PRF-128",
00667                         mbedtls_aes_cmac_prf_128(tmp, 16, buf, BUFSIZE,
00668                                 output));
00669 
00670     ret = 0;
00671 
00672 exit:
00673 
00674     return ret;
00675 }
00676 #endif /* MBEDTLS_AES_C && MBEDTLS_CMAC_C */
00677 
00678 #if defined(MBEDTLS_CAMELLIA_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
00679 MBED_NOINLINE static int benchmark_camellia()
00680 {
00681     int ret = 0;
00682     int keysize;
00683     mbedtls_camellia_context camellia;
00684 
00685     mbedtls_camellia_init(&camellia);
00686 
00687     for (keysize = 128; keysize <= 256; keysize += 64) {
00688         ret = mbedtls_snprintf(title, sizeof(title), "CAMELLIA-CBC-%d",
00689                                keysize);
00690         if (ret < 0 || static_cast<size_t>(ret) >= sizeof(title)) {
00691             mbedtls_printf("Failed to compose title string using "
00692                            "mbedtls_snprintf(): %d\n", ret);
00693             goto exit;
00694         }
00695 
00696         memset(buf, 0, sizeof(buf));
00697         memset(tmp, 0, sizeof(tmp));
00698 
00699         ret = mbedtls_camellia_setkey_enc(&camellia, tmp, keysize);
00700         if (ret == MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED) {
00701             /* Do not consider this as a failure */
00702             mbedtls_printf(HEADER_FORMAT "Feature unsupported\n", title);
00703             ret = 0;
00704             continue;
00705         } else if (ret != 0) {
00706             PRINT_ERROR(ret, "mbedtls_camellia_setkey_enc()");
00707             goto exit;
00708         }
00709 
00710         BENCHMARK_FUNC_CALL(title,
00711                             mbedtls_camellia_crypt_cbc(&camellia,
00712                                     MBEDTLS_CAMELLIA_ENCRYPT,
00713                                     BUFSIZE, tmp, buf, buf));
00714     }
00715 
00716     ret = 0;
00717 
00718 exit:
00719     mbedtls_camellia_free(&camellia);
00720 
00721     return ret;
00722 }
00723 #endif /* MBEDTLS_CAMELLIA_C && MBEDTLS_CIPHER_MODE_CBC */
00724 
00725 #if defined(MBEDTLS_BLOWFISH_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
00726 MBED_NOINLINE static int benchmark_blowfish()
00727 {
00728     int ret = 0;
00729     int keysize;
00730     mbedtls_blowfish_context *blowfish;
00731 
00732     blowfish = (mbedtls_blowfish_context *)mbedtls_calloc(1,
00733                sizeof(mbedtls_blowfish_context));
00734     if (blowfish == NULL) {
00735         mbedtls_printf("Failed to allocate mbedtls_blowfish_context\n");
00736         return -1;
00737     }
00738 
00739     mbedtls_blowfish_init(blowfish);
00740 
00741     for (keysize = 128; keysize <= 256; keysize += 64) {
00742         mbedtls_snprintf(title, sizeof(title), "BLOWFISH-CBC-%d", keysize);
00743         if (ret < 0 || static_cast<size_t>(ret) >= sizeof(title)) {
00744             mbedtls_printf("Failed to compose title string using "
00745                            "mbedtls_snprintf(): %d\n", ret);
00746             goto exit;
00747         }
00748 
00749         memset(buf, 0, sizeof(buf));
00750         memset(tmp, 0, sizeof(tmp));
00751 
00752         ret = mbedtls_blowfish_setkey(blowfish, tmp, keysize);
00753         if (ret == MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED) {
00754             /* Do not consider this as a failure */
00755             mbedtls_printf(HEADER_FORMAT "Feature unsupported\n", title);
00756             ret = 0;
00757             continue;
00758         } else if (ret != 0) {
00759             PRINT_ERROR(ret, "mbedtls_blowfish_setkey()");
00760             goto exit;
00761         }
00762 
00763         BENCHMARK_FUNC_CALL(title,
00764                             mbedtls_blowfish_crypt_cbc(blowfish,
00765                                     MBEDTLS_BLOWFISH_ENCRYPT,
00766                                     BUFSIZE,
00767                                     tmp, buf, buf));
00768     }
00769 
00770     ret = 0;
00771 
00772 exit:
00773     mbedtls_blowfish_free(blowfish);
00774     mbedtls_free(blowfish);
00775 
00776     return ret;
00777 }
00778 #endif /* MBEDTLS_BLOWFISH_C && MBEDTLS_CIPHER_MODE_CBC */
00779 
00780 #if defined(MBEDTLS_HAVEGE_C)
00781 MBED_NOINLINE static int benchmark_havege()
00782 {
00783     int ret = 0;
00784     mbedtls_havege_state hs;
00785 
00786     mbedtls_havege_init(&hs);
00787 
00788     BENCHMARK_FUNC_CALL("HAVEGE", mbedtls_havege_random(&hs, buf, BUFSIZE));
00789 
00790     ret = 0;
00791 
00792 exit:
00793     mbedtls_havege_free(&hs);
00794 
00795     return ret;
00796 }
00797 #endif /* MBEDTLS_HAVEGE_C */
00798 
00799 #if defined(MBEDTLS_CTR_DRBG_C)
00800 MBED_NOINLINE static int benchmark_ctr_drbg()
00801 {
00802     int ret = 0;
00803     const char *nopr_title = "CTR_DRBG (NOPR)";
00804     const char *pr_title = "CTR_DRBG (PR)";
00805     mbedtls_ctr_drbg_context ctr_drbg;
00806 
00807     mbedtls_ctr_drbg_init(&ctr_drbg);
00808     ret = mbedtls_ctr_drbg_seed(&ctr_drbg, myrand, NULL, NULL, 0);
00809     if (ret != 0) {
00810         PRINT_ERROR(ret, "mbedtls_ctr_drbg_seed()");
00811         goto exit;
00812     }
00813     BENCHMARK_FUNC_CALL(nopr_title,
00814                         mbedtls_ctr_drbg_random(&ctr_drbg, buf, BUFSIZE));
00815     mbedtls_ctr_drbg_free(&ctr_drbg);
00816 
00817     mbedtls_ctr_drbg_init(&ctr_drbg);
00818     ret = mbedtls_ctr_drbg_seed(&ctr_drbg, myrand, NULL, NULL, 0);
00819     if (ret != 0) {
00820         PRINT_ERROR(ret, "mbedtls_ctr_drbg_seed()");
00821         goto exit;
00822     }
00823     mbedtls_ctr_drbg_set_prediction_resistance(&ctr_drbg,
00824             MBEDTLS_CTR_DRBG_PR_ON);
00825     BENCHMARK_FUNC_CALL(pr_title,
00826                         mbedtls_ctr_drbg_random(&ctr_drbg, buf, BUFSIZE));
00827 
00828     ret = 0;
00829 
00830 exit:
00831     mbedtls_ctr_drbg_free(&ctr_drbg);
00832 
00833     return ret;
00834 }
00835 #endif /* MBEDTLS_CTR_DRBG_C */
00836 
00837 #if defined(MBEDTLS_HMAC_DRBG_C)
00838 MBED_NOINLINE static int benchmark_hmac_drbg()
00839 {
00840     int ret = 0;
00841     mbedtls_hmac_drbg_context hmac_drbg;
00842     const mbedtls_md_info_t *md_info;
00843 
00844     mbedtls_hmac_drbg_init(&hmac_drbg);
00845 
00846 #if defined(MBEDTLS_SHA1_C)
00847     md_info = mbedtls_md_info_from_type(MBEDTLS_MD_SHA1);
00848     if (md_info == NULL) {
00849         mbedtls_printf("mbedtls_md_info_from_type() returned NULL\n");
00850         ret = -1;
00851         goto exit;
00852     }
00853 
00854     ret = mbedtls_hmac_drbg_seed(&hmac_drbg, md_info, myrand, NULL, NULL, 0);
00855     if (ret != 0) {
00856         PRINT_ERROR(ret, "mbedtls_hmac_drbg_seed()");
00857         goto exit;
00858     }
00859     BENCHMARK_FUNC_CALL("HMAC_DRBG SHA-1 (NOPR)",
00860                         mbedtls_hmac_drbg_random(&hmac_drbg, buf, BUFSIZE));
00861 
00862     ret = mbedtls_hmac_drbg_seed(&hmac_drbg, md_info, myrand, NULL, NULL, 0);
00863     if (ret != 0) {
00864         PRINT_ERROR(ret, "mbedtls_hmac_drbg_seed()");
00865         goto exit;
00866     }
00867     mbedtls_hmac_drbg_set_prediction_resistance(&hmac_drbg,
00868             MBEDTLS_HMAC_DRBG_PR_ON);
00869     BENCHMARK_FUNC_CALL("HMAC_DRBG SHA-1 (PR)",
00870                         mbedtls_hmac_drbg_random(&hmac_drbg, buf, BUFSIZE));
00871 #endif /* MBEDTLS_SHA1_C */
00872 
00873 #if defined(MBEDTLS_SHA256_C)
00874     md_info = mbedtls_md_info_from_type(MBEDTLS_MD_SHA256);
00875     if (md_info == NULL) {
00876         PRINT_ERROR(ret, "mbedtls_md_info_from_type()");
00877         goto exit;
00878     }
00879 
00880     ret = mbedtls_hmac_drbg_seed(&hmac_drbg, md_info, myrand, NULL, NULL, 0);
00881     if (ret != 0) {
00882         PRINT_ERROR(ret, "mbedtls_hmac_drbg_seed()");
00883         goto exit;
00884     }
00885     BENCHMARK_FUNC_CALL("HMAC_DRBG SHA-256 (NOPR)",
00886                         mbedtls_hmac_drbg_random(&hmac_drbg, buf, BUFSIZE));
00887 
00888     ret = mbedtls_hmac_drbg_seed(&hmac_drbg, md_info, myrand, NULL, NULL, 0);
00889     if (ret != 0) {
00890         PRINT_ERROR(ret, "mbedtls_hmac_drbg_seed()");
00891         goto exit;
00892     }
00893     mbedtls_hmac_drbg_set_prediction_resistance(&hmac_drbg,
00894             MBEDTLS_HMAC_DRBG_PR_ON);
00895     BENCHMARK_FUNC_CALL("HMAC_DRBG SHA-256 (PR)",
00896                         mbedtls_hmac_drbg_random(&hmac_drbg, buf, BUFSIZE));
00897 #endif /* MBEDTLS_SHA256_C */
00898 
00899     ret = 0;
00900 
00901 exit:
00902     mbedtls_hmac_drbg_free(&hmac_drbg);
00903 
00904     return ret;
00905 }
00906 #endif /* MBEDTLS_HMAC_DRBG_C */
00907 
00908 #if defined(MBEDTLS_RSA_C) && \
00909     defined(MBEDTLS_PEM_PARSE_C) && defined(MBEDTLS_PK_PARSE_C)
00910 MBED_NOINLINE static int benchmark_rsa()
00911 {
00912     int ret = 0;
00913     mbedtls_pk_context pk;
00914     mbedtls_rsa_context *rsa;
00915     const char *rsa_keys[] = {
00916         RSA_PRIVATE_KEY_2048,
00917     };
00918     size_t i;
00919 
00920     for (i = 0; i < sizeof(rsa_keys) / sizeof(rsa_keys[0]) && ret == 0; i++) {
00921         mbedtls_pk_init(&pk);
00922 
00923         ret = mbedtls_pk_parse_key(&pk, (const unsigned char *)rsa_keys[i],
00924                                    strlen(rsa_keys[i]) + 1, NULL, 0);
00925         if (ret == MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED) {
00926             /* Do not consider this as a failure */
00927             mbedtls_printf(HEADER_FORMAT "Feature unsupported\n", title);
00928             ret = 0;
00929             continue;
00930         } else if (ret != 0) {
00931             PRINT_ERROR(ret, "mbedtls_pk_parse_key()");
00932             goto exit;
00933         }
00934 
00935         rsa = mbedtls_pk_rsa(pk);
00936 
00937         ret = mbedtls_snprintf(title, sizeof(title), "RSA-%d",
00938                                mbedtls_pk_get_bitlen(&pk));
00939         if (ret < 0 || static_cast<size_t>(ret) >= sizeof(title)) {
00940             mbedtls_printf("Failed to compose title string using "
00941                            "mbedtls_snprintf(): %d\n", ret);
00942             goto exit;
00943         }
00944 
00945         BENCHMARK_PUBLIC(title, " public",
00946                          buf[0] = 0;
00947                          ret = mbedtls_rsa_public(rsa, buf, buf));
00948 
00949         BENCHMARK_PUBLIC(title, "private",
00950                          buf[0] = 0;
00951                          ret = mbedtls_rsa_private(rsa, myrand, NULL, buf,
00952                                  buf));
00953 
00954 exit:
00955         mbedtls_pk_free(&pk);
00956     }
00957 
00958     return ret;
00959 }
00960 #endif /* MBEDTLS_RSA_C && MBEDTLS_PEM_PARSE_C && MBEDTLS_PK_PARSE_C */
00961 
00962 #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_BIGNUM_C)
00963 MBED_NOINLINE static int benchmark_dhm()
00964 {
00965     int ret = 0;
00966     int dhm_sizes[] = {
00967         2048,
00968         3072,
00969     };
00970     const char *dhm_P[] = {
00971         MBEDTLS_DHM_RFC3526_MODP_2048_P,
00972         MBEDTLS_DHM_RFC3526_MODP_3072_P,
00973     };
00974     const char *dhm_G[] = {
00975         MBEDTLS_DHM_RFC3526_MODP_2048_G,
00976         MBEDTLS_DHM_RFC3526_MODP_3072_G,
00977     };
00978 
00979     mbedtls_dhm_context dhm;
00980     size_t olen;
00981     size_t i;
00982 
00983     for (i = 0;
00984             i < sizeof(dhm_sizes) / sizeof(dhm_sizes[0]) && ret == 0;
00985             i++) {
00986         mbedtls_dhm_init(&dhm);
00987 
00988         ret = mbedtls_mpi_read_string(&dhm.P, 16, dhm_P[i]);
00989         if (ret != 0) {
00990             PRINT_ERROR(ret, "mbedtls_mpi_read_string()");
00991             goto exit;
00992         }
00993         ret = mbedtls_mpi_read_string(&dhm.G, 16, dhm_G[i]);
00994         if (ret != 0) {
00995             PRINT_ERROR(ret, "mbedtls_mpi_read_string()");
00996             goto exit;
00997         }
00998 
00999         dhm.len = mbedtls_mpi_size(&dhm.P);
01000         ret = mbedtls_dhm_make_public(&dhm, (int) dhm.len, buf, dhm.len,
01001                                       myrand, NULL);
01002         if (ret == MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED) {
01003             /* Do not consider this as a failure */
01004             mbedtls_printf(HEADER_FORMAT "Feature unsupported\n", title);
01005             ret = 0;
01006             continue;
01007         } else if (ret != 0) {
01008             PRINT_ERROR(ret, "mbedtls_dhm_make_public()");
01009             goto exit;
01010         }
01011 
01012         ret = mbedtls_mpi_copy(&dhm.GY, &dhm.GX);
01013         if (ret != 0) {
01014             PRINT_ERROR(ret, "mbedtls_mpi_copy()");
01015             goto exit;
01016         }
01017 
01018         ret = mbedtls_snprintf(title, sizeof(title), "DHE-%d", dhm_sizes[i]);
01019         if (ret < 0 || static_cast<size_t>(ret) >= sizeof(title)) {
01020             mbedtls_printf("Failed to compose title string using "
01021                            "mbedtls_snprintf(): %d\n", ret);
01022             goto exit;
01023         }
01024 
01025         /*
01026          * Benchmarking this requires two function calls that can fail. We
01027          * add a check in between them to check for any errors. In normal
01028          * operation, the overhead of this check is negligible
01029          */
01030         BENCHMARK_PUBLIC(title, "handshake",
01031                          ret = mbedtls_dhm_make_public(&dhm, (int)dhm.len,
01032                                  buf, dhm.len, myrand,
01033                                  NULL);
01034                          if (ret != 0) {
01035                              PRINT_ERROR(ret, "mbedtls_dhm_make_public()");
01036                              goto exit;
01037                          }
01038                          ret = mbedtls_dhm_calc_secret(&dhm, buf, sizeof(buf),
01039                                  &olen, myrand, NULL));
01040 
01041         ret = mbedtls_snprintf(title, sizeof(title), "DH-%d", dhm_sizes[i]);
01042         if (ret < 0 || static_cast<size_t>(ret) >= sizeof(title)) {
01043             mbedtls_printf("Failed to compose title string using "
01044                            "mbedtls_snprintf(): %d\n", ret);
01045             goto exit;
01046         }
01047 
01048         BENCHMARK_PUBLIC(title, "handshake",
01049                          ret = mbedtls_dhm_calc_secret(&dhm, buf, sizeof(buf),
01050                                  &olen, myrand, NULL));
01051 
01052 exit:
01053         mbedtls_dhm_free(&dhm);
01054     }
01055 
01056     return ret;
01057 }
01058 #endif /* MBEDTLS_DHM_C && MBEDTLS_BIGNUM_C */
01059 
01060 #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA256_C)
01061 MBED_NOINLINE static int benchmark_ecdsa()
01062 {
01063     int ret = 0;
01064     mbedtls_ecdsa_context ecdsa;
01065     const mbedtls_ecp_curve_info *curve_info;
01066     size_t sig_len;
01067     size_t hash_len;
01068 
01069     memset(buf, 0x2A, sizeof(buf));
01070 
01071     for (curve_info = mbedtls_ecp_curve_list();
01072             curve_info->grp_id != MBEDTLS_ECP_DP_NONE && ret == 0;
01073             curve_info++) {
01074         mbedtls_ecdsa_init(&ecdsa);
01075 
01076         ret = mbedtls_snprintf(title, sizeof(title), "ECDSA-%s",
01077                                curve_info->name);
01078         if (ret < 0 || static_cast<size_t>(ret) >= sizeof(title)) {
01079             mbedtls_printf("Failed to compose title string using "
01080                            "mbedtls_snprintf(): %d\n", ret);
01081             goto exit;
01082         }
01083 
01084         ret = mbedtls_ecdsa_genkey(&ecdsa, curve_info->grp_id, myrand, NULL);
01085         if (ret == MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED) {
01086             /* Do not consider this as a failure */
01087             mbedtls_printf(HEADER_FORMAT "Feature unsupported\n", title);
01088             ret = 0;
01089             continue;
01090         } else if (ret != 0) {
01091             PRINT_ERROR(ret, "mbedtls_ecdsa_genkey()");
01092             goto exit;
01093         }
01094 
01095         ecp_clear_precomputed(&ecdsa.grp);
01096 
01097         hash_len = (curve_info->bit_size + 7) / 8;
01098         BENCHMARK_PUBLIC(title, "sign",
01099                          ret = mbedtls_ecdsa_write_signature(&ecdsa,
01100                                  MBEDTLS_MD_SHA256,
01101                                  buf, hash_len,
01102                                  tmp, &sig_len,
01103                                  myrand, NULL));
01104 
01105         mbedtls_ecdsa_free(&ecdsa);
01106     }
01107 
01108     for (curve_info = mbedtls_ecp_curve_list();
01109             curve_info->grp_id != MBEDTLS_ECP_DP_NONE && ret == 0;
01110             curve_info++) {
01111         mbedtls_ecdsa_init(&ecdsa);
01112 
01113         ret = mbedtls_ecdsa_genkey(&ecdsa, curve_info->grp_id, myrand, NULL);
01114         if (ret == MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED) {
01115             /* Do not consider this as a failure */
01116             mbedtls_printf(HEADER_FORMAT "Feature unsupported\n", title);
01117             ret = 0;
01118             continue;
01119         } else if (ret != 0) {
01120             PRINT_ERROR(ret, "mbedtls_ecdsa_genkey()");
01121             goto exit;
01122         }
01123 
01124         hash_len = (curve_info->bit_size + 7) / 8;
01125         ret = mbedtls_ecdsa_write_signature(&ecdsa, MBEDTLS_MD_SHA256, buf,
01126                                             hash_len, tmp, &sig_len, myrand,
01127                                             NULL);
01128         if (ret == MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED) {
01129             /* Do not consider this as a failure */
01130             mbedtls_printf(HEADER_FORMAT "Feature unsupported\n", title);
01131             ret = 0;
01132             continue;
01133         } else if (ret != 0) {
01134             PRINT_ERROR(ret, "mbedtls_ecdsa_write_signature()");
01135             goto exit;
01136         }
01137 
01138         ecp_clear_precomputed(&ecdsa.grp);
01139 
01140         ret = mbedtls_snprintf(title, sizeof(title), "ECDSA-%s",
01141                                curve_info->name);
01142         if (ret < 0 || static_cast<size_t>(ret) >= sizeof(title)) {
01143             mbedtls_printf("Failed to compose title string using "
01144                            "mbedtls_snprintf(): %d\n", ret);
01145             goto exit;
01146         }
01147 
01148         BENCHMARK_PUBLIC(title, "verify",
01149                          ret = mbedtls_ecdsa_read_signature(&ecdsa, buf,
01150                                  hash_len, tmp,
01151                                  sig_len));
01152 
01153 exit:
01154         mbedtls_ecdsa_free(&ecdsa);
01155     }
01156 
01157     return ret;
01158 }
01159 #endif /* MBEDTLS_ECDSA_C && MBEDTLS_SHA2565_C */
01160 
01161 #if defined(MBEDTLS_ECDH_C)
01162 MBED_NOINLINE static int benchmark_ecdh()
01163 {
01164     int ret = 0;
01165     mbedtls_ecdh_context ecdh;
01166     const mbedtls_ecp_curve_info *curve_info;
01167     size_t olen;
01168 
01169     for (curve_info = mbedtls_ecp_curve_list();
01170             curve_info->grp_id != MBEDTLS_ECP_DP_NONE && ret == 0;
01171             curve_info++) {
01172         mbedtls_ecdh_init(&ecdh);
01173 
01174         ret = mbedtls_ecp_group_load(&ecdh.grp, curve_info->grp_id);
01175         if (ret == MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED) {
01176             /* Do not consider this as a failure */
01177             mbedtls_printf(HEADER_FORMAT "Feature unsupported\n", title);
01178             ret = 0;
01179             continue;
01180         } else if (ret != 0) {
01181             PRINT_ERROR(ret, "mbedtls_ecp_group_load()");
01182             goto exit;
01183         }
01184 
01185         ret = mbedtls_snprintf(title, sizeof(title), "ECDHE-%s",
01186                                curve_info->name);
01187         if (ret < 0 || static_cast<size_t>(ret) >= sizeof(title)) {
01188             mbedtls_printf("Failed to compose title string using "
01189                            "mbedtls_snprintf(): %d\n", ret);
01190             goto exit;
01191         }
01192 
01193         ret = mbedtls_ecdh_make_public(&ecdh, &olen, buf, sizeof(buf),
01194                                        myrand, NULL);
01195         if (ret == MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED) {
01196             /* Do not consider this as a failure */
01197             mbedtls_printf(HEADER_FORMAT "Feature unsupported\n", title);
01198             ret = 0;
01199             continue;
01200         } else if (ret != 0) {
01201             PRINT_ERROR(ret, "mbedtls_ecdh_make_public()");
01202             goto exit;
01203         }
01204 
01205         ret = mbedtls_ecp_copy(&ecdh.Qp, &ecdh.Q);
01206         if (ret == MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED) {
01207             /* Do not consider this as a failure */
01208             mbedtls_printf(HEADER_FORMAT "Feature unsupported\n", title);
01209             ret = 0;
01210             continue;
01211         } else if (ret != 0) {
01212             PRINT_ERROR(ret, "mbedtls_ecp_copy()");
01213             goto exit;
01214         }
01215 
01216         ecp_clear_precomputed(&ecdh.grp);
01217 
01218         /*
01219          * Benchmarking this requires two function calls that can fail. We
01220          * add a check in between them to check for any errors. In normal
01221          * operation, the overhead of this check is negligible
01222          */
01223         BENCHMARK_PUBLIC(title, "handshake",
01224                          ret = mbedtls_ecdh_make_public(&ecdh, &olen, buf,
01225                                  sizeof(buf), myrand,
01226                                  NULL);
01227                          if (ret != 0) {
01228                              PRINT_ERROR(ret, "mbedtls_ecdh_make_public()");
01229                              goto exit;
01230                          }
01231                          ret = mbedtls_ecdh_calc_secret(&ecdh, &olen, buf,
01232                                  sizeof(buf), myrand,
01233                                  NULL));
01234         mbedtls_ecdh_free(&ecdh);
01235     }
01236 
01237     for (curve_info = mbedtls_ecp_curve_list();
01238             curve_info->grp_id != MBEDTLS_ECP_DP_NONE && ret == 0;
01239             curve_info++) {
01240         mbedtls_ecdh_init(&ecdh);
01241 
01242         ret = mbedtls_ecp_group_load(&ecdh.grp, curve_info->grp_id);
01243         if (ret == MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED) {
01244             /* Do not consider this as a failure */
01245             mbedtls_printf(HEADER_FORMAT "Feature unsupported\n", title);
01246             ret = 0;
01247             continue;
01248         } else if (ret != 0) {
01249             PRINT_ERROR(ret, "mbedtls_ecp_group_load()");
01250             goto exit;
01251         }
01252 
01253         ret = mbedtls_snprintf(title, sizeof(title), "ECDH-%s",
01254                                        curve_info->name);
01255         if (ret < 0 || static_cast<size_t>(ret) >= sizeof(title)) {
01256             mbedtls_printf("Failed to compose title string using "
01257                            "mbedtls_snprintf(): %d\n", ret);
01258             goto exit;
01259         }
01260 
01261         ret = mbedtls_ecdh_make_public(&ecdh, &olen, buf, sizeof(buf), myrand,
01262                                        NULL);
01263         if (ret == MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED) {
01264             /* Do not consider this as a failure */
01265             mbedtls_printf(HEADER_FORMAT "Feature unsupported\n", title);
01266             ret = 0;
01267             continue;
01268         } else if (ret != 0) {
01269             PRINT_ERROR(ret, "mbedtls_ecdh_make_public()");
01270             goto exit;
01271         }
01272 
01273         ret = mbedtls_ecp_copy(&ecdh.Qp, &ecdh.Q);
01274         if (ret == MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED) {
01275             /* Do not consider this as a failure */
01276             mbedtls_printf(HEADER_FORMAT "Feature unsupported\n", title);
01277             ret = 0;
01278             continue;
01279         } else if (ret != 0) {
01280             PRINT_ERROR(ret, "mbedtls_ecp_copy()");
01281             goto exit;
01282         }
01283 
01284         ret = mbedtls_ecdh_make_public(&ecdh, &olen, buf, sizeof(buf), myrand,
01285                                        NULL);
01286         if (ret == MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED) {
01287             /* Do not consider this as a failure */
01288             mbedtls_printf(HEADER_FORMAT "Feature unsupported\n", title);
01289             ret = 0;
01290             continue;
01291         } else if (ret != 0) {
01292             PRINT_ERROR(ret, "mbedtls_ecdh_make_public()");
01293             goto exit;
01294         }
01295 
01296         ecp_clear_precomputed(&ecdh.grp);
01297 
01298         BENCHMARK_PUBLIC(title, "handshake",
01299                          ret = mbedtls_ecdh_calc_secret(&ecdh, &olen, buf,
01300                                  sizeof(buf), myrand,
01301                                  NULL));
01302 
01303 exit:
01304         mbedtls_ecdh_free(&ecdh);
01305     }
01306 
01307     return ret;
01308 }
01309 #endif /* MBEDTLS_ECDH_C */
01310 
01311 #if defined(MBEDTLS_ECDH_C) && defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
01312 /* Curve25519 needs to be handled separately */
01313 MBED_NOINLINE static int benchmark_ecdh_curve22519()
01314 {
01315     int ret = 0;
01316     mbedtls_ecdh_context ecdh;
01317     mbedtls_mpi z;
01318 
01319     mbedtls_ecdh_init(&ecdh);
01320     mbedtls_mpi_init(&z);
01321 
01322     ret = mbedtls_snprintf(title, sizeof(title), "ECDHE-Curve25519");
01323     if (ret < 0 || static_cast<size_t>(ret) >= sizeof(title)) {
01324         mbedtls_printf("Failed to compose title string using "
01325                         "mbedtls_snprintf(): %d\n", ret);
01326          goto exit;
01327      }
01328 
01329     ret = mbedtls_ecp_group_load(&ecdh.grp, MBEDTLS_ECP_DP_CURVE25519);
01330     if (ret == MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED) {
01331         /* Do not consider this as a failure */
01332         mbedtls_printf(HEADER_FORMAT "Feature unsupported\n", title);
01333         ret = 0;
01334         goto exit;
01335     } else if (ret != 0) {
01336         PRINT_ERROR(ret, "mbedtls_ecp_group_load()");
01337         goto exit;
01338     }
01339 
01340     ret = mbedtls_ecdh_gen_public(&ecdh.grp, &ecdh.d, &ecdh.Qp, myrand,
01341                                   NULL);
01342     if (ret == MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED) {
01343         /* Do not consider this as a failure */
01344         mbedtls_printf(HEADER_FORMAT "Feature unsupported\n", title);
01345         ret = 0;
01346         goto exit;
01347     } else if (ret != 0) {
01348         PRINT_ERROR(ret, "mbedtls_ecdh_gen_public()");
01349         goto exit;
01350     }
01351 
01352     /*
01353      * Benchmarking this requires two function calls that can fail. We
01354      * add a check in between them to check for any errors. In normal
01355      * operation, the overhead of this check is negligible
01356      */
01357     BENCHMARK_PUBLIC(title, "handshake",
01358                      ret = mbedtls_ecdh_gen_public(&ecdh.grp, &ecdh.d,
01359                              &ecdh.Q, myrand, NULL);
01360                      if (ret != 0) {
01361                          PRINT_ERROR(ret, "mbedtls_ecdh_make_public()");
01362                          goto exit;
01363                      }
01364                      ret = mbedtls_ecdh_compute_shared(&ecdh.grp, &z,
01365                              &ecdh.Qp, &ecdh.d,
01366                              myrand, NULL));
01367 
01368     mbedtls_ecdh_free(&ecdh);
01369     mbedtls_mpi_free(&z);
01370 
01371     mbedtls_ecdh_init(&ecdh);
01372     mbedtls_mpi_init(&z);
01373 
01374     ret = mbedtls_snprintf(title, sizeof(title), "ECDH-Curve25519");
01375          if (ret < 0 || static_cast<size_t>(ret) >= sizeof(title)) {
01376              mbedtls_printf("Failed to compose title string using "
01377                             "mbedtls_snprintf(): %d\n", ret);
01378              goto exit;
01379          }
01380     ret = mbedtls_ecp_group_load(&ecdh.grp, MBEDTLS_ECP_DP_CURVE25519);
01381     if (ret == MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED) {
01382         /* Do not consider this as a failure */
01383         mbedtls_printf(HEADER_FORMAT "Feature unsupported\n", title);
01384         ret = 0;
01385         goto exit;
01386     } else if (ret != 0) {
01387         PRINT_ERROR(ret, "mbedtls_ecp_group_load()");
01388         goto exit;
01389     }
01390 
01391     ret = mbedtls_ecdh_gen_public(&ecdh.grp, &ecdh.d, &ecdh.Qp, myrand, NULL);
01392     if (ret == MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED) {
01393         /* Do not consider this as a failure */
01394         mbedtls_printf(HEADER_FORMAT "Feature unsupported\n", title);
01395         ret = 0;
01396         goto exit;
01397     } else if (ret != 0) {
01398         PRINT_ERROR(ret, "mbedtls_ecdh_gen_public()");
01399         goto exit;
01400     }
01401 
01402     ret = mbedtls_ecdh_gen_public(&ecdh.grp, &ecdh.d, &ecdh.Q, myrand, NULL);
01403     if (ret == MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED) {
01404         /* Do not consider this as a failure */
01405         mbedtls_printf(HEADER_FORMAT "Feature unsupported\n", title);
01406         ret = 0;
01407         goto exit;
01408     } else if (ret != 0) {
01409         PRINT_ERROR(ret, "mbedtls_ecdh_gen_public()");
01410         goto exit;
01411     }
01412 
01413     BENCHMARK_PUBLIC(title, "handshake",
01414                      ret = mbedtls_ecdh_compute_shared(&ecdh.grp, &z,
01415                              &ecdh.Qp, &ecdh.d,
01416                              myrand, NULL));
01417 
01418 exit:
01419     mbedtls_ecdh_free(&ecdh);
01420     mbedtls_mpi_free(&z);
01421 
01422     return ret;
01423 }
01424 #endif /* MBEDTLS_ECDH_C && MBEDTLS_ECP_DP_CURVE25519_ENABLED */
01425 
01426 int main()
01427 {
01428     int exit_code = MBEDTLS_EXIT_SUCCESS;
01429 
01430     memset(buf, 0xAA, sizeof(buf));
01431     memset(tmp, 0xBB, sizeof(tmp));
01432 
01433     if ((exit_code = mbedtls_platform_setup(NULL)) != 0) {
01434         mbedtls_printf("Platform initialization failed with error %d\r\n",
01435                        exit_code);
01436         return MBEDTLS_EXIT_FAILURE;
01437     }
01438 
01439 #if defined(MBEDTLS_MD4_C)
01440     if (benchmark_md4() != 0) {
01441         exit_code = MBEDTLS_EXIT_FAILURE;
01442     }
01443 #endif /* MBEDTLS_MD4_C */
01444 
01445 #if defined(MBEDTLS_MD5_C)
01446     if (benchmark_md5() != 0) {
01447         exit_code = MBEDTLS_EXIT_FAILURE;
01448     }
01449 #endif /* MBEDTLS_MD5_C */
01450 
01451 #if defined(MBEDTLS_RIPEMD160_C)
01452     if (benchmark_ripemd160() != 0) {
01453         exit_code = MBEDTLS_EXIT_FAILURE;
01454     }
01455 #endif /* MBEDTLS_RIPEMD160_C */
01456 
01457 #if defined(MBEDTLS_SHA1_C)
01458     if (benchmark_sha1() != 0) {
01459         exit_code = MBEDTLS_EXIT_FAILURE;
01460     }
01461 #endif /* MBEDTLS_SHA1_C */
01462 
01463 #if defined(MBEDTLS_SHA256_C)
01464     if (benchmark_sha256() != 0) {
01465         exit_code = MBEDTLS_EXIT_FAILURE;
01466     }
01467 #endif /* MBEDTLS_SHA256_C */
01468 
01469 #if defined(MBEDTLS_SHA256_C)
01470     if (benchmark_sha512() != 0) {
01471         exit_code = MBEDTLS_EXIT_FAILURE;
01472     }
01473 #endif /* MBEDTLS_SHA512_C */
01474 
01475 #if defined(MBEDTLS_ARC4_C)
01476     if (benchmark_arc4() != 0) {
01477         exit_code = MBEDTLS_EXIT_FAILURE;
01478     }
01479 #endif /* MBEDTLS_ARC4_C */
01480 
01481 #if defined(MBEDTLS_DES_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
01482     if (benchmark_des3() != 0) {
01483         exit_code = MBEDTLS_EXIT_FAILURE;
01484     }
01485 #endif /* MBEDTLS_DES_C && MBEDTLS_CIPHER_MODE_CBC */
01486 
01487 #if defined(MBEDTLS_DES_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
01488     if (benchmark_des() != 0) {
01489         exit_code = MBEDTLS_EXIT_FAILURE;
01490     }
01491 #endif /* MBEDTLS_DES_C && MBEDTLS_CIPHER_MODE_CBC */
01492 
01493 #if defined(MBEDTLS_DES_C) && defined(MBEDTLS_CIPHER_MODE_CBC) && \
01494     defined(MBEDTLS_CMAC_C)
01495     if (benchmark_des3_cmac() != 0) {
01496         exit_code = MBEDTLS_EXIT_FAILURE;
01497     }
01498 #endif /* MBEDTLS_DES_C && MBEDTLS_CIPHER_MODE_CBC && MBEDTLS_CMAC_C */
01499 
01500 #if defined(MBEDTLS_AES_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
01501     if (benchmark_aes_cbc() != 0) {
01502         exit_code = MBEDTLS_EXIT_FAILURE;
01503     }
01504 #endif /* MBEDTLS_AES_C && MBEDTLS_CIPHER_MODE_CBC */
01505 
01506 #if defined(MBEDTLS_AES_C) && defined(MBEDTLS_CIPHER_MODE_CTR)
01507     if (benchmark_aes_ctr() != 0) {
01508         exit_code = MBEDTLS_EXIT_FAILURE;
01509     }
01510 #endif /* MBEDTLS_AES_C && MBEDTLS_CIPHER_MODE_CTR */
01511 
01512 #if defined(MBEDTLS_AES_C) && defined(MBEDTLS_GCM_C)
01513     if (benchmark_aes_gcm() != 0) {
01514         exit_code = MBEDTLS_EXIT_FAILURE;
01515     }
01516 #endif /* MBEDTLS_AES_C && MBEDTLS_GCM_C */
01517 
01518 #if defined(MBEDTLS_AES_C) && defined(MBEDTLS_CCM_C)
01519     if (benchmark_aes_ccm() != 0) {
01520         exit_code = MBEDTLS_EXIT_FAILURE;
01521     }
01522 #endif /* MBEDTLS_AES_C && MBEDTLS_CCM_C */
01523 
01524 #if defined(MBEDTLS_AES_C) && defined(MBEDTLS_CMAC_C)
01525     if (benchmark_aes_cmac() != 0) {
01526         exit_code = MBEDTLS_EXIT_FAILURE;
01527     }
01528 #endif /* MBEDTLS_AES_C && MBEDTLS_CMAC_C */
01529 
01530 #if defined(MBEDTLS_CAMELLIA_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
01531     if (benchmark_camellia() != 0) {
01532         exit_code = MBEDTLS_EXIT_FAILURE;
01533     }
01534 #endif /* MBEDTLS_CAMELLIA_C && MBEDTLS_CIPHER_MODE_CBC */
01535 
01536 #if defined(MBEDTLS_BLOWFISH_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
01537     if (benchmark_blowfish() != 0) {
01538         exit_code = MBEDTLS_EXIT_FAILURE;
01539     }
01540 #endif /* MBEDTLS_BLOWFISH_C && MBEDTLS_CIPHER_MODE_CBC */
01541 
01542 #if defined(MBEDTLS_HAVEGE_C)
01543     if (benchmark_havege() != 0) {
01544         exit_code = MBEDTLS_EXIT_FAILURE;
01545     }
01546 #endif /* MBEDTLS_HAVEGE_C */
01547 
01548 #if defined(MBEDTLS_CTR_DRBG_C)
01549     if (benchmark_ctr_drbg() != 0) {
01550         exit_code = MBEDTLS_EXIT_FAILURE;
01551     }
01552 #endif /* MBEDTLS_CTR_DRBG_C */
01553 
01554 #if defined(MBEDTLS_HMAC_DRBG_C)
01555     if (benchmark_hmac_drbg() != 0) {
01556         exit_code = MBEDTLS_EXIT_FAILURE;
01557     }
01558 #endif /* MBEDTLS_HMAC_DRBG_C */
01559 
01560 #if defined(MBEDTLS_RSA_C) && \
01561     defined(MBEDTLS_PEM_PARSE_C) && defined(MBEDTLS_PK_PARSE_C)
01562     if (benchmark_rsa() != 0) {
01563         exit_code = MBEDTLS_EXIT_FAILURE;
01564     }
01565 #endif /* MBEDTLS_RSA_C && MBEDTLS_PEM_PARSE_C && MBEDTLS_PK_PARSE_C */
01566 
01567 #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_BIGNUM_C)
01568     if (benchmark_dhm() != 0) {
01569         exit_code = MBEDTLS_EXIT_FAILURE;
01570     }
01571 #endif /* MBEDTLS_DHM_C && MBEDTLS_BIGNUM_C */
01572 
01573 #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA256_C)
01574     if (benchmark_ecdsa() != 0) {
01575         exit_code = MBEDTLS_EXIT_FAILURE;
01576     }
01577 #endif /* MBEDTLS_ECDSA_C && MBEDTLS_SHA2565_C */
01578 
01579 #if defined(MBEDTLS_ECDH_C)
01580     if (benchmark_ecdh() != 0) {
01581         exit_code = MBEDTLS_EXIT_FAILURE;
01582     }
01583 
01584 #if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
01585     if (benchmark_ecdh_curve22519() != 0) {
01586         exit_code = MBEDTLS_EXIT_FAILURE;
01587     }
01588 #endif /* MBEDTLS_ECP_DP_CURVE25519_ENABLED */
01589 #endif /* MBEDTLS_ECDH_C */
01590 
01591     mbedtls_printf("DONE\n");
01592 
01593     mbedtls_platform_teardown(NULL);
01594     return exit_code;
01595 }