|
| 1 | +// |
| 2 | +// A compact AES-256-CTR implementation. |
| 3 | +// Complies with RFC3686, http://tools.ietf.org/html/rfc3686 |
| 4 | +// |
| 5 | +// Copyright (c) 2022 Ilia Levin (ilia@levin.sg) |
| 6 | +// |
| 7 | +// This source code is licensed under the terms of the MIT license. |
| 8 | +// For a copy, see <https://opensource.org/licenses/MIT>. |
| 9 | + |
| 10 | +#include "aes256ctr.h" |
| 11 | + |
| 12 | +// ----------------------------------------------------------------------------- |
| 13 | +static inline uint8_t |
| 14 | +_inc_b(uint8_t b) |
| 15 | +{ |
| 16 | + return ((b == 0xff) ? 0 : (1 + b)); |
| 17 | +} // _inc_b |
| 18 | + |
| 19 | + |
| 20 | +// ----------------------------------------------------------------------------- |
| 21 | +static inline uint8_t |
| 22 | +_inc_ctr(uint8_t val[static 4]) |
| 23 | +{ |
| 24 | + if (0 == (val[3] = _inc_b(val[3]))) |
| 25 | + if (0 == (val[2] = _inc_b(val[2]))) |
| 26 | + if (0 == (val[1] = _inc_b(val[1]))) { |
| 27 | + val[0] = _inc_b(val[0]); |
| 28 | + } |
| 29 | + return 0; |
| 30 | +} // _inc_ctr |
| 31 | + |
| 32 | + |
| 33 | +// ----------------------------------------------------------------------------- |
| 34 | +static uint8_t |
| 35 | +_clock_keystream(aes256ctr_ctx_t *ctx, aes256_blk_t *kb) |
| 36 | +{ |
| 37 | + *kb = *((aes256_blk_t *)&ctx->ctr.blk); |
| 38 | + aes256_encrypt_ecb(&ctx->ecb, kb); |
| 39 | + |
| 40 | + return _inc_ctr(&ctx->ctr.blk.ctr[0]); |
| 41 | +} // _clock_keystream |
| 42 | + |
| 43 | + |
| 44 | +// ----------------------------------------------------------------------------- |
| 45 | +uint8_t |
| 46 | +aes256ctr_setblk(aes256ctr_ctx_t *ctx, rfc3686_blk_t *blk) |
| 47 | +{ |
| 48 | + if ((NULL != ctx) && (NULL != blk)) { |
| 49 | + ctx->ctr.blk = *blk; |
| 50 | + return AES_SUCCESS; |
| 51 | + } |
| 52 | + |
| 53 | + return AES_ERROR; |
| 54 | +} // aes256ctr_setblk |
| 55 | + |
| 56 | + |
| 57 | +// ----------------------------------------------------------------------------- |
| 58 | +aes256ctr_ctx_t |
| 59 | +aes256ctr_init(aes256_key_t *key, rfc3686_blk_t *blk) |
| 60 | +{ |
| 61 | + aes256ctr_ctx_t ret = {0}; |
| 62 | + |
| 63 | + if (NULL != key) { |
| 64 | + ret.ctr.enckey = *key; |
| 65 | + } |
| 66 | + (void)aes256ctr_setblk(&ret, blk); |
| 67 | + |
| 68 | + return ret; |
| 69 | +} // aes255ctr_init |
| 70 | + |
| 71 | + |
| 72 | +// ----------------------------------------------------------------------------- |
| 73 | +uint8_t |
| 74 | +aes256ctr_encrypt(aes256ctr_ctx_t *ctx, uint8_t *buf, size_t size) |
| 75 | +{ |
| 76 | + if ((NULL != ctx) && (NULL != buf)) { |
| 77 | + aes256_blk_t ctrkey; |
| 78 | + size_t j = sizeof(ctrkey); |
| 79 | + |
| 80 | + for (size_t i = 0; i < size; i++) { |
| 81 | + if (j == sizeof(ctrkey)) { |
| 82 | + j = _clock_keystream(ctx, &ctrkey); |
| 83 | + } |
| 84 | + buf[i] ^= ctrkey.raw[j++]; |
| 85 | + ctrkey.raw[j - 1] = 0; |
| 86 | + } |
| 87 | + return AES_SUCCESS; |
| 88 | + } |
| 89 | + |
| 90 | + return AES_ERROR; |
| 91 | +} // aes256ctr_encrypt |
| 92 | + |
| 93 | + |
| 94 | +// ----------------------------------------------------------------------------- |
| 95 | +uint8_t |
| 96 | +aes256ctr_done(aes256ctr_ctx_t *ctx) |
| 97 | +{ |
| 98 | + return (NULL == ctx) ? AES_ERROR : aes256_done(&ctx->ecb); |
| 99 | +} // aes256ctr_done |
| 100 | + |
| 101 | + |
| 102 | +#if 0 |
| 103 | +#pragma mark - Self Test |
| 104 | +#endif |
| 105 | + |
| 106 | +#ifdef AES256CTR_SELF_TEST__ |
| 107 | +#include <stdio.h> |
| 108 | +#include <string.h> |
| 109 | + |
| 110 | +int |
| 111 | +main(void) |
| 112 | +{ |
| 113 | + static const uint8_t kav[] = { // RFC3686 Test Vector #9 |
| 114 | + 0xEB, 0x6C, 0x52, 0x82, 0x1D, 0x0B, 0xBB, 0xF7, 0xCE, 0x75, 0x94, 0x46, |
| 115 | + 0x2A, 0xCA, 0x4F, 0xAA, 0xB4, 0x07, 0xDF, 0x86, 0x65, 0x69, 0xFD, 0x07, |
| 116 | + 0xF4, 0x8C, 0xC0, 0xB5, 0x83, 0xD6, 0x07, 0x1F, 0x1E, 0xC0, 0xE6, 0xB8 |
| 117 | + }; |
| 118 | + uint8_t buf[] = { |
| 119 | + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, |
| 120 | + 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, |
| 121 | + 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23 |
| 122 | + }; |
| 123 | + aes256_key_t key = {.raw = { |
| 124 | + 0xFF, 0x7A, 0x61, 0x7C, 0xE6, 0x91, 0x48, 0xE4, 0xF1, 0x72, 0x6E, |
| 125 | + 0x2F, 0x43, 0x58, 0x1D, 0xE2, 0xAA, 0x62, 0xD9, 0xF8, 0x05, 0x53, |
| 126 | + 0x2E, 0xDF, 0xF1, 0xEE, 0xD6, 0x87, 0xFB, 0x54, 0x15, 0x3D |
| 127 | + } |
| 128 | + }; |
| 129 | + rfc3686_blk_t ctr = { |
| 130 | + .nonce = {0x00, 0x1C, 0xC5, 0xB7}, |
| 131 | + .iv = {0x51, 0xA5, 0x1D, 0x70, 0xA1, 0xC1, 0x11, 0x48}, |
| 132 | + .ctr = {0x00, 0x00, 0x00, 0x01} |
| 133 | + }; |
| 134 | + |
| 135 | + uint8_t ref[sizeof(buf)]; |
| 136 | + memcpy(ref, buf, sizeof(ref)); |
| 137 | + |
| 138 | + aes256ctr_ctx_t ctx = aes256ctr_init(&key, &ctr); |
| 139 | + aes256ctr_encrypt(&ctx, buf, sizeof(buf)); |
| 140 | + if ((sizeof(buf) != sizeof(kav)) || (0 != memcmp(buf, kav, sizeof(buf)))) { |
| 141 | + printf("Encrypt failed\n"); |
| 142 | + return -1; |
| 143 | + } |
| 144 | + |
| 145 | + aes256ctr_setblk(&ctx, &ctr); |
| 146 | + aes256ctr_decrypt(&ctx, buf, sizeof(buf)); |
| 147 | + if (0 != memcmp(buf, ref, sizeof(buf))) { |
| 148 | + printf("Decrypt failed\n"); |
| 149 | + return -1; |
| 150 | + } |
| 151 | + |
| 152 | + printf("Success\n"); |
| 153 | + return 0; |
| 154 | +} |
| 155 | +#endif // AES256CTR_SELF_TEST__ |
0 commit comments