comparison libtomcrypt/src/modes/ctr/ctr_start.c @ 1435:f849a5ca2efc

update to libtomcrypt 1.17 (with Dropbear changes)
author Matt Johnston <matt@ucc.asn.au>
date Sat, 24 Jun 2017 17:50:50 +0800
parents 0cbe8f6dbf9e
children 6dba84798cd5
comparison
equal deleted inserted replaced
1434:27b9ddb06b09 1435:f849a5ca2efc
4 * algorithms in a highly modular and flexible manner. 4 * algorithms in a highly modular and flexible manner.
5 * 5 *
6 * The library is free for all purposes without any express 6 * The library is free for all purposes without any express
7 * guarantee it works. 7 * guarantee it works.
8 * 8 *
9 * Tom St Denis, [email protected], http://libtomcrypt.com 9 * Tom St Denis, [email protected], http://libtom.org
10 */ 10 */
11 #include "tomcrypt.h" 11 #include "tomcrypt.h"
12 12
13 /** 13 /**
14 @file ctr_start.c 14 @file ctr_start.c
44 /* bad param? */ 44 /* bad param? */
45 if ((err = cipher_is_valid(cipher)) != CRYPT_OK) { 45 if ((err = cipher_is_valid(cipher)) != CRYPT_OK) {
46 return err; 46 return err;
47 } 47 }
48 48
49 /* ctrlen == counter width */
50 ctr->ctrlen = (ctr_mode & 255) ? (ctr_mode & 255) : cipher_descriptor[cipher].block_length;
51 if (ctr->ctrlen > cipher_descriptor[cipher].block_length) {
52 return CRYPT_INVALID_ARG;
53 }
54
55 if ((ctr_mode & 0x1000) == CTR_COUNTER_BIG_ENDIAN) {
56 ctr->ctrlen = cipher_descriptor[cipher].block_length - ctr->ctrlen;
57 }
58
49 /* setup cipher */ 59 /* setup cipher */
50 if ((err = cipher_descriptor[cipher].setup(key, keylen, num_rounds, &ctr->key)) != CRYPT_OK) { 60 if ((err = cipher_descriptor[cipher].setup(key, keylen, num_rounds, &ctr->key)) != CRYPT_OK) {
51 return err; 61 return err;
52 } 62 }
53 63
54 /* copy ctr */ 64 /* copy ctr */
55 ctr->blocklen = cipher_descriptor[cipher].block_length; 65 ctr->blocklen = cipher_descriptor[cipher].block_length;
56 ctr->cipher = cipher; 66 ctr->cipher = cipher;
57 ctr->padlen = 0; 67 ctr->padlen = 0;
58 ctr->mode = ctr_mode & 1; 68 ctr->mode = ctr_mode & 0x1000;
59 for (x = 0; x < ctr->blocklen; x++) { 69 for (x = 0; x < ctr->blocklen; x++) {
60 ctr->ctr[x] = IV[x]; 70 ctr->ctr[x] = IV[x];
61 } 71 }
62 72
63 if (ctr_mode & LTC_CTR_RFC3686) { 73 if (ctr_mode & LTC_CTR_RFC3686) {
64 /* increment the IV as per RFC 3686 */ 74 /* increment the IV as per RFC 3686 */
65 if (ctr->mode == CTR_COUNTER_LITTLE_ENDIAN) { 75 if (ctr->mode == CTR_COUNTER_LITTLE_ENDIAN) {
66 /* little-endian */ 76 /* little-endian */
67 for (x = 0; x < ctr->blocklen; x++) { 77 for (x = 0; x < ctr->ctrlen; x++) {
68 ctr->ctr[x] = (ctr->ctr[x] + (unsigned char)1) & (unsigned char)255; 78 ctr->ctr[x] = (ctr->ctr[x] + (unsigned char)1) & (unsigned char)255;
69 if (ctr->ctr[x] != (unsigned char)0) { 79 if (ctr->ctr[x] != (unsigned char)0) {
70 break; 80 break;
71 } 81 }
72 } 82 }
73 } else { 83 } else {
74 /* big-endian */ 84 /* big-endian */
75 for (x = ctr->blocklen-1; x >= 0; x--) { 85 for (x = ctr->blocklen-1; x >= ctr->ctrlen; x--) {
76 ctr->ctr[x] = (ctr->ctr[x] + (unsigned char)1) & (unsigned char)255; 86 ctr->ctr[x] = (ctr->ctr[x] + (unsigned char)1) & (unsigned char)255;
77 if (ctr->ctr[x] != (unsigned char)0) { 87 if (ctr->ctr[x] != (unsigned char)0) {
78 break; 88 break;
79 } 89 }
80 } 90 }
84 return cipher_descriptor[ctr->cipher].ecb_encrypt(ctr->ctr, ctr->pad, &ctr->key); 94 return cipher_descriptor[ctr->cipher].ecb_encrypt(ctr->ctr, ctr->pad, &ctr->key);
85 } 95 }
86 96
87 #endif 97 #endif
88 98
89 /* $Source: /cvs/libtom/libtomcrypt/src/modes/ctr/ctr_start.c,v $ */ 99 /* $Source$ */
90 /* $Revision: 1.11 $ */ 100 /* $Revision$ */
91 /* $Date: 2006/11/05 01:46:35 $ */ 101 /* $Date$ */