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:
Mon Oct 08 17:00:11 2018 +0100
Revision:
76:68ac2a548d58
Parent:
70:72c865037f5d
Child:
78:5f419e0f96dd
Merge pull request #126 from andresag01/line-endings

Change line endings from \r\n to \n only
.
Commit copied from https://github.com/ARMmbed/mbed-os-example-tls

Who changed what in which revision?

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