| 1 | /* |
| 2 | |
| 3 | Header file for the reference implementation of the Kalyna block cipher (DSTU 7624:2014), all block and key length variants |
| 4 | |
| 5 | Authors: Ruslan Kiianchuk, Ruslan Mordvinov, Roman Oliynykov |
| 6 | |
| 7 | */ |
| 8 | |
| 9 | #ifndef KALYNA_H |
| 10 | #define KALYNA_H |
| 11 | |
| 12 | |
| 13 | #include <stdlib.h> |
| 14 | #include <string.h> |
| 15 | |
| 16 | |
| 17 | typedef unsigned char uint8_t; |
| 18 | typedef unsigned long long uint64_t; |
| 19 | |
| 20 | /*! |
| 21 | * Context to store Kalyna cipher parameters. |
| 22 | */ |
| 23 | typedef struct { |
| 24 | size_t nb; /**< Number of 64-bit words in enciphering block. */ |
| 25 | size_t nk; /**< Number of 64-bit words in key. */ |
| 26 | size_t nr; /**< Number of enciphering rounds. */ |
| 27 | uint64_t* state; /**< Current cipher state. */ |
| 28 | uint64_t** round_keys; /**< Round key computed from enciphering key. */ |
| 29 | } kalyna_t; |
| 30 | |
| 31 | |
| 32 | /*! |
| 33 | * Initialize Kalyna parameters and create cipher context. |
| 34 | * |
| 35 | * @param block_size Enciphering block bit size (128, 256 or 512 bit sizes are |
| 36 | * allowed). |
| 37 | * @param block_size Enciphering key bit size. Must be equal or double the |
| 38 | * block bit size. |
| 39 | * @return Pointer to Kalyna context containing cipher instance |
| 40 | * parameters and allocated memory for state and round keys. NULL in case of |
| 41 | * error. |
| 42 | */ |
| 43 | kalyna_t* KalynaInit(size_t block_size, size_t key_size); |
| 44 | |
| 45 | /*! |
| 46 | * Delete Kalyna cipher context and free used memory. |
| 47 | * |
| 48 | * @param ctx Kalyna cipher context. |
| 49 | * @return Zero in case of success. |
| 50 | */ |
| 51 | int KalynaDelete(kalyna_t* ctx); |
| 52 | |
| 53 | /*! |
| 54 | * Compute round keys given the enciphering key and store them in cipher |
| 55 | * context `ctx`. |
| 56 | * |
| 57 | * @param key Kalyna enciphering key. |
| 58 | * @param ctx Initialized cipher context. |
| 59 | */ |
| 60 | void KalynaKeyExpand(uint64_t* key, kalyna_t* ctx); |
| 61 | |
| 62 | /*! |
| 63 | * Encipher plaintext using Kalyna symmetric block cipher. |
| 64 | * KalynaInit() function with appropriate block and enciphering key sizes must |
| 65 | * be called beforehand to get the cipher context `ctx`. After all enciphering |
| 66 | * is completed KalynaDelete() must be called to free up allocated memory. |
| 67 | * |
| 68 | * @param plaintext Plaintext of length Nb words for enciphering. |
| 69 | * @param ctx Initialized cipher context with precomputed round keys. |
| 70 | * @param ciphertext The result of enciphering. |
| 71 | */ |
| 72 | void KalynaEncipher(uint64_t* plaintext, kalyna_t* ctx, uint64_t* ciphertext); |
| 73 | |
| 74 | /*! |
| 75 | * Decipher ciphertext using Kalyna symmetric block cipher. |
| 76 | * KalynaInit() function with appropriate block and enciphering key sizes must |
| 77 | * be called beforehand to get the cipher context `ctx`. After all enciphering |
| 78 | * is completed KalynaDelete() must be called to free up allocated memory. |
| 79 | * |
| 80 | * @param ciphertext Enciphered data of length Nb words. |
| 81 | * @param ctx Initialized cipher context with precomputed round keys. |
| 82 | * @param plaintext The result of deciphering. |
| 83 | */ |
| 84 | void KalynaDecipher(uint64_t* ciphertext, kalyna_t* ctx, uint64_t* plaintext); |
| 85 | |
| 86 | #endif /* KALYNA_H */ |
| 87 | |