22 #define PROTOHMAC_PAD1_VALUE 0x36
23 #define PROTOHMAC_PAD2_VALUE 0x5C
24 #define PROTOHMAC_PAD1 "\x36\x36\x36\x36\x36\x36\x36\x36" \
25 "\x36\x36\x36\x36\x36\x36\x36\x36" \
26 "\x36\x36\x36\x36\x36\x36\x36\x36" \
27 "\x36\x36\x36\x36\x36\x36\x36\x36" \
28 "\x36\x36\x36\x36\x36\x36\x36\x36" \
29 "\x36\x36\x36\x36\x36\x36\x36\x36"
30 #define PROTOHMAC_PAD2 "\x5C\x5C\x5C\x5C\x5C\x5C\x5C\x5C" \
31 "\x5C\x5C\x5C\x5C\x5C\x5C\x5C\x5C" \
32 "\x5C\x5C\x5C\x5C\x5C\x5C\x5C\x5C" \
33 "\x5C\x5C\x5C\x5C\x5C\x5C\x5C\x5C" \
34 "\x5C\x5C\x5C\x5C\x5C\x5C\x5C\x5C" \
35 "\x5C\x5C\x5C\x5C\x5C\x5C\x5C\x5C"
64 assert(
isWritePtr( dataLength,
sizeof(
int ) ) );
67 REQUIRES( type >= 0 && type <= 255 );
74 memset( data, 0,
min( 16, dataMaxLength ) );
79 sMemOpen( &stream, data, dataMaxLength );
80 writeUint64( &stream, seqNo );
81 sputc( &stream, type );
83 sputc( &stream, version );
84 status = writeUint16( &stream, payloadLength );
86 *dataLength = stell( &stream );
87 sMemDisconnect( &stream );
117 assert(
isWritePtr( dataLength,
sizeof(
int ) ) );
122 payloadLength <= sessionInfoPtr->sendBufSize && \
123 payloadLength <= dataMaxLength );
136 length + padSize + 1 <= dataMaxLength );
139 for( i = 0; i < padSize + 1; i++ )
161 CRYPT_IATTRIBUTE_ICV );
181 assert(
isWritePtr( processedDataLength,
sizeof(
int ) ) );
184 dataLength <= sessionInfoPtr->receiveBufEnd && \
188 *processedDataLength = 0;
197 "Packet decryption failed" ) );
215 "Bad message ICV for packet type %d, length %d",
216 data[ 0 ], length ) );
223 *processedDataLength =
length;
245 padSize =
byteToInt( data[ dataLength - 1 ] );
246 if( padSize < 0 || padSize > 255 || \
252 "Invalid encryption padding value 0x%02X (%d)",
253 padSize, padSize ) );
255 length -= padSize + 1;
260 "Encryption padding adjustment value %d is greater "
261 "than packet length %d", padSize, dataLength ) );
270 for( i = 0; i < padSize; i++ )
272 if( data[ length + i ] != padSize )
276 "Invalid encryption padding byte 0x%02X at "
277 "position %d, should be 0x%02X",
278 data[ length + i ], length + i, padSize ) );
282 *processedDataLength =
length;
307 const void *macSecret,
320 assert(
isReadPtr( macSecret, macSecretLength ) );
321 assert( ( data == NULL && dataLength == 0 ) || \
330 REQUIRES( ( data == NULL && dataLength == 0 ) || \
333 REQUIRES( type >= 0 && type <= 255 );
337 sMemOpen( &stream, buffer + padSize, 128 - padSize );
338 writeUint64( &stream, seqNo );
339 sputc( &stream, type );
340 status = writeUint16( &stream, dataLength );
342 length = stell( &stream );
343 sMemDisconnect( &stream );
379 padSize + msgData.
length );
391 SSL_INFO *sslInfo = sessionInfoPtr->sessionSSL;
397 assert(
isWritePtr( dataLength,
sizeof(
int ) ) );
399 REQUIRES( dataMaxLength > 0 && dataMaxLength < MAX_INTLENGTH );
401 payloadLength + sessionInfoPtr->authBlocksize <= dataMaxLength );
402 REQUIRES( type >= 0 && type <= 255 );
408 status = macDataSSL( sessionInfoPtr->iAuthOutContext,
409 sessionInfoPtr->integrityAlgo,
410 sslInfo->macWriteSecret,
411 sessionInfoPtr->authBlocksize, sslInfo->
writeSeqNo,
412 data, payloadLength, type );
421 sessionInfoPtr->authBlocksize );
427 *dataLength = payloadLength + msgData.
length;
434 IN_BUFFER( dataLength )
const void *data,
440 SSL_INFO *sslInfo = sessionInfoPtr->sessionSSL;
447 REQUIRES( dataLength > 0 && dataLength < MAX_INTLENGTH );
449 payloadLength + sessionInfoPtr->authBlocksize <= dataLength );
450 REQUIRES( type >= 0 && type <= 255 );
456 if( payloadLength <= 0 )
458 status = macDataSSL( sessionInfoPtr->iAuthInContext,
459 sessionInfoPtr->integrityAlgo,
460 sslInfo->macReadSecret,
461 sessionInfoPtr->authBlocksize,
466 status = macDataSSL( sessionInfoPtr->iAuthInContext,
467 sessionInfoPtr->integrityAlgo,
468 sslInfo->macReadSecret,
469 sessionInfoPtr->authBlocksize,
470 sslInfo->
readSeqNo, data, payloadLength, type );
481 sessionInfoPtr->authBlocksize );
494 "Bad message MAC for packet type %d, length %d",
495 type, dataLength ) );
526 BYTE buffer[ 64 + 8 ];
529 assert( ( data == NULL && dataLength == 0 ) || \
536 REQUIRES( ( data == NULL && dataLength == 0 ) || \
539 REQUIRES( type >= 0 && type <= 255 );
542 status = writePacketMetadata( buffer, 64, &length, type, seqNo, version,
563 OUT_BUFFER( dataMaxLength, *dataLength )
void *data,
569 SSL_INFO *sslInfo = sessionInfoPtr->sessionSSL;
575 assert(
isWritePtr( dataLength,
sizeof(
int ) ) );
577 REQUIRES( dataMaxLength > 0 && dataMaxLength < MAX_INTLENGTH );
579 payloadLength + sessionInfoPtr->authBlocksize <= dataMaxLength );
580 REQUIRES( type >= 0 && type <= 255 );
586 status = macDataTLS( sessionInfoPtr->iAuthOutContext, sslInfo->
writeSeqNo,
587 sessionInfoPtr->version, data, payloadLength, type );
596 sessionInfoPtr->authBlocksize );
602 *dataLength = payloadLength + msgData.
length;
609 IN_BUFFER( dataLength )
const void *data,
615 SSL_INFO *sslInfo = sessionInfoPtr->sessionSSL;
622 REQUIRES( dataLength > 0 && dataLength < MAX_INTLENGTH );
624 payloadLength + sessionInfoPtr->authBlocksize <= dataLength );
625 REQUIRES( type >= 0 && type <= 255 );
631 if( payloadLength <= 0 )
633 status = macDataTLS( sessionInfoPtr->iAuthInContext,
634 sslInfo->
readSeqNo, sessionInfoPtr->version,
639 status = macDataTLS( sessionInfoPtr->iAuthInContext,
640 sslInfo->
readSeqNo, sessionInfoPtr->version,
641 data, payloadLength, type );
652 sessionInfoPtr->authBlocksize );
665 "Bad message MAC for packet type %d, length %d",
666 type, dataLength ) );
693 BYTE buffer[ 64 + 8 ];
701 REQUIRES( type >= 0 && type <= 255 );
704 status = writePacketMetadata( buffer, 64, &length, type, seqNo,
705 version, payloadLength );
713 &msgData, CRYPT_IATTRIBUTE_AAD ) );
743 IN_BUFFER( dataLength )
const void *data,
751 REQUIRES( dataLength > 0 && dataLength < MAX_INTLENGTH );
791 const int dataLength = sMemDataLeft( stream );
798 REQUIRES( dataLength > 0 && dataLength < MAX_INTLENGTH );
804 status = sMemGetDataBlock( stream, &data, dataLength );
808 status = hashHSData( handshakeInfo, data, dataLength );
819 const int dataLength = stell( stream ) - dataStart;
826 REQUIRES( offset >= 0 && offset < MAX_INTLENGTH );
827 REQUIRES( dataLength > 0 && dataLength < MAX_INTLENGTH );
831 status = sMemGetDataBlockAbs( stream, dataStart, &data, dataLength );
835 status = hashHSData( handshakeInfo, data, dataLength );
858 const int hashValuesMaxLen,
860 IN_BUFFER( labelLength )
const char *label,
861 IN_RANGE( 1, 64 )
const int labelLength,
868 assert(
isWritePtr( hashValues, hashValuesMaxLen ) );
869 assert(
isWritePtr( hashValuesLen,
sizeof(
int ) ) );
870 assert(
isReadPtr( label, labelLength ) );
871 assert(
isReadPtr( masterSecret, masterSecretLen ) );
877 REQUIRES( labelLength > 0 && labelLength <= 64 );
950 OUT_BUFFER( hashValuesMaxLen, *hashValuesLen )
953 const int hashValuesMaxLen,
955 IN_BUFFER( labelLength )
const char *label,
956 IN_RANGE( 1, 64 )
const int labelLength,
965 assert(
isWritePtr( hashValues, hashValuesMaxLen ) );
966 assert(
isWritePtr( hashValuesLen,
sizeof(
int ) ) );
967 assert(
isReadPtr( label, labelLength ) );
968 assert(
isReadPtr( masterSecret, masterSecretLen ) );
974 REQUIRES( labelLength > 0 && labelLength <= 64 && \
981 memcpy( hashBuffer, label, labelLength );
1019 OUT_BUFFER( hashValuesMaxLen, *hashValuesLen ) \
1022 const int hashValuesMaxLen,
1024 IN_BUFFER( labelLength )
const char *label,
1025 IN_RANGE( 1, 64 )
const int labelLength,
1034 assert(
isWritePtr( hashValues, hashValuesMaxLen ) );
1035 assert(
isWritePtr( hashValuesLen,
sizeof(
int ) ) );
1036 assert(
isReadPtr( label, labelLength ) );
1037 assert(
isReadPtr( masterSecret, masterSecretLen ) );
1042 REQUIRES( labelLength > 0 && labelLength <= 64 && \
1048 memcpy( hashBuffer, label, labelLength );
1073 labelLength + macSize, 1 );
1166 memcpy( nonceBuffer,
"certificate verify", 18 );
1167 memcpy( nonceBuffer + 18, handshakeInfo->clientNonce,
SSL_NONCE_SIZE );
1168 memcpy( nonceBuffer + 18 +
SSL_NONCE_SIZE, handshakeInfo->serverNonce,
1187 int createCertVerifyHash(
const SESSION_INFO *sessionInfoPtr,
1202 status = cloneHashContext( handshakeInfo->sha2context, &iHashContext );
1217 int createCertVerify(
const SESSION_INFO *sessionInfoPtr,
1229 status = sMemGetDataBlockRemaining( stream, &dataPtr, &dataLength );
1242 status = createCertVerifyAltHash( handshakeInfo, &iHashContext );
1246 status = iCryptCreateSignature( dataPtr,
1249 iHashContext, NULL );
1254 status = iCryptCreateSignature( dataPtr,
1256 CRYPT_IFORMAT_TLS12, sessionInfoPtr->
privateKey,
1257 handshakeInfo->certVerifyContext, NULL );
1263 status = sSkip( stream, length );
1268 int checkCertVerify(
const SESSION_INFO *sessionInfoPtr,
1272 const int sigLength )
1285 status = sMemGetDataBlock( stream, &dataPtr, sigLength );
1299 status = createCertVerifyAltHash( handshakeInfo, &iHashContext );
1303 status = iCryptCheckSignature( dataPtr,
1311 status = iCryptCheckSignature( dataPtr,
1314 handshakeInfo->certVerifyContext,
CRYPT_UNUSED, NULL );
1318 #ifdef CONFIG_SUITEB_TESTS
1328 DEBUG_PRINT((
"Verified client's P%d authentication.\n",
1360 assert(
isReadPtr( keyData, keyDataLength ) );
1378 if( hashParam != 0 )
1392 memcpy( nonceBuffer, handshakeInfo->clientNonce,
SSL_NONCE_SIZE );
1393 memcpy( nonceBuffer +
SSL_NONCE_SIZE, handshakeInfo->serverNonce,
1417 IN_BUFFER( keyDataLength )
const void *keyData,
1427 assert(
isReadPtr( keyData, keyDataLength ) );
1432 status = createKeyexHash( handshakeInfo, &shaContext,
1435 handshakeInfo->keyexSigHashAlgoParam, keyData, keyDataLength );
1440 status = createKeyexHash( handshakeInfo, &md5Context,
1453 status = sMemGetDataBlockRemaining( stream, &dataPtr, &dataLength );
1458 status = iCryptCreateSignature( dataPtr,
1461 &sigLength, CRYPT_IFORMAT_TLS12,
1462 sessionInfoPtr->privateKey,
1471 status = iCryptCreateSignature( dataPtr,
1474 &sigLength, CRYPT_IFORMAT_SSL,
1475 sessionInfoPtr->privateKey,
1476 md5Context, &sigParams );
1485 return( sSkip( stream, sigLength ) );
1492 IN_BUFFER( keyDataLength )
const void *keyData,
1505 assert(
isReadPtr( keyData, keyDataLength ) );
1511 if( sMemDataLeft( stream ) < ( isECC ? \
1514 status = sMemGetDataBlockRemaining( stream, &dataPtr, &dataLength );
1525 static const MAP_TABLE hashAlgoIDTbl[] = {
1526 #ifdef CONFIG_SUITEB
1534 int cryptHashAlgo, tlsHashAlgo;
1539 status = tlsHashAlgo = sgetc( stream );
1542 ( void ) sgetc( stream );
1543 status = mapValue( tlsHashAlgo, &cryptHashAlgo, hashAlgoIDTbl,
1547 hashAlgo = cryptHashAlgo;
1553 status = createKeyexHash( handshakeInfo, &shaContext, hashAlgo,
1554 hashParam, keyData, keyDataLength );
1559 status = createKeyexHash( handshakeInfo, &md5Context,
1574 status = iCryptCheckSignature( dataPtr,
1576 CRYPT_IFORMAT_TLS12,
1577 sessionInfoPtr->iKeyexCryptContext,
1582 status = iCryptCheckSignature( dataPtr,
1585 sessionInfoPtr->iKeyexCryptContext,
1586 md5Context, shaContext, NULL );
1615 if( sigKeySize < keyexKeySize -
bitsToBytes( 8 ) )
1617 #ifdef CONFIG_SUITEB
1621 #ifdef CONFIG_SUITEB_TESTS
1622 DEBUG_PRINT((
"Verified server's P%d keyex with P%d signature.\n",
1633 if( sigKeySize < keyexKeySize -
bitsToBytes( 32 ) )
1638 return( readUniversal16( stream ) );