[d7dadcb4] | 1 | /*
|
---|
| 2 | * Copyright (c) 2015 Jan Kolarik
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @file rc4.c
|
---|
[8a64320e] | 30 | *
|
---|
[d7dadcb4] | 31 | * Implementation of ARC4 symmetric cipher cryptographic algorithm.
|
---|
[8a64320e] | 32 | *
|
---|
[d7dadcb4] | 33 | */
|
---|
| 34 |
|
---|
| 35 | #include <errno.h>
|
---|
| 36 | #include <mem.h>
|
---|
| 37 | #include "crypto.h"
|
---|
| 38 |
|
---|
| 39 | /* Sbox table size. */
|
---|
[8a64320e] | 40 | #define SBOX_SIZE 256
|
---|
[d7dadcb4] | 41 |
|
---|
[8a64320e] | 42 | /** Swap two values in sbox.
|
---|
| 43 | *
|
---|
| 44 | * @param i First index of value in sbox to be swapped.
|
---|
| 45 | * @param j Second index of value in sbox to be swapped.
|
---|
[d7dadcb4] | 46 | * @param sbox Sbox to be modified.
|
---|
[8a64320e] | 47 | *
|
---|
[d7dadcb4] | 48 | */
|
---|
| 49 | static void swap(size_t i, size_t j, uint8_t *sbox)
|
---|
| 50 | {
|
---|
| 51 | uint8_t temp = sbox[i];
|
---|
| 52 | sbox[i] = sbox[j];
|
---|
| 53 | sbox[j] = temp;
|
---|
| 54 | }
|
---|
| 55 |
|
---|
[8a64320e] | 56 | /** Sbox initialization procedure.
|
---|
| 57 | *
|
---|
| 58 | * @param key Input key.
|
---|
[d7dadcb4] | 59 | * @param key_size Size of key sequence.
|
---|
[8a64320e] | 60 | * @param sbox Place for result sbox.
|
---|
| 61 | *
|
---|
[d7dadcb4] | 62 | */
|
---|
| 63 | static void create_sbox(uint8_t *key, size_t key_size, uint8_t *sbox)
|
---|
| 64 | {
|
---|
[8a64320e] | 65 | for (size_t i = 0; i < SBOX_SIZE; i++)
|
---|
[d7dadcb4] | 66 | sbox[i] = i;
|
---|
[a35b458] | 67 |
|
---|
[d7dadcb4] | 68 | uint8_t j = 0;
|
---|
[8a64320e] | 69 | for (size_t i = 0; i < SBOX_SIZE; i++) {
|
---|
[d7dadcb4] | 70 | j = j + sbox[i] + key[i % key_size];
|
---|
| 71 | swap(i, j, sbox);
|
---|
| 72 | }
|
---|
| 73 | }
|
---|
| 74 |
|
---|
[8a64320e] | 75 | /** ARC4 encryption/decryption algorithm.
|
---|
| 76 | *
|
---|
| 77 | * @param key Input key.
|
---|
| 78 | * @param key_size Size of key sequence.
|
---|
| 79 | * @param input Input data sequence to be processed.
|
---|
[d7dadcb4] | 80 | * @param input_size Size of input data sequence.
|
---|
[8a64320e] | 81 | * @param skip Number of bytes to be skipped from
|
---|
| 82 | * the beginning of key stream.
|
---|
| 83 | * @param output Result data sequence.
|
---|
| 84 | *
|
---|
| 85 | * @return EINVAL when input or key not specified,
|
---|
| 86 | * ENOMEM when pointer for output is not allocated,
|
---|
| 87 | * otherwise EOK.
|
---|
| 88 | *
|
---|
[d7dadcb4] | 89 | */
|
---|
[b7fd2a0] | 90 | errno_t rc4(uint8_t *key, size_t key_size, uint8_t *input, size_t input_size,
|
---|
[8a64320e] | 91 | size_t skip, uint8_t *output)
|
---|
[d7dadcb4] | 92 | {
|
---|
[8a64320e] | 93 | if ((!key) || (!input))
|
---|
[d7dadcb4] | 94 | return EINVAL;
|
---|
[a35b458] | 95 |
|
---|
[8a64320e] | 96 | if (!output)
|
---|
[d7dadcb4] | 97 | return ENOMEM;
|
---|
[a35b458] | 98 |
|
---|
[d7dadcb4] | 99 | /* Initialize sbox. */
|
---|
| 100 | uint8_t sbox[SBOX_SIZE];
|
---|
| 101 | create_sbox(key, key_size, sbox);
|
---|
[a35b458] | 102 |
|
---|
[a931b7b] | 103 | /* Skip first x bytes. */
|
---|
[8a64320e] | 104 | uint8_t i = 0;
|
---|
| 105 | uint8_t j = 0;
|
---|
| 106 | for (size_t k = 0; k < skip; k++) {
|
---|
[cc575ef9] | 107 | i = i + 1;
|
---|
[a931b7b] | 108 | j = j + sbox[i];
|
---|
| 109 | swap(i, j, sbox);
|
---|
| 110 | }
|
---|
[a35b458] | 111 |
|
---|
[d7dadcb4] | 112 | /* Processing loop. */
|
---|
[a931b7b] | 113 | uint8_t val;
|
---|
[8a64320e] | 114 | for (size_t k = 0; k < input_size; k++) {
|
---|
[cc575ef9] | 115 | i = i + 1;
|
---|
[d7dadcb4] | 116 | j = j + sbox[i];
|
---|
| 117 | swap(i, j, sbox);
|
---|
| 118 | val = sbox[sbox[i] + sbox[j]];
|
---|
| 119 | output[k] = val ^ input[k];
|
---|
| 120 | }
|
---|
[a35b458] | 121 |
|
---|
[d7dadcb4] | 122 | return EOK;
|
---|
[8a64320e] | 123 | }
|
---|