[Git][NTPsec/ntpsec][cookie] 4 commits: tests/ntpd/nts_cookie.c: Remove tabs, Single line /* comments to //
Hal Murray (@hal.murray)
gitlab at mg.gitlab.com
Thu Aug 6 17:20:24 UTC 2026
Hal Murray pushed to branch cookie at NTPsec / ntpsec
Commits:
d73d3498 by Gary E. Miller at 2026-08-06T10:16:50-07:00
tests/ntpd/nts_cookie.c: Remove tabs, Single line /* comments to //
No functional changes.
- - - - -
2255add0 by Gary E. Miller at 2026-08-06T10:16:50-07:00
tests/ntpd/nts_cookie.c: restore previous comment indents
No functional changes.
- - - - -
b18adc04 by Gary E. Miller at 2026-08-06T10:20:17-07:00
ntpd/nts_cookie.c: Remove tabs, Single line /* comments to //
No functional changes.
- - - - -
2d5d803d by Gary E. Miller at 2026-08-06T10:20:17-07:00
ntpd/nts_cookie.c: restore previous comment indents
No functional changes.
- - - - -
2 changed files:
- ntpd/nts_cookie.c
- tests/ntpd/nts_cookie.c
Changes:
=====================================
ntpd/nts_cookie.c
=====================================
@@ -81,7 +81,7 @@
* #define is in include/nts.h
*/
-/* cookies use same AEAD algorithms as wire */
+// cookies use same AEAD algorithms as wire
/* This determines which algorithm we use.
* Valid choices are 32, 48, and 64
* making this a variable rather than #define
@@ -90,7 +90,7 @@
* You can change that by editing the keys file.
*/
int K_length = AEAD_AES_SIV_CMAC_256_KEYLEN;
-time_t K_time = 0; /* time K was created, 0 for none */
+time_t K_time = 0; // time K was created, 0 for none
struct NTS_Key nts_keys[NTS_nKEYS];
int nts_nKeys = 0;
@@ -105,11 +105,11 @@ void nts_lock_cookielock(void);
void nts_unlock_cookielock(void);
// FIXME AEAD_LENGTH
-/* Associated data: aead (rounded up to 4) plus NONCE */
+// Associated data: aead (rounded up to 4) plus NONCE
#define AD_LENGTH 20
#define AEAD_LENGTH 4
-/* cookie_ctx needed for client side */
+// cookie_ctx needed for client side
bool nts_cookie_init(void) {
cookie_ctx = AES_SIV_CTX_new();
if (NULL == cookie_ctx) {
@@ -119,16 +119,16 @@ bool nts_cookie_init(void) {
return true;
}
-/* cookie key needed for server side */
+// cookie key needed for server side
bool nts_cookie_init2(void) {
- bool OK = true;
- if (!nts_read_cookie_keys()) {
- /* Can't read cookie file. Make one */
- nts_make_cookie_key();
- K_time = time(NULL);
- nts_write_cookie_keys();
- }
- return OK;
+ bool OK = true;
+ if (!nts_read_cookie_keys()) {
+ // Can't read cookie file. Make one
+ nts_make_cookie_key();
+ K_time = time(NULL);
+ nts_write_cookie_keys();
+ }
+ return OK;
}
/* Rotate key -- 24 hours after last rotation
@@ -142,83 +142,83 @@ bool nts_cookie_init2(void) {
// Just uncommenting the next line will generate a warning reminder.
// #define SecondsPerDay 3600
void nts_cookie_timer(void) {
- time_t now;
- if (0 == K_time) {
- return;
- }
- now = time(NULL);
- if (SecondsPerDay > (now-K_time)) {
- return;
- }
- nts_make_cookie_key();
- /* In case we were off for many days. */
- while (SecondsPerDay < (now-K_time)) {
- K_time += SecondsPerDay;
- }
- if (nts_write_cookie_keys() )
- msyslog(LOG_INFO, "NTS: Wrote new cookie file, %d keys.", nts_nKeys);
- else
- msyslog(LOG_INFO, "NTS: Trouble writing new cookie file.");
- return;
+ time_t now;
+ if (0 == K_time) {
+ return;
+ }
+ now = time(NULL);
+ if (SecondsPerDay > (now-K_time)) {
+ return;
+ }
+ nts_make_cookie_key();
+ // In case we were off for many days.
+ while (SecondsPerDay < (now-K_time)) {
+ K_time += SecondsPerDay;
+ }
+ if (nts_write_cookie_keys() )
+ msyslog(LOG_INFO, "NTS: Wrote new cookie file, %d keys.", nts_nKeys);
+ else
+ msyslog(LOG_INFO, "NTS: Trouble writing new cookie file.");
+ return;
}
bool nts_read_cookie_keys(void) {
- const char *cookie_filename = NTS_COOKIE_KEY_FILE;
- FILE *in;
- unsigned long templ;
- if (NULL != ntsconfig.KI)
- cookie_filename = ntsconfig.KI;
- in = fopen(cookie_filename, "r");
- if (NULL == in) {
- char errbuf[100];
- if (ENOENT == errno)
- return false; /* File doesn't exist */
- ntp_strerror_r(errno, errbuf, sizeof(errbuf));
- msyslog(LOG_ERR, "NTSs: can't read old cookie file: %s=>%s",
- cookie_filename, errbuf);
- exit(1);
- }
- if (1 != fscanf(in, "T: %lu\n", &templ)) {
- goto bail;
- }
- K_time = templ;
- if (1 != fscanf(in, "L: %d\n", &K_length)) {
- goto bail;
- }
- if ( !((32 == K_length) || (48 == K_length) || (64 == K_length))) {
- goto bail;
- }
- nts_nKeys = 0;
- for (int i=0; i<NTS_nKEYS; i++) {
- struct NTS_Key *key = &nts_keys[i];
- if (1 != fscanf(in, "I: %u\n", &key->I)) {
- if (0 < nts_nKeys) break;
- goto bail;
- }
- if (0 != fscanf(in, "K: ")) {
- goto bail;
- }
- for (int j=0; j< K_length; j++) {
- unsigned int temp;
- if (1 != fscanf(in, "%02x", &temp)) {
- goto bail;
- }
- key->K[j] = temp;
- }
- if (0 != fscanf(in, "\n")) {
- goto bail;
- }
- nts_nKeys = i+1;
- }
- fclose(in);
- msyslog(LOG_INFO, "NTS: Read cookie file, %d keys.", nts_nKeys);
- return true;
+ const char *cookie_filename = NTS_COOKIE_KEY_FILE;
+ FILE *in;
+ unsigned long templ;
+ if (NULL != ntsconfig.KI)
+ cookie_filename = ntsconfig.KI;
+ in = fopen(cookie_filename, "r");
+ if (NULL == in) {
+ char errbuf[100];
+ if (ENOENT == errno)
+ return false; // File doesn't exist
+ ntp_strerror_r(errno, errbuf, sizeof(errbuf));
+ msyslog(LOG_ERR, "NTSs: can't read old cookie file: %s=>%s",
+ cookie_filename, errbuf);
+ exit(1);
+ }
+ if (1 != fscanf(in, "T: %lu\n", &templ)) {
+ goto bail;
+ }
+ K_time = templ;
+ if (1 != fscanf(in, "L: %d\n", &K_length)) {
+ goto bail;
+ }
+ if ( !((32 == K_length) || (48 == K_length) || (64 == K_length))) {
+ goto bail;
+ }
+ nts_nKeys = 0;
+ for (int i=0; i<NTS_nKEYS; i++) {
+ struct NTS_Key *key = &nts_keys[i];
+ if (1 != fscanf(in, "I: %u\n", &key->I)) {
+ if (0 < nts_nKeys) break;
+ goto bail;
+ }
+ if (0 != fscanf(in, "K: ")) {
+ goto bail;
+ }
+ for (int j=0; j< K_length; j++) {
+ unsigned int temp;
+ if (1 != fscanf(in, "%02x", &temp)) {
+ goto bail;
+ }
+ key->K[j] = temp;
+ }
+ if (0 != fscanf(in, "\n")) {
+ goto bail;
+ }
+ nts_nKeys = i+1;
+ }
+ fclose(in);
+ msyslog(LOG_INFO, "NTS: Read cookie file, %d keys.", nts_nKeys);
+ return true;
bail:
- msyslog(LOG_ERR, "ERR: Error parsing cookie keys file");
- fclose(in);
- return false;
+ msyslog(LOG_ERR, "ERR: Error parsing cookie keys file");
+ fclose(in);
+ return false;
}
/* RFC 8915 describes a ratchet mode to make new keys
@@ -229,241 +229,241 @@ bool nts_read_cookie_keys(void) {
* they copy the key file to other systems and have them load it.
*/
void nts_make_cookie_key(void) {
- if (nts_nKeys < NTS_nKEYS) nts_nKeys++;
- for (int i=nts_nKeys-1; i>0; i--) {
- nts_keys[i] = nts_keys[i-1];
- }
- ntp_RAND_priv_bytes(nts_keys[0].K, K_length);
- ntp_RAND_bytes((uint8_t *)&nts_keys[0].I, sizeof(nts_keys[0].I));
- return;
+ if (nts_nKeys < NTS_nKEYS) nts_nKeys++;
+ for (int i=nts_nKeys-1; i>0; i--) {
+ nts_keys[i] = nts_keys[i-1];
+ }
+ ntp_RAND_priv_bytes(nts_keys[0].K, K_length);
+ ntp_RAND_bytes((uint8_t *)&nts_keys[0].I, sizeof(nts_keys[0].I));
+ return;
}
bool nts_write_cookie_keys(void) {
- const char *cookiefile = NTS_COOKIE_KEY_FILE;
- char tempfile[PATH_MAX];
- int fd;
- FILE *out;
- char errbuf[100];
- if (NULL != ntsconfig.KI)
- cookiefile = ntsconfig.KI;
- strlcpy(tempfile, cookiefile, sizeof(tempfile));
- strlcat(tempfile, "-tmp", sizeof(tempfile));
- fd = open(tempfile, O_CREAT|O_WRONLY, S_IRUSR|S_IWUSR);
- if (-1 == fd) {
- ntp_strerror_r(errno, errbuf, sizeof(errbuf));
- msyslog(LOG_ERR, "ERR: can't open %s: %s", tempfile, errbuf);
- return false;
- }
- out = fdopen(fd, "w");
- if (NULL == out) {
- ntp_strerror_r(errno, errbuf, sizeof(errbuf));
- msyslog(LOG_ERR, "ERR: can't fdopen %s: %s", tempfile, errbuf);
- close(fd);
- return false;
- }
-
- fprintf(out, "T: %lu\n", (unsigned long)K_time);
- fprintf(out, "L: %d\n", K_length);
- for (int i=0; i<nts_nKeys; i++) {
- struct NTS_Key *key = &nts_keys[i];
- fprintf(out, "I: %u\n", key->I);
- fprintf(out, "K: ");
- for (int j=0; j< K_length; j++) fprintf(out, "%02x", key->K[j]);
- fprintf(out, "\n");
- key++;
- }
- fclose(out);
+ const char *cookiefile = NTS_COOKIE_KEY_FILE;
+ char tempfile[PATH_MAX];
+ int fd;
+ FILE *out;
+ char errbuf[100];
+ if (NULL != ntsconfig.KI)
+ cookiefile = ntsconfig.KI;
+ strlcpy(tempfile, cookiefile, sizeof(tempfile));
+ strlcat(tempfile, "-tmp", sizeof(tempfile));
+ fd = open(tempfile, O_CREAT|O_WRONLY, S_IRUSR|S_IWUSR);
+ if (-1 == fd) {
+ ntp_strerror_r(errno, errbuf, sizeof(errbuf));
+ msyslog(LOG_ERR, "ERR: can't open %s: %s", tempfile, errbuf);
+ return false;
+ }
+ out = fdopen(fd, "w");
+ if (NULL == out) {
+ ntp_strerror_r(errno, errbuf, sizeof(errbuf));
+ msyslog(LOG_ERR, "ERR: can't fdopen %s: %s", tempfile, errbuf);
+ close(fd);
+ return false;
+ }
+
+ fprintf(out, "T: %lu\n", (unsigned long)K_time);
+ fprintf(out, "L: %d\n", K_length);
+ for (int i=0; i<nts_nKeys; i++) {
+ struct NTS_Key *key = &nts_keys[i];
+ fprintf(out, "I: %u\n", key->I);
+ fprintf(out, "K: ");
+ for (int j=0; j< K_length; j++) fprintf(out, "%02x", key->K[j]);
+ fprintf(out, "\n");
+ key++;
+ }
+ fclose(out);
if (rename(tempfile, cookiefile)) {
- ntp_strerror_r(errno, errbuf, sizeof(errbuf));
+ ntp_strerror_r(errno, errbuf, sizeof(errbuf));
msyslog(LOG_WARNING,
"LOG: Unable to rename temp cookie file %s to %s, %s",
tempfile, cookiefile, errbuf);
- return false;
- }
- return true;
+ return false;
+ }
+ return true;
}
-/* returns actual length */
+// returns actual length
int nts_make_cookie(uint8_t *cookie,
uint16_t aead,
uint8_t *c2s, uint8_t *s2c, int keylen) {
- uint8_t plaintext[NTS_MAX_COOKIELEN];
- uint8_t *nonce;
- int used, plainlength;
- bool ok;
- uint8_t * finger;
- uint32_t temp; /* keep 4 byte alignment */
- size_t left;
-
- if (NULL == cookie_ctx)
- return 0; /* We aren't initialized yet. */
-
- nts_cnt.cookie_make++;
-
- INSIST(keylen <= NTS_MAX_KEYLEN);
-
- /* collect plaintext
- * separate buffer avoids encrypt in place
- * but costs cache space
- */
- finger = plaintext;
- temp = aead;
- memcpy(finger, &temp, AEAD_LENGTH);
- finger += AEAD_LENGTH;
- memcpy(finger, c2s, keylen);
- finger += keylen;
- memcpy(finger, s2c, keylen);
- finger += keylen;
- plainlength = finger-plaintext;
-
- /* collect associated data */
- finger = cookie;
-
- memcpy(finger, &nts_keys[0].I, sizeof(nts_keys[0].I));
- finger += sizeof(nts_keys[0].I);
-
- nonce = finger;
- ntp_RAND_bytes(finger, NONCE_LENGTH);
- finger += NONCE_LENGTH;
-
- used = finger-cookie;
- left = NTS_MAX_COOKIELEN-used;
-
- nts_lock_cookielock();
-
- ok = AES_SIV_Encrypt(cookie_ctx,
- finger, &left, /* left: in: max out length, out: length used */
- nts_keys[0].K, K_length,
- nonce, NONCE_LENGTH,
- plaintext, plainlength,
- cookie, AD_LENGTH);
-
- nts_unlock_cookielock();
-
- if (!ok) {
- msyslog(LOG_ERR, "NTS: nts_make_cookie - Error from AES_SIV_Encrypt");
- /* I don't think this should happen,
- * so crash rather than work incorrectly.
- * Hal, 2019-Feb-17
- * Similar code in ntp_extens
- */
- exit(1);
- }
-
- used += left;
- INSIST(used <= NTS_MAX_COOKIELEN);
-
- return used;
+ uint8_t plaintext[NTS_MAX_COOKIELEN];
+ uint8_t *nonce;
+ int used, plainlength;
+ bool ok;
+ uint8_t * finger;
+ uint32_t temp; // keep 4 byte alignment
+ size_t left;
+
+ if (NULL == cookie_ctx)
+ return 0; // We aren't initialized yet.
+
+ nts_cnt.cookie_make++;
+
+ INSIST(keylen <= NTS_MAX_KEYLEN);
+
+ /* collect plaintext
+ * separate buffer avoids encrypt in place
+ * but costs cache space
+ */
+ finger = plaintext;
+ temp = aead;
+ memcpy(finger, &temp, AEAD_LENGTH);
+ finger += AEAD_LENGTH;
+ memcpy(finger, c2s, keylen);
+ finger += keylen;
+ memcpy(finger, s2c, keylen);
+ finger += keylen;
+ plainlength = finger-plaintext;
+
+ // collect associated data
+ finger = cookie;
+
+ memcpy(finger, &nts_keys[0].I, sizeof(nts_keys[0].I));
+ finger += sizeof(nts_keys[0].I);
+
+ nonce = finger;
+ ntp_RAND_bytes(finger, NONCE_LENGTH);
+ finger += NONCE_LENGTH;
+
+ used = finger-cookie;
+ left = NTS_MAX_COOKIELEN-used;
+
+ nts_lock_cookielock();
+
+ ok = AES_SIV_Encrypt(cookie_ctx,
+ finger, &left, // left: in: max out length, out: length used
+ nts_keys[0].K, K_length,
+ nonce, NONCE_LENGTH,
+ plaintext, plainlength,
+ cookie, AD_LENGTH);
+
+ nts_unlock_cookielock();
+
+ if (!ok) {
+ msyslog(LOG_ERR, "NTS: nts_make_cookie - Error from AES_SIV_Encrypt");
+ /* I don't think this should happen,
+ * so crash rather than work incorrectly.
+ * Hal, 2019-Feb-17
+ * Similar code in ntp_extens
+ */
+ exit(1);
+ }
+
+ used += left;
+ INSIST(used <= NTS_MAX_COOKIELEN);
+
+ return used;
}
-/* can't decrypt in place - that would trash the unauthenticated packet */
+// can't decrypt in place - that would trash the unauthenticated packet
bool nts_unpack_cookie(uint8_t *cookie, int cookielen,
uint16_t *aead,
uint8_t *c2s, uint8_t *s2c, int *keylen) {
- uint8_t *finger;
- uint8_t plaintext[NTS_MAX_COOKIELEN];
- uint8_t *nonce;
- uint32_t temp;
- size_t plainlength;
- int cipherlength;
- bool ok;
- struct NTS_Key *key;
- int i;
-
- if (NULL == cookie_ctx)
- return false; /* We aren't initialized yet. */
-
- if (0 == nts_nKeys) {
- nts_cnt.cookie_not_server++;
- return false; /* We are not a NTS enabled server. */
- }
-
- /* We may get garbage from the net */
- if (cookielen > NTS_MAX_COOKIELEN)
- return false;
-
- finger = cookie;
- key = NULL; /* squash uninitialized warning */
- for (i=0; i<nts_nKeys; i++) {
- key = &nts_keys[i];
- if (0 == memcmp(finger, &key->I, sizeof(key->I))) {
- break;
- }
- }
- nts_cnt.cookie_decode_total++; /* total attempts, includes too old */
- if (nts_nKeys == i) {
- nts_cnt.cookie_decode_too_old++;
- return false;
+ uint8_t *finger;
+ uint8_t plaintext[NTS_MAX_COOKIELEN];
+ uint8_t *nonce;
+ uint32_t temp;
+ size_t plainlength;
+ int cipherlength;
+ bool ok;
+ struct NTS_Key *key;
+ int i;
+
+ if (NULL == cookie_ctx)
+ return false; // We aren't initialized yet.
+
+ if (0 == nts_nKeys) {
+ nts_cnt.cookie_not_server++;
+ return false; // We are not a NTS enabled server.
+ }
+
+ // We may get garbage from the net
+ if (cookielen > NTS_MAX_COOKIELEN)
+ return false;
+
+ finger = cookie;
+ key = NULL; // squash uninitialized warning
+ for (i=0; i<nts_nKeys; i++) {
+ key = &nts_keys[i];
+ if (0 == memcmp(finger, &key->I, sizeof(key->I))) {
+ break;
+ }
+ }
+ nts_cnt.cookie_decode_total++; // total attempts, includes too old
+ if (nts_nKeys == i) {
+ nts_cnt.cookie_decode_too_old++;
+ return false;
+ }
+ if (0 == i) {
+ nts_cnt.cookie_decode_current++;
+ } else if (1 == i) {
+ nts_cnt.cookie_decode_old++;
+ } else if (2 == i) {
+ nts_cnt.cookie_decode_old2++;
+ } else {
+ nts_cnt.cookie_decode_older++;
}
- if (0 == i) {
- nts_cnt.cookie_decode_current++;
- } else if (1 == i) {
- nts_cnt.cookie_decode_old++;
- } else if (2 == i) {
- nts_cnt.cookie_decode_old2++;
- } else {
- nts_cnt.cookie_decode_older++;
- }
#if 0
- if (1<i) {
- /* Hack for debugging */
- /* Beware: DoS possibility on a public server */
- msyslog(LOG_INFO, "NTS: Old cookie: %d days.", i);
- }
+ if (1<i) {
+ // Hack for debugging
+ // Beware: DoS possibility on a public server
+ msyslog(LOG_INFO, "NTS: Old cookie: %d days.", i);
+ }
#endif
- finger += sizeof(key->I);
- nonce = finger;
- finger += NONCE_LENGTH;
-
- // require(AD_LENGTH==finger-cookie);
+ finger += sizeof(key->I);
+ nonce = finger;
+ finger += NONCE_LENGTH;
- cipherlength = cookielen - AD_LENGTH;
- plainlength = NTS_MAX_COOKIELEN;
+ // require(AD_LENGTH==finger-cookie);
- nts_lock_cookielock();
+ cipherlength = cookielen - AD_LENGTH;
+ plainlength = NTS_MAX_COOKIELEN;
- ok = AES_SIV_Decrypt(cookie_ctx,
- plaintext, &plainlength,
- key->K, K_length,
- nonce, NONCE_LENGTH,
- finger, cipherlength,
- cookie, AD_LENGTH);
+ nts_lock_cookielock();
- nts_unlock_cookielock();
+ ok = AES_SIV_Decrypt(cookie_ctx,
+ plaintext, &plainlength,
+ key->K, K_length,
+ nonce, NONCE_LENGTH,
+ finger, cipherlength,
+ cookie, AD_LENGTH);
- if (!ok) {
- nts_cnt.cookie_decode_error++;
- return false;
- }
+ nts_unlock_cookielock();
- *keylen = (plainlength-AEAD_LENGTH)/2;
- finger = plaintext;
- memcpy(&temp, finger, AEAD_LENGTH);
- *aead = temp;
- finger += AEAD_LENGTH;
- memcpy(c2s, finger, *keylen);
- finger += *keylen;
- memcpy(s2c, finger, *keylen);
- finger += *keylen;
+ if (!ok) {
+ nts_cnt.cookie_decode_error++;
+ return false;
+ }
- return true;
+ *keylen = (plainlength-AEAD_LENGTH)/2;
+ finger = plaintext;
+ memcpy(&temp, finger, AEAD_LENGTH);
+ *aead = temp;
+ finger += AEAD_LENGTH;
+ memcpy(c2s, finger, *keylen);
+ finger += *keylen;
+ memcpy(s2c, finger, *keylen);
+ finger += *keylen;
+
+ return true;
}
void nts_lock_cookielock(void) {
- int err = pthread_mutex_lock(&cookie_lock);
- if (0 != err) {
- msyslog(LOG_ERR, "ERR: Can't lock cookie_lock: %d", err);
- exit(2);
- }
+ int err = pthread_mutex_lock(&cookie_lock);
+ if (0 != err) {
+ msyslog(LOG_ERR, "ERR: Can't lock cookie_lock: %d", err);
+ exit(2);
+ }
}
void nts_unlock_cookielock(void) {
- int err = pthread_mutex_unlock(&cookie_lock);
- if (0 != err) {
- msyslog(LOG_ERR, "ERR: Can't unlock cookie_lock: %d", err);
- exit(2);
- }
+ int err = pthread_mutex_unlock(&cookie_lock);
+ if (0 != err) {
+ msyslog(LOG_ERR, "ERR: Can't unlock cookie_lock: %d", err);
+ exit(2);
+ }
}
-/* end */
+// end
=====================================
tests/ntpd/nts_cookie.c
=====================================
@@ -22,99 +22,99 @@ TEST_SETUP(nts_cookie) {}
TEST_TEAR_DOWN(nts_cookie) {}
TEST(nts_cookie, nts_make_cookie_key) {
- /* init */
- struct NTS_Key k0 = {.K={1, 2, 3, 4, 5}, .I=123};
- struct NTS_Key k1 = {.K={10, 20, 30, 40, 50}, .I=456};
- /* copy to key variables */
- nts_keys[0] = k0;
- nts_keys[1] = k1;
- /* run test */
- nts_nKeys = 2;
- nts_make_cookie_key(); /* push k0 to k1 */
- /* check that K[1] now equals former-K[0] */
- TEST_ASSERT_EQUAL_UINT8_ARRAY(nts_keys[1].K, k0.K, NTS_MAX_KEYLEN);
- TEST_ASSERT_EQUAL(nts_keys[1].I, k0.I);
- /* check that K[0] does not equal former-K[0] */
- /* There is no "TEST UNEQUAL", do it manually */
- bool equal = true;
- for (unsigned int i = 0; i < NTS_MAX_KEYLEN; i++) {
- if (nts_keys[0].K[i] != k0.K[i]) {
- equal = false;
- break;
- }
- }
- TEST_ASSERT_EQUAL(false, equal);
- /* Check that I[0] does not equal former-I[0] */
- TEST_ASSERT_NOT_EQUAL(nts_keys[0].I, k0.I);
+ // init
+ struct NTS_Key k0 = {.K={1, 2, 3, 4, 5}, .I=123};
+ struct NTS_Key k1 = {.K={10, 20, 30, 40, 50}, .I=456};
+ // copy to key variables
+ nts_keys[0] = k0;
+ nts_keys[1] = k1;
+ // run test
+ nts_nKeys = 2;
+ nts_make_cookie_key(); // push k0 to k1
+ // check that K[1] now equals former-K[0]
+ TEST_ASSERT_EQUAL_UINT8_ARRAY(nts_keys[1].K, k0.K, NTS_MAX_KEYLEN);
+ TEST_ASSERT_EQUAL(nts_keys[1].I, k0.I);
+ // check that K[0] does not equal former-K[0]
+ // There is no "TEST UNEQUAL", do it manually
+ bool equal = true;
+ for (unsigned int i = 0; i < NTS_MAX_KEYLEN; i++) {
+ if (nts_keys[0].K[i] != k0.K[i]) {
+ equal = false;
+ break;
+ }
+ }
+ TEST_ASSERT_EQUAL(false, equal);
+ // Check that I[0] does not equal former-I[0]
+ TEST_ASSERT_NOT_EQUAL(nts_keys[0].I, k0.I);
}
TEST(nts_cookie, nts_make_unpack_cookie) {
- /* init */
- uint8_t cookie[NTS_MAX_COOKIELEN];
- /* Using 16 bytes in test for ease of handling */
- uint8_t c2s[16] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
- uint8_t s2c[16] = {16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
- uint8_t c2s_2[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
- uint8_t s2c_2[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
- int len;
- int keylen;
- bool ok;
- uint16_t aead; /* retrieved on unpack */
- /* Init for cookie_ctx */
- nts_cookie_init();
- nts_nKeys = 0;
- nts_make_cookie_key();
- /* Test */
- len = nts_make_cookie(cookie, AEAD_AES_SIV_CMAC_256, c2s, s2c, sizeof(c2s));
- TEST_ASSERT_EQUAL(72, len);
- /* Very limited in what data can be directly checked here */
- /* Reverse the test */
- ok = nts_unpack_cookie(cookie, len, &aead, c2s_2, s2c_2, &keylen);
- TEST_ASSERT_EQUAL(true, ok);
- TEST_ASSERT_EQUAL(AEAD_AES_SIV_CMAC_256, aead);
- TEST_ASSERT_EQUAL(16, keylen);
- TEST_ASSERT_EQUAL_UINT8_ARRAY(c2s, c2s_2, 16);
- TEST_ASSERT_EQUAL_UINT8_ARRAY(s2c, s2c_2, 16);
+ // init
+ uint8_t cookie[NTS_MAX_COOKIELEN];
+ // Using 16 bytes in test for ease of handling
+ uint8_t c2s[16] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
+ uint8_t s2c[16] = {16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
+ uint8_t c2s_2[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
+ uint8_t s2c_2[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
+ int len;
+ int keylen;
+ bool ok;
+ uint16_t aead; // retrieved on unpack
+ // Init for cookie_ctx
+ nts_cookie_init();
+ nts_nKeys = 0;
+ nts_make_cookie_key();
+ // Test
+ len = nts_make_cookie(cookie, AEAD_AES_SIV_CMAC_256, c2s, s2c, sizeof(c2s));
+ TEST_ASSERT_EQUAL(72, len);
+ // Very limited in what data can be directly checked here
+ // Reverse the test
+ ok = nts_unpack_cookie(cookie, len, &aead, c2s_2, s2c_2, &keylen);
+ TEST_ASSERT_EQUAL(true, ok);
+ TEST_ASSERT_EQUAL(AEAD_AES_SIV_CMAC_256, aead);
+ TEST_ASSERT_EQUAL(16, keylen);
+ TEST_ASSERT_EQUAL_UINT8_ARRAY(c2s, c2s_2, 16);
+ TEST_ASSERT_EQUAL_UINT8_ARRAY(s2c, s2c_2, 16);
}
const char *cookie_file_name = "test-cookie-keys";
TEST(nts_cookie, nts_read_write_cookies) {
- struct NTS_Key k0, k1, k2;
- bool ok;
- ntsconfig.KI = cookie_file_name;
- nts_nKeys = 0;
- nts_make_cookie_key();
- nts_make_cookie_key();
- nts_make_cookie_key();
- k0 = nts_keys[0];
- k1 = nts_keys[1];
- k2 = nts_keys[2];
- TEST_ASSERT_EQUAL(nts_nKeys, 3);
- ok = nts_write_cookie_keys();
- TEST_ASSERT_EQUAL(true, ok);
- nts_make_cookie_key(); /* scramble things */
- ZERO(nts_keys);
- nts_nKeys = 377;
- ok = nts_read_cookie_keys();
- TEST_ASSERT_EQUAL(true, ok);
- TEST_ASSERT_EQUAL(nts_nKeys, 3);
- TEST_ASSERT_EQUAL_UINT8_ARRAY(nts_keys[0].K, k0.K, NTS_MAX_KEYLEN);
- TEST_ASSERT_EQUAL(nts_keys[0].I, k0.I);
- TEST_ASSERT_EQUAL_UINT8_ARRAY(nts_keys[1].K, k1.K, NTS_MAX_KEYLEN);
- TEST_ASSERT_EQUAL(nts_keys[1].I, k1.I);
- TEST_ASSERT_EQUAL_UINT8_ARRAY(nts_keys[2].K, k2.K, NTS_MAX_KEYLEN);
- TEST_ASSERT_EQUAL(nts_keys[2].I, k2.I);
+ struct NTS_Key k0, k1, k2;
+ bool ok;
+ ntsconfig.KI = cookie_file_name;
+ nts_nKeys = 0;
+ nts_make_cookie_key();
+ nts_make_cookie_key();
+ nts_make_cookie_key();
+ k0 = nts_keys[0];
+ k1 = nts_keys[1];
+ k2 = nts_keys[2];
+ TEST_ASSERT_EQUAL(nts_nKeys, 3);
+ ok = nts_write_cookie_keys();
+ TEST_ASSERT_EQUAL(true, ok);
+ nts_make_cookie_key(); // scramble things
+ ZERO(nts_keys);
+ nts_nKeys = 377;
+ ok = nts_read_cookie_keys();
+ TEST_ASSERT_EQUAL(true, ok);
+ TEST_ASSERT_EQUAL(nts_nKeys, 3);
+ TEST_ASSERT_EQUAL_UINT8_ARRAY(nts_keys[0].K, k0.K, NTS_MAX_KEYLEN);
+ TEST_ASSERT_EQUAL(nts_keys[0].I, k0.I);
+ TEST_ASSERT_EQUAL_UINT8_ARRAY(nts_keys[1].K, k1.K, NTS_MAX_KEYLEN);
+ TEST_ASSERT_EQUAL(nts_keys[1].I, k1.I);
+ TEST_ASSERT_EQUAL_UINT8_ARRAY(nts_keys[2].K, k2.K, NTS_MAX_KEYLEN);
+ TEST_ASSERT_EQUAL(nts_keys[2].I, k2.I);
}
TEST_GROUP_RUNNER(nts_cookie) {
- RUN_TEST_CASE(nts_cookie, nts_make_unpack_cookie);
- RUN_TEST_CASE(nts_cookie, nts_make_cookie_key);
- RUN_TEST_CASE(nts_cookie, nts_read_write_cookies);
- /* This test gets run as root during install
- * that leaves the cookie file that we can't read/write
- * so clean it up now.
- * If we crash, we don't get here so the evidence is still
- * left around in case it helps debugging. */
- unlink(cookie_file_name);
+ RUN_TEST_CASE(nts_cookie, nts_make_unpack_cookie);
+ RUN_TEST_CASE(nts_cookie, nts_make_cookie_key);
+ RUN_TEST_CASE(nts_cookie, nts_read_write_cookies);
+ /* This test gets run as root during install
+ * that leaves the cookie file that we can't read/write
+ * so clean it up now.
+ * If we crash, we don't get here so the evidence is still
+ * left around in case it helps debugging. */
+ unlink(cookie_file_name);
}
View it on GitLab: https://gitlab.com/NTPsec/ntpsec/-/compare/2dee877da4625caca807b0c94cac84bf6a21ca1c...2d5d803d127022e1e209a85a622c107ffa4f90c2
--
View it on GitLab: https://gitlab.com/NTPsec/ntpsec/-/compare/2dee877da4625caca807b0c94cac84bf6a21ca1c...2d5d803d127022e1e209a85a622c107ffa4f90c2
You're receiving this email because of your account on gitlab.com. Manage all notifications: https://gitlab.com/-/profile/notifications | Help: https://gitlab.com/help
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.ntpsec.org/pipermail/vc/attachments/20260806/9b6bb150/attachment-0001.htm>
More information about the vc
mailing list