Webserver+3d print
cyclone_crypto/keccak.h@0:8918a71cdbe9, 2017-02-04 (annotated)
- Committer:
- Sergunb
- Date:
- Sat Feb 04 18:15:49 2017 +0000
- Revision:
- 0:8918a71cdbe9
nothing else
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Sergunb | 0:8918a71cdbe9 | 1 | /** |
Sergunb | 0:8918a71cdbe9 | 2 | * @file keccak.h |
Sergunb | 0:8918a71cdbe9 | 3 | * @brief Keccak sponge function |
Sergunb | 0:8918a71cdbe9 | 4 | * |
Sergunb | 0:8918a71cdbe9 | 5 | * @section License |
Sergunb | 0:8918a71cdbe9 | 6 | * |
Sergunb | 0:8918a71cdbe9 | 7 | * Copyright (C) 2010-2017 Oryx Embedded SARL. All rights reserved. |
Sergunb | 0:8918a71cdbe9 | 8 | * |
Sergunb | 0:8918a71cdbe9 | 9 | * This file is part of CycloneCrypto Open. |
Sergunb | 0:8918a71cdbe9 | 10 | * |
Sergunb | 0:8918a71cdbe9 | 11 | * This program is free software; you can redistribute it and/or |
Sergunb | 0:8918a71cdbe9 | 12 | * modify it under the terms of the GNU General Public License |
Sergunb | 0:8918a71cdbe9 | 13 | * as published by the Free Software Foundation; either version 2 |
Sergunb | 0:8918a71cdbe9 | 14 | * of the License, or (at your option) any later version. |
Sergunb | 0:8918a71cdbe9 | 15 | * |
Sergunb | 0:8918a71cdbe9 | 16 | * This program is distributed in the hope that it will be useful, |
Sergunb | 0:8918a71cdbe9 | 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
Sergunb | 0:8918a71cdbe9 | 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
Sergunb | 0:8918a71cdbe9 | 19 | * GNU General Public License for more details. |
Sergunb | 0:8918a71cdbe9 | 20 | * |
Sergunb | 0:8918a71cdbe9 | 21 | * You should have received a copy of the GNU General Public License |
Sergunb | 0:8918a71cdbe9 | 22 | * along with this program; if not, write to the Free Software Foundation, |
Sergunb | 0:8918a71cdbe9 | 23 | * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
Sergunb | 0:8918a71cdbe9 | 24 | * |
Sergunb | 0:8918a71cdbe9 | 25 | * @author Oryx Embedded SARL (www.oryx-embedded.com) |
Sergunb | 0:8918a71cdbe9 | 26 | * @version 1.7.6 |
Sergunb | 0:8918a71cdbe9 | 27 | **/ |
Sergunb | 0:8918a71cdbe9 | 28 | |
Sergunb | 0:8918a71cdbe9 | 29 | #ifndef _KECCAK_H |
Sergunb | 0:8918a71cdbe9 | 30 | #define _KECCAK_H |
Sergunb | 0:8918a71cdbe9 | 31 | |
Sergunb | 0:8918a71cdbe9 | 32 | //Dependencies |
Sergunb | 0:8918a71cdbe9 | 33 | #include "crypto.h" |
Sergunb | 0:8918a71cdbe9 | 34 | |
Sergunb | 0:8918a71cdbe9 | 35 | //The binary logarithm of the lane size |
Sergunb | 0:8918a71cdbe9 | 36 | #ifndef KECCAK_L |
Sergunb | 0:8918a71cdbe9 | 37 | #define KECCAK_L 6 |
Sergunb | 0:8918a71cdbe9 | 38 | #endif |
Sergunb | 0:8918a71cdbe9 | 39 | |
Sergunb | 0:8918a71cdbe9 | 40 | //Check lane size |
Sergunb | 0:8918a71cdbe9 | 41 | #if (KECCAK_L == 3) |
Sergunb | 0:8918a71cdbe9 | 42 | //Base type that represents a lane |
Sergunb | 0:8918a71cdbe9 | 43 | typedef uint8_t keccak_lane_t; |
Sergunb | 0:8918a71cdbe9 | 44 | //Rotate left operation |
Sergunb | 0:8918a71cdbe9 | 45 | #define KECCAK_ROL(a, n) ROL8(a, n) |
Sergunb | 0:8918a71cdbe9 | 46 | //Host byte order to little-endian byte order |
Sergunb | 0:8918a71cdbe9 | 47 | #define KECCAK_HTOLE(a) (a) |
Sergunb | 0:8918a71cdbe9 | 48 | //Little-endian byte order to host byte order |
Sergunb | 0:8918a71cdbe9 | 49 | #define KECCAK_LETOH(a) (a) |
Sergunb | 0:8918a71cdbe9 | 50 | #elif (KECCAK_L == 4) |
Sergunb | 0:8918a71cdbe9 | 51 | //Base type that represents a lane |
Sergunb | 0:8918a71cdbe9 | 52 | #define keccak_lane_t uint16_t |
Sergunb | 0:8918a71cdbe9 | 53 | //Rotate left operation |
Sergunb | 0:8918a71cdbe9 | 54 | #define KECCAK_ROL(a, n) ROL16(a, n) |
Sergunb | 0:8918a71cdbe9 | 55 | //Host byte order to little-endian byte order |
Sergunb | 0:8918a71cdbe9 | 56 | #define KECCAK_HTOLE(a) htole16(a) |
Sergunb | 0:8918a71cdbe9 | 57 | //Little-endian byte order to host byte order |
Sergunb | 0:8918a71cdbe9 | 58 | #define KECCAK_LETOH(a) letoh16(a) |
Sergunb | 0:8918a71cdbe9 | 59 | #elif (KECCAK_L == 5) |
Sergunb | 0:8918a71cdbe9 | 60 | //Base type that represents a lane |
Sergunb | 0:8918a71cdbe9 | 61 | #define keccak_lane_t uint32_t |
Sergunb | 0:8918a71cdbe9 | 62 | //Rotate left operation |
Sergunb | 0:8918a71cdbe9 | 63 | #define KECCAK_ROL(a, n) ROL32(a, n) |
Sergunb | 0:8918a71cdbe9 | 64 | //Host byte order to little-endian byte order |
Sergunb | 0:8918a71cdbe9 | 65 | #define KECCAK_HTOLE(a) htole32(a) |
Sergunb | 0:8918a71cdbe9 | 66 | //Little-endian byte order to host byte order |
Sergunb | 0:8918a71cdbe9 | 67 | #define KECCAK_LETOH(a) letoh32(a) |
Sergunb | 0:8918a71cdbe9 | 68 | #elif (KECCAK_L == 6) |
Sergunb | 0:8918a71cdbe9 | 69 | //Base type that represents a lane |
Sergunb | 0:8918a71cdbe9 | 70 | #define keccak_lane_t uint64_t |
Sergunb | 0:8918a71cdbe9 | 71 | //Rotate left operation |
Sergunb | 0:8918a71cdbe9 | 72 | #define KECCAK_ROL(a, n) ROL64(a, n) |
Sergunb | 0:8918a71cdbe9 | 73 | //Host byte order to little-endian byte order conversion |
Sergunb | 0:8918a71cdbe9 | 74 | #define KECCAK_HTOLE(a) htole64(a) |
Sergunb | 0:8918a71cdbe9 | 75 | //Little-endian byte order to host byte order conversion |
Sergunb | 0:8918a71cdbe9 | 76 | #define KECCAK_LETOH(a) letoh64(a) |
Sergunb | 0:8918a71cdbe9 | 77 | #else |
Sergunb | 0:8918a71cdbe9 | 78 | #error KECCAK_L parameter is not valid |
Sergunb | 0:8918a71cdbe9 | 79 | #endif |
Sergunb | 0:8918a71cdbe9 | 80 | |
Sergunb | 0:8918a71cdbe9 | 81 | //The lane size of a Keccak-p permutation in bits |
Sergunb | 0:8918a71cdbe9 | 82 | #define KECCAK_W (1 << KECCAK_L) |
Sergunb | 0:8918a71cdbe9 | 83 | //The width of a Keccak-p permutation |
Sergunb | 0:8918a71cdbe9 | 84 | #define KECCAK_B (KECCAK_W * 25) |
Sergunb | 0:8918a71cdbe9 | 85 | //The number of rounds for a Keccak-p permutation |
Sergunb | 0:8918a71cdbe9 | 86 | #define KECCAK_NR (12 + 2 * KECCAK_L) |
Sergunb | 0:8918a71cdbe9 | 87 | |
Sergunb | 0:8918a71cdbe9 | 88 | |
Sergunb | 0:8918a71cdbe9 | 89 | /** |
Sergunb | 0:8918a71cdbe9 | 90 | * @brief Keccak context |
Sergunb | 0:8918a71cdbe9 | 91 | **/ |
Sergunb | 0:8918a71cdbe9 | 92 | |
Sergunb | 0:8918a71cdbe9 | 93 | typedef struct |
Sergunb | 0:8918a71cdbe9 | 94 | { |
Sergunb | 0:8918a71cdbe9 | 95 | union |
Sergunb | 0:8918a71cdbe9 | 96 | { |
Sergunb | 0:8918a71cdbe9 | 97 | keccak_lane_t a[5][5]; |
Sergunb | 0:8918a71cdbe9 | 98 | uint8_t digest[1]; |
Sergunb | 0:8918a71cdbe9 | 99 | }; |
Sergunb | 0:8918a71cdbe9 | 100 | union |
Sergunb | 0:8918a71cdbe9 | 101 | { |
Sergunb | 0:8918a71cdbe9 | 102 | keccak_lane_t block[24]; |
Sergunb | 0:8918a71cdbe9 | 103 | uint8_t buffer[1]; |
Sergunb | 0:8918a71cdbe9 | 104 | }; |
Sergunb | 0:8918a71cdbe9 | 105 | uint_t blockSize; |
Sergunb | 0:8918a71cdbe9 | 106 | size_t length; |
Sergunb | 0:8918a71cdbe9 | 107 | } KeccakContext; |
Sergunb | 0:8918a71cdbe9 | 108 | |
Sergunb | 0:8918a71cdbe9 | 109 | |
Sergunb | 0:8918a71cdbe9 | 110 | //Keccak related functions |
Sergunb | 0:8918a71cdbe9 | 111 | error_t keccakInit(KeccakContext *context, uint_t capacity); |
Sergunb | 0:8918a71cdbe9 | 112 | void keccakAbsorb(KeccakContext *context, const void *input, size_t length); |
Sergunb | 0:8918a71cdbe9 | 113 | void keccakFinal(KeccakContext *context, uint8_t pad); |
Sergunb | 0:8918a71cdbe9 | 114 | void keccakSqueeze(KeccakContext *context, uint8_t *output, size_t length); |
Sergunb | 0:8918a71cdbe9 | 115 | void keccakPermutBlock(KeccakContext *context); |
Sergunb | 0:8918a71cdbe9 | 116 | |
Sergunb | 0:8918a71cdbe9 | 117 | #endif |
Sergunb | 0:8918a71cdbe9 | 118 |