Benchmark demonstration program for mbed TLS. The canonical source for this example lives at https://github.com/ARMmbed/mbed-os-example-tls

Mbed TLS Benchmark example on Mbed OS

This application benchmarks the various cryptographic primitives offered by Mbed TLS.

Getting started

Building with Mbed CLI

If you'd like to use Mbed CLI to build this, then you should set up your environment if you have not done so already. For instructions, refer to the main readme. The instructions on this page relate to using the developer.mbed.org Online Compiler

Import the program in to the Online Compiler, select your board from the drop down in the top right hand corner and then compile the application. Once it has built, you can drag and drop the binary onto your device.

Monitoring the application

The output in the terminal window should be similar to this:

terminal output

  SHA-256                  :       1673 Kb/s,         70 cycles/byte
  SHA-512                  :        546 Kb/s,        215 cycles/byte
  AES-CBC-128              :       1428 Kb/s,         82 cycles/byte
  AES-CBC-192              :       1260 Kb/s,         93 cycles/byte
  AES-CBC-256              :       1127 Kb/s,        104 cycles/byte
  AES-GCM-128              :        486 Kb/s,        242 cycles/byte
  AES-GCM-192              :        464 Kb/s,        253 cycles/byte
  AES-GCM-256              :        445 Kb/s,        264 cycles/byte
  AES-CCM-128              :        610 Kb/s,        192 cycles/byte
  AES-CCM-192              :        547 Kb/s,        214 cycles/byte
  AES-CCM-256              :        496 Kb/s,        237 cycles/byte
  CTR_DRBG (NOPR)          :       1139 Kb/s,        102 cycles/byte
  CTR_DRBG (PR)            :        826 Kb/s,        142 cycles/byte
  HMAC_DRBG SHA-256 (NOPR) :        193 Kb/s,        611 cycles/byte
  HMAC_DRBG SHA-256 (PR)   :        170 Kb/s,        695 cycles/byte
  RSA-2048                 :      28 ms/ public
  RSA-2048                 :     953 ms/private
  RSA-4096                 :      93 ms/ public
  RSA-4096                 :    5327 ms/private
  ECDSA-secp384r1          :     451 ms/sign
  ECDSA-secp256r1          :     304 ms/sign
  ECDSA-secp384r1          :     863 ms/verify
  ECDSA-secp256r1          :     594 ms/verify
  ECDHE-secp384r1          :     829 ms/handshake
  ECDHE-secp256r1          :     566 ms/handshake
  ECDHE-Curve25519         :     533 ms/handshake
  ECDH-secp384r1           :     407 ms/handshake
  ECDH-secp256r1           :     281 ms/handshake
  ECDH-Curve25519          :     268 ms/handshake

DONE
Committer:
mbed_official
Date:
Tue Jul 23 12:01:17 2019 +0100
Revision:
89:2d5255b9d6c5
Parent:
83:c40c51a46813
Child:
90:8aec6c3f0695
Merge pull request #211 from RonEld/unsupported_feature_error_support

Add support for the Feature unsupported error
.
Commit copied from https://github.com/ARMmbed/mbed-os-example-tls

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Janos Follath 0:1f7c5025e59d 1 /*
Janos Follath 0:1f7c5025e59d 2 * Benchmark demonstration program
Janos Follath 0:1f7c5025e59d 3 *
mbed_official 70:72c865037f5d 4 * Copyright (C) 2006-2018, Arm Limited, All Rights Reserved
Janos Follath 0:1f7c5025e59d 5 * SPDX-License-Identifier: Apache-2.0
Janos Follath 0:1f7c5025e59d 6 *
Janos Follath 0:1f7c5025e59d 7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
Janos Follath 0:1f7c5025e59d 8 * not use this file except in compliance with the License.
Janos Follath 0:1f7c5025e59d 9 * You may obtain a copy of the License at
Janos Follath 0:1f7c5025e59d 10 *
Janos Follath 0:1f7c5025e59d 11 * http://www.apache.org/licenses/LICENSE-2.0
Janos Follath 0:1f7c5025e59d 12 *
Janos Follath 0:1f7c5025e59d 13 * Unless required by applicable law or agreed to in writing, software
Janos Follath 0:1f7c5025e59d 14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
Janos Follath 0:1f7c5025e59d 15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Janos Follath 0:1f7c5025e59d 16 * See the License for the specific language governing permissions and
Janos Follath 0:1f7c5025e59d 17 * limitations under the License.
Janos Follath 0:1f7c5025e59d 18 */
Janos Follath 0:1f7c5025e59d 19
Janos Follath 0:1f7c5025e59d 20 #include "mbed.h"
Janos Follath 0:1f7c5025e59d 21
Janos Follath 0:1f7c5025e59d 22 #if !defined(MBEDTLS_CONFIG_FILE)
Janos Follath 0:1f7c5025e59d 23 #include "mbedtls/config.h"
Janos Follath 0:1f7c5025e59d 24 #else
Janos Follath 0:1f7c5025e59d 25 #include MBEDTLS_CONFIG_FILE
mbed_official 78:5f419e0f96dd 26 #endif /* MBEDTLS_CONFIG_FILE */
Janos Follath 0:1f7c5025e59d 27
Janos Follath 0:1f7c5025e59d 28 #include "mbedtls/platform.h"
Janos Follath 0:1f7c5025e59d 29 #include "mbedtls/md4.h"
Janos Follath 0:1f7c5025e59d 30 #include "mbedtls/md5.h"
Janos Follath 0:1f7c5025e59d 31 #include "mbedtls/ripemd160.h"
Janos Follath 0:1f7c5025e59d 32 #include "mbedtls/sha1.h"
Janos Follath 0:1f7c5025e59d 33 #include "mbedtls/sha256.h"
Janos Follath 0:1f7c5025e59d 34 #include "mbedtls/sha512.h"
Janos Follath 0:1f7c5025e59d 35 #include "mbedtls/arc4.h"
Janos Follath 0:1f7c5025e59d 36 #include "mbedtls/des.h"
Janos Follath 0:1f7c5025e59d 37 #include "mbedtls/aes.h"
mbed_official 11:6ccae3ebafd5 38 #include "mbedtls/cmac.h"
Janos Follath 0:1f7c5025e59d 39 #include "mbedtls/blowfish.h"
Janos Follath 0:1f7c5025e59d 40 #include "mbedtls/camellia.h"
Janos Follath 0:1f7c5025e59d 41 #include "mbedtls/gcm.h"
Janos Follath 0:1f7c5025e59d 42 #include "mbedtls/ccm.h"
Janos Follath 0:1f7c5025e59d 43 #include "mbedtls/havege.h"
Janos Follath 0:1f7c5025e59d 44 #include "mbedtls/ctr_drbg.h"
Janos Follath 0:1f7c5025e59d 45 #include "mbedtls/hmac_drbg.h"
Janos Follath 0:1f7c5025e59d 46 #include "mbedtls/rsa.h"
Janos Follath 0:1f7c5025e59d 47 #include "mbedtls/pk.h"
Janos Follath 0:1f7c5025e59d 48 #include "mbedtls/dhm.h"
Janos Follath 0:1f7c5025e59d 49 #include "mbedtls/ecdsa.h"
Janos Follath 0:1f7c5025e59d 50 #include "mbedtls/ecdh.h"
Janos Follath 0:1f7c5025e59d 51 #include "mbedtls/error.h"
Janos Follath 0:1f7c5025e59d 52
mbed_official 76:68ac2a548d58 53 #define RSA_PRIVATE_KEY_2048 \
mbed_official 76:68ac2a548d58 54 "-----BEGIN RSA PRIVATE KEY-----\n" \
mbed_official 76:68ac2a548d58 55 "MIIEogIBAAKCAQEA2dwVr+IMGEtA2/MCP6fA5eb/6B18Bq6e7gw8brNPkm3E6LyR\n" \
mbed_official 76:68ac2a548d58 56 "4DnMJVxZmw3bPDKBDoKzfntkMESi/Yw5UopLtVfjGfWeQWPClqffLZBsZ60BRAsg\n" \
mbed_official 76:68ac2a548d58 57 "/g+ID5tgzxSuxzftypK59uexOVCAm7hCKZHGO3DbI7bLY27j7VAgEP7d/yuaz5Fx\n" \
mbed_official 76:68ac2a548d58 58 "Kl/vu7shqrBoz6ABJVJD3KC8nUiMRUCXRINmxbyUUjA4DnicZv6+xrGKr36r6M8h\n" \
mbed_official 76:68ac2a548d58 59 "VYLa5msKc8WzbnBWzpUsrpb4/r7ML+qp92gdSfVJ8/bLiU7h2C7faDA59uaqrFK9\n" \
mbed_official 76:68ac2a548d58 60 "xmDdx7FaWhGQs3LWW6w1UNgkPS0FDYUslpsnsQIDAQABAoIBAC7IJNwM5V3+IuJY\n" \
mbed_official 76:68ac2a548d58 61 "T35Nzo1PyloUosJokvY5KGz5Ejg2XBdCDu0gXCcVqqQyGIbXrYDpLhQV+RCoXHun\n" \
mbed_official 76:68ac2a548d58 62 "tdN0oQdC5SB47s/J1Uo2qCUHo0+sBd6PqTkFKsl3KxWssk9TQjvCwC412IefMs69\n" \
mbed_official 76:68ac2a548d58 63 "hW+ZvwCanmQP56LleApIr2oW4KLfW8Ry/QfZlua+dizctdN7+H1mWwgZQTY9T27J\n" \
mbed_official 76:68ac2a548d58 64 "6RtGRA5NVkKVPzIHVJfdpKoO7xGg1g06aEbPB/VmGvZaaFWWnaf7uRvFjLZecBLu\n" \
mbed_official 76:68ac2a548d58 65 "QSx2DA/GDjirlDYj99PJb7DtB4xRtKzsyw0o+xapC8w6OtIl/3xFt9moCu2jGrsx\n" \
mbed_official 76:68ac2a548d58 66 "vpjHdfECgYEA7fSACRseIs9gAIVX8wq6gayTpA47DHYWAD6IQfIj35SJ+AgsvbFF\n" \
mbed_official 76:68ac2a548d58 67 "4AmrwDhcJVPmDy1N4nLBfyGAMt/2CfiYkdkW6QFX/ULRMMBL/G7kWV8hYQDICB2g\n" \
mbed_official 76:68ac2a548d58 68 "xaMRN1lPCmFq6BkSWjwIYTnYDFBDWVm1GVT8TMtJoM8Erej9qC0PeFUCgYEA6mF3\n" \
mbed_official 76:68ac2a548d58 69 "bigO3t8f5sig+XepaftEUbkJMzo72TVRnIR2ycdR2ihelPQ+25g9dwV0ZA5XXhBS\n" \
mbed_official 76:68ac2a548d58 70 "DKOABWjMM739Mwmy9v26Dlmu9R01zHQktMvtEAyfz7lk2NF0aMuj8285OJUBf9bz\n" \
mbed_official 76:68ac2a548d58 71 "Cq3MjtMCD+4CZ6iaEqCdUKOuxfpx5cWVJV+qve0CgYBhD1YaYMFOGaBjFgDl1f51\n" \
mbed_official 76:68ac2a548d58 72 "Xltqk5NqZdBbkSYrIAWZ8RDF5y+4wFJsLAWuhk6vuyUgE66tK3nZzWRpXAkT0B8L\n" \
mbed_official 76:68ac2a548d58 73 "fq1lpXKqj1KcvBNCiEkEW1VWJ+dvyAYIF5eyJ++hoFLnETL3M32HivyhKSwPihPg\n" \
mbed_official 76:68ac2a548d58 74 "nVW8TT9fJJIYDe1JZ/fjcQKBgHJfv7UsrR0LSvkG3K8AOtbx+8PZhOjPuRbk0v+L\n" \
mbed_official 76:68ac2a548d58 75 "EKCkuIe5/XW4vtfQMeZb7hFJgk7vrepm+vkoy8VQKDf4urGW3W1VTHBmobM01hi4\n" \
mbed_official 76:68ac2a548d58 76 "DuYvEul+Mf0wMRtWjJolo4m+BO5KiW2jpFfqFm6JmfjVqOIAKOSKC6am8V/MDF0h\n" \
mbed_official 76:68ac2a548d58 77 "kyN9AoGAT9oOiEXMolbkDZw/QCaBiRoAGlGlNYUkJ+58U6OjIZLISw6aFv+Y2uE0\n" \
mbed_official 76:68ac2a548d58 78 "mEImItjuYZtSYKblWikp6ldPoKlt9bwEFe3c6IZ8kJ3+xyEyAGrvjXjEY7PzP6dp\n" \
mbed_official 76:68ac2a548d58 79 "Ajbjp9X9uocEBv9W/KsBLdQ7yizcL/toHwdBO4vQqmqTvAc5IIw=\n" \
mbed_official 76:68ac2a548d58 80 "-----END RSA PRIVATE KEY-----\n"
Janos Follath 0:1f7c5025e59d 81
mbed_official 76:68ac2a548d58 82 #define RSA_PRIVATE_KEY_4096 \
mbed_official 76:68ac2a548d58 83 "-----BEGIN RSA PRIVATE KEY-----\n" \
mbed_official 76:68ac2a548d58 84 "MIIJKgIBAAKCAgEAmkdGjoIshJuOt2NO47qB3Z3yyvmLg2j351isItSNuFQU3qr+\n" \
mbed_official 76:68ac2a548d58 85 "jXHIeANf03yw/K0Zvos8RPd+CqLjoxAQL3QDH4bZAl88bIo29i+SANbNSrKQmc0k\n" \
mbed_official 76:68ac2a548d58 86 "pH+yzw3alDzO0GZaOPZjsbo6AwBrno5msi0vRuC2aY8vGLPsZWSyLai7tneS1j/o\n" \
mbed_official 76:68ac2a548d58 87 "vYW6XIo8Cj61j2Ypy9HhVUW/4Wc+zAT25D/x7jTpkqJLWWT+YzibNbOY48M5eJcB\n" \
mbed_official 76:68ac2a548d58 88 "6/sMyUIeI3/u/wXyMrooNyLiCpedkuHRA0m7u5cWPTUISTunSRlVFij/NHJjuU8e\n" \
mbed_official 76:68ac2a548d58 89 "wA3B29yfZFsUqDEnyc+OxniIueAixTomVszxAaVn8zFEbYhFMPqziiFp99u3jfeG\n" \
mbed_official 76:68ac2a548d58 90 "k1q9mmUi/uCfUC4e2IC5rqq1ZbKSduH7Ug/Vn2bGQahww0sZFRHDXFrnBcotcW+M\n" \
mbed_official 76:68ac2a548d58 91 "bnC290VBDnYgzmdYrIOxuPb2aUwJo4ZlbKh5uBB1PigMuyhLKibQ1a+V5ZJGdpP6\n" \
mbed_official 76:68ac2a548d58 92 "SE9PGIdgYWSmh2QEMuLE6v+wTO2LQ5JgqsvFfi3GIZvkn0s8jTS72Jq2uMkFkMer\n" \
mbed_official 76:68ac2a548d58 93 "UBjPDYaSPy5kpo103KerWs+cMPOJ/3FtZzI++7MoSUTkWVr1ySQFt5i1EIZ/0Thi\n" \
mbed_official 76:68ac2a548d58 94 "jut2jNe8a4AoA3TtC8Rkk/3AIIbg8MVNT4EnT+KHROTMu6gET1oJ3YfBRpUCAwEA\n" \
mbed_official 76:68ac2a548d58 95 "AQKCAgEAhuNSmT7PVZH8kfLOAuYKrY1vvm+4v0iDl048Eqfs0QESziyLK3gUYnnw\n" \
mbed_official 76:68ac2a548d58 96 "yqP2yrU+EQ8Dvvj0xq/sf6GHxTWVlXb9PcmutueRbmXhLcKg83J0Y0StiPXtjIL8\n" \
mbed_official 76:68ac2a548d58 97 "XSddW3Bh6fPi7n14Qy+W6KZwu9AtybanRlvePabyRSRpdOpWVQ7u30w5XZsSed6S\n" \
mbed_official 76:68ac2a548d58 98 "6BI0BBC68m2qqje1sInoqdCdXKtcB31TytUDNEHM+UuAyM8iGeGS2hCNqZlycHTS\n" \
mbed_official 76:68ac2a548d58 99 "jQ9KEsdMH3YLu0lQgRpWtxmg+VL6ROWwmAtKF12EwbDYZ+uoVl69OkQnCpv8pxKa\n" \
mbed_official 76:68ac2a548d58 100 "ec/4m6V+uEA1AOpaAMorHG3fH31IKWC/fTZstovgO/eG2XCtlbcCoWCQ7amFq16l\n" \
mbed_official 76:68ac2a548d58 101 "Gh1UKeBHxMXpDj4oDmIUGUvgzSNnEeSN/v76losWvWYQDjXR/LMDa/CNYsD8BmJR\n" \
mbed_official 76:68ac2a548d58 102 "PZidIjIXdVRlYOhA7ljtySQvp6RBujBfw3tsVMyZw2XzXFwM9O89b1xXC6+M5jf9\n" \
mbed_official 76:68ac2a548d58 103 "DXs/U7Fw+J9qq/YpByABcPCwWdttwdQFRbOxwxaSOKarIqS87TW1JuFcNJ59Ut6G\n" \
mbed_official 76:68ac2a548d58 104 "kMvAg6gC34U+0ktkG/AmI1hgjC+P7ErHCXBR2xARoGzcO/CMZF59S+Z2HFchpTSP\n" \
mbed_official 76:68ac2a548d58 105 "5T2o4mGy3VfHSBidQQrcZRukg8ZP8M1NF3bXjpY6QZpeLHc4oHECggEBAMjdgzzk\n" \
mbed_official 76:68ac2a548d58 106 "xp4mIYFxAEiXYt7tzuUXJk+0UpEJj5uboWLirUZqZmNUPyh6WDnzlREBH++Ms0LO\n" \
mbed_official 76:68ac2a548d58 107 "+AWSfaGPDoMb0NE2j3c4FRWAhe7Vn6lj7nLVpF2RdwRo88yGerZ4uwGMY8NUQCtn\n" \
mbed_official 76:68ac2a548d58 108 "zum3J7eCJ5DojiceRb6uMxTJ8xZmUC4W2f3J/lrR7wlYjyVnnHqH5HcemYUipWSw\n" \
mbed_official 76:68ac2a548d58 109 "sM0/cHp3lrz2VWrbAEu8HVpklvDQpdAgl7cjXt/JHYawY+p426IF/PzQSRROnzgy\n" \
mbed_official 76:68ac2a548d58 110 "4WI8FVYNV2tgu0TOFURbkkEvuj/duDKeooUIF0G0XHzha5oAX/j0iWiHbrOF6wHj\n" \
mbed_official 76:68ac2a548d58 111 "0xeajL9msKBnmD8CggEBAMSgLWmv7G31x4tndJCcXnX4AyVL7KpygAx/ZwCcyTR8\n" \
mbed_official 76:68ac2a548d58 112 "rY1rO07f/ta2noEra/xmEW/BW98qJFCHSU2nSLAQ5FpFSWyuQqrnffrMJnfWyvpr\n" \
mbed_official 76:68ac2a548d58 113 "ceQ0yQ/MiA6/JIOvGAjabcspzZijxzGp+Qk3eTT0yOXLSVOCH9B9XVHLodcy4PQM\n" \
mbed_official 76:68ac2a548d58 114 "KSCxy0vVHhVNl2SdPEwTXRmxk99Q/rw6IHVpQxBq1OhQt05nTKT+rZMD/grSK22e\n" \
mbed_official 76:68ac2a548d58 115 "my2F0DodAJwLo063Zv3RXQZhDYodMmjcp9Hqrtvj9P3HD7J3z6ACiV3SCi8cZumL\n" \
mbed_official 76:68ac2a548d58 116 "bSmnKCcd0bb45+aOWm31ieECJuIcJ9rOREEa/KDYTCsCggEBAMG5WkSVhLWsou37\n" \
mbed_official 76:68ac2a548d58 117 "dUGNuA63nq42SH3gtS0q4nU6gUkkw+dA4ST1cMByVrr1oRQ4WHup4I4TnQOKyF3T\n" \
mbed_official 76:68ac2a548d58 118 "4jQy1I+ipnVeAn+tZ/7zyzwMpEHeqNqRXA9FxbTBEoMAJ6QTqXgOvqDeSqIAQm7r\n" \
mbed_official 76:68ac2a548d58 119 "OYu5rrgtqyh/S8bGCwvUe4ooAfCSKx2ekYMbBVwW9MT8YS09tuS/iHJ3Mt2RTMLg\n" \
mbed_official 76:68ac2a548d58 120 "qeHvVmxrcXqZoFm44Ba7tN/pP0mi9HKyviZT4tmV3IYEbn3JyGGsfkUuVU9wEUfg\n" \
mbed_official 76:68ac2a548d58 121 "MCrgrVxrwfketAzooiHMjkVL2ASjzAJTmEvdAPETYXxzJD9LN0ovY3t8JfAC37IN\n" \
mbed_official 76:68ac2a548d58 122 "sVXS8/MCggEBALByOS59Y4Ktq1rLBQx8djwQyuneP0wZohUVAx7Gk7xZIfklQDyg\n" \
mbed_official 76:68ac2a548d58 123 "v/R4PrcVezstcPpDnykdjScCsGJR+uWc0v667I/ttP/e6utz5hVmmBGu965dPAzE\n" \
mbed_official 76:68ac2a548d58 124 "c1ggaSkOqFfRg/Nr2Qbf+fH0YPnHYSqHe/zSt0OMIvaaeXLcdKhEDSCUBRhE1HWB\n" \
mbed_official 76:68ac2a548d58 125 "kxR046WzgBeYzNQwycz9xwqsctJKGpeR9ute+5ANHPd3X9XtID0fqz8ctI5eZaSw\n" \
mbed_official 76:68ac2a548d58 126 "wApIW01ZQcAF8B+4WkkVuFXnpWW33yCOaRyPVOPHpnclr5WU1fS+3Q85QkW9rkej\n" \
mbed_official 76:68ac2a548d58 127 "97zlkl0QY9AHJqrXnoML1ywAK7ns+MVyNK8CggEAf62xcKZhOb1djeF72Ms+i/i/\n" \
mbed_official 76:68ac2a548d58 128 "WIAq4Q4YpsElgvJTHpNH2v9g4ngSTKe3ws3bGc502sWRlhcoTFMOW2rJNe/iqKkb\n" \
mbed_official 76:68ac2a548d58 129 "3cdeTkseDbpqozmJWz9dJWSVtXas2bZjzBEa//gQ7nHGVeQdqZJQ9rxPsoOAkfpi\n" \
mbed_official 76:68ac2a548d58 130 "qCFrmfUVUqC53e3XMt8+W+aSvKl+JZiB9ozkO9A6Q0vfQLKtjUMdQE3XaCFQT8DI\n" \
mbed_official 76:68ac2a548d58 131 "smaLBlBmeRaBpc02ENeC4ADlWosm1SwgxqMhuh2Alba/GrHOoPlVl4hDs9Fb5a6R\n" \
mbed_official 76:68ac2a548d58 132 "rmpXSt07GAxnG6j9jssA95E4rc1zO0CVKG5bvjVTxwi/sT0/VVX7VsJM4uTAQg==\n" \
mbed_official 76:68ac2a548d58 133 "-----END RSA PRIVATE KEY-----\n"
Janos Follath 0:1f7c5025e59d 134
Janos Follath 0:1f7c5025e59d 135 #define BUFSIZE 1024
Janos Follath 0:1f7c5025e59d 136 #define HEADER_FORMAT " %-24s : "
Janos Follath 0:1f7c5025e59d 137 #define TITLE_LEN 25
Janos Follath 0:1f7c5025e59d 138
Janos Follath 0:1f7c5025e59d 139 #if defined(MBEDTLS_ERROR_C)
mbed_official 78:5f419e0f96dd 140 #define PRINT_ERROR(RET, CODE) \
mbed_official 78:5f419e0f96dd 141 mbedtls_strerror(RET, err_buf, sizeof(err_buf)); \
mbed_official 78:5f419e0f96dd 142 mbedtls_printf("%s returned -0x%04X\n", CODE, -RET); \
mbed_official 78:5f419e0f96dd 143 mbedtls_printf(" ! %s\n", err_buf);
Janos Follath 0:1f7c5025e59d 144 #else
mbed_official 78:5f419e0f96dd 145 #define PRINT_ERROR(RET, CODE) \
mbed_official 78:5f419e0f96dd 146 mbedtls_printf("%s returned -0x%04X\n", CODE, -RET);
mbed_official 78:5f419e0f96dd 147 #endif /* MBEDTLS_ERROR_C */
Janos Follath 0:1f7c5025e59d 148
mbed_official 78:5f419e0f96dd 149 #define BENCHMARK_FUNC_CALL(TITLE, CODE) \
mbed_official 78:5f419e0f96dd 150 do { \
mbed_official 78:5f419e0f96dd 151 unsigned long i; \
mbed_official 78:5f419e0f96dd 152 Timeout t; \
mbed_official 78:5f419e0f96dd 153 \
mbed_official 78:5f419e0f96dd 154 mbedtls_printf(HEADER_FORMAT, TITLE); \
mbed_official 78:5f419e0f96dd 155 fflush(stdout); \
mbed_official 78:5f419e0f96dd 156 \
mbed_official 89:2d5255b9d6c5 157 for (i = 1, alarmed = 0, t.attach(alarm, 1.0); !alarmed; i++) { \
mbed_official 78:5f419e0f96dd 158 ret = CODE; \
mbed_official 89:2d5255b9d6c5 159 if (ret == MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED) { \
mbed_official 89:2d5255b9d6c5 160 mbedtls_printf("Feature unsupported\n"); \
mbed_official 78:5f419e0f96dd 161 break; \
mbed_official 78:5f419e0f96dd 162 } else if (ret != 0) { \
mbed_official 78:5f419e0f96dd 163 PRINT_ERROR(ret, #CODE); \
mbed_official 78:5f419e0f96dd 164 goto exit; \
mbed_official 78:5f419e0f96dd 165 } \
mbed_official 78:5f419e0f96dd 166 } \
mbed_official 78:5f419e0f96dd 167 \
mbed_official 78:5f419e0f96dd 168 if (ret == 0) { \
mbed_official 78:5f419e0f96dd 169 mbedtls_printf("%9lu KB/s\n", i * BUFSIZE / 1024); \
mbed_official 78:5f419e0f96dd 170 } \
mbed_official 78:5f419e0f96dd 171 } while(0)
Janos Follath 0:1f7c5025e59d 172
mbed_official 89:2d5255b9d6c5 173 #define BENCHMARK_PUBLIC(TITLE, TYPE, CODE) \
mbed_official 89:2d5255b9d6c5 174 do { \
mbed_official 89:2d5255b9d6c5 175 unsigned long ms; \
mbed_official 89:2d5255b9d6c5 176 Timer t; \
mbed_official 89:2d5255b9d6c5 177 \
mbed_official 89:2d5255b9d6c5 178 mbedtls_printf(HEADER_FORMAT, TITLE); \
mbed_official 89:2d5255b9d6c5 179 fflush(stdout); \
mbed_official 89:2d5255b9d6c5 180 \
mbed_official 89:2d5255b9d6c5 181 t.start(); \
mbed_official 89:2d5255b9d6c5 182 CODE; \
mbed_official 89:2d5255b9d6c5 183 t.stop(); \
mbed_official 89:2d5255b9d6c5 184 ms = t.read_ms(); \
mbed_official 89:2d5255b9d6c5 185 \
mbed_official 89:2d5255b9d6c5 186 if (ret == MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED) {\
mbed_official 89:2d5255b9d6c5 187 mbedtls_printf("Feature unsupported\n"); \
mbed_official 89:2d5255b9d6c5 188 break; \
mbed_official 89:2d5255b9d6c5 189 } else if (ret != 0) { \
mbed_official 89:2d5255b9d6c5 190 PRINT_ERROR(ret, "Public function"); \
mbed_official 89:2d5255b9d6c5 191 goto exit; \
mbed_official 89:2d5255b9d6c5 192 } else { \
mbed_official 89:2d5255b9d6c5 193 mbedtls_printf("%6lu ms/" TYPE, ms); \
mbed_official 89:2d5255b9d6c5 194 mbedtls_printf("\n"); \
mbed_official 89:2d5255b9d6c5 195 } \
mbed_official 78:5f419e0f96dd 196 } while(0)
mbed_official 78:5f419e0f96dd 197
mbed_official 78:5f419e0f96dd 198 /* Clear some memory that was used to prepare the context */
mbed_official 78:5f419e0f96dd 199 #if defined(MBEDTLS_ECP_C)
mbed_official 78:5f419e0f96dd 200 void ecp_clear_precomputed(mbedtls_ecp_group *grp)
Janos Follath 0:1f7c5025e59d 201 {
mbed_official 78:5f419e0f96dd 202 if (grp->T != NULL) {
Janos Follath 0:1f7c5025e59d 203 size_t i;
mbed_official 78:5f419e0f96dd 204 for (i = 0; i < grp->T_size; i++) {
mbed_official 78:5f419e0f96dd 205 mbedtls_ecp_point_free(&grp->T[i]);
mbed_official 78:5f419e0f96dd 206 }
mbed_official 78:5f419e0f96dd 207 mbedtls_free(grp->T);
Janos Follath 0:1f7c5025e59d 208 }
Janos Follath 0:1f7c5025e59d 209 grp->T = NULL;
Janos Follath 0:1f7c5025e59d 210 grp->T_size = 0;
Janos Follath 0:1f7c5025e59d 211 }
Janos Follath 0:1f7c5025e59d 212 #else
mbed_official 78:5f419e0f96dd 213 #define ecp_clear_precomputed(g)
mbed_official 78:5f419e0f96dd 214 #endif /* MBEDTLS_ECP_C */
Janos Follath 0:1f7c5025e59d 215
mbed_official 78:5f419e0f96dd 216 static unsigned char buf[BUFSIZE];
mbed_official 78:5f419e0f96dd 217 /*
mbed_official 78:5f419e0f96dd 218 * Buffer used to hold various data such as IV, signatures, keys, etc. ECDSA
mbed_official 78:5f419e0f96dd 219 * seems to be the benchmark that uses the most memory from this buffer as it
mbed_official 78:5f419e0f96dd 220 * is holds the output signature
mbed_official 78:5f419e0f96dd 221 */
mbed_official 78:5f419e0f96dd 222 static unsigned char tmp[150];
mbed_official 78:5f419e0f96dd 223 /* The longest error message has 134 characters (including \0) */
mbed_official 78:5f419e0f96dd 224 static char err_buf[134];
mbed_official 78:5f419e0f96dd 225 static char title[TITLE_LEN];
mbed_official 78:5f419e0f96dd 226
mbed_official 78:5f419e0f96dd 227 static volatile int alarmed;
mbed_official 78:5f419e0f96dd 228
mbed_official 78:5f419e0f96dd 229 static void alarm()
mbed_official 78:5f419e0f96dd 230 {
mbed_official 78:5f419e0f96dd 231 alarmed = 1;
mbed_official 78:5f419e0f96dd 232 }
Janos Follath 0:1f7c5025e59d 233
mbed_official 78:5f419e0f96dd 234 static int myrand(void *rng_state, unsigned char *output, size_t len)
mbed_official 78:5f419e0f96dd 235 {
mbed_official 78:5f419e0f96dd 236 size_t use_len;
mbed_official 78:5f419e0f96dd 237 int rnd;
mbed_official 78:5f419e0f96dd 238
mbed_official 78:5f419e0f96dd 239 if (rng_state != NULL) {
mbed_official 78:5f419e0f96dd 240 rng_state = NULL;
mbed_official 78:5f419e0f96dd 241 }
Janos Follath 0:1f7c5025e59d 242
mbed_official 78:5f419e0f96dd 243 while (len > 0) {
mbed_official 78:5f419e0f96dd 244 use_len = len;
mbed_official 78:5f419e0f96dd 245 if (use_len > sizeof(int)) {
mbed_official 78:5f419e0f96dd 246 use_len = sizeof(int);
mbed_official 78:5f419e0f96dd 247 }
mbed_official 78:5f419e0f96dd 248
mbed_official 78:5f419e0f96dd 249 rnd = rand();
mbed_official 78:5f419e0f96dd 250 memcpy(output, &rnd, use_len);
mbed_official 78:5f419e0f96dd 251 output += use_len;
mbed_official 78:5f419e0f96dd 252 len -= use_len;
mbed_official 78:5f419e0f96dd 253 }
mbed_official 78:5f419e0f96dd 254
mbed_official 78:5f419e0f96dd 255 return 0;
mbed_official 78:5f419e0f96dd 256 }
Janos Follath 0:1f7c5025e59d 257
Janos Follath 0:1f7c5025e59d 258 #if defined(MBEDTLS_MD4_C)
mbed_official 78:5f419e0f96dd 259 MBED_NOINLINE static int benchmark_md4()
mbed_official 78:5f419e0f96dd 260 {
mbed_official 78:5f419e0f96dd 261 int ret;
mbed_official 78:5f419e0f96dd 262
mbed_official 78:5f419e0f96dd 263 BENCHMARK_FUNC_CALL("MD4", mbedtls_md4_ret(buf, BUFSIZE, tmp));
mbed_official 78:5f419e0f96dd 264
mbed_official 78:5f419e0f96dd 265 ret = 0;
mbed_official 78:5f419e0f96dd 266
mbed_official 78:5f419e0f96dd 267 exit:
mbed_official 78:5f419e0f96dd 268
mbed_official 78:5f419e0f96dd 269 return ret;
mbed_official 78:5f419e0f96dd 270 }
mbed_official 78:5f419e0f96dd 271 #endif /* MBEDTLS_MD4_C */
Janos Follath 0:1f7c5025e59d 272
Janos Follath 0:1f7c5025e59d 273 #if defined(MBEDTLS_MD5_C)
mbed_official 78:5f419e0f96dd 274 MBED_NOINLINE static int benchmark_md5()
mbed_official 78:5f419e0f96dd 275 {
mbed_official 78:5f419e0f96dd 276 int ret;
mbed_official 78:5f419e0f96dd 277
mbed_official 78:5f419e0f96dd 278 BENCHMARK_FUNC_CALL("MD5", mbedtls_md5_ret(buf, BUFSIZE, tmp));
mbed_official 78:5f419e0f96dd 279
mbed_official 78:5f419e0f96dd 280 ret = 0;
mbed_official 78:5f419e0f96dd 281
mbed_official 78:5f419e0f96dd 282 exit:
mbed_official 78:5f419e0f96dd 283
mbed_official 78:5f419e0f96dd 284 return ret;
mbed_official 78:5f419e0f96dd 285 }
mbed_official 78:5f419e0f96dd 286 #endif /* MBEDTLS_MD5_C */
Janos Follath 0:1f7c5025e59d 287
Janos Follath 0:1f7c5025e59d 288 #if defined(MBEDTLS_RIPEMD160_C)
mbed_official 78:5f419e0f96dd 289 MBED_NOINLINE static int benchmark_ripemd160()
mbed_official 78:5f419e0f96dd 290 {
mbed_official 78:5f419e0f96dd 291 int ret;
mbed_official 78:5f419e0f96dd 292
mbed_official 78:5f419e0f96dd 293 BENCHMARK_FUNC_CALL("RIPEMD160", mbedtls_ripemd160_ret(buf, BUFSIZE, tmp));
mbed_official 78:5f419e0f96dd 294
mbed_official 78:5f419e0f96dd 295 ret = 0;
mbed_official 78:5f419e0f96dd 296
mbed_official 78:5f419e0f96dd 297 exit:
mbed_official 78:5f419e0f96dd 298
mbed_official 78:5f419e0f96dd 299 return ret;
mbed_official 78:5f419e0f96dd 300 }
mbed_official 78:5f419e0f96dd 301 #endif /* MBEDTLS_RIPEMD160_C */
Janos Follath 0:1f7c5025e59d 302
Janos Follath 0:1f7c5025e59d 303 #if defined(MBEDTLS_SHA1_C)
mbed_official 78:5f419e0f96dd 304 MBED_NOINLINE static int benchmark_sha1()
mbed_official 78:5f419e0f96dd 305 {
mbed_official 78:5f419e0f96dd 306 int ret;
mbed_official 78:5f419e0f96dd 307
mbed_official 78:5f419e0f96dd 308 BENCHMARK_FUNC_CALL("SHA-1", mbedtls_sha1_ret(buf, BUFSIZE, tmp));
mbed_official 78:5f419e0f96dd 309
mbed_official 78:5f419e0f96dd 310 ret = 0;
mbed_official 78:5f419e0f96dd 311
mbed_official 78:5f419e0f96dd 312 exit:
mbed_official 78:5f419e0f96dd 313
mbed_official 78:5f419e0f96dd 314 return ret;
mbed_official 78:5f419e0f96dd 315 }
mbed_official 78:5f419e0f96dd 316 #endif /* MBEDTLS_SHA1_C */
Janos Follath 0:1f7c5025e59d 317
Janos Follath 0:1f7c5025e59d 318 #if defined(MBEDTLS_SHA256_C)
mbed_official 78:5f419e0f96dd 319 MBED_NOINLINE static int benchmark_sha256()
mbed_official 78:5f419e0f96dd 320 {
mbed_official 78:5f419e0f96dd 321 int ret;
mbed_official 78:5f419e0f96dd 322
mbed_official 78:5f419e0f96dd 323 BENCHMARK_FUNC_CALL("SHA-256", mbedtls_sha256_ret(buf, BUFSIZE, tmp, 0));
mbed_official 78:5f419e0f96dd 324
mbed_official 78:5f419e0f96dd 325 ret = 0;
mbed_official 78:5f419e0f96dd 326
mbed_official 78:5f419e0f96dd 327 exit:
mbed_official 78:5f419e0f96dd 328
mbed_official 78:5f419e0f96dd 329 return ret;
mbed_official 78:5f419e0f96dd 330 }
mbed_official 78:5f419e0f96dd 331 #endif /* MBEDTLS_SHA256_C */
Janos Follath 0:1f7c5025e59d 332
Janos Follath 0:1f7c5025e59d 333 #if defined(MBEDTLS_SHA512_C)
mbed_official 78:5f419e0f96dd 334 MBED_NOINLINE static int benchmark_sha512()
mbed_official 70:72c865037f5d 335 {
mbed_official 78:5f419e0f96dd 336 int ret;
mbed_official 78:5f419e0f96dd 337
mbed_official 78:5f419e0f96dd 338 BENCHMARK_FUNC_CALL("SHA-512", mbedtls_sha512_ret(buf, BUFSIZE, tmp, 0));
mbed_official 78:5f419e0f96dd 339
mbed_official 78:5f419e0f96dd 340 ret = 0;
mbed_official 78:5f419e0f96dd 341
mbed_official 78:5f419e0f96dd 342 exit:
mbed_official 78:5f419e0f96dd 343
mbed_official 78:5f419e0f96dd 344 return ret;
mbed_official 78:5f419e0f96dd 345 }
mbed_official 78:5f419e0f96dd 346 #endif /* MBEDTLS_SHA512_C */
mbed_official 78:5f419e0f96dd 347
Janos Follath 0:1f7c5025e59d 348 #if defined(MBEDTLS_ARC4_C)
mbed_official 78:5f419e0f96dd 349 MBED_NOINLINE static int benchmark_arc4()
mbed_official 78:5f419e0f96dd 350 {
mbed_official 78:5f419e0f96dd 351 int ret = 0;
mbed_official 78:5f419e0f96dd 352 mbedtls_arc4_context arc4;
mbed_official 78:5f419e0f96dd 353
mbed_official 78:5f419e0f96dd 354 mbedtls_arc4_init(&arc4);
mbed_official 78:5f419e0f96dd 355
mbed_official 78:5f419e0f96dd 356 mbedtls_arc4_setup(&arc4, tmp, 32);
mbed_official 78:5f419e0f96dd 357 BENCHMARK_FUNC_CALL("ARC4",
mbed_official 78:5f419e0f96dd 358 mbedtls_arc4_crypt(&arc4, BUFSIZE, buf, buf));
mbed_official 78:5f419e0f96dd 359
mbed_official 78:5f419e0f96dd 360 ret = 0;
mbed_official 78:5f419e0f96dd 361
mbed_official 78:5f419e0f96dd 362 exit:
mbed_official 78:5f419e0f96dd 363 mbedtls_arc4_free(&arc4);
mbed_official 78:5f419e0f96dd 364
mbed_official 78:5f419e0f96dd 365 return ret;
mbed_official 78:5f419e0f96dd 366 }
mbed_official 78:5f419e0f96dd 367 #endif /* MBEDTLS_ARC4_C */
mbed_official 78:5f419e0f96dd 368
mbed_official 78:5f419e0f96dd 369 #if defined(MBEDTLS_DES_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
mbed_official 78:5f419e0f96dd 370 MBED_NOINLINE static int benchmark_des3()
mbed_official 78:5f419e0f96dd 371 {
mbed_official 78:5f419e0f96dd 372 int ret = 0;
mbed_official 78:5f419e0f96dd 373 mbedtls_des3_context des3;
mbed_official 78:5f419e0f96dd 374
mbed_official 78:5f419e0f96dd 375 mbedtls_des3_init(&des3);
mbed_official 78:5f419e0f96dd 376
mbed_official 78:5f419e0f96dd 377 if ((ret = mbedtls_des3_set3key_enc(&des3, tmp)) != 0) {
mbed_official 78:5f419e0f96dd 378 PRINT_ERROR(ret, "mbedtls_des3_set3key_enc()");
mbed_official 78:5f419e0f96dd 379 goto exit;
Janos Follath 0:1f7c5025e59d 380 }
mbed_official 78:5f419e0f96dd 381 BENCHMARK_FUNC_CALL("3DES",
mbed_official 78:5f419e0f96dd 382 mbedtls_des3_crypt_cbc(&des3, MBEDTLS_DES_ENCRYPT,
mbed_official 78:5f419e0f96dd 383 BUFSIZE, tmp, buf, buf));
mbed_official 78:5f419e0f96dd 384
mbed_official 78:5f419e0f96dd 385 ret = 0;
mbed_official 78:5f419e0f96dd 386
mbed_official 78:5f419e0f96dd 387 exit:
mbed_official 78:5f419e0f96dd 388 mbedtls_des3_free(&des3);
mbed_official 78:5f419e0f96dd 389
mbed_official 78:5f419e0f96dd 390 return ret;
mbed_official 78:5f419e0f96dd 391 }
mbed_official 78:5f419e0f96dd 392 #endif /* MBEDTLS_DES_C && MBEDTLS_CIPHER_MODE_CBC */
Janos Follath 0:1f7c5025e59d 393
Janos Follath 0:1f7c5025e59d 394 #if defined(MBEDTLS_DES_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
mbed_official 78:5f419e0f96dd 395 MBED_NOINLINE static int benchmark_des()
mbed_official 78:5f419e0f96dd 396 {
mbed_official 78:5f419e0f96dd 397 int ret = 0;
mbed_official 78:5f419e0f96dd 398 mbedtls_des_context des;
mbed_official 78:5f419e0f96dd 399
mbed_official 78:5f419e0f96dd 400 mbedtls_des_init(&des);
mbed_official 78:5f419e0f96dd 401
mbed_official 78:5f419e0f96dd 402 if ((ret = mbedtls_des_setkey_enc(&des, tmp)) != 0) {
mbed_official 78:5f419e0f96dd 403 PRINT_ERROR(ret, "mbedtls_des_setkey_enc()");
mbed_official 78:5f419e0f96dd 404 goto exit;
mbed_official 78:5f419e0f96dd 405 }
mbed_official 78:5f419e0f96dd 406 BENCHMARK_FUNC_CALL("DES",
mbed_official 78:5f419e0f96dd 407 mbedtls_des_crypt_cbc(&des, MBEDTLS_DES_ENCRYPT,
mbed_official 78:5f419e0f96dd 408 BUFSIZE, tmp, buf, buf));
mbed_official 78:5f419e0f96dd 409
mbed_official 78:5f419e0f96dd 410 ret = 0;
mbed_official 78:5f419e0f96dd 411
mbed_official 78:5f419e0f96dd 412 exit:
mbed_official 78:5f419e0f96dd 413 mbedtls_des_free(&des);
mbed_official 78:5f419e0f96dd 414
mbed_official 78:5f419e0f96dd 415 return ret;
mbed_official 78:5f419e0f96dd 416 }
mbed_official 78:5f419e0f96dd 417 #endif /* MBEDTLS_DES_C && MBEDTLS_CIPHER_MODE_CBC */
mbed_official 78:5f419e0f96dd 418
mbed_official 78:5f419e0f96dd 419 #if defined(MBEDTLS_DES_C) && defined(MBEDTLS_CIPHER_MODE_CBC) && \
mbed_official 78:5f419e0f96dd 420 defined(MBEDTLS_CMAC_C)
mbed_official 78:5f419e0f96dd 421 MBED_NOINLINE static int benchmark_des3_cmac()
mbed_official 78:5f419e0f96dd 422 {
mbed_official 78:5f419e0f96dd 423 int ret = 0;
mbed_official 78:5f419e0f96dd 424 unsigned char output[8];
mbed_official 78:5f419e0f96dd 425 const mbedtls_cipher_info_t *cipher_info;
mbed_official 78:5f419e0f96dd 426
mbed_official 78:5f419e0f96dd 427 memset(buf, 0, sizeof(buf));
mbed_official 78:5f419e0f96dd 428 memset(tmp, 0, sizeof(tmp));
mbed_official 78:5f419e0f96dd 429
mbed_official 78:5f419e0f96dd 430 cipher_info = mbedtls_cipher_info_from_type(MBEDTLS_CIPHER_DES_EDE3_ECB);
mbed_official 78:5f419e0f96dd 431 if (cipher_info == NULL) {
mbed_official 78:5f419e0f96dd 432 mbedtls_printf("mbedtls_cipher_info_from_type() returned NULL\n");
mbed_official 78:5f419e0f96dd 433 return -1;
mbed_official 78:5f419e0f96dd 434 }
mbed_official 78:5f419e0f96dd 435
mbed_official 78:5f419e0f96dd 436 BENCHMARK_FUNC_CALL("3DES-CMAC",
mbed_official 78:5f419e0f96dd 437 mbedtls_cipher_cmac(cipher_info, tmp, 192, buf,
mbed_official 78:5f419e0f96dd 438 BUFSIZE, output));
mbed_official 78:5f419e0f96dd 439
mbed_official 78:5f419e0f96dd 440 ret = 0;
mbed_official 78:5f419e0f96dd 441
mbed_official 78:5f419e0f96dd 442 exit:
mbed_official 78:5f419e0f96dd 443 return ret;
mbed_official 78:5f419e0f96dd 444 }
mbed_official 78:5f419e0f96dd 445 #endif /* MBEDTLS_DES_C && MBEDTLS_CIPHER_MODE_CBC && MBEDTLS_CMAC_C */
mbed_official 78:5f419e0f96dd 446
mbed_official 78:5f419e0f96dd 447 #if defined(MBEDTLS_AES_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
mbed_official 78:5f419e0f96dd 448 MBED_NOINLINE static int benchmark_aes_cbc()
mbed_official 78:5f419e0f96dd 449 {
mbed_official 78:5f419e0f96dd 450 int ret = 0;
mbed_official 78:5f419e0f96dd 451 int keysize;
mbed_official 78:5f419e0f96dd 452 mbedtls_aes_context aes;
mbed_official 78:5f419e0f96dd 453
mbed_official 78:5f419e0f96dd 454 mbedtls_aes_init(&aes);
mbed_official 78:5f419e0f96dd 455
mbed_official 78:5f419e0f96dd 456 for (keysize = 128; keysize <= 256; keysize += 64) {
mbed_official 78:5f419e0f96dd 457 ret = mbedtls_snprintf(title, sizeof(title), "AES-CBC-%d", keysize);
mbed_official 78:5f419e0f96dd 458 if (ret < 0 || static_cast<size_t>(ret) >= sizeof(title)) {
mbed_official 78:5f419e0f96dd 459 mbedtls_printf("Failed to compose title string using "
mbed_official 78:5f419e0f96dd 460 "mbedtls_snprintf(): %d\n", ret);
mbed_official 78:5f419e0f96dd 461 goto exit;
mbed_official 78:5f419e0f96dd 462 }
mbed_official 78:5f419e0f96dd 463
mbed_official 78:5f419e0f96dd 464 memset(buf, 0, sizeof(buf));
mbed_official 78:5f419e0f96dd 465 memset(tmp, 0, sizeof(tmp));
mbed_official 78:5f419e0f96dd 466
mbed_official 78:5f419e0f96dd 467 ret = mbedtls_aes_setkey_enc(&aes, tmp, keysize);
mbed_official 89:2d5255b9d6c5 468 if (ret == MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED) {
mbed_official 78:5f419e0f96dd 469 /* Do not consider this as a failure */
mbed_official 89:2d5255b9d6c5 470 mbedtls_printf(HEADER_FORMAT "Feature unsupported\n", title);
mbed_official 78:5f419e0f96dd 471 continue;
mbed_official 78:5f419e0f96dd 472 } else if (ret != 0) {
mbed_official 78:5f419e0f96dd 473 PRINT_ERROR(ret, "mbedtls_aes_setkey_enc()");
mbed_official 78:5f419e0f96dd 474 goto exit;
mbed_official 78:5f419e0f96dd 475 }
mbed_official 78:5f419e0f96dd 476
mbed_official 78:5f419e0f96dd 477 BENCHMARK_FUNC_CALL(title,
mbed_official 78:5f419e0f96dd 478 mbedtls_aes_crypt_cbc(&aes,
mbed_official 78:5f419e0f96dd 479 MBEDTLS_AES_ENCRYPT, BUFSIZE,
mbed_official 78:5f419e0f96dd 480 tmp, buf, buf));
mbed_official 78:5f419e0f96dd 481 }
mbed_official 78:5f419e0f96dd 482
mbed_official 78:5f419e0f96dd 483 ret = 0;
mbed_official 78:5f419e0f96dd 484
mbed_official 78:5f419e0f96dd 485 exit:
mbed_official 78:5f419e0f96dd 486 mbedtls_aes_free(&aes);
mbed_official 78:5f419e0f96dd 487
mbed_official 78:5f419e0f96dd 488 return ret;
mbed_official 78:5f419e0f96dd 489 }
mbed_official 78:5f419e0f96dd 490 #endif /* MBEDTLS_AES_C && MBEDTLS_CIPHER_MODE_CBC */
mbed_official 78:5f419e0f96dd 491
mbed_official 78:5f419e0f96dd 492 #if defined(MBEDTLS_AES_C) && defined(MBEDTLS_CIPHER_MODE_CTR)
mbed_official 78:5f419e0f96dd 493 MBED_NOINLINE static int benchmark_aes_ctr()
mbed_official 78:5f419e0f96dd 494 {
mbed_official 78:5f419e0f96dd 495 int ret = 0;
mbed_official 78:5f419e0f96dd 496 int keysize;
mbed_official 78:5f419e0f96dd 497 size_t nc_offset = 0;
mbed_official 78:5f419e0f96dd 498 unsigned char stream_block[16];
mbed_official 78:5f419e0f96dd 499 mbedtls_aes_context aes;
mbed_official 78:5f419e0f96dd 500
mbed_official 78:5f419e0f96dd 501 mbedtls_aes_init(&aes);
mbed_official 78:5f419e0f96dd 502
mbed_official 78:5f419e0f96dd 503 for (keysize = 128; keysize <= 256; keysize += 64) {
mbed_official 78:5f419e0f96dd 504 ret = mbedtls_snprintf(title, sizeof(title), "AES-CTR-%d", keysize);
mbed_official 78:5f419e0f96dd 505 if (ret < 0 || static_cast<size_t>(ret) >= sizeof(title)) {
mbed_official 78:5f419e0f96dd 506 mbedtls_printf("Failed to compose title string using "
mbed_official 78:5f419e0f96dd 507 "mbedtls_snprintf(): %d\n", ret);
mbed_official 78:5f419e0f96dd 508 goto exit;
mbed_official 78:5f419e0f96dd 509 }
mbed_official 78:5f419e0f96dd 510
mbed_official 78:5f419e0f96dd 511 memset(buf, 0, sizeof(buf));
mbed_official 78:5f419e0f96dd 512 memset(tmp, 0, sizeof(tmp));
mbed_official 78:5f419e0f96dd 513
mbed_official 78:5f419e0f96dd 514 ret = mbedtls_aes_setkey_enc(&aes, tmp, keysize);
mbed_official 89:2d5255b9d6c5 515 if (ret == MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED) {
mbed_official 78:5f419e0f96dd 516 /* Do not consider this as a failure */
mbed_official 89:2d5255b9d6c5 517 mbedtls_printf(HEADER_FORMAT "Feature unsupported\n", title);
mbed_official 78:5f419e0f96dd 518 continue;
mbed_official 78:5f419e0f96dd 519 } else if (ret != 0) {
mbed_official 78:5f419e0f96dd 520 PRINT_ERROR(ret, "mbedtls_aes_setkey_enc()");
mbed_official 78:5f419e0f96dd 521 goto exit;
mbed_official 78:5f419e0f96dd 522 }
mbed_official 78:5f419e0f96dd 523
mbed_official 78:5f419e0f96dd 524 BENCHMARK_FUNC_CALL(title,
mbed_official 78:5f419e0f96dd 525 mbedtls_aes_crypt_ctr(&aes, BUFSIZE, &nc_offset,
mbed_official 78:5f419e0f96dd 526 tmp, stream_block, buf,
mbed_official 78:5f419e0f96dd 527 buf));
Janos Follath 0:1f7c5025e59d 528 }
Janos Follath 0:1f7c5025e59d 529
mbed_official 78:5f419e0f96dd 530 ret = 0;
mbed_official 78:5f419e0f96dd 531
mbed_official 78:5f419e0f96dd 532 exit:
mbed_official 78:5f419e0f96dd 533 mbedtls_aes_free(&aes);
mbed_official 78:5f419e0f96dd 534
mbed_official 78:5f419e0f96dd 535 return ret;
mbed_official 78:5f419e0f96dd 536 }
mbed_official 78:5f419e0f96dd 537 #endif /* MBEDTLS_AES_C && MBEDTLS_CIPHER_MODE_CTR */
mbed_official 11:6ccae3ebafd5 538
mbed_official 78:5f419e0f96dd 539 #if defined(MBEDTLS_AES_C) && defined(MBEDTLS_GCM_C)
mbed_official 78:5f419e0f96dd 540 MBED_NOINLINE static int benchmark_aes_gcm()
mbed_official 78:5f419e0f96dd 541 {
mbed_official 78:5f419e0f96dd 542 int ret = 0;
mbed_official 78:5f419e0f96dd 543 int keysize;
mbed_official 78:5f419e0f96dd 544 mbedtls_gcm_context gcm;
mbed_official 11:6ccae3ebafd5 545
mbed_official 78:5f419e0f96dd 546 mbedtls_gcm_init(&gcm);
Janos Follath 0:1f7c5025e59d 547
mbed_official 78:5f419e0f96dd 548 for (keysize = 128; keysize <= 256; keysize += 64) {
mbed_official 78:5f419e0f96dd 549 ret = mbedtls_snprintf(title, sizeof(title), "AES-GCM-%d", keysize);
mbed_official 78:5f419e0f96dd 550 if (ret < 0 || static_cast<size_t>(ret) >= sizeof(title)) {
mbed_official 78:5f419e0f96dd 551 mbedtls_printf("Failed to compose title string using "
mbed_official 78:5f419e0f96dd 552 "mbedtls_snprintf(): %d\n", ret);
mbed_official 78:5f419e0f96dd 553 goto exit;
Janos Follath 0:1f7c5025e59d 554 }
mbed_official 78:5f419e0f96dd 555
mbed_official 78:5f419e0f96dd 556 memset(buf, 0, sizeof(buf));
mbed_official 78:5f419e0f96dd 557 memset(tmp, 0, sizeof(tmp));
mbed_official 30:e0ea8c1ef9f5 558
mbed_official 78:5f419e0f96dd 559 ret = mbedtls_gcm_setkey(&gcm, MBEDTLS_CIPHER_ID_AES, tmp, keysize);
mbed_official 89:2d5255b9d6c5 560 if (ret == MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED) {
mbed_official 78:5f419e0f96dd 561 /* Do not consider this as a failure */
mbed_official 89:2d5255b9d6c5 562 mbedtls_printf(HEADER_FORMAT "Feature unsupported\n", title);
mbed_official 78:5f419e0f96dd 563 continue;
mbed_official 78:5f419e0f96dd 564 } else if (ret != 0) {
mbed_official 78:5f419e0f96dd 565 PRINT_ERROR(ret, "mbedtls_gcm_setkey()");
mbed_official 78:5f419e0f96dd 566 goto exit;
mbed_official 78:5f419e0f96dd 567 }
mbed_official 30:e0ea8c1ef9f5 568
mbed_official 78:5f419e0f96dd 569 BENCHMARK_FUNC_CALL(title,
mbed_official 78:5f419e0f96dd 570 mbedtls_gcm_crypt_and_tag(&gcm,
mbed_official 78:5f419e0f96dd 571 MBEDTLS_GCM_ENCRYPT,
mbed_official 78:5f419e0f96dd 572 BUFSIZE, tmp, 12, NULL,
mbed_official 78:5f419e0f96dd 573 0, buf, buf, 16, tmp));
mbed_official 30:e0ea8c1ef9f5 574 }
mbed_official 30:e0ea8c1ef9f5 575
mbed_official 78:5f419e0f96dd 576 ret = 0;
Janos Follath 0:1f7c5025e59d 577
mbed_official 78:5f419e0f96dd 578 exit:
mbed_official 78:5f419e0f96dd 579 mbedtls_gcm_free(&gcm);
Janos Follath 0:1f7c5025e59d 580
mbed_official 78:5f419e0f96dd 581 return ret;
mbed_official 78:5f419e0f96dd 582 }
mbed_official 78:5f419e0f96dd 583 #endif /* MBEDTLS_AES_C && MBEDTLS_GCM_C */
Janos Follath 0:1f7c5025e59d 584
mbed_official 78:5f419e0f96dd 585 #if defined(MBEDTLS_AES_C) && defined(MBEDTLS_CCM_C)
mbed_official 78:5f419e0f96dd 586 MBED_NOINLINE static int benchmark_aes_ccm()
mbed_official 78:5f419e0f96dd 587 {
mbed_official 78:5f419e0f96dd 588 int ret = 0;
mbed_official 78:5f419e0f96dd 589 int keysize;
mbed_official 78:5f419e0f96dd 590 mbedtls_ccm_context ccm;
Janos Follath 0:1f7c5025e59d 591
mbed_official 78:5f419e0f96dd 592 mbedtls_ccm_init(&ccm);
mbed_official 11:6ccae3ebafd5 593
mbed_official 78:5f419e0f96dd 594 for (keysize = 128; keysize <= 256; keysize += 64) {
mbed_official 78:5f419e0f96dd 595 ret = mbedtls_snprintf(title, sizeof(title), "AES-CCM-%d", keysize);
mbed_official 78:5f419e0f96dd 596 if (ret < 0 || static_cast<size_t>(ret) >= sizeof(title)) {
mbed_official 78:5f419e0f96dd 597 mbedtls_printf("Failed to compose title string using "
mbed_official 78:5f419e0f96dd 598 "mbedtls_snprintf(): %d\n", ret);
mbed_official 78:5f419e0f96dd 599 goto exit;
mbed_official 11:6ccae3ebafd5 600 }
mbed_official 11:6ccae3ebafd5 601
mbed_official 78:5f419e0f96dd 602 memset(buf, 0, sizeof(buf));
mbed_official 78:5f419e0f96dd 603 memset(tmp, 0, sizeof(tmp));
mbed_official 78:5f419e0f96dd 604
mbed_official 78:5f419e0f96dd 605 ret = mbedtls_ccm_setkey(&ccm, MBEDTLS_CIPHER_ID_AES, tmp, keysize);
mbed_official 89:2d5255b9d6c5 606 if (ret == MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED) {
mbed_official 78:5f419e0f96dd 607 /* Do not consider this as a failure */
mbed_official 89:2d5255b9d6c5 608 mbedtls_printf(HEADER_FORMAT "Feature unsupported\n", title);
mbed_official 78:5f419e0f96dd 609 continue;
mbed_official 78:5f419e0f96dd 610 } else if (ret != 0) {
mbed_official 78:5f419e0f96dd 611 PRINT_ERROR(ret, "mbedtls_gcm_setkey()");
mbed_official 78:5f419e0f96dd 612 goto exit;
mbed_official 78:5f419e0f96dd 613 }
mbed_official 78:5f419e0f96dd 614
mbed_official 78:5f419e0f96dd 615 BENCHMARK_FUNC_CALL(title,
mbed_official 78:5f419e0f96dd 616 mbedtls_ccm_encrypt_and_tag(&ccm, BUFSIZE, tmp, 12,
mbed_official 78:5f419e0f96dd 617 NULL, 0, buf, buf, tmp,
mbed_official 78:5f419e0f96dd 618 16));
mbed_official 11:6ccae3ebafd5 619 }
mbed_official 78:5f419e0f96dd 620
mbed_official 78:5f419e0f96dd 621 ret = 0;
mbed_official 78:5f419e0f96dd 622
mbed_official 78:5f419e0f96dd 623 exit:
mbed_official 78:5f419e0f96dd 624 mbedtls_ccm_free(&ccm);
mbed_official 78:5f419e0f96dd 625
mbed_official 78:5f419e0f96dd 626 return ret;
mbed_official 78:5f419e0f96dd 627 }
mbed_official 78:5f419e0f96dd 628 #endif /* MBEDTLS_AES_C && MBEDTLS_CCM_C */
mbed_official 78:5f419e0f96dd 629
mbed_official 78:5f419e0f96dd 630 #if defined(MBEDTLS_AES_C) && defined(MBEDTLS_CMAC_C)
mbed_official 78:5f419e0f96dd 631 MBED_NOINLINE static int benchmark_aes_cmac()
mbed_official 78:5f419e0f96dd 632 {
mbed_official 78:5f419e0f96dd 633 int ret = 0;
mbed_official 78:5f419e0f96dd 634 unsigned char output[16];
mbed_official 78:5f419e0f96dd 635 const mbedtls_cipher_info_t *cipher_info;
mbed_official 78:5f419e0f96dd 636 mbedtls_cipher_type_t cipher_type;
mbed_official 78:5f419e0f96dd 637 int keysize;
mbed_official 78:5f419e0f96dd 638
mbed_official 78:5f419e0f96dd 639 cipher_type = MBEDTLS_CIPHER_AES_128_ECB;
mbed_official 78:5f419e0f96dd 640 for (keysize = 128; keysize <= 256; keysize += 64) {
mbed_official 78:5f419e0f96dd 641 ret = mbedtls_snprintf(title, sizeof(title), "AES-CMAC-%d", keysize);
mbed_official 78:5f419e0f96dd 642 if (ret < 0 || static_cast<size_t>(ret) >= sizeof(title)) {
mbed_official 78:5f419e0f96dd 643 mbedtls_printf("Failed to compose title string using "
mbed_official 78:5f419e0f96dd 644 "mbedtls_snprintf(): %d\n", ret);
mbed_official 78:5f419e0f96dd 645 goto exit;
mbed_official 78:5f419e0f96dd 646 }
mbed_official 78:5f419e0f96dd 647
mbed_official 78:5f419e0f96dd 648 memset(buf, 0, sizeof(buf));
mbed_official 78:5f419e0f96dd 649 memset(tmp, 0, sizeof(tmp));
mbed_official 78:5f419e0f96dd 650
mbed_official 78:5f419e0f96dd 651 cipher_info = mbedtls_cipher_info_from_type(cipher_type);
mbed_official 78:5f419e0f96dd 652 if (cipher_info == NULL) {
mbed_official 78:5f419e0f96dd 653 mbedtls_printf("mbedtls_cipher_info_from_type() returned NULL\n");
mbed_official 78:5f419e0f96dd 654 goto exit;
mbed_official 78:5f419e0f96dd 655 }
mbed_official 78:5f419e0f96dd 656
mbed_official 78:5f419e0f96dd 657 BENCHMARK_FUNC_CALL(title,
mbed_official 78:5f419e0f96dd 658 mbedtls_cipher_cmac(cipher_info, tmp, keysize,
mbed_official 78:5f419e0f96dd 659 buf, BUFSIZE, output));
mbed_official 78:5f419e0f96dd 660 cipher_type = (mbedtls_cipher_type_t)(cipher_type + 1);
mbed_official 78:5f419e0f96dd 661 }
mbed_official 78:5f419e0f96dd 662
mbed_official 78:5f419e0f96dd 663 memset(buf, 0, sizeof(buf));
mbed_official 78:5f419e0f96dd 664 memset(tmp, 0, sizeof(tmp));
mbed_official 78:5f419e0f96dd 665
mbed_official 78:5f419e0f96dd 666 BENCHMARK_FUNC_CALL("AES-CMAC-PRF-128",
mbed_official 78:5f419e0f96dd 667 mbedtls_aes_cmac_prf_128(tmp, 16, buf, BUFSIZE,
mbed_official 78:5f419e0f96dd 668 output));
mbed_official 78:5f419e0f96dd 669
mbed_official 78:5f419e0f96dd 670 ret = 0;
mbed_official 78:5f419e0f96dd 671
mbed_official 78:5f419e0f96dd 672 exit:
mbed_official 78:5f419e0f96dd 673
mbed_official 78:5f419e0f96dd 674 return ret;
mbed_official 78:5f419e0f96dd 675 }
mbed_official 78:5f419e0f96dd 676 #endif /* MBEDTLS_AES_C && MBEDTLS_CMAC_C */
Janos Follath 0:1f7c5025e59d 677
Janos Follath 0:1f7c5025e59d 678 #if defined(MBEDTLS_CAMELLIA_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
mbed_official 78:5f419e0f96dd 679 MBED_NOINLINE static int benchmark_camellia()
mbed_official 78:5f419e0f96dd 680 {
mbed_official 78:5f419e0f96dd 681 int ret = 0;
mbed_official 78:5f419e0f96dd 682 int keysize;
mbed_official 78:5f419e0f96dd 683 mbedtls_camellia_context camellia;
mbed_official 78:5f419e0f96dd 684
mbed_official 78:5f419e0f96dd 685 mbedtls_camellia_init(&camellia);
mbed_official 78:5f419e0f96dd 686
mbed_official 78:5f419e0f96dd 687 for (keysize = 128; keysize <= 256; keysize += 64) {
mbed_official 78:5f419e0f96dd 688 ret = mbedtls_snprintf(title, sizeof(title), "CAMELLIA-CBC-%d",
mbed_official 78:5f419e0f96dd 689 keysize);
mbed_official 78:5f419e0f96dd 690 if (ret < 0 || static_cast<size_t>(ret) >= sizeof(title)) {
mbed_official 78:5f419e0f96dd 691 mbedtls_printf("Failed to compose title string using "
mbed_official 78:5f419e0f96dd 692 "mbedtls_snprintf(): %d\n", ret);
mbed_official 78:5f419e0f96dd 693 goto exit;
mbed_official 78:5f419e0f96dd 694 }
mbed_official 78:5f419e0f96dd 695
mbed_official 78:5f419e0f96dd 696 memset(buf, 0, sizeof(buf));
mbed_official 78:5f419e0f96dd 697 memset(tmp, 0, sizeof(tmp));
Janos Follath 0:1f7c5025e59d 698
mbed_official 78:5f419e0f96dd 699 ret = mbedtls_camellia_setkey_enc(&camellia, tmp, keysize);
mbed_official 89:2d5255b9d6c5 700 if (ret == MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED) {
mbed_official 89:2d5255b9d6c5 701 /* Do not consider this as a failure */
mbed_official 89:2d5255b9d6c5 702 mbedtls_printf(HEADER_FORMAT "Feature unsupported\n", title);
mbed_official 89:2d5255b9d6c5 703 ret = 0;
mbed_official 89:2d5255b9d6c5 704 continue;
mbed_official 89:2d5255b9d6c5 705 } else if (ret != 0) {
mbed_official 78:5f419e0f96dd 706 PRINT_ERROR(ret, "mbedtls_camellia_setkey_enc()");
mbed_official 78:5f419e0f96dd 707 goto exit;
mbed_official 78:5f419e0f96dd 708 }
Janos Follath 0:1f7c5025e59d 709
mbed_official 78:5f419e0f96dd 710 BENCHMARK_FUNC_CALL(title,
mbed_official 78:5f419e0f96dd 711 mbedtls_camellia_crypt_cbc(&camellia,
mbed_official 78:5f419e0f96dd 712 MBEDTLS_CAMELLIA_ENCRYPT,
mbed_official 78:5f419e0f96dd 713 BUFSIZE, tmp, buf, buf));
Janos Follath 0:1f7c5025e59d 714 }
mbed_official 78:5f419e0f96dd 715
mbed_official 78:5f419e0f96dd 716 ret = 0;
mbed_official 78:5f419e0f96dd 717
mbed_official 78:5f419e0f96dd 718 exit:
mbed_official 78:5f419e0f96dd 719 mbedtls_camellia_free(&camellia);
mbed_official 78:5f419e0f96dd 720
mbed_official 78:5f419e0f96dd 721 return ret;
mbed_official 78:5f419e0f96dd 722 }
mbed_official 78:5f419e0f96dd 723 #endif /* MBEDTLS_CAMELLIA_C && MBEDTLS_CIPHER_MODE_CBC */
Janos Follath 0:1f7c5025e59d 724
Janos Follath 0:1f7c5025e59d 725 #if defined(MBEDTLS_BLOWFISH_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
mbed_official 78:5f419e0f96dd 726 MBED_NOINLINE static int benchmark_blowfish()
mbed_official 78:5f419e0f96dd 727 {
mbed_official 78:5f419e0f96dd 728 int ret = 0;
mbed_official 78:5f419e0f96dd 729 int keysize;
mbed_official 78:5f419e0f96dd 730 mbedtls_blowfish_context *blowfish;
mbed_official 78:5f419e0f96dd 731
mbed_official 78:5f419e0f96dd 732 blowfish = (mbedtls_blowfish_context *)mbedtls_calloc(1,
mbed_official 78:5f419e0f96dd 733 sizeof(mbedtls_blowfish_context));
mbed_official 78:5f419e0f96dd 734 if (blowfish == NULL) {
mbed_official 78:5f419e0f96dd 735 mbedtls_printf("Failed to allocate mbedtls_blowfish_context\n");
mbed_official 78:5f419e0f96dd 736 return -1;
mbed_official 78:5f419e0f96dd 737 }
mbed_official 78:5f419e0f96dd 738
mbed_official 78:5f419e0f96dd 739 mbedtls_blowfish_init(blowfish);
Janos Follath 0:1f7c5025e59d 740
mbed_official 78:5f419e0f96dd 741 for (keysize = 128; keysize <= 256; keysize += 64) {
mbed_official 78:5f419e0f96dd 742 mbedtls_snprintf(title, sizeof(title), "BLOWFISH-CBC-%d", keysize);
mbed_official 78:5f419e0f96dd 743 if (ret < 0 || static_cast<size_t>(ret) >= sizeof(title)) {
mbed_official 78:5f419e0f96dd 744 mbedtls_printf("Failed to compose title string using "
mbed_official 78:5f419e0f96dd 745 "mbedtls_snprintf(): %d\n", ret);
mbed_official 78:5f419e0f96dd 746 goto exit;
mbed_official 78:5f419e0f96dd 747 }
Janos Follath 0:1f7c5025e59d 748
mbed_official 78:5f419e0f96dd 749 memset(buf, 0, sizeof(buf));
mbed_official 78:5f419e0f96dd 750 memset(tmp, 0, sizeof(tmp));
Janos Follath 0:1f7c5025e59d 751
mbed_official 89:2d5255b9d6c5 752 ret = mbedtls_blowfish_setkey(blowfish, tmp, keysize);
mbed_official 89:2d5255b9d6c5 753 if (ret == MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED) {
mbed_official 89:2d5255b9d6c5 754 /* Do not consider this as a failure */
mbed_official 89:2d5255b9d6c5 755 mbedtls_printf(HEADER_FORMAT "Feature unsupported\n", title);
mbed_official 89:2d5255b9d6c5 756 ret = 0;
mbed_official 89:2d5255b9d6c5 757 continue;
mbed_official 89:2d5255b9d6c5 758 } else if (ret != 0) {
mbed_official 78:5f419e0f96dd 759 PRINT_ERROR(ret, "mbedtls_blowfish_setkey()");
mbed_official 78:5f419e0f96dd 760 goto exit;
Janos Follath 0:1f7c5025e59d 761 }
Janos Follath 0:1f7c5025e59d 762
mbed_official 78:5f419e0f96dd 763 BENCHMARK_FUNC_CALL(title,
mbed_official 78:5f419e0f96dd 764 mbedtls_blowfish_crypt_cbc(blowfish,
mbed_official 78:5f419e0f96dd 765 MBEDTLS_BLOWFISH_ENCRYPT,
mbed_official 78:5f419e0f96dd 766 BUFSIZE,
mbed_official 78:5f419e0f96dd 767 tmp, buf, buf));
Janos Follath 0:1f7c5025e59d 768 }
mbed_official 70:72c865037f5d 769
mbed_official 78:5f419e0f96dd 770 ret = 0;
mbed_official 78:5f419e0f96dd 771
mbed_official 78:5f419e0f96dd 772 exit:
mbed_official 78:5f419e0f96dd 773 mbedtls_blowfish_free(blowfish);
mbed_official 78:5f419e0f96dd 774 mbedtls_free(blowfish);
mbed_official 78:5f419e0f96dd 775
mbed_official 78:5f419e0f96dd 776 return ret;
mbed_official 78:5f419e0f96dd 777 }
mbed_official 78:5f419e0f96dd 778 #endif /* MBEDTLS_BLOWFISH_C && MBEDTLS_CIPHER_MODE_CBC */
mbed_official 70:72c865037f5d 779
Janos Follath 0:1f7c5025e59d 780 #if defined(MBEDTLS_HAVEGE_C)
mbed_official 78:5f419e0f96dd 781 MBED_NOINLINE static int benchmark_havege()
mbed_official 78:5f419e0f96dd 782 {
mbed_official 78:5f419e0f96dd 783 int ret = 0;
mbed_official 78:5f419e0f96dd 784 mbedtls_havege_state hs;
mbed_official 78:5f419e0f96dd 785
mbed_official 78:5f419e0f96dd 786 mbedtls_havege_init(&hs);
mbed_official 78:5f419e0f96dd 787
mbed_official 78:5f419e0f96dd 788 BENCHMARK_FUNC_CALL("HAVEGE", mbedtls_havege_random(&hs, buf, BUFSIZE));
mbed_official 78:5f419e0f96dd 789
mbed_official 78:5f419e0f96dd 790 ret = 0;
mbed_official 78:5f419e0f96dd 791
mbed_official 78:5f419e0f96dd 792 exit:
mbed_official 78:5f419e0f96dd 793 mbedtls_havege_free(&hs);
mbed_official 78:5f419e0f96dd 794
mbed_official 78:5f419e0f96dd 795 return ret;
mbed_official 78:5f419e0f96dd 796 }
mbed_official 78:5f419e0f96dd 797 #endif /* MBEDTLS_HAVEGE_C */
Janos Follath 0:1f7c5025e59d 798
Janos Follath 0:1f7c5025e59d 799 #if defined(MBEDTLS_CTR_DRBG_C)
mbed_official 78:5f419e0f96dd 800 MBED_NOINLINE static int benchmark_ctr_drbg()
mbed_official 78:5f419e0f96dd 801 {
mbed_official 78:5f419e0f96dd 802 int ret = 0;
mbed_official 78:5f419e0f96dd 803 const char *nopr_title = "CTR_DRBG (NOPR)";
mbed_official 78:5f419e0f96dd 804 const char *pr_title = "CTR_DRBG (PR)";
mbed_official 78:5f419e0f96dd 805 mbedtls_ctr_drbg_context ctr_drbg;
mbed_official 78:5f419e0f96dd 806
mbed_official 78:5f419e0f96dd 807 mbedtls_ctr_drbg_init(&ctr_drbg);
Janos Follath 0:1f7c5025e59d 808
mbed_official 78:5f419e0f96dd 809 ret = mbedtls_ctr_drbg_seed(&ctr_drbg, myrand, NULL, NULL, 0);
mbed_official 78:5f419e0f96dd 810 if (ret != 0) {
mbed_official 78:5f419e0f96dd 811 PRINT_ERROR(ret, "mbedtls_ctr_drbg_seed()");
mbed_official 78:5f419e0f96dd 812 goto exit;
mbed_official 78:5f419e0f96dd 813 }
mbed_official 78:5f419e0f96dd 814
mbed_official 78:5f419e0f96dd 815 BENCHMARK_FUNC_CALL(nopr_title,
mbed_official 78:5f419e0f96dd 816 mbedtls_ctr_drbg_random(&ctr_drbg, buf, BUFSIZE));
Janos Follath 0:1f7c5025e59d 817
mbed_official 78:5f419e0f96dd 818 ret = mbedtls_ctr_drbg_seed(&ctr_drbg, myrand, NULL, NULL, 0);
mbed_official 78:5f419e0f96dd 819 if (ret != 0) {
mbed_official 78:5f419e0f96dd 820 PRINT_ERROR(ret, "mbedtls_ctr_drbg_seed()");
mbed_official 78:5f419e0f96dd 821 goto exit;
mbed_official 78:5f419e0f96dd 822 }
Janos Follath 0:1f7c5025e59d 823
mbed_official 78:5f419e0f96dd 824 mbedtls_ctr_drbg_set_prediction_resistance(&ctr_drbg,
mbed_official 78:5f419e0f96dd 825 MBEDTLS_CTR_DRBG_PR_ON);
mbed_official 78:5f419e0f96dd 826 BENCHMARK_FUNC_CALL(pr_title,
mbed_official 78:5f419e0f96dd 827 mbedtls_ctr_drbg_random(&ctr_drbg, buf, BUFSIZE));
mbed_official 78:5f419e0f96dd 828
mbed_official 78:5f419e0f96dd 829 ret = 0;
mbed_official 78:5f419e0f96dd 830
mbed_official 78:5f419e0f96dd 831 exit:
mbed_official 78:5f419e0f96dd 832 mbedtls_ctr_drbg_free(&ctr_drbg);
mbed_official 78:5f419e0f96dd 833
mbed_official 78:5f419e0f96dd 834 return ret;
mbed_official 78:5f419e0f96dd 835 }
mbed_official 78:5f419e0f96dd 836 #endif /* MBEDTLS_CTR_DRBG_C */
Janos Follath 0:1f7c5025e59d 837
Janos Follath 0:1f7c5025e59d 838 #if defined(MBEDTLS_HMAC_DRBG_C)
mbed_official 78:5f419e0f96dd 839 MBED_NOINLINE static int benchmark_hmac_drbg()
mbed_official 78:5f419e0f96dd 840 {
mbed_official 78:5f419e0f96dd 841 int ret = 0;
mbed_official 78:5f419e0f96dd 842 mbedtls_hmac_drbg_context hmac_drbg;
mbed_official 78:5f419e0f96dd 843 const mbedtls_md_info_t *md_info;
Janos Follath 0:1f7c5025e59d 844
mbed_official 78:5f419e0f96dd 845 mbedtls_hmac_drbg_init(&hmac_drbg);
Janos Follath 0:1f7c5025e59d 846
Janos Follath 0:1f7c5025e59d 847 #if defined(MBEDTLS_SHA1_C)
mbed_official 78:5f419e0f96dd 848 md_info = mbedtls_md_info_from_type(MBEDTLS_MD_SHA1);
mbed_official 78:5f419e0f96dd 849 if (md_info == NULL) {
mbed_official 78:5f419e0f96dd 850 mbedtls_printf("mbedtls_md_info_from_type() returned NULL\n");
mbed_official 78:5f419e0f96dd 851 ret = -1;
mbed_official 78:5f419e0f96dd 852 goto exit;
mbed_official 78:5f419e0f96dd 853 }
Janos Follath 0:1f7c5025e59d 854
mbed_official 78:5f419e0f96dd 855 ret = mbedtls_hmac_drbg_seed(&hmac_drbg, md_info, myrand, NULL, NULL, 0);
mbed_official 78:5f419e0f96dd 856 if (ret != 0) {
mbed_official 78:5f419e0f96dd 857 PRINT_ERROR(ret, "mbedtls_hmac_drbg_seed()");
mbed_official 78:5f419e0f96dd 858 goto exit;
mbed_official 78:5f419e0f96dd 859 }
mbed_official 78:5f419e0f96dd 860 BENCHMARK_FUNC_CALL("HMAC_DRBG SHA-1 (NOPR)",
mbed_official 78:5f419e0f96dd 861 mbedtls_hmac_drbg_random(&hmac_drbg, buf, BUFSIZE));
mbed_official 78:5f419e0f96dd 862
mbed_official 78:5f419e0f96dd 863 ret = mbedtls_hmac_drbg_seed(&hmac_drbg, md_info, myrand, NULL, NULL, 0);
mbed_official 78:5f419e0f96dd 864 if (ret != 0) {
mbed_official 78:5f419e0f96dd 865 PRINT_ERROR(ret, "mbedtls_hmac_drbg_seed()");
mbed_official 78:5f419e0f96dd 866 goto exit;
mbed_official 78:5f419e0f96dd 867 }
mbed_official 78:5f419e0f96dd 868 mbedtls_hmac_drbg_set_prediction_resistance(&hmac_drbg,
mbed_official 78:5f419e0f96dd 869 MBEDTLS_HMAC_DRBG_PR_ON);
mbed_official 78:5f419e0f96dd 870 BENCHMARK_FUNC_CALL("HMAC_DRBG SHA-1 (PR)",
mbed_official 78:5f419e0f96dd 871 mbedtls_hmac_drbg_random(&hmac_drbg, buf, BUFSIZE));
mbed_official 78:5f419e0f96dd 872 #endif /* MBEDTLS_SHA1_C */
Janos Follath 0:1f7c5025e59d 873
Janos Follath 0:1f7c5025e59d 874 #if defined(MBEDTLS_SHA256_C)
mbed_official 78:5f419e0f96dd 875 md_info = mbedtls_md_info_from_type(MBEDTLS_MD_SHA256);
mbed_official 78:5f419e0f96dd 876 if (md_info == NULL) {
mbed_official 78:5f419e0f96dd 877 PRINT_ERROR(ret, "mbedtls_md_info_from_type()");
mbed_official 78:5f419e0f96dd 878 goto exit;
mbed_official 78:5f419e0f96dd 879 }
Janos Follath 0:1f7c5025e59d 880
mbed_official 78:5f419e0f96dd 881 ret = mbedtls_hmac_drbg_seed(&hmac_drbg, md_info, myrand, NULL, NULL, 0);
mbed_official 78:5f419e0f96dd 882 if (ret != 0) {
mbed_official 78:5f419e0f96dd 883 PRINT_ERROR(ret, "mbedtls_hmac_drbg_seed()");
mbed_official 78:5f419e0f96dd 884 goto exit;
mbed_official 78:5f419e0f96dd 885 }
mbed_official 78:5f419e0f96dd 886 BENCHMARK_FUNC_CALL("HMAC_DRBG SHA-256 (NOPR)",
mbed_official 78:5f419e0f96dd 887 mbedtls_hmac_drbg_random(&hmac_drbg, buf, BUFSIZE));
Janos Follath 0:1f7c5025e59d 888
mbed_official 78:5f419e0f96dd 889 ret = mbedtls_hmac_drbg_seed(&hmac_drbg, md_info, myrand, NULL, NULL, 0);
mbed_official 78:5f419e0f96dd 890 if (ret != 0) {
mbed_official 78:5f419e0f96dd 891 PRINT_ERROR(ret, "mbedtls_hmac_drbg_seed()");
mbed_official 78:5f419e0f96dd 892 goto exit;
Janos Follath 0:1f7c5025e59d 893 }
mbed_official 78:5f419e0f96dd 894 mbedtls_hmac_drbg_set_prediction_resistance(&hmac_drbg,
mbed_official 78:5f419e0f96dd 895 MBEDTLS_HMAC_DRBG_PR_ON);
mbed_official 78:5f419e0f96dd 896 BENCHMARK_FUNC_CALL("HMAC_DRBG SHA-256 (PR)",
mbed_official 78:5f419e0f96dd 897 mbedtls_hmac_drbg_random(&hmac_drbg, buf, BUFSIZE));
mbed_official 78:5f419e0f96dd 898 #endif /* MBEDTLS_SHA256_C */
mbed_official 70:72c865037f5d 899
mbed_official 78:5f419e0f96dd 900 ret = 0;
mbed_official 78:5f419e0f96dd 901
mbed_official 78:5f419e0f96dd 902 exit:
mbed_official 78:5f419e0f96dd 903 mbedtls_hmac_drbg_free(&hmac_drbg);
mbed_official 78:5f419e0f96dd 904
mbed_official 78:5f419e0f96dd 905 return ret;
mbed_official 78:5f419e0f96dd 906 }
mbed_official 78:5f419e0f96dd 907 #endif /* MBEDTLS_HMAC_DRBG_C */
Janos Follath 0:1f7c5025e59d 908
Janos Follath 0:1f7c5025e59d 909 #if defined(MBEDTLS_RSA_C) && \
Janos Follath 0:1f7c5025e59d 910 defined(MBEDTLS_PEM_PARSE_C) && defined(MBEDTLS_PK_PARSE_C)
mbed_official 78:5f419e0f96dd 911 MBED_NOINLINE static int benchmark_rsa()
mbed_official 78:5f419e0f96dd 912 {
mbed_official 78:5f419e0f96dd 913 int ret = 0;
mbed_official 78:5f419e0f96dd 914 mbedtls_pk_context pk;
mbed_official 78:5f419e0f96dd 915 mbedtls_rsa_context *rsa;
mbed_official 78:5f419e0f96dd 916 const char *rsa_keys[] = {
mbed_official 78:5f419e0f96dd 917 RSA_PRIVATE_KEY_2048,
mbed_official 78:5f419e0f96dd 918 };
mbed_official 78:5f419e0f96dd 919 size_t i;
Janos Follath 0:1f7c5025e59d 920
mbed_official 78:5f419e0f96dd 921 for (i = 0; i < sizeof(rsa_keys) / sizeof(rsa_keys[0]) && ret == 0; i++) {
mbed_official 78:5f419e0f96dd 922 mbedtls_pk_init(&pk);
mbed_official 78:5f419e0f96dd 923
mbed_official 78:5f419e0f96dd 924 ret = mbedtls_pk_parse_key(&pk, (const unsigned char *)rsa_keys[i],
mbed_official 78:5f419e0f96dd 925 strlen(rsa_keys[i]) + 1, NULL, 0);
mbed_official 89:2d5255b9d6c5 926 if (ret == MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED) {
mbed_official 89:2d5255b9d6c5 927 /* Do not consider this as a failure */
mbed_official 89:2d5255b9d6c5 928 mbedtls_printf(HEADER_FORMAT "Feature unsupported\n", title);
mbed_official 89:2d5255b9d6c5 929 ret = 0;
mbed_official 89:2d5255b9d6c5 930 continue;
mbed_official 89:2d5255b9d6c5 931 } else if (ret != 0) {
mbed_official 78:5f419e0f96dd 932 PRINT_ERROR(ret, "mbedtls_pk_parse_key()");
mbed_official 78:5f419e0f96dd 933 goto exit;
mbed_official 78:5f419e0f96dd 934 }
mbed_official 78:5f419e0f96dd 935
mbed_official 78:5f419e0f96dd 936 rsa = mbedtls_pk_rsa(pk);
Janos Follath 0:1f7c5025e59d 937
mbed_official 78:5f419e0f96dd 938 ret = mbedtls_snprintf(title, sizeof(title), "RSA-%d",
mbed_official 78:5f419e0f96dd 939 mbedtls_pk_get_bitlen(&pk));
mbed_official 78:5f419e0f96dd 940 if (ret < 0 || static_cast<size_t>(ret) >= sizeof(title)) {
mbed_official 78:5f419e0f96dd 941 mbedtls_printf("Failed to compose title string using "
mbed_official 78:5f419e0f96dd 942 "mbedtls_snprintf(): %d\n", ret);
mbed_official 78:5f419e0f96dd 943 goto exit;
mbed_official 78:5f419e0f96dd 944 }
Janos Follath 0:1f7c5025e59d 945
mbed_official 78:5f419e0f96dd 946 BENCHMARK_PUBLIC(title, " public",
mbed_official 78:5f419e0f96dd 947 buf[0] = 0;
mbed_official 78:5f419e0f96dd 948 ret = mbedtls_rsa_public(rsa, buf, buf));
Janos Follath 0:1f7c5025e59d 949
mbed_official 78:5f419e0f96dd 950 BENCHMARK_PUBLIC(title, "private",
mbed_official 78:5f419e0f96dd 951 buf[0] = 0;
mbed_official 78:5f419e0f96dd 952 ret = mbedtls_rsa_private(rsa, myrand, NULL, buf,
mbed_official 78:5f419e0f96dd 953 buf));
Janos Follath 0:1f7c5025e59d 954
mbed_official 78:5f419e0f96dd 955 exit:
mbed_official 78:5f419e0f96dd 956 mbedtls_pk_free(&pk);
Janos Follath 0:1f7c5025e59d 957 }
mbed_official 78:5f419e0f96dd 958
mbed_official 78:5f419e0f96dd 959 return ret;
mbed_official 78:5f419e0f96dd 960 }
mbed_official 78:5f419e0f96dd 961 #endif /* MBEDTLS_RSA_C && MBEDTLS_PEM_PARSE_C && MBEDTLS_PK_PARSE_C */
Janos Follath 0:1f7c5025e59d 962
Janos Follath 0:1f7c5025e59d 963 #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_BIGNUM_C)
mbed_official 78:5f419e0f96dd 964 MBED_NOINLINE static int benchmark_dhm()
mbed_official 78:5f419e0f96dd 965 {
mbed_official 78:5f419e0f96dd 966 int ret = 0;
mbed_official 78:5f419e0f96dd 967 int dhm_sizes[] = {
mbed_official 78:5f419e0f96dd 968 2048,
mbed_official 78:5f419e0f96dd 969 3072,
mbed_official 78:5f419e0f96dd 970 };
mbed_official 78:5f419e0f96dd 971 const char *dhm_P[] = {
mbed_official 78:5f419e0f96dd 972 MBEDTLS_DHM_RFC3526_MODP_2048_P,
mbed_official 78:5f419e0f96dd 973 MBEDTLS_DHM_RFC3526_MODP_3072_P,
mbed_official 78:5f419e0f96dd 974 };
mbed_official 78:5f419e0f96dd 975 const char *dhm_G[] = {
mbed_official 78:5f419e0f96dd 976 MBEDTLS_DHM_RFC3526_MODP_2048_G,
mbed_official 78:5f419e0f96dd 977 MBEDTLS_DHM_RFC3526_MODP_3072_G,
mbed_official 78:5f419e0f96dd 978 };
Janos Follath 0:1f7c5025e59d 979
mbed_official 78:5f419e0f96dd 980 mbedtls_dhm_context dhm;
mbed_official 78:5f419e0f96dd 981 size_t olen;
mbed_official 78:5f419e0f96dd 982 size_t i;
mbed_official 78:5f419e0f96dd 983
mbed_official 78:5f419e0f96dd 984 for (i = 0;
mbed_official 78:5f419e0f96dd 985 i < sizeof(dhm_sizes) / sizeof(dhm_sizes[0]) && ret == 0;
mbed_official 78:5f419e0f96dd 986 i++) {
mbed_official 78:5f419e0f96dd 987 mbedtls_dhm_init(&dhm);
Janos Follath 0:1f7c5025e59d 988
mbed_official 78:5f419e0f96dd 989 ret = mbedtls_mpi_read_string(&dhm.P, 16, dhm_P[i]);
mbed_official 78:5f419e0f96dd 990 if (ret != 0) {
mbed_official 78:5f419e0f96dd 991 PRINT_ERROR(ret, "mbedtls_mpi_read_string()");
mbed_official 78:5f419e0f96dd 992 goto exit;
mbed_official 78:5f419e0f96dd 993 }
mbed_official 78:5f419e0f96dd 994 ret = mbedtls_mpi_read_string(&dhm.G, 16, dhm_G[i]);
mbed_official 78:5f419e0f96dd 995 if (ret != 0) {
mbed_official 78:5f419e0f96dd 996 PRINT_ERROR(ret, "mbedtls_mpi_read_string()");
mbed_official 78:5f419e0f96dd 997 goto exit;
mbed_official 78:5f419e0f96dd 998 }
Janos Follath 0:1f7c5025e59d 999
mbed_official 78:5f419e0f96dd 1000 dhm.len = mbedtls_mpi_size(&dhm.P);
mbed_official 78:5f419e0f96dd 1001 ret = mbedtls_dhm_make_public(&dhm, (int) dhm.len, buf, dhm.len,
mbed_official 78:5f419e0f96dd 1002 myrand, NULL);
mbed_official 89:2d5255b9d6c5 1003 if (ret == MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED) {
mbed_official 89:2d5255b9d6c5 1004 /* Do not consider this as a failure */
mbed_official 89:2d5255b9d6c5 1005 mbedtls_printf(HEADER_FORMAT "Feature unsupported\n", title);
mbed_official 89:2d5255b9d6c5 1006 ret = 0;
mbed_official 89:2d5255b9d6c5 1007 continue;
mbed_official 89:2d5255b9d6c5 1008 } else if (ret != 0) {
mbed_official 78:5f419e0f96dd 1009 PRINT_ERROR(ret, "mbedtls_dhm_make_public()");
mbed_official 78:5f419e0f96dd 1010 goto exit;
Janos Follath 0:1f7c5025e59d 1011 }
Janos Follath 0:1f7c5025e59d 1012
mbed_official 78:5f419e0f96dd 1013 ret = mbedtls_mpi_copy(&dhm.GY, &dhm.GX);
mbed_official 78:5f419e0f96dd 1014 if (ret != 0) {
mbed_official 78:5f419e0f96dd 1015 PRINT_ERROR(ret, "mbedtls_mpi_copy()");
mbed_official 78:5f419e0f96dd 1016 goto exit;
mbed_official 78:5f419e0f96dd 1017 }
Janos Follath 0:1f7c5025e59d 1018
mbed_official 78:5f419e0f96dd 1019 ret = mbedtls_snprintf(title, sizeof(title), "DHE-%d", dhm_sizes[i]);
mbed_official 78:5f419e0f96dd 1020 if (ret < 0 || static_cast<size_t>(ret) >= sizeof(title)) {
mbed_official 78:5f419e0f96dd 1021 mbedtls_printf("Failed to compose title string using "
mbed_official 78:5f419e0f96dd 1022 "mbedtls_snprintf(): %d\n", ret);
mbed_official 78:5f419e0f96dd 1023 goto exit;
mbed_official 78:5f419e0f96dd 1024 }
Janos Follath 0:1f7c5025e59d 1025
mbed_official 78:5f419e0f96dd 1026 /*
mbed_official 78:5f419e0f96dd 1027 * Benchmarking this requires two function calls that can fail. We
mbed_official 78:5f419e0f96dd 1028 * add a check in between them to check for any errors. In normal
mbed_official 78:5f419e0f96dd 1029 * operation, the overhead of this check is negligible
mbed_official 78:5f419e0f96dd 1030 */
mbed_official 78:5f419e0f96dd 1031 BENCHMARK_PUBLIC(title, "handshake",
mbed_official 78:5f419e0f96dd 1032 ret = mbedtls_dhm_make_public(&dhm, (int)dhm.len,
mbed_official 78:5f419e0f96dd 1033 buf, dhm.len, myrand,
mbed_official 78:5f419e0f96dd 1034 NULL);
mbed_official 78:5f419e0f96dd 1035 if (ret != 0) {
mbed_official 78:5f419e0f96dd 1036 PRINT_ERROR(ret, "mbedtls_dhm_make_public()");
mbed_official 78:5f419e0f96dd 1037 goto exit;
mbed_official 78:5f419e0f96dd 1038 }
mbed_official 78:5f419e0f96dd 1039 ret = mbedtls_dhm_calc_secret(&dhm, buf, sizeof(buf),
mbed_official 78:5f419e0f96dd 1040 &olen, myrand, NULL));
Janos Follath 0:1f7c5025e59d 1041
mbed_official 78:5f419e0f96dd 1042 ret = mbedtls_snprintf(title, sizeof(title), "DH-%d", dhm_sizes[i]);
mbed_official 78:5f419e0f96dd 1043 if (ret < 0 || static_cast<size_t>(ret) >= sizeof(title)) {
mbed_official 78:5f419e0f96dd 1044 mbedtls_printf("Failed to compose title string using "
mbed_official 78:5f419e0f96dd 1045 "mbedtls_snprintf(): %d\n", ret);
mbed_official 78:5f419e0f96dd 1046 goto exit;
Janos Follath 0:1f7c5025e59d 1047 }
Janos Follath 0:1f7c5025e59d 1048
mbed_official 78:5f419e0f96dd 1049 BENCHMARK_PUBLIC(title, "handshake",
mbed_official 78:5f419e0f96dd 1050 ret = mbedtls_dhm_calc_secret(&dhm, buf, sizeof(buf),
mbed_official 78:5f419e0f96dd 1051 &olen, myrand, NULL));
mbed_official 78:5f419e0f96dd 1052
mbed_official 78:5f419e0f96dd 1053 exit:
mbed_official 78:5f419e0f96dd 1054 mbedtls_dhm_free(&dhm);
mbed_official 78:5f419e0f96dd 1055 }
mbed_official 78:5f419e0f96dd 1056
mbed_official 78:5f419e0f96dd 1057 return ret;
mbed_official 78:5f419e0f96dd 1058 }
mbed_official 78:5f419e0f96dd 1059 #endif /* MBEDTLS_DHM_C && MBEDTLS_BIGNUM_C */
Janos Follath 0:1f7c5025e59d 1060
mbed_official 78:5f419e0f96dd 1061 #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA256_C)
mbed_official 78:5f419e0f96dd 1062 MBED_NOINLINE static int benchmark_ecdsa()
mbed_official 78:5f419e0f96dd 1063 {
mbed_official 78:5f419e0f96dd 1064 int ret = 0;
mbed_official 78:5f419e0f96dd 1065 mbedtls_ecdsa_context ecdsa;
mbed_official 78:5f419e0f96dd 1066 const mbedtls_ecp_curve_info *curve_info;
mbed_official 78:5f419e0f96dd 1067 size_t sig_len;
mbed_official 78:5f419e0f96dd 1068 size_t hash_len;
mbed_official 78:5f419e0f96dd 1069
mbed_official 78:5f419e0f96dd 1070 memset(buf, 0x2A, sizeof(buf));
Janos Follath 0:1f7c5025e59d 1071
mbed_official 78:5f419e0f96dd 1072 for (curve_info = mbedtls_ecp_curve_list();
mbed_official 78:5f419e0f96dd 1073 curve_info->grp_id != MBEDTLS_ECP_DP_NONE && ret == 0;
mbed_official 78:5f419e0f96dd 1074 curve_info++) {
mbed_official 78:5f419e0f96dd 1075 mbedtls_ecdsa_init(&ecdsa);
Janos Follath 0:1f7c5025e59d 1076
mbed_official 78:5f419e0f96dd 1077 ret = mbedtls_snprintf(title, sizeof(title), "ECDSA-%s",
mbed_official 78:5f419e0f96dd 1078 curve_info->name);
mbed_official 78:5f419e0f96dd 1079 if (ret < 0 || static_cast<size_t>(ret) >= sizeof(title)) {
mbed_official 78:5f419e0f96dd 1080 mbedtls_printf("Failed to compose title string using "
mbed_official 78:5f419e0f96dd 1081 "mbedtls_snprintf(): %d\n", ret);
mbed_official 78:5f419e0f96dd 1082 goto exit;
mbed_official 78:5f419e0f96dd 1083 }
Janos Follath 0:1f7c5025e59d 1084
mbed_official 89:2d5255b9d6c5 1085 ret = mbedtls_ecdsa_genkey(&ecdsa, curve_info->grp_id, myrand, NULL);
mbed_official 89:2d5255b9d6c5 1086 if (ret == MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED) {
mbed_official 89:2d5255b9d6c5 1087 /* Do not consider this as a failure */
mbed_official 89:2d5255b9d6c5 1088 mbedtls_printf(HEADER_FORMAT "Feature unsupported\n", title);
mbed_official 89:2d5255b9d6c5 1089 ret = 0;
mbed_official 89:2d5255b9d6c5 1090 continue;
mbed_official 89:2d5255b9d6c5 1091 } else if (ret != 0) {
mbed_official 89:2d5255b9d6c5 1092 PRINT_ERROR(ret, "mbedtls_ecdsa_genkey()");
mbed_official 89:2d5255b9d6c5 1093 goto exit;
mbed_official 89:2d5255b9d6c5 1094 }
mbed_official 89:2d5255b9d6c5 1095
mbed_official 89:2d5255b9d6c5 1096 ecp_clear_precomputed(&ecdsa.grp);
mbed_official 89:2d5255b9d6c5 1097
mbed_official 78:5f419e0f96dd 1098 hash_len = (curve_info->bit_size + 7) / 8;
mbed_official 78:5f419e0f96dd 1099 BENCHMARK_PUBLIC(title, "sign",
mbed_official 78:5f419e0f96dd 1100 ret = mbedtls_ecdsa_write_signature(&ecdsa,
mbed_official 78:5f419e0f96dd 1101 MBEDTLS_MD_SHA256,
mbed_official 78:5f419e0f96dd 1102 buf, hash_len,
mbed_official 78:5f419e0f96dd 1103 tmp, &sig_len,
mbed_official 78:5f419e0f96dd 1104 myrand, NULL));
mbed_official 78:5f419e0f96dd 1105
mbed_official 78:5f419e0f96dd 1106 mbedtls_ecdsa_free(&ecdsa);
mbed_official 78:5f419e0f96dd 1107 }
mbed_official 78:5f419e0f96dd 1108
mbed_official 78:5f419e0f96dd 1109 for (curve_info = mbedtls_ecp_curve_list();
mbed_official 78:5f419e0f96dd 1110 curve_info->grp_id != MBEDTLS_ECP_DP_NONE && ret == 0;
mbed_official 78:5f419e0f96dd 1111 curve_info++) {
mbed_official 78:5f419e0f96dd 1112 mbedtls_ecdsa_init(&ecdsa);
Janos Follath 0:1f7c5025e59d 1113
mbed_official 78:5f419e0f96dd 1114 ret = mbedtls_ecdsa_genkey(&ecdsa, curve_info->grp_id, myrand, NULL);
mbed_official 89:2d5255b9d6c5 1115 if (ret == MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED) {
mbed_official 89:2d5255b9d6c5 1116 /* Do not consider this as a failure */
mbed_official 89:2d5255b9d6c5 1117 mbedtls_printf(HEADER_FORMAT "Feature unsupported\n", title);
mbed_official 89:2d5255b9d6c5 1118 ret = 0;
mbed_official 89:2d5255b9d6c5 1119 continue;
mbed_official 89:2d5255b9d6c5 1120 } else if (ret != 0) {
mbed_official 78:5f419e0f96dd 1121 PRINT_ERROR(ret, "mbedtls_ecdsa_genkey()");
mbed_official 78:5f419e0f96dd 1122 goto exit;
mbed_official 78:5f419e0f96dd 1123 }
Janos Follath 0:1f7c5025e59d 1124
mbed_official 78:5f419e0f96dd 1125 hash_len = (curve_info->bit_size + 7) / 8;
mbed_official 78:5f419e0f96dd 1126 ret = mbedtls_ecdsa_write_signature(&ecdsa, MBEDTLS_MD_SHA256, buf,
mbed_official 78:5f419e0f96dd 1127 hash_len, tmp, &sig_len, myrand,
mbed_official 78:5f419e0f96dd 1128 NULL);
mbed_official 89:2d5255b9d6c5 1129 if (ret == MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED) {
mbed_official 89:2d5255b9d6c5 1130 /* Do not consider this as a failure */
mbed_official 89:2d5255b9d6c5 1131 mbedtls_printf(HEADER_FORMAT "Feature unsupported\n", title);
mbed_official 89:2d5255b9d6c5 1132 ret = 0;
mbed_official 89:2d5255b9d6c5 1133 continue;
mbed_official 89:2d5255b9d6c5 1134 } else if (ret != 0) {
mbed_official 78:5f419e0f96dd 1135 PRINT_ERROR(ret, "mbedtls_ecdsa_write_signature()");
mbed_official 78:5f419e0f96dd 1136 goto exit;
mbed_official 78:5f419e0f96dd 1137 }
mbed_official 78:5f419e0f96dd 1138
mbed_official 78:5f419e0f96dd 1139 ecp_clear_precomputed(&ecdsa.grp);
mbed_official 78:5f419e0f96dd 1140
mbed_official 78:5f419e0f96dd 1141 ret = mbedtls_snprintf(title, sizeof(title), "ECDSA-%s",
mbed_official 78:5f419e0f96dd 1142 curve_info->name);
mbed_official 78:5f419e0f96dd 1143 if (ret < 0 || static_cast<size_t>(ret) >= sizeof(title)) {
mbed_official 78:5f419e0f96dd 1144 mbedtls_printf("Failed to compose title string using "
mbed_official 78:5f419e0f96dd 1145 "mbedtls_snprintf(): %d\n", ret);
mbed_official 78:5f419e0f96dd 1146 goto exit;
Janos Follath 0:1f7c5025e59d 1147 }
Janos Follath 0:1f7c5025e59d 1148
mbed_official 78:5f419e0f96dd 1149 BENCHMARK_PUBLIC(title, "verify",
mbed_official 78:5f419e0f96dd 1150 ret = mbedtls_ecdsa_read_signature(&ecdsa, buf,
mbed_official 78:5f419e0f96dd 1151 hash_len, tmp,
mbed_official 78:5f419e0f96dd 1152 sig_len));
mbed_official 78:5f419e0f96dd 1153
mbed_official 78:5f419e0f96dd 1154 exit:
mbed_official 78:5f419e0f96dd 1155 mbedtls_ecdsa_free(&ecdsa);
mbed_official 78:5f419e0f96dd 1156 }
mbed_official 78:5f419e0f96dd 1157
mbed_official 78:5f419e0f96dd 1158 return ret;
mbed_official 78:5f419e0f96dd 1159 }
mbed_official 78:5f419e0f96dd 1160 #endif /* MBEDTLS_ECDSA_C && MBEDTLS_SHA2565_C */
mbed_official 78:5f419e0f96dd 1161
mbed_official 78:5f419e0f96dd 1162 #if defined(MBEDTLS_ECDH_C)
mbed_official 78:5f419e0f96dd 1163 MBED_NOINLINE static int benchmark_ecdh()
mbed_official 78:5f419e0f96dd 1164 {
mbed_official 78:5f419e0f96dd 1165 int ret = 0;
mbed_official 78:5f419e0f96dd 1166 mbedtls_ecdh_context ecdh;
mbed_official 78:5f419e0f96dd 1167 const mbedtls_ecp_curve_info *curve_info;
mbed_official 78:5f419e0f96dd 1168 size_t olen;
Janos Follath 0:1f7c5025e59d 1169
mbed_official 78:5f419e0f96dd 1170 for (curve_info = mbedtls_ecp_curve_list();
mbed_official 78:5f419e0f96dd 1171 curve_info->grp_id != MBEDTLS_ECP_DP_NONE && ret == 0;
mbed_official 78:5f419e0f96dd 1172 curve_info++) {
mbed_official 78:5f419e0f96dd 1173 mbedtls_ecdh_init(&ecdh);
mbed_official 78:5f419e0f96dd 1174
mbed_official 78:5f419e0f96dd 1175 ret = mbedtls_ecp_group_load(&ecdh.grp, curve_info->grp_id);
mbed_official 89:2d5255b9d6c5 1176 if (ret == MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED) {
mbed_official 89:2d5255b9d6c5 1177 /* Do not consider this as a failure */
mbed_official 89:2d5255b9d6c5 1178 mbedtls_printf(HEADER_FORMAT "Feature unsupported\n", title);
mbed_official 89:2d5255b9d6c5 1179 ret = 0;
mbed_official 89:2d5255b9d6c5 1180 continue;
mbed_official 89:2d5255b9d6c5 1181 } else if (ret != 0) {
mbed_official 78:5f419e0f96dd 1182 PRINT_ERROR(ret, "mbedtls_ecp_group_load()");
mbed_official 78:5f419e0f96dd 1183 goto exit;
mbed_official 78:5f419e0f96dd 1184 }
mbed_official 78:5f419e0f96dd 1185
mbed_official 78:5f419e0f96dd 1186 ret = mbedtls_snprintf(title, sizeof(title), "ECDHE-%s",
mbed_official 78:5f419e0f96dd 1187 curve_info->name);
mbed_official 78:5f419e0f96dd 1188 if (ret < 0 || static_cast<size_t>(ret) >= sizeof(title)) {
mbed_official 78:5f419e0f96dd 1189 mbedtls_printf("Failed to compose title string using "
mbed_official 78:5f419e0f96dd 1190 "mbedtls_snprintf(): %d\n", ret);
mbed_official 78:5f419e0f96dd 1191 goto exit;
Janos Follath 0:1f7c5025e59d 1192 }
Janos Follath 0:1f7c5025e59d 1193
mbed_official 89:2d5255b9d6c5 1194 ret = mbedtls_ecdh_make_public(&ecdh, &olen, buf, sizeof(buf),
mbed_official 89:2d5255b9d6c5 1195 myrand, NULL);
mbed_official 89:2d5255b9d6c5 1196 if (ret == MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED) {
mbed_official 89:2d5255b9d6c5 1197 /* Do not consider this as a failure */
mbed_official 89:2d5255b9d6c5 1198 mbedtls_printf(HEADER_FORMAT "Feature unsupported\n", title);
mbed_official 89:2d5255b9d6c5 1199 ret = 0;
mbed_official 89:2d5255b9d6c5 1200 continue;
mbed_official 89:2d5255b9d6c5 1201 } else if (ret != 0) {
mbed_official 89:2d5255b9d6c5 1202 PRINT_ERROR(ret, "mbedtls_ecdh_make_public()");
mbed_official 89:2d5255b9d6c5 1203 goto exit;
mbed_official 89:2d5255b9d6c5 1204 }
mbed_official 89:2d5255b9d6c5 1205
mbed_official 89:2d5255b9d6c5 1206 ret = mbedtls_ecp_copy(&ecdh.Qp, &ecdh.Q);
mbed_official 89:2d5255b9d6c5 1207 if (ret == MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED) {
mbed_official 89:2d5255b9d6c5 1208 /* Do not consider this as a failure */
mbed_official 89:2d5255b9d6c5 1209 mbedtls_printf(HEADER_FORMAT "Feature unsupported\n", title);
mbed_official 89:2d5255b9d6c5 1210 ret = 0;
mbed_official 89:2d5255b9d6c5 1211 continue;
mbed_official 89:2d5255b9d6c5 1212 } else if (ret != 0) {
mbed_official 89:2d5255b9d6c5 1213 PRINT_ERROR(ret, "mbedtls_ecp_copy()");
mbed_official 89:2d5255b9d6c5 1214 goto exit;
mbed_official 89:2d5255b9d6c5 1215 }
mbed_official 89:2d5255b9d6c5 1216
mbed_official 89:2d5255b9d6c5 1217 ecp_clear_precomputed(&ecdh.grp);
mbed_official 89:2d5255b9d6c5 1218
mbed_official 78:5f419e0f96dd 1219 /*
mbed_official 78:5f419e0f96dd 1220 * Benchmarking this requires two function calls that can fail. We
mbed_official 78:5f419e0f96dd 1221 * add a check in between them to check for any errors. In normal
mbed_official 78:5f419e0f96dd 1222 * operation, the overhead of this check is negligible
mbed_official 78:5f419e0f96dd 1223 */
mbed_official 78:5f419e0f96dd 1224 BENCHMARK_PUBLIC(title, "handshake",
mbed_official 78:5f419e0f96dd 1225 ret = mbedtls_ecdh_make_public(&ecdh, &olen, buf,
mbed_official 78:5f419e0f96dd 1226 sizeof(buf), myrand,
mbed_official 78:5f419e0f96dd 1227 NULL);
mbed_official 78:5f419e0f96dd 1228 if (ret != 0) {
mbed_official 78:5f419e0f96dd 1229 PRINT_ERROR(ret, "mbedtls_ecdh_make_public()");
mbed_official 78:5f419e0f96dd 1230 goto exit;
mbed_official 78:5f419e0f96dd 1231 }
mbed_official 78:5f419e0f96dd 1232 ret = mbedtls_ecdh_calc_secret(&ecdh, &olen, buf,
mbed_official 78:5f419e0f96dd 1233 sizeof(buf), myrand,
mbed_official 78:5f419e0f96dd 1234 NULL));
mbed_official 78:5f419e0f96dd 1235 mbedtls_ecdh_free(&ecdh);
mbed_official 78:5f419e0f96dd 1236 }
mbed_official 78:5f419e0f96dd 1237
mbed_official 78:5f419e0f96dd 1238 for (curve_info = mbedtls_ecp_curve_list();
mbed_official 78:5f419e0f96dd 1239 curve_info->grp_id != MBEDTLS_ECP_DP_NONE && ret == 0;
mbed_official 78:5f419e0f96dd 1240 curve_info++) {
mbed_official 78:5f419e0f96dd 1241 mbedtls_ecdh_init(&ecdh);
Janos Follath 0:1f7c5025e59d 1242
mbed_official 78:5f419e0f96dd 1243 ret = mbedtls_ecp_group_load(&ecdh.grp, curve_info->grp_id);
mbed_official 89:2d5255b9d6c5 1244 if (ret == MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED) {
mbed_official 89:2d5255b9d6c5 1245 /* Do not consider this as a failure */
mbed_official 89:2d5255b9d6c5 1246 mbedtls_printf(HEADER_FORMAT "Feature unsupported\n", title);
mbed_official 89:2d5255b9d6c5 1247 ret = 0;
mbed_official 89:2d5255b9d6c5 1248 continue;
mbed_official 89:2d5255b9d6c5 1249 } else if (ret != 0) {
mbed_official 78:5f419e0f96dd 1250 PRINT_ERROR(ret, "mbedtls_ecp_group_load()");
mbed_official 78:5f419e0f96dd 1251 goto exit;
mbed_official 78:5f419e0f96dd 1252 }
mbed_official 78:5f419e0f96dd 1253
mbed_official 89:2d5255b9d6c5 1254 ret = mbedtls_snprintf(title, sizeof(title), "ECDH-%s",
mbed_official 89:2d5255b9d6c5 1255 curve_info->name);
mbed_official 89:2d5255b9d6c5 1256 if (ret < 0 || static_cast<size_t>(ret) >= sizeof(title)) {
mbed_official 89:2d5255b9d6c5 1257 mbedtls_printf("Failed to compose title string using "
mbed_official 89:2d5255b9d6c5 1258 "mbedtls_snprintf(): %d\n", ret);
mbed_official 89:2d5255b9d6c5 1259 goto exit;
mbed_official 89:2d5255b9d6c5 1260 }
mbed_official 89:2d5255b9d6c5 1261
mbed_official 78:5f419e0f96dd 1262 ret = mbedtls_ecdh_make_public(&ecdh, &olen, buf, sizeof(buf), myrand,
mbed_official 78:5f419e0f96dd 1263 NULL);
mbed_official 89:2d5255b9d6c5 1264 if (ret == MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED) {
mbed_official 89:2d5255b9d6c5 1265 /* Do not consider this as a failure */
mbed_official 89:2d5255b9d6c5 1266 mbedtls_printf(HEADER_FORMAT "Feature unsupported\n", title);
mbed_official 89:2d5255b9d6c5 1267 ret = 0;
mbed_official 89:2d5255b9d6c5 1268 continue;
mbed_official 89:2d5255b9d6c5 1269 } else if (ret != 0) {
mbed_official 78:5f419e0f96dd 1270 PRINT_ERROR(ret, "mbedtls_ecdh_make_public()");
mbed_official 78:5f419e0f96dd 1271 goto exit;
mbed_official 78:5f419e0f96dd 1272 }
mbed_official 78:5f419e0f96dd 1273
mbed_official 78:5f419e0f96dd 1274 ret = mbedtls_ecp_copy(&ecdh.Qp, &ecdh.Q);
mbed_official 89:2d5255b9d6c5 1275 if (ret == MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED) {
mbed_official 89:2d5255b9d6c5 1276 /* Do not consider this as a failure */
mbed_official 89:2d5255b9d6c5 1277 mbedtls_printf(HEADER_FORMAT "Feature unsupported\n", title);
mbed_official 89:2d5255b9d6c5 1278 ret = 0;
mbed_official 89:2d5255b9d6c5 1279 continue;
mbed_official 89:2d5255b9d6c5 1280 } else if (ret != 0) {
mbed_official 78:5f419e0f96dd 1281 PRINT_ERROR(ret, "mbedtls_ecp_copy()");
mbed_official 78:5f419e0f96dd 1282 goto exit;
mbed_official 78:5f419e0f96dd 1283 }
mbed_official 78:5f419e0f96dd 1284
mbed_official 78:5f419e0f96dd 1285 ret = mbedtls_ecdh_make_public(&ecdh, &olen, buf, sizeof(buf), myrand,
mbed_official 78:5f419e0f96dd 1286 NULL);
mbed_official 89:2d5255b9d6c5 1287 if (ret == MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED) {
mbed_official 89:2d5255b9d6c5 1288 /* Do not consider this as a failure */
mbed_official 89:2d5255b9d6c5 1289 mbedtls_printf(HEADER_FORMAT "Feature unsupported\n", title);
mbed_official 89:2d5255b9d6c5 1290 ret = 0;
mbed_official 89:2d5255b9d6c5 1291 continue;
mbed_official 89:2d5255b9d6c5 1292 } else if (ret != 0) {
mbed_official 78:5f419e0f96dd 1293 PRINT_ERROR(ret, "mbedtls_ecdh_make_public()");
mbed_official 78:5f419e0f96dd 1294 goto exit;
Janos Follath 0:1f7c5025e59d 1295 }
Janos Follath 0:1f7c5025e59d 1296
mbed_official 78:5f419e0f96dd 1297 ecp_clear_precomputed(&ecdh.grp);
mbed_official 70:72c865037f5d 1298
mbed_official 78:5f419e0f96dd 1299 BENCHMARK_PUBLIC(title, "handshake",
mbed_official 78:5f419e0f96dd 1300 ret = mbedtls_ecdh_calc_secret(&ecdh, &olen, buf,
mbed_official 78:5f419e0f96dd 1301 sizeof(buf), myrand,
mbed_official 78:5f419e0f96dd 1302 NULL));
mbed_official 70:72c865037f5d 1303
mbed_official 78:5f419e0f96dd 1304 exit:
mbed_official 78:5f419e0f96dd 1305 mbedtls_ecdh_free(&ecdh);
mbed_official 70:72c865037f5d 1306 }
mbed_official 78:5f419e0f96dd 1307
mbed_official 78:5f419e0f96dd 1308 return ret;
mbed_official 78:5f419e0f96dd 1309 }
mbed_official 78:5f419e0f96dd 1310 #endif /* MBEDTLS_ECDH_C */
mbed_official 70:72c865037f5d 1311
mbed_official 78:5f419e0f96dd 1312 #if defined(MBEDTLS_ECDH_C) && defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
mbed_official 78:5f419e0f96dd 1313 /* Curve25519 needs to be handled separately */
mbed_official 78:5f419e0f96dd 1314 MBED_NOINLINE static int benchmark_ecdh_curve22519()
mbed_official 78:5f419e0f96dd 1315 {
mbed_official 78:5f419e0f96dd 1316 int ret = 0;
mbed_official 78:5f419e0f96dd 1317 mbedtls_ecdh_context ecdh;
mbed_official 78:5f419e0f96dd 1318 mbedtls_mpi z;
mbed_official 78:5f419e0f96dd 1319
mbed_official 78:5f419e0f96dd 1320 mbedtls_ecdh_init(&ecdh);
mbed_official 78:5f419e0f96dd 1321 mbedtls_mpi_init(&z);
mbed_official 78:5f419e0f96dd 1322
mbed_official 89:2d5255b9d6c5 1323 ret = mbedtls_snprintf(title, sizeof(title), "ECDHE-Curve25519");
mbed_official 89:2d5255b9d6c5 1324 if (ret < 0 || static_cast<size_t>(ret) >= sizeof(title)) {
mbed_official 89:2d5255b9d6c5 1325 mbedtls_printf("Failed to compose title string using "
mbed_official 89:2d5255b9d6c5 1326 "mbedtls_snprintf(): %d\n", ret);
mbed_official 89:2d5255b9d6c5 1327 goto exit;
mbed_official 89:2d5255b9d6c5 1328 }
mbed_official 89:2d5255b9d6c5 1329
mbed_official 78:5f419e0f96dd 1330 ret = mbedtls_ecp_group_load(&ecdh.grp, MBEDTLS_ECP_DP_CURVE25519);
mbed_official 89:2d5255b9d6c5 1331 if (ret == MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED) {
mbed_official 89:2d5255b9d6c5 1332 /* Do not consider this as a failure */
mbed_official 89:2d5255b9d6c5 1333 mbedtls_printf(HEADER_FORMAT "Feature unsupported\n", title);
mbed_official 89:2d5255b9d6c5 1334 ret = 0;
mbed_official 89:2d5255b9d6c5 1335 goto exit;
mbed_official 89:2d5255b9d6c5 1336 } else if (ret != 0) {
mbed_official 78:5f419e0f96dd 1337 PRINT_ERROR(ret, "mbedtls_ecp_group_load()");
mbed_official 78:5f419e0f96dd 1338 goto exit;
mbed_official 78:5f419e0f96dd 1339 }
mbed_official 78:5f419e0f96dd 1340
mbed_official 78:5f419e0f96dd 1341 ret = mbedtls_ecdh_gen_public(&ecdh.grp, &ecdh.d, &ecdh.Qp, myrand,
mbed_official 78:5f419e0f96dd 1342 NULL);
mbed_official 89:2d5255b9d6c5 1343 if (ret == MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED) {
mbed_official 89:2d5255b9d6c5 1344 /* Do not consider this as a failure */
mbed_official 89:2d5255b9d6c5 1345 mbedtls_printf(HEADER_FORMAT "Feature unsupported\n", title);
mbed_official 89:2d5255b9d6c5 1346 ret = 0;
mbed_official 89:2d5255b9d6c5 1347 goto exit;
mbed_official 89:2d5255b9d6c5 1348 } else if (ret != 0) {
mbed_official 78:5f419e0f96dd 1349 PRINT_ERROR(ret, "mbedtls_ecdh_gen_public()");
mbed_official 78:5f419e0f96dd 1350 goto exit;
mbed_official 70:72c865037f5d 1351 }
mbed_official 70:72c865037f5d 1352
mbed_official 78:5f419e0f96dd 1353 /*
mbed_official 78:5f419e0f96dd 1354 * Benchmarking this requires two function calls that can fail. We
mbed_official 78:5f419e0f96dd 1355 * add a check in between them to check for any errors. In normal
mbed_official 78:5f419e0f96dd 1356 * operation, the overhead of this check is negligible
mbed_official 78:5f419e0f96dd 1357 */
mbed_official 89:2d5255b9d6c5 1358 BENCHMARK_PUBLIC(title, "handshake",
mbed_official 78:5f419e0f96dd 1359 ret = mbedtls_ecdh_gen_public(&ecdh.grp, &ecdh.d,
mbed_official 78:5f419e0f96dd 1360 &ecdh.Q, myrand, NULL);
mbed_official 78:5f419e0f96dd 1361 if (ret != 0) {
mbed_official 78:5f419e0f96dd 1362 PRINT_ERROR(ret, "mbedtls_ecdh_make_public()");
mbed_official 78:5f419e0f96dd 1363 goto exit;
mbed_official 78:5f419e0f96dd 1364 }
mbed_official 78:5f419e0f96dd 1365 ret = mbedtls_ecdh_compute_shared(&ecdh.grp, &z,
mbed_official 78:5f419e0f96dd 1366 &ecdh.Qp, &ecdh.d,
mbed_official 78:5f419e0f96dd 1367 myrand, NULL));
mbed_official 70:72c865037f5d 1368
mbed_official 78:5f419e0f96dd 1369 mbedtls_ecdh_free(&ecdh);
mbed_official 78:5f419e0f96dd 1370 mbedtls_mpi_free(&z);
mbed_official 78:5f419e0f96dd 1371
mbed_official 78:5f419e0f96dd 1372 mbedtls_ecdh_init(&ecdh);
mbed_official 78:5f419e0f96dd 1373 mbedtls_mpi_init(&z);
mbed_official 78:5f419e0f96dd 1374
mbed_official 89:2d5255b9d6c5 1375 ret = mbedtls_snprintf(title, sizeof(title), "ECDH-Curve25519");
mbed_official 89:2d5255b9d6c5 1376 if (ret < 0 || static_cast<size_t>(ret) >= sizeof(title)) {
mbed_official 89:2d5255b9d6c5 1377 mbedtls_printf("Failed to compose title string using "
mbed_official 89:2d5255b9d6c5 1378 "mbedtls_snprintf(): %d\n", ret);
mbed_official 89:2d5255b9d6c5 1379 goto exit;
mbed_official 89:2d5255b9d6c5 1380 }
mbed_official 78:5f419e0f96dd 1381 ret = mbedtls_ecp_group_load(&ecdh.grp, MBEDTLS_ECP_DP_CURVE25519);
mbed_official 89:2d5255b9d6c5 1382 if (ret == MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED) {
mbed_official 89:2d5255b9d6c5 1383 /* Do not consider this as a failure */
mbed_official 89:2d5255b9d6c5 1384 mbedtls_printf(HEADER_FORMAT "Feature unsupported\n", title);
mbed_official 89:2d5255b9d6c5 1385 ret = 0;
mbed_official 89:2d5255b9d6c5 1386 goto exit;
mbed_official 89:2d5255b9d6c5 1387 } else if (ret != 0) {
mbed_official 78:5f419e0f96dd 1388 PRINT_ERROR(ret, "mbedtls_ecp_group_load()");
mbed_official 78:5f419e0f96dd 1389 goto exit;
mbed_official 78:5f419e0f96dd 1390 }
Janos Follath 0:1f7c5025e59d 1391
mbed_official 78:5f419e0f96dd 1392 ret = mbedtls_ecdh_gen_public(&ecdh.grp, &ecdh.d, &ecdh.Qp, myrand, NULL);
mbed_official 89:2d5255b9d6c5 1393 if (ret == MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED) {
mbed_official 89:2d5255b9d6c5 1394 /* Do not consider this as a failure */
mbed_official 89:2d5255b9d6c5 1395 mbedtls_printf(HEADER_FORMAT "Feature unsupported\n", title);
mbed_official 89:2d5255b9d6c5 1396 ret = 0;
mbed_official 89:2d5255b9d6c5 1397 goto exit;
mbed_official 89:2d5255b9d6c5 1398 } else if (ret != 0) {
mbed_official 78:5f419e0f96dd 1399 PRINT_ERROR(ret, "mbedtls_ecdh_gen_public()");
mbed_official 78:5f419e0f96dd 1400 goto exit;
mbed_official 78:5f419e0f96dd 1401 }
Janos Follath 0:1f7c5025e59d 1402
mbed_official 78:5f419e0f96dd 1403 ret = mbedtls_ecdh_gen_public(&ecdh.grp, &ecdh.d, &ecdh.Q, myrand, NULL);
mbed_official 89:2d5255b9d6c5 1404 if (ret == MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED) {
mbed_official 89:2d5255b9d6c5 1405 /* Do not consider this as a failure */
mbed_official 89:2d5255b9d6c5 1406 mbedtls_printf(HEADER_FORMAT "Feature unsupported\n", title);
mbed_official 89:2d5255b9d6c5 1407 ret = 0;
mbed_official 89:2d5255b9d6c5 1408 goto exit;
mbed_official 89:2d5255b9d6c5 1409 } else if (ret != 0) {
mbed_official 78:5f419e0f96dd 1410 PRINT_ERROR(ret, "mbedtls_ecdh_gen_public()");
mbed_official 78:5f419e0f96dd 1411 goto exit;
mbed_official 78:5f419e0f96dd 1412 }
Janos Follath 0:1f7c5025e59d 1413
mbed_official 89:2d5255b9d6c5 1414 BENCHMARK_PUBLIC(title, "handshake",
mbed_official 78:5f419e0f96dd 1415 ret = mbedtls_ecdh_compute_shared(&ecdh.grp, &z,
mbed_official 78:5f419e0f96dd 1416 &ecdh.Qp, &ecdh.d,
mbed_official 78:5f419e0f96dd 1417 myrand, NULL));
mbed_official 78:5f419e0f96dd 1418
mbed_official 78:5f419e0f96dd 1419 exit:
mbed_official 78:5f419e0f96dd 1420 mbedtls_ecdh_free(&ecdh);
mbed_official 78:5f419e0f96dd 1421 mbedtls_mpi_free(&z);
mbed_official 78:5f419e0f96dd 1422
mbed_official 78:5f419e0f96dd 1423 return ret;
Janos Follath 0:1f7c5025e59d 1424 }
mbed_official 78:5f419e0f96dd 1425 #endif /* MBEDTLS_ECDH_C && MBEDTLS_ECP_DP_CURVE25519_ENABLED */
Janos Follath 0:1f7c5025e59d 1426
mbed_official 78:5f419e0f96dd 1427 int main()
mbed_official 78:5f419e0f96dd 1428 {
mbed_official 78:5f419e0f96dd 1429 int exit_code = MBEDTLS_EXIT_SUCCESS;
mbed_official 63:9f7e5224fc60 1430
mbed_official 78:5f419e0f96dd 1431 memset(buf, 0xAA, sizeof(buf));
mbed_official 78:5f419e0f96dd 1432 memset(tmp, 0xBB, sizeof(tmp));
mbed_official 78:5f419e0f96dd 1433
mbed_official 80:0f7a38bbcebe 1434 if ((exit_code = mbedtls_platform_setup(NULL)) != 0) {
mbed_official 78:5f419e0f96dd 1435 mbedtls_printf("Platform initialization failed with error %d\r\n",
mbed_official 78:5f419e0f96dd 1436 exit_code);
mbed_official 63:9f7e5224fc60 1437 return MBEDTLS_EXIT_FAILURE;
Janos Follath 0:1f7c5025e59d 1438 }
mbed_official 63:9f7e5224fc60 1439
mbed_official 78:5f419e0f96dd 1440 #if defined(MBEDTLS_MD4_C)
mbed_official 78:5f419e0f96dd 1441 if (benchmark_md4() != 0) {
mbed_official 78:5f419e0f96dd 1442 exit_code = MBEDTLS_EXIT_FAILURE;
mbed_official 78:5f419e0f96dd 1443 }
mbed_official 78:5f419e0f96dd 1444 #endif /* MBEDTLS_MD4_C */
mbed_official 78:5f419e0f96dd 1445
mbed_official 78:5f419e0f96dd 1446 #if defined(MBEDTLS_MD5_C)
mbed_official 78:5f419e0f96dd 1447 if (benchmark_md5() != 0) {
mbed_official 78:5f419e0f96dd 1448 exit_code = MBEDTLS_EXIT_FAILURE;
mbed_official 78:5f419e0f96dd 1449 }
mbed_official 78:5f419e0f96dd 1450 #endif /* MBEDTLS_MD5_C */
mbed_official 78:5f419e0f96dd 1451
mbed_official 78:5f419e0f96dd 1452 #if defined(MBEDTLS_RIPEMD160_C)
mbed_official 78:5f419e0f96dd 1453 if (benchmark_ripemd160() != 0) {
mbed_official 78:5f419e0f96dd 1454 exit_code = MBEDTLS_EXIT_FAILURE;
mbed_official 78:5f419e0f96dd 1455 }
mbed_official 78:5f419e0f96dd 1456 #endif /* MBEDTLS_RIPEMD160_C */
mbed_official 78:5f419e0f96dd 1457
mbed_official 78:5f419e0f96dd 1458 #if defined(MBEDTLS_SHA1_C)
mbed_official 78:5f419e0f96dd 1459 if (benchmark_sha1() != 0) {
mbed_official 78:5f419e0f96dd 1460 exit_code = MBEDTLS_EXIT_FAILURE;
mbed_official 78:5f419e0f96dd 1461 }
mbed_official 78:5f419e0f96dd 1462 #endif /* MBEDTLS_SHA1_C */
mbed_official 78:5f419e0f96dd 1463
mbed_official 78:5f419e0f96dd 1464 #if defined(MBEDTLS_SHA256_C)
mbed_official 78:5f419e0f96dd 1465 if (benchmark_sha256() != 0) {
mbed_official 78:5f419e0f96dd 1466 exit_code = MBEDTLS_EXIT_FAILURE;
mbed_official 78:5f419e0f96dd 1467 }
mbed_official 78:5f419e0f96dd 1468 #endif /* MBEDTLS_SHA256_C */
mbed_official 78:5f419e0f96dd 1469
mbed_official 78:5f419e0f96dd 1470 #if defined(MBEDTLS_SHA256_C)
mbed_official 78:5f419e0f96dd 1471 if (benchmark_sha512() != 0) {
mbed_official 78:5f419e0f96dd 1472 exit_code = MBEDTLS_EXIT_FAILURE;
mbed_official 78:5f419e0f96dd 1473 }
mbed_official 78:5f419e0f96dd 1474 #endif /* MBEDTLS_SHA512_C */
mbed_official 78:5f419e0f96dd 1475
mbed_official 78:5f419e0f96dd 1476 #if defined(MBEDTLS_ARC4_C)
mbed_official 78:5f419e0f96dd 1477 if (benchmark_arc4() != 0) {
mbed_official 78:5f419e0f96dd 1478 exit_code = MBEDTLS_EXIT_FAILURE;
mbed_official 78:5f419e0f96dd 1479 }
mbed_official 78:5f419e0f96dd 1480 #endif /* MBEDTLS_ARC4_C */
mbed_official 78:5f419e0f96dd 1481
mbed_official 78:5f419e0f96dd 1482 #if defined(MBEDTLS_DES_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
mbed_official 78:5f419e0f96dd 1483 if (benchmark_des3() != 0) {
mbed_official 78:5f419e0f96dd 1484 exit_code = MBEDTLS_EXIT_FAILURE;
mbed_official 78:5f419e0f96dd 1485 }
mbed_official 78:5f419e0f96dd 1486 #endif /* MBEDTLS_DES_C && MBEDTLS_CIPHER_MODE_CBC */
mbed_official 78:5f419e0f96dd 1487
mbed_official 78:5f419e0f96dd 1488 #if defined(MBEDTLS_DES_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
mbed_official 78:5f419e0f96dd 1489 if (benchmark_des() != 0) {
mbed_official 78:5f419e0f96dd 1490 exit_code = MBEDTLS_EXIT_FAILURE;
mbed_official 78:5f419e0f96dd 1491 }
mbed_official 78:5f419e0f96dd 1492 #endif /* MBEDTLS_DES_C && MBEDTLS_CIPHER_MODE_CBC */
mbed_official 78:5f419e0f96dd 1493
mbed_official 78:5f419e0f96dd 1494 #if defined(MBEDTLS_DES_C) && defined(MBEDTLS_CIPHER_MODE_CBC) && \
mbed_official 78:5f419e0f96dd 1495 defined(MBEDTLS_CMAC_C)
mbed_official 78:5f419e0f96dd 1496 if (benchmark_des3_cmac() != 0) {
mbed_official 78:5f419e0f96dd 1497 exit_code = MBEDTLS_EXIT_FAILURE;
mbed_official 78:5f419e0f96dd 1498 }
mbed_official 78:5f419e0f96dd 1499 #endif /* MBEDTLS_DES_C && MBEDTLS_CIPHER_MODE_CBC && MBEDTLS_CMAC_C */
mbed_official 78:5f419e0f96dd 1500
mbed_official 78:5f419e0f96dd 1501 #if defined(MBEDTLS_AES_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
mbed_official 78:5f419e0f96dd 1502 if (benchmark_aes_cbc() != 0) {
mbed_official 78:5f419e0f96dd 1503 exit_code = MBEDTLS_EXIT_FAILURE;
mbed_official 78:5f419e0f96dd 1504 }
mbed_official 78:5f419e0f96dd 1505 #endif /* MBEDTLS_AES_C && MBEDTLS_CIPHER_MODE_CBC */
mbed_official 78:5f419e0f96dd 1506
mbed_official 78:5f419e0f96dd 1507 #if defined(MBEDTLS_AES_C) && defined(MBEDTLS_CIPHER_MODE_CTR)
mbed_official 78:5f419e0f96dd 1508 if (benchmark_aes_ctr() != 0) {
mbed_official 78:5f419e0f96dd 1509 exit_code = MBEDTLS_EXIT_FAILURE;
mbed_official 78:5f419e0f96dd 1510 }
mbed_official 78:5f419e0f96dd 1511 #endif /* MBEDTLS_AES_C && MBEDTLS_CIPHER_MODE_CTR */
mbed_official 78:5f419e0f96dd 1512
mbed_official 78:5f419e0f96dd 1513 #if defined(MBEDTLS_AES_C) && defined(MBEDTLS_GCM_C)
mbed_official 78:5f419e0f96dd 1514 if (benchmark_aes_gcm() != 0) {
mbed_official 78:5f419e0f96dd 1515 exit_code = MBEDTLS_EXIT_FAILURE;
mbed_official 78:5f419e0f96dd 1516 }
mbed_official 78:5f419e0f96dd 1517 #endif /* MBEDTLS_AES_C && MBEDTLS_GCM_C */
mbed_official 78:5f419e0f96dd 1518
mbed_official 78:5f419e0f96dd 1519 #if defined(MBEDTLS_AES_C) && defined(MBEDTLS_CCM_C)
mbed_official 78:5f419e0f96dd 1520 if (benchmark_aes_ccm() != 0) {
mbed_official 78:5f419e0f96dd 1521 exit_code = MBEDTLS_EXIT_FAILURE;
mbed_official 78:5f419e0f96dd 1522 }
mbed_official 78:5f419e0f96dd 1523 #endif /* MBEDTLS_AES_C && MBEDTLS_CCM_C */
mbed_official 78:5f419e0f96dd 1524
mbed_official 78:5f419e0f96dd 1525 #if defined(MBEDTLS_AES_C) && defined(MBEDTLS_CMAC_C)
mbed_official 78:5f419e0f96dd 1526 if (benchmark_aes_cmac() != 0) {
mbed_official 78:5f419e0f96dd 1527 exit_code = MBEDTLS_EXIT_FAILURE;
mbed_official 78:5f419e0f96dd 1528 }
mbed_official 78:5f419e0f96dd 1529 #endif /* MBEDTLS_AES_C && MBEDTLS_CMAC_C */
mbed_official 78:5f419e0f96dd 1530
mbed_official 78:5f419e0f96dd 1531 #if defined(MBEDTLS_CAMELLIA_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
mbed_official 78:5f419e0f96dd 1532 if (benchmark_camellia() != 0) {
mbed_official 78:5f419e0f96dd 1533 exit_code = MBEDTLS_EXIT_FAILURE;
mbed_official 78:5f419e0f96dd 1534 }
mbed_official 78:5f419e0f96dd 1535 #endif /* MBEDTLS_CAMELLIA_C && MBEDTLS_CIPHER_MODE_CBC */
mbed_official 78:5f419e0f96dd 1536
mbed_official 78:5f419e0f96dd 1537 #if defined(MBEDTLS_BLOWFISH_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
mbed_official 78:5f419e0f96dd 1538 if (benchmark_blowfish() != 0) {
mbed_official 78:5f419e0f96dd 1539 exit_code = MBEDTLS_EXIT_FAILURE;
mbed_official 78:5f419e0f96dd 1540 }
mbed_official 78:5f419e0f96dd 1541 #endif /* MBEDTLS_BLOWFISH_C && MBEDTLS_CIPHER_MODE_CBC */
mbed_official 78:5f419e0f96dd 1542
mbed_official 78:5f419e0f96dd 1543 #if defined(MBEDTLS_HAVEGE_C)
mbed_official 78:5f419e0f96dd 1544 if (benchmark_havege() != 0) {
mbed_official 78:5f419e0f96dd 1545 exit_code = MBEDTLS_EXIT_FAILURE;
mbed_official 78:5f419e0f96dd 1546 }
mbed_official 78:5f419e0f96dd 1547 #endif /* MBEDTLS_HAVEGE_C */
mbed_official 78:5f419e0f96dd 1548
mbed_official 78:5f419e0f96dd 1549 #if defined(MBEDTLS_CTR_DRBG_C)
mbed_official 78:5f419e0f96dd 1550 if (benchmark_ctr_drbg() != 0) {
mbed_official 78:5f419e0f96dd 1551 exit_code = MBEDTLS_EXIT_FAILURE;
mbed_official 78:5f419e0f96dd 1552 }
mbed_official 78:5f419e0f96dd 1553 #endif /* MBEDTLS_CTR_DRBG_C */
mbed_official 78:5f419e0f96dd 1554
mbed_official 78:5f419e0f96dd 1555 #if defined(MBEDTLS_HMAC_DRBG_C)
mbed_official 78:5f419e0f96dd 1556 if (benchmark_hmac_drbg() != 0) {
mbed_official 78:5f419e0f96dd 1557 exit_code = MBEDTLS_EXIT_FAILURE;
mbed_official 78:5f419e0f96dd 1558 }
mbed_official 78:5f419e0f96dd 1559 #endif /* MBEDTLS_HMAC_DRBG_C */
mbed_official 78:5f419e0f96dd 1560
mbed_official 78:5f419e0f96dd 1561 #if defined(MBEDTLS_RSA_C) && \
mbed_official 78:5f419e0f96dd 1562 defined(MBEDTLS_PEM_PARSE_C) && defined(MBEDTLS_PK_PARSE_C)
mbed_official 78:5f419e0f96dd 1563 if (benchmark_rsa() != 0) {
mbed_official 78:5f419e0f96dd 1564 exit_code = MBEDTLS_EXIT_FAILURE;
mbed_official 78:5f419e0f96dd 1565 }
mbed_official 78:5f419e0f96dd 1566 #endif /* MBEDTLS_RSA_C && MBEDTLS_PEM_PARSE_C && MBEDTLS_PK_PARSE_C */
mbed_official 78:5f419e0f96dd 1567
mbed_official 78:5f419e0f96dd 1568 #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_BIGNUM_C)
mbed_official 78:5f419e0f96dd 1569 if (benchmark_dhm() != 0) {
mbed_official 78:5f419e0f96dd 1570 exit_code = MBEDTLS_EXIT_FAILURE;
mbed_official 78:5f419e0f96dd 1571 }
mbed_official 78:5f419e0f96dd 1572 #endif /* MBEDTLS_DHM_C && MBEDTLS_BIGNUM_C */
mbed_official 78:5f419e0f96dd 1573
mbed_official 78:5f419e0f96dd 1574 #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA256_C)
mbed_official 78:5f419e0f96dd 1575 if (benchmark_ecdsa() != 0) {
mbed_official 78:5f419e0f96dd 1576 exit_code = MBEDTLS_EXIT_FAILURE;
mbed_official 78:5f419e0f96dd 1577 }
mbed_official 78:5f419e0f96dd 1578 #endif /* MBEDTLS_ECDSA_C && MBEDTLS_SHA2565_C */
mbed_official 78:5f419e0f96dd 1579
mbed_official 78:5f419e0f96dd 1580 #if defined(MBEDTLS_ECDH_C)
mbed_official 78:5f419e0f96dd 1581 if (benchmark_ecdh() != 0) {
mbed_official 63:9f7e5224fc60 1582 exit_code = MBEDTLS_EXIT_FAILURE;
mbed_official 63:9f7e5224fc60 1583 }
mbed_official 63:9f7e5224fc60 1584
mbed_official 78:5f419e0f96dd 1585 #if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
mbed_official 78:5f419e0f96dd 1586 if (benchmark_ecdh_curve22519() != 0) {
mbed_official 78:5f419e0f96dd 1587 exit_code = MBEDTLS_EXIT_FAILURE;
mbed_official 78:5f419e0f96dd 1588 }
mbed_official 78:5f419e0f96dd 1589 #endif /* MBEDTLS_ECP_DP_CURVE25519_ENABLED */
mbed_official 78:5f419e0f96dd 1590 #endif /* MBEDTLS_ECDH_C */
mbed_official 78:5f419e0f96dd 1591
mbed_official 78:5f419e0f96dd 1592 mbedtls_printf("DONE\n");
mbed_official 78:5f419e0f96dd 1593
mbed_official 80:0f7a38bbcebe 1594 mbedtls_platform_teardown(NULL);
mbed_official 63:9f7e5224fc60 1595 return exit_code;
Janos Follath 0:1f7c5025e59d 1596 }