Exemplo n.º 1
0
import sys
from CryptoHelper import CryptoHelper
from TestVectors import *

# Preamble
def test(n, x):
    passfail = "FAIL"
    if x:
        passfail = "PASS"
    print "[{0:2d}] [{1}]".format(n, passfail)

### TESTS #############################################################

# 1. AES key wrap
test( 1, t1_result == CryptoHelper.aes_key_wrap(t1_key, t1_data) )

# 2. AES key unwrap
test( 2, t2_result == CryptoHelper.aes_key_unwrap(t2_key, t2_data) )

# 3. SHA-256 digest
test( 3, t3_result == CryptoHelper.sha256(t3_data) )

# 4. HMAC SHA-245
test( 4, t4_result == CryptoHelper.hmac_sha256(t4_key, t4_data) )

# 5. AES-128-CCM encryption
test( 5, t5_result == CryptoHelper.encrypt_AES128CCM(t5_key, t5_nonce, t5_tlen, t5_data, t5_adata) );

# 6. AES-128-CCM decryption
test( 6, t6_result == CryptoHelper.decrypt_AES128CCM(t6_key, t6_nonce, t6_tlen, t6_data, t6_adata) );
Exemplo n.º 2
0
def main():
    usage = """-
        Usage: {0} <command> [args]
        The following commands are available:
            random <bytes>
            sign_pkcs1_sha256 <n> <e> <d> <content>
            verify_pkcs1_sha256 <n> <e> <content>
            aes_key_wrap <key> <p>
            aes_key_unwrap <key> <c>
            rsa_oaep_key_wrap <n> <e> <p>
            rsa_oaep_key_unwrap <n> <e> <d> <p>
            hmac_sha256 <key> <content>
            encrypt_gen_aead_AES128CBC_HMACSHA256 <key> <n> <iv> <content>
            decrypt_gen_aead_AES128CBC_HMACSHA256 <key> <n> <iv> <content>
            encrypt_AES128CCM <key> <n> <M> <content> <assoc>
            decrypt_AES128CCM <key> <n> <M> <content> <assoc>

        All arguments should be base64url encoded, except if they're integers.  
        Results will come back base64url encoded.
    """

    usage_msg = usage.format(sys.argv[0])
    if len(sys.argv) < 3:
        print usage_msg
        quit()

    cmd = sys.argv[1]
    a = sys.argv[2:]
    
    ret = b''
    if cmd == "random": # <bytes>
        b = int(a.pop(0))
        ret = CryptoHelper.random(b)
    elif cmd == "sign_pkcs1_sha256": # <n> <e> <d> <content>
        n = long_b64d(a.pop(0))
        e = long_b64d(a.pop(0))
        d = long_b64d(a.pop(0))
        content = b64d(a.pop(0))
        ret = CryptoHelper.sign_pkcs1_sha256(n, e, d, content)
    elif cmd == "verify_pkcs1_sha256": # <n> <e> <content>
        n = long_b64d(a.pop(0))
        e = long_b64d(a.pop(0))
        content = b64d(a.pop(0))
        sig = b64d(a.pop(0))
        ret = CryptoHelper.verify_pkcs1_sha256(n, e, content, sig)
    elif cmd == "aes_key_wrap": # <key> <p>
        key = b64d(a.pop(0))
        p = b64d(a.pop(0))
        ret = CryptoHelper.aes_key_wrap(key, p)
    elif cmd == "aes_key_unwrap": # <key> <c>
        key = b64d(a.pop(0))
        c = b64d(a.pop(0))
        ret = CryptoHelper.aes_key_unwrap(key, c)
    elif cmd == "rsa_oaep_key_wrap": # <n> <e> <p>
        n = long_b64d(a.pop(0))
        e = long_b64d(a.pop(0))
        p = b64d(a.pop(0))
        ret = CryptoHelper.rsa_oaep_key_wrap(n, e, p)
    elif cmd == "rsa_oaep_key_unwrap": # <n> <e> <d> <c>
        n = long_b64d(a.pop(0))
        e = long_b64d(a.pop(0))
        d = long_b64d(a.pop(0))
        c = b64d(a.pop(0))
        ret = CryptoHelper.rsa_oaep_key_unwrap(n, e, d, c)
    elif cmd == "hmac_sha256": # <key> <content>
        key = b64d(a.pop(0))
        content = b64d(a.pop(0))
        ret = CryptoHelper.hmac_sha256(key, content)
    elif cmd == "encrypt_gen_aead_AES128CBC_HMACSHA256": # <key> <n> <iv> <content>
        key = b64d(a.pop(0))
        n = b64d(a.pop(0))
        iv = b64d(a.pop(0))
        content = b64d(a.pop(0))
        ret = CryptoHelper.encrypt_gen_aead_AES128CBC_HMACSHA256(key, n, iv, content)
    elif cmd == "decrypt_gen_aead_AES128CBC_HMACSHA256": # <key> <n> <iv> <content>
        key = b64d(a.pop(0))
        n = b64d(a.pop(0))
        iv = b64d(a.pop(0))
        content = b64d(a.pop(0))
        ret = CryptoHelper.decrypt_gen_aead_AES128CBC_HMACSHA256(key, n, iv, content)
    elif cmd == "encrypt_AES128CCM": # <key> <n> <M> <content> <assoc>
        key = b64d(a.pop(0))
        n = b64d(a.pop(0))
        M = int(a.pop(0))
        content = b64d(a.pop(0))
        assoc = b64d(a.pop(0))
        ret = CryptoHelper.encrypt_AES128CCM(key, n, M, content, assoc)
    elif cmd == "decrypt_AES128CCM": # <key> <n> <M> <content> <assoc>
        key = b64d(a.pop(0))
        n = b64d(a.pop(0))
        M = int(a.pop(0))
        content = b64d(a.pop(0))
        assoc = b64d(a.pop(0))
        ret = CryptoHelper.decrypt_AES128CCM(key, n, M, content, assoc)
    else:
        print "-" + "Unknown command"

    if isinstance(ret,bool):
        print "+{0}".format(ret)
    else:
        print "+" + b64e(ret)
Exemplo n.º 3
0
import sys
from CryptoHelper import CryptoHelper
from TestVectors import *

# Preamble
def test(n, x):
    passfail = "FAIL"
    if x:
        passfail = "PASS"
    print "[{0:2d}] [{1}]".format(n, passfail)


### TESTS #############################################################

# 1. AES key wrap
test(1, t1_result == CryptoHelper.aes_key_wrap(t1_key, t1_data))

# 2. AES key unwrap
test(2, t2_result == CryptoHelper.aes_key_unwrap(t2_key, t2_data))

# 3. SHA-256 digest
test(3, t3_result == CryptoHelper.sha256(t3_data))

# 4. HMAC SHA-245
test(4, t4_result == CryptoHelper.hmac_sha256(t4_key, t4_data))

# 5. AES-128-CCM encryption
test(5, t5_result == CryptoHelper.encrypt_AES128CCM(t5_key, t5_nonce, t5_tlen, t5_data, t5_adata))

# 6. AES-128-CCM decryption
test(6, t6_result == CryptoHelper.decrypt_AES128CCM(t6_key, t6_nonce, t6_tlen, t6_data, t6_adata))