diff options
Diffstat (limited to 'src/lib/libcrypto/aes/aes_ctr.c')
| -rw-r--r-- | src/lib/libcrypto/aes/aes_ctr.c | 21 |
1 files changed, 16 insertions, 5 deletions
diff --git a/src/lib/libcrypto/aes/aes_ctr.c b/src/lib/libcrypto/aes/aes_ctr.c index aea3db2092..59088499a0 100644 --- a/src/lib/libcrypto/aes/aes_ctr.c +++ b/src/lib/libcrypto/aes/aes_ctr.c | |||
| @@ -49,7 +49,13 @@ | |||
| 49 | * | 49 | * |
| 50 | */ | 50 | */ |
| 51 | 51 | ||
| 52 | #ifndef AES_DEBUG | ||
| 53 | # ifndef NDEBUG | ||
| 54 | # define NDEBUG | ||
| 55 | # endif | ||
| 56 | #endif | ||
| 52 | #include <assert.h> | 57 | #include <assert.h> |
| 58 | |||
| 53 | #include <openssl/aes.h> | 59 | #include <openssl/aes.h> |
| 54 | #include "aes_locl.h" | 60 | #include "aes_locl.h" |
| 55 | 61 | ||
| @@ -90,26 +96,31 @@ static void AES_ctr128_inc(unsigned char *counter) { | |||
| 90 | 96 | ||
| 91 | /* The input encrypted as though 128bit counter mode is being | 97 | /* The input encrypted as though 128bit counter mode is being |
| 92 | * used. The extra state information to record how much of the | 98 | * used. The extra state information to record how much of the |
| 93 | * 128bit block we have used is contained in *num; | 99 | * 128bit block we have used is contained in *num, and the |
| 100 | * encrypted counter is kept in ecount_buf. Both *num and | ||
| 101 | * ecount_buf must be initialised with zeros before the first | ||
| 102 | * call to AES_ctr128_encrypt(). | ||
| 94 | */ | 103 | */ |
| 95 | void AES_ctr128_encrypt(const unsigned char *in, unsigned char *out, | 104 | void AES_ctr128_encrypt(const unsigned char *in, unsigned char *out, |
| 96 | const unsigned long length, const AES_KEY *key, | 105 | const unsigned long length, const AES_KEY *key, |
| 97 | unsigned char *counter, unsigned int *num) { | 106 | unsigned char counter[AES_BLOCK_SIZE], |
| 107 | unsigned char ecount_buf[AES_BLOCK_SIZE], | ||
| 108 | unsigned int *num) { | ||
| 98 | 109 | ||
| 99 | unsigned int n; | 110 | unsigned int n; |
| 100 | unsigned long l=length; | 111 | unsigned long l=length; |
| 101 | unsigned char tmp[AES_BLOCK_SIZE]; | ||
| 102 | 112 | ||
| 103 | assert(in && out && key && counter && num); | 113 | assert(in && out && key && counter && num); |
| 114 | assert(*num < AES_BLOCK_SIZE); | ||
| 104 | 115 | ||
| 105 | n = *num; | 116 | n = *num; |
| 106 | 117 | ||
| 107 | while (l--) { | 118 | while (l--) { |
| 108 | if (n == 0) { | 119 | if (n == 0) { |
| 109 | AES_encrypt(counter, tmp, key); | 120 | AES_encrypt(counter, ecount_buf, key); |
| 110 | AES_ctr128_inc(counter); | 121 | AES_ctr128_inc(counter); |
| 111 | } | 122 | } |
| 112 | *(out++) = *(in++) ^ tmp[n]; | 123 | *(out++) = *(in++) ^ ecount_buf[n]; |
| 113 | n = (n+1) % AES_BLOCK_SIZE; | 124 | n = (n+1) % AES_BLOCK_SIZE; |
| 114 | } | 125 | } |
| 115 | 126 | ||
