Skip to content

Commit 1501b3a

Browse files
committed
cryptolib/botan: Add Dilithium scripts
1 parent 0ae9441 commit 1501b3a

File tree

5 files changed

+288
-0
lines changed

5 files changed

+288
-0
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
bin/
2+
results/
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
all: dilithium
2+
3+
dilithium: dilithium.cpp
4+
mkdir -p bin
5+
g++-11 -std=c++20 -g -O3 -Wall -I ../botan/build/include -Wl,-rpath=../botan/ $^ -o bin/$@ -L ../botan/ -l:libbotan-3.a
6+
7+
clean:
8+
rm -rf bin/
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
PINFLAGS="--phase1 --phase2 --export --parallel"
6+
export RESULTDIR=results
7+
8+
9+
pushd ${BASH_SOURCE%/*}
10+
11+
if [[ $1 == "clean" || $2 == "clean" ]]; then
12+
rm -rf results
13+
fi
14+
15+
./framework.sh ${PINFLAGS} 4x4_AES
16+
17+
if [[ $1 == "test" || $2 == "test" ]]; then
18+
popd
19+
exit 0
20+
fi
21+
22+
./framework.sh ${PINFLAGS} 4x4_AES
23+
./framework.sh ${PINFLAGS} 6x5
24+
./framework.sh ${PINFLAGS} 6x5_AES
25+
./framework.sh ${PINFLAGS} 8x7
26+
./framework.sh ${PINFLAGS} 8x7_AES
27+
28+
popd
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
#include <cassert>
2+
#include <fstream>
3+
#include <iostream>
4+
#include <string>
5+
#include <vector>
6+
using namespace std;
7+
8+
#include <botan/auto_rng.h>
9+
#include <botan/block_cipher.h>
10+
#include <botan/dilithium.h>
11+
#include <botan/hex.h>
12+
#include <botan/oids.h>
13+
#include <botan/pubkey.h>
14+
#include <botan/rng.h>
15+
16+
vector<string> modes = {
17+
"4x4", "4x4_AES", "6x5", "6x5_AES", "8x7", "8x7_AES",
18+
};
19+
20+
vector<string> operations = {"keygen", "sign"};
21+
22+
Botan::DilithiumMode name_to_mode(const std::string &algo_name) {
23+
if (algo_name == "4x4") {
24+
return Botan::DilithiumMode::Dilithium4x4;
25+
}
26+
if (algo_name == "4x4_AES") {
27+
return Botan::DilithiumMode::Dilithium4x4_AES;
28+
}
29+
if (algo_name == "6x5") {
30+
return Botan::DilithiumMode::Dilithium6x5;
31+
}
32+
if (algo_name == "6x5_AES") {
33+
return Botan::DilithiumMode::Dilithium6x5_AES;
34+
}
35+
if (algo_name == "8x7") {
36+
return Botan::DilithiumMode::Dilithium8x7;
37+
}
38+
if (algo_name == "8x7_AES") {
39+
return Botan::DilithiumMode::Dilithium8x7_AES;
40+
}
41+
42+
assert(false);
43+
}
44+
45+
int main(int argc, char *argv[]) {
46+
Botan::AutoSeeded_RNG rng;
47+
48+
if (argc != 4) {
49+
cout << "Usage:\n\n"
50+
<< " dilithium <mode> <operation> <keyfile>\n\n"
51+
<< " <mode> ..... asymmetric cipher mode\n"
52+
<< " <operation> ..... operation to execute, e.g. keygen or "
53+
"kem\n"
54+
<< " <keyfile> ... kyber key file, read as text\n"
55+
<< endl;
56+
cout << "List of available modes:" << endl;
57+
for (vector<string>::size_type i = 0; i != modes.size(); i++) {
58+
cout << " " << modes[i] << endl;
59+
}
60+
cout << endl;
61+
cout << "List of available operations:" << endl;
62+
for (vector<string>::size_type i = 0; i != operations.size(); i++) {
63+
cout << " " << operations[i] << endl;
64+
}
65+
cout << endl;
66+
return (1);
67+
}
68+
69+
string str_mode(argv[1]);
70+
string str_operation(argv[2]);
71+
string str_keyfile(argv[3]);
72+
73+
Botan::DilithiumMode mode = name_to_mode(str_mode);
74+
// auto encoding = Botan::DilithiumKeyEncoding::DER;
75+
auto encoding = Botan::DilithiumKeyEncoding::Raw;
76+
77+
std::string keyfile_buffer_sk(str_keyfile);
78+
79+
if (str_operation == "keygen") {
80+
const Botan::Dilithium_PrivateKey priv_key(rng, mode);
81+
const auto priv_key_bits = priv_key.private_key_bits();
82+
83+
ofstream keyfile_sk;
84+
keyfile_sk.open(keyfile_buffer_sk);
85+
keyfile_sk << Botan::hex_encode(priv_key_bits);
86+
keyfile_sk.close();
87+
} else if (str_operation == "sign") {
88+
std::vector<uint8_t> message, signature;
89+
message.push_back(0xde);
90+
message.push_back(0xad);
91+
message.push_back(0xbe);
92+
message.push_back(0xef);
93+
94+
// Load key pair
95+
string line_sk;
96+
ifstream keyfile_sk;
97+
keyfile_sk.open(keyfile_buffer_sk);
98+
getline(keyfile_sk, line_sk);
99+
keyfile_sk.close();
100+
Botan::secure_vector<uint8_t> priv_key_bits =
101+
Botan::hex_decode_locked(line_sk);
102+
103+
Botan::Dilithium_PrivateKey priv_key(priv_key_bits, mode, encoding);
104+
105+
Botan::PK_Signer sig(priv_key, rng, "Deterministic");
106+
signature = sig.sign_message(message, rng);
107+
108+
Botan::PK_Verifier ver(priv_key, "");
109+
ver.update(message);
110+
assert(ver.check_signature(signature));
111+
} else {
112+
cout << str_operation << " is no valid operation!" << endl;
113+
assert(false);
114+
}
115+
116+
return (0);
117+
}
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
#!/bin/bash
2+
3+
#########################################################################
4+
# DO NOT CHANGE: Preparing DATA
5+
#------------------------------------------------------------------------
6+
source "${DATA_COMMON}/DATA_init.sh" || { echo "source data.sh first!" && exit 1; }
7+
#########################################################################
8+
9+
#------------------------------------------------------------------------
10+
# Specify your framework settings used by DATA
11+
#------------------------------------------------------------------------
12+
13+
# The name of the framework. Do not use spaces or special characters.
14+
export FRAMEWORK=botan
15+
16+
# The file containing all supported algorithms
17+
export TARGETFILE=targets.txt
18+
19+
# The number of measurements for difference detection (phase1)
20+
export PHASE1_TRACES=3
21+
22+
# The number of constant keys for generic tests (phase2)
23+
# Make sure that PHASE2_FIXEDKEYS <= PHASE1_TRACES
24+
export PHASE2_FIXEDKEYS=3
25+
26+
# The number of measurements per constant key for generic tests (phase2)
27+
export PHASE2_TRACES=100
28+
29+
# The number of measurements for specific tests (phase3)
30+
export PHASE3_TRACES=200
31+
32+
# (Optional) Additional flags for the pintool. Supported flags are:
33+
# -main <main> Start recording at function <main>. Note that the <main>
34+
# symbol must exist, otherwise this will yield empty traces!
35+
# -heap Trace heap allocations and replace heap addresses with
36+
# relative offset
37+
export PINTOOL_ARGS="-heap"
38+
39+
#------------------------------------------------------------------------
40+
# Implement your framework-specific callbacks
41+
#------------------------------------------------------------------------
42+
#
43+
# Globally available environment variables:
44+
# $FRAMEWORK The framework name
45+
# $BASEDIR The absolute directory path of this script
46+
# $DATA_COMMON The absolute directory for common DATA scripts
47+
# $DATA_LEAKAGE_MODELS The absolute directory for DATA leakage models
48+
#
49+
# Available for cb_genkey, cb_pre_run, cb_run_command, cb_post_run
50+
# $ALGO The currently tested algo
51+
#
52+
# Available for cb_pre_run, cb_run_command, cb_post_run
53+
# $ENVFILE
54+
55+
export BINARY=${PWD}/bin/dilithium
56+
57+
# The leakage model of phase 3.
58+
# See ${DATA_LEAKAGE_MODELS} for all options.
59+
# export SPECIFIC_LEAKAGE_CALLBACK=${DATA_LEAKAGE_MODELS}/rsa_privkey_hw.py
60+
61+
# DATA callback for setting up the framework to analyze. This callback
62+
# is invoked once inside the current directory before analysis starts.
63+
# Implement framework-specific tasks here like framework compilation.
64+
function cb_prepare_framework {
65+
:
66+
}
67+
68+
# DATA callback for generating keys. This callback is invoked every
69+
# time a new key is needed. Implement key generation according to
70+
# your algorithm and store the generated key inside a file named $2.
71+
#
72+
# $1 ... key file name
73+
function cb_genkey {
74+
${BINARY} ${ALGO} keygen $1
75+
RES=$((RES + $?))
76+
}
77+
78+
# DATA callback for custom commands that are executed immediately before
79+
# the algorithm is profiled. It is executed in a temporary directory
80+
# which contains the keyfile $1 and ${ENVFILE}.
81+
#
82+
# If 'cb_run_command' needs any other files, copy them to ${PWD}.
83+
#
84+
# $1 ... key file name
85+
function cb_pre_run {
86+
log_verbose "running with key $1"
87+
}
88+
89+
# DATA callback for the main invocation of the tested algorithm.
90+
# It shall return the bash command to execute as string. It is
91+
# executed inside a temporary directory with a clean environment.
92+
# If you need special files or environment variables set, specify
93+
# them in cb_pre_run.
94+
#
95+
# $1 ... key file name
96+
function cb_run_command {
97+
echo "${BINARY} ${ALGO} sign $1"
98+
}
99+
100+
# DATA callback for custom commands that are executed immediately after
101+
# the algorithm is profiled. It is executed in a temporary directory.
102+
# You can cleanup any custom files generated by your algorithm.
103+
#
104+
# $1 ... key file name
105+
function cb_post_run {
106+
:
107+
}
108+
109+
# DATA callback for preparing an individual algorithm. It shall:
110+
# 1. Parse the next algorithm from the commandline string of all algorithms
111+
# and set up anything necessary for analyzing this algorithm.
112+
# If the algorithm needs additional parameters (like key sizes),
113+
# increase $SHIFT accordingly.
114+
# 2. Configure $WORKDIR, which will create a subdirectory holding all
115+
# intermediate files generated by the algorithm and the results.
116+
# Do not use an absolute path!
117+
#
118+
# $* ... algorithm string from the commandline
119+
function cb_prepare_algo {
120+
ALGO=$1
121+
# key bits
122+
SHIFT=$((SHIFT))
123+
124+
WORKDIR="dilithium-$ALGO"
125+
}
126+
127+
#########################################################################
128+
# DO NOT CHANGE: Running DATA's commandline parser
129+
#------------------------------------------------------------------------
130+
DATA_parse "$@"
131+
#------------------------------------------------------------------------
132+
# DO NOT ADD CODE AFTER THIS LINE
133+
#########################################################################

0 commit comments

Comments
 (0)