aboutsummaryrefslogtreecommitdiffstats
path: root/crypto/ocb.py
blob: 08814c715b3564ea0b9c5092402653a6c77e71e9 (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
#-*- coding: utf-8 -*-
from aespython import key_expander, aes_cipher, ocb_mode
import json
import base64
def hex2byte(hexstring):
    return map(ord, hexstring.decode("hex"))
def byte2hex(byteslist):
    return str(bytearray(byteslist)).encode("hex")

if __name__=="__main__":
    KE = key_expander.KeyExpander(128)
    key = hex2byte('12538243c49f1c58e6f7b0687bbd65b2')
    expanded_key = KE.expand(key)
    aes_cipher_128 = aes_cipher.AESCipher(expanded_key)
    aes_ocb_128 = ocb_mode.OCBMode(aes_cipher_128, 16)
    iv = hex2byte('250c3041c00a605a4100e264abbc588b')
    aes_ocb_128.set_iv(iv)
    plaintext = "La chaire est triste, hélas ! et j'ai lu tous les livres.".encode("hex")
    header = "".encode("hex")
    (tag,ciphertext) = aes_ocb_128.encrypt_block(hex2byte(plaintext), hex2byte(header))
    sjcl_object = {"iv": base64.b64encode(bytearray(iv)).rstrip("="),
                 "mode": "ocb2",
                 "tag": 128,
                 "ct": base64.b64encode(bytearray(ciphertext) +
                                        bytearray(tag)).rstrip("=")
                 }
    print json.dumps(sjcl_object)