Overview
I am trying to create a way for the server and the client to be able to generate a unique IV for each request, which is different for each client and yet deterministic. What I mean by deterministic is that the server can calculate IV for any request in the future, knowing only the initial sequence.
The reason I need this feature is because I use AES encryption to implement a one-time password (OTP) scheme. When a client registers with the server, he is provided with seed. The first OTP is generated by encrypting this seed. For each subsequent request, the client encrypts the last OTP using the shared AES key between the server and the client. Therefore, even if an attacker sniffed the last OTP without a shared key, they would not be able to get the next OTP. OTP is encrypted by AES in CBC mode. The problem occurs if the server and client are not synchronized. The way I planned to deal with this is to create several OTPs in the future on the server side and see if any of them match the client. However, without a way to deterministically calculate IV for each encryption iteration, this is not possible.
Before I enter my proposed solution, let me express my understanding of AES, IV, CBC and ECB. Thus, if I have any misunderstandings in my basics, they can be pointed out and corrected.
Theory
ECB
I know that ECB will produce the same output for identical plaintext blocks encrypted with the same keys. Therefore, it should not be used for several data blocks, since plaintext information can be recognized by statistical analysis of the data. Based on this, it would seem that if you could guarantee that your data was always less than 16 bytes (128 bits), this would eliminate the problem of statistical attack. In addition, if you can also ensure that you never encrypt the same plaintext with the same key, you will never get the same result. Therefore, it seems to me that using ECB is safe if your system always meets these very strict criteria.
CBC and IV
I know that CBC is designed to fix both of these issues. By blocking blocks, it eliminates the statistical attack of several ECB blocks. By never using the same IV for the same AES key, you fix the problem that the same file will be encrypted with the same output using the same key.
Key uniqueness
If each client receives a generated AES key, then when there is a small chance that several users having the same key have very little chance. Therefore, we can safely assume that none of the two clients will use the same AES key.
Proposed solution
My proposed solution is to provide each client with a unique AES key. When a key is generated, the counter will be initialized with a random number. Each time something needs to be encrypted, the counter will increase by one. This number will be supplemented by a block and then encrypted using AES in ECB mode. The result of this will be my IV for encrypting data using CBC.
If the server does not synchronize with the client counter because it has the same key and the ECB does not require an IV, it can continue to generate an IV until it finds one that can decrypt the data.
My thoughts are that IV is safe from statistical attack because it is equal to the size of the AES block. In addition, each user will be different, each time, because each user will have a unique key, and the counter will always increase. Obviously, the AES keys must be securely transmitted (right now the client is encrypting the generated key with the RSA server public key).
My questions
Is my fundamental understanding of the technologies described in the proposed solution correct? Is there something clearly wrong with my proposed solution? Is there a security flaw in using the same key to generate IV in the proposed form, as well as for encryption using CBC?
I understand that the last question may be difficult / impossible to answer, because cryptography is really complex, but any understanding will be appreciated.
Thanks in advance.