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:
Thu Nov 09 09:15:12 2017 +0000
Revision:
48:4e6f86cca9fa
Parent:
33:0af60cd5226d
Child:
63:9f7e5224fc60
Change Arm trademarks in the examples' source files

.
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 48:4e6f86cca9fa 4 * Copyright (C) 2006-2016, 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 #if DEBUG_LEVEL > 0
Janos Follath 0:1f7c5025e59d 22 #include "mbedtls/debug.h"
Janos Follath 0:1f7c5025e59d 23 #endif
Janos Follath 0:1f7c5025e59d 24
Janos Follath 0:1f7c5025e59d 25 #if !defined(MBEDTLS_CONFIG_FILE)
Janos Follath 0:1f7c5025e59d 26 #include "mbedtls/config.h"
Janos Follath 0:1f7c5025e59d 27 #else
Janos Follath 0:1f7c5025e59d 28 #include MBEDTLS_CONFIG_FILE
Janos Follath 0:1f7c5025e59d 29 #endif
Janos Follath 0:1f7c5025e59d 30
Janos Follath 0:1f7c5025e59d 31 #include "mbedtls/platform.h"
Janos Follath 0:1f7c5025e59d 32
Janos Follath 0:1f7c5025e59d 33 #include <string.h>
Janos Follath 0:1f7c5025e59d 34
Janos Follath 0:1f7c5025e59d 35 #include "mbedtls/md4.h"
Janos Follath 0:1f7c5025e59d 36 #include "mbedtls/md5.h"
Janos Follath 0:1f7c5025e59d 37 #include "mbedtls/ripemd160.h"
Janos Follath 0:1f7c5025e59d 38 #include "mbedtls/sha1.h"
Janos Follath 0:1f7c5025e59d 39 #include "mbedtls/sha256.h"
Janos Follath 0:1f7c5025e59d 40 #include "mbedtls/sha512.h"
Janos Follath 0:1f7c5025e59d 41 #include "mbedtls/arc4.h"
Janos Follath 0:1f7c5025e59d 42 #include "mbedtls/des.h"
Janos Follath 0:1f7c5025e59d 43 #include "mbedtls/aes.h"
mbed_official 11:6ccae3ebafd5 44 #include "mbedtls/cmac.h"
Janos Follath 0:1f7c5025e59d 45 #include "mbedtls/blowfish.h"
Janos Follath 0:1f7c5025e59d 46 #include "mbedtls/camellia.h"
Janos Follath 0:1f7c5025e59d 47 #include "mbedtls/gcm.h"
Janos Follath 0:1f7c5025e59d 48 #include "mbedtls/ccm.h"
Janos Follath 0:1f7c5025e59d 49 #include "mbedtls/havege.h"
Janos Follath 0:1f7c5025e59d 50 #include "mbedtls/ctr_drbg.h"
Janos Follath 0:1f7c5025e59d 51 #include "mbedtls/hmac_drbg.h"
Janos Follath 0:1f7c5025e59d 52 #include "mbedtls/rsa.h"
Janos Follath 0:1f7c5025e59d 53 #include "mbedtls/pk.h"
Janos Follath 0:1f7c5025e59d 54 #include "mbedtls/dhm.h"
Janos Follath 0:1f7c5025e59d 55 #include "mbedtls/ecdsa.h"
Janos Follath 0:1f7c5025e59d 56 #include "mbedtls/ecdh.h"
Janos Follath 0:1f7c5025e59d 57 #include "mbedtls/error.h"
Janos Follath 0:1f7c5025e59d 58
Janos Follath 0:1f7c5025e59d 59 #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Janos Follath 0:1f7c5025e59d 60 #include "mbedtls/memory_buffer_alloc.h"
Janos Follath 0:1f7c5025e59d 61 #endif
Janos Follath 0:1f7c5025e59d 62
Janos Follath 0:1f7c5025e59d 63 #define RSA_PRIVATE_KEY_2048 \
Janos Follath 0:1f7c5025e59d 64 "-----BEGIN RSA PRIVATE KEY-----\r\n" \
Janos Follath 0:1f7c5025e59d 65 "MIIEogIBAAKCAQEA2dwVr+IMGEtA2/MCP6fA5eb/6B18Bq6e7gw8brNPkm3E6LyR\r\n" \
Janos Follath 0:1f7c5025e59d 66 "4DnMJVxZmw3bPDKBDoKzfntkMESi/Yw5UopLtVfjGfWeQWPClqffLZBsZ60BRAsg\r\n" \
Janos Follath 0:1f7c5025e59d 67 "/g+ID5tgzxSuxzftypK59uexOVCAm7hCKZHGO3DbI7bLY27j7VAgEP7d/yuaz5Fx\r\n" \
Janos Follath 0:1f7c5025e59d 68 "Kl/vu7shqrBoz6ABJVJD3KC8nUiMRUCXRINmxbyUUjA4DnicZv6+xrGKr36r6M8h\r\n" \
Janos Follath 0:1f7c5025e59d 69 "VYLa5msKc8WzbnBWzpUsrpb4/r7ML+qp92gdSfVJ8/bLiU7h2C7faDA59uaqrFK9\r\n" \
Janos Follath 0:1f7c5025e59d 70 "xmDdx7FaWhGQs3LWW6w1UNgkPS0FDYUslpsnsQIDAQABAoIBAC7IJNwM5V3+IuJY\r\n" \
Janos Follath 0:1f7c5025e59d 71 "T35Nzo1PyloUosJokvY5KGz5Ejg2XBdCDu0gXCcVqqQyGIbXrYDpLhQV+RCoXHun\r\n" \
Janos Follath 0:1f7c5025e59d 72 "tdN0oQdC5SB47s/J1Uo2qCUHo0+sBd6PqTkFKsl3KxWssk9TQjvCwC412IefMs69\r\n" \
Janos Follath 0:1f7c5025e59d 73 "hW+ZvwCanmQP56LleApIr2oW4KLfW8Ry/QfZlua+dizctdN7+H1mWwgZQTY9T27J\r\n" \
Janos Follath 0:1f7c5025e59d 74 "6RtGRA5NVkKVPzIHVJfdpKoO7xGg1g06aEbPB/VmGvZaaFWWnaf7uRvFjLZecBLu\r\n" \
Janos Follath 0:1f7c5025e59d 75 "QSx2DA/GDjirlDYj99PJb7DtB4xRtKzsyw0o+xapC8w6OtIl/3xFt9moCu2jGrsx\r\n" \
Janos Follath 0:1f7c5025e59d 76 "vpjHdfECgYEA7fSACRseIs9gAIVX8wq6gayTpA47DHYWAD6IQfIj35SJ+AgsvbFF\r\n" \
Janos Follath 0:1f7c5025e59d 77 "4AmrwDhcJVPmDy1N4nLBfyGAMt/2CfiYkdkW6QFX/ULRMMBL/G7kWV8hYQDICB2g\r\n" \
Janos Follath 0:1f7c5025e59d 78 "xaMRN1lPCmFq6BkSWjwIYTnYDFBDWVm1GVT8TMtJoM8Erej9qC0PeFUCgYEA6mF3\r\n" \
Janos Follath 0:1f7c5025e59d 79 "bigO3t8f5sig+XepaftEUbkJMzo72TVRnIR2ycdR2ihelPQ+25g9dwV0ZA5XXhBS\r\n" \
Janos Follath 0:1f7c5025e59d 80 "DKOABWjMM739Mwmy9v26Dlmu9R01zHQktMvtEAyfz7lk2NF0aMuj8285OJUBf9bz\r\n" \
Janos Follath 0:1f7c5025e59d 81 "Cq3MjtMCD+4CZ6iaEqCdUKOuxfpx5cWVJV+qve0CgYBhD1YaYMFOGaBjFgDl1f51\r\n" \
Janos Follath 0:1f7c5025e59d 82 "Xltqk5NqZdBbkSYrIAWZ8RDF5y+4wFJsLAWuhk6vuyUgE66tK3nZzWRpXAkT0B8L\r\n" \
Janos Follath 0:1f7c5025e59d 83 "fq1lpXKqj1KcvBNCiEkEW1VWJ+dvyAYIF5eyJ++hoFLnETL3M32HivyhKSwPihPg\r\n" \
Janos Follath 0:1f7c5025e59d 84 "nVW8TT9fJJIYDe1JZ/fjcQKBgHJfv7UsrR0LSvkG3K8AOtbx+8PZhOjPuRbk0v+L\r\n" \
Janos Follath 0:1f7c5025e59d 85 "EKCkuIe5/XW4vtfQMeZb7hFJgk7vrepm+vkoy8VQKDf4urGW3W1VTHBmobM01hi4\r\n" \
Janos Follath 0:1f7c5025e59d 86 "DuYvEul+Mf0wMRtWjJolo4m+BO5KiW2jpFfqFm6JmfjVqOIAKOSKC6am8V/MDF0h\r\n" \
Janos Follath 0:1f7c5025e59d 87 "kyN9AoGAT9oOiEXMolbkDZw/QCaBiRoAGlGlNYUkJ+58U6OjIZLISw6aFv+Y2uE0\r\n" \
Janos Follath 0:1f7c5025e59d 88 "mEImItjuYZtSYKblWikp6ldPoKlt9bwEFe3c6IZ8kJ3+xyEyAGrvjXjEY7PzP6dp\r\n" \
Janos Follath 0:1f7c5025e59d 89 "Ajbjp9X9uocEBv9W/KsBLdQ7yizcL/toHwdBO4vQqmqTvAc5IIw=\r\n" \
Janos Follath 0:1f7c5025e59d 90 "-----END RSA PRIVATE KEY-----\r\n"
Janos Follath 0:1f7c5025e59d 91
Janos Follath 0:1f7c5025e59d 92 #define RSA_PRIVATE_KEY_4096 \
Janos Follath 0:1f7c5025e59d 93 "-----BEGIN RSA PRIVATE KEY-----\r\n" \
Janos Follath 0:1f7c5025e59d 94 "MIIJKgIBAAKCAgEAmkdGjoIshJuOt2NO47qB3Z3yyvmLg2j351isItSNuFQU3qr+\r\n" \
Janos Follath 0:1f7c5025e59d 95 "jXHIeANf03yw/K0Zvos8RPd+CqLjoxAQL3QDH4bZAl88bIo29i+SANbNSrKQmc0k\r\n" \
Janos Follath 0:1f7c5025e59d 96 "pH+yzw3alDzO0GZaOPZjsbo6AwBrno5msi0vRuC2aY8vGLPsZWSyLai7tneS1j/o\r\n" \
Janos Follath 0:1f7c5025e59d 97 "vYW6XIo8Cj61j2Ypy9HhVUW/4Wc+zAT25D/x7jTpkqJLWWT+YzibNbOY48M5eJcB\r\n" \
Janos Follath 0:1f7c5025e59d 98 "6/sMyUIeI3/u/wXyMrooNyLiCpedkuHRA0m7u5cWPTUISTunSRlVFij/NHJjuU8e\r\n" \
Janos Follath 0:1f7c5025e59d 99 "wA3B29yfZFsUqDEnyc+OxniIueAixTomVszxAaVn8zFEbYhFMPqziiFp99u3jfeG\r\n" \
Janos Follath 0:1f7c5025e59d 100 "k1q9mmUi/uCfUC4e2IC5rqq1ZbKSduH7Ug/Vn2bGQahww0sZFRHDXFrnBcotcW+M\r\n" \
Janos Follath 0:1f7c5025e59d 101 "bnC290VBDnYgzmdYrIOxuPb2aUwJo4ZlbKh5uBB1PigMuyhLKibQ1a+V5ZJGdpP6\r\n" \
Janos Follath 0:1f7c5025e59d 102 "SE9PGIdgYWSmh2QEMuLE6v+wTO2LQ5JgqsvFfi3GIZvkn0s8jTS72Jq2uMkFkMer\r\n" \
Janos Follath 0:1f7c5025e59d 103 "UBjPDYaSPy5kpo103KerWs+cMPOJ/3FtZzI++7MoSUTkWVr1ySQFt5i1EIZ/0Thi\r\n" \
Janos Follath 0:1f7c5025e59d 104 "jut2jNe8a4AoA3TtC8Rkk/3AIIbg8MVNT4EnT+KHROTMu6gET1oJ3YfBRpUCAwEA\r\n" \
Janos Follath 0:1f7c5025e59d 105 "AQKCAgEAhuNSmT7PVZH8kfLOAuYKrY1vvm+4v0iDl048Eqfs0QESziyLK3gUYnnw\r\n" \
Janos Follath 0:1f7c5025e59d 106 "yqP2yrU+EQ8Dvvj0xq/sf6GHxTWVlXb9PcmutueRbmXhLcKg83J0Y0StiPXtjIL8\r\n" \
Janos Follath 0:1f7c5025e59d 107 "XSddW3Bh6fPi7n14Qy+W6KZwu9AtybanRlvePabyRSRpdOpWVQ7u30w5XZsSed6S\r\n" \
Janos Follath 0:1f7c5025e59d 108 "6BI0BBC68m2qqje1sInoqdCdXKtcB31TytUDNEHM+UuAyM8iGeGS2hCNqZlycHTS\r\n" \
Janos Follath 0:1f7c5025e59d 109 "jQ9KEsdMH3YLu0lQgRpWtxmg+VL6ROWwmAtKF12EwbDYZ+uoVl69OkQnCpv8pxKa\r\n" \
Janos Follath 0:1f7c5025e59d 110 "ec/4m6V+uEA1AOpaAMorHG3fH31IKWC/fTZstovgO/eG2XCtlbcCoWCQ7amFq16l\r\n" \
Janos Follath 0:1f7c5025e59d 111 "Gh1UKeBHxMXpDj4oDmIUGUvgzSNnEeSN/v76losWvWYQDjXR/LMDa/CNYsD8BmJR\r\n" \
Janos Follath 0:1f7c5025e59d 112 "PZidIjIXdVRlYOhA7ljtySQvp6RBujBfw3tsVMyZw2XzXFwM9O89b1xXC6+M5jf9\r\n" \
Janos Follath 0:1f7c5025e59d 113 "DXs/U7Fw+J9qq/YpByABcPCwWdttwdQFRbOxwxaSOKarIqS87TW1JuFcNJ59Ut6G\r\n" \
Janos Follath 0:1f7c5025e59d 114 "kMvAg6gC34U+0ktkG/AmI1hgjC+P7ErHCXBR2xARoGzcO/CMZF59S+Z2HFchpTSP\r\n" \
Janos Follath 0:1f7c5025e59d 115 "5T2o4mGy3VfHSBidQQrcZRukg8ZP8M1NF3bXjpY6QZpeLHc4oHECggEBAMjdgzzk\r\n" \
Janos Follath 0:1f7c5025e59d 116 "xp4mIYFxAEiXYt7tzuUXJk+0UpEJj5uboWLirUZqZmNUPyh6WDnzlREBH++Ms0LO\r\n" \
Janos Follath 0:1f7c5025e59d 117 "+AWSfaGPDoMb0NE2j3c4FRWAhe7Vn6lj7nLVpF2RdwRo88yGerZ4uwGMY8NUQCtn\r\n" \
Janos Follath 0:1f7c5025e59d 118 "zum3J7eCJ5DojiceRb6uMxTJ8xZmUC4W2f3J/lrR7wlYjyVnnHqH5HcemYUipWSw\r\n" \
Janos Follath 0:1f7c5025e59d 119 "sM0/cHp3lrz2VWrbAEu8HVpklvDQpdAgl7cjXt/JHYawY+p426IF/PzQSRROnzgy\r\n" \
Janos Follath 0:1f7c5025e59d 120 "4WI8FVYNV2tgu0TOFURbkkEvuj/duDKeooUIF0G0XHzha5oAX/j0iWiHbrOF6wHj\r\n" \
Janos Follath 0:1f7c5025e59d 121 "0xeajL9msKBnmD8CggEBAMSgLWmv7G31x4tndJCcXnX4AyVL7KpygAx/ZwCcyTR8\r\n" \
Janos Follath 0:1f7c5025e59d 122 "rY1rO07f/ta2noEra/xmEW/BW98qJFCHSU2nSLAQ5FpFSWyuQqrnffrMJnfWyvpr\r\n" \
Janos Follath 0:1f7c5025e59d 123 "ceQ0yQ/MiA6/JIOvGAjabcspzZijxzGp+Qk3eTT0yOXLSVOCH9B9XVHLodcy4PQM\r\n" \
Janos Follath 0:1f7c5025e59d 124 "KSCxy0vVHhVNl2SdPEwTXRmxk99Q/rw6IHVpQxBq1OhQt05nTKT+rZMD/grSK22e\r\n" \
Janos Follath 0:1f7c5025e59d 125 "my2F0DodAJwLo063Zv3RXQZhDYodMmjcp9Hqrtvj9P3HD7J3z6ACiV3SCi8cZumL\r\n" \
Janos Follath 0:1f7c5025e59d 126 "bSmnKCcd0bb45+aOWm31ieECJuIcJ9rOREEa/KDYTCsCggEBAMG5WkSVhLWsou37\r\n" \
Janos Follath 0:1f7c5025e59d 127 "dUGNuA63nq42SH3gtS0q4nU6gUkkw+dA4ST1cMByVrr1oRQ4WHup4I4TnQOKyF3T\r\n" \
Janos Follath 0:1f7c5025e59d 128 "4jQy1I+ipnVeAn+tZ/7zyzwMpEHeqNqRXA9FxbTBEoMAJ6QTqXgOvqDeSqIAQm7r\r\n" \
Janos Follath 0:1f7c5025e59d 129 "OYu5rrgtqyh/S8bGCwvUe4ooAfCSKx2ekYMbBVwW9MT8YS09tuS/iHJ3Mt2RTMLg\r\n" \
Janos Follath 0:1f7c5025e59d 130 "qeHvVmxrcXqZoFm44Ba7tN/pP0mi9HKyviZT4tmV3IYEbn3JyGGsfkUuVU9wEUfg\r\n" \
Janos Follath 0:1f7c5025e59d 131 "MCrgrVxrwfketAzooiHMjkVL2ASjzAJTmEvdAPETYXxzJD9LN0ovY3t8JfAC37IN\r\n" \
Janos Follath 0:1f7c5025e59d 132 "sVXS8/MCggEBALByOS59Y4Ktq1rLBQx8djwQyuneP0wZohUVAx7Gk7xZIfklQDyg\r\n" \
Janos Follath 0:1f7c5025e59d 133 "v/R4PrcVezstcPpDnykdjScCsGJR+uWc0v667I/ttP/e6utz5hVmmBGu965dPAzE\r\n" \
Janos Follath 0:1f7c5025e59d 134 "c1ggaSkOqFfRg/Nr2Qbf+fH0YPnHYSqHe/zSt0OMIvaaeXLcdKhEDSCUBRhE1HWB\r\n" \
Janos Follath 0:1f7c5025e59d 135 "kxR046WzgBeYzNQwycz9xwqsctJKGpeR9ute+5ANHPd3X9XtID0fqz8ctI5eZaSw\r\n" \
Janos Follath 0:1f7c5025e59d 136 "wApIW01ZQcAF8B+4WkkVuFXnpWW33yCOaRyPVOPHpnclr5WU1fS+3Q85QkW9rkej\r\n" \
Janos Follath 0:1f7c5025e59d 137 "97zlkl0QY9AHJqrXnoML1ywAK7ns+MVyNK8CggEAf62xcKZhOb1djeF72Ms+i/i/\r\n" \
Janos Follath 0:1f7c5025e59d 138 "WIAq4Q4YpsElgvJTHpNH2v9g4ngSTKe3ws3bGc502sWRlhcoTFMOW2rJNe/iqKkb\r\n" \
Janos Follath 0:1f7c5025e59d 139 "3cdeTkseDbpqozmJWz9dJWSVtXas2bZjzBEa//gQ7nHGVeQdqZJQ9rxPsoOAkfpi\r\n" \
Janos Follath 0:1f7c5025e59d 140 "qCFrmfUVUqC53e3XMt8+W+aSvKl+JZiB9ozkO9A6Q0vfQLKtjUMdQE3XaCFQT8DI\r\n" \
Janos Follath 0:1f7c5025e59d 141 "smaLBlBmeRaBpc02ENeC4ADlWosm1SwgxqMhuh2Alba/GrHOoPlVl4hDs9Fb5a6R\r\n" \
Janos Follath 0:1f7c5025e59d 142 "rmpXSt07GAxnG6j9jssA95E4rc1zO0CVKG5bvjVTxwi/sT0/VVX7VsJM4uTAQg==\r\n" \
Janos Follath 0:1f7c5025e59d 143 "-----END RSA PRIVATE KEY-----\r\n"
Janos Follath 0:1f7c5025e59d 144
Janos Follath 0:1f7c5025e59d 145 /*
mbed_official 11:6ccae3ebafd5 146 * Uncomment this line to enable ECDSA benchmark.
mbed_official 11:6ccae3ebafd5 147 */
mbed_official 11:6ccae3ebafd5 148 //#define ENABLE_ECDSA
mbed_official 11:6ccae3ebafd5 149
mbed_official 11:6ccae3ebafd5 150 /*
Janos Follath 0:1f7c5025e59d 151 * For heap usage estimates, we need an estimate of the overhead per allocated
Janos Follath 0:1f7c5025e59d 152 * block. ptmalloc2/3 (used in gnu libc for instance) uses 2 size_t per block,
Janos Follath 0:1f7c5025e59d 153 * so use that as our baseline.
Janos Follath 0:1f7c5025e59d 154 */
Janos Follath 0:1f7c5025e59d 155 #define MEM_BLOCK_OVERHEAD ( 2 * sizeof( size_t ) )
Janos Follath 0:1f7c5025e59d 156
Janos Follath 0:1f7c5025e59d 157 /*
Janos Follath 0:1f7c5025e59d 158 * Size to use for the malloc buffer if MEMORY_BUFFER_ALLOC_C is defined.
Janos Follath 0:1f7c5025e59d 159 */
Janos Follath 0:1f7c5025e59d 160 #define HEAP_SIZE (1u << 16) // 64k
Janos Follath 0:1f7c5025e59d 161
Janos Follath 0:1f7c5025e59d 162 #define BUFSIZE 1024
Janos Follath 0:1f7c5025e59d 163 #define HEADER_FORMAT " %-24s : "
Janos Follath 0:1f7c5025e59d 164 #define TITLE_LEN 25
Janos Follath 0:1f7c5025e59d 165
Janos Follath 0:1f7c5025e59d 166 #define OPTIONS \
Janos Follath 0:1f7c5025e59d 167 "md4, md5, ripemd160, sha1, sha256, sha512,\r\n" \
mbed_official 11:6ccae3ebafd5 168 "arc4, camellia, blowfish,\r\n" \
mbed_official 30:e0ea8c1ef9f5 169 "des3, des, aes_cmac, des3_cmac, aes_cbc, \r\n" \
mbed_official 30:e0ea8c1ef9f5 170 "aes_ctr, aes_gcm, aes_ccm,\r\n" \
mbed_official 30:e0ea8c1ef9f5 171 "havege, ctr_drbg, hmac_drbg,\r\n" \
Janos Follath 0:1f7c5025e59d 172 "rsa, dhm, ecdsa, ecdh.\r\n"
Janos Follath 0:1f7c5025e59d 173
Janos Follath 0:1f7c5025e59d 174 #if defined(MBEDTLS_ERROR_C)
Janos Follath 0:1f7c5025e59d 175 #define PRINT_ERROR \
Janos Follath 0:1f7c5025e59d 176 mbedtls_strerror( ret, ( char * )tmp, sizeof( tmp ) ); \
Janos Follath 0:1f7c5025e59d 177 mbedtls_printf( "FAILED: %s\r\n", tmp );
Janos Follath 0:1f7c5025e59d 178 #else
Janos Follath 0:1f7c5025e59d 179 #define PRINT_ERROR \
Janos Follath 0:1f7c5025e59d 180 mbedtls_printf( "FAILED: -0x%04x\r\n", -ret );
Janos Follath 0:1f7c5025e59d 181 #endif
Janos Follath 0:1f7c5025e59d 182
Janos Follath 0:1f7c5025e59d 183 static volatile int alarmed;
Janos Follath 0:1f7c5025e59d 184 static void alarm() { alarmed = 1; }
Janos Follath 0:1f7c5025e59d 185
Janos Follath 0:1f7c5025e59d 186 #define TIME_AND_TSC( TITLE, CODE ) \
Janos Follath 0:1f7c5025e59d 187 do { \
mbed_official 33:0af60cd5226d 188 unsigned long i; \
Janos Follath 0:1f7c5025e59d 189 Timeout t; \
Janos Follath 0:1f7c5025e59d 190 \
Janos Follath 0:1f7c5025e59d 191 mbedtls_printf( HEADER_FORMAT, TITLE ); \
Janos Follath 0:1f7c5025e59d 192 \
Janos Follath 0:1f7c5025e59d 193 for( i = 1, alarmed = 0, t.attach( alarm, 1.0 ); !alarmed; i++ ) \
Janos Follath 0:1f7c5025e59d 194 { \
Janos Follath 0:1f7c5025e59d 195 CODE; \
Janos Follath 0:1f7c5025e59d 196 } \
Janos Follath 0:1f7c5025e59d 197 \
mbed_official 33:0af60cd5226d 198 mbedtls_printf( "%9lu KB/s\r\n", \
mbed_official 33:0af60cd5226d 199 i * BUFSIZE / 1024 ); \
Janos Follath 0:1f7c5025e59d 200 } while( 0 )
Janos Follath 0:1f7c5025e59d 201
Janos Follath 0:1f7c5025e59d 202 #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && defined(MBEDTLS_MEMORY_DEBUG)
Janos Follath 0:1f7c5025e59d 203
Janos Follath 0:1f7c5025e59d 204 #define MEMORY_MEASURE_INIT \
Janos Follath 0:1f7c5025e59d 205 size_t max_used, max_blocks, max_bytes; \
Janos Follath 0:1f7c5025e59d 206 size_t prv_used, prv_blocks; \
Janos Follath 0:1f7c5025e59d 207 mbedtls_memory_buffer_alloc_cur_get( &prv_used, &prv_blocks ); \
Janos Follath 0:1f7c5025e59d 208 mbedtls_memory_buffer_alloc_max_reset( );
Janos Follath 0:1f7c5025e59d 209
Janos Follath 0:1f7c5025e59d 210 #define MEMORY_MEASURE_PRINT( title_len ) \
Janos Follath 0:1f7c5025e59d 211 mbedtls_memory_buffer_alloc_max_get( &max_used, &max_blocks ); \
Janos Follath 0:1f7c5025e59d 212 for( i = 12 - title_len; i != 0; i-- ) mbedtls_printf( " " ); \
Janos Follath 0:1f7c5025e59d 213 max_used -= prv_used; \
Janos Follath 0:1f7c5025e59d 214 max_blocks -= prv_blocks; \
Janos Follath 0:1f7c5025e59d 215 max_bytes = max_used + MEM_BLOCK_OVERHEAD * max_blocks; \
Janos Follath 0:1f7c5025e59d 216 mbedtls_printf( "%6u heap bytes", (unsigned) max_bytes );
Janos Follath 0:1f7c5025e59d 217
Janos Follath 0:1f7c5025e59d 218 #else
Janos Follath 0:1f7c5025e59d 219 #define MEMORY_MEASURE_INIT
Janos Follath 0:1f7c5025e59d 220 #define MEMORY_MEASURE_PRINT( title_len )
Janos Follath 0:1f7c5025e59d 221 #endif
Janos Follath 0:1f7c5025e59d 222
Janos Follath 0:1f7c5025e59d 223 #define TIME_PUBLIC( TITLE, TYPE, CODE ) \
Janos Follath 0:1f7c5025e59d 224 do { \
Janos Follath 0:1f7c5025e59d 225 unsigned long ms; \
Janos Follath 0:1f7c5025e59d 226 int ret = 0; \
Janos Follath 0:1f7c5025e59d 227 Timer t; \
Janos Follath 0:1f7c5025e59d 228 MEMORY_MEASURE_INIT; \
Janos Follath 0:1f7c5025e59d 229 \
Janos Follath 0:1f7c5025e59d 230 mbedtls_printf( HEADER_FORMAT, TITLE ); \
Janos Follath 0:1f7c5025e59d 231 fflush( stdout ); \
Janos Follath 0:1f7c5025e59d 232 \
Janos Follath 0:1f7c5025e59d 233 t.start(); \
Janos Follath 0:1f7c5025e59d 234 CODE; \
Janos Follath 0:1f7c5025e59d 235 t.stop(); \
Janos Follath 0:1f7c5025e59d 236 ms = t.read_ms(); \
Janos Follath 0:1f7c5025e59d 237 \
Janos Follath 0:1f7c5025e59d 238 if( ret != 0 ) \
Janos Follath 0:1f7c5025e59d 239 { \
Janos Follath 0:1f7c5025e59d 240 PRINT_ERROR; \
Janos Follath 0:1f7c5025e59d 241 } \
Janos Follath 0:1f7c5025e59d 242 else \
Janos Follath 0:1f7c5025e59d 243 { \
Janos Follath 0:1f7c5025e59d 244 mbedtls_printf( "%6lu ms/" TYPE, ms ); \
Janos Follath 0:1f7c5025e59d 245 MEMORY_MEASURE_PRINT( sizeof( TYPE ) + 1 ); \
Janos Follath 0:1f7c5025e59d 246 mbedtls_printf( "\r\n" ); \
Janos Follath 0:1f7c5025e59d 247 } \
Janos Follath 0:1f7c5025e59d 248 } while( 0 )
Janos Follath 0:1f7c5025e59d 249
Janos Follath 0:1f7c5025e59d 250 static int myrand( void *rng_state, unsigned char *output, size_t len )
Janos Follath 0:1f7c5025e59d 251 {
Janos Follath 0:1f7c5025e59d 252 size_t use_len;
Janos Follath 0:1f7c5025e59d 253 int rnd;
Janos Follath 0:1f7c5025e59d 254
Janos Follath 0:1f7c5025e59d 255 if( rng_state != NULL )
Janos Follath 0:1f7c5025e59d 256 rng_state = NULL;
Janos Follath 0:1f7c5025e59d 257
Janos Follath 0:1f7c5025e59d 258 while( len > 0 )
Janos Follath 0:1f7c5025e59d 259 {
Janos Follath 0:1f7c5025e59d 260 use_len = len;
Janos Follath 0:1f7c5025e59d 261 if( use_len > sizeof(int) )
Janos Follath 0:1f7c5025e59d 262 use_len = sizeof(int);
Janos Follath 0:1f7c5025e59d 263
Janos Follath 0:1f7c5025e59d 264 rnd = rand();
Janos Follath 0:1f7c5025e59d 265 memcpy( output, &rnd, use_len );
Janos Follath 0:1f7c5025e59d 266 output += use_len;
Janos Follath 0:1f7c5025e59d 267 len -= use_len;
Janos Follath 0:1f7c5025e59d 268 }
Janos Follath 0:1f7c5025e59d 269
Janos Follath 0:1f7c5025e59d 270 return( 0 );
Janos Follath 0:1f7c5025e59d 271 }
Janos Follath 0:1f7c5025e59d 272
Janos Follath 0:1f7c5025e59d 273 /*
Janos Follath 0:1f7c5025e59d 274 * Clear some memory that was used to prepare the context
Janos Follath 0:1f7c5025e59d 275 */
Janos Follath 0:1f7c5025e59d 276 #if defined(MBEDTLS_ECP_C)
Janos Follath 0:1f7c5025e59d 277 void ecp_clear_precomputed( mbedtls_ecp_group *grp )
Janos Follath 0:1f7c5025e59d 278 {
Janos Follath 0:1f7c5025e59d 279 if( grp->T != NULL )
Janos Follath 0:1f7c5025e59d 280 {
Janos Follath 0:1f7c5025e59d 281 size_t i;
Janos Follath 0:1f7c5025e59d 282 for( i = 0; i < grp->T_size; i++ )
Janos Follath 0:1f7c5025e59d 283 mbedtls_ecp_point_free( &grp->T[i] );
Janos Follath 0:1f7c5025e59d 284 mbedtls_free( grp->T );
Janos Follath 0:1f7c5025e59d 285 }
Janos Follath 0:1f7c5025e59d 286 grp->T = NULL;
Janos Follath 0:1f7c5025e59d 287 grp->T_size = 0;
Janos Follath 0:1f7c5025e59d 288 }
Janos Follath 0:1f7c5025e59d 289 #else
Janos Follath 0:1f7c5025e59d 290 #define ecp_clear_precomputed( g )
Janos Follath 0:1f7c5025e59d 291 #endif
Janos Follath 0:1f7c5025e59d 292
Janos Follath 0:1f7c5025e59d 293 unsigned char buf[BUFSIZE];
Janos Follath 0:1f7c5025e59d 294
Janos Follath 0:1f7c5025e59d 295 typedef struct {
Janos Follath 0:1f7c5025e59d 296 char md4, md5, ripemd160, sha1, sha256, sha512,
mbed_official 30:e0ea8c1ef9f5 297 arc4, des3, des, aes_cbc, aes_ctr, aes_gcm, aes_ccm,
mbed_official 11:6ccae3ebafd5 298 aes_cmac, des3_cmac, camellia, blowfish,
Janos Follath 0:1f7c5025e59d 299 havege, ctr_drbg, hmac_drbg,
Janos Follath 0:1f7c5025e59d 300 rsa, dhm, ecdsa, ecdh;
Janos Follath 0:1f7c5025e59d 301 } todo_list;
Janos Follath 0:1f7c5025e59d 302
Janos Follath 0:1f7c5025e59d 303 static int benchmark( int argc, char *argv[] )
Janos Follath 0:1f7c5025e59d 304 {
Janos Follath 0:1f7c5025e59d 305 int i;
Janos Follath 0:1f7c5025e59d 306 unsigned char tmp[200];
Janos Follath 0:1f7c5025e59d 307 char title[TITLE_LEN];
Janos Follath 0:1f7c5025e59d 308 todo_list todo;
Janos Follath 0:1f7c5025e59d 309 #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Janos Follath 0:1f7c5025e59d 310 unsigned char malloc_buf[HEAP_SIZE] = { 0 };
Janos Follath 0:1f7c5025e59d 311 #endif
Janos Follath 0:1f7c5025e59d 312
Janos Follath 0:1f7c5025e59d 313 if( argc <= 1 )
Janos Follath 0:1f7c5025e59d 314 {
Janos Follath 0:1f7c5025e59d 315 memset( &todo, 1, sizeof( todo ) );
Janos Follath 0:1f7c5025e59d 316 }
Janos Follath 0:1f7c5025e59d 317 else
Janos Follath 0:1f7c5025e59d 318 {
Janos Follath 0:1f7c5025e59d 319 memset( &todo, 0, sizeof( todo ) );
Janos Follath 0:1f7c5025e59d 320
Janos Follath 0:1f7c5025e59d 321 for( i = 1; i < argc; i++ )
Janos Follath 0:1f7c5025e59d 322 {
Janos Follath 0:1f7c5025e59d 323 if( strcmp( argv[i], "md4" ) == 0 )
Janos Follath 0:1f7c5025e59d 324 todo.md4 = 1;
Janos Follath 0:1f7c5025e59d 325 else if( strcmp( argv[i], "md5" ) == 0 )
Janos Follath 0:1f7c5025e59d 326 todo.md5 = 1;
Janos Follath 0:1f7c5025e59d 327 else if( strcmp( argv[i], "ripemd160" ) == 0 )
Janos Follath 0:1f7c5025e59d 328 todo.ripemd160 = 1;
Janos Follath 0:1f7c5025e59d 329 else if( strcmp( argv[i], "sha1" ) == 0 )
Janos Follath 0:1f7c5025e59d 330 todo.sha1 = 1;
Janos Follath 0:1f7c5025e59d 331 else if( strcmp( argv[i], "sha256" ) == 0 )
Janos Follath 0:1f7c5025e59d 332 todo.sha256 = 1;
Janos Follath 0:1f7c5025e59d 333 else if( strcmp( argv[i], "sha512" ) == 0 )
Janos Follath 0:1f7c5025e59d 334 todo.sha512 = 1;
Janos Follath 0:1f7c5025e59d 335 else if( strcmp( argv[i], "arc4" ) == 0 )
Janos Follath 0:1f7c5025e59d 336 todo.arc4 = 1;
Janos Follath 0:1f7c5025e59d 337 else if( strcmp( argv[i], "des3" ) == 0 )
Janos Follath 0:1f7c5025e59d 338 todo.des3 = 1;
Janos Follath 0:1f7c5025e59d 339 else if( strcmp( argv[i], "des" ) == 0 )
Janos Follath 0:1f7c5025e59d 340 todo.des = 1;
Janos Follath 0:1f7c5025e59d 341 else if( strcmp( argv[i], "aes_cbc" ) == 0 )
Janos Follath 0:1f7c5025e59d 342 todo.aes_cbc = 1;
mbed_official 30:e0ea8c1ef9f5 343 else if( strcmp( argv[i], "aes_ctr" ) == 0 )
mbed_official 30:e0ea8c1ef9f5 344 todo.aes_ctr = 1;
Janos Follath 0:1f7c5025e59d 345 else if( strcmp( argv[i], "aes_gcm" ) == 0 )
Janos Follath 0:1f7c5025e59d 346 todo.aes_gcm = 1;
Janos Follath 0:1f7c5025e59d 347 else if( strcmp( argv[i], "aes_ccm" ) == 0 )
Janos Follath 0:1f7c5025e59d 348 todo.aes_ccm = 1;
mbed_official 11:6ccae3ebafd5 349 else if( strcmp( argv[i], "aes_cmac" ) == 0 )
mbed_official 11:6ccae3ebafd5 350 todo.aes_cmac = 1;
mbed_official 11:6ccae3ebafd5 351 else if( strcmp( argv[i], "des3_cmac" ) == 0 )
mbed_official 11:6ccae3ebafd5 352 todo.des3_cmac = 1;
Janos Follath 0:1f7c5025e59d 353 else if( strcmp( argv[i], "camellia" ) == 0 )
Janos Follath 0:1f7c5025e59d 354 todo.camellia = 1;
Janos Follath 0:1f7c5025e59d 355 else if( strcmp( argv[i], "blowfish" ) == 0 )
Janos Follath 0:1f7c5025e59d 356 todo.blowfish = 1;
Janos Follath 0:1f7c5025e59d 357 else if( strcmp( argv[i], "havege" ) == 0 )
Janos Follath 0:1f7c5025e59d 358 todo.havege = 1;
Janos Follath 0:1f7c5025e59d 359 else if( strcmp( argv[i], "ctr_drbg" ) == 0 )
Janos Follath 0:1f7c5025e59d 360 todo.ctr_drbg = 1;
Janos Follath 0:1f7c5025e59d 361 else if( strcmp( argv[i], "hmac_drbg" ) == 0 )
Janos Follath 0:1f7c5025e59d 362 todo.hmac_drbg = 1;
Janos Follath 0:1f7c5025e59d 363 else if( strcmp( argv[i], "rsa" ) == 0 )
Janos Follath 0:1f7c5025e59d 364 todo.rsa = 1;
Janos Follath 0:1f7c5025e59d 365 else if( strcmp( argv[i], "dhm" ) == 0 )
Janos Follath 0:1f7c5025e59d 366 todo.dhm = 1;
Janos Follath 0:1f7c5025e59d 367 else if( strcmp( argv[i], "ecdsa" ) == 0 )
Janos Follath 0:1f7c5025e59d 368 todo.ecdsa = 1;
Janos Follath 0:1f7c5025e59d 369 else if( strcmp( argv[i], "ecdh" ) == 0 )
Janos Follath 0:1f7c5025e59d 370 todo.ecdh = 1;
Janos Follath 0:1f7c5025e59d 371 else
Janos Follath 0:1f7c5025e59d 372 {
Janos Follath 0:1f7c5025e59d 373 mbedtls_printf( "Unrecognized option: %s\r\n", argv[i] );
Janos Follath 0:1f7c5025e59d 374 mbedtls_printf( "Available options: " OPTIONS );
Janos Follath 0:1f7c5025e59d 375 }
Janos Follath 0:1f7c5025e59d 376 }
Janos Follath 0:1f7c5025e59d 377 }
Janos Follath 0:1f7c5025e59d 378
Janos Follath 0:1f7c5025e59d 379 mbedtls_printf( "\r\n\r\n" );
Janos Follath 0:1f7c5025e59d 380
Janos Follath 0:1f7c5025e59d 381 #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Janos Follath 0:1f7c5025e59d 382 mbedtls_memory_buffer_alloc_init( malloc_buf, sizeof( malloc_buf ) );
Janos Follath 0:1f7c5025e59d 383 #endif
Janos Follath 0:1f7c5025e59d 384 memset( buf, 0xAA, sizeof( buf ) );
Janos Follath 0:1f7c5025e59d 385 memset( tmp, 0xBB, sizeof( tmp ) );
Janos Follath 0:1f7c5025e59d 386
Janos Follath 0:1f7c5025e59d 387 #if defined(MBEDTLS_MD4_C)
Janos Follath 0:1f7c5025e59d 388 if( todo.md4 )
Janos Follath 0:1f7c5025e59d 389 TIME_AND_TSC( "MD4", mbedtls_md4( buf, BUFSIZE, tmp ) );
Janos Follath 0:1f7c5025e59d 390 #endif
Janos Follath 0:1f7c5025e59d 391
Janos Follath 0:1f7c5025e59d 392 #if defined(MBEDTLS_MD5_C)
Janos Follath 0:1f7c5025e59d 393 if( todo.md5 )
Janos Follath 0:1f7c5025e59d 394 TIME_AND_TSC( "MD5", mbedtls_md5( buf, BUFSIZE, tmp ) );
Janos Follath 0:1f7c5025e59d 395 #endif
Janos Follath 0:1f7c5025e59d 396
Janos Follath 0:1f7c5025e59d 397 #if defined(MBEDTLS_RIPEMD160_C)
Janos Follath 0:1f7c5025e59d 398 if( todo.ripemd160 )
Janos Follath 0:1f7c5025e59d 399 TIME_AND_TSC( "RIPEMD160", mbedtls_ripemd160( buf, BUFSIZE, tmp ) );
Janos Follath 0:1f7c5025e59d 400 #endif
Janos Follath 0:1f7c5025e59d 401
Janos Follath 0:1f7c5025e59d 402 #if defined(MBEDTLS_SHA1_C)
Janos Follath 0:1f7c5025e59d 403 if( todo.sha1 )
Janos Follath 0:1f7c5025e59d 404 TIME_AND_TSC( "SHA-1", mbedtls_sha1( buf, BUFSIZE, tmp ) );
Janos Follath 0:1f7c5025e59d 405 #endif
Janos Follath 0:1f7c5025e59d 406
Janos Follath 0:1f7c5025e59d 407 #if defined(MBEDTLS_SHA256_C)
Janos Follath 0:1f7c5025e59d 408 if( todo.sha256 )
Janos Follath 0:1f7c5025e59d 409 TIME_AND_TSC( "SHA-256", mbedtls_sha256( buf, BUFSIZE, tmp, 0 ) );
Janos Follath 0:1f7c5025e59d 410 #endif
Janos Follath 0:1f7c5025e59d 411
Janos Follath 0:1f7c5025e59d 412 #if defined(MBEDTLS_SHA512_C)
Janos Follath 0:1f7c5025e59d 413 if( todo.sha512 )
Janos Follath 0:1f7c5025e59d 414 TIME_AND_TSC( "SHA-512", mbedtls_sha512( buf, BUFSIZE, tmp, 0 ) );
Janos Follath 0:1f7c5025e59d 415 #endif
Janos Follath 0:1f7c5025e59d 416
Janos Follath 0:1f7c5025e59d 417 #if defined(MBEDTLS_ARC4_C)
Janos Follath 0:1f7c5025e59d 418 if( todo.arc4 )
Janos Follath 0:1f7c5025e59d 419 {
Janos Follath 0:1f7c5025e59d 420 mbedtls_arc4_context arc4;
Janos Follath 0:1f7c5025e59d 421 mbedtls_arc4_init( &arc4 );
Janos Follath 0:1f7c5025e59d 422 mbedtls_arc4_setup( &arc4, tmp, 32 );
Janos Follath 0:1f7c5025e59d 423 TIME_AND_TSC( "ARC4", mbedtls_arc4_crypt( &arc4, BUFSIZE, buf, buf ) );
Janos Follath 0:1f7c5025e59d 424 mbedtls_arc4_free( &arc4 );
Janos Follath 0:1f7c5025e59d 425 }
Janos Follath 0:1f7c5025e59d 426 #endif
Janos Follath 0:1f7c5025e59d 427
Janos Follath 0:1f7c5025e59d 428 #if defined(MBEDTLS_DES_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
Janos Follath 0:1f7c5025e59d 429 if( todo.des3 )
Janos Follath 0:1f7c5025e59d 430 {
Janos Follath 0:1f7c5025e59d 431 mbedtls_des3_context des3;
Janos Follath 0:1f7c5025e59d 432 mbedtls_des3_init( &des3 );
Janos Follath 0:1f7c5025e59d 433 mbedtls_des3_set3key_enc( &des3, tmp );
Janos Follath 0:1f7c5025e59d 434 TIME_AND_TSC( "3DES",
Janos Follath 0:1f7c5025e59d 435 mbedtls_des3_crypt_cbc( &des3, MBEDTLS_DES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
Janos Follath 0:1f7c5025e59d 436 mbedtls_des3_free( &des3 );
Janos Follath 0:1f7c5025e59d 437 }
Janos Follath 0:1f7c5025e59d 438
Janos Follath 0:1f7c5025e59d 439 if( todo.des )
Janos Follath 0:1f7c5025e59d 440 {
Janos Follath 0:1f7c5025e59d 441 mbedtls_des_context des;
Janos Follath 0:1f7c5025e59d 442 mbedtls_des_init( &des );
Janos Follath 0:1f7c5025e59d 443 mbedtls_des_setkey_enc( &des, tmp );
Janos Follath 0:1f7c5025e59d 444 TIME_AND_TSC( "DES",
Janos Follath 0:1f7c5025e59d 445 mbedtls_des_crypt_cbc( &des, MBEDTLS_DES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
Janos Follath 0:1f7c5025e59d 446 mbedtls_des_free( &des );
Janos Follath 0:1f7c5025e59d 447 }
mbed_official 11:6ccae3ebafd5 448 #if defined(MBEDTLS_CMAC_C)
mbed_official 11:6ccae3ebafd5 449 if( todo.des3_cmac )
mbed_official 11:6ccae3ebafd5 450 {
mbed_official 11:6ccae3ebafd5 451 unsigned char output[8];
mbed_official 11:6ccae3ebafd5 452 const mbedtls_cipher_info_t *cipher_info;
mbed_official 11:6ccae3ebafd5 453
mbed_official 11:6ccae3ebafd5 454 memset( buf, 0, sizeof( buf ) );
mbed_official 11:6ccae3ebafd5 455 memset( tmp, 0, sizeof( tmp ) );
mbed_official 11:6ccae3ebafd5 456
mbed_official 11:6ccae3ebafd5 457 cipher_info = mbedtls_cipher_info_from_type( MBEDTLS_CIPHER_DES_EDE3_ECB );
mbed_official 11:6ccae3ebafd5 458
mbed_official 11:6ccae3ebafd5 459 TIME_AND_TSC( "3DES-CMAC",
mbed_official 11:6ccae3ebafd5 460 mbedtls_cipher_cmac( cipher_info, tmp, 192, buf,
mbed_official 11:6ccae3ebafd5 461 BUFSIZE, output ) );
mbed_official 11:6ccae3ebafd5 462 }
mbed_official 11:6ccae3ebafd5 463 #endif /* MBEDTLS_CMAC_C */
Janos Follath 0:1f7c5025e59d 464 #endif
Janos Follath 0:1f7c5025e59d 465
Janos Follath 0:1f7c5025e59d 466 #if defined(MBEDTLS_AES_C)
Janos Follath 0:1f7c5025e59d 467 #if defined(MBEDTLS_CIPHER_MODE_CBC)
Janos Follath 0:1f7c5025e59d 468 if( todo.aes_cbc )
Janos Follath 0:1f7c5025e59d 469 {
Janos Follath 0:1f7c5025e59d 470 int keysize;
Janos Follath 0:1f7c5025e59d 471 mbedtls_aes_context aes;
Janos Follath 0:1f7c5025e59d 472 mbedtls_aes_init( &aes );
Janos Follath 0:1f7c5025e59d 473 for( keysize = 128; keysize <= 256; keysize += 64 )
Janos Follath 0:1f7c5025e59d 474 {
Janos Follath 0:1f7c5025e59d 475 mbedtls_snprintf( title, sizeof( title ), "AES-CBC-%d", keysize );
Janos Follath 0:1f7c5025e59d 476
Janos Follath 0:1f7c5025e59d 477 memset( buf, 0, sizeof( buf ) );
Janos Follath 0:1f7c5025e59d 478 memset( tmp, 0, sizeof( tmp ) );
Janos Follath 0:1f7c5025e59d 479 mbedtls_aes_setkey_enc( &aes, tmp, keysize );
Janos Follath 0:1f7c5025e59d 480
Janos Follath 0:1f7c5025e59d 481 TIME_AND_TSC( title,
Janos Follath 0:1f7c5025e59d 482 mbedtls_aes_crypt_cbc( &aes, MBEDTLS_AES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
Janos Follath 0:1f7c5025e59d 483 }
Janos Follath 0:1f7c5025e59d 484 mbedtls_aes_free( &aes );
Janos Follath 0:1f7c5025e59d 485 }
Janos Follath 0:1f7c5025e59d 486 #endif
mbed_official 30:e0ea8c1ef9f5 487
mbed_official 30:e0ea8c1ef9f5 488 #if defined(MBEDTLS_CIPHER_MODE_CTR)
mbed_official 30:e0ea8c1ef9f5 489 if( todo.aes_ctr )
mbed_official 30:e0ea8c1ef9f5 490 {
mbed_official 30:e0ea8c1ef9f5 491 int keysize;
mbed_official 30:e0ea8c1ef9f5 492 size_t nc_offset = 0;
mbed_official 30:e0ea8c1ef9f5 493 unsigned char stream_block[16];
mbed_official 30:e0ea8c1ef9f5 494 mbedtls_aes_context aes;
mbed_official 30:e0ea8c1ef9f5 495 mbedtls_aes_init( &aes );
mbed_official 30:e0ea8c1ef9f5 496 for( keysize = 128; keysize <= 256; keysize += 64 )
mbed_official 30:e0ea8c1ef9f5 497 {
mbed_official 30:e0ea8c1ef9f5 498 mbedtls_snprintf( title, sizeof( title ), "AES-CTR-%d", keysize );
mbed_official 30:e0ea8c1ef9f5 499
mbed_official 30:e0ea8c1ef9f5 500 memset( buf, 0, sizeof( buf ) );
mbed_official 30:e0ea8c1ef9f5 501 memset( tmp, 0, sizeof( tmp ) );
mbed_official 30:e0ea8c1ef9f5 502 mbedtls_aes_setkey_enc( &aes, tmp, keysize );
mbed_official 30:e0ea8c1ef9f5 503
mbed_official 30:e0ea8c1ef9f5 504 TIME_AND_TSC( title,
mbed_official 30:e0ea8c1ef9f5 505 mbedtls_aes_crypt_ctr( &aes, BUFSIZE, &nc_offset, tmp, stream_block, buf, buf ) );
mbed_official 30:e0ea8c1ef9f5 506 }
mbed_official 30:e0ea8c1ef9f5 507 mbedtls_aes_free( &aes );
mbed_official 30:e0ea8c1ef9f5 508 }
mbed_official 30:e0ea8c1ef9f5 509 #endif
mbed_official 30:e0ea8c1ef9f5 510
Janos Follath 0:1f7c5025e59d 511 #if defined(MBEDTLS_GCM_C)
Janos Follath 0:1f7c5025e59d 512 if( todo.aes_gcm )
Janos Follath 0:1f7c5025e59d 513 {
Janos Follath 0:1f7c5025e59d 514 int keysize;
Janos Follath 0:1f7c5025e59d 515 mbedtls_gcm_context gcm;
Janos Follath 0:1f7c5025e59d 516
Janos Follath 0:1f7c5025e59d 517 mbedtls_gcm_init( &gcm );
Janos Follath 0:1f7c5025e59d 518 for( keysize = 128; keysize <= 256; keysize += 64 )
Janos Follath 0:1f7c5025e59d 519 {
Janos Follath 0:1f7c5025e59d 520 mbedtls_snprintf( title, sizeof( title ), "AES-GCM-%d", keysize );
Janos Follath 0:1f7c5025e59d 521
Janos Follath 0:1f7c5025e59d 522 memset( buf, 0, sizeof( buf ) );
Janos Follath 0:1f7c5025e59d 523 memset( tmp, 0, sizeof( tmp ) );
Janos Follath 0:1f7c5025e59d 524 mbedtls_gcm_setkey( &gcm, MBEDTLS_CIPHER_ID_AES, tmp, keysize );
Janos Follath 0:1f7c5025e59d 525
Janos Follath 0:1f7c5025e59d 526 TIME_AND_TSC( title,
Janos Follath 0:1f7c5025e59d 527 mbedtls_gcm_crypt_and_tag( &gcm, MBEDTLS_GCM_ENCRYPT, BUFSIZE, tmp,
Janos Follath 0:1f7c5025e59d 528 12, NULL, 0, buf, buf, 16, tmp ) );
Janos Follath 0:1f7c5025e59d 529
Janos Follath 0:1f7c5025e59d 530 mbedtls_gcm_free( &gcm );
Janos Follath 0:1f7c5025e59d 531 }
Janos Follath 0:1f7c5025e59d 532 }
Janos Follath 0:1f7c5025e59d 533 #endif
Janos Follath 0:1f7c5025e59d 534 #if defined(MBEDTLS_CCM_C)
Janos Follath 0:1f7c5025e59d 535 if( todo.aes_ccm )
Janos Follath 0:1f7c5025e59d 536 {
Janos Follath 0:1f7c5025e59d 537 int keysize;
Janos Follath 0:1f7c5025e59d 538 mbedtls_ccm_context ccm;
Janos Follath 0:1f7c5025e59d 539
Janos Follath 0:1f7c5025e59d 540 mbedtls_ccm_init( &ccm );
Janos Follath 0:1f7c5025e59d 541 for( keysize = 128; keysize <= 256; keysize += 64 )
Janos Follath 0:1f7c5025e59d 542 {
Janos Follath 0:1f7c5025e59d 543 mbedtls_snprintf( title, sizeof( title ), "AES-CCM-%d", keysize );
Janos Follath 0:1f7c5025e59d 544
Janos Follath 0:1f7c5025e59d 545 memset( buf, 0, sizeof( buf ) );
Janos Follath 0:1f7c5025e59d 546 memset( tmp, 0, sizeof( tmp ) );
Janos Follath 0:1f7c5025e59d 547 mbedtls_ccm_setkey( &ccm, MBEDTLS_CIPHER_ID_AES, tmp, keysize );
Janos Follath 0:1f7c5025e59d 548
Janos Follath 0:1f7c5025e59d 549 TIME_AND_TSC( title,
Janos Follath 0:1f7c5025e59d 550 mbedtls_ccm_encrypt_and_tag( &ccm, BUFSIZE, tmp,
Janos Follath 0:1f7c5025e59d 551 12, NULL, 0, buf, buf, tmp, 16 ) );
Janos Follath 0:1f7c5025e59d 552
Janos Follath 0:1f7c5025e59d 553 mbedtls_ccm_free( &ccm );
Janos Follath 0:1f7c5025e59d 554 }
Janos Follath 0:1f7c5025e59d 555 }
Janos Follath 0:1f7c5025e59d 556 #endif
mbed_official 11:6ccae3ebafd5 557 #if defined(MBEDTLS_CMAC_C)
mbed_official 11:6ccae3ebafd5 558 if( todo.aes_cmac )
mbed_official 11:6ccae3ebafd5 559 {
mbed_official 11:6ccae3ebafd5 560 unsigned char output[16];
mbed_official 11:6ccae3ebafd5 561 const mbedtls_cipher_info_t *cipher_info;
mbed_official 11:6ccae3ebafd5 562 mbedtls_cipher_type_t cipher_type;
mbed_official 11:6ccae3ebafd5 563 int keysize;
mbed_official 11:6ccae3ebafd5 564
mbed_official 11:6ccae3ebafd5 565 cipher_type = MBEDTLS_CIPHER_AES_128_ECB;
mbed_official 11:6ccae3ebafd5 566 for( keysize = 128; keysize <= 256; keysize += 64 )
mbed_official 11:6ccae3ebafd5 567 {
mbed_official 11:6ccae3ebafd5 568 mbedtls_snprintf( title, sizeof( title ), "AES-CMAC-%d", keysize );
mbed_official 11:6ccae3ebafd5 569
mbed_official 11:6ccae3ebafd5 570 memset( buf, 0, sizeof( buf ) );
mbed_official 11:6ccae3ebafd5 571 memset( tmp, 0, sizeof( tmp ) );
mbed_official 11:6ccae3ebafd5 572
mbed_official 11:6ccae3ebafd5 573 cipher_info = mbedtls_cipher_info_from_type( cipher_type );
mbed_official 11:6ccae3ebafd5 574
mbed_official 11:6ccae3ebafd5 575 TIME_AND_TSC( title,
mbed_official 11:6ccae3ebafd5 576 mbedtls_cipher_cmac( cipher_info, tmp, keysize,
mbed_official 11:6ccae3ebafd5 577 buf, BUFSIZE, output ) );
mbed_official 11:6ccae3ebafd5 578 cipher_type = (mbedtls_cipher_type_t)( cipher_type + 1 );
mbed_official 11:6ccae3ebafd5 579 }
mbed_official 11:6ccae3ebafd5 580
mbed_official 11:6ccae3ebafd5 581 memset( buf, 0, sizeof( buf ) );
mbed_official 11:6ccae3ebafd5 582 memset( tmp, 0, sizeof( tmp ) );
mbed_official 11:6ccae3ebafd5 583 TIME_AND_TSC( "AES-CMAC-PRF-128",
mbed_official 11:6ccae3ebafd5 584 mbedtls_aes_cmac_prf_128( tmp, 16, buf, BUFSIZE,
mbed_official 11:6ccae3ebafd5 585 output ) );
mbed_official 11:6ccae3ebafd5 586 }
mbed_official 11:6ccae3ebafd5 587 #endif /* MBEDTLS_CMAC_C */
Janos Follath 0:1f7c5025e59d 588 #endif
Janos Follath 0:1f7c5025e59d 589
Janos Follath 0:1f7c5025e59d 590 #if defined(MBEDTLS_CAMELLIA_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
Janos Follath 0:1f7c5025e59d 591 if( todo.camellia )
Janos Follath 0:1f7c5025e59d 592 {
Janos Follath 0:1f7c5025e59d 593 int keysize;
Janos Follath 0:1f7c5025e59d 594 mbedtls_camellia_context camellia;
Janos Follath 0:1f7c5025e59d 595 mbedtls_camellia_init( &camellia );
Janos Follath 0:1f7c5025e59d 596 for( keysize = 128; keysize <= 256; keysize += 64 )
Janos Follath 0:1f7c5025e59d 597 {
Janos Follath 0:1f7c5025e59d 598 mbedtls_snprintf( title, sizeof( title ), "CAMELLIA-CBC-%d", keysize );
Janos Follath 0:1f7c5025e59d 599
Janos Follath 0:1f7c5025e59d 600 memset( buf, 0, sizeof( buf ) );
Janos Follath 0:1f7c5025e59d 601 memset( tmp, 0, sizeof( tmp ) );
Janos Follath 0:1f7c5025e59d 602 mbedtls_camellia_setkey_enc( &camellia, tmp, keysize );
Janos Follath 0:1f7c5025e59d 603
Janos Follath 0:1f7c5025e59d 604 TIME_AND_TSC( title,
Janos Follath 0:1f7c5025e59d 605 mbedtls_camellia_crypt_cbc( &camellia, MBEDTLS_CAMELLIA_ENCRYPT,
Janos Follath 0:1f7c5025e59d 606 BUFSIZE, tmp, buf, buf ) );
Janos Follath 0:1f7c5025e59d 607 }
Janos Follath 0:1f7c5025e59d 608 mbedtls_camellia_free( &camellia );
Janos Follath 0:1f7c5025e59d 609 }
Janos Follath 0:1f7c5025e59d 610 #endif
Janos Follath 0:1f7c5025e59d 611
Janos Follath 0:1f7c5025e59d 612 #if defined(MBEDTLS_BLOWFISH_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
Janos Follath 0:1f7c5025e59d 613 if( todo.blowfish )
Janos Follath 0:1f7c5025e59d 614 {
Janos Follath 0:1f7c5025e59d 615 int keysize;
Janos Follath 0:1f7c5025e59d 616 mbedtls_blowfish_context blowfish;
Janos Follath 0:1f7c5025e59d 617 mbedtls_blowfish_init( &blowfish );
Janos Follath 0:1f7c5025e59d 618
Janos Follath 0:1f7c5025e59d 619 for( keysize = 128; keysize <= 256; keysize += 64 )
Janos Follath 0:1f7c5025e59d 620 {
Janos Follath 0:1f7c5025e59d 621 mbedtls_snprintf( title, sizeof( title ), "BLOWFISH-CBC-%d", keysize );
Janos Follath 0:1f7c5025e59d 622
Janos Follath 0:1f7c5025e59d 623 memset( buf, 0, sizeof( buf ) );
Janos Follath 0:1f7c5025e59d 624 memset( tmp, 0, sizeof( tmp ) );
Janos Follath 0:1f7c5025e59d 625 mbedtls_blowfish_setkey( &blowfish, tmp, keysize );
Janos Follath 0:1f7c5025e59d 626
Janos Follath 0:1f7c5025e59d 627 TIME_AND_TSC( title,
Janos Follath 0:1f7c5025e59d 628 mbedtls_blowfish_crypt_cbc( &blowfish, MBEDTLS_BLOWFISH_ENCRYPT, BUFSIZE,
Janos Follath 0:1f7c5025e59d 629 tmp, buf, buf ) );
Janos Follath 0:1f7c5025e59d 630 }
Janos Follath 0:1f7c5025e59d 631
Janos Follath 0:1f7c5025e59d 632 mbedtls_blowfish_free( &blowfish );
Janos Follath 0:1f7c5025e59d 633 }
Janos Follath 0:1f7c5025e59d 634 #endif
Janos Follath 0:1f7c5025e59d 635
Janos Follath 0:1f7c5025e59d 636 #if defined(MBEDTLS_HAVEGE_C)
Janos Follath 0:1f7c5025e59d 637 if( todo.havege )
Janos Follath 0:1f7c5025e59d 638 {
Janos Follath 0:1f7c5025e59d 639 mbedtls_havege_state hs;
Janos Follath 0:1f7c5025e59d 640 mbedtls_havege_init( &hs );
Janos Follath 0:1f7c5025e59d 641 TIME_AND_TSC( "HAVEGE", mbedtls_havege_random( &hs, buf, BUFSIZE ) );
Janos Follath 0:1f7c5025e59d 642 mbedtls_havege_free( &hs );
Janos Follath 0:1f7c5025e59d 643 }
Janos Follath 0:1f7c5025e59d 644 #endif
Janos Follath 0:1f7c5025e59d 645
Janos Follath 0:1f7c5025e59d 646 #if defined(MBEDTLS_CTR_DRBG_C)
Janos Follath 0:1f7c5025e59d 647 if( todo.ctr_drbg )
Janos Follath 0:1f7c5025e59d 648 {
Janos Follath 0:1f7c5025e59d 649 mbedtls_ctr_drbg_context ctr_drbg;
Janos Follath 0:1f7c5025e59d 650
Janos Follath 0:1f7c5025e59d 651 mbedtls_ctr_drbg_init( &ctr_drbg );
Janos Follath 0:1f7c5025e59d 652
Janos Follath 0:1f7c5025e59d 653 if( mbedtls_ctr_drbg_seed( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 )
Janos Follath 0:1f7c5025e59d 654 mbedtls_exit(1);
Janos Follath 0:1f7c5025e59d 655 TIME_AND_TSC( "CTR_DRBG (NOPR)",
Janos Follath 0:1f7c5025e59d 656 if( mbedtls_ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) != 0 )
Janos Follath 0:1f7c5025e59d 657 mbedtls_exit(1) );
Janos Follath 0:1f7c5025e59d 658
Janos Follath 0:1f7c5025e59d 659 if( mbedtls_ctr_drbg_seed( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 )
Janos Follath 0:1f7c5025e59d 660 mbedtls_exit(1);
Janos Follath 0:1f7c5025e59d 661 mbedtls_ctr_drbg_set_prediction_resistance( &ctr_drbg, MBEDTLS_CTR_DRBG_PR_ON );
Janos Follath 0:1f7c5025e59d 662 TIME_AND_TSC( "CTR_DRBG (PR)",
Janos Follath 0:1f7c5025e59d 663 if( mbedtls_ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) != 0 )
Janos Follath 0:1f7c5025e59d 664 mbedtls_exit(1) );
Janos Follath 0:1f7c5025e59d 665 mbedtls_ctr_drbg_free( &ctr_drbg );
Janos Follath 0:1f7c5025e59d 666 }
Janos Follath 0:1f7c5025e59d 667 #endif
Janos Follath 0:1f7c5025e59d 668
Janos Follath 0:1f7c5025e59d 669 #if defined(MBEDTLS_HMAC_DRBG_C)
Janos Follath 0:1f7c5025e59d 670 if( todo.hmac_drbg )
Janos Follath 0:1f7c5025e59d 671 {
Janos Follath 0:1f7c5025e59d 672 mbedtls_hmac_drbg_context hmac_drbg;
Janos Follath 0:1f7c5025e59d 673 const mbedtls_md_info_t *md_info;
Janos Follath 0:1f7c5025e59d 674
Janos Follath 0:1f7c5025e59d 675 mbedtls_hmac_drbg_init( &hmac_drbg );
Janos Follath 0:1f7c5025e59d 676
Janos Follath 0:1f7c5025e59d 677 #if defined(MBEDTLS_SHA1_C)
Janos Follath 0:1f7c5025e59d 678 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
Janos Follath 0:1f7c5025e59d 679 mbedtls_exit(1);
Janos Follath 0:1f7c5025e59d 680
Janos Follath 0:1f7c5025e59d 681 if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
Janos Follath 0:1f7c5025e59d 682 mbedtls_exit(1);
Janos Follath 0:1f7c5025e59d 683 TIME_AND_TSC( "HMAC_DRBG SHA-1 (NOPR)",
Janos Follath 0:1f7c5025e59d 684 if( mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 )
Janos Follath 0:1f7c5025e59d 685 mbedtls_exit(1) );
Janos Follath 0:1f7c5025e59d 686 mbedtls_hmac_drbg_free( &hmac_drbg );
Janos Follath 0:1f7c5025e59d 687
Janos Follath 0:1f7c5025e59d 688 if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
Janos Follath 0:1f7c5025e59d 689 mbedtls_exit(1);
Janos Follath 0:1f7c5025e59d 690 mbedtls_hmac_drbg_set_prediction_resistance( &hmac_drbg,
Janos Follath 0:1f7c5025e59d 691 MBEDTLS_HMAC_DRBG_PR_ON );
Janos Follath 0:1f7c5025e59d 692 TIME_AND_TSC( "HMAC_DRBG SHA-1 (PR)",
Janos Follath 0:1f7c5025e59d 693 if( mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 )
Janos Follath 0:1f7c5025e59d 694 mbedtls_exit(1) );
Janos Follath 0:1f7c5025e59d 695 mbedtls_hmac_drbg_free( &hmac_drbg );
Janos Follath 0:1f7c5025e59d 696 #endif
Janos Follath 0:1f7c5025e59d 697
Janos Follath 0:1f7c5025e59d 698 #if defined(MBEDTLS_SHA256_C)
Janos Follath 0:1f7c5025e59d 699 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 ) ) == NULL )
Janos Follath 0:1f7c5025e59d 700 mbedtls_exit(1);
Janos Follath 0:1f7c5025e59d 701
Janos Follath 0:1f7c5025e59d 702 if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
Janos Follath 0:1f7c5025e59d 703 mbedtls_exit(1);
Janos Follath 0:1f7c5025e59d 704 TIME_AND_TSC( "HMAC_DRBG SHA-256 (NOPR)",
Janos Follath 0:1f7c5025e59d 705 if( mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 )
Janos Follath 0:1f7c5025e59d 706 mbedtls_exit(1) );
Janos Follath 0:1f7c5025e59d 707 mbedtls_hmac_drbg_free( &hmac_drbg );
Janos Follath 0:1f7c5025e59d 708
Janos Follath 0:1f7c5025e59d 709 if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
Janos Follath 0:1f7c5025e59d 710 mbedtls_exit(1);
Janos Follath 0:1f7c5025e59d 711 mbedtls_hmac_drbg_set_prediction_resistance( &hmac_drbg,
Janos Follath 0:1f7c5025e59d 712 MBEDTLS_HMAC_DRBG_PR_ON );
Janos Follath 0:1f7c5025e59d 713 TIME_AND_TSC( "HMAC_DRBG SHA-256 (PR)",
Janos Follath 0:1f7c5025e59d 714 if( mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 )
Janos Follath 0:1f7c5025e59d 715 mbedtls_exit(1) );
Janos Follath 0:1f7c5025e59d 716 mbedtls_hmac_drbg_free( &hmac_drbg );
Janos Follath 0:1f7c5025e59d 717 #endif
Janos Follath 0:1f7c5025e59d 718 }
Janos Follath 0:1f7c5025e59d 719 #endif
Janos Follath 0:1f7c5025e59d 720
Janos Follath 0:1f7c5025e59d 721 #if defined(MBEDTLS_RSA_C) && \
Janos Follath 0:1f7c5025e59d 722 defined(MBEDTLS_PEM_PARSE_C) && defined(MBEDTLS_PK_PARSE_C)
Janos Follath 0:1f7c5025e59d 723 if( todo.rsa )
Janos Follath 0:1f7c5025e59d 724 {
Janos Follath 0:1f7c5025e59d 725 mbedtls_pk_context pk;
Janos Follath 0:1f7c5025e59d 726 mbedtls_rsa_context *rsa;
Janos Follath 0:1f7c5025e59d 727 const char *rsa_keys[] = { RSA_PRIVATE_KEY_2048, RSA_PRIVATE_KEY_4096 };
Janos Follath 0:1f7c5025e59d 728 size_t i;
Janos Follath 0:1f7c5025e59d 729
Janos Follath 0:1f7c5025e59d 730 for( i = 0; i < sizeof( rsa_keys ) / sizeof( rsa_keys[0] ); i++ )
Janos Follath 0:1f7c5025e59d 731 {
Janos Follath 0:1f7c5025e59d 732 mbedtls_pk_init( &pk );
Janos Follath 0:1f7c5025e59d 733 mbedtls_pk_parse_key( &pk, (const unsigned char *) rsa_keys[i],
Janos Follath 0:1f7c5025e59d 734 strlen( rsa_keys[i] ) + 1, NULL, 0 );
Janos Follath 0:1f7c5025e59d 735 rsa = mbedtls_pk_rsa( pk );
Janos Follath 0:1f7c5025e59d 736
Janos Follath 0:1f7c5025e59d 737 mbedtls_snprintf( title, sizeof( title ), "RSA-%d", mbedtls_pk_get_bitlen( &pk ) );
Janos Follath 0:1f7c5025e59d 738
Janos Follath 0:1f7c5025e59d 739 TIME_PUBLIC( title, " public",
Janos Follath 0:1f7c5025e59d 740 buf[0] = 0;
Janos Follath 0:1f7c5025e59d 741 ret = mbedtls_rsa_public( rsa, buf, buf ) );
Janos Follath 0:1f7c5025e59d 742
Janos Follath 0:1f7c5025e59d 743 TIME_PUBLIC( title, "private",
Janos Follath 0:1f7c5025e59d 744 buf[0] = 0;
Janos Follath 0:1f7c5025e59d 745 ret = mbedtls_rsa_private( rsa, myrand, NULL, buf, buf ) );
Janos Follath 0:1f7c5025e59d 746
Janos Follath 0:1f7c5025e59d 747 mbedtls_pk_free( &pk );
Janos Follath 0:1f7c5025e59d 748 }
Janos Follath 0:1f7c5025e59d 749 }
Janos Follath 0:1f7c5025e59d 750 #endif
Janos Follath 0:1f7c5025e59d 751
Janos Follath 0:1f7c5025e59d 752 #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_BIGNUM_C)
Janos Follath 0:1f7c5025e59d 753 if( todo.dhm )
Janos Follath 0:1f7c5025e59d 754 {
Janos Follath 0:1f7c5025e59d 755 int dhm_sizes[] = { 2048, 3072 };
Janos Follath 0:1f7c5025e59d 756 const char *dhm_P[] = {
Janos Follath 0:1f7c5025e59d 757 MBEDTLS_DHM_RFC3526_MODP_2048_P,
Janos Follath 0:1f7c5025e59d 758 MBEDTLS_DHM_RFC3526_MODP_3072_P,
Janos Follath 0:1f7c5025e59d 759 };
Janos Follath 0:1f7c5025e59d 760 const char *dhm_G[] = {
Janos Follath 0:1f7c5025e59d 761 MBEDTLS_DHM_RFC3526_MODP_2048_G,
Janos Follath 0:1f7c5025e59d 762 MBEDTLS_DHM_RFC3526_MODP_3072_G,
Janos Follath 0:1f7c5025e59d 763 };
Janos Follath 0:1f7c5025e59d 764
Janos Follath 0:1f7c5025e59d 765 mbedtls_dhm_context dhm;
Janos Follath 0:1f7c5025e59d 766 size_t olen;
Janos Follath 0:1f7c5025e59d 767 for( i = 0; (size_t) i < sizeof( dhm_sizes ) / sizeof( dhm_sizes[0] ); i++ )
Janos Follath 0:1f7c5025e59d 768 {
Janos Follath 0:1f7c5025e59d 769 mbedtls_dhm_init( &dhm );
Janos Follath 0:1f7c5025e59d 770
Janos Follath 0:1f7c5025e59d 771 if( mbedtls_mpi_read_string( &dhm.P, 16, dhm_P[i] ) != 0 ||
Janos Follath 0:1f7c5025e59d 772 mbedtls_mpi_read_string( &dhm.G, 16, dhm_G[i] ) != 0 )
Janos Follath 0:1f7c5025e59d 773 {
Janos Follath 0:1f7c5025e59d 774 mbedtls_exit( 1 );
Janos Follath 0:1f7c5025e59d 775 }
Janos Follath 0:1f7c5025e59d 776
Janos Follath 0:1f7c5025e59d 777 dhm.len = mbedtls_mpi_size( &dhm.P );
Janos Follath 0:1f7c5025e59d 778 mbedtls_dhm_make_public( &dhm, (int) dhm.len, buf, dhm.len, myrand, NULL );
Janos Follath 0:1f7c5025e59d 779 if( mbedtls_mpi_copy( &dhm.GY, &dhm.GX ) != 0 )
Janos Follath 0:1f7c5025e59d 780 mbedtls_exit( 1 );
Janos Follath 0:1f7c5025e59d 781
Janos Follath 0:1f7c5025e59d 782 mbedtls_snprintf( title, sizeof( title ), "DHE-%d", dhm_sizes[i] );
Janos Follath 0:1f7c5025e59d 783 TIME_PUBLIC( title, "handshake",
Janos Follath 0:1f7c5025e59d 784 ret |= mbedtls_dhm_make_public( &dhm, (int) dhm.len, buf, dhm.len,
Janos Follath 0:1f7c5025e59d 785 myrand, NULL );
Janos Follath 0:1f7c5025e59d 786 ret |= mbedtls_dhm_calc_secret( &dhm, buf, sizeof( buf ), &olen, myrand, NULL ) );
Janos Follath 0:1f7c5025e59d 787
Janos Follath 0:1f7c5025e59d 788 mbedtls_snprintf( title, sizeof( title ), "DH-%d", dhm_sizes[i] );
Janos Follath 0:1f7c5025e59d 789 TIME_PUBLIC( title, "handshake",
Janos Follath 0:1f7c5025e59d 790 ret |= mbedtls_dhm_calc_secret( &dhm, buf, sizeof( buf ), &olen, myrand, NULL ) );
Janos Follath 0:1f7c5025e59d 791
Janos Follath 0:1f7c5025e59d 792 mbedtls_dhm_free( &dhm );
Janos Follath 0:1f7c5025e59d 793 }
Janos Follath 0:1f7c5025e59d 794 }
Janos Follath 0:1f7c5025e59d 795 #endif
Janos Follath 0:1f7c5025e59d 796
mbed_official 11:6ccae3ebafd5 797 #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA256_C) && defined(ENABLE_ECDSA)
Janos Follath 0:1f7c5025e59d 798 if( todo.ecdsa )
Janos Follath 0:1f7c5025e59d 799 {
Janos Follath 0:1f7c5025e59d 800 mbedtls_ecdsa_context ecdsa;
Janos Follath 0:1f7c5025e59d 801 const mbedtls_ecp_curve_info *curve_info;
Janos Follath 0:1f7c5025e59d 802 size_t sig_len;
Janos Follath 0:1f7c5025e59d 803
Janos Follath 0:1f7c5025e59d 804 memset( buf, 0x2A, sizeof( buf ) );
Janos Follath 0:1f7c5025e59d 805
Janos Follath 0:1f7c5025e59d 806 for( curve_info = mbedtls_ecp_curve_list();
Janos Follath 0:1f7c5025e59d 807 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Janos Follath 0:1f7c5025e59d 808 curve_info++ )
Janos Follath 0:1f7c5025e59d 809 {
Janos Follath 0:1f7c5025e59d 810 mbedtls_ecdsa_init( &ecdsa );
Janos Follath 0:1f7c5025e59d 811
Janos Follath 0:1f7c5025e59d 812 if( mbedtls_ecdsa_genkey( &ecdsa, curve_info->grp_id, myrand, NULL ) != 0 )
Janos Follath 0:1f7c5025e59d 813 mbedtls_exit( 1 );
Janos Follath 0:1f7c5025e59d 814 ecp_clear_precomputed( &ecdsa.grp );
Janos Follath 0:1f7c5025e59d 815
Janos Follath 0:1f7c5025e59d 816 mbedtls_snprintf( title, sizeof( title ), "ECDSA-%s",
Janos Follath 0:1f7c5025e59d 817 curve_info->name );
Janos Follath 0:1f7c5025e59d 818 TIME_PUBLIC( title, "sign",
Janos Follath 0:1f7c5025e59d 819 ret = mbedtls_ecdsa_write_signature( &ecdsa, MBEDTLS_MD_SHA256, buf, curve_info->bit_size,
Janos Follath 0:1f7c5025e59d 820 tmp, &sig_len, myrand, NULL ) );
Janos Follath 0:1f7c5025e59d 821
Janos Follath 0:1f7c5025e59d 822 mbedtls_ecdsa_free( &ecdsa );
Janos Follath 0:1f7c5025e59d 823 }
Janos Follath 0:1f7c5025e59d 824
Janos Follath 0:1f7c5025e59d 825 for( curve_info = mbedtls_ecp_curve_list();
Janos Follath 0:1f7c5025e59d 826 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Janos Follath 0:1f7c5025e59d 827 curve_info++ )
Janos Follath 0:1f7c5025e59d 828 {
Janos Follath 0:1f7c5025e59d 829 mbedtls_ecdsa_init( &ecdsa );
Janos Follath 0:1f7c5025e59d 830
Janos Follath 0:1f7c5025e59d 831 if( mbedtls_ecdsa_genkey( &ecdsa, curve_info->grp_id, myrand, NULL ) != 0 ||
Janos Follath 0:1f7c5025e59d 832 mbedtls_ecdsa_write_signature( &ecdsa, MBEDTLS_MD_SHA256, buf, curve_info->bit_size,
Janos Follath 0:1f7c5025e59d 833 tmp, &sig_len, myrand, NULL ) != 0 )
Janos Follath 0:1f7c5025e59d 834 {
Janos Follath 0:1f7c5025e59d 835 mbedtls_exit( 1 );
Janos Follath 0:1f7c5025e59d 836 }
Janos Follath 0:1f7c5025e59d 837 ecp_clear_precomputed( &ecdsa.grp );
Janos Follath 0:1f7c5025e59d 838
Janos Follath 0:1f7c5025e59d 839 mbedtls_snprintf( title, sizeof( title ), "ECDSA-%s",
Janos Follath 0:1f7c5025e59d 840 curve_info->name );
Janos Follath 0:1f7c5025e59d 841 TIME_PUBLIC( title, "verify",
Janos Follath 0:1f7c5025e59d 842 ret = mbedtls_ecdsa_read_signature( &ecdsa, buf, curve_info->bit_size,
Janos Follath 0:1f7c5025e59d 843 tmp, sig_len ) );
Janos Follath 0:1f7c5025e59d 844
Janos Follath 0:1f7c5025e59d 845 mbedtls_ecdsa_free( &ecdsa );
Janos Follath 0:1f7c5025e59d 846 }
Janos Follath 0:1f7c5025e59d 847 }
Janos Follath 0:1f7c5025e59d 848 #endif
Janos Follath 0:1f7c5025e59d 849
Janos Follath 0:1f7c5025e59d 850 #if defined(MBEDTLS_ECDH_C)
Janos Follath 0:1f7c5025e59d 851 if( todo.ecdh )
Janos Follath 0:1f7c5025e59d 852 {
Janos Follath 0:1f7c5025e59d 853 mbedtls_ecdh_context ecdh;
Janos Follath 0:1f7c5025e59d 854 #if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
Janos Follath 0:1f7c5025e59d 855 mbedtls_mpi z;
Janos Follath 0:1f7c5025e59d 856 #endif
Janos Follath 0:1f7c5025e59d 857 const mbedtls_ecp_curve_info *curve_info;
Janos Follath 0:1f7c5025e59d 858 size_t olen;
Janos Follath 0:1f7c5025e59d 859
Janos Follath 0:1f7c5025e59d 860 for( curve_info = mbedtls_ecp_curve_list();
Janos Follath 0:1f7c5025e59d 861 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Janos Follath 0:1f7c5025e59d 862 curve_info++ )
Janos Follath 0:1f7c5025e59d 863 {
Janos Follath 0:1f7c5025e59d 864 mbedtls_ecdh_init( &ecdh );
Janos Follath 0:1f7c5025e59d 865
Janos Follath 0:1f7c5025e59d 866 if( mbedtls_ecp_group_load( &ecdh.grp, curve_info->grp_id ) != 0 ||
Janos Follath 0:1f7c5025e59d 867 mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf ),
Janos Follath 0:1f7c5025e59d 868 myrand, NULL ) != 0 ||
Janos Follath 0:1f7c5025e59d 869 mbedtls_ecp_copy( &ecdh.Qp, &ecdh.Q ) != 0 )
Janos Follath 0:1f7c5025e59d 870 {
Janos Follath 0:1f7c5025e59d 871 mbedtls_exit( 1 );
Janos Follath 0:1f7c5025e59d 872 }
Janos Follath 0:1f7c5025e59d 873 ecp_clear_precomputed( &ecdh.grp );
Janos Follath 0:1f7c5025e59d 874
Janos Follath 0:1f7c5025e59d 875 mbedtls_snprintf( title, sizeof( title ), "ECDHE-%s",
Janos Follath 0:1f7c5025e59d 876 curve_info->name );
Janos Follath 0:1f7c5025e59d 877 TIME_PUBLIC( title, "handshake",
Janos Follath 0:1f7c5025e59d 878 ret |= mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf ),
Janos Follath 0:1f7c5025e59d 879 myrand, NULL );
Janos Follath 0:1f7c5025e59d 880 ret |= mbedtls_ecdh_calc_secret( &ecdh, &olen, buf, sizeof( buf ),
Janos Follath 0:1f7c5025e59d 881 myrand, NULL ) );
Janos Follath 0:1f7c5025e59d 882 mbedtls_ecdh_free( &ecdh );
Janos Follath 0:1f7c5025e59d 883 }
Janos Follath 0:1f7c5025e59d 884
Janos Follath 0:1f7c5025e59d 885 /* Curve25519 needs to be handled separately */
Janos Follath 0:1f7c5025e59d 886 #if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
Janos Follath 0:1f7c5025e59d 887 mbedtls_ecdh_init( &ecdh );
Janos Follath 0:1f7c5025e59d 888 mbedtls_mpi_init( &z );
Janos Follath 0:1f7c5025e59d 889
Janos Follath 0:1f7c5025e59d 890 if( mbedtls_ecp_group_load( &ecdh.grp, MBEDTLS_ECP_DP_CURVE25519 ) != 0 ||
Janos Follath 0:1f7c5025e59d 891 mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Qp, myrand, NULL ) != 0 )
Janos Follath 0:1f7c5025e59d 892 {
Janos Follath 0:1f7c5025e59d 893 mbedtls_exit( 1 );
Janos Follath 0:1f7c5025e59d 894 }
Janos Follath 0:1f7c5025e59d 895
Janos Follath 0:1f7c5025e59d 896 TIME_PUBLIC( "ECDHE-Curve25519", "handshake",
Janos Follath 0:1f7c5025e59d 897 ret |= mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Q,
Janos Follath 0:1f7c5025e59d 898 myrand, NULL );
Janos Follath 0:1f7c5025e59d 899 ret |= mbedtls_ecdh_compute_shared( &ecdh.grp, &z, &ecdh.Qp, &ecdh.d,
Janos Follath 0:1f7c5025e59d 900 myrand, NULL ) );
Janos Follath 0:1f7c5025e59d 901
Janos Follath 0:1f7c5025e59d 902 mbedtls_ecdh_free( &ecdh );
Janos Follath 0:1f7c5025e59d 903 mbedtls_mpi_free( &z );
Janos Follath 0:1f7c5025e59d 904 #endif
Janos Follath 0:1f7c5025e59d 905
Janos Follath 0:1f7c5025e59d 906 for( curve_info = mbedtls_ecp_curve_list();
Janos Follath 0:1f7c5025e59d 907 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Janos Follath 0:1f7c5025e59d 908 curve_info++ )
Janos Follath 0:1f7c5025e59d 909 {
Janos Follath 0:1f7c5025e59d 910 mbedtls_ecdh_init( &ecdh );
Janos Follath 0:1f7c5025e59d 911
Janos Follath 0:1f7c5025e59d 912 if( mbedtls_ecp_group_load( &ecdh.grp, curve_info->grp_id ) != 0 ||
Janos Follath 0:1f7c5025e59d 913 mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf ),
Janos Follath 0:1f7c5025e59d 914 myrand, NULL ) != 0 ||
Janos Follath 0:1f7c5025e59d 915 mbedtls_ecp_copy( &ecdh.Qp, &ecdh.Q ) != 0 ||
Janos Follath 0:1f7c5025e59d 916 mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf ),
Janos Follath 0:1f7c5025e59d 917 myrand, NULL ) != 0 )
Janos Follath 0:1f7c5025e59d 918 {
Janos Follath 0:1f7c5025e59d 919 mbedtls_exit( 1 );
Janos Follath 0:1f7c5025e59d 920 }
Janos Follath 0:1f7c5025e59d 921 ecp_clear_precomputed( &ecdh.grp );
Janos Follath 0:1f7c5025e59d 922
Janos Follath 0:1f7c5025e59d 923 mbedtls_snprintf( title, sizeof( title ), "ECDH-%s",
Janos Follath 0:1f7c5025e59d 924 curve_info->name );
Janos Follath 0:1f7c5025e59d 925 TIME_PUBLIC( title, "handshake",
Janos Follath 0:1f7c5025e59d 926 ret |= mbedtls_ecdh_calc_secret( &ecdh, &olen, buf, sizeof( buf ),
Janos Follath 0:1f7c5025e59d 927 myrand, NULL ) );
Janos Follath 0:1f7c5025e59d 928 mbedtls_ecdh_free( &ecdh );
Janos Follath 0:1f7c5025e59d 929 }
Janos Follath 0:1f7c5025e59d 930
Janos Follath 0:1f7c5025e59d 931 /* Curve25519 needs to be handled separately */
Janos Follath 0:1f7c5025e59d 932 #if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
Janos Follath 0:1f7c5025e59d 933 mbedtls_ecdh_init( &ecdh );
Janos Follath 0:1f7c5025e59d 934 mbedtls_mpi_init( &z );
Janos Follath 0:1f7c5025e59d 935
Janos Follath 0:1f7c5025e59d 936 if( mbedtls_ecp_group_load( &ecdh.grp, MBEDTLS_ECP_DP_CURVE25519 ) != 0 ||
Janos Follath 0:1f7c5025e59d 937 mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Qp,
Janos Follath 0:1f7c5025e59d 938 myrand, NULL ) != 0 ||
Janos Follath 0:1f7c5025e59d 939 mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Q, myrand, NULL ) != 0 )
Janos Follath 0:1f7c5025e59d 940 {
Janos Follath 0:1f7c5025e59d 941 mbedtls_exit( 1 );
Janos Follath 0:1f7c5025e59d 942 }
Janos Follath 0:1f7c5025e59d 943
Janos Follath 0:1f7c5025e59d 944 TIME_PUBLIC( "ECDH-Curve25519", "handshake",
Janos Follath 0:1f7c5025e59d 945 ret |= mbedtls_ecdh_compute_shared( &ecdh.grp, &z, &ecdh.Qp, &ecdh.d,
Janos Follath 0:1f7c5025e59d 946 myrand, NULL ) );
Janos Follath 0:1f7c5025e59d 947
Janos Follath 0:1f7c5025e59d 948 mbedtls_ecdh_free( &ecdh );
Janos Follath 0:1f7c5025e59d 949 mbedtls_mpi_free( &z );
Janos Follath 0:1f7c5025e59d 950 #endif
Janos Follath 0:1f7c5025e59d 951 }
Janos Follath 0:1f7c5025e59d 952 #endif
Janos Follath 0:1f7c5025e59d 953
Janos Follath 0:1f7c5025e59d 954 mbedtls_printf("\r\nDONE\r\n");
Janos Follath 0:1f7c5025e59d 955
Janos Follath 0:1f7c5025e59d 956 #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Janos Follath 0:1f7c5025e59d 957 mbedtls_memory_buffer_alloc_free();
Janos Follath 0:1f7c5025e59d 958 #endif
Janos Follath 0:1f7c5025e59d 959
Janos Follath 0:1f7c5025e59d 960 return( 0 );
Janos Follath 0:1f7c5025e59d 961 }
Janos Follath 0:1f7c5025e59d 962
Janos Follath 0:1f7c5025e59d 963 int main(void) {
Janos Follath 0:1f7c5025e59d 964 int ret = benchmark(0, NULL);
Janos Follath 0:1f7c5025e59d 965 if (ret != 0) {
Janos Follath 0:1f7c5025e59d 966 mbedtls_printf("Benchmark failed with error %d\r\n", ret);
Janos Follath 0:1f7c5025e59d 967 }
Janos Follath 0:1f7c5025e59d 968 }