aboutsummaryrefslogtreecommitdiffstats
path: root/docs/development/custom-vectors/seed/generate_seed.py
blob: d59597fd27064d33b572fcddc8b06c3e9530c716 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import binascii

from cryptography.hazmat.backends.openssl.backend import backend
from cryptography.hazmat.primitives.ciphers import algorithms, base, modes


def encrypt(mode, key, iv, plaintext):
    cipher = base.Cipher(
        algorithms.SEED(binascii.unhexlify(key)),
        mode(binascii.unhexlify(iv)),
        backend
    )
    encryptor = cipher.encryptor()
    ct = encryptor.update(binascii.unhexlify(plaintext))
    ct += encryptor.finalize()
    return binascii.hexlify(ct)


def build_vectors(mode, filename):
    with open(filename, "r") as f:
        vector_file = f.read().splitlines()

    count = 0
    output = []
    key = None
    iv = None
    plaintext = None
    for line in vector_file:
        line = line.strip()
        if line.startswith("KEY"):
            if count != 0:
                output.append("CIPHERTEXT = {0}".format(
                    encrypt(mode, key, iv, plaintext))
                )
            output.append("\nCOUNT = {0}".format(count))
            count += 1
            name, key = line.split(" = ")
            output.append("KEY = {0}".format(key))
        elif line.startswith("IV"):
            name, iv = line.split(" = ")
            output.append("IV = {0}".format(iv))
        elif line.startswith("PLAINTEXT"):
            name, plaintext = line.split(" = ")
            output.append("PLAINTEXT = {0}".format(plaintext))

    output.append("CIPHERTEXT = {0}".format(encrypt(mode, key, iv, plaintext)))
    return "\n".join(output)


def write_file(data, filename):
    with open(filename, "w") as f:
        f.write(data)

OFB_PATH = "vectors/cryptography_vectors/ciphers/AES/OFB/OFBMMT128.rsp"
write_file(build_vectors(modes.OFB, OFB_PATH), "seed-ofb.txt")
CFB_PATH = "vectors/cryptography_vectors/ciphers/AES/CFB/CFB128MMT128.rsp"
write_file(build_vectors(modes.CFB, CFB_PATH), "seed-cfb.txt")
">indentToken = str.substr(0, nonwhitespaceIndex); indent->Token = indentToken; return true; } ParseHelper::ParseHelper( ) { } void ParseHelper::process( const std::string& str ) { std::string top; commandBuffer.push_back(str); //std::string top = commandBuffer.back(); //commandBuffer.pop_back(); std::shared_ptr<ParseState> blockStatePtr; while (stateStack.size()) { top = commandBuffer.back(); commandBuffer.pop_back(); blockStatePtr = stateStack.back(); if (blockStatePtr->process(top)) return; } if ( ! commandBuffer.size() ) return; // standard state top = commandBuffer.back(); if ( !top.size() ) { reset( ); broadcast( std::string() ); return; } { // check for unexpected indent Indent ind; bool isIndented = PeekIndent( top, &ind ); if ( isIndented && ! isInContinuation( ) ) { reset( ); ParseMessage msg( 1, "IndentationError: unexpected indent"); broadcast( msg ); return; } } // enter indented block state if ( top[top.size()-1] == ':' ) { std::shared_ptr<ParseState> parseState( new BlockParseState( *this ) ); stateStack.push_back( parseState ); return; } if ( top[top.size()-1] == '\\' ) { std::shared_ptr<ParseState> parseState( new ContinuationParseState( *this ) ); stateStack.push_back( parseState ); return; } if (BracketParseState::HasOpenBrackets( top )) { // FIXME: Every parse state should have its own local buffer commandBuffer.pop_back( ); std::shared_ptr<ParseState> parseState( new BracketParseState( *this, top ) ); stateStack.push_back( parseState ); return; } // handle single-line statement flush( ); } bool ParseHelper::buffered( ) const { return commandBuffer.size( ) || stateStack.size( ); } void ParseHelper::flush( ) { std::stringstream ss; for (size_t i = 0; i < commandBuffer.size(); ++i ) { ss << commandBuffer[i] << "\n"; } commandBuffer.clear(); broadcast( ss.str() ); // TODO: feed string to interpreter } void ParseHelper::reset( ) { // inContinuation = false; stateStack.clear( ); commandBuffer.clear( ); } bool ParseHelper::isInContinuation( ) const { return (stateStack.size() && (std::dynamic_pointer_cast<ContinuationParseState>( stateStack.back()))); } void ParseHelper::subscribe( ParseListener* listener ) { listeners.push_back( listener ); } void ParseHelper::unsubscribeAll( ) { listeners.clear( ); } void ParseHelper::broadcast( const ParseMessage& msg ) { // broadcast signal for (size_t i = 0; i < listeners.size(); ++i) { if (listeners[i]) { listeners[i]->parseEvent(msg); } } }