Changeset d7dadcb4 in mainline for uspace/lib/crypto/aes.c
- Timestamp:
- 2015-04-08T10:54:31Z (10 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 053fc2b
- Parents:
- 1dcc0b9
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/crypto/aes.c
r1dcc0b9 rd7dadcb4 29 29 /** @file aes.c 30 30 * 31 * Implementation of AES algorithm.31 * Implementation of AES-128 symmetric cipher cryptographic algorithm. 32 32 * 33 33 * Based on FIPS 197. 34 34 */ 35 35 36 #include <std io.h>36 #include <stdbool.h> 37 37 #include <errno.h> 38 38 #include <mem.h> … … 60 60 61 61 /* Precomputed values for AES sub_byte transformation. */ 62 static const uint8_t s ub_byte_array[BLOCK_LEN][BLOCK_LEN] = {62 static const uint8_t sbox[BLOCK_LEN][BLOCK_LEN] = { 63 63 { 64 64 0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, … … 128 128 129 129 /* Precomputed values for AES inv_sub_byte transformation. */ 130 static uint8_t inv_s ub_byte_array[BLOCK_LEN][BLOCK_LEN] = {130 static uint8_t inv_sbox[BLOCK_LEN][BLOCK_LEN] = { 131 131 { 132 132 0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38, … … 202 202 }; 203 203 204 /** 205 * Perform substitution transformation on given byte. 206 * 207 * @param byte Input byte. 208 * @param inv Flag indicating whether to use inverse table. 209 * 210 * @return Substituted value. 211 */ 204 212 static uint8_t sub_byte(uint8_t byte, bool inv) 205 213 { … … 208 216 209 217 if(!inv) { 210 return s ub_byte_array[i][j];218 return sbox[i][j]; 211 219 } else { 212 return inv_sub_byte_array[i][j]; 213 } 214 } 215 220 return inv_sbox[i][j]; 221 } 222 } 223 224 /** 225 * Perform substitution transformation on state table. 226 * 227 * @param state State table to be modified. 228 * @param inv Flag indicating whether to use inverse table. 229 */ 216 230 static void sub_bytes(uint8_t state[ELEMS][ELEMS], bool inv) 217 231 { … … 226 240 } 227 241 242 /** 243 * Perform shift rows transformation on state table. 244 * 245 * @param state State table to be modified. 246 */ 228 247 static void shift_rows(uint8_t state[ELEMS][ELEMS]) 229 248 { … … 237 256 } 238 257 258 /** 259 * Perform inverted shift rows transformation on state table. 260 * 261 * @param state State table to be modified. 262 */ 239 263 static void inv_shift_rows(uint8_t state[ELEMS][ELEMS]) 240 264 { … … 248 272 } 249 273 274 /** 275 * Multiplication in GF(2^8). 276 * 277 * @param x First factor. 278 * @param y Second factor. 279 * 280 * @return Multiplication of given factors in GF(2^8). 281 */ 250 282 static uint8_t galois_mult(uint8_t x, uint8_t y) { 251 283 uint8_t result = 0; … … 265 297 } 266 298 299 /** 300 * Perform mix columns transformation on state table. 301 * 302 * @param state State table to be modified. 303 */ 267 304 static void mix_columns(uint8_t state[ELEMS][ELEMS]) 268 305 { … … 294 331 } 295 332 333 /** 334 * Perform inverted mix columns transformation on state table. 335 * 336 * @param state State table to be modified. 337 */ 296 338 static void inv_mix_columns(uint8_t state[ELEMS][ELEMS]) 297 339 { … … 323 365 } 324 366 367 /** 368 * Perform round key transformation on state table. 369 * 370 * @param state State table to be modified. 371 * @param round_key Round key to be applied on state table. 372 */ 325 373 static void add_round_key(uint8_t state[ELEMS][ELEMS], uint32_t *round_key) 326 374 { … … 338 386 } 339 387 388 /** 389 * Perform substitution transformation on given word. 390 * 391 * @param byte Input word. 392 * 393 * @return Substituted word. 394 */ 340 395 static uint32_t sub_word(uint32_t word) 341 396 { … … 350 405 } 351 406 407 /** 408 * Perform left rotation by one byte on given word. 409 * 410 * @param byte Input word. 411 * 412 * @return Rotated word. 413 */ 352 414 static uint32_t rot_word(uint32_t word) 353 415 { … … 355 417 } 356 418 419 /** 420 * Key expansion procedure for AES algorithm. 421 * 422 * @param key Input key. 423 * @param key_exp Result key expansion. 424 */ 357 425 static void key_expansion(uint8_t *key, uint32_t *key_exp) 358 426 { … … 379 447 } 380 448 449 /** 450 * AES-128 encryption algorithm. 451 * 452 * @param key Input key. 453 * @param input Input data sequence to be encrypted. 454 * @param output Encrypted data sequence. 455 * 456 * @return EINVAL when input or key not specified, ENOMEM when pointer for 457 * output is not allocated, otherwise EOK. 458 */ 381 459 int aes_encrypt(uint8_t *key, uint8_t *input, uint8_t *output) 382 460 { … … 404 482 for(size_t k = 1; k <= ROUNDS; k++) { 405 483 sub_bytes(state, false); 406 for(size_t i = 0; i < ELEMS; i++) {407 for(size_t j = 0; j < ELEMS; j++) {408 printf("STATE SUB %d %d : %x\n", i, j, state[i][j]);409 }410 }411 484 shift_rows(state); 412 for(size_t i = 0; i < ELEMS; i++) {413 for(size_t j = 0; j < ELEMS; j++) {414 printf("STATE SHIFT %d %d : %x\n", i, j, state[i][j]);415 }416 }417 485 if(k < ROUNDS) 418 486 mix_columns(state); … … 430 498 } 431 499 500 /** 501 * AES-128 decryption algorithm. 502 * 503 * @param key Input key. 504 * @param input Input data sequence to be decrypted. 505 * @param output Decrypted data sequence. 506 * 507 * @return EINVAL when input or key not specified, ENOMEM when pointer for 508 * output is not allocated, otherwise EOK. 509 */ 432 510 int aes_decrypt(uint8_t *key, uint8_t *input, uint8_t *output) 433 511 {
Note:
See TracChangeset
for help on using the changeset viewer.