Changeset 8a64320e in mainline for uspace/lib/crypto/rc4.c
- Timestamp:
- 2015-04-23T23:40:14Z (10 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- dcba819
- Parents:
- 09044cb
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/crypto/rc4.c
r09044cb r8a64320e 28 28 29 29 /** @file rc4.c 30 * 30 * 31 31 * Implementation of ARC4 symmetric cipher cryptographic algorithm. 32 * 32 * 33 33 */ 34 34 35 35 #include <errno.h> 36 36 #include <mem.h> 37 38 37 #include "crypto.h" 39 38 40 39 /* Sbox table size. */ 41 #define SBOX_SIZE 25640 #define SBOX_SIZE 256 42 41 43 /** 44 * Swap two values in sbox. 45 * 46 * @param i First index of value in sbox to be swapped. 47 * @param j Second index of value in sbox to be swapped. 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. 48 46 * @param sbox Sbox to be modified. 47 * 49 48 */ 50 49 static void swap(size_t i, size_t j, uint8_t *sbox) … … 55 54 } 56 55 57 /** 58 * Sbox initialization procedure. 59 * 60 * @param key Input key. 56 /** Sbox initialization procedure. 57 * 58 * @param key Input key. 61 59 * @param key_size Size of key sequence. 62 * @param sbox Place for result sbox. 60 * @param sbox Place for result sbox. 61 * 63 62 */ 64 63 static void create_sbox(uint8_t *key, size_t key_size, uint8_t *sbox) 65 64 { 66 for (size_t i = 0; i < SBOX_SIZE; i++) {65 for (size_t i = 0; i < SBOX_SIZE; i++) 67 66 sbox[i] = i; 68 }69 67 70 68 uint8_t j = 0; 71 for (size_t i = 0; i < SBOX_SIZE; i++) {69 for (size_t i = 0; i < SBOX_SIZE; i++) { 72 70 j = j + sbox[i] + key[i % key_size]; 73 71 swap(i, j, sbox); … … 75 73 } 76 74 77 /** 78 * ARC4 encryption/decryption algorithm. 79 * 80 * @param key Input key. 81 * @param key_size Size of key sequence. 82 * @param input Input data sequence to be processed. 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. 83 80 * @param input_size Size of input data sequence. 84 * @param skip Number of bytes to be skipped from the beginning of key stream. 85 * @param output Result data sequence. 86 * 87 * @return EINVAL when input or key not specified, ENOMEM when pointer for 88 * output is not allocated, otherwise EOK. 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 89 */ 90 int rc4(uint8_t *key, size_t key_size, uint8_t *input, size_t input_size, 91 90 int rc4(uint8_t *key, size_t key_size, uint8_t *input, size_t input_size, 91 size_t skip, uint8_t *output) 92 92 { 93 if (!key || !input)93 if ((!key) || (!input)) 94 94 return EINVAL; 95 95 96 if (!output)96 if (!output) 97 97 return ENOMEM; 98 98 … … 102 102 103 103 /* Skip first x bytes. */ 104 uint8_t i = 0, j = 0; 105 for(size_t k = 0; k < skip; k++) { 104 uint8_t i = 0; 105 uint8_t j = 0; 106 for (size_t k = 0; k < skip; k++) { 106 107 i = i + 1; 107 108 j = j + sbox[i]; … … 111 112 /* Processing loop. */ 112 113 uint8_t val; 113 for (size_t k = 0; k < input_size; k++) {114 for (size_t k = 0; k < input_size; k++) { 114 115 i = i + 1; 115 116 j = j + sbox[i];
Note:
See TracChangeset
for help on using the changeset viewer.