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:
Fri Oct 21 16:00:09 2016 +0100
Revision:
11:6ccae3ebafd5
Parent:
0:1f7c5025e59d
Child:
30:e0ea8c1ef9f5
Updating mbed-os to mbed-os-5.2.0-rc4


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 *
Janos Follath 0:1f7c5025e59d 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 11:6ccae3ebafd5 169 "des3, des, aes_cmac, des3_cmac, aes_cbc, aes_gcm, aes_ccm,\r\n" \
Janos Follath 0:1f7c5025e59d 170 "havege, ctr_drbg, hmac_drbg\r\n" \
Janos Follath 0:1f7c5025e59d 171 "rsa, dhm, ecdsa, ecdh.\r\n"
Janos Follath 0:1f7c5025e59d 172
Janos Follath 0:1f7c5025e59d 173 #if defined(MBEDTLS_ERROR_C)
Janos Follath 0:1f7c5025e59d 174 #define PRINT_ERROR \
Janos Follath 0:1f7c5025e59d 175 mbedtls_strerror( ret, ( char * )tmp, sizeof( tmp ) ); \
Janos Follath 0:1f7c5025e59d 176 mbedtls_printf( "FAILED: %s\r\n", tmp );
Janos Follath 0:1f7c5025e59d 177 #else
Janos Follath 0:1f7c5025e59d 178 #define PRINT_ERROR \
Janos Follath 0:1f7c5025e59d 179 mbedtls_printf( "FAILED: -0x%04x\r\n", -ret );
Janos Follath 0:1f7c5025e59d 180 #endif
Janos Follath 0:1f7c5025e59d 181
Janos Follath 0:1f7c5025e59d 182 static unsigned long mbedtls_timing_hardclock( void )
Janos Follath 0:1f7c5025e59d 183 {
Janos Follath 0:1f7c5025e59d 184 static int dwt_started = 0;
Janos Follath 0:1f7c5025e59d 185
Janos Follath 0:1f7c5025e59d 186 if( dwt_started == 0 )
Janos Follath 0:1f7c5025e59d 187 {
Janos Follath 0:1f7c5025e59d 188 CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;
Janos Follath 0:1f7c5025e59d 189 DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk;
Janos Follath 0:1f7c5025e59d 190 }
Janos Follath 0:1f7c5025e59d 191
Janos Follath 0:1f7c5025e59d 192 return( DWT->CYCCNT );
Janos Follath 0:1f7c5025e59d 193 }
Janos Follath 0:1f7c5025e59d 194
Janos Follath 0:1f7c5025e59d 195 static volatile int alarmed;
Janos Follath 0:1f7c5025e59d 196 static void alarm() { alarmed = 1; }
Janos Follath 0:1f7c5025e59d 197
Janos Follath 0:1f7c5025e59d 198 #define TIME_AND_TSC( TITLE, CODE ) \
Janos Follath 0:1f7c5025e59d 199 do { \
Janos Follath 0:1f7c5025e59d 200 unsigned long i, j, tsc; \
Janos Follath 0:1f7c5025e59d 201 Timeout t; \
Janos Follath 0:1f7c5025e59d 202 \
Janos Follath 0:1f7c5025e59d 203 mbedtls_printf( HEADER_FORMAT, TITLE ); \
Janos Follath 0:1f7c5025e59d 204 \
Janos Follath 0:1f7c5025e59d 205 for( i = 1, alarmed = 0, t.attach( alarm, 1.0 ); !alarmed; i++ ) \
Janos Follath 0:1f7c5025e59d 206 { \
Janos Follath 0:1f7c5025e59d 207 CODE; \
Janos Follath 0:1f7c5025e59d 208 } \
Janos Follath 0:1f7c5025e59d 209 \
Janos Follath 0:1f7c5025e59d 210 tsc = mbedtls_timing_hardclock(); \
Janos Follath 0:1f7c5025e59d 211 for( j = 0; j < 1024; j++ ) \
Janos Follath 0:1f7c5025e59d 212 { \
Janos Follath 0:1f7c5025e59d 213 CODE; \
Janos Follath 0:1f7c5025e59d 214 } \
Janos Follath 0:1f7c5025e59d 215 \
Janos Follath 0:1f7c5025e59d 216 mbedtls_printf( "%9lu Kb/s, %9lu cycles/byte\r\n", \
Janos Follath 0:1f7c5025e59d 217 i * BUFSIZE / 1024, \
Janos Follath 0:1f7c5025e59d 218 ( mbedtls_timing_hardclock() - tsc ) / ( j * BUFSIZE ) ); \
Janos Follath 0:1f7c5025e59d 219 } while( 0 )
Janos Follath 0:1f7c5025e59d 220
Janos Follath 0:1f7c5025e59d 221 #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && defined(MBEDTLS_MEMORY_DEBUG)
Janos Follath 0:1f7c5025e59d 222
Janos Follath 0:1f7c5025e59d 223 #define MEMORY_MEASURE_INIT \
Janos Follath 0:1f7c5025e59d 224 size_t max_used, max_blocks, max_bytes; \
Janos Follath 0:1f7c5025e59d 225 size_t prv_used, prv_blocks; \
Janos Follath 0:1f7c5025e59d 226 mbedtls_memory_buffer_alloc_cur_get( &prv_used, &prv_blocks ); \
Janos Follath 0:1f7c5025e59d 227 mbedtls_memory_buffer_alloc_max_reset( );
Janos Follath 0:1f7c5025e59d 228
Janos Follath 0:1f7c5025e59d 229 #define MEMORY_MEASURE_PRINT( title_len ) \
Janos Follath 0:1f7c5025e59d 230 mbedtls_memory_buffer_alloc_max_get( &max_used, &max_blocks ); \
Janos Follath 0:1f7c5025e59d 231 for( i = 12 - title_len; i != 0; i-- ) mbedtls_printf( " " ); \
Janos Follath 0:1f7c5025e59d 232 max_used -= prv_used; \
Janos Follath 0:1f7c5025e59d 233 max_blocks -= prv_blocks; \
Janos Follath 0:1f7c5025e59d 234 max_bytes = max_used + MEM_BLOCK_OVERHEAD * max_blocks; \
Janos Follath 0:1f7c5025e59d 235 mbedtls_printf( "%6u heap bytes", (unsigned) max_bytes );
Janos Follath 0:1f7c5025e59d 236
Janos Follath 0:1f7c5025e59d 237 #else
Janos Follath 0:1f7c5025e59d 238 #define MEMORY_MEASURE_INIT
Janos Follath 0:1f7c5025e59d 239 #define MEMORY_MEASURE_PRINT( title_len )
Janos Follath 0:1f7c5025e59d 240 #endif
Janos Follath 0:1f7c5025e59d 241
Janos Follath 0:1f7c5025e59d 242 #define TIME_PUBLIC( TITLE, TYPE, CODE ) \
Janos Follath 0:1f7c5025e59d 243 do { \
Janos Follath 0:1f7c5025e59d 244 unsigned long ms; \
Janos Follath 0:1f7c5025e59d 245 int ret = 0; \
Janos Follath 0:1f7c5025e59d 246 Timer t; \
Janos Follath 0:1f7c5025e59d 247 MEMORY_MEASURE_INIT; \
Janos Follath 0:1f7c5025e59d 248 \
Janos Follath 0:1f7c5025e59d 249 mbedtls_printf( HEADER_FORMAT, TITLE ); \
Janos Follath 0:1f7c5025e59d 250 fflush( stdout ); \
Janos Follath 0:1f7c5025e59d 251 \
Janos Follath 0:1f7c5025e59d 252 t.start(); \
Janos Follath 0:1f7c5025e59d 253 CODE; \
Janos Follath 0:1f7c5025e59d 254 t.stop(); \
Janos Follath 0:1f7c5025e59d 255 ms = t.read_ms(); \
Janos Follath 0:1f7c5025e59d 256 \
Janos Follath 0:1f7c5025e59d 257 if( ret != 0 ) \
Janos Follath 0:1f7c5025e59d 258 { \
Janos Follath 0:1f7c5025e59d 259 PRINT_ERROR; \
Janos Follath 0:1f7c5025e59d 260 } \
Janos Follath 0:1f7c5025e59d 261 else \
Janos Follath 0:1f7c5025e59d 262 { \
Janos Follath 0:1f7c5025e59d 263 mbedtls_printf( "%6lu ms/" TYPE, ms ); \
Janos Follath 0:1f7c5025e59d 264 MEMORY_MEASURE_PRINT( sizeof( TYPE ) + 1 ); \
Janos Follath 0:1f7c5025e59d 265 mbedtls_printf( "\r\n" ); \
Janos Follath 0:1f7c5025e59d 266 } \
Janos Follath 0:1f7c5025e59d 267 } while( 0 )
Janos Follath 0:1f7c5025e59d 268
Janos Follath 0:1f7c5025e59d 269 static int myrand( void *rng_state, unsigned char *output, size_t len )
Janos Follath 0:1f7c5025e59d 270 {
Janos Follath 0:1f7c5025e59d 271 size_t use_len;
Janos Follath 0:1f7c5025e59d 272 int rnd;
Janos Follath 0:1f7c5025e59d 273
Janos Follath 0:1f7c5025e59d 274 if( rng_state != NULL )
Janos Follath 0:1f7c5025e59d 275 rng_state = NULL;
Janos Follath 0:1f7c5025e59d 276
Janos Follath 0:1f7c5025e59d 277 while( len > 0 )
Janos Follath 0:1f7c5025e59d 278 {
Janos Follath 0:1f7c5025e59d 279 use_len = len;
Janos Follath 0:1f7c5025e59d 280 if( use_len > sizeof(int) )
Janos Follath 0:1f7c5025e59d 281 use_len = sizeof(int);
Janos Follath 0:1f7c5025e59d 282
Janos Follath 0:1f7c5025e59d 283 rnd = rand();
Janos Follath 0:1f7c5025e59d 284 memcpy( output, &rnd, use_len );
Janos Follath 0:1f7c5025e59d 285 output += use_len;
Janos Follath 0:1f7c5025e59d 286 len -= use_len;
Janos Follath 0:1f7c5025e59d 287 }
Janos Follath 0:1f7c5025e59d 288
Janos Follath 0:1f7c5025e59d 289 return( 0 );
Janos Follath 0:1f7c5025e59d 290 }
Janos Follath 0:1f7c5025e59d 291
Janos Follath 0:1f7c5025e59d 292 /*
Janos Follath 0:1f7c5025e59d 293 * Clear some memory that was used to prepare the context
Janos Follath 0:1f7c5025e59d 294 */
Janos Follath 0:1f7c5025e59d 295 #if defined(MBEDTLS_ECP_C)
Janos Follath 0:1f7c5025e59d 296 void ecp_clear_precomputed( mbedtls_ecp_group *grp )
Janos Follath 0:1f7c5025e59d 297 {
Janos Follath 0:1f7c5025e59d 298 if( grp->T != NULL )
Janos Follath 0:1f7c5025e59d 299 {
Janos Follath 0:1f7c5025e59d 300 size_t i;
Janos Follath 0:1f7c5025e59d 301 for( i = 0; i < grp->T_size; i++ )
Janos Follath 0:1f7c5025e59d 302 mbedtls_ecp_point_free( &grp->T[i] );
Janos Follath 0:1f7c5025e59d 303 mbedtls_free( grp->T );
Janos Follath 0:1f7c5025e59d 304 }
Janos Follath 0:1f7c5025e59d 305 grp->T = NULL;
Janos Follath 0:1f7c5025e59d 306 grp->T_size = 0;
Janos Follath 0:1f7c5025e59d 307 }
Janos Follath 0:1f7c5025e59d 308 #else
Janos Follath 0:1f7c5025e59d 309 #define ecp_clear_precomputed( g )
Janos Follath 0:1f7c5025e59d 310 #endif
Janos Follath 0:1f7c5025e59d 311
Janos Follath 0:1f7c5025e59d 312 unsigned char buf[BUFSIZE];
Janos Follath 0:1f7c5025e59d 313
Janos Follath 0:1f7c5025e59d 314 typedef struct {
Janos Follath 0:1f7c5025e59d 315 char md4, md5, ripemd160, sha1, sha256, sha512,
mbed_official 11:6ccae3ebafd5 316 arc4, des3, des, aes_cbc, aes_gcm, aes_ccm,
mbed_official 11:6ccae3ebafd5 317 aes_cmac, des3_cmac, camellia, blowfish,
Janos Follath 0:1f7c5025e59d 318 havege, ctr_drbg, hmac_drbg,
Janos Follath 0:1f7c5025e59d 319 rsa, dhm, ecdsa, ecdh;
Janos Follath 0:1f7c5025e59d 320 } todo_list;
Janos Follath 0:1f7c5025e59d 321
Janos Follath 0:1f7c5025e59d 322 static int benchmark( int argc, char *argv[] )
Janos Follath 0:1f7c5025e59d 323 {
Janos Follath 0:1f7c5025e59d 324 int i;
Janos Follath 0:1f7c5025e59d 325 unsigned char tmp[200];
Janos Follath 0:1f7c5025e59d 326 char title[TITLE_LEN];
Janos Follath 0:1f7c5025e59d 327 todo_list todo;
Janos Follath 0:1f7c5025e59d 328 #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Janos Follath 0:1f7c5025e59d 329 unsigned char malloc_buf[HEAP_SIZE] = { 0 };
Janos Follath 0:1f7c5025e59d 330 #endif
Janos Follath 0:1f7c5025e59d 331
Janos Follath 0:1f7c5025e59d 332 if( argc <= 1 )
Janos Follath 0:1f7c5025e59d 333 {
Janos Follath 0:1f7c5025e59d 334 memset( &todo, 1, sizeof( todo ) );
Janos Follath 0:1f7c5025e59d 335 }
Janos Follath 0:1f7c5025e59d 336 else
Janos Follath 0:1f7c5025e59d 337 {
Janos Follath 0:1f7c5025e59d 338 memset( &todo, 0, sizeof( todo ) );
Janos Follath 0:1f7c5025e59d 339
Janos Follath 0:1f7c5025e59d 340 for( i = 1; i < argc; i++ )
Janos Follath 0:1f7c5025e59d 341 {
Janos Follath 0:1f7c5025e59d 342 if( strcmp( argv[i], "md4" ) == 0 )
Janos Follath 0:1f7c5025e59d 343 todo.md4 = 1;
Janos Follath 0:1f7c5025e59d 344 else if( strcmp( argv[i], "md5" ) == 0 )
Janos Follath 0:1f7c5025e59d 345 todo.md5 = 1;
Janos Follath 0:1f7c5025e59d 346 else if( strcmp( argv[i], "ripemd160" ) == 0 )
Janos Follath 0:1f7c5025e59d 347 todo.ripemd160 = 1;
Janos Follath 0:1f7c5025e59d 348 else if( strcmp( argv[i], "sha1" ) == 0 )
Janos Follath 0:1f7c5025e59d 349 todo.sha1 = 1;
Janos Follath 0:1f7c5025e59d 350 else if( strcmp( argv[i], "sha256" ) == 0 )
Janos Follath 0:1f7c5025e59d 351 todo.sha256 = 1;
Janos Follath 0:1f7c5025e59d 352 else if( strcmp( argv[i], "sha512" ) == 0 )
Janos Follath 0:1f7c5025e59d 353 todo.sha512 = 1;
Janos Follath 0:1f7c5025e59d 354 else if( strcmp( argv[i], "arc4" ) == 0 )
Janos Follath 0:1f7c5025e59d 355 todo.arc4 = 1;
Janos Follath 0:1f7c5025e59d 356 else if( strcmp( argv[i], "des3" ) == 0 )
Janos Follath 0:1f7c5025e59d 357 todo.des3 = 1;
Janos Follath 0:1f7c5025e59d 358 else if( strcmp( argv[i], "des" ) == 0 )
Janos Follath 0:1f7c5025e59d 359 todo.des = 1;
Janos Follath 0:1f7c5025e59d 360 else if( strcmp( argv[i], "aes_cbc" ) == 0 )
Janos Follath 0:1f7c5025e59d 361 todo.aes_cbc = 1;
Janos Follath 0:1f7c5025e59d 362 else if( strcmp( argv[i], "aes_gcm" ) == 0 )
Janos Follath 0:1f7c5025e59d 363 todo.aes_gcm = 1;
Janos Follath 0:1f7c5025e59d 364 else if( strcmp( argv[i], "aes_ccm" ) == 0 )
Janos Follath 0:1f7c5025e59d 365 todo.aes_ccm = 1;
mbed_official 11:6ccae3ebafd5 366 else if( strcmp( argv[i], "aes_cmac" ) == 0 )
mbed_official 11:6ccae3ebafd5 367 todo.aes_cmac = 1;
mbed_official 11:6ccae3ebafd5 368 else if( strcmp( argv[i], "des3_cmac" ) == 0 )
mbed_official 11:6ccae3ebafd5 369 todo.des3_cmac = 1;
Janos Follath 0:1f7c5025e59d 370 else if( strcmp( argv[i], "camellia" ) == 0 )
Janos Follath 0:1f7c5025e59d 371 todo.camellia = 1;
Janos Follath 0:1f7c5025e59d 372 else if( strcmp( argv[i], "blowfish" ) == 0 )
Janos Follath 0:1f7c5025e59d 373 todo.blowfish = 1;
Janos Follath 0:1f7c5025e59d 374 else if( strcmp( argv[i], "havege" ) == 0 )
Janos Follath 0:1f7c5025e59d 375 todo.havege = 1;
Janos Follath 0:1f7c5025e59d 376 else if( strcmp( argv[i], "ctr_drbg" ) == 0 )
Janos Follath 0:1f7c5025e59d 377 todo.ctr_drbg = 1;
Janos Follath 0:1f7c5025e59d 378 else if( strcmp( argv[i], "hmac_drbg" ) == 0 )
Janos Follath 0:1f7c5025e59d 379 todo.hmac_drbg = 1;
Janos Follath 0:1f7c5025e59d 380 else if( strcmp( argv[i], "rsa" ) == 0 )
Janos Follath 0:1f7c5025e59d 381 todo.rsa = 1;
Janos Follath 0:1f7c5025e59d 382 else if( strcmp( argv[i], "dhm" ) == 0 )
Janos Follath 0:1f7c5025e59d 383 todo.dhm = 1;
Janos Follath 0:1f7c5025e59d 384 else if( strcmp( argv[i], "ecdsa" ) == 0 )
Janos Follath 0:1f7c5025e59d 385 todo.ecdsa = 1;
Janos Follath 0:1f7c5025e59d 386 else if( strcmp( argv[i], "ecdh" ) == 0 )
Janos Follath 0:1f7c5025e59d 387 todo.ecdh = 1;
Janos Follath 0:1f7c5025e59d 388 else
Janos Follath 0:1f7c5025e59d 389 {
Janos Follath 0:1f7c5025e59d 390 mbedtls_printf( "Unrecognized option: %s\r\n", argv[i] );
Janos Follath 0:1f7c5025e59d 391 mbedtls_printf( "Available options: " OPTIONS );
Janos Follath 0:1f7c5025e59d 392 }
Janos Follath 0:1f7c5025e59d 393 }
Janos Follath 0:1f7c5025e59d 394 }
Janos Follath 0:1f7c5025e59d 395
Janos Follath 0:1f7c5025e59d 396 mbedtls_printf( "\r\n\r\n" );
Janos Follath 0:1f7c5025e59d 397
Janos Follath 0:1f7c5025e59d 398 #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Janos Follath 0:1f7c5025e59d 399 mbedtls_memory_buffer_alloc_init( malloc_buf, sizeof( malloc_buf ) );
Janos Follath 0:1f7c5025e59d 400 #endif
Janos Follath 0:1f7c5025e59d 401 memset( buf, 0xAA, sizeof( buf ) );
Janos Follath 0:1f7c5025e59d 402 memset( tmp, 0xBB, sizeof( tmp ) );
Janos Follath 0:1f7c5025e59d 403
Janos Follath 0:1f7c5025e59d 404 #if defined(MBEDTLS_MD4_C)
Janos Follath 0:1f7c5025e59d 405 if( todo.md4 )
Janos Follath 0:1f7c5025e59d 406 TIME_AND_TSC( "MD4", mbedtls_md4( buf, BUFSIZE, tmp ) );
Janos Follath 0:1f7c5025e59d 407 #endif
Janos Follath 0:1f7c5025e59d 408
Janos Follath 0:1f7c5025e59d 409 #if defined(MBEDTLS_MD5_C)
Janos Follath 0:1f7c5025e59d 410 if( todo.md5 )
Janos Follath 0:1f7c5025e59d 411 TIME_AND_TSC( "MD5", mbedtls_md5( buf, BUFSIZE, tmp ) );
Janos Follath 0:1f7c5025e59d 412 #endif
Janos Follath 0:1f7c5025e59d 413
Janos Follath 0:1f7c5025e59d 414 #if defined(MBEDTLS_RIPEMD160_C)
Janos Follath 0:1f7c5025e59d 415 if( todo.ripemd160 )
Janos Follath 0:1f7c5025e59d 416 TIME_AND_TSC( "RIPEMD160", mbedtls_ripemd160( buf, BUFSIZE, tmp ) );
Janos Follath 0:1f7c5025e59d 417 #endif
Janos Follath 0:1f7c5025e59d 418
Janos Follath 0:1f7c5025e59d 419 #if defined(MBEDTLS_SHA1_C)
Janos Follath 0:1f7c5025e59d 420 if( todo.sha1 )
Janos Follath 0:1f7c5025e59d 421 TIME_AND_TSC( "SHA-1", mbedtls_sha1( buf, BUFSIZE, tmp ) );
Janos Follath 0:1f7c5025e59d 422 #endif
Janos Follath 0:1f7c5025e59d 423
Janos Follath 0:1f7c5025e59d 424 #if defined(MBEDTLS_SHA256_C)
Janos Follath 0:1f7c5025e59d 425 if( todo.sha256 )
Janos Follath 0:1f7c5025e59d 426 TIME_AND_TSC( "SHA-256", mbedtls_sha256( buf, BUFSIZE, tmp, 0 ) );
Janos Follath 0:1f7c5025e59d 427 #endif
Janos Follath 0:1f7c5025e59d 428
Janos Follath 0:1f7c5025e59d 429 #if defined(MBEDTLS_SHA512_C)
Janos Follath 0:1f7c5025e59d 430 if( todo.sha512 )
Janos Follath 0:1f7c5025e59d 431 TIME_AND_TSC( "SHA-512", mbedtls_sha512( buf, BUFSIZE, tmp, 0 ) );
Janos Follath 0:1f7c5025e59d 432 #endif
Janos Follath 0:1f7c5025e59d 433
Janos Follath 0:1f7c5025e59d 434 #if defined(MBEDTLS_ARC4_C)
Janos Follath 0:1f7c5025e59d 435 if( todo.arc4 )
Janos Follath 0:1f7c5025e59d 436 {
Janos Follath 0:1f7c5025e59d 437 mbedtls_arc4_context arc4;
Janos Follath 0:1f7c5025e59d 438 mbedtls_arc4_init( &arc4 );
Janos Follath 0:1f7c5025e59d 439 mbedtls_arc4_setup( &arc4, tmp, 32 );
Janos Follath 0:1f7c5025e59d 440 TIME_AND_TSC( "ARC4", mbedtls_arc4_crypt( &arc4, BUFSIZE, buf, buf ) );
Janos Follath 0:1f7c5025e59d 441 mbedtls_arc4_free( &arc4 );
Janos Follath 0:1f7c5025e59d 442 }
Janos Follath 0:1f7c5025e59d 443 #endif
Janos Follath 0:1f7c5025e59d 444
Janos Follath 0:1f7c5025e59d 445 #if defined(MBEDTLS_DES_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
Janos Follath 0:1f7c5025e59d 446 if( todo.des3 )
Janos Follath 0:1f7c5025e59d 447 {
Janos Follath 0:1f7c5025e59d 448 mbedtls_des3_context des3;
Janos Follath 0:1f7c5025e59d 449 mbedtls_des3_init( &des3 );
Janos Follath 0:1f7c5025e59d 450 mbedtls_des3_set3key_enc( &des3, tmp );
Janos Follath 0:1f7c5025e59d 451 TIME_AND_TSC( "3DES",
Janos Follath 0:1f7c5025e59d 452 mbedtls_des3_crypt_cbc( &des3, MBEDTLS_DES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
Janos Follath 0:1f7c5025e59d 453 mbedtls_des3_free( &des3 );
Janos Follath 0:1f7c5025e59d 454 }
Janos Follath 0:1f7c5025e59d 455
Janos Follath 0:1f7c5025e59d 456 if( todo.des )
Janos Follath 0:1f7c5025e59d 457 {
Janos Follath 0:1f7c5025e59d 458 mbedtls_des_context des;
Janos Follath 0:1f7c5025e59d 459 mbedtls_des_init( &des );
Janos Follath 0:1f7c5025e59d 460 mbedtls_des_setkey_enc( &des, tmp );
Janos Follath 0:1f7c5025e59d 461 TIME_AND_TSC( "DES",
Janos Follath 0:1f7c5025e59d 462 mbedtls_des_crypt_cbc( &des, MBEDTLS_DES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
Janos Follath 0:1f7c5025e59d 463 mbedtls_des_free( &des );
Janos Follath 0:1f7c5025e59d 464 }
mbed_official 11:6ccae3ebafd5 465 #if defined(MBEDTLS_CMAC_C)
mbed_official 11:6ccae3ebafd5 466 if( todo.des3_cmac )
mbed_official 11:6ccae3ebafd5 467 {
mbed_official 11:6ccae3ebafd5 468 unsigned char output[8];
mbed_official 11:6ccae3ebafd5 469 const mbedtls_cipher_info_t *cipher_info;
mbed_official 11:6ccae3ebafd5 470
mbed_official 11:6ccae3ebafd5 471 memset( buf, 0, sizeof( buf ) );
mbed_official 11:6ccae3ebafd5 472 memset( tmp, 0, sizeof( tmp ) );
mbed_official 11:6ccae3ebafd5 473
mbed_official 11:6ccae3ebafd5 474 cipher_info = mbedtls_cipher_info_from_type( MBEDTLS_CIPHER_DES_EDE3_ECB );
mbed_official 11:6ccae3ebafd5 475
mbed_official 11:6ccae3ebafd5 476 TIME_AND_TSC( "3DES-CMAC",
mbed_official 11:6ccae3ebafd5 477 mbedtls_cipher_cmac( cipher_info, tmp, 192, buf,
mbed_official 11:6ccae3ebafd5 478 BUFSIZE, output ) );
mbed_official 11:6ccae3ebafd5 479 }
mbed_official 11:6ccae3ebafd5 480 #endif /* MBEDTLS_CMAC_C */
Janos Follath 0:1f7c5025e59d 481 #endif
Janos Follath 0:1f7c5025e59d 482
Janos Follath 0:1f7c5025e59d 483 #if defined(MBEDTLS_AES_C)
Janos Follath 0:1f7c5025e59d 484 #if defined(MBEDTLS_CIPHER_MODE_CBC)
Janos Follath 0:1f7c5025e59d 485 if( todo.aes_cbc )
Janos Follath 0:1f7c5025e59d 486 {
Janos Follath 0:1f7c5025e59d 487 int keysize;
Janos Follath 0:1f7c5025e59d 488 mbedtls_aes_context aes;
Janos Follath 0:1f7c5025e59d 489 mbedtls_aes_init( &aes );
Janos Follath 0:1f7c5025e59d 490 for( keysize = 128; keysize <= 256; keysize += 64 )
Janos Follath 0:1f7c5025e59d 491 {
Janos Follath 0:1f7c5025e59d 492 mbedtls_snprintf( title, sizeof( title ), "AES-CBC-%d", keysize );
Janos Follath 0:1f7c5025e59d 493
Janos Follath 0:1f7c5025e59d 494 memset( buf, 0, sizeof( buf ) );
Janos Follath 0:1f7c5025e59d 495 memset( tmp, 0, sizeof( tmp ) );
Janos Follath 0:1f7c5025e59d 496 mbedtls_aes_setkey_enc( &aes, tmp, keysize );
Janos Follath 0:1f7c5025e59d 497
Janos Follath 0:1f7c5025e59d 498 TIME_AND_TSC( title,
Janos Follath 0:1f7c5025e59d 499 mbedtls_aes_crypt_cbc( &aes, MBEDTLS_AES_ENCRYPT, BUFSIZE, tmp, buf, buf ) );
Janos Follath 0:1f7c5025e59d 500 }
Janos Follath 0:1f7c5025e59d 501 mbedtls_aes_free( &aes );
Janos Follath 0:1f7c5025e59d 502 }
Janos Follath 0:1f7c5025e59d 503 #endif
Janos Follath 0:1f7c5025e59d 504 #if defined(MBEDTLS_GCM_C)
Janos Follath 0:1f7c5025e59d 505 if( todo.aes_gcm )
Janos Follath 0:1f7c5025e59d 506 {
Janos Follath 0:1f7c5025e59d 507 int keysize;
Janos Follath 0:1f7c5025e59d 508 mbedtls_gcm_context gcm;
Janos Follath 0:1f7c5025e59d 509
Janos Follath 0:1f7c5025e59d 510 mbedtls_gcm_init( &gcm );
Janos Follath 0:1f7c5025e59d 511 for( keysize = 128; keysize <= 256; keysize += 64 )
Janos Follath 0:1f7c5025e59d 512 {
Janos Follath 0:1f7c5025e59d 513 mbedtls_snprintf( title, sizeof( title ), "AES-GCM-%d", keysize );
Janos Follath 0:1f7c5025e59d 514
Janos Follath 0:1f7c5025e59d 515 memset( buf, 0, sizeof( buf ) );
Janos Follath 0:1f7c5025e59d 516 memset( tmp, 0, sizeof( tmp ) );
Janos Follath 0:1f7c5025e59d 517 mbedtls_gcm_setkey( &gcm, MBEDTLS_CIPHER_ID_AES, tmp, keysize );
Janos Follath 0:1f7c5025e59d 518
Janos Follath 0:1f7c5025e59d 519 TIME_AND_TSC( title,
Janos Follath 0:1f7c5025e59d 520 mbedtls_gcm_crypt_and_tag( &gcm, MBEDTLS_GCM_ENCRYPT, BUFSIZE, tmp,
Janos Follath 0:1f7c5025e59d 521 12, NULL, 0, buf, buf, 16, tmp ) );
Janos Follath 0:1f7c5025e59d 522
Janos Follath 0:1f7c5025e59d 523 mbedtls_gcm_free( &gcm );
Janos Follath 0:1f7c5025e59d 524 }
Janos Follath 0:1f7c5025e59d 525 }
Janos Follath 0:1f7c5025e59d 526 #endif
Janos Follath 0:1f7c5025e59d 527 #if defined(MBEDTLS_CCM_C)
Janos Follath 0:1f7c5025e59d 528 if( todo.aes_ccm )
Janos Follath 0:1f7c5025e59d 529 {
Janos Follath 0:1f7c5025e59d 530 int keysize;
Janos Follath 0:1f7c5025e59d 531 mbedtls_ccm_context ccm;
Janos Follath 0:1f7c5025e59d 532
Janos Follath 0:1f7c5025e59d 533 mbedtls_ccm_init( &ccm );
Janos Follath 0:1f7c5025e59d 534 for( keysize = 128; keysize <= 256; keysize += 64 )
Janos Follath 0:1f7c5025e59d 535 {
Janos Follath 0:1f7c5025e59d 536 mbedtls_snprintf( title, sizeof( title ), "AES-CCM-%d", keysize );
Janos Follath 0:1f7c5025e59d 537
Janos Follath 0:1f7c5025e59d 538 memset( buf, 0, sizeof( buf ) );
Janos Follath 0:1f7c5025e59d 539 memset( tmp, 0, sizeof( tmp ) );
Janos Follath 0:1f7c5025e59d 540 mbedtls_ccm_setkey( &ccm, MBEDTLS_CIPHER_ID_AES, tmp, keysize );
Janos Follath 0:1f7c5025e59d 541
Janos Follath 0:1f7c5025e59d 542 TIME_AND_TSC( title,
Janos Follath 0:1f7c5025e59d 543 mbedtls_ccm_encrypt_and_tag( &ccm, BUFSIZE, tmp,
Janos Follath 0:1f7c5025e59d 544 12, NULL, 0, buf, buf, tmp, 16 ) );
Janos Follath 0:1f7c5025e59d 545
Janos Follath 0:1f7c5025e59d 546 mbedtls_ccm_free( &ccm );
Janos Follath 0:1f7c5025e59d 547 }
Janos Follath 0:1f7c5025e59d 548 }
Janos Follath 0:1f7c5025e59d 549 #endif
mbed_official 11:6ccae3ebafd5 550 #if defined(MBEDTLS_CMAC_C)
mbed_official 11:6ccae3ebafd5 551 if( todo.aes_cmac )
mbed_official 11:6ccae3ebafd5 552 {
mbed_official 11:6ccae3ebafd5 553 unsigned char output[16];
mbed_official 11:6ccae3ebafd5 554 const mbedtls_cipher_info_t *cipher_info;
mbed_official 11:6ccae3ebafd5 555 mbedtls_cipher_type_t cipher_type;
mbed_official 11:6ccae3ebafd5 556 int keysize;
mbed_official 11:6ccae3ebafd5 557
mbed_official 11:6ccae3ebafd5 558 cipher_type = MBEDTLS_CIPHER_AES_128_ECB;
mbed_official 11:6ccae3ebafd5 559 for( keysize = 128; keysize <= 256; keysize += 64 )
mbed_official 11:6ccae3ebafd5 560 {
mbed_official 11:6ccae3ebafd5 561 mbedtls_snprintf( title, sizeof( title ), "AES-CMAC-%d", keysize );
mbed_official 11:6ccae3ebafd5 562
mbed_official 11:6ccae3ebafd5 563 memset( buf, 0, sizeof( buf ) );
mbed_official 11:6ccae3ebafd5 564 memset( tmp, 0, sizeof( tmp ) );
mbed_official 11:6ccae3ebafd5 565
mbed_official 11:6ccae3ebafd5 566 cipher_info = mbedtls_cipher_info_from_type( cipher_type );
mbed_official 11:6ccae3ebafd5 567
mbed_official 11:6ccae3ebafd5 568 TIME_AND_TSC( title,
mbed_official 11:6ccae3ebafd5 569 mbedtls_cipher_cmac( cipher_info, tmp, keysize,
mbed_official 11:6ccae3ebafd5 570 buf, BUFSIZE, output ) );
mbed_official 11:6ccae3ebafd5 571 cipher_type = (mbedtls_cipher_type_t)( cipher_type + 1 );
mbed_official 11:6ccae3ebafd5 572 }
mbed_official 11:6ccae3ebafd5 573
mbed_official 11:6ccae3ebafd5 574 memset( buf, 0, sizeof( buf ) );
mbed_official 11:6ccae3ebafd5 575 memset( tmp, 0, sizeof( tmp ) );
mbed_official 11:6ccae3ebafd5 576 TIME_AND_TSC( "AES-CMAC-PRF-128",
mbed_official 11:6ccae3ebafd5 577 mbedtls_aes_cmac_prf_128( tmp, 16, buf, BUFSIZE,
mbed_official 11:6ccae3ebafd5 578 output ) );
mbed_official 11:6ccae3ebafd5 579 }
mbed_official 11:6ccae3ebafd5 580 #endif /* MBEDTLS_CMAC_C */
Janos Follath 0:1f7c5025e59d 581 #endif
Janos Follath 0:1f7c5025e59d 582
Janos Follath 0:1f7c5025e59d 583 #if defined(MBEDTLS_CAMELLIA_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
Janos Follath 0:1f7c5025e59d 584 if( todo.camellia )
Janos Follath 0:1f7c5025e59d 585 {
Janos Follath 0:1f7c5025e59d 586 int keysize;
Janos Follath 0:1f7c5025e59d 587 mbedtls_camellia_context camellia;
Janos Follath 0:1f7c5025e59d 588 mbedtls_camellia_init( &camellia );
Janos Follath 0:1f7c5025e59d 589 for( keysize = 128; keysize <= 256; keysize += 64 )
Janos Follath 0:1f7c5025e59d 590 {
Janos Follath 0:1f7c5025e59d 591 mbedtls_snprintf( title, sizeof( title ), "CAMELLIA-CBC-%d", keysize );
Janos Follath 0:1f7c5025e59d 592
Janos Follath 0:1f7c5025e59d 593 memset( buf, 0, sizeof( buf ) );
Janos Follath 0:1f7c5025e59d 594 memset( tmp, 0, sizeof( tmp ) );
Janos Follath 0:1f7c5025e59d 595 mbedtls_camellia_setkey_enc( &camellia, tmp, keysize );
Janos Follath 0:1f7c5025e59d 596
Janos Follath 0:1f7c5025e59d 597 TIME_AND_TSC( title,
Janos Follath 0:1f7c5025e59d 598 mbedtls_camellia_crypt_cbc( &camellia, MBEDTLS_CAMELLIA_ENCRYPT,
Janos Follath 0:1f7c5025e59d 599 BUFSIZE, tmp, buf, buf ) );
Janos Follath 0:1f7c5025e59d 600 }
Janos Follath 0:1f7c5025e59d 601 mbedtls_camellia_free( &camellia );
Janos Follath 0:1f7c5025e59d 602 }
Janos Follath 0:1f7c5025e59d 603 #endif
Janos Follath 0:1f7c5025e59d 604
Janos Follath 0:1f7c5025e59d 605 #if defined(MBEDTLS_BLOWFISH_C) && defined(MBEDTLS_CIPHER_MODE_CBC)
Janos Follath 0:1f7c5025e59d 606 if( todo.blowfish )
Janos Follath 0:1f7c5025e59d 607 {
Janos Follath 0:1f7c5025e59d 608 int keysize;
Janos Follath 0:1f7c5025e59d 609 mbedtls_blowfish_context blowfish;
Janos Follath 0:1f7c5025e59d 610 mbedtls_blowfish_init( &blowfish );
Janos Follath 0:1f7c5025e59d 611
Janos Follath 0:1f7c5025e59d 612 for( keysize = 128; keysize <= 256; keysize += 64 )
Janos Follath 0:1f7c5025e59d 613 {
Janos Follath 0:1f7c5025e59d 614 mbedtls_snprintf( title, sizeof( title ), "BLOWFISH-CBC-%d", keysize );
Janos Follath 0:1f7c5025e59d 615
Janos Follath 0:1f7c5025e59d 616 memset( buf, 0, sizeof( buf ) );
Janos Follath 0:1f7c5025e59d 617 memset( tmp, 0, sizeof( tmp ) );
Janos Follath 0:1f7c5025e59d 618 mbedtls_blowfish_setkey( &blowfish, tmp, keysize );
Janos Follath 0:1f7c5025e59d 619
Janos Follath 0:1f7c5025e59d 620 TIME_AND_TSC( title,
Janos Follath 0:1f7c5025e59d 621 mbedtls_blowfish_crypt_cbc( &blowfish, MBEDTLS_BLOWFISH_ENCRYPT, BUFSIZE,
Janos Follath 0:1f7c5025e59d 622 tmp, buf, buf ) );
Janos Follath 0:1f7c5025e59d 623 }
Janos Follath 0:1f7c5025e59d 624
Janos Follath 0:1f7c5025e59d 625 mbedtls_blowfish_free( &blowfish );
Janos Follath 0:1f7c5025e59d 626 }
Janos Follath 0:1f7c5025e59d 627 #endif
Janos Follath 0:1f7c5025e59d 628
Janos Follath 0:1f7c5025e59d 629 #if defined(MBEDTLS_HAVEGE_C)
Janos Follath 0:1f7c5025e59d 630 if( todo.havege )
Janos Follath 0:1f7c5025e59d 631 {
Janos Follath 0:1f7c5025e59d 632 mbedtls_havege_state hs;
Janos Follath 0:1f7c5025e59d 633 mbedtls_havege_init( &hs );
Janos Follath 0:1f7c5025e59d 634 TIME_AND_TSC( "HAVEGE", mbedtls_havege_random( &hs, buf, BUFSIZE ) );
Janos Follath 0:1f7c5025e59d 635 mbedtls_havege_free( &hs );
Janos Follath 0:1f7c5025e59d 636 }
Janos Follath 0:1f7c5025e59d 637 #endif
Janos Follath 0:1f7c5025e59d 638
Janos Follath 0:1f7c5025e59d 639 #if defined(MBEDTLS_CTR_DRBG_C)
Janos Follath 0:1f7c5025e59d 640 if( todo.ctr_drbg )
Janos Follath 0:1f7c5025e59d 641 {
Janos Follath 0:1f7c5025e59d 642 mbedtls_ctr_drbg_context ctr_drbg;
Janos Follath 0:1f7c5025e59d 643
Janos Follath 0:1f7c5025e59d 644 mbedtls_ctr_drbg_init( &ctr_drbg );
Janos Follath 0:1f7c5025e59d 645
Janos Follath 0:1f7c5025e59d 646 if( mbedtls_ctr_drbg_seed( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 )
Janos Follath 0:1f7c5025e59d 647 mbedtls_exit(1);
Janos Follath 0:1f7c5025e59d 648 TIME_AND_TSC( "CTR_DRBG (NOPR)",
Janos Follath 0:1f7c5025e59d 649 if( mbedtls_ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) != 0 )
Janos Follath 0:1f7c5025e59d 650 mbedtls_exit(1) );
Janos Follath 0:1f7c5025e59d 651
Janos Follath 0:1f7c5025e59d 652 if( mbedtls_ctr_drbg_seed( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 )
Janos Follath 0:1f7c5025e59d 653 mbedtls_exit(1);
Janos Follath 0:1f7c5025e59d 654 mbedtls_ctr_drbg_set_prediction_resistance( &ctr_drbg, MBEDTLS_CTR_DRBG_PR_ON );
Janos Follath 0:1f7c5025e59d 655 TIME_AND_TSC( "CTR_DRBG (PR)",
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 mbedtls_ctr_drbg_free( &ctr_drbg );
Janos Follath 0:1f7c5025e59d 659 }
Janos Follath 0:1f7c5025e59d 660 #endif
Janos Follath 0:1f7c5025e59d 661
Janos Follath 0:1f7c5025e59d 662 #if defined(MBEDTLS_HMAC_DRBG_C)
Janos Follath 0:1f7c5025e59d 663 if( todo.hmac_drbg )
Janos Follath 0:1f7c5025e59d 664 {
Janos Follath 0:1f7c5025e59d 665 mbedtls_hmac_drbg_context hmac_drbg;
Janos Follath 0:1f7c5025e59d 666 const mbedtls_md_info_t *md_info;
Janos Follath 0:1f7c5025e59d 667
Janos Follath 0:1f7c5025e59d 668 mbedtls_hmac_drbg_init( &hmac_drbg );
Janos Follath 0:1f7c5025e59d 669
Janos Follath 0:1f7c5025e59d 670 #if defined(MBEDTLS_SHA1_C)
Janos Follath 0:1f7c5025e59d 671 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
Janos Follath 0:1f7c5025e59d 672 mbedtls_exit(1);
Janos Follath 0:1f7c5025e59d 673
Janos Follath 0:1f7c5025e59d 674 if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
Janos Follath 0:1f7c5025e59d 675 mbedtls_exit(1);
Janos Follath 0:1f7c5025e59d 676 TIME_AND_TSC( "HMAC_DRBG SHA-1 (NOPR)",
Janos Follath 0:1f7c5025e59d 677 if( mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 )
Janos Follath 0:1f7c5025e59d 678 mbedtls_exit(1) );
Janos Follath 0:1f7c5025e59d 679 mbedtls_hmac_drbg_free( &hmac_drbg );
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 mbedtls_hmac_drbg_set_prediction_resistance( &hmac_drbg,
Janos Follath 0:1f7c5025e59d 684 MBEDTLS_HMAC_DRBG_PR_ON );
Janos Follath 0:1f7c5025e59d 685 TIME_AND_TSC( "HMAC_DRBG SHA-1 (PR)",
Janos Follath 0:1f7c5025e59d 686 if( mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 )
Janos Follath 0:1f7c5025e59d 687 mbedtls_exit(1) );
Janos Follath 0:1f7c5025e59d 688 mbedtls_hmac_drbg_free( &hmac_drbg );
Janos Follath 0:1f7c5025e59d 689 #endif
Janos Follath 0:1f7c5025e59d 690
Janos Follath 0:1f7c5025e59d 691 #if defined(MBEDTLS_SHA256_C)
Janos Follath 0:1f7c5025e59d 692 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 ) ) == NULL )
Janos Follath 0:1f7c5025e59d 693 mbedtls_exit(1);
Janos Follath 0:1f7c5025e59d 694
Janos Follath 0:1f7c5025e59d 695 if( mbedtls_hmac_drbg_seed( &hmac_drbg, md_info, myrand, NULL, NULL, 0 ) != 0 )
Janos Follath 0:1f7c5025e59d 696 mbedtls_exit(1);
Janos Follath 0:1f7c5025e59d 697 TIME_AND_TSC( "HMAC_DRBG SHA-256 (NOPR)",
Janos Follath 0:1f7c5025e59d 698 if( mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 )
Janos Follath 0:1f7c5025e59d 699 mbedtls_exit(1) );
Janos Follath 0:1f7c5025e59d 700 mbedtls_hmac_drbg_free( &hmac_drbg );
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 mbedtls_hmac_drbg_set_prediction_resistance( &hmac_drbg,
Janos Follath 0:1f7c5025e59d 705 MBEDTLS_HMAC_DRBG_PR_ON );
Janos Follath 0:1f7c5025e59d 706 TIME_AND_TSC( "HMAC_DRBG SHA-256 (PR)",
Janos Follath 0:1f7c5025e59d 707 if( mbedtls_hmac_drbg_random( &hmac_drbg, buf, BUFSIZE ) != 0 )
Janos Follath 0:1f7c5025e59d 708 mbedtls_exit(1) );
Janos Follath 0:1f7c5025e59d 709 mbedtls_hmac_drbg_free( &hmac_drbg );
Janos Follath 0:1f7c5025e59d 710 #endif
Janos Follath 0:1f7c5025e59d 711 }
Janos Follath 0:1f7c5025e59d 712 #endif
Janos Follath 0:1f7c5025e59d 713
Janos Follath 0:1f7c5025e59d 714 #if defined(MBEDTLS_RSA_C) && \
Janos Follath 0:1f7c5025e59d 715 defined(MBEDTLS_PEM_PARSE_C) && defined(MBEDTLS_PK_PARSE_C)
Janos Follath 0:1f7c5025e59d 716 if( todo.rsa )
Janos Follath 0:1f7c5025e59d 717 {
Janos Follath 0:1f7c5025e59d 718 mbedtls_pk_context pk;
Janos Follath 0:1f7c5025e59d 719 mbedtls_rsa_context *rsa;
Janos Follath 0:1f7c5025e59d 720 const char *rsa_keys[] = { RSA_PRIVATE_KEY_2048, RSA_PRIVATE_KEY_4096 };
Janos Follath 0:1f7c5025e59d 721 size_t i;
Janos Follath 0:1f7c5025e59d 722
Janos Follath 0:1f7c5025e59d 723 for( i = 0; i < sizeof( rsa_keys ) / sizeof( rsa_keys[0] ); i++ )
Janos Follath 0:1f7c5025e59d 724 {
Janos Follath 0:1f7c5025e59d 725 mbedtls_pk_init( &pk );
Janos Follath 0:1f7c5025e59d 726 mbedtls_pk_parse_key( &pk, (const unsigned char *) rsa_keys[i],
Janos Follath 0:1f7c5025e59d 727 strlen( rsa_keys[i] ) + 1, NULL, 0 );
Janos Follath 0:1f7c5025e59d 728 rsa = mbedtls_pk_rsa( pk );
Janos Follath 0:1f7c5025e59d 729
Janos Follath 0:1f7c5025e59d 730 mbedtls_snprintf( title, sizeof( title ), "RSA-%d", mbedtls_pk_get_bitlen( &pk ) );
Janos Follath 0:1f7c5025e59d 731
Janos Follath 0:1f7c5025e59d 732 TIME_PUBLIC( title, " public",
Janos Follath 0:1f7c5025e59d 733 buf[0] = 0;
Janos Follath 0:1f7c5025e59d 734 ret = mbedtls_rsa_public( rsa, buf, buf ) );
Janos Follath 0:1f7c5025e59d 735
Janos Follath 0:1f7c5025e59d 736 TIME_PUBLIC( title, "private",
Janos Follath 0:1f7c5025e59d 737 buf[0] = 0;
Janos Follath 0:1f7c5025e59d 738 ret = mbedtls_rsa_private( rsa, myrand, NULL, buf, buf ) );
Janos Follath 0:1f7c5025e59d 739
Janos Follath 0:1f7c5025e59d 740 mbedtls_pk_free( &pk );
Janos Follath 0:1f7c5025e59d 741 }
Janos Follath 0:1f7c5025e59d 742 }
Janos Follath 0:1f7c5025e59d 743 #endif
Janos Follath 0:1f7c5025e59d 744
Janos Follath 0:1f7c5025e59d 745 #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_BIGNUM_C)
Janos Follath 0:1f7c5025e59d 746 if( todo.dhm )
Janos Follath 0:1f7c5025e59d 747 {
Janos Follath 0:1f7c5025e59d 748 int dhm_sizes[] = { 2048, 3072 };
Janos Follath 0:1f7c5025e59d 749 const char *dhm_P[] = {
Janos Follath 0:1f7c5025e59d 750 MBEDTLS_DHM_RFC3526_MODP_2048_P,
Janos Follath 0:1f7c5025e59d 751 MBEDTLS_DHM_RFC3526_MODP_3072_P,
Janos Follath 0:1f7c5025e59d 752 };
Janos Follath 0:1f7c5025e59d 753 const char *dhm_G[] = {
Janos Follath 0:1f7c5025e59d 754 MBEDTLS_DHM_RFC3526_MODP_2048_G,
Janos Follath 0:1f7c5025e59d 755 MBEDTLS_DHM_RFC3526_MODP_3072_G,
Janos Follath 0:1f7c5025e59d 756 };
Janos Follath 0:1f7c5025e59d 757
Janos Follath 0:1f7c5025e59d 758 mbedtls_dhm_context dhm;
Janos Follath 0:1f7c5025e59d 759 size_t olen;
Janos Follath 0:1f7c5025e59d 760 for( i = 0; (size_t) i < sizeof( dhm_sizes ) / sizeof( dhm_sizes[0] ); i++ )
Janos Follath 0:1f7c5025e59d 761 {
Janos Follath 0:1f7c5025e59d 762 mbedtls_dhm_init( &dhm );
Janos Follath 0:1f7c5025e59d 763
Janos Follath 0:1f7c5025e59d 764 if( mbedtls_mpi_read_string( &dhm.P, 16, dhm_P[i] ) != 0 ||
Janos Follath 0:1f7c5025e59d 765 mbedtls_mpi_read_string( &dhm.G, 16, dhm_G[i] ) != 0 )
Janos Follath 0:1f7c5025e59d 766 {
Janos Follath 0:1f7c5025e59d 767 mbedtls_exit( 1 );
Janos Follath 0:1f7c5025e59d 768 }
Janos Follath 0:1f7c5025e59d 769
Janos Follath 0:1f7c5025e59d 770 dhm.len = mbedtls_mpi_size( &dhm.P );
Janos Follath 0:1f7c5025e59d 771 mbedtls_dhm_make_public( &dhm, (int) dhm.len, buf, dhm.len, myrand, NULL );
Janos Follath 0:1f7c5025e59d 772 if( mbedtls_mpi_copy( &dhm.GY, &dhm.GX ) != 0 )
Janos Follath 0:1f7c5025e59d 773 mbedtls_exit( 1 );
Janos Follath 0:1f7c5025e59d 774
Janos Follath 0:1f7c5025e59d 775 mbedtls_snprintf( title, sizeof( title ), "DHE-%d", dhm_sizes[i] );
Janos Follath 0:1f7c5025e59d 776 TIME_PUBLIC( title, "handshake",
Janos Follath 0:1f7c5025e59d 777 ret |= mbedtls_dhm_make_public( &dhm, (int) dhm.len, buf, dhm.len,
Janos Follath 0:1f7c5025e59d 778 myrand, NULL );
Janos Follath 0:1f7c5025e59d 779 ret |= mbedtls_dhm_calc_secret( &dhm, buf, sizeof( buf ), &olen, myrand, NULL ) );
Janos Follath 0:1f7c5025e59d 780
Janos Follath 0:1f7c5025e59d 781 mbedtls_snprintf( title, sizeof( title ), "DH-%d", dhm_sizes[i] );
Janos Follath 0:1f7c5025e59d 782 TIME_PUBLIC( title, "handshake",
Janos Follath 0:1f7c5025e59d 783 ret |= mbedtls_dhm_calc_secret( &dhm, buf, sizeof( buf ), &olen, myrand, NULL ) );
Janos Follath 0:1f7c5025e59d 784
Janos Follath 0:1f7c5025e59d 785 mbedtls_dhm_free( &dhm );
Janos Follath 0:1f7c5025e59d 786 }
Janos Follath 0:1f7c5025e59d 787 }
Janos Follath 0:1f7c5025e59d 788 #endif
Janos Follath 0:1f7c5025e59d 789
mbed_official 11:6ccae3ebafd5 790 #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA256_C) && defined(ENABLE_ECDSA)
Janos Follath 0:1f7c5025e59d 791 if( todo.ecdsa )
Janos Follath 0:1f7c5025e59d 792 {
Janos Follath 0:1f7c5025e59d 793 mbedtls_ecdsa_context ecdsa;
Janos Follath 0:1f7c5025e59d 794 const mbedtls_ecp_curve_info *curve_info;
Janos Follath 0:1f7c5025e59d 795 size_t sig_len;
Janos Follath 0:1f7c5025e59d 796
Janos Follath 0:1f7c5025e59d 797 memset( buf, 0x2A, sizeof( buf ) );
Janos Follath 0:1f7c5025e59d 798
Janos Follath 0:1f7c5025e59d 799 for( curve_info = mbedtls_ecp_curve_list();
Janos Follath 0:1f7c5025e59d 800 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Janos Follath 0:1f7c5025e59d 801 curve_info++ )
Janos Follath 0:1f7c5025e59d 802 {
Janos Follath 0:1f7c5025e59d 803 mbedtls_ecdsa_init( &ecdsa );
Janos Follath 0:1f7c5025e59d 804
Janos Follath 0:1f7c5025e59d 805 if( mbedtls_ecdsa_genkey( &ecdsa, curve_info->grp_id, myrand, NULL ) != 0 )
Janos Follath 0:1f7c5025e59d 806 mbedtls_exit( 1 );
Janos Follath 0:1f7c5025e59d 807 ecp_clear_precomputed( &ecdsa.grp );
Janos Follath 0:1f7c5025e59d 808
Janos Follath 0:1f7c5025e59d 809 mbedtls_snprintf( title, sizeof( title ), "ECDSA-%s",
Janos Follath 0:1f7c5025e59d 810 curve_info->name );
Janos Follath 0:1f7c5025e59d 811 TIME_PUBLIC( title, "sign",
Janos Follath 0:1f7c5025e59d 812 ret = mbedtls_ecdsa_write_signature( &ecdsa, MBEDTLS_MD_SHA256, buf, curve_info->bit_size,
Janos Follath 0:1f7c5025e59d 813 tmp, &sig_len, myrand, NULL ) );
Janos Follath 0:1f7c5025e59d 814
Janos Follath 0:1f7c5025e59d 815 mbedtls_ecdsa_free( &ecdsa );
Janos Follath 0:1f7c5025e59d 816 }
Janos Follath 0:1f7c5025e59d 817
Janos Follath 0:1f7c5025e59d 818 for( curve_info = mbedtls_ecp_curve_list();
Janos Follath 0:1f7c5025e59d 819 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Janos Follath 0:1f7c5025e59d 820 curve_info++ )
Janos Follath 0:1f7c5025e59d 821 {
Janos Follath 0:1f7c5025e59d 822 mbedtls_ecdsa_init( &ecdsa );
Janos Follath 0:1f7c5025e59d 823
Janos Follath 0:1f7c5025e59d 824 if( mbedtls_ecdsa_genkey( &ecdsa, curve_info->grp_id, myrand, NULL ) != 0 ||
Janos Follath 0:1f7c5025e59d 825 mbedtls_ecdsa_write_signature( &ecdsa, MBEDTLS_MD_SHA256, buf, curve_info->bit_size,
Janos Follath 0:1f7c5025e59d 826 tmp, &sig_len, myrand, NULL ) != 0 )
Janos Follath 0:1f7c5025e59d 827 {
Janos Follath 0:1f7c5025e59d 828 mbedtls_exit( 1 );
Janos Follath 0:1f7c5025e59d 829 }
Janos Follath 0:1f7c5025e59d 830 ecp_clear_precomputed( &ecdsa.grp );
Janos Follath 0:1f7c5025e59d 831
Janos Follath 0:1f7c5025e59d 832 mbedtls_snprintf( title, sizeof( title ), "ECDSA-%s",
Janos Follath 0:1f7c5025e59d 833 curve_info->name );
Janos Follath 0:1f7c5025e59d 834 TIME_PUBLIC( title, "verify",
Janos Follath 0:1f7c5025e59d 835 ret = mbedtls_ecdsa_read_signature( &ecdsa, buf, curve_info->bit_size,
Janos Follath 0:1f7c5025e59d 836 tmp, sig_len ) );
Janos Follath 0:1f7c5025e59d 837
Janos Follath 0:1f7c5025e59d 838 mbedtls_ecdsa_free( &ecdsa );
Janos Follath 0:1f7c5025e59d 839 }
Janos Follath 0:1f7c5025e59d 840 }
Janos Follath 0:1f7c5025e59d 841 #endif
Janos Follath 0:1f7c5025e59d 842
Janos Follath 0:1f7c5025e59d 843 #if defined(MBEDTLS_ECDH_C)
Janos Follath 0:1f7c5025e59d 844 if( todo.ecdh )
Janos Follath 0:1f7c5025e59d 845 {
Janos Follath 0:1f7c5025e59d 846 mbedtls_ecdh_context ecdh;
Janos Follath 0:1f7c5025e59d 847 #if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
Janos Follath 0:1f7c5025e59d 848 mbedtls_mpi z;
Janos Follath 0:1f7c5025e59d 849 #endif
Janos Follath 0:1f7c5025e59d 850 const mbedtls_ecp_curve_info *curve_info;
Janos Follath 0:1f7c5025e59d 851 size_t olen;
Janos Follath 0:1f7c5025e59d 852
Janos Follath 0:1f7c5025e59d 853 for( curve_info = mbedtls_ecp_curve_list();
Janos Follath 0:1f7c5025e59d 854 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Janos Follath 0:1f7c5025e59d 855 curve_info++ )
Janos Follath 0:1f7c5025e59d 856 {
Janos Follath 0:1f7c5025e59d 857 mbedtls_ecdh_init( &ecdh );
Janos Follath 0:1f7c5025e59d 858
Janos Follath 0:1f7c5025e59d 859 if( mbedtls_ecp_group_load( &ecdh.grp, curve_info->grp_id ) != 0 ||
Janos Follath 0:1f7c5025e59d 860 mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf ),
Janos Follath 0:1f7c5025e59d 861 myrand, NULL ) != 0 ||
Janos Follath 0:1f7c5025e59d 862 mbedtls_ecp_copy( &ecdh.Qp, &ecdh.Q ) != 0 )
Janos Follath 0:1f7c5025e59d 863 {
Janos Follath 0:1f7c5025e59d 864 mbedtls_exit( 1 );
Janos Follath 0:1f7c5025e59d 865 }
Janos Follath 0:1f7c5025e59d 866 ecp_clear_precomputed( &ecdh.grp );
Janos Follath 0:1f7c5025e59d 867
Janos Follath 0:1f7c5025e59d 868 mbedtls_snprintf( title, sizeof( title ), "ECDHE-%s",
Janos Follath 0:1f7c5025e59d 869 curve_info->name );
Janos Follath 0:1f7c5025e59d 870 TIME_PUBLIC( title, "handshake",
Janos Follath 0:1f7c5025e59d 871 ret |= mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf ),
Janos Follath 0:1f7c5025e59d 872 myrand, NULL );
Janos Follath 0:1f7c5025e59d 873 ret |= mbedtls_ecdh_calc_secret( &ecdh, &olen, buf, sizeof( buf ),
Janos Follath 0:1f7c5025e59d 874 myrand, NULL ) );
Janos Follath 0:1f7c5025e59d 875 mbedtls_ecdh_free( &ecdh );
Janos Follath 0:1f7c5025e59d 876 }
Janos Follath 0:1f7c5025e59d 877
Janos Follath 0:1f7c5025e59d 878 /* Curve25519 needs to be handled separately */
Janos Follath 0:1f7c5025e59d 879 #if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
Janos Follath 0:1f7c5025e59d 880 mbedtls_ecdh_init( &ecdh );
Janos Follath 0:1f7c5025e59d 881 mbedtls_mpi_init( &z );
Janos Follath 0:1f7c5025e59d 882
Janos Follath 0:1f7c5025e59d 883 if( mbedtls_ecp_group_load( &ecdh.grp, MBEDTLS_ECP_DP_CURVE25519 ) != 0 ||
Janos Follath 0:1f7c5025e59d 884 mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Qp, myrand, NULL ) != 0 )
Janos Follath 0:1f7c5025e59d 885 {
Janos Follath 0:1f7c5025e59d 886 mbedtls_exit( 1 );
Janos Follath 0:1f7c5025e59d 887 }
Janos Follath 0:1f7c5025e59d 888
Janos Follath 0:1f7c5025e59d 889 TIME_PUBLIC( "ECDHE-Curve25519", "handshake",
Janos Follath 0:1f7c5025e59d 890 ret |= mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Q,
Janos Follath 0:1f7c5025e59d 891 myrand, NULL );
Janos Follath 0:1f7c5025e59d 892 ret |= mbedtls_ecdh_compute_shared( &ecdh.grp, &z, &ecdh.Qp, &ecdh.d,
Janos Follath 0:1f7c5025e59d 893 myrand, NULL ) );
Janos Follath 0:1f7c5025e59d 894
Janos Follath 0:1f7c5025e59d 895 mbedtls_ecdh_free( &ecdh );
Janos Follath 0:1f7c5025e59d 896 mbedtls_mpi_free( &z );
Janos Follath 0:1f7c5025e59d 897 #endif
Janos Follath 0:1f7c5025e59d 898
Janos Follath 0:1f7c5025e59d 899 for( curve_info = mbedtls_ecp_curve_list();
Janos Follath 0:1f7c5025e59d 900 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Janos Follath 0:1f7c5025e59d 901 curve_info++ )
Janos Follath 0:1f7c5025e59d 902 {
Janos Follath 0:1f7c5025e59d 903 mbedtls_ecdh_init( &ecdh );
Janos Follath 0:1f7c5025e59d 904
Janos Follath 0:1f7c5025e59d 905 if( mbedtls_ecp_group_load( &ecdh.grp, curve_info->grp_id ) != 0 ||
Janos Follath 0:1f7c5025e59d 906 mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf ),
Janos Follath 0:1f7c5025e59d 907 myrand, NULL ) != 0 ||
Janos Follath 0:1f7c5025e59d 908 mbedtls_ecp_copy( &ecdh.Qp, &ecdh.Q ) != 0 ||
Janos Follath 0:1f7c5025e59d 909 mbedtls_ecdh_make_public( &ecdh, &olen, buf, sizeof( buf ),
Janos Follath 0:1f7c5025e59d 910 myrand, NULL ) != 0 )
Janos Follath 0:1f7c5025e59d 911 {
Janos Follath 0:1f7c5025e59d 912 mbedtls_exit( 1 );
Janos Follath 0:1f7c5025e59d 913 }
Janos Follath 0:1f7c5025e59d 914 ecp_clear_precomputed( &ecdh.grp );
Janos Follath 0:1f7c5025e59d 915
Janos Follath 0:1f7c5025e59d 916 mbedtls_snprintf( title, sizeof( title ), "ECDH-%s",
Janos Follath 0:1f7c5025e59d 917 curve_info->name );
Janos Follath 0:1f7c5025e59d 918 TIME_PUBLIC( title, "handshake",
Janos Follath 0:1f7c5025e59d 919 ret |= mbedtls_ecdh_calc_secret( &ecdh, &olen, buf, sizeof( buf ),
Janos Follath 0:1f7c5025e59d 920 myrand, NULL ) );
Janos Follath 0:1f7c5025e59d 921 mbedtls_ecdh_free( &ecdh );
Janos Follath 0:1f7c5025e59d 922 }
Janos Follath 0:1f7c5025e59d 923
Janos Follath 0:1f7c5025e59d 924 /* Curve25519 needs to be handled separately */
Janos Follath 0:1f7c5025e59d 925 #if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
Janos Follath 0:1f7c5025e59d 926 mbedtls_ecdh_init( &ecdh );
Janos Follath 0:1f7c5025e59d 927 mbedtls_mpi_init( &z );
Janos Follath 0:1f7c5025e59d 928
Janos Follath 0:1f7c5025e59d 929 if( mbedtls_ecp_group_load( &ecdh.grp, MBEDTLS_ECP_DP_CURVE25519 ) != 0 ||
Janos Follath 0:1f7c5025e59d 930 mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Qp,
Janos Follath 0:1f7c5025e59d 931 myrand, NULL ) != 0 ||
Janos Follath 0:1f7c5025e59d 932 mbedtls_ecdh_gen_public( &ecdh.grp, &ecdh.d, &ecdh.Q, myrand, NULL ) != 0 )
Janos Follath 0:1f7c5025e59d 933 {
Janos Follath 0:1f7c5025e59d 934 mbedtls_exit( 1 );
Janos Follath 0:1f7c5025e59d 935 }
Janos Follath 0:1f7c5025e59d 936
Janos Follath 0:1f7c5025e59d 937 TIME_PUBLIC( "ECDH-Curve25519", "handshake",
Janos Follath 0:1f7c5025e59d 938 ret |= mbedtls_ecdh_compute_shared( &ecdh.grp, &z, &ecdh.Qp, &ecdh.d,
Janos Follath 0:1f7c5025e59d 939 myrand, NULL ) );
Janos Follath 0:1f7c5025e59d 940
Janos Follath 0:1f7c5025e59d 941 mbedtls_ecdh_free( &ecdh );
Janos Follath 0:1f7c5025e59d 942 mbedtls_mpi_free( &z );
Janos Follath 0:1f7c5025e59d 943 #endif
Janos Follath 0:1f7c5025e59d 944 }
Janos Follath 0:1f7c5025e59d 945 #endif
Janos Follath 0:1f7c5025e59d 946
Janos Follath 0:1f7c5025e59d 947 mbedtls_printf("\r\nDONE\r\n");
Janos Follath 0:1f7c5025e59d 948
Janos Follath 0:1f7c5025e59d 949 #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Janos Follath 0:1f7c5025e59d 950 mbedtls_memory_buffer_alloc_free();
Janos Follath 0:1f7c5025e59d 951 #endif
Janos Follath 0:1f7c5025e59d 952
Janos Follath 0:1f7c5025e59d 953 return( 0 );
Janos Follath 0:1f7c5025e59d 954 }
Janos Follath 0:1f7c5025e59d 955
Janos Follath 0:1f7c5025e59d 956 int main(void) {
Janos Follath 0:1f7c5025e59d 957 int ret = benchmark(0, NULL);
Janos Follath 0:1f7c5025e59d 958 if (ret != 0) {
Janos Follath 0:1f7c5025e59d 959 mbedtls_printf("Benchmark failed with error %d\r\n", ret);
Janos Follath 0:1f7c5025e59d 960 }
Janos Follath 0:1f7c5025e59d 961 }