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
|
---|
30 | *
|
---|
31 | * Implementation of ARC4 symmetric cipher cryptographic algorithm.
|
---|
32 | *
|
---|
33 | */
|
---|
34 |
|
---|
35 | #include <errno.h>
|
---|
36 | #include <mem.h>
|
---|
37 | #include "crypto.h"
|
---|
38 |
|
---|
39 | /* Sbox table size. */
|
---|
40 | #define SBOX_SIZE 256
|
---|
41 |
|
---|
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.
|
---|
46 | * @param sbox Sbox to be modified.
|
---|
47 | *
|
---|
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 |
|
---|
56 | /** Sbox initialization procedure.
|
---|
57 | *
|
---|
58 | * @param key Input key.
|
---|
59 | * @param key_size Size of key sequence.
|
---|
60 | * @param sbox Place for result sbox.
|
---|
61 | *
|
---|
62 | */
|
---|
63 | static void create_sbox(uint8_t *key, size_t key_size, uint8_t *sbox)
|
---|
64 | {
|
---|
65 | for (size_t i = 0; i < SBOX_SIZE; i++)
|
---|
66 | sbox[i] = i;
|
---|
67 |
|
---|
68 | uint8_t j = 0;
|
---|
69 | for (size_t i = 0; i < SBOX_SIZE; i++) {
|
---|
70 | j = j + sbox[i] + key[i % key_size];
|
---|
71 | swap(i, j, sbox);
|
---|
72 | }
|
---|
73 | }
|
---|
74 |
|
---|
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.
|
---|
80 | * @param input_size Size of input data sequence.
|
---|
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 | *
|
---|
89 | */
|
---|
90 | errno_t rc4(uint8_t *key, size_t key_size, uint8_t *input, size_t input_size,
|
---|
91 | size_t skip, uint8_t *output)
|
---|
92 | {
|
---|
93 | if ((!key) || (!input))
|
---|
94 | return EINVAL;
|
---|
95 |
|
---|
96 | if (!output)
|
---|
97 | return ENOMEM;
|
---|
98 |
|
---|
99 | /* Initialize sbox. */
|
---|
100 | uint8_t sbox[SBOX_SIZE];
|
---|
101 | create_sbox(key, key_size, sbox);
|
---|
102 |
|
---|
103 | /* Skip first x bytes. */
|
---|
104 | uint8_t i = 0;
|
---|
105 | uint8_t j = 0;
|
---|
106 | for (size_t k = 0; k < skip; k++) {
|
---|
107 | i = i + 1;
|
---|
108 | j = j + sbox[i];
|
---|
109 | swap(i, j, sbox);
|
---|
110 | }
|
---|
111 |
|
---|
112 | /* Processing loop. */
|
---|
113 | uint8_t val;
|
---|
114 | for (size_t k = 0; k < input_size; k++) {
|
---|
115 | i = i + 1;
|
---|
116 | j = j + sbox[i];
|
---|
117 | swap(i, j, sbox);
|
---|
118 | val = sbox[sbox[i] + sbox[j]];
|
---|
119 | output[k] = val ^ input[k];
|
---|
120 | }
|
---|
121 |
|
---|
122 | return EOK;
|
---|
123 | }
|
---|