Fork of François Berder Crypto, fixed AES CBC and small rework
Dependents: AES_example shaun_larada Smartage
Fork of Crypto by
cipher/BlockCipher.cpp@15:6093fc19aad6, 2015-01-28 (annotated)
- Committer:
- Geremia
- Date:
- Wed Jan 28 17:15:32 2015 +0000
- Revision:
- 15:6093fc19aad6
- Parent:
- 14:f04410cef037
- Child:
- 16:4399e2e6260b
AES: bugfixed CBC, added PCBC, added public setIV(iv), moved keyExpansion() to public
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
feb11 | 8:a090264e9b2d | 1 | #include "BlockCipher.h" |
feb11 | 8:a090264e9b2d | 2 | #include <string.h> |
feb11 | 8:a090264e9b2d | 3 | |
feb11 | 8:a090264e9b2d | 4 | BlockCipher::BlockCipher(uint32_t bs, BLOCK_CIPHER_MODE m, uint8_t *iv): |
feb11 | 8:a090264e9b2d | 5 | Cipher(), |
feb11 | 8:a090264e9b2d | 6 | blockSize(bs), |
feb11 | 8:a090264e9b2d | 7 | mode(m), |
Geremia | 15:6093fc19aad6 | 8 | IV(0), |
Geremia | 15:6093fc19aad6 | 9 | tmpIV(0), |
Geremia | 15:6093fc19aad6 | 10 | tmpdata(0) |
feb11 | 8:a090264e9b2d | 11 | { |
Geremia | 15:6093fc19aad6 | 12 | if(mode != ECB_MODE) |
feb11 | 8:a090264e9b2d | 13 | { |
feb11 | 8:a090264e9b2d | 14 | IV = new uint8_t[blockSize]; |
Geremia | 15:6093fc19aad6 | 15 | tmpIV = new uint8_t[blockSize]; |
Geremia | 15:6093fc19aad6 | 16 | tmpdatain = new uint8_t[blockSize]; |
Geremia | 15:6093fc19aad6 | 17 | tmpdata = new uint8_t[blockSize]; |
feb11 | 8:a090264e9b2d | 18 | memcpy(IV, iv, blockSize); |
feb11 | 8:a090264e9b2d | 19 | } |
feb11 | 8:a090264e9b2d | 20 | } |
feb11 | 8:a090264e9b2d | 21 | |
feb11 | 8:a090264e9b2d | 22 | BlockCipher::~BlockCipher() |
feb11 | 8:a090264e9b2d | 23 | { |
Geremia | 15:6093fc19aad6 | 24 | if(IV != 0) delete[] IV; |
Geremia | 15:6093fc19aad6 | 25 | if(tmpIV != 0) delete[] tmpIV; |
Geremia | 15:6093fc19aad6 | 26 | if(tmpdatain != 0) delete[] tmpdatain; |
Geremia | 15:6093fc19aad6 | 27 | if(tmpdata != 0) delete[] tmpdata; |
feb11 | 8:a090264e9b2d | 28 | } |
feb11 | 8:a090264e9b2d | 29 | |
feb11 | 8:a090264e9b2d | 30 | CIPHER_TYPE BlockCipher::getType() const |
feb11 | 8:a090264e9b2d | 31 | { |
feb11 | 8:a090264e9b2d | 32 | return BLOCK_CIPHER; |
feb11 | 8:a090264e9b2d | 33 | } |
feb11 | 8:a090264e9b2d | 34 | |
feb11 | 8:a090264e9b2d | 35 | uint32_t BlockCipher::getBlockSize() const |
feb11 | 8:a090264e9b2d | 36 | { |
feb11 | 8:a090264e9b2d | 37 | return blockSize; |
feb11 | 8:a090264e9b2d | 38 | } |
feb11 | 8:a090264e9b2d | 39 | |
feb11 | 8:a090264e9b2d | 40 | void BlockCipher::encrypt(uint8_t *out, uint8_t *in, uint32_t length) |
feb11 | 8:a090264e9b2d | 41 | { |
Geremia | 15:6093fc19aad6 | 42 | |
Geremia | 15:6093fc19aad6 | 43 | switch (mode) |
feb11 | 8:a090264e9b2d | 44 | { |
Geremia | 15:6093fc19aad6 | 45 | case ECB_MODE: |
Geremia | 15:6093fc19aad6 | 46 | for(uint32_t i = 0; i < length; i += blockSize) |
Geremia | 15:6093fc19aad6 | 47 | { |
Geremia | 15:6093fc19aad6 | 48 | encryptBlock(out+i, in+i); |
Geremia | 15:6093fc19aad6 | 49 | } |
Geremia | 15:6093fc19aad6 | 50 | break; |
Geremia | 15:6093fc19aad6 | 51 | case PCBC_MODE: |
Geremia | 15:6093fc19aad6 | 52 | case CBC_MODE: |
Geremia | 15:6093fc19aad6 | 53 | memcpy(tmpIV, IV, blockSize); |
Geremia | 15:6093fc19aad6 | 54 | for(uint32_t i = 0; i < length; i += blockSize) |
Geremia | 15:6093fc19aad6 | 55 | { |
Geremia | 15:6093fc19aad6 | 56 | if(mode==PCBC_MODE) memcpy(tmpdata, in+i, blockSize); |
Geremia | 15:6093fc19aad6 | 57 | memcpy(tmpdatain, in+i, blockSize); |
Geremia | 15:6093fc19aad6 | 58 | for(int j = 0; j < blockSize; ++j) tmpdatain[j] ^= tmpIV[j]; |
Geremia | 15:6093fc19aad6 | 59 | encryptBlock(out+i, tmpdatain); |
Geremia | 15:6093fc19aad6 | 60 | memcpy(tmpIV, out+i, blockSize); |
Geremia | 15:6093fc19aad6 | 61 | if(mode==PCBC_MODE) |
Geremia | 15:6093fc19aad6 | 62 | { |
Geremia | 15:6093fc19aad6 | 63 | for(int j = 0; j < blockSize; ++j) tmpIV[j] ^= tmpdata[j]; |
Geremia | 15:6093fc19aad6 | 64 | } |
Geremia | 15:6093fc19aad6 | 65 | } |
Geremia | 15:6093fc19aad6 | 66 | break; |
feb11 | 8:a090264e9b2d | 67 | } |
feb11 | 8:a090264e9b2d | 68 | } |
feb11 | 8:a090264e9b2d | 69 | |
feb11 | 8:a090264e9b2d | 70 | void BlockCipher::decrypt(uint8_t *out, uint8_t *in, uint32_t length) |
feb11 | 8:a090264e9b2d | 71 | { |
Geremia | 15:6093fc19aad6 | 72 | switch (mode) |
feb11 | 8:a090264e9b2d | 73 | { |
Geremia | 15:6093fc19aad6 | 74 | case ECB_MODE: |
Geremia | 15:6093fc19aad6 | 75 | for(uint32_t i = 0; i < length; i += blockSize) |
Geremia | 15:6093fc19aad6 | 76 | { |
Geremia | 15:6093fc19aad6 | 77 | decryptBlock(out+i, in+i); |
Geremia | 15:6093fc19aad6 | 78 | } |
Geremia | 15:6093fc19aad6 | 79 | break; |
Geremia | 15:6093fc19aad6 | 80 | case PCBC_MODE: |
Geremia | 15:6093fc19aad6 | 81 | case CBC_MODE: |
Geremia | 15:6093fc19aad6 | 82 | memcpy(tmpIV, IV, blockSize); |
Geremia | 15:6093fc19aad6 | 83 | for(uint32_t i = 0; i < length; i += blockSize) |
Geremia | 15:6093fc19aad6 | 84 | { |
Geremia | 15:6093fc19aad6 | 85 | // if(mode==PCBC_MODE) memcpy(tmpdata, in+i, blockSize); |
Geremia | 15:6093fc19aad6 | 86 | memcpy(tmpdatain, in+i, blockSize); |
Geremia | 15:6093fc19aad6 | 87 | decryptBlock(out+i, tmpdatain); |
Geremia | 15:6093fc19aad6 | 88 | for(int j = 0; j < blockSize; ++j) out[i+j] ^= tmpIV[j]; |
Geremia | 15:6093fc19aad6 | 89 | memcpy(tmpIV, tmpdatain, blockSize); |
Geremia | 15:6093fc19aad6 | 90 | if(mode==PCBC_MODE) |
Geremia | 15:6093fc19aad6 | 91 | { |
Geremia | 15:6093fc19aad6 | 92 | for(int j = 0; j < blockSize; ++j) tmpIV[j] ^= out[i+j]; |
Geremia | 15:6093fc19aad6 | 93 | } |
Geremia | 15:6093fc19aad6 | 94 | } |
Geremia | 15:6093fc19aad6 | 95 | break; |
feb11 | 8:a090264e9b2d | 96 | } |
feb11 | 8:a090264e9b2d | 97 | } |
Geremia | 15:6093fc19aad6 | 98 | |
Geremia | 15:6093fc19aad6 | 99 | void BlockCipher::setIV(uint8_t *iv) |
Geremia | 15:6093fc19aad6 | 100 | { |
Geremia | 15:6093fc19aad6 | 101 | if(IV!=0) memcpy(IV, iv, blockSize); |
Geremia | 15:6093fc19aad6 | 102 | } |