blob: 754928bf30fafa65b96e08a97909631e6daee010 [file] [log] [blame]
#!/usr/bin/env python3
"""
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
"""
import os
import sys
from bench import time_func
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
from amcl import core_utils, rsa_WWW
pt_2048_hex = "53ea5dc08cd260fb3b858567287fa91552c30b2febfba213f0ae87702d068d19bab07fe574523dfb42139d68c3c5afeee0bfe4cb7969cbf382b804d6e61396144e2d0e60741f8993c3014b58b9b1957a8babcd23af854f4c356fb1662aa72bfcc7e586559dc4280d160c126785a723ebeebeff71f11594440aaef87d10793a8774a239d4a04c87fe1467b9daf85208ec6c7255794a96cc29142f9a8bd418e3c1fd67344b0cd0829df3b2bec60253196293c6b34d3f75d32f213dd45c6273d505adf4cced1057cb758fc26aeefa441255ed4e64c199ee075e7f16646182fdb464739b68ab5daff0e63e9552016824f054bf4d3c8c90a97bb6b6553284eb429fcc"
pt_4096_hex = "53ea5dc08cd260fb3b858567287fa91552c30b2febfba213f0ae87702d068d19bab07fe574523dfb42139d68c3c5afeee0bfe4cb7969cbf382b804d6e61396144e2d0e60741f8993c3014b58b9b1957a8babcd23af854f4c356fb1662aa72bfcc7e586559dc4280d160c126785a723ebeebeff71f11594440aaef87d10793a8774a239d4a04c87fe1467b9daf85208ec6c7255794a96cc29142f9a8bd418e3c1fd67344b0cd0829df3b2bec60253196293c6b34d3f75d32f213dd45c6273d505adf4cced1057cb758fc26aeefa441255ed4e64c199ee075e7f16646182fdb464739b68ab5daff0e63e9552016824f054bf4d3c8c90a97bb6b6553284eb429fcc53ea5dc08cd260fb3b858567287fa91552c30b2febfba213f0ae87702d068d19bab07fe574523dfb42139d68c3c5afeee0bfe4cb7969cbf382b804d6e61396144e2d0e60741f8993c3014b58b9b1957a8babcd23af854f4c356fb1662aa72bfcc7e586559dc4280d160c126785a723ebeebeff71f11594440aaef87d10793a8774a239d4a04c87fe1467b9daf85208ec6c7255794a96cc29142f9a8bd418e3c1fd67344b0cd0829df3b2bec60253196293c6b34d3f75d32f213dd45c6273d505adf4cced1057cb758fc26aeefa441255ed4e64c199ee075e7f16646182fdb464739b68ab5daff0e63e9552016824f054bf4d3c8c90a97bb6b6553284eb429fcc"
e = 0x10001
seed_hex = "78d0fb6705ce77dee47d03eb5b9c5d30"
if __name__ == "__main__":
pt = bytes.fromhex(pt_WWW_hex)
seed = bytes.fromhex(seed_hex)
rng = core_utils.create_csprng(seed)
m = b'test message'
# Generate quantities for bench run
public_key, private_key = rsa_WWW.generate_key_pair(rng, e)
enc, rc = rsa_WWW.oaep_encode(rng, rsa_WWW.SHA256, m)
assert rc == 0, 'OAEP encode failure'
_, rc = rsa_WWW.oaep_decode(rsa_WWW.SHA256, enc)
assert rc == 0, 'OAEP decode failure'
# Run benchmark
fncall = lambda: rsa_WWW.generate_key_pair(rng, e)
time_func("rsa_WWW.generate_key_pair", fncall)
fncall = lambda: rsa_WWW.encrypt(public_key, pt)
time_func("rsa_WWW.encrypt ", fncall, unit = 'us')
fncall = lambda: rsa_WWW.decrypt(private_key, pt)
time_func("rsa_WWW.decrypt ", fncall)
fncall = lambda: rsa_WWW.oaep_encode(rng, rsa_WWW.SHA256, m)
time_func("rsa_WWW.oaep_encode ", fncall, unit = 'us')
fncall = lambda: rsa_WWW.oaep_decode(rsa_WWW.SHA256, enc)
time_func("rsa_WWW.oaep_decode ", fncall, unit = 'us')