-
Notifications
You must be signed in to change notification settings - Fork 173
/
CryptLib_AesCtr.h
140 lines (126 loc) · 6.6 KB
/
CryptLib_AesCtr.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// CryptLib_AesCtr
//
// Implementation of AES CTR stream cipher.
//
// Depends on: CryptoLib_Aes
//
// AES CTR is a stream cipher using the AES block cipher in counter mode.
// This implementation works on both little and big endian architectures.
//
// This is free and unencumbered software released into the public domain - November 2017 waterjuice.org
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma once
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// IMPORTS
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <stdint.h>
#include "CryptLib_Aes.h"
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// TYPES
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#define AES_CTR_IV_SIZE 8
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// TYPES
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// AesCtrContext
// Do not modify the contents of this structure directly.
typedef struct
{
AesContext Aes;
uint8_t IV [AES_CTR_IV_SIZE];
uint64_t StreamIndex;
uint64_t CurrentCipherBlockIndex;
uint8_t CurrentCipherBlock [AES_BLOCK_SIZE];
} AesCtrContext;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// PUBLIC FUNCTIONS
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// AesCtrInitialise
//
// Initialises an AesCtrContext with an already initialised AesContext and a IV. This function can quickly be used
// to change the IV without requiring the more length processes of reinitialising an AES key.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void
AesCtrInitialise
(
AesCtrContext* Context, // [out]
AesContext const* InitialisedAesContext, // [in]
uint8_t const IV [AES_CTR_IV_SIZE] // [in]
);
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// AesCtrInitialiseWithKey
//
// Initialises an AesCtrContext with an AES Key and an IV. This combines the initialising an AES Context and then
// running AesCtrInitialise. KeySize must be 16, 24, or 32 (for 128, 192, or 256 bit key size)
// Returns 0 if successful, or -1 if invalid KeySize provided
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int
AesCtrInitialiseWithKey
(
AesCtrContext* Context, // [out]
uint8_t const* Key, // [in]
uint32_t KeySize, // [in]
uint8_t const IV [AES_CTR_IV_SIZE] // [in]
);
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// AesCtrSetStreamIndex
//
// Sets the current stream index to any arbitrary position. Setting to 0 sets it to the beginning of the stream. Any
// subsequent output will start from this position
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void
AesCtrSetStreamIndex
(
AesCtrContext* Context, // [in out]
uint64_t StreamIndex // [in]
);
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// AesCtrXor
//
// XORs the stream of byte of the AesCtrContext from its current stream position onto the specified buffer. This will
// advance the stream index by that number of bytes.
// Use once over data to encrypt it. Use it a second time over the same data from the same stream position and the
// data will be decrypted.
// InBuffer and OutBuffer can point to the same location for in-place encrypting/decrypting
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void
AesCtrXor
(
AesCtrContext* Context, // [in out]
void const* InBuffer, // [in]
void* OutBuffer, // [out]
uint32_t Size // [in]
);
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// AesCtrOutput
//
// Outputs the stream of byte of the AesCtrContext from its current stream position. This will advance the stream
// index by that number of bytes.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void
AesCtrOutput
(
AesCtrContext* Context, // [in out]
void* Buffer, // [out]
uint32_t Size // [in]
);
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// AesCtrXorWithKey
//
// This function combines AesCtrInitialiseWithKey and AesCtrXor. This is suitable when encrypting/decypting data in
// one go with a key that is not going to be reused.
// This will used the provided Key and IV and generate a stream that is XORed over Buffer.
// Returns 0 if successful, or -1 if invalid KeySize provided
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int
AesCtrXorWithKey
(
uint8_t const* Key, // [in]
uint32_t KeySize, // [in]
uint8_t const IV [AES_CTR_IV_SIZE], // [in]
void const* InBuffer, // [in]
void* OutBuffer, // [out]
uint32_t BufferSize // [in]
);