|
|
|
@ -101,7 +101,8 @@ STATIC mp_obj_t mod_trezorcrypto_AES_update(mp_obj_t self, mp_obj_t data) {
|
|
|
|
|
if (buf.len == 0) {
|
|
|
|
|
return mp_const_empty_bytes;
|
|
|
|
|
}
|
|
|
|
|
uint8_t out[buf.len];
|
|
|
|
|
vstr_t vstr;
|
|
|
|
|
vstr_init_len(&vstr, buf.len);
|
|
|
|
|
mp_obj_AES_t *o = MP_OBJ_TO_PTR(self);
|
|
|
|
|
switch (o->mode & AESModeMask) {
|
|
|
|
|
case ECB:
|
|
|
|
@ -109,9 +110,9 @@ STATIC mp_obj_t mod_trezorcrypto_AES_update(mp_obj_t self, mp_obj_t data) {
|
|
|
|
|
mp_raise_ValueError("Invalid data length");
|
|
|
|
|
}
|
|
|
|
|
if ((o->mode & AESDirMask) == Encrypt) {
|
|
|
|
|
aes_ecb_encrypt(buf.buf, out, buf.len, &(o->ctx.encrypt_ctx));
|
|
|
|
|
aes_ecb_encrypt(buf.buf, (uint8_t *)vstr.buf, buf.len, &(o->ctx.encrypt_ctx));
|
|
|
|
|
} else {
|
|
|
|
|
aes_ecb_decrypt(buf.buf, out, buf.len, &(o->ctx.decrypt_ctx));
|
|
|
|
|
aes_ecb_decrypt(buf.buf, (uint8_t *)vstr.buf, buf.len, &(o->ctx.decrypt_ctx));
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case CBC:
|
|
|
|
@ -119,26 +120,26 @@ STATIC mp_obj_t mod_trezorcrypto_AES_update(mp_obj_t self, mp_obj_t data) {
|
|
|
|
|
mp_raise_ValueError("Invalid data length");
|
|
|
|
|
}
|
|
|
|
|
if ((o->mode & AESDirMask) == Encrypt) {
|
|
|
|
|
aes_cbc_encrypt(buf.buf, out, buf.len, o->iv, &(o->ctx.encrypt_ctx));
|
|
|
|
|
aes_cbc_encrypt(buf.buf, (uint8_t *)vstr.buf, buf.len, o->iv, &(o->ctx.encrypt_ctx));
|
|
|
|
|
} else {
|
|
|
|
|
aes_cbc_decrypt(buf.buf, out, buf.len, o->iv, &(o->ctx.decrypt_ctx));
|
|
|
|
|
aes_cbc_decrypt(buf.buf, (uint8_t *)vstr.buf, buf.len, o->iv, &(o->ctx.decrypt_ctx));
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case CFB:
|
|
|
|
|
if ((o->mode & AESDirMask) == Encrypt) {
|
|
|
|
|
aes_cfb_encrypt(buf.buf, out, buf.len, o->iv, &(o->ctx.encrypt_ctx));
|
|
|
|
|
aes_cfb_encrypt(buf.buf, (uint8_t *)vstr.buf, buf.len, o->iv, &(o->ctx.encrypt_ctx));
|
|
|
|
|
} else {
|
|
|
|
|
aes_cfb_decrypt(buf.buf, out, buf.len, o->iv, &(o->ctx.encrypt_ctx));
|
|
|
|
|
aes_cfb_decrypt(buf.buf, (uint8_t *)vstr.buf, buf.len, o->iv, &(o->ctx.encrypt_ctx));
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case OFB: // (encrypt == decrypt)
|
|
|
|
|
aes_ofb_crypt(buf.buf, out, buf.len, o->iv, &(o->ctx.encrypt_ctx));
|
|
|
|
|
aes_ofb_crypt(buf.buf, (uint8_t *)vstr.buf, buf.len, o->iv, &(o->ctx.encrypt_ctx));
|
|
|
|
|
break;
|
|
|
|
|
case CTR: // (encrypt == decrypt)
|
|
|
|
|
aes_ctr_crypt(buf.buf, out, buf.len, o->ctr, aes_ctr_cbuf_inc, &(o->ctx.encrypt_ctx));
|
|
|
|
|
aes_ctr_crypt(buf.buf, (uint8_t *)vstr.buf, buf.len, o->ctr, aes_ctr_cbuf_inc, &(o->ctx.encrypt_ctx));
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
return mp_obj_new_bytes(out, sizeof(out));
|
|
|
|
|
return mp_obj_new_bytes((uint8_t *)vstr.buf, vstr.len);
|
|
|
|
|
}
|
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorcrypto_AES_update_obj, mod_trezorcrypto_AES_update);
|
|
|
|
|
|
|
|
|
|